xref: /csrg-svn/usr.bin/find/misc.c (revision 49868)
141367Sbostic /*-
241367Sbostic  * Copyright (c) 1990 The Regents of the University of California.
341367Sbostic  * 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*49868Sbostic static char sccsid[] = "@(#)misc.c	5.8 (Berkeley) 05/24/91";
1341367Sbostic #endif /* not lint */
1441367Sbostic 
1541367Sbostic #include <sys/types.h>
1641367Sbostic #include <sys/stat.h>
1745610Sbostic #include <sys/errno.h>
1841367Sbostic #include <stdio.h>
1945616Sbostic #include <stdlib.h>
2045616Sbostic #include <string.h>
2147189Sbostic #include "find.h"
2241367Sbostic 
2341367Sbostic /*
2445616Sbostic  * brace_subst --
2541367Sbostic  *	Replace occurrences of {} in s1 with s2 and return the result string.
2641367Sbostic  */
2745616Sbostic void
2845616Sbostic brace_subst(orig, store, path, len)
2941367Sbostic 	char *orig, **store, *path;
3041367Sbostic 	int len;
3141367Sbostic {
3241367Sbostic 	register int plen;
3341367Sbostic 	register char ch, *p;
3441367Sbostic 
3541367Sbostic 	plen = strlen(path);
3641367Sbostic 	for (p = *store; ch = *orig; ++orig)
3741367Sbostic 		if (ch == '{' && orig[1] == '}') {
3841367Sbostic 			while ((p - *store) + plen > len)
39*49868Sbostic 				if (!(*store = realloc(*store, len *= 2)))
40*49868Sbostic 					err("%s", strerror(errno));
4141367Sbostic 			bcopy(path, p, plen);
4241367Sbostic 			p += plen;
4341367Sbostic 			++orig;
4441367Sbostic 		} else
4541367Sbostic 			*p++ = ch;
4641367Sbostic 	*p = '\0';
4741367Sbostic }
4841367Sbostic 
4941367Sbostic /*
5045616Sbostic  * queryuser --
5141367Sbostic  *	print a message to standard error and then read input from standard
5241367Sbostic  *	input. If the input is 'y' then 1 is returned.
5341367Sbostic  */
5445616Sbostic queryuser(argv)
5541367Sbostic 	register char **argv;
5641367Sbostic {
5745616Sbostic 	int ch, first, nl;
5841367Sbostic 
5941367Sbostic 	(void)fprintf(stderr, "\"%s", *argv);
6041367Sbostic 	while (*++argv)
6141367Sbostic 		(void)fprintf(stderr, " %s", *argv);
6241367Sbostic 	(void)fprintf(stderr, "\"? ");
6341367Sbostic 	(void)fflush(stderr);
6445616Sbostic 
6545616Sbostic 	first = ch = getchar();
6645616Sbostic 	for (nl = 0;;) {
6745616Sbostic 		if (ch == '\n') {
6845616Sbostic 			nl = 1;
6945616Sbostic 			break;
7045616Sbostic 		}
7145616Sbostic 		if (ch == EOF)
7245616Sbostic 			break;
7345616Sbostic 		ch = getchar();
7445616Sbostic 	}
7545616Sbostic 
7645616Sbostic 	if (!nl) {
7745616Sbostic 		(void)fprintf(stderr, "\n");
7845616Sbostic 		(void)fflush(stderr);
7945616Sbostic 	}
8045616Sbostic         return(first == 'y');
8141367Sbostic }
8241367Sbostic 
8341367Sbostic /*
8441367Sbostic  * emalloc --
8541367Sbostic  *	malloc with error checking.
8641367Sbostic  */
8745616Sbostic void *
8841367Sbostic emalloc(len)
8941367Sbostic 	u_int len;
9041367Sbostic {
9145616Sbostic 	void *p;
9241367Sbostic 
93*49868Sbostic 	if (p = malloc(len))
94*49868Sbostic 		return(p);
95*49868Sbostic 	err("%s", strerror(errno));
96*49868Sbostic 	/* NOTREACHED */
9741367Sbostic }
9841367Sbostic 
99*49868Sbostic #if __STDC__
100*49868Sbostic #include <stdarg.h>
101*49868Sbostic #else
102*49868Sbostic #include <varargs.h>
103*49868Sbostic #endif
104*49868Sbostic 
105*49868Sbostic void
106*49868Sbostic #if __STDC__
107*49868Sbostic err(const char *fmt, ...)
108*49868Sbostic #else
109*49868Sbostic err(fmt, va_alist)
110*49868Sbostic 	char *fmt;
111*49868Sbostic         va_dcl
112*49868Sbostic #endif
11341367Sbostic {
114*49868Sbostic 	va_list ap;
115*49868Sbostic #if __STDC__
116*49868Sbostic 	va_start(ap, fmt);
117*49868Sbostic #else
118*49868Sbostic 	va_start(ap);
119*49868Sbostic #endif
120*49868Sbostic 	(void)fprintf(stderr, "find: ");
121*49868Sbostic 	(void)vfprintf(stderr, fmt, ap);
122*49868Sbostic 	va_end(ap);
123*49868Sbostic 	(void)fprintf(stderr, "\n");
12441367Sbostic 	exit(1);
125*49868Sbostic 	/* NOTREACHED */
12641367Sbostic }
127