system.h revision 259065
11590Srgrimes/* System dependent declarations. 21590Srgrimes 31590Srgrimes Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1998, 2001, 2002, 41590Srgrimes 2004 Free Software Foundation, Inc. 51590Srgrimes 61590Srgrimes This file is part of GNU DIFF. 71590Srgrimes 81590Srgrimes GNU DIFF is free software; you can redistribute it and/or modify 91590Srgrimes it under the terms of the GNU General Public License as published by 101590Srgrimes the Free Software Foundation; either version 2, or (at your option) 111590Srgrimes any later version. 121590Srgrimes 131590Srgrimes GNU DIFF is distributed in the hope that it will be useful, 141590Srgrimes but WITHOUT ANY WARRANTY; without even the implied warranty of 151590Srgrimes MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 161590Srgrimes GNU General Public License for more details. 171590Srgrimes 181590Srgrimes You should have received a copy of the GNU General Public License 191590Srgrimes along with this program; see the file COPYING. 201590Srgrimes If not, write to the Free Software Foundation, 211590Srgrimes 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 221590Srgrimes 231590Srgrimes#include <config.h> 241590Srgrimes 251590Srgrimes/* Don't bother to support K&R C compilers any more; it's not worth 261590Srgrimes the trouble. These macros prevent some library modules from being 271590Srgrimes compiled in K&R C mode. */ 281590Srgrimes#define PARAMS(Args) Args 291590Srgrimes#define PROTOTYPES 1 301590Srgrimes 311590Srgrimes/* Define `__attribute__' and `volatile' first 321590Srgrimes so that they're used consistently in all system includes. */ 331590Srgrimes#if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 6) || __STRICT_ANSI__ 341590Srgrimes# define __attribute__(x) 351590Srgrimes#endif 361590Srgrimes#if defined const && !defined volatile 371590Srgrimes# define volatile 381590Srgrimes#endif 391590Srgrimes 401590Srgrimes/* Verify a requirement at compile-time (unlike assert, which is runtime). */ 411590Srgrimes#define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; } 421590Srgrimes 431590Srgrimes 441590Srgrimes/* Determine whether an integer type is signed, and its bounds. 451590Srgrimes This code assumes two's (or one's!) complement with no holes. */ 461590Srgrimes 471590Srgrimes/* The extra casts work around common compiler bugs, 481590Srgrimes e.g. Cray C 5.0.3.0 when t == time_t. */ 491590Srgrimes#ifndef TYPE_SIGNED 501590Srgrimes# define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) 511590Srgrimes#endif 521590Srgrimes#ifndef TYPE_MINIMUM 531590Srgrimes# define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \ 541590Srgrimes ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) \ 551590Srgrimes : (t) 0)) 561590Srgrimes#endif 571590Srgrimes#ifndef TYPE_MAXIMUM 581590Srgrimes# define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t))) 591590Srgrimes#endif 601590Srgrimes 611590Srgrimes#include <sys/types.h> 621590Srgrimes#include <sys/stat.h> 631590Srgrimes 641590Srgrimes#ifndef S_IRWXU 651590Srgrimes# define S_IRWXU 0700 661590Srgrimes#endif 671590Srgrimes#ifndef S_IRWXG 681590Srgrimes# define S_IRWXG 0070 691590Srgrimes#endif 701590Srgrimes#ifndef S_IRWXO 711590Srgrimes# define S_IRWXO 0007 721590Srgrimes#endif 731590Srgrimes 741590Srgrimes#if HAVE_UNISTD_H 751590Srgrimes# include <unistd.h> 761590Srgrimes#endif 771590Srgrimes 781590Srgrimes#ifndef SEEK_SET 791590Srgrimes# define SEEK_SET 0 801590Srgrimes#endif 811590Srgrimes#ifndef SEEK_CUR 821590Srgrimes# define SEEK_CUR 1 831590Srgrimes#endif 841590Srgrimes 851590Srgrimes#ifndef STDIN_FILENO 861590Srgrimes# define STDIN_FILENO 0 871590Srgrimes#endif 881590Srgrimes#ifndef STDOUT_FILENO 891590Srgrimes# define STDOUT_FILENO 1 908874Srgrimes#endif 911590Srgrimes#ifndef STDERR_FILENO 921590Srgrimes# define STDERR_FILENO 2 931590Srgrimes#endif 941590Srgrimes 951590Srgrimes#include <time.h> 961590Srgrimes 971590Srgrimes#if HAVE_FCNTL_H 981590Srgrimes# include <fcntl.h> 991590Srgrimes#else 1001590Srgrimes# if HAVE_SYS_FILE_H 1011590Srgrimes# include <sys/file.h> 1026776Sguido# endif 1036776Sguido#endif 1046776Sguido 1056776Sguido#if !HAVE_DUP2 1061590Srgrimes# define dup2(f, t) (close (t), fcntl (f, F_DUPFD, t)) 1071590Srgrimes#endif 1086776Sguido 1096776Sguido#ifndef O_RDONLY 1106776Sguido# define O_RDONLY 0 1111590Srgrimes#endif 1121590Srgrimes#ifndef O_RDWR 1138874Srgrimes# define O_RDWR 2 1141590Srgrimes#endif 1151590Srgrimes#ifndef S_IRUSR 1161590Srgrimes# define S_IRUSR 0400 1171590Srgrimes#endif 1181590Srgrimes#ifndef S_IWUSR 1191590Srgrimes# define S_IWUSR 0200 1201590Srgrimes#endif 1211590Srgrimes 1221590Srgrimes#if HAVE_SYS_WAIT_H 1231590Srgrimes# include <sys/wait.h> 1241590Srgrimes#endif 1251590Srgrimes#ifndef WEXITSTATUS 1261590Srgrimes# define WEXITSTATUS(stat_val) ((unsigned int) (stat_val) >> 8) 1271590Srgrimes#endif 1281590Srgrimes#ifndef WIFEXITED 1291590Srgrimes# define WIFEXITED(stat_val) (((stat_val) & 255) == 0) 1301590Srgrimes#endif 1311590Srgrimes 1321590Srgrimes#ifndef STAT_BLOCKSIZE 1331590Srgrimes# if HAVE_STRUCT_STAT_ST_BLKSIZE 1341590Srgrimes# define STAT_BLOCKSIZE(s) ((s).st_blksize) 1351590Srgrimes# else 1361590Srgrimes# define STAT_BLOCKSIZE(s) (8 * 1024) 1371590Srgrimes# endif 1381590Srgrimes#endif 1391590Srgrimes 1401590Srgrimes#if HAVE_DIRENT_H 1411590Srgrimes# include <dirent.h> 1428874Srgrimes# define NAMLEN(dirent) strlen ((dirent)->d_name) 1431590Srgrimes#else 1441590Srgrimes# define dirent direct 1451590Srgrimes# define NAMLEN(dirent) ((dirent)->d_namlen) 1461590Srgrimes# if HAVE_SYS_NDIR_H 1471590Srgrimes# include <sys/ndir.h> 1481590Srgrimes# endif 1491590Srgrimes# if HAVE_SYS_DIR_H 1501590Srgrimes# include <sys/dir.h> 1511590Srgrimes# endif 1521590Srgrimes# if HAVE_NDIR_H 1531590Srgrimes# include <ndir.h> 1541590Srgrimes# endif 1551590Srgrimes#endif 1561590Srgrimes 1571590Srgrimes#include <stdlib.h> 1588874Srgrimes#define EXIT_TROUBLE 2 1591590Srgrimes 1601590Srgrimes#include <limits.h> 1611590Srgrimes 1621590Srgrimes#if HAVE_INTTYPES_H 1631590Srgrimes# include <inttypes.h> 1641590Srgrimes#endif 1651590Srgrimes#ifndef PTRDIFF_MAX 1661590Srgrimes# define PTRDIFF_MAX TYPE_MAXIMUM (ptrdiff_t) 1671590Srgrimes#endif 1681590Srgrimes#ifndef SIZE_MAX 1691590Srgrimes# define SIZE_MAX TYPE_MAXIMUM (size_t) 1701590Srgrimes#endif 1711590Srgrimes#ifndef UINTMAX_MAX 1721590Srgrimes# define UINTMAX_MAX TYPE_MAXIMUM (uintmax_t) 1731590Srgrimes#endif 1741590Srgrimes#if ! HAVE_STRTOUMAX && ! defined strtoumax 1751590Srgrimesuintmax_t strtoumax (char const *, char **, int); 1761590Srgrimes#endif 1771590Srgrimes 1781590Srgrimes#include <stddef.h> 1791590Srgrimes 1801590Srgrimes#include <string.h> 1811590Srgrimes#if ! HAVE_STRCASECOLL 1821590Srgrimes# if HAVE_STRICOLL || defined stricoll 1831590Srgrimes# define strcasecoll(a, b) stricoll (a, b) 1841590Srgrimes# else 1851590Srgrimes# define strcasecoll(a, b) strcasecmp (a, b) /* best we can do */ 1861590Srgrimes# endif 1871590Srgrimes#endif 1888874Srgrimes#if ! (HAVE_STRCASECMP || defined strcasecmp) 1891590Srgrimesint strcasecmp (char const *, char const *); 1901590Srgrimes#endif 1911590Srgrimes 1921590Srgrimes#if HAVE_LOCALE_H 1931590Srgrimes# include <locale.h> 1941590Srgrimes#else 1951590Srgrimes# define setlocale(category, locale) 1961590Srgrimes#endif 1971590Srgrimes 1981590Srgrimes#include <gettext.h> 1991590Srgrimes 200#define _(msgid) gettext (msgid) 201#define N_(msgid) msgid 202 203#include <ctype.h> 204 205/* ISDIGIT differs from isdigit, as follows: 206 - Its arg may be any int or unsigned int; it need not be an unsigned char. 207 - It's guaranteed to evaluate its argument exactly once. 208 - It's typically faster. 209 POSIX 1003.1-2001 says that only '0' through '9' are digits. 210 Prefer ISDIGIT to isdigit unless it's important to use the locale's 211 definition of `digit' even when the host does not conform to POSIX. */ 212#define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9) 213 214#include <errno.h> 215 216#include <signal.h> 217#ifndef SA_RESTART 218# ifdef SA_INTERRUPT /* e.g. SunOS 4.1.x */ 219# define SA_RESTART SA_INTERRUPT 220# else 221# define SA_RESTART 0 222# endif 223#endif 224#if !defined SIGCHLD && defined SIGCLD 225# define SIGCHLD SIGCLD 226#endif 227 228#undef MIN 229#undef MAX 230#define MIN(a, b) ((a) <= (b) ? (a) : (b)) 231#define MAX(a, b) ((a) >= (b) ? (a) : (b)) 232 233#include <stdbool.h> 234 235#if HAVE_VFORK_H 236# include <vfork.h> 237#endif 238 239#if ! HAVE_WORKING_VFORK 240# define vfork fork 241#endif 242 243/* Type used for fast comparison of several bytes at a time. */ 244 245#ifndef word 246# define word uintmax_t 247#endif 248 249/* The integer type of a line number. Since files are read into main 250 memory, ptrdiff_t should be wide enough. */ 251 252typedef ptrdiff_t lin; 253#define LIN_MAX PTRDIFF_MAX 254verify (lin_is_signed, TYPE_SIGNED (lin)); 255verify (lin_is_wide_enough, sizeof (ptrdiff_t) <= sizeof (lin)); 256verify (lin_is_printable_as_long_int, sizeof (lin) <= sizeof (long int)); 257 258/* This section contains POSIX-compliant defaults for macros 259 that are meant to be overridden by hand in config.h as needed. */ 260 261#ifndef file_name_cmp 262# define file_name_cmp strcmp 263#endif 264 265#ifndef initialize_main 266# define initialize_main(argcp, argvp) 267#endif 268 269#ifndef NULL_DEVICE 270# define NULL_DEVICE "/dev/null" 271#endif 272 273/* Do struct stat *S, *T describe the same special file? */ 274#ifndef same_special_file 275# if HAVE_ST_RDEV && defined S_ISBLK && defined S_ISCHR 276# define same_special_file(s, t) \ 277 (((S_ISBLK ((s)->st_mode) && S_ISBLK ((t)->st_mode)) \ 278 || (S_ISCHR ((s)->st_mode) && S_ISCHR ((t)->st_mode))) \ 279 && (s)->st_rdev == (t)->st_rdev) 280# else 281# define same_special_file(s, t) 0 282# endif 283#endif 284 285/* Do struct stat *S, *T describe the same file? Answer -1 if unknown. */ 286#ifndef same_file 287# define same_file(s, t) \ 288 ((((s)->st_ino == (t)->st_ino) && ((s)->st_dev == (t)->st_dev)) \ 289 || same_special_file (s, t)) 290#endif 291 292/* Do struct stat *S, *T have the same file attributes? 293 294 POSIX says that two files are identical if st_ino and st_dev are 295 the same, but many filesystems incorrectly assign the same (device, 296 inode) pair to two distinct files, including: 297 298 - GNU/Linux NFS servers that export all local filesystems as a 299 single NFS filesystem, if a local device number (st_dev) exceeds 300 255, or if a local inode number (st_ino) exceeds 16777215. 301 302 - Network Appliance NFS servers in snapshot directories; see 303 Network Appliance bug #195. 304 305 - ClearCase MVFS; see bug id ATRia04618. 306 307 Check whether two files that purport to be the same have the same 308 attributes, to work around instances of this common bug. Do not 309 inspect all attributes, only attributes useful in checking for this 310 bug. 311 312 It's possible for two distinct files on a buggy filesystem to have 313 the same attributes, but it's not worth slowing down all 314 implementations (or complicating the configuration) to cater to 315 these rare cases in buggy implementations. */ 316 317#ifndef same_file_attributes 318# define same_file_attributes(s, t) \ 319 ((s)->st_mode == (t)->st_mode \ 320 && (s)->st_nlink == (t)->st_nlink \ 321 && (s)->st_uid == (t)->st_uid \ 322 && (s)->st_gid == (t)->st_gid \ 323 && (s)->st_size == (t)->st_size \ 324 && (s)->st_mtime == (t)->st_mtime \ 325 && (s)->st_ctime == (t)->st_ctime) 326#endif 327