1 /* system-dependent definitions for coreutils
2 Copyright (C) 1989, 1991-2011 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. */
21 /* Include <sys/types.h> before this file.
22 Note this doesn't warn if we're included
23 before all system headers. */
25 #if 2 < __GLIBC__ || ( 2 == ___GLIBC__ && 2 <= __GLIBC_MINOR__ )
26 # if ! defined _SYS_TYPES_H
27 you must include
<sys
/types
.h
> before including
this file
33 #if !defined HAVE_MKFIFO
34 # define mkfifo(name, mode) mknod (name, (mode) | S_IFIFO, 0)
38 # include <sys/param.h>
45 # define PATH_MAX 8192
48 #include "configmake.h"
53 /* Since major is a function on SVR4, we can't use `ifndef major'. */
55 # include <sys/mkdev.h>
58 #if MAJOR_IN_SYSMACROS
59 # include <sys/sysmacros.h>
62 #ifdef major /* Might be defined in sys/types.h. */
67 # define major(dev) (((dev) >> 8) & 0xff)
68 # define minor(dev) ((dev) & 0xff)
69 # define makedev(maj, min) (((maj) << 8) | (min))
73 #if ! defined makedev && defined mkdev
74 # define makedev(maj, min) mkdev (maj, min)
77 /* Don't use bcopy! Use memmove if source and destination may overlap,
84 /* Some systems don't define this; POSIX mentions it but says it is
85 obsolete, so gnulib does not provide it either. */
94 /* Exit statuses for programs like 'env' that exec other programs. */
97 EXIT_TIMEDOUT
= 124, /* Time expired before child completed. */
98 EXIT_CANCELED
= 125, /* Internal error prior to exec attempt. */
99 EXIT_CANNOT_INVOKE
= 126, /* Program located, but not usable. */
100 EXIT_ENOENT
= 127 /* Could not find program to exec. */
103 #include "exitfail.h"
105 /* Set exit_failure to STATUS if that's not the default already. */
107 initialize_exit_failure (int status
)
109 if (status
!= EXIT_FAILURE
)
110 exit_failure
= status
;
116 #ifndef _D_EXACT_NAMLEN
117 # define _D_EXACT_NAMLEN(dp) strlen ((dp)->d_name)
122 NOT_AN_INODE_NUMBER
= 0
125 #ifdef D_INO_IN_DIRENT
126 # define D_INO(dp) (dp)->d_ino
128 /* Some systems don't have inodes, so fake them to avoid lots of ifdefs. */
129 # define D_INO(dp) NOT_AN_INODE_NUMBER
132 /* include here for SIZE_MAX. */
133 #include <inttypes.h>
135 /* Redirection and wildcarding when done by the utility itself.
136 Generally a noop, but used in particular for native VMS. */
137 #ifndef initialize_main
138 # define initialize_main(ac, av)
141 #include "stat-macros.h"
143 #include "timespec.h"
147 /* ISDIGIT differs from isdigit, as follows:
148 - Its arg may be any int or unsigned int; it need not be an unsigned char
150 - It's typically faster.
151 POSIX says that only '0' through '9' are digits. Prefer ISDIGIT to
152 isdigit unless it's important to use the locale's definition
153 of `digit' even when the host does not conform to POSIX. */
154 #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
156 /* Convert a possibly-signed character to an unsigned character. This is
157 a bit safer than casting to unsigned char, since it catches some type
158 errors that the cast doesn't. */
159 static inline unsigned char to_uchar (char ch
) { return ch
; }
163 /* Take care of NLS matters. */
168 # define textdomain(Domainname) /* empty */
169 # undef bindtextdomain
170 # define bindtextdomain(Domainname, Dirname) /* empty */
173 #define _(msgid) gettext (msgid)
174 #define N_(msgid) msgid
176 /* Return a value that pluralizes the same way that N does, in all
177 languages we know of. */
178 static inline unsigned long int
179 select_plural (uintmax_t n
)
181 /* Reduce by a power of ten, but keep it away from zero. The
182 gettext manual says 1000000 should be safe. */
183 enum { PLURAL_REDUCER
= 1000000 };
184 return (n
<= ULONG_MAX
? n
: n
% PLURAL_REDUCER
+ PLURAL_REDUCER
);
187 #define STREQ(a, b) (strcmp (a, b) == 0)
188 #define STREQ_LEN(a, b, n) (strncmp (a, b, n) == 0)
189 #define STRPREFIX(a, b) (strncmp(a, b, strlen (b)) == 0)
191 /* Just like strncmp, but the first argument must be a literal string
192 and you don't specify the length. */
193 #define STRNCMP_LIT(s, literal) \
194 strncmp (s, "" literal "", sizeof (literal) - 1)
196 #if !HAVE_DECL_GETLOGIN
200 #if !HAVE_DECL_TTYNAME
204 #if !HAVE_DECL_GETEUID
208 #if !HAVE_DECL_GETPWUID
209 struct passwd
*getpwuid ();
212 #if !HAVE_DECL_GETGRGID
213 struct group
*getgrgid ();
216 #if !HAVE_DECL_GETUID
223 /* This is simply a shorthand for the common case in which
224 the third argument to x2nrealloc would be `sizeof *(P)'.
225 Ensure that sizeof *(P) is *not* 1. In that case, it'd be
226 better to use X2REALLOC, although not strictly necessary. */
227 #define X2NREALLOC(P, PN) ((void) verify_true (sizeof *(P) != 1), \
228 x2nrealloc (P, PN, sizeof *(P)))
230 /* Using x2realloc (when appropriate) usually makes your code more
231 readable than using x2nrealloc, but it also makes it so your
232 code will malfunction if sizeof *(P) ever becomes 2 or greater.
233 So use this macro instead of using x2realloc directly. */
234 #define X2REALLOC(P, PN) ((void) verify_true (sizeof *(P) == 1), \
237 #include "unlocked-io.h"
238 #include "same-inode.h"
244 dot_or_dotdot (char const *file_name
)
246 if (file_name
[0] == '.')
248 char sep
= file_name
[(file_name
[1] == '.') + 1];
249 return (! sep
|| ISSLASH (sep
));
255 /* A wrapper for readdir so that callers don't see entries for `.' or `..'. */
256 static inline struct dirent
const *
257 readdir_ignoring_dot_and_dotdot (DIR *dirp
)
261 struct dirent
const *dp
= readdir (dirp
);
262 if (dp
== NULL
|| ! dot_or_dotdot (dp
->d_name
))
267 /* Return true if DIR is determined to be an empty directory. */
269 is_empty_dir (int fd_cwd
, char const *dir
)
272 struct dirent
const *dp
;
274 int fd
= openat (fd_cwd
, dir
,
275 (O_RDONLY
| O_DIRECTORY
276 | O_NOCTTY
| O_NOFOLLOW
| O_NONBLOCK
));
281 dirp
= fdopendir (fd
);
289 dp
= readdir_ignoring_dot_and_dotdot (dirp
);
294 return saved_errno
== 0 ? true : false;
297 /* Factor out some of the common --help and --version processing code. */
299 /* These enum values cannot possibly conflict with the option values
300 ordinarily used by commands, including CHAR_MAX + 1, etc. Avoid
301 CHAR_MIN - 1, as it may equal -1, the getopt end-of-options value. */
304 GETOPT_HELP_CHAR
= (CHAR_MIN
- 2),
305 GETOPT_VERSION_CHAR
= (CHAR_MIN
- 3)
308 #define GETOPT_HELP_OPTION_DECL \
309 "help", no_argument, NULL, GETOPT_HELP_CHAR
310 #define GETOPT_VERSION_OPTION_DECL \
311 "version", no_argument, NULL, GETOPT_VERSION_CHAR
312 #define GETOPT_SELINUX_CONTEXT_OPTION_DECL \
313 "context", required_argument, NULL, 'Z'
315 #define case_GETOPT_HELP_CHAR \
316 case GETOPT_HELP_CHAR: \
317 usage (EXIT_SUCCESS); \
320 /* Program_name must be a literal string.
321 Usually it is just PROGRAM_NAME. */
322 #define USAGE_BUILTIN_WARNING \
324 "NOTE: your shell may have its own version of %s, which usually supersedes\n" \
325 "the version described here. Please refer to your shell's documentation\n" \
326 "for details about the options it supports.\n")
328 #define HELP_OPTION_DESCRIPTION \
329 _(" --help display this help and exit\n")
330 #define VERSION_OPTION_DESCRIPTION \
331 _(" --version output version information and exit\n")
334 #include "closeout.h"
336 #define emit_bug_reporting_address unused__emit_bug_reporting_address
337 #include "version-etc.h"
338 #undef emit_bug_reporting_address
340 #include "propername.h"
341 /* Define away proper_name (leaving proper_name_utf8, which affects far
342 fewer programs), since it's not worth the cost of adding ~17KB to
343 the x86_64 text size of every single program. This avoids a 40%
344 (almost ~2MB) increase in the on-disk space utilization for the set
345 of the 100 binaries. */
346 #define proper_name(x) (x)
348 #include "progname.h"
350 #define case_GETOPT_VERSION_CHAR(Program_name, Authors) \
351 case GETOPT_VERSION_CHAR: \
352 version_etc (stdout, Program_name, PACKAGE_NAME, Version, Authors, \
354 exit (EXIT_SUCCESS); \
358 # define MAX(a, b) ((a) > (b) ? (a) : (b))
362 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
365 #include "intprops.h"
368 # define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
372 # define OFF_T_MIN TYPE_MINIMUM (off_t)
376 # define OFF_T_MAX TYPE_MAXIMUM (off_t)
380 # define UID_T_MAX TYPE_MAXIMUM (uid_t)
384 # define GID_T_MAX TYPE_MAXIMUM (gid_t)
388 # define PID_T_MAX TYPE_MAXIMUM (pid_t)
391 /* Use this to suppress gcc's `...may be used before initialized' warnings. */
393 # define IF_LINT(Code) Code
395 # define IF_LINT(Code) /* empty */
398 #ifndef __attribute__
399 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8)
400 # define __attribute__(x) /* empty */
404 #ifndef ATTRIBUTE_NORETURN
405 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
408 #ifndef ATTRIBUTE_UNUSED
409 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
412 /* The warn_unused_result attribute appeared first in gcc-3.4.0 */
413 #undef ATTRIBUTE_WARN_UNUSED_RESULT
414 #if __GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
415 # define ATTRIBUTE_WARN_UNUSED_RESULT /* empty */
417 # define ATTRIBUTE_WARN_UNUSED_RESULT __attribute__ ((__warn_unused_result__))
421 # define ASSIGN_STRDUPA(DEST, S) \
422 do { DEST = strdupa (S); } while (0)
424 # define ASSIGN_STRDUPA(DEST, S) \
427 const char *s_ = (S); \
428 size_t len_ = strlen (s_) + 1; \
429 char *tmp_dest_ = alloca (len_); \
430 DEST = memcpy (tmp_dest_, s_, len_); \
436 # define sync() /* empty */
439 /* Compute the greatest common divisor of U and V using Euclid's
440 algorithm. U and V must be nonzero. */
443 gcd (size_t u
, size_t v
)
456 /* Compute the least common multiple of U and V. U and V must be
457 nonzero. There is no overflow checking, so callers should not
458 specify outlandish sizes. */
461 lcm (size_t u
, size_t v
)
463 return u
* (v
/ gcd (u
, v
));
466 /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
467 ALIGNMENT must be nonzero. The caller must arrange for ((char *)
468 PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
472 ptr_align (void const *ptr
, size_t alignment
)
474 char const *p0
= ptr
;
475 char const *p1
= p0
+ alignment
- 1;
476 return (void *) (p1
- (size_t) p1
% alignment
);
479 /* If 10*Accum + Digit_val is larger than the maximum value for Type,
480 then don't update Accum and return false to indicate it would
481 overflow. Otherwise, set Accum to that new value and return true.
482 Verify at compile-time that Type is Accum's type, and that Type is
483 unsigned. Accum must be an object, so that we can take its
484 address. Accum and Digit_val may be evaluated multiple times.
486 The "Added check" below is not strictly required, but it causes GCC
487 to return a nonzero exit status instead of merely a warning
488 diagnostic, and that is more useful. */
490 #define DECIMAL_DIGIT_ACCUMULATE(Accum, Digit_val, Type) \
492 (void) (&(Accum) == (Type *) NULL), /* The type matches. */ \
493 (void) verify_true (! TYPE_SIGNED (Type)), /* The type is unsigned. */ \
494 (void) verify_true (sizeof (Accum) == sizeof (Type)), /* Added check. */ \
495 (((Type) -1 / 10 < (Accum) \
496 || (Type) ((Accum) * 10 + (Digit_val)) < (Accum)) \
497 ? false : (((Accum) = (Accum) * 10 + (Digit_val)), true)) \
501 emit_size_note (void)
504 SIZE may be (or may be an integer optionally followed by) one of following:\n\
505 KB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.\n\
510 emit_blocksize_note (char const *program
)
513 Display values are in units of the first available SIZE from --block-size,\n\
514 and the %s_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables.\n\
515 Otherwise, units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).\n\
520 emit_ancillary_info (void)
522 printf (_("\nReport %s bugs to %s\n"), last_component (program_name
),
524 printf (_("%s home page: <%s>\n"), PACKAGE_NAME
, PACKAGE_URL
);
525 fputs (_("General help using GNU software: <http://www.gnu.org/gethelp/>\n"),
527 /* Don't output this redundant message for English locales.
528 Note we still output for 'C' so that it gets included in the man page. */
529 const char *lc_messages
= setlocale (LC_MESSAGES
, NULL
);
530 if (lc_messages
&& STRNCMP_LIT (lc_messages
, "en_"))
532 /* TRANSLATORS: Replace LANG_CODE in this URL with your language code
533 <http://translationproject.org/team/LANG_CODE.html> to form one of
534 the URLs at http://translationproject.org/team/. Otherwise, replace
535 the entire URL with your translation team's email address. */
536 printf (_("Report %s translation bugs to "
537 "<http://translationproject.org/team/>\n"),
538 last_component (program_name
));
540 printf (_("For complete documentation, run: "
541 "info coreutils '%s invocation'\n"), last_component (program_name
));
544 #include "inttostr.h"
547 timetostr (time_t t
, char *buf
)
549 return (TYPE_SIGNED (time_t)
551 : umaxtostr (t
, buf
));
555 bad_cast (char const *s
)
560 void usage (int status
) ATTRIBUTE_NORETURN
;
562 #define emit_cycle_warning(file_name) \
566 WARNING: Circular directory structure.\n\
567 This almost certainly means that you have a corrupted file system.\n\
568 NOTIFY YOUR SYSTEM MANAGER.\n\
569 The following directory is part of the cycle:\n %s\n"), \
570 quote (file_name)); \
574 #ifndef ARRAY_CARDINALITY
575 # define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))