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