1 /*
2  * $OpenBSD: inp.c,v 1.34 2006/03/11 19:41:30 otto Exp $
3  * $DragonFly: src/usr.bin/patch/inp.c,v 1.6 2007/09/29 23:11:10 swildner Exp $
4  * $NetBSD: inp.c,v 1.30 2023/06/16 23:36:26 wiz Exp $
5  */
6 
7 /*
8  * patch - a program to apply diffs to original files
9  *
10  * Copyright 1986, Larry Wall
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following condition is met:
14  * 1. Redistributions of source code must retain the above copyright notice,
15  * this condition and the following disclaimer.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * -C option added in 1998, original code by Marc Espie, based on FreeBSD
30  * behaviour
31  */
32 
33 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: inp.c,v 1.30 2023/06/16 23:36:26 wiz Exp $");
35 
36 #include <sys/types.h>
37 #include <sys/file.h>
38 #include <sys/stat.h>
39 #include <sys/mman.h>
40 #include <sys/wait.h>
41 
42 #include <ctype.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <libgen.h>
46 #include <limits.h>
47 #include <stddef.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 
53 #include "common.h"
54 #include "util.h"
55 #include "pch.h"
56 #include "inp.h"
57 
58 
59 /* Input-file-with-indexable-lines abstract type */
60 
61 static off_t        i_size;             /* size of the input file */
62 static char         *i_womp;  /* plan a buffer for entire file */
63 static char         **i_ptr;  /* pointers to lines in i_womp */
64 static char         empty_line[] = { '\0' };
65 
66 static int          tifd = -1;          /* plan b virtual string array */
67 static char         *tibuf[2];          /* plan b buffers */
68 static LINENUM      tiline[2] = {-1, -1};         /* 1st line in each buffer */
69 static LINENUM      lines_per_buf;      /* how many lines per buffer */
70 static int          tireclen; /* length of records in tmp file */
71 
72 static bool         rev_in_string(const char *);
73 static bool         reallocate_lines(size_t *);
74 
75 /* returns false if insufficient memory */
76 static bool         plan_a(const char *);
77 
78 static void         plan_b(const char *);
79 
80 /* New patch--prepare to edit another file. */
81 
82 void
re_input(void)83 re_input(void)
84 {
85           if (using_plan_a) {
86                     i_size = 0;
87                     free(i_ptr);
88                     i_ptr = NULL;
89                     if (i_womp != NULL) {
90                               munmap(i_womp, i_size);
91                               i_womp = NULL;
92                     }
93           } else {
94                     using_plan_a = true;          /* maybe the next one is smaller */
95                     close(tifd);
96                     tifd = -1;
97                     free(tibuf[0]);
98                     free(tibuf[1]);
99                     tibuf[0] = tibuf[1] = NULL;
100                     tiline[0] = tiline[1] = -1;
101                     tireclen = 0;
102           }
103 }
104 
105 /* Construct the line index, somehow or other. */
106 
107 void
scan_input(const char * filename)108 scan_input(const char *filename)
109 {
110           if (!plan_a(filename))
111                     plan_b(filename);
112           if (verbose) {
113                     say("Patching file %s using Plan %s...\n", filename,
114                         (using_plan_a ? "A" : "B"));
115           }
116 }
117 
118 static bool
reallocate_lines(size_t * lines_allocated)119 reallocate_lines(size_t *lines_allocated)
120 {
121           char      **p;
122           size_t    new_size;
123 
124           new_size = *lines_allocated * 3 / 2;
125           p = pch_realloc(i_ptr, new_size + 2,  sizeof(char *));
126           if (p == NULL) {    /* shucks, it was a near thing */
127                     munmap(i_womp, i_size);
128                     i_womp = NULL;
129                     free(i_ptr);
130                     i_ptr = NULL;
131                     *lines_allocated = 0;
132                     return false;
133           }
134           *lines_allocated = new_size;
135           i_ptr = p;
136           return true;
137 }
138 
139 /* Try keeping everything in memory. */
140 
141 static bool
plan_a(const char * filename)142 plan_a(const char *filename)
143 {
144           int                 ifd, statfailed, devnull, pstat;
145           char                *p, *s, *lbuf;
146           struct stat         filestat;
147           off_t               i;
148           ptrdiff_t sz;
149           size_t              iline, lines_allocated, lbufsz;
150           pid_t               pid;
151           char                *argp[4] = {NULL};
152 
153 #ifdef DEBUGGING
154           if (debug & 8)
155                     return false;
156 #endif
157 
158           if (filename == NULL || *filename == '\0')
159                     return false;
160 
161           statfailed = stat(filename, &filestat);
162           if (statfailed && ok_to_create_file) {
163                     if (verbose)
164                               say("(Creating file %s...)\n", filename);
165 
166                     /*
167                      * in check_patch case, we still display `Creating file' even
168                      * though we're not. The rule is that -C should be as similar
169                      * to normal patch behavior as possible
170                      */
171                     if (check_only)
172                               return true;
173                     makedirs(filename, true);
174                     close(creat(filename, 0666));
175                     statfailed = stat(filename, &filestat);
176           }
177           if (statfailed && check_only)
178                     fatal("%s not found, -C mode, can't probe further\n", filename);
179           /* For nonexistent or read-only files, look for RCS versions.  */
180           if (statfailed ||
181               /* No one can write to it.  */
182               (filestat.st_mode & 0222) == 0 ||
183               /* I can't write to it.  */
184               ((filestat.st_mode & 0022) == 0 && filestat.st_uid != getuid())) {
185                     char      *filebase, *filedir;
186                     struct stat         cstat;
187                     char *tmp_filename1, *tmp_filename2;
188 
189                     tmp_filename1 = strdup(filename);
190                     tmp_filename2 = strdup(filename);
191                     if (tmp_filename1 == NULL || tmp_filename2 == NULL)
192                               fatal("strdupping filename");
193 
194                     filebase = basename(tmp_filename1);
195                     filedir = dirname(tmp_filename2);
196 
197                     lbufsz = INITLINELEN;
198                     if ((lbuf = malloc(bufsz)) == NULL)
199                               pfatal("allocating line buffer");
200                     lbuf[0] = '\0';
201 
202 #define try(f, a1, a2, a3) \
203           (snprintf(lbuf, lbufsz, f, a1, a2, a3), stat(lbuf, &cstat) == 0)
204 
205                     /*
206                      * else we can't write to it but it's not under a version
207                      * control system, so just proceed.
208                      */
209                     if (try("%s/RCS/%s%s", filedir, filebase, RCSSUFFIX) ||
210                         try("%s/RCS/%s%s", filedir, filebase, "") ||
211                         try("%s/%s%s", filedir, filebase, RCSSUFFIX)) {
212                               if (!statfailed) {
213                                         if ((filestat.st_mode & 0222) != 0)
214                                                   /* The owner can write to it.  */
215                                                   fatal("file %s seems to be locked "
216                                                       "by somebody else under RCS\n",
217                                                       filename);
218                                         /*
219                                          * It might be checked out unlocked.  See if
220                                          * it's safe to check out the default version
221                                          * locked.
222                                          */
223                                         if (verbose)
224                                                   say("Comparing file %s to default "
225                                                       "RCS version...\n", filename);
226 
227                                         switch (pid = fork()) {
228                                         case -1:
229                                                   fatal("can't fork: %s\n",
230                                                       strerror(errno));
231                                         case 0:
232                                                   devnull = open("/dev/null", O_RDONLY);
233                                                   if (devnull == -1) {
234                                                             fatal("can't open /dev/null: %s",
235                                                                 strerror(errno));
236                                                   }
237                                                   (void)dup2(devnull, STDOUT_FILENO);
238                                                   argp[0] = __UNCONST(RCSDIFF);
239                                                   argp[1] = __UNCONST(filename);
240                                                   execv(RCSDIFF, argp);
241                                                   exit(127);
242                                         }
243                                         pid = waitpid(pid, &pstat, 0);
244                                         if (pid == -1 || WEXITSTATUS(pstat) != 0) {
245                                                   fatal("can't check out file %s: "
246                                                       "differs from default RCS version\n",
247                                                       filename);
248                                         }
249                               }
250 
251                               if (verbose)
252                                         say("Checking out file %s from RCS...\n",
253                                             filename);
254 
255                               switch (pid = fork()) {
256                               case -1:
257                                         fatal("can't fork: %s\n", strerror(errno));
258                               case 0:
259                                         argp[0] = __UNCONST(CHECKOUT);
260                                         argp[1] = __UNCONST("-l");
261                                         argp[2] = __UNCONST(filename);
262                                         execv(CHECKOUT, argp);
263                                         exit(127);
264                               }
265                               pid = waitpid(pid, &pstat, 0);
266                               if (pid == -1 || WEXITSTATUS(pstat) != 0 ||
267                                   stat(filename, &filestat)) {
268                                         fatal("can't check out file %s from RCS\n",
269                                             filename);
270                               }
271                     } else if (statfailed) {
272                               fatal("can't find %s\n", filename);
273                     }
274                     free(lbuf);
275                     free(tmp_filename1);
276                     free(tmp_filename2);
277           }
278 
279           filemode = filestat.st_mode;
280           if (!S_ISREG(filemode))
281                     fatal("%s is not a normal file--can't patch\n", filename);
282           i_size = filestat.st_size;
283           if (out_of_mem) {
284                     set_hunkmax();      /* make sure dynamic arrays are allocated */
285                     out_of_mem = false;
286                     return false;       /* force plan b because plan a bombed */
287           }
288           if ((uintmax_t)i_size > (uintmax_t)SIZE_MAX) {
289                     say("block too large to mmap\n");
290                     return false;
291           }
292           if ((ifd = open(filename, O_RDONLY)) < 0)
293                     pfatal("can't open file %s", filename);
294 
295           if (i_size) {
296                     i_womp = mmap(NULL, i_size, PROT_READ, MAP_PRIVATE, ifd, 0);
297                     if (i_womp == MAP_FAILED) {
298                               perror("mmap failed");
299                               i_womp = NULL;
300                               close(ifd);
301                               return false;
302                     }
303           } else {
304                     i_womp = NULL;
305           }
306 
307           close(ifd);
308           if (i_size)
309                     madvise(i_womp, i_size, MADV_SEQUENTIAL);
310 
311           /* estimate the number of lines */
312           lines_allocated = i_size / 25;
313           if (lines_allocated < 100)
314                     lines_allocated = 100;
315 
316           if (!reallocate_lines(&lines_allocated))
317                     return false;
318 
319           /* now scan the buffer and build pointer array */
320           iline = 1;
321           i_ptr[iline] = i_womp;
322           /* test for NUL too, to maintain the behavior of the original code */
323           for (s = i_womp, i = 0; i < i_size && *s != '\0'; s++, i++) {
324                     if (*s == '\n') {
325                               if (iline == lines_allocated) {
326                                         if (!reallocate_lines(&lines_allocated))
327                                                   return false;
328                               }
329                               /* these are NOT NUL terminated */
330                               i_ptr[++iline] = s + 1;
331                     }
332           }
333           /* if the last line contains no EOL, append one */
334           if (i_size > 0 && i_womp[i_size - 1] != '\n') {
335                     last_line_missing_eol = true;
336                     /* fix last line */
337                     sz = s - i_ptr[iline];
338                     p = malloc(sz + 1);
339                     if (p == NULL) {
340                               free(i_ptr);
341                               i_ptr = NULL;
342                               munmap(i_womp, i_size);
343                               i_womp = NULL;
344                               return false;
345                     }
346 
347                     memcpy(p, i_ptr[iline], sz);
348                     p[sz] = '\n';
349                     i_ptr[iline] = p;
350                     /* count the extra line and make it point to some valid mem */
351                     i_ptr[++iline] = empty_line;
352           } else
353                     last_line_missing_eol = false;
354 
355           input_lines = iline - 1;
356 
357           /* now check for revision, if any */
358 
359           if (revision != NULL) {
360                     if (!rev_in_string(i_womp)) {
361                               if (force) {
362                                         if (verbose)
363                                                   say("Warning: this file doesn't appear "
364                                                       "to be the %s version--patching anyway.\n",
365                                                       revision);
366                               } else if (batch) {
367                                         fatal("this file doesn't appear to be the "
368                                             "%s version--aborting.\n",
369                                             revision);
370                               } else {
371                                         ask("This file doesn't appear to be the "
372                                             "%s version--patch anyway? [n] ",
373                                             revision);
374                                         if (*buf != 'y')
375                                                   fatal("aborted\n");
376                               }
377                     } else if (verbose)
378                               say("Good.  This file appears to be the %s version.\n",
379                                   revision);
380           }
381           return true;                  /* plan a will work */
382 }
383 
384 /* Keep (virtually) nothing in memory. */
385 
386 static void
plan_b(const char * filename)387 plan_b(const char *filename)
388 {
389           FILE      *ifp;
390           size_t    i = 0, j, maxlen = 1;
391           char      *p;
392           bool      found_revision = (revision == NULL);
393 
394           using_plan_a = false;
395           if ((ifp = fopen(filename, "r")) == NULL)
396                     pfatal("can't open file %s", filename);
397           unlink(TMPINNAME);
398           if ((tifd = open(TMPINNAME, O_EXCL | O_CREAT | O_WRONLY, 0666)) < 0)
399                     pfatal("can't open file %s", TMPINNAME);
400           while (getline(&buf, &bufsz, ifp) != -1) {
401                     if (revision != NULL && !found_revision && rev_in_string(buf))
402                               found_revision = true;
403                     if ((i = strlen(buf)) > maxlen)
404                               maxlen = i;         /* find longest line */
405           }
406           last_line_missing_eol = i > 0 && buf[i - 1] != '\n';
407           if (last_line_missing_eol && maxlen == i)
408                     maxlen++;
409 
410           if (revision != NULL) {
411                     if (!found_revision) {
412                               if (force) {
413                                         if (verbose)
414                                                   say("Warning: this file doesn't appear "
415                                                       "to be the %s version--patching anyway.\n",
416                                                       revision);
417                               } else if (batch) {
418                                         fatal("this file doesn't appear to be the "
419                                             "%s version--aborting.\n",
420                                             revision);
421                               } else {
422                                         ask("This file doesn't appear to be the %s "
423                                             "version--patch anyway? [n] ",
424                                             revision);
425                                         if (*buf != 'y')
426                                                   fatal("aborted\n");
427                               }
428                     } else if (verbose)
429                               say("Good.  This file appears to be the %s version.\n",
430                                   revision);
431           }
432           fseek(ifp, 0L, SEEK_SET);     /* rewind file */
433           lines_per_buf = BUFFERSIZE / maxlen;
434           tireclen = maxlen;
435           tibuf[0] = malloc(BUFFERSIZE + 1);
436           if (tibuf[0] == NULL)
437                     fatal("out of memory\n");
438           tibuf[1] = malloc(BUFFERSIZE + 1);
439           if (tibuf[1] == NULL)
440                     fatal("out of memory\n");
441           for (i = 1;; i++) {
442                     p = tibuf[0] + maxlen * (i % lines_per_buf);
443                     if (i % lines_per_buf == 0)   /* new block */
444                               if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
445                                         pfatal("can't write temp file");
446                     if (fgets(p, maxlen + 1, ifp) == NULL) {
447                               input_lines = i - 1;
448                               if (i % lines_per_buf != 0)
449                                         if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
450                                                   pfatal("can't write temp file");
451                               break;
452                     }
453                     j = strlen(p);
454                     /* These are '\n' terminated strings, so no need to add a NUL */
455                     if (j == 0 || p[j - 1] != '\n')
456                               p[j] = '\n';
457           }
458           fclose(ifp);
459           close(tifd);
460           if ((tifd = open(TMPINNAME, O_RDONLY)) < 0)
461                     pfatal("can't reopen file %s", TMPINNAME);
462 }
463 
464 /*
465  * Fetch a line from the input file, \n terminated, not necessarily \0.
466  */
467 char *
ifetch(LINENUM line,int whichbuf)468 ifetch(LINENUM line, int whichbuf)
469 {
470           if (line < 1 || line > input_lines) {
471                     if (warn_on_invalid_line) {
472                               say("No such line %ld in input file, ignoring\n", line);
473                               warn_on_invalid_line = false;
474                     }
475                     return NULL;
476           }
477           if (using_plan_a)
478                     return i_ptr[line];
479           else {
480                     LINENUM   offline = line % lines_per_buf;
481                     LINENUM   baseline = line - offline;
482 
483                     if (tiline[0] == baseline)
484                               whichbuf = 0;
485                     else if (tiline[1] == baseline)
486                               whichbuf = 1;
487                     else {
488                               tiline[whichbuf] = baseline;
489 
490                               if (lseek(tifd, (off_t) (baseline / lines_per_buf *
491                                   BUFFERSIZE), SEEK_SET) < 0)
492                                         pfatal("cannot seek in the temporary input file");
493 
494                               if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
495                                         pfatal("error reading tmp file %s", TMPINNAME);
496                     }
497                     return tibuf[whichbuf] + (tireclen * offline);
498           }
499 }
500 
501 /*
502  * True if the string argument contains the revision number we want.
503  */
504 static bool
rev_in_string(const char * string)505 rev_in_string(const char *string)
506 {
507           const char          *s;
508           size_t              patlen;
509 
510           if (revision == NULL)
511                     return true;
512           patlen = strlen(revision);
513           if (strnEQ(string, revision, patlen) && isspace((unsigned char)string[patlen]))
514                     return true;
515           for (s = string; *s; s++) {
516                     if (isspace((unsigned char)*s) && strnEQ(s + 1, revision, patlen) &&
517                         isspace((unsigned char)s[patlen + 1])) {
518                               return true;
519                     }
520           }
521           return false;
522 }
523