xref: /titanic_52/usr/src/cmd/mv/mv.c (revision 0f6e7ba6e1c0f032e0e839fdba06b2e0ddb0c0d4)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
594d2b9abSmarks  * Common Development and Distribution License (the "License").
694d2b9abSmarks  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*0f6e7ba6Sas145665  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * Combined mv/cp/ln command:
437c478bd9Sstevel@tonic-gate  *	mv file1 file2
447c478bd9Sstevel@tonic-gate  *	mv dir1 dir2
457c478bd9Sstevel@tonic-gate  *	mv file1 ... filen dir1
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate #include <stdio.h>
487c478bd9Sstevel@tonic-gate #include <sys/types.h>
497c478bd9Sstevel@tonic-gate #include <sys/stat.h>
507c478bd9Sstevel@tonic-gate #include <sys/avl.h>
517c478bd9Sstevel@tonic-gate #include <sys/mman.h>
527c478bd9Sstevel@tonic-gate #include <fcntl.h>
537c478bd9Sstevel@tonic-gate #include <sys/time.h>
547c478bd9Sstevel@tonic-gate #include <signal.h>
557c478bd9Sstevel@tonic-gate #include <errno.h>
567c478bd9Sstevel@tonic-gate #include <dirent.h>
577c478bd9Sstevel@tonic-gate #include <stdlib.h>
587c478bd9Sstevel@tonic-gate #include <locale.h>
597c478bd9Sstevel@tonic-gate #include <langinfo.h>
607c478bd9Sstevel@tonic-gate #include <stdarg.h>
617c478bd9Sstevel@tonic-gate #include <string.h>
627c478bd9Sstevel@tonic-gate #include <unistd.h>
637c478bd9Sstevel@tonic-gate #include <limits.h>
647c478bd9Sstevel@tonic-gate #include <sys/acl.h>
657c478bd9Sstevel@tonic-gate #include <libcmdutils.h>
66fa9e4066Sahrens #include <aclutils.h>
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #define	FTYPE(A)	(A.st_mode)
697c478bd9Sstevel@tonic-gate #define	FMODE(A)	(A.st_mode)
707c478bd9Sstevel@tonic-gate #define	UID(A)		(A.st_uid)
717c478bd9Sstevel@tonic-gate #define	GID(A)		(A.st_gid)
727c478bd9Sstevel@tonic-gate #define	IDENTICAL(A, B)	(A.st_dev == B.st_dev && A.st_ino == B.st_ino)
737c478bd9Sstevel@tonic-gate #define	ISBLK(A)	((A.st_mode & S_IFMT) == S_IFBLK)
747c478bd9Sstevel@tonic-gate #define	ISCHR(A)	((A.st_mode & S_IFMT) == S_IFCHR)
757c478bd9Sstevel@tonic-gate #define	ISDIR(A)	((A.st_mode & S_IFMT) == S_IFDIR)
767c478bd9Sstevel@tonic-gate #define	ISDOOR(A)	((A.st_mode & S_IFMT) == S_IFDOOR)
777c478bd9Sstevel@tonic-gate #define	ISFIFO(A)	((A.st_mode & S_IFMT) == S_IFIFO)
787c478bd9Sstevel@tonic-gate #define	ISLNK(A)	((A.st_mode & S_IFMT) == S_IFLNK)
797c478bd9Sstevel@tonic-gate #define	ISREG(A)	(((A).st_mode & S_IFMT) == S_IFREG)
807c478bd9Sstevel@tonic-gate #define	ISDEV(A)	((A.st_mode & S_IFMT) == S_IFCHR || \
817c478bd9Sstevel@tonic-gate 			(A.st_mode & S_IFMT) == S_IFBLK || \
827c478bd9Sstevel@tonic-gate 			(A.st_mode & S_IFMT) == S_IFIFO)
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #define	BLKSIZE	4096
857c478bd9Sstevel@tonic-gate #define	PATHSIZE 1024
867c478bd9Sstevel@tonic-gate #define	DOT	"."
877c478bd9Sstevel@tonic-gate #define	DOTDOT	".."
887c478bd9Sstevel@tonic-gate #define	DELIM	'/'
897c478bd9Sstevel@tonic-gate #define	EQ(x, y)	(strcmp(x, y) == 0)
907c478bd9Sstevel@tonic-gate #define	FALSE	0
917c478bd9Sstevel@tonic-gate #define	MODEBITS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)
927c478bd9Sstevel@tonic-gate #define	TRUE 1
937c478bd9Sstevel@tonic-gate #define	MAXMAPSIZE	(1024*1024*8)	/* map at most 8MB */
947c478bd9Sstevel@tonic-gate #define	SMALLFILESIZE	(32*1024)	/* don't use mmap on little files */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static char		*dname(char *);
977c478bd9Sstevel@tonic-gate static int		getresp(void);
987c478bd9Sstevel@tonic-gate static int		lnkfil(char *, char *);
997c478bd9Sstevel@tonic-gate static int		cpymve(char *, char *);
1007c478bd9Sstevel@tonic-gate static int		chkfiles(char *, char **);
1017c478bd9Sstevel@tonic-gate static int		rcopy(char *, char *);
1027c478bd9Sstevel@tonic-gate static int		chk_different(char *, char *);
1037c478bd9Sstevel@tonic-gate static int		chg_time(char *, struct stat);
1047c478bd9Sstevel@tonic-gate static int		chg_mode(char *, uid_t, gid_t, mode_t);
1057c478bd9Sstevel@tonic-gate static int		copydir(char *, char *);
1067c478bd9Sstevel@tonic-gate static int		copyspecial(char *);
1077c478bd9Sstevel@tonic-gate static int		getrealpath(char *, char *);
1087c478bd9Sstevel@tonic-gate static void		usage(void);
1097c478bd9Sstevel@tonic-gate static void		Perror(char *);
1107c478bd9Sstevel@tonic-gate static void		Perror2(char *, char *);
1117c478bd9Sstevel@tonic-gate static int		writefile(int, int, char *, char *,
1127c478bd9Sstevel@tonic-gate 			    struct stat *, struct stat *);
1137c478bd9Sstevel@tonic-gate static int		use_stdin(void);
1147c478bd9Sstevel@tonic-gate static int		copyattributes(char *, char *);
1157c478bd9Sstevel@tonic-gate static void		timestruc_to_timeval(timestruc_t *, struct timeval *);
1167c478bd9Sstevel@tonic-gate static tree_node_t	*create_tnode(dev_t, ino_t);
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate extern	int 		errno;
1197c478bd9Sstevel@tonic-gate extern  char 		*optarg;
1207c478bd9Sstevel@tonic-gate extern	int 		optind, opterr;
1217c478bd9Sstevel@tonic-gate static struct stat 	s1, s2;
1227c478bd9Sstevel@tonic-gate static int 		cpy = FALSE;
1237c478bd9Sstevel@tonic-gate static int 		mve = FALSE;
1247c478bd9Sstevel@tonic-gate static int 		lnk = FALSE;
1257c478bd9Sstevel@tonic-gate static char		*cmd;
1267c478bd9Sstevel@tonic-gate static int		silent = 0;
1277c478bd9Sstevel@tonic-gate static int		fflg = 0;
1287c478bd9Sstevel@tonic-gate static int		iflg = 0;
1297c478bd9Sstevel@tonic-gate static int		pflg = 0;
1307c478bd9Sstevel@tonic-gate static int		Rflg = 0;	/* recursive copy */
1317c478bd9Sstevel@tonic-gate static int		rflg = 0;	/* recursive copy */
1327c478bd9Sstevel@tonic-gate static int		sflg = 0;
1337c478bd9Sstevel@tonic-gate static int		Hflg = 0;	/* follow cmd line arg symlink to dir */
1347c478bd9Sstevel@tonic-gate static int		Lflg = 0;	/* follow symlinks */
1357c478bd9Sstevel@tonic-gate static int		Pflg = 0;	/* do not follow symlinks */
1367c478bd9Sstevel@tonic-gate static int		atflg = 0;
1377c478bd9Sstevel@tonic-gate static int		attrsilent = 0;
1387c478bd9Sstevel@tonic-gate static int		targetexists = 0;
1397c478bd9Sstevel@tonic-gate static char		yeschr[SCHAR_MAX + 2];
1407c478bd9Sstevel@tonic-gate static char		nochr[SCHAR_MAX + 2];
1417c478bd9Sstevel@tonic-gate static int		cmdarg;		/* command line argument */
1427c478bd9Sstevel@tonic-gate static avl_tree_t	*stree = NULL;	/* source file inode search tree */
143fa9e4066Sahrens static acl_t		*s1acl;
1447c478bd9Sstevel@tonic-gate 
145a77d64afScf46844 int
1467c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1477c478bd9Sstevel@tonic-gate {
1487c478bd9Sstevel@tonic-gate 	int c, i, r, errflg = 0;
1497c478bd9Sstevel@tonic-gate 	char target[PATH_MAX];
1507c478bd9Sstevel@tonic-gate 	int (*move)(char *, char *);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	/*
1537c478bd9Sstevel@tonic-gate 	 * Determine command invoked (mv, cp, or ln)
1547c478bd9Sstevel@tonic-gate 	 */
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	if (cmd = strrchr(argv[0], '/'))
1577c478bd9Sstevel@tonic-gate 		++cmd;
1587c478bd9Sstevel@tonic-gate 	else
1597c478bd9Sstevel@tonic-gate 		cmd = argv[0];
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	/*
1627c478bd9Sstevel@tonic-gate 	 * Set flags based on command.
1637c478bd9Sstevel@tonic-gate 	 */
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1667c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1677c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
1687c478bd9Sstevel@tonic-gate #endif
1697c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	(void) strncpy(yeschr, nl_langinfo(YESSTR), SCHAR_MAX + 2);
1727c478bd9Sstevel@tonic-gate 	(void) strncpy(nochr, nl_langinfo(NOSTR), SCHAR_MAX + 2);
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	if (EQ(cmd, "mv"))
1757c478bd9Sstevel@tonic-gate 		mve = TRUE;
1767c478bd9Sstevel@tonic-gate 	else if (EQ(cmd, "ln"))
1777c478bd9Sstevel@tonic-gate 		lnk = TRUE;
1787c478bd9Sstevel@tonic-gate 	else if (EQ(cmd, "cp"))
1797c478bd9Sstevel@tonic-gate 		cpy = TRUE;
1807c478bd9Sstevel@tonic-gate 	else {
1817c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1827c478bd9Sstevel@tonic-gate 		    gettext("Invalid command name (%s); expecting "
1837c478bd9Sstevel@tonic-gate 		    "mv, cp, or ln.\n"), cmd);
1847c478bd9Sstevel@tonic-gate 		exit(1);
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	/*
1887c478bd9Sstevel@tonic-gate 	 * Check for options:
1897c478bd9Sstevel@tonic-gate 	 * 	cp  -r|-R [-H|-L|-P] [-fip@] file1 [file2 ...] target
1907c478bd9Sstevel@tonic-gate 	 * 	cp [-fiprR@] file1 [file2 ...] target
1917c478bd9Sstevel@tonic-gate 	 *	ln [-f] [-n] [-s] file1 [file2 ...] target
1927c478bd9Sstevel@tonic-gate 	 *	ln [-f] [-n] [-s] file1 [file2 ...]
1937c478bd9Sstevel@tonic-gate 	 *	mv [-f|i] file1 [file2 ...] target
1947c478bd9Sstevel@tonic-gate 	 *	mv [-f|i] dir1 target
1957c478bd9Sstevel@tonic-gate 	 */
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (cpy) {
1987c478bd9Sstevel@tonic-gate 		while ((c = getopt(argc, argv, "fHiLpPrR@")) != EOF)
1997c478bd9Sstevel@tonic-gate 			switch (c) {
2007c478bd9Sstevel@tonic-gate 			case 'f':
2017c478bd9Sstevel@tonic-gate 				fflg++;
2027c478bd9Sstevel@tonic-gate 				break;
2037c478bd9Sstevel@tonic-gate 			case 'i':
2047c478bd9Sstevel@tonic-gate 				iflg++;
2057c478bd9Sstevel@tonic-gate 				break;
2067c478bd9Sstevel@tonic-gate 			case 'p':
2077c478bd9Sstevel@tonic-gate 				pflg++;
2087c478bd9Sstevel@tonic-gate #ifdef XPG4
2097c478bd9Sstevel@tonic-gate 				attrsilent = 1;
2107c478bd9Sstevel@tonic-gate 				atflg = 0;
2117c478bd9Sstevel@tonic-gate #else
2127c478bd9Sstevel@tonic-gate 				if (atflg == 0)
2137c478bd9Sstevel@tonic-gate 					attrsilent = 1;
2147c478bd9Sstevel@tonic-gate #endif
2157c478bd9Sstevel@tonic-gate 				break;
2167c478bd9Sstevel@tonic-gate 			case 'H':
2177c478bd9Sstevel@tonic-gate 				/*
2187c478bd9Sstevel@tonic-gate 				 * If more than one of -H, -L, or -P are
2197c478bd9Sstevel@tonic-gate 				 * specified, only the last option specified
2207c478bd9Sstevel@tonic-gate 				 * determines the behavior.
2217c478bd9Sstevel@tonic-gate 				 */
2227c478bd9Sstevel@tonic-gate 				Lflg = Pflg = 0;
2237c478bd9Sstevel@tonic-gate 				Hflg++;
2247c478bd9Sstevel@tonic-gate 				break;
2257c478bd9Sstevel@tonic-gate 			case 'L':
2267c478bd9Sstevel@tonic-gate 				Hflg = Pflg = 0;
2277c478bd9Sstevel@tonic-gate 				Lflg++;
2287c478bd9Sstevel@tonic-gate 				break;
2297c478bd9Sstevel@tonic-gate 			case 'P':
2307c478bd9Sstevel@tonic-gate 				Lflg = Hflg = 0;
2317c478bd9Sstevel@tonic-gate 				Pflg++;
2327c478bd9Sstevel@tonic-gate 				break;
2337c478bd9Sstevel@tonic-gate 			case 'R':
2347c478bd9Sstevel@tonic-gate 				/*
2357c478bd9Sstevel@tonic-gate 				 * The default behavior of cp -R|-r
2367c478bd9Sstevel@tonic-gate 				 * when specified without -H|-L|-P
2377c478bd9Sstevel@tonic-gate 				 * is -L.
2387c478bd9Sstevel@tonic-gate 				 */
2397c478bd9Sstevel@tonic-gate 				Rflg++;
2407c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
2417c478bd9Sstevel@tonic-gate 			case 'r':
2427c478bd9Sstevel@tonic-gate 				rflg++;
2437c478bd9Sstevel@tonic-gate 				break;
2447c478bd9Sstevel@tonic-gate 			case '@':
2457c478bd9Sstevel@tonic-gate 				atflg++;
2467c478bd9Sstevel@tonic-gate 				attrsilent = 0;
2477c478bd9Sstevel@tonic-gate #ifdef XPG4
2487c478bd9Sstevel@tonic-gate 				pflg = 0;
2497c478bd9Sstevel@tonic-gate #endif
2507c478bd9Sstevel@tonic-gate 				break;
2517c478bd9Sstevel@tonic-gate 			default:
2527c478bd9Sstevel@tonic-gate 				errflg++;
2537c478bd9Sstevel@tonic-gate 			}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 		/* -R or -r must be specified with -H, -L, or -P */
2567c478bd9Sstevel@tonic-gate 		if ((Hflg || Lflg || Pflg) && !(Rflg || rflg)) {
2577c478bd9Sstevel@tonic-gate 			errflg++;
2587c478bd9Sstevel@tonic-gate 		}
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	} else if (mve) {
2617c478bd9Sstevel@tonic-gate 		while ((c = getopt(argc, argv, "fis")) != EOF)
2627c478bd9Sstevel@tonic-gate 			switch (c) {
2637c478bd9Sstevel@tonic-gate 			case 'f':
2647c478bd9Sstevel@tonic-gate 				silent++;
2657c478bd9Sstevel@tonic-gate #ifdef XPG4
2667c478bd9Sstevel@tonic-gate 				iflg = 0;
2677c478bd9Sstevel@tonic-gate #endif
2687c478bd9Sstevel@tonic-gate 				break;
2697c478bd9Sstevel@tonic-gate 			case 'i':
2707c478bd9Sstevel@tonic-gate 				iflg++;
2717c478bd9Sstevel@tonic-gate #ifdef XPG4
2727c478bd9Sstevel@tonic-gate 				silent = 0;
2737c478bd9Sstevel@tonic-gate #endif
2747c478bd9Sstevel@tonic-gate 				break;
2757c478bd9Sstevel@tonic-gate 			default:
2767c478bd9Sstevel@tonic-gate 				errflg++;
2777c478bd9Sstevel@tonic-gate 			}
2787c478bd9Sstevel@tonic-gate 	} else { /* ln */
2797c478bd9Sstevel@tonic-gate 		while ((c = getopt(argc, argv, "fns")) != EOF)
2807c478bd9Sstevel@tonic-gate 			switch (c) {
2817c478bd9Sstevel@tonic-gate 			case 'f':
2827c478bd9Sstevel@tonic-gate 				silent++;
2837c478bd9Sstevel@tonic-gate 				break;
2847c478bd9Sstevel@tonic-gate 			case 'n':
2857c478bd9Sstevel@tonic-gate 				/* silently ignored; this is the default */
2867c478bd9Sstevel@tonic-gate 				break;
2877c478bd9Sstevel@tonic-gate 			case 's':
2887c478bd9Sstevel@tonic-gate 				sflg++;
2897c478bd9Sstevel@tonic-gate 				break;
2907c478bd9Sstevel@tonic-gate 			default:
2917c478bd9Sstevel@tonic-gate 				errflg++;
2927c478bd9Sstevel@tonic-gate 			}
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	/*
2967c478bd9Sstevel@tonic-gate 	 * For BSD compatibility allow - to delimit the end of
2977c478bd9Sstevel@tonic-gate 	 * options for mv.
2987c478bd9Sstevel@tonic-gate 	 */
2997c478bd9Sstevel@tonic-gate 	if (mve && optind < argc && (strcmp(argv[optind], "-") == 0))
3007c478bd9Sstevel@tonic-gate 		optind++;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	/*
3037c478bd9Sstevel@tonic-gate 	 * Check for sufficient arguments
3047c478bd9Sstevel@tonic-gate 	 * or a usage error.
3057c478bd9Sstevel@tonic-gate 	 */
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	argc -= optind;
3087c478bd9Sstevel@tonic-gate 	argv  = &argv[optind];
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	if ((argc < 2 && lnk != TRUE) || (argc < 1 && lnk == TRUE)) {
3117c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3127c478bd9Sstevel@tonic-gate 		    gettext("%s: Insufficient arguments (%d)\n"),
3137c478bd9Sstevel@tonic-gate 		    cmd, argc);
3147c478bd9Sstevel@tonic-gate 		usage();
3157c478bd9Sstevel@tonic-gate 	}
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	if (errflg != 0)
3187c478bd9Sstevel@tonic-gate 		usage();
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	/*
3217c478bd9Sstevel@tonic-gate 	 * If there is more than a source and target,
3227c478bd9Sstevel@tonic-gate 	 * the last argument (the target) must be a directory
3237c478bd9Sstevel@tonic-gate 	 * which really exists.
3247c478bd9Sstevel@tonic-gate 	 */
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	if (argc > 2) {
3277c478bd9Sstevel@tonic-gate 		if (stat(argv[argc-1], &s2) < 0) {
3287c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3297c478bd9Sstevel@tonic-gate 			    gettext("%s: %s not found\n"),
3307c478bd9Sstevel@tonic-gate 			    cmd, argv[argc-1]);
3317c478bd9Sstevel@tonic-gate 			exit(2);
3327c478bd9Sstevel@tonic-gate 		}
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 		if (!ISDIR(s2)) {
3357c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3367c478bd9Sstevel@tonic-gate 			    gettext("%s: Target %s must be a directory\n"),
3377c478bd9Sstevel@tonic-gate 			    cmd, argv[argc-1]);
3387c478bd9Sstevel@tonic-gate 			usage();
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	if (strlen(argv[argc-1]) >= PATH_MAX) {
3437c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3447c478bd9Sstevel@tonic-gate 		    gettext("%s: Target %s file name length exceeds PATH_MAX"
3457c478bd9Sstevel@tonic-gate 		    " %d\n"), cmd, argv[argc-1], PATH_MAX);
3467c478bd9Sstevel@tonic-gate 		exit(78);
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	if (argc == 1) {
3507c478bd9Sstevel@tonic-gate 		if (!lnk)
3517c478bd9Sstevel@tonic-gate 			usage();
3527c478bd9Sstevel@tonic-gate 		(void) strcpy(target, ".");
3537c478bd9Sstevel@tonic-gate 	} else {
3547c478bd9Sstevel@tonic-gate 		(void) strcpy(target, argv[--argc]);
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	/*
3587c478bd9Sstevel@tonic-gate 	 * Perform a multiple argument mv|cp|ln by
3597c478bd9Sstevel@tonic-gate 	 * multiple invocations of cpymve() or lnkfil().
3607c478bd9Sstevel@tonic-gate 	 */
3617c478bd9Sstevel@tonic-gate 	if (lnk)
3627c478bd9Sstevel@tonic-gate 		move = lnkfil;
3637c478bd9Sstevel@tonic-gate 	else
3647c478bd9Sstevel@tonic-gate 		move = cpymve;
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	r = 0;
3677c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
3687c478bd9Sstevel@tonic-gate 		stree = NULL;
3697c478bd9Sstevel@tonic-gate 		cmdarg = 1;
3707c478bd9Sstevel@tonic-gate 		r += move(argv[i], target);
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	/*
3747c478bd9Sstevel@tonic-gate 	 * Show errors by nonzero exit code.
3757c478bd9Sstevel@tonic-gate 	 */
3767c478bd9Sstevel@tonic-gate 
377a77d64afScf46844 	return (r?2:0);
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate static int
3817c478bd9Sstevel@tonic-gate lnkfil(char *source, char *target)
3827c478bd9Sstevel@tonic-gate {
3837c478bd9Sstevel@tonic-gate 	char	*buf = NULL;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	if (sflg) {
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 		/*
3887c478bd9Sstevel@tonic-gate 		 * If target is a directory make complete
3897c478bd9Sstevel@tonic-gate 		 * name of the new symbolic link within that
3907c478bd9Sstevel@tonic-gate 		 * directory.
3917c478bd9Sstevel@tonic-gate 		 */
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 		if ((stat(target, &s2) >= 0) && ISDIR(s2)) {
3947c478bd9Sstevel@tonic-gate 			size_t len;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 			len = strlen(target) + strlen(dname(source)) + 4;
3977c478bd9Sstevel@tonic-gate 			if ((buf = (char *)malloc(len)) == NULL) {
3987c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
3997c478bd9Sstevel@tonic-gate 				    gettext("%s: Insufficient memory "
4007c478bd9Sstevel@tonic-gate 				    "to %s %s\n"), cmd, cmd, source);
4017c478bd9Sstevel@tonic-gate 				exit(3);
4027c478bd9Sstevel@tonic-gate 			}
4037c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, len, "%s/%s",
4047c478bd9Sstevel@tonic-gate 			    target, dname(source));
4057c478bd9Sstevel@tonic-gate 			target = buf;
4067c478bd9Sstevel@tonic-gate 		}
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 		/*
4097c478bd9Sstevel@tonic-gate 		 * Check to see if the file exists already
4107c478bd9Sstevel@tonic-gate 		 */
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 		if ((stat(target, &s2) == 0)) {
4137c478bd9Sstevel@tonic-gate 			/*
4147c478bd9Sstevel@tonic-gate 			 * Check if the silent flag is set ie. the -f option
4157c478bd9Sstevel@tonic-gate 			 * is used.  If so, use unlink to remove the current
4167c478bd9Sstevel@tonic-gate 			 * target to replace with the new target, specified
4177c478bd9Sstevel@tonic-gate 			 * on the command line.  Proceed with symlink.
4187c478bd9Sstevel@tonic-gate 			 */
4197c478bd9Sstevel@tonic-gate 			if (silent) {
4207c478bd9Sstevel@tonic-gate 				if (unlink(target) < 0) {
4217c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
4227c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot unlink %s: "),
4237c478bd9Sstevel@tonic-gate 					    cmd, target);
4247c478bd9Sstevel@tonic-gate 					perror("");
4257c478bd9Sstevel@tonic-gate 					return (1);
4267c478bd9Sstevel@tonic-gate 				}
4277c478bd9Sstevel@tonic-gate 			}
4287c478bd9Sstevel@tonic-gate 		}
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 		/*
4327c478bd9Sstevel@tonic-gate 		 * Create a symbolic link to the source.
4337c478bd9Sstevel@tonic-gate 		 */
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 		if (symlink(source, target) < 0) {
4367c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4377c478bd9Sstevel@tonic-gate 			    gettext("%s: cannot create %s: "),
4387c478bd9Sstevel@tonic-gate 			    cmd, target);
4397c478bd9Sstevel@tonic-gate 			perror("");
4407c478bd9Sstevel@tonic-gate 			if (buf != NULL)
4417c478bd9Sstevel@tonic-gate 				free(buf);
4427c478bd9Sstevel@tonic-gate 			return (1);
4437c478bd9Sstevel@tonic-gate 		}
4447c478bd9Sstevel@tonic-gate 		if (buf != NULL)
4457c478bd9Sstevel@tonic-gate 			free(buf);
4467c478bd9Sstevel@tonic-gate 		return (0);
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	switch (chkfiles(source, &target)) {
4507c478bd9Sstevel@tonic-gate 		case 1: return (1);
4517c478bd9Sstevel@tonic-gate 		case 2: return (0);
4527c478bd9Sstevel@tonic-gate 			/* default - fall through */
4537c478bd9Sstevel@tonic-gate 	}
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	/*
4567c478bd9Sstevel@tonic-gate 	 * Make sure source file is not a directory,
4577c478bd9Sstevel@tonic-gate 	 * we can't link directories...
4587c478bd9Sstevel@tonic-gate 	 */
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	if (ISDIR(s1)) {
4617c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4627c478bd9Sstevel@tonic-gate 		    gettext("%s: %s is a directory\n"), cmd, source);
4637c478bd9Sstevel@tonic-gate 		return (1);
4647c478bd9Sstevel@tonic-gate 	}
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	/*
4677c478bd9Sstevel@tonic-gate 	 * hard link, call link() and return.
4687c478bd9Sstevel@tonic-gate 	 */
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	if (link(source, target) < 0) {
4717c478bd9Sstevel@tonic-gate 		if (errno == EXDEV)
4727c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4737c478bd9Sstevel@tonic-gate 			    gettext("%s: %s is on a different file system\n"),
4747c478bd9Sstevel@tonic-gate 			    cmd, target);
4757c478bd9Sstevel@tonic-gate 		else {
4767c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4777c478bd9Sstevel@tonic-gate 			    gettext("%s: cannot create link %s: "),
4787c478bd9Sstevel@tonic-gate 			    cmd, target);
4797c478bd9Sstevel@tonic-gate 			perror("");
4807c478bd9Sstevel@tonic-gate 		}
4817c478bd9Sstevel@tonic-gate 		if (buf != NULL)
4827c478bd9Sstevel@tonic-gate 			free(buf);
4837c478bd9Sstevel@tonic-gate 		return (1);
4847c478bd9Sstevel@tonic-gate 	} else {
4857c478bd9Sstevel@tonic-gate 		if (buf != NULL)
4867c478bd9Sstevel@tonic-gate 			free(buf);
4877c478bd9Sstevel@tonic-gate 		return (0);
4887c478bd9Sstevel@tonic-gate 	}
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate static int
4927c478bd9Sstevel@tonic-gate cpymve(char *source, char *target)
4937c478bd9Sstevel@tonic-gate {
4947c478bd9Sstevel@tonic-gate 	int	n;
4957c478bd9Sstevel@tonic-gate 	int fi, fo;
4967c478bd9Sstevel@tonic-gate 	int ret = 0;
4977c478bd9Sstevel@tonic-gate 	int attret = 0;
4987c478bd9Sstevel@tonic-gate 	int errno_save;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	switch (chkfiles(source, &target)) {
5017c478bd9Sstevel@tonic-gate 		case 1: return (1);
5027c478bd9Sstevel@tonic-gate 		case 2: return (0);
5037c478bd9Sstevel@tonic-gate 			/* default - fall through */
5047c478bd9Sstevel@tonic-gate 	}
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	/*
5077c478bd9Sstevel@tonic-gate 	 * If it's a recursive copy and source
5087c478bd9Sstevel@tonic-gate 	 * is a directory, then call rcopy (from copydir).
5097c478bd9Sstevel@tonic-gate 	 */
5107c478bd9Sstevel@tonic-gate 	if (cpy) {
5117c478bd9Sstevel@tonic-gate 		if (ISDIR(s1)) {
5127c478bd9Sstevel@tonic-gate 			int		rc;
5137c478bd9Sstevel@tonic-gate 			avl_index_t	where = 0;
5147c478bd9Sstevel@tonic-gate 			tree_node_t	*tnode;
5157c478bd9Sstevel@tonic-gate 			tree_node_t	*tptr;
5167c478bd9Sstevel@tonic-gate 			dev_t		save_dev = s1.st_dev;
5177c478bd9Sstevel@tonic-gate 			ino_t		save_ino = s1.st_ino;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 			/*
5207c478bd9Sstevel@tonic-gate 			 * We will be recursing into the directory so
5217c478bd9Sstevel@tonic-gate 			 * save the inode information to a search tree
5227c478bd9Sstevel@tonic-gate 			 * to avoid getting into an endless loop.
5237c478bd9Sstevel@tonic-gate 			 */
5247c478bd9Sstevel@tonic-gate 			if ((rc = add_tnode(&stree, save_dev, save_ino)) != 1) {
5257c478bd9Sstevel@tonic-gate 				if (rc == 0) {
5267c478bd9Sstevel@tonic-gate 					/*
5277c478bd9Sstevel@tonic-gate 					 * We've already visited this directory.
5287c478bd9Sstevel@tonic-gate 					 * Don't remove the search tree entry
5297c478bd9Sstevel@tonic-gate 					 * to make sure we don't get into an
5307c478bd9Sstevel@tonic-gate 					 * endless loop if revisited from a
5317c478bd9Sstevel@tonic-gate 					 * different part of the hierarchy.
5327c478bd9Sstevel@tonic-gate 					 */
5337c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
5347c478bd9Sstevel@tonic-gate 					    "%s: cycle detected: %s\n"),
5357c478bd9Sstevel@tonic-gate 					    cmd, source);
5367c478bd9Sstevel@tonic-gate 				} else {
5377c478bd9Sstevel@tonic-gate 					Perror(source);
5387c478bd9Sstevel@tonic-gate 				}
5397c478bd9Sstevel@tonic-gate 				return (1);
5407c478bd9Sstevel@tonic-gate 			}
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 			cmdarg = 0;
5437c478bd9Sstevel@tonic-gate 			rc = copydir(source, target);
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 			/*
5467c478bd9Sstevel@tonic-gate 			 * Create a tnode to get an index to the matching
5477c478bd9Sstevel@tonic-gate 			 * node (same dev and inode) in the search tree,
5487c478bd9Sstevel@tonic-gate 			 * then use the index to remove the matching node
5497c478bd9Sstevel@tonic-gate 			 * so it we do not wrongly detect a cycle when
5507c478bd9Sstevel@tonic-gate 			 * revisiting this directory from another part of
5517c478bd9Sstevel@tonic-gate 			 * the hierarchy.
5527c478bd9Sstevel@tonic-gate 			 */
5537c478bd9Sstevel@tonic-gate 			if ((tnode = create_tnode(save_dev,
5547c478bd9Sstevel@tonic-gate 			    save_ino)) == NULL) {
5557c478bd9Sstevel@tonic-gate 				Perror(source);
5567c478bd9Sstevel@tonic-gate 				return (1);
5577c478bd9Sstevel@tonic-gate 			}
5587c478bd9Sstevel@tonic-gate 			if ((tptr = avl_find(stree, tnode, &where)) != NULL) {
5597c478bd9Sstevel@tonic-gate 				avl_remove(stree, tptr);
5607c478bd9Sstevel@tonic-gate 			}
5617c478bd9Sstevel@tonic-gate 			free(tptr);
5627c478bd9Sstevel@tonic-gate 			free(tnode);
5637c478bd9Sstevel@tonic-gate 			return (rc);
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 		} else if (ISDEV(s1) && Rflg) {
5667c478bd9Sstevel@tonic-gate 			return (copyspecial(target));
5677c478bd9Sstevel@tonic-gate 		} else {
5687c478bd9Sstevel@tonic-gate 			goto copy;
5697c478bd9Sstevel@tonic-gate 		}
5707c478bd9Sstevel@tonic-gate 	}
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	if (mve) {
5737c478bd9Sstevel@tonic-gate 		if (rename(source, target) >= 0)
5747c478bd9Sstevel@tonic-gate 			return (0);
5757c478bd9Sstevel@tonic-gate 		if (errno != EXDEV) {
5767c478bd9Sstevel@tonic-gate 			if (errno == ENOTDIR && ISDIR(s1)) {
5777c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
5787c478bd9Sstevel@tonic-gate 				    gettext("%s: %s is a directory\n"),
5797c478bd9Sstevel@tonic-gate 				    cmd, source);
5807c478bd9Sstevel@tonic-gate 				return (1);
5817c478bd9Sstevel@tonic-gate 			}
5827c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5837c478bd9Sstevel@tonic-gate 			    gettext("%s: cannot rename %s to %s: "),
5847c478bd9Sstevel@tonic-gate 			    cmd, source, target);
5857c478bd9Sstevel@tonic-gate 			perror("");
5867c478bd9Sstevel@tonic-gate 			return (1);
5877c478bd9Sstevel@tonic-gate 		}
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 		/*
5907c478bd9Sstevel@tonic-gate 		 * cannot move a non-directory (source) onto an existing
5917c478bd9Sstevel@tonic-gate 		 * directory (target)
5927c478bd9Sstevel@tonic-gate 		 *
5937c478bd9Sstevel@tonic-gate 		 */
5947c478bd9Sstevel@tonic-gate 		if (targetexists && ISDIR(s2) && (!ISDIR(s1))) {
5957c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5967c478bd9Sstevel@tonic-gate 			    gettext("%s: cannot mv a non directory %s "
5977c478bd9Sstevel@tonic-gate 			    "over existing directory"
5987c478bd9Sstevel@tonic-gate 			    " %s \n"), cmd, source, target);
5997c478bd9Sstevel@tonic-gate 			return (1);
6007c478bd9Sstevel@tonic-gate 		}
6017c478bd9Sstevel@tonic-gate 		if (ISDIR(s1)) {
6027c478bd9Sstevel@tonic-gate #ifdef XPG4
6037c478bd9Sstevel@tonic-gate 			if (targetexists && ISDIR(s2)) {
6047c478bd9Sstevel@tonic-gate 				/* existing target dir must be empty */
6057c478bd9Sstevel@tonic-gate 				if (rmdir(target) < 0) {
6067c478bd9Sstevel@tonic-gate 					errno_save = errno;
6077c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
6087c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot rmdir %s: "),
6097c478bd9Sstevel@tonic-gate 					    cmd, target);
6107c478bd9Sstevel@tonic-gate 					errno = errno_save;
6117c478bd9Sstevel@tonic-gate 					perror("");
6127c478bd9Sstevel@tonic-gate 					return (1);
6137c478bd9Sstevel@tonic-gate 				}
6147c478bd9Sstevel@tonic-gate 			}
6157c478bd9Sstevel@tonic-gate #endif
6167c478bd9Sstevel@tonic-gate 			if ((n =  copydir(source, target)) == 0)
6177c478bd9Sstevel@tonic-gate 				(void) rmdir(source);
6187c478bd9Sstevel@tonic-gate 			return (n);
6197c478bd9Sstevel@tonic-gate 		}
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 		/* doors can't be moved across filesystems */
6227c478bd9Sstevel@tonic-gate 		if (ISDOOR(s1)) {
6237c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
6247c478bd9Sstevel@tonic-gate 			    gettext("%s: %s: can't move door "
6257c478bd9Sstevel@tonic-gate 			    "across file systems\n"), cmd, source);
6267c478bd9Sstevel@tonic-gate 			return (1);
6277c478bd9Sstevel@tonic-gate 		}
6287c478bd9Sstevel@tonic-gate 		/*
6297c478bd9Sstevel@tonic-gate 		 * File can't be renamed, try to recreate the symbolic
6307c478bd9Sstevel@tonic-gate 		 * link or special device, or copy the file wholesale
6317c478bd9Sstevel@tonic-gate 		 * between file systems.
6327c478bd9Sstevel@tonic-gate 		 */
6337c478bd9Sstevel@tonic-gate 		if (ISLNK(s1)) {
6347c478bd9Sstevel@tonic-gate 			register int	m;
6357c478bd9Sstevel@tonic-gate 			register mode_t md;
6367c478bd9Sstevel@tonic-gate 			char symln[PATH_MAX + 1];
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 			if (targetexists && unlink(target) < 0) {
6397c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6407c478bd9Sstevel@tonic-gate 				    gettext("%s: cannot unlink %s: "),
6417c478bd9Sstevel@tonic-gate 				    cmd, target);
6427c478bd9Sstevel@tonic-gate 				perror("");
6437c478bd9Sstevel@tonic-gate 				return (1);
6447c478bd9Sstevel@tonic-gate 			}
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 			if ((m = readlink(source, symln,
6477c478bd9Sstevel@tonic-gate 			    sizeof (symln) - 1)) < 0) {
6487c478bd9Sstevel@tonic-gate 				Perror(source);
6497c478bd9Sstevel@tonic-gate 				return (1);
6507c478bd9Sstevel@tonic-gate 			}
6517c478bd9Sstevel@tonic-gate 			symln[m] = '\0';
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 			md = umask(~(s1.st_mode & MODEBITS));
6547c478bd9Sstevel@tonic-gate 			if (symlink(symln, target) < 0) {
6557c478bd9Sstevel@tonic-gate 				Perror(target);
6567c478bd9Sstevel@tonic-gate 				return (1);
6577c478bd9Sstevel@tonic-gate 			}
6587c478bd9Sstevel@tonic-gate 			(void) umask(md);
6597c478bd9Sstevel@tonic-gate 			m = lchown(target, UID(s1), GID(s1));
6607c478bd9Sstevel@tonic-gate #ifdef XPG4
6617c478bd9Sstevel@tonic-gate 			if (m < 0) {
6627c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: cannot"
6637c478bd9Sstevel@tonic-gate 				    " change owner and group of"
6647c478bd9Sstevel@tonic-gate 				    " %s: "), cmd, target);
6657c478bd9Sstevel@tonic-gate 				perror("");
6667c478bd9Sstevel@tonic-gate 			}
6677c478bd9Sstevel@tonic-gate #endif
6687c478bd9Sstevel@tonic-gate 			goto cleanup;
6697c478bd9Sstevel@tonic-gate 		}
6707c478bd9Sstevel@tonic-gate 		if (ISDEV(s1)) {
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 			if (targetexists && unlink(target) < 0) {
6737c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6747c478bd9Sstevel@tonic-gate 				    gettext("%s: cannot unlink %s: "),
6757c478bd9Sstevel@tonic-gate 				    cmd, target);
6767c478bd9Sstevel@tonic-gate 				perror("");
6777c478bd9Sstevel@tonic-gate 				return (1);
6787c478bd9Sstevel@tonic-gate 			}
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 			if (mknod(target, s1.st_mode, s1.st_rdev) < 0) {
6817c478bd9Sstevel@tonic-gate 				Perror(target);
6827c478bd9Sstevel@tonic-gate 				return (1);
6837c478bd9Sstevel@tonic-gate 			}
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 			(void) chg_mode(target, UID(s1), GID(s1), FMODE(s1));
6867c478bd9Sstevel@tonic-gate 			(void) chg_time(target, s1);
6877c478bd9Sstevel@tonic-gate 			goto cleanup;
6887c478bd9Sstevel@tonic-gate 		}
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 		if (ISREG(s1)) {
6917c478bd9Sstevel@tonic-gate 			if (ISDIR(s2)) {
6927c478bd9Sstevel@tonic-gate 				if (targetexists && rmdir(target) < 0) {
6937c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
6947c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot rmdir %s: "),
6957c478bd9Sstevel@tonic-gate 					    cmd, target);
6967c478bd9Sstevel@tonic-gate 					perror("");
6977c478bd9Sstevel@tonic-gate 					return (1);
6987c478bd9Sstevel@tonic-gate 				}
6997c478bd9Sstevel@tonic-gate 			} else {
7007c478bd9Sstevel@tonic-gate 				if (targetexists && unlink(target) < 0) {
7017c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
7027c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot unlink %s: "),
7037c478bd9Sstevel@tonic-gate 					    cmd, target);
7047c478bd9Sstevel@tonic-gate 					perror("");
7057c478bd9Sstevel@tonic-gate 					return (1);
7067c478bd9Sstevel@tonic-gate 				}
7077c478bd9Sstevel@tonic-gate 			}
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate copy:
7117c478bd9Sstevel@tonic-gate 			/*
7127c478bd9Sstevel@tonic-gate 			 * If the source file is a symlink, and either
7137c478bd9Sstevel@tonic-gate 			 * -P or -H flag (only if -H is specified and the
7147c478bd9Sstevel@tonic-gate 			 * source file is not a command line argument)
7157c478bd9Sstevel@tonic-gate 			 * were specified, then action is taken on the symlink
7167c478bd9Sstevel@tonic-gate 			 * itself, not the file referenced by the symlink.
7177c478bd9Sstevel@tonic-gate 			 * Note: this is executed for 'cp' only.
7187c478bd9Sstevel@tonic-gate 			 */
7197c478bd9Sstevel@tonic-gate 			if (cpy && (Pflg || (Hflg && !cmdarg)) && (ISLNK(s1))) {
7207c478bd9Sstevel@tonic-gate 				int	m;
7217c478bd9Sstevel@tonic-gate 				mode_t	md;
7227c478bd9Sstevel@tonic-gate 				char symln[PATH_MAX + 1];
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 				m = readlink(source, symln, sizeof (symln) - 1);
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 				if (m < 0) {
7277c478bd9Sstevel@tonic-gate 					Perror(source);
7287c478bd9Sstevel@tonic-gate 					return (1);
7297c478bd9Sstevel@tonic-gate 				}
7307c478bd9Sstevel@tonic-gate 				symln[m] = '\0';
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 				/*
7337c478bd9Sstevel@tonic-gate 				 * Copy the sym link to the target.
7347c478bd9Sstevel@tonic-gate 				 * Note: If the target exists, write a
7357c478bd9Sstevel@tonic-gate 				 * diagnostic message, do nothing more
7367c478bd9Sstevel@tonic-gate 				 * with the source file, and return to
7377c478bd9Sstevel@tonic-gate 				 * process any remaining files.
7387c478bd9Sstevel@tonic-gate 				 */
7397c478bd9Sstevel@tonic-gate 				md = umask(~(s1.st_mode & MODEBITS));
7407c478bd9Sstevel@tonic-gate 				if (symlink(symln, target) < 0) {
7417c478bd9Sstevel@tonic-gate 					Perror(target);
7427c478bd9Sstevel@tonic-gate 					return (1);
7437c478bd9Sstevel@tonic-gate 				}
7447c478bd9Sstevel@tonic-gate 				(void) umask(md);
7457c478bd9Sstevel@tonic-gate 				m = lchown(target, UID(s1), GID(s1));
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 				if (m < 0) {
7487c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
7497c478bd9Sstevel@tonic-gate 					    "cp: cannot change owner and "
7507c478bd9Sstevel@tonic-gate 					    "group of %s:"), target);
7517c478bd9Sstevel@tonic-gate 					perror("");
7527c478bd9Sstevel@tonic-gate 				}
7537c478bd9Sstevel@tonic-gate 			} else {
7547c478bd9Sstevel@tonic-gate 				/*
7557c478bd9Sstevel@tonic-gate 				 * Copy the file.  If it happens to be a
7567c478bd9Sstevel@tonic-gate 				 * symlink, copy the file referenced
7577c478bd9Sstevel@tonic-gate 				 * by the symlink.
7587c478bd9Sstevel@tonic-gate 				 */
7597c478bd9Sstevel@tonic-gate 				fi = open(source, O_RDONLY);
7607c478bd9Sstevel@tonic-gate 				if (fi < 0) {
7617c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
7627c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot open %s: "),
7637c478bd9Sstevel@tonic-gate 					    cmd, source);
7647c478bd9Sstevel@tonic-gate 					perror("");
7657c478bd9Sstevel@tonic-gate 					return (1);
7667c478bd9Sstevel@tonic-gate 				}
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 				fo = creat(target, s1.st_mode & MODEBITS);
7697c478bd9Sstevel@tonic-gate 				if (fo < 0) {
7707c478bd9Sstevel@tonic-gate 					/*
7717c478bd9Sstevel@tonic-gate 					 * If -f and creat() failed, unlink
7727c478bd9Sstevel@tonic-gate 					 * and try again.
7737c478bd9Sstevel@tonic-gate 					 */
7747c478bd9Sstevel@tonic-gate 					if (fflg) {
7757c478bd9Sstevel@tonic-gate 						(void) unlink(target);
7767c478bd9Sstevel@tonic-gate 						fo = creat(target,
7777c478bd9Sstevel@tonic-gate 						    s1.st_mode & MODEBITS);
7787c478bd9Sstevel@tonic-gate 					}
7797c478bd9Sstevel@tonic-gate 				}
7807c478bd9Sstevel@tonic-gate 				if (fo < 0) {
7817c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
7827c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot create %s: "),
7837c478bd9Sstevel@tonic-gate 					    cmd, target);
7847c478bd9Sstevel@tonic-gate 					perror("");
7857c478bd9Sstevel@tonic-gate 					(void) close(fi);
7867c478bd9Sstevel@tonic-gate 					return (1);
7877c478bd9Sstevel@tonic-gate 				} else {
7887c478bd9Sstevel@tonic-gate 					/* stat the new file, its used below */
7897c478bd9Sstevel@tonic-gate 					(void) stat(target, &s2);
7907c478bd9Sstevel@tonic-gate 				}
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 				/*
7937c478bd9Sstevel@tonic-gate 				 * Set target's permissions to the source
7947c478bd9Sstevel@tonic-gate 				 * before any copying so that any partially
7957c478bd9Sstevel@tonic-gate 				 * copied file will have the source's
7967c478bd9Sstevel@tonic-gate 				 * permissions (at most) or umask permissions
7977c478bd9Sstevel@tonic-gate 				 * whichever is the most restrictive.
7987c478bd9Sstevel@tonic-gate 				 *
7997c478bd9Sstevel@tonic-gate 				 * ACL for regular files
8007c478bd9Sstevel@tonic-gate 				 */
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 				if (pflg || mve) {
8037c478bd9Sstevel@tonic-gate 					(void) chmod(target, FMODE(s1));
804fa9e4066Sahrens 					if (s1acl != NULL) {
805fa9e4066Sahrens 						if ((acl_set(target,
806fa9e4066Sahrens 						    s1acl)) < 0) {
8077c478bd9Sstevel@tonic-gate 							if (pflg || mve) {
8087c478bd9Sstevel@tonic-gate 								(void) fprintf(
8097c478bd9Sstevel@tonic-gate 								    stderr,
8107c478bd9Sstevel@tonic-gate 					"%s: failed to set acl entries on %s\n",
8117c478bd9Sstevel@tonic-gate 								    cmd,
8127c478bd9Sstevel@tonic-gate 								    target);
8137c478bd9Sstevel@tonic-gate 							}
81494d2b9abSmarks 							acl_free(s1acl);
81594d2b9abSmarks 							s1acl = NULL;
8167c478bd9Sstevel@tonic-gate 							/*
8177c478bd9Sstevel@tonic-gate 							 * else: silent and
8187c478bd9Sstevel@tonic-gate 							 * continue
8197c478bd9Sstevel@tonic-gate 							 */
8207c478bd9Sstevel@tonic-gate 						}
8217c478bd9Sstevel@tonic-gate 					}
8227c478bd9Sstevel@tonic-gate 				}
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 				if (fstat(fi, &s1) < 0) {
8257c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
8267c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot access %s\n"),
8277c478bd9Sstevel@tonic-gate 					    cmd, source);
8287c478bd9Sstevel@tonic-gate 					return (1);
8297c478bd9Sstevel@tonic-gate 				}
8307c478bd9Sstevel@tonic-gate 				if (IDENTICAL(s1, s2)) {
8317c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
8327c478bd9Sstevel@tonic-gate 					    gettext(
8337c478bd9Sstevel@tonic-gate 					    "%s: %s and %s are identical\n"),
8347c478bd9Sstevel@tonic-gate 					    cmd, source, target);
8357c478bd9Sstevel@tonic-gate 					return (1);
8367c478bd9Sstevel@tonic-gate 				}
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 				if (writefile(fi, fo, source, target,
8397c478bd9Sstevel@tonic-gate 				    &s1, &s2) != 0) {
8407c478bd9Sstevel@tonic-gate 					return (1);
8417c478bd9Sstevel@tonic-gate 				}
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 				(void) close(fi);
8447c478bd9Sstevel@tonic-gate 				if (close(fo) < 0) {
8457c478bd9Sstevel@tonic-gate 					Perror2(target, "write");
8467c478bd9Sstevel@tonic-gate 					return (1);
8477c478bd9Sstevel@tonic-gate 				}
8487c478bd9Sstevel@tonic-gate 			}
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 			if (pflg || atflg || mve) {
8517c478bd9Sstevel@tonic-gate 				attret = copyattributes(source, target);
8527c478bd9Sstevel@tonic-gate 				if (attret != 0 && !attrsilent) {
8537c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
8547c478bd9Sstevel@tonic-gate 						"%s: Failed to preserve"
8557c478bd9Sstevel@tonic-gate 						" extended attributes of file"
8567c478bd9Sstevel@tonic-gate 						" %s\n"), cmd, source);
8577c478bd9Sstevel@tonic-gate 				}
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 				if (mve && attret != 0) {
8607c478bd9Sstevel@tonic-gate 					(void) unlink(target);
8617c478bd9Sstevel@tonic-gate 					return (1);
8627c478bd9Sstevel@tonic-gate 				}
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 				if (attrsilent)
8657c478bd9Sstevel@tonic-gate 					attret = 0;
8667c478bd9Sstevel@tonic-gate 			}
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 			/*
8697c478bd9Sstevel@tonic-gate 			 * XPG4: the write system call will clear setgid
8707c478bd9Sstevel@tonic-gate 			 * and setuid bits, so set them again.
8717c478bd9Sstevel@tonic-gate 			 */
8727c478bd9Sstevel@tonic-gate 			if (pflg || mve) {
8737c478bd9Sstevel@tonic-gate 				if ((ret = chg_mode(target, UID(s1), GID(s1),
8747c478bd9Sstevel@tonic-gate 				    FMODE(s1))) > 0)
8757c478bd9Sstevel@tonic-gate 					return (1);
876d2443e76Smarks 				/*
877d2443e76Smarks 				 * Reapply ACL, since chmod may have
878d2443e76Smarks 				 * altered ACL
879d2443e76Smarks 				 */
880d2443e76Smarks 				if (s1acl != NULL) {
881d2443e76Smarks 					if ((acl_set(target, s1acl)) < 0) {
882d2443e76Smarks 						if (pflg || mve) {
883d2443e76Smarks 							(void) fprintf(
884d2443e76Smarks 							    stderr,
885d2443e76Smarks 					"%s: failed to set acl entries on %s\n",
886d2443e76Smarks 							    cmd,
887d2443e76Smarks 							    target);
888d2443e76Smarks 						}
889d2443e76Smarks 						/*
890d2443e76Smarks 						 * else: silent and
891d2443e76Smarks 						 * continue
892d2443e76Smarks 						 */
893d2443e76Smarks 					}
894d2443e76Smarks 				}
8957c478bd9Sstevel@tonic-gate 				if ((ret = chg_time(target, s1)) > 0)
8967c478bd9Sstevel@tonic-gate 					return (1);
8977c478bd9Sstevel@tonic-gate 			}
8987c478bd9Sstevel@tonic-gate 			if (cpy) {
8997c478bd9Sstevel@tonic-gate 				if (attret != 0)
9007c478bd9Sstevel@tonic-gate 					return (1);
9017c478bd9Sstevel@tonic-gate 				return (0);
9027c478bd9Sstevel@tonic-gate 			}
9037c478bd9Sstevel@tonic-gate 			goto cleanup;
9047c478bd9Sstevel@tonic-gate 		}
9057c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
9067c478bd9Sstevel@tonic-gate 		    gettext("%s: %s: unknown file type 0x%x\n"), cmd,
9077c478bd9Sstevel@tonic-gate 			source, (s1.st_mode & S_IFMT));
9087c478bd9Sstevel@tonic-gate 		return (1);
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate cleanup:
9117c478bd9Sstevel@tonic-gate 		if (unlink(source) < 0) {
9127c478bd9Sstevel@tonic-gate 			(void) unlink(target);
9137c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9147c478bd9Sstevel@tonic-gate 			    gettext("%s: cannot unlink %s: "),
9157c478bd9Sstevel@tonic-gate 			    cmd, source);
9167c478bd9Sstevel@tonic-gate 			perror("");
9177c478bd9Sstevel@tonic-gate 			return (1);
9187c478bd9Sstevel@tonic-gate 		}
9197c478bd9Sstevel@tonic-gate 		if (attret != 0)
9207c478bd9Sstevel@tonic-gate 			return (attret);
9217c478bd9Sstevel@tonic-gate 		return (ret);
9227c478bd9Sstevel@tonic-gate 	}
9237c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
924a77d64afScf46844 	return (ret);
9257c478bd9Sstevel@tonic-gate }
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate static int
9287c478bd9Sstevel@tonic-gate writefile(int fi, int fo, char *source, char *target,
9297c478bd9Sstevel@tonic-gate 		struct stat *s1p, struct stat *s2p)
9307c478bd9Sstevel@tonic-gate {
9317c478bd9Sstevel@tonic-gate 	int mapsize, munmapsize;
9327c478bd9Sstevel@tonic-gate 	caddr_t cp;
9337c478bd9Sstevel@tonic-gate 	off_t filesize = s1p->st_size;
9347c478bd9Sstevel@tonic-gate 	off_t offset;
9357c478bd9Sstevel@tonic-gate 	int nbytes;
9367c478bd9Sstevel@tonic-gate 	int remains;
9377c478bd9Sstevel@tonic-gate 	int n;
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 	if (ISREG(*s1p) && s1p->st_size > SMALLFILESIZE) {
9407c478bd9Sstevel@tonic-gate 		/*
9417c478bd9Sstevel@tonic-gate 		 * Determine size of initial mapping.  This will determine the
9427c478bd9Sstevel@tonic-gate 		 * size of the address space chunk we work with.  This initial
9437c478bd9Sstevel@tonic-gate 		 * mapping size will be used to perform munmap() in the future.
9447c478bd9Sstevel@tonic-gate 		 */
9457c478bd9Sstevel@tonic-gate 		mapsize = MAXMAPSIZE;
9467c478bd9Sstevel@tonic-gate 		if (s1p->st_size < mapsize) mapsize = s1p->st_size;
9477c478bd9Sstevel@tonic-gate 		munmapsize = mapsize;
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 		/*
9507c478bd9Sstevel@tonic-gate 		 * Mmap time!
9517c478bd9Sstevel@tonic-gate 		 */
9527c478bd9Sstevel@tonic-gate 		if ((cp = mmap((caddr_t)NULL, mapsize, PROT_READ,
9537c478bd9Sstevel@tonic-gate 		    MAP_SHARED, fi, (off_t)0)) == MAP_FAILED)
9547c478bd9Sstevel@tonic-gate 			mapsize = 0;   /* can't mmap today */
9557c478bd9Sstevel@tonic-gate 	} else
9567c478bd9Sstevel@tonic-gate 		mapsize = 0;
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	if (mapsize != 0) {
9597c478bd9Sstevel@tonic-gate 		offset = 0;
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 		for (;;) {
9627c478bd9Sstevel@tonic-gate 			nbytes = write(fo, cp, mapsize);
9637c478bd9Sstevel@tonic-gate 			/*
9647c478bd9Sstevel@tonic-gate 			 * if we write less than the mmaped size it's due to a
9657c478bd9Sstevel@tonic-gate 			 * media error on the input file or out of space on
9667c478bd9Sstevel@tonic-gate 			 * the output file.  So, try again, and look for errno.
9677c478bd9Sstevel@tonic-gate 			 */
9687c478bd9Sstevel@tonic-gate 			if ((nbytes >= 0) && (nbytes != (int)mapsize)) {
9697c478bd9Sstevel@tonic-gate 				remains = mapsize - nbytes;
9707c478bd9Sstevel@tonic-gate 				while (remains > 0) {
9717c478bd9Sstevel@tonic-gate 					nbytes = write(fo,
9727c478bd9Sstevel@tonic-gate 					    cp + mapsize - remains, remains);
9737c478bd9Sstevel@tonic-gate 					if (nbytes < 0) {
9747c478bd9Sstevel@tonic-gate 						if (errno == ENOSPC)
9757c478bd9Sstevel@tonic-gate 							Perror(target);
9767c478bd9Sstevel@tonic-gate 						else
9777c478bd9Sstevel@tonic-gate 							Perror(source);
9787c478bd9Sstevel@tonic-gate 						(void) close(fi);
9797c478bd9Sstevel@tonic-gate 						(void) close(fo);
9807c478bd9Sstevel@tonic-gate 						(void) munmap(cp, munmapsize);
9817c478bd9Sstevel@tonic-gate 						if (ISREG(*s2p))
9827c478bd9Sstevel@tonic-gate 							(void) unlink(target);
9837c478bd9Sstevel@tonic-gate 						return (1);
9847c478bd9Sstevel@tonic-gate 					}
9857c478bd9Sstevel@tonic-gate 					remains -= nbytes;
9867c478bd9Sstevel@tonic-gate 					if (remains == 0)
9877c478bd9Sstevel@tonic-gate 						nbytes = mapsize;
9887c478bd9Sstevel@tonic-gate 				}
9897c478bd9Sstevel@tonic-gate 			}
9907c478bd9Sstevel@tonic-gate 			/*
9917c478bd9Sstevel@tonic-gate 			 * although the write manual page doesn't specify this
9927c478bd9Sstevel@tonic-gate 			 * as a possible errno, it is set when the nfs read
9937c478bd9Sstevel@tonic-gate 			 * via the mmap'ed file is accessed, so report the
9947c478bd9Sstevel@tonic-gate 			 * problem as a source access problem, not a target file
9957c478bd9Sstevel@tonic-gate 			 * problem
9967c478bd9Sstevel@tonic-gate 			 */
9977c478bd9Sstevel@tonic-gate 			if (nbytes < 0) {
9987c478bd9Sstevel@tonic-gate 				if (errno == EACCES)
9997c478bd9Sstevel@tonic-gate 					Perror(source);
10007c478bd9Sstevel@tonic-gate 				else
10017c478bd9Sstevel@tonic-gate 					Perror(target);
10027c478bd9Sstevel@tonic-gate 				(void) close(fi);
10037c478bd9Sstevel@tonic-gate 				(void) close(fo);
10047c478bd9Sstevel@tonic-gate 				(void) munmap(cp, munmapsize);
10057c478bd9Sstevel@tonic-gate 				if (ISREG(*s2p))
10067c478bd9Sstevel@tonic-gate 					(void) unlink(target);
10077c478bd9Sstevel@tonic-gate 				return (1);
10087c478bd9Sstevel@tonic-gate 			}
10097c478bd9Sstevel@tonic-gate 			filesize -= nbytes;
10107c478bd9Sstevel@tonic-gate 			if (filesize == 0)
10117c478bd9Sstevel@tonic-gate 				break;
10127c478bd9Sstevel@tonic-gate 			offset += nbytes;
10137c478bd9Sstevel@tonic-gate 			if (filesize < mapsize)
10147c478bd9Sstevel@tonic-gate 				mapsize = filesize;
10157c478bd9Sstevel@tonic-gate 			if (mmap(cp, mapsize, PROT_READ, MAP_SHARED | MAP_FIXED,
10167c478bd9Sstevel@tonic-gate 			    fi, offset) == MAP_FAILED) {
10177c478bd9Sstevel@tonic-gate 				Perror(source);
10187c478bd9Sstevel@tonic-gate 				(void) close(fi);
10197c478bd9Sstevel@tonic-gate 				(void) close(fo);
10207c478bd9Sstevel@tonic-gate 				(void) munmap(cp, munmapsize);
10217c478bd9Sstevel@tonic-gate 				if (ISREG(*s2p))
10227c478bd9Sstevel@tonic-gate 					(void) unlink(target);
10237c478bd9Sstevel@tonic-gate 				return (1);
10247c478bd9Sstevel@tonic-gate 			}
10257c478bd9Sstevel@tonic-gate 		}
10267c478bd9Sstevel@tonic-gate 		(void) munmap(cp, munmapsize);
10277c478bd9Sstevel@tonic-gate 	} else {
10287c478bd9Sstevel@tonic-gate 		char buf[SMALLFILESIZE];
10297c478bd9Sstevel@tonic-gate 		for (;;) {
10307c478bd9Sstevel@tonic-gate 			n = read(fi, buf, sizeof (buf));
10317c478bd9Sstevel@tonic-gate 			if (n == 0) {
10327c478bd9Sstevel@tonic-gate 				return (0);
10337c478bd9Sstevel@tonic-gate 			} else if (n < 0) {
10347c478bd9Sstevel@tonic-gate 				Perror2(source, "read");
10357c478bd9Sstevel@tonic-gate 				(void) close(fi);
10367c478bd9Sstevel@tonic-gate 				(void) close(fo);
10377c478bd9Sstevel@tonic-gate 				if (ISREG(*s2p))
10387c478bd9Sstevel@tonic-gate 					(void) unlink(target);
10397c478bd9Sstevel@tonic-gate 				return (1);
10407c478bd9Sstevel@tonic-gate 			} else if (write(fo, buf, n) != n) {
10417c478bd9Sstevel@tonic-gate 				Perror2(target, "write");
10427c478bd9Sstevel@tonic-gate 				(void) close(fi);
10437c478bd9Sstevel@tonic-gate 				(void) close(fo);
10447c478bd9Sstevel@tonic-gate 				if (ISREG(*s2p))
10457c478bd9Sstevel@tonic-gate 					(void) unlink(target);
10467c478bd9Sstevel@tonic-gate 				return (1);
10477c478bd9Sstevel@tonic-gate 			}
10487c478bd9Sstevel@tonic-gate 		}
10497c478bd9Sstevel@tonic-gate 	}
10507c478bd9Sstevel@tonic-gate 	return (0);
10517c478bd9Sstevel@tonic-gate }
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate /*
10547c478bd9Sstevel@tonic-gate  * create_tnode()
10557c478bd9Sstevel@tonic-gate  *
10567c478bd9Sstevel@tonic-gate  * Create a node for use with the search tree which contains the
10577c478bd9Sstevel@tonic-gate  * inode information (device id and inode number).
10587c478bd9Sstevel@tonic-gate  *
10597c478bd9Sstevel@tonic-gate  * Input
10607c478bd9Sstevel@tonic-gate  *	dev	- device id
10617c478bd9Sstevel@tonic-gate  *	ino	- inode number
10627c478bd9Sstevel@tonic-gate  *
10637c478bd9Sstevel@tonic-gate  * Output
10647c478bd9Sstevel@tonic-gate  *	tnode	- NULL on error, otherwise returns a tnode structure
10657c478bd9Sstevel@tonic-gate  *		  which contains the input device id and inode number.
10667c478bd9Sstevel@tonic-gate  */
10677c478bd9Sstevel@tonic-gate static tree_node_t *
10687c478bd9Sstevel@tonic-gate create_tnode(dev_t dev, ino_t ino)
10697c478bd9Sstevel@tonic-gate {
10707c478bd9Sstevel@tonic-gate 	tree_node_t	*tnode;
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 	if ((tnode = (tree_node_t *)malloc(sizeof (tree_node_t))) != NULL) {
10737c478bd9Sstevel@tonic-gate 		tnode->node_dev = dev;
10747c478bd9Sstevel@tonic-gate 		tnode->node_ino = ino;
10757c478bd9Sstevel@tonic-gate 	}
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	return (tnode);
10787c478bd9Sstevel@tonic-gate }
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate static int
10817c478bd9Sstevel@tonic-gate chkfiles(char *source, char **to)
10827c478bd9Sstevel@tonic-gate {
10837c478bd9Sstevel@tonic-gate 	char	*buf = (char *)NULL;
10847c478bd9Sstevel@tonic-gate 	int	(*statf)() = (cpy &&
10857c478bd9Sstevel@tonic-gate 		    !(Pflg || (Hflg && !cmdarg))) ? stat : lstat;
10867c478bd9Sstevel@tonic-gate 	char    *target = *to;
1087fa9e4066Sahrens 	int	error;
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 	/*
10907c478bd9Sstevel@tonic-gate 	 * Make sure source file exists.
10917c478bd9Sstevel@tonic-gate 	 */
10927c478bd9Sstevel@tonic-gate 	if ((*statf)(source, &s1) < 0) {
10937c478bd9Sstevel@tonic-gate 		/*
10947c478bd9Sstevel@tonic-gate 		 * Keep the old error message except when someone tries to
10957c478bd9Sstevel@tonic-gate 		 * mv/cp/ln a symbolic link that has a trailing slash and
10967c478bd9Sstevel@tonic-gate 		 * points to a file.
10977c478bd9Sstevel@tonic-gate 		 */
10987c478bd9Sstevel@tonic-gate 		if (errno == ENOTDIR)
10997c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s: %s\n", cmd, source,
11007c478bd9Sstevel@tonic-gate 			    strerror(errno));
11017c478bd9Sstevel@tonic-gate 		else
11027c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
11037c478bd9Sstevel@tonic-gate 			    gettext("%s: cannot access %s\n"), cmd, source);
11047c478bd9Sstevel@tonic-gate 		return (1);
11057c478bd9Sstevel@tonic-gate 	}
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	/*
11087c478bd9Sstevel@tonic-gate 	 * Get ACL info: don't bother with ln or mv'ing symlinks
11097c478bd9Sstevel@tonic-gate 	 */
11107c478bd9Sstevel@tonic-gate 	if ((!lnk) && !(mve && ISLNK(s1))) {
1111fa9e4066Sahrens 		if (s1acl != NULL) {
1112fa9e4066Sahrens 			acl_free(s1acl);
1113fa9e4066Sahrens 			s1acl = NULL;
11147c478bd9Sstevel@tonic-gate 		}
1115fa9e4066Sahrens 		if ((error = acl_get(source, ACL_NO_TRIVIAL, &s1acl)) != 0) {
11167c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1117fa9e4066Sahrens 			    "%s: failed to get acl entries: %s\n", source,
1118fa9e4066Sahrens 			    acl_strerror(error));
11197c478bd9Sstevel@tonic-gate 			return (1);
11207c478bd9Sstevel@tonic-gate 		}
11217c478bd9Sstevel@tonic-gate 		/* else: just permission bits */
11227c478bd9Sstevel@tonic-gate 	}
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	/*
11257c478bd9Sstevel@tonic-gate 	 * If stat fails, then the target doesn't exist,
11267c478bd9Sstevel@tonic-gate 	 * we will create a new target with default file type of regular.
11277c478bd9Sstevel@tonic-gate 	 */
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	FTYPE(s2) = S_IFREG;
11307c478bd9Sstevel@tonic-gate 	targetexists = 0;
11317c478bd9Sstevel@tonic-gate 	if ((*statf)(target, &s2) >= 0) {
11327c478bd9Sstevel@tonic-gate 		if (ISLNK(s2))
11337c478bd9Sstevel@tonic-gate 			(void) stat(target, &s2);
11347c478bd9Sstevel@tonic-gate 		/*
11357c478bd9Sstevel@tonic-gate 		 * If target is a directory,
11367c478bd9Sstevel@tonic-gate 		 * make complete name of new file
11377c478bd9Sstevel@tonic-gate 		 * within that directory.
11387c478bd9Sstevel@tonic-gate 		 */
11397c478bd9Sstevel@tonic-gate 		if (ISDIR(s2)) {
11407c478bd9Sstevel@tonic-gate 			size_t len;
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 			len = strlen(target) + strlen(dname(source)) + 4;
11437c478bd9Sstevel@tonic-gate 			if ((buf = (char *)malloc(len)) == NULL) {
11447c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
11457c478bd9Sstevel@tonic-gate 				    gettext("%s: Insufficient memory to "
11467c478bd9Sstevel@tonic-gate 					"%s %s\n "), cmd, cmd, source);
11477c478bd9Sstevel@tonic-gate 				exit(3);
11487c478bd9Sstevel@tonic-gate 			}
11497c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, len, "%s/%s",
11507c478bd9Sstevel@tonic-gate 			    target, dname(source));
11517c478bd9Sstevel@tonic-gate 			*to = target = buf;
11527c478bd9Sstevel@tonic-gate 		}
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 		if ((*statf)(target, &s2) >= 0) {
11557c478bd9Sstevel@tonic-gate 			int overwrite	= FALSE;
11567c478bd9Sstevel@tonic-gate 			int override	= FALSE;
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 			targetexists++;
11597c478bd9Sstevel@tonic-gate 			if (cpy || mve) {
11607c478bd9Sstevel@tonic-gate 				/*
11617c478bd9Sstevel@tonic-gate 				 * For cp and mv, it is an error if the
11627c478bd9Sstevel@tonic-gate 				 * source and target are the same file.
11637c478bd9Sstevel@tonic-gate 				 * Check for the same inode and file
11647c478bd9Sstevel@tonic-gate 				 * system, but don't check for the same
11657c478bd9Sstevel@tonic-gate 				 * absolute pathname because it is an
11667c478bd9Sstevel@tonic-gate 				 * error when the source and target are
11677c478bd9Sstevel@tonic-gate 				 * hard links to the same file.
11687c478bd9Sstevel@tonic-gate 				 */
11697c478bd9Sstevel@tonic-gate 				if (IDENTICAL(s1, s2)) {
11707c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
11717c478bd9Sstevel@tonic-gate 					    gettext(
11727c478bd9Sstevel@tonic-gate 					    "%s: %s and %s are identical\n"),
11737c478bd9Sstevel@tonic-gate 					    cmd, source, target);
11747c478bd9Sstevel@tonic-gate 					if (buf != NULL)
11757c478bd9Sstevel@tonic-gate 						free(buf);
11767c478bd9Sstevel@tonic-gate 					return (1);
11777c478bd9Sstevel@tonic-gate 				}
11787c478bd9Sstevel@tonic-gate 			}
11797c478bd9Sstevel@tonic-gate 			if (lnk) {
11807c478bd9Sstevel@tonic-gate 				/*
11817c478bd9Sstevel@tonic-gate 				 * For ln, it is an error if the source and
11827c478bd9Sstevel@tonic-gate 				 * target are identical files (same inode,
11837c478bd9Sstevel@tonic-gate 				 * same file system, and filenames resolve
11847c478bd9Sstevel@tonic-gate 				 * to same absolute pathname).
11857c478bd9Sstevel@tonic-gate 				 */
11867c478bd9Sstevel@tonic-gate 				if (!chk_different(source, target)) {
11877c478bd9Sstevel@tonic-gate 					if (buf != NULL)
11887c478bd9Sstevel@tonic-gate 						free(buf);
11897c478bd9Sstevel@tonic-gate 					return (1);
11907c478bd9Sstevel@tonic-gate 				}
11917c478bd9Sstevel@tonic-gate 			}
11927c478bd9Sstevel@tonic-gate 			if (lnk && !silent) {
11937c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
11947c478bd9Sstevel@tonic-gate 				    gettext("%s: %s: File exists\n"),
11957c478bd9Sstevel@tonic-gate 				    cmd, target);
11967c478bd9Sstevel@tonic-gate 				if (buf != NULL)
11977c478bd9Sstevel@tonic-gate 					free(buf);
11987c478bd9Sstevel@tonic-gate 				return (1);
11997c478bd9Sstevel@tonic-gate 			}
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 			/*
12027c478bd9Sstevel@tonic-gate 			 * overwrite:
12037c478bd9Sstevel@tonic-gate 			 * If the user does not have access to
12047c478bd9Sstevel@tonic-gate 			 * the target, ask ----if it is not
12057c478bd9Sstevel@tonic-gate 			 * silent and user invoked command
12067c478bd9Sstevel@tonic-gate 			 * interactively.
12077c478bd9Sstevel@tonic-gate 			 *
12087c478bd9Sstevel@tonic-gate 			 * override:
12097c478bd9Sstevel@tonic-gate 			 * If not silent, and stdin is a terminal, and
12107c478bd9Sstevel@tonic-gate 			 * there's no write access, and the file isn't a
12117c478bd9Sstevel@tonic-gate 			 * symbolic link, ask for permission.
12127c478bd9Sstevel@tonic-gate 			 *
12137c478bd9Sstevel@tonic-gate 			 * XPG4: both overwrite and override:
12147c478bd9Sstevel@tonic-gate 			 * ask only one question.
12157c478bd9Sstevel@tonic-gate 			 *
12167c478bd9Sstevel@tonic-gate 			 * TRANSLATION_NOTE - The following messages will
12177c478bd9Sstevel@tonic-gate 			 * contain the first character of the strings for
12187c478bd9Sstevel@tonic-gate 			 * "yes" and "no" defined in the file
12197c478bd9Sstevel@tonic-gate 			 * "nl_langinfo.po".  After substitution, the
12207c478bd9Sstevel@tonic-gate 			 * message will appear as follows:
12217c478bd9Sstevel@tonic-gate 			 *	<cmd>: overwrite <filename> (y/n)?
12227c478bd9Sstevel@tonic-gate 			 * where <cmd> is the name of the command
12237c478bd9Sstevel@tonic-gate 			 * (cp, mv) and <filename> is the destination file
12247c478bd9Sstevel@tonic-gate 			 */
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 			overwrite = iflg && !silent && use_stdin();
12287c478bd9Sstevel@tonic-gate 			override = !cpy && (access(target, 2) < 0) &&
12297c478bd9Sstevel@tonic-gate 			    !silent && use_stdin() && !ISLNK(s2);
12307c478bd9Sstevel@tonic-gate 
1231*0f6e7ba6Sas145665 			if (overwrite && override) {
12327c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
12337c478bd9Sstevel@tonic-gate 				    gettext("%s: overwrite %s and override "
12347c478bd9Sstevel@tonic-gate 				    "protection %o (%s/%s)? "), cmd, target,
12357c478bd9Sstevel@tonic-gate 				    FMODE(s2) & MODEBITS, yeschr, nochr);
1236*0f6e7ba6Sas145665 				if (getresp()) {
1237*0f6e7ba6Sas145665 					if (buf != NULL)
1238*0f6e7ba6Sas145665 						free(buf);
1239*0f6e7ba6Sas145665 					return (2);
1240*0f6e7ba6Sas145665 				}
1241*0f6e7ba6Sas145665 			} else if (overwrite && ISREG(s2)) {
12427c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
12437c478bd9Sstevel@tonic-gate 				    gettext("%s: overwrite %s (%s/%s)? "),
12447c478bd9Sstevel@tonic-gate 				    cmd, target, yeschr, nochr);
1245*0f6e7ba6Sas145665 				if (getresp()) {
1246*0f6e7ba6Sas145665 					if (buf != NULL)
1247*0f6e7ba6Sas145665 						free(buf);
1248*0f6e7ba6Sas145665 					return (2);
1249*0f6e7ba6Sas145665 				}
1250*0f6e7ba6Sas145665 			} else if (override) {
12517c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
12527c478bd9Sstevel@tonic-gate 				    gettext("%s: %s: override protection "
12537c478bd9Sstevel@tonic-gate 				    /*CSTYLED*/
12547c478bd9Sstevel@tonic-gate 				    "%o (%s/%s)? "),
12557c478bd9Sstevel@tonic-gate 				    /*CSTYLED*/
12567c478bd9Sstevel@tonic-gate 				    cmd, target, FMODE(s2) & MODEBITS,
12577c478bd9Sstevel@tonic-gate 				    yeschr, nochr);
12587c478bd9Sstevel@tonic-gate 				if (getresp()) {
12597c478bd9Sstevel@tonic-gate 					if (buf != NULL)
12607c478bd9Sstevel@tonic-gate 						free(buf);
12617c478bd9Sstevel@tonic-gate 					return (2);
12627c478bd9Sstevel@tonic-gate 				}
12637c478bd9Sstevel@tonic-gate 			}
1264*0f6e7ba6Sas145665 
12657c478bd9Sstevel@tonic-gate 			if (lnk && unlink(target) < 0) {
12667c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
12677c478bd9Sstevel@tonic-gate 				    gettext("%s: cannot unlink %s: "),
12687c478bd9Sstevel@tonic-gate 				    cmd, target);
12697c478bd9Sstevel@tonic-gate 				perror("");
12707c478bd9Sstevel@tonic-gate 				return (1);
12717c478bd9Sstevel@tonic-gate 			}
12727c478bd9Sstevel@tonic-gate 		}
12737c478bd9Sstevel@tonic-gate 	}
12747c478bd9Sstevel@tonic-gate 	return (0);
12757c478bd9Sstevel@tonic-gate }
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate /*
12787c478bd9Sstevel@tonic-gate  * check whether source and target are different
12797c478bd9Sstevel@tonic-gate  * return 1 when they are different
12807c478bd9Sstevel@tonic-gate  * return 0 when they are identical, or when unable to resolve a pathname
12817c478bd9Sstevel@tonic-gate  */
12827c478bd9Sstevel@tonic-gate static int
12837c478bd9Sstevel@tonic-gate chk_different(char *source, char *target)
12847c478bd9Sstevel@tonic-gate {
12857c478bd9Sstevel@tonic-gate 	char	rtarget[PATH_MAX], rsource[PATH_MAX];
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 	if (IDENTICAL(s1, s2)) {
12887c478bd9Sstevel@tonic-gate 		/*
12897c478bd9Sstevel@tonic-gate 		 * IDENTICAL will be true for hard links, therefore
12907c478bd9Sstevel@tonic-gate 		 * check whether the filenames are different
12917c478bd9Sstevel@tonic-gate 		 */
12927c478bd9Sstevel@tonic-gate 		if ((getrealpath(source, rsource) == 0) ||
12937c478bd9Sstevel@tonic-gate 		    (getrealpath(target, rtarget) == 0)) {
12947c478bd9Sstevel@tonic-gate 			return (0);
12957c478bd9Sstevel@tonic-gate 		}
12967c478bd9Sstevel@tonic-gate 		if (strncmp(rsource, rtarget, PATH_MAX) == 0) {
12977c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
12987c478bd9Sstevel@tonic-gate 			    "%s: %s and %s are identical\n"),
12997c478bd9Sstevel@tonic-gate 			    cmd, source, target);
13007c478bd9Sstevel@tonic-gate 			return (0);
13017c478bd9Sstevel@tonic-gate 		}
13027c478bd9Sstevel@tonic-gate 	}
13037c478bd9Sstevel@tonic-gate 	return (1);
13047c478bd9Sstevel@tonic-gate }
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate /*
13077c478bd9Sstevel@tonic-gate  * get real path (resolved absolute pathname)
13087c478bd9Sstevel@tonic-gate  * return 1 on success, 0 on failure
13097c478bd9Sstevel@tonic-gate  */
13107c478bd9Sstevel@tonic-gate static int
13117c478bd9Sstevel@tonic-gate getrealpath(char *path, char *rpath)
13127c478bd9Sstevel@tonic-gate {
13137c478bd9Sstevel@tonic-gate 	if (realpath(path, rpath) == NULL) {
13147c478bd9Sstevel@tonic-gate 		int	errno_save = errno;
13157c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
13167c478bd9Sstevel@tonic-gate 		    "%s: can't resolve path %s: "), cmd, path);
13177c478bd9Sstevel@tonic-gate 		errno = errno_save;
13187c478bd9Sstevel@tonic-gate 		perror("");
13197c478bd9Sstevel@tonic-gate 		return (0);
13207c478bd9Sstevel@tonic-gate 	}
13217c478bd9Sstevel@tonic-gate 	return (1);
13227c478bd9Sstevel@tonic-gate }
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate static int
13257c478bd9Sstevel@tonic-gate rcopy(char *from, char *to)
13267c478bd9Sstevel@tonic-gate {
13277c478bd9Sstevel@tonic-gate 	DIR *fold = opendir(from);
13287c478bd9Sstevel@tonic-gate 	struct dirent *dp;
13297c478bd9Sstevel@tonic-gate 	struct stat statb, s1save;
13307c478bd9Sstevel@tonic-gate 	int errs = 0;
13317c478bd9Sstevel@tonic-gate 	char fromname[PATH_MAX];
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 	if (fold == 0 || ((pflg || mve) && fstat(fold->dd_fd, &statb) < 0)) {
13347c478bd9Sstevel@tonic-gate 		Perror(from);
13357c478bd9Sstevel@tonic-gate 		return (1);
13367c478bd9Sstevel@tonic-gate 	}
13377c478bd9Sstevel@tonic-gate 	if (pflg || mve) {
13387c478bd9Sstevel@tonic-gate 		/*
13397c478bd9Sstevel@tonic-gate 		 * Save s1 (stat information for source dir) so that
13407c478bd9Sstevel@tonic-gate 		 * mod and access times can be reserved during "cp -p"
13417c478bd9Sstevel@tonic-gate 		 * or mv, since s1 gets overwritten.
13427c478bd9Sstevel@tonic-gate 		 */
13437c478bd9Sstevel@tonic-gate 		s1save = s1;
13447c478bd9Sstevel@tonic-gate 	}
13457c478bd9Sstevel@tonic-gate 	for (;;) {
13467c478bd9Sstevel@tonic-gate 		dp = readdir(fold);
13477c478bd9Sstevel@tonic-gate 		if (dp == 0) {
13487c478bd9Sstevel@tonic-gate 			(void) closedir(fold);
13497c478bd9Sstevel@tonic-gate 			if (pflg || mve)
13507c478bd9Sstevel@tonic-gate 				return (chg_time(to, s1save) + errs);
13517c478bd9Sstevel@tonic-gate 			return (errs);
13527c478bd9Sstevel@tonic-gate 		}
13537c478bd9Sstevel@tonic-gate 		if (dp->d_ino == 0)
13547c478bd9Sstevel@tonic-gate 			continue;
13557c478bd9Sstevel@tonic-gate 		if ((strcmp(dp->d_name, ".") == 0) ||
13567c478bd9Sstevel@tonic-gate 		    (strcmp(dp->d_name, "..") == 0))
13577c478bd9Sstevel@tonic-gate 			continue;
13587c478bd9Sstevel@tonic-gate 		if (strlen(from)+1+strlen(dp->d_name) >=
13597c478bd9Sstevel@tonic-gate 		    sizeof (fromname) - 1) {
13607c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
13617c478bd9Sstevel@tonic-gate 			    gettext("%s : %s/%s: Name too long\n"),
13627c478bd9Sstevel@tonic-gate 			    cmd, from, dp->d_name);
13637c478bd9Sstevel@tonic-gate 			errs++;
13647c478bd9Sstevel@tonic-gate 			continue;
13657c478bd9Sstevel@tonic-gate 		}
13667c478bd9Sstevel@tonic-gate 		(void) snprintf(fromname, sizeof (fromname),
13677c478bd9Sstevel@tonic-gate 		    "%s/%s", from, dp->d_name);
13687c478bd9Sstevel@tonic-gate 		errs += cpymve(fromname, to);
13697c478bd9Sstevel@tonic-gate 	}
13707c478bd9Sstevel@tonic-gate }
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate static char *
13737c478bd9Sstevel@tonic-gate dname(char *name)
13747c478bd9Sstevel@tonic-gate {
13757c478bd9Sstevel@tonic-gate 	register char *p;
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 	/*
13787c478bd9Sstevel@tonic-gate 	 * Return just the file name given the complete path.
13797c478bd9Sstevel@tonic-gate 	 * Like basename(1).
13807c478bd9Sstevel@tonic-gate 	 */
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 	p = name;
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 	/*
13857c478bd9Sstevel@tonic-gate 	 * While there are characters left,
13867c478bd9Sstevel@tonic-gate 	 * set name to start after last
13877c478bd9Sstevel@tonic-gate 	 * delimiter.
13887c478bd9Sstevel@tonic-gate 	 */
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 	while (*p)
13917c478bd9Sstevel@tonic-gate 		if (*p++ == DELIM && *p)
13927c478bd9Sstevel@tonic-gate 			name = p;
13937c478bd9Sstevel@tonic-gate 	return (name);
13947c478bd9Sstevel@tonic-gate }
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate static int
13977c478bd9Sstevel@tonic-gate getresp(void)
13987c478bd9Sstevel@tonic-gate {
13997c478bd9Sstevel@tonic-gate 	register int	c, i;
14007c478bd9Sstevel@tonic-gate 	char	ans_buf[SCHAR_MAX + 1];
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 	/*
14037c478bd9Sstevel@tonic-gate 	 * Get response from user. Based on
14047c478bd9Sstevel@tonic-gate 	 * first character, make decision.
14057c478bd9Sstevel@tonic-gate 	 * Discard rest of line.
14067c478bd9Sstevel@tonic-gate 	 */
14077c478bd9Sstevel@tonic-gate 	for (i = 0; ; i++) {
14087c478bd9Sstevel@tonic-gate 		c = getchar();
14097c478bd9Sstevel@tonic-gate 		if (c == '\n' || c == 0 || c == EOF) {
14107c478bd9Sstevel@tonic-gate 			ans_buf[i] = 0;
14117c478bd9Sstevel@tonic-gate 			break;
14127c478bd9Sstevel@tonic-gate 		}
14137c478bd9Sstevel@tonic-gate 		if (i < SCHAR_MAX)
14147c478bd9Sstevel@tonic-gate 			ans_buf[i] = c;
14157c478bd9Sstevel@tonic-gate 	}
14167c478bd9Sstevel@tonic-gate 	if (i >= SCHAR_MAX) {
14177c478bd9Sstevel@tonic-gate 		i = SCHAR_MAX;
14187c478bd9Sstevel@tonic-gate 		ans_buf[SCHAR_MAX] = 0;
14197c478bd9Sstevel@tonic-gate 	}
14207c478bd9Sstevel@tonic-gate 	if ((i == 0) | (strncmp(yeschr, ans_buf, i)))
14217c478bd9Sstevel@tonic-gate 		return (1);
14227c478bd9Sstevel@tonic-gate 	return (0);
14237c478bd9Sstevel@tonic-gate }
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate static void
14267c478bd9Sstevel@tonic-gate usage(void)
14277c478bd9Sstevel@tonic-gate {
14287c478bd9Sstevel@tonic-gate 	/*
14297c478bd9Sstevel@tonic-gate 	 * Display usage message.
14307c478bd9Sstevel@tonic-gate 	 */
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate 	if (mve) {
14337c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
14347c478bd9Sstevel@tonic-gate 		    "Usage: mv [-f] [-i] f1 f2\n"
14357c478bd9Sstevel@tonic-gate 		    "       mv [-f] [-i] f1 ... fn d1\n"
14367c478bd9Sstevel@tonic-gate 		    "       mv [-f] [-i] d1 d2\n"));
14377c478bd9Sstevel@tonic-gate 	} else if (lnk) {
14387c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
14397c478bd9Sstevel@tonic-gate #ifdef XPG4
14407c478bd9Sstevel@tonic-gate 		    "Usage: ln [-f] [-s] f1 [f2]\n"
14417c478bd9Sstevel@tonic-gate 		    "       ln [-f] [-s] f1 ... fn d1\n"
14427c478bd9Sstevel@tonic-gate 		    "       ln [-f] -s d1 d2\n"));
14437c478bd9Sstevel@tonic-gate #else
14447c478bd9Sstevel@tonic-gate 		    "Usage: ln [-f] [-n] [-s] f1 [f2]\n"
14457c478bd9Sstevel@tonic-gate 		    "       ln [-f] [-n] [-s] f1 ... fn d1\n"
14467c478bd9Sstevel@tonic-gate 		    "       ln [-f] [-n] -s d1 d2\n"));
14477c478bd9Sstevel@tonic-gate #endif
14487c478bd9Sstevel@tonic-gate 	} else if (cpy) {
14497c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
14507c478bd9Sstevel@tonic-gate 		    "Usage: cp [-f] [-i] [-p] [-@] f1 f2\n"
14517c478bd9Sstevel@tonic-gate 		    "       cp [-f] [-i] [-p] [-@] f1 ... fn d1\n"
14527c478bd9Sstevel@tonic-gate 		    "       cp -r|-R [-H|-L|-P] [-f] [-i] [-p] [-@] "
14537c478bd9Sstevel@tonic-gate 		    "d1 ... dn-1 dn\n"));
14547c478bd9Sstevel@tonic-gate 	}
14557c478bd9Sstevel@tonic-gate 	exit(2);
14567c478bd9Sstevel@tonic-gate }
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate /*
14597c478bd9Sstevel@tonic-gate  * chg_time()
14607c478bd9Sstevel@tonic-gate  *
14617c478bd9Sstevel@tonic-gate  * Try to preserve modification and access time.
14627c478bd9Sstevel@tonic-gate  * If 1) pflg is not set, or 2) pflg is set and this is the Solaris version,
14637c478bd9Sstevel@tonic-gate  * don't report a utime() failure.
14647c478bd9Sstevel@tonic-gate  * If this is the XPG4 version and utime fails, if 1) pflg is set (cp -p)
14657c478bd9Sstevel@tonic-gate  * or 2) we are doing a mv, print a diagnostic message; arrange for a non-zero
14667c478bd9Sstevel@tonic-gate  * exit status only if pflg is set.
14677c478bd9Sstevel@tonic-gate  * utimes(2) is being used to achieve granularity in
14687c478bd9Sstevel@tonic-gate  * microseconds while setting file times.
14697c478bd9Sstevel@tonic-gate  */
14707c478bd9Sstevel@tonic-gate static int
14717c478bd9Sstevel@tonic-gate chg_time(char *to, struct stat ss)
14727c478bd9Sstevel@tonic-gate {
14737c478bd9Sstevel@tonic-gate 	struct timeval times[2];
14747c478bd9Sstevel@tonic-gate 	int rc;
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 	timestruc_to_timeval(&ss.st_atim, times);
14777c478bd9Sstevel@tonic-gate 	timestruc_to_timeval(&ss.st_mtim, times + 1);
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 	rc = utimes(to, times);
14807c478bd9Sstevel@tonic-gate #ifdef XPG4
14817c478bd9Sstevel@tonic-gate 	if ((pflg || mve) && rc != 0) {
14827c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
14837c478bd9Sstevel@tonic-gate 		    gettext("%s: cannot set times for %s: "), cmd, to);
14847c478bd9Sstevel@tonic-gate 		perror("");
14857c478bd9Sstevel@tonic-gate 		if (pflg)
14867c478bd9Sstevel@tonic-gate 			return (1);
14877c478bd9Sstevel@tonic-gate 	}
14887c478bd9Sstevel@tonic-gate #endif
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate 	return (0);
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate }
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate /*
14957c478bd9Sstevel@tonic-gate  * chg_mode()
14967c478bd9Sstevel@tonic-gate  *
14977c478bd9Sstevel@tonic-gate  * This function is called upon "cp -p" or mv across filesystems.
14987c478bd9Sstevel@tonic-gate  *
14997c478bd9Sstevel@tonic-gate  * Try to preserve the owner and group id.  If chown() fails,
15007c478bd9Sstevel@tonic-gate  * only print a diagnostic message if doing a mv in the XPG4 version;
15017c478bd9Sstevel@tonic-gate  * try to clear S_ISUID and S_ISGID bits in the target.  If unable to clear
15027c478bd9Sstevel@tonic-gate  * S_ISUID and S_ISGID bits, print a diagnostic message and arrange for a
15037c478bd9Sstevel@tonic-gate  * non-zero exit status because this is a security violation.
15047c478bd9Sstevel@tonic-gate  * Try to preserve permissions.
15057c478bd9Sstevel@tonic-gate  * If this is the XPG4 version and chmod() fails, print a diagnostic message
15067c478bd9Sstevel@tonic-gate  * and arrange for a non-zero exit status.
15077c478bd9Sstevel@tonic-gate  * If this is the Solaris version and chmod() fails, do not print a
15087c478bd9Sstevel@tonic-gate  * diagnostic message or exit with a non-zero value.
15097c478bd9Sstevel@tonic-gate  */
15107c478bd9Sstevel@tonic-gate static int
15117c478bd9Sstevel@tonic-gate chg_mode(char *target, uid_t uid, gid_t gid, mode_t mode)
15127c478bd9Sstevel@tonic-gate {
15137c478bd9Sstevel@tonic-gate 	int clearflg = 0; /* controls message printed upon chown() error */
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 	if (chown(target, uid, gid) != 0) {
15167c478bd9Sstevel@tonic-gate #ifdef XPG4
15177c478bd9Sstevel@tonic-gate 		if (mve) {
15187c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: cannot change"
15197c478bd9Sstevel@tonic-gate 			    " owner and group of %s: "), cmd, target);
15207c478bd9Sstevel@tonic-gate 			perror("");
15217c478bd9Sstevel@tonic-gate 		}
15227c478bd9Sstevel@tonic-gate #endif
15237c478bd9Sstevel@tonic-gate 		if (mode & (S_ISUID | S_ISGID)) {
15247c478bd9Sstevel@tonic-gate 			/* try to clear S_ISUID and S_ISGID */
15257c478bd9Sstevel@tonic-gate 			mode &= ~S_ISUID & ~S_ISGID;
15267c478bd9Sstevel@tonic-gate 			++clearflg;
15277c478bd9Sstevel@tonic-gate 		}
15287c478bd9Sstevel@tonic-gate 	}
15297c478bd9Sstevel@tonic-gate 	if (chmod(target, mode) != 0) {
15307c478bd9Sstevel@tonic-gate 		if (clearflg) {
15317c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
15327c478bd9Sstevel@tonic-gate 			    "%s: cannot clear S_ISUID and S_ISGID bits in"
15337c478bd9Sstevel@tonic-gate 			    " %s: "), cmd, target);
15347c478bd9Sstevel@tonic-gate 			perror("");
15357c478bd9Sstevel@tonic-gate 			/* cp -p should get non-zero exit; mv should not */
15367c478bd9Sstevel@tonic-gate 			if (pflg)
15377c478bd9Sstevel@tonic-gate 				return (1);
15387c478bd9Sstevel@tonic-gate 		}
15397c478bd9Sstevel@tonic-gate #ifdef XPG4
15407c478bd9Sstevel@tonic-gate 		else {
15417c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
15427c478bd9Sstevel@tonic-gate 			"%s: cannot set permissions for %s: "), cmd, target);
15437c478bd9Sstevel@tonic-gate 			perror("");
15447c478bd9Sstevel@tonic-gate 			/* cp -p should get non-zero exit; mv should not */
15457c478bd9Sstevel@tonic-gate 			if (pflg)
15467c478bd9Sstevel@tonic-gate 				return (1);
15477c478bd9Sstevel@tonic-gate 		}
15487c478bd9Sstevel@tonic-gate #endif
15497c478bd9Sstevel@tonic-gate 	}
15507c478bd9Sstevel@tonic-gate 	return (0);
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate }
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate static void
15557c478bd9Sstevel@tonic-gate Perror(char *s)
15567c478bd9Sstevel@tonic-gate {
15577c478bd9Sstevel@tonic-gate 	char buf[PATH_MAX + 10];
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "%s: %s", cmd, s);
15607c478bd9Sstevel@tonic-gate 	perror(buf);
15617c478bd9Sstevel@tonic-gate }
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate static void
15647c478bd9Sstevel@tonic-gate Perror2(char *s1, char *s2)
15657c478bd9Sstevel@tonic-gate {
15667c478bd9Sstevel@tonic-gate 	char buf[PATH_MAX + 20];
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "%s: %s: %s",
15697c478bd9Sstevel@tonic-gate 	    cmd, gettext(s1), gettext(s2));
15707c478bd9Sstevel@tonic-gate 	perror(buf);
15717c478bd9Sstevel@tonic-gate }
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate /*
15747c478bd9Sstevel@tonic-gate  * used for cp -R and for mv across file systems
15757c478bd9Sstevel@tonic-gate  */
15767c478bd9Sstevel@tonic-gate static int
15777c478bd9Sstevel@tonic-gate copydir(char *source, char *target)
15787c478bd9Sstevel@tonic-gate {
15797c478bd9Sstevel@tonic-gate 	int ret, attret = 0;
15807c478bd9Sstevel@tonic-gate 	int pret = 0;		/* need separate flag if -p is specified */
15817c478bd9Sstevel@tonic-gate 	mode_t	fixmode = (mode_t)0;	/* cleanup mode after copy */
15827c478bd9Sstevel@tonic-gate 	struct stat s1save;
1583fa9e4066Sahrens 	acl_t  *s1acl_save;
1584fa9e4066Sahrens 
1585fa9e4066Sahrens 	s1acl_save = NULL;
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 	if (cpy && !rflg) {
15887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
15897c478bd9Sstevel@tonic-gate 		    gettext("%s: %s: is a directory\n"), cmd, source);
15907c478bd9Sstevel@tonic-gate 		return (1);
15917c478bd9Sstevel@tonic-gate 	}
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate 	if (stat(target, &s2) < 0) {
15947c478bd9Sstevel@tonic-gate 		if (mkdir(target, (s1.st_mode & MODEBITS)) < 0) {
15957c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: ", cmd);
15967c478bd9Sstevel@tonic-gate 			perror(target);
15977c478bd9Sstevel@tonic-gate 			return (1);
15987c478bd9Sstevel@tonic-gate 		}
15997c478bd9Sstevel@tonic-gate 		if (stat(target, &s2) == 0) {
16007c478bd9Sstevel@tonic-gate 			fixmode = s2.st_mode;
16017c478bd9Sstevel@tonic-gate 		} else {
16027c478bd9Sstevel@tonic-gate 			fixmode = s1.st_mode;
16037c478bd9Sstevel@tonic-gate 		}
16047c478bd9Sstevel@tonic-gate 		(void) chmod(target, ((fixmode & MODEBITS) | S_IRWXU));
16057c478bd9Sstevel@tonic-gate 	} else if (!(ISDIR(s2))) {
16067c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
16077c478bd9Sstevel@tonic-gate 		    gettext("%s: %s: not a directory.\n"), cmd, target);
16087c478bd9Sstevel@tonic-gate 		return (1);
16097c478bd9Sstevel@tonic-gate 	}
16107c478bd9Sstevel@tonic-gate 	if (pflg || mve) {
16117c478bd9Sstevel@tonic-gate 		/*
16127c478bd9Sstevel@tonic-gate 		 * Save s1 (stat information for source dir) and acl info,
16137c478bd9Sstevel@tonic-gate 		 * if any, so that ownership, modes, times, and acl's can
16147c478bd9Sstevel@tonic-gate 		 * be reserved during "cp -p" or mv.
16157c478bd9Sstevel@tonic-gate 		 * s1 gets overwritten when doing the recursive copy.
16167c478bd9Sstevel@tonic-gate 		 */
16177c478bd9Sstevel@tonic-gate 		s1save = s1;
1618fa9e4066Sahrens 		if (s1acl != NULL) {
1619fa9e4066Sahrens 			s1acl_save = acl_dup(s1acl);
1620fa9e4066Sahrens 			if (s1acl_save == NULL) {
1621fa9e4066Sahrens 				(void) fprintf(stderr, gettext("%s: "
1622fa9e4066Sahrens 				    "Insufficient memory to save acl"
1623fa9e4066Sahrens 				    " entry\n"), cmd);
1624fa9e4066Sahrens 				if (pflg)
1625fa9e4066Sahrens 					return (1);
1626fa9e4066Sahrens 
16277c478bd9Sstevel@tonic-gate 			}
16287c478bd9Sstevel@tonic-gate #ifdef XPG4
16297c478bd9Sstevel@tonic-gate 			else {
16307c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: "
16317c478bd9Sstevel@tonic-gate 				    "Insufficient memory to save acl"
16327c478bd9Sstevel@tonic-gate 				    " entry\n"), cmd);
16337c478bd9Sstevel@tonic-gate 				if (pflg)
16347c478bd9Sstevel@tonic-gate 					return (1);
16357c478bd9Sstevel@tonic-gate 			}
16367c478bd9Sstevel@tonic-gate #endif
16377c478bd9Sstevel@tonic-gate 		}
16387c478bd9Sstevel@tonic-gate 	}
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 	ret = rcopy(source, target);
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate 	/*
16437c478bd9Sstevel@tonic-gate 	 * Once we created a directory, go ahead and set
16447c478bd9Sstevel@tonic-gate 	 * its attributes, e.g. acls and time. The info
16457c478bd9Sstevel@tonic-gate 	 * may get overwritten if we continue traversing
16467c478bd9Sstevel@tonic-gate 	 * down the tree.
16477c478bd9Sstevel@tonic-gate 	 *
16487c478bd9Sstevel@tonic-gate 	 * ACL for directory
16497c478bd9Sstevel@tonic-gate 	 */
16507c478bd9Sstevel@tonic-gate 	if (pflg || mve) {
1651d2443e76Smarks 		if ((pret = chg_mode(target, UID(s1save), GID(s1save),
1652d2443e76Smarks 		    FMODE(s1save))) == 0)
1653d2443e76Smarks 			pret = chg_time(target, s1save);
1654d2443e76Smarks 		ret += pret;
1655fa9e4066Sahrens 		if (s1acl_save != NULL) {
1656fa9e4066Sahrens 			if (acl_set(target, s1acl_save) < 0) {
16577c478bd9Sstevel@tonic-gate #ifdef XPG4
16587c478bd9Sstevel@tonic-gate 				if (pflg || mve) {
16597c478bd9Sstevel@tonic-gate #else
16607c478bd9Sstevel@tonic-gate 				if (pflg) {
16617c478bd9Sstevel@tonic-gate #endif
16627c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
16637c478bd9Sstevel@tonic-gate 					    "%s: failed to set acl entries "
16647c478bd9Sstevel@tonic-gate 					    "on %s\n"), cmd, target);
16657c478bd9Sstevel@tonic-gate 					if (pflg) {
1666fa9e4066Sahrens 						acl_free(s1acl_save);
1667fa9e4066Sahrens 						s1acl_save = NULL;
16687c478bd9Sstevel@tonic-gate 						ret++;
16697c478bd9Sstevel@tonic-gate 					}
16707c478bd9Sstevel@tonic-gate 				}
16717c478bd9Sstevel@tonic-gate 				/* else: silent and continue */
16727c478bd9Sstevel@tonic-gate 			}
1673fa9e4066Sahrens 			acl_free(s1acl_save);
1674fa9e4066Sahrens 			s1acl_save = NULL;
16757c478bd9Sstevel@tonic-gate 		}
16767c478bd9Sstevel@tonic-gate 	} else if (fixmode != (mode_t)0)
16777c478bd9Sstevel@tonic-gate 		(void) chmod(target, fixmode & MODEBITS);
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 	if (pflg || atflg || mve) {
16807c478bd9Sstevel@tonic-gate 		attret = copyattributes(source, target);
16817c478bd9Sstevel@tonic-gate 		if (!attrsilent && attret != 0) {
16827c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: Failed to preserve"
16837c478bd9Sstevel@tonic-gate 			    " extended attributes of directory"
16847c478bd9Sstevel@tonic-gate 			    " %s\n"), cmd, source);
16857c478bd9Sstevel@tonic-gate 		} else {
16867c478bd9Sstevel@tonic-gate 			/*
16877c478bd9Sstevel@tonic-gate 			 * Otherwise ignore failure.
16887c478bd9Sstevel@tonic-gate 			 */
16897c478bd9Sstevel@tonic-gate 			attret = 0;
16907c478bd9Sstevel@tonic-gate 		}
16917c478bd9Sstevel@tonic-gate 	}
16927c478bd9Sstevel@tonic-gate 	if (attret != 0)
16937c478bd9Sstevel@tonic-gate 		return (attret);
16947c478bd9Sstevel@tonic-gate 	return (ret);
16957c478bd9Sstevel@tonic-gate }
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate static int
16987c478bd9Sstevel@tonic-gate copyspecial(char *target)
16997c478bd9Sstevel@tonic-gate {
17007c478bd9Sstevel@tonic-gate 	int ret = 0;
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate 	if (mknod(target, s1.st_mode, s1.st_rdev) != 0) {
17037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
17047c478bd9Sstevel@tonic-gate 		    "cp: cannot create special file %s: "), target);
17057c478bd9Sstevel@tonic-gate 		perror("");
17067c478bd9Sstevel@tonic-gate 		return (1);
17077c478bd9Sstevel@tonic-gate 	}
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 	if (pflg) {
17107c478bd9Sstevel@tonic-gate 		if ((ret = chg_mode(target, UID(s1), GID(s1), FMODE(s1))) == 0)
17117c478bd9Sstevel@tonic-gate 			ret = chg_time(target, s1);
17127c478bd9Sstevel@tonic-gate 	}
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	return (ret);
17157c478bd9Sstevel@tonic-gate }
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate static int
17187c478bd9Sstevel@tonic-gate use_stdin(void)
17197c478bd9Sstevel@tonic-gate {
17207c478bd9Sstevel@tonic-gate #ifdef XPG4
17217c478bd9Sstevel@tonic-gate 	return (1);
17227c478bd9Sstevel@tonic-gate #else
17237c478bd9Sstevel@tonic-gate 	return (isatty(fileno(stdin)));
17247c478bd9Sstevel@tonic-gate #endif
17257c478bd9Sstevel@tonic-gate }
17267c478bd9Sstevel@tonic-gate 
17277c478bd9Sstevel@tonic-gate static int
17287c478bd9Sstevel@tonic-gate copyattributes(char *source, char *target)
17297c478bd9Sstevel@tonic-gate {
17307c478bd9Sstevel@tonic-gate 	int sourcedirfd, targetdirfd;
17317c478bd9Sstevel@tonic-gate 	int srcfd, targfd;
17327c478bd9Sstevel@tonic-gate 	int tmpfd;
17337c478bd9Sstevel@tonic-gate 	DIR *srcdirp;
17347c478bd9Sstevel@tonic-gate 	int srcattrfd, targattrfd;
17357c478bd9Sstevel@tonic-gate 	struct dirent *dp;
17367c478bd9Sstevel@tonic-gate 	char *attrstr;
17377c478bd9Sstevel@tonic-gate 	char *srcbuf, *targbuf;
17387c478bd9Sstevel@tonic-gate 	size_t src_size, targ_size;
17397c478bd9Sstevel@tonic-gate 	int error = 0;
1740fa9e4066Sahrens 	int aclerror;
17417c478bd9Sstevel@tonic-gate 	mode_t mode;
17427c478bd9Sstevel@tonic-gate 	int clearflg = 0;
1743fa9e4066Sahrens 	acl_t *xacl = NULL;
1744fa9e4066Sahrens 	acl_t *attrdiracl = NULL;
17457c478bd9Sstevel@tonic-gate 	struct stat attrdir, s3, s4;
17467c478bd9Sstevel@tonic-gate 	struct timeval times[2];
17477c478bd9Sstevel@tonic-gate 	mode_t	targmode;
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate 	srcdirp = NULL;
17507c478bd9Sstevel@tonic-gate 	srcfd = targfd = tmpfd = -1;
17517c478bd9Sstevel@tonic-gate 	sourcedirfd = targetdirfd = srcattrfd = targattrfd = -1;
17527c478bd9Sstevel@tonic-gate 	srcbuf = targbuf = NULL;
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate 	if (pathconf(source, _PC_XATTR_EXISTS) != 1)
17557c478bd9Sstevel@tonic-gate 		return (0);
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 	if (pathconf(target, _PC_XATTR_ENABLED) != 1) {
17587c478bd9Sstevel@tonic-gate 		if (!attrsilent) {
17597c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
17607c478bd9Sstevel@tonic-gate 			    gettext(
17617c478bd9Sstevel@tonic-gate 			    "%s: cannot preserve extended attributes, "
17627c478bd9Sstevel@tonic-gate 			    "operation not supported on file"
17637c478bd9Sstevel@tonic-gate 			    " %s\n"), cmd, target);
17647c478bd9Sstevel@tonic-gate 		}
17657c478bd9Sstevel@tonic-gate 		return (1);
17667c478bd9Sstevel@tonic-gate 	}
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 	if ((srcfd = open(source, O_RDONLY)) == -1) {
17707c478bd9Sstevel@tonic-gate 		if (pflg && attrsilent) {
17717c478bd9Sstevel@tonic-gate 			error = 0;
17727c478bd9Sstevel@tonic-gate 			goto out;
17737c478bd9Sstevel@tonic-gate 		}
17747c478bd9Sstevel@tonic-gate 		if (!attrsilent) {
17757c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
17767c478bd9Sstevel@tonic-gate 			    gettext("%s: cannot open file"
17777c478bd9Sstevel@tonic-gate 			    " %s: "), cmd, source);
17787c478bd9Sstevel@tonic-gate 			perror("");
17797c478bd9Sstevel@tonic-gate 		}
17807c478bd9Sstevel@tonic-gate 		++error;
17817c478bd9Sstevel@tonic-gate 		goto out;
17827c478bd9Sstevel@tonic-gate 	}
17837c478bd9Sstevel@tonic-gate 	if ((targfd = open(target, O_RDONLY)) == -1) {
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 		if (pflg && attrsilent) {
17867c478bd9Sstevel@tonic-gate 			error = 0;
17877c478bd9Sstevel@tonic-gate 			goto out;
17887c478bd9Sstevel@tonic-gate 		}
17897c478bd9Sstevel@tonic-gate 		if (!attrsilent) {
17907c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
17917c478bd9Sstevel@tonic-gate 			    gettext("%s: cannot open file"
17927c478bd9Sstevel@tonic-gate 			    " %s: "), cmd, source);
17937c478bd9Sstevel@tonic-gate 			perror("");
17947c478bd9Sstevel@tonic-gate 		}
17957c478bd9Sstevel@tonic-gate 		++error;
17967c478bd9Sstevel@tonic-gate 		goto out;
17977c478bd9Sstevel@tonic-gate 	}
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate 	if ((sourcedirfd = openat(srcfd, ".", O_RDONLY|O_XATTR)) == -1) {
18007c478bd9Sstevel@tonic-gate 		if (pflg && attrsilent) {
18017c478bd9Sstevel@tonic-gate 			error = 0;
18027c478bd9Sstevel@tonic-gate 			goto out;
18037c478bd9Sstevel@tonic-gate 		}
18047c478bd9Sstevel@tonic-gate 		if (!attrsilent) {
18057c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
18067c478bd9Sstevel@tonic-gate 			    gettext("%s: cannot open attribute"
18077c478bd9Sstevel@tonic-gate 			    " directory for %s: "), cmd, source);
18087c478bd9Sstevel@tonic-gate 			perror("");
18097c478bd9Sstevel@tonic-gate 			++error;
18107c478bd9Sstevel@tonic-gate 		}
18117c478bd9Sstevel@tonic-gate 		goto out;
18127c478bd9Sstevel@tonic-gate 	}
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate 	if (fstat(sourcedirfd, &attrdir) == -1) {
18157c478bd9Sstevel@tonic-gate 		if (pflg && attrsilent) {
18167c478bd9Sstevel@tonic-gate 			error = 0;
18177c478bd9Sstevel@tonic-gate 			goto out;
18187c478bd9Sstevel@tonic-gate 		}
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 		if (!attrsilent) {
18217c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
18227c478bd9Sstevel@tonic-gate 				gettext("%s: could not retrieve stat"
18237c478bd9Sstevel@tonic-gate 					" information for attribute directory"
18247c478bd9Sstevel@tonic-gate 					"of file %s: "), cmd, source);
18257c478bd9Sstevel@tonic-gate 			perror("");
18267c478bd9Sstevel@tonic-gate 			++error;
18277c478bd9Sstevel@tonic-gate 		}
18287c478bd9Sstevel@tonic-gate 		goto out;
18297c478bd9Sstevel@tonic-gate 	}
18307c478bd9Sstevel@tonic-gate 	if ((targetdirfd = openat(targfd, ".", O_RDONLY|O_XATTR)) == -1) {
18317c478bd9Sstevel@tonic-gate 		/*
18327c478bd9Sstevel@tonic-gate 		 * We couldn't create the attribute directory
18337c478bd9Sstevel@tonic-gate 		 *
18347c478bd9Sstevel@tonic-gate 		 * Lets see if we can add write support to the mode
18357c478bd9Sstevel@tonic-gate 		 * and create the directory and then put the mode back
18367c478bd9Sstevel@tonic-gate 		 * to way it should be.
18377c478bd9Sstevel@tonic-gate 		 */
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 		targmode = FMODE(s1) | S_IWUSR;
18407c478bd9Sstevel@tonic-gate 		if (fchmod(targfd, targmode) == 0) {
18417c478bd9Sstevel@tonic-gate 			targetdirfd = openat(targfd, ".", O_RDONLY|O_XATTR);
18427c478bd9Sstevel@tonic-gate 			/*
18437c478bd9Sstevel@tonic-gate 			 * Put mode back to what it was
18447c478bd9Sstevel@tonic-gate 			 */
18457c478bd9Sstevel@tonic-gate 			targmode = FMODE(s1) & MODEBITS;
18467c478bd9Sstevel@tonic-gate 			if (fchmod(targfd, targmode) == -1) {
18477c478bd9Sstevel@tonic-gate 				if (pflg && attrsilent) {
18487c478bd9Sstevel@tonic-gate 					error = 0;
18497c478bd9Sstevel@tonic-gate 					goto out;
18507c478bd9Sstevel@tonic-gate 				}
18517c478bd9Sstevel@tonic-gate 				if (!attrsilent) {
18527c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
18537c478bd9Sstevel@tonic-gate 					    gettext("%s: failed to set"
18547c478bd9Sstevel@tonic-gate 					    " mode correctly on file"
18557c478bd9Sstevel@tonic-gate 					    " %s: "), cmd, target);
18567c478bd9Sstevel@tonic-gate 					perror("");
18577c478bd9Sstevel@tonic-gate 					++error;
18587c478bd9Sstevel@tonic-gate 					goto out;
18597c478bd9Sstevel@tonic-gate 				}
18607c478bd9Sstevel@tonic-gate 			}
18617c478bd9Sstevel@tonic-gate 		} else {
18627c478bd9Sstevel@tonic-gate 			if (pflg && attrsilent) {
18637c478bd9Sstevel@tonic-gate 				error = 0;
18647c478bd9Sstevel@tonic-gate 				goto out;
18657c478bd9Sstevel@tonic-gate 			}
18667c478bd9Sstevel@tonic-gate 			if (!attrsilent) {
18677c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
18687c478bd9Sstevel@tonic-gate 				    gettext("%s: cannot open attribute"
18697c478bd9Sstevel@tonic-gate 				    " directory for %s: "), cmd, target);
18707c478bd9Sstevel@tonic-gate 				perror("");
18717c478bd9Sstevel@tonic-gate 				++error;
18727c478bd9Sstevel@tonic-gate 			}
18737c478bd9Sstevel@tonic-gate 			goto out;
18747c478bd9Sstevel@tonic-gate 		}
18757c478bd9Sstevel@tonic-gate 	}
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate 	if (targetdirfd == -1) {
18787c478bd9Sstevel@tonic-gate 		if (pflg && attrsilent) {
18797c478bd9Sstevel@tonic-gate 			error = 0;
18807c478bd9Sstevel@tonic-gate 			goto out;
18817c478bd9Sstevel@tonic-gate 		}
18827c478bd9Sstevel@tonic-gate 		if (!attrsilent) {
18837c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
18847c478bd9Sstevel@tonic-gate 			    gettext("%s: cannot open attribute directory"
18857c478bd9Sstevel@tonic-gate 			    " for %s: "), cmd, target);
18867c478bd9Sstevel@tonic-gate 			perror("");
18877c478bd9Sstevel@tonic-gate 			++error;
18887c478bd9Sstevel@tonic-gate 		}
18897c478bd9Sstevel@tonic-gate 		goto out;
18907c478bd9Sstevel@tonic-gate 	}
18917c478bd9Sstevel@tonic-gate 
18927c478bd9Sstevel@tonic-gate 	/*
18937c478bd9Sstevel@tonic-gate 	 * Set mode of attribute directory same as on source,
18947c478bd9Sstevel@tonic-gate 	 * if pflg set or this is a move.
18957c478bd9Sstevel@tonic-gate 	 */
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 	if (pflg || mve) {
18987c478bd9Sstevel@tonic-gate 		if (fchmod(targetdirfd, attrdir.st_mode) == -1) {
18997c478bd9Sstevel@tonic-gate 			if (!attrsilent) {
19007c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
19017c478bd9Sstevel@tonic-gate 					gettext("%s: failed to set file mode"
19027c478bd9Sstevel@tonic-gate 					" correctly on attribute directory of"
19037c478bd9Sstevel@tonic-gate 					" file %s: "), cmd, target);
19047c478bd9Sstevel@tonic-gate 				perror("");
19057c478bd9Sstevel@tonic-gate 				++error;
19067c478bd9Sstevel@tonic-gate 			}
19077c478bd9Sstevel@tonic-gate 		}
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate 		if (fchown(targetdirfd, attrdir.st_uid, attrdir.st_gid) == -1) {
19107c478bd9Sstevel@tonic-gate 			if (!attrsilent) {
19117c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
19127c478bd9Sstevel@tonic-gate 				    gettext("%s: failed to set file"
19137c478bd9Sstevel@tonic-gate 				    " ownership correctly on attribute"
19147c478bd9Sstevel@tonic-gate 				    " directory of file %s: "), cmd, target);
19157c478bd9Sstevel@tonic-gate 				perror("");
19167c478bd9Sstevel@tonic-gate 				++error;
19177c478bd9Sstevel@tonic-gate 			}
19187c478bd9Sstevel@tonic-gate 		}
19197c478bd9Sstevel@tonic-gate 		/*
19207c478bd9Sstevel@tonic-gate 		 * Now that we are the owner we can update st_ctime by calling
19217c478bd9Sstevel@tonic-gate 		 * futimesat.
19227c478bd9Sstevel@tonic-gate 		 */
19237c478bd9Sstevel@tonic-gate 		times[0].tv_sec = attrdir.st_atime;
19247c478bd9Sstevel@tonic-gate 		times[0].tv_usec = 0;
19257c478bd9Sstevel@tonic-gate 		times[1].tv_sec = attrdir.st_mtime;
19267c478bd9Sstevel@tonic-gate 		times[1].tv_usec = 0;
19277c478bd9Sstevel@tonic-gate 		if (futimesat(targetdirfd, ".", times) < 0) {
19287c478bd9Sstevel@tonic-gate 			if (!attrsilent) {
19297c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
19307c478bd9Sstevel@tonic-gate 					gettext("%s: cannot set attribute times"
19317c478bd9Sstevel@tonic-gate 					" for %s: "), cmd, target);
19327c478bd9Sstevel@tonic-gate 				perror("");
19337c478bd9Sstevel@tonic-gate 				++error;
19347c478bd9Sstevel@tonic-gate 			}
19357c478bd9Sstevel@tonic-gate 		}
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate 		/*
19387c478bd9Sstevel@tonic-gate 		 * Now set owner and group of attribute directory, implies
19397c478bd9Sstevel@tonic-gate 		 * changing the ACL of the hidden attribute directory first.
19407c478bd9Sstevel@tonic-gate 		 */
1941fa9e4066Sahrens 		if ((aclerror = facl_get(sourcedirfd,
1942fa9e4066Sahrens 		    ACL_NO_TRIVIAL, &attrdiracl)) != 0) {
19437c478bd9Sstevel@tonic-gate 			if (!attrsilent) {
19447c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
19457c478bd9Sstevel@tonic-gate 				    "%s: failed to get acl entries of"
19467c478bd9Sstevel@tonic-gate 				    " attribute directory for"
1947fa9e4066Sahrens 				    " %s : %s\n"), cmd,
1948fa9e4066Sahrens 				    source, acl_strerror(aclerror));
19497c478bd9Sstevel@tonic-gate 				++error;
19507c478bd9Sstevel@tonic-gate 			}
19517c478bd9Sstevel@tonic-gate 		}
19527c478bd9Sstevel@tonic-gate 
1953fa9e4066Sahrens 		if (attrdiracl) {
1954fa9e4066Sahrens 			if (facl_set(targetdirfd, attrdiracl) != 0) {
19557c478bd9Sstevel@tonic-gate 				if (!attrsilent) {
19567c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
19577c478bd9Sstevel@tonic-gate 					"%s: failed to set acl entries"
19587c478bd9Sstevel@tonic-gate 					" on attribute directory "
19597c478bd9Sstevel@tonic-gate 					"for %s\n"), cmd, target);
19607c478bd9Sstevel@tonic-gate 					++error;
19617c478bd9Sstevel@tonic-gate 				}
1962fa9e4066Sahrens 				acl_free(attrdiracl);
1963fa9e4066Sahrens 				attrdiracl = NULL;
19647c478bd9Sstevel@tonic-gate 			}
19657c478bd9Sstevel@tonic-gate 		}
19667c478bd9Sstevel@tonic-gate 	}
19677c478bd9Sstevel@tonic-gate 
19687c478bd9Sstevel@tonic-gate 	/*
19697c478bd9Sstevel@tonic-gate 	 * dup sourcedirfd for use by fdopendir().
19707c478bd9Sstevel@tonic-gate 	 * fdopendir will take ownership of given fd and will close
19717c478bd9Sstevel@tonic-gate 	 * it when closedir() is called.
19727c478bd9Sstevel@tonic-gate 	 */
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate 	if ((tmpfd = dup(sourcedirfd)) == -1) {
19757c478bd9Sstevel@tonic-gate 		if (pflg && attrsilent) {
19767c478bd9Sstevel@tonic-gate 			error = 0;
19777c478bd9Sstevel@tonic-gate 			goto out;
19787c478bd9Sstevel@tonic-gate 		}
19797c478bd9Sstevel@tonic-gate 		if (!attrsilent) {
19807c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
19817c478bd9Sstevel@tonic-gate 			    gettext(
19827c478bd9Sstevel@tonic-gate 			    "%s: unable to dup attribute directory"
19837c478bd9Sstevel@tonic-gate 			    " file descriptor for %s: "), cmd, source);
19847c478bd9Sstevel@tonic-gate 			perror("");
19857c478bd9Sstevel@tonic-gate 			++error;
19867c478bd9Sstevel@tonic-gate 		}
19877c478bd9Sstevel@tonic-gate 		goto out;
19887c478bd9Sstevel@tonic-gate 	}
19897c478bd9Sstevel@tonic-gate 	if ((srcdirp = fdopendir(tmpfd)) == NULL) {
19907c478bd9Sstevel@tonic-gate 		if (pflg && attrsilent) {
19917c478bd9Sstevel@tonic-gate 			error = 0;
19927c478bd9Sstevel@tonic-gate 			goto out;
19937c478bd9Sstevel@tonic-gate 		}
19947c478bd9Sstevel@tonic-gate 		if (!attrsilent) {
19957c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
19967c478bd9Sstevel@tonic-gate 			    gettext("%s: failed to open attribute"
19977c478bd9Sstevel@tonic-gate 			    " directory for %s: "), cmd, source);
19987c478bd9Sstevel@tonic-gate 			perror("");
19997c478bd9Sstevel@tonic-gate 			++error;
20007c478bd9Sstevel@tonic-gate 		}
20017c478bd9Sstevel@tonic-gate 		goto out;
20027c478bd9Sstevel@tonic-gate 	}
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate 	while (dp = readdir(srcdirp)) {
20057c478bd9Sstevel@tonic-gate 		if ((dp->d_name[0] == '.' && dp->d_name[1] == '\0') ||
20067c478bd9Sstevel@tonic-gate 			(dp->d_name[0] == '.' && dp->d_name[1] == '.' &&
20077c478bd9Sstevel@tonic-gate 			dp->d_name[2] == '\0'))
20087c478bd9Sstevel@tonic-gate 			continue;
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate 		if ((srcattrfd = openat(sourcedirfd, dp->d_name,
20117c478bd9Sstevel@tonic-gate 		    O_RDONLY)) == -1) {
20127c478bd9Sstevel@tonic-gate 			if (!attrsilent) {
20137c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
20147c478bd9Sstevel@tonic-gate 				    gettext("%s: cannot open attribute %s on"
20157c478bd9Sstevel@tonic-gate 				    " file %s: "), cmd, dp->d_name, source);
20167c478bd9Sstevel@tonic-gate 				perror("");
20177c478bd9Sstevel@tonic-gate 				++error;
20187c478bd9Sstevel@tonic-gate 				goto next;
20197c478bd9Sstevel@tonic-gate 			}
20207c478bd9Sstevel@tonic-gate 		}
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 		if (fstat(srcattrfd, &s3) < 0) {
20237c478bd9Sstevel@tonic-gate 			if (!attrsilent) {
20247c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
20257c478bd9Sstevel@tonic-gate 				    gettext("%s: could not stat attribute"
20267c478bd9Sstevel@tonic-gate 				    " %s on file"
20277c478bd9Sstevel@tonic-gate 				    " %s: "), cmd, dp->d_name, source);
20287c478bd9Sstevel@tonic-gate 				perror("");
20297c478bd9Sstevel@tonic-gate 				++error;
20307c478bd9Sstevel@tonic-gate 			}
20317c478bd9Sstevel@tonic-gate 			goto next;
20327c478bd9Sstevel@tonic-gate 		}
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate 		if (pflg || mve) {
2035fa9e4066Sahrens 			if ((aclerror = facl_get(srcattrfd,
2036fa9e4066Sahrens 			    ACL_NO_TRIVIAL, &xacl)) != 0) {
20377c478bd9Sstevel@tonic-gate 				if (!attrsilent) {
20387c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
20397c478bd9Sstevel@tonic-gate 					    "%s: failed to get acl entries of"
20407c478bd9Sstevel@tonic-gate 					    " attribute %s for"
2041fa9e4066Sahrens 					    " %s: %s"), cmd, dp->d_name,
2042fa9e4066Sahrens 					    source, acl_strerror(aclerror));
20437c478bd9Sstevel@tonic-gate 					++error;
20447c478bd9Sstevel@tonic-gate 				}
20457c478bd9Sstevel@tonic-gate 			}
20467c478bd9Sstevel@tonic-gate 		}
20477c478bd9Sstevel@tonic-gate 
20487c478bd9Sstevel@tonic-gate 		(void) unlinkat(targetdirfd, dp->d_name, 0);
20497c478bd9Sstevel@tonic-gate 		if ((targattrfd = openat(targetdirfd, dp->d_name,
20507c478bd9Sstevel@tonic-gate 		    O_RDWR|O_CREAT|O_TRUNC, s3.st_mode & MODEBITS)) == -1) {
20517c478bd9Sstevel@tonic-gate 			if (!attrsilent) {
20527c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
20537c478bd9Sstevel@tonic-gate 				    gettext("%s: could not create attribute"
20547c478bd9Sstevel@tonic-gate 				    " %s on file"
20557c478bd9Sstevel@tonic-gate 				    " %s: "), cmd, dp->d_name, target);
20567c478bd9Sstevel@tonic-gate 				perror("");
20577c478bd9Sstevel@tonic-gate 				++error;
20587c478bd9Sstevel@tonic-gate 			}
20597c478bd9Sstevel@tonic-gate 			goto next;
20607c478bd9Sstevel@tonic-gate 		}
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 		/*
20637c478bd9Sstevel@tonic-gate 		 * preserve ACL
20647c478bd9Sstevel@tonic-gate 		 */
2065fa9e4066Sahrens 		if ((pflg || mve) && xacl != NULL) {
2066fa9e4066Sahrens 			if ((facl_set(targattrfd, xacl)) < 0) {
20677c478bd9Sstevel@tonic-gate 				if (!attrsilent) {
20687c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
20697c478bd9Sstevel@tonic-gate 					    "%s: failed to set acl entries on"
20707c478bd9Sstevel@tonic-gate 					    " attribute %s for"
20717c478bd9Sstevel@tonic-gate 					    "%s\n"), cmd, dp->d_name, target);
20727c478bd9Sstevel@tonic-gate 					++error;
20737c478bd9Sstevel@tonic-gate 				}
2074fa9e4066Sahrens 				acl_free(xacl);
2075fa9e4066Sahrens 				xacl = NULL;
20767c478bd9Sstevel@tonic-gate 			}
20777c478bd9Sstevel@tonic-gate 		}
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate 		if (fstat(targattrfd, &s4) < 0) {
20807c478bd9Sstevel@tonic-gate 			if (!attrsilent) {
20817c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
20827c478bd9Sstevel@tonic-gate 				    gettext("%s: could not stat attribute"
20837c478bd9Sstevel@tonic-gate 				    " %s on file"
20847c478bd9Sstevel@tonic-gate 				    " %s: "), cmd, dp->d_name, source);
20857c478bd9Sstevel@tonic-gate 				perror("");
20867c478bd9Sstevel@tonic-gate 				++error;
20877c478bd9Sstevel@tonic-gate 			}
20887c478bd9Sstevel@tonic-gate 			goto next;
20897c478bd9Sstevel@tonic-gate 		}
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate /*
20927c478bd9Sstevel@tonic-gate  * setup path string to be passed to writefile
20937c478bd9Sstevel@tonic-gate  *
20947c478bd9Sstevel@tonic-gate  * We need to include attribute in the string so that
20957c478bd9Sstevel@tonic-gate  * a useful error message can be printed in the case of a failure.
20967c478bd9Sstevel@tonic-gate  */
20977c478bd9Sstevel@tonic-gate 		attrstr = gettext(" attribute ");
20987c478bd9Sstevel@tonic-gate 		src_size = strlen(source) +
20997c478bd9Sstevel@tonic-gate 		    strlen(dp->d_name) + strlen(attrstr) + 1;
21007c478bd9Sstevel@tonic-gate 		srcbuf = malloc(src_size);
21017c478bd9Sstevel@tonic-gate 
21027c478bd9Sstevel@tonic-gate 		if (srcbuf == NULL) {
21037c478bd9Sstevel@tonic-gate 			if (!attrsilent) {
21047c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
21057c478bd9Sstevel@tonic-gate 				gettext("%s: could not allocate memory"
21067c478bd9Sstevel@tonic-gate 					" for path buffer: "), cmd);
21077c478bd9Sstevel@tonic-gate 				perror("");
21087c478bd9Sstevel@tonic-gate 				++error;
21097c478bd9Sstevel@tonic-gate 			}
21107c478bd9Sstevel@tonic-gate 			goto next;
21117c478bd9Sstevel@tonic-gate 		}
21127c478bd9Sstevel@tonic-gate 		targ_size = strlen(target) +
21137c478bd9Sstevel@tonic-gate 		    strlen(dp->d_name) + strlen(attrstr) + 1;
21147c478bd9Sstevel@tonic-gate 		targbuf = malloc(targ_size);
21157c478bd9Sstevel@tonic-gate 		if (targbuf == NULL) {
21167c478bd9Sstevel@tonic-gate 			if (!attrsilent) {
21177c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
21187c478bd9Sstevel@tonic-gate 					gettext("%s: could not allocate memory"
21197c478bd9Sstevel@tonic-gate 						" for path buffer: "), cmd);
21207c478bd9Sstevel@tonic-gate 				perror("");
21217c478bd9Sstevel@tonic-gate 				++error;
21227c478bd9Sstevel@tonic-gate 			}
21237c478bd9Sstevel@tonic-gate 			goto next;
21247c478bd9Sstevel@tonic-gate 		}
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 		(void) snprintf(srcbuf, src_size, "%s%s%s",
21277c478bd9Sstevel@tonic-gate 		    source, attrstr, dp->d_name);
21287c478bd9Sstevel@tonic-gate 		(void) snprintf(targbuf, targ_size, "%s%s%s",
21297c478bd9Sstevel@tonic-gate 		    target, attrstr, dp->d_name);
21307c478bd9Sstevel@tonic-gate 
21317c478bd9Sstevel@tonic-gate 		if (writefile(srcattrfd, targattrfd,
21327c478bd9Sstevel@tonic-gate 		    srcbuf, targbuf, &s3, &s4) != 0) {
21337c478bd9Sstevel@tonic-gate 			if (!attrsilent) {
21347c478bd9Sstevel@tonic-gate 				++error;
21357c478bd9Sstevel@tonic-gate 			}
21367c478bd9Sstevel@tonic-gate 			goto next;
21377c478bd9Sstevel@tonic-gate 		}
21387c478bd9Sstevel@tonic-gate 
21397c478bd9Sstevel@tonic-gate 		if (pflg || mve) {
21407c478bd9Sstevel@tonic-gate 			mode = FMODE(s3);
21417c478bd9Sstevel@tonic-gate 
21427c478bd9Sstevel@tonic-gate 			if (fchown(targattrfd, UID(s3), GID(s3)) != 0) {
21437c478bd9Sstevel@tonic-gate 				if (!attrsilent) {
21447c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
21457c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot change"
21467c478bd9Sstevel@tonic-gate 					    " owner and group of"
21477c478bd9Sstevel@tonic-gate 					    " attribute %s for" " file"
21487c478bd9Sstevel@tonic-gate 					    " %s: "), cmd, dp->d_name, target);
21497c478bd9Sstevel@tonic-gate 					perror("");
21507c478bd9Sstevel@tonic-gate 					++error;
21517c478bd9Sstevel@tonic-gate 				}
21527c478bd9Sstevel@tonic-gate 				if (mode & (S_ISUID | S_ISGID)) {
21537c478bd9Sstevel@tonic-gate 					/* try to clear S_ISUID and S_ISGID */
21547c478bd9Sstevel@tonic-gate 					mode &= ~S_ISUID & ~S_ISGID;
21557c478bd9Sstevel@tonic-gate 					++clearflg;
21567c478bd9Sstevel@tonic-gate 				}
21577c478bd9Sstevel@tonic-gate 			}
21587c478bd9Sstevel@tonic-gate 			/* tv_usec were cleared above */
21597c478bd9Sstevel@tonic-gate 			times[0].tv_sec = s3.st_atime;
21607c478bd9Sstevel@tonic-gate 			times[1].tv_sec = s3.st_mtime;
21617c478bd9Sstevel@tonic-gate 			if (futimesat(targetdirfd, dp->d_name, times) < 0) {
21627c478bd9Sstevel@tonic-gate 				if (!attrsilent) {
21637c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
21647c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot set attribute"
21657c478bd9Sstevel@tonic-gate 					    " times for %s: "), cmd, target);
21667c478bd9Sstevel@tonic-gate 					perror("");
21677c478bd9Sstevel@tonic-gate 					++error;
21687c478bd9Sstevel@tonic-gate 				}
21697c478bd9Sstevel@tonic-gate 			}
21707c478bd9Sstevel@tonic-gate 			if (fchmod(targattrfd, mode) != 0) {
21717c478bd9Sstevel@tonic-gate 				if (clearflg) {
21727c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
21737c478bd9Sstevel@tonic-gate 					    "%s: cannot clear S_ISUID and "
21747c478bd9Sstevel@tonic-gate 					    "S_ISGID bits in attribute %s"
21757c478bd9Sstevel@tonic-gate 					    " for file"
21767c478bd9Sstevel@tonic-gate 					    " %s: "), cmd, dp->d_name, target);
21777c478bd9Sstevel@tonic-gate 				} else {
21787c478bd9Sstevel@tonic-gate 					if (!attrsilent) {
21797c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
21807c478bd9Sstevel@tonic-gate 							gettext(
21817c478bd9Sstevel@tonic-gate 				"%s: cannot set permissions of attribute"
21827c478bd9Sstevel@tonic-gate 				" %s for %s: "), cmd, dp->d_name, target);
21837c478bd9Sstevel@tonic-gate 						perror("");
21847c478bd9Sstevel@tonic-gate 						++error;
21857c478bd9Sstevel@tonic-gate 					}
21867c478bd9Sstevel@tonic-gate 				}
21877c478bd9Sstevel@tonic-gate 			}
2188d2443e76Smarks 			if (xacl && ((facl_set(targattrfd, xacl)) < 0)) {
2189d2443e76Smarks 				if (!attrsilent) {
2190d2443e76Smarks 					(void) fprintf(stderr, gettext(
2191d2443e76Smarks 					    "%s: failed to set acl entries on"
2192d2443e76Smarks 					    " attribute %s for"
2193d2443e76Smarks 					    "%s\n"), cmd, dp->d_name, target);
2194d2443e76Smarks 					++error;
2195d2443e76Smarks 				}
2196d2443e76Smarks 				acl_free(xacl);
2197d2443e76Smarks 				xacl = NULL;
2198d2443e76Smarks 			}
21997c478bd9Sstevel@tonic-gate 		}
22007c478bd9Sstevel@tonic-gate next:
2201fa9e4066Sahrens 		if (xacl != NULL) {
2202fa9e4066Sahrens 			acl_free(xacl);
2203fa9e4066Sahrens 			xacl = NULL;
22047c478bd9Sstevel@tonic-gate 		}
22057c478bd9Sstevel@tonic-gate 		if (srcbuf != NULL)
22067c478bd9Sstevel@tonic-gate 			free(srcbuf);
22077c478bd9Sstevel@tonic-gate 		if (targbuf != NULL)
22087c478bd9Sstevel@tonic-gate 			free(targbuf);
22097c478bd9Sstevel@tonic-gate 		if (srcattrfd != -1)
22107c478bd9Sstevel@tonic-gate 			(void) close(srcattrfd);
22117c478bd9Sstevel@tonic-gate 		if (targattrfd != -1)
22127c478bd9Sstevel@tonic-gate 			(void) close(targattrfd);
22137c478bd9Sstevel@tonic-gate 		srcattrfd = targattrfd = -1;
22147c478bd9Sstevel@tonic-gate 		srcbuf = targbuf = NULL;
22157c478bd9Sstevel@tonic-gate 	}
22167c478bd9Sstevel@tonic-gate out:
2217fa9e4066Sahrens 	if (xacl != NULL) {
2218fa9e4066Sahrens 		acl_free(xacl);
2219fa9e4066Sahrens 		xacl = NULL;
2220fa9e4066Sahrens 	}
2221fa9e4066Sahrens 	if (attrdiracl != NULL) {
2222fa9e4066Sahrens 		acl_free(attrdiracl);
2223fa9e4066Sahrens 		attrdiracl = NULL;
2224fa9e4066Sahrens 	}
22257c478bd9Sstevel@tonic-gate 	if (srcbuf)
22267c478bd9Sstevel@tonic-gate 		free(srcbuf);
22277c478bd9Sstevel@tonic-gate 	if (targbuf)
22287c478bd9Sstevel@tonic-gate 		free(targbuf);
22297c478bd9Sstevel@tonic-gate 	if (sourcedirfd != -1)
22307c478bd9Sstevel@tonic-gate 		(void) close(sourcedirfd);
22317c478bd9Sstevel@tonic-gate 	if (targetdirfd != -1)
22327c478bd9Sstevel@tonic-gate 		(void) close(targetdirfd);
22337c478bd9Sstevel@tonic-gate 	if (srcdirp != NULL)
22347c478bd9Sstevel@tonic-gate 		(void) closedir(srcdirp);
22357c478bd9Sstevel@tonic-gate 	if (srcfd != -1)
22367c478bd9Sstevel@tonic-gate 		(void) close(srcfd);
22377c478bd9Sstevel@tonic-gate 	if (targfd != -1)
22387c478bd9Sstevel@tonic-gate 		(void) close(targfd);
22397c478bd9Sstevel@tonic-gate 	return (error == 0 ? 0 : 1);
22407c478bd9Sstevel@tonic-gate }
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate /*
22437c478bd9Sstevel@tonic-gate  * nanoseconds are rounded off to microseconds by flooring.
22447c478bd9Sstevel@tonic-gate  */
22457c478bd9Sstevel@tonic-gate static void
22467c478bd9Sstevel@tonic-gate timestruc_to_timeval(timestruc_t *ts, struct timeval *tv)
22477c478bd9Sstevel@tonic-gate {
22487c478bd9Sstevel@tonic-gate 	tv->tv_sec = ts->tv_sec;
22497c478bd9Sstevel@tonic-gate 	tv->tv_usec = ts->tv_nsec / 1000;
22507c478bd9Sstevel@tonic-gate }
2251