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)
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. */
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(path, mode) (mknod ((path), (mode) | S_IFIFO, 0))
35 # include <sys/param.h>
38 /* <unistd.h> should be included before any preprocessor test
45 # define STDIN_FILENO 0
49 # define STDOUT_FILENO 1
53 # define STDERR_FILENO 2
57 /* limits.h must come before pathmax.h because limits.h on some systems
58 undefs PATH_MAX, whereas pathmax.h sets PATH_MAX. */
62 #include "localedir.h"
64 #if TIME_WITH_SYS_TIME
65 # include <sys/time.h>
69 # include <sys/time.h>
75 /* Since major is a function on SVR4, we can't use `ifndef major'. */
77 # include <sys/mkdev.h>
80 #if MAJOR_IN_SYSMACROS
81 # include <sys/sysmacros.h>
84 #ifdef major /* Might be defined in sys/types.h. */
89 # define major(dev) (((dev) >> 8) & 0xff)
90 # define minor(dev) ((dev) & 0xff)
91 # define makedev(maj, min) (((maj) << 8) | (min))
95 #if ! defined makedev && defined mkdev
96 # define makedev(maj, min) mkdev (maj, min)
103 /* Some systems (even some that do have <utime.h>) don't declare this
104 structure anywhere. */
105 #ifndef HAVE_STRUCT_UTIMBUF
113 /* Don't use bcopy! Use memmove if source and destination may overlap,
117 #if ! HAVE_DECL_MEMRCHR
118 void *memrchr (const void *, int, size_t);
123 /* Some systems don't define the following symbols. */
133 #define getopt system_getopt
137 /* The following test is to work around the gross typo in
138 systems like Sony NEWS-OS Release 4.0C, whereby EXIT_FAILURE
139 is defined to 0, not 1. */
142 # define EXIT_FAILURE 1
146 # define EXIT_SUCCESS 0
149 /* Exit statuses for programs like 'env' that exec other programs.
150 EXIT_FAILURE might not be 1, so use EXIT_FAIL in such programs. */
154 EXIT_CANNOT_INVOKE = 126,
158 #include "exitfail.h"
160 /* Set exit_failure to STATUS if that's not the default already. */
162 initialize_exit_failure (int status)
164 if (status != EXIT_FAILURE)
165 exit_failure = status;
171 # include <sys/file.h>
174 #if !defined SEEK_SET
186 /* For systems that distinguish between text and binary I/O.
187 O_BINARY is usually declared in fcntl.h */
188 #if !defined O_BINARY && defined _O_BINARY
189 /* For MSC-compatible compilers. */
190 # define O_BINARY _O_BINARY
191 # define O_TEXT _O_TEXT
194 #if !defined O_DIRECT
202 #if !defined O_NDELAY
206 #if !defined O_NONBLOCK
207 # define O_NONBLOCK O_NDELAY
210 #if !defined O_NOCTTY
214 #if !defined O_NOFOLLOW
215 # define O_NOFOLLOW 0
227 /* BeOS 5 has O_BINARY and O_TEXT, but they have no effect. */
234 # define setmode _setmode
235 # define fileno(_fp) _fileno (_fp)
236 # endif /* not DJGPP */
237 # define SET_MODE(_f, _m) setmode (_f, _m)
238 # define SET_BINARY(_f) do {if (!isatty(_f)) setmode (_f, O_BINARY);} while (0)
239 # define SET_BINARY2(_f1, _f2) \
243 setmode (_f1, O_BINARY); \
245 setmode (_f2, O_BINARY); \
249 # define SET_MODE(_f, _m) (void)0
250 # define SET_BINARY(f) (void)0
251 # define SET_BINARY2(f1,f2) (void)0
256 #endif /* O_BINARY */
260 # define NLENGTH(direct) (strlen((direct)->d_name))
261 #else /* not HAVE_DIRENT_H */
262 # define dirent direct
263 # define NLENGTH(direct) ((direct)->d_namlen)
265 # include <sys/ndir.h>
266 # endif /* HAVE_SYS_NDIR_H */
268 # include <sys/dir.h>
269 # endif /* HAVE_SYS_DIR_H */
272 # endif /* HAVE_NDIR_H */
273 #endif /* HAVE_DIRENT_H */
276 /* Fake a return value. */
277 # define CLOSEDIR(d) (closedir (d), 0)
279 # define CLOSEDIR(d) closedir (d)
282 /* Get or fake the disk device blocksize.
283 Usually defined by sys/param.h (if at all). */
284 #if !defined DEV_BSIZE && defined BSIZE
285 # define DEV_BSIZE BSIZE
287 #if !defined DEV_BSIZE && defined BBSIZE /* SGI */
288 # define DEV_BSIZE BBSIZE
291 # define DEV_BSIZE 4096
294 /* Extract or fake data from a `struct stat'.
295 ST_BLKSIZE: Preferred I/O blocksize for the file, in bytes.
296 ST_NBLOCKS: Number of blocks in the file, including indirect blocks.
297 ST_NBLOCKSIZE: Size of blocks used when calculating ST_NBLOCKS. */
298 #ifndef HAVE_STRUCT_STAT_ST_BLOCKS
299 # define ST_BLKSIZE(statbuf) DEV_BSIZE
300 # if defined _POSIX_SOURCE || !defined BSIZE /* fileblocks.c uses BSIZE. */
301 # define ST_NBLOCKS(statbuf) \
302 ((statbuf).st_size / ST_NBLOCKSIZE + ((statbuf).st_size % ST_NBLOCKSIZE != 0))
303 # else /* !_POSIX_SOURCE && BSIZE */
304 # define ST_NBLOCKS(statbuf) \
305 (S_ISREG ((statbuf).st_mode) \
306 || S_ISDIR ((statbuf).st_mode) \
307 ? st_blocks ((statbuf).st_size) : 0)
308 # endif /* !_POSIX_SOURCE && BSIZE */
309 #else /* HAVE_STRUCT_STAT_ST_BLOCKS */
310 /* Some systems, like Sequents, return st_blksize of 0 on pipes.
311 Also, when running `rsh hpux11-system cat any-file', cat would
312 determine that the output stream had an st_blksize of 2147421096.
313 So here we arbitrarily limit the `optimal' block size to 4MB.
314 If anyone knows of a system for which the legitimate value for
315 st_blksize can exceed 4MB, please report it as a bug in this code. */
316 # define ST_BLKSIZE(statbuf) ((0 < (statbuf).st_blksize \
317 && (statbuf).st_blksize <= (1 << 22)) /* 4MB */ \
318 ? (statbuf).st_blksize : DEV_BSIZE)
319 # if defined hpux || defined __hpux__ || defined __hpux
320 /* HP-UX counts st_blocks in 1024-byte units.
321 This loses when mixing HP-UX and BSD file systems with NFS. */
322 # define ST_NBLOCKSIZE 1024
324 # if defined _AIX && defined _I386
325 /* AIX PS/2 counts st_blocks in 4K units. */
326 # define ST_NBLOCKSIZE (4 * 1024)
327 # else /* not AIX PS/2 */
329 # define ST_NBLOCKS(statbuf) \
330 (S_ISREG ((statbuf).st_mode) \
331 || S_ISDIR ((statbuf).st_mode) \
332 ? (statbuf).st_blocks * ST_BLKSIZE(statbuf)/ST_NBLOCKSIZE : 0)
334 # endif /* not AIX PS/2 */
336 #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
339 # define ST_NBLOCKS(statbuf) ((statbuf).st_blocks)
342 #ifndef ST_NBLOCKSIZE
343 # define ST_NBLOCKSIZE 512
346 /* Redirection and wildcarding when done by the utility itself.
347 Generally a noop, but used in particular for native VMS. */
348 #ifndef initialize_main
349 # define initialize_main(ac, av)
352 #include "stat-macros.h"
354 #include "timespec.h"
357 # define RETSIGTYPE void
361 /* We need the declaration of setmode. */
363 /* We need the declaration of __djgpp_set_ctrl_c. */
364 # include <sys/exceptn.h>
368 # include <inttypes.h>
374 #if ULONG_MAX < ULLONG_MAX
375 # define LONGEST_MODIFIER "ll"
377 # define LONGEST_MODIFIER "l"
379 #if PRI_MACROS_BROKEN
386 # define PRIdMAX LONGEST_MODIFIER "d"
389 # define PRIoMAX LONGEST_MODIFIER "o"
392 # define PRIuMAX LONGEST_MODIFIER "u"
395 # define PRIxMAX LONGEST_MODIFIER "x"
400 /* Jim Meyering writes:
402 "... Some ctype macros are valid only for character codes that
403 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
404 using /bin/cc or gcc but without giving an ansi option). So, all
405 ctype uses should be through macros like ISPRINT... If
406 STDC_HEADERS is defined, then autoconf has verified that the ctype
407 macros don't need to be guarded with references to isascii. ...
408 Defining isascii to 1 should let any compiler worth its salt
409 eliminate the && through constant folding."
413 "... Furthermore, isupper(c) etc. have an undefined result if c is
414 outside the range -1 <= c <= 255. One is tempted to write isupper(c)
415 with c being of type `char', but this is wrong if c is an 8-bit
416 character >= 128 which gets sign-extended to a negative value.
417 The macro ISUPPER protects against this as well." */
419 #if STDC_HEADERS || (!defined (isascii) && !HAVE_ISASCII)
420 # define IN_CTYPE_DOMAIN(c) 1
422 # define IN_CTYPE_DOMAIN(c) isascii(c)
426 # define ISBLANK(c) (IN_CTYPE_DOMAIN (c) && isblank (c))
428 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
431 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isgraph (c))
433 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isprint (c) && !isspace (c))
436 /* This is defined in <sys/euc.h> on at least Solaris2.6 systems. */
439 #define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
440 #define ISALNUM(c) (IN_CTYPE_DOMAIN (c) && isalnum (c))
441 #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
442 #define ISCNTRL(c) (IN_CTYPE_DOMAIN (c) && iscntrl (c))
443 #define ISLOWER(c) (IN_CTYPE_DOMAIN (c) && islower (c))
444 #define ISPUNCT(c) (IN_CTYPE_DOMAIN (c) && ispunct (c))
445 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
446 #define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
447 #define ISXDIGIT(c) (IN_CTYPE_DOMAIN (c) && isxdigit (c))
448 #define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
451 # define TOLOWER(Ch) tolower (Ch)
452 # define TOUPPER(Ch) toupper (Ch)
454 # define TOLOWER(Ch) (ISUPPER (Ch) ? tolower (Ch) : (Ch))
455 # define TOUPPER(Ch) (ISLOWER (Ch) ? toupper (Ch) : (Ch))
458 /* ISDIGIT differs from ISDIGIT_LOCALE, as follows:
459 - Its arg may be any int or unsigned int; it need not be an unsigned char.
460 - It's guaranteed to evaluate its argument exactly once.
461 - It's typically faster.
462 POSIX says that only '0' through '9' are digits. Prefer ISDIGIT to
463 ISDIGIT_LOCALE unless it's important to use the locale's definition
464 of `digit' even when the host does not conform to POSIX. */
465 #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
467 /* Convert a possibly-signed character to an unsigned character. This is
468 a bit safer than casting to unsigned char, since it catches some type
469 errors that the cast doesn't. */
470 static inline unsigned char to_uchar (char ch) { return ch; }
472 /* Take care of NLS matters. */
477 # define setlocale(Category, Locale) /* empty */
483 # define textdomain(Domainname) /* empty */
484 # undef bindtextdomain
485 # define bindtextdomain(Domainname, Dirname) /* empty */
488 #define _(msgid) gettext (msgid)
489 #define N_(msgid) msgid
491 #ifndef HAVE_SETLOCALE
492 # define HAVE_SETLOCALE 0
495 #define STREQ(a, b) (strcmp ((a), (b)) == 0)
501 #if !HAVE_DECL_MALLOC
505 #if !HAVE_DECL_MEMCHR
509 #if !HAVE_DECL_REALLOC
513 #if !HAVE_DECL_STPCPY
519 #if !HAVE_DECL_STRNDUP
523 #if !HAVE_DECL_STRSTR
527 #if !HAVE_DECL_GETENV
535 /* This is needed on some AIX systems. */
536 #if !HAVE_DECL_STRTOUL
537 unsigned long strtoul ();
540 #if !HAVE_DECL_GETLOGIN
544 #if !HAVE_DECL_TTYNAME
548 #if !HAVE_DECL_GETEUID
552 #if !HAVE_DECL_GETPWUID
553 struct passwd *getpwuid ();
556 #if !HAVE_DECL_GETGRGID
557 struct group *getgrgid ();
560 #if !HAVE_DECL_GETUID
566 #if ! defined HAVE_MEMPCPY && ! defined mempcpy
567 /* Be CAREFUL that there are no side effects in N. */
568 # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
571 /* Include automatically-generated macros for unlocked I/O. */
572 #include "unlocked-io.h"
574 #define SAME_INODE(Stat_buf_1, Stat_buf_2) \
575 ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
576 && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
578 #define DOT_OR_DOTDOT(Basename) \
579 (Basename[0] == '.' && (Basename[1] == '\0' \
580 || (Basename[1] == '.' && Basename[2] == '\0')))
582 /* A wrapper for readdir so that callers don't see entries for `.' or `..'. */
583 static inline struct dirent const *
584 readdir_ignoring_dot_and_dotdot (DIR *dirp)
588 struct dirent const *dp = readdir (dirp);
589 if (dp == NULL || ! DOT_OR_DOTDOT (dp->d_name))
595 # define SETVBUF(Stream, Buffer, Type, Size) \
596 setvbuf (Stream, Type, Buffer, Size)
598 # define SETVBUF(Stream, Buffer, Type, Size) \
599 setvbuf (Stream, Buffer, Type, Size)
602 /* Factor out some of the common --help and --version processing code. */
604 /* These enum values cannot possibly conflict with the option values
605 ordinarily used by commands, including CHAR_MAX + 1, etc. Avoid
606 CHAR_MIN - 1, as it may equal -1, the getopt end-of-options value. */
609 GETOPT_HELP_CHAR = (CHAR_MIN - 2),
610 GETOPT_VERSION_CHAR = (CHAR_MIN - 3)
613 #define GETOPT_HELP_OPTION_DECL \
614 "help", no_argument, 0, GETOPT_HELP_CHAR
615 #define GETOPT_VERSION_OPTION_DECL \
616 "version", no_argument, 0, GETOPT_VERSION_CHAR
618 #define case_GETOPT_HELP_CHAR \
619 case GETOPT_HELP_CHAR: \
620 usage (EXIT_SUCCESS); \
623 #define HELP_OPTION_DESCRIPTION \
624 _(" --help display this help and exit\n")
625 #define VERSION_OPTION_DESCRIPTION \
626 _(" --version output version information and exit\n")
628 #include "closeout.h"
629 #include "version-etc.h"
631 #define case_GETOPT_VERSION_CHAR(Program_name, Authors) \
632 case GETOPT_VERSION_CHAR: \
633 version_etc (stdout, Program_name, GNU_PACKAGE, VERSION, Authors, \
635 exit (EXIT_SUCCESS); \
639 # define MAX(a, b) ((a) > (b) ? (a) : (b))
643 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
646 /* The extra casts work around common compiler bugs. */
647 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
648 /* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
649 It is necessary at least when t == time_t. */
650 #define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
651 ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) : (t) 0))
652 #define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
654 /* Upper bound on the string length of an integer converted to string.
655 302 / 1000 is ceil (log10 (2.0)). Subtract 1 for the sign bit;
656 add 1 for integer division truncation; add 1 more for a minus sign. */
657 #define INT_STRLEN_BOUND(t) ((sizeof (t) * CHAR_BIT - 1) * 302 / 1000 + 2)
660 # define CHAR_MIN TYPE_MINIMUM (char)
664 # define CHAR_MAX TYPE_MAXIMUM (char)
668 # define SCHAR_MIN (-1 - SCHAR_MAX)
672 # define SCHAR_MAX (CHAR_MAX == UCHAR_MAX ? CHAR_MAX / 2 : CHAR_MAX)
676 # define UCHAR_MAX TYPE_MAXIMUM (unsigned char)
680 # define SHRT_MIN TYPE_MINIMUM (short int)
684 # define SHRT_MAX TYPE_MAXIMUM (short int)
688 # define INT_MAX TYPE_MAXIMUM (int)
692 # define INT_MIN TYPE_MINIMUM (int)
696 # define INTMAX_MAX TYPE_MAXIMUM (intmax_t)
700 # define INTMAX_MIN TYPE_MINIMUM (intmax_t)
704 # define UINT_MAX TYPE_MAXIMUM (unsigned int)
708 # define LONG_MAX TYPE_MAXIMUM (long int)
712 # define ULONG_MAX TYPE_MAXIMUM (unsigned long int)
716 # define SIZE_MAX TYPE_MAXIMUM (size_t)
720 # define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
724 # define UINTMAX_MAX TYPE_MAXIMUM (uintmax_t)
728 # define OFF_T_MIN TYPE_MINIMUM (off_t)
732 # define OFF_T_MAX TYPE_MAXIMUM (off_t)
736 # define UID_T_MAX TYPE_MAXIMUM (uid_t)
740 # define GID_T_MAX TYPE_MAXIMUM (gid_t)
744 # define PID_T_MAX TYPE_MAXIMUM (pid_t)
747 /* Use this to suppress gcc's `...may be used before initialized' warnings. */
749 # define IF_LINT(Code) Code
751 # define IF_LINT(Code) /* empty */
754 #ifndef __attribute__
755 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
756 # define __attribute__(x)
760 #ifndef ATTRIBUTE_NORETURN
761 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
764 #ifndef ATTRIBUTE_UNUSED
765 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
769 # define ASSIGN_STRDUPA(DEST, S) \
770 do { DEST = strdupa (S); } while (0)
772 # define ASSIGN_STRDUPA(DEST, S) \
775 const char *s_ = (S); \
776 size_t len_ = strlen (s_) + 1; \
777 char *tmp_dest_ = alloca (len_); \
778 DEST = memcpy (tmp_dest_, (s_), len_); \
784 # define EOVERFLOW EINVAL
787 #if ! HAVE_FSEEKO && ! defined fseeko
788 # define fseeko(s, o, w) ((o) == (long int) (o) \
790 : (errno = EOVERFLOW, -1))
793 /* Compute the greatest common divisor of U and V using Euclid's
794 algorithm. U and V must be nonzero. */
797 gcd (size_t u, size_t v)
810 /* Compute the least common multiple of U and V. U and V must be
811 nonzero. There is no overflow checking, so callers should not
812 specify outlandish sizes. */
815 lcm (size_t u, size_t v)
817 return u * (v / gcd (u, v));
820 /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
821 ALIGNMENT must be nonzero. The caller must arrange for ((char *)
822 PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
826 ptr_align (void *ptr, size_t alignment)
829 char *p1 = p0 + alignment - 1;
830 return p1 - (size_t) p1 % alignment;