1 /*- 2 * Copyright (c) 1992 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)util.c 5.2 (Berkeley) 06/30/92"; 10 #endif /* not lint */ 11 12 #include <sys/param.h> 13 #include <sys/stat.h> 14 #include <sys/wait.h> 15 16 #include <signal.h> 17 #include <errno.h> 18 #include <unistd.h> 19 #include <stdlib.h> 20 #include <stdio.h> 21 #include <ctype.h> 22 #include <string.h> 23 #include <paths.h> 24 #include "extern.h" 25 26 char * 27 colon(cp) 28 register char *cp; 29 { 30 if (*cp == ':') /* Leading colon is part of file name. */ 31 return (0); 32 33 for (; *cp; ++cp) { 34 if (*cp == ':') 35 return (cp); 36 if (*cp == '/') 37 return (0); 38 } 39 return (0); 40 } 41 42 void 43 verifydir(cp) 44 char *cp; 45 { 46 struct stat stb; 47 48 if (!stat(cp, &stb)) { 49 if (S_ISDIR(stb.st_mode)) 50 return; 51 errno = ENOTDIR; 52 } 53 err("%s: %s", cp, strerror(errno)); 54 exit(1); 55 } 56 57 int 58 okname(cp0) 59 char *cp0; 60 { 61 register int c; 62 register char *cp; 63 64 cp = cp0; 65 do { 66 c = *cp; 67 if (c & 0200) 68 goto bad; 69 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-') 70 goto bad; 71 } while (*++cp); 72 return (1); 73 74 bad: (void)fprintf(stderr, "rcp: %s: invalid user name\n", cp0); 75 return (0); 76 } 77 78 int 79 susystem(s, userid) 80 int userid; 81 char *s; 82 { 83 register sig_t istat, qstat; 84 int status, pid, w; 85 86 if ((pid = vfork()) == 0) { 87 (void)setuid(userid); 88 execl(_PATH_BSHELL, "sh", "-c", s, NULL); 89 _exit(127); 90 } 91 istat = signal(SIGINT, SIG_IGN); 92 qstat = signal(SIGQUIT, SIG_IGN); 93 while ((w = wait(&status)) != pid && w != -1) 94 ; 95 if (w == -1) 96 status = -1; 97 (void)signal(SIGINT, istat); 98 (void)signal(SIGQUIT, qstat); 99 return (status); 100 } 101 102 BUF * 103 allocbuf(bp, fd, blksize) 104 BUF *bp; 105 int fd, blksize; 106 { 107 struct stat stb; 108 size_t size; 109 110 if (fstat(fd, &stb) < 0) { 111 err("fstat: %s", strerror(errno)); 112 return (0); 113 } 114 size = roundup(stb.st_blksize, blksize); 115 if (size == 0) 116 size = blksize; 117 if (bp->cnt >= size) 118 return (bp); 119 if ((bp->buf = realloc(bp->buf, size)) == NULL) { 120 bp->cnt = 0; 121 err("%s", strerror(errno)); 122 return (0); 123 } 124 bp->cnt = size; 125 return (bp); 126 } 127 128 void 129 lostconn(signo) 130 int signo; 131 { 132 if (!iamremote) 133 (void)fprintf(stderr, "rcp: lost connection\n"); 134 exit(1); 135 } 136 137 void 138 nospace() 139 { 140 (void)fprintf(stderr, "rcp: %s\n", strerror(errno)); 141 exit(1); 142 } 143