~ubuntu-branches/ubuntu/hardy/postgresql-8.4/hardy-backports

« back to all changes in this revision

Viewing changes to src/timezone/zic.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-03-20 12:00:13 UTC
  • Revision ID: james.westby@ubuntu.com-20090320120013-hogj7egc5mjncc5g
Tags: upstream-8.4~0cvs20090328
ImportĀ upstreamĀ versionĀ 8.4~0cvs20090328

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is in the public domain, so clarified as of
 
3
 * 2006-07-17 by Arthur David Olson.
 
4
 *
 
5
 * IDENTIFICATION
 
6
 *        $PostgreSQL$
 
7
 */
 
8
 
 
9
#include "postgres_fe.h"
 
10
 
 
11
#ifdef HAVE_GETOPT_H
 
12
#include <getopt.h>
 
13
#endif
 
14
#include <limits.h>
 
15
#include <locale.h>
 
16
 
 
17
extern int      optind;
 
18
extern char *optarg;
 
19
 
 
20
#include "private.h"
 
21
#include "pgtz.h"
 
22
#include "tzfile.h"
 
23
 
 
24
#define       ZIC_VERSION     '2'
 
25
 
 
26
typedef int64  zic_t;
 
27
 
 
28
#ifndef ZIC_MAX_ABBR_LEN_WO_WARN
 
29
#define ZIC_MAX_ABBR_LEN_WO_WARN      6
 
30
#endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
 
31
 
 
32
#ifdef HAVE_SYS_STAT_H
 
33
#include <sys/stat.h>
 
34
#endif
 
35
 
 
36
#ifndef WIN32
 
37
#ifdef S_IRUSR
 
38
#define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
 
39
#else
 
40
#define MKDIR_UMASK 0755
 
41
#endif
 
42
#endif
 
43
 
 
44
static char elsieid[] = "@(#)zic.c  8.17";
 
45
 
 
46
/*
 
47
 * On some ancient hosts, predicates like `isspace(C)' are defined
 
48
 * only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
 
49
 * which says they are defined only if C == ((unsigned char) C) || C == EOF.
 
50
 * Neither the C Standard nor Posix require that `isascii' exist.
 
51
 * For portability, we check both ancient and modern requirements.
 
52
 * If isascii is not defined, the isascii check succeeds trivially.
 
53
 */
 
54
#include <ctype.h>
 
55
#ifndef isascii
 
56
#define isascii(x) 1
 
57
#endif
 
58
 
 
59
#define OFFSET_STRLEN_MAXIMUM (7 + INT_STRLEN_MAXIMUM(long))
 
60
#define RULE_STRLEN_MAXIMUM   8       /* "Mdd.dd.d" */
 
61
 
 
62
#define end(cp)       (strchr((cp), '\0'))
 
63
 
 
64
struct rule
 
65
{
 
66
        const char *r_filename;
 
67
        int                     r_linenum;
 
68
        const char *r_name;
 
69
 
 
70
        int                     r_loyear;               /* for example, 1986 */
 
71
        int                     r_hiyear;               /* for example, 1986 */
 
72
        const char *r_yrtype;
 
73
        int                     r_lowasnum;
 
74
        int                     r_hiwasnum;
 
75
 
 
76
        int                     r_month;                /* 0..11 */
 
77
 
 
78
        int                     r_dycode;               /* see below */
 
79
        int                     r_dayofmonth;
 
80
        int                     r_wday;
 
81
 
 
82
        long            r_tod;                  /* time from midnight */
 
83
        int                     r_todisstd;             /* above is standard time if TRUE */
 
84
        /* or wall clock time if FALSE */
 
85
        int                     r_todisgmt;             /* above is GMT if TRUE */
 
86
        /* or local time if FALSE */
 
87
        long            r_stdoff;               /* offset from standard time */
 
88
        const char *r_abbrvar;          /* variable part of abbreviation */
 
89
 
 
90
        int                     r_todo;                 /* a rule to do (used in outzone) */
 
91
        zic_t           r_temp;                 /* used in outzone */
 
92
};
 
93
 
 
94
/*
 
95
 *      r_dycode                r_dayofmonth    r_wday
 
96
 */
 
97
 
 
98
#define DC_DOM          0       /* 1..31 */             /* unused */
 
99
#define DC_DOWGEQ       1       /* 1..31 */             /* 0..6 (Sun..Sat) */
 
100
#define DC_DOWLEQ       2       /* 1..31 */             /* 0..6 (Sun..Sat) */
 
101
 
 
102
struct zone
 
103
{
 
104
        const char *z_filename;
 
105
        int                     z_linenum;
 
106
 
 
107
        const char *z_name;
 
108
        long            z_gmtoff;
 
109
        const char *z_rule;
 
110
        const char *z_format;
 
111
 
 
112
        long            z_stdoff;
 
113
 
 
114
        struct rule *z_rules;
 
115
        int                     z_nrules;
 
116
 
 
117
        struct rule z_untilrule;
 
118
        zic_t   z_untiltime;
 
119
};
 
120
 
 
121
extern int      link(const char *fromname, const char *toname);
 
122
static void addtt(const pg_time_t starttime, int type);
 
123
static int addtype(long gmtoff, const char *abbr, int isdst,
 
124
                int ttisstd, int ttisgmt);
 
125
static void leapadd(const pg_time_t t, int positive, int rolling, int count);
 
126
static void adjleap(void);
 
127
static void associate(void);
 
128
static int      ciequal(const char *ap, const char *bp);
 
129
static void convert(long val, char *buf);
 
130
static void dolink(const char *fromfile, const char *tofile);
 
131
static void doabbr(char *abbr, const char *format,
 
132
           const char *letters, int isdst, int doquotes);
 
133
static void eat(const char *name, int num);
 
134
static void eats(const char *name, int num,
 
135
         const char *rname, int rnum);
 
136
static long eitol(int i);
 
137
static void error(const char *message);
 
138
static char **getfields(char *buf);
 
139
static long gethms(const char *string, const char *errstrng,
 
140
           int signable);
 
141
static void infile(const char *filename);
 
142
static void inleap(char **fields, int nfields);
 
143
static void inlink(char **fields, int nfields);
 
144
static void inrule(char **fields, int nfields);
 
145
static int      inzcont(char **fields, int nfields);
 
146
static int      inzone(char **fields, int nfields);
 
147
static int      inzsub(char **fields, int nfields, int iscont);
 
148
static int      itsabbr(const char *abbr, const char *word);
 
149
static int      itsdir(const char *name);
 
150
static int      lowerit(int c);
 
151
static char *memcheck(char *tocheck);
 
152
static int      mkdirs(char *filename);
 
153
static void newabbr(const char *abbr);
 
154
static long oadd(long t1, long t2);
 
155
static void outzone(const struct zone * zp, int ntzones);
 
156
static void puttzcode(long code, FILE *fp);
 
157
static int      rcomp(const void *leftp, const void *rightp);
 
158
static pg_time_t rpytime(const struct rule * rp, int wantedy);
 
159
static void rulesub(struct rule * rp,
 
160
                const char *loyearp, const char *hiyearp,
 
161
                const char *typep, const char *monthp,
 
162
                const char *dayp, const char *timep);
 
163
static void setboundaries(void);
 
164
static pg_time_t tadd(const pg_time_t t1, long t2);
 
165
static void usage(void);
 
166
static void writezone(const char *name, const char *string);
 
167
static int      yearistype(int year, const char *type);
 
168
 
 
169
static int      charcnt;
 
170
static int      errors;
 
171
static const char *filename;
 
172
static int      leapcnt;
 
173
static int      leapseen;
 
174
static int      leapminyear;
 
175
static int      leapmaxyear;
 
176
static int      linenum;
 
177
static int      max_abbrvar_len;
 
178
static int      max_format_len;
 
179
static zic_t max_time;
 
180
static int      max_year;
 
181
static zic_t min_time;
 
182
static int      min_year;
 
183
static int      noise;
 
184
static const char *rfilename;
 
185
static int      rlinenum;
 
186
static const char *progname;
 
187
static int      timecnt;
 
188
static int      typecnt;
 
189
 
 
190
/*
 
191
 * Line codes.
 
192
 */
 
193
 
 
194
#define LC_RULE         0
 
195
#define LC_ZONE         1
 
196
#define LC_LINK         2
 
197
#define LC_LEAP         3
 
198
 
 
199
/*
 
200
 * Which fields are which on a Zone line.
 
201
 */
 
202
 
 
203
#define ZF_NAME         1
 
204
#define ZF_GMTOFF       2
 
205
#define ZF_RULE         3
 
206
#define ZF_FORMAT       4
 
207
#define ZF_TILYEAR      5
 
208
#define ZF_TILMONTH 6
 
209
#define ZF_TILDAY       7
 
210
#define ZF_TILTIME      8
 
211
#define ZONE_MINFIELDS  5
 
212
#define ZONE_MAXFIELDS  9
 
213
 
 
214
/*
 
215
 * Which fields are which on a Zone continuation line.
 
216
 */
 
217
 
 
218
#define ZFC_GMTOFF      0
 
219
#define ZFC_RULE        1
 
220
#define ZFC_FORMAT      2
 
221
#define ZFC_TILYEAR 3
 
222
#define ZFC_TILMONTH    4
 
223
#define ZFC_TILDAY      5
 
224
#define ZFC_TILTIME 6
 
225
#define ZONEC_MINFIELDS 3
 
226
#define ZONEC_MAXFIELDS 7
 
227
 
 
228
/*
 
229
 * Which files are which on a Rule line.
 
230
 */
 
231
 
 
232
#define RF_NAME         1
 
233
#define RF_LOYEAR       2
 
234
#define RF_HIYEAR       3
 
235
#define RF_COMMAND      4
 
236
#define RF_MONTH        5
 
237
#define RF_DAY          6
 
238
#define RF_TOD          7
 
239
#define RF_STDOFF       8
 
240
#define RF_ABBRVAR      9
 
241
#define RULE_FIELDS 10
 
242
 
 
243
/*
 
244
 * Which fields are which on a Link line.
 
245
 */
 
246
 
 
247
#define LF_FROM         1
 
248
#define LF_TO           2
 
249
#define LINK_FIELDS 3
 
250
 
 
251
/*
 
252
 * Which fields are which on a Leap line.
 
253
 */
 
254
 
 
255
#define LP_YEAR         1
 
256
#define LP_MONTH        2
 
257
#define LP_DAY          3
 
258
#define LP_TIME         4
 
259
#define LP_CORR         5
 
260
#define LP_ROLL         6
 
261
#define LEAP_FIELDS 7
 
262
 
 
263
/*
 
264
 * Year synonyms.
 
265
 */
 
266
 
 
267
#define YR_MINIMUM      0
 
268
#define YR_MAXIMUM      1
 
269
#define YR_ONLY         2
 
270
 
 
271
static struct rule *rules;
 
272
static int      nrules;                         /* number of rules */
 
273
 
 
274
static struct zone *zones;
 
275
static int      nzones;                         /* number of zones */
 
276
 
 
277
struct link
 
278
{
 
279
        const char *l_filename;
 
280
        int                     l_linenum;
 
281
        const char *l_from;
 
282
        const char *l_to;
 
283
};
 
284
 
 
285
static struct link *links;
 
286
static int      nlinks;
 
287
 
 
288
struct lookup
 
289
{
 
290
        const char *l_word;
 
291
        const int       l_value;
 
292
};
 
293
 
 
294
static struct lookup const *byword(const char *string,
 
295
           const struct lookup * lp);
 
296
 
 
297
static struct lookup const line_codes[] = {
 
298
        {"Rule", LC_RULE},
 
299
        {"Zone", LC_ZONE},
 
300
        {"Link", LC_LINK},
 
301
        {"Leap", LC_LEAP},
 
302
        {NULL, 0}
 
303
};
 
304
 
 
305
static struct lookup const mon_names[] = {
 
306
        {"January", TM_JANUARY},
 
307
        {"February", TM_FEBRUARY},
 
308
        {"March", TM_MARCH},
 
309
        {"April", TM_APRIL},
 
310
        {"May", TM_MAY},
 
311
        {"June", TM_JUNE},
 
312
        {"July", TM_JULY},
 
313
        {"August", TM_AUGUST},
 
314
        {"September", TM_SEPTEMBER},
 
315
        {"October", TM_OCTOBER},
 
316
        {"November", TM_NOVEMBER},
 
317
        {"December", TM_DECEMBER},
 
318
        {NULL, 0}
 
319
};
 
320
 
 
321
static struct lookup const wday_names[] = {
 
322
        {"Sunday", TM_SUNDAY},
 
323
        {"Monday", TM_MONDAY},
 
324
        {"Tuesday", TM_TUESDAY},
 
325
        {"Wednesday", TM_WEDNESDAY},
 
326
        {"Thursday", TM_THURSDAY},
 
327
        {"Friday", TM_FRIDAY},
 
328
        {"Saturday", TM_SATURDAY},
 
329
        {NULL, 0}
 
330
};
 
331
 
 
332
static struct lookup const lasts[] = {
 
333
        {"last-Sunday", TM_SUNDAY},
 
334
        {"last-Monday", TM_MONDAY},
 
335
        {"last-Tuesday", TM_TUESDAY},
 
336
        {"last-Wednesday", TM_WEDNESDAY},
 
337
        {"last-Thursday", TM_THURSDAY},
 
338
        {"last-Friday", TM_FRIDAY},
 
339
        {"last-Saturday", TM_SATURDAY},
 
340
        {NULL, 0}
 
341
};
 
342
 
 
343
static struct lookup const begin_years[] = {
 
344
        {"minimum", YR_MINIMUM},
 
345
        {"maximum", YR_MAXIMUM},
 
346
        {NULL, 0}
 
347
};
 
348
 
 
349
static struct lookup const end_years[] = {
 
350
        {"minimum", YR_MINIMUM},
 
351
        {"maximum", YR_MAXIMUM},
 
352
        {"only", YR_ONLY},
 
353
        {NULL, 0}
 
354
};
 
355
 
 
356
static struct lookup const leap_types[] = {
 
357
        {"Rolling", TRUE},
 
358
        {"Stationary", FALSE},
 
359
        {NULL, 0}
 
360
};
 
