]>
git.cameronkatri.com Git - apple_cmds.git/blob - patch_cmds/patch/inp.c
1 /* $OpenBSD: inp.c,v 1.35 2009/10/27 23:59:41 deraadt Exp $ */
4 * patch - a program to apply diffs to original files
6 * Copyright 1986, Larry Wall
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following condition is met:
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this condition and the following disclaimer.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
17 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * -C option added in 1998, original code by Marc Espie, based on FreeBSD
29 #include <sys/types.h>
49 /* Input-file-with-indexable-lines abstract type */
51 static off_t i_size
; /* size of the input file */
52 static char *i_womp
; /* plan a buffer for entire file */
53 static char **i_ptr
; /* pointers to lines in i_womp */
55 static int tifd
= -1; /* plan b virtual string array */
56 static char *tibuf
[2]; /* plan b buffers */
57 static LINENUM tiline
[2] = {-1, -1}; /* 1st line in each buffer */
58 static LINENUM lines_per_buf
; /* how many lines per buffer */
59 static int tireclen
; /* length of records in tmp file */
61 static bool rev_in_string(const char *);
62 static bool reallocate_lines(size_t *);
64 /* returns false if insufficient memory */
65 static bool plan_a(const char *);
67 static void plan_b(const char *);
69 /* New patch--prepare to edit another file. */
79 munmap(i_womp
, i_size
);
83 using_plan_a
= true; /* maybe the next one is smaller */
88 tibuf
[0] = tibuf
[1] = NULL
;
89 tiline
[0] = tiline
[1] = -1;
94 /* Construct the line index, somehow or other. */
97 scan_input(const char *filename
)
99 if (!plan_a(filename
))
102 say("Patching file %s using Plan %s...\n", filename
,
103 (using_plan_a
? "A" : "B"));
105 say("patching file %s\n", filename
);
110 reallocate_lines(size_t *lines_allocated
)
115 new_size
= *lines_allocated
* 3 / 2;
116 p
= realloc(i_ptr
, (new_size
+ 2) * sizeof(char *));
117 if (p
== NULL
) { /* shucks, it was a near thing */
118 munmap(i_womp
, i_size
);
122 *lines_allocated
= 0;
125 *lines_allocated
= new_size
;
130 /* Try keeping everything in memory. */
133 plan_a(const char *filename
)
136 char *p
, *s
, lbuf
[MAXLINELEN
];
137 struct stat filestat
;
140 size_t iline
, lines_allocated
;
147 if (filename
== NULL
|| *filename
== '\0')
150 statfailed
= stat(filename
, &filestat
);
151 if (statfailed
&& ok_to_create_file
) {
153 say("(Creating file %s...)\n", filename
);
156 * in check_patch case, we still display `Creating file' even
157 * though we're not. The rule is that -C should be as similar
158 * to normal patch behavior as possible
162 makedirs(filename
, true);
163 close(creat(filename
, 0666));
164 statfailed
= stat(filename
, &filestat
);
166 if (statfailed
&& check_only
)
167 fatal("%s not found, -C mode, can't probe further\n", filename
);
168 /* For nonexistent or read-only files, look for RCS or SCCS versions. */
170 /* No one can write to it. */
171 (filestat
.st_mode
& 0222) == 0 ||
172 /* I can't write to it. */
173 ((filestat
.st_mode
& 0022) == 0 && filestat
.st_uid
!= getuid())) {
174 char *cs
= NULL
, *filebase
, *filedir
;
177 filebase
= basename((char *)filename
);
178 filedir
= dirname((char *)filename
);
180 /* Leave room in lbuf for the diff command. */
183 #define try(f, a1, a2, a3) \
184 (snprintf(s, sizeof lbuf - 20, f, a1, a2, a3), stat(s, &cstat) == 0)
186 if (try("%s/RCS/%s%s", filedir
, filebase
, RCSSUFFIX
) ||
187 try("%s/RCS/%s%s", filedir
, filebase
, "") ||
188 try("%s/%s%s", filedir
, filebase
, RCSSUFFIX
)) {
189 snprintf(buf
, sizeof buf
, CHECKOUT
, filename
);
190 snprintf(lbuf
, sizeof lbuf
, RCSDIFF
, filename
);
192 } else if (try("%s/SCCS/%s%s", filedir
, SCCSPREFIX
, filebase
) ||
193 try("%s/%s%s", filedir
, SCCSPREFIX
, filebase
)) {
194 snprintf(buf
, sizeof buf
, GET
, s
);
195 snprintf(lbuf
, sizeof lbuf
, SCCSDIFF
, s
, filename
);
197 } else if (statfailed
)
198 fatal("can't find %s\n", filename
);
200 * else we can't write to it but it's not under a version
201 * control system, so just proceed.
205 if ((filestat
.st_mode
& 0222) != 0)
206 /* The owner can write to it. */
207 fatal("file %s seems to be locked "
208 "by somebody else under %s\n",
211 * It might be checked out unlocked. See if
212 * it's safe to check out the default version
216 say("Comparing file %s to default "
220 fatal("can't check out file %s: "
221 "differs from default %s version\n",
225 say("Checking out file %s from %s...\n",
227 if (system(buf
) || stat(filename
, &filestat
))
228 fatal("can't check out file %s from %s\n",
232 filemode
= filestat
.st_mode
;
233 if (!S_ISREG(filemode
))
234 fatal("%s is not a normal file--can't patch\n", filename
);
235 i_size
= filestat
.st_size
;
237 set_hunkmax(); /* make sure dynamic arrays are allocated */
239 return false; /* force plan b because plan a bombed */
241 if (i_size
> SIZE_MAX
) {
242 say("block too large to mmap\n");
245 if ((ifd
= open(filename
, O_RDONLY
)) < 0)
246 pfatal("can't open file %s", filename
);
248 i_womp
= mmap(NULL
, i_size
== 0 ? 64 : i_size
, PROT_READ
, MAP_PRIVATE
, ifd
, 0);
249 if (i_womp
== MAP_FAILED
) {
250 perror("mmap failed");
258 madvise(i_womp
, i_size
, MADV_SEQUENTIAL
);
260 /* estimate the number of lines */
261 lines_allocated
= i_size
/ 25;
262 if (lines_allocated
< 100)
263 lines_allocated
= 100;
265 if (!reallocate_lines(&lines_allocated
))
268 /* now scan the buffer and build pointer array */
270 i_ptr
[iline
] = i_womp
;
271 /* test for NUL too, to maintain the behavior of the original code */
272 for (s
= i_womp
, i
= 0; i
< i_size
&& *s
!= '\0'; s
++, i
++) {
274 if (iline
== lines_allocated
) {
275 if (!reallocate_lines(&lines_allocated
))
278 /* these are NOT NUL terminated */
279 i_ptr
[++iline
] = s
+ 1;
282 /* if the last line contains no EOL, append one */
283 if (i_size
> 0 && i_womp
[i_size
- 1] != '\n') {
284 last_line_missing_eol
= true;
286 sz
= s
- i_ptr
[iline
];
291 munmap(i_womp
, i_size
);
296 memcpy(p
, i_ptr
[iline
], sz
);
299 /* count the extra line and make it point to some valid mem */
302 last_line_missing_eol
= false;
304 input_lines
= iline
- 1;
306 /* now check for revision, if any */
308 if (revision
!= NULL
) {
309 if (!rev_in_string(i_womp
)) {
312 say("Warning: this file doesn't appear "
313 "to be the %s version--patching anyway.\n",
316 fatal("this file doesn't appear to be the "
317 "%s version--aborting.\n",
320 ask("This file doesn't appear to be the "
321 "%s version--patch anyway? [n] ",
327 say("Good. This file appears to be the %s version.\n",
330 return true; /* plan a will work */
333 /* Keep (virtually) nothing in memory. */
336 plan_b(const char *filename
)
339 size_t i
= 0, j
, maxlen
= 1;
341 bool found_revision
= (revision
== NULL
);
343 using_plan_a
= false;
344 if ((ifp
= fopen(filename
, "r")) == NULL
)
345 pfatal("can't open file %s", filename
);
346 (void) unlink(TMPINNAME
);
347 if ((tifd
= open(TMPINNAME
, O_EXCL
| O_CREAT
| O_WRONLY
, 0666)) < 0)
348 pfatal("can't open file %s", TMPINNAME
);
349 while (fgets(buf
, sizeof buf
, ifp
) != NULL
) {
350 if (revision
!= NULL
&& !found_revision
&& rev_in_string(buf
))
351 found_revision
= true;
352 if ((i
= strlen(buf
)) > maxlen
)
353 maxlen
= i
; /* find longest line */
355 last_line_missing_eol
= i
> 0 && buf
[i
- 1] != '\n';
356 if (last_line_missing_eol
&& maxlen
== i
)
359 if (revision
!= NULL
) {
360 if (!found_revision
) {
363 say("Warning: this file doesn't appear "
364 "to be the %s version--patching anyway.\n",
367 fatal("this file doesn't appear to be the "
368 "%s version--aborting.\n",
371 ask("This file doesn't appear to be the %s "
372 "version--patch anyway? [n] ",
378 say("Good. This file appears to be the %s version.\n",
381 fseek(ifp
, 0L, SEEK_SET
); /* rewind file */
382 lines_per_buf
= BUFFERSIZE
/ maxlen
;
384 tibuf
[0] = malloc(BUFFERSIZE
+ 1);
385 if (tibuf
[0] == NULL
)
386 fatal("out of memory\n");
387 tibuf
[1] = malloc(BUFFERSIZE
+ 1);
388 if (tibuf
[1] == NULL
)
389 fatal("out of memory\n");
391 p
= tibuf
[0] + maxlen
* (i
% lines_per_buf
);
392 if (i
% lines_per_buf
== 0) /* new block */
393 if (write(tifd
, tibuf
[0], BUFFERSIZE
) < BUFFERSIZE
)
394 pfatal("can't write temp file");
395 if (fgets(p
, maxlen
+ 1, ifp
) == NULL
) {
397 if (i
% lines_per_buf
!= 0)
398 if (write(tifd
, tibuf
[0], BUFFERSIZE
) < BUFFERSIZE
)
399 pfatal("can't write temp file");
403 /* These are '\n' terminated strings, so no need to add a NUL */
404 if (j
== 0 || p
[j
- 1] != '\n')
409 if ((tifd
= open(TMPINNAME
, O_RDONLY
)) < 0)
410 pfatal("can't reopen file %s", TMPINNAME
);
414 * Fetch a line from the input file, \n terminated, not necessarily \0.
417 ifetch(LINENUM line
, int whichbuf
)
419 if (line
< 1 || line
> input_lines
) {
420 if (warn_on_invalid_line
) {
421 say("No such line %ld in input file, ignoring\n", line
);
422 warn_on_invalid_line
= false;
429 LINENUM offline
= line
% lines_per_buf
;
430 LINENUM baseline
= line
- offline
;
432 if (tiline
[0] == baseline
)
434 else if (tiline
[1] == baseline
)
437 tiline
[whichbuf
] = baseline
;
439 if (lseek(tifd
, (off_t
) (baseline
/ lines_per_buf
*
440 BUFFERSIZE
), SEEK_SET
) < 0)
441 pfatal("cannot seek in the temporary input file");
443 if (read(tifd
, tibuf
[whichbuf
], BUFFERSIZE
) < 0)
444 pfatal("error reading tmp file %s", TMPINNAME
);
446 return tibuf
[whichbuf
] + (tireclen
* offline
);
451 * True if the string argument contains the revision number we want.
454 rev_in_string(const char *string
)
459 if (revision
== NULL
)
461 patlen
= strlen(revision
);
462 if (strnEQ(string
, revision
, patlen
) && isspace(string
[patlen
]))
464 for (s
= string
; *s
; s
++) {
465 if (isspace(*s
) && strnEQ(s
+ 1, revision
, patlen
) &&
466 isspace(s
[patlen
+ 1])) {