rework susystem
[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
KB
4 *
5 * %sccs.include.redist.c%
6 */
7
8#ifndef lint
0bcf7a5b 9static char sccsid[] = "@(#)util.c 8.2 (Berkeley) %G%";
a873b449
KB
10#endif /* not lint */
11
12#include <sys/param.h>
13#include <sys/stat.h>
14#include <sys/wait.h>
15
d6fb15ad
KB
16#include <ctype.h>
17#include <err.h>
a873b449 18#include <errno.h>
d6fb15ad
KB
19#include <paths.h>
20#include <signal.h>
a873b449 21#include <stdio.h>
d6fb15ad 22#include <stdlib.h>
a873b449 23#include <string.h>
d6fb15ad
KB
24#include <unistd.h>
25
a873b449
KB
26#include "extern.h"
27
28char *
29colon(cp)
0bcf7a5b 30 char *cp;
a873b449 31{
ac59afe5
AC
32 if (*cp == ':') /* Leading colon is part of file name. */
33 return (0);
34
a873b449
KB
35 for (; *cp; ++cp) {
36 if (*cp == ':')
37 return (cp);
38 if (*cp == '/')
39 return (0);
40 }
41 return (0);
42}
43
44void
45verifydir(cp)
46 char *cp;
47{
48 struct stat stb;
49
50 if (!stat(cp, &stb)) {
51 if (S_ISDIR(stb.st_mode))
52 return;
53 errno = ENOTDIR;
54 }
d6fb15ad 55 run_err("%s: %s", cp, strerror(errno));
a873b449
KB
56 exit(1);
57}
58
59int
60okname(cp0)
61 char *cp0;
62{
0bcf7a5b
JSP
63 int c;
64 char *cp;
a873b449
KB
65
66 cp = cp0;
67 do {
68 c = *cp;
69 if (c & 0200)
70 goto bad;
71 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
72 goto bad;
73 } while (*++cp);
74 return (1);
75
d6fb15ad 76bad: warnx("%s: invalid user name", cp0);
a873b449
KB
77 return (0);
78}
79
80int
81susystem(s, userid)
82 int userid;
83 char *s;
84{
0bcf7a5b
JSP
85 sig_t istat, qstat;
86 int status, w;
87 pid_t pid;
88
89 pid = vfork();
90 switch (pid) {
91 case -1:
92 return (127);
93
94 case 0:
a873b449
KB
95 (void)setuid(userid);
96 execl(_PATH_BSHELL, "sh", "-c", s, NULL);
97 _exit(127);
98 }
99 istat = signal(SIGINT, SIG_IGN);
100 qstat = signal(SIGQUIT, SIG_IGN);
0bcf7a5b 101 if (waitpid(pid, &status, 0) < 0)
a873b449
KB
102 status = -1;
103 (void)signal(SIGINT, istat);
104 (void)signal(SIGQUIT, qstat);
105 return (status);
106}
107
108BUF *
109allocbuf(bp, fd, blksize)
110 BUF *bp;
111 int fd, blksize;
112{
113 struct stat stb;
114 size_t size;
115
116 if (fstat(fd, &stb) < 0) {
d6fb15ad 117 run_err("fstat: %s", strerror(errno));
a873b449
KB
118 return (0);
119 }
120 size = roundup(stb.st_blksize, blksize);
121 if (size == 0)
122 size = blksize;
123 if (bp->cnt >= size)
124 return (bp);
125 if ((bp->buf = realloc(bp->buf, size)) == NULL) {
126 bp->cnt = 0;
d6fb15ad 127 run_err("%s", strerror(errno));
a873b449
KB
128 return (0);
129 }
130 bp->cnt = size;
131 return (bp);
132}
133
134void
135lostconn(signo)
136 int signo;
137{
138 if (!iamremote)
d6fb15ad 139 warnx("lost connection");
a873b449
KB
140 exit(1);
141}