361
 
 
362
static const int len_months[2][MONSPERYEAR] = {
 
363
        {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
 
364
        {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
 
365
};
 
366
 
 
367
static const int len_years[2] = {
 
368
        DAYSPERNYEAR, DAYSPERLYEAR
 
369
};
 
370
 
 
371
static struct attype
 
372
{
 
373
        zic_t   at;
 
374
        unsigned char type;
 
375
}       attypes[TZ_MAX_TIMES];
 
376
static long gmtoffs[TZ_MAX_TYPES];
 
377
static char isdsts[TZ_MAX_TYPES];
 
378
static unsigned char abbrinds[TZ_MAX_TYPES];
 
379
static char ttisstds[TZ_MAX_TYPES];
 
380
static char ttisgmts[TZ_MAX_TYPES];
 
381
static char chars[TZ_MAX_CHARS];
 
382
static zic_t trans[TZ_MAX_LEAPS];
 
383
static long corr[TZ_MAX_LEAPS];
 
384
static char roll[TZ_MAX_LEAPS];
 
385
 
 
386
/*
 
387
 * Memory allocation.
 
388
 */
 
389
 
 
390
static char *
 
391
memcheck(char *ptr)
 
392
{
 
393
        if (ptr == NULL)
 
394
        {
 
395
                const char *e = strerror(errno);
 
396
 
 
397
                (void) fprintf(stderr, _("%s: Memory exhausted: %s\n"),
 
398
                                           progname, e);
 
399
                exit(EXIT_FAILURE);
 
400
        }
 
401
        return ptr;
 
402
}
 
403
 
 
404
#define emalloc(size)           memcheck(imalloc(size))
 
405
#define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
 
406
#define ecpyalloc(ptr)          memcheck(icpyalloc(ptr))
 
407
#define ecatalloc(oldp, newp)   memcheck(icatalloc((oldp), (newp)))
 
408
 
 
409
/*
 
410
 * Error handling.
 
411
 */
 
412
 
 
413
static void
 
414
eats(const char *name, int num, const char *rname, int rnum)
 
415
{
 
416
        filename = name;
 
417
        linenum = num;
 
418
        rfilename = rname;
 
419
        rlinenum = rnum;
 
420
}
 
421
 
 
422
static void
 
423
eat(const char *name, int num)
 
424
{
 
425
        eats(name, num, (char *) NULL, -1);
 
426
}
 
427
 
 
428
static void
 
429
error(const char *string)
 
430
{
 
431
        /*
 
432
         * Match the format of "cc" to allow sh users to  zic ... 2>&1 | error -t
 
433
         * "*" -v on BSD systems.
 
434
         */
 
435
        (void) fprintf(stderr, _("\"%s\", line %d: %s"),
 
436
                                   filename, linenum, string);
 
437
        if (rfilename != NULL)
 
438
                (void) fprintf(stderr, _(" (rule from \"%s\", line %d)"),
 
439
                                           rfilename, rlinenum);
 
440
        (void) fprintf(stderr, "\n");
 
441
        ++errors;
 
442
}
 
443
 
 
444
static void
 
445
warning(const char *string)
 
446
{
 
447
        char       *cp;
 
448
 
 
449
        cp = ecpyalloc(_("warning: "));
 
450
        cp = ecatalloc(cp, string);
 
451
        error(cp);
 
452
        ifree(cp);
 
453
        --errors;
 
454
}
 
455
 
 
456
static void
 
457
usage(void)
 
458
{
 
459
        (void) fprintf(stderr, _("%s: usage is %s \
 
460
[ --version ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\
 
461
\t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n"),
 
462
                                   progname, progname);
 
463
        exit(EXIT_FAILURE);
 
464
}
 
465
 
 
466
static const char *psxrules;
 
467
static const char *lcltime;
 
468
static const char *directory;
 
469
static const char *leapsec;
 
470
static const char *yitcommand;
 
471
 
 
472
int
 
473
main(int argc, char *argv[])
 
474
{
 
475
        int                     i;
 
476
        int                     j;
 
477
        int                     c;
 
478
 
 
479
#ifndef WIN32
 
480
        (void) umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
 
481
#endif   /* !WIN32 */
 
482
        progname = argv[0];
 
483
        if (TYPE_BIT(zic_t) < 64) {
 
484
                (void) fprintf(stderr, "%s: %s\n", progname,
 
485
                       _("wild compilation-time specification of zic_t"));
 
486
                exit(EXIT_FAILURE);
 
487
        }
 
488
        for (i = 1; i < argc; ++i)
 
489
                if (strcmp(argv[i], "--version") == 0)
 
490
                {
 
491
                        (void) printf("%s\n", elsieid);
 
492
                        exit(EXIT_SUCCESS);
 
493
                }
 
494
        while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != EOF && c != -1)
 
495
                switch (c)
 
496
                {
 
497
                        default:
 
498
                                usage();
 
499
                        case 'd':
 
500
                                if (directory == NULL)
 
501
                                        directory = optarg;
 
502
                                else
 
503
                                {
 
504
                                        (void) fprintf(stderr,
 
505
                                                                _("%s: More than one -d option specified\n"),
 
506
                                                                   progname);
 
507
                                        exit(EXIT_FAILURE);
 
508
                                }
 
509
                                break;
 
510
                        case 'l':
 
511
                                if (lcltime == NULL)
 
512
                                        lcltime = optarg;
 
513
                                else
 
514
                                {
 
515
                                        (void) fprintf(stderr,
 
516
                                                                _("%s: More than one -l option specified\n"),
 
517
                                                                   progname);
 
518
                                        exit(EXIT_FAILURE);
 
519
                                }
 
520
                                break;
 
521
                        case 'p':
 
522
                                if (psxrules == NULL)
 
523
                                        psxrules = optarg;
 
524
                                else
 
525
                                {
 
526
                                        (void) fprintf(stderr,
 
527
                                                                _("%s: More than one -p option specified\n"),
 
528
                                                                   progname);
 
529
                                        exit(EXIT_FAILURE);
 
530
                                }
 
531
                                break;
 
532
                        case 'y':
 
533
                                if (yitcommand == NULL)
 
534
                                        yitcommand = optarg;
 
535
                                else
 
536
                                {
 
537
                                        (void) fprintf(stderr,
 
538
                                                                _("%s: More than one -y option specified\n"),
 
539
                                                                   progname);
 
540
                                        exit(EXIT_FAILURE);
 
541
                                }
 
542
                                break;
 
543
                        case 'L':
 
544
                                if (leapsec == NULL)
 
545
                                        leapsec = optarg;
 
546
                                else
 
547
                                {
 
548
                                        (void) fprintf(stderr,
 
549
                                                                _("%s: More than one -L option specified\n"),
 
550
                                                                   progname);
 
551
                                        exit(EXIT_FAILURE);
 
552
                                }
 
553
                                break;
 
554
                        case 'v':
 
555
                                noise = TRUE;
 
556
                                break;
 
557
                        case 's':
 
558
                                (void) printf("%s: -s ignored\n", progname);
 
559
                                break;
 
560
                }
 
561
        if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
 
562
                usage();                                /* usage message by request */
 
563
        if (directory == NULL)
 
564
                directory = "data";
 
565
        if (yitcommand == NULL)
 
566
                yitcommand = "yearistype";
 
567
 
 
568
        setboundaries();
 
569
 
 
570
        if (optind < argc && leapsec != NULL)
 
571
        {
 
572
                infile(leapsec);
 
573
                adjleap();
 
574
        }
 
575
 
 
576
        for (i = optind; i < argc; ++i)
 
577
                infile(argv[i]);
 
578
        if (errors)
 
579
                exit(EXIT_FAILURE);
 
580
        associate();
 
581
        for (i = 0; i < nzones; i = j)
 
582
        {
 
583
                /*
 
584
                 * Find the next non-continuation zone entry.
 
585
                 */
 
586
                for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
 
587
                        continue;
 
588
                outzone(&zones[i], j - i);
 
589
        }
 
590
 
 
591
        /*
 
592
         * Make links.
 
593
         */
 
594
        for (i = 0; i < nlinks; ++i)
 
595
        {
 
596
                eat(links[i].l_filename, links[i].l_linenum);
 
597
                dolink(links[i].l_from, links[i].l_to);
 
598
                if (noise)
 
599
                        for (j = 0; j < nlinks; ++j)
 
600
                                if (strcmp(links[i].l_to,
 
601
                                                   links[j].l_from) == 0)
 
602
                                        warning(_("link to link"));
 
603
        }
 
604
        if (lcltime != NULL)
 
605
        {
 
606
                eat("command line", 1);
 
607
                dolink(lcltime, TZDEFAULT);
 
608
        }
 
609
        if (psxrules != NULL)
 
610
        {
 
611
                eat("command line", 1);
 
612
                dolink(psxrules, TZDEFRULES);
 
613
        }
 
614
        return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 
615
}
 
616
 
 
617
static void
 
618
dolink(const char *fromfield, const char *tofield)
 
619
{
 
620
        char       *fromname;
 
621
        char       *toname;
 
622
 
 
623
        if (fromfield[0] == '/')
 
624
                fromname = ecpyalloc(fromfield);
 
625
        else
 
626
        {
 
627
                fromname = ecpyalloc(directory);
 
628
                fromname = ecatalloc(fromname, "/");
 
629
                fromname = ecatalloc(fromname, fromfield);
 
630
        }
 
631
        if (tofield[0] == '/')
 
632
                toname = ecpyalloc(tofield);
 
633
        else
 
634
        {
 
635
                toname = ecpyalloc(directory);
 
636
                toname = ecatalloc(toname, "/");
 
637
                toname = ecatalloc(toname, tofield);
 
638
        }
 
639
 
 
640
        /*
 
641
         * We get to be careful here since there's a fair chance of root running
 
642
         * us.
 
643
         */
 
644
        if (!itsdir(toname))
 
645
                (void) remove(toname);
 
646
        if (link(fromname, toname) != 0)
 
647
        {
 
648
                int                     result;
 
649
 
 
650
                if (mkdirs(toname) != 0)
 
651
                        exit(EXIT_FAILURE);
 
652
 
 
653
                result = link(fromname, toname);
 
654
#ifdef HAVE_SYMLINK
 
655
                if (result != 0 &&
 
656
                        access(fromname, F_OK) == 0 &&
 
657
                        !itsdir(fromname))
 
658
                {
 
659
                        const char *s = tofield;
 
660
                        char       *symlinkcontents = NULL;
 
661
 
 
662
                        while ((s = strchr(s + 1, '/')) != NULL)
 
663
                                symlinkcontents = ecatalloc(symlinkcontents, "../");
 
664
                        symlinkcontents = ecatalloc(symlinkcontents, fromfield);
 
665
 
 
666
                        result = symlink(symlinkcontents, toname);
 
667
                        if (result == 0)
 
668
                                warning(_("hard link failed, symbolic link used"));
 
669
                        ifree(symlinkcontents);
 
670
                }
 
671
#endif
 
672
                if (result != 0)
 
673
                {
 
674
                        const char *e = strerror(errno);
 
675
 
 
676
                        (void) fprintf(stderr,
 
677
                                                   _("%s: Cannot link from %s to %s: %s\n"),
 
678
                                                   progname, fromname, toname, e);
 
679
                        exit(EXIT_FAILURE);
 
680
                }
 
681
        }
 
682
        ifree(fromname);
 
683
        ifree(toname);
 
684
}
 
685
 
 
686
#define TIME_T_BITS_IN_FILE   64
 
687
 
 
688
static void
 
689
setboundaries(void)
 
690
{
 
691
        int    i;
 
692
 
 
693
        min_time = -1;
 
694
        for (i = 0; i < TIME_T_BITS_IN_FILE - 1; ++i)
 
695
                min_time *= 2;
 
696
        max_time = -(min_time + 1);
 
697
}
 
698
 
 
699
static int
 
700
itsdir(const char *name)
 
701
{
 
702
        char       *myname;
 
703
        int                     accres;
 
704
 
 
705
        myname = ecpyalloc(name);
 
706
        myname = ecatalloc(myname, "/.");
 
707
        accres = access(myname, F_OK);
 
708
        ifree(myname);
 
709
        return accres == 0;
 
710
}
 
711
 
 
712
/*
 
713
 * Associate sets of rules with zones.
 
714
 */
 
715
 
 
716
/*
 
717
 * Sort by rule name.
 
718
 */
 
719
 
 
720
static int
 
721
rcomp(const void *cp1, const void *cp2)
 
722
{
 
723
        return strcmp(((const struct rule *) cp1)->r_name,
 
724
                                  ((const struct rule *) cp2)->r_name);
 
725
}
 
726
 
 
727
static void
 
728
associate(void)
 
729
{
 
730
        struct zone *zp;
 
731
        struct rule *rp;
 
732
        int                     base,
 
733
                                out;
 
734
        int                     i,
 
735
                                j;
 
736
 
 
737
        if (nrules != 0)
 
738
        {
 
739
                (void) qsort((void *) rules, (size_t) nrules,
 
740
                                         (size_t) sizeof *rules, rcomp);
 
741
                for (i = 0; i < nrules - 1; ++i)
 
742
                {
 
743
                        if (strcmp(rules[i].r_name,
 
744
                                           rules[i + 1].r_name) != 0)
 
745
                                continue;
 
746
                        if (strcmp(rules[i].r_filename,
 
747
                                           rules[i + 1].r_filename) == 0)
 
748
                                continue;
 
749
                        eat(rules[i].r_filename, rules[i].r_linenum);
 
750
                        warning(_("same rule name in multiple files"));
 
751
                        eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
 
752
                        warning(_("same rule name in multiple files"));
 
753
                        for (j = i + 2; j < nrules; ++j)
 
754
                        {
 
755
                                if (strcmp(rules[i].r_name,
 
756
                                                   rules[j].r_name) != 0)
 
757
                                        break;
 
758
                                if (strcmp(rules[i].r_filename,
 
759
                                                   rules[j].r_filename) == 0)
 
760
                                        continue;
 
761
                                if (strcmp(rules[i + 1].r_filename,
 
762
                                                   rules[j].r_filename) == 0)
 
763
                                        continue;
 
764
                                break;
 
765
                        }
 
766
                        i = j - 1;
 
767
                }
 
768
        }
 
769
        for (i = 0; i < nzones; ++i)
 
770
        {
 
771
                zp = &zones[i];
 
772
                zp->z_rules = NULL;
 
773
                zp->z_nrules = 0;
 
774
        }
 
775
        for (base = 0; base < nrules; base = out)
 
776
        {
 
777
                rp = &rules[base];
 
778
                for (out = base + 1; out < nrules; ++out)
 
779
                        if (strcmp(rp->r_name, rules[out].r_name) != 0)
 
780
                                break;
 
781
                for (i = 0; i < nzones; ++i)
 
782
                {
 
783
                        zp = &zones[i];
 
784
                        if (strcmp(zp->z_rule, rp->r_name) != 0)
 
785
                                continue;
 
786
                        zp->z_rules = rp;
 
787
                        zp->z_nrules = out - base;
 
788
                }
 
789
        }
 
790
        for (i = 0; i < nzones; ++i)
 
791
        {
 
792
                zp = &zones[i];
 
793
                if (zp->z_nrules == 0)
 
794
                {
 
795
                        /*
 
796
                         * Maybe we have a local standard time offset.
 
797
                         */
 
798
                        eat(zp->z_filename, zp->z_linenum);
 
799
                        zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
 
800
                                                                  TRUE);
 
801
 
 
802
                        /*
 
803
                         * Note, though, that if there's no rule, a '%s' in the format is
 
804
                         * a bad thing.
 
805
                         */
 
806
                        if (strchr(zp->z_format, '%') != 0)
 
807
                                error(_("%s in ruleless zone"));
 
808
                }
 
809
        }
 
810
        if (errors)
 
811
                exit(EXIT_FAILURE);
 
812
}
 
813
 
 
814
static void
 
815
infile(const char *name)
 
816
{
 
817
        FILE       *fp;
 
818
        char      **fields;
 
819
        char       *cp;
 
820
        const struct lookup *lp;
 
821
        int                     nfields;
 
822
        int                     wantcont;
 
823
        int                     num;
 
824
        char            buf[BUFSIZ];
 
825
 
 
826
        if (strcmp(name, "-") == 0)
 
827
        {
 
828
                name = _("standard input");
 
829
                fp = stdin;
 
830
        }
 
831
        else if ((fp = fopen(name, "r")) == NULL)
 
832
        {
 
833
                const char *e = strerror(errno);
 
834
 
 
835
                (void) fprintf(stderr, _("%s: Cannot open %s: %s\n"),
 
836
                                           progname, name, e);
 
837
                exit(EXIT_FAILURE);
 
838
        }
 
839
        wantcont = FALSE;
 
840
        for (num = 1;; ++num)
 
841
        {
 
842
                eat(name, num);
 
843
                if (fgets(buf, (int) sizeof buf, fp) != buf)
 
844
                        break;
 
845
                cp = strchr(buf, '\n');
 
846
                if (cp == NULL)
 
847
                {
 
848
                        error(_("line too long"));
 
849
                        exit(EXIT_FAILURE);
 
850
                }
 
851
                *cp = '\0';
 
852
                fields = getfields(buf);
 
853
                nfields = 0;
 
854
                while (fields[nfields] != NULL)
 
855
                {
 
856
                        static char nada;
 
857
 
 
858
                        if (strcmp(fields[nfields], "-") == 0)
 
859
                                fields[nfields] = &nada;
 
860
                        ++nfields;
 
861
                }
 
862
                if (nfields == 0)
 
863
                {
 
864
                        /* nothing to do */
 
865
                }
 
866
                else if (wantcont)
 
867
                        wantcont = inzcont(fields, nfields);
 
868
                else
 
869
                {
 
870
                        lp = byword(fields[0], line_codes);
 
871
                        if (lp == NULL)
 
872
                                error(_("input line of unknown type"));
 
873
                        else
 
874
                                switch ((int) (lp->l_value))
 
875
                                {
 
876
                                        case LC_RULE:
 
877
                                                inrule(fields, nfields);
 
878
                                                wantcont = FALSE;
 
879
                                                break;
 
880
                                        case LC_ZONE:
 
881
                                                wantcont = inzone(fields, nfields);
 
882
                                                break;
 
883
                                        case LC_LINK:
 
884
                                                inlink(fields, nfields);
 
885
                                                wantcont = FALSE;
 
886
                                                break;
 
887
                                        case LC_LEAP:
 
888
                                                if (name != leapsec)
 
889
                                                        (void) fprintf(stderr,
 
890
                                                        _("%s: Leap line in non leap seconds file %s\n"),
 
891
                                                                                   progname, name);
 
892
                                                else
 
893
                                                        inleap(fields, nfields);
 
894
                                                wantcont = FALSE;
 
895
                                                break;
 
896
                                        default:        /* "cannot happen" */
 
897
                                                (void) fprintf(stderr,
 
898
                                                                           _("%s: panic: Invalid l_value %d\n"),
 
899
                                                                           progname, lp->l_value);
 
900
                                                exit(EXIT_FAILURE);
 
901
                                }
 
902
                }
 
903
                ifree((char *) fields);
 
904
        }
 
905
        if (ferror(fp))
 
906
        {
 
907
                (void) fprintf(stderr, _("%s: Error reading %s\n"),
 
908
                                           progname, filename);
 
909
                exit(EXIT_FAILURE);
 
910
        }
 
911
        if (fp != stdin && fclose(fp))
 
912
        {
 
913
                const char *e = strerror(errno);
 
914
 
 
915
                (void) fprintf(stderr, _("%s: Error closing %s: %s\n"),
 
916
                                           progname, filename, e);
 
917
                exit(EXIT_FAILURE);
 
918
        }
 
919
        if (wantcont)
 
920
                error(_("expected continuation line not found"));
 
921
}
 
922
 
 
923
/*----------
 
924
 * Convert a string of one of the forms
 
925
 *      h       -h      hh:mm   -hh:mm  hh:mm:ss        -hh:mm:ss
 
926
 * into a number of seconds.
 
927
 * A null string maps to zero.
 
928
 * Call error with errstring and return zero on errors.
 
929
 *----------
 
930
 */
 
931
static long
 
932
gethms(const char *string, const char *errstring, int signable)
 
933
{
 
934
        long            hh;
 
935
        int                     mm,
 
936
                                ss,
 
937
                                sign;
 
938
 
 
939
        if (string == NULL || *string == '\0')
 
940
                return 0;
 
941
        if (!signable)
 
942
                sign = 1;
 
943
        else if (*string == '-')
 
944
        {
 
945
                sign = -1;
 
946
                ++string;
 
947
        }
 
948
        else
 
949
                sign = 1;
 
950
        if (sscanf(string, scheck(string, "%ld"), &hh) == 1)
 
951
                mm = ss = 0;
 
952
        else if (sscanf(string, scheck(string, "%ld:%d"), &hh, &mm) == 2)
 
953
                ss = 0;
 
954
        else if (sscanf(string, scheck(string, "%ld:%d:%d"),
 
955
                                        &hh, &mm, &ss) != 3)
 
956
        {
 
957
                error(errstring);
 
958
                return 0;
 
959
        }
 
960
        if (hh < 0 ||
 
961
                mm < 0 || mm >= MINSPERHOUR ||
 
962
                ss < 0 || ss > SECSPERMIN)
 
963
        {
 
964
                error(errstring);
 
965
                return 0;
 
966
        }
 
967
        if (LONG_MAX / SECSPERHOUR < hh) {
 
968
                error(_("time overflow"));
 
969
                return 0;
 
970
        }
 
971
        if (noise && hh == HOURSPERDAY && mm == 0 && ss == 0)
 
972
                warning(_("24:00 not handled by pre-1998 versions of zic"));
 
973
        if (noise && (hh > HOURSPERDAY ||
 
974
                                  (hh == HOURSPERDAY && (mm != 0 || ss != 0))))
 
975
                warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
 
976
        return oadd(eitol(sign) * hh * eitol(SECSPERHOUR),
 
977
                                eitol(sign) * (eitol(mm) * eitol(SECSPERMIN) + eitol(ss)));
 
978
}
 
979
 
 
980
static void
 
981
inrule(char **fields, int nfields)
 
982
{
 
983
        static struct rule r;
 
984
 
 
985
        if (nfields != RULE_FIELDS)
 
986
        {
 
987
                error(_("wrong number of fields on Rule line"));
 
988
                return;
 
989
        }
 
990
        if (*fields[RF_NAME] == '\0')
 
991
        {
 
992
                error(_("nameless rule"));
 
993
                return;
 
994
        }
 
995
        r.r_filename = filename;
 
996
        r.r_linenum = linenum;
 
997
        r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
 
998
        rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
 
999
                        fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
 
1000
        r.r_name = ecpyalloc(fields[RF_NAME]);
 
1001
        r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
 
1002
        if (max_abbrvar_len < strlen(r.r_abbrvar))
 
1003
                max_abbrvar_len = strlen(r.r_abbrvar);
 
1004
        rules = (struct rule *) (void *) erealloc((char *) rules,
 
1005
                                                                           (int) ((nrules + 1) * sizeof *rules));
 
1006
        rules[nrules++] = r;
 
1007
}
 
1008
 
 
1009
static int
 
1010
inzone(char **fields, int nfields)
 
1011
{
 
1012
        int                     i;
 
1013
        static char *buf;
 
1014
 
 
1015
        if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS)
 
1016
        {
 
1017
                error(_("wrong number of fields on Zone line"));
 
1018
                return FALSE;
 
1019
        }
 
1020
        if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL)
 
1021
        {
 
1022
                buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
 
1023
                (void) sprintf(buf,
 
1024
                                  _("\"Zone %s\" line and -l option are mutually exclusive"),
 
1025
                                           TZDEFAULT);
 
1026
                error(buf);
 
1027
                return FALSE;
 
1028
        }
 
1029
        if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL)
 
