Assume <dirent.h> exists, since gnulib assumes this now as well.
[platform/upstream/coreutils.git] / src / system.h
1 /* system-dependent definitions for coreutils
2    Copyright (C) 1989, 1991-2006 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
24 you 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.  */
128 enum
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.  */
138 static inline void
139 initialize_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 && defined O_DIRECTIO
160 /* Tru64 spells it `O_DIRECTIO'.  */
161 # define O_DIRECT O_DIRECTIO
162 #endif
163
164 #if !defined O_DIRECT
165 # define O_DIRECT 0
166 #endif
167
168 #if !defined O_DIRECTORY
169 # define O_DIRECTORY 0
170 #endif
171
172 #if !defined O_DSYNC
173 # define O_DSYNC 0
174 #endif
175
176 #if !defined O_NDELAY
177 # define O_NDELAY 0
178 #endif
179
180 #if !defined O_NOATIME
181 # define O_NOATIME 0
182 #endif
183
184 #if !defined O_NONBLOCK
185 # define O_NONBLOCK O_NDELAY
186 #endif
187
188 #if !defined O_NOCTTY
189 # define O_NOCTTY 0
190 #endif
191
192 #if !defined O_NOFOLLOW
193 # define O_NOFOLLOW 0
194 #endif
195
196 #if !defined O_NOLINKS
197 # define O_NOLINKS 0
198 #endif
199
200 #if !defined O_RSYNC
201 # define O_RSYNC 0
202 #endif
203
204 #if !defined O_SYNC
205 # define O_SYNC 0
206 #endif
207
208 /* For systems that distinguish between text and binary I/O.
209    O_BINARY is usually declared in fcntl.h  */
210 #if !defined O_BINARY && defined _O_BINARY
211   /* For MSC-compatible compilers.  */
212 # define O_BINARY _O_BINARY
213 # define O_TEXT _O_TEXT
214 #endif
215
216 #ifdef __BEOS__
217   /* BeOS 5 has O_BINARY and O_TEXT, but they have no effect.  */
218 # undef O_BINARY
219 # undef O_TEXT
220 #endif
221
222 #ifndef O_BINARY
223 # define O_BINARY 0
224 # define O_TEXT 0
225 #endif
226
227 #include <dirent.h>
228 #ifndef _D_EXACT_NAMLEN
229 # define _D_EXACT_NAMLEN(dp) strlen ((dp)->d_name)
230 #endif
231
232 enum
233 {
234   NOT_AN_INODE_NUMBER = 0
235 };
236
237 #ifdef D_INO_IN_DIRENT
238 # define D_INO(dp) (dp)->d_ino
239 #else
240 /* Some systems don't have inodes, so fake them to avoid lots of ifdefs.  */
241 # define D_INO(dp) NOT_AN_INODE_NUMBER
242 #endif
243
244 /* Get or fake the disk device blocksize.
245    Usually defined by sys/param.h (if at all).  */
246 #if !defined DEV_BSIZE && defined BSIZE
247 # define DEV_BSIZE BSIZE
248 #endif
249 #if !defined DEV_BSIZE && defined BBSIZE /* SGI */
250 # define DEV_BSIZE BBSIZE
251 #endif
252 #ifndef DEV_BSIZE
253 # define DEV_BSIZE 4096
254 #endif
255
256 /* Extract or fake data from a `struct stat'.
257    ST_BLKSIZE: Preferred I/O blocksize for the file, in bytes.
258    ST_NBLOCKS: Number of blocks in the file, including indirect blocks.
259    ST_NBLOCKSIZE: Size of blocks used when calculating ST_NBLOCKS.  */
260 #ifndef HAVE_STRUCT_STAT_ST_BLOCKS
261 # define ST_BLKSIZE(statbuf) DEV_BSIZE
262 # if defined _POSIX_SOURCE || !defined BSIZE /* fileblocks.c uses BSIZE.  */
263 #  define ST_NBLOCKS(statbuf) \
264   ((statbuf).st_size / ST_NBLOCKSIZE + ((statbuf).st_size % ST_NBLOCKSIZE != 0))
265 # else /* !_POSIX_SOURCE && BSIZE */
266 #  define ST_NBLOCKS(statbuf) \
267   (S_ISREG ((statbuf).st_mode) \
268    || S_ISDIR ((statbuf).st_mode) \
269    ? st_blocks ((statbuf).st_size) : 0)
270 # endif /* !_POSIX_SOURCE && BSIZE */
271 #else /* HAVE_STRUCT_STAT_ST_BLOCKS */
272 /* Some systems, like Sequents, return st_blksize of 0 on pipes.
273    Also, when running `rsh hpux11-system cat any-file', cat would
274    determine that the output stream had an st_blksize of 2147421096.
275    So here we arbitrarily limit the `optimal' block size to 4MB.
276    If anyone knows of a system for which the legitimate value for
277    st_blksize can exceed 4MB, please report it as a bug in this code.  */
278 # define ST_BLKSIZE(statbuf) ((0 < (statbuf).st_blksize \
279                                && (statbuf).st_blksize <= (1 << 22)) /* 4MB */ \
280                               ? (statbuf).st_blksize : DEV_BSIZE)
281 # if defined hpux || defined __hpux__ || defined __hpux
282 /* HP-UX counts st_blocks in 1024-byte units.
283    This loses when mixing HP-UX and BSD file systems with NFS.  */
284 #  define ST_NBLOCKSIZE 1024
285 # else /* !hpux */
286 #  if defined _AIX && defined _I386
287 /* AIX PS/2 counts st_blocks in 4K units.  */
288 #   define ST_NBLOCKSIZE (4 * 1024)
289 #  else /* not AIX PS/2 */
290 #   if defined _CRAY
291 #    define ST_NBLOCKS(statbuf) \
292   (S_ISREG ((statbuf).st_mode) \
293    || S_ISDIR ((statbuf).st_mode) \
294    ? (statbuf).st_blocks * ST_BLKSIZE(statbuf)/ST_NBLOCKSIZE : 0)
295 #   endif /* _CRAY */
296 #  endif /* not AIX PS/2 */
297 # endif /* !hpux */
298 #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
299
300 #ifndef ST_NBLOCKS
301 # define ST_NBLOCKS(statbuf) ((statbuf).st_blocks)
302 #endif
303
304 #ifndef ST_NBLOCKSIZE
305 # ifdef S_BLKSIZE
306 #  define ST_NBLOCKSIZE S_BLKSIZE
307 # else
308 #  define ST_NBLOCKSIZE 512
309 # endif
310 #endif
311
312 /* Redirection and wildcarding when done by the utility itself.
313    Generally a noop, but used in particular for native VMS. */
314 #ifndef initialize_main
315 # define initialize_main(ac, av)
316 #endif
317
318 #include "stat-macros.h"
319
320 #include "timespec.h"
321
322 #if HAVE_INTTYPES_H
323 # include <inttypes.h>
324 #endif
325 #include <stdint.h>
326
327 #if ULONG_MAX_LT_ULLONG_MAX
328 # define LONGEST_MODIFIER "ll"
329 #else
330 # define LONGEST_MODIFIER "l"
331 #endif
332 #if PRI_MACROS_BROKEN
333 # undef PRIdMAX
334 # undef PRIoMAX
335 # undef PRIuMAX
336 # undef PRIxMAX
337 #endif
338 #ifndef PRIdMAX
339 # define PRIdMAX LONGEST_MODIFIER "d"
340 #endif
341 #ifndef PRIoMAX
342 # define PRIoMAX LONGEST_MODIFIER "o"
343 #endif
344 #ifndef PRIuMAX
345 # define PRIuMAX LONGEST_MODIFIER "u"
346 #endif
347 #ifndef PRIxMAX
348 # define PRIxMAX LONGEST_MODIFIER "x"
349 #endif
350
351 #include <ctype.h>
352
353 #if ! (defined isblank || HAVE_DECL_ISBLANK)
354 # define isblank(c) ((c) == ' ' || (c) == '\t')
355 #endif
356
357 /* ISDIGIT differs from isdigit, as follows:
358    - Its arg may be any int or unsigned int; it need not be an unsigned char
359      or EOF.
360    - It's typically faster.
361    POSIX says that only '0' through '9' are digits.  Prefer ISDIGIT to
362    isdigit unless it's important to use the locale's definition
363    of `digit' even when the host does not conform to POSIX.  */
364 #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
365
366 /* Convert a possibly-signed character to an unsigned character.  This is
367    a bit safer than casting to unsigned char, since it catches some type
368    errors that the cast doesn't.  */
369 static inline unsigned char to_uchar (char ch) { return ch; }
370
371 #include <locale.h>
372
373 /* Take care of NLS matters.  */
374
375 #include "gettext.h"
376 #if ! ENABLE_NLS
377 # undef textdomain
378 # define textdomain(Domainname) /* empty */
379 # undef bindtextdomain
380 # define bindtextdomain(Domainname, Dirname) /* empty */
381 #endif
382
383 #define _(msgid) gettext (msgid)
384 #define N_(msgid) msgid
385
386 #define STREQ(a, b) (strcmp ((a), (b)) == 0)
387
388 #if !HAVE_DECL_FREE
389 void free ();
390 #endif
391
392 #if !HAVE_DECL_MALLOC
393 char *malloc ();
394 #endif
395
396 #if !HAVE_DECL_MEMCHR
397 char *memchr ();
398 #endif
399
400 #if !HAVE_DECL_REALLOC
401 char *realloc ();
402 #endif
403
404 #if !HAVE_DECL_STPCPY
405 # ifndef stpcpy
406 char *stpcpy ();
407 # endif
408 #endif
409
410 #if !HAVE_DECL_STRNDUP
411 char *strndup ();
412 #endif
413
414 #if !HAVE_DECL_STRSTR
415 char *strstr ();
416 #endif
417
418 #if !HAVE_DECL_GETENV
419 char *getenv ();
420 #endif
421
422 #if !HAVE_DECL_LSEEK
423 off_t lseek ();
424 #endif
425
426 /* This is needed on some AIX systems.  */
427 #if !HAVE_DECL_STRTOUL
428 unsigned long strtoul ();
429 #endif
430
431 #if !HAVE_DECL_GETLOGIN
432 char *getlogin ();
433 #endif
434
435 #if !HAVE_DECL_TTYNAME
436 char *ttyname ();
437 #endif
438
439 #if !HAVE_DECL_GETEUID
440 uid_t geteuid ();
441 #endif
442
443 #if !HAVE_DECL_GETPWUID
444 struct passwd *getpwuid ();
445 #endif
446
447 #if !HAVE_DECL_GETGRGID
448 struct group *getgrgid ();
449 #endif
450
451 #if !HAVE_DECL_GETUID
452 uid_t getuid ();
453 #endif
454
455 #include "xalloc.h"
456 #include "verify.h"
457
458 /* This is simply a shorthand for the common case in which
459    the third argument to x2nrealloc would be `sizeof *(P)'.
460    Ensure that sizeof *(P) is *not* 1.  In that case, it'd be
461    better to use X2REALLOC, although not strictly necessary.  */
462 #define X2NREALLOC(P, PN) ((void) verify_true (sizeof *(P) != 1), \
463                            x2nrealloc (P, PN, sizeof *(P)))
464
465 /* Using x2realloc (when appropriate) usually makes your code more
466    readable than using x2nrealloc, but it also makes it so your
467    code will malfunction if sizeof *(P) ever becomes 2 or greater.
468    So use this macro instead of using x2realloc directly.  */
469 #define X2REALLOC(P, PN) ((void) verify_true (sizeof *(P) == 1), \
470                           x2realloc (P, PN))
471
472 #if ! defined HAVE_MEMPCPY && ! defined mempcpy
473 /* Be CAREFUL that there are no side effects in N.  */
474 # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
475 #endif
476
477 /* Include automatically-generated macros for unlocked I/O.  */
478 #include "unlocked-io.h"
479 #include "same-inode.h"
480
481 #define DOT_OR_DOTDOT(Basename) \
482   (Basename[0] == '.' && (Basename[1] == '\0' \
483                           || (Basename[1] == '.' && Basename[2] == '\0')))
484
485 /* A wrapper for readdir so that callers don't see entries for `.' or `..'.  */
486 static inline struct dirent const *
487 readdir_ignoring_dot_and_dotdot (DIR *dirp)
488 {
489   while (1)
490     {
491       struct dirent const *dp = readdir (dirp);
492       if (dp == NULL || ! DOT_OR_DOTDOT (dp->d_name))
493         return dp;
494     }
495 }
496
497 #if SETVBUF_REVERSED
498 # define SETVBUF(Stream, Buffer, Type, Size) \
499     setvbuf (Stream, Type, Buffer, Size)
500 #else
501 # define SETVBUF(Stream, Buffer, Type, Size) \
502     setvbuf (Stream, Buffer, Type, Size)
503 #endif
504
505 /* Factor out some of the common --help and --version processing code.  */
506
507 /* These enum values cannot possibly conflict with the option values
508    ordinarily used by commands, including CHAR_MAX + 1, etc.  Avoid
509    CHAR_MIN - 1, as it may equal -1, the getopt end-of-options value.  */
510 enum
511 {
512   GETOPT_HELP_CHAR = (CHAR_MIN - 2),
513   GETOPT_VERSION_CHAR = (CHAR_MIN - 3)
514 };
515
516 #define GETOPT_HELP_OPTION_DECL \
517   "help", no_argument, NULL, GETOPT_HELP_CHAR
518 #define GETOPT_VERSION_OPTION_DECL \
519   "version", no_argument, NULL, GETOPT_VERSION_CHAR
520
521 #define case_GETOPT_HELP_CHAR                   \
522   case GETOPT_HELP_CHAR:                        \
523     usage (EXIT_SUCCESS);                       \
524     break;
525
526 /* Program_name must be a literal string.
527    Usually it is just PROGRAM_NAME.  */
528 #define USAGE_BUILTIN_WARNING \
529   _("\n" \
530 "NOTE: your shell may have its own version of %s, which usually supersedes\n" \
531 "the version described here.  Please refer to your shell's documentation\n" \
532 "for details about the options it supports.\n")
533
534 #define HELP_OPTION_DESCRIPTION \
535   _("      --help     display this help and exit\n")
536 #define VERSION_OPTION_DESCRIPTION \
537   _("      --version  output version information and exit\n")
538
539 #include "closeout.h"
540 #include "version-etc.h"
541
542 #define case_GETOPT_VERSION_CHAR(Program_name, Authors)                 \
543   case GETOPT_VERSION_CHAR:                                             \
544     version_etc (stdout, Program_name, GNU_PACKAGE, VERSION, Authors,   \
545                  (char *) NULL);                                        \
546     exit (EXIT_SUCCESS);                                                \
547     break;
548
549 #ifndef MAX
550 # define MAX(a, b) ((a) > (b) ? (a) : (b))
551 #endif
552
553 #ifndef MIN
554 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
555 #endif
556
557 #include "intprops.h"
558
559 #ifndef CHAR_MIN
560 # define CHAR_MIN TYPE_MINIMUM (char)
561 #endif
562
563 #ifndef CHAR_MAX
564 # define CHAR_MAX TYPE_MAXIMUM (char)
565 #endif
566
567 #ifndef SCHAR_MIN
568 # define SCHAR_MIN (-1 - SCHAR_MAX)
569 #endif
570
571 #ifndef SCHAR_MAX
572 # define SCHAR_MAX (CHAR_MAX == UCHAR_MAX ? CHAR_MAX / 2 : CHAR_MAX)
573 #endif
574
575 #ifndef UCHAR_MAX
576 # define UCHAR_MAX TYPE_MAXIMUM (unsigned char)
577 #endif
578
579 #ifndef SHRT_MIN
580 # define SHRT_MIN TYPE_MINIMUM (short int)
581 #endif
582
583 #ifndef SHRT_MAX
584 # define SHRT_MAX TYPE_MAXIMUM (short int)
585 #endif
586
587 #ifndef INT_MAX
588 # define INT_MAX TYPE_MAXIMUM (int)
589 #endif
590
591 #ifndef INT_MIN
592 # define INT_MIN TYPE_MINIMUM (int)
593 #endif
594
595 #ifndef INTMAX_MAX
596 # define INTMAX_MAX TYPE_MAXIMUM (intmax_t)
597 #endif
598
599 #ifndef INTMAX_MIN
600 # define INTMAX_MIN TYPE_MINIMUM (intmax_t)
601 #endif
602
603 #ifndef UINT_MAX
604 # define UINT_MAX TYPE_MAXIMUM (unsigned int)
605 #endif
606
607 #ifndef LONG_MAX
608 # define LONG_MAX TYPE_MAXIMUM (long int)
609 #endif
610
611 #ifndef ULONG_MAX
612 # define ULONG_MAX TYPE_MAXIMUM (unsigned long int)
613 #endif
614
615 #ifndef SIZE_MAX
616 # define SIZE_MAX TYPE_MAXIMUM (size_t)
617 #endif
618
619 #ifndef SSIZE_MAX
620 # define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
621 #endif
622
623 #ifndef UINTMAX_MAX
624 # define UINTMAX_MAX TYPE_MAXIMUM (uintmax_t)
625 #endif
626
627 #ifndef OFF_T_MIN
628 # define OFF_T_MIN TYPE_MINIMUM (off_t)
629 #endif
630
631 #ifndef OFF_T_MAX
632 # define OFF_T_MAX TYPE_MAXIMUM (off_t)
633 #endif
634
635 #ifndef UID_T_MAX
636 # define UID_T_MAX TYPE_MAXIMUM (uid_t)
637 #endif
638
639 #ifndef GID_T_MAX
640 # define GID_T_MAX TYPE_MAXIMUM (gid_t)
641 #endif
642
643 #ifndef PID_T_MAX
644 # define PID_T_MAX TYPE_MAXIMUM (pid_t)
645 #endif
646
647 /* Use this to suppress gcc's `...may be used before initialized' warnings. */
648 #ifdef lint
649 # define IF_LINT(Code) Code
650 #else
651 # define IF_LINT(Code) /* empty */
652 #endif
653
654 #ifndef __attribute__
655 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
656 #  define __attribute__(x) /* empty */
657 # endif
658 #endif
659
660 #ifndef ATTRIBUTE_NORETURN
661 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
662 #endif
663
664 #ifndef ATTRIBUTE_UNUSED
665 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
666 #endif
667
668 #if defined strdupa
669 # define ASSIGN_STRDUPA(DEST, S)                \
670   do { DEST = strdupa (S); } while (0)
671 #else
672 # define ASSIGN_STRDUPA(DEST, S)                \
673   do                                            \
674     {                                           \
675       const char *s_ = (S);                     \
676       size_t len_ = strlen (s_) + 1;            \
677       char *tmp_dest_ = alloca (len_);          \
678       DEST = memcpy (tmp_dest_, (s_), len_);    \
679     }                                           \
680   while (0)
681 #endif
682
683 #ifndef EOVERFLOW
684 # define EOVERFLOW EINVAL
685 #endif
686
687 #if ! HAVE_FSEEKO && ! defined fseeko
688 # define fseeko(s, o, w) ((o) == (long int) (o)         \
689                           ? fseek (s, o, w)             \
690                           : (errno = EOVERFLOW, -1))
691 #endif
692
693 #if ! HAVE_SYNC
694 # define sync() /* empty */
695 #endif
696
697 /* Compute the greatest common divisor of U and V using Euclid's
698    algorithm.  U and V must be nonzero.  */
699
700 static inline size_t
701 gcd (size_t u, size_t v)
702 {
703   do
704     {
705       size_t t = u % v;
706       u = v;
707       v = t;
708     }
709   while (v);
710
711   return u;
712 }
713
714 /* Compute the least common multiple of U and V.  U and V must be
715    nonzero.  There is no overflow checking, so callers should not
716    specify outlandish sizes.  */
717
718 static inline size_t
719 lcm (size_t u, size_t v)
720 {
721   return u * (v / gcd (u, v));
722 }
723
724 /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
725    ALIGNMENT must be nonzero.  The caller must arrange for ((char *)
726    PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
727    locations.  */
728
729 static inline void *
730 ptr_align (void const *ptr, size_t alignment)
731 {
732   char const *p0 = ptr;
733   char const *p1 = p0 + alignment - 1;
734   return (void *) (p1 - (size_t) p1 % alignment);
735 }
736
737 /* If 10*Accum + Digit_val is larger than the maximum value for Type,
738    then don't update Accum and return false to indicate it would
739    overflow.  Otherwise, set Accum to that new value and return true.
740    Verify at compile-time that Type is Accum's type, and that Type is
741    unsigned.  Accum must be an object, so that we can take its
742    address.  Accum and Digit_val may be evaluated multiple times.
743
744    The "Added check" below is not strictly required, but it causes GCC
745    to return a nonzero exit status instead of merely a warning
746    diagnostic, and that is more useful.  */
747
748 #define DECIMAL_DIGIT_ACCUMULATE(Accum, Digit_val, Type)                \
749   (                                                                     \
750    (void) (&(Accum) == (Type *) NULL),  /* The type matches.  */        \
751    (void) verify_true (! TYPE_SIGNED (Type)), /* The type is unsigned.  */ \
752    (void) verify_true (sizeof (Accum) == sizeof (Type)), /* Added check.  */ \
753    (((Type) -1 / 10 < (Accum)                                           \
754      || (Type) ((Accum) * 10 + (Digit_val)) < (Accum))                  \
755     ? false : (((Accum) = (Accum) * 10 + (Digit_val)), true))           \
756   )