12dd076b8SGabor Kovesdan /*-
22dd076b8SGabor Kovesdan * Copyright 1986, Larry Wall
32dd076b8SGabor Kovesdan *
42dd076b8SGabor Kovesdan * Redistribution and use in source and binary forms, with or without
52dd076b8SGabor Kovesdan * modification, are permitted provided that the following condition is met:
62dd076b8SGabor Kovesdan * 1. Redistributions of source code must retain the above copyright notice,
72dd076b8SGabor Kovesdan * this condition and the following disclaimer.
82dd076b8SGabor Kovesdan *
92dd076b8SGabor Kovesdan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
102dd076b8SGabor Kovesdan * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
112dd076b8SGabor Kovesdan * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
122dd076b8SGabor Kovesdan * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
132dd076b8SGabor Kovesdan * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
142dd076b8SGabor Kovesdan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
152dd076b8SGabor Kovesdan * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
162dd076b8SGabor Kovesdan * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
172dd076b8SGabor Kovesdan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
182dd076b8SGabor Kovesdan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
192dd076b8SGabor Kovesdan * SUCH DAMAGE.
202dd076b8SGabor Kovesdan *
212dd076b8SGabor Kovesdan * patch - a program to apply diffs to original files
222dd076b8SGabor Kovesdan *
232dd076b8SGabor Kovesdan * -C option added in 1998, original code by Marc Espie, based on FreeBSD
242dd076b8SGabor Kovesdan * behaviour
252dd076b8SGabor Kovesdan *
26e56ef7d3SXin LI * $OpenBSD: util.c,v 1.35 2010/07/24 01:10:12 ray Exp $
272dd076b8SGabor Kovesdan */
282dd076b8SGabor Kovesdan
292dd076b8SGabor Kovesdan #include <sys/stat.h>
302dd076b8SGabor Kovesdan
312dd076b8SGabor Kovesdan #include <ctype.h>
322dd076b8SGabor Kovesdan #include <errno.h>
332dd076b8SGabor Kovesdan #include <fcntl.h>
342dd076b8SGabor Kovesdan #include <libgen.h>
35df6e4074SPedro F. Giffuni #include <limits.h>
362dd076b8SGabor Kovesdan #include <paths.h>
372dd076b8SGabor Kovesdan #include <signal.h>
382dd076b8SGabor Kovesdan #include <stdarg.h>
392dd076b8SGabor Kovesdan #include <stdlib.h>
402dd076b8SGabor Kovesdan #include <stdio.h>
412dd076b8SGabor Kovesdan #include <string.h>
422dd076b8SGabor Kovesdan #include <unistd.h>
432dd076b8SGabor Kovesdan
442dd076b8SGabor Kovesdan #include "common.h"
452dd076b8SGabor Kovesdan #include "util.h"
462dd076b8SGabor Kovesdan #include "backupfile.h"
472dd076b8SGabor Kovesdan #include "pathnames.h"
482dd076b8SGabor Kovesdan
492dd076b8SGabor Kovesdan /* Rename a file, copying it if necessary. */
502dd076b8SGabor Kovesdan
512dd076b8SGabor Kovesdan int
move_file(const char * from,const char * to)522dd076b8SGabor Kovesdan move_file(const char *from, const char *to)
532dd076b8SGabor Kovesdan {
542dd076b8SGabor Kovesdan int fromfd;
552dd076b8SGabor Kovesdan ssize_t i;
562dd076b8SGabor Kovesdan
572dd076b8SGabor Kovesdan /* to stdout? */
582dd076b8SGabor Kovesdan
592dd076b8SGabor Kovesdan if (strEQ(to, "-")) {
602dd076b8SGabor Kovesdan #ifdef DEBUGGING
612dd076b8SGabor Kovesdan if (debug & 4)
622dd076b8SGabor Kovesdan say("Moving %s to stdout.\n", from);
632dd076b8SGabor Kovesdan #endif
642dd076b8SGabor Kovesdan fromfd = open(from, O_RDONLY);
652dd076b8SGabor Kovesdan if (fromfd < 0)
662dd076b8SGabor Kovesdan pfatal("internal error, can't reopen %s", from);
672dd076b8SGabor Kovesdan while ((i = read(fromfd, buf, buf_size)) > 0)
682dd076b8SGabor Kovesdan if (write(STDOUT_FILENO, buf, i) != i)
692dd076b8SGabor Kovesdan pfatal("write failed");
702dd076b8SGabor Kovesdan close(fromfd);
712dd076b8SGabor Kovesdan return 0;
722dd076b8SGabor Kovesdan }
732dd076b8SGabor Kovesdan if (backup_file(to) < 0) {
742dd076b8SGabor Kovesdan say("Can't backup %s, output is in %s: %s\n", to, from,
752dd076b8SGabor Kovesdan strerror(errno));
762dd076b8SGabor Kovesdan return -1;
772dd076b8SGabor Kovesdan }
782dd076b8SGabor Kovesdan #ifdef DEBUGGING
792dd076b8SGabor Kovesdan if (debug & 4)
802dd076b8SGabor Kovesdan say("Moving %s to %s.\n", from, to);
812dd076b8SGabor Kovesdan #endif
822dd076b8SGabor Kovesdan if (rename(from, to) < 0) {
832dd076b8SGabor Kovesdan if (errno != EXDEV || copy_file(from, to) < 0) {
842dd076b8SGabor Kovesdan say("Can't create %s, output is in %s: %s\n",
852dd076b8SGabor Kovesdan to, from, strerror(errno));
862dd076b8SGabor Kovesdan return -1;
872dd076b8SGabor Kovesdan }
882dd076b8SGabor Kovesdan }
892dd076b8SGabor Kovesdan return 0;
902dd076b8SGabor Kovesdan }
912dd076b8SGabor Kovesdan
922dd076b8SGabor Kovesdan /* Backup the original file. */
932dd076b8SGabor Kovesdan
942dd076b8SGabor Kovesdan int
backup_file(const char * orig)952dd076b8SGabor Kovesdan backup_file(const char *orig)
962dd076b8SGabor Kovesdan {
972dd076b8SGabor Kovesdan struct stat filestat;
98df6e4074SPedro F. Giffuni char bakname[PATH_MAX], *s, *simplename;
992dd076b8SGabor Kovesdan dev_t orig_device;
1002dd076b8SGabor Kovesdan ino_t orig_inode;
1012dd076b8SGabor Kovesdan
1022dd076b8SGabor Kovesdan if (backup_type == none || stat(orig, &filestat) != 0)
1032dd076b8SGabor Kovesdan return 0; /* nothing to do */
1042dd076b8SGabor Kovesdan /*
1052dd076b8SGabor Kovesdan * If the user used zero prefixes or suffixes, then
1062dd076b8SGabor Kovesdan * he doesn't want backups. Yet we have to remove
1072dd076b8SGabor Kovesdan * orig to break possible hardlinks.
1082dd076b8SGabor Kovesdan */
1092dd076b8SGabor Kovesdan if ((origprae && *origprae == 0) || *simple_backup_suffix == 0) {
1102dd076b8SGabor Kovesdan unlink(orig);
1112dd076b8SGabor Kovesdan return 0;
1122dd076b8SGabor Kovesdan }
1132dd076b8SGabor Kovesdan orig_device = filestat.st_dev;
1142dd076b8SGabor Kovesdan orig_inode = filestat.st_ino;
1152dd076b8SGabor Kovesdan
1162dd076b8SGabor Kovesdan if (origprae) {
1172dd076b8SGabor Kovesdan if (strlcpy(bakname, origprae, sizeof(bakname)) >= sizeof(bakname) ||
1182dd076b8SGabor Kovesdan strlcat(bakname, orig, sizeof(bakname)) >= sizeof(bakname))
1192dd076b8SGabor Kovesdan fatal("filename %s too long for buffer\n", origprae);
1202dd076b8SGabor Kovesdan } else {
1212dd076b8SGabor Kovesdan if ((s = find_backup_file_name(orig)) == NULL)
1222dd076b8SGabor Kovesdan fatal("out of memory\n");
1232dd076b8SGabor Kovesdan if (strlcpy(bakname, s, sizeof(bakname)) >= sizeof(bakname))
1242dd076b8SGabor Kovesdan fatal("filename %s too long for buffer\n", s);
1252dd076b8SGabor Kovesdan free(s);
1262dd076b8SGabor Kovesdan }
1272dd076b8SGabor Kovesdan
1282dd076b8SGabor Kovesdan if ((simplename = strrchr(bakname, '/')) != NULL)
1292dd076b8SGabor Kovesdan simplename = simplename + 1;
1302dd076b8SGabor Kovesdan else
1312dd076b8SGabor Kovesdan simplename = bakname;
1322dd076b8SGabor Kovesdan
1332dd076b8SGabor Kovesdan /*
1342dd076b8SGabor Kovesdan * Find a backup name that is not the same file. Change the
1352dd076b8SGabor Kovesdan * first lowercase char into uppercase; if that isn't
1362dd076b8SGabor Kovesdan * sufficient, chop off the first char and try again.
1372dd076b8SGabor Kovesdan */
1382dd076b8SGabor Kovesdan while (stat(bakname, &filestat) == 0 &&
1392dd076b8SGabor Kovesdan orig_device == filestat.st_dev && orig_inode == filestat.st_ino) {
1402dd076b8SGabor Kovesdan /* Skip initial non-lowercase chars. */
1412dd076b8SGabor Kovesdan for (s = simplename; *s && !islower((unsigned char)*s); s++)
1422dd076b8SGabor Kovesdan ;
1432dd076b8SGabor Kovesdan if (*s)
1442dd076b8SGabor Kovesdan *s = toupper((unsigned char)*s);
1452dd076b8SGabor Kovesdan else
1462dd076b8SGabor Kovesdan memmove(simplename, simplename + 1,
1472dd076b8SGabor Kovesdan strlen(simplename + 1) + 1);
1482dd076b8SGabor Kovesdan }
1492dd076b8SGabor Kovesdan #ifdef DEBUGGING
1502dd076b8SGabor Kovesdan if (debug & 4)
1512dd076b8SGabor Kovesdan say("Moving %s to %s.\n", orig, bakname);
1522dd076b8SGabor Kovesdan #endif
1532dd076b8SGabor Kovesdan if (rename(orig, bakname) < 0) {
1542dd076b8SGabor Kovesdan if (errno != EXDEV || copy_file(orig, bakname) < 0)
1552dd076b8SGabor Kovesdan return -1;
1562dd076b8SGabor Kovesdan }
1572dd076b8SGabor Kovesdan return 0;
1582dd076b8SGabor Kovesdan }
1592dd076b8SGabor Kovesdan
1602dd076b8SGabor Kovesdan /*
1612dd076b8SGabor Kovesdan * Copy a file.
1622dd076b8SGabor Kovesdan */
1632dd076b8SGabor Kovesdan int
copy_file(const char * from,const char * to)1642dd076b8SGabor Kovesdan copy_file(const char *from, const char *to)
1652dd076b8SGabor Kovesdan {
1662dd076b8SGabor Kovesdan int tofd, fromfd;
1672dd076b8SGabor Kovesdan ssize_t i;
1682dd076b8SGabor Kovesdan
1692dd076b8SGabor Kovesdan tofd = open(to, O_CREAT|O_TRUNC|O_WRONLY, 0666);
1702dd076b8SGabor Kovesdan if (tofd < 0)
1712dd076b8SGabor Kovesdan return -1;
1722dd076b8SGabor Kovesdan fromfd = open(from, O_RDONLY, 0);
1732dd076b8SGabor Kovesdan if (fromfd < 0)
1742dd076b8SGabor Kovesdan pfatal("internal error, can't reopen %s", from);
1752dd076b8SGabor Kovesdan while ((i = read(fromfd, buf, buf_size)) > 0)
1762dd076b8SGabor Kovesdan if (write(tofd, buf, i) != i)
1772dd076b8SGabor Kovesdan pfatal("write to %s failed", to);
1782dd076b8SGabor Kovesdan close(fromfd);
1792dd076b8SGabor Kovesdan close(tofd);
1802dd076b8SGabor Kovesdan return 0;
1812dd076b8SGabor Kovesdan }
1822dd076b8SGabor Kovesdan
1832dd076b8SGabor Kovesdan /*
1842dd076b8SGabor Kovesdan * Allocate a unique area for a string.
1852dd076b8SGabor Kovesdan */
1862dd076b8SGabor Kovesdan char *
savestr(const char * s)1872dd076b8SGabor Kovesdan savestr(const char *s)
1882dd076b8SGabor Kovesdan {
1892dd076b8SGabor Kovesdan char *rv;
1902dd076b8SGabor Kovesdan
1912dd076b8SGabor Kovesdan if (!s)
1922dd076b8SGabor Kovesdan s = "Oops";
1932dd076b8SGabor Kovesdan rv = strdup(s);
1942dd076b8SGabor Kovesdan if (rv == NULL) {
1952dd076b8SGabor Kovesdan if (using_plan_a)
1962dd076b8SGabor Kovesdan out_of_mem = true;
1972dd076b8SGabor Kovesdan else
1982dd076b8SGabor Kovesdan fatal("out of memory\n");
1992dd076b8SGabor Kovesdan }
2002dd076b8SGabor Kovesdan return rv;
2012dd076b8SGabor Kovesdan }
2022dd076b8SGabor Kovesdan
2032dd076b8SGabor Kovesdan /*
204547e0acbSPedro F. Giffuni * Allocate a unique area for a string. Call fatal if out of memory.
205547e0acbSPedro F. Giffuni */
206547e0acbSPedro F. Giffuni char *
xstrdup(const char * s)207547e0acbSPedro F. Giffuni xstrdup(const char *s)
208547e0acbSPedro F. Giffuni {
209547e0acbSPedro F. Giffuni char *rv;
210547e0acbSPedro F. Giffuni
211547e0acbSPedro F. Giffuni if (!s)
212547e0acbSPedro F. Giffuni s = "Oops";
213547e0acbSPedro F. Giffuni rv = strdup(s);
214547e0acbSPedro F. Giffuni if (rv == NULL)
215547e0acbSPedro F. Giffuni fatal("out of memory\n");
216547e0acbSPedro F. Giffuni return rv;
217547e0acbSPedro F. Giffuni }
218547e0acbSPedro F. Giffuni
219547e0acbSPedro F. Giffuni /*
2202dd076b8SGabor Kovesdan * Vanilla terminal output (buffered).
2212dd076b8SGabor Kovesdan */
2222dd076b8SGabor Kovesdan void
say(const char * fmt,...)2232dd076b8SGabor Kovesdan say(const char *fmt, ...)
2242dd076b8SGabor Kovesdan {
2252dd076b8SGabor Kovesdan va_list ap;
2262dd076b8SGabor Kovesdan
2272dd076b8SGabor Kovesdan va_start(ap, fmt);
228e56ef7d3SXin LI vfprintf(stdout, fmt, ap);
2292dd076b8SGabor Kovesdan va_end(ap);
230e56ef7d3SXin LI fflush(stdout);
2312dd076b8SGabor Kovesdan }
2322dd076b8SGabor Kovesdan
2332dd076b8SGabor Kovesdan /*
2342dd076b8SGabor Kovesdan * Terminal output, pun intended.
2352dd076b8SGabor Kovesdan */
2362dd076b8SGabor Kovesdan void
fatal(const char * fmt,...)2372dd076b8SGabor Kovesdan fatal(const char *fmt, ...)
2382dd076b8SGabor Kovesdan {
2392dd076b8SGabor Kovesdan va_list ap;
2402dd076b8SGabor Kovesdan
2412dd076b8SGabor Kovesdan va_start(ap, fmt);
2422dd076b8SGabor Kovesdan fprintf(stderr, "patch: **** ");
2432dd076b8SGabor Kovesdan vfprintf(stderr, fmt, ap);
2442dd076b8SGabor Kovesdan va_end(ap);
2452dd076b8SGabor Kovesdan my_exit(2);
2462dd076b8SGabor Kovesdan }
2472dd076b8SGabor Kovesdan
2482dd076b8SGabor Kovesdan /*
2492dd076b8SGabor Kovesdan * Say something from patch, something from the system, then silence . . .
2502dd076b8SGabor Kovesdan */
2512dd076b8SGabor Kovesdan void
pfatal(const char * fmt,...)2522dd076b8SGabor Kovesdan pfatal(const char *fmt, ...)
2532dd076b8SGabor Kovesdan {
2542dd076b8SGabor Kovesdan va_list ap;
2552dd076b8SGabor Kovesdan int errnum = errno;
2562dd076b8SGabor Kovesdan
2572dd076b8SGabor Kovesdan fprintf(stderr, "patch: **** ");
2582dd076b8SGabor Kovesdan va_start(ap, fmt);
2592dd076b8SGabor Kovesdan vfprintf(stderr, fmt, ap);
2602dd076b8SGabor Kovesdan va_end(ap);
2612dd076b8SGabor Kovesdan fprintf(stderr, ": %s\n", strerror(errnum));
2622dd076b8SGabor Kovesdan my_exit(2);
2632dd076b8SGabor Kovesdan }
2642dd076b8SGabor Kovesdan
2652dd076b8SGabor Kovesdan /*
2662dd076b8SGabor Kovesdan * Get a response from the user via /dev/tty
2672dd076b8SGabor Kovesdan */
2682dd076b8SGabor Kovesdan void
ask(const char * fmt,...)2692dd076b8SGabor Kovesdan ask(const char *fmt, ...)
2702dd076b8SGabor Kovesdan {
2712dd076b8SGabor Kovesdan va_list ap;
2722dd076b8SGabor Kovesdan ssize_t nr = 0;
2732dd076b8SGabor Kovesdan static int ttyfd = -1;
2742dd076b8SGabor Kovesdan
2752dd076b8SGabor Kovesdan va_start(ap, fmt);
2762dd076b8SGabor Kovesdan vfprintf(stdout, fmt, ap);
2772dd076b8SGabor Kovesdan va_end(ap);
2782dd076b8SGabor Kovesdan fflush(stdout);
2792dd076b8SGabor Kovesdan if (ttyfd < 0)
2802dd076b8SGabor Kovesdan ttyfd = open(_PATH_TTY, O_RDONLY);
2812dd076b8SGabor Kovesdan if (ttyfd >= 0) {
2822dd076b8SGabor Kovesdan if ((nr = read(ttyfd, buf, buf_size)) > 0 &&
2832dd076b8SGabor Kovesdan buf[nr - 1] == '\n')
2842dd076b8SGabor Kovesdan buf[nr - 1] = '\0';
2852dd076b8SGabor Kovesdan }
2862dd076b8SGabor Kovesdan if (ttyfd < 0 || nr <= 0) {
2872dd076b8SGabor Kovesdan /* no tty or error reading, pretend user entered 'return' */
2882dd076b8SGabor Kovesdan putchar('\n');
2892dd076b8SGabor Kovesdan buf[0] = '\0';
2902dd076b8SGabor Kovesdan }
2912dd076b8SGabor Kovesdan }
2922dd076b8SGabor Kovesdan
2932dd076b8SGabor Kovesdan /*
2942dd076b8SGabor Kovesdan * How to handle certain events when not in a critical region.
2952dd076b8SGabor Kovesdan */
2962dd076b8SGabor Kovesdan void
set_signals(int reset)2972dd076b8SGabor Kovesdan set_signals(int reset)
2982dd076b8SGabor Kovesdan {
2992dd076b8SGabor Kovesdan static sig_t hupval, intval;
3002dd076b8SGabor Kovesdan
3012dd076b8SGabor Kovesdan if (!reset) {
3022dd076b8SGabor Kovesdan hupval = signal(SIGHUP, SIG_IGN);
3032dd076b8SGabor Kovesdan if (hupval != SIG_IGN)
3042dd076b8SGabor Kovesdan hupval = my_exit;
3052dd076b8SGabor Kovesdan intval = signal(SIGINT, SIG_IGN);
3062dd076b8SGabor Kovesdan if (intval != SIG_IGN)
3072dd076b8SGabor Kovesdan intval = my_exit;
3082dd076b8SGabor Kovesdan }
3092dd076b8SGabor Kovesdan signal(SIGHUP, hupval);
3102dd076b8SGabor Kovesdan signal(SIGINT, intval);
3112dd076b8SGabor Kovesdan }
3122dd076b8SGabor Kovesdan
3132dd076b8SGabor Kovesdan /*
3142dd076b8SGabor Kovesdan * How to handle certain events when in a critical region.
3152dd076b8SGabor Kovesdan */
3162dd076b8SGabor Kovesdan void
ignore_signals(void)3172dd076b8SGabor Kovesdan ignore_signals(void)
3182dd076b8SGabor Kovesdan {
3192dd076b8SGabor Kovesdan signal(SIGHUP, SIG_IGN);
3202dd076b8SGabor Kovesdan signal(SIGINT, SIG_IGN);
3212dd076b8SGabor Kovesdan }
3222dd076b8SGabor Kovesdan
3232dd076b8SGabor Kovesdan /*
3242dd076b8SGabor Kovesdan * Make sure we'll have the directories to create a file. If `striplast' is
3252dd076b8SGabor Kovesdan * true, ignore the last element of `filename'.
3262dd076b8SGabor Kovesdan */
3272dd076b8SGabor Kovesdan
3282dd076b8SGabor Kovesdan void
makedirs(const char * filename,bool striplast)3292dd076b8SGabor Kovesdan makedirs(const char *filename, bool striplast)
3302dd076b8SGabor Kovesdan {
3312dd076b8SGabor Kovesdan char *tmpbuf;
3322dd076b8SGabor Kovesdan
3332dd076b8SGabor Kovesdan if ((tmpbuf = strdup(filename)) == NULL)
3342dd076b8SGabor Kovesdan fatal("out of memory\n");
3352dd076b8SGabor Kovesdan
3362dd076b8SGabor Kovesdan if (striplast) {
3372dd076b8SGabor Kovesdan char *s = strrchr(tmpbuf, '/');
3382dd076b8SGabor Kovesdan if (s == NULL) {
3392dd076b8SGabor Kovesdan free(tmpbuf);
3402dd076b8SGabor Kovesdan return; /* nothing to be done */
3412dd076b8SGabor Kovesdan }
3422dd076b8SGabor Kovesdan *s = '\0';
3432dd076b8SGabor Kovesdan }
3442dd076b8SGabor Kovesdan if (mkpath(tmpbuf) != 0)
3452dd076b8SGabor Kovesdan pfatal("creation of %s failed", tmpbuf);
3462dd076b8SGabor Kovesdan free(tmpbuf);
3472dd076b8SGabor Kovesdan }
3482dd076b8SGabor Kovesdan
3492dd076b8SGabor Kovesdan /*
3502dd076b8SGabor Kovesdan * Make filenames more reasonable.
3512dd076b8SGabor Kovesdan */
3522dd076b8SGabor Kovesdan char *
fetchname(const char * at,bool * exists,int strip_leading)3532dd076b8SGabor Kovesdan fetchname(const char *at, bool *exists, int strip_leading)
3542dd076b8SGabor Kovesdan {
3552dd076b8SGabor Kovesdan char *fullname, *name, *t;
3562dd076b8SGabor Kovesdan int sleading, tab;
3572dd076b8SGabor Kovesdan struct stat filestat;
3582dd076b8SGabor Kovesdan
3592dd076b8SGabor Kovesdan if (at == NULL || *at == '\0')
3602dd076b8SGabor Kovesdan return NULL;
3612dd076b8SGabor Kovesdan while (isspace((unsigned char)*at))
3622dd076b8SGabor Kovesdan at++;
3632dd076b8SGabor Kovesdan #ifdef DEBUGGING
3642dd076b8SGabor Kovesdan if (debug & 128)
3652dd076b8SGabor Kovesdan say("fetchname %s %d\n", at, strip_leading);
3662dd076b8SGabor Kovesdan #endif
3672dd076b8SGabor Kovesdan /* So files can be created by diffing against /dev/null. */
368*50dacbf6SKyle Evans if (strnEQ(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1)) {
369*50dacbf6SKyle Evans *exists = true;
3702dd076b8SGabor Kovesdan return NULL;
371*50dacbf6SKyle Evans }
3722dd076b8SGabor Kovesdan name = fullname = t = savestr(at);
3732dd076b8SGabor Kovesdan
3742dd076b8SGabor Kovesdan tab = strchr(t, '\t') != NULL;
3752dd076b8SGabor Kovesdan /* Strip off up to `strip_leading' path components and NUL terminate. */
3762dd076b8SGabor Kovesdan for (sleading = strip_leading; *t != '\0' && ((tab && *t != '\t') ||
3772dd076b8SGabor Kovesdan !isspace((unsigned char)*t)); t++) {
3782dd076b8SGabor Kovesdan if (t[0] == '/' && t[1] != '/' && t[1] != '\0')
3792dd076b8SGabor Kovesdan if (--sleading >= 0)
3802dd076b8SGabor Kovesdan name = t + 1;
3812dd076b8SGabor Kovesdan }
3822dd076b8SGabor Kovesdan *t = '\0';
3832dd076b8SGabor Kovesdan
3842dd076b8SGabor Kovesdan /*
3852dd076b8SGabor Kovesdan * If no -p option was given (957 is the default value!), we were
3862dd076b8SGabor Kovesdan * given a relative pathname, and the leading directories that we
3872dd076b8SGabor Kovesdan * just stripped off all exist, put them back on.
3882dd076b8SGabor Kovesdan */
3892dd076b8SGabor Kovesdan if (strip_leading == 957 && name != fullname && *fullname != '/') {
3902dd076b8SGabor Kovesdan name[-1] = '\0';
3912dd076b8SGabor Kovesdan if (stat(fullname, &filestat) == 0 && S_ISDIR(filestat.st_mode)) {
3922dd076b8SGabor Kovesdan name[-1] = '/';
3932dd076b8SGabor Kovesdan name = fullname;
3942dd076b8SGabor Kovesdan }
3952dd076b8SGabor Kovesdan }
3962dd076b8SGabor Kovesdan name = savestr(name);
3972dd076b8SGabor Kovesdan free(fullname);
3982dd076b8SGabor Kovesdan
3992dd076b8SGabor Kovesdan *exists = stat(name, &filestat) == 0;
4002dd076b8SGabor Kovesdan return name;
4012dd076b8SGabor Kovesdan }
4022dd076b8SGabor Kovesdan
4032dd076b8SGabor Kovesdan void
version(void)4042dd076b8SGabor Kovesdan version(void)
4052dd076b8SGabor Kovesdan {
406e678759cSXin LI printf("patch 2.0-12u11 FreeBSD\n");
4072dd076b8SGabor Kovesdan my_exit(EXIT_SUCCESS);
4082dd076b8SGabor Kovesdan }
4092dd076b8SGabor Kovesdan
4102dd076b8SGabor Kovesdan /*
4112dd076b8SGabor Kovesdan * Exit with cleanup.
4122dd076b8SGabor Kovesdan */
4132dd076b8SGabor Kovesdan void
my_exit(int status)4142dd076b8SGabor Kovesdan my_exit(int status)
4152dd076b8SGabor Kovesdan {
4162dd076b8SGabor Kovesdan unlink(TMPINNAME);
4172dd076b8SGabor Kovesdan if (!toutkeep)
4182dd076b8SGabor Kovesdan unlink(TMPOUTNAME);
4192dd076b8SGabor Kovesdan if (!trejkeep)
4202dd076b8SGabor Kovesdan unlink(TMPREJNAME);
4212dd076b8SGabor Kovesdan unlink(TMPPATNAME);
4222dd076b8SGabor Kovesdan exit(status);
4232dd076b8SGabor Kovesdan }
424