doc: improve ls --help grammar
[coreutils/ericb.git] / src / system.h
blobd250d94f8da65a558cdf6d5be46f10a4931af652
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. */
19 #include <alloca.h>
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
28 # endif
29 #endif
31 #include <sys/stat.h>
33 #if !defined HAVE_MKFIFO
34 # define mkfifo(name, mode) mknod (name, (mode) | S_IFIFO, 0)
35 #endif
37 #if HAVE_SYS_PARAM_H
38 # include <sys/param.h>
39 #endif
41 #include <unistd.h>
43 #include <limits.h>
44 #ifndef PATH_MAX
45 # define PATH_MAX 8192
46 #endif
48 #include "configmake.h"
50 #include <sys/time.h>
51 #include <time.h>
53 /* Since major is a function on SVR4, we can't use `ifndef major'. */
54 #if MAJOR_IN_MKDEV
55 # include <sys/mkdev.h>
56 # define HAVE_MAJOR
57 #endif
58 #if MAJOR_IN_SYSMACROS
59 # include <sys/sysmacros.h>
60 # define HAVE_MAJOR
61 #endif
62 #ifdef major /* Might be defined in sys/types.h. */
63 # define HAVE_MAJOR
64 #endif
66 #ifndef HAVE_MAJOR
67 # define major(dev) (((dev) >> 8) & 0xff)
68 # define minor(dev) ((dev) & 0xff)
69 # define makedev(maj, min) (((maj) << 8) | (min))
70 #endif
71 #undef HAVE_MAJOR
73 #if ! defined makedev && defined mkdev
74 # define makedev(maj, min) mkdev (maj, min)
75 #endif
77 /* Don't use bcopy! Use memmove if source and destination may overlap,
78 memcpy otherwise. */
80 #include <string.h>
82 #include <errno.h>
84 /* Some systems don't define this; POSIX mentions it but says it is
85 obsolete, so gnulib does not provide it either. */
86 #ifndef ENODATA
87 # define ENODATA (-1)
88 #endif
90 #include <stdbool.h>
91 #include <stdlib.h>
92 #include "version.h"
94 /* Exit statuses for programs like 'env' that exec other programs. */
95 enum
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. */
106 static inline void
107 initialize_exit_failure (int status)
109 if (status != EXIT_FAILURE)
110 exit_failure = status;
113 #include <fcntl.h>
115 #include <dirent.h>
116 #ifndef _D_EXACT_NAMLEN
117 # define _D_EXACT_NAMLEN(dp) strlen ((dp)->d_name)
118 #endif
120 enum
122 NOT_AN_INODE_NUMBER = 0
125 #ifdef D_INO_IN_DIRENT
126 # define D_INO(dp) (dp)->d_ino
127 #else
128 /* Some systems don't have inodes, so fake them to avoid lots of ifdefs. */
129 # define D_INO(dp) NOT_AN_INODE_NUMBER
130 #endif
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)
139 #endif
141 #include "stat-macros.h"
143 #include "timespec.h"
145 #include <ctype.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
149 or EOF.
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; }
161 #include <locale.h>
163 /* Take care of NLS matters. */
165 #include "gettext.h"
166 #if ! ENABLE_NLS
167 # undef textdomain
168 # define textdomain(Domainname) /* empty */
169 # undef bindtextdomain
170 # define bindtextdomain(Domainname, Dirname) /* empty */
171 #endif
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
197 char *getlogin ();
198 #endif
200 #if !HAVE_DECL_TTYNAME
201 char *ttyname ();
202 #endif
204 #if !HAVE_DECL_GETEUID
205 uid_t geteuid ();
206 #endif
208 #if !HAVE_DECL_GETPWUID
209 struct passwd *getpwuid ();
210 #endif
212 #if !HAVE_DECL_GETGRGID
213 struct group *getgrgid ();
214 #endif
216 #if !HAVE_DECL_GETUID
217 uid_t getuid ();
218 #endif
220 #include "xalloc.h"
221 #include "verify.h"
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), \
235 x2realloc (P, PN))
237 #include "unlocked-io.h"
238 #include "same-inode.h"
240 #include "dirname.h"
241 #include "openat.h"
243 static inline bool
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));
251 else
252 return false;
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)
259 while (1)
261 struct dirent const *dp = readdir (dirp);
262 if (dp == NULL || ! dot_or_dotdot (dp->d_name))
263 return dp;
267 /* Return true if DIR is determined to be an empty directory. */
268 static inline bool
269 is_empty_dir (int fd_cwd, char const *dir)
271 DIR *dirp;
272 struct dirent const *dp;
273 int saved_errno;
274 int fd = openat (fd_cwd, dir,
275 (O_RDONLY | O_DIRECTORY
276 | O_NOCTTY | O_NOFOLLOW | O_NONBLOCK));
278 if (fd < 0)
279 return false;
281 dirp = fdopendir (fd);
282 if (dirp == NULL)
284 close (fd);
285 return false;
288 errno = 0;
289 dp = readdir_ignoring_dot_and_dotdot (dirp);
290 saved_errno = errno;
291 closedir (dirp);
292 if (dp != NULL)
293 return false;
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. */
302 enum
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); \
318 break;
320 /* Program_name must be a literal string.
321 Usually it is just PROGRAM_NAME. */
322 #define USAGE_BUILTIN_WARNING \
323 _("\n" \
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")
333 #include "closein.h"
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, \
353 (char *) NULL); \
354 exit (EXIT_SUCCESS); \
355 break;
357 #ifndef MAX
358 # define MAX(a, b) ((a) > (b) ? (a) : (b))
359 #endif
361 #ifndef MIN
362 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
363 #endif
365 #include "intprops.h"
367 #ifndef SSIZE_MAX
368 # define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
369 #endif
371 #ifndef OFF_T_MIN
372 # define OFF_T_MIN TYPE_MINIMUM (off_t)
373 #endif
375 #ifndef OFF_T_MAX
376 # define OFF_T_MAX TYPE_MAXIMUM (off_t)
377 #endif
379 #ifndef UID_T_MAX
380 # define UID_T_MAX TYPE_MAXIMUM (uid_t)
381 #endif
383 #ifndef GID_T_MAX
384 # define GID_T_MAX TYPE_MAXIMUM (gid_t)
385 #endif
387 #ifndef PID_T_MAX
388 # define PID_T_MAX TYPE_MAXIMUM (pid_t)
389 #endif
391 /* Use this to suppress gcc's `...may be used before initialized' warnings. */
392 #ifdef lint
393 # define IF_LINT(Code) Code
394 #else
395 # define IF_LINT(Code) /* empty */
396 #endif
398 #ifndef __attribute__
399 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8)
400 # define __attribute__(x) /* empty */
401 # endif
402 #endif
404 #ifndef ATTRIBUTE_NORETURN
405 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
406 #endif
408 #ifndef ATTRIBUTE_UNUSED
409 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
410 #endif
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 */
416 #else
417 # define ATTRIBUTE_WARN_UNUSED_RESULT __attribute__ ((__warn_unused_result__))
418 #endif
420 #if defined strdupa
421 # define ASSIGN_STRDUPA(DEST, S) \
422 do { DEST = strdupa (S); } while (0)
423 #else
424 # define ASSIGN_STRDUPA(DEST, S) \
425 do \
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_); \
432 while (0)
433 #endif
435 #if ! HAVE_SYNC
436 # define sync() /* empty */
437 #endif
439 /* Compute the greatest common divisor of U and V using Euclid's
440 algorithm. U and V must be nonzero. */
442 static inline size_t
443 gcd (size_t u, size_t v)
447 size_t t = u % v;
448 u = v;
449 v = t;
451 while (v);
453 return u;
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. */
460 static inline size_t
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
469 locations. */
471 static inline void *
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)) \
500 static inline void
501 emit_size_note (void)
503 fputs (_("\n\
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\
506 "), stdout);
509 static inline void
510 emit_blocksize_note (char const *program)
512 printf (_("\n\
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\
516 "), program);
519 static inline void
520 emit_ancillary_info (void)
522 printf (_("\nReport %s bugs to %s\n"), last_component (program_name),
523 PACKAGE_BUGREPORT);
524 printf (_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
525 fputs (_("General help using GNU software: <http://www.gnu.org/gethelp/>\n"),
526 stdout);
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"
546 static inline char *
547 timetostr (time_t t, char *buf)
549 return (TYPE_SIGNED (time_t)
550 ? imaxtostr (t, buf)
551 : umaxtostr (t, buf));
554 static inline char *
555 bad_cast (char const *s)
557 return (char *) s;
560 void usage (int status) ATTRIBUTE_NORETURN;
562 #define emit_cycle_warning(file_name) \
563 do \
565 error (0, 0, _("\
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)); \
572 while (0)
574 #ifndef ARRAY_CARDINALITY
575 # define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
576 #endif