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