sys/reent.h and time.h

J. Johnston jjohnstn@cygnus.com
Mon Aug 13 16:20:00 GMT 2001


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.
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/13 23:17:12
@@ -11,6 +11,7 @@
 #define _SYS_REENT_H_
 
 #include <_ansi.h>
+#define __need_struct_tm
 #include <time.h>
 
 #ifndef __Long
Index: libc/include/time.h
===================================================================
RCS file: /cvs/src/src/newlib/libc/include/time.h,v
retrieving revision 1.5
diff -u -r1.5 time.h
--- libc/include/time.h	2001/04/19 15:54:47	1.5
+++ libc/include/time.h	2001/08/13 23:17:12
@@ -4,7 +4,11 @@
  * Struct and function declarations for dealing with time.
  */
 
-#ifndef _TIME_H_
+#if !defined(_TIME_H_) || defined(__need_struct_tm)
+
+/* if __need_struct_tm is defined, we only want to define struct tm */
+#ifndef __need_struct_tm
+
 #define _TIME_H_
 
 #include "_ansi.h"
@@ -30,6 +34,7 @@
 #include <stddef.h>
 
 #include <sys/types.h>
+#endif /* !__need_struct_tm */
 
 struct tm
 {
@@ -44,6 +49,7 @@
   int	tm_isdst;
 };
 
+#ifndef __need_struct_tm
 clock_t	   _EXFUN(clock,    (void));
 double	   _EXFUN(difftime, (time_t _time2, time_t _time1));
 time_t	   _EXFUN(mktime,   (struct tm *_timeptr));
@@ -187,5 +193,6 @@
 #ifdef __cplusplus
 }
 #endif
+#endif /* !__need_struct_tm */
 #endif /* _TIME_H_ */
 


More information about the Newlib mailing list