Update.
[platform/upstream/glibc.git] / time / tzfile.c
index 2d0752c..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;
 
@@ -43,8 +43,10 @@ struct leap
     long int change;           /* Seconds of correction to apply.  */
   };
 
-static struct ttinfo *find_transition (time_t timer);
-static void compute_tzname_max (size_t);
+extern char * __tzstring (const char *); /* Defined in tzset.c.  */
+
+static struct ttinfo *find_transition (time_t timer) internal_function;
+static void compute_tzname_max (size_t) internal_function;
 
 static size_t num_transitions;
 static time_t *transitions = NULL;
@@ -52,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
@@ -63,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;
@@ -86,7 +93,6 @@ __tzfile_read (const char *file)
   struct tzhead tzhead;
   size_t chars;
   register size_t i;
-  struct ttinfo *info;
 
   __use_tzfile = 0;
 
@@ -110,8 +116,8 @@ __tzfile_read (const char *file)
     /* No user specification; use the site-wide default.  */
     file = TZDEFAULT;
   else if (*file == '\0')
-    /* User specified the empty string; use UTC explicitly.  */
-    file = "Universal";
+    /* User specified the empty string; use UTC with no leap seconds.  */
+    return;
   else
     {
       /* We must not allow to read an arbitrary file in a setuid
@@ -132,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')
@@ -144,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;
     }
 
@@ -154,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);
@@ -195,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
@@ -219,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;
@@ -254,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;
@@ -264,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)
-    __tzname[types[i].isdst] = &zone_names[types[i].idx];
-  if (info->isdst < sizeof (__tzname) / sizeof (__tzname[0]))
-    __tzname[info->isdst] = &zone_names[info->idx];
+  /* 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 (__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.
@@ -285,10 +313,10 @@ __tzfile_read (const char *file)
    from the TZDEFRULES file.  */
 
 void
-__tzfile_default (char *std, char *dst, long int stdoff, long int dstoff)
+__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);
@@ -313,27 +341,11 @@ __tzfile_default (char *std, char *dst, long int stdoff, long int dstoff)
       __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]];
@@ -372,13 +384,17 @@ __tzfile_default (char *std, char *dst, long int stdoff, long int dstoff)
   types[1].isdst = 1;
 
   /* Reset the zone names to point to the user's names.  */
-  __tzname[0] = &zone_names[0];
-  __tzname[1] = &zone_names[stdlen];
+  __tzname[0] = (char *) std;
+  __tzname[1] = (char *) dst;
+
+  /* Set the timezone.  */
+  __timezone = -types[0].offset;
 
   compute_tzname_max (stdlen + dstlen);
 }
 \f
 static struct ttinfo *
+internal_function
 find_transition (time_t timer)
 {
   size_t i;
@@ -408,19 +424,27 @@ find_transition (time_t timer)
 }
 \f
 int
-__tzfile_compute (time_t timer, long int *leap_correct, int *leap_hit)
+__tzfile_compute (time_t timer, int use_localtime,
+                 long int *leap_correct, int *leap_hit,
+                 struct tm *tp)
 {
-  struct ttinfo *info;
   register size_t i;
 
-  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];
+  if (use_localtime)
+    {
+      struct ttinfo *info = find_transition (timer);
+      __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;
   *leap_hit = 0;
@@ -440,9 +464,9 @@ __tzfile_compute (time_t timer, long int *leap_correct, int *leap_hit)
        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;
@@ -452,10 +476,11 @@ __tzfile_compute (time_t timer, long int *leap_correct, int *leap_hit)
   return 1;
 }
 \f
-void
+static void
+internal_function
 compute_tzname_max (size_t chars)
 {
-  extern size_t __tzname_cur_max; /* Defined in __tzset.c. */
+  extern size_t __tzname_cur_max; /* Defined in tzset.c. */
 
   const char *p;
 
@@ -467,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]);
 }