misc.c revision 116333
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 * 3. All advertising materials mentioning features or use of this software
171590Srgrimes *    must display the following acknowledgement:
181590Srgrimes *	This product includes software developed by the University of
191590Srgrimes *	California, Berkeley and its contributors.
201590Srgrimes * 4. Neither the name of the University nor the names of its contributors
211590Srgrimes *    may be used to endorse or promote products derived from this software
221590Srgrimes *    without specific prior written permission.
231590Srgrimes *
241590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341590Srgrimes * SUCH DAMAGE.
351590Srgrimes */
361590Srgrimes
371590Srgrimes#ifndef lint
3861575Sroberto#if 0
391590Srgrimesstatic char sccsid[] = "@(#)misc.c	8.2 (Berkeley) 4/1/94";
4061575Sroberto#else
4161575Sroberto#endif
421590Srgrimes#endif /* not lint */
43116333Smarkm
4493604Sobrien#include <sys/cdefs.h>
4593604Sobrien__FBSDID("$FreeBSD: head/usr.bin/find/misc.c 116333 2003-06-14 13:00:21Z markm $");
461590Srgrimes
471590Srgrimes#include <sys/types.h>
481590Srgrimes#include <sys/stat.h>
491590Srgrimes
501590Srgrimes#include <err.h>
511590Srgrimes#include <errno.h>
521590Srgrimes#include <fts.h>
531590Srgrimes#include <stdio.h>
541590Srgrimes#include <stdlib.h>
551590Srgrimes#include <string.h>
561590Srgrimes
571590Srgrimes#include "find.h"
588874Srgrimes
591590Srgrimes/*
601590Srgrimes * brace_subst --
611590Srgrimes *	Replace occurrences of {} in s1 with s2 and return the result string.
621590Srgrimes */
631590Srgrimesvoid
64116333Smarkmbrace_subst(char *orig, char **store, char *path, int len)
651590Srgrimes{
6691400Sdwmalone	int plen;
6791400Sdwmalone	char ch, *p;
681590Srgrimes
691590Srgrimes	plen = strlen(path);
701590Srgrimes	for (p = *store; (ch = *orig) != '\0'; ++orig)
711590Srgrimes		if (ch == '{' && orig[1] == '}') {
721590Srgrimes			while ((p - *store) + plen > len)
731590Srgrimes				if (!(*store = realloc(*store, len *= 2)))
741590Srgrimes					err(1, NULL);
751590Srgrimes			memmove(p, path, plen);
761590Srgrimes			p += plen;
771590Srgrimes			++orig;
781590Srgrimes		} else
791590Srgrimes			*p++ = ch;
801590Srgrimes	*p = '\0';
811590Srgrimes}
821590Srgrimes
831590Srgrimes/*
841590Srgrimes * queryuser --
851590Srgrimes *	print a message to standard error and then read input from standard
861590Srgrimes *	input. If the input is 'y' then 1 is returned.
871590Srgrimes */
881590Srgrimesint
89116333Smarkmqueryuser(char *argv[])
901590Srgrimes{
911590Srgrimes	int ch, first, nl;
921590Srgrimes
931590Srgrimes	(void)fprintf(stderr, "\"%s", *argv);
941590Srgrimes	while (*++argv)
951590Srgrimes		(void)fprintf(stderr, " %s", *argv);
961590Srgrimes	(void)fprintf(stderr, "\"? ");
971590Srgrimes	(void)fflush(stderr);
981590Srgrimes
991590Srgrimes	first = ch = getchar();
1001590Srgrimes	for (nl = 0;;) {
1011590Srgrimes		if (ch == '\n') {
1021590Srgrimes			nl = 1;
1031590Srgrimes			break;
1041590Srgrimes		}
1051590Srgrimes		if (ch == EOF)
1061590Srgrimes			break;
1071590Srgrimes		ch = getchar();
1081590Srgrimes	}
1091590Srgrimes
1101590Srgrimes	if (!nl) {
1111590Srgrimes		(void)fprintf(stderr, "\n");
1121590Srgrimes		(void)fflush(stderr);
1131590Srgrimes	}
1141590Srgrimes        return (first == 'y');
1151590Srgrimes}
116