xref: /openbsd-src/usr.bin/find/misc.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: misc.c,v 1.10 2003/09/26 22:22:26 tedu 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. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #ifndef lint
36 /*static char sccsid[] = "from: @(#)misc.c	8.1 (Berkeley) 6/6/93";*/
37 static char rcsid[] = "$OpenBSD: misc.c,v 1.10 2003/09/26 22:22:26 tedu Exp $";
38 #endif /* not lint */
39 
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <sys/uio.h>
43 
44 #include <err.h>
45 #include <errno.h>
46 #include <fts.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 
52 #include "find.h"
53 
54 /*
55  * brace_subst --
56  *	Replace occurrences of {} in s1 with s2 and return the result string.
57  */
58 void
59 brace_subst(char *orig, char **store, char *path, int len)
60 {
61 	int plen;
62 	char ch, *p;
63 
64 	plen = strlen(path);
65 	for (p = *store; (ch = *orig); ++orig)
66 		if (ch == '{' && orig[1] == '}') {
67 			while ((p - *store) + plen > len) {
68 				int newlen = len * 2;
69 				char *newstore;
70 
71 				if (!(newstore = realloc(*store, newlen)))
72 					err(1, NULL);
73 				*store = newstore;
74 				len = newlen;
75 			}
76 			memmove(p, path, plen);
77 			p += plen;
78 			++orig;
79 		} else
80 			*p++ = ch;
81 	*p = '\0';
82 }
83 
84 /*
85  * queryuser --
86  *	print a message to standard error and then read input from standard
87  *	input. If the input is 'y' then 1 is returned.
88  */
89 int
90 queryuser(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(u_int len)
124 {
125 	void *p;
126 
127 	if ((p = malloc(len)))
128 		return (p);
129 	err(1, NULL);
130 }
131 
132 /*
133  * show_path --
134  *	called on SIGINFO
135  */
136 /* ARGSUSED */
137 void
138 show_path(int signo)
139 {
140 	int save_errno = errno;
141 	extern FTSENT *entry;
142 	struct iovec iov[3];
143 
144 	if (entry != NULL) {
145 		iov[0].iov_base = "find path: ";
146 		iov[0].iov_len = strlen(iov[0].iov_base);
147 		iov[1].iov_base = entry->fts_path;
148 		iov[1].iov_len = entry->fts_pathlen;
149 		iov[2].iov_base = "\n";
150 		iov[2].iov_len = strlen(iov[2].iov_base);
151 		writev(STDERR_FILENO, iov, 3);
152 		errno = save_errno;
153 	}
154 }
155