xref: /original-bsd/usr.bin/find/misc.c (revision e58c8952)
1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *	The Regents of the University of California.  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	8.2 (Berkeley) 04/01/94";
13 #endif /* not lint */
14 
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 
18 #include <err.h>
19 #include <errno.h>
20 #include <fts.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include "find.h"
26 
27 /*
28  * brace_subst --
29  *	Replace occurrences of {} in s1 with s2 and return the result string.
30  */
31 void
32 brace_subst(orig, store, path, len)
33 	char *orig, **store, *path;
34 	int len;
35 {
36 	register int plen;
37 	register char ch, *p;
38 
39 	plen = strlen(path);
40 	for (p = *store; (ch = *orig) != '\0'; ++orig)
41 		if (ch == '{' && orig[1] == '}') {
42 			while ((p - *store) + plen > len)
43 				if (!(*store = realloc(*store, len *= 2)))
44 					err(1, NULL);
45 			memmove(p, path, plen);
46 			p += plen;
47 			++orig;
48 		} else
49 			*p++ = ch;
50 	*p = '\0';
51 }
52 
53 /*
54  * queryuser --
55  *	print a message to standard error and then read input from standard
56  *	input. If the input is 'y' then 1 is returned.
57  */
58 int
59 queryuser(argv)
60 	register char **argv;
61 {
62 	int ch, first, nl;
63 
64 	(void)fprintf(stderr, "\"%s", *argv);
65 	while (*++argv)
66 		(void)fprintf(stderr, " %s", *argv);
67 	(void)fprintf(stderr, "\"? ");
68 	(void)fflush(stderr);
69 
70 	first = ch = getchar();
71 	for (nl = 0;;) {
72 		if (ch == '\n') {
73 			nl = 1;
74 			break;
75 		}
76 		if (ch == EOF)
77 			break;
78 		ch = getchar();
79 	}
80 
81 	if (!nl) {
82 		(void)fprintf(stderr, "\n");
83 		(void)fflush(stderr);
84 	}
85         return (first == 'y');
86 }
87 
88 /*
89  * emalloc --
90  *	malloc with error checking.
91  */
92 void *
93 emalloc(len)
94 	u_int len;
95 {
96 	void *p;
97 
98 	if ((p = malloc(len)) == NULL)
99 		err(1, NULL);
100 	return (p);
101 }
102