1 /* inputting files to be patched */
3 /* $Id: inp.c,v 1.18 1997/07/21 17:59:46 eggert Exp $ */
6 Copyright 1986, 1988 Larry Wall
7 Copyright 1991, 1992, 1993, 1997 Free Software Foundation, Inc.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING.
21 If not, write to the Free Software Foundation,
22 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include <backupfile.h>
34 /* Input-file-with-indexable-lines abstract type */
36 static char *i_buffer; /* plan A buffer */
37 static char const **i_ptr; /* pointers to lines in plan A buffer */
39 static size_t tibufsize; /* size of plan b buffers */
40 #ifndef TIBUFSIZE_MINIMUM
41 #define TIBUFSIZE_MINIMUM (8 * 1024) /* minimum value for tibufsize */
43 static int tifd = -1; /* plan b virtual string array */
44 static char *tibuf[2]; /* plan b buffers */
45 static LINENUM tiline[2] = {-1, -1}; /* 1st line in each buffer */
46 static LINENUM lines_per_buf; /* how many lines per buffer */
47 static size_t tireclen; /* length of records in tmp file */
48 static size_t last_line_size; /* size of last input line */
50 static bool plan_a PARAMS ((char const *));/* yield FALSE if memory runs out */
51 static void plan_b PARAMS ((char const *));
52 static void report_revision PARAMS ((int));
53 static void too_many_lines PARAMS ((char const *)) __attribute__((noreturn));
55 /* New patch--prepare to edit another file. */
69 tiline[0] = tiline[1] = -1;
74 /* Construct the line index, somehow or other. */
80 using_plan_a = ! (debug & 16) && plan_a (filename);
89 say ("Patching file `%s' using Plan %s...\n",
90 filename, using_plan_a ? "A" : "B");
93 case DEFAULT_VERBOSITY:
94 say ("patching file `%s'\n", filename);
99 /* Report whether a desired revision was found. */
102 report_revision (found_revision)
107 if (verbosity == VERBOSE)
108 say ("Good. This file appears to be the %s version.\n", revision);
112 if (verbosity != SILENT)
113 say ("Warning: this file doesn't appear to be the %s version -- patching anyway.\n",
118 fatal ("This file doesn't appear to be the %s version -- aborting.",
123 ask ("This file doesn't appear to be the %s version -- patch anyway? [n] ",
132 too_many_lines (filename)
133 char const *filename;
135 fatal ("File `%s' has too many lines.", filename);
140 get_input_file (filename, outname)
141 char const *filename;
144 int elsewhere = strcmp (filename, outname);
150 inerrno = stat (inname, &instat) == 0 ? 0 : errno;
152 /* Perhaps look for RCS or SCCS versions. */
157 && (/* No one can write to it. */
158 (instat.st_mode & (S_IWUSR|S_IWGRP|S_IWOTH)) == 0
159 /* Only the owner (who's not me) can write to it. */
160 || ((instat.st_mode & (S_IWGRP|S_IWOTH)) == 0
161 && instat.st_uid != geteuid ()))))
162 && (invc = !! (cs = (version_controller
163 (filename, elsewhere,
164 inerrno ? (struct stat *) 0 : &instat,
165 &getbuf, &diffbuf))))) {
169 && (instat.st_mode & (S_IWUSR|S_IWGRP|S_IWOTH)) != 0)
170 /* Somebody can write to it. */
171 fatal ("file `%s' seems to be locked by somebody else under %s",
173 /* It might be checked out unlocked. See if it's safe to
174 check out the default version locked. */
175 if (verbosity == VERBOSE)
176 say ("Comparing file `%s' to default %s version...\n",
178 if (systemic (diffbuf) != 0)
180 say ("warning: patching file `%s', which does not match default %s version\n",
186 if (cs && version_get (filename, cs, ! inerrno, elsewhere, getbuf,
193 } else if (inerrno && !pch_says_nonexistent (reverse))
196 pfatal ("can't find file `%s'", filename);
201 instat.st_mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
204 else if (! S_ISREG (instat.st_mode))
205 fatal ("`%s' is not a regular file -- can't patch", filename);
209 /* Try keeping everything in memory. */
213 char const *filename;
215 register char const *s;
216 register char const *lim;
217 register char const **ptr;
218 register char *buffer;
219 register LINENUM iline;
220 size_t size = instat.st_size;
222 /* Fail if the file size doesn't fit in a size_t,
223 or if storage isn't available. */
224 if (! (size == instat.st_size
225 && (buffer = malloc (size ? size : (size_t) 1))))
228 /* Read the input file, but don't bother reading it if it's empty.
229 When creating files, the files do not actually exist. */
232 int ifd = open (filename, O_RDONLY|binary_transput);
233 size_t buffered = 0, n;
235 pfatal ("can't open file `%s'", filename);
237 while (size - buffered != 0)
239 n = read (ifd, buffer + buffered, size - buffered);
242 /* Some non-POSIX hosts exaggerate st_size in text mode;
243 or the file may have shrunk! */
247 if (n == (size_t) -1)
249 /* Perhaps size is too large for this host. */
257 if (close (ifd) != 0)
261 /* Scan the buffer and build array of pointers to lines. */
263 iline = 3; /* 1 unused, 1 for SOF, 1 for EOF if last line is incomplete */
264 for (s = buffer; (s = (char *) memchr (s, '\n', lim - s)); s++)
266 too_many_lines (filename);
267 if (! (iline == (size_t) iline
268 && (size_t) iline * sizeof *ptr / sizeof *ptr == (size_t) iline
269 && (ptr = (char const **) malloc ((size_t) iline * sizeof *ptr))))
275 for (s = buffer; ; s++)
278 if (! (s = (char *) memchr (s, '\n', lim - s)))
281 if (size && lim[-1] != '\n')
283 input_lines = iline - 1;
287 char const *rev = revision;
289 int found_revision = 0;
290 size_t revlen = strlen (rev);
294 char const *limrev = lim - revlen;
296 for (s = buffer; (s = (char *) memchr (s, rev0, limrev - s)); s++)
297 if (memcmp (s, rev, revlen) == 0
298 && (s == buffer || ISSPACE ((unsigned char) s[-1]))
299 && (s + 1 == limrev || ISSPACE ((unsigned char) s[revlen])))
306 report_revision (found_revision);
309 /* Plan A will work. */
315 /* Keep (virtually) nothing in memory. */
319 char const *filename;
324 register size_t maxlen;
325 register int found_revision;
327 register char const *rev;
328 register size_t revlen;
329 register LINENUM line = 1;
331 if (instat.st_size == 0)
332 filename = NULL_DEVICE;
333 if (! (ifp = fopen (filename, binary_transput ? "rb" : "r")))
334 pfatal ("can't open file `%s'", filename);
335 tifd = create_file (TMPINNAME, O_RDWR | O_BINARY, (mode_t) 0);
340 found_revision = !rev;
341 revlen = rev ? strlen (rev) : 0;
343 while ((c = getc (ifp)) != EOF)
350 too_many_lines (filename);
360 found_revision = ISSPACE ((unsigned char) c);
363 else if (i != (size_t) -1)
364 i = rev[i]==c ? i + 1 : (size_t) -1;
366 if (i == (size_t) -1 && ISSPACE ((unsigned char) c))
372 report_revision (found_revision);
373 Fseek (ifp, (off_t) 0, SEEK_SET); /* rewind file */
374 for (tibufsize = TIBUFSIZE_MINIMUM; tibufsize < maxlen; tibufsize <<= 1)
376 lines_per_buf = tibufsize / maxlen;
378 tibuf[0] = xmalloc (2 * tibufsize);
379 tibuf[1] = tibuf[0] + tibufsize;
381 for (line = 1; ; line++)
383 char *p = tibuf[0] + maxlen * (line % lines_per_buf);
385 if (! (line % lines_per_buf)) /* new block */
386 if (write (tifd, tibuf[0], tibufsize) != tibufsize)
388 if ((c = getc (ifp)) == EOF)
396 last_line_size = p - p0;
400 if ((c = getc (ifp)) == EOF)
402 last_line_size = p - p0;
409 if (ferror (ifp) || fclose (ifp) != 0)
412 if (line % lines_per_buf != 0)
413 if (write (tifd, tibuf[0], tibufsize) != tibufsize)
415 input_lines = line - 1;
418 /* Fetch a line from the input file. */
421 ifetch (line, whichbuf, psize)
422 register LINENUM line;
423 int whichbuf; /* ignored when file in memory */
426 register char const *q;
427 register char const *p;
429 if (line < 1 || line > input_lines) {
435 *psize = i_ptr[line + 1] - p;
438 LINENUM offline = line % lines_per_buf;
439 LINENUM baseline = line - offline;
441 if (tiline[0] == baseline)
443 else if (tiline[1] == baseline)
446 tiline[whichbuf] = baseline;
447 if (lseek (tifd, (off_t) (baseline/lines_per_buf * tibufsize),
449 || read (tifd, tibuf[whichbuf], tibufsize) < 0)
452 p = tibuf[whichbuf] + (tireclen*offline);
453 if (line == input_lines)
454 *psize = last_line_size;
456 for (q = p; *q++ != '\n'; )