xref: /illumos-gate/usr/src/cmd/mv/mv.c (revision da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0)
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 /*
220f6e7ba6Sas  * 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 <sys/time.h>
487c478bd9Sstevel@tonic-gate #include <signal.h>
497c478bd9Sstevel@tonic-gate #include <locale.h>
507c478bd9Sstevel@tonic-gate #include <stdarg.h>
517c478bd9Sstevel@tonic-gate #include <sys/acl.h>
527c478bd9Sstevel@tonic-gate #include <libcmdutils.h>
53fa9e4066Sahrens #include <aclutils.h>
543d63ea05Sas #include "getresponse.h"
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #define	FTYPE(A)	(A.st_mode)
577c478bd9Sstevel@tonic-gate #define	FMODE(A)	(A.st_mode)
587c478bd9Sstevel@tonic-gate #define	UID(A)		(A.st_uid)
597c478bd9Sstevel@tonic-gate #define	GID(A)		(A.st_gid)
607c478bd9Sstevel@tonic-gate #define	IDENTICAL(A, B)	(A.st_dev == B.st_dev && A.st_ino == B.st_ino)
617c478bd9Sstevel@tonic-gate #define	ISBLK(A)	((A.st_mode & S_IFMT) == S_IFBLK)
627c478bd9Sstevel@tonic-gate #define	ISCHR(A)	((A.st_mode & S_IFMT) == S_IFCHR)
637c478bd9Sstevel@tonic-gate #define	ISDIR(A)	((A.st_mode & S_IFMT) == S_IFDIR)
647c478bd9Sstevel@tonic-gate #define	ISDOOR(A)	((A.st_mode & S_IFMT) == S_IFDOOR)
657c478bd9Sstevel@tonic-gate #define	ISFIFO(A)	((A.st_mode & S_IFMT) == S_IFIFO)
667c478bd9Sstevel@tonic-gate #define	ISLNK(A)	((A.st_mode & S_IFMT) == S_IFLNK)
677c478bd9Sstevel@tonic-gate #define	ISREG(A)	(((A).st_mode & S_IFMT) == S_IFREG)
687c478bd9Sstevel@tonic-gate #define	ISDEV(A)	((A.st_mode & S_IFMT) == S_IFCHR || \
697c478bd9Sstevel@tonic-gate 			(A.st_mode & S_IFMT) == S_IFBLK || \
707c478bd9Sstevel@tonic-gate 			(A.st_mode & S_IFMT) == S_IFIFO)
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #define	BLKSIZE	4096
737c478bd9Sstevel@tonic-gate #define	PATHSIZE 1024
747c478bd9Sstevel@tonic-gate #define	DOT	"."
757c478bd9Sstevel@tonic-gate #define	DOTDOT	".."
767c478bd9Sstevel@tonic-gate #define	DELIM	'/'
777c478bd9Sstevel@tonic-gate #define	EQ(x, y)	(strcmp(x, y) == 0)
787c478bd9Sstevel@tonic-gate #define	FALSE	0
797c478bd9Sstevel@tonic-gate #define	MODEBITS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)
807c478bd9Sstevel@tonic-gate #define	TRUE 1
817c478bd9Sstevel@tonic-gate #define	MAXMAPSIZE	(1024*1024*8)	/* map at most 8MB */
827c478bd9Sstevel@tonic-gate #define	SMALLFILESIZE	(32*1024)	/* don't use mmap on little files */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static char		*dname(char *);
857c478bd9Sstevel@tonic-gate static int		lnkfil(char *, char *);
867c478bd9Sstevel@tonic-gate static int		cpymve(char *, char *);
877c478bd9Sstevel@tonic-gate static int		chkfiles(char *, char **);
887c478bd9Sstevel@tonic-gate static int		rcopy(char *, char *);
897c478bd9Sstevel@tonic-gate static int		chk_different(char *, char *);
907c478bd9Sstevel@tonic-gate static int		chg_time(char *, struct stat);
917c478bd9Sstevel@tonic-gate static int		chg_mode(char *, uid_t, gid_t, mode_t);
927c478bd9Sstevel@tonic-gate static int		copydir(char *, char *);
937c478bd9Sstevel@tonic-gate static int		copyspecial(char *);
947c478bd9Sstevel@tonic-gate static int		getrealpath(char *, char *);
957c478bd9Sstevel@tonic-gate static void		usage(void);
967c478bd9Sstevel@tonic-gate static void		Perror(char *);
977c478bd9Sstevel@tonic-gate static void		Perror2(char *, char *);
987c478bd9Sstevel@tonic-gate static int		use_stdin(void);
997c478bd9Sstevel@tonic-gate static int		copyattributes(char *, char *);
100*da6c28aaSamw static int		copy_sysattr(char *, char *);
1017c478bd9Sstevel@tonic-gate static void		timestruc_to_timeval(timestruc_t *, struct timeval *);
1027c478bd9Sstevel@tonic-gate static tree_node_t	*create_tnode(dev_t, ino_t);
1037c478bd9Sstevel@tonic-gate 
104*da6c28aaSamw static struct stat 	s1, s2, s3, s4;
1057c478bd9Sstevel@tonic-gate static int 		cpy = FALSE;
1067c478bd9Sstevel@tonic-gate static int 		mve = FALSE;
1077c478bd9Sstevel@tonic-gate static int 		lnk = FALSE;
1087c478bd9Sstevel@tonic-gate static char		*cmd;
1097c478bd9Sstevel@tonic-gate static int		silent = 0;
1107c478bd9Sstevel@tonic-gate static int		fflg = 0;
1117c478bd9Sstevel@tonic-gate static int		iflg = 0;
1127c478bd9Sstevel@tonic-gate static int		pflg = 0;
1137c478bd9Sstevel@tonic-gate static int		Rflg = 0;	/* recursive copy */
1147c478bd9Sstevel@tonic-gate static int		rflg = 0;	/* recursive copy */
1157c478bd9Sstevel@tonic-gate static int		sflg = 0;
1167c478bd9Sstevel@tonic-gate static int		Hflg = 0;	/* follow cmd line arg symlink to dir */
1177c478bd9Sstevel@tonic-gate static int		Lflg = 0;	/* follow symlinks */
1187c478bd9Sstevel@tonic-gate static int		Pflg = 0;	/* do not follow symlinks */
1197c478bd9Sstevel@tonic-gate static int		atflg = 0;
1207c478bd9Sstevel@tonic-gate static int		attrsilent = 0;
1217c478bd9Sstevel@tonic-gate static int		targetexists = 0;
1227c478bd9Sstevel@tonic-gate static int		cmdarg;		/* command line argument */
1237c478bd9Sstevel@tonic-gate static avl_tree_t	*stree = NULL;	/* source file inode search tree */
124fa9e4066Sahrens static acl_t		*s1acl;
125*da6c28aaSamw static int		saflg = 0;	/* 'cp' extended system attr. */
126*da6c28aaSamw static int		srcfd;
127*da6c28aaSamw static int		targfd;
128*da6c28aaSamw static int		sourcedirfd;
129*da6c28aaSamw static int		targetdirfd;
130*da6c28aaSamw static DIR 		*srcdirp;
131*da6c28aaSamw static int		srcattrfd;
132*da6c28aaSamw static int		targattrfd;
133*da6c28aaSamw static struct stat 	attrdir;
134*da6c28aaSamw 
135*da6c28aaSamw /* Extended system attributes support */
136*da6c28aaSamw 
137*da6c28aaSamw static int open_source(char  *);
138*da6c28aaSamw static int open_target_srctarg_attrdirs(char  *, char *);
139*da6c28aaSamw static int open_attrdirp(char *);
140*da6c28aaSamw static int traverse_attrfile(struct dirent *, char *, char *, int);
141*da6c28aaSamw static void rewind_attrdir(DIR *);
142*da6c28aaSamw static void close_all();
143*da6c28aaSamw 
1447c478bd9Sstevel@tonic-gate 
145a77d64afScf 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);
1703d63ea05Sas 	if (init_yes() < 0) {
1713d63ea05Sas 		(void) fprintf(stderr, gettext(ERR_MSG_INIT_YES),
1723d63ea05Sas 		    strerror(errno));
1733d63ea05Sas 		exit(3);
1743d63ea05Sas 	}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	if (EQ(cmd, "mv"))
1777c478bd9Sstevel@tonic-gate 		mve = TRUE;
1787c478bd9Sstevel@tonic-gate 	else if (EQ(cmd, "ln"))
1797c478bd9Sstevel@tonic-gate 		lnk = TRUE;
1807c478bd9Sstevel@tonic-gate 	else if (EQ(cmd, "cp"))
1817c478bd9Sstevel@tonic-gate 		cpy = TRUE;
1827c478bd9Sstevel@tonic-gate 	else {
1837c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1847c478bd9Sstevel@tonic-gate 		    gettext("Invalid command name (%s); expecting "
1857c478bd9Sstevel@tonic-gate 		    "mv, cp, or ln.\n"), cmd);
1867c478bd9Sstevel@tonic-gate 		exit(1);
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	/*
1907c478bd9Sstevel@tonic-gate 	 * Check for options:
191*da6c28aaSamw 	 * 	cp  -r|-R [-H|-L|-P] [-fip@/] file1 [file2 ...] target
192*da6c28aaSamw 	 * 	cp [-fiprR@/] file1 [file2 ...] target
1937c478bd9Sstevel@tonic-gate 	 *	ln [-f] [-n] [-s] file1 [file2 ...] target
1947c478bd9Sstevel@tonic-gate 	 *	ln [-f] [-n] [-s] file1 [file2 ...]
1957c478bd9Sstevel@tonic-gate 	 *	mv [-f|i] file1 [file2 ...] target
1967c478bd9Sstevel@tonic-gate 	 *	mv [-f|i] dir1 target
1977c478bd9Sstevel@tonic-gate 	 */
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	if (cpy) {
200*da6c28aaSamw 		while ((c = getopt(argc, argv, "fHiLpPrR@/")) != EOF)
2017c478bd9Sstevel@tonic-gate 			switch (c) {
2027c478bd9Sstevel@tonic-gate 			case 'f':
2037c478bd9Sstevel@tonic-gate 				fflg++;
2047c478bd9Sstevel@tonic-gate 				break;
2057c478bd9Sstevel@tonic-gate 			case 'i':
2067c478bd9Sstevel@tonic-gate 				iflg++;
2077c478bd9Sstevel@tonic-gate 				break;
2087c478bd9Sstevel@tonic-gate 			case 'p':
2097c478bd9Sstevel@tonic-gate 				pflg++;
2107c478bd9Sstevel@tonic-gate #ifdef XPG4
2117c478bd9Sstevel@tonic-gate 				attrsilent = 1;
2127c478bd9Sstevel@tonic-gate 				atflg = 0;
213*da6c28aaSamw 				saflg = 0;
2147c478bd9Sstevel@tonic-gate #else
215*da6c28aaSamw 				if ((atflg == 0) && (saflg == 0))
2167c478bd9Sstevel@tonic-gate 					attrsilent = 1;
2177c478bd9Sstevel@tonic-gate #endif
2187c478bd9Sstevel@tonic-gate 				break;
2197c478bd9Sstevel@tonic-gate 			case 'H':
2207c478bd9Sstevel@tonic-gate 				/*
2217c478bd9Sstevel@tonic-gate 				 * If more than one of -H, -L, or -P are
2227c478bd9Sstevel@tonic-gate 				 * specified, only the last option specified
2237c478bd9Sstevel@tonic-gate 				 * determines the behavior.
2247c478bd9Sstevel@tonic-gate 				 */
2257c478bd9Sstevel@tonic-gate 				Lflg = Pflg = 0;
2267c478bd9Sstevel@tonic-gate 				Hflg++;
2277c478bd9Sstevel@tonic-gate 				break;
2287c478bd9Sstevel@tonic-gate 			case 'L':
2297c478bd9Sstevel@tonic-gate 				Hflg = Pflg = 0;
2307c478bd9Sstevel@tonic-gate 				Lflg++;
2317c478bd9Sstevel@tonic-gate 				break;
2327c478bd9Sstevel@tonic-gate 			case 'P':
2337c478bd9Sstevel@tonic-gate 				Lflg = Hflg = 0;
2347c478bd9Sstevel@tonic-gate 				Pflg++;
2357c478bd9Sstevel@tonic-gate 				break;
2367c478bd9Sstevel@tonic-gate 			case 'R':
2377c478bd9Sstevel@tonic-gate 				/*
2387c478bd9Sstevel@tonic-gate 				 * The default behavior of cp -R|-r
2397c478bd9Sstevel@tonic-gate 				 * when specified without -H|-L|-P
2407c478bd9Sstevel@tonic-gate 				 * is -L.
2417c478bd9Sstevel@tonic-gate 				 */
2427c478bd9Sstevel@tonic-gate 				Rflg++;
2437c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
2447c478bd9Sstevel@tonic-gate 			case 'r':
2457c478bd9Sstevel@tonic-gate 				rflg++;
2467c478bd9Sstevel@tonic-gate 				break;
2477c478bd9Sstevel@tonic-gate 			case '@':
2487c478bd9Sstevel@tonic-gate 				atflg++;
2497c478bd9Sstevel@tonic-gate 				attrsilent = 0;
2507c478bd9Sstevel@tonic-gate #ifdef XPG4
2517c478bd9Sstevel@tonic-gate 				pflg = 0;
252*da6c28aaSamw #endif
253*da6c28aaSamw 				break;
254*da6c28aaSamw 			case '/':
255*da6c28aaSamw 				saflg++;
256*da6c28aaSamw 				attrsilent = 0;
257*da6c28aaSamw #ifdef XPG4
258*da6c28aaSamw 				pflg = 0;
2597c478bd9Sstevel@tonic-gate #endif
2607c478bd9Sstevel@tonic-gate 				break;
2617c478bd9Sstevel@tonic-gate 			default:
2627c478bd9Sstevel@tonic-gate 				errflg++;
2637c478bd9Sstevel@tonic-gate 			}
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 		/* -R or -r must be specified with -H, -L, or -P */
2667c478bd9Sstevel@tonic-gate 		if ((Hflg || Lflg || Pflg) && !(Rflg || rflg)) {
2677c478bd9Sstevel@tonic-gate 			errflg++;
2687c478bd9Sstevel@tonic-gate 		}
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	} else if (mve) {
2717c478bd9Sstevel@tonic-gate 		while ((c = getopt(argc, argv, "fis")) != EOF)
2727c478bd9Sstevel@tonic-gate 			switch (c) {
2737c478bd9Sstevel@tonic-gate 			case 'f':
2747c478bd9Sstevel@tonic-gate 				silent++;
2757c478bd9Sstevel@tonic-gate #ifdef XPG4
2767c478bd9Sstevel@tonic-gate 				iflg = 0;
2777c478bd9Sstevel@tonic-gate #endif
2787c478bd9Sstevel@tonic-gate 				break;
2797c478bd9Sstevel@tonic-gate 			case 'i':
2807c478bd9Sstevel@tonic-gate 				iflg++;
2817c478bd9Sstevel@tonic-gate #ifdef XPG4
2827c478bd9Sstevel@tonic-gate 				silent = 0;
2837c478bd9Sstevel@tonic-gate #endif
2847c478bd9Sstevel@tonic-gate 				break;
2857c478bd9Sstevel@tonic-gate 			default:
2867c478bd9Sstevel@tonic-gate 				errflg++;
2877c478bd9Sstevel@tonic-gate 			}
2887c478bd9Sstevel@tonic-gate 	} else { /* ln */
2897c478bd9Sstevel@tonic-gate 		while ((c = getopt(argc, argv, "fns")) != EOF)
2907c478bd9Sstevel@tonic-gate 			switch (c) {
2917c478bd9Sstevel@tonic-gate 			case 'f':
2927c478bd9Sstevel@tonic-gate 				silent++;
2937c478bd9Sstevel@tonic-gate 				break;
2947c478bd9Sstevel@tonic-gate 			case 'n':
2957c478bd9Sstevel@tonic-gate 				/* silently ignored; this is the default */
2967c478bd9Sstevel@tonic-gate 				break;
2977c478bd9Sstevel@tonic-gate 			case 's':
2987c478bd9Sstevel@tonic-gate 				sflg++;
2997c478bd9Sstevel@tonic-gate 				break;
3007c478bd9Sstevel@tonic-gate 			default:
3017c478bd9Sstevel@tonic-gate 				errflg++;
3027c478bd9Sstevel@tonic-gate 			}
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	/*
3067c478bd9Sstevel@tonic-gate 	 * For BSD compatibility allow - to delimit the end of
3077c478bd9Sstevel@tonic-gate 	 * options for mv.
3087c478bd9Sstevel@tonic-gate 	 */
3097c478bd9Sstevel@tonic-gate 	if (mve && optind < argc && (strcmp(argv[optind], "-") == 0))
3107c478bd9Sstevel@tonic-gate 		optind++;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	/*
3137c478bd9Sstevel@tonic-gate 	 * Check for sufficient arguments
3147c478bd9Sstevel@tonic-gate 	 * or a usage error.
3157c478bd9Sstevel@tonic-gate 	 */
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	argc -= optind;
3187c478bd9Sstevel@tonic-gate 	argv  = &argv[optind];
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	if ((argc < 2 && lnk != TRUE) || (argc < 1 && lnk == TRUE)) {
3217c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3227c478bd9Sstevel@tonic-gate 		    gettext("%s: Insufficient arguments (%d)\n"),
3237c478bd9Sstevel@tonic-gate 		    cmd, argc);
3247c478bd9Sstevel@tonic-gate 		usage();
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	if (errflg != 0)
3287c478bd9Sstevel@tonic-gate 		usage();
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	/*
3317c478bd9Sstevel@tonic-gate 	 * If there is more than a source and target,
3327c478bd9Sstevel@tonic-gate 	 * the last argument (the target) must be a directory
3337c478bd9Sstevel@tonic-gate 	 * which really exists.
3347c478bd9Sstevel@tonic-gate 	 */
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	if (argc > 2) {
3377c478bd9Sstevel@tonic-gate 		if (stat(argv[argc-1], &s2) < 0) {
3387c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3397c478bd9Sstevel@tonic-gate 			    gettext("%s: %s not found\n"),
3407c478bd9Sstevel@tonic-gate 			    cmd, argv[argc-1]);
3417c478bd9Sstevel@tonic-gate 			exit(2);
3427c478bd9Sstevel@tonic-gate 		}
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 		if (!ISDIR(s2)) {
3457c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3467c478bd9Sstevel@tonic-gate 			    gettext("%s: Target %s must be a directory\n"),
3477c478bd9Sstevel@tonic-gate 			    cmd, argv[argc-1]);
3487c478bd9Sstevel@tonic-gate 			usage();
3497c478bd9Sstevel@tonic-gate 		}
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	if (strlen(argv[argc-1]) >= PATH_MAX) {
3537c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3547c478bd9Sstevel@tonic-gate 		    gettext("%s: Target %s file name length exceeds PATH_MAX"
3557c478bd9Sstevel@tonic-gate 		    " %d\n"), cmd, argv[argc-1], PATH_MAX);
3567c478bd9Sstevel@tonic-gate 		exit(78);
3577c478bd9Sstevel@tonic-gate 	}
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	if (argc == 1) {
3607c478bd9Sstevel@tonic-gate 		if (!lnk)
3617c478bd9Sstevel@tonic-gate 			usage();
3627c478bd9Sstevel@tonic-gate 		(void) strcpy(target, ".");
3637c478bd9Sstevel@tonic-gate 	} else {
3647c478bd9Sstevel@tonic-gate 		(void) strcpy(target, argv[--argc]);
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	/*
3687c478bd9Sstevel@tonic-gate 	 * Perform a multiple argument mv|cp|ln by
3697c478bd9Sstevel@tonic-gate 	 * multiple invocations of cpymve() or lnkfil().
3707c478bd9Sstevel@tonic-gate 	 */
3717c478bd9Sstevel@tonic-gate 	if (lnk)
3727c478bd9Sstevel@tonic-gate 		move = lnkfil;
3737c478bd9Sstevel@tonic-gate 	else
3747c478bd9Sstevel@tonic-gate 		move = cpymve;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	r = 0;
3777c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
3787c478bd9Sstevel@tonic-gate 		stree = NULL;
3797c478bd9Sstevel@tonic-gate 		cmdarg = 1;
3807c478bd9Sstevel@tonic-gate 		r += move(argv[i], target);
3817c478bd9Sstevel@tonic-gate 	}
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	/*
3847c478bd9Sstevel@tonic-gate 	 * Show errors by nonzero exit code.
3857c478bd9Sstevel@tonic-gate 	 */
3867c478bd9Sstevel@tonic-gate 
387a77d64afScf 	return (r?2:0);
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate static int
3917c478bd9Sstevel@tonic-gate lnkfil(char *source, char *target)
3927c478bd9Sstevel@tonic-gate {
3937c478bd9Sstevel@tonic-gate 	char	*buf = NULL;
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	if (sflg) {
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 		/*
3987c478bd9Sstevel@tonic-gate 		 * If target is a directory make complete
3997c478bd9Sstevel@tonic-gate 		 * name of the new symbolic link within that
4007c478bd9Sstevel@tonic-gate 		 * directory.
4017c478bd9Sstevel@tonic-gate 		 */
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 		if ((stat(target, &s2) >= 0) && ISDIR(s2)) {
4047c478bd9Sstevel@tonic-gate 			size_t len;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 			len = strlen(target) + strlen(dname(source)) + 4;
4077c478bd9Sstevel@tonic-gate 			if ((buf = (char *)malloc(len)) == NULL) {
4087c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4097c478bd9Sstevel@tonic-gate 				    gettext("%s: Insufficient memory "
4107c478bd9Sstevel@tonic-gate 				    "to %s %s\n"), cmd, cmd, source);
4117c478bd9Sstevel@tonic-gate 				exit(3);
4127c478bd9Sstevel@tonic-gate 			}
4137c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, len, "%s/%s",
4147c478bd9Sstevel@tonic-gate 			    target, dname(source));
4157c478bd9Sstevel@tonic-gate 			target = buf;
4167c478bd9Sstevel@tonic-gate 		}
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 		/*
4197c478bd9Sstevel@tonic-gate 		 * Check to see if the file exists already
4207c478bd9Sstevel@tonic-gate 		 */
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 		if ((stat(target, &s2) == 0)) {
4237c478bd9Sstevel@tonic-gate 			/*
4247c478bd9Sstevel@tonic-gate 			 * Check if the silent flag is set ie. the -f option
4257c478bd9Sstevel@tonic-gate 			 * is used.  If so, use unlink to remove the current
4267c478bd9Sstevel@tonic-gate 			 * target to replace with the new target, specified
4277c478bd9Sstevel@tonic-gate 			 * on the command line.  Proceed with symlink.
4287c478bd9Sstevel@tonic-gate 			 */
4297c478bd9Sstevel@tonic-gate 			if (silent) {
4307c478bd9Sstevel@tonic-gate 				if (unlink(target) < 0) {
4317c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
4327c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot unlink %s: "),
4337c478bd9Sstevel@tonic-gate 					    cmd, target);
4347c478bd9Sstevel@tonic-gate 					perror("");
4357c478bd9Sstevel@tonic-gate 					return (1);
4367c478bd9Sstevel@tonic-gate 				}
4377c478bd9Sstevel@tonic-gate 			}
4387c478bd9Sstevel@tonic-gate 		}
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 		/*
4427c478bd9Sstevel@tonic-gate 		 * Create a symbolic link to the source.
4437c478bd9Sstevel@tonic-gate 		 */
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 		if (symlink(source, target) < 0) {
4467c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4477c478bd9Sstevel@tonic-gate 			    gettext("%s: cannot create %s: "),
4487c478bd9Sstevel@tonic-gate 			    cmd, target);
4497c478bd9Sstevel@tonic-gate 			perror("");
4507c478bd9Sstevel@tonic-gate 			if (buf != NULL)
4517c478bd9Sstevel@tonic-gate 				free(buf);
4527c478bd9Sstevel@tonic-gate 			return (1);
4537c478bd9Sstevel@tonic-gate 		}
4547c478bd9Sstevel@tonic-gate 		if (buf != NULL)
4557c478bd9Sstevel@tonic-gate 			free(buf);
4567c478bd9Sstevel@tonic-gate 		return (0);
4577c478bd9Sstevel@tonic-gate 	}
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	switch (chkfiles(source, &target)) {
4607c478bd9Sstevel@tonic-gate 		case 1: return (1);
4617c478bd9Sstevel@tonic-gate 		case 2: return (0);
4627c478bd9Sstevel@tonic-gate 			/* default - fall through */
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	/*
4667c478bd9Sstevel@tonic-gate 	 * Make sure source file is not a directory,
4677c478bd9Sstevel@tonic-gate 	 * we can't link directories...
4687c478bd9Sstevel@tonic-gate 	 */
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	if (ISDIR(s1)) {
4717c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4727c478bd9Sstevel@tonic-gate 		    gettext("%s: %s is a directory\n"), cmd, source);
4737c478bd9Sstevel@tonic-gate 		return (1);
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	/*
4777c478bd9Sstevel@tonic-gate 	 * hard link, call link() and return.
4787c478bd9Sstevel@tonic-gate 	 */
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	if (link(source, target) < 0) {
4817c478bd9Sstevel@tonic-gate 		if (errno == EXDEV)
4827c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4837c478bd9Sstevel@tonic-gate 			    gettext("%s: %s is on a different file system\n"),
4847c478bd9Sstevel@tonic-gate 			    cmd, target);
4857c478bd9Sstevel@tonic-gate 		else {
4867c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4877c478bd9Sstevel@tonic-gate 			    gettext("%s: cannot create link %s: "),
4887c478bd9Sstevel@tonic-gate 			    cmd, target);
4897c478bd9Sstevel@tonic-gate 			perror("");
4907c478bd9Sstevel@tonic-gate 		}
4917c478bd9Sstevel@tonic-gate 		if (buf != NULL)
4927c478bd9Sstevel@tonic-gate 			free(buf);
4937c478bd9Sstevel@tonic-gate 		return (1);
4947c478bd9Sstevel@tonic-gate 	} else {
4957c478bd9Sstevel@tonic-gate 		if (buf != NULL)
4967c478bd9Sstevel@tonic-gate 			free(buf);
4977c478bd9Sstevel@tonic-gate 		return (0);
4987c478bd9Sstevel@tonic-gate 	}
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate static int
5027c478bd9Sstevel@tonic-gate cpymve(char *source, char *target)
5037c478bd9Sstevel@tonic-gate {
5047c478bd9Sstevel@tonic-gate 	int	n;
5057c478bd9Sstevel@tonic-gate 	int fi, fo;
5067c478bd9Sstevel@tonic-gate 	int ret = 0;
5077c478bd9Sstevel@tonic-gate 	int attret = 0;
508*da6c28aaSamw 	int sattret = 0;
5097c478bd9Sstevel@tonic-gate 	int errno_save;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	switch (chkfiles(source, &target)) {
5127c478bd9Sstevel@tonic-gate 		case 1: return (1);
5137c478bd9Sstevel@tonic-gate 		case 2: return (0);
5147c478bd9Sstevel@tonic-gate 			/* default - fall through */
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	/*
5187c478bd9Sstevel@tonic-gate 	 * If it's a recursive copy and source
5197c478bd9Sstevel@tonic-gate 	 * is a directory, then call rcopy (from copydir).
5207c478bd9Sstevel@tonic-gate 	 */
5217c478bd9Sstevel@tonic-gate 	if (cpy) {
5227c478bd9Sstevel@tonic-gate 		if (ISDIR(s1)) {
5237c478bd9Sstevel@tonic-gate 			int		rc;
5247c478bd9Sstevel@tonic-gate 			avl_index_t	where = 0;
5257c478bd9Sstevel@tonic-gate 			tree_node_t	*tnode;
5267c478bd9Sstevel@tonic-gate 			tree_node_t	*tptr;
5277c478bd9Sstevel@tonic-gate 			dev_t		save_dev = s1.st_dev;
5287c478bd9Sstevel@tonic-gate 			ino_t		save_ino = s1.st_ino;
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 			/*
5317c478bd9Sstevel@tonic-gate 			 * We will be recursing into the directory so
5327c478bd9Sstevel@tonic-gate 			 * save the inode information to a search tree
5337c478bd9Sstevel@tonic-gate 			 * to avoid getting into an endless loop.
5347c478bd9Sstevel@tonic-gate 			 */
5357c478bd9Sstevel@tonic-gate 			if ((rc = add_tnode(&stree, save_dev, save_ino)) != 1) {
5367c478bd9Sstevel@tonic-gate 				if (rc == 0) {
5377c478bd9Sstevel@tonic-gate 					/*
5387c478bd9Sstevel@tonic-gate 					 * We've already visited this directory.
5397c478bd9Sstevel@tonic-gate 					 * Don't remove the search tree entry
5407c478bd9Sstevel@tonic-gate 					 * to make sure we don't get into an
5417c478bd9Sstevel@tonic-gate 					 * endless loop if revisited from a
5427c478bd9Sstevel@tonic-gate 					 * different part of the hierarchy.
5437c478bd9Sstevel@tonic-gate 					 */
5447c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
5457c478bd9Sstevel@tonic-gate 					    "%s: cycle detected: %s\n"),
5467c478bd9Sstevel@tonic-gate 					    cmd, source);
5477c478bd9Sstevel@tonic-gate 				} else {
5487c478bd9Sstevel@tonic-gate 					Perror(source);
5497c478bd9Sstevel@tonic-gate 				}
5507c478bd9Sstevel@tonic-gate 				return (1);
5517c478bd9Sstevel@tonic-gate 			}
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 			cmdarg = 0;
5547c478bd9Sstevel@tonic-gate 			rc = copydir(source, target);
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 			/*
5577c478bd9Sstevel@tonic-gate 			 * Create a tnode to get an index to the matching
5587c478bd9Sstevel@tonic-gate 			 * node (same dev and inode) in the search tree,
5597c478bd9Sstevel@tonic-gate 			 * then use the index to remove the matching node
5607c478bd9Sstevel@tonic-gate 			 * so it we do not wrongly detect a cycle when
5617c478bd9Sstevel@tonic-gate 			 * revisiting this directory from another part of
5627c478bd9Sstevel@tonic-gate 			 * the hierarchy.
5637c478bd9Sstevel@tonic-gate 			 */
5647c478bd9Sstevel@tonic-gate 			if ((tnode = create_tnode(save_dev,
5657c478bd9Sstevel@tonic-gate 			    save_ino)) == NULL) {
5667c478bd9Sstevel@tonic-gate 				Perror(source);
5677c478bd9Sstevel@tonic-gate 				return (1);
5687c478bd9Sstevel@tonic-gate 			}
5697c478bd9Sstevel@tonic-gate 			if ((tptr = avl_find(stree, tnode, &where)) != NULL) {
5707c478bd9Sstevel@tonic-gate 				avl_remove(stree, tptr);
5717c478bd9Sstevel@tonic-gate 			}
5727c478bd9Sstevel@tonic-gate 			free(tptr);
5737c478bd9Sstevel@tonic-gate 			free(tnode);
5747c478bd9Sstevel@tonic-gate 			return (rc);
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 		} else if (ISDEV(s1) && Rflg) {
5777c478bd9Sstevel@tonic-gate 			return (copyspecial(target));
5787c478bd9Sstevel@tonic-gate 		} else {
5797c478bd9Sstevel@tonic-gate 			goto copy;
5807c478bd9Sstevel@tonic-gate 		}
5817c478bd9Sstevel@tonic-gate 	}
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	if (mve) {
5847c478bd9Sstevel@tonic-gate 		if (rename(source, target) >= 0)
5857c478bd9Sstevel@tonic-gate 			return (0);
5867c478bd9Sstevel@tonic-gate 		if (errno != EXDEV) {
5877c478bd9Sstevel@tonic-gate 			if (errno == ENOTDIR && ISDIR(s1)) {
5887c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
5897c478bd9Sstevel@tonic-gate 				    gettext("%s: %s is a directory\n"),
5907c478bd9Sstevel@tonic-gate 				    cmd, source);
5917c478bd9Sstevel@tonic-gate 				return (1);
5927c478bd9Sstevel@tonic-gate 			}
5937c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5947c478bd9Sstevel@tonic-gate 			    gettext("%s: cannot rename %s to %s: "),
5957c478bd9Sstevel@tonic-gate 			    cmd, source, target);
5967c478bd9Sstevel@tonic-gate 			perror("");
5977c478bd9Sstevel@tonic-gate 			return (1);
5987c478bd9Sstevel@tonic-gate 		}
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 		/*
6017c478bd9Sstevel@tonic-gate 		 * cannot move a non-directory (source) onto an existing
6027c478bd9Sstevel@tonic-gate 		 * directory (target)
6037c478bd9Sstevel@tonic-gate 		 *
6047c478bd9Sstevel@tonic-gate 		 */
6057c478bd9Sstevel@tonic-gate 		if (targetexists && ISDIR(s2) && (!ISDIR(s1))) {
6067c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
6077c478bd9Sstevel@tonic-gate 			    gettext("%s: cannot mv a non directory %s "
6087c478bd9Sstevel@tonic-gate 			    "over existing directory"
6097c478bd9Sstevel@tonic-gate 			    " %s \n"), cmd, source, target);
6107c478bd9Sstevel@tonic-gate 			return (1);
6117c478bd9Sstevel@tonic-gate 		}
6127c478bd9Sstevel@tonic-gate 		if (ISDIR(s1)) {
6137c478bd9Sstevel@tonic-gate #ifdef XPG4
6147c478bd9Sstevel@tonic-gate 			if (targetexists && ISDIR(s2)) {
6157c478bd9Sstevel@tonic-gate 				/* existing target dir must be empty */
6167c478bd9Sstevel@tonic-gate 				if (rmdir(target) < 0) {
6177c478bd9Sstevel@tonic-gate 					errno_save = errno;
6187c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
6197c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot rmdir %s: "),
6207c478bd9Sstevel@tonic-gate 					    cmd, target);
6217c478bd9Sstevel@tonic-gate 					errno = errno_save;
6227c478bd9Sstevel@tonic-gate 					perror("");
6237c478bd9Sstevel@tonic-gate 					return (1);
6247c478bd9Sstevel@tonic-gate 				}
6257c478bd9Sstevel@tonic-gate 			}
6267c478bd9Sstevel@tonic-gate #endif
6277c478bd9Sstevel@tonic-gate 			if ((n =  copydir(source, target)) == 0)
6287c478bd9Sstevel@tonic-gate 				(void) rmdir(source);
6297c478bd9Sstevel@tonic-gate 			return (n);
6307c478bd9Sstevel@tonic-gate 		}
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 		/* doors can't be moved across filesystems */
6337c478bd9Sstevel@tonic-gate 		if (ISDOOR(s1)) {
6347c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
6357c478bd9Sstevel@tonic-gate 			    gettext("%s: %s: can't move door "
6367c478bd9Sstevel@tonic-gate 			    "across file systems\n"), cmd, source);
6377c478bd9Sstevel@tonic-gate 			return (1);
6387c478bd9Sstevel@tonic-gate 		}
6397c478bd9Sstevel@tonic-gate 		/*
6407c478bd9Sstevel@tonic-gate 		 * File can't be renamed, try to recreate the symbolic
6417c478bd9Sstevel@tonic-gate 		 * link or special device, or copy the file wholesale
6427c478bd9Sstevel@tonic-gate 		 * between file systems.
6437c478bd9Sstevel@tonic-gate 		 */
6447c478bd9Sstevel@tonic-gate 		if (ISLNK(s1)) {
6457c478bd9Sstevel@tonic-gate 			register int	m;
6467c478bd9Sstevel@tonic-gate 			register mode_t md;
6477c478bd9Sstevel@tonic-gate 			char symln[PATH_MAX + 1];
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 			if (targetexists && unlink(target) < 0) {
6507c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6517c478bd9Sstevel@tonic-gate 				    gettext("%s: cannot unlink %s: "),
6527c478bd9Sstevel@tonic-gate 				    cmd, target);
6537c478bd9Sstevel@tonic-gate 				perror("");
6547c478bd9Sstevel@tonic-gate 				return (1);
6557c478bd9Sstevel@tonic-gate 			}
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 			if ((m = readlink(source, symln,
6587c478bd9Sstevel@tonic-gate 			    sizeof (symln) - 1)) < 0) {
6597c478bd9Sstevel@tonic-gate 				Perror(source);
6607c478bd9Sstevel@tonic-gate 				return (1);
6617c478bd9Sstevel@tonic-gate 			}
6627c478bd9Sstevel@tonic-gate 			symln[m] = '\0';
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 			md = umask(~(s1.st_mode & MODEBITS));
6657c478bd9Sstevel@tonic-gate 			if (symlink(symln, target) < 0) {
6667c478bd9Sstevel@tonic-gate 				Perror(target);
6677c478bd9Sstevel@tonic-gate 				return (1);
6687c478bd9Sstevel@tonic-gate 			}
6697c478bd9Sstevel@tonic-gate 			(void) umask(md);
6707c478bd9Sstevel@tonic-gate 			m = lchown(target, UID(s1), GID(s1));
6717c478bd9Sstevel@tonic-gate #ifdef XPG4
6727c478bd9Sstevel@tonic-gate 			if (m < 0) {
6737c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: cannot"
6747c478bd9Sstevel@tonic-gate 				    " change owner and group of"
6757c478bd9Sstevel@tonic-gate 				    " %s: "), cmd, target);
6767c478bd9Sstevel@tonic-gate 				perror("");
6777c478bd9Sstevel@tonic-gate 			}
6787c478bd9Sstevel@tonic-gate #endif
6797c478bd9Sstevel@tonic-gate 			goto cleanup;
6807c478bd9Sstevel@tonic-gate 		}
6817c478bd9Sstevel@tonic-gate 		if (ISDEV(s1)) {
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 			if (targetexists && unlink(target) < 0) {
6847c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6857c478bd9Sstevel@tonic-gate 				    gettext("%s: cannot unlink %s: "),
6867c478bd9Sstevel@tonic-gate 				    cmd, target);
6877c478bd9Sstevel@tonic-gate 				perror("");
6887c478bd9Sstevel@tonic-gate 				return (1);
6897c478bd9Sstevel@tonic-gate 			}
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 			if (mknod(target, s1.st_mode, s1.st_rdev) < 0) {
6927c478bd9Sstevel@tonic-gate 				Perror(target);
6937c478bd9Sstevel@tonic-gate 				return (1);
6947c478bd9Sstevel@tonic-gate 			}
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 			(void) chg_mode(target, UID(s1), GID(s1), FMODE(s1));
6977c478bd9Sstevel@tonic-gate 			(void) chg_time(target, s1);
6987c478bd9Sstevel@tonic-gate 			goto cleanup;
6997c478bd9Sstevel@tonic-gate 		}
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 		if (ISREG(s1)) {
7027c478bd9Sstevel@tonic-gate 			if (ISDIR(s2)) {
7037c478bd9Sstevel@tonic-gate 				if (targetexists && rmdir(target) < 0) {
7047c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
7057c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot rmdir %s: "),
7067c478bd9Sstevel@tonic-gate 					    cmd, target);
7077c478bd9Sstevel@tonic-gate 					perror("");
7087c478bd9Sstevel@tonic-gate 					return (1);
7097c478bd9Sstevel@tonic-gate 				}
7107c478bd9Sstevel@tonic-gate 			} else {
7117c478bd9Sstevel@tonic-gate 				if (targetexists && unlink(target) < 0) {
7127c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
7137c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot unlink %s: "),
7147c478bd9Sstevel@tonic-gate 					    cmd, target);
7157c478bd9Sstevel@tonic-gate 					perror("");
7167c478bd9Sstevel@tonic-gate 					return (1);
7177c478bd9Sstevel@tonic-gate 				}
7187c478bd9Sstevel@tonic-gate 			}
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate copy:
7227c478bd9Sstevel@tonic-gate 			/*
7237c478bd9Sstevel@tonic-gate 			 * If the source file is a symlink, and either
7247c478bd9Sstevel@tonic-gate 			 * -P or -H flag (only if -H is specified and the
7257c478bd9Sstevel@tonic-gate 			 * source file is not a command line argument)
7267c478bd9Sstevel@tonic-gate 			 * were specified, then action is taken on the symlink
7277c478bd9Sstevel@tonic-gate 			 * itself, not the file referenced by the symlink.
7287c478bd9Sstevel@tonic-gate 			 * Note: this is executed for 'cp' only.
7297c478bd9Sstevel@tonic-gate 			 */
7307c478bd9Sstevel@tonic-gate 			if (cpy && (Pflg || (Hflg && !cmdarg)) && (ISLNK(s1))) {
7317c478bd9Sstevel@tonic-gate 				int	m;
7327c478bd9Sstevel@tonic-gate 				mode_t	md;
7337c478bd9Sstevel@tonic-gate 				char symln[PATH_MAX + 1];
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 				m = readlink(source, symln, sizeof (symln) - 1);
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 				if (m < 0) {
7387c478bd9Sstevel@tonic-gate 					Perror(source);
7397c478bd9Sstevel@tonic-gate 					return (1);
7407c478bd9Sstevel@tonic-gate 				}
7417c478bd9Sstevel@tonic-gate 				symln[m] = '\0';
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 				/*
7447c478bd9Sstevel@tonic-gate 				 * Copy the sym link to the target.
7457c478bd9Sstevel@tonic-gate 				 * Note: If the target exists, write a
7467c478bd9Sstevel@tonic-gate 				 * diagnostic message, do nothing more
7477c478bd9Sstevel@tonic-gate 				 * with the source file, and return to
7487c478bd9Sstevel@tonic-gate 				 * process any remaining files.
7497c478bd9Sstevel@tonic-gate 				 */
7507c478bd9Sstevel@tonic-gate 				md = umask(~(s1.st_mode & MODEBITS));
7517c478bd9Sstevel@tonic-gate 				if (symlink(symln, target) < 0) {
7527c478bd9Sstevel@tonic-gate 					Perror(target);
7537c478bd9Sstevel@tonic-gate 					return (1);
7547c478bd9Sstevel@tonic-gate 				}
7557c478bd9Sstevel@tonic-gate 				(void) umask(md);
7567c478bd9Sstevel@tonic-gate 				m = lchown(target, UID(s1), GID(s1));
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 				if (m < 0) {
7597c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
7607c478bd9Sstevel@tonic-gate 					    "cp: cannot change owner and "
7617c478bd9Sstevel@tonic-gate 					    "group of %s:"), target);
7627c478bd9Sstevel@tonic-gate 					perror("");
7637c478bd9Sstevel@tonic-gate 				}
7647c478bd9Sstevel@tonic-gate 			} else {
7657c478bd9Sstevel@tonic-gate 				/*
7667c478bd9Sstevel@tonic-gate 				 * Copy the file.  If it happens to be a
7677c478bd9Sstevel@tonic-gate 				 * symlink, copy the file referenced
7687c478bd9Sstevel@tonic-gate 				 * by the symlink.
7697c478bd9Sstevel@tonic-gate 				 */
7707c478bd9Sstevel@tonic-gate 				fi = open(source, O_RDONLY);
7717c478bd9Sstevel@tonic-gate 				if (fi < 0) {
7727c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
7737c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot open %s: "),
7747c478bd9Sstevel@tonic-gate 					    cmd, source);
7757c478bd9Sstevel@tonic-gate 					perror("");
7767c478bd9Sstevel@tonic-gate 					return (1);
7777c478bd9Sstevel@tonic-gate 				}
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 				fo = creat(target, s1.st_mode & MODEBITS);
7807c478bd9Sstevel@tonic-gate 				if (fo < 0) {
7817c478bd9Sstevel@tonic-gate 					/*
7827c478bd9Sstevel@tonic-gate 					 * If -f and creat() failed, unlink
7837c478bd9Sstevel@tonic-gate 					 * and try again.
7847c478bd9Sstevel@tonic-gate 					 */
7857c478bd9Sstevel@tonic-gate 					if (fflg) {
7867c478bd9Sstevel@tonic-gate 						(void) unlink(target);
7877c478bd9Sstevel@tonic-gate 						fo = creat(target,
7887c478bd9Sstevel@tonic-gate 						    s1.st_mode & MODEBITS);
7897c478bd9Sstevel@tonic-gate 					}
7907c478bd9Sstevel@tonic-gate 				}
7917c478bd9Sstevel@tonic-gate 				if (fo < 0) {
7927c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
7937c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot create %s: "),
7947c478bd9Sstevel@tonic-gate 					    cmd, target);
7957c478bd9Sstevel@tonic-gate 					perror("");
7967c478bd9Sstevel@tonic-gate 					(void) close(fi);
7977c478bd9Sstevel@tonic-gate 					return (1);
7987c478bd9Sstevel@tonic-gate 				} else {
7997c478bd9Sstevel@tonic-gate 					/* stat the new file, its used below */
8007c478bd9Sstevel@tonic-gate 					(void) stat(target, &s2);
8017c478bd9Sstevel@tonic-gate 				}
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 				/*
8047c478bd9Sstevel@tonic-gate 				 * Set target's permissions to the source
8057c478bd9Sstevel@tonic-gate 				 * before any copying so that any partially
8067c478bd9Sstevel@tonic-gate 				 * copied file will have the source's
8077c478bd9Sstevel@tonic-gate 				 * permissions (at most) or umask permissions
8087c478bd9Sstevel@tonic-gate 				 * whichever is the most restrictive.
8097c478bd9Sstevel@tonic-gate 				 *
8107c478bd9Sstevel@tonic-gate 				 * ACL for regular files
8117c478bd9Sstevel@tonic-gate 				 */
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 				if (pflg || mve) {
8147c478bd9Sstevel@tonic-gate 					(void) chmod(target, FMODE(s1));
815fa9e4066Sahrens 					if (s1acl != NULL) {
816fa9e4066Sahrens 						if ((acl_set(target,
817fa9e4066Sahrens 						    s1acl)) < 0) {
8187c478bd9Sstevel@tonic-gate 							if (pflg || mve) {
8197c478bd9Sstevel@tonic-gate 								(void) fprintf(
8207c478bd9Sstevel@tonic-gate 								    stderr,
8217c478bd9Sstevel@tonic-gate 					"%s: failed to set acl entries on %s\n",
8227c478bd9Sstevel@tonic-gate 								    cmd,
8237c478bd9Sstevel@tonic-gate 								    target);
8247c478bd9Sstevel@tonic-gate 							}
82594d2b9abSmarks 							acl_free(s1acl);
82694d2b9abSmarks 							s1acl = NULL;
8277c478bd9Sstevel@tonic-gate 							/*
8287c478bd9Sstevel@tonic-gate 							 * else: silent and
8297c478bd9Sstevel@tonic-gate 							 * continue
8307c478bd9Sstevel@tonic-gate 							 */
8317c478bd9Sstevel@tonic-gate 						}
8327c478bd9Sstevel@tonic-gate 					}
8337c478bd9Sstevel@tonic-gate 				}
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 				if (fstat(fi, &s1) < 0) {
8367c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
8377c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot access %s\n"),
8387c478bd9Sstevel@tonic-gate 					    cmd, source);
8397c478bd9Sstevel@tonic-gate 					return (1);
8407c478bd9Sstevel@tonic-gate 				}
8417c478bd9Sstevel@tonic-gate 				if (IDENTICAL(s1, s2)) {
8427c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
8437c478bd9Sstevel@tonic-gate 					    gettext(
8447c478bd9Sstevel@tonic-gate 					    "%s: %s and %s are identical\n"),
8457c478bd9Sstevel@tonic-gate 					    cmd, source, target);
8467c478bd9Sstevel@tonic-gate 					return (1);
8477c478bd9Sstevel@tonic-gate 				}
8487c478bd9Sstevel@tonic-gate 
849*da6c28aaSamw 				if (writefile(fi, fo, source, target, NULL,
850*da6c28aaSamw 				    NULL, &s1, &s2) != 0) {
8517c478bd9Sstevel@tonic-gate 					return (1);
8527c478bd9Sstevel@tonic-gate 				}
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 				(void) close(fi);
8557c478bd9Sstevel@tonic-gate 				if (close(fo) < 0) {
8567c478bd9Sstevel@tonic-gate 					Perror2(target, "write");
8577c478bd9Sstevel@tonic-gate 					return (1);
8587c478bd9Sstevel@tonic-gate 				}
8597c478bd9Sstevel@tonic-gate 			}
860*da6c28aaSamw 			/* Copy regular extended attributes */
861*da6c28aaSamw 			if (pflg || atflg || mve || saflg) {
8627c478bd9Sstevel@tonic-gate 				attret = copyattributes(source, target);
8637c478bd9Sstevel@tonic-gate 				if (attret != 0 && !attrsilent) {
8647c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
8653d63ea05Sas 					    "%s: Failed to preserve"
8663d63ea05Sas 					    " extended attributes of file"
8673d63ea05Sas 					    " %s\n"), cmd, source);
8687c478bd9Sstevel@tonic-gate 				}
869*da6c28aaSamw 				/* Copy extended system attributes */
870*da6c28aaSamw 				if (pflg || mve || saflg) {
871*da6c28aaSamw 					sattret = copy_sysattr(source, target);
872*da6c28aaSamw 					if (sattret != 0 && !attrsilent) {
873*da6c28aaSamw 						(void) fprintf(stderr, gettext(
874*da6c28aaSamw 						    "%s: Failed to preserve "
875*da6c28aaSamw 						    "extended system "
876*da6c28aaSamw 						    "attributes of file "
877*da6c28aaSamw 						    "%s\n"), cmd, source);
878*da6c28aaSamw 					}
879*da6c28aaSamw 				}
8807c478bd9Sstevel@tonic-gate 				if (mve && attret != 0) {
8817c478bd9Sstevel@tonic-gate 					(void) unlink(target);
8827c478bd9Sstevel@tonic-gate 					return (1);
8837c478bd9Sstevel@tonic-gate 				}
884*da6c28aaSamw 				if (attrsilent) {
8857c478bd9Sstevel@tonic-gate 					attret = 0;
886*da6c28aaSamw 					sattret = 0;
887*da6c28aaSamw 				}
8887c478bd9Sstevel@tonic-gate 			}
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 			/*
8917c478bd9Sstevel@tonic-gate 			 * XPG4: the write system call will clear setgid
8927c478bd9Sstevel@tonic-gate 			 * and setuid bits, so set them again.
8937c478bd9Sstevel@tonic-gate 			 */
8947c478bd9Sstevel@tonic-gate 			if (pflg || mve) {
8957c478bd9Sstevel@tonic-gate 				if ((ret = chg_mode(target, UID(s1), GID(s1),
8967c478bd9Sstevel@tonic-gate 				    FMODE(s1))) > 0)
8977c478bd9Sstevel@tonic-gate 					return (1);
898d2443e76Smarks 				/*
899d2443e76Smarks 				 * Reapply ACL, since chmod may have
900d2443e76Smarks 				 * altered ACL
901d2443e76Smarks 				 */
902d2443e76Smarks 				if (s1acl != NULL) {
903d2443e76Smarks 					if ((acl_set(target, s1acl)) < 0) {
904d2443e76Smarks 						if (pflg || mve) {
905d2443e76Smarks 							(void) fprintf(
906d2443e76Smarks 							    stderr,
907d2443e76Smarks 					"%s: failed to set acl entries on %s\n",
908d2443e76Smarks 							    cmd,
909d2443e76Smarks 							    target);
910d2443e76Smarks 						}
911d2443e76Smarks 						/*
912d2443e76Smarks 						 * else: silent and
913d2443e76Smarks 						 * continue
914d2443e76Smarks 						 */
915d2443e76Smarks 					}
916d2443e76Smarks 				}
9177c478bd9Sstevel@tonic-gate 				if ((ret = chg_time(target, s1)) > 0)
9187c478bd9Sstevel@tonic-gate 					return (1);
9197c478bd9Sstevel@tonic-gate 			}
9207c478bd9Sstevel@tonic-gate 			if (cpy) {
9217c478bd9Sstevel@tonic-gate 				if (attret != 0)
9227c478bd9Sstevel@tonic-gate 					return (1);
9237c478bd9Sstevel@tonic-gate 				return (0);
9247c478bd9Sstevel@tonic-gate 			}
9257c478bd9Sstevel@tonic-gate 			goto cleanup;
9267c478bd9Sstevel@tonic-gate 		}
9277c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
9287c478bd9Sstevel@tonic-gate 		    gettext("%s: %s: unknown file type 0x%x\n"), cmd,
9293d63ea05Sas 		    source, (s1.st_mode & S_IFMT));
9307c478bd9Sstevel@tonic-gate 		return (1);
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate cleanup:
9337c478bd9Sstevel@tonic-gate 		if (unlink(source) < 0) {
9347c478bd9Sstevel@tonic-gate 			(void) unlink(target);
9357c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9367c478bd9Sstevel@tonic-gate 			    gettext("%s: cannot unlink %s: "),
9377c478bd9Sstevel@tonic-gate 			    cmd, source);
9387c478bd9Sstevel@tonic-gate 			perror("");
9397c478bd9Sstevel@tonic-gate 			return (1);
9407c478bd9Sstevel@tonic-gate 		}
9417c478bd9Sstevel@tonic-gate 		if (attret != 0)
9427c478bd9Sstevel@tonic-gate 			return (attret);
9437c478bd9Sstevel@tonic-gate 		return (ret);
9447c478bd9Sstevel@tonic-gate 	}
9457c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
946a77d64afScf 	return (ret);
9477c478bd9Sstevel@tonic-gate }
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate /*
9507c478bd9Sstevel@tonic-gate  * create_tnode()
9517c478bd9Sstevel@tonic-gate  *
9527c478bd9Sstevel@tonic-gate  * Create a node for use with the search tree which contains the
9537c478bd9Sstevel@tonic-gate  * inode information (device id and inode number).
9547c478bd9Sstevel@tonic-gate  *
9557c478bd9Sstevel@tonic-gate  * Input
9567c478bd9Sstevel@tonic-gate  *	dev	- device id
9577c478bd9Sstevel@tonic-gate  *	ino	- inode number
9587c478bd9Sstevel@tonic-gate  *
9597c478bd9Sstevel@tonic-gate  * Output
9607c478bd9Sstevel@tonic-gate  *	tnode	- NULL on error, otherwise returns a tnode structure
9617c478bd9Sstevel@tonic-gate  *		  which contains the input device id and inode number.
9627c478bd9Sstevel@tonic-gate  */
9637c478bd9Sstevel@tonic-gate static tree_node_t *
9647c478bd9Sstevel@tonic-gate create_tnode(dev_t dev, ino_t ino)
9657c478bd9Sstevel@tonic-gate {
9667c478bd9Sstevel@tonic-gate 	tree_node_t	*tnode;
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	if ((tnode = (tree_node_t *)malloc(sizeof (tree_node_t))) != NULL) {
9697c478bd9Sstevel@tonic-gate 		tnode->node_dev = dev;
9707c478bd9Sstevel@tonic-gate 		tnode->node_ino = ino;
9717c478bd9Sstevel@tonic-gate 	}
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	return (tnode);
9747c478bd9Sstevel@tonic-gate }
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate static int
9777c478bd9Sstevel@tonic-gate chkfiles(char *source, char **to)
9787c478bd9Sstevel@tonic-gate {
9797c478bd9Sstevel@tonic-gate 	char	*buf = (char *)NULL;
9807c478bd9Sstevel@tonic-gate 	int	(*statf)() = (cpy &&
9813d63ea05Sas 	    !(Pflg || (Hflg && !cmdarg))) ? stat : lstat;
9827c478bd9Sstevel@tonic-gate 	char    *target = *to;
983fa9e4066Sahrens 	int	error;
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	/*
9867c478bd9Sstevel@tonic-gate 	 * Make sure source file exists.
9877c478bd9Sstevel@tonic-gate 	 */
9887c478bd9Sstevel@tonic-gate 	if ((*statf)(source, &s1) < 0) {
9897c478bd9Sstevel@tonic-gate 		/*
9907c478bd9Sstevel@tonic-gate 		 * Keep the old error message except when someone tries to
9917c478bd9Sstevel@tonic-gate 		 * mv/cp/ln a symbolic link that has a trailing slash and
9927c478bd9Sstevel@tonic-gate 		 * points to a file.
9937c478bd9Sstevel@tonic-gate 		 */
9947c478bd9Sstevel@tonic-gate 		if (errno == ENOTDIR)
9957c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s: %s\n", cmd, source,
9967c478bd9Sstevel@tonic-gate 			    strerror(errno));
9977c478bd9Sstevel@tonic-gate 		else
9987c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9997c478bd9Sstevel@tonic-gate 			    gettext("%s: cannot access %s\n"), cmd, source);
10007c478bd9Sstevel@tonic-gate 		return (1);
10017c478bd9Sstevel@tonic-gate 	}
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 	/*
10047c478bd9Sstevel@tonic-gate 	 * Get ACL info: don't bother with ln or mv'ing symlinks
10057c478bd9Sstevel@tonic-gate 	 */
10067c478bd9Sstevel@tonic-gate 	if ((!lnk) && !(mve && ISLNK(s1))) {
1007fa9e4066Sahrens 		if (s1acl != NULL) {
1008fa9e4066Sahrens 			acl_free(s1acl);
1009fa9e4066Sahrens 			s1acl = NULL;
10107c478bd9Sstevel@tonic-gate 		}
1011fa9e4066Sahrens 		if ((error = acl_get(source, ACL_NO_TRIVIAL, &s1acl)) != 0) {
10127c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1013fa9e4066Sahrens 			    "%s: failed to get acl entries: %s\n", source,
1014fa9e4066Sahrens 			    acl_strerror(error));
10157c478bd9Sstevel@tonic-gate 			return (1);
10167c478bd9Sstevel@tonic-gate 		}
10177c478bd9Sstevel@tonic-gate 		/* else: just permission bits */
10187c478bd9Sstevel@tonic-gate 	}
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	/*
10217c478bd9Sstevel@tonic-gate 	 * If stat fails, then the target doesn't exist,
10227c478bd9Sstevel@tonic-gate 	 * we will create a new target with default file type of regular.
10237c478bd9Sstevel@tonic-gate 	 */
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	FTYPE(s2) = S_IFREG;
10267c478bd9Sstevel@tonic-gate 	targetexists = 0;
10277c478bd9Sstevel@tonic-gate 	if ((*statf)(target, &s2) >= 0) {
10287c478bd9Sstevel@tonic-gate 		if (ISLNK(s2))
10297c478bd9Sstevel@tonic-gate 			(void) stat(target, &s2);
10307c478bd9Sstevel@tonic-gate 		/*
10317c478bd9Sstevel@tonic-gate 		 * If target is a directory,
10327c478bd9Sstevel@tonic-gate 		 * make complete name of new file
10337c478bd9Sstevel@tonic-gate 		 * within that directory.
10347c478bd9Sstevel@tonic-gate 		 */
10357c478bd9Sstevel@tonic-gate 		if (ISDIR(s2)) {
10367c478bd9Sstevel@tonic-gate 			size_t len;
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 			len = strlen(target) + strlen(dname(source)) + 4;
10397c478bd9Sstevel@tonic-gate 			if ((buf = (char *)malloc(len)) == NULL) {
10407c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10417c478bd9Sstevel@tonic-gate 				    gettext("%s: Insufficient memory to "
10423d63ea05Sas 				    "%s %s\n "), cmd, cmd, source);
10437c478bd9Sstevel@tonic-gate 				exit(3);
10447c478bd9Sstevel@tonic-gate 			}
10457c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, len, "%s/%s",
10467c478bd9Sstevel@tonic-gate 			    target, dname(source));
10477c478bd9Sstevel@tonic-gate 			*to = target = buf;
10487c478bd9Sstevel@tonic-gate 		}
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 		if ((*statf)(target, &s2) >= 0) {
10517c478bd9Sstevel@tonic-gate 			int overwrite	= FALSE;
10527c478bd9Sstevel@tonic-gate 			int override	= FALSE;
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 			targetexists++;
10557c478bd9Sstevel@tonic-gate 			if (cpy || mve) {
10567c478bd9Sstevel@tonic-gate 				/*
10577c478bd9Sstevel@tonic-gate 				 * For cp and mv, it is an error if the
10587c478bd9Sstevel@tonic-gate 				 * source and target are the same file.
10597c478bd9Sstevel@tonic-gate 				 * Check for the same inode and file
10607c478bd9Sstevel@tonic-gate 				 * system, but don't check for the same
10617c478bd9Sstevel@tonic-gate 				 * absolute pathname because it is an
10627c478bd9Sstevel@tonic-gate 				 * error when the source and target are
10637c478bd9Sstevel@tonic-gate 				 * hard links to the same file.
10647c478bd9Sstevel@tonic-gate 				 */
10657c478bd9Sstevel@tonic-gate 				if (IDENTICAL(s1, s2)) {
10667c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
10677c478bd9Sstevel@tonic-gate 					    gettext(
10687c478bd9Sstevel@tonic-gate 					    "%s: %s and %s are identical\n"),
10697c478bd9Sstevel@tonic-gate 					    cmd, source, target);
10707c478bd9Sstevel@tonic-gate 					if (buf != NULL)
10717c478bd9Sstevel@tonic-gate 						free(buf);
10727c478bd9Sstevel@tonic-gate 					return (1);
10737c478bd9Sstevel@tonic-gate 				}
10747c478bd9Sstevel@tonic-gate 			}
10757c478bd9Sstevel@tonic-gate 			if (lnk) {
10767c478bd9Sstevel@tonic-gate 				/*
10777c478bd9Sstevel@tonic-gate 				 * For ln, it is an error if the source and
10787c478bd9Sstevel@tonic-gate 				 * target are identical files (same inode,
10797c478bd9Sstevel@tonic-gate 				 * same file system, and filenames resolve
10807c478bd9Sstevel@tonic-gate 				 * to same absolute pathname).
10817c478bd9Sstevel@tonic-gate 				 */
10827c478bd9Sstevel@tonic-gate 				if (!chk_different(source, target)) {
10837c478bd9Sstevel@tonic-gate 					if (buf != NULL)
10847c478bd9Sstevel@tonic-gate 						free(buf);
10857c478bd9Sstevel@tonic-gate 					return (1);
10867c478bd9Sstevel@tonic-gate 				}
10877c478bd9Sstevel@tonic-gate 			}
10887c478bd9Sstevel@tonic-gate 			if (lnk && !silent) {
10897c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10907c478bd9Sstevel@tonic-gate 				    gettext("%s: %s: File exists\n"),
10917c478bd9Sstevel@tonic-gate 				    cmd, target);
10927c478bd9Sstevel@tonic-gate 				if (buf != NULL)
10937c478bd9Sstevel@tonic-gate 					free(buf);
10947c478bd9Sstevel@tonic-gate 				return (1);
10957c478bd9Sstevel@tonic-gate 			}
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 			/*
10987c478bd9Sstevel@tonic-gate 			 * overwrite:
10997c478bd9Sstevel@tonic-gate 			 * If the user does not have access to
11007c478bd9Sstevel@tonic-gate 			 * the target, ask ----if it is not
11017c478bd9Sstevel@tonic-gate 			 * silent and user invoked command
11027c478bd9Sstevel@tonic-gate 			 * interactively.
11037c478bd9Sstevel@tonic-gate 			 *
11047c478bd9Sstevel@tonic-gate 			 * override:
11057c478bd9Sstevel@tonic-gate 			 * If not silent, and stdin is a terminal, and
11067c478bd9Sstevel@tonic-gate 			 * there's no write access, and the file isn't a
11077c478bd9Sstevel@tonic-gate 			 * symbolic link, ask for permission.
11087c478bd9Sstevel@tonic-gate 			 *
11097c478bd9Sstevel@tonic-gate 			 * XPG4: both overwrite and override:
11107c478bd9Sstevel@tonic-gate 			 * ask only one question.
11117c478bd9Sstevel@tonic-gate 			 *
11127c478bd9Sstevel@tonic-gate 			 * TRANSLATION_NOTE - The following messages will
11137c478bd9Sstevel@tonic-gate 			 * contain the first character of the strings for
11147c478bd9Sstevel@tonic-gate 			 * "yes" and "no" defined in the file
11157c478bd9Sstevel@tonic-gate 			 * "nl_langinfo.po".  After substitution, the
11167c478bd9Sstevel@tonic-gate 			 * message will appear as follows:
11177c478bd9Sstevel@tonic-gate 			 *	<cmd>: overwrite <filename> (y/n)?
11187c478bd9Sstevel@tonic-gate 			 * where <cmd> is the name of the command
11197c478bd9Sstevel@tonic-gate 			 * (cp, mv) and <filename> is the destination file
11207c478bd9Sstevel@tonic-gate 			 */
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 			overwrite = iflg && !silent && use_stdin();
11247c478bd9Sstevel@tonic-gate 			override = !cpy && (access(target, 2) < 0) &&
11257c478bd9Sstevel@tonic-gate 			    !silent && use_stdin() && !ISLNK(s2);
11267c478bd9Sstevel@tonic-gate 
11270f6e7ba6Sas 			if (overwrite && override) {
11287c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
11297c478bd9Sstevel@tonic-gate 				    gettext("%s: overwrite %s and override "
11307c478bd9Sstevel@tonic-gate 				    "protection %o (%s/%s)? "), cmd, target,
11313d63ea05Sas 				    FMODE(s2) & MODEBITS, yesstr, nostr);
11323d63ea05Sas 				if (yes() == 0) {
11330f6e7ba6Sas 					if (buf != NULL)
11340f6e7ba6Sas 						free(buf);
11350f6e7ba6Sas 					return (2);
11360f6e7ba6Sas 				}
11370f6e7ba6Sas 			} else if (overwrite && ISREG(s2)) {
11387c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
11397c478bd9Sstevel@tonic-gate 				    gettext("%s: overwrite %s (%s/%s)? "),
11403d63ea05Sas 				    cmd, target, yesstr, nostr);
11413d63ea05Sas 				if (yes() == 0) {
11420f6e7ba6Sas 					if (buf != NULL)
11430f6e7ba6Sas 						free(buf);
11440f6e7ba6Sas 					return (2);
11450f6e7ba6Sas 				}
11460f6e7ba6Sas 			} else if (override) {
11477c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
11487c478bd9Sstevel@tonic-gate 				    gettext("%s: %s: override protection "
11497c478bd9Sstevel@tonic-gate 				    /*CSTYLED*/
11507c478bd9Sstevel@tonic-gate 				    "%o (%s/%s)? "),
11517c478bd9Sstevel@tonic-gate 				    /*CSTYLED*/
11527c478bd9Sstevel@tonic-gate 				    cmd, target, FMODE(s2) & MODEBITS,
11533d63ea05Sas 				    yesstr, nostr);
11543d63ea05Sas 				if (yes() == 0) {
11550f6e7ba6Sas 					if (buf != NULL)
11560f6e7ba6Sas 						free(buf);
11570f6e7ba6Sas 					return (2);
11587c478bd9Sstevel@tonic-gate 				}
11597c478bd9Sstevel@tonic-gate 			}
11600f6e7ba6Sas 
11617c478bd9Sstevel@tonic-gate 			if (lnk && unlink(target) < 0) {
11627c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
11637c478bd9Sstevel@tonic-gate 				    gettext("%s: cannot unlink %s: "),
11647c478bd9Sstevel@tonic-gate 				    cmd, target);
11657c478bd9Sstevel@tonic-gate 				perror("");
11667c478bd9Sstevel@tonic-gate 				return (1);
11677c478bd9Sstevel@tonic-gate 			}
11687c478bd9Sstevel@tonic-gate 		}
11697c478bd9Sstevel@tonic-gate 	}
11707c478bd9Sstevel@tonic-gate 	return (0);
11717c478bd9Sstevel@tonic-gate }
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate /*
11747c478bd9Sstevel@tonic-gate  * check whether source and target are different
11757c478bd9Sstevel@tonic-gate  * return 1 when they are different
11767c478bd9Sstevel@tonic-gate  * return 0 when they are identical, or when unable to resolve a pathname
11777c478bd9Sstevel@tonic-gate  */
11787c478bd9Sstevel@tonic-gate static int
11797c478bd9Sstevel@tonic-gate chk_different(char *source, char *target)
11807c478bd9Sstevel@tonic-gate {
11817c478bd9Sstevel@tonic-gate 	char	rtarget[PATH_MAX], rsource[PATH_MAX];
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 	if (IDENTICAL(s1, s2)) {
11847c478bd9Sstevel@tonic-gate 		/*
11857c478bd9Sstevel@tonic-gate 		 * IDENTICAL will be true for hard links, therefore
11867c478bd9Sstevel@tonic-gate 		 * check whether the filenames are different
11877c478bd9Sstevel@tonic-gate 		 */
11887c478bd9Sstevel@tonic-gate 		if ((getrealpath(source, rsource) == 0) ||
11897c478bd9Sstevel@tonic-gate 		    (getrealpath(target, rtarget) == 0)) {
11907c478bd9Sstevel@tonic-gate 			return (0);
11917c478bd9Sstevel@tonic-gate 		}
11927c478bd9Sstevel@tonic-gate 		if (strncmp(rsource, rtarget, PATH_MAX) == 0) {
11937c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
11947c478bd9Sstevel@tonic-gate 			    "%s: %s and %s are identical\n"),
11957c478bd9Sstevel@tonic-gate 			    cmd, source, target);
11967c478bd9Sstevel@tonic-gate 			return (0);
11977c478bd9Sstevel@tonic-gate 		}
11987c478bd9Sstevel@tonic-gate 	}
11997c478bd9Sstevel@tonic-gate 	return (1);
12007c478bd9Sstevel@tonic-gate }
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate /*
12037c478bd9Sstevel@tonic-gate  * get real path (resolved absolute pathname)
12047c478bd9Sstevel@tonic-gate  * return 1 on success, 0 on failure
12057c478bd9Sstevel@tonic-gate  */
12067c478bd9Sstevel@tonic-gate static int
12077c478bd9Sstevel@tonic-gate getrealpath(char *path, char *rpath)
12087c478bd9Sstevel@tonic-gate {
12097c478bd9Sstevel@tonic-gate 	if (realpath(path, rpath) == NULL) {
12107c478bd9Sstevel@tonic-gate 		int	errno_save = errno;
12117c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
12127c478bd9Sstevel@tonic-gate 		    "%s: can't resolve path %s: "), cmd, path);
12137c478bd9Sstevel@tonic-gate 		errno = errno_save;
12147c478bd9Sstevel@tonic-gate 		perror("");
12157c478bd9Sstevel@tonic-gate 		return (0);
12167c478bd9Sstevel@tonic-gate 	}
12177c478bd9Sstevel@tonic-gate 	return (1);
12187c478bd9Sstevel@tonic-gate }
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate static int
12217c478bd9Sstevel@tonic-gate rcopy(char *from, char *to)
12227c478bd9Sstevel@tonic-gate {
12237c478bd9Sstevel@tonic-gate 	DIR *fold = opendir(from);
12247c478bd9Sstevel@tonic-gate 	struct dirent *dp;
12257c478bd9Sstevel@tonic-gate 	struct stat statb, s1save;
12267c478bd9Sstevel@tonic-gate 	int errs = 0;
12277c478bd9Sstevel@tonic-gate 	char fromname[PATH_MAX];
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 	if (fold == 0 || ((pflg || mve) && fstat(fold->dd_fd, &statb) < 0)) {
12307c478bd9Sstevel@tonic-gate 		Perror(from);
12317c478bd9Sstevel@tonic-gate 		return (1);
12327c478bd9Sstevel@tonic-gate 	}
12337c478bd9Sstevel@tonic-gate 	if (pflg || mve) {
12347c478bd9Sstevel@tonic-gate 		/*
12357c478bd9Sstevel@tonic-gate 		 * Save s1 (stat information for source dir) so that
12367c478bd9Sstevel@tonic-gate 		 * mod and access times can be reserved during "cp -p"
12377c478bd9Sstevel@tonic-gate 		 * or mv, since s1 gets overwritten.
12387c478bd9Sstevel@tonic-gate 		 */
12397c478bd9Sstevel@tonic-gate 		s1save = s1;
12407c478bd9Sstevel@tonic-gate 	}
12417c478bd9Sstevel@tonic-gate 	for (;;) {
12427c478bd9Sstevel@tonic-gate 		dp = readdir(fold);
12437c478bd9Sstevel@tonic-gate 		if (dp == 0) {
12447c478bd9Sstevel@tonic-gate 			(void) closedir(fold);
12457c478bd9Sstevel@tonic-gate 			if (pflg || mve)
12467c478bd9Sstevel@tonic-gate 				return (chg_time(to, s1save) + errs);
12477c478bd9Sstevel@tonic-gate 			return (errs);
12487c478bd9Sstevel@tonic-gate 		}
12497c478bd9Sstevel@tonic-gate 		if (dp->d_ino == 0)
12507c478bd9Sstevel@tonic-gate 			continue;
12517c478bd9Sstevel@tonic-gate 		if ((strcmp(dp->d_name, ".") == 0) ||
12527c478bd9Sstevel@tonic-gate 		    (strcmp(dp->d_name, "..") == 0))
12537c478bd9Sstevel@tonic-gate 			continue;
12547c478bd9Sstevel@tonic-gate 		if (strlen(from)+1+strlen(dp->d_name) >=
12557c478bd9Sstevel@tonic-gate 		    sizeof (fromname) - 1) {
12567c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
12577c478bd9Sstevel@tonic-gate 			    gettext("%s : %s/%s: Name too long\n"),
12587c478bd9Sstevel@tonic-gate 			    cmd, from, dp->d_name);
12597c478bd9Sstevel@tonic-gate 			errs++;
12607c478bd9Sstevel@tonic-gate 			continue;
12617c478bd9Sstevel@tonic-gate 		}
12627c478bd9Sstevel@tonic-gate 		(void) snprintf(fromname, sizeof (fromname),
12637c478bd9Sstevel@tonic-gate 		    "%s/%s", from, dp->d_name);
12647c478bd9Sstevel@tonic-gate 		errs += cpymve(fromname, to);
12657c478bd9Sstevel@tonic-gate 	}
12667c478bd9Sstevel@tonic-gate }
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate static char *
12697c478bd9Sstevel@tonic-gate dname(char *name)
12707c478bd9Sstevel@tonic-gate {
12717c478bd9Sstevel@tonic-gate 	register char *p;
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate 	/*
12747c478bd9Sstevel@tonic-gate 	 * Return just the file name given the complete path.
12757c478bd9Sstevel@tonic-gate 	 * Like basename(1).
12767c478bd9Sstevel@tonic-gate 	 */
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 	p = name;
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 	/*
12817c478bd9Sstevel@tonic-gate 	 * While there are characters left,
12827c478bd9Sstevel@tonic-gate 	 * set name to start after last
12837c478bd9Sstevel@tonic-gate 	 * delimiter.
12847c478bd9Sstevel@tonic-gate 	 */
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 	while (*p)
12877c478bd9Sstevel@tonic-gate 		if (*p++ == DELIM && *p)
12887c478bd9Sstevel@tonic-gate 			name = p;
12897c478bd9Sstevel@tonic-gate 	return (name);
12907c478bd9Sstevel@tonic-gate }
12917c478bd9Sstevel@tonic-gate 
12927c478bd9Sstevel@tonic-gate static void
12937c478bd9Sstevel@tonic-gate usage(void)
12947c478bd9Sstevel@tonic-gate {
12957c478bd9Sstevel@tonic-gate 	/*
12967c478bd9Sstevel@tonic-gate 	 * Display usage message.
12977c478bd9Sstevel@tonic-gate 	 */
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 	if (mve) {
13007c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
13017c478bd9Sstevel@tonic-gate 		    "Usage: mv [-f] [-i] f1 f2\n"
13027c478bd9Sstevel@tonic-gate 		    "       mv [-f] [-i] f1 ... fn d1\n"
13037c478bd9Sstevel@tonic-gate 		    "       mv [-f] [-i] d1 d2\n"));
13047c478bd9Sstevel@tonic-gate 	} else if (lnk) {
13057c478bd9Sstevel@tonic-gate #ifdef XPG4
13063d63ea05Sas 		(void) fprintf(stderr, gettext(
13077c478bd9Sstevel@tonic-gate 		    "Usage: ln [-f] [-s] f1 [f2]\n"
13087c478bd9Sstevel@tonic-gate 		    "       ln [-f] [-s] f1 ... fn d1\n"
13097c478bd9Sstevel@tonic-gate 		    "       ln [-f] -s d1 d2\n"));
13107c478bd9Sstevel@tonic-gate #else
13113d63ea05Sas 		(void) fprintf(stderr, gettext(
13127c478bd9Sstevel@tonic-gate 		    "Usage: ln [-f] [-n] [-s] f1 [f2]\n"
13137c478bd9Sstevel@tonic-gate 		    "       ln [-f] [-n] [-s] f1 ... fn d1\n"
13147c478bd9Sstevel@tonic-gate 		    "       ln [-f] [-n] -s d1 d2\n"));
13157c478bd9Sstevel@tonic-gate #endif
13167c478bd9Sstevel@tonic-gate 	} else if (cpy) {
13177c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
1318*da6c28aaSamw 		    "Usage: cp [-f] [-i] [-p] [-@] [-/] f1 f2\n"
1319*da6c28aaSamw 		    "       cp [-f] [-i] [-p] [-@] [-/] f1 ... fn d1\n"
1320*da6c28aaSamw 		    "       cp -r|-R [-H|-L|-P] [-f] [-i] [-p] [-@] [-/] "
13217c478bd9Sstevel@tonic-gate 		    "d1 ... dn-1 dn\n"));
13227c478bd9Sstevel@tonic-gate 	}
13237c478bd9Sstevel@tonic-gate 	exit(2);
13247c478bd9Sstevel@tonic-gate }
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate /*
13277c478bd9Sstevel@tonic-gate  * chg_time()
13287c478bd9Sstevel@tonic-gate  *
13297c478bd9Sstevel@tonic-gate  * Try to preserve modification and access time.
13307c478bd9Sstevel@tonic-gate  * If 1) pflg is not set, or 2) pflg is set and this is the Solaris version,
13317c478bd9Sstevel@tonic-gate  * don't report a utime() failure.
13327c478bd9Sstevel@tonic-gate  * If this is the XPG4 version and utime fails, if 1) pflg is set (cp -p)
13337c478bd9Sstevel@tonic-gate  * or 2) we are doing a mv, print a diagnostic message; arrange for a non-zero
13347c478bd9Sstevel@tonic-gate  * exit status only if pflg is set.
13357c478bd9Sstevel@tonic-gate  * utimes(2) is being used to achieve granularity in
13367c478bd9Sstevel@tonic-gate  * microseconds while setting file times.
13377c478bd9Sstevel@tonic-gate  */
13387c478bd9Sstevel@tonic-gate static int
13397c478bd9Sstevel@tonic-gate chg_time(char *to, struct stat ss)
13407c478bd9Sstevel@tonic-gate {
13417c478bd9Sstevel@tonic-gate 	struct timeval times[2];
13427c478bd9Sstevel@tonic-gate 	int rc;
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 	timestruc_to_timeval(&ss.st_atim, times);
13457c478bd9Sstevel@tonic-gate 	timestruc_to_timeval(&ss.st_mtim, times + 1);
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	rc = utimes(to, times);
13487c478bd9Sstevel@tonic-gate #ifdef XPG4
13497c478bd9Sstevel@tonic-gate 	if ((pflg || mve) && rc != 0) {
13507c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13517c478bd9Sstevel@tonic-gate 		    gettext("%s: cannot set times for %s: "), cmd, to);
13527c478bd9Sstevel@tonic-gate 		perror("");
13537c478bd9Sstevel@tonic-gate 		if (pflg)
13547c478bd9Sstevel@tonic-gate 			return (1);
13557c478bd9Sstevel@tonic-gate 	}
13567c478bd9Sstevel@tonic-gate #endif
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 	return (0);
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate }
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate /*
13637c478bd9Sstevel@tonic-gate  * chg_mode()
13647c478bd9Sstevel@tonic-gate  *
13657c478bd9Sstevel@tonic-gate  * This function is called upon "cp -p" or mv across filesystems.
13667c478bd9Sstevel@tonic-gate  *
13677c478bd9Sstevel@tonic-gate  * Try to preserve the owner and group id.  If chown() fails,
13687c478bd9Sstevel@tonic-gate  * only print a diagnostic message if doing a mv in the XPG4 version;
13697c478bd9Sstevel@tonic-gate  * try to clear S_ISUID and S_ISGID bits in the target.  If unable to clear
13707c478bd9Sstevel@tonic-gate  * S_ISUID and S_ISGID bits, print a diagnostic message and arrange for a
13717c478bd9Sstevel@tonic-gate  * non-zero exit status because this is a security violation.
13727c478bd9Sstevel@tonic-gate  * Try to preserve permissions.
13737c478bd9Sstevel@tonic-gate  * If this is the XPG4 version and chmod() fails, print a diagnostic message
13747c478bd9Sstevel@tonic-gate  * and arrange for a non-zero exit status.
13757c478bd9Sstevel@tonic-gate  * If this is the Solaris version and chmod() fails, do not print a
13767c478bd9Sstevel@tonic-gate  * diagnostic message or exit with a non-zero value.
13777c478bd9Sstevel@tonic-gate  */
13787c478bd9Sstevel@tonic-gate static int
13797c478bd9Sstevel@tonic-gate chg_mode(char *target, uid_t uid, gid_t gid, mode_t mode)
13807c478bd9Sstevel@tonic-gate {
13817c478bd9Sstevel@tonic-gate 	int clearflg = 0; /* controls message printed upon chown() error */
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 	if (chown(target, uid, gid) != 0) {
13847c478bd9Sstevel@tonic-gate #ifdef XPG4
13857c478bd9Sstevel@tonic-gate 		if (mve) {
13867c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: cannot change"
13877c478bd9Sstevel@tonic-gate 			    " owner and group of %s: "), cmd, target);
13887c478bd9Sstevel@tonic-gate 			perror("");
13897c478bd9Sstevel@tonic-gate 		}
13907c478bd9Sstevel@tonic-gate #endif
13917c478bd9Sstevel@tonic-gate 		if (mode & (S_ISUID | S_ISGID)) {
13927c478bd9Sstevel@tonic-gate 			/* try to clear S_ISUID and S_ISGID */
13937c478bd9Sstevel@tonic-gate 			mode &= ~S_ISUID & ~S_ISGID;
13947c478bd9Sstevel@tonic-gate 			++clearflg;
13957c478bd9Sstevel@tonic-gate 		}
13967c478bd9Sstevel@tonic-gate 	}
13977c478bd9Sstevel@tonic-gate 	if (chmod(target, mode) != 0) {
13987c478bd9Sstevel@tonic-gate 		if (clearflg) {
13997c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
14007c478bd9Sstevel@tonic-gate 			    "%s: cannot clear S_ISUID and S_ISGID bits in"
14017c478bd9Sstevel@tonic-gate 			    " %s: "), cmd, target);
14027c478bd9Sstevel@tonic-gate 			perror("");
14037c478bd9Sstevel@tonic-gate 			/* cp -p should get non-zero exit; mv should not */
14047c478bd9Sstevel@tonic-gate 			if (pflg)
14057c478bd9Sstevel@tonic-gate 				return (1);
14067c478bd9Sstevel@tonic-gate 		}
14077c478bd9Sstevel@tonic-gate #ifdef XPG4
14087c478bd9Sstevel@tonic-gate 		else {
14097c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
14107c478bd9Sstevel@tonic-gate 			"%s: cannot set permissions for %s: "), cmd, target);
14117c478bd9Sstevel@tonic-gate 			perror("");
14127c478bd9Sstevel@tonic-gate 			/* cp -p should get non-zero exit; mv should not */
14137c478bd9Sstevel@tonic-gate 			if (pflg)
14147c478bd9Sstevel@tonic-gate 				return (1);
14157c478bd9Sstevel@tonic-gate 		}
14167c478bd9Sstevel@tonic-gate #endif
14177c478bd9Sstevel@tonic-gate 	}
14187c478bd9Sstevel@tonic-gate 	return (0);
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate }
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate static void
14237c478bd9Sstevel@tonic-gate Perror(char *s)
14247c478bd9Sstevel@tonic-gate {
14257c478bd9Sstevel@tonic-gate 	char buf[PATH_MAX + 10];
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "%s: %s", cmd, s);
14287c478bd9Sstevel@tonic-gate 	perror(buf);
14297c478bd9Sstevel@tonic-gate }
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate static void
14327c478bd9Sstevel@tonic-gate Perror2(char *s1, char *s2)
14337c478bd9Sstevel@tonic-gate {
14347c478bd9Sstevel@tonic-gate 	char buf[PATH_MAX + 20];
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "%s: %s: %s",
14377c478bd9Sstevel@tonic-gate 	    cmd, gettext(s1), gettext(s2));
14387c478bd9Sstevel@tonic-gate 	perror(buf);
14397c478bd9Sstevel@tonic-gate }
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate /*
14427c478bd9Sstevel@tonic-gate  * used for cp -R and for mv across file systems
14437c478bd9Sstevel@tonic-gate  */
14447c478bd9Sstevel@tonic-gate static int
14457c478bd9Sstevel@tonic-gate copydir(char *source, char *target)
14467c478bd9Sstevel@tonic-gate {
14477c478bd9Sstevel@tonic-gate 	int ret, attret = 0;
14487c478bd9Sstevel@tonic-gate 	int pret = 0;		/* need separate flag if -p is specified */
14497c478bd9Sstevel@tonic-gate 	mode_t	fixmode = (mode_t)0;	/* cleanup mode after copy */
14507c478bd9Sstevel@tonic-gate 	struct stat s1save;
1451fa9e4066Sahrens 	acl_t  *s1acl_save;
1452fa9e4066Sahrens 
1453fa9e4066Sahrens 	s1acl_save = NULL;
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 	if (cpy && !rflg) {
14567c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
14577c478bd9Sstevel@tonic-gate 		    gettext("%s: %s: is a directory\n"), cmd, source);
14587c478bd9Sstevel@tonic-gate 		return (1);
14597c478bd9Sstevel@tonic-gate 	}
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	if (stat(target, &s2) < 0) {
14627c478bd9Sstevel@tonic-gate 		if (mkdir(target, (s1.st_mode & MODEBITS)) < 0) {
14637c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: ", cmd);
14647c478bd9Sstevel@tonic-gate 			perror(target);
14657c478bd9Sstevel@tonic-gate 			return (1);
14667c478bd9Sstevel@tonic-gate 		}
14677c478bd9Sstevel@tonic-gate 		if (stat(target, &s2) == 0) {
14687c478bd9Sstevel@tonic-gate 			fixmode = s2.st_mode;
14697c478bd9Sstevel@tonic-gate 		} else {
14707c478bd9Sstevel@tonic-gate 			fixmode = s1.st_mode;
14717c478bd9Sstevel@tonic-gate 		}
14727c478bd9Sstevel@tonic-gate 		(void) chmod(target, ((fixmode & MODEBITS) | S_IRWXU));
14737c478bd9Sstevel@tonic-gate 	} else if (!(ISDIR(s2))) {
14747c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
14757c478bd9Sstevel@tonic-gate 		    gettext("%s: %s: not a directory.\n"), cmd, target);
14767c478bd9Sstevel@tonic-gate 		return (1);
14777c478bd9Sstevel@tonic-gate 	}
14787c478bd9Sstevel@tonic-gate 	if (pflg || mve) {
14797c478bd9Sstevel@tonic-gate 		/*
14807c478bd9Sstevel@tonic-gate 		 * Save s1 (stat information for source dir) and acl info,
14817c478bd9Sstevel@tonic-gate 		 * if any, so that ownership, modes, times, and acl's can
14827c478bd9Sstevel@tonic-gate 		 * be reserved during "cp -p" or mv.
14837c478bd9Sstevel@tonic-gate 		 * s1 gets overwritten when doing the recursive copy.
14847c478bd9Sstevel@tonic-gate 		 */
14857c478bd9Sstevel@tonic-gate 		s1save = s1;
1486fa9e4066Sahrens 		if (s1acl != NULL) {
1487fa9e4066Sahrens 			s1acl_save = acl_dup(s1acl);
1488fa9e4066Sahrens 			if (s1acl_save == NULL) {
1489fa9e4066Sahrens 				(void) fprintf(stderr, gettext("%s: "
1490fa9e4066Sahrens 				    "Insufficient memory to save acl"
1491fa9e4066Sahrens 				    " entry\n"), cmd);
1492fa9e4066Sahrens 				if (pflg)
1493fa9e4066Sahrens 					return (1);
1494fa9e4066Sahrens 
14957c478bd9Sstevel@tonic-gate 			}
14963d63ea05Sas #ifdef XPG4
14973d63ea05Sas 			else {
14983d63ea05Sas 				(void) fprintf(stderr, gettext("%s: "
14993d63ea05Sas 				    "Insufficient memory to save acl"
15003d63ea05Sas 				    " entry\n"), cmd);
15013d63ea05Sas 				if (pflg)
15023d63ea05Sas 					return (1);
15033d63ea05Sas 			}
15047c478bd9Sstevel@tonic-gate #endif
15057c478bd9Sstevel@tonic-gate 		}
15067c478bd9Sstevel@tonic-gate 	}
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 	ret = rcopy(source, target);
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate 	/*
15117c478bd9Sstevel@tonic-gate 	 * Once we created a directory, go ahead and set
15127c478bd9Sstevel@tonic-gate 	 * its attributes, e.g. acls and time. The info
15137c478bd9Sstevel@tonic-gate 	 * may get overwritten if we continue traversing
15147c478bd9Sstevel@tonic-gate 	 * down the tree.
15157c478bd9Sstevel@tonic-gate 	 *
15167c478bd9Sstevel@tonic-gate 	 * ACL for directory
15177c478bd9Sstevel@tonic-gate 	 */
15187c478bd9Sstevel@tonic-gate 	if (pflg || mve) {
1519d2443e76Smarks 		if ((pret = chg_mode(target, UID(s1save), GID(s1save),
1520d2443e76Smarks 		    FMODE(s1save))) == 0)
1521d2443e76Smarks 			pret = chg_time(target, s1save);
1522d2443e76Smarks 		ret += pret;
1523fa9e4066Sahrens 		if (s1acl_save != NULL) {
1524fa9e4066Sahrens 			if (acl_set(target, s1acl_save) < 0) {
15257c478bd9Sstevel@tonic-gate #ifdef XPG4
15267c478bd9Sstevel@tonic-gate 				if (pflg || mve) {
15277c478bd9Sstevel@tonic-gate #else
15287c478bd9Sstevel@tonic-gate 				if (pflg) {
15297c478bd9Sstevel@tonic-gate #endif
15307c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
15317c478bd9Sstevel@tonic-gate 					    "%s: failed to set acl entries "
15327c478bd9Sstevel@tonic-gate 					    "on %s\n"), cmd, target);
15337c478bd9Sstevel@tonic-gate 					if (pflg) {
1534fa9e4066Sahrens 						acl_free(s1acl_save);
1535fa9e4066Sahrens 						s1acl_save = NULL;
15367c478bd9Sstevel@tonic-gate 						ret++;
15377c478bd9Sstevel@tonic-gate 					}
15387c478bd9Sstevel@tonic-gate 				}
15397c478bd9Sstevel@tonic-gate 				/* else: silent and continue */
15407c478bd9Sstevel@tonic-gate 			}
1541fa9e4066Sahrens 			acl_free(s1acl_save);
1542fa9e4066Sahrens 			s1acl_save = NULL;
15437c478bd9Sstevel@tonic-gate 		}
15447c478bd9Sstevel@tonic-gate 	} else if (fixmode != (mode_t)0)
15457c478bd9Sstevel@tonic-gate 		(void) chmod(target, fixmode & MODEBITS);
15467c478bd9Sstevel@tonic-gate 
1547*da6c28aaSamw 	if (pflg || atflg || mve || saflg) {
15487c478bd9Sstevel@tonic-gate 		attret = copyattributes(source, target);
15497c478bd9Sstevel@tonic-gate 		if (!attrsilent && attret != 0) {
15507c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: Failed to preserve"
15517c478bd9Sstevel@tonic-gate 			    " extended attributes of directory"
15527c478bd9Sstevel@tonic-gate 			    " %s\n"), cmd, source);
15537c478bd9Sstevel@tonic-gate 		} else {
15547c478bd9Sstevel@tonic-gate 			/*
15557c478bd9Sstevel@tonic-gate 			 * Otherwise ignore failure.
15567c478bd9Sstevel@tonic-gate 			 */
15577c478bd9Sstevel@tonic-gate 			attret = 0;
15587c478bd9Sstevel@tonic-gate 		}
1559*da6c28aaSamw 		/* Copy extended system attributes */
1560*da6c28aaSamw 		if (pflg || mve || saflg) {
1561*da6c28aaSamw 			attret = copy_sysattr(source, target);
1562*da6c28aaSamw 			if (attret != 0 && !attrsilent) {
1563*da6c28aaSamw 				(void) fprintf(stderr, gettext(
1564*da6c28aaSamw 				    "%s: Failed to preserve "
1565*da6c28aaSamw 				    "extended system attributes "
1566*da6c28aaSamw 				    "of directory %s\n"), cmd, source);
1567*da6c28aaSamw 			} else {
1568*da6c28aaSamw 				attret = 0;
1569*da6c28aaSamw 			}
1570*da6c28aaSamw 		}
15717c478bd9Sstevel@tonic-gate 	}
15727c478bd9Sstevel@tonic-gate 	if (attret != 0)
15737c478bd9Sstevel@tonic-gate 		return (attret);
15747c478bd9Sstevel@tonic-gate 	return (ret);
15757c478bd9Sstevel@tonic-gate }
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate static int
15787c478bd9Sstevel@tonic-gate copyspecial(char *target)
15797c478bd9Sstevel@tonic-gate {
15807c478bd9Sstevel@tonic-gate 	int ret = 0;
15817c478bd9Sstevel@tonic-gate 
15827c478bd9Sstevel@tonic-gate 	if (mknod(target, s1.st_mode, s1.st_rdev) != 0) {
15837c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
15847c478bd9Sstevel@tonic-gate 		    "cp: cannot create special file %s: "), target);
15857c478bd9Sstevel@tonic-gate 		perror("");
15867c478bd9Sstevel@tonic-gate 		return (1);
15877c478bd9Sstevel@tonic-gate 	}
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate 	if (pflg) {
15907c478bd9Sstevel@tonic-gate 		if ((ret = chg_mode(target, UID(s1), GID(s1), FMODE(s1))) == 0)
15917c478bd9Sstevel@tonic-gate 			ret = chg_time(target, s1);
15927c478bd9Sstevel@tonic-gate 	}
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 	return (ret);
15957c478bd9Sstevel@tonic-gate }
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate static int
15987c478bd9Sstevel@tonic-gate use_stdin(void)
15997c478bd9Sstevel@tonic-gate {
16007c478bd9Sstevel@tonic-gate #ifdef XPG4
16017c478bd9Sstevel@tonic-gate 	return (1);
16027c478bd9Sstevel@tonic-gate #else
16037c478bd9Sstevel@tonic-gate 	return (isatty(fileno(stdin)));
16047c478bd9Sstevel@tonic-gate #endif
16057c478bd9Sstevel@tonic-gate }
16067c478bd9Sstevel@tonic-gate 
1607*da6c28aaSamw /* Copy non-system extended attributes */
1608*da6c28aaSamw 
16097c478bd9Sstevel@tonic-gate static int
16107c478bd9Sstevel@tonic-gate copyattributes(char *source, char *target)
16117c478bd9Sstevel@tonic-gate {
16127c478bd9Sstevel@tonic-gate 	struct dirent *dp;
16137c478bd9Sstevel@tonic-gate 	char *srcbuf, *targbuf;
16147c478bd9Sstevel@tonic-gate 	int error = 0;
1615fa9e4066Sahrens 	int aclerror;
16167c478bd9Sstevel@tonic-gate 	mode_t mode;
16177c478bd9Sstevel@tonic-gate 	int clearflg = 0;
1618fa9e4066Sahrens 	acl_t *xacl = NULL;
1619fa9e4066Sahrens 	acl_t *attrdiracl = NULL;
16207c478bd9Sstevel@tonic-gate 	struct timeval times[2];
16217c478bd9Sstevel@tonic-gate 
16227c478bd9Sstevel@tonic-gate 	srcbuf = targbuf = NULL;
16237c478bd9Sstevel@tonic-gate 
1624*da6c28aaSamw 
1625*da6c28aaSamw 	if (pathconf(source,  _PC_XATTR_EXISTS) != 1)
16267c478bd9Sstevel@tonic-gate 		return (0);
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate 	if (pathconf(target, _PC_XATTR_ENABLED) != 1) {
16297c478bd9Sstevel@tonic-gate 		if (!attrsilent) {
16307c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
16317c478bd9Sstevel@tonic-gate 			    gettext(
16327c478bd9Sstevel@tonic-gate 			    "%s: cannot preserve extended attributes, "
16337c478bd9Sstevel@tonic-gate 			    "operation not supported on file"
16347c478bd9Sstevel@tonic-gate 			    " %s\n"), cmd, target);
16357c478bd9Sstevel@tonic-gate 		}
16367c478bd9Sstevel@tonic-gate 		return (1);
16377c478bd9Sstevel@tonic-gate 	}
1638*da6c28aaSamw 	if (open_source(source) != 0)
1639*da6c28aaSamw 		return (1);
1640*da6c28aaSamw 	if (open_target_srctarg_attrdirs(source, target) !=  0)
1641*da6c28aaSamw 		return (1);
1642*da6c28aaSamw 	if (open_attrdirp(source) != 0)
1643*da6c28aaSamw 		return (1);
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 	if (pflg || mve) {
16467c478bd9Sstevel@tonic-gate 		if (fchmod(targetdirfd, attrdir.st_mode) == -1) {
16477c478bd9Sstevel@tonic-gate 			if (!attrsilent) {
16487c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
16493d63ea05Sas 				    gettext("%s: failed to set file mode"
16503d63ea05Sas 				    " correctly on attribute directory of"
16513d63ea05Sas 				    " file %s: "), cmd, target);
16527c478bd9Sstevel@tonic-gate 				perror("");
16537c478bd9Sstevel@tonic-gate 				++error;
16547c478bd9Sstevel@tonic-gate 			}
16557c478bd9Sstevel@tonic-gate 		}
16567c478bd9Sstevel@tonic-gate 
16577c478bd9Sstevel@tonic-gate 		if (fchown(targetdirfd, attrdir.st_uid, attrdir.st_gid) == -1) {
16587c478bd9Sstevel@tonic-gate 			if (!attrsilent) {
16597c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
16607c478bd9Sstevel@tonic-gate 				    gettext("%s: failed to set file"
16617c478bd9Sstevel@tonic-gate 				    " ownership correctly on attribute"
16627c478bd9Sstevel@tonic-gate 				    " directory of file %s: "), cmd, target);
16637c478bd9Sstevel@tonic-gate 				perror("");
16647c478bd9Sstevel@tonic-gate 				++error;
16657c478bd9Sstevel@tonic-gate 			}
16667c478bd9Sstevel@tonic-gate 		}
16677c478bd9Sstevel@tonic-gate 		/*
16687c478bd9Sstevel@tonic-gate 		 * Now that we are the owner we can update st_ctime by calling
16697c478bd9Sstevel@tonic-gate 		 * futimesat.
16707c478bd9Sstevel@tonic-gate 		 */
16717c478bd9Sstevel@tonic-gate 		times[0].tv_sec = attrdir.st_atime;
16727c478bd9Sstevel@tonic-gate 		times[0].tv_usec = 0;
16737c478bd9Sstevel@tonic-gate 		times[1].tv_sec = attrdir.st_mtime;
16747c478bd9Sstevel@tonic-gate 		times[1].tv_usec = 0;
16757c478bd9Sstevel@tonic-gate 		if (futimesat(targetdirfd, ".", times) < 0) {
16767c478bd9Sstevel@tonic-gate 			if (!attrsilent) {
16777c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
16783d63ea05Sas 				    gettext("%s: cannot set attribute times"
16793d63ea05Sas 				    " for %s: "), cmd, target);
16807c478bd9Sstevel@tonic-gate 				perror("");
16817c478bd9Sstevel@tonic-gate 				++error;
16827c478bd9Sstevel@tonic-gate 			}
16837c478bd9Sstevel@tonic-gate 		}
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate 		/*
16867c478bd9Sstevel@tonic-gate 		 * Now set owner and group of attribute directory, implies
16877c478bd9Sstevel@tonic-gate 		 * changing the ACL of the hidden attribute directory first.
16887c478bd9Sstevel@tonic-gate 		 */
1689fa9e4066Sahrens 		if ((aclerror = facl_get(sourcedirfd,
1690fa9e4066Sahrens 		    ACL_NO_TRIVIAL, &attrdiracl)) != 0) {
16917c478bd9Sstevel@tonic-gate 			if (!attrsilent) {
16927c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
16937c478bd9Sstevel@tonic-gate 				    "%s: failed to get acl entries of"
16947c478bd9Sstevel@tonic-gate 				    " attribute directory for"
1695fa9e4066Sahrens 				    " %s : %s\n"), cmd,
1696fa9e4066Sahrens 				    source, acl_strerror(aclerror));
16977c478bd9Sstevel@tonic-gate 				++error;
16987c478bd9Sstevel@tonic-gate 			}
16997c478bd9Sstevel@tonic-gate 		}
1700fa9e4066Sahrens 
1701fa9e4066Sahrens 		if (attrdiracl) {
1702fa9e4066Sahrens 			if (facl_set(targetdirfd, attrdiracl) != 0) {
17037c478bd9Sstevel@tonic-gate 				if (!attrsilent) {
17047c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
1705fa9e4066Sahrens 					"%s: failed to set acl entries"
1706fa9e4066Sahrens 					" on attribute directory "
1707fa9e4066Sahrens 					"for %s\n"), cmd, target);
17087c478bd9Sstevel@tonic-gate 					++error;
17097c478bd9Sstevel@tonic-gate 				}
1710fa9e4066Sahrens 				acl_free(attrdiracl);
1711fa9e4066Sahrens 				attrdiracl = NULL;
17127c478bd9Sstevel@tonic-gate 			}
17137c478bd9Sstevel@tonic-gate 		}
17147c478bd9Sstevel@tonic-gate 	}
17157c478bd9Sstevel@tonic-gate 
1716*da6c28aaSamw 	while ((dp = readdir(srcdirp)) != NULL) {
1717*da6c28aaSamw 		int ret;
17187c478bd9Sstevel@tonic-gate 
1719*da6c28aaSamw 		if ((ret = traverse_attrfile(dp, source, target, 1)) == -1)
1720*da6c28aaSamw 			continue;
1721*da6c28aaSamw 		else if (ret > 0) {
17227c478bd9Sstevel@tonic-gate 			++error;
17237c478bd9Sstevel@tonic-gate 			goto out;
17247c478bd9Sstevel@tonic-gate 		}
17257c478bd9Sstevel@tonic-gate 
17267c478bd9Sstevel@tonic-gate 		if (pflg || mve) {
1727fa9e4066Sahrens 			if ((aclerror = facl_get(srcattrfd,
1728fa9e4066Sahrens 			    ACL_NO_TRIVIAL, &xacl)) != 0) {
17297c478bd9Sstevel@tonic-gate 				if (!attrsilent) {
17307c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
17317c478bd9Sstevel@tonic-gate 					    "%s: failed to get acl entries of"
17327c478bd9Sstevel@tonic-gate 					    " attribute %s for"
1733fa9e4066Sahrens 					    " %s: %s"), cmd, dp->d_name,
1734fa9e4066Sahrens 					    source, acl_strerror(aclerror));
17357c478bd9Sstevel@tonic-gate 					++error;
17367c478bd9Sstevel@tonic-gate 				}
17377c478bd9Sstevel@tonic-gate 			}
17387c478bd9Sstevel@tonic-gate 		}
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate 		/*
17417c478bd9Sstevel@tonic-gate 		 * preserve ACL
17427c478bd9Sstevel@tonic-gate 		 */
1743fa9e4066Sahrens 		if ((pflg || mve) && xacl != NULL) {
1744fa9e4066Sahrens 			if ((facl_set(targattrfd, xacl)) < 0) {
17457c478bd9Sstevel@tonic-gate 				if (!attrsilent) {
17467c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
17477c478bd9Sstevel@tonic-gate 					    "%s: failed to set acl entries on"
17487c478bd9Sstevel@tonic-gate 					    " attribute %s for"
17497c478bd9Sstevel@tonic-gate 					    "%s\n"), cmd, dp->d_name, target);
17507c478bd9Sstevel@tonic-gate 					++error;
17517c478bd9Sstevel@tonic-gate 				}
1752fa9e4066Sahrens 				acl_free(xacl);
1753fa9e4066Sahrens 				xacl = NULL;
17547c478bd9Sstevel@tonic-gate 			}
17557c478bd9Sstevel@tonic-gate 		}
17567c478bd9Sstevel@tonic-gate 
1757*da6c28aaSamw 		if (writefile(srcattrfd, targattrfd, source, target,
1758*da6c28aaSamw 		    dp->d_name, dp->d_name, &s3, &s4) != 0) {
17597c478bd9Sstevel@tonic-gate 			if (!attrsilent) {
17607c478bd9Sstevel@tonic-gate 				++error;
17617c478bd9Sstevel@tonic-gate 			}
17627c478bd9Sstevel@tonic-gate 			goto next;
17637c478bd9Sstevel@tonic-gate 		}
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate 		if (pflg || mve) {
17667c478bd9Sstevel@tonic-gate 			mode = FMODE(s3);
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate 			if (fchown(targattrfd, UID(s3), GID(s3)) != 0) {
17697c478bd9Sstevel@tonic-gate 				if (!attrsilent) {
17707c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
17717c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot change"
17727c478bd9Sstevel@tonic-gate 					    " owner and group of"
17737c478bd9Sstevel@tonic-gate 					    " attribute %s for" " file"
17747c478bd9Sstevel@tonic-gate 					    " %s: "), cmd, dp->d_name, target);
17757c478bd9Sstevel@tonic-gate 					perror("");
17767c478bd9Sstevel@tonic-gate 					++error;
17777c478bd9Sstevel@tonic-gate 				}
17787c478bd9Sstevel@tonic-gate 				if (mode & (S_ISUID | S_ISGID)) {
17797c478bd9Sstevel@tonic-gate 					/* try to clear S_ISUID and S_ISGID */
17807c478bd9Sstevel@tonic-gate 					mode &= ~S_ISUID & ~S_ISGID;
17817c478bd9Sstevel@tonic-gate 					++clearflg;
17827c478bd9Sstevel@tonic-gate 				}
17837c478bd9Sstevel@tonic-gate 			}
17847c478bd9Sstevel@tonic-gate 			/* tv_usec were cleared above */
17857c478bd9Sstevel@tonic-gate 			times[0].tv_sec = s3.st_atime;
17867c478bd9Sstevel@tonic-gate 			times[1].tv_sec = s3.st_mtime;
17877c478bd9Sstevel@tonic-gate 			if (futimesat(targetdirfd, dp->d_name, times) < 0) {
17887c478bd9Sstevel@tonic-gate 				if (!attrsilent) {
17897c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
17907c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot set attribute"
17917c478bd9Sstevel@tonic-gate 					    " times for %s: "), cmd, target);
17927c478bd9Sstevel@tonic-gate 					perror("");
17937c478bd9Sstevel@tonic-gate 					++error;
17947c478bd9Sstevel@tonic-gate 				}
17957c478bd9Sstevel@tonic-gate 			}
17967c478bd9Sstevel@tonic-gate 			if (fchmod(targattrfd, mode) != 0) {
17977c478bd9Sstevel@tonic-gate 				if (clearflg) {
17987c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
17997c478bd9Sstevel@tonic-gate 					    "%s: cannot clear S_ISUID and "
18007c478bd9Sstevel@tonic-gate 					    "S_ISGID bits in attribute %s"
18017c478bd9Sstevel@tonic-gate 					    " for file"
18027c478bd9Sstevel@tonic-gate 					    " %s: "), cmd, dp->d_name, target);
18037c478bd9Sstevel@tonic-gate 				} else {
18047c478bd9Sstevel@tonic-gate 					if (!attrsilent) {
18057c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
18063d63ea05Sas 						    gettext(
18077c478bd9Sstevel@tonic-gate 				"%s: cannot set permissions of attribute"
18087c478bd9Sstevel@tonic-gate 				" %s for %s: "), cmd, dp->d_name, target);
18097c478bd9Sstevel@tonic-gate 						perror("");
18107c478bd9Sstevel@tonic-gate 						++error;
18117c478bd9Sstevel@tonic-gate 					}
18127c478bd9Sstevel@tonic-gate 				}
18137c478bd9Sstevel@tonic-gate 			}
1814d2443e76Smarks 			if (xacl && ((facl_set(targattrfd, xacl)) < 0)) {
1815d2443e76Smarks 				if (!attrsilent) {
1816d2443e76Smarks 					(void) fprintf(stderr, gettext(
1817d2443e76Smarks 					    "%s: failed to set acl entries on"
1818d2443e76Smarks 					    " attribute %s for"
1819d2443e76Smarks 					    "%s\n"), cmd, dp->d_name, target);
1820d2443e76Smarks 					++error;
1821d2443e76Smarks 				}
1822d2443e76Smarks 				acl_free(xacl);
1823d2443e76Smarks 				xacl = NULL;
1824d2443e76Smarks 			}
18257c478bd9Sstevel@tonic-gate 		}
18267c478bd9Sstevel@tonic-gate next:
1827fa9e4066Sahrens 		if (xacl != NULL) {
1828fa9e4066Sahrens 			acl_free(xacl);
1829fa9e4066Sahrens 			xacl = NULL;
18307c478bd9Sstevel@tonic-gate 		}
18317c478bd9Sstevel@tonic-gate 		if (srcbuf != NULL)
18327c478bd9Sstevel@tonic-gate 			free(srcbuf);
18337c478bd9Sstevel@tonic-gate 		if (targbuf != NULL)
18347c478bd9Sstevel@tonic-gate 			free(targbuf);
18357c478bd9Sstevel@tonic-gate 		if (srcattrfd != -1)
18367c478bd9Sstevel@tonic-gate 			(void) close(srcattrfd);
18377c478bd9Sstevel@tonic-gate 		if (targattrfd != -1)
18387c478bd9Sstevel@tonic-gate 			(void) close(targattrfd);
18397c478bd9Sstevel@tonic-gate 		srcattrfd = targattrfd = -1;
18407c478bd9Sstevel@tonic-gate 		srcbuf = targbuf = NULL;
18417c478bd9Sstevel@tonic-gate 	}
18427c478bd9Sstevel@tonic-gate out:
1843fa9e4066Sahrens 	if (xacl != NULL) {
1844fa9e4066Sahrens 		acl_free(xacl);
1845fa9e4066Sahrens 		xacl = NULL;
1846fa9e4066Sahrens 	}
1847fa9e4066Sahrens 	if (attrdiracl != NULL) {
1848fa9e4066Sahrens 		acl_free(attrdiracl);
1849fa9e4066Sahrens 		attrdiracl = NULL;
1850fa9e4066Sahrens 	}
18517c478bd9Sstevel@tonic-gate 	if (srcbuf)
18527c478bd9Sstevel@tonic-gate 		free(srcbuf);
18537c478bd9Sstevel@tonic-gate 	if (targbuf)
18547c478bd9Sstevel@tonic-gate 		free(targbuf);
1855*da6c28aaSamw 
1856*da6c28aaSamw 	if (!saflg && !pflg && !mve)
1857*da6c28aaSamw 		close_all();
1858*da6c28aaSamw 	return (error == 0 ? 0 : 1);
1859*da6c28aaSamw }
1860*da6c28aaSamw 
1861*da6c28aaSamw /* Copy extended system attributes from source to target */
1862*da6c28aaSamw 
1863*da6c28aaSamw static int
1864*da6c28aaSamw copy_sysattr(char *source, char *target)
1865*da6c28aaSamw {
1866*da6c28aaSamw 	struct dirent	*dp;
1867*da6c28aaSamw 	nvlist_t	*response;
1868*da6c28aaSamw 	int		error = 0;
1869*da6c28aaSamw 	int		chk_support = 0;
1870*da6c28aaSamw 	int		sa_support = 1;
1871*da6c28aaSamw 
1872*da6c28aaSamw 	if (sysattr_support(source, _PC_SATTR_EXISTS) != 1)
1873*da6c28aaSamw 		return (0);
1874*da6c28aaSamw 
1875*da6c28aaSamw 	if (open_source(source) != 0)
1876*da6c28aaSamw 		return (1);
1877*da6c28aaSamw 
1878*da6c28aaSamw 	/*
1879*da6c28aaSamw 	 * Gets non default extended system attributes from the
1880*da6c28aaSamw 	 * source file to copy to the target. The target has
1881*da6c28aaSamw 	 * the defaults set when its created and thus  no need
1882*da6c28aaSamw 	 * to copy the defaults.
1883*da6c28aaSamw 	 */
1884*da6c28aaSamw 	response = sysattr_list(cmd, srcfd, source);
1885*da6c28aaSamw 
1886*da6c28aaSamw 	if (response != NULL &&
1887*da6c28aaSamw 	    sysattr_support(target, _PC_SATTR_ENABLED) != 1) {
1888*da6c28aaSamw 		if (!attrsilent) {
1889*da6c28aaSamw 			(void) fprintf(stderr,
1890*da6c28aaSamw 			    gettext(
1891*da6c28aaSamw 			    "%s: cannot preserve extended system "
1892*da6c28aaSamw 			    "attribute, operation not supported on file"
1893*da6c28aaSamw 			    " %s\n"), cmd, target);
1894*da6c28aaSamw 		}
1895*da6c28aaSamw 		error++;
1896*da6c28aaSamw 		goto out;
1897*da6c28aaSamw 	}
1898*da6c28aaSamw 
1899*da6c28aaSamw 	if (srcdirp == NULL) {
1900*da6c28aaSamw 		if (open_target_srctarg_attrdirs(source, target) !=  0) {
1901*da6c28aaSamw 			error++;
1902*da6c28aaSamw 			goto out;
1903*da6c28aaSamw 		}
1904*da6c28aaSamw 		if (open_attrdirp(source) != 0) {
1905*da6c28aaSamw 			error++;
1906*da6c28aaSamw 			goto out;
1907*da6c28aaSamw 		}
1908*da6c28aaSamw 	} else {
1909*da6c28aaSamw 		rewind_attrdir(srcdirp);
1910*da6c28aaSamw 	}
1911*da6c28aaSamw 	while (sa_support && (dp = readdir(srcdirp)) != NULL) {
1912*da6c28aaSamw 		nvlist_t	*res;
1913*da6c28aaSamw 		int		ret;
1914*da6c28aaSamw 
1915*da6c28aaSamw 		if ((ret = traverse_attrfile(dp, source, target, 0)) == -1)
1916*da6c28aaSamw 			continue;
1917*da6c28aaSamw 		else if (ret > 0) {
1918*da6c28aaSamw 			++error;
1919*da6c28aaSamw 			goto out;
1920*da6c28aaSamw 		}
1921*da6c28aaSamw 		/*
1922*da6c28aaSamw 		 * Gets non default extended system attributes from the
1923*da6c28aaSamw 		 * attribute file to copy to the target. The target has
1924*da6c28aaSamw 		 * the defaults set when its created and thus  no need
1925*da6c28aaSamw 		 * to copy the defaults.
1926*da6c28aaSamw 		 */
1927*da6c28aaSamw 		if (dp->d_name != NULL) {
1928*da6c28aaSamw 			res = sysattr_list(cmd, srcattrfd, dp->d_name);
1929*da6c28aaSamw 			if (res == NULL)
1930*da6c28aaSamw 				goto next;
1931*da6c28aaSamw 
1932*da6c28aaSamw 			if (!chk_support &&
1933*da6c28aaSamw 			    sysattr_support(target, _PC_SATTR_ENABLED) != 1) {
1934*da6c28aaSamw 				if (!attrsilent) {
1935*da6c28aaSamw 					(void) fprintf(stderr,
1936*da6c28aaSamw 					    gettext(
1937*da6c28aaSamw 					    "%s: cannot preserve extended "
1938*da6c28aaSamw 					    "system attribute, operation not "
1939*da6c28aaSamw 					    " %s\n"), cmd, target);
1940*da6c28aaSamw 				}
1941*da6c28aaSamw 				error++;
1942*da6c28aaSamw 				sa_support = 0;
1943*da6c28aaSamw 			} else {
1944*da6c28aaSamw 				chk_support = 1;
1945*da6c28aaSamw 			}
1946*da6c28aaSamw 
1947*da6c28aaSamw 			/*
1948*da6c28aaSamw 			 * Copy non default extended system attributes of named
1949*da6c28aaSamw 			 * attribute file.
1950*da6c28aaSamw 			 */
1951*da6c28aaSamw 			if (sa_support && fsetattr(targattrfd,
1952*da6c28aaSamw 			    XATTR_VIEW_READWRITE, res) != 0) {
1953*da6c28aaSamw 				++error;
1954*da6c28aaSamw 				if (!attrsilent) {
1955*da6c28aaSamw 					(void) fprintf(stderr, gettext("%s: "
1956*da6c28aaSamw 					    "Failed to copy extended system "
1957*da6c28aaSamw 					    "attributes from attribute file "
1958*da6c28aaSamw 					    "%s of %s to %s\n"), cmd,
1959*da6c28aaSamw 					    dp->d_name, source, target);
1960*da6c28aaSamw 				}
1961*da6c28aaSamw 			}
1962*da6c28aaSamw 		}
1963*da6c28aaSamw next:
1964*da6c28aaSamw 		if (srcattrfd != -1)
1965*da6c28aaSamw 			(void) close(srcattrfd);
1966*da6c28aaSamw 		if (targattrfd != -1)
1967*da6c28aaSamw 			(void) close(targattrfd);
1968*da6c28aaSamw 		srcattrfd = targattrfd = -1;
1969*da6c28aaSamw 		if (res != NULL)
1970*da6c28aaSamw 			nvlist_free(res);
1971*da6c28aaSamw 	}
1972*da6c28aaSamw 
1973*da6c28aaSamw 	/* Copy source file non default extended system attributes to target */
1974*da6c28aaSamw 	if ((response != NULL) &&
1975*da6c28aaSamw 	    (fsetattr(targfd, XATTR_VIEW_READWRITE, response)) != 0) {
1976*da6c28aaSamw 		++error;
1977*da6c28aaSamw 		if (!attrsilent) {
1978*da6c28aaSamw 			(void) fprintf(stderr, gettext("%s: Failed to "
1979*da6c28aaSamw 			    "copy extended system attributes from "
1980*da6c28aaSamw 			    "%s to %s\n"), cmd, source, target);
1981*da6c28aaSamw 		}
1982*da6c28aaSamw 	}
1983*da6c28aaSamw out:
1984*da6c28aaSamw 	if (response != NULL)
1985*da6c28aaSamw 		nvlist_free(response);
1986*da6c28aaSamw 	close_all();
19877c478bd9Sstevel@tonic-gate 	return (error == 0 ? 0 : 1);
19887c478bd9Sstevel@tonic-gate }
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate /*
19917c478bd9Sstevel@tonic-gate  * nanoseconds are rounded off to microseconds by flooring.
19927c478bd9Sstevel@tonic-gate  */
19937c478bd9Sstevel@tonic-gate static void
19947c478bd9Sstevel@tonic-gate timestruc_to_timeval(timestruc_t *ts, struct timeval *tv)
19957c478bd9Sstevel@tonic-gate {
19967c478bd9Sstevel@tonic-gate 	tv->tv_sec = ts->tv_sec;
19977c478bd9Sstevel@tonic-gate 	tv->tv_usec = ts->tv_nsec / 1000;
19987c478bd9Sstevel@tonic-gate }
1999*da6c28aaSamw 
2000*da6c28aaSamw /* Open the source file */
2001*da6c28aaSamw 
2002*da6c28aaSamw int
2003*da6c28aaSamw open_source(char  *src)
2004*da6c28aaSamw {
2005*da6c28aaSamw 	int	error = 0;
2006*da6c28aaSamw 
2007*da6c28aaSamw 	srcfd = -1;
2008*da6c28aaSamw 	if ((srcfd = open(src, O_RDONLY)) == -1) {
2009*da6c28aaSamw 		if (pflg && attrsilent) {
2010*da6c28aaSamw 			error++;
2011*da6c28aaSamw 			goto out;
2012*da6c28aaSamw 		}
2013*da6c28aaSamw 		if (!attrsilent) {
2014*da6c28aaSamw 			(void) fprintf(stderr,
2015*da6c28aaSamw 			    gettext("%s: cannot open file"
2016*da6c28aaSamw 			    " %s: "), cmd, src);
2017*da6c28aaSamw 			perror("");
2018*da6c28aaSamw 		}
2019*da6c28aaSamw 		++error;
2020*da6c28aaSamw 	}
2021*da6c28aaSamw out:
2022*da6c28aaSamw 	if (error)
2023*da6c28aaSamw 		close_all();
2024*da6c28aaSamw 	return (error == 0 ? 0 : 1);
2025*da6c28aaSamw }
2026*da6c28aaSamw 
2027*da6c28aaSamw /* Open source attribute dir, target and target attribute dir. */
2028*da6c28aaSamw 
2029*da6c28aaSamw int
2030*da6c28aaSamw open_target_srctarg_attrdirs(char  *src, char *targ)
2031*da6c28aaSamw {
2032*da6c28aaSamw 	int		error = 0;
2033*da6c28aaSamw 
2034*da6c28aaSamw 	targfd = sourcedirfd = targetdirfd = -1;
2035*da6c28aaSamw 
2036*da6c28aaSamw 	if ((targfd = open(targ, O_RDONLY)) == -1) {
2037*da6c28aaSamw 		if (pflg && attrsilent) {
2038*da6c28aaSamw 			error++;
2039*da6c28aaSamw 			goto out;
2040*da6c28aaSamw 		}
2041*da6c28aaSamw 		if (!attrsilent) {
2042*da6c28aaSamw 			(void) fprintf(stderr,
2043*da6c28aaSamw 			    gettext("%s: cannot open file"
2044*da6c28aaSamw 			    " %s: "), cmd, targ);
2045*da6c28aaSamw 			perror("");
2046*da6c28aaSamw 		}
2047*da6c28aaSamw 		++error;
2048*da6c28aaSamw 		goto out;
2049*da6c28aaSamw 	}
2050*da6c28aaSamw 
2051*da6c28aaSamw 	if ((sourcedirfd = openat(srcfd, ".", O_RDONLY|O_XATTR)) == -1) {
2052*da6c28aaSamw 		if (pflg && attrsilent) {
2053*da6c28aaSamw 			error++;
2054*da6c28aaSamw 			goto out;
2055*da6c28aaSamw 		}
2056*da6c28aaSamw 		if (!attrsilent) {
2057*da6c28aaSamw 			(void) fprintf(stderr,
2058*da6c28aaSamw 			    gettext("%s: cannot open attribute"
2059*da6c28aaSamw 			    " directory for %s: "), cmd, src);
2060*da6c28aaSamw 			perror("");
2061*da6c28aaSamw 		}
2062*da6c28aaSamw 		++error;
2063*da6c28aaSamw 		goto out;
2064*da6c28aaSamw 	}
2065*da6c28aaSamw 
2066*da6c28aaSamw 	if (fstat(sourcedirfd, &attrdir) == -1) {
2067*da6c28aaSamw 		if (pflg && attrsilent) {
2068*da6c28aaSamw 			error++;
2069*da6c28aaSamw 			goto out;
2070*da6c28aaSamw 		}
2071*da6c28aaSamw 
2072*da6c28aaSamw 		if (!attrsilent) {
2073*da6c28aaSamw 			(void) fprintf(stderr,
2074*da6c28aaSamw 			    gettext("%s: could not retrieve stat"
2075*da6c28aaSamw 			    " information for attribute directory"
2076*da6c28aaSamw 			    "of file %s: "), cmd, src);
2077*da6c28aaSamw 			perror("");
2078*da6c28aaSamw 		}
2079*da6c28aaSamw 		++error;
2080*da6c28aaSamw 		goto out;
2081*da6c28aaSamw 	}
2082*da6c28aaSamw 	if ((targetdirfd = openat(targfd, ".", O_RDONLY|O_XATTR)) == -1) {
2083*da6c28aaSamw 		if (pflg && attrsilent) {
2084*da6c28aaSamw 			error++;
2085*da6c28aaSamw 			goto out;
2086*da6c28aaSamw 		}
2087*da6c28aaSamw 		if (!attrsilent) {
2088*da6c28aaSamw 			(void) fprintf(stderr,
2089*da6c28aaSamw 			    gettext("%s: cannot open attribute"
2090*da6c28aaSamw 			    " directory for %s: "), cmd, targ);
2091*da6c28aaSamw 			perror("");
2092*da6c28aaSamw 		}
2093*da6c28aaSamw 		++error;
2094*da6c28aaSamw 	}
2095*da6c28aaSamw out:
2096*da6c28aaSamw 	if (error)
2097*da6c28aaSamw 		close_all();
2098*da6c28aaSamw 	return (error == 0 ? 0 : 1);
2099*da6c28aaSamw }
2100*da6c28aaSamw 
2101*da6c28aaSamw int
2102*da6c28aaSamw open_attrdirp(char *source)
2103*da6c28aaSamw {
2104*da6c28aaSamw 	int tmpfd = -1;
2105*da6c28aaSamw 	int error = 0;
2106*da6c28aaSamw 
2107*da6c28aaSamw 	/*
2108*da6c28aaSamw 	 * dup sourcedirfd for use by fdopendir().
2109*da6c28aaSamw 	 * fdopendir will take ownership of given fd and will close
2110*da6c28aaSamw 	 * it when closedir() is called.
2111*da6c28aaSamw 	 */
2112*da6c28aaSamw 
2113*da6c28aaSamw 	if ((tmpfd = dup(sourcedirfd)) == -1) {
2114*da6c28aaSamw 		if (pflg && attrsilent) {
2115*da6c28aaSamw 			error++;
2116*da6c28aaSamw 			goto out;
2117*da6c28aaSamw 		}
2118*da6c28aaSamw 		if (!attrsilent) {
2119*da6c28aaSamw 			(void) fprintf(stderr,
2120*da6c28aaSamw 			    gettext(
2121*da6c28aaSamw 			    "%s: unable to dup attribute directory"
2122*da6c28aaSamw 			    " file descriptor for %s: "), cmd, source);
2123*da6c28aaSamw 			perror("");
2124*da6c28aaSamw 			++error;
2125*da6c28aaSamw 		}
2126*da6c28aaSamw 		goto out;
2127*da6c28aaSamw 	}
2128*da6c28aaSamw 	if ((srcdirp = fdopendir(tmpfd)) == NULL) {
2129*da6c28aaSamw 		if (pflg && attrsilent) {
2130*da6c28aaSamw 			error++;
2131*da6c28aaSamw 			goto out;
2132*da6c28aaSamw 		}
2133*da6c28aaSamw 		if (!attrsilent) {
2134*da6c28aaSamw 			(void) fprintf(stderr,
2135*da6c28aaSamw 			    gettext("%s: failed to open attribute"
2136*da6c28aaSamw 			    " directory for %s: "), cmd, source);
2137*da6c28aaSamw 			perror("");
2138*da6c28aaSamw 			++error;
2139*da6c28aaSamw 		}
2140*da6c28aaSamw 	}
2141*da6c28aaSamw out:
2142*da6c28aaSamw 	if (error)
2143*da6c28aaSamw 		close_all();
2144*da6c28aaSamw 	return (error == 0 ? 0 : 1);
2145*da6c28aaSamw }
2146*da6c28aaSamw 
2147*da6c28aaSamw /* Skips through ., .., and system attribute 'view' files */
2148*da6c28aaSamw int
2149*da6c28aaSamw traverse_attrfile(struct dirent *dp, char *source, char *target, int  first)
2150*da6c28aaSamw {
2151*da6c28aaSamw 	int		error = 0;
2152*da6c28aaSamw 
2153*da6c28aaSamw 	if ((dp->d_name[0] == '.' && dp->d_name[1] == '\0') ||
2154*da6c28aaSamw 	    (dp->d_name[0] == '.' && dp->d_name[1] == '.' &&
2155*da6c28aaSamw 	    dp->d_name[2] == '\0') ||
2156*da6c28aaSamw 	    (sysattr_type(dp->d_name) == _RO_SATTR) ||
2157*da6c28aaSamw 	    (sysattr_type(dp->d_name) == _RW_SATTR))
2158*da6c28aaSamw 		return (-1);
2159*da6c28aaSamw 
2160*da6c28aaSamw 	if ((srcattrfd = openat(sourcedirfd, dp->d_name,
2161*da6c28aaSamw 	    O_RDONLY)) == -1) {
2162*da6c28aaSamw 		if (!attrsilent) {
2163*da6c28aaSamw 			(void) fprintf(stderr,
2164*da6c28aaSamw 			    gettext("%s: cannot open attribute %s on"
2165*da6c28aaSamw 			    " file %s: "), cmd, dp->d_name, source);
2166*da6c28aaSamw 			perror("");
2167*da6c28aaSamw 			++error;
2168*da6c28aaSamw 			goto out;
2169*da6c28aaSamw 		}
2170*da6c28aaSamw 	}
2171*da6c28aaSamw 
2172*da6c28aaSamw 	if (fstat(srcattrfd, &s3) < 0) {
2173*da6c28aaSamw 		if (!attrsilent) {
2174*da6c28aaSamw 			(void) fprintf(stderr,
2175*da6c28aaSamw 			    gettext("%s: could not stat attribute"
2176*da6c28aaSamw 			    " %s on file"
2177*da6c28aaSamw 			    " %s: "), cmd, dp->d_name, source);
2178*da6c28aaSamw 			perror("");
2179*da6c28aaSamw 			++error;
2180*da6c28aaSamw 		}
2181*da6c28aaSamw 		goto out;
2182*da6c28aaSamw 	}
2183*da6c28aaSamw 
2184*da6c28aaSamw 	if (first) {
2185*da6c28aaSamw 		(void) unlinkat(targetdirfd, dp->d_name, 0);
2186*da6c28aaSamw 		targattrfd = openat(targetdirfd, dp->d_name,
2187*da6c28aaSamw 		    O_RDWR|O_CREAT|O_TRUNC, s3.st_mode & MODEBITS);
2188*da6c28aaSamw 	} else {
2189*da6c28aaSamw 		targattrfd = openat(targetdirfd, dp->d_name, O_RDWR);
2190*da6c28aaSamw 	}
2191*da6c28aaSamw 
2192*da6c28aaSamw 	if (targattrfd == -1) {
2193*da6c28aaSamw 		if (!attrsilent) {
2194*da6c28aaSamw 			(void) fprintf(stderr,
2195*da6c28aaSamw 			    gettext("%s: could not create attribute"
2196*da6c28aaSamw 			    " %s on file %s: "), cmd, dp->d_name,
2197*da6c28aaSamw 			    target);
2198*da6c28aaSamw 			perror("");
2199*da6c28aaSamw 			++error;
2200*da6c28aaSamw 		}
2201*da6c28aaSamw 		goto out;
2202*da6c28aaSamw 	}
2203*da6c28aaSamw 
2204*da6c28aaSamw 	if (fstat(targattrfd, &s4) < 0) {
2205*da6c28aaSamw 		if (!attrsilent) {
2206*da6c28aaSamw 			(void) fprintf(stderr,
2207*da6c28aaSamw 			    gettext("%s: could not stat attribute"
2208*da6c28aaSamw 			    " %s on file"
2209*da6c28aaSamw 			    " %s: "), cmd, dp->d_name, target);
2210*da6c28aaSamw 			perror("");
2211*da6c28aaSamw 			++error;
2212*da6c28aaSamw 		}
2213*da6c28aaSamw 	}
2214*da6c28aaSamw 
2215*da6c28aaSamw out:
2216*da6c28aaSamw 	if (error) {
2217*da6c28aaSamw 		if (srcattrfd != -1)
2218*da6c28aaSamw 			(void) close(srcattrfd);
2219*da6c28aaSamw 		if (targattrfd != -1)
2220*da6c28aaSamw 		(void) close(targattrfd);
2221*da6c28aaSamw 	}
2222*da6c28aaSamw 	return (error == 0 ? 0 :1);
2223*da6c28aaSamw }
2224*da6c28aaSamw 
2225*da6c28aaSamw void
2226*da6c28aaSamw rewind_attrdir(DIR * sdp)
2227*da6c28aaSamw {
2228*da6c28aaSamw 	int pwdfd;
2229*da6c28aaSamw 
2230*da6c28aaSamw 	pwdfd = open(".", O_RDONLY);
2231*da6c28aaSamw 	if ((pwdfd != -1) && (fchdir(sourcedirfd) == 0)) {
2232*da6c28aaSamw 		rewinddir(sdp);
2233*da6c28aaSamw 		(void) fchdir(pwdfd);
2234*da6c28aaSamw 		(void) close(pwdfd);
2235*da6c28aaSamw 	} else {
2236*da6c28aaSamw 		if (!attrsilent) {
2237*da6c28aaSamw 			(void) fprintf(stderr, gettext("%s: "
2238*da6c28aaSamw 			    "failed to rewind attribute dir\n"),
2239*da6c28aaSamw 			    cmd);
2240*da6c28aaSamw 		}
2241*da6c28aaSamw 	}
2242*da6c28aaSamw }
2243*da6c28aaSamw 
2244*da6c28aaSamw void
2245*da6c28aaSamw close_all()
2246*da6c28aaSamw {
2247*da6c28aaSamw 	if (srcattrfd != -1)
2248*da6c28aaSamw 		(void) close(srcattrfd);
2249*da6c28aaSamw 	if (targattrfd != -1)
2250*da6c28aaSamw 		(void) close(targattrfd);
2251*da6c28aaSamw 	if (sourcedirfd != -1)
2252*da6c28aaSamw 		(void) close(sourcedirfd);
2253*da6c28aaSamw 	if (targetdirfd != -1)
2254*da6c28aaSamw 		(void) close(targetdirfd);
2255*da6c28aaSamw 	if (srcdirp != NULL) {
2256*da6c28aaSamw 		(void) closedir(srcdirp);
2257*da6c28aaSamw 		srcdirp = NULL;
2258*da6c28aaSamw 	}
2259*da6c28aaSamw 	if (srcfd != -1)
2260*da6c28aaSamw 		(void) close(srcfd);
2261*da6c28aaSamw 	if (targfd != -1)
2262*da6c28aaSamw 		(void) close(targfd);
2263*da6c28aaSamw }
2264