xref: /openbsd-src/usr.bin/find/misc.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: misc.c,v 1.4 1997/06/30 23:54:08 millert Exp $	*/
2 
3 /*-
4  * Copyright (c) 1990, 1993
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 #ifndef lint
40 /*static char sccsid[] = "from: @(#)misc.c	8.1 (Berkeley) 6/6/93";*/
41 static char rcsid[] = "$OpenBSD: misc.c,v 1.4 1997/06/30 23:54:08 millert Exp $";
42 #endif /* not lint */
43 
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 
47 #include <err.h>
48 #include <errno.h>
49 #include <fts.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 
55 #include "find.h"
56 
57 /*
58  * brace_subst --
59  *	Replace occurrences of {} in s1 with s2 and return the result string.
60  */
61 void
62 brace_subst(orig, store, path, len)
63 	char *orig, **store, *path;
64 	int len;
65 {
66 	register int plen;
67 	register char ch, *p;
68 
69 	plen = strlen(path);
70 	for (p = *store; (ch = *orig); ++orig)
71 		if (ch == '{' && orig[1] == '}') {
72 			while ((p - *store) + plen > len)
73 				if (!(*store = realloc(*store, len *= 2)))
74 					err(1, NULL);
75 			memmove(p, path, plen);
76 			p += plen;
77 			++orig;
78 		} else
79 			*p++ = ch;
80 	*p = '\0';
81 }
82 
83 /*
84  * queryuser --
85  *	print a message to standard error and then read input from standard
86  *	input. If the input is 'y' then 1 is returned.
87  */
88 int
89 queryuser(argv)
90 	register char **argv;
91 {
92 	int ch, first, nl;
93 
94 	(void)fprintf(stderr, "\"%s", *argv);
95 	while (*++argv)
96 		(void)fprintf(stderr, " %s", *argv);
97 	(void)fprintf(stderr, "\"? ");
98 	(void)fflush(stderr);
99 
100 	first = ch = getchar();
101 	for (nl = 0;;) {
102 		if (ch == '\n') {
103 			nl = 1;
104 			break;
105 		}
106 		if (ch == EOF)
107 			break;
108 		ch = getchar();
109 	}
110 
111 	if (!nl) {
112 		(void)fprintf(stderr, "\n");
113 		(void)fflush(stderr);
114 	}
115         return (first == 'y');
116 }
117 
118 /*
119  * emalloc --
120  *	malloc with error checking.
121  */
122 void *
123 emalloc(len)
124 	u_int len;
125 {
126 	void *p;
127 
128 	if ((p = malloc(len)))
129 		return (p);
130 	err(1, NULL);
131 }
132 
133 /*
134  * show_path --
135  *	called on SIGINFO
136  */
137 /* ARGSUSED */
138 void
139 show_path(sig)
140 	int sig;
141 {
142 	extern FTSENT *entry;
143 
144 	write(STDERR_FILENO, "find path: ", 11);
145 	write(STDERR_FILENO, entry->fts_path, entry->fts_pathlen);
146 	write(STDERR_FILENO, "\n", 1);
147 }
148