1030
        {
 
1031
                buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES)));
 
1032
                (void) sprintf(buf,
 
1033
                                  _("\"Zone %s\" line and -p option are mutually exclusive"),
 
1034
                                           TZDEFRULES);
 
1035
                error(buf);
 
1036
                return FALSE;
 
1037
        }
 
1038
        for (i = 0; i < nzones; ++i)
 
1039
                if (zones[i].z_name != NULL &&
 
1040
                        strcmp(zones[i].z_name, fields[ZF_NAME]) == 0)
 
1041
                {
 
1042
                        buf = erealloc(buf, (int) (132 +
 
1043
                                                                           strlen(fields[ZF_NAME]) +
 
1044
                                                                           strlen(zones[i].z_filename)));
 
1045
                        (void) sprintf(buf,
 
1046
                                                   _("duplicate zone name %s (file \"%s\", line %d)"),
 
1047
                                                   fields[ZF_NAME],
 
1048
                                                   zones[i].z_filename,
 
1049
                                                   zones[i].z_linenum);
 
1050
                        error(buf);
 
1051
                        return FALSE;
 
1052
                }
 
1053
        return inzsub(fields, nfields, FALSE);
 
1054
}
 
1055
 
 
1056
static int
 
1057
inzcont(char **fields, int nfields)
 
1058
{
 
1059
        if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS)
 
1060
        {
 
1061
                error(_("wrong number of fields on Zone continuation line"));
 
1062
                return FALSE;
 
1063
        }
 
1064
        return inzsub(fields, nfields, TRUE);
 
1065
}
 
1066
 
 
1067
static int
 
1068
inzsub(char **fields, int nfields, int iscont)
 
1069
{
 
1070
        char       *cp;
 
1071
        static struct zone z;
 
1072
        int                     i_gmtoff,
 
1073
                                i_rule,
 
1074
                                i_format;
 
1075
        int                     i_untilyear,
 
1076
                                i_untilmonth;
 
1077
        int                     i_untilday,
 
1078
                                i_untiltime;
 
1079
        int                     hasuntil;
 
1080
 
 
1081
        if (iscont)
 
1082
        {
 
1083
                i_gmtoff = ZFC_GMTOFF;
 
1084
                i_rule = ZFC_RULE;
 
1085
                i_format = ZFC_FORMAT;
 
1086
                i_untilyear = ZFC_TILYEAR;
 
1087
                i_untilmonth = ZFC_TILMONTH;
 
1088
                i_untilday = ZFC_TILDAY;
 
1089
                i_untiltime = ZFC_TILTIME;
 
1090
                z.z_name = NULL;
 
1091
        }
 
1092
        else
 
1093
        {
 
1094
                i_gmtoff = ZF_GMTOFF;
 
1095
                i_rule = ZF_RULE;
 
1096
                i_format = ZF_FORMAT;
 
1097
                i_untilyear = ZF_TILYEAR;
 
1098
                i_untilmonth = ZF_TILMONTH;
 
1099
                i_untilday = ZF_TILDAY;
 
1100
                i_untiltime = ZF_TILTIME;
 
1101
                z.z_name = ecpyalloc(fields[ZF_NAME]);
 
1102
        }
 
1103
        z.z_filename = filename;
 
1104
        z.z_linenum = linenum;
 
1105
        z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UTC offset"), TRUE);
 
1106
        if ((cp = strchr(fields[i_format], '%')) != 0)
 
1107
        {
 
1108
                if (*++cp != 's' || strchr(cp, '%') != 0)
 
1109
                {
 
1110
                        error(_("invalid abbreviation format"));
 
1111
                        return FALSE;
 
1112
                }
 
1113
        }
 
1114
        z.z_rule = ecpyalloc(fields[i_rule]);
 
1115
        z.z_format = ecpyalloc(fields[i_format]);
 
1116
        if (max_format_len < strlen(z.z_format))
 
1117
                max_format_len = strlen(z.z_format);
 
1118
        hasuntil = nfields > i_untilyear;
 
1119
        if (hasuntil)
 
1120
        {
 
1121
                z.z_untilrule.r_filename = filename;
 
1122
                z.z_untilrule.r_linenum = linenum;
 
1123
                rulesub(&z.z_untilrule,
 
1124
                                fields[i_untilyear],
 
1125
                                "only",
 
1126
                                "",
 
1127
                                (nfields > i_untilmonth) ?
 
1128
                                fields[i_untilmonth] : "Jan",
 
1129
                                (nfields > i_untilday) ? fields[i_untilday] : "1",
 
1130
                                (nfields > i_untiltime) ? fields[i_untiltime] : "0");
 
1131
                z.z_untiltime = rpytime(&z.z_untilrule,
 
1132
                                                                z.z_untilrule.r_loyear);
 
1133
                if (iscont && nzones > 0 &&
 
1134
                        z.z_untiltime > min_time &&
 
1135
                        z.z_untiltime < max_time &&
 
1136
                        zones[nzones - 1].z_untiltime > min_time &&
 
1137
                        zones[nzones - 1].z_untiltime < max_time &&
 
1138
                        zones[nzones - 1].z_untiltime >= z.z_untiltime)
 
1139
                {
 
1140
                        error(_("Zone continuation line end time is not after end time of previous line"));
 
1141
                        return FALSE;
 
1142
                }
 
1143
        }
 
1144
        zones = (struct zone *) (void *) erealloc((char *) zones,
 
1145
                                                                           (int) ((nzones + 1) * sizeof *zones));
 
1146
        zones[nzones++] = z;
 
1147
 
 
1148
        /*
 
1149
         * If there was an UNTIL field on this line, there's more information
 
1150
         * about the zone on the next line.
 
1151
         */
 
1152
        return hasuntil;
 
1153
}
 
