1 /* $NetBSD: util.c,v 1.7 2003/09/19 08:46:32 itojun 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)util.c 8.2 (Berkeley) 4/2/94"; 36 #else 37 __RCSID("$NetBSD: util.c,v 1.7 2003/09/19 08:46:32 itojun Exp $"); 38 #endif 39 #endif /* not lint */ 40 41 #include <sys/param.h> 42 #include <sys/stat.h> 43 #include <sys/wait.h> 44 45 #include <ctype.h> 46 #include <err.h> 47 #include <errno.h> 48 #include <paths.h> 49 #include <signal.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include <unistd.h> 54 55 #include "extern.h" 56 57 char * 58 colon(cp) 59 char *cp; 60 { 61 if (*cp == ':') /* Leading colon is part of file name. */ 62 return (0); 63 64 for (; *cp; ++cp) { 65 if (*cp == ':') 66 return (cp); 67 if (*cp == '/') 68 return (0); 69 } 70 return (0); 71 } 72 73 void 74 verifydir(cp) 75 char *cp; 76 { 77 struct stat stb; 78 79 if (!stat(cp, &stb)) { 80 if (S_ISDIR(stb.st_mode)) 81 return; 82 errno = ENOTDIR; 83 } 84 run_err("%s: %s", cp, strerror(errno)); 85 exit(1); 86 /* NOTREACHED */ 87 } 88 89 int 90 okname(cp0) 91 char *cp0; 92 { 93 int c; 94 char *cp; 95 96 cp = cp0; 97 do { 98 c = *cp; 99 if (c & 0200) 100 goto bad; 101 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-') 102 goto bad; 103 } while (*++cp); 104 return (1); 105 106 bad: warnx("%s: invalid user name", cp0); 107 return (0); 108 } 109 110 int 111 susystem(s) 112 char *s; 113 { 114 sig_t istat, qstat; 115 int status; 116 pid_t pid; 117 118 pid = vfork(); 119 switch (pid) { 120 case -1: 121 return (127); 122 123 case 0: 124 execl(_PATH_BSHELL, "sh", "-c", s, NULL); 125 _exit(127); 126 /* NOTREACHED */ 127 } 128 istat = signal(SIGINT, SIG_IGN); 129 qstat = signal(SIGQUIT, SIG_IGN); 130 if (waitpid(pid, &status, 0) < 0) 131 status = -1; 132 (void)signal(SIGINT, istat); 133 (void)signal(SIGQUIT, qstat); 134 return (status); 135 } 136 137 BUF * 138 allocbuf(bp, fd, blksize) 139 BUF *bp; 140 int fd, blksize; 141 { 142 struct stat stb; 143 size_t size; 144 char *nbuf; 145 146 if (fstat(fd, &stb) < 0) { 147 run_err("fstat: %s", strerror(errno)); 148 return (0); 149 } 150 size = roundup(stb.st_blksize, blksize); 151 if (size == 0) 152 size = blksize; 153 if (bp->cnt >= size) 154 return (bp); 155 if ((nbuf = realloc(bp->buf, size)) == NULL) { 156 free(bp->buf); 157 bp->buf = NULL; 158 bp->cnt = 0; 159 run_err("%s", strerror(errno)); 160 return (0); 161 } 162 bp->buf = nbuf; 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