misc.c revision 91400
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
4161575Srobertostatic const char rcsid[] =
4261575Sroberto  "$FreeBSD: head/usr.bin/find/misc.c 91400 2002-02-27 17:57:00Z dwmalone $";
4361575Sroberto#endif
441590Srgrimes#endif /* not lint */
451590Srgrimes
461590Srgrimes#include <sys/types.h>
471590Srgrimes#include <sys/stat.h>
481590Srgrimes
491590Srgrimes#include <err.h>
501590Srgrimes#include <errno.h>
511590Srgrimes#include <fts.h>
521590Srgrimes#include <stdio.h>
531590Srgrimes#include <stdlib.h>
541590Srgrimes#include <string.h>
551590Srgrimes
561590Srgrimes#include "find.h"
578874Srgrimes
581590Srgrimes/*
591590Srgrimes * brace_subst --
601590Srgrimes *	Replace occurrences of {} in s1 with s2 and return the result string.
611590Srgrimes */
621590Srgrimesvoid
631590Srgrimesbrace_subst(orig, store, path, len)
641590Srgrimes	char *orig, **store, *path;
651590Srgrimes	int len;
661590Srgrimes{
6791400Sdwmalone	int plen;
6891400Sdwmalone	char ch, *p;
691590Srgrimes
701590Srgrimes	plen = strlen(path);
711590Srgrimes	for (p = *store; (ch = *orig) != '\0'; ++orig)
721590Srgrimes		if (ch == '{' && orig[1] == '}') {
731590Srgrimes			while ((p - *store) + plen > len)
741590Srgrimes				if (!(*store = realloc(*store, len *= 2)))
751590Srgrimes					err(1, NULL);
761590Srgrimes			memmove(p, path, plen);
771590Srgrimes			p += plen;
781590Srgrimes			++orig;
791590Srgrimes		} else
801590Srgrimes			*p++ = ch;
811590Srgrimes	*p = '\0';
821590Srgrimes}
831590Srgrimes
841590Srgrimes/*
851590Srgrimes * queryuser --
861590Srgrimes *	print a message to standard error and then read input from standard
871590Srgrimes *	input. If the input is 'y' then 1 is returned.
881590Srgrimes */
891590Srgrimesint
901590Srgrimesqueryuser(argv)
9191400Sdwmalone	char **argv;
921590Srgrimes{
931590Srgrimes	int ch, first, nl;
941590Srgrimes
951590Srgrimes	(void)fprintf(stderr, "\"%s", *argv);
961590Srgrimes	while (*++argv)
971590Srgrimes		(void)fprintf(stderr, " %s", *argv);
981590Srgrimes	(void)fprintf(stderr, "\"? ");
991590Srgrimes	(void)fflush(stderr);
1001590Srgrimes
1011590Srgrimes	first = ch = getchar();
1021590Srgrimes	for (nl = 0;;) {
1031590Srgrimes		if (ch == '\n') {
1041590Srgrimes			nl = 1;
1051590Srgrimes			break;
1061590Srgrimes		}
1071590Srgrimes		if (ch == EOF)
1081590Srgrimes			break;
1091590Srgrimes		ch = getchar();
1101590Srgrimes	}
1111590Srgrimes
1121590Srgrimes	if (!nl) {
1131590Srgrimes		(void)fprintf(stderr, "\n");
1141590Srgrimes		(void)fflush(stderr);
1151590Srgrimes	}
1161590Srgrimes        return (first == 'y');
1171590Srgrimes}
118