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