4 * Revision 2.0.1.1 88/06/03 15:06:13 lwall
5 * patch10: made a little smarter about sccs files
7 * Revision 2.0 86/09/17 15:37:02 lwall
8 * Baseline for netwide release.
19 /* Input-file-with-indexable-lines abstract type. */
21 static long i_size; /* Size of the input file */
22 static char *i_womp; /* Plan a buffer for entire file */
23 static char **i_ptr; /* Pointers to lines in i_womp */
25 static int tifd = -1; /* Plan b virtual string array */
26 static char *tibuf[2]; /* Plan b buffers */
27 static LINENUM tiline[2] = {-1, -1}; /* 1st line in each buffer */
28 static LINENUM lines_per_buf; /* How many lines per buffer */
29 static int tireclen; /* Length of records in tmp file */
32 * New patch--prepare to edit another file.
40 if (i_ptr != Null(char**))
46 i_ptr = Null(char **);
48 using_plan_a = TRUE; /* maybe the next one is smaller */
53 tibuf[0] = tibuf[1] = Nullch;
54 tiline[0] = tiline[1] = -1;
60 * Constuct the line index, somehow or other.
63 scan_input(char *filename)
65 if (!plan_a(filename))
68 say3("Patching file %s using Plan %s...\n", filename,
69 (using_plan_a ? "A" : "B") );
74 * Try keeping everything in memory.
77 plan_a(char *filename)
82 char lbuf[MAXLINELEN];
83 int output_elsewhere = strcmp(filename, outname);
84 extern int check_patch;
86 statfailed = stat(filename, &filestat);
87 if (statfailed && ok_to_create_file) {
89 say2("(Creating file %s...)\n",filename);
92 makedirs(filename, TRUE);
93 close(creat(filename, 0666));
94 statfailed = stat(filename, &filestat);
96 if (statfailed && check_patch) {
97 fatal2("%s not found and in check_patch mode.", filename);
100 * For nonexistent or read-only files, look for RCS or SCCS
104 (! output_elsewhere &&
105 (/* No one can write to it. */
106 (filestat.st_mode & 0222) == 0 ||
107 /* I can't write to it. */
108 ((filestat.st_mode & 0022) == 0 && filestat.st_uid != myuid)))) {
114 filebase = basename(filename);
115 pathlen = filebase - filename;
118 * Put any leading path into `s'.
119 * Leave room in lbuf for the diff command.
122 strncpy(s, filename, pathlen);
124 #define try(f,a1,a2) (Sprintf(s + pathlen, f, a1, a2), stat(s, &cstat) == 0)
125 if ((try("RCS/%s%s", filebase, RCSSUFFIX) ||
126 try("RCS/%s%s", filebase, "") ||
127 try("%s%s", filebase, RCSSUFFIX)) &&
129 * Check that RCS file is not working file.
130 * Some hosts don't report file name length errors.
133 ((filestat.st_dev ^ cstat.st_dev) |
134 (filestat.st_ino ^ cstat.st_ino)))) {
135 Sprintf(buf, output_elsewhere?CHECKOUT:CHECKOUT_LOCKED, filename);
136 Sprintf(lbuf, RCSDIFF, filename);
138 } else if (try("SCCS/%s%s", SCCSPREFIX, filebase) ||
139 try("%s%s", SCCSPREFIX, filebase)) {
140 Sprintf(buf, output_elsewhere?GET:GET_LOCKED, s);
141 Sprintf(lbuf, SCCSDIFF, s, filename);
143 } else if (statfailed)
144 fatal2("can't find %s\n", filename);
146 * else we can't write to it but it's not under a version
147 * control system, so just proceed.
151 if ((filestat.st_mode & 0222) != 0)
152 /* The owner can write to it. */
154 "file %s seems to be locked by somebody else under %s\n",
157 * It might be checked out unlocked. See if
158 * it's safe to check out the default version
163 "Comparing file %s to default %s version...\n",
167 "can't check out file %s: differs from default %s version\n",
171 say3("Checking out file %s from %s...\n", filename, cs);
172 if (system(buf) || stat(filename, &filestat))
173 fatal3("can't check out file %s from %s\n",
177 filemode = filestat.st_mode;
178 if (!S_ISREG(filemode))
179 fatal2("%s is not a normal file--can't patch\n", filename);
180 i_size = filestat.st_size;
182 set_hunkmax(); /* make sure dynamic arrays are allocated */
184 return FALSE; /* force plan b because plan a bombed */
190 * Lint says this may alloc less than i_size,
191 * but that's okay, I think.
193 i_womp = malloc((MEM)(i_size + 2));
195 if (i_womp == Nullch)
197 if ((ifd = open(filename, 0)) < 0)
198 pfatal2("can't open file %s", filename);
200 if (read(ifd, i_womp, (int)i_size) != i_size) {
202 * This probably means i_size > 15 or 16 bits worth. At
203 * this point it doesn't matter if i_womp was undersized.
211 if (i_size && i_womp[i_size - 1] != '\n')
212 i_womp[i_size++] = '\n';
213 i_womp[i_size] = '\0';
216 * Count the lines in the buffer so we know how many pointers we
220 for (s = i_womp; *s; s++) {
225 i_ptr = Null(char**);
227 i_ptr = (char **)malloc((MEM)((iline + 2) * sizeof(char *)));
229 if (i_ptr == Null(char **)) { /* shucks, it was a near thing */
230 free((char *)i_womp);
234 /* Now scan the buffer and build pointer array. */
236 i_ptr[iline] = i_womp;
237 for (s = i_womp; *s; s++) {
239 /* These are NOT null terminated. */
240 i_ptr[++iline] = s + 1;
243 input_lines = iline - 1;
245 /* Now check for revision, if any. */
246 if (revision != Nullch) {
247 if (!rev_in_string(i_womp)) {
251 "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
255 "this file doesn't appear to be the %s version--aborting.\n", revision);
258 "This file doesn't appear to be the %s version--patch anyway? [n] ",
264 say2("Good. This file appears to be the %s version.\n",
268 return TRUE; /* Plan a will work. */
272 * Keep (virtually) nothing in memory.
275 plan_b(char *filename)
280 Reg4 bool found_revision = (revision == Nullch);
282 using_plan_a = FALSE;
283 if ((ifp = fopen(filename, "r")) == Nullfp)
284 pfatal2("can't open file %s", filename);
285 if ((tifd = creat(TMPINNAME, 0666)) < 0)
286 pfatal2("can't open file %s", TMPINNAME);
287 while (fgets(buf, sizeof buf, ifp) != Nullch) {
288 if (revision != Nullch && !found_revision && rev_in_string(buf))
289 found_revision = TRUE;
290 if ((i = strlen(buf)) > maxlen)
291 maxlen = i; /* Find longest line. */
293 if (revision != Nullch) {
294 if (!found_revision) {
298 "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
302 "this file doesn't appear to be the %s version--aborting.\n", revision);
305 "This file doesn't appear to be the %s version--patch anyway? [n] ",
311 say2("Good. This file appears to be the %s version.\n",
314 Fseek(ifp, 0L, 0); /* Rewind file. */
315 lines_per_buf = BUFFERSIZE / maxlen;
317 tibuf[0] = malloc((MEM)(BUFFERSIZE + 1));
318 tibuf[1] = malloc((MEM)(BUFFERSIZE + 1));
319 if (tibuf[1] == Nullch)
320 fatal1("out of memory\n");
322 if (! (i % lines_per_buf)) /* New block. */
323 if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
324 pfatal1("can't write temp file");
325 if (fgets(tibuf[0] + maxlen * (i%lines_per_buf),
326 maxlen + 1, ifp) == Nullch) {
328 if (i % lines_per_buf)
329 if (write(tifd, tibuf[0], BUFFERSIZE) <
331 pfatal1("can't write temp file");
337 if ((tifd = open(TMPINNAME, 0)) < 0) {
338 pfatal2("can't reopen file %s", TMPINNAME);
343 * Fetch a line from the input file, \n terminated, not necessarily \0.
346 ifetch(line,whichbuf)
348 int whichbuf; /* ignored when file in memory */
350 if (line < 1 || line > input_lines)
355 LINENUM offline = line % lines_per_buf;
356 LINENUM baseline = line - offline;
358 if (tiline[0] == baseline)
360 else if (tiline[1] == baseline)
363 tiline[whichbuf] = baseline;
364 #ifndef lint /* complains of long accuracy */
365 Lseek(tifd, (long)baseline / lines_per_buf * BUFFERSIZE, 0);
367 if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
368 pfatal2("error reading tmp file %s", TMPINNAME);
370 return tibuf[whichbuf] + (tireclen * offline);
375 * True if the string argument contains the revision number we want.
378 rev_in_string(char *string)
383 if (revision == Nullch)
385 patlen = strlen(revision);
386 if (strnEQ(string,revision,patlen) &&
387 isspace((unsigned char)string[patlen]))
389 for (s = string; *s; s++) {
390 if (isspace((unsigned char)*s) &&
391 strnEQ(s + 1, revision, patlen) &&
392 isspace((unsigned char)s[patlen + 1] )) {