Remove advertising header from all userland binaries.
[dragonfly.git] / bin / rcp / util.c
1 /*-
2  * Copyright (c) 1992, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)util.c   8.2 (Berkeley) 4/2/94
30  * $FreeBSD: src/bin/rcp/util.c,v 1.9.2.3 2002/07/19 07:54:51 jmallett Exp $
31  * $DragonFly: src/bin/rcp/util.c,v 1.5 2004/11/19 19:01:52 eirikn Exp $
32  */
33
34 #include <sys/param.h>
35 #include <sys/stat.h>
36 #include <sys/wait.h>
37
38 #include <ctype.h>
39 #include <err.h>
40 #include <errno.h>
41 #include <paths.h>
42 #include <signal.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47
48 #include "extern.h"
49
50 char *
51 colon(char *cp)
52 {
53         if (*cp == ':')         /* Leading colon is part of file name. */
54                 return (0);
55
56         for (; *cp; ++cp) {
57                 if (*cp == ':')
58                         return (cp);
59                 if (*cp == '/')
60                         return (0);
61         }
62         return (0);
63 }
64
65 void
66 verifydir(char *cp)
67 {
68         struct stat stb;
69
70         if (!stat(cp, &stb)) {
71                 if (S_ISDIR(stb.st_mode))
72                         return;
73                 errno = ENOTDIR;
74         }
75         run_err("%s: %s", cp, strerror(errno));
76         exit(1);
77 }
78
79 int
80 okname(char *cp0)
81 {
82         int c;
83         char *cp;
84
85         cp = cp0;
86         do {
87                 c = *cp;
88                 if (c & 0200)
89                         goto bad;
90                 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-' && c != '.')
91                         goto bad;
92         } while (*++cp);
93         return (1);
94
95 bad:    warnx("%s: invalid user name", cp0);
96         return (0);
97 }
98
99 int
100 susystem(char *s, int userid)
101 {
102         sig_t istat, qstat;
103         int status;
104         pid_t pid;
105
106         pid = vfork();
107         switch (pid) {
108         case -1:
109                 return (127);
110
111         case 0:
112                 setuid(userid);
113                 execl(_PATH_BSHELL, "sh", "-c", s, NULL);
114                 _exit(127);
115         }
116         istat = signal(SIGINT, SIG_IGN);
117         qstat = signal(SIGQUIT, SIG_IGN);
118         if (waitpid(pid, &status, 0) < 0)
119                 status = -1;
120         signal(SIGINT, istat);
121         signal(SIGQUIT, qstat);
122         return (status);
123 }
124
125 BUF *
126 allocbuf(BUF *bp, int fd, int blksize)
127 {
128         struct stat stb;
129         size_t size;
130
131         if (fstat(fd, &stb) < 0) {
132                 run_err("fstat: %s", strerror(errno));
133                 return (0);
134         }
135         size = roundup(stb.st_blksize, blksize);
136         if (size == 0)
137                 size = blksize;
138         if (bp->cnt >= size)
139                 return (bp);
140         if ((bp->buf = realloc(bp->buf, size)) == NULL) {
141                 bp->cnt = 0;
142                 run_err("%s", strerror(errno));
143                 return (0);
144         }
145         bp->cnt = size;
146         return (bp);
147 }
148
149 /* ARGSUSED */
150 void
151 lostconn(int signo __unused)
152 {
153         if (!iamremote)
154                 warnx("lost connection");
155         exit(1);
156 }