No empty .Rs/.Re
[netbsd-mini2440.git] / bin / rcp / util.c
blob6e9c1b3d6e3fac8a361a2cd35e25bd03a21096b3
1 /* $NetBSD: util.c,v 1.10 2006/12/15 20:22:33 christos Exp $ */
3 /*-
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
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.
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.
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.10 2006/12/15 20:22:33 christos Exp $");
38 #endif
39 #endif /* not lint */
41 #include <sys/param.h>
42 #include <sys/stat.h>
43 #include <sys/wait.h>
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>
55 #include "extern.h"
57 char *
58 colon(char *cp)
60 if (*cp == ':') /* Leading colon is part of file name. */
61 return (0);
63 for (; *cp; ++cp) {
64 if (*cp == ':')
65 return (cp);
66 if (*cp == '/')
67 return (0);
69 return (0);
72 char *
73 unbracket(char *cp)
75 char *ep;
77 if (*cp == '[') {
78 ep = cp + (strlen(cp) - 1);
79 if (*ep == ']') {
80 *ep = '\0';
81 ++cp;
84 return (cp);
87 void
88 verifydir(char *cp)
90 struct stat stb;
92 if (!stat(cp, &stb)) {
93 if (S_ISDIR(stb.st_mode))
94 return;
95 errno = ENOTDIR;
97 run_err("%s: %s", cp, strerror(errno));
98 exit(1);
99 /* NOTREACHED */
103 okname(char *cp0)
105 int c;
106 char *cp;
108 cp = cp0;
109 do {
110 c = *cp;
111 if (c & 0200)
112 goto bad;
113 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
114 goto bad;
115 } while (*++cp);
116 return (1);
118 bad: warnx("%s: invalid user name", cp0);
119 return (0);
123 susystem(char *s)
125 sig_t istat, qstat;
126 int status;
127 pid_t pid;
129 pid = vfork();
130 switch (pid) {
131 case -1:
132 return (127);
134 case 0:
135 (void)execl(_PATH_BSHELL, "sh", "-c", s, NULL);
136 _exit(127);
137 /* NOTREACHED */
139 istat = signal(SIGINT, SIG_IGN);
140 qstat = signal(SIGQUIT, SIG_IGN);
141 if (waitpid(pid, &status, 0) < 0)
142 status = -1;
143 (void)signal(SIGINT, istat);
144 (void)signal(SIGQUIT, qstat);
145 return (status);
148 BUF *
149 allocbuf(BUF *bp, int fd, int blksize)
151 struct stat stb;
152 size_t size;
153 char *nbuf;
155 if (fstat(fd, &stb) < 0) {
156 run_err("fstat: %s", strerror(errno));
157 return (0);
159 size = roundup(stb.st_blksize, blksize);
160 if (size == 0)
161 size = blksize;
162 if (bp->cnt >= size)
163 return (bp);
164 if ((nbuf = realloc(bp->buf, size)) == NULL) {
165 free(bp->buf);
166 bp->buf = NULL;
167 bp->cnt = 0;
168 run_err("%s", strerror(errno));
169 return (0);
171 bp->buf = nbuf;
172 bp->cnt = size;
173 return (bp);
176 void
177 /*ARGSUSED*/
178 lostconn(int signo __unused)
180 if (!iamremote)
181 warnx("lost connection");
182 exit(1);
183 /* NOTREACHED */