(errno, CHAR_BIT): Remove decls;
[platform/upstream/coreutils.git] / src / system.h
1 /* system-dependent definitions for fileutils, textutils, and sh-utils packages.
2    Copyright (C) 1989, 1991-2004 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 #include <alloca.h>
19
20 /* Include sys/types.h before this file.  */
21
22 #if 2 <= __GLIBC__ && 2 <= __GLIBC_MINOR__
23 # if ! defined _SYS_TYPES_H
24 you must include <sys/types.h> before including this file
25 # endif
26 #endif
27
28 #include <sys/stat.h>
29
30 #if !defined HAVE_MKFIFO
31 # define mkfifo(path, mode) (mknod ((path), (mode) | S_IFIFO, 0))
32 #endif
33
34 #if HAVE_SYS_PARAM_H
35 # include <sys/param.h>
36 #endif
37
38 /* <unistd.h> should be included before any preprocessor test
39    of _POSIX_VERSION.  */
40 #if HAVE_UNISTD_H
41 # include <unistd.h>
42 #endif
43
44 #ifndef STDIN_FILENO
45 # define STDIN_FILENO 0
46 #endif
47
48 #ifndef STDOUT_FILENO
49 # define STDOUT_FILENO 1
50 #endif
51
52 #ifndef STDERR_FILENO
53 # define STDERR_FILENO 2
54 #endif
55
56
57 /* limits.h must come before pathmax.h because limits.h on some systems
58    undefs PATH_MAX, whereas pathmax.h sets PATH_MAX.  */
59 #include <limits.h>
60
61 #include "pathmax.h"
62 #include "localedir.h"
63
64 #if TIME_WITH_SYS_TIME
65 # include <sys/time.h>
66 # include <time.h>
67 #else
68 # if HAVE_SYS_TIME_H
69 #  include <sys/time.h>
70 # else
71 #  include <time.h>
72 # endif
73 #endif
74
75 /* Since major is a function on SVR4, we can't use `ifndef major'.  */
76 #if MAJOR_IN_MKDEV
77 # include <sys/mkdev.h>
78 # define HAVE_MAJOR
79 #endif
80 #if MAJOR_IN_SYSMACROS
81 # include <sys/sysmacros.h>
82 # define HAVE_MAJOR
83 #endif
84 #ifdef major                    /* Might be defined in sys/types.h.  */
85 # define HAVE_MAJOR
86 #endif
87
88 #ifndef HAVE_MAJOR
89 # define major(dev)  (((dev) >> 8) & 0xff)
90 # define minor(dev)  ((dev) & 0xff)
91 # define makedev(maj, min)  (((maj) << 8) | (min))
92 #endif
93 #undef HAVE_MAJOR
94
95 #if ! defined makedev && defined mkdev
96 # define makedev(maj, min)  mkdev (maj, min)
97 #endif
98
99 #if HAVE_UTIME_H
100 # include <utime.h>
101 #endif
102
103 /* Some systems (even some that do have <utime.h>) don't declare this
104    structure anywhere.  */
105 #ifndef HAVE_STRUCT_UTIMBUF
106 struct utimbuf
107 {
108   long actime;
109   long modtime;
110 };
111 #endif
112
113 /* Don't use bcopy!  Use memmove if source and destination may overlap,
114    memcpy otherwise.  */
115
116 #include <string.h>
117 #if ! HAVE_DECL_MEMRCHR
118 void *memrchr (const void *, int, size_t);
119 #endif
120
121 #include <errno.h>
122
123 /* Some systems don't define the following symbols.  */
124 #ifndef ENOSYS
125 # define ENOSYS (-1)
126 #endif
127 #ifndef EISDIR
128 # define EISDIR (-1)
129 #endif
130
131 #include <stdbool.h>
132
133 #define getopt system_getopt
134 #include <stdlib.h>
135 #undef getopt
136
137 /* The following test is to work around the gross typo in
138    systems like Sony NEWS-OS Release 4.0C, whereby EXIT_FAILURE
139    is defined to 0, not 1.  */
140 #if !EXIT_FAILURE
141 # undef EXIT_FAILURE
142 # define EXIT_FAILURE 1
143 #endif
144
145 #ifndef EXIT_SUCCESS
146 # define EXIT_SUCCESS 0
147 #endif
148
149 /* Exit statuses for programs like 'env' that exec other programs.
150    EXIT_FAILURE might not be 1, so use EXIT_FAIL in such programs.  */
151 enum
152 {
153   EXIT_FAIL = 1,
154   EXIT_CANNOT_INVOKE = 126,
155   EXIT_ENOENT = 127
156 };
157
158 #include "exitfail.h"
159
160 /* Set exit_failure to STATUS if that's not the default already.  */
161 static inline void
162 initialize_exit_failure (int status)
163 {
164   if (status != EXIT_FAILURE)
165     exit_failure = status;
166 }
167
168 #if HAVE_FCNTL_H
169 # include <fcntl.h>
170 #else
171 # include <sys/file.h>
172 #endif
173
174 #if !defined SEEK_SET
175 # define SEEK_SET 0
176 # define SEEK_CUR 1
177 # define SEEK_END 2
178 #endif
179 #ifndef F_OK
180 # define F_OK 0
181 # define X_OK 1
182 # define W_OK 2
183 # define R_OK 4
184 #endif
185
186 /* For systems that distinguish between text and binary I/O.
187    O_BINARY is usually declared in fcntl.h  */
188 #if !defined O_BINARY && defined _O_BINARY
189   /* For MSC-compatible compilers.  */
190 # define O_BINARY _O_BINARY
191 # define O_TEXT _O_TEXT
192 #endif
193
194 #if !defined O_DIRECT
195 # define O_DIRECT 0
196 #endif
197
198 #if !defined O_DSYNC
199 # define O_DSYNC 0
200 #endif
201
202 #if !defined O_NDELAY
203 # define O_NDELAY 0
204 #endif
205
206 #if !defined O_NONBLOCK
207 # define O_NONBLOCK O_NDELAY
208 #endif
209
210 #if !defined O_NOCTTY
211 # define O_NOCTTY 0
212 #endif
213
214 #if !defined O_NOFOLLOW
215 # define O_NOFOLLOW 0
216 #endif
217
218 #if !defined O_RSYNC
219 # define O_RSYNC 0
220 #endif
221
222 #if !defined O_SYNC
223 # define O_SYNC 0
224 #endif
225
226 #ifdef __BEOS__
227   /* BeOS 5 has O_BINARY and O_TEXT, but they have no effect.  */
228 # undef O_BINARY
229 # undef O_TEXT
230 #endif
231
232 #if O_BINARY
233 # ifndef __DJGPP__
234 #  define setmode _setmode
235 #  define fileno(_fp) _fileno (_fp)
236 # endif /* not DJGPP */
237 # define SET_MODE(_f, _m) setmode (_f, _m)
238 # define SET_BINARY(_f) do {if (!isatty(_f)) setmode (_f, O_BINARY);} while (0)
239 # define SET_BINARY2(_f1, _f2)          \
240   do {                                  \
241     if (!isatty (_f1))                  \
242       {                                 \
243         setmode (_f1, O_BINARY);        \
244         if (!isatty (_f2))              \
245           setmode (_f2, O_BINARY);      \
246       }                                 \
247   } while(0)
248 #else
249 # define SET_MODE(_f, _m) (void)0
250 # define SET_BINARY(f) (void)0
251 # define SET_BINARY2(f1,f2) (void)0
252 # define O_BINARY 0
253 # define O_TEXT 0
254 #endif /* O_BINARY */
255
256 #if HAVE_DIRENT_H
257 # include <dirent.h>
258 # define NLENGTH(direct) (strlen((direct)->d_name))
259 #else /* not HAVE_DIRENT_H */
260 # define dirent direct
261 # define NLENGTH(direct) ((direct)->d_namlen)
262 # if HAVE_SYS_NDIR_H
263 #  include <sys/ndir.h>
264 # endif /* HAVE_SYS_NDIR_H */
265 # if HAVE_SYS_DIR_H
266 #  include <sys/dir.h>
267 # endif /* HAVE_SYS_DIR_H */
268 # if HAVE_NDIR_H
269 #  include <ndir.h>
270 # endif /* HAVE_NDIR_H */
271 #endif /* HAVE_DIRENT_H */
272
273 #if CLOSEDIR_VOID
274 /* Fake a return value. */
275 # define CLOSEDIR(d) (closedir (d), 0)
276 #else
277 # define CLOSEDIR(d) closedir (d)
278 #endif
279
280 /* Get or fake the disk device blocksize.
281    Usually defined by sys/param.h (if at all).  */
282 #if !defined DEV_BSIZE && defined BSIZE
283 # define DEV_BSIZE BSIZE
284 #endif
285 #if !defined DEV_BSIZE && defined BBSIZE /* SGI */
286 # define DEV_BSIZE BBSIZE
287 #endif
288 #ifndef DEV_BSIZE
289 # define DEV_BSIZE 4096
290 #endif
291
292 /* Extract or fake data from a `struct stat'.
293    ST_BLKSIZE: Preferred I/O blocksize for the file, in bytes.
294    ST_NBLOCKS: Number of blocks in the file, including indirect blocks.
295    ST_NBLOCKSIZE: Size of blocks used when calculating ST_NBLOCKS.  */
296 #ifndef HAVE_STRUCT_STAT_ST_BLOCKS
297 # define ST_BLKSIZE(statbuf) DEV_BSIZE
298 # if defined _POSIX_SOURCE || !defined BSIZE /* fileblocks.c uses BSIZE.  */
299 #  define ST_NBLOCKS(statbuf) \
300   ((statbuf).st_size / ST_NBLOCKSIZE + ((statbuf).st_size % ST_NBLOCKSIZE != 0))
301 # else /* !_POSIX_SOURCE && BSIZE */
302 #  define ST_NBLOCKS(statbuf) \
303   (S_ISREG ((statbuf).st_mode) \
304    || S_ISDIR ((statbuf).st_mode) \
305    ? st_blocks ((statbuf).st_size) : 0)
306 # endif /* !_POSIX_SOURCE && BSIZE */
307 #else /* HAVE_STRUCT_STAT_ST_BLOCKS */
308 /* Some systems, like Sequents, return st_blksize of 0 on pipes.
309    Also, when running `rsh hpux11-system cat any-file', cat would
310    determine that the output stream had an st_blksize of 2147421096.
311    So here we arbitrarily limit the `optimal' block size to 4MB.
312    If anyone knows of a system for which the legitimate value for
313    st_blksize can exceed 4MB, please report it as a bug in this code.  */
314 # define ST_BLKSIZE(statbuf) ((0 < (statbuf).st_blksize \
315                                && (statbuf).st_blksize <= (1 << 22)) /* 4MB */ \
316                               ? (statbuf).st_blksize : DEV_BSIZE)
317 # if defined hpux || defined __hpux__ || defined __hpux
318 /* HP-UX counts st_blocks in 1024-byte units.
319    This loses when mixing HP-UX and BSD file systems with NFS.  */
320 #  define ST_NBLOCKSIZE 1024
321 # else /* !hpux */
322 #  if defined _AIX && defined _I386
323 /* AIX PS/2 counts st_blocks in 4K units.  */
324 #   define ST_NBLOCKSIZE (4 * 1024)
325 #  else /* not AIX PS/2 */
326 #   if defined _CRAY
327 #    define ST_NBLOCKS(statbuf) \
328   (S_ISREG ((statbuf).st_mode) \
329    || S_ISDIR ((statbuf).st_mode) \
330    ? (statbuf).st_blocks * ST_BLKSIZE(statbuf)/ST_NBLOCKSIZE : 0)
331 #   endif /* _CRAY */
332 #  endif /* not AIX PS/2 */
333 # endif /* !hpux */
334 #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
335
336 #ifndef ST_NBLOCKS
337 # define ST_NBLOCKS(statbuf) ((statbuf).st_blocks)
338 #endif
339
340 #ifndef ST_NBLOCKSIZE
341 # define ST_NBLOCKSIZE 512
342 #endif
343
344 /* Redirection and wildcarding when done by the utility itself.
345    Generally a noop, but used in particular for native VMS. */
346 #ifndef initialize_main
347 # define initialize_main(ac, av)
348 #endif
349
350 #include "stat-macros.h"
351
352 #include "timespec.h"
353
354 #ifndef RETSIGTYPE
355 # define RETSIGTYPE void
356 #endif
357
358 #ifdef __DJGPP__
359   /* We need the declaration of setmode.  */
360 # include <io.h>
361   /* We need the declaration of __djgpp_set_ctrl_c.  */
362 # include <sys/exceptn.h>
363 #endif
364
365 #if HAVE_INTTYPES_H
366 # include <inttypes.h>
367 #endif
368 #if HAVE_STDINT_H
369 # include <stdint.h>
370 #endif
371
372 #if !defined PRIdMAX || PRI_MACROS_BROKEN
373 # undef PRIdMAX
374 # define PRIdMAX (sizeof (uintmax_t) == sizeof (long int) ? "ld" : "lld")
375 #endif
376 #if !defined PRIoMAX || PRI_MACROS_BROKEN
377 # undef PRIoMAX
378 # define PRIoMAX (sizeof (uintmax_t) == sizeof (long int) ? "lo" : "llo")
379 #endif
380 #if !defined PRIuMAX || PRI_MACROS_BROKEN
381 # undef PRIuMAX
382 # define PRIuMAX (sizeof (uintmax_t) == sizeof (long int) ? "lu" : "llu")
383 #endif
384 #if !defined PRIxMAX || PRI_MACROS_BROKEN
385 # undef PRIxMAX
386 # define PRIxMAX (sizeof (uintmax_t) == sizeof (long int) ? "lx" : "llx")
387 #endif
388
389 #include <ctype.h>
390
391 /* Jim Meyering writes:
392
393    "... Some ctype macros are valid only for character codes that
394    isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
395    using /bin/cc or gcc but without giving an ansi option).  So, all
396    ctype uses should be through macros like ISPRINT...  If
397    STDC_HEADERS is defined, then autoconf has verified that the ctype
398    macros don't need to be guarded with references to isascii. ...
399    Defining isascii to 1 should let any compiler worth its salt
400    eliminate the && through constant folding."
401
402    Bruno Haible adds:
403
404    "... Furthermore, isupper(c) etc. have an undefined result if c is
405    outside the range -1 <= c <= 255. One is tempted to write isupper(c)
406    with c being of type `char', but this is wrong if c is an 8-bit
407    character >= 128 which gets sign-extended to a negative value.
408    The macro ISUPPER protects against this as well."  */
409
410 #if STDC_HEADERS || (!defined (isascii) && !HAVE_ISASCII)
411 # define IN_CTYPE_DOMAIN(c) 1
412 #else
413 # define IN_CTYPE_DOMAIN(c) isascii(c)
414 #endif
415
416 #ifdef isblank
417 # define ISBLANK(c) (IN_CTYPE_DOMAIN (c) && isblank (c))
418 #else
419 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
420 #endif
421 #ifdef isgraph
422 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isgraph (c))
423 #else
424 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isprint (c) && !isspace (c))
425 #endif
426
427 /* This is defined in <sys/euc.h> on at least Solaris2.6 systems.  */
428 #undef ISPRINT
429
430 #define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
431 #define ISALNUM(c) (IN_CTYPE_DOMAIN (c) && isalnum (c))
432 #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
433 #define ISCNTRL(c) (IN_CTYPE_DOMAIN (c) && iscntrl (c))
434 #define ISLOWER(c) (IN_CTYPE_DOMAIN (c) && islower (c))
435 #define ISPUNCT(c) (IN_CTYPE_DOMAIN (c) && ispunct (c))
436 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
437 #define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
438 #define ISXDIGIT(c) (IN_CTYPE_DOMAIN (c) && isxdigit (c))
439 #define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
440
441 #if STDC_HEADERS
442 # define TOLOWER(Ch) tolower (Ch)
443 # define TOUPPER(Ch) toupper (Ch)
444 #else
445 # define TOLOWER(Ch) (ISUPPER (Ch) ? tolower (Ch) : (Ch))
446 # define TOUPPER(Ch) (ISLOWER (Ch) ? toupper (Ch) : (Ch))
447 #endif
448
449 /* ISDIGIT differs from ISDIGIT_LOCALE, as follows:
450    - Its arg may be any int or unsigned int; it need not be an unsigned char.
451    - It's guaranteed to evaluate its argument exactly once.
452    - It's typically faster.
453    POSIX says that only '0' through '9' are digits.  Prefer ISDIGIT to
454    ISDIGIT_LOCALE unless it's important to use the locale's definition
455    of `digit' even when the host does not conform to POSIX.  */
456 #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
457
458 /* Convert a possibly-signed character to an unsigned character.  This is
459    a bit safer than casting to unsigned char, since it catches some type
460    errors that the cast doesn't.  */
461 static inline unsigned char to_uchar (char ch) { return ch; }
462
463 /* Take care of NLS matters.  */
464
465 #if HAVE_LOCALE_H
466 # include <locale.h>
467 #else
468 # define setlocale(Category, Locale) /* empty */
469 #endif
470
471 #include "gettext.h"
472 #if ! ENABLE_NLS
473 # undef textdomain
474 # define textdomain(Domainname) /* empty */
475 # undef bindtextdomain
476 # define bindtextdomain(Domainname, Dirname) /* empty */
477 #endif
478
479 #define _(msgid) gettext (msgid)
480 #define N_(msgid) msgid
481
482 #ifndef HAVE_SETLOCALE
483 # define HAVE_SETLOCALE 0
484 #endif
485
486 #define STREQ(a, b) (strcmp ((a), (b)) == 0)
487
488 #if !HAVE_DECL_FREE
489 void free ();
490 #endif
491
492 #if !HAVE_DECL_MALLOC
493 char *malloc ();
494 #endif
495
496 #if !HAVE_DECL_MEMCHR
497 char *memchr ();
498 #endif
499
500 #if !HAVE_DECL_REALLOC
501 char *realloc ();
502 #endif
503
504 #if !HAVE_DECL_STPCPY
505 # ifndef stpcpy
506 char *stpcpy ();
507 # endif
508 #endif
509
510 #if !HAVE_DECL_STRNDUP
511 char *strndup ();
512 #endif
513
514 #if !HAVE_DECL_STRSTR
515 char *strstr ();
516 #endif
517
518 #if !HAVE_DECL_GETENV
519 char *getenv ();
520 #endif
521
522 #if !HAVE_DECL_LSEEK
523 off_t lseek ();
524 #endif
525
526 /* This is needed on some AIX systems.  */
527 #if !HAVE_DECL_STRTOUL
528 unsigned long strtoul ();
529 #endif
530
531 #if !HAVE_DECL_GETLOGIN
532 char *getlogin ();
533 #endif
534
535 #if !HAVE_DECL_TTYNAME
536 char *ttyname ();
537 #endif
538
539 #if !HAVE_DECL_GETEUID
540 uid_t geteuid ();
541 #endif
542
543 #if !HAVE_DECL_GETPWUID
544 struct passwd *getpwuid ();
545 #endif
546
547 #if !HAVE_DECL_GETGRGID
548 struct group *getgrgid ();
549 #endif
550
551 #if !HAVE_DECL_GETUID
552 uid_t getuid ();
553 #endif
554
555 #include "xalloc.h"
556
557 #if ! defined HAVE_MEMPCPY && ! defined mempcpy
558 /* Be CAREFUL that there are no side effects in N.  */
559 # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
560 #endif
561
562 /* Include automatically-generated macros for unlocked I/O.  */
563 #include "unlocked-io.h"
564
565 #define SAME_INODE(Stat_buf_1, Stat_buf_2) \
566   ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
567    && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
568
569 #define DOT_OR_DOTDOT(Basename) \
570   (Basename[0] == '.' && (Basename[1] == '\0' \
571                           || (Basename[1] == '.' && Basename[2] == '\0')))
572
573 /* A wrapper for readdir so that callers don't see entries for `.' or `..'.  */
574 static inline struct dirent const *
575 readdir_ignoring_dot_and_dotdot (DIR *dirp)
576 {
577   while (1)
578     {
579       struct dirent const *dp = readdir (dirp);
580       if (dp == NULL || ! DOT_OR_DOTDOT (dp->d_name))
581         return dp;
582     }
583 }
584
585 #if SETVBUF_REVERSED
586 # define SETVBUF(Stream, Buffer, Type, Size) \
587     setvbuf (Stream, Type, Buffer, Size)
588 #else
589 # define SETVBUF(Stream, Buffer, Type, Size) \
590     setvbuf (Stream, Buffer, Type, Size)
591 #endif
592
593 /* Factor out some of the common --help and --version processing code.  */
594
595 /* These enum values cannot possibly conflict with the option values
596    ordinarily used by commands, including CHAR_MAX + 1, etc.  Avoid
597    CHAR_MIN - 1, as it may equal -1, the getopt end-of-options value.  */
598 enum
599 {
600   GETOPT_HELP_CHAR = (CHAR_MIN - 2),
601   GETOPT_VERSION_CHAR = (CHAR_MIN - 3)
602 };
603
604 #define GETOPT_HELP_OPTION_DECL \
605   "help", no_argument, 0, GETOPT_HELP_CHAR
606 #define GETOPT_VERSION_OPTION_DECL \
607   "version", no_argument, 0, GETOPT_VERSION_CHAR
608
609 #define case_GETOPT_HELP_CHAR                   \
610   case GETOPT_HELP_CHAR:                        \
611     usage (EXIT_SUCCESS);                       \
612     break;
613
614 #define HELP_OPTION_DESCRIPTION \
615   _("      --help     display this help and exit\n")
616 #define VERSION_OPTION_DESCRIPTION \
617   _("      --version  output version information and exit\n")
618
619 #include "closeout.h"
620 #include "version-etc.h"
621
622 #define case_GETOPT_VERSION_CHAR(Program_name, Authors)                 \
623   case GETOPT_VERSION_CHAR:                                             \
624     version_etc (stdout, Program_name, GNU_PACKAGE, VERSION, Authors,   \
625                  (char *) NULL);                                        \
626     exit (EXIT_SUCCESS);                                                \
627     break;
628
629 #ifndef MAX
630 # define MAX(a, b) ((a) > (b) ? (a) : (b))
631 #endif
632
633 #ifndef MIN
634 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
635 #endif
636
637 /* The extra casts work around common compiler bugs.  */
638 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
639 /* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
640    It is necessary at least when t == time_t.  */
641 #define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
642                               ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) : (t) 0))
643 #define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
644
645 /* Upper bound on the string length of an integer converted to string.
646    302 / 1000 is ceil (log10 (2.0)).  Subtract 1 for the sign bit;
647    add 1 for integer division truncation; add 1 more for a minus sign.  */
648 #define INT_STRLEN_BOUND(t) ((sizeof (t) * CHAR_BIT - 1) * 302 / 1000 + 2)
649
650 #ifndef CHAR_MIN
651 # define CHAR_MIN TYPE_MINIMUM (char)
652 #endif
653
654 #ifndef CHAR_MAX
655 # define CHAR_MAX TYPE_MAXIMUM (char)
656 #endif
657
658 #ifndef SCHAR_MIN
659 # define SCHAR_MIN (-1 - SCHAR_MAX)
660 #endif
661
662 #ifndef SCHAR_MAX
663 # define SCHAR_MAX (CHAR_MAX == UCHAR_MAX ? CHAR_MAX / 2 : CHAR_MAX)
664 #endif
665
666 #ifndef UCHAR_MAX
667 # define UCHAR_MAX TYPE_MAXIMUM (unsigned char)
668 #endif
669
670 #ifndef SHRT_MIN
671 # define SHRT_MIN TYPE_MINIMUM (short int)
672 #endif
673
674 #ifndef SHRT_MAX
675 # define SHRT_MAX TYPE_MAXIMUM (short int)
676 #endif
677
678 #ifndef INT_MAX
679 # define INT_MAX TYPE_MAXIMUM (int)
680 #endif
681
682 #ifndef INT_MIN
683 # define INT_MIN TYPE_MINIMUM (int)
684 #endif
685
686 #ifndef UINT_MAX
687 # define UINT_MAX TYPE_MAXIMUM (unsigned int)
688 #endif
689
690 #ifndef LONG_MAX
691 # define LONG_MAX TYPE_MAXIMUM (long int)
692 #endif
693
694 #ifndef ULONG_MAX
695 # define ULONG_MAX TYPE_MAXIMUM (unsigned long int)
696 #endif
697
698 #ifndef SIZE_MAX
699 # define SIZE_MAX TYPE_MAXIMUM (size_t)
700 #endif
701
702 #ifndef SSIZE_MAX
703 # define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
704 #endif
705
706 #ifndef UINTMAX_MAX
707 # define UINTMAX_MAX TYPE_MAXIMUM (uintmax_t)
708 #endif
709
710 #ifndef OFF_T_MIN
711 # define OFF_T_MIN TYPE_MINIMUM (off_t)
712 #endif
713
714 #ifndef OFF_T_MAX
715 # define OFF_T_MAX TYPE_MAXIMUM (off_t)
716 #endif
717
718 #ifndef UID_T_MAX
719 # define UID_T_MAX TYPE_MAXIMUM (uid_t)
720 #endif
721
722 #ifndef GID_T_MAX
723 # define GID_T_MAX TYPE_MAXIMUM (gid_t)
724 #endif
725
726 #ifndef PID_T_MAX
727 # define PID_T_MAX TYPE_MAXIMUM (pid_t)
728 #endif
729
730 /* Use this to suppress gcc's `...may be used before initialized' warnings. */
731 #ifdef lint
732 # define IF_LINT(Code) Code
733 #else
734 # define IF_LINT(Code) /* empty */
735 #endif
736
737 #ifndef __attribute__
738 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
739 #  define __attribute__(x)
740 # endif
741 #endif
742
743 #ifndef ATTRIBUTE_NORETURN
744 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
745 #endif
746
747 #ifndef ATTRIBUTE_UNUSED
748 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
749 #endif
750
751 #if defined strdupa
752 # define ASSIGN_STRDUPA(DEST, S)                \
753   do { DEST = strdupa (S); } while (0)
754 #else
755 # define ASSIGN_STRDUPA(DEST, S)                \
756   do                                            \
757     {                                           \
758       const char *s_ = (S);                     \
759       size_t len_ = strlen (s_) + 1;            \
760       char *tmp_dest_ = alloca (len_);          \
761       DEST = memcpy (tmp_dest_, (s_), len_);    \
762     }                                           \
763   while (0)
764 #endif
765
766 #ifndef EOVERFLOW
767 # define EOVERFLOW EINVAL
768 #endif
769
770 #if ! HAVE_FSEEKO && ! defined fseeko
771 # define fseeko(s, o, w) ((o) == (long int) (o)         \
772                           ? fseek (s, o, w)             \
773                           : (errno = EOVERFLOW, -1))
774 #endif
775
776 /* Compute the greatest common divisor of U and V using Euclid's
777    algorithm.  U and V must be nonzero.  */
778
779 static inline size_t
780 gcd (size_t u, size_t v)
781 {
782   do
783     {
784       size_t t = u % v;
785       u = v;
786       v = t;
787     }
788   while (v);
789
790   return u;
791 }
792
793 /* Compute the least common multiple of U and V.  U and V must be
794    nonzero.  There is no overflow checking, so callers should not
795    specify outlandish sizes.  */
796
797 static inline size_t
798 lcm (size_t u, size_t v)
799 {
800   return u * (v / gcd (u, v));
801 }
802
803 /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
804    ALIGNMENT must be nonzero.  The caller must arrange for ((char *)
805    PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
806    locations.  */
807
808 static inline void *
809 ptr_align (void *ptr, size_t alignment)
810 {
811   char *p0 = ptr;
812   char *p1 = p0 + alignment - 1;
813   return p1 - (uintptr_t) p1 % alignment;
814 }