xref: /titanic_52/usr/src/cmd/tar/tar.c (revision da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
222c0f0499Slovely  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
307c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
347c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <unistd.h>
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/param.h>
427c478bd9Sstevel@tonic-gate #include <sys/stat.h>
437c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
447c478bd9Sstevel@tonic-gate #include <sys/wait.h>
457c478bd9Sstevel@tonic-gate #include <dirent.h>
467c478bd9Sstevel@tonic-gate #include <errno.h>
477c478bd9Sstevel@tonic-gate #include <stdio.h>
487c478bd9Sstevel@tonic-gate #include <signal.h>
497c478bd9Sstevel@tonic-gate #include <ctype.h>
507c478bd9Sstevel@tonic-gate #include <locale.h>
517c478bd9Sstevel@tonic-gate #include <nl_types.h>
527c478bd9Sstevel@tonic-gate #include <langinfo.h>
537c478bd9Sstevel@tonic-gate #include <pwd.h>
547c478bd9Sstevel@tonic-gate #include <grp.h>
557c478bd9Sstevel@tonic-gate #include <fcntl.h>
567c478bd9Sstevel@tonic-gate #include <string.h>
577c478bd9Sstevel@tonic-gate #include <malloc.h>
587c478bd9Sstevel@tonic-gate #include <time.h>
597c478bd9Sstevel@tonic-gate #include <utime.h>
607c478bd9Sstevel@tonic-gate #include <stdlib.h>
617c478bd9Sstevel@tonic-gate #include <stdarg.h>
627c478bd9Sstevel@tonic-gate #include <widec.h>
637c478bd9Sstevel@tonic-gate #include <sys/mtio.h>
647c478bd9Sstevel@tonic-gate #include <sys/acl.h>
657c478bd9Sstevel@tonic-gate #include <strings.h>
667c478bd9Sstevel@tonic-gate #include <deflt.h>
677c478bd9Sstevel@tonic-gate #include <limits.h>
687c478bd9Sstevel@tonic-gate #include <iconv.h>
697c478bd9Sstevel@tonic-gate #include <assert.h>
70*da6c28aaSamw #include <libgen.h>
71*da6c28aaSamw #include <libintl.h>
72fa9e4066Sahrens #include <aclutils.h>
73*da6c28aaSamw #include <libnvpair.h>
74*da6c28aaSamw #include <archives.h>
753d63ea05Sas145665 
767c478bd9Sstevel@tonic-gate #if defined(__SunOS_5_6) || defined(__SunOS_5_7)
777c478bd9Sstevel@tonic-gate extern int defcntl();
787c478bd9Sstevel@tonic-gate #endif
79*da6c28aaSamw #if defined(_PC_SATTR_ENABLED)
80*da6c28aaSamw #include <attr.h>
81*da6c28aaSamw #include <libcmdutils.h>
82*da6c28aaSamw #endif
837c478bd9Sstevel@tonic-gate 
8445916cd2Sjpk /* Trusted Extensions */
8545916cd2Sjpk #include <zone.h>
8645916cd2Sjpk #include <tsol/label.h>
8745916cd2Sjpk #include <sys/tsol/label_macro.h>
8845916cd2Sjpk 
89*da6c28aaSamw #include "getresponse.h"
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  * Source compatibility
927c478bd9Sstevel@tonic-gate  */
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * These constants come from archives.h and sys/fcntl.h
967c478bd9Sstevel@tonic-gate  * and were introduced by the extended attributes project
977c478bd9Sstevel@tonic-gate  * in Solaris 9.
987c478bd9Sstevel@tonic-gate  */
997c478bd9Sstevel@tonic-gate #if !defined(O_XATTR)
1007c478bd9Sstevel@tonic-gate #define	AT_SYMLINK_NOFOLLOW	0x1000
1017c478bd9Sstevel@tonic-gate #define	AT_REMOVEDIR		0x1
1027c478bd9Sstevel@tonic-gate #define	AT_FDCWD		0xffd19553
1037c478bd9Sstevel@tonic-gate #define	_XATTR_HDRTYPE		'E'
1047c478bd9Sstevel@tonic-gate static int attropen();
1057c478bd9Sstevel@tonic-gate static int fstatat();
1067c478bd9Sstevel@tonic-gate static int renameat();
1077c478bd9Sstevel@tonic-gate static int unlinkat();
1087c478bd9Sstevel@tonic-gate static int openat();
1097c478bd9Sstevel@tonic-gate static int fchownat();
1107c478bd9Sstevel@tonic-gate static int futimesat();
1117c478bd9Sstevel@tonic-gate #endif
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * Compiling with -D_XPG4_2 gets this but produces other problems, so
1157c478bd9Sstevel@tonic-gate  * instead of including sys/time.h and compiling with -D_XPG4_2, I'm
1167c478bd9Sstevel@tonic-gate  * explicitly doing the declaration here.
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate int utimes(const char *path, const struct timeval timeval_ptr[]);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate #ifndef MINSIZE
1217c478bd9Sstevel@tonic-gate #define	MINSIZE 250
1227c478bd9Sstevel@tonic-gate #endif
1237c478bd9Sstevel@tonic-gate #define	DEF_FILE "/etc/default/tar"
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate #define	min(a, b)  ((a) < (b) ? (a) : (b))
1267c478bd9Sstevel@tonic-gate #define	max(a, b)  ((a) > (b) ? (a) : (b))
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /* -DDEBUG	ONLY for debugging */
1297c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1307c478bd9Sstevel@tonic-gate #undef	DEBUG
1317c478bd9Sstevel@tonic-gate #define	DEBUG(a, b, c)\
1327c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "DEBUG - "), (void) fprintf(stderr, a, b, c)
1337c478bd9Sstevel@tonic-gate #endif
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate #define	TBLOCK	512	/* tape block size--should be universal */
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate #ifdef	BSIZE
1387c478bd9Sstevel@tonic-gate #define	SYS_BLOCK BSIZE	/* from sys/param.h:  secondary block size */
1397c478bd9Sstevel@tonic-gate #else	/* BSIZE */
1407c478bd9Sstevel@tonic-gate #define	SYS_BLOCK 512	/* default if no BSIZE in param.h */
1417c478bd9Sstevel@tonic-gate #endif	/* BSIZE */
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate #define	NBLOCK	20
1447c478bd9Sstevel@tonic-gate #define	NAMSIZ	100
1457c478bd9Sstevel@tonic-gate #define	PRESIZ	155
1467c478bd9Sstevel@tonic-gate #define	MAXNAM	256
1477c478bd9Sstevel@tonic-gate #define	MODEMASK 0777777	/* file creation mode mask */
1487c478bd9Sstevel@tonic-gate #define	POSIXMODES 07777	/* mask for POSIX mode bits */
1497c478bd9Sstevel@tonic-gate #define	MAXEXT	9	/* reasonable max # extents for a file */
1507c478bd9Sstevel@tonic-gate #define	EXTMIN	50	/* min blks left on floppy to split a file */
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /* max value dblock.dbuf.efsize can store */
1537c478bd9Sstevel@tonic-gate #define	TAR_EFSIZE_MAX	 0777777777
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate  * Symbols which specify the values at which the use of the 'E' function
1577c478bd9Sstevel@tonic-gate  * modifier is required to properly store a file.
1587c478bd9Sstevel@tonic-gate  *
1597c478bd9Sstevel@tonic-gate  *     TAR_OFFSET_MAX    - the largest file size we can archive
1607c478bd9Sstevel@tonic-gate  *     OCTAL7CHAR        - the limit for ustar gid, uid, dev
1617c478bd9Sstevel@tonic-gate  */
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate #ifdef XHDR_DEBUG
1647c478bd9Sstevel@tonic-gate /* tiny values which force the creation of extended header entries */
1657c478bd9Sstevel@tonic-gate #define	TAR_OFFSET_MAX 9
1667c478bd9Sstevel@tonic-gate #define	OCTAL7CHAR 2
1677c478bd9Sstevel@tonic-gate #else
1687c478bd9Sstevel@tonic-gate /* normal values */
169b7d62af5Sceastha #define	TAR_OFFSET_MAX	077777777777ULL
1707c478bd9Sstevel@tonic-gate #define	OCTAL7CHAR	07777777
1717c478bd9Sstevel@tonic-gate #endif
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate #define	TBLOCKS(bytes)	(((bytes) + TBLOCK - 1) / TBLOCK)
1747c478bd9Sstevel@tonic-gate #define	K(tblocks)	((tblocks+1)/2)	/* tblocks to Kbytes for printing */
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate #define	MAXLEV	(PATH_MAX / 2)
1777c478bd9Sstevel@tonic-gate #define	LEV0	1
1787c478bd9Sstevel@tonic-gate #define	SYMLINK_LEV0	0
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate #define	TRUE	1
1817c478bd9Sstevel@tonic-gate #define	FALSE	0
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate #define	XATTR_FILE	1
1847c478bd9Sstevel@tonic-gate #define	NORMAL_FILE	0
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate #define	PUT_AS_LINK	1
1877c478bd9Sstevel@tonic-gate #define	PUT_NOTAS_LINK	0
1887c478bd9Sstevel@tonic-gate 
189*da6c28aaSamw #ifndef VIEW_READONLY
190*da6c28aaSamw #define	VIEW_READONLY	"SUNWattr_ro"
191*da6c28aaSamw #endif
192*da6c28aaSamw 
193*da6c28aaSamw #ifndef VIEW_READWRITE
194*da6c28aaSamw #define	VIEW_READWRITE	"SUNWattr_rw"
195*da6c28aaSamw #endif
196*da6c28aaSamw 
1977c478bd9Sstevel@tonic-gate #if _FILE_OFFSET_BITS == 64
1987c478bd9Sstevel@tonic-gate #define	FMT_off_t "lld"
1997c478bd9Sstevel@tonic-gate #define	FMT_off_t_o "llo"
2007c478bd9Sstevel@tonic-gate #define	FMT_blkcnt_t "lld"
2017c478bd9Sstevel@tonic-gate #else
2027c478bd9Sstevel@tonic-gate #define	FMT_off_t "ld"
2037c478bd9Sstevel@tonic-gate #define	FMT_off_t_o "lo"
2047c478bd9Sstevel@tonic-gate #define	FMT_blkcnt_t "ld"
2057c478bd9Sstevel@tonic-gate #endif
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /* ACL support */
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate static
2107c478bd9Sstevel@tonic-gate struct	sec_attr {
2117c478bd9Sstevel@tonic-gate 	char	attr_type;
2127c478bd9Sstevel@tonic-gate 	char	attr_len[7];
2137c478bd9Sstevel@tonic-gate 	char	attr_info[1];
2147c478bd9Sstevel@tonic-gate } *attr;
2157c478bd9Sstevel@tonic-gate 
216*da6c28aaSamw #if defined(O_XATTR)
217*da6c28aaSamw typedef enum {
218*da6c28aaSamw 	ATTR_OK,
219*da6c28aaSamw 	ATTR_SKIP,
220*da6c28aaSamw 	ATTR_CHDIR_ERR,
221*da6c28aaSamw 	ATTR_OPEN_ERR,
222*da6c28aaSamw 	ATTR_XATTR_ERR,
223*da6c28aaSamw 	ATTR_SATTR_ERR
224*da6c28aaSamw } attr_status_t;
225*da6c28aaSamw #endif
226*da6c28aaSamw 
227*da6c28aaSamw #if defined(O_XATTR)
228*da6c28aaSamw typedef enum {
229*da6c28aaSamw 	ARC_CREATE,
230*da6c28aaSamw 	ARC_RESTORE
231*da6c28aaSamw } arc_action_t;
232*da6c28aaSamw #endif
233*da6c28aaSamw 
234*da6c28aaSamw typedef struct attr_data {
235*da6c28aaSamw 	char	*attr_parent;
236*da6c28aaSamw 	char	*attr_path;
237*da6c28aaSamw 	int	attr_parentfd;
238*da6c28aaSamw 	int	attr_rw_sysattr;
239*da6c28aaSamw } attr_data_t;
240*da6c28aaSamw 
2417c478bd9Sstevel@tonic-gate /*
2427c478bd9Sstevel@tonic-gate  *
2437c478bd9Sstevel@tonic-gate  * Tar has been changed to support extended attributes.
2447c478bd9Sstevel@tonic-gate  *
2457c478bd9Sstevel@tonic-gate  * As part of this change tar now uses the new *at() syscalls
2467c478bd9Sstevel@tonic-gate  * such as openat, fchownat(), unlinkat()...
2477c478bd9Sstevel@tonic-gate  *
2487c478bd9Sstevel@tonic-gate  * This was done so that attributes can be handled with as few code changes
2497c478bd9Sstevel@tonic-gate  * as possible.
2507c478bd9Sstevel@tonic-gate  *
2517c478bd9Sstevel@tonic-gate  * What this means is that tar now opens the directory that a file or directory
2527c478bd9Sstevel@tonic-gate  * resides in and then performs *at() functions to manipulate the entry.
2537c478bd9Sstevel@tonic-gate  *
2547c478bd9Sstevel@tonic-gate  * For example a new file is now created like this:
2557c478bd9Sstevel@tonic-gate  *
2567c478bd9Sstevel@tonic-gate  * dfd = open(<some dir path>)
2577c478bd9Sstevel@tonic-gate  * fd = openat(dfd, <name>,....);
2587c478bd9Sstevel@tonic-gate  *
2597c478bd9Sstevel@tonic-gate  * or in the case of an extended attribute
2607c478bd9Sstevel@tonic-gate  *
2617c478bd9Sstevel@tonic-gate  * dfd = attropen(<pathname>, ".", ....)
2627c478bd9Sstevel@tonic-gate  *
2637c478bd9Sstevel@tonic-gate  * Once we have a directory file descriptor all of the *at() functions can
2647c478bd9Sstevel@tonic-gate  * be applied to it.
2657c478bd9Sstevel@tonic-gate  *
2667c478bd9Sstevel@tonic-gate  * unlinkat(dfd, <component name>,...)
2677c478bd9Sstevel@tonic-gate  * fchownat(dfd, <component name>,..)
2687c478bd9Sstevel@tonic-gate  *
2697c478bd9Sstevel@tonic-gate  * This works for both normal namespace files and extended attribute file
2707c478bd9Sstevel@tonic-gate  *
2717c478bd9Sstevel@tonic-gate  */
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate /*
2747c478bd9Sstevel@tonic-gate  *
2757c478bd9Sstevel@tonic-gate  * Extended attribute Format
2767c478bd9Sstevel@tonic-gate  *
2777c478bd9Sstevel@tonic-gate  * Extended attributes are stored in two pieces.
2787c478bd9Sstevel@tonic-gate  * 1. An attribute header which has information about
2797c478bd9Sstevel@tonic-gate  *    what file the attribute is for and what the attribute
2807c478bd9Sstevel@tonic-gate  *    is named.
2817c478bd9Sstevel@tonic-gate  * 2. The attribute record itself.  Stored as a normal file type
2827c478bd9Sstevel@tonic-gate  *    of entry.
2837c478bd9Sstevel@tonic-gate  * Both the header and attribute record have special modes/typeflags
2847c478bd9Sstevel@tonic-gate  * associated with them.
2857c478bd9Sstevel@tonic-gate  *
2867c478bd9Sstevel@tonic-gate  * The names of the header in the archive look like:
2877c478bd9Sstevel@tonic-gate  * /dev/null/attr.hdr
2887c478bd9Sstevel@tonic-gate  *
2897c478bd9Sstevel@tonic-gate  * The name of the attribute looks like:
2907c478bd9Sstevel@tonic-gate  * /dev/null/attr
2917c478bd9Sstevel@tonic-gate  *
2927c478bd9Sstevel@tonic-gate  * This is done so that an archiver that doesn't understand these formats
2937c478bd9Sstevel@tonic-gate  * can just dispose of the attribute records.
2947c478bd9Sstevel@tonic-gate  *
2957c478bd9Sstevel@tonic-gate  * The format is composed of a fixed size header followed
2967c478bd9Sstevel@tonic-gate  * by a variable sized xattr_buf. If the attribute is a hard link
2977c478bd9Sstevel@tonic-gate  * to another attribute then another xattr_buf section is included
2987c478bd9Sstevel@tonic-gate  * for the link.
2997c478bd9Sstevel@tonic-gate  *
3007c478bd9Sstevel@tonic-gate  * The xattr_buf is used to define the necessary "pathing" steps
3017c478bd9Sstevel@tonic-gate  * to get to the extended attribute.  This is necessary to support
3027c478bd9Sstevel@tonic-gate  * a fully recursive attribute model where an attribute may itself
3037c478bd9Sstevel@tonic-gate  * have an attribute.
3047c478bd9Sstevel@tonic-gate  *
3057c478bd9Sstevel@tonic-gate  * The basic layout looks like this.
3067c478bd9Sstevel@tonic-gate  *
3077c478bd9Sstevel@tonic-gate  *     --------------------------------
3087c478bd9Sstevel@tonic-gate  *     |                              |
3097c478bd9Sstevel@tonic-gate  *     |         xattr_hdr            |
3107c478bd9Sstevel@tonic-gate  *     |                              |
3117c478bd9Sstevel@tonic-gate  *     --------------------------------
3127c478bd9Sstevel@tonic-gate  *     --------------------------------
3137c478bd9Sstevel@tonic-gate  *     |                              |
3147c478bd9Sstevel@tonic-gate  *     |        xattr_buf             |
3157c478bd9Sstevel@tonic-gate  *     |                              |
3167c478bd9Sstevel@tonic-gate  *     --------------------------------
3177c478bd9Sstevel@tonic-gate  *     --------------------------------
3187c478bd9Sstevel@tonic-gate  *     |                              |
3197c478bd9Sstevel@tonic-gate  *     |      (optional link info)    |
3207c478bd9Sstevel@tonic-gate  *     |                              |
3217c478bd9Sstevel@tonic-gate  *     --------------------------------
3227c478bd9Sstevel@tonic-gate  *     --------------------------------
3237c478bd9Sstevel@tonic-gate  *     |                              |
3247c478bd9Sstevel@tonic-gate  *     |      attribute itself        |
3257c478bd9Sstevel@tonic-gate  *     |      stored as normal tar    |
3267c478bd9Sstevel@tonic-gate  *     |      or cpio data with       |
3277c478bd9Sstevel@tonic-gate  *     |      special mode or         |
3287c478bd9Sstevel@tonic-gate  *     |      typeflag                |
3297c478bd9Sstevel@tonic-gate  *     |                              |
3307c478bd9Sstevel@tonic-gate  *     --------------------------------
3317c478bd9Sstevel@tonic-gate  *
3327c478bd9Sstevel@tonic-gate  */
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate /*
3357c478bd9Sstevel@tonic-gate  * xattrhead is a pointer to the xattr_hdr
3367c478bd9Sstevel@tonic-gate  *
3377c478bd9Sstevel@tonic-gate  * xattrp is a pointer to the xattr_buf structure
3387c478bd9Sstevel@tonic-gate  * which contains the "pathing" steps to get to attributes
3397c478bd9Sstevel@tonic-gate  *
3407c478bd9Sstevel@tonic-gate  * xattr_linkp is a pointer to another xattr_buf structure that is
3417c478bd9Sstevel@tonic-gate  * only used when an attribute is actually linked to another attribute
3427c478bd9Sstevel@tonic-gate  *
3437c478bd9Sstevel@tonic-gate  */
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate static struct xattr_hdr *xattrhead;
3467c478bd9Sstevel@tonic-gate static struct xattr_buf *xattrp;
3477c478bd9Sstevel@tonic-gate static struct xattr_buf *xattr_linkp;	/* pointer to link info, if any */
348*da6c28aaSamw static char *xattrapath;		/* attribute name */
3497c478bd9Sstevel@tonic-gate static char *xattr_linkaname;		/* attribute attribute is linked to */
3507c478bd9Sstevel@tonic-gate static char Hiddendir;			/* are we processing hidden xattr dir */
3517c478bd9Sstevel@tonic-gate static char xattrbadhead;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate /* Was statically allocated tbuf[NBLOCK] */
3547c478bd9Sstevel@tonic-gate static
3557c478bd9Sstevel@tonic-gate union hblock {
3567c478bd9Sstevel@tonic-gate 	char dummy[TBLOCK];
3577c478bd9Sstevel@tonic-gate 	struct header {
3587c478bd9Sstevel@tonic-gate 		char name[NAMSIZ];	/* If non-null prefix, path is	*/
3597c478bd9Sstevel@tonic-gate 					/* <prefix>/<name>;  otherwise	*/
3607c478bd9Sstevel@tonic-gate 					/* <name>			*/
3617c478bd9Sstevel@tonic-gate 		char mode[8];
3627c478bd9Sstevel@tonic-gate 		char uid[8];
3637c478bd9Sstevel@tonic-gate 		char gid[8];
3647c478bd9Sstevel@tonic-gate 		char size[12];		/* size of this extent if file split */
3657c478bd9Sstevel@tonic-gate 		char mtime[12];
3667c478bd9Sstevel@tonic-gate 		char chksum[8];
3677c478bd9Sstevel@tonic-gate 		char typeflag;
3687c478bd9Sstevel@tonic-gate 		char linkname[NAMSIZ];
3697c478bd9Sstevel@tonic-gate 		char magic[6];
3707c478bd9Sstevel@tonic-gate 		char version[2];
3717c478bd9Sstevel@tonic-gate 		char uname[32];
3727c478bd9Sstevel@tonic-gate 		char gname[32];
3737c478bd9Sstevel@tonic-gate 		char devmajor[8];
3747c478bd9Sstevel@tonic-gate 		char devminor[8];
3757c478bd9Sstevel@tonic-gate 		char prefix[PRESIZ];	/* Together with "name", the path of */
3767c478bd9Sstevel@tonic-gate 					/* the file:  <prefix>/<name>	*/
3777c478bd9Sstevel@tonic-gate 		char extno;		/* extent #, null if not split */
3787c478bd9Sstevel@tonic-gate 		char extotal;		/* total extents */
3797c478bd9Sstevel@tonic-gate 		char efsize[10];	/* size of entire file */
3807c478bd9Sstevel@tonic-gate 	} dbuf;
3817c478bd9Sstevel@tonic-gate } dblock, *tbuf, xhdr_buf;
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate static
3847c478bd9Sstevel@tonic-gate struct xtar_hdr {
3857c478bd9Sstevel@tonic-gate 	uid_t		x_uid,		/* Uid of file */
3867c478bd9Sstevel@tonic-gate 			x_gid;		/* Gid of file */
3877c478bd9Sstevel@tonic-gate 	major_t		x_devmajor;	/* Device major node */
3887c478bd9Sstevel@tonic-gate 	minor_t		x_devminor;	/* Device minor node */
3897c478bd9Sstevel@tonic-gate 	off_t		x_filesz;	/* Length of file */
3907c478bd9Sstevel@tonic-gate 	char		*x_uname,	/* Pointer to name of user */
3917c478bd9Sstevel@tonic-gate 			*x_gname,	/* Pointer to gid of user */
3927c478bd9Sstevel@tonic-gate 			*x_linkpath,	/* Path for a hard/symbolic link */
3937c478bd9Sstevel@tonic-gate 			*x_path;	/* Path of file */
3947c478bd9Sstevel@tonic-gate 	timestruc_t	x_mtime;	/* Seconds and nanoseconds */
3957c478bd9Sstevel@tonic-gate } Xtarhdr;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate static
3987c478bd9Sstevel@tonic-gate struct gen_hdr {
3997c478bd9Sstevel@tonic-gate 	ulong_t		g_mode;		/* Mode of file */
4007c478bd9Sstevel@tonic-gate 	uid_t		g_uid,		/* Uid of file */
4017c478bd9Sstevel@tonic-gate 			g_gid;		/* Gid of file */
4027c478bd9Sstevel@tonic-gate 	off_t		g_filesz;	/* Length of file */
4037c478bd9Sstevel@tonic-gate 	time_t		g_mtime;	/* Modification time */
4047c478bd9Sstevel@tonic-gate 	uint_t		g_cksum;	/* Checksum of file */
4057c478bd9Sstevel@tonic-gate 	ulong_t		g_devmajor,	/* File system of file */
4067c478bd9Sstevel@tonic-gate 			g_devminor;	/* Major/minor of special files */
4077c478bd9Sstevel@tonic-gate } Gen;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate static
4107c478bd9Sstevel@tonic-gate struct linkbuf {
4117c478bd9Sstevel@tonic-gate 	ino_t	inum;
4127c478bd9Sstevel@tonic-gate 	dev_t	devnum;
4137c478bd9Sstevel@tonic-gate 	int	count;
4147c478bd9Sstevel@tonic-gate 	char	pathname[MAXNAM+1];	/* added 1 for last NULL */
4157c478bd9Sstevel@tonic-gate 	char 	attrname[MAXNAM+1];
4167c478bd9Sstevel@tonic-gate 	struct	linkbuf *nextp;
4177c478bd9Sstevel@tonic-gate } *ihead;
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate /* see comments before build_table() */
4207c478bd9Sstevel@tonic-gate #define	TABLE_SIZE 512
4217c478bd9Sstevel@tonic-gate struct	file_list	{
4227c478bd9Sstevel@tonic-gate 	char	*name;			/* Name of file to {in,ex}clude */
4237c478bd9Sstevel@tonic-gate 	struct	file_list	*next;	/* Linked list */
4247c478bd9Sstevel@tonic-gate };
4257c478bd9Sstevel@tonic-gate static	struct	file_list	*exclude_tbl[TABLE_SIZE],
4267c478bd9Sstevel@tonic-gate 				*include_tbl[TABLE_SIZE];
4277c478bd9Sstevel@tonic-gate 
42845916cd2Sjpk static int	append_secattr(char **, int *, int, char *, char);
4297c478bd9Sstevel@tonic-gate static void	write_ancillary(union hblock *, char *, int, char);
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate static void add_file_to_table(struct file_list *table[], char *str);
4327c478bd9Sstevel@tonic-gate static void assert_string(char *s, char *msg);
4337c478bd9Sstevel@tonic-gate static int istape(int fd, int type);
4347c478bd9Sstevel@tonic-gate static void backtape(void);
4357c478bd9Sstevel@tonic-gate static void build_table(struct file_list *table[], char *file);
436*da6c28aaSamw static int check_prefix(char **namep, char **dirp, char **compp);
4377c478bd9Sstevel@tonic-gate static void closevol(void);
4387c478bd9Sstevel@tonic-gate static void copy(void *dst, void *src);
4397c478bd9Sstevel@tonic-gate static int convtoreg(off_t);
4402c0f0499Slovely static void delete_target(int fd, char *comp, char *namep);
4417c478bd9Sstevel@tonic-gate static void doDirTimes(char *name, timestruc_t modTime);
4427c478bd9Sstevel@tonic-gate static void done(int n);
4437c478bd9Sstevel@tonic-gate static void dorep(char *argv[]);
4447c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
4457c478bd9Sstevel@tonic-gate static void dotable(char *argv[], int cnt);
4467c478bd9Sstevel@tonic-gate static void doxtract(char *argv[], int cnt);
4477c478bd9Sstevel@tonic-gate #else
4487c478bd9Sstevel@tonic-gate static void dotable(char *argv[]);
4497c478bd9Sstevel@tonic-gate static void doxtract(char *argv[]);
4507c478bd9Sstevel@tonic-gate #endif
4517c478bd9Sstevel@tonic-gate static void fatal(char *format, ...);
4527c478bd9Sstevel@tonic-gate static void vperror(int exit_status, char *fmt, ...);
4537c478bd9Sstevel@tonic-gate static void flushtape(void);
4547c478bd9Sstevel@tonic-gate static void getdir(void);
4557c478bd9Sstevel@tonic-gate static void *getmem(size_t);
4567c478bd9Sstevel@tonic-gate static void longt(struct stat *st, char aclchar);
4577c478bd9Sstevel@tonic-gate static int makeDir(char *name);
4587c478bd9Sstevel@tonic-gate static void mterr(char *operation, int i, int exitcode);
4597c478bd9Sstevel@tonic-gate static void newvol(void);
4607c478bd9Sstevel@tonic-gate static void passtape(void);
4617c478bd9Sstevel@tonic-gate static void putempty(blkcnt_t n);
4627c478bd9Sstevel@tonic-gate static int putfile(char *longname, char *shortname, char *parent,
463*da6c28aaSamw     attr_data_t *attrinfo, int filetype, int lev, int symlink_lev);
4647c478bd9Sstevel@tonic-gate static void readtape(char *buffer);
4657c478bd9Sstevel@tonic-gate static void seekdisk(blkcnt_t blocks);
4667c478bd9Sstevel@tonic-gate static void setPathTimes(int dirfd, char *path, timestruc_t modTime);
4677c478bd9Sstevel@tonic-gate static void splitfile(char *longname, int ifd, char *name,
4687c478bd9Sstevel@tonic-gate 	char *prefix, int filetype);
4697c478bd9Sstevel@tonic-gate static void tomodes(struct stat *sp);
4707c478bd9Sstevel@tonic-gate static void usage(void);
471*da6c28aaSamw static int xblocks(int issysattr, off_t bytes, int ofile);
472*da6c28aaSamw static int xsfile(int issysattr, int ofd);
4737c478bd9Sstevel@tonic-gate static void resugname(int dirfd, char *name, int symflag);
4747c478bd9Sstevel@tonic-gate static int bcheck(char *bstr);
4757c478bd9Sstevel@tonic-gate static int checkdir(char *name);
4767c478bd9Sstevel@tonic-gate static int checksum(union hblock *dblockp);
4777c478bd9Sstevel@tonic-gate #ifdef	EUC
4787c478bd9Sstevel@tonic-gate static int checksum_signed(union hblock *dblockp);
4797c478bd9Sstevel@tonic-gate #endif	/* EUC */
4807c478bd9Sstevel@tonic-gate static int checkupdate(char *arg);
4817c478bd9Sstevel@tonic-gate static int checkw(char c, char *name);
4827c478bd9Sstevel@tonic-gate static int cmp(char *b, char *s, int n);
4837c478bd9Sstevel@tonic-gate static int defset(char *arch);
4847c478bd9Sstevel@tonic-gate static int endtape(void);
4857c478bd9Sstevel@tonic-gate static int is_in_table(struct file_list *table[], char *str);
4867c478bd9Sstevel@tonic-gate static int notsame(void);
4877c478bd9Sstevel@tonic-gate static int is_prefix(char *s1, char *s2);
4887c478bd9Sstevel@tonic-gate static int response(void);
4897c478bd9Sstevel@tonic-gate static int build_dblock(const char *, const char *, const char,
4907c478bd9Sstevel@tonic-gate 	const int filetype, const struct stat *, const dev_t, const char *);
4917c478bd9Sstevel@tonic-gate static unsigned int hash(char *str);
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
4947c478bd9Sstevel@tonic-gate static void initarg(char *argv[], char *file);
4957c478bd9Sstevel@tonic-gate static char *nextarg();
4967c478bd9Sstevel@tonic-gate #endif
4977c478bd9Sstevel@tonic-gate static blkcnt_t kcheck(char *kstr);
4987c478bd9Sstevel@tonic-gate static off_t bsrch(char *s, int n, off_t l, off_t h);
4997c478bd9Sstevel@tonic-gate static void onintr(int sig);
5007c478bd9Sstevel@tonic-gate static void onquit(int sig);
5017c478bd9Sstevel@tonic-gate static void onhup(int sig);
5027c478bd9Sstevel@tonic-gate static uid_t getuidbyname(char *);
5037c478bd9Sstevel@tonic-gate static gid_t getgidbyname(char *);
5047c478bd9Sstevel@tonic-gate static char *getname(gid_t);
5057c478bd9Sstevel@tonic-gate static char *getgroup(gid_t);
5067c478bd9Sstevel@tonic-gate static int checkf(char *name, int mode, int howmuch);
5077c478bd9Sstevel@tonic-gate static int writetbuf(char *buffer, int n);
508*da6c28aaSamw static int wantit(char *argv[], char **namep, char **dirp, char **comp,
509*da6c28aaSamw     attr_data_t **attrinfo);
51045916cd2Sjpk static void append_ext_attr(char *shortname, char **secinfo, int *len);
5117c478bd9Sstevel@tonic-gate static int get_xdata(void);
5127c478bd9Sstevel@tonic-gate static void gen_num(const char *keyword, const u_longlong_t number);
5137c478bd9Sstevel@tonic-gate static void gen_date(const char *keyword, const timestruc_t time_value);
5147c478bd9Sstevel@tonic-gate static void gen_string(const char *keyword, const char *value);
5157c478bd9Sstevel@tonic-gate static void get_xtime(char *value, timestruc_t *xtime);
5167c478bd9Sstevel@tonic-gate static int chk_path_build(char *name, char *longname, char *linkname,
5177c478bd9Sstevel@tonic-gate     char *prefix, char type, int filetype);
5187c478bd9Sstevel@tonic-gate static int gen_utf8_names(const char *filename);
5197c478bd9Sstevel@tonic-gate static int utf8_local(char *option, char **Xhdr_ptrptr, char *target,
5207c478bd9Sstevel@tonic-gate     const char *src, int max_val);
5217c478bd9Sstevel@tonic-gate static int local_utf8(char **Xhdr_ptrptr, char *target, const char *src,
5227c478bd9Sstevel@tonic-gate     iconv_t iconv_cd, int xhdrflg, int max_val);
5237c478bd9Sstevel@tonic-gate static int c_utf8(char *target, const char *source);
524*da6c28aaSamw static int getstat(int dirfd, char *longname, char *shortname,
525*da6c28aaSamw     char *attrparent);
526*da6c28aaSamw static void xattrs_put(char *, char *, char *, char *);
5277c478bd9Sstevel@tonic-gate static void prepare_xattr(char **, char	*, char	*,
5287c478bd9Sstevel@tonic-gate     char, struct linkbuf *, int *);
529*da6c28aaSamw static int put_link(char *name, char *longname, char *component,
530*da6c28aaSamw     char *longattrname, char *prefix, int filetype, char typeflag);
5317c478bd9Sstevel@tonic-gate static int put_extra_attributes(char *longname, char *shortname,
532*da6c28aaSamw     char *longattrname, char *prefix, int filetype, char typeflag);
533*da6c28aaSamw static int put_xattr_hdr(char *longname, char *shortname, char *longattrname,
534*da6c28aaSamw     char *prefix, int typeflag, int filetype, struct linkbuf *lp);
535*da6c28aaSamw static int read_xattr_hdr(attr_data_t **attrinfo);
53645916cd2Sjpk 
53745916cd2Sjpk /* Trusted Extensions */
53845916cd2Sjpk #define	AUTO_ZONE	"/zone"
53945916cd2Sjpk 
54045916cd2Sjpk static void extract_attr(char **file_ptr, struct sec_attr *);
54145916cd2Sjpk static int check_ext_attr(char *filename);
54245916cd2Sjpk static void rebuild_comp_path(char *str, char **namep);
54345916cd2Sjpk static int rebuild_lk_comp_path(char *str, char **namep);
54445916cd2Sjpk 
5457c478bd9Sstevel@tonic-gate static void get_parent(char *path, char *dir);
5467c478bd9Sstevel@tonic-gate static char *get_component(char *path);
547*da6c28aaSamw static int retry_open_attr(int pdirfd, int cwd, char *dirp, char *pattr,
548*da6c28aaSamw     char *name, int oflag, mode_t mode);
5497c478bd9Sstevel@tonic-gate static char *skipslashes(char *string, char *start);
5507c478bd9Sstevel@tonic-gate static void chop_endslashes(char *path);
551fa9e4066Sahrens 
5527c478bd9Sstevel@tonic-gate static	struct stat stbuf;
5537c478bd9Sstevel@tonic-gate 
554*da6c28aaSamw static	char	*myname;
5557c478bd9Sstevel@tonic-gate static	int	checkflag = 0;
5567c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
5577c478bd9Sstevel@tonic-gate static	int	Fileflag;
5587c478bd9Sstevel@tonic-gate char    *sysv3_env;
5597c478bd9Sstevel@tonic-gate #endif
5607c478bd9Sstevel@tonic-gate static	int	Xflag, Fflag, iflag, hflag, Bflag, Iflag;
5617c478bd9Sstevel@tonic-gate static	int	rflag, xflag, vflag, tflag, mt, cflag, mflag, pflag;
5627c478bd9Sstevel@tonic-gate static	int	uflag;
5637c478bd9Sstevel@tonic-gate static	int	eflag, errflag, qflag;
5647c478bd9Sstevel@tonic-gate static	int	oflag;
5657c478bd9Sstevel@tonic-gate static	int	bflag, kflag, Aflag;
5667c478bd9Sstevel@tonic-gate static 	int	Pflag;			/* POSIX conformant archive */
5677c478bd9Sstevel@tonic-gate static	int	Eflag;			/* Allow files greater than 8GB */
5687c478bd9Sstevel@tonic-gate static	int	atflag;			/* traverse extended attributes */
569*da6c28aaSamw static	int	saflag;			/* traverse extended sys attributes */
5707c478bd9Sstevel@tonic-gate static	int	Dflag;			/* Data change flag */
57145916cd2Sjpk /* Trusted Extensions */
57245916cd2Sjpk static	int	Tflag;			/* Trusted Extensions attr flags */
57345916cd2Sjpk static	int	dir_flag;		/* for attribute extract */
57445916cd2Sjpk static	int	mld_flag;		/* for attribute extract */
57545916cd2Sjpk static	char	*orig_namep;		/* original namep - unadorned */
57645916cd2Sjpk static	int	rpath_flag;		/* MLD real path is rebuilt */
57745916cd2Sjpk static	char	real_path[MAXPATHLEN];	/* MLD real path */
57845916cd2Sjpk static	int	lk_rpath_flag;		/* linked to real path is rebuilt */
57945916cd2Sjpk static	char	lk_real_path[MAXPATHLEN]; /* linked real path */
58045916cd2Sjpk static	bslabel_t	bs_label;	/* for attribute extract */
58145916cd2Sjpk static	bslabel_t	admin_low;
58245916cd2Sjpk static	bslabel_t	admin_high;
58345916cd2Sjpk static	int	ignored_aprivs = 0;
58445916cd2Sjpk static	int	ignored_fprivs = 0;
58545916cd2Sjpk static	int	ignored_fattrs = 0;
58645916cd2Sjpk 
5877c478bd9Sstevel@tonic-gate static	int	term, chksum, wflag,
5887c478bd9Sstevel@tonic-gate 		first = TRUE, defaults_used = FALSE, linkerrok;
5897c478bd9Sstevel@tonic-gate static	blkcnt_t	recno;
5907c478bd9Sstevel@tonic-gate static	int	freemem = 1;
5917c478bd9Sstevel@tonic-gate static	int	nblock = NBLOCK;
5927c478bd9Sstevel@tonic-gate static	int	Errflg = 0;
5937c478bd9Sstevel@tonic-gate static	int	exitflag = 0;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate static	dev_t	mt_dev;		/* device containing output file */
5967c478bd9Sstevel@tonic-gate static	ino_t	mt_ino;		/* inode number of output file */
5977c478bd9Sstevel@tonic-gate static	int	mt_devtype;	/* dev type of archive, from stat structure */
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate static	int update = 1;		/* for `open' call */
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate static	off_t	low;
6027c478bd9Sstevel@tonic-gate static	off_t	high;
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate static	FILE	*tfile;
6057c478bd9Sstevel@tonic-gate static	FILE	*vfile = stdout;
6067c478bd9Sstevel@tonic-gate static	char	tname[] = "/tmp/tarXXXXXX";
6077c478bd9Sstevel@tonic-gate static	char	archive[] = "archive0=";
6087c478bd9Sstevel@tonic-gate static	char	*Xfile;
6097c478bd9Sstevel@tonic-gate static	char	*usefile;
6107c478bd9Sstevel@tonic-gate static	char	*Filefile;
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate static	int	mulvol;		/* multi-volume option selected */
6137c478bd9Sstevel@tonic-gate static	blkcnt_t	blocklim; /* number of blocks to accept per volume */
6147c478bd9Sstevel@tonic-gate static	blkcnt_t	tapepos; /* current block number to be written */
6157c478bd9Sstevel@tonic-gate static	int	NotTape;	/* true if tape is a disk */
6167c478bd9Sstevel@tonic-gate static	int	dumping;	/* true if writing a tape or other archive */
6177c478bd9Sstevel@tonic-gate static	int	extno;		/* number of extent:  starts at 1 */
6187c478bd9Sstevel@tonic-gate static	int	extotal;	/* total extents in this file */
6197c478bd9Sstevel@tonic-gate static	off_t	extsize;	/* size of current extent during extraction */
6207c478bd9Sstevel@tonic-gate static	ushort_t	Oumask = 0;	/* old umask value */
6217c478bd9Sstevel@tonic-gate static 	int is_posix;	/* true if archive we're reading is POSIX-conformant */
6227c478bd9Sstevel@tonic-gate static	const	char	*magic_type = "ustar";
6237c478bd9Sstevel@tonic-gate static	size_t	xrec_size = 8 * PATH_MAX;	/* extended rec initial size */
6247c478bd9Sstevel@tonic-gate static	char	*xrec_ptr;
6257c478bd9Sstevel@tonic-gate static	off_t	xrec_offset = 0;
6267c478bd9Sstevel@tonic-gate static	int	Xhdrflag;
6277c478bd9Sstevel@tonic-gate static	int	charset_type = 0;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate static	u_longlong_t	xhdr_flgs;	/* Bits set determine which items */
6307c478bd9Sstevel@tonic-gate 					/*   need to be in extended header. */
6317c478bd9Sstevel@tonic-gate #define	_X_DEVMAJOR	0x1
6327c478bd9Sstevel@tonic-gate #define	_X_DEVMINOR	0x2
6337c478bd9Sstevel@tonic-gate #define	_X_GID		0x4
6347c478bd9Sstevel@tonic-gate #define	_X_GNAME	0x8
6357c478bd9Sstevel@tonic-gate #define	_X_LINKPATH	0x10
6367c478bd9Sstevel@tonic-gate #define	_X_PATH		0x20
6377c478bd9Sstevel@tonic-gate #define	_X_SIZE		0x40
6387c478bd9Sstevel@tonic-gate #define	_X_UID		0x80
6397c478bd9Sstevel@tonic-gate #define	_X_UNAME	0x100
6407c478bd9Sstevel@tonic-gate #define	_X_ATIME	0x200
6417c478bd9Sstevel@tonic-gate #define	_X_CTIME	0x400
6427c478bd9Sstevel@tonic-gate #define	_X_MTIME	0x800
6437c478bd9Sstevel@tonic-gate #define	_X_LAST		0x40000000
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate #define	PID_MAX_DIGITS		(10 * sizeof (pid_t) / 4)
6467c478bd9Sstevel@tonic-gate #define	TIME_MAX_DIGITS		(10 * sizeof (time_t) / 4)
6477c478bd9Sstevel@tonic-gate #define	LONG_MAX_DIGITS		(10 * sizeof (long) / 4)
6487c478bd9Sstevel@tonic-gate #define	ULONGLONG_MAX_DIGITS	(10 * sizeof (u_longlong_t) / 4)
6497c478bd9Sstevel@tonic-gate /*
6507c478bd9Sstevel@tonic-gate  * UTF_8 encoding requires more space than the current codeset equivalent.
6517c478bd9Sstevel@tonic-gate  * Currently a factor of 2-3 would suffice, but it is possible for a factor
6527c478bd9Sstevel@tonic-gate  * of 6 to be needed in the future, so for saftey, we use that here.
6537c478bd9Sstevel@tonic-gate  */
6547c478bd9Sstevel@tonic-gate #define	UTF_8_FACTOR	6
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate static	u_longlong_t	xhdr_count = 0;
6577c478bd9Sstevel@tonic-gate static char		xhdr_dirname[PRESIZ + 1];
6587c478bd9Sstevel@tonic-gate static char		pidchars[PID_MAX_DIGITS + 1];
6597c478bd9Sstevel@tonic-gate static char		*tchar = "";		/* null linkpath */
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate static	char	local_path[UTF_8_FACTOR * PATH_MAX + 1];
6627c478bd9Sstevel@tonic-gate static	char	local_linkpath[UTF_8_FACTOR * PATH_MAX + 1];
6637c478bd9Sstevel@tonic-gate static	char	local_gname[UTF_8_FACTOR * _POSIX_NAME_MAX + 1];
6647c478bd9Sstevel@tonic-gate static	char	local_uname[UTF_8_FACTOR * _POSIX_NAME_MAX + 1];
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate /*
6677c478bd9Sstevel@tonic-gate  * The following mechanism is provided to allow us to debug tar in complicated
6687c478bd9Sstevel@tonic-gate  * situations, like when it is part of a pipe.  The idea is that you compile
6697c478bd9Sstevel@tonic-gate  * with -DWAITAROUND defined, and then add the 'z' function modifier to the
6707c478bd9Sstevel@tonic-gate  * target tar invocation, eg. "tar czf tarfile file".  If stderr is available,
6717c478bd9Sstevel@tonic-gate  * it will tell you to which pid to attach the debugger; otherwise, use ps to
6727c478bd9Sstevel@tonic-gate  * find it.  Attach to the process from the debugger, and, *PRESTO*, you are
6737c478bd9Sstevel@tonic-gate  * there!
6747c478bd9Sstevel@tonic-gate  *
6757c478bd9Sstevel@tonic-gate  * Simply assign "waitaround = 0" once you attach to the process, and then
6767c478bd9Sstevel@tonic-gate  * proceed from there as usual.
6777c478bd9Sstevel@tonic-gate  */
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
6807c478bd9Sstevel@tonic-gate int waitaround = 0;		/* wait for rendezvous with the debugger */
6817c478bd9Sstevel@tonic-gate #endif
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate int
6857c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
6867c478bd9Sstevel@tonic-gate {
6877c478bd9Sstevel@tonic-gate 	char		*cp;
6887c478bd9Sstevel@tonic-gate 	char		*tmpdirp;
6897c478bd9Sstevel@tonic-gate 	pid_t		thispid;
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
6927c478bd9Sstevel@tonic-gate 	int	tbl_cnt = 0;
6937c478bd9Sstevel@tonic-gate 	sysv3_env = getenv("SYSV3");
6947c478bd9Sstevel@tonic-gate #endif
6957c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
6967c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
6977c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
6987c478bd9Sstevel@tonic-gate #endif
6997c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
7007c478bd9Sstevel@tonic-gate 	if (argc < 2)
7017c478bd9Sstevel@tonic-gate 		usage();
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	tfile = NULL;
704*da6c28aaSamw 	if ((myname = strdup(argv[0])) == NULL) {
705*da6c28aaSamw 		(void) fprintf(stderr, gettext(
706*da6c28aaSamw 		    "tar: cannot allocate program name\n"));
707*da6c28aaSamw 		exit(1);
708*da6c28aaSamw 	}
7097c478bd9Sstevel@tonic-gate 
7103d63ea05Sas145665 	if (init_yes() < 0) {
7113d63ea05Sas145665 		(void) fprintf(stderr, gettext(ERR_MSG_INIT_YES),
7123d63ea05Sas145665 		    strerror(errno));
7133d63ea05Sas145665 		exit(2);
7143d63ea05Sas145665 	}
7153d63ea05Sas145665 
7167c478bd9Sstevel@tonic-gate 	/*
7177c478bd9Sstevel@tonic-gate 	 *  For XPG4 compatibility, we must be able to accept the "--"
7187c478bd9Sstevel@tonic-gate 	 *  argument normally recognized by getopt; it is used to delimit
7197c478bd9Sstevel@tonic-gate 	 *  the end opt the options section, and so can only appear in
7207c478bd9Sstevel@tonic-gate 	 *  the position of the first argument.  We simply skip it.
7217c478bd9Sstevel@tonic-gate 	 */
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	if (strcmp(argv[1], "--") == 0) {
7247c478bd9Sstevel@tonic-gate 		argv++;
7257c478bd9Sstevel@tonic-gate 		argc--;
7267c478bd9Sstevel@tonic-gate 		if (argc < 3)
7277c478bd9Sstevel@tonic-gate 			usage();
7287c478bd9Sstevel@tonic-gate 	}
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	argv[argc] = NULL;
7317c478bd9Sstevel@tonic-gate 	argv++;
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	/*
7347c478bd9Sstevel@tonic-gate 	 * Set up default values.
7357c478bd9Sstevel@tonic-gate 	 * Search the operand string looking for the first digit or an 'f'.
7367c478bd9Sstevel@tonic-gate 	 * If you find a digit, use the 'archive#' entry in DEF_FILE.
7377c478bd9Sstevel@tonic-gate 	 * If 'f' is given, bypass looking in DEF_FILE altogether.
7387c478bd9Sstevel@tonic-gate 	 * If no digit or 'f' is given, still look in DEF_FILE but use '0'.
7397c478bd9Sstevel@tonic-gate 	 */
7407c478bd9Sstevel@tonic-gate 	if ((usefile = getenv("TAPE")) == (char *)NULL) {
7417c478bd9Sstevel@tonic-gate 		for (cp = *argv; *cp; ++cp)
7427c478bd9Sstevel@tonic-gate 			if (isdigit(*cp) || *cp == 'f')
7437c478bd9Sstevel@tonic-gate 				break;
7447c478bd9Sstevel@tonic-gate 		if (*cp != 'f') {
7457c478bd9Sstevel@tonic-gate 			archive[7] = (*cp)? *cp: '0';
7467c478bd9Sstevel@tonic-gate 			if (!(defaults_used = defset(archive))) {
7477c478bd9Sstevel@tonic-gate 				usefile = NULL;
7487c478bd9Sstevel@tonic-gate 				nblock = 1;
7497c478bd9Sstevel@tonic-gate 				blocklim = 0;
7507c478bd9Sstevel@tonic-gate 				NotTape = 0;
7517c478bd9Sstevel@tonic-gate 			}
7527c478bd9Sstevel@tonic-gate 		}
7537c478bd9Sstevel@tonic-gate 	}
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	for (cp = *argv++; *cp; cp++)
7567c478bd9Sstevel@tonic-gate 		switch (*cp) {
7577c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
7587c478bd9Sstevel@tonic-gate 		case 'z':
7597c478bd9Sstevel@tonic-gate 			/* rendezvous with the debugger */
7607c478bd9Sstevel@tonic-gate 			waitaround = 1;
7617c478bd9Sstevel@tonic-gate 			break;
7627c478bd9Sstevel@tonic-gate #endif
7637c478bd9Sstevel@tonic-gate 		case 'f':
7647c478bd9Sstevel@tonic-gate 			assert_string(*argv, gettext(
7657c478bd9Sstevel@tonic-gate 			    "tar: tarfile must be specified with 'f' "
7667c478bd9Sstevel@tonic-gate 			    "function modifier\n"));
7677c478bd9Sstevel@tonic-gate 			usefile = *argv++;
7687c478bd9Sstevel@tonic-gate 			break;
7697c478bd9Sstevel@tonic-gate 		case 'F':
7707c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
7717c478bd9Sstevel@tonic-gate 			if (sysv3_env) {
7727c478bd9Sstevel@tonic-gate 				assert_string(*argv, gettext(
7737c478bd9Sstevel@tonic-gate 				    "tar: 'F' requires a file name\n"));
7747c478bd9Sstevel@tonic-gate 				Filefile = *argv++;
7757c478bd9Sstevel@tonic-gate 				Fileflag++;
7767c478bd9Sstevel@tonic-gate 			} else
7777c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
7787c478bd9Sstevel@tonic-gate 				Fflag++;
7797c478bd9Sstevel@tonic-gate 			break;
7807c478bd9Sstevel@tonic-gate 		case 'c':
7817c478bd9Sstevel@tonic-gate 			cflag++;
7827c478bd9Sstevel@tonic-gate 			rflag++;
7837c478bd9Sstevel@tonic-gate 			update = 1;
7847c478bd9Sstevel@tonic-gate 			break;
7857c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
7867c478bd9Sstevel@tonic-gate 		case '@':
7877c478bd9Sstevel@tonic-gate 			atflag++;
7887c478bd9Sstevel@tonic-gate 			break;
789*da6c28aaSamw #endif	/* O_XATTR */
790*da6c28aaSamw #if defined(_PC_SATTR_ENABLED)
791*da6c28aaSamw 		case '/':
792*da6c28aaSamw 			saflag++;
793*da6c28aaSamw 			break;
794*da6c28aaSamw #endif	/* _PC_SATTR_ENABLED */
7957c478bd9Sstevel@tonic-gate 		case 'u':
7967c478bd9Sstevel@tonic-gate 			uflag++;	/* moved code after signals caught */
7977c478bd9Sstevel@tonic-gate 			rflag++;
7987c478bd9Sstevel@tonic-gate 			update = 2;
7997c478bd9Sstevel@tonic-gate 			break;
8007c478bd9Sstevel@tonic-gate 		case 'r':
8017c478bd9Sstevel@tonic-gate 			rflag++;
8027c478bd9Sstevel@tonic-gate 			update = 2;
8037c478bd9Sstevel@tonic-gate 			break;
8047c478bd9Sstevel@tonic-gate 		case 'v':
8057c478bd9Sstevel@tonic-gate 			vflag++;
8067c478bd9Sstevel@tonic-gate 			break;
8077c478bd9Sstevel@tonic-gate 		case 'w':
8087c478bd9Sstevel@tonic-gate 			wflag++;
8097c478bd9Sstevel@tonic-gate 			break;
8107c478bd9Sstevel@tonic-gate 		case 'x':
8117c478bd9Sstevel@tonic-gate 			xflag++;
8127c478bd9Sstevel@tonic-gate 			break;
8137c478bd9Sstevel@tonic-gate 		case 'X':
8147c478bd9Sstevel@tonic-gate 			assert_string(*argv, gettext(
8157c478bd9Sstevel@tonic-gate 			    "tar: exclude file must be specified with 'X' "
8167c478bd9Sstevel@tonic-gate 			    "function modifier\n"));
8177c478bd9Sstevel@tonic-gate 			Xflag = 1;
8187c478bd9Sstevel@tonic-gate 			Xfile = *argv++;
8197c478bd9Sstevel@tonic-gate 			build_table(exclude_tbl, Xfile);
8207c478bd9Sstevel@tonic-gate 			break;
8217c478bd9Sstevel@tonic-gate 		case 't':
8227c478bd9Sstevel@tonic-gate 			tflag++;
8237c478bd9Sstevel@tonic-gate 			break;
8247c478bd9Sstevel@tonic-gate 		case 'm':
8257c478bd9Sstevel@tonic-gate 			mflag++;
8267c478bd9Sstevel@tonic-gate 			break;
8277c478bd9Sstevel@tonic-gate 		case 'p':
8287c478bd9Sstevel@tonic-gate 			pflag++;
8297c478bd9Sstevel@tonic-gate 			break;
8307c478bd9Sstevel@tonic-gate 		case 'D':
8317c478bd9Sstevel@tonic-gate 			Dflag++;
8327c478bd9Sstevel@tonic-gate 			break;
8337c478bd9Sstevel@tonic-gate 		case '-':
8347c478bd9Sstevel@tonic-gate 			/* ignore this silently */
8357c478bd9Sstevel@tonic-gate 			break;
8367c478bd9Sstevel@tonic-gate 		case '0':	/* numeric entries used only for defaults */
8377c478bd9Sstevel@tonic-gate 		case '1':
8387c478bd9Sstevel@tonic-gate 		case '2':
8397c478bd9Sstevel@tonic-gate 		case '3':
8407c478bd9Sstevel@tonic-gate 		case '4':
8417c478bd9Sstevel@tonic-gate 		case '5':
8427c478bd9Sstevel@tonic-gate 		case '6':
8437c478bd9Sstevel@tonic-gate 		case '7':
8447c478bd9Sstevel@tonic-gate 			break;
8457c478bd9Sstevel@tonic-gate 		case 'b':
8467c478bd9Sstevel@tonic-gate 			assert_string(*argv, gettext(
8477c478bd9Sstevel@tonic-gate 			    "tar: blocking factor must be specified "
8487c478bd9Sstevel@tonic-gate 			    "with 'b' function modifier\n"));
8497c478bd9Sstevel@tonic-gate 			bflag++;
8507c478bd9Sstevel@tonic-gate 			nblock = bcheck(*argv++);
8517c478bd9Sstevel@tonic-gate 			break;
8527c478bd9Sstevel@tonic-gate 		case 'q':
8537c478bd9Sstevel@tonic-gate 			qflag++;
8547c478bd9Sstevel@tonic-gate 			break;
8557c478bd9Sstevel@tonic-gate 		case 'k':
8567c478bd9Sstevel@tonic-gate 			assert_string(*argv, gettext(
8577c478bd9Sstevel@tonic-gate 			    "tar: size value must be specified with 'k' "
8587c478bd9Sstevel@tonic-gate 			    "function modifier\n"));
8597c478bd9Sstevel@tonic-gate 			kflag++;
8607c478bd9Sstevel@tonic-gate 			blocklim = kcheck(*argv++);
8617c478bd9Sstevel@tonic-gate 			break;
8627c478bd9Sstevel@tonic-gate 		case 'n':		/* not a magtape (instead of 'k') */
8637c478bd9Sstevel@tonic-gate 			NotTape++;	/* assume non-magtape */
8647c478bd9Sstevel@tonic-gate 			break;
8657c478bd9Sstevel@tonic-gate 		case 'l':
8667c478bd9Sstevel@tonic-gate 			linkerrok++;
8677c478bd9Sstevel@tonic-gate 			break;
8687c478bd9Sstevel@tonic-gate 		case 'e':
8697c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
8707c478bd9Sstevel@tonic-gate 			/* If sysv3 IS set, don't be as verbose */
8717c478bd9Sstevel@tonic-gate 			if (!sysv3_env)
8727c478bd9Sstevel@tonic-gate #endif	/* _iBCS2 */
8737c478bd9Sstevel@tonic-gate 				errflag++;
8747c478bd9Sstevel@tonic-gate 			eflag++;
8757c478bd9Sstevel@tonic-gate 			break;
8767c478bd9Sstevel@tonic-gate 		case 'o':
8777c478bd9Sstevel@tonic-gate 			oflag++;
8787c478bd9Sstevel@tonic-gate 			break;
8797c478bd9Sstevel@tonic-gate 		case 'h':
8807c478bd9Sstevel@tonic-gate 			hflag++;
8817c478bd9Sstevel@tonic-gate 			break;
8827c478bd9Sstevel@tonic-gate 		case 'i':
8837c478bd9Sstevel@tonic-gate 			iflag++;
8847c478bd9Sstevel@tonic-gate 			break;
8857c478bd9Sstevel@tonic-gate 		case 'B':
8867c478bd9Sstevel@tonic-gate 			Bflag++;
8877c478bd9Sstevel@tonic-gate 			break;
8887c478bd9Sstevel@tonic-gate 		case 'P':
8897c478bd9Sstevel@tonic-gate 			Pflag++;
8907c478bd9Sstevel@tonic-gate 			break;
8917c478bd9Sstevel@tonic-gate 		case 'E':
8927c478bd9Sstevel@tonic-gate 			Eflag++;
8937c478bd9Sstevel@tonic-gate 			Pflag++;	/* Only POSIX archive made */
8947c478bd9Sstevel@tonic-gate 			break;
89545916cd2Sjpk 		case 'T':
89645916cd2Sjpk 			Tflag++;	/* Handle Trusted Extensions attrs */
89745916cd2Sjpk 			pflag++;	/* also set flag for ACL */
89845916cd2Sjpk 			break;
8997c478bd9Sstevel@tonic-gate 		default:
9007c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
9017c478bd9Sstevel@tonic-gate 			"tar: %c: unknown function modifier\n"), *cp);
9027c478bd9Sstevel@tonic-gate 			usage();
9037c478bd9Sstevel@tonic-gate 		}
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
9067c478bd9Sstevel@tonic-gate 	if (Xflag && Fileflag) {
9077c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
9087c478bd9Sstevel@tonic-gate 		"tar: specify only one of X or F.\n"));
9097c478bd9Sstevel@tonic-gate 		usage();
9107c478bd9Sstevel@tonic-gate 	}
9117c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	if (!rflag && !xflag && !tflag)
9147c478bd9Sstevel@tonic-gate 		usage();
9157c478bd9Sstevel@tonic-gate 	if ((rflag && xflag) || (xflag && tflag) || (rflag && tflag)) {
9167c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
9177c478bd9Sstevel@tonic-gate 		"tar: specify only one of [ctxru].\n"));
9187c478bd9Sstevel@tonic-gate 		usage();
9197c478bd9Sstevel@tonic-gate 	}
92045916cd2Sjpk 	/* Trusted Extensions attribute handling */
92145916cd2Sjpk 	if (Tflag && ((getzoneid() != GLOBAL_ZONEID) ||
92245916cd2Sjpk 	    !is_system_labeled())) {
92345916cd2Sjpk 		(void) fprintf(stderr, gettext(
92445916cd2Sjpk 		"tar: the 'T' option is only available with "
92545916cd2Sjpk 		    "Trusted Extensions\nand must be run from "
92645916cd2Sjpk 		    "the global zone.\n"));
92745916cd2Sjpk 		usage();
92845916cd2Sjpk 	}
9297c478bd9Sstevel@tonic-gate 	if (cflag && *argv == NULL && Filefile == NULL)
9307c478bd9Sstevel@tonic-gate 		fatal(gettext("Missing filenames"));
9317c478bd9Sstevel@tonic-gate 	if (usefile == NULL)
9327c478bd9Sstevel@tonic-gate 		fatal(gettext("device argument required"));
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	/* alloc a buffer of the right size */
9357c478bd9Sstevel@tonic-gate 	if ((tbuf = (union hblock *)
9367c478bd9Sstevel@tonic-gate 	    calloc(sizeof (union hblock) * nblock, sizeof (char))) ==
9377c478bd9Sstevel@tonic-gate 	    (union hblock *)NULL) {
9387c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
9397c478bd9Sstevel@tonic-gate 		"tar: cannot allocate physio buffer\n"));
9407c478bd9Sstevel@tonic-gate 		exit(1);
9417c478bd9Sstevel@tonic-gate 	}
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	if ((xrec_ptr = malloc(xrec_size)) == NULL) {
9447c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
9457c478bd9Sstevel@tonic-gate 		    "tar: cannot allocate extended header buffer\n"));
9467c478bd9Sstevel@tonic-gate 		exit(1);
9477c478bd9Sstevel@tonic-gate 	}
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
9507c478bd9Sstevel@tonic-gate 	if (waitaround) {
9517c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Rendezvous with tar on pid"
9527c478bd9Sstevel@tonic-gate 		    " %d\n"), getpid());
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 		while (waitaround) {
9557c478bd9Sstevel@tonic-gate 			(void) sleep(10);
9567c478bd9Sstevel@tonic-gate 		}
9577c478bd9Sstevel@tonic-gate 	}
9587c478bd9Sstevel@tonic-gate #endif
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	thispid = getpid();
9617c478bd9Sstevel@tonic-gate 	(void) sprintf(pidchars, "%ld", thispid);
9627c478bd9Sstevel@tonic-gate 	thispid = strlen(pidchars);
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	if ((tmpdirp = getenv("TMPDIR")) == (char *)NULL)
9657c478bd9Sstevel@tonic-gate 		(void) strcpy(xhdr_dirname, "/tmp");
9667c478bd9Sstevel@tonic-gate 	else {
9677c478bd9Sstevel@tonic-gate 		/*
9687c478bd9Sstevel@tonic-gate 		 * Make sure that dir is no longer than what can
9697c478bd9Sstevel@tonic-gate 		 * fit in the prefix part of the header.
9707c478bd9Sstevel@tonic-gate 		 */
9717c478bd9Sstevel@tonic-gate 		if (strlen(tmpdirp) > (size_t)(PRESIZ - thispid - 12)) {
9727c478bd9Sstevel@tonic-gate 			(void) strcpy(xhdr_dirname, "/tmp");
9737c478bd9Sstevel@tonic-gate 			if ((vflag > 0) && (Eflag > 0))
9747c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
9757c478bd9Sstevel@tonic-gate 				    "Ignoring TMPDIR\n"));
9767c478bd9Sstevel@tonic-gate 		} else
9777c478bd9Sstevel@tonic-gate 			(void) strcpy(xhdr_dirname, tmpdirp);
9787c478bd9Sstevel@tonic-gate 	}
9797c478bd9Sstevel@tonic-gate 	(void) strcat(xhdr_dirname, "/PaxHeaders.");
9807c478bd9Sstevel@tonic-gate 	(void) strcat(xhdr_dirname, pidchars);
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	if (rflag) {
9837c478bd9Sstevel@tonic-gate 		if (cflag && tfile != NULL)
9847c478bd9Sstevel@tonic-gate 			usage();
9857c478bd9Sstevel@tonic-gate 		if (signal(SIGINT, SIG_IGN) != SIG_IGN)
9867c478bd9Sstevel@tonic-gate 			(void) signal(SIGINT, onintr);
9877c478bd9Sstevel@tonic-gate 		if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
9887c478bd9Sstevel@tonic-gate 			(void) signal(SIGHUP, onhup);
9897c478bd9Sstevel@tonic-gate 		if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
9907c478bd9Sstevel@tonic-gate 			(void) signal(SIGQUIT, onquit);
9917c478bd9Sstevel@tonic-gate 		if (uflag) {
9927c478bd9Sstevel@tonic-gate 			int tnum;
9937c478bd9Sstevel@tonic-gate 			if ((tnum = mkstemp(tname)) == -1)
9947c478bd9Sstevel@tonic-gate 				vperror(1, "%s", tname);
9957c478bd9Sstevel@tonic-gate 			if ((tfile = fdopen(tnum, "w")) == NULL)
9967c478bd9Sstevel@tonic-gate 				vperror(1, "%s", tname);
9977c478bd9Sstevel@tonic-gate 		}
9987c478bd9Sstevel@tonic-gate 		if (strcmp(usefile, "-") == 0) {
9997c478bd9Sstevel@tonic-gate 			if (cflag == 0)
10007c478bd9Sstevel@tonic-gate 				fatal(gettext(
10017c478bd9Sstevel@tonic-gate 				"can only create standard output archives."));
10027c478bd9Sstevel@tonic-gate 			vfile = stderr;
10037c478bd9Sstevel@tonic-gate 			mt = dup(1);
10047c478bd9Sstevel@tonic-gate 			++bflag;
10057c478bd9Sstevel@tonic-gate 		} else {
10067c478bd9Sstevel@tonic-gate 			if (cflag)
10077c478bd9Sstevel@tonic-gate 				mt = open(usefile,
10087c478bd9Sstevel@tonic-gate 				    O_RDWR|O_CREAT|O_TRUNC, 0666);
10097c478bd9Sstevel@tonic-gate 			else
10107c478bd9Sstevel@tonic-gate 				mt = open(usefile, O_RDWR);
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 			if (mt < 0) {
10137c478bd9Sstevel@tonic-gate 				if (cflag == 0 || (mt =  creat(usefile, 0666))
10147c478bd9Sstevel@tonic-gate 				    < 0)
10157c478bd9Sstevel@tonic-gate 				vperror(1, "%s", usefile);
10167c478bd9Sstevel@tonic-gate 			}
10177c478bd9Sstevel@tonic-gate 		}
10187c478bd9Sstevel@tonic-gate 		/* Get inode and device number of output file */
10197c478bd9Sstevel@tonic-gate 		(void) fstat(mt, &stbuf);
10207c478bd9Sstevel@tonic-gate 		mt_ino = stbuf.st_ino;
10217c478bd9Sstevel@tonic-gate 		mt_dev = stbuf.st_dev;
10227c478bd9Sstevel@tonic-gate 		mt_devtype = stbuf.st_mode & S_IFMT;
10237c478bd9Sstevel@tonic-gate 		NotTape = !istape(mt, mt_devtype);
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 		if (rflag && !cflag && (mt_devtype == S_IFIFO))
10267c478bd9Sstevel@tonic-gate 			fatal(gettext("cannot append to pipe or FIFO."));
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 		if (Aflag && vflag)
10297c478bd9Sstevel@tonic-gate 			(void) printf(
10307c478bd9Sstevel@tonic-gate 			gettext("Suppressing absolute pathnames\n"));
10317c478bd9Sstevel@tonic-gate 		dorep(argv);
10327c478bd9Sstevel@tonic-gate 	} else if (xflag || tflag) {
10337c478bd9Sstevel@tonic-gate 		/*
10347c478bd9Sstevel@tonic-gate 		 * for each argument, check to see if there is a "-I file" pair.
10357c478bd9Sstevel@tonic-gate 		 * if so, move the 3rd argument into "-I"'s place, build_table()
10367c478bd9Sstevel@tonic-gate 		 * using "file"'s name and increment argc one (the second
10377c478bd9Sstevel@tonic-gate 		 * increment appears in the for loop) which removes the two
10387c478bd9Sstevel@tonic-gate 		 * args "-I" and "file" from the argument vector.
10397c478bd9Sstevel@tonic-gate 		 */
10407c478bd9Sstevel@tonic-gate 		for (argc = 0; argv[argc]; argc++) {
10417c478bd9Sstevel@tonic-gate 			if (strcmp(argv[argc], "-I") == 0) {
10427c478bd9Sstevel@tonic-gate 				if (!argv[argc+1]) {
10437c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
10447c478bd9Sstevel@tonic-gate 					"tar: missing argument for -I flag\n"));
10457c478bd9Sstevel@tonic-gate 					done(2);
10467c478bd9Sstevel@tonic-gate 				} else {
10477c478bd9Sstevel@tonic-gate 					Iflag = 1;
10487c478bd9Sstevel@tonic-gate 					argv[argc] = argv[argc+2];
10497c478bd9Sstevel@tonic-gate 					build_table(include_tbl, argv[++argc]);
10507c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
10517c478bd9Sstevel@tonic-gate 					if (Fileflag) {
10527c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(
10537c478bd9Sstevel@tonic-gate 						"tar: only one of I or F.\n"));
10547c478bd9Sstevel@tonic-gate 						usage();
10557c478bd9Sstevel@tonic-gate 					}
10567c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 				}
10597c478bd9Sstevel@tonic-gate 			}
10607c478bd9Sstevel@tonic-gate 		}
10617c478bd9Sstevel@tonic-gate 		if (strcmp(usefile, "-") == 0) {
10627c478bd9Sstevel@tonic-gate 			mt = dup(0);
10637c478bd9Sstevel@tonic-gate 			++bflag;
10647c478bd9Sstevel@tonic-gate 			/* try to recover from short reads when reading stdin */
10657c478bd9Sstevel@tonic-gate 			++Bflag;
10667c478bd9Sstevel@tonic-gate 		} else if ((mt = open(usefile, 0)) < 0)
10677c478bd9Sstevel@tonic-gate 			vperror(1, "%s", usefile);
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 		if (xflag) {
10707c478bd9Sstevel@tonic-gate 			if (Aflag && vflag)
1071eace40a5Sceastha 				(void) printf(gettext(
1072eace40a5Sceastha 				    "Suppressing absolute pathnames.\n"));
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
10757c478bd9Sstevel@tonic-gate 			doxtract(argv, tbl_cnt);
10767c478bd9Sstevel@tonic-gate #else
10777c478bd9Sstevel@tonic-gate 			doxtract(argv);
10787c478bd9Sstevel@tonic-gate #endif
10797c478bd9Sstevel@tonic-gate 		} else if (tflag)
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
10827c478bd9Sstevel@tonic-gate 			dotable(argv, tbl_cnt);
10837c478bd9Sstevel@tonic-gate #else
10847c478bd9Sstevel@tonic-gate 			dotable(argv);
10857c478bd9Sstevel@tonic-gate #endif
10867c478bd9Sstevel@tonic-gate 	}
10877c478bd9Sstevel@tonic-gate 	else
10887c478bd9Sstevel@tonic-gate 		usage();
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	done(Errflg);
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	/* Not reached:  keep compiler quiet */
10937c478bd9Sstevel@tonic-gate 	return (1);
10947c478bd9Sstevel@tonic-gate }
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate static void
10977c478bd9Sstevel@tonic-gate usage(void)
10987c478bd9Sstevel@tonic-gate {
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
11017c478bd9Sstevel@tonic-gate 	if (sysv3_env) {
11027c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
11037c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
1104*da6c28aaSamw #if defined(_PC_SATTR_ENABLED)
1105*da6c28aaSamw 		"Usage: tar {c|r|t|u|x}[BDeEhilmnopPqTvw@/[0-7]][bfFk][X...] "
1106*da6c28aaSamw #else
110745916cd2Sjpk 		"Usage: tar {c|r|t|u|x}[BDeEhilmnopPqTvw@[0-7]][bfFk][X...] "
1108*da6c28aaSamw #endif	/* _PC_SATTR_ENABLED */
11097c478bd9Sstevel@tonic-gate #else
111045916cd2Sjpk 		"Usage: tar {c|r|t|u|x}[BDeEhilmnopPqTvw[0-7]][bfFk][X...] "
1111*da6c28aaSamw #endif	/* O_XATTR */
11127c478bd9Sstevel@tonic-gate 		"[blocksize] [tarfile] [filename] [size] [exclude-file...] "
11137c478bd9Sstevel@tonic-gate 		"{file | -I include-file | -C directory file}...\n"));
11147c478bd9Sstevel@tonic-gate 	} else
11157c478bd9Sstevel@tonic-gate #endif	/* _iBCS2 */
11167c478bd9Sstevel@tonic-gate 	{
11177c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
11187c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
1119*da6c28aaSamw #if defined(_PC_SATTR_ENABLED)
1120*da6c28aaSamw 		"Usage: tar {c|r|t|u|x}[BDeEFhilmnopPqTvw@/[0-7]][bfk][X...] "
1121*da6c28aaSamw #else
112245916cd2Sjpk 		"Usage: tar {c|r|t|u|x}[BDeEFhilmnopPqTvw@[0-7]][bfk][X...] "
1123*da6c28aaSamw #endif	/* _PC_SATTR_ENABLED */
11247c478bd9Sstevel@tonic-gate #else
112545916cd2Sjpk 		"Usage: tar {c|r|t|u|x}[BDeEFhilmnopPqTvw[0-7]][bfk][X...] "
1126*da6c28aaSamw #endif	/* O_XATTR */
11277c478bd9Sstevel@tonic-gate 		"[blocksize] [tarfile] [size] [exclude-file...] "
11287c478bd9Sstevel@tonic-gate 		"{file | -I include-file | -C directory file}...\n"));
11297c478bd9Sstevel@tonic-gate 	}
11307c478bd9Sstevel@tonic-gate 	done(1);
11317c478bd9Sstevel@tonic-gate }
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate /*
11347c478bd9Sstevel@tonic-gate  * dorep - do "replacements"
11357c478bd9Sstevel@tonic-gate  *
11367c478bd9Sstevel@tonic-gate  *	Dorep is responsible for creating ('c'),  appending ('r')
11377c478bd9Sstevel@tonic-gate  *	and updating ('u');
11387c478bd9Sstevel@tonic-gate  */
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate static void
11417c478bd9Sstevel@tonic-gate dorep(char *argv[])
11427c478bd9Sstevel@tonic-gate {
11437c478bd9Sstevel@tonic-gate 	char *cp, *cp2, *p;
11447c478bd9Sstevel@tonic-gate 	char wdir[PATH_MAX+2], tempdir[PATH_MAX+2], *parent;
11457c478bd9Sstevel@tonic-gate 	char file[PATH_MAX*2], origdir[PATH_MAX+1];
11467c478bd9Sstevel@tonic-gate 	FILE *fp = (FILE *)NULL;
11477c478bd9Sstevel@tonic-gate 	FILE *ff = (FILE *)NULL;
11487c478bd9Sstevel@tonic-gate 	int archtype;
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 	if (!cflag) {
11527c478bd9Sstevel@tonic-gate 		xhdr_flgs = 0;
11537c478bd9Sstevel@tonic-gate 		getdir();			/* read header for next file */
11547c478bd9Sstevel@tonic-gate 		if (Xhdrflag > 0) {
11557c478bd9Sstevel@tonic-gate 			if (!Eflag)
11567c478bd9Sstevel@tonic-gate 				fatal(gettext("Archive contains extended"
11577c478bd9Sstevel@tonic-gate 				    " header.  -E flag required.\n"));
11587c478bd9Sstevel@tonic-gate 			(void) get_xdata();	/* Get extended header items */
11597c478bd9Sstevel@tonic-gate 						/*   and regular header */
11607c478bd9Sstevel@tonic-gate 		} else {
11617c478bd9Sstevel@tonic-gate 			if (Eflag)
11627c478bd9Sstevel@tonic-gate 				fatal(gettext("Archive contains no extended"
11637c478bd9Sstevel@tonic-gate 				    " header.  -E flag not allowed.\n"));
11647c478bd9Sstevel@tonic-gate 		}
11657c478bd9Sstevel@tonic-gate 		while (!endtape()) {		/* changed from a do while */
11667c478bd9Sstevel@tonic-gate 			passtape();		/* skip the file data */
11677c478bd9Sstevel@tonic-gate 			if (term)
11687c478bd9Sstevel@tonic-gate 				done(Errflg);	/* received signal to stop */
11697c478bd9Sstevel@tonic-gate 			xhdr_flgs = 0;
11707c478bd9Sstevel@tonic-gate 			getdir();
11717c478bd9Sstevel@tonic-gate 			if (Xhdrflag > 0)
11727c478bd9Sstevel@tonic-gate 				(void) get_xdata();
11737c478bd9Sstevel@tonic-gate 		}
11747c478bd9Sstevel@tonic-gate 		backtape();			/* was called by endtape */
11757c478bd9Sstevel@tonic-gate 		if (tfile != NULL) {
11767c478bd9Sstevel@tonic-gate 			char buf[200];
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 			(void) sprintf(buf, "sort +0 -1 +1nr %s -o %s; awk '$1 "
11797c478bd9Sstevel@tonic-gate 			    "!= prev {print; prev=$1}' %s >%sX;mv %sX %s",
11807c478bd9Sstevel@tonic-gate 			    tname, tname, tname, tname, tname, tname);
11817c478bd9Sstevel@tonic-gate 			(void) fflush(tfile);
11827c478bd9Sstevel@tonic-gate 			(void) system(buf);
11837c478bd9Sstevel@tonic-gate 			(void) freopen(tname, "r", tfile);
11847c478bd9Sstevel@tonic-gate 			(void) fstat(fileno(tfile), &stbuf);
11857c478bd9Sstevel@tonic-gate 			high = stbuf.st_size;
11867c478bd9Sstevel@tonic-gate 		}
11877c478bd9Sstevel@tonic-gate 	}
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 	dumping = 1;
11907c478bd9Sstevel@tonic-gate 	if (mulvol) {	/* SP-1 */
11917c478bd9Sstevel@tonic-gate 		if (nblock && (blocklim%nblock) != 0)
11927c478bd9Sstevel@tonic-gate 			fatal(gettext(
11937c478bd9Sstevel@tonic-gate 			"Volume size not a multiple of block size."));
11947c478bd9Sstevel@tonic-gate 		blocklim -= 2;			/* for trailer records */
11957c478bd9Sstevel@tonic-gate 		if (vflag)
11967c478bd9Sstevel@tonic-gate 			(void) fprintf(vfile, gettext("Volume ends at %"
11977c478bd9Sstevel@tonic-gate 			    FMT_blkcnt_t "K, blocking factor = %dK\n"),
11987c478bd9Sstevel@tonic-gate 			    K((blocklim - 1)), K(nblock));
11997c478bd9Sstevel@tonic-gate 	}
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
12027c478bd9Sstevel@tonic-gate 	if (Fileflag) {
12037c478bd9Sstevel@tonic-gate 		if (Filefile != NULL) {
12047c478bd9Sstevel@tonic-gate 			if ((ff = fopen(Filefile, "r")) == NULL)
12057c478bd9Sstevel@tonic-gate 				vperror(0, "%s", Filefile);
12067c478bd9Sstevel@tonic-gate 		} else {
12077c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
12087c478bd9Sstevel@tonic-gate 			    "tar: F requires a file name.\n"));
12097c478bd9Sstevel@tonic-gate 			usage();
12107c478bd9Sstevel@tonic-gate 		}
12117c478bd9Sstevel@tonic-gate 	}
12127c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	/*
12157c478bd9Sstevel@tonic-gate 	 * Save the original directory before it gets
12167c478bd9Sstevel@tonic-gate 	 * changed.
12177c478bd9Sstevel@tonic-gate 	 */
12187c478bd9Sstevel@tonic-gate 	if (getcwd(origdir, (PATH_MAX+1)) == NULL) {
12197c478bd9Sstevel@tonic-gate 		vperror(0, gettext("A parent directory cannot be read"));
12207c478bd9Sstevel@tonic-gate 		exit(1);
12217c478bd9Sstevel@tonic-gate 	}
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	(void) strcpy(wdir, origdir);
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	while ((*argv || fp || ff) && !term) {
12267c478bd9Sstevel@tonic-gate 		if (fp || (strcmp(*argv, "-I") == 0)) {
12277c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
12287c478bd9Sstevel@tonic-gate 			if (Fileflag) {
12297c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
12307c478bd9Sstevel@tonic-gate 				"tar: only one of I or F.\n"));
12317c478bd9Sstevel@tonic-gate 				usage();
12327c478bd9Sstevel@tonic-gate 			}
12337c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
12347c478bd9Sstevel@tonic-gate 			if (fp == NULL) {
12357c478bd9Sstevel@tonic-gate 				if (*++argv == NULL)
12367c478bd9Sstevel@tonic-gate 					fatal(gettext(
12377c478bd9Sstevel@tonic-gate 					    "missing file name for -I flag."));
12387c478bd9Sstevel@tonic-gate 				else if ((fp = fopen(*argv++, "r")) == NULL)
12397c478bd9Sstevel@tonic-gate 					vperror(0, "%s", argv[-1]);
12407c478bd9Sstevel@tonic-gate 				continue;
12417c478bd9Sstevel@tonic-gate 			} else if ((fgets(file, PATH_MAX-1, fp)) == NULL) {
12427c478bd9Sstevel@tonic-gate 				(void) fclose(fp);
12437c478bd9Sstevel@tonic-gate 				fp = NULL;
12447c478bd9Sstevel@tonic-gate 				continue;
12457c478bd9Sstevel@tonic-gate 			} else {
12467c478bd9Sstevel@tonic-gate 				cp = cp2 = file;
12477c478bd9Sstevel@tonic-gate 				if ((p = strchr(cp2, '\n')))
12487c478bd9Sstevel@tonic-gate 					*p = 0;
12497c478bd9Sstevel@tonic-gate 			}
12507c478bd9Sstevel@tonic-gate 		} else if ((strcmp(*argv, "-C") == 0) && argv[1]) {
12517c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
12527c478bd9Sstevel@tonic-gate 			if (Fileflag) {
12537c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
12547c478bd9Sstevel@tonic-gate 				"tar: only one of F or C\n"));
12557c478bd9Sstevel@tonic-gate 				usage();
12567c478bd9Sstevel@tonic-gate 			}
12577c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 			if (chdir(*++argv) < 0)
12607c478bd9Sstevel@tonic-gate 				vperror(0, gettext(
12617c478bd9Sstevel@tonic-gate 				    "can't change directories to %s"), *argv);
12627c478bd9Sstevel@tonic-gate 			else
12637c478bd9Sstevel@tonic-gate 				(void) getcwd(wdir, (sizeof (wdir)));
12647c478bd9Sstevel@tonic-gate 			argv++;
12657c478bd9Sstevel@tonic-gate 			continue;
12667c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
12677c478bd9Sstevel@tonic-gate 		} else if (Fileflag && (ff != NULL)) {
12687c478bd9Sstevel@tonic-gate 			if ((fgets(file, PATH_MAX-1, ff)) == NULL) {
12697c478bd9Sstevel@tonic-gate 				(void) fclose(ff);
12707c478bd9Sstevel@tonic-gate 				ff = NULL;
12717c478bd9Sstevel@tonic-gate 				continue;
12727c478bd9Sstevel@tonic-gate 			} else {
12737c478bd9Sstevel@tonic-gate 				cp = cp2 = file;
12747c478bd9Sstevel@tonic-gate 				if (p = strchr(cp2, '\n'))
12757c478bd9Sstevel@tonic-gate 					*p = 0;
12767c478bd9Sstevel@tonic-gate 			}
12777c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
12787c478bd9Sstevel@tonic-gate 		} else
12797c478bd9Sstevel@tonic-gate 			cp = cp2 = strcpy(file, *argv++);
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 		/*
12827c478bd9Sstevel@tonic-gate 		 * point cp2 to the last '/' in file, but not
12837c478bd9Sstevel@tonic-gate 		 * to a trailing '/'
12847c478bd9Sstevel@tonic-gate 		 */
12857c478bd9Sstevel@tonic-gate 		for (; *cp; cp++) {
12867c478bd9Sstevel@tonic-gate 			if (*cp == '/') {
12877c478bd9Sstevel@tonic-gate 				while (*(cp+1) == '/') {
12887c478bd9Sstevel@tonic-gate 					++cp;
12897c478bd9Sstevel@tonic-gate 				}
12907c478bd9Sstevel@tonic-gate 				if (*(cp+1) != '\0') {
12917c478bd9Sstevel@tonic-gate 					/* not trailing slash */
12927c478bd9Sstevel@tonic-gate 					cp2 = cp;
12937c478bd9Sstevel@tonic-gate 				}
12947c478bd9Sstevel@tonic-gate 			}
12957c478bd9Sstevel@tonic-gate 		}
12967c478bd9Sstevel@tonic-gate 		if (cp2 != file) {
12977c478bd9Sstevel@tonic-gate 			*cp2 = '\0';
12987c478bd9Sstevel@tonic-gate 			if (chdir(file) < 0) {
12997c478bd9Sstevel@tonic-gate 				vperror(0, gettext(
13007c478bd9Sstevel@tonic-gate 				    "can't change directories to %s"), file);
13017c478bd9Sstevel@tonic-gate 				continue;
13027c478bd9Sstevel@tonic-gate 			}
13037c478bd9Sstevel@tonic-gate 			*cp2 = '/';
13047c478bd9Sstevel@tonic-gate 			cp2++;
13057c478bd9Sstevel@tonic-gate 		}
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate 		parent = getcwd(tempdir, (sizeof (tempdir)));
1308*da6c28aaSamw 
1309*da6c28aaSamw 		archtype = putfile(file, cp2, parent, NULL, NORMAL_FILE,
13107c478bd9Sstevel@tonic-gate 		    LEV0, SYMLINK_LEV0);
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
13137c478bd9Sstevel@tonic-gate 		if (!exitflag) {
1314*da6c28aaSamw 			if ((atflag || saflag) &&
1315*da6c28aaSamw 			    (archtype == PUT_NOTAS_LINK)) {
1316*da6c28aaSamw 				xattrs_put(file, cp2, parent, NULL);
13177c478bd9Sstevel@tonic-gate 			}
13187c478bd9Sstevel@tonic-gate 		}
13197c478bd9Sstevel@tonic-gate #endif
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 		if (chdir(origdir) < 0)
13227c478bd9Sstevel@tonic-gate 			vperror(0, gettext("cannot change back?: %s"), origdir);
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate 		if (exitflag) {
13257c478bd9Sstevel@tonic-gate 			/*
13267c478bd9Sstevel@tonic-gate 			 * If e function modifier has been specified
13277c478bd9Sstevel@tonic-gate 			 * write the files (that are listed before the
13287c478bd9Sstevel@tonic-gate 			 * file causing the error) to tape.  exitflag is
13297c478bd9Sstevel@tonic-gate 			 * used because only some of the error conditions
13307c478bd9Sstevel@tonic-gate 			 * in putfile() recognize the e function modifier.
13317c478bd9Sstevel@tonic-gate 			 */
13327c478bd9Sstevel@tonic-gate 			break;
13337c478bd9Sstevel@tonic-gate 		}
13347c478bd9Sstevel@tonic-gate 	}
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 	putempty((blkcnt_t)2);
13377c478bd9Sstevel@tonic-gate 	flushtape();
13387c478bd9Sstevel@tonic-gate 	closevol();	/* SP-1 */
13397c478bd9Sstevel@tonic-gate 	if (linkerrok == 1)
13407c478bd9Sstevel@tonic-gate 		for (; ihead != NULL; ihead = ihead->nextp) {
13417c478bd9Sstevel@tonic-gate 			if (ihead->count == 0)
13427c478bd9Sstevel@tonic-gate 				continue;
13437c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
13447c478bd9Sstevel@tonic-gate 			"tar: missing links to %s\n"), ihead->pathname);
13457c478bd9Sstevel@tonic-gate 			if (errflag)
13467c478bd9Sstevel@tonic-gate 				done(1);
13477c478bd9Sstevel@tonic-gate 			else
13487c478bd9Sstevel@tonic-gate 				Errflg = 1;
13497c478bd9Sstevel@tonic-gate 		}
13507c478bd9Sstevel@tonic-gate }
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate /*
13547c478bd9Sstevel@tonic-gate  * endtape - check for tape at end
13557c478bd9Sstevel@tonic-gate  *
13567c478bd9Sstevel@tonic-gate  *	endtape checks the entry in dblock.dbuf to see if its the
13577c478bd9Sstevel@tonic-gate  *	special EOT entry.  Endtape is usually called after getdir().
13587c478bd9Sstevel@tonic-gate  *
13597c478bd9Sstevel@tonic-gate  *	endtape used to call backtape; it no longer does, he who
13607c478bd9Sstevel@tonic-gate  *	wants it backed up must call backtape himself
13617c478bd9Sstevel@tonic-gate  *	RETURNS:	0 if not EOT, tape position unaffected
13627c478bd9Sstevel@tonic-gate  *			1 if	 EOT, tape position unaffected
13637c478bd9Sstevel@tonic-gate  */
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate static int
13667c478bd9Sstevel@tonic-gate endtape(void)
13677c478bd9Sstevel@tonic-gate {
13687c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.name[0] == '\0') {	/* null header = EOT */
13697c478bd9Sstevel@tonic-gate 		return (1);
13707c478bd9Sstevel@tonic-gate 	} else
13717c478bd9Sstevel@tonic-gate 		return (0);
13727c478bd9Sstevel@tonic-gate }
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate /*
13757c478bd9Sstevel@tonic-gate  *	getdir - get directory entry from tar tape
13767c478bd9Sstevel@tonic-gate  *
13777c478bd9Sstevel@tonic-gate  *	getdir reads the next tarblock off the tape and cracks
13787c478bd9Sstevel@tonic-gate  *	it as a directory. The checksum must match properly.
13797c478bd9Sstevel@tonic-gate  *
13807c478bd9Sstevel@tonic-gate  *	If tfile is non-null getdir writes the file name and mod date
13817c478bd9Sstevel@tonic-gate  *	to tfile.
13827c478bd9Sstevel@tonic-gate  */
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate static void
13857c478bd9Sstevel@tonic-gate getdir(void)
13867c478bd9Sstevel@tonic-gate {
13877c478bd9Sstevel@tonic-gate 	struct stat *sp;
13887c478bd9Sstevel@tonic-gate #ifdef EUC
13897c478bd9Sstevel@tonic-gate 	static int warn_chksum_sign = 0;
13907c478bd9Sstevel@tonic-gate #endif /* EUC */
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate top:
13937c478bd9Sstevel@tonic-gate 	readtape((char *)&dblock);
13947c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.name[0] == '\0')
13957c478bd9Sstevel@tonic-gate 		return;
13967c478bd9Sstevel@tonic-gate 	sp = &stbuf;
13977c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.mode, "%8lo", &Gen.g_mode);
13987c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.uid, "%8lo", (ulong_t *)&Gen.g_uid);
13997c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.gid, "%8lo", (ulong_t *)&Gen.g_gid);
14007c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.size, "%12" FMT_off_t_o, &Gen.g_filesz);
14017c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.mtime, "%12lo", (ulong_t *)&Gen.g_mtime);
14027c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.chksum, "%8o", &Gen.g_cksum);
14037c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.devmajor, "%8lo", &Gen.g_devmajor);
14047c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.devminor, "%8lo", &Gen.g_devminor);
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 	is_posix = (strcmp(dblock.dbuf.magic, magic_type) == 0);
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 	sp->st_mode = Gen.g_mode;
14097c478bd9Sstevel@tonic-gate 	if (is_posix && (sp->st_mode & S_IFMT) == 0)
14107c478bd9Sstevel@tonic-gate 		switch (dblock.dbuf.typeflag) {
14117c478bd9Sstevel@tonic-gate 		case '0': case 0: case _XATTR_HDRTYPE:
14127c478bd9Sstevel@tonic-gate 			sp->st_mode |= S_IFREG;
14137c478bd9Sstevel@tonic-gate 			break;
14147c478bd9Sstevel@tonic-gate 		case '1':	/* hard link */
14157c478bd9Sstevel@tonic-gate 			break;
14167c478bd9Sstevel@tonic-gate 		case '2':
14177c478bd9Sstevel@tonic-gate 			sp->st_mode |= S_IFLNK;
14187c478bd9Sstevel@tonic-gate 			break;
14197c478bd9Sstevel@tonic-gate 		case '3':
14207c478bd9Sstevel@tonic-gate 			sp->st_mode |= S_IFCHR;
14217c478bd9Sstevel@tonic-gate 			break;
14227c478bd9Sstevel@tonic-gate 		case '4':
14237c478bd9Sstevel@tonic-gate 			sp->st_mode |= S_IFBLK;
14247c478bd9Sstevel@tonic-gate 			break;
14257c478bd9Sstevel@tonic-gate 		case '5':
14267c478bd9Sstevel@tonic-gate 			sp->st_mode |= S_IFDIR;
14277c478bd9Sstevel@tonic-gate 			break;
14287c478bd9Sstevel@tonic-gate 		case '6':
14297c478bd9Sstevel@tonic-gate 			sp->st_mode |= S_IFIFO;
14307c478bd9Sstevel@tonic-gate 			break;
14317c478bd9Sstevel@tonic-gate 		default:
14327c478bd9Sstevel@tonic-gate 			if (convtoreg(Gen.g_filesz))
14337c478bd9Sstevel@tonic-gate 				sp->st_mode |= S_IFREG;
14347c478bd9Sstevel@tonic-gate 			break;
14357c478bd9Sstevel@tonic-gate 		}
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.typeflag == 'X')
14387c478bd9Sstevel@tonic-gate 		Xhdrflag = 1;	/* Currently processing extended header */
14397c478bd9Sstevel@tonic-gate 	else
14407c478bd9Sstevel@tonic-gate 		Xhdrflag = 0;
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 	sp->st_uid = Gen.g_uid;
14437c478bd9Sstevel@tonic-gate 	sp->st_gid = Gen.g_gid;
14447c478bd9Sstevel@tonic-gate 	sp->st_size = Gen.g_filesz;
14457c478bd9Sstevel@tonic-gate 	sp->st_mtime = Gen.g_mtime;
14467c478bd9Sstevel@tonic-gate 	chksum = Gen.g_cksum;
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.extno != '\0') {	/* split file? */
14497c478bd9Sstevel@tonic-gate 		extno = dblock.dbuf.extno;
14507c478bd9Sstevel@tonic-gate 		extsize = Gen.g_filesz;
14517c478bd9Sstevel@tonic-gate 		extotal = dblock.dbuf.extotal;
14527c478bd9Sstevel@tonic-gate 	} else {
14537c478bd9Sstevel@tonic-gate 		extno = 0;	/* tell others file not split */
14547c478bd9Sstevel@tonic-gate 		extsize = 0;
14557c478bd9Sstevel@tonic-gate 		extotal = 0;
14567c478bd9Sstevel@tonic-gate 	}
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate #ifdef	EUC
14597c478bd9Sstevel@tonic-gate 	if (chksum != checksum(&dblock)) {
14607c478bd9Sstevel@tonic-gate 		if (chksum != checksum_signed(&dblock)) {
14617c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
14627c478bd9Sstevel@tonic-gate 			    "tar: directory checksum error\n"));
14637c478bd9Sstevel@tonic-gate 			if (iflag)
14647c478bd9Sstevel@tonic-gate 				goto top;
14657c478bd9Sstevel@tonic-gate 			done(2);
14667c478bd9Sstevel@tonic-gate 		} else {
14677c478bd9Sstevel@tonic-gate 			if (! warn_chksum_sign) {
14687c478bd9Sstevel@tonic-gate 				warn_chksum_sign = 1;
14697c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
14707c478bd9Sstevel@tonic-gate 			"tar: warning: tar file made with signed checksum\n"));
14717c478bd9Sstevel@tonic-gate 			}
14727c478bd9Sstevel@tonic-gate 		}
14737c478bd9Sstevel@tonic-gate 	}
14747c478bd9Sstevel@tonic-gate #else
14757c478bd9Sstevel@tonic-gate 	if (chksum != checksum(&dblock)) {
14767c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
14777c478bd9Sstevel@tonic-gate 		"tar: directory checksum error\n"));
14787c478bd9Sstevel@tonic-gate 		if (iflag)
14797c478bd9Sstevel@tonic-gate 			goto top;
14807c478bd9Sstevel@tonic-gate 		done(2);
14817c478bd9Sstevel@tonic-gate 	}
14827c478bd9Sstevel@tonic-gate #endif	/* EUC */
14837c478bd9Sstevel@tonic-gate 	if (tfile != NULL && Xhdrflag == 0) {
14847c478bd9Sstevel@tonic-gate 		/*
14857c478bd9Sstevel@tonic-gate 		 * If an extended header is present, then time is available
14867c478bd9Sstevel@tonic-gate 		 * in nanoseconds in the extended header data, so set it.
14877c478bd9Sstevel@tonic-gate 		 * Otherwise, give an invalid value so that checkupdate will
14887c478bd9Sstevel@tonic-gate 		 * not test beyond seconds.
14897c478bd9Sstevel@tonic-gate 		 */
14907c478bd9Sstevel@tonic-gate 		if ((xhdr_flgs & _X_MTIME))
14917c478bd9Sstevel@tonic-gate 			sp->st_mtim.tv_nsec = Xtarhdr.x_mtime.tv_nsec;
14927c478bd9Sstevel@tonic-gate 		else
14937c478bd9Sstevel@tonic-gate 			sp->st_mtim.tv_nsec = -1;
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_PATH)
14967c478bd9Sstevel@tonic-gate 			(void) fprintf(tfile, "%s %10ld.%9.9ld\n",
14977c478bd9Sstevel@tonic-gate 			    Xtarhdr.x_path, sp->st_mtim.tv_sec,
14987c478bd9Sstevel@tonic-gate 			    sp->st_mtim.tv_nsec);
14997c478bd9Sstevel@tonic-gate 		else
15007c478bd9Sstevel@tonic-gate 			(void) fprintf(tfile, "%.*s %10ld.%9.9ld\n",
15017c478bd9Sstevel@tonic-gate 			    NAMSIZ, dblock.dbuf.name, sp->st_mtim.tv_sec,
15027c478bd9Sstevel@tonic-gate 			    sp->st_mtim.tv_nsec);
15037c478bd9Sstevel@tonic-gate 	}
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
15067c478bd9Sstevel@tonic-gate 	Hiddendir = 0;
15077c478bd9Sstevel@tonic-gate 	if (xattrp && dblock.dbuf.typeflag == _XATTR_HDRTYPE) {
15087c478bd9Sstevel@tonic-gate 		if (xattrbadhead) {
15097c478bd9Sstevel@tonic-gate 			free(xattrhead);
15107c478bd9Sstevel@tonic-gate 			xattrp = NULL;
15117c478bd9Sstevel@tonic-gate 			xattr_linkp = NULL;
15127c478bd9Sstevel@tonic-gate 			xattrhead = NULL;
15137c478bd9Sstevel@tonic-gate 		} else {
1514*da6c28aaSamw 			char	*aname = basename(xattrapath);
1515*da6c28aaSamw 			size_t	xindex  = aname - xattrapath;
1516*da6c28aaSamw 
1517*da6c28aaSamw 			if (xattrapath[xindex] == '.' &&
1518*da6c28aaSamw 			    xattrapath[xindex + 1] == '\0' &&
15197c478bd9Sstevel@tonic-gate 			    xattrp->h_typeflag == '5') {
15207c478bd9Sstevel@tonic-gate 				Hiddendir = 1;
15217c478bd9Sstevel@tonic-gate 				sp->st_mode =
1522d2443e76Smarks 				    (S_IFDIR | (sp->st_mode & POSIXMODES));
15237c478bd9Sstevel@tonic-gate 			}
15247c478bd9Sstevel@tonic-gate 			dblock.dbuf.typeflag = xattrp->h_typeflag;
15257c478bd9Sstevel@tonic-gate 		}
15267c478bd9Sstevel@tonic-gate 	}
15277c478bd9Sstevel@tonic-gate #endif
15287c478bd9Sstevel@tonic-gate }
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 
15317c478bd9Sstevel@tonic-gate /*
15327c478bd9Sstevel@tonic-gate  *	passtape - skip over a file on the tape
15337c478bd9Sstevel@tonic-gate  *
15347c478bd9Sstevel@tonic-gate  *	passtape skips over the next data file on the tape.
15357c478bd9Sstevel@tonic-gate  *	The tape directory entry must be in dblock.dbuf. This
15367c478bd9Sstevel@tonic-gate  *	routine just eats the number of blocks computed from the
15377c478bd9Sstevel@tonic-gate  *	directory size entry; the tape must be (logically) positioned
15387c478bd9Sstevel@tonic-gate  *	right after thee directory info.
15397c478bd9Sstevel@tonic-gate  */
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate static void
15427c478bd9Sstevel@tonic-gate passtape(void)
15437c478bd9Sstevel@tonic-gate {
15447c478bd9Sstevel@tonic-gate 	blkcnt_t blocks;
15457c478bd9Sstevel@tonic-gate 	char buf[TBLOCK];
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 	/*
15487c478bd9Sstevel@tonic-gate 	 * Types link(1), sym-link(2), char special(3), blk special(4),
15497c478bd9Sstevel@tonic-gate 	 *  directory(5), and FIFO(6) do not have data blocks associated
15507c478bd9Sstevel@tonic-gate 	 *  with them so just skip reading the data block.
15517c478bd9Sstevel@tonic-gate 	 */
15527c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.typeflag == '1' || dblock.dbuf.typeflag == '2' ||
15537c478bd9Sstevel@tonic-gate 	    dblock.dbuf.typeflag == '3' || dblock.dbuf.typeflag == '4' ||
15547c478bd9Sstevel@tonic-gate 	    dblock.dbuf.typeflag == '5' || dblock.dbuf.typeflag == '6')
15557c478bd9Sstevel@tonic-gate 		return;
15567c478bd9Sstevel@tonic-gate 	blocks = TBLOCKS(stbuf.st_size);
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate 	/* if operating on disk, seek instead of reading */
15597c478bd9Sstevel@tonic-gate 	if (NotTape)
15607c478bd9Sstevel@tonic-gate 		seekdisk(blocks);
15617c478bd9Sstevel@tonic-gate 	else
15627c478bd9Sstevel@tonic-gate 		while (blocks-- > 0)
15637c478bd9Sstevel@tonic-gate 			readtape(buf);
15647c478bd9Sstevel@tonic-gate }
15657c478bd9Sstevel@tonic-gate 
1566*da6c28aaSamw #if defined(O_XATTR)
1567*da6c28aaSamw static int
1568*da6c28aaSamw is_sysattr(char *name)
1569*da6c28aaSamw {
1570*da6c28aaSamw 	return ((strcmp(name, VIEW_READONLY) == 0) ||
1571*da6c28aaSamw 	    (strcmp(name, VIEW_READWRITE) == 0));
1572*da6c28aaSamw }
1573*da6c28aaSamw #endif
1574*da6c28aaSamw 
1575*da6c28aaSamw #if defined(O_XATTR)
1576*da6c28aaSamw /*
1577*da6c28aaSamw  * Verify the attribute, attrname, is an attribute we want to restore.
1578*da6c28aaSamw  * Never restore read-only system attribute files.  Only restore read-write
1579*da6c28aaSamw  * system attributes files when -/ was specified, and only traverse into
1580*da6c28aaSamw  * the 2nd level attribute directory containing only system attributes if
1581*da6c28aaSamw  * -@ was specified.  This keeps us from archiving
1582*da6c28aaSamw  *	<attribute name>/<read-write system attribute file>
1583*da6c28aaSamw  * when -/ was specified without -@.
1584*da6c28aaSamw  *
1585*da6c28aaSamw  * attrname	- attribute file name
1586*da6c28aaSamw  * attrparent	- attribute's parent name within the base file's attribute
1587*da6c28aaSamw  *		directory hierarchy
1588*da6c28aaSamw  */
1589*da6c28aaSamw static attr_status_t
1590*da6c28aaSamw verify_attr(char *attrname, char *attrparent, int arc_rwsysattr,
1591*da6c28aaSamw     int *rw_sysattr)
1592*da6c28aaSamw {
1593*da6c28aaSamw #if defined(_PC_SATTR_ENABLED)
1594*da6c28aaSamw 	int	attr_supported;
1595*da6c28aaSamw 
1596*da6c28aaSamw 	/* Never restore read-only system attribute files */
1597*da6c28aaSamw 	if ((attr_supported = sysattr_type(attrname)) == _RO_SATTR) {
1598*da6c28aaSamw 		*rw_sysattr = 0;
1599*da6c28aaSamw 		return (ATTR_SKIP);
1600*da6c28aaSamw 	} else {
1601*da6c28aaSamw 		*rw_sysattr = (attr_supported == _RW_SATTR);
1602*da6c28aaSamw 	}
1603*da6c28aaSamw #else
1604*da6c28aaSamw 	/*
1605*da6c28aaSamw 	 * Only need to check if this attribute is an extended system
1606*da6c28aaSamw 	 * attribute.
1607*da6c28aaSamw 	 */
1608*da6c28aaSamw 	if (*rw_sysattr = is_sysattr(attrname)) {
1609*da6c28aaSamw 		return (ATTR_SKIP);
1610*da6c28aaSamw 	} else {
1611*da6c28aaSamw 		return (ATTR_OK);
1612*da6c28aaSamw 	}
1613*da6c28aaSamw #endif	/* _PC_SATTR_ENABLED */
1614*da6c28aaSamw 
1615*da6c28aaSamw 	/*
1616*da6c28aaSamw 	 * If the extended system attribute file is specified with the
1617*da6c28aaSamw 	 * arc_rwsysattr flag, as being transient (default extended
1618*da6c28aaSamw 	 * attributes), then don't archive it.
1619*da6c28aaSamw 	 */
1620*da6c28aaSamw 	if (*rw_sysattr && !arc_rwsysattr) {
1621*da6c28aaSamw 		return (ATTR_SKIP);
1622*da6c28aaSamw 	}
1623*da6c28aaSamw 
1624*da6c28aaSamw 	/*
1625*da6c28aaSamw 	 * Only restore read-write system attribute files
1626*da6c28aaSamw 	 * when -/ was specified.  Only restore extended
1627*da6c28aaSamw 	 * attributes when -@ was specified.
1628*da6c28aaSamw 	 */
1629*da6c28aaSamw 	if (atflag) {
1630*da6c28aaSamw 		if (!saflag) {
1631*da6c28aaSamw 			/*
1632*da6c28aaSamw 			 * Only archive/restore the hidden directory "." if
1633*da6c28aaSamw 			 * we're processing the top level hidden attribute
1634*da6c28aaSamw 			 * directory.  We don't want to process the
1635*da6c28aaSamw 			 * hidden attribute directory of the attribute
1636*da6c28aaSamw 			 * directory that contains only extended system
1637*da6c28aaSamw 			 * attributes.
1638*da6c28aaSamw 			 */
1639*da6c28aaSamw 			if (*rw_sysattr || (Hiddendir &&
1640*da6c28aaSamw 			    (attrparent != NULL))) {
1641*da6c28aaSamw 				return (ATTR_SKIP);
1642*da6c28aaSamw 			}
1643*da6c28aaSamw 		}
1644*da6c28aaSamw 	} else if (saflag) {
1645*da6c28aaSamw 		/*
1646*da6c28aaSamw 		 * Only archive/restore read-write extended system attribute
1647*da6c28aaSamw 		 * files of the base file.
1648*da6c28aaSamw 		 */
1649*da6c28aaSamw 		if (!*rw_sysattr || (attrparent != NULL)) {
1650*da6c28aaSamw 			return (ATTR_SKIP);
1651*da6c28aaSamw 		}
1652*da6c28aaSamw 	} else {
1653*da6c28aaSamw 		return (ATTR_SKIP);
1654*da6c28aaSamw 	}
1655*da6c28aaSamw 
1656*da6c28aaSamw 	return (ATTR_OK);
1657*da6c28aaSamw }
1658*da6c28aaSamw #endif
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate static int
1661*da6c28aaSamw putfile(char *longname, char *shortname, char *parent, attr_data_t *attrinfo,
16627c478bd9Sstevel@tonic-gate     int filetype, int lev, int symlink_lev)
16637c478bd9Sstevel@tonic-gate {
16647c478bd9Sstevel@tonic-gate 	int infile = -1;	/* deliberately invalid */
16657c478bd9Sstevel@tonic-gate 	blkcnt_t blocks;
16667c478bd9Sstevel@tonic-gate 	char buf[PATH_MAX + 2];	/* Add trailing slash and null */
16677c478bd9Sstevel@tonic-gate 	char *bigbuf;
16687c478bd9Sstevel@tonic-gate 	int	maxread;
16697c478bd9Sstevel@tonic-gate 	int	hint;		/* amount to write to get "in sync" */
16707c478bd9Sstevel@tonic-gate 	char filetmp[PATH_MAX + 1];
16717c478bd9Sstevel@tonic-gate 	char *cp;
16727c478bd9Sstevel@tonic-gate 	char *name;
1673*da6c28aaSamw 	char *attrparent = NULL;
1674*da6c28aaSamw 	char *longattrname = NULL;
16757c478bd9Sstevel@tonic-gate 	struct dirent *dp;
16767c478bd9Sstevel@tonic-gate 	DIR *dirp;
16777c478bd9Sstevel@tonic-gate 	int i;
16787c478bd9Sstevel@tonic-gate 	long l;
16797c478bd9Sstevel@tonic-gate 	int split;
16807c478bd9Sstevel@tonic-gate 	int dirfd = -1;
16817c478bd9Sstevel@tonic-gate 	int rc = PUT_NOTAS_LINK;
16827c478bd9Sstevel@tonic-gate 	int archtype = 0;
1683*da6c28aaSamw 	int rw_sysattr = 0;
16847c478bd9Sstevel@tonic-gate 	char newparent[PATH_MAX + MAXNAMLEN + 1];
16857c478bd9Sstevel@tonic-gate 	char *prefix = "";
16867c478bd9Sstevel@tonic-gate 	char *tmpbuf;
16877c478bd9Sstevel@tonic-gate 	char goodbuf[PRESIZ + 2];
16887c478bd9Sstevel@tonic-gate 	char junkbuf[MAXNAM+1];
16897c478bd9Sstevel@tonic-gate 	char *lastslash;
16907c478bd9Sstevel@tonic-gate 	int j;
16917c478bd9Sstevel@tonic-gate 	struct stat sbuf;
16927c478bd9Sstevel@tonic-gate 	int readlink_max;
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 	(void) memset(goodbuf, '\0', sizeof (goodbuf));
16957c478bd9Sstevel@tonic-gate 	(void) memset(junkbuf, '\0', sizeof (junkbuf));
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 	xhdr_flgs = 0;
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate 	if (filetype == XATTR_FILE) {
1700*da6c28aaSamw 		attrparent = attrinfo->attr_parent;
1701*da6c28aaSamw 		longattrname = attrinfo->attr_path;
1702*da6c28aaSamw 		dirfd = attrinfo->attr_parentfd;
17037c478bd9Sstevel@tonic-gate 	} else {
17047c478bd9Sstevel@tonic-gate 		dirfd = open(".", O_RDONLY);
17057c478bd9Sstevel@tonic-gate 	}
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 	if (dirfd == -1) {
17087c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
1709*da6c28aaSamw 		    "tar: unable to open%sdirectory %s%s%s%s\n"),
17107c478bd9Sstevel@tonic-gate 		    (filetype == XATTR_FILE) ? gettext(" attribute ") : " ",
1711*da6c28aaSamw 		    (attrparent == NULL) ? "" : gettext("of attribute "),
1712*da6c28aaSamw 		    (attrparent == NULL) ? "" : attrparent,
1713*da6c28aaSamw 		    (attrparent == NULL) ? "" : gettext(" of "),
17147c478bd9Sstevel@tonic-gate 		    (filetype == XATTR_FILE) ? longname : parent);
17157c478bd9Sstevel@tonic-gate 		goto out;
17167c478bd9Sstevel@tonic-gate 	}
17177c478bd9Sstevel@tonic-gate 
17187c478bd9Sstevel@tonic-gate 	if (lev > MAXLEV) {
17197c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
17207c478bd9Sstevel@tonic-gate 		    gettext("tar: directory nesting too deep, %s not dumped\n"),
17217c478bd9Sstevel@tonic-gate 		    longname);
17227c478bd9Sstevel@tonic-gate 		goto out;
17237c478bd9Sstevel@tonic-gate 	}
17247c478bd9Sstevel@tonic-gate 
1725*da6c28aaSamw 	if (getstat(dirfd, longname, shortname, attrparent))
17267c478bd9Sstevel@tonic-gate 		goto out;
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 	if (hflag) {
17297c478bd9Sstevel@tonic-gate 		/*
17307c478bd9Sstevel@tonic-gate 		 * Catch nesting where a file is a symlink to its directory.
17317c478bd9Sstevel@tonic-gate 		 */
17327c478bd9Sstevel@tonic-gate 		j = fstatat(dirfd, shortname, &sbuf, AT_SYMLINK_NOFOLLOW);
17337c478bd9Sstevel@tonic-gate 		if (S_ISLNK(sbuf.st_mode)) {
17347c478bd9Sstevel@tonic-gate 			if (symlink_lev++ >= MAXSYMLINKS) {
17357c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
17367c478bd9Sstevel@tonic-gate 				    "tar: %s: Number of symbolic links "
17377c478bd9Sstevel@tonic-gate 				    "encountered during path name traversal "
17387c478bd9Sstevel@tonic-gate 				    "exceeds MAXSYMLINKS\n"), longname);
17397c478bd9Sstevel@tonic-gate 				Errflg = 1;
17407c478bd9Sstevel@tonic-gate 				goto out;
17417c478bd9Sstevel@tonic-gate 			}
17427c478bd9Sstevel@tonic-gate 		}
17437c478bd9Sstevel@tonic-gate 	}
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate 	/*
17467c478bd9Sstevel@tonic-gate 	 * Check if the input file is the same as the tar file we
17477c478bd9Sstevel@tonic-gate 	 * are creating
17487c478bd9Sstevel@tonic-gate 	 */
17497c478bd9Sstevel@tonic-gate 	if ((mt_ino == stbuf.st_ino) && (mt_dev == stbuf.st_dev)) {
17507c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
1751*da6c28aaSamw 		    "tar: %s%s%s%s%s same as archive file\n"),
1752*da6c28aaSamw 		    rw_sysattr ? gettext("system ") : "",
1753*da6c28aaSamw 		    (longattrname == NULL) ? "" : gettext("attribute "),
1754*da6c28aaSamw 		    (longattrname == NULL) ? "" : longattrname,
1755*da6c28aaSamw 		    (longattrname == NULL) ? "" : gettext(" of "),
1756*da6c28aaSamw 		    longname);
17577c478bd9Sstevel@tonic-gate 		Errflg = 1;
17587c478bd9Sstevel@tonic-gate 		goto out;
17597c478bd9Sstevel@tonic-gate 	}
17607c478bd9Sstevel@tonic-gate 	/*
17617c478bd9Sstevel@tonic-gate 	 * Check size limit - we can't archive files that
17627c478bd9Sstevel@tonic-gate 	 * exceed TAR_OFFSET_MAX bytes because of header
17637c478bd9Sstevel@tonic-gate 	 * limitations. Exclude file types that set
17647c478bd9Sstevel@tonic-gate 	 * st_size to zero below because they take no
17657c478bd9Sstevel@tonic-gate 	 * archive space to represent contents.
17667c478bd9Sstevel@tonic-gate 	 */
17677c478bd9Sstevel@tonic-gate 	if ((stbuf.st_size > (off_t)TAR_OFFSET_MAX) &&
17687c478bd9Sstevel@tonic-gate 	    !S_ISDIR(stbuf.st_mode) &&
17697c478bd9Sstevel@tonic-gate 	    !S_ISCHR(stbuf.st_mode) &&
17707c478bd9Sstevel@tonic-gate 	    !S_ISBLK(stbuf.st_mode) &&
17717c478bd9Sstevel@tonic-gate 	    (Eflag == 0)) {
17727c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
1773*da6c28aaSamw 		    "tar: %s%s%s%s%s too large to archive.  "
1774*da6c28aaSamw 		    "Use E function modifier.\n"),
1775*da6c28aaSamw 		    rw_sysattr ? gettext("system ") : "",
1776*da6c28aaSamw 		    (longattrname == NULL) ? "" : gettext("attribute "),
1777*da6c28aaSamw 		    (longattrname == NULL) ? "" : longattrname,
1778*da6c28aaSamw 		    (longattrname == NULL) ? "" : gettext(" of "),
1779*da6c28aaSamw 		    longname);
17807c478bd9Sstevel@tonic-gate 		if (errflag)
17817c478bd9Sstevel@tonic-gate 			exitflag = 1;
17827c478bd9Sstevel@tonic-gate 		Errflg = 1;
17837c478bd9Sstevel@tonic-gate 		goto out;
17847c478bd9Sstevel@tonic-gate 	}
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 	if (tfile != NULL && checkupdate(longname) == 0) {
17877c478bd9Sstevel@tonic-gate 		goto out;
17887c478bd9Sstevel@tonic-gate 	}
17897c478bd9Sstevel@tonic-gate 	if (checkw('r', longname) == 0) {
17907c478bd9Sstevel@tonic-gate 		goto out;
17917c478bd9Sstevel@tonic-gate 	}
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 	if (Fflag && checkf(shortname, stbuf.st_mode, Fflag) == 0)
17947c478bd9Sstevel@tonic-gate 		goto out;
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 	if (Xflag) {
17977c478bd9Sstevel@tonic-gate 		if (is_in_table(exclude_tbl, longname)) {
17987c478bd9Sstevel@tonic-gate 			if (vflag) {
17997c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext(
18007c478bd9Sstevel@tonic-gate 				    "a %s excluded\n"), longname);
18017c478bd9Sstevel@tonic-gate 			}
18027c478bd9Sstevel@tonic-gate 			goto out;
18037c478bd9Sstevel@tonic-gate 		}
18047c478bd9Sstevel@tonic-gate 	}
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 	/*
18077c478bd9Sstevel@tonic-gate 	 * If the length of the fullname is greater than MAXNAM,
18087c478bd9Sstevel@tonic-gate 	 * print out a message and return (unless extended headers are used,
18097c478bd9Sstevel@tonic-gate 	 * in which case fullname is limited to PATH_MAX).
18107c478bd9Sstevel@tonic-gate 	 */
18117c478bd9Sstevel@tonic-gate 
18127c478bd9Sstevel@tonic-gate 	if ((((split = (int)strlen(longname)) > MAXNAM) && (Eflag == 0)) ||
18137c478bd9Sstevel@tonic-gate 	    (split > PATH_MAX)) {
18147c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
18157c478bd9Sstevel@tonic-gate 		    "tar: %s: file name too long\n"), longname);
18167c478bd9Sstevel@tonic-gate 		if (errflag)
18177c478bd9Sstevel@tonic-gate 			exitflag = 1;
18187c478bd9Sstevel@tonic-gate 		Errflg = 1;
18197c478bd9Sstevel@tonic-gate 		goto out;
18207c478bd9Sstevel@tonic-gate 	}
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate 	/*
18237c478bd9Sstevel@tonic-gate 	 * We split the fullname into prefix and name components if any one
18247c478bd9Sstevel@tonic-gate 	 * of three conditions holds:
18257c478bd9Sstevel@tonic-gate 	 *	-- the length of the fullname exceeds NAMSIZ,
18267c478bd9Sstevel@tonic-gate 	 *	-- the length of the fullname equals NAMSIZ, and the shortname
18277c478bd9Sstevel@tonic-gate 	 *	   is less than NAMSIZ, (splitting in this case preserves
18287c478bd9Sstevel@tonic-gate 	 *	   compatibility with 5.6 and 5.5.1 tar), or
18297c478bd9Sstevel@tonic-gate 	 * 	-- the length of the fullname equals NAMSIZ, the file is a
18307c478bd9Sstevel@tonic-gate 	 *	   directory and we are not in POSIX-conformant mode (where
18317c478bd9Sstevel@tonic-gate 	 *	   trailing slashes are removed from directories).
18327c478bd9Sstevel@tonic-gate 	 */
18337c478bd9Sstevel@tonic-gate 	if ((split > NAMSIZ) ||
18347c478bd9Sstevel@tonic-gate 	    (split == NAMSIZ && strlen(shortname) < NAMSIZ) ||
18354bc0a2efScasper 	    (split == NAMSIZ && S_ISDIR(stbuf.st_mode) && !Pflag)) {
18367c478bd9Sstevel@tonic-gate 		/*
18377c478bd9Sstevel@tonic-gate 		 * Since path is limited to PRESIZ characters, look for the
18387c478bd9Sstevel@tonic-gate 		 * last slash within PRESIZ + 1 characters only.
18397c478bd9Sstevel@tonic-gate 		 */
18407c478bd9Sstevel@tonic-gate 		(void) strncpy(&goodbuf[0], longname, min(split, PRESIZ + 1));
18417c478bd9Sstevel@tonic-gate 		tmpbuf = goodbuf;
18427c478bd9Sstevel@tonic-gate 		lastslash = strrchr(tmpbuf, '/');
18437c478bd9Sstevel@tonic-gate 		if (lastslash == NULL) {
18447c478bd9Sstevel@tonic-gate 			i = split;		/* Length of name */
18457c478bd9Sstevel@tonic-gate 			j = 0;			/* Length of prefix */
18467c478bd9Sstevel@tonic-gate 			goodbuf[0] = '\0';
18477c478bd9Sstevel@tonic-gate 		} else {
18487c478bd9Sstevel@tonic-gate 			*lastslash = '\0';	/* Terminate the prefix */
18497c478bd9Sstevel@tonic-gate 			j = strlen(tmpbuf);
18507c478bd9Sstevel@tonic-gate 			i = split - j - 1;
18517c478bd9Sstevel@tonic-gate 		}
18527c478bd9Sstevel@tonic-gate 		/*
18537c478bd9Sstevel@tonic-gate 		 * If the filename is greater than NAMSIZ we can't
18547c478bd9Sstevel@tonic-gate 		 * archive the file unless we are using extended headers.
18557c478bd9Sstevel@tonic-gate 		 */
18564bc0a2efScasper 		if ((i > NAMSIZ) || (i == NAMSIZ && S_ISDIR(stbuf.st_mode) &&
18577c478bd9Sstevel@tonic-gate 		    !Pflag)) {
18587c478bd9Sstevel@tonic-gate 			/* Determine which (filename or path) is too long. */
18597c478bd9Sstevel@tonic-gate 			lastslash = strrchr(longname, '/');
18607c478bd9Sstevel@tonic-gate 			if (lastslash != NULL)
18617c478bd9Sstevel@tonic-gate 				i = strlen(lastslash + 1);
18627c478bd9Sstevel@tonic-gate 			if (Eflag > 0) {
18637c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_PATH;
18647c478bd9Sstevel@tonic-gate 				Xtarhdr.x_path = longname;
18657c478bd9Sstevel@tonic-gate 				if (i <= NAMSIZ)
18667c478bd9Sstevel@tonic-gate 					(void) strcpy(junkbuf, lastslash + 1);
18677c478bd9Sstevel@tonic-gate 				else
18687c478bd9Sstevel@tonic-gate 					(void) sprintf(junkbuf, "%llu",
18697c478bd9Sstevel@tonic-gate 					    xhdr_count + 1);
18707c478bd9Sstevel@tonic-gate 				if (split - i - 1 > PRESIZ)
18717c478bd9Sstevel@tonic-gate 					(void) strcpy(goodbuf, xhdr_dirname);
18727c478bd9Sstevel@tonic-gate 			} else {
18737c478bd9Sstevel@tonic-gate 				if ((i > NAMSIZ) || (i == NAMSIZ &&
18744bc0a2efScasper 				    S_ISDIR(stbuf.st_mode) && !Pflag))
18757c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
18767c478bd9Sstevel@tonic-gate 					    "tar: %s: filename is greater than "
18777c478bd9Sstevel@tonic-gate 					    "%d\n"), lastslash == NULL ?
18787c478bd9Sstevel@tonic-gate 					    longname : lastslash + 1, NAMSIZ);
18797c478bd9Sstevel@tonic-gate 				else
18807c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
18817c478bd9Sstevel@tonic-gate 					    "tar: %s: prefix is greater than %d"
18827c478bd9Sstevel@tonic-gate 					    "\n"), longname, PRESIZ);
18837c478bd9Sstevel@tonic-gate 				if (errflag)
18847c478bd9Sstevel@tonic-gate 					exitflag = 1;
18857c478bd9Sstevel@tonic-gate 				Errflg = 1;
18867c478bd9Sstevel@tonic-gate 				goto out;
18877c478bd9Sstevel@tonic-gate 			}
18887c478bd9Sstevel@tonic-gate 		} else
18897c478bd9Sstevel@tonic-gate 			(void) strncpy(&junkbuf[0], longname + j + 1,
18907c478bd9Sstevel@tonic-gate 			    strlen(longname + j + 1));
18917c478bd9Sstevel@tonic-gate 		name = junkbuf;
18927c478bd9Sstevel@tonic-gate 		prefix = goodbuf;
18937c478bd9Sstevel@tonic-gate 	} else {
18947c478bd9Sstevel@tonic-gate 		name = longname;
18957c478bd9Sstevel@tonic-gate 	}
18967c478bd9Sstevel@tonic-gate 	if (Aflag) {
18977c478bd9Sstevel@tonic-gate 		if ((prefix != NULL) && (*prefix != '\0'))
18987c478bd9Sstevel@tonic-gate 			while (*prefix == '/')
18997c478bd9Sstevel@tonic-gate 				++prefix;
19007c478bd9Sstevel@tonic-gate 		else
19017c478bd9Sstevel@tonic-gate 			while (*name == '/')
19027c478bd9Sstevel@tonic-gate 				++name;
19037c478bd9Sstevel@tonic-gate 	}
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate 	switch (stbuf.st_mode & S_IFMT) {
19067c478bd9Sstevel@tonic-gate 	case S_IFDIR:
19077c478bd9Sstevel@tonic-gate 		stbuf.st_size = (off_t)0;
19087c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(stbuf.st_size);
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate 		if (filetype != XATTR_FILE && Hiddendir == 0) {
19117c478bd9Sstevel@tonic-gate 			i = 0;
19127c478bd9Sstevel@tonic-gate 			cp = buf;
19137c478bd9Sstevel@tonic-gate 			while ((*cp++ = longname[i++]))
19147c478bd9Sstevel@tonic-gate 				;
19157c478bd9Sstevel@tonic-gate 			*--cp = '/';
19167c478bd9Sstevel@tonic-gate 			*++cp = 0;
19177c478bd9Sstevel@tonic-gate 		}
19187c478bd9Sstevel@tonic-gate 		if (!oflag) {
19197c478bd9Sstevel@tonic-gate 			tomodes(&stbuf);
19207c478bd9Sstevel@tonic-gate 			if (build_dblock(name, tchar, '5', filetype,
19217c478bd9Sstevel@tonic-gate 			    &stbuf, stbuf.st_dev, prefix) != 0) {
19227c478bd9Sstevel@tonic-gate 				goto out;
19237c478bd9Sstevel@tonic-gate 			}
19247c478bd9Sstevel@tonic-gate 			if (!Pflag) {
19257c478bd9Sstevel@tonic-gate 				/*
19267c478bd9Sstevel@tonic-gate 				 * Old archives require a slash at the end
19277c478bd9Sstevel@tonic-gate 				 * of a directory name.
19287c478bd9Sstevel@tonic-gate 				 *
19297c478bd9Sstevel@tonic-gate 				 * XXX
19307c478bd9Sstevel@tonic-gate 				 * If directory name is too long, will
19317c478bd9Sstevel@tonic-gate 				 * slash overfill field?
19327c478bd9Sstevel@tonic-gate 				 */
19337c478bd9Sstevel@tonic-gate 				if (strlen(name) > (unsigned)NAMSIZ-1) {
19347c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
19357c478bd9Sstevel@tonic-gate 					    "tar: %s: filename is greater "
19367c478bd9Sstevel@tonic-gate 					    "than %d\n"), name, NAMSIZ);
19377c478bd9Sstevel@tonic-gate 					if (errflag)
19387c478bd9Sstevel@tonic-gate 						exitflag = 1;
19397c478bd9Sstevel@tonic-gate 					Errflg = 1;
19407c478bd9Sstevel@tonic-gate 					goto out;
19417c478bd9Sstevel@tonic-gate 				} else {
19427c478bd9Sstevel@tonic-gate 					if (strlen(name) == (NAMSIZ - 1)) {
19437c478bd9Sstevel@tonic-gate 						(void) memcpy(dblock.dbuf.name,
19447c478bd9Sstevel@tonic-gate 						    name, NAMSIZ);
19457c478bd9Sstevel@tonic-gate 						dblock.dbuf.name[NAMSIZ-1]
19467c478bd9Sstevel@tonic-gate 						    = '/';
19477c478bd9Sstevel@tonic-gate 					} else
19487c478bd9Sstevel@tonic-gate 						(void) sprintf(dblock.dbuf.name,
19497c478bd9Sstevel@tonic-gate 						    "%s/", name);
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 					/*
19527c478bd9Sstevel@tonic-gate 					 * need to recalculate checksum
19537c478bd9Sstevel@tonic-gate 					 * because the name changed.
19547c478bd9Sstevel@tonic-gate 					 */
19557c478bd9Sstevel@tonic-gate 					(void) sprintf(dblock.dbuf.chksum,
19567c478bd9Sstevel@tonic-gate 					    "%07o", checksum(&dblock));
19577c478bd9Sstevel@tonic-gate 				}
19587c478bd9Sstevel@tonic-gate 			}
19597c478bd9Sstevel@tonic-gate 
1960*da6c28aaSamw 			if (put_extra_attributes(longname, shortname,
1961*da6c28aaSamw 			    longattrname, prefix, filetype, '5') != 0)
19627c478bd9Sstevel@tonic-gate 				goto out;
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
19657c478bd9Sstevel@tonic-gate 			/*
19667c478bd9Sstevel@tonic-gate 			 * Reset header typeflag when archiving directory, since
19677c478bd9Sstevel@tonic-gate 			 * build_dblock changed it on us.
19687c478bd9Sstevel@tonic-gate 			 */
19697c478bd9Sstevel@tonic-gate 			if (filetype == XATTR_FILE) {
19707c478bd9Sstevel@tonic-gate 				dblock.dbuf.typeflag = _XATTR_HDRTYPE;
19717c478bd9Sstevel@tonic-gate 			} else {
19727c478bd9Sstevel@tonic-gate 				dblock.dbuf.typeflag = '5';
19737c478bd9Sstevel@tonic-gate 			}
19747c478bd9Sstevel@tonic-gate #else
19757c478bd9Sstevel@tonic-gate 			dblock.dbuf.typeflag = '5';
19767c478bd9Sstevel@tonic-gate #endif
19777c478bd9Sstevel@tonic-gate 
19787c478bd9Sstevel@tonic-gate 			(void) sprintf(dblock.dbuf.chksum, "%07o",
19797c478bd9Sstevel@tonic-gate 			    checksum(&dblock));
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate 			(void) writetbuf((char *)&dblock, 1);
19827c478bd9Sstevel@tonic-gate 		}
19837c478bd9Sstevel@tonic-gate 		if (vflag) {
19847c478bd9Sstevel@tonic-gate #ifdef DEBUG
19857c478bd9Sstevel@tonic-gate 			if (NotTape)
19867c478bd9Sstevel@tonic-gate 				DEBUG("seek = %" FMT_blkcnt_t "K\t", K(tapepos),
19877c478bd9Sstevel@tonic-gate 				    0);
19887c478bd9Sstevel@tonic-gate #endif
19897c478bd9Sstevel@tonic-gate 			if (filetype == XATTR_FILE && Hiddendir) {
1990*da6c28aaSamw 				(void) fprintf(vfile, "a %s attribute %s ",
1991*da6c28aaSamw 				    longname, longattrname);
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate 			} else {
19947c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "a %s/ ", longname);
19957c478bd9Sstevel@tonic-gate 			}
19967c478bd9Sstevel@tonic-gate 			if (NotTape)
19977c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "%" FMT_blkcnt_t "K\n",
19987c478bd9Sstevel@tonic-gate 				    K(blocks));
19997c478bd9Sstevel@tonic-gate 			else
20007c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("%" FMT_blkcnt_t
20017c478bd9Sstevel@tonic-gate 				    " tape blocks\n"), blocks);
20027c478bd9Sstevel@tonic-gate 		}
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate 		/*
20057c478bd9Sstevel@tonic-gate 		 * If hidden dir then break now since xattrs_put() will do
20067c478bd9Sstevel@tonic-gate 		 * the iterating of the directory.
20077c478bd9Sstevel@tonic-gate 		 *
2008*da6c28aaSamw 		 * At the moment, there can only be system attributes on
2009*da6c28aaSamw 		 * attributes.  There can be no attributes on attributes or
2010*da6c28aaSamw 		 * directories within the attributes hidden directory hierarchy.
20117c478bd9Sstevel@tonic-gate 		 */
20127c478bd9Sstevel@tonic-gate 		if (filetype == XATTR_FILE)
20137c478bd9Sstevel@tonic-gate 			break;
20147c478bd9Sstevel@tonic-gate 
20157c478bd9Sstevel@tonic-gate 		if (*shortname != '/')
20167c478bd9Sstevel@tonic-gate 			(void) sprintf(newparent, "%s/%s", parent, shortname);
20177c478bd9Sstevel@tonic-gate 		else
20187c478bd9Sstevel@tonic-gate 			(void) sprintf(newparent, "%s", shortname);
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate 		if (chdir(shortname) < 0) {
20217c478bd9Sstevel@tonic-gate 			vperror(0, "%s", newparent);
20227c478bd9Sstevel@tonic-gate 			goto out;
20237c478bd9Sstevel@tonic-gate 		}
20247c478bd9Sstevel@tonic-gate 
20257c478bd9Sstevel@tonic-gate 		if ((dirp = opendir(".")) == NULL) {
20267c478bd9Sstevel@tonic-gate 			vperror(0, gettext(
20277c478bd9Sstevel@tonic-gate 			    "can't open directory %s"), longname);
20287c478bd9Sstevel@tonic-gate 			if (chdir(parent) < 0)
20297c478bd9Sstevel@tonic-gate 				vperror(0, gettext("cannot change back?: %s"),
20307c478bd9Sstevel@tonic-gate 				    parent);
20317c478bd9Sstevel@tonic-gate 			goto out;
20327c478bd9Sstevel@tonic-gate 		}
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate 		while ((dp = readdir(dirp)) != NULL && !term) {
20357c478bd9Sstevel@tonic-gate 			if ((strcmp(".", dp->d_name) == 0) ||
20367c478bd9Sstevel@tonic-gate 			    (strcmp("..", dp->d_name) == 0))
20377c478bd9Sstevel@tonic-gate 				continue;
20387c478bd9Sstevel@tonic-gate 			(void) strcpy(cp, dp->d_name);
20397c478bd9Sstevel@tonic-gate 			if (stat(dp->d_name, &sbuf) < 0 ||
20407c478bd9Sstevel@tonic-gate 			    (sbuf.st_mode & S_IFMT) == S_IFDIR) {
20417c478bd9Sstevel@tonic-gate 				l = telldir(dirp);
20427c478bd9Sstevel@tonic-gate 				(void) closedir(dirp);
20437c478bd9Sstevel@tonic-gate 			} else
20447c478bd9Sstevel@tonic-gate 				l = -1;
20457c478bd9Sstevel@tonic-gate 
2046*da6c28aaSamw 			archtype = putfile(buf, cp, newparent, NULL,
20477c478bd9Sstevel@tonic-gate 			    NORMAL_FILE, lev + 1, symlink_lev);
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate 			if (!exitflag) {
2050*da6c28aaSamw 				if ((atflag || saflag) &&
2051*da6c28aaSamw 				    (archtype == PUT_NOTAS_LINK)) {
2052*da6c28aaSamw 					xattrs_put(buf, cp, newparent, NULL);
20537c478bd9Sstevel@tonic-gate 				}
20547c478bd9Sstevel@tonic-gate 			}
20557c478bd9Sstevel@tonic-gate 			if (exitflag)
20567c478bd9Sstevel@tonic-gate 				break;
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 			/*
20597c478bd9Sstevel@tonic-gate 			 * If the directory was not closed, then it does
20607c478bd9Sstevel@tonic-gate 			 * not need to be reopened.
20617c478bd9Sstevel@tonic-gate 			 */
20627c478bd9Sstevel@tonic-gate 			if (l < 0)
20637c478bd9Sstevel@tonic-gate 				continue;
20647c478bd9Sstevel@tonic-gate 			if ((dirp = opendir(".")) == NULL) {
20657c478bd9Sstevel@tonic-gate 				vperror(0, gettext(
20667c478bd9Sstevel@tonic-gate 				    "can't open directory %s"), longname);
20677c478bd9Sstevel@tonic-gate 				if (chdir(parent) < 0)
20687c478bd9Sstevel@tonic-gate 					vperror(0,
20697c478bd9Sstevel@tonic-gate 					    gettext("cannot change back?: %s"),
20707c478bd9Sstevel@tonic-gate 					    parent);
20717c478bd9Sstevel@tonic-gate 				goto out;
20727c478bd9Sstevel@tonic-gate 			}
20737c478bd9Sstevel@tonic-gate 			seekdir(dirp, l);
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 		}
20767c478bd9Sstevel@tonic-gate 		(void) closedir(dirp);
20777c478bd9Sstevel@tonic-gate 
20787c478bd9Sstevel@tonic-gate 		if (chdir(parent) < 0) {
20797c478bd9Sstevel@tonic-gate 			vperror(0, gettext("cannot change back?: %s"), parent);
20807c478bd9Sstevel@tonic-gate 		}
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate 		break;
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 	case S_IFLNK:
20857c478bd9Sstevel@tonic-gate 		readlink_max = NAMSIZ;
20867c478bd9Sstevel@tonic-gate 		if (stbuf.st_size > NAMSIZ) {
20877c478bd9Sstevel@tonic-gate 			if (Eflag > 0) {
20887c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_LINKPATH;
20897c478bd9Sstevel@tonic-gate 				readlink_max = PATH_MAX;
20907c478bd9Sstevel@tonic-gate 			} else {
20917c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
20927c478bd9Sstevel@tonic-gate 				    "tar: %s: symbolic link too long\n"),
20937c478bd9Sstevel@tonic-gate 				    longname);
20947c478bd9Sstevel@tonic-gate 				if (errflag)
20957c478bd9Sstevel@tonic-gate 					exitflag = 1;
20967c478bd9Sstevel@tonic-gate 				Errflg = 1;
20977c478bd9Sstevel@tonic-gate 				goto out;
20987c478bd9Sstevel@tonic-gate 			}
20997c478bd9Sstevel@tonic-gate 		}
21007c478bd9Sstevel@tonic-gate 		/*
21017c478bd9Sstevel@tonic-gate 		 * Sym-links need header size of zero since you
21027c478bd9Sstevel@tonic-gate 		 * don't store any data for this type.
21037c478bd9Sstevel@tonic-gate 		 */
21047c478bd9Sstevel@tonic-gate 		stbuf.st_size = (off_t)0;
21057c478bd9Sstevel@tonic-gate 		tomodes(&stbuf);
21067c478bd9Sstevel@tonic-gate 		i = readlink(shortname, filetmp, readlink_max);
21077c478bd9Sstevel@tonic-gate 		if (i < 0) {
21087c478bd9Sstevel@tonic-gate 			vperror(0, gettext(
21097c478bd9Sstevel@tonic-gate 			    "can't read symbolic link %s"), longname);
21107c478bd9Sstevel@tonic-gate 			goto out;
21117c478bd9Sstevel@tonic-gate 		} else {
21127c478bd9Sstevel@tonic-gate 			filetmp[i] = 0;
21137c478bd9Sstevel@tonic-gate 		}
21147c478bd9Sstevel@tonic-gate 		if (vflag)
21157c478bd9Sstevel@tonic-gate 			(void) fprintf(vfile, gettext(
21167c478bd9Sstevel@tonic-gate 			    "a %s symbolic link to %s\n"),
21177c478bd9Sstevel@tonic-gate 			    longname, filetmp);
21187c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_LINKPATH) {
21197c478bd9Sstevel@tonic-gate 			Xtarhdr.x_linkpath = filetmp;
21207c478bd9Sstevel@tonic-gate 			if (build_dblock(name, tchar, '2', filetype, &stbuf,
21217c478bd9Sstevel@tonic-gate 			    stbuf.st_dev, prefix) != 0)
21227c478bd9Sstevel@tonic-gate 				goto out;
21237c478bd9Sstevel@tonic-gate 		} else
21247c478bd9Sstevel@tonic-gate 			if (build_dblock(name, filetmp, '2', filetype, &stbuf,
21257c478bd9Sstevel@tonic-gate 			    stbuf.st_dev, prefix) != 0)
21267c478bd9Sstevel@tonic-gate 				goto out;
21277c478bd9Sstevel@tonic-gate 		(void) writetbuf((char *)&dblock, 1);
21287c478bd9Sstevel@tonic-gate 		/*
21297c478bd9Sstevel@tonic-gate 		 * No acls for symlinks: mode is always 777
21307c478bd9Sstevel@tonic-gate 		 * dont call write ancillary
21317c478bd9Sstevel@tonic-gate 		 */
21327c478bd9Sstevel@tonic-gate 		rc = PUT_AS_LINK;
21337c478bd9Sstevel@tonic-gate 		break;
21347c478bd9Sstevel@tonic-gate 	case S_IFREG:
21357c478bd9Sstevel@tonic-gate 		if ((infile = openat(dirfd, shortname, 0)) < 0) {
2136*da6c28aaSamw 			vperror(0, "unable to open %s%s%s%s", longname,
2137*da6c28aaSamw 			    rw_sysattr ? gettext(" system") : "",
21387c478bd9Sstevel@tonic-gate 			    (filetype == XATTR_FILE) ?
21397c478bd9Sstevel@tonic-gate 			    gettext(" attribute ") : "",
2140*da6c28aaSamw 			    (filetype == XATTR_FILE) ? (longattrname == NULL) ?
2141*da6c28aaSamw 			    shortname : longattrname : "");
21427c478bd9Sstevel@tonic-gate 			goto out;
21437c478bd9Sstevel@tonic-gate 		}
21447c478bd9Sstevel@tonic-gate 
21457c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(stbuf.st_size);
21467c478bd9Sstevel@tonic-gate 
2147*da6c28aaSamw 		if (put_link(name, longname, shortname, longattrname,
21487c478bd9Sstevel@tonic-gate 		    prefix, filetype, '1') == 0) {
21497c478bd9Sstevel@tonic-gate 			(void) close(infile);
21507c478bd9Sstevel@tonic-gate 			rc = PUT_AS_LINK;
21517c478bd9Sstevel@tonic-gate 			goto out;
21527c478bd9Sstevel@tonic-gate 		}
21537c478bd9Sstevel@tonic-gate 
21547c478bd9Sstevel@tonic-gate 		tomodes(&stbuf);
21557c478bd9Sstevel@tonic-gate 
21567c478bd9Sstevel@tonic-gate 		/* correctly handle end of volume */
21577c478bd9Sstevel@tonic-gate 		while (mulvol && tapepos + blocks + 1 > blocklim) {
21587c478bd9Sstevel@tonic-gate 			/* file won't fit */
21597c478bd9Sstevel@tonic-gate 			if (eflag) {
21607c478bd9Sstevel@tonic-gate 				if (blocks <= blocklim) {
21617c478bd9Sstevel@tonic-gate 					newvol();
21627c478bd9Sstevel@tonic-gate 					break;
21637c478bd9Sstevel@tonic-gate 				}
21647c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
21657c478bd9Sstevel@tonic-gate 				    "tar: Single file cannot fit on volume\n"));
21667c478bd9Sstevel@tonic-gate 				done(3);
21677c478bd9Sstevel@tonic-gate 			}
21687c478bd9Sstevel@tonic-gate 			/* split if floppy has some room and file is large */
21697c478bd9Sstevel@tonic-gate 			if (((blocklim - tapepos) >= EXTMIN) &&
21707c478bd9Sstevel@tonic-gate 			    ((blocks + 1) >= blocklim/10)) {
21717c478bd9Sstevel@tonic-gate 				splitfile(longname, infile,
21727c478bd9Sstevel@tonic-gate 				    name, prefix, filetype);
21737c478bd9Sstevel@tonic-gate 				(void) close(dirfd);
21747c478bd9Sstevel@tonic-gate 				(void) close(infile);
21757c478bd9Sstevel@tonic-gate 				goto out;
21767c478bd9Sstevel@tonic-gate 			}
21777c478bd9Sstevel@tonic-gate 			newvol();	/* not worth it--just get new volume */
21787c478bd9Sstevel@tonic-gate 		}
21797c478bd9Sstevel@tonic-gate #ifdef DEBUG
21807c478bd9Sstevel@tonic-gate 		DEBUG("putfile: %s wants %" FMT_blkcnt_t " blocks\n", longname,
21817c478bd9Sstevel@tonic-gate 		    blocks);
21827c478bd9Sstevel@tonic-gate #endif
21837c478bd9Sstevel@tonic-gate 		if (build_dblock(name, tchar, '0', filetype,
21847c478bd9Sstevel@tonic-gate 		    &stbuf, stbuf.st_dev, prefix) != 0) {
21857c478bd9Sstevel@tonic-gate 			goto out;
21867c478bd9Sstevel@tonic-gate 		}
21877c478bd9Sstevel@tonic-gate 		if (vflag) {
21887c478bd9Sstevel@tonic-gate #ifdef DEBUG
21897c478bd9Sstevel@tonic-gate 			if (NotTape)
21907c478bd9Sstevel@tonic-gate 				DEBUG("seek = %" FMT_blkcnt_t "K\t", K(tapepos),
21917c478bd9Sstevel@tonic-gate 				    0);
21927c478bd9Sstevel@tonic-gate #endif
2193*da6c28aaSamw 			(void) fprintf(vfile, "a %s%s%s%s ", longname,
2194*da6c28aaSamw 			    rw_sysattr ? gettext(" system") : "",
2195*da6c28aaSamw 			    (filetype == XATTR_FILE) ? gettext(
2196*da6c28aaSamw 			    " attribute ") : "",
21977c478bd9Sstevel@tonic-gate 			    (filetype == XATTR_FILE) ?
2198*da6c28aaSamw 			    longattrname : "");
21997c478bd9Sstevel@tonic-gate 			if (NotTape)
22007c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "%" FMT_blkcnt_t "K\n",
22017c478bd9Sstevel@tonic-gate 				    K(blocks));
22027c478bd9Sstevel@tonic-gate 			else
22037c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile,
22047c478bd9Sstevel@tonic-gate 				    gettext("%" FMT_blkcnt_t " tape blocks\n"),
22057c478bd9Sstevel@tonic-gate 				    blocks);
22067c478bd9Sstevel@tonic-gate 		}
22077c478bd9Sstevel@tonic-gate 
2208*da6c28aaSamw 		if (put_extra_attributes(longname, shortname, longattrname,
2209*da6c28aaSamw 		    prefix, filetype, '0') != 0)
22107c478bd9Sstevel@tonic-gate 			goto out;
22117c478bd9Sstevel@tonic-gate 
22127c478bd9Sstevel@tonic-gate 		/*
22137c478bd9Sstevel@tonic-gate 		 * No need to reset typeflag for extended attribute here, since
22147c478bd9Sstevel@tonic-gate 		 * put_extra_attributes already set it and we haven't called
22157c478bd9Sstevel@tonic-gate 		 * build_dblock().
22167c478bd9Sstevel@tonic-gate 		 */
22177c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
22187c478bd9Sstevel@tonic-gate 		hint = writetbuf((char *)&dblock, 1);
2219eace40a5Sceastha 		maxread = max(min(stbuf.st_blksize, stbuf.st_size),
2220eace40a5Sceastha 		    (nblock * TBLOCK));
22217c478bd9Sstevel@tonic-gate 		if ((bigbuf = calloc((unsigned)maxread, sizeof (char))) == 0) {
22227c478bd9Sstevel@tonic-gate 			maxread = TBLOCK;
22237c478bd9Sstevel@tonic-gate 			bigbuf = buf;
22247c478bd9Sstevel@tonic-gate 		}
22257c478bd9Sstevel@tonic-gate 
22267c478bd9Sstevel@tonic-gate 		while (((i = (int)
22277c478bd9Sstevel@tonic-gate 		    read(infile, bigbuf, min((hint*TBLOCK), maxread))) > 0) &&
22287c478bd9Sstevel@tonic-gate 		    blocks) {
22297c478bd9Sstevel@tonic-gate 			blkcnt_t nblks;
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate 			nblks = ((i-1)/TBLOCK)+1;
22327c478bd9Sstevel@tonic-gate 			if (nblks > blocks)
22337c478bd9Sstevel@tonic-gate 				nblks = blocks;
22347c478bd9Sstevel@tonic-gate 			hint = writetbuf(bigbuf, nblks);
22357c478bd9Sstevel@tonic-gate 			blocks -= nblks;
22367c478bd9Sstevel@tonic-gate 		}
22377c478bd9Sstevel@tonic-gate 		(void) close(infile);
22387c478bd9Sstevel@tonic-gate 		if (bigbuf != buf)
22397c478bd9Sstevel@tonic-gate 			free(bigbuf);
22407c478bd9Sstevel@tonic-gate 		if (i < 0)
22417c478bd9Sstevel@tonic-gate 			vperror(0, gettext("Read error on %s"), longname);
22427c478bd9Sstevel@tonic-gate 		else if (blocks != 0 || i != 0) {
22437c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
22447c478bd9Sstevel@tonic-gate 			"tar: %s: file changed size\n"), longname);
22457c478bd9Sstevel@tonic-gate 			if (errflag) {
22467c478bd9Sstevel@tonic-gate 				exitflag = 1;
22477c478bd9Sstevel@tonic-gate 				Errflg = 1;
22487c478bd9Sstevel@tonic-gate 			} else if (!Dflag) {
22497c478bd9Sstevel@tonic-gate 				Errflg = 1;
22507c478bd9Sstevel@tonic-gate 			}
22517c478bd9Sstevel@tonic-gate 		}
22527c478bd9Sstevel@tonic-gate 		putempty(blocks);
22537c478bd9Sstevel@tonic-gate 		break;
22547c478bd9Sstevel@tonic-gate 	case S_IFIFO:
22557c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(stbuf.st_size);
22567c478bd9Sstevel@tonic-gate 		stbuf.st_size = (off_t)0;
22577c478bd9Sstevel@tonic-gate 
2258*da6c28aaSamw 		if (put_link(name, longname, shortname, longattrname,
22597c478bd9Sstevel@tonic-gate 		    prefix, filetype, '6') == 0) {
22607c478bd9Sstevel@tonic-gate 			rc = PUT_AS_LINK;
22617c478bd9Sstevel@tonic-gate 			goto out;
22627c478bd9Sstevel@tonic-gate 		}
22637c478bd9Sstevel@tonic-gate 		tomodes(&stbuf);
22647c478bd9Sstevel@tonic-gate 
22657c478bd9Sstevel@tonic-gate 		while (mulvol && tapepos + blocks + 1 > blocklim) {
22667c478bd9Sstevel@tonic-gate 			if (eflag) {
22677c478bd9Sstevel@tonic-gate 				if (blocks <= blocklim) {
22687c478bd9Sstevel@tonic-gate 					newvol();
22697c478bd9Sstevel@tonic-gate 					break;
22707c478bd9Sstevel@tonic-gate 				}
22717c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
22727c478bd9Sstevel@tonic-gate 				    "tar: Single file cannot fit on volume\n"));
22737c478bd9Sstevel@tonic-gate 				done(3);
22747c478bd9Sstevel@tonic-gate 			}
22757c478bd9Sstevel@tonic-gate 
22767c478bd9Sstevel@tonic-gate 			if (((blocklim - tapepos) >= EXTMIN) &&
22777c478bd9Sstevel@tonic-gate 			    ((blocks + 1) >= blocklim/10)) {
22787c478bd9Sstevel@tonic-gate 				splitfile(longname, infile, name,
22797c478bd9Sstevel@tonic-gate 				    prefix, filetype);
22807c478bd9Sstevel@tonic-gate 				(void) close(dirfd);
22817c478bd9Sstevel@tonic-gate 				(void) close(infile);
22827c478bd9Sstevel@tonic-gate 				goto out;
22837c478bd9Sstevel@tonic-gate 			}
22847c478bd9Sstevel@tonic-gate 			newvol();
22857c478bd9Sstevel@tonic-gate 		}
22867c478bd9Sstevel@tonic-gate #ifdef DEBUG
22877c478bd9Sstevel@tonic-gate 		DEBUG("putfile: %s wants %" FMT_blkcnt_t " blocks\n", longname,
22887c478bd9Sstevel@tonic-gate 		    blocks);
22897c478bd9Sstevel@tonic-gate #endif
22907c478bd9Sstevel@tonic-gate 		if (vflag) {
22917c478bd9Sstevel@tonic-gate #ifdef DEBUG
22927c478bd9Sstevel@tonic-gate 			if (NotTape)
22937c478bd9Sstevel@tonic-gate 				DEBUG("seek = %" FMT_blkcnt_t "K\t", K(tapepos),
22947c478bd9Sstevel@tonic-gate 				    0);
22957c478bd9Sstevel@tonic-gate #endif
22967c478bd9Sstevel@tonic-gate 			if (NotTape)
22977c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("a %s %"
22987c478bd9Sstevel@tonic-gate 				    FMT_blkcnt_t "K\n "), longname, K(blocks));
22997c478bd9Sstevel@tonic-gate 			else
23007c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext(
23017c478bd9Sstevel@tonic-gate 				    "a %s %" FMT_blkcnt_t " tape blocks\n"),
23027c478bd9Sstevel@tonic-gate 				    longname, blocks);
23037c478bd9Sstevel@tonic-gate 		}
23047c478bd9Sstevel@tonic-gate 		if (build_dblock(name, tchar, '6', filetype,
23057c478bd9Sstevel@tonic-gate 		    &stbuf, stbuf.st_dev, prefix) != 0)
23067c478bd9Sstevel@tonic-gate 			goto out;
23077c478bd9Sstevel@tonic-gate 
2308*da6c28aaSamw 		if (put_extra_attributes(longname, shortname, longattrname,
2309*da6c28aaSamw 		    prefix, filetype, '6') != 0)
23107c478bd9Sstevel@tonic-gate 			goto out;
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
23137c478bd9Sstevel@tonic-gate 		dblock.dbuf.typeflag = '6';
23147c478bd9Sstevel@tonic-gate 
23157c478bd9Sstevel@tonic-gate 		(void) writetbuf((char *)&dblock, 1);
23167c478bd9Sstevel@tonic-gate 		break;
23177c478bd9Sstevel@tonic-gate 	case S_IFCHR:
23187c478bd9Sstevel@tonic-gate 		stbuf.st_size = (off_t)0;
23197c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(stbuf.st_size);
2320*da6c28aaSamw 		if (put_link(name, longname, shortname, longattrname,
2321*da6c28aaSamw 		    prefix, filetype, '3') == 0) {
23227c478bd9Sstevel@tonic-gate 			rc = PUT_AS_LINK;
23237c478bd9Sstevel@tonic-gate 			goto out;
23247c478bd9Sstevel@tonic-gate 		}
23257c478bd9Sstevel@tonic-gate 		tomodes(&stbuf);
23267c478bd9Sstevel@tonic-gate 
23277c478bd9Sstevel@tonic-gate 		while (mulvol && tapepos + blocks + 1 > blocklim) {
23287c478bd9Sstevel@tonic-gate 			if (eflag) {
23297c478bd9Sstevel@tonic-gate 				if (blocks <= blocklim) {
23307c478bd9Sstevel@tonic-gate 					newvol();
23317c478bd9Sstevel@tonic-gate 					break;
23327c478bd9Sstevel@tonic-gate 				}
23337c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
23347c478bd9Sstevel@tonic-gate 				    "tar: Single file cannot fit on volume\n"));
23357c478bd9Sstevel@tonic-gate 				done(3);
23367c478bd9Sstevel@tonic-gate 			}
23377c478bd9Sstevel@tonic-gate 
23387c478bd9Sstevel@tonic-gate 			if (((blocklim - tapepos) >= EXTMIN) &&
23397c478bd9Sstevel@tonic-gate 			    ((blocks + 1) >= blocklim/10)) {
23407c478bd9Sstevel@tonic-gate 				splitfile(longname, infile, name,
23417c478bd9Sstevel@tonic-gate 				    prefix, filetype);
23427c478bd9Sstevel@tonic-gate 				(void) close(dirfd);
23437c478bd9Sstevel@tonic-gate 				goto out;
23447c478bd9Sstevel@tonic-gate 			}
23457c478bd9Sstevel@tonic-gate 			newvol();
23467c478bd9Sstevel@tonic-gate 		}
23477c478bd9Sstevel@tonic-gate #ifdef DEBUG
23487c478bd9Sstevel@tonic-gate 		DEBUG("putfile: %s wants %" FMT_blkcnt_t " blocks\n", longname,
23497c478bd9Sstevel@tonic-gate 		    blocks);
23507c478bd9Sstevel@tonic-gate #endif
23517c478bd9Sstevel@tonic-gate 		if (vflag) {
23527c478bd9Sstevel@tonic-gate #ifdef DEBUG
23537c478bd9Sstevel@tonic-gate 			if (NotTape)
23547c478bd9Sstevel@tonic-gate 				DEBUG("seek = %" FMT_blkcnt_t "K\t", K(tapepos),
23557c478bd9Sstevel@tonic-gate 				    0);
23567c478bd9Sstevel@tonic-gate #endif
23577c478bd9Sstevel@tonic-gate 			if (NotTape)
23587c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("a %s %"
23597c478bd9Sstevel@tonic-gate 				    FMT_blkcnt_t "K\n"), longname, K(blocks));
23607c478bd9Sstevel@tonic-gate 			else
23617c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("a %s %"
23627c478bd9Sstevel@tonic-gate 				    FMT_blkcnt_t " tape blocks\n"), longname,
23637c478bd9Sstevel@tonic-gate 				    blocks);
23647c478bd9Sstevel@tonic-gate 		}
23657c478bd9Sstevel@tonic-gate 		if (build_dblock(name, tchar, '3',
23667c478bd9Sstevel@tonic-gate 		    filetype, &stbuf, stbuf.st_rdev, prefix) != 0)
23677c478bd9Sstevel@tonic-gate 			goto out;
23687c478bd9Sstevel@tonic-gate 
2369*da6c28aaSamw 		if (put_extra_attributes(longname, shortname, longattrname,
23707c478bd9Sstevel@tonic-gate 		    prefix, filetype, '3') != 0)
23717c478bd9Sstevel@tonic-gate 			goto out;
23727c478bd9Sstevel@tonic-gate 
23737c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
23747c478bd9Sstevel@tonic-gate 		dblock.dbuf.typeflag = '3';
23757c478bd9Sstevel@tonic-gate 
23767c478bd9Sstevel@tonic-gate 		(void) writetbuf((char *)&dblock, 1);
23777c478bd9Sstevel@tonic-gate 		break;
23787c478bd9Sstevel@tonic-gate 	case S_IFBLK:
23797c478bd9Sstevel@tonic-gate 		stbuf.st_size = (off_t)0;
23807c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(stbuf.st_size);
2381*da6c28aaSamw 		if (put_link(name, longname, shortname, longattrname,
2382*da6c28aaSamw 		    prefix, filetype, '4') == 0) {
23837c478bd9Sstevel@tonic-gate 			rc = PUT_AS_LINK;
23847c478bd9Sstevel@tonic-gate 			goto out;
23857c478bd9Sstevel@tonic-gate 		}
23867c478bd9Sstevel@tonic-gate 		tomodes(&stbuf);
23877c478bd9Sstevel@tonic-gate 
23887c478bd9Sstevel@tonic-gate 		while (mulvol && tapepos + blocks + 1 > blocklim) {
23897c478bd9Sstevel@tonic-gate 			if (eflag) {
23907c478bd9Sstevel@tonic-gate 				if (blocks <= blocklim) {
23917c478bd9Sstevel@tonic-gate 					newvol();
23927c478bd9Sstevel@tonic-gate 					break;
23937c478bd9Sstevel@tonic-gate 				}
23947c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
23957c478bd9Sstevel@tonic-gate 				    "tar: Single file cannot fit on volume\n"));
23967c478bd9Sstevel@tonic-gate 				done(3);
23977c478bd9Sstevel@tonic-gate 			}
23987c478bd9Sstevel@tonic-gate 
23997c478bd9Sstevel@tonic-gate 			if (((blocklim - tapepos) >= EXTMIN) &&
24007c478bd9Sstevel@tonic-gate 			    ((blocks + 1) >= blocklim/10)) {
24017c478bd9Sstevel@tonic-gate 				splitfile(longname, infile,
24027c478bd9Sstevel@tonic-gate 				    name, prefix, filetype);
24037c478bd9Sstevel@tonic-gate 				(void) close(dirfd);
24047c478bd9Sstevel@tonic-gate 				goto out;
24057c478bd9Sstevel@tonic-gate 			}
24067c478bd9Sstevel@tonic-gate 			newvol();
24077c478bd9Sstevel@tonic-gate 		}
24087c478bd9Sstevel@tonic-gate #ifdef DEBUG
24097c478bd9Sstevel@tonic-gate 		DEBUG("putfile: %s wants %" FMT_blkcnt_t " blocks\n", longname,
24107c478bd9Sstevel@tonic-gate 		    blocks);
24117c478bd9Sstevel@tonic-gate #endif
24127c478bd9Sstevel@tonic-gate 		if (vflag) {
24137c478bd9Sstevel@tonic-gate #ifdef DEBUG
24147c478bd9Sstevel@tonic-gate 			if (NotTape)
24157c478bd9Sstevel@tonic-gate 				DEBUG("seek = %" FMT_blkcnt_t "K\t", K(tapepos),
24167c478bd9Sstevel@tonic-gate 				    0);
24177c478bd9Sstevel@tonic-gate #endif
24187c478bd9Sstevel@tonic-gate 			(void) fprintf(vfile, "a %s ", longname);
24197c478bd9Sstevel@tonic-gate 			if (NotTape)
24207c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "%" FMT_blkcnt_t "K\n",
24217c478bd9Sstevel@tonic-gate 				    K(blocks));
24227c478bd9Sstevel@tonic-gate 			else
24237c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("%"
24247c478bd9Sstevel@tonic-gate 				    FMT_blkcnt_t " tape blocks\n"), blocks);
24257c478bd9Sstevel@tonic-gate 		}
24267c478bd9Sstevel@tonic-gate 		if (build_dblock(name, tchar, '4',
24277c478bd9Sstevel@tonic-gate 		    filetype, &stbuf, stbuf.st_rdev, prefix) != 0)
24287c478bd9Sstevel@tonic-gate 			goto out;
24297c478bd9Sstevel@tonic-gate 
2430*da6c28aaSamw 		if (put_extra_attributes(longname, shortname, longattrname,
24317c478bd9Sstevel@tonic-gate 		    prefix, filetype, '4') != 0)
24327c478bd9Sstevel@tonic-gate 			goto out;
24337c478bd9Sstevel@tonic-gate 
24347c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
24357c478bd9Sstevel@tonic-gate 		dblock.dbuf.typeflag = '4';
24367c478bd9Sstevel@tonic-gate 
24377c478bd9Sstevel@tonic-gate 		(void) writetbuf((char *)&dblock, 1);
24387c478bd9Sstevel@tonic-gate 		break;
24397c478bd9Sstevel@tonic-gate 	default:
24407c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
24417c478bd9Sstevel@tonic-gate 		    "tar: %s is not a file. Not dumped\n"), longname);
24427c478bd9Sstevel@tonic-gate 		if (errflag)
24437c478bd9Sstevel@tonic-gate 			exitflag = 1;
24447c478bd9Sstevel@tonic-gate 		Errflg = 1;
24457c478bd9Sstevel@tonic-gate 		goto out;
24467c478bd9Sstevel@tonic-gate 	}
24477c478bd9Sstevel@tonic-gate 
24487c478bd9Sstevel@tonic-gate out:
2449*da6c28aaSamw 	if ((dirfd != -1) && (filetype != XATTR_FILE)) {
24507c478bd9Sstevel@tonic-gate 		(void) close(dirfd);
24517c478bd9Sstevel@tonic-gate 	}
24527c478bd9Sstevel@tonic-gate 	return (rc);
24537c478bd9Sstevel@tonic-gate }
24547c478bd9Sstevel@tonic-gate 
24557c478bd9Sstevel@tonic-gate 
24567c478bd9Sstevel@tonic-gate /*
24577c478bd9Sstevel@tonic-gate  *	splitfile	dump a large file across volumes
24587c478bd9Sstevel@tonic-gate  *
24597c478bd9Sstevel@tonic-gate  *	splitfile(longname, fd);
24607c478bd9Sstevel@tonic-gate  *		char *longname;		full name of file
24617c478bd9Sstevel@tonic-gate  *		int ifd;		input file descriptor
24627c478bd9Sstevel@tonic-gate  *
24637c478bd9Sstevel@tonic-gate  *	NOTE:  only called by putfile() to dump a large file.
24647c478bd9Sstevel@tonic-gate  */
24657c478bd9Sstevel@tonic-gate 
24667c478bd9Sstevel@tonic-gate static void
24677c478bd9Sstevel@tonic-gate splitfile(char *longname, int ifd, char *name, char *prefix, int filetype)
24687c478bd9Sstevel@tonic-gate {
24697c478bd9Sstevel@tonic-gate 	blkcnt_t blocks;
24707c478bd9Sstevel@tonic-gate 	off_t bytes, s;
24717c478bd9Sstevel@tonic-gate 	char buf[TBLOCK];
24727c478bd9Sstevel@tonic-gate 	int i, extents;
24737c478bd9Sstevel@tonic-gate 
24747c478bd9Sstevel@tonic-gate 	blocks = TBLOCKS(stbuf.st_size);	/* blocks file needs */
24757c478bd9Sstevel@tonic-gate 
24767c478bd9Sstevel@tonic-gate 	/*
24777c478bd9Sstevel@tonic-gate 	 * # extents =
24787c478bd9Sstevel@tonic-gate 	 *	size of file after using up rest of this floppy
24797c478bd9Sstevel@tonic-gate 	 *		blocks - (blocklim - tapepos) + 1	(for header)
24807c478bd9Sstevel@tonic-gate 	 *	plus roundup value before divide by blocklim-1
24817c478bd9Sstevel@tonic-gate 	 *		+ (blocklim - 1) - 1
24827c478bd9Sstevel@tonic-gate 	 *	all divided by blocklim-1 (one block for each header).
24837c478bd9Sstevel@tonic-gate 	 * this gives
24847c478bd9Sstevel@tonic-gate 	 *	(blocks - blocklim + tapepos + 1 + blocklim - 2)/(blocklim-1)
24857c478bd9Sstevel@tonic-gate 	 * which reduces to the expression used.
24867c478bd9Sstevel@tonic-gate 	 * one is added to account for this first extent.
24877c478bd9Sstevel@tonic-gate 	 *
24887c478bd9Sstevel@tonic-gate 	 * When one is dealing with extremely large archives, one may want
24897c478bd9Sstevel@tonic-gate 	 * to allow for a large number of extents.  This code should be
24907c478bd9Sstevel@tonic-gate 	 * revisited to determine if extents should be changed to something
24917c478bd9Sstevel@tonic-gate 	 * larger than an int.
24927c478bd9Sstevel@tonic-gate 	 */
24937c478bd9Sstevel@tonic-gate 	extents = (int)((blocks + tapepos - 1ULL)/(blocklim - 1ULL) + 1);
24947c478bd9Sstevel@tonic-gate 
24957c478bd9Sstevel@tonic-gate 	if (extents < 2 || extents > MAXEXT) {	/* let's be reasonable */
24967c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
24977c478bd9Sstevel@tonic-gate 		    "tar: %s needs unusual number of volumes to split\n"
24987c478bd9Sstevel@tonic-gate 		    "tar: %s not dumped\n"), longname, longname);
24997c478bd9Sstevel@tonic-gate 		return;
25007c478bd9Sstevel@tonic-gate 	}
25017c478bd9Sstevel@tonic-gate 	if (build_dblock(name, tchar, '0', filetype,
25027c478bd9Sstevel@tonic-gate 	    &stbuf, stbuf.st_dev, prefix) != 0)
25037c478bd9Sstevel@tonic-gate 		return;
25047c478bd9Sstevel@tonic-gate 
25057c478bd9Sstevel@tonic-gate 	dblock.dbuf.extotal = extents;
25067c478bd9Sstevel@tonic-gate 	bytes = stbuf.st_size;
25077c478bd9Sstevel@tonic-gate 
25087c478bd9Sstevel@tonic-gate 	/*
25097c478bd9Sstevel@tonic-gate 	 * The value contained in dblock.dbuf.efsize was formerly used when the
25107c478bd9Sstevel@tonic-gate 	 * v flag was specified in conjunction with the t flag. Although it is
25117c478bd9Sstevel@tonic-gate 	 * no longer used, older versions of tar will expect the former
25127c478bd9Sstevel@tonic-gate 	 * behaviour, so we must continue to write it to the archive.
25137c478bd9Sstevel@tonic-gate 	 *
25147c478bd9Sstevel@tonic-gate 	 * Since dblock.dbuf.efsize is 10 chars in size, the maximum value it
25157c478bd9Sstevel@tonic-gate 	 * can store is TAR_EFSIZE_MAX. If bytes exceeds that value, simply
25167c478bd9Sstevel@tonic-gate 	 * store 0.
25177c478bd9Sstevel@tonic-gate 	 */
25187c478bd9Sstevel@tonic-gate 	if (bytes <= TAR_EFSIZE_MAX)
25197c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.efsize, "%9" FMT_off_t_o, bytes);
25207c478bd9Sstevel@tonic-gate 	else
25217c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.efsize, "%9" FMT_off_t_o, (off_t)0);
25227c478bd9Sstevel@tonic-gate 
25237c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
25247c478bd9Sstevel@tonic-gate 	    "tar: large file %s needs %d extents.\n"
25257c478bd9Sstevel@tonic-gate 	    "tar: current device seek position = %" FMT_blkcnt_t "K\n"),
25267c478bd9Sstevel@tonic-gate 	    longname, extents, K(tapepos));
25277c478bd9Sstevel@tonic-gate 
25287c478bd9Sstevel@tonic-gate 	s = (off_t)(blocklim - tapepos - 1) * TBLOCK;
25297c478bd9Sstevel@tonic-gate 	for (i = 1; i <= extents; i++) {
25307c478bd9Sstevel@tonic-gate 		if (i > 1) {
25317c478bd9Sstevel@tonic-gate 			newvol();
25327c478bd9Sstevel@tonic-gate 			if (i == extents)
25337c478bd9Sstevel@tonic-gate 				s = bytes;	/* last ext. gets true bytes */
25347c478bd9Sstevel@tonic-gate 			else
25357c478bd9Sstevel@tonic-gate 				s = (off_t)(blocklim - 1)*TBLOCK; /* all */
25367c478bd9Sstevel@tonic-gate 		}
25377c478bd9Sstevel@tonic-gate 		bytes -= s;
25387c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(s);
25397c478bd9Sstevel@tonic-gate 
25407c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.size, "%011" FMT_off_t_o, s);
25417c478bd9Sstevel@tonic-gate 		dblock.dbuf.extno = i;
25427c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
25437c478bd9Sstevel@tonic-gate 		(void) writetbuf((char *)&dblock, 1);
25447c478bd9Sstevel@tonic-gate 
25457c478bd9Sstevel@tonic-gate 		if (vflag)
25467c478bd9Sstevel@tonic-gate 			(void) fprintf(vfile,
25477c478bd9Sstevel@tonic-gate 			    "+++ a %s %" FMT_blkcnt_t "K [extent #%d of %d]\n",
25487c478bd9Sstevel@tonic-gate 			    longname, K(blocks), i, extents);
25497c478bd9Sstevel@tonic-gate 		while (blocks && read(ifd, buf, TBLOCK) > 0) {
25507c478bd9Sstevel@tonic-gate 			blocks--;
25517c478bd9Sstevel@tonic-gate 			(void) writetbuf(buf, 1);
25527c478bd9Sstevel@tonic-gate 		}
25537c478bd9Sstevel@tonic-gate 		if (blocks != 0) {
25547c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
25557c478bd9Sstevel@tonic-gate 			    "tar: %s: file changed size\n"), longname);
25567c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
25577c478bd9Sstevel@tonic-gate 			    "tar: aborting split file %s\n"), longname);
25587c478bd9Sstevel@tonic-gate 			(void) close(ifd);
25597c478bd9Sstevel@tonic-gate 			return;
25607c478bd9Sstevel@tonic-gate 		}
25617c478bd9Sstevel@tonic-gate 	}
25627c478bd9Sstevel@tonic-gate 	(void) close(ifd);
25637c478bd9Sstevel@tonic-gate 	if (vflag)
25647c478bd9Sstevel@tonic-gate 		(void) fprintf(vfile, gettext("a %s %" FMT_off_t "K (in %d "
25657c478bd9Sstevel@tonic-gate 		    "extents)\n"), longname, K(TBLOCKS(stbuf.st_size)),
25667c478bd9Sstevel@tonic-gate 		    extents);
25677c478bd9Sstevel@tonic-gate }
25687c478bd9Sstevel@tonic-gate 
25697c478bd9Sstevel@tonic-gate /*
25707c478bd9Sstevel@tonic-gate  *	convtoreg - determines whether the file should be converted to a
25717c478bd9Sstevel@tonic-gate  *	            regular file when extracted
25727c478bd9Sstevel@tonic-gate  *
25737c478bd9Sstevel@tonic-gate  *	Returns 1 when file size > 0 and typeflag is not recognized
25747c478bd9Sstevel@tonic-gate  * 	Otherwise returns 0
25757c478bd9Sstevel@tonic-gate  */
25767c478bd9Sstevel@tonic-gate static int
25777c478bd9Sstevel@tonic-gate convtoreg(off_t size)
25787c478bd9Sstevel@tonic-gate {
25797c478bd9Sstevel@tonic-gate 	if ((size > 0) && (dblock.dbuf.typeflag != '0') &&
25807c478bd9Sstevel@tonic-gate 	    (dblock.dbuf.typeflag != NULL) && (dblock.dbuf.typeflag != '1') &&
25817c478bd9Sstevel@tonic-gate 	    (dblock.dbuf.typeflag != '2') && (dblock.dbuf.typeflag != '3') &&
25827c478bd9Sstevel@tonic-gate 	    (dblock.dbuf.typeflag != '4') && (dblock.dbuf.typeflag != '5') &&
25837c478bd9Sstevel@tonic-gate 	    (dblock.dbuf.typeflag != '6') && (dblock.dbuf.typeflag != 'A') &&
25847c478bd9Sstevel@tonic-gate 	    (dblock.dbuf.typeflag != _XATTR_HDRTYPE) &&
25857c478bd9Sstevel@tonic-gate 	    (dblock.dbuf.typeflag != 'X')) {
25867c478bd9Sstevel@tonic-gate 		return (1);
25877c478bd9Sstevel@tonic-gate 	}
25887c478bd9Sstevel@tonic-gate 	return (0);
25897c478bd9Sstevel@tonic-gate }
25907c478bd9Sstevel@tonic-gate 
2591*da6c28aaSamw #if defined(O_XATTR)
2592*da6c28aaSamw static int
2593*da6c28aaSamw save_cwd(void)
2594*da6c28aaSamw {
2595*da6c28aaSamw 	return (open(".", O_RDONLY));
2596*da6c28aaSamw }
2597*da6c28aaSamw #endif
2598*da6c28aaSamw 
2599*da6c28aaSamw #if defined(O_XATTR)
2600*da6c28aaSamw static void
2601*da6c28aaSamw rest_cwd(int *cwd)
2602*da6c28aaSamw {
2603*da6c28aaSamw 	if (*cwd != -1) {
2604*da6c28aaSamw 		if (fchdir(*cwd) < 0) {
2605*da6c28aaSamw 			vperror(0, gettext(
2606*da6c28aaSamw 			    "Cannot fchdir to attribute directory"));
2607*da6c28aaSamw 			exit(1);
2608*da6c28aaSamw 		}
2609*da6c28aaSamw 		(void) close(*cwd);
2610*da6c28aaSamw 		*cwd = -1;
2611*da6c28aaSamw 	}
2612*da6c28aaSamw }
2613*da6c28aaSamw #endif
2614*da6c28aaSamw 
2615*da6c28aaSamw /*
2616*da6c28aaSamw  * Verify the underlying file system supports the attribute type.
2617*da6c28aaSamw  * Only archive extended attribute files when '-@' was specified.
2618*da6c28aaSamw  * Only archive system extended attribute files if '-/' was specified.
2619*da6c28aaSamw  */
2620*da6c28aaSamw #if defined(O_XATTR)
2621*da6c28aaSamw static attr_status_t
2622*da6c28aaSamw verify_attr_support(char *filename, arc_action_t actflag)
2623*da6c28aaSamw {
2624*da6c28aaSamw 	/*
2625*da6c28aaSamw 	 */
2626*da6c28aaSamw 	if (atflag) {
2627*da6c28aaSamw 		/* Verify extended attributes are supported */
2628*da6c28aaSamw 		if (pathconf(filename, (actflag == ARC_CREATE) ?
2629*da6c28aaSamw 		    _PC_XATTR_EXISTS : _PC_XATTR_ENABLED) != 1) {
2630*da6c28aaSamw #if defined(_PC_SATTR_ENABLED)
2631*da6c28aaSamw 			if (saflag) {
2632*da6c28aaSamw 				/* Verify system attributes are supported */
2633*da6c28aaSamw 				if (sysattr_support(filename,
2634*da6c28aaSamw 				    (actflag == ARC_CREATE) ? _PC_SATTR_EXISTS :
2635*da6c28aaSamw 				    _PC_SATTR_ENABLED) != 1) {
2636*da6c28aaSamw 					return (ATTR_SATTR_ERR);
2637*da6c28aaSamw 				}
2638*da6c28aaSamw 			} else
2639*da6c28aaSamw 				return (ATTR_XATTR_ERR);
2640*da6c28aaSamw #else
2641*da6c28aaSamw 				return (ATTR_XATTR_ERR);
2642*da6c28aaSamw #endif	/* _PC_SATTR_ENABLED */
2643*da6c28aaSamw 		}
2644*da6c28aaSamw 
2645*da6c28aaSamw #if defined(_PC_SATTR_ENABLED)
2646*da6c28aaSamw 	} else if (saflag) {
2647*da6c28aaSamw 		/* Verify system attributes are supported */
2648*da6c28aaSamw 		if (sysattr_support(filename, (actflag == ARC_CREATE) ?
2649*da6c28aaSamw 		    _PC_SATTR_EXISTS : _PC_SATTR_ENABLED) != 1) {
2650*da6c28aaSamw 			return (ATTR_SATTR_ERR);
2651*da6c28aaSamw 		}
2652*da6c28aaSamw #endif	/* _PC_SATTR_ENABLED */
2653*da6c28aaSamw 	} else {
2654*da6c28aaSamw 		return (ATTR_SKIP);
2655*da6c28aaSamw 	}
2656*da6c28aaSamw 
2657*da6c28aaSamw 	return (ATTR_OK);
2658*da6c28aaSamw }
2659*da6c28aaSamw #endif
2660*da6c28aaSamw 
2661*da6c28aaSamw #if defined(O_XATTR)
2662*da6c28aaSamw /*
2663*da6c28aaSamw  * Recursively open attribute directories until the attribute directory
2664*da6c28aaSamw  * containing the specified attribute, attrname, is opened.
2665*da6c28aaSamw  *
2666*da6c28aaSamw  * Currently, only 2 directory levels of attributes are supported, (i.e.,
2667*da6c28aaSamw  * extended system attributes on extended attributes).  The following are
2668*da6c28aaSamw  * the possible input combinations:
2669*da6c28aaSamw  *	1.  Open the attribute directory of the base file (don't change
2670*da6c28aaSamw  *	    into it).
2671*da6c28aaSamw  *		attrinfo->parent = NULL
2672*da6c28aaSamw  *		attrname = '.'
2673*da6c28aaSamw  *	2.  Open the attribute directory of the base file and change into it.
2674*da6c28aaSamw  *		attrinfo->parent = NULL
2675*da6c28aaSamw  *		attrname = <attr> | <sys_attr>
2676*da6c28aaSamw  *	3.  Open the attribute directory of the base file, change into it,
2677*da6c28aaSamw  *	    then recursively call open_attr_dir() to open the attribute's
2678*da6c28aaSamw  *	    parent directory (don't change into it).
2679*da6c28aaSamw  *		attrinfo->parent = <attr>
2680*da6c28aaSamw  *		attrname = '.'
2681*da6c28aaSamw  *	4.  Open the attribute directory of the base file, change into it,
2682*da6c28aaSamw  *	    then recursively call open_attr_dir() to open the attribute's
2683*da6c28aaSamw  *	    parent directory and change into it.
2684*da6c28aaSamw  *		attrinfo->parent = <attr>
2685*da6c28aaSamw  *		attrname = <attr> | <sys_attr>
2686*da6c28aaSamw  *
2687*da6c28aaSamw  * An attribute directory will be opened only if the underlying file system
2688*da6c28aaSamw  * supports the attribute type, and if the command line specifications (atflag
2689*da6c28aaSamw  * and saflag) enable the processing of the attribute type.
2690*da6c28aaSamw  *
2691*da6c28aaSamw  * On succesful return, attrinfo->parentfd will be the file descriptor of the
2692*da6c28aaSamw  * opened attribute directory.  In addition, if the attribute is a read-write
2693*da6c28aaSamw  * extended system attribute, attrinfo->rw_sysattr will be set to 1, otherwise
2694*da6c28aaSamw  * it will be set to 0.
2695*da6c28aaSamw  *
2696*da6c28aaSamw  * Possible return values:
2697*da6c28aaSamw  * 	ATTR_OK		Successfully opened and, if needed, changed into the
2698*da6c28aaSamw  *			attribute directory containing attrname.
2699*da6c28aaSamw  *	ATTR_SKIP	The command line specifications don't enable the
2700*da6c28aaSamw  *			processing of the attribute type.
2701*da6c28aaSamw  * 	ATTR_CHDIR_ERR	An error occurred while trying to change into an
2702*da6c28aaSamw  *			attribute directory.
2703*da6c28aaSamw  * 	ATTR_OPEN_ERR	An error occurred while trying to open an
2704*da6c28aaSamw  *			attribute directory.
2705*da6c28aaSamw  *	ATTR_XATTR_ERR	The underlying file system doesn't support extended
2706*da6c28aaSamw  *			attributes.
2707*da6c28aaSamw  *	ATTR_SATTR_ERR	The underlying file system doesn't support extended
2708*da6c28aaSamw  *			system attributes.
2709*da6c28aaSamw  */
2710*da6c28aaSamw static int
2711*da6c28aaSamw open_attr_dir(char *attrname, char *dirp, int cwd, attr_data_t *attrinfo)
2712*da6c28aaSamw {
2713*da6c28aaSamw 	attr_status_t	rc;
2714*da6c28aaSamw 	int		firsttime = (attrinfo->attr_parentfd == -1);
2715*da6c28aaSamw 	int		saveerrno;
2716*da6c28aaSamw 
2717*da6c28aaSamw 	/*
2718*da6c28aaSamw 	 * open_attr_dir() was recursively called (input combination number 4),
2719*da6c28aaSamw 	 * close the previously opened file descriptor as we've already changed
2720*da6c28aaSamw 	 * into it.
2721*da6c28aaSamw 	 */
2722*da6c28aaSamw 	if (!firsttime) {
2723*da6c28aaSamw 		(void) close(attrinfo->attr_parentfd);
2724*da6c28aaSamw 		attrinfo->attr_parentfd = -1;
2725*da6c28aaSamw 	}
2726*da6c28aaSamw 
2727*da6c28aaSamw 	/*
2728*da6c28aaSamw 	 * Verify that the underlying file system supports the restoration
2729*da6c28aaSamw 	 * of the attribute.
2730*da6c28aaSamw 	 */
2731*da6c28aaSamw 	if ((rc = verify_attr_support(dirp, ARC_RESTORE)) != ATTR_OK) {
2732*da6c28aaSamw 		return (rc);
2733*da6c28aaSamw 	}
2734*da6c28aaSamw 
2735*da6c28aaSamw 	/* Open the base file's attribute directory */
2736*da6c28aaSamw 	if ((attrinfo->attr_parentfd = attropen(dirp, ".", O_RDONLY)) == -1) {
2737*da6c28aaSamw 		/*
2738*da6c28aaSamw 		 * Save the errno from the attropen so it can be reported
2739*da6c28aaSamw 		 * if the retry of the attropen fails.
2740*da6c28aaSamw 		 */
2741*da6c28aaSamw 		saveerrno = errno;
2742*da6c28aaSamw 		if ((attrinfo->attr_parentfd = retry_open_attr(-1, cwd, dirp,
2743*da6c28aaSamw 		    NULL, ".", O_RDONLY, 0)) == -1) {
2744*da6c28aaSamw 			/*
2745*da6c28aaSamw 			 * Reset typeflag back to real value so passtape
2746*da6c28aaSamw 			 * will skip ahead correctly.
2747*da6c28aaSamw 			 */
2748*da6c28aaSamw 			dblock.dbuf.typeflag = _XATTR_HDRTYPE;
2749*da6c28aaSamw 			(void) close(attrinfo->attr_parentfd);
2750*da6c28aaSamw 			attrinfo->attr_parentfd = -1;
2751*da6c28aaSamw 			errno = saveerrno;
2752*da6c28aaSamw 			return (ATTR_OPEN_ERR);
2753*da6c28aaSamw 		}
2754*da6c28aaSamw 	}
2755*da6c28aaSamw 
2756*da6c28aaSamw 	/*
2757*da6c28aaSamw 	 * Change into the parent attribute's directory unless we are
2758*da6c28aaSamw 	 * processing the hidden attribute directory of the base file itself.
2759*da6c28aaSamw 	 */
2760*da6c28aaSamw 	if ((Hiddendir == 0) || (firsttime && attrinfo->attr_parent != NULL)) {
2761*da6c28aaSamw 		if (fchdir(attrinfo->attr_parentfd) != 0) {
2762*da6c28aaSamw 			saveerrno = errno;
2763*da6c28aaSamw 			(void) close(attrinfo->attr_parentfd);
2764*da6c28aaSamw 			attrinfo->attr_parentfd = -1;
2765*da6c28aaSamw 			errno = saveerrno;
2766*da6c28aaSamw 			return (ATTR_CHDIR_ERR);
2767*da6c28aaSamw 		}
2768*da6c28aaSamw 	}
2769*da6c28aaSamw 
2770*da6c28aaSamw 	/* Determine if the attribute should be processed */
2771*da6c28aaSamw 	if ((rc = verify_attr(attrname, attrinfo->attr_parent, 1,
2772*da6c28aaSamw 	    &attrinfo->attr_rw_sysattr)) != ATTR_OK) {
2773*da6c28aaSamw 		saveerrno = errno;
2774*da6c28aaSamw 		(void) close(attrinfo->attr_parentfd);
2775*da6c28aaSamw 		attrinfo->attr_parentfd = -1;
2776*da6c28aaSamw 		errno = saveerrno;
2777*da6c28aaSamw 		return (rc);
2778*da6c28aaSamw 	}
2779*da6c28aaSamw 
2780*da6c28aaSamw 	/*
2781*da6c28aaSamw 	 * If the attribute is an extended attribute, or extended system
2782*da6c28aaSamw 	 * attribute, of an attribute (i.e., <attr>/<sys_attr>), then
2783*da6c28aaSamw 	 * recursively call open_attr_dir() to open the attribute directory
2784*da6c28aaSamw 	 * of the parent attribute.
2785*da6c28aaSamw 	 */
2786*da6c28aaSamw 	if (firsttime && (attrinfo->attr_parent != NULL)) {
2787*da6c28aaSamw 		return (open_attr_dir(attrname, attrinfo->attr_parent,
2788*da6c28aaSamw 		    attrinfo->attr_parentfd, attrinfo));
2789*da6c28aaSamw 	}
2790*da6c28aaSamw 
2791*da6c28aaSamw 	return (ATTR_OK);
2792*da6c28aaSamw }
2793*da6c28aaSamw #endif
2794*da6c28aaSamw 
27957c478bd9Sstevel@tonic-gate static void
27967c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
27977c478bd9Sstevel@tonic-gate doxtract(char *argv[], int tbl_cnt)
27987c478bd9Sstevel@tonic-gate #else
27997c478bd9Sstevel@tonic-gate doxtract(char *argv[])
28007c478bd9Sstevel@tonic-gate #endif
28017c478bd9Sstevel@tonic-gate {
28027c478bd9Sstevel@tonic-gate 	struct	stat	xtractbuf;	/* stat on file after extracting */
28037c478bd9Sstevel@tonic-gate 	blkcnt_t blocks;
28047c478bd9Sstevel@tonic-gate 	off_t bytes;
28057c478bd9Sstevel@tonic-gate 	int ofile;
28067c478bd9Sstevel@tonic-gate 	int newfile;			/* Does the file already exist  */
28077c478bd9Sstevel@tonic-gate 	int xcnt = 0;			/* count # files extracted */
28087c478bd9Sstevel@tonic-gate 	int fcnt = 0;			/* count # files in argv list */
28097c478bd9Sstevel@tonic-gate 	int dir;
28107c478bd9Sstevel@tonic-gate 	int dirfd = -1;
2811*da6c28aaSamw 	int cwd = -1;
2812*da6c28aaSamw 	int rw_sysattr = 0;
2813*da6c28aaSamw 	int saveerrno;
28147c478bd9Sstevel@tonic-gate 	uid_t Uid;
28157c478bd9Sstevel@tonic-gate 	char *namep, *dirp, *comp, *linkp; /* for removing absolute paths */
28167c478bd9Sstevel@tonic-gate 	char dirname[PATH_MAX+1];
28177c478bd9Sstevel@tonic-gate 	char templink[PATH_MAX+1];	/* temp link with terminating NULL */
28187c478bd9Sstevel@tonic-gate 	int once = 1;
28197c478bd9Sstevel@tonic-gate 	int error;
28207c478bd9Sstevel@tonic-gate 	int symflag;
28217c478bd9Sstevel@tonic-gate 	int want;
2822*da6c28aaSamw 	attr_data_t *attrinfo = NULL;	/* attribute info */
2823fa9e4066Sahrens 	acl_t	*aclp = NULL;	/* acl info */
282445916cd2Sjpk 	char dot[] = ".";		/* dirp for using realpath */
28257c478bd9Sstevel@tonic-gate 	timestruc_t	time_zero;	/* used for call to doDirTimes */
28267c478bd9Sstevel@tonic-gate 	int		dircreate;
28277c478bd9Sstevel@tonic-gate 	int convflag;
28287c478bd9Sstevel@tonic-gate 	time_zero.tv_sec = 0;
28297c478bd9Sstevel@tonic-gate 	time_zero.tv_nsec = 0;
28307c478bd9Sstevel@tonic-gate 
283145916cd2Sjpk 	/* reset Trusted Extensions variables */
283245916cd2Sjpk 	rpath_flag = 0;
283345916cd2Sjpk 	lk_rpath_flag = 0;
283445916cd2Sjpk 	dir_flag = 0;
283545916cd2Sjpk 	mld_flag = 0;
283645916cd2Sjpk 	bslundef(&bs_label);
283745916cd2Sjpk 	bsllow(&admin_low);
283845916cd2Sjpk 	bslhigh(&admin_high);
283945916cd2Sjpk 	orig_namep = 0;
284045916cd2Sjpk 
28417c478bd9Sstevel@tonic-gate 	dumping = 0;	/* for newvol(), et al:  we are not writing */
28427c478bd9Sstevel@tonic-gate 
28437c478bd9Sstevel@tonic-gate 	/*
28447c478bd9Sstevel@tonic-gate 	 * Count the number of files that are to be extracted
28457c478bd9Sstevel@tonic-gate 	 */
28467c478bd9Sstevel@tonic-gate 	Uid = getuid();
28477c478bd9Sstevel@tonic-gate 
28487c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
28497c478bd9Sstevel@tonic-gate 	initarg(argv, Filefile);
28507c478bd9Sstevel@tonic-gate 	while (nextarg() != NULL)
28517c478bd9Sstevel@tonic-gate 		++fcnt;
28527c478bd9Sstevel@tonic-gate 	fcnt += tbl_cnt;
28537c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
28547c478bd9Sstevel@tonic-gate 
28557c478bd9Sstevel@tonic-gate 	for (;;) {
28567c478bd9Sstevel@tonic-gate 		convflag = 0;
28577c478bd9Sstevel@tonic-gate 		symflag = 0;
28587c478bd9Sstevel@tonic-gate 		dir = 0;
28597c478bd9Sstevel@tonic-gate 		ofile = -1;
28607c478bd9Sstevel@tonic-gate 
2861*da6c28aaSamw 		if (dirfd != -1) {
2862*da6c28aaSamw 			(void) close(dirfd);
2863*da6c28aaSamw 			dirfd = -1;
2864*da6c28aaSamw 		}
2865*da6c28aaSamw 		if (ofile != -1) {
2866*da6c28aaSamw 			if (close(ofile) != 0)
2867*da6c28aaSamw 				vperror(2, gettext("close error"));
2868*da6c28aaSamw 		}
2869*da6c28aaSamw 
28707c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
2871*da6c28aaSamw 		if (cwd != -1) {
2872*da6c28aaSamw 			rest_cwd(&cwd);
2873*da6c28aaSamw 		}
2874*da6c28aaSamw #endif
2875*da6c28aaSamw 
2876*da6c28aaSamw 		/* namep is set by wantit to point to the full name */
2877*da6c28aaSamw 		if ((want = wantit(argv, &namep, &dirp, &comp,
2878*da6c28aaSamw 		    &attrinfo)) == 0) {
2879*da6c28aaSamw #if defined(O_XATTR)
2880*da6c28aaSamw 			if (xattrp != NULL) {
28817c478bd9Sstevel@tonic-gate 				free(xattrhead);
28827c478bd9Sstevel@tonic-gate 				xattrp = NULL;
28837c478bd9Sstevel@tonic-gate 				xattr_linkp = NULL;
28847c478bd9Sstevel@tonic-gate 				xattrhead = NULL;
28857c478bd9Sstevel@tonic-gate 			}
28867c478bd9Sstevel@tonic-gate #endif
28877c478bd9Sstevel@tonic-gate 			continue;
28887c478bd9Sstevel@tonic-gate 		}
28897c478bd9Sstevel@tonic-gate 		if (want == -1)
28907c478bd9Sstevel@tonic-gate 			break;
28917c478bd9Sstevel@tonic-gate 
289245916cd2Sjpk /* Trusted Extensions */
289345916cd2Sjpk 		/*
289445916cd2Sjpk 		 * During tar extract (x):
289545916cd2Sjpk 		 * If the pathname of the restored file has been
289645916cd2Sjpk 		 * reconstructed from the ancillary file,
289745916cd2Sjpk 		 * use it to process the normal file.
289845916cd2Sjpk 		 */
289945916cd2Sjpk 		if (mld_flag) {		/* Skip over .MLD. directory */
290045916cd2Sjpk 			mld_flag = 0;
290145916cd2Sjpk 			passtape();
290245916cd2Sjpk 			continue;
290345916cd2Sjpk 		}
290445916cd2Sjpk 		orig_namep = namep;	/* save original */
290545916cd2Sjpk 		if (rpath_flag) {
290645916cd2Sjpk 			namep = real_path;	/* use zone path */
290745916cd2Sjpk 			comp = real_path;	/* use zone path */
290845916cd2Sjpk 			dirp = dot;		/* work from the top */
290945916cd2Sjpk 			rpath_flag = 0;		/* reset */
291045916cd2Sjpk 		}
291145916cd2Sjpk 
29127c478bd9Sstevel@tonic-gate 		if (dirfd != -1)
29137c478bd9Sstevel@tonic-gate 			(void) close(dirfd);
29147c478bd9Sstevel@tonic-gate 
29157c478bd9Sstevel@tonic-gate 		(void) strcpy(&dirname[0], namep);
29167c478bd9Sstevel@tonic-gate 		dircreate = checkdir(&dirname[0]);
29177c478bd9Sstevel@tonic-gate 
29187c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
2919*da6c28aaSamw 		if (xattrp != NULL) {
2920*da6c28aaSamw 			int	rc;
29217c478bd9Sstevel@tonic-gate 
2922*da6c28aaSamw 			if (((cwd = save_cwd()) == -1) ||
2923*da6c28aaSamw 			    ((rc = open_attr_dir(comp, dirp, cwd,
2924*da6c28aaSamw 			    attrinfo)) != ATTR_OK)) {
2925*da6c28aaSamw 				if (cwd == -1) {
2926*da6c28aaSamw 					vperror(0, gettext(
2927*da6c28aaSamw 					    "unable to save current working "
2928*da6c28aaSamw 					    "directory while processing "
2929*da6c28aaSamw 					    "attribute %s of %s"),
2930*da6c28aaSamw 					    dirp, attrinfo->attr_path);
2931*da6c28aaSamw 				} else if (rc != ATTR_SKIP) {
2932d2443e76Smarks 					(void) fprintf(vfile,
2933d2443e76Smarks 					    gettext("tar: cannot open "
2934*da6c28aaSamw 					    "%sattribute %s of file %s: %s\n"),
2935*da6c28aaSamw 					    attrinfo->attr_rw_sysattr ? gettext(
2936*da6c28aaSamw 					    "system ") : "",
2937*da6c28aaSamw 					    comp, dirp, strerror(errno));
2938*da6c28aaSamw 				}
2939d2443e76Smarks 				free(xattrhead);
2940d2443e76Smarks 				xattrp = NULL;
2941d2443e76Smarks 				xattr_linkp = NULL;
2942d2443e76Smarks 				xattrhead = NULL;
2943*da6c28aaSamw 
29447c478bd9Sstevel@tonic-gate 				passtape();
29457c478bd9Sstevel@tonic-gate 				continue;
2946*da6c28aaSamw 			} else {
2947*da6c28aaSamw 				dirfd = attrinfo->attr_parentfd;
2948*da6c28aaSamw 				rw_sysattr = attrinfo->attr_rw_sysattr;
29497c478bd9Sstevel@tonic-gate 			}
2950*da6c28aaSamw 		} else {
2951*da6c28aaSamw 			dirfd = open(dirp, O_RDONLY);
2952*da6c28aaSamw 		}
2953*da6c28aaSamw #else
2954*da6c28aaSamw 		dirfd = open(dirp, O_RDONLY);
2955*da6c28aaSamw #endif
2956*da6c28aaSamw 		if (dirfd == -1) {
2957*da6c28aaSamw 			(void) fprintf(vfile, gettext(
2958*da6c28aaSamw 			    "tar: cannot open %s: %s\n"),
2959*da6c28aaSamw 			    dirp, strerror(errno));
2960*da6c28aaSamw 			passtape();
2961*da6c28aaSamw 			continue;
29627c478bd9Sstevel@tonic-gate 		}
29637c478bd9Sstevel@tonic-gate 
29647c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_LINKPATH)
29657c478bd9Sstevel@tonic-gate 			(void) strcpy(templink, Xtarhdr.x_linkpath);
29667c478bd9Sstevel@tonic-gate 		else {
29677c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
29687c478bd9Sstevel@tonic-gate 			if (xattrp && dblock.dbuf.typeflag == '1') {
29697c478bd9Sstevel@tonic-gate 				(void) sprintf(templink, "%.*s", NAMSIZ,
29707c478bd9Sstevel@tonic-gate 				    xattrp->h_names);
29717c478bd9Sstevel@tonic-gate 			} else {
29727c478bd9Sstevel@tonic-gate 				(void) sprintf(templink, "%.*s", NAMSIZ,
29737c478bd9Sstevel@tonic-gate 				    dblock.dbuf.linkname);
29747c478bd9Sstevel@tonic-gate 			}
29757c478bd9Sstevel@tonic-gate #else
29767c478bd9Sstevel@tonic-gate 			(void) sprintf(templink, "%.*s", NAMSIZ,
29777c478bd9Sstevel@tonic-gate 			    dblock.dbuf.linkname);
29787c478bd9Sstevel@tonic-gate #endif
29797c478bd9Sstevel@tonic-gate 		}
29807c478bd9Sstevel@tonic-gate 
29817c478bd9Sstevel@tonic-gate 		if (Fflag) {
29827c478bd9Sstevel@tonic-gate 			char *s;
29837c478bd9Sstevel@tonic-gate 
29847c478bd9Sstevel@tonic-gate 			if ((s = strrchr(namep, '/')) == 0)
29857c478bd9Sstevel@tonic-gate 				s = namep;
29867c478bd9Sstevel@tonic-gate 
29877c478bd9Sstevel@tonic-gate 			else
29887c478bd9Sstevel@tonic-gate 				s++;
29897c478bd9Sstevel@tonic-gate 			if (checkf(s, stbuf.st_mode, Fflag) == 0) {
29907c478bd9Sstevel@tonic-gate 				passtape();
29917c478bd9Sstevel@tonic-gate 				continue;
29927c478bd9Sstevel@tonic-gate 			}
29937c478bd9Sstevel@tonic-gate 		}
29947c478bd9Sstevel@tonic-gate 
29957c478bd9Sstevel@tonic-gate 		if (checkw('x', namep) == 0) {
29967c478bd9Sstevel@tonic-gate 			passtape();
29977c478bd9Sstevel@tonic-gate 			continue;
29987c478bd9Sstevel@tonic-gate 		}
29997c478bd9Sstevel@tonic-gate 		if (once) {
30007c478bd9Sstevel@tonic-gate 			if (strcmp(dblock.dbuf.magic, magic_type) == 0) {
30017c478bd9Sstevel@tonic-gate 				if (geteuid() == (uid_t)0) {
30027c478bd9Sstevel@tonic-gate 					checkflag = 1;
30037c478bd9Sstevel@tonic-gate 					pflag = 1;
30047c478bd9Sstevel@tonic-gate 				} else {
30057c478bd9Sstevel@tonic-gate 					/* get file creation mask */
30067c478bd9Sstevel@tonic-gate 					Oumask = umask(0);
30077c478bd9Sstevel@tonic-gate 					(void) umask(Oumask);
30087c478bd9Sstevel@tonic-gate 				}
30097c478bd9Sstevel@tonic-gate 				once = 0;
30107c478bd9Sstevel@tonic-gate 			} else {
30117c478bd9Sstevel@tonic-gate 				if (geteuid() == (uid_t)0) {
30127c478bd9Sstevel@tonic-gate 					pflag = 1;
30137c478bd9Sstevel@tonic-gate 					checkflag = 2;
30147c478bd9Sstevel@tonic-gate 				}
30157c478bd9Sstevel@tonic-gate 				if (!pflag) {
30167c478bd9Sstevel@tonic-gate 					/* get file creation mask */
30177c478bd9Sstevel@tonic-gate 					Oumask = umask(0);
30187c478bd9Sstevel@tonic-gate 					(void) umask(Oumask);
30197c478bd9Sstevel@tonic-gate 				}
30207c478bd9Sstevel@tonic-gate 				once = 0;
30217c478bd9Sstevel@tonic-gate 			}
30227c478bd9Sstevel@tonic-gate 		}
30237c478bd9Sstevel@tonic-gate 
30247c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
30257c478bd9Sstevel@tonic-gate 		/*
30267c478bd9Sstevel@tonic-gate 		 * Handle extraction of hidden attr dir.
30277c478bd9Sstevel@tonic-gate 		 * Dir is automatically created, we only
30287c478bd9Sstevel@tonic-gate 		 * need to update mode and perm's.
30297c478bd9Sstevel@tonic-gate 		 */
3030*da6c28aaSamw 		if ((xattrp != NULL) && Hiddendir == 1) {
3031*da6c28aaSamw 			bytes = stbuf.st_size;
3032*da6c28aaSamw 			blocks = TBLOCKS(bytes);
3033*da6c28aaSamw 			if (vflag) {
3034*da6c28aaSamw 				(void) fprintf(vfile,
3035*da6c28aaSamw 				    "x %s%s%s, %" FMT_off_t " bytes, ", namep,
3036*da6c28aaSamw 				    gettext(" attribute "),
3037*da6c28aaSamw 				    xattrapath, bytes);
3038*da6c28aaSamw 				if (NotTape)
3039*da6c28aaSamw 					(void) fprintf(vfile,
3040*da6c28aaSamw 					    "%" FMT_blkcnt_t "K\n", K(blocks));
3041*da6c28aaSamw 				else
3042*da6c28aaSamw 					(void) fprintf(vfile, gettext("%"
3043*da6c28aaSamw 					    FMT_blkcnt_t " tape blocks\n"),
3044*da6c28aaSamw 					    blocks);
3045*da6c28aaSamw 			}
3046*da6c28aaSamw 
3047*da6c28aaSamw 			/*
3048*da6c28aaSamw 			 * Set the permissions and mode of the attribute
3049*da6c28aaSamw 			 * unless the attribute is a system attribute (can't
3050*da6c28aaSamw 			 * successfully do this) or the hidden attribute
3051*da6c28aaSamw 			 * directory (".") of an attribute (when the attribute
3052*da6c28aaSamw 			 * is restored, the hidden attribute directory of an
3053*da6c28aaSamw 			 * attribute is transient).  Note:  when the permissions
3054*da6c28aaSamw 			 * and mode are set for the hidden attribute directory
3055*da6c28aaSamw 			 * of a file on a system supporting extended system
3056*da6c28aaSamw 			 * attributes, even though it returns successfully, it
3057*da6c28aaSamw 			 * will not have any affect since the attribute
3058*da6c28aaSamw 			 * directory is transient.
3059*da6c28aaSamw 			 */
3060*da6c28aaSamw 			if (attrinfo->attr_parent == NULL) {
30617c478bd9Sstevel@tonic-gate 				if (fchownat(dirfd, ".", stbuf.st_uid,
30627c478bd9Sstevel@tonic-gate 				    stbuf.st_gid, 0) != 0) {
30637c478bd9Sstevel@tonic-gate 					vperror(0, gettext(
3064*da6c28aaSamw 					    "%s%s%s: failed to set ownership "
3065*da6c28aaSamw 					    "of attribute directory"), namep,
3066*da6c28aaSamw 					    gettext(" attribute "), xattrapath);
30677c478bd9Sstevel@tonic-gate 				}
30687c478bd9Sstevel@tonic-gate 
30697c478bd9Sstevel@tonic-gate 				if (fchmod(dirfd, stbuf.st_mode) != 0) {
30707c478bd9Sstevel@tonic-gate 					vperror(0, gettext(
3071*da6c28aaSamw 					    "%s%s%s: failed to set permissions "
3072*da6c28aaSamw 					    "of attribute directory"), namep,
3073*da6c28aaSamw 					    gettext(" attribute "), xattrapath);
3074*da6c28aaSamw 				}
30757c478bd9Sstevel@tonic-gate 			}
30767c478bd9Sstevel@tonic-gate 			goto filedone;
30777c478bd9Sstevel@tonic-gate 		}
30787c478bd9Sstevel@tonic-gate #endif
30797c478bd9Sstevel@tonic-gate 
30807c478bd9Sstevel@tonic-gate 		if (dircreate && (!is_posix || dblock.dbuf.typeflag == '5')) {
30817c478bd9Sstevel@tonic-gate 			dir = 1;
30827c478bd9Sstevel@tonic-gate 			if (vflag) {
30837c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "x %s, 0 bytes, ",
30847c478bd9Sstevel@tonic-gate 				    &dirname[0]);
30857c478bd9Sstevel@tonic-gate 				if (NotTape)
30867c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, "0K\n");
30877c478bd9Sstevel@tonic-gate 				else
30887c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext("%"
30897c478bd9Sstevel@tonic-gate 					    FMT_blkcnt_t " tape blocks\n"),
30907c478bd9Sstevel@tonic-gate 					    (blkcnt_t)0);
30917c478bd9Sstevel@tonic-gate 			}
30927c478bd9Sstevel@tonic-gate 			goto filedone;
30937c478bd9Sstevel@tonic-gate 		}
30947c478bd9Sstevel@tonic-gate 
30957c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '6') {	/* FIFO */
30967c478bd9Sstevel@tonic-gate 			if (rmdir(namep) < 0) {
30977c478bd9Sstevel@tonic-gate 				if (errno == ENOTDIR)
30987c478bd9Sstevel@tonic-gate 					(void) unlink(namep);
30997c478bd9Sstevel@tonic-gate 			}
31007c478bd9Sstevel@tonic-gate 			linkp = templink;
31017c478bd9Sstevel@tonic-gate 			if (*linkp !=  NULL) {
31027c478bd9Sstevel@tonic-gate 				if (Aflag && *linkp == '/')
31037c478bd9Sstevel@tonic-gate 					linkp++;
31047c478bd9Sstevel@tonic-gate 				if (link(linkp, namep) < 0) {
31057c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
31067c478bd9Sstevel@tonic-gate 					    "tar: %s: cannot link\n"), namep);
31077c478bd9Sstevel@tonic-gate 					continue;
31087c478bd9Sstevel@tonic-gate 				}
31097c478bd9Sstevel@tonic-gate 				if (vflag)
31107c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext(
3111*da6c28aaSamw 					    "x %s linked to %s\n"), namep,
3112*da6c28aaSamw 					    linkp);
31137c478bd9Sstevel@tonic-gate 				xcnt++;	 /* increment # files extracted */
31147c478bd9Sstevel@tonic-gate 				continue;
31157c478bd9Sstevel@tonic-gate 			}
31167c478bd9Sstevel@tonic-gate 			if (mknod(namep, (int)(Gen.g_mode|S_IFIFO),
31177c478bd9Sstevel@tonic-gate 			    (int)Gen.g_devmajor) < 0) {
31187c478bd9Sstevel@tonic-gate 				vperror(0, gettext("%s: mknod failed"), namep);
31197c478bd9Sstevel@tonic-gate 				continue;
31207c478bd9Sstevel@tonic-gate 			}
31217c478bd9Sstevel@tonic-gate 			bytes = stbuf.st_size;
31227c478bd9Sstevel@tonic-gate 			blocks = TBLOCKS(bytes);
31237c478bd9Sstevel@tonic-gate 			if (vflag) {
31247c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "x %s, %" FMT_off_t
31257c478bd9Sstevel@tonic-gate 				    " bytes, ", namep, bytes);
31267c478bd9Sstevel@tonic-gate 				if (NotTape)
31277c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, "%" FMT_blkcnt_t
31287c478bd9Sstevel@tonic-gate 					    "K\n", K(blocks));
31297c478bd9Sstevel@tonic-gate 				else
31307c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext("%"
31317c478bd9Sstevel@tonic-gate 					    FMT_blkcnt_t " tape blocks\n"),
31327c478bd9Sstevel@tonic-gate 					    blocks);
31337c478bd9Sstevel@tonic-gate 			}
31347c478bd9Sstevel@tonic-gate 			goto filedone;
31357c478bd9Sstevel@tonic-gate 		}
31367c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '3' && !Uid) { /* CHAR SPECIAL */
31377c478bd9Sstevel@tonic-gate 			if (rmdir(namep) < 0) {
31387c478bd9Sstevel@tonic-gate 				if (errno == ENOTDIR)
31397c478bd9Sstevel@tonic-gate 					(void) unlink(namep);
31407c478bd9Sstevel@tonic-gate 			}
31417c478bd9Sstevel@tonic-gate 			linkp = templink;
31427c478bd9Sstevel@tonic-gate 			if (*linkp != NULL) {
31437c478bd9Sstevel@tonic-gate 				if (Aflag && *linkp == '/')
31447c478bd9Sstevel@tonic-gate 					linkp++;
31457c478bd9Sstevel@tonic-gate 				if (link(linkp, namep) < 0) {
31467c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
31477c478bd9Sstevel@tonic-gate 					    "tar: %s: cannot link\n"), namep);
31487c478bd9Sstevel@tonic-gate 					continue;
31497c478bd9Sstevel@tonic-gate 				}
31507c478bd9Sstevel@tonic-gate 				if (vflag)
31517c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext(
3152*da6c28aaSamw 					    "x %s linked to %s\n"), namep,
3153*da6c28aaSamw 					    linkp);
31547c478bd9Sstevel@tonic-gate 				xcnt++;	 /* increment # files extracted */
31557c478bd9Sstevel@tonic-gate 				continue;
31567c478bd9Sstevel@tonic-gate 			}
31577c478bd9Sstevel@tonic-gate 			if (mknod(namep, (int)(Gen.g_mode|S_IFCHR),
31587c478bd9Sstevel@tonic-gate 			    (int)makedev(Gen.g_devmajor, Gen.g_devminor)) < 0) {
31597c478bd9Sstevel@tonic-gate 				vperror(0, gettext(
31607c478bd9Sstevel@tonic-gate 				    "%s: mknod failed"), namep);
31617c478bd9Sstevel@tonic-gate 				continue;
31627c478bd9Sstevel@tonic-gate 			}
31637c478bd9Sstevel@tonic-gate 			bytes = stbuf.st_size;
31647c478bd9Sstevel@tonic-gate 			blocks = TBLOCKS(bytes);
31657c478bd9Sstevel@tonic-gate 			if (vflag) {
31667c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "x %s, %" FMT_off_t
31677c478bd9Sstevel@tonic-gate 				    " bytes, ", namep, bytes);
31687c478bd9Sstevel@tonic-gate 				if (NotTape)
31697c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, "%" FMT_blkcnt_t
31707c478bd9Sstevel@tonic-gate 					    "K\n", K(blocks));
31717c478bd9Sstevel@tonic-gate 				else
31727c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext("%"
31737c478bd9Sstevel@tonic-gate 					    FMT_blkcnt_t " tape blocks\n"),
31747c478bd9Sstevel@tonic-gate 					    blocks);
31757c478bd9Sstevel@tonic-gate 			}
31767c478bd9Sstevel@tonic-gate 			goto filedone;
31777c478bd9Sstevel@tonic-gate 		} else if (dblock.dbuf.typeflag == '3' && Uid) {
31787c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
31797c478bd9Sstevel@tonic-gate 			    "Can't create special %s\n"), namep);
31807c478bd9Sstevel@tonic-gate 			continue;
31817c478bd9Sstevel@tonic-gate 		}
31827c478bd9Sstevel@tonic-gate 
31837c478bd9Sstevel@tonic-gate 		/* BLOCK SPECIAL */
31847c478bd9Sstevel@tonic-gate 
31857c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '4' && !Uid) {
31867c478bd9Sstevel@tonic-gate 			if (rmdir(namep) < 0) {
31877c478bd9Sstevel@tonic-gate 				if (errno == ENOTDIR)
31887c478bd9Sstevel@tonic-gate 					(void) unlink(namep);
31897c478bd9Sstevel@tonic-gate 			}
31907c478bd9Sstevel@tonic-gate 			linkp = templink;
31917c478bd9Sstevel@tonic-gate 			if (*linkp != NULL) {
31927c478bd9Sstevel@tonic-gate 				if (Aflag && *linkp == '/')
31937c478bd9Sstevel@tonic-gate 					linkp++;
31947c478bd9Sstevel@tonic-gate 				if (link(linkp, namep) < 0) {
31957c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
31967c478bd9Sstevel@tonic-gate 					    "tar: %s: cannot link\n"), namep);
31977c478bd9Sstevel@tonic-gate 					continue;
31987c478bd9Sstevel@tonic-gate 				}
31997c478bd9Sstevel@tonic-gate 				if (vflag)
32007c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext(
3201*da6c28aaSamw 					    "x %s linked to %s\n"), namep,
3202*da6c28aaSamw 					    linkp);
32037c478bd9Sstevel@tonic-gate 				xcnt++;	 /* increment # files extracted */
32047c478bd9Sstevel@tonic-gate 				continue;
32057c478bd9Sstevel@tonic-gate 			}
32067c478bd9Sstevel@tonic-gate 			if (mknod(namep, (int)(Gen.g_mode|S_IFBLK),
32077c478bd9Sstevel@tonic-gate 			    (int)makedev(Gen.g_devmajor, Gen.g_devminor)) < 0) {
32087c478bd9Sstevel@tonic-gate 				vperror(0, gettext("%s: mknod failed"), namep);
32097c478bd9Sstevel@tonic-gate 				continue;
32107c478bd9Sstevel@tonic-gate 			}
32117c478bd9Sstevel@tonic-gate 			bytes = stbuf.st_size;
32127c478bd9Sstevel@tonic-gate 			blocks = TBLOCKS(bytes);
32137c478bd9Sstevel@tonic-gate 			if (vflag) {
32147c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("x %s, %"
32157c478bd9Sstevel@tonic-gate 				    FMT_off_t " bytes, "), namep, bytes);
32167c478bd9Sstevel@tonic-gate 				if (NotTape)
32177c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, "%" FMT_blkcnt_t
32187c478bd9Sstevel@tonic-gate 					    "K\n", K(blocks));
32197c478bd9Sstevel@tonic-gate 				else
32207c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext("%"
32217c478bd9Sstevel@tonic-gate 					    FMT_blkcnt_t " tape blocks\n"),
32227c478bd9Sstevel@tonic-gate 					    blocks);
32237c478bd9Sstevel@tonic-gate 			}
32247c478bd9Sstevel@tonic-gate 			goto filedone;
32257c478bd9Sstevel@tonic-gate 		} else if (dblock.dbuf.typeflag == '4' && Uid) {
32267c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
32277c478bd9Sstevel@tonic-gate 			    gettext("Can't create special %s\n"), namep);
32287c478bd9Sstevel@tonic-gate 			continue;
32297c478bd9Sstevel@tonic-gate 		}
32307c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '2') {	/* symlink */
323145916cd2Sjpk 			if ((Tflag) && (lk_rpath_flag == 1))
323245916cd2Sjpk 				linkp = lk_real_path;
323345916cd2Sjpk 			else
32347c478bd9Sstevel@tonic-gate 				linkp = templink;
32357c478bd9Sstevel@tonic-gate 			if (Aflag && *linkp == '/')
32367c478bd9Sstevel@tonic-gate 				linkp++;
32377c478bd9Sstevel@tonic-gate 			if (rmdir(namep) < 0) {
32387c478bd9Sstevel@tonic-gate 				if (errno == ENOTDIR)
32397c478bd9Sstevel@tonic-gate 					(void) unlink(namep);
32407c478bd9Sstevel@tonic-gate 			}
32417c478bd9Sstevel@tonic-gate 			if (symlink(linkp, namep) < 0) {
32427c478bd9Sstevel@tonic-gate 				vperror(0, gettext("%s: symbolic link failed"),
32437c478bd9Sstevel@tonic-gate 				    namep);
32447c478bd9Sstevel@tonic-gate 				continue;
32457c478bd9Sstevel@tonic-gate 			}
32467c478bd9Sstevel@tonic-gate 			if (vflag)
32477c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext(
32487c478bd9Sstevel@tonic-gate 				    "x %s symbolic link to %s\n"),
32497c478bd9Sstevel@tonic-gate 				    namep, linkp);
32507c478bd9Sstevel@tonic-gate 
32517c478bd9Sstevel@tonic-gate 			symflag = AT_SYMLINK_NOFOLLOW;
32527c478bd9Sstevel@tonic-gate 			goto filedone;
32537c478bd9Sstevel@tonic-gate 		}
32547c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '1') {
32557c478bd9Sstevel@tonic-gate 			linkp = templink;
32567c478bd9Sstevel@tonic-gate 			if (Aflag && *linkp == '/')
32577c478bd9Sstevel@tonic-gate 				linkp++;
32587c478bd9Sstevel@tonic-gate 			if (unlinkat(dirfd, comp, AT_REMOVEDIR) < 0) {
32597c478bd9Sstevel@tonic-gate 				if (errno == ENOTDIR)
32607c478bd9Sstevel@tonic-gate 					(void) unlinkat(dirfd, comp, 0);
32617c478bd9Sstevel@tonic-gate 			}
32627c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
32637c478bd9Sstevel@tonic-gate 			if (xattrp && xattr_linkp) {
32647c478bd9Sstevel@tonic-gate 				if (fchdir(dirfd) < 0) {
32657c478bd9Sstevel@tonic-gate 					vperror(0, gettext(
32667c478bd9Sstevel@tonic-gate 					    "Cannot fchdir to attribute "
3267*da6c28aaSamw 					    "directory %s"),
3268*da6c28aaSamw 					    (attrinfo->attr_parent == NULL) ?
3269*da6c28aaSamw 					    dirp : attrinfo->attr_parent);
32707c478bd9Sstevel@tonic-gate 					exit(1);
32717c478bd9Sstevel@tonic-gate 				}
32727c478bd9Sstevel@tonic-gate 
3273*da6c28aaSamw 				error = link(xattr_linkaname, xattrapath);
32747c478bd9Sstevel@tonic-gate 			} else {
32757c478bd9Sstevel@tonic-gate 				error = link(linkp, namep);
32767c478bd9Sstevel@tonic-gate 			}
32777c478bd9Sstevel@tonic-gate #else
32787c478bd9Sstevel@tonic-gate 			error = link(linkp, namep);
32797c478bd9Sstevel@tonic-gate #endif
32807c478bd9Sstevel@tonic-gate 
32817c478bd9Sstevel@tonic-gate 			if (error < 0) {
32827c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
32837c478bd9Sstevel@tonic-gate 				    "tar: %s%s%s: cannot link\n"),
32847c478bd9Sstevel@tonic-gate 				    namep, (xattr_linkp != NULL) ?
32857c478bd9Sstevel@tonic-gate 				    gettext(" attribute ") : "",
32867c478bd9Sstevel@tonic-gate 				    (xattr_linkp != NULL) ?
3287*da6c28aaSamw 				    xattrapath : "");
32887c478bd9Sstevel@tonic-gate 				continue;
32897c478bd9Sstevel@tonic-gate 			}
32907c478bd9Sstevel@tonic-gate 			if (vflag)
32917c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext(
3292*da6c28aaSamw 				    "x %s%s%s linked to %s%s%s\n"), namep,
32937c478bd9Sstevel@tonic-gate 				    (xattr_linkp != NULL) ?
32947c478bd9Sstevel@tonic-gate 				    gettext(" attribute ") : "",
32957c478bd9Sstevel@tonic-gate 				    (xattr_linkp != NULL) ?
32967c478bd9Sstevel@tonic-gate 				    xattr_linkaname : "",
3297*da6c28aaSamw 				    linkp,
32987c478bd9Sstevel@tonic-gate 				    (xattr_linkp != NULL) ?
3299*da6c28aaSamw 				    gettext(" attribute ") : "",
3300*da6c28aaSamw 				    (xattr_linkp != NULL) ? xattrapath : "");
33017c478bd9Sstevel@tonic-gate 			xcnt++;		/* increment # files extracted */
33027c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
3303*da6c28aaSamw 			if (xattrp != NULL) {
33047c478bd9Sstevel@tonic-gate 				free(xattrhead);
33057c478bd9Sstevel@tonic-gate 				xattrp = NULL;
33067c478bd9Sstevel@tonic-gate 				xattr_linkp = NULL;
33077c478bd9Sstevel@tonic-gate 				xattrhead = NULL;
33087c478bd9Sstevel@tonic-gate 			}
33097c478bd9Sstevel@tonic-gate #endif
33107c478bd9Sstevel@tonic-gate 			continue;
33117c478bd9Sstevel@tonic-gate 		}
33127c478bd9Sstevel@tonic-gate 
33137c478bd9Sstevel@tonic-gate 		/* REGULAR FILES */
33147c478bd9Sstevel@tonic-gate 
33157c478bd9Sstevel@tonic-gate 		if (convtoreg(stbuf.st_size)) {
33167c478bd9Sstevel@tonic-gate 			convflag = 1;
33177c478bd9Sstevel@tonic-gate 			if (errflag) {
33187c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
33197c478bd9Sstevel@tonic-gate 				    "tar: %s: typeflag '%c' not recognized\n"),
33207c478bd9Sstevel@tonic-gate 				    namep, dblock.dbuf.typeflag);
33217c478bd9Sstevel@tonic-gate 				done(1);
33227c478bd9Sstevel@tonic-gate 			} else {
33237c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
33247c478bd9Sstevel@tonic-gate 				    "tar: %s: typeflag '%c' not recognized, "
33257c478bd9Sstevel@tonic-gate 				    "converting to regular file\n"), namep,
33267c478bd9Sstevel@tonic-gate 				    dblock.dbuf.typeflag);
33277c478bd9Sstevel@tonic-gate 				Errflg = 1;
33287c478bd9Sstevel@tonic-gate 			}
33297c478bd9Sstevel@tonic-gate 		}
33307c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '0' ||
33317c478bd9Sstevel@tonic-gate 		    dblock.dbuf.typeflag == NULL || convflag) {
33322c0f0499Slovely 			delete_target(dirfd, comp, namep);
33337c478bd9Sstevel@tonic-gate 			linkp = templink;
33347c478bd9Sstevel@tonic-gate 			if (*linkp != NULL) {
33357c478bd9Sstevel@tonic-gate 				if (Aflag && *linkp == '/')
33367c478bd9Sstevel@tonic-gate 					linkp++;
33377c478bd9Sstevel@tonic-gate 				if (link(linkp, comp) < 0) {
33387c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
33397c478bd9Sstevel@tonic-gate 					    "tar: %s: cannot link\n"), namep);
33407c478bd9Sstevel@tonic-gate 					continue;
33417c478bd9Sstevel@tonic-gate 				}
33427c478bd9Sstevel@tonic-gate 				if (vflag)
33437c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext(
3344*da6c28aaSamw 					    "x %s linked to %s\n"), comp,
3345*da6c28aaSamw 					    linkp);
33467c478bd9Sstevel@tonic-gate 				xcnt++;	 /* increment # files extracted */
3347*da6c28aaSamw #if defined(O_XATTR)
3348*da6c28aaSamw 				if (xattrp != NULL) {
3349*da6c28aaSamw 					free(xattrhead);
3350*da6c28aaSamw 					xattrp = NULL;
3351*da6c28aaSamw 					xattr_linkp = NULL;
3352*da6c28aaSamw 					xattrhead = NULL;
3353*da6c28aaSamw 				}
3354*da6c28aaSamw #endif
33557c478bd9Sstevel@tonic-gate 				continue;
33567c478bd9Sstevel@tonic-gate 			}
33577c478bd9Sstevel@tonic-gate 		newfile = ((fstatat(dirfd, comp,
33587c478bd9Sstevel@tonic-gate 		    &xtractbuf, 0) == -1) ? TRUE : FALSE);
3359*da6c28aaSamw 		ofile = openat(dirfd, comp, O_RDWR|O_CREAT|O_TRUNC,
3360*da6c28aaSamw 		    stbuf.st_mode & MODEMASK);
3361*da6c28aaSamw 		saveerrno = errno;
3362*da6c28aaSamw 
3363*da6c28aaSamw #if defined(O_XATTR)
3364*da6c28aaSamw 		if (xattrp != NULL) {
3365*da6c28aaSamw 			if (ofile < 0) {
3366*da6c28aaSamw 				ofile = retry_open_attr(dirfd, cwd,
3367*da6c28aaSamw 				    dirp, attrinfo->attr_parent, comp,
3368*da6c28aaSamw 				    O_RDWR|O_CREAT|O_TRUNC,
3369*da6c28aaSamw 				    stbuf.st_mode & MODEMASK);
3370*da6c28aaSamw 			}
3371*da6c28aaSamw 		}
3372*da6c28aaSamw #endif
3373*da6c28aaSamw 		if (ofile < 0) {
3374*da6c28aaSamw 			errno = saveerrno;
33757c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
3376*da6c28aaSamw 			    "tar: %s%s%s%s - cannot create\n"),
3377*da6c28aaSamw 			    (xattrp == NULL) ? "" : (rw_sysattr ?
3378*da6c28aaSamw 			    gettext("system attribure ") :
3379*da6c28aaSamw 			    gettext("attribute ")),
3380*da6c28aaSamw 			    (xattrp == NULL) ? "" : xattrapath,
3381*da6c28aaSamw 			    (xattrp == NULL) ? "" : gettext(" of "),
3382*da6c28aaSamw 			    (xattrp == NULL) ? comp : namep);
33837c478bd9Sstevel@tonic-gate 			if (errflag)
33847c478bd9Sstevel@tonic-gate 				done(1);
33857c478bd9Sstevel@tonic-gate 			else
33867c478bd9Sstevel@tonic-gate 				Errflg = 1;
3387*da6c28aaSamw #if defined(O_XATTR)
3388*da6c28aaSamw 			if (xattrp != NULL) {
3389*da6c28aaSamw 				dblock.dbuf.typeflag = _XATTR_HDRTYPE;
3390*da6c28aaSamw 				free(xattrhead);
3391*da6c28aaSamw 				xattrp = NULL;
3392*da6c28aaSamw 				xattr_linkp = NULL;
3393*da6c28aaSamw 				xattrhead = NULL;
3394*da6c28aaSamw 			}
3395*da6c28aaSamw #endif
33967c478bd9Sstevel@tonic-gate 			passtape();
33977c478bd9Sstevel@tonic-gate 			continue;
33987c478bd9Sstevel@tonic-gate 		}
33997c478bd9Sstevel@tonic-gate 
340045916cd2Sjpk 		if (Tflag && (check_ext_attr(namep) == 0)) {
340145916cd2Sjpk 			if (errflag)
340245916cd2Sjpk 				done(1);
340345916cd2Sjpk 			else
340445916cd2Sjpk 				Errflg = 1;
340545916cd2Sjpk 			passtape();
340645916cd2Sjpk 			continue;
340745916cd2Sjpk 		}
340845916cd2Sjpk 
34097c478bd9Sstevel@tonic-gate 		if (extno != 0) {	/* file is in pieces */
34107c478bd9Sstevel@tonic-gate 			if (extotal < 1 || extotal > MAXEXT)
34117c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
3412*da6c28aaSamw 				    "tar: ignoring bad extent info for "
3413*da6c28aaSamw 				    "%s%s%s%s\n"),
3414*da6c28aaSamw 				    (xattrp == NULL) ? "" : (rw_sysattr ?
3415*da6c28aaSamw 				    gettext("system attribute ") :
3416*da6c28aaSamw 				    gettext("attribute ")),
3417*da6c28aaSamw 				    (xattrp == NULL) ? "" : xattrapath,
3418*da6c28aaSamw 				    (xattrp == NULL) ? "" : gettext(" of "),
3419*da6c28aaSamw 				    (xattrp == NULL) ? comp : namep);
34207c478bd9Sstevel@tonic-gate 			else {
3421*da6c28aaSamw 				/* extract it */
3422*da6c28aaSamw 				(void) xsfile(rw_sysattr, ofile);
34237c478bd9Sstevel@tonic-gate 			}
34247c478bd9Sstevel@tonic-gate 		}
34257c478bd9Sstevel@tonic-gate 		extno = 0;	/* let everyone know file is not split */
34267c478bd9Sstevel@tonic-gate 		bytes = stbuf.st_size;
34277c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(bytes);
34287c478bd9Sstevel@tonic-gate 		if (vflag) {
34297c478bd9Sstevel@tonic-gate 			(void) fprintf(vfile,
34307c478bd9Sstevel@tonic-gate 			    "x %s%s%s, %" FMT_off_t " bytes, ",
34317c478bd9Sstevel@tonic-gate 			    (xattrp == NULL) ? "" : dirp,
3432*da6c28aaSamw 			    (xattrp == NULL) ? "" : (rw_sysattr ?
3433*da6c28aaSamw 			    gettext(" system attribute ") :
3434*da6c28aaSamw 			    gettext(" attribute ")),
3435*da6c28aaSamw 			    (xattrp == NULL) ? namep : xattrapath, bytes);
34367c478bd9Sstevel@tonic-gate 			if (NotTape)
34377c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "%" FMT_blkcnt_t "K\n",
34387c478bd9Sstevel@tonic-gate 				    K(blocks));
34397c478bd9Sstevel@tonic-gate 			else
34407c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("%"
34417c478bd9Sstevel@tonic-gate 				    FMT_blkcnt_t " tape blocks\n"), blocks);
34427c478bd9Sstevel@tonic-gate 		}
34437c478bd9Sstevel@tonic-gate 
3444*da6c28aaSamw 		if (xblocks(rw_sysattr, bytes, ofile) != 0) {
3445*da6c28aaSamw #if defined(O_XATTR)
3446*da6c28aaSamw 			if (xattrp != NULL) {
3447*da6c28aaSamw 				free(xattrhead);
3448*da6c28aaSamw 				xattrp = NULL;
3449*da6c28aaSamw 				xattr_linkp = NULL;
3450*da6c28aaSamw 				xattrhead = NULL;
3451*da6c28aaSamw 			}
3452*da6c28aaSamw #endif
3453*da6c28aaSamw 			continue;
3454*da6c28aaSamw 		}
34557c478bd9Sstevel@tonic-gate filedone:
34567c478bd9Sstevel@tonic-gate 		if (mflag == 0 && !symflag) {
34577c478bd9Sstevel@tonic-gate 			if (dir)
34587c478bd9Sstevel@tonic-gate 				doDirTimes(namep, stbuf.st_mtim);
3459*da6c28aaSamw 
34607c478bd9Sstevel@tonic-gate 			else
3461*da6c28aaSamw #if defined(O_XATTR)
3462*da6c28aaSamw 				if (xattrp != NULL) {
3463*da6c28aaSamw 					/*
3464*da6c28aaSamw 					 * Set the time on the attribute unless
3465*da6c28aaSamw 					 * the attribute is a system attribute
3466*da6c28aaSamw 					 * (can't successfully do this) or the
3467*da6c28aaSamw 					 * hidden attribute directory, "." (the
3468*da6c28aaSamw 					 * time on the hidden attribute
3469*da6c28aaSamw 					 * directory will be updated when
3470*da6c28aaSamw 					 * attributes are restored, otherwise
3471*da6c28aaSamw 					 * it's transient).
3472*da6c28aaSamw 					 */
3473*da6c28aaSamw 					if (!rw_sysattr && (Hiddendir == 0)) {
3474*da6c28aaSamw 						setPathTimes(dirfd, comp,
3475*da6c28aaSamw 						    stbuf.st_mtim);
3476*da6c28aaSamw 					}
3477*da6c28aaSamw 				} else
3478*da6c28aaSamw 					setPathTimes(dirfd, comp,
3479*da6c28aaSamw 					    stbuf.st_mtim);
3480*da6c28aaSamw #else
34817c478bd9Sstevel@tonic-gate 				setPathTimes(dirfd, comp, stbuf.st_mtim);
3482*da6c28aaSamw #endif
34837c478bd9Sstevel@tonic-gate 		}
34847c478bd9Sstevel@tonic-gate 
34857c478bd9Sstevel@tonic-gate 		/* moved this code from above */
34867c478bd9Sstevel@tonic-gate 		if (pflag && !symflag && Hiddendir == 0) {
3487*da6c28aaSamw 			if (xattrp != NULL)
34887c478bd9Sstevel@tonic-gate 				(void) fchmod(ofile, stbuf.st_mode & MODEMASK);
34897c478bd9Sstevel@tonic-gate 			else
34907c478bd9Sstevel@tonic-gate 				(void) chmod(namep, stbuf.st_mode & MODEMASK);
34917c478bd9Sstevel@tonic-gate 		}
34927c478bd9Sstevel@tonic-gate 
34937c478bd9Sstevel@tonic-gate 
34947c478bd9Sstevel@tonic-gate 		/*
34957c478bd9Sstevel@tonic-gate 		 * Because ancillary file preceeds the normal file,
34967c478bd9Sstevel@tonic-gate 		 * acl info may have been retrieved (in aclp).
34977c478bd9Sstevel@tonic-gate 		 * All file types are directed here (go filedone).
34987c478bd9Sstevel@tonic-gate 		 * Always restore ACLs if there are ACLs.
34997c478bd9Sstevel@tonic-gate 		 */
35007c478bd9Sstevel@tonic-gate 		if (aclp != NULL) {
35017c478bd9Sstevel@tonic-gate 			int ret;
35027c478bd9Sstevel@tonic-gate 
35037c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
3504*da6c28aaSamw 			if (xattrp != NULL) {
35057c478bd9Sstevel@tonic-gate 				if (Hiddendir)
3506fa9e4066Sahrens 					ret = facl_set(dirfd, aclp);
35077c478bd9Sstevel@tonic-gate 				else
3508fa9e4066Sahrens 					ret = facl_set(ofile, aclp);
35097c478bd9Sstevel@tonic-gate 			} else {
3510fa9e4066Sahrens 				ret = acl_set(namep, aclp);
35117c478bd9Sstevel@tonic-gate 			}
35127c478bd9Sstevel@tonic-gate #else
351345916cd2Sjpk 			ret = acl_set(namep, aclp);
35147c478bd9Sstevel@tonic-gate #endif
35157c478bd9Sstevel@tonic-gate 			if (ret < 0) {
35167c478bd9Sstevel@tonic-gate 				if (pflag) {
35177c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
3518*da6c28aaSamw 					    "%s%s%s%s: failed to set acl "
3519*da6c28aaSamw 					    "entries\n"), namep,
3520*da6c28aaSamw 					    (xattrp == NULL) ? "" :
3521*da6c28aaSamw 					    (rw_sysattr ? gettext(
3522*da6c28aaSamw 					    " system attribute ") :
3523*da6c28aaSamw 					    gettext(" attribute ")),
3524*da6c28aaSamw 					    (xattrp == NULL) ? "" :
3525*da6c28aaSamw 					    xattrapath);
35267c478bd9Sstevel@tonic-gate 				}
35277c478bd9Sstevel@tonic-gate 				/* else: silent and continue */
35287c478bd9Sstevel@tonic-gate 			}
3529fa9e4066Sahrens 			acl_free(aclp);
35307c478bd9Sstevel@tonic-gate 			aclp = NULL;
35317c478bd9Sstevel@tonic-gate 		}
35327c478bd9Sstevel@tonic-gate 
35337c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
3534*da6c28aaSamw 		if (xattrp != NULL) {
35357c478bd9Sstevel@tonic-gate 			free(xattrhead);
35367c478bd9Sstevel@tonic-gate 			xattrp = NULL;
35377c478bd9Sstevel@tonic-gate 			xattr_linkp = NULL;
35387c478bd9Sstevel@tonic-gate 			xattrhead = NULL;
35397c478bd9Sstevel@tonic-gate 		}
35407c478bd9Sstevel@tonic-gate #endif
35417c478bd9Sstevel@tonic-gate 
35427c478bd9Sstevel@tonic-gate 		if (!oflag)
35437c478bd9Sstevel@tonic-gate 		    resugname(dirfd, comp, symflag); /* set file ownership */
35447c478bd9Sstevel@tonic-gate 
35457c478bd9Sstevel@tonic-gate 		if (pflag && newfile == TRUE && !dir &&
35467c478bd9Sstevel@tonic-gate 		    (dblock.dbuf.typeflag == '0' ||
35477c478bd9Sstevel@tonic-gate 		    dblock.dbuf.typeflag == NULL ||
35487c478bd9Sstevel@tonic-gate 		    convflag || dblock.dbuf.typeflag == '1')) {
35497c478bd9Sstevel@tonic-gate 			if (fstat(ofile, &xtractbuf) == -1)
35507c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
3551*da6c28aaSamw 				    "tar: cannot stat extracted file "
3552*da6c28aaSamw 				    "%s%s%s%s\n"),
3553*da6c28aaSamw 				    (xattrp == NULL) ? "" : (rw_sysattr ?
3554*da6c28aaSamw 				    gettext("system attribute ") :
3555*da6c28aaSamw 				    gettext("attribute ")),
3556*da6c28aaSamw 				    (xattrp == NULL) ? "" : xattrapath,
3557*da6c28aaSamw 				    (xattrp == NULL) ? "" :
3558*da6c28aaSamw 				    gettext(" of "), namep);
3559*da6c28aaSamw 
35607c478bd9Sstevel@tonic-gate 			else if ((xtractbuf.st_mode & (MODEMASK & ~S_IFMT))
35617c478bd9Sstevel@tonic-gate 			    != (stbuf.st_mode & (MODEMASK & ~S_IFMT))) {
35627c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
35637c478bd9Sstevel@tonic-gate 				    "tar: warning - file permissions have "
3564*da6c28aaSamw 				    "changed for %s%s%s%s (are 0%o, should be "
35657c478bd9Sstevel@tonic-gate 				    "0%o)\n"),
3566*da6c28aaSamw 				    (xattrp == NULL) ? "" : (rw_sysattr ?
3567*da6c28aaSamw 				    gettext("system attribute ") :
3568*da6c28aaSamw 				    gettext("attribute ")),
3569*da6c28aaSamw 				    (xattrp == NULL) ? "" : xattrapath,
3570*da6c28aaSamw 				    (xattrp == NULL) ? "" :
3571*da6c28aaSamw 				    gettext(" of "), namep,
3572*da6c28aaSamw 				    xtractbuf.st_mode, stbuf.st_mode);
3573*da6c28aaSamw 
35747c478bd9Sstevel@tonic-gate 			}
35757c478bd9Sstevel@tonic-gate 		}
35767c478bd9Sstevel@tonic-gate 		if (ofile != -1) {
35777c478bd9Sstevel@tonic-gate 			(void) close(dirfd);
35787c478bd9Sstevel@tonic-gate 			dirfd = -1;
35797c478bd9Sstevel@tonic-gate 			if (close(ofile) != 0)
35807c478bd9Sstevel@tonic-gate 				vperror(2, gettext("close error"));
3581*da6c28aaSamw 			ofile = -1;
35827c478bd9Sstevel@tonic-gate 		}
35837c478bd9Sstevel@tonic-gate 		xcnt++;			/* increment # files extracted */
35847c478bd9Sstevel@tonic-gate 		}
358545916cd2Sjpk 
358645916cd2Sjpk 		/*
358745916cd2Sjpk 		 * Process ancillary file.
358845916cd2Sjpk 		 *
358945916cd2Sjpk 		 */
359045916cd2Sjpk 
35917c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == 'A') {	/* acl info */
35927c478bd9Sstevel@tonic-gate 			char	buf[TBLOCK];
35937c478bd9Sstevel@tonic-gate 			char	*secp;
35947c478bd9Sstevel@tonic-gate 			char	*tp;
35957c478bd9Sstevel@tonic-gate 			int	attrsize;
35967c478bd9Sstevel@tonic-gate 			int	cnt;
35977c478bd9Sstevel@tonic-gate 
359845916cd2Sjpk 			/* reset Trusted Extensions flags */
359945916cd2Sjpk 			dir_flag = 0;
360045916cd2Sjpk 			mld_flag = 0;
360145916cd2Sjpk 			lk_rpath_flag = 0;
360245916cd2Sjpk 			rpath_flag = 0;
36037c478bd9Sstevel@tonic-gate 
36047c478bd9Sstevel@tonic-gate 			if (pflag) {
36057c478bd9Sstevel@tonic-gate 				bytes = stbuf.st_size;
36067c478bd9Sstevel@tonic-gate 				if ((secp = malloc((int)bytes)) == NULL) {
36077c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
36087c478bd9Sstevel@tonic-gate 					    "Insufficient memory for acl\n"));
36097c478bd9Sstevel@tonic-gate 					passtape();
36107c478bd9Sstevel@tonic-gate 					continue;
36117c478bd9Sstevel@tonic-gate 				}
36127c478bd9Sstevel@tonic-gate 				tp = secp;
36137c478bd9Sstevel@tonic-gate 				blocks = TBLOCKS(bytes);
361445916cd2Sjpk 
361545916cd2Sjpk 				/*
361645916cd2Sjpk 				 * Display a line for each ancillary file.
361745916cd2Sjpk 				 */
361845916cd2Sjpk 				if (vflag && Tflag)
361945916cd2Sjpk 					(void) fprintf(vfile, "x %s(A), %"
362045916cd2Sjpk 					    FMT_blkcnt_t " bytes, %"
362145916cd2Sjpk 					    FMT_blkcnt_t " tape blocks\n",
362245916cd2Sjpk 					    namep, bytes, blocks);
362345916cd2Sjpk 
36247c478bd9Sstevel@tonic-gate 				while (blocks-- > 0) {
36257c478bd9Sstevel@tonic-gate 					readtape(buf);
36267c478bd9Sstevel@tonic-gate 					if (bytes <= TBLOCK) {
36277c478bd9Sstevel@tonic-gate 						(void) memcpy(tp, buf,
36287c478bd9Sstevel@tonic-gate 						    (size_t)bytes);
36297c478bd9Sstevel@tonic-gate 						break;
36307c478bd9Sstevel@tonic-gate 					} else {
36317c478bd9Sstevel@tonic-gate 						(void) memcpy(tp, buf,
36327c478bd9Sstevel@tonic-gate 						    TBLOCK);
36337c478bd9Sstevel@tonic-gate 						tp += TBLOCK;
36347c478bd9Sstevel@tonic-gate 					}
36357c478bd9Sstevel@tonic-gate 					bytes -= TBLOCK;
36367c478bd9Sstevel@tonic-gate 				}
3637fa9e4066Sahrens 				bytes = stbuf.st_size;
36387c478bd9Sstevel@tonic-gate 				/* got all attributes in secp */
36397c478bd9Sstevel@tonic-gate 				tp = secp;
36407c478bd9Sstevel@tonic-gate 				do {
36417c478bd9Sstevel@tonic-gate 					attr = (struct sec_attr *)tp;
36427c478bd9Sstevel@tonic-gate 					switch (attr->attr_type) {
36437c478bd9Sstevel@tonic-gate 					case UFSD_ACL:
3644fa9e4066Sahrens 					case ACE_ACL:
36457c478bd9Sstevel@tonic-gate 						(void) sscanf(attr->attr_len,
3646fa9e4066Sahrens 						    "%7o",
3647fa9e4066Sahrens 						    (uint_t *)
3648fa9e4066Sahrens 						    &cnt);
36497c478bd9Sstevel@tonic-gate 						/* header is 8 */
36507c478bd9Sstevel@tonic-gate 						attrsize = 8 + (int)strlen(
36517c478bd9Sstevel@tonic-gate 						    &attr->attr_info[0]) + 1;
3652fa9e4066Sahrens 						error =
3653fa9e4066Sahrens 						    acl_fromtext(
3654fa9e4066Sahrens 						    &attr->attr_info[0], &aclp);
3655fa9e4066Sahrens 
3656fa9e4066Sahrens 						if (error != 0) {
36577c478bd9Sstevel@tonic-gate 							(void) fprintf(stderr,
36587c478bd9Sstevel@tonic-gate 							    gettext(
36597c478bd9Sstevel@tonic-gate 							    "aclfromtext "
3660fa9e4066Sahrens 							    "failed: %s\n"),
3661fa9e4066Sahrens 							    acl_strerror(
3662fa9e4066Sahrens 							    error));
3663fa9e4066Sahrens 							bytes -= attrsize;
36647c478bd9Sstevel@tonic-gate 							break;
36657c478bd9Sstevel@tonic-gate 						}
3666fa9e4066Sahrens 						if (acl_cnt(aclp) != cnt) {
36677c478bd9Sstevel@tonic-gate 							(void) fprintf(stderr,
36687c478bd9Sstevel@tonic-gate 							    gettext(
36697c478bd9Sstevel@tonic-gate 							    "aclcnt error\n"));
3670fa9e4066Sahrens 							bytes -= attrsize;
36717c478bd9Sstevel@tonic-gate 							break;
36727c478bd9Sstevel@tonic-gate 						}
36737c478bd9Sstevel@tonic-gate 						bytes -= attrsize;
36747c478bd9Sstevel@tonic-gate 						break;
36757c478bd9Sstevel@tonic-gate 
367645916cd2Sjpk 					/* Trusted Extensions */
367745916cd2Sjpk 
367845916cd2Sjpk 					case DIR_TYPE:
367945916cd2Sjpk 					case LBL_TYPE:
368045916cd2Sjpk 					case APRIV_TYPE:
368145916cd2Sjpk 					case FPRIV_TYPE:
368245916cd2Sjpk 					case COMP_TYPE:
368345916cd2Sjpk 					case LK_COMP_TYPE:
368445916cd2Sjpk 					case ATTR_FLAG_TYPE:
368545916cd2Sjpk 						attrsize =
368645916cd2Sjpk 						    sizeof (struct sec_attr) +
368745916cd2Sjpk 						    strlen(&attr->attr_info[0]);
368845916cd2Sjpk 						bytes -= attrsize;
368945916cd2Sjpk 						if (Tflag)
369045916cd2Sjpk 							extract_attr(&namep,
369145916cd2Sjpk 							    attr);
369245916cd2Sjpk 						break;
36937c478bd9Sstevel@tonic-gate 
36947c478bd9Sstevel@tonic-gate 					default:
36957c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(
36967c478bd9Sstevel@tonic-gate 						    "unrecognized attr"
36977c478bd9Sstevel@tonic-gate 						    " type\n"));
36987c478bd9Sstevel@tonic-gate 						bytes = (off_t)0;
36997c478bd9Sstevel@tonic-gate 						break;
37007c478bd9Sstevel@tonic-gate 					}
37017c478bd9Sstevel@tonic-gate 
37027c478bd9Sstevel@tonic-gate 					/* next attributes */
37037c478bd9Sstevel@tonic-gate 					tp += attrsize;
37047c478bd9Sstevel@tonic-gate 				} while (bytes != 0);
37057c478bd9Sstevel@tonic-gate 				free(secp);
3706*da6c28aaSamw 			} else {
37077c478bd9Sstevel@tonic-gate 				passtape();
3708*da6c28aaSamw 			}
37097c478bd9Sstevel@tonic-gate 		} /* acl */
37107c478bd9Sstevel@tonic-gate 
37117c478bd9Sstevel@tonic-gate 	} /* for */
37127c478bd9Sstevel@tonic-gate 
37137c478bd9Sstevel@tonic-gate 	/*
37147c478bd9Sstevel@tonic-gate 	 *  Ensure that all the directories still on the directory stack
37157c478bd9Sstevel@tonic-gate 	 *  get their modification times set correctly by flushing the
37167c478bd9Sstevel@tonic-gate 	 *  stack.
37177c478bd9Sstevel@tonic-gate 	 */
37187c478bd9Sstevel@tonic-gate 
37197c478bd9Sstevel@tonic-gate 	doDirTimes(NULL, time_zero);
37207c478bd9Sstevel@tonic-gate 
3721*da6c28aaSamw #if defined(O_XATTR)
3722*da6c28aaSamw 		if (xattrp != NULL) {
3723*da6c28aaSamw 			free(xattrhead);
3724*da6c28aaSamw 			xattrp = NULL;
3725*da6c28aaSamw 			xattr_linkp = NULL;
3726*da6c28aaSamw 			xattrhead = NULL;
3727*da6c28aaSamw 		}
3728*da6c28aaSamw #endif
3729*da6c28aaSamw 
37307c478bd9Sstevel@tonic-gate 	/*
37317c478bd9Sstevel@tonic-gate 	 * Check if the number of files extracted is different from the
37327c478bd9Sstevel@tonic-gate 	 * number of files listed on the command line
37337c478bd9Sstevel@tonic-gate 	 */
37347c478bd9Sstevel@tonic-gate 	if (fcnt > xcnt) {
37357c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
37367c478bd9Sstevel@tonic-gate 		    gettext("tar: %d file(s) not extracted\n"),
37377c478bd9Sstevel@tonic-gate 		fcnt-xcnt);
37387c478bd9Sstevel@tonic-gate 		Errflg = 1;
37397c478bd9Sstevel@tonic-gate 	}
37407c478bd9Sstevel@tonic-gate }
37417c478bd9Sstevel@tonic-gate 
37427c478bd9Sstevel@tonic-gate /*
37437c478bd9Sstevel@tonic-gate  *	xblocks		extract file/extent from tape to output file
37447c478bd9Sstevel@tonic-gate  *
37457c478bd9Sstevel@tonic-gate  *	xblocks(bytes, ofile);
37467c478bd9Sstevel@tonic-gate  *	unsigned long long bytes;	size of extent or file to be extracted
37477c478bd9Sstevel@tonic-gate  *
37487c478bd9Sstevel@tonic-gate  *	called by doxtract() and xsfile()
37497c478bd9Sstevel@tonic-gate  */
37507c478bd9Sstevel@tonic-gate 
3751*da6c28aaSamw static int
3752*da6c28aaSamw xblocks(int issysattr, off_t bytes, int ofile)
37537c478bd9Sstevel@tonic-gate {
37547c478bd9Sstevel@tonic-gate 	blkcnt_t blocks;
37557c478bd9Sstevel@tonic-gate 	char buf[TBLOCK];
37567c478bd9Sstevel@tonic-gate 	char tempname[NAMSIZ+1];
37577c478bd9Sstevel@tonic-gate 	int write_count;
37587c478bd9Sstevel@tonic-gate 
37597c478bd9Sstevel@tonic-gate 	blocks = TBLOCKS(bytes);
37607c478bd9Sstevel@tonic-gate 	while (blocks-- > 0) {
37617c478bd9Sstevel@tonic-gate 		readtape(buf);
37627c478bd9Sstevel@tonic-gate 		if (bytes > TBLOCK)
37637c478bd9Sstevel@tonic-gate 			write_count = TBLOCK;
37647c478bd9Sstevel@tonic-gate 		else
37657c478bd9Sstevel@tonic-gate 			write_count = bytes;
37667c478bd9Sstevel@tonic-gate 		if (write(ofile, buf, write_count) < 0) {
3767*da6c28aaSamw 			int saveerrno = errno;
3768*da6c28aaSamw 
37697c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_PATH)
37707c478bd9Sstevel@tonic-gate 				(void) strcpy(tempname, Xtarhdr.x_path);
37717c478bd9Sstevel@tonic-gate 			else
37727c478bd9Sstevel@tonic-gate 				(void) sprintf(tempname, "%.*s", NAMSIZ,
37737c478bd9Sstevel@tonic-gate 				    dblock.dbuf.name);
3774*da6c28aaSamw 			/*
3775*da6c28aaSamw 			 * If the extended system attribute being extracted
3776*da6c28aaSamw 			 * contains attributes that the user needs privileges
3777*da6c28aaSamw 			 * for, then just display a warning message, skip
3778*da6c28aaSamw 			 * the extraction of this file, and return.
3779*da6c28aaSamw 			 */
3780*da6c28aaSamw 			if ((saveerrno == EPERM) && issysattr) {
37817c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
3782*da6c28aaSamw 				    "tar: unable to extract system attribute "
3783*da6c28aaSamw 				    "%s: insufficient privileges\n"), tempname);
3784*da6c28aaSamw 				Errflg = 1;
3785*da6c28aaSamw 				return (1);
3786*da6c28aaSamw 			} else {
3787*da6c28aaSamw 				(void) fprintf(stderr, gettext(
3788*da6c28aaSamw 				    "tar: %s: HELP - extract write error\n"),
3789*da6c28aaSamw 				    tempname);
37907c478bd9Sstevel@tonic-gate 				done(2);
37917c478bd9Sstevel@tonic-gate 			}
3792*da6c28aaSamw 		}
37937c478bd9Sstevel@tonic-gate 		bytes -= TBLOCK;
37947c478bd9Sstevel@tonic-gate 	}
3795*da6c28aaSamw 
3796*da6c28aaSamw 	return (0);
37977c478bd9Sstevel@tonic-gate }
37987c478bd9Sstevel@tonic-gate 
37997c478bd9Sstevel@tonic-gate 
38007c478bd9Sstevel@tonic-gate /*
38017c478bd9Sstevel@tonic-gate  * 	xsfile	extract split file
38027c478bd9Sstevel@tonic-gate  *
38037c478bd9Sstevel@tonic-gate  *	xsfile(ofd);	ofd = output file descriptor
38047c478bd9Sstevel@tonic-gate  *
38057c478bd9Sstevel@tonic-gate  *	file extracted and put in ofd via xblocks()
38067c478bd9Sstevel@tonic-gate  *
38077c478bd9Sstevel@tonic-gate  *	NOTE:  only called by doxtract() to extract one large file
38087c478bd9Sstevel@tonic-gate  */
38097c478bd9Sstevel@tonic-gate 
38107c478bd9Sstevel@tonic-gate static	union	hblock	savedblock;	/* to ensure same file across volumes */
38117c478bd9Sstevel@tonic-gate 
3812*da6c28aaSamw static int
3813*da6c28aaSamw xsfile(int issysattr, int ofd)
38147c478bd9Sstevel@tonic-gate {
38157c478bd9Sstevel@tonic-gate 	int i, c;
3816*da6c28aaSamw 	int sysattrerr = 0;
38177c478bd9Sstevel@tonic-gate 	char name[PATH_MAX+1];	/* holds name for diagnostics */
38187c478bd9Sstevel@tonic-gate 	int extents, totalext;
38197c478bd9Sstevel@tonic-gate 	off_t bytes, totalbytes;
38207c478bd9Sstevel@tonic-gate 
38217c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_PATH)
38227c478bd9Sstevel@tonic-gate 		(void) strcpy(name, Xtarhdr.x_path);
38237c478bd9Sstevel@tonic-gate 	else
38247c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "%.*s", NAMSIZ, dblock.dbuf.name);
38257c478bd9Sstevel@tonic-gate 
38267c478bd9Sstevel@tonic-gate 	totalbytes = (off_t)0;		/* in case we read in half the file */
38277c478bd9Sstevel@tonic-gate 	totalext = 0;		/* these keep count */
38287c478bd9Sstevel@tonic-gate 
38297c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
38307c478bd9Sstevel@tonic-gate 	    "tar: %s split across %d volumes\n"), name, extotal);
38317c478bd9Sstevel@tonic-gate 
38327c478bd9Sstevel@tonic-gate 	/* make sure we do extractions in order */
38337c478bd9Sstevel@tonic-gate 	if (extno != 1) {	/* starting in middle of file? */
38347c478bd9Sstevel@tonic-gate 		(void) printf(gettext(
38357c478bd9Sstevel@tonic-gate 		    "tar: first extent read is not #1\n"
38363d63ea05Sas145665 		    "OK to read file beginning with extent #%d (%s/%s) ? "),
38373d63ea05Sas145665 		    extno, yesstr, nostr);
38383d63ea05Sas145665 		if (yes() == 0) {
38397c478bd9Sstevel@tonic-gate canit:
38407c478bd9Sstevel@tonic-gate 			passtape();
38417c478bd9Sstevel@tonic-gate 			if (close(ofd) != 0)
38427c478bd9Sstevel@tonic-gate 				vperror(2, gettext("close error"));
3843*da6c28aaSamw 			if (sysattrerr) {
3844*da6c28aaSamw 				return (1);
3845*da6c28aaSamw 			} else {
3846*da6c28aaSamw 				return (0);
3847*da6c28aaSamw 			}
38487c478bd9Sstevel@tonic-gate 		}
38497c478bd9Sstevel@tonic-gate 	}
38507c478bd9Sstevel@tonic-gate 	extents = extotal;
38517c478bd9Sstevel@tonic-gate 	i = extno;
38527c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
38537c478bd9Sstevel@tonic-gate 	while (1) {
38547c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_SIZE) {
38557c478bd9Sstevel@tonic-gate 			bytes = extsize;
38567c478bd9Sstevel@tonic-gate 		} else {
38577c478bd9Sstevel@tonic-gate 			bytes = stbuf.st_size;
38587c478bd9Sstevel@tonic-gate 		}
38597c478bd9Sstevel@tonic-gate 
38607c478bd9Sstevel@tonic-gate 		if (vflag)
38617c478bd9Sstevel@tonic-gate 			(void) fprintf(vfile, "+++ x %s [extent #%d], %"
38627c478bd9Sstevel@tonic-gate 			    FMT_off_t " bytes, %ldK\n", name, extno, bytes,
38637c478bd9Sstevel@tonic-gate 			    (long)K(TBLOCKS(bytes)));
3864*da6c28aaSamw 		if (xblocks(issysattr, bytes, ofd) != 0) {
3865*da6c28aaSamw 			sysattrerr = 1;
3866*da6c28aaSamw 			goto canit;
3867*da6c28aaSamw 		}
38687c478bd9Sstevel@tonic-gate 
38697c478bd9Sstevel@tonic-gate 		totalbytes += bytes;
38707c478bd9Sstevel@tonic-gate 		totalext++;
38717c478bd9Sstevel@tonic-gate 		if (++i > extents)
38727c478bd9Sstevel@tonic-gate 			break;
38737c478bd9Sstevel@tonic-gate 
38747c478bd9Sstevel@tonic-gate 		/* get next volume and verify it's the right one */
38757c478bd9Sstevel@tonic-gate 		copy(&savedblock, &dblock);
38767c478bd9Sstevel@tonic-gate tryagain:
38777c478bd9Sstevel@tonic-gate 		newvol();
38787c478bd9Sstevel@tonic-gate 		xhdr_flgs = 0;
38797c478bd9Sstevel@tonic-gate 		getdir();
38807c478bd9Sstevel@tonic-gate 		if (Xhdrflag > 0)
38817c478bd9Sstevel@tonic-gate 			(void) get_xdata();	/* Get x-header & regular hdr */
38827c478bd9Sstevel@tonic-gate 		if (endtape()) {	/* seemingly empty volume */
38837c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
38847c478bd9Sstevel@tonic-gate 			    "tar: first record is null\n"));
38857c478bd9Sstevel@tonic-gate asknicely:
38867c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
38877c478bd9Sstevel@tonic-gate 			    "tar: need volume with extent #%d of %s\n"),
38887c478bd9Sstevel@tonic-gate 			    i, name);
38897c478bd9Sstevel@tonic-gate 			goto tryagain;
38907c478bd9Sstevel@tonic-gate 		}
38917c478bd9Sstevel@tonic-gate 		if (notsame()) {
38927c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
38937c478bd9Sstevel@tonic-gate 			    "tar: first file on that volume is not "
38947c478bd9Sstevel@tonic-gate 			    "the same file\n"));
38957c478bd9Sstevel@tonic-gate 			goto asknicely;
38967c478bd9Sstevel@tonic-gate 		}
38977c478bd9Sstevel@tonic-gate 		if (i != extno) {
38987c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
3899eace40a5Sceastha 			    "tar: extent #%d received out of order\ntar: "
3900eace40a5Sceastha 			    "should be #%d\n"), extno, i);
39017c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
39027c478bd9Sstevel@tonic-gate 			    "Ignore error, Abort this file, or "
39037c478bd9Sstevel@tonic-gate 			    "load New volume (i/a/n) ? "));
39047c478bd9Sstevel@tonic-gate 			c = response();
39057c478bd9Sstevel@tonic-gate 			if (c == 'a')
39067c478bd9Sstevel@tonic-gate 				goto canit;
39077c478bd9Sstevel@tonic-gate 			if (c != 'i')		/* default to new volume */
39087c478bd9Sstevel@tonic-gate 				goto asknicely;
39097c478bd9Sstevel@tonic-gate 			i = extno;		/* okay, start from there */
39107c478bd9Sstevel@tonic-gate 		}
39117c478bd9Sstevel@tonic-gate 	}
39127c478bd9Sstevel@tonic-gate 	if (vflag)
39137c478bd9Sstevel@tonic-gate 		(void) fprintf(vfile, gettext(
39147c478bd9Sstevel@tonic-gate 		    "x %s (in %d extents), %" FMT_off_t " bytes, %ldK\n"),
39157c478bd9Sstevel@tonic-gate 		    name, totalext, totalbytes, (long)K(TBLOCKS(totalbytes)));
3916*da6c28aaSamw 
3917*da6c28aaSamw 	return (0);
39187c478bd9Sstevel@tonic-gate }
39197c478bd9Sstevel@tonic-gate 
39207c478bd9Sstevel@tonic-gate 
39217c478bd9Sstevel@tonic-gate /*
39227c478bd9Sstevel@tonic-gate  *	notsame()	check if extract file extent is invalid
39237c478bd9Sstevel@tonic-gate  *
39247c478bd9Sstevel@tonic-gate  *	returns true if anything differs between savedblock and dblock
39257c478bd9Sstevel@tonic-gate  *	except extno (extent number), checksum, or size (extent size).
39267c478bd9Sstevel@tonic-gate  *	Determines if this header belongs to the same file as the one we're
39277c478bd9Sstevel@tonic-gate  *	extracting.
39287c478bd9Sstevel@tonic-gate  *
39297c478bd9Sstevel@tonic-gate  *	NOTE:	though rather bulky, it is only called once per file
39307c478bd9Sstevel@tonic-gate  *		extension, and it can withstand changes in the definition
39317c478bd9Sstevel@tonic-gate  *		of the header structure.
39327c478bd9Sstevel@tonic-gate  *
39337c478bd9Sstevel@tonic-gate  *	WARNING:	this routine is local to xsfile() above
39347c478bd9Sstevel@tonic-gate  */
39357c478bd9Sstevel@tonic-gate 
39367c478bd9Sstevel@tonic-gate static int
39377c478bd9Sstevel@tonic-gate notsame(void)
39387c478bd9Sstevel@tonic-gate {
39397c478bd9Sstevel@tonic-gate 	return (
39407c478bd9Sstevel@tonic-gate 	    (strncmp(savedblock.dbuf.name, dblock.dbuf.name, NAMSIZ)) ||
39417c478bd9Sstevel@tonic-gate 	    (strcmp(savedblock.dbuf.mode, dblock.dbuf.mode)) ||
39427c478bd9Sstevel@tonic-gate 	    (strcmp(savedblock.dbuf.uid, dblock.dbuf.uid)) ||
39437c478bd9Sstevel@tonic-gate 	    (strcmp(savedblock.dbuf.gid, dblock.dbuf.gid)) ||
39447c478bd9Sstevel@tonic-gate 	    (strcmp(savedblock.dbuf.mtime, dblock.dbuf.mtime)) ||
39457c478bd9Sstevel@tonic-gate 	    (savedblock.dbuf.typeflag != dblock.dbuf.typeflag) ||
39467c478bd9Sstevel@tonic-gate 	    (strncmp(savedblock.dbuf.linkname, dblock.dbuf.linkname, NAMSIZ)) ||
39477c478bd9Sstevel@tonic-gate 	    (savedblock.dbuf.extotal != dblock.dbuf.extotal) ||
39487c478bd9Sstevel@tonic-gate 	    (strcmp(savedblock.dbuf.efsize, dblock.dbuf.efsize)));
39497c478bd9Sstevel@tonic-gate }
39507c478bd9Sstevel@tonic-gate 
39517c478bd9Sstevel@tonic-gate static void
39527c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
39537c478bd9Sstevel@tonic-gate dotable(char *argv[], int tbl_cnt)
39547c478bd9Sstevel@tonic-gate #else
39557c478bd9Sstevel@tonic-gate dotable(char *argv[])
39567c478bd9Sstevel@tonic-gate #endif
39577c478bd9Sstevel@tonic-gate 
39587c478bd9Sstevel@tonic-gate {
39597c478bd9Sstevel@tonic-gate 	int tcnt;			/* count # files tabled */
39607c478bd9Sstevel@tonic-gate 	int fcnt;			/* count # files in argv list */
39617c478bd9Sstevel@tonic-gate 	char *namep, *dirp, *comp;
39627c478bd9Sstevel@tonic-gate 	int want;
39637c478bd9Sstevel@tonic-gate 	char aclchar = ' ';			/* either blank or '+' */
39647c478bd9Sstevel@tonic-gate 	char templink[PATH_MAX+1];
3965*da6c28aaSamw 	attr_data_t *attrinfo = NULL;
39667c478bd9Sstevel@tonic-gate 
39677c478bd9Sstevel@tonic-gate 	dumping = 0;
39687c478bd9Sstevel@tonic-gate 
39697c478bd9Sstevel@tonic-gate 	/* if not on magtape, maximize seek speed */
39707c478bd9Sstevel@tonic-gate 	if (NotTape && !bflag) {
39717c478bd9Sstevel@tonic-gate #if SYS_BLOCK > TBLOCK
39727c478bd9Sstevel@tonic-gate 		nblock = SYS_BLOCK / TBLOCK;
39737c478bd9Sstevel@tonic-gate #else
39747c478bd9Sstevel@tonic-gate 		nblock = 1;
39757c478bd9Sstevel@tonic-gate #endif
39767c478bd9Sstevel@tonic-gate 	}
39777c478bd9Sstevel@tonic-gate 	/*
39787c478bd9Sstevel@tonic-gate 	 * Count the number of files that are to be tabled
39797c478bd9Sstevel@tonic-gate 	 */
39807c478bd9Sstevel@tonic-gate 	fcnt = tcnt = 0;
39817c478bd9Sstevel@tonic-gate 
39827c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
39837c478bd9Sstevel@tonic-gate 	initarg(argv, Filefile);
39847c478bd9Sstevel@tonic-gate 	while (nextarg() != NULL)
39857c478bd9Sstevel@tonic-gate 		++fcnt;
39867c478bd9Sstevel@tonic-gate 	fcnt += tbl_cnt;
39877c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
39887c478bd9Sstevel@tonic-gate 
39897c478bd9Sstevel@tonic-gate 	for (;;) {
39907c478bd9Sstevel@tonic-gate 
39917c478bd9Sstevel@tonic-gate 		/* namep is set by wantit to point to the full name */
3992*da6c28aaSamw 		if ((want = wantit(argv, &namep, &dirp, &comp, &attrinfo)) == 0)
39937c478bd9Sstevel@tonic-gate 			continue;
39947c478bd9Sstevel@tonic-gate 		if (want == -1)
39957c478bd9Sstevel@tonic-gate 			break;
39967c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag != 'A')
39977c478bd9Sstevel@tonic-gate 			++tcnt;
39987c478bd9Sstevel@tonic-gate 
39997c478bd9Sstevel@tonic-gate 		/*
40007c478bd9Sstevel@tonic-gate 		 * ACL support:
40017c478bd9Sstevel@tonic-gate 		 * aclchar is introduced to indicate if there are
40027c478bd9Sstevel@tonic-gate 		 * acl entries. longt() now takes one extra argument.
40037c478bd9Sstevel@tonic-gate 		 */
40047c478bd9Sstevel@tonic-gate 		if (vflag) {
40057c478bd9Sstevel@tonic-gate 			if (dblock.dbuf.typeflag == 'A') {
40067c478bd9Sstevel@tonic-gate 				aclchar = '+';
40077c478bd9Sstevel@tonic-gate 				passtape();
40087c478bd9Sstevel@tonic-gate 				continue;
40097c478bd9Sstevel@tonic-gate 			}
40107c478bd9Sstevel@tonic-gate 			longt(&stbuf, aclchar);
40117c478bd9Sstevel@tonic-gate 			aclchar = ' ';
40127c478bd9Sstevel@tonic-gate 		}
40137c478bd9Sstevel@tonic-gate 
40147c478bd9Sstevel@tonic-gate 
40157c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
4016*da6c28aaSamw 		if (xattrp != NULL) {
4017*da6c28aaSamw 			int	issysattr;
4018*da6c28aaSamw 			char	*bn = basename(attrinfo->attr_path);
40197c478bd9Sstevel@tonic-gate 
4020*da6c28aaSamw 			/*
4021*da6c28aaSamw 			 * We could use sysattr_type() to test whether or not
4022*da6c28aaSamw 			 * the attribute we are processing is really an
4023*da6c28aaSamw 			 * extended system attribute, which as of this writing
4024*da6c28aaSamw 			 * just does a strcmp(), however, sysattr_type() may
4025*da6c28aaSamw 			 * be changed to issue a pathconf() call instead, which
4026*da6c28aaSamw 			 * would require being changed into the parent attribute
4027*da6c28aaSamw 			 * directory.  So instead, just do simple string
4028*da6c28aaSamw 			 * comparisons to see if we are processing an extended
4029*da6c28aaSamw 			 * system attribute.
4030*da6c28aaSamw 			 */
4031*da6c28aaSamw 			issysattr = is_sysattr(bn);
4032*da6c28aaSamw 
4033*da6c28aaSamw 			(void) printf(gettext("%s %sattribute %s"),
4034*da6c28aaSamw 			    xattrp->h_names,
4035*da6c28aaSamw 			    issysattr ? gettext("system ") : "",
4036*da6c28aaSamw 			    attrinfo->attr_path);
40377c478bd9Sstevel@tonic-gate 		} else {
40387c478bd9Sstevel@tonic-gate 			(void) printf("%s", namep);
40397c478bd9Sstevel@tonic-gate 		}
40407c478bd9Sstevel@tonic-gate #else
40417c478bd9Sstevel@tonic-gate 			(void) printf("%s", namep);
40427c478bd9Sstevel@tonic-gate #endif
40437c478bd9Sstevel@tonic-gate 
40447c478bd9Sstevel@tonic-gate 		if (extno != 0) {
40457c478bd9Sstevel@tonic-gate 			if (vflag) {
40467c478bd9Sstevel@tonic-gate 				/* keep the '\n' for backwards compatibility */
40477c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext(
40487c478bd9Sstevel@tonic-gate 				    "\n [extent #%d of %d]"), extno, extotal);
40497c478bd9Sstevel@tonic-gate 			} else {
40507c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext(
40517c478bd9Sstevel@tonic-gate 				    " [extent #%d of %d]"), extno, extotal);
40527c478bd9Sstevel@tonic-gate 			}
40537c478bd9Sstevel@tonic-gate 		}
40547c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_LINKPATH) {
40557c478bd9Sstevel@tonic-gate 			(void) strcpy(templink, Xtarhdr.x_linkpath);
40567c478bd9Sstevel@tonic-gate 		} else {
40577c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
4058*da6c28aaSamw 			if (xattrp != NULL) {
40597c478bd9Sstevel@tonic-gate 				(void) sprintf(templink,
40607c478bd9Sstevel@tonic-gate 				    "file %.*s", NAMSIZ, xattrp->h_names);
40617c478bd9Sstevel@tonic-gate 			} else {
40627c478bd9Sstevel@tonic-gate 				(void) sprintf(templink, "%.*s", NAMSIZ,
40637c478bd9Sstevel@tonic-gate 				    dblock.dbuf.linkname);
40647c478bd9Sstevel@tonic-gate 			}
40657c478bd9Sstevel@tonic-gate #else
40667c478bd9Sstevel@tonic-gate 			(void) sprintf(templink, "%.*s", NAMSIZ,
40677c478bd9Sstevel@tonic-gate 			    dblock.dbuf.linkname);
40687c478bd9Sstevel@tonic-gate #endif
40697c478bd9Sstevel@tonic-gate 			templink[NAMSIZ] = '\0';
40707c478bd9Sstevel@tonic-gate 		}
40717c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '1') {
40727c478bd9Sstevel@tonic-gate 			/*
40737c478bd9Sstevel@tonic-gate 			 * TRANSLATION_NOTE
40747c478bd9Sstevel@tonic-gate 			 *	Subject is omitted here.
40757c478bd9Sstevel@tonic-gate 			 *	Translate this as if
40767c478bd9Sstevel@tonic-gate 			 *		<subject> linked to %s
40777c478bd9Sstevel@tonic-gate 			 */
40787c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
4079*da6c28aaSamw 			if (xattrp != NULL) {
40807c478bd9Sstevel@tonic-gate 				(void) printf(
40817c478bd9Sstevel@tonic-gate 				    gettext(" linked to attribute %s"),
40827c478bd9Sstevel@tonic-gate 				    xattr_linkp->h_names +
40837c478bd9Sstevel@tonic-gate 				    strlen(xattr_linkp->h_names) + 1);
40847c478bd9Sstevel@tonic-gate 			} else {
40857c478bd9Sstevel@tonic-gate 				(void) printf(
40867c478bd9Sstevel@tonic-gate 				    gettext(" linked to %s"), templink);
40877c478bd9Sstevel@tonic-gate 			}
40887c478bd9Sstevel@tonic-gate #else
40897c478bd9Sstevel@tonic-gate 				(void) printf(
40907c478bd9Sstevel@tonic-gate 				    gettext(" linked to %s"), templink);
40917c478bd9Sstevel@tonic-gate 
40927c478bd9Sstevel@tonic-gate #endif
40937c478bd9Sstevel@tonic-gate 		}
40947c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '2')
40957c478bd9Sstevel@tonic-gate 			(void) printf(gettext(
40967c478bd9Sstevel@tonic-gate 			/*
40977c478bd9Sstevel@tonic-gate 			 * TRANSLATION_NOTE
40987c478bd9Sstevel@tonic-gate 			 *	Subject is omitted here.
40997c478bd9Sstevel@tonic-gate 			 *	Translate this as if
41007c478bd9Sstevel@tonic-gate 			 *		<subject> symbolic link to %s
41017c478bd9Sstevel@tonic-gate 			 */
41027c478bd9Sstevel@tonic-gate 			" symbolic link to %s"), templink);
41037c478bd9Sstevel@tonic-gate 		(void) printf("\n");
41047c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
4105*da6c28aaSamw 		if (xattrp != NULL) {
41067c478bd9Sstevel@tonic-gate 			free(xattrhead);
41077c478bd9Sstevel@tonic-gate 			xattrp = NULL;
41087c478bd9Sstevel@tonic-gate 			xattrhead = NULL;
41097c478bd9Sstevel@tonic-gate 		}
41107c478bd9Sstevel@tonic-gate #endif
41117c478bd9Sstevel@tonic-gate 		passtape();
41127c478bd9Sstevel@tonic-gate 	}
41137c478bd9Sstevel@tonic-gate 	/*
41147c478bd9Sstevel@tonic-gate 	 * Check if the number of files tabled is different from the
41157c478bd9Sstevel@tonic-gate 	 * number of files listed on the command line
41167c478bd9Sstevel@tonic-gate 	 */
41177c478bd9Sstevel@tonic-gate 	if (fcnt > tcnt) {
41187c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
41197c478bd9Sstevel@tonic-gate 		    "tar: %d file(s) not found\n"), fcnt-tcnt);
41207c478bd9Sstevel@tonic-gate 		Errflg = 1;
41217c478bd9Sstevel@tonic-gate 	}
41227c478bd9Sstevel@tonic-gate }
41237c478bd9Sstevel@tonic-gate 
41247c478bd9Sstevel@tonic-gate static void
41257c478bd9Sstevel@tonic-gate putempty(blkcnt_t n)
41267c478bd9Sstevel@tonic-gate {
41277c478bd9Sstevel@tonic-gate 	char buf[TBLOCK];
41287c478bd9Sstevel@tonic-gate 	char *cp;
41297c478bd9Sstevel@tonic-gate 
41307c478bd9Sstevel@tonic-gate 	for (cp = buf; cp < &buf[TBLOCK]; )
41317c478bd9Sstevel@tonic-gate 		*cp++ = '\0';
41327c478bd9Sstevel@tonic-gate 	while (n-- > 0)
41337c478bd9Sstevel@tonic-gate 		(void) writetbuf(buf, 1);
41347c478bd9Sstevel@tonic-gate }
41357c478bd9Sstevel@tonic-gate 
41367c478bd9Sstevel@tonic-gate static	ushort_t	Ftype = S_IFMT;
41377c478bd9Sstevel@tonic-gate 
41387c478bd9Sstevel@tonic-gate static	void
41397c478bd9Sstevel@tonic-gate verbose(struct stat *st, char aclchar)
41407c478bd9Sstevel@tonic-gate {
41417c478bd9Sstevel@tonic-gate 	int i, j, temp;
41427c478bd9Sstevel@tonic-gate 	mode_t mode;
41437c478bd9Sstevel@tonic-gate 	char modestr[12];
41447c478bd9Sstevel@tonic-gate 
41457c478bd9Sstevel@tonic-gate 	for (i = 0; i < 11; i++)
41467c478bd9Sstevel@tonic-gate 		modestr[i] = '-';
41477c478bd9Sstevel@tonic-gate 	modestr[i] = '\0';
41487c478bd9Sstevel@tonic-gate 
41497c478bd9Sstevel@tonic-gate 	/* a '+' sign is printed if there is ACL */
41507c478bd9Sstevel@tonic-gate 	modestr[i-1] = aclchar;
41517c478bd9Sstevel@tonic-gate 
41527c478bd9Sstevel@tonic-gate 	mode = st->st_mode;
41537c478bd9Sstevel@tonic-gate 	for (i = 0; i < 3; i++) {
41547c478bd9Sstevel@tonic-gate 		temp = (mode >> (6 - (i * 3)));
41557c478bd9Sstevel@tonic-gate 		j = (i * 3) + 1;
41567c478bd9Sstevel@tonic-gate 		if (S_IROTH & temp)
41577c478bd9Sstevel@tonic-gate 			modestr[j] = 'r';
41587c478bd9Sstevel@tonic-gate 		if (S_IWOTH & temp)
41597c478bd9Sstevel@tonic-gate 			modestr[j + 1] = 'w';
41607c478bd9Sstevel@tonic-gate 		if (S_IXOTH & temp)
41617c478bd9Sstevel@tonic-gate 			modestr[j + 2] = 'x';
41627c478bd9Sstevel@tonic-gate 	}
41637c478bd9Sstevel@tonic-gate 	temp = st->st_mode & Ftype;
41647c478bd9Sstevel@tonic-gate 	switch (temp) {
41657c478bd9Sstevel@tonic-gate 	case (S_IFIFO):
41667c478bd9Sstevel@tonic-gate 		modestr[0] = 'p';
41677c478bd9Sstevel@tonic-gate 		break;
41687c478bd9Sstevel@tonic-gate 	case (S_IFCHR):
41697c478bd9Sstevel@tonic-gate 		modestr[0] = 'c';
41707c478bd9Sstevel@tonic-gate 		break;
41717c478bd9Sstevel@tonic-gate 	case (S_IFDIR):
41727c478bd9Sstevel@tonic-gate 		modestr[0] = 'd';
41737c478bd9Sstevel@tonic-gate 		break;
41747c478bd9Sstevel@tonic-gate 	case (S_IFBLK):
41757c478bd9Sstevel@tonic-gate 		modestr[0] = 'b';
41767c478bd9Sstevel@tonic-gate 		break;
41777c478bd9Sstevel@tonic-gate 	case (S_IFREG): /* was initialized to '-' */
41787c478bd9Sstevel@tonic-gate 		break;
41797c478bd9Sstevel@tonic-gate 	case (S_IFLNK):
41807c478bd9Sstevel@tonic-gate 		modestr[0] = 'l';
41817c478bd9Sstevel@tonic-gate 		break;
41827c478bd9Sstevel@tonic-gate 	default:
41837c478bd9Sstevel@tonic-gate 		/* This field may be zero in old archives. */
41847c478bd9Sstevel@tonic-gate 		if (is_posix && dblock.dbuf.typeflag != '1') {
41857c478bd9Sstevel@tonic-gate 			/*
41867c478bd9Sstevel@tonic-gate 			 * For POSIX compliant archives, the mode field
41877c478bd9Sstevel@tonic-gate 			 * consists of 12 bits, ie:  the file type bits
41887c478bd9Sstevel@tonic-gate 			 * are not stored in dblock.dbuf.mode.
41897c478bd9Sstevel@tonic-gate 			 * For files other than hard links, getdir() sets
41907c478bd9Sstevel@tonic-gate 			 * the file type bits in the st_mode field of the
41917c478bd9Sstevel@tonic-gate 			 * stat structure based upon dblock.dbuf.typeflag.
41927c478bd9Sstevel@tonic-gate 			 */
41937c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
41947c478bd9Sstevel@tonic-gate 			    "tar: impossible file type"));
41957c478bd9Sstevel@tonic-gate 		}
41967c478bd9Sstevel@tonic-gate 	}
41977c478bd9Sstevel@tonic-gate 
41987c478bd9Sstevel@tonic-gate 	if ((S_ISUID & Gen.g_mode) == S_ISUID)
41997c478bd9Sstevel@tonic-gate 		modestr[3] = 's';
42007c478bd9Sstevel@tonic-gate 	if ((S_ISVTX & Gen.g_mode) == S_ISVTX)
42017c478bd9Sstevel@tonic-gate 		modestr[9] = 't';
42027c478bd9Sstevel@tonic-gate 	if ((S_ISGID & Gen.g_mode) == S_ISGID && modestr[6] == 'x')
42037c478bd9Sstevel@tonic-gate 		modestr[6] = 's';
42047c478bd9Sstevel@tonic-gate 	else if ((S_ENFMT & Gen.g_mode) == S_ENFMT && modestr[6] != 'x')
42057c478bd9Sstevel@tonic-gate 		modestr[6] = 'l';
42067c478bd9Sstevel@tonic-gate 	(void) fprintf(vfile, "%s", modestr);
42077c478bd9Sstevel@tonic-gate }
42087c478bd9Sstevel@tonic-gate 
42097c478bd9Sstevel@tonic-gate static void
42107c478bd9Sstevel@tonic-gate longt(struct stat *st, char aclchar)
42117c478bd9Sstevel@tonic-gate {
42127c478bd9Sstevel@tonic-gate 	char fileDate[30];
42137c478bd9Sstevel@tonic-gate 	struct tm *tm;
42147c478bd9Sstevel@tonic-gate 
42157c478bd9Sstevel@tonic-gate 	verbose(st, aclchar);
42167c478bd9Sstevel@tonic-gate 	(void) fprintf(vfile, "%3ld/%-3ld", st->st_uid, st->st_gid);
42177c478bd9Sstevel@tonic-gate 
42187c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.typeflag == '2') {
42197c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_LINKPATH)
42207c478bd9Sstevel@tonic-gate 			st->st_size = (off_t)strlen(Xtarhdr.x_linkpath);
42217c478bd9Sstevel@tonic-gate 		else
42227c478bd9Sstevel@tonic-gate 			st->st_size = (off_t)(memchr(dblock.dbuf.linkname,
42237c478bd9Sstevel@tonic-gate 			    '\0', NAMSIZ) ?
42247c478bd9Sstevel@tonic-gate 			    (strlen(dblock.dbuf.linkname)) : (NAMSIZ));
42257c478bd9Sstevel@tonic-gate 	}
42267c478bd9Sstevel@tonic-gate 	(void) fprintf(vfile, " %6" FMT_off_t, st->st_size);
42277c478bd9Sstevel@tonic-gate 
42287c478bd9Sstevel@tonic-gate 	tm = localtime(&(st->st_mtime));
42297c478bd9Sstevel@tonic-gate 	(void) strftime(fileDate, sizeof (fileDate),
42307c478bd9Sstevel@tonic-gate 	    dcgettext((const char *)0, "%b %e %R %Y", LC_TIME), tm);
42317c478bd9Sstevel@tonic-gate 	(void) fprintf(vfile, " %s ", fileDate);
42327c478bd9Sstevel@tonic-gate }
42337c478bd9Sstevel@tonic-gate 
42347c478bd9Sstevel@tonic-gate 
42357c478bd9Sstevel@tonic-gate /*
42367c478bd9Sstevel@tonic-gate  *  checkdir - Attempt to ensure that the path represented in name
42377c478bd9Sstevel@tonic-gate  *             exists, and return 1 if this is true and name itself is a
42387c478bd9Sstevel@tonic-gate  *             directory.
42397c478bd9Sstevel@tonic-gate  *             Return 0 if this path cannot be created or if name is not
42407c478bd9Sstevel@tonic-gate  *             a directory.
42417c478bd9Sstevel@tonic-gate  */
42427c478bd9Sstevel@tonic-gate 
42437c478bd9Sstevel@tonic-gate static int
42447c478bd9Sstevel@tonic-gate checkdir(char *name)
42457c478bd9Sstevel@tonic-gate {
42467c478bd9Sstevel@tonic-gate 	char lastChar;		   /* the last character in name */
42477c478bd9Sstevel@tonic-gate 	char *cp;		   /* scratch pointer into name */
42487c478bd9Sstevel@tonic-gate 	char *firstSlash = NULL;   /* first slash in name */
42497c478bd9Sstevel@tonic-gate 	char *lastSlash = NULL;	   /* last slash in name */
42507c478bd9Sstevel@tonic-gate 	int  nameLen;		   /* length of name */
42517c478bd9Sstevel@tonic-gate 	int  trailingSlash;	   /* true if name ends in slash */
42527c478bd9Sstevel@tonic-gate 	int  leadingSlash;	   /* true if name begins with slash */
42537c478bd9Sstevel@tonic-gate 	int  markedDir;		   /* true if name denotes a directory */
42547c478bd9Sstevel@tonic-gate 	int  success;		   /* status of makeDir call */
42557c478bd9Sstevel@tonic-gate 
42567c478bd9Sstevel@tonic-gate 
42577c478bd9Sstevel@tonic-gate 	/*
42587c478bd9Sstevel@tonic-gate 	 *  Scan through the name, and locate first and last slashes.
42597c478bd9Sstevel@tonic-gate 	 */
42607c478bd9Sstevel@tonic-gate 
42617c478bd9Sstevel@tonic-gate 	for (cp = name; *cp; cp++) {
42627c478bd9Sstevel@tonic-gate 		if (*cp == '/') {
42637c478bd9Sstevel@tonic-gate 			if (! firstSlash) {
42647c478bd9Sstevel@tonic-gate 				firstSlash = cp;
42657c478bd9Sstevel@tonic-gate 			}
42667c478bd9Sstevel@tonic-gate 			lastSlash = cp;
42677c478bd9Sstevel@tonic-gate 		}
42687c478bd9Sstevel@tonic-gate 	}
42697c478bd9Sstevel@tonic-gate 
42707c478bd9Sstevel@tonic-gate 	/*
42717c478bd9Sstevel@tonic-gate 	 *  Determine what you can from the proceeds of the scan.
42727c478bd9Sstevel@tonic-gate 	 */
42737c478bd9Sstevel@tonic-gate 
42747c478bd9Sstevel@tonic-gate 	lastChar	= *(cp - 1);
42757c478bd9Sstevel@tonic-gate 	nameLen		= (int)(cp - name);
42767c478bd9Sstevel@tonic-gate 	trailingSlash	= (lastChar == '/');
42777c478bd9Sstevel@tonic-gate 	leadingSlash	= (*name == '/');
42787c478bd9Sstevel@tonic-gate 	markedDir	= (dblock.dbuf.typeflag == '5' || trailingSlash);
42797c478bd9Sstevel@tonic-gate 
42807c478bd9Sstevel@tonic-gate 	if (! lastSlash && ! markedDir) {
42817c478bd9Sstevel@tonic-gate 		/*
42827c478bd9Sstevel@tonic-gate 		 *  The named file does not have any subdrectory
42837c478bd9Sstevel@tonic-gate 		 *  structure; just bail out.
42847c478bd9Sstevel@tonic-gate 		 */
42857c478bd9Sstevel@tonic-gate 
42867c478bd9Sstevel@tonic-gate 		return (0);
42877c478bd9Sstevel@tonic-gate 	}
42887c478bd9Sstevel@tonic-gate 
42897c478bd9Sstevel@tonic-gate 	/*
42907c478bd9Sstevel@tonic-gate 	 *  Make sure that name doesn`t end with slash for the loop.
42917c478bd9Sstevel@tonic-gate 	 *  This ensures that the makeDir attempt after the loop is
42927c478bd9Sstevel@tonic-gate 	 *  meaningful.
42937c478bd9Sstevel@tonic-gate 	 */
42947c478bd9Sstevel@tonic-gate 
42957c478bd9Sstevel@tonic-gate 	if (trailingSlash) {
42967c478bd9Sstevel@tonic-gate 		name[nameLen-1] = '\0';
42977c478bd9Sstevel@tonic-gate 	}
42987c478bd9Sstevel@tonic-gate 
42997c478bd9Sstevel@tonic-gate 	/*
43007c478bd9Sstevel@tonic-gate 	 *  Make the path one component at a time.
43017c478bd9Sstevel@tonic-gate 	 */
43027c478bd9Sstevel@tonic-gate 
43037c478bd9Sstevel@tonic-gate 	for (cp = strchr(leadingSlash ? name+1 : name, '/');
43047c478bd9Sstevel@tonic-gate 	    cp;
43057c478bd9Sstevel@tonic-gate 	    cp = strchr(cp+1, '/')) {
43067c478bd9Sstevel@tonic-gate 		*cp = '\0';
43077c478bd9Sstevel@tonic-gate 		success = makeDir(name);
43087c478bd9Sstevel@tonic-gate 		*cp = '/';
43097c478bd9Sstevel@tonic-gate 
43107c478bd9Sstevel@tonic-gate 		if (!success) {
43117c478bd9Sstevel@tonic-gate 			name[nameLen-1] = lastChar;
43127c478bd9Sstevel@tonic-gate 			return (0);
43137c478bd9Sstevel@tonic-gate 		}
43147c478bd9Sstevel@tonic-gate 	}
43157c478bd9Sstevel@tonic-gate 
43167c478bd9Sstevel@tonic-gate 	/*
43177c478bd9Sstevel@tonic-gate 	 *  This makes the last component of the name, if it is a
43187c478bd9Sstevel@tonic-gate 	 *  directory.
43197c478bd9Sstevel@tonic-gate 	 */
43207c478bd9Sstevel@tonic-gate 
43217c478bd9Sstevel@tonic-gate 	if (markedDir) {
43227c478bd9Sstevel@tonic-gate 		if (! makeDir(name)) {
43237c478bd9Sstevel@tonic-gate 			name[nameLen-1] = lastChar;
43247c478bd9Sstevel@tonic-gate 			return (0);
43257c478bd9Sstevel@tonic-gate 		}
43267c478bd9Sstevel@tonic-gate 	}
43277c478bd9Sstevel@tonic-gate 
43287c478bd9Sstevel@tonic-gate 	name[nameLen-1] = (lastChar == '/') ? '\0' : lastChar;
43297c478bd9Sstevel@tonic-gate 	return (markedDir);
43307c478bd9Sstevel@tonic-gate }
43317c478bd9Sstevel@tonic-gate 
43327c478bd9Sstevel@tonic-gate /*
43337c478bd9Sstevel@tonic-gate  * resugname - Restore the user name and group name.  Search the NIS
43347c478bd9Sstevel@tonic-gate  *             before using the uid and gid.
43357c478bd9Sstevel@tonic-gate  *             (It is presumed that an archive entry cannot be
43367c478bd9Sstevel@tonic-gate  *	       simultaneously a symlink and some other type.)
43377c478bd9Sstevel@tonic-gate  */
43387c478bd9Sstevel@tonic-gate 
43397c478bd9Sstevel@tonic-gate static void
43407c478bd9Sstevel@tonic-gate resugname(int dirfd, 	/* dir fd file resides in */
43417c478bd9Sstevel@tonic-gate 	char *name,	/* name of the file to be modified */
43427c478bd9Sstevel@tonic-gate 	int symflag)	/* true if file is a symbolic link */
43437c478bd9Sstevel@tonic-gate {
43447c478bd9Sstevel@tonic-gate 	uid_t duid;
43457c478bd9Sstevel@tonic-gate 	gid_t dgid;
43467c478bd9Sstevel@tonic-gate 	struct stat *sp = &stbuf;
43477c478bd9Sstevel@tonic-gate 	char	*u_g_name;
43487c478bd9Sstevel@tonic-gate 
43497c478bd9Sstevel@tonic-gate 	if (checkflag == 1) { /* Extended tar format and euid == 0 */
43507c478bd9Sstevel@tonic-gate 
43517c478bd9Sstevel@tonic-gate 		/*
43527c478bd9Sstevel@tonic-gate 		 * Try and extract the intended uid and gid from the name
43537c478bd9Sstevel@tonic-gate 		 * service before believing the uid and gid in the header.
43547c478bd9Sstevel@tonic-gate 		 *
43557c478bd9Sstevel@tonic-gate 		 * In the case where we archived a setuid or setgid file
43567c478bd9Sstevel@tonic-gate 		 * owned by someone with a large uid, then it will
43577c478bd9Sstevel@tonic-gate 		 * have made it into the archive with a uid of nobody.  If
43587c478bd9Sstevel@tonic-gate 		 * the corresponding username doesn't appear to exist, then we
43597c478bd9Sstevel@tonic-gate 		 * want to make sure it *doesn't* end up as setuid nobody!
43607c478bd9Sstevel@tonic-gate 		 *
43617c478bd9Sstevel@tonic-gate 		 * Our caller will print an error message about the fact
43627c478bd9Sstevel@tonic-gate 		 * that the restore didn't work out quite right ..
43637c478bd9Sstevel@tonic-gate 		 */
43647c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_UNAME)
43657c478bd9Sstevel@tonic-gate 			u_g_name = Xtarhdr.x_uname;
43667c478bd9Sstevel@tonic-gate 		else
43677c478bd9Sstevel@tonic-gate 			u_g_name = dblock.dbuf.uname;
43687c478bd9Sstevel@tonic-gate 		if ((duid = getuidbyname(u_g_name)) == -1) {
43697c478bd9Sstevel@tonic-gate 			if (S_ISREG(sp->st_mode) && sp->st_uid == UID_NOBODY &&
43707c478bd9Sstevel@tonic-gate 			    (sp->st_mode & S_ISUID) == S_ISUID)
43717c478bd9Sstevel@tonic-gate 				(void) chmod(name,
43727c478bd9Sstevel@tonic-gate 				    MODEMASK & sp->st_mode & ~S_ISUID);
43737c478bd9Sstevel@tonic-gate 			duid = sp->st_uid;
43747c478bd9Sstevel@tonic-gate 		}
43757c478bd9Sstevel@tonic-gate 
43767c478bd9Sstevel@tonic-gate 		/* (Ditto for gids) */
43777c478bd9Sstevel@tonic-gate 
43787c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_GNAME)
43797c478bd9Sstevel@tonic-gate 			u_g_name = Xtarhdr.x_gname;
43807c478bd9Sstevel@tonic-gate 		else
43817c478bd9Sstevel@tonic-gate 			u_g_name = dblock.dbuf.gname;
43827c478bd9Sstevel@tonic-gate 		if ((dgid = getgidbyname(u_g_name)) == -1) {
43837c478bd9Sstevel@tonic-gate 			if (S_ISREG(sp->st_mode) && sp->st_gid == GID_NOBODY &&
43847c478bd9Sstevel@tonic-gate 			    (sp->st_mode & S_ISGID) == S_ISGID)
43857c478bd9Sstevel@tonic-gate 				(void) chmod(name,
43867c478bd9Sstevel@tonic-gate 				    MODEMASK & sp->st_mode & ~S_ISGID);
43877c478bd9Sstevel@tonic-gate 			dgid = sp->st_gid;
43887c478bd9Sstevel@tonic-gate 		}
43897c478bd9Sstevel@tonic-gate 	} else if (checkflag == 2) { /* tar format and euid == 0 */
43907c478bd9Sstevel@tonic-gate 		duid = sp->st_uid;
43917c478bd9Sstevel@tonic-gate 		dgid = sp->st_gid;
43927c478bd9Sstevel@tonic-gate 	}
43937c478bd9Sstevel@tonic-gate 	if ((checkflag == 1) || (checkflag == 2))
43947c478bd9Sstevel@tonic-gate 		(void) fchownat(dirfd, name, duid, dgid, symflag);
43957c478bd9Sstevel@tonic-gate }
43967c478bd9Sstevel@tonic-gate 
43977c478bd9Sstevel@tonic-gate /*ARGSUSED*/
43987c478bd9Sstevel@tonic-gate static void
43997c478bd9Sstevel@tonic-gate onintr(int sig)
44007c478bd9Sstevel@tonic-gate {
44017c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, SIG_IGN);
44027c478bd9Sstevel@tonic-gate 	term++;
44037c478bd9Sstevel@tonic-gate }
44047c478bd9Sstevel@tonic-gate 
44057c478bd9Sstevel@tonic-gate /*ARGSUSED*/
44067c478bd9Sstevel@tonic-gate static void
44077c478bd9Sstevel@tonic-gate onquit(int sig)
44087c478bd9Sstevel@tonic-gate {
44097c478bd9Sstevel@tonic-gate 	(void) signal(SIGQUIT, SIG_IGN);
44107c478bd9Sstevel@tonic-gate 	term++;
44117c478bd9Sstevel@tonic-gate }
44127c478bd9Sstevel@tonic-gate 
44137c478bd9Sstevel@tonic-gate /*ARGSUSED*/
44147c478bd9Sstevel@tonic-gate static void
44157c478bd9Sstevel@tonic-gate onhup(int sig)
44167c478bd9Sstevel@tonic-gate {
44177c478bd9Sstevel@tonic-gate 	(void) signal(SIGHUP, SIG_IGN);
44187c478bd9Sstevel@tonic-gate 	term++;
44197c478bd9Sstevel@tonic-gate }
44207c478bd9Sstevel@tonic-gate 
44217c478bd9Sstevel@tonic-gate static void
44227c478bd9Sstevel@tonic-gate tomodes(struct stat *sp)
44237c478bd9Sstevel@tonic-gate {
44247c478bd9Sstevel@tonic-gate 	uid_t uid;
44257c478bd9Sstevel@tonic-gate 	gid_t gid;
44267c478bd9Sstevel@tonic-gate 
44277c478bd9Sstevel@tonic-gate 	bzero(dblock.dummy, TBLOCK);
44287c478bd9Sstevel@tonic-gate 
44297c478bd9Sstevel@tonic-gate 	/*
44307c478bd9Sstevel@tonic-gate 	 * If the uid or gid is too large, we can't put it into
44317c478bd9Sstevel@tonic-gate 	 * the archive.  We could fail to put anything in the
44327c478bd9Sstevel@tonic-gate 	 * archive at all .. but most of the time the name service
44337c478bd9Sstevel@tonic-gate 	 * will save the day when we do a lookup at restore time.
44347c478bd9Sstevel@tonic-gate 	 *
44357c478bd9Sstevel@tonic-gate 	 * Instead we choose a "safe" uid and gid, and fix up whether
44367c478bd9Sstevel@tonic-gate 	 * or not the setuid and setgid bits are left set to extraction
44377c478bd9Sstevel@tonic-gate 	 * time.
44387c478bd9Sstevel@tonic-gate 	 */
44397c478bd9Sstevel@tonic-gate 	if (Eflag) {
44407c478bd9Sstevel@tonic-gate 		if ((ulong_t)(uid = sp->st_uid) > (ulong_t)OCTAL7CHAR) {
44417c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_UID;
44427c478bd9Sstevel@tonic-gate 			Xtarhdr.x_uid = uid;
44437c478bd9Sstevel@tonic-gate 		}
44447c478bd9Sstevel@tonic-gate 		if ((ulong_t)(gid = sp->st_gid) > (ulong_t)OCTAL7CHAR) {
44457c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_GID;
44467c478bd9Sstevel@tonic-gate 			Xtarhdr.x_gid = gid;
44477c478bd9Sstevel@tonic-gate 		}
44487c478bd9Sstevel@tonic-gate 		if (sp->st_size > TAR_OFFSET_MAX) {
44497c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_SIZE;
44507c478bd9Sstevel@tonic-gate 			Xtarhdr.x_filesz = sp->st_size;
44517c478bd9Sstevel@tonic-gate 			(void) sprintf(dblock.dbuf.size, "%011" FMT_off_t_o,
44527c478bd9Sstevel@tonic-gate 			    (off_t)0);
44537c478bd9Sstevel@tonic-gate 		} else
44547c478bd9Sstevel@tonic-gate 			(void) sprintf(dblock.dbuf.size, "%011" FMT_off_t_o,
44557c478bd9Sstevel@tonic-gate 			    sp->st_size);
44567c478bd9Sstevel@tonic-gate 	} else {
44577c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.size, "%011" FMT_off_t_o,
44587c478bd9Sstevel@tonic-gate 		    sp->st_size);
44597c478bd9Sstevel@tonic-gate 	}
44607c478bd9Sstevel@tonic-gate 	if ((ulong_t)(uid = sp->st_uid) > (ulong_t)OCTAL7CHAR)
44617c478bd9Sstevel@tonic-gate 		uid = UID_NOBODY;
44627c478bd9Sstevel@tonic-gate 	if ((ulong_t)(gid = sp->st_gid) > (ulong_t)OCTAL7CHAR)
44637c478bd9Sstevel@tonic-gate 		gid = GID_NOBODY;
44647c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.gid, "%07lo", gid);
44657c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.uid, "%07lo", uid);
44667c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.mode, "%07lo", sp->st_mode & POSIXMODES);
44677c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.mtime, "%011lo", sp->st_mtime);
44687c478bd9Sstevel@tonic-gate }
44697c478bd9Sstevel@tonic-gate 
44707c478bd9Sstevel@tonic-gate static	int
44717c478bd9Sstevel@tonic-gate #ifdef	EUC
44727c478bd9Sstevel@tonic-gate /*
44737c478bd9Sstevel@tonic-gate  * Warning:  the result of this function depends whether 'char' is a
44747c478bd9Sstevel@tonic-gate  * signed or unsigned data type.  This a source of potential
44757c478bd9Sstevel@tonic-gate  * non-portability among heterogeneous systems.  It is retained here
44767c478bd9Sstevel@tonic-gate  * for backward compatibility.
44777c478bd9Sstevel@tonic-gate  */
44787c478bd9Sstevel@tonic-gate checksum_signed(union hblock *dblockp)
44797c478bd9Sstevel@tonic-gate #else
44807c478bd9Sstevel@tonic-gate checksum(union hblock *dblockp)
44817c478bd9Sstevel@tonic-gate #endif	/* EUC */
44827c478bd9Sstevel@tonic-gate {
44837c478bd9Sstevel@tonic-gate 	int i;
44847c478bd9Sstevel@tonic-gate 	char *cp;
44857c478bd9Sstevel@tonic-gate 
44867c478bd9Sstevel@tonic-gate 	for (cp = dblockp->dbuf.chksum;
44877c478bd9Sstevel@tonic-gate 	    cp < &dblockp->dbuf.chksum[sizeof (dblockp->dbuf.chksum)]; cp++)
44887c478bd9Sstevel@tonic-gate 		*cp = ' ';
44897c478bd9Sstevel@tonic-gate 	i = 0;
44907c478bd9Sstevel@tonic-gate 	for (cp = dblockp->dummy; cp < &(dblockp->dummy[TBLOCK]); cp++)
44917c478bd9Sstevel@tonic-gate 		i += *cp;
44927c478bd9Sstevel@tonic-gate 	return (i);
44937c478bd9Sstevel@tonic-gate }
44947c478bd9Sstevel@tonic-gate 
44957c478bd9Sstevel@tonic-gate #ifdef	EUC
44967c478bd9Sstevel@tonic-gate /*
44977c478bd9Sstevel@tonic-gate  * Generate unsigned checksum, regardless of what C compiler is
44987c478bd9Sstevel@tonic-gate  * used.  Survives in the face of arbitrary 8-bit clean filenames,
44997c478bd9Sstevel@tonic-gate  * e.g., internationalized filenames.
45007c478bd9Sstevel@tonic-gate  */
45017c478bd9Sstevel@tonic-gate static int
45027c478bd9Sstevel@tonic-gate checksum(union hblock *dblockp)
45037c478bd9Sstevel@tonic-gate {
45047c478bd9Sstevel@tonic-gate 	unsigned i;
45057c478bd9Sstevel@tonic-gate 	unsigned char *cp;
45067c478bd9Sstevel@tonic-gate 
45077c478bd9Sstevel@tonic-gate 	for (cp = (unsigned char *) dblockp->dbuf.chksum;
45087c478bd9Sstevel@tonic-gate 	    cp < (unsigned char *)
45097c478bd9Sstevel@tonic-gate 	    &(dblockp->dbuf.chksum[sizeof (dblockp->dbuf.chksum)]); cp++)
45107c478bd9Sstevel@tonic-gate 		*cp = ' ';
45117c478bd9Sstevel@tonic-gate 	i = 0;
45127c478bd9Sstevel@tonic-gate 	for (cp = (unsigned char *) dblockp->dummy;
45137c478bd9Sstevel@tonic-gate 	    cp < (unsigned char *) &(dblockp->dummy[TBLOCK]); cp++)
45147c478bd9Sstevel@tonic-gate 		i += *cp;
45157c478bd9Sstevel@tonic-gate 
45167c478bd9Sstevel@tonic-gate 	return (i);
45177c478bd9Sstevel@tonic-gate }
45187c478bd9Sstevel@tonic-gate #endif	/* EUC */
45197c478bd9Sstevel@tonic-gate 
45207c478bd9Sstevel@tonic-gate /*
45217c478bd9Sstevel@tonic-gate  * If the w flag is set, output the action to be taken and the name of the
45227c478bd9Sstevel@tonic-gate  * file.  Perform the action if the user response is affirmative.
45237c478bd9Sstevel@tonic-gate  */
45247c478bd9Sstevel@tonic-gate 
45257c478bd9Sstevel@tonic-gate static int
45267c478bd9Sstevel@tonic-gate checkw(char c, char *name)
45277c478bd9Sstevel@tonic-gate {
45287c478bd9Sstevel@tonic-gate 	if (wflag) {
45297c478bd9Sstevel@tonic-gate 		(void) fprintf(vfile, "%c ", c);
45307c478bd9Sstevel@tonic-gate 		if (vflag)
45317c478bd9Sstevel@tonic-gate 			longt(&stbuf, ' ');	/* do we have acl info here */
45327c478bd9Sstevel@tonic-gate 		(void) fprintf(vfile, "%s: ", name);
45333d63ea05Sas145665 		if (yes() == 1) {
45347c478bd9Sstevel@tonic-gate 			return (1);
45357c478bd9Sstevel@tonic-gate 		}
45367c478bd9Sstevel@tonic-gate 		return (0);
45377c478bd9Sstevel@tonic-gate 	}
45387c478bd9Sstevel@tonic-gate 	return (1);
45397c478bd9Sstevel@tonic-gate }
45407c478bd9Sstevel@tonic-gate 
45417c478bd9Sstevel@tonic-gate /*
45427c478bd9Sstevel@tonic-gate  * When the F flag is set, exclude RCS and SCCS directories.  If F is set
45437c478bd9Sstevel@tonic-gate  * twice, also exclude .o files, and files names errs, core, and a.out.
45447c478bd9Sstevel@tonic-gate  */
45457c478bd9Sstevel@tonic-gate 
45467c478bd9Sstevel@tonic-gate static int
45477c478bd9Sstevel@tonic-gate checkf(char *name, int mode, int howmuch)
45487c478bd9Sstevel@tonic-gate {
45497c478bd9Sstevel@tonic-gate 	int l;
45507c478bd9Sstevel@tonic-gate 
45517c478bd9Sstevel@tonic-gate 	if ((mode & S_IFMT) == S_IFDIR) {
45527c478bd9Sstevel@tonic-gate 		if ((strcmp(name, "SCCS") == 0) || (strcmp(name, "RCS") == 0))
45537c478bd9Sstevel@tonic-gate 			return (0);
45547c478bd9Sstevel@tonic-gate 		return (1);
45557c478bd9Sstevel@tonic-gate 	}
45567c478bd9Sstevel@tonic-gate 	if ((l = (int)strlen(name)) < 3)
45577c478bd9Sstevel@tonic-gate 		return (1);
45587c478bd9Sstevel@tonic-gate 	if (howmuch > 1 && name[l-2] == '.' && name[l-1] == 'o')
45597c478bd9Sstevel@tonic-gate 		return (0);
45607c478bd9Sstevel@tonic-gate 	if (howmuch > 1) {
45617c478bd9Sstevel@tonic-gate 		if (strcmp(name, "core") == 0 || strcmp(name, "errs") == 0 ||
45627c478bd9Sstevel@tonic-gate 		    strcmp(name, "a.out") == 0)
45637c478bd9Sstevel@tonic-gate 			return (0);
45647c478bd9Sstevel@tonic-gate 	}
45657c478bd9Sstevel@tonic-gate 
45667c478bd9Sstevel@tonic-gate 	/* SHOULD CHECK IF IT IS EXECUTABLE */
45677c478bd9Sstevel@tonic-gate 	return (1);
45687c478bd9Sstevel@tonic-gate }
45697c478bd9Sstevel@tonic-gate 
45707c478bd9Sstevel@tonic-gate static int
45717c478bd9Sstevel@tonic-gate response(void)
45727c478bd9Sstevel@tonic-gate {
45737c478bd9Sstevel@tonic-gate 	int c;
45747c478bd9Sstevel@tonic-gate 
45757c478bd9Sstevel@tonic-gate 	c = getchar();
45767c478bd9Sstevel@tonic-gate 	if (c != '\n')
4577eace40a5Sceastha 		while (getchar() != '\n')
4578eace40a5Sceastha 			;
45797c478bd9Sstevel@tonic-gate 	else c = 'n';
45807c478bd9Sstevel@tonic-gate 	return ((c >= 'A' && c <= 'Z') ? c + ('a'-'A') : c);
45817c478bd9Sstevel@tonic-gate }
45827c478bd9Sstevel@tonic-gate 
45837c478bd9Sstevel@tonic-gate /* Has file been modified since being put into archive? If so, return > 0. */
45847c478bd9Sstevel@tonic-gate 
45857c478bd9Sstevel@tonic-gate static int
45867c478bd9Sstevel@tonic-gate checkupdate(char *arg)
45877c478bd9Sstevel@tonic-gate {
45887c478bd9Sstevel@tonic-gate 	char name[PATH_MAX+1];
45897c478bd9Sstevel@tonic-gate 	time_t	mtime;
45907c478bd9Sstevel@tonic-gate 	long nsecs;
45917c478bd9Sstevel@tonic-gate 	off_t seekp;
45927c478bd9Sstevel@tonic-gate 	static off_t	lookup(char *);
45937c478bd9Sstevel@tonic-gate 
45947c478bd9Sstevel@tonic-gate 	rewind(tfile);
45957c478bd9Sstevel@tonic-gate 	if ((seekp = lookup(arg)) < 0)
45967c478bd9Sstevel@tonic-gate 		return (1);
45977c478bd9Sstevel@tonic-gate 	(void) fseek(tfile, seekp, 0);
45987c478bd9Sstevel@tonic-gate 	(void) fscanf(tfile, "%s %ld.%ld", name, &mtime, &nsecs);
45997c478bd9Sstevel@tonic-gate 
46007c478bd9Sstevel@tonic-gate 	/*
46017c478bd9Sstevel@tonic-gate 	 * Unless nanoseconds were stored in the file, only use seconds for
46027c478bd9Sstevel@tonic-gate 	 * comparison of time.  Nanoseconds are stored when -E is specified.
46037c478bd9Sstevel@tonic-gate 	 */
46047c478bd9Sstevel@tonic-gate 	if (Eflag == 0)
46057c478bd9Sstevel@tonic-gate 		return (stbuf.st_mtime > mtime);
46067c478bd9Sstevel@tonic-gate 
46077c478bd9Sstevel@tonic-gate 	if ((stbuf.st_mtime < mtime) ||
46087c478bd9Sstevel@tonic-gate 	    ((stbuf.st_mtime == mtime) && (stbuf.st_mtim.tv_nsec <= nsecs)))
46097c478bd9Sstevel@tonic-gate 		return (0);
46107c478bd9Sstevel@tonic-gate 	return (1);
46117c478bd9Sstevel@tonic-gate }
46127c478bd9Sstevel@tonic-gate 
46137c478bd9Sstevel@tonic-gate 
46147c478bd9Sstevel@tonic-gate /*
46157c478bd9Sstevel@tonic-gate  *	newvol	get new floppy (or tape) volume
46167c478bd9Sstevel@tonic-gate  *
46177c478bd9Sstevel@tonic-gate  *	newvol();		resets tapepos and first to TRUE, prompts for
46187c478bd9Sstevel@tonic-gate  *				for new volume, and waits.
46197c478bd9Sstevel@tonic-gate  *	if dumping, end-of-file is written onto the tape.
46207c478bd9Sstevel@tonic-gate  */
46217c478bd9Sstevel@tonic-gate 
46227c478bd9Sstevel@tonic-gate static void
46237c478bd9Sstevel@tonic-gate newvol(void)
46247c478bd9Sstevel@tonic-gate {
46257c478bd9Sstevel@tonic-gate 	int c;
46267c478bd9Sstevel@tonic-gate 
46277c478bd9Sstevel@tonic-gate 	if (dumping) {
46287c478bd9Sstevel@tonic-gate #ifdef DEBUG
46297c478bd9Sstevel@tonic-gate 		DEBUG("newvol called with 'dumping' set\n", 0, 0);
46307c478bd9Sstevel@tonic-gate #endif
46317c478bd9Sstevel@tonic-gate 		putempty((blkcnt_t)2);	/* 2 EOT marks */
46327c478bd9Sstevel@tonic-gate 		closevol();
46337c478bd9Sstevel@tonic-gate 		flushtape();
46347c478bd9Sstevel@tonic-gate 		sync();
46357c478bd9Sstevel@tonic-gate 		tapepos = 0;
46367c478bd9Sstevel@tonic-gate 	} else
46377c478bd9Sstevel@tonic-gate 		first = TRUE;
46387c478bd9Sstevel@tonic-gate 	if (close(mt) != 0)
46397c478bd9Sstevel@tonic-gate 		vperror(2, gettext("close error"));
46407c478bd9Sstevel@tonic-gate 	mt = 0;
46417c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
46427c478bd9Sstevel@tonic-gate 	    "tar: \007please insert new volume, then press RETURN."));
46437c478bd9Sstevel@tonic-gate 	(void) fseek(stdin, (off_t)0, 2);	/* scan over read-ahead */
46447c478bd9Sstevel@tonic-gate 	while ((c = getchar()) != '\n' && ! term)
46457c478bd9Sstevel@tonic-gate 		if (c == EOF)
46467c478bd9Sstevel@tonic-gate 			done(Errflg);
46477c478bd9Sstevel@tonic-gate 	if (term)
46487c478bd9Sstevel@tonic-gate 		done(Errflg);
46497c478bd9Sstevel@tonic-gate 
46507c478bd9Sstevel@tonic-gate 	errno = 0;
46517c478bd9Sstevel@tonic-gate 
46527c478bd9Sstevel@tonic-gate 	if (strcmp(usefile, "-") == 0) {
46537c478bd9Sstevel@tonic-gate 		mt = dup(1);
46547c478bd9Sstevel@tonic-gate 	} else {
46557c478bd9Sstevel@tonic-gate 		mt = open(usefile, dumping ? update : 0);
46567c478bd9Sstevel@tonic-gate 	}
46577c478bd9Sstevel@tonic-gate 
46587c478bd9Sstevel@tonic-gate 	if (mt < 0) {
46597c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
46607c478bd9Sstevel@tonic-gate 		    "tar: cannot reopen %s (%s)\n"),
46617c478bd9Sstevel@tonic-gate 		    dumping ? gettext("output") : gettext("input"), usefile);
46627c478bd9Sstevel@tonic-gate 
46637c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "update=%d, usefile=%s, mt=%d, [%s]\n",
46647c478bd9Sstevel@tonic-gate 		    update, usefile, mt, strerror(errno));
46657c478bd9Sstevel@tonic-gate 
46667c478bd9Sstevel@tonic-gate 		done(2);
46677c478bd9Sstevel@tonic-gate 	}
46687c478bd9Sstevel@tonic-gate }
46697c478bd9Sstevel@tonic-gate 
46707c478bd9Sstevel@tonic-gate /*
46717c478bd9Sstevel@tonic-gate  * Write a trailer portion to close out the current output volume.
46727c478bd9Sstevel@tonic-gate  */
46737c478bd9Sstevel@tonic-gate 
46747c478bd9Sstevel@tonic-gate static void
46757c478bd9Sstevel@tonic-gate closevol(void)
46767c478bd9Sstevel@tonic-gate {
46777c478bd9Sstevel@tonic-gate 	if (mulvol) {
46787c478bd9Sstevel@tonic-gate 		/*
46797c478bd9Sstevel@tonic-gate 		 * blocklim does not count the 2 EOT marks;
46807c478bd9Sstevel@tonic-gate 		 * tapepos  does count the 2 EOT marks;
46817c478bd9Sstevel@tonic-gate 		 * therefore we need the +2 below.
46827c478bd9Sstevel@tonic-gate 		 */
46837c478bd9Sstevel@tonic-gate 		putempty(blocklim + (blkcnt_t)2 - tapepos);
46847c478bd9Sstevel@tonic-gate 	}
46857c478bd9Sstevel@tonic-gate }
46867c478bd9Sstevel@tonic-gate 
46877c478bd9Sstevel@tonic-gate static void
46887c478bd9Sstevel@tonic-gate done(int n)
46897c478bd9Sstevel@tonic-gate {
46907c478bd9Sstevel@tonic-gate 	(void) unlink(tname);
46917c478bd9Sstevel@tonic-gate 	if (mt > 0) {
46927c478bd9Sstevel@tonic-gate 		if ((close(mt) != 0) || (fclose(stdout) != 0)) {
46937c478bd9Sstevel@tonic-gate 			perror(gettext("tar: close error"));
46947c478bd9Sstevel@tonic-gate 			exit(2);
46957c478bd9Sstevel@tonic-gate 		}
46967c478bd9Sstevel@tonic-gate 	}
46977c478bd9Sstevel@tonic-gate 	exit(n);
46987c478bd9Sstevel@tonic-gate }
46997c478bd9Sstevel@tonic-gate 
47007c478bd9Sstevel@tonic-gate /*
47017c478bd9Sstevel@tonic-gate  * Determine if s1 is a prefix portion of s2 (or the same as s2).
47027c478bd9Sstevel@tonic-gate  */
47037c478bd9Sstevel@tonic-gate 
47047c478bd9Sstevel@tonic-gate static	int
47057c478bd9Sstevel@tonic-gate is_prefix(char *s1, char *s2)
47067c478bd9Sstevel@tonic-gate {
47077c478bd9Sstevel@tonic-gate 	while (*s1)
47087c478bd9Sstevel@tonic-gate 		if (*s1++ != *s2++)
47097c478bd9Sstevel@tonic-gate 			return (0);
47107c478bd9Sstevel@tonic-gate 	if (*s2)
47117c478bd9Sstevel@tonic-gate 		return (*s2 == '/');
47127c478bd9Sstevel@tonic-gate 	return (1);
47137c478bd9Sstevel@tonic-gate }
47147c478bd9Sstevel@tonic-gate 
47157c478bd9Sstevel@tonic-gate /*
47167c478bd9Sstevel@tonic-gate  * lookup and bsrch look through tfile entries to find a match for a name.
47177c478bd9Sstevel@tonic-gate  * The name can be up to PATH_MAX bytes.  bsrch compares what it sees between
47187c478bd9Sstevel@tonic-gate  * a pair of newline chars, so the buffer it uses must be long enough for
47197c478bd9Sstevel@tonic-gate  * two lines:  name and modification time as well as period, newline and space.
47207c478bd9Sstevel@tonic-gate  *
47217c478bd9Sstevel@tonic-gate  * A kludge was added to bsrch to take care of matching on the first entry
47227c478bd9Sstevel@tonic-gate  * in the file--there is no leading newline.  So, if we are reading from the
47237c478bd9Sstevel@tonic-gate  * start of the file, read into byte two and set the first byte to a newline.
47247c478bd9Sstevel@tonic-gate  * Otherwise, the first entry cannot be matched.
47257c478bd9Sstevel@tonic-gate  *
47267c478bd9Sstevel@tonic-gate  */
47277c478bd9Sstevel@tonic-gate 
47287c478bd9Sstevel@tonic-gate #define	N	(2 * (PATH_MAX + TIME_MAX_DIGITS + LONG_MAX_DIGITS + 3))
47297c478bd9Sstevel@tonic-gate static	off_t
47307c478bd9Sstevel@tonic-gate lookup(char *s)
47317c478bd9Sstevel@tonic-gate {
47327c478bd9Sstevel@tonic-gate 	int i;
47337c478bd9Sstevel@tonic-gate 	off_t a;
47347c478bd9Sstevel@tonic-gate 
47357c478bd9Sstevel@tonic-gate 	for (i = 0; s[i]; i++)
47367c478bd9Sstevel@tonic-gate 		if (s[i] == ' ')
47377c478bd9Sstevel@tonic-gate 			break;
47387c478bd9Sstevel@tonic-gate 	a = bsrch(s, i, low, high);
47397c478bd9Sstevel@tonic-gate 	return (a);
47407c478bd9Sstevel@tonic-gate }
47417c478bd9Sstevel@tonic-gate 
47427c478bd9Sstevel@tonic-gate static off_t
47437c478bd9Sstevel@tonic-gate bsrch(char *s, int n, off_t l, off_t h)
47447c478bd9Sstevel@tonic-gate {
47457c478bd9Sstevel@tonic-gate 	int i, j;
47467c478bd9Sstevel@tonic-gate 	char b[N];
47477c478bd9Sstevel@tonic-gate 	off_t m, m1;
47487c478bd9Sstevel@tonic-gate 
47497c478bd9Sstevel@tonic-gate 
47507c478bd9Sstevel@tonic-gate loop:
47517c478bd9Sstevel@tonic-gate 	if (l >= h)
47527c478bd9Sstevel@tonic-gate 		return ((off_t)-1);
47537c478bd9Sstevel@tonic-gate 	m = l + (h-l)/2 - N/2;
47547c478bd9Sstevel@tonic-gate 	if (m < l)
47557c478bd9Sstevel@tonic-gate 		m = l;
47567c478bd9Sstevel@tonic-gate 	(void) fseek(tfile, m, 0);
47577c478bd9Sstevel@tonic-gate 	if (m == 0) {
47587c478bd9Sstevel@tonic-gate 		(void) fread(b+1, 1, N-1, tfile);
47597c478bd9Sstevel@tonic-gate 		b[0] = '\n';
47607c478bd9Sstevel@tonic-gate 		m--;
47617c478bd9Sstevel@tonic-gate 	} else
47627c478bd9Sstevel@tonic-gate 		(void) fread(b, 1, N, tfile);
47637c478bd9Sstevel@tonic-gate 	for (i = 0; i < N; i++) {
47647c478bd9Sstevel@tonic-gate 		if (b[i] == '\n')
47657c478bd9Sstevel@tonic-gate 			break;
47667c478bd9Sstevel@tonic-gate 		m++;
47677c478bd9Sstevel@tonic-gate 	}
47687c478bd9Sstevel@tonic-gate 	if (m >= h)
47697c478bd9Sstevel@tonic-gate 		return ((off_t)-1);
47707c478bd9Sstevel@tonic-gate 	m1 = m;
47717c478bd9Sstevel@tonic-gate 	j = i;
47727c478bd9Sstevel@tonic-gate 	for (i++; i < N; i++) {
47737c478bd9Sstevel@tonic-gate 		m1++;
47747c478bd9Sstevel@tonic-gate 		if (b[i] == '\n')
47757c478bd9Sstevel@tonic-gate 			break;
47767c478bd9Sstevel@tonic-gate 	}
47777c478bd9Sstevel@tonic-gate 	i = cmp(b+j, s, n);
47787c478bd9Sstevel@tonic-gate 	if (i < 0) {
47797c478bd9Sstevel@tonic-gate 		h = m;
47807c478bd9Sstevel@tonic-gate 		goto loop;
47817c478bd9Sstevel@tonic-gate 	}
47827c478bd9Sstevel@tonic-gate 	if (i > 0) {
47837c478bd9Sstevel@tonic-gate 		l = m1;
47847c478bd9Sstevel@tonic-gate 		goto loop;
47857c478bd9Sstevel@tonic-gate 	}
47867c478bd9Sstevel@tonic-gate 	if (m < 0)
47877c478bd9Sstevel@tonic-gate 		m = 0;
47887c478bd9Sstevel@tonic-gate 	return (m);
47897c478bd9Sstevel@tonic-gate }
47907c478bd9Sstevel@tonic-gate 
47917c478bd9Sstevel@tonic-gate static int
47927c478bd9Sstevel@tonic-gate cmp(char *b, char *s, int n)
47937c478bd9Sstevel@tonic-gate {
47947c478bd9Sstevel@tonic-gate 	int i;
47957c478bd9Sstevel@tonic-gate 
47967c478bd9Sstevel@tonic-gate 	assert(b[0] == '\n');
47977c478bd9Sstevel@tonic-gate 
47987c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
47997c478bd9Sstevel@tonic-gate 		if (b[i+1] > s[i])
48007c478bd9Sstevel@tonic-gate 			return (-1);
48017c478bd9Sstevel@tonic-gate 		if (b[i+1] < s[i])
48027c478bd9Sstevel@tonic-gate 			return (1);
48037c478bd9Sstevel@tonic-gate 	}
48047c478bd9Sstevel@tonic-gate 	return (b[i+1] == ' '? 0 : -1);
48057c478bd9Sstevel@tonic-gate }
48067c478bd9Sstevel@tonic-gate 
48077c478bd9Sstevel@tonic-gate 
48087c478bd9Sstevel@tonic-gate /*
48097c478bd9Sstevel@tonic-gate  *	seekdisk	seek to next file on archive
48107c478bd9Sstevel@tonic-gate  *
48117c478bd9Sstevel@tonic-gate  *	called by passtape() only
48127c478bd9Sstevel@tonic-gate  *
48137c478bd9Sstevel@tonic-gate  *	WARNING: expects "nblock" to be set, that is, readtape() to have
48147c478bd9Sstevel@tonic-gate  *		already been called.  Since passtape() is only called
48157c478bd9Sstevel@tonic-gate  *		after a file header block has been read (why else would
48167c478bd9Sstevel@tonic-gate  *		we skip to next file?), this is currently safe.
48177c478bd9Sstevel@tonic-gate  *
48187c478bd9Sstevel@tonic-gate  *	changed to guarantee SYS_BLOCK boundary
48197c478bd9Sstevel@tonic-gate  */
48207c478bd9Sstevel@tonic-gate 
48217c478bd9Sstevel@tonic-gate static void
48227c478bd9Sstevel@tonic-gate seekdisk(blkcnt_t blocks)
48237c478bd9Sstevel@tonic-gate {
48247c478bd9Sstevel@tonic-gate 	off_t seekval;
48257c478bd9Sstevel@tonic-gate #if SYS_BLOCK > TBLOCK
48267c478bd9Sstevel@tonic-gate 	/* handle non-multiple of SYS_BLOCK */
48277c478bd9Sstevel@tonic-gate 	blkcnt_t nxb;	/* # extra blocks */
48287c478bd9Sstevel@tonic-gate #endif
48297c478bd9Sstevel@tonic-gate 
48307c478bd9Sstevel@tonic-gate 	tapepos += blocks;
48317c478bd9Sstevel@tonic-gate #ifdef DEBUG
48327c478bd9Sstevel@tonic-gate 	DEBUG("seekdisk(%" FMT_blkcnt_t ") called\n", blocks, 0);
48337c478bd9Sstevel@tonic-gate #endif
48347c478bd9Sstevel@tonic-gate 	if (recno + blocks <= nblock) {
48357c478bd9Sstevel@tonic-gate 		recno += blocks;
48367c478bd9Sstevel@tonic-gate 		return;
48377c478bd9Sstevel@tonic-gate 	}
48387c478bd9Sstevel@tonic-gate 	if (recno > nblock)
48397c478bd9Sstevel@tonic-gate 		recno = nblock;
48407c478bd9Sstevel@tonic-gate 	seekval = (off_t)blocks - (nblock - recno);
48417c478bd9Sstevel@tonic-gate 	recno = nblock;	/* so readtape() reads next time through */
48427c478bd9Sstevel@tonic-gate #if SYS_BLOCK > TBLOCK
48437c478bd9Sstevel@tonic-gate 	nxb = (blkcnt_t)(seekval % (off_t)(SYS_BLOCK / TBLOCK));
48447c478bd9Sstevel@tonic-gate #ifdef DEBUG
48457c478bd9Sstevel@tonic-gate 	DEBUG("xtrablks=%" FMT_blkcnt_t " seekval=%" FMT_blkcnt_t " blks\n",
48467c478bd9Sstevel@tonic-gate 	    nxb, seekval);
48477c478bd9Sstevel@tonic-gate #endif
48487c478bd9Sstevel@tonic-gate 	if (nxb && nxb > seekval) /* don't seek--we'll read */
48497c478bd9Sstevel@tonic-gate 		goto noseek;
48507c478bd9Sstevel@tonic-gate 	seekval -=  nxb;	/* don't seek quite so far */
48517c478bd9Sstevel@tonic-gate #endif
48527c478bd9Sstevel@tonic-gate 	if (lseek(mt, (off_t)(TBLOCK * seekval), 1) == (off_t)-1) {
48537c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
48547c478bd9Sstevel@tonic-gate 		    "tar: device seek error\n"));
48557c478bd9Sstevel@tonic-gate 		done(3);
48567c478bd9Sstevel@tonic-gate 	}
48577c478bd9Sstevel@tonic-gate #if SYS_BLOCK > TBLOCK
48587c478bd9Sstevel@tonic-gate 	/* read those extra blocks */
48597c478bd9Sstevel@tonic-gate noseek:
48607c478bd9Sstevel@tonic-gate 	if (nxb) {
48617c478bd9Sstevel@tonic-gate #ifdef DEBUG
48627c478bd9Sstevel@tonic-gate 		DEBUG("reading extra blocks\n", 0, 0);
48637c478bd9Sstevel@tonic-gate #endif
48647c478bd9Sstevel@tonic-gate 		if (read(mt, tbuf, TBLOCK*nblock) < 0) {
48657c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
48667c478bd9Sstevel@tonic-gate 			    "tar: read error while skipping file\n"));
48677c478bd9Sstevel@tonic-gate 			done(8);
48687c478bd9Sstevel@tonic-gate 		}
48697c478bd9Sstevel@tonic-gate 		recno = nxb;	/* so we don't read in next readtape() */
48707c478bd9Sstevel@tonic-gate 	}
48717c478bd9Sstevel@tonic-gate #endif
48727c478bd9Sstevel@tonic-gate }
48737c478bd9Sstevel@tonic-gate 
48747c478bd9Sstevel@tonic-gate static void
48757c478bd9Sstevel@tonic-gate readtape(char *buffer)
48767c478bd9Sstevel@tonic-gate {
48777c478bd9Sstevel@tonic-gate 	int i, j;
48787c478bd9Sstevel@tonic-gate 
48797c478bd9Sstevel@tonic-gate 	++tapepos;
48807c478bd9Sstevel@tonic-gate 	if (recno >= nblock || first) {
48817c478bd9Sstevel@tonic-gate 		if (first) {
48827c478bd9Sstevel@tonic-gate 			/*
48837c478bd9Sstevel@tonic-gate 			 * set the number of blocks to read initially, based on
48847c478bd9Sstevel@tonic-gate 			 * the defined defaults for the device, or on the
48857c478bd9Sstevel@tonic-gate 			 * explicit block factor given.
48867c478bd9Sstevel@tonic-gate 			 */
48877c478bd9Sstevel@tonic-gate 			if (bflag || defaults_used)
48887c478bd9Sstevel@tonic-gate 				j = nblock;
48897c478bd9Sstevel@tonic-gate 			else
48907c478bd9Sstevel@tonic-gate 				j = NBLOCK;
48917c478bd9Sstevel@tonic-gate 		} else
48927c478bd9Sstevel@tonic-gate 			j = nblock;
48937c478bd9Sstevel@tonic-gate 
48947c478bd9Sstevel@tonic-gate 		if ((i = read(mt, tbuf, TBLOCK*j)) < 0) {
48957c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
48967c478bd9Sstevel@tonic-gate 			    "tar: tape read error\n"));
48977c478bd9Sstevel@tonic-gate 			done(3);
48987c478bd9Sstevel@tonic-gate 		/*
4899b35e803cSss161016 		 * i == 0 and !rflag means that EOF is reached and we are
4900b35e803cSss161016 		 * trying to update or replace an empty tar file, so exit
4901b35e803cSss161016 		 * with an error.
4902b35e803cSss161016 		 *
4903b35e803cSss161016 		 * If i == 0 and !first and NotTape, it means the pointer
4904b35e803cSss161016 		 * has gone past the EOF. It could happen if two processes
4905b35e803cSss161016 		 * try to update the same tar file simultaneously. So exit
4906b35e803cSss161016 		 * with an error.
49077c478bd9Sstevel@tonic-gate 		 */
4908b35e803cSss161016 
4909b35e803cSss161016 		} else if (i == 0) {
4910b35e803cSss161016 			if (first && !rflag) {
49117c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
49127c478bd9Sstevel@tonic-gate 				    "tar: blocksize = %d\n"), i);
49137c478bd9Sstevel@tonic-gate 				done(Errflg);
4914b35e803cSss161016 			} else if (!first && (!rflag || NotTape)) {
49157c478bd9Sstevel@tonic-gate 				mterr("read", 0, 2);
4916b35e803cSss161016 			}
49177c478bd9Sstevel@tonic-gate 		} else if ((!first || Bflag) && i != TBLOCK*j) {
49187c478bd9Sstevel@tonic-gate 			/*
49197c478bd9Sstevel@tonic-gate 			 * Short read - try to get the remaining bytes.
49207c478bd9Sstevel@tonic-gate 			 */
49217c478bd9Sstevel@tonic-gate 
49227c478bd9Sstevel@tonic-gate 			int remaining = (TBLOCK * j) - i;
49237c478bd9Sstevel@tonic-gate 			char *b = (char *)tbuf + i;
49247c478bd9Sstevel@tonic-gate 			int r;
49257c478bd9Sstevel@tonic-gate 
49267c478bd9Sstevel@tonic-gate 			do {
49277c478bd9Sstevel@tonic-gate 				if ((r = read(mt, b, remaining)) < 0) {
49287c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
49297c478bd9Sstevel@tonic-gate 					    gettext("tar: tape read error\n"));
49307c478bd9Sstevel@tonic-gate 					done(3);
49317c478bd9Sstevel@tonic-gate 				}
49327c478bd9Sstevel@tonic-gate 				b += r;
49337c478bd9Sstevel@tonic-gate 				remaining -= r;
49347c478bd9Sstevel@tonic-gate 				i += r;
49357c478bd9Sstevel@tonic-gate 			} while (remaining > 0 && r != 0);
49367c478bd9Sstevel@tonic-gate 		}
49377c478bd9Sstevel@tonic-gate 		if (first) {
49387c478bd9Sstevel@tonic-gate 			if ((i % TBLOCK) != 0) {
49397c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
49407c478bd9Sstevel@tonic-gate 				    "tar: tape blocksize error\n"));
49417c478bd9Sstevel@tonic-gate 				done(3);
49427c478bd9Sstevel@tonic-gate 			}
49437c478bd9Sstevel@tonic-gate 			i /= TBLOCK;
49447c478bd9Sstevel@tonic-gate 			if (vflag && i != nblock && i != 1) {
49457c478bd9Sstevel@tonic-gate 				if (!NotTape)
49467c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
49477c478bd9Sstevel@tonic-gate 					    "tar: blocksize = %d\n"), i);
49487c478bd9Sstevel@tonic-gate 			}
49497c478bd9Sstevel@tonic-gate 
49507c478bd9Sstevel@tonic-gate 			/*
49517c478bd9Sstevel@tonic-gate 			 * If we are reading a tape, then a short read is
49527c478bd9Sstevel@tonic-gate 			 * understood to signify that the amount read is
49537c478bd9Sstevel@tonic-gate 			 * the tape's actual blocking factor.  We adapt
49547c478bd9Sstevel@tonic-gate 			 * nblock accordingly.  There is no reason to do
49557c478bd9Sstevel@tonic-gate 			 * this when the device is not blocked.
49567c478bd9Sstevel@tonic-gate 			 */
49577c478bd9Sstevel@tonic-gate 
49587c478bd9Sstevel@tonic-gate 			if (!NotTape)
49597c478bd9Sstevel@tonic-gate 				nblock = i;
49607c478bd9Sstevel@tonic-gate 		}
49617c478bd9Sstevel@tonic-gate 		recno = 0;
49627c478bd9Sstevel@tonic-gate 	}
49637c478bd9Sstevel@tonic-gate 
49647c478bd9Sstevel@tonic-gate 	first = FALSE;
49657c478bd9Sstevel@tonic-gate 	copy(buffer, &tbuf[recno++]);
49667c478bd9Sstevel@tonic-gate }
49677c478bd9Sstevel@tonic-gate 
49687c478bd9Sstevel@tonic-gate 
49697c478bd9Sstevel@tonic-gate /*
49707c478bd9Sstevel@tonic-gate  * replacement for writetape.
49717c478bd9Sstevel@tonic-gate  */
49727c478bd9Sstevel@tonic-gate 
49737c478bd9Sstevel@tonic-gate static int
49747c478bd9Sstevel@tonic-gate writetbuf(char *buffer, int n)
49757c478bd9Sstevel@tonic-gate {
49767c478bd9Sstevel@tonic-gate 	int i;
49777c478bd9Sstevel@tonic-gate 
49787c478bd9Sstevel@tonic-gate 	tapepos += n;		/* output block count */
49797c478bd9Sstevel@tonic-gate 
49807c478bd9Sstevel@tonic-gate 	if (recno >= nblock) {
49817c478bd9Sstevel@tonic-gate 		i = write(mt, (char *)tbuf, TBLOCK*nblock);
49827c478bd9Sstevel@tonic-gate 		if (i != TBLOCK*nblock)
49837c478bd9Sstevel@tonic-gate 			mterr("write", i, 2);
49847c478bd9Sstevel@tonic-gate 		recno = 0;
49857c478bd9Sstevel@tonic-gate 	}
49867c478bd9Sstevel@tonic-gate 
49877c478bd9Sstevel@tonic-gate 	/*
49887c478bd9Sstevel@tonic-gate 	 *  Special case:  We have an empty tape buffer, and the
49897c478bd9Sstevel@tonic-gate 	 *  users data size is >= the tape block size:  Avoid
49907c478bd9Sstevel@tonic-gate 	 *  the bcopy and dma direct to tape.  BIG WIN.  Add the
49917c478bd9Sstevel@tonic-gate 	 *  residual to the tape buffer.
49927c478bd9Sstevel@tonic-gate 	 */
49937c478bd9Sstevel@tonic-gate 	while (recno == 0 && n >= nblock) {
49947c478bd9Sstevel@tonic-gate 		i = (int)write(mt, buffer, TBLOCK*nblock);
49957c478bd9Sstevel@tonic-gate 		if (i != TBLOCK*nblock)
49967c478bd9Sstevel@tonic-gate 			mterr("write", i, 2);
49977c478bd9Sstevel@tonic-gate 		n -= nblock;
49987c478bd9Sstevel@tonic-gate 		buffer += (nblock * TBLOCK);
49997c478bd9Sstevel@tonic-gate 	}
50007c478bd9Sstevel@tonic-gate 
50017c478bd9Sstevel@tonic-gate 	while (n-- > 0) {
50027c478bd9Sstevel@tonic-gate 		(void) memcpy((char *)&tbuf[recno++], buffer, TBLOCK);
50037c478bd9Sstevel@tonic-gate 		buffer += TBLOCK;
50047c478bd9Sstevel@tonic-gate 		if (recno >= nblock) {
50057c478bd9Sstevel@tonic-gate 			i = (int)write(mt, (char *)tbuf, TBLOCK*nblock);
50067c478bd9Sstevel@tonic-gate 			if (i != TBLOCK*nblock)
50077c478bd9Sstevel@tonic-gate 				mterr("write", i, 2);
50087c478bd9Sstevel@tonic-gate 			recno = 0;
50097c478bd9Sstevel@tonic-gate 		}
50107c478bd9Sstevel@tonic-gate 	}
50117c478bd9Sstevel@tonic-gate 
50127c478bd9Sstevel@tonic-gate 	/* Tell the user how much to write to get in sync */
50137c478bd9Sstevel@tonic-gate 	return (nblock - recno);
50147c478bd9Sstevel@tonic-gate }
50157c478bd9Sstevel@tonic-gate 
50167c478bd9Sstevel@tonic-gate /*
50177c478bd9Sstevel@tonic-gate  *	backtape - reposition tape after reading soft "EOF" record
50187c478bd9Sstevel@tonic-gate  *
50197c478bd9Sstevel@tonic-gate  *	Backtape tries to reposition the tape back over the EOF
50207c478bd9Sstevel@tonic-gate  *	record.  This is for the 'u' and 'r' function letters so that the
50217c478bd9Sstevel@tonic-gate  *	tape can be extended.  This code is not well designed, but
50227c478bd9Sstevel@tonic-gate  *	I'm confident that the only callers who care about the
50237c478bd9Sstevel@tonic-gate  *	backspace-over-EOF feature are those involved in 'u' and 'r'.
50247c478bd9Sstevel@tonic-gate  *
50257c478bd9Sstevel@tonic-gate  *	The proper way to backup the tape is through the use of mtio.
50267c478bd9Sstevel@tonic-gate  *	Earlier spins used lseek combined with reads in a confusing
50277c478bd9Sstevel@tonic-gate  *	maneuver that only worked on 4.x, but shouldn't have, even
50287c478bd9Sstevel@tonic-gate  *	there.  Lseeks are explicitly not supported for tape devices.
50297c478bd9Sstevel@tonic-gate  */
50307c478bd9Sstevel@tonic-gate 
50317c478bd9Sstevel@tonic-gate static void
50327c478bd9Sstevel@tonic-gate backtape(void)
50337c478bd9Sstevel@tonic-gate {
50347c478bd9Sstevel@tonic-gate 	struct mtop mtcmd;
50357c478bd9Sstevel@tonic-gate #ifdef DEBUG
50367c478bd9Sstevel@tonic-gate 	DEBUG("backtape() called, recno=%" FMT_blkcnt_t " nblock=%d\n", recno,
50377c478bd9Sstevel@tonic-gate 	    nblock);
50387c478bd9Sstevel@tonic-gate #endif
50397c478bd9Sstevel@tonic-gate 	/*
50407c478bd9Sstevel@tonic-gate 	 * Backup to the position in the archive where the record
50417c478bd9Sstevel@tonic-gate 	 * currently sitting in the tbuf buffer is situated.
50427c478bd9Sstevel@tonic-gate 	 */
50437c478bd9Sstevel@tonic-gate 
50447c478bd9Sstevel@tonic-gate 	if (NotTape) {
50457c478bd9Sstevel@tonic-gate 		/*
50467c478bd9Sstevel@tonic-gate 		 * For non-tape devices, this means lseeking to the
50477c478bd9Sstevel@tonic-gate 		 * correct position.  The absolute location tapepos-recno
50487c478bd9Sstevel@tonic-gate 		 * should be the beginning of the current record.
50497c478bd9Sstevel@tonic-gate 		 */
50507c478bd9Sstevel@tonic-gate 
50517c478bd9Sstevel@tonic-gate 		if (lseek(mt, (off_t)(TBLOCK*(tapepos-recno)), SEEK_SET) ==
50527c478bd9Sstevel@tonic-gate 		    (off_t)-1) {
50537c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
50547c478bd9Sstevel@tonic-gate 			    gettext("tar: lseek to end of archive failed\n"));
50557c478bd9Sstevel@tonic-gate 			done(4);
50567c478bd9Sstevel@tonic-gate 		}
50577c478bd9Sstevel@tonic-gate 	} else {
50587c478bd9Sstevel@tonic-gate 		/*
50597c478bd9Sstevel@tonic-gate 		 * For tape devices, we backup over the most recently
50607c478bd9Sstevel@tonic-gate 		 * read record.
50617c478bd9Sstevel@tonic-gate 		 */
50627c478bd9Sstevel@tonic-gate 
50637c478bd9Sstevel@tonic-gate 		mtcmd.mt_op = MTBSR;
50647c478bd9Sstevel@tonic-gate 		mtcmd.mt_count = 1;
50657c478bd9Sstevel@tonic-gate 
50667c478bd9Sstevel@tonic-gate 		if (ioctl(mt, MTIOCTOP, &mtcmd) < 0) {
50677c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
50687c478bd9Sstevel@tonic-gate 			    gettext("tar: backspace over record failed\n"));
50697c478bd9Sstevel@tonic-gate 			done(4);
50707c478bd9Sstevel@tonic-gate 		}
50717c478bd9Sstevel@tonic-gate 	}
50727c478bd9Sstevel@tonic-gate 
50737c478bd9Sstevel@tonic-gate 	/*
50747c478bd9Sstevel@tonic-gate 	 * Decrement the tape and tbuf buffer indices to prepare for the
50757c478bd9Sstevel@tonic-gate 	 * coming write to overwrite the soft EOF record.
50767c478bd9Sstevel@tonic-gate 	 */
50777c478bd9Sstevel@tonic-gate 
50787c478bd9Sstevel@tonic-gate 	recno--;
50797c478bd9Sstevel@tonic-gate 	tapepos--;
50807c478bd9Sstevel@tonic-gate }
50817c478bd9Sstevel@tonic-gate 
50827c478bd9Sstevel@tonic-gate 
50837c478bd9Sstevel@tonic-gate /*
50847c478bd9Sstevel@tonic-gate  *	flushtape  write buffered block(s) onto tape
50857c478bd9Sstevel@tonic-gate  *
50867c478bd9Sstevel@tonic-gate  *      recno points to next free block in tbuf.  If nonzero, a write is done.
50877c478bd9Sstevel@tonic-gate  *	Care is taken to write in multiples of SYS_BLOCK when device is
50887c478bd9Sstevel@tonic-gate  *	non-magtape in case raw i/o is used.
50897c478bd9Sstevel@tonic-gate  *
50907c478bd9Sstevel@tonic-gate  *	NOTE: this is called by writetape() to do the actual writing
50917c478bd9Sstevel@tonic-gate  */
50927c478bd9Sstevel@tonic-gate 
50937c478bd9Sstevel@tonic-gate static void
50947c478bd9Sstevel@tonic-gate flushtape(void)
50957c478bd9Sstevel@tonic-gate {
50967c478bd9Sstevel@tonic-gate #ifdef DEBUG
50977c478bd9Sstevel@tonic-gate 	DEBUG("flushtape() called, recno=%" FMT_blkcnt_t "\n", recno, 0);
50987c478bd9Sstevel@tonic-gate #endif
50997c478bd9Sstevel@tonic-gate 	if (recno > 0) {	/* anything buffered? */
51007c478bd9Sstevel@tonic-gate 		if (NotTape) {
51017c478bd9Sstevel@tonic-gate #if SYS_BLOCK > TBLOCK
51027c478bd9Sstevel@tonic-gate 			int i;
51037c478bd9Sstevel@tonic-gate 
51047c478bd9Sstevel@tonic-gate 			/*
51057c478bd9Sstevel@tonic-gate 			 * an odd-block write can only happen when
51067c478bd9Sstevel@tonic-gate 			 * we are at the end of a volume that is not a tape.
51077c478bd9Sstevel@tonic-gate 			 * Here we round recno up to an even SYS_BLOCK
51087c478bd9Sstevel@tonic-gate 			 * boundary.
51097c478bd9Sstevel@tonic-gate 			 */
51107c478bd9Sstevel@tonic-gate 			if ((i = recno % (SYS_BLOCK / TBLOCK)) != 0) {
51117c478bd9Sstevel@tonic-gate #ifdef DEBUG
51127c478bd9Sstevel@tonic-gate 				DEBUG("flushtape() %d rounding blocks\n", i, 0);
51137c478bd9Sstevel@tonic-gate #endif
51147c478bd9Sstevel@tonic-gate 				recno += i;	/* round up to even SYS_BLOCK */
51157c478bd9Sstevel@tonic-gate 			}
51167c478bd9Sstevel@tonic-gate #endif
51177c478bd9Sstevel@tonic-gate 			if (recno > nblock)
51187c478bd9Sstevel@tonic-gate 				recno = nblock;
51197c478bd9Sstevel@tonic-gate 		}
51207c478bd9Sstevel@tonic-gate #ifdef DEBUG
51217c478bd9Sstevel@tonic-gate 		DEBUG("writing out %" FMT_blkcnt_t " blocks of %" FMT_blkcnt_t
51227c478bd9Sstevel@tonic-gate 		    " bytes\n", (blkcnt_t)(NotTape ? recno : nblock),
51237c478bd9Sstevel@tonic-gate 		    (blkcnt_t)(NotTape ? recno : nblock) * TBLOCK);
51247c478bd9Sstevel@tonic-gate #endif
51257c478bd9Sstevel@tonic-gate 		if (write(mt, tbuf,
51267c478bd9Sstevel@tonic-gate 		    (size_t)(NotTape ? recno : nblock) * TBLOCK) < 0) {
51277c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
51287c478bd9Sstevel@tonic-gate 			    "tar: tape write error\n"));
51297c478bd9Sstevel@tonic-gate 			done(2);
51307c478bd9Sstevel@tonic-gate 		}
51317c478bd9Sstevel@tonic-gate 		recno = 0;
51327c478bd9Sstevel@tonic-gate 	}
51337c478bd9Sstevel@tonic-gate }
51347c478bd9Sstevel@tonic-gate 
51357c478bd9Sstevel@tonic-gate static void
51367c478bd9Sstevel@tonic-gate copy(void *dst, void *src)
51377c478bd9Sstevel@tonic-gate {
51387c478bd9Sstevel@tonic-gate 	(void) memcpy(dst, src, TBLOCK);
51397c478bd9Sstevel@tonic-gate }
51407c478bd9Sstevel@tonic-gate 
51417c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
51427c478bd9Sstevel@tonic-gate /*
51437c478bd9Sstevel@tonic-gate  *	initarg -- initialize things for nextarg.
51447c478bd9Sstevel@tonic-gate  *
51457c478bd9Sstevel@tonic-gate  *	argv		filename list, a la argv.
51467c478bd9Sstevel@tonic-gate  *	filefile	name of file containing filenames.  Unless doing
51477c478bd9Sstevel@tonic-gate  *		a create, seeks must be allowable (e.g. no named pipes).
51487c478bd9Sstevel@tonic-gate  *
51497c478bd9Sstevel@tonic-gate  *	- if filefile is non-NULL, it will be used first, and argv will
51507c478bd9Sstevel@tonic-gate  *	be used when the data in filefile are exhausted.
51517c478bd9Sstevel@tonic-gate  *	- otherwise argv will be used.
51527c478bd9Sstevel@tonic-gate  */
51537c478bd9Sstevel@tonic-gate static char **Cmdargv = NULL;
51547c478bd9Sstevel@tonic-gate static FILE *FILEFile = NULL;
51557c478bd9Sstevel@tonic-gate static long seekFile = -1;
51567c478bd9Sstevel@tonic-gate static char *ptrtoFile, *begofFile, *endofFile;
51577c478bd9Sstevel@tonic-gate 
51587c478bd9Sstevel@tonic-gate static	void
51597c478bd9Sstevel@tonic-gate initarg(char *argv[], char *filefile)
51607c478bd9Sstevel@tonic-gate {
51617c478bd9Sstevel@tonic-gate 	struct stat statbuf;
51627c478bd9Sstevel@tonic-gate 	char *p;
51637c478bd9Sstevel@tonic-gate 	int nbytes;
51647c478bd9Sstevel@tonic-gate 
51657c478bd9Sstevel@tonic-gate 	Cmdargv = argv;
51667c478bd9Sstevel@tonic-gate 	if (filefile == NULL)
51677c478bd9Sstevel@tonic-gate 		return;		/* no -F file */
51687c478bd9Sstevel@tonic-gate 	if (FILEFile != NULL) {
51697c478bd9Sstevel@tonic-gate 		/*
51707c478bd9Sstevel@tonic-gate 		 * need to REinitialize
51717c478bd9Sstevel@tonic-gate 		 */
51727c478bd9Sstevel@tonic-gate 		if (seekFile != -1)
51737c478bd9Sstevel@tonic-gate 			(void) fseek(FILEFile, seekFile, 0);
51747c478bd9Sstevel@tonic-gate 		ptrtoFile = begofFile;
51757c478bd9Sstevel@tonic-gate 		return;
51767c478bd9Sstevel@tonic-gate 	}
51777c478bd9Sstevel@tonic-gate 	/*
51787c478bd9Sstevel@tonic-gate 	 * first time initialization
51797c478bd9Sstevel@tonic-gate 	 */
51807c478bd9Sstevel@tonic-gate 	if ((FILEFile = fopen(filefile, "r")) == NULL)
51817c478bd9Sstevel@tonic-gate 		fatal(gettext("cannot open (%s)"), filefile);
51827c478bd9Sstevel@tonic-gate 	(void) fstat(fileno(FILEFile), &statbuf);
51837c478bd9Sstevel@tonic-gate 	if ((statbuf.st_mode & S_IFMT) != S_IFREG) {
51847c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
51857c478bd9Sstevel@tonic-gate 		    "tar: %s is not a regular file\n"), filefile);
51867c478bd9Sstevel@tonic-gate 		(void) fclose(FILEFile);
51877c478bd9Sstevel@tonic-gate 		done(1);
51887c478bd9Sstevel@tonic-gate 	}
51897c478bd9Sstevel@tonic-gate 	ptrtoFile = begofFile = endofFile;
51907c478bd9Sstevel@tonic-gate 	seekFile = 0;
51917c478bd9Sstevel@tonic-gate 	if (!xflag)
51927c478bd9Sstevel@tonic-gate 		return;		/* the file will be read only once anyway */
51937c478bd9Sstevel@tonic-gate 	nbytes = statbuf.st_size;
51947c478bd9Sstevel@tonic-gate 	while ((begofFile = calloc(nbytes, sizeof (char))) == NULL)
51957c478bd9Sstevel@tonic-gate 		nbytes -= 20;
51967c478bd9Sstevel@tonic-gate 	if (nbytes < 50) {
51977c478bd9Sstevel@tonic-gate 		free(begofFile);
51987c478bd9Sstevel@tonic-gate 		begofFile = endofFile;
51997c478bd9Sstevel@tonic-gate 		return;		/* no room so just do plain reads */
52007c478bd9Sstevel@tonic-gate 	}
52017c478bd9Sstevel@tonic-gate 	if (fread(begofFile, 1, nbytes, FILEFile) != nbytes)
52027c478bd9Sstevel@tonic-gate 		fatal(gettext("could not read %s"), filefile);
52037c478bd9Sstevel@tonic-gate 	ptrtoFile = begofFile;
52047c478bd9Sstevel@tonic-gate 	endofFile = begofFile + nbytes;
52057c478bd9Sstevel@tonic-gate 	for (p = begofFile; p < endofFile; ++p)
52067c478bd9Sstevel@tonic-gate 		if (*p == '\n')
52077c478bd9Sstevel@tonic-gate 			*p = '\0';
52087c478bd9Sstevel@tonic-gate 	if (nbytes != statbuf.st_size)
52097c478bd9Sstevel@tonic-gate 		seekFile = nbytes + 1;
52107c478bd9Sstevel@tonic-gate 	else
52117c478bd9Sstevel@tonic-gate 		(void) fclose(FILEFile);
52127c478bd9Sstevel@tonic-gate }
52137c478bd9Sstevel@tonic-gate 
52147c478bd9Sstevel@tonic-gate /*
52157c478bd9Sstevel@tonic-gate  *	nextarg -- get next argument of arglist.
52167c478bd9Sstevel@tonic-gate  *
52177c478bd9Sstevel@tonic-gate  *	The argument is taken from wherever is appropriate.
52187c478bd9Sstevel@tonic-gate  *
52197c478bd9Sstevel@tonic-gate  *	If the 'F file' function modifier has been specified, the argument
52207c478bd9Sstevel@tonic-gate  *	will be taken from the file, unless EOF has been reached.
52217c478bd9Sstevel@tonic-gate  *	Otherwise the argument will be taken from argv.
52227c478bd9Sstevel@tonic-gate  *
52237c478bd9Sstevel@tonic-gate  *	WARNING:
52247c478bd9Sstevel@tonic-gate  *	  Return value may point to static data, whose contents are over-
52257c478bd9Sstevel@tonic-gate  *	  written on each call.
52267c478bd9Sstevel@tonic-gate  */
52277c478bd9Sstevel@tonic-gate static	char  *
52287c478bd9Sstevel@tonic-gate nextarg(void)
52297c478bd9Sstevel@tonic-gate {
52307c478bd9Sstevel@tonic-gate 	static char nameFile[PATH_MAX + 1];
52317c478bd9Sstevel@tonic-gate 	int n;
52327c478bd9Sstevel@tonic-gate 	char *p;
52337c478bd9Sstevel@tonic-gate 
52347c478bd9Sstevel@tonic-gate 	if (FILEFile) {
52357c478bd9Sstevel@tonic-gate 		if (ptrtoFile < endofFile) {
52367c478bd9Sstevel@tonic-gate 			p = ptrtoFile;
52377c478bd9Sstevel@tonic-gate 			while (*ptrtoFile)
52387c478bd9Sstevel@tonic-gate 				++ptrtoFile;
52397c478bd9Sstevel@tonic-gate 			++ptrtoFile;
52407c478bd9Sstevel@tonic-gate 			return (p);
52417c478bd9Sstevel@tonic-gate 		}
52427c478bd9Sstevel@tonic-gate 		if (fgets(nameFile, PATH_MAX + 1, FILEFile) != NULL) {
52437c478bd9Sstevel@tonic-gate 			n = strlen(nameFile);
52447c478bd9Sstevel@tonic-gate 			if (n > 0 && nameFile[n-1] == '\n')
52457c478bd9Sstevel@tonic-gate 				nameFile[n-1] = '\0';
52467c478bd9Sstevel@tonic-gate 			return (nameFile);
52477c478bd9Sstevel@tonic-gate 		}
52487c478bd9Sstevel@tonic-gate 	}
52497c478bd9Sstevel@tonic-gate 	return (*Cmdargv++);
52507c478bd9Sstevel@tonic-gate }
52517c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
52527c478bd9Sstevel@tonic-gate 
52537c478bd9Sstevel@tonic-gate /*
52547c478bd9Sstevel@tonic-gate  * kcheck()
52557c478bd9Sstevel@tonic-gate  *	- checks the validity of size values for non-tape devices
52567c478bd9Sstevel@tonic-gate  *	- if size is zero, mulvol tar is disabled and size is
52577c478bd9Sstevel@tonic-gate  *	  assumed to be infinite.
52587c478bd9Sstevel@tonic-gate  *	- returns volume size in TBLOCKS
52597c478bd9Sstevel@tonic-gate  */
52607c478bd9Sstevel@tonic-gate 
52617c478bd9Sstevel@tonic-gate static blkcnt_t
52627c478bd9Sstevel@tonic-gate kcheck(char *kstr)
52637c478bd9Sstevel@tonic-gate {
52647c478bd9Sstevel@tonic-gate 	blkcnt_t kval;
52657c478bd9Sstevel@tonic-gate 
52667c478bd9Sstevel@tonic-gate 	kval = strtoll(kstr, NULL, 0);
52677c478bd9Sstevel@tonic-gate 	if (kval == (blkcnt_t)0) {	/* no multi-volume; size is infinity. */
52687c478bd9Sstevel@tonic-gate 		mulvol = 0;	/* definitely not mulvol, but we must  */
52697c478bd9Sstevel@tonic-gate 		return (0);	/* took out setting of NotTape */
52707c478bd9Sstevel@tonic-gate 	}
52717c478bd9Sstevel@tonic-gate 	if (kval < (blkcnt_t)MINSIZE) {
52727c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
52737c478bd9Sstevel@tonic-gate 		    "tar: sizes below %luK not supported (%" FMT_blkcnt_t
52747c478bd9Sstevel@tonic-gate 		    ").\n"), (ulong_t)MINSIZE, kval);
52757c478bd9Sstevel@tonic-gate 		if (!kflag)
52767c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
52777c478bd9Sstevel@tonic-gate 			    "bad size entry for %s in %s.\n"),
52787c478bd9Sstevel@tonic-gate 			    archive, DEF_FILE);
52797c478bd9Sstevel@tonic-gate 		done(1);
52807c478bd9Sstevel@tonic-gate 	}
52817c478bd9Sstevel@tonic-gate 	mulvol++;
52827c478bd9Sstevel@tonic-gate 	NotTape++;			/* implies non-tape */
52837c478bd9Sstevel@tonic-gate 	return (kval * 1024 / TBLOCK);	/* convert to TBLOCKS */
52847c478bd9Sstevel@tonic-gate }
52857c478bd9Sstevel@tonic-gate 
52867c478bd9Sstevel@tonic-gate 
52877c478bd9Sstevel@tonic-gate /*
52887c478bd9Sstevel@tonic-gate  * bcheck()
52897c478bd9Sstevel@tonic-gate  *	- checks the validity of blocking factors
52907c478bd9Sstevel@tonic-gate  *	- returns blocking factor
52917c478bd9Sstevel@tonic-gate  */
52927c478bd9Sstevel@tonic-gate 
52937c478bd9Sstevel@tonic-gate static int
52947c478bd9Sstevel@tonic-gate bcheck(char *bstr)
52957c478bd9Sstevel@tonic-gate {
52967c478bd9Sstevel@tonic-gate 	blkcnt_t bval;
52977c478bd9Sstevel@tonic-gate 
52987c478bd9Sstevel@tonic-gate 	bval = strtoll(bstr, NULL, 0);
52997c478bd9Sstevel@tonic-gate 	if ((bval <= 0) || (bval > INT_MAX / TBLOCK)) {
53007c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
53017c478bd9Sstevel@tonic-gate 		    "tar: invalid blocksize \"%s\".\n"), bstr);
53027c478bd9Sstevel@tonic-gate 		if (!bflag)
53037c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
53047c478bd9Sstevel@tonic-gate 			    "bad blocksize entry for '%s' in %s.\n"),
53057c478bd9Sstevel@tonic-gate 			    archive, DEF_FILE);
53067c478bd9Sstevel@tonic-gate 		done(1);
53077c478bd9Sstevel@tonic-gate 	}
53087c478bd9Sstevel@tonic-gate 
53097c478bd9Sstevel@tonic-gate 	return ((int)bval);
53107c478bd9Sstevel@tonic-gate }
53117c478bd9Sstevel@tonic-gate 
53127c478bd9Sstevel@tonic-gate 
53137c478bd9Sstevel@tonic-gate /*
53147c478bd9Sstevel@tonic-gate  * defset()
53157c478bd9Sstevel@tonic-gate  *	- reads DEF_FILE for the set of default values specified.
53167c478bd9Sstevel@tonic-gate  *	- initializes 'usefile', 'nblock', and 'blocklim', and 'NotTape'.
53177c478bd9Sstevel@tonic-gate  *	- 'usefile' points to static data, so will be overwritten
53187c478bd9Sstevel@tonic-gate  *	  if this routine is called a second time.
53197c478bd9Sstevel@tonic-gate  *	- the pattern specified by 'arch' must be followed by four
53207c478bd9Sstevel@tonic-gate  *	  blank-separated fields (1) device (2) blocking,
53217c478bd9Sstevel@tonic-gate  *				 (3) size(K), and (4) tape
53227c478bd9Sstevel@tonic-gate  *	  for example: archive0=/dev/fd 1 400 n
53237c478bd9Sstevel@tonic-gate  */
53247c478bd9Sstevel@tonic-gate 
53257c478bd9Sstevel@tonic-gate static int
53267c478bd9Sstevel@tonic-gate defset(char *arch)
53277c478bd9Sstevel@tonic-gate {
53287c478bd9Sstevel@tonic-gate 	char *bp;
53297c478bd9Sstevel@tonic-gate 
53307c478bd9Sstevel@tonic-gate 	if (defopen(DEF_FILE) != 0)
53317c478bd9Sstevel@tonic-gate 		return (FALSE);
53327c478bd9Sstevel@tonic-gate 	if (defcntl(DC_SETFLAGS, (DC_STD & ~(DC_CASE))) == -1) {
53337c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
53347c478bd9Sstevel@tonic-gate 		    "tar: error setting parameters for %s.\n"), DEF_FILE);
53357c478bd9Sstevel@tonic-gate 		return (FALSE);			/* & following ones too */
53367c478bd9Sstevel@tonic-gate 	}
53377c478bd9Sstevel@tonic-gate 	if ((bp = defread(arch)) == NULL) {
53387c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
53397c478bd9Sstevel@tonic-gate 		    "tar: missing or invalid '%s' entry in %s.\n"),
53407c478bd9Sstevel@tonic-gate 		    arch, DEF_FILE);
53417c478bd9Sstevel@tonic-gate 		return (FALSE);
53427c478bd9Sstevel@tonic-gate 	}
53437c478bd9Sstevel@tonic-gate 	if ((usefile = strtok(bp, " \t")) == NULL) {
53447c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
53457c478bd9Sstevel@tonic-gate 		    "tar: '%s' entry in %s is empty!\n"), arch, DEF_FILE);
53467c478bd9Sstevel@tonic-gate 		return (FALSE);
53477c478bd9Sstevel@tonic-gate 	}
53487c478bd9Sstevel@tonic-gate 	if ((bp = strtok(NULL, " \t")) == NULL) {
53497c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
53507c478bd9Sstevel@tonic-gate 		    "tar: block component missing in '%s' entry in %s.\n"),
53517c478bd9Sstevel@tonic-gate 		    arch, DEF_FILE);
53527c478bd9Sstevel@tonic-gate 		return (FALSE);
53537c478bd9Sstevel@tonic-gate 	}
53547c478bd9Sstevel@tonic-gate 	nblock = bcheck(bp);
53557c478bd9Sstevel@tonic-gate 	if ((bp = strtok(NULL, " \t")) == NULL) {
53567c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
53577c478bd9Sstevel@tonic-gate 		    "tar: size component missing in '%s' entry in %s.\n"),
53587c478bd9Sstevel@tonic-gate 		    arch, DEF_FILE);
53597c478bd9Sstevel@tonic-gate 		return (FALSE);
53607c478bd9Sstevel@tonic-gate 	}
53617c478bd9Sstevel@tonic-gate 	blocklim = kcheck(bp);
53627c478bd9Sstevel@tonic-gate 	if ((bp = strtok(NULL, " \t")) != NULL)
53637c478bd9Sstevel@tonic-gate 		NotTape = (*bp == 'n' || *bp == 'N');
53647c478bd9Sstevel@tonic-gate 	else
53657c478bd9Sstevel@tonic-gate 		NotTape = (blocklim != 0);
53667c478bd9Sstevel@tonic-gate 	(void) defopen(NULL);
53677c478bd9Sstevel@tonic-gate #ifdef DEBUG
53687c478bd9Sstevel@tonic-gate 	DEBUG("defset: archive='%s'; usefile='%s'\n", arch, usefile);
53697c478bd9Sstevel@tonic-gate 	DEBUG("defset: nblock='%d'; blocklim='%" FMT_blkcnt_t "'\n",
53707c478bd9Sstevel@tonic-gate 	    nblock, blocklim);
53717c478bd9Sstevel@tonic-gate 	DEBUG("defset: not tape = %d\n", NotTape, 0);
53727c478bd9Sstevel@tonic-gate #endif
53737c478bd9Sstevel@tonic-gate 	return (TRUE);
53747c478bd9Sstevel@tonic-gate }
53757c478bd9Sstevel@tonic-gate 
53767c478bd9Sstevel@tonic-gate 
53777c478bd9Sstevel@tonic-gate /*
53787c478bd9Sstevel@tonic-gate  * Following code handles excluded and included files.
53797c478bd9Sstevel@tonic-gate  * A hash table of file names to be {in,ex}cluded is built.
53807c478bd9Sstevel@tonic-gate  * For excluded files, before writing or extracting a file
53817c478bd9Sstevel@tonic-gate  * check to see if it is in the exclude_tbl.
53827c478bd9Sstevel@tonic-gate  * For included files, the wantit() procedure will check to
53837c478bd9Sstevel@tonic-gate  * see if the named file is in the include_tbl.
53847c478bd9Sstevel@tonic-gate  */
53857c478bd9Sstevel@tonic-gate 
53867c478bd9Sstevel@tonic-gate static void
53877c478bd9Sstevel@tonic-gate build_table(struct file_list *table[], char *file)
53887c478bd9Sstevel@tonic-gate {
53897c478bd9Sstevel@tonic-gate 	FILE	*fp;
53907c478bd9Sstevel@tonic-gate 	char	buf[PATH_MAX + 1];
53917c478bd9Sstevel@tonic-gate 
53927c478bd9Sstevel@tonic-gate 	if ((fp = fopen(file, "r")) == (FILE *)NULL)
53937c478bd9Sstevel@tonic-gate 		vperror(1, gettext("could not open %s"), file);
53947c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
53957c478bd9Sstevel@tonic-gate 		if (buf[strlen(buf) - 1] == '\n')
53967c478bd9Sstevel@tonic-gate 			buf[strlen(buf) - 1] = '\0';
53977c478bd9Sstevel@tonic-gate 		/* Only add to table if line has something in it */
53987c478bd9Sstevel@tonic-gate 		if (strspn(buf, " \t") != strlen(buf))
53997c478bd9Sstevel@tonic-gate 			add_file_to_table(table, buf);
54007c478bd9Sstevel@tonic-gate 	}
54017c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
54027c478bd9Sstevel@tonic-gate }
54037c478bd9Sstevel@tonic-gate 
54047c478bd9Sstevel@tonic-gate 
54057c478bd9Sstevel@tonic-gate /*
54067c478bd9Sstevel@tonic-gate  * Add a file name to the the specified table, if the file name has any
54077c478bd9Sstevel@tonic-gate  * trailing '/'s then delete them before inserting into the table
54087c478bd9Sstevel@tonic-gate  */
54097c478bd9Sstevel@tonic-gate 
54107c478bd9Sstevel@tonic-gate static void
54117c478bd9Sstevel@tonic-gate add_file_to_table(struct file_list *table[], char *str)
54127c478bd9Sstevel@tonic-gate {
54137c478bd9Sstevel@tonic-gate 	char	name[PATH_MAX + 1];
54147c478bd9Sstevel@tonic-gate 	unsigned int h;
54157c478bd9Sstevel@tonic-gate 	struct	file_list	*exp;
54167c478bd9Sstevel@tonic-gate 
54177c478bd9Sstevel@tonic-gate 	(void) strcpy(name, str);
54187c478bd9Sstevel@tonic-gate 	while (name[strlen(name) - 1] == '/') {
54197c478bd9Sstevel@tonic-gate 		name[strlen(name) - 1] = NULL;
54207c478bd9Sstevel@tonic-gate 	}
54217c478bd9Sstevel@tonic-gate 
54227c478bd9Sstevel@tonic-gate 	h = hash(name);
54237c478bd9Sstevel@tonic-gate 	if ((exp = (struct file_list *)calloc(sizeof (struct file_list),
54247c478bd9Sstevel@tonic-gate 	    sizeof (char))) == NULL) {
54257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
54267c478bd9Sstevel@tonic-gate 		    "tar: out of memory, exclude/include table(entry)\n"));
54277c478bd9Sstevel@tonic-gate 		exit(1);
54287c478bd9Sstevel@tonic-gate 	}
54297c478bd9Sstevel@tonic-gate 
54307c478bd9Sstevel@tonic-gate 	if ((exp->name = strdup(name)) == NULL) {
54317c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
54327c478bd9Sstevel@tonic-gate 		    "tar: out of memory, exclude/include table(file name)\n"));
54337c478bd9Sstevel@tonic-gate 		exit(1);
54347c478bd9Sstevel@tonic-gate 	}
54357c478bd9Sstevel@tonic-gate 
54367c478bd9Sstevel@tonic-gate 	exp->next = table[h];
54377c478bd9Sstevel@tonic-gate 	table[h] = exp;
54387c478bd9Sstevel@tonic-gate }
54397c478bd9Sstevel@tonic-gate 
54407c478bd9Sstevel@tonic-gate 
54417c478bd9Sstevel@tonic-gate /*
54427c478bd9Sstevel@tonic-gate  * See if a file name or any of the file's parent directories is in the
54437c478bd9Sstevel@tonic-gate  * specified table, if the file name has any trailing '/'s then delete
54447c478bd9Sstevel@tonic-gate  * them before searching the table
54457c478bd9Sstevel@tonic-gate  */
54467c478bd9Sstevel@tonic-gate 
54477c478bd9Sstevel@tonic-gate static int
54487c478bd9Sstevel@tonic-gate is_in_table(struct file_list *table[], char *str)
54497c478bd9Sstevel@tonic-gate {
54507c478bd9Sstevel@tonic-gate 	char	name[PATH_MAX + 1];
54517c478bd9Sstevel@tonic-gate 	unsigned int	h;
54527c478bd9Sstevel@tonic-gate 	struct	file_list	*exp;
54537c478bd9Sstevel@tonic-gate 	char	*ptr;
54547c478bd9Sstevel@tonic-gate 
54557c478bd9Sstevel@tonic-gate 	(void) strcpy(name, str);
54567c478bd9Sstevel@tonic-gate 	while (name[strlen(name) - 1] == '/') {
54577c478bd9Sstevel@tonic-gate 		name[strlen(name) - 1] = NULL;
54587c478bd9Sstevel@tonic-gate 	}
54597c478bd9Sstevel@tonic-gate 
54607c478bd9Sstevel@tonic-gate 	/*
54617c478bd9Sstevel@tonic-gate 	 * check for the file name in the passed list
54627c478bd9Sstevel@tonic-gate 	 */
54637c478bd9Sstevel@tonic-gate 	h = hash(name);
54647c478bd9Sstevel@tonic-gate 	exp = table[h];
54657c478bd9Sstevel@tonic-gate 	while (exp != NULL) {
54667c478bd9Sstevel@tonic-gate 		if (strcmp(name, exp->name) == 0) {
54677c478bd9Sstevel@tonic-gate 			return (1);
54687c478bd9Sstevel@tonic-gate 		}
54697c478bd9Sstevel@tonic-gate 		exp = exp->next;
54707c478bd9Sstevel@tonic-gate 	}
54717c478bd9Sstevel@tonic-gate 
54727c478bd9Sstevel@tonic-gate 	/*
54737c478bd9Sstevel@tonic-gate 	 * check for any parent directories in the file list
54747c478bd9Sstevel@tonic-gate 	 */
54757c478bd9Sstevel@tonic-gate 	while ((ptr = strrchr(name, '/'))) {
54767c478bd9Sstevel@tonic-gate 		*ptr = NULL;
54777c478bd9Sstevel@tonic-gate 		h = hash(name);
54787c478bd9Sstevel@tonic-gate 		exp = table[h];
54797c478bd9Sstevel@tonic-gate 		while (exp != NULL) {
54807c478bd9Sstevel@tonic-gate 			if (strcmp(name, exp->name) == 0) {
54817c478bd9Sstevel@tonic-gate 				return (1);
54827c478bd9Sstevel@tonic-gate 			}
54837c478bd9Sstevel@tonic-gate 			exp = exp->next;
54847c478bd9Sstevel@tonic-gate 		}
54857c478bd9Sstevel@tonic-gate 	}
54867c478bd9Sstevel@tonic-gate 
54877c478bd9Sstevel@tonic-gate 	return (0);
54887c478bd9Sstevel@tonic-gate }
54897c478bd9Sstevel@tonic-gate 
54907c478bd9Sstevel@tonic-gate 
54917c478bd9Sstevel@tonic-gate /*
54927c478bd9Sstevel@tonic-gate  * Compute a hash from a string.
54937c478bd9Sstevel@tonic-gate  */
54947c478bd9Sstevel@tonic-gate 
54957c478bd9Sstevel@tonic-gate static unsigned int
54967c478bd9Sstevel@tonic-gate hash(char *str)
54977c478bd9Sstevel@tonic-gate {
54987c478bd9Sstevel@tonic-gate 	char	*cp;
54997c478bd9Sstevel@tonic-gate 	unsigned int	h;
55007c478bd9Sstevel@tonic-gate 
55017c478bd9Sstevel@tonic-gate 	h = 0;
55027c478bd9Sstevel@tonic-gate 	for (cp = str; *cp; cp++) {
55037c478bd9Sstevel@tonic-gate 		h += *cp;
55047c478bd9Sstevel@tonic-gate 	}
55057c478bd9Sstevel@tonic-gate 	return (h % TABLE_SIZE);
55067c478bd9Sstevel@tonic-gate }
55077c478bd9Sstevel@tonic-gate 
55087c478bd9Sstevel@tonic-gate static	void *
55097c478bd9Sstevel@tonic-gate getmem(size_t size)
55107c478bd9Sstevel@tonic-gate {
55117c478bd9Sstevel@tonic-gate 	void *p = calloc((unsigned)size, sizeof (char));
55127c478bd9Sstevel@tonic-gate 
55137c478bd9Sstevel@tonic-gate 	if (p == NULL && freemem) {
55147c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
55157c478bd9Sstevel@tonic-gate 		    "tar: out of memory, link and directory modtime "
55167c478bd9Sstevel@tonic-gate 		    "info lost\n"));
55177c478bd9Sstevel@tonic-gate 		freemem = 0;
55187c478bd9Sstevel@tonic-gate 		if (errflag)
55197c478bd9Sstevel@tonic-gate 			done(1);
55207c478bd9Sstevel@tonic-gate 		else
55217c478bd9Sstevel@tonic-gate 			Errflg = 1;
55227c478bd9Sstevel@tonic-gate 	}
55237c478bd9Sstevel@tonic-gate 	return (p);
55247c478bd9Sstevel@tonic-gate }
55257c478bd9Sstevel@tonic-gate 
55267c478bd9Sstevel@tonic-gate /*
55277c478bd9Sstevel@tonic-gate  * vperror() --variable argument perror.
55287c478bd9Sstevel@tonic-gate  * Takes 3 args: exit_status, formats, args.  If exit_status is 0, then
55297c478bd9Sstevel@tonic-gate  * the errflag (exit on error) is checked -- if it is non-zero, tar exits
55307c478bd9Sstevel@tonic-gate  * with the value of whatever "errno" is set to.  If exit_status is not
55317c478bd9Sstevel@tonic-gate  * zero, then tar exits with that error status. If errflag and exit_status
55327c478bd9Sstevel@tonic-gate  * are both zero, the routine returns to where it was called and sets Errflg
55337c478bd9Sstevel@tonic-gate  * to errno.
55347c478bd9Sstevel@tonic-gate  */
55357c478bd9Sstevel@tonic-gate 
55367c478bd9Sstevel@tonic-gate static void
55377c478bd9Sstevel@tonic-gate vperror(int exit_status, char *fmt, ...)
55387c478bd9Sstevel@tonic-gate {
55397c478bd9Sstevel@tonic-gate 	va_list	ap;
55407c478bd9Sstevel@tonic-gate 
55417c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
55427c478bd9Sstevel@tonic-gate 	(void) fputs("tar: ", stderr);
55437c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
55447c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, ": %s\n", strerror(errno));
55457c478bd9Sstevel@tonic-gate 	va_end(ap);
55467c478bd9Sstevel@tonic-gate 	if (exit_status)
55477c478bd9Sstevel@tonic-gate 		done(exit_status);
55487c478bd9Sstevel@tonic-gate 	else
55497c478bd9Sstevel@tonic-gate 		if (errflag)
55507c478bd9Sstevel@tonic-gate 			done(errno);
55517c478bd9Sstevel@tonic-gate 		else
55527c478bd9Sstevel@tonic-gate 			Errflg = errno;
55537c478bd9Sstevel@tonic-gate }
55547c478bd9Sstevel@tonic-gate 
55557c478bd9Sstevel@tonic-gate 
55567c478bd9Sstevel@tonic-gate static void
55577c478bd9Sstevel@tonic-gate fatal(char *format, ...)
55587c478bd9Sstevel@tonic-gate {
55597c478bd9Sstevel@tonic-gate 	va_list	ap;
55607c478bd9Sstevel@tonic-gate 
55617c478bd9Sstevel@tonic-gate 	va_start(ap, format);
55627c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "tar: ");
55637c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
55647c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
55657c478bd9Sstevel@tonic-gate 	va_end(ap);
55667c478bd9Sstevel@tonic-gate 	done(1);
55677c478bd9Sstevel@tonic-gate }
55687c478bd9Sstevel@tonic-gate 
55697c478bd9Sstevel@tonic-gate 
55707c478bd9Sstevel@tonic-gate /*
55717c478bd9Sstevel@tonic-gate  * Check to make sure that argument is a char * ptr.
55727c478bd9Sstevel@tonic-gate  * Actually, we just check to see that it is non-null.
55737c478bd9Sstevel@tonic-gate  * If it is null, print out the message and call usage(), bailing out.
55747c478bd9Sstevel@tonic-gate  */
55757c478bd9Sstevel@tonic-gate 
55767c478bd9Sstevel@tonic-gate static void
55777c478bd9Sstevel@tonic-gate assert_string(char *s, char *msg)
55787c478bd9Sstevel@tonic-gate {
55797c478bd9Sstevel@tonic-gate 	if (s == NULL) {
55807c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, msg);
55817c478bd9Sstevel@tonic-gate 		usage();
55827c478bd9Sstevel@tonic-gate 	}
55837c478bd9Sstevel@tonic-gate }
55847c478bd9Sstevel@tonic-gate 
55857c478bd9Sstevel@tonic-gate 
55867c478bd9Sstevel@tonic-gate static void
55877c478bd9Sstevel@tonic-gate mterr(char *operation, int i, int exitcode)
55887c478bd9Sstevel@tonic-gate {
55897c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
55907c478bd9Sstevel@tonic-gate 	    "tar: %s error: "), operation);
55917c478bd9Sstevel@tonic-gate 	if (i < 0)
55927c478bd9Sstevel@tonic-gate 		perror("");
55937c478bd9Sstevel@tonic-gate 	else
55947c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("unexpected EOF\n"));
55957c478bd9Sstevel@tonic-gate 	done(exitcode);
55967c478bd9Sstevel@tonic-gate }
55977c478bd9Sstevel@tonic-gate 
55987c478bd9Sstevel@tonic-gate static int
5599*da6c28aaSamw wantit(char *argv[], char **namep, char **dirp, char **component,
5600*da6c28aaSamw     attr_data_t **attrinfo)
56017c478bd9Sstevel@tonic-gate {
56027c478bd9Sstevel@tonic-gate 	char **cp;
56037c478bd9Sstevel@tonic-gate 	int gotit;		/* true if we've found a match */
56047c478bd9Sstevel@tonic-gate 
56057c478bd9Sstevel@tonic-gate top:
56067c478bd9Sstevel@tonic-gate 	xhdr_flgs = 0;
56077c478bd9Sstevel@tonic-gate 	getdir();
56087c478bd9Sstevel@tonic-gate 	if (Xhdrflag > 0) {
56097c478bd9Sstevel@tonic-gate 		if (get_xdata() != 0) {	/* Xhdr items and regular header */
56107c478bd9Sstevel@tonic-gate 			passtape();
56117c478bd9Sstevel@tonic-gate 			return (0);	/* Error--don't want to extract  */
56127c478bd9Sstevel@tonic-gate 		}
56137c478bd9Sstevel@tonic-gate 	}
56147c478bd9Sstevel@tonic-gate 
56157c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
56167c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.typeflag == _XATTR_HDRTYPE && xattrbadhead == 0) {
5617*da6c28aaSamw 		/*
5618*da6c28aaSamw 		 * Always needs to read the extended header.  If atflag, saflag,
5619*da6c28aaSamw 		 * or tflag isn't set, then we'll have the correct info for
5620*da6c28aaSamw 		 * passtape() later.
5621*da6c28aaSamw 		 */
5622*da6c28aaSamw 		(void) read_xattr_hdr(attrinfo);
56237c478bd9Sstevel@tonic-gate 		goto top;
56247c478bd9Sstevel@tonic-gate 	}
5625*da6c28aaSamw 	/*
5626*da6c28aaSamw 	 * Now that we've read the extended header, call passtape() if we aren't
5627*da6c28aaSamw 	 * processing extended attributes.
5628*da6c28aaSamw 	 */
5629*da6c28aaSamw 	if ((xattrp != NULL) && !atflag && !saflag && !tflag) {
5630*da6c28aaSamw 		passtape();
5631*da6c28aaSamw 		return (0);
5632*da6c28aaSamw 	}
56337c478bd9Sstevel@tonic-gate #endif
56347c478bd9Sstevel@tonic-gate 
56357c478bd9Sstevel@tonic-gate 	/* sets *namep to point at the proper name */
5636*da6c28aaSamw 	if (check_prefix(namep, dirp, component) != 0) {
5637*da6c28aaSamw 		passtape();
5638*da6c28aaSamw 		return (0);
5639*da6c28aaSamw 	}
56407c478bd9Sstevel@tonic-gate 
56417c478bd9Sstevel@tonic-gate 	if (endtape()) {
56427c478bd9Sstevel@tonic-gate 		if (Bflag) {
56437c478bd9Sstevel@tonic-gate 			/*
56447c478bd9Sstevel@tonic-gate 			 * Logically at EOT - consume any extra blocks
56457c478bd9Sstevel@tonic-gate 			 * so that write to our stdin won't fail and
56467c478bd9Sstevel@tonic-gate 			 * emit an error message; otherwise something
56477c478bd9Sstevel@tonic-gate 			 * like "dd if=foo.tar | (cd bar; tar xvf -)"
56487c478bd9Sstevel@tonic-gate 			 * will produce a bogus error message from "dd".
56497c478bd9Sstevel@tonic-gate 			 */
56507c478bd9Sstevel@tonic-gate 
56517c478bd9Sstevel@tonic-gate 			while (read(mt, tbuf, TBLOCK*nblock) > 0) {
56527c478bd9Sstevel@tonic-gate 				/* empty body */
56537c478bd9Sstevel@tonic-gate 			}
56547c478bd9Sstevel@tonic-gate 		}
56557c478bd9Sstevel@tonic-gate 		return (-1);
56567c478bd9Sstevel@tonic-gate 	}
56577c478bd9Sstevel@tonic-gate 
56587c478bd9Sstevel@tonic-gate 	gotit = 0;
56597c478bd9Sstevel@tonic-gate 
56607c478bd9Sstevel@tonic-gate 	if ((Iflag && is_in_table(include_tbl, *namep)) ||
56617c478bd9Sstevel@tonic-gate 	    (! Iflag && *argv == NULL)) {
56627c478bd9Sstevel@tonic-gate 		gotit = 1;
56637c478bd9Sstevel@tonic-gate 	} else {
56647c478bd9Sstevel@tonic-gate 		for (cp = argv; *cp; cp++) {
56657c478bd9Sstevel@tonic-gate 			if (is_prefix(*cp, *namep)) {
56667c478bd9Sstevel@tonic-gate 				gotit = 1;
56677c478bd9Sstevel@tonic-gate 				break;
56687c478bd9Sstevel@tonic-gate 			}
56697c478bd9Sstevel@tonic-gate 		}
56707c478bd9Sstevel@tonic-gate 	}
56717c478bd9Sstevel@tonic-gate 
56727c478bd9Sstevel@tonic-gate 	if (! gotit) {
56737c478bd9Sstevel@tonic-gate 		passtape();
56747c478bd9Sstevel@tonic-gate 		return (0);
56757c478bd9Sstevel@tonic-gate 	}
56767c478bd9Sstevel@tonic-gate 
56777c478bd9Sstevel@tonic-gate 	if (Xflag && is_in_table(exclude_tbl, *namep)) {
56787c478bd9Sstevel@tonic-gate 		if (vflag) {
56797c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s excluded\n"),
56807c478bd9Sstevel@tonic-gate 			    *namep);
56817c478bd9Sstevel@tonic-gate 		}
56827c478bd9Sstevel@tonic-gate 		passtape();
56837c478bd9Sstevel@tonic-gate 		return (0);
56847c478bd9Sstevel@tonic-gate 	}
56857c478bd9Sstevel@tonic-gate 
56867c478bd9Sstevel@tonic-gate 	return (1);
56877c478bd9Sstevel@tonic-gate }
56887c478bd9Sstevel@tonic-gate 
5689*da6c28aaSamw static int
5690*da6c28aaSamw fill_in_attr_info(char *attr, char *longname, char *attrparent, int atparentfd,
5691*da6c28aaSamw     int rw_sysattr, attr_data_t **attrinfo)
5692*da6c28aaSamw {
5693*da6c28aaSamw 	size_t	pathlen;
5694*da6c28aaSamw 	char	*tpath;
5695*da6c28aaSamw 	char	*tparent;
5696*da6c28aaSamw 
5697*da6c28aaSamw 	/* parent info */
5698*da6c28aaSamw 	if (attrparent != NULL) {
5699*da6c28aaSamw 		if ((tparent = strdup(attrparent)) == NULL) {
5700*da6c28aaSamw 			vperror(0, gettext(
5701*da6c28aaSamw 			    "unable to allocate memory for attribute parent "
5702*da6c28aaSamw 			    "name for %sattribute %s/%s of %s"),
5703*da6c28aaSamw 			    rw_sysattr ? gettext("system ") : "",
5704*da6c28aaSamw 			    attrparent, attr, longname);
5705*da6c28aaSamw 			return (1);
5706*da6c28aaSamw 		}
5707*da6c28aaSamw 	} else {
5708*da6c28aaSamw 		tparent = NULL;
5709*da6c28aaSamw 	}
5710*da6c28aaSamw 
5711*da6c28aaSamw 	/* path info */
5712*da6c28aaSamw 	pathlen = strlen(attr) + 1;
5713*da6c28aaSamw 	if (attrparent != NULL) {
5714*da6c28aaSamw 		pathlen += strlen(attrparent) + 1;	/* add 1 for '/' */
5715*da6c28aaSamw 	}
5716*da6c28aaSamw 	if ((tpath = calloc(1, pathlen)) == NULL) {
5717*da6c28aaSamw 		vperror(0, gettext(
5718*da6c28aaSamw 		    "unable to allocate memory for full "
5719*da6c28aaSamw 		    "attribute path name for %sattribute %s%s%s of %s"),
5720*da6c28aaSamw 		    rw_sysattr ? gettext("system ") : "",
5721*da6c28aaSamw 		    (attrparent == NULL) ? "" : attrparent,
5722*da6c28aaSamw 		    (attrparent == NULL) ? "" : "/",
5723*da6c28aaSamw 		    attr, longname);
5724*da6c28aaSamw 		if (tparent != NULL) {
5725*da6c28aaSamw 			free(tparent);
5726*da6c28aaSamw 		}
5727*da6c28aaSamw 		return (1);
5728*da6c28aaSamw 	}
5729*da6c28aaSamw 	(void) snprintf(tpath, pathlen, "%s%s%s",
5730*da6c28aaSamw 	    (attrparent == NULL) ? "" : attrparent,
5731*da6c28aaSamw 	    (attrparent == NULL) ? "" : "/",
5732*da6c28aaSamw 	    attr);
5733*da6c28aaSamw 
5734*da6c28aaSamw 	/* fill in the attribute info */
5735*da6c28aaSamw 	if (*attrinfo == NULL) {
5736*da6c28aaSamw 		if ((*attrinfo = malloc(sizeof (attr_data_t))) == NULL) {
5737*da6c28aaSamw 			vperror(0, gettext(
5738*da6c28aaSamw 			    "unable to allocate memory for attribute "
5739*da6c28aaSamw 			    "information for %sattribute %s%s%s of %s"),
5740*da6c28aaSamw 			    rw_sysattr ? gettext("system ") : "",
5741*da6c28aaSamw 			    (attrparent == NULL) ? "" : attrparent,
5742*da6c28aaSamw 			    (attrparent == NULL) ? "" : gettext("/"),
5743*da6c28aaSamw 			    attr, longname);
5744*da6c28aaSamw 			if (tparent != NULL) {
5745*da6c28aaSamw 				free(tparent);
5746*da6c28aaSamw 			}
5747*da6c28aaSamw 			free(tpath);
5748*da6c28aaSamw 			return (1);
5749*da6c28aaSamw 		}
5750*da6c28aaSamw 	} else {
5751*da6c28aaSamw 		if ((*attrinfo)->attr_parent != NULL) {
5752*da6c28aaSamw 			free((*attrinfo)->attr_parent);
5753*da6c28aaSamw 		}
5754*da6c28aaSamw 		if ((*attrinfo)->attr_path != NULL) {
5755*da6c28aaSamw 			free((*attrinfo)->attr_path);
5756*da6c28aaSamw 		}
5757*da6c28aaSamw 		/*
5758*da6c28aaSamw 		 * The parent file descriptor is passed in, so don't
5759*da6c28aaSamw 		 * close it here as it should be closed by the function
5760*da6c28aaSamw 		 * that opened it.
5761*da6c28aaSamw 		 */
5762*da6c28aaSamw 	}
5763*da6c28aaSamw 	(*attrinfo)->attr_parent = tparent;
5764*da6c28aaSamw 	(*attrinfo)->attr_path = tpath;
5765*da6c28aaSamw 	(*attrinfo)->attr_rw_sysattr = rw_sysattr;
5766*da6c28aaSamw 	(*attrinfo)->attr_parentfd = atparentfd;
5767*da6c28aaSamw 
5768*da6c28aaSamw 	return (0);
5769*da6c28aaSamw }
57707c478bd9Sstevel@tonic-gate 
57717c478bd9Sstevel@tonic-gate /*
57727c478bd9Sstevel@tonic-gate  *  Return through *namep a pointer to the proper fullname (i.e  "<name> |
57737c478bd9Sstevel@tonic-gate  *  <prefix>/<name>"), as represented in the header entry dblock.dbuf.
5774*da6c28aaSamw  *
5775*da6c28aaSamw  * Returns 0 if successful, otherwise returns 1.
57767c478bd9Sstevel@tonic-gate  */
57777c478bd9Sstevel@tonic-gate 
5778*da6c28aaSamw static int
57797c478bd9Sstevel@tonic-gate check_prefix(char **namep, char **dirp, char **compp)
57807c478bd9Sstevel@tonic-gate {
57817c478bd9Sstevel@tonic-gate 	static char fullname[PATH_MAX + 1];
57827c478bd9Sstevel@tonic-gate 	static char dir[PATH_MAX + 1];
57837c478bd9Sstevel@tonic-gate 	static char component[PATH_MAX + 1];
57847c478bd9Sstevel@tonic-gate 	static char savename[PATH_MAX + 1];
57857c478bd9Sstevel@tonic-gate 	char *s;
57867c478bd9Sstevel@tonic-gate 
57877c478bd9Sstevel@tonic-gate 	(void) memset(dir, 0, sizeof (dir));
57887c478bd9Sstevel@tonic-gate 	(void) memset(component, 0, sizeof (component));
57897c478bd9Sstevel@tonic-gate 
57907c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_PATH) {
57917c478bd9Sstevel@tonic-gate 		(void) strcpy(fullname, Xtarhdr.x_path);
57927c478bd9Sstevel@tonic-gate 	} else {
57937c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.prefix[0] != '\0')
57947c478bd9Sstevel@tonic-gate 			(void) sprintf(fullname, "%.*s/%.*s", PRESIZ,
57957c478bd9Sstevel@tonic-gate 			    dblock.dbuf.prefix, NAMSIZ, dblock.dbuf.name);
57967c478bd9Sstevel@tonic-gate 		else
57977c478bd9Sstevel@tonic-gate 			(void) sprintf(fullname, "%.*s", NAMSIZ,
57987c478bd9Sstevel@tonic-gate 			    dblock.dbuf.name);
57997c478bd9Sstevel@tonic-gate 	}
58007c478bd9Sstevel@tonic-gate 
58017c478bd9Sstevel@tonic-gate 	/*
58027c478bd9Sstevel@tonic-gate 	 * Set dir and component names
58037c478bd9Sstevel@tonic-gate 	 */
58047c478bd9Sstevel@tonic-gate 
58057c478bd9Sstevel@tonic-gate 	get_parent(fullname, dir);
58067c478bd9Sstevel@tonic-gate 
58077c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
5808*da6c28aaSamw 	if (xattrp == NULL) {
58097c478bd9Sstevel@tonic-gate #endif
58107c478bd9Sstevel@tonic-gate 		/*
58117c478bd9Sstevel@tonic-gate 		 * Save of real name since were going to chop off the
58127c478bd9Sstevel@tonic-gate 		 * trailing slashes.
58137c478bd9Sstevel@tonic-gate 		 */
58147c478bd9Sstevel@tonic-gate 		(void) strcpy(savename, fullname);
58157c478bd9Sstevel@tonic-gate 		/*
58167c478bd9Sstevel@tonic-gate 		 * first strip of trailing slashes.
58177c478bd9Sstevel@tonic-gate 		 */
58187c478bd9Sstevel@tonic-gate 		chop_endslashes(savename);
58197c478bd9Sstevel@tonic-gate 		s = get_component(savename);
58207c478bd9Sstevel@tonic-gate 		(void) strcpy(component, s);
58217c478bd9Sstevel@tonic-gate 
58227c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
58237c478bd9Sstevel@tonic-gate 	} else {
58247c478bd9Sstevel@tonic-gate 		(void) strcpy(fullname, xattrp->h_names);
58257c478bd9Sstevel@tonic-gate 		(void) strcpy(dir, fullname);
5826*da6c28aaSamw 		(void) strcpy(component, basename(xattrp->h_names +
5827*da6c28aaSamw 		    strlen(xattrp->h_names) + 1));
58287c478bd9Sstevel@tonic-gate 	}
58297c478bd9Sstevel@tonic-gate #endif
58307c478bd9Sstevel@tonic-gate 	*namep = fullname;
58317c478bd9Sstevel@tonic-gate 	*dirp = dir;
58327c478bd9Sstevel@tonic-gate 	*compp = component;
5833*da6c28aaSamw 
5834*da6c28aaSamw 	return (0);
58357c478bd9Sstevel@tonic-gate }
58367c478bd9Sstevel@tonic-gate 
58377c478bd9Sstevel@tonic-gate /*
58387c478bd9Sstevel@tonic-gate  * Return true if the object indicated by the file descriptor and type
58397c478bd9Sstevel@tonic-gate  * is a tape device, false otherwise
58407c478bd9Sstevel@tonic-gate  */
58417c478bd9Sstevel@tonic-gate 
58427c478bd9Sstevel@tonic-gate static int
58437c478bd9Sstevel@tonic-gate istape(int fd, int type)
58447c478bd9Sstevel@tonic-gate {
58457c478bd9Sstevel@tonic-gate 	int result = 0;
58467c478bd9Sstevel@tonic-gate 
58474bc0a2efScasper 	if (S_ISCHR(type)) {
58487c478bd9Sstevel@tonic-gate 		struct mtget mtg;
58497c478bd9Sstevel@tonic-gate 
58507c478bd9Sstevel@tonic-gate 		if (ioctl(fd, MTIOCGET, &mtg) != -1) {
58517c478bd9Sstevel@tonic-gate 			result = 1;
58527c478bd9Sstevel@tonic-gate 		}
58537c478bd9Sstevel@tonic-gate 	}
58547c478bd9Sstevel@tonic-gate 
58557c478bd9Sstevel@tonic-gate 	return (result);
58567c478bd9Sstevel@tonic-gate }
58577c478bd9Sstevel@tonic-gate 
58587c478bd9Sstevel@tonic-gate #include <utmpx.h>
58597c478bd9Sstevel@tonic-gate 
58607c478bd9Sstevel@tonic-gate struct utmpx utmpx;
58617c478bd9Sstevel@tonic-gate 
58627c478bd9Sstevel@tonic-gate #define	NMAX	(sizeof (utmpx.ut_name))
58637c478bd9Sstevel@tonic-gate 
58647c478bd9Sstevel@tonic-gate typedef struct cachenode {	/* this struct must be zeroed before using */
58657c478bd9Sstevel@tonic-gate 	struct cachenode *next;	/* next in hash chain */
58667c478bd9Sstevel@tonic-gate 	int val;		/* the uid or gid of this entry */
58677c478bd9Sstevel@tonic-gate 	int namehash;		/* name's hash signature */
58687c478bd9Sstevel@tonic-gate 	char name[NMAX+1];	/* the string that val maps to */
58697c478bd9Sstevel@tonic-gate } cachenode_t;
58707c478bd9Sstevel@tonic-gate 
58717c478bd9Sstevel@tonic-gate #define	HASHSIZE	256
58727c478bd9Sstevel@tonic-gate 
58737c478bd9Sstevel@tonic-gate static cachenode_t *names[HASHSIZE];
58747c478bd9Sstevel@tonic-gate static cachenode_t *groups[HASHSIZE];
58757c478bd9Sstevel@tonic-gate static cachenode_t *uids[HASHSIZE];
58767c478bd9Sstevel@tonic-gate static cachenode_t *gids[HASHSIZE];
58777c478bd9Sstevel@tonic-gate 
58787c478bd9Sstevel@tonic-gate static int
58797c478bd9Sstevel@tonic-gate hash_byname(char *name)
58807c478bd9Sstevel@tonic-gate {
58817c478bd9Sstevel@tonic-gate 	int i, c, h = 0;
58827c478bd9Sstevel@tonic-gate 
58837c478bd9Sstevel@tonic-gate 	for (i = 0; i < NMAX; i++) {
58847c478bd9Sstevel@tonic-gate 		c = name[i];
58857c478bd9Sstevel@tonic-gate 		if (c == '\0')
58867c478bd9Sstevel@tonic-gate 			break;
58877c478bd9Sstevel@tonic-gate 		h = (h << 4) + h + c;
58887c478bd9Sstevel@tonic-gate 	}
58897c478bd9Sstevel@tonic-gate 	return (h);
58907c478bd9Sstevel@tonic-gate }
58917c478bd9Sstevel@tonic-gate 
58927c478bd9Sstevel@tonic-gate static cachenode_t *
58937c478bd9Sstevel@tonic-gate hash_lookup_byval(cachenode_t *table[], int val)
58947c478bd9Sstevel@tonic-gate {
58957c478bd9Sstevel@tonic-gate 	int h = val;
58967c478bd9Sstevel@tonic-gate 	cachenode_t *c;
58977c478bd9Sstevel@tonic-gate 
58987c478bd9Sstevel@tonic-gate 	for (c = table[h & (HASHSIZE - 1)]; c != NULL; c = c->next) {
58997c478bd9Sstevel@tonic-gate 		if (c->val == val)
59007c478bd9Sstevel@tonic-gate 			return (c);
59017c478bd9Sstevel@tonic-gate 	}
59027c478bd9Sstevel@tonic-gate 	return (NULL);
59037c478bd9Sstevel@tonic-gate }
59047c478bd9Sstevel@tonic-gate 
59057c478bd9Sstevel@tonic-gate static cachenode_t *
59067c478bd9Sstevel@tonic-gate hash_lookup_byname(cachenode_t *table[], char *name)
59077c478bd9Sstevel@tonic-gate {
59087c478bd9Sstevel@tonic-gate 	int h = hash_byname(name);
59097c478bd9Sstevel@tonic-gate 	cachenode_t *c;
59107c478bd9Sstevel@tonic-gate 
59117c478bd9Sstevel@tonic-gate 	for (c = table[h & (HASHSIZE - 1)]; c != NULL; c = c->next) {
59127c478bd9Sstevel@tonic-gate 		if (c->namehash == h && strcmp(c->name, name) == 0)
59137c478bd9Sstevel@tonic-gate 			return (c);
59147c478bd9Sstevel@tonic-gate 	}
59157c478bd9Sstevel@tonic-gate 	return (NULL);
59167c478bd9Sstevel@tonic-gate }
59177c478bd9Sstevel@tonic-gate 
59187c478bd9Sstevel@tonic-gate static cachenode_t *
59197c478bd9Sstevel@tonic-gate hash_insert(cachenode_t *table[], char *name, int value)
59207c478bd9Sstevel@tonic-gate {
59217c478bd9Sstevel@tonic-gate 	cachenode_t *c;
59227c478bd9Sstevel@tonic-gate 	int signature;
59237c478bd9Sstevel@tonic-gate 
59247c478bd9Sstevel@tonic-gate 	c = calloc(1, sizeof (cachenode_t));
59257c478bd9Sstevel@tonic-gate 	if (c == NULL) {
59267c478bd9Sstevel@tonic-gate 		perror("malloc");
59277c478bd9Sstevel@tonic-gate 		exit(1);
59287c478bd9Sstevel@tonic-gate 	}
59297c478bd9Sstevel@tonic-gate 	if (name != NULL) {
59307c478bd9Sstevel@tonic-gate 		(void) strncpy(c->name, name, NMAX);
59317c478bd9Sstevel@tonic-gate 		c->namehash = hash_byname(name);
59327c478bd9Sstevel@tonic-gate 	}
59337c478bd9Sstevel@tonic-gate 	c->val = value;
59347c478bd9Sstevel@tonic-gate 	if (table == uids || table == gids)
59357c478bd9Sstevel@tonic-gate 		signature = c->val;
59367c478bd9Sstevel@tonic-gate 	else
59377c478bd9Sstevel@tonic-gate 		signature = c->namehash;
59387c478bd9Sstevel@tonic-gate 	c->next = table[signature & (HASHSIZE - 1)];
59397c478bd9Sstevel@tonic-gate 	table[signature & (HASHSIZE - 1)] = c;
59407c478bd9Sstevel@tonic-gate 	return (c);
59417c478bd9Sstevel@tonic-gate }
59427c478bd9Sstevel@tonic-gate 
59437c478bd9Sstevel@tonic-gate static char *
59447c478bd9Sstevel@tonic-gate getname(uid_t uid)
59457c478bd9Sstevel@tonic-gate {
59467c478bd9Sstevel@tonic-gate 	cachenode_t *c;
59477c478bd9Sstevel@tonic-gate 
59487c478bd9Sstevel@tonic-gate 	if ((c = hash_lookup_byval(uids, uid)) == NULL) {
59497c478bd9Sstevel@tonic-gate 		struct passwd *pwent = getpwuid(uid);
59507c478bd9Sstevel@tonic-gate 		c = hash_insert(uids, pwent ? pwent->pw_name : NULL, uid);
59517c478bd9Sstevel@tonic-gate 	}
59527c478bd9Sstevel@tonic-gate 	return (c->name);
59537c478bd9Sstevel@tonic-gate }
59547c478bd9Sstevel@tonic-gate 
59557c478bd9Sstevel@tonic-gate static char *
59567c478bd9Sstevel@tonic-gate getgroup(gid_t gid)
59577c478bd9Sstevel@tonic-gate {
59587c478bd9Sstevel@tonic-gate 	cachenode_t *c;
59597c478bd9Sstevel@tonic-gate 
59607c478bd9Sstevel@tonic-gate 	if ((c = hash_lookup_byval(gids, gid)) == NULL) {
59617c478bd9Sstevel@tonic-gate 		struct group *grent = getgrgid(gid);
59627c478bd9Sstevel@tonic-gate 		c = hash_insert(gids, grent ? grent->gr_name : NULL, gid);
59637c478bd9Sstevel@tonic-gate 	}
59647c478bd9Sstevel@tonic-gate 	return (c->name);
59657c478bd9Sstevel@tonic-gate }
59667c478bd9Sstevel@tonic-gate 
59677c478bd9Sstevel@tonic-gate static uid_t
59687c478bd9Sstevel@tonic-gate getuidbyname(char *name)
59697c478bd9Sstevel@tonic-gate {
59707c478bd9Sstevel@tonic-gate 	cachenode_t *c;
59717c478bd9Sstevel@tonic-gate 
59727c478bd9Sstevel@tonic-gate 	if ((c = hash_lookup_byname(names, name)) == NULL) {
59737c478bd9Sstevel@tonic-gate 		struct passwd *pwent = getpwnam(name);
59747c478bd9Sstevel@tonic-gate 		c = hash_insert(names, name, pwent ? (int)pwent->pw_uid : -1);
59757c478bd9Sstevel@tonic-gate 	}
59767c478bd9Sstevel@tonic-gate 	return ((uid_t)c->val);
59777c478bd9Sstevel@tonic-gate }
59787c478bd9Sstevel@tonic-gate 
59797c478bd9Sstevel@tonic-gate static gid_t
59807c478bd9Sstevel@tonic-gate getgidbyname(char *group)
59817c478bd9Sstevel@tonic-gate {
59827c478bd9Sstevel@tonic-gate 	cachenode_t *c;
59837c478bd9Sstevel@tonic-gate 
59847c478bd9Sstevel@tonic-gate 	if ((c = hash_lookup_byname(groups, group)) == NULL) {
59857c478bd9Sstevel@tonic-gate 		struct group *grent = getgrnam(group);
59867c478bd9Sstevel@tonic-gate 		c = hash_insert(groups, group, grent ? (int)grent->gr_gid : -1);
59877c478bd9Sstevel@tonic-gate 	}
59887c478bd9Sstevel@tonic-gate 	return ((gid_t)c->val);
59897c478bd9Sstevel@tonic-gate }
59907c478bd9Sstevel@tonic-gate 
59917c478bd9Sstevel@tonic-gate /*
59927c478bd9Sstevel@tonic-gate  * Build the header.
59937c478bd9Sstevel@tonic-gate  * Determine whether or not an extended header is also needed.  If needed,
59947c478bd9Sstevel@tonic-gate  * create and write the extended header and its data.
59957c478bd9Sstevel@tonic-gate  * Writing of the extended header assumes that "tomodes" has been called and
59967c478bd9Sstevel@tonic-gate  * the relevant information has been placed in the header block.
59977c478bd9Sstevel@tonic-gate  */
59987c478bd9Sstevel@tonic-gate 
59997c478bd9Sstevel@tonic-gate static int
60007c478bd9Sstevel@tonic-gate build_dblock(
60017c478bd9Sstevel@tonic-gate 	const char		*name,
60027c478bd9Sstevel@tonic-gate 	const char		*linkname,
60037c478bd9Sstevel@tonic-gate 	const char		typeflag,
60047c478bd9Sstevel@tonic-gate 	const int		filetype,
60057c478bd9Sstevel@tonic-gate 	const struct stat	*sp,
60067c478bd9Sstevel@tonic-gate 	const dev_t		device,
60077c478bd9Sstevel@tonic-gate 	const char		*prefix)
60087c478bd9Sstevel@tonic-gate {
60097c478bd9Sstevel@tonic-gate 	int nblks;
60107c478bd9Sstevel@tonic-gate 	major_t		dev;
60117c478bd9Sstevel@tonic-gate 	const char	*filename;
60127c478bd9Sstevel@tonic-gate 	const char	*lastslash;
60137c478bd9Sstevel@tonic-gate 
60147c478bd9Sstevel@tonic-gate 	if (filetype == XATTR_FILE)
60157c478bd9Sstevel@tonic-gate 		dblock.dbuf.typeflag = _XATTR_HDRTYPE;
60167c478bd9Sstevel@tonic-gate 	else
60177c478bd9Sstevel@tonic-gate 		dblock.dbuf.typeflag = typeflag;
60187c478bd9Sstevel@tonic-gate 	(void) memset(dblock.dbuf.name, '\0', NAMSIZ);
60197c478bd9Sstevel@tonic-gate 	(void) memset(dblock.dbuf.linkname, '\0', NAMSIZ);
60207c478bd9Sstevel@tonic-gate 	(void) memset(dblock.dbuf.prefix, '\0', PRESIZ);
60217c478bd9Sstevel@tonic-gate 
60227c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_PATH)
60237c478bd9Sstevel@tonic-gate 		filename = Xtarhdr.x_path;
60247c478bd9Sstevel@tonic-gate 	else
60257c478bd9Sstevel@tonic-gate 		filename = name;
60267c478bd9Sstevel@tonic-gate 
60277c478bd9Sstevel@tonic-gate 	if ((dev = major(device)) > OCTAL7CHAR) {
60287c478bd9Sstevel@tonic-gate 		if (Eflag) {
60297c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_DEVMAJOR;
60307c478bd9Sstevel@tonic-gate 			Xtarhdr.x_devmajor = dev;
60317c478bd9Sstevel@tonic-gate 		} else {
60327c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
60337c478bd9Sstevel@tonic-gate 			    "Device major too large for %s.  Use -E flag."),
60347c478bd9Sstevel@tonic-gate 			    filename);
60357c478bd9Sstevel@tonic-gate 			if (errflag)
60367c478bd9Sstevel@tonic-gate 				done(1);
60377c478bd9Sstevel@tonic-gate 			else
60387c478bd9Sstevel@tonic-gate 				Errflg = 1;
60397c478bd9Sstevel@tonic-gate 		}
60407c478bd9Sstevel@tonic-gate 		dev = 0;
60417c478bd9Sstevel@tonic-gate 	}
60427c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.devmajor, "%07lo", dev);
60437c478bd9Sstevel@tonic-gate 	if ((dev = minor(device)) > OCTAL7CHAR) {
60447c478bd9Sstevel@tonic-gate 		if (Eflag) {
60457c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_DEVMINOR;
60467c478bd9Sstevel@tonic-gate 			Xtarhdr.x_devminor = dev;
60477c478bd9Sstevel@tonic-gate 		} else {
60487c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
60497c478bd9Sstevel@tonic-gate 			    "Device minor too large for %s.  Use -E flag."),
60507c478bd9Sstevel@tonic-gate 			    filename);
60517c478bd9Sstevel@tonic-gate 			if (errflag)
60527c478bd9Sstevel@tonic-gate 				done(1);
60537c478bd9Sstevel@tonic-gate 			else
60547c478bd9Sstevel@tonic-gate 				Errflg = 1;
60557c478bd9Sstevel@tonic-gate 		}
60567c478bd9Sstevel@tonic-gate 		dev = 0;
60577c478bd9Sstevel@tonic-gate 	}
60587c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.devminor, "%07lo", dev);
60597c478bd9Sstevel@tonic-gate 
60607c478bd9Sstevel@tonic-gate 	(void) strncpy(dblock.dbuf.name, name, NAMSIZ);
60617c478bd9Sstevel@tonic-gate 	(void) strncpy(dblock.dbuf.linkname, linkname, NAMSIZ);
60627c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.magic, "%.5s", magic_type);
60637c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.version, "00");
60647c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.uname, "%.31s", getname(sp->st_uid));
60657c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.gname, "%.31s", getgroup(sp->st_gid));
60667c478bd9Sstevel@tonic-gate 	(void) strncpy(dblock.dbuf.prefix, prefix, PRESIZ);
60677c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
60687c478bd9Sstevel@tonic-gate 
60697c478bd9Sstevel@tonic-gate 	if (Eflag) {
60707c478bd9Sstevel@tonic-gate 		(void) bcopy(dblock.dummy, xhdr_buf.dummy, TBLOCK);
60717c478bd9Sstevel@tonic-gate 		(void) memset(xhdr_buf.dbuf.name, '\0', NAMSIZ);
60727c478bd9Sstevel@tonic-gate 		lastslash = strrchr(name, '/');
60737c478bd9Sstevel@tonic-gate 		if (lastslash == NULL)
60747c478bd9Sstevel@tonic-gate 			lastslash = name;
60757c478bd9Sstevel@tonic-gate 		else
60767c478bd9Sstevel@tonic-gate 			lastslash++;
60777c478bd9Sstevel@tonic-gate 		(void) strcpy(xhdr_buf.dbuf.name, lastslash);
60787c478bd9Sstevel@tonic-gate 		(void) memset(xhdr_buf.dbuf.linkname, '\0', NAMSIZ);
60797c478bd9Sstevel@tonic-gate 		(void) memset(xhdr_buf.dbuf.prefix, '\0', PRESIZ);
60807c478bd9Sstevel@tonic-gate 		(void) strcpy(xhdr_buf.dbuf.prefix, xhdr_dirname);
60817c478bd9Sstevel@tonic-gate 		xhdr_count++;
60827c478bd9Sstevel@tonic-gate 		xrec_offset = 0;
60837c478bd9Sstevel@tonic-gate 		gen_date("mtime", sp->st_mtim);
60847c478bd9Sstevel@tonic-gate 		xhdr_buf.dbuf.typeflag = 'X';
60857c478bd9Sstevel@tonic-gate 		if (gen_utf8_names(filename) != 0)
60867c478bd9Sstevel@tonic-gate 			return (1);
60877c478bd9Sstevel@tonic-gate 
60887c478bd9Sstevel@tonic-gate #ifdef XHDR_DEBUG
60897c478bd9Sstevel@tonic-gate 		Xtarhdr.x_uname = dblock.dbuf.uname;
60907c478bd9Sstevel@tonic-gate 		Xtarhdr.x_gname = dblock.dbuf.gname;
60917c478bd9Sstevel@tonic-gate 		xhdr_flgs |= (_X_UNAME | _X_GNAME);
60927c478bd9Sstevel@tonic-gate #endif
60937c478bd9Sstevel@tonic-gate 		if (xhdr_flgs) {
60947c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_DEVMAJOR)
60957c478bd9Sstevel@tonic-gate 				gen_num("SUN.devmajor", Xtarhdr.x_devmajor);
60967c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_DEVMINOR)
60977c478bd9Sstevel@tonic-gate 				gen_num("SUN.devminor", Xtarhdr.x_devminor);
60987c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_GID)
60997c478bd9Sstevel@tonic-gate 				gen_num("gid", Xtarhdr.x_gid);
61007c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_UID)
61017c478bd9Sstevel@tonic-gate 				gen_num("uid", Xtarhdr.x_uid);
61027c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_SIZE)
61037c478bd9Sstevel@tonic-gate 				gen_num("size", Xtarhdr.x_filesz);
61047c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_PATH)
61057c478bd9Sstevel@tonic-gate 				gen_string("path", Xtarhdr.x_path);
61067c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_LINKPATH)
61077c478bd9Sstevel@tonic-gate 				gen_string("linkpath", Xtarhdr.x_linkpath);
61087c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_GNAME)
61097c478bd9Sstevel@tonic-gate 				gen_string("gname", Xtarhdr.x_gname);
61107c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_UNAME)
61117c478bd9Sstevel@tonic-gate 				gen_string("uname", Xtarhdr.x_uname);
61127c478bd9Sstevel@tonic-gate 		}
61137c478bd9Sstevel@tonic-gate 		(void) sprintf(xhdr_buf.dbuf.size,
61147c478bd9Sstevel@tonic-gate 		    "%011" FMT_off_t_o, xrec_offset);
61157c478bd9Sstevel@tonic-gate 		(void) sprintf(xhdr_buf.dbuf.chksum, "%07o",
61167c478bd9Sstevel@tonic-gate 		    checksum(&xhdr_buf));
61177c478bd9Sstevel@tonic-gate 		(void) writetbuf((char *)&xhdr_buf, 1);
61187c478bd9Sstevel@tonic-gate 		nblks = TBLOCKS(xrec_offset);
61197c478bd9Sstevel@tonic-gate 		(void) writetbuf(xrec_ptr, nblks);
61207c478bd9Sstevel@tonic-gate 	}
61217c478bd9Sstevel@tonic-gate 	return (0);
61227c478bd9Sstevel@tonic-gate }
61237c478bd9Sstevel@tonic-gate 
61247c478bd9Sstevel@tonic-gate 
61257c478bd9Sstevel@tonic-gate /*
61267c478bd9Sstevel@tonic-gate  *  makeDir - ensure that a directory with the pathname denoted by name
61277c478bd9Sstevel@tonic-gate  *            exists, and return 1 on success, and 0 on failure (e.g.,
61287c478bd9Sstevel@tonic-gate  *	      read-only file system, exists but not-a-directory).
61297c478bd9Sstevel@tonic-gate  */
61307c478bd9Sstevel@tonic-gate 
61317c478bd9Sstevel@tonic-gate static int
61327c478bd9Sstevel@tonic-gate makeDir(char *name)
61337c478bd9Sstevel@tonic-gate {
61347c478bd9Sstevel@tonic-gate 	struct stat buf;
61357c478bd9Sstevel@tonic-gate 
61367c478bd9Sstevel@tonic-gate 	if (access(name, 0) < 0) {  /* name doesn't exist */
61377c478bd9Sstevel@tonic-gate 		if (mkdir(name, 0777) < 0) {
61387c478bd9Sstevel@tonic-gate 			vperror(0, "%s", name);
61397c478bd9Sstevel@tonic-gate 			return (0);
61407c478bd9Sstevel@tonic-gate 		}
61417c478bd9Sstevel@tonic-gate 	} else {		   /* name exists */
61427c478bd9Sstevel@tonic-gate 		if (stat(name, &buf) < 0) {
61437c478bd9Sstevel@tonic-gate 			vperror(0, "%s", name);
61447c478bd9Sstevel@tonic-gate 			return (0);
61457c478bd9Sstevel@tonic-gate 		}
61467c478bd9Sstevel@tonic-gate 
61477c478bd9Sstevel@tonic-gate 		return ((buf.st_mode & S_IFMT) == S_IFDIR);
61487c478bd9Sstevel@tonic-gate 	}
61497c478bd9Sstevel@tonic-gate 
61507c478bd9Sstevel@tonic-gate 	return (1);
61517c478bd9Sstevel@tonic-gate }
61527c478bd9Sstevel@tonic-gate 
61537c478bd9Sstevel@tonic-gate 
61547c478bd9Sstevel@tonic-gate /*
61557c478bd9Sstevel@tonic-gate  * Save this directory and its mtime on the stack, popping and setting
61567c478bd9Sstevel@tonic-gate  * the mtimes of any stacked dirs which aren't parents of this one.
61577c478bd9Sstevel@tonic-gate  * A null name causes the entire stack to be unwound and set.
61587c478bd9Sstevel@tonic-gate  *
61597c478bd9Sstevel@tonic-gate  * Since all the elements of the directory "stack" share a common
61607c478bd9Sstevel@tonic-gate  * prefix, we can make do with one string.  We keep only the current
61617c478bd9Sstevel@tonic-gate  * directory path, with an associated array of mtime's. A negative
61627c478bd9Sstevel@tonic-gate  * mtime means no mtime.
61637c478bd9Sstevel@tonic-gate  *
61647c478bd9Sstevel@tonic-gate  * This stack algorithm is not guaranteed to work for tapes created
61657c478bd9Sstevel@tonic-gate  * with the 'r' function letter, but the vast majority of tapes with
61667c478bd9Sstevel@tonic-gate  * directories are not.  This avoids saving every directory record on
61677c478bd9Sstevel@tonic-gate  * the tape and setting all the times at the end.
61687c478bd9Sstevel@tonic-gate  *
61697c478bd9Sstevel@tonic-gate  * (This was borrowed from the 4.1.3 source, and adapted to the 5.x
61707c478bd9Sstevel@tonic-gate  *  environment)
61717c478bd9Sstevel@tonic-gate  */
61727c478bd9Sstevel@tonic-gate 
61737c478bd9Sstevel@tonic-gate static void
61747c478bd9Sstevel@tonic-gate doDirTimes(char *name, timestruc_t modTime)
61757c478bd9Sstevel@tonic-gate {
61767c478bd9Sstevel@tonic-gate 	static char dirstack[PATH_MAX+2];
61777c478bd9Sstevel@tonic-gate 			/* Add spaces for the last slash and last NULL */
61787c478bd9Sstevel@tonic-gate 	static timestruc_t	modtimes[PATH_MAX+1]; /* hash table */
61797c478bd9Sstevel@tonic-gate 	char *p = dirstack;
61807c478bd9Sstevel@tonic-gate 	char *q = name;
61817c478bd9Sstevel@tonic-gate 	char *savp;
61827c478bd9Sstevel@tonic-gate 
61837c478bd9Sstevel@tonic-gate 	if (q) {
61847c478bd9Sstevel@tonic-gate 		/*
61857c478bd9Sstevel@tonic-gate 		 * Find common prefix
61867c478bd9Sstevel@tonic-gate 		 */
61877c478bd9Sstevel@tonic-gate 
61887c478bd9Sstevel@tonic-gate 		while (*p == *q && *p) {
61897c478bd9Sstevel@tonic-gate 			p++; q++;
61907c478bd9Sstevel@tonic-gate 		}
61917c478bd9Sstevel@tonic-gate 	}
61927c478bd9Sstevel@tonic-gate 
61937c478bd9Sstevel@tonic-gate 	savp = p;
61947c478bd9Sstevel@tonic-gate 	while (*p) {
61957c478bd9Sstevel@tonic-gate 		/*
61967c478bd9Sstevel@tonic-gate 		 * Not a child: unwind the stack, setting the times.
61977c478bd9Sstevel@tonic-gate 		 * The order we do this doesn't matter, so we go "forward."
61987c478bd9Sstevel@tonic-gate 		 */
61997c478bd9Sstevel@tonic-gate 
62007c478bd9Sstevel@tonic-gate 		if (*p == '/')
62017c478bd9Sstevel@tonic-gate 			if (modtimes[p - dirstack].tv_sec >= 0) {
62027c478bd9Sstevel@tonic-gate 				*p = '\0';	 /* zap the slash */
62037c478bd9Sstevel@tonic-gate 				setPathTimes(AT_FDCWD, dirstack,
62047c478bd9Sstevel@tonic-gate 				    modtimes[p - dirstack]);
62057c478bd9Sstevel@tonic-gate 				*p = '/';
62067c478bd9Sstevel@tonic-gate 			}
62077c478bd9Sstevel@tonic-gate 		++p;
62087c478bd9Sstevel@tonic-gate 	}
62097c478bd9Sstevel@tonic-gate 
62107c478bd9Sstevel@tonic-gate 	p = savp;
62117c478bd9Sstevel@tonic-gate 
62127c478bd9Sstevel@tonic-gate 	/*
62137c478bd9Sstevel@tonic-gate 	 *  Push this one on the "stack"
62147c478bd9Sstevel@tonic-gate 	 */
62157c478bd9Sstevel@tonic-gate 
62167c478bd9Sstevel@tonic-gate 	if (q) {
62177c478bd9Sstevel@tonic-gate 
62187c478bd9Sstevel@tonic-gate 		/*
62197c478bd9Sstevel@tonic-gate 		 * Since the name parameter points the dir pathname
62207c478bd9Sstevel@tonic-gate 		 * which is limited only to contain PATH_MAX chars
62217c478bd9Sstevel@tonic-gate 		 * at maximum, we can ignore the overflow case of p.
62227c478bd9Sstevel@tonic-gate 		 */
62237c478bd9Sstevel@tonic-gate 
62247c478bd9Sstevel@tonic-gate 		while ((*p = *q++)) {	/* append the rest of the new dir */
62257c478bd9Sstevel@tonic-gate 			modtimes[p - dirstack].tv_sec = -1;
62267c478bd9Sstevel@tonic-gate 			p++;
62277c478bd9Sstevel@tonic-gate 		}
62287c478bd9Sstevel@tonic-gate 
62297c478bd9Sstevel@tonic-gate 		/*
62307c478bd9Sstevel@tonic-gate 		 * If the tar file had used 'P' or 'E' function modifier,
62317c478bd9Sstevel@tonic-gate 		 * append the last slash.
62327c478bd9Sstevel@tonic-gate 		 */
62337c478bd9Sstevel@tonic-gate 		if (*(p - 1) != '/') {
62347c478bd9Sstevel@tonic-gate 			*p++ = '/';
62357c478bd9Sstevel@tonic-gate 			*p = '\0';
62367c478bd9Sstevel@tonic-gate 		}
62377c478bd9Sstevel@tonic-gate 		/* overwrite the last one */
62387c478bd9Sstevel@tonic-gate 		modtimes[p - dirstack - 1] = modTime;
62397c478bd9Sstevel@tonic-gate 	}
62407c478bd9Sstevel@tonic-gate }
62417c478bd9Sstevel@tonic-gate 
62427c478bd9Sstevel@tonic-gate 
62437c478bd9Sstevel@tonic-gate /*
62447c478bd9Sstevel@tonic-gate  *  setPathTimes - set the modification time for given path.  Return 1 if
62457c478bd9Sstevel@tonic-gate  *                 successful and 0 if not successful.
62467c478bd9Sstevel@tonic-gate  */
62477c478bd9Sstevel@tonic-gate 
62487c478bd9Sstevel@tonic-gate static void
62497c478bd9Sstevel@tonic-gate setPathTimes(int dirfd, char *path, timestruc_t modTime)
62507c478bd9Sstevel@tonic-gate 
62517c478bd9Sstevel@tonic-gate {
62527c478bd9Sstevel@tonic-gate 	struct timeval timebuf[2];
62537c478bd9Sstevel@tonic-gate 
62547c478bd9Sstevel@tonic-gate 	/*
62557c478bd9Sstevel@tonic-gate 	 * futimesat takes an array of two timeval structs.
62567c478bd9Sstevel@tonic-gate 	 * The first entry contains access time.
62577c478bd9Sstevel@tonic-gate 	 * The second entry contains modification time.
62587c478bd9Sstevel@tonic-gate 	 * Unlike a timestruc_t, which uses nanoseconds, timeval uses
62597c478bd9Sstevel@tonic-gate 	 * microseconds.
62607c478bd9Sstevel@tonic-gate 	 */
62617c478bd9Sstevel@tonic-gate 	timebuf[0].tv_sec = time((time_t *)0);
62627c478bd9Sstevel@tonic-gate 	timebuf[0].tv_usec = 0;
62637c478bd9Sstevel@tonic-gate 	timebuf[1].tv_sec = modTime.tv_sec;
62647c478bd9Sstevel@tonic-gate 
62657c478bd9Sstevel@tonic-gate 	/* Extended header: use microseconds */
62667c478bd9Sstevel@tonic-gate 	timebuf[1].tv_usec = (xhdr_flgs & _X_MTIME) ? modTime.tv_nsec/1000 : 0;
62677c478bd9Sstevel@tonic-gate 
62687c478bd9Sstevel@tonic-gate 	if (futimesat(dirfd, path, timebuf) < 0)
62697c478bd9Sstevel@tonic-gate 		vperror(0, "can't set time on %s", path);
62707c478bd9Sstevel@tonic-gate }
62717c478bd9Sstevel@tonic-gate 
62727c478bd9Sstevel@tonic-gate 
62737c478bd9Sstevel@tonic-gate /*
62747c478bd9Sstevel@tonic-gate  * If hflag is set then delete the symbolic link's target.
62757c478bd9Sstevel@tonic-gate  * If !hflag then delete the target.
62767c478bd9Sstevel@tonic-gate  */
62777c478bd9Sstevel@tonic-gate 
62787c478bd9Sstevel@tonic-gate static void
62792c0f0499Slovely delete_target(int fd, char *comp, char *namep)
62807c478bd9Sstevel@tonic-gate {
62817c478bd9Sstevel@tonic-gate 	struct	stat	xtractbuf;
62827c478bd9Sstevel@tonic-gate 	char buf[PATH_MAX + 1];
62837c478bd9Sstevel@tonic-gate 	int n;
62847c478bd9Sstevel@tonic-gate 
62857c478bd9Sstevel@tonic-gate 
62862c0f0499Slovely 	if (unlinkat(fd, comp, AT_REMOVEDIR) < 0) {
62877c478bd9Sstevel@tonic-gate 		if (errno == ENOTDIR && !hflag) {
62882c0f0499Slovely 			(void) unlinkat(fd, comp, 0);
62897c478bd9Sstevel@tonic-gate 		} else if (errno == ENOTDIR && hflag) {
62907c478bd9Sstevel@tonic-gate 			if (!lstat(namep, &xtractbuf)) {
62917c478bd9Sstevel@tonic-gate 				if ((xtractbuf.st_mode & S_IFMT) != S_IFLNK) {
62922c0f0499Slovely 					(void) unlinkat(fd, comp, 0);
62937c478bd9Sstevel@tonic-gate 				} else if ((n = readlink(namep, buf,
62947c478bd9Sstevel@tonic-gate 				    PATH_MAX)) != -1) {
62957c478bd9Sstevel@tonic-gate 					buf[n] = (char)NULL;
62967c478bd9Sstevel@tonic-gate 					(void) unlinkat(fd, buf,
62977c478bd9Sstevel@tonic-gate 					    AT_REMOVEDIR);
62987c478bd9Sstevel@tonic-gate 					if (errno == ENOTDIR)
62997c478bd9Sstevel@tonic-gate 						(void) unlinkat(fd, buf, 0);
63007c478bd9Sstevel@tonic-gate 				} else {
63012c0f0499Slovely 					(void) unlinkat(fd, comp, 0);
63027c478bd9Sstevel@tonic-gate 				}
63037c478bd9Sstevel@tonic-gate 			} else {
63042c0f0499Slovely 				(void) unlinkat(fd, comp, 0);
63057c478bd9Sstevel@tonic-gate 			}
63067c478bd9Sstevel@tonic-gate 		}
63077c478bd9Sstevel@tonic-gate 	}
63087c478bd9Sstevel@tonic-gate }
63097c478bd9Sstevel@tonic-gate 
63107c478bd9Sstevel@tonic-gate 
63117c478bd9Sstevel@tonic-gate /*
63127c478bd9Sstevel@tonic-gate  * ACL changes:
63137c478bd9Sstevel@tonic-gate  *	putfile():
63147c478bd9Sstevel@tonic-gate  *		Get acl info after stat. Write out ancillary file
63157c478bd9Sstevel@tonic-gate  *		before the normal file, i.e. directory, regular, FIFO,
63167c478bd9Sstevel@tonic-gate  *		link, special. If acl count is less than 4, no need to
63177c478bd9Sstevel@tonic-gate  *		create ancillary file. (i.e. standard permission is in
63187c478bd9Sstevel@tonic-gate  *		use.
63197c478bd9Sstevel@tonic-gate  *	doxtract():
63207c478bd9Sstevel@tonic-gate  *		Process ancillary file. Read it in and set acl info.
63217c478bd9Sstevel@tonic-gate  *		watch out for 'o' function modifier.
63227c478bd9Sstevel@tonic-gate  *	't' function letter to display table
63237c478bd9Sstevel@tonic-gate  */
63247c478bd9Sstevel@tonic-gate 
63257c478bd9Sstevel@tonic-gate /*
63267c478bd9Sstevel@tonic-gate  * New functions for ACLs and other security attributes
63277c478bd9Sstevel@tonic-gate  */
63287c478bd9Sstevel@tonic-gate 
63297c478bd9Sstevel@tonic-gate /*
63307c478bd9Sstevel@tonic-gate  * The function appends the new security attribute info to the end of
63317c478bd9Sstevel@tonic-gate  * existing secinfo.
63327c478bd9Sstevel@tonic-gate  */
63337c478bd9Sstevel@tonic-gate int
63347c478bd9Sstevel@tonic-gate append_secattr(
63357c478bd9Sstevel@tonic-gate 	char	 **secinfo,	/* existing security info */
63367c478bd9Sstevel@tonic-gate 	int	 *secinfo_len,	/* length of existing security info */
633745916cd2Sjpk 	int	 size,		/* new attribute size: unit depends on type */
633845916cd2Sjpk 	char	*attrtext,	/* new attribute text */
633945916cd2Sjpk 	char	 attr_type)	/* new attribute type */
63407c478bd9Sstevel@tonic-gate {
63417c478bd9Sstevel@tonic-gate 	char	*new_secinfo;
63427c478bd9Sstevel@tonic-gate 	int	newattrsize;
63437c478bd9Sstevel@tonic-gate 	int	oldsize;
634445916cd2Sjpk 	struct sec_attr	*attr;
63457c478bd9Sstevel@tonic-gate 
63467c478bd9Sstevel@tonic-gate 	/* no need to add */
634745916cd2Sjpk 	if (attr_type != DIR_TYPE) {
634845916cd2Sjpk 		if (attrtext == NULL)
63497c478bd9Sstevel@tonic-gate 			return (0);
635045916cd2Sjpk 	}
63517c478bd9Sstevel@tonic-gate 
635245916cd2Sjpk 	switch (attr_type) {
635345916cd2Sjpk 	case UFSD_ACL:
635445916cd2Sjpk 	case ACE_ACL:
63557c478bd9Sstevel@tonic-gate 		if (attrtext == NULL) {
63567c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "acltotext failed\n");
63577c478bd9Sstevel@tonic-gate 			return (-1);
63587c478bd9Sstevel@tonic-gate 		}
63597c478bd9Sstevel@tonic-gate 		/* header: type + size = 8 */
63607c478bd9Sstevel@tonic-gate 		newattrsize = 8 + (int)strlen(attrtext) + 1;
63617c478bd9Sstevel@tonic-gate 		attr = (struct sec_attr *)malloc(newattrsize);
63627c478bd9Sstevel@tonic-gate 		if (attr == NULL) {
63637c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "can't allocate memory\n");
63647c478bd9Sstevel@tonic-gate 			return (-1);
63657c478bd9Sstevel@tonic-gate 		}
636645916cd2Sjpk 		attr->attr_type = attr_type;
63677c478bd9Sstevel@tonic-gate 		(void) sprintf(attr->attr_len,
636845916cd2Sjpk 		    "%06o", size); /* acl entry count */
63697c478bd9Sstevel@tonic-gate 		(void) strcpy((char *)&attr->attr_info[0], attrtext);
63707c478bd9Sstevel@tonic-gate 		free(attrtext);
63717c478bd9Sstevel@tonic-gate 		break;
63727c478bd9Sstevel@tonic-gate 
637345916cd2Sjpk 	/* Trusted Extensions */
637445916cd2Sjpk 	case DIR_TYPE:
637545916cd2Sjpk 	case LBL_TYPE:
637645916cd2Sjpk 		newattrsize = sizeof (struct sec_attr) + strlen(attrtext);
637745916cd2Sjpk 		attr = (struct sec_attr *)malloc(newattrsize);
637845916cd2Sjpk 		if (attr == NULL) {
637945916cd2Sjpk 			(void) fprintf(stderr,
638045916cd2Sjpk 			gettext("can't allocate memory\n"));
638145916cd2Sjpk 			return (-1);
638245916cd2Sjpk 		}
638345916cd2Sjpk 		attr->attr_type = attr_type;
638445916cd2Sjpk 		(void) sprintf(attr->attr_len,
638545916cd2Sjpk 		    "%06d", size); /* len of attr data */
638645916cd2Sjpk 		(void) strcpy((char *)&attr->attr_info[0], attrtext);
638745916cd2Sjpk 		break;
63887c478bd9Sstevel@tonic-gate 
63897c478bd9Sstevel@tonic-gate 	default:
63907c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "unrecognized attribute type\n");
63917c478bd9Sstevel@tonic-gate 		return (-1);
63927c478bd9Sstevel@tonic-gate 	}
63937c478bd9Sstevel@tonic-gate 
63947c478bd9Sstevel@tonic-gate 	/* old security info + new attr header(8) + new attr */
63957c478bd9Sstevel@tonic-gate 	oldsize = *secinfo_len;
63967c478bd9Sstevel@tonic-gate 	*secinfo_len += newattrsize;
63977c478bd9Sstevel@tonic-gate 	new_secinfo = (char *)malloc(*secinfo_len);
63987c478bd9Sstevel@tonic-gate 	if (new_secinfo == NULL) {
63997c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "can't allocate memory\n");
64007c478bd9Sstevel@tonic-gate 		*secinfo_len -= newattrsize;
640145916cd2Sjpk 		free(attr);
64027c478bd9Sstevel@tonic-gate 		return (-1);
64037c478bd9Sstevel@tonic-gate 	}
64047c478bd9Sstevel@tonic-gate 
64057c478bd9Sstevel@tonic-gate 	(void) memcpy(new_secinfo, *secinfo, oldsize);
64067c478bd9Sstevel@tonic-gate 	(void) memcpy(new_secinfo + oldsize, attr, newattrsize);
64077c478bd9Sstevel@tonic-gate 
64087c478bd9Sstevel@tonic-gate 	free(*secinfo);
640945916cd2Sjpk 	free(attr);
64107c478bd9Sstevel@tonic-gate 	*secinfo = new_secinfo;
64117c478bd9Sstevel@tonic-gate 	return (0);
64127c478bd9Sstevel@tonic-gate }
64137c478bd9Sstevel@tonic-gate 
64147c478bd9Sstevel@tonic-gate /*
64157c478bd9Sstevel@tonic-gate  * write_ancillary(): write out an ancillary file.
64167c478bd9Sstevel@tonic-gate  *      The file has the same header as normal file except the type and size
64177c478bd9Sstevel@tonic-gate  *      fields. The type is 'A' and size is the sum of all attributes
64187c478bd9Sstevel@tonic-gate  *	in bytes.
64197c478bd9Sstevel@tonic-gate  *	The body contains a list of attribute type, size and info. Currently,
64207c478bd9Sstevel@tonic-gate  *	there is only ACL info.  This file is put before the normal file.
64217c478bd9Sstevel@tonic-gate  */
64227c478bd9Sstevel@tonic-gate void
64237c478bd9Sstevel@tonic-gate write_ancillary(union hblock *dblockp, char *secinfo, int len, char hdrtype)
64247c478bd9Sstevel@tonic-gate {
64257c478bd9Sstevel@tonic-gate 	long    blocks;
64267c478bd9Sstevel@tonic-gate 	int	savflag;
64277c478bd9Sstevel@tonic-gate 	int	savsize;
64287c478bd9Sstevel@tonic-gate 
64297c478bd9Sstevel@tonic-gate 	/* Just tranditional permissions or no security attribute info */
64307c478bd9Sstevel@tonic-gate 	if (len == 0 || secinfo == NULL)
64317c478bd9Sstevel@tonic-gate 		return;
64327c478bd9Sstevel@tonic-gate 
64337c478bd9Sstevel@tonic-gate 	/* save flag and size */
64347c478bd9Sstevel@tonic-gate 	savflag = (dblockp->dbuf).typeflag;
64357c478bd9Sstevel@tonic-gate 	(void) sscanf(dblockp->dbuf.size, "%12o", (uint_t *)&savsize);
64367c478bd9Sstevel@tonic-gate 
64377c478bd9Sstevel@tonic-gate 	/* special flag for ancillary file */
64387c478bd9Sstevel@tonic-gate 	if (hdrtype == _XATTR_HDRTYPE)
64397c478bd9Sstevel@tonic-gate 		dblockp->dbuf.typeflag = _XATTR_HDRTYPE;
64407c478bd9Sstevel@tonic-gate 	else
64417c478bd9Sstevel@tonic-gate 		dblockp->dbuf.typeflag = 'A';
64427c478bd9Sstevel@tonic-gate 
64437c478bd9Sstevel@tonic-gate 	/* for pre-2.5 versions of tar, need to make sure */
64447c478bd9Sstevel@tonic-gate 	/* the ACL file is readable			  */
64457c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.mode, "%07lo",
64467c478bd9Sstevel@tonic-gate 	    (stbuf.st_mode & POSIXMODES) | 0000200);
64477c478bd9Sstevel@tonic-gate 	(void) sprintf(dblockp->dbuf.size, "%011o", len);
64487c478bd9Sstevel@tonic-gate 	(void) sprintf(dblockp->dbuf.chksum, "%07o", checksum(dblockp));
64497c478bd9Sstevel@tonic-gate 
64507c478bd9Sstevel@tonic-gate 	/* write out the header */
64517c478bd9Sstevel@tonic-gate 	(void) writetbuf((char *)dblockp, 1);
64527c478bd9Sstevel@tonic-gate 
64537c478bd9Sstevel@tonic-gate 	/* write out security info */
64547c478bd9Sstevel@tonic-gate 	blocks = TBLOCKS(len);
64557c478bd9Sstevel@tonic-gate 	(void) writetbuf((char *)secinfo, (int)blocks);
64567c478bd9Sstevel@tonic-gate 
64577c478bd9Sstevel@tonic-gate 	/* restore mode, flag and size */
64587c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.mode, "%07lo", stbuf.st_mode & POSIXMODES);
64597c478bd9Sstevel@tonic-gate 	dblockp->dbuf.typeflag = savflag;
64607c478bd9Sstevel@tonic-gate 	(void) sprintf(dblockp->dbuf.size, "%011o", savsize);
64617c478bd9Sstevel@tonic-gate }
64627c478bd9Sstevel@tonic-gate 
64637c478bd9Sstevel@tonic-gate /*
64647c478bd9Sstevel@tonic-gate  * Read the data record for extended headers and then the regular header.
64657c478bd9Sstevel@tonic-gate  * The data are read into the buffer and then null-terminated.  Entries
64667c478bd9Sstevel@tonic-gate  * are of the format:
64677c478bd9Sstevel@tonic-gate  * 	"%d %s=%s\n"
64687c478bd9Sstevel@tonic-gate  *
64697c478bd9Sstevel@tonic-gate  * When an extended header record is found, the extended header must
64707c478bd9Sstevel@tonic-gate  * be processed and its values used to override the values in the
64717c478bd9Sstevel@tonic-gate  * normal header.  The way this is done is to process the extended
64727c478bd9Sstevel@tonic-gate  * header data record and set the data values, then call getdir
64737c478bd9Sstevel@tonic-gate  * to process the regular header, then then to reconcile the two
64747c478bd9Sstevel@tonic-gate  * sets of data.
64757c478bd9Sstevel@tonic-gate  */
64767c478bd9Sstevel@tonic-gate 
64777c478bd9Sstevel@tonic-gate static int
64787c478bd9Sstevel@tonic-gate get_xdata(void)
64797c478bd9Sstevel@tonic-gate {
64807c478bd9Sstevel@tonic-gate 	struct keylist_pair {
64817c478bd9Sstevel@tonic-gate 		int keynum;
64827c478bd9Sstevel@tonic-gate 		char *keylist;
64837c478bd9Sstevel@tonic-gate 	}	keylist_pair[] = {	_X_DEVMAJOR, "SUN.devmajor",
64847c478bd9Sstevel@tonic-gate 					_X_DEVMINOR, "SUN.devminor",
64857c478bd9Sstevel@tonic-gate 					_X_GID, "gid",
64867c478bd9Sstevel@tonic-gate 					_X_GNAME, "gname",
64877c478bd9Sstevel@tonic-gate 					_X_LINKPATH, "linkpath",
64887c478bd9Sstevel@tonic-gate 					_X_PATH, "path",
64897c478bd9Sstevel@tonic-gate 					_X_SIZE, "size",
64907c478bd9Sstevel@tonic-gate 					_X_UID, "uid",
64917c478bd9Sstevel@tonic-gate 					_X_UNAME, "uname",
64927c478bd9Sstevel@tonic-gate 					_X_MTIME, "mtime",
64937c478bd9Sstevel@tonic-gate 					_X_LAST, "NULL" };
64947c478bd9Sstevel@tonic-gate 	char		*lineloc;
64957c478bd9Sstevel@tonic-gate 	int		length, i;
64967c478bd9Sstevel@tonic-gate 	char		*keyword, *value;
64977c478bd9Sstevel@tonic-gate 	blkcnt_t	nblocks;
64987c478bd9Sstevel@tonic-gate 	int		bufneeded;
64997c478bd9Sstevel@tonic-gate 	struct stat	*sp = &stbuf;
65007c478bd9Sstevel@tonic-gate 	int		errors;
65017c478bd9Sstevel@tonic-gate 
65027c478bd9Sstevel@tonic-gate 	Xtarhdr.x_uid = 0;
65037c478bd9Sstevel@tonic-gate 	Xtarhdr.x_gid = 0;
65047c478bd9Sstevel@tonic-gate 	Xtarhdr.x_devmajor = 0;
65057c478bd9Sstevel@tonic-gate 	Xtarhdr.x_devminor = 0;
65067c478bd9Sstevel@tonic-gate 	Xtarhdr.x_filesz = 0;
65077c478bd9Sstevel@tonic-gate 	Xtarhdr.x_uname = NULL;
65087c478bd9Sstevel@tonic-gate 	Xtarhdr.x_gname = NULL;
65097c478bd9Sstevel@tonic-gate 	Xtarhdr.x_linkpath = NULL;
65107c478bd9Sstevel@tonic-gate 	Xtarhdr.x_path = NULL;
65117c478bd9Sstevel@tonic-gate 	Xtarhdr.x_mtime.tv_sec = 0;
65127c478bd9Sstevel@tonic-gate 	Xtarhdr.x_mtime.tv_nsec = 0;
65137c478bd9Sstevel@tonic-gate 	xhdr_count++;
65147c478bd9Sstevel@tonic-gate 	errors = 0;
65157c478bd9Sstevel@tonic-gate 
65167c478bd9Sstevel@tonic-gate 	nblocks = TBLOCKS(stbuf.st_size);
65177c478bd9Sstevel@tonic-gate 	bufneeded = nblocks * TBLOCK;
65187c478bd9Sstevel@tonic-gate 	if (bufneeded >= xrec_size) {
65197c478bd9Sstevel@tonic-gate 		free(xrec_ptr);
65207c478bd9Sstevel@tonic-gate 		xrec_size = bufneeded + 1;
65217c478bd9Sstevel@tonic-gate 		if ((xrec_ptr = malloc(xrec_size)) == NULL)
65227c478bd9Sstevel@tonic-gate 			fatal(gettext("cannot allocate buffer"));
65237c478bd9Sstevel@tonic-gate 	}
65247c478bd9Sstevel@tonic-gate 
65257c478bd9Sstevel@tonic-gate 	lineloc = xrec_ptr;
65267c478bd9Sstevel@tonic-gate 
65277c478bd9Sstevel@tonic-gate 	while (nblocks-- > 0) {
65287c478bd9Sstevel@tonic-gate 		readtape(lineloc);
65297c478bd9Sstevel@tonic-gate 		lineloc += TBLOCK;
65307c478bd9Sstevel@tonic-gate 	}
65317c478bd9Sstevel@tonic-gate 	lineloc = xrec_ptr;
65327c478bd9Sstevel@tonic-gate 	xrec_ptr[stbuf.st_size] = '\0';
65337c478bd9Sstevel@tonic-gate 	while (lineloc < xrec_ptr + stbuf.st_size) {
65347c478bd9Sstevel@tonic-gate 		length = atoi(lineloc);
65357c478bd9Sstevel@tonic-gate 		*(lineloc + length - 1) = '\0';
65367c478bd9Sstevel@tonic-gate 		keyword = strchr(lineloc, ' ') + 1;
65377c478bd9Sstevel@tonic-gate 		value = strchr(keyword, '=') + 1;
65387c478bd9Sstevel@tonic-gate 		*(value - 1) = '\0';
65397c478bd9Sstevel@tonic-gate 		i = 0;
65407c478bd9Sstevel@tonic-gate 		lineloc += length;
65417c478bd9Sstevel@tonic-gate 		while (keylist_pair[i].keynum != (int)_X_LAST) {
65427c478bd9Sstevel@tonic-gate 			if (strcmp(keyword, keylist_pair[i].keylist) == 0)
65437c478bd9Sstevel@tonic-gate 				break;
65447c478bd9Sstevel@tonic-gate 			i++;
65457c478bd9Sstevel@tonic-gate 		}
65467c478bd9Sstevel@tonic-gate 		errno = 0;
65477c478bd9Sstevel@tonic-gate 		switch (keylist_pair[i].keynum) {
65487c478bd9Sstevel@tonic-gate 		case _X_DEVMAJOR:
65497c478bd9Sstevel@tonic-gate 			Xtarhdr.x_devmajor = (major_t)strtoul(value, NULL, 0);
65507c478bd9Sstevel@tonic-gate 			if (errno) {
65517c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
65527c478bd9Sstevel@tonic-gate 				    "tar: Extended header major value error "
65537c478bd9Sstevel@tonic-gate 				    "for file # %llu.\n"), xhdr_count);
65547c478bd9Sstevel@tonic-gate 				errors++;
65557c478bd9Sstevel@tonic-gate 			} else
65567c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_DEVMAJOR;
65577c478bd9Sstevel@tonic-gate 			break;
65587c478bd9Sstevel@tonic-gate 		case _X_DEVMINOR:
65597c478bd9Sstevel@tonic-gate 			Xtarhdr.x_devminor = (minor_t)strtoul(value, NULL, 0);
65607c478bd9Sstevel@tonic-gate 			if (errno) {
65617c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
65627c478bd9Sstevel@tonic-gate 				    "tar: Extended header minor value error "
65637c478bd9Sstevel@tonic-gate 				    "for file # %llu.\n"), xhdr_count);
65647c478bd9Sstevel@tonic-gate 				errors++;
65657c478bd9Sstevel@tonic-gate 			} else
65667c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_DEVMINOR;
65677c478bd9Sstevel@tonic-gate 			break;
65687c478bd9Sstevel@tonic-gate 		case _X_GID:
65697c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_GID;
65707c478bd9Sstevel@tonic-gate 			Xtarhdr.x_gid = strtol(value, NULL, 0);
65717c478bd9Sstevel@tonic-gate 			if ((errno) || (Xtarhdr.x_gid > UID_MAX)) {
65727c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
65737c478bd9Sstevel@tonic-gate 				    "tar: Extended header gid value error "
65747c478bd9Sstevel@tonic-gate 				    "for file # %llu.\n"), xhdr_count);
65757c478bd9Sstevel@tonic-gate 				Xtarhdr.x_gid = GID_NOBODY;
65767c478bd9Sstevel@tonic-gate 			}
65777c478bd9Sstevel@tonic-gate 			break;
65787c478bd9Sstevel@tonic-gate 		case _X_GNAME:
65797c478bd9Sstevel@tonic-gate 			if (utf8_local("gname", &Xtarhdr.x_gname,
65807c478bd9Sstevel@tonic-gate 			    local_gname, value, _POSIX_NAME_MAX) == 0)
65817c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_GNAME;
65827c478bd9Sstevel@tonic-gate 			break;
65837c478bd9Sstevel@tonic-gate 		case _X_LINKPATH:
65847c478bd9Sstevel@tonic-gate 			if (utf8_local("linkpath", &Xtarhdr.x_linkpath,
65857c478bd9Sstevel@tonic-gate 			    local_linkpath, value, PATH_MAX) == 0)
65867c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_LINKPATH;
65877c478bd9Sstevel@tonic-gate 			else
65887c478bd9Sstevel@tonic-gate 				errors++;
65897c478bd9Sstevel@tonic-gate 			break;
65907c478bd9Sstevel@tonic-gate 		case _X_PATH:
65917c478bd9Sstevel@tonic-gate 			if (utf8_local("path", &Xtarhdr.x_path,
65927c478bd9Sstevel@tonic-gate 			    local_path, value, PATH_MAX) == 0)
65937c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_PATH;
65947c478bd9Sstevel@tonic-gate 			else
65957c478bd9Sstevel@tonic-gate 				errors++;
65967c478bd9Sstevel@tonic-gate 			break;
65977c478bd9Sstevel@tonic-gate 		case _X_SIZE:
65987c478bd9Sstevel@tonic-gate 			Xtarhdr.x_filesz = strtoull(value, NULL, 0);
65997c478bd9Sstevel@tonic-gate 			if (errno) {
66007c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
66017c478bd9Sstevel@tonic-gate 				    "tar: Extended header invalid filesize "
66027c478bd9Sstevel@tonic-gate 				    "for file # %llu.\n"), xhdr_count);
66037c478bd9Sstevel@tonic-gate 				errors++;
66047c478bd9Sstevel@tonic-gate 			} else
66057c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_SIZE;
66067c478bd9Sstevel@tonic-gate 			break;
66077c478bd9Sstevel@tonic-gate 		case _X_UID:
66087c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_UID;
66097c478bd9Sstevel@tonic-gate 			Xtarhdr.x_uid = strtol(value, NULL, 0);
66107c478bd9Sstevel@tonic-gate 			if ((errno) || (Xtarhdr.x_uid > UID_MAX)) {
66117c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
66127c478bd9Sstevel@tonic-gate 				    "tar: Extended header uid value error "
66137c478bd9Sstevel@tonic-gate 				    "for file # %llu.\n"), xhdr_count);
66147c478bd9Sstevel@tonic-gate 				Xtarhdr.x_uid = UID_NOBODY;
66157c478bd9Sstevel@tonic-gate 			}
66167c478bd9Sstevel@tonic-gate 			break;
66177c478bd9Sstevel@tonic-gate 		case _X_UNAME:
66187c478bd9Sstevel@tonic-gate 			if (utf8_local("uname", &Xtarhdr.x_uname,
66197c478bd9Sstevel@tonic-gate 			    local_uname, value, _POSIX_NAME_MAX) == 0)
66207c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_UNAME;
66217c478bd9Sstevel@tonic-gate 			break;
66227c478bd9Sstevel@tonic-gate 		case _X_MTIME:
66237c478bd9Sstevel@tonic-gate 			get_xtime(value, &(Xtarhdr.x_mtime));
66247c478bd9Sstevel@tonic-gate 			if (errno)
66257c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
66267c478bd9Sstevel@tonic-gate 				    "tar: Extended header modification time "
66277c478bd9Sstevel@tonic-gate 				    "value error for file # %llu.\n"),
66287c478bd9Sstevel@tonic-gate 				    xhdr_count);
66297c478bd9Sstevel@tonic-gate 			else
66307c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_MTIME;
66317c478bd9Sstevel@tonic-gate 			break;
66327c478bd9Sstevel@tonic-gate 		default:
66337c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
66347c478bd9Sstevel@tonic-gate 			    gettext("tar:  unrecognized extended"
66357c478bd9Sstevel@tonic-gate 			    " header keyword '%s'.  Ignored.\n"), keyword);
66367c478bd9Sstevel@tonic-gate 			break;
66377c478bd9Sstevel@tonic-gate 		}
66387c478bd9Sstevel@tonic-gate 	}
66397c478bd9Sstevel@tonic-gate 
66407c478bd9Sstevel@tonic-gate 	getdir();	/* get regular header */
66417c478bd9Sstevel@tonic-gate 
66427c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_DEVMAJOR) {
66437c478bd9Sstevel@tonic-gate 		Gen.g_devmajor = Xtarhdr.x_devmajor;
66447c478bd9Sstevel@tonic-gate 	}
66457c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_DEVMINOR) {
66467c478bd9Sstevel@tonic-gate 		Gen.g_devminor = Xtarhdr.x_devminor;
66477c478bd9Sstevel@tonic-gate 	}
66487c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_GID) {
66497c478bd9Sstevel@tonic-gate 		Gen.g_gid = Xtarhdr.x_gid;
66507c478bd9Sstevel@tonic-gate 		sp->st_gid = Gen.g_gid;
66517c478bd9Sstevel@tonic-gate 	}
66527c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_UID) {
66537c478bd9Sstevel@tonic-gate 		Gen.g_uid = Xtarhdr.x_uid;
66547c478bd9Sstevel@tonic-gate 		sp->st_uid = Gen.g_uid;
66557c478bd9Sstevel@tonic-gate 	}
66567c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_SIZE) {
66577c478bd9Sstevel@tonic-gate 		Gen.g_filesz = Xtarhdr.x_filesz;
66587c478bd9Sstevel@tonic-gate 		sp->st_size = Gen.g_filesz;
66597c478bd9Sstevel@tonic-gate 	}
66607c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_MTIME) {
66617c478bd9Sstevel@tonic-gate 		Gen.g_mtime = Xtarhdr.x_mtime.tv_sec;
66627c478bd9Sstevel@tonic-gate 		sp->st_mtim.tv_sec = Gen.g_mtime;
66637c478bd9Sstevel@tonic-gate 		sp->st_mtim.tv_nsec = Xtarhdr.x_mtime.tv_nsec;
66647c478bd9Sstevel@tonic-gate 	}
66657c478bd9Sstevel@tonic-gate 
66667c478bd9Sstevel@tonic-gate 	if (errors && errflag)
66677c478bd9Sstevel@tonic-gate 		done(1);
66687c478bd9Sstevel@tonic-gate 	else
66697c478bd9Sstevel@tonic-gate 		if (errors)
66707c478bd9Sstevel@tonic-gate 			Errflg = 1;
66717c478bd9Sstevel@tonic-gate 	return (errors);
66727c478bd9Sstevel@tonic-gate }
66737c478bd9Sstevel@tonic-gate 
66747c478bd9Sstevel@tonic-gate /*
66757c478bd9Sstevel@tonic-gate  * gen_num creates a string from a keyword and an usigned long long in the
66767c478bd9Sstevel@tonic-gate  * format:  %d %s=%s\n
66777c478bd9Sstevel@tonic-gate  * This is part of the extended header data record.
66787c478bd9Sstevel@tonic-gate  */
66797c478bd9Sstevel@tonic-gate 
66807c478bd9Sstevel@tonic-gate void
66817c478bd9Sstevel@tonic-gate gen_num(const char *keyword, const u_longlong_t number)
66827c478bd9Sstevel@tonic-gate {
66837c478bd9Sstevel@tonic-gate 	char	save_val[ULONGLONG_MAX_DIGITS + 1];
66847c478bd9Sstevel@tonic-gate 	int	len;
66857c478bd9Sstevel@tonic-gate 	char	*curr_ptr;
66867c478bd9Sstevel@tonic-gate 
66877c478bd9Sstevel@tonic-gate 	(void) sprintf(save_val, "%llu", number);
66887c478bd9Sstevel@tonic-gate 	/*
66897c478bd9Sstevel@tonic-gate 	 * len = length of entire line, including itself.  len will be
66907c478bd9Sstevel@tonic-gate 	 * two digits.  So, add the string lengths plus the length of len,
66917c478bd9Sstevel@tonic-gate 	 * plus a blank, an equal sign, and a newline.
66927c478bd9Sstevel@tonic-gate 	 */
66937c478bd9Sstevel@tonic-gate 	len = strlen(save_val) + strlen(keyword) + 5;
66947c478bd9Sstevel@tonic-gate 	if (xrec_offset + len > xrec_size) {
66957c478bd9Sstevel@tonic-gate 		if (((curr_ptr = realloc(xrec_ptr, 2 * xrec_size)) == NULL))
66967c478bd9Sstevel@tonic-gate 			fatal(gettext(
66977c478bd9Sstevel@tonic-gate 			    "cannot allocate extended header buffer"));
66987c478bd9Sstevel@tonic-gate 		xrec_ptr = curr_ptr;
66997c478bd9Sstevel@tonic-gate 		xrec_size *= 2;
67007c478bd9Sstevel@tonic-gate 	}
67017c478bd9Sstevel@tonic-gate 	(void) sprintf(&xrec_ptr[xrec_offset],
67027c478bd9Sstevel@tonic-gate 	    "%d %s=%s\n", len, keyword, save_val);
67037c478bd9Sstevel@tonic-gate 	xrec_offset += len;
67047c478bd9Sstevel@tonic-gate }
67057c478bd9Sstevel@tonic-gate 
67067c478bd9Sstevel@tonic-gate /*
67077c478bd9Sstevel@tonic-gate  * gen_date creates a string from a keyword and a timestruc_t in the
67087c478bd9Sstevel@tonic-gate  * format:  %d %s=%s\n
67097c478bd9Sstevel@tonic-gate  * This is part of the extended header data record.
67107c478bd9Sstevel@tonic-gate  * Currently, granularity is only microseconds, so the low-order three digits
67117c478bd9Sstevel@tonic-gate  * will be truncated.
67127c478bd9Sstevel@tonic-gate  */
67137c478bd9Sstevel@tonic-gate 
67147c478bd9Sstevel@tonic-gate void
67157c478bd9Sstevel@tonic-gate gen_date(const char *keyword, const timestruc_t time_value)
67167c478bd9Sstevel@tonic-gate {
67177c478bd9Sstevel@tonic-gate 	/* Allow for <seconds>.<nanoseconds>\n */
67187c478bd9Sstevel@tonic-gate 	char	save_val[TIME_MAX_DIGITS + LONG_MAX_DIGITS + 2];
67197c478bd9Sstevel@tonic-gate 	int	len;
67207c478bd9Sstevel@tonic-gate 	char	*curr_ptr;
67217c478bd9Sstevel@tonic-gate 
67227c478bd9Sstevel@tonic-gate 	(void) sprintf(save_val, "%ld", time_value.tv_sec);
67237c478bd9Sstevel@tonic-gate 	len = strlen(save_val);
67247c478bd9Sstevel@tonic-gate 	save_val[len] = '.';
67257c478bd9Sstevel@tonic-gate 	(void) sprintf(&save_val[len + 1], "%9.9ld", time_value.tv_nsec);
67267c478bd9Sstevel@tonic-gate 
67277c478bd9Sstevel@tonic-gate 	/*
67287c478bd9Sstevel@tonic-gate 	 * len = length of entire line, including itself.  len will be
67297c478bd9Sstevel@tonic-gate 	 * two digits.  So, add the string lengths plus the length of len,
67307c478bd9Sstevel@tonic-gate 	 * plus a blank, an equal sign, and a newline.
67317c478bd9Sstevel@tonic-gate 	 */
67327c478bd9Sstevel@tonic-gate 	len = strlen(save_val) + strlen(keyword) + 5;
67337c478bd9Sstevel@tonic-gate 	if (xrec_offset + len > xrec_size) {
67347c478bd9Sstevel@tonic-gate 		if (((curr_ptr = realloc(xrec_ptr, 2 * xrec_size)) == NULL))
67357c478bd9Sstevel@tonic-gate 			fatal(gettext(
67367c478bd9Sstevel@tonic-gate 			    "cannot allocate extended header buffer"));
67377c478bd9Sstevel@tonic-gate 		xrec_ptr = curr_ptr;
67387c478bd9Sstevel@tonic-gate 		xrec_size *= 2;
67397c478bd9Sstevel@tonic-gate 	}
67407c478bd9Sstevel@tonic-gate 	(void) sprintf(&xrec_ptr[xrec_offset],
67417c478bd9Sstevel@tonic-gate 	    "%d %s=%s\n", len, keyword, save_val);
67427c478bd9Sstevel@tonic-gate 	xrec_offset += len;
67437c478bd9Sstevel@tonic-gate }
67447c478bd9Sstevel@tonic-gate 
67457c478bd9Sstevel@tonic-gate /*
67467c478bd9Sstevel@tonic-gate  * gen_string creates a string from a keyword and a char * in the
67477c478bd9Sstevel@tonic-gate  * format:  %d %s=%s\n
67487c478bd9Sstevel@tonic-gate  * This is part of the extended header data record.
67497c478bd9Sstevel@tonic-gate  */
67507c478bd9Sstevel@tonic-gate 
67517c478bd9Sstevel@tonic-gate void
67527c478bd9Sstevel@tonic-gate gen_string(const char *keyword, const char *value)
67537c478bd9Sstevel@tonic-gate {
67547c478bd9Sstevel@tonic-gate 	int	len;
67557c478bd9Sstevel@tonic-gate 	char	*curr_ptr;
67567c478bd9Sstevel@tonic-gate 
67577c478bd9Sstevel@tonic-gate 	/*
67587c478bd9Sstevel@tonic-gate 	 * len = length of entire line, including itself.  The character length
67597c478bd9Sstevel@tonic-gate 	 * of len must be 1-4 characters, because the maximum size of the path
67607c478bd9Sstevel@tonic-gate 	 * or the name is PATH_MAX, which is 1024.  So, assume 1 character
67617c478bd9Sstevel@tonic-gate 	 * for len, one for the space, one for the "=", and one for the newline.
67627c478bd9Sstevel@tonic-gate 	 * Then adjust as needed.
67637c478bd9Sstevel@tonic-gate 	 */
67647c478bd9Sstevel@tonic-gate 	/* LINTED constant expression */
67657c478bd9Sstevel@tonic-gate 	assert(PATH_MAX <= 9996);
67667c478bd9Sstevel@tonic-gate 	len = strlen(value) + strlen(keyword) + 4;
67677c478bd9Sstevel@tonic-gate 	if (len > 997)
67687c478bd9Sstevel@tonic-gate 		len += 3;
67697c478bd9Sstevel@tonic-gate 	else if (len > 98)
67707c478bd9Sstevel@tonic-gate 		len += 2;
67717c478bd9Sstevel@tonic-gate 	else if (len > 9)
67727c478bd9Sstevel@tonic-gate 		len += 1;
67737c478bd9Sstevel@tonic-gate 	if (xrec_offset + len > xrec_size) {
67747c478bd9Sstevel@tonic-gate 		if (((curr_ptr = realloc(xrec_ptr, 2 * xrec_size)) == NULL))
67757c478bd9Sstevel@tonic-gate 			fatal(gettext(
67767c478bd9Sstevel@tonic-gate 			    "cannot allocate extended header buffer"));
67777c478bd9Sstevel@tonic-gate 		xrec_ptr = curr_ptr;
67787c478bd9Sstevel@tonic-gate 		xrec_size *= 2;
67797c478bd9Sstevel@tonic-gate 	}
67807c478bd9Sstevel@tonic-gate #ifdef XHDR_DEBUG
67817c478bd9Sstevel@tonic-gate 	if (strcmp(keyword+1, "name") != 0)
67827c478bd9Sstevel@tonic-gate #endif
67837c478bd9Sstevel@tonic-gate 	(void) sprintf(&xrec_ptr[xrec_offset],
67847c478bd9Sstevel@tonic-gate 	    "%d %s=%s\n", len, keyword, value);
67857c478bd9Sstevel@tonic-gate #ifdef XHDR_DEBUG
67867c478bd9Sstevel@tonic-gate 	else {
67877c478bd9Sstevel@tonic-gate 	len += 11;
67887c478bd9Sstevel@tonic-gate 	(void) sprintf(&xrec_ptr[xrec_offset],
67897c478bd9Sstevel@tonic-gate 	    "%d %s=%snametoolong\n", len, keyword, value);
67907c478bd9Sstevel@tonic-gate 	}
67917c478bd9Sstevel@tonic-gate #endif
67927c478bd9Sstevel@tonic-gate 	xrec_offset += len;
67937c478bd9Sstevel@tonic-gate }
67947c478bd9Sstevel@tonic-gate 
67957c478bd9Sstevel@tonic-gate /*
67967c478bd9Sstevel@tonic-gate  * Convert time found in the extended header data to seconds and nanoseconds.
67977c478bd9Sstevel@tonic-gate  */
67987c478bd9Sstevel@tonic-gate 
67997c478bd9Sstevel@tonic-gate void
68007c478bd9Sstevel@tonic-gate get_xtime(char *value, timestruc_t *xtime)
68017c478bd9Sstevel@tonic-gate {
68027c478bd9Sstevel@tonic-gate 	char nanosec[10];
68037c478bd9Sstevel@tonic-gate 	char *period;
68047c478bd9Sstevel@tonic-gate 	int i;
68057c478bd9Sstevel@tonic-gate 
68067c478bd9Sstevel@tonic-gate 	(void) memset(nanosec, '0', 9);
68077c478bd9Sstevel@tonic-gate 	nanosec[9] = '\0';
68087c478bd9Sstevel@tonic-gate 
68097c478bd9Sstevel@tonic-gate 	period = strchr(value, '.');
68107c478bd9Sstevel@tonic-gate 	if (period != NULL)
68117c478bd9Sstevel@tonic-gate 		period[0] = '\0';
68127c478bd9Sstevel@tonic-gate 	xtime->tv_sec = strtol(value, NULL, 10);
68137c478bd9Sstevel@tonic-gate 	if (period == NULL)
68147c478bd9Sstevel@tonic-gate 		xtime->tv_nsec = 0;
68157c478bd9Sstevel@tonic-gate 	else {
68167c478bd9Sstevel@tonic-gate 		i = strlen(period +1);
68177c478bd9Sstevel@tonic-gate 		(void) strncpy(nanosec, period + 1, min(i, 9));
68187c478bd9Sstevel@tonic-gate 		xtime->tv_nsec = strtol(nanosec, NULL, 10);
68197c478bd9Sstevel@tonic-gate 	}
68207c478bd9Sstevel@tonic-gate }
68217c478bd9Sstevel@tonic-gate 
68227c478bd9Sstevel@tonic-gate /*
68237c478bd9Sstevel@tonic-gate  *	Check linkpath for length.
68247c478bd9Sstevel@tonic-gate  *	Emit an error message and return 1 if too long.
68257c478bd9Sstevel@tonic-gate  */
68267c478bd9Sstevel@tonic-gate 
68277c478bd9Sstevel@tonic-gate int
68287c478bd9Sstevel@tonic-gate chk_path_build(
68297c478bd9Sstevel@tonic-gate 	char	*name,
68307c478bd9Sstevel@tonic-gate 	char	*longname,
68317c478bd9Sstevel@tonic-gate 	char	*linkname,
68327c478bd9Sstevel@tonic-gate 	char	*prefix,
68337c478bd9Sstevel@tonic-gate 	char	type,
68347c478bd9Sstevel@tonic-gate 	int	filetype)
68357c478bd9Sstevel@tonic-gate {
68367c478bd9Sstevel@tonic-gate 
68377c478bd9Sstevel@tonic-gate 	if (strlen(linkname) > (size_t)NAMSIZ) {
68387c478bd9Sstevel@tonic-gate 		if (Eflag > 0) {
68397c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_LINKPATH;
68407c478bd9Sstevel@tonic-gate 			Xtarhdr.x_linkpath = linkname;
68417c478bd9Sstevel@tonic-gate 		} else {
68427c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
68437c478bd9Sstevel@tonic-gate 			    "tar: %s: linked to %s\n"), longname, linkname);
68447c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
68457c478bd9Sstevel@tonic-gate 			    "tar: %s: linked name too long\n"), linkname);
68467c478bd9Sstevel@tonic-gate 			if (errflag)
68477c478bd9Sstevel@tonic-gate 				done(1);
68487c478bd9Sstevel@tonic-gate 			else
68497c478bd9Sstevel@tonic-gate 				Errflg = 1;
68507c478bd9Sstevel@tonic-gate 			return (1);
68517c478bd9Sstevel@tonic-gate 		}
68527c478bd9Sstevel@tonic-gate 	}
68537c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_LINKPATH)
68547c478bd9Sstevel@tonic-gate 		return (build_dblock(name, tchar, type,
68557c478bd9Sstevel@tonic-gate 		    filetype, &stbuf, stbuf.st_dev,
68567c478bd9Sstevel@tonic-gate 		    prefix));
68577c478bd9Sstevel@tonic-gate 	else
68587c478bd9Sstevel@tonic-gate 		return (build_dblock(name, linkname, type,
68597c478bd9Sstevel@tonic-gate 		    filetype, &stbuf, stbuf.st_dev, prefix));
68607c478bd9Sstevel@tonic-gate }
68617c478bd9Sstevel@tonic-gate 
68627c478bd9Sstevel@tonic-gate /*
68637c478bd9Sstevel@tonic-gate  * Convert from UTF-8 to local character set.
68647c478bd9Sstevel@tonic-gate  */
68657c478bd9Sstevel@tonic-gate 
68667c478bd9Sstevel@tonic-gate static int
68677c478bd9Sstevel@tonic-gate utf8_local(
68687c478bd9Sstevel@tonic-gate 	char		*option,
68697c478bd9Sstevel@tonic-gate 	char		**Xhdr_ptrptr,
68707c478bd9Sstevel@tonic-gate 	char		*target,
68717c478bd9Sstevel@tonic-gate 	const char	*source,
68727c478bd9Sstevel@tonic-gate 	int		max_val)
68737c478bd9Sstevel@tonic-gate {
68747c478bd9Sstevel@tonic-gate 	static	iconv_t	iconv_cd;
68757c478bd9Sstevel@tonic-gate 	char		*nl_target;
68767c478bd9Sstevel@tonic-gate 	const	char	*iconv_src;
68777c478bd9Sstevel@tonic-gate 	char		*iconv_trg;
6878eace40a5Sceastha 	size_t		inlen;
6879eace40a5Sceastha 	size_t		outlen;
68807c478bd9Sstevel@tonic-gate 
68817c478bd9Sstevel@tonic-gate 	if (charset_type == -1) {	/* iconv_open failed in earlier try */
68827c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
68837c478bd9Sstevel@tonic-gate 		    "tar:  file # %llu: (%s) UTF-8 conversion failed.\n"),
68847c478bd9Sstevel@tonic-gate 		    xhdr_count, source);
68857c478bd9Sstevel@tonic-gate 		return (1);
68867c478bd9Sstevel@tonic-gate 	} else if (charset_type == 0) {	/* iconv_open has not yet been done */
68877c478bd9Sstevel@tonic-gate 		nl_target = nl_langinfo(CODESET);
68887c478bd9Sstevel@tonic-gate 		if (strlen(nl_target) == 0)	/* locale using 7-bit codeset */
68897c478bd9Sstevel@tonic-gate 			nl_target = "646";
68907c478bd9Sstevel@tonic-gate 		if (strcmp(nl_target, "646") == 0)
68917c478bd9Sstevel@tonic-gate 			charset_type = 1;
68927c478bd9Sstevel@tonic-gate 		else if (strcmp(nl_target, "UTF-8") == 0)
68937c478bd9Sstevel@tonic-gate 			charset_type = 3;
68947c478bd9Sstevel@tonic-gate 		else {
68957c478bd9Sstevel@tonic-gate 			if (strncmp(nl_target, "ISO", 3) == 0)
68967c478bd9Sstevel@tonic-gate 				nl_target += 3;
68977c478bd9Sstevel@tonic-gate 			charset_type = 2;
68987c478bd9Sstevel@tonic-gate 			errno = 0;
68997c478bd9Sstevel@tonic-gate 			if ((iconv_cd = iconv_open(nl_target, "UTF-8")) ==
69007c478bd9Sstevel@tonic-gate 			    (iconv_t)-1) {
69017c478bd9Sstevel@tonic-gate 				if (errno == EINVAL)
69027c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
69037c478bd9Sstevel@tonic-gate 					    "tar: conversion routines not "
69047c478bd9Sstevel@tonic-gate 					    "available for current locale.  "));
69057c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
69067c478bd9Sstevel@tonic-gate 				    "file # %llu: (%s) UTF-8 conversion"
69077c478bd9Sstevel@tonic-gate 				    " failed.\n"), xhdr_count, source);
69087c478bd9Sstevel@tonic-gate 				charset_type = -1;
69097c478bd9Sstevel@tonic-gate 				return (1);
69107c478bd9Sstevel@tonic-gate 			}
69117c478bd9Sstevel@tonic-gate 		}
69127c478bd9Sstevel@tonic-gate 	}
69137c478bd9Sstevel@tonic-gate 
69147c478bd9Sstevel@tonic-gate 	/* locale using 7-bit codeset or UTF-8 locale */
69157c478bd9Sstevel@tonic-gate 	if (charset_type == 1 || charset_type == 3) {
69167c478bd9Sstevel@tonic-gate 		if (strlen(source) > max_val) {
69177c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
69187c478bd9Sstevel@tonic-gate 			    "tar: file # %llu: Extended header %s too long.\n"),
69197c478bd9Sstevel@tonic-gate 			    xhdr_count, option);
69207c478bd9Sstevel@tonic-gate 			return (1);
69217c478bd9Sstevel@tonic-gate 		}
69227c478bd9Sstevel@tonic-gate 		if (charset_type == 3)
69237c478bd9Sstevel@tonic-gate 			(void) strcpy(target, source);
69247c478bd9Sstevel@tonic-gate 		else if (c_utf8(target, source) != 0) {
69257c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
69267c478bd9Sstevel@tonic-gate 			    "tar:  file # %llu: (%s) UTF-8 conversion"
69277c478bd9Sstevel@tonic-gate 			    " failed.\n"), xhdr_count, source);
69287c478bd9Sstevel@tonic-gate 			return (1);
69297c478bd9Sstevel@tonic-gate 		}
69307c478bd9Sstevel@tonic-gate 		*Xhdr_ptrptr = target;
69317c478bd9Sstevel@tonic-gate 		return (0);
69327c478bd9Sstevel@tonic-gate 	}
69337c478bd9Sstevel@tonic-gate 
69347c478bd9Sstevel@tonic-gate 	iconv_src = source;
69357c478bd9Sstevel@tonic-gate 	iconv_trg = target;
69367c478bd9Sstevel@tonic-gate 	inlen = strlen(source);
69377c478bd9Sstevel@tonic-gate 	outlen = max_val * UTF_8_FACTOR;
69387c478bd9Sstevel@tonic-gate 	if (iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen) ==
69397c478bd9Sstevel@tonic-gate 	    (size_t)-1) {	/* Error occurred:  didn't convert */
69407c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
69417c478bd9Sstevel@tonic-gate 		    "tar:  file # %llu: (%s) UTF-8 conversion failed.\n"),
69427c478bd9Sstevel@tonic-gate 		    xhdr_count, source);
69437c478bd9Sstevel@tonic-gate 		/* Get remaining output; reinitialize conversion descriptor */
69447c478bd9Sstevel@tonic-gate 		iconv_src = (const char *)NULL;
69457c478bd9Sstevel@tonic-gate 		inlen = 0;
69467c478bd9Sstevel@tonic-gate 		(void) iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen);
69477c478bd9Sstevel@tonic-gate 		return (1);
69487c478bd9Sstevel@tonic-gate 	}
69497c478bd9Sstevel@tonic-gate 	/* Get remaining output; reinitialize conversion descriptor */
69507c478bd9Sstevel@tonic-gate 	iconv_src = (const char *)NULL;
69517c478bd9Sstevel@tonic-gate 	inlen = 0;
69527c478bd9Sstevel@tonic-gate 	if (iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen) ==
69537c478bd9Sstevel@tonic-gate 	    (size_t)-1) {	/* Error occurred:  didn't convert */
69547c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
69557c478bd9Sstevel@tonic-gate 		    "tar:  file # %llu: (%s) UTF-8 conversion failed.\n"),
69567c478bd9Sstevel@tonic-gate 		    xhdr_count, source);
69577c478bd9Sstevel@tonic-gate 		return (1);
69587c478bd9Sstevel@tonic-gate 	}
69597c478bd9Sstevel@tonic-gate 
69607c478bd9Sstevel@tonic-gate 	*iconv_trg = '\0';	/* Null-terminate iconv output string */
69617c478bd9Sstevel@tonic-gate 	if (strlen(target) > max_val) {
69627c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
69637c478bd9Sstevel@tonic-gate 		    "tar: file # %llu: Extended header %s too long.\n"),
69647c478bd9Sstevel@tonic-gate 		    xhdr_count, option);
69657c478bd9Sstevel@tonic-gate 		return (1);
69667c478bd9Sstevel@tonic-gate 	}
69677c478bd9Sstevel@tonic-gate 	*Xhdr_ptrptr = target;
69687c478bd9Sstevel@tonic-gate 	return (0);
69697c478bd9Sstevel@tonic-gate }
69707c478bd9Sstevel@tonic-gate 
69717c478bd9Sstevel@tonic-gate /*
69727c478bd9Sstevel@tonic-gate  * Check gname, uname, path, and linkpath to see if they need to go in an
69737c478bd9Sstevel@tonic-gate  * extended header.  If they are already slated to be in an extended header,
69747c478bd9Sstevel@tonic-gate  * or if they are not ascii, then they need to be in the extended header.
69757c478bd9Sstevel@tonic-gate  * Then, convert all extended names to UTF-8.
69767c478bd9Sstevel@tonic-gate  */
69777c478bd9Sstevel@tonic-gate 
69787c478bd9Sstevel@tonic-gate int
69797c478bd9Sstevel@tonic-gate gen_utf8_names(const char *filename)
69807c478bd9Sstevel@tonic-gate {
69817c478bd9Sstevel@tonic-gate 	static	iconv_t	iconv_cd;
69827c478bd9Sstevel@tonic-gate 	char		*nl_target;
69837c478bd9Sstevel@tonic-gate 	char		tempbuf[MAXNAM + 1];
6984eace40a5Sceastha 	int		nbytes;
6985eace40a5Sceastha 	int		errors;
69867c478bd9Sstevel@tonic-gate 
69877c478bd9Sstevel@tonic-gate 	if (charset_type == -1)	{	/* Previous failure to open. */
69887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
69897c478bd9Sstevel@tonic-gate 		    "tar: file # %llu: UTF-8 conversion failed.\n"),
69907c478bd9Sstevel@tonic-gate 		    xhdr_count);
69917c478bd9Sstevel@tonic-gate 		return (1);
69927c478bd9Sstevel@tonic-gate 	}
69937c478bd9Sstevel@tonic-gate 
69947c478bd9Sstevel@tonic-gate 	if (charset_type == 0) {	/* Need to get conversion descriptor */
69957c478bd9Sstevel@tonic-gate 		nl_target = nl_langinfo(CODESET);
69967c478bd9Sstevel@tonic-gate 		if (strlen(nl_target) == 0)	/* locale using 7-bit codeset */
69977c478bd9Sstevel@tonic-gate 			nl_target = "646";
69987c478bd9Sstevel@tonic-gate 		if (strcmp(nl_target, "646") == 0)
69997c478bd9Sstevel@tonic-gate 			charset_type = 1;
70007c478bd9Sstevel@tonic-gate 		else if (strcmp(nl_target, "UTF-8") == 0)
70017c478bd9Sstevel@tonic-gate 			charset_type = 3;
70027c478bd9Sstevel@tonic-gate 		else {
70037c478bd9Sstevel@tonic-gate 			if (strncmp(nl_target, "ISO", 3) == 0)
70047c478bd9Sstevel@tonic-gate 				nl_target += 3;
70057c478bd9Sstevel@tonic-gate 			charset_type = 2;
70067c478bd9Sstevel@tonic-gate 			errno = 0;
70077c478bd9Sstevel@tonic-gate #ifdef ICONV_DEBUG
70087c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
70097c478bd9Sstevel@tonic-gate 			    "Opening iconv_cd with target %s\n",
70107c478bd9Sstevel@tonic-gate 			    nl_target);
70117c478bd9Sstevel@tonic-gate #endif
70127c478bd9Sstevel@tonic-gate 			if ((iconv_cd = iconv_open("UTF-8", nl_target)) ==
70137c478bd9Sstevel@tonic-gate 			    (iconv_t)-1) {
70147c478bd9Sstevel@tonic-gate 				if (errno == EINVAL)
70157c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
70167c478bd9Sstevel@tonic-gate 					    "tar: conversion routines not "
70177c478bd9Sstevel@tonic-gate 					    "available for current locale.  "));
70187c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
70197c478bd9Sstevel@tonic-gate 				    "file (%s): UTF-8 conversion failed.\n"),
70207c478bd9Sstevel@tonic-gate 				    filename);
70217c478bd9Sstevel@tonic-gate 				charset_type = -1;
70227c478bd9Sstevel@tonic-gate 				return (1);
70237c478bd9Sstevel@tonic-gate 			}
70247c478bd9Sstevel@tonic-gate 		}
70257c478bd9Sstevel@tonic-gate 	}
70267c478bd9Sstevel@tonic-gate 
70277c478bd9Sstevel@tonic-gate 	errors = 0;
70287c478bd9Sstevel@tonic-gate 
70297c478bd9Sstevel@tonic-gate 	errors += local_utf8(&Xtarhdr.x_gname, local_gname,
70307c478bd9Sstevel@tonic-gate 	    dblock.dbuf.gname, iconv_cd, _X_GNAME, _POSIX_NAME_MAX);
70317c478bd9Sstevel@tonic-gate 	errors += local_utf8(&Xtarhdr.x_uname, local_uname,
70327c478bd9Sstevel@tonic-gate 	    dblock.dbuf.uname, iconv_cd, _X_UNAME,  _POSIX_NAME_MAX);
70337c478bd9Sstevel@tonic-gate 	if ((xhdr_flgs & _X_LINKPATH) == 0) {	/* Need null-terminated str. */
70347c478bd9Sstevel@tonic-gate 		(void) strncpy(tempbuf, dblock.dbuf.linkname, NAMSIZ);
70357c478bd9Sstevel@tonic-gate 		tempbuf[NAMSIZ] = '\0';
70367c478bd9Sstevel@tonic-gate 	}
70377c478bd9Sstevel@tonic-gate 	errors += local_utf8(&Xtarhdr.x_linkpath, local_linkpath,
70387c478bd9Sstevel@tonic-gate 	    tempbuf, iconv_cd, _X_LINKPATH, PATH_MAX);
70397c478bd9Sstevel@tonic-gate 	if ((xhdr_flgs & _X_PATH) == 0) {	/* Concatenate prefix & name */
70407c478bd9Sstevel@tonic-gate 		(void) strncpy(tempbuf, dblock.dbuf.prefix, PRESIZ);
70410578ac30Ssr161167 		tempbuf[PRESIZ] = '\0';
70427c478bd9Sstevel@tonic-gate 		nbytes = strlen(tempbuf);
70437c478bd9Sstevel@tonic-gate 		if (nbytes > 0) {
70447c478bd9Sstevel@tonic-gate 			tempbuf[nbytes++] = '/';
70457c478bd9Sstevel@tonic-gate 			tempbuf[nbytes] = '\0';
70467c478bd9Sstevel@tonic-gate 		}
70470578ac30Ssr161167 		(void) strncat(tempbuf + nbytes, dblock.dbuf.name,
70480578ac30Ssr161167 		    (MAXNAM - nbytes));
70490578ac30Ssr161167 		tempbuf[MAXNAM] = '\0';
70507c478bd9Sstevel@tonic-gate 	}
70517c478bd9Sstevel@tonic-gate 	errors += local_utf8(&Xtarhdr.x_path, local_path,
70527c478bd9Sstevel@tonic-gate 	    tempbuf, iconv_cd, _X_PATH, PATH_MAX);
70537c478bd9Sstevel@tonic-gate 
70547c478bd9Sstevel@tonic-gate 	if (errors > 0)
70557c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
70567c478bd9Sstevel@tonic-gate 		    "tar: file (%s): UTF-8 conversion failed.\n"), filename);
70577c478bd9Sstevel@tonic-gate 
70587c478bd9Sstevel@tonic-gate 	if (errors && errflag)
70597c478bd9Sstevel@tonic-gate 		done(1);
70607c478bd9Sstevel@tonic-gate 	else
70617c478bd9Sstevel@tonic-gate 		if (errors)
70627c478bd9Sstevel@tonic-gate 			Errflg = 1;
70637c478bd9Sstevel@tonic-gate 	return (errors);
70647c478bd9Sstevel@tonic-gate }
70657c478bd9Sstevel@tonic-gate 
70667c478bd9Sstevel@tonic-gate static int
70677c478bd9Sstevel@tonic-gate local_utf8(
70687c478bd9Sstevel@tonic-gate 		char	**Xhdr_ptrptr,
70697c478bd9Sstevel@tonic-gate 		char	*target,
70707c478bd9Sstevel@tonic-gate 		const	char	*source,
70717c478bd9Sstevel@tonic-gate 		iconv_t	iconv_cd,
70727c478bd9Sstevel@tonic-gate 		int	xhdrflg,
70737c478bd9Sstevel@tonic-gate 		int	max_val)
70747c478bd9Sstevel@tonic-gate {
70757c478bd9Sstevel@tonic-gate 	const	char	*iconv_src;
70767c478bd9Sstevel@tonic-gate 	const	char	*starting_src;
70777c478bd9Sstevel@tonic-gate 	char		*iconv_trg;
7078eace40a5Sceastha 	size_t		inlen;
7079eace40a5Sceastha 	size_t		outlen;
70807c478bd9Sstevel@tonic-gate #ifdef ICONV_DEBUG
70817c478bd9Sstevel@tonic-gate 	unsigned char	c_to_hex;
70827c478bd9Sstevel@tonic-gate #endif
70837c478bd9Sstevel@tonic-gate 
70847c478bd9Sstevel@tonic-gate 	/*
70857c478bd9Sstevel@tonic-gate 	 * If the item is already slated for extended format, get the string
70867c478bd9Sstevel@tonic-gate 	 * to convert from the extended header record.  Otherwise, get it from
70877c478bd9Sstevel@tonic-gate 	 * the regular (dblock) area.
70887c478bd9Sstevel@tonic-gate 	 */
70897c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & xhdrflg) {
70907c478bd9Sstevel@tonic-gate 		if (charset_type == 3) {	/* Already UTF-8, just copy */
70917c478bd9Sstevel@tonic-gate 			(void) strcpy(target, *Xhdr_ptrptr);
70927c478bd9Sstevel@tonic-gate 			*Xhdr_ptrptr = target;
70937c478bd9Sstevel@tonic-gate 			return (0);
70947c478bd9Sstevel@tonic-gate 		} else
70957c478bd9Sstevel@tonic-gate 			iconv_src = (const char *) *Xhdr_ptrptr;
70967c478bd9Sstevel@tonic-gate 	} else {
70977c478bd9Sstevel@tonic-gate 		if (charset_type == 3)		/* Already in UTF-8 format */
70987c478bd9Sstevel@tonic-gate 			return (0);		/* Don't create xhdr record */
70997c478bd9Sstevel@tonic-gate 		iconv_src = source;
71007c478bd9Sstevel@tonic-gate 	}
71017c478bd9Sstevel@tonic-gate 	starting_src = iconv_src;
71027c478bd9Sstevel@tonic-gate 	iconv_trg = target;
71037c478bd9Sstevel@tonic-gate 	if ((inlen = strlen(iconv_src)) == 0)
71047c478bd9Sstevel@tonic-gate 		return (0);
71057c478bd9Sstevel@tonic-gate 
71067c478bd9Sstevel@tonic-gate 	if (charset_type == 1) {	/* locale using 7-bit codeset */
71077c478bd9Sstevel@tonic-gate 		if (c_utf8(target, starting_src) != 0) {
71087c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
71097c478bd9Sstevel@tonic-gate 			    gettext("tar: invalid character in"
71107c478bd9Sstevel@tonic-gate 			    " UTF-8 conversion of '%s'\n"), starting_src);
71117c478bd9Sstevel@tonic-gate 			return (1);
71127c478bd9Sstevel@tonic-gate 		}
71137c478bd9Sstevel@tonic-gate 		return (0);
71147c478bd9Sstevel@tonic-gate 	}
71157c478bd9Sstevel@tonic-gate 
71167c478bd9Sstevel@tonic-gate 	outlen = max_val * UTF_8_FACTOR;
71177c478bd9Sstevel@tonic-gate 	errno = 0;
71187c478bd9Sstevel@tonic-gate 	if (iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen) ==
71197c478bd9Sstevel@tonic-gate 	    (size_t)-1) {
71207c478bd9Sstevel@tonic-gate 		/* An error occurred, or not all characters were converted */
71217c478bd9Sstevel@tonic-gate 		if (errno == EILSEQ)
71227c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
71237c478bd9Sstevel@tonic-gate 			    gettext("tar: invalid character in"
71247c478bd9Sstevel@tonic-gate 			    " UTF-8 conversion of '%s'\n"), starting_src);
71257c478bd9Sstevel@tonic-gate 		else
71267c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
71277c478bd9Sstevel@tonic-gate 			    "tar: conversion to UTF-8 aborted for '%s'.\n"),
71287c478bd9Sstevel@tonic-gate 			    starting_src);
71297c478bd9Sstevel@tonic-gate 		/* Get remaining output; reinitialize conversion descriptor */
71307c478bd9Sstevel@tonic-gate 		iconv_src = (const char *)NULL;
71317c478bd9Sstevel@tonic-gate 		inlen = 0;
71327c478bd9Sstevel@tonic-gate 		(void) iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen);
71337c478bd9Sstevel@tonic-gate 		return (1);
71347c478bd9Sstevel@tonic-gate 	}
71357c478bd9Sstevel@tonic-gate 	/* Get remaining output; reinitialize conversion descriptor */
71367c478bd9Sstevel@tonic-gate 	iconv_src = (const char *)NULL;
71377c478bd9Sstevel@tonic-gate 	inlen = 0;
71387c478bd9Sstevel@tonic-gate 	if (iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen) ==
71397c478bd9Sstevel@tonic-gate 	    (size_t)-1) {	/* Error occurred:  didn't convert */
71407c478bd9Sstevel@tonic-gate 		if (errno == EILSEQ)
71417c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
71427c478bd9Sstevel@tonic-gate 			    gettext("tar: invalid character in"
71437c478bd9Sstevel@tonic-gate 			    " UTF-8 conversion of '%s'\n"), starting_src);
71447c478bd9Sstevel@tonic-gate 		else
71457c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
71467c478bd9Sstevel@tonic-gate 			    "tar: conversion to UTF-8 aborted for '%s'.\n"),
71477c478bd9Sstevel@tonic-gate 			    starting_src);
71487c478bd9Sstevel@tonic-gate 		return (1);
71497c478bd9Sstevel@tonic-gate 	}
71507c478bd9Sstevel@tonic-gate 
71517c478bd9Sstevel@tonic-gate 	*iconv_trg = '\0';	/* Null-terminate iconv output string */
71527c478bd9Sstevel@tonic-gate 	if (strcmp(starting_src, target) != 0) {
71537c478bd9Sstevel@tonic-gate 		*Xhdr_ptrptr = target;
71547c478bd9Sstevel@tonic-gate 		xhdr_flgs |= xhdrflg;
71557c478bd9Sstevel@tonic-gate #ifdef ICONV_DEBUG
71567c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "***  inlen: %d %d; outlen: %d %d\n",
71577c478bd9Sstevel@tonic-gate 		    strlen(starting_src), inlen, max_val, outlen);
71587c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Input string:\n  ");
71597c478bd9Sstevel@tonic-gate 		for (inlen = 0; inlen < strlen(starting_src); inlen++) {
71607c478bd9Sstevel@tonic-gate 			c_to_hex = (unsigned char)starting_src[inlen];
71617c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, " %2.2x", c_to_hex);
71627c478bd9Sstevel@tonic-gate 			if (inlen % 20 == 19)
71637c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\n  ");
71647c478bd9Sstevel@tonic-gate 		}
71657c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\nOutput string:\n  ");
71667c478bd9Sstevel@tonic-gate 		for (inlen = 0; inlen < strlen(target); inlen++) {
71677c478bd9Sstevel@tonic-gate 			c_to_hex = (unsigned char)target[inlen];
71687c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, " %2.2x", c_to_hex);
71697c478bd9Sstevel@tonic-gate 			if (inlen % 20 == 19)
71707c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\n  ");
71717c478bd9Sstevel@tonic-gate 		}
71727c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
71737c478bd9Sstevel@tonic-gate #endif
71747c478bd9Sstevel@tonic-gate 	}
71757c478bd9Sstevel@tonic-gate 
71767c478bd9Sstevel@tonic-gate 	return (0);
71777c478bd9Sstevel@tonic-gate }
71787c478bd9Sstevel@tonic-gate 
71797c478bd9Sstevel@tonic-gate /*
71807c478bd9Sstevel@tonic-gate  *	Function to test each byte of the source string to make sure it is
71817c478bd9Sstevel@tonic-gate  *	in within bounds (value between 0 and 127).
71827c478bd9Sstevel@tonic-gate  *	If valid, copy source to target.
71837c478bd9Sstevel@tonic-gate  */
71847c478bd9Sstevel@tonic-gate 
71857c478bd9Sstevel@tonic-gate int
71867c478bd9Sstevel@tonic-gate c_utf8(char *target, const char *source)
71877c478bd9Sstevel@tonic-gate {
71887c478bd9Sstevel@tonic-gate 	size_t		len;
71897c478bd9Sstevel@tonic-gate 	const char	*thischar;
71907c478bd9Sstevel@tonic-gate 
71917c478bd9Sstevel@tonic-gate 	len = strlen(source);
71927c478bd9Sstevel@tonic-gate 	thischar = source;
71937c478bd9Sstevel@tonic-gate 	while (len-- > 0) {
71947c478bd9Sstevel@tonic-gate 		if (!isascii((int)(*thischar++)))
71957c478bd9Sstevel@tonic-gate 			return (1);
71967c478bd9Sstevel@tonic-gate 	}
71977c478bd9Sstevel@tonic-gate 
71987c478bd9Sstevel@tonic-gate 	(void) strcpy(target, source);
71997c478bd9Sstevel@tonic-gate 	return (0);
72007c478bd9Sstevel@tonic-gate }
72017c478bd9Sstevel@tonic-gate 
72027c478bd9Sstevel@tonic-gate 
72037c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
72047c478bd9Sstevel@tonic-gate #define	ROUNDTOTBLOCK(a)	((a + (TBLOCK -1)) & ~(TBLOCK -1))
72057c478bd9Sstevel@tonic-gate 
72067c478bd9Sstevel@tonic-gate static void
72077c478bd9Sstevel@tonic-gate prepare_xattr(
72087c478bd9Sstevel@tonic-gate 	char		**attrbuf,
72097c478bd9Sstevel@tonic-gate 	char		*filename,
7210*da6c28aaSamw 	char		*attrpath,
72117c478bd9Sstevel@tonic-gate 	char		typeflag,
72127c478bd9Sstevel@tonic-gate 	struct linkbuf	*linkinfo,
72137c478bd9Sstevel@tonic-gate 	int		*rlen)
72147c478bd9Sstevel@tonic-gate {
72157c478bd9Sstevel@tonic-gate 	char			*bufhead;	/* ptr to full buffer */
7216*da6c28aaSamw 	char			*aptr;
72177c478bd9Sstevel@tonic-gate 	struct xattr_hdr 	*hptr;		/* ptr to header in bufhead */
72187c478bd9Sstevel@tonic-gate 	struct xattr_buf	*tptr;		/* ptr to pathing pieces */
72197c478bd9Sstevel@tonic-gate 	int			totalen;	/* total buffer length */
72207c478bd9Sstevel@tonic-gate 	int			len;		/* length returned to user */
72217c478bd9Sstevel@tonic-gate 	int			stringlen;	/* length of filename + attr */
72227c478bd9Sstevel@tonic-gate 						/*
72237c478bd9Sstevel@tonic-gate 						 * length of filename + attr
72247c478bd9Sstevel@tonic-gate 						 * in link section
72257c478bd9Sstevel@tonic-gate 						 */
72267c478bd9Sstevel@tonic-gate 	int			linkstringlen;
72277c478bd9Sstevel@tonic-gate 	int			complen;	/* length of pathing section */
72287c478bd9Sstevel@tonic-gate 	int			linklen;	/* length of link section */
7229*da6c28aaSamw 	int			attrnames_index; /* attrnames starting index */
72307c478bd9Sstevel@tonic-gate 
72317c478bd9Sstevel@tonic-gate 	/*
72327c478bd9Sstevel@tonic-gate 	 * Release previous buffer
72337c478bd9Sstevel@tonic-gate 	 */
72347c478bd9Sstevel@tonic-gate 
72357c478bd9Sstevel@tonic-gate 	if (*attrbuf != (char *)NULL) {
72367c478bd9Sstevel@tonic-gate 		free(*attrbuf);
72377c478bd9Sstevel@tonic-gate 		*attrbuf = NULL;
72387c478bd9Sstevel@tonic-gate 	}
72397c478bd9Sstevel@tonic-gate 
72407c478bd9Sstevel@tonic-gate 	/*
72417c478bd9Sstevel@tonic-gate 	 * First add in fixed size stuff
72427c478bd9Sstevel@tonic-gate 	 */
72437c478bd9Sstevel@tonic-gate 	len = sizeof (struct xattr_hdr) + sizeof (struct xattr_buf);
72447c478bd9Sstevel@tonic-gate 
72457c478bd9Sstevel@tonic-gate 	/*
72467c478bd9Sstevel@tonic-gate 	 * Add space for two nulls
72477c478bd9Sstevel@tonic-gate 	 */
7248*da6c28aaSamw 	stringlen = strlen(attrpath) + strlen(filename) + 2;
72497c478bd9Sstevel@tonic-gate 	complen = stringlen + sizeof (struct xattr_buf);
72507c478bd9Sstevel@tonic-gate 
72517c478bd9Sstevel@tonic-gate 	len += stringlen;
72527c478bd9Sstevel@tonic-gate 
72537c478bd9Sstevel@tonic-gate 	/*
72547c478bd9Sstevel@tonic-gate 	 * Now add on space for link info if any
72557c478bd9Sstevel@tonic-gate 	 */
72567c478bd9Sstevel@tonic-gate 
72577c478bd9Sstevel@tonic-gate 	if (linkinfo != NULL) {
72587c478bd9Sstevel@tonic-gate 		/*
72597c478bd9Sstevel@tonic-gate 		 * Again add space for two nulls
72607c478bd9Sstevel@tonic-gate 		 */
72617c478bd9Sstevel@tonic-gate 		linkstringlen = strlen(linkinfo->pathname) +
72627c478bd9Sstevel@tonic-gate 		    strlen(linkinfo->attrname) + 2;
7263*da6c28aaSamw 		linklen = linkstringlen + sizeof (struct xattr_buf);
7264*da6c28aaSamw 		len += linklen;
7265*da6c28aaSamw 	} else {
7266*da6c28aaSamw 		linklen = 0;
72677c478bd9Sstevel@tonic-gate 	}
72687c478bd9Sstevel@tonic-gate 
72697c478bd9Sstevel@tonic-gate 	/*
72707c478bd9Sstevel@tonic-gate 	 * Now add padding to end to fill out TBLOCK
72717c478bd9Sstevel@tonic-gate 	 *
72727c478bd9Sstevel@tonic-gate 	 * Function returns size of real data and not size + padding.
72737c478bd9Sstevel@tonic-gate 	 */
72747c478bd9Sstevel@tonic-gate 
72757c478bd9Sstevel@tonic-gate 	totalen = ROUNDTOTBLOCK(len);
72767c478bd9Sstevel@tonic-gate 
72777c478bd9Sstevel@tonic-gate 	if ((bufhead = calloc(1, totalen)) == NULL) {
72787c478bd9Sstevel@tonic-gate 		fatal(gettext("Out of memory."));
72797c478bd9Sstevel@tonic-gate 	}
72807c478bd9Sstevel@tonic-gate 
72817c478bd9Sstevel@tonic-gate 
72827c478bd9Sstevel@tonic-gate 	/*
72837c478bd9Sstevel@tonic-gate 	 * Now we can fill in the necessary pieces
72847c478bd9Sstevel@tonic-gate 	 */
72857c478bd9Sstevel@tonic-gate 
72867c478bd9Sstevel@tonic-gate 	/*
72877c478bd9Sstevel@tonic-gate 	 * first fill in the fixed header
72887c478bd9Sstevel@tonic-gate 	 */
72897c478bd9Sstevel@tonic-gate 	hptr = (struct xattr_hdr *)bufhead;
72907c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_version, "%s", XATTR_ARCH_VERS);
72917c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_component_len, "%0*d",
72927c478bd9Sstevel@tonic-gate 	    sizeof (hptr->h_component_len) - 1, complen);
72937c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_link_component_len, "%0*d",
72947c478bd9Sstevel@tonic-gate 	    sizeof (hptr->h_link_component_len) - 1, linklen);
72957c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_size, "%0*d", sizeof (hptr->h_size) - 1, len);
72967c478bd9Sstevel@tonic-gate 
72977c478bd9Sstevel@tonic-gate 	/*
72987c478bd9Sstevel@tonic-gate 	 * Now fill in the filename + attrnames section
7299*da6c28aaSamw 	 * The filename and attrnames section can be composed of two or more
7300*da6c28aaSamw 	 * path segments separated by a null character.  The first segment
7301*da6c28aaSamw 	 * is the path to the parent file that roots the entire sequence in
7302*da6c28aaSamw 	 * the normal name space. The remaining segments describes a path
7303*da6c28aaSamw 	 * rooted at the hidden extended attribute directory of the leaf file of
7304*da6c28aaSamw 	 * the previous segment, making it possible to name attributes on
7305*da6c28aaSamw 	 * attributes.  Thus, if we are just archiving an extended attribute,
7306*da6c28aaSamw 	 * the second segment will contain the attribute name.  If we are
7307*da6c28aaSamw 	 * archiving a system attribute of an extended attribute, then the
7308*da6c28aaSamw 	 * second segment will contain the attribute name, and a third segment
7309*da6c28aaSamw 	 * will contain the system attribute name.  The attribute pathing
7310*da6c28aaSamw 	 * information is obtained from 'attrpath'.
73117c478bd9Sstevel@tonic-gate 	 */
73127c478bd9Sstevel@tonic-gate 
73137c478bd9Sstevel@tonic-gate 	tptr = (struct xattr_buf *)(bufhead + sizeof (struct xattr_hdr));
73147c478bd9Sstevel@tonic-gate 	(void) sprintf(tptr->h_namesz, "%0*d", sizeof (tptr->h_namesz) - 1,
73157c478bd9Sstevel@tonic-gate 	    stringlen);
73167c478bd9Sstevel@tonic-gate 	(void) strcpy(tptr->h_names, filename);
7317*da6c28aaSamw 	attrnames_index = strlen(filename) + 1;
7318*da6c28aaSamw 	(void) strcpy(&tptr->h_names[attrnames_index], attrpath);
73197c478bd9Sstevel@tonic-gate 	tptr->h_typeflag = typeflag;
73207c478bd9Sstevel@tonic-gate 
73217c478bd9Sstevel@tonic-gate 	/*
7322*da6c28aaSamw 	 * Split the attrnames section into two segments if 'attrpath'
7323*da6c28aaSamw 	 * contains pathing information for a system attribute of an
7324*da6c28aaSamw 	 * extended attribute.  We split them by replacing the '/' with
7325*da6c28aaSamw 	 * a '\0'.
7326*da6c28aaSamw 	 */
7327*da6c28aaSamw 	if ((aptr = strpbrk(&tptr->h_names[attrnames_index], "/")) != NULL) {
7328*da6c28aaSamw 		*aptr = '\0';
7329*da6c28aaSamw 	}
7330*da6c28aaSamw 
7331*da6c28aaSamw 	/*
73327c478bd9Sstevel@tonic-gate 	 * Now fill in the optional link section if we have one
73337c478bd9Sstevel@tonic-gate 	 */
73347c478bd9Sstevel@tonic-gate 
73357c478bd9Sstevel@tonic-gate 	if (linkinfo != (struct linkbuf *)NULL) {
73367c478bd9Sstevel@tonic-gate 		tptr = (struct xattr_buf *)(bufhead +
73377c478bd9Sstevel@tonic-gate 		    sizeof (struct xattr_hdr) + complen);
73387c478bd9Sstevel@tonic-gate 
73397c478bd9Sstevel@tonic-gate 		(void) sprintf(tptr->h_namesz, "%0*d",
73407c478bd9Sstevel@tonic-gate 		    sizeof (tptr->h_namesz) - 1, linkstringlen);
73417c478bd9Sstevel@tonic-gate 		(void) strcpy(tptr->h_names, linkinfo->pathname);
73427c478bd9Sstevel@tonic-gate 		(void) strcpy(
73437c478bd9Sstevel@tonic-gate 		    &tptr->h_names[strlen(linkinfo->pathname) + 1],
73447c478bd9Sstevel@tonic-gate 		    linkinfo->attrname);
73457c478bd9Sstevel@tonic-gate 		tptr->h_typeflag = typeflag;
73467c478bd9Sstevel@tonic-gate 	}
73477c478bd9Sstevel@tonic-gate 	*attrbuf = (char *)bufhead;
73487c478bd9Sstevel@tonic-gate 	*rlen = len;
73497c478bd9Sstevel@tonic-gate }
73507c478bd9Sstevel@tonic-gate 
73517c478bd9Sstevel@tonic-gate #else
73527c478bd9Sstevel@tonic-gate static void
73537c478bd9Sstevel@tonic-gate prepare_xattr(
73547c478bd9Sstevel@tonic-gate 	char		**attrbuf,
73557c478bd9Sstevel@tonic-gate 	char		*filename,
73567c478bd9Sstevel@tonic-gate 	char		*attrname,
73577c478bd9Sstevel@tonic-gate 	char		typeflag,
73587c478bd9Sstevel@tonic-gate 	struct linkbuf	*linkinfo,
73597c478bd9Sstevel@tonic-gate 	int		*rlen)
73607c478bd9Sstevel@tonic-gate {
73617c478bd9Sstevel@tonic-gate 	*attrbuf = NULL;
73627c478bd9Sstevel@tonic-gate 	*rlen = 0;
73637c478bd9Sstevel@tonic-gate }
73647c478bd9Sstevel@tonic-gate #endif
73657c478bd9Sstevel@tonic-gate 
73667c478bd9Sstevel@tonic-gate int
7367*da6c28aaSamw getstat(int dirfd, char *longname, char *shortname, char *attrparent)
73687c478bd9Sstevel@tonic-gate {
73697c478bd9Sstevel@tonic-gate 
73707c478bd9Sstevel@tonic-gate 	int i, j;
73717c478bd9Sstevel@tonic-gate 	int	printerr;
73727c478bd9Sstevel@tonic-gate 	int	slnkerr;
73737c478bd9Sstevel@tonic-gate 	struct stat symlnbuf;
73747c478bd9Sstevel@tonic-gate 
73757c478bd9Sstevel@tonic-gate 	if (!hflag)
73767c478bd9Sstevel@tonic-gate 		i = fstatat(dirfd, shortname, &stbuf, AT_SYMLINK_NOFOLLOW);
73777c478bd9Sstevel@tonic-gate 	else
73787c478bd9Sstevel@tonic-gate 		i = fstatat(dirfd, shortname, &stbuf, 0);
73797c478bd9Sstevel@tonic-gate 
73807c478bd9Sstevel@tonic-gate 	if (i < 0) {
73817c478bd9Sstevel@tonic-gate 		/* Initialize flag to print error mesg. */
73827c478bd9Sstevel@tonic-gate 		printerr = 1;
73837c478bd9Sstevel@tonic-gate 		/*
73847c478bd9Sstevel@tonic-gate 		 * If stat is done, then need to do lstat
73857c478bd9Sstevel@tonic-gate 		 * to determine whether it's a sym link
73867c478bd9Sstevel@tonic-gate 		 */
73877c478bd9Sstevel@tonic-gate 		if (hflag) {
73887c478bd9Sstevel@tonic-gate 			/* Save returned error */
73897c478bd9Sstevel@tonic-gate 			slnkerr = errno;
73907c478bd9Sstevel@tonic-gate 
73917c478bd9Sstevel@tonic-gate 			j = fstatat(dirfd, shortname,
73927c478bd9Sstevel@tonic-gate 			    &symlnbuf, AT_SYMLINK_NOFOLLOW);
73937c478bd9Sstevel@tonic-gate 			/*
73947c478bd9Sstevel@tonic-gate 			 * Suppress error message when file is a symbolic link
73957c478bd9Sstevel@tonic-gate 			 * and function modifier 'l' is off.  Exception:  when
73967c478bd9Sstevel@tonic-gate 			 * a symlink points to a symlink points to a
73977c478bd9Sstevel@tonic-gate 			 * symlink ... and we get past MAXSYMLINKS.  That
73987c478bd9Sstevel@tonic-gate 			 * error will cause a file not to be archived, and
73997c478bd9Sstevel@tonic-gate 			 * needs to be printed.
74007c478bd9Sstevel@tonic-gate 			 */
74017c478bd9Sstevel@tonic-gate 			if ((j == 0) && (!linkerrok) && (slnkerr != ELOOP) &&
74027c478bd9Sstevel@tonic-gate 			    (S_ISLNK(symlnbuf.st_mode)))
74037c478bd9Sstevel@tonic-gate 				printerr = 0;
74047c478bd9Sstevel@tonic-gate 
74057c478bd9Sstevel@tonic-gate 			/*
74067c478bd9Sstevel@tonic-gate 			 * Restore errno in case the lstat
74077c478bd9Sstevel@tonic-gate 			 * on symbolic link change
74087c478bd9Sstevel@tonic-gate 			 */
74097c478bd9Sstevel@tonic-gate 			errno = slnkerr;
74107c478bd9Sstevel@tonic-gate 		}
74117c478bd9Sstevel@tonic-gate 
74127c478bd9Sstevel@tonic-gate 		if (printerr) {
74137c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
7414*da6c28aaSamw 			    "tar: %s%s%s%s: %s\n"),
7415*da6c28aaSamw 			    (attrparent == NULL) ? "" : gettext("attribute "),
7416*da6c28aaSamw 			    (attrparent == NULL) ? "" : attrparent,
7417*da6c28aaSamw 			    (attrparent == NULL) ? "" : gettext(" of "),
7418*da6c28aaSamw 			    longname, strerror(errno));
74197c478bd9Sstevel@tonic-gate 			Errflg = 1;
74207c478bd9Sstevel@tonic-gate 		}
74217c478bd9Sstevel@tonic-gate 		return (1);
74227c478bd9Sstevel@tonic-gate 	}
74237c478bd9Sstevel@tonic-gate 	return (0);
74247c478bd9Sstevel@tonic-gate }
74257c478bd9Sstevel@tonic-gate 
7426*da6c28aaSamw /*
7427*da6c28aaSamw  * Recursively archive the extended attributes and/or extended system attributes
7428*da6c28aaSamw  * of the base file, longname.  Note:  extended system attribute files will be
7429*da6c28aaSamw  * archived only if the extended system attributes are not transient (i.e. the
7430*da6c28aaSamw  * extended system attributes are other than the default values).
7431*da6c28aaSamw  *
7432*da6c28aaSamw  * If -@ was specified and the underlying file system supports it, archive the
7433*da6c28aaSamw  * extended attributes, and if there is a system attribute associated with the
7434*da6c28aaSamw  * extended attribute, then recursively call xattrs_put() to archive the
7435*da6c28aaSamw  * hidden attribute directory and the extended system attribute.  If -/ was
7436*da6c28aaSamw  * specified and the underlying file system supports it, archive the extended
7437*da6c28aaSamw  * system attributes.  Read-only extended system attributes are never archived.
7438*da6c28aaSamw  *
7439*da6c28aaSamw  * Currently, there cannot be attributes on attributes; only system
7440*da6c28aaSamw  * attributes on attributes.  In addition, there cannot be attributes on
7441*da6c28aaSamw  * system attributes.  A file and it's attribute directory hierarchy looks as
7442*da6c28aaSamw  * follows:
7443*da6c28aaSamw  *	longname ---->	.	("." is the hidden attribute directory)
7444*da6c28aaSamw  *			|
7445*da6c28aaSamw  *	     ----------------------------
7446*da6c28aaSamw  *	     |				|
7447*da6c28aaSamw  *	<sys_attr_name>		   <attr_name> ---->	.
7448*da6c28aaSamw  *							|
7449*da6c28aaSamw  *						  <sys_attr_name>
7450*da6c28aaSamw  *
7451*da6c28aaSamw  */
74527c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
74537c478bd9Sstevel@tonic-gate static void
7454*da6c28aaSamw xattrs_put(char *longname, char *shortname, char *parent, char *attrparent)
74557c478bd9Sstevel@tonic-gate {
7456*da6c28aaSamw 	char *filename = (attrparent == NULL) ? shortname : attrparent;
7457*da6c28aaSamw 	int arc_rwsysattr = 0;
74587c478bd9Sstevel@tonic-gate 	int dirfd;
7459*da6c28aaSamw 	int fd = -1;
7460*da6c28aaSamw 	int rw_sysattr = 0;
7461*da6c28aaSamw 	int rc;
74627c478bd9Sstevel@tonic-gate 	DIR *dirp;
74637c478bd9Sstevel@tonic-gate 	struct dirent *dp;
7464*da6c28aaSamw 	attr_data_t *attrinfo = NULL;
74657c478bd9Sstevel@tonic-gate 
7466*da6c28aaSamw 	/*
7467*da6c28aaSamw 	 * If the underlying file system supports it, then archive the extended
7468*da6c28aaSamw 	 * attributes if -@ was specified, and the extended system attributes
7469*da6c28aaSamw 	 * if -/ was specified.
7470*da6c28aaSamw 	 */
7471*da6c28aaSamw 	if (verify_attr_support(filename, ARC_CREATE) != ATTR_OK) {
74727c478bd9Sstevel@tonic-gate 		return;
74737c478bd9Sstevel@tonic-gate 	}
74747c478bd9Sstevel@tonic-gate 
7475*da6c28aaSamw 	/*
7476*da6c28aaSamw 	 * Only want to archive a read-write extended system attribute file
7477*da6c28aaSamw 	 * if it contains extended system attribute settings that are not the
7478*da6c28aaSamw 	 * default values.
7479*da6c28aaSamw 	 */
7480*da6c28aaSamw #if defined(_PC_SATTR_ENABLED)
7481*da6c28aaSamw 	if (saflag) {
7482*da6c28aaSamw 		int	filefd;
7483*da6c28aaSamw 		nvlist_t *slist = NULL;
7484*da6c28aaSamw 
7485*da6c28aaSamw 		/* Determine if there are non-transient system attributes */
7486*da6c28aaSamw 		errno = 0;
7487*da6c28aaSamw 		if ((filefd = open(filename, O_RDONLY)) == -1) {
7488*da6c28aaSamw 			if (attrparent == NULL) {
7489*da6c28aaSamw 				vperror(0, gettext(
7490*da6c28aaSamw 				    "unable to open file %s"), longname);
7491*da6c28aaSamw 			}
7492*da6c28aaSamw 			return;
7493*da6c28aaSamw 		}
7494*da6c28aaSamw 		if (((slist = sysattr_list(basename(myname), filefd,
7495*da6c28aaSamw 		    filename)) != NULL) || (errno != 0)) {
7496*da6c28aaSamw 			arc_rwsysattr = 1;
7497*da6c28aaSamw 		}
7498*da6c28aaSamw 		if (slist != NULL) {
7499*da6c28aaSamw 			(void) nvlist_free(slist);
7500*da6c28aaSamw 			slist = NULL;
7501*da6c28aaSamw 		}
7502*da6c28aaSamw 		(void) close(filefd);
7503*da6c28aaSamw 	}
7504*da6c28aaSamw #endif	/* _PC_SATTR_ENABLED */
7505*da6c28aaSamw 
7506*da6c28aaSamw 	/* open the parent attribute directory */
7507*da6c28aaSamw 	fd = attropen(filename, ".", O_RDONLY);
7508*da6c28aaSamw 	if (fd < 0) {
7509*da6c28aaSamw 		vperror(0, gettext(
7510*da6c28aaSamw 		    "unable to open attribute directory for %s%s%sfile %s"),
7511*da6c28aaSamw 		    (attrparent == NULL) ? "" : gettext("attribute "),
7512*da6c28aaSamw 		    (attrparent == NULL) ? "" : attrparent,
7513*da6c28aaSamw 		    (attrparent == NULL) ? "" : gettext(" of "),
75147c478bd9Sstevel@tonic-gate 		    longname);
75157c478bd9Sstevel@tonic-gate 		return;
75167c478bd9Sstevel@tonic-gate 	}
75177c478bd9Sstevel@tonic-gate 
7518*da6c28aaSamw 	/*
7519*da6c28aaSamw 	 * We need to change into the parent's attribute directory to determine
7520*da6c28aaSamw 	 * if each of the attributes should be archived.
7521*da6c28aaSamw 	 */
7522*da6c28aaSamw 	if (fchdir(fd) < 0) {
7523*da6c28aaSamw 		vperror(0, gettext(
7524*da6c28aaSamw 		    "cannot change to attribute directory of %s%s%sfile %s"),
7525*da6c28aaSamw 		    (attrparent == NULL) ? "" : gettext("attribute "),
7526*da6c28aaSamw 		    (attrparent == NULL) ? "" : attrparent,
7527*da6c28aaSamw 		    (attrparent == NULL) ? "" : gettext(" of "),
7528*da6c28aaSamw 		    longname);
7529*da6c28aaSamw 		(void) close(fd);
7530*da6c28aaSamw 		return;
7531*da6c28aaSamw 	}
7532*da6c28aaSamw 
7533*da6c28aaSamw 	if (((dirfd = dup(fd)) == -1) ||
7534*da6c28aaSamw 	    ((dirp = fdopendir(dirfd)) == NULL)) {
75357c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
7536*da6c28aaSamw 		    "tar: unable to open dir pointer for %s%s%sfile %s\n"),
7537*da6c28aaSamw 		    (attrparent == NULL) ? "" : gettext("attribute "),
7538*da6c28aaSamw 		    (attrparent == NULL) ? "" : attrparent,
7539*da6c28aaSamw 		    (attrparent == NULL) ? "" : gettext(" of "),
7540*da6c28aaSamw 		    longname);
7541*da6c28aaSamw 		if (fd > 0) {
7542*da6c28aaSamw 			(void) close(fd);
7543*da6c28aaSamw 		}
75447c478bd9Sstevel@tonic-gate 		return;
75457c478bd9Sstevel@tonic-gate 	}
75467c478bd9Sstevel@tonic-gate 
75477c478bd9Sstevel@tonic-gate 	while (dp = readdir(dirp)) {
7548*da6c28aaSamw 		if (strcmp(dp->d_name, "..") == 0) {
75497c478bd9Sstevel@tonic-gate 			continue;
7550*da6c28aaSamw 		} else if (strcmp(dp->d_name, ".") == 0) {
75517c478bd9Sstevel@tonic-gate 			Hiddendir = 1;
7552*da6c28aaSamw 		} else {
75537c478bd9Sstevel@tonic-gate 			Hiddendir = 0;
7554*da6c28aaSamw 		}
75557c478bd9Sstevel@tonic-gate 
7556*da6c28aaSamw 		/* Determine if this attribute should be archived */
7557*da6c28aaSamw 		if (verify_attr(dp->d_name, attrparent, arc_rwsysattr,
7558*da6c28aaSamw 		    &rw_sysattr) != ATTR_OK) {
7559*da6c28aaSamw 			continue;
7560*da6c28aaSamw 		}
7561*da6c28aaSamw 
7562*da6c28aaSamw 		/* gather the attribute's information to pass to putfile() */
7563*da6c28aaSamw 		if ((fill_in_attr_info(dp->d_name, longname, attrparent,
7564*da6c28aaSamw 		    fd, rw_sysattr, &attrinfo)) == 1) {
7565*da6c28aaSamw 			continue;
7566*da6c28aaSamw 		}
7567*da6c28aaSamw 
7568*da6c28aaSamw 		/* add the attribute to the archive */
7569*da6c28aaSamw 		rc = putfile(longname, dp->d_name, parent, attrinfo,
75707c478bd9Sstevel@tonic-gate 		    XATTR_FILE, LEV0, SYMLINK_LEV0);
75717c478bd9Sstevel@tonic-gate 
7572*da6c28aaSamw 		if (exitflag) {
75737c478bd9Sstevel@tonic-gate 			break;
75747c478bd9Sstevel@tonic-gate 		}
75757c478bd9Sstevel@tonic-gate 
7576*da6c28aaSamw #if defined(_PC_SATTR_ENABLED)
7577*da6c28aaSamw 		/*
7578*da6c28aaSamw 		 * If both -/ and -@ were specified, then archive the
7579*da6c28aaSamw 		 * attribute's extended system attributes and hidden directory
7580*da6c28aaSamw 		 * by making a recursive call to xattrs_put().
7581*da6c28aaSamw 		 */
7582*da6c28aaSamw 		if (!rw_sysattr && saflag && atflag && (rc != PUT_AS_LINK) &&
7583*da6c28aaSamw 		    (Hiddendir == 0)) {
7584*da6c28aaSamw 
7585*da6c28aaSamw 			xattrs_put(longname, shortname, parent, dp->d_name);
7586*da6c28aaSamw 
7587*da6c28aaSamw 			/*
7588*da6c28aaSamw 			 * Change back to the parent's attribute directory
7589*da6c28aaSamw 			 * to process any further attributes.
7590*da6c28aaSamw 			 */
7591*da6c28aaSamw 			if (fchdir(fd) < 0) {
7592*da6c28aaSamw 				vperror(0, gettext(
7593*da6c28aaSamw 				    "cannot change back to attribute directory "
7594*da6c28aaSamw 				    "of file %s"), longname);
7595*da6c28aaSamw 				break;
7596*da6c28aaSamw 			}
7597*da6c28aaSamw 		}
7598*da6c28aaSamw #endif	/* _PC_SATTR_ENABLED */
7599*da6c28aaSamw 	}
7600*da6c28aaSamw 
7601*da6c28aaSamw 	if (attrinfo != NULL) {
7602*da6c28aaSamw 		if (attrinfo->attr_parent != NULL) {
7603*da6c28aaSamw 			free(attrinfo->attr_parent);
7604*da6c28aaSamw 		}
7605*da6c28aaSamw 		free(attrinfo->attr_path);
7606*da6c28aaSamw 		free(attrinfo);
7607*da6c28aaSamw 	}
76087c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
7609*da6c28aaSamw 	if (fd != -1) {
7610*da6c28aaSamw 		(void) close(fd);
7611*da6c28aaSamw 	}
7612*da6c28aaSamw 
7613*da6c28aaSamw 	/* Change back to the parent directory of the base file */
7614*da6c28aaSamw 	if (attrparent == NULL) {
7615*da6c28aaSamw 		(void) chdir(parent);
7616*da6c28aaSamw 	}
76177c478bd9Sstevel@tonic-gate }
76187c478bd9Sstevel@tonic-gate #else
76197c478bd9Sstevel@tonic-gate static void
7620*da6c28aaSamw xattrs_put(char *longname, char *shortname, char *parent, char *attrppath)
76217c478bd9Sstevel@tonic-gate {
76227c478bd9Sstevel@tonic-gate }
76237c478bd9Sstevel@tonic-gate #endif /* O_XATTR */
76247c478bd9Sstevel@tonic-gate 
76257c478bd9Sstevel@tonic-gate static int
7626*da6c28aaSamw put_link(char *name, char *longname, char *component, char *longattrname,
76277c478bd9Sstevel@tonic-gate     char *prefix, int filetype, char type)
76287c478bd9Sstevel@tonic-gate {
76297c478bd9Sstevel@tonic-gate 
76307c478bd9Sstevel@tonic-gate 	if (stbuf.st_nlink > 1) {
76317c478bd9Sstevel@tonic-gate 		struct linkbuf *lp;
76327c478bd9Sstevel@tonic-gate 		int found = 0;
76337c478bd9Sstevel@tonic-gate 
76347c478bd9Sstevel@tonic-gate 		for (lp = ihead; lp != NULL; lp = lp->nextp)
76357c478bd9Sstevel@tonic-gate 			if (lp->inum == stbuf.st_ino &&
76367c478bd9Sstevel@tonic-gate 			    lp->devnum == stbuf.st_dev) {
76377c478bd9Sstevel@tonic-gate 				found++;
76387c478bd9Sstevel@tonic-gate 				break;
76397c478bd9Sstevel@tonic-gate 			}
76407c478bd9Sstevel@tonic-gate 		if (found) {
76417c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
76427c478bd9Sstevel@tonic-gate 			if (filetype == XATTR_FILE)
7643*da6c28aaSamw 				if (put_xattr_hdr(longname, component,
7644*da6c28aaSamw 				    longattrname, prefix, type, filetype, lp)) {
76457c478bd9Sstevel@tonic-gate 					goto out;
76467c478bd9Sstevel@tonic-gate 			}
76477c478bd9Sstevel@tonic-gate #endif
76487c478bd9Sstevel@tonic-gate 			stbuf.st_size = (off_t)0;
76497c478bd9Sstevel@tonic-gate 			if (filetype != XATTR_FILE) {
76507c478bd9Sstevel@tonic-gate 				tomodes(&stbuf);
76517c478bd9Sstevel@tonic-gate 				if (chk_path_build(name, longname, lp->pathname,
76527c478bd9Sstevel@tonic-gate 				    prefix, type, filetype) > 0) {
76537c478bd9Sstevel@tonic-gate 					goto out;
76547c478bd9Sstevel@tonic-gate 				}
76557c478bd9Sstevel@tonic-gate 			}
76567c478bd9Sstevel@tonic-gate 
76577c478bd9Sstevel@tonic-gate 			if (mulvol && tapepos + 1 >= blocklim)
76587c478bd9Sstevel@tonic-gate 				newvol();
76597c478bd9Sstevel@tonic-gate 			(void) writetbuf((char *)&dblock, 1);
76607c478bd9Sstevel@tonic-gate 			/*
76617c478bd9Sstevel@tonic-gate 			 * write_ancillary() is not needed here.
76627c478bd9Sstevel@tonic-gate 			 * The first link is handled in the following
76637c478bd9Sstevel@tonic-gate 			 * else statement. No need to process ACLs
76647c478bd9Sstevel@tonic-gate 			 * for other hard links since they are the
76657c478bd9Sstevel@tonic-gate 			 * same file.
76667c478bd9Sstevel@tonic-gate 			 */
76677c478bd9Sstevel@tonic-gate 
76687c478bd9Sstevel@tonic-gate 			if (vflag) {
76697c478bd9Sstevel@tonic-gate #ifdef DEBUG
76707c478bd9Sstevel@tonic-gate 				if (NotTape)
76717c478bd9Sstevel@tonic-gate 					DEBUG("seek = %" FMT_blkcnt_t
76727c478bd9Sstevel@tonic-gate 					    "K\t", K(tapepos), 0);
76737c478bd9Sstevel@tonic-gate #endif
76747c478bd9Sstevel@tonic-gate 				if (filetype == XATTR_FILE) {
76757c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext(
76767c478bd9Sstevel@tonic-gate 					    "a %s attribute %s link to "
7677*da6c28aaSamw 					    "%s attribute %s\n"),
7678*da6c28aaSamw 					    name, component, name,
7679*da6c28aaSamw 					    lp->attrname);
76807c478bd9Sstevel@tonic-gate 				} else {
76817c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext(
76827c478bd9Sstevel@tonic-gate 					    "a %s link to %s\n"),
76837c478bd9Sstevel@tonic-gate 					    longname, lp->pathname);
76847c478bd9Sstevel@tonic-gate 				}
76857c478bd9Sstevel@tonic-gate 			}
76867c478bd9Sstevel@tonic-gate 			lp->count--;
76877c478bd9Sstevel@tonic-gate 			return (0);
76887c478bd9Sstevel@tonic-gate 		} else {
76897c478bd9Sstevel@tonic-gate 			lp = (struct linkbuf *)getmem(sizeof (*lp));
76907c478bd9Sstevel@tonic-gate 			if (lp != (struct linkbuf *)NULL) {
76917c478bd9Sstevel@tonic-gate 				lp->nextp = ihead;
76927c478bd9Sstevel@tonic-gate 				ihead = lp;
76937c478bd9Sstevel@tonic-gate 				lp->inum = stbuf.st_ino;
76947c478bd9Sstevel@tonic-gate 				lp->devnum = stbuf.st_dev;
76957c478bd9Sstevel@tonic-gate 				lp->count = stbuf.st_nlink - 1;
76967c478bd9Sstevel@tonic-gate 				if (filetype == XATTR_FILE) {
76977c478bd9Sstevel@tonic-gate 					(void) strcpy(lp->pathname, longname);
7698*da6c28aaSamw 					(void) strcpy(lp->attrname,
7699*da6c28aaSamw 					    component);
77007c478bd9Sstevel@tonic-gate 				} else {
77017c478bd9Sstevel@tonic-gate 					(void) strcpy(lp->pathname, longname);
77027c478bd9Sstevel@tonic-gate 					(void) strcpy(lp->attrname, "");
77037c478bd9Sstevel@tonic-gate 				}
77047c478bd9Sstevel@tonic-gate 			}
77057c478bd9Sstevel@tonic-gate 		}
77067c478bd9Sstevel@tonic-gate 	}
77077c478bd9Sstevel@tonic-gate 
77087c478bd9Sstevel@tonic-gate out:
77097c478bd9Sstevel@tonic-gate 	return (1);
77107c478bd9Sstevel@tonic-gate }
77117c478bd9Sstevel@tonic-gate 
77127c478bd9Sstevel@tonic-gate static int
7713*da6c28aaSamw put_extra_attributes(char *longname, char *shortname, char *longattrname,
7714*da6c28aaSamw     char *prefix, int filetype, char typeflag)
77157c478bd9Sstevel@tonic-gate {
7716fa9e4066Sahrens 	static acl_t *aclp = NULL;
7717fa9e4066Sahrens 	int error;
77187c478bd9Sstevel@tonic-gate 
7719fa9e4066Sahrens 	if (aclp != NULL) {
7720fa9e4066Sahrens 		acl_free(aclp);
77217c478bd9Sstevel@tonic-gate 		aclp = NULL;
77227c478bd9Sstevel@tonic-gate 	}
77237c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
7724*da6c28aaSamw 	if ((atflag || saflag) && (filetype == XATTR_FILE)) {
7725*da6c28aaSamw 		if (put_xattr_hdr(longname, shortname, longattrname, prefix,
77267c478bd9Sstevel@tonic-gate 		    typeflag, filetype, NULL)) {
77277c478bd9Sstevel@tonic-gate 			return (1);
77287c478bd9Sstevel@tonic-gate 		}
77297c478bd9Sstevel@tonic-gate 	}
77307c478bd9Sstevel@tonic-gate #endif
77317c478bd9Sstevel@tonic-gate 
77327c478bd9Sstevel@tonic-gate 	/* ACL support */
77337c478bd9Sstevel@tonic-gate 	if (pflag) {
77347c478bd9Sstevel@tonic-gate 		char	*secinfo = NULL;
77357c478bd9Sstevel@tonic-gate 		int	len = 0;
77367c478bd9Sstevel@tonic-gate 
77377c478bd9Sstevel@tonic-gate 		/* ACL support */
77387c478bd9Sstevel@tonic-gate 		if (((stbuf.st_mode & S_IFMT) != S_IFLNK)) {
77397c478bd9Sstevel@tonic-gate 			/*
77407c478bd9Sstevel@tonic-gate 			 * Get ACL info: dont bother allocating space if
7741fa9e4066Sahrens 			 * there is only a trivial ACL.
77427c478bd9Sstevel@tonic-gate 			 */
7743fa9e4066Sahrens 			if ((error = acl_get(shortname, ACL_NO_TRIVIAL,
7744fa9e4066Sahrens 			    &aclp)) != 0) {
77457c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
7746fa9e4066Sahrens 				    "%s: failed to retrieve acl : %s\n"),
7747fa9e4066Sahrens 				    longname, acl_strerror(error));
77487c478bd9Sstevel@tonic-gate 				return (1);
77497c478bd9Sstevel@tonic-gate 			}
77507c478bd9Sstevel@tonic-gate 		}
77517c478bd9Sstevel@tonic-gate 
77527c478bd9Sstevel@tonic-gate 		/* append security attributes if any */
7753fa9e4066Sahrens 		if (aclp != NULL) {
775445916cd2Sjpk 			(void) append_secattr(&secinfo, &len, acl_cnt(aclp),
775545916cd2Sjpk 			    acl_totext(aclp, ACL_APPEND_ID | ACL_COMPACT_FMT),
775645916cd2Sjpk 			    (acl_type(aclp) == ACLENT_T) ? UFSD_ACL : ACE_ACL);
775745916cd2Sjpk 		}
775845916cd2Sjpk 
775945916cd2Sjpk 		if (Tflag) {
776045916cd2Sjpk 			/* append Trusted Extensions extended attributes */
776145916cd2Sjpk 			append_ext_attr(shortname, &secinfo, &len);
776245916cd2Sjpk 			(void) write_ancillary(&dblock, secinfo, len, ACL_HDR);
776345916cd2Sjpk 
776445916cd2Sjpk 		} else if (aclp != NULL) {
77657c478bd9Sstevel@tonic-gate 			(void) write_ancillary(&dblock, secinfo, len, ACL_HDR);
77667c478bd9Sstevel@tonic-gate 		}
77677c478bd9Sstevel@tonic-gate 	}
77687c478bd9Sstevel@tonic-gate 	return (0);
77697c478bd9Sstevel@tonic-gate }
77707c478bd9Sstevel@tonic-gate 
77717c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
77727c478bd9Sstevel@tonic-gate static int
7773*da6c28aaSamw put_xattr_hdr(char *longname, char *shortname, char *longattrname, char *prefix,
77747c478bd9Sstevel@tonic-gate 	int typeflag, int filetype, struct linkbuf *lp)
77757c478bd9Sstevel@tonic-gate {
77767c478bd9Sstevel@tonic-gate 	char *lname = NULL;
77777c478bd9Sstevel@tonic-gate 	char *sname = NULL;
77787c478bd9Sstevel@tonic-gate 	int  error = 0;
77797c478bd9Sstevel@tonic-gate 	static char *attrbuf = NULL;
77807c478bd9Sstevel@tonic-gate 	int attrlen;
77817c478bd9Sstevel@tonic-gate 
77827c478bd9Sstevel@tonic-gate 	lname = malloc(sizeof (char) * strlen("/dev/null") + 1 +
77837c478bd9Sstevel@tonic-gate 	    strlen(shortname) + strlen(".hdr") + 1);
77847c478bd9Sstevel@tonic-gate 
77857c478bd9Sstevel@tonic-gate 	if (lname == NULL) {
77867c478bd9Sstevel@tonic-gate 		fatal(gettext("Out of Memory."));
77877c478bd9Sstevel@tonic-gate 	}
77887c478bd9Sstevel@tonic-gate 	sname = malloc(sizeof (char) * strlen(shortname) +
7789*da6c28aaSamw 	    strlen(".hdr") + 1);
77907c478bd9Sstevel@tonic-gate 	if (sname == NULL) {
77917c478bd9Sstevel@tonic-gate 		fatal(gettext("Out of Memory."));
77927c478bd9Sstevel@tonic-gate 	}
77937c478bd9Sstevel@tonic-gate 
77947c478bd9Sstevel@tonic-gate 	(void) sprintf(sname, "%s.hdr", shortname);
77957c478bd9Sstevel@tonic-gate 	(void) sprintf(lname, "/dev/null/%s", sname);
77967c478bd9Sstevel@tonic-gate 
77977c478bd9Sstevel@tonic-gate 	if (strlcpy(dblock.dbuf.name, lname, sizeof (dblock.dbuf.name)) >=
77987c478bd9Sstevel@tonic-gate 	    sizeof (dblock.dbuf.name)) {
77997c478bd9Sstevel@tonic-gate 		fatal(gettext(
78007c478bd9Sstevel@tonic-gate 		    "Buffer overflow writing extended attribute file name"));
78017c478bd9Sstevel@tonic-gate 	}
78027c478bd9Sstevel@tonic-gate 
78037c478bd9Sstevel@tonic-gate 	/*
78047c478bd9Sstevel@tonic-gate 	 * dump extended attr lookup info
78057c478bd9Sstevel@tonic-gate 	 */
7806*da6c28aaSamw 	prepare_xattr(&attrbuf, longname, longattrname, typeflag, lp, &attrlen);
78077c478bd9Sstevel@tonic-gate 	write_ancillary(&dblock, attrbuf, attrlen, _XATTR_HDRTYPE);
78087c478bd9Sstevel@tonic-gate 
78097c478bd9Sstevel@tonic-gate 	(void) sprintf(lname, "/dev/null/%s", shortname);
78107c478bd9Sstevel@tonic-gate 	(void) strncpy(dblock.dbuf.name, sname, NAMSIZ);
78117c478bd9Sstevel@tonic-gate 
78127c478bd9Sstevel@tonic-gate 	/*
78137c478bd9Sstevel@tonic-gate 	 * Set up filename for attribute
78147c478bd9Sstevel@tonic-gate 	 */
78157c478bd9Sstevel@tonic-gate 
78167c478bd9Sstevel@tonic-gate 	error = build_dblock(lname, tchar, '0', filetype,
78177c478bd9Sstevel@tonic-gate 	    &stbuf, stbuf.st_dev, prefix);
78187c478bd9Sstevel@tonic-gate 	free(lname);
78197c478bd9Sstevel@tonic-gate 	free(sname);
78207c478bd9Sstevel@tonic-gate 
78217c478bd9Sstevel@tonic-gate 	return (error);
78227c478bd9Sstevel@tonic-gate }
78237c478bd9Sstevel@tonic-gate #endif
78247c478bd9Sstevel@tonic-gate 
78257c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
78267c478bd9Sstevel@tonic-gate static int
7827*da6c28aaSamw read_xattr_hdr(attr_data_t **attrinfo)
78287c478bd9Sstevel@tonic-gate {
78297c478bd9Sstevel@tonic-gate 	char		buf[TBLOCK];
7830*da6c28aaSamw 	char		*attrparent = NULL;
78317c478bd9Sstevel@tonic-gate 	blkcnt_t	blocks;
78327c478bd9Sstevel@tonic-gate 	char		*tp;
78337c478bd9Sstevel@tonic-gate 	off_t		bytes;
78347c478bd9Sstevel@tonic-gate 	int		comp_len, link_len;
78357c478bd9Sstevel@tonic-gate 	int		namelen;
7836*da6c28aaSamw 	int		attrparentlen;
7837*da6c28aaSamw 	int		parentfilelen;
78387c478bd9Sstevel@tonic-gate 
78397c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.typeflag != _XATTR_HDRTYPE)
78407c478bd9Sstevel@tonic-gate 		return (1);
78417c478bd9Sstevel@tonic-gate 
78427c478bd9Sstevel@tonic-gate 	bytes = stbuf.st_size;
78437c478bd9Sstevel@tonic-gate 	if ((xattrhead = calloc(1, (int)bytes)) == NULL) {
78447c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
78457c478bd9Sstevel@tonic-gate 		    "Insufficient memory for extended attribute\n"));
78467c478bd9Sstevel@tonic-gate 		return (1);
78477c478bd9Sstevel@tonic-gate 	}
78487c478bd9Sstevel@tonic-gate 
78497c478bd9Sstevel@tonic-gate 	tp = (char *)xattrhead;
78507c478bd9Sstevel@tonic-gate 	blocks = TBLOCKS(bytes);
78517c478bd9Sstevel@tonic-gate 	while (blocks-- > 0) {
78527c478bd9Sstevel@tonic-gate 		readtape(buf);
78537c478bd9Sstevel@tonic-gate 		if (bytes <= TBLOCK) {
78547c478bd9Sstevel@tonic-gate 			(void) memcpy(tp, buf, (size_t)bytes);
78557c478bd9Sstevel@tonic-gate 			break;
78567c478bd9Sstevel@tonic-gate 		} else {
78577c478bd9Sstevel@tonic-gate 			(void) memcpy(tp, buf, TBLOCK);
78587c478bd9Sstevel@tonic-gate 			tp += TBLOCK;
78597c478bd9Sstevel@tonic-gate 		}
78607c478bd9Sstevel@tonic-gate 		bytes -= TBLOCK;
78617c478bd9Sstevel@tonic-gate 	}
78627c478bd9Sstevel@tonic-gate 
78637c478bd9Sstevel@tonic-gate 	/*
78647c478bd9Sstevel@tonic-gate 	 * Validate that we can handle header format
78657c478bd9Sstevel@tonic-gate 	 */
78667c478bd9Sstevel@tonic-gate 	if (strcmp(xattrhead->h_version, XATTR_ARCH_VERS) != 0) {
78677c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
78687c478bd9Sstevel@tonic-gate 		    gettext("Unknown extended attribute format encountered\n"));
78697c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
78707c478bd9Sstevel@tonic-gate 		    gettext("Disabling extended attribute parsing\n"));
78717c478bd9Sstevel@tonic-gate 		xattrbadhead = 1;
78727c478bd9Sstevel@tonic-gate 		return (0);
78737c478bd9Sstevel@tonic-gate 	}
78747c478bd9Sstevel@tonic-gate 	(void) sscanf(xattrhead->h_component_len, "%10d", &comp_len);
78757c478bd9Sstevel@tonic-gate 	(void) sscanf(xattrhead->h_link_component_len,	"%10d", &link_len);
78767c478bd9Sstevel@tonic-gate 	xattrp = (struct xattr_buf *)(((char *)xattrhead) +
78777c478bd9Sstevel@tonic-gate 	    sizeof (struct xattr_hdr));
78787c478bd9Sstevel@tonic-gate 	(void) sscanf(xattrp->h_namesz, "%7d", &namelen);
78797c478bd9Sstevel@tonic-gate 	if (link_len > 0)
78807c478bd9Sstevel@tonic-gate 		xattr_linkp = (struct xattr_buf *)
78817c478bd9Sstevel@tonic-gate 		    ((int)xattrp + (int)comp_len);
78827c478bd9Sstevel@tonic-gate 	else
78837c478bd9Sstevel@tonic-gate 		xattr_linkp = NULL;
78847c478bd9Sstevel@tonic-gate 
7885*da6c28aaSamw 	/*
7886*da6c28aaSamw 	 * Gather the attribute path from the filename and attrnames section.
7887*da6c28aaSamw 	 * The filename and attrnames section can be composed of two or more
7888*da6c28aaSamw 	 * path segments separated by a null character.  The first segment
7889*da6c28aaSamw 	 * is the path to the parent file that roots the entire sequence in
7890*da6c28aaSamw 	 * the normal name space. The remaining segments describes a path
7891*da6c28aaSamw 	 * rooted at the hidden extended attribute directory of the leaf file of
7892*da6c28aaSamw 	 * the previous segment, making it possible to name attributes on
7893*da6c28aaSamw 	 * attributes.
7894*da6c28aaSamw 	 */
7895*da6c28aaSamw 	parentfilelen = strlen(xattrp->h_names);
7896*da6c28aaSamw 	xattrapath = xattrp->h_names + parentfilelen + 1;
7897*da6c28aaSamw 	if ((strlen(xattrapath) + parentfilelen + 2) < namelen) {
7898*da6c28aaSamw 		/*
7899*da6c28aaSamw 		 * The attrnames section contains a system attribute on an
7900*da6c28aaSamw 		 * attribute.  Save the name of the attribute for use later,
7901*da6c28aaSamw 		 * and replace the null separating the attribute name from
7902*da6c28aaSamw 		 * the system attribute name with a '/' so that xattrapath can
7903*da6c28aaSamw 		 * be used to display messages with the full attribute path name
7904*da6c28aaSamw 		 * rooted at the hidden attribute directory of the base file
7905*da6c28aaSamw 		 * in normal name space.
7906*da6c28aaSamw 		 */
7907*da6c28aaSamw 		attrparent = strdup(xattrapath);
7908*da6c28aaSamw 		attrparentlen = strlen(attrparent);
7909*da6c28aaSamw 		xattrapath[attrparentlen] = '/';
7910*da6c28aaSamw 	}
7911*da6c28aaSamw 	if ((fill_in_attr_info((attrparent == NULL) ? xattrapath :
7912*da6c28aaSamw 	    xattrapath + attrparentlen + 1, xattrapath, attrparent,
7913*da6c28aaSamw 	    -1, 0, attrinfo)) == 1) {
7914*da6c28aaSamw 		free(attrparent);
7915*da6c28aaSamw 		return (1);
7916*da6c28aaSamw 	}
7917*da6c28aaSamw 
7918*da6c28aaSamw 	/* Gather link info */
79197c478bd9Sstevel@tonic-gate 	if (xattr_linkp) {
79207c478bd9Sstevel@tonic-gate 		xattr_linkaname = xattr_linkp->h_names +
79217c478bd9Sstevel@tonic-gate 		    strlen(xattr_linkp->h_names) + 1;
79227c478bd9Sstevel@tonic-gate 	} else {
79237c478bd9Sstevel@tonic-gate 		xattr_linkaname = NULL;
79247c478bd9Sstevel@tonic-gate 	}
7925d2443e76Smarks 
79267c478bd9Sstevel@tonic-gate 	return (0);
79277c478bd9Sstevel@tonic-gate }
79287c478bd9Sstevel@tonic-gate #else
79297c478bd9Sstevel@tonic-gate static int
7930*da6c28aaSamw read_xattr_hdr(attr_data_t **attrinfo)
79317c478bd9Sstevel@tonic-gate {
79327c478bd9Sstevel@tonic-gate 	return (0);
79337c478bd9Sstevel@tonic-gate }
79347c478bd9Sstevel@tonic-gate #endif
79357c478bd9Sstevel@tonic-gate 
79367c478bd9Sstevel@tonic-gate /*
79377c478bd9Sstevel@tonic-gate  * skip over extra slashes in string.
79387c478bd9Sstevel@tonic-gate  *
79397c478bd9Sstevel@tonic-gate  * For example:
79407c478bd9Sstevel@tonic-gate  * /usr/tmp/////
79417c478bd9Sstevel@tonic-gate  *
79427c478bd9Sstevel@tonic-gate  * would return pointer at
79437c478bd9Sstevel@tonic-gate  * /usr/tmp/////
79447c478bd9Sstevel@tonic-gate  *         ^
79457c478bd9Sstevel@tonic-gate  */
79467c478bd9Sstevel@tonic-gate static char *
79477c478bd9Sstevel@tonic-gate skipslashes(char *string, char *start)
79487c478bd9Sstevel@tonic-gate {
79497c478bd9Sstevel@tonic-gate 	while ((string > start) && *(string - 1) == '/') {
79507c478bd9Sstevel@tonic-gate 		string--;
79517c478bd9Sstevel@tonic-gate 	}
79527c478bd9Sstevel@tonic-gate 
79537c478bd9Sstevel@tonic-gate 	return (string);
79547c478bd9Sstevel@tonic-gate }
79557c478bd9Sstevel@tonic-gate 
79567c478bd9Sstevel@tonic-gate /*
79577c478bd9Sstevel@tonic-gate  * Return the parent directory of a given path.
79587c478bd9Sstevel@tonic-gate  *
79597c478bd9Sstevel@tonic-gate  * Examples:
79607c478bd9Sstevel@tonic-gate  * /usr/tmp return /usr
79617c478bd9Sstevel@tonic-gate  * /usr/tmp/file return /usr/tmp
79627c478bd9Sstevel@tonic-gate  * /  returns .
79637c478bd9Sstevel@tonic-gate  * /usr returns /
79647c478bd9Sstevel@tonic-gate  * file returns .
79657c478bd9Sstevel@tonic-gate  *
79667c478bd9Sstevel@tonic-gate  * dir is assumed to be at least as big as path.
79677c478bd9Sstevel@tonic-gate  */
79687c478bd9Sstevel@tonic-gate static void
79697c478bd9Sstevel@tonic-gate get_parent(char *path, char *dir)
79707c478bd9Sstevel@tonic-gate {
79717c478bd9Sstevel@tonic-gate 	char *s;
79727c478bd9Sstevel@tonic-gate 	char tmpdir[PATH_MAX + 1];
79737c478bd9Sstevel@tonic-gate 
79747c478bd9Sstevel@tonic-gate 	if (strlen(path) > PATH_MAX) {
79757c478bd9Sstevel@tonic-gate 		fatal(gettext("pathname is too long"));
79767c478bd9Sstevel@tonic-gate 	}
79777c478bd9Sstevel@tonic-gate 	(void) strcpy(tmpdir, path);
79787c478bd9Sstevel@tonic-gate 	chop_endslashes(tmpdir);
79797c478bd9Sstevel@tonic-gate 
79807c478bd9Sstevel@tonic-gate 	if ((s = strrchr(tmpdir, '/')) == NULL) {
79817c478bd9Sstevel@tonic-gate 		(void) strcpy(dir, ".");
79827c478bd9Sstevel@tonic-gate 	} else {
79837c478bd9Sstevel@tonic-gate 		s = skipslashes(s, tmpdir);
79847c478bd9Sstevel@tonic-gate 		*s = '\0';
79857c478bd9Sstevel@tonic-gate 		if (s == tmpdir)
79867c478bd9Sstevel@tonic-gate 			(void) strcpy(dir, "/");
79877c478bd9Sstevel@tonic-gate 		else
79887c478bd9Sstevel@tonic-gate 			(void) strcpy(dir, tmpdir);
79897c478bd9Sstevel@tonic-gate 	}
79907c478bd9Sstevel@tonic-gate }
79917c478bd9Sstevel@tonic-gate 
79927c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
79937c478bd9Sstevel@tonic-gate static char *
79947c478bd9Sstevel@tonic-gate get_component(char *path)
79957c478bd9Sstevel@tonic-gate {
79967c478bd9Sstevel@tonic-gate 	char *ptr;
79977c478bd9Sstevel@tonic-gate 
79987c478bd9Sstevel@tonic-gate 	ptr = strrchr(path, '/');
79997c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
80007c478bd9Sstevel@tonic-gate 		return (path);
80017c478bd9Sstevel@tonic-gate 	} else {
80027c478bd9Sstevel@tonic-gate 		/*
80037c478bd9Sstevel@tonic-gate 		 * Handle trailing slash
80047c478bd9Sstevel@tonic-gate 		 */
80057c478bd9Sstevel@tonic-gate 		if (*(ptr + 1) == '\0')
80067c478bd9Sstevel@tonic-gate 			return (ptr);
80077c478bd9Sstevel@tonic-gate 		else
80087c478bd9Sstevel@tonic-gate 			return (ptr + 1);
80097c478bd9Sstevel@tonic-gate 	}
80107c478bd9Sstevel@tonic-gate }
80117c478bd9Sstevel@tonic-gate #else
80127c478bd9Sstevel@tonic-gate static char *
80137c478bd9Sstevel@tonic-gate get_component(char *path)
80147c478bd9Sstevel@tonic-gate {
80157c478bd9Sstevel@tonic-gate 	return (path);
80167c478bd9Sstevel@tonic-gate }
80177c478bd9Sstevel@tonic-gate #endif
80187c478bd9Sstevel@tonic-gate 
8019*da6c28aaSamw #if defined(O_XATTR)
80207c478bd9Sstevel@tonic-gate static int
8021*da6c28aaSamw retry_open_attr(int pdirfd, int cwd, char *dirp, char *pattr, char *name,
8022*da6c28aaSamw     int oflag, mode_t mode)
80237c478bd9Sstevel@tonic-gate {
8024*da6c28aaSamw 	int dirfd;
8025*da6c28aaSamw 	int ofilefd = -1;
80267c478bd9Sstevel@tonic-gate 	struct timeval times[2];
80277c478bd9Sstevel@tonic-gate 	mode_t newmode;
80287c478bd9Sstevel@tonic-gate 	struct stat parentstat;
8029d2443e76Smarks 	acl_t *aclp = NULL;
8030d2443e76Smarks 	int error;
80317c478bd9Sstevel@tonic-gate 
80327c478bd9Sstevel@tonic-gate 	/*
80337c478bd9Sstevel@tonic-gate 	 * We couldn't get to attrdir. See if its
80347c478bd9Sstevel@tonic-gate 	 * just a mode problem on the parent file.
80357c478bd9Sstevel@tonic-gate 	 * for example: a mode such as r-xr--r--
8036*da6c28aaSamw 	 * on a ufs file system without extended
8037*da6c28aaSamw 	 * system attribute support won't let us
8038*da6c28aaSamw 	 * create an attribute dir if it doesn't
8039*da6c28aaSamw 	 * already exist, and on a ufs file system
8040*da6c28aaSamw 	 * with extended system attribute support
8041*da6c28aaSamw 	 * won't let us open the attribute for
8042*da6c28aaSamw 	 * write.
8043d2443e76Smarks 	 *
8044d2443e76Smarks 	 * If file has a non-trivial ACL, then save it
8045d2443e76Smarks 	 * off so that we can place it back on after doing
8046d2443e76Smarks 	 * chmod's.
80477c478bd9Sstevel@tonic-gate 	 */
8048*da6c28aaSamw 	if ((dirfd = openat(cwd, (pattr == NULL) ? dirp : pattr,
8049*da6c28aaSamw 	    O_RDONLY)) == -1) {
8050d2443e76Smarks 		return (-1);
80517c478bd9Sstevel@tonic-gate 	}
8052*da6c28aaSamw 	if (fstat(dirfd, &parentstat) == -1) {
8053*da6c28aaSamw 		(void) fprintf(stderr, gettext(
8054*da6c28aaSamw 		    "tar: cannot stat %sfile %s: %s\n"),
8055*da6c28aaSamw 		    (pdirfd == -1) ? "" : gettext("parent of "),
8056*da6c28aaSamw 		    (pdirfd == -1) ? dirp : name, strerror(errno));
8057*da6c28aaSamw 			return (-1);
8058*da6c28aaSamw 	}
8059*da6c28aaSamw 	if ((error = facl_get(dirfd, ACL_NO_TRIVIAL, &aclp)) != 0) {
8060*da6c28aaSamw 		(void) fprintf(stderr, gettext(
8061*da6c28aaSamw 		    "tar: failed to retrieve ACL on %sfile %s: %s\n"),
8062*da6c28aaSamw 		    (pdirfd == -1) ? "" : gettext("parent of "),
8063*da6c28aaSamw 		    (pdirfd == -1) ? dirp : name, strerror(errno));
8064d2443e76Smarks 			return (-1);
8065d2443e76Smarks 	}
8066d2443e76Smarks 
80677c478bd9Sstevel@tonic-gate 	newmode = S_IWUSR | parentstat.st_mode;
8068*da6c28aaSamw 	if (fchmod(dirfd, newmode) == -1) {
80697c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
8070*da6c28aaSamw 		    gettext(
8071*da6c28aaSamw 		    "tar: cannot fchmod %sfile %s to %o: %s\n"),
8072*da6c28aaSamw 		    (pdirfd == -1) ? "" : gettext("parent of "),
8073*da6c28aaSamw 		    (pdirfd == -1) ? dirp : name, newmode, strerror(errno));
8074d2443e76Smarks 		if (aclp)
8075d2443e76Smarks 			acl_free(aclp);
8076d2443e76Smarks 		return (-1);
80777c478bd9Sstevel@tonic-gate 	}
80787c478bd9Sstevel@tonic-gate 
8079d2443e76Smarks 
8080*da6c28aaSamw 	if (pdirfd == -1) {
8081d2443e76Smarks 		/*
8082*da6c28aaSamw 		 * We weren't able to create the attribute directory before.
8083*da6c28aaSamw 		 * Now try again.
8084d2443e76Smarks 		 */
8085*da6c28aaSamw 		ofilefd = attropen(dirp, ".", oflag);
8086*da6c28aaSamw 	} else {
8087*da6c28aaSamw 		/*
8088*da6c28aaSamw 		 * We weren't able to create open the attribute before.
8089*da6c28aaSamw 		 * Now try again.
8090*da6c28aaSamw 		 */
8091*da6c28aaSamw 		ofilefd = openat(pdirfd, name, oflag, mode);
8092*da6c28aaSamw 	}
80937c478bd9Sstevel@tonic-gate 
80947c478bd9Sstevel@tonic-gate 	/*
80957c478bd9Sstevel@tonic-gate 	 * Put mode back to original
80967c478bd9Sstevel@tonic-gate 	 */
8097*da6c28aaSamw 	if (fchmod(dirfd, parentstat.st_mode) == -1) {
8098d2443e76Smarks 		(void) fprintf(stderr,
8099*da6c28aaSamw 		    gettext("tar: cannot chmod %sfile %s to %o: %s\n"),
8100*da6c28aaSamw 		    (pdirfd == -1) ? "" : gettext("parent of "),
8101*da6c28aaSamw 		    (pdirfd == -1) ? dirp : name, newmode, strerror(errno));
8102d2443e76Smarks 	}
8103d2443e76Smarks 
8104d2443e76Smarks 	if (aclp) {
8105*da6c28aaSamw 		error = facl_set(dirfd, aclp);
8106d2443e76Smarks 		if (error) {
8107d2443e76Smarks 			(void) fprintf(stderr,
8108*da6c28aaSamw 			    gettext("tar: failed to set acl entries on "
8109*da6c28aaSamw 			    "%sfile %s\n"),
8110*da6c28aaSamw 			    (pdirfd == -1) ? "" : gettext("parent of "),
8111*da6c28aaSamw 			    (pdirfd == -1) ? dirp : name);
8112d2443e76Smarks 		}
8113d2443e76Smarks 		acl_free(aclp);
8114d2443e76Smarks 	}
81157c478bd9Sstevel@tonic-gate 
81167c478bd9Sstevel@tonic-gate 	/*
81177c478bd9Sstevel@tonic-gate 	 * Put back time stamps
81187c478bd9Sstevel@tonic-gate 	 */
81197c478bd9Sstevel@tonic-gate 
81207c478bd9Sstevel@tonic-gate 	times[0].tv_sec = parentstat.st_atime;
81217c478bd9Sstevel@tonic-gate 	times[0].tv_usec = 0;
81227c478bd9Sstevel@tonic-gate 	times[1].tv_sec = parentstat.st_mtime;
81237c478bd9Sstevel@tonic-gate 	times[1].tv_usec = 0;
81247c478bd9Sstevel@tonic-gate 
8125*da6c28aaSamw 	(void) futimesat(cwd, (pattr == NULL) ? dirp : pattr, times);
8126*da6c28aaSamw 
8127*da6c28aaSamw 	(void) close(dirfd);
8128*da6c28aaSamw 
8129*da6c28aaSamw 	return (ofilefd);
81307c478bd9Sstevel@tonic-gate }
8131*da6c28aaSamw #endif
81327c478bd9Sstevel@tonic-gate 
81337c478bd9Sstevel@tonic-gate #if !defined(O_XATTR)
81347c478bd9Sstevel@tonic-gate static int
81357c478bd9Sstevel@tonic-gate openat64(int fd, const char *name, int oflag, mode_t cmode)
81367c478bd9Sstevel@tonic-gate {
81377c478bd9Sstevel@tonic-gate 	return (open64(name, oflag, cmode));
81387c478bd9Sstevel@tonic-gate }
81397c478bd9Sstevel@tonic-gate 
81407c478bd9Sstevel@tonic-gate static int
81417c478bd9Sstevel@tonic-gate openat(int fd, const char *name, int oflag, mode_t cmode)
81427c478bd9Sstevel@tonic-gate {
81437c478bd9Sstevel@tonic-gate 	return (open(name, oflag, cmode));
81447c478bd9Sstevel@tonic-gate }
81457c478bd9Sstevel@tonic-gate 
81467c478bd9Sstevel@tonic-gate static int
81477c478bd9Sstevel@tonic-gate fchownat(int fd, const char *name, uid_t owner, gid_t group, int flag)
81487c478bd9Sstevel@tonic-gate {
81497c478bd9Sstevel@tonic-gate 	if (flag == AT_SYMLINK_NOFOLLOW)
81507c478bd9Sstevel@tonic-gate 		return (lchown(name, owner, group));
81517c478bd9Sstevel@tonic-gate 	else
81527c478bd9Sstevel@tonic-gate 		return (chown(name, owner, group));
81537c478bd9Sstevel@tonic-gate }
81547c478bd9Sstevel@tonic-gate 
81557c478bd9Sstevel@tonic-gate static int
81567c478bd9Sstevel@tonic-gate renameat(int fromfd, char *old, int tofd, char *new)
81577c478bd9Sstevel@tonic-gate {
81587c478bd9Sstevel@tonic-gate 	return (rename(old, new));
81597c478bd9Sstevel@tonic-gate }
81607c478bd9Sstevel@tonic-gate 
81617c478bd9Sstevel@tonic-gate static int
81627c478bd9Sstevel@tonic-gate futimesat(int fd, char *path, struct timeval times[2])
81637c478bd9Sstevel@tonic-gate {
81647c478bd9Sstevel@tonic-gate 	return (utimes(path, times));
81657c478bd9Sstevel@tonic-gate }
81667c478bd9Sstevel@tonic-gate 
81677c478bd9Sstevel@tonic-gate static int
81687c478bd9Sstevel@tonic-gate unlinkat(int dirfd, char *path, int flag)
81697c478bd9Sstevel@tonic-gate {
81707c478bd9Sstevel@tonic-gate 	if (flag == AT_REMOVEDIR)
81717c478bd9Sstevel@tonic-gate 		return (rmdir(path));
81727c478bd9Sstevel@tonic-gate 	else
81737c478bd9Sstevel@tonic-gate 		return (unlink(path));
81747c478bd9Sstevel@tonic-gate }
81757c478bd9Sstevel@tonic-gate 
81767c478bd9Sstevel@tonic-gate static int
81777c478bd9Sstevel@tonic-gate fstatat(int fd, char *path, struct stat *buf, int flag)
81787c478bd9Sstevel@tonic-gate {
81797c478bd9Sstevel@tonic-gate 	if (flag == AT_SYMLINK_NOFOLLOW)
81807c478bd9Sstevel@tonic-gate 		return (lstat(path, buf));
81817c478bd9Sstevel@tonic-gate 	else
81827c478bd9Sstevel@tonic-gate 		return (stat(path, buf));
81837c478bd9Sstevel@tonic-gate }
81847c478bd9Sstevel@tonic-gate 
81857c478bd9Sstevel@tonic-gate static int
81867c478bd9Sstevel@tonic-gate attropen(char *file, char *attr, int omode, mode_t cmode)
81877c478bd9Sstevel@tonic-gate {
81887c478bd9Sstevel@tonic-gate 	errno = ENOTSUP;
81897c478bd9Sstevel@tonic-gate 	return (-1);
81907c478bd9Sstevel@tonic-gate }
81917c478bd9Sstevel@tonic-gate #endif
81927c478bd9Sstevel@tonic-gate 
81937c478bd9Sstevel@tonic-gate static void
81947c478bd9Sstevel@tonic-gate chop_endslashes(char *path)
81957c478bd9Sstevel@tonic-gate {
81967c478bd9Sstevel@tonic-gate 	char *end, *ptr;
81977c478bd9Sstevel@tonic-gate 
81987c478bd9Sstevel@tonic-gate 	/*
81997c478bd9Sstevel@tonic-gate 	 * Chop of slashes, but not if all we have is slashes
82007c478bd9Sstevel@tonic-gate 	 * for example: ////
82017c478bd9Sstevel@tonic-gate 	 * should make no changes, otherwise it will screw up
82027c478bd9Sstevel@tonic-gate 	 * checkdir
82037c478bd9Sstevel@tonic-gate 	 */
82047c478bd9Sstevel@tonic-gate 	end = &path[strlen(path) -1];
82057c478bd9Sstevel@tonic-gate 	if (*end == '/' && end != path) {
82067c478bd9Sstevel@tonic-gate 		ptr = skipslashes(end, path);
82077c478bd9Sstevel@tonic-gate 		if (ptr != NULL && ptr != path) {
82087c478bd9Sstevel@tonic-gate 			*ptr = '\0';
82097c478bd9Sstevel@tonic-gate 		}
82107c478bd9Sstevel@tonic-gate 	}
82117c478bd9Sstevel@tonic-gate }
821245916cd2Sjpk /* Trusted Extensions */
821345916cd2Sjpk 
821445916cd2Sjpk /*
821545916cd2Sjpk  * append_ext_attr():
821645916cd2Sjpk  *
821745916cd2Sjpk  * Append extended attributes and other information into the buffer
821845916cd2Sjpk  * that gets written to the ancillary file.
821945916cd2Sjpk  *
822045916cd2Sjpk  * With option 'T', we create a tarfile which
822145916cd2Sjpk  * has an ancillary file each corresponding archived file.
822245916cd2Sjpk  * Each ancillary file contains 1 or more of the
822345916cd2Sjpk  * following attributes:
822445916cd2Sjpk  *
822545916cd2Sjpk  *	attribute type        attribute		process procedure
822645916cd2Sjpk  *	----------------      ----------------  --------------------------
822745916cd2Sjpk  *   	DIR_TYPE       = 'D'   directory flag	append if a directory
822845916cd2Sjpk  *    	LBL_TYPE       = 'L'   SL[IL] or SL	append ascii label
822945916cd2Sjpk  *
823045916cd2Sjpk  *
823145916cd2Sjpk  */
823245916cd2Sjpk static void
823345916cd2Sjpk append_ext_attr(char *shortname, char **secinfo, int *len)
823445916cd2Sjpk {
823545916cd2Sjpk 	bslabel_t	b_slabel;	/* binary sensitvity label */
823645916cd2Sjpk 	char		*ascii = NULL;	/* ascii label */
823745916cd2Sjpk 
823845916cd2Sjpk 	/*
823945916cd2Sjpk 	 * For each attribute type, append it if it is
824045916cd2Sjpk 	 * relevant to the file type.
824145916cd2Sjpk 	 */
824245916cd2Sjpk 
824345916cd2Sjpk 	/*
824445916cd2Sjpk 	 * For attribute type DIR_TYPE,
824545916cd2Sjpk 	 * append it to the following file type:
824645916cd2Sjpk 	 *
824745916cd2Sjpk 	 *	S_IFDIR: directories
824845916cd2Sjpk 	 */
824945916cd2Sjpk 
825045916cd2Sjpk 	/*
825145916cd2Sjpk 	 * For attribute type LBL_TYPE,
825245916cd2Sjpk 	 * append it to the following file type:
825345916cd2Sjpk 	 *
825445916cd2Sjpk 	 *	S_IFDIR: directories (including mld, sld)
825545916cd2Sjpk 	 *	S_IFLNK: symbolic link
825645916cd2Sjpk 	 *	S_IFREG: regular file but not hard link
825745916cd2Sjpk 	 *	S_IFIFO: FIFO file but not hard link
825845916cd2Sjpk 	 *	S_IFCHR: char special file but not hard link
825945916cd2Sjpk 	 *	S_IFBLK: block special file but not hard link
826045916cd2Sjpk 	 */
826145916cd2Sjpk 	switch (stbuf.st_mode & S_IFMT) {
826245916cd2Sjpk 
826345916cd2Sjpk 	case S_IFDIR:
826445916cd2Sjpk 
826545916cd2Sjpk 		/*
826645916cd2Sjpk 		 * append DIR_TYPE
826745916cd2Sjpk 		 */
826845916cd2Sjpk 		(void) append_secattr(secinfo, len, 1,
826945916cd2Sjpk 		    "\0", DIR_TYPE);
827045916cd2Sjpk 
827145916cd2Sjpk 		/*
827245916cd2Sjpk 		 * Get and append attribute types LBL_TYPE.
827345916cd2Sjpk 		 * For directories, LBL_TYPE contains SL.
827445916cd2Sjpk 		 */
827545916cd2Sjpk 		/* get binary sensitivity label */
827645916cd2Sjpk 		if (getlabel(shortname, &b_slabel) != 0) {
827745916cd2Sjpk 			(void) fprintf(stderr,
827845916cd2Sjpk 			    gettext("tar: can't get sensitvity label for "
827945916cd2Sjpk 			    " %s, getlabel() error: %s\n"),
828045916cd2Sjpk 			    shortname, strerror(errno));
828145916cd2Sjpk 		} else {
828245916cd2Sjpk 			/* get ascii SL */
828345916cd2Sjpk 			if (bsltos(&b_slabel, &ascii,
828445916cd2Sjpk 			    0, 0) <= 0) {
828545916cd2Sjpk 				(void) fprintf(stderr,
828645916cd2Sjpk 				    gettext("tar: can't get ascii SL for"
828745916cd2Sjpk 				    " %s\n"), shortname);
828845916cd2Sjpk 			} else {
828945916cd2Sjpk 				/* append LBL_TYPE */
829045916cd2Sjpk 				(void) append_secattr(secinfo, len,
829145916cd2Sjpk 				    strlen(ascii) + 1, ascii,
829245916cd2Sjpk 				    LBL_TYPE);
829345916cd2Sjpk 
829445916cd2Sjpk 				/* free storage */
829545916cd2Sjpk 				if (ascii != NULL) {
829645916cd2Sjpk 					free(ascii);
829745916cd2Sjpk 					ascii = (char *)0;
829845916cd2Sjpk 				}
829945916cd2Sjpk 			}
830045916cd2Sjpk 
830145916cd2Sjpk 		}
830245916cd2Sjpk 		break;
830345916cd2Sjpk 
830445916cd2Sjpk 	case S_IFLNK:
830545916cd2Sjpk 	case S_IFREG:
830645916cd2Sjpk 	case S_IFIFO:
830745916cd2Sjpk 	case S_IFCHR:
830845916cd2Sjpk 	case S_IFBLK:
830945916cd2Sjpk 
831045916cd2Sjpk 		/* get binary sensitivity label */
831145916cd2Sjpk 		if (getlabel(shortname, &b_slabel) != 0) {
831245916cd2Sjpk 			(void) fprintf(stderr,
831345916cd2Sjpk 			    gettext("tar: can't get sensitivty label for %s, "
831445916cd2Sjpk 			    "getlabel() error: %s\n"),
831545916cd2Sjpk 			    shortname, strerror(errno));
831645916cd2Sjpk 		} else {
831745916cd2Sjpk 			/* get ascii IL[SL] */
831845916cd2Sjpk 			if (bsltos(&b_slabel, &ascii, 0, 0) <= 0) {
831945916cd2Sjpk 				(void) fprintf(stderr,
832045916cd2Sjpk 				    gettext("tar: can't translate sensitivity "
832145916cd2Sjpk 				    " label for %s\n"), shortname);
832245916cd2Sjpk 			} else {
832345916cd2Sjpk 				char *cmw_label;
832445916cd2Sjpk 				size_t  cmw_length;
832545916cd2Sjpk 
832645916cd2Sjpk 				cmw_length = strlen("ADMIN_LOW [] ") +
832745916cd2Sjpk 				    strlen(ascii);
832845916cd2Sjpk 				if ((cmw_label = malloc(cmw_length)) == NULL) {
832945916cd2Sjpk 					(void) fprintf(stderr, gettext(
833045916cd2Sjpk 					    "Insufficient memory for label\n"));
833145916cd2Sjpk 					exit(1);
833245916cd2Sjpk 				}
833345916cd2Sjpk 				/* append LBL_TYPE */
833445916cd2Sjpk 				(void) snprintf(cmw_label, cmw_length,
833545916cd2Sjpk 				    "ADMIN_LOW [%s]", ascii);
833645916cd2Sjpk 				(void) append_secattr(secinfo, len,
833745916cd2Sjpk 				    strlen(cmw_label) + 1, cmw_label,
833845916cd2Sjpk 				    LBL_TYPE);
833945916cd2Sjpk 
834045916cd2Sjpk 				/* free storage */
834145916cd2Sjpk 				if (ascii != NULL) {
834245916cd2Sjpk 					free(cmw_label);
834345916cd2Sjpk 					free(ascii);
834445916cd2Sjpk 					ascii = (char *)0;
834545916cd2Sjpk 				}
834645916cd2Sjpk 			}
834745916cd2Sjpk 		}
834845916cd2Sjpk 		break;
834945916cd2Sjpk 
835045916cd2Sjpk 	default:
835145916cd2Sjpk 		break;
835245916cd2Sjpk 	} /* end switch for LBL_TYPE */
835345916cd2Sjpk 
835445916cd2Sjpk 
835545916cd2Sjpk 	/* DONE !! */
835645916cd2Sjpk 	return;
835745916cd2Sjpk 
835845916cd2Sjpk } /* end of append_ext_attr */
835945916cd2Sjpk 
836045916cd2Sjpk 
836145916cd2Sjpk /*
836245916cd2Sjpk  *	Name: extract_attr()
836345916cd2Sjpk  *
836445916cd2Sjpk  *	Description:
836545916cd2Sjpk  *		Process attributes from the ancillary file due to
836645916cd2Sjpk  *		the T option.
836745916cd2Sjpk  *
836845916cd2Sjpk  *	Call by doxtract() as part of the switch case structure.
836945916cd2Sjpk  *	Making this a separate routine because the nesting are too
837045916cd2Sjpk  *	deep in doxtract, thus, leaving very little space
837145916cd2Sjpk  *	on each line for instructions.
837245916cd2Sjpk  *
837345916cd2Sjpk  * With option 'T', we extract from a TS 8 or TS 2.5 ancillary file
837445916cd2Sjpk  *
837545916cd2Sjpk  * For option 'T', following are possible attributes in
837645916cd2Sjpk  * a TS 8 ancillary file: (NOTE: No IL support)
837745916cd2Sjpk  *
837845916cd2Sjpk  *	attribute type        attribute		process procedure
837945916cd2Sjpk  *	----------------      ----------------  -------------------------
838045916cd2Sjpk  *    #	LBL_TYPE       = 'L'   SL               construct binary label
838145916cd2Sjpk  *    #	APRIV_TYPE     = 'P'   allowed priv    	construct privileges
838245916cd2Sjpk  *    #	FPRIV_TYPE     = 'p'   forced priv	construct privileges
838345916cd2Sjpk  *    #	COMP_TYPE      = 'C'   path component	construct real path
838445916cd2Sjpk  *    #	DIR_TYPE       = 'D'   directory flag	note it is a directory
838545916cd2Sjpk  *    $	UFSD_ACL       = '1'   ACL data		construct ACL entries
838645916cd2Sjpk  *	ATTR_FLAG_TYPE = 'F'   file attr flags  construct binary flags
838745916cd2Sjpk  *	LK_COMP_TYPE   = 'K'   linked path comp construct linked real path
838845916cd2Sjpk  *
838945916cd2Sjpk  * note: # = attribute names common between TS 8 & TS 2.5 ancillary
839045916cd2Sjpk  *           files.
839145916cd2Sjpk  *       $ = ACL attribute is processed for the option 'p', it doesn't
839245916cd2Sjpk  *           need option 'T'.
839345916cd2Sjpk  *
839445916cd2Sjpk  * Trusted Extensions ignores APRIV_TYPE, FPRIV_TYPE, and ATTR_FLAG_TYPE
839545916cd2Sjpk  *
839645916cd2Sjpk  */
839745916cd2Sjpk static void
839845916cd2Sjpk extract_attr(char **file_ptr, struct sec_attr *attr)
839945916cd2Sjpk {
840045916cd2Sjpk 	int	reterr, err;
840145916cd2Sjpk 	char	*dummy_buf;	/* for attribute extract */
840245916cd2Sjpk 
840345916cd2Sjpk 	dummy_buf = attr->attr_info;
840445916cd2Sjpk 
840545916cd2Sjpk 	switch (attr->attr_type) {
840645916cd2Sjpk 
840745916cd2Sjpk 	case DIR_TYPE:
840845916cd2Sjpk 
840945916cd2Sjpk 		dir_flag++;
841045916cd2Sjpk 		break;
841145916cd2Sjpk 
841245916cd2Sjpk 	case LBL_TYPE:
841345916cd2Sjpk 
841445916cd2Sjpk 		/*
841545916cd2Sjpk 		 * LBL_TYPE is used to indicate SL for directory, and
841645916cd2Sjpk 		 * CMW label for other file types.
841745916cd2Sjpk 		 */
841845916cd2Sjpk 
841945916cd2Sjpk 		if (!dir_flag) { /* not directory */
842045916cd2Sjpk 			/* Skip over IL portion */
842145916cd2Sjpk 			char *sl_ptr = strchr(dummy_buf, '[');
842245916cd2Sjpk 
842345916cd2Sjpk 			if (sl_ptr == NULL)
842445916cd2Sjpk 				err = 0;
842545916cd2Sjpk 			else
842645916cd2Sjpk 				err = stobsl(sl_ptr, &bs_label,
842745916cd2Sjpk 				    NEW_LABEL, &reterr);
842845916cd2Sjpk 		} else { /* directory */
842945916cd2Sjpk 			err = stobsl(dummy_buf, &bs_label,
843045916cd2Sjpk 			    NEW_LABEL, &reterr);
843145916cd2Sjpk 		}
843245916cd2Sjpk 		if (err == 0) {
843345916cd2Sjpk 			(void) fprintf(stderr, gettext("tar: "
843445916cd2Sjpk 			    "can't convert %s to binary label\n"),
843545916cd2Sjpk 			    dummy_buf);
843645916cd2Sjpk 			bslundef(&bs_label);
843745916cd2Sjpk 		} else if (!blequal(&bs_label, &admin_low) &&
843845916cd2Sjpk 		    !blequal(&bs_label, &admin_high)) {
843945916cd2Sjpk 			bslabel_t *from_label;
844045916cd2Sjpk 			char *buf;
844145916cd2Sjpk 			char tempbuf[MAXPATHLEN];
844245916cd2Sjpk 
844345916cd2Sjpk 			if (*orig_namep != '/') {
844445916cd2Sjpk 				/* got relative linked to path */
844545916cd2Sjpk 				(void) getcwd(tempbuf, (sizeof (tempbuf)));
844645916cd2Sjpk 				(void) strncat(tempbuf, "/", MAXPATHLEN);
844745916cd2Sjpk 			} else
844845916cd2Sjpk 				*tempbuf = '\0';
844945916cd2Sjpk 
845045916cd2Sjpk 			buf = real_path;
845145916cd2Sjpk 			(void) strncat(tempbuf, orig_namep, MAXPATHLEN);
845245916cd2Sjpk 			from_label = getlabelbypath(tempbuf);
845345916cd2Sjpk 			if (from_label != NULL) {
845445916cd2Sjpk 				if (blequal(from_label, &admin_low)) {
845545916cd2Sjpk 					if ((getpathbylabel(tempbuf, buf,
845645916cd2Sjpk 					    MAXPATHLEN, &bs_label) == NULL)) {
845745916cd2Sjpk 						(void) fprintf(stderr,
845845916cd2Sjpk 						    gettext("tar: "
845945916cd2Sjpk 						"can't get zone root path for "
846045916cd2Sjpk 						"%s\n"), tempbuf);
846145916cd2Sjpk 					} else
846245916cd2Sjpk 						rpath_flag = 1;
846345916cd2Sjpk 				}
846445916cd2Sjpk 				free(from_label);
846545916cd2Sjpk 			}
846645916cd2Sjpk 		}
846745916cd2Sjpk 		break;
846845916cd2Sjpk 
846945916cd2Sjpk 	case COMP_TYPE:
847045916cd2Sjpk 
847145916cd2Sjpk 		rebuild_comp_path(dummy_buf, file_ptr);
847245916cd2Sjpk 		break;
847345916cd2Sjpk 
847445916cd2Sjpk 	case LK_COMP_TYPE:
847545916cd2Sjpk 
847645916cd2Sjpk 		if (rebuild_lk_comp_path(dummy_buf, file_ptr)
847745916cd2Sjpk 		    == 0) {
847845916cd2Sjpk 			lk_rpath_flag = 1;
847945916cd2Sjpk 		} else {
848045916cd2Sjpk 			(void) fprintf(stderr, gettext("tar: warning: link's "
848145916cd2Sjpk 			    "target pathname might be invalid.\n"));
848245916cd2Sjpk 			lk_rpath_flag = 0;
848345916cd2Sjpk 		}
848445916cd2Sjpk 		break;
848545916cd2Sjpk 	case APRIV_TYPE:
848645916cd2Sjpk 		ignored_aprivs++;
848745916cd2Sjpk 		break;
848845916cd2Sjpk 	case FPRIV_TYPE:
848945916cd2Sjpk 		ignored_fprivs++;
849045916cd2Sjpk 		break;
849145916cd2Sjpk 	case ATTR_FLAG_TYPE:
849245916cd2Sjpk 		ignored_fattrs++;
849345916cd2Sjpk 		break;
849445916cd2Sjpk 
849545916cd2Sjpk 	default:
849645916cd2Sjpk 
849745916cd2Sjpk 		break;
849845916cd2Sjpk 	}
849945916cd2Sjpk 
850045916cd2Sjpk 	/* done */
850145916cd2Sjpk 	return;
850245916cd2Sjpk 
850345916cd2Sjpk }	/* end extract_attr */
850445916cd2Sjpk 
850545916cd2Sjpk 
850645916cd2Sjpk 
850745916cd2Sjpk /*
850845916cd2Sjpk  *	Name:	rebuild_comp_path()
850945916cd2Sjpk  *
851045916cd2Sjpk  *	Description:
851145916cd2Sjpk  *		Take the string of components passed down by the calling
851245916cd2Sjpk  *		routine and parse the values and rebuild the path.
851345916cd2Sjpk  *		This routine no longer needs to produce a new real_path
851445916cd2Sjpk  *		string because it is produced when the 'L' LABEL_TYPE is
851545916cd2Sjpk  *		interpreted. So the only thing done here is to distinguish
851645916cd2Sjpk  *		between an SLD and an MLD entry. We only want one, so we
851745916cd2Sjpk  *		ignore the MLD entry by setting the mld_flag.
851845916cd2Sjpk  *
851945916cd2Sjpk  *	return value:
852045916cd2Sjpk  *		none
852145916cd2Sjpk  */
852245916cd2Sjpk static void
852345916cd2Sjpk rebuild_comp_path(char *str, char **namep)
852445916cd2Sjpk {
852545916cd2Sjpk 	char		*cp;
852645916cd2Sjpk 
852745916cd2Sjpk 	while (*str != '\0') {
852845916cd2Sjpk 
852945916cd2Sjpk 		switch (*str) {
853045916cd2Sjpk 
853145916cd2Sjpk 		case MLD_TYPE:
853245916cd2Sjpk 
853345916cd2Sjpk 			str++;
853445916cd2Sjpk 			if ((cp = strstr(str, ";;")) != NULL) {
853545916cd2Sjpk 				*cp = '\0';
853645916cd2Sjpk 				str = cp + 2;
853745916cd2Sjpk 				*cp = ';';
853845916cd2Sjpk 			}
853945916cd2Sjpk 			mld_flag = 1;
854045916cd2Sjpk 			break;
854145916cd2Sjpk 
854245916cd2Sjpk 		case SLD_TYPE:
854345916cd2Sjpk 
854445916cd2Sjpk 			str++;
854545916cd2Sjpk 			if ((cp = strstr(str, ";;")) != NULL) {
854645916cd2Sjpk 				*cp = '\0';
854745916cd2Sjpk 				str = cp + 2;
854845916cd2Sjpk 				*cp = ';';
854945916cd2Sjpk 			}
855045916cd2Sjpk 			mld_flag = 0;
855145916cd2Sjpk 			break;
855245916cd2Sjpk 
855345916cd2Sjpk 		case PATH_TYPE:
855445916cd2Sjpk 
855545916cd2Sjpk 			str++;
855645916cd2Sjpk 			if ((cp = strstr(str, ";;")) != NULL) {
855745916cd2Sjpk 				*cp = '\0';
855845916cd2Sjpk 				str = cp + 2;
855945916cd2Sjpk 				*cp = ';';
856045916cd2Sjpk 			}
856145916cd2Sjpk 			break;
856245916cd2Sjpk 		}
856345916cd2Sjpk 	}
856445916cd2Sjpk 	if (rpath_flag)
856545916cd2Sjpk 		*namep = real_path;
856645916cd2Sjpk 	return;
856745916cd2Sjpk 
856845916cd2Sjpk } /* end rebuild_comp_path() */
856945916cd2Sjpk 
857045916cd2Sjpk /*
857145916cd2Sjpk  *	Name:	rebuild_lk_comp_path()
857245916cd2Sjpk  *
857345916cd2Sjpk  *	Description:
857445916cd2Sjpk  *		Take the string of components passed down by the calling
857545916cd2Sjpk  *		routine and parse the values and rebuild the path.
857645916cd2Sjpk  *
857745916cd2Sjpk  *	return value:
857845916cd2Sjpk  *		0 = succeeded
857945916cd2Sjpk  *		-1 = failed
858045916cd2Sjpk  */
858145916cd2Sjpk static int
858245916cd2Sjpk rebuild_lk_comp_path(char *str, char **namep)
858345916cd2Sjpk {
858445916cd2Sjpk 	char		*cp;
858545916cd2Sjpk 	int		reterr;
858645916cd2Sjpk 	bslabel_t	bslabel;
858745916cd2Sjpk 	char		*buf;
858845916cd2Sjpk 	char		pbuf[MAXPATHLEN];
858945916cd2Sjpk 	char		*ptr1, *ptr2;
859045916cd2Sjpk 	int		plen;
859145916cd2Sjpk 	int		use_pbuf;
859245916cd2Sjpk 	char		tempbuf[MAXPATHLEN];
859345916cd2Sjpk 	int		mismatch;
859445916cd2Sjpk 	bslabel_t	*from_label;
859545916cd2Sjpk 	char		zonename[ZONENAME_MAX];
859645916cd2Sjpk 	zoneid_t	zoneid;
859745916cd2Sjpk 
859845916cd2Sjpk 	/* init stuff */
859945916cd2Sjpk 	use_pbuf = 0;
860045916cd2Sjpk 	mismatch = 0;
860145916cd2Sjpk 
860245916cd2Sjpk 	/*
860345916cd2Sjpk 	 * For linked to pathname (LK_COMP_TYPE):
860445916cd2Sjpk 	 *  - If the linked to pathname is absolute (start with /), we
860545916cd2Sjpk 	 *    will use it as is.
860645916cd2Sjpk 	 *  - If it is a relative pathname then it is relative to 1 of 2
860745916cd2Sjpk 	 *    directories.  For a hardlink, it is relative to the current
860845916cd2Sjpk 	 *    directory.  For a symbolic link, it is relative to the
860945916cd2Sjpk 	 *    directory the symbolic link is in.  For the symbolic link
861045916cd2Sjpk 	 *    case, set a flag to indicate we need to use the prefix of
861145916cd2Sjpk 	 *    the restored file's pathname with the linked to pathname.
861245916cd2Sjpk 	 *
861345916cd2Sjpk 	 *    NOTE: At this point, we have no way to determine if we have
861445916cd2Sjpk 	 *    a hardlink or a symbolic link.  We will compare the 1st
861545916cd2Sjpk 	 *    component in the prefix portion of the restore file's
861645916cd2Sjpk 	 *    pathname to the 1st component in the attribute data
861745916cd2Sjpk 	 *    (the linked pathname).  If they are the same, we will assume
861845916cd2Sjpk 	 *    the link pathname to reconstruct is relative to the current
861945916cd2Sjpk 	 *    directory.  Otherwise, we will set a flag indicate we need
862045916cd2Sjpk 	 *    to use a prefix with the reconstructed name.  Need to compare
862145916cd2Sjpk 	 *    both the adorned and unadorned version before deciding a
862245916cd2Sjpk 	 *    mismatch.
862345916cd2Sjpk 	 */
862445916cd2Sjpk 
862545916cd2Sjpk 	buf = lk_real_path;
862645916cd2Sjpk 	if (*(str + 1) != '/') { /* got relative linked to path */
862745916cd2Sjpk 		ptr1 = orig_namep;
862845916cd2Sjpk 		ptr2 = strrchr(ptr1, '/');
862945916cd2Sjpk 		plen = ptr2 - ptr1;
863045916cd2Sjpk 		if (plen > 0) {
863145916cd2Sjpk 			pbuf[0] = '\0';
863245916cd2Sjpk 			plen++;		/* include '/' */
863345916cd2Sjpk 			(void) strncpy(pbuf, ptr1, plen);
863445916cd2Sjpk 			*(pbuf + plen) = '\0';
863545916cd2Sjpk 			ptr2 = strchr(pbuf, '/');
863645916cd2Sjpk 			if (strncmp(pbuf, str + 1, ptr2 - pbuf) != 0)
863745916cd2Sjpk 				mismatch = 1;
863845916cd2Sjpk 		}
863945916cd2Sjpk 
864045916cd2Sjpk 		if (mismatch == 1)
864145916cd2Sjpk 			use_pbuf = 1;
864245916cd2Sjpk 	}
864345916cd2Sjpk 
864445916cd2Sjpk 	buf[0] = '\0';
864545916cd2Sjpk 
864645916cd2Sjpk 	while (*str != '\0') {
864745916cd2Sjpk 
864845916cd2Sjpk 		switch (*str) {
864945916cd2Sjpk 
865045916cd2Sjpk 		case MLD_TYPE:
865145916cd2Sjpk 
865245916cd2Sjpk 			str++;
865345916cd2Sjpk 			if ((cp = strstr(str, ";;")) != NULL) {
865445916cd2Sjpk 				*cp = '\0';
865545916cd2Sjpk 
865645916cd2Sjpk 				/*
865745916cd2Sjpk 				 * Ignore attempts to backup over .MLD.
865845916cd2Sjpk 				 */
865945916cd2Sjpk 				if (strcmp(str, "../") != 0)
866045916cd2Sjpk 					(void) strncat(buf, str, MAXPATHLEN);
866145916cd2Sjpk 				str = cp + 2;
866245916cd2Sjpk 				*cp = ';';
866345916cd2Sjpk 			}
866445916cd2Sjpk 			break;
866545916cd2Sjpk 
866645916cd2Sjpk 		case SLD_TYPE:
866745916cd2Sjpk 
866845916cd2Sjpk 			str++;
866945916cd2Sjpk 			if ((cp = strstr(str, ";;")) != NULL) {
867045916cd2Sjpk 				*cp = '\0';
867145916cd2Sjpk 
867245916cd2Sjpk 				/*
867345916cd2Sjpk 				 * Use the path name in the header if
867445916cd2Sjpk 				 * error occurs when processing the
867545916cd2Sjpk 				 * SLD type.
867645916cd2Sjpk 				 */
867745916cd2Sjpk 
867845916cd2Sjpk 				if (!stobsl(str, &bslabel,
867945916cd2Sjpk 				    NO_CORRECTION, &reterr)) {
868045916cd2Sjpk 					(void) fprintf(stderr, gettext(
868145916cd2Sjpk 					    "tar: can't translate to binary"
868245916cd2Sjpk 					    "SL for SLD, stobsl() error:"
868345916cd2Sjpk 					    " %s\n"), strerror(errno));
868445916cd2Sjpk 					return (-1);
868545916cd2Sjpk 				}
868645916cd2Sjpk 
868745916cd2Sjpk 				str = cp + 2;
868845916cd2Sjpk 				*cp = ';';
868945916cd2Sjpk 
869045916cd2Sjpk 				if (use_pbuf == 1) {
869145916cd2Sjpk 					if (*pbuf != '/') {
869245916cd2Sjpk 						/* relative linked to path */
869345916cd2Sjpk 
869445916cd2Sjpk 						(void) getcwd(tempbuf,
869545916cd2Sjpk 						    (sizeof (tempbuf)));
869645916cd2Sjpk 						(void) strncat(tempbuf, "/",
869745916cd2Sjpk 						    MAXPATHLEN);
869845916cd2Sjpk 						(void) strncat(tempbuf, pbuf,
869945916cd2Sjpk 						    MAXPATHLEN);
870045916cd2Sjpk 					}
870145916cd2Sjpk 					else
870245916cd2Sjpk 						(void) strcpy(tempbuf, pbuf);
870345916cd2Sjpk 
870445916cd2Sjpk 				} else if (*buf != '/') {
870545916cd2Sjpk 					/* got relative linked to path */
870645916cd2Sjpk 
870745916cd2Sjpk 					(void) getcwd(tempbuf,
870845916cd2Sjpk 					    (sizeof (tempbuf)));
870945916cd2Sjpk 					(void) strncat(tempbuf, "/",
871045916cd2Sjpk 					    MAXPATHLEN);
871145916cd2Sjpk 				} else
871245916cd2Sjpk 					*tempbuf = '\0';
871345916cd2Sjpk 
871445916cd2Sjpk 				(void) strncat(tempbuf, buf, MAXPATHLEN);
871545916cd2Sjpk 				*buf = '\0';
871645916cd2Sjpk 
871745916cd2Sjpk 				if (blequal(&bslabel, &admin_high)) {
871845916cd2Sjpk 					bslabel = admin_low;
871945916cd2Sjpk 				}
872045916cd2Sjpk 
872145916cd2Sjpk 
872245916cd2Sjpk 				/*
872345916cd2Sjpk 				 * Check for cross-zone symbolic links
872445916cd2Sjpk 				 */
872545916cd2Sjpk 				from_label = getlabelbypath(real_path);
872645916cd2Sjpk 				if (rpath_flag && (from_label != NULL) &&
872745916cd2Sjpk 				    !blequal(&bslabel, from_label)) {
872845916cd2Sjpk 					if ((zoneid =
872945916cd2Sjpk 					    getzoneidbylabel(&bslabel)) == -1) {
873045916cd2Sjpk 						(void) fprintf(stderr,
873145916cd2Sjpk 						    gettext("tar: can't get "
873245916cd2Sjpk 						    "zone ID for %s\n"),
873345916cd2Sjpk 						    tempbuf);
873445916cd2Sjpk 						return (-1);
873545916cd2Sjpk 					}
873645916cd2Sjpk 					if (zone_getattr(zoneid, ZONE_ATTR_NAME,
873745916cd2Sjpk 					    &zonename, ZONENAME_MAX) == -1) {
873845916cd2Sjpk 						/* Badly configured zone info */
873945916cd2Sjpk 						(void) fprintf(stderr,
874045916cd2Sjpk 						    gettext("tar: can't get "
874145916cd2Sjpk 						    "zonename for %s\n"),
874245916cd2Sjpk 						    tempbuf);
874345916cd2Sjpk 						return (-1);
874445916cd2Sjpk 					}
874545916cd2Sjpk 					(void) strncpy(buf, AUTO_ZONE,
874645916cd2Sjpk 					    MAXPATHLEN);
874745916cd2Sjpk 					(void) strncat(buf, "/",
874845916cd2Sjpk 					    MAXPATHLEN);
874945916cd2Sjpk 					(void) strncat(buf, zonename,
875045916cd2Sjpk 					    MAXPATHLEN);
875145916cd2Sjpk 				}
875245916cd2Sjpk 				if (from_label != NULL)
875345916cd2Sjpk 					free(from_label);
875445916cd2Sjpk 				(void) strncat(buf, tempbuf, MAXPATHLEN);
875545916cd2Sjpk 				break;
875645916cd2Sjpk 			}
875745916cd2Sjpk 			mld_flag = 0;
875845916cd2Sjpk 			break;
875945916cd2Sjpk 
876045916cd2Sjpk 		case PATH_TYPE:
876145916cd2Sjpk 
876245916cd2Sjpk 			str++;
876345916cd2Sjpk 			if ((cp = strstr(str, ";;")) != NULL) {
876445916cd2Sjpk 				*cp = '\0';
876545916cd2Sjpk 				(void) strncat(buf, str, MAXPATHLEN);
876645916cd2Sjpk 				str = cp + 2;
876745916cd2Sjpk 				*cp = ';';
876845916cd2Sjpk 			}
876945916cd2Sjpk 			break;
877045916cd2Sjpk 
877145916cd2Sjpk 		default:
877245916cd2Sjpk 
877345916cd2Sjpk 			(void) fprintf(stderr, gettext(
877445916cd2Sjpk 			    "tar: error rebuilding path %s\n"),
877545916cd2Sjpk 			    *namep);
877645916cd2Sjpk 			*buf = '\0';
877745916cd2Sjpk 			str++;
877845916cd2Sjpk 			return (-1);
877945916cd2Sjpk 		}
878045916cd2Sjpk 	}
878145916cd2Sjpk 
878245916cd2Sjpk 	/*
878345916cd2Sjpk 	 * Done for LK_COMP_TYPE
878445916cd2Sjpk 	 */
878545916cd2Sjpk 
878645916cd2Sjpk 	return (0);    /* component path is rebuilt successfully */
878745916cd2Sjpk 
878845916cd2Sjpk } /* end rebuild_lk_comp_path() */
878945916cd2Sjpk 
879045916cd2Sjpk /*
879145916cd2Sjpk  *	Name: check_ext_attr()
879245916cd2Sjpk  *
879345916cd2Sjpk  *	Description:
879445916cd2Sjpk  *		Check the extended attributes for a file being extracted.
879545916cd2Sjpk  *		The attributes being checked here are CMW labels.
879645916cd2Sjpk  *		ACLs are not set here because they are set by the
879745916cd2Sjpk  *		pflag in doxtract().
879845916cd2Sjpk  *
879945916cd2Sjpk  *		If the label doesn't match, return 0
880045916cd2Sjpk  *		else return 1
880145916cd2Sjpk  */
880245916cd2Sjpk static int
880345916cd2Sjpk check_ext_attr(char *filename)
880445916cd2Sjpk {
880545916cd2Sjpk 	bslabel_t	currentlabel;	/* label from zone */
880645916cd2Sjpk 
880745916cd2Sjpk 	if (bltype(&bs_label, SUN_SL_UN)) {
880845916cd2Sjpk 		/* No label check possible */
880945916cd2Sjpk 		return (0);
881045916cd2Sjpk 	}
881145916cd2Sjpk 	if (getlabel(filename, &currentlabel) != 0) {
881245916cd2Sjpk 		(void) fprintf(stderr,
881345916cd2Sjpk 		    gettext("tar: can't get label for "
881445916cd2Sjpk 		    " %s, getlabel() error: %s\n"),
881545916cd2Sjpk 		    filename, strerror(errno));
881645916cd2Sjpk 		return (0);
881745916cd2Sjpk 	} else if ((blequal(&currentlabel, &bs_label)) == 0) {
881845916cd2Sjpk 		char	*src_label = NULL;	/* ascii label */
881945916cd2Sjpk 
882045916cd2Sjpk 		/* get current src SL */
882145916cd2Sjpk 		if (bsltos(&bs_label, &src_label, 0, 0) <= 0) {
882245916cd2Sjpk 			(void) fprintf(stderr,
882345916cd2Sjpk 			    gettext("tar: can't interpret requested label for"
882445916cd2Sjpk 			    " %s\n"), filename);
882545916cd2Sjpk 		} else {
882645916cd2Sjpk 			(void) fprintf(stderr,
882745916cd2Sjpk 			    gettext("tar: can't apply label %s to %s\n"),
882845916cd2Sjpk 			    src_label, filename);
882945916cd2Sjpk 			free(src_label);
883045916cd2Sjpk 		}
883145916cd2Sjpk 		(void) fprintf(stderr,
883245916cd2Sjpk 		    gettext("tar: %s not restored\n"), filename);
883345916cd2Sjpk 		return (0);
883445916cd2Sjpk 	}
883545916cd2Sjpk 	return (1);
883645916cd2Sjpk 
883745916cd2Sjpk }	/* end check_ext_attr */
8838