xref: /csrg-svn/usr.bin/find/misc.c (revision 47189)
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*47189Sbostic static char sccsid[] = "@(#)misc.c	5.6 (Berkeley) 03/09/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>
21*47189Sbostic #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)
3941367Sbostic 				if (!(*store = realloc(*store, len *= 2))) {
4041367Sbostic 					(void)fprintf(stderr,
4141367Sbostic 					    "find: %s.\n", strerror(errno));
4241367Sbostic 					exit(1);
4341367Sbostic 				}
4441367Sbostic 			bcopy(path, p, plen);
4541367Sbostic 			p += plen;
4641367Sbostic 			++orig;
4741367Sbostic 		} else
4841367Sbostic 			*p++ = ch;
4941367Sbostic 	*p = '\0';
5041367Sbostic }
5141367Sbostic 
5241367Sbostic /*
5345616Sbostic  * queryuser --
5441367Sbostic  *	print a message to standard error and then read input from standard
5541367Sbostic  *	input. If the input is 'y' then 1 is returned.
5641367Sbostic  */
5745616Sbostic queryuser(argv)
5841367Sbostic 	register char **argv;
5941367Sbostic {
6045616Sbostic 	int ch, first, nl;
6141367Sbostic 
6241367Sbostic 	(void)fprintf(stderr, "\"%s", *argv);
6341367Sbostic 	while (*++argv)
6441367Sbostic 		(void)fprintf(stderr, " %s", *argv);
6541367Sbostic 	(void)fprintf(stderr, "\"? ");
6641367Sbostic 	(void)fflush(stderr);
6745616Sbostic 
6845616Sbostic 	first = ch = getchar();
6945616Sbostic 	for (nl = 0;;) {
7045616Sbostic 		if (ch == '\n') {
7145616Sbostic 			nl = 1;
7245616Sbostic 			break;
7345616Sbostic 		}
7445616Sbostic 		if (ch == EOF)
7545616Sbostic 			break;
7645616Sbostic 		ch = getchar();
7745616Sbostic 	}
7845616Sbostic 
7945616Sbostic 	if (!nl) {
8045616Sbostic 		(void)fprintf(stderr, "\n");
8145616Sbostic 		(void)fflush(stderr);
8245616Sbostic 	}
8345616Sbostic         return(first == 'y');
8441367Sbostic }
8541367Sbostic 
8641367Sbostic /*
8741367Sbostic  * bad_arg --
8841367Sbostic  *	print out a bad argument message.
8941367Sbostic  */
9041367Sbostic void
9141367Sbostic bad_arg(option, error)
9241367Sbostic 	char *option, *error;
9341367Sbostic {
9441367Sbostic 	(void)fprintf(stderr, "find: %s: %s.\n", option, error);
9541367Sbostic 	exit(1);
9641367Sbostic }
9741367Sbostic 
9841367Sbostic /*
9941367Sbostic  * emalloc --
10041367Sbostic  *	malloc with error checking.
10141367Sbostic  */
10245616Sbostic void *
10341367Sbostic emalloc(len)
10441367Sbostic 	u_int len;
10541367Sbostic {
10645616Sbostic 	void *p;
10741367Sbostic 
10841367Sbostic 	if (!(p = malloc(len))) {
10941367Sbostic 		(void)fprintf(stderr, "find: %s.\n", strerror(errno));
11041367Sbostic 		exit(1);
11141367Sbostic 	}
11241367Sbostic 	return(p);
11341367Sbostic }
11441367Sbostic 
11541367Sbostic usage()
11641367Sbostic {
11745610Sbostic 	if (isdeprecated)
11841778Sbostic 		(void)fprintf(stderr, "usage: find path-list expression\n");
11941778Sbostic 	else
12041778Sbostic 		(void)fprintf(stderr,
12145404Sbostic 		    "usage: find [-drsx] -f path ... expression\n");
12241367Sbostic 	exit(1);
12341367Sbostic }
124