1/* System dependent declarations. 2 3 Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1998, 2001, 2002 4 Free Software Foundation, Inc. 5 6 This file is part of GNU DIFF. 7 8 GNU DIFF is free software; you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 2, or (at your option) 11 any later version. 12 13 GNU DIFF is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program; see the file COPYING. 20 If not, write to the Free Software Foundation, 21 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 22 23#include <config.h> 24 25/* Don't bother to support K&R C compilers any more; it's not worth 26 the trouble. These macros prevent some library modules from being 27 compiled in K&R C mode. */ 28#define PARAMS(Args) Args 29#define PROTOTYPES 1 30 31/* Define `__attribute__' and `volatile' first 32 so that they're used consistently in all system includes. */ 33#if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 6) || __STRICT_ANSI__ 34# define __attribute__(x) 35#endif 36#if defined const && !defined volatile 37# define volatile 38#endif 39 40/* Verify a requirement at compile-time (unlike assert, which is runtime). */ 41#define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; } 42 43 44/* Determine whether an integer type is signed, and its bounds. 45 This code assumes two's (or one's!) complement with no holes. */ 46 47/* The extra casts work around common compiler bugs, 48 e.g. Cray C 5.0.3.0 when t == time_t. */ 49#ifndef TYPE_SIGNED 50# define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) 51#endif 52#ifndef TYPE_MINIMUM 53# define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \ 54 ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) \ 55 : (t) 0)) 56#endif 57#ifndef TYPE_MAXIMUM 58# define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t))) 59#endif 60 61#include <sys/types.h> 62#include <sys/stat.h> 63 64#if STAT_MACROS_BROKEN 65# undef S_ISBLK 66# undef S_ISCHR 67# undef S_ISDIR 68# undef S_ISFIFO 69# undef S_ISREG 70# undef S_ISSOCK 71#endif 72#ifndef S_ISDIR 73# define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) 74#endif 75#ifndef S_ISREG 76# define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) 77#endif 78#if !defined S_ISBLK && defined S_IFBLK 79# define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK) 80#endif 81#if !defined S_ISCHR && defined S_IFCHR 82# define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR) 83#endif 84#if !defined S_ISFIFO && defined S_IFFIFO 85# define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFFIFO) 86#endif 87#if !defined S_ISSOCK && defined S_IFSOCK 88# define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK) 89#endif 90#ifndef S_IXUSR 91# define S_IXUSR 0100 92#endif 93#ifndef S_IXGRP 94# define S_IXGRP 0010 95#endif 96#ifndef S_IXOTH 97# define S_IXOTH 0001 98#endif 99 100#if HAVE_UNISTD_H 101# include <unistd.h> 102#endif 103 104#ifndef SEEK_SET 105# define SEEK_SET 0 106#endif 107#ifndef SEEK_CUR 108# define SEEK_CUR 1 109#endif 110 111#ifndef STDIN_FILENO 112# define STDIN_FILENO 0 113#endif 114#ifndef STDOUT_FILENO 115# define STDOUT_FILENO 1 116#endif 117#ifndef STDERR_FILENO 118# define STDERR_FILENO 2 119#endif 120 121#if HAVE_TIME_H 122# include <time.h> 123#else 124# include <sys/time.h> 125#endif 126 127#if HAVE_FCNTL_H 128# include <fcntl.h> 129#else 130# if HAVE_SYS_FILE_H 131# include <sys/file.h> 132# endif 133#endif 134 135#if !HAVE_DUP2 136# define dup2(f, t) (close (t), fcntl (f, F_DUPFD, t)) 137#endif 138 139#ifndef O_RDONLY 140# define O_RDONLY 0 141#endif 142#ifndef O_RDWR 143# define O_RDWR 2 144#endif 145#ifndef S_IRUSR 146# define S_IRUSR 0400 147#endif 148#ifndef S_IWUSR 149# define S_IWUSR 0200 150#endif 151 152#if HAVE_SYS_WAIT_H 153# include <sys/wait.h> 154#endif 155#ifndef WEXITSTATUS 156# define WEXITSTATUS(stat_val) ((unsigned int) (stat_val) >> 8) 157#endif 158#ifndef WIFEXITED 159# define WIFEXITED(stat_val) (((stat_val) & 255) == 0) 160#endif 161 162#ifndef STAT_BLOCKSIZE 163# if HAVE_STRUCT_STAT_ST_BLKSIZE 164# define STAT_BLOCKSIZE(s) ((s).st_blksize) 165# else 166# define STAT_BLOCKSIZE(s) (8 * 1024) 167# endif 168#endif 169 170#if HAVE_DIRENT_H 171# include <dirent.h> 172# define NAMLEN(dirent) strlen ((dirent)->d_name) 173#else 174# define dirent direct 175# define NAMLEN(dirent) ((dirent)->d_namlen) 176# if HAVE_SYS_NDIR_H 177# include <sys/ndir.h> 178# endif 179# if HAVE_SYS_DIR_H 180# include <sys/dir.h> 181# endif 182# if HAVE_NDIR_H 183# include <ndir.h> 184# endif 185#endif 186 187#if HAVE_STDLIB_H 188# include <stdlib.h> 189#else 190# ifndef getenv 191 char *getenv (); 192# endif 193#endif 194#ifndef EXIT_SUCCESS 195# define EXIT_SUCCESS 0 196#endif 197#if !EXIT_FAILURE 198# undef EXIT_FAILURE /* Sony NEWS-OS 4.0C defines EXIT_FAILURE to 0. */ 199# define EXIT_FAILURE 1 200#endif 201#define EXIT_TROUBLE 2 202 203#include <limits.h> 204#ifndef SSIZE_MAX 205# define SSIZE_MAX TYPE_MAXIMUM (ssize_t) 206#endif 207 208#if HAVE_INTTYPES_H 209# include <inttypes.h> 210#endif 211#ifndef PTRDIFF_MAX 212# define PTRDIFF_MAX TYPE_MAXIMUM (ptrdiff_t) 213#endif 214#ifndef SIZE_MAX 215# define SIZE_MAX TYPE_MAXIMUM (size_t) 216#endif 217#ifndef UINTMAX_MAX 218# define UINTMAX_MAX TYPE_MAXIMUM (uintmax_t) 219#endif 220#if ! HAVE_STRTOUMAX && ! defined strtoumax 221uintmax_t strtoumax (char const *, char **, int); 222#endif 223 224#include <stddef.h> 225 226#if STDC_HEADERS || HAVE_STRING_H 227# include <string.h> 228#else 229# if !HAVE_STRCHR 230# define strchr index 231# define strrchr rindex 232# endif 233char *strchr (), *strrchr (); 234# if !HAVE_MEMCHR 235# define memcmp(s1, s2, n) bcmp (s1, s2, n) 236# define memcpy(d, s, n) bcopy (s, d, n) 237void *memchr (); 238# endif 239#endif 240 241#if HAVE_LOCALE_H 242# include <locale.h> 243#else 244# define setlocale(category, locale) 245#endif 246 247#include <gettext.h> 248 249#define _(msgid) gettext (msgid) 250#define N_(msgid) msgid 251 252#include <ctype.h> 253 254/* CTYPE_DOMAIN (C) is nonzero if the unsigned char C can safely be given 255 as an argument to <ctype.h> macros like `isspace'. */ 256#if STDC_HEADERS 257# define CTYPE_DOMAIN(c) 1 258#else 259# define CTYPE_DOMAIN(c) ((unsigned int) (c) <= 0177) 260#endif 261#define ISPRINT(c) (CTYPE_DOMAIN (c) && isprint (c)) 262#define ISSPACE(c) (CTYPE_DOMAIN (c) && isspace (c)) 263 264#if STDC_HEADERS 265# define TOLOWER(c) tolower (c) 266#else 267# ifndef _tolower 268# define _tolower(c) tolower (c) 269# endif 270# define TOLOWER(c) (CTYPE_DOMAIN (c) && isupper (c) ? _tolower (c) : (c)) 271#endif 272 273/* ISDIGIT differs from isdigit, as follows: 274 - Its arg may be any int or unsigned int; it need not be an unsigned char. 275 - It's guaranteed to evaluate its argument exactly once. 276 - It's typically faster. 277 POSIX 1003.1-2001 says that only '0' through '9' are digits. 278 Prefer ISDIGIT to isdigit unless it's important to use the locale's 279 definition of `digit' even when the host does not conform to POSIX. */ 280#define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9) 281 282#include <errno.h> 283#if !STDC_HEADERS 284 extern int errno; 285#endif 286 287#include <signal.h> 288#ifndef SA_RESTART 289# ifdef SA_INTERRUPT /* e.g. SunOS 4.1.x */ 290# define SA_RESTART SA_INTERRUPT 291# else 292# define SA_RESTART 0 293# endif 294#endif 295#if !defined SIGCHLD && defined SIGCLD 296# define SIGCHLD SIGCLD 297#endif 298 299#undef MIN 300#undef MAX 301#define MIN(a, b) ((a) <= (b) ? (a) : (b)) 302#define MAX(a, b) ((a) >= (b) ? (a) : (b)) 303 304#if HAVE_STDBOOL_H 305# include <stdbool.h> 306#else 307# define bool unsigned char 308#endif 309 310#if HAVE_VFORK_H 311# include <vfork.h> 312#endif 313 314#if ! HAVE_WORKING_VFORK 315# define vfork fork 316#endif 317 318/* Type used for fast comparison of several bytes at a time. */ 319 320#ifndef word 321# define word uintmax_t 322#endif 323 324/* The integer type of a line number. Since files are read into main 325 memory, ptrdiff_t should be wide enough. */ 326 327typedef ptrdiff_t lin; 328#define LIN_MAX PTRDIFF_MAX 329verify (lin_is_signed, TYPE_SIGNED (lin)); 330verify (lin_is_wide_enough, sizeof (ptrdiff_t) <= sizeof (lin)); 331verify (lin_is_printable_as_long, sizeof (lin) <= sizeof (long)); 332 333/* This section contains POSIX-compliant defaults for macros 334 that are meant to be overridden by hand in config.h as needed. */ 335 336#ifndef file_name_cmp 337# define file_name_cmp strcmp 338#endif 339 340#ifndef initialize_main 341# define initialize_main(argcp, argvp) 342#endif 343 344#ifndef NULL_DEVICE 345# define NULL_DEVICE "/dev/null" 346#endif 347 348/* Do struct stat *S, *T describe the same special file? */ 349#ifndef same_special_file 350# if HAVE_ST_RDEV && defined S_ISBLK && defined S_ISCHR 351# define same_special_file(s, t) \ 352 (((S_ISBLK ((s)->st_mode) && S_ISBLK ((t)->st_mode)) \ 353 || (S_ISCHR ((s)->st_mode) && S_ISCHR ((t)->st_mode))) \ 354 && (s)->st_rdev == (t)->st_rdev) 355# else 356# define same_special_file(s, t) 0 357# endif 358#endif 359 360/* Do struct stat *S, *T describe the same file? Answer -1 if unknown. */ 361#ifndef same_file 362# define same_file(s, t) \ 363 ((((s)->st_ino == (t)->st_ino) && ((s)->st_dev == (t)->st_dev)) \ 364 || same_special_file (s, t)) 365#endif 366 367/* Do struct stat *S, *T have the same file attributes? 368 369 POSIX says that two files are identical if st_ino and st_dev are 370 the same, but many filesystems incorrectly assign the same (device, 371 inode) pair to two distinct files, including: 372 373 - GNU/Linux NFS servers that export all local filesystems as a 374 single NFS filesystem, if a local device number (st_dev) exceeds 375 255, or if a local inode number (st_ino) exceeds 16777215. 376 377 - Network Appliance NFS servers in snapshot directories; see 378 Network Appliance bug #195. 379 380 - ClearCase MVFS; see bug id ATRia04618. 381 382 Check whether two files that purport to be the same have the same 383 attributes, to work around instances of this common bug. Do not 384 inspect all attributes, only attributes useful in checking for this 385 bug. 386 387 It's possible for two distinct files on a buggy filesystem to have 388 the same attributes, but it's not worth slowing down all 389 implementations (or complicating the configuration) to cater to 390 these rare cases in buggy implementations. */ 391 392#ifndef same_file_attributes 393# define same_file_attributes(s, t) \ 394 ((s)->st_mode == (t)->st_mode \ 395 && (s)->st_nlink == (t)->st_nlink \ 396 && (s)->st_uid == (t)->st_uid \ 397 && (s)->st_gid == (t)->st_gid \ 398 && (s)->st_size == (t)->st_size \ 399 && (s)->st_mtime == (t)->st_mtime \ 400 && (s)->st_ctime == (t)->st_ctime) 401#endif 402