xref: /netbsd-src/external/gpl2/diffutils/dist/src/system.h (revision f8c23a2b94243924f9b7311eb0ad24bf23d5c657)
1*f8c23a2bSchristos /*	$NetBSD: system.h,v 1.2 2016/01/13 03:39:28 christos Exp $	*/
275f6d617Schristos 
375f6d617Schristos /* System dependent declarations.
475f6d617Schristos 
575f6d617Schristos    Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1998, 2001, 2002
675f6d617Schristos    Free Software Foundation, Inc.
775f6d617Schristos 
875f6d617Schristos    This file is part of GNU DIFF.
975f6d617Schristos 
1075f6d617Schristos    GNU DIFF is free software; you can redistribute it and/or modify
1175f6d617Schristos    it under the terms of the GNU General Public License as published by
1275f6d617Schristos    the Free Software Foundation; either version 2, or (at your option)
1375f6d617Schristos    any later version.
1475f6d617Schristos 
1575f6d617Schristos    GNU DIFF is distributed in the hope that it will be useful,
1675f6d617Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
1775f6d617Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1875f6d617Schristos    GNU General Public License for more details.
1975f6d617Schristos 
2075f6d617Schristos    You should have received a copy of the GNU General Public License
2175f6d617Schristos    along with this program; see the file COPYING.
2275f6d617Schristos    If not, write to the Free Software Foundation,
2375f6d617Schristos    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
2475f6d617Schristos 
2575f6d617Schristos #include <config.h>
2675f6d617Schristos 
2775f6d617Schristos /* Don't bother to support K&R C compilers any more; it's not worth
2875f6d617Schristos    the trouble.  These macros prevent some library modules from being
2975f6d617Schristos    compiled in K&R C mode.  */
3075f6d617Schristos #define PARAMS(Args) Args
3175f6d617Schristos #define PROTOTYPES 1
3275f6d617Schristos 
3375f6d617Schristos /* Define `__attribute__' and `volatile' first
3475f6d617Schristos    so that they're used consistently in all system includes.  */
3575f6d617Schristos #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 6) || __STRICT_ANSI__
3675f6d617Schristos # define __attribute__(x)
3775f6d617Schristos #endif
3875f6d617Schristos #if defined const && !defined volatile
3975f6d617Schristos # define volatile
4075f6d617Schristos #endif
4175f6d617Schristos 
4275f6d617Schristos /* Verify a requirement at compile-time (unlike assert, which is runtime).  */
4375f6d617Schristos #define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
4475f6d617Schristos 
4575f6d617Schristos 
4675f6d617Schristos /* Determine whether an integer type is signed, and its bounds.
4775f6d617Schristos    This code assumes two's (or one's!) complement with no holes.  */
4875f6d617Schristos 
4975f6d617Schristos /* The extra casts work around common compiler bugs,
5075f6d617Schristos    e.g. Cray C 5.0.3.0 when t == time_t.  */
5175f6d617Schristos #ifndef TYPE_SIGNED
5275f6d617Schristos # define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
5375f6d617Schristos #endif
5475f6d617Schristos #ifndef TYPE_MINIMUM
5575f6d617Schristos # define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
5675f6d617Schristos 			       ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) \
5775f6d617Schristos 			       : (t) 0))
5875f6d617Schristos #endif
5975f6d617Schristos #ifndef TYPE_MAXIMUM
6075f6d617Schristos # define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
6175f6d617Schristos #endif
6275f6d617Schristos 
6375f6d617Schristos #include <sys/types.h>
6475f6d617Schristos #include <sys/stat.h>
6575f6d617Schristos 
6675f6d617Schristos #if STAT_MACROS_BROKEN
6775f6d617Schristos # undef S_ISBLK
6875f6d617Schristos # undef S_ISCHR
6975f6d617Schristos # undef S_ISDIR
7075f6d617Schristos # undef S_ISFIFO
7175f6d617Schristos # undef S_ISREG
7275f6d617Schristos # undef S_ISSOCK
7375f6d617Schristos #endif
7475f6d617Schristos #ifndef S_ISDIR
7575f6d617Schristos # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
7675f6d617Schristos #endif
7775f6d617Schristos #ifndef S_ISREG
7875f6d617Schristos # define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
7975f6d617Schristos #endif
8075f6d617Schristos #if !defined S_ISBLK && defined S_IFBLK
8175f6d617Schristos # define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
8275f6d617Schristos #endif
8375f6d617Schristos #if !defined S_ISCHR && defined S_IFCHR
8475f6d617Schristos # define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
8575f6d617Schristos #endif
8675f6d617Schristos #if !defined S_ISFIFO && defined S_IFFIFO
8775f6d617Schristos # define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFFIFO)
8875f6d617Schristos #endif
8975f6d617Schristos #if !defined S_ISSOCK && defined S_IFSOCK
9075f6d617Schristos # define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
9175f6d617Schristos #endif
9275f6d617Schristos #ifndef S_IXUSR
9375f6d617Schristos # define S_IXUSR 0100
9475f6d617Schristos #endif
9575f6d617Schristos #ifndef S_IXGRP
9675f6d617Schristos # define S_IXGRP 0010
9775f6d617Schristos #endif
9875f6d617Schristos #ifndef S_IXOTH
9975f6d617Schristos # define S_IXOTH 0001
10075f6d617Schristos #endif
10175f6d617Schristos 
10275f6d617Schristos #if HAVE_UNISTD_H
10375f6d617Schristos # include <unistd.h>
10475f6d617Schristos #endif
10575f6d617Schristos 
10675f6d617Schristos #ifndef SEEK_SET
10775f6d617Schristos # define SEEK_SET 0
10875f6d617Schristos #endif
10975f6d617Schristos #ifndef SEEK_CUR
11075f6d617Schristos # define SEEK_CUR 1
11175f6d617Schristos #endif
11275f6d617Schristos 
11375f6d617Schristos #ifndef STDIN_FILENO
11475f6d617Schristos # define STDIN_FILENO 0
11575f6d617Schristos #endif
11675f6d617Schristos #ifndef STDOUT_FILENO
11775f6d617Schristos # define STDOUT_FILENO 1
11875f6d617Schristos #endif
11975f6d617Schristos #ifndef STDERR_FILENO
12075f6d617Schristos # define STDERR_FILENO 2
12175f6d617Schristos #endif
12275f6d617Schristos 
12375f6d617Schristos #if HAVE_TIME_H
12475f6d617Schristos # include <time.h>
12575f6d617Schristos #else
12675f6d617Schristos # include <sys/time.h>
12775f6d617Schristos #endif
12875f6d617Schristos 
12975f6d617Schristos #if HAVE_FCNTL_H
13075f6d617Schristos # include <fcntl.h>
13175f6d617Schristos #else
13275f6d617Schristos # if HAVE_SYS_FILE_H
13375f6d617Schristos #  include <sys/file.h>
13475f6d617Schristos # endif
13575f6d617Schristos #endif
13675f6d617Schristos 
13775f6d617Schristos #if !HAVE_DUP2
13875f6d617Schristos # define dup2(f, t) (close (t), fcntl (f, F_DUPFD, t))
13975f6d617Schristos #endif
14075f6d617Schristos 
14175f6d617Schristos #ifndef O_RDONLY
14275f6d617Schristos # define O_RDONLY 0
14375f6d617Schristos #endif
14475f6d617Schristos #ifndef O_RDWR
14575f6d617Schristos # define O_RDWR 2
14675f6d617Schristos #endif
14775f6d617Schristos #ifndef S_IRUSR
14875f6d617Schristos # define S_IRUSR 0400
14975f6d617Schristos #endif
15075f6d617Schristos #ifndef S_IWUSR
15175f6d617Schristos # define S_IWUSR 0200
15275f6d617Schristos #endif
15375f6d617Schristos 
15475f6d617Schristos #if HAVE_SYS_WAIT_H
15575f6d617Schristos # include <sys/wait.h>
15675f6d617Schristos #endif
15775f6d617Schristos #ifndef WEXITSTATUS
15875f6d617Schristos # define WEXITSTATUS(stat_val) ((unsigned int) (stat_val) >> 8)
15975f6d617Schristos #endif
16075f6d617Schristos #ifndef WIFEXITED
16175f6d617Schristos # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
16275f6d617Schristos #endif
16375f6d617Schristos 
16475f6d617Schristos #ifndef STAT_BLOCKSIZE
16575f6d617Schristos # if HAVE_STRUCT_STAT_ST_BLKSIZE
16675f6d617Schristos #  define STAT_BLOCKSIZE(s) ((s).st_blksize)
16775f6d617Schristos # else
16875f6d617Schristos #  define STAT_BLOCKSIZE(s) (8 * 1024)
16975f6d617Schristos # endif
17075f6d617Schristos #endif
17175f6d617Schristos 
17275f6d617Schristos #if HAVE_DIRENT_H
17375f6d617Schristos # include <dirent.h>
17475f6d617Schristos # define NAMLEN(dirent) strlen ((dirent)->d_name)
17575f6d617Schristos #else
17675f6d617Schristos # define dirent direct
17775f6d617Schristos # define NAMLEN(dirent) ((dirent)->d_namlen)
17875f6d617Schristos # if HAVE_SYS_NDIR_H
17975f6d617Schristos #  include <sys/ndir.h>
18075f6d617Schristos # endif
18175f6d617Schristos # if HAVE_SYS_DIR_H
18275f6d617Schristos #  include <sys/dir.h>
18375f6d617Schristos # endif
18475f6d617Schristos # if HAVE_NDIR_H
18575f6d617Schristos #  include <ndir.h>
18675f6d617Schristos # endif
18775f6d617Schristos #endif
18875f6d617Schristos 
18975f6d617Schristos #if HAVE_STDLIB_H
19075f6d617Schristos # include <stdlib.h>
19175f6d617Schristos #else
19275f6d617Schristos # ifndef getenv
19375f6d617Schristos    char *getenv ();
19475f6d617Schristos # endif
19575f6d617Schristos #endif
19675f6d617Schristos #ifndef EXIT_SUCCESS
19775f6d617Schristos # define EXIT_SUCCESS 0
19875f6d617Schristos #endif
19975f6d617Schristos #if !EXIT_FAILURE
20075f6d617Schristos # undef EXIT_FAILURE /* Sony NEWS-OS 4.0C defines EXIT_FAILURE to 0.  */
20175f6d617Schristos # define EXIT_FAILURE 1
20275f6d617Schristos #endif
20375f6d617Schristos #define EXIT_TROUBLE 2
20475f6d617Schristos 
20575f6d617Schristos #include <limits.h>
20675f6d617Schristos #ifndef SSIZE_MAX
20775f6d617Schristos # define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
20875f6d617Schristos #endif
20975f6d617Schristos 
21075f6d617Schristos #if HAVE_INTTYPES_H
21175f6d617Schristos # include <inttypes.h>
21275f6d617Schristos #endif
21375f6d617Schristos #ifndef PTRDIFF_MAX
21475f6d617Schristos # define PTRDIFF_MAX TYPE_MAXIMUM (ptrdiff_t)
21575f6d617Schristos #endif
21675f6d617Schristos #ifndef SIZE_MAX
21775f6d617Schristos # define SIZE_MAX TYPE_MAXIMUM (size_t)
21875f6d617Schristos #endif
21975f6d617Schristos #ifndef UINTMAX_MAX
22075f6d617Schristos # define UINTMAX_MAX TYPE_MAXIMUM (uintmax_t)
22175f6d617Schristos #endif
22275f6d617Schristos #if ! HAVE_STRTOUMAX  && ! defined strtoumax
22375f6d617Schristos uintmax_t strtoumax (char const *, char **, int);
22475f6d617Schristos #endif
22575f6d617Schristos 
22675f6d617Schristos #include <stddef.h>
22775f6d617Schristos 
22875f6d617Schristos #if STDC_HEADERS || HAVE_STRING_H
22975f6d617Schristos # include <string.h>
23075f6d617Schristos #else
23175f6d617Schristos # if !HAVE_STRCHR
23275f6d617Schristos #  define strchr index
23375f6d617Schristos #  define strrchr rindex
23475f6d617Schristos # endif
23575f6d617Schristos char *strchr (), *strrchr ();
23675f6d617Schristos # if !HAVE_MEMCHR
23775f6d617Schristos #  define memcmp(s1, s2, n) bcmp (s1, s2, n)
23875f6d617Schristos #  define memcpy(d, s, n) bcopy (s, d, n)
23975f6d617Schristos void *memchr ();
24075f6d617Schristos # endif
24175f6d617Schristos #endif
24275f6d617Schristos 
24375f6d617Schristos #if HAVE_LOCALE_H
24475f6d617Schristos # include <locale.h>
24575f6d617Schristos #else
24675f6d617Schristos # define setlocale(category, locale)
24775f6d617Schristos #endif
24875f6d617Schristos 
24975f6d617Schristos #include <gettext.h>
25075f6d617Schristos 
25175f6d617Schristos #define _(msgid) gettext (msgid)
25275f6d617Schristos #define N_(msgid) msgid
25375f6d617Schristos 
25475f6d617Schristos #include <ctype.h>
25575f6d617Schristos 
25675f6d617Schristos /* CTYPE_DOMAIN (C) is nonzero if the unsigned char C can safely be given
25775f6d617Schristos    as an argument to <ctype.h> macros like `isspace'.  */
25875f6d617Schristos #if STDC_HEADERS
25975f6d617Schristos # define CTYPE_DOMAIN(c) 1
26075f6d617Schristos #else
26175f6d617Schristos # define CTYPE_DOMAIN(c) ((unsigned int) (c) <= 0177)
26275f6d617Schristos #endif
26375f6d617Schristos #define ISPRINT(c) (CTYPE_DOMAIN (c) && isprint (c))
26475f6d617Schristos #define ISSPACE(c) (CTYPE_DOMAIN (c) && isspace (c))
26575f6d617Schristos 
26675f6d617Schristos #if STDC_HEADERS
26775f6d617Schristos # define TOLOWER(c) tolower (c)
26875f6d617Schristos #else
26975f6d617Schristos # ifndef _tolower
27075f6d617Schristos #  define _tolower(c) tolower (c)
27175f6d617Schristos # endif
27275f6d617Schristos # define TOLOWER(c) (CTYPE_DOMAIN (c) && isupper (c) ? _tolower (c) : (c))
27375f6d617Schristos #endif
27475f6d617Schristos 
27575f6d617Schristos /* ISDIGIT differs from isdigit, as follows:
27675f6d617Schristos    - Its arg may be any int or unsigned int; it need not be an unsigned char.
27775f6d617Schristos    - It's guaranteed to evaluate its argument exactly once.
27875f6d617Schristos    - It's typically faster.
27975f6d617Schristos    POSIX 1003.1-2001 says that only '0' through '9' are digits.
28075f6d617Schristos    Prefer ISDIGIT to isdigit unless it's important to use the locale's
28175f6d617Schristos    definition of `digit' even when the host does not conform to POSIX.  */
28275f6d617Schristos #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
28375f6d617Schristos 
28475f6d617Schristos #include <errno.h>
28575f6d617Schristos #if !STDC_HEADERS
28675f6d617Schristos  extern int errno;
28775f6d617Schristos #endif
28875f6d617Schristos 
28975f6d617Schristos #include <signal.h>
29075f6d617Schristos #ifndef SA_RESTART
29175f6d617Schristos # ifdef SA_INTERRUPT /* e.g. SunOS 4.1.x */
29275f6d617Schristos #  define SA_RESTART SA_INTERRUPT
29375f6d617Schristos # else
29475f6d617Schristos #  define SA_RESTART 0
29575f6d617Schristos # endif
29675f6d617Schristos #endif
29775f6d617Schristos #if !defined SIGCHLD && defined SIGCLD
29875f6d617Schristos # define SIGCHLD SIGCLD
29975f6d617Schristos #endif
30075f6d617Schristos 
30175f6d617Schristos #undef MIN
30275f6d617Schristos #undef MAX
30375f6d617Schristos #define MIN(a, b) ((a) <= (b) ? (a) : (b))
30475f6d617Schristos #define MAX(a, b) ((a) >= (b) ? (a) : (b))
30575f6d617Schristos 
30675f6d617Schristos #if HAVE_STDBOOL_H
30775f6d617Schristos # include <stdbool.h>
30875f6d617Schristos #else
30975f6d617Schristos # define bool unsigned char
31075f6d617Schristos #endif
31175f6d617Schristos 
31275f6d617Schristos #if HAVE_VFORK_H
31375f6d617Schristos # include <vfork.h>
31475f6d617Schristos #endif
31575f6d617Schristos 
31675f6d617Schristos #if ! HAVE_WORKING_VFORK
31775f6d617Schristos # define vfork fork
31875f6d617Schristos #endif
31975f6d617Schristos 
32075f6d617Schristos /* Type used for fast comparison of several bytes at a time.  */
32175f6d617Schristos 
32275f6d617Schristos #ifndef word
32375f6d617Schristos # define word uintmax_t
32475f6d617Schristos #endif
32575f6d617Schristos 
32675f6d617Schristos /* The integer type of a line number.  Since files are read into main
32775f6d617Schristos    memory, ptrdiff_t should be wide enough.  */
32875f6d617Schristos 
32975f6d617Schristos typedef ptrdiff_t lin;
33075f6d617Schristos #define LIN_MAX PTRDIFF_MAX
33175f6d617Schristos verify (lin_is_signed, TYPE_SIGNED (lin));
33275f6d617Schristos verify (lin_is_wide_enough, sizeof (ptrdiff_t) <= sizeof (lin));
33375f6d617Schristos verify (lin_is_printable_as_long, sizeof (lin) <= sizeof (long));
33475f6d617Schristos 
33575f6d617Schristos /* This section contains POSIX-compliant defaults for macros
33675f6d617Schristos    that are meant to be overridden by hand in config.h as needed.  */
33775f6d617Schristos 
33875f6d617Schristos #ifndef file_name_cmp
33975f6d617Schristos # define file_name_cmp strcmp
34075f6d617Schristos #endif
34175f6d617Schristos 
34275f6d617Schristos #ifndef initialize_main
34375f6d617Schristos # define initialize_main(argcp, argvp)
34475f6d617Schristos #endif
34575f6d617Schristos 
34675f6d617Schristos #ifndef NULL_DEVICE
34775f6d617Schristos # define NULL_DEVICE "/dev/null"
34875f6d617Schristos #endif
34975f6d617Schristos 
35075f6d617Schristos /* Do struct stat *S, *T describe the same special file?  */
35175f6d617Schristos #ifndef same_special_file
352*f8c23a2bSchristos # if HAVE_STRUCT_STAT_ST_RDEV && defined S_ISBLK && defined S_ISCHR
35375f6d617Schristos #  define same_special_file(s, t) \
35475f6d617Schristos      (((S_ISBLK ((s)->st_mode) && S_ISBLK ((t)->st_mode)) \
35575f6d617Schristos        || (S_ISCHR ((s)->st_mode) && S_ISCHR ((t)->st_mode))) \
35675f6d617Schristos       && (s)->st_rdev == (t)->st_rdev)
35775f6d617Schristos # else
35875f6d617Schristos #  define same_special_file(s, t) 0
35975f6d617Schristos # endif
36075f6d617Schristos #endif
36175f6d617Schristos 
36275f6d617Schristos /* Do struct stat *S, *T describe the same file?  Answer -1 if unknown.  */
36375f6d617Schristos #ifndef same_file
36475f6d617Schristos # define same_file(s, t) \
36575f6d617Schristos     ((((s)->st_ino == (t)->st_ino) && ((s)->st_dev == (t)->st_dev)) \
36675f6d617Schristos      || same_special_file (s, t))
36775f6d617Schristos #endif
36875f6d617Schristos 
36975f6d617Schristos /* Do struct stat *S, *T have the same file attributes?
37075f6d617Schristos 
37175f6d617Schristos    POSIX says that two files are identical if st_ino and st_dev are
37275f6d617Schristos    the same, but many filesystems incorrectly assign the same (device,
37375f6d617Schristos    inode) pair to two distinct files, including:
37475f6d617Schristos 
37575f6d617Schristos    - GNU/Linux NFS servers that export all local filesystems as a
37675f6d617Schristos      single NFS filesystem, if a local device number (st_dev) exceeds
37775f6d617Schristos      255, or if a local inode number (st_ino) exceeds 16777215.
37875f6d617Schristos 
37975f6d617Schristos    - Network Appliance NFS servers in snapshot directories; see
38075f6d617Schristos      Network Appliance bug #195.
38175f6d617Schristos 
38275f6d617Schristos    - ClearCase MVFS; see bug id ATRia04618.
38375f6d617Schristos 
38475f6d617Schristos    Check whether two files that purport to be the same have the same
38575f6d617Schristos    attributes, to work around instances of this common bug.  Do not
38675f6d617Schristos    inspect all attributes, only attributes useful in checking for this
38775f6d617Schristos    bug.
38875f6d617Schristos 
38975f6d617Schristos    It's possible for two distinct files on a buggy filesystem to have
39075f6d617Schristos    the same attributes, but it's not worth slowing down all
39175f6d617Schristos    implementations (or complicating the configuration) to cater to
39275f6d617Schristos    these rare cases in buggy implementations.  */
39375f6d617Schristos 
39475f6d617Schristos #ifndef same_file_attributes
39575f6d617Schristos # define same_file_attributes(s, t) \
39675f6d617Schristos    ((s)->st_mode == (t)->st_mode \
39775f6d617Schristos     && (s)->st_nlink == (t)->st_nlink \
39875f6d617Schristos     && (s)->st_uid == (t)->st_uid \
39975f6d617Schristos     && (s)->st_gid == (t)->st_gid \
40075f6d617Schristos     && (s)->st_size == (t)->st_size \
40175f6d617Schristos     && (s)->st_mtime == (t)->st_mtime \
40275f6d617Schristos     && (s)->st_ctime == (t)->st_ctime)
40375f6d617Schristos #endif
404