* src/system.h (DOT_OR_DOTDOT): Remove macro. Rewrite as a...
[platform/upstream/coreutils.git] / src / system.h
1 /* system-dependent definitions for coreutils
2    Copyright (C) 1989, 1991-2006 Free Software Foundation, Inc.
3
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.
8
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.
13
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.  */
17
18 #include <alloca.h>
19
20 /* Include sys/types.h before this file.  */
21
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
27
28 #include <sys/stat.h>
29
30 #if !defined HAVE_MKFIFO
31 # define mkfifo(name, mode) mknod (name, (mode) | S_IFIFO, 0)
32 #endif
33
34 #if HAVE_SYS_PARAM_H
35 # include <sys/param.h>
36 #endif
37
38 #include <unistd.h>
39
40 #ifndef STDIN_FILENO
41 # define STDIN_FILENO 0
42 #endif
43
44 #ifndef STDOUT_FILENO
45 # define STDOUT_FILENO 1
46 #endif
47
48 #ifndef STDERR_FILENO
49 # define STDERR_FILENO 2
50 #endif
51
52
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>
56
57 #include "pathmax.h"
58
59 #include "configmake.h"
60 #define LOCALEDIR CONFIGMAKE_LOCALEDIR
61
62 #if TIME_WITH_SYS_TIME
63 # include <sys/time.h>
64 # include <time.h>
65 #else
66 # if HAVE_SYS_TIME_H
67 #  include <sys/time.h>
68 # else
69 #  include <time.h>
70 # endif
71 #endif
72
73 /* Since major is a function on SVR4, we can't use `ifndef major'.  */
74 #if MAJOR_IN_MKDEV
75 # include <sys/mkdev.h>
76 # define HAVE_MAJOR
77 #endif
78 #if MAJOR_IN_SYSMACROS
79 # include <sys/sysmacros.h>
80 # define HAVE_MAJOR
81 #endif
82 #ifdef major                    /* Might be defined in sys/types.h.  */
83 # define HAVE_MAJOR
84 #endif
85
86 #ifndef HAVE_MAJOR
87 # define major(dev)  (((dev) >> 8) & 0xff)
88 # define minor(dev)  ((dev) & 0xff)
89 # define makedev(maj, min)  (((maj) << 8) | (min))
90 #endif
91 #undef HAVE_MAJOR
92
93 #if ! defined makedev && defined mkdev
94 # define makedev(maj, min)  mkdev (maj, min)
95 #endif
96
97 /* Don't use bcopy!  Use memmove if source and destination may overlap,
98    memcpy otherwise.  */
99
100 #include <string.h>
101 #include "mempcpy.h"
102 #include "memrchr.h"
103 #include "stpcpy.h"
104 #include "strpbrk.h"
105
106 #include <errno.h>
107
108 /* Some systems don't define the following symbols.  */
109 #ifndef EDQUOT
110 # define EDQUOT (-1)
111 #endif
112 #ifndef EISDIR
113 # define EISDIR (-1)
114 #endif
115 #ifndef ENOSYS
116 # define ENOSYS (-1)
117 #endif
118
119 #include <stdbool.h>
120 #include <stdlib.h>
121
122 /* The following test is to work around the gross typo in
123    systems like Sony NEWS-OS Release 4.0C, whereby EXIT_FAILURE
124    is defined to 0, not 1.  */
125 #if !EXIT_FAILURE
126 # undef EXIT_FAILURE
127 # define EXIT_FAILURE 1
128 #endif
129
130 #ifndef EXIT_SUCCESS
131 # define EXIT_SUCCESS 0
132 #endif
133
134 /* Exit statuses for programs like 'env' that exec other programs.
135    EXIT_FAILURE might not be 1, so use EXIT_FAIL in such programs.  */
136 enum
137 {
138   EXIT_FAIL = 1,
139   EXIT_CANNOT_INVOKE = 126,
140   EXIT_ENOENT = 127
141 };
142
143 #include "exitfail.h"
144
145 /* Set exit_failure to STATUS if that's not the default already.  */
146 static inline void
147 initialize_exit_failure (int status)
148 {
149   if (status != EXIT_FAILURE)
150     exit_failure = status;
151 }
152
153 #include <fcntl.h>
154
155 #if !defined SEEK_SET
156 # define SEEK_SET 0
157 # define SEEK_CUR 1
158 # define SEEK_END 2
159 #endif
160 #ifndef F_OK
161 # define F_OK 0
162 # define X_OK 1
163 # define W_OK 2
164 # define R_OK 4
165 #endif
166
167 #if !defined O_DIRECT && defined O_DIRECTIO
168 /* Tru64 spells it `O_DIRECTIO'.  */
169 # define O_DIRECT O_DIRECTIO
170 #endif
171
172 #if !defined O_DIRECT
173 # define O_DIRECT 0
174 #endif
175
176 #if !defined O_DIRECTORY
177 # define O_DIRECTORY 0
178 #endif
179
180 #if !defined O_DSYNC
181 # define O_DSYNC 0
182 #endif
183
184 #if !defined O_NDELAY
185 # define O_NDELAY 0
186 #endif
187
188 #if !defined O_NOATIME
189 # define O_NOATIME 0
190 #endif
191
192 #if !defined O_NONBLOCK
193 # define O_NONBLOCK O_NDELAY
194 #endif
195
196 #if !defined O_NOCTTY
197 # define O_NOCTTY 0
198 #endif
199
200 #if !defined O_NOFOLLOW
201 # define O_NOFOLLOW 0
202 #endif
203
204 #if !defined O_NOLINKS
205 # define O_NOLINKS 0
206 #endif
207
208 #if !defined O_RSYNC
209 # define O_RSYNC 0
210 #endif
211
212 #if !defined O_SYNC
213 # define O_SYNC 0
214 #endif
215
216 /* For systems that distinguish between text and binary I/O.
217    O_BINARY is usually declared in fcntl.h  */
218 #if !defined O_BINARY && defined _O_BINARY
219   /* For MSC-compatible compilers.  */
220 # define O_BINARY _O_BINARY
221 # define O_TEXT _O_TEXT
222 #endif
223
224 #ifdef __BEOS__
225   /* BeOS 5 has O_BINARY and O_TEXT, but they have no effect.  */
226 # undef O_BINARY
227 # undef O_TEXT
228 #endif
229
230 #ifndef O_BINARY
231 # define O_BINARY 0
232 # define O_TEXT 0
233 #endif
234
235 #include <dirent.h>
236 #ifndef _D_EXACT_NAMLEN
237 # define _D_EXACT_NAMLEN(dp) strlen ((dp)->d_name)
238 #endif
239
240 enum
241 {
242   NOT_AN_INODE_NUMBER = 0
243 };
244
245 #ifdef D_INO_IN_DIRENT
246 # define D_INO(dp) (dp)->d_ino
247 #else
248 /* Some systems don't have inodes, so fake them to avoid lots of ifdefs.  */
249 # define D_INO(dp) NOT_AN_INODE_NUMBER
250 #endif
251
252 /* Get or fake the disk device blocksize.
253    Usually defined by sys/param.h (if at all).  */
254 #if !defined DEV_BSIZE && defined BSIZE
255 # define DEV_BSIZE BSIZE
256 #endif
257 #if !defined DEV_BSIZE && defined BBSIZE /* SGI */
258 # define DEV_BSIZE BBSIZE
259 #endif
260 #ifndef DEV_BSIZE
261 # define DEV_BSIZE 4096
262 #endif
263
264 /* Extract or fake data from a `struct stat'.
265    ST_BLKSIZE: Preferred I/O blocksize for the file, in bytes.
266    ST_NBLOCKS: Number of blocks in the file, including indirect blocks.
267    ST_NBLOCKSIZE: Size of blocks used when calculating ST_NBLOCKS.  */
268 #ifndef HAVE_STRUCT_STAT_ST_BLOCKS
269 # define ST_BLKSIZE(statbuf) DEV_BSIZE
270 # if defined _POSIX_SOURCE || !defined BSIZE /* fileblocks.c uses BSIZE.  */
271 #  define ST_NBLOCKS(statbuf) \
272   ((statbuf).st_size / ST_NBLOCKSIZE + ((statbuf).st_size % ST_NBLOCKSIZE != 0))
273 # else /* !_POSIX_SOURCE && BSIZE */
274 #  define ST_NBLOCKS(statbuf) \
275   (S_ISREG ((statbuf).st_mode) \
276    || S_ISDIR ((statbuf).st_mode) \
277    ? st_blocks ((statbuf).st_size) : 0)
278 # endif /* !_POSIX_SOURCE && BSIZE */
279 #else /* HAVE_STRUCT_STAT_ST_BLOCKS */
280 /* Some systems, like Sequents, return st_blksize of 0 on pipes.
281    Also, when running `rsh hpux11-system cat any-file', cat would
282    determine that the output stream had an st_blksize of 2147421096.
283    So here we arbitrarily limit the `optimal' block size to 4MB.
284    If anyone knows of a system for which the legitimate value for
285    st_blksize can exceed 4MB, please report it as a bug in this code.  */
286 # define ST_BLKSIZE(statbuf) ((0 < (statbuf).st_blksize \
287                                && (statbuf).st_blksize <= (1 << 22)) /* 4MB */ \
288                               ? (statbuf).st_blksize : DEV_BSIZE)
289 # if defined hpux || defined __hpux__ || defined __hpux
290 /* HP-UX counts st_blocks in 1024-byte units.
291    This loses when mixing HP-UX and BSD file systems with NFS.  */
292 #  define ST_NBLOCKSIZE 1024
293 # else /* !hpux */
294 #  if defined _AIX && defined _I386
295 /* AIX PS/2 counts st_blocks in 4K units.  */
296 #   define ST_NBLOCKSIZE (4 * 1024)
297 #  else /* not AIX PS/2 */
298 #   if defined _CRAY
299 #    define ST_NBLOCKS(statbuf) \
300   (S_ISREG ((statbuf).st_mode) \
301    || S_ISDIR ((statbuf).st_mode) \
302    ? (statbuf).st_blocks * ST_BLKSIZE(statbuf)/ST_NBLOCKSIZE : 0)
303 #   endif /* _CRAY */
304 #  endif /* not AIX PS/2 */
305 # endif /* !hpux */
306 #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
307
308 #ifndef ST_NBLOCKS
309 # define ST_NBLOCKS(statbuf) ((statbuf).st_blocks)
310 #endif
311
312 #ifndef ST_NBLOCKSIZE
313 # ifdef S_BLKSIZE
314 #  define ST_NBLOCKSIZE S_BLKSIZE
315 # else
316 #  define ST_NBLOCKSIZE 512
317 # endif
318 #endif
319
320 /* Redirection and wildcarding when done by the utility itself.
321    Generally a noop, but used in particular for native VMS. */
322 #ifndef initialize_main
323 # define initialize_main(ac, av)
324 #endif
325
326 #include "stat-macros.h"
327
328 #include "timespec.h"
329
330 #include <inttypes.h>
331 #include <stdint.h>
332
333 #include <ctype.h>
334
335 #if ! (defined isblank || HAVE_DECL_ISBLANK)
336 # define isblank(c) ((c) == ' ' || (c) == '\t')
337 #endif
338
339 /* ISDIGIT differs from isdigit, as follows:
340    - Its arg may be any int or unsigned int; it need not be an unsigned char
341      or EOF.
342    - It's typically faster.
343    POSIX says that only '0' through '9' are digits.  Prefer ISDIGIT to
344    isdigit unless it's important to use the locale's definition
345    of `digit' even when the host does not conform to POSIX.  */
346 #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
347
348 /* Convert a possibly-signed character to an unsigned character.  This is
349    a bit safer than casting to unsigned char, since it catches some type
350    errors that the cast doesn't.  */
351 static inline unsigned char to_uchar (char ch) { return ch; }
352
353 #include <locale.h>
354
355 /* Take care of NLS matters.  */
356
357 #include "gettext.h"
358 #if ! ENABLE_NLS
359 # undef textdomain
360 # define textdomain(Domainname) /* empty */
361 # undef bindtextdomain
362 # define bindtextdomain(Domainname, Dirname) /* empty */
363 #endif
364
365 #define _(msgid) gettext (msgid)
366 #define N_(msgid) msgid
367
368 /* Return a value that pluralizes the same way that N does, in all
369    languages we know of.  */
370 static inline unsigned long int
371 select_plural (uintmax_t n)
372 {
373   /* Reduce by a power of ten, but keep it away from zero.  The
374      gettext manual says 1000000 should be safe.  */
375   enum { PLURAL_REDUCER = 1000000 };
376   return (n <= ULONG_MAX ? n : n % PLURAL_REDUCER + PLURAL_REDUCER);
377 }
378
379 #define STREQ(a, b) (strcmp ((a), (b)) == 0)
380
381 #if !HAVE_DECL_FREE
382 void free ();
383 #endif
384
385 #if !HAVE_DECL_MALLOC
386 char *malloc ();
387 #endif
388
389 #if !HAVE_DECL_MEMCHR
390 char *memchr ();
391 #endif
392
393 #if !HAVE_DECL_REALLOC
394 char *realloc ();
395 #endif
396
397 #if !HAVE_DECL_GETENV
398 char *getenv ();
399 #endif
400
401 #if !HAVE_DECL_LSEEK
402 off_t lseek ();
403 #endif
404
405 #if !HAVE_DECL_GETLOGIN
406 char *getlogin ();
407 #endif
408
409 #if !HAVE_DECL_TTYNAME
410 char *ttyname ();
411 #endif
412
413 #if !HAVE_DECL_GETEUID
414 uid_t geteuid ();
415 #endif
416
417 #if !HAVE_DECL_GETPWUID
418 struct passwd *getpwuid ();
419 #endif
420
421 #if !HAVE_DECL_GETGRGID
422 struct group *getgrgid ();
423 #endif
424
425 #if !HAVE_DECL_GETUID
426 uid_t getuid ();
427 #endif
428
429 #include "xalloc.h"
430 #include "verify.h"
431
432 /* This is simply a shorthand for the common case in which
433    the third argument to x2nrealloc would be `sizeof *(P)'.
434    Ensure that sizeof *(P) is *not* 1.  In that case, it'd be
435    better to use X2REALLOC, although not strictly necessary.  */
436 #define X2NREALLOC(P, PN) ((void) verify_true (sizeof *(P) != 1), \
437                            x2nrealloc (P, PN, sizeof *(P)))
438
439 /* Using x2realloc (when appropriate) usually makes your code more
440    readable than using x2nrealloc, but it also makes it so your
441    code will malfunction if sizeof *(P) ever becomes 2 or greater.
442    So use this macro instead of using x2realloc directly.  */
443 #define X2REALLOC(P, PN) ((void) verify_true (sizeof *(P) == 1), \
444                           x2realloc (P, PN))
445
446 /* Include automatically-generated macros for unlocked I/O.  */
447 #include "unlocked-io.h"
448 #include "same-inode.h"
449
450 static inline bool
451 dot_or_dotdot (char const *file_name)
452 {
453   return (file_name[0] == '.'
454           && (file_name[1] == '\0'
455               || (file_name[1] == '.'
456                   && file_name[2] == '\0')));
457 }
458
459 /* A wrapper for readdir so that callers don't see entries for `.' or `..'.  */
460 static inline struct dirent const *
461 readdir_ignoring_dot_and_dotdot (DIR *dirp)
462 {
463   while (1)
464     {
465       struct dirent const *dp = readdir (dirp);
466       if (dp == NULL || ! dot_or_dotdot (dp->d_name))
467         return dp;
468     }
469 }
470
471 #if SETVBUF_REVERSED
472 # define SETVBUF(Stream, Buffer, Type, Size) \
473     setvbuf (Stream, Type, Buffer, Size)
474 #else
475 # define SETVBUF(Stream, Buffer, Type, Size) \
476     setvbuf (Stream, Buffer, Type, Size)
477 #endif
478
479 /* Factor out some of the common --help and --version processing code.  */
480
481 /* These enum values cannot possibly conflict with the option values
482    ordinarily used by commands, including CHAR_MAX + 1, etc.  Avoid
483    CHAR_MIN - 1, as it may equal -1, the getopt end-of-options value.  */
484 enum
485 {
486   GETOPT_HELP_CHAR = (CHAR_MIN - 2),
487   GETOPT_VERSION_CHAR = (CHAR_MIN - 3)
488 };
489
490 #define GETOPT_HELP_OPTION_DECL \
491   "help", no_argument, NULL, GETOPT_HELP_CHAR
492 #define GETOPT_VERSION_OPTION_DECL \
493   "version", no_argument, NULL, GETOPT_VERSION_CHAR
494
495 #define case_GETOPT_HELP_CHAR                   \
496   case GETOPT_HELP_CHAR:                        \
497     usage (EXIT_SUCCESS);                       \
498     break;
499
500 /* Program_name must be a literal string.
501    Usually it is just PROGRAM_NAME.  */
502 #define USAGE_BUILTIN_WARNING \
503   _("\n" \
504 "NOTE: your shell may have its own version of %s, which usually supersedes\n" \
505 "the version described here.  Please refer to your shell's documentation\n" \
506 "for details about the options it supports.\n")
507
508 #define HELP_OPTION_DESCRIPTION \
509   _("      --help     display this help and exit\n")
510 #define VERSION_OPTION_DESCRIPTION \
511   _("      --version  output version information and exit\n")
512
513 #include "closeout.h"
514 #include "version-etc.h"
515
516 #define case_GETOPT_VERSION_CHAR(Program_name, Authors)                 \
517   case GETOPT_VERSION_CHAR:                                             \
518     version_etc (stdout, Program_name, GNU_PACKAGE, VERSION, Authors,   \
519                  (char *) NULL);                                        \
520     exit (EXIT_SUCCESS);                                                \
521     break;
522
523 #ifndef MAX
524 # define MAX(a, b) ((a) > (b) ? (a) : (b))
525 #endif
526
527 #ifndef MIN
528 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
529 #endif
530
531 #include "intprops.h"
532
533 #ifndef SSIZE_MAX
534 # define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
535 #endif
536
537 #ifndef OFF_T_MIN
538 # define OFF_T_MIN TYPE_MINIMUM (off_t)
539 #endif
540
541 #ifndef OFF_T_MAX
542 # define OFF_T_MAX TYPE_MAXIMUM (off_t)
543 #endif
544
545 #ifndef UID_T_MAX
546 # define UID_T_MAX TYPE_MAXIMUM (uid_t)
547 #endif
548
549 #ifndef GID_T_MAX
550 # define GID_T_MAX TYPE_MAXIMUM (gid_t)
551 #endif
552
553 #ifndef PID_T_MAX
554 # define PID_T_MAX TYPE_MAXIMUM (pid_t)
555 #endif
556
557 /* Use this to suppress gcc's `...may be used before initialized' warnings. */
558 #ifdef lint
559 # define IF_LINT(Code) Code
560 #else
561 # define IF_LINT(Code) /* empty */
562 #endif
563
564 #ifndef __attribute__
565 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
566 #  define __attribute__(x) /* empty */
567 # endif
568 #endif
569
570 #ifndef ATTRIBUTE_NORETURN
571 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
572 #endif
573
574 #ifndef ATTRIBUTE_UNUSED
575 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
576 #endif
577
578 #if defined strdupa
579 # define ASSIGN_STRDUPA(DEST, S)                \
580   do { DEST = strdupa (S); } while (0)
581 #else
582 # define ASSIGN_STRDUPA(DEST, S)                \
583   do                                            \
584     {                                           \
585       const char *s_ = (S);                     \
586       size_t len_ = strlen (s_) + 1;            \
587       char *tmp_dest_ = alloca (len_);          \
588       DEST = memcpy (tmp_dest_, (s_), len_);    \
589     }                                           \
590   while (0)
591 #endif
592
593 #ifndef EOVERFLOW
594 # define EOVERFLOW EINVAL
595 #endif
596
597 #if ! HAVE_FSEEKO && ! defined fseeko
598 # define fseeko(s, o, w) ((o) == (long int) (o)         \
599                           ? fseek (s, o, w)             \
600                           : (errno = EOVERFLOW, -1))
601 #endif
602
603 #if ! HAVE_SYNC
604 # define sync() /* empty */
605 #endif
606
607 /* Compute the greatest common divisor of U and V using Euclid's
608    algorithm.  U and V must be nonzero.  */
609
610 static inline size_t
611 gcd (size_t u, size_t v)
612 {
613   do
614     {
615       size_t t = u % v;
616       u = v;
617       v = t;
618     }
619   while (v);
620
621   return u;
622 }
623
624 /* Compute the least common multiple of U and V.  U and V must be
625    nonzero.  There is no overflow checking, so callers should not
626    specify outlandish sizes.  */
627
628 static inline size_t
629 lcm (size_t u, size_t v)
630 {
631   return u * (v / gcd (u, v));
632 }
633
634 /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
635    ALIGNMENT must be nonzero.  The caller must arrange for ((char *)
636    PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
637    locations.  */
638
639 static inline void *
640 ptr_align (void const *ptr, size_t alignment)
641 {
642   char const *p0 = ptr;
643   char const *p1 = p0 + alignment - 1;
644   return (void *) (p1 - (size_t) p1 % alignment);
645 }
646
647 /* If 10*Accum + Digit_val is larger than the maximum value for Type,
648    then don't update Accum and return false to indicate it would
649    overflow.  Otherwise, set Accum to that new value and return true.
650    Verify at compile-time that Type is Accum's type, and that Type is
651    unsigned.  Accum must be an object, so that we can take its
652    address.  Accum and Digit_val may be evaluated multiple times.
653
654    The "Added check" below is not strictly required, but it causes GCC
655    to return a nonzero exit status instead of merely a warning
656    diagnostic, and that is more useful.  */
657
658 #define DECIMAL_DIGIT_ACCUMULATE(Accum, Digit_val, Type)                \
659   (                                                                     \
660    (void) (&(Accum) == (Type *) NULL),  /* The type matches.  */        \
661    (void) verify_true (! TYPE_SIGNED (Type)), /* The type is unsigned.  */ \
662    (void) verify_true (sizeof (Accum) == sizeof (Type)), /* Added check.  */ \
663    (((Type) -1 / 10 < (Accum)                                           \
664      || (Type) ((Accum) * 10 + (Digit_val)) < (Accum))                  \
665     ? false : (((Accum) = (Accum) * 10 + (Digit_val)), true))           \
666   )