[HAVE_INTTYPES_H]: Include <inttypes.h>.
[coreutils.git] / src / sys2.h
blob7eda8ef3a1b1c7e521c1c89baaeb375caabc9acc
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 #if STAT_MACROS_BROKEN
7 # undef S_ISBLK
8 # undef S_ISCHR
9 # undef S_ISDIR
10 # undef S_ISDOOR
11 # undef S_ISFIFO
12 # undef S_ISLNK
13 # undef S_ISMPB
14 # undef S_ISMPC
15 # undef S_ISNWK
16 # undef S_ISREG
17 # undef S_ISSOCK
18 #endif /* STAT_MACROS_BROKEN. */
20 #ifndef S_IFMT
21 # define S_IFMT 0170000
22 #endif
23 #if !defined(S_ISBLK) && defined(S_IFBLK)
24 # define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
25 #endif
26 #if !defined(S_ISCHR) && defined(S_IFCHR)
27 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
28 #endif
29 #if !defined(S_ISDIR) && defined(S_IFDIR)
30 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
31 #endif
32 #if !defined(S_ISREG) && defined(S_IFREG)
33 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
34 #endif
35 #if !defined(S_ISFIFO) && defined(S_IFIFO)
36 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
37 #endif
38 #if !defined(S_ISLNK) && defined(S_IFLNK)
39 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
40 #endif
41 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
42 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
43 #endif
44 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
45 # define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
46 # define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
47 #endif
48 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
49 # define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
50 #endif
51 #if !defined(S_ISDOOR) && defined(S_IFDOOR) /* Solaris 2.5 and up */
52 # define S_ISDOOR(m) (((m) & S_IFMT) == S_IFDOOR)
53 #endif
55 #if !S_ISUID
56 # define S_ISUID 04000
57 #endif
58 #if !S_ISGID
59 # define S_ISGID 02000
60 #endif
62 /* S_ISVTX is a common extension to POSIX.1. */
63 #ifndef S_ISVTX
64 # define S_ISVTX 01000
65 #endif
67 #if !S_IRUSR && S_IREAD
68 # define S_IRUSR S_IREAD
69 #endif
70 #if !S_IRUSR
71 # define S_IRUSR 00400
72 #endif
73 #if !S_IRGRP
74 # define S_IRGRP (S_IRUSR >> 3)
75 #endif
76 #if !S_IROTH
77 # define S_IROTH (S_IRUSR >> 6)
78 #endif
80 #if !S_IWUSR && S_IWRITE
81 # define S_IWUSR S_IWRITE
82 #endif
83 #if !S_IWUSR
84 # define S_IWUSR 00200
85 #endif
86 #if !S_IWGRP
87 # define S_IWGRP (S_IWUSR >> 3)
88 #endif
89 #if !S_IWOTH
90 # define S_IWOTH (S_IWUSR >> 6)
91 #endif
93 #if !S_IXUSR && S_IEXEC
94 # define S_IXUSR S_IEXEC
95 #endif
96 #if !S_IXUSR
97 # define S_IXUSR 00100
98 #endif
99 #if !S_IXGRP
100 # define S_IXGRP (S_IXUSR >> 3)
101 #endif
102 #if !S_IXOTH
103 # define S_IXOTH (S_IXUSR >> 6)
104 #endif
106 #if !S_IRWXU
107 # define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
108 #endif
109 #if !S_IRWXG
110 # define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
111 #endif
112 #if !S_IRWXO
113 # define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
114 #endif
116 /* S_IXUGO is a common extension to POSIX.1. */
117 #if !S_IXUGO
118 # define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
119 #endif
121 #ifndef S_IRWXUGO
122 # define S_IRWXUGO (S_IRWXU | S_IRWXG | S_IRWXO)
123 #endif
125 /* All the mode bits that can be affected by chmod. */
126 #define CHMOD_MODE_BITS \
127 (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
129 #ifdef ST_MTIM_NSEC
130 # define ST_TIME_CMP_NS(a, b, ns) ((a).ns < (b).ns ? -1 : (a).ns > (b).ns)
131 #else
132 # define ST_TIME_CMP_NS(a, b, ns) 0
133 #endif
134 #define ST_TIME_CMP(a, b, s, ns) \
135 ((a).s < (b).s ? -1 : (a).s > (b).s ? 1 : ST_TIME_CMP_NS(a, b, ns))
136 #define ATIME_CMP(a, b) ST_TIME_CMP (a, b, st_atime, st_atim.ST_MTIM_NSEC)
137 #define CTIME_CMP(a, b) ST_TIME_CMP (a, b, st_ctime, st_ctim.ST_MTIM_NSEC)
138 #define MTIME_CMP(a, b) ST_TIME_CMP (a, b, st_mtime, st_mtim.ST_MTIM_NSEC)
140 #ifndef RETSIGTYPE
141 # define RETSIGTYPE void
142 #endif
144 #ifndef __GNUC__
145 # if HAVE_ALLOCA_H
146 # include <alloca.h>
147 # else
148 # ifdef _AIX
149 # pragma alloca
150 # else
151 # ifdef _WIN32
152 # include <malloc.h>
153 # include <io.h>
154 # else
155 # ifndef alloca
156 char *alloca ();
157 # endif
158 # endif
159 # endif
160 # endif
161 #endif
163 #ifdef __DJGPP__
164 /* We need the declaration of setmode. */
165 # include <io.h>
166 /* We need the declaration of __djgpp_set_ctrl_c. */
167 # include <sys/exceptn.h>
168 #endif
170 #if HAVE_STDINT_H
171 # include <stdint.h>
172 #endif
174 #if HAVE_INTTYPES_H
175 # include <inttypes.h> /* for the definition of UINTMAX_MAX */
176 #endif
178 #include <ctype.h>
180 /* Jim Meyering writes:
182 "... Some ctype macros are valid only for character codes that
183 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
184 using /bin/cc or gcc but without giving an ansi option). So, all
185 ctype uses should be through macros like ISPRINT... If
186 STDC_HEADERS is defined, then autoconf has verified that the ctype
187 macros don't need to be guarded with references to isascii. ...
188 Defining isascii to 1 should let any compiler worth its salt
189 eliminate the && through constant folding."
191 Bruno Haible adds:
193 "... Furthermore, isupper(c) etc. have an undefined result if c is
194 outside the range -1 <= c <= 255. One is tempted to write isupper(c)
195 with c being of type `char', but this is wrong if c is an 8-bit
196 character >= 128 which gets sign-extended to a negative value.
197 The macro ISUPPER protects against this as well." */
199 #if STDC_HEADERS || (!defined (isascii) && !HAVE_ISASCII)
200 # define IN_CTYPE_DOMAIN(c) 1
201 #else
202 # define IN_CTYPE_DOMAIN(c) isascii(c)
203 #endif
205 #ifdef isblank
206 # define ISBLANK(c) (IN_CTYPE_DOMAIN (c) && isblank (c))
207 #else
208 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
209 #endif
210 #ifdef isgraph
211 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isgraph (c))
212 #else
213 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isprint (c) && !isspace (c))
214 #endif
216 /* This is defined in <sys/euc.h> on at least Solaris2.6 systems. */
217 #undef ISPRINT
219 #define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
220 #define ISALNUM(c) (IN_CTYPE_DOMAIN (c) && isalnum (c))
221 #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
222 #define ISCNTRL(c) (IN_CTYPE_DOMAIN (c) && iscntrl (c))
223 #define ISLOWER(c) (IN_CTYPE_DOMAIN (c) && islower (c))
224 #define ISPUNCT(c) (IN_CTYPE_DOMAIN (c) && ispunct (c))
225 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
226 #define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
227 #define ISXDIGIT(c) (IN_CTYPE_DOMAIN (c) && isxdigit (c))
228 #define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
230 #if STDC_HEADERS
231 # define TOLOWER(Ch) tolower (Ch)
232 # define TOUPPER(Ch) toupper (Ch)
233 #else
234 # define TOLOWER(Ch) (ISUPPER (Ch) ? tolower (Ch) : (Ch))
235 # define TOUPPER(Ch) (ISLOWER (Ch) ? toupper (Ch) : (Ch))
236 #endif
238 /* ISDIGIT differs from ISDIGIT_LOCALE, as follows:
239 - Its arg may be any int or unsigned int; it need not be an unsigned char.
240 - It's guaranteed to evaluate its argument exactly once.
241 - It's typically faster.
242 Posix 1003.2-1992 section 2.5.2.1 page 50 lines 1556-1558 says that
243 only '0' through '9' are digits. Prefer ISDIGIT to ISDIGIT_LOCALE unless
244 it's important to use the locale's definition of `digit' even when the
245 host does not conform to Posix. */
246 #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
248 #ifndef PARAMS
249 # if PROTOTYPES
250 # define PARAMS(Args) Args
251 # else
252 # define PARAMS(Args) ()
253 # endif
254 #endif
256 /* Take care of NLS matters. */
258 #if HAVE_LOCALE_H
259 # include <locale.h>
260 #endif
261 #if !HAVE_SETLOCALE
262 # define setlocale(Category, Locale) /* empty */
263 #endif
265 #if ENABLE_NLS
266 # include <libintl.h>
267 # define _(Text) gettext (Text)
268 #else
269 # undef bindtextdomain
270 # define bindtextdomain(Domain, Directory) /* empty */
271 # undef textdomain
272 # define textdomain(Domain) /* empty */
273 # define _(Text) Text
274 #endif
275 #define N_(Text) Text
277 #define STREQ(a, b) (strcmp ((a), (b)) == 0)
279 #if !HAVE_DECL_FREE
280 void free ();
281 #endif
283 #if !HAVE_DECL_MALLOC
284 char *malloc ();
285 #endif
287 #if !HAVE_DECL_MEMCHR
288 char *memchr ();
289 #endif
291 #if !HAVE_DECL_REALLOC
292 char *realloc ();
293 #endif
295 #if !HAVE_DECL_STPCPY
296 # ifndef stpcpy
297 char *stpcpy ();
298 # endif
299 #endif
301 #if !HAVE_DECL_STRNDUP
302 char *strndup ();
303 #endif
305 #if !HAVE_DECL_STRSTR
306 char *strstr ();
307 #endif
309 #if !HAVE_DECL_GETENV
310 char *getenv ();
311 #endif
313 #if !HAVE_DECL_LSEEK
314 off_t lseek ();
315 #endif
317 /* This is needed on some AIX systems. */
318 #if !HAVE_DECL_STRTOUL
319 unsigned long strtoul ();
320 #endif
322 /* This is needed on some AIX systems. */
323 #if !HAVE_DECL_STRTOULL && HAVE_UNSIGNED_LONG_LONG
324 unsigned long long strtoull ();
325 #endif
327 #if !HAVE_DECL_GETLOGIN
328 char *getlogin ();
329 #endif
331 #if !HAVE_DECL_TTYNAME
332 char *ttyname ();
333 #endif
335 #if !HAVE_DECL_GETEUID
336 uid_t geteuid ();
337 #endif
339 #if !HAVE_DECL_GETPWUID
340 struct passwd *getpwuid ();
341 #endif
343 #if !HAVE_DECL_GETGRGID
344 struct group *getgrgid ();
345 #endif
347 #if !HAVE_DECL_GETUID
348 uid_t getuid ();
349 #endif
351 #include "xalloc.h"
353 #if ! defined HAVE_MEMPCPY && ! defined mempcpy
354 /* Be CAREFUL that there are no side effects in N. */
355 # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
356 #endif
358 /* These are wrappers for functions/macros from GNU libc.
359 The standard I/O functions are thread-safe. These *_unlocked ones
360 are more efficient but not thread-safe. That they're not thread-safe
361 is fine since all these applications are single threaded. */
363 #if HAVE_CLEARERR_UNLOCKED
364 # undef clearerr
365 # define clearerr(S) clearerr_unlocked (S)
366 #endif
368 #if HAVE_FEOF_UNLOCKED
369 # undef feof
370 # define feof(S) feof_unlocked (S)
371 #endif
373 #if HAVE_FERROR_UNLOCKED
374 # undef ferror
375 # define ferror(S) ferror_unlocked (S)
376 #endif
378 #if HAVE_FFLUSH_UNLOCKED
379 # undef fflush
380 # define fflush(S) fflush_unlocked (S)
381 #endif
383 #if HAVE_FPUTC_UNLOCKED
384 # undef fputc
385 # define fputc(C, S) fputc_unlocked (C, S)
386 #endif
388 #if HAVE_FREAD_UNLOCKED
389 # undef fread
390 # define fread(P, Z, N, S) fread_unlocked (P, Z, N, S)
391 #endif
393 #if HAVE_FWRITE_UNLOCKED
394 # undef fwrite
395 # define fwrite(P, Z, N, S) fwrite_unlocked (P, Z, N, S)
396 #endif
398 #if HAVE_GETC_UNLOCKED
399 # undef getc
400 # define getc(S) getc_unlocked (S)
401 #endif
403 #if HAVE_GETCHAR_UNLOCKED
404 # undef getchar
405 # define getchar(S) getchar_unlocked (S)
406 #endif
408 #if HAVE_PUTC_UNLOCKED
409 # undef putc
410 # define putc(C, S) putc_unlocked (C, S)
411 #endif
413 #if HAVE_PUTCHAR_UNLOCKED
414 # undef putchar
415 # define putchar(C) putchar_unlocked (C)
416 #endif
418 #define SAME_INODE(Stat_buf_1, Stat_buf_2) \
419 ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
420 && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
422 #define DOT_OR_DOTDOT(Basename) \
423 (Basename[0] == '.' && (Basename[1] == '\0' \
424 || (Basename[1] == '.' && Basename[2] == '\0')))
426 #if SETVBUF_REVERSED
427 # define SETVBUF(Stream, Buffer, Type, Size) \
428 setvbuf (Stream, Type, Buffer, Size)
429 #else
430 # define SETVBUF(Stream, Buffer, Type, Size) \
431 setvbuf (Stream, Buffer, Type, Size)
432 #endif
434 char *base_name PARAMS ((char const *));
436 /* Factor out some of the common --help and --version processing code. */
438 /* These enum values cannot possibly conflict with the option values
439 ordinarily used by commands, including CHAR_MAX + 1, etc. Avoid
440 CHAR_MIN - 1, as it may equal -1, the getopt end-of-options value. */
441 enum
443 GETOPT_HELP_CHAR = (CHAR_MIN - 2),
444 GETOPT_VERSION_CHAR = (CHAR_MIN - 3)
447 #define GETOPT_HELP_OPTION_DECL \
448 "help", no_argument, 0, GETOPT_HELP_CHAR
449 #define GETOPT_VERSION_OPTION_DECL \
450 "version", no_argument, 0, GETOPT_VERSION_CHAR
452 #define case_GETOPT_HELP_CHAR \
453 case GETOPT_HELP_CHAR: \
454 usage (EXIT_SUCCESS); \
455 break;
457 #include "closeout.h"
458 #include "version-etc.h"
460 #define case_GETOPT_VERSION_CHAR(Program_name, Authors) \
461 case GETOPT_VERSION_CHAR: \
462 version_etc (stdout, Program_name, GNU_PACKAGE, VERSION, Authors); \
463 exit (EXIT_SUCCESS); \
464 break;
466 #ifndef MAX
467 # define MAX(a, b) ((a) > (b) ? (a) : (b))
468 #endif
470 #ifndef MIN
471 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
472 #endif
474 #ifndef CHAR_BIT
475 # define CHAR_BIT 8
476 #endif
478 /* The extra casts work around common compiler bugs. */
479 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
480 /* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
481 It is necessary at least when t == time_t. */
482 #define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
483 ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) : (t) 0))
484 #define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
486 #ifndef CHAR_MIN
487 # define CHAR_MIN TYPE_MINIMUM (char)
488 #endif
490 #ifndef CHAR_MAX
491 # define CHAR_MAX TYPE_MAXIMUM (char)
492 #endif
494 #ifndef SCHAR_MIN
495 # define SCHAR_MIN (-1 - SCHAR_MAX)
496 #endif
498 #ifndef SCHAR_MAX
499 # define SCHAR_MAX (CHAR_MAX == UCHAR_MAX ? CHAR_MAX / 2 : CHAR_MAX)
500 #endif
502 #ifndef UCHAR_MAX
503 # define UCHAR_MAX TYPE_MAXIMUM (unsigned char)
504 #endif
506 #ifndef SHRT_MIN
507 # define SHRT_MIN TYPE_MINIMUM (short int)
508 #endif
510 #ifndef SHRT_MAX
511 # define SHRT_MAX TYPE_MAXIMUM (short int)
512 #endif
514 #ifndef INT_MAX
515 # define INT_MAX TYPE_MAXIMUM (int)
516 #endif
518 #ifndef UINT_MAX
519 # define UINT_MAX TYPE_MAXIMUM (unsigned int)
520 #endif
522 #ifndef LONG_MAX
523 # define LONG_MAX TYPE_MAXIMUM (long)
524 #endif
526 #ifndef ULONG_MAX
527 # define ULONG_MAX TYPE_MAXIMUM (unsigned long)
528 #endif
530 #ifndef SIZE_MAX
531 # define SIZE_MAX TYPE_MAXIMUM (size_t)
532 #endif
534 #ifndef UINTMAX_MAX
535 # define UINTMAX_MAX TYPE_MAXIMUM (uintmax_t)
536 #endif
538 #ifndef OFF_T_MIN
539 # define OFF_T_MIN TYPE_MINIMUM (off_t)
540 #endif
542 #ifndef OFF_T_MAX
543 # define OFF_T_MAX TYPE_MAXIMUM (off_t)
544 #endif
546 #ifndef UID_T_MAX
547 # define UID_T_MAX TYPE_MAXIMUM (uid_t)
548 #endif
550 #ifndef GID_T_MAX
551 # define GID_T_MAX TYPE_MAXIMUM (gid_t)
552 #endif
554 #ifndef PID_T_MAX
555 # define PID_T_MAX TYPE_MAXIMUM (pid_t)
556 #endif
558 #ifndef CHAR_BIT
559 # define CHAR_BIT 8
560 #endif
562 /* Use this to suppress gcc's `...may be used before initialized' warnings. */
563 #ifdef lint
564 # define IF_LINT(Code) Code
565 #else
566 # define IF_LINT(Code) /* empty */
567 #endif
569 #ifndef __attribute__
570 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
571 # define __attribute__(x)
572 # endif
573 #endif
575 #ifndef ATTRIBUTE_NORETURN
576 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
577 #endif
579 #ifndef ATTRIBUTE_UNUSED
580 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
581 #endif
583 #if defined strdupa
584 # define ASSIGN_STRDUPA(DEST, S) \
585 do { DEST = strdupa(S); } while (0)
586 #else
587 # define ASSIGN_STRDUPA(DEST, S) \
588 do \
590 const char *s_ = (S); \
591 size_t len_ = strlen (s_) + 1; \
592 char *tmp_dest_ = (char *) alloca (len_); \
593 DEST = memcpy (tmp_dest_, (s_), len_); \
595 while (0)
596 #endif