xref: /original-bsd/usr.bin/find/misc.c (revision a6d4d8bb)
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.8 (Berkeley) 05/24/91";
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 <stdlib.h>
20 #include <string.h>
21 #include "find.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 					err("%s", strerror(errno));
41 			bcopy(path, p, plen);
42 			p += plen;
43 			++orig;
44 		} else
45 			*p++ = ch;
46 	*p = '\0';
47 }
48 
49 /*
50  * 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 queryuser(argv)
55 	register char **argv;
56 {
57 	int ch, first, nl;
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 
65 	first = ch = getchar();
66 	for (nl = 0;;) {
67 		if (ch == '\n') {
68 			nl = 1;
69 			break;
70 		}
71 		if (ch == EOF)
72 			break;
73 		ch = getchar();
74 	}
75 
76 	if (!nl) {
77 		(void)fprintf(stderr, "\n");
78 		(void)fflush(stderr);
79 	}
80         return(first == 'y');
81 }
82 
83 /*
84  * emalloc --
85  *	malloc with error checking.
86  */
87 void *
88 emalloc(len)
89 	u_int len;
90 {
91 	void *p;
92 
93 	if (p = malloc(len))
94 		return(p);
95 	err("%s", strerror(errno));
96 	/* NOTREACHED */
97 }
98 
99 #if __STDC__
100 #include <stdarg.h>
101 #else
102 #include <varargs.h>
103 #endif
104 
105 void
106 #if __STDC__
107 err(const char *fmt, ...)
108 #else
109 err(fmt, va_alist)
110 	char *fmt;
111         va_dcl
112 #endif
113 {
114 	va_list ap;
115 #if __STDC__
116 	va_start(ap, fmt);
117 #else
118 	va_start(ap);
119 #endif
120 	(void)fprintf(stderr, "find: ");
121 	(void)vfprintf(stderr, fmt, ap);
122 	va_end(ap);
123 	(void)fprintf(stderr, "\n");
124 	exit(1);
125 	/* NOTREACHED */
126 }
127