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