home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / emacs-18.59-src.tgz / emacs-18.59-src.tar / fsf / emacs18 / amiga / unix / src / zoneinfo / zic.c < prev   
C/C++ Source or Header  |  1996-09-28  |  44KB  |  1,904 lines

  1. /*-
  2.  * Copyright (c) 1991 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Arthur David Olson of the National Cancer Institute.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #ifndef lint
  38. static char sccsid[] = "@(#)zic.c    5.3 (Berkeley) 4/20/91";
  39. #endif /* not lint */
  40.  
  41. #ifdef notdef
  42. static char    elsieid[] = "@(#)zic.c    4.12";
  43. #endif
  44.  
  45. #include <sys/types.h>
  46. #include <sys/cdefs.h>
  47. #include <sys/stat.h>
  48. #include <time.h>
  49. #include <tzfile.h>
  50. #include <stdio.h>
  51. #include <ctype.h>
  52. #include <string.h>
  53. #include <stdlib.h>
  54.  
  55. #ifndef TRUE
  56. #define TRUE    1
  57. #define FALSE    0
  58. #endif /* !defined TRUE */
  59.  
  60. #ifndef EXIT_FAILURE
  61. #define EXIT_FAILURE 1
  62. #endif
  63.  
  64. #ifndef EXIT_SUCCESS
  65. #define EXIT_SUCCESS 0
  66. #endif
  67.  
  68. struct rule {
  69.     const char *    r_filename;
  70.     int        r_linenum;
  71.     const char *    r_name;
  72.  
  73.     int        r_loyear;    /* for example, 1986 */
  74.     int        r_hiyear;    /* for example, 1986 */
  75.     const char *    r_yrtype;
  76.  
  77.     int        r_month;    /* 0..11 */
  78.  
  79.     int        r_dycode;    /* see below */
  80.     int        r_dayofmonth;
  81.     int        r_wday;
  82.  
  83.     long        r_tod;        /* time from midnight */
  84.     int        r_todisstd;    /* above is standard time if TRUE */
  85.                     /* or wall clock time if FALSE */
  86.     long        r_stdoff;    /* offset from standard time */
  87.     const char *    r_abbrvar;    /* variable part of abbreviation */
  88.  
  89.     int        r_todo;        /* a rule to do (used in outzone) */
  90.     time_t        r_temp;        /* used in outzone */
  91. };
  92.  
  93. /*
  94. **    r_dycode        r_dayofmonth    r_wday
  95. */
  96.  
  97. #define DC_DOM        0    /* 1..31 */    /* unused */
  98. #define DC_DOWGEQ    1    /* 1..31 */    /* 0..6 (Sun..Sat) */
  99. #define DC_DOWLEQ    2    /* 1..31 */    /* 0..6 (Sun..Sat) */
  100.  
  101. struct zone {
  102.     const char *    z_filename;
  103.     int        z_linenum;
  104.  
  105.     const char *    z_name;
  106.     long        z_gmtoff;
  107.     const char *    z_rule;
  108.     const char *    z_format;
  109.  
  110.     long        z_stdoff;
  111.  
  112.     struct rule *    z_rules;
  113.     int        z_nrules;
  114.  
  115.     struct rule    z_untilrule;
  116.     time_t        z_untiltime;
  117. };
  118.  
  119. extern char *    icatalloc __P((char * old, const char * new));
  120. extern char *    icpyalloc __P((const char * string));
  121. extern void    ifree __P((char * p));
  122. extern char *    imalloc __P((int n));
  123. extern char *    irealloc __P((char * old, int n));
  124. extern int    link __P((const char * fromname, const char * toname));
  125. extern char *    optarg;
  126. extern int    optind;
  127. static void    addtt __P((time_t starttime, int type));
  128. static int    addtype
  129.             __P((long gmtoff, const char * abbr, int isdst,
  130.             int ttisstd));
  131. static void    addleap __P((time_t t, int positive, int rolling));
  132. static void    adjleap __P((void));
  133. static void    associate __P((void));
  134. static int    ciequal __P((const char * ap, const char * bp));
  135. static void    convert __P((long val, char * buf));
  136. static void    dolink __P((const char * fromfile, const char * tofile));
  137. static void    eat __P((const char * name, int num));
  138. static void    eats __P((const char * name, int num,
  139.             const char * rname, int rnum));
  140. static long    eitol __P((int i));
  141. static void    error __P((const char * message));
  142. static char **    getfields __P((char * buf));
  143. static long    gethms __P((char * string, const char * errstrng,
  144.             int signable));
  145. static void    infile __P((const char * filename));
  146. static void    inleap __P((char ** fields, int nfields));
  147. static void    inlink __P((char ** fields, int nfields));
  148. static void    inrule __P((char ** fields, int nfields));
  149. static int    inzcont __P((char ** fields, int nfields));
  150. static int    inzone __P((char ** fields, int nfields));
  151. static int    inzsub __P((char ** fields, int nfields, int iscont));
  152. static int    itsabbr __P((const char * abbr, const char * word));
  153. static int    itsdir __P((const char * name));
  154. static int    lowerit __P((int c));
  155. static char *    memcheck __P((char * tocheck));
  156. static int    mkdirs __P((char * filename));
  157. static void    newabbr __P((const char * abbr));
  158. static long    oadd __P((long t1, long t2));
  159. static void    outzone __P((const struct zone * zp, int ntzones));
  160. static void    puttzcode __P((long code, FILE * fp));
  161. static int    rcomp __P((const void *leftp, const void *rightp));
  162. static time_t    rpytime __P((const struct rule * rp, int wantedy));
  163. static void    rulesub __P((struct rule * rp, char * loyearp, char * hiyearp,
  164.         char * typep, char * monthp, char * dayp, char * timep));
  165. static void    setboundaries __P((void));
  166. static time_t    tadd __P((time_t t1, long t2));
  167. static void    usage __P((void));
  168. static void    writezone __P((const char * name));
  169. static int    yearistype __P((int year, const char * type));
  170.  
  171. static int        charcnt;
  172. static int        errors;
  173. static const char *    filename;
  174. static int        leapcnt;
  175. static int        linenum;
  176. static time_t        max_time;
  177. static int        max_year;
  178. static time_t        min_time;
  179. static int        min_year;
  180. static int        noise;
  181. static const char *    rfilename;
  182. static int        rlinenum;
  183. static const char *    progname;
  184. static int        timecnt;
  185. static int        typecnt;
  186. static int        tt_signed;
  187.  
  188. /*
  189. ** Line codes.
  190. */
  191.  
  192. #define LC_RULE        0
  193. #define LC_ZONE        1
  194. #define LC_LINK        2
  195. #define LC_LEAP        3
  196.  
  197. /*
  198. ** Which fields are which on a Zone line.
  199. */
  200.  
  201. #define ZF_NAME        1
  202. #define ZF_GMTOFF    2
  203. #define ZF_RULE        3
  204. #define ZF_FORMAT    4
  205. #define ZF_TILYEAR    5
  206. #define ZF_TILMONTH    6
  207. #define ZF_TILDAY    7
  208. #define ZF_TILTIME    8
  209. #define ZONE_MINFIELDS    5
  210. #define ZONE_MAXFIELDS    9
  211.  
  212. /*
  213. ** Which fields are which on a Zone continuation line.
  214. */
  215.  
  216. #define ZFC_GMTOFF    0
  217. #define ZFC_RULE    1
  218. #define ZFC_FORMAT    2
  219. #define ZFC_TILYEAR    3
  220. #define ZFC_TILMONTH    4
  221. #define ZFC_TILDAY    5
  222. #define ZFC_TILTIME    6
  223. #define ZONEC_MINFIELDS    3
  224. #define ZONEC_MAXFIELDS    7
  225.  
  226. /*
  227. ** Which files are which on a Rule line.
  228. */
  229.  
  230. #define RF_NAME        1
  231. #define RF_LOYEAR    2
  232. #define RF_HIYEAR    3
  233. #define RF_COMMAND    4
  234. #define RF_MONTH    5
  235. #define RF_DAY        6
  236. #define RF_TOD        7
  237. #define RF_STDOFF    8
  238. #define RF_ABBRVAR    9
  239. #define RULE_FIELDS    10
  240.  
  241. /*
  242. ** Which fields are which on a Link line.
  243. */
  244.  
  245. #define LF_FROM        1
  246. #define LF_TO        2
  247. #define LINK_FIELDS    3
  248.  
  249. /*
  250. ** Which fields are which on a Leap line.
  251. */
  252.  
  253. #define LP_YEAR        1
  254. #define LP_MONTH    2
  255. #define LP_DAY        3
  256. #define LP_TIME        4
  257. #define LP_CORR        5
  258. #define LP_ROLL        6
  259. #define LEAP_FIELDS    7
  260.  
  261. /*
  262. ** Year synonyms.
  263. */
  264.  
  265. #define YR_MINIMUM    0
  266. #define YR_MAXIMUM    1
  267. #define YR_ONLY        2
  268.  
  269. static struct rule *    rules;
  270. static int        nrules;    /* number of rules */
  271.  
  272. static struct zone *    zones;
  273. static int        nzones;    /* number of zones */
  274.  
  275. struct link {
  276.     const char *    l_filename;
  277.     int        l_linenum;
  278.     const char *    l_from;
  279.     const char *    l_to;
  280. };
  281.  
  282. static struct link *    links;
  283. static int        nlinks;
  284.  
  285. struct lookup {
  286.     const char *    l_word;
  287.     const int    l_value;
  288. };
  289.  
  290. static struct lookup const *    byword __P((const char * string,
  291.                     const struct lookup * lp));
  292.  
  293. static struct lookup const    line_codes[] = {
  294.     "Rule",        LC_RULE,
  295.     "Zone",        LC_ZONE,
  296.     "Link",        LC_LINK,
  297.     "Leap",        LC_LEAP,
  298.     NULL,        0
  299. };
  300.  
  301. static struct lookup const    mon_names[] = {
  302.     "January",    TM_JANUARY,
  303.     "February",    TM_FEBRUARY,
  304.     "March",    TM_MARCH,
  305.     "April",    TM_APRIL,
  306.     "May",        TM_MAY,
  307.     "June",        TM_JUNE,
  308.     "July",        TM_JULY,
  309.     "August",    TM_AUGUST,
  310.     "September",    TM_SEPTEMBER,
  311.     "October",    TM_OCTOBER,
  312.     "November",    TM_NOVEMBER,
  313.     "December",    TM_DECEMBER,
  314.     NULL,        0
  315. };
  316.  
  317. static struct lookup const    wday_names[] = {
  318.     "Sunday",    TM_SUNDAY,
  319.     "Monday",    TM_MONDAY,
  320.     "Tuesday",    TM_TUESDAY,
  321.     "Wednesday",    TM_WEDNESDAY,
  322.     "Thursday",    TM_THURSDAY,
  323.     "Friday",    TM_FRIDAY,
  324.     "Saturday",    TM_SATURDAY,
  325.     NULL,        0
  326. };
  327.  
  328. static struct lookup const    lasts[] = {
  329.     "last-Sunday",        TM_SUNDAY,
  330.     "last-Monday",        TM_MONDAY,
  331.     "last-Tuesday",        TM_TUESDAY,
  332.     "last-Wednesday",    TM_WEDNESDAY,
  333.     "last-Thursday",    TM_THURSDAY,
  334.     "last-Friday",        TM_FRIDAY,
  335.     "last-Saturday",    TM_SATURDAY,
  336.     NULL,            0
  337. };
  338.  
  339. static struct lookup const    begin_years[] = {
  340.     "minimum",        YR_MINIMUM,
  341.     "maximum",        YR_MAXIMUM,
  342.     NULL,            0
  343. };
  344.  
  345. static struct lookup const    end_years[] = {
  346.     "minimum",        YR_MINIMUM,
  347.     "maximum",        YR_MAXIMUM,
  348.     "only",            YR_ONLY,
  349.     NULL,            0
  350. };
  351.  
  352. static struct lookup const    leap_types[] = {
  353.     "Rolling",        TRUE,
  354.     "Stationary",        FALSE,
  355.     NULL,            0
  356. };
  357.  
  358. static const int    len_months[2][MONSPERYEAR] = {
  359.     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
  360.     31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  361. };
  362.  
  363. static const int    len_years[2] = {
  364.     DAYSPERNYEAR, DAYSPERLYEAR
  365. };
  366.  
  367. static time_t        ats[TZ_MAX_TIMES];
  368. static unsigned char    types[TZ_MAX_TIMES];
  369. static long        gmtoffs[TZ_MAX_TYPES];
  370. static char        isdsts[TZ_MAX_TYPES];
  371. static char        abbrinds[TZ_MAX_TYPES];
  372. static char        ttisstds[TZ_MAX_TYPES];
  373. static char        chars[TZ_MAX_CHARS];
  374. static time_t        trans[TZ_MAX_LEAPS];
  375. static long        corr[TZ_MAX_LEAPS];
  376. static char        roll[TZ_MAX_LEAPS];
  377.  
  378. /*
  379. ** Memory allocation.
  380. */
  381.  
  382. static char *
  383. memcheck(ptr)
  384. char * const    ptr;
  385. {
  386.     if (ptr == NULL) {
  387.         (void) perror(progname);
  388.         (void) exit(EXIT_FAILURE);
  389.     }
  390.     return ptr;
  391. }
  392.  
  393. #define emalloc(size)        memcheck(imalloc(size))
  394. #define erealloc(ptr, size)    memcheck(irealloc(ptr, size))
  395. #define ecpyalloc(ptr)        memcheck(icpyalloc(ptr))
  396. #define ecatalloc(oldp, newp)    memcheck(icatalloc(oldp, newp))
  397.  
  398. /*
  399. ** Error handling.
  400. */
  401.  
  402. static void
  403. eats(name, num, rname, rnum)
  404. const char * const    name;
  405. const int        num;
  406. const char * const    rname;
  407. const int        rnum;
  408. {
  409.     filename = name;
  410.     linenum = num;
  411.     rfilename = rname;
  412.     rlinenum = rnum;
  413. }
  414.  
  415. static void
  416. eat(name, num)
  417. const char * const    name;
  418. const int        num;
  419. {
  420.     eats(name, num, (char *) NULL, -1);
  421. }
  422.  
  423. static void
  424. error(string)
  425. const char * const    string;
  426. {
  427.     /*
  428.     ** Match the format of "cc" to allow sh users to
  429.     **     zic ... 2>&1 | error -t "*" -v
  430.     ** on BSD systems.
  431.     */
  432.     (void) fprintf(stderr, "\"%s\", line %d: %s",
  433.         filename, linenum, string);
  434.     if (rfilename != NULL)
  435.         (void) fprintf(stderr, " (rule from \"%s\", line %d)",
  436.             rfilename, rlinenum);
  437.     (void) fprintf(stderr, "\n");
  438.     ++errors;
  439. }
  440.  
  441. static void
  442. usage()
  443. {
  444.     (void) fprintf(stderr,
  445. "%s: usage is %s [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] [ -d directory ]\n\
  446. \t[ -L leapseconds ] [ filename ... ]\n",
  447.         progname, progname);
  448.     (void) exit(EXIT_FAILURE);
  449. }
  450.  
  451. static const char *    psxrules = NULL;
  452. static const char *    lcltime = NULL;
  453. static const char *    directory = NULL;
  454. static const char *    leapsec = NULL;
  455. static int        sflag = FALSE;
  456.  
  457. int
  458. main(argc, argv)
  459. int    argc;
  460. char *    argv[];
  461. {
  462.     register int    i, j;
  463.     register int    c;
  464.  
  465.     (void) umask(umask(022) | 022);
  466.     progname = argv[0];
  467.     while ((c = getopt(argc, argv, "d:l:p:L:vs")) != EOF)
  468.         switch (c) {
  469.             default:
  470.                 usage();
  471.             case 'd':
  472.                 if (directory == NULL)
  473.                     directory = optarg;
  474.                 else {
  475.                     (void) fprintf(stderr,
  476. "%s: More than one -d option specified\n",
  477.                         progname);
  478.                     (void) exit(EXIT_FAILURE);
  479.                 }
  480.                 break;
  481.             case 'l':
  482.                 if (lcltime == NULL)
  483.                     lcltime = optarg;
  484.                 else {
  485.                     (void) fprintf(stderr,
  486. "%s: More than one -l option specified\n",
  487.                         progname);
  488.                     (void) exit(EXIT_FAILURE);
  489.                 }
  490.                 break;
  491.             case 'p':
  492.                 if (psxrules == NULL)
  493.                     psxrules = optarg;
  494.                 else {
  495.                     (void) fprintf(stderr,
  496. "%s: More than one -p option specified\n",
  497.                         progname);
  498.                     (void) exit(EXIT_FAILURE);
  499.                 }
  500.                 break;
  501.             case 'L':
  502.                 if (leapsec == NULL)
  503.                     leapsec = optarg;
  504.                 else {
  505.                     (void) fprintf(stderr,
  506. "%s: More than one -L option specified\n",
  507.                         progname);
  508.                     (void) exit(EXIT_FAILURE);
  509.                 }
  510.                 break;
  511.             case 'v':
  512.                 noise = TRUE;
  513.                 break;
  514.             case 's':
  515.                 sflag = TRUE;
  516.                 break;
  517.         }
  518.     if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
  519.         usage();    /* usage message by request */
  520.     if (directory == NULL)
  521.         directory = TZDIR;
  522.  
  523.     setboundaries();
  524.  
  525.     if (optind < argc && leapsec != NULL) {
  526.         infile(leapsec);
  527.         adjleap();
  528.     }
  529.  
  530.     zones = (struct zone *) emalloc(0);
  531.     rules = (struct rule *) emalloc(0);
  532.     links = (struct link *) emalloc(0);
  533.     for (i = optind; i < argc; ++i)
  534.         infile(argv[i]);
  535.     if (errors)
  536.         (void) exit(EXIT_FAILURE);
  537.     associate();
  538.     for (i = 0; i < nzones; i = j) {
  539.         /*
  540.         ** Find the next non-continuation zone entry.
  541.         */
  542.         for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
  543.             ;
  544.         outzone(&zones[i], j - i);
  545.     }
  546.     /*
  547.     ** Make links.
  548.     */
  549.     for (i = 0; i < nlinks; ++i)
  550.         dolink(links[i].l_from, links[i].l_to);
  551.     if (lcltime != NULL)
  552.         dolink(lcltime, TZDEFAULT);
  553.     if (psxrules != NULL)
  554.         dolink(psxrules, TZDEFRULES);
  555.     return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  556. }
  557.  
  558. static void
  559. dolink(fromfile, tofile)
  560. const char * const    fromfile;
  561. const char * const    tofile;
  562. {
  563.     register char *    fromname;
  564.     register char *    toname;
  565.  
  566.     fromname = ecpyalloc(directory);
  567.     fromname = ecatalloc(fromname, "/");
  568.     fromname = ecatalloc(fromname, fromfile);
  569.     if (strchr(tofile, ':')) toname = ecpyalloc(tofile);
  570.     else
  571.       {
  572.         toname = ecpyalloc(directory);
  573.         toname = ecatalloc(toname, "/");
  574.         toname = ecatalloc(toname, tofile);
  575.       }
  576.     /*
  577.     ** We get to be careful here since
  578.     ** there's a fair chance of root running us.
  579.     */
  580.     if (!itsdir(toname))
  581.         (void) remove(toname);
  582.     if (link(fromname, toname) != 0) {
  583.         (void) fprintf(stderr, "%s: Can't link from %s to ",
  584.             progname, fromname);
  585.         (void) perror(toname);
  586.         (void) exit(EXIT_FAILURE);
  587.     }
  588.     ifree(fromname);
  589.     ifree(toname);
  590. }
  591.  
  592. static void
  593. setboundaries()
  594. {
  595.     register time_t    bit;
  596.  
  597.     for (bit = 1; bit > 0; bit <<= 1)
  598.         ;
  599.     if (bit == 0) {        /* time_t is an unsigned type */
  600.         tt_signed = FALSE;
  601.         min_time = 0;
  602.         max_time = ~(time_t) 0;
  603.         if (sflag)
  604.             max_time >>= 1;
  605.     } else {
  606.         tt_signed = TRUE;
  607.         min_time = bit;
  608.         max_time = bit;
  609.         ++max_time;
  610.         max_time = -max_time;
  611.         if (sflag)
  612.             min_time = 0;
  613.     }
  614.     min_year = TM_YEAR_BASE + gmtime(&min_time)->tm_year;
  615.     max_year = TM_YEAR_BASE + gmtime(&max_time)->tm_year;
  616. }
  617.  
  618. static int
  619. itsdir(name)
  620. const char * const    name;
  621. {
  622.     struct stat    s;
  623.  
  624.     return (stat(name, &s) == 0 && S_ISDIR(s.st_mode));
  625. }
  626.  
  627. /*
  628. ** Associate sets of rules with zones.
  629. */
  630.  
  631. /*
  632. ** Sort by rule name.
  633. */
  634.  
  635. static int
  636. rcomp(cp1, cp2)
  637. const void *    cp1;
  638. const void *    cp2;
  639. {
  640.     return strcmp(((struct rule *) cp1)->r_name,
  641.         ((struct rule *) cp2)->r_name);
  642. }
  643.  
  644. static void
  645. associate()
  646. {
  647.     register struct zone *    zp;
  648.     register struct rule *    rp;
  649.     register int        base, out;
  650.     register int        i;
  651.  
  652.     if (nrules != 0)
  653.         (void) qsort((void *) rules, (size_t) nrules,
  654.              (size_t) sizeof *rules, rcomp);
  655.     for (i = 0; i < nzones; ++i) {
  656.         zp = &zones[i];
  657.         zp->z_rules = NULL;
  658.         zp->z_nrules = 0;
  659.     }
  660.     for (base = 0; base < nrules; base = out) {
  661.         rp = &rules[base];
  662.         for (out = base + 1; out < nrules; ++out)
  663.             if (strcmp(rp->r_name, rules[out].r_name) != 0)
  664.                 break;
  665.         for (i = 0; i < nzones; ++i) {
  666.             zp = &zones[i];
  667.             if (strcmp(zp->z_rule, rp->r_name) != 0)
  668.                 continue;
  669.             zp->z_rules = rp;
  670.             zp->z_nrules = out - base;
  671.         }
  672.     }
  673.     for (i = 0; i < nzones; ++i) {
  674.         zp = &zones[i];
  675.         if (zp->z_nrules == 0) {
  676.             /*
  677.             ** Maybe we have a local standard time offset.
  678.             */
  679.             eat(zp->z_filename, zp->z_linenum);
  680.             zp->z_stdoff =
  681.                 gethms((char *)zp->z_rule, "unruly zone", TRUE);
  682.             /*
  683.             ** Note, though, that if there's no rule,
  684.             ** a '%s' in the format is a bad thing.
  685.             */
  686.             if (strchr(zp->z_format, '%') != 0)
  687.                 error("%s in ruleless zone");
  688.         }
  689.     }
  690.     if (errors)
  691.         (void) exit(EXIT_FAILURE);
  692. }
  693.  
  694. static void
  695. infile(name)
  696. const char *    name;
  697. {
  698.     register FILE *            fp;
  699.     register char **        fields;
  700.     register char *            cp;
  701.     register const struct lookup *    lp;
  702.     register int            nfields;
  703.     register int            wantcont;
  704.     register int            num;
  705.     char                buf[BUFSIZ];
  706.  
  707.     if (strcmp(name, "-") == 0) {
  708.         name = "standard input";
  709.         fp = stdin;
  710.     } else if ((fp = fopen(name, "r")) == NULL) {
  711.         (void) fprintf(stderr, "%s: Can't open ", progname);
  712.         (void) perror(name);
  713.         (void) exit(EXIT_FAILURE);
  714.     }
  715.     wantcont = FALSE;
  716.     for (num = 1; ; ++num) {
  717.         eat(name, num);
  718.         if (fgets(buf, (int) sizeof buf, fp) != buf)
  719.             break;
  720.         cp = strchr(buf, '\n');
  721.         if (cp == NULL) {
  722.             error("line too long");
  723.             (void) exit(EXIT_FAILURE);
  724.         }
  725.         *cp = '\0';
  726.         fields = getfields(buf);
  727.         nfields = 0;
  728.         while (fields[nfields] != NULL) {
  729.             if (ciequal(fields[nfields], "-"))
  730.                 fields[nfields] = "";
  731.             ++nfields;
  732.         }
  733.         if (nfields == 0) {
  734.             /* nothing to do */
  735.         } else if (wantcont) {
  736.             wantcont = inzcont(fields, nfields);
  737.         } else {
  738.             lp = byword(fields[0], line_codes);
  739.             if (lp == NULL)
  740.                 error("input line of unknown type");
  741.             else switch ((int) (lp->l_value)) {
  742.                 case LC_RULE:
  743.                     inrule(fields, nfields);
  744.                     wantcont = FALSE;
  745.                     break;
  746.                 case LC_ZONE:
  747.                     wantcont = inzone(fields, nfields);
  748.                     break;
  749.                 case LC_LINK:
  750.                     inlink(fields, nfields);
  751.                     wantcont = FALSE;
  752.                     break;
  753.                 case LC_LEAP:
  754.                     if (name != leapsec)
  755.                         (void) fprintf(stderr,
  756. "%s: Leap line in non leap seconds file %s\n",
  757.                             progname, name);
  758.                     else    inleap(fields, nfields);
  759.                     wantcont = FALSE;
  760.                     break;
  761.                 default:    /* "cannot happen" */
  762.                     (void) fprintf(stderr,
  763. "%s: panic: Invalid l_value %d\n",
  764.                         progname, lp->l_value);
  765.                     (void) exit(EXIT_FAILURE);
  766.             }
  767.         }
  768.         ifree((char *) fields);
  769.     }
  770.     if (ferror(fp)) {
  771.         (void) fprintf(stderr, "%s: Error reading ", progname);
  772.         (void) perror(filename);
  773.         (void) exit(EXIT_FAILURE);
  774.     }
  775.     if (fp != stdin && fclose(fp)) {
  776.         (void) fprintf(stderr, "%s: Error closing ", progname);
  777.         (void) perror(filename);
  778.         (void) exit(EXIT_FAILURE);
  779.     }
  780.     if (wantcont)
  781.         error("expected continuation line not found");
  782. }
  783.  
  784. /*
  785. ** Convert a string of one of the forms
  786. **    h    -h     hh:mm    -hh:mm    hh:mm:ss    -hh:mm:ss
  787. ** into a number of seconds.
  788. ** A null string maps to zero.
  789. ** Call error with errstring and return zero on errors.
  790. */
  791.  
  792. static long
  793. gethms(string, errstring, signable)
  794. char *        string;
  795. const char * const    errstring;
  796. const int        signable;
  797. {
  798.     int    hh, mm, ss, sign;
  799.  
  800.     if (string == NULL || *string == '\0')
  801.         return 0;
  802.     if (!signable)
  803.         sign = 1;
  804.     else if (*string == '-') {
  805.         sign = -1;
  806.         ++string;
  807.     } else    sign = 1;
  808.     if (sscanf(string, "%d:%d:%d", &hh, &mm, &ss) == 3) ;
  809.     else if (sscanf(string, "%d:%d", &hh, &mm) == 2) ss = 0;
  810.     else if (sscanf(string, "%d", &hh) == 1) mm = ss = 0;
  811.     else
  812.     {
  813.         error(errstring);
  814.         return 0;
  815.     }
  816.     if (hh < 0 || hh >= HOURSPERDAY ||
  817.         mm < 0 || mm >= MINSPERHOUR ||
  818.         ss < 0 || ss > SECSPERMIN) {
  819.             error(errstring);
  820.             return 0;
  821.     }
  822.     return eitol(sign) *
  823.         (eitol(hh * MINSPERHOUR + mm) *
  824.         eitol(SECSPERMIN) + eitol(ss));
  825. }
  826.  
  827. static void
  828. inrule(fields, nfields)
  829. register char ** const    fields;
  830. const int        nfields;
  831. {
  832.     static struct rule    r;
  833.  
  834.     if (nfields != RULE_FIELDS) {
  835.         error("wrong number of fields on Rule line");
  836.         return;
  837.     }
  838.     if (*fields[RF_NAME] == '\0') {
  839.         error("nameless rule");
  840.         return;
  841.     }
  842.     r.r_filename = filename;
  843.     r.r_linenum = linenum;
  844.     r.r_stdoff = gethms(fields[RF_STDOFF], "invalid saved time", TRUE);
  845.     rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
  846.         fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
  847.     r.r_name = ecpyalloc(fields[RF_NAME]);
  848.     r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
  849.     rules = (struct rule *) erealloc((char *) rules,
  850.         (int) ((nrules + 1) * sizeof *rules));
  851.     rules[nrules++] = r;
  852. }
  853.  
  854. static int
  855. inzone(fields, nfields)
  856. register char ** const    fields;
  857. const int        nfields;
  858. {
  859.     register int    i;
  860.     char        buf[132];
  861.  
  862.     if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
  863.         error("wrong number of fields on Zone line");
  864.         return FALSE;
  865.     }
  866.     if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
  867.         (void) sprintf(buf,
  868.             "\"Zone %s\" line and -l option are mutually exclusive",
  869.             TZDEFAULT);
  870.         error(buf);
  871.         return FALSE;
  872.     }
  873.     if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
  874.         (void) sprintf(buf,
  875.             "\"Zone %s\" line and -p option are mutually exclusive",
  876.             TZDEFRULES);
  877.         error(buf);
  878.         return FALSE;
  879.     }
  880.     for (i = 0; i < nzones; ++i)
  881.         if (zones[i].z_name != NULL &&
  882.             strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
  883.                 (void) sprintf(buf,
  884. "duplicate zone name %s (file \"%s\", line %d)",
  885.                     fields[ZF_NAME],
  886.                     zones[i].z_filename,
  887.                     zones[i].z_linenum);
  888.                 error(buf);
  889.                 return FALSE;
  890.         }
  891.     return inzsub(fields, nfields, FALSE);
  892. }
  893.  
  894. static int
  895. inzcont(fields, nfields)
  896. register char ** const    fields;
  897. const int        nfields;
  898. {
  899.     if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
  900.         error("wrong number of fields on Zone continuation line");
  901.         return FALSE;
  902.     }
  903.     return inzsub(fields, nfields, TRUE);
  904. }
  905.  
  906. static int
  907. inzsub(fields, nfields, iscont)
  908. register char ** const    fields;
  909. const int        nfields;
  910. const int        iscont;
  911. {
  912.     register char *        cp;
  913.     static struct zone    z;
  914.     register int        i_gmtoff, i_rule, i_format;
  915.     register int        i_untilyear, i_untilmonth;
  916.     register int        i_untilday, i_untiltime;
  917.     register int        hasuntil;
  918.  
  919.     if (iscont) {
  920.         i_gmtoff = ZFC_GMTOFF;
  921.         i_rule = ZFC_RULE;
  922.         i_format = ZFC_FORMAT;
  923.         i_untilyear = ZFC_TILYEAR;
  924.         i_untilmonth = ZFC_TILMONTH;
  925.         i_untilday = ZFC_TILDAY;
  926.         i_untiltime = ZFC_TILTIME;
  927.         z.z_name = NULL;
  928.     } else {
  929.         i_gmtoff = ZF_GMTOFF;
  930.         i_rule = ZF_RULE;
  931.         i_format = ZF_FORMAT;
  932.         i_untilyear = ZF_TILYEAR;
  933.         i_untilmonth = ZF_TILMONTH;
  934.         i_untilday = ZF_TILDAY;
  935.         i_untiltime = ZF_TILTIME;
  936.         z.z_name = ecpyalloc(fields[ZF_NAME]);
  937.     }
  938.     z.z_filename = filename;
  939.     z.z_linenum = linenum;
  940.     z.z_gmtoff = gethms(fields[i_gmtoff], "invalid GMT offset", TRUE);
  941.     if ((cp = strchr(fields[i_format], '%')) != 0) {
  942.         if (*++cp != 's' || strchr(cp, '%') != 0) {
  943.             error("invalid abbreviation format");
  944.             return FALSE;
  945.         }
  946.     }
  947.     z.z_rule = ecpyalloc(fields[i_rule]);
  948.     z.z_format = ecpyalloc(fields[i_format]);
  949.     hasuntil = nfields > i_untilyear;
  950.     if (hasuntil) {
  951.         z.z_untilrule.r_filename = filename;
  952.         z.z_untilrule.r_linenum = linenum;
  953.         rulesub(&z.z_untilrule,
  954.             fields[i_untilyear],
  955.             "only",
  956.             "",
  957.             (nfields > i_untilmonth) ? fields[i_untilmonth] : "Jan",
  958.             (nfields > i_untilday) ? fields[i_untilday] : "1",
  959.             (nfields > i_untiltime) ? fields[i_untiltime] : "0");
  960.         z.z_untiltime = rpytime(&z.z_untilrule, z.z_untilrule.r_loyear);
  961.         if (iscont && nzones > 0 && z.z_untiltime < max_time &&
  962.             z.z_untiltime > min_time &&
  963.             zones[nzones - 1].z_untiltime >= z.z_untiltime) {
  964. error("Zone continuation line end time is not after end time of previous line");
  965.             return FALSE;
  966.         }
  967.     }
  968.     zones = (struct zone *) erealloc((char *) zones,
  969.         (int) ((nzones + 1) * sizeof *zones));
  970.     zones[nzones++] = z;
  971.     /*
  972.     ** If there was an UNTIL field on this line,
  973.     ** there's more information about the zone on the next line.
  974.     */
  975.     return hasuntil;
  976. }
  977.  
  978. static void
  979. inleap(fields, nfields)
  980. register char ** const    fields;
  981. const int        nfields;
  982. {
  983.     register const char *        cp;
  984.     register const struct lookup *    lp;
  985.     register int            i, j;
  986.     int                year, month, day;
  987.     long                dayoff, tod;
  988.     time_t                t;
  989.  
  990.     if (nfields != LEAP_FIELDS) {
  991.         error("wrong number of fields on Leap line");
  992.         return;
  993.     }
  994.     dayoff = 0;
  995.     cp = fields[LP_YEAR];
  996.     if (sscanf((char *)cp, "%d", &year) != 1 ||
  997.         year < min_year || year > max_year) {
  998.             /*
  999.              * Leapin' Lizards!
  1000.              */
  1001.             error("invalid leaping year");
  1002.             return;
  1003.     }
  1004.     j = EPOCH_YEAR;
  1005.     while (j != year) {
  1006.         if (year > j) {
  1007.             i = len_years[isleap(j)];
  1008.             ++j;
  1009.         } else {
  1010.             --j;
  1011.             i = -len_years[isleap(j)];
  1012.         }
  1013.         dayoff = oadd(dayoff, eitol(i));
  1014.     }
  1015.     if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
  1016.         error("invalid month name");
  1017.         return;
  1018.     }
  1019.     month = lp->l_value;
  1020.     j = TM_JANUARY;
  1021.     while (j != month) {
  1022.         i = len_months[isleap(year)][j];
  1023.         dayoff = oadd(dayoff, eitol(i));
  1024.         ++j;
  1025.     }
  1026.     cp = fields[LP_DAY];
  1027.     if (sscanf((char *)cp, "%d", &day) != 1 ||
  1028.         day <= 0 || day > len_months[isleap(year)][month]) {
  1029.             error("invalid day of month");
  1030.             return;
  1031.     }
  1032.     dayoff = oadd(dayoff, eitol(day - 1));
  1033.     if (dayoff < 0 && !tt_signed) {
  1034.         error("time before zero");
  1035.         return;
  1036.     }
  1037.     t = (time_t) dayoff * SECSPERDAY;
  1038.     /*
  1039.     ** Cheap overflow check.
  1040.     */
  1041.     if (t / SECSPERDAY != dayoff) {
  1042.         error("time overflow");
  1043.         return;
  1044.     }
  1045.     tod = gethms(fields[LP_TIME], "invalid time of day", FALSE);
  1046.     cp = fields[LP_CORR];
  1047.     if (strcmp(cp, "+") != 0 && strcmp(cp, "") != 0) {
  1048.         /* infile() turned "-" into "" */
  1049.         error("illegal CORRECTION field on Leap line");
  1050.         return;
  1051.     }
  1052.     if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
  1053.         error("illegal Rolling/Stationary field on Leap line");
  1054.         return;
  1055.     }
  1056.     addleap(tadd(t, tod), *cp == '+', lp->l_value);
  1057. }
  1058.  
  1059. static void
  1060. inlink(fields, nfields)
  1061. register char ** const    fields;
  1062. const int        nfields;
  1063. {
  1064.     struct link    l;
  1065.  
  1066.     if (nfields != LINK_FIELDS) {
  1067.         error("wrong number of fields on Link line");
  1068.         return;
  1069.     }
  1070.     if (*fields[LF_FROM] == '\0') {
  1071.         error("blank FROM field on Link line");
  1072.         return;
  1073.     }
  1074.     if (*fields[LF_TO] == '\0') {
  1075.         error("blank TO field on Link line");
  1076.         return;
  1077.     }
  1078.     l.l_filename = filename;
  1079.     l.l_linenum = linenum;
  1080.     l.l_from = ecpyalloc(fields[LF_FROM]);
  1081.     l.l_to = ecpyalloc(fields[LF_TO]);
  1082.     links = (struct link *) erealloc((char *) links,
  1083.         (int) ((nlinks + 1) * sizeof *links));
  1084.     links[nlinks++] = l;
  1085. }
  1086.  
  1087. static void
  1088. rulesub(rp, loyearp, hiyearp, typep, monthp, dayp, timep)
  1089. register struct rule * const    rp;
  1090. char * const            loyearp;
  1091. char * const            hiyearp;
  1092. char * const            typep;
  1093. char * const            monthp;
  1094. char * const            dayp;
  1095. char * const            timep;
  1096. {
  1097.     register struct lookup const *    lp;
  1098.     register char *            cp;
  1099.  
  1100.     if ((lp = byword(monthp, mon_names)) == NULL) {
  1101.         error("invalid month name");
  1102.         return;
  1103.     }
  1104.     rp->r_month = lp->l_value;
  1105.     rp->r_todisstd = FALSE;
  1106.     cp = timep;
  1107.     if (*cp != '\0') {
  1108.         cp += strlen(cp) - 1;
  1109.         switch (lowerit(*cp)) {
  1110.             case 's':
  1111.                 rp->r_todisstd = TRUE;
  1112.                 *cp = '\0';
  1113.                 break;
  1114.             case 'w':
  1115.                 rp->r_todisstd = FALSE;
  1116.                 *cp = '\0';
  1117.                 break;
  1118.         }
  1119.     }
  1120.     rp->r_tod = gethms(timep, "invalid time of day", FALSE);
  1121.     /*
  1122.     ** Year work.
  1123.     */
  1124.     cp = loyearp;
  1125.     if ((lp = byword(cp, begin_years)) != NULL) switch ((int) lp->l_value) {
  1126.         case YR_MINIMUM:
  1127.             rp->r_loyear = min_year;
  1128.             break;
  1129.         case YR_MAXIMUM:
  1130.             rp->r_loyear = max_year;
  1131.             break;
  1132.         default:    /* "cannot happen" */
  1133.             (void) fprintf(stderr,
  1134.                 "%s: panic: Invalid l_value %d\n",
  1135.                 progname, lp->l_value);
  1136.             (void) exit(EXIT_FAILURE);
  1137.     } else if (sscanf(cp, "%d", &rp->r_loyear) != 1 ||
  1138.         rp->r_loyear < min_year || rp->r_loyear > max_year) {
  1139.             if (noise)
  1140.                 error("invalid starting year");
  1141.             if (rp->r_loyear > max_year)
  1142.                 return;
  1143.     }
  1144.     cp = hiyearp;
  1145.     if ((lp = byword(cp, end_years)) != NULL) switch ((int) lp->l_value) {
  1146.         case YR_MINIMUM:
  1147.             rp->r_hiyear = min_year;
  1148.             break;
  1149.         case YR_MAXIMUM:
  1150.             rp->r_hiyear = max_year;
  1151.             break;
  1152.         case YR_ONLY:
  1153.             rp->r_hiyear = rp->r_loyear;
  1154.             break;
  1155.         default:    /* "cannot happen" */
  1156.             (void) fprintf(stderr,
  1157.                 "%s: panic: Invalid l_value %d\n",
  1158.                 progname, lp->l_value);
  1159.             (void) exit(EXIT_FAILURE);
  1160.     } else if (sscanf(cp, "%d", &rp->r_hiyear) != 1 ||
  1161.         rp->r_hiyear < min_year || rp->r_hiyear > max_year) {
  1162.             if (noise)
  1163.                 error("invalid ending year");
  1164.             if (rp->r_hiyear < min_year)
  1165.                 return;
  1166.     }
  1167.     if (rp->r_hiyear < min_year)
  1168.          return;
  1169.      if (rp->r_loyear < min_year)
  1170.          rp->r_loyear = min_year;
  1171.      if (rp->r_hiyear > max_year)
  1172.          rp->r_hiyear = max_year;
  1173.     if (rp->r_loyear > rp->r_hiyear) {
  1174.         error("starting year greater than ending year");
  1175.         return;
  1176.     }
  1177.     if (*typep == '\0')
  1178.         rp->r_yrtype = NULL;
  1179.     else {
  1180.         if (rp->r_loyear == rp->r_hiyear) {
  1181.             error("typed single year");
  1182.             return;
  1183.         }
  1184.         rp->r_yrtype = ecpyalloc(typep);
  1185.     }
  1186.     /*
  1187.     ** Day work.
  1188.     ** Accept things such as:
  1189.     **    1
  1190.     **    last-Sunday
  1191.     **    Sun<=20
  1192.     **    Sun>=7
  1193.     */
  1194.     if ((lp = byword(dayp, lasts)) != NULL) {
  1195.         rp->r_dycode = DC_DOWLEQ;
  1196.         rp->r_wday = lp->l_value;
  1197.         rp->r_dayofmonth = len_months[1][rp->r_month];
  1198.     } else {
  1199.         if ((cp = strchr(dayp, '<')) != 0)
  1200.             rp->r_dycode = DC_DOWLEQ;
  1201.         else if ((cp = strchr(dayp, '>')) != 0)
  1202.             rp->r_dycode = DC_DOWGEQ;
  1203.         else {
  1204.             cp = dayp;
  1205.             rp->r_dycode = DC_DOM;
  1206.         }
  1207.         if (rp->r_dycode != DC_DOM) {
  1208.             *cp++ = 0;
  1209.             if (*cp++ != '=') {
  1210.                 error("invalid day of month");
  1211.                 return;
  1212.             }
  1213.             if ((lp = byword(dayp, wday_names)) == NULL) {
  1214.                 error("invalid weekday name");
  1215.                 return;
  1216.             }
  1217.             rp->r_wday = lp->l_value;
  1218.         }
  1219.         if (sscanf(cp, "%d", &rp->r_dayofmonth) != 1 ||
  1220.             rp->r_dayofmonth <= 0 ||
  1221.             (rp->r_dayofmonth > len_months[1][rp->r_month])) {
  1222.                 error("invalid day of month");
  1223.                 return;
  1224.         }
  1225.     }
  1226. }
  1227.  
  1228. static void
  1229. convert(val, buf)
  1230. const long    val;
  1231. char * const    buf;
  1232. {
  1233.     register int    i;
  1234.     register long    shift;
  1235.  
  1236.     for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
  1237.         buf[i] = val >> shift;
  1238. }
  1239.  
  1240. static void
  1241. puttzcode(val, fp)
  1242. const long    val;
  1243. FILE * const    fp;
  1244. {
  1245.     char    buf[4];
  1246.  
  1247.     convert(val, buf);
  1248.     (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
  1249. }
  1250.  
  1251. static void
  1252. writezone(name)
  1253. const char * const    name;
  1254. {
  1255.     register FILE *        fp;
  1256.     register int        i, j;
  1257.     char            fullname[BUFSIZ];
  1258.     static struct tzhead    tzh;
  1259.  
  1260.     if (strlen(directory) + 1 + strlen(name) >= sizeof fullname) {
  1261.         (void) fprintf(stderr,
  1262.             "%s: File name %s/%s too long\n", progname,
  1263.             directory, name);
  1264.         (void) exit(EXIT_FAILURE);
  1265.     }
  1266.     (void) sprintf(fullname, "%s/%s", directory, name);
  1267.     if ((fp = fopen(fullname, "wb")) == NULL) {
  1268.         if (mkdirs(fullname) != 0)
  1269.             (void) exit(EXIT_FAILURE);
  1270.         if ((fp = fopen(fullname, "wb")) == NULL) {
  1271.             (void) fprintf(stderr, "%s: Can't create ", progname);
  1272.             (void) perror(fullname);
  1273.             (void) exit(EXIT_FAILURE);
  1274.         }
  1275.     }
  1276.     convert(eitol(typecnt), tzh.tzh_ttisstdcnt);
  1277.     convert(eitol(leapcnt), tzh.tzh_leapcnt);
  1278.     convert(eitol(timecnt), tzh.tzh_timecnt);
  1279.     convert(eitol(typecnt), tzh.tzh_typecnt);
  1280.     convert(eitol(charcnt), tzh.tzh_charcnt);
  1281.     (void) fwrite((void *) &tzh, (size_t) sizeof tzh, (size_t) 1, fp);
  1282.     for (i = 0; i < timecnt; ++i) {
  1283.         j = leapcnt;
  1284.         while (--j >= 0)
  1285.             if (ats[i] >= trans[j]) {
  1286.                 ats[i] = tadd(ats[i], corr[j]);
  1287.                 break;
  1288.             }
  1289.         puttzcode((long) ats[i], fp);
  1290.     }
  1291.     if (timecnt > 0)
  1292.         (void) fwrite((void *) types, (size_t) sizeof types[0],
  1293.             (size_t) timecnt, fp);
  1294.     for (i = 0; i < typecnt; ++i) {
  1295.         puttzcode((long) gmtoffs[i], fp);
  1296.         (void) putc(isdsts[i], fp);
  1297.         (void) putc(abbrinds[i], fp);
  1298.     }
  1299.     if (charcnt != 0)
  1300.         (void) fwrite((void *) chars, (size_t) sizeof chars[0],
  1301.             (size_t) charcnt, fp);
  1302.     for (i = 0; i < leapcnt; ++i) {
  1303.         if (roll[i]) {
  1304.             if (timecnt == 0 || trans[i] < ats[0]) {
  1305.                 j = 0;
  1306.                 while (isdsts[j])
  1307.                     if (++j >= typecnt) {
  1308.                         j = 0;
  1309.                         break;
  1310.                     }
  1311.             } else {
  1312.                 j = 1;
  1313.                 while (j < timecnt && trans[i] >= ats[j])
  1314.                     ++j;
  1315.                 j = types[j - 1];
  1316.             }
  1317.             puttzcode((long) tadd(trans[i], -gmtoffs[j]), fp);
  1318.         } else    puttzcode((long) trans[i], fp);
  1319.         puttzcode((long) corr[i], fp);
  1320.     }
  1321.     for (i = 0; i < typecnt; ++i)
  1322.         (void) putc(ttisstds[i], fp);
  1323.     if (ferror(fp) || fclose(fp)) {
  1324.         (void) fprintf(stderr, "%s: Write error on ", progname);
  1325.         (void) perror(fullname);
  1326.         (void) exit(EXIT_FAILURE);
  1327.     }
  1328. }
  1329.  
  1330. static void
  1331. outzone(zpfirst, zonecount)
  1332. const struct zone * const    zpfirst;
  1333. const int            zonecount;
  1334. {
  1335.     register const struct zone *    zp;
  1336.     register struct rule *        rp;
  1337.     register int            i, j;
  1338.     register int            usestart, useuntil;
  1339.     register time_t            starttime, untiltime;
  1340.     register long            gmtoff;
  1341.     register long            stdoff;
  1342.     register int            year;
  1343.     register long            startoff;
  1344.     register int            startisdst;
  1345.     register int            startttisstd;
  1346.     register int            type;
  1347.     char                startbuf[BUFSIZ];
  1348.  
  1349.     /*
  1350.     ** Now. . .finally. . .generate some useful data!
  1351.     */
  1352.     timecnt = 0;
  1353.     typecnt = 0;
  1354.     charcnt = 0;
  1355.     /*
  1356.     ** Two guesses. . .the second may well be corrected later.
  1357.     */
  1358.     gmtoff = zpfirst->z_gmtoff;
  1359.     stdoff = 0;
  1360. #ifdef lint
  1361.     starttime = 0;
  1362.     startttisstd = FALSE;
  1363. #endif /* defined lint */
  1364.     for (i = 0; i < zonecount; ++i) {
  1365.         usestart = i > 0;
  1366.         useuntil = i < (zonecount - 1);
  1367.         zp = &zpfirst[i];
  1368.         eat(zp->z_filename, zp->z_linenum);
  1369.         startisdst = -1;
  1370.         if (zp->z_nrules == 0) {
  1371.             type = addtype(oadd(zp->z_gmtoff, zp->z_stdoff),
  1372.                 zp->z_format, zp->z_stdoff != 0,
  1373.                 startttisstd);
  1374.             if (usestart)
  1375.                 addtt(starttime, type);
  1376.             gmtoff = zp->z_gmtoff;
  1377.             stdoff = zp->z_stdoff;
  1378.         } else for (year = min_year; year <= max_year; ++year) {
  1379.             if (useuntil && year > zp->z_untilrule.r_hiyear)
  1380.                 break;
  1381.             /*
  1382.             ** Mark which rules to do in the current year.
  1383.             ** For those to do, calculate rpytime(rp, year);
  1384.             */
  1385.             for (j = 0; j < zp->z_nrules; ++j) {
  1386.                 rp = &zp->z_rules[j];
  1387.                 eats(zp->z_filename, zp->z_linenum,
  1388.                     rp->r_filename, rp->r_linenum);
  1389.                 rp->r_todo = year >= rp->r_loyear &&
  1390.                         year <= rp->r_hiyear &&
  1391.                         yearistype(year, rp->r_yrtype);
  1392.                 if (rp->r_todo)
  1393.                     rp->r_temp = rpytime(rp, year);
  1394.             }
  1395.             for ( ; ; ) {
  1396.                 register int    k;
  1397.                 register time_t    jtime, ktime;
  1398.                 register long    offset;
  1399.                 char        buf[BUFSIZ];
  1400.  
  1401.                 if (useuntil) {
  1402.                     /*
  1403.                     ** Turn untiltime into GMT
  1404.                     ** assuming the current gmtoff and
  1405.                     ** stdoff values.
  1406.                     */
  1407.                     offset = gmtoff;
  1408.                     if (!zp->z_untilrule.r_todisstd)
  1409.                         offset = oadd(offset, stdoff);
  1410.                     untiltime = tadd(zp->z_untiltime,
  1411.                         -offset);
  1412.                 }
  1413.                 /*
  1414.                 ** Find the rule (of those to do, if any)
  1415.                 ** that takes effect earliest in the year.
  1416.                 */
  1417.                 k = -1;
  1418. #ifdef lint
  1419.                 ktime = 0;
  1420. #endif /* defined lint */
  1421.                 for (j = 0; j < zp->z_nrules; ++j) {
  1422.                     rp = &zp->z_rules[j];
  1423.                     if (!rp->r_todo)
  1424.                         continue;
  1425.                     eats(zp->z_filename, zp->z_linenum,
  1426.                         rp->r_filename, rp->r_linenum);
  1427.                     offset = gmtoff;
  1428.                     if (!rp->r_todisstd)
  1429.                         offset = oadd(offset, stdoff);
  1430.                     jtime = rp->r_temp;
  1431.                     if (jtime == min_time ||
  1432.                         jtime == max_time)
  1433.                             continue;
  1434.                     jtime = tadd(jtime, -offset);
  1435.                     if (k < 0 || jtime < ktime) {
  1436.                         k = j;
  1437.                         ktime = jtime;
  1438.                     }
  1439.                 }
  1440.                 if (k < 0)
  1441.                     break;    /* go on to next year */
  1442.                 rp = &zp->z_rules[k];
  1443.                 rp->r_todo = FALSE;
  1444.                 if (useuntil && ktime >= untiltime)
  1445.                     break;
  1446.                 if (usestart) {
  1447.                     if (ktime < starttime) {
  1448.                         stdoff = rp->r_stdoff;
  1449.                         startoff = oadd(zp->z_gmtoff,
  1450.                             rp->r_stdoff);
  1451.                         (void) sprintf(startbuf,
  1452.                             zp->z_format,
  1453.                             rp->r_abbrvar);
  1454.                         startisdst =
  1455.                             rp->r_stdoff != 0;
  1456.                         continue;
  1457.                     }
  1458.                     if (ktime != starttime &&
  1459.                         startisdst >= 0)
  1460. addtt(starttime, addtype(startoff, startbuf, startisdst, startttisstd));
  1461.                     usestart = FALSE;
  1462.                 }
  1463.                 eats(zp->z_filename, zp->z_linenum,
  1464.                     rp->r_filename, rp->r_linenum);
  1465.                 (void) sprintf(buf, zp->z_format,
  1466.                     rp->r_abbrvar);
  1467.                 offset = oadd(zp->z_gmtoff, rp->r_stdoff);
  1468.                 type = addtype(offset, buf, rp->r_stdoff != 0,
  1469.                     rp->r_todisstd);
  1470.                 if (timecnt != 0 || rp->r_stdoff != 0)
  1471.                     addtt(ktime, type);
  1472.                 gmtoff = zp->z_gmtoff;
  1473.                 stdoff = rp->r_stdoff;
  1474.             }
  1475.         }
  1476.         /*
  1477.         ** Now we may get to set starttime for the next zone line.
  1478.         */
  1479.         if (useuntil) {
  1480.             starttime = tadd(zp->z_untiltime,
  1481.                 -gmtoffs[types[timecnt - 1]]);
  1482.             startttisstd = zp->z_untilrule.r_todisstd;
  1483.         }
  1484.     }
  1485.     writezone(zpfirst->z_name);
  1486. }
  1487.  
  1488. static void
  1489. addtt(starttime, type)
  1490. const time_t    starttime;
  1491. const int    type;
  1492. {
  1493.     if (timecnt != 0 && type == types[timecnt - 1])
  1494.         return;    /* easy enough! */
  1495.     if (timecnt >= TZ_MAX_TIMES) {
  1496.         error("too many transitions?!");
  1497.         (void) exit(EXIT_FAILURE);
  1498.     }
  1499.     ats[timecnt] = starttime;
  1500.     types[timecnt] = type;
  1501.     ++timecnt;
  1502. }
  1503.  
  1504. static int
  1505. addtype(gmtoff, abbr, isdst, ttisstd)
  1506. const long        gmtoff;
  1507. const char * const    abbr;
  1508. const int        isdst;
  1509. const int        ttisstd;
  1510. {
  1511.     register int    i, j;
  1512.  
  1513.     /*
  1514.     ** See if there's already an entry for this zone type.
  1515.     ** If so, just return its index.
  1516.     */
  1517.     for (i = 0; i < typecnt; ++i) {
  1518.         if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
  1519.             strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
  1520.             ttisstd == ttisstds[i])
  1521.                 return i;
  1522.     }
  1523.     /*
  1524.     ** There isn't one; add a new one, unless there are already too
  1525.     ** many.
  1526.     */
  1527.     if (typecnt >= TZ_MAX_TYPES) {
  1528.         error("too many local time types");
  1529.         (void) exit(EXIT_FAILURE);
  1530.     }
  1531.     gmtoffs[i] = gmtoff;
  1532.     isdsts[i] = isdst;
  1533.     ttisstds[i] = ttisstd;
  1534.  
  1535.     for (j = 0; j < charcnt; ++j)
  1536.         if (strcmp(&chars[j], abbr) == 0)
  1537.             break;
  1538.     if (j == charcnt)
  1539.         newabbr(abbr);
  1540.     abbrinds[i] = j;
  1541.     ++typecnt;
  1542.     return i;
  1543. }
  1544.  
  1545. static void
  1546. addleap(t, positive, rolling)
  1547. const time_t    t;
  1548. const int    positive;
  1549. const int    rolling;
  1550. {
  1551.     register int    i, j;
  1552.  
  1553.     if (leapcnt >= TZ_MAX_LEAPS) {
  1554.         error("too many leap seconds");
  1555.         (void) exit(EXIT_FAILURE);
  1556.     }
  1557.     for (i = 0; i < leapcnt; ++i)
  1558.         if (t <= trans[i]) {
  1559.             if (t == trans[i]) {
  1560.                 error("repeated leap second moment");
  1561.                 (void) exit(EXIT_FAILURE);
  1562.             }
  1563.             break;
  1564.         }
  1565.     for (j = leapcnt; j > i; --j) {
  1566.         trans[j] = trans[j-1];
  1567.         corr[j] = corr[j-1];
  1568.         roll[j] = roll[j-1];
  1569.     }
  1570.     trans[i] = t;
  1571.     corr[i] = (positive ? 1L : -1L);
  1572.     roll[i] = rolling;
  1573.     ++leapcnt;
  1574. }
  1575.  
  1576. static void
  1577. adjleap()
  1578. {
  1579.     register int    i;
  1580.     register long    last = 0;
  1581.  
  1582.     /*
  1583.     ** propagate leap seconds forward
  1584.     */
  1585.     for (i = 0; i < leapcnt; ++i) {
  1586.         trans[i] = tadd(trans[i], last);
  1587.         last = corr[i] += last;
  1588.     }
  1589. }
  1590.  
  1591. static int
  1592. yearistype(year, type)
  1593. const int        year;
  1594. const char * const    type;
  1595. {
  1596.     char    buf[BUFSIZ];
  1597.     int    result;
  1598.  
  1599.     if (type == NULL || *type == '\0')
  1600.         return TRUE;
  1601.     if (strcmp(type, "uspres") == 0)
  1602.         return (year % 4) == 0;
  1603.     if (strcmp(type, "nonpres") == 0)
  1604.         return (year % 4) != 0;
  1605.     (void) sprintf(buf, "yearistype %d %s", year, type);
  1606.     result = system(buf);
  1607.     if (result == 0)
  1608.         return TRUE;
  1609.     if (result == (1 << 8))
  1610.         return FALSE;
  1611.     error("Wild result from command execution");
  1612.     (void) fprintf(stderr, "%s: command was '%s', result was %d\n",
  1613.         progname, buf, result);
  1614.     for ( ; ; )
  1615.         (void) exit(EXIT_FAILURE);
  1616. }
  1617.  
  1618. static int
  1619. lowerit(a)
  1620. const int    a;
  1621. {
  1622.     return (isascii(a) && isupper(a)) ? tolower(a) : a;
  1623. }
  1624.  
  1625. static int
  1626. ciequal(ap, bp)        /* case-insensitive equality */
  1627. register const char *    ap;
  1628. register const char *    bp;
  1629. {
  1630.     while (lowerit(*ap) == lowerit(*bp++))
  1631.         if (*ap++ == '\0')
  1632.             return TRUE;
  1633.     return FALSE;
  1634. }
  1635.  
  1636. static int
  1637. itsabbr(abbr, word)
  1638. register const char *    abbr;
  1639. register const char *    word;
  1640. {
  1641.     if (lowerit(*abbr) != lowerit(*word))
  1642.         return FALSE;
  1643.     ++word;
  1644.     while (*++abbr != '\0')
  1645.         do if (*word == '\0')
  1646.             return FALSE;
  1647.                 while (lowerit(*word++) != lowerit(*abbr));
  1648.     return TRUE;
  1649. }
  1650.  
  1651. static const struct lookup *
  1652. byword(word, table)
  1653. register const char * const        word;
  1654. register const struct lookup * const    table;
  1655. {
  1656.     register const struct lookup *    foundlp;
  1657.     register const struct lookup *    lp;
  1658.  
  1659.     if (word == NULL || table == NULL)
  1660.         return NULL;
  1661.     /*
  1662.     ** Look for exact match.
  1663.     */
  1664.     for (lp = table; lp->l_word != NULL; ++lp)
  1665.         if (ciequal(word, lp->l_word))
  1666.             return lp;
  1667.     /*
  1668.     ** Look for inexact match.
  1669.     */
  1670.     foundlp = NULL;
  1671.     for (lp = table; lp->l_word != NULL; ++lp)
  1672.         if (itsabbr(word, lp->l_word))
  1673.             if (foundlp == NULL)
  1674.                 foundlp = lp;
  1675.             else    return NULL;    /* multiple inexact matches */
  1676.     return foundlp;
  1677. }
  1678.  
  1679. static char **
  1680. getfields(cp)
  1681. register char *    cp;
  1682. {
  1683.     register char *        dp;
  1684.     register char **    array;
  1685.     register int        nsubs;
  1686.  
  1687.     if (cp == NULL)
  1688.         return NULL;
  1689.     array = (char **) emalloc((int) ((strlen(cp) + 1) * sizeof *array));
  1690.     nsubs = 0;
  1691.     for ( ; ; ) {
  1692.         while (isascii(*cp) && isspace(*cp))
  1693.             ++cp;
  1694.         if (*cp == '\0' || *cp == '#')
  1695.             break;
  1696.         array[nsubs++] = dp = cp;
  1697.         do {
  1698.             if ((*dp = *cp++) != '"')
  1699.                 ++dp;
  1700.             else while ((*dp = *cp++) != '"')
  1701.                 if (*dp != '\0')
  1702.                     ++dp;
  1703.                 else    error("Odd number of quotation marks");
  1704.         } while (*cp != '\0' && *cp != '#' &&
  1705.             (!isascii(*cp) || !isspace(*cp)));
  1706.         if (isascii(*cp) && isspace(*cp))
  1707.             ++cp;
  1708.         *dp = '\0';
  1709.     }
  1710.     array[nsubs] = NULL;
  1711.     return array;
  1712. }
  1713.  
  1714. static long
  1715. oadd(t1, t2)
  1716. const long    t1;
  1717. const long    t2;
  1718. {
  1719.     register long    t;
  1720.  
  1721.     t = t1 + t2;
  1722.     if (t2 > 0 && t <= t1 || t2 < 0 && t >= t1) {
  1723.         error("time overflow");
  1724.         (void) exit(EXIT_FAILURE);
  1725.     }
  1726.     return t;
  1727. }
  1728.  
  1729. static time_t
  1730. tadd(t1, t2)
  1731. const time_t    t1;
  1732. const long    t2;
  1733. {
  1734.     register time_t    t;
  1735.  
  1736.     if (t1 == max_time && t2 > 0)
  1737.         return max_time;
  1738.     if (t1 == min_time && t2 < 0)
  1739.         return min_time;
  1740.     t = t1 + t2;
  1741.     if (t2 > 0 && t <= t1 || t2 < 0 && t >= t1) {
  1742.         error("time overflow");
  1743.         (void) exit(EXIT_FAILURE);
  1744.     }
  1745.     return t;
  1746. }
  1747.  
  1748. /*
  1749. ** Given a rule, and a year, compute the date - in seconds since January 1,
  1750. ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
  1751. */
  1752.  
  1753. static time_t
  1754. rpytime(rp, wantedy)
  1755. register const struct rule * const    rp;
  1756. register const int            wantedy;
  1757. {
  1758.     register int    y, m, i;
  1759.     register long    dayoff;            /* with a nod to Margaret O. */
  1760.     register time_t    t;
  1761.  
  1762.     dayoff = 0;
  1763.     m = TM_JANUARY;
  1764.     y = EPOCH_YEAR;
  1765.     while (wantedy != y) {
  1766.         if (wantedy > y) {
  1767.             i = len_years[isleap(y)];
  1768.             ++y;
  1769.         } else {
  1770.             --y;
  1771.             i = -len_years[isleap(y)];
  1772.         }
  1773.         dayoff = oadd(dayoff, eitol(i));
  1774.     }
  1775.     while (m != rp->r_month) {
  1776.         i = len_months[isleap(y)][m];
  1777.         dayoff = oadd(dayoff, eitol(i));
  1778.         ++m;
  1779.     }
  1780.     i = rp->r_dayofmonth;
  1781.     if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
  1782.         if (rp->r_dycode == DC_DOWLEQ)
  1783.             --i;
  1784.         else {
  1785.             error("use of 2/29 in non leap-year");
  1786.             (void) exit(EXIT_FAILURE);
  1787.         }
  1788.     }
  1789.     --i;
  1790.     dayoff = oadd(dayoff, eitol(i));
  1791.     if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
  1792.         register long    wday;
  1793.  
  1794. #define LDAYSPERWEEK    ((long) DAYSPERWEEK)
  1795.         wday = eitol(EPOCH_WDAY);
  1796.         /*
  1797.         ** Don't trust mod of negative numbers.
  1798.         */
  1799.         if (dayoff >= 0)
  1800.             wday = (wday + dayoff) % LDAYSPERWEEK;
  1801.         else {
  1802.             wday -= ((-dayoff) % LDAYSPERWEEK);
  1803.             if (wday < 0)
  1804.                 wday += LDAYSPERWEEK;
  1805.         }
  1806.         while (wday != eitol(rp->r_wday))
  1807.             if (rp->r_dycode == DC_DOWGEQ) {
  1808.                 dayoff = oadd(dayoff, (long) 1);
  1809.                 if (++wday >= LDAYSPERWEEK)
  1810.                     wday = 0;
  1811.                 ++i;
  1812.             } else {
  1813.                 dayoff = oadd(dayoff, (long) -1);
  1814.                 if (--wday < 0)
  1815.                     wday = LDAYSPERWEEK;
  1816.                 --i;
  1817.             }
  1818.         if (i < 0 || i >= len_months[isleap(y)][m]) {
  1819.             error("no day in month matches rule");
  1820.             (void) exit(EXIT_FAILURE);
  1821.         }
  1822.     }
  1823.     if (dayoff < 0 && !tt_signed) {
  1824.         if (wantedy == rp->r_loyear)
  1825.             return min_time;
  1826.         error("time before zero");
  1827.         (void) exit(EXIT_FAILURE);
  1828.     }
  1829.     t = (time_t) dayoff * SECSPERDAY;
  1830.     /*
  1831.     ** Cheap overflow check.
  1832.     */
  1833.     if (t / SECSPERDAY != dayoff) {
  1834.         if (wantedy == rp->r_hiyear)
  1835.             return max_time;
  1836.         if (wantedy == rp->r_loyear)
  1837.             return min_time;
  1838.         error("time overflow");
  1839.         (void) exit(EXIT_FAILURE);
  1840.     }
  1841.     return tadd(t, rp->r_tod);
  1842. }
  1843.  
  1844. static void
  1845. newabbr(string)
  1846. const char * const    string;
  1847. {
  1848.     register int    i;
  1849.  
  1850.     i = strlen(string) + 1;
  1851.     if (charcnt + i >= TZ_MAX_CHARS) {
  1852.         error("too many, or too long, time zone abbreviations");
  1853.         (void) exit(EXIT_FAILURE);
  1854.     }
  1855.     (void) strcpy(&chars[charcnt], string);
  1856.     charcnt += eitol(i);
  1857. }
  1858.  
  1859. static int
  1860. mkdirs(name)
  1861. char * const    name;
  1862. {
  1863.     register char *    cp;
  1864.  
  1865.     if ((cp = name) == NULL || *cp == '\0')
  1866.         return 0;
  1867.     while ((cp = strchr(cp + 1, '/')) != 0) {
  1868.         *cp = '\0';
  1869.         if (!itsdir(name)) {
  1870.             /*
  1871.             ** It doesn't seem to exist, so we try to create it.
  1872.             */
  1873.             if (mkdir(name, 0755) != 0) {
  1874.                 (void) fprintf(stderr,
  1875.                     "%s: Can't create directory ",
  1876.                     progname);
  1877.                 (void) perror(name);
  1878.                 return -1;
  1879.             }
  1880.         }
  1881.         *cp = '/';
  1882.     }
  1883.     return 0;
  1884. }
  1885.  
  1886. static long
  1887. eitol(i)
  1888. const int    i;
  1889. {
  1890.     long    l;
  1891.  
  1892.     l = i;
  1893.     if (i < 0 && l >= 0 || i == 0 && l != 0 || i > 0 && l <= 0) {
  1894.         (void) fprintf(stderr, "%s: %d did not sign extend correctly\n",
  1895.             progname, i);
  1896.         (void) exit(EXIT_FAILURE);
  1897.     }
  1898.     return l;
  1899. }
  1900.  
  1901. /*
  1902. ** UNIX is a registered trademark of AT&T.
  1903. */
  1904.