utils.c revision 1.2 1 1.1 mycroft /*-
2 1.1 mycroft * Copyright (c) 1991, 1993, 1994
3 1.1 mycroft * The Regents of the University of California. All rights reserved.
4 1.1 mycroft *
5 1.1 mycroft * Redistribution and use in source and binary forms, with or without
6 1.1 mycroft * modification, are permitted provided that the following conditions
7 1.1 mycroft * are met:
8 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
9 1.1 mycroft * notice, this list of conditions and the following disclaimer.
10 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
12 1.1 mycroft * documentation and/or other materials provided with the distribution.
13 1.1 mycroft * 3. All advertising materials mentioning features or use of this software
14 1.1 mycroft * must display the following acknowledgement:
15 1.1 mycroft * This product includes software developed by the University of
16 1.1 mycroft * California, Berkeley and its contributors.
17 1.1 mycroft * 4. Neither the name of the University nor the names of its contributors
18 1.1 mycroft * may be used to endorse or promote products derived from this software
19 1.1 mycroft * without specific prior written permission.
20 1.1 mycroft *
21 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 mycroft * SUCH DAMAGE.
32 1.1 mycroft */
33 1.1 mycroft
34 1.1 mycroft #ifndef lint
35 1.2 mycroft /*static char sccsid[] = "from: @(#)utils.c 8.3 (Berkeley) 4/1/94";*/
36 1.2 mycroft static char *rcsid = "$Id: utils.c,v 1.2 1994/09/22 09:24:43 mycroft Exp $";
37 1.1 mycroft #endif /* not lint */
38 1.1 mycroft
39 1.1 mycroft #include <sys/param.h>
40 1.1 mycroft #include <sys/stat.h>
41 1.1 mycroft #include <sys/mman.h>
42 1.1 mycroft #include <sys/time.h>
43 1.1 mycroft
44 1.1 mycroft #include <err.h>
45 1.1 mycroft #include <errno.h>
46 1.1 mycroft #include <fcntl.h>
47 1.1 mycroft #include <fts.h>
48 1.1 mycroft #include <stdio.h>
49 1.1 mycroft #include <stdlib.h>
50 1.1 mycroft #include <string.h>
51 1.1 mycroft #include <unistd.h>
52 1.1 mycroft
53 1.1 mycroft #include "extern.h"
54 1.1 mycroft
55 1.1 mycroft int
56 1.1 mycroft copy_file(entp, dne)
57 1.1 mycroft FTSENT *entp;
58 1.1 mycroft int dne;
59 1.1 mycroft {
60 1.1 mycroft static char buf[MAXBSIZE];
61 1.1 mycroft struct stat to_stat, *fs;
62 1.1 mycroft int ch, checkch, from_fd, rcount, rval, to_fd, wcount;
63 1.1 mycroft #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
64 1.1 mycroft char *p;
65 1.1 mycroft #endif
66 1.1 mycroft
67 1.1 mycroft if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
68 1.1 mycroft warn("%s", entp->fts_path);
69 1.1 mycroft return (1);
70 1.1 mycroft }
71 1.1 mycroft
72 1.1 mycroft fs = entp->fts_statp;
73 1.1 mycroft
74 1.1 mycroft /*
75 1.1 mycroft * If the file exists and we're interactive, verify with the user.
76 1.1 mycroft * If the file DNE, set the mode to be the from file, minus setuid
77 1.1 mycroft * bits, modified by the umask; arguably wrong, but it makes copying
78 1.1 mycroft * executables work right and it's been that way forever. (The
79 1.1 mycroft * other choice is 666 or'ed with the execute bits on the from file
80 1.1 mycroft * modified by the umask.)
81 1.1 mycroft */
82 1.1 mycroft if (!dne) {
83 1.1 mycroft if (iflag) {
84 1.1 mycroft (void)fprintf(stderr, "overwrite %s? ", to.p_path);
85 1.1 mycroft checkch = ch = getchar();
86 1.1 mycroft while (ch != '\n' && ch != EOF)
87 1.1 mycroft ch = getchar();
88 1.2 mycroft if (checkch != 'y' && checkch != 'Y') {
89 1.1 mycroft (void)close(from_fd);
90 1.1 mycroft return (0);
91 1.1 mycroft }
92 1.1 mycroft }
93 1.1 mycroft to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
94 1.1 mycroft } else
95 1.1 mycroft to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
96 1.1 mycroft fs->st_mode & ~(S_ISUID | S_ISGID));
97 1.1 mycroft
98 1.1 mycroft if (to_fd == -1) {
99 1.1 mycroft warn("%s", to.p_path);
100 1.1 mycroft (void)close(from_fd);
101 1.1 mycroft return (1);;
102 1.1 mycroft }
103 1.1 mycroft
104 1.1 mycroft rval = 0;
105 1.1 mycroft
106 1.1 mycroft /*
107 1.1 mycroft * Mmap and write if less than 8M (the limit is so we don't totally
108 1.1 mycroft * trash memory on big files. This is really a minor hack, but it
109 1.1 mycroft * wins some CPU back.
110 1.1 mycroft */
111 1.1 mycroft #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
112 1.1 mycroft if (fs->st_size <= 8 * 1048576) {
113 1.1 mycroft if ((p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
114 1.1 mycroft 0, from_fd, (off_t)0)) == (char *)-1) {
115 1.1 mycroft warn("%s", entp->fts_path);
116 1.1 mycroft rval = 1;
117 1.1 mycroft } else {
118 1.1 mycroft if (write(to_fd, p, fs->st_size) != fs->st_size) {
119 1.1 mycroft warn("%s", to.p_path);
120 1.1 mycroft rval = 1;
121 1.1 mycroft }
122 1.1 mycroft /* Some systems don't unmap on close(2). */
123 1.1 mycroft if (munmap(p, fs->st_size) < 0) {
124 1.1 mycroft warn("%s", entp->fts_path);
125 1.1 mycroft rval = 1;
126 1.1 mycroft }
127 1.1 mycroft }
128 1.1 mycroft } else
129 1.1 mycroft #endif
130 1.1 mycroft {
131 1.1 mycroft while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
132 1.1 mycroft wcount = write(to_fd, buf, rcount);
133 1.1 mycroft if (rcount != wcount || wcount == -1) {
134 1.1 mycroft warn("%s", to.p_path);
135 1.1 mycroft rval = 1;
136 1.1 mycroft break;
137 1.1 mycroft }
138 1.1 mycroft }
139 1.1 mycroft if (rcount < 0) {
140 1.1 mycroft warn("%s", entp->fts_path);
141 1.1 mycroft rval = 1;
142 1.1 mycroft }
143 1.1 mycroft }
144 1.1 mycroft
145 1.1 mycroft /* If the copy went bad, lose the file. */
146 1.1 mycroft if (rval == 1) {
147 1.1 mycroft (void)unlink(to.p_path);
148 1.1 mycroft (void)close(from_fd);
149 1.1 mycroft (void)close(to_fd);
150 1.1 mycroft return (1);
151 1.1 mycroft }
152 1.1 mycroft
153 1.1 mycroft if (pflag && setfile(fs, to_fd))
154 1.1 mycroft rval = 1;
155 1.1 mycroft /*
156 1.1 mycroft * If the source was setuid or setgid, lose the bits unless the
157 1.1 mycroft * copy is owned by the same user and group.
158 1.1 mycroft */
159 1.1 mycroft #define RETAINBITS \
160 1.1 mycroft (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
161 1.1 mycroft else if (fs->st_mode & (S_ISUID | S_ISGID) && fs->st_uid == myuid)
162 1.1 mycroft if (fstat(to_fd, &to_stat)) {
163 1.1 mycroft warn("%s", to.p_path);
164 1.1 mycroft rval = 1;
165 1.1 mycroft } else if (fs->st_gid == to_stat.st_gid &&
166 1.1 mycroft fchmod(to_fd, fs->st_mode & RETAINBITS & ~myumask)) {
167 1.1 mycroft warn("%s", to.p_path);
168 1.1 mycroft rval = 1;
169 1.1 mycroft }
170 1.1 mycroft (void)close(from_fd);
171 1.1 mycroft if (close(to_fd)) {
172 1.1 mycroft warn("%s", to.p_path);
173 1.1 mycroft rval = 1;
174 1.1 mycroft }
175 1.1 mycroft return (rval);
176 1.1 mycroft }
177 1.1 mycroft
178 1.1 mycroft int
179 1.1 mycroft copy_link(p, exists)
180 1.1 mycroft FTSENT *p;
181 1.1 mycroft int exists;
182 1.1 mycroft {
183 1.1 mycroft int len;
184 1.1 mycroft char link[MAXPATHLEN];
185 1.1 mycroft
186 1.1 mycroft if ((len = readlink(p->fts_path, link, sizeof(link))) == -1) {
187 1.1 mycroft warn("readlink: %s", p->fts_path);
188 1.1 mycroft return (1);
189 1.1 mycroft }
190 1.1 mycroft link[len] = '\0';
191 1.1 mycroft if (exists && unlink(to.p_path)) {
192 1.1 mycroft warn("unlink: %s", to.p_path);
193 1.1 mycroft return (1);
194 1.1 mycroft }
195 1.1 mycroft if (symlink(link, to.p_path)) {
196 1.1 mycroft warn("symlink: %s", link);
197 1.1 mycroft return (1);
198 1.1 mycroft }
199 1.1 mycroft return (0);
200 1.1 mycroft }
201 1.1 mycroft
202 1.1 mycroft int
203 1.1 mycroft copy_fifo(from_stat, exists)
204 1.1 mycroft struct stat *from_stat;
205 1.1 mycroft int exists;
206 1.1 mycroft {
207 1.1 mycroft if (exists && unlink(to.p_path)) {
208 1.1 mycroft warn("unlink: %s", to.p_path);
209 1.1 mycroft return (1);
210 1.1 mycroft }
211 1.1 mycroft if (mkfifo(to.p_path, from_stat->st_mode)) {
212 1.1 mycroft warn("mkfifo: %s", to.p_path);
213 1.1 mycroft return (1);
214 1.1 mycroft }
215 1.1 mycroft return (pflag ? setfile(from_stat, 0) : 0);
216 1.1 mycroft }
217 1.1 mycroft
218 1.1 mycroft int
219 1.1 mycroft copy_special(from_stat, exists)
220 1.1 mycroft struct stat *from_stat;
221 1.1 mycroft int exists;
222 1.1 mycroft {
223 1.1 mycroft if (exists && unlink(to.p_path)) {
224 1.1 mycroft warn("unlink: %s", to.p_path);
225 1.1 mycroft return (1);
226 1.1 mycroft }
227 1.1 mycroft if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
228 1.1 mycroft warn("mknod: %s", to.p_path);
229 1.1 mycroft return (1);
230 1.1 mycroft }
231 1.1 mycroft return (pflag ? setfile(from_stat, 0) : 0);
232 1.1 mycroft }
233 1.1 mycroft
234 1.1 mycroft
235 1.1 mycroft int
236 1.1 mycroft setfile(fs, fd)
237 1.1 mycroft register struct stat *fs;
238 1.1 mycroft int fd;
239 1.1 mycroft {
240 1.1 mycroft static struct timeval tv[2];
241 1.1 mycroft int rval;
242 1.1 mycroft
243 1.1 mycroft rval = 0;
244 1.1 mycroft fs->st_mode &= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
245 1.1 mycroft
246 1.1 mycroft TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
247 1.1 mycroft TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
248 1.1 mycroft if (utimes(to.p_path, tv)) {
249 1.1 mycroft warn("utimes: %s", to.p_path);
250 1.1 mycroft rval = 1;
251 1.1 mycroft }
252 1.1 mycroft /*
253 1.1 mycroft * Changing the ownership probably won't succeed, unless we're root
254 1.1 mycroft * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
255 1.1 mycroft * the mode; current BSD behavior is to remove all setuid bits on
256 1.1 mycroft * chown. If chown fails, lose setuid/setgid bits.
257 1.1 mycroft */
258 1.1 mycroft if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
259 1.1 mycroft chown(to.p_path, fs->st_uid, fs->st_gid)) {
260 1.1 mycroft if (errno != EPERM) {
261 1.1 mycroft warn("chown: %s", to.p_path);
262 1.1 mycroft rval = 1;
263 1.1 mycroft }
264 1.1 mycroft fs->st_mode &= ~(S_ISUID | S_ISGID);
265 1.1 mycroft }
266 1.1 mycroft if (fd ? fchmod(fd, fs->st_mode) : chmod(to.p_path, fs->st_mode)) {
267 1.1 mycroft warn("chown: %s", to.p_path);
268 1.1 mycroft rval = 1;
269 1.1 mycroft }
270 1.1 mycroft
271 1.1 mycroft if (fd ?
272 1.1 mycroft fchflags(fd, fs->st_flags) : chflags(to.p_path, fs->st_flags)) {
273 1.1 mycroft warn("chflags: %s", to.p_path);
274 1.1 mycroft rval = 1;
275 1.1 mycroft }
276 1.1 mycroft return (rval);
277 1.1 mycroft }
278 1.1 mycroft
279 1.1 mycroft void
280 1.1 mycroft usage()
281 1.1 mycroft {
282 1.1 mycroft (void)fprintf(stderr, "%s\n%s\n",
283 1.1 mycroft "usage: cp [-R [-H | -L | -P] [-fip] src target",
284 1.1 mycroft " cp [-R [-H | -L | -P] [-fip] src1 ... srcN directory");
285 1.1 mycroft exit(1);
286 1.1 mycroft }
287