1 /* $OpenBSD: misc.c,v 1.9 2003/06/26 07:27:29 deraadt Exp $ */ 2 3 /*- 4 * Copyright (c) 1990, 1993 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. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #ifndef lint 36 /*static char sccsid[] = "from: @(#)misc.c 8.1 (Berkeley) 6/6/93";*/ 37 static char rcsid[] = "$OpenBSD: misc.c,v 1.9 2003/06/26 07:27:29 deraadt Exp $"; 38 #endif /* not lint */ 39 40 #include <sys/types.h> 41 #include <sys/stat.h> 42 #include <sys/uio.h> 43 44 #include <err.h> 45 #include <errno.h> 46 #include <fts.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <unistd.h> 51 52 #include "find.h" 53 54 /* 55 * brace_subst -- 56 * Replace occurrences of {} in s1 with s2 and return the result string. 57 */ 58 void 59 brace_subst(char *orig, char **store, char *path, int len) 60 { 61 int plen; 62 char ch, *p; 63 64 plen = strlen(path); 65 for (p = *store; (ch = *orig); ++orig) 66 if (ch == '{' && orig[1] == '}') { 67 while ((p - *store) + plen > len) 68 if (!(*store = realloc(*store, len *= 2))) 69 err(1, NULL); 70 memmove(p, path, plen); 71 p += plen; 72 ++orig; 73 } else 74 *p++ = ch; 75 *p = '\0'; 76 } 77 78 /* 79 * queryuser -- 80 * print a message to standard error and then read input from standard 81 * input. If the input is 'y' then 1 is returned. 82 */ 83 int 84 queryuser(char **argv) 85 { 86 int ch, first, nl; 87 88 (void)fprintf(stderr, "\"%s", *argv); 89 while (*++argv) 90 (void)fprintf(stderr, " %s", *argv); 91 (void)fprintf(stderr, "\"? "); 92 (void)fflush(stderr); 93 94 first = ch = getchar(); 95 for (nl = 0;;) { 96 if (ch == '\n') { 97 nl = 1; 98 break; 99 } 100 if (ch == EOF) 101 break; 102 ch = getchar(); 103 } 104 105 if (!nl) { 106 (void)fprintf(stderr, "\n"); 107 (void)fflush(stderr); 108 } 109 return (first == 'y'); 110 } 111 112 /* 113 * emalloc -- 114 * malloc with error checking. 115 */ 116 void * 117 emalloc(u_int len) 118 { 119 void *p; 120 121 if ((p = malloc(len))) 122 return (p); 123 err(1, NULL); 124 } 125 126 /* 127 * show_path -- 128 * called on SIGINFO 129 */ 130 /* ARGSUSED */ 131 void 132 show_path(int signo) 133 { 134 int save_errno = errno; 135 extern FTSENT *entry; 136 struct iovec iov[3]; 137 138 if (entry != NULL) { 139 iov[0].iov_base = "find path: "; 140 iov[0].iov_len = strlen(iov[0].iov_base); 141 iov[1].iov_base = entry->fts_path; 142 iov[1].iov_len = entry->fts_pathlen; 143 iov[2].iov_base = "\n"; 144 iov[2].iov_len = strlen(iov[2].iov_base); 145 writev(STDERR_FILENO, iov, 3); 146 errno = save_errno; 147 } 148 } 149