1/* system-dependent definitions for coreutils
2   Copyright (C) 1989, 1991-2005 Free Software Foundation, Inc.
3
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.
8
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.
13
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18#include <alloca.h>
19
20/* Include sys/types.h before this file.  */
21
22#if 2 <= __GLIBC__ && 2 <= __GLIBC_MINOR__
23# if ! defined _SYS_TYPES_H
24you must include <sys/types.h> before including this file
25# endif
26#endif
27
28#include <sys/stat.h>
29
30#if !defined HAVE_MKFIFO
31# define mkfifo(name, mode) mknod (name, (mode) | S_IFIFO, 0)
32#endif
33
34#if HAVE_SYS_PARAM_H
35# include <sys/param.h>
36#endif
37
38#include <unistd.h>
39
40#ifndef STDIN_FILENO
41# define STDIN_FILENO 0
42#endif
43
44#ifndef STDOUT_FILENO
45# define STDOUT_FILENO 1
46#endif
47
48#ifndef STDERR_FILENO
49# define STDERR_FILENO 2
50#endif
51
52
53/* limits.h must come before pathmax.h because limits.h on some systems
54   undefs PATH_MAX, whereas pathmax.h sets PATH_MAX.  */
55#include <limits.h>
56
57#include "pathmax.h"
58#include "localedir.h"
59
60#if TIME_WITH_SYS_TIME
61# include <sys/time.h>
62# include <time.h>
63#else
64# if HAVE_SYS_TIME_H
65#  include <sys/time.h>
66# else
67#  include <time.h>
68# endif
69#endif
70
71/* Since major is a function on SVR4, we can't use `ifndef major'.  */
72#if MAJOR_IN_MKDEV
73# include <sys/mkdev.h>
74# define HAVE_MAJOR
75#endif
76#if MAJOR_IN_SYSMACROS
77# include <sys/sysmacros.h>
78# define HAVE_MAJOR
79#endif
80#ifdef major			/* Might be defined in sys/types.h.  */
81# define HAVE_MAJOR
82#endif
83
84#ifndef HAVE_MAJOR
85# define major(dev)  (((dev) >> 8) & 0xff)
86# define minor(dev)  ((dev) & 0xff)
87# define makedev(maj, min)  (((maj) << 8) | (min))
88#endif
89#undef HAVE_MAJOR
90
91#if ! defined makedev && defined mkdev
92# define makedev(maj, min)  mkdev (maj, min)
93#endif
94
95/* Don't use bcopy!  Use memmove if source and destination may overlap,
96   memcpy otherwise.  */
97
98#include <string.h>
99#include "memrchr.h"
100
101#include <errno.h>
102
103/* Some systems don't define the following symbols.  */
104#ifndef ENOSYS
105# define ENOSYS (-1)
106#endif
107#ifndef EISDIR
108# define EISDIR (-1)
109#endif
110
111#include <stdbool.h>
112#include <stdlib.h>
113
114/* The following test is to work around the gross typo in
115   systems like Sony NEWS-OS Release 4.0C, whereby EXIT_FAILURE
116   is defined to 0, not 1.  */
117#if !EXIT_FAILURE
118# undef EXIT_FAILURE
119# define EXIT_FAILURE 1
120#endif
121
122#ifndef EXIT_SUCCESS
123# define EXIT_SUCCESS 0
124#endif
125
126/* Exit statuses for programs like 'env' that exec other programs.
127   EXIT_FAILURE might not be 1, so use EXIT_FAIL in such programs.  */
128enum
129{
130  EXIT_FAIL = 1,
131  EXIT_CANNOT_INVOKE = 126,
132  EXIT_ENOENT = 127
133};
134
135#include "exitfail.h"
136
137/* Set exit_failure to STATUS if that's not the default already.  */
138static inline void
139initialize_exit_failure (int status)
140{
141  if (status != EXIT_FAILURE)
142    exit_failure = status;
143}
144
145#include <fcntl.h>
146
147#if !defined SEEK_SET
148# define SEEK_SET 0
149# define SEEK_CUR 1
150# define SEEK_END 2
151#endif
152#ifndef F_OK
153# define F_OK 0
154# define X_OK 1
155# define W_OK 2
156# define R_OK 4
157#endif
158
159#if !defined O_DIRECT
160# define O_DIRECT 0
161#endif
162
163#if !defined O_DSYNC
164# define O_DSYNC 0
165#endif
166
167#if !defined O_NDELAY
168# define O_NDELAY 0
169#endif
170
171#if !defined O_NONBLOCK
172# define O_NONBLOCK O_NDELAY
173#endif
174
175#if !defined O_NOCTTY
176# define O_NOCTTY 0
177#endif
178
179#if !defined O_NOFOLLOW
180# define O_NOFOLLOW 0
181#endif
182
183#if !defined O_RSYNC
184# define O_RSYNC 0
185#endif
186
187#if !defined O_SYNC
188# define O_SYNC 0
189#endif
190
191/* For systems that distinguish between text and binary I/O.
192   O_BINARY is usually declared in fcntl.h  */
193#if !defined O_BINARY && defined _O_BINARY
194  /* For MSC-compatible compilers.  */
195# define O_BINARY _O_BINARY
196# define O_TEXT _O_TEXT
197#endif
198
199#ifdef __BEOS__
200  /* BeOS 5 has O_BINARY and O_TEXT, but they have no effect.  */
201# undef O_BINARY
202# undef O_TEXT
203#endif
204
205#ifndef O_BINARY
206# define O_BINARY 0
207# define O_TEXT 0
208#endif
209
210#if HAVE_DIRENT_H
211# include <dirent.h>
212# define NLENGTH(direct) (strlen((direct)->d_name))
213#else /* not HAVE_DIRENT_H */
214# define dirent direct
215# define NLENGTH(direct) ((direct)->d_namlen)
216# if HAVE_SYS_NDIR_H
217#  include <sys/ndir.h>
218# endif /* HAVE_SYS_NDIR_H */
219# if HAVE_SYS_DIR_H
220#  include <sys/dir.h>
221# endif /* HAVE_SYS_DIR_H */
222# if HAVE_NDIR_H
223#  include <ndir.h>
224# endif /* HAVE_NDIR_H */
225#endif /* HAVE_DIRENT_H */
226
227#if CLOSEDIR_VOID
228/* Fake a return value. */
229# define CLOSEDIR(d) (closedir (d), 0)
230#else
231# define CLOSEDIR(d) closedir (d)
232#endif
233
234/* Get or fake the disk device blocksize.
235   Usually defined by sys/param.h (if at all).  */
236#if !defined DEV_BSIZE && defined BSIZE
237# define DEV_BSIZE BSIZE
238#endif
239#if !defined DEV_BSIZE && defined BBSIZE /* SGI */
240# define DEV_BSIZE BBSIZE
241#endif
242#ifndef DEV_BSIZE
243# define DEV_BSIZE 4096
244#endif
245
246/* Extract or fake data from a `struct stat'.
247   ST_BLKSIZE: Preferred I/O blocksize for the file, in bytes.
248   ST_NBLOCKS: Number of blocks in the file, including indirect blocks.
249   ST_NBLOCKSIZE: Size of blocks used when calculating ST_NBLOCKS.  */
250#ifndef HAVE_STRUCT_STAT_ST_BLOCKS
251# define ST_BLKSIZE(statbuf) DEV_BSIZE
252# if defined _POSIX_SOURCE || !defined BSIZE /* fileblocks.c uses BSIZE.  */
253#  define ST_NBLOCKS(statbuf) \
254  ((statbuf).st_size / ST_NBLOCKSIZE + ((statbuf).st_size % ST_NBLOCKSIZE != 0))
255# else /* !_POSIX_SOURCE && BSIZE */
256#  define ST_NBLOCKS(statbuf) \
257  (S_ISREG ((statbuf).st_mode) \
258   || S_ISDIR ((statbuf).st_mode) \
259   ? st_blocks ((statbuf).st_size) : 0)
260# endif /* !_POSIX_SOURCE && BSIZE */
261#else /* HAVE_STRUCT_STAT_ST_BLOCKS */
262/* Some systems, like Sequents, return st_blksize of 0 on pipes.
263   Also, when running `rsh hpux11-system cat any-file', cat would
264   determine that the output stream had an st_blksize of 2147421096.
265   So here we arbitrarily limit the `optimal' block size to 4MB.
266   If anyone knows of a system for which the legitimate value for
267   st_blksize can exceed 4MB, please report it as a bug in this code.  */
268# define ST_BLKSIZE(statbuf) ((0 < (statbuf).st_blksize \
269			       && (statbuf).st_blksize <= (1 << 22)) /* 4MB */ \
270			      ? (statbuf).st_blksize : DEV_BSIZE)
271# if defined hpux || defined __hpux__ || defined __hpux
272/* HP-UX counts st_blocks in 1024-byte units.
273   This loses when mixing HP-UX and BSD file systems with NFS.  */
274#  define ST_NBLOCKSIZE 1024
275# else /* !hpux */
276#  if defined _AIX && defined _I386
277/* AIX PS/2 counts st_blocks in 4K units.  */
278#   define ST_NBLOCKSIZE (4 * 1024)
279#  else /* not AIX PS/2 */
280#   if defined _CRAY
281#    define ST_NBLOCKS(statbuf) \
282  (S_ISREG ((statbuf).st_mode) \
283   || S_ISDIR ((statbuf).st_mode) \
284   ? (statbuf).st_blocks * ST_BLKSIZE(statbuf)/ST_NBLOCKSIZE : 0)
285#   endif /* _CRAY */
286#  endif /* not AIX PS/2 */
287# endif /* !hpux */
288#endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
289
290#ifndef ST_NBLOCKS
291# define ST_NBLOCKS(statbuf) ((statbuf).st_blocks)
292#endif
293
294#ifndef ST_NBLOCKSIZE
295# ifdef S_BLKSIZE
296#  define ST_NBLOCKSIZE S_BLKSIZE
297# else
298#  define ST_NBLOCKSIZE 512
299# endif
300#endif
301
302/* Redirection and wildcarding when done by the utility itself.
303   Generally a noop, but used in particular for native VMS. */
304#ifndef initialize_main
305# define initialize_main(ac, av)
306#endif
307
308#include "stat-macros.h"
309
310#include "timespec.h"
311
312#if HAVE_INTTYPES_H
313# include <inttypes.h>
314#endif
315#if HAVE_STDINT_H
316# include <stdint.h>
317#endif
318
319#if ULONG_MAX_LT_ULLONG_MAX
320# define LONGEST_MODIFIER "ll"
321#else
322# define LONGEST_MODIFIER "l"
323#endif
324#if PRI_MACROS_BROKEN
325# undef PRIdMAX
326# undef PRIoMAX
327# undef PRIuMAX
328# undef PRIxMAX
329#endif
330#ifndef PRIdMAX
331# define PRIdMAX LONGEST_MODIFIER "d"
332#endif
333#ifndef PRIoMAX
334# define PRIoMAX LONGEST_MODIFIER "o"
335#endif
336#ifndef PRIuMAX
337# define PRIuMAX LONGEST_MODIFIER "u"
338#endif
339#ifndef PRIxMAX
340# define PRIxMAX LONGEST_MODIFIER "x"
341#endif
342
343#include <ctype.h>
344
345/* Jim Meyering writes:
346
347   "... Some ctype macros are valid only for character codes that
348   isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
349   using /bin/cc or gcc but without giving an ansi option).  So, all
350   ctype uses should be through macros like ISPRINT...  If
351   STDC_HEADERS is defined, then autoconf has verified that the ctype
352   macros don't need to be guarded with references to isascii. ...
353   Defining isascii to 1 should let any compiler worth its salt
354   eliminate the && through constant folding."
355
356   Bruno Haible adds:
357
358   "... Furthermore, isupper(c) etc. have an undefined result if c is
359   outside the range -1 <= c <= 255. One is tempted to write isupper(c)
360   with c being of type `char', but this is wrong if c is an 8-bit
361   character >= 128 which gets sign-extended to a negative value.
362   The macro ISUPPER protects against this as well."  */
363
364#if STDC_HEADERS || (!defined isascii && !HAVE_ISASCII)
365# define IN_CTYPE_DOMAIN(c) 1
366#else
367# define IN_CTYPE_DOMAIN(c) isascii(c)
368#endif
369
370#ifdef isblank
371# define ISBLANK(c) (IN_CTYPE_DOMAIN (c) && isblank (c))
372#else
373# define ISBLANK(c) ((c) == ' ' || (c) == '\t')
374#endif
375#ifdef isgraph
376# define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isgraph (c))
377#else
378# define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isprint (c) && !isspace (c))
379#endif
380
381/* This is defined in <sys/euc.h> on at least Solaris2.6 systems.  */
382#undef ISPRINT
383
384#define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
385#define ISALNUM(c) (IN_CTYPE_DOMAIN (c) && isalnum (c))
386#define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
387#define ISCNTRL(c) (IN_CTYPE_DOMAIN (c) && iscntrl (c))
388#define ISLOWER(c) (IN_CTYPE_DOMAIN (c) && islower (c))
389#define ISPUNCT(c) (IN_CTYPE_DOMAIN (c) && ispunct (c))
390#define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
391#define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
392#define ISXDIGIT(c) (IN_CTYPE_DOMAIN (c) && isxdigit (c))
393#define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
394
395#if STDC_HEADERS
396# define TOLOWER(Ch) tolower (Ch)
397# define TOUPPER(Ch) toupper (Ch)
398#else
399# define TOLOWER(Ch) (ISUPPER (Ch) ? tolower (Ch) : (Ch))
400# define TOUPPER(Ch) (ISLOWER (Ch) ? toupper (Ch) : (Ch))
401#endif
402
403/* ISDIGIT differs from ISDIGIT_LOCALE, as follows:
404   - Its arg may be any int or unsigned int; it need not be an unsigned char.
405   - It's guaranteed to evaluate its argument exactly once.
406   - It's typically faster.
407   POSIX says that only '0' through '9' are digits.  Prefer ISDIGIT to
408   ISDIGIT_LOCALE unless it's important to use the locale's definition
409   of `digit' even when the host does not conform to POSIX.  */
410#define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
411
412/* Convert a possibly-signed character to an unsigned character.  This is
413   a bit safer than casting to unsigned char, since it catches some type
414   errors that the cast doesn't.  */
415static inline unsigned char to_uchar (char ch) { return ch; }
416
417#include <locale.h>
418
419/* Take care of NLS matters.  */
420
421#include "gettext.h"
422#if ! ENABLE_NLS
423# undef textdomain
424# define textdomain(Domainname) /* empty */
425# undef bindtextdomain
426# define bindtextdomain(Domainname, Dirname) /* empty */
427#endif
428
429#define _(msgid) gettext (msgid)
430#define N_(msgid) msgid
431
432#define STREQ(a, b) (strcmp ((a), (b)) == 0)
433
434#if !HAVE_DECL_FREE
435void free ();
436#endif
437
438#if !HAVE_DECL_MALLOC
439char *malloc ();
440#endif
441
442#if !HAVE_DECL_MEMCHR
443char *memchr ();
444#endif
445
446#if !HAVE_DECL_REALLOC
447char *realloc ();
448#endif
449
450#if !HAVE_DECL_STPCPY
451# ifndef stpcpy
452char *stpcpy ();
453# endif
454#endif
455
456#if !HAVE_DECL_STRNDUP
457char *strndup ();
458#endif
459
460#if !HAVE_DECL_STRSTR
461char *strstr ();
462#endif
463
464#if !HAVE_DECL_GETENV
465char *getenv ();
466#endif
467
468#if !HAVE_DECL_LSEEK
469off_t lseek ();
470#endif
471
472/* This is needed on some AIX systems.  */
473#if !HAVE_DECL_STRTOUL
474unsigned long strtoul ();
475#endif
476
477#if !HAVE_DECL_GETLOGIN
478char *getlogin ();
479#endif
480
481#if !HAVE_DECL_TTYNAME
482char *ttyname ();
483#endif
484
485#if !HAVE_DECL_GETEUID
486uid_t geteuid ();
487#endif
488
489#if !HAVE_DECL_GETPWUID
490struct passwd *getpwuid ();
491#endif
492
493#if !HAVE_DECL_GETGRGID
494struct group *getgrgid ();
495#endif
496
497#if !HAVE_DECL_GETUID
498uid_t getuid ();
499#endif
500
501#include "xalloc.h"
502#include "verify.h"
503
504/* This is simply a shorthand for the common case in which
505   the third argument to x2nrealloc would be `sizeof *(P)'.
506   Ensure that sizeof *(P) is *not* 1.  In that case, it'd be
507   better to use X2REALLOC, although not strictly necessary.  */
508#define X2NREALLOC(P, PN) (verify_expr (sizeof *(P) != 1), \
509			   x2nrealloc (P, PN, sizeof *(P)))
510
511/* Using x2realloc (when appropriate) usually makes your code more
512   readable than using x2nrealloc, but it also makes it so your
513   code will malfunction if sizeof *(P) ever becomes 2 or greater.
514   So use this macro instead of using x2realloc directly.  */
515#define X2REALLOC(P, PN) (verify_expr (sizeof *(P) == 1), x2realloc (P, PN))
516
517#if ! defined HAVE_MEMPCPY && ! defined mempcpy
518/* Be CAREFUL that there are no side effects in N.  */
519# define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
520#endif
521
522/* Include automatically-generated macros for unlocked I/O.  */
523#include "unlocked-io.h"
524
525#define SAME_INODE(Stat_buf_1, Stat_buf_2) \
526  ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
527   && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
528
529#define DOT_OR_DOTDOT(Basename) \
530  (Basename[0] == '.' && (Basename[1] == '\0' \
531			  || (Basename[1] == '.' && Basename[2] == '\0')))
532
533/* A wrapper for readdir so that callers don't see entries for `.' or `..'.  */
534static inline struct dirent const *
535readdir_ignoring_dot_and_dotdot (DIR *dirp)
536{
537  while (1)
538    {
539      struct dirent const *dp = readdir (dirp);
540      if (dp == NULL || ! DOT_OR_DOTDOT (dp->d_name))
541	return dp;
542    }
543}
544
545#if SETVBUF_REVERSED
546# define SETVBUF(Stream, Buffer, Type, Size) \
547    setvbuf (Stream, Type, Buffer, Size)
548#else
549# define SETVBUF(Stream, Buffer, Type, Size) \
550    setvbuf (Stream, Buffer, Type, Size)
551#endif
552
553/* Factor out some of the common --help and --version processing code.  */
554
555/* These enum values cannot possibly conflict with the option values
556   ordinarily used by commands, including CHAR_MAX + 1, etc.  Avoid
557   CHAR_MIN - 1, as it may equal -1, the getopt end-of-options value.  */
558enum
559{
560  GETOPT_HELP_CHAR = (CHAR_MIN - 2),
561  GETOPT_VERSION_CHAR = (CHAR_MIN - 3)
562};
563
564#define GETOPT_HELP_OPTION_DECL \
565  "help", no_argument, NULL, GETOPT_HELP_CHAR
566#define GETOPT_VERSION_OPTION_DECL \
567  "version", no_argument, NULL, GETOPT_VERSION_CHAR
568
569#define case_GETOPT_HELP_CHAR			\
570  case GETOPT_HELP_CHAR:			\
571    usage (EXIT_SUCCESS);			\
572    break;
573
574/* Program_name must be a literal string.
575   Usually it is just PROGRAM_NAME.  */
576#define USAGE_BUILTIN_WARNING \
577  _("\n" \
578"NOTE: your shell may have its own version of %s, which usually supersedes\n" \
579"the version described here.  Please refer to your shell's documentation\n" \
580"for details about the options it supports.\n")
581
582#define HELP_OPTION_DESCRIPTION \
583  _("      --help     display this help and exit\n")
584#define VERSION_OPTION_DESCRIPTION \
585  _("      --version  output version information and exit\n")
586
587#include "closeout.h"
588#include "version-etc.h"
589
590#define case_GETOPT_VERSION_CHAR(Program_name, Authors)			\
591  case GETOPT_VERSION_CHAR:						\
592    version_etc (stdout, Program_name, GNU_PACKAGE, VERSION, Authors,	\
593                 (char *) NULL);					\
594    exit (EXIT_SUCCESS);						\
595    break;
596
597#ifndef MAX
598# define MAX(a, b) ((a) > (b) ? (a) : (b))
599#endif
600
601#ifndef MIN
602# define MIN(a,b) (((a) < (b)) ? (a) : (b))
603#endif
604
605#include "intprops.h"
606
607#ifndef CHAR_MIN
608# define CHAR_MIN TYPE_MINIMUM (char)
609#endif
610
611#ifndef CHAR_MAX
612# define CHAR_MAX TYPE_MAXIMUM (char)
613#endif
614
615#ifndef SCHAR_MIN
616# define SCHAR_MIN (-1 - SCHAR_MAX)
617#endif
618
619#ifndef SCHAR_MAX
620# define SCHAR_MAX (CHAR_MAX == UCHAR_MAX ? CHAR_MAX / 2 : CHAR_MAX)
621#endif
622
623#ifndef UCHAR_MAX
624# define UCHAR_MAX TYPE_MAXIMUM (unsigned char)
625#endif
626
627#ifndef SHRT_MIN
628# define SHRT_MIN TYPE_MINIMUM (short int)
629#endif
630
631#ifndef SHRT_MAX
632# define SHRT_MAX TYPE_MAXIMUM (short int)
633#endif
634
635#ifndef INT_MAX
636# define INT_MAX TYPE_MAXIMUM (int)
637#endif
638
639#ifndef INT_MIN
640# define INT_MIN TYPE_MINIMUM (int)
641#endif
642
643#ifndef INTMAX_MAX
644# define INTMAX_MAX TYPE_MAXIMUM (intmax_t)
645#endif
646
647#ifndef INTMAX_MIN
648# define INTMAX_MIN TYPE_MINIMUM (intmax_t)
649#endif
650
651#ifndef UINT_MAX
652# define UINT_MAX TYPE_MAXIMUM (unsigned int)
653#endif
654
655#ifndef LONG_MAX
656# define LONG_MAX TYPE_MAXIMUM (long int)
657#endif
658
659#ifndef ULONG_MAX
660# define ULONG_MAX TYPE_MAXIMUM (unsigned long int)
661#endif
662
663#ifndef SIZE_MAX
664# define SIZE_MAX TYPE_MAXIMUM (size_t)
665#endif
666
667#ifndef SSIZE_MAX
668# define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
669#endif
670
671#ifndef UINTMAX_MAX
672# define UINTMAX_MAX TYPE_MAXIMUM (uintmax_t)
673#endif
674
675#ifndef OFF_T_MIN
676# define OFF_T_MIN TYPE_MINIMUM (off_t)
677#endif
678
679#ifndef OFF_T_MAX
680# define OFF_T_MAX TYPE_MAXIMUM (off_t)
681#endif
682
683#ifndef UID_T_MAX
684# define UID_T_MAX TYPE_MAXIMUM (uid_t)
685#endif
686
687#ifndef GID_T_MAX
688# define GID_T_MAX TYPE_MAXIMUM (gid_t)
689#endif
690
691#ifndef PID_T_MAX
692# define PID_T_MAX TYPE_MAXIMUM (pid_t)
693#endif
694
695/* Use this to suppress gcc's `...may be used before initialized' warnings. */
696#ifdef lint
697# define IF_LINT(Code) Code
698#else
699# define IF_LINT(Code) /* empty */
700#endif
701
702#ifndef __attribute__
703# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
704#  define __attribute__(x) /* empty */
705# endif
706#endif
707
708#ifndef ATTRIBUTE_NORETURN
709# define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
710#endif
711
712#ifndef ATTRIBUTE_UNUSED
713# define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
714#endif
715
716#if defined strdupa
717# define ASSIGN_STRDUPA(DEST, S)		\
718  do { DEST = strdupa (S); } while (0)
719#else
720# define ASSIGN_STRDUPA(DEST, S)		\
721  do						\
722    {						\
723      const char *s_ = (S);			\
724      size_t len_ = strlen (s_) + 1;		\
725      char *tmp_dest_ = alloca (len_);		\
726      DEST = memcpy (tmp_dest_, (s_), len_);	\
727    }						\
728  while (0)
729#endif
730
731#ifndef EOVERFLOW
732# define EOVERFLOW EINVAL
733#endif
734
735#if ! HAVE_FSEEKO && ! defined fseeko
736# define fseeko(s, o, w) ((o) == (long int) (o)		\
737			  ? fseek (s, o, w)		\
738			  : (errno = EOVERFLOW, -1))
739#endif
740
741/* Compute the greatest common divisor of U and V using Euclid's
742   algorithm.  U and V must be nonzero.  */
743
744static inline size_t
745gcd (size_t u, size_t v)
746{
747  do
748    {
749      size_t t = u % v;
750      u = v;
751      v = t;
752    }
753  while (v);
754
755  return u;
756}
757
758/* Compute the least common multiple of U and V.  U and V must be
759   nonzero.  There is no overflow checking, so callers should not
760   specify outlandish sizes.  */
761
762static inline size_t
763lcm (size_t u, size_t v)
764{
765  return u * (v / gcd (u, v));
766}
767
768/* Return PTR, aligned upward to the next multiple of ALIGNMENT.
769   ALIGNMENT must be nonzero.  The caller must arrange for ((char *)
770   PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
771   locations.  */
772
773static inline void *
774ptr_align (void const *ptr, size_t alignment)
775{
776  char const *p0 = ptr;
777  char const *p1 = p0 + alignment - 1;
778  return (void *) (p1 - (size_t) p1 % alignment);
779}
780
781/* If 10*Accum + Digit_val is larger than the maximum value for Type,
782   then don't update Accum and return false to indicate it would
783   overflow.  Otherwise, set Accum to that new value and return true.
784   Verify at compile-time that Type is Accum's type, and that Type is
785   unsigned.  Accum must be an object, so that we can take its
786   address.  Accum and Digit_val may be evaluated multiple times.
787
788   The "Added check" below is not strictly required, but it causes GCC
789   to return a nonzero exit status instead of merely a warning
790   diagnostic, and that is more useful.  */
791
792#define DECIMAL_DIGIT_ACCUMULATE(Accum, Digit_val, Type)		\
793  (									\
794   (void) (&(Accum) == (Type *) NULL),  /* The type matches.  */	\
795   verify_expr (! TYPE_SIGNED (Type)),  /* The type is unsigned.  */	\
796   verify_expr (sizeof (Accum) == sizeof (Type)),  /* Added check.  */	\
797   (((Type) -1 / 10 < (Accum)						\
798     || (Type) ((Accum) * 10 + (Digit_val)) < (Accum))			\
799    ? false : (((Accum) = (Accum) * 10 + (Digit_val)), true))		\
800  )
801