changeset 71:5177290a8425

GNU shell utilities
author Jim Meyering <jim@meyering.net>
date Wed, 08 Sep 1993 17:47:09 +0000
parents b897a21e7492
children 361372d725f6
files lib/basename.c lib/getdate.y lib/getugroups.c lib/getusershell.c lib/mktime.c lib/posixtm.y lib/putenv.c lib/strftime.c lib/strtod.c
diffstat 9 files changed, 367 insertions(+), 276 deletions(-) [+]
line wrap: on
line diff
--- a/lib/basename.c
+++ b/lib/basename.c
@@ -15,9 +15,11 @@
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
-#if defined(USG) || defined(STDC_HEADERS)
+#if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
 #include <string.h>
+#ifndef rindex
 #define rindex strrchr
+#endif
 #else
 #include <strings.h>
 #endif
--- a/lib/getdate.y
+++ b/lib/getdate.y
@@ -1,23 +1,37 @@
 %{
-/* $Revision: 2.1 $
-**
+/*
 **  Originally written by Steven M. Bellovin <smb@research.att.com> while
 **  at the University of North Carolina at Chapel Hill.  Later tweaked by
 **  a couple of people on Usenet.  Completely overhauled by Rich $alz
 **  <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990;
 **  send any email to Rich.
 **
-**  This grammar has eight shift/reduce conflicts.
+**  This grammar has nine shift/reduce conflicts.
 **
 **  This code is in the public domain and has no copyright.
 */
 /* SUPPRESS 287 on yaccpar_sccsid *//* Unusd static variable */
 /* SUPPRESS 288 on yyerrlab *//* Label unused */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+/* Since the code of getdate.y is not included in the Emacs executable
+   itself, there is no need to #define static in this file.  Even if
+   the code were included in the Emacs executable, it probably
+   wouldn't do any harm to #undef it here; this will only cause
+   problems if we try to write to a static variable, which I don't
+   think this code needs to do.  */
+#ifdef emacs
+#undef static
+#endif
+
 #ifdef __GNUC__
+#undef alloca
 #define alloca __builtin_alloca
 #else
-#ifdef sparc
+#ifdef HAVE_ALLOCA_H
 #include <alloca.h>
 #else
 #ifdef _AIX /* for Bison */
@@ -36,10 +50,6 @@
    tricks are need, but defaults to using the gettimeofday system call.
    Include <sys/time.h> if that will be used.  */
 
-#if !defined (USG) && !defined (sgi) && !defined (__386BSD__)
-#include <sys/time.h>
-#endif
-
 #if	defined(vms)
 
 #include <types.h>
@@ -49,32 +59,36 @@
 
 #include <sys/types.h>
 
-#if	defined(USG) || !defined(HAVE_FTIME)
+#ifdef TIME_WITH_SYS_TIME
+#include <sys/time.h>
+#include <time.h>
+#else
+#ifdef HAVE_SYS_TIME_H
+#include <sys/time.h>
+#else
+#include <time.h>
+#endif
+#endif
+
+#ifdef timezone
+#undef timezone /* needed for sgi */
+#endif
+
+#if defined(HAVE_SYS_TIMEB_H) || (!defined(USG) && defined(HAVE_FTIME))
+#include <sys/timeb.h>
+#else
 /*
-**  If you need to do a tzset() call to set the
-**  timezone, and don't have ftime().
+** We use the obsolete `struct timeb' as part of our interface!
+** Since the system doesn't have it, we define it here;
+** our callers must do likewise.
 */
 struct timeb {
     time_t		time;		/* Seconds since the epoch	*/
     unsigned short	millitm;	/* Field not used		*/
-    short		timezone;
+    short		timezone;	/* Minutes west of GMT		*/
     short		dstflag;	/* Field not used		*/
 };
-
-#else
-
-#include <sys/timeb.h>
-
-#endif	/* defined(USG) && !defined(HAVE_FTIME) */
-
-#if	defined(BSD4_2) || defined(BSD4_1C) || (defined (hp9000) && !defined (hpux))
-#include <sys/time.h>
-#else
-#if defined(_AIX)
-#include <sys/time.h>
-#endif
-#include <time.h>
-#endif	/* defined(BSD4_2) */
+#endif /* defined(HAVE_SYS_TIMEB_H) */
 
 #endif	/* defined(vms) */
 
@@ -82,16 +96,23 @@
 #include <string.h>
 #endif
 
-#if sgi
-#undef timezone
+/* Some old versions of bison generate parsers that use bcopy.
+   That loses on systems that don't provide the function, so we have
+   to redefine it here.  */
+#if !defined (HAVE_BCOPY) && defined (HAVE_MEMCPY) && !defined (bcopy)
+#define bcopy(from, to, len) memcpy ((to), (from), (len))
 #endif
 
+extern struct tm	*gmtime();
 extern struct tm	*localtime();
 
 #define yyparse getdate_yyparse
 #define yylex getdate_yylex
 #define yyerror getdate_yyerror
 
+static int yylex ();
+static int yyerror ();
+
 #if	!defined(lint) && !defined(SABER)
 static char RCS[] =
 	"$Header: str2date.y,v 2.1 90/09/06 08:15:06 cronan Exp $";
@@ -366,7 +387,7 @@
 %%
 
 /* Month and day table. */
-static TABLE	MonthDayTable[] = {
+static TABLE const MonthDayTable[] = {
     { "january",	tMONTH,  1 },
     { "february",	tMONTH,  2 },
     { "march",		tMONTH,  3 },
@@ -395,7 +416,7 @@
 };
 
 /* Time units table. */
-static TABLE	UnitsTable[] = {
+static TABLE const UnitsTable[] = {
     { "year",		tMONTH_UNIT,	12 },
     { "month",		tMONTH_UNIT,	1 },
     { "fortnight",	tMINUTE_UNIT,	14 * 24 * 60 },
@@ -410,7 +431,7 @@
 };
 
 /* Assorted relative-time words. */
-static TABLE	OtherTable[] = {
+static TABLE const OtherTable[] = {
     { "tomorrow",	tMINUTE_UNIT,	1 * 24 * 60 },
     { "yesterday",	tMINUTE_UNIT,	-1 * 24 * 60 },
     { "today",		tMINUTE_UNIT,	0 },
@@ -436,7 +457,7 @@
 
 /* The timezone table. */
 /* Some of these are commented out because a time_t can't store a float. */
-static TABLE	TimezoneTable[] = {
+static TABLE const TimezoneTable[] = {
     { "gmt",	tZONE,     HOUR( 0) },	/* Greenwich Mean */
     { "ut",	tZONE,     HOUR( 0) },	/* Universal (Coordinated) */
     { "utc",	tZONE,     HOUR( 0) },
@@ -520,7 +541,7 @@
 };
 
 /* Military timezone table. */
-static TABLE	MilitaryTable[] = {
+static TABLE const MilitaryTable[] = {
     { "a",	tZONE,	HOUR(  1) },
     { "b",	tZONE,	HOUR(  2) },
     { "c",	tZONE,	HOUR(  3) },
@@ -553,7 +574,7 @@
 
 
 /* ARGSUSED */
-int
+static int
 yyerror(s)
     char	*s;
 {
@@ -599,7 +620,7 @@
     MERIDIAN	Meridian;
     DSTMODE	DSTmode;
 {
-    static int	DaysInMonth[12] = {
+    static int DaysInMonth[12] = {
 	31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
     };
     time_t	tod;
@@ -693,7 +714,7 @@
 {
     register char	*p;
     register char	*q;
-    register TABLE	*tp;
+    register const TABLE	*tp;
     int			i;
     int			abbrev;
 
@@ -794,7 +815,7 @@
 }
 
 
-int
+static int
 yylex()
 {
     register char	c;
@@ -847,12 +868,38 @@
 }
 
 
+#define TM_YEAR_ORIGIN 1900
+
+/* Yield A - B, measured in seconds.  */
+static time_t
+difftm(a, b)
+     struct tm *a, *b;
+{
+  int ay = a->tm_year + (TM_YEAR_ORIGIN - 1);
+  int by = b->tm_year + (TM_YEAR_ORIGIN - 1);
+  return
+    (
+     (
+      (
+       /* difference in day of year */
+       a->tm_yday - b->tm_yday
+       /* + intervening leap days */
+       +  ((ay >> 2) - (by >> 2))
+       -  (ay/100 - by/100)
+       +  ((ay/100 >> 2) - (by/100 >> 2))
+       /* + difference in years * 365 */
+       +  (time_t)(ay-by) * 365
+       )*24 + (a->tm_hour - b->tm_hour)
+      )*60 + (a->tm_min - b->tm_min)
+     )*60 + (a->tm_sec - b->tm_sec);
+}
+
 time_t
 get_date(p, now)
     char		*p;
     struct timeb	*now;
 {
-    struct tm		*tm;
+    struct tm		*tm, gmt;
     struct timeb	ftz;
     time_t		Start;
     time_t		tod;
@@ -860,34 +907,12 @@
     yyInput = p;
     if (now == NULL) {
         now = &ftz;
-#if	!defined(HAVE_FTIME)
 	(void)time(&ftz.time);
-	/* Set the timezone global. */
-	tzset();
-	{
-#if sgi
-	    ftz.timezone = (int) _timezone / 60;
-#else /* not sgi */
-#ifdef __386BSD__
-	    ftz.timezone = 0;
-#else /* neither sgi nor 386BSD */
-#if defined (USG)
-	    extern time_t timezone;
 
-	    ftz.timezone = (int) timezone / 60;
-#else /* neither sgi nor 386BSD nor USG */
-	    struct timeval tv;
-	    struct timezone tz;
-
-	    gettimeofday (&tv, &tz);
-	    ftz.timezone = (int) tz.tz_minuteswest;
-#endif /* neither sgi nor 386BSD nor USG */
-#endif /* neither sgi nor 386BSD */
-#endif /* not sgi */
-	}
-#else /* HAVE_FTIME */
-	(void)ftime(&ftz);
-#endif /* HAVE_FTIME */
+	if (! (tm = gmtime (&ftz.time)))
+	    return -1;
+	gmt = *tm;	/* Make a copy, in case localtime modifies *tm.  */
+	ftz.timezone = difftm (&gmt, localtime (&ftz.time)) / 60;
     }
 
     tm = localtime(&now->time);
--- a/lib/getugroups.c
+++ b/lib/getugroups.c
@@ -24,29 +24,13 @@
 #include <unistd.h>
 #endif
 
-/* Even though SunOS 4, Ultrix 4, and 386BSD are mostly POSIX.1 compliant,
-   their getgroups system call (except in the `System V' environment, which
-   is troublesome in other ways) fills in an array of int, not gid_t
-   (which is `short' on those systems).  We do the same, for consistency.
-   Kludge, kludge.  */
-
-#ifdef _POSIX_VERSION
-#if !defined(sun) && !defined(ultrix) && !defined(__386BSD__)
-#define GETGROUPS_T gid_t
-#else /* sun or ultrix or 386BSD */
-#define GETGROUPS_T int
-#endif /* sun or ultrix or 386BSD */
-#else /* not _POSIX_VERSION */
-#define GETGROUPS_T int
-#endif /* not _POSIX_VERSION */
-
 /* setgrent, getgrent, and endgrent are not specified by POSIX.1,
    so header files might not declare them.
    If you don't have them at all, we can't implement this function.
    You lose!  */
 struct group *getgrent ();
 
-#if defined(USG) || defined(STDC_HEADERS)
+#if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
 #include <string.h>
 #else
 #include <strings.h>
--- a/lib/getusershell.c
+++ b/lib/getusershell.c
@@ -35,7 +35,7 @@
 static int readname ();
 
 /* List of shells to use if the shells file is missing. */
-static char *default_shells[] =
+static char const* const default_shells[] =
 {
   "/bin/sh", "/bin/csh", "/usr/bin/sh", "/usr/bin/csh", NULL
 };
--- a/lib/mktime.c
+++ b/lib/mktime.c
@@ -1,4 +1,6 @@
-/* Copyright (C) 1991 Free Software Foundation, Inc.
+/* Copyright (C) 1993 Free Software Foundation, Inc.
+   Contributed by Noel Cragg (noel@cs.oberlin.edu).
+
 This file is part of the GNU C Library.
 
 The GNU C Library is free software; you can redistribute it and/or
@@ -16,209 +18,276 @@
 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
 Cambridge, MA 02139, USA.  */
 
-#include <sys/types.h>
-#include <errno.h>
-#ifndef STDC_HEADERS
-extern int errno;
-#endif
-#ifdef TM_IN_SYS_TIME
-#include <sys/time.h>
-#else
-#include <time.h>
-#endif
-#ifdef HAVE_LIMITS_H
-#include <limits.h>
-#else
-#define	LONG_MAX (~(1 << (sizeof (long) * 8 - 1)))
-#define LONG_MIN (-LONG_MAX - 1)
-#define	INT_MAX (~(1 << (sizeof (int) * 8 - 1)))
-#define INT_MIN (-INT_MAX - 1)
+#ifdef HAVE_CONFIG_H
+#include "config.h"
 #endif
 
-#ifndef NULL
-#define NULL 0
-#endif
+#include <sys/types.h>		/* Some systems define `time_t' here.  */
+#include <time.h>
+
 
 #ifndef __isleap
 /* Nonzero if YEAR is a leap year (every 4 years,
-   except every 100th isn't, and every 1000th is).  */
+   except every 100th isn't, and every 400th is).  */
 #define	__isleap(year)	\
-  ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 1000 == 0))
+  ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
 #endif
 
+
 /* How many days are in each month.  */
-static unsigned short int __mon_lengths[2][12] =
-{
-  /* Normal years.  */
-  { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
-  /* Leap years.  */
-  { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
-};
+const unsigned short int __mon_lengths[2][12] =
+  {
+    /* Normal years.  */
+    { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
+    /* Leap years.  */
+    { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
+  };
+
 
-#define	invalid()	return (time_t) -1
+/* After testing this, the maximum number of iterations that I had on
+   any number that I tried was 3!  Not bad.
+
+   mktime converts a `struct tm' (broken-down local time) into a `time_t';
+   it is the opposite of localtime.  It is possible to put the following
+   values out of range and have mktime compensate: tm_sec, tm_min, tm_hour,
+   tm_mday, tm_year.  The other values in the structure are ignored.  */
+
+#ifdef DEBUG
+
+int debugging_enabled = 0;
 
-/* Return the `time_t' representation of TP and normalizes TP.
-   Return (time_t) -1 if TP is not representable as a `time_t'.
-   Note that 31 Dec 1969 23:59:59 is not representable
-   because it is represented as (time_t) -1.  */
-time_t
-mktime(tp)
-register struct tm *tp;
+/* Print the values in a `struct tm'.  */
+static void
+printtm (it)
+     struct tm *it;
 {
-  static struct tm min, max;
-  static char init = 0;
+  printf ("%d/%d/%d %d:%d:%d (%s) yday:%d f:%d o:%ld",
+	  it->tm_mon,
+	  it->tm_mday,
+	  it->tm_year,
+	  it->tm_hour,
+	  it->tm_min,
+	  it->tm_sec,
+	  it->tm_zone,
+	  it->tm_yday,
+	  it->tm_isdst,
+	  it->tm_gmtoff);
+}
+#endif
+
+static time_t
+dist_tm (t1, t2)
+     struct tm *t1;
+     struct tm *t2;
+{
+  time_t distance = 0;
+  unsigned long int v1, v2;
+  int diff_flag = 0;
 
-  register time_t result;
-  register time_t t;
-  register int i;
-  register unsigned short *l;
-  register struct tm *new;
-  time_t end;
+  v1 = v2 = 0;
 
-  if (tp == NULL)
-    {
-      errno = EINVAL;
-      invalid();
+#define doit(x, secs)							      \
+  v1 += t1->x * secs;							      \
+  v2 += t2->x * secs;							      \
+  if (!diff_flag)							      \
+    {									      \
+      if (t1->x < t2->x)						      \
+	diff_flag = -1;							      \
+      else if (t1->x > t2->x)						      \
+	diff_flag = 1;							      \
     }
+  
+  doit (tm_year, 31536000);	/* Okay, not all years have 365 days.  */
+  doit (tm_mon, 2592000);	/* Okay, not all months have 30 days.  */
+  doit (tm_mday, 86400);
+  doit (tm_hour, 3600);
+  doit (tm_min, 60);
+  doit (tm_sec, 1);
+  
+#undef doit
+
+  distance = v1 - v2;
+
+  /* We need this DIFF_FLAG business because it is forseeable that the
+     distance may be zero when, in actuality, the two structures are
+     different.  This is usually the case when the dates are 366 days
+     apart and one of the years is a leap year.  */
 
-  if (!init)
+  if (distance == 0 && diff_flag)
+    distance = 86400 * diff_flag;
+
+  return distance;
+}
+      
+
+/* Modified binary search -- make intelligent guesses as to where the time
+   might lie along the timeline, assuming that our target time lies a
+   linear distance (w/o considering time jumps of a particular region).
+
+   Assume that time does not fluctuate at all along the timeline -- e.g.,
+   assume that a day will always take 86400 seconds, etc. -- and come up
+   with a hypothetical value for the time_t representation of the struct tm
+   TARGET, in relation to the guess variable -- it should be pretty close!  */
+
+static time_t
+search (target)
+     struct tm *target;
+{
+  struct tm *guess_tm;
+  time_t guess = 0;
+  time_t distance = 0;
+
+  do
     {
-      init = 1;
-      end = (time_t) LONG_MIN;
-      new = gmtime(&end);
-      if (new != NULL)
-	min = *new;
-      else
-	min.tm_sec = min.tm_min = min.tm_hour =
-	  min.tm_mday = min.tm_mon = min.tm_year = INT_MIN;
+      guess += distance;
 
-      end = (time_t) LONG_MAX;
-      new = gmtime(&end);
-      if (new != NULL)
-	max = *new;
-      else
-	max.tm_sec = max.tm_min = max.tm_hour =
-	  max.tm_mday = max.tm_mon = max.tm_year = INT_MAX;
-    }
+      guess_tm = localtime (&guess);
+      
+#ifdef DEBUG
+      if (debugging_enabled)
+	{
+	  printf ("guess %d == ", guess);
+	  printtm (guess_tm);
+	  puts ("");
+	}
+#endif
+      
+      /* Are we on the money?  */
+      distance = dist_tm (target, guess_tm);
+
+    } while (distance != 0);
+
+  return guess;
+}
+
+/* Since this function will call localtime many times (and the user might
+   be passing their `struct tm *' right from localtime, let's make a copy
+   for ourselves and run the search on the copy.
 
-  /* Make all the elements of TP that we pay attention to
-     be within the ranges of reasonable values for those things.  */
-#define	normalize(elt, min, max, nextelt) \
-  while (tp->elt < min)							      \
-    {									      \
-      --tp->nextelt;							      \
-      tp->elt += max + 1;						      \
-    }									      \
-  while (tp->elt > max)							      \
-    {									      \
-      ++tp->nextelt;							      \
-      tp->elt -= max + 1;						      \
+   Also, we have to normalize the timeptr because it's possible to call mktime
+   with values that are out of range for a specific item (like 30th Feb).  */
+
+time_t
+mktime (timeptr)
+     struct tm *timeptr;
+{
+  struct tm private_mktime_struct_tm; /* Yes, users can get a ptr to this.  */
+  struct tm *me;
+  time_t result;
+
+  me = &private_mktime_struct_tm;
+  
+  *me = *timeptr;
+
+#define normalize(foo,x,y,bar); \
+  while (me->foo < x) \
+    { \
+      me->bar--; \
+      me->foo = (y - (x - me->foo)); \
+    } \
+  while (me->foo > y) \
+    { \
+      me->bar++; \
+      me->foo = (x + (me->foo - y)); \
     }
-
+  
   normalize (tm_sec, 0, 59, tm_min);
   normalize (tm_min, 0, 59, tm_hour);
-  normalize (tm_hour, 0, 24, tm_mday);
-
-  /* Normalize the month first so we can use
-     it to figure the range for the day.  */
+  normalize (tm_hour, 0, 23, tm_mday);
+  
+  /* Do the month first, so day range can be found.  */
   normalize (tm_mon, 0, 11, tm_year);
-  normalize (tm_mday, 1, __mon_lengths[__isleap (tp->tm_year)][tp->tm_mon],
+  normalize (tm_mday, 1,
+	     __mon_lengths[__isleap (me->tm_year)][me->tm_mon],
 	     tm_mon);
 
-  /* Normalize the month again, since normalizing
-     the day may have pushed it out of range.  */
+  /* Do the month again, because the day may have pushed it out of range.  */
   normalize (tm_mon, 0, 11, tm_year);
 
-  /* Normalize the day again, because normalizing
-     the month may have changed the range.  */
-  normalize (tm_mday, 1, __mon_lengths[__isleap (tp->tm_year)][tp->tm_mon],
+  /* Do day again, because month may have changed the range.  */
+  normalize (tm_mday, 1,
+	     __mon_lengths[__isleap (me->tm_year)][me->tm_mon],
 	     tm_mon);
-
-  /* Check for out-of-range values.  */
-#define	lowhigh(field, minmax, cmp)	(tp->field cmp minmax.field)
-#define	low(field)			lowhigh(field, min, <)
-#define	high(field)			lowhigh(field, max, >)
-#define	oor(field)			(low(field) || high(field))
-#define	lowbound(field)			(tp->field == min.field)
-#define	highbound(field)		(tp->field == max.field)
-  if (oor(tm_year))
-    invalid();
-  else if (lowbound(tm_year))
-    {
-      if (low(tm_mon))
-	invalid();
-      else if (lowbound(tm_mon))
-	{
-	  if (low(tm_mday))
-	    invalid();
-	  else if (lowbound(tm_mday))
-	    {
-	      if (low(tm_hour))
-		invalid();
-	      else if (lowbound(tm_hour))
-		{
-		  if (low(tm_min))
-		    invalid();
-		  else if (lowbound(tm_min))
-		    {
-		      if (low(tm_sec))
-			invalid();
-		    }
-		}
-	    }
-	}
-    }
-  else if (highbound(tm_year))
+  
+#ifdef DEBUG
+  if (debugging_enabled)
     {
-      if (high(tm_mon))
-	invalid();
-      else if (highbound(tm_mon))
-	{
-	  if (high(tm_mday))
-	    invalid();
-	  else if (highbound(tm_mday))
-	    {
-	      if (high(tm_hour))
-		invalid();
-	      else if (highbound(tm_hour))
-		{
-		  if (high(tm_min))
-		    invalid();
-		  else if (highbound(tm_min))
-		    {
-		      if (high(tm_sec))
-			invalid();
-		    }
-		}
-	    }
-	}
+      printf ("After normalizing: ");
+      printtm (me);
+      puts ("\n");
     }
+#endif
 
-  t = 0;
-  for (i = 1970; i > 1900 + tp->tm_year; --i)
-    t -= __isleap(i) ? 366 : 365;
-  for (i = 1970; i < 1900 + tp->tm_year; ++i)
-    t += __isleap(i) ? 366 : 365;
-  l = __mon_lengths[__isleap(1900 + tp->tm_year)];
-  for (i = 0; i < tp->tm_mon; ++i)
-    t += l[i];
-  t += tp->tm_mday - 1;
-  result = ((t * 60 * 60 * 24) +
-	    (tp->tm_hour * 60 * 60) +
-	    (tp->tm_min * 60) +
-	    tp->tm_sec);
+  result = search (me);
 
-  end = result;
-#if 0				/* This code breaks it, on SunOS anyway. */
-  if (tp->tm_isdst < 0)
-    new = localtime(&end);
-  else
-#endif
-    new = gmtime(&end);
-  if (new == NULL)
-    invalid();
-  new->tm_isdst = tp->tm_isdst;
-  *tp = *new;
+  *timeptr = *me;
 
   return result;
 }
+
+#ifdef DEBUG
+void
+main (argc, argv)
+     int argc;
+     char *argv[];
+{
+  int time;
+  int result_time;
+  struct tm *tmptr;
+  
+  if (argc == 1)
+    {
+      long q;
+      
+      printf ("starting long test...\n");
+
+      for (q = 10000000; q < 1000000000; q++)
+	{
+	  struct tm *tm = localtime (&q);
+	  if ((q % 10000) == 0) { printf ("%ld\n", q); fflush (stdout); }
+	  if (q != my_mktime (tm))
+	    { printf ("failed for %ld\n", q); fflush (stdout); }
+	}
+      
+      printf ("test finished\n");
+
+      exit (0);
+    }
+  
+  if (argc != 2)
+    {
+      printf ("wrong # of args\n");
+      exit (0);
+    }
+  
+  debugging_enabled = 1;	/* we want to see the info */
+
+  ++argv;
+  time = atoi (*argv);
+  
+  printf ("Time: %d %s\n", time, ctime (&time));
+
+  tmptr = localtime (&time);
+  printf ("localtime returns: ");
+  printtm (tmptr);
+  printf ("\n");
+  printf ("mktime: %d\n\n", mktime (tmptr));
+
+  tmptr->tm_sec -= 20;
+  tmptr->tm_min -= 20;
+  tmptr->tm_hour -= 20;
+  tmptr->tm_mday -= 20;
+  tmptr->tm_mon -= 20;
+  tmptr->tm_year -= 20;
+  tmptr->tm_gmtoff -= 20000;	/* this has no effect! */
+  tmptr->tm_zone = NULL;	/* nor does this! */
+  tmptr->tm_isdst = -1;
+
+  printf ("changed ranges: ");
+  printtm (tmptr);
+  printf ("\n\n");
+
+  result_time = mktime (tmptr);
+  printf ("\n  mine: %d\n", result_time);
+}
+#endif /* DEBUG */
--- a/lib/posixtm.y
+++ b/lib/posixtm.y
@@ -20,7 +20,7 @@
 #ifdef __GNUC__
 #define alloca __builtin_alloca
 #else
-#ifdef sparc
+#ifdef HAVE_ALLOCA_H
 #include <alloca.h>
 #else
 #ifdef _AIX
@@ -33,7 +33,19 @@
 
 #include <stdio.h>
 #include <sys/types.h>
+
+#ifdef TM_IN_SYS_TIME
+#include <sys/time.h>
+#else
 #include <time.h>
+#endif
+
+/* Some old versions of bison generate parsers that use bcopy.
+   That loses on systems that don't provide the function, so we have
+   to redefine it here.  */
+#if !defined (HAVE_BCOPY) && defined (HAVE_MEMCPY) && !defined (bcopy)
+#define bcopy(from, to, len) memcpy ((to), (from), (len))
+#endif
 
 #define YYDEBUG 1
 
--- a/lib/putenv.c
+++ b/lib/putenv.c
@@ -18,19 +18,30 @@
 
 #include <sys/types.h>
 #include <errno.h>
-#ifdef STDC_HEADERS
+
+/* This needs to come after some library #include
+   to get __GNU_LIBRARY__ defined.  */
+#ifdef	__GNU_LIBRARY__
+/* Don't include stdlib.h for non-GNU C libraries because some of them
+   contain conflicting prototypes for getopt.  */
 #include <stdlib.h>
-#else
+#endif	/* GNU C library.  */
+
+#ifndef STDC_HEADERS
 extern int errno;
 #endif
 
-#if defined(STDC_HEADERS) || defined(USG)
+#if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
 #include <string.h>
+#ifndef index
 #define index strchr
+#endif
+#ifndef bcopy
 #define bcopy(s, d, n) memcpy((d), (s), (n))
-#else /* not (STDC_HEADERS or USG) */
+#endif
+#else
 #include <strings.h>
-#endif /* STDC_HEADERS or USG */
+#endif
 
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
@@ -40,10 +51,6 @@
 #define NULL 0
 #endif
 
-#if !__STDC__
-#define const
-#endif
-
 extern char **environ;
 
 /* Put STRING, which is of the form "NAME=VALUE", in the environment.  */
--- a/lib/strftime.c
+++ b/lib/strftime.c
@@ -84,22 +84,18 @@
 extern char *tzname[2];
 #endif
 
-#if !__STDC__
-#define const
-#endif
-
 /* Types of padding for numbers in date and time. */
 enum padding
 {
   none, blank, zero
 };
 
-static char *days[] =
+static char const* const days[] =
 {
   "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
 };
 
-static char *months[] =
+static char const * const months[] =
 {
   "January", "February", "March", "April", "May", "June",
   "July", "August", "September", "October", "November", "December"
--- a/lib/strtod.c
+++ b/lib/strtod.c
@@ -34,10 +34,6 @@
 #define HUGE_VAL HUGE
 #endif
 
-#if !__STDC__
-#define const
-#endif
-
 /* Convert NPTR to a double.  If ENDPTR is not NULL, a pointer to the
    character after the last one used in the number is put in *ENDPTR.  */
 double