1 /* $NetBSD: misc.c,v 1.9 2002/09/27 15:56:27 provos Exp $ */ 2 3 /*- 4 * Copyright (c) 1990, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Cimarron D. Taylor of the University of California, Berkeley. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "from: @(#)misc.c 8.2 (Berkeley) 4/1/94"; 43 #else 44 __RCSID("$NetBSD: misc.c,v 1.9 2002/09/27 15:56:27 provos Exp $"); 45 #endif 46 #endif /* not lint */ 47 48 #include <sys/types.h> 49 #include <sys/stat.h> 50 51 #include <err.h> 52 #include <errno.h> 53 #include <fts.h> 54 #include <stdio.h> 55 #include <stdlib.h> 56 #include <string.h> 57 #include <unistd.h> 58 59 #include "find.h" 60 61 /* 62 * brace_subst -- 63 * Replace occurrences of {} in orig with path, and place it in a malloced 64 * area of memory set in store. 65 */ 66 void 67 brace_subst(orig, store, path, len) 68 char *orig, **store, *path; 69 int *len; 70 { 71 int nlen, plen, rest; 72 char ch, *p, *ostore; 73 74 plen = strlen(path); 75 for (p = *store; (ch = *orig) != '\0'; ++orig) 76 if (ch == '{' && orig[1] == '}') { 77 /* Length of string after the {}. */ 78 rest = strlen(&orig[2]); 79 80 nlen = *len; 81 while ((p - *store) + plen + rest + 1 > nlen) 82 nlen *= 2; 83 84 if (nlen > *len) { 85 ostore = *store; 86 if ((*store = realloc(ostore, nlen)) == NULL) 87 err(1, "realloc"); 88 *len = nlen; 89 p += *store - ostore; /* Relocate. */ 90 } 91 memmove(p, path, plen); 92 p += plen; 93 ++orig; 94 } else 95 *p++ = ch; 96 *p = '\0'; 97 } 98 99 /* 100 * queryuser -- 101 * print a message to standard error and then read input from standard 102 * input. If the input is 'y' then 1 is returned. 103 */ 104 int 105 queryuser(argv) 106 char **argv; 107 { 108 int ch, first, nl; 109 110 (void)fprintf(stderr, "\"%s", *argv); 111 while (*++argv) 112 (void)fprintf(stderr, " %s", *argv); 113 (void)fprintf(stderr, "\"? "); 114 (void)fflush(stderr); 115 116 first = ch = getchar(); 117 for (nl = 0;;) { 118 if (ch == '\n') { 119 nl = 1; 120 break; 121 } 122 if (ch == EOF) 123 break; 124 ch = getchar(); 125 } 126 127 if (!nl) { 128 (void)fprintf(stderr, "\n"); 129 (void)fflush(stderr); 130 } 131 return (first == 'y'); 132 } 133 134 /* 135 * emalloc -- 136 * malloc with error checking. 137 */ 138 void * 139 emalloc(len) 140 u_int len; 141 { 142 void *p; 143 144 if ((p = malloc(len)) == NULL) 145 err(1, "malloc"); 146 return (p); 147 } 148 149 /* 150 * show_path -- 151 * called on SIGINFO 152 */ 153 /* ARGSUSED */ 154 void 155 show_path(sig) 156 int sig; 157 { 158 extern FTSENT *entry; 159 160 write(STDERR_FILENO, "find path: ", 11); 161 write(STDERR_FILENO, entry->fts_path, entry->fts_pathlen); 162 write(STDERR_FILENO, "\n", 1); 163 } 164