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