xref: /netbsd-src/usr.bin/find/misc.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*-
2  * Copyright (c) 1990, 1993
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  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 /*static char sccsid[] = "from: @(#)misc.c	8.1 (Berkeley) 6/6/93";*/
39 static char rcsid[] = "$Id: misc.c,v 1.4 1996/06/23 11:16:39 mrg Exp $";
40 #endif /* not lint */
41 
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 
45 #include <err.h>
46 #include <errno.h>
47 #include <fts.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 
52 #include "find.h"
53 
54 /*
55  * brace_subst --
56  *	Replace occurrences of {} in orig with path, and place it in a malloced
57  *      area of memory set in store.
58  */
59 void
60 brace_subst(orig, store, path, len)
61 	char *orig, **store, *path;
62 	int len;
63 {
64 	register int plen;
65 	register char ch, *p;
66 
67 	plen = strlen(path);
68 	for (p = *store; ch = *orig; ++orig)
69 		if (ch == '{' && orig[1] == '}') {
70 			while ((p - *store) + plen > len)
71 				if (!(*store = realloc(*store, len *= 2)))
72 					err(1, NULL);
73 			memmove(p, path, plen);
74 			p += plen;
75 			++orig;
76 		} else
77 			*p++ = ch;
78 	*p = '\0';
79 }
80 
81 /*
82  * queryuser --
83  *	print a message to standard error and then read input from standard
84  *	input. If the input is 'y' then 1 is returned.
85  */
86 int
87 queryuser(argv)
88 	register char **argv;
89 {
90 	int ch, first, nl;
91 
92 	(void)fprintf(stderr, "\"%s", *argv);
93 	while (*++argv)
94 		(void)fprintf(stderr, " %s", *argv);
95 	(void)fprintf(stderr, "\"? ");
96 	(void)fflush(stderr);
97 
98 	first = ch = getchar();
99 	for (nl = 0;;) {
100 		if (ch == '\n') {
101 			nl = 1;
102 			break;
103 		}
104 		if (ch == EOF)
105 			break;
106 		ch = getchar();
107 	}
108 
109 	if (!nl) {
110 		(void)fprintf(stderr, "\n");
111 		(void)fflush(stderr);
112 	}
113         return (first == 'y');
114 }
115 
116 /*
117  * emalloc --
118  *	malloc with error checking.
119  */
120 void *
121 emalloc(len)
122 	u_int len;
123 {
124 	void *p;
125 
126 	if (p = malloc(len))
127 		return (p);
128 	err(1, NULL);
129 }
130