xref: /csrg-svn/usr.bin/find/misc.c (revision 45404)
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*45404Sbostic static char sccsid[] = "@(#)misc.c	5.3 (Berkeley) 10/22/90";
1341367Sbostic #endif /* not lint */
1441367Sbostic 
1541367Sbostic #include <sys/types.h>
1641367Sbostic #include <sys/stat.h>
1741367Sbostic #include <stdio.h>
1841367Sbostic #include "find.h"
1941367Sbostic 
2041367Sbostic /*
2141367Sbostic  * find_subst --
2241367Sbostic  *	Replace occurrences of {} in s1 with s2 and return the result string.
2341367Sbostic  */
2441367Sbostic find_subst(orig, store, path, len)
2541367Sbostic 	char *orig, **store, *path;
2641367Sbostic 	int len;
2741367Sbostic {
2841367Sbostic 	register int plen;
2941367Sbostic 	register char ch, *p;
3041778Sbostic 	char *realloc(), *strerror();
3141367Sbostic 
3241367Sbostic 	plen = strlen(path);
3341367Sbostic 	for (p = *store; ch = *orig; ++orig)
3441367Sbostic 		if (ch == '{' && orig[1] == '}') {
3541367Sbostic 			while ((p - *store) + plen > len)
3641367Sbostic 				if (!(*store = realloc(*store, len *= 2))) {
3741367Sbostic 					(void)fprintf(stderr,
3841367Sbostic 					    "find: %s.\n", strerror(errno));
3941367Sbostic 					exit(1);
4041367Sbostic 				}
4141367Sbostic 			bcopy(path, p, plen);
4241367Sbostic 			p += plen;
4341367Sbostic 			++orig;
4441367Sbostic 		} else
4541367Sbostic 			*p++ = ch;
4641367Sbostic 	*p = '\0';
4741367Sbostic }
4841367Sbostic 
4941367Sbostic /*
5041367Sbostic  * find_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  */
5441367Sbostic find_queryuser(argv)
5541367Sbostic 	register char **argv;
5641367Sbostic {
5741367Sbostic 	char buf[10];
5841367Sbostic 
5941367Sbostic 	(void)fprintf(stderr, "\"%s", *argv);
6041367Sbostic 	while (*++argv)
6141367Sbostic 		(void)fprintf(stderr, " %s", *argv);
6241367Sbostic 	(void)fprintf(stderr, "\"? ");
6341367Sbostic 	(void)fflush(stderr);
6441367Sbostic 	return(!fgets(buf, sizeof(buf), stdin) || buf[0] != 'y' ? 0 : 1);
6541367Sbostic }
6641367Sbostic 
6741367Sbostic /*
6841367Sbostic  * bad_arg --
6941367Sbostic  *	print out a bad argument message.
7041367Sbostic  */
7141367Sbostic void
7241367Sbostic bad_arg(option, error)
7341367Sbostic 	char *option, *error;
7441367Sbostic {
7541367Sbostic 	(void)fprintf(stderr, "find: %s: %s.\n", option, error);
7641367Sbostic 	exit(1);
7741367Sbostic }
7841367Sbostic 
7941367Sbostic /*
8041367Sbostic  * emalloc --
8141367Sbostic  *	malloc with error checking.
8241367Sbostic  */
8341367Sbostic char *
8441367Sbostic emalloc(len)
8541367Sbostic 	u_int len;
8641367Sbostic {
8741778Sbostic 	char *p, *malloc(), *strerror();
8841367Sbostic 
8941367Sbostic 	if (!(p = malloc(len))) {
9041367Sbostic 		(void)fprintf(stderr, "find: %s.\n", strerror(errno));
9141367Sbostic 		exit(1);
9241367Sbostic 	}
9341367Sbostic 	return(p);
9441367Sbostic }
9541367Sbostic 
9641367Sbostic usage()
9741367Sbostic {
9841778Sbostic 	extern int deprecated;
9941778Sbostic 
10041778Sbostic 	if (deprecated)
10141778Sbostic 		(void)fprintf(stderr, "usage: find path-list expression\n");
10241778Sbostic 	else
10341778Sbostic 		(void)fprintf(stderr,
104*45404Sbostic 		    "usage: find [-drsx] -f path ... expression\n");
10541367Sbostic 	exit(1);
10641367Sbostic }
107