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