misc.c revision 207705
118334Speter/*-
290075Sobrien * Copyright (c) 1990, 1993, 1994
3132718Skan *	The Regents of the University of California.  All rights reserved.
418334Speter *
590075Sobrien * This code is derived from software contributed to Berkeley by
618334Speter * Cimarron D. Taylor of the University of California, Berkeley.
790075Sobrien *
890075Sobrien * Redistribution and use in source and binary forms, with or without
990075Sobrien * modification, are permitted provided that the following conditions
1090075Sobrien * are met:
1118334Speter * 1. Redistributions of source code must retain the above copyright
1290075Sobrien *    notice, this list of conditions and the following disclaimer.
1390075Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1490075Sobrien *    notice, this list of conditions and the following disclaimer in the
1590075Sobrien *    documentation and/or other materials provided with the distribution.
1618334Speter * 3. All advertising materials mentioning features or use of this software
1718334Speter *    must display the following acknowledgement:
1890075Sobrien *	This product includes software developed by the University of
1990075Sobrien *	California, Berkeley and its contributors.
2090075Sobrien * 4. Neither the name of the University nor the names of its contributors
2118334Speter *    may be used to endorse or promote products derived from this software
2218334Speter *    without specific prior written permission.
2318334Speter *
2418334Speter * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2518334Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2618334Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2718334Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2818334Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2918334Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3018334Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3118334Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3218334Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3350397Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34132718Skan * SUCH DAMAGE.
35132718Skan */
3690075Sobrien
3718334Speter#ifndef lint
3818334Speter#if 0
3990075Sobrienstatic char sccsid[] = "@(#)misc.c	8.2 (Berkeley) 4/1/94";
4018334Speter#else
4118334Speter#endif
4250397Sobrien#endif /* not lint */
4350397Sobrien
4452284Sobrien#include <sys/cdefs.h>
4590075Sobrien__FBSDID("$FreeBSD: head/usr.bin/find/misc.c 207705 2010-05-06 17:06:36Z delphij $");
4690075Sobrien
4718334Speter#include <sys/types.h>
4818334Speter#include <sys/stat.h>
4918334Speter
5018334Speter#include <err.h>
5118334Speter#include <errno.h>
52132718Skan#include <fts.h>
53132718Skan#include <stdio.h>
5490075Sobrien#include <stdlib.h>
55132718Skan#include <string.h>
56132718Skan
57132718Skan#include "find.h"
58132718Skan
59132718Skan/*
60132718Skan * brace_subst --
61132718Skan *	Replace occurrences of {} in s1 with s2 and return the result string.
62132718Skan */
63132718Skanvoid
64132718Skanbrace_subst(char *orig, char **store, char *path, int len)
65132718Skan{
66132718Skan	int plen;
67132718Skan	char ch, *p;
68132718Skan
69132718Skan	plen = strlen(path);
70132718Skan	for (p = *store; (ch = *orig) != '\0'; ++orig)
71132718Skan		if (ch == '{' && orig[1] == '}') {
72132718Skan			while ((p - *store) + plen > len)
73132718Skan				if (!(*store = realloc(*store, len *= 2)))
74132718Skan					err(1, NULL);
75132718Skan			memmove(p, path, plen);
76132718Skan			p += plen;
77132718Skan			++orig;
78132718Skan		} else
79132718Skan			*p++ = ch;
80132718Skan	*p = '\0';
81132718Skan}
82132718Skan
83132718Skan/*
84132718Skan * queryuser --
85132718Skan *	print a message to standard error and then read input from standard
86132718Skan *	input. If the input is an affirmative response (according to the
87132718Skan *	current locale) then 1 is returned.
8818334Speter */
8918334Speterint
9018334Speterqueryuser(char *argv[])
9118334Speter{
9218334Speter	char *p, resp[256];
93132718Skan
9418334Speter	(void)fprintf(stderr, "\"%s", *argv);
9518334Speter	while (*++argv)
9618334Speter		(void)fprintf(stderr, " %s", *argv);
9790075Sobrien	(void)fprintf(stderr, "\"? ");
9852284Sobrien	(void)fflush(stderr);
9952284Sobrien
10018334Speter	if (fgets(resp, sizeof(resp), stdin) == NULL)
10190075Sobrien		*resp = '\0';
10218334Speter	if ((p = strchr(resp, '\n')) != NULL)
10318334Speter		*p = '\0';
104117395Skan	else {
10518334Speter		(void)fprintf(stderr, "\n");
10618334Speter		(void)fflush(stderr);
10718334Speter	}
10818334Speter        return (rpmatch(resp) == 1);
10918334Speter}
11018334Speter