2 * Copyright 1986, Larry Wall
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following condition is met:
6 * 1. Redistributions of source code must retain the above copyright notice,
7 * this condition and the following disclaimer.
9 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
10 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
12 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
13 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
14 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
15 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
16 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
17 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
18 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
21 * patch - a program to apply diffs to original files
23 * -C option added in 1998, original code by Marc Espie, based on FreeBSD
26 * $OpenBSD: util.c,v 1.35 2010/07/24 01:10:12 ray Exp $
27 * $FreeBSD: head/usr.bin/patch/util.c 354328 2019-11-04 03:07:01Z kevans $
47 #include "backupfile.h"
48 #include "pathnames.h"
50 /* Rename a file, copying it if necessary. */
53 move_file(const char *from, const char *to)
63 say("Moving %s to stdout.\n", from);
65 fromfd = open(from, O_RDONLY);
67 pfatal("internal error, can't reopen %s", from);
68 while ((i = read(fromfd, buf, buf_size)) > 0)
69 if (write(STDOUT_FILENO, buf, i) != i)
70 pfatal("write failed");
74 if (backup_file(to) < 0) {
75 say("Can't backup %s, output is in %s: %s\n", to, from,
81 say("Moving %s to %s.\n", from, to);
83 if (rename(from, to) < 0) {
84 if (errno != EXDEV || copy_file(from, to) < 0) {
85 say("Can't create %s, output is in %s: %s\n",
86 to, from, strerror(errno));
93 /* Backup the original file. */
96 backup_file(const char *orig)
99 char bakname[PATH_MAX], *s, *simplename;
103 if (backup_type == none || stat(orig, &filestat) != 0)
104 return 0; /* nothing to do */
106 * If the user used zero prefixes or suffixes, then
107 * he doesn't want backups. Yet we have to remove
108 * orig to break possible hardlinks.
110 if ((origprae && *origprae == 0) || *simple_backup_suffix == 0) {
114 orig_device = filestat.st_dev;
115 orig_inode = filestat.st_ino;
118 if (strlcpy(bakname, origprae, sizeof(bakname)) >= sizeof(bakname) ||
119 strlcat(bakname, orig, sizeof(bakname)) >= sizeof(bakname))
120 fatal("filename %s too long for buffer\n", origprae);
122 if ((s = find_backup_file_name(orig)) == NULL)
123 fatal("out of memory\n");
124 if (strlcpy(bakname, s, sizeof(bakname)) >= sizeof(bakname))
125 fatal("filename %s too long for buffer\n", s);
129 if ((simplename = strrchr(bakname, '/')) != NULL)
130 simplename = simplename + 1;
132 simplename = bakname;
135 * Find a backup name that is not the same file. Change the
136 * first lowercase char into uppercase; if that isn't
137 * sufficient, chop off the first char and try again.
139 while (stat(bakname, &filestat) == 0 &&
140 orig_device == filestat.st_dev && orig_inode == filestat.st_ino) {
141 /* Skip initial non-lowercase chars. */
142 for (s = simplename; *s && !islower((unsigned char)*s); s++)
145 *s = toupper((unsigned char)*s);
147 memmove(simplename, simplename + 1,
148 strlen(simplename + 1) + 1);
152 say("Moving %s to %s.\n", orig, bakname);
154 if (rename(orig, bakname) < 0) {
155 if (errno != EXDEV || copy_file(orig, bakname) < 0)
165 copy_file(const char *from, const char *to)
170 tofd = open(to, O_CREAT|O_TRUNC|O_WRONLY, 0666);
173 fromfd = open(from, O_RDONLY, 0);
175 pfatal("internal error, can't reopen %s", from);
176 while ((i = read(fromfd, buf, buf_size)) > 0)
177 if (write(tofd, buf, i) != i)
178 pfatal("write to %s failed", to);
185 * Allocate a unique area for a string.
188 savestr(const char *s)
199 fatal("out of memory\n");
205 * Allocate a unique area for a string. Call fatal if out of memory.
208 xstrdup(const char *s)
216 fatal("out of memory\n");
221 * Vanilla terminal output (buffered).
224 say(const char *fmt, ...)
229 vfprintf(stdout, fmt, ap);
235 * Terminal output, pun intended.
238 fatal(const char *fmt, ...)
243 fprintf(stderr, "patch: **** ");
244 vfprintf(stderr, fmt, ap);
250 * Say something from patch, something from the system, then silence . . .
253 pfatal(const char *fmt, ...)
258 fprintf(stderr, "patch: **** ");
260 vfprintf(stderr, fmt, ap);
262 fprintf(stderr, ": %s\n", strerror(errnum));
267 * Get a response from the user via /dev/tty
270 ask(const char *fmt, ...)
274 static int ttyfd = -1;
277 vfprintf(stdout, fmt, ap);
281 ttyfd = open(_PATH_TTY, O_RDONLY);
283 if ((nr = read(ttyfd, buf, buf_size)) > 0 &&
287 if (ttyfd < 0 || nr <= 0) {
288 /* no tty or error reading, pretend user entered 'return' */
295 * How to handle certain events when not in a critical region.
298 set_signals(int reset)
300 static sig_t hupval, intval;
303 hupval = signal(SIGHUP, SIG_IGN);
304 if (hupval != SIG_IGN)
306 intval = signal(SIGINT, SIG_IGN);
307 if (intval != SIG_IGN)
310 signal(SIGHUP, hupval);
311 signal(SIGINT, intval);
315 * How to handle certain events when in a critical region.
320 signal(SIGHUP, SIG_IGN);
321 signal(SIGINT, SIG_IGN);
325 * Make sure we'll have the directories to create a file. If `striplast' is
326 * true, ignore the last element of `filename'.
330 makedirs(const char *filename, bool striplast)
334 if ((tmpbuf = strdup(filename)) == NULL)
335 fatal("out of memory\n");
338 char *s = strrchr(tmpbuf, '/');
341 return; /* nothing to be done */
345 if (mkpath(tmpbuf) != 0)
346 pfatal("creation of %s failed", tmpbuf);
351 * Make filenames more reasonable.
354 fetchname(const char *at, bool *exists, int strip_leading)
356 char *fullname, *name, *t;
358 struct stat filestat;
360 if (at == NULL || *at == '\0')
362 while (isspace((unsigned char)*at))
366 say("fetchname %s %d\n", at, strip_leading);
368 /* So files can be created by diffing against /dev/null. */
369 if (strnEQ(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1)) {
373 name = fullname = t = savestr(at);
375 tab = strchr(t, '\t') != NULL;
376 /* Strip off up to `strip_leading' path components and NUL terminate. */
377 for (sleading = strip_leading; *t != '\0' && ((tab && *t != '\t') ||
378 !isspace((unsigned char)*t)); t++) {
379 if (t[0] == '/' && t[1] != '/' && t[1] != '\0')
386 * If no -p option was given (957 is the default value!), we were
387 * given a relative pathname, and the leading directories that we
388 * just stripped off all exist, put them back on.
390 if (strip_leading == 957 && name != fullname && *fullname != '/') {
392 if (stat(fullname, &filestat) == 0 && S_ISDIR(filestat.st_mode)) {
397 name = savestr(name);
400 *exists = stat(name, &filestat) == 0;
407 printf("patch 2.0-12u11\n");
408 my_exit(EXIT_SUCCESS);