*** empty log message ***
[coreutils.git] / src / sys2.h
blob0ba910eb9e5332cb29f241684e15e48d011720ae
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. */
6 #ifndef S_IFMT
7 # define S_IFMT 0170000
8 #endif
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
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
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
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
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
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
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
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
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
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
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
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
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
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
133 #ifndef S_TYPEISMQ
134 # define S_TYPEISMQ(p) 0
135 #endif
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
147 /* S_ISVTX is a common extension to POSIX. */
148 #ifndef S_ISVTX
149 # define S_ISVTX 01000
150 #endif
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
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
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
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
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
206 #ifndef S_IRWXUGO
207 # define S_IRWXUGO (S_IRWXU | S_IRWXG | S_IRWXO)
208 #endif
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)
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)
225 #ifndef RETSIGTYPE
226 # define RETSIGTYPE void
227 #endif
229 #if __GNUC__
230 # ifndef alloca
231 # define alloca __builtin_alloca
232 # endif
233 #else
234 # if HAVE_ALLOCA_H
235 # include <alloca.h>
236 # else
237 # ifdef _AIX
238 # pragma alloca
239 # else
240 # ifdef _WIN32
241 # include <malloc.h>
242 # include <io.h>
243 # else
244 # ifndef alloca
245 char *alloca ();
246 # endif
247 # endif
248 # endif
249 # endif
250 #endif
252 #ifdef __DJGPP__
253 /* We need the declaration of setmode. */
254 # include <io.h>
255 /* We need the declaration of __djgpp_set_ctrl_c. */
256 # include <sys/exceptn.h>
257 #endif
259 #if HAVE_STDINT_H
260 # include <stdint.h>
261 #endif
263 #if HAVE_INTTYPES_H
264 # include <inttypes.h> /* for the definition of UINTMAX_MAX */
265 #endif
267 #include <ctype.h>
269 /* Jim Meyering writes:
271 "... Some ctype macros are valid only for character codes that
272 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
273 using /bin/cc or gcc but without giving an ansi option). So, all
274 ctype uses should be through macros like ISPRINT... If
275 STDC_HEADERS is defined, then autoconf has verified that the ctype
276 macros don't need to be guarded with references to isascii. ...
277 Defining isascii to 1 should let any compiler worth its salt
278 eliminate the && through constant folding."
280 Bruno Haible adds:
282 "... Furthermore, isupper(c) etc. have an undefined result if c is
283 outside the range -1 <= c <= 255. One is tempted to write isupper(c)
284 with c being of type `char', but this is wrong if c is an 8-bit
285 character >= 128 which gets sign-extended to a negative value.
286 The macro ISUPPER protects against this as well." */
288 #if STDC_HEADERS || (!defined (isascii) && !HAVE_ISASCII)
289 # define IN_CTYPE_DOMAIN(c) 1
290 #else
291 # define IN_CTYPE_DOMAIN(c) isascii(c)
292 #endif
294 #ifdef isblank
295 # define ISBLANK(c) (IN_CTYPE_DOMAIN (c) && isblank (c))
296 #else
297 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
298 #endif
299 #ifdef isgraph
300 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isgraph (c))
301 #else
302 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isprint (c) && !isspace (c))
303 #endif
305 /* This is defined in <sys/euc.h> on at least Solaris2.6 systems. */
306 #undef ISPRINT
308 #define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
309 #define ISALNUM(c) (IN_CTYPE_DOMAIN (c) && isalnum (c))
310 #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
311 #define ISCNTRL(c) (IN_CTYPE_DOMAIN (c) && iscntrl (c))
312 #define ISLOWER(c) (IN_CTYPE_DOMAIN (c) && islower (c))
313 #define ISPUNCT(c) (IN_CTYPE_DOMAIN (c) && ispunct (c))
314 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
315 #define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
316 #define ISXDIGIT(c) (IN_CTYPE_DOMAIN (c) && isxdigit (c))
317 #define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
319 #if STDC_HEADERS
320 # define TOLOWER(Ch) tolower (Ch)
321 # define TOUPPER(Ch) toupper (Ch)
322 #else
323 # define TOLOWER(Ch) (ISUPPER (Ch) ? tolower (Ch) : (Ch))
324 # define TOUPPER(Ch) (ISLOWER (Ch) ? toupper (Ch) : (Ch))
325 #endif
327 /* ISDIGIT differs from ISDIGIT_LOCALE, as follows:
328 - Its arg may be any int or unsigned int; it need not be an unsigned char.
329 - It's guaranteed to evaluate its argument exactly once.
330 - It's typically faster.
331 POSIX says that only '0' through '9' are digits. Prefer ISDIGIT to
332 ISDIGIT_LOCALE unless it's important to use the locale's definition
333 of `digit' even when the host does not conform to POSIX. */
334 #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
336 #ifndef PARAMS
337 # if PROTOTYPES
338 # define PARAMS(Args) Args
339 # else
340 # define PARAMS(Args) ()
341 # endif
342 #endif
344 /* Take care of NLS matters. */
346 #if HAVE_LOCALE_H
347 # include <locale.h>
348 #else
349 # define setlocale(Category, Locale) /* empty */
350 #endif
352 #include "gettext.h"
354 #define _(msgid) gettext (msgid)
355 #define N_(msgid) msgid
357 #ifndef HAVE_SETLOCALE
358 # define HAVE_SETLOCALE 0
359 #endif
361 #define STREQ(a, b) (strcmp ((a), (b)) == 0)
363 #if !HAVE_DECL_FREE
364 void free ();
365 #endif
367 #if !HAVE_DECL_MALLOC
368 char *malloc ();
369 #endif
371 #if !HAVE_DECL_MEMCHR
372 char *memchr ();
373 #endif
375 #if !HAVE_DECL_REALLOC
376 char *realloc ();
377 #endif
379 #if !HAVE_DECL_STPCPY
380 # ifndef stpcpy
381 char *stpcpy ();
382 # endif
383 #endif
385 #if !HAVE_DECL_STRNDUP
386 char *strndup ();
387 #endif
389 #if !HAVE_DECL_STRSTR
390 char *strstr ();
391 #endif
393 #if !HAVE_DECL_GETENV
394 char *getenv ();
395 #endif
397 #if !HAVE_DECL_LSEEK
398 off_t lseek ();
399 #endif
401 /* This is needed on some AIX systems. */
402 #if !HAVE_DECL_STRTOUL
403 unsigned long strtoul ();
404 #endif
406 /* This is needed on some AIX systems. */
407 #if !HAVE_DECL_STRTOULL && HAVE_UNSIGNED_LONG_LONG
408 unsigned long long strtoull ();
409 #endif
411 #if !HAVE_DECL_GETLOGIN
412 char *getlogin ();
413 #endif
415 #if !HAVE_DECL_TTYNAME
416 char *ttyname ();
417 #endif
419 #if !HAVE_DECL_GETEUID
420 uid_t geteuid ();
421 #endif
423 #if !HAVE_DECL_GETPWUID
424 struct passwd *getpwuid ();
425 #endif
427 #if !HAVE_DECL_GETGRGID
428 struct group *getgrgid ();
429 #endif
431 #if !HAVE_DECL_GETUID
432 uid_t getuid ();
433 #endif
435 #include "xalloc.h"
437 #if ! defined HAVE_MEMPCPY && ! defined mempcpy
438 /* Be CAREFUL that there are no side effects in N. */
439 # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
440 #endif
442 /* Include automatically-generated macros for unlocked I/O. */
443 #include "unlocked-io.h"
445 #define SAME_INODE(Stat_buf_1, Stat_buf_2) \
446 ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
447 && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
449 #define DOT_OR_DOTDOT(Basename) \
450 (Basename[0] == '.' && (Basename[1] == '\0' \
451 || (Basename[1] == '.' && Basename[2] == '\0')))
453 #if SETVBUF_REVERSED
454 # define SETVBUF(Stream, Buffer, Type, Size) \
455 setvbuf (Stream, Type, Buffer, Size)
456 #else
457 # define SETVBUF(Stream, Buffer, Type, Size) \
458 setvbuf (Stream, Buffer, Type, Size)
459 #endif
461 /* Factor out some of the common --help and --version processing code. */
463 /* These enum values cannot possibly conflict with the option values
464 ordinarily used by commands, including CHAR_MAX + 1, etc. Avoid
465 CHAR_MIN - 1, as it may equal -1, the getopt end-of-options value. */
466 enum
468 GETOPT_HELP_CHAR = (CHAR_MIN - 2),
469 GETOPT_VERSION_CHAR = (CHAR_MIN - 3)
472 #define GETOPT_HELP_OPTION_DECL \
473 "help", no_argument, 0, GETOPT_HELP_CHAR
474 #define GETOPT_VERSION_OPTION_DECL \
475 "version", no_argument, 0, GETOPT_VERSION_CHAR
477 #define case_GETOPT_HELP_CHAR \
478 case GETOPT_HELP_CHAR: \
479 usage (EXIT_SUCCESS); \
480 break;
482 #define HELP_OPTION_DESCRIPTION \
483 _(" --help display this help and exit\n")
484 #define VERSION_OPTION_DESCRIPTION \
485 _(" --version output version information and exit\n")
487 #include "closeout.h"
488 #include "version-etc.h"
490 #define case_GETOPT_VERSION_CHAR(Program_name, Authors) \
491 case GETOPT_VERSION_CHAR: \
492 version_etc (stdout, Program_name, PACKAGE, VERSION, Authors); \
493 exit (EXIT_SUCCESS); \
494 break;
496 #ifndef MAX
497 # define MAX(a, b) ((a) > (b) ? (a) : (b))
498 #endif
500 #ifndef MIN
501 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
502 #endif
504 #ifndef CHAR_BIT
505 # define CHAR_BIT 8
506 #endif
508 /* The extra casts work around common compiler bugs. */
509 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
510 /* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
511 It is necessary at least when t == time_t. */
512 #define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
513 ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) : (t) 0))
514 #define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
516 /* Upper bound on the string length of an integer converted to string.
517 302 / 1000 is ceil (log10 (2.0)). Subtract 1 for the sign bit;
518 add 1 for integer division truncation; add 1 more for a minus sign. */
519 #define INT_STRLEN_BOUND(t) ((sizeof (t) * CHAR_BIT - 1) * 302 / 1000 + 2)
521 #ifndef CHAR_MIN
522 # define CHAR_MIN TYPE_MINIMUM (char)
523 #endif
525 #ifndef CHAR_MAX
526 # define CHAR_MAX TYPE_MAXIMUM (char)
527 #endif
529 #ifndef SCHAR_MIN
530 # define SCHAR_MIN (-1 - SCHAR_MAX)
531 #endif
533 #ifndef SCHAR_MAX
534 # define SCHAR_MAX (CHAR_MAX == UCHAR_MAX ? CHAR_MAX / 2 : CHAR_MAX)
535 #endif
537 #ifndef UCHAR_MAX
538 # define UCHAR_MAX TYPE_MAXIMUM (unsigned char)
539 #endif
541 #ifndef SHRT_MIN
542 # define SHRT_MIN TYPE_MINIMUM (short int)
543 #endif
545 #ifndef SHRT_MAX
546 # define SHRT_MAX TYPE_MAXIMUM (short int)
547 #endif
549 #ifndef INT_MAX
550 # define INT_MAX TYPE_MAXIMUM (int)
551 #endif
553 #ifndef UINT_MAX
554 # define UINT_MAX TYPE_MAXIMUM (unsigned int)
555 #endif
557 #ifndef LONG_MAX
558 # define LONG_MAX TYPE_MAXIMUM (long)
559 #endif
561 #ifndef ULONG_MAX
562 # define ULONG_MAX TYPE_MAXIMUM (unsigned long)
563 #endif
565 #ifndef SIZE_MAX
566 # define SIZE_MAX TYPE_MAXIMUM (size_t)
567 #endif
569 #ifndef UINTMAX_MAX
570 # define UINTMAX_MAX TYPE_MAXIMUM (uintmax_t)
571 #endif
573 #ifndef OFF_T_MIN
574 # define OFF_T_MIN TYPE_MINIMUM (off_t)
575 #endif
577 #ifndef OFF_T_MAX
578 # define OFF_T_MAX TYPE_MAXIMUM (off_t)
579 #endif
581 #ifndef UID_T_MAX
582 # define UID_T_MAX TYPE_MAXIMUM (uid_t)
583 #endif
585 #ifndef GID_T_MAX
586 # define GID_T_MAX TYPE_MAXIMUM (gid_t)
587 #endif
589 #ifndef PID_T_MAX
590 # define PID_T_MAX TYPE_MAXIMUM (pid_t)
591 #endif
593 #ifndef CHAR_BIT
594 # define CHAR_BIT 8
595 #endif
597 /* Use this to suppress gcc's `...may be used before initialized' warnings. */
598 #ifdef lint
599 # define IF_LINT(Code) Code
600 #else
601 # define IF_LINT(Code) /* empty */
602 #endif
604 #ifndef __attribute__
605 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
606 # define __attribute__(x)
607 # endif
608 #endif
610 #ifndef ATTRIBUTE_NORETURN
611 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
612 #endif
614 #ifndef ATTRIBUTE_UNUSED
615 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
616 #endif
618 #if defined strdupa
619 # define ASSIGN_STRDUPA(DEST, S) \
620 do { DEST = strdupa(S); } while (0)
621 #else
622 # define ASSIGN_STRDUPA(DEST, S) \
623 do \
625 const char *s_ = (S); \
626 size_t len_ = strlen (s_) + 1; \
627 char *tmp_dest_ = (char *) alloca (len_); \
628 DEST = memcpy (tmp_dest_, (s_), len_); \
630 while (0)
631 #endif
633 #ifndef EOVERFLOW
634 # define EOVERFLOW EINVAL
635 #endif
637 #if ! HAVE_FSEEKO && ! defined fseeko
638 # define fseeko(s, o, w) ((o) == (long) (o) \
639 ? fseek (s, o, w) \
640 : (errno = EOVERFLOW, -1))
641 #endif