BSD 4_4 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
ad787160 35static char sccsid[] = "@(#)util.c 8.1 (Berkeley) 5/31/93";
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)
56 register char *cp;
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{
89 register int c;
90 register char *cp;
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{
111 register sig_t istat, qstat;
112 int status, pid, w;
113
114 if ((pid = vfork()) == 0) {
115 (void)setuid(userid);
116 execl(_PATH_BSHELL, "sh", "-c", s, NULL);
117 _exit(127);
118 }
119 istat = signal(SIGINT, SIG_IGN);
120 qstat = signal(SIGQUIT, SIG_IGN);
121 while ((w = wait(&status)) != pid && w != -1)
122 ;
123 if (w == -1)
124 status = -1;
125 (void)signal(SIGINT, istat);
126 (void)signal(SIGQUIT, qstat);
127 return (status);
128}
129
130BUF *
131allocbuf(bp, fd, blksize)
132 BUF *bp;
133 int fd, blksize;
134{
135 struct stat stb;
136 size_t size;
137
138 if (fstat(fd, &stb) < 0) {
d6fb15ad 139 run_err("fstat: %s", strerror(errno));
a873b449
KB
140 return (0);
141 }
142 size = roundup(stb.st_blksize, blksize);
143 if (size == 0)
144 size = blksize;
145 if (bp->cnt >= size)
146 return (bp);
147 if ((bp->buf = realloc(bp->buf, size)) == NULL) {
148 bp->cnt = 0;
d6fb15ad 149 run_err("%s", strerror(errno));
a873b449
KB
150 return (0);
151 }
152 bp->cnt = size;
153 return (bp);
154}
155
156void
157lostconn(signo)
158 int signo;
159{
160 if (!iamremote)
d6fb15ad 161 warnx("lost connection");
a873b449
KB
162 exit(1);
163}