1 /* system-dependent definitions for coreutils
2 Copyright (C) 1989, 1991-2008 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 3 of the License, or
7 (at your option) 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, see <http://www.gnu.org/licenses/>. */
19 /* Include sys/types.h before this file. */
21 #if 2 <= __GLIBC__ && 2 <= __GLIBC_MINOR__
22 # if ! defined _SYS_TYPES_H
23 you must include
<sys
/types
.h
> before including
this file
29 #if !defined HAVE_MKFIFO
30 # define mkfifo(name, mode) mknod (name, (mode) | S_IFIFO, 0)
34 # include <sys/param.h>
40 # define STDIN_FILENO 0
44 # define STDOUT_FILENO 1
48 # define STDERR_FILENO 2
52 /* limits.h must come before pathmax.h because limits.h on some systems
53 undefs PATH_MAX, whereas pathmax.h sets PATH_MAX. */
58 #include "configmake.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,
102 /* Some systems don't define the following symbols. */
113 # define ENODATA (-1)
119 /* Exit statuses for programs like 'env' that exec other programs. */
122 EXIT_CANNOT_INVOKE
= 126,
126 #include "exitfail.h"
128 /* Set exit_failure to STATUS if that's not the default already. */
130 initialize_exit_failure (int status
)
132 if (status
!= EXIT_FAILURE
)
133 exit_failure
= status
;
146 #ifndef _D_EXACT_NAMLEN
147 # define _D_EXACT_NAMLEN(dp) strlen ((dp)->d_name)
152 NOT_AN_INODE_NUMBER
= 0
155 #ifdef D_INO_IN_DIRENT
156 # define D_INO(dp) (dp)->d_ino
158 /* Some systems don't have inodes, so fake them to avoid lots of ifdefs. */
159 # define D_INO(dp) NOT_AN_INODE_NUMBER
162 /* Get or fake the disk device blocksize.
163 Usually defined by sys/param.h (if at all). */
164 #if !defined DEV_BSIZE && defined BSIZE
165 # define DEV_BSIZE BSIZE
167 #if !defined DEV_BSIZE && defined BBSIZE /* SGI */
168 # define DEV_BSIZE BBSIZE
171 # define DEV_BSIZE 4096
174 /* Extract or fake data from a `struct stat'.
175 ST_BLKSIZE: Preferred I/O blocksize for the file, in bytes.
176 ST_NBLOCKS: Number of blocks in the file, including indirect blocks.
177 ST_NBLOCKSIZE: Size of blocks used when calculating ST_NBLOCKS. */
178 #ifndef HAVE_STRUCT_STAT_ST_BLOCKS
179 # define ST_BLKSIZE(statbuf) DEV_BSIZE
180 # if defined _POSIX_SOURCE || !defined BSIZE /* fileblocks.c uses BSIZE. */
181 # define ST_NBLOCKS(statbuf) \
182 ((statbuf).st_size / ST_NBLOCKSIZE + ((statbuf).st_size % ST_NBLOCKSIZE != 0))
183 # else /* !_POSIX_SOURCE && BSIZE */
184 # define ST_NBLOCKS(statbuf) \
185 (S_ISREG ((statbuf).st_mode) \
186 || S_ISDIR ((statbuf).st_mode) \
187 ? st_blocks ((statbuf).st_size) : 0)
188 # endif /* !_POSIX_SOURCE && BSIZE */
189 #else /* HAVE_STRUCT_STAT_ST_BLOCKS */
190 /* Some systems, like Sequents, return st_blksize of 0 on pipes.
191 Also, when running `rsh hpux11-system cat any-file', cat would
192 determine that the output stream had an st_blksize of 2147421096.
193 Conversely st_blksize can be 2 GiB (or maybe even larger) with XFS
194 on 64-bit hosts. Somewhat arbitrarily, limit the `optimal' block
195 size to SIZE_MAX / 8 + 1. (Dividing SIZE_MAX by only 4 wouldn't
196 suffice, since "cat" sometimes multiplies the result by 4.) If
197 anyone knows of a system for which this limit is too small, please
198 report it as a bug in this code. */
199 # define ST_BLKSIZE(statbuf) ((0 < (statbuf).st_blksize \
200 && (statbuf).st_blksize <= SIZE_MAX / 8 + 1) \
201 ? (statbuf).st_blksize : DEV_BSIZE)
202 # if defined hpux || defined __hpux__ || defined __hpux
203 /* HP-UX counts st_blocks in 1024-byte units.
204 This loses when mixing HP-UX and BSD file systems with NFS. */
205 # define ST_NBLOCKSIZE 1024
207 # if defined _AIX && defined _I386
208 /* AIX PS/2 counts st_blocks in 4K units. */
209 # define ST_NBLOCKSIZE (4 * 1024)
210 # else /* not AIX PS/2 */
212 # define ST_NBLOCKS(statbuf) \
213 (S_ISREG ((statbuf).st_mode) \
214 || S_ISDIR ((statbuf).st_mode) \
215 ? (statbuf).st_blocks * ST_BLKSIZE(statbuf)/ST_NBLOCKSIZE : 0)
217 # endif /* not AIX PS/2 */
219 #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
222 # define ST_NBLOCKS(statbuf) ((statbuf).st_blocks)
225 #ifndef ST_NBLOCKSIZE
227 # define ST_NBLOCKSIZE S_BLKSIZE
229 # define ST_NBLOCKSIZE 512
233 /* Redirection and wildcarding when done by the utility itself.
234 Generally a noop, but used in particular for native VMS. */
235 #ifndef initialize_main
236 # define initialize_main(ac, av)
239 #include "stat-macros.h"
241 #include "timespec.h"
243 #include <inttypes.h>
247 #if ! (defined isblank || HAVE_DECL_ISBLANK)
248 # define isblank(c) ((c) == ' ' || (c) == '\t')
251 /* ISDIGIT differs from isdigit, as follows:
252 - Its arg may be any int or unsigned int; it need not be an unsigned char
254 - It's typically faster.
255 POSIX says that only '0' through '9' are digits. Prefer ISDIGIT to
256 isdigit unless it's important to use the locale's definition
257 of `digit' even when the host does not conform to POSIX. */
258 #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
260 /* Convert a possibly-signed character to an unsigned character. This is
261 a bit safer than casting to unsigned char, since it catches some type
262 errors that the cast doesn't. */
263 static inline unsigned char to_uchar (char ch
) { return ch
; }
267 /* Take care of NLS matters. */
272 # define textdomain(Domainname) /* empty */
273 # undef bindtextdomain
274 # define bindtextdomain(Domainname, Dirname) /* empty */
277 #define _(msgid) gettext (msgid)
278 #define N_(msgid) msgid
280 /* Return a value that pluralizes the same way that N does, in all
281 languages we know of. */
282 static inline unsigned long int
283 select_plural (uintmax_t n
)
285 /* Reduce by a power of ten, but keep it away from zero. The
286 gettext manual says 1000000 should be safe. */
287 enum { PLURAL_REDUCER
= 1000000 };
288 return (n
<= ULONG_MAX
? n
: n
% PLURAL_REDUCER
+ PLURAL_REDUCER
);
291 #define STREQ(a, b) (strcmp (a, b) == 0)
297 #if !HAVE_DECL_MALLOC
301 #if !HAVE_DECL_MEMCHR
305 #if !HAVE_DECL_REALLOC
309 #if !HAVE_DECL_GETENV
317 #if !HAVE_DECL_GETLOGIN
321 #if !HAVE_DECL_TTYNAME
325 #if !HAVE_DECL_GETEUID
329 #if !HAVE_DECL_GETPWUID
330 struct passwd
*getpwuid ();
333 #if !HAVE_DECL_GETGRGID
334 struct group
*getgrgid ();
337 #if !HAVE_DECL_GETUID
344 /* This is simply a shorthand for the common case in which
345 the third argument to x2nrealloc would be `sizeof *(P)'.
346 Ensure that sizeof *(P) is *not* 1. In that case, it'd be
347 better to use X2REALLOC, although not strictly necessary. */
348 #define X2NREALLOC(P, PN) ((void) verify_true (sizeof *(P) != 1), \
349 x2nrealloc (P, PN, sizeof *(P)))
351 /* Using x2realloc (when appropriate) usually makes your code more
352 readable than using x2nrealloc, but it also makes it so your
353 code will malfunction if sizeof *(P) ever becomes 2 or greater.
354 So use this macro instead of using x2realloc directly. */
355 #define X2REALLOC(P, PN) ((void) verify_true (sizeof *(P) == 1), \
358 #include "unlocked-io.h"
359 #include "same-inode.h"
365 dot_or_dotdot (char const *file_name
)
367 if (file_name
[0] == '.')
369 char sep
= file_name
[(file_name
[1] == '.') + 1];
370 return (! sep
|| ISSLASH (sep
));
376 /* A wrapper for readdir so that callers don't see entries for `.' or `..'. */
377 static inline struct dirent
const *
378 readdir_ignoring_dot_and_dotdot (DIR *dirp
)
382 struct dirent
const *dp
= readdir (dirp
);
383 if (dp
== NULL
|| ! dot_or_dotdot (dp
->d_name
))
388 /* Return true if DIR is determined to be an empty directory. */
390 is_empty_dir (int fd_cwd
, char const *dir
)
393 struct dirent
const *dp
;
395 int fd
= openat (fd_cwd
, dir
,
396 (O_RDONLY
| O_DIRECTORY
397 | O_NOCTTY
| O_NOFOLLOW
| O_NONBLOCK
));
402 dirp
= fdopendir (fd
);
410 dp
= readdir_ignoring_dot_and_dotdot (dirp
);
415 return saved_errno
== 0 ? true : false;
418 /* Factor out some of the common --help and --version processing code. */
420 /* These enum values cannot possibly conflict with the option values
421 ordinarily used by commands, including CHAR_MAX + 1, etc. Avoid
422 CHAR_MIN - 1, as it may equal -1, the getopt end-of-options value. */
425 GETOPT_HELP_CHAR
= (CHAR_MIN
- 2),
426 GETOPT_VERSION_CHAR
= (CHAR_MIN
- 3)
429 #define GETOPT_HELP_OPTION_DECL \
430 "help", no_argument, NULL, GETOPT_HELP_CHAR
431 #define GETOPT_VERSION_OPTION_DECL \
432 "version", no_argument, NULL, GETOPT_VERSION_CHAR
433 #define GETOPT_SELINUX_CONTEXT_OPTION_DECL \
434 "context", required_argument, NULL, 'Z'
436 #define case_GETOPT_HELP_CHAR \
437 case GETOPT_HELP_CHAR: \
438 usage (EXIT_SUCCESS); \
441 /* Program_name must be a literal string.
442 Usually it is just PROGRAM_NAME. */
443 #define USAGE_BUILTIN_WARNING \
445 "NOTE: your shell may have its own version of %s, which usually supersedes\n" \
446 "the version described here. Please refer to your shell's documentation\n" \
447 "for details about the options it supports.\n")
449 #define HELP_OPTION_DESCRIPTION \
450 _(" --help display this help and exit\n")
451 #define VERSION_OPTION_DESCRIPTION \
452 _(" --version output version information and exit\n")
455 #include "closeout.h"
456 #include "version-etc.h"
457 #include "propername.h"
458 /* Define away proper_name (leaving proper_name_utf8, which affects far
459 fewer programs), since it's not worth the cost of adding ~17KB to
460 the x86_64 text size of every single program. This avoids a 40%
461 (almost ~2MB) increase in the on-disk space utilization for the set
462 of the 100 binaries. */
463 #define proper_name(x) (x)
465 #define case_GETOPT_VERSION_CHAR(Program_name, Authors) \
466 case GETOPT_VERSION_CHAR: \
467 version_etc (stdout, Program_name, PACKAGE_NAME, VERSION, Authors, \
469 exit (EXIT_SUCCESS); \
473 # define MAX(a, b) ((a) > (b) ? (a) : (b))
477 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
480 #include "intprops.h"
483 # define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
487 # define OFF_T_MIN TYPE_MINIMUM (off_t)
491 # define OFF_T_MAX TYPE_MAXIMUM (off_t)
495 # define UID_T_MAX TYPE_MAXIMUM (uid_t)
499 # define GID_T_MAX TYPE_MAXIMUM (gid_t)
503 # define PID_T_MAX TYPE_MAXIMUM (pid_t)
506 /* Use this to suppress gcc's `...may be used before initialized' warnings. */
508 # define IF_LINT(Code) Code
510 # define IF_LINT(Code) /* empty */
513 #ifndef __attribute__
514 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8)
515 # define __attribute__(x) /* empty */
519 #ifndef ATTRIBUTE_NORETURN
520 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
523 #ifndef ATTRIBUTE_UNUSED
524 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
528 # define ASSIGN_STRDUPA(DEST, S) \
529 do { DEST = strdupa (S); } while (0)
531 # define ASSIGN_STRDUPA(DEST, S) \
534 const char *s_ = (S); \
535 size_t len_ = strlen (s_) + 1; \
536 char *tmp_dest_ = alloca (len_); \
537 DEST = memcpy (tmp_dest_, s_, len_); \
543 # define EOVERFLOW EINVAL
547 # define sync() /* empty */
550 /* Compute the greatest common divisor of U and V using Euclid's
551 algorithm. U and V must be nonzero. */
554 gcd (size_t u
, size_t v
)
567 /* Compute the least common multiple of U and V. U and V must be
568 nonzero. There is no overflow checking, so callers should not
569 specify outlandish sizes. */
572 lcm (size_t u
, size_t v
)
574 return u
* (v
/ gcd (u
, v
));
577 /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
578 ALIGNMENT must be nonzero. The caller must arrange for ((char *)
579 PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
583 ptr_align (void const *ptr
, size_t alignment
)
585 char const *p0
= ptr
;
586 char const *p1
= p0
+ alignment
- 1;
587 return (void *) (p1
- (size_t) p1
% alignment
);
590 /* If 10*Accum + Digit_val is larger than the maximum value for Type,
591 then don't update Accum and return false to indicate it would
592 overflow. Otherwise, set Accum to that new value and return true.
593 Verify at compile-time that Type is Accum's type, and that Type is
594 unsigned. Accum must be an object, so that we can take its
595 address. Accum and Digit_val may be evaluated multiple times.
597 The "Added check" below is not strictly required, but it causes GCC
598 to return a nonzero exit status instead of merely a warning
599 diagnostic, and that is more useful. */
601 #define DECIMAL_DIGIT_ACCUMULATE(Accum, Digit_val, Type) \
603 (void) (&(Accum) == (Type *) NULL), /* The type matches. */ \
604 (void) verify_true (! TYPE_SIGNED (Type)), /* The type is unsigned. */ \
605 (void) verify_true (sizeof (Accum) == sizeof (Type)), /* Added check. */ \
606 (((Type) -1 / 10 < (Accum) \
607 || (Type) ((Accum) * 10 + (Digit_val)) < (Accum)) \
608 ? false : (((Accum) = (Accum) * 10 + (Digit_val)), true)) \
612 emit_bug_reporting_address (void)
614 /* TRANSLATORS: The placeholder indicates the bug-reporting address
615 for this package. Please add _another line_ saying
616 "Report translation bugs to <...>\n" with the address for translation
617 bugs (typically your translation team's web or email address). */
618 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);