home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume4 / settz / settz.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  4.0 KB  |  197 lines

  1. #
  2.  
  3. /*LINTLIBRARY*/
  4.  
  5. /*
  6. ** Should there be any built-in rules other than GMT?
  7. ** In particular, should zones such as "EST5" (abbreviation is always "EST",
  8. ** GMT offset is always 5 hours) be built in?
  9. */
  10.  
  11. #include "tzfile.h"
  12. #include "time.h"
  13.  
  14. #ifdef OBJECTID
  15. static char    sccsid[] = "@(#)settz.c    2.1";
  16. #endif
  17.  
  18. #ifndef TRUE
  19. #define TRUE        1
  20. #define FALSE        0
  21. #endif
  22.  
  23. #ifndef MAXPATHLEN
  24. #define MAXPATHLEN    1024
  25. #endif
  26.  
  27. extern char *        asctime();
  28. extern struct tm *    gmtime();
  29. extern char *        strcpy();
  30. extern char *        strcat();
  31. extern char *        getenv();
  32.  
  33. static struct tzhead    h;
  34. static long        ats[TZ_MAX_TIMES];
  35. static unsigned char    types[TZ_MAX_TIMES];
  36. static struct ttinfo    ttis[TZ_MAX_TYPES];
  37. static char        chars[TZ_MAX_CHARS + 1];
  38.  
  39. #define TZ_MAX_TOTAL    (sizeof h + sizeof ats + sizeof types + \
  40.                 sizeof ttis + sizeof chars)
  41.  
  42. static char        isset;
  43.  
  44. char *            tz_abbr;    /* set by localtime; available to all */
  45.  
  46. /*
  47. ** Not available west of the Rockies. . .
  48. */
  49.  
  50. static char *
  51. memcpy(to, from, n)
  52. char *    to;
  53. char *    from;
  54. {
  55.     register int    i;
  56.  
  57.     for (i = 0; i < n; ++i)
  58.         to[i] = from[i];
  59.     return to;
  60. }
  61.  
  62. static
  63. tzload(tzname)
  64. register char *    tzname;
  65. {
  66.     register char *    p;
  67.     register int    fid;
  68.     register int    i;
  69.     register int    doaccess;
  70.     char        buf[(TZ_MAX_TOTAL>MAXPATHLEN)?TZ_MAX_TOTAL:MAXPATHLEN];
  71.  
  72.     if (tzname == 0 && (tzname = TZDEFAULT) == 0)
  73.         return -1;
  74.     doaccess = tzname[0] == '/';
  75.     if (!doaccess) {
  76.         if ((p = TZDIR) == 0)
  77.             return -1;
  78.         if ((strlen(p) + strlen(tzname) + 1) >= sizeof buf)
  79.             return -1;
  80.         (void) strcpy(buf, p);
  81.         (void) strcat(buf, "/");
  82.         (void) strcat(buf, tzname);
  83.         /*
  84.         ** Set doaccess if '.' (as in "../") shows up in name.
  85.         */
  86.         while (*tzname != '\0')
  87.             if (*tzname++ == '.')
  88.                 doaccess = TRUE;
  89.         tzname = buf;
  90.     }
  91.     if (doaccess && access(tzname, 4) != 0)
  92.         return -1;
  93.     if ((fid = open(tzname, 0)) == -1)
  94.         return -1;
  95.     p = buf;
  96.     i = read(fid, p, sizeof buf);
  97.     if (close(fid) != 0 || i < sizeof h)
  98.         return -1;
  99.     (void) memcpy((char *) &h, p, sizeof h);
  100.     if (h.tzh_timecnt > TZ_MAX_TIMES ||
  101.         h.tzh_typecnt == 0 || h.tzh_typecnt > TZ_MAX_TYPES ||
  102.         h.tzh_charcnt > TZ_MAX_CHARS)
  103.             return -1;
  104.     if (i < sizeof h +
  105.         h.tzh_timecnt * (sizeof ats[0] + sizeof types[0]) +
  106.         h.tzh_typecnt * sizeof ttis[0] +
  107.         h.tzh_charcnt * sizeof chars[0])
  108.             return -1;
  109.     p += sizeof h;
  110.     if ((i = h.tzh_timecnt) > 0) {
  111.         (void) memcpy((char *) ats, p, i * sizeof ats[0]);
  112.         p += i * sizeof ats[0];
  113.         (void) memcpy((char *) types, p, i * sizeof types[0]);
  114.         p += i * sizeof types[0];
  115.     }
  116.     if ((i = h.tzh_typecnt) > 0) {
  117.         (void) memcpy((char *) ttis, p, i * sizeof ttis[0]);
  118.         p += i * sizeof ttis[0];
  119.     }
  120.     if ((i = h.tzh_charcnt) > 0)
  121.         (void) memcpy((char *) chars, p, i * sizeof chars[0]);
  122.     chars[h.tzh_charcnt] = '\0';    /* ensure '\0'-termination */
  123.     for (i = 0; i < h.tzh_timecnt; ++i)
  124.         if (types[i] >= h.tzh_typecnt)
  125.             return -1;
  126.     for (i = 0; i < h.tzh_typecnt; ++i)
  127.         if (ttis[i].tt_abbrind >= h.tzh_charcnt)
  128.             return -1;
  129.     return 0;
  130. }
  131.  
  132. /*
  133. ** settz("")        Use built-in GMT.
  134. ** settz((char *) 0)    Use TZDEFAULT.
  135. ** settz(otherwise)    Use otherwise.
  136. */
  137.  
  138. settz(tzname)
  139. char *    tzname;
  140. {
  141.     register int    answer;
  142.  
  143.     isset = TRUE;
  144.     if (tzname != 0 && *tzname == '\0')
  145.         answer = 0;            /* Use built-in GMT */
  146.     else {
  147.         if (tzload(tzname) == 0)
  148.             return 0;
  149.         /*
  150.         ** If we want to try for local time on errors. . .
  151.         if (tzload((char *) 0) == 0)
  152.             return -1;
  153.         */
  154.         answer = -1;
  155.     }
  156.     h.tzh_timecnt = 0;
  157.     ttis[0].tt_gmtoff = 0;
  158.     ttis[0].tt_abbrind = 0;
  159.     (void) strcpy(chars, "GMT");
  160.     return answer;
  161. }
  162.  
  163. struct tm *
  164. newlocaltime(timep)
  165. long *    timep;
  166. {
  167.     register struct ttinfo *    ttip;
  168.     register struct tm *        tmp;
  169.     register int            i;
  170.     long                t;
  171.  
  172.     t = *timep;
  173.     if (!isset)
  174.         (void) settz(getenv("TZ"));
  175.     if (h.tzh_timecnt == 0 || t < ats[0])
  176.         i = 0;
  177.     else {
  178.         for (i = 1; i < h.tzh_timecnt; ++i)
  179.             if (t < ats[i])
  180.                 break;
  181.         i = types[i - 1];
  182.     }
  183.     ttip = &ttis[i];
  184.     t += ttip->tt_gmtoff;
  185.     tmp = gmtime(&t);
  186.     tmp->tm_isdst = ttip->tt_isdst;
  187.     tz_abbr = &chars[ttip->tt_abbrind];
  188.     return tmp;
  189. }
  190.  
  191. char *
  192. newctime(timep)
  193. long *    timep;
  194. {
  195.     return asctime(newlocaltime(timep));
  196. }
  197.