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