2 * $OpenBSD: inp.c,v 1.32 2004/08/05 21:47:24 deraadt Exp $
3 * $DragonFly: src/usr.bin/patch/inp.c,v 1.5 2006/04/18 22:11:35 joerg Exp $
7 * patch - a program to apply diffs to original files
9 * Copyright 1986, Larry Wall
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following condition is met:
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this condition and the following disclaimer.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * -C option added in 1998, original code by Marc Espie, based on FreeBSD
32 #include <sys/types.h>
52 /* Input-file-with-indexable-lines abstract type */
54 static off_t i_size
; /* size of the input file */
55 static char *i_womp
; /* plan a buffer for entire file */
56 static char **i_ptr
; /* pointers to lines in i_womp */
57 static char empty_line
[] = { '\0' };
59 static int tifd
= -1; /* plan b virtual string array */
60 static char *tibuf
[2]; /* plan b buffers */
61 static LINENUM tiline
[2] = {-1, -1}; /* 1st line in each buffer */
62 static LINENUM lines_per_buf
; /* how many lines per buffer */
63 static int tireclen
; /* length of records in tmp file */
65 static bool rev_in_string(const char *);
66 static bool reallocate_lines(size_t *);
68 /* returns false if insufficient memory */
69 static bool plan_a(const char *);
71 static void plan_b(const char *);
73 /* New patch--prepare to edit another file. */
83 munmap(i_womp
, i_size
);
87 using_plan_a
= true; /* maybe the next one is smaller */
92 tibuf
[0] = tibuf
[1] = NULL
;
93 tiline
[0] = tiline
[1] = -1;
98 /* Constuct the line index, somehow or other. */
101 scan_input(const char *filename
)
103 if (!plan_a(filename
))
106 say("Patching file %s using Plan %s...\n", filename
,
107 (using_plan_a
? "A" : "B"));
112 reallocate_lines(size_t *lines_allocated
)
117 new_size
= *lines_allocated
* 3 / 2;
118 p
= realloc(i_ptr
, (new_size
+ 2) * sizeof(char *));
119 if (p
== NULL
) { /* shucks, it was a near thing */
120 munmap(i_womp
, i_size
);
124 *lines_allocated
= 0;
127 *lines_allocated
= new_size
;
132 /* Try keeping everything in memory. */
135 plan_a(const char *filename
)
138 char *p
, *s
, lbuf
[MAXLINELEN
];
140 struct stat filestat
;
143 size_t lines_allocated
;
150 if (filename
== NULL
|| *filename
== '\0')
153 statfailed
= stat(filename
, &filestat
);
154 if (statfailed
&& ok_to_create_file
) {
156 say("(Creating file %s...)\n", filename
);
159 * in check_patch case, we still display `Creating file' even
160 * though we're not. The rule is that -C should be as similar
161 * to normal patch behavior as possible
165 makedirs(filename
, true);
166 close(creat(filename
, 0666));
167 statfailed
= stat(filename
, &filestat
);
169 if (statfailed
&& check_only
)
170 fatal("%s not found, -C mode, can't probe further\n", filename
);
171 /* For nonexistent or read-only files, look for RCS or SCCS versions. */
173 /* No one can write to it. */
174 (filestat
.st_mode
& 0222) == 0 ||
175 /* I can't write to it. */
176 ((filestat
.st_mode
& 0022) == 0 && filestat
.st_uid
!= getuid())) {
177 const char *cs
= NULL
, *filebase
, *filedir
;
179 char *tmp_filename1
, *tmp_filename2
;
181 tmp_filename1
= strdup(filename
);
182 tmp_filename2
= strdup(filename
);
183 if (tmp_filename1
== NULL
|| tmp_filename2
== NULL
)
184 fatal("strdupping filename");
185 filebase
= basename(tmp_filename1
);
186 filedir
= dirname(tmp_filename2
);
188 /* Leave room in lbuf for the diff command. */
191 #define try(f, a1, a2, a3) \
192 (snprintf(s, sizeof lbuf - 20, f, a1, a2, a3), stat(s, &cstat) == 0)
194 if (try("%s/RCS/%s%s", filedir
, filebase
, RCSSUFFIX
) ||
195 try("%s/RCS/%s%s", filedir
, filebase
, "") ||
196 try("%s/%s%s", filedir
, filebase
, RCSSUFFIX
)) {
197 snprintf(buf
, buf_len
, CHECKOUT
, filename
);
198 snprintf(lbuf
, sizeof lbuf
, RCSDIFF
, filename
);
200 } else if (try("%s/SCCS/%s%s", filedir
, SCCSPREFIX
, filebase
) ||
201 try("%s/%s%s", filedir
, SCCSPREFIX
, filebase
)) {
202 snprintf(buf
, buf_len
, GET
, s
);
203 snprintf(lbuf
, sizeof lbuf
, SCCSDIFF
, s
, filename
);
205 } else if (statfailed
)
206 fatal("can't find %s\n", filename
);
212 * else we can't write to it but it's not under a version
213 * control system, so just proceed.
217 if ((filestat
.st_mode
& 0222) != 0)
218 /* The owner can write to it. */
219 fatal("file %s seems to be locked "
220 "by somebody else under %s\n",
223 * It might be checked out unlocked. See if
224 * it's safe to check out the default version
228 say("Comparing file %s to default "
232 fatal("can't check out file %s: "
233 "differs from default %s version\n",
237 say("Checking out file %s from %s...\n",
239 if (system(buf
) || stat(filename
, &filestat
))
240 fatal("can't check out file %s from %s\n",
244 filemode
= filestat
.st_mode
;
245 if (!S_ISREG(filemode
))
246 fatal("%s is not a normal file--can't patch\n", filename
);
247 i_size
= filestat
.st_size
;
249 set_hunkmax(); /* make sure dynamic arrays are allocated */
251 return false; /* force plan b because plan a bombed */
253 if (i_size
> SIZE_MAX
) {
254 say("block too large to mmap\n");
257 if ((ifd
= open(filename
, O_RDONLY
)) < 0)
258 pfatal("can't open file %s", filename
);
260 i_womp
= mmap(NULL
, i_size
, PROT_READ
, MAP_PRIVATE
, ifd
, 0);
261 if (i_womp
== MAP_FAILED
) {
262 perror("mmap failed");
270 madvise(i_womp
, i_size
, MADV_SEQUENTIAL
);
272 /* estimate the number of lines */
273 lines_allocated
= i_size
/ 25;
274 if (lines_allocated
< 100)
275 lines_allocated
= 100;
277 if (!reallocate_lines(&lines_allocated
))
280 /* now scan the buffer and build pointer array */
282 i_ptr
[iline
] = i_womp
;
283 /* test for NUL too, to maintain the behavior of the original code */
284 for (s
= i_womp
, i
= 0; i
< i_size
&& *s
!= '\0'; s
++, i
++) {
286 if (iline
== (LINENUM
)lines_allocated
) {
287 if (!reallocate_lines(&lines_allocated
))
290 /* these are NOT NUL terminated */
291 i_ptr
[++iline
] = s
+ 1;
294 /* if the last line contains no EOL, append one */
295 if (i_size
> 0 && i_womp
[i_size
- 1] != '\n') {
296 last_line_missing_eol
= true;
298 sz
= s
- i_ptr
[iline
];
303 munmap(i_womp
, i_size
);
308 memcpy(p
, i_ptr
[iline
], sz
);
311 /* count the extra line and make it point to some valid mem */
312 i_ptr
[++iline
] = empty_line
;
314 last_line_missing_eol
= false;
316 input_lines
= iline
- 1;
318 /* now check for revision, if any */
320 if (revision
!= NULL
) {
321 if (!rev_in_string(i_womp
)) {
324 say("Warning: this file doesn't appear "
325 "to be the %s version--patching anyway.\n",
328 fatal("this file doesn't appear to be the "
329 "%s version--aborting.\n",
332 ask("This file doesn't appear to be the "
333 "%s version--patch anyway? [n] ",
339 say("Good. This file appears to be the %s version.\n",
342 return true; /* plan a will work */
345 /* Keep (virtually) nothing in memory. */
348 plan_b(const char *filename
)
351 int i
= 0, j
, maxlen
= 1;
353 bool found_revision
= (revision
== NULL
);
355 using_plan_a
= false;
356 if ((ifp
= fopen(filename
, "r")) == NULL
)
357 pfatal("can't open file %s", filename
);
359 if ((tifd
= open(TMPINNAME
, O_EXCL
| O_CREAT
| O_WRONLY
, 0666)) < 0)
360 pfatal("can't open file %s", TMPINNAME
);
361 while (fgets(buf
, buf_len
, ifp
) != NULL
) {
362 if (revision
!= NULL
&& !found_revision
&& rev_in_string(buf
))
363 found_revision
= true;
364 if ((i
= strlen(buf
)) > maxlen
)
365 maxlen
= i
; /* find longest line */
367 last_line_missing_eol
= i
> 0 && buf
[i
- 1] != '\n';
368 if (last_line_missing_eol
&& maxlen
== i
)
371 if (revision
!= NULL
) {
372 if (!found_revision
) {
375 say("Warning: this file doesn't appear "
376 "to be the %s version--patching anyway.\n",
379 fatal("this file doesn't appear to be the "
380 "%s version--aborting.\n",
383 ask("This file doesn't appear to be the %s "
384 "version--patch anyway? [n] ",
390 say("Good. This file appears to be the %s version.\n",
393 fseek(ifp
, 0L, SEEK_SET
); /* rewind file */
394 lines_per_buf
= BUFFERSIZE
/ maxlen
;
396 tibuf
[0] = malloc(BUFFERSIZE
+ 1);
397 if (tibuf
[0] == NULL
)
398 fatal("out of memory\n");
399 tibuf
[1] = malloc(BUFFERSIZE
+ 1);
400 if (tibuf
[1] == NULL
)
401 fatal("out of memory\n");
403 p
= tibuf
[0] + maxlen
* (i
% lines_per_buf
);
404 if (i
% lines_per_buf
== 0) /* new block */
405 if (write(tifd
, tibuf
[0], BUFFERSIZE
) < BUFFERSIZE
)
406 pfatal("can't write temp file");
407 if (fgets(p
, maxlen
+ 1, ifp
) == NULL
) {
409 if (i
% lines_per_buf
!= 0)
410 if (write(tifd
, tibuf
[0], BUFFERSIZE
) < BUFFERSIZE
)
411 pfatal("can't write temp file");
415 /* These are '\n' terminated strings, so no need to add a NUL */
416 if (j
== 0 || p
[j
- 1] != '\n')
421 if ((tifd
= open(TMPINNAME
, O_RDONLY
)) < 0)
422 pfatal("can't reopen file %s", TMPINNAME
);
426 * Fetch a line from the input file, \n terminated, not necessarily \0.
429 ifetch(LINENUM line
, int whichbuf
)
431 if (line
< 1 || line
> input_lines
) {
432 if (warn_on_invalid_line
) {
433 say("No such line %ld in input file, ignoring\n", line
);
434 warn_on_invalid_line
= false;
441 LINENUM offline
= line
% lines_per_buf
;
442 LINENUM baseline
= line
- offline
;
444 if (tiline
[0] == baseline
)
446 else if (tiline
[1] == baseline
)
449 tiline
[whichbuf
] = baseline
;
451 lseek(tifd
, (off_t
) (baseline
/ lines_per_buf
*
452 BUFFERSIZE
), SEEK_SET
);
454 if (read(tifd
, tibuf
[whichbuf
], BUFFERSIZE
) < 0)
455 pfatal("error reading tmp file %s", TMPINNAME
);
457 return tibuf
[whichbuf
] + (tireclen
* offline
);
462 * True if the string argument contains the revision number we want.
465 rev_in_string(const char *string
)
470 if (revision
== NULL
)
472 patlen
= strlen(revision
);
473 if (strnEQ(string
, revision
, patlen
) && isspace((unsigned char)string
[patlen
]))
475 for (s
= string
; *s
; s
++) {
476 if (isspace((unsigned char)*s
) && strnEQ(s
+ 1, revision
, patlen
) &&
477 isspace((unsigned char)s
[patlen
+ 1])) {