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