Home | History | Annotate | Line # | Download | only in cp
cp.c revision 1.6
      1  1.1      cgd /*
      2  1.1      cgd  * Copyright (c) 1988 The Regents of the University of California.
      3  1.1      cgd  * 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.1      cgd char copyright[] =
     39  1.1      cgd "@(#) Copyright (c) 1988 The Regents of the University of California.\n\
     40  1.1      cgd  All rights reserved.\n";
     41  1.1      cgd #endif /* not lint */
     42  1.1      cgd 
     43  1.1      cgd #ifndef lint
     44  1.5  mycroft /*static char sccsid[] = "from: @(#)cp.c	5.26 (Berkeley) 10/27/91";*/
     45  1.6  mycroft static char rcsid[] = "$Id: cp.c,v 1.6 1993/10/30 13:06:46 mycroft Exp $";
     46  1.1      cgd #endif /* not lint */
     47  1.1      cgd 
     48  1.1      cgd /*
     49  1.1      cgd  * cp copies source files to target files.
     50  1.1      cgd  *
     51  1.1      cgd  * The global PATH_T structures "to" and "from" always contain paths to the
     52  1.1      cgd  * current source and target files, respectively.  Since cp does not change
     53  1.1      cgd  * directories, these paths can be either absolute or dot-realative.
     54  1.1      cgd  *
     55  1.1      cgd  * The basic algorithm is to initialize "to" and "from", and then call the
     56  1.1      cgd  * recursive copy() function to do the actual work.  If "from" is a file,
     57  1.1      cgd  * copy copies the data.  If "from" is a directory, copy creates the
     58  1.1      cgd  * corresponding "to" directory, and calls itself recursively on all of
     59  1.1      cgd  * the entries in the "from" directory.
     60  1.1      cgd  */
     61  1.1      cgd 
     62  1.1      cgd #include <sys/param.h>
     63  1.1      cgd #include <sys/stat.h>
     64  1.5  mycroft #include <sys/mman.h>
     65  1.1      cgd #include <sys/time.h>
     66  1.1      cgd #include <dirent.h>
     67  1.1      cgd #include <fcntl.h>
     68  1.1      cgd #include <errno.h>
     69  1.1      cgd #include <unistd.h>
     70  1.1      cgd #include <stdio.h>
     71  1.1      cgd #include <stdlib.h>
     72  1.1      cgd #include <string.h>
     73  1.5  mycroft #include "extern.h"
     74  1.1      cgd 
     75  1.5  mycroft static void copy __P((void));
     76  1.5  mycroft static void copy_dir __P((void));
     77  1.5  mycroft static void copy_fifo __P((struct stat *, int));
     78  1.5  mycroft static void copy_file __P((struct stat *, int));
     79  1.5  mycroft static void copy_link __P((int));
     80  1.5  mycroft static void copy_special __P((struct stat *, int));
     81  1.5  mycroft static void setfile __P((struct stat *, int));
     82  1.5  mycroft static void usage __P((void));
     83  1.5  mycroft 
     84  1.1      cgd PATH_T from = { from.p_path, "" };
     85  1.1      cgd PATH_T to = { to.p_path, "" };
     86  1.1      cgd 
     87  1.1      cgd uid_t myuid;
     88  1.1      cgd int exit_val, myumask;
     89  1.1      cgd int iflag, pflag, orflag, rflag;
     90  1.1      cgd int (*statfcn)();
     91  1.5  mycroft char *progname;
     92  1.1      cgd 
     93  1.1      cgd main(argc, argv)
     94  1.1      cgd 	int argc;
     95  1.1      cgd 	char **argv;
     96  1.1      cgd {
     97  1.1      cgd 	extern int optind;
     98  1.1      cgd 	struct stat to_stat;
     99  1.1      cgd 	register int c, r;
    100  1.1      cgd 	int symfollow, lstat(), stat();
    101  1.1      cgd 	char *old_to, *p;
    102  1.1      cgd 
    103  1.1      cgd 	/*
    104  1.1      cgd 	 * The utility cp(1) is used by mv(1) -- except for usage statements,
    105  1.1      cgd 	 * print the "called as" program name.
    106  1.1      cgd 	 */
    107  1.1      cgd 	progname = (p = rindex(*argv,'/')) ? ++p : *argv;
    108  1.1      cgd 
    109  1.1      cgd 	symfollow = 0;
    110  1.1      cgd 	while ((c = getopt(argc, argv, "Rfhipr")) != EOF) {
    111  1.1      cgd 	switch ((char)c) {
    112  1.1      cgd 		case 'f':
    113  1.1      cgd 			iflag = 0;
    114  1.1      cgd 			break;
    115  1.1      cgd 		case 'h':
    116  1.1      cgd 			symfollow = 1;
    117  1.1      cgd 			break;
    118  1.1      cgd 		case 'i':
    119  1.1      cgd 			iflag = isatty(fileno(stdin));
    120  1.1      cgd 			break;
    121  1.1      cgd 		case 'p':
    122  1.1      cgd 			pflag = 1;
    123  1.1      cgd 			break;
    124  1.1      cgd 		case 'R':
    125  1.1      cgd 			rflag = 1;
    126  1.1      cgd 			break;
    127  1.1      cgd 		case 'r':
    128  1.1      cgd 			orflag = 1;
    129  1.1      cgd 			break;
    130  1.1      cgd 		case '?':
    131  1.1      cgd 		default:
    132  1.1      cgd 			usage();
    133  1.1      cgd 			break;
    134  1.1      cgd 		}
    135  1.1      cgd 	}
    136  1.1      cgd 	argc -= optind;
    137  1.1      cgd 	argv += optind;
    138  1.1      cgd 
    139  1.1      cgd 	if (argc < 2)
    140  1.1      cgd 		usage();
    141  1.1      cgd 
    142  1.1      cgd 	if (rflag && orflag) {
    143  1.1      cgd 		(void)fprintf(stderr,
    144  1.1      cgd 		    "cp: the -R and -r options are mutually exclusive.\n");
    145  1.1      cgd 		exit(1);
    146  1.1      cgd 	}
    147  1.1      cgd 
    148  1.1      cgd 	myuid = getuid();
    149  1.1      cgd 
    150  1.1      cgd 	/* copy the umask for explicit mode setting */
    151  1.1      cgd 	myumask = umask(0);
    152  1.1      cgd 	(void)umask(myumask);
    153  1.1      cgd 
    154  1.1      cgd 	/* consume last argument first. */
    155  1.1      cgd 	if (!path_set(&to, argv[--argc]))
    156  1.1      cgd 		exit(1);
    157  1.1      cgd 
    158  1.1      cgd 	statfcn = symfollow || !rflag ? stat : lstat;
    159  1.1      cgd 
    160  1.1      cgd 	/*
    161  1.1      cgd 	 * Cp has two distinct cases:
    162  1.1      cgd 	 *
    163  1.1      cgd 	 * % cp [-rip] source target
    164  1.1      cgd 	 * % cp [-rip] source1 ... directory
    165  1.1      cgd 	 *
    166  1.1      cgd 	 * In both cases, source can be either a file or a directory.
    167  1.1      cgd 	 *
    168  1.1      cgd 	 * In (1), the target becomes a copy of the source. That is, if the
    169  1.1      cgd 	 * source is a file, the target will be a file, and likewise for
    170  1.1      cgd 	 * directories.
    171  1.1      cgd 	 *
    172  1.1      cgd 	 * In (2), the real target is not directory, but "directory/source".
    173  1.1      cgd 	 */
    174  1.1      cgd 
    175  1.1      cgd 	r = stat(to.p_path, &to_stat);
    176  1.1      cgd 	if (r == -1 && errno != ENOENT) {
    177  1.5  mycroft 		err("%s: %s", to.p_path, strerror(errno));
    178  1.1      cgd 		exit(1);
    179  1.1      cgd 	}
    180  1.1      cgd 	if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
    181  1.1      cgd 		/*
    182  1.1      cgd 		 * Case (1).  Target is not a directory.
    183  1.1      cgd 		 */
    184  1.1      cgd 		if (argc > 1) {
    185  1.1      cgd 			usage();
    186  1.1      cgd 			exit(1);
    187  1.1      cgd 		}
    188  1.1      cgd 		if (!path_set(&from, *argv))
    189  1.1      cgd 			exit(1);
    190  1.1      cgd 		copy();
    191  1.1      cgd 	}
    192  1.1      cgd 	else {
    193  1.1      cgd 		/*
    194  1.1      cgd 		 * Case (2).  Target is a directory.
    195  1.1      cgd 		 */
    196  1.1      cgd 		for (;; ++argv) {
    197  1.5  mycroft 			if (!path_set(&from, *argv))
    198  1.1      cgd 				continue;
    199  1.5  mycroft 			if (!(old_to =
    200  1.5  mycroft 			    path_append(&to, path_basename(&from), -1)))
    201  1.1      cgd 				continue;
    202  1.1      cgd 			copy();
    203  1.1      cgd 			if (!--argc)
    204  1.1      cgd 				break;
    205  1.1      cgd 			path_restore(&to, old_to);
    206  1.1      cgd 		}
    207  1.1      cgd 	}
    208  1.1      cgd 	exit(exit_val);
    209  1.1      cgd }
    210  1.1      cgd 
    211  1.1      cgd /* copy file or directory at "from" to "to". */
    212  1.5  mycroft static void
    213  1.1      cgd copy()
    214  1.1      cgd {
    215  1.1      cgd 	struct stat from_stat, to_stat;
    216  1.1      cgd 	int dne, statval;
    217  1.1      cgd 
    218  1.1      cgd 	statval = statfcn(from.p_path, &from_stat);
    219  1.1      cgd 	if (statval == -1) {
    220  1.5  mycroft 		err("%s: %s", from.p_path, strerror(errno));
    221  1.1      cgd 		return;
    222  1.1      cgd 	}
    223  1.1      cgd 
    224  1.1      cgd 	/* not an error, but need to remember it happened */
    225  1.1      cgd 	if (stat(to.p_path, &to_stat) == -1)
    226  1.1      cgd 		dne = 1;
    227  1.1      cgd 	else {
    228  1.1      cgd 		if (to_stat.st_dev == from_stat.st_dev &&
    229  1.1      cgd 		    to_stat.st_ino == from_stat.st_ino) {
    230  1.1      cgd 			(void)fprintf(stderr,
    231  1.1      cgd 			    "%s: %s and %s are identical (not copied).\n",
    232  1.1      cgd 			    progname, to.p_path, from.p_path);
    233  1.1      cgd 			exit_val = 1;
    234  1.1      cgd 			return;
    235  1.1      cgd 		}
    236  1.1      cgd 		dne = 0;
    237  1.1      cgd 	}
    238  1.1      cgd 
    239  1.1      cgd 	switch(from_stat.st_mode & S_IFMT) {
    240  1.1      cgd 	case S_IFLNK:
    241  1.1      cgd 		copy_link(!dne);
    242  1.1      cgd 		return;
    243  1.1      cgd 	case S_IFDIR:
    244  1.1      cgd 		if (!rflag && !orflag) {
    245  1.1      cgd 			(void)fprintf(stderr,
    246  1.1      cgd 			    "%s: %s is a directory (not copied).\n",
    247  1.1      cgd 			    progname, from.p_path);
    248  1.1      cgd 			exit_val = 1;
    249  1.1      cgd 			return;
    250  1.1      cgd 		}
    251  1.1      cgd 		if (dne) {
    252  1.1      cgd 			/*
    253  1.1      cgd 			 * If the directory doesn't exist, create the new
    254  1.1      cgd 			 * one with the from file mode plus owner RWX bits,
    255  1.1      cgd 			 * modified by the umask.  Trade-off between being
    256  1.1      cgd 			 * able to write the directory (if from directory is
    257  1.1      cgd 			 * 555) and not causing a permissions race.  If the
    258  1.1      cgd 			 * umask blocks owner writes cp fails.
    259  1.1      cgd 			 */
    260  1.1      cgd 			if (mkdir(to.p_path, from_stat.st_mode|S_IRWXU) < 0) {
    261  1.5  mycroft 				err("%s: %s", to.p_path, strerror(errno));
    262  1.1      cgd 				return;
    263  1.1      cgd 			}
    264  1.1      cgd 		}
    265  1.5  mycroft 		else if (!S_ISDIR(to_stat.st_mode)) {
    266  1.1      cgd 			(void)fprintf(stderr, "%s: %s: not a directory.\n",
    267  1.1      cgd 			    progname, to.p_path);
    268  1.1      cgd 			return;
    269  1.1      cgd 		}
    270  1.1      cgd 		copy_dir();
    271  1.1      cgd 		/*
    272  1.1      cgd 		 * If not -p and directory didn't exist, set it to be the
    273  1.1      cgd 		 * same as the from directory, umodified by the umask;
    274  1.1      cgd 		 * arguably wrong, but it's been that way forever.
    275  1.1      cgd 		 */
    276  1.1      cgd 		if (pflag)
    277  1.1      cgd 			setfile(&from_stat, 0);
    278  1.1      cgd 		else if (dne)
    279  1.1      cgd 			(void)chmod(to.p_path, from_stat.st_mode);
    280  1.1      cgd 		return;
    281  1.1      cgd 	case S_IFCHR:
    282  1.1      cgd 	case S_IFBLK:
    283  1.1      cgd 		if (rflag) {
    284  1.1      cgd 			copy_special(&from_stat, !dne);
    285  1.1      cgd 			return;
    286  1.1      cgd 		}
    287  1.1      cgd 		break;
    288  1.1      cgd 	case S_IFIFO:
    289  1.1      cgd 		if (rflag) {
    290  1.1      cgd 			copy_fifo(&from_stat, !dne);
    291  1.1      cgd 			return;
    292  1.1      cgd 		}
    293  1.1      cgd 		break;
    294  1.1      cgd 	}
    295  1.1      cgd 	copy_file(&from_stat, dne);
    296  1.1      cgd }
    297  1.1      cgd 
    298  1.5  mycroft static void
    299  1.1      cgd copy_file(fs, dne)
    300  1.1      cgd 	struct stat *fs;
    301  1.1      cgd 	int dne;
    302  1.1      cgd {
    303  1.5  mycroft 	static char buf[MAXBSIZE];
    304  1.1      cgd 	register int from_fd, to_fd, rcount, wcount;
    305  1.1      cgd 	struct stat to_stat;
    306  1.5  mycroft 	char *p;
    307  1.1      cgd 
    308  1.1      cgd 	if ((from_fd = open(from.p_path, O_RDONLY, 0)) == -1) {
    309  1.5  mycroft 		err("%s: %s", from.p_path, strerror(errno));
    310  1.1      cgd 		return;
    311  1.1      cgd 	}
    312  1.1      cgd 
    313  1.1      cgd 	/*
    314  1.1      cgd 	 * If the file exists and we're interactive, verify with the user.
    315  1.1      cgd 	 * If the file DNE, set the mode to be the from file, minus setuid
    316  1.1      cgd 	 * bits, modified by the umask; arguably wrong, but it makes copying
    317  1.1      cgd 	 * executables work right and it's been that way forever.  (The
    318  1.1      cgd 	 * other choice is 666 or'ed with the execute bits on the from file
    319  1.1      cgd 	 * modified by the umask.)
    320  1.1      cgd 	 */
    321  1.1      cgd 	if (!dne) {
    322  1.1      cgd 		if (iflag) {
    323  1.1      cgd 			int checkch, ch;
    324  1.1      cgd 
    325  1.1      cgd 			(void)fprintf(stderr, "overwrite %s? ", to.p_path);
    326  1.1      cgd 			checkch = ch = getchar();
    327  1.1      cgd 			while (ch != '\n' && ch != EOF)
    328  1.1      cgd 				ch = getchar();
    329  1.1      cgd 			if (checkch != 'y') {
    330  1.1      cgd 				(void)close(from_fd);
    331  1.1      cgd 				return;
    332  1.1      cgd 			}
    333  1.1      cgd 		}
    334  1.1      cgd 		to_fd = open(to.p_path, O_WRONLY|O_TRUNC, 0);
    335  1.1      cgd 	} else
    336  1.1      cgd 		to_fd = open(to.p_path, O_WRONLY|O_CREAT|O_TRUNC,
    337  1.1      cgd 		    fs->st_mode & ~(S_ISUID|S_ISGID));
    338  1.1      cgd 
    339  1.1      cgd 	if (to_fd == -1) {
    340  1.5  mycroft 		err("%s: %s", to.p_path, strerror(errno));
    341  1.1      cgd 		(void)close(from_fd);
    342  1.1      cgd 		return;
    343  1.1      cgd 	}
    344  1.1      cgd 
    345  1.5  mycroft 	/*
    346  1.5  mycroft 	 * Mmap and write if less than 8M (the limit is so we don't totally
    347  1.5  mycroft 	 * trash memory on big files.  This is really a minor hack, but it
    348  1.5  mycroft 	 * wins some CPU back.
    349  1.5  mycroft 	 */
    350  1.5  mycroft 	if (fs->st_size <= 8 * 1048576) {
    351  1.5  mycroft 		if ((p = mmap(NULL, fs->st_size, PROT_READ,
    352  1.5  mycroft 		    MAP_FILE, from_fd, (off_t)0)) == (char *)-1)
    353  1.5  mycroft 			err("%s: %s", from.p_path, strerror(errno));
    354  1.5  mycroft 		if (write(to_fd, p, fs->st_size) != fs->st_size)
    355  1.5  mycroft 			err("%s: %s", to.p_path, strerror(errno));
    356  1.6  mycroft 		if (munmap(p, fs->st_size) < 0)
    357  1.6  mycroft 			err("%s: %s", from.p_path, strerror(errno));
    358  1.5  mycroft 	} else {
    359  1.5  mycroft 		while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
    360  1.5  mycroft 			wcount = write(to_fd, buf, rcount);
    361  1.5  mycroft 			if (rcount != wcount || wcount == -1) {
    362  1.5  mycroft 				err("%s: %s", to.p_path, strerror(errno));
    363  1.5  mycroft 				break;
    364  1.5  mycroft 			}
    365  1.1      cgd 		}
    366  1.5  mycroft 		if (rcount < 0)
    367  1.5  mycroft 			err("%s: %s", from.p_path, strerror(errno));
    368  1.1      cgd 	}
    369  1.1      cgd 	if (pflag)
    370  1.1      cgd 		setfile(fs, to_fd);
    371  1.1      cgd 	/*
    372  1.1      cgd 	 * If the source was setuid or setgid, lose the bits unless the
    373  1.1      cgd 	 * copy is owned by the same user and group.
    374  1.1      cgd 	 */
    375  1.1      cgd 	else if (fs->st_mode & (S_ISUID|S_ISGID) && fs->st_uid == myuid)
    376  1.1      cgd 		if (fstat(to_fd, &to_stat))
    377  1.5  mycroft 			err("%s: %s", to.p_path, strerror(errno));
    378  1.1      cgd #define	RETAINBITS	(S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)
    379  1.1      cgd 		else if (fs->st_gid == to_stat.st_gid && fchmod(to_fd,
    380  1.1      cgd 		    fs->st_mode & RETAINBITS & ~myumask))
    381  1.5  mycroft 			err("%s: %s", to.p_path, strerror(errno));
    382  1.1      cgd 	(void)close(from_fd);
    383  1.1      cgd 	if (close(to_fd))
    384  1.5  mycroft 		err("%s: %s", to.p_path, strerror(errno));
    385  1.1      cgd }
    386  1.1      cgd 
    387  1.5  mycroft static void
    388  1.1      cgd copy_dir()
    389  1.1      cgd {
    390  1.1      cgd 	struct stat from_stat;
    391  1.1      cgd 	struct dirent *dp, **dir_list;
    392  1.1      cgd 	register int dir_cnt, i;
    393  1.1      cgd 	char *old_from, *old_to;
    394  1.1      cgd 
    395  1.1      cgd 	dir_cnt = scandir(from.p_path, &dir_list, NULL, NULL);
    396  1.1      cgd 	if (dir_cnt == -1) {
    397  1.1      cgd 		(void)fprintf(stderr, "%s: can't read directory %s.\n",
    398  1.1      cgd 		    progname, from.p_path);
    399  1.1      cgd 		exit_val = 1;
    400  1.1      cgd 	}
    401  1.1      cgd 
    402  1.1      cgd 	/*
    403  1.1      cgd 	 * Instead of handling directory entries in the order they appear
    404  1.1      cgd 	 * on disk, do non-directory files before directory files.
    405  1.1      cgd 	 * There are two reasons to do directories last.  The first is
    406  1.1      cgd 	 * efficiency.  Files tend to be in the same cylinder group as
    407  1.1      cgd 	 * their parent, whereas directories tend not to be.  Copying files
    408  1.1      cgd 	 * all at once reduces seeking.  Second, deeply nested tree's
    409  1.1      cgd 	 * could use up all the file descriptors if we didn't close one
    410  1.1      cgd 	 * directory before recursivly starting on the next.
    411  1.1      cgd 	 */
    412  1.1      cgd 	/* copy files */
    413  1.1      cgd 	for (i = 0; i < dir_cnt; ++i) {
    414  1.1      cgd 		dp = dir_list[i];
    415  1.1      cgd 		if (dp->d_namlen <= 2 && dp->d_name[0] == '.'
    416  1.1      cgd 		    && (dp->d_name[1] == NULL || dp->d_name[1] == '.'))
    417  1.1      cgd 			goto done;
    418  1.5  mycroft 		if (!(old_from =
    419  1.5  mycroft 		    path_append(&from, dp->d_name, (int)dp->d_namlen)))
    420  1.1      cgd 			goto done;
    421  1.1      cgd 
    422  1.1      cgd 		if (statfcn(from.p_path, &from_stat) < 0) {
    423  1.5  mycroft 			err("%s: %s", dp->d_name, strerror(errno));
    424  1.1      cgd 			path_restore(&from, old_from);
    425  1.1      cgd 			goto done;
    426  1.1      cgd 		}
    427  1.1      cgd 		if (S_ISDIR(from_stat.st_mode)) {
    428  1.1      cgd 			path_restore(&from, old_from);
    429  1.1      cgd 			continue;
    430  1.1      cgd 		}
    431  1.5  mycroft 		if (old_to = path_append(&to, dp->d_name, (int)dp->d_namlen)) {
    432  1.1      cgd 			copy();
    433  1.1      cgd 			path_restore(&to, old_to);
    434  1.5  mycroft 		}
    435  1.1      cgd 		path_restore(&from, old_from);
    436  1.1      cgd done:		dir_list[i] = NULL;
    437  1.5  mycroft 		free(dp);
    438  1.1      cgd 	}
    439  1.1      cgd 
    440  1.1      cgd 	/* copy directories */
    441  1.1      cgd 	for (i = 0; i < dir_cnt; ++i) {
    442  1.1      cgd 		dp = dir_list[i];
    443  1.1      cgd 		if (!dp)
    444  1.1      cgd 			continue;
    445  1.5  mycroft 		if (!(old_from =
    446  1.5  mycroft 		    path_append(&from, dp->d_name, (int)dp->d_namlen))) {
    447  1.5  mycroft 			free(dp);
    448  1.1      cgd 			continue;
    449  1.1      cgd 		}
    450  1.5  mycroft 		if (!(old_to =
    451  1.5  mycroft 		    path_append(&to, dp->d_name, (int)dp->d_namlen))) {
    452  1.5  mycroft 			free(dp);
    453  1.1      cgd 			path_restore(&from, old_from);
    454  1.1      cgd 			continue;
    455  1.1      cgd 		}
    456  1.1      cgd 		copy();
    457  1.5  mycroft 		free(dp);
    458  1.1      cgd 		path_restore(&from, old_from);
    459  1.1      cgd 		path_restore(&to, old_to);
    460  1.1      cgd 	}
    461  1.5  mycroft 	free(dir_list);
    462  1.1      cgd }
    463  1.1      cgd 
    464  1.5  mycroft static void
    465  1.1      cgd copy_link(exists)
    466  1.1      cgd 	int exists;
    467  1.1      cgd {
    468  1.1      cgd 	int len;
    469  1.1      cgd 	char link[MAXPATHLEN];
    470  1.1      cgd 
    471  1.1      cgd 	if ((len = readlink(from.p_path, link, sizeof(link))) == -1) {
    472  1.5  mycroft 		err("readlink: %s: %s", from.p_path, strerror(errno));
    473  1.1      cgd 		return;
    474  1.1      cgd 	}
    475  1.1      cgd 	link[len] = '\0';
    476  1.1      cgd 	if (exists && unlink(to.p_path)) {
    477  1.5  mycroft 		err("unlink: %s: %s", to.p_path, strerror(errno));
    478  1.1      cgd 		return;
    479  1.1      cgd 	}
    480  1.1      cgd 	if (symlink(link, to.p_path)) {
    481  1.5  mycroft 		err("symlink: %s: %s", link, strerror(errno));
    482  1.1      cgd 		return;
    483  1.1      cgd 	}
    484  1.1      cgd }
    485  1.1      cgd 
    486  1.5  mycroft static void
    487  1.1      cgd copy_fifo(from_stat, exists)
    488  1.1      cgd 	struct stat *from_stat;
    489  1.1      cgd 	int exists;
    490  1.1      cgd {
    491  1.1      cgd 	if (exists && unlink(to.p_path)) {
    492  1.5  mycroft 		err("unlink: %s: %s", to.p_path, strerror(errno));
    493  1.1      cgd 		return;
    494  1.1      cgd 	}
    495  1.1      cgd 	if (mkfifo(to.p_path, from_stat->st_mode)) {
    496  1.5  mycroft 		err("mkfifo: %s: %s", to.p_path, strerror(errno));
    497  1.1      cgd 		return;
    498  1.1      cgd 	}
    499  1.1      cgd 	if (pflag)
    500  1.1      cgd 		setfile(from_stat, 0);
    501  1.1      cgd }
    502  1.1      cgd 
    503  1.5  mycroft static void
    504  1.1      cgd copy_special(from_stat, exists)
    505  1.1      cgd 	struct stat *from_stat;
    506  1.1      cgd 	int exists;
    507  1.1      cgd {
    508  1.1      cgd 	if (exists && unlink(to.p_path)) {
    509  1.5  mycroft 		err("unlink: %s: %s", to.p_path, strerror(errno));
    510  1.1      cgd 		return;
    511  1.1      cgd 	}
    512  1.1      cgd 	if (mknod(to.p_path, from_stat->st_mode,  from_stat->st_rdev)) {
    513  1.5  mycroft 		err("mknod: %s: %s", to.p_path, strerror(errno));
    514  1.1      cgd 		return;
    515  1.1      cgd 	}
    516  1.1      cgd 	if (pflag)
    517  1.1      cgd 		setfile(from_stat, 0);
    518  1.1      cgd }
    519  1.1      cgd 
    520  1.5  mycroft static void
    521  1.1      cgd setfile(fs, fd)
    522  1.1      cgd 	register struct stat *fs;
    523  1.1      cgd 	int fd;
    524  1.1      cgd {
    525  1.1      cgd 	static struct timeval tv[2];
    526  1.1      cgd 
    527  1.1      cgd 	fs->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
    528  1.1      cgd 
    529  1.1      cgd 	tv[0].tv_sec = fs->st_atime;
    530  1.1      cgd 	tv[1].tv_sec = fs->st_mtime;
    531  1.5  mycroft 	if (utimes(to.p_path, tv))
    532  1.5  mycroft 		err("utimes: %s: %s", to.p_path, strerror(errno));
    533  1.1      cgd 	/*
    534  1.1      cgd 	 * Changing the ownership probably won't succeed, unless we're root
    535  1.1      cgd 	 * or POSIX_CHOWN_RESTRICTED is not set.  Set uid/gid before setting
    536  1.1      cgd 	 * the mode; current BSD behavior is to remove all setuid bits on
    537  1.1      cgd 	 * chown.  If chown fails, lose setuid/setgid bits.
    538  1.1      cgd 	 */
    539  1.1      cgd 	if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
    540  1.1      cgd 	    chown(to.p_path, fs->st_uid, fs->st_gid)) {
    541  1.5  mycroft 		if (errno != EPERM)
    542  1.5  mycroft 			err("chown: %s: %s", to.p_path, strerror(errno));
    543  1.1      cgd 		fs->st_mode &= ~(S_ISUID|S_ISGID);
    544  1.1      cgd 	}
    545  1.5  mycroft 	if (fd ? fchmod(fd, fs->st_mode) : chmod(to.p_path, fs->st_mode))
    546  1.5  mycroft 		err("chown: %s: %s", to.p_path, strerror(errno));
    547  1.1      cgd }
    548  1.1      cgd 
    549  1.5  mycroft static void
    550  1.5  mycroft usage()
    551  1.1      cgd {
    552  1.5  mycroft 	(void)fprintf(stderr,
    553  1.5  mycroft "usage: cp [-Rfhip] src target;\n       cp [-Rfhip] src1 ... srcN directory\n");
    554  1.5  mycroft 	exit(1);
    555  1.1      cgd }
    556  1.1      cgd 
    557  1.5  mycroft #if __STDC__
    558  1.5  mycroft #include <stdarg.h>
    559  1.5  mycroft #else
    560  1.5  mycroft #include <varargs.h>
    561  1.5  mycroft #endif
    562  1.5  mycroft 
    563  1.5  mycroft void
    564  1.5  mycroft #if __STDC__
    565  1.5  mycroft err(const char *fmt, ...)
    566  1.5  mycroft #else
    567  1.5  mycroft err(fmt, va_alist)
    568  1.5  mycroft 	char *fmt;
    569  1.5  mycroft         va_dcl
    570  1.5  mycroft #endif
    571  1.1      cgd {
    572  1.5  mycroft 	va_list ap;
    573  1.5  mycroft #if __STDC__
    574  1.5  mycroft 	va_start(ap, fmt);
    575  1.5  mycroft #else
    576  1.5  mycroft 	va_start(ap);
    577  1.5  mycroft #endif
    578  1.5  mycroft 	(void)fprintf(stderr, "%s: ", progname);
    579  1.5  mycroft 	(void)vfprintf(stderr, fmt, ap);
    580  1.5  mycroft 	va_end(ap);
    581  1.5  mycroft 	(void)fprintf(stderr, "\n");
    582  1.5  mycroft 	exit_val = 1;
    583  1.1      cgd }
    584