This is the mail archive of the libc-alpha@sources.redhat.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

mktime.c fixes (part 1 of 6): assume freestanding C89 or better


While fixing Debian bug 177940, Jeff Bailey suggested that we merge
gnulib's fixes to mktime.c back into the GNU C library.  See:
<http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=177940>

The gnulib patches to mktime.c have been stable for three months, so
this would be a good time to do the merge.  The gnulib version is used
in several GNU applications (in preference to glibc mktime, which
fails Autoconf's runtime test for mktime), and it seems to work well
in practice.

I've broken up the fixes into a series of six patches; each assumes
that the previous patches have been applied.

Here's the first patch.  All GNU applications that use mktime.c now
assume freestanding C89 or better, and gnulib has standardized on
this, so it simplifies maintenance to remove the K&R gorp.  As it
happens, switching to C89 uncovers a bug in the debug code, which this
patch also fixes.


2003-12-30  Paul Eggert  <eggert@twinsun.com>

	* time/mktime.c: Assume freestanding C89 or better.

	(HAVE_LIMITS_H, STDC_HEADERS) [defined _LIBC]: Remove;
	assume they're 1.
	(__P): Remove; not used.
	(CHAR_BIT, INT_MIN, INT_MAX): Remove; <limits.h> defines them.
	(mktime, not_equal_tm, print_tm, check_result, main): Use prototypes.
	Prototypes use const * where appropriate.
	(main) [DEBUG]: Fix typo in testing code uncovered by above changes,
	which caused the testing code to dump core on some hosts.

===================================================================
RCS file: RCS/mktime.c,v
retrieving revision 1.51
retrieving revision 1.51.0.1
diff -pu -r1.51 -r1.51.0.1
--- mktime.c	2002/11/24 18:37:56	1.51
+++ mktime.c	2003/12/31 05:10:34	1.51.0.1
@@ -1,5 +1,5 @@
 /* Convert a `struct tm' to a time_t value.
-   Copyright (C) 1993-1999, 2002 Free Software Foundation, Inc.
+   Copyright (C) 1993-1999, 2002, 2003 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Paul Eggert (eggert@twinsun.com).
 
@@ -26,11 +26,6 @@
 # include <config.h>
 #endif
 
-#ifdef _LIBC
-# define HAVE_LIMITS_H 1
-# define STDC_HEADERS 1
-#endif
-
 /* Assume that leap seconds are possible, unless told otherwise.
    If the host has a `zic' command with a `-L leapsecondfilename' option,
    then it supports leap seconds; otherwise it probably doesn't.  */
@@ -41,32 +36,16 @@
 #include <sys/types.h>		/* Some systems define `time_t' here.  */
 #include <time.h>
 
-#if HAVE_LIMITS_H
-# include <limits.h>
-#endif
+#include <limits.h>
 
 #if DEBUG
 # include <stdio.h>
-# if STDC_HEADERS
-#  include <stdlib.h>
-#  include <string.h>
-# endif
+# include <stdlib.h>
+# include <string.h>
 /* Make it work even if the system's libc has its own mktime routine.  */
 # define mktime my_mktime
 #endif /* DEBUG */
 
-#ifndef __P
-# if defined __GNUC__ || (defined __STDC__ && __STDC__)
-#  define __P(args) args
-# else
-#  define __P(args) ()
-# endif  /* GCC.  */
-#endif  /* Not __P.  */
-
-#ifndef CHAR_BIT
-# define CHAR_BIT 8
-#endif
-
 /* The extra casts work around common compiler bugs.  */
 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
 /* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
@@ -75,13 +54,6 @@
 			      ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) : (t) 0))
 #define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
 
-#ifndef INT_MIN
-# define INT_MIN TYPE_MINIMUM (int)
-#endif
-#ifndef INT_MAX
-# define INT_MAX TYPE_MAXIMUM (int)
-#endif
-
 #ifndef TIME_T_MIN
 # define TIME_T_MIN TYPE_MINIMUM (time_t)
 #endif
@@ -395,8 +367,7 @@ static time_t localtime_offset;
 
 /* Convert *TP to a time_t value.  */
 time_t
-mktime (tp)
-     struct tm *tp;
+mktime (struct tm *tp)
 {
 #ifdef _LIBC
   /* POSIX.1 8.1.1 requires that whenever mktime() is called, the
@@ -420,9 +391,7 @@ libc_hidden_weak (timelocal)
 #if DEBUG
 
 static int
-not_equal_tm (a, b)
-     struct tm *a;
-     struct tm *b;
+not_equal_tm (const struct tm *a, const struct tm *b)
 {
   return ((a->tm_sec ^ b->tm_sec)
 	  | (a->tm_min ^ b->tm_min)
@@ -436,8 +405,7 @@ not_equal_tm (a, b)
 }
 
 static void
-print_tm (tp)
-     struct tm *tp;
+print_tm (const struct tm *tp)
 {
   if (tp)
     printf ("%04d-%02d-%02d %02d:%02d:%02d yday %03d wday %d isdst %d",
@@ -449,11 +417,7 @@ print_tm (tp)
 }
 
 static int
-check_result (tk, tmk, tl, lt)
-     time_t tk;
-     struct tm tmk;
-     time_t tl;
-     struct tm *lt;
+check_result (time_t tk, struct tm tmk, time_t tl, const struct tm *lt)
 {
   if (tk != tl || !lt || not_equal_tm (&tmk, lt))
     {
@@ -469,9 +433,7 @@ check_result (tk, tmk, tl, lt)
 }
 
 int
-main (argc, argv)
-     int argc;
-     char **argv;
+main (int argc, char **argv)
 {
   int status = 0;
   struct tm tm, tmk, tml;
@@ -517,7 +479,7 @@ main (argc, argv)
 	      {
 		tmk = tml = *lt;
 		tk = mktime (&tmk);
-		status |= check_result (tk, tmk, tl, tml);
+		status |= check_result (tk, tmk, tl, &tml);
 	      }
 	    else
 	      {
@@ -534,7 +496,7 @@ main (argc, argv)
 	      {
 		tmk = tml = *lt;
 		tk = tl;
-		status |= check_result (tk, tmk, tl, tml);
+		status |= check_result (tk, tmk, tl, &tml);
 	      }
 	    else
 	      {
@@ -557,6 +519,6 @@ main (argc, argv)
 
 /*
 Local Variables:
-compile-command: "gcc -DDEBUG -DHAVE_LIMITS_H -DSTDC_HEADERS -Wall -W -O -g mktime.c -o mktime"
+compile-command: "gcc -DDEBUG -Wall -W -O -g mktime.c -o mktime"
 End:
 */


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]