xref: /csrg-svn/usr.bin/find/misc.c (revision 66586)
141367Sbostic /*-
2*66586Sbostic  * Copyright (c) 1990, 1993, 1994
361995Sbostic  *	The Regents of the University of California.  All rights reserved.
441367Sbostic  *
541367Sbostic  * This code is derived from software contributed to Berkeley by
641367Sbostic  * Cimarron D. Taylor of the University of California, Berkeley.
741367Sbostic  *
841367Sbostic  * %sccs.include.redist.c%
941367Sbostic  */
1041367Sbostic 
1141367Sbostic #ifndef lint
12*66586Sbostic static char sccsid[] = "@(#)misc.c	8.2 (Berkeley) 04/01/94";
1341367Sbostic #endif /* not lint */
1441367Sbostic 
1541367Sbostic #include <sys/types.h>
1641367Sbostic #include <sys/stat.h>
1759612Sbostic 
1859612Sbostic #include <err.h>
1959612Sbostic #include <errno.h>
2059612Sbostic #include <fts.h>
2141367Sbostic #include <stdio.h>
2245616Sbostic #include <stdlib.h>
2345616Sbostic #include <string.h>
2459612Sbostic 
2547189Sbostic #include "find.h"
2641367Sbostic 
2741367Sbostic /*
2845616Sbostic  * brace_subst --
2941367Sbostic  *	Replace occurrences of {} in s1 with s2 and return the result string.
3041367Sbostic  */
3145616Sbostic void
brace_subst(orig,store,path,len)3245616Sbostic brace_subst(orig, store, path, len)
3341367Sbostic 	char *orig, **store, *path;
3441367Sbostic 	int len;
3541367Sbostic {
3641367Sbostic 	register int plen;
3741367Sbostic 	register char ch, *p;
3841367Sbostic 
3941367Sbostic 	plen = strlen(path);
40*66586Sbostic 	for (p = *store; (ch = *orig) != '\0'; ++orig)
4141367Sbostic 		if (ch == '{' && orig[1] == '}') {
4241367Sbostic 			while ((p - *store) + plen > len)
4349868Sbostic 				if (!(*store = realloc(*store, len *= 2)))
4459612Sbostic 					err(1, NULL);
4559612Sbostic 			memmove(p, path, plen);
4641367Sbostic 			p += plen;
4741367Sbostic 			++orig;
4841367Sbostic 		} else
4941367Sbostic 			*p++ = ch;
5041367Sbostic 	*p = '\0';
5141367Sbostic }
5241367Sbostic 
5341367Sbostic /*
5445616Sbostic  * queryuser --
5541367Sbostic  *	print a message to standard error and then read input from standard
5641367Sbostic  *	input. If the input is 'y' then 1 is returned.
5741367Sbostic  */
5859612Sbostic int
queryuser(argv)5945616Sbostic queryuser(argv)
6041367Sbostic 	register char **argv;
6141367Sbostic {
6245616Sbostic 	int ch, first, nl;
6341367Sbostic 
6441367Sbostic 	(void)fprintf(stderr, "\"%s", *argv);
6541367Sbostic 	while (*++argv)
6641367Sbostic 		(void)fprintf(stderr, " %s", *argv);
6741367Sbostic 	(void)fprintf(stderr, "\"? ");
6841367Sbostic 	(void)fflush(stderr);
6945616Sbostic 
7045616Sbostic 	first = ch = getchar();
7145616Sbostic 	for (nl = 0;;) {
7245616Sbostic 		if (ch == '\n') {
7345616Sbostic 			nl = 1;
7445616Sbostic 			break;
7545616Sbostic 		}
7645616Sbostic 		if (ch == EOF)
7745616Sbostic 			break;
7845616Sbostic 		ch = getchar();
7945616Sbostic 	}
8045616Sbostic 
8145616Sbostic 	if (!nl) {
8245616Sbostic 		(void)fprintf(stderr, "\n");
8345616Sbostic 		(void)fflush(stderr);
8445616Sbostic 	}
8559612Sbostic         return (first == 'y');
8641367Sbostic }
8741367Sbostic 
8841367Sbostic /*
8941367Sbostic  * emalloc --
9041367Sbostic  *	malloc with error checking.
9141367Sbostic  */
9245616Sbostic void *
emalloc(len)9341367Sbostic emalloc(len)
9441367Sbostic 	u_int len;
9541367Sbostic {
9645616Sbostic 	void *p;
9741367Sbostic 
98*66586Sbostic 	if ((p = malloc(len)) == NULL)
99*66586Sbostic 		err(1, NULL);
100*66586Sbostic 	return (p);
10141367Sbostic }
102