172445Sassar/*-
272445Sassar * Copyright (c) 1992, 1993
372445Sassar *	The Regents of the University of California.  All rights reserved.
472445Sassar *
572445Sassar * Redistribution and use in source and binary forms, with or without
672445Sassar * modification, are permitted provided that the following conditions
772445Sassar * are met:
872445Sassar * 1. Redistributions of source code must retain the above copyright
972445Sassar *    notice, this list of conditions and the following disclaimer.
1072445Sassar * 2. Redistributions in binary form must reproduce the above copyright
1172445Sassar *    notice, this list of conditions and the following disclaimer in the
1272445Sassar *    documentation and/or other materials provided with the distribution.
1372445Sassar * 3. All advertising materials mentioning features or use of this software
1472445Sassar *    must display the following acknowledgement:
1572445Sassar *	This product includes software developed by the University of
1672445Sassar *	California, Berkeley and its contributors.
1772445Sassar * 4. Neither the name of the University nor the names of its contributors
1872445Sassar *    may be used to endorse or promote products derived from this software
1972445Sassar *    without specific prior written permission.
2072445Sassar *
2172445Sassar * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2272445Sassar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2372445Sassar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2472445Sassar * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2572445Sassar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2672445Sassar * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2772445Sassar * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2872445Sassar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2972445Sassar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3072445Sassar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3172445Sassar * SUCH DAMAGE.
3272445Sassar */
3372445Sassar
3472445Sassar#if 0
3572445Sassar#ifndef lint
3672445Sassar#if 0
3772445Sassarstatic char sccsid[] = "@(#)util.c	8.2 (Berkeley) 4/2/94";
3872445Sassar#endif
3972445Sassarstatic const char rcsid[] =
4072445Sassar  "$FreeBSD$";
4172445Sassar#endif /* not lint */
4272445Sassar#endif
4372445Sassar
4472445Sassar#include "rcp_locl.h"
4572445Sassar
46233294SstasRCSID("$Id$");
4772445Sassar
4872445Sassarchar *
4972445Sassarcolon(cp)
5072445Sassar	char *cp;
5172445Sassar{
5272445Sassar	if (*cp == ':')		/* Leading colon is part of file name. */
5372445Sassar		return (0);
5472445Sassar
5572445Sassar	for (; *cp; ++cp) {
5672445Sassar		if (*cp == ':')
5772445Sassar			return (cp);
5872445Sassar		if (*cp == '/')
5972445Sassar			return (0);
6072445Sassar	}
6172445Sassar	return (0);
6272445Sassar}
6372445Sassar
64233294Sstaschar *
65233294Sstasunbracket(char *cp)
66233294Sstas{
67233294Sstas	char *ep;
68233294Sstas
69233294Sstas	if (*cp == '[') {
70233294Sstas		ep = cp + (strlen(cp) - 1);
71233294Sstas		if (*ep == ']') {
72233294Sstas			*ep = '\0';
73233294Sstas			++cp;
74233294Sstas		}
75233294Sstas	}
76233294Sstas	return (cp);
77233294Sstas}
78233294Sstas
7972445Sassarvoid
8072445Sassarverifydir(cp)
8172445Sassar	char *cp;
8272445Sassar{
8372445Sassar	struct stat stb;
8472445Sassar
8572445Sassar	if (!stat(cp, &stb)) {
8672445Sassar		if (S_ISDIR(stb.st_mode))
8772445Sassar			return;
8872445Sassar		errno = ENOTDIR;
8972445Sassar	}
9072445Sassar	run_err("%s: %s", cp, strerror(errno));
9172445Sassar	exit(1);
9272445Sassar}
9372445Sassar
9472445Sassarint
9572445Sassarokname(cp0)
9672445Sassar	char *cp0;
9772445Sassar{
9872445Sassar	int c;
99178825Sdfr	unsigned char *cp;
10072445Sassar
101178825Sdfr	cp = (unsigned char *)cp0;
10272445Sassar	do {
10372445Sassar		c = *cp;
10472445Sassar		if (c & 0200)
10572445Sassar			goto bad;
10672445Sassar		if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
10772445Sassar			goto bad;
10872445Sassar	} while (*++cp);
10972445Sassar	return (1);
11072445Sassar
11172445Sassarbad:	warnx("%s: invalid user name", cp0);
11272445Sassar	return (0);
11372445Sassar}
11472445Sassar
11572445Sassarint
116233294Sstassusystem(s)
11772445Sassar	char *s;
11872445Sassar{
11972445Sassar	void (*istat)(int), (*qstat)(int);
12072445Sassar	int status;
12172445Sassar	pid_t pid;
12272445Sassar
12372445Sassar	pid = fork();
12472445Sassar	switch (pid) {
12572445Sassar	case -1:
12672445Sassar		return (127);
12772445Sassar
12872445Sassar	case 0:
12972445Sassar		execl(_PATH_BSHELL, "sh", "-c", s, NULL);
13072445Sassar		_exit(127);
13172445Sassar	}
13272445Sassar	istat = signal(SIGINT, SIG_IGN);
13372445Sassar	qstat = signal(SIGQUIT, SIG_IGN);
13472445Sassar	if (waitpid(pid, &status, 0) < 0)
13572445Sassar		status = -1;
13672445Sassar	(void)signal(SIGINT, istat);
13772445Sassar	(void)signal(SIGQUIT, qstat);
13872445Sassar	return (status);
13972445Sassar}
14072445Sassar
14172445Sassar#ifndef roundup
14272445Sassar#define	roundup(x, y)	((((x)+((y)-1))/(y))*(y))
14372445Sassar#endif
14472445Sassar
14572445SassarBUF *
14672445Sassarallocbuf(bp, fd, blksize)
14772445Sassar	BUF *bp;
14872445Sassar	int fd, blksize;
14972445Sassar{
15072445Sassar	struct stat stb;
15172445Sassar	size_t size;
15290926Snectar	char *p;
15372445Sassar
15472445Sassar	if (fstat(fd, &stb) < 0) {
15572445Sassar		run_err("fstat: %s", strerror(errno));
15672445Sassar		return (0);
15772445Sassar	}
15872445Sassar	size = roundup(stb.st_blksize, blksize);
15972445Sassar	if (size == 0)
16072445Sassar		size = blksize;
16172445Sassar	if (bp->cnt >= size)
16272445Sassar		return (bp);
16390926Snectar	if ((p = realloc(bp->buf, size)) == NULL) {
16490926Snectar		if (bp->buf)
16590926Snectar			free(bp->buf);
16690926Snectar		bp->buf = NULL;
16772445Sassar		bp->cnt = 0;
16872445Sassar		run_err("%s", strerror(errno));
16972445Sassar		return (0);
17072445Sassar	}
17190926Snectar	memset(p, 0, size);
17290926Snectar	bp->buf = p;
17372445Sassar	bp->cnt = size;
17472445Sassar	return (bp);
17572445Sassar}
17672445Sassar
17772445Sassarvoid
17872445Sassarlostconn(signo)
17972445Sassar	int signo;
18072445Sassar{
18172445Sassar	if (!iamremote)
18272445Sassar		warnx("lost connection");
18372445Sassar	exit(1);
18472445Sassar}
185