* src/dd.c (flags): noatime and nofollow now depend on
[coreutils/bo.git] / src / system.h
blob29d170fd33420238fa24c1e88530840ddc048456
1 /* system-dependent definitions for coreutils
2 Copyright (C) 1989, 1991-2006 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)
7 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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 #include <alloca.h>
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
25 # endif
26 #endif
28 #include <sys/stat.h>
30 #if !defined HAVE_MKFIFO
31 # define mkfifo(name, mode) mknod (name, (mode) | S_IFIFO, 0)
32 #endif
34 #if HAVE_SYS_PARAM_H
35 # include <sys/param.h>
36 #endif
38 #include <unistd.h>
40 #ifndef STDIN_FILENO
41 # define STDIN_FILENO 0
42 #endif
44 #ifndef STDOUT_FILENO
45 # define STDOUT_FILENO 1
46 #endif
48 #ifndef STDERR_FILENO
49 # define STDERR_FILENO 2
50 #endif
53 /* limits.h must come before pathmax.h because limits.h on some systems
54 undefs PATH_MAX, whereas pathmax.h sets PATH_MAX. */
55 #include <limits.h>
57 #include "pathmax.h"
59 #include "configmake.h"
61 #if TIME_WITH_SYS_TIME
62 # include <sys/time.h>
63 # include <time.h>
64 #else
65 # if HAVE_SYS_TIME_H
66 # include <sys/time.h>
67 # else
68 # include <time.h>
69 # endif
70 #endif
72 /* Since major is a function on SVR4, we can't use `ifndef major'. */
73 #if MAJOR_IN_MKDEV
74 # include <sys/mkdev.h>
75 # define HAVE_MAJOR
76 #endif
77 #if MAJOR_IN_SYSMACROS
78 # include <sys/sysmacros.h>
79 # define HAVE_MAJOR
80 #endif
81 #ifdef major /* Might be defined in sys/types.h. */
82 # define HAVE_MAJOR
83 #endif
85 #ifndef HAVE_MAJOR
86 # define major(dev) (((dev) >> 8) & 0xff)
87 # define minor(dev) ((dev) & 0xff)
88 # define makedev(maj, min) (((maj) << 8) | (min))
89 #endif
90 #undef HAVE_MAJOR
92 #if ! defined makedev && defined mkdev
93 # define makedev(maj, min) mkdev (maj, min)
94 #endif
96 /* Don't use bcopy! Use memmove if source and destination may overlap,
97 memcpy otherwise. */
99 #include <string.h>
100 #include "mempcpy.h"
101 #include "memrchr.h"
102 #include "stpcpy.h"
103 #include "strpbrk.h"
105 #include <errno.h>
107 /* Some systems don't define the following symbols. */
108 #ifndef EDQUOT
109 # define EDQUOT (-1)
110 #endif
111 #ifndef EISDIR
112 # define EISDIR (-1)
113 #endif
114 #ifndef ENOSYS
115 # define ENOSYS (-1)
116 #endif
118 #include <stdbool.h>
119 #include <stdlib.h>
121 /* The following test is to work around the gross typo in
122 systems like Sony NEWS-OS Release 4.0C, whereby EXIT_FAILURE
123 is defined to 0, not 1. */
124 #if !EXIT_FAILURE
125 # undef EXIT_FAILURE
126 # define EXIT_FAILURE 1
127 #endif
129 #ifndef EXIT_SUCCESS
130 # define EXIT_SUCCESS 0
131 #endif
133 /* Exit statuses for programs like 'env' that exec other programs.
134 EXIT_FAILURE might not be 1, so use EXIT_FAIL in such programs. */
135 enum
137 EXIT_FAIL = 1,
138 EXIT_CANNOT_INVOKE = 126,
139 EXIT_ENOENT = 127
142 #include "exitfail.h"
144 /* Set exit_failure to STATUS if that's not the default already. */
145 static inline void
146 initialize_exit_failure (int status)
148 if (status != EXIT_FAILURE)
149 exit_failure = status;
152 #include <fcntl.h>
154 #ifndef F_OK
155 # define F_OK 0
156 # define X_OK 1
157 # define W_OK 2
158 # define R_OK 4
159 #endif
161 #include <dirent.h>
162 #ifndef _D_EXACT_NAMLEN
163 # define _D_EXACT_NAMLEN(dp) strlen ((dp)->d_name)
164 #endif
166 enum
168 NOT_AN_INODE_NUMBER = 0
171 #ifdef D_INO_IN_DIRENT
172 # define D_INO(dp) (dp)->d_ino
173 #else
174 /* Some systems don't have inodes, so fake them to avoid lots of ifdefs. */
175 # define D_INO(dp) NOT_AN_INODE_NUMBER
176 #endif
178 /* Get or fake the disk device blocksize.
179 Usually defined by sys/param.h (if at all). */
180 #if !defined DEV_BSIZE && defined BSIZE
181 # define DEV_BSIZE BSIZE
182 #endif
183 #if !defined DEV_BSIZE && defined BBSIZE /* SGI */
184 # define DEV_BSIZE BBSIZE
185 #endif
186 #ifndef DEV_BSIZE
187 # define DEV_BSIZE 4096
188 #endif
190 /* Extract or fake data from a `struct stat'.
191 ST_BLKSIZE: Preferred I/O blocksize for the file, in bytes.
192 ST_NBLOCKS: Number of blocks in the file, including indirect blocks.
193 ST_NBLOCKSIZE: Size of blocks used when calculating ST_NBLOCKS. */
194 #ifndef HAVE_STRUCT_STAT_ST_BLOCKS
195 # define ST_BLKSIZE(statbuf) DEV_BSIZE
196 # if defined _POSIX_SOURCE || !defined BSIZE /* fileblocks.c uses BSIZE. */
197 # define ST_NBLOCKS(statbuf) \
198 ((statbuf).st_size / ST_NBLOCKSIZE + ((statbuf).st_size % ST_NBLOCKSIZE != 0))
199 # else /* !_POSIX_SOURCE && BSIZE */
200 # define ST_NBLOCKS(statbuf) \
201 (S_ISREG ((statbuf).st_mode) \
202 || S_ISDIR ((statbuf).st_mode) \
203 ? st_blocks ((statbuf).st_size) : 0)
204 # endif /* !_POSIX_SOURCE && BSIZE */
205 #else /* HAVE_STRUCT_STAT_ST_BLOCKS */
206 /* Some systems, like Sequents, return st_blksize of 0 on pipes.
207 Also, when running `rsh hpux11-system cat any-file', cat would
208 determine that the output stream had an st_blksize of 2147421096.
209 Conversely st_blksize can be 2 GiB (or maybe even larger) with XFS
210 on 64-bit hosts. Somewhat arbitrarily, limit the `optimal' block
211 size to SIZE_MAX / 8 + 1. (Dividing SIZE_MAX by only 4 wouldn't
212 suffice, since "cat" sometimes multiplies the result by 4.) If
213 anyone knows of a system for which this limit is too small, please
214 report it as a bug in this code. */
215 # define ST_BLKSIZE(statbuf) ((0 < (statbuf).st_blksize \
216 && (statbuf).st_blksize <= SIZE_MAX / 8 + 1) \
217 ? (statbuf).st_blksize : DEV_BSIZE)
218 # if defined hpux || defined __hpux__ || defined __hpux
219 /* HP-UX counts st_blocks in 1024-byte units.
220 This loses when mixing HP-UX and BSD file systems with NFS. */
221 # define ST_NBLOCKSIZE 1024
222 # else /* !hpux */
223 # if defined _AIX && defined _I386
224 /* AIX PS/2 counts st_blocks in 4K units. */
225 # define ST_NBLOCKSIZE (4 * 1024)
226 # else /* not AIX PS/2 */
227 # if defined _CRAY
228 # define ST_NBLOCKS(statbuf) \
229 (S_ISREG ((statbuf).st_mode) \
230 || S_ISDIR ((statbuf).st_mode) \
231 ? (statbuf).st_blocks * ST_BLKSIZE(statbuf)/ST_NBLOCKSIZE : 0)
232 # endif /* _CRAY */
233 # endif /* not AIX PS/2 */
234 # endif /* !hpux */
235 #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
237 #ifndef ST_NBLOCKS
238 # define ST_NBLOCKS(statbuf) ((statbuf).st_blocks)
239 #endif
241 #ifndef ST_NBLOCKSIZE
242 # ifdef S_BLKSIZE
243 # define ST_NBLOCKSIZE S_BLKSIZE
244 # else
245 # define ST_NBLOCKSIZE 512
246 # endif
247 #endif
249 /* Redirection and wildcarding when done by the utility itself.
250 Generally a noop, but used in particular for native VMS. */
251 #ifndef initialize_main
252 # define initialize_main(ac, av)
253 #endif
255 #include "stat-macros.h"
257 #include "timespec.h"
259 #include <inttypes.h>
261 #include <ctype.h>
263 #if ! (defined isblank || HAVE_DECL_ISBLANK)
264 # define isblank(c) ((c) == ' ' || (c) == '\t')
265 #endif
267 /* ISDIGIT differs from isdigit, as follows:
268 - Its arg may be any int or unsigned int; it need not be an unsigned char
269 or EOF.
270 - It's typically faster.
271 POSIX says that only '0' through '9' are digits. Prefer ISDIGIT to
272 isdigit unless it's important to use the locale's definition
273 of `digit' even when the host does not conform to POSIX. */
274 #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
276 /* Convert a possibly-signed character to an unsigned character. This is
277 a bit safer than casting to unsigned char, since it catches some type
278 errors that the cast doesn't. */
279 static inline unsigned char to_uchar (char ch) { return ch; }
281 #include <locale.h>
283 /* Take care of NLS matters. */
285 #include "gettext.h"
286 #if ! ENABLE_NLS
287 # undef textdomain
288 # define textdomain(Domainname) /* empty */
289 # undef bindtextdomain
290 # define bindtextdomain(Domainname, Dirname) /* empty */
291 #endif
293 #define _(msgid) gettext (msgid)
294 #define N_(msgid) msgid
296 /* Return a value that pluralizes the same way that N does, in all
297 languages we know of. */
298 static inline unsigned long int
299 select_plural (uintmax_t n)
301 /* Reduce by a power of ten, but keep it away from zero. The
302 gettext manual says 1000000 should be safe. */
303 enum { PLURAL_REDUCER = 1000000 };
304 return (n <= ULONG_MAX ? n : n % PLURAL_REDUCER + PLURAL_REDUCER);
307 #define STREQ(a, b) (strcmp ((a), (b)) == 0)
309 #if !HAVE_DECL_FREE
310 void free ();
311 #endif
313 #if !HAVE_DECL_MALLOC
314 char *malloc ();
315 #endif
317 #if !HAVE_DECL_MEMCHR
318 char *memchr ();
319 #endif
321 #if !HAVE_DECL_REALLOC
322 char *realloc ();
323 #endif
325 #if !HAVE_DECL_GETENV
326 char *getenv ();
327 #endif
329 #if !HAVE_DECL_LSEEK
330 off_t lseek ();
331 #endif
333 #if !HAVE_DECL_GETLOGIN
334 char *getlogin ();
335 #endif
337 #if !HAVE_DECL_TTYNAME
338 char *ttyname ();
339 #endif
341 #if !HAVE_DECL_GETEUID
342 uid_t geteuid ();
343 #endif
345 #if !HAVE_DECL_GETPWUID
346 struct passwd *getpwuid ();
347 #endif
349 #if !HAVE_DECL_GETGRGID
350 struct group *getgrgid ();
351 #endif
353 #if !HAVE_DECL_GETUID
354 uid_t getuid ();
355 #endif
357 #include "xalloc.h"
358 #include "verify.h"
360 /* This is simply a shorthand for the common case in which
361 the third argument to x2nrealloc would be `sizeof *(P)'.
362 Ensure that sizeof *(P) is *not* 1. In that case, it'd be
363 better to use X2REALLOC, although not strictly necessary. */
364 #define X2NREALLOC(P, PN) ((void) verify_true (sizeof *(P) != 1), \
365 x2nrealloc (P, PN, sizeof *(P)))
367 /* Using x2realloc (when appropriate) usually makes your code more
368 readable than using x2nrealloc, but it also makes it so your
369 code will malfunction if sizeof *(P) ever becomes 2 or greater.
370 So use this macro instead of using x2realloc directly. */
371 #define X2REALLOC(P, PN) ((void) verify_true (sizeof *(P) == 1), \
372 x2realloc (P, PN))
374 #include "unlocked-io.h"
375 #include "same-inode.h"
377 #include "dirname.h"
379 static inline bool
380 dot_or_dotdot (char const *file_name)
382 if (file_name[0] == '.')
384 char sep = file_name[(file_name[1] == '.') + 1];
385 return (! sep || ISSLASH (sep));
387 else
388 return false;
391 /* A wrapper for readdir so that callers don't see entries for `.' or `..'. */
392 static inline struct dirent const *
393 readdir_ignoring_dot_and_dotdot (DIR *dirp)
395 while (1)
397 struct dirent const *dp = readdir (dirp);
398 if (dp == NULL || ! dot_or_dotdot (dp->d_name))
399 return dp;
403 #if SETVBUF_REVERSED
404 # define SETVBUF(Stream, Buffer, Type, Size) \
405 setvbuf (Stream, Type, Buffer, Size)
406 #else
407 # define SETVBUF(Stream, Buffer, Type, Size) \
408 setvbuf (Stream, Buffer, Type, Size)
409 #endif
411 /* Factor out some of the common --help and --version processing code. */
413 /* These enum values cannot possibly conflict with the option values
414 ordinarily used by commands, including CHAR_MAX + 1, etc. Avoid
415 CHAR_MIN - 1, as it may equal -1, the getopt end-of-options value. */
416 enum
418 GETOPT_HELP_CHAR = (CHAR_MIN - 2),
419 GETOPT_VERSION_CHAR = (CHAR_MIN - 3)
422 #define GETOPT_HELP_OPTION_DECL \
423 "help", no_argument, NULL, GETOPT_HELP_CHAR
424 #define GETOPT_VERSION_OPTION_DECL \
425 "version", no_argument, NULL, GETOPT_VERSION_CHAR
427 #define case_GETOPT_HELP_CHAR \
428 case GETOPT_HELP_CHAR: \
429 usage (EXIT_SUCCESS); \
430 break;
432 /* Program_name must be a literal string.
433 Usually it is just PROGRAM_NAME. */
434 #define USAGE_BUILTIN_WARNING \
435 _("\n" \
436 "NOTE: your shell may have its own version of %s, which usually supersedes\n" \
437 "the version described here. Please refer to your shell's documentation\n" \
438 "for details about the options it supports.\n")
440 #define HELP_OPTION_DESCRIPTION \
441 _(" --help display this help and exit\n")
442 #define VERSION_OPTION_DESCRIPTION \
443 _(" --version output version information and exit\n")
445 #include "closeout.h"
446 #include "version-etc.h"
448 #define case_GETOPT_VERSION_CHAR(Program_name, Authors) \
449 case GETOPT_VERSION_CHAR: \
450 version_etc (stdout, Program_name, GNU_PACKAGE, VERSION, Authors, \
451 (char *) NULL); \
452 exit (EXIT_SUCCESS); \
453 break;
455 #ifndef MAX
456 # define MAX(a, b) ((a) > (b) ? (a) : (b))
457 #endif
459 #ifndef MIN
460 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
461 #endif
463 #include "intprops.h"
465 #ifndef SSIZE_MAX
466 # define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
467 #endif
469 #ifndef OFF_T_MIN
470 # define OFF_T_MIN TYPE_MINIMUM (off_t)
471 #endif
473 #ifndef OFF_T_MAX
474 # define OFF_T_MAX TYPE_MAXIMUM (off_t)
475 #endif
477 #ifndef UID_T_MAX
478 # define UID_T_MAX TYPE_MAXIMUM (uid_t)
479 #endif
481 #ifndef GID_T_MAX
482 # define GID_T_MAX TYPE_MAXIMUM (gid_t)
483 #endif
485 #ifndef PID_T_MAX
486 # define PID_T_MAX TYPE_MAXIMUM (pid_t)
487 #endif
489 /* Use this to suppress gcc's `...may be used before initialized' warnings. */
490 #ifdef lint
491 # define IF_LINT(Code) Code
492 #else
493 # define IF_LINT(Code) /* empty */
494 #endif
496 #ifndef __attribute__
497 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
498 # define __attribute__(x) /* empty */
499 # endif
500 #endif
502 #ifndef ATTRIBUTE_NORETURN
503 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
504 #endif
506 #ifndef ATTRIBUTE_UNUSED
507 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
508 #endif
510 #if defined strdupa
511 # define ASSIGN_STRDUPA(DEST, S) \
512 do { DEST = strdupa (S); } while (0)
513 #else
514 # define ASSIGN_STRDUPA(DEST, S) \
515 do \
517 const char *s_ = (S); \
518 size_t len_ = strlen (s_) + 1; \
519 char *tmp_dest_ = alloca (len_); \
520 DEST = memcpy (tmp_dest_, (s_), len_); \
522 while (0)
523 #endif
525 #ifndef EOVERFLOW
526 # define EOVERFLOW EINVAL
527 #endif
529 #if ! HAVE_FSEEKO && ! defined fseeko
530 # define fseeko(s, o, w) ((o) == (long int) (o) \
531 ? fseek (s, o, w) \
532 : (errno = EOVERFLOW, -1))
533 #endif
535 #if ! HAVE_SYNC
536 # define sync() /* empty */
537 #endif
539 /* Compute the greatest common divisor of U and V using Euclid's
540 algorithm. U and V must be nonzero. */
542 static inline size_t
543 gcd (size_t u, size_t v)
547 size_t t = u % v;
548 u = v;
549 v = t;
551 while (v);
553 return u;
556 /* Compute the least common multiple of U and V. U and V must be
557 nonzero. There is no overflow checking, so callers should not
558 specify outlandish sizes. */
560 static inline size_t
561 lcm (size_t u, size_t v)
563 return u * (v / gcd (u, v));
566 /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
567 ALIGNMENT must be nonzero. The caller must arrange for ((char *)
568 PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
569 locations. */
571 static inline void *
572 ptr_align (void const *ptr, size_t alignment)
574 char const *p0 = ptr;
575 char const *p1 = p0 + alignment - 1;
576 return (void *) (p1 - (size_t) p1 % alignment);
579 /* If 10*Accum + Digit_val is larger than the maximum value for Type,
580 then don't update Accum and return false to indicate it would
581 overflow. Otherwise, set Accum to that new value and return true.
582 Verify at compile-time that Type is Accum's type, and that Type is
583 unsigned. Accum must be an object, so that we can take its
584 address. Accum and Digit_val may be evaluated multiple times.
586 The "Added check" below is not strictly required, but it causes GCC
587 to return a nonzero exit status instead of merely a warning
588 diagnostic, and that is more useful. */
590 #define DECIMAL_DIGIT_ACCUMULATE(Accum, Digit_val, Type) \
592 (void) (&(Accum) == (Type *) NULL), /* The type matches. */ \
593 (void) verify_true (! TYPE_SIGNED (Type)), /* The type is unsigned. */ \
594 (void) verify_true (sizeof (Accum) == sizeof (Type)), /* Added check. */ \
595 (((Type) -1 / 10 < (Accum) \
596 || (Type) ((Accum) * 10 + (Digit_val)) < (Accum)) \
597 ? false : (((Accum) = (Accum) * 10 + (Digit_val)), true)) \