Most .c files (AUTHORS): Revert the WRITTEN_BY/AUTHORS change
[platform/upstream/coreutils.git] / src / sys2.h
1 /* WARNING -- this file is temporary.  It is shared between the
2    sh-utils, fileutils, and textutils packages.  Once I find a little
3    more time, I'll merge the remaining things in system.h and everything
4    in this file will go back there. */
5
6 #ifndef S_IFMT
7 # define S_IFMT 0170000
8 #endif
9
10 #if STAT_MACROS_BROKEN
11 # undef S_ISBLK
12 # undef S_ISCHR
13 # undef S_ISDIR
14 # undef S_ISDOOR
15 # undef S_ISFIFO
16 # undef S_ISLNK
17 # undef S_ISNAM
18 # undef S_ISMPB
19 # undef S_ISMPC
20 # undef S_ISNWK
21 # undef S_ISREG
22 # undef S_ISSOCK
23 #endif
24
25
26 #ifndef S_ISBLK
27 # ifdef S_IFBLK
28 #  define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
29 # else
30 #  define S_ISBLK(m) 0
31 # endif
32 #endif
33
34 #ifndef S_ISCHR
35 # ifdef S_IFCHR
36 #  define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
37 # else
38 #  define S_ISCHR(m) 0
39 # endif
40 #endif
41
42 #ifndef S_ISDIR
43 # ifdef S_IFDIR
44 #  define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
45 # else
46 #  define S_ISDIR(m) 0
47 # endif
48 #endif
49
50 #ifndef S_ISDOOR /* Solaris 2.5 and up */
51 # ifdef S_IFDOOR
52 #  define S_ISDOOR(m) (((m) & S_IFMT) == S_IFDOOR)
53 # else
54 #  define S_ISDOOR(m) 0
55 # endif
56 #endif
57
58 #ifndef S_ISFIFO
59 # ifdef S_IFIFO
60 #  define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
61 # else
62 #  define S_ISFIFO(m) 0
63 # endif
64 #endif
65
66 #ifndef S_ISLNK
67 # ifdef S_IFLNK
68 #  define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
69 # else
70 #  define S_ISLNK(m) 0
71 # endif
72 #endif
73
74 #ifndef S_ISMPB /* V7 */
75 # ifdef S_IFMPB
76 #  define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
77 #  define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
78 # else
79 #  define S_ISMPB(m) 0
80 #  define S_ISMPC(m) 0
81 # endif
82 #endif
83
84 #ifndef S_ISNAM /* Xenix */
85 # ifdef S_IFNAM
86 #  define S_ISNAM(m) (((m) & S_IFMT) == S_IFNAM)
87 # else
88 #  define S_ISNAM(m) 0
89 # endif
90 #endif
91
92 #ifndef S_ISNWK /* HP/UX */
93 # ifdef S_IFNWK
94 #  define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
95 # else
96 #  define S_ISNWK(m) 0
97 # endif
98 #endif
99
100 #ifndef S_ISREG
101 # ifdef S_IFREG
102 #  define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
103 # else
104 #  define S_ISREG(m) 0
105 # endif
106 #endif
107
108 #ifndef S_ISSOCK
109 # ifdef S_IFSOCK
110 #  define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
111 # else
112 #  define S_ISSOCK(m) 0
113 # endif
114 #endif
115
116
117 #ifndef S_TYPEISSEM
118 # ifdef S_INSEM
119 #  define S_TYPEISSEM(p) (S_ISNAM ((p)->st_mode) && (p)->st_rdev == S_INSEM)
120 # else
121 #  define S_TYPEISSEM(p) 0
122 # endif
123 #endif
124
125 #ifndef S_TYPEISSHM
126 # ifdef S_INSHD
127 #  define S_TYPEISSHM(p) (S_ISNAM ((p)->st_mode) && (p)->st_rdev == S_INSHD)
128 # else
129 #  define S_TYPEISSHM(p) 0
130 # endif
131 #endif
132
133 #ifndef S_TYPEISMQ
134 # define S_TYPEISMQ(p) 0
135 #endif
136
137
138 /* If any of the following are undefined,
139    define them to their de facto standard values.  */
140 #if !S_ISUID
141 # define S_ISUID 04000
142 #endif
143 #if !S_ISGID
144 # define S_ISGID 02000
145 #endif
146
147 /* S_ISVTX is a common extension to POSIX.  */
148 #ifndef S_ISVTX
149 # define S_ISVTX 01000
150 #endif
151
152 #if !S_IRUSR && S_IREAD
153 # define S_IRUSR S_IREAD
154 #endif
155 #if !S_IRUSR
156 # define S_IRUSR 00400
157 #endif
158 #if !S_IRGRP
159 # define S_IRGRP (S_IRUSR >> 3)
160 #endif
161 #if !S_IROTH
162 # define S_IROTH (S_IRUSR >> 6)
163 #endif
164
165 #if !S_IWUSR && S_IWRITE
166 # define S_IWUSR S_IWRITE
167 #endif
168 #if !S_IWUSR
169 # define S_IWUSR 00200
170 #endif
171 #if !S_IWGRP
172 # define S_IWGRP (S_IWUSR >> 3)
173 #endif
174 #if !S_IWOTH
175 # define S_IWOTH (S_IWUSR >> 6)
176 #endif
177
178 #if !S_IXUSR && S_IEXEC
179 # define S_IXUSR S_IEXEC
180 #endif
181 #if !S_IXUSR
182 # define S_IXUSR 00100
183 #endif
184 #if !S_IXGRP
185 # define S_IXGRP (S_IXUSR >> 3)
186 #endif
187 #if !S_IXOTH
188 # define S_IXOTH (S_IXUSR >> 6)
189 #endif
190
191 #if !S_IRWXU
192 # define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
193 #endif
194 #if !S_IRWXG
195 # define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
196 #endif
197 #if !S_IRWXO
198 # define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
199 #endif
200
201 /* S_IXUGO is a common extension to POSIX.  */
202 #if !S_IXUGO
203 # define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
204 #endif
205
206 #ifndef S_IRWXUGO
207 # define S_IRWXUGO (S_IRWXU | S_IRWXG | S_IRWXO)
208 #endif
209
210 /* All the mode bits that can be affected by chmod.  */
211 #define CHMOD_MODE_BITS \
212   (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
213
214 #ifdef ST_MTIM_NSEC
215 # define ST_TIME_CMP_NS(a, b, ns) ((a).ns < (b).ns ? -1 : (a).ns > (b).ns)
216 #else
217 # define ST_TIME_CMP_NS(a, b, ns) 0
218 #endif
219 #define ST_TIME_CMP(a, b, s, ns) \
220   ((a).s < (b).s ? -1 : (a).s > (b).s ? 1 : ST_TIME_CMP_NS(a, b, ns))
221 #define ATIME_CMP(a, b) ST_TIME_CMP (a, b, st_atime, st_atim.ST_MTIM_NSEC)
222 #define CTIME_CMP(a, b) ST_TIME_CMP (a, b, st_ctime, st_ctim.ST_MTIM_NSEC)
223 #define MTIME_CMP(a, b) ST_TIME_CMP (a, b, st_mtime, st_mtim.ST_MTIM_NSEC)
224
225 #ifndef RETSIGTYPE
226 # define RETSIGTYPE void
227 #endif
228
229 #ifdef __DJGPP__
230   /* We need the declaration of setmode.  */
231 # include <io.h>
232   /* We need the declaration of __djgpp_set_ctrl_c.  */
233 # include <sys/exceptn.h>
234 #endif
235
236 #if HAVE_STDINT_H
237 # include <stdint.h>
238 #endif
239
240 #if HAVE_INTTYPES_H
241 # include <inttypes.h> /* for the definition of UINTMAX_MAX */
242 #endif
243
244 #if !defined PRIdMAX || PRI_MACROS_BROKEN
245 # undef PRIdMAX
246 # define PRIdMAX (sizeof (uintmax_t) == sizeof (long) ? "ld" : "lld")
247 #endif
248 #if !defined PRIoMAX || PRI_MACROS_BROKEN
249 # undef PRIoMAX
250 # define PRIoMAX (sizeof (uintmax_t) == sizeof (long) ? "lo" : "llo")
251 #endif
252 #if !defined PRIuMAX || PRI_MACROS_BROKEN
253 # undef PRIuMAX
254 # define PRIuMAX (sizeof (uintmax_t) == sizeof (long) ? "lu" : "llu")
255 #endif
256 #if !defined PRIxMAX || PRI_MACROS_BROKEN
257 # undef PRIxMAX
258 # define PRIxMAX (sizeof (uintmax_t) == sizeof (long) ? "lx" : "llx")
259 #endif
260
261 #include <ctype.h>
262
263 /* Jim Meyering writes:
264
265    "... Some ctype macros are valid only for character codes that
266    isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
267    using /bin/cc or gcc but without giving an ansi option).  So, all
268    ctype uses should be through macros like ISPRINT...  If
269    STDC_HEADERS is defined, then autoconf has verified that the ctype
270    macros don't need to be guarded with references to isascii. ...
271    Defining isascii to 1 should let any compiler worth its salt
272    eliminate the && through constant folding."
273
274    Bruno Haible adds:
275
276    "... Furthermore, isupper(c) etc. have an undefined result if c is
277    outside the range -1 <= c <= 255. One is tempted to write isupper(c)
278    with c being of type `char', but this is wrong if c is an 8-bit
279    character >= 128 which gets sign-extended to a negative value.
280    The macro ISUPPER protects against this as well."  */
281
282 #if STDC_HEADERS || (!defined (isascii) && !HAVE_ISASCII)
283 # define IN_CTYPE_DOMAIN(c) 1
284 #else
285 # define IN_CTYPE_DOMAIN(c) isascii(c)
286 #endif
287
288 #ifdef isblank
289 # define ISBLANK(c) (IN_CTYPE_DOMAIN (c) && isblank (c))
290 #else
291 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
292 #endif
293 #ifdef isgraph
294 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isgraph (c))
295 #else
296 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isprint (c) && !isspace (c))
297 #endif
298
299 /* This is defined in <sys/euc.h> on at least Solaris2.6 systems.  */
300 #undef ISPRINT
301
302 #define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
303 #define ISALNUM(c) (IN_CTYPE_DOMAIN (c) && isalnum (c))
304 #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
305 #define ISCNTRL(c) (IN_CTYPE_DOMAIN (c) && iscntrl (c))
306 #define ISLOWER(c) (IN_CTYPE_DOMAIN (c) && islower (c))
307 #define ISPUNCT(c) (IN_CTYPE_DOMAIN (c) && ispunct (c))
308 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
309 #define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
310 #define ISXDIGIT(c) (IN_CTYPE_DOMAIN (c) && isxdigit (c))
311 #define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
312
313 #if STDC_HEADERS
314 # define TOLOWER(Ch) tolower (Ch)
315 # define TOUPPER(Ch) toupper (Ch)
316 #else
317 # define TOLOWER(Ch) (ISUPPER (Ch) ? tolower (Ch) : (Ch))
318 # define TOUPPER(Ch) (ISLOWER (Ch) ? toupper (Ch) : (Ch))
319 #endif
320
321 /* ISDIGIT differs from ISDIGIT_LOCALE, as follows:
322    - Its arg may be any int or unsigned int; it need not be an unsigned char.
323    - It's guaranteed to evaluate its argument exactly once.
324    - It's typically faster.
325    POSIX says that only '0' through '9' are digits.  Prefer ISDIGIT to
326    ISDIGIT_LOCALE unless it's important to use the locale's definition
327    of `digit' even when the host does not conform to POSIX.  */
328 #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
329
330 /* Take care of NLS matters.  */
331
332 #if HAVE_LOCALE_H
333 # include <locale.h>
334 #else
335 # define setlocale(Category, Locale) /* empty */
336 #endif
337
338 #include "gettext.h"
339 #if ! ENABLE_NLS
340 # undef textdomain
341 # define textdomain(Domainname) /* empty */
342 # undef bindtextdomain
343 # define bindtextdomain(Domainname, Dirname) /* empty */
344 #endif
345
346 #define _(msgid) gettext (msgid)
347 #define N_(msgid) msgid
348
349 #ifndef HAVE_SETLOCALE
350 # define HAVE_SETLOCALE 0
351 #endif
352
353 #define STREQ(a, b) (strcmp ((a), (b)) == 0)
354
355 #if !HAVE_DECL_FREE
356 void free ();
357 #endif
358
359 #if !HAVE_DECL_MALLOC
360 char *malloc ();
361 #endif
362
363 #if !HAVE_DECL_MEMCHR
364 char *memchr ();
365 #endif
366
367 #if !HAVE_DECL_REALLOC
368 char *realloc ();
369 #endif
370
371 #if !HAVE_DECL_STPCPY
372 # ifndef stpcpy
373 char *stpcpy ();
374 # endif
375 #endif
376
377 #if !HAVE_DECL_STRNDUP
378 char *strndup ();
379 #endif
380
381 #if !HAVE_DECL_STRSTR
382 char *strstr ();
383 #endif
384
385 #if !HAVE_DECL_GETENV
386 char *getenv ();
387 #endif
388
389 #if !HAVE_DECL_LSEEK
390 off_t lseek ();
391 #endif
392
393 /* This is needed on some AIX systems.  */
394 #if !HAVE_DECL_STRTOUL
395 unsigned long strtoul ();
396 #endif
397
398 #if !HAVE_DECL_GETLOGIN
399 char *getlogin ();
400 #endif
401
402 #if !HAVE_DECL_TTYNAME
403 char *ttyname ();
404 #endif
405
406 #if !HAVE_DECL_GETEUID
407 uid_t geteuid ();
408 #endif
409
410 #if !HAVE_DECL_GETPWUID
411 struct passwd *getpwuid ();
412 #endif
413
414 #if !HAVE_DECL_GETGRGID
415 struct group *getgrgid ();
416 #endif
417
418 #if !HAVE_DECL_GETUID
419 uid_t getuid ();
420 #endif
421
422 #include "xalloc.h"
423
424 #if ! defined HAVE_MEMPCPY && ! defined mempcpy
425 /* Be CAREFUL that there are no side effects in N.  */
426 # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
427 #endif
428
429 /* Include automatically-generated macros for unlocked I/O.  */
430 #include "unlocked-io.h"
431
432 #define SAME_INODE(Stat_buf_1, Stat_buf_2) \
433   ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
434    && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
435
436 #define DOT_OR_DOTDOT(Basename) \
437   (Basename[0] == '.' && (Basename[1] == '\0' \
438                           || (Basename[1] == '.' && Basename[2] == '\0')))
439
440 #if SETVBUF_REVERSED
441 # define SETVBUF(Stream, Buffer, Type, Size) \
442     setvbuf (Stream, Type, Buffer, Size)
443 #else
444 # define SETVBUF(Stream, Buffer, Type, Size) \
445     setvbuf (Stream, Buffer, Type, Size)
446 #endif
447
448 /* Factor out some of the common --help and --version processing code.  */
449
450 /* These enum values cannot possibly conflict with the option values
451    ordinarily used by commands, including CHAR_MAX + 1, etc.  Avoid
452    CHAR_MIN - 1, as it may equal -1, the getopt end-of-options value.  */
453 enum
454 {
455   GETOPT_HELP_CHAR = (CHAR_MIN - 2),
456   GETOPT_VERSION_CHAR = (CHAR_MIN - 3)
457 };
458
459 #define GETOPT_HELP_OPTION_DECL \
460   "help", no_argument, 0, GETOPT_HELP_CHAR
461 #define GETOPT_VERSION_OPTION_DECL \
462   "version", no_argument, 0, GETOPT_VERSION_CHAR
463
464 #define case_GETOPT_HELP_CHAR                   \
465   case GETOPT_HELP_CHAR:                        \
466     usage (EXIT_SUCCESS);                       \
467     break;
468
469 #define HELP_OPTION_DESCRIPTION \
470   _("      --help     display this help and exit\n")
471 #define VERSION_OPTION_DESCRIPTION \
472   _("      --version  output version information and exit\n")
473
474 #include "closeout.h"
475 #include "version-etc.h"
476
477 #define case_GETOPT_VERSION_CHAR(Program_name, Authors)                 \
478   case GETOPT_VERSION_CHAR:                                             \
479     version_etc (stdout, Program_name, PACKAGE, VERSION, Authors, NULL);\
480     exit (EXIT_SUCCESS);                                                \
481     break;
482
483 #ifndef MAX
484 # define MAX(a, b) ((a) > (b) ? (a) : (b))
485 #endif
486
487 #ifndef MIN
488 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
489 #endif
490
491 #ifndef CHAR_BIT
492 # define CHAR_BIT 8
493 #endif
494
495 /* The extra casts work around common compiler bugs.  */
496 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
497 /* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
498    It is necessary at least when t == time_t.  */
499 #define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
500                               ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) : (t) 0))
501 #define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
502
503 /* Upper bound on the string length of an integer converted to string.
504    302 / 1000 is ceil (log10 (2.0)).  Subtract 1 for the sign bit;
505    add 1 for integer division truncation; add 1 more for a minus sign.  */
506 #define INT_STRLEN_BOUND(t) ((sizeof (t) * CHAR_BIT - 1) * 302 / 1000 + 2)
507
508 #ifndef CHAR_MIN
509 # define CHAR_MIN TYPE_MINIMUM (char)
510 #endif
511
512 #ifndef CHAR_MAX
513 # define CHAR_MAX TYPE_MAXIMUM (char)
514 #endif
515
516 #ifndef SCHAR_MIN
517 # define SCHAR_MIN (-1 - SCHAR_MAX)
518 #endif
519
520 #ifndef SCHAR_MAX
521 # define SCHAR_MAX (CHAR_MAX == UCHAR_MAX ? CHAR_MAX / 2 : CHAR_MAX)
522 #endif
523
524 #ifndef UCHAR_MAX
525 # define UCHAR_MAX TYPE_MAXIMUM (unsigned char)
526 #endif
527
528 #ifndef SHRT_MIN
529 # define SHRT_MIN TYPE_MINIMUM (short int)
530 #endif
531
532 #ifndef SHRT_MAX
533 # define SHRT_MAX TYPE_MAXIMUM (short int)
534 #endif
535
536 #ifndef INT_MAX
537 # define INT_MAX TYPE_MAXIMUM (int)
538 #endif
539
540 #ifndef INT_MIN
541 # define INT_MIN TYPE_MINIMUM (int)
542 #endif
543
544 #ifndef UINT_MAX
545 # define UINT_MAX TYPE_MAXIMUM (unsigned int)
546 #endif
547
548 #ifndef LONG_MAX
549 # define LONG_MAX TYPE_MAXIMUM (long)
550 #endif
551
552 #ifndef ULONG_MAX
553 # define ULONG_MAX TYPE_MAXIMUM (unsigned long)
554 #endif
555
556 #ifndef SIZE_MAX
557 # define SIZE_MAX TYPE_MAXIMUM (size_t)
558 #endif
559
560 #ifndef SSIZE_MAX
561 # define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
562 #endif
563
564 #ifndef UINTMAX_MAX
565 # define UINTMAX_MAX TYPE_MAXIMUM (uintmax_t)
566 #endif
567
568 #ifndef OFF_T_MIN
569 # define OFF_T_MIN TYPE_MINIMUM (off_t)
570 #endif
571
572 #ifndef OFF_T_MAX
573 # define OFF_T_MAX TYPE_MAXIMUM (off_t)
574 #endif
575
576 #ifndef UID_T_MAX
577 # define UID_T_MAX TYPE_MAXIMUM (uid_t)
578 #endif
579
580 #ifndef GID_T_MAX
581 # define GID_T_MAX TYPE_MAXIMUM (gid_t)
582 #endif
583
584 #ifndef PID_T_MAX
585 # define PID_T_MAX TYPE_MAXIMUM (pid_t)
586 #endif
587
588 /* Use this to suppress gcc's `...may be used before initialized' warnings. */
589 #ifdef lint
590 # define IF_LINT(Code) Code
591 #else
592 # define IF_LINT(Code) /* empty */
593 #endif
594
595 #ifndef __attribute__
596 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
597 #  define __attribute__(x)
598 # endif
599 #endif
600
601 #ifndef ATTRIBUTE_NORETURN
602 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
603 #endif
604
605 #ifndef ATTRIBUTE_UNUSED
606 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
607 #endif
608
609 #if defined strdupa
610 # define ASSIGN_STRDUPA(DEST, S)                \
611   do { DEST = strdupa (S); } while (0)
612 #else
613 # define ASSIGN_STRDUPA(DEST, S)                \
614   do                                            \
615     {                                           \
616       const char *s_ = (S);                     \
617       size_t len_ = strlen (s_) + 1;            \
618       char *tmp_dest_ = alloca (len_);          \
619       DEST = memcpy (tmp_dest_, (s_), len_);    \
620     }                                           \
621   while (0)
622 #endif
623
624 #ifndef EOVERFLOW
625 # define EOVERFLOW EINVAL
626 #endif
627
628 #if ! HAVE_FSEEKO && ! defined fseeko
629 # define fseeko(s, o, w) ((o) == (long) (o)             \
630                           ? fseek (s, o, w)             \
631                           : (errno = EOVERFLOW, -1))
632 #endif