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