xref: /original-bsd/usr.bin/find/misc.c (revision 2ad443ff)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Cimarron D. Taylor of the University of California, Berkeley.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)misc.c	5.2 (Berkeley) 05/12/90";
13 #endif /* not lint */
14 
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <stdio.h>
18 #include "find.h"
19 
20 /*
21  * find_subst --
22  *	Replace occurrences of {} in s1 with s2 and return the result string.
23  */
24 find_subst(orig, store, path, len)
25 	char *orig, **store, *path;
26 	int len;
27 {
28 	register int plen;
29 	register char ch, *p;
30 	char *realloc(), *strerror();
31 
32 	plen = strlen(path);
33 	for (p = *store; ch = *orig; ++orig)
34 		if (ch == '{' && orig[1] == '}') {
35 			while ((p - *store) + plen > len)
36 				if (!(*store = realloc(*store, len *= 2))) {
37 					(void)fprintf(stderr,
38 					    "find: %s.\n", strerror(errno));
39 					exit(1);
40 				}
41 			bcopy(path, p, plen);
42 			p += plen;
43 			++orig;
44 		} else
45 			*p++ = ch;
46 	*p = '\0';
47 }
48 
49 /*
50  * find_queryuser --
51  *	print a message to standard error and then read input from standard
52  *	input. If the input is 'y' then 1 is returned.
53  */
54 find_queryuser(argv)
55 	register char **argv;
56 {
57 	char buf[10];
58 
59 	(void)fprintf(stderr, "\"%s", *argv);
60 	while (*++argv)
61 		(void)fprintf(stderr, " %s", *argv);
62 	(void)fprintf(stderr, "\"? ");
63 	(void)fflush(stderr);
64 	return(!fgets(buf, sizeof(buf), stdin) || buf[0] != 'y' ? 0 : 1);
65 }
66 
67 /*
68  * bad_arg --
69  *	print out a bad argument message.
70  */
71 void
72 bad_arg(option, error)
73 	char *option, *error;
74 {
75 	(void)fprintf(stderr, "find: %s: %s.\n", option, error);
76 	exit(1);
77 }
78 
79 /*
80  * emalloc --
81  *	malloc with error checking.
82  */
83 char *
84 emalloc(len)
85 	u_int len;
86 {
87 	char *p, *malloc(), *strerror();
88 
89 	if (!(p = malloc(len))) {
90 		(void)fprintf(stderr, "find: %s.\n", strerror(errno));
91 		exit(1);
92 	}
93 	return(p);
94 }
95 
96 usage()
97 {
98 	extern int deprecated;
99 
100 	if (deprecated)
101 		(void)fprintf(stderr, "usage: find path-list expression\n");
102 	else
103 		(void)fprintf(stderr,
104 		    "usage: find [-dsx] -f path ... expression\n");
105 	exit(1);
106 }
107