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