Home | History | Annotate | Line # | Download | only in patch
util.c revision 1.19
      1  1.19    itojun /*	$NetBSD: util.c,v 1.19 2003/07/30 08:51:55 itojun Exp $	*/
      2  1.16    itojun 
      3  1.16    itojun /*
      4  1.16    itojun  * Copyright (c) 1988, Larry Wall
      5  1.16    itojun  *
      6  1.16    itojun  * Redistribution and use in source and binary forms, with or without
      7  1.16    itojun  * modification, are permitted provided that the following condition
      8  1.16    itojun  * is met:
      9  1.16    itojun  *  1. Redistributions of source code must retain the above copyright
     10  1.16    itojun  *     notice, this condition and the following disclaimer.
     11  1.16    itojun  *
     12  1.16    itojun  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     13  1.16    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     14  1.16    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     15  1.16    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     16  1.16    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     17  1.16    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     18  1.16    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     19  1.16    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     20  1.16    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     21  1.16    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     22  1.16    itojun  * SUCH DAMAGE.
     23  1.16    itojun  */
     24  1.16    itojun 
     25   1.5  christos #include <sys/cdefs.h>
     26   1.2   mycroft #ifndef lint
     27  1.19    itojun __RCSID("$NetBSD: util.c,v 1.19 2003/07/30 08:51:55 itojun Exp $");
     28   1.2   mycroft #endif /* not lint */
     29   1.2   mycroft 
     30  1.12  kristerw #include <sys/param.h>
     31  1.12  kristerw 
     32   1.1       cgd #include "EXTERN.h"
     33   1.1       cgd #include "common.h"
     34   1.1       cgd #include "INTERN.h"
     35   1.1       cgd #include "util.h"
     36   1.1       cgd #include "backupfile.h"
     37  1.10  kristerw 
     38   1.5  christos #include <stdarg.h>
     39   1.5  christos #include <stdlib.h>
     40   1.5  christos #include <unistd.h>
     41   1.5  christos #include <fcntl.h>
     42   1.1       cgd 
     43  1.11  kristerw /*
     44  1.11  kristerw  * Rename a file, copying it if necessary.
     45  1.11  kristerw  */
     46   1.1       cgd int
     47   1.9  kristerw move_file(char *from, char *to)
     48   1.1       cgd {
     49  1.12  kristerw 	char bakname[MAXPATHLEN];
     50  1.11  kristerw 	char *s;
     51  1.14  kristerw 	size_t i;
     52  1.11  kristerw 	int fromfd;
     53   1.1       cgd 
     54  1.11  kristerw 	/* to stdout? */
     55   1.1       cgd 
     56  1.11  kristerw 	if (strEQ(to, "-")) {
     57   1.1       cgd #ifdef DEBUGGING
     58  1.11  kristerw 		if (debug & 4)
     59  1.11  kristerw 			say("Moving %s to stdout.\n", from);
     60   1.1       cgd #endif
     61  1.11  kristerw 		fromfd = open(from, 0);
     62  1.11  kristerw 		if (fromfd < 0)
     63  1.11  kristerw 			pfatal("internal error, can't reopen %s", from);
     64  1.11  kristerw 		while ((i = read(fromfd, buf, sizeof buf)) > 0)
     65  1.11  kristerw 			if (write(1, buf, i) != 1)
     66  1.11  kristerw 				pfatal("write failed");
     67  1.11  kristerw 		Close(fromfd);
     68  1.11  kristerw 		return 0;
     69  1.11  kristerw 	}
     70   1.1       cgd 
     71  1.11  kristerw 	if (origprae) {
     72  1.18    itojun 		strlcpy(bakname, origprae, sizeof(bakname));
     73  1.18    itojun 		strlcat(bakname, to, sizeof(bakname));
     74  1.11  kristerw 	} else {
     75   1.1       cgd #ifndef NODIR
     76  1.11  kristerw 		char *backupname = find_backup_file_name(to);
     77  1.18    itojun 		strlcpy(bakname, backupname, sizeof(bakname));
     78  1.11  kristerw 		free(backupname);
     79   1.1       cgd #else /* NODIR */
     80  1.18    itojun 		strlcpy(bakname, to, sizeof(bakname));
     81  1.18    itojun     		strlcat(bakname, simple_backup_suffix, sizeof(bakname));
     82   1.1       cgd #endif /* NODIR */
     83  1.11  kristerw 	}
     84   1.1       cgd 
     85  1.11  kristerw 	if (stat(to, &filestat) == 0) {	/* output file exists */
     86  1.11  kristerw 		dev_t to_device = filestat.st_dev;
     87  1.11  kristerw 		ino_t to_inode  = filestat.st_ino;
     88  1.11  kristerw 		char *simplename = bakname;
     89   1.1       cgd 
     90  1.11  kristerw 		for (s = bakname; *s; s++) {
     91  1.11  kristerw 			if (*s == '/')
     92  1.11  kristerw 				simplename = s + 1;
     93  1.11  kristerw 		}
     94  1.11  kristerw 		/*
     95  1.11  kristerw 		 * Find a backup name that is not the same file.
     96  1.11  kristerw 		 * Change the first lowercase char into uppercase;
     97  1.11  kristerw 		 * if that isn't sufficient, chop off the first char
     98  1.11  kristerw 		 * and try again.
     99  1.11  kristerw 		 */
    100  1.11  kristerw 		while (stat(bakname, &filestat) == 0 &&
    101  1.11  kristerw 		       to_device == filestat.st_dev &&
    102  1.11  kristerw 		       to_inode == filestat.st_ino) {
    103  1.11  kristerw 			/* Skip initial non-lowercase chars. */
    104  1.11  kristerw 			for (s = simplename;
    105  1.11  kristerw 			     *s && !islower((unsigned char)*s);
    106  1.11  kristerw 			     s++)
    107  1.11  kristerw 				;
    108  1.11  kristerw 			if (*s)
    109  1.11  kristerw 				*s = toupper(*s);
    110  1.11  kristerw 			else
    111  1.19    itojun 				strcpy(simplename, simplename + 1);
    112  1.11  kristerw 		}
    113  1.11  kristerw 		while (unlink(bakname) >= 0)
    114  1.11  kristerw 			;	/* while() is for benefit of Eunice */
    115  1.11  kristerw #ifdef DEBUGGING
    116  1.11  kristerw 		if (debug & 4)
    117  1.11  kristerw 			say("Moving %s to %s.\n", to, bakname);
    118  1.11  kristerw #endif
    119  1.11  kristerw 		if (link(to, bakname) < 0) {
    120  1.11  kristerw 			/*
    121  1.11  kristerw 			 * Maybe `to' is a symlink into a different file
    122  1.11  kristerw 			 * system. Copying replaces the symlink with a file;
    123  1.11  kristerw 			 * using rename would be better.
    124  1.11  kristerw 			 */
    125  1.11  kristerw 			int tofd;
    126  1.11  kristerw 			int bakfd;
    127  1.11  kristerw 
    128  1.11  kristerw 			bakfd = creat(bakname, 0666);
    129  1.11  kristerw 			if (bakfd < 0) {
    130  1.11  kristerw 				say("Can't backup %s, output is in %s: %s\n",
    131  1.11  kristerw 				    to, from, strerror(errno));
    132  1.11  kristerw 				return -1;
    133  1.11  kristerw 			}
    134  1.11  kristerw 			tofd = open(to, 0);
    135  1.11  kristerw 			if (tofd < 0)
    136  1.11  kristerw 				pfatal("internal error, can't open %s", to);
    137  1.11  kristerw 			while ((i = read(tofd, buf, sizeof buf)) > 0)
    138  1.11  kristerw 				if (write(bakfd, buf, i) != i)
    139  1.11  kristerw 					pfatal("write failed");
    140  1.11  kristerw 			Close(tofd);
    141  1.11  kristerw 			Close(bakfd);
    142  1.11  kristerw 		}
    143  1.11  kristerw 		while (unlink(to) >= 0) ;
    144   1.1       cgd 	}
    145   1.1       cgd #ifdef DEBUGGING
    146   1.1       cgd 	if (debug & 4)
    147  1.11  kristerw 		say("Moving %s to %s.\n", from, to);
    148   1.1       cgd #endif
    149  1.11  kristerw 	if (link(from, to) < 0) {		/* different file system? */
    150  1.11  kristerw 		int tofd;
    151  1.11  kristerw 
    152  1.11  kristerw 		tofd = creat(to, 0666);
    153  1.11  kristerw 		if (tofd < 0) {
    154  1.11  kristerw 			say("Can't create %s, output is in %s: %s\n",
    155  1.11  kristerw 			    to, from, strerror(errno));
    156  1.11  kristerw 			return -1;
    157  1.11  kristerw 		}
    158  1.11  kristerw 		fromfd = open(from, 0);
    159  1.11  kristerw 		if (fromfd < 0)
    160  1.11  kristerw 			pfatal("internal error, can't reopen %s", from);
    161  1.11  kristerw 		while ((i = read(fromfd, buf, sizeof buf)) > 0)
    162  1.11  kristerw 			if (write(tofd, buf, i) != i)
    163  1.11  kristerw 				pfatal("write failed");
    164  1.11  kristerw 		Close(fromfd);
    165  1.11  kristerw 		Close(tofd);
    166   1.1       cgd 	}
    167  1.11  kristerw 	Unlink(from);
    168  1.11  kristerw 	return 0;
    169  1.11  kristerw }
    170  1.11  kristerw 
    171  1.11  kristerw /*
    172  1.11  kristerw  * Copy a file.
    173  1.11  kristerw  */
    174  1.11  kristerw void
    175  1.11  kristerw copy_file(char *from, char *to)
    176  1.11  kristerw {
    177   1.9  kristerw 	int tofd;
    178  1.11  kristerw 	int fromfd;
    179  1.14  kristerw 	size_t i;
    180  1.11  kristerw 
    181   1.1       cgd 	tofd = creat(to, 0666);
    182  1.11  kristerw 	if (tofd < 0)
    183  1.11  kristerw 		pfatal("can't create %s", to);
    184   1.1       cgd 	fromfd = open(from, 0);
    185   1.1       cgd 	if (fromfd < 0)
    186  1.11  kristerw 		pfatal("internal error, can't reopen %s", from);
    187  1.11  kristerw 	while ((i = read(fromfd, buf, sizeof buf)) > 0)
    188  1.11  kristerw 		if (write(tofd, buf, i) != i)
    189  1.11  kristerw 			pfatal("write to %s failed", to);
    190   1.1       cgd 	Close(fromfd);
    191   1.1       cgd 	Close(tofd);
    192   1.1       cgd }
    193   1.1       cgd 
    194  1.11  kristerw /*
    195  1.12  kristerw  * malloc with result test.
    196  1.12  kristerw  */
    197  1.12  kristerw void *
    198  1.12  kristerw xmalloc(size_t size)
    199  1.12  kristerw {
    200  1.12  kristerw 	void *p;
    201  1.12  kristerw 
    202  1.12  kristerw 	if ((p = malloc(size)) == NULL)
    203  1.12  kristerw 		fatal("out of memory\n");
    204  1.12  kristerw 	return p;
    205  1.12  kristerw }
    206  1.12  kristerw 
    207  1.12  kristerw /*
    208  1.15  kristerw  * realloc with result test.
    209  1.12  kristerw  */
    210  1.15  kristerw void *
    211  1.15  kristerw xrealloc(void *ptr, size_t size)
    212  1.12  kristerw {
    213  1.15  kristerw 	void *p;
    214  1.12  kristerw 
    215  1.15  kristerw 	if ((p = realloc(ptr, size)) == NULL)
    216  1.12  kristerw 		fatal("out of memory\n");
    217  1.12  kristerw 	return p;
    218  1.12  kristerw }
    219  1.12  kristerw 
    220  1.12  kristerw /*
    221  1.15  kristerw  * strdup with result test.
    222  1.11  kristerw  */
    223   1.1       cgd char *
    224  1.15  kristerw xstrdup(const char *s)
    225   1.1       cgd {
    226  1.15  kristerw 	char *p;
    227   1.1       cgd 
    228  1.15  kristerw 	if ((p = strdup(s)) == NULL)
    229  1.15  kristerw 		fatal("out of memory\n");
    230  1.15  kristerw 	return p;
    231   1.1       cgd }
    232   1.1       cgd 
    233  1.11  kristerw /*
    234  1.12  kristerw  * Vanilla terminal output.
    235  1.11  kristerw  */
    236   1.1       cgd void
    237   1.5  christos say(const char *pat, ...)
    238   1.1       cgd {
    239  1.11  kristerw 	va_list ap;
    240  1.11  kristerw 	va_start(ap, pat);
    241   1.5  christos 
    242  1.11  kristerw 	vfprintf(stderr, pat, ap);
    243  1.11  kristerw 	va_end(ap);
    244  1.11  kristerw 	Fflush(stderr);
    245   1.1       cgd }
    246   1.1       cgd 
    247  1.11  kristerw /*
    248  1.11  kristerw  * Terminal output, pun intended.
    249  1.11  kristerw  */
    250   1.1       cgd void				/* very void */
    251   1.5  christos fatal(const char *pat, ...)
    252   1.1       cgd {
    253  1.11  kristerw 	va_list ap;
    254  1.11  kristerw 	va_start(ap, pat);
    255   1.5  christos 
    256  1.11  kristerw 	fprintf(stderr, "patch: **** ");
    257  1.11  kristerw 	vfprintf(stderr, pat, ap);
    258  1.11  kristerw 	va_end(ap);
    259  1.11  kristerw 	my_exit(1);
    260   1.1       cgd }
    261   1.1       cgd 
    262  1.11  kristerw /*
    263  1.11  kristerw  * Say something from patch, something from the system, then silence...
    264  1.11  kristerw  */
    265   1.1       cgd void				/* very void */
    266   1.5  christos pfatal(const char *pat, ...)
    267   1.1       cgd {
    268  1.11  kristerw 	va_list ap;
    269  1.11  kristerw 	int errnum = errno;
    270  1.11  kristerw 	va_start(ap, pat);
    271   1.5  christos 
    272  1.11  kristerw 	fprintf(stderr, "patch: **** ");
    273  1.11  kristerw 	vfprintf(stderr, pat, ap);
    274  1.11  kristerw 	fprintf(stderr, ": %s\n", strerror(errnum));
    275  1.11  kristerw 	va_end(ap);
    276  1.11  kristerw 	my_exit(1);
    277   1.1       cgd }
    278   1.1       cgd 
    279  1.11  kristerw /*
    280  1.11  kristerw  * Get a response from the user, somehow or other.
    281  1.11  kristerw  */
    282   1.1       cgd void
    283   1.5  christos ask(const char *pat, ...)
    284   1.1       cgd {
    285  1.11  kristerw 	int ttyfd;
    286  1.11  kristerw 	int r;
    287  1.11  kristerw 	bool tty2 = isatty(2);
    288  1.11  kristerw 	va_list ap;
    289  1.11  kristerw 	va_start(ap, pat);
    290  1.11  kristerw 
    291  1.11  kristerw 	(void)vsprintf(buf, pat, ap);
    292  1.11  kristerw 	va_end(ap);
    293  1.11  kristerw 	Fflush(stderr);
    294  1.11  kristerw 	write(2, buf, strlen(buf));
    295  1.11  kristerw 	if (tty2) {			/* might be redirected to a file */
    296  1.11  kristerw 		r = read(2, buf, sizeof buf);
    297  1.11  kristerw 	} else if (isatty(1)) {		/* this may be new file output */
    298  1.11  kristerw 		Fflush(stdout);
    299  1.11  kristerw 		write(1, buf, strlen(buf));
    300  1.11  kristerw 		r = read(1, buf, sizeof buf);
    301  1.11  kristerw 	} else if ((ttyfd = open("/dev/tty", 2)) >= 0 && isatty(ttyfd)) {
    302  1.13       wiz 					/* might be deleted or unwritable */
    303  1.11  kristerw 		write(ttyfd, buf, strlen(buf));
    304  1.11  kristerw 		r = read(ttyfd, buf, sizeof buf);
    305  1.11  kristerw 		Close(ttyfd);
    306  1.11  kristerw 	} else if (isatty(0)) {		/* this is probably patch input */
    307  1.11  kristerw 		Fflush(stdin);
    308  1.11  kristerw 		write(0, buf, strlen(buf));
    309  1.11  kristerw 		r = read(0, buf, sizeof buf);
    310  1.11  kristerw 	} else {			/* no terminal at all--default it */
    311  1.11  kristerw 		buf[0] = '\n';
    312  1.11  kristerw 		r = 1;
    313  1.11  kristerw 	}
    314  1.11  kristerw 	if (r <= 0)
    315  1.11  kristerw 		buf[0] = 0;
    316  1.11  kristerw 	else
    317  1.11  kristerw 		buf[r] = '\0';
    318  1.11  kristerw 	if (!tty2)
    319  1.11  kristerw 		say("%s", buf);
    320   1.1       cgd }
    321   1.1       cgd 
    322  1.11  kristerw /*
    323  1.11  kristerw  * How to handle certain events when not in a critical region.
    324  1.11  kristerw  */
    325   1.1       cgd void
    326   1.9  kristerw set_signals(int reset)
    327   1.1       cgd {
    328  1.11  kristerw 	static void (*hupval)(int);
    329  1.11  kristerw 	static void (*intval)(int);
    330   1.1       cgd 
    331  1.11  kristerw 	if (!reset) {
    332  1.11  kristerw 		hupval = signal(SIGHUP, SIG_IGN);
    333  1.11  kristerw 		if (hupval != SIG_IGN)
    334  1.11  kristerw 			hupval = my_exit;
    335  1.11  kristerw 		intval = signal(SIGINT, SIG_IGN);
    336  1.11  kristerw 		if (intval != SIG_IGN)
    337  1.11  kristerw 			intval = my_exit;
    338  1.11  kristerw 	}
    339  1.11  kristerw 	Signal(SIGHUP, hupval);
    340  1.11  kristerw 	Signal(SIGINT, intval);
    341   1.1       cgd }
    342   1.1       cgd 
    343  1.11  kristerw /*
    344  1.11  kristerw  * How to handle certain events when in a critical region.
    345  1.11  kristerw  */
    346   1.1       cgd void
    347   1.1       cgd ignore_signals()
    348   1.1       cgd {
    349  1.11  kristerw 	Signal(SIGHUP, SIG_IGN);
    350  1.11  kristerw 	Signal(SIGINT, SIG_IGN);
    351   1.1       cgd }
    352   1.1       cgd 
    353  1.11  kristerw /*
    354  1.11  kristerw  * Make sure we'll have the directories to create a file.
    355  1.11  kristerw  * If `striplast' is TRUE, ignore the last element of `filename'.
    356  1.11  kristerw  */
    357   1.1       cgd void
    358   1.9  kristerw makedirs(char *filename, bool striplast)
    359   1.1       cgd {
    360  1.12  kristerw 	char tmpbuf[MAXPATHLEN];
    361  1.11  kristerw 	char *s = tmpbuf;
    362  1.12  kristerw 	char *dirv[MAXPATHLEN];	/* Point to the NULs between elements.  */
    363  1.11  kristerw 	int i;
    364  1.11  kristerw 	int dirvp = 0;		/* Number of finished entries in dirv. */
    365  1.11  kristerw 
    366  1.11  kristerw 	/*
    367  1.11  kristerw 	 * Copy `filename' into `tmpbuf' with a NUL instead of a slash
    368  1.11  kristerw 	 * between the directories.
    369  1.11  kristerw 	 */
    370  1.11  kristerw 	while (*filename) {
    371  1.11  kristerw 		if (*filename == '/') {
    372  1.11  kristerw 			filename++;
    373  1.11  kristerw 			dirv[dirvp++] = s;
    374  1.11  kristerw 			*s++ = '\0';
    375  1.11  kristerw 		} else {
    376  1.11  kristerw 			*s++ = *filename++;
    377  1.11  kristerw 		}
    378  1.11  kristerw 	}
    379  1.11  kristerw 	*s = '\0';
    380  1.11  kristerw 	dirv[dirvp] = s;
    381  1.11  kristerw 	if (striplast)
    382  1.11  kristerw 		dirvp--;
    383  1.11  kristerw 	if (dirvp < 0)
    384  1.11  kristerw 		return;
    385  1.11  kristerw 
    386  1.18    itojun 	strlcpy(buf, "mkdir", sizeof(buf));
    387  1.11  kristerw 	s = buf;
    388  1.11  kristerw 	for (i = 0; i <= dirvp; i++) {
    389  1.11  kristerw 		struct stat sbuf;
    390  1.11  kristerw 
    391  1.11  kristerw 		if (stat(tmpbuf, &sbuf) && errno == ENOENT) {
    392  1.11  kristerw 			while (*s)
    393  1.11  kristerw 				s++;
    394  1.11  kristerw 			*s++ = ' ';
    395  1.18    itojun 			strlcpy(s, tmpbuf, sizeof(buf) - (s - buf));
    396  1.11  kristerw 		}
    397  1.11  kristerw 		*dirv[i] = '/';
    398  1.11  kristerw 	}
    399  1.11  kristerw 	if (s != buf)
    400  1.11  kristerw 		system(buf);
    401   1.1       cgd }
    402   1.1       cgd 
    403  1.11  kristerw /*
    404  1.11  kristerw  * Make filenames more reasonable.
    405  1.11  kristerw  */
    406   1.1       cgd char *
    407   1.9  kristerw fetchname(char *at, int strip_leading, int assume_exists)
    408   1.1       cgd {
    409  1.11  kristerw 	char *fullname;
    410  1.11  kristerw 	char *name;
    411  1.11  kristerw 	char *t;
    412  1.12  kristerw 	char tmpbuf[MAXPATHLEN];
    413  1.11  kristerw 	int sleading = strip_leading;
    414  1.11  kristerw 
    415  1.11  kristerw 	if (!at)
    416  1.11  kristerw 		return NULL;
    417  1.11  kristerw 	while (isspace((unsigned char)*at))
    418  1.11  kristerw 		at++;
    419   1.1       cgd #ifdef DEBUGGING
    420  1.11  kristerw 	if (debug & 128)
    421  1.11  kristerw 		say("fetchname %s %d %d\n", at, strip_leading, assume_exists);
    422   1.1       cgd #endif
    423  1.11  kristerw 	filename_is_dev_null = FALSE;
    424  1.11  kristerw 	if (strnEQ(at, "/dev/null", 9)) {
    425  1.11  kristerw 		/* So files can be created by diffing against /dev/null. */
    426  1.11  kristerw 		filename_is_dev_null = TRUE;
    427  1.11  kristerw 		return NULL;
    428  1.11  kristerw 	}
    429  1.12  kristerw 	name = fullname = t = xstrdup(at);
    430  1.11  kristerw 
    431  1.11  kristerw 	/* Strip off up to `sleading' leading slashes and null terminate. */
    432  1.11  kristerw 	for (; *t && !isspace((unsigned char)*t); t++)
    433  1.11  kristerw 		if (*t == '/')
    434  1.11  kristerw 			if (--sleading >= 0)
    435  1.11  kristerw 				name = t + 1;
    436  1.11  kristerw 	*t = '\0';
    437  1.11  kristerw 
    438  1.11  kristerw 	/*
    439  1.11  kristerw 	 * If no -p option was given (957 is the default value!),
    440  1.11  kristerw 	 * we were given a relative pathname,
    441  1.11  kristerw 	 * and the leading directories that we just stripped off all exist,
    442  1.11  kristerw 	 * put them back on.
    443  1.11  kristerw 	 */
    444  1.11  kristerw 	if (strip_leading == 957 && name != fullname && *fullname != '/') {
    445  1.11  kristerw 		name[-1] = '\0';
    446  1.11  kristerw 		if (stat(fullname, &filestat) == 0 &&
    447  1.11  kristerw 		    S_ISDIR(filestat.st_mode)) {
    448  1.11  kristerw 			name[-1] = '/';
    449  1.11  kristerw 			name = fullname;
    450  1.11  kristerw 		}
    451  1.11  kristerw 	}
    452  1.11  kristerw 
    453  1.12  kristerw 	name = xstrdup(name);
    454  1.11  kristerw 	free(fullname);
    455  1.11  kristerw 
    456  1.11  kristerw 	if (stat(name, &filestat) && !assume_exists) {
    457  1.11  kristerw 		char *filebase = basename(name);
    458  1.14  kristerw 		size_t pathlen = filebase - name;
    459  1.11  kristerw 
    460  1.11  kristerw 		/* Put any leading path into `tmpbuf'. */
    461  1.17    itojun 		if (pathlen >= sizeof(tmpbuf))
    462  1.17    itojun 			return NULL;
    463  1.11  kristerw 		strncpy(tmpbuf, name, pathlen);
    464  1.17    itojun 		tmpbuf[pathlen] = '\0';
    465  1.11  kristerw 
    466  1.11  kristerw #define try(f, a1, a2) \
    467  1.18    itojun     (snprintf(tmpbuf + pathlen, sizeof(tmpbuf) - pathlen, f, a1, a2), \
    468  1.18    itojun      stat(tmpbuf, &filestat) == 0)
    469  1.11  kristerw #define try1(f, a1) \
    470  1.18    itojun     (snprintf(tmpbuf + pathlen, sizeof(tmpbuf) - pathlen, f, a1), \
    471  1.18    itojun      stat(tmpbuf, &filestat) == 0)
    472  1.11  kristerw 		if (try("RCS/%s%s", filebase, RCSSUFFIX) ||
    473  1.11  kristerw 		    try1("RCS/%s"  , filebase) ||
    474  1.11  kristerw 		    try(    "%s%s", filebase, RCSSUFFIX) ||
    475  1.11  kristerw 		    try("SCCS/%s%s", SCCSPREFIX, filebase) ||
    476  1.11  kristerw 		    try(     "%s%s", SCCSPREFIX, filebase))
    477  1.11  kristerw 			return name;
    478  1.11  kristerw 		free(name);
    479  1.11  kristerw 		name = NULL;
    480  1.11  kristerw 	}
    481   1.1       cgd 
    482  1.11  kristerw 	return name;
    483   1.1       cgd }
    484