1170754Sdelphij/* System dependent declarations. 2170754Sdelphij 3170754Sdelphij Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1998, 2001, 2002, 4170754Sdelphij 2004 Free Software Foundation, Inc. 5170754Sdelphij 6170754Sdelphij This file is part of GNU DIFF. 7170754Sdelphij 8170754Sdelphij GNU DIFF is free software; you can redistribute it and/or modify 9170754Sdelphij it under the terms of the GNU General Public License as published by 10170754Sdelphij the Free Software Foundation; either version 2, or (at your option) 11170754Sdelphij any later version. 12170754Sdelphij 13170754Sdelphij GNU DIFF is distributed in the hope that it will be useful, 14170754Sdelphij but WITHOUT ANY WARRANTY; without even the implied warranty of 15170754Sdelphij MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16170754Sdelphij GNU General Public License for more details. 17170754Sdelphij 18170754Sdelphij You should have received a copy of the GNU General Public License 19170754Sdelphij along with this program; see the file COPYING. 20170754Sdelphij If not, write to the Free Software Foundation, 21170754Sdelphij 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 22170754Sdelphij 23170754Sdelphij#include <config.h> 24170754Sdelphij 25170754Sdelphij/* Don't bother to support K&R C compilers any more; it's not worth 26170754Sdelphij the trouble. These macros prevent some library modules from being 27170754Sdelphij compiled in K&R C mode. */ 28170754Sdelphij#define PARAMS(Args) Args 29170754Sdelphij#define PROTOTYPES 1 30170754Sdelphij 31170754Sdelphij/* Define `__attribute__' and `volatile' first 32170754Sdelphij so that they're used consistently in all system includes. */ 33170754Sdelphij#if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 6) || __STRICT_ANSI__ 34170754Sdelphij# define __attribute__(x) 35170754Sdelphij#endif 36170754Sdelphij#if defined const && !defined volatile 37170754Sdelphij# define volatile 38170754Sdelphij#endif 39170754Sdelphij 40170754Sdelphij/* Verify a requirement at compile-time (unlike assert, which is runtime). */ 41170754Sdelphij#define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; } 42170754Sdelphij 43170754Sdelphij 44170754Sdelphij/* Determine whether an integer type is signed, and its bounds. 45170754Sdelphij This code assumes two's (or one's!) complement with no holes. */ 46170754Sdelphij 47170754Sdelphij/* The extra casts work around common compiler bugs, 48170754Sdelphij e.g. Cray C 5.0.3.0 when t == time_t. */ 49170754Sdelphij#ifndef TYPE_SIGNED 50170754Sdelphij# define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) 51170754Sdelphij#endif 52170754Sdelphij#ifndef TYPE_MINIMUM 53170754Sdelphij# define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \ 54170754Sdelphij ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) \ 55170754Sdelphij : (t) 0)) 56170754Sdelphij#endif 57170754Sdelphij#ifndef TYPE_MAXIMUM 58170754Sdelphij# define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t))) 59170754Sdelphij#endif 60170754Sdelphij 61170754Sdelphij#include <sys/types.h> 62170754Sdelphij#include <sys/stat.h> 63170754Sdelphij 64170754Sdelphij#ifndef S_IRWXU 65170754Sdelphij# define S_IRWXU 0700 66170754Sdelphij#endif 67170754Sdelphij#ifndef S_IRWXG 68170754Sdelphij# define S_IRWXG 0070 69170754Sdelphij#endif 70170754Sdelphij#ifndef S_IRWXO 71170754Sdelphij# define S_IRWXO 0007 72170754Sdelphij#endif 73170754Sdelphij 74170754Sdelphij#if HAVE_UNISTD_H 75170754Sdelphij# include <unistd.h> 76170754Sdelphij#endif 77170754Sdelphij 78170754Sdelphij#ifndef SEEK_SET 79170754Sdelphij# define SEEK_SET 0 80170754Sdelphij#endif 81170754Sdelphij#ifndef SEEK_CUR 82170754Sdelphij# define SEEK_CUR 1 83170754Sdelphij#endif 84170754Sdelphij 85170754Sdelphij#ifndef STDIN_FILENO 86170754Sdelphij# define STDIN_FILENO 0 87170754Sdelphij#endif 88170754Sdelphij#ifndef STDOUT_FILENO 89170754Sdelphij# define STDOUT_FILENO 1 90170754Sdelphij#endif 91170754Sdelphij#ifndef STDERR_FILENO 92170754Sdelphij# define STDERR_FILENO 2 93170754Sdelphij#endif 94170754Sdelphij 95170754Sdelphij#include <time.h> 96170754Sdelphij 97170754Sdelphij#if HAVE_FCNTL_H 98170754Sdelphij# include <fcntl.h> 99170754Sdelphij#else 100170754Sdelphij# if HAVE_SYS_FILE_H 101170754Sdelphij# include <sys/file.h> 102170754Sdelphij# endif 103170754Sdelphij#endif 104170754Sdelphij 105170754Sdelphij#if !HAVE_DUP2 106170754Sdelphij# define dup2(f, t) (close (t), fcntl (f, F_DUPFD, t)) 107170754Sdelphij#endif 108170754Sdelphij 109170754Sdelphij#ifndef O_RDONLY 110170754Sdelphij# define O_RDONLY 0 111170754Sdelphij#endif 112170754Sdelphij#ifndef O_RDWR 113170754Sdelphij# define O_RDWR 2 114170754Sdelphij#endif 115170754Sdelphij#ifndef S_IRUSR 116170754Sdelphij# define S_IRUSR 0400 117170754Sdelphij#endif 118170754Sdelphij#ifndef S_IWUSR 119170754Sdelphij# define S_IWUSR 0200 120170754Sdelphij#endif 121170754Sdelphij 122170754Sdelphij#if HAVE_SYS_WAIT_H 123170754Sdelphij# include <sys/wait.h> 124170754Sdelphij#endif 125170754Sdelphij#ifndef WEXITSTATUS 126170754Sdelphij# define WEXITSTATUS(stat_val) ((unsigned int) (stat_val) >> 8) 127170754Sdelphij#endif 128170754Sdelphij#ifndef WIFEXITED 129170754Sdelphij# define WIFEXITED(stat_val) (((stat_val) & 255) == 0) 130170754Sdelphij#endif 131170754Sdelphij 132170754Sdelphij#ifndef STAT_BLOCKSIZE 133170754Sdelphij# if HAVE_STRUCT_STAT_ST_BLKSIZE 134170754Sdelphij# define STAT_BLOCKSIZE(s) ((s).st_blksize) 135170754Sdelphij# else 136170754Sdelphij# define STAT_BLOCKSIZE(s) (8 * 1024) 137170754Sdelphij# endif 138170754Sdelphij#endif 139170754Sdelphij 140170754Sdelphij#if HAVE_DIRENT_H 141170754Sdelphij# include <dirent.h> 142170754Sdelphij# define NAMLEN(dirent) strlen ((dirent)->d_name) 143170754Sdelphij#else 144170754Sdelphij# define dirent direct 145170754Sdelphij# define NAMLEN(dirent) ((dirent)->d_namlen) 146170754Sdelphij# if HAVE_SYS_NDIR_H 147170754Sdelphij# include <sys/ndir.h> 148170754Sdelphij# endif 149170754Sdelphij# if HAVE_SYS_DIR_H 150170754Sdelphij# include <sys/dir.h> 151170754Sdelphij# endif 152170754Sdelphij# if HAVE_NDIR_H 153170754Sdelphij# include <ndir.h> 154170754Sdelphij# endif 155170754Sdelphij#endif 156170754Sdelphij 157170754Sdelphij#include <stdlib.h> 158170754Sdelphij#define EXIT_TROUBLE 2 159170754Sdelphij 160170754Sdelphij#include <limits.h> 161170754Sdelphij 162170754Sdelphij#if HAVE_INTTYPES_H 163170754Sdelphij# include <inttypes.h> 164170754Sdelphij#endif 165170754Sdelphij#ifndef PTRDIFF_MAX 166170754Sdelphij# define PTRDIFF_MAX TYPE_MAXIMUM (ptrdiff_t) 167170754Sdelphij#endif 168170754Sdelphij#ifndef SIZE_MAX 169170754Sdelphij# define SIZE_MAX TYPE_MAXIMUM (size_t) 170170754Sdelphij#endif 171170754Sdelphij#ifndef UINTMAX_MAX 172170754Sdelphij# define UINTMAX_MAX TYPE_MAXIMUM (uintmax_t) 173170754Sdelphij#endif 174170754Sdelphij#if ! HAVE_STRTOUMAX && ! defined strtoumax 175170754Sdelphijuintmax_t strtoumax (char const *, char **, int); 176170754Sdelphij#endif 177170754Sdelphij 178170754Sdelphij#include <stddef.h> 179170754Sdelphij 180170754Sdelphij#include <string.h> 181170754Sdelphij#if ! HAVE_STRCASECOLL 182170754Sdelphij# if HAVE_STRICOLL || defined stricoll 183170754Sdelphij# define strcasecoll(a, b) stricoll (a, b) 184170754Sdelphij# else 185170754Sdelphij# define strcasecoll(a, b) strcasecmp (a, b) /* best we can do */ 186170754Sdelphij# endif 187170754Sdelphij#endif 188170754Sdelphij#if ! (HAVE_STRCASECMP || defined strcasecmp) 189170754Sdelphijint strcasecmp (char const *, char const *); 190170754Sdelphij#endif 191170754Sdelphij 192170754Sdelphij#if HAVE_LOCALE_H 193170754Sdelphij# include <locale.h> 194170754Sdelphij#else 195170754Sdelphij# define setlocale(category, locale) 196170754Sdelphij#endif 197170754Sdelphij 198170754Sdelphij#include <gettext.h> 199170754Sdelphij 200170754Sdelphij#define _(msgid) gettext (msgid) 201170754Sdelphij#define N_(msgid) msgid 202170754Sdelphij 203170754Sdelphij#include <ctype.h> 204170754Sdelphij 205170754Sdelphij/* ISDIGIT differs from isdigit, as follows: 206170754Sdelphij - Its arg may be any int or unsigned int; it need not be an unsigned char. 207170754Sdelphij - It's guaranteed to evaluate its argument exactly once. 208170754Sdelphij - It's typically faster. 209170754Sdelphij POSIX 1003.1-2001 says that only '0' through '9' are digits. 210170754Sdelphij Prefer ISDIGIT to isdigit unless it's important to use the locale's 211170754Sdelphij definition of `digit' even when the host does not conform to POSIX. */ 212170754Sdelphij#define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9) 213170754Sdelphij 214170754Sdelphij#include <errno.h> 215170754Sdelphij 216170754Sdelphij#include <signal.h> 217170754Sdelphij#ifndef SA_RESTART 218170754Sdelphij# ifdef SA_INTERRUPT /* e.g. SunOS 4.1.x */ 219170754Sdelphij# define SA_RESTART SA_INTERRUPT 220170754Sdelphij# else 221170754Sdelphij# define SA_RESTART 0 222170754Sdelphij# endif 223170754Sdelphij#endif 224170754Sdelphij#if !defined SIGCHLD && defined SIGCLD 225170754Sdelphij# define SIGCHLD SIGCLD 226170754Sdelphij#endif 227170754Sdelphij 228170754Sdelphij#undef MIN 229170754Sdelphij#undef MAX 230170754Sdelphij#define MIN(a, b) ((a) <= (b) ? (a) : (b)) 231170754Sdelphij#define MAX(a, b) ((a) >= (b) ? (a) : (b)) 232170754Sdelphij 233170754Sdelphij#include <stdbool.h> 234170754Sdelphij 235170754Sdelphij#if HAVE_VFORK_H 236170754Sdelphij# include <vfork.h> 237170754Sdelphij#endif 238170754Sdelphij 239170754Sdelphij#if ! HAVE_WORKING_VFORK 240170754Sdelphij# define vfork fork 241170754Sdelphij#endif 242170754Sdelphij 243170754Sdelphij/* Type used for fast comparison of several bytes at a time. */ 244170754Sdelphij 245170754Sdelphij#ifndef word 246170754Sdelphij# define word uintmax_t 247170754Sdelphij#endif 248170754Sdelphij 249170754Sdelphij/* The integer type of a line number. Since files are read into main 250170754Sdelphij memory, ptrdiff_t should be wide enough. */ 251170754Sdelphij 252170754Sdelphijtypedef ptrdiff_t lin; 253170754Sdelphij#define LIN_MAX PTRDIFF_MAX 254170754Sdelphijverify (lin_is_signed, TYPE_SIGNED (lin)); 255170754Sdelphijverify (lin_is_wide_enough, sizeof (ptrdiff_t) <= sizeof (lin)); 256170754Sdelphijverify (lin_is_printable_as_long_int, sizeof (lin) <= sizeof (long int)); 257170754Sdelphij 258170754Sdelphij/* This section contains POSIX-compliant defaults for macros 259170754Sdelphij that are meant to be overridden by hand in config.h as needed. */ 260170754Sdelphij 261170754Sdelphij#ifndef file_name_cmp 262170754Sdelphij# define file_name_cmp strcmp 263170754Sdelphij#endif 264170754Sdelphij 265170754Sdelphij#ifndef initialize_main 266170754Sdelphij# define initialize_main(argcp, argvp) 267170754Sdelphij#endif 268170754Sdelphij 269170754Sdelphij#ifndef NULL_DEVICE 270170754Sdelphij# define NULL_DEVICE "/dev/null" 271170754Sdelphij#endif 272170754Sdelphij 273170754Sdelphij/* Do struct stat *S, *T describe the same special file? */ 274170754Sdelphij#ifndef same_special_file 275170754Sdelphij# if HAVE_ST_RDEV && defined S_ISBLK && defined S_ISCHR 276170754Sdelphij# define same_special_file(s, t) \ 277170754Sdelphij (((S_ISBLK ((s)->st_mode) && S_ISBLK ((t)->st_mode)) \ 278170754Sdelphij || (S_ISCHR ((s)->st_mode) && S_ISCHR ((t)->st_mode))) \ 279170754Sdelphij && (s)->st_rdev == (t)->st_rdev) 280170754Sdelphij# else 281170754Sdelphij# define same_special_file(s, t) 0 282170754Sdelphij# endif 283170754Sdelphij#endif 284170754Sdelphij 285170754Sdelphij/* Do struct stat *S, *T describe the same file? Answer -1 if unknown. */ 286170754Sdelphij#ifndef same_file 287170754Sdelphij# define same_file(s, t) \ 288170754Sdelphij ((((s)->st_ino == (t)->st_ino) && ((s)->st_dev == (t)->st_dev)) \ 289170754Sdelphij || same_special_file (s, t)) 290170754Sdelphij#endif 291170754Sdelphij 292170754Sdelphij/* Do struct stat *S, *T have the same file attributes? 293170754Sdelphij 294170754Sdelphij POSIX says that two files are identical if st_ino and st_dev are 295170754Sdelphij the same, but many filesystems incorrectly assign the same (device, 296170754Sdelphij inode) pair to two distinct files, including: 297170754Sdelphij 298170754Sdelphij - GNU/Linux NFS servers that export all local filesystems as a 299170754Sdelphij single NFS filesystem, if a local device number (st_dev) exceeds 300170754Sdelphij 255, or if a local inode number (st_ino) exceeds 16777215. 301170754Sdelphij 302170754Sdelphij - Network Appliance NFS servers in snapshot directories; see 303170754Sdelphij Network Appliance bug #195. 304170754Sdelphij 305170754Sdelphij - ClearCase MVFS; see bug id ATRia04618. 306170754Sdelphij 307170754Sdelphij Check whether two files that purport to be the same have the same 308170754Sdelphij attributes, to work around instances of this common bug. Do not 309170754Sdelphij inspect all attributes, only attributes useful in checking for this 310170754Sdelphij bug. 311170754Sdelphij 312170754Sdelphij It's possible for two distinct files on a buggy filesystem to have 313170754Sdelphij the same attributes, but it's not worth slowing down all 314170754Sdelphij implementations (or complicating the configuration) to cater to 315170754Sdelphij these rare cases in buggy implementations. */ 316170754Sdelphij 317170754Sdelphij#ifndef same_file_attributes 318170754Sdelphij# define same_file_attributes(s, t) \ 319170754Sdelphij ((s)->st_mode == (t)->st_mode \ 320170754Sdelphij && (s)->st_nlink == (t)->st_nlink \ 321170754Sdelphij && (s)->st_uid == (t)->st_uid \ 322170754Sdelphij && (s)->st_gid == (t)->st_gid \ 323170754Sdelphij && (s)->st_size == (t)->st_size \ 324170754Sdelphij && (s)->st_mtime == (t)->st_mtime \ 325170754Sdelphij && (s)->st_ctime == (t)->st_ctime) 326170754Sdelphij#endif 327