*** empty log message ***
[coreutils.git] / src / sys2.h
blob48af648c3003c8d1da8095af0bdb0d3dfc0785ae
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 # if HAVE_GETTEXT && !HAVE_DCGETTEXT && !defined dcgettext
268 # define dcgettext(Domain, Text, Category) Text
269 # endif
270 # define _(Text) gettext (Text)
271 #else
272 # undef bindtextdomain
273 # define bindtextdomain(Domain, Directory) /* empty */
274 # undef textdomain
275 # define textdomain(Domain) /* empty */
276 # undef dcgettext
277 # define dcgettext(Domainname, Text, Category) Text
278 # define _(Text) Text
279 #endif
280 #define N_(Text) Text
282 #define STREQ(a, b) (strcmp ((a), (b)) == 0)
284 #if !HAVE_DECL_FREE
285 void free ();
286 #endif
288 #if !HAVE_DECL_MALLOC
289 char *malloc ();
290 #endif
292 #if !HAVE_DECL_MEMCHR
293 char *memchr ();
294 #endif
296 #if !HAVE_DECL_REALLOC
297 char *realloc ();
298 #endif
300 #if !HAVE_DECL_STPCPY
301 # ifndef stpcpy
302 char *stpcpy ();
303 # endif
304 #endif
306 #if !HAVE_DECL_STRNDUP
307 char *strndup ();
308 #endif
310 #if !HAVE_DECL_STRSTR
311 char *strstr ();
312 #endif
314 #if !HAVE_DECL_GETENV
315 char *getenv ();
316 #endif
318 #if !HAVE_DECL_LSEEK
319 off_t lseek ();
320 #endif
322 /* This is needed on some AIX systems. */
323 #if !HAVE_DECL_STRTOUL
324 unsigned long strtoul ();
325 #endif
327 /* This is needed on some AIX systems. */
328 #if !HAVE_DECL_STRTOULL && HAVE_UNSIGNED_LONG_LONG
329 unsigned long long strtoull ();
330 #endif
332 #if !HAVE_DECL_GETLOGIN
333 char *getlogin ();
334 #endif
336 #if !HAVE_DECL_TTYNAME
337 char *ttyname ();
338 #endif
340 #if !HAVE_DECL_GETEUID
341 uid_t geteuid ();
342 #endif
344 #if !HAVE_DECL_GETPWUID
345 struct passwd *getpwuid ();
346 #endif
348 #if !HAVE_DECL_GETGRGID
349 struct group *getgrgid ();
350 #endif
352 #if !HAVE_DECL_GETUID
353 uid_t getuid ();
354 #endif
356 #include "xalloc.h"
358 #if ! defined HAVE_MEMPCPY && ! defined mempcpy
359 /* Be CAREFUL that there are no side effects in N. */
360 # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
361 #endif
363 /* These are wrappers for functions/macros from GNU libc.
364 The standard I/O functions are thread-safe. These *_unlocked ones
365 are more efficient but not thread-safe. That they're not thread-safe
366 is fine since all these applications are single threaded. */
368 #if HAVE_CLEARERR_UNLOCKED
369 # undef clearerr
370 # define clearerr(S) clearerr_unlocked (S)
371 #endif
373 #if HAVE_FEOF_UNLOCKED
374 # undef feof
375 # define feof(S) feof_unlocked (S)
376 #endif
378 #if HAVE_FERROR_UNLOCKED
379 # undef ferror
380 # define ferror(S) ferror_unlocked (S)
381 #endif
383 #if HAVE_FFLUSH_UNLOCKED
384 # undef fflush
385 # define fflush(S) fflush_unlocked (S)
386 #endif
388 #if HAVE_FPUTC_UNLOCKED
389 # undef fputc
390 # define fputc(C, S) fputc_unlocked (C, S)
391 #endif
393 #if HAVE_FREAD_UNLOCKED
394 # undef fread
395 # define fread(P, Z, N, S) fread_unlocked (P, Z, N, S)
396 #endif
398 #if HAVE_FWRITE_UNLOCKED
399 # undef fwrite
400 # define fwrite(P, Z, N, S) fwrite_unlocked (P, Z, N, S)
401 #endif
403 #if HAVE_GETC_UNLOCKED
404 # undef getc
405 # define getc(S) getc_unlocked (S)
406 #endif
408 #if HAVE_GETCHAR_UNLOCKED
409 # undef getchar
410 # define getchar(S) getchar_unlocked (S)
411 #endif
413 #if HAVE_PUTC_UNLOCKED
414 # undef putc
415 # define putc(C, S) putc_unlocked (C, S)
416 #endif
418 #if HAVE_PUTCHAR_UNLOCKED
419 # undef putchar
420 # define putchar(C) putchar_unlocked (C)
421 #endif
423 #define SAME_INODE(Stat_buf_1, Stat_buf_2) \
424 ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
425 && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
427 #define DOT_OR_DOTDOT(Basename) \
428 (Basename[0] == '.' && (Basename[1] == '\0' \
429 || (Basename[1] == '.' && Basename[2] == '\0')))
431 #if SETVBUF_REVERSED
432 # define SETVBUF(Stream, Buffer, Type, Size) \
433 setvbuf (Stream, Type, Buffer, Size)
434 #else
435 # define SETVBUF(Stream, Buffer, Type, Size) \
436 setvbuf (Stream, Buffer, Type, Size)
437 #endif
439 char *base_name PARAMS ((char const *));
441 /* Factor out some of the common --help and --version processing code. */
443 /* These enum values cannot possibly conflict with the option values
444 ordinarily used by commands, including CHAR_MAX + 1, etc. Avoid
445 CHAR_MIN - 1, as it may equal -1, the getopt end-of-options value. */
446 enum
448 GETOPT_HELP_CHAR = (CHAR_MIN - 2),
449 GETOPT_VERSION_CHAR = (CHAR_MIN - 3)
452 #define GETOPT_HELP_OPTION_DECL \
453 "help", no_argument, 0, GETOPT_HELP_CHAR
454 #define GETOPT_VERSION_OPTION_DECL \
455 "version", no_argument, 0, GETOPT_VERSION_CHAR
457 #define case_GETOPT_HELP_CHAR \
458 case GETOPT_HELP_CHAR: \
459 usage (EXIT_SUCCESS); \
460 break;
462 #include "closeout.h"
463 #include "version-etc.h"
465 #define case_GETOPT_VERSION_CHAR(Program_name, Authors) \
466 case GETOPT_VERSION_CHAR: \
467 version_etc (stdout, Program_name, PACKAGE, VERSION, Authors); \
468 exit (EXIT_SUCCESS); \
469 break;
471 #ifndef MAX
472 # define MAX(a, b) ((a) > (b) ? (a) : (b))
473 #endif
475 #ifndef MIN
476 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
477 #endif
479 #ifndef CHAR_BIT
480 # define CHAR_BIT 8
481 #endif
483 /* The extra casts work around common compiler bugs. */
484 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
485 /* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
486 It is necessary at least when t == time_t. */
487 #define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
488 ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) : (t) 0))
489 #define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
491 /* Upper bound on the string length of an integer converted to string.
492 302 / 1000 is ceil (log10 (2.0)). Subtract 1 for the sign bit;
493 add 1 for integer division truncation; add 1 more for a minus sign. */
494 #define INT_STRLEN_BOUND(t) ((sizeof (t) * CHAR_BIT - 1) * 302 / 1000 + 2)
496 #ifndef CHAR_MIN
497 # define CHAR_MIN TYPE_MINIMUM (char)
498 #endif
500 #ifndef CHAR_MAX
501 # define CHAR_MAX TYPE_MAXIMUM (char)
502 #endif
504 #ifndef SCHAR_MIN
505 # define SCHAR_MIN (-1 - SCHAR_MAX)
506 #endif
508 #ifndef SCHAR_MAX
509 # define SCHAR_MAX (CHAR_MAX == UCHAR_MAX ? CHAR_MAX / 2 : CHAR_MAX)
510 #endif
512 #ifndef UCHAR_MAX
513 # define UCHAR_MAX TYPE_MAXIMUM (unsigned char)
514 #endif
516 #ifndef SHRT_MIN
517 # define SHRT_MIN TYPE_MINIMUM (short int)
518 #endif
520 #ifndef SHRT_MAX
521 # define SHRT_MAX TYPE_MAXIMUM (short int)
522 #endif
524 #ifndef INT_MAX
525 # define INT_MAX TYPE_MAXIMUM (int)
526 #endif
528 #ifndef UINT_MAX
529 # define UINT_MAX TYPE_MAXIMUM (unsigned int)
530 #endif
532 #ifndef LONG_MAX
533 # define LONG_MAX TYPE_MAXIMUM (long)
534 #endif
536 #ifndef ULONG_MAX
537 # define ULONG_MAX TYPE_MAXIMUM (unsigned long)
538 #endif
540 #ifndef SIZE_MAX
541 # define SIZE_MAX TYPE_MAXIMUM (size_t)
542 #endif
544 #ifndef UINTMAX_MAX
545 # define UINTMAX_MAX TYPE_MAXIMUM (uintmax_t)
546 #endif
548 #ifndef OFF_T_MIN
549 # define OFF_T_MIN TYPE_MINIMUM (off_t)
550 #endif
552 #ifndef OFF_T_MAX
553 # define OFF_T_MAX TYPE_MAXIMUM (off_t)
554 #endif
556 #ifndef UID_T_MAX
557 # define UID_T_MAX TYPE_MAXIMUM (uid_t)
558 #endif
560 #ifndef GID_T_MAX
561 # define GID_T_MAX TYPE_MAXIMUM (gid_t)
562 #endif
564 #ifndef PID_T_MAX
565 # define PID_T_MAX TYPE_MAXIMUM (pid_t)
566 #endif
568 #ifndef CHAR_BIT
569 # define CHAR_BIT 8
570 #endif
572 /* Use this to suppress gcc's `...may be used before initialized' warnings. */
573 #ifdef lint
574 # define IF_LINT(Code) Code
575 #else
576 # define IF_LINT(Code) /* empty */
577 #endif
579 #ifndef __attribute__
580 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
581 # define __attribute__(x)
582 # endif
583 #endif
585 #ifndef ATTRIBUTE_NORETURN
586 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
587 #endif
589 #ifndef ATTRIBUTE_UNUSED
590 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
591 #endif
593 #if defined strdupa
594 # define ASSIGN_STRDUPA(DEST, S) \
595 do { DEST = strdupa(S); } while (0)
596 #else
597 # define ASSIGN_STRDUPA(DEST, S) \
598 do \
600 const char *s_ = (S); \
601 size_t len_ = strlen (s_) + 1; \
602 char *tmp_dest_ = (char *) alloca (len_); \
603 DEST = memcpy (tmp_dest_, (s_), len_); \
605 while (0)
606 #endif