1 /*        $NetBSD: misc.c,v 1.16 2023/08/10 20:36:28 mrg 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. 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/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "from: @(#)misc.c          8.2 (Berkeley) 4/1/94";
39 #else
40 __RCSID("$NetBSD: misc.c,v 1.16 2023/08/10 20:36:28 mrg Exp $");
41 #endif
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 #include <paths.h>
55 #include <fcntl.h>
56 
57 #include "find.h"
58 
59 /*
60  * brace_subst --
61  *        Replace occurrences of {} in orig with path, and place it in a malloced
62  *      area of memory set in store.
63  */
64 void
brace_subst(char * orig,char ** store,char * path,size_t * len)65 brace_subst(char *orig, char **store, char *path, size_t *len)
66 {
67           size_t nlen, plen, rest;
68           char ch, *p, *ostore;
69 
70           plen = strlen(path);
71           for (p = *store; (ch = *orig) != '\0'; ++orig)
72                     if (ch == '{' && orig[1] == '}') {
73                               /* Length of string after the {}. */
74                               rest = strlen(&orig[2]);
75 
76                               nlen = *len;
77                               while ((p - *store) + plen + rest + 1 > nlen)
78                                         nlen *= 2;
79 
80                               if (nlen > *len) {
81                                         ptrdiff_t off = p - *store;
82 
83                                         ostore = *store;
84                                         if ((*store = realloc(ostore, nlen)) == NULL)
85                                                   err(1, "realloc");
86                                         *len = nlen;
87                                         p = *store + off;   /* Relocate. */
88                               }
89                               memmove(p, path, plen);
90                               p += plen;
91                               ++orig;
92                     } else
93                               *p++ = ch;
94           *p = '\0';
95 }
96 
97 /*
98  * queryuser --
99  *        print a message to standard error and then read input from standard
100  *        input. If the input is 'y' then 1 is returned.
101  */
102 int
queryuser(char ** argv)103 queryuser(char **argv)
104 {
105           int ch, first, nl;
106 
107           (void)fprintf(stderr, "\"%s", *argv);
108           while (*++argv)
109                     (void)fprintf(stderr, " %s", *argv);
110           (void)fprintf(stderr, "\"? ");
111           (void)fflush(stderr);
112 
113           first = ch = getchar();
114           for (nl = 0;;) {
115                     if (ch == '\n') {
116                               nl = 1;
117                               break;
118                     }
119                     if (ch == EOF)
120                               break;
121                     ch = getchar();
122           }
123 
124           if (!nl) {
125                     (void)fprintf(stderr, "\n");
126                     (void)fflush(stderr);
127           }
128         return (first == 'y');
129 }
130 
131 /*
132  * show_path --
133  *        called on SIGINFO
134  */
135 /* ARGSUSED */
136 void
show_path(int sig)137 show_path(int sig)
138 {
139           extern FTSENT *g_entry;
140           static int ttyfd = -2;
141           char buf[2048];
142           int oerrno, n;
143 
144           if (g_entry == NULL) {
145                     /*
146                      * not initialized yet.
147                      * assumption: pointer assignment is atomic.
148                      */
149                     return;
150           }
151 
152           oerrno = errno;
153           if (ttyfd == -2)
154                     ttyfd = open(_PATH_TTY, O_RDWR | O_CLOEXEC);
155 
156           if (ttyfd == -1)
157                     goto out;
158 
159           n = snprintf_ss(buf, sizeof(buf), "%s: path %s\n", getprogname(),
160               g_entry->fts_path);
161 
162           if (n <= 0)
163                     goto out;
164 
165           write(ttyfd, buf, (size_t)n);
166 out:
167           errno = oerrno;
168 }
169