-
Notifications
You must be signed in to change notification settings - Fork 0
/
zic.c
3015 lines (2817 loc) · 71.1 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.
*/
#include "version.h"
#include "private.h"
#include "locale.h"
#include "tzfile.h"
#include <stdarg.h>
#define ZIC_VERSION_PRE_2013 '2'
#define ZIC_VERSION '3'
typedef int_fast64_t zic_t;
#define ZIC_MIN INT_FAST64_MIN
#define ZIC_MAX INT_FAST64_MAX
#define SCNdZIC SCNdFAST64
#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
struct rule {
const char * r_filename;
int r_linenum;
const char * r_name;
zic_t r_loyear; /* for example, 1986 */
zic_t r_hiyear; /* for example, 1986 */
const char * r_yrtype;
bool r_lowasnum;
bool r_hiwasnum;
int r_month; /* 0..11 */
int r_dycode; /* see below */
int r_dayofmonth;
int r_wday;
zic_t r_tod; /* time from midnight */
bool r_todisstd; /* above is standard time if 1 */
/* or wall clock time if 0 */
bool r_todisgmt; /* above is GMT if 1 */
/* or local time if 0 */
zic_t 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;
zic_t z_gmtoff;
const char * z_rule;
const char * z_format;
char z_format_specifier;
zic_t 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;
#if ! HAVE_LINK
# define link(from, to) (-1)
#endif
#if ! HAVE_SYMLINK
# define symlink(from, to) (-1)
#endif
static void addtt(zic_t starttime, int type);
static int addtype(zic_t, char const *, bool, bool, bool);
static void leapadd(zic_t, bool, int, int);
static void adjleap(void);
static void associate(void);
static void dolink(const char * fromfield, const char * tofield);
static char ** getfields(char * buf);
static zic_t gethms(const char * string, const char * errstring,
bool);
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 bool inzcont(char ** fields, int nfields);
static bool inzone(char ** fields, int nfields);
static bool inzsub(char **, int, bool);
static int itsdir(const char * name);
static bool is_alpha(char a);
static char lowerit(char);
static bool mkdirs(char *);
static void newabbr(const char * abbr);
static zic_t oadd(zic_t t1, zic_t t2);
static void outzone(const struct zone * zp, int ntzones);
static zic_t rpytime(const struct rule * rp, zic_t 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 zic_t tadd(zic_t t1, zic_t t2);
static bool yearistype(int year, const char * type);
/* Bound on length of what %z can expand to. */
enum { PERCENT_Z_LEN_BOUND = sizeof "+995959" - 1 };
static int charcnt;
static bool errors;
static bool warnings;
static const char * filename;
static int leapcnt;
static bool leapseen;
static zic_t leapminyear;
static zic_t leapmaxyear;
static int linenum;
static int max_abbrvar_len = PERCENT_Z_LEN_BOUND;
static int max_format_len;
static zic_t max_year;
static zic_t min_year;
static bool noise;
static const char * rfilename;
static int rlinenum;
static const char * progname;
static int timecnt;
static int timecnt_alloc;
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 int nrules_alloc;
static struct zone * zones;
static int nzones; /* number of zones */
static int nzones_alloc;
struct link {
const char * l_filename;
int l_linenum;
const char * l_from;
const char * l_to;
};
static struct link * links;
static int nlinks;
static int nlinks_alloc;
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;
static zic_t gmtoffs[TZ_MAX_TYPES];
static char isdsts[TZ_MAX_TYPES];
static unsigned char abbrinds[TZ_MAX_TYPES];
static bool ttisstds[TZ_MAX_TYPES];
static bool ttisgmts[TZ_MAX_TYPES];
static char chars[TZ_MAX_CHARS];
static zic_t trans[TZ_MAX_LEAPS];
static zic_t corr[TZ_MAX_LEAPS];
static char roll[TZ_MAX_LEAPS];
/*
** Memory allocation.
*/
static _Noreturn void
memory_exhausted(const char *msg)
{
fprintf(stderr, _("%s: Memory exhausted: %s\n"), progname, msg);
exit(EXIT_FAILURE);
}
static ATTRIBUTE_PURE size_t
size_product(size_t nitems, size_t itemsize)
{
if (SIZE_MAX / itemsize < nitems)
memory_exhausted(_("size overflow"));
return nitems * itemsize;
}
#if !HAVE_STRDUP
static char *
strdup(char const *str)
{
char *result = malloc(strlen(str) + 1);
return result ? strcpy(result, str) : result;
}
#endif
static ATTRIBUTE_PURE void *
memcheck(void *ptr)
{
if (ptr == NULL)
memory_exhausted(strerror(errno));
return ptr;
}
static void *
emalloc(size_t size)
{
return memcheck(malloc(size));
}
static void *
erealloc(void *ptr, size_t size)
{
return memcheck(realloc(ptr, size));
}
static char *
ecpyalloc (char const *str)
{
return memcheck(strdup(str));
}
static void *
growalloc(void *ptr, size_t itemsize, int nitems, int *nitems_alloc)
{
if (nitems < *nitems_alloc)
return ptr;
else {
int amax = INT_MAX < SIZE_MAX ? INT_MAX : SIZE_MAX;
if ((amax - 1) / 3 * 2 < *nitems_alloc)
memory_exhausted(_("int overflow"));
*nitems_alloc = *nitems_alloc + (*nitems_alloc >> 1) + 1;
return erealloc(ptr, size_product(*nitems_alloc, itemsize));
}
}
/*
** Error handling.
*/
static void
eats(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(const char *const name, const int num)
{
eats(name, num, NULL, -1);
}
static void ATTRIBUTE_FORMAT((printf, 1, 0))
verror(const char *const string, va_list args)
{
/*
** Match the format of "cc" to allow sh users to
** zic ... 2>&1 | error -t "*" -v
** on BSD systems.
*/
if (filename)
fprintf(stderr, _("\"%s\", line %d: "), filename, linenum);
vfprintf(stderr, string, args);
if (rfilename != NULL)
fprintf(stderr, _(" (rule from \"%s\", line %d)"),
rfilename, rlinenum);
fprintf(stderr, "\n");
}
static void ATTRIBUTE_FORMAT((printf, 1, 2))
error(const char *const string, ...)
{
va_list args;
va_start(args, string);
verror(string, args);
va_end(args);
errors = true;
}
static void ATTRIBUTE_FORMAT((printf, 1, 2))
warning(const char *const string, ...)
{
va_list args;
fprintf(stderr, _("warning: "));
va_start(args, string);
verror(string, args);
va_end(args);
warnings = true;
}
static void
close_file(FILE *stream, char const *name)
{
char const *e = (ferror(stream) ? _("I/O error")
: fclose(stream) != 0 ? strerror(errno) : NULL);
if (e) {
fprintf(stderr, "%s: ", progname);
if (name)
fprintf(stderr, "%s: ", name);
fprintf(stderr, "%s\n", e);
exit(EXIT_FAILURE);
}
}
static _Noreturn void
usage(FILE *stream, int status)
{
fprintf(stream,
_("%s: usage is %s [ --version ] [ --help ] [ -v ] \\\n"
"\t[ -l localtime ] [ -p posixrules ] [ -d directory ] \\\n"
"\t[ -L leapseconds ] [ filename ... ]\n\n"
"Report bugs to %s.\n"),
progname, progname, REPORT_BUGS_TO);
if (status == EXIT_SUCCESS)
close_file(stream, NULL);
exit(status);
}
static const char * psxrules;
static const char * lcltime;
static const char * directory;
static const char * leapsec;
static const char * yitcommand;
int
main(int argc, char **argv)
{
register int i;
register int j;
register int c;
#ifdef S_IWGRP
umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
#endif
#if HAVE_GETTEXT
setlocale(LC_ALL, "");
#ifdef TZ_DOMAINDIR
bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
#endif /* defined TEXTDOMAINDIR */
textdomain(TZ_DOMAIN);
#endif /* HAVE_GETTEXT */
progname = argv[0];
if (TYPE_BIT(zic_t) < 64) {
fprintf(stderr, "%s: %s\n", progname,
_("wild compilation-time specification of zic_t"));
return EXIT_FAILURE;
}
for (i = 1; i < argc; ++i)
if (strcmp(argv[i], "--version") == 0) {
printf("zic %s%s\n", PKGVERSION, TZVERSION);
close_file(stdout, NULL);
return 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 {
fprintf(stderr,
_("%s: More than one -d option specified\n"),
progname);
return EXIT_FAILURE;
}
break;
case 'l':
if (lcltime == NULL)
lcltime = optarg;
else {
fprintf(stderr,
_("%s: More than one -l option specified\n"),
progname);
return EXIT_FAILURE;
}
break;
case 'p':
if (psxrules == NULL)
psxrules = optarg;
else {
fprintf(stderr,
_("%s: More than one -p option specified\n"),
progname);
return EXIT_FAILURE;
}
break;
case 'y':
if (yitcommand == NULL)
yitcommand = optarg;
else {
fprintf(stderr,
_("%s: More than one -y option specified\n"),
progname);
return EXIT_FAILURE;
}
break;
case 'L':
if (leapsec == NULL)
leapsec = optarg;
else {
fprintf(stderr,
_("%s: More than one -L option specified\n"),
progname);
return EXIT_FAILURE;
}
break;
case 'v':
noise = true;
break;
case 's':
warning(_("-s ignored"));
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";
if (optind < argc && leapsec != NULL) {
infile(leapsec);
adjleap();
}
for (i = optind; i < argc; ++i)
infile(argv[i]);
if (errors)
return 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);
}
if (warnings && (ferror(stderr) || fclose(stderr) != 0))
return EXIT_FAILURE;
return errors ? EXIT_FAILURE : EXIT_SUCCESS;
}
static bool
componentcheck(char const *name, char const *component,
char const *component_end)
{
enum { component_len_max = 14 };
size_t component_len = component_end - component;
if (component_len == 0) {
if (!*name)
error (_("empty file name"));
else
error (_(component == name
? "file name '%s' begins with '/'"
: *component_end
? "file name '%s' contains '//'"
: "file name '%s' ends with '/'"),
name);
return false;
}
if (0 < component_len && component_len <= 2
&& component[0] == '.' && component_end[-1] == '.') {
error(_("file name '%s' contains '%.*s' component"),
name, (int) component_len, component);
return false;
}
if (noise) {
if (0 < component_len && component[0] == '-')
warning(_("file name '%s' component contains leading '-'"),
name);
if (component_len_max < component_len)
warning(_("file name '%s' contains overlength component"
" '%.*s...'"),
name, component_len_max, component);
}
return true;
}
static bool
namecheck(const char *name)
{
register char const *cp;
/* Benign characters in a portable file name. */
static char const benign[] =
"-/_"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/* Non-control chars in the POSIX portable character set,
excluding the benign characters. */
static char const printable_and_not_benign[] =
" !\"#$%&'()*+,.0123456789:;<=>?@[\\]^`{|}~";
register char const *component = name;
for (cp = name; *cp; cp++) {
unsigned char c = *cp;
if (noise && !strchr(benign, c)) {
warning((strchr(printable_and_not_benign, c)
? _("file name '%s' contains byte '%c'")
: _("file name '%s' contains byte '\\%o'")),
name, c);
}
if (c == '/') {
if (!componentcheck(name, component, cp))
return false;
component = cp + 1;
}
}
return componentcheck(name, component, cp);
}
static char *
relname(char const *dir, char const *base)
{
if (*base == '/')
return ecpyalloc(base);
else {
size_t dir_len = strlen(dir);
bool needs_slash = dir_len && dir[dir_len - 1] != '/';
char *result = emalloc(dir_len + needs_slash + strlen(base) + 1);
result[dir_len] = '/';
strcpy(result + dir_len + needs_slash, base);
return memcpy(result, dir, dir_len);
}
}
static void
dolink(char const *fromfield, char const *tofield)
{
register char * fromname;
register char * toname;
register int fromisdir;
fromname = relname(directory, fromfield);
toname = relname(directory, tofield);
/*
** We get to be careful here since
** there's a fair chance of root running us.
*/
fromisdir = itsdir(fromname);
if (fromisdir) {
char const *e = strerror(fromisdir < 0 ? errno : EPERM);
fprintf(stderr, _("%s: link from %s failed: %s"),
progname, fromname, e);
exit(EXIT_FAILURE);
}
if (itsdir(toname) <= 0)
remove(toname);
if (link(fromname, toname) != 0) {
int result;
if (! mkdirs(toname))
exit(EXIT_FAILURE);
result = link(fromname, toname);
if (result != 0) {
const char *s = fromfield;
const char *t;
char *p;
size_t dotdots = 0;
register char * symlinkcontents = NULL;
do
t = s;
while ((s = strchr(s, '/'))
&& ! strncmp (fromfield, tofield,
++s - fromfield));
for (s = tofield + (t - fromfield); *s; s++)
dotdots += *s == '/';
symlinkcontents
= emalloc(3 * dotdots + strlen(t) + 1);
for (p = symlinkcontents; dotdots-- != 0; p += 3)
memcpy(p, "../", 3);
strcpy(p, t);
result = symlink(symlinkcontents, toname);
if (result == 0)
warning(_("hard link failed, symbolic link used"));
free(symlinkcontents);
}
if (result != 0) {
FILE *fp, *tp;
int c;
fp = fopen(fromname, "rb");
if (!fp) {
const char *e = strerror(errno);
fprintf(stderr,
_("%s: Can't read %s: %s\n"),
progname, fromname, e);
exit(EXIT_FAILURE);
}
tp = fopen(toname, "wb");
if (!tp) {
const char *e = strerror(errno);
fprintf(stderr,
_("%s: Can't create %s: %s\n"),
progname, toname, e);
exit(EXIT_FAILURE);
}
while ((c = getc(fp)) != EOF)
putc(c, tp);
close_file(fp, fromname);
close_file(tp, toname);
warning(_("link failed, copy used"));
}
}
free(fromname);
free(toname);
}
#define TIME_T_BITS_IN_FILE 64
static zic_t const min_time = MINVAL (zic_t, TIME_T_BITS_IN_FILE);
static zic_t const max_time = MAXVAL (zic_t, TIME_T_BITS_IN_FILE);
/* Estimated time of the Big Bang, in seconds since the POSIX epoch.
rounded downward to the negation of a power of two that is
comfortably outside the error bounds.
zic does not output time stamps before this, partly because they
are physically suspect, and partly because GNOME mishandles them; see
GNOME bug 730332 <https://bugzilla.gnome.org/show_bug.cgi?id=730332>.
For the time of the Big Bang, see:
Ade PAR, Aghanim N, Armitage-Caplan C et al. Planck 2013 results.
I. Overview of products and scientific results.
arXiv:1303.5062 2013-03-20 20:10:01 UTC
<http://arxiv.org/pdf/1303.5062v1> [PDF]
Page 36, Table 9, row Age/Gyr, column Planck+WP+highL+BAO 68% limits
gives the value 13.798 plus-or-minus 0.037 billion years.
Multiplying this by 1000000000 and then by 31557600 (the number of
seconds in an astronomical year) gives a value that is comfortably
less than 2**59, so BIG_BANG is - 2**59.
BIG_BANG is approximate, and may change in future versions.
Please do not rely on its exact value. */
#ifndef BIG_BANG
#define BIG_BANG (- (1LL << 59))
#endif
static const zic_t big_bang_time = BIG_BANG;
/* Return 1 if NAME is a directory, 0 if it's something else, -1 if trouble. */
static int
itsdir(char const *name)
{
struct stat st;
int res = stat(name, &st);
if (res != 0)
return res;
#ifdef S_ISDIR
return S_ISDIR(st.st_mode) != 0;
#else
{
char *nameslashdot = relname(name, ".");
res = stat(nameslashdot, &st);
free(nameslashdot);
return res == 0;
}
#endif
}
/*
** Associate sets of rules with zones.
*/
/*
** Sort by rule name.
*/
static int
rcomp(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) {
qsort(rules, nrules, 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 (zp->z_format_specifier == 's')
error("%s", _("%s in ruleless zone"));
}
}
if (errors)
exit(EXIT_FAILURE);
}
static void
infile(const char *name)
{
register FILE * fp;
register char ** fields;
register char * cp;
register const struct lookup * lp;
register int nfields;
register bool 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);
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, sizeof buf, fp) != buf)
break;
cp = strchr(buf, '\n');
if (cp == NULL) {
error(_("line too long"));
exit(EXIT_FAILURE);