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