150488Speter/* $FreeBSD$ 232672Sache * 332672Sache * $Log: common.h,v $ 432672Sache * Revision 2.0.1.2 88/06/22 20:44:53 lwall 532672Sache * patch12: sprintf was declared wrong 632672Sache * 732672Sache * Revision 2.0.1.1 88/06/03 15:01:56 lwall 832672Sache * patch10: support for shorter extensions. 932672Sache * 1032672Sache * Revision 2.0 86/09/17 15:36:39 lwall 1132672Sache * Baseline for netwide release. 1232672Sache * 1332672Sache */ 1432672Sache 1532672Sache#define DEBUGGING 1632672Sache 1732672Sache#define VOIDUSED 7 1832672Sache#include "config.h" 1932672Sache 2032672Sache/* shut lint up about the following when return value ignored */ 2132672Sache 2232672Sache#define Signal (void)signal 2332672Sache#define Unlink (void)unlink 2432672Sache#define Lseek (void)lseek 2532672Sache#define Fseek (void)fseek 2632672Sache#define Fstat (void)fstat 2732672Sache#define Pclose (void)pclose 2832672Sache#define Close (void)close 2932672Sache#define Fclose (void)fclose 3032672Sache#define Fflush (void)fflush 3132672Sache#define Sprintf (void)sprintf 32105017Skris#define Snprintf (void)snprintf 3332672Sache#define Mktemp (void)mktemp 3432672Sache#define Strcpy (void)strcpy 3532672Sache#define Strcat (void)strcat 36105017Skris#define Strlcpy (void)strlcpy 37197259Ssepotvin#define Strncpy (void)strncpy 38105017Skris#define Strlcat (void)strlcat 3932672Sache 4032672Sache/* NeXT declares malloc and realloc incompatibly from us in some of 4132672Sache these files. Temporarily redefine them to prevent errors. */ 4232672Sache#define malloc system_malloc 4332672Sache#define realloc system_realloc 4432672Sache#include <stdio.h> 4532672Sache#include <assert.h> 4632672Sache#include <sys/types.h> 4732672Sache#include <sys/stat.h> 4832672Sache#include <ctype.h> 4932672Sache#include <signal.h> 5032672Sache#undef malloc 5132672Sache#undef realloc 5232672Sache 5332672Sache/* constants */ 5432672Sache 5532672Sache/* AIX predefines these. */ 5632672Sache#ifdef TRUE 5732672Sache#undef TRUE 5832672Sache#endif 5932672Sache#ifdef FALSE 6032672Sache#undef FALSE 6132672Sache#endif 6232672Sache#define TRUE (1) 6332672Sache#define FALSE (0) 6432672Sache 6532672Sache#define MAXHUNKSIZE 200000 /* is this enough lines? */ 6632672Sache#define INITHUNKMAX 125 /* initial dynamic allocation size */ 67191910Ssepotvin#define INITLINELEN 4096 6832672Sache#define BUFFERSIZE 4096 6932672Sache 7032672Sache#define SCCSPREFIX "s." 7132672Sache#define GET "get %s" 7232672Sache#define GET_LOCKED "get -e %s" 7332672Sache#define SCCSDIFF "get -p %s | diff - %s >/dev/null" 7432672Sache 7532672Sache#define RCSSUFFIX ",v" 7632672Sache#define CHECKOUT "co %s" 7732672Sache#define CHECKOUT_LOCKED "co -l %s" 7832672Sache#define RCSDIFF "rcsdiff %s > /dev/null" 7932672Sache 8032672Sache/* handy definitions */ 8132672Sache 8232672Sache#define Null(t) ((t)0) 8332672Sache#define Nullch Null(char *) 8432672Sache#define Nullfp Null(FILE *) 8532672Sache#define Nulline Null(LINENUM) 8632672Sache 8732672Sache#define Ctl(ch) ((ch) & 037) 8832672Sache 8932672Sache#define strNE(s1,s2) (strcmp(s1, s2)) 9032672Sache#define strEQ(s1,s2) (!strcmp(s1, s2)) 9132672Sache#define strnNE(s1,s2,l) (strncmp(s1, s2, l)) 9232672Sache#define strnEQ(s1,s2,l) (!strncmp(s1, s2, l)) 9332672Sache 9432672Sache/* typedefs */ 9532672Sache 9632672Sachetypedef char bool; 9732672Sachetypedef long LINENUM; /* must be signed */ 9832672Sachetypedef unsigned MEM; /* what to feed malloc */ 9932672Sache 10032672Sache/* globals */ 10132672Sache 10232672SacheEXT int Argc; /* guess */ 10332672SacheEXT char **Argv; 10432672SacheEXT int optind_last; /* for restarting plan_b */ 10532672Sache 10632672SacheEXT struct stat filestat; /* file statistics area */ 10732672SacheEXT int filemode INIT(0644); 10832672Sache 109191910SsepotvinEXT char *buf; /* general purpose buffer */ 110191910SsepotvinEXT size_t buf_size; /* size of the general purpose buffer */ 11132672SacheEXT FILE *ofp INIT(Nullfp); /* output file pointer */ 11232672SacheEXT FILE *rejfp INIT(Nullfp); /* reject file pointer */ 11332672Sache 11432672SacheEXT int myuid; /* cache getuid return value */ 11532672Sache 11632672SacheEXT bool using_plan_a INIT(TRUE); /* try to keep everything in memory */ 11732672SacheEXT bool out_of_mem INIT(FALSE); /* ran out of memory in plan a */ 11832672Sache 11932672Sache#define MAXFILEC 2 12032672SacheEXT int filec INIT(0); /* how many file arguments? */ 12132672SacheEXT char *filearg[MAXFILEC]; 12232672SacheEXT bool ok_to_create_file INIT(FALSE); 12332672SacheEXT char *bestguess INIT(Nullch); /* guess at correct filename */ 12432672Sache 12532672SacheEXT char *outname INIT(Nullch); 12632672SacheEXT char rejname[128]; 12732672Sache 12832672SacheEXT char *origprae INIT(Nullch); 12932672Sache 13032672SacheEXT char *TMPOUTNAME; 13132672SacheEXT char *TMPINNAME; 13232672SacheEXT char *TMPREJNAME; 13332672SacheEXT char *TMPPATNAME; 13432672SacheEXT bool toutkeep INIT(FALSE); 13532672SacheEXT bool trejkeep INIT(FALSE); 13632672Sache 13732672SacheEXT LINENUM last_offset INIT(0); 13832672Sache#ifdef DEBUGGING 13932672SacheEXT int debug INIT(0); 14032672Sache#endif 14132672SacheEXT LINENUM maxfuzz INIT(2); 14232672SacheEXT bool force INIT(FALSE); 14332672SacheEXT bool batch INIT(FALSE); 14432672SacheEXT bool verbose INIT(TRUE); 14532672SacheEXT bool reverse INIT(FALSE); 14632672SacheEXT bool noreverse INIT(FALSE); 14732672SacheEXT bool skip_rest_of_patch INIT(FALSE); 14832672SacheEXT int strippath INIT(957); 14932672SacheEXT bool canonicalize INIT(FALSE); 15032672Sache 15132672Sache#define CONTEXT_DIFF 1 15232672Sache#define NORMAL_DIFF 2 15332672Sache#define ED_DIFF 3 15432672Sache#define NEW_CONTEXT_DIFF 4 15532672Sache#define UNI_DIFF 5 15632672SacheEXT int diff_type INIT(0); 15732672Sache 15832672SacheEXT bool do_defines INIT(FALSE); /* patch using ifdef, ifndef, etc. */ 15932672SacheEXT char if_defined[128]; /* #ifdef xyzzy */ 16032672SacheEXT char not_defined[128]; /* #ifndef xyzzy */ 16132672SacheEXT char else_defined[] INIT("#else\n");/* #else */ 16232672SacheEXT char end_defined[128]; /* #endif xyzzy */ 16332672Sache 16432672SacheEXT char *revision INIT(Nullch); /* prerequisite revision, if any */ 16532672Sache 16632672Sache#include <errno.h> 16732672Sache#ifdef STDC_HEADERS 16832672Sache#include <stdlib.h> 16932672Sache#include <string.h> 17032672Sache#else 17132672Sacheextern int errno; 17232672SacheFILE *popen(); 17332672Sachechar *malloc(); 17432672Sachechar *realloc(); 17532672Sachelong atol(); 17632672Sachechar *getenv(); 17732672Sachechar *strcpy(); 17832672Sachechar *strcat(); 17932672Sache#endif 18095601Sgadchar *mktemp(char *); 18132672Sache#ifdef HAVE_UNISTD_H 18232672Sache#include <unistd.h> 18332672Sache#else 18432672Sachelong lseek(); 18532672Sache#endif 18632672Sache#if defined(_POSIX_VERSION) || defined(HAVE_FCNTL_H) 18732672Sache#include <fcntl.h> 18832672Sache#endif 18932672Sache 19032672Sache#if !defined(S_ISDIR) && defined(S_IFDIR) 19132672Sache#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) 19232672Sache#endif 19332672Sache#if !defined(S_ISREG) && defined(S_IFREG) 19432672Sache#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) 19532672Sache#endif 196