(invalid-j): New partial test for the above fix.
[coreutils.git] / src / system.h
blob6154843d1daec98d2aaba4994db68aeef703b398
1 /* system-dependent definitions for fileutils, textutils, and sh-utils packages.
2 Copyright (C) 1989, 1991-2004 Free Software Foundation, Inc.
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 #include <alloca.h>
20 /* Include sys/types.h before this file. */
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
28 #include <sys/stat.h>
30 #if !defined HAVE_MKFIFO
31 # define mkfifo(path, mode) (mknod ((path), (mode) | S_IFIFO, 0))
32 #endif
34 #if HAVE_SYS_PARAM_H
35 # include <sys/param.h>
36 #endif
38 /* <unistd.h> should be included before any preprocessor test
39 of _POSIX_VERSION. */
40 #if HAVE_UNISTD_H
41 # include <unistd.h>
42 #endif
44 #ifndef STDIN_FILENO
45 # define STDIN_FILENO 0
46 #endif
48 #ifndef STDOUT_FILENO
49 # define STDOUT_FILENO 1
50 #endif
52 #ifndef STDERR_FILENO
53 # define STDERR_FILENO 2
54 #endif
57 /* limits.h must come before pathmax.h because limits.h on some systems
58 undefs PATH_MAX, whereas pathmax.h sets PATH_MAX. */
59 #include <limits.h>
61 #include "pathmax.h"
62 #include "localedir.h"
64 #if TIME_WITH_SYS_TIME
65 # include <sys/time.h>
66 # include <time.h>
67 #else
68 # if HAVE_SYS_TIME_H
69 # include <sys/time.h>
70 # else
71 # include <time.h>
72 # endif
73 #endif
75 /* Since major is a function on SVR4, we can't use `ifndef major'. */
76 #if MAJOR_IN_MKDEV
77 # include <sys/mkdev.h>
78 # define HAVE_MAJOR
79 #endif
80 #if MAJOR_IN_SYSMACROS
81 # include <sys/sysmacros.h>
82 # define HAVE_MAJOR
83 #endif
84 #ifdef major /* Might be defined in sys/types.h. */
85 # define HAVE_MAJOR
86 #endif
88 #ifndef HAVE_MAJOR
89 # define major(dev) (((dev) >> 8) & 0xff)
90 # define minor(dev) ((dev) & 0xff)
91 # define makedev(maj, min) (((maj) << 8) | (min))
92 #endif
93 #undef HAVE_MAJOR
95 #if HAVE_UTIME_H
96 # include <utime.h>
97 #endif
99 /* Some systems (even some that do have <utime.h>) don't declare this
100 structure anywhere. */
101 #ifndef HAVE_STRUCT_UTIMBUF
102 struct utimbuf
104 long actime;
105 long modtime;
107 #endif
109 /* Don't use bcopy! Use memmove if source and destination may overlap,
110 memcpy otherwise. */
112 #include <string.h>
113 #if ! HAVE_DECL_MEMRCHR
114 void *memrchr (const void *, int, size_t);
115 #endif
117 #include <errno.h>
118 #ifndef errno
119 extern int errno;
120 #endif
122 /* Some systems don't define the following symbols. */
123 #ifndef ENOSYS
124 # define ENOSYS (-1)
125 #endif
126 #ifndef ENOTSUP
127 # define ENOTSUP (-1)
128 #endif
129 #ifndef EISDIR
130 # define EISDIR (-1)
131 #endif
133 #include <stdbool.h>
135 #define getopt system_getopt
136 #include <stdlib.h>
137 #undef getopt
139 /* The following test is to work around the gross typo in
140 systems like Sony NEWS-OS Release 4.0C, whereby EXIT_FAILURE
141 is defined to 0, not 1. */
142 #if !EXIT_FAILURE
143 # undef EXIT_FAILURE
144 # define EXIT_FAILURE 1
145 #endif
147 #ifndef EXIT_SUCCESS
148 # define EXIT_SUCCESS 0
149 #endif
151 /* Exit statuses for programs like 'env' that exec other programs.
152 EXIT_FAILURE might not be 1, so use EXIT_FAIL in such programs. */
153 enum
155 EXIT_FAIL = 1,
156 EXIT_CANNOT_INVOKE = 126,
157 EXIT_ENOENT = 127
160 #include "exitfail.h"
162 /* Set exit_failure to STATUS if that's not the default already. */
163 static inline void
164 initialize_exit_failure (int status)
166 if (status != EXIT_FAILURE)
167 exit_failure = status;
170 #if HAVE_FCNTL_H
171 # include <fcntl.h>
172 #else
173 # include <sys/file.h>
174 #endif
176 #if !defined SEEK_SET
177 # define SEEK_SET 0
178 # define SEEK_CUR 1
179 # define SEEK_END 2
180 #endif
181 #ifndef F_OK
182 # define F_OK 0
183 # define X_OK 1
184 # define W_OK 2
185 # define R_OK 4
186 #endif
188 /* For systems that distinguish between text and binary I/O.
189 O_BINARY is usually declared in fcntl.h */
190 #if !defined O_BINARY && defined _O_BINARY
191 /* For MSC-compatible compilers. */
192 # define O_BINARY _O_BINARY
193 # define O_TEXT _O_TEXT
194 #endif
196 #if !defined O_NDELAY
197 # define O_NDELAY 0
198 #endif
200 #if !defined O_NONBLOCK
201 # define O_NONBLOCK O_NDELAY
202 #endif
204 #if !defined O_NOCTTY
205 # define O_NOCTTY 0
206 #endif
208 #ifdef __BEOS__
209 /* BeOS 5 has O_BINARY and O_TEXT, but they have no effect. */
210 # undef O_BINARY
211 # undef O_TEXT
212 #endif
214 #if O_BINARY
215 # ifndef __DJGPP__
216 # define setmode _setmode
217 # define fileno(_fp) _fileno (_fp)
218 # endif /* not DJGPP */
219 # define SET_MODE(_f, _m) setmode (_f, _m)
220 # define SET_BINARY(_f) do {if (!isatty(_f)) setmode (_f, O_BINARY);} while (0)
221 # define SET_BINARY2(_f1, _f2) \
222 do { \
223 if (!isatty (_f1)) \
225 setmode (_f1, O_BINARY); \
226 if (!isatty (_f2)) \
227 setmode (_f2, O_BINARY); \
229 } while(0)
230 #else
231 # define SET_MODE(_f, _m) (void)0
232 # define SET_BINARY(f) (void)0
233 # define SET_BINARY2(f1,f2) (void)0
234 # define O_BINARY 0
235 # define O_TEXT 0
236 #endif /* O_BINARY */
238 #if HAVE_DIRENT_H
239 # include <dirent.h>
240 # define NLENGTH(direct) (strlen((direct)->d_name))
241 #else /* not HAVE_DIRENT_H */
242 # define dirent direct
243 # define NLENGTH(direct) ((direct)->d_namlen)
244 # if HAVE_SYS_NDIR_H
245 # include <sys/ndir.h>
246 # endif /* HAVE_SYS_NDIR_H */
247 # if HAVE_SYS_DIR_H
248 # include <sys/dir.h>
249 # endif /* HAVE_SYS_DIR_H */
250 # if HAVE_NDIR_H
251 # include <ndir.h>
252 # endif /* HAVE_NDIR_H */
253 #endif /* HAVE_DIRENT_H */
255 #if CLOSEDIR_VOID
256 /* Fake a return value. */
257 # define CLOSEDIR(d) (closedir (d), 0)
258 #else
259 # define CLOSEDIR(d) closedir (d)
260 #endif
262 /* Get or fake the disk device blocksize.
263 Usually defined by sys/param.h (if at all). */
264 #if !defined DEV_BSIZE && defined BSIZE
265 # define DEV_BSIZE BSIZE
266 #endif
267 #if !defined DEV_BSIZE && defined BBSIZE /* SGI */
268 # define DEV_BSIZE BBSIZE
269 #endif
270 #ifndef DEV_BSIZE
271 # define DEV_BSIZE 4096
272 #endif
274 /* Extract or fake data from a `struct stat'.
275 ST_BLKSIZE: Preferred I/O blocksize for the file, in bytes.
276 ST_NBLOCKS: Number of blocks in the file, including indirect blocks.
277 ST_NBLOCKSIZE: Size of blocks used when calculating ST_NBLOCKS. */
278 #ifndef HAVE_STRUCT_STAT_ST_BLOCKS
279 # define ST_BLKSIZE(statbuf) DEV_BSIZE
280 # if defined _POSIX_SOURCE || !defined BSIZE /* fileblocks.c uses BSIZE. */
281 # define ST_NBLOCKS(statbuf) \
282 ((statbuf).st_size / ST_NBLOCKSIZE + ((statbuf).st_size % ST_NBLOCKSIZE != 0))
283 # else /* !_POSIX_SOURCE && BSIZE */
284 # define ST_NBLOCKS(statbuf) \
285 (S_ISREG ((statbuf).st_mode) \
286 || S_ISDIR ((statbuf).st_mode) \
287 ? st_blocks ((statbuf).st_size) : 0)
288 # endif /* !_POSIX_SOURCE && BSIZE */
289 #else /* HAVE_STRUCT_STAT_ST_BLOCKS */
290 /* Some systems, like Sequents, return st_blksize of 0 on pipes.
291 Also, when running `rsh hpux11-system cat any-file', cat would
292 determine that the output stream had an st_blksize of 2147421096.
293 So here we arbitrarily limit the `optimal' block size to 4MB.
294 If anyone knows of a system for which the legitimate value for
295 st_blksize can exceed 4MB, please report it as a bug in this code. */
296 # define ST_BLKSIZE(statbuf) ((0 < (statbuf).st_blksize \
297 && (statbuf).st_blksize <= (1 << 22)) /* 4MB */ \
298 ? (statbuf).st_blksize : DEV_BSIZE)
299 # if defined hpux || defined __hpux__ || defined __hpux
300 /* HP-UX counts st_blocks in 1024-byte units.
301 This loses when mixing HP-UX and BSD filesystems with NFS. */
302 # define ST_NBLOCKSIZE 1024
303 # else /* !hpux */
304 # if defined _AIX && defined _I386
305 /* AIX PS/2 counts st_blocks in 4K units. */
306 # define ST_NBLOCKSIZE (4 * 1024)
307 # else /* not AIX PS/2 */
308 # if defined _CRAY
309 # define ST_NBLOCKS(statbuf) \
310 (S_ISREG ((statbuf).st_mode) \
311 || S_ISDIR ((statbuf).st_mode) \
312 ? (statbuf).st_blocks * ST_BLKSIZE(statbuf)/ST_NBLOCKSIZE : 0)
313 # endif /* _CRAY */
314 # endif /* not AIX PS/2 */
315 # endif /* !hpux */
316 #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
318 #ifndef ST_NBLOCKS
319 # define ST_NBLOCKS(statbuf) ((statbuf).st_blocks)
320 #endif
322 #ifndef ST_NBLOCKSIZE
323 # define ST_NBLOCKSIZE 512
324 #endif
326 /* Redirection and wildcarding when done by the utility itself.
327 Generally a noop, but used in particular for native VMS. */
328 #ifndef initialize_main
329 # define initialize_main(ac, av)
330 #endif
332 #ifndef S_IFMT
333 # define S_IFMT 0170000
334 #endif
336 #if STAT_MACROS_BROKEN
337 # undef S_ISBLK
338 # undef S_ISCHR
339 # undef S_ISDIR
340 # undef S_ISDOOR
341 # undef S_ISFIFO
342 # undef S_ISLNK
343 # undef S_ISNAM
344 # undef S_ISMPB
345 # undef S_ISMPC
346 # undef S_ISNWK
347 # undef S_ISREG
348 # undef S_ISSOCK
349 #endif
352 #ifndef S_ISBLK
353 # ifdef S_IFBLK
354 # define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
355 # else
356 # define S_ISBLK(m) 0
357 # endif
358 #endif
360 #ifndef S_ISCHR
361 # ifdef S_IFCHR
362 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
363 # else
364 # define S_ISCHR(m) 0
365 # endif
366 #endif
368 #ifndef S_ISDIR
369 # ifdef S_IFDIR
370 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
371 # else
372 # define S_ISDIR(m) 0
373 # endif
374 #endif
376 #ifndef S_ISDOOR /* Solaris 2.5 and up */
377 # ifdef S_IFDOOR
378 # define S_ISDOOR(m) (((m) & S_IFMT) == S_IFDOOR)
379 # else
380 # define S_ISDOOR(m) 0
381 # endif
382 #endif
384 #ifndef S_ISFIFO
385 # ifdef S_IFIFO
386 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
387 # else
388 # define S_ISFIFO(m) 0
389 # endif
390 #endif
392 #ifndef S_ISLNK
393 # ifdef S_IFLNK
394 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
395 # else
396 # define S_ISLNK(m) 0
397 # endif
398 #endif
400 #ifndef S_ISMPB /* V7 */
401 # ifdef S_IFMPB
402 # define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
403 # define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
404 # else
405 # define S_ISMPB(m) 0
406 # define S_ISMPC(m) 0
407 # endif
408 #endif
410 #ifndef S_ISNAM /* Xenix */
411 # ifdef S_IFNAM
412 # define S_ISNAM(m) (((m) & S_IFMT) == S_IFNAM)
413 # else
414 # define S_ISNAM(m) 0
415 # endif
416 #endif
418 #ifndef S_ISNWK /* HP/UX */
419 # ifdef S_IFNWK
420 # define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
421 # else
422 # define S_ISNWK(m) 0
423 # endif
424 #endif
426 #ifndef S_ISREG
427 # ifdef S_IFREG
428 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
429 # else
430 # define S_ISREG(m) 0
431 # endif
432 #endif
434 #ifndef S_ISSOCK
435 # ifdef S_IFSOCK
436 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
437 # else
438 # define S_ISSOCK(m) 0
439 # endif
440 #endif
443 #ifndef S_TYPEISSEM
444 # ifdef S_INSEM
445 # define S_TYPEISSEM(p) (S_ISNAM ((p)->st_mode) && (p)->st_rdev == S_INSEM)
446 # else
447 # define S_TYPEISSEM(p) 0
448 # endif
449 #endif
451 #ifndef S_TYPEISSHM
452 # ifdef S_INSHD
453 # define S_TYPEISSHM(p) (S_ISNAM ((p)->st_mode) && (p)->st_rdev == S_INSHD)
454 # else
455 # define S_TYPEISSHM(p) 0
456 # endif
457 #endif
459 #ifndef S_TYPEISMQ
460 # define S_TYPEISMQ(p) 0
461 #endif
464 /* If any of the following are undefined,
465 define them to their de facto standard values. */
466 #if !S_ISUID
467 # define S_ISUID 04000
468 #endif
469 #if !S_ISGID
470 # define S_ISGID 02000
471 #endif
473 /* S_ISVTX is a common extension to POSIX. */
474 #ifndef S_ISVTX
475 # define S_ISVTX 01000
476 #endif
478 #if !S_IRUSR && S_IREAD
479 # define S_IRUSR S_IREAD
480 #endif
481 #if !S_IRUSR
482 # define S_IRUSR 00400
483 #endif
484 #if !S_IRGRP
485 # define S_IRGRP (S_IRUSR >> 3)
486 #endif
487 #if !S_IROTH
488 # define S_IROTH (S_IRUSR >> 6)
489 #endif
491 #if !S_IWUSR && S_IWRITE
492 # define S_IWUSR S_IWRITE
493 #endif
494 #if !S_IWUSR
495 # define S_IWUSR 00200
496 #endif
497 #if !S_IWGRP
498 # define S_IWGRP (S_IWUSR >> 3)
499 #endif
500 #if !S_IWOTH
501 # define S_IWOTH (S_IWUSR >> 6)
502 #endif
504 #if !S_IXUSR && S_IEXEC
505 # define S_IXUSR S_IEXEC
506 #endif
507 #if !S_IXUSR
508 # define S_IXUSR 00100
509 #endif
510 #if !S_IXGRP
511 # define S_IXGRP (S_IXUSR >> 3)
512 #endif
513 #if !S_IXOTH
514 # define S_IXOTH (S_IXUSR >> 6)
515 #endif
517 #if !S_IRWXU
518 # define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
519 #endif
520 #if !S_IRWXG
521 # define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
522 #endif
523 #if !S_IRWXO
524 # define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
525 #endif
527 /* S_IXUGO is a common extension to POSIX. */
528 #if !S_IXUGO
529 # define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
530 #endif
532 #ifndef S_IRWXUGO
533 # define S_IRWXUGO (S_IRWXU | S_IRWXG | S_IRWXO)
534 #endif
536 /* All the mode bits that can be affected by chmod. */
537 #define CHMOD_MODE_BITS \
538 (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
540 #include "timespec.h"
542 #ifndef RETSIGTYPE
543 # define RETSIGTYPE void
544 #endif
546 #ifdef __DJGPP__
547 /* We need the declaration of setmode. */
548 # include <io.h>
549 /* We need the declaration of __djgpp_set_ctrl_c. */
550 # include <sys/exceptn.h>
551 #endif
553 #if HAVE_STDINT_H
554 # include <stdint.h>
555 #endif
557 #if HAVE_INTTYPES_H
558 # include <inttypes.h> /* for the definition of UINTMAX_MAX */
559 #endif
561 #if !defined PRIdMAX || PRI_MACROS_BROKEN
562 # undef PRIdMAX
563 # define PRIdMAX (sizeof (uintmax_t) == sizeof (long) ? "ld" : "lld")
564 #endif
565 #if !defined PRIoMAX || PRI_MACROS_BROKEN
566 # undef PRIoMAX
567 # define PRIoMAX (sizeof (uintmax_t) == sizeof (long) ? "lo" : "llo")
568 #endif
569 #if !defined PRIuMAX || PRI_MACROS_BROKEN
570 # undef PRIuMAX
571 # define PRIuMAX (sizeof (uintmax_t) == sizeof (long) ? "lu" : "llu")
572 #endif
573 #if !defined PRIxMAX || PRI_MACROS_BROKEN
574 # undef PRIxMAX
575 # define PRIxMAX (sizeof (uintmax_t) == sizeof (long) ? "lx" : "llx")
576 #endif
578 #include <ctype.h>
580 /* Jim Meyering writes:
582 "... Some ctype macros are valid only for character codes that
583 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
584 using /bin/cc or gcc but without giving an ansi option). So, all
585 ctype uses should be through macros like ISPRINT... If
586 STDC_HEADERS is defined, then autoconf has verified that the ctype
587 macros don't need to be guarded with references to isascii. ...
588 Defining isascii to 1 should let any compiler worth its salt
589 eliminate the && through constant folding."
591 Bruno Haible adds:
593 "... Furthermore, isupper(c) etc. have an undefined result if c is
594 outside the range -1 <= c <= 255. One is tempted to write isupper(c)
595 with c being of type `char', but this is wrong if c is an 8-bit
596 character >= 128 which gets sign-extended to a negative value.
597 The macro ISUPPER protects against this as well." */
599 #if STDC_HEADERS || (!defined (isascii) && !HAVE_ISASCII)
600 # define IN_CTYPE_DOMAIN(c) 1
601 #else
602 # define IN_CTYPE_DOMAIN(c) isascii(c)
603 #endif
605 #ifdef isblank
606 # define ISBLANK(c) (IN_CTYPE_DOMAIN (c) && isblank (c))
607 #else
608 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
609 #endif
610 #ifdef isgraph
611 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isgraph (c))
612 #else
613 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isprint (c) && !isspace (c))
614 #endif
616 /* This is defined in <sys/euc.h> on at least Solaris2.6 systems. */
617 #undef ISPRINT
619 #define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
620 #define ISALNUM(c) (IN_CTYPE_DOMAIN (c) && isalnum (c))
621 #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
622 #define ISCNTRL(c) (IN_CTYPE_DOMAIN (c) && iscntrl (c))
623 #define ISLOWER(c) (IN_CTYPE_DOMAIN (c) && islower (c))
624 #define ISPUNCT(c) (IN_CTYPE_DOMAIN (c) && ispunct (c))
625 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
626 #define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
627 #define ISXDIGIT(c) (IN_CTYPE_DOMAIN (c) && isxdigit (c))
628 #define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
630 #if STDC_HEADERS
631 # define TOLOWER(Ch) tolower (Ch)
632 # define TOUPPER(Ch) toupper (Ch)
633 #else
634 # define TOLOWER(Ch) (ISUPPER (Ch) ? tolower (Ch) : (Ch))
635 # define TOUPPER(Ch) (ISLOWER (Ch) ? toupper (Ch) : (Ch))
636 #endif
638 /* ISDIGIT differs from ISDIGIT_LOCALE, as follows:
639 - Its arg may be any int or unsigned int; it need not be an unsigned char.
640 - It's guaranteed to evaluate its argument exactly once.
641 - It's typically faster.
642 POSIX says that only '0' through '9' are digits. Prefer ISDIGIT to
643 ISDIGIT_LOCALE unless it's important to use the locale's definition
644 of `digit' even when the host does not conform to POSIX. */
645 #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
647 /* Take care of NLS matters. */
649 #if HAVE_LOCALE_H
650 # include <locale.h>
651 #else
652 # define setlocale(Category, Locale) /* empty */
653 #endif
655 #include "gettext.h"
656 #if ! ENABLE_NLS
657 # undef textdomain
658 # define textdomain(Domainname) /* empty */
659 # undef bindtextdomain
660 # define bindtextdomain(Domainname, Dirname) /* empty */
661 #endif
663 #define _(msgid) gettext (msgid)
664 #define N_(msgid) msgid
666 #ifndef HAVE_SETLOCALE
667 # define HAVE_SETLOCALE 0
668 #endif
670 #define STREQ(a, b) (strcmp ((a), (b)) == 0)
672 #if !HAVE_DECL_FREE
673 void free ();
674 #endif
676 #if !HAVE_DECL_MALLOC
677 char *malloc ();
678 #endif
680 #if !HAVE_DECL_MEMCHR
681 char *memchr ();
682 #endif
684 #if !HAVE_DECL_REALLOC
685 char *realloc ();
686 #endif
688 #if !HAVE_DECL_STPCPY
689 # ifndef stpcpy
690 char *stpcpy ();
691 # endif
692 #endif
694 #if !HAVE_DECL_STRNDUP
695 char *strndup ();
696 #endif
698 #if !HAVE_DECL_STRSTR
699 char *strstr ();
700 #endif
702 #if !HAVE_DECL_GETENV
703 char *getenv ();
704 #endif
706 #if !HAVE_DECL_LSEEK
707 off_t lseek ();
708 #endif
710 /* This is needed on some AIX systems. */
711 #if !HAVE_DECL_STRTOUL
712 unsigned long strtoul ();
713 #endif
715 #if !HAVE_DECL_GETLOGIN
716 char *getlogin ();
717 #endif
719 #if !HAVE_DECL_TTYNAME
720 char *ttyname ();
721 #endif
723 #if !HAVE_DECL_GETEUID
724 uid_t geteuid ();
725 #endif
727 #if !HAVE_DECL_GETPWUID
728 struct passwd *getpwuid ();
729 #endif
731 #if !HAVE_DECL_GETGRGID
732 struct group *getgrgid ();
733 #endif
735 #if !HAVE_DECL_GETUID
736 uid_t getuid ();
737 #endif
739 #include "xalloc.h"
741 #if ! defined HAVE_MEMPCPY && ! defined mempcpy
742 /* Be CAREFUL that there are no side effects in N. */
743 # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
744 #endif
746 /* Include automatically-generated macros for unlocked I/O. */
747 #include "unlocked-io.h"
749 #define SAME_INODE(Stat_buf_1, Stat_buf_2) \
750 ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
751 && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
753 #define DOT_OR_DOTDOT(Basename) \
754 (Basename[0] == '.' && (Basename[1] == '\0' \
755 || (Basename[1] == '.' && Basename[2] == '\0')))
757 #if SETVBUF_REVERSED
758 # define SETVBUF(Stream, Buffer, Type, Size) \
759 setvbuf (Stream, Type, Buffer, Size)
760 #else
761 # define SETVBUF(Stream, Buffer, Type, Size) \
762 setvbuf (Stream, Buffer, Type, Size)
763 #endif
765 /* Factor out some of the common --help and --version processing code. */
767 /* These enum values cannot possibly conflict with the option values
768 ordinarily used by commands, including CHAR_MAX + 1, etc. Avoid
769 CHAR_MIN - 1, as it may equal -1, the getopt end-of-options value. */
770 enum
772 GETOPT_HELP_CHAR = (CHAR_MIN - 2),
773 GETOPT_VERSION_CHAR = (CHAR_MIN - 3)
776 #define GETOPT_HELP_OPTION_DECL \
777 "help", no_argument, 0, GETOPT_HELP_CHAR
778 #define GETOPT_VERSION_OPTION_DECL \
779 "version", no_argument, 0, GETOPT_VERSION_CHAR
781 #define case_GETOPT_HELP_CHAR \
782 case GETOPT_HELP_CHAR: \
783 usage (EXIT_SUCCESS); \
784 break;
786 #define HELP_OPTION_DESCRIPTION \
787 _(" --help display this help and exit\n")
788 #define VERSION_OPTION_DESCRIPTION \
789 _(" --version output version information and exit\n")
791 #include "closeout.h"
792 #include "version-etc.h"
794 #define case_GETOPT_VERSION_CHAR(Program_name, Authors) \
795 case GETOPT_VERSION_CHAR: \
796 version_etc (stdout, Program_name, PACKAGE, VERSION, Authors, \
797 (char *) NULL); \
798 exit (EXIT_SUCCESS); \
799 break;
801 #ifndef MAX
802 # define MAX(a, b) ((a) > (b) ? (a) : (b))
803 #endif
805 #ifndef MIN
806 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
807 #endif
809 #ifndef CHAR_BIT
810 # define CHAR_BIT 8
811 #endif
813 /* The extra casts work around common compiler bugs. */
814 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
815 /* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
816 It is necessary at least when t == time_t. */
817 #define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
818 ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) : (t) 0))
819 #define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
821 /* Upper bound on the string length of an integer converted to string.
822 302 / 1000 is ceil (log10 (2.0)). Subtract 1 for the sign bit;
823 add 1 for integer division truncation; add 1 more for a minus sign. */
824 #define INT_STRLEN_BOUND(t) ((sizeof (t) * CHAR_BIT - 1) * 302 / 1000 + 2)
826 #ifndef CHAR_MIN
827 # define CHAR_MIN TYPE_MINIMUM (char)
828 #endif
830 #ifndef CHAR_MAX
831 # define CHAR_MAX TYPE_MAXIMUM (char)
832 #endif
834 #ifndef SCHAR_MIN
835 # define SCHAR_MIN (-1 - SCHAR_MAX)
836 #endif
838 #ifndef SCHAR_MAX
839 # define SCHAR_MAX (CHAR_MAX == UCHAR_MAX ? CHAR_MAX / 2 : CHAR_MAX)
840 #endif
842 #ifndef UCHAR_MAX
843 # define UCHAR_MAX TYPE_MAXIMUM (unsigned char)
844 #endif
846 #ifndef SHRT_MIN
847 # define SHRT_MIN TYPE_MINIMUM (short int)
848 #endif
850 #ifndef SHRT_MAX
851 # define SHRT_MAX TYPE_MAXIMUM (short int)
852 #endif
854 #ifndef INT_MAX
855 # define INT_MAX TYPE_MAXIMUM (int)
856 #endif
858 #ifndef INT_MIN
859 # define INT_MIN TYPE_MINIMUM (int)
860 #endif
862 #ifndef UINT_MAX
863 # define UINT_MAX TYPE_MAXIMUM (unsigned int)
864 #endif
866 #ifndef LONG_MAX
867 # define LONG_MAX TYPE_MAXIMUM (long)
868 #endif
870 #ifndef ULONG_MAX
871 # define ULONG_MAX TYPE_MAXIMUM (unsigned long)
872 #endif
874 #ifndef SIZE_MAX
875 # define SIZE_MAX TYPE_MAXIMUM (size_t)
876 #endif
878 #ifndef SSIZE_MAX
879 # define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
880 #endif
882 #ifndef UINTMAX_MAX
883 # define UINTMAX_MAX TYPE_MAXIMUM (uintmax_t)
884 #endif
886 #ifndef OFF_T_MIN
887 # define OFF_T_MIN TYPE_MINIMUM (off_t)
888 #endif
890 #ifndef OFF_T_MAX
891 # define OFF_T_MAX TYPE_MAXIMUM (off_t)
892 #endif
894 #ifndef UID_T_MAX
895 # define UID_T_MAX TYPE_MAXIMUM (uid_t)
896 #endif
898 #ifndef GID_T_MAX
899 # define GID_T_MAX TYPE_MAXIMUM (gid_t)
900 #endif
902 #ifndef PID_T_MAX
903 # define PID_T_MAX TYPE_MAXIMUM (pid_t)
904 #endif
906 /* Use this to suppress gcc's `...may be used before initialized' warnings. */
907 #ifdef lint
908 # define IF_LINT(Code) Code
909 #else
910 # define IF_LINT(Code) /* empty */
911 #endif
913 #ifndef __attribute__
914 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
915 # define __attribute__(x)
916 # endif
917 #endif
919 #ifndef ATTRIBUTE_NORETURN
920 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
921 #endif
923 #ifndef ATTRIBUTE_UNUSED
924 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
925 #endif
927 #if defined strdupa
928 # define ASSIGN_STRDUPA(DEST, S) \
929 do { DEST = strdupa (S); } while (0)
930 #else
931 # define ASSIGN_STRDUPA(DEST, S) \
932 do \
934 const char *s_ = (S); \
935 size_t len_ = strlen (s_) + 1; \
936 char *tmp_dest_ = alloca (len_); \
937 DEST = memcpy (tmp_dest_, (s_), len_); \
939 while (0)
940 #endif
942 #ifndef EOVERFLOW
943 # define EOVERFLOW EINVAL
944 #endif
946 #if ! HAVE_FSEEKO && ! defined fseeko
947 # define fseeko(s, o, w) ((o) == (long) (o) \
948 ? fseek (s, o, w) \
949 : (errno = EOVERFLOW, -1))
950 #endif