xref: /original-bsd/usr.bin/find/misc.c (revision 62cd422e)
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.5 (Berkeley) 11/15/90";
13 #endif /* not lint */
14 
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <sys/errno.h>
18 #include <stdio.h>
19 #include "find.h"
20 #include <stdlib.h>
21 #include <string.h>
22 
23 /*
24  * brace_subst --
25  *	Replace occurrences of {} in s1 with s2 and return the result string.
26  */
27 void
28 brace_subst(orig, store, path, len)
29 	char *orig, **store, *path;
30 	int len;
31 {
32 	register int plen;
33 	register char ch, *p;
34 
35 	plen = strlen(path);
36 	for (p = *store; ch = *orig; ++orig)
37 		if (ch == '{' && orig[1] == '}') {
38 			while ((p - *store) + plen > len)
39 				if (!(*store = realloc(*store, len *= 2))) {
40 					(void)fprintf(stderr,
41 					    "find: %s.\n", strerror(errno));
42 					exit(1);
43 				}
44 			bcopy(path, p, plen);
45 			p += plen;
46 			++orig;
47 		} else
48 			*p++ = ch;
49 	*p = '\0';
50 }
51 
52 /*
53  * queryuser --
54  *	print a message to standard error and then read input from standard
55  *	input. If the input is 'y' then 1 is returned.
56  */
57 queryuser(argv)
58 	register char **argv;
59 {
60 	int ch, first, nl;
61 
62 	(void)fprintf(stderr, "\"%s", *argv);
63 	while (*++argv)
64 		(void)fprintf(stderr, " %s", *argv);
65 	(void)fprintf(stderr, "\"? ");
66 	(void)fflush(stderr);
67 
68 	first = ch = getchar();
69 	for (nl = 0;;) {
70 		if (ch == '\n') {
71 			nl = 1;
72 			break;
73 		}
74 		if (ch == EOF)
75 			break;
76 		ch = getchar();
77 	}
78 
79 	if (!nl) {
80 		(void)fprintf(stderr, "\n");
81 		(void)fflush(stderr);
82 	}
83         return(first == 'y');
84 }
85 
86 /*
87  * bad_arg --
88  *	print out a bad argument message.
89  */
90 void
91 bad_arg(option, error)
92 	char *option, *error;
93 {
94 	(void)fprintf(stderr, "find: %s: %s.\n", option, error);
95 	exit(1);
96 }
97 
98 /*
99  * emalloc --
100  *	malloc with error checking.
101  */
102 void *
103 emalloc(len)
104 	u_int len;
105 {
106 	void *p;
107 
108 	if (!(p = malloc(len))) {
109 		(void)fprintf(stderr, "find: %s.\n", strerror(errno));
110 		exit(1);
111 	}
112 	return(p);
113 }
114 
115 usage()
116 {
117 	if (isdeprecated)
118 		(void)fprintf(stderr, "usage: find path-list expression\n");
119 	else
120 		(void)fprintf(stderr,
121 		    "usage: find [-drsx] -f path ... expression\n");
122 	exit(1);
123 }
124