1 /* system-dependent definitions for coreutils
2 Copyright (C) 1989, 1991-2005 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>
43 # define STDIN_FILENO 0
47 # define STDOUT_FILENO 1
51 # define STDERR_FILENO 2
55 /* limits.h must come before pathmax.h because limits.h on some systems
56 undefs PATH_MAX, whereas pathmax.h sets PATH_MAX. */
60 #include "localedir.h"
62 #if TIME_WITH_SYS_TIME
63 # include <sys/time.h>
67 # include <sys/time.h>
73 /* Since major is a function on SVR4, we can't use `ifndef major'. */
75 # include <sys/mkdev.h>
78 #if MAJOR_IN_SYSMACROS
79 # include <sys/sysmacros.h>
82 #ifdef major /* Might be defined in sys/types.h. */
87 # define major(dev) (((dev) >> 8) & 0xff)
88 # define minor(dev) ((dev) & 0xff)
89 # define makedev(maj, min) (((maj) << 8) | (min))
93 #if ! defined makedev && defined mkdev
94 # define makedev(maj, min) mkdev (maj, min)
101 /* Some systems (even some that do have <utime.h>) don't declare this
102 structure anywhere. */
103 #ifndef HAVE_STRUCT_UTIMBUF
111 /* Don't use bcopy! Use memmove if source and destination may overlap,
119 /* Some systems don't define the following symbols. */
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. */
135 # define EXIT_FAILURE 1
139 # define EXIT_SUCCESS 0
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. */
147 EXIT_CANNOT_INVOKE = 126,
151 #include "exitfail.h"
153 /* Set exit_failure to STATUS if that's not the default already. */
155 initialize_exit_failure (int status)
157 if (status != EXIT_FAILURE)
158 exit_failure = status;
164 # include <sys/file.h>
167 #if !defined SEEK_SET
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
187 #if !defined O_DIRECT
195 #if !defined O_NDELAY
199 #if !defined O_NONBLOCK
200 # define O_NONBLOCK O_NDELAY
203 #if !defined O_NOCTTY
207 #if !defined O_NOFOLLOW
208 # define O_NOFOLLOW 0
220 /* BeOS 5 has O_BINARY and O_TEXT, but they have no effect. */
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) \
236 setmode (_f1, O_BINARY); \
238 setmode (_f2, O_BINARY); \
242 # define SET_MODE(_f, _m) (void)0
243 # define SET_BINARY(f) (void)0
244 # define SET_BINARY2(f1,f2) (void)0
249 #endif /* O_BINARY */
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)
258 # include <sys/ndir.h>
259 # endif /* HAVE_SYS_NDIR_H */
261 # include <sys/dir.h>
262 # endif /* HAVE_SYS_DIR_H */
265 # endif /* HAVE_NDIR_H */
266 #endif /* HAVE_DIRENT_H */
269 /* Fake a return value. */
270 # define CLOSEDIR(d) (closedir (d), 0)
272 # define CLOSEDIR(d) closedir (d)
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
280 #if !defined DEV_BSIZE && defined BBSIZE /* SGI */
281 # define DEV_BSIZE BBSIZE
284 # define DEV_BSIZE 4096
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
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 */
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)
327 # endif /* not AIX PS/2 */
329 #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
332 # define ST_NBLOCKS(statbuf) ((statbuf).st_blocks)
335 #ifndef ST_NBLOCKSIZE
337 # define ST_NBLOCKSIZE S_BLKSIZE
339 # define ST_NBLOCKSIZE 512
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)
349 #include "stat-macros.h"
351 #include "timespec.h"
354 /* We need the declaration of setmode. */
356 /* We need the declaration of __djgpp_set_ctrl_c. */
357 # include <sys/exceptn.h>
361 # include <inttypes.h>
367 #if ULONG_MAX < ULLONG_MAX
368 # define LONGEST_MODIFIER "ll"
370 # define LONGEST_MODIFIER "l"
372 #if PRI_MACROS_BROKEN
379 # define PRIdMAX LONGEST_MODIFIER "d"
382 # define PRIoMAX LONGEST_MODIFIER "o"
385 # define PRIuMAX LONGEST_MODIFIER "u"
388 # define PRIxMAX LONGEST_MODIFIER "x"
393 /* Jim Meyering writes:
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."
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." */
412 #if STDC_HEADERS || (!defined (isascii) && !HAVE_ISASCII)
413 # define IN_CTYPE_DOMAIN(c) 1
415 # define IN_CTYPE_DOMAIN(c) isascii(c)
419 # define ISBLANK(c) (IN_CTYPE_DOMAIN (c) && isblank (c))
421 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
424 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isgraph (c))
426 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isprint (c) && !isspace (c))
429 /* This is defined in <sys/euc.h> on at least Solaris2.6 systems. */
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))
444 # define TOLOWER(Ch) tolower (Ch)
445 # define TOUPPER(Ch) toupper (Ch)
447 # define TOLOWER(Ch) (ISUPPER (Ch) ? tolower (Ch) : (Ch))
448 # define TOUPPER(Ch) (ISLOWER (Ch) ? toupper (Ch) : (Ch))
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)
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; }
467 /* Take care of NLS matters. */
472 # define textdomain(Domainname) /* empty */
473 # undef bindtextdomain
474 # define bindtextdomain(Domainname, Dirname) /* empty */
477 #define _(msgid) gettext (msgid)
478 #define N_(msgid) msgid
480 #define STREQ(a, b) (strcmp ((a), (b)) == 0)
486 #if !HAVE_DECL_MALLOC
490 #if !HAVE_DECL_MEMCHR
494 #if !HAVE_DECL_REALLOC
498 #if !HAVE_DECL_STPCPY
504 #if !HAVE_DECL_STRNDUP
508 #if !HAVE_DECL_STRSTR
512 #if !HAVE_DECL_GETENV
520 /* This is needed on some AIX systems. */
521 #if !HAVE_DECL_STRTOUL
522 unsigned long strtoul ();
525 #if !HAVE_DECL_GETLOGIN
529 #if !HAVE_DECL_TTYNAME
533 #if !HAVE_DECL_GETEUID
537 #if !HAVE_DECL_GETPWUID
538 struct passwd *getpwuid ();
541 #if !HAVE_DECL_GETGRGID
542 struct group *getgrgid ();
545 #if !HAVE_DECL_GETUID
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)))
556 /* Include automatically-generated macros for unlocked I/O. */
557 #include "unlocked-io.h"
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)
563 #define DOT_OR_DOTDOT(Basename) \
564 (Basename[0] == '.' && (Basename[1] == '\0' \
565 || (Basename[1] == '.' && Basename[2] == '\0')))
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)
573 struct dirent const *dp = readdir (dirp);
574 if (dp == NULL || ! DOT_OR_DOTDOT (dp->d_name))
580 # define SETVBUF(Stream, Buffer, Type, Size) \
581 setvbuf (Stream, Type, Buffer, Size)
583 # define SETVBUF(Stream, Buffer, Type, Size) \
584 setvbuf (Stream, Buffer, Type, Size)
587 /* Factor out some of the common --help and --version processing code. */
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. */
594 GETOPT_HELP_CHAR = (CHAR_MIN - 2),
595 GETOPT_VERSION_CHAR = (CHAR_MIN - 3)
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
603 #define case_GETOPT_HELP_CHAR \
604 case GETOPT_HELP_CHAR: \
605 usage (EXIT_SUCCESS); \
608 /* Program_name must be a literal string.
609 Usually it is just PROGRAM_NAME. */
610 #define USAGE_BUILTIN_WARNING \
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")
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")
621 #include "closeout.h"
622 #include "version-etc.h"
624 #define case_GETOPT_VERSION_CHAR(Program_name, Authors) \
625 case GETOPT_VERSION_CHAR: \
626 version_etc (stdout, Program_name, GNU_PACKAGE, VERSION, Authors, \
628 exit (EXIT_SUCCESS); \
632 # define MAX(a, b) ((a) > (b) ? (a) : (b))
636 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
639 #include "intprops.h"
642 # define CHAR_MIN TYPE_MINIMUM (char)
646 # define CHAR_MAX TYPE_MAXIMUM (char)
650 # define SCHAR_MIN (-1 - SCHAR_MAX)
654 # define SCHAR_MAX (CHAR_MAX == UCHAR_MAX ? CHAR_MAX / 2 : CHAR_MAX)
658 # define UCHAR_MAX TYPE_MAXIMUM (unsigned char)
662 # define SHRT_MIN TYPE_MINIMUM (short int)
666 # define SHRT_MAX TYPE_MAXIMUM (short int)
670 # define INT_MAX TYPE_MAXIMUM (int)
674 # define INT_MIN TYPE_MINIMUM (int)
678 # define INTMAX_MAX TYPE_MAXIMUM (intmax_t)
682 # define INTMAX_MIN TYPE_MINIMUM (intmax_t)
686 # define UINT_MAX TYPE_MAXIMUM (unsigned int)
690 # define LONG_MAX TYPE_MAXIMUM (long int)
694 # define ULONG_MAX TYPE_MAXIMUM (unsigned long int)
698 # define SIZE_MAX TYPE_MAXIMUM (size_t)
702 # define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
706 # define UINTMAX_MAX TYPE_MAXIMUM (uintmax_t)
710 # define OFF_T_MIN TYPE_MINIMUM (off_t)
714 # define OFF_T_MAX TYPE_MAXIMUM (off_t)
718 # define UID_T_MAX TYPE_MAXIMUM (uid_t)
722 # define GID_T_MAX TYPE_MAXIMUM (gid_t)
726 # define PID_T_MAX TYPE_MAXIMUM (pid_t)
729 /* Use this to suppress gcc's `...may be used before initialized' warnings. */
731 # define IF_LINT(Code) Code
733 # define IF_LINT(Code) /* empty */
736 #ifndef __attribute__
737 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
738 # define __attribute__(x) /* empty */
742 #ifndef ATTRIBUTE_NORETURN
743 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
746 #ifndef ATTRIBUTE_UNUSED
747 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
751 # define ASSIGN_STRDUPA(DEST, S) \
752 do { DEST = strdupa (S); } while (0)
754 # define ASSIGN_STRDUPA(DEST, S) \
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_); \
766 # define EOVERFLOW EINVAL
769 #if ! HAVE_FSEEKO && ! defined fseeko
770 # define fseeko(s, o, w) ((o) == (long int) (o) \
772 : (errno = EOVERFLOW, -1))
775 /* Compute the greatest common divisor of U and V using Euclid's
776 algorithm. U and V must be nonzero. */
779 gcd (size_t u, size_t v)
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. */
797 lcm (size_t u, size_t v)
799 return u * (v / gcd (u, v));
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
808 ptr_align (void const *ptr, size_t alignment)
810 char const *p0 = ptr;
811 char const *p1 = p0 + alignment - 1;
812 return (void *) (p1 - (size_t) p1 % alignment);
815 /* Verify a requirement at compile-time (unlike assert, which is runtime). */
816 #define VERIFY(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
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)
823 /* Use the compile-time type-max. assertion only if the compiler provides
824 the __typeof__ operator. */
826 # define VERIFY_W_TYPEOF(assertion) VERIFY_EXPR (assertion)
828 # define VERIFY_W_TYPEOF(assertion) (void) 0
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
837 #define DECIMAL_DIGIT_ACCUMULATE(Accum, Digit_val, Type_max) \
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)) \