-
Notifications
You must be signed in to change notification settings - Fork 0
/
zic.c
2772 lines (2592 loc) · 63.4 KB
/
zic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** This file is in the public domain, so clarified as of
** 2006-07-17 by Arthur David Olson.
*/
static char elsieid[] = "@(#)zic.c 8.25";
#include "private.h"
#include "locale.h"
#include "tzfile.h"
#define ZIC_VERSION '2'
typedef int_fast64_t zic_t;
#ifndef ZIC_MAX_ABBR_LEN_WO_WARN
#define ZIC_MAX_ABBR_LEN_WO_WARN 6
#endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
#if HAVE_SYS_STAT_H
#include "sys/stat.h"
#endif
#ifdef S_IRUSR
#define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
#else
#define MKDIR_UMASK 0755
#endif
/*
** On some ancient hosts, predicates like `isspace(C)' are defined
** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
** which says they are defined only if C == ((unsigned char) C) || C == EOF.
** Neither the C Standard nor Posix require that `isascii' exist.
** For portability, we check both ancient and modern requirements.
** If isascii is not defined, the isascii check succeeds trivially.
*/
#include "ctype.h"
#ifndef isascii
#define isascii(x) 1
#endif
#define OFFSET_STRLEN_MAXIMUM (7 + INT_STRLEN_MAXIMUM(long))
#define RULE_STRLEN_MAXIMUM 8 /* "Mdd.dd.d" */
#define end(cp) (strchr((cp), '\0'))
struct rule {
const char * r_filename;
int r_linenum;
const char * r_name;
int r_loyear; /* for example, 1986 */
int r_hiyear; /* for example, 1986 */
const char * r_yrtype;
int r_lowasnum;
int r_hiwasnum;
int r_month; /* 0..11 */
int r_dycode; /* see below */
int r_dayofmonth;
int r_wday;
long r_tod; /* time from midnight */
int r_todisstd; /* above is standard time if TRUE */
/* or wall clock time if FALSE */
int r_todisgmt; /* above is GMT if TRUE */
/* or local time if FALSE */
long r_stdoff; /* offset from standard time */
const char * r_abbrvar; /* variable part of abbreviation */
int r_todo; /* a rule to do (used in outzone) */
zic_t r_temp; /* used in outzone */
};
/*
** r_dycode r_dayofmonth r_wday
*/
#define DC_DOM 0 /* 1..31 */ /* unused */
#define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
#define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
struct zone {
const char * z_filename;
int z_linenum;
const char * z_name;
long z_gmtoff;
const char * z_rule;
const char * z_format;
long z_stdoff;
struct rule * z_rules;
int z_nrules;
struct rule z_untilrule;
zic_t z_untiltime;
};
extern int getopt(int argc, char * const argv[],
const char * options);
extern int link(const char * fromname, const char * toname);
extern char * optarg;
extern int optind;
static void addtt(zic_t starttime, int type);
static int addtype(long gmtoff, const char * abbr, int isdst,
int ttisstd, int ttisgmt);
static void leapadd(zic_t t, int positive, int rolling, int count);
static void adjleap(void);
static void associate(void);
static int ciequal(const char * ap, const char * bp);
static void convert(long val, char * buf);
static void convert64(zic_t val, char * buf);
static void dolink(const char * fromfield, const char * tofield);
static void doabbr(char * abbr, const char * format,
const char * letters, int isdst, int doquotes);
static void eat(const char * name, int num);
static void eats(const char * name, int num,
const char * rname, int rnum);
static long eitol(int i);
static void error(const char * message);
static char ** getfields(char * buf);
static long gethms(const char * string, const char * errstrng,
int signable);
static void infile(const char * filename);
static void inleap(char ** fields, int nfields);
static void inlink(char ** fields, int nfields);
static void inrule(char ** fields, int nfields);
static int inzcont(char ** fields, int nfields);
static int inzone(char ** fields, int nfields);
static int inzsub(char ** fields, int nfields, int iscont);
static int is32(zic_t x);
static int itsabbr(const char * abbr, const char * word);
static int itsdir(const char * name);
static int lowerit(int c);
static char * memcheck(char * tocheck);
static int mkdirs(char * filename);
static void newabbr(const char * abbr);
static long oadd(long t1, long t2);
static void outzone(const struct zone * zp, int ntzones);
static void puttzcode(long code, FILE * fp);
static void puttzcode64(zic_t code, FILE * fp);
static int rcomp(const void * leftp, const void * rightp);
static zic_t rpytime(const struct rule * rp, int wantedy);
static void rulesub(struct rule * rp,
const char * loyearp, const char * hiyearp,
const char * typep, const char * monthp,
const char * dayp, const char * timep);
static int stringoffset(char * result, long offset);
static int stringrule(char * result, const struct rule * rp,
long dstoff, long gmtoff);
static void stringzone(char * result,
const struct zone * zp, int ntzones);
static void setboundaries(void);
static zic_t tadd(zic_t t1, long t2);
static void usage(FILE *stream, int status);
static void writezone(const char * name, const char * string);
static int yearistype(int year, const char * type);
static int charcnt;
static int errors;
static const char * filename;
static int leapcnt;
static int leapseen;
static int leapminyear;
static int leapmaxyear;
static int linenum;
static int max_abbrvar_len;
static int max_format_len;
static zic_t max_time;
static int max_year;
static zic_t min_time;
static int min_year;
static int noise;
static const char * rfilename;
static int rlinenum;
static const char * progname;
static int timecnt;
static int typecnt;
/*
** Line codes.
*/
#define LC_RULE 0
#define LC_ZONE 1
#define LC_LINK 2
#define LC_LEAP 3
/*
** Which fields are which on a Zone line.
*/
#define ZF_NAME 1
#define ZF_GMTOFF 2
#define ZF_RULE 3
#define ZF_FORMAT 4
#define ZF_TILYEAR 5
#define ZF_TILMONTH 6
#define ZF_TILDAY 7
#define ZF_TILTIME 8
#define ZONE_MINFIELDS 5
#define ZONE_MAXFIELDS 9
/*
** Which fields are which on a Zone continuation line.
*/
#define ZFC_GMTOFF 0
#define ZFC_RULE 1
#define ZFC_FORMAT 2
#define ZFC_TILYEAR 3
#define ZFC_TILMONTH 4
#define ZFC_TILDAY 5
#define ZFC_TILTIME 6
#define ZONEC_MINFIELDS 3
#define ZONEC_MAXFIELDS 7
/*
** Which files are which on a Rule line.
*/
#define RF_NAME 1
#define RF_LOYEAR 2
#define RF_HIYEAR 3
#define RF_COMMAND 4
#define RF_MONTH 5
#define RF_DAY 6
#define RF_TOD 7
#define RF_STDOFF 8
#define RF_ABBRVAR 9
#define RULE_FIELDS 10
/*
** Which fields are which on a Link line.
*/
#define LF_FROM 1
#define LF_TO 2
#define LINK_FIELDS 3
/*
** Which fields are which on a Leap line.
*/
#define LP_YEAR 1
#define LP_MONTH 2
#define LP_DAY 3
#define LP_TIME 4
#define LP_CORR 5
#define LP_ROLL 6
#define LEAP_FIELDS 7
/*
** Year synonyms.
*/
#define YR_MINIMUM 0
#define YR_MAXIMUM 1
#define YR_ONLY 2
static struct rule * rules;
static int nrules; /* number of rules */
static struct zone * zones;
static int nzones; /* number of zones */
struct link {
const char * l_filename;
int l_linenum;
const char * l_from;
const char * l_to;
};
static struct link * links;
static int nlinks;
struct lookup {
const char * l_word;
const int l_value;
};
static struct lookup const * byword(const char * string,
const struct lookup * lp);
static struct lookup const line_codes[] = {
{ "Rule", LC_RULE },
{ "Zone", LC_ZONE },
{ "Link", LC_LINK },
{ "Leap", LC_LEAP },
{ NULL, 0}
};
static struct lookup const mon_names[] = {
{ "January", TM_JANUARY },
{ "February", TM_FEBRUARY },
{ "March", TM_MARCH },
{ "April", TM_APRIL },
{ "May", TM_MAY },
{ "June", TM_JUNE },
{ "July", TM_JULY },
{ "August", TM_AUGUST },
{ "September", TM_SEPTEMBER },
{ "October", TM_OCTOBER },
{ "November", TM_NOVEMBER },
{ "December", TM_DECEMBER },
{ NULL, 0 }
};
static struct lookup const wday_names[] = {
{ "Sunday", TM_SUNDAY },
{ "Monday", TM_MONDAY },
{ "Tuesday", TM_TUESDAY },
{ "Wednesday", TM_WEDNESDAY },
{ "Thursday", TM_THURSDAY },
{ "Friday", TM_FRIDAY },
{ "Saturday", TM_SATURDAY },
{ NULL, 0 }
};
static struct lookup const lasts[] = {
{ "last-Sunday", TM_SUNDAY },
{ "last-Monday", TM_MONDAY },
{ "last-Tuesday", TM_TUESDAY },
{ "last-Wednesday", TM_WEDNESDAY },
{ "last-Thursday", TM_THURSDAY },
{ "last-Friday", TM_FRIDAY },
{ "last-Saturday", TM_SATURDAY },
{ NULL, 0 }
};
static struct lookup const begin_years[] = {
{ "minimum", YR_MINIMUM },
{ "maximum", YR_MAXIMUM },
{ NULL, 0 }
};
static struct lookup const end_years[] = {
{ "minimum", YR_MINIMUM },
{ "maximum", YR_MAXIMUM },
{ "only", YR_ONLY },
{ NULL, 0 }
};
static struct lookup const leap_types[] = {
{ "Rolling", TRUE },
{ "Stationary", FALSE },
{ NULL, 0 }
};
static const int len_months[2][MONSPERYEAR] = {
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
};
static const int len_years[2] = {
DAYSPERNYEAR, DAYSPERLYEAR
};
static struct attype {
zic_t at;
unsigned char type;
} attypes[TZ_MAX_TIMES];
static long gmtoffs[TZ_MAX_TYPES];
static char isdsts[TZ_MAX_TYPES];
static unsigned char abbrinds[TZ_MAX_TYPES];
static char ttisstds[TZ_MAX_TYPES];
static char ttisgmts[TZ_MAX_TYPES];
static char chars[TZ_MAX_CHARS];
static zic_t trans[TZ_MAX_LEAPS];
static long corr[TZ_MAX_LEAPS];
static char roll[TZ_MAX_LEAPS];
/*
** Memory allocation.
*/
static char *
memcheck(ptr)
char * const ptr;
{
if (ptr == NULL) {
const char *e = strerror(errno);
(void) fprintf(stderr, _("%s: Memory exhausted: %s\n"),
progname, e);
exit(EXIT_FAILURE);
}
return ptr;
}
#define emalloc(size) memcheck(imalloc(size))
#define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
#define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
#define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
/*
** Error handling.
*/
static void
eats(name, num, rname, rnum)
const char * const name;
const int num;
const char * const rname;
const int rnum;
{
filename = name;
linenum = num;
rfilename = rname;
rlinenum = rnum;
}
static void
eat(name, num)
const char * const name;
const int num;
{
eats(name, num, (char *) NULL, -1);
}
static void
error(string)
const char * const string;
{
/*
** Match the format of "cc" to allow sh users to
** zic ... 2>&1 | error -t "*" -v
** on BSD systems.
*/
(void) fprintf(stderr, _("\"%s\", line %d: %s"),
filename, linenum, string);
if (rfilename != NULL)
(void) fprintf(stderr, _(" (rule from \"%s\", line %d)"),
rfilename, rlinenum);
(void) fprintf(stderr, "\n");
++errors;
}
static void
warning(string)
const char * const string;
{
char * cp;
cp = ecpyalloc(_("warning: "));
cp = ecatalloc(cp, string);
error(cp);
ifree(cp);
--errors;
}
static void
usage(FILE *stream, int status)
{
(void) fprintf(stream, _("%s: usage is %s \
[ --version ] [ --help ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\
\t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n\
\n\
Report bugs to tz@elsie.nci.nih.gov.\n"),
progname, progname);
exit(status);
}
static const char * psxrules;
static const char * lcltime;
static const char * directory;
static const char * leapsec;
static const char * yitcommand;
int
main(argc, argv)
int argc;
char * argv[];
{
register int i;
register int j;
register int c;
#ifdef unix
(void) umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
#endif /* defined unix */
#if HAVE_GETTEXT
(void) setlocale(LC_ALL, "");
#ifdef TZ_DOMAINDIR
(void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
#endif /* defined TEXTDOMAINDIR */
(void) textdomain(TZ_DOMAIN);
#endif /* HAVE_GETTEXT */
progname = argv[0];
if (TYPE_BIT(zic_t) < 64) {
(void) fprintf(stderr, "%s: %s\n", progname,
_("wild compilation-time specification of zic_t"));
exit(EXIT_FAILURE);
}
for (i = 1; i < argc; ++i)
if (strcmp(argv[i], "--version") == 0) {
(void) printf("%s\n", elsieid);
exit(EXIT_SUCCESS);
} else if (strcmp(argv[i], "--help") == 0) {
usage(stdout, EXIT_SUCCESS);
}
while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != EOF && c != -1)
switch (c) {
default:
usage(stderr, EXIT_FAILURE);
case 'd':
if (directory == NULL)
directory = optarg;
else {
(void) fprintf(stderr,
_("%s: More than one -d option specified\n"),
progname);
exit(EXIT_FAILURE);
}
break;
case 'l':
if (lcltime == NULL)
lcltime = optarg;
else {
(void) fprintf(stderr,
_("%s: More than one -l option specified\n"),
progname);
exit(EXIT_FAILURE);
}
break;
case 'p':
if (psxrules == NULL)
psxrules = optarg;
else {
(void) fprintf(stderr,
_("%s: More than one -p option specified\n"),
progname);
exit(EXIT_FAILURE);
}
break;
case 'y':
if (yitcommand == NULL)
yitcommand = optarg;
else {
(void) fprintf(stderr,
_("%s: More than one -y option specified\n"),
progname);
exit(EXIT_FAILURE);
}
break;
case 'L':
if (leapsec == NULL)
leapsec = optarg;
else {
(void) fprintf(stderr,
_("%s: More than one -L option specified\n"),
progname);
exit(EXIT_FAILURE);
}
break;
case 'v':
noise = TRUE;
break;
case 's':
(void) printf("%s: -s ignored\n", progname);
break;
}
if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
usage(stderr, EXIT_FAILURE); /* usage message by request */
if (directory == NULL)
directory = TZDIR;
if (yitcommand == NULL)
yitcommand = "yearistype";
setboundaries();
if (optind < argc && leapsec != NULL) {
infile(leapsec);
adjleap();
}
for (i = optind; i < argc; ++i)
infile(argv[i]);
if (errors)
exit(EXIT_FAILURE);
associate();
for (i = 0; i < nzones; i = j) {
/*
** Find the next non-continuation zone entry.
*/
for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
continue;
outzone(&zones[i], j - i);
}
/*
** Make links.
*/
for (i = 0; i < nlinks; ++i) {
eat(links[i].l_filename, links[i].l_linenum);
dolink(links[i].l_from, links[i].l_to);
if (noise)
for (j = 0; j < nlinks; ++j)
if (strcmp(links[i].l_to,
links[j].l_from) == 0)
warning(_("link to link"));
}
if (lcltime != NULL) {
eat("command line", 1);
dolink(lcltime, TZDEFAULT);
}
if (psxrules != NULL) {
eat("command line", 1);
dolink(psxrules, TZDEFRULES);
}
return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
static void
dolink(fromfield, tofield)
const char * const fromfield;
const char * const tofield;
{
register char * fromname;
register char * toname;
if (fromfield[0] == '/')
fromname = ecpyalloc(fromfield);
else {
fromname = ecpyalloc(directory);
fromname = ecatalloc(fromname, "/");
fromname = ecatalloc(fromname, fromfield);
}
if (tofield[0] == '/')
toname = ecpyalloc(tofield);
else {
toname = ecpyalloc(directory);
toname = ecatalloc(toname, "/");
toname = ecatalloc(toname, tofield);
}
/*
** We get to be careful here since
** there's a fair chance of root running us.
*/
if (!itsdir(toname))
(void) remove(toname);
if (link(fromname, toname) != 0) {
int result;
if (mkdirs(toname) != 0)
exit(EXIT_FAILURE);
result = link(fromname, toname);
#if HAVE_SYMLINK
if (result != 0 &&
access(fromname, F_OK) == 0 &&
!itsdir(fromname)) {
const char *s = tofield;
register char * symlinkcontents = NULL;
while ((s = strchr(s+1, '/')) != NULL)
symlinkcontents =
ecatalloc(symlinkcontents,
"../");
symlinkcontents =
ecatalloc(symlinkcontents,
fromname);
result = symlink(symlinkcontents,
toname);
if (result == 0)
warning(_("hard link failed, symbolic link used"));
ifree(symlinkcontents);
}
#endif /* HAVE_SYMLINK */
if (result != 0) {
const char *e = strerror(errno);
(void) fprintf(stderr,
_("%s: Can't link from %s to %s: %s\n"),
progname, fromname, toname, e);
exit(EXIT_FAILURE);
}
}
ifree(fromname);
ifree(toname);
}
#define TIME_T_BITS_IN_FILE 64
static void
setboundaries(void)
{
register int i;
min_time = -1;
for (i = 0; i < TIME_T_BITS_IN_FILE - 1; ++i)
min_time *= 2;
max_time = -(min_time + 1);
}
static int
itsdir(name)
const char * const name;
{
register char * myname;
register int accres;
myname = ecpyalloc(name);
myname = ecatalloc(myname, "/.");
accres = access(myname, F_OK);
ifree(myname);
return accres == 0;
}
/*
** Associate sets of rules with zones.
*/
/*
** Sort by rule name.
*/
static int
rcomp(cp1, cp2)
const void * cp1;
const void * cp2;
{
return strcmp(((const struct rule *) cp1)->r_name,
((const struct rule *) cp2)->r_name);
}
static void
associate(void)
{
register struct zone * zp;
register struct rule * rp;
register int base, out;
register int i, j;
if (nrules != 0) {
(void) qsort((void *) rules, (size_t) nrules,
(size_t) sizeof *rules, rcomp);
for (i = 0; i < nrules - 1; ++i) {
if (strcmp(rules[i].r_name,
rules[i + 1].r_name) != 0)
continue;
if (strcmp(rules[i].r_filename,
rules[i + 1].r_filename) == 0)
continue;
eat(rules[i].r_filename, rules[i].r_linenum);
warning(_("same rule name in multiple files"));
eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
warning(_("same rule name in multiple files"));
for (j = i + 2; j < nrules; ++j) {
if (strcmp(rules[i].r_name,
rules[j].r_name) != 0)
break;
if (strcmp(rules[i].r_filename,
rules[j].r_filename) == 0)
continue;
if (strcmp(rules[i + 1].r_filename,
rules[j].r_filename) == 0)
continue;
break;
}
i = j - 1;
}
}
for (i = 0; i < nzones; ++i) {
zp = &zones[i];
zp->z_rules = NULL;
zp->z_nrules = 0;
}
for (base = 0; base < nrules; base = out) {
rp = &rules[base];
for (out = base + 1; out < nrules; ++out)
if (strcmp(rp->r_name, rules[out].r_name) != 0)
break;
for (i = 0; i < nzones; ++i) {
zp = &zones[i];
if (strcmp(zp->z_rule, rp->r_name) != 0)
continue;
zp->z_rules = rp;
zp->z_nrules = out - base;
}
}
for (i = 0; i < nzones; ++i) {
zp = &zones[i];
if (zp->z_nrules == 0) {
/*
** Maybe we have a local standard time offset.
*/
eat(zp->z_filename, zp->z_linenum);
zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
TRUE);
/*
** Note, though, that if there's no rule,
** a '%s' in the format is a bad thing.
*/
if (strchr(zp->z_format, '%') != 0)
error(_("%s in ruleless zone"));
}
}
if (errors)
exit(EXIT_FAILURE);
}
static void
infile(name)
const char * name;
{
register FILE * fp;
register char ** fields;
register char * cp;
register const struct lookup * lp;
register int nfields;
register int wantcont;
register int num;
char buf[BUFSIZ];
if (strcmp(name, "-") == 0) {
name = _("standard input");
fp = stdin;
} else if ((fp = fopen(name, "r")) == NULL) {
const char *e = strerror(errno);
(void) fprintf(stderr, _("%s: Can't open %s: %s\n"),
progname, name, e);
exit(EXIT_FAILURE);
}
wantcont = FALSE;
for (num = 1; ; ++num) {
eat(name, num);
if (fgets(buf, (int) sizeof buf, fp) != buf)
break;
cp = strchr(buf, '\n');
if (cp == NULL) {
error(_("line too long"));
exit(EXIT_FAILURE);
}
*cp = '\0';
fields = getfields(buf);
nfields = 0;
while (fields[nfields] != NULL) {
static char nada;
if (strcmp(fields[nfields], "-") == 0)
fields[nfields] = &nada;
++nfields;
}
if (nfields == 0) {
/* nothing to do */
} else if (wantcont) {
wantcont = inzcont(fields, nfields);
} else {
lp = byword(fields[0], line_codes);
if (lp == NULL)
error(_("input line of unknown type"));
else switch ((int) (lp->l_value)) {
case LC_RULE:
inrule(fields, nfields);
wantcont = FALSE;
break;
case LC_ZONE:
wantcont = inzone(fields, nfields);
break;
case LC_LINK:
inlink(fields, nfields);
wantcont = FALSE;
break;
case LC_LEAP:
if (name != leapsec)
(void) fprintf(stderr,
_("%s: Leap line in non leap seconds file %s\n"),
progname, name);
else inleap(fields, nfields);
wantcont = FALSE;
break;
default: /* "cannot happen" */
(void) fprintf(stderr,
_("%s: panic: Invalid l_value %d\n"),
progname, lp->l_value);
exit(EXIT_FAILURE);
}
}
ifree((char *) fields);
}
if (ferror(fp)) {
(void) fprintf(stderr, _("%s: Error reading %s\n"),
progname, filename);
exit(EXIT_FAILURE);
}
if (fp != stdin && fclose(fp)) {
const char *e = strerror(errno);
(void) fprintf(stderr, _("%s: Error closing %s: %s\n"),
progname, filename, e);
exit(EXIT_FAILURE);
}
if (wantcont)
error(_("expected continuation line not found"));
}
/*
** Convert a string of one of the forms
** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
** into a number of seconds.
** A null string maps to zero.
** Call error with errstring and return zero on errors.
*/
static long
gethms(string, errstring, signable)
const char * string;
const char * const errstring;
const int signable;
{
long hh;
int mm, ss, sign;
if (string == NULL || *string == '\0')
return 0;
if (!signable)
sign = 1;
else if (*string == '-') {
sign = -1;
++string;
} else sign = 1;
if (sscanf(string, scheck(string, "%ld"), &hh) == 1)
mm = ss = 0;
else if (sscanf(string, scheck(string, "%ld:%d"), &hh, &mm) == 2)
ss = 0;
else if (sscanf(string, scheck(string, "%ld:%d:%d"),
&hh, &mm, &ss) != 3) {
error(errstring);
return 0;
}
if (hh < 0 ||
mm < 0 || mm >= MINSPERHOUR ||
ss < 0 || ss > SECSPERMIN) {
error(errstring);
return 0;
}
if (LONG_MAX / SECSPERHOUR < hh) {
error(_("time overflow"));
return 0;
}
if (noise && hh == HOURSPERDAY && mm == 0 && ss == 0)
warning(_("24:00 not handled by pre-1998 versions of zic"));
if (noise && (hh > HOURSPERDAY ||
(hh == HOURSPERDAY && (mm != 0 || ss != 0))))
warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
return oadd(eitol(sign) * hh * eitol(SECSPERHOUR),
eitol(sign) * (eitol(mm) * eitol(SECSPERMIN) + eitol(ss)));
}
static void
inrule(fields, nfields)
register char ** const fields;
const int nfields;
{
static struct rule r;
if (nfields != RULE_FIELDS) {
error(_("wrong number of fields on Rule line"));
return;
}
if (*fields[RF_NAME] == '\0') {
error(_("nameless rule"));
return;
}
r.r_filename = filename;
r.r_linenum = linenum;
r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
r.r_name = ecpyalloc(fields[RF_NAME]);
r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
if (max_abbrvar_len < strlen(r.r_abbrvar))
max_abbrvar_len = strlen(r.r_abbrvar);
rules = (struct rule *) (void *) erealloc((char *) rules,
(int) ((nrules + 1) * sizeof *rules));
rules[nrules++] = r;
}
static int
inzone(fields, nfields)
register char ** const fields;
const int nfields;
{
register int i;
static char * buf;
if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
error(_("wrong number of fields on Zone line"));
return FALSE;
}
if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
(void) sprintf(buf,
_("\"Zone %s\" line and -l option are mutually exclusive"),
TZDEFAULT);