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