home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / ansi / time / ctime.c next >
Encoding:
C/C++ Source or Header  |  1995-08-27  |  35.0 KB  |  1,401 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. /* This file has been modified by DJ Delorie.  These modifications are
  3. ** Copyright (C) 1995 DJ Delorie, 24 Kirsten Ave, Rochester NH,
  4. ** 03867-2954, USA.
  5. */
  6.  
  7. /*
  8.  * Copyright (c) 1987, 1989 Regents of the University of California.
  9.  * All rights reserved.
  10.  *
  11.  * This code is derived from software contributed to Berkeley by
  12.  * Arthur David Olson of the National Cancer Institute.
  13.  *
  14.  * Redistribution and use in source and binary forms are permitted provided
  15.  * that: (1) source distributions retain this entire copyright notice and
  16.  * comment, and (2) distributions including binaries display the following
  17.  * acknowledgement:  ``This product includes software developed by the
  18.  * University of California, Berkeley and its contributors'' in the
  19.  * documentation or other materials provided with the distribution and in
  20.  * all advertising materials mentioning features or use of this software.
  21.  * Neither the name of the University nor the names of its contributors may
  22.  * be used to endorse or promote products derived from this software without
  23.  * specific prior written permission.
  24.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  25.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  26.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  27.  */
  28.  
  29. #if defined(LIBC_SCCS) && !defined(lint)
  30. static char sccsid[] = "@(#)ctime.c    5.23 (Berkeley) 6/22/90";
  31. #endif /* LIBC_SCCS and not lint */
  32.  
  33. /*
  34. ** Leap second handling from Bradley White (bww@k.gp.cs.cmu.edu).
  35. ** POSIX-style TZ environment variable handling from Guy Harris
  36. ** (guy@auspex.com).
  37. */
  38.  
  39. #include <libc/stubs.h>
  40. #include <fcntl.h>
  41. #include <time.h>
  42. #include <string.h>
  43. #include <ctype.h>
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <unistd.h>
  47. #include <tzfile.h>
  48.  
  49. #include <libc/unconst.h>
  50.  
  51. #include "posixrul.h"
  52.  
  53. #define P(s)        s
  54. #define alloc_size_t    size_t
  55. #define qsort_size_t    size_t
  56. #define fread_size_t    size_t
  57. #define fwrite_size_t    size_t
  58.  
  59. #define ACCESS_MODE    O_RDONLY|O_BINARY
  60. #define OPEN_MODE    O_RDONLY|O_BINARY
  61.  
  62. /*
  63. ** Someone might make incorrect use of a time zone abbreviation:
  64. **    1.    They might reference tzname[0] before calling tzset (explicitly
  65. **         or implicitly).
  66. **    2.    They might reference tzname[1] before calling tzset (explicitly
  67. **         or implicitly).
  68. **    3.    They might reference tzname[1] after setting to a time zone
  69. **        in which Daylight Saving Time is never observed.
  70. **    4.    They might reference tzname[0] after setting to a time zone
  71. **        in which Standard Time is never observed.
  72. **    5.    They might reference tm.TM_ZONE after calling offtime.
  73. ** What's best to do in the above cases is open to debate;
  74. ** for now, we just set things up so that in any of the five cases
  75. ** WILDABBR is used.  Another possibility:  initialize tzname[0] to the
  76. ** string "tzname[0] used before set", and similarly for the other cases.
  77. ** And another:  initialize tzname[0] to "ERA", with an explanation in the
  78. ** manual page of what this "time zone abbreviation" means (doing this so
  79. ** that tzname[0] has the "normal" length of three characters).
  80. */
  81. static char WILDABBR[] = "   ";
  82.  
  83. #ifndef TRUE
  84. #define TRUE        1
  85. #define FALSE        0
  86. #endif /* !defined TRUE */
  87.  
  88. static const char GMT[] = "GMT";
  89.  
  90. struct ttinfo {        /* time type information */
  91.   long tt_gmtoff;    /* GMT offset in seconds */
  92.   int tt_isdst;        /* used to set tm_isdst */
  93.   int tt_abbrind;    /* abbreviation list index */
  94.   int tt_ttisstd;    /* TRUE if transition is std time */
  95. };
  96.  
  97. struct lsinfo {        /* leap second information */
  98.   time_t ls_trans;    /* transition time */
  99.   long ls_corr;        /* correction to apply */
  100. };
  101.  
  102. struct state {
  103.   int leapcnt;
  104.   int timecnt;
  105.   int typecnt;
  106.   int charcnt;
  107.   time_t ats[TZ_MAX_TIMES];
  108.   unsigned char types[TZ_MAX_TIMES];
  109.   struct ttinfo ttis[TZ_MAX_TYPES];
  110.   char chars[(TZ_MAX_CHARS + 1 > sizeof GMT) ? TZ_MAX_CHARS + 1 : sizeof GMT];
  111.   struct lsinfo lsis[TZ_MAX_LEAPS];
  112. };
  113.  
  114. struct rule {
  115.   int r_type;        /* type of rule--see below */
  116.   int r_day;        /* day number of rule */
  117.   int r_week;        /* week number of rule */
  118.   int r_mon;        /* month number of rule */
  119.   long r_time;        /* transition time of rule */
  120. };
  121.  
  122. #define    JULIAN_DAY        0    /* Jn - Julian day */
  123. #define    DAY_OF_YEAR        1    /* n - day of year */
  124. #define    MONTH_NTH_DAY_OF_WEEK    2    /* Mm.n.d - month, week, day of week */
  125.  
  126. /*
  127. ** Prototypes for static functions.
  128. */
  129.  
  130. static long        detzcode P((const char * codep));
  131. static const char *    getzname P((const char * strp));
  132. static const char *    getnum P((const char * strp, int * nump, int min,
  133.                 int max));
  134. static const char *    getsecs P((const char * strp, long * secsp));
  135. static const char *    getoffset P((const char * strp, long * offsetp));
  136. static const char *    getrule P((const char * strp, struct rule * rulep));
  137. static void        gmtload P((struct state * sp));
  138. static void        gmtsub P((const time_t * timep, long offset,
  139.                 struct tm * tmp));
  140. static void        localsub P((const time_t * timep, long offset,
  141.                 struct tm * tmp));
  142. static void        normalize P((int * tensptr, int * unitsptr, int base));
  143. static void        settzname P((void));
  144. static time_t        time1 P((struct tm * tmp, void (* funcp)(const time_t * const, const long, struct tm * const),
  145.                 long offset));
  146. static time_t        time2 P((struct tm *tmp, void (* funcp)(const time_t * const, const long, struct tm * const),
  147.                 long offset, int * okayp));
  148. static void        timesub P((const time_t * timep, long offset,
  149.                 const struct state * sp, struct tm * tmp));
  150. static int        tmcomp P((const struct tm * atmp,
  151.                 const struct tm * btmp));
  152. static time_t        transtime P((time_t janfirst, int year,
  153.                 const struct rule * rulep, long offset));
  154. static int        tzload P((const char * name, struct state * sp));
  155. static int        tzparse P((const char * name, struct state * sp,
  156.                 int lastditch));
  157. static void        tzsetwall(void);
  158.  
  159. #ifdef ALL_STATE
  160. static struct state *lclptr;
  161. static struct state *gmtptr;
  162. #endif /* defined ALL_STATE */
  163.  
  164. #ifndef ALL_STATE
  165. static struct state lclmem;
  166. static struct state gmtmem;
  167. #define lclptr (&lclmem)
  168. #define gmtptr (&gmtmem)
  169. #endif /* State Farm */
  170.  
  171. static int lcl_is_set;
  172. static int gmt_is_set;
  173.  
  174. char * tzname[2] = {
  175.   WILDABBR,
  176.   WILDABBR
  177. };
  178.  
  179. static long
  180. detzcode(const char * const codep)
  181. {
  182.   long result;
  183.   int i;
  184.  
  185.   result = 0;
  186.   for (i = 0; i < 4; ++i)
  187.     result = (result << 8) | (codep[i] & 0xff);
  188.   return result;
  189. }
  190.  
  191. static void
  192. settzname(void)
  193. {
  194.   const struct state * const sp = lclptr;
  195.   int i;
  196.  
  197.   tzname[0] = WILDABBR;
  198.   tzname[1] = WILDABBR;
  199. #ifdef ALL_STATE
  200.   if (sp == NULL)
  201.   {
  202.     tzname[0] = tzname[1] = GMT;
  203.     return;
  204.   }
  205. #endif /* defined ALL_STATE */
  206.   for (i = 0; i < sp->typecnt; ++i)
  207.   {
  208.     register const struct ttinfo * const ttisp = &sp->ttis[i];
  209.  
  210.     tzname[ttisp->tt_isdst] =
  211.       unconst(&sp->chars[ttisp->tt_abbrind], char *);
  212. #if 0
  213.     if (ttisp->tt_isdst)
  214.       _daylight = 1;
  215.     if (i == 0 || !ttisp->tt_isdst)
  216.       _timezone = -(ttisp->tt_gmtoff);
  217.     if (i == 0 || ttisp->tt_isdst)
  218.       _altzone = -(ttisp->tt_gmtoff);
  219. #endif
  220.   }
  221.   /*
  222.    ** And to get the latest zone names into tzname. . .
  223.    */
  224.   for (i = 0; i < sp->timecnt; ++i)
  225.   {
  226.     const struct ttinfo * const ttisp = &sp->ttis[sp->types[i]];
  227.  
  228.     tzname[ttisp->tt_isdst] = unconst(&sp->chars[ttisp->tt_abbrind], char *);
  229.   }
  230. }
  231.  
  232. static char *
  233. tzdir(void)
  234. {
  235.   static char dir[80]={0}, *cp;
  236.   if (dir[0] == 0)
  237.   {
  238.     if ((cp = getenv("TZDIR")))
  239.     {
  240.       strcpy(dir, cp);
  241.     }
  242.     else if ((cp = getenv("DJDIR")))
  243.     {
  244.       strcpy(dir, cp);
  245.       strcat(dir, "/zoneinfo");
  246.     }
  247.     else
  248.       strcpy(dir, "./");
  249.   }
  250.   return dir;
  251. }
  252.  
  253. static int
  254. tzload(const char *name, struct state * const sp)
  255. {
  256.   const char * p;
  257.   int i;
  258.   int fid;
  259.   char fullname[FILENAME_MAX + 1];
  260.   const struct tzhead * tzhp;
  261.   char buf[sizeof *sp + sizeof *tzhp];
  262.   int ttisstdcnt;
  263.  
  264.   if (name == NULL && (name = TZDEFAULT) == NULL)
  265.     return -1;
  266.  
  267.   if (name[0] == ':')
  268.     ++name;
  269.   if (name[0] != '/')
  270.   {
  271.     if ((p = tzdir()) == NULL)
  272.       return -1;
  273.     if ((strlen(p) + strlen(name) + 1) >= sizeof fullname)
  274.       return -1;
  275.     strcpy(fullname, p);
  276.     strcat(fullname, "/");
  277.     strcat(fullname, name);
  278.     name = fullname;
  279.   }
  280.  
  281.   if ((fid = open(name, OPEN_MODE)) == -1)
  282.   {
  283.     const char *base = strrchr(name, '/');
  284.     if (base)
  285.       base++;
  286.     else
  287.       base = name;
  288.     if (strcmp(base, "posixrules"))
  289.       return -1;
  290.  
  291.     /* We've got a built-in copy of posixrules just in case */
  292.     memcpy(buf, _posixrules_data, sizeof(_posixrules_data));
  293.     i = sizeof(_posixrules_data);
  294.   }
  295.   else
  296.   {
  297.     i = read(fid, buf, sizeof buf);
  298.     if (close(fid) != 0 || i < sizeof *tzhp)
  299.       return -1;
  300.   }
  301.  
  302.   tzhp = (struct tzhead *) buf;
  303.   ttisstdcnt = (int) detzcode(tzhp->tzh_ttisstdcnt);
  304.   sp->leapcnt = (int) detzcode(tzhp->tzh_leapcnt);
  305.   sp->timecnt = (int) detzcode(tzhp->tzh_timecnt);
  306.   sp->typecnt = (int) detzcode(tzhp->tzh_typecnt);
  307.   sp->charcnt = (int) detzcode(tzhp->tzh_charcnt);
  308.   if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
  309.       sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
  310.       sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
  311.       sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
  312.       (ttisstdcnt != sp->typecnt && ttisstdcnt != 0))
  313.     return -1;
  314.   if (i < sizeof *tzhp +
  315.       sp->timecnt * (4 + sizeof (char)) +
  316.       sp->typecnt * (4 + 2 * sizeof (char)) +
  317.       sp->charcnt * sizeof (char) +
  318.       sp->leapcnt * 2 * 4 +
  319.       ttisstdcnt * sizeof (char))
  320.     return -1;
  321.   p = buf + sizeof *tzhp;
  322.   for (i = 0; i < sp->timecnt; ++i)
  323.   {
  324.     sp->ats[i] = detzcode(p);
  325.     p += 4;
  326.   }
  327.   for (i = 0; i < sp->timecnt; ++i)
  328.   {
  329.     sp->types[i] = (unsigned char) *p++;
  330.     if (sp->types[i] >= sp->typecnt)
  331.       return -1;
  332.   }
  333.   for (i = 0; i < sp->typecnt; ++i)
  334.   {
  335.     struct ttinfo * ttisp;
  336.  
  337.     ttisp = &sp->ttis[i];
  338.     ttisp->tt_gmtoff = detzcode(p);
  339.     p += 4;
  340.     ttisp->tt_isdst = (unsigned char) *p++;
  341.     if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
  342.       return -1;
  343.     ttisp->tt_abbrind = (unsigned char) *p++;
  344.     if (ttisp->tt_abbrind < 0 ||
  345.     ttisp->tt_abbrind > sp->charcnt)
  346.       return -1;
  347.   }
  348.   for (i = 0; i < sp->charcnt; ++i)
  349.     sp->chars[i] = *p++;
  350.   sp->chars[i] = '\0';        /* ensure '\0' at end */
  351.   for (i = 0; i < sp->leapcnt; ++i)
  352.   {
  353.     struct lsinfo * lsisp;
  354.  
  355.     lsisp = &sp->lsis[i];
  356.     lsisp->ls_trans = detzcode(p);
  357.     p += 4;
  358.     lsisp->ls_corr = detzcode(p);
  359.     p += 4;
  360.   }
  361.   for (i = 0; i < sp->typecnt; ++i)
  362.   {
  363.     struct ttinfo * ttisp;
  364.  
  365.     ttisp = &sp->ttis[i];
  366.     if (ttisstdcnt == 0)
  367.       ttisp->tt_ttisstd = FALSE;
  368.     else
  369.     {
  370.       ttisp->tt_ttisstd = *p++;
  371.       if (ttisp->tt_ttisstd != TRUE &&
  372.       ttisp->tt_ttisstd != FALSE)
  373.     return -1;
  374.     }
  375.   }
  376.   return 0;
  377. }
  378.  
  379. static const int mon_lengths[2][MONSPERYEAR] = {
  380. { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
  381. { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
  382. };
  383.  
  384. static const int year_lengths[2] = {
  385. DAYSPERNYEAR, DAYSPERLYEAR
  386. };
  387.  
  388. /*
  389. ** Given a pointer into a time zone string, scan until a character that is not
  390. ** a valid character in a zone name is found.  Return a pointer to that
  391. ** character.
  392. */
  393.  
  394. static const char *
  395. getzname(const char *strp)
  396. {
  397.   char c;
  398.  
  399.   while ((c = *strp) != '\0' && !isdigit(c) && c != ',' && c != '-' &&
  400.      c != '+')
  401.     ++strp;
  402.   return strp;
  403. }
  404.  
  405. /*
  406. ** Given a pointer into a time zone string, extract a number from that string.
  407. ** Check that the number is within a specified range; if it is not, return
  408. ** NULL.
  409. ** Otherwise, return a pointer to the first character not part of the number.
  410. */
  411.  
  412. static const char *
  413. getnum(const char *strp, int * const nump, const int min, const int max)
  414. {
  415.   char c;
  416.   int num;
  417.  
  418.   if (strp == NULL || !isdigit(*strp))
  419.     return NULL;
  420.   num = 0;
  421.   while ((c = *strp) != '\0' && isdigit(c))
  422.   {
  423.     num = num * 10 + (c - '0');
  424.     if (num > max)
  425.       return NULL;
  426.     ++strp;
  427.   }
  428.   if (num < min)
  429.     return NULL;
  430.   *nump = num;
  431.   return strp;
  432. }
  433.  
  434. /*
  435. ** Given a pointer into a time zone string, extract a number of seconds,
  436. ** in hh[:mm[:ss]] form, from the string.
  437. ** If any error occurs, return NULL.
  438. ** Otherwise, return a pointer to the first character not part of the number
  439. ** of seconds.
  440. */
  441.  
  442. static const char *
  443. getsecs(const char *strp, long * const secsp)
  444. {
  445.   int num;
  446.  
  447.   strp = getnum(strp, &num, 0, HOURSPERDAY);
  448.   if (strp == NULL)
  449.     return NULL;
  450.   *secsp = num * SECSPERHOUR;
  451.   if (*strp == ':')
  452.   {
  453.     ++strp;
  454.     strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
  455.     if (strp == NULL)
  456.       return NULL;
  457.     *secsp += num * SECSPERMIN;
  458.     if (*strp == ':')
  459.     {
  460.       ++strp;
  461.       strp = getnum(strp, &num, 0, SECSPERMIN - 1);
  462.       if (strp == NULL)
  463.     return NULL;
  464.       *secsp += num;
  465.     }
  466.   }
  467.   return strp;
  468. }
  469.  
  470. /*
  471. ** Given a pointer into a time zone string, extract an offset, in
  472. ** [+-]hh[:mm[:ss]] form, from the string.
  473. ** If any error occurs, return NULL.
  474. ** Otherwise, return a pointer to the first character not part of the time.
  475. */
  476.  
  477. static const char *
  478. getoffset(const char *strp, long * const offsetp)
  479. {
  480.   int neg;
  481.  
  482.   if (*strp == '-')
  483.   {
  484.     neg = 1;
  485.     ++strp;
  486.   }
  487.   else if (isdigit(*strp) || *strp++ == '+')
  488.     neg = 0;
  489.   else
  490.     return NULL; /* illegal offset */
  491.   strp = getsecs(strp, offsetp);
  492.   if (strp == NULL)
  493.     return NULL; /* illegal time */
  494.   if (neg)
  495.     *offsetp = -*offsetp;
  496.   return strp;
  497. }
  498.  
  499. /*
  500. ** Given a pointer into a time zone string, extract a rule in the form
  501. ** date[/time].  See POSIX section 8 for the format of "date" and "time".
  502. ** If a valid rule is not found, return NULL.
  503. ** Otherwise, return a pointer to the first character not part of the rule.
  504. */
  505.  
  506. static const char *
  507. getrule(const char *strp, struct rule * const rulep)
  508. {
  509.   if (*strp == 'J')
  510.   {
  511.     /*
  512.      ** Julian day.
  513.      */
  514.     rulep->r_type = JULIAN_DAY;
  515.     ++strp;
  516.     strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
  517.   }
  518.   else if (*strp == 'M')
  519.   {
  520.     /*
  521.      ** Month, week, day.
  522.      */
  523.     rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
  524.     ++strp;
  525.     strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
  526.     if (strp == NULL)
  527.       return NULL;
  528.     if (*strp++ != '.')
  529.       return NULL;
  530.     strp = getnum(strp, &rulep->r_week, 1, 5);
  531.     if (strp == NULL)
  532.       return NULL;
  533.     if (*strp++ != '.')
  534.       return NULL;
  535.     strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
  536.   }
  537.   else if (isdigit(*strp))
  538.   {
  539.     /*
  540.      ** Day of year.
  541.      */
  542.     rulep->r_type = DAY_OF_YEAR;
  543.     strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
  544.   }
  545.   else
  546.     return NULL;        /* invalid format */
  547.   if (strp == NULL)
  548.     return NULL;
  549.   if (*strp == '/')
  550.   {
  551.     /*
  552.      ** Time specified.
  553.      */
  554.     ++strp;
  555.     strp = getsecs(strp, &rulep->r_time);
  556.   }
  557.   else
  558.     rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
  559.   return strp;
  560. }
  561.  
  562. /*
  563. ** Given the Epoch-relative time of January 1, 00:00:00 GMT, in a year, the
  564. ** year, a rule, and the offset from GMT at the time that rule takes effect,
  565. ** calculate the Epoch-relative time that rule takes effect.
  566. */
  567.  
  568. static time_t
  569. transtime(const time_t janfirst, const int year, const struct rule * const rulep, const long offset)
  570. {
  571.   int leapyear;
  572.   time_t value=0;
  573.   int i;
  574.   int d, m1, yy0, yy1, yy2, dow;
  575.  
  576.   leapyear = isleap(year);
  577.   switch (rulep->r_type)
  578.   {
  579.  
  580.   case JULIAN_DAY:
  581.     /*
  582.      ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
  583.      ** years.
  584.      ** In non-leap years, or if the day number is 59 or less, just
  585.      ** add SECSPERDAY times the day number-1 to the time of
  586.      ** January 1, midnight, to get the day.
  587.      */
  588.     value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
  589.     if (leapyear && rulep->r_day >= 60)
  590.       value += SECSPERDAY;
  591.     break;
  592.  
  593.   case DAY_OF_YEAR:
  594.     /*
  595.      ** n - day of year.
  596.      ** Just add SECSPERDAY times the day number to the time of
  597.      ** January 1, midnight, to get the day.
  598.      */
  599.     value = janfirst + rulep->r_day * SECSPERDAY;
  600.     break;
  601.  
  602.   case MONTH_NTH_DAY_OF_WEEK:
  603.     /*
  604.      ** Mm.n.d - nth "dth day" of month m.
  605.      */
  606.     value = janfirst;
  607.     for (i = 0; i < rulep->r_mon - 1; ++i)
  608.       value += mon_lengths[leapyear][i] * SECSPERDAY;
  609.  
  610.     /*
  611.      ** Use Zeller's Congruence to get day-of-week of first day of
  612.      ** month.
  613.      */
  614.     m1 = (rulep->r_mon + 9) % 12 + 1;
  615.     yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
  616.     yy1 = yy0 / 100;
  617.     yy2 = yy0 % 100;
  618.     dow = ((26 * m1 - 2) / 10 +
  619.        1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
  620.     if (dow < 0)
  621.       dow += DAYSPERWEEK;
  622.  
  623.     /*
  624.      ** "dow" is the day-of-week of the first day of the month.  Get
  625.      ** the day-of-month (zero-origin) of the first "dow" day of the
  626.      ** month.
  627.      */
  628.     d = rulep->r_day - dow;
  629.     if (d < 0)
  630.       d += DAYSPERWEEK;
  631.     for (i = 1; i < rulep->r_week; ++i)
  632.     {
  633.       if (d + DAYSPERWEEK >=
  634.       mon_lengths[leapyear][rulep->r_mon - 1])
  635.     break;
  636.       d += DAYSPERWEEK;
  637.     }
  638.  
  639.     /*
  640.      ** "d" is the day-of-month (zero-origin) of the day we want.
  641.      */
  642.     value += d * SECSPERDAY;
  643.     break;
  644.   }
  645.  
  646.   /*
  647.    ** "value" is the Epoch-relative time of 00:00:00 GMT on the day in
  648.    ** question.  To get the Epoch-relative time of the specified local
  649.    ** time on that day, add the transition time and the current offset
  650.    ** from GMT.
  651.    */
  652.   return value + rulep->r_time + offset;
  653. }
  654.  
  655. /*
  656. ** Given a POSIX section 8-style TZ string, fill in the rule tables as
  657. ** appropriate.
  658. */
  659.  
  660. static int
  661. tzparse(const char *name, struct state * const sp, const int lastditch)
  662. {
  663.   const char * stdname;
  664.   const char * dstname=0;
  665.   int stdlen;
  666.   int dstlen;
  667.   long stdoffset;
  668.   long dstoffset;
  669.   time_t * atp;
  670.   unsigned char * typep;
  671.   char * cp;
  672.   int load_result;
  673.  
  674.   stdname = name;
  675.   if (lastditch)
  676.   {
  677.     stdlen = strlen(name);    /* length of standard zone name */
  678.     name += stdlen;
  679.     if (stdlen >= sizeof sp->chars)
  680.       stdlen = (sizeof sp->chars) - 1;
  681.   }
  682.   else
  683.   {
  684.     name = getzname(name);
  685.     stdlen = name - stdname;
  686.     if (stdlen < 3)
  687.       return -1;
  688.   }
  689.   if (*name == '\0')
  690.     return -1;
  691.   else
  692.   {
  693.     name = getoffset(name, &stdoffset);
  694.     if (name == NULL)
  695.       return -1;
  696.   }
  697.   load_result = tzload(TZDEFRULES, sp);
  698.   if (load_result != 0)
  699.     sp->leapcnt = 0;        /* so, we're off a little */
  700.   if (*name != '\0')
  701.   {
  702.     dstname = name;
  703.     name = getzname(name);
  704.     dstlen = name - dstname;    /* length of DST zone name */
  705.     if (dstlen < 3)
  706.       return -1;
  707.     if (*name != '\0' && *name != ',' && *name != ';')
  708.     {
  709.       name = getoffset(name, &dstoffset);
  710.       if (name == NULL)
  711.     return -1;
  712.     }
  713.     else
  714.       dstoffset = stdoffset - SECSPERHOUR;
  715.     if (*name == ',' || *name == ';')
  716.     {
  717.       struct rule start;
  718.       struct rule end;
  719.       int year;
  720.       time_t janfirst;
  721.       time_t starttime;
  722.       time_t endtime;
  723.  
  724.       ++name;
  725.       if ((name = getrule(name, &start)) == NULL)
  726.     return -1;
  727.       if (*name++ != ',')
  728.     return -1;
  729.       if ((name = getrule(name, &end)) == NULL)
  730.     return -1;
  731.       if (*name != '\0')
  732.     return -1;
  733.       sp->typecnt = 2;        /* standard time and DST */
  734.       /*
  735.        ** Two transitions per year, from EPOCH_YEAR to 2037.
  736.        */
  737.       sp->timecnt = 2 * (2037 - EPOCH_YEAR + 1);
  738.       if (sp->timecnt > TZ_MAX_TIMES)
  739.     return -1;
  740.       sp->ttis[0].tt_gmtoff = -dstoffset;
  741.       sp->ttis[0].tt_isdst = 1;
  742.       sp->ttis[0].tt_abbrind = stdlen + 1;
  743.       sp->ttis[1].tt_gmtoff = -stdoffset;
  744.       sp->ttis[1].tt_isdst = 0;
  745.       sp->ttis[1].tt_abbrind = 0;
  746.       atp = sp->ats;
  747.       typep = sp->types;
  748.       janfirst = 0;
  749.       for (year = EPOCH_YEAR; year <= 2037; ++year)
  750.       {
  751.     starttime = transtime(janfirst, year, &start,
  752.                   stdoffset);
  753.     endtime = transtime(janfirst, year, &end,
  754.                 dstoffset);
  755.     if (starttime > endtime)
  756.     {
  757.       *atp++ = endtime;
  758.       *typep++ = 1;        /* DST ends */
  759.       *atp++ = starttime;
  760.       *typep++ = 0;        /* DST begins */
  761.     }
  762.     else
  763.     {
  764.       *atp++ = starttime;
  765.       *typep++ = 0;        /* DST begins */
  766.       *atp++ = endtime;
  767.       *typep++ = 1;        /* DST ends */
  768.     }
  769.     janfirst +=
  770.       year_lengths[isleap(year)] * SECSPERDAY;
  771.       }
  772.     }
  773.     else
  774.     {
  775.       int sawstd;
  776.       int sawdst;
  777.       long stdfix;
  778.       long dstfix;
  779.       long oldfix;
  780.       int isdst;
  781.       int i;
  782.  
  783.       if (*name != '\0')
  784.     return -1;
  785.       if (load_result != 0)
  786.     return -1;
  787.       /*
  788.        ** Compute the difference between the real and
  789.        ** prototype standard and summer time offsets
  790.        ** from GMT, and put the real standard and summer
  791.        ** time offsets into the rules in place of the
  792.        ** prototype offsets.
  793.        */
  794.       sawstd = FALSE;
  795.       sawdst = FALSE;
  796.       stdfix = 0;
  797.       dstfix = 0;
  798.       for (i = 0; i < sp->typecnt; ++i)
  799.       {
  800.     if (sp->ttis[i].tt_isdst)
  801.     {
  802.       oldfix = dstfix;
  803.       dstfix =
  804.         sp->ttis[i].tt_gmtoff + dstoffset;
  805.       if (sawdst && (oldfix != dstfix))
  806.         return -1;
  807.       sp->ttis[i].tt_gmtoff = -dstoffset;
  808.       sp->ttis[i].tt_abbrind = stdlen + 1;
  809.       sawdst = TRUE;
  810.     }
  811.     else
  812.     {
  813.       oldfix = stdfix;
  814.       stdfix =
  815.         sp->ttis[i].tt_gmtoff + stdoffset;
  816.       if (sawstd && (oldfix != stdfix))
  817.         return -1;
  818.       sp->ttis[i].tt_gmtoff = -stdoffset;
  819.       sp->ttis[i].tt_abbrind = 0;
  820.       sawstd = TRUE;
  821.     }
  822.       }
  823.       /*
  824.        ** Make sure we have both standard and summer time.
  825.        */
  826.       if (!sawdst || !sawstd)
  827.     return -1;
  828.       /*
  829.        ** Now correct the transition times by shifting
  830.        ** them by the difference between the real and
  831.        ** prototype offsets.  Note that this difference
  832.        ** can be different in standard and summer time;
  833.        ** the prototype probably has a 1-hour difference
  834.        ** between standard and summer time, but a different
  835.        ** difference can be specified in TZ.
  836.        */
  837.       isdst = FALSE; /* we start in standard time */
  838.       for (i = 0; i < sp->timecnt; ++i)
  839.       {
  840.     const struct ttinfo * ttisp;
  841.  
  842.     /*
  843.      ** If summer time is in effect, and the
  844.      ** transition time was not specified as
  845.      ** standard time, add the summer time
  846.      ** offset to the transition time;
  847.      ** otherwise, add the standard time offset
  848.      ** to the transition time.
  849.      */
  850.     ttisp = &sp->ttis[sp->types[i]];
  851.     sp->ats[i] +=
  852.       (isdst && !ttisp->tt_ttisstd) ?
  853.         dstfix : stdfix;
  854.     isdst = ttisp->tt_isdst;
  855.       }
  856.     }
  857.   }
  858.   else
  859.   {
  860.     dstlen = 0;
  861.     sp->typecnt = 1;        /* only standard time */
  862.     sp->timecnt = 0;
  863.     sp->ttis[0].tt_gmtoff = -stdoffset;
  864.     sp->ttis[0].tt_isdst = 0;
  865.     sp->ttis[0].tt_abbrind = 0;
  866.   }
  867.   sp->charcnt = stdlen + 1;
  868.   if (dstlen != 0)
  869.     sp->charcnt += dstlen + 1;
  870.   if (sp->charcnt > sizeof sp->chars)
  871.     return -1;
  872.   cp = sp->chars;
  873.   (void) strncpy(cp, stdname, stdlen);
  874.   cp += stdlen;
  875.   *cp++ = '\0';
  876.   if (dstlen != 0)
  877.   {
  878.     (void) strncpy(cp, dstname, dstlen);
  879.     *(cp + dstlen) = '\0';
  880.   }
  881.   return 0;
  882. }
  883.  
  884. static void
  885. gmtload(struct state * const sp)
  886. {
  887.   if (tzload(GMT, sp) != 0)
  888.     (void) tzparse(GMT, sp, TRUE);
  889. }
  890.  
  891. void
  892. tzset(void)
  893. {
  894.   const char * name;
  895.  
  896.   name = getenv("TZ");
  897.   if (name == NULL)
  898.   {
  899.     tzsetwall();
  900.     return;
  901.   }
  902.   lcl_is_set = TRUE;
  903. #ifdef ALL_STATE
  904.   if (lclptr == NULL)
  905.   {
  906.     lclptr = (struct state *) malloc(sizeof *lclptr);
  907.     if (lclptr == NULL)
  908.     {
  909.       settzname();        /* all we can do */
  910.       return;
  911.     }
  912.   }
  913. #endif /* defined ALL_STATE */
  914.   if (*name == '\0')
  915.   {
  916.     /*
  917.      ** User wants it fast rather than right.
  918.      */
  919.     lclptr->leapcnt = 0;    /* so, we're off a little */
  920.     lclptr->timecnt = 0;
  921.     lclptr->ttis[0].tt_gmtoff = 0;
  922.     lclptr->ttis[0].tt_abbrind = 0;
  923.     (void) strcpy(lclptr->chars, GMT);
  924.   }
  925.   else if (tzload(name, lclptr) != 0)
  926.     if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
  927.       gmtload(lclptr);
  928.   settzname();
  929. }
  930.  
  931. void
  932. tzsetwall(void)
  933. {
  934.   lcl_is_set = TRUE;
  935. #ifdef ALL_STATE
  936.   if (lclptr == NULL)
  937.   {
  938.     lclptr = (struct state *) malloc(sizeof *lclptr);
  939.     if (lclptr == NULL)
  940.     {
  941.       settzname();        /* all we can do */
  942.       return;
  943.     }
  944.   }
  945. #endif /* defined ALL_STATE */
  946.   if (tzload((char *) NULL, lclptr) != 0)
  947.     gmtload(lclptr);
  948.   settzname();
  949. }
  950.  
  951. /*
  952. ** The easy way to behave "as if no library function calls" localtime
  953. ** is to not call it--so we drop its guts into "localsub", which can be
  954. ** freely called.  (And no, the PANS doesn't require the above behavior--
  955. ** but it *is* desirable.)
  956. **
  957. ** The unused offset argument is for the benefit of mktime variants.
  958. */
  959.  
  960. /*ARGSUSED*/
  961. static void
  962. localsub(const time_t * const timep, const long offset, struct tm * const tmp)
  963. {
  964.   const struct state * sp;
  965.   const struct ttinfo * ttisp;
  966.   int i;
  967.   const time_t t = *timep;
  968.  
  969.   if (!lcl_is_set)
  970.     tzset();
  971.   sp = lclptr;
  972. #ifdef ALL_STATE
  973.   if (sp == NULL)
  974.   {
  975.     gmtsub(timep, offset, tmp);
  976.     return;
  977.   }
  978. #endif /* defined ALL_STATE */
  979.   if (sp->timecnt == 0 || t < sp->ats[0])
  980.   {
  981.     i = 0;
  982.     while (sp->ttis[i].tt_isdst)
  983.       if (++i >= sp->typecnt)
  984.       {
  985.     i = 0;
  986.     break;
  987.       }
  988.   }
  989.   else
  990.   {
  991.     for (i = 1; i < sp->timecnt; ++i)
  992.       if (t < sp->ats[i])
  993.     break;
  994.     i = sp->types[i - 1];
  995.   }
  996.   ttisp = &sp->ttis[i];
  997.   /*
  998.    ** To get (wrong) behavior that's compatible with System V Release 2.0
  999.    ** you'd replace the statement below with
  1000.    ** t += ttisp->tt_gmtoff;
  1001.    ** timesub(&t, 0L, sp, tmp);
  1002.    */
  1003.   timesub(&t, ttisp->tt_gmtoff, sp, tmp);
  1004.   tmp->tm_isdst = ttisp->tt_isdst;
  1005.   tzname[tmp->tm_isdst] = unconst(&sp->chars[ttisp->tt_abbrind], char *);
  1006.   tmp->tm_zone = unconst(&sp->chars[ttisp->tt_abbrind], char *);
  1007. }
  1008.  
  1009. struct tm *
  1010. localtime(const time_t * const timep)
  1011. {
  1012.   static struct tm tm;
  1013.  
  1014.   localsub(timep, 0L, &tm);
  1015.   return &tm;
  1016. }
  1017.  
  1018. /*
  1019. ** gmtsub is to gmtime as localsub is to localtime.
  1020. */
  1021.  
  1022. static void
  1023. gmtsub(const time_t * const timep, const long offset, struct tm * const tmp)
  1024. {
  1025.   if (!gmt_is_set)
  1026.   {
  1027.     gmt_is_set = TRUE;
  1028. #ifdef ALL_STATE
  1029.     gmtptr = (struct state *) malloc(sizeof *gmtptr);
  1030.     if (gmtptr != NULL)
  1031. #endif /* defined ALL_STATE */
  1032.       gmtload(gmtptr);
  1033.   }
  1034.   timesub(timep, offset, gmtptr, tmp);
  1035.   /*
  1036.    ** Could get fancy here and deliver something such as
  1037.    ** "GMT+xxxx" or "GMT-xxxx" if offset is non-zero,
  1038.    ** but this is no time for a treasure hunt.
  1039.    */
  1040.   if (offset != 0)
  1041.     tmp->tm_zone = WILDABBR;
  1042.   else
  1043.   {
  1044. #ifdef ALL_STATE
  1045.     if (gmtptr == NULL)
  1046.       tmp->TM_ZONE = GMT;
  1047.     else
  1048.       tmp->TM_ZONE = gmtptr->chars;
  1049. #endif /* defined ALL_STATE */
  1050. #ifndef ALL_STATE
  1051.     tmp->tm_zone = gmtptr->chars;
  1052. #endif /* State Farm */
  1053.   }
  1054. }
  1055.  
  1056. struct tm *
  1057. gmtime(const time_t * const timep)
  1058. {
  1059.   static struct tm tm;
  1060.  
  1061.   gmtsub(timep, 0L, &tm);
  1062.   return &tm;
  1063. }
  1064.  
  1065. static void
  1066. timesub(const time_t * const timep, const long offset, const struct state * const sp, struct tm * const tmp)
  1067. {
  1068.   const struct lsinfo * lp;
  1069.   long days;
  1070.   long rem;
  1071.   int y;
  1072.   int yleap;
  1073.   const int * ip;
  1074.   long corr;
  1075.   int hit;
  1076.   int i;
  1077.  
  1078.   corr = 0;
  1079.   hit = FALSE;
  1080. #ifdef ALL_STATE
  1081.   i = (sp == NULL) ? 0 : sp->leapcnt;
  1082. #endif /* defined ALL_STATE */
  1083. #ifndef ALL_STATE
  1084.   i = sp->leapcnt;
  1085. #endif /* State Farm */
  1086.   while (--i >= 0)
  1087.   {
  1088.     lp = &sp->lsis[i];
  1089.     if (*timep >= lp->ls_trans)
  1090.     {
  1091.       if (*timep == lp->ls_trans)
  1092.     hit = ((i == 0 && lp->ls_corr > 0) ||
  1093.            lp->ls_corr > sp->lsis[i - 1].ls_corr);
  1094.       corr = lp->ls_corr;
  1095.       break;
  1096.     }
  1097.   }
  1098.   days = *timep / SECSPERDAY;
  1099.   rem = *timep % SECSPERDAY;
  1100. #ifdef mc68k
  1101.   if (*timep == 0x80000000)
  1102.   {
  1103.     /*
  1104.      ** A 3B1 muffs the division on the most negative number.
  1105.      */
  1106.     days = -24855;
  1107.     rem = -11648;
  1108.   }
  1109. #endif /* mc68k */
  1110.   rem += (offset - corr);
  1111.   while (rem < 0)
  1112.   {
  1113.     rem += SECSPERDAY;
  1114.     --days;
  1115.   }
  1116.   while (rem >= SECSPERDAY)
  1117.   {
  1118.     rem -= SECSPERDAY;
  1119.     ++days;
  1120.   }
  1121.   tmp->tm_hour = (int) (rem / SECSPERHOUR);
  1122.   rem = rem % SECSPERHOUR;
  1123.   tmp->tm_min = (int) (rem / SECSPERMIN);
  1124.   tmp->tm_sec = (int) (rem % SECSPERMIN);
  1125.   if (hit)
  1126.     /*
  1127.      ** A positive leap second requires a special
  1128.      ** representation.  This uses "... ??:59:60".
  1129.      */
  1130.     ++(tmp->tm_sec);
  1131.   tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYSPERWEEK);
  1132.   if (tmp->tm_wday < 0)
  1133.     tmp->tm_wday += DAYSPERWEEK;
  1134.   y = EPOCH_YEAR;
  1135.   if (days >= 0)
  1136.     for ( ; ; )
  1137.     {
  1138.       yleap = isleap(y);
  1139.       if (days < (long) year_lengths[yleap])
  1140.     break;
  1141.       ++y;
  1142.       days = days - (long) year_lengths[yleap];
  1143.     }
  1144.   else
  1145.     do {
  1146.     --y;
  1147.     yleap = isleap(y);
  1148.     days = days + (long) year_lengths[yleap];
  1149.   } while (days < 0);
  1150.   tmp->tm_year = y - TM_YEAR_BASE;
  1151.   tmp->tm_yday = (int) days;
  1152.   ip = mon_lengths[yleap];
  1153.   for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon))
  1154.     days = days - (long) ip[tmp->tm_mon];
  1155.   tmp->tm_mday = (int) (days + 1);
  1156.   tmp->tm_isdst = 0;
  1157.   tmp->tm_gmtoff = offset;
  1158. }
  1159.  
  1160. /*
  1161. ** A la X3J11
  1162. */
  1163.  
  1164. char *
  1165. asctime(const struct tm *timeptr)
  1166. {
  1167.   static const char wday_name[DAYSPERWEEK][3] = {
  1168.     "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  1169.   };
  1170.   static const char mon_name[MONSPERYEAR][3] = {
  1171.     "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  1172.     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  1173.   };
  1174.   static char result[26];
  1175.  
  1176.   (void) sprintf(result, "%.3s %.3s%3d %02d:%02d:%02d %d\n",
  1177.          wday_name[timeptr->tm_wday],
  1178.          mon_name[timeptr->tm_mon],
  1179.          timeptr->tm_mday, timeptr->tm_hour,
  1180.          timeptr->tm_min, timeptr->tm_sec,
  1181.          TM_YEAR_BASE + timeptr->tm_year);
  1182.   return result;
  1183. }
  1184.  
  1185. char *
  1186. ctime(const time_t * const timep)
  1187. {
  1188.   return asctime(localtime(timep));
  1189. }
  1190.  
  1191. /*
  1192. ** Adapted from code provided by Robert Elz, who writes:
  1193. **    The "best" way to do mktime I think is based on an idea of Bob
  1194. **    Kridle's (so its said...) from a long time ago. (mtxinu!kridle now).
  1195. **    It does a binary search of the time_t space.  Since time_t's are
  1196. **    just 32 bits, its a max of 32 iterations (even at 64 bits it
  1197. **    would still be very reasonable).
  1198. */
  1199.  
  1200. #ifndef WRONG
  1201. #define WRONG    (-1)
  1202. #endif /* !defined WRONG */
  1203.  
  1204. static void
  1205. normalize(int * const tensptr, int * const unitsptr, const int base)
  1206. {
  1207.   if (*unitsptr >= base)
  1208.   {
  1209.     *tensptr += *unitsptr / base;
  1210.     *unitsptr %= base;
  1211.   }
  1212.   else if (*unitsptr < 0)
  1213.   {
  1214.     --*tensptr;
  1215.     *unitsptr += base;
  1216.     if (*unitsptr < 0)
  1217.     {
  1218.       *tensptr -= 1 + (-*unitsptr) / base;
  1219.       *unitsptr = base - (-*unitsptr) % base;
  1220.     }
  1221.   }
  1222. }
  1223.  
  1224. static int
  1225. tmcomp(const struct tm * const atmp, const struct tm * const btmp)
  1226. {
  1227.   int result;
  1228.  
  1229.   if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
  1230.       (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
  1231.       (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
  1232.       (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
  1233.       (result = (atmp->tm_min - btmp->tm_min)) == 0)
  1234.     result = atmp->tm_sec - btmp->tm_sec;
  1235.   return result;
  1236. }
  1237.  
  1238. static time_t
  1239. time2(struct tm *tmp, void (*const funcp)(const time_t *const,const long,struct tm *), const long offset, int * const okayp)
  1240. {
  1241.   const struct state * sp;
  1242.   int dir;
  1243.   int bits;
  1244.   int i, j ;
  1245.   int saved_seconds;
  1246.   time_t newt;
  1247.   time_t t;
  1248.   struct tm yourtm, mytm;
  1249.  
  1250.   *okayp = FALSE;
  1251.   yourtm = *tmp;
  1252.   if (yourtm.tm_sec >= SECSPERMIN + 2 || yourtm.tm_sec < 0)
  1253.     normalize(&yourtm.tm_min, &yourtm.tm_sec, SECSPERMIN);
  1254.   normalize(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR);
  1255.   normalize(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY);
  1256.   normalize(&yourtm.tm_year, &yourtm.tm_mon, MONSPERYEAR);
  1257.   while (yourtm.tm_mday <= 0)
  1258.   {
  1259.     --yourtm.tm_year;
  1260.     yourtm.tm_mday +=
  1261.       year_lengths[isleap(yourtm.tm_year + TM_YEAR_BASE)];
  1262.   }
  1263.   for ( ; ; )
  1264.   {
  1265.     i = mon_lengths[isleap(yourtm.tm_year +
  1266.                TM_YEAR_BASE)][yourtm.tm_mon];
  1267.     if (yourtm.tm_mday <= i)
  1268.       break;
  1269.     yourtm.tm_mday -= i;
  1270.     if (++yourtm.tm_mon >= MONSPERYEAR)
  1271.     {
  1272.       yourtm.tm_mon = 0;
  1273.       ++yourtm.tm_year;
  1274.     }
  1275.   }
  1276.   saved_seconds = yourtm.tm_sec;
  1277.   yourtm.tm_sec = 0;
  1278.   /*
  1279.    ** Calculate the number of magnitude bits in a time_t
  1280.    ** (this works regardless of whether time_t is
  1281.    ** signed or unsigned, though lint complains if unsigned).
  1282.    */
  1283.   for (bits = 0, t = 1; t > 0; ++bits, t <<= 1)
  1284.     ;
  1285.   /*
  1286.    ** If time_t is signed, then 0 is the median value,
  1287.    ** if time_t is unsigned, then 1 << bits is median.
  1288.    */
  1289.   t = (time_t) 1 << bits;
  1290.   for ( ; ; )
  1291.   {
  1292.     (*funcp)(&t, offset, &mytm);
  1293.     dir = tmcomp(&mytm, &yourtm);
  1294.     if (dir != 0)
  1295.     {
  1296.       if (bits-- < 0)
  1297.     return WRONG;
  1298.       if (bits < 0)
  1299.     --t;
  1300.       else if (dir > 0)
  1301.     t -= (time_t) 1 << bits;
  1302.       else t += (time_t) 1 << bits;
  1303.       continue;
  1304.     }
  1305.     if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
  1306.       break;
  1307.     /*
  1308.      ** Right time, wrong type.
  1309.      ** Hunt for right time, right type.
  1310.      ** It's okay to guess wrong since the guess
  1311.      ** gets checked.
  1312.      */
  1313.     sp = (const struct state *)
  1314.       ((funcp == localsub) ? lclptr : gmtptr);
  1315. #ifdef ALL_STATE
  1316.     if (sp == NULL)
  1317.       return WRONG;
  1318. #endif /* defined ALL_STATE */
  1319.     for (i = 0; i < sp->typecnt; ++i)
  1320.     {
  1321.       if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
  1322.     continue;
  1323.       for (j = 0; j < sp->typecnt; ++j)
  1324.       {
  1325.     if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
  1326.       continue;
  1327.     newt = t + sp->ttis[j].tt_gmtoff -
  1328.       sp->ttis[i].tt_gmtoff;
  1329.     (*funcp)(&newt, offset, &mytm);
  1330.     if (tmcomp(&mytm, &yourtm) != 0)
  1331.       continue;
  1332.     if (mytm.tm_isdst != yourtm.tm_isdst)
  1333.       continue;
  1334.     /*
  1335.      ** We have a match.
  1336.      */
  1337.     t = newt;
  1338.     goto label;
  1339.       }
  1340.     }
  1341.     return WRONG;
  1342.   }
  1343.  label:
  1344.   t += saved_seconds;
  1345.   (*funcp)(&t, offset, tmp);
  1346.   *okayp = TRUE;
  1347.   return t;
  1348. }
  1349.  
  1350. static time_t
  1351. time1(struct tm * const tmp, void (*const funcp)(const time_t * const, const long, struct tm *), const long offset)
  1352. {
  1353.   time_t t;
  1354.   const struct state * sp;
  1355.   int samei, otheri;
  1356.   int okay;
  1357.  
  1358.   if (tmp->tm_isdst > 1)
  1359.     tmp->tm_isdst = 1;
  1360.   t = time2(tmp, funcp, offset, &okay);
  1361.   if (okay || tmp->tm_isdst < 0)
  1362.     return t;
  1363.   /*
  1364.    ** We're supposed to assume that somebody took a time of one type
  1365.    ** and did some math on it that yielded a "struct tm" that's bad.
  1366.    ** We try to divine the type they started from and adjust to the
  1367.    ** type they need.
  1368.    */
  1369.   sp = (const struct state *) ((funcp == localsub) ? lclptr : gmtptr);
  1370. #ifdef ALL_STATE
  1371.   if (sp == NULL)
  1372.     return WRONG;
  1373. #endif /* defined ALL_STATE */
  1374.   for (samei = 0; samei < sp->typecnt; ++samei)
  1375.   {
  1376.     if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
  1377.       continue;
  1378.     for (otheri = 0; otheri < sp->typecnt; ++otheri)
  1379.     {
  1380.       if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
  1381.     continue;
  1382.       tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
  1383.     sp->ttis[samei].tt_gmtoff;
  1384.       tmp->tm_isdst = !tmp->tm_isdst;
  1385.       t = time2(tmp, funcp, offset, &okay);
  1386.       if (okay)
  1387.     return t;
  1388.       tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
  1389.     sp->ttis[samei].tt_gmtoff;
  1390.       tmp->tm_isdst = !tmp->tm_isdst;
  1391.     }
  1392.   }
  1393.   return WRONG;
  1394. }
  1395.  
  1396. time_t
  1397. mktime(struct tm * tmp)
  1398. {
  1399.   return time1(tmp, localsub, 0L);
  1400. }
  1401.