2 * $OpenBSD: util.c,v 1.28 2004/08/05 21:47:24 deraadt Exp $
3 * $DragonFly: src/usr.bin/patch/util.c,v 1.2 2004/09/28 19:09:50 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/param.h>
48 #include "backupfile.h"
49 #include "pathnames.h"
52 /* Rename a file, copying it if necessary. */
55 move_file(const char *from, const char *to)
65 say("Moving %s to stdout.\n", from);
67 fromfd = open(from, O_RDONLY);
69 pfatal("internal error, can't reopen %s", from);
70 while ((i = read(fromfd, buf, sizeof buf)) > 0)
71 if (write(STDOUT_FILENO, buf, i) != i)
72 pfatal("write failed");
76 if (backup_file(to) < 0) {
77 say("Can't backup %s, output is in %s: %s\n", to, from,
83 say("Moving %s to %s.\n", from, to);
85 if (rename(from, to) < 0) {
86 if (errno != EXDEV || copy_file(from, to) < 0) {
87 say("Can't create %s, output is in %s: %s\n",
88 to, from, strerror(errno));
95 /* Backup the original file. */
98 backup_file(const char *orig)
100 struct stat filestat;
101 char bakname[MAXPATHLEN], *s, *simplename;
105 if (backup_type == none || stat(orig, &filestat) != 0)
106 return 0; /* nothing to do */
107 orig_device = filestat.st_dev;
108 orig_inode = filestat.st_ino;
111 if (strlcpy(bakname, origprae, sizeof(bakname)) >= sizeof(bakname) ||
112 strlcat(bakname, orig, sizeof(bakname)) >= sizeof(bakname))
113 fatal("filename %s too long for buffer\n", origprae);
115 if ((s = find_backup_file_name(orig)) == NULL)
116 fatal("out of memory\n");
117 if (strlcpy(bakname, s, sizeof(bakname)) >= sizeof(bakname))
118 fatal("filename %s too long for buffer\n", s);
122 if ((simplename = strrchr(bakname, '/')) != NULL)
123 simplename = simplename + 1;
125 simplename = bakname;
128 * Find a backup name that is not the same file. Change the
129 * first lowercase char into uppercase; if that isn't
130 * sufficient, chop off the first char and try again.
132 while (stat(bakname, &filestat) == 0 &&
133 orig_device == filestat.st_dev && orig_inode == filestat.st_ino) {
134 /* Skip initial non-lowercase chars. */
135 for (s = simplename; *s && !islower(*s); s++)
140 memmove(simplename, simplename + 1,
141 strlen(simplename + 1) + 1);
145 say("Moving %s to %s.\n", orig, bakname);
147 if (rename(orig, bakname) < 0) {
148 if (errno != EXDEV || copy_file(orig, bakname) < 0)
158 copy_file(const char *from, const char *to)
163 tofd = open(to, O_CREAT|O_TRUNC|O_WRONLY, 0666);
166 fromfd = open(from, O_RDONLY, 0);
168 pfatal("internal error, can't reopen %s", from);
169 while ((i = read(fromfd, buf, sizeof buf)) > 0)
170 if (write(tofd, buf, i) != i)
171 pfatal("write to %s failed", to);
178 * Allocate a unique area for a string.
181 savestr(const char *s)
192 fatal("out of memory\n");
198 * Vanilla terminal output (buffered).
201 say(const char *fmt, ...)
206 vfprintf(stderr, fmt, ap);
212 * Terminal output, pun intended.
215 fatal(const char *fmt, ...)
220 fprintf(stderr, "patch: **** ");
221 vfprintf(stderr, fmt, ap);
227 * Say something from patch, something from the system, then silence . . .
230 pfatal(const char *fmt, ...)
235 fprintf(stderr, "patch: **** ");
237 vfprintf(stderr, fmt, ap);
239 fprintf(stderr, ": %s\n", strerror(errnum));
244 * Get a response from the user via /dev/tty
247 ask(const char *fmt, ...)
251 static int ttyfd = -1;
254 vfprintf(stdout, fmt, ap);
258 ttyfd = open(_PATH_TTY, O_RDONLY);
260 if ((nr = read(ttyfd, buf, sizeof(buf))) > 0 &&
264 if (ttyfd < 0 || nr <= 0) {
265 /* no tty or error reading, pretend user entered 'return' */
272 * How to handle certain events when not in a critical region.
275 set_signals(int reset)
277 static sig_t hupval, intval;
280 hupval = signal(SIGHUP, SIG_IGN);
281 if (hupval != SIG_IGN)
283 intval = signal(SIGINT, SIG_IGN);
284 if (intval != SIG_IGN)
287 signal(SIGHUP, hupval);
288 signal(SIGINT, intval);
292 * How to handle certain events when in a critical region.
297 signal(SIGHUP, SIG_IGN);
298 signal(SIGINT, SIG_IGN);
302 * Make sure we'll have the directories to create a file. If `striplast' is
303 * true, ignore the last element of `filename'.
307 makedirs(const char *filename, bool striplast)
311 if ((tmpbuf = strdup(filename)) == NULL)
312 fatal("out of memory\n");
315 char *s = strrchr(tmpbuf, '/');
317 return; /* nothing to be done */
320 if (snprintf(buf, sizeof(buf), "%s -p %s", _PATH_MKDIR, tmpbuf)
322 fatal("buffer too small to hold %.20s...\n", tmpbuf);
325 pfatal("%.40s failed", buf);
329 * Make filenames more reasonable.
332 fetchname(const char *at, bool *exists, int strip_leading)
334 char *fullname, *name, *t;
336 struct stat filestat;
338 if (at == NULL || *at == '\0')
344 say("fetchname %s %d\n", at, strip_leading);
346 /* So files can be created by diffing against /dev/null. */
347 if (strnEQ(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
349 name = fullname = t = savestr(at);
351 /* Strip off up to `strip_leading' path components and NUL terminate. */
352 for (sleading = strip_leading; *t != '\0' && !isspace(*t); t++) {
353 if (t[0] == '/' && t[1] != '/' && t[1] != '\0')
360 * If no -p option was given (957 is the default value!), we were
361 * given a relative pathname, and the leading directories that we
362 * just stripped off all exist, put them back on.
364 if (strip_leading == 957 && name != fullname && *fullname != '/') {
366 if (stat(fullname, &filestat) == 0 && S_ISDIR(filestat.st_mode)) {
371 name = savestr(name);
374 *exists = stat(name, &filestat) == 0;
379 * Takes the name returned by fetchname and looks in RCS/SCCS directories
380 * for a checked in version.
383 checked_in(char *file)
385 char *filebase, *filedir, tmpbuf[MAXPATHLEN];
386 struct stat filestat;
388 filebase = basename(file);
389 filedir = dirname(file);
391 #define try(f, a1, a2, a3) \
392 (snprintf(tmpbuf, sizeof tmpbuf, f, a1, a2, a3), stat(tmpbuf, &filestat) == 0)
394 if (try("%s/RCS/%s%s", filedir, filebase, RCSSUFFIX) ||
395 try("%s/RCS/%s%s", filedir, filebase, "") ||
396 try("%s/%s%s", filedir, filebase, RCSSUFFIX) ||
397 try("%s/SCCS/%s%s", filedir, SCCSPREFIX, filebase) ||
398 try("%s/%s%s", filedir, SCCSPREFIX, filebase))
407 fprintf(stderr, "Patch version 2.0-12u8-OpenBSD\n");
408 my_exit(EXIT_SUCCESS);