1*81a719dfSmrg /* $NetBSD: misc.c,v 1.16 2023/08/10 20:36:28 mrg Exp $ */
29d225a17Stls
361f28255Scgd /*-
47b983ca6Smrg * Copyright (c) 1990, 1993, 1994
53dfa59ecSjtc * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * This code is derived from software contributed to Berkeley by
861f28255Scgd * Cimarron D. Taylor of the University of California, Berkeley.
961f28255Scgd *
1061f28255Scgd * Redistribution and use in source and binary forms, with or without
1161f28255Scgd * modification, are permitted provided that the following conditions
1261f28255Scgd * are met:
1361f28255Scgd * 1. Redistributions of source code must retain the above copyright
1461f28255Scgd * notice, this list of conditions and the following disclaimer.
1561f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd * notice, this list of conditions and the following disclaimer in the
1761f28255Scgd * documentation and/or other materials provided with the distribution.
1889aaa1bbSagc * 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd * may be used to endorse or promote products derived from this software
2061f28255Scgd * without specific prior written permission.
2161f28255Scgd *
2261f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd * SUCH DAMAGE.
3361f28255Scgd */
3461f28255Scgd
35403b699bSlukem #include <sys/cdefs.h>
3661f28255Scgd #ifndef lint
37403b699bSlukem #if 0
387b983ca6Smrg static char sccsid[] = "from: @(#)misc.c 8.2 (Berkeley) 4/1/94";
39403b699bSlukem #else
40*81a719dfSmrg __RCSID("$NetBSD: misc.c,v 1.16 2023/08/10 20:36:28 mrg Exp $");
41403b699bSlukem #endif
4261f28255Scgd #endif /* not lint */
4361f28255Scgd
4461f28255Scgd #include <sys/types.h>
4561f28255Scgd #include <sys/stat.h>
463dfa59ecSjtc
473dfa59ecSjtc #include <err.h>
483dfa59ecSjtc #include <errno.h>
493dfa59ecSjtc #include <fts.h>
5061f28255Scgd #include <stdio.h>
5161f28255Scgd #include <stdlib.h>
5261f28255Scgd #include <string.h>
539972ef9fSprovos #include <unistd.h>
548ff73d85Schristos #include <paths.h>
558ff73d85Schristos #include <fcntl.h>
563dfa59ecSjtc
5761f28255Scgd #include "find.h"
5861f28255Scgd
5961f28255Scgd /*
6061f28255Scgd * brace_subst --
6100333c9eSmrg * Replace occurrences of {} in orig with path, and place it in a malloced
6200333c9eSmrg * area of memory set in store.
6361f28255Scgd */
6461f28255Scgd void
brace_subst(char * orig,char ** store,char * path,size_t * len)658ff73d85Schristos brace_subst(char *orig, char **store, char *path, size_t *len)
6661f28255Scgd {
678ff73d85Schristos size_t nlen, plen, rest;
6849f30085Senami char ch, *p, *ostore;
6961f28255Scgd
7061f28255Scgd plen = strlen(path);
717b983ca6Smrg for (p = *store; (ch = *orig) != '\0'; ++orig)
7261f28255Scgd if (ch == '{' && orig[1] == '}') {
7349f30085Senami /* Length of string after the {}. */
7449f30085Senami rest = strlen(&orig[2]);
7549f30085Senami
7649f30085Senami nlen = *len;
7749f30085Senami while ((p - *store) + plen + rest + 1 > nlen)
7849f30085Senami nlen *= 2;
7949f30085Senami
8049f30085Senami if (nlen > *len) {
81*81a719dfSmrg ptrdiff_t off = p - *store;
82*81a719dfSmrg
8349f30085Senami ostore = *store;
8449f30085Senami if ((*store = realloc(ostore, nlen)) == NULL)
85403b699bSlukem err(1, "realloc");
8649f30085Senami *len = nlen;
87*81a719dfSmrg p = *store + off; /* Relocate. */
8849f30085Senami }
893dfa59ecSjtc memmove(p, path, plen);
9061f28255Scgd p += plen;
9161f28255Scgd ++orig;
9261f28255Scgd } else
9361f28255Scgd *p++ = ch;
9461f28255Scgd *p = '\0';
9561f28255Scgd }
9661f28255Scgd
9761f28255Scgd /*
9861f28255Scgd * queryuser --
9961f28255Scgd * print a message to standard error and then read input from standard
10061f28255Scgd * input. If the input is 'y' then 1 is returned.
10161f28255Scgd */
1023dfa59ecSjtc int
queryuser(char ** argv)1032eed134bSapb queryuser(char **argv)
10461f28255Scgd {
10561f28255Scgd int ch, first, nl;
10661f28255Scgd
10761f28255Scgd (void)fprintf(stderr, "\"%s", *argv);
10861f28255Scgd while (*++argv)
10961f28255Scgd (void)fprintf(stderr, " %s", *argv);
11061f28255Scgd (void)fprintf(stderr, "\"? ");
11161f28255Scgd (void)fflush(stderr);
11261f28255Scgd
11361f28255Scgd first = ch = getchar();
11461f28255Scgd for (nl = 0;;) {
11561f28255Scgd if (ch == '\n') {
11661f28255Scgd nl = 1;
11761f28255Scgd break;
11861f28255Scgd }
11961f28255Scgd if (ch == EOF)
12061f28255Scgd break;
12161f28255Scgd ch = getchar();
12261f28255Scgd }
12361f28255Scgd
12461f28255Scgd if (!nl) {
12561f28255Scgd (void)fprintf(stderr, "\n");
12661f28255Scgd (void)fflush(stderr);
12761f28255Scgd }
12861f28255Scgd return (first == 'y');
12961f28255Scgd }
13061f28255Scgd
13161f28255Scgd /*
1329972ef9fSprovos * show_path --
1339972ef9fSprovos * called on SIGINFO
1349972ef9fSprovos */
1359972ef9fSprovos /* ARGSUSED */
1369972ef9fSprovos void
show_path(int sig)1372eed134bSapb show_path(int sig)
1389972ef9fSprovos {
1399847a814Syamt extern FTSENT *g_entry;
1408ff73d85Schristos static int ttyfd = -2;
1418ff73d85Schristos char buf[2048];
1428ff73d85Schristos int oerrno, n;
1439972ef9fSprovos
1440c0dee33Syamt if (g_entry == NULL) {
1450c0dee33Syamt /*
1460c0dee33Syamt * not initialized yet.
1470c0dee33Syamt * assumption: pointer assignment is atomic.
1480c0dee33Syamt */
1490c0dee33Syamt return;
1500c0dee33Syamt }
1510c0dee33Syamt
1528ff73d85Schristos oerrno = errno;
1538ff73d85Schristos if (ttyfd == -2)
1548ff73d85Schristos ttyfd = open(_PATH_TTY, O_RDWR | O_CLOEXEC);
1558ff73d85Schristos
1568ff73d85Schristos if (ttyfd == -1)
1578ff73d85Schristos goto out;
1588ff73d85Schristos
1598ff73d85Schristos n = snprintf_ss(buf, sizeof(buf), "%s: path %s\n", getprogname(),
1608ff73d85Schristos g_entry->fts_path);
1618ff73d85Schristos
1628ff73d85Schristos if (n <= 0)
1638ff73d85Schristos goto out;
1648ff73d85Schristos
1658ff73d85Schristos write(ttyfd, buf, (size_t)n);
1668ff73d85Schristos out:
1678ff73d85Schristos errno = oerrno;
1689972ef9fSprovos }
169