xref: /freebsd/contrib/diff/src/system.h (revision 18fd37a72c3a7549d2d4f6c6ea00bdcd2bdaca01)
118fd37a7SXin LI /* System dependent declarations.
218fd37a7SXin LI 
318fd37a7SXin LI    Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1998, 2001, 2002,
418fd37a7SXin LI    2004 Free Software Foundation, Inc.
518fd37a7SXin LI 
618fd37a7SXin LI    This file is part of GNU DIFF.
718fd37a7SXin LI 
818fd37a7SXin LI    GNU DIFF is free software; you can redistribute it and/or modify
918fd37a7SXin LI    it under the terms of the GNU General Public License as published by
1018fd37a7SXin LI    the Free Software Foundation; either version 2, or (at your option)
1118fd37a7SXin LI    any later version.
1218fd37a7SXin LI 
1318fd37a7SXin LI    GNU DIFF is distributed in the hope that it will be useful,
1418fd37a7SXin LI    but WITHOUT ANY WARRANTY; without even the implied warranty of
1518fd37a7SXin LI    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1618fd37a7SXin LI    GNU General Public License for more details.
1718fd37a7SXin LI 
1818fd37a7SXin LI    You should have received a copy of the GNU General Public License
1918fd37a7SXin LI    along with this program; see the file COPYING.
2018fd37a7SXin LI    If not, write to the Free Software Foundation,
2118fd37a7SXin LI    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
2218fd37a7SXin LI 
2318fd37a7SXin LI #include <config.h>
2418fd37a7SXin LI 
2518fd37a7SXin LI /* Don't bother to support K&R C compilers any more; it's not worth
2618fd37a7SXin LI    the trouble.  These macros prevent some library modules from being
2718fd37a7SXin LI    compiled in K&R C mode.  */
2818fd37a7SXin LI #define PARAMS(Args) Args
2918fd37a7SXin LI #define PROTOTYPES 1
3018fd37a7SXin LI 
3118fd37a7SXin LI /* Define `__attribute__' and `volatile' first
3218fd37a7SXin LI    so that they're used consistently in all system includes.  */
3318fd37a7SXin LI #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 6) || __STRICT_ANSI__
3418fd37a7SXin LI # define __attribute__(x)
3518fd37a7SXin LI #endif
3618fd37a7SXin LI #if defined const && !defined volatile
3718fd37a7SXin LI # define volatile
3818fd37a7SXin LI #endif
3918fd37a7SXin LI 
4018fd37a7SXin LI /* Verify a requirement at compile-time (unlike assert, which is runtime).  */
4118fd37a7SXin LI #define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
4218fd37a7SXin LI 
4318fd37a7SXin LI 
4418fd37a7SXin LI /* Determine whether an integer type is signed, and its bounds.
4518fd37a7SXin LI    This code assumes two's (or one's!) complement with no holes.  */
4618fd37a7SXin LI 
4718fd37a7SXin LI /* The extra casts work around common compiler bugs,
4818fd37a7SXin LI    e.g. Cray C 5.0.3.0 when t == time_t.  */
4918fd37a7SXin LI #ifndef TYPE_SIGNED
5018fd37a7SXin LI # define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
5118fd37a7SXin LI #endif
5218fd37a7SXin LI #ifndef TYPE_MINIMUM
5318fd37a7SXin LI # define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
5418fd37a7SXin LI 			       ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) \
5518fd37a7SXin LI 			       : (t) 0))
5618fd37a7SXin LI #endif
5718fd37a7SXin LI #ifndef TYPE_MAXIMUM
5818fd37a7SXin LI # define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
5918fd37a7SXin LI #endif
6018fd37a7SXin LI 
6118fd37a7SXin LI #include <sys/types.h>
6218fd37a7SXin LI #include <sys/stat.h>
6318fd37a7SXin LI 
6418fd37a7SXin LI #ifndef S_IRWXU
6518fd37a7SXin LI # define S_IRWXU 0700
6618fd37a7SXin LI #endif
6718fd37a7SXin LI #ifndef S_IRWXG
6818fd37a7SXin LI # define S_IRWXG 0070
6918fd37a7SXin LI #endif
7018fd37a7SXin LI #ifndef S_IRWXO
7118fd37a7SXin LI # define S_IRWXO 0007
7218fd37a7SXin LI #endif
7318fd37a7SXin LI 
7418fd37a7SXin LI #if HAVE_UNISTD_H
7518fd37a7SXin LI # include <unistd.h>
7618fd37a7SXin LI #endif
7718fd37a7SXin LI 
7818fd37a7SXin LI #ifndef SEEK_SET
7918fd37a7SXin LI # define SEEK_SET 0
8018fd37a7SXin LI #endif
8118fd37a7SXin LI #ifndef SEEK_CUR
8218fd37a7SXin LI # define SEEK_CUR 1
8318fd37a7SXin LI #endif
8418fd37a7SXin LI 
8518fd37a7SXin LI #ifndef STDIN_FILENO
8618fd37a7SXin LI # define STDIN_FILENO 0
8718fd37a7SXin LI #endif
8818fd37a7SXin LI #ifndef STDOUT_FILENO
8918fd37a7SXin LI # define STDOUT_FILENO 1
9018fd37a7SXin LI #endif
9118fd37a7SXin LI #ifndef STDERR_FILENO
9218fd37a7SXin LI # define STDERR_FILENO 2
9318fd37a7SXin LI #endif
9418fd37a7SXin LI 
9518fd37a7SXin LI #include <time.h>
9618fd37a7SXin LI 
9718fd37a7SXin LI #if HAVE_FCNTL_H
9818fd37a7SXin LI # include <fcntl.h>
9918fd37a7SXin LI #else
10018fd37a7SXin LI # if HAVE_SYS_FILE_H
10118fd37a7SXin LI #  include <sys/file.h>
10218fd37a7SXin LI # endif
10318fd37a7SXin LI #endif
10418fd37a7SXin LI 
10518fd37a7SXin LI #if !HAVE_DUP2
10618fd37a7SXin LI # define dup2(f, t) (close (t), fcntl (f, F_DUPFD, t))
10718fd37a7SXin LI #endif
10818fd37a7SXin LI 
10918fd37a7SXin LI #ifndef O_RDONLY
11018fd37a7SXin LI # define O_RDONLY 0
11118fd37a7SXin LI #endif
11218fd37a7SXin LI #ifndef O_RDWR
11318fd37a7SXin LI # define O_RDWR 2
11418fd37a7SXin LI #endif
11518fd37a7SXin LI #ifndef S_IRUSR
11618fd37a7SXin LI # define S_IRUSR 0400
11718fd37a7SXin LI #endif
11818fd37a7SXin LI #ifndef S_IWUSR
11918fd37a7SXin LI # define S_IWUSR 0200
12018fd37a7SXin LI #endif
12118fd37a7SXin LI 
12218fd37a7SXin LI #if HAVE_SYS_WAIT_H
12318fd37a7SXin LI # include <sys/wait.h>
12418fd37a7SXin LI #endif
12518fd37a7SXin LI #ifndef WEXITSTATUS
12618fd37a7SXin LI # define WEXITSTATUS(stat_val) ((unsigned int) (stat_val) >> 8)
12718fd37a7SXin LI #endif
12818fd37a7SXin LI #ifndef WIFEXITED
12918fd37a7SXin LI # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
13018fd37a7SXin LI #endif
13118fd37a7SXin LI 
13218fd37a7SXin LI #ifndef STAT_BLOCKSIZE
13318fd37a7SXin LI # if HAVE_STRUCT_STAT_ST_BLKSIZE
13418fd37a7SXin LI #  define STAT_BLOCKSIZE(s) ((s).st_blksize)
13518fd37a7SXin LI # else
13618fd37a7SXin LI #  define STAT_BLOCKSIZE(s) (8 * 1024)
13718fd37a7SXin LI # endif
13818fd37a7SXin LI #endif
13918fd37a7SXin LI 
14018fd37a7SXin LI #if HAVE_DIRENT_H
14118fd37a7SXin LI # include <dirent.h>
14218fd37a7SXin LI # define NAMLEN(dirent) strlen ((dirent)->d_name)
14318fd37a7SXin LI #else
14418fd37a7SXin LI # define dirent direct
14518fd37a7SXin LI # define NAMLEN(dirent) ((dirent)->d_namlen)
14618fd37a7SXin LI # if HAVE_SYS_NDIR_H
14718fd37a7SXin LI #  include <sys/ndir.h>
14818fd37a7SXin LI # endif
14918fd37a7SXin LI # if HAVE_SYS_DIR_H
15018fd37a7SXin LI #  include <sys/dir.h>
15118fd37a7SXin LI # endif
15218fd37a7SXin LI # if HAVE_NDIR_H
15318fd37a7SXin LI #  include <ndir.h>
15418fd37a7SXin LI # endif
15518fd37a7SXin LI #endif
15618fd37a7SXin LI 
15718fd37a7SXin LI #include <stdlib.h>
15818fd37a7SXin LI #define EXIT_TROUBLE 2
15918fd37a7SXin LI 
16018fd37a7SXin LI #include <limits.h>
16118fd37a7SXin LI 
16218fd37a7SXin LI #if HAVE_INTTYPES_H
16318fd37a7SXin LI # include <inttypes.h>
16418fd37a7SXin LI #endif
16518fd37a7SXin LI #ifndef PTRDIFF_MAX
16618fd37a7SXin LI # define PTRDIFF_MAX TYPE_MAXIMUM (ptrdiff_t)
16718fd37a7SXin LI #endif
16818fd37a7SXin LI #ifndef SIZE_MAX
16918fd37a7SXin LI # define SIZE_MAX TYPE_MAXIMUM (size_t)
17018fd37a7SXin LI #endif
17118fd37a7SXin LI #ifndef UINTMAX_MAX
17218fd37a7SXin LI # define UINTMAX_MAX TYPE_MAXIMUM (uintmax_t)
17318fd37a7SXin LI #endif
17418fd37a7SXin LI #if ! HAVE_STRTOUMAX  && ! defined strtoumax
17518fd37a7SXin LI uintmax_t strtoumax (char const *, char **, int);
17618fd37a7SXin LI #endif
17718fd37a7SXin LI 
17818fd37a7SXin LI #include <stddef.h>
17918fd37a7SXin LI 
18018fd37a7SXin LI #include <string.h>
18118fd37a7SXin LI #if ! HAVE_STRCASECOLL
18218fd37a7SXin LI # if HAVE_STRICOLL || defined stricoll
18318fd37a7SXin LI #  define strcasecoll(a, b) stricoll (a, b)
18418fd37a7SXin LI # else
18518fd37a7SXin LI #  define strcasecoll(a, b) strcasecmp (a, b) /* best we can do */
18618fd37a7SXin LI # endif
18718fd37a7SXin LI #endif
18818fd37a7SXin LI #if ! (HAVE_STRCASECMP || defined strcasecmp)
18918fd37a7SXin LI int strcasecmp (char const *, char const *);
19018fd37a7SXin LI #endif
19118fd37a7SXin LI 
19218fd37a7SXin LI #if HAVE_LOCALE_H
19318fd37a7SXin LI # include <locale.h>
19418fd37a7SXin LI #else
19518fd37a7SXin LI # define setlocale(category, locale)
19618fd37a7SXin LI #endif
19718fd37a7SXin LI 
19818fd37a7SXin LI #include <gettext.h>
19918fd37a7SXin LI 
20018fd37a7SXin LI #define _(msgid) gettext (msgid)
20118fd37a7SXin LI #define N_(msgid) msgid
20218fd37a7SXin LI 
20318fd37a7SXin LI #include <ctype.h>
20418fd37a7SXin LI 
20518fd37a7SXin LI /* ISDIGIT differs from isdigit, as follows:
20618fd37a7SXin LI    - Its arg may be any int or unsigned int; it need not be an unsigned char.
20718fd37a7SXin LI    - It's guaranteed to evaluate its argument exactly once.
20818fd37a7SXin LI    - It's typically faster.
20918fd37a7SXin LI    POSIX 1003.1-2001 says that only '0' through '9' are digits.
21018fd37a7SXin LI    Prefer ISDIGIT to isdigit unless it's important to use the locale's
21118fd37a7SXin LI    definition of `digit' even when the host does not conform to POSIX.  */
21218fd37a7SXin LI #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
21318fd37a7SXin LI 
21418fd37a7SXin LI #include <errno.h>
21518fd37a7SXin LI 
21618fd37a7SXin LI #include <signal.h>
21718fd37a7SXin LI #ifndef SA_RESTART
21818fd37a7SXin LI # ifdef SA_INTERRUPT /* e.g. SunOS 4.1.x */
21918fd37a7SXin LI #  define SA_RESTART SA_INTERRUPT
22018fd37a7SXin LI # else
22118fd37a7SXin LI #  define SA_RESTART 0
22218fd37a7SXin LI # endif
22318fd37a7SXin LI #endif
22418fd37a7SXin LI #if !defined SIGCHLD && defined SIGCLD
22518fd37a7SXin LI # define SIGCHLD SIGCLD
22618fd37a7SXin LI #endif
22718fd37a7SXin LI 
22818fd37a7SXin LI #undef MIN
22918fd37a7SXin LI #undef MAX
23018fd37a7SXin LI #define MIN(a, b) ((a) <= (b) ? (a) : (b))
23118fd37a7SXin LI #define MAX(a, b) ((a) >= (b) ? (a) : (b))
23218fd37a7SXin LI 
23318fd37a7SXin LI #include <stdbool.h>
23418fd37a7SXin LI 
23518fd37a7SXin LI #if HAVE_VFORK_H
23618fd37a7SXin LI # include <vfork.h>
23718fd37a7SXin LI #endif
23818fd37a7SXin LI 
23918fd37a7SXin LI #if ! HAVE_WORKING_VFORK
24018fd37a7SXin LI # define vfork fork
24118fd37a7SXin LI #endif
24218fd37a7SXin LI 
24318fd37a7SXin LI /* Type used for fast comparison of several bytes at a time.  */
24418fd37a7SXin LI 
24518fd37a7SXin LI #ifndef word
24618fd37a7SXin LI # define word uintmax_t
24718fd37a7SXin LI #endif
24818fd37a7SXin LI 
24918fd37a7SXin LI /* The integer type of a line number.  Since files are read into main
25018fd37a7SXin LI    memory, ptrdiff_t should be wide enough.  */
25118fd37a7SXin LI 
25218fd37a7SXin LI typedef ptrdiff_t lin;
25318fd37a7SXin LI #define LIN_MAX PTRDIFF_MAX
25418fd37a7SXin LI verify (lin_is_signed, TYPE_SIGNED (lin));
25518fd37a7SXin LI verify (lin_is_wide_enough, sizeof (ptrdiff_t) <= sizeof (lin));
25618fd37a7SXin LI verify (lin_is_printable_as_long_int, sizeof (lin) <= sizeof (long int));
25718fd37a7SXin LI 
25818fd37a7SXin LI /* This section contains POSIX-compliant defaults for macros
25918fd37a7SXin LI    that are meant to be overridden by hand in config.h as needed.  */
26018fd37a7SXin LI 
26118fd37a7SXin LI #ifndef file_name_cmp
26218fd37a7SXin LI # define file_name_cmp strcmp
26318fd37a7SXin LI #endif
26418fd37a7SXin LI 
26518fd37a7SXin LI #ifndef initialize_main
26618fd37a7SXin LI # define initialize_main(argcp, argvp)
26718fd37a7SXin LI #endif
26818fd37a7SXin LI 
26918fd37a7SXin LI #ifndef NULL_DEVICE
27018fd37a7SXin LI # define NULL_DEVICE "/dev/null"
27118fd37a7SXin LI #endif
27218fd37a7SXin LI 
27318fd37a7SXin LI /* Do struct stat *S, *T describe the same special file?  */
27418fd37a7SXin LI #ifndef same_special_file
27518fd37a7SXin LI # if HAVE_ST_RDEV && defined S_ISBLK && defined S_ISCHR
27618fd37a7SXin LI #  define same_special_file(s, t) \
27718fd37a7SXin LI      (((S_ISBLK ((s)->st_mode) && S_ISBLK ((t)->st_mode)) \
27818fd37a7SXin LI        || (S_ISCHR ((s)->st_mode) && S_ISCHR ((t)->st_mode))) \
27918fd37a7SXin LI       && (s)->st_rdev == (t)->st_rdev)
28018fd37a7SXin LI # else
28118fd37a7SXin LI #  define same_special_file(s, t) 0
28218fd37a7SXin LI # endif
28318fd37a7SXin LI #endif
28418fd37a7SXin LI 
28518fd37a7SXin LI /* Do struct stat *S, *T describe the same file?  Answer -1 if unknown.  */
28618fd37a7SXin LI #ifndef same_file
28718fd37a7SXin LI # define same_file(s, t) \
28818fd37a7SXin LI     ((((s)->st_ino == (t)->st_ino) && ((s)->st_dev == (t)->st_dev)) \
28918fd37a7SXin LI      || same_special_file (s, t))
29018fd37a7SXin LI #endif
29118fd37a7SXin LI 
29218fd37a7SXin LI /* Do struct stat *S, *T have the same file attributes?
29318fd37a7SXin LI 
29418fd37a7SXin LI    POSIX says that two files are identical if st_ino and st_dev are
29518fd37a7SXin LI    the same, but many filesystems incorrectly assign the same (device,
29618fd37a7SXin LI    inode) pair to two distinct files, including:
29718fd37a7SXin LI 
29818fd37a7SXin LI    - GNU/Linux NFS servers that export all local filesystems as a
29918fd37a7SXin LI      single NFS filesystem, if a local device number (st_dev) exceeds
30018fd37a7SXin LI      255, or if a local inode number (st_ino) exceeds 16777215.
30118fd37a7SXin LI 
30218fd37a7SXin LI    - Network Appliance NFS servers in snapshot directories; see
30318fd37a7SXin LI      Network Appliance bug #195.
30418fd37a7SXin LI 
30518fd37a7SXin LI    - ClearCase MVFS; see bug id ATRia04618.
30618fd37a7SXin LI 
30718fd37a7SXin LI    Check whether two files that purport to be the same have the same
30818fd37a7SXin LI    attributes, to work around instances of this common bug.  Do not
30918fd37a7SXin LI    inspect all attributes, only attributes useful in checking for this
31018fd37a7SXin LI    bug.
31118fd37a7SXin LI 
31218fd37a7SXin LI    It's possible for two distinct files on a buggy filesystem to have
31318fd37a7SXin LI    the same attributes, but it's not worth slowing down all
31418fd37a7SXin LI    implementations (or complicating the configuration) to cater to
31518fd37a7SXin LI    these rare cases in buggy implementations.  */
31618fd37a7SXin LI 
31718fd37a7SXin LI #ifndef same_file_attributes
31818fd37a7SXin LI # define same_file_attributes(s, t) \
31918fd37a7SXin LI    ((s)->st_mode == (t)->st_mode \
32018fd37a7SXin LI     && (s)->st_nlink == (t)->st_nlink \
32118fd37a7SXin LI     && (s)->st_uid == (t)->st_uid \
32218fd37a7SXin LI     && (s)->st_gid == (t)->st_gid \
32318fd37a7SXin LI     && (s)->st_size == (t)->st_size \
32418fd37a7SXin LI     && (s)->st_mtime == (t)->st_mtime \
32518fd37a7SXin LI     && (s)->st_ctime == (t)->st_ctime)
32618fd37a7SXin LI #endif
327