util.c revision 1.8 1 /* $NetBSD: util.c,v 1.8 2005/02/17 15:25:02 xtraeme 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.8 2005/02/17 15:25:02 xtraeme 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(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 void
73 verifydir(char *cp)
74 {
75 struct stat stb;
76
77 if (!stat(cp, &stb)) {
78 if (S_ISDIR(stb.st_mode))
79 return;
80 errno = ENOTDIR;
81 }
82 run_err("%s: %s", cp, strerror(errno));
83 exit(1);
84 /* NOTREACHED */
85 }
86
87 int
88 okname(char *cp0)
89 {
90 int c;
91 char *cp;
92
93 cp = cp0;
94 do {
95 c = *cp;
96 if (c & 0200)
97 goto bad;
98 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
99 goto bad;
100 } while (*++cp);
101 return (1);
102
103 bad: warnx("%s: invalid user name", cp0);
104 return (0);
105 }
106
107 int
108 susystem(char *s)
109 {
110 sig_t istat, qstat;
111 int status;
112 pid_t pid;
113
114 pid = vfork();
115 switch (pid) {
116 case -1:
117 return (127);
118
119 case 0:
120 execl(_PATH_BSHELL, "sh", "-c", s, NULL);
121 _exit(127);
122 /* NOTREACHED */
123 }
124 istat = signal(SIGINT, SIG_IGN);
125 qstat = signal(SIGQUIT, SIG_IGN);
126 if (waitpid(pid, &status, 0) < 0)
127 status = -1;
128 (void)signal(SIGINT, istat);
129 (void)signal(SIGQUIT, qstat);
130 return (status);
131 }
132
133 BUF *
134 allocbuf(BUF *bp, int fd, int blksize)
135 {
136 struct stat stb;
137 size_t size;
138 char *nbuf;
139
140 if (fstat(fd, &stb) < 0) {
141 run_err("fstat: %s", strerror(errno));
142 return (0);
143 }
144 size = roundup(stb.st_blksize, blksize);
145 if (size == 0)
146 size = blksize;
147 if (bp->cnt >= size)
148 return (bp);
149 if ((nbuf = realloc(bp->buf, size)) == NULL) {
150 free(bp->buf);
151 bp->buf = NULL;
152 bp->cnt = 0;
153 run_err("%s", strerror(errno));
154 return (0);
155 }
156 bp->buf = nbuf;
157 bp->cnt = size;
158 return (bp);
159 }
160
161 void
162 lostconn(int signo)
163 {
164 if (!iamremote)
165 warnx("lost connection");
166 exit(1);
167 /* NOTREACHED */
168 }
169