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