summary refs log tree commit diff
path: root/time/tzset.c
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>1997-07-14 21:49:20 +0000
committerUlrich Drepper <drepper@redhat.com>1997-07-14 21:49:20 +0000
commitcd6ede759fadcf8cd1f8c069928611d18cef5c1a (patch)
tree9d4bbf1daaf37c365dfa5234d1caf2b6b9547708 /time/tzset.c
parentf21acc89c06c14160eab88246e9dbe0b17eb5f89 (diff)
downloadglibc-cd6ede759fadcf8cd1f8c069928611d18cef5c1a.tar.gz
glibc-cd6ede759fadcf8cd1f8c069928611d18cef5c1a.tar.xz
glibc-cd6ede759fadcf8cd1f8c069928611d18cef5c1a.zip
1997-07-14 23:37  Ulrich Drepper  <drepper@cygnus.com>

	* inet/getnameinfo.c: Pretty print.
	Correctly enlarge buffers.

	* login/programs/utmpd.c: Use _() instead of gettext().

	* nis/nss_nisplus/nisplus-hosts.c: Optimize some uses of stpcpy away.
	* nis/nss_nisplus/nisplus-network.c: Likewise.
	* nis/nss_nisplus/nisplus-proto.c: Likewise.
	* nis/nss_nisplus/nisplus-rpc.c: Likewise.
	* nis/nss_nisplus/nisplus-service.c: Likewise.

	* sysdeps/alpha/fpu/bits/mathinline.h: Only define functions if
	__OPTIMIZE__.
	* sysdeps/powerpc/bits/mathinline.h: Likewise.
	* sysdeps/i386/fpu/bits/mathinline.h: Define ISO C9x comparison
	function always.
	* sysdeps/m68k/fpu/bits/mathinline.h: Likewise.
	* sysdeps/stub/bits/mathinline.h: Add conditionals to show how
	it should look like in real files.

	* sysdeps/generic/bits/select.h (__FD_ZERO): Don't use memset to
	prevent prototype trouble, use simple loop.
	* sysdeps/i386/bits/select.h [!__GNUC__] (__FD_ZERO): Likewise.

	* sysdeps/mips/mips64/Implies: Imply ieee754.

	* sysdeps/unix/sysv/linux/Makefile: Make sure bits/syscall.h is
	installed.
	* sysdeps/unix/sysv/linux/sys/syscal.h: Pretty print.

1997-07-14 00:25  Ulrich Drepper  <drepper@cygnus.com>

	* sysdeps/stub/bits/stdio_lim.h: Unify with standalone version.
	* sysdeps/standalone/bits/stdio_lim.h: Removed.
	Patch by Zack Weinberg <zack@rabi.phys.columbia.edu>.

1997-06-22  Paul Eggert  <eggert@twinsun.com>

	* time/strftime.c (strftime): Use tm_zone if available, even if _LIBC.

	* time/tzfile.c (__tzstring): New decl.
	(__tzfile_read, __tzfile_default): Set __tzname to permanent strings.
	(__tzfile_default): First two args are now const char *.

	* time/tzset.c (__tzstring): New function.
	(tz_rule): Name is now const char *.
	(struct tzstring_head): New type.
	(tzstring_list, tzstring_last_buffer_size): New static vars.
	(__tzset_internal): Time zone names are now permanent, not temporary.
Diffstat (limited to 'time/tzset.c')
-rw-r--r--time/tzset.c134
1 files changed, 87 insertions, 47 deletions
diff --git a/time/tzset.c b/time/tzset.c
index 9eceb73cf5..d1c2c091a5 100644
--- a/time/tzset.c
+++ b/time/tzset.c
@@ -31,8 +31,9 @@ extern const unsigned short int __mon_yday[2][13];
 
 extern int __use_tzfile;
 extern void __tzfile_read __P ((const char *file));