1154
 
 
1155
static void
 
1156
inleap(char **fields, int nfields)
 
1157
{
 
1158
        const char *cp;
 
1159
        const struct lookup *lp;
 
1160
        int                     i,
 
1161
                                j;
 
1162
        int                     year,
 
1163
                                month,
 
1164
                                day;
 
1165
        long            dayoff,
 
1166
                                tod;
 
1167
        zic_t           t;
 
1168
 
 
1169
        if (nfields != LEAP_FIELDS)
 
1170
        {
 
1171
                error(_("wrong number of fields on Leap line"));
 
1172
                return;
 
1173
        }
 
1174
        dayoff = 0;
 
1175
        cp = fields[LP_YEAR];
 
1176
        if (sscanf(cp, scheck(cp, "%d"), &year) != 1)
 
1177
        {
 
1178
                /*
 
1179
                 * Leapin' Lizards!
 
1180
                 */
 
1181
                error(_("invalid leaping year"));
 
1182
                return;
 
1183
        }
 
1184
        if (!leapseen || leapmaxyear < year)
 
1185
                leapmaxyear = year;
 
1186
        if (!leapseen || leapminyear > year)
 
1187
                leapminyear = year;
 
1188
        leapseen = TRUE;
 
1189
        j = EPOCH_YEAR;
 
1190
        while (j != year)
 
1191
        {
 
1192
                if (year > j)
 
1193
                {
 
1194
                        i = len_years[isleap(j)];
 
1195
                        ++j;
 
1196
                }
 
1197
                else
 
1198
                {
 
1199
                        --j;
 
1200
                        i = -len_years[isleap(j)];
 
1201
                }
 
1202
                dayoff = oadd(dayoff, eitol(i));
 
1203
        }
 
1204
        if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL)
 
1205
        {
 
1206
                error(_("invalid month name"));
 
1207
                return;
 
1208
        }
 
1209
        month = lp->l_value;
 
1210
        j = TM_JANUARY;
 
1211
        while (j != month)
 
1212
        {
 
1213
                i = len_months[isleap(year)][j];
 
1214
                dayoff = oadd(dayoff, eitol(i));
 
1215
                ++j;
 
1216
        }
 
1217
        cp = fields[LP_DAY];
 
1218
        if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
 
1219
                day <= 0 || day > len_months[isleap(year)][month])
 
1220
        {
 
1221
                error(_("invalid day of month"));
 
1222
                return;
 
1223
        }
 
1224
        dayoff = oadd(dayoff, eitol(day - 1));
 
1225
        if (dayoff < min_time / SECSPERDAY)
 
1226
        {
 
1227
                error(_("time too small"));
 
1228
                return;
 
1229
        }
 
1230
        if (dayoff > max_time / SECSPERDAY)
 
1231
        {
 
1232
                error(_("time too large"));
 
1233
                return;
 
1234
        }
 
1235
        t = (zic_t) dayoff *SECSPERDAY;
 
1236
 
 
1237
        tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE);
 
1238
        cp = fields[LP_CORR];
 
1239
        {
 
1240
                int                     positive;
 
1241
                int                     count;
 
1242
 
 
1243
                if (strcmp(cp, "") == 0)
 
1244
                {                                               /* infile() turns "-" into "" */
 
1245
                        positive = FALSE;
 
1246
                        count = 1;
 
1247
                }
 
1248
                else if (strcmp(cp, "--") == 0)
 
1249
                {
 
1250
                        positive = FALSE;
 
1251
                        count = 2;
 
1252
                }
 
1253
                else if (strcmp(cp, "+") == 0)
 
1254
                {
 
1255
                        positive = TRUE;
 
1256
                        count = 1;
 
1257
                }
 
1258
                else if (strcmp(cp, "++") == 0)
 
1259
                {
 
1260
                        positive = TRUE;
 
1261
                        count = 2;
 
1262
                }
 
1263
                else
 
1264
                {
 
1265
                        error(_("illegal CORRECTION field on Leap line"));
 
1266
                        return;
 
1267
                }
 
1268
                if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL)
 
1269
                {
 
1270
                        error(_("illegal Rolling/Stationary field on Leap line"));
 
1271
                        return;
 
1272
                }
 
1273
                leapadd(tadd(t, tod), positive, lp->l_value, count);
 
1274
        }
 
1275
}
 
1276
 
 
1277
static void
 
1278
inlink(char **fields, int nfields)
 
1279
{
 
1280
        struct link l;
 
1281
 
 
1282
        if (nfields != LINK_FIELDS)
 
1283
        {
 
1284
                error(_("wrong number of fields on Link line"));
 
1285
                return;
 
1286
        }
 
1287
        if (*fields[LF_FROM] == '\0')
 
1288
        {
 
1289
                error(_("blank FROM field on Link line"));
 
1290
                return;
 
1291
        }
 
1292
        if (*fields[LF_TO] == '\0')
 
1293
        {
 
1294
                error(_("blank TO field on Link line"));
 
1295
                return;
 
1296
        }
 
1297
        l.l_filename = filename;
 
1298
        l.l_linenum = linenum;
 
1299
        l.l_from = ecpyalloc(fields[LF_FROM]);
 
1300
        l.l_to = ecpyalloc(fields[LF_TO]);
 
1301
        links = (struct link *) (void *) erealloc((char *) links,
 
1302
                                                                           (int) ((nlinks + 1) * sizeof *links));
 
1303
        links[nlinks++] = l;
 
1304
}
 
1305
 
 
1306
static void
 
1307
rulesub(struct rule * rp, const char *loyearp, const char *hiyearp,
 
1308
                const char *typep, const char *monthp, const char *dayp,
 
1309
                const char *timep)
 
1310
{
 
1311
        const struct lookup *lp;
 
1312
        const char *cp;
 
1313
        char       *dp;
 
1314
        char       *ep;
 
1315
 
 
1316
        if ((lp = byword(monthp, mon_names)) == NULL)
 
1317
        {
 
1318
                error(_("invalid month name"));
 
1319
                return;
 
1320
        }
 
1321
        rp->r_month = lp->l_value;
 
1322
        rp->r_todisstd = FALSE;
 
1323
        rp->r_todisgmt = FALSE;
 
1324
        dp = ecpyalloc(timep);
 
1325
        if (*dp != '\0')
 
1326
        {
 
1327
                ep = dp + strlen(dp) - 1;
 
1328
                switch (lowerit(*ep))
 
1329
                {
 
1330
                        case 's':                       /* Standard */
 
1331
                                rp->r_todisstd = TRUE;
 
1332
                                rp->r_todisgmt = FALSE;
 
1333
                                *ep = '\0';
 
1334
                                break;
 
1335
                        case 'w':                       /* Wall */
 
1336
                                rp->r_todisstd = FALSE;
 
1337
                                rp->r_todisgmt = FALSE;
 
1338
                                *ep = '\0';
 
1339
                                break;
 
1340
                        case 'g':                       /* Greenwich */
 
1341
                        case 'u':                       /* Universal */
 
1342
                        case 'z':                       /* Zulu */
 
1343
                                rp->r_todisstd = TRUE;
 
1344
                                rp->r_todisgmt = TRUE;
 
1345
                                *ep = '\0';
 
1346
                                break;
 
1347
                }
 
1348
        }
 
1349
        rp->r_tod = gethms(dp, _("invalid time of day"), FALSE);
 
1350
        ifree(dp);
 
1351
 
 
1352
        /*
 
1353
         * Year work.
 
1354
         */
 
1355
        cp = loyearp;
 
1356
        lp = byword(cp, begin_years);
 
1357
        rp->r_lowasnum = lp == NULL;
 
1358
        if (!rp->r_lowasnum)
 
1359
                switch ((int) lp->l_value)
 
1360
                {
 
1361
                        case YR_MINIMUM:
 
1362
                                rp->r_loyear = INT_MIN;
 
1363
                                break;
 
1364
                        case YR_MAXIMUM:
 
1365
                                rp->r_loyear = INT_MAX;
 
1366
                                break;
 
1367
                        default:                        /* "cannot happen" */
 
1368
                                (void) fprintf(stderr,
 
1369
                                                           _("%s: panic: Invalid l_value %d\n"),
 
1370
                                                           progname, lp->l_value);
 
1371
                                exit(EXIT_FAILURE);
 
1372
                }
 
1373
        else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1)
 
1374
        {
 
1375
                error(_("invalid starting year"));
 
1376
                return;
 
1377
        }
 
1378
        cp = hiyearp;
 
1379
        lp = byword(cp, end_years);
 
1380
        rp->r_hiwasnum = lp == NULL;
 
1381
        if (!rp->r_hiwasnum)
 
1382
                switch ((int) lp->l_value)
 
1383
                {
 
1384
                        case YR_MINIMUM:
 
1385
                                rp->r_hiyear = INT_MIN;
 
1386
                                break;
 
1387
                        case YR_MAXIMUM:
 
1388
                                rp->r_hiyear = INT_MAX;
 
1389
                                break;
 
1390
                        case YR_ONLY:
 
1391
                                rp->r_hiyear = rp->r_loyear;
 
1392
                                break;
 
1393
                        default:                        /* "cannot happen" */
 
1394
                                (void) fprintf(stderr,
 
1395
                                                           _("%s: panic: Invalid l_value %d\n"),
 
1396
                                                           progname, lp->l_value);
 
1397
                                exit(EXIT_FAILURE);
 
1398
                }
 
1399
        else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1)
 
1400
        {
 
1401
                error(_("invalid ending year"));
 
1402
                return;
 
1403
        }
 
1404
        if (rp->r_loyear > rp->r_hiyear)
 
1405
        {
 
1406
                error(_("starting year greater than ending year"));
 
1407
                return;
 
1408
        }
 
1409
        if (*typep == '\0')
 
1410
                rp->r_yrtype = NULL;
 
1411
        else
 
1412
        {
 
1413
                if (rp->r_loyear == rp->r_hiyear)
 
1414
                {
 
1415
                        error(_("typed single year"));
 
1416
                        return;
 
1417
                }
 
1418
                rp->r_yrtype = ecpyalloc(typep);
 
1419
        }
 
1420
 
 
1421
        /*
 
1422
         * Day work. Accept things such as:  1  last-Sunday  Sun<=20  Sun>=7
 
1423
         */
 
1424
        dp = ecpyalloc(dayp);
 
1425
        if ((lp = byword(dp, lasts)) != NULL)
 
1426
        {
 
1427
                rp->r_dycode = DC_DOWLEQ;
 
1428
                rp->r_wday = lp->l_value;
 
1429
                rp->r_dayofmonth = len_months[1][rp->r_month];
 
1430
        }
 
1431
        else
 
1432
        {
 
1433
                if ((ep = strchr(dp, '<')) != 0)
 
1434
                        rp->r_dycode = DC_DOWLEQ;
 
1435
                else if ((ep = strchr(dp, '>')) != 0)
 
1436
                        rp->r_dycode = DC_DOWGEQ;
 
1437
                else
 
1438
                {
 
1439
                        ep = dp;
 
1440
                        rp->r_dycode = DC_DOM;
 
1441
                }
 
1442
                if (rp->r_dycode != DC_DOM)
 
1443
                {
 
1444
                        *ep++ = 0;
 
1445
                        if (*ep++ != '=')
 
1446
                        {
 
1447
                                error(_("invalid day of month"));
 
1448
                                ifree(dp);
 
1449
                                return;
 
1450
                        }
 
1451
                        if ((lp = byword(dp, wday_names)) == NULL)
 
1452
                        {
 
1453
                                error(_("invalid weekday name"));
 
1454
                                ifree(dp);
 
1455
                                return;
 
1456
                        }
 
1457
                        rp->r_wday = lp->l_value;
 
1458
                }
 
1459
                if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
 
1460
                        rp->r_dayofmonth <= 0 ||
 
1461
                        (rp->r_dayofmonth > len_months[1][rp->r_month]))
 
1462
                {
 
1463
                        error(_("invalid day of month"));
 
1464
                        ifree(dp);
 
1465
                        return;
 
1466
                }
 
1467
        }
 
1468
        ifree(dp);
 
1469
}
 
1470
 
 
1471
static void
 
1472
convert(long val, char *buf)
 
1473
{
 
1474
        int                     i;
 
1475
        int                     shift;
 
1476
 
 
1477
        for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
 
1478
                buf[i] = val >> shift;
 
1479
}
 
1480
 
 
1481
static void
 
1482
convert64(zic_t val, char *buf)
 
1483
{
 
1484
        int    i;
 
1485
        int    shift;
 
1486
 
 
1487
        for (i = 0, shift = 56; i < 8; ++i, shift -= 8)
 
1488
                buf[i] = val >> shift;
 
1489
}
 
