11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1990, 1993, 1994
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * This code is derived from software contributed to Berkeley by
61590Srgrimes * Cimarron D. Taylor of the University of California, Berkeley.
71590Srgrimes *
81590Srgrimes * Redistribution and use in source and binary forms, with or without
91590Srgrimes * modification, are permitted provided that the following conditions
101590Srgrimes * are met:
111590Srgrimes * 1. Redistributions of source code must retain the above copyright
121590Srgrimes *    notice, this list of conditions and the following disclaimer.
131590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141590Srgrimes *    notice, this list of conditions and the following disclaimer in the
151590Srgrimes *    documentation and/or other materials provided with the distribution.
161590Srgrimes * 4. Neither the name of the University nor the names of its contributors
171590Srgrimes *    may be used to endorse or promote products derived from this software
181590Srgrimes *    without specific prior written permission.
191590Srgrimes *
201590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301590Srgrimes * SUCH DAMAGE.
311590Srgrimes */
321590Srgrimes
33207705Sdelphij#ifndef lint
34207705Sdelphij#if 0
35207705Sdelphijstatic char sccsid[] = "@(#)misc.c	8.2 (Berkeley) 4/1/94";
36207705Sdelphij#else
37207705Sdelphij#endif
38207705Sdelphij#endif /* not lint */
39207705Sdelphij
4093604Sobrien#include <sys/cdefs.h>
4193604Sobrien__FBSDID("$FreeBSD$");
421590Srgrimes
431590Srgrimes#include <sys/types.h>
441590Srgrimes#include <sys/stat.h>
451590Srgrimes
461590Srgrimes#include <err.h>
47200462Sdelphij#include <errno.h>
481590Srgrimes#include <fts.h>
491590Srgrimes#include <stdio.h>
501590Srgrimes#include <stdlib.h>
511590Srgrimes#include <string.h>
521590Srgrimes
531590Srgrimes#include "find.h"
548874Srgrimes
551590Srgrimes/*
561590Srgrimes * brace_subst --
571590Srgrimes *	Replace occurrences of {} in s1 with s2 and return the result string.
581590Srgrimes */
591590Srgrimesvoid
60287266Sjillesbrace_subst(char *orig, char **store, char *path, size_t len)
611590Srgrimes{
62287266Sjilles	const char *pastorigend, *p, *q;
63287266Sjilles	char *dst;
64287266Sjilles	size_t newlen, plen;
651590Srgrimes
661590Srgrimes	plen = strlen(path);
67287266Sjilles	newlen = strlen(orig) + 1;
68287266Sjilles	pastorigend = orig + newlen;
69287266Sjilles	for (p = orig; (q = strstr(p, "{}")) != NULL; p = q + 2) {
70287266Sjilles		if (plen > 2 && newlen + plen - 2 < newlen)
71287266Sjilles			errx(2, "brace_subst overflow");
72287266Sjilles		newlen += plen - 2;
73287266Sjilles	}
74287266Sjilles	if (newlen > len) {
75287266Sjilles		*store = reallocf(*store, newlen);
76287266Sjilles		if (*store == NULL)
77287266Sjilles			err(2, NULL);
78287266Sjilles	}
79287266Sjilles	dst = *store;
80287266Sjilles	for (p = orig; (q = strstr(p, "{}")) != NULL; p = q + 2) {
81287266Sjilles		memcpy(dst, p, q - p);
82287266Sjilles		dst += q - p;
83287266Sjilles		memcpy(dst, path, plen);
84287266Sjilles		dst += plen;
85287266Sjilles	}
86287266Sjilles	memcpy(dst, p, pastorigend - p);
871590Srgrimes}
881590Srgrimes
891590Srgrimes/*
901590Srgrimes * queryuser --
911590Srgrimes *	print a message to standard error and then read input from standard
92144526Stjr *	input. If the input is an affirmative response (according to the
93144526Stjr *	current locale) then 1 is returned.
941590Srgrimes */
951590Srgrimesint
96116333Smarkmqueryuser(char *argv[])
971590Srgrimes{
98144526Stjr	char *p, resp[256];
991590Srgrimes
1001590Srgrimes	(void)fprintf(stderr, "\"%s", *argv);
1011590Srgrimes	while (*++argv)
1021590Srgrimes		(void)fprintf(stderr, " %s", *argv);
1031590Srgrimes	(void)fprintf(stderr, "\"? ");
1041590Srgrimes	(void)fflush(stderr);
1051590Srgrimes
106144526Stjr	if (fgets(resp, sizeof(resp), stdin) == NULL)
107144526Stjr		*resp = '\0';
108144526Stjr	if ((p = strchr(resp, '\n')) != NULL)
109144526Stjr		*p = '\0';
110144526Stjr	else {
1111590Srgrimes		(void)fprintf(stderr, "\n");
1121590Srgrimes		(void)fflush(stderr);
1131590Srgrimes	}
114144526Stjr        return (rpmatch(resp) == 1);
1151590Srgrimes}
116