1 /* @(#)common.h	1.30 20/02/13 2011-2020 J. Schilling */
2 /*
3  *	Copyright (c) 1986, 1988 Larry Wall
4  *	Copyright (c) 2011-2020 J. Schilling
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following condition is met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this condition and the following disclaimer.
11  *
12  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
13  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
16  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22  * SUCH DAMAGE.
23  */
24 
25 #define	DEBUGGING
26 
27 #include <schily/stdio.h>
28 #include <schily/types.h>
29 #include <schily/time.h>
30 #include <schily/stdlib.h>
31 #include <schily/unistd.h>
32 #include <schily/standard.h>
33 #include <schily/string.h>
34 #include <schily/stat.h>
35 #include <schily/fcntl.h>
36 #include <schily/assert.h>
37 #include <schily/ctype.h>
38 #include <schily/signal.h>
39 #include <schily/utypes.h>
40 #include <schily/errno.h>
41 #include <schily/nlsdefs.h>
42 #include <schily/schily.h>	/* e.g. ___malloc() */
43 
44 #define	CH	(char)
45 #define	UCH	(unsigned char)
46 #define	C	(char *)
47 #define	UC	(unsigned char *)
48 #define	CP	(char **)
49 #define	UCP	(unsigned char **)
50 #define	CPP	(char ***)
51 #define	UCPP	(unsigned char ***)
52 
53 #ifndef	NO_FLEXNAMES
54 /*
55  * FLEXFILENAMES:
56  *	This symbol, if defined, indicates that the system supports filenames
57  *	longer than 14 characters.
58  */
59 #define	FLEXFILENAMES
60 #endif
61 
62 /* shut lint up about the following when return value ignored */
63 
64 #define	Signal		(void)signal
65 #define	Unlink		(void)unlink
66 #define	Lseek		(void)lseek
67 #define	Fseek		(void)fseek
68 #define	Fstat		(void)fstat
69 #define	Pclose		(void)pclose
70 #define	Close		(void)close
71 #define	Fclose		(void)fclose
72 #define	Fflush		(void)fflush
73 #define	Sprintf		(void)sprintf
74 #define	Snprintf	(void)snprintf
75 #define	Mktemp		(void)mktemp
76 #define	Strcpy		(void)strcpy
77 #define	Strcat		(void)strcat
78 
79 /* constants */
80 
81 #define	INITHUNKMAX	125		/* initial dynamic allocation size */
82 #define	BUFFERSIZE	8192
83 #define	SCCSPREFIX	"s."
84 #define	GET		"get"		/* use: "get -e %s" */
85 #define	GETEDIT		"-e"
86 #define	RCSSUFFIX	",v"
87 #define	CHECKOUT	"co"		/* use: "co -l %s" */
88 #define	COEDIT		"-l"
89 
90 #ifdef FLEXFILENAMES
91 #define	ORIGEXT		".orig"
92 #define	REJEXT		".rej"
93 #else
94 #define	ORIGEXT		"~"
95 #define	REJEXT		"#"
96 #endif
97 
98 /* handy definitions */
99 
100 #define	Null(t)		((t)0)
101 #define	Nullch		Null(char *)
102 #define	Nullfp		Null(FILE *)
103 #define	Nulline		Null(LINENUM)
104 
105 #define	strNE(s1, s2)		(strcmp(s1, s2))
106 #define	strEQ(s1, s2)		(strcmp(s1, s2) == 0)
107 #define	strnNE(s1, s2, l)	(strncmp(s1, s2, l))
108 #define	strnEQ(s1, s2, l)	(strncmp(s1, s2, l) == 0)
109 
110 /* typedefs */
111 
112 typedef int	bool;
113 typedef off_t	LINENUM;		/* must be signed */
114 typedef unsigned MEM;			/* what to feed malloc */
115 
116 /* globals */
117 
118 EXT int Argc;				/* guess */
119 EXT char **Argv;
120 EXT int Argc_last;			/* for restarting plan_b */
121 EXT char **Argv_last;
122 
123 EXT struct stat file_stat;		/* file statistics area */
124 EXT int filemode;
125 EXT struct timespec filetime;
126 
127 EXT char *buf;				/* general purpose buffer */
128 EXT size_t bufsize;			/* current size of buf */
129 EXT FILE *ofp;				/* output file pointer */
130 EXT FILE *rejfp;			/* reject file pointer */
131 
132 EXT bool using_plan_a;			/* try to keep everything in memory */
133 EXT bool out_of_mem;			/* ran out of memory in plan a */
134 
135 #define	MAXFILEC 2
136 EXT int filec;				/* how many file arguments? */
137 EXT char *filearg[MAXFILEC];
138 EXT bool ok_to_create_file;
139 EXT bool is_null_time[2];
140 EXT char *bestguess;			/* guess at correct filename */
141 EXT time_t starttime;
142 
143 EXT char *outname;			/*   -o outname		*/
144 EXT char rejname[128];			/*   -r rejname		*/
145 
146 EXT char *origext;			/*   -b ext		*/
147 EXT char *origprae;
148 
149 EXT int	TMPDLEN;
150 EXT char *TMPDIR;
151 EXT char *TMPOUTNAME;
152 EXT char *TMPINNAME;
153 EXT char *TMPREJNAME;
154 EXT char *TMPPATNAME;
155 EXT bool toutkeep;
156 EXT bool trejkeep;
157 
158 EXT LINENUM last_offset;
159 #ifdef DEBUGGING
160 EXT int debug;				/*   -x#		*/
161 #endif
162 EXT bool force;				/*   -f			*/
163 EXT bool verbose;			/* ! -s			*/
164 EXT bool reverse;			/*   -R			*/
165 EXT bool noreverse;			/*   -N			*/
166 EXT bool skip_rest_of_patch;		/*   -S			*/
167 EXT bool touch_local;			/*   -T			*/
168 EXT bool touch_gmt;			/*   -Z			*/
169 EXT int strippath;			/*   -p#		*/
170 EXT bool canonicalize;			/*   -l			*/
171 
172 EXT bool do_posix;			/* POSIX vers. old patch behavior */
173 EXT bool do_wall;			/* Old Larry patch-2.0 compatibility */
174 EXT bool wall_plus;			/* Permit enhancements from old patch */
175 EXT bool do_backup;			/* Create backup from original file */
176 
177 #define	CONTEXT_DIFF		1
178 #define	NORMAL_DIFF		2
179 #define	ED_DIFF			3
180 #define	NEW_CONTEXT_DIFF	4
181 #define	UNI_DIFF		5
182 
183 #define	EXIT_OK			0
184 #define	EXIT_REJECT		1
185 #define	EXIT_FAIL		2
186 #define	EXIT_SIGNAL		3
187 
188 EXT int diff_type;			/*   -c/-e/-n/-u	*/
189 
190 EXT char *revision;			/* prerequisite revision, if any */
191 
192 extern	void	my_exit __PR((int status));
193 
194 #ifdef	HAVE_GETDELIM
195 #define	fgetaline(f, bufp, lenp)	getdelim(bufp, lenp, '\n', f)
196 #endif
197