1490
 
 
1491
static void
 
1492
puttzcode(long val, FILE *fp)
 
1493
{
 
1494
        char            buf[4];
 
1495
 
 
1496
        convert(val, buf);
 
1497
        (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
 
1498
}
 
1499
 
 
1500
static void
 
1501
puttzcode64(zic_t val, FILE *fp)
 
1502
{
 
1503
        char    buf[8];
 
1504
 
 
1505
        convert64(val, buf);
 
1506
        (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
 
1507
}
 
1508
 
 
1509
static int
 
1510
atcomp(const void *avp, const void *bvp)
 
1511
{
 
1512
        const zic_t     a = ((const struct attype *) avp)->at;
 
1513
        const zic_t     b = ((const struct attype *) bvp)->at;
 
1514
 
 
1515
        return (a < b) ? -1 : (a > b);
 
1516
}
 
1517
 
 
1518
static int
 
1519
is32(zic_t x)
 
1520
{
 
1521
        return x == ((zic_t) ((int32) x));
 
1522
}
 
1523
 
 
1524
static void
 
1525
writezone(const char *name, const char *string)
 
1526
{
 
1527
        FILE       *fp;
 
1528
        int                     i,
 
1529
                                j;
 
1530
        int                     leapcnt32, leapi32;
 
1531
        int                     timecnt32, timei32;
 
1532
        int                     pass;
 
1533
        static char *fullname;
 
1534
        static const struct tzhead tzh0;
 
1535
        static struct tzhead tzh;
 
1536
        zic_t           ats[TZ_MAX_TIMES];
 
1537
        unsigned char types[TZ_MAX_TIMES];
 
1538
 
 
1539
        /*
 
1540
         * Sort.
 
1541
         */
 
1542
        if (timecnt > 1)
 
1543
                (void) qsort((void *) attypes, (size_t) timecnt,
 
1544
                                         (size_t) sizeof *attypes, atcomp);
 
1545
 
 
1546
        /*
 
1547
         * Optimize.
 
1548
         */
 
1549
        {
 
1550
                int                     fromi;
 
1551
                int                     toi;
 
1552
 
 
1553
                toi = 0;
 
1554
                fromi = 0;
 
1555
                while (fromi < timecnt && attypes[fromi].at < min_time)
 
1556
                        ++fromi;
 
1557
                if (isdsts[0] == 0)
 
1558
                        while (fromi < timecnt && attypes[fromi].type == 0)
 
1559
                                ++fromi;                /* handled by default rule */
 
1560
                for (; fromi < timecnt; ++fromi)
 
1561
                {
 
1562
                        if (toi != 0
 
1563
                                && ((attypes[fromi].at
 
1564
                                         + gmtoffs[attypes[toi - 1].type])
 
1565
                                        <= (attypes[toi - 1].at
 
1566
                                                + gmtoffs[toi == 1 ? 0
 
1567
                                                                  : attypes[toi - 2].type])))
 
1568
                        {
 
1569
                                attypes[toi - 1].type = attypes[fromi].type;
 
1570
                                continue;
 
1571
                        }
 
1572
                        if (toi == 0 ||
 
1573
                                attypes[toi - 1].type != attypes[fromi].type)
 
1574
                                attypes[toi++] = attypes[fromi];
 
1575
                }
 
1576
                timecnt = toi;
 
1577
        }
 
1578
 
 
1579
        /*
 
1580
         * Transfer.
 
1581
         */
 
1582
        for (i = 0; i < timecnt; ++i)
 
1583
        {
 
1584
                ats[i] = attypes[i].at;
 
1585
                types[i] = attypes[i].type;
 
1586
        }
 
1587
        /*
 
1588
         * Correct for leap seconds.
 
1589
         */
 
1590
        for (i = 0; i < timecnt; ++i) {
 
1591
                j = leapcnt;
 
1592
                while (--j >= 0)
 
1593
                        if (ats[i] > trans[j] - corr[j]) {
 
1594
                                ats[i] = tadd(ats[i], corr[j]);
 
1595
                                break;
 
1596
                        }
 
1597
        }
 
1598
        /*
 
1599
         * Figure out 32-bit-limited starts and counts.
 
1600
         */
 
1601
        timecnt32 = timecnt;
 
1602
        timei32 = 0;
 
1603
        leapcnt32 = leapcnt;
 
1604
        leapi32 = 0;
 
1605
        while (timecnt32 > 0 && !is32(ats[timecnt32 - 1]))
 
1606
                --timecnt32;
 
1607
        while (timecnt32 > 0 && !is32(ats[timei32]))
 
1608
        {
 
1609
                --timecnt32;
 
1610
                ++timei32;
 
1611
        }
 
1612
        while (leapcnt32 > 0 && !is32(trans[leapcnt32 - 1]))
 
1613
                --leapcnt32;
 
1614
        while (leapcnt32 > 0 && !is32(trans[leapi32]))
 
1615
        {
 
1616
                --leapcnt32;
 
1617
                ++leapi32;
 
1618
        }
 
1619
        fullname = erealloc(fullname,
 
1620
                                                (int) (strlen(directory) + 1 + strlen(name) + 1));
 
1621
        (void) sprintf(fullname, "%s/%s", directory, name);
 
1622
 
 
1623
        /*
 
1624
         * Remove old file, if any, to snap links.
 
1625
         */
 
1626
        if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT)
 
1627
        {
 
1628
                const char *e = strerror(errno);
 
1629
 
 
1630
                (void) fprintf(stderr, _("%s: Cannot remove %s: %s\n"),
 
1631
                                           progname, fullname, e);
 
1632
                exit(EXIT_FAILURE);
 
1633
        }
 
1634
        if ((fp = fopen(fullname, "wb")) == NULL)
 
1635
        {
 
1636
                if (mkdirs(fullname) != 0)
 
1637
                        (void) exit(EXIT_FAILURE);
 
1638
                if ((fp = fopen(fullname, "wb")) == NULL)
 
1639
                {
 
1640
                        const char *e = strerror(errno);
 
1641
 
 
1642
                        (void) fprintf(stderr, _("%s: Cannot create %s: %s\n"),
 
1643
                                                   progname, fullname, e);
 
1644
                        exit(EXIT_FAILURE);
 
1645
                }
 
1646
        }
 
1647
        for (pass = 1; pass <= 2; ++pass) {
 
1648
                register int    thistimei, thistimecnt;
 
1649
                register int    thisleapi, thisleapcnt;
 
1650
                register int    thistimelim, thisleaplim;
 
1651
                int             writetype[TZ_MAX_TIMES];
 
1652
                int             typemap[TZ_MAX_TYPES];
 
1653
                register int    thistypecnt;
 
1654
                char            thischars[TZ_MAX_CHARS];
 
1655
                char            thischarcnt;
 
1656
                int             indmap[TZ_MAX_CHARS];
 
1657
 
 
1658
                if (pass == 1) {
 
1659
                        thistimei = timei32;
 
1660
                        thistimecnt = timecnt32;
 
1661
                        thisleapi = leapi32;
 
1662
                        thisleapcnt = leapcnt32;
 
1663
                } else {
 
1664
                        thistimei = 0;
 
1665
                        thistimecnt = timecnt;
 
1666
                        thisleapi = 0;
 
1667
                        thisleapcnt = leapcnt;
 
1668
                }
 
1669
                thistimelim = thistimei + thistimecnt;
 
1670
                thisleaplim = thisleapi + thisleapcnt;
 
1671
                for (i = 0; i < typecnt; ++i)
 
1672
                        writetype[i] = thistimecnt == timecnt;
 
1673
                if (thistimecnt == 0) {
 
1674
                        /*
 
1675
                        ** No transition times fall in the current
 
1676
                        ** (32- or 64-bit) window.
 
1677
                        */
 
1678
                        if (typecnt != 0)
 
1679
                                writetype[typecnt - 1] = TRUE;
 
1680
                } else {
 
1681
                        for (i = thistimei - 1; i < thistimelim; ++i)
 
1682
                                if (i >= 0)
 
1683
                                        writetype[types[i]] = TRUE;
 
1684
                        /*
 
1685
                        ** For America/Godthab and Antarctica/Palmer
 
1686
                        */
 
1687
                        if (thistimei == 0)
 
1688
                                writetype[0] = TRUE;
 
1689
                }
 
1690
                thistypecnt = 0;
 
1691
                for (i = 0; i < typecnt; ++i)
 
1692
                        typemap[i] = writetype[i] ?  thistypecnt++ : -1;
 
1693
                for (i = 0; i < sizeof indmap / sizeof indmap[0]; ++i)
 
1694
                        indmap[i] = -1;
 
1695
                thischarcnt = 0;
 
1696
                for (i = 0; i < typecnt; ++i) {
 
1697
                        register char * thisabbr;
 
1698
 
 
1699
                        if (!writetype[i])
 
1700
                                continue;
 
1701
                        if (indmap[abbrinds[i]] >= 0)
 
1702
                                continue;
 
1703
                        thisabbr = &chars[abbrinds[i]];
 
1704
                        for (j = 0; j < thischarcnt; ++j)
 
1705
                                if (strcmp(&thischars[j], thisabbr) == 0)
 
1706
                                        break;
 
1707
                        if (j == thischarcnt) {
 
1708
                                (void) strcpy(&thischars[(int) thischarcnt],
 
1709
                                        thisabbr);
 
1710
                                thischarcnt += strlen(thisabbr) + 1;
 
1711
                        }
 
1712
                        indmap[abbrinds[i]] = j;
 
1713
                }
 
1714
#define DO(field)       (void) fwrite((void *) tzh.field, \
 
1715
                                (size_t) sizeof tzh.field, (size_t) 1, fp)
 
1716
                tzh = tzh0;
 
1717
                (void) strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
 
1718
                tzh.tzh_version[0] = ZIC_VERSION;
 
1719
                convert(eitol(thistypecnt), tzh.tzh_ttisgmtcnt);
 
1720
                convert(eitol(thistypecnt), tzh.tzh_ttisstdcnt);
 
1721
                convert(eitol(thisleapcnt), tzh.tzh_leapcnt);
 
1722
                convert(eitol(thistimecnt), tzh.tzh_timecnt);
 
1723
                convert(eitol(thistypecnt), tzh.tzh_typecnt);
 
1724
                convert(eitol(thischarcnt), tzh.tzh_charcnt);
 
1725
                DO(tzh_magic);
 
1726
                DO(tzh_version);
 
1727
                DO(tzh_reserved);
 
1728
                DO(tzh_ttisgmtcnt);
 
1729
                DO(tzh_ttisstdcnt);
 
1730
                DO(tzh_leapcnt);
 
1731
                DO(tzh_timecnt);
 
1732
                DO(tzh_typecnt);
 
1733
                DO(tzh_charcnt);
 
1734
#undef DO
 
1735
                for (i = thistimei; i < thistimelim; ++i)
 
1736
                        if (pass == 1)
 
1737
                                puttzcode((long) ats[i], fp);
 
1738
                        else    puttzcode64(ats[i], fp);
 
1739
                for (i = thistimei; i < thistimelim; ++i) {
 
1740
                        unsigned char   uc;
 
1741
 
 
1742
                        uc = typemap[types[i]];
 
1743
                        (void) fwrite((void *) &uc,
 
1744
                                (size_t) sizeof uc,
 
1745
                                (size_t) 1,
 
1746
                                fp);
 
1747
                }
 
1748
                for (i = 0; i < typecnt; ++i)
 
1749
                        if (writetype[i]) {
 
1750
                                puttzcode(gmtoffs[i], fp);
 
1751
                                (void) putc(isdsts[i], fp);
 
1752
                                (void) putc((unsigned char) indmap[abbrinds[i]], fp);
 
1753
                        }
 
1754
                if (thischarcnt != 0)
 
1755
                        (void) fwrite((void *) thischars,
 
1756
                                (size_t) sizeof thischars[0],
 
1757
                                (size_t) thischarcnt, fp);
 
1758
                for (i = thisleapi; i < thisleaplim; ++i) {
 
1759
                        register zic_t  todo;
 
1760
 
 
1761
                        if (roll[i]) {
 
1762
                                if (timecnt == 0 || trans[i] < ats[0]) {
 
1763
                                        j = 0;
 
1764
                                        while (isdsts[j])
 
1765
                                                if (++j >= typecnt) {
 
1766
                                                        j = 0;
 
1767
                                                        break;
 
1768
                                                }
 
1769
                                } else {
 
1770
                                        j = 1;
 
1771
                                        while (j < timecnt &&
 
1772
                                                trans[i] >= ats[j])
 
1773
                                                        ++j;
 
1774
                                        j = types[j - 1];
 
1775
                                }
 
1776
                                todo = tadd(trans[i], -gmtoffs[j]);
 
1777
                        } else  todo = trans[i];
 
1778
                        if (pass == 1)
 
1779
                                puttzcode((long) todo, fp);
 
1780
                        else    puttzcode64(todo, fp);
 
1781
                        puttzcode(corr[i], fp);
 
1782
                }
 
1783
                for (i = 0; i < typecnt; ++i)
 
1784
                        if (writetype[i])
 
1785
                                (void) putc(ttisstds[i], fp);
 
1786
                for (i = 0; i < typecnt; ++i)
 
1787
                        if (writetype[i])
 
1788
                                (void) putc(ttisgmts[i], fp);
 
1789
        }
 
1790
        (void) fprintf(fp, "\n%s\n", string);
 
1791
        if (ferror(fp) || fclose(fp)) {
 
1792
                (void) fprintf(stderr, _("%s: Error writing %s\n"),
 
1793
                        progname, fullname);
 
1794
                exit(EXIT_FAILURE);
 
1795
        }
 
1796
}
 
1797
 
 
1798
static void
 
1799
doabbr(char *abbr, const char *format, const char *letters, int isdst, 
 
1800
           int doquotes)
 
