<sys/libkern.h>: Fix whitespace/indent, remove arg names in prototypes.
[dragonfly.git] / bin / rcp / util.c
bloba571536ea5cbd830b12626a615b6e5b28d6af098
1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#)util.c 8.2 (Berkeley) 4/2/94
30 * $FreeBSD: src/bin/rcp/util.c,v 1.9.2.3 2002/07/19 07:54:51 jmallett Exp $
31 * $DragonFly: src/bin/rcp/util.c,v 1.5 2004/11/19 19:01:52 eirikn Exp $
34 #include <sys/param.h>
35 #include <sys/stat.h>
36 #include <sys/wait.h>
38 #include <ctype.h>
39 #include <err.h>
40 #include <errno.h>
41 #include <paths.h>
42 #include <signal.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
48 #include "extern.h"
50 char *
51 colon(char *cp)
53 if (*cp == ':') /* Leading colon is part of file name. */
54 return (0);
56 for (; *cp; ++cp) {
57 if (*cp == ':')
58 return (cp);
59 if (*cp == '/')
60 return (0);
62 return (0);
65 void
66 verifydir(char *cp)
68 struct stat stb;
70 if (!stat(cp, &stb)) {
71 if (S_ISDIR(stb.st_mode))
72 return;
73 errno = ENOTDIR;
75 run_err("%s: %s", cp, strerror(errno));
76 exit(1);
79 int
80 okname(char *cp0)
82 int c;
83 char *cp;
85 cp = cp0;
86 do {
87 c = *cp;
88 if (c & 0200)
89 goto bad;
90 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-' && c != '.')
91 goto bad;
92 } while (*++cp);
93 return (1);
95 bad: warnx("%s: invalid user name", cp0);
96 return (0);
99 int
100 susystem(char *s, int userid)
102 sig_t istat, qstat;
103 int status;
104 pid_t pid;
106 pid = vfork();
107 switch (pid) {
108 case -1:
109 return (127);
111 case 0:
112 setuid(userid);
113 execl(_PATH_BSHELL, "sh", "-c", s, NULL);
114 _exit(127);
116 istat = signal(SIGINT, SIG_IGN);
117 qstat = signal(SIGQUIT, SIG_IGN);
118 if (waitpid(pid, &status, 0) < 0)
119 status = -1;
120 signal(SIGINT, istat);
121 signal(SIGQUIT, qstat);
122 return (status);
125 BUF *
126 allocbuf(BUF *bp, int fd, int blksize)
128 struct stat stb;
129 size_t size;
131 if (fstat(fd, &stb) < 0) {
132 run_err("fstat: %s", strerror(errno));
133 return (0);
135 size = roundup(stb.st_blksize, blksize);
136 if (size == 0)
137 size = blksize;
138 if (bp->cnt >= size)
139 return (bp);
140 if ((bp->buf = realloc(bp->buf, size)) == NULL) {
141 bp->cnt = 0;
142 run_err("%s", strerror(errno));
143 return (0);
145 bp->cnt = size;
146 return (bp);
149 /* ARGSUSED */
150 void
151 lostconn(int signo __unused)
153 if (!iamremote)
154 warnx("lost connection");
155 exit(1);