Home | History | Annotate | Line # | Download | only in cp
cp.c revision 1.58
      1  1.58  christos /* $NetBSD: cp.c,v 1.58 2012/01/04 15:58:37 christos Exp $ */
      2  1.12       cgd 
      3   1.1       cgd /*
      4  1.10   mycroft  * Copyright (c) 1988, 1993, 1994
      5  1.10   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * David Hitz of Auspex Systems Inc.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.34       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35  1.18   thorpej #include <sys/cdefs.h>
     36   1.1       cgd #ifndef lint
     37  1.18   thorpej __COPYRIGHT(
     38  1.51     lukem "@(#) Copyright (c) 1988, 1993, 1994\
     39  1.51     lukem  The Regents of the University of California.  All rights reserved.");
     40   1.1       cgd #endif /* not lint */
     41   1.1       cgd 
     42   1.1       cgd #ifndef lint
     43  1.12       cgd #if 0
     44  1.14       jtc static char sccsid[] = "@(#)cp.c	8.5 (Berkeley) 4/29/95";
     45  1.12       cgd #else
     46  1.58  christos __RCSID("$NetBSD: cp.c,v 1.58 2012/01/04 15:58:37 christos Exp $");
     47  1.12       cgd #endif
     48   1.1       cgd #endif /* not lint */
     49   1.1       cgd 
     50   1.1       cgd /*
     51  1.10   mycroft  * Cp copies source files to target files.
     52   1.1       cgd  *
     53  1.10   mycroft  * The global PATH_T structure "to" always contains the path to the
     54  1.10   mycroft  * current target file.  Since fts(3) does not change directories,
     55  1.10   mycroft  * this path can be either absolute or dot-relative.
     56   1.1       cgd  *
     57  1.10   mycroft  * The basic algorithm is to initialize "to" and use fts(3) to traverse
     58  1.10   mycroft  * the file hierarchy rooted in the argument list.  A trivial case is the
     59  1.10   mycroft  * case of 'cp file1 file2'.  The more interesting case is the case of
     60  1.10   mycroft  * 'cp file1 file2 ... fileN dir' where the hierarchy is traversed and the
     61  1.10   mycroft  * path (relative to the root of the traversal) is appended to dir (stored
     62  1.10   mycroft  * in "to") to form the final target path.
     63   1.1       cgd  */
     64   1.1       cgd 
     65   1.1       cgd #include <sys/param.h>
     66   1.1       cgd #include <sys/stat.h>
     67  1.10   mycroft 
     68  1.53     pooka #include <assert.h>
     69  1.10   mycroft #include <err.h>
     70  1.10   mycroft #include <errno.h>
     71  1.10   mycroft #include <fts.h>
     72  1.28    kleink #include <locale.h>
     73  1.58  christos #include <signal.h>
     74  1.29      matt #include <stdlib.h>
     75   1.1       cgd #include <stdio.h>
     76   1.1       cgd #include <string.h>
     77  1.10   mycroft #include <unistd.h>
     78  1.10   mycroft 
     79   1.5   mycroft #include "extern.h"
     80   1.1       cgd 
     81  1.10   mycroft #define	STRIP_TRAILING_SLASH(p) {					\
     82  1.15    kleink         while ((p).p_end > (p).p_path + 1 && (p).p_end[-1] == '/')	\
     83  1.27       mjl                 *--(p).p_end = '\0';					\
     84  1.10   mycroft }
     85   1.5   mycroft 
     86  1.36  christos static char empty[] = "";
     87  1.47  christos PATH_T to = { .p_end = to.p_path, .target_end = empty  };
     88   1.1       cgd 
     89   1.1       cgd uid_t myuid;
     90  1.55     darcy int Hflag, Lflag, Rflag, Pflag, fflag, iflag, lflag, pflag, rflag, vflag, Nflag;
     91  1.20   mycroft mode_t myumask;
     92  1.58  christos sig_atomic_t pinfo;
     93  1.10   mycroft 
     94  1.10   mycroft enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
     95   1.1       cgd 
     96  1.58  christos static int copy(char *[], enum op, int);
     97  1.58  christos 
     98  1.58  christos static void
     99  1.58  christos progress(int sig __unused)
    100  1.58  christos {
    101  1.58  christos 
    102  1.58  christos 	pinfo++;
    103  1.58  christos }
    104  1.10   mycroft 
    105  1.10   mycroft int
    106  1.31       wiz main(int argc, char *argv[])
    107   1.1       cgd {
    108  1.10   mycroft 	struct stat to_stat, tmp_stat;
    109  1.10   mycroft 	enum op type;
    110  1.44  jschauma 	int ch, fts_options, r, have_trailing_slash;
    111  1.40  christos 	char *target, **src;
    112  1.28    kleink 
    113  1.49  christos 	setprogname(argv[0]);
    114  1.28    kleink 	(void)setlocale(LC_ALL, "");
    115  1.10   mycroft 
    116  1.10   mycroft 	Hflag = Lflag = Pflag = Rflag = 0;
    117  1.55     darcy 	while ((ch = getopt(argc, argv, "HLNPRfailprv")) != -1)
    118  1.10   mycroft 		switch (ch) {
    119  1.10   mycroft 		case 'H':
    120  1.10   mycroft 			Hflag = 1;
    121  1.10   mycroft 			Lflag = Pflag = 0;
    122  1.10   mycroft 			break;
    123  1.10   mycroft 		case 'L':
    124  1.10   mycroft 			Lflag = 1;
    125  1.10   mycroft 			Hflag = Pflag = 0;
    126  1.10   mycroft 			break;
    127  1.37      elad 		case 'N':
    128  1.37      elad 			Nflag = 1;
    129  1.37      elad 			break;
    130  1.10   mycroft 		case 'P':
    131  1.10   mycroft 			Pflag = 1;
    132  1.10   mycroft 			Hflag = Lflag = 0;
    133  1.10   mycroft 			break;
    134  1.10   mycroft 		case 'R':
    135  1.10   mycroft 			Rflag = 1;
    136  1.10   mycroft 			break;
    137  1.54  christos 		case 'a':
    138  1.54  christos 			Pflag = 1;
    139  1.54  christos 			pflag = 1;
    140  1.54  christos 			Rflag = 1;
    141  1.54  christos 			Hflag = Lflag = 0;
    142  1.54  christos 			break;
    143   1.1       cgd 		case 'f':
    144  1.25  wsanchez 			fflag = 1;
    145   1.1       cgd 			iflag = 0;
    146   1.1       cgd 			break;
    147   1.1       cgd 		case 'i':
    148   1.1       cgd 			iflag = isatty(fileno(stdin));
    149  1.25  wsanchez 			fflag = 0;
    150   1.1       cgd 			break;
    151  1.55     darcy 		case 'l':
    152  1.55     darcy 			lflag = 1;
    153  1.55     darcy 			break;
    154   1.1       cgd 		case 'p':
    155   1.1       cgd 			pflag = 1;
    156   1.1       cgd 			break;
    157  1.10   mycroft 		case 'r':
    158   1.1       cgd 			rflag = 1;
    159   1.1       cgd 			break;
    160  1.32       jrf 		case 'v':
    161  1.32       jrf 			vflag = 1;
    162  1.32       jrf 			break;
    163   1.1       cgd 		case '?':
    164   1.1       cgd 		default:
    165   1.1       cgd 			usage();
    166  1.38  christos 			/* NOTREACHED */
    167   1.1       cgd 		}
    168   1.1       cgd 	argc -= optind;
    169   1.1       cgd 	argv += optind;
    170   1.1       cgd 
    171   1.1       cgd 	if (argc < 2)
    172   1.1       cgd 		usage();
    173   1.1       cgd 
    174  1.10   mycroft 	fts_options = FTS_NOCHDIR | FTS_PHYSICAL;
    175  1.10   mycroft 	if (rflag) {
    176  1.33  jschauma 		if (Rflag) {
    177  1.33  jschauma 			errx(EXIT_FAILURE,
    178  1.10   mycroft 		    "the -R and -r options may not be specified together.");
    179  1.33  jschauma 			/* NOTREACHED */
    180  1.33  jschauma 		}
    181  1.33  jschauma 		if (Hflag || Lflag || Pflag) {
    182  1.33  jschauma 			errx(EXIT_FAILURE,
    183  1.10   mycroft 	"the -H, -L, and -P options may not be specified with the -r option.");
    184  1.33  jschauma 			/* NOTREACHED */
    185  1.33  jschauma 		}
    186  1.10   mycroft 		fts_options &= ~FTS_PHYSICAL;
    187  1.10   mycroft 		fts_options |= FTS_LOGICAL;
    188  1.10   mycroft 	}
    189  1.44  jschauma 
    190  1.10   mycroft 	if (Rflag) {
    191  1.10   mycroft 		if (Hflag)
    192  1.10   mycroft 			fts_options |= FTS_COMFOLLOW;
    193  1.10   mycroft 		if (Lflag) {
    194  1.10   mycroft 			fts_options &= ~FTS_PHYSICAL;
    195  1.10   mycroft 			fts_options |= FTS_LOGICAL;
    196  1.10   mycroft 		}
    197  1.44  jschauma 	} else if (!Pflag) {
    198  1.10   mycroft 		fts_options &= ~FTS_PHYSICAL;
    199  1.27       mjl 		fts_options |= FTS_LOGICAL | FTS_COMFOLLOW;
    200   1.1       cgd 	}
    201   1.1       cgd 
    202   1.1       cgd 	myuid = getuid();
    203   1.1       cgd 
    204  1.10   mycroft 	/* Copy the umask for explicit mode setting. */
    205   1.1       cgd 	myumask = umask(0);
    206   1.1       cgd 	(void)umask(myumask);
    207   1.1       cgd 
    208  1.10   mycroft 	/* Save the target base in "to". */
    209  1.10   mycroft 	target = argv[--argc];
    210  1.40  christos 	if (strlcpy(to.p_path, target, sizeof(to.p_path)) >= sizeof(to.p_path))
    211  1.35  jschauma 		errx(EXIT_FAILURE, "%s: name too long", target);
    212  1.10   mycroft 	to.p_end = to.p_path + strlen(to.p_path);
    213  1.40  christos 	have_trailing_slash = (to.p_end[-1] == '/');
    214  1.40  christos 	if (have_trailing_slash)
    215  1.40  christos 		STRIP_TRAILING_SLASH(to);
    216  1.10   mycroft 	to.target_end = to.p_end;
    217  1.10   mycroft 
    218  1.10   mycroft 	/* Set end of argument list for fts(3). */
    219  1.10   mycroft 	argv[argc] = NULL;
    220  1.10   mycroft 
    221  1.58  christos 	(void)signal(SIGINFO, progress);
    222  1.58  christos 
    223   1.1       cgd 	/*
    224   1.1       cgd 	 * Cp has two distinct cases:
    225   1.1       cgd 	 *
    226  1.10   mycroft 	 * cp [-R] source target
    227  1.10   mycroft 	 * cp [-R] source1 ... sourceN directory
    228   1.1       cgd 	 *
    229   1.1       cgd 	 * In both cases, source can be either a file or a directory.
    230   1.1       cgd 	 *
    231   1.1       cgd 	 * In (1), the target becomes a copy of the source. That is, if the
    232   1.1       cgd 	 * source is a file, the target will be a file, and likewise for
    233   1.1       cgd 	 * directories.
    234   1.1       cgd 	 *
    235   1.1       cgd 	 * In (2), the real target is not directory, but "directory/source".
    236   1.1       cgd 	 */
    237  1.44  jschauma 	if (Pflag)
    238  1.44  jschauma 		r = lstat(to.p_path, &to_stat);
    239  1.44  jschauma 	else
    240  1.44  jschauma 		r = stat(to.p_path, &to_stat);
    241  1.33  jschauma 	if (r == -1 && errno != ENOENT) {
    242  1.35  jschauma 		err(EXIT_FAILURE, "%s", to.p_path);
    243  1.33  jschauma 		/* NOTREACHED */
    244  1.33  jschauma 	}
    245   1.1       cgd 	if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
    246   1.1       cgd 		/*
    247   1.1       cgd 		 * Case (1).  Target is not a directory.
    248  1.10   mycroft 		 */
    249  1.38  christos 		if (argc > 1)
    250   1.1       cgd 			usage();
    251  1.10   mycroft 		/*
    252  1.10   mycroft 		 * Need to detect the case:
    253  1.10   mycroft 		 *	cp -R dir foo
    254  1.10   mycroft 		 * Where dir is a directory and foo does not exist, where
    255  1.10   mycroft 		 * we want pathname concatenations turned on but not for
    256  1.10   mycroft 		 * the initial mkdir().
    257  1.10   mycroft 		 */
    258  1.10   mycroft 		if (r == -1) {
    259  1.10   mycroft 			if (rflag || (Rflag && (Lflag || Hflag)))
    260  1.21   mycroft 				r = stat(*argv, &tmp_stat);
    261  1.10   mycroft 			else
    262  1.21   mycroft 				r = lstat(*argv, &tmp_stat);
    263  1.33  jschauma 			if (r == -1) {
    264  1.35  jschauma 				err(EXIT_FAILURE, "%s", *argv);
    265  1.33  jschauma 				/* NOTREACHED */
    266  1.33  jschauma 			}
    267  1.10   mycroft 
    268  1.10   mycroft 			if (S_ISDIR(tmp_stat.st_mode) && (Rflag || rflag))
    269  1.10   mycroft 				type = DIR_TO_DNE;
    270  1.10   mycroft 			else
    271  1.10   mycroft 				type = FILE_TO_FILE;
    272  1.10   mycroft 		} else
    273  1.10   mycroft 			type = FILE_TO_FILE;
    274  1.40  christos 
    275  1.40  christos 		if (have_trailing_slash && type == FILE_TO_FILE) {
    276  1.40  christos 			if (r == -1)
    277  1.40  christos 				errx(1, "directory %s does not exist",
    278  1.40  christos 				     to.p_path);
    279  1.40  christos 			else
    280  1.40  christos 				errx(1, "%s is not a directory", to.p_path);
    281  1.40  christos 		}
    282  1.25  wsanchez 	} else {
    283   1.1       cgd 		/*
    284   1.1       cgd 		 * Case (2).  Target is a directory.
    285   1.1       cgd 		 */
    286  1.10   mycroft 		type = FILE_TO_DIR;
    287  1.25  wsanchez 	}
    288  1.10   mycroft 
    289  1.40  christos 	/*
    290  1.40  christos 	 * make "cp -rp src/ dst" behave like "cp -rp src dst" not
    291  1.40  christos 	 * like "cp -rp src/. dst"
    292  1.40  christos 	 */
    293  1.40  christos 	for (src = argv; *src; src++) {
    294  1.40  christos 		size_t len = strlen(*src);
    295  1.40  christos 		while (len-- > 1 && (*src)[len] == '/')
    296  1.40  christos 			(*src)[len] = '\0';
    297  1.40  christos 	}
    298  1.40  christos 
    299  1.31       wiz 	exit(copy(argv, type, fts_options));
    300  1.20   mycroft 	/* NOTREACHED */
    301   1.1       cgd }
    302   1.1       cgd 
    303  1.53     pooka static int dnestack[MAXPATHLEN]; /* unlikely we'll have more nested dirs */
    304  1.53     pooka static ssize_t dnesp;
    305  1.53     pooka static void
    306  1.53     pooka pushdne(int dne)
    307  1.53     pooka {
    308  1.53     pooka 
    309  1.53     pooka 	dnestack[dnesp++] = dne;
    310  1.53     pooka 	assert(dnesp < MAXPATHLEN);
    311  1.53     pooka }
    312  1.53     pooka 
    313  1.53     pooka static int
    314  1.53     pooka popdne(void)
    315  1.53     pooka {
    316  1.53     pooka 	int rv;
    317  1.53     pooka 
    318  1.53     pooka 	rv = dnestack[--dnesp];
    319  1.53     pooka 	assert(dnesp >= 0);
    320  1.53     pooka 	return rv;
    321  1.53     pooka }
    322  1.53     pooka 
    323  1.58  christos static int
    324  1.31       wiz copy(char *argv[], enum op type, int fts_options)
    325   1.1       cgd {
    326  1.10   mycroft 	struct stat to_stat;
    327  1.10   mycroft 	FTS *ftsp;
    328  1.10   mycroft 	FTSENT *curr;
    329  1.50  dholland 	int base, dne, sval;
    330  1.50  dholland 	int this_failed, any_failed;
    331  1.38  christos 	size_t nlen;
    332  1.40  christos 	char *p, *target_mid;
    333   1.1       cgd 
    334  1.18   thorpej 	base = 0;	/* XXX gcc -Wuninitialized (see comment below) */
    335  1.18   thorpej 
    336  1.52     pooka 	if ((ftsp = fts_open(argv, fts_options, NULL)) == NULL)
    337  1.35  jschauma 		err(EXIT_FAILURE, "%s", argv[0]);
    338  1.33  jschauma 		/* NOTREACHED */
    339  1.50  dholland 	for (any_failed = 0; (curr = fts_read(ftsp)) != NULL;) {
    340  1.50  dholland 		this_failed = 0;
    341  1.10   mycroft 		switch (curr->fts_info) {
    342  1.10   mycroft 		case FTS_NS:
    343  1.27       mjl 		case FTS_DNR:
    344  1.10   mycroft 		case FTS_ERR:
    345  1.35  jschauma 			warnx("%s: %s", curr->fts_path,
    346  1.35  jschauma 					strerror(curr->fts_errno));
    347  1.50  dholland 			this_failed = any_failed = 1;
    348  1.10   mycroft 			continue;
    349  1.10   mycroft 		case FTS_DC:			/* Warn, continue. */
    350  1.35  jschauma 			warnx("%s: directory causes a cycle", curr->fts_path);
    351  1.50  dholland 			this_failed = any_failed = 1;
    352  1.10   mycroft 			continue;
    353  1.10   mycroft 		}
    354  1.10   mycroft 
    355  1.10   mycroft 		/*
    356  1.10   mycroft 		 * If we are in case (2) or (3) above, we need to append the
    357  1.10   mycroft                  * source name to the target name.
    358  1.10   mycroft                  */
    359  1.10   mycroft 		if (type != FILE_TO_FILE) {
    360  1.10   mycroft 			if ((curr->fts_namelen +
    361  1.10   mycroft 			    to.target_end - to.p_path + 1) > MAXPATHLEN) {
    362  1.35  jschauma 				warnx("%s/%s: name too long (not copied)",
    363  1.35  jschauma 						to.p_path, curr->fts_name);
    364  1.50  dholland 				this_failed = any_failed = 1;
    365  1.10   mycroft 				continue;
    366  1.10   mycroft 			}
    367   1.1       cgd 
    368  1.10   mycroft 			/*
    369  1.10   mycroft 			 * Need to remember the roots of traversals to create
    370  1.10   mycroft 			 * correct pathnames.  If there's a directory being
    371  1.10   mycroft 			 * copied to a non-existent directory, e.g.
    372  1.10   mycroft 			 *	cp -R a/dir noexist
    373  1.10   mycroft 			 * the resulting path name should be noexist/foo, not
    374  1.10   mycroft 			 * noexist/dir/foo (where foo is a file in dir), which
    375  1.10   mycroft 			 * is the case where the target exists.
    376  1.10   mycroft 			 *
    377  1.10   mycroft 			 * Also, check for "..".  This is for correct path
    378  1.10   mycroft 			 * concatentation for paths ending in "..", e.g.
    379  1.10   mycroft 			 *	cp -R .. /tmp
    380  1.10   mycroft 			 * Paths ending in ".." are changed to ".".  This is
    381  1.10   mycroft 			 * tricky, but seems the easiest way to fix the problem.
    382  1.10   mycroft 			 *
    383  1.10   mycroft 			 * XXX
    384  1.10   mycroft 			 * Since the first level MUST be FTS_ROOTLEVEL, base
    385  1.10   mycroft 			 * is always initialized.
    386  1.10   mycroft 			 */
    387  1.24   thorpej 			if (curr->fts_level == FTS_ROOTLEVEL) {
    388  1.10   mycroft 				if (type != DIR_TO_DNE) {
    389  1.10   mycroft 					p = strrchr(curr->fts_path, '/');
    390  1.10   mycroft 					base = (p == NULL) ? 0 :
    391  1.10   mycroft 					    (int)(p - curr->fts_path + 1);
    392  1.10   mycroft 
    393  1.10   mycroft 					if (!strcmp(&curr->fts_path[base],
    394  1.10   mycroft 					    ".."))
    395  1.10   mycroft 						base += 1;
    396  1.10   mycroft 				} else
    397  1.10   mycroft 					base = curr->fts_pathlen;
    398  1.24   thorpej 			}
    399  1.10   mycroft 
    400  1.10   mycroft 			p = &curr->fts_path[base];
    401  1.10   mycroft 			nlen = curr->fts_pathlen - base;
    402  1.40  christos 			target_mid = to.target_end;
    403  1.40  christos 			if (*p != '/' && target_mid[-1] != '/')
    404  1.40  christos 				*target_mid++ = '/';
    405  1.40  christos 			*target_mid = 0;
    406  1.40  christos 
    407  1.40  christos 			if (target_mid - to.p_path + nlen >= PATH_MAX) {
    408  1.40  christos 				warnx("%s%s: name too long (not copied)",
    409  1.40  christos 				    to.p_path, p);
    410  1.50  dholland 				this_failed = any_failed = 1;
    411  1.40  christos 				continue;
    412  1.40  christos 			}
    413  1.40  christos 			(void)strncat(target_mid, p, nlen);
    414  1.40  christos 			to.p_end = target_mid + nlen;
    415  1.10   mycroft 			*to.p_end = 0;
    416  1.10   mycroft 			STRIP_TRAILING_SLASH(to);
    417  1.10   mycroft 		}
    418  1.10   mycroft 
    419  1.44  jschauma 		sval = Pflag ? lstat(to.p_path, &to_stat) : stat(to.p_path, &to_stat);
    420  1.10   mycroft 		/* Not an error but need to remember it happened */
    421  1.44  jschauma 		if (sval == -1)
    422  1.10   mycroft 			dne = 1;
    423  1.10   mycroft 		else {
    424  1.10   mycroft 			if (to_stat.st_dev == curr->fts_statp->st_dev &&
    425  1.10   mycroft 			    to_stat.st_ino == curr->fts_statp->st_ino) {
    426  1.10   mycroft 				warnx("%s and %s are identical (not copied).",
    427  1.10   mycroft 				    to.p_path, curr->fts_path);
    428  1.50  dholland 				this_failed = any_failed = 1;
    429  1.10   mycroft 				if (S_ISDIR(curr->fts_statp->st_mode))
    430  1.10   mycroft 					(void)fts_set(ftsp, curr, FTS_SKIP);
    431  1.10   mycroft 				continue;
    432  1.10   mycroft 			}
    433  1.10   mycroft 			if (!S_ISDIR(curr->fts_statp->st_mode) &&
    434  1.10   mycroft 			    S_ISDIR(to_stat.st_mode)) {
    435  1.14       jtc 		warnx("cannot overwrite directory %s with non-directory %s",
    436  1.35  jschauma 				    to.p_path, curr->fts_path);
    437  1.50  dholland 				this_failed = any_failed = 1;
    438  1.10   mycroft 				continue;
    439  1.10   mycroft 			}
    440  1.53     pooka 			dne = 0;
    441   1.1       cgd 		}
    442   1.1       cgd 
    443  1.10   mycroft 		switch (curr->fts_statp->st_mode & S_IFMT) {
    444  1.10   mycroft 		case S_IFLNK:
    445  1.27       mjl 			/* Catch special case of a non dangling symlink */
    446  1.27       mjl 			if((fts_options & FTS_LOGICAL) ||
    447  1.27       mjl 			   ((fts_options & FTS_COMFOLLOW) && curr->fts_level == 0)) {
    448  1.27       mjl 				if (copy_file(curr, dne))
    449  1.50  dholland 					this_failed = any_failed = 1;
    450  1.27       mjl 			} else {
    451  1.27       mjl 				if (copy_link(curr, !dne))
    452  1.50  dholland 					this_failed = any_failed = 1;
    453  1.27       mjl 			}
    454  1.10   mycroft 			break;
    455  1.10   mycroft 		case S_IFDIR:
    456  1.10   mycroft 			if (!Rflag && !rflag) {
    457  1.35  jschauma 				if (curr->fts_info == FTS_D)
    458  1.26      dean 					warnx("%s is a directory (not copied).",
    459  1.35  jschauma 					    curr->fts_path);
    460  1.10   mycroft 				(void)fts_set(ftsp, curr, FTS_SKIP);
    461  1.50  dholland 				this_failed = any_failed = 1;
    462  1.10   mycroft 				break;
    463  1.10   mycroft 			}
    464  1.25  wsanchez 
    465  1.25  wsanchez                         /*
    466  1.25  wsanchez                          * Directories get noticed twice:
    467  1.25  wsanchez                          *  In the first pass, create it if needed.
    468  1.25  wsanchez                          *  In the second pass, after the children have been copied, set the permissions.
    469  1.25  wsanchez                          */
    470  1.25  wsanchez 			if (curr->fts_info == FTS_D) /* First pass */
    471  1.25  wsanchez 			{
    472  1.25  wsanchez 				/*
    473  1.25  wsanchez 				 * If the directory doesn't exist, create the new
    474  1.25  wsanchez 				 * one with the from file mode plus owner RWX bits,
    475  1.25  wsanchez 				 * modified by the umask.  Trade-off between being
    476  1.25  wsanchez 				 * able to write the directory (if from directory is
    477  1.25  wsanchez 				 * 555) and not causing a permissions race.  If the
    478  1.25  wsanchez 				 * umask blocks owner writes, we fail..
    479  1.25  wsanchez 				 */
    480  1.53     pooka 				pushdne(dne);
    481  1.25  wsanchez 				if (dne) {
    482  1.25  wsanchez 					if (mkdir(to.p_path,
    483  1.25  wsanchez 					    curr->fts_statp->st_mode | S_IRWXU) < 0)
    484  1.33  jschauma 						err(EXIT_FAILURE, "%s",
    485  1.35  jschauma 						    to.p_path);
    486  1.33  jschauma 						/* NOTREACHED */
    487  1.25  wsanchez 				} else if (!S_ISDIR(to_stat.st_mode)) {
    488  1.25  wsanchez 					errno = ENOTDIR;
    489  1.33  jschauma 					err(EXIT_FAILURE, "%s",
    490  1.35  jschauma 						to.p_path);
    491  1.33  jschauma 					/* NOTREACHED */
    492  1.25  wsanchez 				}
    493   1.1       cgd 			}
    494  1.25  wsanchez 			else if (curr->fts_info == FTS_DP) /* Second pass */
    495  1.25  wsanchez 			{
    496  1.25  wsanchez 	                        /*
    497  1.25  wsanchez 				 * If not -p and directory didn't exist, set it to be
    498  1.25  wsanchez 				 * the same as the from directory, umodified by the
    499  1.25  wsanchez                         	 * umask; arguably wrong, but it's been that way
    500  1.25  wsanchez                         	 * forever.
    501  1.25  wsanchez 				 */
    502  1.25  wsanchez 				if (pflag && setfile(curr->fts_statp, 0))
    503  1.50  dholland 					this_failed = any_failed = 1;
    504  1.53     pooka 				else if ((dne = popdne()))
    505  1.25  wsanchez 					(void)chmod(to.p_path,
    506  1.25  wsanchez 					    curr->fts_statp->st_mode);
    507  1.25  wsanchez 			}
    508  1.25  wsanchez 			else
    509  1.48       alc 			{
    510  1.48       alc 				warnx("directory %s encountered when not expected.",
    511  1.48       alc 				    curr->fts_path);
    512  1.50  dholland 				this_failed = any_failed = 1;
    513  1.48       alc 				break;
    514  1.48       alc 			}
    515  1.25  wsanchez 
    516  1.10   mycroft 			break;
    517  1.10   mycroft 		case S_IFBLK:
    518  1.10   mycroft 		case S_IFCHR:
    519  1.10   mycroft 			if (Rflag) {
    520  1.10   mycroft 				if (copy_special(curr->fts_statp, !dne))
    521  1.50  dholland 					this_failed = any_failed = 1;
    522  1.10   mycroft 			} else
    523  1.10   mycroft 				if (copy_file(curr, dne))
    524  1.50  dholland 					this_failed = any_failed = 1;
    525  1.10   mycroft 			break;
    526  1.10   mycroft 		case S_IFIFO:
    527  1.13   mycroft 			if (Rflag) {
    528  1.10   mycroft 				if (copy_fifo(curr->fts_statp, !dne))
    529  1.50  dholland 					this_failed = any_failed = 1;
    530  1.13   mycroft 			} else
    531  1.10   mycroft 				if (copy_file(curr, dne))
    532  1.50  dholland 					this_failed = any_failed = 1;
    533  1.10   mycroft 			break;
    534  1.10   mycroft 		default:
    535  1.10   mycroft 			if (copy_file(curr, dne))
    536  1.50  dholland 				this_failed = any_failed = 1;
    537  1.10   mycroft 			break;
    538   1.1       cgd 		}
    539  1.50  dholland 		if (vflag && !this_failed)
    540  1.35  jschauma 			(void)printf("%s -> %s\n", curr->fts_path, to.p_path);
    541  1.33  jschauma 	}
    542  1.33  jschauma 	if (errno) {
    543  1.33  jschauma 		err(EXIT_FAILURE, "fts_read");
    544  1.33  jschauma 		/* NOTREACHED */
    545   1.1       cgd 	}
    546  1.41       erh 	(void)fts_close(ftsp);
    547  1.50  dholland 	return (any_failed);
    548   1.1       cgd }
    549