Annotation of src/usr.bin/patch/util.c, Revision 1.35
1.35 ! ray 1: /* $OpenBSD: util.c,v 1.34 2010/01/08 13:27:59 oga Exp $ */
1.26 otto 2:
3: /*
4: * patch - a program to apply diffs to original files
5: *
6: * Copyright 1986, Larry Wall
7: *
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.
12: *
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
23: * SUCH DAMAGE.
24: *
25: * -C option added in 1998, original code by Marc Espie, based on FreeBSD
26: * behaviour
27: */
1.1 deraadt 28:
1.16 otto 29: #include <sys/param.h>
30: #include <sys/stat.h>
31:
32: #include <ctype.h>
33: #include <errno.h>
34: #include <fcntl.h>
35: #include <libgen.h>
36: #include <paths.h>
1.32 otto 37: #include <signal.h>
1.16 otto 38: #include <stdarg.h>
39: #include <stdlib.h>
1.20 otto 40: #include <stdio.h>
1.16 otto 41: #include <string.h>
42: #include <unistd.h>
43:
1.1 deraadt 44: #include "common.h"
45: #include "util.h"
46: #include "backupfile.h"
1.22 millert 47: #include "pathnames.h"
1.1 deraadt 48:
49: /* Rename a file, copying it if necessary. */
50:
51: int
1.20 otto 52: move_file(const char *from, const char *to)
1.1 deraadt 53: {
1.20 otto 54: int fromfd;
55: ssize_t i;
1.1 deraadt 56:
1.13 deraadt 57: /* to stdout? */
1.1 deraadt 58:
1.13 deraadt 59: if (strEQ(to, "-")) {
1.1 deraadt 60: #ifdef DEBUGGING
1.13 deraadt 61: if (debug & 4)
62: say("Moving %s to stdout.\n", from);
1.1 deraadt 63: #endif
1.13 deraadt 64: fromfd = open(from, O_RDONLY);
65: if (fromfd < 0)
66: pfatal("internal error, can't reopen %s", from);
67: while ((i = read(fromfd, buf, sizeof buf)) > 0)
1.16 otto 68: if (write(STDOUT_FILENO, buf, i) != i)
1.13 deraadt 69: pfatal("write failed");
70: close(fromfd);
71: return 0;
1.1 deraadt 72: }
1.18 millert 73: if (backup_file(to) < 0) {
74: say("Can't backup %s, output is in %s: %s\n", to, from,
75: strerror(errno));
76: return -1;
77: }
78: #ifdef DEBUGGING
79: if (debug & 4)
80: say("Moving %s to %s.\n", from, to);
81: #endif
82: if (rename(from, to) < 0) {
83: if (errno != EXDEV || copy_file(from, to) < 0) {
84: say("Can't create %s, output is in %s: %s\n",
85: to, from, strerror(errno));
86: return -1;
87: }
88: }
89: return 0;
90: }
91:
92: /* Backup the original file. */
93:
94: int
1.20 otto 95: backup_file(const char *orig)
1.18 millert 96: {
1.20 otto 97: struct stat filestat;
98: char bakname[MAXPATHLEN], *s, *simplename;
99: dev_t orig_device;
100: ino_t orig_inode;
1.18 millert 101:
102: if (backup_type == none || stat(orig, &filestat) != 0)
103: return 0; /* nothing to do */
104: orig_device = filestat.st_dev;
105: orig_inode = filestat.st_ino;
106:
1.13 deraadt 107: if (origprae) {
108: if (strlcpy(bakname, origprae, sizeof(bakname)) >= sizeof(bakname) ||
1.18 millert 109: strlcat(bakname, orig, sizeof(bakname)) >= sizeof(bakname))
1.13 deraadt 110: fatal("filename %s too long for buffer\n", origprae);
111: } else {
1.18 millert 112: if ((s = find_backup_file_name(orig)) == NULL)
1.13 deraadt 113: fatal("out of memory\n");
1.18 millert 114: if (strlcpy(bakname, s, sizeof(bakname)) >= sizeof(bakname))
115: fatal("filename %s too long for buffer\n", s);
116: free(s);
1.1 deraadt 117: }
1.13 deraadt 118:
1.18 millert 119: if ((simplename = strrchr(bakname, '/')) != NULL)
120: simplename = simplename + 1;
121: else
122: simplename = bakname;
123:
124: /*
125: * Find a backup name that is not the same file. Change the
126: * first lowercase char into uppercase; if that isn't
127: * sufficient, chop off the first char and try again.
128: */
129: while (stat(bakname, &filestat) == 0 &&
130: orig_device == filestat.st_dev && orig_inode == filestat.st_ino) {
131: /* Skip initial non-lowercase chars. */
132: for (s = simplename; *s && !islower(*s); s++)
133: ;
134: if (*s)
135: *s = toupper(*s);
136: else
137: memmove(simplename, simplename + 1,
138: strlen(simplename + 1) + 1);
1.1 deraadt 139: }
140: #ifdef DEBUGGING
1.13 deraadt 141: if (debug & 4)
1.18 millert 142: say("Moving %s to %s.\n", orig, bakname);
1.1 deraadt 143: #endif
1.18 millert 144: if (rename(orig, bakname) < 0) {
145: if (errno != EXDEV || copy_file(orig, bakname) < 0)
1.13 deraadt 146: return -1;
147: }
148: return 0;
149: }
150:
151: /*
152: * Copy a file.
153: */
1.18 millert 154: int
1.20 otto 155: copy_file(const char *from, const char *to)
1.13 deraadt 156: {
1.20 otto 157: int tofd, fromfd;
158: ssize_t i;
1.12 deraadt 159:
1.18 millert 160: tofd = open(to, O_CREAT|O_TRUNC|O_WRONLY, 0666);
1.13 deraadt 161: if (tofd < 0)
1.18 millert 162: return -1;
163: fromfd = open(from, O_RDONLY, 0);
1.1 deraadt 164: if (fromfd < 0)
1.13 deraadt 165: pfatal("internal error, can't reopen %s", from);
166: while ((i = read(fromfd, buf, sizeof buf)) > 0)
167: if (write(tofd, buf, i) != i)
168: pfatal("write to %s failed", to);
1.12 deraadt 169: close(fromfd);
170: close(tofd);
1.18 millert 171: return 0;
1.1 deraadt 172: }
173:
1.13 deraadt 174: /*
175: * Allocate a unique area for a string.
176: */
1.1 deraadt 177: char *
1.20 otto 178: savestr(const char *s)
1.1 deraadt 179: {
1.20 otto 180: char *rv;
1.1 deraadt 181:
1.13 deraadt 182: if (!s)
183: s = "Oops";
1.20 otto 184: rv = strdup(s);
1.16 otto 185: if (rv == NULL) {
1.13 deraadt 186: if (using_plan_a)
1.25 otto 187: out_of_mem = true;
1.13 deraadt 188: else
189: fatal("out of memory\n");
190: }
191: return rv;
1.1 deraadt 192: }
193:
1.13 deraadt 194: /*
195: * Vanilla terminal output (buffered).
196: */
1.1 deraadt 197: void
1.20 otto 198: say(const char *fmt, ...)
1.1 deraadt 199: {
1.13 deraadt 200: va_list ap;
1.12 deraadt 201:
1.13 deraadt 202: va_start(ap, fmt);
1.35 ! ray 203: vfprintf(stdout, fmt, ap);
1.13 deraadt 204: va_end(ap);
1.35 ! ray 205: fflush(stdout);
1.1 deraadt 206: }
207:
1.13 deraadt 208: /*
209: * Terminal output, pun intended.
210: */
1.12 deraadt 211: void
1.20 otto 212: fatal(const char *fmt, ...)
1.1 deraadt 213: {
1.13 deraadt 214: va_list ap;
1.12 deraadt 215:
1.13 deraadt 216: va_start(ap, fmt);
217: fprintf(stderr, "patch: **** ");
218: vfprintf(stderr, fmt, ap);
219: va_end(ap);
1.19 millert 220: my_exit(2);
1.1 deraadt 221: }
222:
1.13 deraadt 223: /*
224: * Say something from patch, something from the system, then silence . . .
225: */
1.12 deraadt 226: void
1.20 otto 227: pfatal(const char *fmt, ...)
1.1 deraadt 228: {
1.13 deraadt 229: va_list ap;
230: int errnum = errno;
1.1 deraadt 231:
1.13 deraadt 232: fprintf(stderr, "patch: **** ");
233: va_start(ap, fmt);
234: vfprintf(stderr, fmt, ap);
235: va_end(ap);
236: fprintf(stderr, ": %s\n", strerror(errnum));
1.19 millert 237: my_exit(2);
1.1 deraadt 238: }
239:
1.13 deraadt 240: /*
1.23 millert 241: * Get a response from the user via /dev/tty
1.13 deraadt 242: */
1.1 deraadt 243: void
1.20 otto 244: ask(const char *fmt, ...)
1.1 deraadt 245: {
1.13 deraadt 246: va_list ap;
1.23 millert 247: ssize_t nr;
248: static int ttyfd = -1;
1.13 deraadt 249:
250: va_start(ap, fmt);
1.23 millert 251: vfprintf(stdout, fmt, ap);
1.13 deraadt 252: va_end(ap);
1.23 millert 253: fflush(stdout);
254: if (ttyfd < 0)
255: ttyfd = open(_PATH_TTY, O_RDONLY);
256: if (ttyfd >= 0) {
257: if ((nr = read(ttyfd, buf, sizeof(buf))) > 0 &&
258: buf[nr - 1] == '\n')
259: buf[nr - 1] = '\0';
260: }
261: if (ttyfd < 0 || nr <= 0) {
262: /* no tty or error reading, pretend user entered 'return' */
263: putchar('\n');
264: buf[0] = '\0';
1.13 deraadt 265: }
1.1 deraadt 266: }
267:
1.13 deraadt 268: /*
269: * How to handle certain events when not in a critical region.
270: */
1.1 deraadt 271: void
1.13 deraadt 272: set_signals(int reset)
1.1 deraadt 273: {
1.13 deraadt 274: static sig_t hupval, intval;
1.1 deraadt 275:
1.13 deraadt 276: if (!reset) {
277: hupval = signal(SIGHUP, SIG_IGN);
278: if (hupval != SIG_IGN)
279: hupval = (sig_t) my_exit;
280: intval = signal(SIGINT, SIG_IGN);
281: if (intval != SIG_IGN)
282: intval = (sig_t) my_exit;
283: }
284: signal(SIGHUP, hupval);
285: signal(SIGINT, intval);
1.1 deraadt 286: }
287:
1.13 deraadt 288: /*
289: * How to handle certain events when in a critical region.
290: */
1.1 deraadt 291: void
1.13 deraadt 292: ignore_signals(void)
1.1 deraadt 293: {
1.13 deraadt 294: signal(SIGHUP, SIG_IGN);
295: signal(SIGINT, SIG_IGN);
1.1 deraadt 296: }
297:
1.13 deraadt 298: /*
299: * Make sure we'll have the directories to create a file. If `striplast' is
1.25 otto 300: * true, ignore the last element of `filename'.
1.13 deraadt 301: */
1.1 deraadt 302:
303: void
1.20 otto 304: makedirs(const char *filename, bool striplast)
1.1 deraadt 305: {
1.13 deraadt 306: char *tmpbuf;
1.9 provos 307:
1.13 deraadt 308: if ((tmpbuf = strdup(filename)) == NULL)
309: fatal("out of memory\n");
1.9 provos 310:
1.13 deraadt 311: if (striplast) {
312: char *s = strrchr(tmpbuf, '/');
1.34 oga 313: if (s == NULL) {
314: free(tmpbuf);
1.13 deraadt 315: return; /* nothing to be done */
1.34 oga 316: }
1.13 deraadt 317: *s = '\0';
318: }
1.31 otto 319: if (mkpath(tmpbuf) != 0)
1.30 espie 320: pfatal("creation of %s failed", tmpbuf);
321: free(tmpbuf);
1.1 deraadt 322: }
323:
1.13 deraadt 324: /*
325: * Make filenames more reasonable.
326: */
327: char *
1.27 millert 328: fetchname(const char *at, bool *exists, int strip_leading)
1.13 deraadt 329: {
1.27 millert 330: char *fullname, *name, *t;
1.29 otto 331: int sleading, tab;
1.20 otto 332: struct stat filestat;
1.1 deraadt 333:
1.20 otto 334: if (at == NULL || *at == '\0')
1.16 otto 335: return NULL;
1.13 deraadt 336: while (isspace(*at))
337: at++;
1.1 deraadt 338: #ifdef DEBUGGING
1.13 deraadt 339: if (debug & 128)
1.27 millert 340: say("fetchname %s %d\n", at, strip_leading);
1.1 deraadt 341: #endif
1.22 millert 342: /* So files can be created by diffing against /dev/null. */
343: if (strnEQ(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
344: return NULL;
1.13 deraadt 345: name = fullname = t = savestr(at);
346:
1.29 otto 347: tab = strchr(t, '\t') != NULL;
1.21 millert 348: /* Strip off up to `strip_leading' path components and NUL terminate. */
1.29 otto 349: for (sleading = strip_leading; *t != '\0' && ((tab && *t != '\t') ||
350: !isspace(*t)); t++) {
1.21 millert 351: if (t[0] == '/' && t[1] != '/' && t[1] != '\0')
1.24 otto 352: if (--sleading >= 0)
1.13 deraadt 353: name = t + 1;
1.21 millert 354: }
1.13 deraadt 355: *t = '\0';
356:
357: /*
358: * If no -p option was given (957 is the default value!), we were
359: * given a relative pathname, and the leading directories that we
360: * just stripped off all exist, put them back on.
361: */
362: if (strip_leading == 957 && name != fullname && *fullname != '/') {
363: name[-1] = '\0';
364: if (stat(fullname, &filestat) == 0 && S_ISDIR(filestat.st_mode)) {
365: name[-1] = '/';
366: name = fullname;
367: }
1.1 deraadt 368: }
1.13 deraadt 369: name = savestr(name);
370: free(fullname);
1.1 deraadt 371:
1.27 millert 372: *exists = stat(name, &filestat) == 0;
373: return name;
374: }
375:
376: /*
377: * Takes the name returned by fetchname and looks in RCS/SCCS directories
378: * for a checked in version.
379: */
380: char *
381: checked_in(char *file)
382: {
383: char *filebase, *filedir, tmpbuf[MAXPATHLEN];
384: struct stat filestat;
385:
386: filebase = basename(file);
387: filedir = dirname(file);
1.13 deraadt 388:
389: #define try(f, a1, a2, a3) \
1.27 millert 390: (snprintf(tmpbuf, sizeof tmpbuf, f, a1, a2, a3), stat(tmpbuf, &filestat) == 0)
1.13 deraadt 391:
1.27 millert 392: if (try("%s/RCS/%s%s", filedir, filebase, RCSSUFFIX) ||
393: try("%s/RCS/%s%s", filedir, filebase, "") ||
394: try("%s/%s%s", filedir, filebase, RCSSUFFIX) ||
395: try("%s/SCCS/%s%s", filedir, SCCSPREFIX, filebase) ||
396: try("%s/%s%s", filedir, SCCSPREFIX, filebase))
397: return file;
398:
399: return NULL;
1.12 deraadt 400: }
401:
402: void
1.13 deraadt 403: version(void)
1.12 deraadt 404: {
1.13 deraadt 405: fprintf(stderr, "Patch version 2.0-12u8-OpenBSD\n");
1.19 millert 406: my_exit(EXIT_SUCCESS);
1.16 otto 407: }
408:
409: /*
410: * Exit with cleanup.
411: */
412: void
413: my_exit(int status)
414: {
415: unlink(TMPINNAME);
416: if (!toutkeep)
417: unlink(TMPOUTNAME);
418: if (!trejkeep)
419: unlink(TMPREJNAME);
420: unlink(TMPPATNAME);
421: exit(status);
1.1 deraadt 422: }