1801
{
 
1802
        char * cp;
 
1803
        char * slashp;
 
1804
        int    len;
 
1805
 
 
1806
        slashp = strchr(format, '/');
 
1807
        if (slashp == NULL)
 
1808
        {
 
1809
                if (letters == NULL)
 
1810
                        (void) strcpy(abbr, format);
 
1811
                else
 
1812
                        (void) sprintf(abbr, format, letters);
 
1813
        }
 
1814
        else if (isdst)
 
1815
                (void) strcpy(abbr, slashp + 1);
 
1816
        else
 
1817
        {
 
1818
                if (slashp > format)
 
1819
                        (void) strncpy(abbr, format,
 
1820
                                                   (unsigned) (slashp - format));
 
1821
                abbr[slashp - format] = '\0';
 
1822
        }
 
1823
        if (!doquotes)
 
1824
                return;
 
1825
        for (cp = abbr; *cp != '\0'; ++cp)
 
1826
                if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", *cp) == NULL &&
 
1827
                        strchr("abcdefghijklmnopqrstuvwxyz", *cp) == NULL)
 
1828
                        break;
 
1829
        len = strlen(abbr);
 
1830
        if (len > 0 && *cp == '\0')
 
1831
                return;
 
1832
        abbr[len + 2] = '\0';
 
1833
        abbr[len + 1] = '>';
 
1834
        for ( ; len > 0; --len)
 
1835
                abbr[len] = abbr[len - 1];
 
1836
        abbr[0] = '<';
 
1837
}
 
1838
 
 
1839
static void
 
1840
updateminmax(int x)
 
1841
{
 
1842
        if (min_year > x)
 
1843
                min_year = x;
 
1844
        if (max_year < x)
 
1845
                max_year = x;
 
1846
}
 
1847
 
 
1848
static int
 
1849
stringoffset(char *result, long offset)
 
1850
{
 
1851
        int    hours;
 
1852
        int    minutes;
 
1853
        int    seconds;
 
1854
 
 
1855
        result[0] = '\0';
 
1856
        if (offset < 0) {
 
1857
                (void) strcpy(result, "-");
 
1858
                offset = -offset;
 
1859
        }
 
1860
        seconds = offset % SECSPERMIN;
 
1861
        offset /= SECSPERMIN;
 
1862
        minutes = offset % MINSPERHOUR;
 
1863
        offset /= MINSPERHOUR;
 
1864
        hours = offset;
 
1865
        if (hours >= HOURSPERDAY) {
 
1866
                result[0] = '\0';
 
1867
                return -1;
 
1868
        }
 
1869
        (void) sprintf(end(result), "%d", hours);
 
1870
        if (minutes != 0 || seconds != 0) {
 
1871
                (void) sprintf(end(result), ":%02d", minutes);
 
1872
                if (seconds != 0)
 
1873
                        (void) sprintf(end(result), ":%02d", seconds);
 
1874
        }
 
1875
        return 0;
 
1876
}
 
1877
 
 
1878
static int
 
1879
stringrule(char *result, const struct rule *rp, long dstoff, long gmtoff)
 
1880
{
 
1881
        long    tod;
 
1882
 
 
1883
        result = end(result);
 
1884
        if (rp->r_dycode == DC_DOM) 
 
1885
        {
 
1886
                int     month, total;
 
1887
 
 
1888
                if (rp->r_dayofmonth == 29 && rp->r_month == TM_FEBRUARY)
 
1889
                        return -1;
 
1890
                total = 0;
 
1891
                for (month = 0; month < rp->r_month; ++month)
 
1892
                        total += len_months[0][month];
 
1893
                (void) sprintf(result, "J%d", total + rp->r_dayofmonth);
 
1894
        }
 
1895
        else
 
1896
        {
 
1897
                int     week;
 
1898
 
 
1899
                if (rp->r_dycode == DC_DOWGEQ)
 
1900
                {
 
1901
                        week = 1 + rp->r_dayofmonth / DAYSPERWEEK;
 
1902
                        if ((week - 1) * DAYSPERWEEK + 1 != rp->r_dayofmonth)
 
1903
                                return -1;
 
1904
                }
 
1905
                else if (rp->r_dycode == DC_DOWLEQ)
 
1906
                {
 
1907
                        if (rp->r_dayofmonth == len_months[1][rp->r_month])
 
1908
                                week = 5;
 
1909
                        else {
 
1910
                                week = 1 + rp->r_dayofmonth / DAYSPERWEEK;
 
1911
                                if (week * DAYSPERWEEK - 1 != rp->r_dayofmonth)
 
1912
                                        return -1;
 
1913
                        }
 
1914
                }
 
1915
                else
 
1916
                        return -1;      /* "cannot happen" */
 
1917
                (void) sprintf(result, "M%d.%d.%d",
 
1918
                                           rp->r_month + 1, week, rp->r_wday);
 
1919
        }
 
1920
        tod = rp->r_tod;
 
1921
        if (rp->r_todisgmt)
 
1922
                tod += gmtoff;
 
1923
        if (rp->r_todisstd && rp->r_stdoff == 0)
 
1924
                tod += dstoff;
 
1925
        if (tod < 0)
 
1926
        {
 
1927
                result[0] = '\0';
 
1928
                return -1;
 
1929
        }
 
1930
        if (tod != 2 * SECSPERMIN * MINSPERHOUR)
 
1931
        {
 
1932
                (void) strcat(result, "/");
 
1933
                if (stringoffset(end(result), tod) != 0)
 
1934
                        return -1;
 
1935
        }
 
1936
        return 0;
 
1937
}
 
1938
 
 
1939
static void
 
1940
stringzone(char *result, const struct zone *zpfirst, int zonecount)
 
1941
{
 
1942
        const struct zone *     zp;
 
1943
        struct rule *           rp;
 
1944
        struct rule *           stdrp;
 
1945
        struct rule *           dstrp;
 
1946
        int                     i;
 
1947
        const char *            abbrvar;
 
1948
 
 
1949
        result[0] = '\0';
 
1950
        zp = zpfirst + zonecount - 1;
 
1951
        stdrp = dstrp = NULL;
 
1952
        for (i = 0; i < zp->z_nrules; ++i)
 
1953
        {
 
1954
                rp = &zp->z_rules[i];
 
1955
                if (rp->r_hiwasnum || rp->r_hiyear != INT_MAX)
 
1956
                        continue;
 
1957
                if (rp->r_yrtype != NULL)
 
1958
                        continue;
 
1959
                if (rp->r_stdoff == 0) {
 
1960
                        if (stdrp == NULL)
 
1961
                                stdrp = rp;
 
1962
                        else    return;
 
1963
                }
 
1964
                else
 
1965
                {
 
1966
                        if (dstrp == NULL)
 
1967
                                dstrp = rp;
 
1968
                        else    return;
 
1969
                }
 
1970
        }
 
1971
        if (stdrp == NULL && dstrp == NULL)
 
1972
        {
 
1973
                /*
 
1974
                 * There are no rules running through "max".
 
1975
                 * Let's find the latest rule.
 
1976
                 */
 
1977
                for (i = 0; i < zp->z_nrules; ++i)
 
1978
                {
 
1979
                        rp = &zp->z_rules[i];
 
1980
                        if (stdrp == NULL || rp->r_hiyear > stdrp->r_hiyear ||
 
1981
                                (rp->r_hiyear == stdrp->r_hiyear &&
 
1982
                                rp->r_month > stdrp->r_month))
 
1983
                                        stdrp = rp;
 
1984
                }
 
1985
                if (stdrp != NULL && stdrp->r_stdoff != 0)
 
1986
                        return; /* We end up in DST (a POSIX no-no). */
 
1987
                /*
 
1988
                 * Horrid special case: if year is 2037,
 
1989
                 * presume this is a zone handled on a year-by-year basis;
 
1990
                 * do not try to apply a rule to the zone.
 
1991
                 */
 
1992
                if (stdrp != NULL && stdrp->r_hiyear == 2037)
 
1993
                        return;
 
1994
        }
 
1995
        if (stdrp == NULL && zp->z_nrules != 0)
 
1996
                return;
 
1997
        abbrvar = (stdrp == NULL) ? "" : stdrp->r_abbrvar;
 
1998
        doabbr(result, zp->z_format, abbrvar, FALSE, TRUE);
 
1999
        if (stringoffset(end(result), -zp->z_gmtoff) != 0) {
 
2000
                result[0] = '\0';
 
2001
                return;
 
2002
        }
 
2003
        if (dstrp == NULL)
 
2004
                return;
 
2005
        doabbr(end(result), zp->z_format, dstrp->r_abbrvar, TRUE, TRUE);
 
2006
        if (dstrp->r_stdoff != SECSPERMIN * MINSPERHOUR)
 
2007
                if (stringoffset(end(result),
 
2008
                        -(zp->z_gmtoff + dstrp->r_stdoff)) != 0) {
 
2009
                                result[0] = '\0';
 
2010
                                return;
 
2011
                }
 
2012
        (void) strcat(result, ",");
 
2013
        if (stringrule(result, dstrp, dstrp->r_stdoff, zp->z_gmtoff) != 0) {
 
2014
                result[0] = '\0';
 
2015
                return;
 
2016
        }
 
2017
        (void) strcat(result, ",");
 
2018
        if (stringrule(result, stdrp, dstrp->r_stdoff, zp->z_gmtoff) != 0) {
 
2019
                result[0] = '\0';
 
2020
                return;
 
2021
        }
 
2022
}
 
2023
 
 
2024
static void
 
2025
outzone(const struct zone * zpfirst, int zonecount)
 
