xref: /csrg-svn/usr.bin/find/misc.c (revision 45616)
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*45616Sbostic static char sccsid[] = "@(#)misc.c	5.5 (Berkeley) 11/15/90";
1341367Sbostic #endif /* not lint */
1441367Sbostic 
1541367Sbostic #include <sys/types.h>
1641367Sbostic #include <sys/stat.h>
1745610Sbostic #include <sys/errno.h>
1841367Sbostic #include <stdio.h>
1941367Sbostic #include "find.h"
20*45616Sbostic #include <stdlib.h>
21*45616Sbostic #include <string.h>
2241367Sbostic 
2341367Sbostic /*
24*45616Sbostic  * brace_subst --
2541367Sbostic  *	Replace occurrences of {} in s1 with s2 and return the result string.
2641367Sbostic  */
27*45616Sbostic void
28*45616Sbostic 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 /*
53*45616Sbostic  * 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  */
57*45616Sbostic queryuser(argv)
5841367Sbostic 	register char **argv;
5941367Sbostic {
60*45616Sbostic 	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);
67*45616Sbostic 
68*45616Sbostic 	first = ch = getchar();
69*45616Sbostic 	for (nl = 0;;) {
70*45616Sbostic 		if (ch == '\n') {
71*45616Sbostic 			nl = 1;
72*45616Sbostic 			break;
73*45616Sbostic 		}
74*45616Sbostic 		if (ch == EOF)
75*45616Sbostic 			break;
76*45616Sbostic 		ch = getchar();
77*45616Sbostic 	}
78*45616Sbostic 
79*45616Sbostic 	if (!nl) {
80*45616Sbostic 		(void)fprintf(stderr, "\n");
81*45616Sbostic 		(void)fflush(stderr);
82*45616Sbostic 	}
83*45616Sbostic         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  */
102*45616Sbostic void *
10341367Sbostic emalloc(len)
10441367Sbostic 	u_int len;
10541367Sbostic {
106*45616Sbostic 	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