Home | History | Annotate | Line # | Download | only in cp
cp.c revision 1.10
      1   1.1      cgd /*
      2  1.10  mycroft  * Copyright (c) 1988, 1993, 1994
      3  1.10  mycroft  *	The Regents of the University of California.  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.10  mycroft static char copyright[] =
     39  1.10  mycroft "@(#) Copyright (c) 1988, 1993, 1994\n\
     40  1.10  mycroft 	The Regents of the University of California.  All rights reserved.\n";
     41   1.1      cgd #endif /* not lint */
     42   1.1      cgd 
     43   1.1      cgd #ifndef lint
     44  1.10  mycroft /*static char sccsid[] = "from: @(#)cp.c	8.2 (Berkeley) 4/1/94";*/
     45  1.10  mycroft static char *rcsid = "$Id: cp.c,v 1.10 1994/09/22 09:24:40 mycroft Exp $";
     46   1.1      cgd #endif /* not lint */
     47   1.1      cgd 
     48   1.1      cgd /*
     49  1.10  mycroft  * Cp copies source files to target files.
     50   1.1      cgd  *
     51  1.10  mycroft  * The global PATH_T structure "to" always contains the path to the
     52  1.10  mycroft  * current target file.  Since fts(3) does not change directories,
     53  1.10  mycroft  * this path can be either absolute or dot-relative.
     54   1.1      cgd  *
     55  1.10  mycroft  * The basic algorithm is to initialize "to" and use fts(3) to traverse
     56  1.10  mycroft  * the file hierarchy rooted in the argument list.  A trivial case is the
     57  1.10  mycroft  * case of 'cp file1 file2'.  The more interesting case is the case of
     58  1.10  mycroft  * 'cp file1 file2 ... fileN dir' where the hierarchy is traversed and the
     59  1.10  mycroft  * path (relative to the root of the traversal) is appended to dir (stored
     60  1.10  mycroft  * in "to") to form the final target path.
     61   1.1      cgd  */
     62   1.1      cgd 
     63   1.1      cgd #include <sys/param.h>
     64   1.1      cgd #include <sys/stat.h>
     65   1.5  mycroft #include <sys/mman.h>
     66   1.1      cgd #include <sys/time.h>
     67  1.10  mycroft 
     68   1.1      cgd #include <dirent.h>
     69  1.10  mycroft #include <err.h>
     70  1.10  mycroft #include <errno.h>
     71   1.1      cgd #include <fcntl.h>
     72  1.10  mycroft #include <fts.h>
     73   1.1      cgd #include <stdio.h>
     74   1.1      cgd #include <stdlib.h>
     75   1.1      cgd #include <string.h>
     76  1.10  mycroft #include <unistd.h>
     77  1.10  mycroft 
     78   1.5  mycroft #include "extern.h"
     79   1.1      cgd 
     80  1.10  mycroft #define	STRIP_TRAILING_SLASH(p) {					\
     81  1.10  mycroft         while ((p).p_end > (p).p_path && (p).p_end[-1] == '/')		\
     82  1.10  mycroft                 *--(p).p_end = 0;					\
     83  1.10  mycroft }
     84   1.5  mycroft 
     85   1.1      cgd PATH_T to = { to.p_path, "" };
     86   1.1      cgd 
     87   1.1      cgd uid_t myuid;
     88  1.10  mycroft int Rflag, iflag, pflag, rflag;
     89  1.10  mycroft int myumask;
     90  1.10  mycroft 
     91  1.10  mycroft enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
     92   1.1      cgd 
     93  1.10  mycroft int copy __P((char *[], enum op, int));
     94  1.10  mycroft int mastercmp __P((const FTSENT **, const FTSENT **));
     95  1.10  mycroft 
     96  1.10  mycroft int
     97   1.1      cgd main(argc, argv)
     98   1.1      cgd 	int argc;
     99  1.10  mycroft 	char *argv[];
    100   1.1      cgd {
    101  1.10  mycroft 	struct stat to_stat, tmp_stat;
    102  1.10  mycroft 	enum op type;
    103  1.10  mycroft 	int Hflag, Lflag, Pflag, ch, fts_options, r;
    104  1.10  mycroft 	char *target;
    105  1.10  mycroft 
    106  1.10  mycroft 	Hflag = Lflag = Pflag = Rflag = 0;
    107  1.10  mycroft 	while ((ch = getopt(argc, argv, "HLPRfipr")) != EOF)
    108  1.10  mycroft 		switch (ch) {
    109  1.10  mycroft 		case 'H':
    110  1.10  mycroft 			Hflag = 1;
    111  1.10  mycroft 			Lflag = Pflag = 0;
    112  1.10  mycroft 			break;
    113  1.10  mycroft 		case 'L':
    114  1.10  mycroft 			Lflag = 1;
    115  1.10  mycroft 			Hflag = Pflag = 0;
    116  1.10  mycroft 			break;
    117  1.10  mycroft 		case 'P':
    118  1.10  mycroft 			Pflag = 1;
    119  1.10  mycroft 			Hflag = Lflag = 0;
    120  1.10  mycroft 			break;
    121  1.10  mycroft 		case 'R':
    122  1.10  mycroft 			Rflag = 1;
    123  1.10  mycroft 			break;
    124   1.1      cgd 		case 'f':
    125   1.1      cgd 			iflag = 0;
    126   1.1      cgd 			break;
    127   1.1      cgd 		case 'i':
    128   1.1      cgd 			iflag = isatty(fileno(stdin));
    129   1.1      cgd 			break;
    130   1.1      cgd 		case 'p':
    131   1.1      cgd 			pflag = 1;
    132   1.1      cgd 			break;
    133  1.10  mycroft 		case 'r':
    134   1.1      cgd 			rflag = 1;
    135   1.1      cgd 			break;
    136   1.1      cgd 		case '?':
    137   1.1      cgd 		default:
    138   1.1      cgd 			usage();
    139   1.1      cgd 			break;
    140   1.1      cgd 		}
    141   1.1      cgd 	argc -= optind;
    142   1.1      cgd 	argv += optind;
    143   1.1      cgd 
    144   1.1      cgd 	if (argc < 2)
    145   1.1      cgd 		usage();
    146   1.1      cgd 
    147  1.10  mycroft 	fts_options = FTS_NOCHDIR | FTS_PHYSICAL;
    148  1.10  mycroft 	if (rflag) {
    149  1.10  mycroft 		if (Rflag)
    150  1.10  mycroft 			errx(1,
    151  1.10  mycroft 		    "the -R and -r options may not be specified together.");
    152  1.10  mycroft 		if (Hflag || Lflag || Pflag)
    153  1.10  mycroft 			errx(1,
    154  1.10  mycroft 	"the -H, -L, and -P options may not be specified with the -r option.");
    155  1.10  mycroft 		fts_options &= ~FTS_PHYSICAL;
    156  1.10  mycroft 		fts_options |= FTS_LOGICAL;
    157  1.10  mycroft 	}
    158  1.10  mycroft 	if (Rflag) {
    159  1.10  mycroft 		if (Hflag)
    160  1.10  mycroft 			fts_options |= FTS_COMFOLLOW;
    161  1.10  mycroft 		if (Lflag) {
    162  1.10  mycroft 			fts_options &= ~FTS_PHYSICAL;
    163  1.10  mycroft 			fts_options |= FTS_LOGICAL;
    164  1.10  mycroft 		}
    165  1.10  mycroft 	} else {
    166  1.10  mycroft 		fts_options &= ~FTS_PHYSICAL;
    167  1.10  mycroft 		fts_options |= FTS_LOGICAL;
    168   1.1      cgd 	}
    169   1.1      cgd 
    170   1.1      cgd 	myuid = getuid();
    171   1.1      cgd 
    172  1.10  mycroft 	/* Copy the umask for explicit mode setting. */
    173   1.1      cgd 	myumask = umask(0);
    174   1.1      cgd 	(void)umask(myumask);
    175   1.1      cgd 
    176  1.10  mycroft 	/* Save the target base in "to". */
    177  1.10  mycroft 	target = argv[--argc];
    178  1.10  mycroft 	if (strlen(target) > MAXPATHLEN)
    179  1.10  mycroft 		errx(1, "%s: name too long", target);
    180  1.10  mycroft 	(void)strcpy(to.p_path, target);
    181  1.10  mycroft 	to.p_end = to.p_path + strlen(to.p_path);
    182  1.10  mycroft         if (to.p_path == to.p_end) {
    183  1.10  mycroft 		*to.p_end++ = '.';
    184  1.10  mycroft 		*to.p_end = 0;
    185  1.10  mycroft 	}
    186  1.10  mycroft         STRIP_TRAILING_SLASH(to);
    187  1.10  mycroft 	to.target_end = to.p_end;
    188  1.10  mycroft 
    189  1.10  mycroft 	/* Set end of argument list for fts(3). */
    190  1.10  mycroft 	argv[argc] = NULL;
    191  1.10  mycroft 
    192   1.1      cgd 	/*
    193   1.1      cgd 	 * Cp has two distinct cases:
    194   1.1      cgd 	 *
    195  1.10  mycroft 	 * cp [-R] source target
    196  1.10  mycroft 	 * cp [-R] source1 ... sourceN directory
    197   1.1      cgd 	 *
    198   1.1      cgd 	 * In both cases, source can be either a file or a directory.
    199   1.1      cgd 	 *
    200   1.1      cgd 	 * In (1), the target becomes a copy of the source. That is, if the
    201   1.1      cgd 	 * source is a file, the target will be a file, and likewise for
    202   1.1      cgd 	 * directories.
    203   1.1      cgd 	 *
    204   1.1      cgd 	 * In (2), the real target is not directory, but "directory/source".
    205   1.1      cgd 	 */
    206   1.1      cgd 	r = stat(to.p_path, &to_stat);
    207  1.10  mycroft 	if (r == -1 && errno != ENOENT)
    208  1.10  mycroft 		err(1, "%s", to.p_path);
    209   1.1      cgd 	if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
    210   1.1      cgd 		/*
    211   1.1      cgd 		 * Case (1).  Target is not a directory.
    212  1.10  mycroft 		 */
    213   1.1      cgd 		if (argc > 1) {
    214   1.1      cgd 			usage();
    215   1.1      cgd 			exit(1);
    216   1.1      cgd 		}
    217  1.10  mycroft 		/*
    218  1.10  mycroft 		 * Need to detect the case:
    219  1.10  mycroft 		 *	cp -R dir foo
    220  1.10  mycroft 		 * Where dir is a directory and foo does not exist, where
    221  1.10  mycroft 		 * we want pathname concatenations turned on but not for
    222  1.10  mycroft 		 * the initial mkdir().
    223  1.10  mycroft 		 */
    224  1.10  mycroft 		if (r == -1) {
    225  1.10  mycroft 			if (rflag || (Rflag && (Lflag || Hflag)))
    226  1.10  mycroft 				stat(*argv, &tmp_stat);
    227  1.10  mycroft 			else
    228  1.10  mycroft 				lstat(*argv, &tmp_stat);
    229  1.10  mycroft 
    230  1.10  mycroft 			if (S_ISDIR(tmp_stat.st_mode) && (Rflag || rflag))
    231  1.10  mycroft 				type = DIR_TO_DNE;
    232  1.10  mycroft 			else
    233  1.10  mycroft 				type = FILE_TO_FILE;
    234  1.10  mycroft 		} else
    235  1.10  mycroft 			type = FILE_TO_FILE;
    236  1.10  mycroft 	} else
    237   1.1      cgd 		/*
    238   1.1      cgd 		 * Case (2).  Target is a directory.
    239   1.1      cgd 		 */
    240  1.10  mycroft 		type = FILE_TO_DIR;
    241  1.10  mycroft 
    242  1.10  mycroft 	exit (copy(argv, type, fts_options));
    243   1.1      cgd }
    244   1.1      cgd 
    245  1.10  mycroft int
    246  1.10  mycroft copy(argv, type, fts_options)
    247  1.10  mycroft 	char *argv[];
    248  1.10  mycroft 	enum op type;
    249  1.10  mycroft 	int fts_options;
    250   1.1      cgd {
    251  1.10  mycroft 	struct stat to_stat;
    252  1.10  mycroft 	FTS *ftsp;
    253  1.10  mycroft 	FTSENT *curr;
    254  1.10  mycroft 	int base, dne, nlen, rval;
    255  1.10  mycroft 	char *p;
    256   1.1      cgd 
    257  1.10  mycroft 	if ((ftsp = fts_open(argv, fts_options, mastercmp)) == NULL)
    258  1.10  mycroft 		err(1, NULL);
    259  1.10  mycroft 	for (rval = 0; (curr = fts_read(ftsp)) != NULL;) {
    260  1.10  mycroft 		switch (curr->fts_info) {
    261  1.10  mycroft 		case FTS_NS:
    262  1.10  mycroft 		case FTS_ERR:
    263  1.10  mycroft 			warnx("%s: %s",
    264  1.10  mycroft 			    curr->fts_path, strerror(curr->fts_errno));
    265  1.10  mycroft 			rval = 1;
    266  1.10  mycroft 			continue;
    267  1.10  mycroft 		case FTS_DC:			/* Warn, continue. */
    268  1.10  mycroft 			warnx("%s: directory causes a cycle", curr->fts_path);
    269  1.10  mycroft 			rval = 1;
    270  1.10  mycroft 			continue;
    271  1.10  mycroft 		case FTS_DP:			/* Ignore, continue. */
    272  1.10  mycroft 			continue;
    273  1.10  mycroft 		}
    274  1.10  mycroft 
    275  1.10  mycroft 		/*
    276  1.10  mycroft 		 * If we are in case (2) or (3) above, we need to append the
    277  1.10  mycroft                  * source name to the target name.
    278  1.10  mycroft                  */
    279  1.10  mycroft 		if (type != FILE_TO_FILE) {
    280  1.10  mycroft 			if ((curr->fts_namelen +
    281  1.10  mycroft 			    to.target_end - to.p_path + 1) > MAXPATHLEN) {
    282  1.10  mycroft 				warnx("%s/%s: name too long (not copied)",
    283  1.10  mycroft 				    to.p_path, curr->fts_name);
    284  1.10  mycroft 				rval = 1;
    285  1.10  mycroft 				continue;
    286  1.10  mycroft 			}
    287   1.1      cgd 
    288  1.10  mycroft 			/*
    289  1.10  mycroft 			 * Need to remember the roots of traversals to create
    290  1.10  mycroft 			 * correct pathnames.  If there's a directory being
    291  1.10  mycroft 			 * copied to a non-existent directory, e.g.
    292  1.10  mycroft 			 *	cp -R a/dir noexist
    293  1.10  mycroft 			 * the resulting path name should be noexist/foo, not
    294  1.10  mycroft 			 * noexist/dir/foo (where foo is a file in dir), which
    295  1.10  mycroft 			 * is the case where the target exists.
    296  1.10  mycroft 			 *
    297  1.10  mycroft 			 * Also, check for "..".  This is for correct path
    298  1.10  mycroft 			 * concatentation for paths ending in "..", e.g.
    299  1.10  mycroft 			 *	cp -R .. /tmp
    300  1.10  mycroft 			 * Paths ending in ".." are changed to ".".  This is
    301  1.10  mycroft 			 * tricky, but seems the easiest way to fix the problem.
    302  1.10  mycroft 			 *
    303  1.10  mycroft 			 * XXX
    304  1.10  mycroft 			 * Since the first level MUST be FTS_ROOTLEVEL, base
    305  1.10  mycroft 			 * is always initialized.
    306  1.10  mycroft 			 */
    307  1.10  mycroft 			if (curr->fts_level == FTS_ROOTLEVEL)
    308  1.10  mycroft 				if (type != DIR_TO_DNE) {
    309  1.10  mycroft 					p = strrchr(curr->fts_path, '/');
    310  1.10  mycroft 					base = (p == NULL) ? 0 :
    311  1.10  mycroft 					    (int)(p - curr->fts_path + 1);
    312  1.10  mycroft 
    313  1.10  mycroft 					if (!strcmp(&curr->fts_path[base],
    314  1.10  mycroft 					    ".."))
    315  1.10  mycroft 						base += 1;
    316  1.10  mycroft 				} else
    317  1.10  mycroft 					base = curr->fts_pathlen;
    318  1.10  mycroft 
    319  1.10  mycroft 			if (to.target_end[-1] != '/') {
    320  1.10  mycroft 				*to.target_end = '/';
    321  1.10  mycroft 				*(to.target_end + 1) = 0;
    322  1.10  mycroft 			}
    323  1.10  mycroft 			p = &curr->fts_path[base];
    324  1.10  mycroft 			nlen = curr->fts_pathlen - base;
    325   1.8      jtc 
    326  1.10  mycroft 			(void)strncat(to.target_end + 1, p, nlen);
    327  1.10  mycroft 			to.p_end = to.target_end + nlen + 1;
    328  1.10  mycroft 			*to.p_end = 0;
    329  1.10  mycroft 			STRIP_TRAILING_SLASH(to);
    330  1.10  mycroft 		}
    331  1.10  mycroft 
    332  1.10  mycroft 		/* Not an error but need to remember it happened */
    333  1.10  mycroft 		if (stat(to.p_path, &to_stat) == -1)
    334  1.10  mycroft 			dne = 1;
    335  1.10  mycroft 		else {
    336  1.10  mycroft 			if (to_stat.st_dev == curr->fts_statp->st_dev &&
    337  1.10  mycroft 			    to_stat.st_ino == curr->fts_statp->st_ino) {
    338  1.10  mycroft 				warnx("%s and %s are identical (not copied).",
    339  1.10  mycroft 				    to.p_path, curr->fts_path);
    340  1.10  mycroft 				rval = 1;
    341  1.10  mycroft 				if (S_ISDIR(curr->fts_statp->st_mode))
    342  1.10  mycroft 					(void)fts_set(ftsp, curr, FTS_SKIP);
    343  1.10  mycroft 				continue;
    344  1.10  mycroft 			}
    345  1.10  mycroft 			if (!S_ISDIR(curr->fts_statp->st_mode) &&
    346  1.10  mycroft 			    S_ISDIR(to_stat.st_mode)) {
    347  1.10  mycroft 				warnx("cannot overwrite directory %s with non-directory %s.",
    348  1.10  mycroft 				    to.p_path, curr->fts_path);
    349  1.10  mycroft 				rval = 1;
    350  1.10  mycroft 				continue;
    351  1.10  mycroft 			}
    352  1.10  mycroft 			dne = 0;
    353   1.1      cgd 		}
    354   1.1      cgd 
    355  1.10  mycroft 		switch (curr->fts_statp->st_mode & S_IFMT) {
    356  1.10  mycroft 		case S_IFLNK:
    357  1.10  mycroft 			if (copy_link(curr, !dne))
    358  1.10  mycroft 				rval = 1;
    359  1.10  mycroft 			break;
    360  1.10  mycroft 		case S_IFDIR:
    361  1.10  mycroft 			if (!Rflag && !rflag) {
    362  1.10  mycroft 				warnx("%s is a directory (not copied).",
    363  1.10  mycroft 				    curr->fts_path);
    364  1.10  mycroft 				(void)fts_set(ftsp, curr, FTS_SKIP);
    365  1.10  mycroft 				rval = 1;
    366  1.10  mycroft 				break;
    367  1.10  mycroft 			}
    368   1.1      cgd 			/*
    369   1.1      cgd 			 * If the directory doesn't exist, create the new
    370   1.1      cgd 			 * one with the from file mode plus owner RWX bits,
    371   1.1      cgd 			 * modified by the umask.  Trade-off between being
    372   1.1      cgd 			 * able to write the directory (if from directory is
    373   1.1      cgd 			 * 555) and not causing a permissions race.  If the
    374  1.10  mycroft 			 * umask blocks owner writes, we fail..
    375   1.1      cgd 			 */
    376  1.10  mycroft 			if (dne) {
    377  1.10  mycroft 				if (mkdir(to.p_path,
    378  1.10  mycroft 				    curr->fts_statp->st_mode | S_IRWXU) < 0)
    379  1.10  mycroft 					err(1, "%s", to.p_path);
    380  1.10  mycroft 			} else if (!S_ISDIR(to_stat.st_mode)) {
    381  1.10  mycroft 				errno = ENOTDIR;
    382  1.10  mycroft 				err(1, "%s: %s", to.p_path);
    383   1.1      cgd 			}
    384  1.10  mycroft 			/*
    385  1.10  mycroft 			 * If not -p and directory didn't exist, set it to be
    386  1.10  mycroft 			 * the same as the from directory, umodified by the
    387  1.10  mycroft                          * umask; arguably wrong, but it's been that way
    388  1.10  mycroft                          * forever.
    389  1.10  mycroft 			 */
    390  1.10  mycroft 			if (pflag && setfile(curr->fts_statp, 0))
    391  1.10  mycroft 				rval = 1;
    392  1.10  mycroft 			else if (dne)
    393  1.10  mycroft 				(void)chmod(to.p_path,
    394  1.10  mycroft 				    curr->fts_statp->st_mode);
    395  1.10  mycroft 			break;
    396  1.10  mycroft 		case S_IFBLK:
    397  1.10  mycroft 		case S_IFCHR:
    398  1.10  mycroft 			if (Rflag) {
    399  1.10  mycroft 				if (copy_special(curr->fts_statp, !dne))
    400  1.10  mycroft 					rval = 1;
    401  1.10  mycroft 			} else
    402  1.10  mycroft 				if (copy_file(curr, dne))
    403  1.10  mycroft 					rval = 1;
    404  1.10  mycroft 			break;
    405  1.10  mycroft 		case S_IFIFO:
    406  1.10  mycroft 			if (Rflag)
    407  1.10  mycroft 				if (copy_fifo(curr->fts_statp, !dne))
    408  1.10  mycroft 					rval = 1;
    409  1.10  mycroft 			else
    410  1.10  mycroft 				if (copy_file(curr, dne))
    411  1.10  mycroft 					rval = 1;
    412  1.10  mycroft 			break;
    413  1.10  mycroft 		default:
    414  1.10  mycroft 			if (copy_file(curr, dne))
    415  1.10  mycroft 				rval = 1;
    416  1.10  mycroft 			break;
    417   1.1      cgd 		}
    418   1.1      cgd 	}
    419  1.10  mycroft 	if (errno)
    420  1.10  mycroft 		err(1, "fts_read");
    421  1.10  mycroft 	return (rval);
    422   1.1      cgd }
    423   1.1      cgd 
    424  1.10  mycroft /*
    425  1.10  mycroft  * mastercmp --
    426  1.10  mycroft  *	The comparison function for the copy order.  The order is to copy
    427  1.10  mycroft  *	non-directory files before directory files.  The reason for this
    428  1.10  mycroft  *	is because files tend to be in the same cylinder group as their
    429  1.10  mycroft  *	parent directory, whereas directories tend not to be.  Copying the
    430  1.10  mycroft  *	files first reduces seeking.
    431  1.10  mycroft  */
    432  1.10  mycroft int
    433  1.10  mycroft mastercmp(a, b)
    434  1.10  mycroft 	const FTSENT **a, **b;
    435   1.1      cgd {
    436  1.10  mycroft 	int a_info, b_info;
    437   1.1      cgd 
    438  1.10  mycroft 	a_info = (*a)->fts_info;
    439  1.10  mycroft 	if (a_info == FTS_ERR || a_info == FTS_NS || a_info == FTS_DNR)
    440  1.10  mycroft 		return (0);
    441  1.10  mycroft 	b_info = (*b)->fts_info;
    442  1.10  mycroft 	if (b_info == FTS_ERR || b_info == FTS_NS || b_info == FTS_DNR)
    443  1.10  mycroft 		return (0);
    444  1.10  mycroft 	if (a_info == FTS_D)
    445  1.10  mycroft 		return (-1);
    446  1.10  mycroft 	if (b_info == FTS_D)
    447  1.10  mycroft 		return (1);
    448  1.10  mycroft 	return (0);
    449   1.1      cgd }
    450