Update.
[platform/upstream/glibc.git] / time / tzfile.c
index 4b891b4..a7753d2 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991, 92, 93, 95, 96, 97 Free Software Foundation, Inc.
+/* Copyright (C) 1991, 92, 93, 95, 96, 97, 98 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -24,7 +24,7 @@
 #include <unistd.h>
 
 #define        NOID
-#include <tzfile.h>
+#include <timezone/tzfile.h>
 
 int __use_tzfile = 0;
 
@@ -54,10 +54,13 @@ static unsigned char *type_idxs = NULL;
 static size_t num_types;
 static struct ttinfo *types = NULL;
 static char *zone_names = NULL;
+static long int rule_stdoff;
+static long int rule_dstoff;
 static size_t num_leaps;
 static struct leap *leaps = NULL;
 
 #include <endian.h>
+#include <byteswap.h>
 
 /* Decode the four bytes at PTR as a signed integer in network byte order.  */
 static inline int
@@ -65,6 +68,8 @@ decode (const void *ptr)
 {
   if ((BYTE_ORDER == BIG_ENDIAN) && sizeof (int) == 4)
     return *(const int *) ptr;
+  else if (BYTE_ORDER == LITTLE_ENDIAN && sizeof (int) == 4)
+    return bswap_32 (*(const int *) ptr);
   else
     {
       const unsigned char *p = ptr;
@@ -88,7 +93,6 @@ __tzfile_read (const char *file)
   struct tzhead tzhead;
   size_t chars;
   register size_t i;
-  struct ttinfo *info;
 
   __use_tzfile = 0;
 
@@ -134,7 +138,7 @@ __tzfile_read (const char *file)
     {
       const char *tzdir;
       unsigned int len, tzdir_len;
-      char *new;
+      char *new, *tmp;
 
       tzdir = __secure_getenv ("TZDIR");
       if (tzdir == NULL || *tzdir == '\0')
@@ -146,9 +150,9 @@ __tzfile_read (const char *file)
        tzdir_len = strlen (tzdir);
       len = strlen (file) + 1;
       new = (char *) __alloca (tzdir_len + 1 + len);
-      memcpy (new, tzdir, tzdir_len);
-      new[tzdir_len] = '/';
-      memcpy (&new[tzdir_len + 1], file, len);
+      tmp = __mempcpy (new, tzdir, tzdir_len);
+      *tmp++ = '/';
+      __mempcpy (tmp, file, len);
       file = new;
     }
 
@@ -156,7 +160,7 @@ __tzfile_read (const char *file)
   if (f == NULL)
     return;
 
-  if (fread ((void *) &tzhead, sizeof (tzhead), 1, f) != 1)
+  if (fread_unlocked ((void *) &tzhead, sizeof (tzhead), 1, f) != 1)
     goto lose;
 
   num_transitions = (size_t) decode (tzhead.tzh_timecnt);
@@ -197,8 +201,8 @@ __tzfile_read (const char *file)
   if (sizeof (time_t) < 4)
       abort ();
 
-  if (fread(transitions, 4, num_transitions, f) != num_transitions ||
-      fread(type_idxs, 1, num_transitions, f) != num_transitions)
+  if (fread_unlocked (transitions, 4, num_transitions, f) != num_transitions
+      || fread_unlocked (type_idxs, 1, num_transitions, f) != num_transitions)
     goto lose;
 
   /* Check for bogus indices in the data file, so we can hereafter
@@ -221,32 +225,32 @@ __tzfile_read (const char *file)
   for (i = 0; i < num_types; ++i)
     {
       unsigned char x[4];
-      if (fread (x, 1, 4, f) != 4 ||
-         fread (&types[i].isdst, 1, 1, f) != 1 ||
-         fread (&types[i].idx, 1, 1, f) != 1)
+      if (fread_unlocked (x, 1, 4, f) != 4
+         || fread_unlocked (&types[i].isdst, 1, 1, f) != 1
+         || fread_unlocked (&types[i].idx, 1, 1, f) != 1)
        goto lose;
       if (types[i].idx >= chars) /* Bogus index in data file.  */
        goto lose;
       types[i].offset = (long int) decode (x);
     }
 
-  if (fread (zone_names, 1, chars, f) != chars)
+  if (fread_unlocked (zone_names, 1, chars, f) != chars)
     goto lose;
 
   for (i = 0; i < num_leaps; ++i)
     {
       unsigned char x[4];
-      if (fread (x, 1, sizeof (x), f) != sizeof (x))
+      if (fread_unlocked (x, 1, sizeof (x), f) != sizeof (x))
        goto lose;
       leaps[i].transition = (time_t) decode (x);
-      if (fread (x, 1, sizeof (x), f) != sizeof (x))
+      if (fread_unlocked (x, 1, sizeof (x), f) != sizeof (x))
        goto lose;
       leaps[i].change = (long int) decode (x);
     }
 
   for (i = 0; i < num_isstd; ++i)
     {
-      int c = getc (f);
+      int c = getc_unlocked (f);
       if (c == EOF)
        goto lose;
       types[i].isstd = c != 0;
@@ -256,7 +260,7 @@ __tzfile_read (const char *file)
 
   for (i = 0; i < num_isgmt; ++i)
     {
-      int c = getc (f);
+      int c = getc_unlocked (f);
       if (c == EOF)
        goto lose;
       types[i].isgmt = c != 0;
@@ -266,20 +270,42 @@ __tzfile_read (const char *file)
 
   fclose (f);
 
-  info = find_transition (0);
-  for (i = 0; i < num_types && i < sizeof (__tzname) / sizeof (__tzname[0]);
-       ++i)
+  /* Find the standard and daylight time offsets used by the rule file.
+     We choose the offsets in the types of each flavor that are
+     transitioned to earliest in time.  */
+  __tzname[1] = NULL;
+  for (i = 0; i < num_types; ++i)
     __tzname[types[i].isdst] = __tzstring (&zone_names[types[i].idx]);
-  if (info->isdst < sizeof (__tzname) / sizeof (__tzname[0]))
-    __tzname[info->isdst] = __tzstring (&zone_names[info->idx]);
+  if (__tzname[1] == NULL)
+    __tzname[1] = __tzname[0];
 
   compute_tzname_max (chars);
 
+  if (num_transitions == 0)
+    /* Use the first rule (which should also be the only one.  */
+    rule_stdoff = rule_dstoff = types[0].offset;
+  else
+    {
+      rule_stdoff = rule_dstoff = 0;
+      for (i = 0; i < num_transitions; ++i)
+       {
+         if (!rule_stdoff && !types[type_idxs[i]].isdst)
+           rule_stdoff = types[type_idxs[i]].offset;
+         if (!rule_dstoff && types[type_idxs[i]].isdst)
+           rule_dstoff = types[type_idxs[i]].offset;
+         if (rule_stdoff && rule_dstoff)
+           break;
+       }
+    }
+
+  __daylight = rule_stdoff != rule_dstoff;
+  __timezone = -rule_stdoff;
+
   __use_tzfile = 1;
   return;
 
- lose:;
-  fclose(f);
+ lose:
+  fclose (f);
 }
 \f
 /* The user specified a hand-made timezone, but not its DST rules.
@@ -291,7 +317,6 @@ __tzfile_default (const char *std, const char *dst,
                  long int stdoff, long int dstoff)
 {
   size_t stdlen, dstlen, i;
-  long int rule_offset, rule_stdoff, rule_dstoff;
   int isdst;
 
   __tzfile_read (TZDEFRULES);
@@ -316,27 +341,11 @@ __tzfile_default (const char *std, const char *dst,
       __use_tzfile = 0;
       return;
     }
-  memcpy (zone_names, std, stdlen);
-  memcpy (&zone_names[stdlen], dst, dstlen);
-
-  /* Find the standard and daylight time offsets used by the rule file.
-     We choose the offsets in the types of each flavor that are
-     transitioned to earliest in time.  */
-  rule_stdoff = rule_dstoff = 0;
-  for (i = 0; i < num_transitions; ++i)
-    {
-      if (!rule_stdoff && !types[type_idxs[i]].isdst)
-       rule_stdoff = types[type_idxs[i]].offset;
-      if (!rule_dstoff && types[type_idxs[i]].isdst)
-       rule_dstoff = types[type_idxs[i]].offset;
-      if (rule_stdoff && rule_dstoff)
-       break;
-    }
+  __mempcpy (__mempcpy (zone_names, std, stdlen), dst, dstlen);
 
   /* Now correct the transition times for the user-specified standard and
      daylight offsets from GMT.  */
   isdst = 0;
-  rule_offset = rule_offset;
   for (i = 0; i < num_transitions; ++i)
     {
       struct ttinfo *trans_type = &types[type_idxs[i]];
@@ -378,6 +387,9 @@ __tzfile_default (const char *std, const char *dst,
   __tzname[0] = (char *) std;
   __tzname[1] = (char *) dst;
 
+  /* Set the timezone.  */
+  __timezone = -types[0].offset;
+
   compute_tzname_max (stdlen + dstlen);
 }
 \f
@@ -413,21 +425,25 @@ find_transition (time_t timer)
 \f
 int
 __tzfile_compute (time_t timer, int use_localtime,
-                 long int *leap_correct, int *leap_hit)
+                 long int *leap_correct, int *leap_hit,
+                 struct tm *tp)
 {
   register size_t i;
 
   if (use_localtime)
     {
       struct ttinfo *info = find_transition (timer);
-      __daylight = info->isdst;
-      __timezone = info->offset;
-      for (i = 0;
-          i < num_types && i < sizeof (__tzname) / sizeof (__tzname[0]);
-          ++i)
-       __tzname[types[i].isdst] = &zone_names[types[i].idx];
-      if (info->isdst < sizeof (__tzname) / sizeof (__tzname[0]))
-       __tzname[info->isdst] = &zone_names[info->idx];
+      __daylight = rule_stdoff != rule_dstoff;
+      __timezone = -rule_stdoff;
+      __tzname[1] = NULL;
+      for (i = 0; i < num_types; ++i)
+       __tzname[types[i].isdst] = __tzstring (&zone_names[types[i].idx]);
+      if (__tzname[1] == NULL)
+       /* There is no daylight saving time.  */
+       __tzname[1] = __tzname[0];
+      tp->tm_isdst = info->isdst;
+      tp->tm_zone = &zone_names[info->idx];
+      tp->tm_gmtoff = info->offset;
     }
 
   *leap_correct = 0L;
@@ -448,9 +464,9 @@ __tzfile_compute (time_t timer, int use_localtime,
        leaps[i].change > leaps[i - 1].change))
     {
       *leap_hit = 1;
-      while (i > 0 &&
-            leaps[i].transition == leaps[i - 1].transition + 1 &&
-            leaps[i].change == leaps[i - 1].change + 1)
+      while (i > 0
+            && leaps[i].transition == leaps[i - 1].transition + 1
+            && leaps[i].change == leaps[i - 1].change + 1)
        {
          ++*leap_hit;
          --i;
@@ -476,5 +492,6 @@ compute_tzname_max (size_t chars)
        ++p;
       if ((size_t) (p - start) > __tzname_cur_max)
        __tzname_cur_max = p - start;
-    } while (++p < &zone_names[chars]);
+    }
+  while (++p < &zone_names[chars]);
 }