2026
{
 
2027
        const struct zone *zp;
 
2028
        struct rule *rp;
 
2029
        int                     i,
 
2030
                                j;
 
2031
        int                     usestart,
 
2032
                                useuntil;
 
2033
        zic_t           starttime = 0;
 
2034
        zic_t           untiltime = 0;
 
2035
        long            gmtoff;
 
2036
        long            stdoff;
 
2037
        int                     year;
 
2038
        long            startoff;
 
2039
        int                     startttisstd;
 
2040
        int                     startttisgmt;
 
2041
        int                     type;
 
2042
        char       *startbuf;
 
2043
        char       *ab;
 
2044
        char       *envvar;
 
2045
        int                     max_abbr_len;
 
2046
        int                     max_envvar_len;
 
2047
 
 
2048
        max_abbr_len = 2 + max_format_len + max_abbrvar_len;
 
2049
        max_envvar_len = 2 * max_abbr_len + 5 * 9;
 
2050
        startbuf = emalloc(max_abbr_len + 1);
 
2051
        ab = emalloc(max_abbr_len + 1);
 
2052
        envvar = emalloc(max_envvar_len + 1);
 
2053
 
 
2054
        /*
 
2055
         * Now. . .finally. . .generate some useful data!
 
2056
         */
 
2057
        timecnt = 0;
 
2058
        typecnt = 0;
 
2059
        charcnt = 0;
 
2060
 
 
2061
        /*
 
2062
         * Thanks to Earl Chew for noting the need to
 
2063
         * unconditionally initialize startttisstd.
 
2064
         */
 
2065
        startttisstd = FALSE;
 
2066
        startttisgmt = FALSE;
 
2067
        min_year = max_year = EPOCH_YEAR;
 
2068
        if (leapseen)
 
2069
        {
 
2070
                updateminmax(leapminyear);
 
2071
                updateminmax(leapmaxyear);
 
2072
        }
 
2073
        for (i = 0; i < zonecount; ++i)
 
2074
        {
 
2075
                zp = &zpfirst[i];
 
2076
                if (i < zonecount - 1)
 
2077
                        updateminmax(zp->z_untilrule.r_loyear);
 
2078
                for (j = 0; j < zp->z_nrules; ++j)
 
2079
                {
 
2080
                        rp = &zp->z_rules[j];
 
2081
                        if (rp->r_lowasnum)
 
2082
                                updateminmax(rp->r_loyear);
 
2083
                        if (rp->r_hiwasnum)
 
2084
                                updateminmax(rp->r_hiyear);
 
2085
                }
 
2086
        }
 
2087
        /*
 
2088
         * Generate lots of data if a rule can't cover all future times.
 
2089
         */
 
2090
        stringzone(envvar, zpfirst, zonecount);
 
2091
        if (noise && envvar[0] == '\0') {
 
2092
                char *  wp;
 
2093
 
 
2094
                wp = ecpyalloc(_("no POSIX environment variable for zone"));
 
2095
                wp = ecatalloc(wp, " ");
 
2096
                wp = ecatalloc(wp, zpfirst->z_name);
 
2097
                warning(wp);
 
2098
                ifree(wp);
 
2099
        }
 
2100
        if (envvar[0] == '\0')
 
2101
        {
 
2102
                if (min_year >= INT_MIN + YEARSPERREPEAT)
 
2103
                        min_year -= YEARSPERREPEAT;
 
2104
                else    min_year = INT_MIN;
 
2105
                if (max_year <= INT_MAX - YEARSPERREPEAT)
 
2106
                        max_year += YEARSPERREPEAT;
 
2107
                else    max_year = INT_MAX;
 
2108
        }
 
2109
        /*
 
2110
         * For the benefit of older systems,
 
2111
         * generate data from 1900 through 2037.
 
2112
         */
 
2113
        if (min_year > 1900)
 
2114
                min_year = 1900;
 
2115
        if (max_year < 2037)
 
2116
                max_year = 2037;
 
2117
        for (i = 0; i < zonecount; ++i)
 
2118
        {
 
2119
                /*
 
2120
                 * A guess that may well be corrected later.
 
2121
                 */
 
2122
                stdoff = 0;
 
2123
                zp = &zpfirst[i];
 
2124
                usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
 
2125
                useuntil = i < (zonecount - 1);
 
2126
                if (useuntil && zp->z_untiltime <= min_time)
 
2127
                        continue;
 
2128
                gmtoff = zp->z_gmtoff;
 
2129
                eat(zp->z_filename, zp->z_linenum);
 
2130
                *startbuf = '\0';
 
2131
                startoff = zp->z_gmtoff;
 
2132
                if (zp->z_nrules == 0)
 
2133
                {
 
2134
                        stdoff = zp->z_stdoff;
 
2135
                        doabbr(startbuf, zp->z_format,
 
2136
                                   (char *) NULL, stdoff != 0, FALSE);
 
2137
                        type = addtype(oadd(zp->z_gmtoff, stdoff),
 
2138
                                                   startbuf, stdoff != 0, startttisstd,
 
2139
                                                   startttisgmt);
 
2140
                        if (usestart)
 
2141
                        {
 
2142
                                addtt(starttime, type);
 
2143
                                usestart = FALSE;
 
2144
                        }
 
2145
                        else if (stdoff != 0)
 
2146
                                addtt(min_time, type);
 
2147
                }
 
2148
                else
 
2149
                        for (year = min_year; year <= max_year; ++year)
 
2150
                        {
 
2151
                                if (useuntil && year > zp->z_untilrule.r_hiyear)
 
2152
                                        break;
 
2153
 
 
2154
                                /*
 
2155
                                 * Mark which rules to do in the current year. For those to
 
2156
                                 * do, calculate rpytime(rp, year);
 
2157
                                 */
 
2158
                                for (j = 0; j < zp->z_nrules; ++j)
 
2159
                                {
 
2160
                                        rp = &zp->z_rules[j];
 
2161
                                        eats(zp->z_filename, zp->z_linenum,
 
2162
                                                 rp->r_filename, rp->r_linenum);
 
2163
                                        rp->r_todo = year >= rp->r_loyear &&
 
2164
                                                year <= rp->r_hiyear &&
 
2165
                                                yearistype(year, rp->r_yrtype);
 
2166
                                        if (rp->r_todo)
 
2167
                                                rp->r_temp = rpytime(rp, year);
 
2168
                                }
 
2169
                                for (;;)
 
2170
                                {
 
2171
                                        int                     k;
 
2172
                                        zic_t           jtime,
 
2173
                                                                ktime = 0;
 
2174
                                        long            offset;
 
2175
 
 
2176
                                        if (useuntil)
 
2177
                                        {
 
2178
                                                /*
 
2179
                                                 * Turn untiltime into UTC assuming the current gmtoff
 
2180
                                                 * and stdoff values.
 
2181
                                                 */
 
2182
                                                untiltime = zp->z_untiltime;
 
2183
                                                if (!zp->z_untilrule.r_todisgmt)
 
2184
                                                        untiltime = tadd(untiltime,
 
2185
                                                                                         -gmtoff);
 
2186
                                                if (!zp->z_untilrule.r_todisstd)
 
2187
                                                        untiltime = tadd(untiltime,
 
2188
                                                                                         -stdoff);
 
2189
                                        }
 
2190
 
 
2191
                                        /*
 
2192
                                         * Find the rule (of those to do, if any) that takes
 
2193
                                         * effect earliest in the year.
 
2194
                                         */
 
2195
                                        k = -1;
 
2196
                                        for (j = 0; j < zp->z_nrules; ++j)
 
2197
                                        {
 
2198
                                                rp = &zp->z_rules[j];
 
2199
                                                if (!rp->r_todo)
 
2200
                                                        continue;
 
2201
                                                eats(zp->z_filename, zp->z_linenum,
 
2202
                                                         rp->r_filename, rp->r_linenum);
 
2203
                                                offset = rp->r_todisgmt ? 0 : gmtoff;
 
2204
                                                if (!rp->r_todisstd)
 
2205
                                                        offset = oadd(offset, stdoff);
 
2206
                                                jtime = rp->r_temp;
 
2207
                                                if (jtime == min_time ||
 
2208
                                                        jtime == max_time)
 
2209
                                                        continue;
 
2210
                                                jtime = tadd(jtime, -offset);
 
2211
                                                if (k < 0 || jtime < ktime)
 
2212
                                                {
 
2213
                                                        k = j;
 
2214
                                                        ktime = jtime;
 
2215
                                                }
 
2216
                                        }
 
2217
                                        if (k < 0)
 
2218
                                                break;  /* go on to next year */
 
2219
                                        rp = &zp->z_rules[k];
 
2220
                                        rp->r_todo = FALSE;
 
2221
                                        if (useuntil && ktime >= untiltime)
 
2222
                                                break;
 
2223
                                        stdoff = rp->r_stdoff;
 
2224
                                        if (usestart && ktime == starttime)
 
2225
                                                usestart = FALSE;
 
2226
                                        if (usestart)
 
2227
                                        {
 
2228
                                                if (ktime < starttime)
 
2229
                                                {
 
2230
                                                        startoff = oadd(zp->z_gmtoff,
 
2231
                                                                                        stdoff);
 
2232
                                                        doabbr(startbuf, zp->z_format,
 
2233
                                                                   rp->r_abbrvar,
 
2234
                                                                   rp->r_stdoff != 0,
 
2235
                                                                   FALSE);
 
2236
                                                        continue;
 
2237
                                                }
 
2238
                                                if (*startbuf == '\0' && 
 
2239
                                                        startoff == oadd(zp->z_gmtoff, stdoff))
 
2240
                                                {
 
2241
                                                        doabbr(startbuf,
 
2242
                                                                   zp->z_format,
 
2243
                                                                   rp->r_abbrvar,
 
2244
                                                                   rp->r_stdoff !=
 
2245
                                                                   0,
 
2246
                                                                   FALSE);
 
2247
                                                }
 
2248
                                        }
 
2249
                                        eats(zp->z_filename, zp->z_linenum,
 
2250
                                                 rp->r_filename, rp->r_linenum);
 
2251
                                        doabbr(ab, zp->z_format, rp->r_abbrvar,
 
2252
                                                   rp->r_stdoff != 0, FALSE);
 
2253
                                        offset = oadd(zp->z_gmtoff, rp->r_stdoff);
 
2254
                                        type = addtype(offset, ab, rp->r_stdoff != 0,
 
2255
                                                                   rp->r_todisstd, rp->r_todisgmt);
 
2256
                                        addtt(ktime, type);
 
2257
                                }
 
2258
                        }
 
2259
                if (usestart)
 
2260
                {
 
2261
                        if (*startbuf == '\0' &&
 
2262
                                zp->z_format != NULL &&
 
2263
                                strchr(zp->z_format, '%') == NULL &&
 
2264
                                strchr(zp->z_format, '/') == NULL)
 
2265
                                (void) strcpy(startbuf, zp->z_format);
 
2266
                        eat(zp->z_filename, zp->z_linenum);
 
2267
                        if (*startbuf == '\0')
 
2268
                                error(_("cannot determine time zone abbreviation to use just after until time"));
 
2269
                        else
 
2270
                                addtt(starttime,
 
2271
                                          addtype(startoff, startbuf,
 
2272
                                                          startoff != zp->z_gmtoff,
 
2273
                                                          startttisstd,
 
2274
                                                          startttisgmt));
 
2275
                }
 
2276
 
 
2277
                /*
 
2278
                 * Now we may get to set starttime for the next zone line.
 
2279
                 */
 
2280
                if (useuntil)
 
2281
                {
 
2282
                        startttisstd = zp->z_untilrule.r_todisstd;
 
2283
                        startttisgmt = zp->z_untilrule.r_todisgmt;
 
2284
                        starttime = zp->z_untiltime;
 
2285
                        if (!startttisstd)
 
2286
                                starttime = tadd(starttime, -stdoff);
 
2287
                        if (!startttisgmt)
 
2288
                                starttime = tadd(starttime, -gmtoff);
 
2289
                }
 
2290
        }
 
2291
        writezone(zpfirst->z_name, envvar);
 
2292
        ifree(startbuf);
 
2293
        ifree(ab);
 
2294
        ifree(envvar);
 
2295
}
 
2296
 
 
2297
static void
 
2298
addtt(const zic_t starttime, int type)
 
2299
{
 
2300
        if (starttime <= min_time ||
 
2301
                (timecnt == 1 && attypes[0].at < min_time))
 
2302
        {
 
2303
                gmtoffs[0] = gmtoffs[type];
 
2304
                isdsts[0] = isdsts[type];
 
2305
                ttisstds[0] = ttisstds[type];
 
2306
                ttisgmts[0] = ttisgmts[type];
 
2307
                if (abbrinds[type] != 0)
 
2308
                        (void) strcpy(chars, &chars[abbrinds[type]]);
 
2309
                abbrinds[0] = 0;
 
2310
                charcnt = strlen(chars) + 1;
 
2311
                typecnt = 1;
 
2312
                timecnt = 0;
 
2313
                type = 0;
 
2314
        }
 
2315
        if (timecnt >= TZ_MAX_TIMES)
 
2316
        {
 
2317
                error(_("too many transitions?!"));
 
2318
                exit(EXIT_FAILURE);
 
2319
        }
 
2320
        attypes[timecnt].at = starttime;
 
2321
        attypes[timecnt].type = type;
 
2322
        ++timecnt;
 
2323
}
 
2324
 
 
2325
static int
 
2326
addtype(long gmtoff, const char *abbr, int isdst,
 
2327
                int ttisstd, int ttisgmt)
 
2328
{
 
2329
        int                     i;
 
2330
        int                     j;
 
2331
 
 
2332
        if (isdst != TRUE && isdst != FALSE)
 
2333
        {
 
2334
                error(_("internal error - addtype called with bad isdst"));
 
2335
                exit(EXIT_FAILURE);
 
2336
        }
 
2337
        if (ttisstd != TRUE && ttisstd != FALSE)
 
2338
        {
 
2339
                error(_("internal error - addtype called with bad ttisstd"));
 
2340
                exit(EXIT_FAILURE);
 
2341
        }
 
2342
        if (ttisgmt != TRUE && ttisgmt != FALSE)
 
2343
        {
 
2344
                error(_("internal error - addtype called with bad ttisgmt"));
 
2345
                exit(EXIT_FAILURE);
 
2346
        }
 
2347
 
 
2348
        /*
 
2349
         * See if there's already an entry for this zone type. If so, just return
 
2350
         * its index.
 
2351
         */
 
2352
        for (i = 0; i < typecnt; ++i)
 
2353
        {
 
2354
                if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
 
2355
                        strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
 
2356
                        ttisstd == ttisstds[i] &&
 
2357
                        ttisgmt == ttisgmts[i])
 
2358
                        return i;
 
2359
        }
 
2360
 
 
2361
        /*
 
2362
         * There isn't one; add a new one, unless there are already too many.
 
2363
         */
 
2364
        if (typecnt >= TZ_MAX_TYPES)
 
2365
        {
 
2366
                error(_("too many local time types"));
 
2367
                exit(EXIT_FAILURE);
 
2368
        }
 
2369
        if (! (-1L - 2147483647L <= gmtoff && gmtoff <= 2147483647L)) {
 
2370
                error(_("UTC offset out of range"));
 
2371
                exit(EXIT_FAILURE);
 
2372
        }
 
2373
        gmtoffs[i] = gmtoff;
 
2374
        isdsts[i] = isdst;
 
2375
        ttisstds[i] = ttisstd;
 
2376
        ttisgmts[i] = ttisgmt;
 
2377
 
 
2378
        for (j = 0; j < charcnt; ++j)
 
2379
                if (strcmp(&chars[j], abbr) == 0)
 
2380
                        break;
 
2381
        if (j == charcnt)
 
2382
                newabbr(abbr);
 
2383
        abbrinds[i] = j;
 
2384
        ++typecnt;
 
2385
        return i;
 
2386
}
 
2387
 
 
2388
static void
 
2389
leapadd(const zic_t t, int positive, int rolling, int count)
 
2390
{
 
2391
        int                     i;
 
2392
        int                     j;
 
2393
 
 
2394
        if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS)
 
2395
        {
 
2396
                error(_("too many leap seconds"));
 
2397
                exit(EXIT_FAILURE);
 
2398
        }
 
2399
        for (i = 0; i < leapcnt; ++i)
 
2400
                if (t <= trans[i])
 
2401
                {
 
2402
                        if (t == trans[i])
 
2403
                        {
 
2404
                                error(_("repeated leap second moment"));
 
2405
                                exit(EXIT_FAILURE);
 
2406
                        }
 
2407
                        break;
 
2408
                }
 
2409
        do
 
2410
        {
 
2411
                for (j = leapcnt; j > i; --j)
 
2412
                {
 
2413
                        trans[j] = trans[j - 1];
 
2414
                        corr[j] = corr[j - 1];
 
2415
                        roll[j] = roll[j - 1];
 
2416
                }
 
2417
                trans[i] = t;
 
2418
                corr[i] = positive ? 1L : eitol(-count);
 
2419
                roll[i] = rolling;
 
2420
                ++leapcnt;
 
2421
        } while (positive && --count != 0);
 
2422
}
 
2423
 
 
2424
static void
 
2425
adjleap(void)
 
2426
{
 
2427
        int                     i;
 
2428
        long            last = 0;
 
2429
 
 
2430
        /*
 
2431
         * propagate leap seconds forward
 
2432
         */
 
2433
        for (i = 0; i < leapcnt; ++i)
 
2434
        {
 
2435
                trans[i] = tadd(trans[i], last);
 
2436
                last = corr[i] += last;
 
2437
        }
 
2438
}
 
2439
 
 
2440
static int
 
2441
yearistype(int year, const char *type)
 
2442
{
 
2443
        static char *buf;
 
2444
        int                     result;
 
2445
 
 
2446
        if (type == NULL || *type == '\0')
 
2447
                return TRUE;
 
2448
        buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
 
2449
        (void) sprintf(buf, "%s %d %s", yitcommand, year, type);
 
2450
        result = system(buf);
 
2451
        if (WIFEXITED(result))
 
2452
                switch (WEXITSTATUS(result))
 
2453
                {
 
2454
                        case 0:
 
2455
                                return TRUE;
 
2456
                        case 1:
 
2457
                                return FALSE;
 
2458
                }
 
2459
        error(_("Wild result from command execution"));
 
2460
        (void) fprintf(stderr, _("%s: command was '%s', result was %d\n"),
 
2461
                                   progname, buf, result);
 
2462
        for (;;)
 
2463
                exit(EXIT_FAILURE);
 
2464
}
 
2465
 
 
2466
static int
 
2467
lowerit(int a)
 
2468
{
 
2469
        a = (unsigned char) a;
 
2470
        return (isascii(a) && isupper(a)) ? tolower(a) : a;
 
2471
}
 
2472
 
 
2473
static int
 
2474
ciequal(const char *ap, const char *bp)
 
