Annotation of src/usr.bin/patch/inp.c, Revision 1.3
1.3 ! deraadt 1: /* $OpenBSD: inp.c,v 1.2 1996/06/10 11:21:28 niklas Exp $ */
1.2 niklas 2:
1.1 deraadt 3: #ifndef lint
1.3 ! deraadt 4: static char rcsid[] = "$OpenBSD: inp.c,v 1.2 1996/06/10 11:21:28 niklas Exp $";
1.1 deraadt 5: #endif /* not lint */
6:
7: #include "EXTERN.h"
8: #include "common.h"
9: #include "util.h"
10: #include "pch.h"
11: #include "INTERN.h"
12: #include "inp.h"
13:
14: /* Input-file-with-indexable-lines abstract type */
15:
16: static long i_size; /* size of the input file */
17: static char *i_womp; /* plan a buffer for entire file */
18: static char **i_ptr; /* pointers to lines in i_womp */
19:
20: static int tifd = -1; /* plan b virtual string array */
21: static char *tibuf[2]; /* plan b buffers */
22: static LINENUM tiline[2] = {-1, -1}; /* 1st line in each buffer */
23: static LINENUM lines_per_buf; /* how many lines per buffer */
24: static int tireclen; /* length of records in tmp file */
25:
26: /* New patch--prepare to edit another file. */
27:
28: void
29: re_input()
30: {
31: if (using_plan_a) {
32: i_size = 0;
33: #ifndef lint
34: if (i_ptr != Null(char**))
35: free((char *)i_ptr);
36: #endif
37: if (i_womp != Nullch)
38: free(i_womp);
39: i_womp = Nullch;
40: i_ptr = Null(char **);
41: }
42: else {
43: using_plan_a = TRUE; /* maybe the next one is smaller */
44: Close(tifd);
45: tifd = -1;
46: free(tibuf[0]);
47: free(tibuf[1]);
48: tibuf[0] = tibuf[1] = Nullch;
49: tiline[0] = tiline[1] = -1;
50: tireclen = 0;
51: }
52: }
53:
54: /* Constuct the line index, somehow or other. */
55:
56: void
57: scan_input(filename)
58: char *filename;
59: {
60: if (!plan_a(filename))
61: plan_b(filename);
62: if (verbose) {
63: say3("Patching file %s using Plan %s...\n", filename,
64: (using_plan_a ? "A" : "B") );
65: }
66: }
67:
68: /* Try keeping everything in memory. */
69:
70: bool
71: plan_a(filename)
72: char *filename;
73: {
74: int ifd, statfailed;
75: Reg1 char *s;
76: Reg2 LINENUM iline;
77: char lbuf[MAXLINELEN];
78:
79: statfailed = stat(filename, &filestat);
80: if (statfailed && ok_to_create_file) {
81: if (verbose)
82: say2("(Creating file %s...)\n",filename);
83: makedirs(filename, TRUE);
84: close(creat(filename, 0666));
85: statfailed = stat(filename, &filestat);
86: }
87: /* For nonexistent or read-only files, look for RCS or SCCS versions. */
88: if (statfailed
89: /* No one can write to it. */
90: || (filestat.st_mode & 0222) == 0
91: /* I can't write to it. */
92: || ((filestat.st_mode & 0022) == 0 && filestat.st_uid != myuid)) {
93: struct stat cstat;
94: char *cs = Nullch;
95: char *filebase;
96: int pathlen;
97:
98: filebase = basename(filename);
99: pathlen = filebase - filename;
100:
101: /* Put any leading path into `s'.
102: Leave room in lbuf for the diff command. */
103: s = lbuf + 20;
104: strncpy(s, filename, pathlen);
105:
106: #define try(f, a1, a2) (Sprintf(s + pathlen, f, a1, a2), stat(s, &cstat) == 0)
107: if ( try("RCS/%s%s", filebase, RCSSUFFIX)
108: || try("RCS/%s" , filebase, 0)
109: || try( "%s%s", filebase, RCSSUFFIX)) {
110: Sprintf(buf, CHECKOUT, filename);
111: Sprintf(lbuf, RCSDIFF, filename);
112: cs = "RCS";
113: } else if ( try("SCCS/%s%s", SCCSPREFIX, filebase)
114: || try( "%s%s", SCCSPREFIX, filebase)) {
115: Sprintf(buf, GET, s);
116: Sprintf(lbuf, SCCSDIFF, s, filename);
117: cs = "SCCS";
118: } else if (statfailed)
119: fatal2("can't find %s\n", filename);
120: /* else we can't write to it but it's not under a version
121: control system, so just proceed. */
122: if (cs) {
123: if (!statfailed) {
124: if ((filestat.st_mode & 0222) != 0)
125: /* The owner can write to it. */
126: fatal3("file %s seems to be locked by somebody else under %s\n",
127: filename, cs);
128: /* It might be checked out unlocked. See if it's safe to
129: check out the default version locked. */
130: if (verbose)
131: say3("Comparing file %s to default %s version...\n",
132: filename, cs);
133: if (system(lbuf))
134: fatal3("can't check out file %s: differs from default %s version\n",
135: filename, cs);
136: }
137: if (verbose)
138: say3("Checking out file %s from %s...\n", filename, cs);
139: if (system(buf) || stat(filename, &filestat))
140: fatal3("can't check out file %s from %s\n", filename, cs);
141: }
142: }
143: filemode = filestat.st_mode;
144: if (!S_ISREG(filemode))
145: fatal2("%s is not a normal file--can't patch\n", filename);
146: i_size = filestat.st_size;
147: if (out_of_mem) {
148: set_hunkmax(); /* make sure dynamic arrays are allocated */
149: out_of_mem = FALSE;
150: return FALSE; /* force plan b because plan a bombed */
151: }
152: #ifdef lint
153: i_womp = Nullch;
154: #else
155: i_womp = malloc((MEM)(i_size+2)); /* lint says this may alloc less than */
156: /* i_size, but that's okay, I think. */
157: #endif
158: if (i_womp == Nullch)
159: return FALSE;
160: if ((ifd = open(filename, 0)) < 0)
161: pfatal2("can't open file %s", filename);
162: #ifndef lint
163: if (read(ifd, i_womp, (int)i_size) != i_size) {
164: Close(ifd); /* probably means i_size > 15 or 16 bits worth */
165: free(i_womp); /* at this point it doesn't matter if i_womp was */
166: return FALSE; /* undersized. */
167: }
168: #endif
169: Close(ifd);
170: if (i_size && i_womp[i_size-1] != '\n')
171: i_womp[i_size++] = '\n';
172: i_womp[i_size] = '\0';
173:
174: /* count the lines in the buffer so we know how many pointers we need */
175:
176: iline = 0;
177: for (s=i_womp; *s; s++) {
178: if (*s == '\n')
179: iline++;
180: }
181: #ifdef lint
182: i_ptr = Null(char**);
183: #else
184: i_ptr = (char **)malloc((MEM)((iline + 2) * sizeof(char *)));
185: #endif
186: if (i_ptr == Null(char **)) { /* shucks, it was a near thing */
187: free((char *)i_womp);
188: return FALSE;
189: }
190:
191: /* now scan the buffer and build pointer array */
192:
193: iline = 1;
194: i_ptr[iline] = i_womp;
195: for (s=i_womp; *s; s++) {
196: if (*s == '\n')
197: i_ptr[++iline] = s+1; /* these are NOT null terminated */
198: }
199: input_lines = iline - 1;
200:
201: /* now check for revision, if any */
202:
203: if (revision != Nullch) {
204: if (!rev_in_string(i_womp)) {
205: if (force) {
206: if (verbose)
207: say2(
208: "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
209: revision);
210: }
211: else if (batch) {
212: fatal2(
213: "this file doesn't appear to be the %s version--aborting.\n", revision);
214: }
215: else {
216: ask2(
217: "This file doesn't appear to be the %s version--patch anyway? [n] ",
218: revision);
219: if (*buf != 'y')
220: fatal1("aborted\n");
221: }
222: }
223: else if (verbose)
224: say2("Good. This file appears to be the %s version.\n",
225: revision);
226: }
227: return TRUE; /* plan a will work */
228: }
229:
230: /* Keep (virtually) nothing in memory. */
231:
232: void
233: plan_b(filename)
234: char *filename;
235: {
236: Reg3 FILE *ifp;
237: Reg1 int i = 0;
238: Reg2 int maxlen = 1;
239: Reg4 bool found_revision = (revision == Nullch);
240:
241: using_plan_a = FALSE;
242: if ((ifp = fopen(filename, "r")) == Nullfp)
243: pfatal2("can't open file %s", filename);
1.3 ! deraadt 244: if ((tifd = open(TMPINNAME, O_EXCL|O_CREAT|O_WRONLY, 0666)) < 0)
1.1 deraadt 245: pfatal2("can't open file %s", TMPINNAME);
246: while (fgets(buf, sizeof buf, ifp) != Nullch) {
247: if (revision != Nullch && !found_revision && rev_in_string(buf))
248: found_revision = TRUE;
249: if ((i = strlen(buf)) > maxlen)
250: maxlen = i; /* find longest line */
251: }
252: if (revision != Nullch) {
253: if (!found_revision) {
254: if (force) {
255: if (verbose)
256: say2(
257: "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
258: revision);
259: }
260: else if (batch) {
261: fatal2(
262: "this file doesn't appear to be the %s version--aborting.\n", revision);
263: }
264: else {
265: ask2(
266: "This file doesn't appear to be the %s version--patch anyway? [n] ",
267: revision);
268: if (*buf != 'y')
269: fatal1("aborted\n");
270: }
271: }
272: else if (verbose)
273: say2("Good. This file appears to be the %s version.\n",
274: revision);
275: }
276: Fseek(ifp, 0L, 0); /* rewind file */
277: lines_per_buf = BUFFERSIZE / maxlen;
278: tireclen = maxlen;
279: tibuf[0] = malloc((MEM)(BUFFERSIZE + 1));
280: tibuf[1] = malloc((MEM)(BUFFERSIZE + 1));
281: if (tibuf[1] == Nullch)
282: fatal1("out of memory\n");
283: for (i=1; ; i++) {
284: if (! (i % lines_per_buf)) /* new block */
285: if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
286: pfatal1("can't write temp file");
287: if (fgets(tibuf[0] + maxlen * (i%lines_per_buf), maxlen + 1, ifp)
288: == Nullch) {
289: input_lines = i - 1;
290: if (i % lines_per_buf)
291: if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
292: pfatal1("can't write temp file");
293: break;
294: }
295: }
296: Fclose(ifp);
297: Close(tifd);
298: if ((tifd = open(TMPINNAME, 0)) < 0) {
299: pfatal2("can't reopen file %s", TMPINNAME);
300: }
301: }
302:
303: /* Fetch a line from the input file, \n terminated, not necessarily \0. */
304:
305: char *
306: ifetch(line,whichbuf)
307: Reg1 LINENUM line;
308: int whichbuf; /* ignored when file in memory */
309: {
310: if (line < 1 || line > input_lines)
311: return "";
312: if (using_plan_a)
313: return i_ptr[line];
314: else {
315: LINENUM offline = line % lines_per_buf;
316: LINENUM baseline = line - offline;
317:
318: if (tiline[0] == baseline)
319: whichbuf = 0;
320: else if (tiline[1] == baseline)
321: whichbuf = 1;
322: else {
323: tiline[whichbuf] = baseline;
324: #ifndef lint /* complains of long accuracy */
325: Lseek(tifd, (long)baseline / lines_per_buf * BUFFERSIZE, 0);
326: #endif
327: if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
328: pfatal2("error reading tmp file %s", TMPINNAME);
329: }
330: return tibuf[whichbuf] + (tireclen*offline);
331: }
332: }
333:
334: /* True if the string argument contains the revision number we want. */
335:
336: bool
337: rev_in_string(string)
338: char *string;
339: {
340: Reg1 char *s;
341: Reg2 int patlen;
342:
343: if (revision == Nullch)
344: return TRUE;
345: patlen = strlen(revision);
346: if (strnEQ(string,revision,patlen) && isspace(string[patlen]))
347: return TRUE;
348: for (s = string; *s; s++) {
349: if (isspace(*s) && strnEQ(s+1, revision, patlen) &&
350: isspace(s[patlen+1] )) {
351: return TRUE;
352: }
353: }
354: return FALSE;
355: }