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