xref: /netbsd-src/lib/libc/time/private.h (revision 7788a0781fe6ff2cce37368b4578a7ade0850cb1)
1 /*	$NetBSD: private.h,v 1.29 2013/07/17 20:13:04 christos Exp $	*/
2 
3 #ifndef PRIVATE_H
4 #define PRIVATE_H
5 
6 /* NetBSD defaults */
7 #define TM_GMTOFF	tm_gmtoff
8 #define TM_ZONE		tm_zone
9 #define STD_INSPIRED	1
10 #define HAVE_LONG_DOUBLE 1
11 
12 /* For when we build zic as a host tool. */
13 #if HAVE_NBTOOL_CONFIG_H
14 #include "nbtool_config.h"
15 #endif
16 
17 /*
18 ** This file is in the public domain, so clarified as of
19 ** 1996-06-05 by Arthur David Olson.
20 */
21 
22 /*
23 ** This header is for use ONLY with the time conversion code.
24 ** There is no guarantee that it will remain unchanged,
25 ** or that it will remain at all.
26 ** Do NOT copy it to any system include directory.
27 ** Thank you!
28 */
29 
30 #define GRANDPARENTED	"Local time zone must be set--see zic manual page"
31 
32 /*
33 ** Defaults for preprocessor symbols.
34 ** You can override these in your C compiler options, e.g. `-DHAVE_ADJTIME=0'.
35 */
36 
37 #ifndef HAVE_ADJTIME
38 #define HAVE_ADJTIME		1
39 #endif /* !defined HAVE_ADJTIME */
40 
41 #ifndef HAVE_GETTEXT
42 #define HAVE_GETTEXT		0
43 #endif /* !defined HAVE_GETTEXT */
44 
45 #ifndef HAVE_INCOMPATIBLE_CTIME_R
46 #define HAVE_INCOMPATIBLE_CTIME_R	0
47 #endif /* !defined INCOMPATIBLE_CTIME_R */
48 
49 #ifndef HAVE_SETTIMEOFDAY
50 #define HAVE_SETTIMEOFDAY	3
51 #endif /* !defined HAVE_SETTIMEOFDAY */
52 
53 #ifndef HAVE_SYMLINK
54 #define HAVE_SYMLINK		1
55 #endif /* !defined HAVE_SYMLINK */
56 
57 #ifndef HAVE_SYS_STAT_H
58 #define HAVE_SYS_STAT_H		1
59 #endif /* !defined HAVE_SYS_STAT_H */
60 
61 #ifndef HAVE_SYS_WAIT_H
62 #define HAVE_SYS_WAIT_H		1
63 #endif /* !defined HAVE_SYS_WAIT_H */
64 
65 #ifndef HAVE_UNISTD_H
66 #define HAVE_UNISTD_H		1
67 #endif /* !defined HAVE_UNISTD_H */
68 
69 #ifndef HAVE_UTMPX_H
70 #define HAVE_UTMPX_H		0
71 #endif /* !defined HAVE_UTMPX_H */
72 
73 #ifdef LOCALE_HOME
74 #undef LOCALE_HOME		/* not to be handled by tzcode itself */
75 #endif /* defined LOCALE_HOME */
76 
77 #if HAVE_INCOMPATIBLE_CTIME_R
78 #define asctime_r _incompatible_asctime_r
79 #define ctime_r _incompatible_ctime_r
80 #endif /* HAVE_INCOMPATIBLE_CTIME_R */
81 
82 /*
83 ** Nested includes
84 */
85 
86 #include "sys/types.h"	/* for time_t */
87 #include "stdio.h"
88 #include "errno.h"
89 #include "string.h"
90 #include "limits.h"	/* for CHAR_BIT et al. */
91 #include "time.h"
92 #include "stdlib.h"
93 
94 #if HAVE_GETTEXT
95 #include "libintl.h"
96 #endif /* HAVE_GETTEXT */
97 
98 #if HAVE_SYS_WAIT_H
99 #include <sys/wait.h>	/* for WIFEXITED and WEXITSTATUS */
100 #endif /* HAVE_SYS_WAIT_H */
101 
102 #ifndef WIFEXITED
103 #define WIFEXITED(status)	(((status) & 0xff) == 0)
104 #endif /* !defined WIFEXITED */
105 #ifndef WEXITSTATUS
106 #define WEXITSTATUS(status)	(((status) >> 8) & 0xff)
107 #endif /* !defined WEXITSTATUS */
108 
109 #if HAVE_UNISTD_H
110 #include "unistd.h"	/* for F_OK, R_OK, and other POSIX goodness */
111 #endif /* HAVE_UNISTD_H */
112 
113 #ifndef F_OK
114 #define F_OK	0
115 #endif /* !defined F_OK */
116 #ifndef R_OK
117 #define R_OK	4
118 #endif /* !defined R_OK */
119 
120 /* Unlike <ctype.h>'s isdigit, this also works if c < 0 | c > UCHAR_MAX. */
121 #define is_digit(c) ((unsigned)(c) - '0' <= 9)
122 
123 /*
124 ** Define HAVE_STDINT_H's default value here, rather than at the
125 ** start, since __GLIBC__'s value depends on previously-included
126 ** files.
127 ** (glibc 2.1 and later have stdint.h, even with pre-C99 compilers.)
128 */
129 #ifndef HAVE_STDINT_H
130 #define HAVE_STDINT_H \
131 	(199901 <= __STDC_VERSION__ || \
132 	2 < (__GLIBC__ + (0 < __GLIBC_MINOR__)))
133 #endif /* !defined HAVE_STDINT_H */
134 
135 #if HAVE_STDINT_H
136 #include "stdint.h"
137 #endif /* !HAVE_STDINT_H */
138 
139 #ifndef HAVE_INTTYPES_H
140 # define HAVE_INTTYPES_H HAVE_STDINT_H
141 #endif
142 #if HAVE_INTTYPES_H
143 # include <inttypes.h>
144 #endif
145 
146 #ifndef INT_FAST64_MAX
147 /* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX.  */
148 #if defined LLONG_MAX || defined __LONG_LONG_MAX__
149 typedef long long	int_fast64_t;
150 # ifdef LLONG_MAX
151 #  define INT_FAST64_MIN LLONG_MIN
152 #  define INT_FAST64_MAX LLONG_MAX
153 # else
154 #  define INT_FAST64_MIN __LONG_LONG_MIN__
155 #  define INT_FAST64_MAX __LONG_LONG_MAX__
156 # endif
157 # define SCNdFAST64 "lld"
158 #else /* ! (defined LLONG_MAX || defined __LONG_LONG_MAX__) */
159 #if (LONG_MAX >> 31) < 0xffffffff
160 Please use a compiler that supports a 64-bit integer type (or wider);
161 you may need to compile with "-DHAVE_STDINT_H".
162 #endif /* (LONG_MAX >> 31) < 0xffffffff */
163 typedef long		int_fast64_t;
164 # define INT_FAST64_MIN LONG_MIN
165 # define INT_FAST64_MAX LONG_MAX
166 # define SCNdFAST64 "ld"
167 #endif /* ! (defined LLONG_MAX || defined __LONG_LONG_MAX__) */
168 #endif /* !defined INT_FAST64_MAX */
169 
170 #ifndef INT_FAST32_MAX
171 # if INT_MAX >> 31 == 0
172 typedef long int_fast32_t;
173 # else
174 typedef int int_fast32_t;
175 # endif
176 #endif
177 
178 #ifndef INTMAX_MAX
179 # if defined LLONG_MAX || defined __LONG_LONG_MAX__
180 typedef long long intmax_t;
181 #  define PRIdMAX "lld"
182 # else
183 typedef long intmax_t;
184 #  define PRIdMAX "ld"
185 # endif
186 #endif
187 
188 #ifndef UINTMAX_MAX
189 # if defined ULLONG_MAX || defined __LONG_LONG_MAX__
190 typedef unsigned long long uintmax_t;
191 #  define PRIuMAX "llu"
192 # else
193 typedef unsigned long uintmax_t;
194 #  define PRIuMAX "lu"
195 # endif
196 #endif
197 
198 #ifndef INT32_MAX
199 #define INT32_MAX 0x7fffffff
200 #endif /* !defined INT32_MAX */
201 #ifndef INT32_MIN
202 #define INT32_MIN (-1 - INT32_MAX)
203 #endif /* !defined INT32_MIN */
204 
205 #if 2 < __GNUC__ + (96 <= __GNUC_MINOR__)
206 # define ATTRIBUTE_CONST __attribute__ ((__const__))
207 # define ATTRIBUTE_PURE __attribute__ ((__pure__))
208 #else
209 # define ATTRIBUTE_CONST /* empty */
210 # define ATTRIBUTE_PURE /* empty */
211 #endif
212 
213 #if !defined _Noreturn && __STDC_VERSION__ < 201112
214 # if 2 < __GNUC__ + (8 <= __GNUC_MINOR__)
215 #  define _Noreturn __attribute__ ((__noreturn__))
216 # else
217 #  define _Noreturn
218 # endif
219 #endif
220 
221 #if __STDC_VERSION__ < 199901 && !defined restrict
222 # define restrict /* empty */
223 #endif
224 
225 /*
226 ** Workarounds for compilers/systems.
227 */
228 
229 /*
230 ** Some time.h implementations don't declare asctime_r.
231 ** Others might define it as a macro.
232 ** Fix the former without affecting the latter.
233 */
234 
235 #ifndef asctime_r
236 extern char *	asctime_r(struct tm const *, char *);
237 #endif
238 
239 /*
240 ** Compile with -Dtime_tz=T to build the tz package with a private
241 ** time_t type equivalent to T rather than the system-supplied time_t.
242 ** This debugging feature can test unusual design decisions
243 ** (e.g., time_t wider than 'long', or unsigned time_t) even on
244 ** typical platforms.
245 */
246 #ifdef time_tz
247 static time_t sys_time(time_t *x) { return time(x); }
248 
249 # undef  ctime
250 # define ctime tz_ctime
251 # undef  ctime_r
252 # define ctime_r tz_ctime_r
253 # undef  difftime
254 # define difftime tz_difftime
255 # undef  gmtime
256 # define gmtime tz_gmtime
257 # undef  gmtime_r
258 # define gmtime_r tz_gmtime_r
259 # undef  localtime
260 # define localtime tz_localtime
261 # undef  localtime_r
262 # define localtime_r tz_localtime_r
263 # undef  mktime
264 # define mktime tz_mktime
265 # undef  time
266 # define time tz_time
267 # undef  time_t
268 # define time_t tz_time_t
269 
270 typedef time_tz time_t;
271 
272 char *ctime(time_t const *);
273 char *ctime_r(time_t const *, char *);
274 double difftime(time_t, time_t);
275 struct tm *gmtime(time_t const *);
276 struct tm *gmtime_r(time_t const *restrict, struct tm *restrict);
277 struct tm *localtime(time_t const *);
278 struct tm *localtime_r(time_t const *restrict, struct tm *restrict);
279 time_t mktime(struct tm *);
280 
281 static time_t
282 time(time_t *p)
283 {
284 	time_t r = sys_time(0);
285 	if (p)
286 		*p = r;
287 	return r;
288 }
289 #endif
290 
291 /*
292 ** Private function declarations.
293 */
294 
295 char *		icatalloc(char * old, const char * new);
296 char *		icpyalloc(const char * string);
297 const char *	scheck(const char * string, const char * format);
298 
299 /*
300 ** Finally, some convenience items.
301 */
302 
303 #ifndef TRUE
304 #define TRUE	1
305 #endif /* !defined TRUE */
306 
307 #ifndef FALSE
308 #define FALSE	0
309 #endif /* !defined FALSE */
310 
311 #ifndef TYPE_BIT
312 #define TYPE_BIT(type)	(sizeof (type) * CHAR_BIT)
313 #endif /* !defined TYPE_BIT */
314 
315 #ifndef TYPE_SIGNED
316 #define TYPE_SIGNED(type) (/*CONSTCOND*/((type) -1) < 0)
317 #endif /* !defined TYPE_SIGNED */
318 
319 /*
320 ** Since the definition of TYPE_INTEGRAL contains floating point numbers,
321 ** it cannot be used in preprocessor directives.
322 */
323 
324 #ifndef TYPE_INTEGRAL
325 #define TYPE_INTEGRAL(type) (/*CONSTCOND*/((type) 0.5) != 0.5)
326 #endif /* !defined TYPE_INTEGRAL */
327 
328 #ifndef INT_STRLEN_MAXIMUM
329 /*
330 ** 302 / 1000 is log10(2.0) rounded up.
331 ** Subtract one for the sign bit if the type is signed;
332 ** add one for integer division truncation;
333 ** add one more for a minus sign if the type is signed.
334 */
335 #define INT_STRLEN_MAXIMUM(type) \
336 	((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
337 	1 + TYPE_SIGNED(type))
338 #endif /* !defined INT_STRLEN_MAXIMUM */
339 
340 /*
341 ** INITIALIZE(x)
342 */
343 
344 #ifndef GNUC_or_lint
345 #ifdef lint
346 #define GNUC_or_lint
347 #endif /* defined lint */
348 #ifndef lint
349 #ifdef __GNUC__
350 #define GNUC_or_lint
351 #endif /* defined __GNUC__ */
352 #endif /* !defined lint */
353 #endif /* !defined GNUC_or_lint */
354 
355 #ifndef INITIALIZE
356 #ifdef GNUC_or_lint
357 #define INITIALIZE(x)	((x) = 0)
358 #endif /* defined GNUC_or_lint */
359 #ifndef GNUC_or_lint
360 #define INITIALIZE(x)
361 #endif /* !defined GNUC_or_lint */
362 #endif /* !defined INITIALIZE */
363 
364 /*
365 ** For the benefit of GNU folk...
366 ** `_(MSGID)' uses the current locale's message library string for MSGID.
367 ** The default is to use gettext if available, and use MSGID otherwise.
368 */
369 
370 #ifndef _
371 #if HAVE_GETTEXT
372 #define _(msgid) gettext(msgid)
373 #else /* !HAVE_GETTEXT */
374 #define _(msgid) msgid
375 #endif /* !HAVE_GETTEXT */
376 #endif /* !defined _ */
377 
378 #ifndef TZ_DOMAIN
379 #define TZ_DOMAIN "tz"
380 #endif /* !defined TZ_DOMAIN */
381 
382 #if HAVE_INCOMPATIBLE_CTIME_R
383 #undef asctime_r
384 #undef ctime_r
385 char *asctime_r(struct tm const *, char *);
386 char *ctime_r(time_t const *, char *);
387 #endif /* HAVE_INCOMPATIBLE_CTIME_R */
388 
389 #ifndef YEARSPERREPEAT
390 #define YEARSPERREPEAT		400	/* years before a Gregorian repeat */
391 #endif /* !defined YEARSPERREPEAT */
392 
393 /*
394 ** The Gregorian year averages 365.2425 days, which is 31556952 seconds.
395 */
396 
397 #ifndef AVGSECSPERYEAR
398 #define AVGSECSPERYEAR		31556952L
399 #endif /* !defined AVGSECSPERYEAR */
400 
401 #ifndef SECSPERREPEAT
402 #define SECSPERREPEAT		((int_fast64_t) YEARSPERREPEAT * (int_fast64_t) AVGSECSPERYEAR)
403 #endif /* !defined SECSPERREPEAT */
404 
405 #ifndef SECSPERREPEAT_BITS
406 #define SECSPERREPEAT_BITS	34	/* ceil(log2(SECSPERREPEAT)) */
407 #endif /* !defined SECSPERREPEAT_BITS */
408 
409 /*
410 ** UNIX was a registered trademark of The Open Group in 2003.
411 */
412 
413 #endif /* !defined PRIVATE_H */
414