1 /* system-dependent definitions for coreutils
2 Copyright (C) 1989-2015 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/>. */
17 /* Include this file _after_ system headers if possible. */
23 /* Commonly used file permission combination. */
24 #define MODE_RW_UGO (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
26 #if !defined HAVE_MKFIFO
27 # define mkfifo(name, mode) mknod (name, (mode) | S_IFIFO, 0)
31 # include <sys/param.h>
40 # define PATH_MAX 8192
43 #include "configmake.h"
48 /* Since major is a function on SVR4, we can't use 'ifndef major'. */
50 # include <sys/mkdev.h>
53 #if MAJOR_IN_SYSMACROS
54 # include <sys/sysmacros.h>
57 #ifdef major /* Might be defined in sys/types.h. */
62 # define major(dev) (((dev) >> 8) & 0xff)
63 # define minor(dev) ((dev) & 0xff)
64 # define makedev(maj, min) (((maj) << 8) | (min))
68 #if ! defined makedev && defined mkdev
69 # define makedev(maj, min) mkdev (maj, min)
75 /* Some systems don't define this; POSIX mentions it but says it is
76 obsolete. gnulib defines it, but only on native Windows systems,
77 and there only because MSVC 10 does. */
86 /* Exit statuses for programs like 'env' that exec other programs. */
89 EXIT_TIMEDOUT
= 124, /* Time expired before child completed. */
90 EXIT_CANCELED
= 125, /* Internal error prior to exec attempt. */
91 EXIT_CANNOT_INVOKE
= 126, /* Program located, but not usable. */
92 EXIT_ENOENT
= 127 /* Could not find program to exec. */
97 /* Set exit_failure to STATUS if that's not the default already. */
99 initialize_exit_failure (int status
)
101 if (status
!= EXIT_FAILURE
)
102 exit_failure
= status
;
108 #ifndef _D_EXACT_NAMLEN
109 # define _D_EXACT_NAMLEN(dp) strlen ((dp)->d_name)
114 NOT_AN_INODE_NUMBER
= 0
117 #ifdef D_INO_IN_DIRENT
118 # define D_INO(dp) (dp)->d_ino
120 /* Some systems don't have inodes, so fake them to avoid lots of ifdefs. */
121 # define D_INO(dp) NOT_AN_INODE_NUMBER
124 /* include here for SIZE_MAX. */
125 #include <inttypes.h>
127 /* Redirection and wildcarding when done by the utility itself.
128 Generally a noop, but used in particular for OS/2. */
129 #ifndef initialize_main
131 # define initialize_main(ac, av)
133 # define initialize_main(ac, av) \
134 do { _wildcard (ac, av); _response (ac, av); } while (0)
138 #include "stat-macros.h"
140 #include "timespec.h"
144 /* ISDIGIT differs from isdigit, as follows:
145 - Its arg may be any int or unsigned int; it need not be an unsigned char
147 - It's typically faster.
148 POSIX says that only '0' through '9' are digits. Prefer ISDIGIT to
149 isdigit unless it's important to use the locale's definition
150 of 'digit' even when the host does not conform to POSIX. */
151 #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
153 /* Convert a possibly-signed character to an unsigned character. This is
154 a bit safer than casting to unsigned char, since it catches some type
155 errors that the cast doesn't. */
156 static inline unsigned char to_uchar (char ch
) { return ch
; }
160 /* Take care of NLS matters. */
165 # define textdomain(Domainname) /* empty */
166 # undef bindtextdomain
167 # define bindtextdomain(Domainname, Dirname) /* empty */
170 #define _(msgid) gettext (msgid)
171 #define N_(msgid) msgid
173 /* Return a value that pluralizes the same way that N does, in all
174 languages we know of. */
175 static inline unsigned long int
176 select_plural (uintmax_t n
)
178 /* Reduce by a power of ten, but keep it away from zero. The
179 gettext manual says 1000000 should be safe. */
180 enum { PLURAL_REDUCER
= 1000000 };
181 return (n
<= ULONG_MAX
? n
: n
% PLURAL_REDUCER
+ PLURAL_REDUCER
);
184 #define STREQ(a, b) (strcmp (a, b) == 0)
185 #define STREQ_LEN(a, b, n) (strncmp (a, b, n) == 0)
186 #define STRPREFIX(a, b) (strncmp (a, b, strlen (b)) == 0)
188 /* Just like strncmp, but the second argument must be a literal string
189 and you don't specify the length; that comes from the literal. */
190 #define STRNCMP_LIT(s, lit) strncmp (s, "" lit "", sizeof (lit) - 1)
192 #if !HAVE_DECL_GETLOGIN
196 #if !HAVE_DECL_TTYNAME
200 #if !HAVE_DECL_GETEUID
204 #if !HAVE_DECL_GETPWUID
205 struct passwd
*getpwuid ();
208 #if !HAVE_DECL_GETGRGID
209 struct group
*getgrgid ();
212 /* Interix has replacements for getgr{gid,nam,ent}, that don't
213 query the domain controller for group members when not required.
214 This speeds up the calls tremendously (<1 ms vs. >3 s). */
215 /* To protect any system that could provide _nomembers functions
216 other than interix, check for HAVE_SETGROUPS, as interix is
217 one of the very few (the only?) platform that lacks it */
219 # if HAVE_GETGRGID_NOMEMBERS
220 # define getgrgid(gid) getgrgid_nomembers(gid)
222 # if HAVE_GETGRNAM_NOMEMBERS
223 # define getgrnam(nam) getgrnam_nomembers(nam)
225 # if HAVE_GETGRENT_NOMEMBERS
226 # define getgrent() getgrent_nomembers()
230 #if !HAVE_DECL_GETUID
237 /* This is simply a shorthand for the common case in which
238 the third argument to x2nrealloc would be 'sizeof *(P)'.
239 Ensure that sizeof *(P) is *not* 1. In that case, it'd be
240 better to use X2REALLOC, although not strictly necessary. */
241 #define X2NREALLOC(P, PN) ((void) verify_true (sizeof *(P) != 1), \
242 x2nrealloc (P, PN, sizeof *(P)))
244 /* Using x2realloc (when appropriate) usually makes your code more
245 readable than using x2nrealloc, but it also makes it so your
246 code will malfunction if sizeof *(P) ever becomes 2 or greater.
247 So use this macro instead of using x2realloc directly. */
248 #define X2REALLOC(P, PN) ((void) verify_true (sizeof *(P) == 1), \
251 #include "unlocked-io.h"
252 #include "same-inode.h"
258 dot_or_dotdot (char const *file_name
)
260 if (file_name
[0] == '.')
262 char sep
= file_name
[(file_name
[1] == '.') + 1];
263 return (! sep
|| ISSLASH (sep
));
269 /* A wrapper for readdir so that callers don't see entries for '.' or '..'. */
270 static inline struct dirent
const *
271 readdir_ignoring_dot_and_dotdot (DIR *dirp
)
275 struct dirent
const *dp
= readdir (dirp
);
276 if (dp
== NULL
|| ! dot_or_dotdot (dp
->d_name
))
281 /* Return true if DIR is determined to be an empty directory. */
283 is_empty_dir (int fd_cwd
, char const *dir
)
286 struct dirent
const *dp
;
288 int fd
= openat (fd_cwd
, dir
,
289 (O_RDONLY
| O_DIRECTORY
290 | O_NOCTTY
| O_NOFOLLOW
| O_NONBLOCK
));
295 dirp
= fdopendir (fd
);
303 dp
= readdir_ignoring_dot_and_dotdot (dirp
);
308 return saved_errno
== 0 ? true : false;
311 /* Factor out some of the common --help and --version processing code. */
313 /* These enum values cannot possibly conflict with the option values
314 ordinarily used by commands, including CHAR_MAX + 1, etc. Avoid
315 CHAR_MIN - 1, as it may equal -1, the getopt end-of-options value. */
318 GETOPT_HELP_CHAR
= (CHAR_MIN
- 2),
319 GETOPT_VERSION_CHAR
= (CHAR_MIN
- 3)
322 #define GETOPT_HELP_OPTION_DECL \
323 "help", no_argument, NULL, GETOPT_HELP_CHAR
324 #define GETOPT_VERSION_OPTION_DECL \
325 "version", no_argument, NULL, GETOPT_VERSION_CHAR
326 #define GETOPT_SELINUX_CONTEXT_OPTION_DECL \
327 "context", optional_argument, NULL, 'Z'
329 #define case_GETOPT_HELP_CHAR \
330 case GETOPT_HELP_CHAR: \
331 usage (EXIT_SUCCESS); \
334 /* Program_name must be a literal string.
335 Usually it is just PROGRAM_NAME. */
336 #define USAGE_BUILTIN_WARNING \
338 "NOTE: your shell may have its own version of %s, which usually supersedes\n" \
339 "the version described here. Please refer to your shell's documentation\n" \
340 "for details about the options it supports.\n")
342 #define HELP_OPTION_DESCRIPTION \
343 _(" --help display this help and exit\n")
344 #define VERSION_OPTION_DESCRIPTION \
345 _(" --version output version information and exit\n")
348 #include "closeout.h"
350 #define emit_bug_reporting_address unused__emit_bug_reporting_address
351 #include "version-etc.h"
352 #undef emit_bug_reporting_address
354 #include "propername.h"
355 /* Define away proper_name (leaving proper_name_utf8, which affects far
356 fewer programs), since it's not worth the cost of adding ~17KB to
357 the x86_64 text size of every single program. This avoids a 40%
358 (almost ~2MB) increase in the on-disk space utilization for the set
359 of the 100 binaries. */
360 #define proper_name(x) (x)
362 #include "progname.h"
364 #define case_GETOPT_VERSION_CHAR(Program_name, Authors) \
365 case GETOPT_VERSION_CHAR: \
366 version_etc (stdout, Program_name, PACKAGE_NAME, Version, Authors, \
368 exit (EXIT_SUCCESS); \
372 # define MAX(a, b) ((a) > (b) ? (a) : (b))
376 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
379 #include "intprops.h"
382 # define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
386 # define OFF_T_MIN TYPE_MINIMUM (off_t)
390 # define OFF_T_MAX TYPE_MAXIMUM (off_t)
394 # define UID_T_MAX TYPE_MAXIMUM (uid_t)
398 # define GID_T_MAX TYPE_MAXIMUM (gid_t)
402 # define PID_T_MAX TYPE_MAXIMUM (pid_t)
405 /* Use this to suppress gcc's '...may be used before initialized' warnings. */
407 # define IF_LINT(Code) Code
409 # define IF_LINT(Code) /* empty */
412 #ifndef __attribute__
413 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8)
414 # define __attribute__(x) /* empty */
418 #ifndef ATTRIBUTE_NORETURN
419 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
422 /* The warn_unused_result attribute appeared first in gcc-3.4.0 */
423 #undef ATTRIBUTE_WARN_UNUSED_RESULT
424 #if __GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
425 # define ATTRIBUTE_WARN_UNUSED_RESULT /* empty */
427 # define ATTRIBUTE_WARN_UNUSED_RESULT __attribute__ ((__warn_unused_result__))
431 # define LIKELY(cond) __builtin_expect ((cond), 1)
432 # define UNLIKELY(cond) __builtin_expect ((cond), 0)
434 # define LIKELY(cond) (cond)
435 # define UNLIKELY(cond) (cond)
440 # define ASSIGN_STRDUPA(DEST, S) \
441 do { DEST = strdupa (S); } while (0)
443 # define ASSIGN_STRDUPA(DEST, S) \
446 const char *s_ = (S); \
447 size_t len_ = strlen (s_) + 1; \
448 char *tmp_dest_ = alloca (len_); \
449 DEST = memcpy (tmp_dest_, s_, len_); \
455 # define sync() /* empty */
458 /* Compute the greatest common divisor of U and V using Euclid's
459 algorithm. U and V must be nonzero. */
461 static inline size_t _GL_ATTRIBUTE_CONST
462 gcd (size_t u
, size_t v
)
475 /* Compute the least common multiple of U and V. U and V must be
476 nonzero. There is no overflow checking, so callers should not
477 specify outlandish sizes. */
479 static inline size_t _GL_ATTRIBUTE_CONST
480 lcm (size_t u
, size_t v
)
482 return u
* (v
/ gcd (u
, v
));
485 /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
486 ALIGNMENT must be nonzero. The caller must arrange for ((char *)
487 PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
491 ptr_align (void const *ptr
, size_t alignment
)
493 char const *p0
= ptr
;
494 char const *p1
= p0
+ alignment
- 1;
495 return (void *) (p1
- (size_t) p1
% alignment
);
498 /* Return whether the buffer consists entirely of NULs.
499 Based on memeqzero in CCAN by Rusty Russell under CC0 (Public domain). */
501 static inline bool _GL_ATTRIBUTE_PURE
502 is_nul (void const *buf
, size_t length
)
504 const unsigned char *p
= buf
;
505 /* Using possibly unaligned access for the first 16 bytes
506 saves about 30-40 cycles, though it is strictly undefined behavior
507 and so would need __attribute__ ((__no_sanitize_undefined__))
508 to avoid -fsanitize=undefined warnings.
509 Considering coreutils is mainly concerned with relatively
510 large buffers, we'll just use the defined behavior. */
511 #if 0 && _STRING_ARCH_unaligned
520 /* Check len bytes not aligned on a word. */
521 while (UNLIKELY (length
& (sizeof word
- 1)))
531 /* Check up to 16 bytes a word at a time. */
534 memcpy (&word
, p
, sizeof word
);
538 length
-= sizeof word
;
541 if (UNLIKELY (length
& 15) == 0)
545 /* Now we know first 16 bytes are NUL, memcmp with self. */
546 return memcmp (buf
, p
, length
) == 0;
549 /* If 10*Accum + Digit_val is larger than the maximum value for Type,
550 then don't update Accum and return false to indicate it would
551 overflow. Otherwise, set Accum to that new value and return true.
552 Verify at compile-time that Type is Accum's type, and that Type is
553 unsigned. Accum must be an object, so that we can take its
554 address. Accum and Digit_val may be evaluated multiple times.
556 The "Added check" below is not strictly required, but it causes GCC
557 to return a nonzero exit status instead of merely a warning
558 diagnostic, and that is more useful. */
560 #define DECIMAL_DIGIT_ACCUMULATE(Accum, Digit_val, Type) \
562 (void) (&(Accum) == (Type *) NULL), /* The type matches. */ \
563 (void) verify_true (! TYPE_SIGNED (Type)), /* The type is unsigned. */ \
564 (void) verify_true (sizeof (Accum) == sizeof (Type)), /* Added check. */ \
565 (((Type) -1 / 10 < (Accum) \
566 || (Type) ((Accum) * 10 + (Digit_val)) < (Accum)) \
567 ? false : (((Accum) = (Accum) * 10 + (Digit_val)), true)) \
571 emit_stdin_note (void)
574 With no FILE, or when FILE is -, read standard input.\n\
578 emit_mandatory_arg_note (void)
581 Mandatory arguments to long options are mandatory for short options too.\n\
586 emit_size_note (void)
589 The SIZE argument is an integer and optional unit (example: 10K is 10*1024).\n\
590 Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).\n\
595 emit_blocksize_note (char const *program
)
598 Display values are in units of the first available SIZE from --block-size,\n\
599 and the %s_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables.\n\
600 Otherwise, units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).\n\
605 emit_ancillary_info (char const *program
)
607 struct infomap
{ char const *program
; char const *node
; } const infomap
[] = {
608 { "[", "test invocation" },
609 { "coreutils", "Multi-call invocation" },
610 { "sha224sum", "sha2 utilities" },
611 { "sha256sum", "sha2 utilities" },
612 { "sha384sum", "sha2 utilities" },
613 { "sha512sum", "sha2 utilities" },
617 char const *node
= program
;
618 struct infomap
const *map_prog
= infomap
;
620 while (map_prog
->program
&& ! STREQ (program
, map_prog
->program
))
624 node
= map_prog
->node
;
626 printf (_("\n%s online help: <%s>\n"), PACKAGE_NAME
, PACKAGE_URL
);
628 /* Don't output this redundant message for English locales.
629 Note we still output for 'C' so that it gets included in the man page. */
630 const char *lc_messages
= setlocale (LC_MESSAGES
, NULL
);
631 if (lc_messages
&& STRNCMP_LIT (lc_messages
, "en_"))
633 /* TRANSLATORS: Replace LANG_CODE in this URL with your language code
634 <http://translationproject.org/team/LANG_CODE.html> to form one of
635 the URLs at http://translationproject.org/team/. Otherwise, replace
636 the entire URL with your translation team's email address. */
637 printf (_("Report %s translation bugs to "
638 "<http://translationproject.org/team/>\n"), program
);
640 printf (_("Full documentation at: <%s%s>\n"),
641 PACKAGE_URL
, program
);
642 printf (_("or available locally via: info '(coreutils) %s%s'\n"),
643 node
, node
== program
? " invocation" : "");
649 fprintf (stderr
, _("Try '%s --help' for more information.\n"), program_name
);
652 #include "inttostr.h"
655 timetostr (time_t t
, char *buf
)
657 return (TYPE_SIGNED (time_t)
659 : umaxtostr (t
, buf
));
663 bad_cast (char const *s
)
668 /* Return a boolean indicating whether SB->st_size is defined. */
670 usable_st_size (struct stat
const *sb
)
672 return (S_ISREG (sb
->st_mode
) || S_ISLNK (sb
->st_mode
)
673 || S_TYPEISSHM (sb
) || S_TYPEISTMO (sb
));
676 void usage (int status
) ATTRIBUTE_NORETURN
;
678 /* Like error(0, 0, ...), but without an implicit newline.
679 Also a noop unless the global DEV_DEBUG is set. */
680 #define devmsg(...) \
684 fprintf (stderr, __VA_ARGS__); \
688 #define emit_cycle_warning(file_name) \
692 WARNING: Circular directory structure.\n\
693 This almost certainly means that you have a corrupted file system.\n\
694 NOTIFY YOUR SYSTEM MANAGER.\n\
695 The following directory is part of the cycle:\n %s\n"), \
696 quote (file_name)); \
700 /* Like stpncpy, but do ensure that the result is NUL-terminated,
701 and do not NUL-pad out to LEN. I.e., when strnlen (src, len) == len,
702 this function writes a NUL byte into dest[len]. Thus, the length
703 of the destination buffer must be at least LEN + 1.
704 The DEST and SRC buffers must not overlap. */
706 stzncpy (char *restrict dest
, char const *restrict src
, size_t len
)
708 char const *src_end
= src
+ len
;
709 while (src
< src_end
&& *src
)
715 #ifndef ARRAY_CARDINALITY
716 # define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
719 /* Avoid const warnings by casting to more portable type.
720 This is to cater for the incorrect const function declarations
721 in selinux.h before libselinux-2.3 (May 2014).
722 When version >= 2.3 is ubiquitous remove this function. */
723 static inline char * se_const (char const * sctx
) { return (char *) sctx
; }
725 /* Return true if ERR is ENOTSUP or EOPNOTSUPP, otherwise false.
726 This wrapper function avoids the redundant 'or'd comparison on
727 systems like Linux for which they have the same value. It also
728 avoids the gcc warning to that effect. */
732 return err
== EOPNOTSUPP
|| (ENOTSUP
!= EOPNOTSUPP
&& err
== ENOTSUP
);