new compat file
[unix-history] / usr / src / bin / cp / cp.c
CommitLineData
1f978f4c 1/*
4d2ae24a
KB
2 * Copyright (c) 1988 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * David Hitz of Auspex Systems Inc.
7 *
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the University of California, Berkeley. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 */
20
21#ifndef lint
22char copyright[] =
23"@(#) Copyright (c) 1988 The Regents of the University of California.\n\
24 All rights reserved.\n";
25#endif /* not lint */
26
27#ifndef lint
14dc7750 28static char sccsid[] = "@(#)cp.c 5.9 (Berkeley) %G%";
4d2ae24a
KB
29#endif /* not lint */
30
31/*
32 * cp copies source files to target files.
ae5e5236
KB
33 *
34 * The global path_t structures "to" and "from" always contain paths to the
4d2ae24a
KB
35 * current source and target files, respectively. Since cp does not change
36 * directories, these paths can be either absolute or dot-realative.
ae5e5236
KB
37 *
38 * The basic algorithm is to initialize "to" and "from", and then call the
39 * recursive copy() function to do the actual work. If "from" is a file,
40 * copy copies the data. If "from" is a directory, copy creates the
41 * corresponding "to" directory, and calls itself recursively on all of
42 * the entries in the "from" directory.
1f978f4c
KM
43 */
44
4d2ae24a 45#include <sys/param.h>
84592a94 46#include <sys/stat.h>
ae5e5236 47#include <sys/file.h>
7afd0a98 48#include <sys/dir.h>
0908a03a 49#include <sys/time.h>
5f89032b 50
4d2ae24a
KB
51#include <stdio.h>
52#include <errno.h>
53#include <strings.h>
ae5e5236
KB
54
55typedef struct {
a783899b
KB
56 char *p_path; /* pointer to the start of a path. */
57 char *p_end; /* pointer to NULL at end of path. */
4d2ae24a 58} path_t;
ae5e5236 59
a783899b
KB
60#define type(st) ((st).st_mode&S_IFMT)
61
4d2ae24a
KB
62char *path_append(), *path_basename();
63void path_restore();
ae5e5236 64
a68fe064 65int exit_val;
4d2ae24a 66int interactive_flag, preserve_flag, recursive_flag;
a68fe064 67int (*statfcn)(); /* stat function to use */
4d2ae24a 68char *buf; /* I/O; malloc for best alignment. */
24e5a125
KB
69char from_buf[MAXPATHLEN + 1], /* source path buffer */
70 to_buf[MAXPATHLEN + 1]; /* target path buffer */
4d2ae24a
KB
71path_t from = {from_buf, from_buf};
72path_t to = {to_buf, to_buf};
84592a94
BJ
73
74main(argc, argv)
4d2ae24a
KB
75 int argc;
76 char **argv;
84592a94 77{
4d2ae24a
KB
78 extern int optind, errno;
79 struct stat to_stat;
80 register int c, r;
a68fe064 81 int force_flag, symfollow, lstat(), stat();
4d2ae24a
KB
82 char *old_to, *malloc();
83
a68fe064 84 force_flag = symfollow = 0;
a783899b
KB
85 while ((c = getopt(argc, argv, "Rfhipr")) != EOF) {
86 switch ((char)c) {
87 case 'f':
88 force_flag = 1;
89 break;
0493752e
KB
90 case 'h':
91 symfollow = 1;
92 break;
4d2ae24a
KB
93 case 'i':
94 interactive_flag = isatty(fileno(stdin));
95 break;
96 case 'p':
97 preserve_flag = 1;
98 (void)umask(0);
99 break;
100 case 'r':
101 case 'R':
102 recursive_flag = 1;
103 break;
104 case '?':
105 default:
106 usage();
107 break;
108 }
84592a94 109 }
0493752e
KB
110 argc -= optind;
111 argv += optind;
ae5e5236 112
4d2ae24a
KB
113 if (argc < 2)
114 usage();
ae5e5236 115
a783899b
KB
116 if (force_flag)
117 interactive_flag = 0;
118
4d2ae24a
KB
119 buf = (char *)malloc(MAXBSIZE);
120 if (!buf) {
0493752e 121 (void)fprintf(stderr, "cp: out of space.\n");
4d2ae24a 122 exit(1);
ae5e5236
KB
123 }
124
24e5a125 125 /* consume last argument first. */
4d2ae24a
KB
126 if (!path_set(&to, argv[--argc]))
127 exit(exit_val);
ae5e5236 128
a68fe064
KB
129 statfcn = symfollow || !recursive_flag ? stat : lstat;
130
ae5e5236 131 /*
4d2ae24a
KB
132 * Cp has two distinct cases:
133 *
134 * Case (1) $ cp [-rip] source target
135 *
136 * Case (2) $ cp [-rip] source1 ... directory
137 *
138 * In both cases, source can be either a file or a directory.
139 *
140 * In (1), the target becomes a copy of the source. That is, if the
141 * source is a file, the target will be a file, and likewise for
142 * directories.
143 *
144 * In (2), the real target is not directory, but "directory/source".
ae5e5236 145 */
ae5e5236 146
4d2ae24a
KB
147 r = stat(to.p_path, &to_stat);
148 if (r == -1 && errno != ENOENT) {
24e5a125 149 error(to.p_path);
4d2ae24a
KB
150 exit(1);
151 }
a783899b 152 if (r == -1 || type(to_stat) != S_IFDIR) {
4d2ae24a
KB
153 /*
154 * Case (1). Target is not a directory.
155 */
156 if (argc > 1) {
157 usage();
158 exit(1);
159 }
160 if (!path_set(&from, *argv))
161 exit(exit_val);
162 copy();
163 }
164 else {
165 /*
166 * Case (2). Target is a directory.
167 */
24e5a125 168 for (;; ++argv) {
4d2ae24a
KB
169 if (!path_set(&from, *argv))
170 continue;
171 old_to = path_append(&to, path_basename(&from), -1);
172 if (!old_to)
173 continue;
174 copy();
24e5a125
KB
175 if (!--argc)
176 break;
4d2ae24a
KB
177 path_restore(&to, old_to);
178 }
ae5e5236 179 }
4d2ae24a 180 exit(exit_val);
84592a94
BJ
181}
182
a783899b 183/* copy file or directory at "from" to "to". */
ae5e5236 184copy()
84592a94 185{
4d2ae24a 186 struct stat from_stat, to_stat;
24e5a125 187 int dne, statval;
ae5e5236 188
a68fe064 189 statval = statfcn(from.p_path, &from_stat);
0493752e 190 if (statval == -1) {
24e5a125 191 error(from.p_path);
4d2ae24a
KB
192 return;
193 }
0493752e
KB
194
195 /* not an error, but need to remember it happened */
4d2ae24a 196 if (stat(to.p_path, &to_stat) == -1)
24e5a125
KB
197 dne = 1;
198 else {
199 if (to_stat.st_dev == from_stat.st_dev &&
200 to_stat.st_ino == from_stat.st_ino) {
201 (void)fprintf(stderr,
202 "cp: %s and %s are identical (not copied).\n",
203 to.p_path, from.p_path);
204 exit_val = 1;
205 return;
206 }
207 dne = 0;
84592a94 208 }
ae5e5236 209
a783899b
KB
210 switch(type(from_stat)) {
211 case S_IFLNK:
24e5a125 212 copy_link(!dne);
0493752e 213 return;
a783899b 214 case S_IFDIR:
4d2ae24a
KB
215 if (!recursive_flag) {
216 (void)fprintf(stderr,
a783899b
KB
217 "cp: %s is a directory (not copied).\n",
218 from.p_path);
4d2ae24a
KB
219 exit_val = 1;
220 return;
221 }
24e5a125
KB
222 if (dne) {
223 /*
224 * If the directory doesn't exist, create the new
225 * one with the from file mode plus owner RWX bits,
226 * modified by the umask. Trade-off between being
227 * able to write the directory (if from directory is
228 * 555) and not causing a permissions race. If the
229 * umask blocks owner writes cp fails.
230 */
231 if (mkdir(to.p_path, from_stat.st_mode|S_IRWXU) < 0) {
232 error(to.p_path);
4d2ae24a
KB
233 return;
234 }
4d2ae24a 235 }
a783899b
KB
236 else if (type(to_stat) != S_IFDIR) {
237 (void)fprintf(stderr, "cp: %s: not a directory.\n",
238 to.p_path);
4d2ae24a
KB
239 return;
240 }
241 copy_dir();
24e5a125 242 /*
9ac69c84
KB
243 * If not -p and directory didn't exist, set it to be the
244 * same as the from directory, umodified by the umask;
245 * arguably wrong, but it's been that way forever.
24e5a125 246 */
9ac69c84
KB
247 if (preserve_flag)
248 setfile(&from_stat, 0);
249 else if (dne)
24e5a125 250 (void)chmod(to.p_path, from_stat.st_mode);
a783899b
KB
251 break;
252 case S_IFCHR:
253 case S_IFBLK:
24e5a125
KB
254 /*
255 * if recursive flag on, try and create the special device
256 * otherwise copy the contents.
257 */
a783899b
KB
258 if (recursive_flag) {
259 copy_special(&from_stat, &to_stat);
260 if (preserve_flag)
24e5a125 261 setfile(&from_stat, 0);
a783899b
KB
262 return;
263 }
264 /* FALLTHROUGH */
265 default:
24e5a125 266 copy_file(&from_stat);
5f89032b 267 }
ae5e5236
KB
268}
269
24e5a125
KB
270copy_file(fs)
271 struct stat *fs;
ae5e5236 272{
a783899b 273 register int from_fd, to_fd, rcount, wcount;
4d2ae24a 274
24e5a125
KB
275 if ((from_fd = open(from.p_path, O_RDONLY, 0)) == -1) {
276 error(from.p_path);
277 return;
4d2ae24a
KB
278 }
279
280 /*
24e5a125
KB
281 * In the interactive case, use O_EXCL to notice existing files.
282 * If the file exists, verify with the user.
283 *
284 * If the file DNE, create it with the mode of the from file modified
285 * by the umask; arguably wrong but it makes copying executables work
286 * right and it's been that way forever. The other choice is 666
287 * or'ed with the execute bits on the from file modified by the umask.
4d2ae24a
KB
288 */
289 to_fd = open(to.p_path,
0493752e 290 (interactive_flag ? O_EXCL : 0) | O_WRONLY | O_CREAT | O_TRUNC,
24e5a125 291 fs->st_mode);
ae5e5236 292
4d2ae24a 293 if (to_fd == -1 && errno == EEXIST && interactive_flag) {
03137eae
KB
294 int checkch, ch;
295
296 (void)fprintf(stderr, "overwrite %s? ", to.p_path);
297 checkch = ch = getchar();
298 while (ch != '\n' && ch != EOF)
299 ch = getchar();
300 if (checkch != 'y')
24e5a125 301 return;
03137eae 302 /* try again. */
24e5a125
KB
303 to_fd = open(to.p_path, O_WRONLY | O_CREAT | O_TRUNC,
304 fs->st_mode);
5f89032b 305 }
ae5e5236 306
4d2ae24a 307 if (to_fd == -1) {
24e5a125 308 error(to.p_path);
4d2ae24a 309 (void)close(from_fd);
24e5a125 310 return;
ae5e5236
KB
311 }
312
4d2ae24a
KB
313 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
314 wcount = write(to_fd, buf, rcount);
315 if (rcount != wcount || wcount == -1) {
24e5a125 316 error(to.p_path);
4d2ae24a
KB
317 break;
318 }
ae5e5236 319 }
24e5a125
KB
320 if (rcount < 0)
321 error(from.p_path);
322 if (preserve_flag)
323 setfile(fs, to_fd);
4d2ae24a
KB
324 (void)close(from_fd);
325 (void)close(to_fd);
4d2ae24a 326}
ae5e5236 327
4d2ae24a
KB
328copy_dir()
329{
330 struct stat from_stat;
4d2ae24a 331 struct direct *dp, **dir_list;
24e5a125
KB
332 register int dir_cnt, i;
333 char *old_from, *old_to;
4d2ae24a
KB
334
335 dir_cnt = scandir(from.p_path, &dir_list, NULL, NULL);
336 if (dir_cnt == -1) {
03137eae 337 (void)fprintf(stderr, "cp: can't read directory %s.\n",
4d2ae24a
KB
338 from.p_path);
339 exit_val = 1;
ae5e5236
KB
340 }
341
24e5a125
KB
342 /*
343 * Instead of handling directory entries in the order they appear
344 * on disk, do non-directory files before directory files.
345 * There are two reasons to do directories last. The first is
346 * efficiency. Files tend to be in the same cylinder group as
347 * their parent, whereas directories tend not to be. Copying files
348 * all at once reduces seeking. Second, deeply nested tree's
349 * could use up all the file descriptors if we didn't close one
350 * directory before recursivly starting on the next.
351 */
352 /* copy files */
4d2ae24a
KB
353 for (i = 0; i < dir_cnt; ++i) {
354 dp = dir_list[i];
355 if (dp->d_namlen <= 2 && dp->d_name[0] == '.'
24e5a125
KB
356 && (dp->d_name[1] == NULL || dp->d_name[1] == '.'))
357 goto done;
4d2ae24a 358 old_from = path_append(&from, dp->d_name, (int)dp->d_namlen);
24e5a125
KB
359 if (!old_from)
360 goto done;
4d2ae24a 361
a68fe064 362 if (statfcn(from.p_path, &from_stat) < 0) {
24e5a125
KB
363 error(dp->d_name);
364 path_restore(&from, old_from);
365 goto done;
366 }
367 if (type(from_stat) == S_IFDIR) {
4d2ae24a
KB
368 path_restore(&from, old_from);
369 continue;
370 }
24e5a125
KB
371 old_to = path_append(&to, dp->d_name, (int)dp->d_namlen);
372 if (old_to) {
4d2ae24a
KB
373 copy();
374 path_restore(&to, old_to);
4d2ae24a
KB
375 }
376 path_restore(&from, old_from);
24e5a125
KB
377done: dir_list[i] = NULL;
378 (void)free((char *)dp);
5f89032b 379 }
ae5e5236 380
24e5a125 381 /* copy directories */
4d2ae24a
KB
382 for (i = 0; i < dir_cnt; ++i) {
383 dp = dir_list[i];
384 if (!dp)
385 continue;
386 old_from = path_append(&from, dp->d_name, (int) dp->d_namlen);
387 if (!old_from) {
388 (void)free((char *)dp);
389 continue;
390 }
391 old_to = path_append(&to, dp->d_name, (int) dp->d_namlen);
392 if (!old_to) {
393 (void)free((char *)dp);
394 path_restore(&from, old_from);
395 continue;
396 }
397 copy();
398 free((char *)dp);
399 path_restore(&from, old_from);
400 path_restore(&to, old_to);
5f89032b 401 }
4d2ae24a 402 free((char *)dir_list);
5f89032b 403}
1a4b831f 404
0493752e
KB
405copy_link(exists)
406 int exists;
407{
14dc7750 408 int len;
0493752e
KB
409 char link[MAXPATHLEN];
410
14dc7750 411 if ((len = readlink(from.p_path, link, sizeof(link))) == -1) {
24e5a125 412 error(from.p_path);
0493752e
KB
413 return;
414 }
14dc7750 415 link[len] = '\0';
0493752e 416 if (exists && unlink(to.p_path)) {
24e5a125 417 error(to.p_path);
0493752e
KB
418 return;
419 }
420 if (symlink(link, to.p_path)) {
24e5a125 421 error(link);
0493752e
KB
422 return;
423 }
424}
425
a783899b
KB
426copy_special(from_stat, to_stat)
427 struct stat *from_stat, *to_stat;
428{
429 if (to_stat->st_ino != -1 && unlink(to.p_path)) {
24e5a125 430 error(to.p_path);
a783899b
KB
431 return;
432 }
433 if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
24e5a125 434 error(to.p_path);
a783899b
KB
435 return;
436 }
437}
438
24e5a125
KB
439setfile(fs, fd)
440 register struct stat *fs;
441 int fd;
a783899b
KB
442{
443 static struct timeval tv[2];
24e5a125 444 static int dochown = 1;
a783899b 445
a783899b
KB
446 tv[0].tv_sec = fs->st_atime;
447 tv[1].tv_sec = fs->st_mtime;
448 if (utimes(to.p_path, tv))
24e5a125
KB
449 error(to.p_path);
450 /*
451 * Changing the ownership probably won't succeed, unless we're
452 * root or POSIX_CHOWN_RESTRICTED is not set. Try it last so
453 * everything else gets set first.
454 */
455 if (fd) {
456 if (fchmod(fd, fs->st_mode))
457 error(to.p_path);
458 if (dochown && fchown(fd, fs->st_uid, fs->st_gid) == -1)
459 if (errno == EPERM)
460 dochown = 0;
461 else
462 error(to.p_path);
463 } else {
464 if (chmod(to.p_path, fs->st_mode))
465 error(to.p_path);
466 if (dochown && chown(to.p_path, fs->st_uid, fs->st_gid) == -1)
467 if (errno == EPERM)
468 dochown = 0;
469 else
470 error(to.p_path);
471 }
a783899b
KB
472}
473
24e5a125
KB
474error(s)
475 char *s;
0908a03a 476{
4d2ae24a 477 extern int errno;
ae5e5236 478
4d2ae24a 479 exit_val = 1;
24e5a125 480 (void)fprintf(stderr, "cp: %s: %s\n", s, strerror(errno));
4d2ae24a 481}
ae5e5236
KB
482
483/********************************************************************
484 * Path Manipulation Routines.
485 ********************************************************************/
486
487/*
488 * These functions manipulate paths in "path_t" structures.
489 *
490 * They eliminate multiple slashes in paths when they notice them, and keep
491 * the path non-slash terminated.
492 *
4d2ae24a 493 * Both path_set() and path_append() return 0 if the requested name
ae5e5236
KB
494 * would be too long.
495 */
496
4d2ae24a
KB
497#define STRIP_TRAILING_SLASH(p) { \
498 while ((p)->p_end > (p)->p_path && (p)->p_end[-1] == '/') \
499 *--(p)->p_end = 0; \
500 }
ae5e5236
KB
501
502/*
503 * Move specified string into path. Convert "" to "." to handle BSD
504 * semantics for a null path. Strip trailing slashes.
505 */
506path_set(p, string)
4d2ae24a
KB
507 register path_t *p;
508 char *string;
ae5e5236 509{
4d2ae24a 510 if (strlen(string) > MAXPATHLEN) {
24e5a125 511 (void)fprintf(stderr, "cp: %s: name too long.\n", string);
4d2ae24a
KB
512 exit_val = 1;
513 return(0);
514 }
ae5e5236 515
4d2ae24a
KB
516 (void)strcpy(p->p_path, string);
517 p->p_end = p->p_path + strlen(p->p_path);
ae5e5236 518
4d2ae24a
KB
519 if (p->p_path == p->p_end) {
520 *p->p_end++ = '.';
521 *p->p_end = 0;
522 }
ae5e5236 523
4d2ae24a
KB
524 STRIP_TRAILING_SLASH(p);
525 return(1);
ae5e5236
KB
526}
527
528/*
529 * Append specified string to path, inserting '/' if necessary. Return a
530 * pointer to the old end of path for restoration.
531 */
4d2ae24a 532char *
ae5e5236 533path_append(p, name, len)
4d2ae24a
KB
534 register path_t *p;
535 char *name;
536 int len;
1a4b831f 537{
4d2ae24a 538 char *old;
ae5e5236 539
4d2ae24a
KB
540 old = p->p_end;
541 if (len == -1)
542 len = strlen(name);
ae5e5236 543
4d2ae24a
KB
544 /*
545 * The final "+ 1" accounts for the '/' between old path and name.
546 */
547 if ((len + p->p_end - p->p_path + 1) > MAXPATHLEN) {
24e5a125 548 (void)fprintf(stderr,
03137eae 549 "cp: %s/%s: name too long.\n", p->p_path, name);
4d2ae24a
KB
550 exit_val = 1;
551 return(0);
552 }
ae5e5236 553
4d2ae24a
KB
554 /*
555 * This code should always be executed, since paths shouldn't
556 * end in '/'.
557 */
558 if (p->p_end[-1] != '/') {
559 *p->p_end++ = '/';
560 *p->p_end = 0;
561 }
ae5e5236 562
4d2ae24a
KB
563 (void)strncat(p->p_end, name, len);
564 p->p_end += len;
565 *p->p_end = 0;
ae5e5236 566
4d2ae24a
KB
567 STRIP_TRAILING_SLASH(p);
568 return(old);
ae5e5236
KB
569}
570
ae5e5236
KB
571/*
572 * Restore path to previous value. (As returned by path_append.)
573 */
574void
575path_restore(p, old)
4d2ae24a
KB
576 path_t *p;
577 char *old;
ae5e5236 578{
4d2ae24a
KB
579 p->p_end = old;
580 *p->p_end = 0;
ae5e5236
KB
581}
582
ae5e5236
KB
583/*
584 * Return basename of path. (Like basename(1).)
585 */
4d2ae24a 586char *
ae5e5236 587path_basename(p)
4d2ae24a 588 path_t *p;
ae5e5236 589{
4d2ae24a 590 char *basename;
ae5e5236 591
4d2ae24a 592 basename = rindex(p->p_path, '/');
24e5a125 593 return(basename ? ++basename : p->p_path);
4d2ae24a 594}
1a4b831f 595
4d2ae24a
KB
596usage()
597{
598 (void)fprintf(stderr,
599 "usage: cp [-ip] f1 f2; or: cp [-irp] f1 ... fn directory\n");
600 exit(1);
1a4b831f 601}