1 /* $NetBSD: util.c,v 1.5 1998/07/28 05:31:27 mycroft Exp $ */ 2 3 /*- 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 #if 0 39 static char sccsid[] = "@(#)util.c 8.2 (Berkeley) 4/2/94"; 40 #else 41 __RCSID("$NetBSD: util.c,v 1.5 1998/07/28 05:31:27 mycroft Exp $"); 42 #endif 43 #endif /* not lint */ 44 45 #include <sys/param.h> 46 #include <sys/stat.h> 47 #include <sys/wait.h> 48 49 #include <ctype.h> 50 #include <err.h> 51 #include <errno.h> 52 #include <paths.h> 53 #include <signal.h> 54 #include <stdio.h> 55 #include <stdlib.h> 56 #include <string.h> 57 #include <unistd.h> 58 59 #include "extern.h" 60 61 char * 62 colon(cp) 63 char *cp; 64 { 65 if (*cp == ':') /* Leading colon is part of file name. */ 66 return (0); 67 68 for (; *cp; ++cp) { 69 if (*cp == ':') 70 return (cp); 71 if (*cp == '/') 72 return (0); 73 } 74 return (0); 75 } 76 77 void 78 verifydir(cp) 79 char *cp; 80 { 81 struct stat stb; 82 83 if (!stat(cp, &stb)) { 84 if (S_ISDIR(stb.st_mode)) 85 return; 86 errno = ENOTDIR; 87 } 88 run_err("%s: %s", cp, strerror(errno)); 89 exit(1); 90 /* NOTREACHED */ 91 } 92 93 int 94 okname(cp0) 95 char *cp0; 96 { 97 int c; 98 char *cp; 99 100 cp = cp0; 101 do { 102 c = *cp; 103 if (c & 0200) 104 goto bad; 105 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-') 106 goto bad; 107 } while (*++cp); 108 return (1); 109 110 bad: warnx("%s: invalid user name", cp0); 111 return (0); 112 } 113 114 int 115 susystem(s) 116 char *s; 117 { 118 sig_t istat, qstat; 119 int status; 120 pid_t pid; 121 122 pid = vfork(); 123 switch (pid) { 124 case -1: 125 return (127); 126 127 case 0: 128 execl(_PATH_BSHELL, "sh", "-c", s, NULL); 129 _exit(127); 130 /* NOTREACHED */ 131 } 132 istat = signal(SIGINT, SIG_IGN); 133 qstat = signal(SIGQUIT, SIG_IGN); 134 if (waitpid(pid, &status, 0) < 0) 135 status = -1; 136 (void)signal(SIGINT, istat); 137 (void)signal(SIGQUIT, qstat); 138 return (status); 139 } 140 141 BUF * 142 allocbuf(bp, fd, blksize) 143 BUF *bp; 144 int fd, blksize; 145 { 146 struct stat stb; 147 size_t size; 148 149 if (fstat(fd, &stb) < 0) { 150 run_err("fstat: %s", strerror(errno)); 151 return (0); 152 } 153 size = roundup(stb.st_blksize, blksize); 154 if (size == 0) 155 size = blksize; 156 if (bp->cnt >= size) 157 return (bp); 158 if ((bp->buf = realloc(bp->buf, size)) == NULL) { 159 bp->cnt = 0; 160 run_err("%s", strerror(errno)); 161 return (0); 162 } 163 bp->cnt = size; 164 return (bp); 165 } 166 167 void 168 lostconn(signo) 169 int signo; 170 { 171 if (!iamremote) 172 warnx("lost connection"); 173 exit(1); 174 /* NOTREACHED */ 175 } 176