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