1/* common definitions for `patch' */ 2 3/* $Id: common.h 8008 2004-06-16 21:22:10Z korli $ */ 4 5/* Copyright 1986, 1988 Larry Wall 6 Copyright 1990, 1991-1993, 1997-1998, 1999 Free Software Foundation, Inc. 7 8 This program 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 This program 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. 16 See the 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#ifndef DEBUGGING 24#define DEBUGGING 1 25#endif 26 27/* We must define `volatile' and `const' first (the latter inside config.h), 28 so that they're used consistently in all system includes. */ 29#ifndef __STDC__ 30# ifndef volatile 31# define volatile 32# endif 33#endif 34 35#include <config.h> 36 37#include <assert.h> 38#include <stdio.h> 39#include <sys/types.h> 40#include <time.h> 41 42#include <sys/stat.h> 43#if ! defined S_ISDIR && defined S_IFDIR 44# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) 45#endif 46#if ! defined S_ISREG && defined S_IFREG 47# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) 48#endif 49#ifndef S_IXOTH 50#define S_IXOTH 1 51#endif 52#ifndef S_IWOTH 53#define S_IWOTH 2 54#endif 55#ifndef S_IROTH 56#define S_IROTH 4 57#endif 58#ifndef S_IXGRP 59#define S_IXGRP (S_IXOTH << 3) 60#endif 61#ifndef S_IWGRP 62#define S_IWGRP (S_IWOTH << 3) 63#endif 64#ifndef S_IRGRP 65#define S_IRGRP (S_IROTH << 3) 66#endif 67#ifndef S_IXUSR 68#define S_IXUSR (S_IXOTH << 6) 69#endif 70#ifndef S_IWUSR 71#define S_IWUSR (S_IWOTH << 6) 72#endif 73#ifndef S_IRUSR 74#define S_IRUSR (S_IROTH << 6) 75#endif 76 77#if HAVE_LIMITS_H 78# include <limits.h> 79#endif 80#ifndef CHAR_BIT 81#define CHAR_BIT 8 82#endif 83/* The extra casts work around common compiler bugs, 84 e.g. Cray C 5.0.3.0 time_t. */ 85#define TYPE_SIGNED(t) ((t) -1 < (t) 0) 86#define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \ 87 ? (t) (~ (t) 0 << (sizeof (t) * CHAR_BIT - 1)) \ 88 : (t) 0)) 89#define TYPE_MAXIMUM(t) ((t) ((t) ~ (t) 0 - TYPE_MINIMUM (t))) 90#ifndef CHAR_MAX 91#define CHAR_MAX TYPE_MAXIMUM (char) 92#endif 93#ifndef INT_MAX 94#define INT_MAX TYPE_MAXIMUM (int) 95#endif 96#ifndef LONG_MIN 97#define LONG_MIN TYPE_MINIMUM (long) 98#endif 99 100#if HAVE_INTTYPES_H 101# include <inttypes.h> 102#endif 103#ifndef SIZE_MAX 104/* On some nonstandard hosts, size_t is signed, 105 so SIZE_MAX != (size_t) -1. */ 106#define SIZE_MAX TYPE_MAXIMUM (size_t) 107#endif 108 109#include <ctype.h> 110/* CTYPE_DOMAIN (C) is nonzero if the unsigned char C can safely be given 111 as an argument to <ctype.h> macros like `isspace'. */ 112#if STDC_HEADERS 113#define CTYPE_DOMAIN(c) 1 114#else 115#define CTYPE_DOMAIN(c) ((unsigned) (c) <= 0177) 116#endif 117#ifndef ISSPACE 118#define ISSPACE(c) (CTYPE_DOMAIN (c) && isspace (c)) 119#endif 120 121#ifndef ISDIGIT 122#define ISDIGIT(c) ((unsigned) (c) - '0' <= 9) 123#endif 124 125 126#ifndef FILESYSTEM_PREFIX_LEN 127#define FILESYSTEM_PREFIX_LEN(f) 0 128#endif 129 130#ifndef ISSLASH 131#define ISSLASH(c) ((c) == '/') 132#endif 133 134 135/* constants */ 136 137/* AIX predefines these. */ 138#ifdef TRUE 139#undef TRUE 140#endif 141#ifdef FALSE 142#undef FALSE 143#endif 144#define TRUE 1 145#define FALSE 0 146 147/* handy definitions */ 148 149#define strEQ(s1,s2) (!strcmp(s1, s2)) 150#define strnEQ(s1,s2,l) (!strncmp(s1, s2, l)) 151 152/* typedefs */ 153 154typedef int bool; /* must promote to itself */ 155typedef off_t LINENUM; /* must be signed */ 156 157/* globals */ 158 159extern char const program_name[]; 160 161XTERN char *buf; /* general purpose buffer */ 162XTERN size_t bufsize; /* allocated size of buf */ 163 164XTERN bool using_plan_a; /* try to keep everything in memory */ 165 166XTERN char *inname; 167XTERN char *outfile; 168XTERN int inerrno; 169XTERN int invc; 170XTERN struct stat instat; 171XTERN bool dry_run; 172XTERN bool posixly_correct; 173 174XTERN char const *origprae; 175XTERN char const *origbase; 176 177XTERN char const * volatile TMPINNAME; 178XTERN char const * volatile TMPOUTNAME; 179XTERN char const * volatile TMPPATNAME; 180 181XTERN int volatile TMPINNAME_needs_removal; 182XTERN int volatile TMPOUTNAME_needs_removal; 183XTERN int volatile TMPPATNAME_needs_removal; 184 185#ifdef DEBUGGING 186XTERN int debug; 187#else 188# define debug 0 189#endif 190XTERN bool force; 191XTERN bool batch; 192XTERN bool noreverse; 193XTERN int reverse; 194XTERN enum { DEFAULT_VERBOSITY, SILENT, VERBOSE } verbosity; 195XTERN bool skip_rest_of_patch; 196XTERN int strippath; 197XTERN bool canonicalize; 198XTERN int patch_get; 199XTERN int set_time; 200XTERN int set_utc; 201 202enum diff 203 { 204 NO_DIFF, 205 CONTEXT_DIFF, 206 NORMAL_DIFF, 207 ED_DIFF, 208 NEW_CONTEXT_DIFF, 209 UNI_DIFF 210 }; 211 212XTERN enum diff diff_type; 213 214XTERN char *revision; /* prerequisite revision, if any */ 215 216#ifdef __STDC__ 217# define GENERIC_OBJECT void 218#else 219# define GENERIC_OBJECT char 220#endif 221 222#if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 6) || __STRICT_ANSI__ 223# define __attribute__(x) 224#endif 225 226#ifndef PARAMS 227# ifdef __STDC__ 228# define PARAMS(args) args 229# else 230# define PARAMS(args) () 231# endif 232#endif 233 234void fatal_exit PARAMS ((int)) __attribute__ ((noreturn)); 235 236#include <errno.h> 237#if !STDC_HEADERS && !defined errno 238extern int errno; 239#endif 240 241#if STDC_HEADERS || HAVE_STRING_H 242# include <string.h> 243#else 244# if !HAVE_MEMCHR 245# define memcmp(s1, s2, n) bcmp (s1, s2, n) 246# define memcpy(d, s, n) bcopy (s, d, n) 247GENERIC_OBJECT *memchr (); 248# endif 249#endif 250 251#if STDC_HEADERS 252# include <stdlib.h> 253#else 254char *getenv (); 255GENERIC_OBJECT *malloc (); 256GENERIC_OBJECT *realloc (); 257#endif 258 259#if HAVE_UNISTD_H 260# include <unistd.h> 261#else 262# ifndef lseek 263 off_t lseek (); 264# endif 265#endif 266#ifndef SEEK_SET 267#define SEEK_SET 0 268#endif 269#ifndef STDIN_FILENO 270#define STDIN_FILENO 0 271#endif 272#ifndef STDOUT_FILENO 273#define STDOUT_FILENO 1 274#endif 275#ifndef STDERR_FILENO 276#define STDERR_FILENO 2 277#endif 278#if HAVE_FSEEKO 279 typedef off_t file_offset; 280# define file_seek fseeko 281# define file_tell ftello 282#else 283 typedef long file_offset; 284# define file_seek fseek 285# define file_tell ftell 286#endif 287 288#if HAVE_FCNTL_H 289# include <fcntl.h> 290#endif 291#ifndef O_RDONLY 292#define O_RDONLY 0 293#endif 294#ifndef O_WRONLY 295#define O_WRONLY 1 296#endif 297#ifndef O_RDWR 298#define O_RDWR 2 299#endif 300#ifndef _O_BINARY 301#define _O_BINARY 0 302#endif 303#ifndef O_BINARY 304#define O_BINARY _O_BINARY 305#endif 306#ifndef O_CREAT 307#define O_CREAT 0 308#endif 309#ifndef O_EXCL 310#define O_EXCL 0 311#endif 312#ifndef O_TRUNC 313#define O_TRUNC 0 314#endif 315 316#if HAVE_SETMODE 317 XTERN int binary_transput; /* O_BINARY if binary i/o is desired */ 318#else 319# define binary_transput 0 320#endif 321 322#ifndef NULL_DEVICE 323#define NULL_DEVICE "/dev/null" 324#endif 325 326#ifndef TTY_DEVICE 327#define TTY_DEVICE "/dev/tty" 328#endif 329