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