1 /*        $NetBSD: util.c,v 1.11 2006/12/15 22:45:34 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. 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.11 2006/12/15 22:45:34 christos 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 *
colon(char * cp)58 colon(char *cp)
59 {
60           if (*cp == ':')               /* Leading colon is part of file name. */
61                     return (0);
62 
63           for (; *cp; ++cp) {
64                     if (*cp == ':')
65                               return (cp);
66                     if (*cp == '/')
67                               return (0);
68           }
69           return (0);
70 }
71 
72 char *
unbracket(char * cp)73 unbracket(char *cp)
74 {
75           char *ep;
76 
77           if (*cp == '[') {
78                     ep = cp + (strlen(cp) - 1);
79                     if (*ep == ']') {
80                               *ep = '\0';
81                               ++cp;
82                     }
83           }
84           return (cp);
85 }
86 
87 void
verifydir(char * cp)88 verifydir(char *cp)
89 {
90           struct stat stb;
91 
92           if (!stat(cp, &stb)) {
93                     if (S_ISDIR(stb.st_mode))
94                               return;
95                     errno = ENOTDIR;
96           }
97           run_err("%s: %s", cp, strerror(errno));
98           exit(1);
99           /* NOTREACHED */
100 }
101 
102 int
okname(char * cp0)103 okname(char *cp0)
104 {
105           int c;
106           char *cp;
107 
108           cp = cp0;
109           do {
110                     c = *cp;
111                     if (c & 0200)
112                               goto bad;
113                     if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
114                               goto bad;
115           } while (*++cp);
116           return (1);
117 
118 bad:      warnx("%s: invalid user name", cp0);
119           return (0);
120 }
121 
122 int
susystem(char * s)123 susystem(char *s)
124 {
125           sig_t istat, qstat;
126           int status;
127           pid_t pid;
128 
129           pid = vfork();
130           switch (pid) {
131           case -1:
132                     return (127);
133 
134           case 0:
135                     (void)execl(_PATH_BSHELL, "sh", "-c", s, NULL);
136                     _exit(127);
137                     /* NOTREACHED */
138           }
139           istat = signal(SIGINT, SIG_IGN);
140           qstat = signal(SIGQUIT, SIG_IGN);
141           if (waitpid(pid, &status, 0) < 0)
142                     status = -1;
143           (void)signal(SIGINT, istat);
144           (void)signal(SIGQUIT, qstat);
145           return (status);
146 }
147 
148 BUF *
allocbuf(BUF * bp,int fd,int blksize)149 allocbuf(BUF *bp, int fd, int blksize)
150 {
151           struct stat stb;
152           size_t size;
153           char *nbuf;
154 
155           if (fstat(fd, &stb) < 0) {
156                     run_err("fstat: %s", strerror(errno));
157                     return (0);
158           }
159           size = roundup(stb.st_blksize, blksize);
160           if (size == 0)
161                     size = blksize;
162           if (bp->cnt >= size)
163                     return (bp);
164           if ((nbuf = realloc(bp->buf, size)) == NULL) {
165                     free(bp->buf);
166                     bp->buf = NULL;
167                     bp->cnt = 0;
168                     run_err("%s", strerror(errno));
169                     return (0);
170           }
171           bp->buf = nbuf;
172           bp->cnt = size;
173           return (bp);
174 }
175 
176 void
177 /*ARGSUSED*/
lostconn(int signo __unused)178 lostconn(int signo __unused)
179 {
180           if (!iamremote)
181                     warnx("lost connection");
182           exit(1);
183           /* NOTREACHED */
184 }
185