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