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