xref: /freebsd/usr.bin/find/misc.c (revision 5e3934b1)
19b50d902SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1990, 1993, 1994
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
89b50d902SRodney W. Grimes  * Cimarron D. Taylor of the University of California, Berkeley.
99b50d902SRodney W. Grimes  *
109b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
119b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
129b50d902SRodney W. Grimes  * are met:
139b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
149b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
159b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
169b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
179b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
199b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
209b50d902SRodney W. Grimes  *    without specific prior written permission.
219b50d902SRodney W. Grimes  *
229b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
239b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
249b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
259b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
269b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
279b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
289b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
299b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
309b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
319b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
329b50d902SRodney W. Grimes  * SUCH DAMAGE.
339b50d902SRodney W. Grimes  */
349b50d902SRodney W. Grimes 
359b50d902SRodney W. Grimes #include <sys/types.h>
369b50d902SRodney W. Grimes #include <sys/stat.h>
379b50d902SRodney W. Grimes 
389b50d902SRodney W. Grimes #include <err.h>
39821df508SXin LI #include <errno.h>
409b50d902SRodney W. Grimes #include <fts.h>
419b50d902SRodney W. Grimes #include <stdio.h>
429b50d902SRodney W. Grimes #include <stdlib.h>
439b50d902SRodney W. Grimes #include <string.h>
449b50d902SRodney W. Grimes 
459b50d902SRodney W. Grimes #include "find.h"
469b50d902SRodney W. Grimes 
479b50d902SRodney W. Grimes /*
489b50d902SRodney W. Grimes  * brace_subst --
499b50d902SRodney W. Grimes  *	Replace occurrences of {} in s1 with s2 and return the result string.
509b50d902SRodney W. Grimes  */
519b50d902SRodney W. Grimes void
brace_subst(char * orig,char ** store,char * path,size_t len)52a2925a5aSJilles Tjoelker brace_subst(char *orig, char **store, char *path, size_t len)
539b50d902SRodney W. Grimes {
54a2925a5aSJilles Tjoelker 	const char *pastorigend, *p, *q;
55a2925a5aSJilles Tjoelker 	char *dst;
56a2925a5aSJilles Tjoelker 	size_t newlen, plen;
579b50d902SRodney W. Grimes 
589b50d902SRodney W. Grimes 	plen = strlen(path);
59a2925a5aSJilles Tjoelker 	newlen = strlen(orig) + 1;
60a2925a5aSJilles Tjoelker 	pastorigend = orig + newlen;
61a2925a5aSJilles Tjoelker 	for (p = orig; (q = strstr(p, "{}")) != NULL; p = q + 2) {
62a2925a5aSJilles Tjoelker 		if (plen > 2 && newlen + plen - 2 < newlen)
63a2925a5aSJilles Tjoelker 			errx(2, "brace_subst overflow");
64a2925a5aSJilles Tjoelker 		newlen += plen - 2;
65a2925a5aSJilles Tjoelker 	}
66a2925a5aSJilles Tjoelker 	if (newlen > len) {
67a2925a5aSJilles Tjoelker 		*store = reallocf(*store, newlen);
68a2925a5aSJilles Tjoelker 		if (*store == NULL)
69a2925a5aSJilles Tjoelker 			err(2, NULL);
70a2925a5aSJilles Tjoelker 	}
71a2925a5aSJilles Tjoelker 	dst = *store;
72a2925a5aSJilles Tjoelker 	for (p = orig; (q = strstr(p, "{}")) != NULL; p = q + 2) {
73a2925a5aSJilles Tjoelker 		memcpy(dst, p, q - p);
74a2925a5aSJilles Tjoelker 		dst += q - p;
75a2925a5aSJilles Tjoelker 		memcpy(dst, path, plen);
76a2925a5aSJilles Tjoelker 		dst += plen;
77a2925a5aSJilles Tjoelker 	}
78a2925a5aSJilles Tjoelker 	memcpy(dst, p, pastorigend - p);
799b50d902SRodney W. Grimes }
809b50d902SRodney W. Grimes 
819b50d902SRodney W. Grimes /*
829b50d902SRodney W. Grimes  * queryuser --
839b50d902SRodney W. Grimes  *	print a message to standard error and then read input from standard
84ef6c7764STim J. Robbins  *	input. If the input is an affirmative response (according to the
85ef6c7764STim J. Robbins  *	current locale) then 1 is returned.
869b50d902SRodney W. Grimes  */
879b50d902SRodney W. Grimes int
queryuser(char * argv[])88ef646f18SMark Murray queryuser(char *argv[])
899b50d902SRodney W. Grimes {
90ef6c7764STim J. Robbins 	char *p, resp[256];
919b50d902SRodney W. Grimes 
929b50d902SRodney W. Grimes 	(void)fprintf(stderr, "\"%s", *argv);
939b50d902SRodney W. Grimes 	while (*++argv)
949b50d902SRodney W. Grimes 		(void)fprintf(stderr, " %s", *argv);
959b50d902SRodney W. Grimes 	(void)fprintf(stderr, "\"? ");
969b50d902SRodney W. Grimes 	(void)fflush(stderr);
979b50d902SRodney W. Grimes 
98ef6c7764STim J. Robbins 	if (fgets(resp, sizeof(resp), stdin) == NULL)
99ef6c7764STim J. Robbins 		*resp = '\0';
100ef6c7764STim J. Robbins 	if ((p = strchr(resp, '\n')) != NULL)
101ef6c7764STim J. Robbins 		*p = '\0';
102ef6c7764STim J. Robbins 	else {
1039b50d902SRodney W. Grimes 		(void)fprintf(stderr, "\n");
1049b50d902SRodney W. Grimes 		(void)fflush(stderr);
1059b50d902SRodney W. Grimes 	}
106ef6c7764STim J. Robbins         return (rpmatch(resp) == 1);
1079b50d902SRodney W. Grimes }
108