1 /* $OpenBSD: misc.c,v 1.18 2023/03/08 04:43:11 guenther 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 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/uio.h>
38
39 #include <err.h>
40 #include <errno.h>
41 #include <fts.h>
42 #include <stddef.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47
48 #include "find.h"
49
50 /*
51 * brace_subst --
52 * Replace occurrences of {} in s1 with s2 and return the result string.
53 */
54 void
brace_subst(char * orig,char ** store,char * path,int len)55 brace_subst(char *orig, char **store, char *path, int len)
56 {
57 int plen;
58 char ch, *p;
59
60 plen = strlen(path);
61 for (p = *store; (ch = *orig); ++orig)
62 if (ch == '{' && orig[1] == '}') {
63 while ((p - *store) + plen > len) {
64 ptrdiff_t p_off;
65 char *newstore;
66
67 p_off = (p - *store);
68 newstore = reallocarray(*store, len, 2);
69 if (newstore == NULL)
70 err(1, NULL);
71 p = newstore + p_off;
72 *store = newstore;
73 len *= 2;
74 }
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
queryuser(char ** argv)89 queryuser(char **argv)
90 {
91 int ch, first, nl;
92
93 (void)fprintf(stderr, "\"%s", *argv);
94 while (*++argv)
95 (void)fprintf(stderr, " %s", *argv);
96 (void)fprintf(stderr, "\"? ");
97 (void)fflush(stderr);
98
99 first = ch = getchar();
100 for (nl = 0;;) {
101 if (ch == '\n') {
102 nl = 1;
103 break;
104 }
105 if (ch == EOF)
106 break;
107 ch = getchar();
108 }
109
110 if (!nl) {
111 (void)fprintf(stderr, "\n");
112 (void)fflush(stderr);
113 }
114 return (first == 'y');
115 }
116
117 /*
118 * emalloc --
119 * malloc with error checking.
120 */
121 void *
emalloc(size_t len)122 emalloc(size_t len)
123 {
124 void *p;
125
126 if ((p = malloc(len)))
127 return (p);
128 err(1, NULL);
129 }
130
131 void *
ereallocarray(void * oldp,size_t sz1,size_t sz2)132 ereallocarray(void *oldp, size_t sz1, size_t sz2)
133 {
134 void *p;
135
136 if ((p = reallocarray(oldp, sz1, sz2)) != NULL)
137 return (p);
138 err(1, NULL);
139 }
140
141 /*
142 * show_path --
143 * called on SIGINFO
144 */
145 void
show_path(int signo)146 show_path(int signo)
147 {
148 int save_errno = errno;
149 extern FTSENT *entry;
150
151 if (entry != NULL) {
152 dprintf(STDERR_FILENO, "find path: %*s\n",
153 (int)entry->fts_pathlen, entry->fts_path);
154 errno = save_errno;
155 }
156 }
157