2475
{
 
2476
        while (lowerit(*ap) == lowerit(*bp++))
 
2477
                if (*ap++ == '\0')
 
2478
                        return TRUE;
 
2479
        return FALSE;
 
2480
}
 
2481
 
 
2482
static int
 
2483
itsabbr(const char *abbr, const char *word)
 
2484
{
 
2485
        if (lowerit(*abbr) != lowerit(*word))
 
2486
                return FALSE;
 
2487
        ++word;
 
2488
        while (*++abbr != '\0')
 
2489
                do
 
2490
                {
 
2491
                        if (*word == '\0')
 
2492
                                return FALSE;
 
2493
                } while (lowerit(*word++) != lowerit(*abbr));
 
2494
        return TRUE;
 
2495
}
 
2496
 
 
2497
static const struct lookup *
 
2498
byword(const char *word, const struct lookup * table)
 
2499
{
 
2500
        const struct lookup *foundlp;
 
2501
        const struct lookup *lp;
 
2502
 
 
2503
        if (word == NULL || table == NULL)
 
2504
                return NULL;
 
2505
 
 
2506
        /*
 
2507
         * Look for exact match.
 
2508
         */
 
2509
        for (lp = table; lp->l_word != NULL; ++lp)
 
2510
                if (ciequal(word, lp->l_word))
 
2511
                        return lp;
 
2512
 
 
2513
        /*
 
2514
         * Look for inexact match.
 
2515
         */
 
2516
        foundlp = NULL;
 
2517
        for (lp = table; lp->l_word != NULL; ++lp)
 
2518
                if (itsabbr(word, lp->l_word))
 
2519
                {
 
2520
                        if (foundlp == NULL)
 
2521
                                foundlp = lp;
 
2522
                        else
 
2523
                                return NULL;    /* multiple inexact matches */
 
2524
                }
 
2525
        return foundlp;
 
2526
}
 
2527
 
 
2528
static char **
 
2529
getfields(char *cp)
 
2530
{
 
2531
        char       *dp;
 
2532
        char      **array;
 
2533
        int                     nsubs;
 
2534
 
 
2535
        if (cp == NULL)
 
2536
                return NULL;
 
2537
        array = (char **) (void *)
 
2538
                emalloc((int) ((strlen(cp) + 1) * sizeof *array));
 
2539
        nsubs = 0;
 
2540
        for (;;)
 
2541
        {
 
2542
                while (isascii((unsigned char) *cp) &&
 
2543
                           isspace((unsigned char) *cp))
 
2544
                        ++cp;
 
2545
                if (*cp == '\0' || *cp == '#')
 
2546
                        break;
 
2547
                array[nsubs++] = dp = cp;
 
2548
                do
 
2549
                {
 
2550
                        if ((*dp = *cp++) != '"')
 
2551
                                ++dp;
 
2552
                        else
 
2553
                                while ((*dp = *cp++) != '"')
 
2554
                                        if (*dp != '\0')
 
2555
                                                ++dp;
 
2556
                                        else
 
2557
                                        {
 
2558
                                                error(_("Odd number of quotation marks"));
 
2559
                                                exit(1);
 
2560
                                        }
 
2561
                } while (*cp != '\0' && *cp != '#' &&
 
2562
                                 (!isascii(*cp) || !isspace((unsigned char) *cp)));
 
2563
                if (isascii(*cp) && isspace((unsigned char) *cp))
 
2564
                        ++cp;
 
2565
                *dp = '\0';
 
2566
        }
 
2567
        array[nsubs] = NULL;
 
2568
        return array;
 
2569
}
 
2570
 
 
2571
static long
 
2572
oadd(long t1, long t2)
 
2573
{
 
2574
        long            t;
 
2575
 
 
2576
        t = t1 + t2;
 
2577
        if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1))
 
2578
        {
 
2579
                error(_("time overflow"));
 
2580
                exit(EXIT_FAILURE);
 
2581
        }
 
2582
        return t;
 
2583
}
 
2584
 
 
2585
static zic_t
 
2586
tadd(const zic_t t1, long t2)
 
2587
{
 
2588
        zic_t   t;
 
2589
 
 
2590
        if (t1 == max_time && t2 > 0)
 
2591
                return max_time;
 
2592
        if (t1 == min_time && t2 < 0)
 
2593
                return min_time;
 
2594
        t = t1 + t2;
 
2595
        if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1))
 
2596
        {
 
2597
                error(_("time overflow"));
 
2598
                exit(EXIT_FAILURE);
 
2599
        }
 
2600
        return t;
 
2601
}
 
2602
 
 
2603
/*
 
2604
 * Given a rule, and a year, compute the date - in seconds since January 1,
 
2605
 * 1970, 00:00 LOCAL time - in that year that the rule refers to.
 
2606
 */
 
2607
 
 
2608
static zic_t
 
2609
rpytime(const struct rule * rp, int wantedy)
 
2610
{
 
2611
        int                     y,
 
2612
                                m,
 
2613
                                i;
 
2614
        long            dayoff;                 /* with a nod to Margaret O. */
 
2615
        zic_t   t;
 
2616
 
 
2617
        if (wantedy == INT_MIN)
 
2618
                return min_time;
 
2619
        if (wantedy == INT_MAX)
 
2620
                return max_time;
 
2621
        dayoff = 0;
 
2622
        m = TM_JANUARY;
 
2623
        y = EPOCH_YEAR;
 
2624
        while (wantedy != y)
 
2625
        {
 
2626
                if (wantedy > y)
 
2627
                {
 
2628
                        i = len_years[isleap(y)];
 
2629
                        ++y;
 
2630
                }
 
2631
                else
 
2632
                {
 
2633
                        --y;
 
2634
                        i = -len_years[isleap(y)];
 
2635
                }
 
2636
                dayoff = oadd(dayoff, eitol(i));
 
2637
        }
 
2638
        while (m != rp->r_month)
 
2639
        {
 
2640
                i = len_months[isleap(y)][m];
 
2641
                dayoff = oadd(dayoff, eitol(i));
 
2642
                ++m;
 
2643
        }
 
2644
        i = rp->r_dayofmonth;
 
2645
        if (m == TM_FEBRUARY && i == 29 && !isleap(y))
 
2646
        {
 
2647
                if (rp->r_dycode == DC_DOWLEQ)
 
2648
                        --i;
 
2649
                else
 
2650
                {
 
2651
                        error(_("use of 2/29 in non leap-year"));
 
2652
                        exit(EXIT_FAILURE);
 
2653
                }
 
2654
        }
 
2655
        --i;
 
2656
        dayoff = oadd(dayoff, eitol(i));
 
2657
        if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ)
 
2658
        {
 
2659
                long            wday;
 
2660
 
 
2661
#define LDAYSPERWEEK    ((long) DAYSPERWEEK)
 
2662
                wday = eitol(EPOCH_WDAY);
 
2663
 
 
2664
                /*
 
2665
                 * Don't trust mod of negative numbers.
 
2666
                 */
 
2667
                if (dayoff >= 0)
 
2668
                        wday = (wday + dayoff) % LDAYSPERWEEK;
 
2669
                else
 
2670
                {
 
2671
                        wday -= ((-dayoff) % LDAYSPERWEEK);
 
2672
                        if (wday < 0)
 
2673
                                wday += LDAYSPERWEEK;
 
2674
                }
 
2675
                while (wday != eitol(rp->r_wday))
 
2676
                        if (rp->r_dycode == DC_DOWGEQ)
 
2677
                        {
 
2678
                                dayoff = oadd(dayoff, (long) 1);
 
2679
                                if (++wday >= LDAYSPERWEEK)
 
2680
                                        wday = 0;
 
2681
                                ++i;
 
2682
                        }
 
2683
                        else
 
2684
                        {
 
2685
                                dayoff = oadd(dayoff, (long) -1);
 
2686
                                if (--wday < 0)
 
2687
                                        wday = LDAYSPERWEEK - 1;
 
2688
                                --i;
 
2689
                        }
 
2690
                if (i < 0 || i >= len_months[isleap(y)][m])
 
2691
                {
 
2692
                        if (noise)
 
2693
                                warning(_("rule goes past start/end of month--\
 
2694
will not work with pre-2004 versions of zic"));
 
2695
                }
 
2696
        }
 
2697
        if (dayoff < min_time / SECSPERDAY)
 
2698
                return min_time;
 
2699
        if (dayoff > max_time / SECSPERDAY)
 
2700
                return max_time;
 
2701
        t = (zic_t) dayoff *SECSPERDAY;
 
2702
        return tadd(t, rp->r_tod);
 
2703
}
 
2704
 
 
2705
static void
 
2706
newabbr(const char *string)
 
2707
{
 
2708
        int                     i;
 
2709
 
 
2710
        if (strcmp(string, GRANDPARENTED) != 0)
 
2711
        {
 
2712
                const char *   cp;
 
2713
                char *         wp;
 
2714
 
 
2715
                /*
 
2716
                 * Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics
 
2717
                 * optionally followed by a + or - and a number from 1 to 14.
 
2718
                 */
 
2719
                cp = string;
 
2720
                wp = NULL;
 
2721
                while (isascii((unsigned char) *cp) &&
 
2722
                           isalpha((unsigned char) *cp))
 
2723
                        ++cp;
 
2724
                if (cp - string == 0)
 
2725
                        wp = _("time zone abbreviation lacks alphabetic at start");
 
2726
                if (noise && cp - string > 3)
 
2727
                        wp = _("time zone abbreviation has more than 3 alphabetics");
 
2728
                if (cp - string > ZIC_MAX_ABBR_LEN_WO_WARN)
 
2729
                        wp = _("time zone abbreviation has too many alphabetics");
 
2730
                if (wp == NULL && (*cp == '+' || *cp == '-')) {
 
2731
                        ++cp;
 
2732
                        if (isascii((unsigned char) *cp) &&
 
2733
                                isdigit((unsigned char) *cp))
 
2734
                                if (*cp++ == '1' &&
 
2735
                                        *cp >= '0' && *cp <= '4')
 
2736
                                        ++cp;
 
2737
                }
 
2738
                if (*cp != '\0')
 
2739
                        wp = _("time zone abbreviation differs from POSIX standard");
 
2740
                if (wp != NULL) {
 
2741
                        wp = ecpyalloc(wp);
 
2742
                        wp = ecatalloc(wp, " (");
 
2743
                        wp = ecatalloc(wp, string);
 
2744
                        wp = ecatalloc(wp, ")");
 
2745
                        warning(wp);
 
2746
                        ifree(wp);
 
2747
                }
 
2748
        }
 
2749
        i = strlen(string) + 1;
 
2750
        if (charcnt + i > TZ_MAX_CHARS)
 
2751
        {
 
2752
                error(_("too many, or too long, time zone abbreviations"));
 
2753
                exit(EXIT_FAILURE);
 
2754
        }
 
2755
        (void) strcpy(&chars[charcnt], string);
 
2756
        charcnt += eitol(i);
 
2757
}
 
2758
 
 
2759
static int
 
2760
mkdirs(char *argname)
 
2761
{
 
2762
        char       *name;
 
2763
        char       *cp;
 
2764
 
 
2765
        if (argname == NULL || *argname == '\0')
 
2766
                return 0;
 
2767
        cp = name = ecpyalloc(argname);
 
2768
        while ((cp = strchr(cp + 1, '/')) != 0)
 
2769
        {
 
2770
                *cp = '\0';
 
2771
#ifdef WIN32
 
2772
 
 
2773
                /*
 
2774
                 * DOS drive specifier?
 
2775
                 */
 
2776
                if (isalpha((unsigned char) name[0]) &&
 
2777
                        name[1] == ':' && name[2] == '\0')
 
2778
                {
 
2779
                        *cp = '/';
 
2780
                        continue;
 
2781
                }
 
2782
#endif   /* WIN32 */
 
2783
                if (!itsdir(name))
 
2784
                {
 
2785
                        /*
 
2786
                         * It doesn't seem to exist, so we try to create it. Creation may
 
2787
                         * fail because of the directory being created by some other
 
2788
                         * multiprocessor, so we get to do extra checking.
 
2789
                         */
 
2790
                        if (mkdir(name, MKDIR_UMASK) != 0)
 
2791
                        {
 
2792
                                const char *e = strerror(errno);
 
2793
 
 
2794
                                if (errno != EEXIST || !itsdir(name))
 
2795
                                {
 
2796
                                        (void) fprintf(stderr,
 
2797
                                                                   _("%s: Cannot create directory %s: %s\n"),
 
2798
                                                                   progname, name, e);
 
2799
                                        ifree(name);
 
2800
                                        return -1;
 
2801
                                }
 
2802
                        }
 
2803
                }
 
2804
                *cp = '/';
 
2805
        }
 
2806
        ifree(name);
 
2807
        return 0;
 
2808
}
 
2809
 
 
2810
static long
 
2811
eitol(int i)
 
2812
{
 
2813
        long            l;
 
2814
 
 
2815
        l = i;
 
2816
        if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0))
 
2817
        {
 
2818
                (void) fprintf(stderr,
 
2819
                                           _("%s: %d did not sign extend correctly\n"),
 
2820
                                           progname, i);
 
2821
                exit(EXIT_FAILURE);
 
2822
        }
 
2823
        return l;
 
2824
}
 
2825
 
 
2826
/*
 
2827
 * UNIX was a registered trademark of The Open Group in 2003.
 
2828
 */
 
2829
 
 
2830
 
 
2831
#ifdef WIN32
 
2832
/*
 
2833
 * To run on win32
 
2834
 */
 
2835
int
 
2836
link(const char *oldpath, const char *newpath)
 
2837
{
 
2838
        if (!CopyFile(oldpath, newpath, FALSE))
 
2839
                return -1;
 
2840
        return 0;
 
2841
}
 
2842
#endif
 
2843
 
 
2844
/*
 
2845
 *      This allows zic to compile by just returning a dummy value.
 
2846
 *      localtime.c references it, but no one uses it from zic.
 
2847
 */
 
2848
int
 
2849
pg_open_tzfile(const char *name, char *canonname)
 
2850
{
 
2851
        return -1;
 
2852
}