This is the mail archive of the newlib@sources.redhat.com mailing list for the newlib project.


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

Re: sys/reent.h and time.h


geoffb@bops.com wrote:
> 
> > From: J. Johnston [mailto:jjohnstn@cygnus.com]
> >
> > geoffb@bops.com wrote:
> > >
> > > The comment at the top of sys/reent.h says:
> > >
> > > /* WARNING: All identifiers here must begin with an
> > underscore.  This file
> > > is
> > >    included by stdio.h and others and we therefore must
> > only use identifiers
> > >    in the namespace allotted to us.  */
> > >
> > > but reent.h includes time.h (presumably to get `struct tm')
> > which brings in
> > > quite a few identifiers that don't begin with an
> > underscore.  The upshot is
> > > the following program won't compile:
> > >
> > >         #include <stdio.h>
> > >
> > >         int
> > >         main ()
> > >         {
> > >                 int time = 0;
> > >                 printf ("%d", time);
> > >         }
> > >
> > > since time is defined in time.h.
> > >
> > > Is this a known problem?  What is the best approach to fixing this?
> > >
> > > Please CC me as I'm not on the list (yet).
> > >
> >
> > IMO the best way to fix this is to do what gcc does with
> > stddef.h when it
> > only wants to have size_t declared.  A __need_struct_tm
> > setting before including
> > time.h should remove all but the desired type declaration.  I
> > have attached such a
> > patch, but I haven't tested it yet.
> >
> > -- Jeff J.
> 
> But then you still get struct tm in your namespace.  What I ended
> up doing was declaring a struct _tm that matches struct tm and
> casting it to struct tm in lcltime.c.
> 

Actually, that isn't unreasonble considering that we never go outside the
ANSI minimum definition of struct tm.  It is not so easy though; there are other
problems caused by removing the include of <time.h> in <sys/reent.h>.

First of all, we break Cygwin and RTEMS because they specify READ_WRITE_RETURN_TYPE as ssize_t
which is no longer defined but is referenced in sys/reent.h.  We can fix this by changing
the type to be _ssize_t which matches the type already used by the low level _read_r and _write_r
routines in <reent.h>.  Because of this, we include <sys/_types.h>.  We also need to include
<sys/_types.h> in <sys/unistd.h>.

There are also NULL references in the reentrant structure that need handling as the
definition for NULL was brought in by <time.h> (e.g. impure.c won't compile).  The 
simple answer to this is just to use _NULL instead and define it to 0 as that was all
<time.h> was doing anyway.

The resultant patch is attached.  It builds for both Cygwin and mn10300.

-- Jeff J.
Index: libc/include/sys/config.h
===================================================================
RCS file: /cvs/src/src/newlib/libc/include/sys/config.h,v
retrieving revision 1.7
diff -u -r1.7 config.h
--- libc/include/sys/config.h	2001/03/07 21:03:43	1.7
+++ libc/include/sys/config.h	2001/08/15 21:00:32
@@ -128,7 +128,7 @@
 
 #if defined(__CYGWIN32__) || defined(__CYGWIN__)
 #define __FILENAME_MAX__ (260 - 1 /* NUL */)
-#define _READ_WRITE_RETURN_TYPE ssize_t
+#define _READ_WRITE_RETURN_TYPE _ssize_t
 #if defined(__INSIDE_CYGWIN__) || defined(_COMPILING_NEWLIB)
 #define __IMPORT
 #else
@@ -138,7 +138,7 @@
 
 #if defined(__rtems__)
 #define __FILENAME_MAX__ 255
-#define _READ_WRITE_RETURN_TYPE ssize_t
+#define _READ_WRITE_RETURN_TYPE _ssize_t
 #endif
 
 #ifndef __IMPORT
Index: libc/include/sys/reent.h
===================================================================
RCS file: /cvs/src/src/newlib/libc/include/sys/reent.h,v
retrieving revision 1.5
diff -u -r1.5 reent.h
--- libc/include/sys/reent.h	2001/03/06 01:04:42	1.5
+++ libc/include/sys/reent.h	2001/08/15 21:00:32
@@ -11,7 +11,7 @@
 #define _SYS_REENT_H_
 
 #include <_ansi.h>
-#include <time.h>
+#include <sys/_types.h>
 
 #ifndef __Long
 #if __LONG_MAX__ == 2147483647L
@@ -42,6 +42,20 @@
   __ULong _x[1];
 };
 
+/* needed by reentrant structure */
+struct __tm
+{
+  int   tm_sec;
+  int   tm_min;
+  int   tm_hour;
+  int   tm_mday;
+  int   tm_mon;
+  int   tm_year;
+  int   tm_wday;
+  int   tm_yday;
+  int   tm_isdst;
+};
+
 /*
  * atexit() support
  */
@@ -208,7 +222,7 @@
           unsigned int _unused_rand;
           char * _strtok_last;
           char _asctime_buf[26];
-          struct tm _localtime_buf;
+          struct __tm _localtime_buf;
           int _gamma_signgam;
           __extension__ unsigned long long _rand_next;
           struct _rand48 _r48;
@@ -238,9 +252,11 @@
   struct __sFILE __sf[3];		/* first three file descriptors */
 };
 
+#define _NULL 0
+
 #define _REENT_INIT(var) \
   { 0, &var.__sf[0], &var.__sf[1], &var.__sf[2], 0, "", 0, "C", \
-    0, NULL, NULL, 0, NULL, NULL, 0, NULL, { {0, NULL, "", \
+    0, _NULL, _NULL, 0, _NULL, _NULL, 0, _NULL, { {0, _NULL, "", \
     { 0,0,0,0,0,0,0,0}, 0, 1, \
     {{_RAND48_SEED_0, _RAND48_SEED_1, _RAND48_SEED_2}, \
      {_RAND48_MULT_0, _RAND48_MULT_1, _RAND48_MULT_2}, _RAND48_ADD}} } }
Index: libc/include/sys/unistd.h
===================================================================
RCS file: /cvs/src/src/newlib/libc/include/sys/unistd.h,v
retrieving revision 1.22
diff -u -r1.22 unistd.h
--- libc/include/sys/unistd.h	2001/05/08 01:15:06	1.22
+++ libc/include/sys/unistd.h	2001/08/15 21:00:32
@@ -7,6 +7,7 @@
 
 #include <_ansi.h>
 #include <sys/types.h>
+#include <sys/_types.h>
 #define __need_size_t
 #define __need_ptrdiff_t
 #include <stddef.h>
Index: libc/time/lcltime.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/time/lcltime.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 lcltime.c
--- libc/time/lcltime.c	2000/02/17 19:39:51	1.1.1.1
+++ libc/time/lcltime.c	2001/08/15 21:00:32
@@ -51,7 +51,7 @@
 _DEFUN (localtime, (tim_p),
 	_CONST time_t * tim_p)
 {
-  return localtime_r (tim_p, &(_REENT->_new._reent._localtime_buf));
+  return localtime_r (tim_p, (struct tm *)&(_REENT->_new._reent._localtime_buf));
 }
 
 #endif

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