diff options
Diffstat (limited to 'bin/cp/cp.c')
-rw-r--r-- | bin/cp/cp.c | 122 |
1 files changed, 48 insertions, 74 deletions
diff --git a/bin/cp/cp.c b/bin/cp/cp.c index dbae4b535843..823376964bd1 100644 --- a/bin/cp/cp.c +++ b/bin/cp/cp.c @@ -32,20 +32,6 @@ * SUCH DAMAGE. */ -#if 0 -#ifndef lint -static char const copyright[] = -"@(#) Copyright (c) 1988, 1993, 1994\n\ - The Regents of the University of California. All rights reserved.\n"; -#endif /* not lint */ - -#ifndef lint -static char sccsid[] = "@(#)cp.c 8.2 (Berkeley) 4/1/94"; -#endif /* not lint */ -#endif -#include <sys/cdefs.h> -__FBSDID("$FreeBSD$"); - /* * Cp copies source files to target files. * @@ -86,8 +72,8 @@ static char emptystring[] = ""; PATH_T to = { to.p_path, emptystring, "" }; -int fflag, iflag, lflag, nflag, pflag, sflag, vflag; -static int Hflag, Lflag, Rflag, rflag; +int Nflag, fflag, iflag, lflag, nflag, pflag, sflag, vflag; +static int Hflag, Lflag, Pflag, Rflag, rflag; volatile sig_atomic_t info; enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE }; @@ -100,12 +86,11 @@ main(int argc, char *argv[]) { struct stat to_stat, tmp_stat; enum op type; - int Pflag, ch, fts_options, r, have_trailing_slash; + int ch, fts_options, r, have_trailing_slash; char *target; fts_options = FTS_NOCHDIR | FTS_PHYSICAL; - Pflag = 0; - while ((ch = getopt(argc, argv, "HLPRafilnprsvx")) != -1) + while ((ch = getopt(argc, argv, "HLPRafilNnprsvx")) != -1) switch (ch) { case 'H': Hflag = 1; @@ -139,6 +124,9 @@ main(int argc, char *argv[]) case 'l': lflag = 1; break; + case 'N': + Nflag = 1; + break; case 'n': nflag = 1; fflag = iflag = 0; @@ -161,7 +149,6 @@ main(int argc, char *argv[]) break; default: usage(); - break; } argc -= optind; argv += optind; @@ -273,22 +260,6 @@ main(int argc, char *argv[]) &to_stat))); } -/* Does the right thing based on -R + -H/-L/-P */ -static int -copy_stat(const char *path, struct stat *sb) -{ - - /* - * For -R -H/-P, we need to lstat() instead; copy() cares about the link - * itself rather than the target if we're not following links during the - * traversal. - */ - if (!Rflag || Lflag) - return (stat(path, sb)); - return (lstat(path, sb)); -} - - static int copy(char *argv[], enum op type, int fts_options, struct stat *root_stat) { @@ -311,14 +282,12 @@ copy(char *argv[], enum op type, int fts_options, struct stat *root_stat) recurse_path = NULL; if ((ftsp = fts_open(argv, fts_options, NULL)) == NULL) err(1, "fts_open"); - for (badcp = rval = 0; errno = 0, (curr = fts_read(ftsp)) != NULL; - badcp = 0) { + for (badcp = rval = 0; (curr = fts_read(ftsp)) != NULL; badcp = 0) { switch (curr->fts_info) { case FTS_NS: case FTS_DNR: case FTS_ERR: - warnx("%s: %s", - curr->fts_path, strerror(curr->fts_errno)); + warnc(curr->fts_errno, "%s", curr->fts_path); badcp = rval = 1; continue; case FTS_DC: /* Warn, continue. */ @@ -417,7 +386,6 @@ copy(char *argv[], enum op type, int fts_options, struct stat *root_stat) continue; } - if (asprintf(&recurse_path, "%s/%s", to.p_path, rootname) == -1) err(1, "asprintf"); @@ -466,39 +434,36 @@ copy(char *argv[], enum op type, int fts_options, struct stat *root_stat) continue; } - /* Not an error but need to remember it happened. */ - if (copy_stat(to.p_path, &to_stat) == -1) - dne = 1; - else { - if (to_stat.st_dev == curr->fts_statp->st_dev && - to_stat.st_ino == curr->fts_statp->st_ino) { - warnx("%s and %s are identical (not copied).", - to.p_path, curr->fts_path); - badcp = rval = 1; - if (S_ISDIR(curr->fts_statp->st_mode)) - (void)fts_set(ftsp, curr, FTS_SKIP); - continue; - } - if (!S_ISDIR(curr->fts_statp->st_mode) && - S_ISDIR(to_stat.st_mode)) { - warnx("cannot overwrite directory %s with " - "non-directory %s", - to.p_path, curr->fts_path); - badcp = rval = 1; - continue; - } - dne = 0; + /* Check if source and destination are identical. */ + if (stat(to.p_path, &to_stat) == 0 && + to_stat.st_dev == curr->fts_statp->st_dev && + to_stat.st_ino == curr->fts_statp->st_ino) { + warnx("%s and %s are identical (not copied).", + to.p_path, curr->fts_path); + badcp = rval = 1; + if (S_ISDIR(curr->fts_statp->st_mode)) + (void)fts_set(ftsp, curr, FTS_SKIP); + continue; } + /* Not an error but need to remember it happened. */ + dne = lstat(to.p_path, &to_stat) != 0; + switch (curr->fts_statp->st_mode & S_IFMT) { case S_IFLNK: - /* Catch special case of a non-dangling symlink. */ if ((fts_options & FTS_LOGICAL) || ((fts_options & FTS_COMFOLLOW) && curr->fts_level == 0)) { + /* + * We asked FTS to follow links but got + * here anyway, which means the target is + * nonexistent or inaccessible. Let + * copy_file() deal with the error. + */ if (copy_file(curr, dne)) badcp = rval = 1; - } else { + } else { + /* Copy the link. */ if (copy_link(curr, !dne)) badcp = rval = 1; } @@ -520,9 +485,13 @@ copy(char *argv[], enum op type, int fts_options, struct stat *root_stat) * umask blocks owner writes, we fail. */ if (dne) { - if (mkdir(to.p_path, - curr->fts_statp->st_mode | S_IRWXU) < 0) - err(1, "%s", to.p_path); + mode = curr->fts_statp->st_mode | S_IRWXU; + if (mkdir(to.p_path, mode) != 0) { + warn("%s", to.p_path); + (void)fts_set(ftsp, curr, FTS_SKIP); + badcp = rval = 1; + break; + } /* * First DNE with a NULL root_stat is the root * path, so set root_stat. We can't really @@ -531,14 +500,19 @@ copy(char *argv[], enum op type, int fts_options, struct stat *root_stat) * first directory we created and use that. */ if (root_stat == NULL && - stat(to.p_path, &created_root_stat) == -1) { - err(1, "stat"); - } else if (root_stat == NULL) { - root_stat = &created_root_stat; + stat(to.p_path, &created_root_stat) != 0) { + warn("%s", to.p_path); + (void)fts_set(ftsp, curr, FTS_SKIP); + badcp = rval = 1; + break; } + if (root_stat == NULL) + root_stat = &created_root_stat; } else if (!S_ISDIR(to_stat.st_mode)) { - errno = ENOTDIR; - err(1, "%s", to.p_path); + warnc(ENOTDIR, "%s", to.p_path); + (void)fts_set(ftsp, curr, FTS_SKIP); + badcp = rval = 1; + break; } /* * Arrange to correct directory attributes later |