xref: /netbsd-src/lib/libc/time/private.h (revision d909946ca08dceb44d7d0f22ec9488679695d976)
1 /*	$NetBSD: private.h,v 1.45 2016/03/15 15:16:01 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_GETTEXT=1'.
35 */
36 
37 #ifndef HAVE_DECL_ASCTIME_R
38 #define HAVE_DECL_ASCTIME_R 1
39 #endif
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_POSIX_DECLS
54 #define HAVE_POSIX_DECLS 1
55 #endif
56 
57 #ifndef HAVE_STRDUP
58 #define HAVE_STRDUP 1
59 #endif
60 
61 #ifndef HAVE_SYMLINK
62 #define HAVE_SYMLINK		1
63 #endif /* !defined HAVE_SYMLINK */
64 
65 #ifndef HAVE_SYS_STAT_H
66 #define HAVE_SYS_STAT_H		1
67 #endif /* !defined HAVE_SYS_STAT_H */
68 
69 #ifndef HAVE_SYS_WAIT_H
70 #define HAVE_SYS_WAIT_H		1
71 #endif /* !defined HAVE_SYS_WAIT_H */
72 
73 #ifndef HAVE_UNISTD_H
74 #define HAVE_UNISTD_H		1
75 #endif /* !defined HAVE_UNISTD_H */
76 
77 #ifndef HAVE_UTMPX_H
78 #define HAVE_UTMPX_H		1
79 #endif /* !defined HAVE_UTMPX_H */
80 
81 #ifndef NETBSD_INSPIRED
82 # define NETBSD_INSPIRED 1
83 #endif
84 
85 #if HAVE_INCOMPATIBLE_CTIME_R
86 #define asctime_r _incompatible_asctime_r
87 #define ctime_r _incompatible_ctime_r
88 #endif /* HAVE_INCOMPATIBLE_CTIME_R */
89 
90 /* Enable tm_gmtoff and tm_zone on GNUish systems.  */
91 #define _GNU_SOURCE 1
92 /* Fix asctime_r on Solaris 10.  */
93 #define _POSIX_PTHREAD_SEMANTICS 1
94 /* Enable strtoimax on Solaris 10.  */
95 #define __EXTENSIONS__ 1
96 
97 /*
98 ** Nested includes
99 */
100 
101 #ifndef __NetBSD__
102 /* Avoid clashes with NetBSD by renaming NetBSD's declarations.  */
103 #define localtime_rz sys_localtime_rz
104 #define mktime_z sys_mktime_z
105 #define posix2time_z sys_posix2time_z
106 #define time2posix_z sys_time2posix_z
107 #define timezone_t sys_timezone_t
108 #define tzalloc sys_tzalloc
109 #define tzfree sys_tzfree
110 #include <time.h>
111 #undef localtime_rz
112 #undef mktime_z
113 #undef posix2time_z
114 #undef time2posix_z
115 #undef timezone_t
116 #undef tzalloc
117 #undef tzfree
118 #else
119 #include "time.h"
120 #endif
121 
122 #include "sys/types.h"	/* for time_t */
123 #include "stdio.h"
124 #include "string.h"
125 #include "limits.h"	/* for CHAR_BIT et al. */
126 #include "stdlib.h"
127 
128 #include "errno.h"
129 
130 #ifndef ENAMETOOLONG
131 # define ENAMETOOLONG EINVAL
132 #endif
133 #ifndef ENOTSUP
134 # define ENOTSUP EINVAL
135 #endif
136 #ifndef EOVERFLOW
137 # define EOVERFLOW EINVAL
138 #endif
139 
140 #if HAVE_GETTEXT
141 #include "libintl.h"
142 #endif /* HAVE_GETTEXT */
143 
144 #if HAVE_SYS_WAIT_H
145 #include <sys/wait.h>	/* for WIFEXITED and WEXITSTATUS */
146 #endif /* HAVE_SYS_WAIT_H */
147 
148 #ifndef WIFEXITED
149 #define WIFEXITED(status)	(((status) & 0xff) == 0)
150 #endif /* !defined WIFEXITED */
151 #ifndef WEXITSTATUS
152 #define WEXITSTATUS(status)	(((status) >> 8) & 0xff)
153 #endif /* !defined WEXITSTATUS */
154 
155 #if HAVE_UNISTD_H
156 #include "unistd.h"	/* for F_OK, R_OK, and other POSIX goodness */
157 #endif /* HAVE_UNISTD_H */
158 
159 #ifndef HAVE_STRFTIME_L
160 # if _POSIX_VERSION < 200809
161 #  define HAVE_STRFTIME_L 0
162 # else
163 #  define HAVE_STRFTIME_L 1
164 # endif
165 #endif
166 
167 #ifndef F_OK
168 #define F_OK	0
169 #endif /* !defined F_OK */
170 #ifndef R_OK
171 #define R_OK	4
172 #endif /* !defined R_OK */
173 
174 /* Unlike <ctype.h>'s isdigit, this also works if c < 0 | c > UCHAR_MAX. */
175 #define is_digit(c) ((unsigned)(c) - '0' <= 9)
176 
177 /*
178 ** Define HAVE_STDINT_H's default value here, rather than at the
179 ** start, since __GLIBC__'s value depends on previously-included
180 ** files.
181 ** (glibc 2.1 and later have stdint.h, even with pre-C99 compilers.)
182 */
183 #ifndef HAVE_STDINT_H
184 #define HAVE_STDINT_H \
185    (199901 <= __STDC_VERSION__ \
186     || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__)	\
187     || __CYGWIN__)
188 #endif /* !defined HAVE_STDINT_H */
189 
190 #if HAVE_STDINT_H
191 #include "stdint.h"
192 #endif /* !HAVE_STDINT_H */
193 
194 #ifndef HAVE_INTTYPES_H
195 # define HAVE_INTTYPES_H HAVE_STDINT_H
196 #endif
197 #if HAVE_INTTYPES_H
198 # include <inttypes.h>
199 #endif
200 
201 /* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX.  */
202 #ifdef __LONG_LONG_MAX__
203 # ifndef LLONG_MAX
204 #  define LLONG_MAX __LONG_LONG_MAX__
205 # endif
206 # ifndef LLONG_MIN
207 #  define LLONG_MIN (-1 - LLONG_MAX)
208 # endif
209 #endif
210 
211 #ifndef INT_FAST64_MAX
212 # ifdef LLONG_MAX
213 typedef long long	int_fast64_t;
214 #  define INT_FAST64_MIN LLONG_MIN
215 #  define INT_FAST64_MAX LLONG_MAX
216 # else
217 #  if LONG_MAX >> 31 < 0xffffffff
218 Please use a compiler that supports a 64-bit integer type (or wider);
219 you may need to compile with "-DHAVE_STDINT_H".
220 #  endif
221 typedef long		int_fast64_t;
222 #  define INT_FAST64_MIN LONG_MIN
223 #  define INT_FAST64_MAX LONG_MAX
224 # endif
225 #endif
226 
227 #ifndef SCNdFAST64
228 # if INT_FAST64_MAX == LLONG_MAX
229 #  define SCNdFAST64 "lld"
230 # else
231 #  define SCNdFAST64 "ld"
232 # endif
233 #endif
234 
235 #ifndef INT_FAST32_MAX
236 # if INT_MAX >> 31 == 0
237 typedef long int_fast32_t;
238 #  define INT_FAST32_MAX LONG_MAX
239 #  define INT_FAST32_MIN LONG_MIN
240 # else
241 typedef int int_fast32_t;
242 #  define INT_FAST32_MAX INT_MAX
243 #  define INT_FAST32_MIN INT_MIN
244 # endif
245 #endif
246 
247 #ifndef INTMAX_MAX
248 # ifdef LLONG_MAX
249 typedef long long intmax_t;
250 #  define strtoimax strtoll
251 #  define INTMAX_MAX LLONG_MAX
252 #  define INTMAX_MIN LLONG_MIN
253 # else
254 typedef long intmax_t;
255 #  define strtoimax strtol
256 #  define INTMAX_MAX LONG_MAX
257 #  define INTMAX_MIN LONG_MIN
258 # endif
259 #endif
260 
261 #ifndef PRIdMAX
262 # if INTMAX_MAX == LLONG_MAX
263 #  define PRIdMAX "lld"
264 # else
265 #  define PRIdMAX "ld"
266 # endif
267 #endif
268 
269 #ifndef UINT_FAST64_MAX
270 # if defined ULLONG_MAX || defined __LONG_LONG_MAX__
271 typedef unsigned long long uint_fast64_t;
272 # else
273 #  if ULONG_MAX >> 31 >> 1 < 0xffffffff
274 Please use a compiler that supports a 64-bit integer type (or wider);
275 you may need to compile with "-DHAVE_STDINT_H".
276 #  endif
277 typedef unsigned long	uint_fast64_t;
278 # endif
279 #endif
280 
281 #ifndef UINTMAX_MAX
282 # if defined ULLONG_MAX || defined __LONG_LONG_MAX__
283 typedef unsigned long long uintmax_t;
284 # else
285 typedef unsigned long uintmax_t;
286 # endif
287 #endif
288 
289 #ifndef PRIuMAX
290 # if defined ULLONG_MAX || defined __LONG_LONG_MAX__
291 #  define PRIuMAX "llu"
292 # else
293 #  define PRIuMAX "lu"
294 # endif
295 #endif
296 
297 #ifndef INT32_MAX
298 #define INT32_MAX 0x7fffffff
299 #endif /* !defined INT32_MAX */
300 #ifndef INT32_MIN
301 #define INT32_MIN (-1 - INT32_MAX)
302 #endif /* !defined INT32_MIN */
303 
304 #ifndef SIZE_MAX
305 #define SIZE_MAX ((size_t) -1)
306 #endif
307 
308 #if 2 < __GNUC__ + (96 <= __GNUC_MINOR__)
309 # define ATTRIBUTE_CONST __attribute__ ((__const__))
310 # define ATTRIBUTE_PURE __attribute__ ((__pure__))
311 # define ATTRIBUTE_FORMAT(spec) __attribute__ ((__format__ spec))
312 #else
313 # define ATTRIBUTE_CONST /* empty */
314 # define ATTRIBUTE_PURE /* empty */
315 # define ATTRIBUTE_FORMAT(spec) /* empty */
316 #endif
317 
318 #if !defined _Noreturn && __STDC_VERSION__ < 201112
319 # if 2 < __GNUC__ + (8 <= __GNUC_MINOR__)
320 #  define _Noreturn __attribute__ ((__noreturn__))
321 # else
322 #  define _Noreturn
323 # endif
324 #endif
325 
326 #if __STDC_VERSION__ < 199901 && !defined restrict
327 # define restrict /* empty */
328 #endif
329 
330 /*
331 ** Workarounds for compilers/systems.
332 */
333 
334 /*
335 ** Compile with -Dtime_tz=T to build the tz package with a private
336 ** time_t type equivalent to T rather than the system-supplied time_t.
337 ** This debugging feature can test unusual design decisions
338 ** (e.g., time_t wider than 'long', or unsigned time_t) even on
339 ** typical platforms.
340 */
341 #ifdef time_tz
342 # ifdef LOCALTIME_IMPLEMENTATION
343 static time_t sys_time(time_t *x) { return time(x); }
344 # endif
345 
346 typedef time_tz tz_time_t;
347 
348 # undef  ctime
349 # define ctime tz_ctime
350 # undef  ctime_r
351 # define ctime_r tz_ctime_r
352 # undef  difftime
353 # define difftime tz_difftime
354 # undef  gmtime
355 # define gmtime tz_gmtime
356 # undef  gmtime_r
357 # define gmtime_r tz_gmtime_r
358 # undef  localtime
359 # define localtime tz_localtime
360 # undef  localtime_r
361 # define localtime_r tz_localtime_r
362 # undef  localtime_rz
363 # define localtime_rz tz_localtime_rz
364 # undef  mktime
365 # define mktime tz_mktime
366 # undef  mktime_z
367 # define mktime_z tz_mktime_z
368 # undef  offtime
369 # define offtime tz_offtime
370 # undef  posix2time
371 # define posix2time tz_posix2time
372 # undef  posix2time_z
373 # define posix2time_z tz_posix2time_z
374 # undef  time
375 # define time tz_time
376 # undef  time2posix
377 # define time2posix tz_time2posix
378 # undef  time2posix_z
379 # define time2posix_z tz_time2posix_z
380 # undef  time_t
381 # define time_t tz_time_t
382 # undef  timegm
383 # define timegm tz_timegm
384 # undef  timelocal
385 # define timelocal tz_timelocal
386 # undef  timeoff
387 # define timeoff tz_timeoff
388 # undef  tzalloc
389 # define tzalloc tz_tzalloc
390 # undef  tzfree
391 # define tzfree tz_tzfree
392 # undef  tzset
393 # define tzset tz_tzset
394 # undef  tzsetwall
395 # define tzsetwall tz_tzsetwall
396 
397 char *ctime(time_t const *);
398 char *ctime_r(time_t const *, char *);
399 double difftime(time_t, time_t);
400 struct tm *gmtime(time_t const *);
401 struct tm *gmtime_r(time_t const *restrict, struct tm *restrict);
402 struct tm *localtime(time_t const *);
403 struct tm *localtime_r(time_t const *restrict, struct tm *restrict);
404 time_t mktime(struct tm *);
405 time_t time(time_t *);
406 void tzset(void);
407 #endif
408 
409 #if !HAVE_DECL_ASCTIME_R && !defined asctime_r
410 extern char *asctime_r(struct tm const *restrict, char *restrict);
411 #endif
412 
413 #if !HAVE_POSIX_DECLS
414 # if defined(USG_COMPAT) && !defined(__NetBSD__)
415 #  ifndef timezone
416 extern long timezone;
417 #  endif
418 #  ifndef daylight
419 extern int daylight;
420 #  endif
421 # endif
422 #endif
423 
424 #if defined ALTZONE && !defined altzone
425 extern long altzone;
426 #endif
427 
428 /*
429 ** The STD_INSPIRED functions are similar, but most also need
430 ** declarations if time_tz is defined.
431 */
432 
433 #ifdef STD_INSPIRED
434 # if !defined tzsetwall || defined time_tz
435 void tzsetwall(void);
436 # endif
437 # if !defined offtime || defined time_tz
438 struct tm *offtime(time_t const *, long);
439 # endif
440 # if !defined timegm || defined time_tz
441 time_t timegm(struct tm *);
442 # endif
443 # if !defined timelocal || defined time_tz
444 time_t timelocal(struct tm *);
445 # endif
446 # if !defined timeoff || defined time_tz
447 time_t timeoff(struct tm *, long);
448 # endif
449 # if !defined time2posix || defined time_tz
450 time_t time2posix(time_t);
451 # endif
452 # if !defined posix2time || defined time_tz
453 time_t posix2time(time_t);
454 # endif
455 #endif
456 
457 /* Infer TM_ZONE on systems where this information is known, but suppress
458    guessing if NO_TM_ZONE is defined.  Similarly for TM_GMTOFF.  */
459 #if (defined __GLIBC__ \
460      || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
461      || (defined __APPLE__ && defined __MACH__))
462 # if !defined TM_GMTOFF && !defined NO_TM_GMTOFF
463 #  define TM_GMTOFF tm_gmtoff
464 # endif
465 # if !defined TM_ZONE && !defined NO_TM_ZONE
466 #  define TM_ZONE tm_zone
467 # endif
468 #endif
469 
470 /*
471 ** Define functions that are ABI compatible with NetBSD but have
472 ** better prototypes.  NetBSD 6.1.4 defines a pointer type timezone_t
473 ** and labors under the misconception that 'const timezone_t' is a
474 ** pointer to a constant.  This use of 'const' is ineffective, so it
475 ** is not done here.  What we call 'struct state' NetBSD calls
476 ** 'struct __state', but this is a private name so it doesn't matter.
477 */
478 #ifndef __NetBSD__
479 #if NETBSD_INSPIRED
480 typedef struct state *timezone_t;
481 struct tm *localtime_rz(timezone_t restrict, time_t const *restrict,
482 			struct tm *restrict);
483 time_t mktime_z(timezone_t restrict, struct tm *restrict);
484 timezone_t tzalloc(char const *);
485 void tzfree(timezone_t);
486 # ifdef STD_INSPIRED
487 #  if !defined posix2time_z || defined time_tz
488 time_t posix2time_z(timezone_t __restrict, time_t) ATTRIBUTE_PURE;
489 #  endif
490 #  if !defined time2posix_z || defined time_tz
491 time_t time2posix_z(timezone_t __restrict, time_t) ATTRIBUTE_PURE;
492 #  endif
493 # endif
494 #endif
495 #endif
496 
497 /*
498 ** Finally, some convenience items.
499 */
500 
501 #if __STDC_VERSION__ < 199901
502 # define true 1
503 # define false 0
504 # define bool int
505 #else
506 # include <stdbool.h>
507 #endif
508 
509 #ifndef TYPE_BIT
510 #define TYPE_BIT(type)	(sizeof (type) * CHAR_BIT)
511 #endif /* !defined TYPE_BIT */
512 
513 #ifndef TYPE_SIGNED
514 #define TYPE_SIGNED(type) (/*CONSTCOND*/((type) -1) < 0)
515 #endif /* !defined TYPE_SIGNED */
516 
517 #define TWOS_COMPLEMENT(t) (/*CONSTCOND*/(t) ~ (t) 0 < 0)
518 
519 /* Max and min values of the integer type T, of which only the bottom
520    B bits are used, and where the highest-order used bit is considered
521    to be a sign bit if T is signed.  */
522 #define MAXVAL(t, b) /*LINTED*/					\
523   ((t) (((t) 1 << ((b) - 1 - TYPE_SIGNED(t)))			\
524 	- 1 + ((t) 1 << ((b) - 1 - TYPE_SIGNED(t)))))
525 #define MINVAL(t, b)						\
526   ((t) (TYPE_SIGNED(t) ? - TWOS_COMPLEMENT(t) - MAXVAL(t, b) : 0))
527 
528 #ifdef LOCALTIME_IMPLEMENTATION
529 /* The minimum and maximum finite time values.  This assumes no padding.  */
530 static time_t const time_t_min = MINVAL(time_t, TYPE_BIT(time_t));
531 static time_t const time_t_max = MAXVAL(time_t, TYPE_BIT(time_t));
532 #endif
533 
534 #ifndef INT_STRLEN_MAXIMUM
535 /*
536 ** 302 / 1000 is log10(2.0) rounded up.
537 ** Subtract one for the sign bit if the type is signed;
538 ** add one for integer division truncation;
539 ** add one more for a minus sign if the type is signed.
540 */
541 #define INT_STRLEN_MAXIMUM(type) \
542 	((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
543 	1 + TYPE_SIGNED(type))
544 #endif /* !defined INT_STRLEN_MAXIMUM */
545 
546 /*
547 ** INITIALIZE(x)
548 */
549 
550 #if defined(__GNUC__) || defined(__lint__)
551 # define INITIALIZE(x)	((x) = 0)
552 #else
553 # define INITIALIZE(x)
554 #endif
555 
556 #ifndef UNINIT_TRAP
557 # define UNINIT_TRAP 0
558 #endif
559 
560 /*
561 ** For the benefit of GNU folk...
562 ** '_(MSGID)' uses the current locale's message library string for MSGID.
563 ** The default is to use gettext if available, and use MSGID otherwise.
564 */
565 
566 #ifndef _
567 #if HAVE_GETTEXT
568 #define _(msgid) gettext(msgid)
569 #else /* !HAVE_GETTEXT */
570 #define _(msgid) msgid
571 #endif /* !HAVE_GETTEXT */
572 #endif /* !defined _ */
573 
574 #if !defined TZ_DOMAIN && defined HAVE_GETTEXT
575 # define TZ_DOMAIN "tz"
576 #endif
577 
578 #if HAVE_INCOMPATIBLE_CTIME_R
579 #undef asctime_r
580 #undef ctime_r
581 char *asctime_r(struct tm const *, char *);
582 char *ctime_r(time_t const *, char *);
583 #endif /* HAVE_INCOMPATIBLE_CTIME_R */
584 
585 #ifndef YEARSPERREPEAT
586 #define YEARSPERREPEAT		400	/* years before a Gregorian repeat */
587 #endif /* !defined YEARSPERREPEAT */
588 
589 /*
590 ** The Gregorian year averages 365.2425 days, which is 31556952 seconds.
591 */
592 
593 #ifndef AVGSECSPERYEAR
594 #define AVGSECSPERYEAR		31556952L
595 #endif /* !defined AVGSECSPERYEAR */
596 
597 #ifndef SECSPERREPEAT
598 #define SECSPERREPEAT		((int_fast64_t) YEARSPERREPEAT * (int_fast64_t) AVGSECSPERYEAR)
599 #endif /* !defined SECSPERREPEAT */
600 
601 #ifndef SECSPERREPEAT_BITS
602 #define SECSPERREPEAT_BITS	34	/* ceil(log2(SECSPERREPEAT)) */
603 #endif /* !defined SECSPERREPEAT_BITS */
604 
605 #endif /* !defined PRIVATE_H */
606