xref: /minix3/bin/cp/utils.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /* $NetBSD: utils.c,v 1.44 2015/03/03 00:20:38 enami Exp $ */
25da400ecSZachary Storer 
35da400ecSZachary Storer /*-
45da400ecSZachary Storer  * Copyright (c) 1991, 1993, 1994
55da400ecSZachary Storer  *	The Regents of the University of California.  All rights reserved.
65da400ecSZachary Storer  *
75da400ecSZachary Storer  * Redistribution and use in source and binary forms, with or without
85da400ecSZachary Storer  * modification, are permitted provided that the following conditions
95da400ecSZachary Storer  * are met:
105da400ecSZachary Storer  * 1. Redistributions of source code must retain the above copyright
115da400ecSZachary Storer  *    notice, this list of conditions and the following disclaimer.
125da400ecSZachary Storer  * 2. Redistributions in binary form must reproduce the above copyright
135da400ecSZachary Storer  *    notice, this list of conditions and the following disclaimer in the
145da400ecSZachary Storer  *    documentation and/or other materials provided with the distribution.
155da400ecSZachary Storer  * 3. Neither the name of the University nor the names of its contributors
165da400ecSZachary Storer  *    may be used to endorse or promote products derived from this software
175da400ecSZachary Storer  *    without specific prior written permission.
185da400ecSZachary Storer  *
195da400ecSZachary Storer  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
205da400ecSZachary Storer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
215da400ecSZachary Storer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
225da400ecSZachary Storer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
235da400ecSZachary Storer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
245da400ecSZachary Storer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
255da400ecSZachary Storer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
265da400ecSZachary Storer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
275da400ecSZachary Storer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
285da400ecSZachary Storer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
295da400ecSZachary Storer  * SUCH DAMAGE.
305da400ecSZachary Storer  */
315da400ecSZachary Storer 
325da400ecSZachary Storer #include <sys/cdefs.h>
335da400ecSZachary Storer #ifndef lint
345da400ecSZachary Storer #if 0
355da400ecSZachary Storer static char sccsid[] = "@(#)utils.c	8.3 (Berkeley) 4/1/94";
365da400ecSZachary Storer #else
37*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: utils.c,v 1.44 2015/03/03 00:20:38 enami Exp $");
385da400ecSZachary Storer #endif
395da400ecSZachary Storer #endif /* not lint */
405da400ecSZachary Storer 
415da400ecSZachary Storer #include <sys/mman.h>
425da400ecSZachary Storer #include <sys/param.h>
435da400ecSZachary Storer #include <sys/stat.h>
445da400ecSZachary Storer #include <sys/time.h>
455da400ecSZachary Storer #include <sys/extattr.h>
465da400ecSZachary Storer 
475da400ecSZachary Storer #include <err.h>
485da400ecSZachary Storer #include <errno.h>
495da400ecSZachary Storer #include <fcntl.h>
505da400ecSZachary Storer #include <fts.h>
515da400ecSZachary Storer #include <stdbool.h>
525da400ecSZachary Storer #include <stdio.h>
535da400ecSZachary Storer #include <stdlib.h>
545da400ecSZachary Storer #include <string.h>
555da400ecSZachary Storer #include <unistd.h>
565da400ecSZachary Storer 
575da400ecSZachary Storer #include "extern.h"
585da400ecSZachary Storer 
595da400ecSZachary Storer #define	MMAP_MAX_SIZE	(8 * 1048576)
605da400ecSZachary Storer #define	MMAP_MAX_WRITE	(64 * 1024)
615da400ecSZachary Storer 
625da400ecSZachary Storer int
set_utimes(const char * file,struct stat * fs)635da400ecSZachary Storer set_utimes(const char *file, struct stat *fs)
645da400ecSZachary Storer {
65*0a6a1f1dSLionel Sambuc     struct timespec ts[2];
665da400ecSZachary Storer 
67*0a6a1f1dSLionel Sambuc     ts[0] = fs->st_atimespec;
68*0a6a1f1dSLionel Sambuc     ts[1] = fs->st_mtimespec;
695da400ecSZachary Storer 
70*0a6a1f1dSLionel Sambuc     if (lutimens(file, ts)) {
71*0a6a1f1dSLionel Sambuc 	warn("lutimens: %s", file);
725da400ecSZachary Storer 	return (1);
735da400ecSZachary Storer     }
745da400ecSZachary Storer     return (0);
755da400ecSZachary Storer }
765da400ecSZachary Storer 
775da400ecSZachary Storer struct finfo {
785da400ecSZachary Storer 	const char *from;
795da400ecSZachary Storer 	const char *to;
805da400ecSZachary Storer 	size_t size;
815da400ecSZachary Storer };
825da400ecSZachary Storer 
835da400ecSZachary Storer static void
progress(const struct finfo * fi,size_t written)845da400ecSZachary Storer progress(const struct finfo *fi, size_t written)
855da400ecSZachary Storer {
865da400ecSZachary Storer 	int pcent = (int)((100.0 * written) / fi->size);
875da400ecSZachary Storer 
885da400ecSZachary Storer 	pinfo = 0;
895da400ecSZachary Storer 	(void)fprintf(stderr, "%s => %s %zu/%zu bytes %d%% written\n",
905da400ecSZachary Storer 	    fi->from, fi->to, written, fi->size, pcent);
915da400ecSZachary Storer }
925da400ecSZachary Storer 
935da400ecSZachary Storer int
copy_file(FTSENT * entp,int dne)945da400ecSZachary Storer copy_file(FTSENT *entp, int dne)
955da400ecSZachary Storer {
965da400ecSZachary Storer 	static char buf[MAXBSIZE];
975da400ecSZachary Storer 	struct stat to_stat, *fs;
985da400ecSZachary Storer 	int ch, checkch, from_fd, rcount, rval, to_fd, tolnk, wcount;
995da400ecSZachary Storer 	char *p;
1005da400ecSZachary Storer 	size_t ptotal = 0;
1015da400ecSZachary Storer 
1025da400ecSZachary Storer 	if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
1035da400ecSZachary Storer 		warn("%s", entp->fts_path);
1045da400ecSZachary Storer 		return (1);
1055da400ecSZachary Storer 	}
1065da400ecSZachary Storer 
1075da400ecSZachary Storer 	to_fd = -1;
1085da400ecSZachary Storer 	fs = entp->fts_statp;
1095da400ecSZachary Storer 	tolnk = ((Rflag && !(Lflag || Hflag)) || Pflag);
1105da400ecSZachary Storer 
1115da400ecSZachary Storer 	/*
1125da400ecSZachary Storer 	 * If the file exists and we're interactive, verify with the user.
1135da400ecSZachary Storer 	 * If the file DNE, set the mode to be the from file, minus setuid
1145da400ecSZachary Storer 	 * bits, modified by the umask; arguably wrong, but it makes copying
1155da400ecSZachary Storer 	 * executables work right and it's been that way forever.  (The
1165da400ecSZachary Storer 	 * other choice is 666 or'ed with the execute bits on the from file
1175da400ecSZachary Storer 	 * modified by the umask.)
1185da400ecSZachary Storer 	 */
1195da400ecSZachary Storer 	if (!dne) {
1205da400ecSZachary Storer 		struct stat sb;
1215da400ecSZachary Storer 		int sval;
1225da400ecSZachary Storer 
1235da400ecSZachary Storer 		if (iflag) {
1245da400ecSZachary Storer 			(void)fprintf(stderr, "overwrite %s? ", to.p_path);
1255da400ecSZachary Storer 			checkch = ch = getchar();
1265da400ecSZachary Storer 			while (ch != '\n' && ch != EOF)
1275da400ecSZachary Storer 				ch = getchar();
1285da400ecSZachary Storer 			if (checkch != 'y' && checkch != 'Y') {
1295da400ecSZachary Storer 				(void)close(from_fd);
1305da400ecSZachary Storer 				return (0);
1315da400ecSZachary Storer 			}
1325da400ecSZachary Storer 		}
1335da400ecSZachary Storer 
1345da400ecSZachary Storer 		sval = tolnk ?
1355da400ecSZachary Storer 			lstat(to.p_path, &sb) : stat(to.p_path, &sb);
1365da400ecSZachary Storer 		if (sval == -1) {
1375da400ecSZachary Storer 			warn("stat: %s", to.p_path);
1385da400ecSZachary Storer 			(void)close(from_fd);
1395da400ecSZachary Storer 			return (1);
1405da400ecSZachary Storer 		}
1415da400ecSZachary Storer 
1425da400ecSZachary Storer 		if (!(tolnk && S_ISLNK(sb.st_mode)))
1435da400ecSZachary Storer 			to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
1445da400ecSZachary Storer 	} else
1455da400ecSZachary Storer 		to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
1465da400ecSZachary Storer 		    fs->st_mode & ~(S_ISUID | S_ISGID));
1475da400ecSZachary Storer 
1485da400ecSZachary Storer 	if (to_fd == -1 && (fflag || tolnk)) {
1495da400ecSZachary Storer 		/*
1505da400ecSZachary Storer 		 * attempt to remove existing destination file name and
1515da400ecSZachary Storer 		 * create a new file
1525da400ecSZachary Storer 		 */
1535da400ecSZachary Storer 		(void)unlink(to.p_path);
1545da400ecSZachary Storer 		to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
1555da400ecSZachary Storer 			     fs->st_mode & ~(S_ISUID | S_ISGID));
1565da400ecSZachary Storer 	}
1575da400ecSZachary Storer 
1585da400ecSZachary Storer 	if (to_fd == -1) {
1595da400ecSZachary Storer 		warn("%s", to.p_path);
1605da400ecSZachary Storer 		(void)close(from_fd);
1615da400ecSZachary Storer 		return (1);
1625da400ecSZachary Storer 	}
1635da400ecSZachary Storer 
1645da400ecSZachary Storer 	rval = 0;
1655da400ecSZachary Storer 
1665da400ecSZachary Storer 	/* if hard linking then simply close the open fds, link and return */
1675da400ecSZachary Storer 	if (lflag) {
1685da400ecSZachary Storer 		(void)close(from_fd);
1695da400ecSZachary Storer 		(void)close(to_fd);
1705da400ecSZachary Storer 		(void)unlink(to.p_path);
1715da400ecSZachary Storer 		if (link(entp->fts_path, to.p_path)) {
1725da400ecSZachary Storer 			warn("%s", to.p_path);
1735da400ecSZachary Storer 			return (1);
1745da400ecSZachary Storer 		}
1755da400ecSZachary Storer 		return (0);
1765da400ecSZachary Storer 	}
1775da400ecSZachary Storer 	/* NOTREACHED */
1785da400ecSZachary Storer 
1795da400ecSZachary Storer 	/*
1805da400ecSZachary Storer 	 * There's no reason to do anything other than close the file
1815da400ecSZachary Storer 	 * now if it's empty, so let's not bother.
1825da400ecSZachary Storer 	 */
1835da400ecSZachary Storer 	if (fs->st_size > 0) {
1845da400ecSZachary Storer 		struct finfo fi;
1855da400ecSZachary Storer 
1865da400ecSZachary Storer 		fi.from = entp->fts_path;
1875da400ecSZachary Storer 		fi.to = to.p_path;
1885da400ecSZachary Storer 		fi.size = (size_t)fs->st_size;
1895da400ecSZachary Storer 
1905da400ecSZachary Storer 		/*
1915da400ecSZachary Storer 		 * Mmap and write if less than 8M (the limit is so
1925da400ecSZachary Storer 		 * we don't totally trash memory on big files).
1935da400ecSZachary Storer 		 * This is really a minor hack, but it wins some CPU back.
1945da400ecSZachary Storer 		 */
1955da400ecSZachary Storer 		bool use_read;
1965da400ecSZachary Storer 
1975da400ecSZachary Storer 		use_read = true;
1985da400ecSZachary Storer 		if (fs->st_size <= MMAP_MAX_SIZE) {
1995da400ecSZachary Storer 			size_t fsize = (size_t)fs->st_size;
2005da400ecSZachary Storer 			p = mmap(NULL, fsize, PROT_READ, MAP_FILE|MAP_SHARED,
2015da400ecSZachary Storer 			    from_fd, (off_t)0);
2025da400ecSZachary Storer 			if (p != MAP_FAILED) {
2035da400ecSZachary Storer 				size_t remainder;
2045da400ecSZachary Storer 
2055da400ecSZachary Storer 				use_read = false;
2065da400ecSZachary Storer 
2075da400ecSZachary Storer #if !defined(__minix)
2085da400ecSZachary Storer 				(void) madvise(p, (size_t)fs->st_size,
2095da400ecSZachary Storer 				     MADV_SEQUENTIAL);
2105da400ecSZachary Storer #endif /* !defined(__minix) */
2115da400ecSZachary Storer 
2125da400ecSZachary Storer 				/*
2135da400ecSZachary Storer 				 * Write out the data in small chunks to
2145da400ecSZachary Storer 				 * avoid locking the output file for a
2155da400ecSZachary Storer 				 * long time if the reading the data from
2165da400ecSZachary Storer 				 * the source is slow.
2175da400ecSZachary Storer 				 */
2185da400ecSZachary Storer 				remainder = fsize;
2195da400ecSZachary Storer 				do {
2205da400ecSZachary Storer 					ssize_t chunk;
2215da400ecSZachary Storer 
2225da400ecSZachary Storer 					chunk = (remainder > MMAP_MAX_WRITE) ?
2235da400ecSZachary Storer 					    MMAP_MAX_WRITE : remainder;
2245da400ecSZachary Storer 					if (write(to_fd, &p[fsize - remainder],
2255da400ecSZachary Storer 					    chunk) != chunk) {
2265da400ecSZachary Storer 						warn("%s", to.p_path);
2275da400ecSZachary Storer 						rval = 1;
2285da400ecSZachary Storer 						break;
2295da400ecSZachary Storer 					}
2305da400ecSZachary Storer 					remainder -= chunk;
2315da400ecSZachary Storer 					ptotal += chunk;
2325da400ecSZachary Storer 					if (pinfo)
2335da400ecSZachary Storer 						progress(&fi, ptotal);
2345da400ecSZachary Storer 				} while (remainder > 0);
2355da400ecSZachary Storer 
2365da400ecSZachary Storer 				if (munmap(p, fsize) < 0) {
2375da400ecSZachary Storer 					warn("%s", entp->fts_path);
2385da400ecSZachary Storer 					rval = 1;
2395da400ecSZachary Storer 				}
2405da400ecSZachary Storer 			}
2415da400ecSZachary Storer 		}
2425da400ecSZachary Storer 
2435da400ecSZachary Storer 		if (use_read) {
2445da400ecSZachary Storer 			while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
2455da400ecSZachary Storer 				wcount = write(to_fd, buf, (size_t)rcount);
2465da400ecSZachary Storer 				if (rcount != wcount || wcount == -1) {
2475da400ecSZachary Storer 					warn("%s", to.p_path);
2485da400ecSZachary Storer 					rval = 1;
2495da400ecSZachary Storer 					break;
2505da400ecSZachary Storer 				}
2515da400ecSZachary Storer 				ptotal += wcount;
2525da400ecSZachary Storer 				if (pinfo)
2535da400ecSZachary Storer 					progress(&fi, ptotal);
2545da400ecSZachary Storer 			}
2555da400ecSZachary Storer 			if (rcount < 0) {
2565da400ecSZachary Storer 				warn("%s", entp->fts_path);
2575da400ecSZachary Storer 				rval = 1;
2585da400ecSZachary Storer 			}
2595da400ecSZachary Storer 		}
2605da400ecSZachary Storer 	}
2615da400ecSZachary Storer 
2625da400ecSZachary Storer #if !defined(__minix)
2635da400ecSZachary Storer 	if (pflag && (fcpxattr(from_fd, to_fd) != 0))
2645da400ecSZachary Storer 		warn("%s: error copying extended attributes", to.p_path);
2655da400ecSZachary Storer #endif /* !defined(__minix) */
2665da400ecSZachary Storer 
2675da400ecSZachary Storer 	(void)close(from_fd);
2685da400ecSZachary Storer 
2695da400ecSZachary Storer 	if (rval == 1) {
2705da400ecSZachary Storer 		(void)close(to_fd);
2715da400ecSZachary Storer 		return (1);
2725da400ecSZachary Storer 	}
2735da400ecSZachary Storer 
2745da400ecSZachary Storer 	if (pflag && setfile(fs, to_fd))
2755da400ecSZachary Storer 		rval = 1;
2765da400ecSZachary Storer 	/*
2775da400ecSZachary Storer 	 * If the source was setuid or setgid, lose the bits unless the
2785da400ecSZachary Storer 	 * copy is owned by the same user and group.
2795da400ecSZachary Storer 	 */
2805da400ecSZachary Storer #define	RETAINBITS \
2815da400ecSZachary Storer 	(S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
2825da400ecSZachary Storer 	if (!pflag && dne
2835da400ecSZachary Storer 	    && fs->st_mode & (S_ISUID | S_ISGID) && fs->st_uid == myuid) {
2845da400ecSZachary Storer 		if (fstat(to_fd, &to_stat)) {
2855da400ecSZachary Storer 			warn("%s", to.p_path);
2865da400ecSZachary Storer 			rval = 1;
2875da400ecSZachary Storer 		} else if (fs->st_gid == to_stat.st_gid &&
2885da400ecSZachary Storer 		    fchmod(to_fd, fs->st_mode & RETAINBITS & ~myumask)) {
2895da400ecSZachary Storer 			warn("%s", to.p_path);
2905da400ecSZachary Storer 			rval = 1;
2915da400ecSZachary Storer 		}
2925da400ecSZachary Storer 	}
2935da400ecSZachary Storer 	if (close(to_fd)) {
2945da400ecSZachary Storer 		warn("%s", to.p_path);
2955da400ecSZachary Storer 		rval = 1;
2965da400ecSZachary Storer 	}
2975da400ecSZachary Storer 	/* set the mod/access times now after close of the fd */
2985da400ecSZachary Storer 	if (pflag && set_utimes(to.p_path, fs)) {
2995da400ecSZachary Storer 	    rval = 1;
3005da400ecSZachary Storer 	}
3015da400ecSZachary Storer 	return (rval);
3025da400ecSZachary Storer }
3035da400ecSZachary Storer 
3045da400ecSZachary Storer int
copy_link(FTSENT * p,int exists)3055da400ecSZachary Storer copy_link(FTSENT *p, int exists)
3065da400ecSZachary Storer {
3075da400ecSZachary Storer 	int len;
3085da400ecSZachary Storer 	char target[MAXPATHLEN];
3095da400ecSZachary Storer 
3105da400ecSZachary Storer 	if ((len = readlink(p->fts_path, target, sizeof(target)-1)) == -1) {
3115da400ecSZachary Storer 		warn("readlink: %s", p->fts_path);
3125da400ecSZachary Storer 		return (1);
3135da400ecSZachary Storer 	}
3145da400ecSZachary Storer 	target[len] = '\0';
3155da400ecSZachary Storer 	if (exists && unlink(to.p_path)) {
3165da400ecSZachary Storer 		warn("unlink: %s", to.p_path);
3175da400ecSZachary Storer 		return (1);
3185da400ecSZachary Storer 	}
3195da400ecSZachary Storer 	if (symlink(target, to.p_path)) {
3205da400ecSZachary Storer 		warn("symlink: %s", target);
3215da400ecSZachary Storer 		return (1);
3225da400ecSZachary Storer 	}
3235da400ecSZachary Storer 	return (pflag ? setfile(p->fts_statp, 0) : 0);
3245da400ecSZachary Storer }
3255da400ecSZachary Storer 
3265da400ecSZachary Storer int
copy_fifo(struct stat * from_stat,int exists)3275da400ecSZachary Storer copy_fifo(struct stat *from_stat, int exists)
3285da400ecSZachary Storer {
3295da400ecSZachary Storer 	if (exists && unlink(to.p_path)) {
3305da400ecSZachary Storer 		warn("unlink: %s", to.p_path);
3315da400ecSZachary Storer 		return (1);
3325da400ecSZachary Storer 	}
3335da400ecSZachary Storer 	if (mkfifo(to.p_path, from_stat->st_mode)) {
3345da400ecSZachary Storer 		warn("mkfifo: %s", to.p_path);
3355da400ecSZachary Storer 		return (1);
3365da400ecSZachary Storer 	}
3375da400ecSZachary Storer 	return (pflag ? setfile(from_stat, 0) : 0);
3385da400ecSZachary Storer }
3395da400ecSZachary Storer 
3405da400ecSZachary Storer int
copy_special(struct stat * from_stat,int exists)3415da400ecSZachary Storer copy_special(struct stat *from_stat, int exists)
3425da400ecSZachary Storer {
3435da400ecSZachary Storer 	if (exists && unlink(to.p_path)) {
3445da400ecSZachary Storer 		warn("unlink: %s", to.p_path);
3455da400ecSZachary Storer 		return (1);
3465da400ecSZachary Storer 	}
3475da400ecSZachary Storer 	if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
3485da400ecSZachary Storer 		warn("mknod: %s", to.p_path);
3495da400ecSZachary Storer 		return (1);
3505da400ecSZachary Storer 	}
3515da400ecSZachary Storer 	return (pflag ? setfile(from_stat, 0) : 0);
3525da400ecSZachary Storer }
3535da400ecSZachary Storer 
3545da400ecSZachary Storer 
3555da400ecSZachary Storer /*
3565da400ecSZachary Storer  * Function: setfile
3575da400ecSZachary Storer  *
3585da400ecSZachary Storer  * Purpose:
3595da400ecSZachary Storer  *   Set the owner/group/permissions for the "to" file to the information
3605da400ecSZachary Storer  *   in the stat structure.  If fd is zero, also call set_utimes() to set
3615da400ecSZachary Storer  *   the mod/access times.  If fd is non-zero, the caller must do a utimes
3625da400ecSZachary Storer  *   itself after close(fd).
3635da400ecSZachary Storer  */
3645da400ecSZachary Storer int
setfile(struct stat * fs,int fd)3655da400ecSZachary Storer setfile(struct stat *fs, int fd)
3665da400ecSZachary Storer {
367*0a6a1f1dSLionel Sambuc 	int rval/* MINIX:, islink*/;
3685da400ecSZachary Storer 
3695da400ecSZachary Storer 	rval = 0;
370*0a6a1f1dSLionel Sambuc #if !defined(__minix)
3715da400ecSZachary Storer 	islink = S_ISLNK(fs->st_mode);
372*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
3735da400ecSZachary Storer 	fs->st_mode &= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
3745da400ecSZachary Storer 
3755da400ecSZachary Storer 	/*
3765da400ecSZachary Storer 	 * Changing the ownership probably won't succeed, unless we're root
3775da400ecSZachary Storer 	 * or POSIX_CHOWN_RESTRICTED is not set.  Set uid/gid before setting
3785da400ecSZachary Storer 	 * the mode; current BSD behavior is to remove all setuid bits on
3795da400ecSZachary Storer 	 * chown.  If chown fails, lose setuid/setgid bits.
3805da400ecSZachary Storer 	 */
3815da400ecSZachary Storer 	if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
3825da400ecSZachary Storer 	    lchown(to.p_path, fs->st_uid, fs->st_gid)) {
3835da400ecSZachary Storer 		if (errno != EPERM) {
3845da400ecSZachary Storer 			warn("chown: %s", to.p_path);
3855da400ecSZachary Storer 			rval = 1;
3865da400ecSZachary Storer 		}
3875da400ecSZachary Storer 		fs->st_mode &= ~(S_ISUID | S_ISGID);
3885da400ecSZachary Storer 	}
3895da400ecSZachary Storer 	if (fd ? fchmod(fd, fs->st_mode) : lchmod(to.p_path, fs->st_mode)) {
3905da400ecSZachary Storer 		warn("chmod: %s", to.p_path);
3915da400ecSZachary Storer 		rval = 1;
3925da400ecSZachary Storer 	}
3935da400ecSZachary Storer 
3945da400ecSZachary Storer #if !defined(__minix)
3955da400ecSZachary Storer 	if (!islink && !Nflag) {
3965da400ecSZachary Storer 		unsigned long fflags = fs->st_flags;
3975da400ecSZachary Storer 		/*
3985da400ecSZachary Storer 		 * XXX
3995da400ecSZachary Storer 		 * NFS doesn't support chflags; ignore errors unless
4005da400ecSZachary Storer 		 * there's reason to believe we're losing bits.
4015da400ecSZachary Storer 		 * (Note, this still won't be right if the server
4025da400ecSZachary Storer 		 * supports flags and we were trying to *remove* flags
4035da400ecSZachary Storer 		 * on a file that we copied, i.e., that we didn't create.)
4045da400ecSZachary Storer 		 */
4055da400ecSZachary Storer 		errno = 0;
4065da400ecSZachary Storer 
4075da400ecSZachary Storer 		if ((fd ? fchflags(fd, fflags) :
4085da400ecSZachary Storer 		    chflags(to.p_path, fflags)) == -1)
4095da400ecSZachary Storer 			if (errno != EOPNOTSUPP || fs->st_flags != 0) {
4105da400ecSZachary Storer 				warn("chflags: %s", to.p_path);
4115da400ecSZachary Storer 				rval = 1;
4125da400ecSZachary Storer 			}
4135da400ecSZachary Storer 	}
4145da400ecSZachary Storer #endif /* !defined(__minix) */
4155da400ecSZachary Storer 
4165da400ecSZachary Storer 	/* if fd is non-zero, caller must call set_utimes() after close() */
4175da400ecSZachary Storer 	if (fd == 0 && set_utimes(to.p_path, fs))
4185da400ecSZachary Storer 	    rval = 1;
4195da400ecSZachary Storer 	return (rval);
4205da400ecSZachary Storer }
4215da400ecSZachary Storer 
4225da400ecSZachary Storer void
usage(void)4235da400ecSZachary Storer usage(void)
4245da400ecSZachary Storer {
4255da400ecSZachary Storer 	(void)fprintf(stderr,
4265da400ecSZachary Storer 	    "usage: %s [-R [-H | -L | -P]] [-f | -i] [-alNpv] src target\n"
4275da400ecSZachary Storer 	    "       %s [-R [-H | -L | -P]] [-f | -i] [-alNpv] src1 ... srcN directory\n",
4285da400ecSZachary Storer 	    getprogname(), getprogname());
4295da400ecSZachary Storer 	exit(1);
4305da400ecSZachary Storer 	/* NOTREACHED */
4315da400ecSZachary Storer }
432