1 /* $NetBSD: util.c,v 1.4 1997/07/20 20:47:33 christos 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.4 1997/07/20 20:47:33 christos 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 } 91 92 int 93 okname(cp0) 94 char *cp0; 95 { 96 int c; 97 char *cp; 98 99 cp = cp0; 100 do { 101 c = *cp; 102 if (c & 0200) 103 goto bad; 104 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-') 105 goto bad; 106 } while (*++cp); 107 return (1); 108 109 bad: warnx("%s: invalid user name", cp0); 110 return (0); 111 } 112 113 int 114 susystem(s) 115 char *s; 116 { 117 sig_t istat, qstat; 118 int status; 119 pid_t pid; 120 121 pid = vfork(); 122 switch (pid) { 123 case -1: 124 return (127); 125 126 case 0: 127 execl(_PATH_BSHELL, "sh", "-c", s, NULL); 128 _exit(127); 129 } 130 istat = signal(SIGINT, SIG_IGN); 131 qstat = signal(SIGQUIT, SIG_IGN); 132 if (waitpid(pid, &status, 0) < 0) 133 status = -1; 134 (void)signal(SIGINT, istat); 135 (void)signal(SIGQUIT, qstat); 136 return (status); 137 } 138 139 BUF * 140 allocbuf(bp, fd, blksize) 141 BUF *bp; 142 int fd, blksize; 143 { 144 struct stat stb; 145 size_t size; 146 147 if (fstat(fd, &stb) < 0) { 148 run_err("fstat: %s", strerror(errno)); 149 return (0); 150 } 151 size = roundup(stb.st_blksize, blksize); 152 if (size == 0) 153 size = blksize; 154 if (bp->cnt >= size) 155 return (bp); 156 if ((bp->buf = realloc(bp->buf, size)) == NULL) { 157 bp->cnt = 0; 158 run_err("%s", strerror(errno)); 159 return (0); 160 } 161 bp->cnt = size; 162 return (bp); 163 } 164 165 void 166 lostconn(signo) 167 int signo; 168 { 169 if (!iamremote) 170 warnx("lost connection"); 171 exit(1); 172 } 173