-extern void __tzfile_default __P ((char *std, char *dst,
+extern void __tzfile_default __P ((const char *std, const char *dst,
 				   long int stdoff, long int dstoff));
+extern const char * __tzstring __P ((const char *string));
 extern int __tz_compute __P ((time_t timer, const struct tm *tm));
 
 char *__tzname[2] = { (char *) "GMT", (char *) "GMT" };
@@ -53,7 +54,7 @@ weak_alias (__timezone, timezone)
    timezone given in the POSIX standard TZ envariable.  */
 typedef struct
   {
-    char *name;
+    const char *name;
 
     /* When to change.  */
     enum { J0, J1, M } type;	/* Interpretation of:  */
@@ -74,6 +75,68 @@ static tz_rule tz_rules[2];
 
 static int compute_change __P ((tz_rule *rule, int year));
 
+/* Header for a list of buffers containing time zone strings.  */
+struct tzstring_head 
+{
+  struct tzstring_head *next;
+  /* The buffer itself immediately follows the header.
+     The buffer contains zero or more (possibly overlapping) strings.
+     The last string is followed by 2 '\0's instead of the usual 1.  */
+};
+
+/* First in a list of buffers containing time zone strings.
+   All the buffers but the last are read-only.  */
+static struct
+{
+  struct tzstring_head head;
+  char data[48];
+} tzstring_list;
+
+/* Size of the last buffer in the list, not counting its header.  */
+static size_t tzstring_last_buffer_size = sizeof tzstring_list.data;
+
+/* Allocate a time zone string with given contents.
+   The string will never be moved or deallocated.
+   However, its contents may be shared with other such strings.  */
+const char *
+__tzstring (string)
+     const char *string;
+{
+  struct tzstring_head *h = &tzstring_list.head;
+  size_t needed;
+  char *p;
+
+  /* Look through time zone string list for a duplicate of this one.  */
+  for (h = &tzstring_list.head;  ;  h = h->next)
+    {
+      for (p = (char *) (h + 1);  p[0] | p[1];  p++)
+	if (strcmp (p, string) == 0)
+	  return p;
+      if (! h->next)
+	break;
+    }
+
+  /* No duplicate was found.  Copy to the end of this buffer if there's room;
+     otherwise, append a large-enough new buffer to the list and use it.  */
+  p++;
+  needed = strlen (string) + 2; /* Need 2 trailing '\0's after last string.  */
+
+  if ((size_t) ((char *) (h + 1) + tzstring_last_buffer_size - p) < needed)
+    {
+      size_t buffer_size = tzstring_last_buffer_size;
+      while ((buffer_size *= 2) < needed)
+	continue;
+      if (! (h = h->next = malloc (sizeof *h + buffer_size)))
+	return NULL;
+      h->next = NULL;
+      tzstring_last_buffer_size = buffer_size;
+      p = (char *) (h + 1);
+    }
+
+  strncpy (p, string, needed);
+  return p;
+}
+
 static char *old_tz = NULL;
 
 /* Interpret the TZ envariable.  */
@@ -85,6 +148,7 @@ __tzset_internal (always)
   static int is_initialized = 0;
   register const char *tz;
   register size_t l;
+  char *tzbuf;
   unsigned short int hh, mm, ss;
   unsigned short int whichrule;
 
@@ -112,12 +176,6 @@ __tzset_internal (always)
     /* No change, simply return.  */
     return;
 
-  /* Free old storage.  */
-  if (tz_rules[0].name != NULL && *tz_rules[0].name != '\0')
-    free ((void *) tz_rules[0].name);
-  if (tz_rules[1].name != NULL && *tz_rules[1].name != '\0' &&
-      tz_rules[1].name != tz_rules[0].name)
-    free ((void *) tz_rules[1].name);
   tz_rules[0].name = NULL;
   tz_rules[1].name = NULL;
 
@@ -135,16 +193,7 @@ __tzset_internal (always)
 
   if (tz == NULL || *tz == '\0')
     {
-      static const char UTC[] = "UTC";
-      size_t len = sizeof UTC;
-      tz_rules[0].name = (char *) malloc (len);
-      if (tz_rules[0].name == NULL)
-	return;
-      tz_rules[1].name = (char *) malloc (len);
-      if (tz_rules[1].name == NULL)
-	return;
-      memcpy ((void *) tz_rules[0].name, UTC, len);
-      memcpy ((void *) tz_rules[1].name, UTC, len);
+      tz_rules[0].name = tz_rules[1].name = "UTC";
       tz_rules[0].type = tz_rules[1].type = J0;
       tz_rules[0].m = tz_rules[0].n = tz_rules[0].d = 0;
       tz_rules[1].m = tz_rules[1].n = tz_rules[1].d = 0;
@@ -157,11 +206,11 @@ __tzset_internal (always)
 
   /* Clear out old state and reset to unnamed UTC.  */
   memset (tz_rules, 0, sizeof tz_rules);
-  tz_rules[0].name = tz_rules[1].name = (char *) "";
+  tz_rules[0].name = tz_rules[1].name = "";
 
   /* Get the standard timezone name.  */
-  tz_rules[0].name = (char *) malloc (strlen (tz) + 1);
-  if (tz_rules[0].name == NULL)
+  tzbuf = malloc (strlen (tz) + 1);
+  if (! tzbuf)
     {
       /* Clear the old tz name so we will try again.  */
       free (old_tz);
@@ -169,25 +218,23 @@ __tzset_internal (always)
       return;
     }
 
-  if (sscanf (tz, "%[^0-9,+-]", tz_rules[0].name) != 1 ||
-      (l = strlen(tz_rules[0].name)) < 3)
+  if (sscanf (tz, "%[^0-9,+-]", tzbuf) != 1 ||
+      (l = strlen (tzbuf)) < 3)
     {
-      free (tz_rules[0].name);
-      tz_rules[0].name = (char *) "";
+      free (tzbuf);
       return;
     }
 
-  {
-    char *n = realloc ((void *) tz_rules[0].name, l + 1);
-    if (n != NULL)
-      tz_rules[0].name = n;
-  }
+  tz_rules[0].name = __tzstring (tzbuf);
 
   tz += l;
 
   /* Figure out the standard offset from UTC.  */
   if (*tz == '\0' || (*tz != '+' && *tz != '-' && !isdigit (*tz)))
-    return;
+    {
+      free (tzbuf);
+      return;
+    }
 
   if (*tz == '-' || *tz == '+')
     tz_rules[0].offset = *tz++ == '-' ? 1L : -1L;
@@ -196,6 +243,7 @@ __tzset_internal (always)
   switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
     {
     default:
+      free (tzbuf);
       return;
     case 1:
       mm = 0;
@@ -218,23 +266,14 @@ __tzset_internal (always)
   /* Get the DST timezone name (if any).  */
   if (*tz != '\0')
     {
-      char *n = malloc (strlen (tz) + 1);
-      if (n != NULL)
-	{
-	  tz_rules[1].name = n;
-	  if (sscanf (tz, "%[^0-9,+-]", tz_rules[1].name) != 1 ||
-	      (l = strlen (tz_rules[1].name)) < 3)
-	    {
-	      free (n);
-	      tz_rules[1].name = (char *) "";
-	      goto done_names;	/* Punt on name, set up the offsets.  */
-	    }
-	  n = realloc ((void *) tz_rules[1].name, l + 1);
-	  if (n != NULL)
-	    tz_rules[1].name = n;
+      char *n = tzbuf + strlen (tzbuf) + 1;
+      if (sscanf (tz, "%[^0-9,+-]", n) != 1 ||
+	  (l = strlen (n)) < 3)
+	goto done_names;	/* Punt on name, set up the offsets.  */
 
-	  tz += l;
-	}
+      tz_rules[1].name = __tzstring (n);
+
+      tz += l;
 
       /* Figure out the DST offset from GMT.  */
       if (*tz == '-' || *tz == '+')
@@ -271,6 +310,7 @@ __tzset_internal (always)
     tz_rules[1].name = tz_rules[0].name;
 
  done_names:
+  free (tzbuf);
 
   if (*tz == '\0' || (tz[0] == ',' && tz[1] == '\0'))
     {