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