misc.c revision 144526
12311Sjkh/*- 22311Sjkh * Copyright (c) 1990, 1993, 1994 32311Sjkh * The Regents of the University of California. All rights reserved. 42311Sjkh * 52311Sjkh * This code is derived from software contributed to Berkeley by 62311Sjkh * Cimarron D. Taylor of the University of California, Berkeley. 72311Sjkh * 82311Sjkh * Redistribution and use in source and binary forms, with or without 92311Sjkh * modification, are permitted provided that the following conditions 102311Sjkh * are met: 112311Sjkh * 1. Redistributions of source code must retain the above copyright 122311Sjkh * notice, this list of conditions and the following disclaimer. 132311Sjkh * 2. Redistributions in binary form must reproduce the above copyright 142311Sjkh * notice, this list of conditions and the following disclaimer in the 152311Sjkh * documentation and/or other materials provided with the distribution. 165176Sache * 3. All advertising materials mentioning features or use of this software 172311Sjkh * must display the following acknowledgement: 182311Sjkh * This product includes software developed by the University of 192311Sjkh * California, Berkeley and its contributors. 2029452Scharnier * 4. Neither the name of the University nor the names of its contributors 2150479Speter * may be used to endorse or promote products derived from this software 222311Sjkh * without specific prior written permission. 232311Sjkh * 242311Sjkh * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 252311Sjkh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 262311Sjkh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 272311Sjkh * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 282311Sjkh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 292311Sjkh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 302311Sjkh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 312311Sjkh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 322311Sjkh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 332311Sjkh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3469793Sobrien * SUCH DAMAGE. 352311Sjkh */ 362311Sjkh 372311Sjkh#ifndef lint 382311Sjkh#if 0 392311Sjkhstatic char sccsid[] = "@(#)misc.c 8.2 (Berkeley) 4/1/94"; 402311Sjkh#else 412311Sjkh#endif 422311Sjkh#endif /* not lint */ 432311Sjkh 442311Sjkh#include <sys/cdefs.h> 452311Sjkh__FBSDID("$FreeBSD: head/usr.bin/find/misc.c 144526 2005-04-02 07:44:12Z tjr $"); 462311Sjkh 472311Sjkh#include <sys/types.h> 482311Sjkh#include <sys/stat.h> 492311Sjkh 502311Sjkh#include <err.h> 512311Sjkh#include <errno.h> 522311Sjkh#include <fts.h> 532311Sjkh#include <stdio.h> 542311Sjkh#include <stdlib.h> 552311Sjkh#include <string.h> 562311Sjkh 572311Sjkh#include "find.h" 582311Sjkh 592311Sjkh/* 602311Sjkh * brace_subst -- 612311Sjkh * Replace occurrences of {} in s1 with s2 and return the result string. 622311Sjkh */ 632311Sjkhvoid 642311Sjkhbrace_subst(char *orig, char **store, char *path, int len) 652311Sjkh{ 662311Sjkh int plen; 672311Sjkh char ch, *p; 682311Sjkh 692311Sjkh plen = strlen(path); 702311Sjkh for (p = *store; (ch = *orig) != '\0'; ++orig) 712311Sjkh if (ch == '{' && orig[1] == '}') { 722311Sjkh while ((p - *store) + plen > len) 732311Sjkh if (!(*store = realloc(*store, len *= 2))) 742311Sjkh err(1, NULL); 752311Sjkh memmove(p, path, plen); 762311Sjkh p += plen; 772311Sjkh ++orig; 7829452Scharnier } else 7929452Scharnier *p++ = ch; 8029452Scharnier *p = '\0'; 8129452Scharnier} 822311Sjkh 832311Sjkh/* 842311Sjkh * queryuser -- 852311Sjkh * print a message to standard error and then read input from standard 862311Sjkh * input. If the input is an affirmative response (according to the 872311Sjkh * current locale) then 1 is returned. 882311Sjkh */ 892311Sjkhint 902311Sjkhqueryuser(char *argv[]) 912311Sjkh{ 922311Sjkh char *p, resp[256]; 932311Sjkh 942311Sjkh (void)fprintf(stderr, "\"%s", *argv); 952311Sjkh while (*++argv) 962311Sjkh (void)fprintf(stderr, " %s", *argv); 972311Sjkh (void)fprintf(stderr, "\"? "); 982311Sjkh (void)fflush(stderr); 992311Sjkh 1002311Sjkh if (fgets(resp, sizeof(resp), stdin) == NULL) 1012311Sjkh *resp = '\0'; 1022311Sjkh if ((p = strchr(resp, '\n')) != NULL) 1032311Sjkh *p = '\0'; 1042311Sjkh else { 1052311Sjkh (void)fprintf(stderr, "\n"); 1062311Sjkh (void)fflush(stderr); 10729452Scharnier } 1082311Sjkh return (rpmatch(resp) == 1); 1092311Sjkh} 1102311Sjkh