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