cp.c revision 1.4 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1988 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This code is derived from software contributed to Berkeley by
6 1.1 cgd * David Hitz of Auspex Systems Inc.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd #ifndef lint
38 1.1 cgd char copyright[] =
39 1.1 cgd "@(#) Copyright (c) 1988 The Regents of the University of California.\n\
40 1.1 cgd All rights reserved.\n";
41 1.1 cgd #endif /* not lint */
42 1.1 cgd
43 1.1 cgd #ifndef lint
44 1.4 mycroft /*static char sccsid[] = "from: @(#)cp.c 5.24 (Berkeley) 5/6/91";*/
45 1.4 mycroft static char rcsid[] = "$Id: cp.c,v 1.4 1993/08/01 19:01:02 mycroft Exp $";
46 1.1 cgd #endif /* not lint */
47 1.1 cgd
48 1.1 cgd /*
49 1.1 cgd * cp copies source files to target files.
50 1.1 cgd *
51 1.1 cgd * The global PATH_T structures "to" and "from" always contain paths to the
52 1.1 cgd * current source and target files, respectively. Since cp does not change
53 1.1 cgd * directories, these paths can be either absolute or dot-realative.
54 1.1 cgd *
55 1.1 cgd * The basic algorithm is to initialize "to" and "from", and then call the
56 1.1 cgd * recursive copy() function to do the actual work. If "from" is a file,
57 1.1 cgd * copy copies the data. If "from" is a directory, copy creates the
58 1.1 cgd * corresponding "to" directory, and calls itself recursively on all of
59 1.1 cgd * the entries in the "from" directory.
60 1.1 cgd */
61 1.1 cgd
62 1.1 cgd #include <sys/param.h>
63 1.1 cgd #include <sys/stat.h>
64 1.1 cgd #include <sys/time.h>
65 1.1 cgd #include <dirent.h>
66 1.1 cgd #include <fcntl.h>
67 1.1 cgd #include <errno.h>
68 1.1 cgd #include <unistd.h>
69 1.1 cgd #include <stdio.h>
70 1.1 cgd #include <stdlib.h>
71 1.1 cgd #include <string.h>
72 1.1 cgd #include "cp.h"
73 1.1 cgd
74 1.1 cgd PATH_T from = { from.p_path, "" };
75 1.1 cgd PATH_T to = { to.p_path, "" };
76 1.1 cgd
77 1.1 cgd uid_t myuid;
78 1.1 cgd int exit_val, myumask;
79 1.1 cgd int iflag, pflag, orflag, rflag;
80 1.1 cgd int (*statfcn)();
81 1.1 cgd char *buf, *progname;
82 1.1 cgd
83 1.1 cgd main(argc, argv)
84 1.1 cgd int argc;
85 1.1 cgd char **argv;
86 1.1 cgd {
87 1.1 cgd extern int optind;
88 1.1 cgd struct stat to_stat;
89 1.1 cgd register int c, r;
90 1.1 cgd int symfollow, lstat(), stat();
91 1.1 cgd char *old_to, *p;
92 1.1 cgd
93 1.1 cgd /*
94 1.1 cgd * The utility cp(1) is used by mv(1) -- except for usage statements,
95 1.1 cgd * print the "called as" program name.
96 1.1 cgd */
97 1.1 cgd progname = (p = rindex(*argv,'/')) ? ++p : *argv;
98 1.1 cgd
99 1.1 cgd symfollow = 0;
100 1.1 cgd while ((c = getopt(argc, argv, "Rfhipr")) != EOF) {
101 1.1 cgd switch ((char)c) {
102 1.1 cgd case 'f':
103 1.1 cgd iflag = 0;
104 1.1 cgd break;
105 1.1 cgd case 'h':
106 1.1 cgd symfollow = 1;
107 1.1 cgd break;
108 1.1 cgd case 'i':
109 1.1 cgd iflag = isatty(fileno(stdin));
110 1.1 cgd break;
111 1.1 cgd case 'p':
112 1.1 cgd pflag = 1;
113 1.1 cgd break;
114 1.1 cgd case 'R':
115 1.1 cgd rflag = 1;
116 1.1 cgd break;
117 1.1 cgd case 'r':
118 1.1 cgd orflag = 1;
119 1.1 cgd break;
120 1.1 cgd case '?':
121 1.1 cgd default:
122 1.1 cgd usage();
123 1.1 cgd break;
124 1.1 cgd }
125 1.1 cgd }
126 1.1 cgd argc -= optind;
127 1.1 cgd argv += optind;
128 1.1 cgd
129 1.1 cgd if (argc < 2)
130 1.1 cgd usage();
131 1.1 cgd
132 1.1 cgd if (rflag && orflag) {
133 1.1 cgd (void)fprintf(stderr,
134 1.1 cgd "cp: the -R and -r options are mutually exclusive.\n");
135 1.1 cgd exit(1);
136 1.1 cgd }
137 1.1 cgd
138 1.1 cgd buf = (char *)malloc(MAXBSIZE);
139 1.1 cgd if (!buf) {
140 1.1 cgd (void)fprintf(stderr, "%s: out of space.\n", progname);
141 1.1 cgd exit(1);
142 1.1 cgd }
143 1.1 cgd
144 1.1 cgd myuid = getuid();
145 1.1 cgd
146 1.1 cgd /* copy the umask for explicit mode setting */
147 1.1 cgd myumask = umask(0);
148 1.1 cgd (void)umask(myumask);
149 1.1 cgd
150 1.1 cgd /* consume last argument first. */
151 1.1 cgd if (!path_set(&to, argv[--argc]))
152 1.1 cgd exit(1);
153 1.1 cgd
154 1.1 cgd statfcn = symfollow || !rflag ? stat : lstat;
155 1.1 cgd
156 1.1 cgd /*
157 1.1 cgd * Cp has two distinct cases:
158 1.1 cgd *
159 1.1 cgd * % cp [-rip] source target
160 1.1 cgd * % cp [-rip] source1 ... directory
161 1.1 cgd *
162 1.1 cgd * In both cases, source can be either a file or a directory.
163 1.1 cgd *
164 1.1 cgd * In (1), the target becomes a copy of the source. That is, if the
165 1.1 cgd * source is a file, the target will be a file, and likewise for
166 1.1 cgd * directories.
167 1.1 cgd *
168 1.1 cgd * In (2), the real target is not directory, but "directory/source".
169 1.1 cgd */
170 1.1 cgd
171 1.1 cgd r = stat(to.p_path, &to_stat);
172 1.1 cgd if (r == -1 && errno != ENOENT) {
173 1.1 cgd error(to.p_path);
174 1.1 cgd exit(1);
175 1.1 cgd }
176 1.1 cgd if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
177 1.1 cgd /*
178 1.1 cgd * Case (1). Target is not a directory.
179 1.1 cgd */
180 1.1 cgd if (argc > 1) {
181 1.1 cgd usage();
182 1.1 cgd exit(1);
183 1.1 cgd }
184 1.1 cgd if (!path_set(&from, *argv))
185 1.1 cgd exit(1);
186 1.1 cgd copy();
187 1.1 cgd }
188 1.1 cgd else {
189 1.1 cgd /*
190 1.1 cgd * Case (2). Target is a directory.
191 1.1 cgd */
192 1.1 cgd for (;; ++argv) {
193 1.1 cgd if (!path_set(&from, *argv)) {
194 1.1 cgd exit_val = 1;
195 1.1 cgd continue;
196 1.1 cgd }
197 1.1 cgd old_to = path_append(&to, path_basename(&from), -1);
198 1.1 cgd if (!old_to) {
199 1.1 cgd exit_val = 1;
200 1.1 cgd continue;
201 1.1 cgd }
202 1.1 cgd copy();
203 1.1 cgd if (!--argc)
204 1.1 cgd break;
205 1.1 cgd path_restore(&to, old_to);
206 1.1 cgd }
207 1.1 cgd }
208 1.1 cgd exit(exit_val);
209 1.1 cgd }
210 1.1 cgd
211 1.1 cgd /* copy file or directory at "from" to "to". */
212 1.1 cgd copy()
213 1.1 cgd {
214 1.1 cgd struct stat from_stat, to_stat;
215 1.1 cgd int dne, statval;
216 1.1 cgd
217 1.1 cgd statval = statfcn(from.p_path, &from_stat);
218 1.1 cgd if (statval == -1) {
219 1.1 cgd error(from.p_path);
220 1.1 cgd return;
221 1.1 cgd }
222 1.1 cgd
223 1.1 cgd /* not an error, but need to remember it happened */
224 1.1 cgd if (stat(to.p_path, &to_stat) == -1)
225 1.1 cgd dne = 1;
226 1.1 cgd else {
227 1.1 cgd if (to_stat.st_dev == from_stat.st_dev &&
228 1.1 cgd to_stat.st_ino == from_stat.st_ino) {
229 1.1 cgd (void)fprintf(stderr,
230 1.1 cgd "%s: %s and %s are identical (not copied).\n",
231 1.1 cgd progname, to.p_path, from.p_path);
232 1.1 cgd exit_val = 1;
233 1.1 cgd return;
234 1.1 cgd }
235 1.1 cgd dne = 0;
236 1.1 cgd }
237 1.1 cgd
238 1.1 cgd switch(from_stat.st_mode & S_IFMT) {
239 1.1 cgd case S_IFLNK:
240 1.1 cgd copy_link(!dne);
241 1.1 cgd return;
242 1.1 cgd case S_IFDIR:
243 1.1 cgd if (!rflag && !orflag) {
244 1.1 cgd (void)fprintf(stderr,
245 1.1 cgd "%s: %s is a directory (not copied).\n",
246 1.1 cgd progname, from.p_path);
247 1.1 cgd exit_val = 1;
248 1.1 cgd return;
249 1.1 cgd }
250 1.1 cgd if (dne) {
251 1.1 cgd /*
252 1.1 cgd * If the directory doesn't exist, create the new
253 1.1 cgd * one with the from file mode plus owner RWX bits,
254 1.1 cgd * modified by the umask. Trade-off between being
255 1.1 cgd * able to write the directory (if from directory is
256 1.1 cgd * 555) and not causing a permissions race. If the
257 1.1 cgd * umask blocks owner writes cp fails.
258 1.1 cgd */
259 1.1 cgd if (mkdir(to.p_path, from_stat.st_mode|S_IRWXU) < 0) {
260 1.1 cgd error(to.p_path);
261 1.1 cgd return;
262 1.1 cgd }
263 1.1 cgd }
264 1.1 cgd else if (!S_ISDIR(to_stat.st_mode) != S_IFDIR) {
265 1.1 cgd (void)fprintf(stderr, "%s: %s: not a directory.\n",
266 1.1 cgd progname, to.p_path);
267 1.1 cgd return;
268 1.1 cgd }
269 1.1 cgd copy_dir();
270 1.1 cgd /*
271 1.1 cgd * If not -p and directory didn't exist, set it to be the
272 1.1 cgd * same as the from directory, umodified by the umask;
273 1.1 cgd * arguably wrong, but it's been that way forever.
274 1.1 cgd */
275 1.1 cgd if (pflag)
276 1.1 cgd setfile(&from_stat, 0);
277 1.1 cgd else if (dne)
278 1.1 cgd (void)chmod(to.p_path, from_stat.st_mode);
279 1.1 cgd return;
280 1.1 cgd case S_IFCHR:
281 1.1 cgd case S_IFBLK:
282 1.1 cgd if (rflag) {
283 1.1 cgd copy_special(&from_stat, !dne);
284 1.1 cgd return;
285 1.1 cgd }
286 1.1 cgd break;
287 1.1 cgd case S_IFIFO:
288 1.1 cgd if (rflag) {
289 1.1 cgd copy_fifo(&from_stat, !dne);
290 1.1 cgd return;
291 1.1 cgd }
292 1.1 cgd break;
293 1.1 cgd }
294 1.1 cgd copy_file(&from_stat, dne);
295 1.1 cgd }
296 1.1 cgd
297 1.1 cgd copy_file(fs, dne)
298 1.1 cgd struct stat *fs;
299 1.1 cgd int dne;
300 1.1 cgd {
301 1.1 cgd register int from_fd, to_fd, rcount, wcount;
302 1.1 cgd struct stat to_stat;
303 1.1 cgd
304 1.1 cgd if ((from_fd = open(from.p_path, O_RDONLY, 0)) == -1) {
305 1.1 cgd error(from.p_path);
306 1.1 cgd return;
307 1.1 cgd }
308 1.1 cgd
309 1.1 cgd /*
310 1.1 cgd * If the file exists and we're interactive, verify with the user.
311 1.1 cgd * If the file DNE, set the mode to be the from file, minus setuid
312 1.1 cgd * bits, modified by the umask; arguably wrong, but it makes copying
313 1.1 cgd * executables work right and it's been that way forever. (The
314 1.1 cgd * other choice is 666 or'ed with the execute bits on the from file
315 1.1 cgd * modified by the umask.)
316 1.1 cgd */
317 1.1 cgd if (!dne) {
318 1.1 cgd if (iflag) {
319 1.1 cgd int checkch, ch;
320 1.1 cgd
321 1.1 cgd (void)fprintf(stderr, "overwrite %s? ", to.p_path);
322 1.1 cgd checkch = ch = getchar();
323 1.1 cgd while (ch != '\n' && ch != EOF)
324 1.1 cgd ch = getchar();
325 1.1 cgd if (checkch != 'y') {
326 1.1 cgd (void)close(from_fd);
327 1.1 cgd return;
328 1.1 cgd }
329 1.1 cgd }
330 1.1 cgd to_fd = open(to.p_path, O_WRONLY|O_TRUNC, 0);
331 1.1 cgd } else
332 1.1 cgd to_fd = open(to.p_path, O_WRONLY|O_CREAT|O_TRUNC,
333 1.1 cgd fs->st_mode & ~(S_ISUID|S_ISGID));
334 1.1 cgd
335 1.1 cgd if (to_fd == -1) {
336 1.1 cgd error(to.p_path);
337 1.1 cgd (void)close(from_fd);
338 1.1 cgd return;
339 1.1 cgd }
340 1.1 cgd
341 1.1 cgd while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
342 1.1 cgd wcount = write(to_fd, buf, rcount);
343 1.1 cgd if (rcount != wcount || wcount == -1) {
344 1.1 cgd error(to.p_path);
345 1.1 cgd break;
346 1.1 cgd }
347 1.1 cgd }
348 1.1 cgd if (rcount < 0)
349 1.1 cgd error(from.p_path);
350 1.1 cgd if (pflag)
351 1.1 cgd setfile(fs, to_fd);
352 1.1 cgd /*
353 1.1 cgd * If the source was setuid or setgid, lose the bits unless the
354 1.1 cgd * copy is owned by the same user and group.
355 1.1 cgd */
356 1.1 cgd else if (fs->st_mode & (S_ISUID|S_ISGID) && fs->st_uid == myuid)
357 1.1 cgd if (fstat(to_fd, &to_stat))
358 1.1 cgd error(to.p_path);
359 1.1 cgd #define RETAINBITS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)
360 1.1 cgd else if (fs->st_gid == to_stat.st_gid && fchmod(to_fd,
361 1.1 cgd fs->st_mode & RETAINBITS & ~myumask))
362 1.1 cgd error(to.p_path);
363 1.1 cgd (void)close(from_fd);
364 1.1 cgd if (close(to_fd))
365 1.1 cgd error(to.p_path);
366 1.1 cgd }
367 1.1 cgd
368 1.1 cgd copy_dir()
369 1.1 cgd {
370 1.1 cgd struct stat from_stat;
371 1.1 cgd struct dirent *dp, **dir_list;
372 1.1 cgd register int dir_cnt, i;
373 1.1 cgd char *old_from, *old_to;
374 1.1 cgd
375 1.1 cgd dir_cnt = scandir(from.p_path, &dir_list, NULL, NULL);
376 1.1 cgd if (dir_cnt == -1) {
377 1.1 cgd (void)fprintf(stderr, "%s: can't read directory %s.\n",
378 1.1 cgd progname, from.p_path);
379 1.1 cgd exit_val = 1;
380 1.1 cgd }
381 1.1 cgd
382 1.1 cgd /*
383 1.1 cgd * Instead of handling directory entries in the order they appear
384 1.1 cgd * on disk, do non-directory files before directory files.
385 1.1 cgd * There are two reasons to do directories last. The first is
386 1.1 cgd * efficiency. Files tend to be in the same cylinder group as
387 1.1 cgd * their parent, whereas directories tend not to be. Copying files
388 1.1 cgd * all at once reduces seeking. Second, deeply nested tree's
389 1.1 cgd * could use up all the file descriptors if we didn't close one
390 1.1 cgd * directory before recursivly starting on the next.
391 1.1 cgd */
392 1.1 cgd /* copy files */
393 1.1 cgd for (i = 0; i < dir_cnt; ++i) {
394 1.1 cgd dp = dir_list[i];
395 1.1 cgd if (dp->d_namlen <= 2 && dp->d_name[0] == '.'
396 1.1 cgd && (dp->d_name[1] == NULL || dp->d_name[1] == '.'))
397 1.1 cgd goto done;
398 1.1 cgd old_from = path_append(&from, dp->d_name, (int)dp->d_namlen);
399 1.1 cgd if (!old_from) {
400 1.1 cgd exit_val = 1;
401 1.1 cgd goto done;
402 1.1 cgd }
403 1.1 cgd
404 1.1 cgd if (statfcn(from.p_path, &from_stat) < 0) {
405 1.1 cgd error(dp->d_name);
406 1.1 cgd path_restore(&from, old_from);
407 1.1 cgd goto done;
408 1.1 cgd }
409 1.1 cgd if (S_ISDIR(from_stat.st_mode)) {
410 1.1 cgd path_restore(&from, old_from);
411 1.1 cgd continue;
412 1.1 cgd }
413 1.1 cgd old_to = path_append(&to, dp->d_name, (int)dp->d_namlen);
414 1.1 cgd if (old_to) {
415 1.1 cgd copy();
416 1.1 cgd path_restore(&to, old_to);
417 1.1 cgd } else
418 1.1 cgd exit_val = 1;
419 1.1 cgd path_restore(&from, old_from);
420 1.1 cgd done: dir_list[i] = NULL;
421 1.1 cgd (void)free((void *)dp);
422 1.1 cgd }
423 1.1 cgd
424 1.1 cgd /* copy directories */
425 1.1 cgd for (i = 0; i < dir_cnt; ++i) {
426 1.1 cgd dp = dir_list[i];
427 1.1 cgd if (!dp)
428 1.1 cgd continue;
429 1.1 cgd old_from = path_append(&from, dp->d_name, (int) dp->d_namlen);
430 1.1 cgd if (!old_from) {
431 1.1 cgd exit_val = 1;
432 1.1 cgd (void)free((void *)dp);
433 1.1 cgd continue;
434 1.1 cgd }
435 1.1 cgd old_to = path_append(&to, dp->d_name, (int) dp->d_namlen);
436 1.1 cgd if (!old_to) {
437 1.1 cgd exit_val = 1;
438 1.1 cgd (void)free((void *)dp);
439 1.1 cgd path_restore(&from, old_from);
440 1.1 cgd continue;
441 1.1 cgd }
442 1.1 cgd copy();
443 1.1 cgd free((void *)dp);
444 1.1 cgd path_restore(&from, old_from);
445 1.1 cgd path_restore(&to, old_to);
446 1.1 cgd }
447 1.1 cgd free((void *)dir_list);
448 1.1 cgd }
449 1.1 cgd
450 1.1 cgd copy_link(exists)
451 1.1 cgd int exists;
452 1.1 cgd {
453 1.1 cgd int len;
454 1.1 cgd char link[MAXPATHLEN];
455 1.1 cgd
456 1.1 cgd if ((len = readlink(from.p_path, link, sizeof(link))) == -1) {
457 1.1 cgd error(from.p_path);
458 1.1 cgd return;
459 1.1 cgd }
460 1.1 cgd link[len] = '\0';
461 1.1 cgd if (exists && unlink(to.p_path)) {
462 1.1 cgd error(to.p_path);
463 1.1 cgd return;
464 1.1 cgd }
465 1.1 cgd if (symlink(link, to.p_path)) {
466 1.1 cgd error(link);
467 1.1 cgd return;
468 1.1 cgd }
469 1.1 cgd }
470 1.1 cgd
471 1.1 cgd copy_fifo(from_stat, exists)
472 1.1 cgd struct stat *from_stat;
473 1.1 cgd int exists;
474 1.1 cgd {
475 1.1 cgd if (exists && unlink(to.p_path)) {
476 1.1 cgd error(to.p_path);
477 1.1 cgd return;
478 1.1 cgd }
479 1.1 cgd if (mkfifo(to.p_path, from_stat->st_mode)) {
480 1.1 cgd error(to.p_path);
481 1.1 cgd return;
482 1.1 cgd }
483 1.1 cgd if (pflag)
484 1.1 cgd setfile(from_stat, 0);
485 1.1 cgd }
486 1.1 cgd
487 1.1 cgd copy_special(from_stat, exists)
488 1.1 cgd struct stat *from_stat;
489 1.1 cgd int exists;
490 1.1 cgd {
491 1.1 cgd if (exists && unlink(to.p_path)) {
492 1.1 cgd error(to.p_path);
493 1.1 cgd return;
494 1.1 cgd }
495 1.1 cgd if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
496 1.1 cgd error(to.p_path);
497 1.1 cgd return;
498 1.1 cgd }
499 1.1 cgd if (pflag)
500 1.1 cgd setfile(from_stat, 0);
501 1.1 cgd }
502 1.1 cgd
503 1.1 cgd setfile(fs, fd)
504 1.1 cgd register struct stat *fs;
505 1.1 cgd int fd;
506 1.1 cgd {
507 1.1 cgd static struct timeval tv[2];
508 1.1 cgd char path[100];
509 1.1 cgd
510 1.1 cgd fs->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
511 1.1 cgd
512 1.1 cgd tv[0].tv_sec = fs->st_atime;
513 1.1 cgd tv[1].tv_sec = fs->st_mtime;
514 1.1 cgd if (utimes(to.p_path, tv)) {
515 1.1 cgd (void)snprintf(path, sizeof(path), "utimes: %s", to.p_path);
516 1.1 cgd error(path);
517 1.1 cgd }
518 1.1 cgd /*
519 1.1 cgd * Changing the ownership probably won't succeed, unless we're root
520 1.1 cgd * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
521 1.1 cgd * the mode; current BSD behavior is to remove all setuid bits on
522 1.1 cgd * chown. If chown fails, lose setuid/setgid bits.
523 1.1 cgd */
524 1.1 cgd if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
525 1.1 cgd chown(to.p_path, fs->st_uid, fs->st_gid)) {
526 1.1 cgd if (errno != EPERM) {
527 1.1 cgd (void)snprintf(path, sizeof(path),
528 1.1 cgd "chown: %s", to.p_path);
529 1.1 cgd error(path);
530 1.1 cgd }
531 1.1 cgd fs->st_mode &= ~(S_ISUID|S_ISGID);
532 1.1 cgd }
533 1.1 cgd if (fd ? fchmod(fd, fs->st_mode) : chmod(to.p_path, fs->st_mode)) {
534 1.1 cgd (void)snprintf(path, sizeof(path), "chown: %s", to.p_path);
535 1.1 cgd error(path);
536 1.1 cgd }
537 1.1 cgd }
538 1.1 cgd
539 1.1 cgd error(s)
540 1.1 cgd char *s;
541 1.1 cgd {
542 1.1 cgd exit_val = 1;
543 1.1 cgd (void)fprintf(stderr, "%s: %s: %s\n", progname, s, strerror(errno));
544 1.1 cgd }
545 1.1 cgd
546 1.1 cgd usage()
547 1.1 cgd {
548 1.1 cgd (void)fprintf(stderr,
549 1.1 cgd "usage: cp [-Rfhip] src target;\n or: cp [-Rfhip] src1 ... srcN directory\n");
550 1.1 cgd exit(1);
551 1.1 cgd }
552