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