xref: /titanic_53/usr/src/cmd/mv/mv.c (revision 6cdb72227544a5f07e7428e591c2ed84825f4d1f)
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  */
2178cca7e2SRoger A. Faulkner 
227c478bd9Sstevel@tonic-gate /*
23*6cdb7222SAlexander Eremin  * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
24*6cdb7222SAlexander Eremin  */
25*6cdb7222SAlexander Eremin 
26*6cdb7222SAlexander Eremin /*
27ee9c203bSRenaud Manus  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
327c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
367c478bd9Sstevel@tonic-gate  * The Regents of the University of California
377c478bd9Sstevel@tonic-gate  * All Rights Reserved
387c478bd9Sstevel@tonic-gate  *
397c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
407c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
417c478bd9Sstevel@tonic-gate  * contributors.
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * Combined mv/cp/ln command:
467c478bd9Sstevel@tonic-gate  *	mv file1 file2
477c478bd9Sstevel@tonic-gate  *	mv dir1 dir2
487c478bd9Sstevel@tonic-gate  *	mv file1 ... filen dir1
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate #include <sys/time.h>
517c478bd9Sstevel@tonic-gate #include <signal.h>
527c478bd9Sstevel@tonic-gate #include <locale.h>
537c478bd9Sstevel@tonic-gate #include <stdarg.h>
547c478bd9Sstevel@tonic-gate #include <sys/acl.h>
557c478bd9Sstevel@tonic-gate #include <libcmdutils.h>
56fa9e4066Sahrens #include <aclutils.h>
573d63ea05Sas145665 #include "getresponse.h"
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #define	FTYPE(A)	(A.st_mode)
607c478bd9Sstevel@tonic-gate #define	FMODE(A)	(A.st_mode)
617c478bd9Sstevel@tonic-gate #define	UID(A)		(A.st_uid)
627c478bd9Sstevel@tonic-gate #define	GID(A)		(A.st_gid)
637c478bd9Sstevel@tonic-gate #define	IDENTICAL(A, B)	(A.st_dev == B.st_dev && A.st_ino == B.st_ino)
647c478bd9Sstevel@tonic-gate #define	ISDIR(A)	((A.st_mode & S_IFMT) == S_IFDIR)
657c478bd9Sstevel@tonic-gate #define	ISDOOR(A)	((A.st_mode & S_IFMT) == S_IFDOOR)
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)
71e1636a06Sdanny webster #define	ISSOCK(A)	((A.st_mode & S_IFMT) == S_IFSOCK)
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #define	DELIM	'/'
747c478bd9Sstevel@tonic-gate #define	EQ(x, y)	(strcmp(x, y) == 0)
757c478bd9Sstevel@tonic-gate #define	FALSE	0
767c478bd9Sstevel@tonic-gate #define	MODEBITS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)
777c478bd9Sstevel@tonic-gate #define	TRUE 1
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate static char		*dname(char *);
807c478bd9Sstevel@tonic-gate static int		lnkfil(char *, char *);
817c478bd9Sstevel@tonic-gate static int		cpymve(char *, char *);
827c478bd9Sstevel@tonic-gate static int		chkfiles(char *, char **);
837c478bd9Sstevel@tonic-gate static int		rcopy(char *, char *);
847c478bd9Sstevel@tonic-gate static int		chk_different(char *, char *);
857c478bd9Sstevel@tonic-gate static int		chg_time(char *, struct stat);
867c478bd9Sstevel@tonic-gate static int		chg_mode(char *, uid_t, gid_t, mode_t);
877c478bd9Sstevel@tonic-gate static int		copydir(char *, char *);
887c478bd9Sstevel@tonic-gate static int		copyspecial(char *);
897c478bd9Sstevel@tonic-gate static int		getrealpath(char *, char *);
907c478bd9Sstevel@tonic-gate static void		usage(void);
917c478bd9Sstevel@tonic-gate static void		Perror(char *);
927c478bd9Sstevel@tonic-gate static void		Perror2(char *, char *);
937c478bd9Sstevel@tonic-gate static int		use_stdin(void);
947c478bd9Sstevel@tonic-gate static int		copyattributes(char *, char *);
95da6c28aaSamw static int		copy_sysattr(char *, char *);
967c478bd9Sstevel@tonic-gate static tree_node_t	*create_tnode(dev_t, ino_t);
977c478bd9Sstevel@tonic-gate 
98da6c28aaSamw static struct stat 	s1, s2, s3, s4;
997c478bd9Sstevel@tonic-gate static int 		cpy = FALSE;
1007c478bd9Sstevel@tonic-gate static int 		mve = FALSE;
1017c478bd9Sstevel@tonic-gate static int 		lnk = FALSE;
1027c478bd9Sstevel@tonic-gate static char		*cmd;
1037c478bd9Sstevel@tonic-gate static int		silent = 0;
1047c478bd9Sstevel@tonic-gate static int		fflg = 0;
1057c478bd9Sstevel@tonic-gate static int		iflg = 0;
1067c478bd9Sstevel@tonic-gate static int		pflg = 0;
1077c478bd9Sstevel@tonic-gate static int		Rflg = 0;	/* recursive copy */
1087c478bd9Sstevel@tonic-gate static int		rflg = 0;	/* recursive copy */
1097c478bd9Sstevel@tonic-gate static int		sflg = 0;
1107c478bd9Sstevel@tonic-gate static int		Hflg = 0;	/* follow cmd line arg symlink to dir */
1117c478bd9Sstevel@tonic-gate static int		Lflg = 0;	/* follow symlinks */
1127c478bd9Sstevel@tonic-gate static int		Pflg = 0;	/* do not follow symlinks */
1137c478bd9Sstevel@tonic-gate static int		atflg = 0;
1147c478bd9Sstevel@tonic-gate static int		attrsilent = 0;
1157c478bd9Sstevel@tonic-gate static int		targetexists = 0;
1167c478bd9Sstevel@tonic-gate static int		cmdarg;		/* command line argument */
1177c478bd9Sstevel@tonic-gate static avl_tree_t	*stree = NULL;	/* source file inode search tree */
118fa9e4066Sahrens static acl_t		*s1acl;
119da6c28aaSamw static int		saflg = 0;	/* 'cp' extended system attr. */
120beab0215Sbasabi static int		srcfd = -1;
121beab0215Sbasabi static int		targfd = -1;
122beab0215Sbasabi static int		sourcedirfd = -1;
123beab0215Sbasabi static int		targetdirfd = -1;
124beab0215Sbasabi static DIR 		*srcdirp = NULL;
125beab0215Sbasabi static int		srcattrfd = -1;
126beab0215Sbasabi static int		targattrfd = -1;
127da6c28aaSamw static struct stat 	attrdir;
128da6c28aaSamw 
129da6c28aaSamw /* Extended system attributes support */
130da6c28aaSamw 
131da6c28aaSamw static int open_source(char  *);
132da6c28aaSamw static int open_target_srctarg_attrdirs(char  *, char *);
133da6c28aaSamw static int open_attrdirp(char *);
134da6c28aaSamw static int traverse_attrfile(struct dirent *, char *, char *, int);
135da6c28aaSamw static void rewind_attrdir(DIR *);
136da6c28aaSamw static void close_all();
137da6c28aaSamw 
1387c478bd9Sstevel@tonic-gate 
139a77d64afScf46844 int
1407c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate 	int c, i, r, errflg = 0;
1437c478bd9Sstevel@tonic-gate 	char target[PATH_MAX];
1447c478bd9Sstevel@tonic-gate 	int (*move)(char *, char *);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	/*
1477c478bd9Sstevel@tonic-gate 	 * Determine command invoked (mv, cp, or ln)
1487c478bd9Sstevel@tonic-gate 	 */
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	if (cmd = strrchr(argv[0], '/'))
1517c478bd9Sstevel@tonic-gate 		++cmd;
1527c478bd9Sstevel@tonic-gate 	else
1537c478bd9Sstevel@tonic-gate 		cmd = argv[0];
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	/*
1567c478bd9Sstevel@tonic-gate 	 * Set flags based on command.
1577c478bd9Sstevel@tonic-gate 	 */
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1607c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1617c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
1627c478bd9Sstevel@tonic-gate #endif
1637c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1643d63ea05Sas145665 	if (init_yes() < 0) {
1653d63ea05Sas145665 		(void) fprintf(stderr, gettext(ERR_MSG_INIT_YES),
1663d63ea05Sas145665 		    strerror(errno));
1673d63ea05Sas145665 		exit(3);
1683d63ea05Sas145665 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if (EQ(cmd, "mv"))
1717c478bd9Sstevel@tonic-gate 		mve = TRUE;
1727c478bd9Sstevel@tonic-gate 	else if (EQ(cmd, "ln"))
1737c478bd9Sstevel@tonic-gate 		lnk = TRUE;
1747c478bd9Sstevel@tonic-gate 	else if (EQ(cmd, "cp"))
1757c478bd9Sstevel@tonic-gate 		cpy = TRUE;
1767c478bd9Sstevel@tonic-gate 	else {
1777c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1787c478bd9Sstevel@tonic-gate 		    gettext("Invalid command name (%s); expecting "
1797c478bd9Sstevel@tonic-gate 		    "mv, cp, or ln.\n"), cmd);
1807c478bd9Sstevel@tonic-gate 		exit(1);
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/*
1847c478bd9Sstevel@tonic-gate 	 * Check for options:
185da6c28aaSamw 	 * 	cp  -r|-R [-H|-L|-P] [-fip@/] file1 [file2 ...] target
186da6c28aaSamw 	 * 	cp [-fiprR@/] file1 [file2 ...] target
1877c478bd9Sstevel@tonic-gate 	 *	ln [-f] [-n] [-s] file1 [file2 ...] target
1887c478bd9Sstevel@tonic-gate 	 *	ln [-f] [-n] [-s] file1 [file2 ...]
1897c478bd9Sstevel@tonic-gate 	 *	mv [-f|i] file1 [file2 ...] target
1907c478bd9Sstevel@tonic-gate 	 *	mv [-f|i] dir1 target
1917c478bd9Sstevel@tonic-gate 	 */
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	if (cpy) {
194da6c28aaSamw 		while ((c = getopt(argc, argv, "fHiLpPrR@/")) != EOF)
1957c478bd9Sstevel@tonic-gate 			switch (c) {
1967c478bd9Sstevel@tonic-gate 			case 'f':
1977c478bd9Sstevel@tonic-gate 				fflg++;
1987c478bd9Sstevel@tonic-gate 				break;
1997c478bd9Sstevel@tonic-gate 			case 'i':
2007c478bd9Sstevel@tonic-gate 				iflg++;
2017c478bd9Sstevel@tonic-gate 				break;
2027c478bd9Sstevel@tonic-gate 			case 'p':
2037c478bd9Sstevel@tonic-gate 				pflg++;
2047c478bd9Sstevel@tonic-gate #ifdef XPG4
2057c478bd9Sstevel@tonic-gate 				attrsilent = 1;
2067c478bd9Sstevel@tonic-gate 				atflg = 0;
207da6c28aaSamw 				saflg = 0;
2087c478bd9Sstevel@tonic-gate #else
209cee1ddadSbasabi 				if (atflg == 0)
2107c478bd9Sstevel@tonic-gate 					attrsilent = 1;
2117c478bd9Sstevel@tonic-gate #endif
2127c478bd9Sstevel@tonic-gate 				break;
2137c478bd9Sstevel@tonic-gate 			case 'H':
2147c478bd9Sstevel@tonic-gate 				/*
2157c478bd9Sstevel@tonic-gate 				 * If more than one of -H, -L, or -P are
2167c478bd9Sstevel@tonic-gate 				 * specified, only the last option specified
2177c478bd9Sstevel@tonic-gate 				 * determines the behavior.
2187c478bd9Sstevel@tonic-gate 				 */
2197c478bd9Sstevel@tonic-gate 				Lflg = Pflg = 0;
2207c478bd9Sstevel@tonic-gate 				Hflg++;
2217c478bd9Sstevel@tonic-gate 				break;
2227c478bd9Sstevel@tonic-gate 			case 'L':
2237c478bd9Sstevel@tonic-gate 				Hflg = Pflg = 0;
2247c478bd9Sstevel@tonic-gate 				Lflg++;
2257c478bd9Sstevel@tonic-gate 				break;
2267c478bd9Sstevel@tonic-gate 			case 'P':
2277c478bd9Sstevel@tonic-gate 				Lflg = Hflg = 0;
2287c478bd9Sstevel@tonic-gate 				Pflg++;
2297c478bd9Sstevel@tonic-gate 				break;
2307c478bd9Sstevel@tonic-gate 			case 'R':
2317c478bd9Sstevel@tonic-gate 				/*
2327c478bd9Sstevel@tonic-gate 				 * The default behavior of cp -R|-r
2337c478bd9Sstevel@tonic-gate 				 * when specified without -H|-L|-P
2347c478bd9Sstevel@tonic-gate 				 * is -L.
2357c478bd9Sstevel@tonic-gate 				 */
2367c478bd9Sstevel@tonic-gate 				Rflg++;
2377c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
2387c478bd9Sstevel@tonic-gate 			case 'r':
2397c478bd9Sstevel@tonic-gate 				rflg++;
2407c478bd9Sstevel@tonic-gate 				break;
2417c478bd9Sstevel@tonic-gate 			case '@':
2427c478bd9Sstevel@tonic-gate 				atflg++;
2437c478bd9Sstevel@tonic-gate 				attrsilent = 0;
2447c478bd9Sstevel@tonic-gate #ifdef XPG4
2457c478bd9Sstevel@tonic-gate 				pflg = 0;
2467c478bd9Sstevel@tonic-gate #endif
2477c478bd9Sstevel@tonic-gate 				break;
248da6c28aaSamw 			case '/':
249da6c28aaSamw 				saflg++;
250da6c28aaSamw 				attrsilent = 0;
251da6c28aaSamw #ifdef XPG4
252da6c28aaSamw 				pflg = 0;
253da6c28aaSamw #endif
254da6c28aaSamw 				break;
2557c478bd9Sstevel@tonic-gate 			default:
2567c478bd9Sstevel@tonic-gate 				errflg++;
2577c478bd9Sstevel@tonic-gate 			}
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 		/* -R or -r must be specified with -H, -L, or -P */
2607c478bd9Sstevel@tonic-gate 		if ((Hflg || Lflg || Pflg) && !(Rflg || rflg)) {
2617c478bd9Sstevel@tonic-gate 			errflg++;
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	} else if (mve) {
2657c478bd9Sstevel@tonic-gate 		while ((c = getopt(argc, argv, "fis")) != EOF)
2667c478bd9Sstevel@tonic-gate 			switch (c) {
2677c478bd9Sstevel@tonic-gate 			case 'f':
2687c478bd9Sstevel@tonic-gate 				silent++;
2697c478bd9Sstevel@tonic-gate #ifdef XPG4
2707c478bd9Sstevel@tonic-gate 				iflg = 0;
2717c478bd9Sstevel@tonic-gate #endif
2727c478bd9Sstevel@tonic-gate 				break;
2737c478bd9Sstevel@tonic-gate 			case 'i':
2747c478bd9Sstevel@tonic-gate 				iflg++;
2757c478bd9Sstevel@tonic-gate #ifdef XPG4
2767c478bd9Sstevel@tonic-gate 				silent = 0;
2777c478bd9Sstevel@tonic-gate #endif
2787c478bd9Sstevel@tonic-gate 				break;
2797c478bd9Sstevel@tonic-gate 			default:
2807c478bd9Sstevel@tonic-gate 				errflg++;
2817c478bd9Sstevel@tonic-gate 			}
2827c478bd9Sstevel@tonic-gate 	} else { /* ln */
2837c478bd9Sstevel@tonic-gate 		while ((c = getopt(argc, argv, "fns")) != EOF)
2847c478bd9Sstevel@tonic-gate 			switch (c) {
2857c478bd9Sstevel@tonic-gate 			case 'f':
2867c478bd9Sstevel@tonic-gate 				silent++;
2877c478bd9Sstevel@tonic-gate 				break;
2887c478bd9Sstevel@tonic-gate 			case 'n':
2897c478bd9Sstevel@tonic-gate 				/* silently ignored; this is the default */
2907c478bd9Sstevel@tonic-gate 				break;
2917c478bd9Sstevel@tonic-gate 			case 's':
2927c478bd9Sstevel@tonic-gate 				sflg++;
2937c478bd9Sstevel@tonic-gate 				break;
2947c478bd9Sstevel@tonic-gate 			default:
2957c478bd9Sstevel@tonic-gate 				errflg++;
2967c478bd9Sstevel@tonic-gate 			}
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	/*
3007c478bd9Sstevel@tonic-gate 	 * For BSD compatibility allow - to delimit the end of
3017c478bd9Sstevel@tonic-gate 	 * options for mv.
3027c478bd9Sstevel@tonic-gate 	 */
3037c478bd9Sstevel@tonic-gate 	if (mve && optind < argc && (strcmp(argv[optind], "-") == 0))
3047c478bd9Sstevel@tonic-gate 		optind++;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	/*
3077c478bd9Sstevel@tonic-gate 	 * Check for sufficient arguments
3087c478bd9Sstevel@tonic-gate 	 * or a usage error.
3097c478bd9Sstevel@tonic-gate 	 */
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	argc -= optind;
3127c478bd9Sstevel@tonic-gate 	argv  = &argv[optind];
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	if ((argc < 2 && lnk != TRUE) || (argc < 1 && lnk == TRUE)) {
3157c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3167c478bd9Sstevel@tonic-gate 		    gettext("%s: Insufficient arguments (%d)\n"),
3177c478bd9Sstevel@tonic-gate 		    cmd, argc);
3187c478bd9Sstevel@tonic-gate 		usage();
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	if (errflg != 0)
3227c478bd9Sstevel@tonic-gate 		usage();
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	/*
3257c478bd9Sstevel@tonic-gate 	 * If there is more than a source and target,
3267c478bd9Sstevel@tonic-gate 	 * the last argument (the target) must be a directory
3277c478bd9Sstevel@tonic-gate 	 * which really exists.
3287c478bd9Sstevel@tonic-gate 	 */
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	if (argc > 2) {
3317c478bd9Sstevel@tonic-gate 		if (stat(argv[argc-1], &s2) < 0) {
3327c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3337c478bd9Sstevel@tonic-gate 			    gettext("%s: %s not found\n"),
3347c478bd9Sstevel@tonic-gate 			    cmd, argv[argc-1]);
3357c478bd9Sstevel@tonic-gate 			exit(2);
3367c478bd9Sstevel@tonic-gate 		}
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 		if (!ISDIR(s2)) {
3397c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3407c478bd9Sstevel@tonic-gate 			    gettext("%s: Target %s must be a directory\n"),
3417c478bd9Sstevel@tonic-gate 			    cmd, argv[argc-1]);
3427c478bd9Sstevel@tonic-gate 			usage();
3437c478bd9Sstevel@tonic-gate 		}
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	if (strlen(argv[argc-1]) >= PATH_MAX) {
3477c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3487c478bd9Sstevel@tonic-gate 		    gettext("%s: Target %s file name length exceeds PATH_MAX"
3497c478bd9Sstevel@tonic-gate 		    " %d\n"), cmd, argv[argc-1], PATH_MAX);
3507c478bd9Sstevel@tonic-gate 		exit(78);
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	if (argc == 1) {
3547c478bd9Sstevel@tonic-gate 		if (!lnk)
3557c478bd9Sstevel@tonic-gate 			usage();
3567c478bd9Sstevel@tonic-gate 		(void) strcpy(target, ".");
3577c478bd9Sstevel@tonic-gate 	} else {
3587c478bd9Sstevel@tonic-gate 		(void) strcpy(target, argv[--argc]);
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	/*
3627c478bd9Sstevel@tonic-gate 	 * Perform a multiple argument mv|cp|ln by
3637c478bd9Sstevel@tonic-gate 	 * multiple invocations of cpymve() or lnkfil().
3647c478bd9Sstevel@tonic-gate 	 */
3657c478bd9Sstevel@tonic-gate 	if (lnk)
3667c478bd9Sstevel@tonic-gate 		move = lnkfil;
3677c478bd9Sstevel@tonic-gate 	else
3687c478bd9Sstevel@tonic-gate 		move = cpymve;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	r = 0;
3717c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
3727c478bd9Sstevel@tonic-gate 		stree = NULL;
3737c478bd9Sstevel@tonic-gate 		cmdarg = 1;
3747c478bd9Sstevel@tonic-gate 		r += move(argv[i], target);
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	/*
3787c478bd9Sstevel@tonic-gate 	 * Show errors by nonzero exit code.
3797c478bd9Sstevel@tonic-gate 	 */
3807c478bd9Sstevel@tonic-gate 
381a77d64afScf46844 	return (r?2:0);
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate static int
3857c478bd9Sstevel@tonic-gate lnkfil(char *source, char *target)
3867c478bd9Sstevel@tonic-gate {
3877c478bd9Sstevel@tonic-gate 	char	*buf = NULL;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	if (sflg) {
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 		/*
3927c478bd9Sstevel@tonic-gate 		 * If target is a directory make complete
3937c478bd9Sstevel@tonic-gate 		 * name of the new symbolic link within that
3947c478bd9Sstevel@tonic-gate 		 * directory.
3957c478bd9Sstevel@tonic-gate 		 */
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 		if ((stat(target, &s2) >= 0) && ISDIR(s2)) {
3987c478bd9Sstevel@tonic-gate 			size_t len;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 			len = strlen(target) + strlen(dname(source)) + 4;
4017c478bd9Sstevel@tonic-gate 			if ((buf = (char *)malloc(len)) == NULL) {
4027c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4037c478bd9Sstevel@tonic-gate 				    gettext("%s: Insufficient memory "
4047c478bd9Sstevel@tonic-gate 				    "to %s %s\n"), cmd, cmd, source);
4057c478bd9Sstevel@tonic-gate 				exit(3);
4067c478bd9Sstevel@tonic-gate 			}
4077c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, len, "%s/%s",
4087c478bd9Sstevel@tonic-gate 			    target, dname(source));
4097c478bd9Sstevel@tonic-gate 			target = buf;
4107c478bd9Sstevel@tonic-gate 		}
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 		/*
4136fcd7f75Schin 		 * Check to see if the file exists already.
4146fcd7f75Schin 		 * In this case we use lstat() instead of stat():
4156fcd7f75Schin 		 * unlink(2) and symlink(2) will operate on the file
4166fcd7f75Schin 		 * itself, not its reference, if the file is a symlink.
4177c478bd9Sstevel@tonic-gate 		 */
4187c478bd9Sstevel@tonic-gate 
4196fcd7f75Schin 		if ((lstat(target, &s2) == 0)) {
4207c478bd9Sstevel@tonic-gate 			/*
4217c478bd9Sstevel@tonic-gate 			 * Check if the silent flag is set ie. the -f option
4227c478bd9Sstevel@tonic-gate 			 * is used.  If so, use unlink to remove the current
4237c478bd9Sstevel@tonic-gate 			 * target to replace with the new target, specified
4247c478bd9Sstevel@tonic-gate 			 * on the command line.  Proceed with symlink.
4257c478bd9Sstevel@tonic-gate 			 */
4267c478bd9Sstevel@tonic-gate 			if (silent) {
4276fcd7f75Schin 			/*
4286fcd7f75Schin 			 * Don't allow silent (-f) removal of an existing
4296fcd7f75Schin 			 * directory; could leave unreferenced directory
4306fcd7f75Schin 			 * entries.
4316fcd7f75Schin 			 */
4326fcd7f75Schin 				if (ISDIR(s2)) {
4336fcd7f75Schin 					(void) fprintf(stderr,
4346fcd7f75Schin 					    gettext("%s: cannot create link "
4356fcd7f75Schin 					    "over directory %s\n"), cmd,
4366fcd7f75Schin 					    target);
4376fcd7f75Schin 					return (1);
4386fcd7f75Schin 				}
4397c478bd9Sstevel@tonic-gate 				if (unlink(target) < 0) {
4407c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
4417c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot unlink %s: "),
4427c478bd9Sstevel@tonic-gate 					    cmd, target);
4437c478bd9Sstevel@tonic-gate 					perror("");
4447c478bd9Sstevel@tonic-gate 					return (1);
4457c478bd9Sstevel@tonic-gate 				}
4467c478bd9Sstevel@tonic-gate 			}
4477c478bd9Sstevel@tonic-gate 		}
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 		/*
4517c478bd9Sstevel@tonic-gate 		 * Create a symbolic link to the source.
4527c478bd9Sstevel@tonic-gate 		 */
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 		if (symlink(source, target) < 0) {
4557c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4567c478bd9Sstevel@tonic-gate 			    gettext("%s: cannot create %s: "),
4577c478bd9Sstevel@tonic-gate 			    cmd, target);
4587c478bd9Sstevel@tonic-gate 			perror("");
4597c478bd9Sstevel@tonic-gate 			if (buf != NULL)
4607c478bd9Sstevel@tonic-gate 				free(buf);
4617c478bd9Sstevel@tonic-gate 			return (1);
4627c478bd9Sstevel@tonic-gate 		}
4637c478bd9Sstevel@tonic-gate 		if (buf != NULL)
4647c478bd9Sstevel@tonic-gate 			free(buf);
4657c478bd9Sstevel@tonic-gate 		return (0);
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	switch (chkfiles(source, &target)) {
4697c478bd9Sstevel@tonic-gate 		case 1: return (1);
4707c478bd9Sstevel@tonic-gate 		case 2: return (0);
4717c478bd9Sstevel@tonic-gate 			/* default - fall through */
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	/*
4757c478bd9Sstevel@tonic-gate 	 * Make sure source file is not a directory,
476e1636a06Sdanny webster 	 * we cannot link directories...
4777c478bd9Sstevel@tonic-gate 	 */
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	if (ISDIR(s1)) {
4807c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4817c478bd9Sstevel@tonic-gate 		    gettext("%s: %s is a directory\n"), cmd, source);
4827c478bd9Sstevel@tonic-gate 		return (1);
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	/*
4867c478bd9Sstevel@tonic-gate 	 * hard link, call link() and return.
4877c478bd9Sstevel@tonic-gate 	 */
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	if (link(source, target) < 0) {
4907c478bd9Sstevel@tonic-gate 		if (errno == EXDEV)
4917c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4927c478bd9Sstevel@tonic-gate 			    gettext("%s: %s is on a different file system\n"),
4937c478bd9Sstevel@tonic-gate 			    cmd, target);
4947c478bd9Sstevel@tonic-gate 		else {
4957c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4967c478bd9Sstevel@tonic-gate 			    gettext("%s: cannot create link %s: "),
4977c478bd9Sstevel@tonic-gate 			    cmd, target);
4987c478bd9Sstevel@tonic-gate 			perror("");
4997c478bd9Sstevel@tonic-gate 		}
5007c478bd9Sstevel@tonic-gate 		if (buf != NULL)
5017c478bd9Sstevel@tonic-gate 			free(buf);
5027c478bd9Sstevel@tonic-gate 		return (1);
5037c478bd9Sstevel@tonic-gate 	} else {
5047c478bd9Sstevel@tonic-gate 		if (buf != NULL)
5057c478bd9Sstevel@tonic-gate 			free(buf);
5067c478bd9Sstevel@tonic-gate 		return (0);
5077c478bd9Sstevel@tonic-gate 	}
5087c478bd9Sstevel@tonic-gate }
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate static int
5117c478bd9Sstevel@tonic-gate cpymve(char *source, char *target)
5127c478bd9Sstevel@tonic-gate {
5137c478bd9Sstevel@tonic-gate 	int	n;
5147c478bd9Sstevel@tonic-gate 	int fi, fo;
5157c478bd9Sstevel@tonic-gate 	int ret = 0;
5167c478bd9Sstevel@tonic-gate 	int attret = 0;
517da6c28aaSamw 	int sattret = 0;
5187c478bd9Sstevel@tonic-gate 	int errno_save;
519cee1ddadSbasabi 	int error = 0;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	switch (chkfiles(source, &target)) {
5227c478bd9Sstevel@tonic-gate 		case 1: return (1);
5237c478bd9Sstevel@tonic-gate 		case 2: return (0);
5247c478bd9Sstevel@tonic-gate 			/* default - fall through */
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	/*
5287c478bd9Sstevel@tonic-gate 	 * If it's a recursive copy and source
5297c478bd9Sstevel@tonic-gate 	 * is a directory, then call rcopy (from copydir).
5307c478bd9Sstevel@tonic-gate 	 */
5317c478bd9Sstevel@tonic-gate 	if (cpy) {
5327c478bd9Sstevel@tonic-gate 		if (ISDIR(s1)) {
5337c478bd9Sstevel@tonic-gate 			int		rc;
5347c478bd9Sstevel@tonic-gate 			avl_index_t	where = 0;
5357c478bd9Sstevel@tonic-gate 			tree_node_t	*tnode;
5367c478bd9Sstevel@tonic-gate 			tree_node_t	*tptr;
5377c478bd9Sstevel@tonic-gate 			dev_t		save_dev = s1.st_dev;
5387c478bd9Sstevel@tonic-gate 			ino_t		save_ino = s1.st_ino;
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 			/*
5417c478bd9Sstevel@tonic-gate 			 * We will be recursing into the directory so
5427c478bd9Sstevel@tonic-gate 			 * save the inode information to a search tree
5437c478bd9Sstevel@tonic-gate 			 * to avoid getting into an endless loop.
5447c478bd9Sstevel@tonic-gate 			 */
5457c478bd9Sstevel@tonic-gate 			if ((rc = add_tnode(&stree, save_dev, save_ino)) != 1) {
5467c478bd9Sstevel@tonic-gate 				if (rc == 0) {
5477c478bd9Sstevel@tonic-gate 					/*
5487c478bd9Sstevel@tonic-gate 					 * We've already visited this directory.
5497c478bd9Sstevel@tonic-gate 					 * Don't remove the search tree entry
5507c478bd9Sstevel@tonic-gate 					 * to make sure we don't get into an
5517c478bd9Sstevel@tonic-gate 					 * endless loop if revisited from a
5527c478bd9Sstevel@tonic-gate 					 * different part of the hierarchy.
5537c478bd9Sstevel@tonic-gate 					 */
5547c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
5557c478bd9Sstevel@tonic-gate 					    "%s: cycle detected: %s\n"),
5567c478bd9Sstevel@tonic-gate 					    cmd, source);
5577c478bd9Sstevel@tonic-gate 				} else {
5587c478bd9Sstevel@tonic-gate 					Perror(source);
5597c478bd9Sstevel@tonic-gate 				}
5607c478bd9Sstevel@tonic-gate 				return (1);
5617c478bd9Sstevel@tonic-gate 			}
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 			cmdarg = 0;
5647c478bd9Sstevel@tonic-gate 			rc = copydir(source, target);
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 			/*
5677c478bd9Sstevel@tonic-gate 			 * Create a tnode to get an index to the matching
5687c478bd9Sstevel@tonic-gate 			 * node (same dev and inode) in the search tree,
5697c478bd9Sstevel@tonic-gate 			 * then use the index to remove the matching node
5707c478bd9Sstevel@tonic-gate 			 * so it we do not wrongly detect a cycle when
5717c478bd9Sstevel@tonic-gate 			 * revisiting this directory from another part of
5727c478bd9Sstevel@tonic-gate 			 * the hierarchy.
5737c478bd9Sstevel@tonic-gate 			 */
5747c478bd9Sstevel@tonic-gate 			if ((tnode = create_tnode(save_dev,
5757c478bd9Sstevel@tonic-gate 			    save_ino)) == NULL) {
5767c478bd9Sstevel@tonic-gate 				Perror(source);
5777c478bd9Sstevel@tonic-gate 				return (1);
5787c478bd9Sstevel@tonic-gate 			}
5797c478bd9Sstevel@tonic-gate 			if ((tptr = avl_find(stree, tnode, &where)) != NULL) {
5807c478bd9Sstevel@tonic-gate 				avl_remove(stree, tptr);
5817c478bd9Sstevel@tonic-gate 			}
5827c478bd9Sstevel@tonic-gate 			free(tptr);
5837c478bd9Sstevel@tonic-gate 			free(tnode);
5847c478bd9Sstevel@tonic-gate 			return (rc);
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 		} else if (ISDEV(s1) && Rflg) {
5877c478bd9Sstevel@tonic-gate 			return (copyspecial(target));
5887c478bd9Sstevel@tonic-gate 		} else {
5897c478bd9Sstevel@tonic-gate 			goto copy;
5907c478bd9Sstevel@tonic-gate 		}
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	if (mve) {
5947c478bd9Sstevel@tonic-gate 		if (rename(source, target) >= 0)
5957c478bd9Sstevel@tonic-gate 			return (0);
5967c478bd9Sstevel@tonic-gate 		if (errno != EXDEV) {
5977c478bd9Sstevel@tonic-gate 			if (errno == ENOTDIR && ISDIR(s1)) {
5987c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
5997c478bd9Sstevel@tonic-gate 				    gettext("%s: %s is a directory\n"),
6007c478bd9Sstevel@tonic-gate 				    cmd, source);
6017c478bd9Sstevel@tonic-gate 				return (1);
6027c478bd9Sstevel@tonic-gate 			}
6037c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
6047c478bd9Sstevel@tonic-gate 			    gettext("%s: cannot rename %s to %s: "),
6057c478bd9Sstevel@tonic-gate 			    cmd, source, target);
6067c478bd9Sstevel@tonic-gate 			perror("");
6077c478bd9Sstevel@tonic-gate 			return (1);
6087c478bd9Sstevel@tonic-gate 		}
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 		/*
6117c478bd9Sstevel@tonic-gate 		 * cannot move a non-directory (source) onto an existing
6127c478bd9Sstevel@tonic-gate 		 * directory (target)
6137c478bd9Sstevel@tonic-gate 		 *
6147c478bd9Sstevel@tonic-gate 		 */
6157c478bd9Sstevel@tonic-gate 		if (targetexists && ISDIR(s2) && (!ISDIR(s1))) {
6167c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
6177c478bd9Sstevel@tonic-gate 			    gettext("%s: cannot mv a non directory %s "
6187c478bd9Sstevel@tonic-gate 			    "over existing directory"
6197c478bd9Sstevel@tonic-gate 			    " %s \n"), cmd, source, target);
6207c478bd9Sstevel@tonic-gate 			return (1);
6217c478bd9Sstevel@tonic-gate 		}
6227c478bd9Sstevel@tonic-gate 		if (ISDIR(s1)) {
6237c478bd9Sstevel@tonic-gate #ifdef XPG4
6247c478bd9Sstevel@tonic-gate 			if (targetexists && ISDIR(s2)) {
6257c478bd9Sstevel@tonic-gate 				/* existing target dir must be empty */
6267c478bd9Sstevel@tonic-gate 				if (rmdir(target) < 0) {
6277c478bd9Sstevel@tonic-gate 					errno_save = errno;
6287c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
6297c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot rmdir %s: "),
6307c478bd9Sstevel@tonic-gate 					    cmd, target);
6317c478bd9Sstevel@tonic-gate 					errno = errno_save;
6327c478bd9Sstevel@tonic-gate 					perror("");
6337c478bd9Sstevel@tonic-gate 					return (1);
6347c478bd9Sstevel@tonic-gate 				}
6357c478bd9Sstevel@tonic-gate 			}
6367c478bd9Sstevel@tonic-gate #endif
6377c478bd9Sstevel@tonic-gate 			if ((n =  copydir(source, target)) == 0)
6387c478bd9Sstevel@tonic-gate 				(void) rmdir(source);
6397c478bd9Sstevel@tonic-gate 			return (n);
6407c478bd9Sstevel@tonic-gate 		}
6417c478bd9Sstevel@tonic-gate 
642e1636a06Sdanny webster 		/* doors cannot be moved across filesystems */
6437c478bd9Sstevel@tonic-gate 		if (ISDOOR(s1)) {
6447c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
645e1636a06Sdanny webster 			    gettext("%s: %s: cannot move door "
6467c478bd9Sstevel@tonic-gate 			    "across file systems\n"), cmd, source);
6477c478bd9Sstevel@tonic-gate 			return (1);
6487c478bd9Sstevel@tonic-gate 		}
649e1636a06Sdanny webster 
650e1636a06Sdanny webster 		/* sockets cannot be moved across filesystems */
651e1636a06Sdanny webster 		if (ISSOCK(s1)) {
652e1636a06Sdanny webster 			(void) fprintf(stderr,
653e1636a06Sdanny webster 			    gettext("%s: %s: cannot move socket "
654e1636a06Sdanny webster 			    "across file systems\n"), cmd, source);
655e1636a06Sdanny webster 			return (1);
656e1636a06Sdanny webster 		}
657e1636a06Sdanny webster 
6587c478bd9Sstevel@tonic-gate 		/*
659e1636a06Sdanny webster 		 * File cannot be renamed, try to recreate the symbolic
6607c478bd9Sstevel@tonic-gate 		 * link or special device, or copy the file wholesale
6617c478bd9Sstevel@tonic-gate 		 * between file systems.
6627c478bd9Sstevel@tonic-gate 		 */
6637c478bd9Sstevel@tonic-gate 		if (ISLNK(s1)) {
6647c478bd9Sstevel@tonic-gate 			register int	m;
6657c478bd9Sstevel@tonic-gate 			register mode_t md;
6667c478bd9Sstevel@tonic-gate 			char symln[PATH_MAX + 1];
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 			if (targetexists && unlink(target) < 0) {
6697c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6707c478bd9Sstevel@tonic-gate 				    gettext("%s: cannot unlink %s: "),
6717c478bd9Sstevel@tonic-gate 				    cmd, target);
6727c478bd9Sstevel@tonic-gate 				perror("");
6737c478bd9Sstevel@tonic-gate 				return (1);
6747c478bd9Sstevel@tonic-gate 			}
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 			if ((m = readlink(source, symln,
6777c478bd9Sstevel@tonic-gate 			    sizeof (symln) - 1)) < 0) {
6787c478bd9Sstevel@tonic-gate 				Perror(source);
6797c478bd9Sstevel@tonic-gate 				return (1);
6807c478bd9Sstevel@tonic-gate 			}
6817c478bd9Sstevel@tonic-gate 			symln[m] = '\0';
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 			md = umask(~(s1.st_mode & MODEBITS));
6847c478bd9Sstevel@tonic-gate 			if (symlink(symln, target) < 0) {
6857c478bd9Sstevel@tonic-gate 				Perror(target);
6867c478bd9Sstevel@tonic-gate 				return (1);
6877c478bd9Sstevel@tonic-gate 			}
6887c478bd9Sstevel@tonic-gate 			(void) umask(md);
6897c478bd9Sstevel@tonic-gate 			m = lchown(target, UID(s1), GID(s1));
6907c478bd9Sstevel@tonic-gate #ifdef XPG4
6917c478bd9Sstevel@tonic-gate 			if (m < 0) {
6927c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: cannot"
6937c478bd9Sstevel@tonic-gate 				    " change owner and group of"
6947c478bd9Sstevel@tonic-gate 				    " %s: "), cmd, target);
6957c478bd9Sstevel@tonic-gate 				perror("");
6967c478bd9Sstevel@tonic-gate 			}
6977c478bd9Sstevel@tonic-gate #endif
6987c478bd9Sstevel@tonic-gate 			goto cleanup;
6997c478bd9Sstevel@tonic-gate 		}
7007c478bd9Sstevel@tonic-gate 		if (ISDEV(s1)) {
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 			if (targetexists && unlink(target) < 0) {
7037c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
7047c478bd9Sstevel@tonic-gate 				    gettext("%s: cannot unlink %s: "),
7057c478bd9Sstevel@tonic-gate 				    cmd, target);
7067c478bd9Sstevel@tonic-gate 				perror("");
7077c478bd9Sstevel@tonic-gate 				return (1);
7087c478bd9Sstevel@tonic-gate 			}
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 			if (mknod(target, s1.st_mode, s1.st_rdev) < 0) {
7117c478bd9Sstevel@tonic-gate 				Perror(target);
7127c478bd9Sstevel@tonic-gate 				return (1);
7137c478bd9Sstevel@tonic-gate 			}
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 			(void) chg_mode(target, UID(s1), GID(s1), FMODE(s1));
7167c478bd9Sstevel@tonic-gate 			(void) chg_time(target, s1);
7177c478bd9Sstevel@tonic-gate 			goto cleanup;
7187c478bd9Sstevel@tonic-gate 		}
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 		if (ISREG(s1)) {
7217c478bd9Sstevel@tonic-gate 			if (ISDIR(s2)) {
7227c478bd9Sstevel@tonic-gate 				if (targetexists && rmdir(target) < 0) {
7237c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
7247c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot rmdir %s: "),
7257c478bd9Sstevel@tonic-gate 					    cmd, target);
7267c478bd9Sstevel@tonic-gate 					perror("");
7277c478bd9Sstevel@tonic-gate 					return (1);
7287c478bd9Sstevel@tonic-gate 				}
7297c478bd9Sstevel@tonic-gate 			} else {
7307c478bd9Sstevel@tonic-gate 				if (targetexists && unlink(target) < 0) {
7317c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
7327c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot unlink %s: "),
7337c478bd9Sstevel@tonic-gate 					    cmd, target);
7347c478bd9Sstevel@tonic-gate 					perror("");
7357c478bd9Sstevel@tonic-gate 					return (1);
7367c478bd9Sstevel@tonic-gate 				}
7377c478bd9Sstevel@tonic-gate 			}
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate copy:
7417c478bd9Sstevel@tonic-gate 			/*
7427c478bd9Sstevel@tonic-gate 			 * If the source file is a symlink, and either
7437c478bd9Sstevel@tonic-gate 			 * -P or -H flag (only if -H is specified and the
7447c478bd9Sstevel@tonic-gate 			 * source file is not a command line argument)
7457c478bd9Sstevel@tonic-gate 			 * were specified, then action is taken on the symlink
7467c478bd9Sstevel@tonic-gate 			 * itself, not the file referenced by the symlink.
7477c478bd9Sstevel@tonic-gate 			 * Note: this is executed for 'cp' only.
7487c478bd9Sstevel@tonic-gate 			 */
7497c478bd9Sstevel@tonic-gate 			if (cpy && (Pflg || (Hflg && !cmdarg)) && (ISLNK(s1))) {
7507c478bd9Sstevel@tonic-gate 				int	m;
7517c478bd9Sstevel@tonic-gate 				mode_t	md;
7527c478bd9Sstevel@tonic-gate 				char symln[PATH_MAX + 1];
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 				m = readlink(source, symln, sizeof (symln) - 1);
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 				if (m < 0) {
7577c478bd9Sstevel@tonic-gate 					Perror(source);
7587c478bd9Sstevel@tonic-gate 					return (1);
7597c478bd9Sstevel@tonic-gate 				}
7607c478bd9Sstevel@tonic-gate 				symln[m] = '\0';
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 				/*
7637c478bd9Sstevel@tonic-gate 				 * Copy the sym link to the target.
7647c478bd9Sstevel@tonic-gate 				 * Note: If the target exists, write a
7657c478bd9Sstevel@tonic-gate 				 * diagnostic message, do nothing more
7667c478bd9Sstevel@tonic-gate 				 * with the source file, and return to
7677c478bd9Sstevel@tonic-gate 				 * process any remaining files.
7687c478bd9Sstevel@tonic-gate 				 */
7697c478bd9Sstevel@tonic-gate 				md = umask(~(s1.st_mode & MODEBITS));
7707c478bd9Sstevel@tonic-gate 				if (symlink(symln, target) < 0) {
7717c478bd9Sstevel@tonic-gate 					Perror(target);
7727c478bd9Sstevel@tonic-gate 					return (1);
7737c478bd9Sstevel@tonic-gate 				}
7747c478bd9Sstevel@tonic-gate 				(void) umask(md);
7757c478bd9Sstevel@tonic-gate 				m = lchown(target, UID(s1), GID(s1));
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 				if (m < 0) {
7787c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
7797c478bd9Sstevel@tonic-gate 					    "cp: cannot change owner and "
7807c478bd9Sstevel@tonic-gate 					    "group of %s:"), target);
7817c478bd9Sstevel@tonic-gate 					perror("");
7827c478bd9Sstevel@tonic-gate 				}
7837c478bd9Sstevel@tonic-gate 			} else {
7847c478bd9Sstevel@tonic-gate 				/*
7857c478bd9Sstevel@tonic-gate 				 * Copy the file.  If it happens to be a
7867c478bd9Sstevel@tonic-gate 				 * symlink, copy the file referenced
7877c478bd9Sstevel@tonic-gate 				 * by the symlink.
7887c478bd9Sstevel@tonic-gate 				 */
7897c478bd9Sstevel@tonic-gate 				fi = open(source, O_RDONLY);
7907c478bd9Sstevel@tonic-gate 				if (fi < 0) {
7917c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
7927c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot open %s: "),
7937c478bd9Sstevel@tonic-gate 					    cmd, source);
7947c478bd9Sstevel@tonic-gate 					perror("");
7957c478bd9Sstevel@tonic-gate 					return (1);
7967c478bd9Sstevel@tonic-gate 				}
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 				fo = creat(target, s1.st_mode & MODEBITS);
7997c478bd9Sstevel@tonic-gate 				if (fo < 0) {
8007c478bd9Sstevel@tonic-gate 					/*
8017c478bd9Sstevel@tonic-gate 					 * If -f and creat() failed, unlink
8027c478bd9Sstevel@tonic-gate 					 * and try again.
8037c478bd9Sstevel@tonic-gate 					 */
8047c478bd9Sstevel@tonic-gate 					if (fflg) {
8057c478bd9Sstevel@tonic-gate 						(void) unlink(target);
8067c478bd9Sstevel@tonic-gate 						fo = creat(target,
8077c478bd9Sstevel@tonic-gate 						    s1.st_mode & MODEBITS);
8087c478bd9Sstevel@tonic-gate 					}
8097c478bd9Sstevel@tonic-gate 				}
8107c478bd9Sstevel@tonic-gate 				if (fo < 0) {
8117c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
8127c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot create %s: "),
8137c478bd9Sstevel@tonic-gate 					    cmd, target);
8147c478bd9Sstevel@tonic-gate 					perror("");
8157c478bd9Sstevel@tonic-gate 					(void) close(fi);
8167c478bd9Sstevel@tonic-gate 					return (1);
8177c478bd9Sstevel@tonic-gate 				} else {
8187c478bd9Sstevel@tonic-gate 					/* stat the new file, its used below */
8197c478bd9Sstevel@tonic-gate 					(void) stat(target, &s2);
8207c478bd9Sstevel@tonic-gate 				}
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 				/*
8237c478bd9Sstevel@tonic-gate 				 * Set target's permissions to the source
8247c478bd9Sstevel@tonic-gate 				 * before any copying so that any partially
8257c478bd9Sstevel@tonic-gate 				 * copied file will have the source's
8267c478bd9Sstevel@tonic-gate 				 * permissions (at most) or umask permissions
8277c478bd9Sstevel@tonic-gate 				 * whichever is the most restrictive.
8287c478bd9Sstevel@tonic-gate 				 *
8297c478bd9Sstevel@tonic-gate 				 * ACL for regular files
8307c478bd9Sstevel@tonic-gate 				 */
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 				if (pflg || mve) {
8337c478bd9Sstevel@tonic-gate 					(void) chmod(target, FMODE(s1));
834fa9e4066Sahrens 					if (s1acl != NULL) {
835fa9e4066Sahrens 						if ((acl_set(target,
836fa9e4066Sahrens 						    s1acl)) < 0) {
837cee1ddadSbasabi 							error++;
838cee1ddadSbasabi 							(void) fprintf(stderr,
839cee1ddadSbasabi 							    gettext("%s: "
840cee1ddadSbasabi 							    "Failed to set "
841cee1ddadSbasabi 							    "acl entries "
842cee1ddadSbasabi 							    "on %s\n"), cmd,
8437c478bd9Sstevel@tonic-gate 							    target);
84494d2b9abSmarks 							acl_free(s1acl);
84594d2b9abSmarks 							s1acl = NULL;
8467c478bd9Sstevel@tonic-gate 							/*
8477c478bd9Sstevel@tonic-gate 							 * else: silent and
8487c478bd9Sstevel@tonic-gate 							 * continue
8497c478bd9Sstevel@tonic-gate 							 */
8507c478bd9Sstevel@tonic-gate 						}
8517c478bd9Sstevel@tonic-gate 					}
8527c478bd9Sstevel@tonic-gate 				}
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 				if (fstat(fi, &s1) < 0) {
8557c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
8567c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot access %s\n"),
8577c478bd9Sstevel@tonic-gate 					    cmd, source);
8587c478bd9Sstevel@tonic-gate 					return (1);
8597c478bd9Sstevel@tonic-gate 				}
8607c478bd9Sstevel@tonic-gate 				if (IDENTICAL(s1, s2)) {
8617c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
8627c478bd9Sstevel@tonic-gate 					    gettext(
8637c478bd9Sstevel@tonic-gate 					    "%s: %s and %s are identical\n"),
8647c478bd9Sstevel@tonic-gate 					    cmd, source, target);
8657c478bd9Sstevel@tonic-gate 					return (1);
8667c478bd9Sstevel@tonic-gate 				}
8677c478bd9Sstevel@tonic-gate 
868da6c28aaSamw 				if (writefile(fi, fo, source, target, NULL,
869da6c28aaSamw 				    NULL, &s1, &s2) != 0) {
8707c478bd9Sstevel@tonic-gate 					return (1);
8717c478bd9Sstevel@tonic-gate 				}
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 				(void) close(fi);
8747c478bd9Sstevel@tonic-gate 				if (close(fo) < 0) {
8757c478bd9Sstevel@tonic-gate 					Perror2(target, "write");
8767c478bd9Sstevel@tonic-gate 					return (1);
8777c478bd9Sstevel@tonic-gate 				}
8787c478bd9Sstevel@tonic-gate 			}
879da6c28aaSamw 			/* Copy regular extended attributes */
880da6c28aaSamw 			if (pflg || atflg || mve || saflg) {
8817c478bd9Sstevel@tonic-gate 				attret = copyattributes(source, target);
8827c478bd9Sstevel@tonic-gate 				if (attret != 0 && !attrsilent) {
8837c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
8847c478bd9Sstevel@tonic-gate 					    "%s: Failed to preserve"
8857c478bd9Sstevel@tonic-gate 					    " extended attributes of file"
8867c478bd9Sstevel@tonic-gate 					    " %s\n"), cmd, source);
8877c478bd9Sstevel@tonic-gate 				}
888da6c28aaSamw 				/* Copy extended system attributes */
889cee1ddadSbasabi 				if (pflg || mve || saflg)
890da6c28aaSamw 					sattret = copy_sysattr(source, target);
8917c478bd9Sstevel@tonic-gate 				if (mve && attret != 0) {
8927c478bd9Sstevel@tonic-gate 					(void) unlink(target);
8937c478bd9Sstevel@tonic-gate 					return (1);
8947c478bd9Sstevel@tonic-gate 				}
895da6c28aaSamw 				if (attrsilent) {
8967c478bd9Sstevel@tonic-gate 					attret = 0;
897da6c28aaSamw 				}
8987c478bd9Sstevel@tonic-gate 			}
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 			/*
9017c478bd9Sstevel@tonic-gate 			 * XPG4: the write system call will clear setgid
9027c478bd9Sstevel@tonic-gate 			 * and setuid bits, so set them again.
9037c478bd9Sstevel@tonic-gate 			 */
9047c478bd9Sstevel@tonic-gate 			if (pflg || mve) {
9057c478bd9Sstevel@tonic-gate 				if ((ret = chg_mode(target, UID(s1), GID(s1),
9067c478bd9Sstevel@tonic-gate 				    FMODE(s1))) > 0)
9077c478bd9Sstevel@tonic-gate 					return (1);
908d2443e76Smarks 				/*
909d2443e76Smarks 				 * Reapply ACL, since chmod may have
910d2443e76Smarks 				 * altered ACL
911d2443e76Smarks 				 */
912d2443e76Smarks 				if (s1acl != NULL) {
913d2443e76Smarks 					if ((acl_set(target, s1acl)) < 0) {
914cee1ddadSbasabi 						error++;
915cee1ddadSbasabi 						(void) fprintf(stderr,
916cee1ddadSbasabi 						    gettext("%s: Failed to "
917cee1ddadSbasabi 						    "set acl entries "
918cee1ddadSbasabi 						    "on %s\n"), cmd, target);
919d2443e76Smarks 						/*
920d2443e76Smarks 						 * else: silent and
921d2443e76Smarks 						 * continue
922d2443e76Smarks 						 */
923d2443e76Smarks 					}
924d2443e76Smarks 				}
9257c478bd9Sstevel@tonic-gate 				if ((ret = chg_time(target, s1)) > 0)
9267c478bd9Sstevel@tonic-gate 					return (1);
9277c478bd9Sstevel@tonic-gate 			}
9287c478bd9Sstevel@tonic-gate 			if (cpy) {
929cee1ddadSbasabi 				if (error != 0 || attret != 0 || sattret != 0)
9307c478bd9Sstevel@tonic-gate 					return (1);
9317c478bd9Sstevel@tonic-gate 				return (0);
9327c478bd9Sstevel@tonic-gate 			}
9337c478bd9Sstevel@tonic-gate 			goto cleanup;
9347c478bd9Sstevel@tonic-gate 		}
9357c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
9367c478bd9Sstevel@tonic-gate 		    gettext("%s: %s: unknown file type 0x%x\n"), cmd,
9377c478bd9Sstevel@tonic-gate 		    source, (s1.st_mode & S_IFMT));
9387c478bd9Sstevel@tonic-gate 		return (1);
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate cleanup:
9417c478bd9Sstevel@tonic-gate 		if (unlink(source) < 0) {
9427c478bd9Sstevel@tonic-gate 			(void) unlink(target);
9437c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9447c478bd9Sstevel@tonic-gate 			    gettext("%s: cannot unlink %s: "),
9457c478bd9Sstevel@tonic-gate 			    cmd, source);
9467c478bd9Sstevel@tonic-gate 			perror("");
9477c478bd9Sstevel@tonic-gate 			return (1);
9487c478bd9Sstevel@tonic-gate 		}
949cee1ddadSbasabi 		if (error != 0 || attret != 0 || sattret != 0)
950cee1ddadSbasabi 			return (1);
9517c478bd9Sstevel@tonic-gate 		return (ret);
9527c478bd9Sstevel@tonic-gate 	}
9537c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
954a77d64afScf46844 	return (ret);
9557c478bd9Sstevel@tonic-gate }
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate /*
9587c478bd9Sstevel@tonic-gate  * create_tnode()
9597c478bd9Sstevel@tonic-gate  *
9607c478bd9Sstevel@tonic-gate  * Create a node for use with the search tree which contains the
9617c478bd9Sstevel@tonic-gate  * inode information (device id and inode number).
9627c478bd9Sstevel@tonic-gate  *
9637c478bd9Sstevel@tonic-gate  * Input
9647c478bd9Sstevel@tonic-gate  *	dev	- device id
9657c478bd9Sstevel@tonic-gate  *	ino	- inode number
9667c478bd9Sstevel@tonic-gate  *
9677c478bd9Sstevel@tonic-gate  * Output
9687c478bd9Sstevel@tonic-gate  *	tnode	- NULL on error, otherwise returns a tnode structure
9697c478bd9Sstevel@tonic-gate  *		  which contains the input device id and inode number.
9707c478bd9Sstevel@tonic-gate  */
9717c478bd9Sstevel@tonic-gate static tree_node_t *
9727c478bd9Sstevel@tonic-gate create_tnode(dev_t dev, ino_t ino)
9737c478bd9Sstevel@tonic-gate {
9747c478bd9Sstevel@tonic-gate 	tree_node_t	*tnode;
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 	if ((tnode = (tree_node_t *)malloc(sizeof (tree_node_t))) != NULL) {
9777c478bd9Sstevel@tonic-gate 		tnode->node_dev = dev;
9787c478bd9Sstevel@tonic-gate 		tnode->node_ino = ino;
9797c478bd9Sstevel@tonic-gate 	}
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	return (tnode);
9827c478bd9Sstevel@tonic-gate }
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate static int
9857c478bd9Sstevel@tonic-gate chkfiles(char *source, char **to)
9867c478bd9Sstevel@tonic-gate {
9877c478bd9Sstevel@tonic-gate 	char	*buf = (char *)NULL;
9887c478bd9Sstevel@tonic-gate 	int	(*statf)() = (cpy &&
9897c478bd9Sstevel@tonic-gate 	    !(Pflg || (Hflg && !cmdarg))) ? stat : lstat;
9907c478bd9Sstevel@tonic-gate 	char    *target = *to;
991fa9e4066Sahrens 	int	error;
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	/*
9947c478bd9Sstevel@tonic-gate 	 * Make sure source file exists.
9957c478bd9Sstevel@tonic-gate 	 */
9967c478bd9Sstevel@tonic-gate 	if ((*statf)(source, &s1) < 0) {
9977c478bd9Sstevel@tonic-gate 		/*
9987c478bd9Sstevel@tonic-gate 		 * Keep the old error message except when someone tries to
9997c478bd9Sstevel@tonic-gate 		 * mv/cp/ln a symbolic link that has a trailing slash and
10007c478bd9Sstevel@tonic-gate 		 * points to a file.
10017c478bd9Sstevel@tonic-gate 		 */
10027c478bd9Sstevel@tonic-gate 		if (errno == ENOTDIR)
10037c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s: %s\n", cmd, source,
10047c478bd9Sstevel@tonic-gate 			    strerror(errno));
10057c478bd9Sstevel@tonic-gate 		else
10067c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
10077c478bd9Sstevel@tonic-gate 			    gettext("%s: cannot access %s\n"), cmd, source);
10087c478bd9Sstevel@tonic-gate 		return (1);
10097c478bd9Sstevel@tonic-gate 	}
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 	/*
1012ee9c203bSRenaud Manus 	 * Get ACL info: don't bother with ln or cp/mv'ing symlinks
10137c478bd9Sstevel@tonic-gate 	 */
1014ee9c203bSRenaud Manus 	if (!lnk && !ISLNK(s1)) {
1015fa9e4066Sahrens 		if (s1acl != NULL) {
1016fa9e4066Sahrens 			acl_free(s1acl);
1017fa9e4066Sahrens 			s1acl = NULL;
10187c478bd9Sstevel@tonic-gate 		}
1019fa9e4066Sahrens 		if ((error = acl_get(source, ACL_NO_TRIVIAL, &s1acl)) != 0) {
10207c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1021fa9e4066Sahrens 			    "%s: failed to get acl entries: %s\n", source,
1022fa9e4066Sahrens 			    acl_strerror(error));
10237c478bd9Sstevel@tonic-gate 			return (1);
10247c478bd9Sstevel@tonic-gate 		}
10257c478bd9Sstevel@tonic-gate 		/* else: just permission bits */
10267c478bd9Sstevel@tonic-gate 	}
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	/*
10297c478bd9Sstevel@tonic-gate 	 * If stat fails, then the target doesn't exist,
10307c478bd9Sstevel@tonic-gate 	 * we will create a new target with default file type of regular.
10317c478bd9Sstevel@tonic-gate 	 */
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	FTYPE(s2) = S_IFREG;
10347c478bd9Sstevel@tonic-gate 	targetexists = 0;
10357c478bd9Sstevel@tonic-gate 	if ((*statf)(target, &s2) >= 0) {
10367c478bd9Sstevel@tonic-gate 		if (ISLNK(s2))
10377c478bd9Sstevel@tonic-gate 			(void) stat(target, &s2);
10387c478bd9Sstevel@tonic-gate 		/*
10397c478bd9Sstevel@tonic-gate 		 * If target is a directory,
10407c478bd9Sstevel@tonic-gate 		 * make complete name of new file
10417c478bd9Sstevel@tonic-gate 		 * within that directory.
10427c478bd9Sstevel@tonic-gate 		 */
10437c478bd9Sstevel@tonic-gate 		if (ISDIR(s2)) {
10447c478bd9Sstevel@tonic-gate 			size_t len;
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 			len = strlen(target) + strlen(dname(source)) + 4;
10477c478bd9Sstevel@tonic-gate 			if ((buf = (char *)malloc(len)) == NULL) {
10487c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10497c478bd9Sstevel@tonic-gate 				    gettext("%s: Insufficient memory to "
10507c478bd9Sstevel@tonic-gate 				    "%s %s\n "), cmd, cmd, source);
10517c478bd9Sstevel@tonic-gate 				exit(3);
10527c478bd9Sstevel@tonic-gate 			}
10537c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, len, "%s/%s",
10547c478bd9Sstevel@tonic-gate 			    target, dname(source));
10557c478bd9Sstevel@tonic-gate 			*to = target = buf;
10567c478bd9Sstevel@tonic-gate 		}
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 		if ((*statf)(target, &s2) >= 0) {
10597c478bd9Sstevel@tonic-gate 			int overwrite	= FALSE;
10607c478bd9Sstevel@tonic-gate 			int override	= FALSE;
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 			targetexists++;
10637c478bd9Sstevel@tonic-gate 			if (cpy || mve) {
10647c478bd9Sstevel@tonic-gate 				/*
10657c478bd9Sstevel@tonic-gate 				 * For cp and mv, it is an error if the
10667c478bd9Sstevel@tonic-gate 				 * source and target are the same file.
10677c478bd9Sstevel@tonic-gate 				 * Check for the same inode and file
10687c478bd9Sstevel@tonic-gate 				 * system, but don't check for the same
10697c478bd9Sstevel@tonic-gate 				 * absolute pathname because it is an
10707c478bd9Sstevel@tonic-gate 				 * error when the source and target are
10717c478bd9Sstevel@tonic-gate 				 * hard links to the same file.
10727c478bd9Sstevel@tonic-gate 				 */
10737c478bd9Sstevel@tonic-gate 				if (IDENTICAL(s1, s2)) {
10747c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
10757c478bd9Sstevel@tonic-gate 					    gettext(
10767c478bd9Sstevel@tonic-gate 					    "%s: %s and %s are identical\n"),
10777c478bd9Sstevel@tonic-gate 					    cmd, source, target);
10787c478bd9Sstevel@tonic-gate 					if (buf != NULL)
10797c478bd9Sstevel@tonic-gate 						free(buf);
10807c478bd9Sstevel@tonic-gate 					return (1);
10817c478bd9Sstevel@tonic-gate 				}
10827c478bd9Sstevel@tonic-gate 			}
10837c478bd9Sstevel@tonic-gate 			if (lnk) {
10847c478bd9Sstevel@tonic-gate 				/*
10857c478bd9Sstevel@tonic-gate 				 * For ln, it is an error if the source and
10867c478bd9Sstevel@tonic-gate 				 * target are identical files (same inode,
10877c478bd9Sstevel@tonic-gate 				 * same file system, and filenames resolve
10887c478bd9Sstevel@tonic-gate 				 * to same absolute pathname).
10897c478bd9Sstevel@tonic-gate 				 */
10907c478bd9Sstevel@tonic-gate 				if (!chk_different(source, target)) {
10917c478bd9Sstevel@tonic-gate 					if (buf != NULL)
10927c478bd9Sstevel@tonic-gate 						free(buf);
10937c478bd9Sstevel@tonic-gate 					return (1);
10947c478bd9Sstevel@tonic-gate 				}
10957c478bd9Sstevel@tonic-gate 			}
10967c478bd9Sstevel@tonic-gate 			if (lnk && !silent) {
10977c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10987c478bd9Sstevel@tonic-gate 				    gettext("%s: %s: File exists\n"),
10997c478bd9Sstevel@tonic-gate 				    cmd, target);
11007c478bd9Sstevel@tonic-gate 				if (buf != NULL)
11017c478bd9Sstevel@tonic-gate 					free(buf);
11027c478bd9Sstevel@tonic-gate 				return (1);
11037c478bd9Sstevel@tonic-gate 			}
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 			/*
11067c478bd9Sstevel@tonic-gate 			 * overwrite:
11077c478bd9Sstevel@tonic-gate 			 * If the user does not have access to
11087c478bd9Sstevel@tonic-gate 			 * the target, ask ----if it is not
11097c478bd9Sstevel@tonic-gate 			 * silent and user invoked command
11107c478bd9Sstevel@tonic-gate 			 * interactively.
11117c478bd9Sstevel@tonic-gate 			 *
11127c478bd9Sstevel@tonic-gate 			 * override:
11137c478bd9Sstevel@tonic-gate 			 * If not silent, and stdin is a terminal, and
11147c478bd9Sstevel@tonic-gate 			 * there's no write access, and the file isn't a
11157c478bd9Sstevel@tonic-gate 			 * symbolic link, ask for permission.
11167c478bd9Sstevel@tonic-gate 			 *
11177c478bd9Sstevel@tonic-gate 			 * XPG4: both overwrite and override:
11187c478bd9Sstevel@tonic-gate 			 * ask only one question.
11197c478bd9Sstevel@tonic-gate 			 *
11207c478bd9Sstevel@tonic-gate 			 * TRANSLATION_NOTE - The following messages will
11217c478bd9Sstevel@tonic-gate 			 * contain the first character of the strings for
11227c478bd9Sstevel@tonic-gate 			 * "yes" and "no" defined in the file
11237c478bd9Sstevel@tonic-gate 			 * "nl_langinfo.po".  After substitution, the
11247c478bd9Sstevel@tonic-gate 			 * message will appear as follows:
11257c478bd9Sstevel@tonic-gate 			 *	<cmd>: overwrite <filename> (y/n)?
11267c478bd9Sstevel@tonic-gate 			 * where <cmd> is the name of the command
11277c478bd9Sstevel@tonic-gate 			 * (cp, mv) and <filename> is the destination file
11287c478bd9Sstevel@tonic-gate 			 */
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 			overwrite = iflg && !silent && use_stdin();
11327c478bd9Sstevel@tonic-gate 			override = !cpy && (access(target, 2) < 0) &&
11337c478bd9Sstevel@tonic-gate 			    !silent && use_stdin() && !ISLNK(s2);
11347c478bd9Sstevel@tonic-gate 
11350f6e7ba6Sas145665 			if (overwrite && override) {
11367c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
11377c478bd9Sstevel@tonic-gate 				    gettext("%s: overwrite %s and override "
11387c478bd9Sstevel@tonic-gate 				    "protection %o (%s/%s)? "), cmd, target,
11393d63ea05Sas145665 				    FMODE(s2) & MODEBITS, yesstr, nostr);
11403d63ea05Sas145665 				if (yes() == 0) {
11410f6e7ba6Sas145665 					if (buf != NULL)
11420f6e7ba6Sas145665 						free(buf);
11430f6e7ba6Sas145665 					return (2);
11440f6e7ba6Sas145665 				}
11450f6e7ba6Sas145665 			} else if (overwrite && ISREG(s2)) {
11467c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
11477c478bd9Sstevel@tonic-gate 				    gettext("%s: overwrite %s (%s/%s)? "),
11483d63ea05Sas145665 				    cmd, target, yesstr, nostr);
11493d63ea05Sas145665 				if (yes() == 0) {
11500f6e7ba6Sas145665 					if (buf != NULL)
11510f6e7ba6Sas145665 						free(buf);
11520f6e7ba6Sas145665 					return (2);
11530f6e7ba6Sas145665 				}
11540f6e7ba6Sas145665 			} else if (override) {
11557c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
11567c478bd9Sstevel@tonic-gate 				    gettext("%s: %s: override protection "
11577c478bd9Sstevel@tonic-gate 				    /*CSTYLED*/
11587c478bd9Sstevel@tonic-gate 				    "%o (%s/%s)? "),
11597c478bd9Sstevel@tonic-gate 				    /*CSTYLED*/
11607c478bd9Sstevel@tonic-gate 				    cmd, target, FMODE(s2) & MODEBITS,
11613d63ea05Sas145665 				    yesstr, nostr);
11623d63ea05Sas145665 				if (yes() == 0) {
11637c478bd9Sstevel@tonic-gate 					if (buf != NULL)
11647c478bd9Sstevel@tonic-gate 						free(buf);
11657c478bd9Sstevel@tonic-gate 					return (2);
11667c478bd9Sstevel@tonic-gate 				}
11677c478bd9Sstevel@tonic-gate 			}
11680f6e7ba6Sas145665 
11697c478bd9Sstevel@tonic-gate 			if (lnk && unlink(target) < 0) {
11707c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
11717c478bd9Sstevel@tonic-gate 				    gettext("%s: cannot unlink %s: "),
11727c478bd9Sstevel@tonic-gate 				    cmd, target);
11737c478bd9Sstevel@tonic-gate 				perror("");
11747c478bd9Sstevel@tonic-gate 				return (1);
11757c478bd9Sstevel@tonic-gate 			}
11767c478bd9Sstevel@tonic-gate 		}
11777c478bd9Sstevel@tonic-gate 	}
11787c478bd9Sstevel@tonic-gate 	return (0);
11797c478bd9Sstevel@tonic-gate }
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate /*
11827c478bd9Sstevel@tonic-gate  * check whether source and target are different
11837c478bd9Sstevel@tonic-gate  * return 1 when they are different
11847c478bd9Sstevel@tonic-gate  * return 0 when they are identical, or when unable to resolve a pathname
11857c478bd9Sstevel@tonic-gate  */
11867c478bd9Sstevel@tonic-gate static int
11877c478bd9Sstevel@tonic-gate chk_different(char *source, char *target)
11887c478bd9Sstevel@tonic-gate {
11897c478bd9Sstevel@tonic-gate 	char	rtarget[PATH_MAX], rsource[PATH_MAX];
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 	if (IDENTICAL(s1, s2)) {
11927c478bd9Sstevel@tonic-gate 		/*
11937c478bd9Sstevel@tonic-gate 		 * IDENTICAL will be true for hard links, therefore
11947c478bd9Sstevel@tonic-gate 		 * check whether the filenames are different
11957c478bd9Sstevel@tonic-gate 		 */
11967c478bd9Sstevel@tonic-gate 		if ((getrealpath(source, rsource) == 0) ||
11977c478bd9Sstevel@tonic-gate 		    (getrealpath(target, rtarget) == 0)) {
11987c478bd9Sstevel@tonic-gate 			return (0);
11997c478bd9Sstevel@tonic-gate 		}
12007c478bd9Sstevel@tonic-gate 		if (strncmp(rsource, rtarget, PATH_MAX) == 0) {
12017c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
12027c478bd9Sstevel@tonic-gate 			    "%s: %s and %s are identical\n"),
12037c478bd9Sstevel@tonic-gate 			    cmd, source, target);
12047c478bd9Sstevel@tonic-gate 			return (0);
12057c478bd9Sstevel@tonic-gate 		}
12067c478bd9Sstevel@tonic-gate 	}
12077c478bd9Sstevel@tonic-gate 	return (1);
12087c478bd9Sstevel@tonic-gate }
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate /*
12117c478bd9Sstevel@tonic-gate  * get real path (resolved absolute pathname)
12127c478bd9Sstevel@tonic-gate  * return 1 on success, 0 on failure
12137c478bd9Sstevel@tonic-gate  */
12147c478bd9Sstevel@tonic-gate static int
12157c478bd9Sstevel@tonic-gate getrealpath(char *path, char *rpath)
12167c478bd9Sstevel@tonic-gate {
12177c478bd9Sstevel@tonic-gate 	if (realpath(path, rpath) == NULL) {
12187c478bd9Sstevel@tonic-gate 		int	errno_save = errno;
12197c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
1220e1636a06Sdanny webster 		    "%s: cannot resolve path %s: "), cmd, path);
12217c478bd9Sstevel@tonic-gate 		errno = errno_save;
12227c478bd9Sstevel@tonic-gate 		perror("");
12237c478bd9Sstevel@tonic-gate 		return (0);
12247c478bd9Sstevel@tonic-gate 	}
12257c478bd9Sstevel@tonic-gate 	return (1);
12267c478bd9Sstevel@tonic-gate }
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate static int
12297c478bd9Sstevel@tonic-gate rcopy(char *from, char *to)
12307c478bd9Sstevel@tonic-gate {
12317c478bd9Sstevel@tonic-gate 	DIR *fold = opendir(from);
12327c478bd9Sstevel@tonic-gate 	struct dirent *dp;
12337c478bd9Sstevel@tonic-gate 	struct stat statb, s1save;
12347c478bd9Sstevel@tonic-gate 	int errs = 0;
12357c478bd9Sstevel@tonic-gate 	char fromname[PATH_MAX];
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 	if (fold == 0 || ((pflg || mve) && fstat(fold->dd_fd, &statb) < 0)) {
12387c478bd9Sstevel@tonic-gate 		Perror(from);
12397c478bd9Sstevel@tonic-gate 		return (1);
12407c478bd9Sstevel@tonic-gate 	}
12417c478bd9Sstevel@tonic-gate 	if (pflg || mve) {
12427c478bd9Sstevel@tonic-gate 		/*
12437c478bd9Sstevel@tonic-gate 		 * Save s1 (stat information for source dir) so that
12447c478bd9Sstevel@tonic-gate 		 * mod and access times can be reserved during "cp -p"
12457c478bd9Sstevel@tonic-gate 		 * or mv, since s1 gets overwritten.
12467c478bd9Sstevel@tonic-gate 		 */
12477c478bd9Sstevel@tonic-gate 		s1save = s1;
12487c478bd9Sstevel@tonic-gate 	}
12497c478bd9Sstevel@tonic-gate 	for (;;) {
12507c478bd9Sstevel@tonic-gate 		dp = readdir(fold);
12517c478bd9Sstevel@tonic-gate 		if (dp == 0) {
12527c478bd9Sstevel@tonic-gate 			(void) closedir(fold);
12537c478bd9Sstevel@tonic-gate 			if (pflg || mve)
12547c478bd9Sstevel@tonic-gate 				return (chg_time(to, s1save) + errs);
12557c478bd9Sstevel@tonic-gate 			return (errs);
12567c478bd9Sstevel@tonic-gate 		}
12577c478bd9Sstevel@tonic-gate 		if (dp->d_ino == 0)
12587c478bd9Sstevel@tonic-gate 			continue;
12597c478bd9Sstevel@tonic-gate 		if ((strcmp(dp->d_name, ".") == 0) ||
12607c478bd9Sstevel@tonic-gate 		    (strcmp(dp->d_name, "..") == 0))
12617c478bd9Sstevel@tonic-gate 			continue;
12627c478bd9Sstevel@tonic-gate 		if (strlen(from)+1+strlen(dp->d_name) >=
12637c478bd9Sstevel@tonic-gate 		    sizeof (fromname) - 1) {
12647c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
12657c478bd9Sstevel@tonic-gate 			    gettext("%s : %s/%s: Name too long\n"),
12667c478bd9Sstevel@tonic-gate 			    cmd, from, dp->d_name);
12677c478bd9Sstevel@tonic-gate 			errs++;
12687c478bd9Sstevel@tonic-gate 			continue;
12697c478bd9Sstevel@tonic-gate 		}
12707c478bd9Sstevel@tonic-gate 		(void) snprintf(fromname, sizeof (fromname),
12717c478bd9Sstevel@tonic-gate 		    "%s/%s", from, dp->d_name);
12727c478bd9Sstevel@tonic-gate 		errs += cpymve(fromname, to);
12737c478bd9Sstevel@tonic-gate 	}
12747c478bd9Sstevel@tonic-gate }
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate static char *
12777c478bd9Sstevel@tonic-gate dname(char *name)
12787c478bd9Sstevel@tonic-gate {
12797c478bd9Sstevel@tonic-gate 	register char *p;
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 	/*
12827c478bd9Sstevel@tonic-gate 	 * Return just the file name given the complete path.
12837c478bd9Sstevel@tonic-gate 	 * Like basename(1).
12847c478bd9Sstevel@tonic-gate 	 */
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 	p = name;
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate 	/*
12897c478bd9Sstevel@tonic-gate 	 * While there are characters left,
12907c478bd9Sstevel@tonic-gate 	 * set name to start after last
12917c478bd9Sstevel@tonic-gate 	 * delimiter.
12927c478bd9Sstevel@tonic-gate 	 */
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 	while (*p)
12957c478bd9Sstevel@tonic-gate 		if (*p++ == DELIM && *p)
12967c478bd9Sstevel@tonic-gate 			name = p;
12977c478bd9Sstevel@tonic-gate 	return (name);
12987c478bd9Sstevel@tonic-gate }
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate static void
13017c478bd9Sstevel@tonic-gate usage(void)
13027c478bd9Sstevel@tonic-gate {
13037c478bd9Sstevel@tonic-gate 	/*
13047c478bd9Sstevel@tonic-gate 	 * Display usage message.
13057c478bd9Sstevel@tonic-gate 	 */
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate 	if (mve) {
13087c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
13097c478bd9Sstevel@tonic-gate 		    "Usage: mv [-f] [-i] f1 f2\n"
13107c478bd9Sstevel@tonic-gate 		    "       mv [-f] [-i] f1 ... fn d1\n"
13117c478bd9Sstevel@tonic-gate 		    "       mv [-f] [-i] d1 d2\n"));
13127c478bd9Sstevel@tonic-gate 	} else if (lnk) {
13137c478bd9Sstevel@tonic-gate #ifdef XPG4
13143d63ea05Sas145665 		(void) fprintf(stderr, gettext(
13157c478bd9Sstevel@tonic-gate 		    "Usage: ln [-f] [-s] f1 [f2]\n"
13167c478bd9Sstevel@tonic-gate 		    "       ln [-f] [-s] f1 ... fn d1\n"
13177c478bd9Sstevel@tonic-gate 		    "       ln [-f] -s d1 d2\n"));
13187c478bd9Sstevel@tonic-gate #else
13193d63ea05Sas145665 		(void) fprintf(stderr, gettext(
13207c478bd9Sstevel@tonic-gate 		    "Usage: ln [-f] [-n] [-s] f1 [f2]\n"
13217c478bd9Sstevel@tonic-gate 		    "       ln [-f] [-n] [-s] f1 ... fn d1\n"
13227c478bd9Sstevel@tonic-gate 		    "       ln [-f] [-n] -s d1 d2\n"));
13237c478bd9Sstevel@tonic-gate #endif
13247c478bd9Sstevel@tonic-gate 	} else if (cpy) {
13257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
1326da6c28aaSamw 		    "Usage: cp [-f] [-i] [-p] [-@] [-/] f1 f2\n"
1327da6c28aaSamw 		    "       cp [-f] [-i] [-p] [-@] [-/] f1 ... fn d1\n"
1328da6c28aaSamw 		    "       cp -r|-R [-H|-L|-P] [-f] [-i] [-p] [-@] [-/] "
13297c478bd9Sstevel@tonic-gate 		    "d1 ... dn-1 dn\n"));
13307c478bd9Sstevel@tonic-gate 	}
13317c478bd9Sstevel@tonic-gate 	exit(2);
13327c478bd9Sstevel@tonic-gate }
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate /*
13357c478bd9Sstevel@tonic-gate  * chg_time()
13367c478bd9Sstevel@tonic-gate  *
13377c478bd9Sstevel@tonic-gate  * Try to preserve modification and access time.
13387c478bd9Sstevel@tonic-gate  * If 1) pflg is not set, or 2) pflg is set and this is the Solaris version,
133978cca7e2SRoger A. Faulkner  * don't report a utimensat() failure.
134078cca7e2SRoger A. Faulkner  * If this is the XPG4 version and utimensat fails, if 1) pflg is set (cp -p)
13417c478bd9Sstevel@tonic-gate  * or 2) we are doing a mv, print a diagnostic message; arrange for a non-zero
13427c478bd9Sstevel@tonic-gate  * exit status only if pflg is set.
134378cca7e2SRoger A. Faulkner  * utimensat(2) is being used to achieve granularity in nanoseconds
134478cca7e2SRoger A. Faulkner  * (if supported by the underlying file system) while setting file times.
13457c478bd9Sstevel@tonic-gate  */
13467c478bd9Sstevel@tonic-gate static int
13477c478bd9Sstevel@tonic-gate chg_time(char *to, struct stat ss)
13487c478bd9Sstevel@tonic-gate {
134978cca7e2SRoger A. Faulkner 	struct timespec times[2];
13507c478bd9Sstevel@tonic-gate 	int rc;
13517c478bd9Sstevel@tonic-gate 
135278cca7e2SRoger A. Faulkner 	times[0] = ss.st_atim;
135378cca7e2SRoger A. Faulkner 	times[1] = ss.st_mtim;
13547c478bd9Sstevel@tonic-gate 
1355*6cdb7222SAlexander Eremin 	rc = utimensat(AT_FDCWD, to, times,
1356*6cdb7222SAlexander Eremin 	    ISLNK(s1) ? AT_SYMLINK_NOFOLLOW : 0);
13577c478bd9Sstevel@tonic-gate #ifdef XPG4
13587c478bd9Sstevel@tonic-gate 	if ((pflg || mve) && rc != 0) {
13597c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13607c478bd9Sstevel@tonic-gate 		    gettext("%s: cannot set times for %s: "), cmd, to);
13617c478bd9Sstevel@tonic-gate 		perror("");
13627c478bd9Sstevel@tonic-gate 		if (pflg)
13637c478bd9Sstevel@tonic-gate 			return (1);
13647c478bd9Sstevel@tonic-gate 	}
13657c478bd9Sstevel@tonic-gate #endif
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 	return (0);
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate }
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate /*
13727c478bd9Sstevel@tonic-gate  * chg_mode()
13737c478bd9Sstevel@tonic-gate  *
13747c478bd9Sstevel@tonic-gate  * This function is called upon "cp -p" or mv across filesystems.
13757c478bd9Sstevel@tonic-gate  *
13767c478bd9Sstevel@tonic-gate  * Try to preserve the owner and group id.  If chown() fails,
13777c478bd9Sstevel@tonic-gate  * only print a diagnostic message if doing a mv in the XPG4 version;
13787c478bd9Sstevel@tonic-gate  * try to clear S_ISUID and S_ISGID bits in the target.  If unable to clear
13797c478bd9Sstevel@tonic-gate  * S_ISUID and S_ISGID bits, print a diagnostic message and arrange for a
13807c478bd9Sstevel@tonic-gate  * non-zero exit status because this is a security violation.
13817c478bd9Sstevel@tonic-gate  * Try to preserve permissions.
13827c478bd9Sstevel@tonic-gate  * If this is the XPG4 version and chmod() fails, print a diagnostic message
13837c478bd9Sstevel@tonic-gate  * and arrange for a non-zero exit status.
13847c478bd9Sstevel@tonic-gate  * If this is the Solaris version and chmod() fails, do not print a
13857c478bd9Sstevel@tonic-gate  * diagnostic message or exit with a non-zero value.
13867c478bd9Sstevel@tonic-gate  */
13877c478bd9Sstevel@tonic-gate static int
13887c478bd9Sstevel@tonic-gate chg_mode(char *target, uid_t uid, gid_t gid, mode_t mode)
13897c478bd9Sstevel@tonic-gate {
13907c478bd9Sstevel@tonic-gate 	int clearflg = 0; /* controls message printed upon chown() error */
1391*6cdb7222SAlexander Eremin 	struct stat st;
1392*6cdb7222SAlexander Eremin 
1393*6cdb7222SAlexander Eremin 	/* Don't change mode if target is symlink */
1394*6cdb7222SAlexander Eremin 	if (lstat(target, &st) == 0 && ISLNK(st))
1395*6cdb7222SAlexander Eremin 		return (0);
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 	if (chown(target, uid, gid) != 0) {
13987c478bd9Sstevel@tonic-gate #ifdef XPG4
13997c478bd9Sstevel@tonic-gate 		if (mve) {
14007c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: cannot change"
14017c478bd9Sstevel@tonic-gate 			    " owner and group of %s: "), cmd, target);
14027c478bd9Sstevel@tonic-gate 			perror("");
14037c478bd9Sstevel@tonic-gate 		}
14047c478bd9Sstevel@tonic-gate #endif
14057c478bd9Sstevel@tonic-gate 		if (mode & (S_ISUID | S_ISGID)) {
14067c478bd9Sstevel@tonic-gate 			/* try to clear S_ISUID and S_ISGID */
14077c478bd9Sstevel@tonic-gate 			mode &= ~S_ISUID & ~S_ISGID;
14087c478bd9Sstevel@tonic-gate 			++clearflg;
14097c478bd9Sstevel@tonic-gate 		}
14107c478bd9Sstevel@tonic-gate 	}
14117c478bd9Sstevel@tonic-gate 	if (chmod(target, mode) != 0) {
14127c478bd9Sstevel@tonic-gate 		if (clearflg) {
14137c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
14147c478bd9Sstevel@tonic-gate 			    "%s: cannot clear S_ISUID and S_ISGID bits in"
14157c478bd9Sstevel@tonic-gate 			    " %s: "), cmd, target);
14167c478bd9Sstevel@tonic-gate 			perror("");
14177c478bd9Sstevel@tonic-gate 			/* cp -p should get non-zero exit; mv should not */
14187c478bd9Sstevel@tonic-gate 			if (pflg)
14197c478bd9Sstevel@tonic-gate 				return (1);
14207c478bd9Sstevel@tonic-gate 		}
14217c478bd9Sstevel@tonic-gate #ifdef XPG4
14227c478bd9Sstevel@tonic-gate 		else {
14237c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
14247c478bd9Sstevel@tonic-gate 			"%s: cannot set permissions for %s: "), cmd, target);
14257c478bd9Sstevel@tonic-gate 			perror("");
14267c478bd9Sstevel@tonic-gate 			/* cp -p should get non-zero exit; mv should not */
14277c478bd9Sstevel@tonic-gate 			if (pflg)
14287c478bd9Sstevel@tonic-gate 				return (1);
14297c478bd9Sstevel@tonic-gate 		}
14307c478bd9Sstevel@tonic-gate #endif
14317c478bd9Sstevel@tonic-gate 	}
14327c478bd9Sstevel@tonic-gate 	return (0);
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate }
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate static void
14377c478bd9Sstevel@tonic-gate Perror(char *s)
14387c478bd9Sstevel@tonic-gate {
14397c478bd9Sstevel@tonic-gate 	char buf[PATH_MAX + 10];
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "%s: %s", cmd, s);
14427c478bd9Sstevel@tonic-gate 	perror(buf);
14437c478bd9Sstevel@tonic-gate }
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate static void
14467c478bd9Sstevel@tonic-gate Perror2(char *s1, char *s2)
14477c478bd9Sstevel@tonic-gate {
14487c478bd9Sstevel@tonic-gate 	char buf[PATH_MAX + 20];
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "%s: %s: %s",
14517c478bd9Sstevel@tonic-gate 	    cmd, gettext(s1), gettext(s2));
14527c478bd9Sstevel@tonic-gate 	perror(buf);
14537c478bd9Sstevel@tonic-gate }
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate /*
14567c478bd9Sstevel@tonic-gate  * used for cp -R and for mv across file systems
14577c478bd9Sstevel@tonic-gate  */
14587c478bd9Sstevel@tonic-gate static int
14597c478bd9Sstevel@tonic-gate copydir(char *source, char *target)
14607c478bd9Sstevel@tonic-gate {
14617c478bd9Sstevel@tonic-gate 	int ret, attret = 0;
1462cee1ddadSbasabi 	int sattret = 0;
14637c478bd9Sstevel@tonic-gate 	int pret = 0;		/* need separate flag if -p is specified */
14647c478bd9Sstevel@tonic-gate 	mode_t	fixmode = (mode_t)0;	/* cleanup mode after copy */
14657c478bd9Sstevel@tonic-gate 	struct stat s1save;
1466fa9e4066Sahrens 	acl_t  *s1acl_save;
1467cee1ddadSbasabi 	int error = 0;
1468fa9e4066Sahrens 
1469fa9e4066Sahrens 	s1acl_save = NULL;
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 	if (cpy && !rflg) {
14727c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
14737c478bd9Sstevel@tonic-gate 		    gettext("%s: %s: is a directory\n"), cmd, source);
14747c478bd9Sstevel@tonic-gate 		return (1);
14757c478bd9Sstevel@tonic-gate 	}
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate 	if (stat(target, &s2) < 0) {
14787c478bd9Sstevel@tonic-gate 		if (mkdir(target, (s1.st_mode & MODEBITS)) < 0) {
14797c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: ", cmd);
14807c478bd9Sstevel@tonic-gate 			perror(target);
14817c478bd9Sstevel@tonic-gate 			return (1);
14827c478bd9Sstevel@tonic-gate 		}
14837c478bd9Sstevel@tonic-gate 		if (stat(target, &s2) == 0) {
14847c478bd9Sstevel@tonic-gate 			fixmode = s2.st_mode;
14857c478bd9Sstevel@tonic-gate 		} else {
14867c478bd9Sstevel@tonic-gate 			fixmode = s1.st_mode;
14877c478bd9Sstevel@tonic-gate 		}
14887c478bd9Sstevel@tonic-gate 		(void) chmod(target, ((fixmode & MODEBITS) | S_IRWXU));
14897c478bd9Sstevel@tonic-gate 	} else if (!(ISDIR(s2))) {
14907c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
14917c478bd9Sstevel@tonic-gate 		    gettext("%s: %s: not a directory.\n"), cmd, target);
14927c478bd9Sstevel@tonic-gate 		return (1);
14937c478bd9Sstevel@tonic-gate 	}
14947c478bd9Sstevel@tonic-gate 	if (pflg || mve) {
14957c478bd9Sstevel@tonic-gate 		/*
14967c478bd9Sstevel@tonic-gate 		 * Save s1 (stat information for source dir) and acl info,
14977c478bd9Sstevel@tonic-gate 		 * if any, so that ownership, modes, times, and acl's can
14987c478bd9Sstevel@tonic-gate 		 * be reserved during "cp -p" or mv.
14997c478bd9Sstevel@tonic-gate 		 * s1 gets overwritten when doing the recursive copy.
15007c478bd9Sstevel@tonic-gate 		 */
15017c478bd9Sstevel@tonic-gate 		s1save = s1;
1502fa9e4066Sahrens 		if (s1acl != NULL) {
1503fa9e4066Sahrens 			s1acl_save = acl_dup(s1acl);
1504fa9e4066Sahrens 			if (s1acl_save == NULL) {
1505fa9e4066Sahrens 				(void) fprintf(stderr, gettext("%s: "
1506fa9e4066Sahrens 				    "Insufficient memory to save acl"
1507fa9e4066Sahrens 				    " entry\n"), cmd);
1508fa9e4066Sahrens 				if (pflg)
1509fa9e4066Sahrens 					return (1);
1510fa9e4066Sahrens 
15117c478bd9Sstevel@tonic-gate 			}
15123d63ea05Sas145665 #ifdef XPG4
15133d63ea05Sas145665 			else {
15143d63ea05Sas145665 				(void) fprintf(stderr, gettext("%s: "
15153d63ea05Sas145665 				    "Insufficient memory to save acl"
15163d63ea05Sas145665 				    " entry\n"), cmd);
15173d63ea05Sas145665 				if (pflg)
15183d63ea05Sas145665 					return (1);
15193d63ea05Sas145665 			}
15207c478bd9Sstevel@tonic-gate #endif
15217c478bd9Sstevel@tonic-gate 		}
15227c478bd9Sstevel@tonic-gate 	}
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 	ret = rcopy(source, target);
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate 	/*
15277c478bd9Sstevel@tonic-gate 	 * Once we created a directory, go ahead and set
15287c478bd9Sstevel@tonic-gate 	 * its attributes, e.g. acls and time. The info
15297c478bd9Sstevel@tonic-gate 	 * may get overwritten if we continue traversing
15307c478bd9Sstevel@tonic-gate 	 * down the tree.
15317c478bd9Sstevel@tonic-gate 	 *
15327c478bd9Sstevel@tonic-gate 	 * ACL for directory
15337c478bd9Sstevel@tonic-gate 	 */
15347c478bd9Sstevel@tonic-gate 	if (pflg || mve) {
1535d2443e76Smarks 		if ((pret = chg_mode(target, UID(s1save), GID(s1save),
1536d2443e76Smarks 		    FMODE(s1save))) == 0)
1537d2443e76Smarks 			pret = chg_time(target, s1save);
1538d2443e76Smarks 		ret += pret;
1539fa9e4066Sahrens 		if (s1acl_save != NULL) {
1540fa9e4066Sahrens 			if (acl_set(target, s1acl_save) < 0) {
1541cee1ddadSbasabi 				error++;
15427c478bd9Sstevel@tonic-gate #ifdef XPG4
15437c478bd9Sstevel@tonic-gate 				if (pflg || mve) {
15447c478bd9Sstevel@tonic-gate #else
15457c478bd9Sstevel@tonic-gate 				if (pflg) {
15467c478bd9Sstevel@tonic-gate #endif
15477c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
15487c478bd9Sstevel@tonic-gate 					    "%s: failed to set acl entries "
15497c478bd9Sstevel@tonic-gate 					    "on %s\n"), cmd, target);
15507c478bd9Sstevel@tonic-gate 					if (pflg) {
1551fa9e4066Sahrens 						acl_free(s1acl_save);
1552fa9e4066Sahrens 						s1acl_save = NULL;
15537c478bd9Sstevel@tonic-gate 						ret++;
15547c478bd9Sstevel@tonic-gate 					}
15557c478bd9Sstevel@tonic-gate 				}
15567c478bd9Sstevel@tonic-gate 				/* else: silent and continue */
15577c478bd9Sstevel@tonic-gate 			}
1558fa9e4066Sahrens 			acl_free(s1acl_save);
1559fa9e4066Sahrens 			s1acl_save = NULL;
15607c478bd9Sstevel@tonic-gate 		}
15617c478bd9Sstevel@tonic-gate 	} else if (fixmode != (mode_t)0)
15627c478bd9Sstevel@tonic-gate 		(void) chmod(target, fixmode & MODEBITS);
15637c478bd9Sstevel@tonic-gate 
1564da6c28aaSamw 	if (pflg || atflg || mve || saflg) {
15657c478bd9Sstevel@tonic-gate 		attret = copyattributes(source, target);
15667c478bd9Sstevel@tonic-gate 		if (!attrsilent && attret != 0) {
15677c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: Failed to preserve"
15687c478bd9Sstevel@tonic-gate 			    " extended attributes of directory"
15697c478bd9Sstevel@tonic-gate 			    " %s\n"), cmd, source);
15707c478bd9Sstevel@tonic-gate 		} else {
15717c478bd9Sstevel@tonic-gate 			/*
15727c478bd9Sstevel@tonic-gate 			 * Otherwise ignore failure.
15737c478bd9Sstevel@tonic-gate 			 */
15747c478bd9Sstevel@tonic-gate 			attret = 0;
15757c478bd9Sstevel@tonic-gate 		}
1576da6c28aaSamw 		/* Copy extended system attributes */
1577da6c28aaSamw 		if (pflg || mve || saflg) {
1578cee1ddadSbasabi 			sattret = copy_sysattr(source, target);
1579cee1ddadSbasabi 			if (sattret != 0) {
1580da6c28aaSamw 				(void) fprintf(stderr, gettext(
1581da6c28aaSamw 				    "%s: Failed to preserve "
1582da6c28aaSamw 				    "extended system attributes "
1583da6c28aaSamw 				    "of directory %s\n"), cmd, source);
1584da6c28aaSamw 			}
1585da6c28aaSamw 		}
15867c478bd9Sstevel@tonic-gate 	}
1587cee1ddadSbasabi 	if (attret != 0 || sattret != 0 || error != 0)
1588cee1ddadSbasabi 		return (1);
15897c478bd9Sstevel@tonic-gate 	return (ret);
15907c478bd9Sstevel@tonic-gate }
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate static int
15937c478bd9Sstevel@tonic-gate copyspecial(char *target)
15947c478bd9Sstevel@tonic-gate {
15957c478bd9Sstevel@tonic-gate 	int ret = 0;
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate 	if (mknod(target, s1.st_mode, s1.st_rdev) != 0) {
15987c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
15997c478bd9Sstevel@tonic-gate 		    "cp: cannot create special file %s: "), target);
16007c478bd9Sstevel@tonic-gate 		perror("");
16017c478bd9Sstevel@tonic-gate 		return (1);
16027c478bd9Sstevel@tonic-gate 	}
16037c478bd9Sstevel@tonic-gate 
16047c478bd9Sstevel@tonic-gate 	if (pflg) {
16057c478bd9Sstevel@tonic-gate 		if ((ret = chg_mode(target, UID(s1), GID(s1), FMODE(s1))) == 0)
16067c478bd9Sstevel@tonic-gate 			ret = chg_time(target, s1);
16077c478bd9Sstevel@tonic-gate 	}
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	return (ret);
16107c478bd9Sstevel@tonic-gate }
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate static int
16137c478bd9Sstevel@tonic-gate use_stdin(void)
16147c478bd9Sstevel@tonic-gate {
16157c478bd9Sstevel@tonic-gate #ifdef XPG4
16167c478bd9Sstevel@tonic-gate 	return (1);
16177c478bd9Sstevel@tonic-gate #else
16187c478bd9Sstevel@tonic-gate 	return (isatty(fileno(stdin)));
16197c478bd9Sstevel@tonic-gate #endif
16207c478bd9Sstevel@tonic-gate }
16217c478bd9Sstevel@tonic-gate 
1622da6c28aaSamw /* Copy non-system extended attributes */
1623da6c28aaSamw 
16247c478bd9Sstevel@tonic-gate static int
16257c478bd9Sstevel@tonic-gate copyattributes(char *source, char *target)
16267c478bd9Sstevel@tonic-gate {
16277c478bd9Sstevel@tonic-gate 	struct dirent *dp;
16287c478bd9Sstevel@tonic-gate 	int error = 0;
1629fa9e4066Sahrens 	int aclerror;
16307c478bd9Sstevel@tonic-gate 	mode_t mode;
16317c478bd9Sstevel@tonic-gate 	int clearflg = 0;
1632fa9e4066Sahrens 	acl_t *xacl = NULL;
1633fa9e4066Sahrens 	acl_t *attrdiracl = NULL;
163478cca7e2SRoger A. Faulkner 	struct timespec times[2];
16357c478bd9Sstevel@tonic-gate 
1636da6c28aaSamw 
16377c478bd9Sstevel@tonic-gate 	if (pathconf(source,  _PC_XATTR_EXISTS) != 1)
16387c478bd9Sstevel@tonic-gate 		return (0);
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 	if (pathconf(target, _PC_XATTR_ENABLED) != 1) {
16417c478bd9Sstevel@tonic-gate 		if (!attrsilent) {
16427c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
16437c478bd9Sstevel@tonic-gate 			    gettext(
16447c478bd9Sstevel@tonic-gate 			    "%s: cannot preserve extended attributes, "
16457c478bd9Sstevel@tonic-gate 			    "operation not supported on file"
16467c478bd9Sstevel@tonic-gate 			    " %s\n"), cmd, target);
16477c478bd9Sstevel@tonic-gate 		}
16487c478bd9Sstevel@tonic-gate 		return (1);
16497c478bd9Sstevel@tonic-gate 	}
1650da6c28aaSamw 	if (open_source(source) != 0)
1651da6c28aaSamw 		return (1);
1652da6c28aaSamw 	if (open_target_srctarg_attrdirs(source, target) !=  0)
1653da6c28aaSamw 		return (1);
1654da6c28aaSamw 	if (open_attrdirp(source) != 0)
1655da6c28aaSamw 		return (1);
16567c478bd9Sstevel@tonic-gate 
16577c478bd9Sstevel@tonic-gate 	if (pflg || mve) {
16587c478bd9Sstevel@tonic-gate 		if (fchmod(targetdirfd, attrdir.st_mode) == -1) {
16597c478bd9Sstevel@tonic-gate 			if (!attrsilent) {
16607c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
16617c478bd9Sstevel@tonic-gate 				    gettext("%s: failed to set file mode"
16627c478bd9Sstevel@tonic-gate 				    " correctly on attribute directory of"
16637c478bd9Sstevel@tonic-gate 				    " file %s: "), cmd, target);
16647c478bd9Sstevel@tonic-gate 				perror("");
16657c478bd9Sstevel@tonic-gate 				++error;
16667c478bd9Sstevel@tonic-gate 			}
16677c478bd9Sstevel@tonic-gate 		}
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 		if (fchown(targetdirfd, attrdir.st_uid, attrdir.st_gid) == -1) {
16707c478bd9Sstevel@tonic-gate 			if (!attrsilent) {
16717c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
16727c478bd9Sstevel@tonic-gate 				    gettext("%s: failed to set file"
16737c478bd9Sstevel@tonic-gate 				    " ownership correctly on attribute"
16747c478bd9Sstevel@tonic-gate 				    " directory of file %s: "), cmd, target);
16757c478bd9Sstevel@tonic-gate 				perror("");
16767c478bd9Sstevel@tonic-gate 				++error;
16777c478bd9Sstevel@tonic-gate 			}
16787c478bd9Sstevel@tonic-gate 		}
16797c478bd9Sstevel@tonic-gate 		/*
16807c478bd9Sstevel@tonic-gate 		 * Now that we are the owner we can update st_ctime by calling
168178cca7e2SRoger A. Faulkner 		 * utimensat.
16827c478bd9Sstevel@tonic-gate 		 */
168378cca7e2SRoger A. Faulkner 		times[0] = attrdir.st_atim;
168478cca7e2SRoger A. Faulkner 		times[1] = attrdir.st_mtim;
168578cca7e2SRoger A. Faulkner 		if (utimensat(targetdirfd, ".", times, 0) < 0) {
16867c478bd9Sstevel@tonic-gate 			if (!attrsilent) {
16877c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
16887c478bd9Sstevel@tonic-gate 				    gettext("%s: cannot set attribute times"
16897c478bd9Sstevel@tonic-gate 				    " for %s: "), cmd, target);
16907c478bd9Sstevel@tonic-gate 				perror("");
16917c478bd9Sstevel@tonic-gate 				++error;
16927c478bd9Sstevel@tonic-gate 			}
16937c478bd9Sstevel@tonic-gate 		}
16947c478bd9Sstevel@tonic-gate 
16957c478bd9Sstevel@tonic-gate 		/*
16967c478bd9Sstevel@tonic-gate 		 * Now set owner and group of attribute directory, implies
16977c478bd9Sstevel@tonic-gate 		 * changing the ACL of the hidden attribute directory first.
16987c478bd9Sstevel@tonic-gate 		 */
1699fa9e4066Sahrens 		if ((aclerror = facl_get(sourcedirfd,
1700fa9e4066Sahrens 		    ACL_NO_TRIVIAL, &attrdiracl)) != 0) {
17017c478bd9Sstevel@tonic-gate 			if (!attrsilent) {
17027c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
17037c478bd9Sstevel@tonic-gate 				    "%s: failed to get acl entries of"
17047c478bd9Sstevel@tonic-gate 				    " attribute directory for"
1705fa9e4066Sahrens 				    " %s : %s\n"), cmd,
1706fa9e4066Sahrens 				    source, acl_strerror(aclerror));
17077c478bd9Sstevel@tonic-gate 				++error;
17087c478bd9Sstevel@tonic-gate 			}
17097c478bd9Sstevel@tonic-gate 		}
17107c478bd9Sstevel@tonic-gate 
1711fa9e4066Sahrens 		if (attrdiracl) {
1712fa9e4066Sahrens 			if (facl_set(targetdirfd, attrdiracl) != 0) {
17137c478bd9Sstevel@tonic-gate 				if (!attrsilent) {
17147c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
17157c478bd9Sstevel@tonic-gate 					"%s: failed to set acl entries"
17167c478bd9Sstevel@tonic-gate 					" on attribute directory "
17177c478bd9Sstevel@tonic-gate 					"for %s\n"), cmd, target);
17187c478bd9Sstevel@tonic-gate 					++error;
17197c478bd9Sstevel@tonic-gate 				}
1720fa9e4066Sahrens 				acl_free(attrdiracl);
1721fa9e4066Sahrens 				attrdiracl = NULL;
17227c478bd9Sstevel@tonic-gate 			}
17237c478bd9Sstevel@tonic-gate 		}
17247c478bd9Sstevel@tonic-gate 	}
17257c478bd9Sstevel@tonic-gate 
1726da6c28aaSamw 	while ((dp = readdir(srcdirp)) != NULL) {
1727da6c28aaSamw 		int ret;
17287c478bd9Sstevel@tonic-gate 
1729da6c28aaSamw 		if ((ret = traverse_attrfile(dp, source, target, 1)) == -1)
17307c478bd9Sstevel@tonic-gate 			continue;
1731da6c28aaSamw 		else if (ret > 0) {
17327c478bd9Sstevel@tonic-gate 			++error;
1733da6c28aaSamw 			goto out;
17347c478bd9Sstevel@tonic-gate 		}
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate 		if (pflg || mve) {
1737fa9e4066Sahrens 			if ((aclerror = facl_get(srcattrfd,
1738fa9e4066Sahrens 			    ACL_NO_TRIVIAL, &xacl)) != 0) {
17397c478bd9Sstevel@tonic-gate 				if (!attrsilent) {
17407c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
17417c478bd9Sstevel@tonic-gate 					    "%s: failed to get acl entries of"
17427c478bd9Sstevel@tonic-gate 					    " attribute %s for"
1743fa9e4066Sahrens 					    " %s: %s"), cmd, dp->d_name,
1744fa9e4066Sahrens 					    source, acl_strerror(aclerror));
17457c478bd9Sstevel@tonic-gate 					++error;
17467c478bd9Sstevel@tonic-gate 				}
17477c478bd9Sstevel@tonic-gate 			}
17487c478bd9Sstevel@tonic-gate 		}
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate 		/*
17517c478bd9Sstevel@tonic-gate 		 * preserve ACL
17527c478bd9Sstevel@tonic-gate 		 */
1753fa9e4066Sahrens 		if ((pflg || mve) && xacl != NULL) {
1754fa9e4066Sahrens 			if ((facl_set(targattrfd, xacl)) < 0) {
17557c478bd9Sstevel@tonic-gate 				if (!attrsilent) {
17567c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
17577c478bd9Sstevel@tonic-gate 					    "%s: failed to set acl entries on"
17587c478bd9Sstevel@tonic-gate 					    " attribute %s for"
17597c478bd9Sstevel@tonic-gate 					    "%s\n"), cmd, dp->d_name, target);
17607c478bd9Sstevel@tonic-gate 					++error;
17617c478bd9Sstevel@tonic-gate 				}
1762fa9e4066Sahrens 				acl_free(xacl);
1763fa9e4066Sahrens 				xacl = NULL;
17647c478bd9Sstevel@tonic-gate 			}
17657c478bd9Sstevel@tonic-gate 		}
17667c478bd9Sstevel@tonic-gate 
1767da6c28aaSamw 		if (writefile(srcattrfd, targattrfd, source, target,
1768da6c28aaSamw 		    dp->d_name, dp->d_name, &s3, &s4) != 0) {
17697c478bd9Sstevel@tonic-gate 			if (!attrsilent) {
17707c478bd9Sstevel@tonic-gate 				++error;
17717c478bd9Sstevel@tonic-gate 			}
17727c478bd9Sstevel@tonic-gate 			goto next;
17737c478bd9Sstevel@tonic-gate 		}
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 		if (pflg || mve) {
17767c478bd9Sstevel@tonic-gate 			mode = FMODE(s3);
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 			if (fchown(targattrfd, UID(s3), GID(s3)) != 0) {
17797c478bd9Sstevel@tonic-gate 				if (!attrsilent) {
17807c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
17817c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot change"
17827c478bd9Sstevel@tonic-gate 					    " owner and group of"
17837c478bd9Sstevel@tonic-gate 					    " attribute %s for" " file"
17847c478bd9Sstevel@tonic-gate 					    " %s: "), cmd, dp->d_name, target);
17857c478bd9Sstevel@tonic-gate 					perror("");
17867c478bd9Sstevel@tonic-gate 					++error;
17877c478bd9Sstevel@tonic-gate 				}
17887c478bd9Sstevel@tonic-gate 				if (mode & (S_ISUID | S_ISGID)) {
17897c478bd9Sstevel@tonic-gate 					/* try to clear S_ISUID and S_ISGID */
17907c478bd9Sstevel@tonic-gate 					mode &= ~S_ISUID & ~S_ISGID;
17917c478bd9Sstevel@tonic-gate 					++clearflg;
17927c478bd9Sstevel@tonic-gate 				}
17937c478bd9Sstevel@tonic-gate 			}
179478cca7e2SRoger A. Faulkner 			times[0] = s3.st_atim;
179578cca7e2SRoger A. Faulkner 			times[1] = s3.st_mtim;
179678cca7e2SRoger A. Faulkner 			if (utimensat(targetdirfd, dp->d_name, times, 0) < 0) {
17977c478bd9Sstevel@tonic-gate 				if (!attrsilent) {
17987c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
17997c478bd9Sstevel@tonic-gate 					    gettext("%s: cannot set attribute"
18007c478bd9Sstevel@tonic-gate 					    " times for %s: "), cmd, target);
18017c478bd9Sstevel@tonic-gate 					perror("");
18027c478bd9Sstevel@tonic-gate 					++error;
18037c478bd9Sstevel@tonic-gate 				}
18047c478bd9Sstevel@tonic-gate 			}
18057c478bd9Sstevel@tonic-gate 			if (fchmod(targattrfd, mode) != 0) {
18067c478bd9Sstevel@tonic-gate 				if (clearflg) {
18077c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
18087c478bd9Sstevel@tonic-gate 					    "%s: cannot clear S_ISUID and "
18097c478bd9Sstevel@tonic-gate 					    "S_ISGID bits in attribute %s"
18107c478bd9Sstevel@tonic-gate 					    " for file"
18117c478bd9Sstevel@tonic-gate 					    " %s: "), cmd, dp->d_name, target);
18127c478bd9Sstevel@tonic-gate 				} else {
18137c478bd9Sstevel@tonic-gate 					if (!attrsilent) {
18147c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
18157c478bd9Sstevel@tonic-gate 						    gettext(
18167c478bd9Sstevel@tonic-gate 				"%s: cannot set permissions of attribute"
18177c478bd9Sstevel@tonic-gate 				" %s for %s: "), cmd, dp->d_name, target);
18187c478bd9Sstevel@tonic-gate 						perror("");
18197c478bd9Sstevel@tonic-gate 						++error;
18207c478bd9Sstevel@tonic-gate 					}
18217c478bd9Sstevel@tonic-gate 				}
18227c478bd9Sstevel@tonic-gate 			}
1823d2443e76Smarks 			if (xacl && ((facl_set(targattrfd, xacl)) < 0)) {
1824d2443e76Smarks 				if (!attrsilent) {
1825d2443e76Smarks 					(void) fprintf(stderr, gettext(
1826d2443e76Smarks 					    "%s: failed to set acl entries on"
1827d2443e76Smarks 					    " attribute %s for"
1828d2443e76Smarks 					    "%s\n"), cmd, dp->d_name, target);
1829d2443e76Smarks 					++error;
1830d2443e76Smarks 				}
1831d2443e76Smarks 				acl_free(xacl);
1832d2443e76Smarks 				xacl = NULL;
1833d2443e76Smarks 			}
18347c478bd9Sstevel@tonic-gate 		}
18357c478bd9Sstevel@tonic-gate next:
1836fa9e4066Sahrens 		if (xacl != NULL) {
1837fa9e4066Sahrens 			acl_free(xacl);
1838fa9e4066Sahrens 			xacl = NULL;
18397c478bd9Sstevel@tonic-gate 		}
18407c478bd9Sstevel@tonic-gate 		if (srcattrfd != -1)
18417c478bd9Sstevel@tonic-gate 			(void) close(srcattrfd);
18427c478bd9Sstevel@tonic-gate 		if (targattrfd != -1)
18437c478bd9Sstevel@tonic-gate 			(void) close(targattrfd);
18447c478bd9Sstevel@tonic-gate 		srcattrfd = targattrfd = -1;
18457c478bd9Sstevel@tonic-gate 	}
18467c478bd9Sstevel@tonic-gate out:
1847fa9e4066Sahrens 	if (xacl != NULL) {
1848fa9e4066Sahrens 		acl_free(xacl);
1849fa9e4066Sahrens 		xacl = NULL;
1850fa9e4066Sahrens 	}
1851fa9e4066Sahrens 	if (attrdiracl != NULL) {
1852fa9e4066Sahrens 		acl_free(attrdiracl);
1853fa9e4066Sahrens 		attrdiracl = NULL;
1854fa9e4066Sahrens 	}
1855da6c28aaSamw 
1856da6c28aaSamw 	if (!saflg && !pflg && !mve)
1857da6c28aaSamw 		close_all();
1858da6c28aaSamw 	return (error == 0 ? 0 : 1);
1859da6c28aaSamw }
1860da6c28aaSamw 
1861da6c28aaSamw /* Copy extended system attributes from source to target */
1862da6c28aaSamw 
1863da6c28aaSamw static int
1864da6c28aaSamw copy_sysattr(char *source, char *target)
1865da6c28aaSamw {
1866da6c28aaSamw 	struct dirent	*dp;
1867da6c28aaSamw 	nvlist_t	*response;
1868da6c28aaSamw 	int		error = 0;
18692b9240ecSbasabi 	int		target_sa_support = 0;
1870da6c28aaSamw 
1871da6c28aaSamw 	if (sysattr_support(source, _PC_SATTR_EXISTS) != 1)
1872da6c28aaSamw 		return (0);
1873da6c28aaSamw 
1874da6c28aaSamw 	if (open_source(source) != 0)
1875da6c28aaSamw 		return (1);
1876da6c28aaSamw 
1877da6c28aaSamw 	/*
1878da6c28aaSamw 	 * Gets non default extended system attributes from the
1879da6c28aaSamw 	 * source file to copy to the target. The target has
1880da6c28aaSamw 	 * the defaults set when its created and thus  no need
1881da6c28aaSamw 	 * to copy the defaults.
1882da6c28aaSamw 	 */
1883da6c28aaSamw 	response = sysattr_list(cmd, srcfd, source);
1884da6c28aaSamw 
18852b9240ecSbasabi 	if (sysattr_support(target, _PC_SATTR_ENABLED) != 1) {
18862b9240ecSbasabi 		if (response != NULL) {
1887da6c28aaSamw 			(void) fprintf(stderr,
1888da6c28aaSamw 			    gettext(
1889da6c28aaSamw 			    "%s: cannot preserve extended system "
1890da6c28aaSamw 			    "attribute, operation not supported on file"
1891da6c28aaSamw 			    " %s\n"), cmd, target);
1892da6c28aaSamw 			error++;
1893da6c28aaSamw 			goto out;
1894da6c28aaSamw 		}
18952b9240ecSbasabi 	} else {
18962b9240ecSbasabi 		target_sa_support = 1;
18972b9240ecSbasabi 	}
1898da6c28aaSamw 
18992b9240ecSbasabi 	if (target_sa_support) {
1900da6c28aaSamw 		if (srcdirp == NULL) {
19012b9240ecSbasabi 			if (open_target_srctarg_attrdirs(source,
19022b9240ecSbasabi 			    target) !=  0) {
1903da6c28aaSamw 				error++;
1904da6c28aaSamw 				goto out;
1905da6c28aaSamw 			}
1906da6c28aaSamw 			if (open_attrdirp(source) != 0) {
1907da6c28aaSamw 				error++;
1908da6c28aaSamw 				goto out;
1909da6c28aaSamw 			}
1910da6c28aaSamw 		} else {
1911da6c28aaSamw 			rewind_attrdir(srcdirp);
1912da6c28aaSamw 		}
19132b9240ecSbasabi 		while ((dp = readdir(srcdirp)) != NULL) {
1914da6c28aaSamw 			nvlist_t	*res;
1915da6c28aaSamw 			int		ret;
1916da6c28aaSamw 
19172b9240ecSbasabi 			if ((ret = traverse_attrfile(dp, source, target,
19182b9240ecSbasabi 			    0)) == -1)
1919da6c28aaSamw 				continue;
1920da6c28aaSamw 			else if (ret > 0) {
1921da6c28aaSamw 				++error;
1922da6c28aaSamw 				goto out;
1923da6c28aaSamw 			}
1924da6c28aaSamw 			/*
1925da6c28aaSamw 			 * Gets non default extended system attributes from the
1926da6c28aaSamw 			 * attribute file to copy to the target. The target has
1927da6c28aaSamw 			 * the defaults set when its created and thus  no need
1928da6c28aaSamw 			 * to copy the defaults.
1929da6c28aaSamw 			 */
1930da6c28aaSamw 			if (dp->d_name != NULL) {
1931da6c28aaSamw 				res = sysattr_list(cmd, srcattrfd, dp->d_name);
1932da6c28aaSamw 				if (res == NULL)
1933da6c28aaSamw 					goto next;
1934da6c28aaSamw 
1935da6c28aaSamw 			/*
1936da6c28aaSamw 			 * Copy non default extended system attributes of named
1937da6c28aaSamw 			 * attribute file.
1938da6c28aaSamw 			 */
19392b9240ecSbasabi 				if (fsetattr(targattrfd,
1940da6c28aaSamw 				    XATTR_VIEW_READWRITE, res) != 0) {
1941da6c28aaSamw 					++error;
1942da6c28aaSamw 					(void) fprintf(stderr, gettext("%s: "
1943da6c28aaSamw 					    "Failed to copy extended system "
1944da6c28aaSamw 					    "attributes from attribute file "
1945da6c28aaSamw 					    "%s of %s to %s\n"), cmd,
1946da6c28aaSamw 					    dp->d_name, source, target);
1947da6c28aaSamw 				}
1948da6c28aaSamw 			}
1949da6c28aaSamw next:
1950da6c28aaSamw 			if (srcattrfd != -1)
1951da6c28aaSamw 				(void) close(srcattrfd);
1952da6c28aaSamw 			if (targattrfd != -1)
1953da6c28aaSamw 				(void) close(targattrfd);
1954da6c28aaSamw 			srcattrfd = targattrfd = -1;
1955da6c28aaSamw 			if (res != NULL)
1956da6c28aaSamw 				nvlist_free(res);
1957da6c28aaSamw 		}
19582b9240ecSbasabi 	}
1959da6c28aaSamw 	/* Copy source file non default extended system attributes to target */
19602b9240ecSbasabi 	if (target_sa_support && (response != NULL) &&
1961da6c28aaSamw 	    (fsetattr(targfd, XATTR_VIEW_READWRITE, response)) != 0) {
1962da6c28aaSamw 		++error;
1963da6c28aaSamw 		(void) fprintf(stderr, gettext("%s: Failed to "
1964da6c28aaSamw 		    "copy extended system attributes from "
1965da6c28aaSamw 		    "%s to %s\n"), cmd, source, target);
1966da6c28aaSamw 	}
1967da6c28aaSamw out:
1968da6c28aaSamw 	if (response != NULL)
1969da6c28aaSamw 		nvlist_free(response);
1970da6c28aaSamw 	close_all();
19717c478bd9Sstevel@tonic-gate 	return (error == 0 ? 0 : 1);
19727c478bd9Sstevel@tonic-gate }
19737c478bd9Sstevel@tonic-gate 
1974da6c28aaSamw /* Open the source file */
1975da6c28aaSamw 
1976da6c28aaSamw int
1977da6c28aaSamw open_source(char  *src)
1978da6c28aaSamw {
1979da6c28aaSamw 	int	error = 0;
1980da6c28aaSamw 
1981da6c28aaSamw 	srcfd = -1;
1982da6c28aaSamw 	if ((srcfd = open(src, O_RDONLY)) == -1) {
1983da6c28aaSamw 		if (pflg && attrsilent) {
1984da6c28aaSamw 			error++;
1985da6c28aaSamw 			goto out;
1986da6c28aaSamw 		}
1987da6c28aaSamw 		if (!attrsilent) {
1988da6c28aaSamw 			(void) fprintf(stderr,
1989da6c28aaSamw 			    gettext("%s: cannot open file"
1990da6c28aaSamw 			    " %s: "), cmd, src);
1991da6c28aaSamw 			perror("");
1992da6c28aaSamw 		}
1993da6c28aaSamw 		++error;
1994da6c28aaSamw 	}
1995da6c28aaSamw out:
1996da6c28aaSamw 	if (error)
1997da6c28aaSamw 		close_all();
1998da6c28aaSamw 	return (error == 0 ? 0 : 1);
1999da6c28aaSamw }
2000da6c28aaSamw 
2001da6c28aaSamw /* Open source attribute dir, target and target attribute dir. */
2002da6c28aaSamw 
2003da6c28aaSamw int
2004da6c28aaSamw open_target_srctarg_attrdirs(char  *src, char *targ)
2005da6c28aaSamw {
2006da6c28aaSamw 	int		error = 0;
2007da6c28aaSamw 
2008da6c28aaSamw 	targfd = sourcedirfd = targetdirfd = -1;
2009da6c28aaSamw 
2010da6c28aaSamw 	if ((targfd = open(targ, O_RDONLY)) == -1) {
2011da6c28aaSamw 		if (pflg && attrsilent) {
2012da6c28aaSamw 			error++;
2013da6c28aaSamw 			goto out;
2014da6c28aaSamw 		}
2015da6c28aaSamw 		if (!attrsilent) {
2016da6c28aaSamw 			(void) fprintf(stderr,
2017da6c28aaSamw 			    gettext("%s: cannot open file"
2018da6c28aaSamw 			    " %s: "), cmd, targ);
2019da6c28aaSamw 			perror("");
2020da6c28aaSamw 		}
2021da6c28aaSamw 		++error;
2022da6c28aaSamw 		goto out;
2023da6c28aaSamw 	}
2024da6c28aaSamw 
2025da6c28aaSamw 	if ((sourcedirfd = openat(srcfd, ".", O_RDONLY|O_XATTR)) == -1) {
2026da6c28aaSamw 		if (pflg && attrsilent) {
2027da6c28aaSamw 			error++;
2028da6c28aaSamw 			goto out;
2029da6c28aaSamw 		}
2030da6c28aaSamw 		if (!attrsilent) {
2031da6c28aaSamw 			(void) fprintf(stderr,
2032da6c28aaSamw 			    gettext("%s: cannot open attribute"
2033da6c28aaSamw 			    " directory for %s: "), cmd, src);
2034da6c28aaSamw 			perror("");
2035da6c28aaSamw 		}
2036da6c28aaSamw 		++error;
2037da6c28aaSamw 		goto out;
2038da6c28aaSamw 	}
2039da6c28aaSamw 
2040da6c28aaSamw 	if (fstat(sourcedirfd, &attrdir) == -1) {
2041da6c28aaSamw 		if (pflg && attrsilent) {
2042da6c28aaSamw 			error++;
2043da6c28aaSamw 			goto out;
2044da6c28aaSamw 		}
2045da6c28aaSamw 
2046da6c28aaSamw 		if (!attrsilent) {
2047da6c28aaSamw 			(void) fprintf(stderr,
2048da6c28aaSamw 			    gettext("%s: could not retrieve stat"
2049da6c28aaSamw 			    " information for attribute directory"
2050da6c28aaSamw 			    "of file %s: "), cmd, src);
2051da6c28aaSamw 			perror("");
2052da6c28aaSamw 		}
2053da6c28aaSamw 		++error;
2054da6c28aaSamw 		goto out;
2055da6c28aaSamw 	}
2056da6c28aaSamw 	if ((targetdirfd = openat(targfd, ".", O_RDONLY|O_XATTR)) == -1) {
2057da6c28aaSamw 		if (pflg && attrsilent) {
2058da6c28aaSamw 			error++;
2059da6c28aaSamw 			goto out;
2060da6c28aaSamw 		}
2061da6c28aaSamw 		if (!attrsilent) {
2062da6c28aaSamw 			(void) fprintf(stderr,
2063da6c28aaSamw 			    gettext("%s: cannot open attribute"
2064da6c28aaSamw 			    " directory for %s: "), cmd, targ);
2065da6c28aaSamw 			perror("");
2066da6c28aaSamw 		}
2067da6c28aaSamw 		++error;
2068da6c28aaSamw 	}
2069da6c28aaSamw out:
2070da6c28aaSamw 	if (error)
2071da6c28aaSamw 		close_all();
2072da6c28aaSamw 	return (error == 0 ? 0 : 1);
2073da6c28aaSamw }
2074da6c28aaSamw 
2075da6c28aaSamw int
2076da6c28aaSamw open_attrdirp(char *source)
2077da6c28aaSamw {
2078da6c28aaSamw 	int tmpfd = -1;
2079da6c28aaSamw 	int error = 0;
2080da6c28aaSamw 
2081da6c28aaSamw 	/*
2082da6c28aaSamw 	 * dup sourcedirfd for use by fdopendir().
2083da6c28aaSamw 	 * fdopendir will take ownership of given fd and will close
2084da6c28aaSamw 	 * it when closedir() is called.
2085da6c28aaSamw 	 */
2086da6c28aaSamw 
2087da6c28aaSamw 	if ((tmpfd = dup(sourcedirfd)) == -1) {
2088da6c28aaSamw 		if (pflg && attrsilent) {
2089da6c28aaSamw 			error++;
2090da6c28aaSamw 			goto out;
2091da6c28aaSamw 		}
2092da6c28aaSamw 		if (!attrsilent) {
2093da6c28aaSamw 			(void) fprintf(stderr,
2094da6c28aaSamw 			    gettext(
2095da6c28aaSamw 			    "%s: unable to dup attribute directory"
2096da6c28aaSamw 			    " file descriptor for %s: "), cmd, source);
2097da6c28aaSamw 			perror("");
2098da6c28aaSamw 			++error;
2099da6c28aaSamw 		}
2100da6c28aaSamw 		goto out;
2101da6c28aaSamw 	}
2102da6c28aaSamw 	if ((srcdirp = fdopendir(tmpfd)) == NULL) {
2103da6c28aaSamw 		if (pflg && attrsilent) {
2104da6c28aaSamw 			error++;
2105da6c28aaSamw 			goto out;
2106da6c28aaSamw 		}
2107da6c28aaSamw 		if (!attrsilent) {
2108da6c28aaSamw 			(void) fprintf(stderr,
2109da6c28aaSamw 			    gettext("%s: failed to open attribute"
2110da6c28aaSamw 			    " directory for %s: "), cmd, source);
2111da6c28aaSamw 			perror("");
2112da6c28aaSamw 			++error;
2113da6c28aaSamw 		}
2114da6c28aaSamw 	}
2115da6c28aaSamw out:
2116da6c28aaSamw 	if (error)
2117da6c28aaSamw 		close_all();
2118da6c28aaSamw 	return (error == 0 ? 0 : 1);
2119da6c28aaSamw }
2120da6c28aaSamw 
2121da6c28aaSamw /* Skips through ., .., and system attribute 'view' files */
2122da6c28aaSamw int
2123da6c28aaSamw traverse_attrfile(struct dirent *dp, char *source, char *target, int  first)
2124da6c28aaSamw {
2125da6c28aaSamw 	int		error = 0;
2126da6c28aaSamw 
2127beab0215Sbasabi 	srcattrfd = targattrfd = -1;
2128beab0215Sbasabi 
2129da6c28aaSamw 	if ((dp->d_name[0] == '.' && dp->d_name[1] == '\0') ||
2130da6c28aaSamw 	    (dp->d_name[0] == '.' && dp->d_name[1] == '.' &&
2131da6c28aaSamw 	    dp->d_name[2] == '\0') ||
2132da6c28aaSamw 	    (sysattr_type(dp->d_name) == _RO_SATTR) ||
2133da6c28aaSamw 	    (sysattr_type(dp->d_name) == _RW_SATTR))
2134da6c28aaSamw 		return (-1);
2135da6c28aaSamw 
2136da6c28aaSamw 	if ((srcattrfd = openat(sourcedirfd, dp->d_name,
2137da6c28aaSamw 	    O_RDONLY)) == -1) {
2138da6c28aaSamw 		if (!attrsilent) {
2139da6c28aaSamw 			(void) fprintf(stderr,
2140da6c28aaSamw 			    gettext("%s: cannot open attribute %s on"
2141da6c28aaSamw 			    " file %s: "), cmd, dp->d_name, source);
2142da6c28aaSamw 			perror("");
2143da6c28aaSamw 			++error;
2144da6c28aaSamw 			goto out;
2145da6c28aaSamw 		}
2146da6c28aaSamw 	}
2147da6c28aaSamw 
2148da6c28aaSamw 	if (fstat(srcattrfd, &s3) < 0) {
2149da6c28aaSamw 		if (!attrsilent) {
2150da6c28aaSamw 			(void) fprintf(stderr,
2151da6c28aaSamw 			    gettext("%s: could not stat attribute"
2152da6c28aaSamw 			    " %s on file"
2153da6c28aaSamw 			    " %s: "), cmd, dp->d_name, source);
2154da6c28aaSamw 			perror("");
2155da6c28aaSamw 			++error;
2156da6c28aaSamw 		}
2157da6c28aaSamw 		goto out;
2158da6c28aaSamw 	}
2159da6c28aaSamw 
2160da6c28aaSamw 	if (first) {
2161da6c28aaSamw 		(void) unlinkat(targetdirfd, dp->d_name, 0);
21623a1874bcSbasabi 		if ((targattrfd = openat(targetdirfd, dp->d_name,
21633a1874bcSbasabi 		    O_RDWR|O_CREAT|O_TRUNC, s3.st_mode & MODEBITS)) == -1) {
2164da6c28aaSamw 			if (!attrsilent) {
2165da6c28aaSamw 				(void) fprintf(stderr,
2166da6c28aaSamw 				    gettext("%s: could not create attribute"
2167da6c28aaSamw 				    " %s on file %s: "), cmd, dp->d_name,
2168da6c28aaSamw 				    target);
2169da6c28aaSamw 				perror("");
2170da6c28aaSamw 				++error;
2171da6c28aaSamw 			}
2172da6c28aaSamw 			goto out;
2173da6c28aaSamw 		}
21743a1874bcSbasabi 	} else {
21753a1874bcSbasabi 		if ((targattrfd = openat(targetdirfd, dp->d_name,
21763a1874bcSbasabi 		    O_RDONLY)) == -1) {
21773a1874bcSbasabi 			if (!attrsilent) {
21783a1874bcSbasabi 				(void) fprintf(stderr,
21793a1874bcSbasabi 				    gettext("%s: could not open attribute"
21803a1874bcSbasabi 				    " %s on file %s: "), cmd, dp->d_name,
21813a1874bcSbasabi 				    target);
21823a1874bcSbasabi 				perror("");
21833a1874bcSbasabi 				++error;
21843a1874bcSbasabi 			}
21853a1874bcSbasabi 			goto out;
21863a1874bcSbasabi 		}
21873a1874bcSbasabi 	}
21883a1874bcSbasabi 
2189da6c28aaSamw 
2190da6c28aaSamw 	if (fstat(targattrfd, &s4) < 0) {
2191da6c28aaSamw 		if (!attrsilent) {
2192da6c28aaSamw 			(void) fprintf(stderr,
2193da6c28aaSamw 			    gettext("%s: could not stat attribute"
2194da6c28aaSamw 			    " %s on file"
2195da6c28aaSamw 			    " %s: "), cmd, dp->d_name, target);
2196da6c28aaSamw 			perror("");
2197da6c28aaSamw 			++error;
2198da6c28aaSamw 		}
2199da6c28aaSamw 	}
2200da6c28aaSamw 
2201da6c28aaSamw out:
2202da6c28aaSamw 	if (error) {
2203da6c28aaSamw 		if (srcattrfd != -1)
2204da6c28aaSamw 			(void) close(srcattrfd);
2205da6c28aaSamw 		if (targattrfd != -1)
2206da6c28aaSamw 			(void) close(targattrfd);
2207beab0215Sbasabi 		srcattrfd = targattrfd = -1;
2208da6c28aaSamw 	}
2209da6c28aaSamw 	return (error == 0 ? 0 :1);
2210da6c28aaSamw }
2211da6c28aaSamw 
2212da6c28aaSamw void
2213da6c28aaSamw rewind_attrdir(DIR * sdp)
2214da6c28aaSamw {
2215da6c28aaSamw 	int pwdfd;
2216da6c28aaSamw 
2217da6c28aaSamw 	pwdfd = open(".", O_RDONLY);
2218da6c28aaSamw 	if ((pwdfd != -1) && (fchdir(sourcedirfd) == 0)) {
2219da6c28aaSamw 		rewinddir(sdp);
2220da6c28aaSamw 		(void) fchdir(pwdfd);
2221da6c28aaSamw 		(void) close(pwdfd);
2222da6c28aaSamw 	} else {
2223da6c28aaSamw 		if (!attrsilent) {
2224da6c28aaSamw 			(void) fprintf(stderr, gettext("%s: "
2225da6c28aaSamw 			    "failed to rewind attribute dir\n"),
2226da6c28aaSamw 			    cmd);
2227da6c28aaSamw 		}
2228da6c28aaSamw 	}
2229da6c28aaSamw }
2230da6c28aaSamw 
2231da6c28aaSamw void
2232da6c28aaSamw close_all()
2233da6c28aaSamw {
2234da6c28aaSamw 	if (srcattrfd != -1)
2235da6c28aaSamw 		(void) close(srcattrfd);
2236da6c28aaSamw 	if (targattrfd != -1)
2237da6c28aaSamw 		(void) close(targattrfd);
2238da6c28aaSamw 	if (sourcedirfd != -1)
2239da6c28aaSamw 		(void) close(sourcedirfd);
2240da6c28aaSamw 	if (targetdirfd != -1)
2241da6c28aaSamw 		(void) close(targetdirfd);
2242da6c28aaSamw 	if (srcdirp != NULL) {
2243da6c28aaSamw 		(void) closedir(srcdirp);
2244da6c28aaSamw 		srcdirp = NULL;
2245da6c28aaSamw 	}
2246da6c28aaSamw 	if (srcfd != -1)
2247da6c28aaSamw 		(void) close(srcfd);
2248da6c28aaSamw 	if (targfd != -1)
2249da6c28aaSamw 		(void) close(targfd);
2250da6c28aaSamw }
2251