/*-1* Copyright (c) 1992, 19932* The Regents of the University of California. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. All advertising materials mentioning features or use of this software13* must display the following acknowledgement:14* This product includes software developed by the University of15* California, Berkeley and its contributors.16* 4. Neither the name of the University nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#if 034#ifndef lint35#if 036static char sccsid[] = "@(#)util.c 8.2 (Berkeley) 4/2/94";37#endif38static const char rcsid[] =39"$FreeBSD$";40#endif /* not lint */41#endif4243#include "rcp_locl.h"4445RCSID("$Id$");4647char *48colon(cp)49char *cp;50{51if (*cp == ':') /* Leading colon is part of file name. */52return (0);5354for (; *cp; ++cp) {55if (*cp == ':')56return (cp);57if (*cp == '/')58return (0);59}60return (0);61}6263char *64unbracket(char *cp)65{66char *ep;6768if (*cp == '[') {69ep = cp + (strlen(cp) - 1);70if (*ep == ']') {71*ep = '\0';72++cp;73}74}75return (cp);76}7778void79verifydir(cp)80char *cp;81{82struct stat stb;8384if (!stat(cp, &stb)) {85if (S_ISDIR(stb.st_mode))86return;87errno = ENOTDIR;88}89run_err("%s: %s", cp, strerror(errno));90exit(1);91}9293int94okname(cp0)95char *cp0;96{97int c;98unsigned char *cp;99100cp = (unsigned char *)cp0;101do {102c = *cp;103if (c & 0200)104goto bad;105if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')106goto bad;107} while (*++cp);108return (1);109110bad: warnx("%s: invalid user name", cp0);111return (0);112}113114int115susystem(s)116char *s;117{118void (*istat)(int), (*qstat)(int);119int status;120pid_t pid;121122pid = fork();123switch (pid) {124case -1:125return (127);126127case 0:128execl(_PATH_BSHELL, "sh", "-c", s, NULL);129_exit(127);130}131istat = signal(SIGINT, SIG_IGN);132qstat = signal(SIGQUIT, SIG_IGN);133if (waitpid(pid, &status, 0) < 0)134status = -1;135(void)signal(SIGINT, istat);136(void)signal(SIGQUIT, qstat);137return (status);138}139140#ifndef roundup141#define roundup(x, y) ((((x)+((y)-1))/(y))*(y))142#endif143144BUF *145allocbuf(bp, fd, blksize)146BUF *bp;147int fd, blksize;148{149struct stat stb;150size_t size;151char *p;152153if (fstat(fd, &stb) < 0) {154run_err("fstat: %s", strerror(errno));155return (0);156}157size = roundup(stb.st_blksize, blksize);158if (size == 0)159size = blksize;160if (bp->cnt >= size)161return (bp);162if ((p = realloc(bp->buf, size)) == NULL) {163if (bp->buf)164free(bp->buf);165bp->buf = NULL;166bp->cnt = 0;167run_err("%s", strerror(errno));168return (0);169}170memset(p, 0, size);171bp->buf = p;172bp->cnt = size;173return (bp);174}175176void177lostconn(signo)178int signo;179{180if (!iamremote)181warnx("lost connection");182exit(1);183}184185186