1 /* system-dependent definitions for coreutils
2 Copyright (C) 1989, 1991-2006 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)
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
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
30 #if !defined HAVE_MKFIFO
31 # define mkfifo(name, mode) mknod (name, (mode) | S_IFIFO, 0)
35 # include <sys/param.h>
41 # define STDIN_FILENO 0
45 # define STDOUT_FILENO 1
49 # define STDERR_FILENO 2
53 /* limits.h must come before pathmax.h because limits.h on some systems
54 undefs PATH_MAX, whereas pathmax.h sets PATH_MAX. */
58 #include "localedir.h"
60 #if TIME_WITH_SYS_TIME
61 # include <sys/time.h>
65 # include <sys/time.h>
71 /* Since major is a function on SVR4, we can't use `ifndef major'. */
73 # include <sys/mkdev.h>
76 #if MAJOR_IN_SYSMACROS
77 # include <sys/sysmacros.h>
80 #ifdef major /* Might be defined in sys/types.h. */
85 # define major(dev) (((dev) >> 8) & 0xff)
86 # define minor(dev) ((dev) & 0xff)
87 # define makedev(maj, min) (((maj) << 8) | (min))
91 #if ! defined makedev && defined mkdev
92 # define makedev(maj, min) mkdev (maj, min)
95 /* Don't use bcopy! Use memmove if source and destination may overlap,
103 /* Some systems don't define the following symbols. */
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. */
119 # define EXIT_FAILURE 1
123 # define EXIT_SUCCESS 0
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. */
131 EXIT_CANNOT_INVOKE = 126,
135 #include "exitfail.h"
137 /* Set exit_failure to STATUS if that's not the default already. */
139 initialize_exit_failure (int status)
141 if (status != EXIT_FAILURE)
142 exit_failure = status;
147 #if !defined SEEK_SET
159 #if !defined O_DIRECT && defined O_DIRECTIO
160 /* Tru64 spells it `O_DIRECTIO'. */
161 # define O_DIRECT O_DIRECTIO
164 #if !defined O_DIRECT
168 #if !defined O_DIRECTORY
169 # define O_DIRECTORY 0
176 #if !defined O_NDELAY
180 #if !defined O_NOATIME
184 #if !defined O_NONBLOCK
185 # define O_NONBLOCK O_NDELAY
188 #if !defined O_NOCTTY
192 #if !defined O_NOFOLLOW
193 # define O_NOFOLLOW 0
196 #if !defined O_NOLINKS
208 /* For systems that distinguish between text and binary I/O.
209 O_BINARY is usually declared in fcntl.h */
210 #if !defined O_BINARY && defined _O_BINARY
211 /* For MSC-compatible compilers. */
212 # define O_BINARY _O_BINARY
213 # define O_TEXT _O_TEXT
217 /* BeOS 5 has O_BINARY and O_TEXT, but they have no effect. */
229 # define NLENGTH(direct) (strlen((direct)->d_name))
230 #else /* not HAVE_DIRENT_H */
231 # define dirent direct
232 # define NLENGTH(direct) ((direct)->d_namlen)
234 # include <sys/ndir.h>
235 # endif /* HAVE_SYS_NDIR_H */
237 # include <sys/dir.h>
238 # endif /* HAVE_SYS_DIR_H */
241 # endif /* HAVE_NDIR_H */
242 #endif /* HAVE_DIRENT_H */
245 /* Fake a return value. */
246 # define CLOSEDIR(d) (closedir (d), 0)
248 # define CLOSEDIR(d) closedir (d)
253 NOT_AN_INODE_NUMBER = 0
256 #ifdef D_INO_IN_DIRENT
257 # define D_INO(dp) ((dp)->d_ino)
259 /* Some systems don't have inodes, so fake them to avoid lots of ifdefs. */
260 # define D_INO(dp) NOT_AN_INODE_NUMBER
263 /* Get or fake the disk device blocksize.
264 Usually defined by sys/param.h (if at all). */
265 #if !defined DEV_BSIZE && defined BSIZE
266 # define DEV_BSIZE BSIZE
268 #if !defined DEV_BSIZE && defined BBSIZE /* SGI */
269 # define DEV_BSIZE BBSIZE
272 # define DEV_BSIZE 4096
275 /* Extract or fake data from a `struct stat'.
276 ST_BLKSIZE: Preferred I/O blocksize for the file, in bytes.
277 ST_NBLOCKS: Number of blocks in the file, including indirect blocks.
278 ST_NBLOCKSIZE: Size of blocks used when calculating ST_NBLOCKS. */
279 #ifndef HAVE_STRUCT_STAT_ST_BLOCKS
280 # define ST_BLKSIZE(statbuf) DEV_BSIZE
281 # if defined _POSIX_SOURCE || !defined BSIZE /* fileblocks.c uses BSIZE. */
282 # define ST_NBLOCKS(statbuf) \
283 ((statbuf).st_size / ST_NBLOCKSIZE + ((statbuf).st_size % ST_NBLOCKSIZE != 0))
284 # else /* !_POSIX_SOURCE && BSIZE */
285 # define ST_NBLOCKS(statbuf) \
286 (S_ISREG ((statbuf).st_mode) \
287 || S_ISDIR ((statbuf).st_mode) \
288 ? st_blocks ((statbuf).st_size) : 0)
289 # endif /* !_POSIX_SOURCE && BSIZE */
290 #else /* HAVE_STRUCT_STAT_ST_BLOCKS */
291 /* Some systems, like Sequents, return st_blksize of 0 on pipes.
292 Also, when running `rsh hpux11-system cat any-file', cat would
293 determine that the output stream had an st_blksize of 2147421096.
294 So here we arbitrarily limit the `optimal' block size to 4MB.
295 If anyone knows of a system for which the legitimate value for
296 st_blksize can exceed 4MB, please report it as a bug in this code. */
297 # define ST_BLKSIZE(statbuf) ((0 < (statbuf).st_blksize \
298 && (statbuf).st_blksize <= (1 << 22)) /* 4MB */ \
299 ? (statbuf).st_blksize : DEV_BSIZE)
300 # if defined hpux || defined __hpux__ || defined __hpux
301 /* HP-UX counts st_blocks in 1024-byte units.
302 This loses when mixing HP-UX and BSD file systems with NFS. */
303 # define ST_NBLOCKSIZE 1024
305 # if defined _AIX && defined _I386
306 /* AIX PS/2 counts st_blocks in 4K units. */
307 # define ST_NBLOCKSIZE (4 * 1024)
308 # else /* not AIX PS/2 */
310 # define ST_NBLOCKS(statbuf) \
311 (S_ISREG ((statbuf).st_mode) \
312 || S_ISDIR ((statbuf).st_mode) \
313 ? (statbuf).st_blocks * ST_BLKSIZE(statbuf)/ST_NBLOCKSIZE : 0)
315 # endif /* not AIX PS/2 */
317 #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
320 # define ST_NBLOCKS(statbuf) ((statbuf).st_blocks)
323 #ifndef ST_NBLOCKSIZE
325 # define ST_NBLOCKSIZE S_BLKSIZE
327 # define ST_NBLOCKSIZE 512
331 /* Redirection and wildcarding when done by the utility itself.
332 Generally a noop, but used in particular for native VMS. */
333 #ifndef initialize_main
334 # define initialize_main(ac, av)
337 #include "stat-macros.h"
339 #include "timespec.h"
342 # include <inttypes.h>
348 #if ULONG_MAX_LT_ULLONG_MAX
349 # define LONGEST_MODIFIER "ll"
351 # define LONGEST_MODIFIER "l"
353 #if PRI_MACROS_BROKEN
360 # define PRIdMAX LONGEST_MODIFIER "d"
363 # define PRIoMAX LONGEST_MODIFIER "o"
366 # define PRIuMAX LONGEST_MODIFIER "u"
369 # define PRIxMAX LONGEST_MODIFIER "x"
374 /* Jim Meyering writes:
376 "... Some ctype macros are valid only for character codes that
377 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
378 using /bin/cc or gcc but without giving an ansi option). So, all
379 ctype uses should be through macros like ISPRINT... If
380 STDC_HEADERS is defined, then autoconf has verified that the ctype
381 macros don't need to be guarded with references to isascii. ...
382 Defining isascii to 1 should let any compiler worth its salt
383 eliminate the && through constant folding."
387 "... Furthermore, isupper(c) etc. have an undefined result if c is
388 outside the range -1 <= c <= 255. One is tempted to write isupper(c)
389 with c being of type `char', but this is wrong if c is an 8-bit
390 character >= 128 which gets sign-extended to a negative value.
391 The macro ISUPPER protects against this as well." */
393 #if STDC_HEADERS || (!defined isascii && !HAVE_ISASCII)
394 # define IN_CTYPE_DOMAIN(c) 1
396 # define IN_CTYPE_DOMAIN(c) isascii(c)
400 # define ISBLANK(c) (IN_CTYPE_DOMAIN (c) && isblank (c))
402 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
405 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isgraph (c))
407 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isprint (c) && !isspace (c))
410 /* This is defined in <sys/euc.h> on at least Solaris2.6 systems. */
413 #define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
414 #define ISALNUM(c) (IN_CTYPE_DOMAIN (c) && isalnum (c))
415 #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
416 #define ISCNTRL(c) (IN_CTYPE_DOMAIN (c) && iscntrl (c))
417 #define ISLOWER(c) (IN_CTYPE_DOMAIN (c) && islower (c))
418 #define ISPUNCT(c) (IN_CTYPE_DOMAIN (c) && ispunct (c))
419 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
420 #define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
421 #define ISXDIGIT(c) (IN_CTYPE_DOMAIN (c) && isxdigit (c))
422 #define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
425 # define TOLOWER(Ch) tolower (Ch)
426 # define TOUPPER(Ch) toupper (Ch)
428 # define TOLOWER(Ch) (ISUPPER (Ch) ? tolower (Ch) : (Ch))
429 # define TOUPPER(Ch) (ISLOWER (Ch) ? toupper (Ch) : (Ch))
432 /* ISDIGIT differs from ISDIGIT_LOCALE, as follows:
433 - Its arg may be any int or unsigned int; it need not be an unsigned char.
434 - It's guaranteed to evaluate its argument exactly once.
435 - It's typically faster.
436 POSIX says that only '0' through '9' are digits. Prefer ISDIGIT to
437 ISDIGIT_LOCALE unless it's important to use the locale's definition
438 of `digit' even when the host does not conform to POSIX. */
439 #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
441 /* Convert a possibly-signed character to an unsigned character. This is
442 a bit safer than casting to unsigned char, since it catches some type
443 errors that the cast doesn't. */
444 static inline unsigned char to_uchar (char ch) { return ch; }
448 /* Take care of NLS matters. */
453 # define textdomain(Domainname) /* empty */
454 # undef bindtextdomain
455 # define bindtextdomain(Domainname, Dirname) /* empty */
458 #define _(msgid) gettext (msgid)
459 #define N_(msgid) msgid
461 #define STREQ(a, b) (strcmp ((a), (b)) == 0)
467 #if !HAVE_DECL_MALLOC
471 #if !HAVE_DECL_MEMCHR
475 #if !HAVE_DECL_REALLOC
479 #if !HAVE_DECL_STPCPY
485 #if !HAVE_DECL_STRNDUP
489 #if !HAVE_DECL_STRSTR
493 #if !HAVE_DECL_GETENV
501 /* This is needed on some AIX systems. */
502 #if !HAVE_DECL_STRTOUL
503 unsigned long strtoul ();
506 #if !HAVE_DECL_GETLOGIN
510 #if !HAVE_DECL_TTYNAME
514 #if !HAVE_DECL_GETEUID
518 #if !HAVE_DECL_GETPWUID
519 struct passwd *getpwuid ();
522 #if !HAVE_DECL_GETGRGID
523 struct group *getgrgid ();
526 #if !HAVE_DECL_GETUID
533 /* This is simply a shorthand for the common case in which
534 the third argument to x2nrealloc would be `sizeof *(P)'.
535 Ensure that sizeof *(P) is *not* 1. In that case, it'd be
536 better to use X2REALLOC, although not strictly necessary. */
537 #define X2NREALLOC(P, PN) ((void) verify_true (sizeof *(P) != 1), \
538 x2nrealloc (P, PN, sizeof *(P)))
540 /* Using x2realloc (when appropriate) usually makes your code more
541 readable than using x2nrealloc, but it also makes it so your
542 code will malfunction if sizeof *(P) ever becomes 2 or greater.
543 So use this macro instead of using x2realloc directly. */
544 #define X2REALLOC(P, PN) ((void) verify_true (sizeof *(P) == 1), \
547 #if ! defined HAVE_MEMPCPY && ! defined mempcpy
548 /* Be CAREFUL that there are no side effects in N. */
549 # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
552 /* Include automatically-generated macros for unlocked I/O. */
553 #include "unlocked-io.h"
554 #include "same-inode.h"
556 #define DOT_OR_DOTDOT(Basename) \
557 (Basename[0] == '.' && (Basename[1] == '\0' \
558 || (Basename[1] == '.' && Basename[2] == '\0')))
560 /* A wrapper for readdir so that callers don't see entries for `.' or `..'. */
561 static inline struct dirent const *
562 readdir_ignoring_dot_and_dotdot (DIR *dirp)
566 struct dirent const *dp = readdir (dirp);
567 if (dp == NULL || ! DOT_OR_DOTDOT (dp->d_name))
573 # define SETVBUF(Stream, Buffer, Type, Size) \
574 setvbuf (Stream, Type, Buffer, Size)
576 # define SETVBUF(Stream, Buffer, Type, Size) \
577 setvbuf (Stream, Buffer, Type, Size)
580 /* Factor out some of the common --help and --version processing code. */
582 /* These enum values cannot possibly conflict with the option values
583 ordinarily used by commands, including CHAR_MAX + 1, etc. Avoid
584 CHAR_MIN - 1, as it may equal -1, the getopt end-of-options value. */
587 GETOPT_HELP_CHAR = (CHAR_MIN - 2),
588 GETOPT_VERSION_CHAR = (CHAR_MIN - 3)
591 #define GETOPT_HELP_OPTION_DECL \
592 "help", no_argument, NULL, GETOPT_HELP_CHAR
593 #define GETOPT_VERSION_OPTION_DECL \
594 "version", no_argument, NULL, GETOPT_VERSION_CHAR
596 #define case_GETOPT_HELP_CHAR \
597 case GETOPT_HELP_CHAR: \
598 usage (EXIT_SUCCESS); \
601 /* Program_name must be a literal string.
602 Usually it is just PROGRAM_NAME. */
603 #define USAGE_BUILTIN_WARNING \
605 "NOTE: your shell may have its own version of %s, which usually supersedes\n" \
606 "the version described here. Please refer to your shell's documentation\n" \
607 "for details about the options it supports.\n")
609 #define HELP_OPTION_DESCRIPTION \
610 _(" --help display this help and exit\n")
611 #define VERSION_OPTION_DESCRIPTION \
612 _(" --version output version information and exit\n")
614 #include "closeout.h"
615 #include "version-etc.h"
617 #define case_GETOPT_VERSION_CHAR(Program_name, Authors) \
618 case GETOPT_VERSION_CHAR: \
619 version_etc (stdout, Program_name, GNU_PACKAGE, VERSION, Authors, \
621 exit (EXIT_SUCCESS); \
625 # define MAX(a, b) ((a) > (b) ? (a) : (b))
629 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
632 #include "intprops.h"
635 # define CHAR_MIN TYPE_MINIMUM (char)
639 # define CHAR_MAX TYPE_MAXIMUM (char)
643 # define SCHAR_MIN (-1 - SCHAR_MAX)
647 # define SCHAR_MAX (CHAR_MAX == UCHAR_MAX ? CHAR_MAX / 2 : CHAR_MAX)
651 # define UCHAR_MAX TYPE_MAXIMUM (unsigned char)
655 # define SHRT_MIN TYPE_MINIMUM (short int)
659 # define SHRT_MAX TYPE_MAXIMUM (short int)
663 # define INT_MAX TYPE_MAXIMUM (int)
667 # define INT_MIN TYPE_MINIMUM (int)
671 # define INTMAX_MAX TYPE_MAXIMUM (intmax_t)
675 # define INTMAX_MIN TYPE_MINIMUM (intmax_t)
679 # define UINT_MAX TYPE_MAXIMUM (unsigned int)
683 # define LONG_MAX TYPE_MAXIMUM (long int)
687 # define ULONG_MAX TYPE_MAXIMUM (unsigned long int)
691 # define SIZE_MAX TYPE_MAXIMUM (size_t)
695 # define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
699 # define UINTMAX_MAX TYPE_MAXIMUM (uintmax_t)
703 # define OFF_T_MIN TYPE_MINIMUM (off_t)
707 # define OFF_T_MAX TYPE_MAXIMUM (off_t)
711 # define UID_T_MAX TYPE_MAXIMUM (uid_t)
715 # define GID_T_MAX TYPE_MAXIMUM (gid_t)
719 # define PID_T_MAX TYPE_MAXIMUM (pid_t)
722 /* Use this to suppress gcc's `...may be used before initialized' warnings. */
724 # define IF_LINT(Code) Code
726 # define IF_LINT(Code) /* empty */
729 #ifndef __attribute__
730 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
731 # define __attribute__(x) /* empty */
735 #ifndef ATTRIBUTE_NORETURN
736 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
739 #ifndef ATTRIBUTE_UNUSED
740 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
744 # define ASSIGN_STRDUPA(DEST, S) \
745 do { DEST = strdupa (S); } while (0)
747 # define ASSIGN_STRDUPA(DEST, S) \
750 const char *s_ = (S); \
751 size_t len_ = strlen (s_) + 1; \
752 char *tmp_dest_ = alloca (len_); \
753 DEST = memcpy (tmp_dest_, (s_), len_); \
759 # define EOVERFLOW EINVAL
762 #if ! HAVE_FSEEKO && ! defined fseeko
763 # define fseeko(s, o, w) ((o) == (long int) (o) \
765 : (errno = EOVERFLOW, -1))
769 # define sync() /* empty */
772 /* Compute the greatest common divisor of U and V using Euclid's
773 algorithm. U and V must be nonzero. */
776 gcd (size_t u, size_t v)
789 /* Compute the least common multiple of U and V. U and V must be
790 nonzero. There is no overflow checking, so callers should not
791 specify outlandish sizes. */
794 lcm (size_t u, size_t v)
796 return u * (v / gcd (u, v));
799 /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
800 ALIGNMENT must be nonzero. The caller must arrange for ((char *)
801 PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
805 ptr_align (void const *ptr, size_t alignment)
807 char const *p0 = ptr;
808 char const *p1 = p0 + alignment - 1;
809 return (void *) (p1 - (size_t) p1 % alignment);
812 /* If 10*Accum + Digit_val is larger than the maximum value for Type,
813 then don't update Accum and return false to indicate it would
814 overflow. Otherwise, set Accum to that new value and return true.
815 Verify at compile-time that Type is Accum's type, and that Type is
816 unsigned. Accum must be an object, so that we can take its
817 address. Accum and Digit_val may be evaluated multiple times.
819 The "Added check" below is not strictly required, but it causes GCC
820 to return a nonzero exit status instead of merely a warning
821 diagnostic, and that is more useful. */
823 #define DECIMAL_DIGIT_ACCUMULATE(Accum, Digit_val, Type) \
825 (void) (&(Accum) == (Type *) NULL), /* The type matches. */ \
826 (void) verify_true (! TYPE_SIGNED (Type)), /* The type is unsigned. */ \
827 (void) verify_true (sizeof (Accum) == sizeof (Type)), /* Added check. */ \
828 (((Type) -1 / 10 < (Accum) \
829 || (Type) ((Accum) * 10 + (Digit_val)) < (Accum)) \
830 ? false : (((Accum) = (Accum) * 10 + (Digit_val)), true)) \