misc.c revision 8874
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 381590Srgrimesstatic char sccsid[] = "@(#)misc.c 8.2 (Berkeley) 4/1/94"; 391590Srgrimes#endif /* not lint */ 401590Srgrimes 411590Srgrimes#include <sys/types.h> 421590Srgrimes#include <sys/stat.h> 431590Srgrimes 441590Srgrimes#include <err.h> 451590Srgrimes#include <errno.h> 461590Srgrimes#include <fts.h> 471590Srgrimes#include <stdio.h> 481590Srgrimes#include <stdlib.h> 491590Srgrimes#include <string.h> 501590Srgrimes 511590Srgrimes#include "find.h" 528874Srgrimes 531590Srgrimes/* 541590Srgrimes * brace_subst -- 551590Srgrimes * Replace occurrences of {} in s1 with s2 and return the result string. 561590Srgrimes */ 571590Srgrimesvoid 581590Srgrimesbrace_subst(orig, store, path, len) 591590Srgrimes char *orig, **store, *path; 601590Srgrimes int len; 611590Srgrimes{ 621590Srgrimes register int plen; 631590Srgrimes register char ch, *p; 641590Srgrimes 651590Srgrimes plen = strlen(path); 661590Srgrimes for (p = *store; (ch = *orig) != '\0'; ++orig) 671590Srgrimes if (ch == '{' && orig[1] == '}') { 681590Srgrimes while ((p - *store) + plen > len) 691590Srgrimes if (!(*store = realloc(*store, len *= 2))) 701590Srgrimes err(1, NULL); 711590Srgrimes memmove(p, path, plen); 721590Srgrimes p += plen; 731590Srgrimes ++orig; 741590Srgrimes } else 751590Srgrimes *p++ = ch; 761590Srgrimes *p = '\0'; 771590Srgrimes} 781590Srgrimes 791590Srgrimes/* 801590Srgrimes * queryuser -- 811590Srgrimes * print a message to standard error and then read input from standard 821590Srgrimes * input. If the input is 'y' then 1 is returned. 831590Srgrimes */ 841590Srgrimesint 851590Srgrimesqueryuser(argv) 861590Srgrimes register char **argv; 871590Srgrimes{ 881590Srgrimes int ch, first, nl; 891590Srgrimes 901590Srgrimes (void)fprintf(stderr, "\"%s", *argv); 911590Srgrimes while (*++argv) 921590Srgrimes (void)fprintf(stderr, " %s", *argv); 931590Srgrimes (void)fprintf(stderr, "\"? "); 941590Srgrimes (void)fflush(stderr); 951590Srgrimes 961590Srgrimes first = ch = getchar(); 971590Srgrimes for (nl = 0;;) { 981590Srgrimes if (ch == '\n') { 991590Srgrimes nl = 1; 1001590Srgrimes break; 1011590Srgrimes } 1021590Srgrimes if (ch == EOF) 1031590Srgrimes break; 1041590Srgrimes ch = getchar(); 1051590Srgrimes } 1061590Srgrimes 1071590Srgrimes if (!nl) { 1081590Srgrimes (void)fprintf(stderr, "\n"); 1091590Srgrimes (void)fflush(stderr); 1101590Srgrimes } 1111590Srgrimes return (first == 'y'); 1121590Srgrimes} 1138874Srgrimes 1141590Srgrimes/* 1151590Srgrimes * emalloc -- 1161590Srgrimes * malloc with error checking. 1171590Srgrimes */ 1181590Srgrimesvoid * 1191590Srgrimesemalloc(len) 1201590Srgrimes u_int len; 1211590Srgrimes{ 1221590Srgrimes void *p; 1231590Srgrimes 1241590Srgrimes if ((p = malloc(len)) == NULL) 1251590Srgrimes err(1, NULL); 1261590Srgrimes return (p); 1271590Srgrimes} 128