xref: /titanic_44/usr/src/cmd/tar/tar.c (revision c536b1f93b31eba539c635a91be8ee2ab0fcb15a)
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 /*
22257ece65SRalph Turner - Sun UK - Contractor  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
23506ea5b8SMilan Jurik  * Copyright 2012 Milan Jurik. All rights reserved.
24c92bf99eSJoshua M. Clulow  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
317c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
357c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <sys/param.h>
417c478bd9Sstevel@tonic-gate #include <sys/stat.h>
427c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
437c478bd9Sstevel@tonic-gate #include <sys/wait.h>
447c478bd9Sstevel@tonic-gate #include <dirent.h>
457c478bd9Sstevel@tonic-gate #include <errno.h>
467c478bd9Sstevel@tonic-gate #include <stdio.h>
477c478bd9Sstevel@tonic-gate #include <signal.h>
487c478bd9Sstevel@tonic-gate #include <ctype.h>
497c478bd9Sstevel@tonic-gate #include <locale.h>
507c478bd9Sstevel@tonic-gate #include <nl_types.h>
517c478bd9Sstevel@tonic-gate #include <langinfo.h>
527c478bd9Sstevel@tonic-gate #include <pwd.h>
537c478bd9Sstevel@tonic-gate #include <grp.h>
547c478bd9Sstevel@tonic-gate #include <fcntl.h>
557c478bd9Sstevel@tonic-gate #include <string.h>
567c478bd9Sstevel@tonic-gate #include <malloc.h>
577c478bd9Sstevel@tonic-gate #include <time.h>
587c478bd9Sstevel@tonic-gate #include <utime.h>
597c478bd9Sstevel@tonic-gate #include <stdlib.h>
607c478bd9Sstevel@tonic-gate #include <stdarg.h>
617c478bd9Sstevel@tonic-gate #include <widec.h>
627c478bd9Sstevel@tonic-gate #include <sys/mtio.h>
637c478bd9Sstevel@tonic-gate #include <sys/acl.h>
647c478bd9Sstevel@tonic-gate #include <strings.h>
657c478bd9Sstevel@tonic-gate #include <deflt.h>
667c478bd9Sstevel@tonic-gate #include <limits.h>
677c478bd9Sstevel@tonic-gate #include <iconv.h>
687c478bd9Sstevel@tonic-gate #include <assert.h>
69da6c28aaSamw #include <libgen.h>
70da6c28aaSamw #include <libintl.h>
71fa9e4066Sahrens #include <aclutils.h>
72da6c28aaSamw #include <libnvpair.h>
73da6c28aaSamw #include <archives.h>
743d63ea05Sas145665 
757c478bd9Sstevel@tonic-gate #if defined(__SunOS_5_6) || defined(__SunOS_5_7)
767c478bd9Sstevel@tonic-gate extern int defcntl();
777c478bd9Sstevel@tonic-gate #endif
78da6c28aaSamw #if defined(_PC_SATTR_ENABLED)
79da6c28aaSamw #include <attr.h>
80da6c28aaSamw #include <libcmdutils.h>
81da6c28aaSamw #endif
827c478bd9Sstevel@tonic-gate 
8345916cd2Sjpk /* Trusted Extensions */
8445916cd2Sjpk #include <zone.h>
8545916cd2Sjpk #include <tsol/label.h>
8645916cd2Sjpk #include <sys/tsol/label_macro.h>
8745916cd2Sjpk 
88da6c28aaSamw #include "getresponse.h"
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate  * Source compatibility
917c478bd9Sstevel@tonic-gate  */
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * These constants come from archives.h and sys/fcntl.h
957c478bd9Sstevel@tonic-gate  * and were introduced by the extended attributes project
967c478bd9Sstevel@tonic-gate  * in Solaris 9.
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate #if !defined(O_XATTR)
997c478bd9Sstevel@tonic-gate #define	AT_SYMLINK_NOFOLLOW	0x1000
1007c478bd9Sstevel@tonic-gate #define	AT_REMOVEDIR		0x1
1017c478bd9Sstevel@tonic-gate #define	AT_FDCWD		0xffd19553
1027c478bd9Sstevel@tonic-gate #define	_XATTR_HDRTYPE		'E'
1037c478bd9Sstevel@tonic-gate static int attropen();
1047c478bd9Sstevel@tonic-gate static int fstatat();
1057c478bd9Sstevel@tonic-gate static int renameat();
1067c478bd9Sstevel@tonic-gate static int unlinkat();
1077c478bd9Sstevel@tonic-gate static int openat();
1087c478bd9Sstevel@tonic-gate static int fchownat();
1097c478bd9Sstevel@tonic-gate static int futimesat();
1107c478bd9Sstevel@tonic-gate #endif
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate /*
1137c478bd9Sstevel@tonic-gate  * Compiling with -D_XPG4_2 gets this but produces other problems, so
1147c478bd9Sstevel@tonic-gate  * instead of including sys/time.h and compiling with -D_XPG4_2, I'm
1157c478bd9Sstevel@tonic-gate  * explicitly doing the declaration here.
1167c478bd9Sstevel@tonic-gate  */
1177c478bd9Sstevel@tonic-gate int utimes(const char *path, const struct timeval timeval_ptr[]);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate #ifndef MINSIZE
1207c478bd9Sstevel@tonic-gate #define	MINSIZE 250
1217c478bd9Sstevel@tonic-gate #endif
1227c478bd9Sstevel@tonic-gate #define	DEF_FILE "/etc/default/tar"
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate #define	min(a, b)  ((a) < (b) ? (a) : (b))
1257c478bd9Sstevel@tonic-gate #define	max(a, b)  ((a) > (b) ? (a) : (b))
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /* -DDEBUG	ONLY for debugging */
1287c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1297c478bd9Sstevel@tonic-gate #undef	DEBUG
1307c478bd9Sstevel@tonic-gate #define	DEBUG(a, b, c)\
1317c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "DEBUG - "), (void) fprintf(stderr, a, b, c)
1327c478bd9Sstevel@tonic-gate #endif
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate #define	TBLOCK	512	/* tape block size--should be universal */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate #ifdef	BSIZE
1377c478bd9Sstevel@tonic-gate #define	SYS_BLOCK BSIZE	/* from sys/param.h:  secondary block size */
1387c478bd9Sstevel@tonic-gate #else	/* BSIZE */
1397c478bd9Sstevel@tonic-gate #define	SYS_BLOCK 512	/* default if no BSIZE in param.h */
1407c478bd9Sstevel@tonic-gate #endif	/* BSIZE */
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate #define	NBLOCK	20
1437c478bd9Sstevel@tonic-gate #define	NAMSIZ	100
1447c478bd9Sstevel@tonic-gate #define	PRESIZ	155
1457c478bd9Sstevel@tonic-gate #define	MAXNAM	256
1467c478bd9Sstevel@tonic-gate #define	MODEMASK 0777777	/* file creation mode mask */
1477c478bd9Sstevel@tonic-gate #define	POSIXMODES 07777	/* mask for POSIX mode bits */
1487c478bd9Sstevel@tonic-gate #define	MAXEXT	9	/* reasonable max # extents for a file */
1497c478bd9Sstevel@tonic-gate #define	EXTMIN	50	/* min blks left on floppy to split a file */
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate /* max value dblock.dbuf.efsize can store */
1527c478bd9Sstevel@tonic-gate #define	TAR_EFSIZE_MAX	 0777777777
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate /*
1557c478bd9Sstevel@tonic-gate  * Symbols which specify the values at which the use of the 'E' function
1567c478bd9Sstevel@tonic-gate  * modifier is required to properly store a file.
1577c478bd9Sstevel@tonic-gate  *
1587c478bd9Sstevel@tonic-gate  *     TAR_OFFSET_MAX    - the largest file size we can archive
1597c478bd9Sstevel@tonic-gate  *     OCTAL7CHAR        - the limit for ustar gid, uid, dev
1607c478bd9Sstevel@tonic-gate  */
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate #ifdef XHDR_DEBUG
1637c478bd9Sstevel@tonic-gate /* tiny values which force the creation of extended header entries */
1647c478bd9Sstevel@tonic-gate #define	TAR_OFFSET_MAX 9
1657c478bd9Sstevel@tonic-gate #define	OCTAL7CHAR 2
1667c478bd9Sstevel@tonic-gate #else
1677c478bd9Sstevel@tonic-gate /* normal values */
168b7d62af5Sceastha #define	TAR_OFFSET_MAX	077777777777ULL
1697c478bd9Sstevel@tonic-gate #define	OCTAL7CHAR	07777777
1707c478bd9Sstevel@tonic-gate #endif
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate #define	TBLOCKS(bytes)	(((bytes) + TBLOCK - 1) / TBLOCK)
1737c478bd9Sstevel@tonic-gate #define	K(tblocks)	((tblocks+1)/2)	/* tblocks to Kbytes for printing */
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate #define	MAXLEV	(PATH_MAX / 2)
1767c478bd9Sstevel@tonic-gate #define	LEV0	1
1777c478bd9Sstevel@tonic-gate #define	SYMLINK_LEV0	0
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate #define	TRUE	1
1807c478bd9Sstevel@tonic-gate #define	FALSE	0
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate #define	XATTR_FILE	1
1837c478bd9Sstevel@tonic-gate #define	NORMAL_FILE	0
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate #define	PUT_AS_LINK	1
1867c478bd9Sstevel@tonic-gate #define	PUT_NOTAS_LINK	0
1877c478bd9Sstevel@tonic-gate 
188da6c28aaSamw #ifndef VIEW_READONLY
189da6c28aaSamw #define	VIEW_READONLY	"SUNWattr_ro"
190da6c28aaSamw #endif
191da6c28aaSamw 
192da6c28aaSamw #ifndef VIEW_READWRITE
193da6c28aaSamw #define	VIEW_READWRITE	"SUNWattr_rw"
194da6c28aaSamw #endif
195da6c28aaSamw 
1967c478bd9Sstevel@tonic-gate #if _FILE_OFFSET_BITS == 64
1977c478bd9Sstevel@tonic-gate #define	FMT_off_t "lld"
1987c478bd9Sstevel@tonic-gate #define	FMT_off_t_o "llo"
1997c478bd9Sstevel@tonic-gate #define	FMT_blkcnt_t "lld"
2007c478bd9Sstevel@tonic-gate #else
2017c478bd9Sstevel@tonic-gate #define	FMT_off_t "ld"
2027c478bd9Sstevel@tonic-gate #define	FMT_off_t_o "lo"
2037c478bd9Sstevel@tonic-gate #define	FMT_blkcnt_t "ld"
2047c478bd9Sstevel@tonic-gate #endif
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /* ACL support */
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate static
2097c478bd9Sstevel@tonic-gate struct	sec_attr {
2107c478bd9Sstevel@tonic-gate 	char	attr_type;
2117c478bd9Sstevel@tonic-gate 	char	attr_len[7];
2127c478bd9Sstevel@tonic-gate 	char	attr_info[1];
2137c478bd9Sstevel@tonic-gate } *attr;
2147c478bd9Sstevel@tonic-gate 
215da6c28aaSamw #if defined(O_XATTR)
216da6c28aaSamw typedef enum {
217da6c28aaSamw 	ATTR_OK,
218da6c28aaSamw 	ATTR_SKIP,
219da6c28aaSamw 	ATTR_CHDIR_ERR,
220da6c28aaSamw 	ATTR_OPEN_ERR,
221da6c28aaSamw 	ATTR_XATTR_ERR,
222da6c28aaSamw 	ATTR_SATTR_ERR
223da6c28aaSamw } attr_status_t;
224da6c28aaSamw #endif
225da6c28aaSamw 
226da6c28aaSamw #if defined(O_XATTR)
227da6c28aaSamw typedef enum {
228da6c28aaSamw 	ARC_CREATE,
229da6c28aaSamw 	ARC_RESTORE
230da6c28aaSamw } arc_action_t;
231da6c28aaSamw #endif
232da6c28aaSamw 
233da6c28aaSamw typedef struct attr_data {
234da6c28aaSamw 	char	*attr_parent;
235da6c28aaSamw 	char	*attr_path;
236da6c28aaSamw 	int	attr_parentfd;
237da6c28aaSamw 	int	attr_rw_sysattr;
238da6c28aaSamw } attr_data_t;
239da6c28aaSamw 
2407c478bd9Sstevel@tonic-gate /*
2417c478bd9Sstevel@tonic-gate  *
2427c478bd9Sstevel@tonic-gate  * Tar has been changed to support extended attributes.
2437c478bd9Sstevel@tonic-gate  *
2447c478bd9Sstevel@tonic-gate  * As part of this change tar now uses the new *at() syscalls
2457c478bd9Sstevel@tonic-gate  * such as openat, fchownat(), unlinkat()...
2467c478bd9Sstevel@tonic-gate  *
2477c478bd9Sstevel@tonic-gate  * This was done so that attributes can be handled with as few code changes
2487c478bd9Sstevel@tonic-gate  * as possible.
2497c478bd9Sstevel@tonic-gate  *
2507c478bd9Sstevel@tonic-gate  * What this means is that tar now opens the directory that a file or directory
2517c478bd9Sstevel@tonic-gate  * resides in and then performs *at() functions to manipulate the entry.
2527c478bd9Sstevel@tonic-gate  *
2537c478bd9Sstevel@tonic-gate  * For example a new file is now created like this:
2547c478bd9Sstevel@tonic-gate  *
2557c478bd9Sstevel@tonic-gate  * dfd = open(<some dir path>)
2567c478bd9Sstevel@tonic-gate  * fd = openat(dfd, <name>,....);
2577c478bd9Sstevel@tonic-gate  *
2587c478bd9Sstevel@tonic-gate  * or in the case of an extended attribute
2597c478bd9Sstevel@tonic-gate  *
2607c478bd9Sstevel@tonic-gate  * dfd = attropen(<pathname>, ".", ....)
2617c478bd9Sstevel@tonic-gate  *
2627c478bd9Sstevel@tonic-gate  * Once we have a directory file descriptor all of the *at() functions can
2637c478bd9Sstevel@tonic-gate  * be applied to it.
2647c478bd9Sstevel@tonic-gate  *
2657c478bd9Sstevel@tonic-gate  * unlinkat(dfd, <component name>,...)
2667c478bd9Sstevel@tonic-gate  * fchownat(dfd, <component name>,..)
2677c478bd9Sstevel@tonic-gate  *
2687c478bd9Sstevel@tonic-gate  * This works for both normal namespace files and extended attribute file
2697c478bd9Sstevel@tonic-gate  *
2707c478bd9Sstevel@tonic-gate  */
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate /*
2737c478bd9Sstevel@tonic-gate  *
2747c478bd9Sstevel@tonic-gate  * Extended attribute Format
2757c478bd9Sstevel@tonic-gate  *
2767c478bd9Sstevel@tonic-gate  * Extended attributes are stored in two pieces.
2777c478bd9Sstevel@tonic-gate  * 1. An attribute header which has information about
2787c478bd9Sstevel@tonic-gate  *    what file the attribute is for and what the attribute
2797c478bd9Sstevel@tonic-gate  *    is named.
2807c478bd9Sstevel@tonic-gate  * 2. The attribute record itself.  Stored as a normal file type
2817c478bd9Sstevel@tonic-gate  *    of entry.
2827c478bd9Sstevel@tonic-gate  * Both the header and attribute record have special modes/typeflags
2837c478bd9Sstevel@tonic-gate  * associated with them.
2847c478bd9Sstevel@tonic-gate  *
2857c478bd9Sstevel@tonic-gate  * The names of the header in the archive look like:
2867c478bd9Sstevel@tonic-gate  * /dev/null/attr.hdr
2877c478bd9Sstevel@tonic-gate  *
2887c478bd9Sstevel@tonic-gate  * The name of the attribute looks like:
2897c478bd9Sstevel@tonic-gate  * /dev/null/attr
2907c478bd9Sstevel@tonic-gate  *
2917c478bd9Sstevel@tonic-gate  * This is done so that an archiver that doesn't understand these formats
2927c478bd9Sstevel@tonic-gate  * can just dispose of the attribute records.
2937c478bd9Sstevel@tonic-gate  *
2947c478bd9Sstevel@tonic-gate  * The format is composed of a fixed size header followed
2957c478bd9Sstevel@tonic-gate  * by a variable sized xattr_buf. If the attribute is a hard link
2967c478bd9Sstevel@tonic-gate  * to another attribute then another xattr_buf section is included
2977c478bd9Sstevel@tonic-gate  * for the link.
2987c478bd9Sstevel@tonic-gate  *
2997c478bd9Sstevel@tonic-gate  * The xattr_buf is used to define the necessary "pathing" steps
3007c478bd9Sstevel@tonic-gate  * to get to the extended attribute.  This is necessary to support
3017c478bd9Sstevel@tonic-gate  * a fully recursive attribute model where an attribute may itself
3027c478bd9Sstevel@tonic-gate  * have an attribute.
3037c478bd9Sstevel@tonic-gate  *
3047c478bd9Sstevel@tonic-gate  * The basic layout looks like this.
3057c478bd9Sstevel@tonic-gate  *
3067c478bd9Sstevel@tonic-gate  *     --------------------------------
3077c478bd9Sstevel@tonic-gate  *     |                              |
3087c478bd9Sstevel@tonic-gate  *     |         xattr_hdr            |
3097c478bd9Sstevel@tonic-gate  *     |                              |
3107c478bd9Sstevel@tonic-gate  *     --------------------------------
3117c478bd9Sstevel@tonic-gate  *     --------------------------------
3127c478bd9Sstevel@tonic-gate  *     |                              |
3137c478bd9Sstevel@tonic-gate  *     |        xattr_buf             |
3147c478bd9Sstevel@tonic-gate  *     |                              |
3157c478bd9Sstevel@tonic-gate  *     --------------------------------
3167c478bd9Sstevel@tonic-gate  *     --------------------------------
3177c478bd9Sstevel@tonic-gate  *     |                              |
3187c478bd9Sstevel@tonic-gate  *     |      (optional link info)    |
3197c478bd9Sstevel@tonic-gate  *     |                              |
3207c478bd9Sstevel@tonic-gate  *     --------------------------------
3217c478bd9Sstevel@tonic-gate  *     --------------------------------
3227c478bd9Sstevel@tonic-gate  *     |                              |
3237c478bd9Sstevel@tonic-gate  *     |      attribute itself        |
3247c478bd9Sstevel@tonic-gate  *     |      stored as normal tar    |
3257c478bd9Sstevel@tonic-gate  *     |      or cpio data with       |
3267c478bd9Sstevel@tonic-gate  *     |      special mode or         |
3277c478bd9Sstevel@tonic-gate  *     |      typeflag                |
3287c478bd9Sstevel@tonic-gate  *     |                              |
3297c478bd9Sstevel@tonic-gate  *     --------------------------------
3307c478bd9Sstevel@tonic-gate  *
3317c478bd9Sstevel@tonic-gate  */
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate /*
3347c478bd9Sstevel@tonic-gate  * xattrhead is a pointer to the xattr_hdr
3357c478bd9Sstevel@tonic-gate  *
3367c478bd9Sstevel@tonic-gate  * xattrp is a pointer to the xattr_buf structure
3377c478bd9Sstevel@tonic-gate  * which contains the "pathing" steps to get to attributes
3387c478bd9Sstevel@tonic-gate  *
3397c478bd9Sstevel@tonic-gate  * xattr_linkp is a pointer to another xattr_buf structure that is
3407c478bd9Sstevel@tonic-gate  * only used when an attribute is actually linked to another attribute
3417c478bd9Sstevel@tonic-gate  *
3427c478bd9Sstevel@tonic-gate  */
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate static struct xattr_hdr *xattrhead;
3457c478bd9Sstevel@tonic-gate static struct xattr_buf *xattrp;
3467c478bd9Sstevel@tonic-gate static struct xattr_buf *xattr_linkp;	/* pointer to link info, if any */
347da6c28aaSamw static char *xattrapath;		/* attribute name */
3487c478bd9Sstevel@tonic-gate static char *xattr_linkaname;		/* attribute attribute is linked to */
3497c478bd9Sstevel@tonic-gate static char Hiddendir;			/* are we processing hidden xattr dir */
3507c478bd9Sstevel@tonic-gate static char xattrbadhead;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate /* Was statically allocated tbuf[NBLOCK] */
3537c478bd9Sstevel@tonic-gate static
3547c478bd9Sstevel@tonic-gate union hblock {
3557c478bd9Sstevel@tonic-gate 	char dummy[TBLOCK];
3567c478bd9Sstevel@tonic-gate 	struct header {
3577c478bd9Sstevel@tonic-gate 		char name[NAMSIZ];	/* If non-null prefix, path is	*/
3587c478bd9Sstevel@tonic-gate 					/* <prefix>/<name>;  otherwise	*/
3597c478bd9Sstevel@tonic-gate 					/* <name>			*/
3607c478bd9Sstevel@tonic-gate 		char mode[8];
3617c478bd9Sstevel@tonic-gate 		char uid[8];
3627c478bd9Sstevel@tonic-gate 		char gid[8];
3637c478bd9Sstevel@tonic-gate 		char size[12];		/* size of this extent if file split */
3647c478bd9Sstevel@tonic-gate 		char mtime[12];
3657c478bd9Sstevel@tonic-gate 		char chksum[8];
3667c478bd9Sstevel@tonic-gate 		char typeflag;
3677c478bd9Sstevel@tonic-gate 		char linkname[NAMSIZ];
3687c478bd9Sstevel@tonic-gate 		char magic[6];
3697c478bd9Sstevel@tonic-gate 		char version[2];
3707c478bd9Sstevel@tonic-gate 		char uname[32];
3717c478bd9Sstevel@tonic-gate 		char gname[32];
3727c478bd9Sstevel@tonic-gate 		char devmajor[8];
3737c478bd9Sstevel@tonic-gate 		char devminor[8];
3747c478bd9Sstevel@tonic-gate 		char prefix[PRESIZ];	/* Together with "name", the path of */
3757c478bd9Sstevel@tonic-gate 					/* the file:  <prefix>/<name>	*/
3767c478bd9Sstevel@tonic-gate 		char extno;		/* extent #, null if not split */
3777c478bd9Sstevel@tonic-gate 		char extotal;		/* total extents */
3787c478bd9Sstevel@tonic-gate 		char efsize[10];	/* size of entire file */
3797c478bd9Sstevel@tonic-gate 	} dbuf;
3807c478bd9Sstevel@tonic-gate } dblock, *tbuf, xhdr_buf;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate static
3837c478bd9Sstevel@tonic-gate struct xtar_hdr {
3847c478bd9Sstevel@tonic-gate 	uid_t		x_uid,		/* Uid of file */
3857c478bd9Sstevel@tonic-gate 			x_gid;		/* Gid of file */
3867c478bd9Sstevel@tonic-gate 	major_t		x_devmajor;	/* Device major node */
3877c478bd9Sstevel@tonic-gate 	minor_t		x_devminor;	/* Device minor node */
3887c478bd9Sstevel@tonic-gate 	off_t		x_filesz;	/* Length of file */
3897c478bd9Sstevel@tonic-gate 	char		*x_uname,	/* Pointer to name of user */
3907c478bd9Sstevel@tonic-gate 			*x_gname,	/* Pointer to gid of user */
3917c478bd9Sstevel@tonic-gate 			*x_linkpath,	/* Path for a hard/symbolic link */
3927c478bd9Sstevel@tonic-gate 			*x_path;	/* Path of file */
3937c478bd9Sstevel@tonic-gate 	timestruc_t	x_mtime;	/* Seconds and nanoseconds */
3947c478bd9Sstevel@tonic-gate } Xtarhdr;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate static
3977c478bd9Sstevel@tonic-gate struct gen_hdr {
3987c478bd9Sstevel@tonic-gate 	ulong_t		g_mode;		/* Mode of file */
3997c478bd9Sstevel@tonic-gate 	uid_t		g_uid,		/* Uid of file */
4007c478bd9Sstevel@tonic-gate 			g_gid;		/* Gid of file */
4017c478bd9Sstevel@tonic-gate 	off_t		g_filesz;	/* Length of file */
4027c478bd9Sstevel@tonic-gate 	time_t		g_mtime;	/* Modification time */
4037c478bd9Sstevel@tonic-gate 	uint_t		g_cksum;	/* Checksum of file */
4047c478bd9Sstevel@tonic-gate 	ulong_t		g_devmajor,	/* File system of file */
4057c478bd9Sstevel@tonic-gate 			g_devminor;	/* Major/minor of special files */
4067c478bd9Sstevel@tonic-gate } Gen;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate static
4097c478bd9Sstevel@tonic-gate struct linkbuf {
4107c478bd9Sstevel@tonic-gate 	ino_t	inum;
4117c478bd9Sstevel@tonic-gate 	dev_t	devnum;
4127c478bd9Sstevel@tonic-gate 	int	count;
4137c478bd9Sstevel@tonic-gate 	char	pathname[MAXNAM+1];	/* added 1 for last NULL */
4147c478bd9Sstevel@tonic-gate 	char 	attrname[MAXNAM+1];
4157c478bd9Sstevel@tonic-gate 	struct	linkbuf *nextp;
4167c478bd9Sstevel@tonic-gate } *ihead;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate /* see comments before build_table() */
4197c478bd9Sstevel@tonic-gate #define	TABLE_SIZE 512
4205e2174acSceastha typedef struct	file_list	{
4217c478bd9Sstevel@tonic-gate 	char	*name;			/* Name of file to {in,ex}clude */
4227c478bd9Sstevel@tonic-gate 	struct	file_list	*next;	/* Linked list */
4235e2174acSceastha } file_list_t;
4245e2174acSceastha static	file_list_t	*exclude_tbl[TABLE_SIZE],
4257c478bd9Sstevel@tonic-gate 			*include_tbl[TABLE_SIZE];
4267c478bd9Sstevel@tonic-gate 
42745916cd2Sjpk static int	append_secattr(char **, int *, int, char *, char);
4287c478bd9Sstevel@tonic-gate static void	write_ancillary(union hblock *, char *, int, char);
4297c478bd9Sstevel@tonic-gate 
4305e2174acSceastha static void add_file_to_table(file_list_t *table[], char *str);
4317c478bd9Sstevel@tonic-gate static void assert_string(char *s, char *msg);
4327c478bd9Sstevel@tonic-gate static int istape(int fd, int type);
4337c478bd9Sstevel@tonic-gate static void backtape(void);
4345e2174acSceastha static void build_table(file_list_t *table[], char *file);
435da6c28aaSamw static int check_prefix(char **namep, char **dirp, char **compp);
4367c478bd9Sstevel@tonic-gate static void closevol(void);
4377c478bd9Sstevel@tonic-gate static void copy(void *dst, void *src);
4387c478bd9Sstevel@tonic-gate static int convtoreg(off_t);
4392c0f0499Slovely static void delete_target(int fd, char *comp, char *namep);
4407c478bd9Sstevel@tonic-gate static void doDirTimes(char *name, timestruc_t modTime);
4417c478bd9Sstevel@tonic-gate static void done(int n);
4427c478bd9Sstevel@tonic-gate static void dorep(char *argv[]);
4437c478bd9Sstevel@tonic-gate static void dotable(char *argv[]);
4447c478bd9Sstevel@tonic-gate static void doxtract(char *argv[]);
445b25dbf38SRich Burridge static int tar_chdir(const char *path);
4463a1dab68SRich Burridge static int is_directory(char *name);
4471a431409SRich Burridge static int has_dot_dot(char *name);
4481a431409SRich Burridge static int is_absolute(char *name);
4491a431409SRich Burridge static char *make_relative_name(char *name, char **stripped_prefix);
4507c478bd9Sstevel@tonic-gate static void fatal(char *format, ...);
4517c478bd9Sstevel@tonic-gate static void vperror(int exit_status, char *fmt, ...);
4527c478bd9Sstevel@tonic-gate static void flushtape(void);
4537c478bd9Sstevel@tonic-gate static void getdir(void);
4547c478bd9Sstevel@tonic-gate static void *getmem(size_t);
4557c478bd9Sstevel@tonic-gate static void longt(struct stat *st, char aclchar);
456123523f8Sas158974 static void load_info_from_xtarhdr(u_longlong_t flag, struct xtar_hdr *xhdrp);
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,
463da6c28aaSamw     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);
467123523f8Sas158974 static void setbytes_to_skip(struct stat *st, int err);
4687c478bd9Sstevel@tonic-gate static void splitfile(char *longname, int ifd, char *name,
4697c478bd9Sstevel@tonic-gate 	char *prefix, int filetype);
4707c478bd9Sstevel@tonic-gate static void tomodes(struct stat *sp);
4717c478bd9Sstevel@tonic-gate static void usage(void);
472da6c28aaSamw static int xblocks(int issysattr, off_t bytes, int ofile);
473da6c28aaSamw static int xsfile(int issysattr, int ofd);
4747c478bd9Sstevel@tonic-gate static void resugname(int dirfd, char *name, int symflag);
4757c478bd9Sstevel@tonic-gate static int bcheck(char *bstr);
4767c478bd9Sstevel@tonic-gate static int checkdir(char *name);
4777c478bd9Sstevel@tonic-gate static int checksum(union hblock *dblockp);
4787c478bd9Sstevel@tonic-gate #ifdef	EUC
4797c478bd9Sstevel@tonic-gate static int checksum_signed(union hblock *dblockp);
4807c478bd9Sstevel@tonic-gate #endif	/* EUC */
4817c478bd9Sstevel@tonic-gate static int checkupdate(char *arg);
4827c478bd9Sstevel@tonic-gate static int checkw(char c, char *name);
4837c478bd9Sstevel@tonic-gate static int cmp(char *b, char *s, int n);
4847c478bd9Sstevel@tonic-gate static int defset(char *arch);
4857c478bd9Sstevel@tonic-gate static int endtape(void);
4865e2174acSceastha static int is_in_table(file_list_t *table[], char *str);
4877c478bd9Sstevel@tonic-gate static int notsame(void);
4887c478bd9Sstevel@tonic-gate static int is_prefix(char *s1, char *s2);
4897c478bd9Sstevel@tonic-gate static int response(void);
4907c478bd9Sstevel@tonic-gate static int build_dblock(const char *, const char *, const char,
4917c478bd9Sstevel@tonic-gate 	const int filetype, const struct stat *, const dev_t, const char *);
4927c478bd9Sstevel@tonic-gate static unsigned int hash(char *str);
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate static blkcnt_t kcheck(char *kstr);
4957c478bd9Sstevel@tonic-gate static off_t bsrch(char *s, int n, off_t l, off_t h);
4967c478bd9Sstevel@tonic-gate static void onintr(int sig);
4977c478bd9Sstevel@tonic-gate static void onquit(int sig);
4987c478bd9Sstevel@tonic-gate static void onhup(int sig);
4997c478bd9Sstevel@tonic-gate static uid_t getuidbyname(char *);
5007c478bd9Sstevel@tonic-gate static gid_t getgidbyname(char *);
5017c478bd9Sstevel@tonic-gate static char *getname(gid_t);
5027c478bd9Sstevel@tonic-gate static char *getgroup(gid_t);
5037c478bd9Sstevel@tonic-gate static int checkf(char *name, int mode, int howmuch);
5047c478bd9Sstevel@tonic-gate static int writetbuf(char *buffer, int n);
505da6c28aaSamw static int wantit(char *argv[], char **namep, char **dirp, char **comp,
506da6c28aaSamw     attr_data_t **attrinfo);
50745916cd2Sjpk static void append_ext_attr(char *shortname, char **secinfo, int *len);
5087c478bd9Sstevel@tonic-gate static int get_xdata(void);
5097c478bd9Sstevel@tonic-gate static void gen_num(const char *keyword, const u_longlong_t number);
5107c478bd9Sstevel@tonic-gate static void gen_date(const char *keyword, const timestruc_t time_value);
5117c478bd9Sstevel@tonic-gate static void gen_string(const char *keyword, const char *value);
5127c478bd9Sstevel@tonic-gate static void get_xtime(char *value, timestruc_t *xtime);
5137c478bd9Sstevel@tonic-gate static int chk_path_build(char *name, char *longname, char *linkname,
5147c478bd9Sstevel@tonic-gate     char *prefix, char type, int filetype);
5157c478bd9Sstevel@tonic-gate static int gen_utf8_names(const char *filename);
5167c478bd9Sstevel@tonic-gate static int utf8_local(char *option, char **Xhdr_ptrptr, char *target,
5177c478bd9Sstevel@tonic-gate     const char *src, int max_val);
5187c478bd9Sstevel@tonic-gate static int local_utf8(char **Xhdr_ptrptr, char *target, const char *src,
5197c478bd9Sstevel@tonic-gate     iconv_t iconv_cd, int xhdrflg, int max_val);
5207c478bd9Sstevel@tonic-gate static int c_utf8(char *target, const char *source);
521da6c28aaSamw static int getstat(int dirfd, char *longname, char *shortname,
522da6c28aaSamw     char *attrparent);
523da6c28aaSamw static void xattrs_put(char *, char *, char *, char *);
5247c478bd9Sstevel@tonic-gate static void prepare_xattr(char **, char	*, char	*,
5257c478bd9Sstevel@tonic-gate     char, struct linkbuf *, int *);
526da6c28aaSamw static int put_link(char *name, char *longname, char *component,
527da6c28aaSamw     char *longattrname, char *prefix, int filetype, char typeflag);
5287c478bd9Sstevel@tonic-gate static int put_extra_attributes(char *longname, char *shortname,
529da6c28aaSamw     char *longattrname, char *prefix, int filetype, char typeflag);
530da6c28aaSamw static int put_xattr_hdr(char *longname, char *shortname, char *longattrname,
531da6c28aaSamw     char *prefix, int typeflag, int filetype, struct linkbuf *lp);
532da6c28aaSamw static int read_xattr_hdr(attr_data_t **attrinfo);
53345916cd2Sjpk 
53445916cd2Sjpk /* Trusted Extensions */
53545916cd2Sjpk #define	AUTO_ZONE	"/zone"
53645916cd2Sjpk 
53745916cd2Sjpk static void extract_attr(char **file_ptr, struct sec_attr *);
53845916cd2Sjpk static int check_ext_attr(char *filename);
53945916cd2Sjpk static void rebuild_comp_path(char *str, char **namep);
54045916cd2Sjpk static int rebuild_lk_comp_path(char *str, char **namep);
54145916cd2Sjpk 
5427c478bd9Sstevel@tonic-gate static void get_parent(char *path, char *dir);
5437c478bd9Sstevel@tonic-gate static char *get_component(char *path);
544da6c28aaSamw static int retry_open_attr(int pdirfd, int cwd, char *dirp, char *pattr,
545da6c28aaSamw     char *name, int oflag, mode_t mode);
5467c478bd9Sstevel@tonic-gate static char *skipslashes(char *string, char *start);
5477c478bd9Sstevel@tonic-gate static void chop_endslashes(char *path);
54836802407SRich Burridge static pid_t compress_file(void);
54936802407SRich Burridge static void compress_back(void);
55036802407SRich Burridge static void decompress_file(void);
55136802407SRich Burridge static pid_t uncompress_file(void);
55236802407SRich Burridge static void *compress_malloc(size_t);
553506ea5b8SMilan Jurik static void check_compression(void);
554506ea5b8SMilan Jurik static char *bz_suffix(void);
555506ea5b8SMilan Jurik static char *gz_suffix(void);
556506ea5b8SMilan Jurik static char *xz_suffix(void);
55736802407SRich Burridge static char *add_suffix();
55836802407SRich Burridge static void wait_pid(pid_t);
559aa7166bdSMilan Jurik static void verify_compress_opt(const char *t);
560aa7166bdSMilan Jurik static void detect_compress(void);
561fa9e4066Sahrens 
5627c478bd9Sstevel@tonic-gate static	struct stat stbuf;
5637c478bd9Sstevel@tonic-gate 
564da6c28aaSamw static	char	*myname;
565c92bf99eSJoshua M. Clulow static	char	*xtract_chdir = NULL;
5667c478bd9Sstevel@tonic-gate static	int	checkflag = 0;
5677c478bd9Sstevel@tonic-gate static	int	Xflag, Fflag, iflag, hflag, Bflag, Iflag;
568*c536b1f9SRobert Mustacchi static	int	rflag, xflag, vflag, tflag, mt, cflag, mflag, pflag;
5697c478bd9Sstevel@tonic-gate static	int	uflag;
570cc22b130SRich Burridge static	int	errflag;
5717c478bd9Sstevel@tonic-gate static	int	oflag;
572cc22b130SRich Burridge static	int	bflag, Aflag;
5737c478bd9Sstevel@tonic-gate static 	int	Pflag;			/* POSIX conformant archive */
5747c478bd9Sstevel@tonic-gate static	int	Eflag;			/* Allow files greater than 8GB */
5757c478bd9Sstevel@tonic-gate static	int	atflag;			/* traverse extended attributes */
576da6c28aaSamw static	int	saflag;			/* traverse extended sys attributes */
5777c478bd9Sstevel@tonic-gate static	int	Dflag;			/* Data change flag */
57836802407SRich Burridge static	int	jflag;			/* flag to use 'bzip2' */
57936802407SRich Burridge static	int	zflag;			/* flag to use 'gzip' */
58036802407SRich Burridge static	int	Zflag;			/* flag to use 'compress' */
581506ea5b8SMilan Jurik static	int	Jflag;			/* flag to use 'xz' */
582aa7166bdSMilan Jurik static	int	aflag;			/* flag to use autocompression */
58336802407SRich Burridge 
58445916cd2Sjpk /* Trusted Extensions */
58545916cd2Sjpk static	int	Tflag;			/* Trusted Extensions attr flags */
58645916cd2Sjpk static	int	dir_flag;		/* for attribute extract */
58745916cd2Sjpk static	int	mld_flag;		/* for attribute extract */
58845916cd2Sjpk static	char	*orig_namep;		/* original namep - unadorned */
58945916cd2Sjpk static	int	rpath_flag;		/* MLD real path is rebuilt */
59045916cd2Sjpk static	char	real_path[MAXPATHLEN];	/* MLD real path */
59145916cd2Sjpk static	int	lk_rpath_flag;		/* linked to real path is rebuilt */
59245916cd2Sjpk static	char	lk_real_path[MAXPATHLEN]; /* linked real path */
59345916cd2Sjpk static	bslabel_t	bs_label;	/* for attribute extract */
59445916cd2Sjpk static	bslabel_t	admin_low;
59545916cd2Sjpk static	bslabel_t	admin_high;
59645916cd2Sjpk static	int	ignored_aprivs = 0;
59745916cd2Sjpk static	int	ignored_fprivs = 0;
59845916cd2Sjpk static	int	ignored_fattrs = 0;
59945916cd2Sjpk 
6007c478bd9Sstevel@tonic-gate static	int	term, chksum, wflag,
6017c478bd9Sstevel@tonic-gate 		first = TRUE, defaults_used = FALSE, linkerrok;
6027c478bd9Sstevel@tonic-gate static	blkcnt_t	recno;
6037c478bd9Sstevel@tonic-gate static	int	freemem = 1;
6047c478bd9Sstevel@tonic-gate static	int	nblock = NBLOCK;
6057c478bd9Sstevel@tonic-gate static	int	Errflg = 0;
6067c478bd9Sstevel@tonic-gate static	int	exitflag = 0;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate static	dev_t	mt_dev;		/* device containing output file */
6097c478bd9Sstevel@tonic-gate static	ino_t	mt_ino;		/* inode number of output file */
6107c478bd9Sstevel@tonic-gate static	int	mt_devtype;	/* dev type of archive, from stat structure */
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate static	int update = 1;		/* for `open' call */
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate static	off_t	low;
6157c478bd9Sstevel@tonic-gate static	off_t	high;
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate static	FILE	*tfile;
6187c478bd9Sstevel@tonic-gate static	FILE	*vfile = stdout;
61931a2de35SRich Burridge static	char	*tmpdir;
62031a2de35SRich Burridge static	char	*tmp_suffix = "/tarXXXXXX";
62131a2de35SRich Burridge static	char	*tname;
6227c478bd9Sstevel@tonic-gate static	char	archive[] = "archive0=";
6237c478bd9Sstevel@tonic-gate static	char	*Xfile;
6247c478bd9Sstevel@tonic-gate static	char	*usefile;
62536802407SRich Burridge static	char	tfname[1024];
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate static	int	mulvol;		/* multi-volume option selected */
6287c478bd9Sstevel@tonic-gate static	blkcnt_t	blocklim; /* number of blocks to accept per volume */
6297c478bd9Sstevel@tonic-gate static	blkcnt_t	tapepos; /* current block number to be written */
6307c478bd9Sstevel@tonic-gate static	int	NotTape;	/* true if tape is a disk */
6317c478bd9Sstevel@tonic-gate static	int	dumping;	/* true if writing a tape or other archive */
6327c478bd9Sstevel@tonic-gate static	int	extno;		/* number of extent:  starts at 1 */
6337c478bd9Sstevel@tonic-gate static	int	extotal;	/* total extents in this file */
6347c478bd9Sstevel@tonic-gate static	off_t	extsize;	/* size of current extent during extraction */
6357c478bd9Sstevel@tonic-gate static	ushort_t	Oumask = 0;	/* old umask value */
6367c478bd9Sstevel@tonic-gate static 	int is_posix;	/* true if archive we're reading is POSIX-conformant */
6377c478bd9Sstevel@tonic-gate static	const	char	*magic_type = "ustar";
6387c478bd9Sstevel@tonic-gate static	size_t	xrec_size = 8 * PATH_MAX;	/* extended rec initial size */
6397c478bd9Sstevel@tonic-gate static	char	*xrec_ptr;
6407c478bd9Sstevel@tonic-gate static	off_t	xrec_offset = 0;
6417c478bd9Sstevel@tonic-gate static	int	Xhdrflag;
6427c478bd9Sstevel@tonic-gate static	int	charset_type = 0;
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate static	u_longlong_t	xhdr_flgs;	/* Bits set determine which items */
6457c478bd9Sstevel@tonic-gate 					/*   need to be in extended header. */
646*c536b1f9SRobert Mustacchi static	pid_t	comp_pid = 0;
647*c536b1f9SRobert Mustacchi 
6487c478bd9Sstevel@tonic-gate #define	_X_DEVMAJOR	0x1
6497c478bd9Sstevel@tonic-gate #define	_X_DEVMINOR	0x2
6507c478bd9Sstevel@tonic-gate #define	_X_GID		0x4
6517c478bd9Sstevel@tonic-gate #define	_X_GNAME	0x8
6527c478bd9Sstevel@tonic-gate #define	_X_LINKPATH	0x10
6537c478bd9Sstevel@tonic-gate #define	_X_PATH		0x20
6547c478bd9Sstevel@tonic-gate #define	_X_SIZE		0x40
6557c478bd9Sstevel@tonic-gate #define	_X_UID		0x80
6567c478bd9Sstevel@tonic-gate #define	_X_UNAME	0x100
6577c478bd9Sstevel@tonic-gate #define	_X_ATIME	0x200
6587c478bd9Sstevel@tonic-gate #define	_X_CTIME	0x400
6597c478bd9Sstevel@tonic-gate #define	_X_MTIME	0x800
660123523f8Sas158974 #define	_X_XHDR		0x1000	/* Bit flag that determines whether 'X' */
661123523f8Sas158974 				/* typeflag was followed by 'A' or non 'A' */
662123523f8Sas158974 				/* typeflag. */
6637c478bd9Sstevel@tonic-gate #define	_X_LAST		0x40000000
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate #define	PID_MAX_DIGITS		(10 * sizeof (pid_t) / 4)
6667c478bd9Sstevel@tonic-gate #define	TIME_MAX_DIGITS		(10 * sizeof (time_t) / 4)
6677c478bd9Sstevel@tonic-gate #define	LONG_MAX_DIGITS		(10 * sizeof (long) / 4)
6687c478bd9Sstevel@tonic-gate #define	ULONGLONG_MAX_DIGITS	(10 * sizeof (u_longlong_t) / 4)
6697c478bd9Sstevel@tonic-gate /*
6707c478bd9Sstevel@tonic-gate  * UTF_8 encoding requires more space than the current codeset equivalent.
6717c478bd9Sstevel@tonic-gate  * Currently a factor of 2-3 would suffice, but it is possible for a factor
6727c478bd9Sstevel@tonic-gate  * of 6 to be needed in the future, so for saftey, we use that here.
6737c478bd9Sstevel@tonic-gate  */
6747c478bd9Sstevel@tonic-gate #define	UTF_8_FACTOR	6
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate static	u_longlong_t	xhdr_count = 0;
6777c478bd9Sstevel@tonic-gate static char		xhdr_dirname[PRESIZ + 1];
6787c478bd9Sstevel@tonic-gate static char		pidchars[PID_MAX_DIGITS + 1];
6797c478bd9Sstevel@tonic-gate static char		*tchar = "";		/* null linkpath */
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate static	char	local_path[UTF_8_FACTOR * PATH_MAX + 1];
6827c478bd9Sstevel@tonic-gate static	char	local_linkpath[UTF_8_FACTOR * PATH_MAX + 1];
6837c478bd9Sstevel@tonic-gate static	char	local_gname[UTF_8_FACTOR * _POSIX_NAME_MAX + 1];
6847c478bd9Sstevel@tonic-gate static	char	local_uname[UTF_8_FACTOR * _POSIX_NAME_MAX + 1];
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate /*
6877c478bd9Sstevel@tonic-gate  * The following mechanism is provided to allow us to debug tar in complicated
6887c478bd9Sstevel@tonic-gate  * situations, like when it is part of a pipe.  The idea is that you compile
68936802407SRich Burridge  * with -DWAITAROUND defined, and then add the 'D' function modifier to the
69036802407SRich Burridge  * target tar invocation, eg. "tar cDf tarfile file".  If stderr is available,
6917c478bd9Sstevel@tonic-gate  * it will tell you to which pid to attach the debugger; otherwise, use ps to
6927c478bd9Sstevel@tonic-gate  * find it.  Attach to the process from the debugger, and, *PRESTO*, you are
6937c478bd9Sstevel@tonic-gate  * there!
6947c478bd9Sstevel@tonic-gate  *
6957c478bd9Sstevel@tonic-gate  * Simply assign "waitaround = 0" once you attach to the process, and then
6967c478bd9Sstevel@tonic-gate  * proceed from there as usual.
6977c478bd9Sstevel@tonic-gate  */
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
7007c478bd9Sstevel@tonic-gate int waitaround = 0;		/* wait for rendezvous with the debugger */
7017c478bd9Sstevel@tonic-gate #endif
7027c478bd9Sstevel@tonic-gate 
70336802407SRich Burridge #define	BZIP		"/usr/bin/bzip2"
70436802407SRich Burridge #define	GZIP		"/usr/bin/gzip"
70536802407SRich Burridge #define	COMPRESS	"/usr/bin/compress"
706506ea5b8SMilan Jurik #define	XZ		"/usr/bin/xz"
70736802407SRich Burridge #define	BZCAT		"/usr/bin/bzcat"
70836802407SRich Burridge #define	GZCAT		"/usr/bin/gzcat"
70936802407SRich Burridge #define	ZCAT		"/usr/bin/zcat"
710506ea5b8SMilan Jurik #define	XZCAT		"/usr/bin/xzcat"
711506ea5b8SMilan Jurik #define	GSUF		8	/* number of valid 'gzip' sufixes */
712506ea5b8SMilan Jurik #define	BSUF		4	/* number of valid 'bzip2' sufixes */
713506ea5b8SMilan Jurik #define	XSUF		1	/* number of valid 'xz' suffixes */
71436802407SRich Burridge 
71536802407SRich Burridge static	char		*compress_opt; 	/* compression type */
71636802407SRich Burridge 
71736802407SRich Burridge static	char		*gsuffix[] = {".gz", "-gz", ".z", "-z", "_z", ".Z",
71836802407SRich Burridge 			".tgz", ".taz"};
71936802407SRich Burridge static	char		*bsuffix[] = {".bz2", ".bz", ".tbz2", ".tbz"};
720506ea5b8SMilan Jurik static	char		*xsuffix[] = {".xz"};
72136802407SRich Burridge static	char		*suffix;
72236802407SRich Burridge 
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])7257c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
7267c478bd9Sstevel@tonic-gate {
7277c478bd9Sstevel@tonic-gate 	char		*cp;
7287c478bd9Sstevel@tonic-gate 	char		*tmpdirp;
7297c478bd9Sstevel@tonic-gate 	pid_t		thispid;
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
7327c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
7337c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
7347c478bd9Sstevel@tonic-gate #endif
7357c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
7367c478bd9Sstevel@tonic-gate 	if (argc < 2)
7377c478bd9Sstevel@tonic-gate 		usage();
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	tfile = NULL;
740da6c28aaSamw 	if ((myname = strdup(argv[0])) == NULL) {
741da6c28aaSamw 		(void) fprintf(stderr, gettext(
742da6c28aaSamw 		    "tar: cannot allocate program name\n"));
743da6c28aaSamw 		exit(1);
744da6c28aaSamw 	}
7457c478bd9Sstevel@tonic-gate 
7463d63ea05Sas145665 	if (init_yes() < 0) {
7473d63ea05Sas145665 		(void) fprintf(stderr, gettext(ERR_MSG_INIT_YES),
7483d63ea05Sas145665 		    strerror(errno));
7493d63ea05Sas145665 		exit(2);
7503d63ea05Sas145665 	}
7513d63ea05Sas145665 
7527c478bd9Sstevel@tonic-gate 	/*
7537c478bd9Sstevel@tonic-gate 	 *  For XPG4 compatibility, we must be able to accept the "--"
7547c478bd9Sstevel@tonic-gate 	 *  argument normally recognized by getopt; it is used to delimit
7557c478bd9Sstevel@tonic-gate 	 *  the end opt the options section, and so can only appear in
7567c478bd9Sstevel@tonic-gate 	 *  the position of the first argument.  We simply skip it.
7577c478bd9Sstevel@tonic-gate 	 */
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	if (strcmp(argv[1], "--") == 0) {
7607c478bd9Sstevel@tonic-gate 		argv++;
7617c478bd9Sstevel@tonic-gate 		argc--;
7627c478bd9Sstevel@tonic-gate 		if (argc < 3)
7637c478bd9Sstevel@tonic-gate 			usage();
7647c478bd9Sstevel@tonic-gate 	}
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	argv[argc] = NULL;
7677c478bd9Sstevel@tonic-gate 	argv++;
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	/*
7707c478bd9Sstevel@tonic-gate 	 * Set up default values.
7717c478bd9Sstevel@tonic-gate 	 * Search the operand string looking for the first digit or an 'f'.
7727c478bd9Sstevel@tonic-gate 	 * If you find a digit, use the 'archive#' entry in DEF_FILE.
7737c478bd9Sstevel@tonic-gate 	 * If 'f' is given, bypass looking in DEF_FILE altogether.
7747c478bd9Sstevel@tonic-gate 	 * If no digit or 'f' is given, still look in DEF_FILE but use '0'.
7757c478bd9Sstevel@tonic-gate 	 */
7767c478bd9Sstevel@tonic-gate 	if ((usefile = getenv("TAPE")) == (char *)NULL) {
7777c478bd9Sstevel@tonic-gate 		for (cp = *argv; *cp; ++cp)
7787c478bd9Sstevel@tonic-gate 			if (isdigit(*cp) || *cp == 'f')
7797c478bd9Sstevel@tonic-gate 				break;
7807c478bd9Sstevel@tonic-gate 		if (*cp != 'f') {
7817c478bd9Sstevel@tonic-gate 			archive[7] = (*cp)? *cp: '0';
7827c478bd9Sstevel@tonic-gate 			if (!(defaults_used = defset(archive))) {
7837c478bd9Sstevel@tonic-gate 				usefile = NULL;
7847c478bd9Sstevel@tonic-gate 				nblock = 1;
7857c478bd9Sstevel@tonic-gate 				blocklim = 0;
7867c478bd9Sstevel@tonic-gate 				NotTape = 0;
7877c478bd9Sstevel@tonic-gate 			}
7887c478bd9Sstevel@tonic-gate 		}
7897c478bd9Sstevel@tonic-gate 	}
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	for (cp = *argv++; *cp; cp++)
7927c478bd9Sstevel@tonic-gate 		switch (*cp) {
7937c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
79436802407SRich Burridge 		case 'D':
7957c478bd9Sstevel@tonic-gate 			/* rendezvous with the debugger */
7967c478bd9Sstevel@tonic-gate 			waitaround = 1;
7977c478bd9Sstevel@tonic-gate 			break;
7987c478bd9Sstevel@tonic-gate #endif
7997c478bd9Sstevel@tonic-gate 		case 'f':
8007c478bd9Sstevel@tonic-gate 			assert_string(*argv, gettext(
8017c478bd9Sstevel@tonic-gate 			    "tar: tarfile must be specified with 'f' "
8027c478bd9Sstevel@tonic-gate 			    "function modifier\n"));
8037c478bd9Sstevel@tonic-gate 			usefile = *argv++;
8047c478bd9Sstevel@tonic-gate 			break;
8057c478bd9Sstevel@tonic-gate 		case 'F':
8067c478bd9Sstevel@tonic-gate 			Fflag++;
8077c478bd9Sstevel@tonic-gate 			break;
8087c478bd9Sstevel@tonic-gate 		case 'c':
8097c478bd9Sstevel@tonic-gate 			cflag++;
8107c478bd9Sstevel@tonic-gate 			rflag++;
8117c478bd9Sstevel@tonic-gate 			update = 1;
8127c478bd9Sstevel@tonic-gate 			break;
8137c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
8147c478bd9Sstevel@tonic-gate 		case '@':
8157c478bd9Sstevel@tonic-gate 			atflag++;
8167c478bd9Sstevel@tonic-gate 			break;
817da6c28aaSamw #endif	/* O_XATTR */
818da6c28aaSamw #if defined(_PC_SATTR_ENABLED)
819da6c28aaSamw 		case '/':
820da6c28aaSamw 			saflag++;
821da6c28aaSamw 			break;
822da6c28aaSamw #endif	/* _PC_SATTR_ENABLED */
8237c478bd9Sstevel@tonic-gate 		case 'u':
8247c478bd9Sstevel@tonic-gate 			uflag++;	/* moved code after signals caught */
8257c478bd9Sstevel@tonic-gate 			rflag++;
8267c478bd9Sstevel@tonic-gate 			update = 2;
8277c478bd9Sstevel@tonic-gate 			break;
8287c478bd9Sstevel@tonic-gate 		case 'r':
8297c478bd9Sstevel@tonic-gate 			rflag++;
8307c478bd9Sstevel@tonic-gate 			update = 2;
8317c478bd9Sstevel@tonic-gate 			break;
8327c478bd9Sstevel@tonic-gate 		case 'v':
8337c478bd9Sstevel@tonic-gate 			vflag++;
8347c478bd9Sstevel@tonic-gate 			break;
8357c478bd9Sstevel@tonic-gate 		case 'w':
8367c478bd9Sstevel@tonic-gate 			wflag++;
8377c478bd9Sstevel@tonic-gate 			break;
8387c478bd9Sstevel@tonic-gate 		case 'x':
8397c478bd9Sstevel@tonic-gate 			xflag++;
8407c478bd9Sstevel@tonic-gate 			break;
8417c478bd9Sstevel@tonic-gate 		case 'X':
8427c478bd9Sstevel@tonic-gate 			assert_string(*argv, gettext(
8437c478bd9Sstevel@tonic-gate 			    "tar: exclude file must be specified with 'X' "
8447c478bd9Sstevel@tonic-gate 			    "function modifier\n"));
8457c478bd9Sstevel@tonic-gate 			Xflag = 1;
8467c478bd9Sstevel@tonic-gate 			Xfile = *argv++;
8477c478bd9Sstevel@tonic-gate 			build_table(exclude_tbl, Xfile);
8487c478bd9Sstevel@tonic-gate 			break;
8497c478bd9Sstevel@tonic-gate 		case 't':
8507c478bd9Sstevel@tonic-gate 			tflag++;
8517c478bd9Sstevel@tonic-gate 			break;
8527c478bd9Sstevel@tonic-gate 		case 'm':
8537c478bd9Sstevel@tonic-gate 			mflag++;
8547c478bd9Sstevel@tonic-gate 			break;
8557c478bd9Sstevel@tonic-gate 		case 'p':
8567c478bd9Sstevel@tonic-gate 			pflag++;
8577c478bd9Sstevel@tonic-gate 			break;
8587c478bd9Sstevel@tonic-gate 		case 'D':
8597c478bd9Sstevel@tonic-gate 			Dflag++;
8607c478bd9Sstevel@tonic-gate 			break;
8617c478bd9Sstevel@tonic-gate 		case '-':
8627c478bd9Sstevel@tonic-gate 			/* ignore this silently */
8637c478bd9Sstevel@tonic-gate 			break;
8647c478bd9Sstevel@tonic-gate 		case '0':	/* numeric entries used only for defaults */
8657c478bd9Sstevel@tonic-gate 		case '1':
8667c478bd9Sstevel@tonic-gate 		case '2':
8677c478bd9Sstevel@tonic-gate 		case '3':
8687c478bd9Sstevel@tonic-gate 		case '4':
8697c478bd9Sstevel@tonic-gate 		case '5':
8707c478bd9Sstevel@tonic-gate 		case '6':
8717c478bd9Sstevel@tonic-gate 		case '7':
8727c478bd9Sstevel@tonic-gate 			break;
8737c478bd9Sstevel@tonic-gate 		case 'b':
8747c478bd9Sstevel@tonic-gate 			assert_string(*argv, gettext(
8757c478bd9Sstevel@tonic-gate 			    "tar: blocking factor must be specified "
8767c478bd9Sstevel@tonic-gate 			    "with 'b' function modifier\n"));
8777c478bd9Sstevel@tonic-gate 			bflag++;
8787c478bd9Sstevel@tonic-gate 			nblock = bcheck(*argv++);
8797c478bd9Sstevel@tonic-gate 			break;
8807c478bd9Sstevel@tonic-gate 		case 'n':		/* not a magtape (instead of 'k') */
8817c478bd9Sstevel@tonic-gate 			NotTape++;	/* assume non-magtape */
8827c478bd9Sstevel@tonic-gate 			break;
8837c478bd9Sstevel@tonic-gate 		case 'l':
8847c478bd9Sstevel@tonic-gate 			linkerrok++;
8857c478bd9Sstevel@tonic-gate 			break;
8867c478bd9Sstevel@tonic-gate 		case 'e':
8877c478bd9Sstevel@tonic-gate 			errflag++;
8887c478bd9Sstevel@tonic-gate 		case 'o':
8897c478bd9Sstevel@tonic-gate 			oflag++;
8907c478bd9Sstevel@tonic-gate 			break;
8917c478bd9Sstevel@tonic-gate 		case 'h':
8927c478bd9Sstevel@tonic-gate 			hflag++;
8937c478bd9Sstevel@tonic-gate 			break;
8947c478bd9Sstevel@tonic-gate 		case 'i':
8957c478bd9Sstevel@tonic-gate 			iflag++;
8967c478bd9Sstevel@tonic-gate 			break;
8977c478bd9Sstevel@tonic-gate 		case 'B':
8987c478bd9Sstevel@tonic-gate 			Bflag++;
8997c478bd9Sstevel@tonic-gate 			break;
9007c478bd9Sstevel@tonic-gate 		case 'P':
9017c478bd9Sstevel@tonic-gate 			Pflag++;
9027c478bd9Sstevel@tonic-gate 			break;
9037c478bd9Sstevel@tonic-gate 		case 'E':
9047c478bd9Sstevel@tonic-gate 			Eflag++;
9057c478bd9Sstevel@tonic-gate 			Pflag++;	/* Only POSIX archive made */
9067c478bd9Sstevel@tonic-gate 			break;
90745916cd2Sjpk 		case 'T':
90845916cd2Sjpk 			Tflag++;	/* Handle Trusted Extensions attrs */
90945916cd2Sjpk 			pflag++;	/* also set flag for ACL */
91045916cd2Sjpk 			break;
91136802407SRich Burridge 		case 'j':		/* compession "bzip2" */
912506ea5b8SMilan Jurik 			jflag = 1;
91336802407SRich Burridge 			break;
91436802407SRich Burridge 		case 'z':		/* compression "gzip" */
915506ea5b8SMilan Jurik 			zflag = 1;
91636802407SRich Burridge 			break;
91736802407SRich Burridge 		case 'Z':		/* compression "compress" */
918506ea5b8SMilan Jurik 			Zflag = 1;
919506ea5b8SMilan Jurik 			break;
920506ea5b8SMilan Jurik 		case 'J':		/* compression "xz" */
921506ea5b8SMilan Jurik 			Jflag = 1;
92236802407SRich Burridge 			break;
923aa7166bdSMilan Jurik 		case 'a':
924aa7166bdSMilan Jurik 			aflag = 1;	/* autocompression */
925aa7166bdSMilan Jurik 			break;
9267c478bd9Sstevel@tonic-gate 		default:
9277c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
9287c478bd9Sstevel@tonic-gate 			"tar: %c: unknown function modifier\n"), *cp);
9297c478bd9Sstevel@tonic-gate 			usage();
9307c478bd9Sstevel@tonic-gate 		}
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 	if (!rflag && !xflag && !tflag)
9337c478bd9Sstevel@tonic-gate 		usage();
9347c478bd9Sstevel@tonic-gate 	if ((rflag && xflag) || (xflag && tflag) || (rflag && tflag)) {
9357c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
9367c478bd9Sstevel@tonic-gate 		"tar: specify only one of [ctxru].\n"));
9377c478bd9Sstevel@tonic-gate 		usage();
9387c478bd9Sstevel@tonic-gate 	}
93936802407SRich Burridge 	if (cflag) {
940aa7166bdSMilan Jurik 		if ((jflag + zflag + Zflag + Jflag + aflag) > 1) {
94136802407SRich Burridge 			(void) fprintf(stderr, gettext(
942aa7166bdSMilan Jurik 			    "tar: specify only one of [ajJzZ] to "
94336802407SRich Burridge 			    "create a compressed file.\n"));
94436802407SRich Burridge 			usage();
94536802407SRich Burridge 		}
94636802407SRich Burridge 	}
94745916cd2Sjpk 	/* Trusted Extensions attribute handling */
94845916cd2Sjpk 	if (Tflag && ((getzoneid() != GLOBAL_ZONEID) ||
94945916cd2Sjpk 	    !is_system_labeled())) {
95045916cd2Sjpk 		(void) fprintf(stderr, gettext(
95145916cd2Sjpk 		"tar: the 'T' option is only available with "
95245916cd2Sjpk 		    "Trusted Extensions\nand must be run from "
95345916cd2Sjpk 		    "the global zone.\n"));
95445916cd2Sjpk 		usage();
95545916cd2Sjpk 	}
956cc22b130SRich Burridge 	if (cflag && *argv == NULL)
9577c478bd9Sstevel@tonic-gate 		fatal(gettext("Missing filenames"));
9587c478bd9Sstevel@tonic-gate 	if (usefile == NULL)
9597c478bd9Sstevel@tonic-gate 		fatal(gettext("device argument required"));
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	/* alloc a buffer of the right size */
9627c478bd9Sstevel@tonic-gate 	if ((tbuf = (union hblock *)
9637c478bd9Sstevel@tonic-gate 	    calloc(sizeof (union hblock) * nblock, sizeof (char))) ==
9647c478bd9Sstevel@tonic-gate 	    (union hblock *)NULL) {
9657c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
9667c478bd9Sstevel@tonic-gate 		"tar: cannot allocate physio buffer\n"));
9677c478bd9Sstevel@tonic-gate 		exit(1);
9687c478bd9Sstevel@tonic-gate 	}
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	if ((xrec_ptr = malloc(xrec_size)) == NULL) {
9717c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
9727c478bd9Sstevel@tonic-gate 		    "tar: cannot allocate extended header buffer\n"));
9737c478bd9Sstevel@tonic-gate 		exit(1);
9747c478bd9Sstevel@tonic-gate 	}
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
9777c478bd9Sstevel@tonic-gate 	if (waitaround) {
9787c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Rendezvous with tar on pid"
9797c478bd9Sstevel@tonic-gate 		    " %d\n"), getpid());
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 		while (waitaround) {
9827c478bd9Sstevel@tonic-gate 			(void) sleep(10);
9837c478bd9Sstevel@tonic-gate 		}
9847c478bd9Sstevel@tonic-gate 	}
9857c478bd9Sstevel@tonic-gate #endif
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 	thispid = getpid();
9887c478bd9Sstevel@tonic-gate 	(void) sprintf(pidchars, "%ld", thispid);
9897c478bd9Sstevel@tonic-gate 	thispid = strlen(pidchars);
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	if ((tmpdirp = getenv("TMPDIR")) == (char *)NULL)
9927c478bd9Sstevel@tonic-gate 		(void) strcpy(xhdr_dirname, "/tmp");
9937c478bd9Sstevel@tonic-gate 	else {
9947c478bd9Sstevel@tonic-gate 		/*
9957c478bd9Sstevel@tonic-gate 		 * Make sure that dir is no longer than what can
9967c478bd9Sstevel@tonic-gate 		 * fit in the prefix part of the header.
9977c478bd9Sstevel@tonic-gate 		 */
9987c478bd9Sstevel@tonic-gate 		if (strlen(tmpdirp) > (size_t)(PRESIZ - thispid - 12)) {
9997c478bd9Sstevel@tonic-gate 			(void) strcpy(xhdr_dirname, "/tmp");
10007c478bd9Sstevel@tonic-gate 			if ((vflag > 0) && (Eflag > 0))
10017c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
10027c478bd9Sstevel@tonic-gate 				    "Ignoring TMPDIR\n"));
10037c478bd9Sstevel@tonic-gate 		} else
10047c478bd9Sstevel@tonic-gate 			(void) strcpy(xhdr_dirname, tmpdirp);
10057c478bd9Sstevel@tonic-gate 	}
10067c478bd9Sstevel@tonic-gate 	(void) strcat(xhdr_dirname, "/PaxHeaders.");
10077c478bd9Sstevel@tonic-gate 	(void) strcat(xhdr_dirname, pidchars);
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	if (rflag) {
101036802407SRich Burridge 		if (cflag && usefile != NULL)  {
101136802407SRich Burridge 			/* Set the compression type */
1012aa7166bdSMilan Jurik 			if (aflag)
1013aa7166bdSMilan Jurik 				detect_compress();
1014aa7166bdSMilan Jurik 
101536802407SRich Burridge 			if (jflag) {
101636802407SRich Burridge 				compress_opt = compress_malloc(strlen(BZIP)
101736802407SRich Burridge 				    + 1);
101836802407SRich Burridge 				(void) strcpy(compress_opt, BZIP);
101936802407SRich Burridge 			} else if (zflag) {
102036802407SRich Burridge 				compress_opt = compress_malloc(strlen(GZIP)
102136802407SRich Burridge 				    + 1);
102236802407SRich Burridge 				(void) strcpy(compress_opt, GZIP);
102336802407SRich Burridge 			} else if (Zflag) {
102436802407SRich Burridge 				compress_opt =
102536802407SRich Burridge 				    compress_malloc(strlen(COMPRESS) + 1);
102636802407SRich Burridge 				(void) strcpy(compress_opt, COMPRESS);
1027506ea5b8SMilan Jurik 			} else if (Jflag) {
1028506ea5b8SMilan Jurik 				compress_opt = compress_malloc(strlen(XZ) + 1);
1029506ea5b8SMilan Jurik 				(void) strcpy(compress_opt, XZ);
103036802407SRich Burridge 			}
103136802407SRich Burridge 		} else {
103236802407SRich Burridge 			/*
103336802407SRich Burridge 			 * Decompress if the file is compressed for
103436802407SRich Burridge 			 * an update or replace.
103536802407SRich Burridge 			 */
103636802407SRich Burridge 			if (strcmp(usefile, "-") != 0) {
103736802407SRich Burridge 				check_compression();
103836802407SRich Burridge 				if (compress_opt != NULL) {
103936802407SRich Burridge 					decompress_file();
104036802407SRich Burridge 				}
104136802407SRich Burridge 			}
104236802407SRich Burridge 		}
104336802407SRich Burridge 
10447c478bd9Sstevel@tonic-gate 		if (cflag && tfile != NULL)
10457c478bd9Sstevel@tonic-gate 			usage();
10467c478bd9Sstevel@tonic-gate 		if (signal(SIGINT, SIG_IGN) != SIG_IGN)
10477c478bd9Sstevel@tonic-gate 			(void) signal(SIGINT, onintr);
10487c478bd9Sstevel@tonic-gate 		if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
10497c478bd9Sstevel@tonic-gate 			(void) signal(SIGHUP, onhup);
10507c478bd9Sstevel@tonic-gate 		if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
10517c478bd9Sstevel@tonic-gate 			(void) signal(SIGQUIT, onquit);
10527c478bd9Sstevel@tonic-gate 		if (uflag) {
10537c478bd9Sstevel@tonic-gate 			int tnum;
105431a2de35SRich Burridge 			struct stat sbuf;
105531a2de35SRich Burridge 
105631a2de35SRich Burridge 			tmpdir = getenv("TMPDIR");
105731a2de35SRich Burridge 			/*
105831a2de35SRich Burridge 			 * If the name is invalid or this isn't a directory,
105931a2de35SRich Burridge 			 * or the directory is not writable, then reset to
106031a2de35SRich Burridge 			 * a default temporary directory.
106131a2de35SRich Burridge 			 */
106231a2de35SRich Burridge 			if (tmpdir == NULL || *tmpdir == '\0' ||
106331a2de35SRich Burridge 			    (strlen(tmpdir) + strlen(tmp_suffix)) > PATH_MAX) {
106431a2de35SRich Burridge 				tmpdir = "/tmp";
106531a2de35SRich Burridge 			} else if (stat(tmpdir, &sbuf) < 0 ||
106631a2de35SRich Burridge 			    (sbuf.st_mode & S_IFMT) != S_IFDIR ||
106731a2de35SRich Burridge 			    (sbuf.st_mode & S_IWRITE) == 0) {
106831a2de35SRich Burridge 				tmpdir = "/tmp";
106931a2de35SRich Burridge 			}
107031a2de35SRich Burridge 
107131a2de35SRich Burridge 			if ((tname = calloc(1, strlen(tmpdir) +
107231a2de35SRich Burridge 			    strlen(tmp_suffix) + 1)) == NULL) {
107331a2de35SRich Burridge 				vperror(1, gettext("tar: out of memory, "
107431a2de35SRich Burridge 				    "cannot create temporary file\n"));
107531a2de35SRich Burridge 			}
107631a2de35SRich Burridge 			(void) strcpy(tname, tmpdir);
107731a2de35SRich Burridge 			(void) strcat(tname, tmp_suffix);
107831a2de35SRich Burridge 
10797c478bd9Sstevel@tonic-gate 			if ((tnum = mkstemp(tname)) == -1)
10807c478bd9Sstevel@tonic-gate 				vperror(1, "%s", tname);
10817c478bd9Sstevel@tonic-gate 			if ((tfile = fdopen(tnum, "w")) == NULL)
10827c478bd9Sstevel@tonic-gate 				vperror(1, "%s", tname);
10837c478bd9Sstevel@tonic-gate 		}
10847c478bd9Sstevel@tonic-gate 		if (strcmp(usefile, "-") == 0) {
10857c478bd9Sstevel@tonic-gate 			if (cflag == 0)
10867c478bd9Sstevel@tonic-gate 				fatal(gettext(
10877c478bd9Sstevel@tonic-gate 				"can only create standard output archives."));
10887c478bd9Sstevel@tonic-gate 			vfile = stderr;
10897c478bd9Sstevel@tonic-gate 			mt = dup(1);
10907c478bd9Sstevel@tonic-gate 			++bflag;
10917c478bd9Sstevel@tonic-gate 		} else {
10927c478bd9Sstevel@tonic-gate 			if (cflag)
10937c478bd9Sstevel@tonic-gate 				mt = open(usefile,
10947c478bd9Sstevel@tonic-gate 				    O_RDWR|O_CREAT|O_TRUNC, 0666);
10957c478bd9Sstevel@tonic-gate 			else
10967c478bd9Sstevel@tonic-gate 				mt = open(usefile, O_RDWR);
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 			if (mt < 0) {
10997c478bd9Sstevel@tonic-gate 				if (cflag == 0 || (mt =  creat(usefile, 0666))
11007c478bd9Sstevel@tonic-gate 				    < 0)
11017c478bd9Sstevel@tonic-gate 				vperror(1, "%s", usefile);
11027c478bd9Sstevel@tonic-gate 			}
11037c478bd9Sstevel@tonic-gate 		}
11047c478bd9Sstevel@tonic-gate 		/* Get inode and device number of output file */
11057c478bd9Sstevel@tonic-gate 		(void) fstat(mt, &stbuf);
11067c478bd9Sstevel@tonic-gate 		mt_ino = stbuf.st_ino;
11077c478bd9Sstevel@tonic-gate 		mt_dev = stbuf.st_dev;
11087c478bd9Sstevel@tonic-gate 		mt_devtype = stbuf.st_mode & S_IFMT;
11097c478bd9Sstevel@tonic-gate 		NotTape = !istape(mt, mt_devtype);
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 		if (rflag && !cflag && (mt_devtype == S_IFIFO))
11127c478bd9Sstevel@tonic-gate 			fatal(gettext("cannot append to pipe or FIFO."));
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 		if (Aflag && vflag)
11157c478bd9Sstevel@tonic-gate 			(void) printf(
11167c478bd9Sstevel@tonic-gate 			gettext("Suppressing absolute pathnames\n"));
1117*c536b1f9SRobert Mustacchi 		if (cflag && compress_opt != NULL)
1118*c536b1f9SRobert Mustacchi 			comp_pid = compress_file();
11197c478bd9Sstevel@tonic-gate 		dorep(argv);
112036802407SRich Burridge 		if (rflag && !cflag && (compress_opt != NULL))
112136802407SRich Burridge 			compress_back();
11227c478bd9Sstevel@tonic-gate 	} else if (xflag || tflag) {
11237c478bd9Sstevel@tonic-gate 		/*
11247c478bd9Sstevel@tonic-gate 		 * for each argument, check to see if there is a "-I file" pair.
11257c478bd9Sstevel@tonic-gate 		 * if so, move the 3rd argument into "-I"'s place, build_table()
11267c478bd9Sstevel@tonic-gate 		 * using "file"'s name and increment argc one (the second
11277c478bd9Sstevel@tonic-gate 		 * increment appears in the for loop) which removes the two
11287c478bd9Sstevel@tonic-gate 		 * args "-I" and "file" from the argument vector.
11297c478bd9Sstevel@tonic-gate 		 */
11307c478bd9Sstevel@tonic-gate 		for (argc = 0; argv[argc]; argc++) {
11317c478bd9Sstevel@tonic-gate 			if (strcmp(argv[argc], "-I") == 0) {
11327c478bd9Sstevel@tonic-gate 				if (!argv[argc+1]) {
11337c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
11347c478bd9Sstevel@tonic-gate 					"tar: missing argument for -I flag\n"));
11357c478bd9Sstevel@tonic-gate 					done(2);
11367c478bd9Sstevel@tonic-gate 				} else {
11377c478bd9Sstevel@tonic-gate 					Iflag = 1;
11387c478bd9Sstevel@tonic-gate 					argv[argc] = argv[argc+2];
11397c478bd9Sstevel@tonic-gate 					build_table(include_tbl, argv[++argc]);
11407c478bd9Sstevel@tonic-gate 				}
1141c92bf99eSJoshua M. Clulow 			} else if (strcmp(argv[argc], "-C") == 0) {
1142c92bf99eSJoshua M. Clulow 				if (!argv[argc+1]) {
1143c92bf99eSJoshua M. Clulow 					(void) fprintf(stderr, gettext("tar: "
1144c92bf99eSJoshua M. Clulow 					    "missing argument for -C flag\n"));
1145c92bf99eSJoshua M. Clulow 					done(2);
1146c92bf99eSJoshua M. Clulow 				} else if (xtract_chdir != NULL) {
1147c92bf99eSJoshua M. Clulow 					(void) fprintf(stderr, gettext("tar: "
1148c92bf99eSJoshua M. Clulow 					    "extract should have only one -C "
1149c92bf99eSJoshua M. Clulow 					    "flag\n"));
1150c92bf99eSJoshua M. Clulow 					done(2);
1151c92bf99eSJoshua M. Clulow 				} else {
1152c92bf99eSJoshua M. Clulow 					argv[argc] = argv[argc+2];
1153c92bf99eSJoshua M. Clulow 					xtract_chdir = argv[++argc];
1154c92bf99eSJoshua M. Clulow 				}
11557c478bd9Sstevel@tonic-gate 			}
11567c478bd9Sstevel@tonic-gate 		}
11577c478bd9Sstevel@tonic-gate 		if (strcmp(usefile, "-") == 0) {
11587c478bd9Sstevel@tonic-gate 			mt = dup(0);
11597c478bd9Sstevel@tonic-gate 			++bflag;
11607c478bd9Sstevel@tonic-gate 			/* try to recover from short reads when reading stdin */
11617c478bd9Sstevel@tonic-gate 			++Bflag;
11627c478bd9Sstevel@tonic-gate 		} else if ((mt = open(usefile, 0)) < 0)
11637c478bd9Sstevel@tonic-gate 			vperror(1, "%s", usefile);
11647c478bd9Sstevel@tonic-gate 
116536802407SRich Burridge 		/* Decompress if the file is compressed */
116636802407SRich Burridge 
116736802407SRich Burridge 		if (strcmp(usefile, "-") != 0) {
116836802407SRich Burridge 			check_compression();
1169*c536b1f9SRobert Mustacchi 			if (compress_opt != NULL)
1170*c536b1f9SRobert Mustacchi 				comp_pid = uncompress_file();
117136802407SRich Burridge 		}
11727c478bd9Sstevel@tonic-gate 		if (xflag) {
1173c92bf99eSJoshua M. Clulow 			if (xtract_chdir != NULL) {
1174c92bf99eSJoshua M. Clulow 				if (tar_chdir(xtract_chdir) < 0) {
1175c92bf99eSJoshua M. Clulow 					vperror(1, gettext("can't change "
1176c92bf99eSJoshua M. Clulow 					    "directories to %s"), xtract_chdir);
1177c92bf99eSJoshua M. Clulow 				}
1178c92bf99eSJoshua M. Clulow 			}
11797c478bd9Sstevel@tonic-gate 			if (Aflag && vflag)
1180eace40a5Sceastha 				(void) printf(gettext(
1181eace40a5Sceastha 				    "Suppressing absolute pathnames.\n"));
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 			doxtract(argv);
11847c478bd9Sstevel@tonic-gate 		} else if (tflag)
11857c478bd9Sstevel@tonic-gate 			dotable(argv);
11867c478bd9Sstevel@tonic-gate 	}
11877c478bd9Sstevel@tonic-gate 	else
11887c478bd9Sstevel@tonic-gate 		usage();
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 	done(Errflg);
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	/* Not reached:  keep compiler quiet */
11937c478bd9Sstevel@tonic-gate 	return (1);
11947c478bd9Sstevel@tonic-gate }
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate static void
usage(void)11977c478bd9Sstevel@tonic-gate usage(void)
11987c478bd9Sstevel@tonic-gate {
11997c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
12007c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
1201da6c28aaSamw #if defined(_PC_SATTR_ENABLED)
1202cc22b130SRich Burridge 	    "Usage: tar {c|r|t|u|x}[BDeEFhilmnopPTvw@/[0-7]][bf][X...] "
1203da6c28aaSamw #else
1204cc22b130SRich Burridge 	    "Usage: tar {c|r|t|u|x}[BDeEFhilmnopPTvw@[0-7]][bf][X...] "
1205da6c28aaSamw #endif	/* _PC_SATTR_ENABLED */
12067c478bd9Sstevel@tonic-gate #else
1207cc22b130SRich Burridge 	    "Usage: tar {c|r|t|u|x}[BDeEFhilmnopPTvw[0-7]][bf][X...] "
1208da6c28aaSamw #endif	/* O_XATTR */
1209506ea5b8SMilan Jurik 	    "[j|J|z|Z] "
12107c478bd9Sstevel@tonic-gate 	    "[blocksize] [tarfile] [size] [exclude-file...] "
12117c478bd9Sstevel@tonic-gate 	    "{file | -I include-file | -C directory file}...\n"));
12127c478bd9Sstevel@tonic-gate 	done(1);
12137c478bd9Sstevel@tonic-gate }
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate /*
12167c478bd9Sstevel@tonic-gate  * dorep - do "replacements"
12177c478bd9Sstevel@tonic-gate  *
12187c478bd9Sstevel@tonic-gate  *	Dorep is responsible for creating ('c'),  appending ('r')
12197c478bd9Sstevel@tonic-gate  *	and updating ('u');
12207c478bd9Sstevel@tonic-gate  */
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate static void
dorep(char * argv[])12237c478bd9Sstevel@tonic-gate dorep(char *argv[])
12247c478bd9Sstevel@tonic-gate {
12257c478bd9Sstevel@tonic-gate 	char *cp, *cp2, *p;
12267c478bd9Sstevel@tonic-gate 	char wdir[PATH_MAX+2], tempdir[PATH_MAX+2], *parent;
12277c478bd9Sstevel@tonic-gate 	char file[PATH_MAX*2], origdir[PATH_MAX+1];
12287c478bd9Sstevel@tonic-gate 	FILE *fp = (FILE *)NULL;
12297c478bd9Sstevel@tonic-gate 	int archtype;
1230123523f8Sas158974 	int ret;
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 	if (!cflag) {
12347c478bd9Sstevel@tonic-gate 		xhdr_flgs = 0;
12357c478bd9Sstevel@tonic-gate 		getdir();			/* read header for next file */
12367c478bd9Sstevel@tonic-gate 		if (Xhdrflag > 0) {
12377c478bd9Sstevel@tonic-gate 			if (!Eflag)
12387c478bd9Sstevel@tonic-gate 				fatal(gettext("Archive contains extended"
12397c478bd9Sstevel@tonic-gate 				    " header.  -E flag required.\n"));
1240123523f8Sas158974 			ret = get_xdata();	/* Get extended header items */
12417c478bd9Sstevel@tonic-gate 						/*   and regular header */
12427c478bd9Sstevel@tonic-gate 		} else {
12437c478bd9Sstevel@tonic-gate 			if (Eflag)
12447c478bd9Sstevel@tonic-gate 				fatal(gettext("Archive contains no extended"
12457c478bd9Sstevel@tonic-gate 				    " header.  -E flag not allowed.\n"));
12467c478bd9Sstevel@tonic-gate 		}
12477c478bd9Sstevel@tonic-gate 		while (!endtape()) {		/* changed from a do while */
1248123523f8Sas158974 			setbytes_to_skip(&stbuf, ret);
12497c478bd9Sstevel@tonic-gate 			passtape();		/* skip the file data */
12507c478bd9Sstevel@tonic-gate 			if (term)
12517c478bd9Sstevel@tonic-gate 				done(Errflg);	/* received signal to stop */
12527c478bd9Sstevel@tonic-gate 			xhdr_flgs = 0;
12537c478bd9Sstevel@tonic-gate 			getdir();
12547c478bd9Sstevel@tonic-gate 			if (Xhdrflag > 0)
1255123523f8Sas158974 				ret = get_xdata();
1256123523f8Sas158974 		}
1257123523f8Sas158974 		if (ret == 0) {
1258123523f8Sas158974 			if ((dblock.dbuf.typeflag != 'A') &&
1259123523f8Sas158974 			    (xhdr_flgs != 0)) {
1260123523f8Sas158974 				load_info_from_xtarhdr(xhdr_flgs,
1261123523f8Sas158974 				    &Xtarhdr);
1262123523f8Sas158974 				xhdr_flgs |= _X_XHDR;
1263123523f8Sas158974 			}
12647c478bd9Sstevel@tonic-gate 		}
12657c478bd9Sstevel@tonic-gate 		backtape();			/* was called by endtape */
12667c478bd9Sstevel@tonic-gate 		if (tfile != NULL) {
126731a2de35SRich Burridge 			/*
126831a2de35SRich Burridge 			 * Buffer size is calculated to be the size of the
126931a2de35SRich Burridge 			 * tmpdir string, plus 6 times the size of the tname
127031a2de35SRich Burridge 			 * string, plus a value that is known to be greater
127131a2de35SRich Burridge 			 * than the command pipeline string.
127231a2de35SRich Burridge 			 */
127331a2de35SRich Burridge 			int buflen = strlen(tmpdir) + (6 * strlen(tname)) + 100;
127431a2de35SRich Burridge 			char *buf;
12757c478bd9Sstevel@tonic-gate 
127631a2de35SRich Burridge 			if ((buf = (char *)calloc(1, buflen)) == NULL) {
127731a2de35SRich Burridge 				vperror(1, gettext("tar: out of memory, "
127831a2de35SRich Burridge 				    "cannot create sort command file\n"));
127931a2de35SRich Burridge 			}
128031a2de35SRich Burridge 
128131a2de35SRich Burridge 			(void) snprintf(buf, buflen, "env 'TMPDIR=%s' "
128231a2de35SRich Burridge 			    "sort +0 -1 +1nr %s -o %s; awk '$1 "
12837c478bd9Sstevel@tonic-gate 			    "!= prev {print; prev=$1}' %s >%sX;mv %sX %s",
128431a2de35SRich Burridge 			    tmpdir, tname, tname, tname, tname, tname, tname);
12857c478bd9Sstevel@tonic-gate 			(void) fflush(tfile);
12867c478bd9Sstevel@tonic-gate 			(void) system(buf);
128731a2de35SRich Burridge 			free(buf);
12887c478bd9Sstevel@tonic-gate 			(void) freopen(tname, "r", tfile);
12897c478bd9Sstevel@tonic-gate 			(void) fstat(fileno(tfile), &stbuf);
12907c478bd9Sstevel@tonic-gate 			high = stbuf.st_size;
12917c478bd9Sstevel@tonic-gate 		}
12927c478bd9Sstevel@tonic-gate 	}
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 	dumping = 1;
12957c478bd9Sstevel@tonic-gate 	if (mulvol) {	/* SP-1 */
12967c478bd9Sstevel@tonic-gate 		if (nblock && (blocklim%nblock) != 0)
12977c478bd9Sstevel@tonic-gate 			fatal(gettext(
12987c478bd9Sstevel@tonic-gate 			"Volume size not a multiple of block size."));
12997c478bd9Sstevel@tonic-gate 		blocklim -= 2;			/* for trailer records */
13007c478bd9Sstevel@tonic-gate 		if (vflag)
13017c478bd9Sstevel@tonic-gate 			(void) fprintf(vfile, gettext("Volume ends at %"
13027c478bd9Sstevel@tonic-gate 			    FMT_blkcnt_t "K, blocking factor = %dK\n"),
13037c478bd9Sstevel@tonic-gate 			    K((blocklim - 1)), K(nblock));
13047c478bd9Sstevel@tonic-gate 	}
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	/*
13077c478bd9Sstevel@tonic-gate 	 * Save the original directory before it gets
13087c478bd9Sstevel@tonic-gate 	 * changed.
13097c478bd9Sstevel@tonic-gate 	 */
13107c478bd9Sstevel@tonic-gate 	if (getcwd(origdir, (PATH_MAX+1)) == NULL) {
13117c478bd9Sstevel@tonic-gate 		vperror(0, gettext("A parent directory cannot be read"));
13127c478bd9Sstevel@tonic-gate 		exit(1);
13137c478bd9Sstevel@tonic-gate 	}
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 	(void) strcpy(wdir, origdir);
13167c478bd9Sstevel@tonic-gate 
1317cc22b130SRich Burridge 	while ((*argv || fp) && !term) {
13187c478bd9Sstevel@tonic-gate 		if (fp || (strcmp(*argv, "-I") == 0)) {
13197c478bd9Sstevel@tonic-gate 			if (fp == NULL) {
13207c478bd9Sstevel@tonic-gate 				if (*++argv == NULL)
13217c478bd9Sstevel@tonic-gate 					fatal(gettext(
13227c478bd9Sstevel@tonic-gate 					    "missing file name for -I flag."));
13237c478bd9Sstevel@tonic-gate 				else if ((fp = fopen(*argv++, "r")) == NULL)
13247c478bd9Sstevel@tonic-gate 					vperror(0, "%s", argv[-1]);
13257c478bd9Sstevel@tonic-gate 				continue;
13267c478bd9Sstevel@tonic-gate 			} else if ((fgets(file, PATH_MAX-1, fp)) == NULL) {
13277c478bd9Sstevel@tonic-gate 				(void) fclose(fp);
13287c478bd9Sstevel@tonic-gate 				fp = NULL;
13297c478bd9Sstevel@tonic-gate 				continue;
13307c478bd9Sstevel@tonic-gate 			} else {
13317c478bd9Sstevel@tonic-gate 				cp = cp2 = file;
13327c478bd9Sstevel@tonic-gate 				if ((p = strchr(cp2, '\n')))
13337c478bd9Sstevel@tonic-gate 					*p = 0;
13347c478bd9Sstevel@tonic-gate 			}
13357c478bd9Sstevel@tonic-gate 		} else if ((strcmp(*argv, "-C") == 0) && argv[1]) {
1336b25dbf38SRich Burridge 			if (tar_chdir(*++argv) < 0)
13377c478bd9Sstevel@tonic-gate 				vperror(0, gettext(
13387c478bd9Sstevel@tonic-gate 				    "can't change directories to %s"), *argv);
13397c478bd9Sstevel@tonic-gate 			else
13407c478bd9Sstevel@tonic-gate 				(void) getcwd(wdir, (sizeof (wdir)));
13417c478bd9Sstevel@tonic-gate 			argv++;
13427c478bd9Sstevel@tonic-gate 			continue;
13437c478bd9Sstevel@tonic-gate 		} else
13447c478bd9Sstevel@tonic-gate 			cp = cp2 = strcpy(file, *argv++);
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 		/*
13477c478bd9Sstevel@tonic-gate 		 * point cp2 to the last '/' in file, but not
13487c478bd9Sstevel@tonic-gate 		 * to a trailing '/'
13497c478bd9Sstevel@tonic-gate 		 */
13507c478bd9Sstevel@tonic-gate 		for (; *cp; cp++) {
13517c478bd9Sstevel@tonic-gate 			if (*cp == '/') {
13527c478bd9Sstevel@tonic-gate 				while (*(cp+1) == '/') {
13537c478bd9Sstevel@tonic-gate 					++cp;
13547c478bd9Sstevel@tonic-gate 				}
13557c478bd9Sstevel@tonic-gate 				if (*(cp+1) != '\0') {
13567c478bd9Sstevel@tonic-gate 					/* not trailing slash */
13577c478bd9Sstevel@tonic-gate 					cp2 = cp;
13587c478bd9Sstevel@tonic-gate 				}
13597c478bd9Sstevel@tonic-gate 			}
13607c478bd9Sstevel@tonic-gate 		}
13617c478bd9Sstevel@tonic-gate 		if (cp2 != file) {
13627c478bd9Sstevel@tonic-gate 			*cp2 = '\0';
1363b25dbf38SRich Burridge 			if (tar_chdir(file) < 0) {
13647c478bd9Sstevel@tonic-gate 				vperror(0, gettext(
13657c478bd9Sstevel@tonic-gate 				    "can't change directories to %s"), file);
13667c478bd9Sstevel@tonic-gate 				continue;
13677c478bd9Sstevel@tonic-gate 			}
13687c478bd9Sstevel@tonic-gate 			*cp2 = '/';
13697c478bd9Sstevel@tonic-gate 			cp2++;
13707c478bd9Sstevel@tonic-gate 		}
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 		parent = getcwd(tempdir, (sizeof (tempdir)));
1373da6c28aaSamw 
1374da6c28aaSamw 		archtype = putfile(file, cp2, parent, NULL, NORMAL_FILE,
13757c478bd9Sstevel@tonic-gate 		    LEV0, SYMLINK_LEV0);
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
13787c478bd9Sstevel@tonic-gate 		if (!exitflag) {
1379da6c28aaSamw 			if ((atflag || saflag) &&
1380da6c28aaSamw 			    (archtype == PUT_NOTAS_LINK)) {
1381da6c28aaSamw 				xattrs_put(file, cp2, parent, NULL);
13827c478bd9Sstevel@tonic-gate 			}
13837c478bd9Sstevel@tonic-gate 		}
13847c478bd9Sstevel@tonic-gate #endif
13857c478bd9Sstevel@tonic-gate 
1386b25dbf38SRich Burridge 		if (tar_chdir(origdir) < 0)
13877c478bd9Sstevel@tonic-gate 			vperror(0, gettext("cannot change back?: %s"), origdir);
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 		if (exitflag) {
13907c478bd9Sstevel@tonic-gate 			/*
13917c478bd9Sstevel@tonic-gate 			 * If e function modifier has been specified
13927c478bd9Sstevel@tonic-gate 			 * write the files (that are listed before the
13937c478bd9Sstevel@tonic-gate 			 * file causing the error) to tape.  exitflag is
13947c478bd9Sstevel@tonic-gate 			 * used because only some of the error conditions
13957c478bd9Sstevel@tonic-gate 			 * in putfile() recognize the e function modifier.
13967c478bd9Sstevel@tonic-gate 			 */
13977c478bd9Sstevel@tonic-gate 			break;
13987c478bd9Sstevel@tonic-gate 		}
13997c478bd9Sstevel@tonic-gate 	}
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate 	putempty((blkcnt_t)2);
14027c478bd9Sstevel@tonic-gate 	flushtape();
14037c478bd9Sstevel@tonic-gate 	closevol();	/* SP-1 */
14047c478bd9Sstevel@tonic-gate 	if (linkerrok == 1)
14057c478bd9Sstevel@tonic-gate 		for (; ihead != NULL; ihead = ihead->nextp) {
14067c478bd9Sstevel@tonic-gate 			if (ihead->count == 0)
14077c478bd9Sstevel@tonic-gate 				continue;
14087c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
14097c478bd9Sstevel@tonic-gate 			"tar: missing links to %s\n"), ihead->pathname);
14107c478bd9Sstevel@tonic-gate 			if (errflag)
14117c478bd9Sstevel@tonic-gate 				done(1);
14127c478bd9Sstevel@tonic-gate 			else
14137c478bd9Sstevel@tonic-gate 				Errflg = 1;
14147c478bd9Sstevel@tonic-gate 		}
14157c478bd9Sstevel@tonic-gate }
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate /*
14197c478bd9Sstevel@tonic-gate  * endtape - check for tape at end
14207c478bd9Sstevel@tonic-gate  *
14217c478bd9Sstevel@tonic-gate  *	endtape checks the entry in dblock.dbuf to see if its the
14227c478bd9Sstevel@tonic-gate  *	special EOT entry.  Endtape is usually called after getdir().
14237c478bd9Sstevel@tonic-gate  *
14247c478bd9Sstevel@tonic-gate  *	endtape used to call backtape; it no longer does, he who
14257c478bd9Sstevel@tonic-gate  *	wants it backed up must call backtape himself
14267c478bd9Sstevel@tonic-gate  *	RETURNS:	0 if not EOT, tape position unaffected
14277c478bd9Sstevel@tonic-gate  *			1 if	 EOT, tape position unaffected
14287c478bd9Sstevel@tonic-gate  */
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate static int
endtape(void)14317c478bd9Sstevel@tonic-gate endtape(void)
14327c478bd9Sstevel@tonic-gate {
14337c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.name[0] == '\0') {	/* null header = EOT */
14347c478bd9Sstevel@tonic-gate 		return (1);
14357c478bd9Sstevel@tonic-gate 	} else
14367c478bd9Sstevel@tonic-gate 		return (0);
14377c478bd9Sstevel@tonic-gate }
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate /*
14407c478bd9Sstevel@tonic-gate  *	getdir - get directory entry from tar tape
14417c478bd9Sstevel@tonic-gate  *
14427c478bd9Sstevel@tonic-gate  *	getdir reads the next tarblock off the tape and cracks
14437c478bd9Sstevel@tonic-gate  *	it as a directory. The checksum must match properly.
14447c478bd9Sstevel@tonic-gate  *
14457c478bd9Sstevel@tonic-gate  *	If tfile is non-null getdir writes the file name and mod date
14467c478bd9Sstevel@tonic-gate  *	to tfile.
14477c478bd9Sstevel@tonic-gate  */
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate static void
getdir(void)14507c478bd9Sstevel@tonic-gate getdir(void)
14517c478bd9Sstevel@tonic-gate {
14527c478bd9Sstevel@tonic-gate 	struct stat *sp;
14537c478bd9Sstevel@tonic-gate #ifdef EUC
14547c478bd9Sstevel@tonic-gate 	static int warn_chksum_sign = 0;
14557c478bd9Sstevel@tonic-gate #endif /* EUC */
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate top:
14587c478bd9Sstevel@tonic-gate 	readtape((char *)&dblock);
14597c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.name[0] == '\0')
14607c478bd9Sstevel@tonic-gate 		return;
14617c478bd9Sstevel@tonic-gate 	sp = &stbuf;
14627c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.mode, "%8lo", &Gen.g_mode);
14637c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.uid, "%8lo", (ulong_t *)&Gen.g_uid);
14647c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.gid, "%8lo", (ulong_t *)&Gen.g_gid);
14657c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.size, "%12" FMT_off_t_o, &Gen.g_filesz);
14667c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.mtime, "%12lo", (ulong_t *)&Gen.g_mtime);
14677c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.chksum, "%8o", &Gen.g_cksum);
14687c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.devmajor, "%8lo", &Gen.g_devmajor);
14697c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.devminor, "%8lo", &Gen.g_devminor);
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 	is_posix = (strcmp(dblock.dbuf.magic, magic_type) == 0);
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 	sp->st_mode = Gen.g_mode;
14747c478bd9Sstevel@tonic-gate 	if (is_posix && (sp->st_mode & S_IFMT) == 0)
14757c478bd9Sstevel@tonic-gate 		switch (dblock.dbuf.typeflag) {
14767c478bd9Sstevel@tonic-gate 		case '0': case 0: case _XATTR_HDRTYPE:
14777c478bd9Sstevel@tonic-gate 			sp->st_mode |= S_IFREG;
14787c478bd9Sstevel@tonic-gate 			break;
14797c478bd9Sstevel@tonic-gate 		case '1':	/* hard link */
14807c478bd9Sstevel@tonic-gate 			break;
14817c478bd9Sstevel@tonic-gate 		case '2':
14827c478bd9Sstevel@tonic-gate 			sp->st_mode |= S_IFLNK;
14837c478bd9Sstevel@tonic-gate 			break;
14847c478bd9Sstevel@tonic-gate 		case '3':
14857c478bd9Sstevel@tonic-gate 			sp->st_mode |= S_IFCHR;
14867c478bd9Sstevel@tonic-gate 			break;
14877c478bd9Sstevel@tonic-gate 		case '4':
14887c478bd9Sstevel@tonic-gate 			sp->st_mode |= S_IFBLK;
14897c478bd9Sstevel@tonic-gate 			break;
14907c478bd9Sstevel@tonic-gate 		case '5':
14917c478bd9Sstevel@tonic-gate 			sp->st_mode |= S_IFDIR;
14927c478bd9Sstevel@tonic-gate 			break;
14937c478bd9Sstevel@tonic-gate 		case '6':
14947c478bd9Sstevel@tonic-gate 			sp->st_mode |= S_IFIFO;
14957c478bd9Sstevel@tonic-gate 			break;
14967c478bd9Sstevel@tonic-gate 		default:
14977c478bd9Sstevel@tonic-gate 			if (convtoreg(Gen.g_filesz))
14987c478bd9Sstevel@tonic-gate 				sp->st_mode |= S_IFREG;
14997c478bd9Sstevel@tonic-gate 			break;
15007c478bd9Sstevel@tonic-gate 		}
15017c478bd9Sstevel@tonic-gate 
1502e765faefSRich Burridge 	if ((dblock.dbuf.typeflag == 'X') || (dblock.dbuf.typeflag == 'L')) {
15037c478bd9Sstevel@tonic-gate 		Xhdrflag = 1;	/* Currently processing extended header */
1504e765faefSRich Burridge 	} else {
15057c478bd9Sstevel@tonic-gate 		Xhdrflag = 0;
1506e765faefSRich Burridge 	}
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 	sp->st_uid = Gen.g_uid;
15097c478bd9Sstevel@tonic-gate 	sp->st_gid = Gen.g_gid;
15107c478bd9Sstevel@tonic-gate 	sp->st_size = Gen.g_filesz;
15117c478bd9Sstevel@tonic-gate 	sp->st_mtime = Gen.g_mtime;
15127c478bd9Sstevel@tonic-gate 	chksum = Gen.g_cksum;
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.extno != '\0') {	/* split file? */
15157c478bd9Sstevel@tonic-gate 		extno = dblock.dbuf.extno;
15167c478bd9Sstevel@tonic-gate 		extsize = Gen.g_filesz;
15177c478bd9Sstevel@tonic-gate 		extotal = dblock.dbuf.extotal;
15187c478bd9Sstevel@tonic-gate 	} else {
15197c478bd9Sstevel@tonic-gate 		extno = 0;	/* tell others file not split */
15207c478bd9Sstevel@tonic-gate 		extsize = 0;
15217c478bd9Sstevel@tonic-gate 		extotal = 0;
15227c478bd9Sstevel@tonic-gate 	}
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate #ifdef	EUC
15257c478bd9Sstevel@tonic-gate 	if (chksum != checksum(&dblock)) {
15267c478bd9Sstevel@tonic-gate 		if (chksum != checksum_signed(&dblock)) {
15277c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
15287c478bd9Sstevel@tonic-gate 			    "tar: directory checksum error\n"));
152903d7b3c0SRich Burridge 			if (iflag) {
153003d7b3c0SRich Burridge 				Errflg = 2;
15317c478bd9Sstevel@tonic-gate 				goto top;
153203d7b3c0SRich Burridge 			}
15337c478bd9Sstevel@tonic-gate 			done(2);
15347c478bd9Sstevel@tonic-gate 		} else {
15357c478bd9Sstevel@tonic-gate 			if (! warn_chksum_sign) {
15367c478bd9Sstevel@tonic-gate 				warn_chksum_sign = 1;
15377c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
15387c478bd9Sstevel@tonic-gate 			"tar: warning: tar file made with signed checksum\n"));
15397c478bd9Sstevel@tonic-gate 			}
15407c478bd9Sstevel@tonic-gate 		}
15417c478bd9Sstevel@tonic-gate 	}
15427c478bd9Sstevel@tonic-gate #else
15437c478bd9Sstevel@tonic-gate 	if (chksum != checksum(&dblock)) {
15447c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
15457c478bd9Sstevel@tonic-gate 		"tar: directory checksum error\n"));
154603d7b3c0SRich Burridge 		if (iflag) {
154703d7b3c0SRich Burridge 			Errflg = 2;
15487c478bd9Sstevel@tonic-gate 			goto top;
154903d7b3c0SRich Burridge 		}
15507c478bd9Sstevel@tonic-gate 		done(2);
15517c478bd9Sstevel@tonic-gate 	}
15527c478bd9Sstevel@tonic-gate #endif	/* EUC */
15537c478bd9Sstevel@tonic-gate 	if (tfile != NULL && Xhdrflag == 0) {
15547c478bd9Sstevel@tonic-gate 		/*
15557c478bd9Sstevel@tonic-gate 		 * If an extended header is present, then time is available
15567c478bd9Sstevel@tonic-gate 		 * in nanoseconds in the extended header data, so set it.
15577c478bd9Sstevel@tonic-gate 		 * Otherwise, give an invalid value so that checkupdate will
15587c478bd9Sstevel@tonic-gate 		 * not test beyond seconds.
15597c478bd9Sstevel@tonic-gate 		 */
15607c478bd9Sstevel@tonic-gate 		if ((xhdr_flgs & _X_MTIME))
15617c478bd9Sstevel@tonic-gate 			sp->st_mtim.tv_nsec = Xtarhdr.x_mtime.tv_nsec;
15627c478bd9Sstevel@tonic-gate 		else
15637c478bd9Sstevel@tonic-gate 			sp->st_mtim.tv_nsec = -1;
15647c478bd9Sstevel@tonic-gate 
15657c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_PATH)
15667c478bd9Sstevel@tonic-gate 			(void) fprintf(tfile, "%s %10ld.%9.9ld\n",
15677c478bd9Sstevel@tonic-gate 			    Xtarhdr.x_path, sp->st_mtim.tv_sec,
15687c478bd9Sstevel@tonic-gate 			    sp->st_mtim.tv_nsec);
15697c478bd9Sstevel@tonic-gate 		else
15707c478bd9Sstevel@tonic-gate 			(void) fprintf(tfile, "%.*s %10ld.%9.9ld\n",
15717c478bd9Sstevel@tonic-gate 			    NAMSIZ, dblock.dbuf.name, sp->st_mtim.tv_sec,
15727c478bd9Sstevel@tonic-gate 			    sp->st_mtim.tv_nsec);
15737c478bd9Sstevel@tonic-gate 	}
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
15767c478bd9Sstevel@tonic-gate 	Hiddendir = 0;
15777c478bd9Sstevel@tonic-gate 	if (xattrp && dblock.dbuf.typeflag == _XATTR_HDRTYPE) {
15787c478bd9Sstevel@tonic-gate 		if (xattrbadhead) {
15797c478bd9Sstevel@tonic-gate 			free(xattrhead);
15807c478bd9Sstevel@tonic-gate 			xattrp = NULL;
15817c478bd9Sstevel@tonic-gate 			xattr_linkp = NULL;
15827c478bd9Sstevel@tonic-gate 			xattrhead = NULL;
15837c478bd9Sstevel@tonic-gate 		} else {
1584da6c28aaSamw 			char	*aname = basename(xattrapath);
1585da6c28aaSamw 			size_t	xindex  = aname - xattrapath;
1586da6c28aaSamw 
1587da6c28aaSamw 			if (xattrapath[xindex] == '.' &&
1588da6c28aaSamw 			    xattrapath[xindex + 1] == '\0' &&
15897c478bd9Sstevel@tonic-gate 			    xattrp->h_typeflag == '5') {
15907c478bd9Sstevel@tonic-gate 				Hiddendir = 1;
15917c478bd9Sstevel@tonic-gate 				sp->st_mode =
1592d2443e76Smarks 				    (S_IFDIR | (sp->st_mode & POSIXMODES));
15937c478bd9Sstevel@tonic-gate 			}
15947c478bd9Sstevel@tonic-gate 			dblock.dbuf.typeflag = xattrp->h_typeflag;
15957c478bd9Sstevel@tonic-gate 		}
15967c478bd9Sstevel@tonic-gate 	}
15977c478bd9Sstevel@tonic-gate #endif
15987c478bd9Sstevel@tonic-gate }
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate /*
16027c478bd9Sstevel@tonic-gate  *	passtape - skip over a file on the tape
16037c478bd9Sstevel@tonic-gate  *
16047c478bd9Sstevel@tonic-gate  *	passtape skips over the next data file on the tape.
16057c478bd9Sstevel@tonic-gate  *	The tape directory entry must be in dblock.dbuf. This
16067c478bd9Sstevel@tonic-gate  *	routine just eats the number of blocks computed from the
16077c478bd9Sstevel@tonic-gate  *	directory size entry; the tape must be (logically) positioned
16087c478bd9Sstevel@tonic-gate  *	right after thee directory info.
16097c478bd9Sstevel@tonic-gate  */
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate static void
passtape(void)16127c478bd9Sstevel@tonic-gate passtape(void)
16137c478bd9Sstevel@tonic-gate {
16147c478bd9Sstevel@tonic-gate 	blkcnt_t blocks;
16157c478bd9Sstevel@tonic-gate 	char buf[TBLOCK];
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 	/*
16187c478bd9Sstevel@tonic-gate 	 * Types link(1), sym-link(2), char special(3), blk special(4),
16197c478bd9Sstevel@tonic-gate 	 *  directory(5), and FIFO(6) do not have data blocks associated
16207c478bd9Sstevel@tonic-gate 	 *  with them so just skip reading the data block.
16217c478bd9Sstevel@tonic-gate 	 */
16227c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.typeflag == '1' || dblock.dbuf.typeflag == '2' ||
16237c478bd9Sstevel@tonic-gate 	    dblock.dbuf.typeflag == '3' || dblock.dbuf.typeflag == '4' ||
16247c478bd9Sstevel@tonic-gate 	    dblock.dbuf.typeflag == '5' || dblock.dbuf.typeflag == '6')
16257c478bd9Sstevel@tonic-gate 		return;
16267c478bd9Sstevel@tonic-gate 	blocks = TBLOCKS(stbuf.st_size);
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate 	/* if operating on disk, seek instead of reading */
16297c478bd9Sstevel@tonic-gate 	if (NotTape)
16307c478bd9Sstevel@tonic-gate 		seekdisk(blocks);
16317c478bd9Sstevel@tonic-gate 	else
16327c478bd9Sstevel@tonic-gate 		while (blocks-- > 0)
16337c478bd9Sstevel@tonic-gate 			readtape(buf);
16347c478bd9Sstevel@tonic-gate }
16357c478bd9Sstevel@tonic-gate 
1636da6c28aaSamw #if defined(O_XATTR)
1637da6c28aaSamw static int
is_sysattr(char * name)1638da6c28aaSamw is_sysattr(char *name)
1639da6c28aaSamw {
1640da6c28aaSamw 	return ((strcmp(name, VIEW_READONLY) == 0) ||
1641da6c28aaSamw 	    (strcmp(name, VIEW_READWRITE) == 0));
1642da6c28aaSamw }
1643da6c28aaSamw #endif
1644da6c28aaSamw 
1645da6c28aaSamw #if defined(O_XATTR)
1646da6c28aaSamw /*
1647da6c28aaSamw  * Verify the attribute, attrname, is an attribute we want to restore.
1648da6c28aaSamw  * Never restore read-only system attribute files.  Only restore read-write
1649da6c28aaSamw  * system attributes files when -/ was specified, and only traverse into
1650da6c28aaSamw  * the 2nd level attribute directory containing only system attributes if
1651da6c28aaSamw  * -@ was specified.  This keeps us from archiving
1652da6c28aaSamw  *	<attribute name>/<read-write system attribute file>
1653da6c28aaSamw  * when -/ was specified without -@.
1654da6c28aaSamw  *
1655da6c28aaSamw  * attrname	- attribute file name
1656da6c28aaSamw  * attrparent	- attribute's parent name within the base file's attribute
1657da6c28aaSamw  *		directory hierarchy
1658da6c28aaSamw  */
1659da6c28aaSamw static attr_status_t
verify_attr(char * attrname,char * attrparent,int arc_rwsysattr,int * rw_sysattr)1660da6c28aaSamw verify_attr(char *attrname, char *attrparent, int arc_rwsysattr,
1661da6c28aaSamw     int *rw_sysattr)
1662da6c28aaSamw {
1663da6c28aaSamw #if defined(_PC_SATTR_ENABLED)
1664da6c28aaSamw 	int	attr_supported;
1665da6c28aaSamw 
1666da6c28aaSamw 	/* Never restore read-only system attribute files */
1667da6c28aaSamw 	if ((attr_supported = sysattr_type(attrname)) == _RO_SATTR) {
1668da6c28aaSamw 		*rw_sysattr = 0;
1669da6c28aaSamw 		return (ATTR_SKIP);
1670da6c28aaSamw 	} else {
1671da6c28aaSamw 		*rw_sysattr = (attr_supported == _RW_SATTR);
1672da6c28aaSamw 	}
1673da6c28aaSamw #else
1674da6c28aaSamw 	/*
1675da6c28aaSamw 	 * Only need to check if this attribute is an extended system
1676da6c28aaSamw 	 * attribute.
1677da6c28aaSamw 	 */
1678da6c28aaSamw 	if (*rw_sysattr = is_sysattr(attrname)) {
1679da6c28aaSamw 		return (ATTR_SKIP);
1680da6c28aaSamw 	} else {
1681da6c28aaSamw 		return (ATTR_OK);
1682da6c28aaSamw 	}
1683da6c28aaSamw #endif	/* _PC_SATTR_ENABLED */
1684da6c28aaSamw 
1685da6c28aaSamw 	/*
1686da6c28aaSamw 	 * If the extended system attribute file is specified with the
1687da6c28aaSamw 	 * arc_rwsysattr flag, as being transient (default extended
1688da6c28aaSamw 	 * attributes), then don't archive it.
1689da6c28aaSamw 	 */
1690da6c28aaSamw 	if (*rw_sysattr && !arc_rwsysattr) {
1691da6c28aaSamw 		return (ATTR_SKIP);
1692da6c28aaSamw 	}
1693da6c28aaSamw 
1694da6c28aaSamw 	/*
1695da6c28aaSamw 	 * Only restore read-write system attribute files
1696da6c28aaSamw 	 * when -/ was specified.  Only restore extended
1697da6c28aaSamw 	 * attributes when -@ was specified.
1698da6c28aaSamw 	 */
1699da6c28aaSamw 	if (atflag) {
1700da6c28aaSamw 		if (!saflag) {
1701da6c28aaSamw 			/*
1702da6c28aaSamw 			 * Only archive/restore the hidden directory "." if
1703da6c28aaSamw 			 * we're processing the top level hidden attribute
1704da6c28aaSamw 			 * directory.  We don't want to process the
1705da6c28aaSamw 			 * hidden attribute directory of the attribute
1706da6c28aaSamw 			 * directory that contains only extended system
1707da6c28aaSamw 			 * attributes.
1708da6c28aaSamw 			 */
1709da6c28aaSamw 			if (*rw_sysattr || (Hiddendir &&
1710da6c28aaSamw 			    (attrparent != NULL))) {
1711da6c28aaSamw 				return (ATTR_SKIP);
1712da6c28aaSamw 			}
1713da6c28aaSamw 		}
1714da6c28aaSamw 	} else if (saflag) {
1715da6c28aaSamw 		/*
1716da6c28aaSamw 		 * Only archive/restore read-write extended system attribute
1717da6c28aaSamw 		 * files of the base file.
1718da6c28aaSamw 		 */
1719da6c28aaSamw 		if (!*rw_sysattr || (attrparent != NULL)) {
1720da6c28aaSamw 			return (ATTR_SKIP);
1721da6c28aaSamw 		}
1722da6c28aaSamw 	} else {
1723da6c28aaSamw 		return (ATTR_SKIP);
1724da6c28aaSamw 	}
1725da6c28aaSamw 
1726da6c28aaSamw 	return (ATTR_OK);
1727da6c28aaSamw }
1728da6c28aaSamw #endif
17297c478bd9Sstevel@tonic-gate 
17305e2174acSceastha static void
free_children(file_list_t * children)17315e2174acSceastha free_children(file_list_t *children)
17325e2174acSceastha {
17335e2174acSceastha 	file_list_t	*child = children;
17345e2174acSceastha 	file_list_t	*cptr;
17355e2174acSceastha 
17365e2174acSceastha 	while (child != NULL) {
17375e2174acSceastha 		cptr = child->next;
17385e2174acSceastha 		if (child->name != NULL) {
17395e2174acSceastha 			free(child->name);
17405e2174acSceastha 		}
17415e2174acSceastha 		child = cptr;
17425e2174acSceastha 	}
17435e2174acSceastha }
17445e2174acSceastha 
17457c478bd9Sstevel@tonic-gate static int
putfile(char * longname,char * shortname,char * parent,attr_data_t * attrinfo,int filetype,int lev,int symlink_lev)1746da6c28aaSamw putfile(char *longname, char *shortname, char *parent, attr_data_t *attrinfo,
17477c478bd9Sstevel@tonic-gate     int filetype, int lev, int symlink_lev)
17487c478bd9Sstevel@tonic-gate {
17497c478bd9Sstevel@tonic-gate 	int infile = -1;	/* deliberately invalid */
17507c478bd9Sstevel@tonic-gate 	blkcnt_t blocks;
17517c478bd9Sstevel@tonic-gate 	char buf[PATH_MAX + 2];	/* Add trailing slash and null */
17527c478bd9Sstevel@tonic-gate 	char *bigbuf;
17537c478bd9Sstevel@tonic-gate 	int	maxread;
17547c478bd9Sstevel@tonic-gate 	int	hint;		/* amount to write to get "in sync" */
17557c478bd9Sstevel@tonic-gate 	char filetmp[PATH_MAX + 1];
17567c478bd9Sstevel@tonic-gate 	char *cp;
17577c478bd9Sstevel@tonic-gate 	char *name;
1758da6c28aaSamw 	char *attrparent = NULL;
1759da6c28aaSamw 	char *longattrname = NULL;
17605e2174acSceastha 	file_list_t	*child = NULL;
17615e2174acSceastha 	file_list_t	*child_end = NULL;
17625e2174acSceastha 	file_list_t	*cptr;
17637c478bd9Sstevel@tonic-gate 	struct dirent *dp;
17647c478bd9Sstevel@tonic-gate 	DIR *dirp;
17657c478bd9Sstevel@tonic-gate 	int i;
17667c478bd9Sstevel@tonic-gate 	int split;
17677c478bd9Sstevel@tonic-gate 	int dirfd = -1;
17687c478bd9Sstevel@tonic-gate 	int rc = PUT_NOTAS_LINK;
17697c478bd9Sstevel@tonic-gate 	int archtype = 0;
1770da6c28aaSamw 	int rw_sysattr = 0;
17717c478bd9Sstevel@tonic-gate 	char newparent[PATH_MAX + MAXNAMLEN + 1];
17727c478bd9Sstevel@tonic-gate 	char *prefix = "";
17737c478bd9Sstevel@tonic-gate 	char *tmpbuf;
17747c478bd9Sstevel@tonic-gate 	char goodbuf[PRESIZ + 2];
17757c478bd9Sstevel@tonic-gate 	char junkbuf[MAXNAM+1];
17767c478bd9Sstevel@tonic-gate 	char *lastslash;
17777c478bd9Sstevel@tonic-gate 	int j;
17787c478bd9Sstevel@tonic-gate 	struct stat sbuf;
17797c478bd9Sstevel@tonic-gate 	int readlink_max;
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate 	(void) memset(goodbuf, '\0', sizeof (goodbuf));
17827c478bd9Sstevel@tonic-gate 	(void) memset(junkbuf, '\0', sizeof (junkbuf));
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate 	xhdr_flgs = 0;
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 	if (filetype == XATTR_FILE) {
1787da6c28aaSamw 		attrparent = attrinfo->attr_parent;
1788da6c28aaSamw 		longattrname = attrinfo->attr_path;
1789da6c28aaSamw 		dirfd = attrinfo->attr_parentfd;
1790ced83f9bSceastha 		rw_sysattr = attrinfo->attr_rw_sysattr;
17917c478bd9Sstevel@tonic-gate 	} else {
17927c478bd9Sstevel@tonic-gate 		dirfd = open(".", O_RDONLY);
17937c478bd9Sstevel@tonic-gate 	}
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 	if (dirfd == -1) {
17967c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
1797da6c28aaSamw 		    "tar: unable to open%sdirectory %s%s%s%s\n"),
17987c478bd9Sstevel@tonic-gate 		    (filetype == XATTR_FILE) ? gettext(" attribute ") : " ",
1799da6c28aaSamw 		    (attrparent == NULL) ? "" : gettext("of attribute "),
1800da6c28aaSamw 		    (attrparent == NULL) ? "" : attrparent,
1801da6c28aaSamw 		    (attrparent == NULL) ? "" : gettext(" of "),
18027c478bd9Sstevel@tonic-gate 		    (filetype == XATTR_FILE) ? longname : parent);
18037c478bd9Sstevel@tonic-gate 		goto out;
18047c478bd9Sstevel@tonic-gate 	}
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 	if (lev > MAXLEV) {
18077c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
18087c478bd9Sstevel@tonic-gate 		    gettext("tar: directory nesting too deep, %s not dumped\n"),
18097c478bd9Sstevel@tonic-gate 		    longname);
18107c478bd9Sstevel@tonic-gate 		goto out;
18117c478bd9Sstevel@tonic-gate 	}
18127c478bd9Sstevel@tonic-gate 
1813da6c28aaSamw 	if (getstat(dirfd, longname, shortname, attrparent))
18147c478bd9Sstevel@tonic-gate 		goto out;
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 	if (hflag) {
18177c478bd9Sstevel@tonic-gate 		/*
18187c478bd9Sstevel@tonic-gate 		 * Catch nesting where a file is a symlink to its directory.
18197c478bd9Sstevel@tonic-gate 		 */
18207c478bd9Sstevel@tonic-gate 		j = fstatat(dirfd, shortname, &sbuf, AT_SYMLINK_NOFOLLOW);
18217c478bd9Sstevel@tonic-gate 		if (S_ISLNK(sbuf.st_mode)) {
18227c478bd9Sstevel@tonic-gate 			if (symlink_lev++ >= MAXSYMLINKS) {
18237c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
18247c478bd9Sstevel@tonic-gate 				    "tar: %s: Number of symbolic links "
18257c478bd9Sstevel@tonic-gate 				    "encountered during path name traversal "
18267c478bd9Sstevel@tonic-gate 				    "exceeds MAXSYMLINKS\n"), longname);
18277c478bd9Sstevel@tonic-gate 				Errflg = 1;
18287c478bd9Sstevel@tonic-gate 				goto out;
18297c478bd9Sstevel@tonic-gate 			}
18307c478bd9Sstevel@tonic-gate 		}
18317c478bd9Sstevel@tonic-gate 	}
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 	/*
18347c478bd9Sstevel@tonic-gate 	 * Check if the input file is the same as the tar file we
18357c478bd9Sstevel@tonic-gate 	 * are creating
18367c478bd9Sstevel@tonic-gate 	 */
18377c478bd9Sstevel@tonic-gate 	if ((mt_ino == stbuf.st_ino) && (mt_dev == stbuf.st_dev)) {
18387c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
1839da6c28aaSamw 		    "tar: %s%s%s%s%s same as archive file\n"),
1840da6c28aaSamw 		    rw_sysattr ? gettext("system ") : "",
1841da6c28aaSamw 		    (longattrname == NULL) ? "" : gettext("attribute "),
1842da6c28aaSamw 		    (longattrname == NULL) ? "" : longattrname,
1843da6c28aaSamw 		    (longattrname == NULL) ? "" : gettext(" of "),
1844da6c28aaSamw 		    longname);
18457c478bd9Sstevel@tonic-gate 		Errflg = 1;
18467c478bd9Sstevel@tonic-gate 		goto out;
18477c478bd9Sstevel@tonic-gate 	}
18487c478bd9Sstevel@tonic-gate 	/*
18497c478bd9Sstevel@tonic-gate 	 * Check size limit - we can't archive files that
18507c478bd9Sstevel@tonic-gate 	 * exceed TAR_OFFSET_MAX bytes because of header
18517c478bd9Sstevel@tonic-gate 	 * limitations. Exclude file types that set
18527c478bd9Sstevel@tonic-gate 	 * st_size to zero below because they take no
18537c478bd9Sstevel@tonic-gate 	 * archive space to represent contents.
18547c478bd9Sstevel@tonic-gate 	 */
18557c478bd9Sstevel@tonic-gate 	if ((stbuf.st_size > (off_t)TAR_OFFSET_MAX) &&
18567c478bd9Sstevel@tonic-gate 	    !S_ISDIR(stbuf.st_mode) &&
18577c478bd9Sstevel@tonic-gate 	    !S_ISCHR(stbuf.st_mode) &&
18587c478bd9Sstevel@tonic-gate 	    !S_ISBLK(stbuf.st_mode) &&
18597c478bd9Sstevel@tonic-gate 	    (Eflag == 0)) {
18607c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
1861da6c28aaSamw 		    "tar: %s%s%s%s%s too large to archive.  "
1862da6c28aaSamw 		    "Use E function modifier.\n"),
1863da6c28aaSamw 		    rw_sysattr ? gettext("system ") : "",
1864da6c28aaSamw 		    (longattrname == NULL) ? "" : gettext("attribute "),
1865da6c28aaSamw 		    (longattrname == NULL) ? "" : longattrname,
1866da6c28aaSamw 		    (longattrname == NULL) ? "" : gettext(" of "),
1867da6c28aaSamw 		    longname);
18687c478bd9Sstevel@tonic-gate 		if (errflag)
18697c478bd9Sstevel@tonic-gate 			exitflag = 1;
18707c478bd9Sstevel@tonic-gate 		Errflg = 1;
18717c478bd9Sstevel@tonic-gate 		goto out;
18727c478bd9Sstevel@tonic-gate 	}
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 	if (tfile != NULL && checkupdate(longname) == 0) {
18757c478bd9Sstevel@tonic-gate 		goto out;
18767c478bd9Sstevel@tonic-gate 	}
18777c478bd9Sstevel@tonic-gate 	if (checkw('r', longname) == 0) {
18787c478bd9Sstevel@tonic-gate 		goto out;
18797c478bd9Sstevel@tonic-gate 	}
18807c478bd9Sstevel@tonic-gate 
18813a1dab68SRich Burridge 	if (Fflag &&
18823a1dab68SRich Burridge 	    checkf(longname, (stbuf.st_mode & S_IFMT) == S_IFDIR, Fflag) == 0)
18837c478bd9Sstevel@tonic-gate 		goto out;
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 	if (Xflag) {
18867c478bd9Sstevel@tonic-gate 		if (is_in_table(exclude_tbl, longname)) {
18877c478bd9Sstevel@tonic-gate 			if (vflag) {
18887c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext(
18897c478bd9Sstevel@tonic-gate 				    "a %s excluded\n"), longname);
18907c478bd9Sstevel@tonic-gate 			}
18917c478bd9Sstevel@tonic-gate 			goto out;
18927c478bd9Sstevel@tonic-gate 		}
18937c478bd9Sstevel@tonic-gate 	}
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate 	/*
18967c478bd9Sstevel@tonic-gate 	 * If the length of the fullname is greater than MAXNAM,
18977c478bd9Sstevel@tonic-gate 	 * print out a message and return (unless extended headers are used,
18987c478bd9Sstevel@tonic-gate 	 * in which case fullname is limited to PATH_MAX).
18997c478bd9Sstevel@tonic-gate 	 */
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate 	if ((((split = (int)strlen(longname)) > MAXNAM) && (Eflag == 0)) ||
19027c478bd9Sstevel@tonic-gate 	    (split > PATH_MAX)) {
19037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
19047c478bd9Sstevel@tonic-gate 		    "tar: %s: file name too long\n"), longname);
19057c478bd9Sstevel@tonic-gate 		if (errflag)
19067c478bd9Sstevel@tonic-gate 			exitflag = 1;
19077c478bd9Sstevel@tonic-gate 		Errflg = 1;
19087c478bd9Sstevel@tonic-gate 		goto out;
19097c478bd9Sstevel@tonic-gate 	}
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate 	/*
19127c478bd9Sstevel@tonic-gate 	 * We split the fullname into prefix and name components if any one
19137c478bd9Sstevel@tonic-gate 	 * of three conditions holds:
19147c478bd9Sstevel@tonic-gate 	 *	-- the length of the fullname exceeds NAMSIZ,
19157c478bd9Sstevel@tonic-gate 	 *	-- the length of the fullname equals NAMSIZ, and the shortname
19167c478bd9Sstevel@tonic-gate 	 *	   is less than NAMSIZ, (splitting in this case preserves
19177c478bd9Sstevel@tonic-gate 	 *	   compatibility with 5.6 and 5.5.1 tar), or
19187c478bd9Sstevel@tonic-gate 	 * 	-- the length of the fullname equals NAMSIZ, the file is a
19197c478bd9Sstevel@tonic-gate 	 *	   directory and we are not in POSIX-conformant mode (where
19207c478bd9Sstevel@tonic-gate 	 *	   trailing slashes are removed from directories).
19217c478bd9Sstevel@tonic-gate 	 */
19227c478bd9Sstevel@tonic-gate 	if ((split > NAMSIZ) ||
19237c478bd9Sstevel@tonic-gate 	    (split == NAMSIZ && strlen(shortname) < NAMSIZ) ||
19244bc0a2efScasper 	    (split == NAMSIZ && S_ISDIR(stbuf.st_mode) && !Pflag)) {
19257c478bd9Sstevel@tonic-gate 		/*
19267c478bd9Sstevel@tonic-gate 		 * Since path is limited to PRESIZ characters, look for the
19277c478bd9Sstevel@tonic-gate 		 * last slash within PRESIZ + 1 characters only.
19287c478bd9Sstevel@tonic-gate 		 */
19297c478bd9Sstevel@tonic-gate 		(void) strncpy(&goodbuf[0], longname, min(split, PRESIZ + 1));
19307c478bd9Sstevel@tonic-gate 		tmpbuf = goodbuf;
19317c478bd9Sstevel@tonic-gate 		lastslash = strrchr(tmpbuf, '/');
19327c478bd9Sstevel@tonic-gate 		if (lastslash == NULL) {
19337c478bd9Sstevel@tonic-gate 			i = split;		/* Length of name */
19347c478bd9Sstevel@tonic-gate 			j = 0;			/* Length of prefix */
19357c478bd9Sstevel@tonic-gate 			goodbuf[0] = '\0';
19367c478bd9Sstevel@tonic-gate 		} else {
19377c478bd9Sstevel@tonic-gate 			*lastslash = '\0';	/* Terminate the prefix */
19387c478bd9Sstevel@tonic-gate 			j = strlen(tmpbuf);
19397c478bd9Sstevel@tonic-gate 			i = split - j - 1;
19407c478bd9Sstevel@tonic-gate 		}
19417c478bd9Sstevel@tonic-gate 		/*
19427c478bd9Sstevel@tonic-gate 		 * If the filename is greater than NAMSIZ we can't
19437c478bd9Sstevel@tonic-gate 		 * archive the file unless we are using extended headers.
19447c478bd9Sstevel@tonic-gate 		 */
19454bc0a2efScasper 		if ((i > NAMSIZ) || (i == NAMSIZ && S_ISDIR(stbuf.st_mode) &&
19467c478bd9Sstevel@tonic-gate 		    !Pflag)) {
19477c478bd9Sstevel@tonic-gate 			/* Determine which (filename or path) is too long. */
19487c478bd9Sstevel@tonic-gate 			lastslash = strrchr(longname, '/');
19497c478bd9Sstevel@tonic-gate 			if (lastslash != NULL)
19507c478bd9Sstevel@tonic-gate 				i = strlen(lastslash + 1);
19517c478bd9Sstevel@tonic-gate 			if (Eflag > 0) {
19527c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_PATH;
19537c478bd9Sstevel@tonic-gate 				Xtarhdr.x_path = longname;
19547c478bd9Sstevel@tonic-gate 				if (i <= NAMSIZ)
19557c478bd9Sstevel@tonic-gate 					(void) strcpy(junkbuf, lastslash + 1);
19567c478bd9Sstevel@tonic-gate 				else
19577c478bd9Sstevel@tonic-gate 					(void) sprintf(junkbuf, "%llu",
19587c478bd9Sstevel@tonic-gate 					    xhdr_count + 1);
19597c478bd9Sstevel@tonic-gate 				if (split - i - 1 > PRESIZ)
19607c478bd9Sstevel@tonic-gate 					(void) strcpy(goodbuf, xhdr_dirname);
19617c478bd9Sstevel@tonic-gate 			} else {
19627c478bd9Sstevel@tonic-gate 				if ((i > NAMSIZ) || (i == NAMSIZ &&
19634bc0a2efScasper 				    S_ISDIR(stbuf.st_mode) && !Pflag))
19647c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
19657c478bd9Sstevel@tonic-gate 					    "tar: %s: filename is greater than "
19667c478bd9Sstevel@tonic-gate 					    "%d\n"), lastslash == NULL ?
19677c478bd9Sstevel@tonic-gate 					    longname : lastslash + 1, NAMSIZ);
19687c478bd9Sstevel@tonic-gate 				else
19697c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
19707c478bd9Sstevel@tonic-gate 					    "tar: %s: prefix is greater than %d"
19717c478bd9Sstevel@tonic-gate 					    "\n"), longname, PRESIZ);
19727c478bd9Sstevel@tonic-gate 				if (errflag)
19737c478bd9Sstevel@tonic-gate 					exitflag = 1;
19747c478bd9Sstevel@tonic-gate 				Errflg = 1;
19757c478bd9Sstevel@tonic-gate 				goto out;
19767c478bd9Sstevel@tonic-gate 			}
19777c478bd9Sstevel@tonic-gate 		} else
19787c478bd9Sstevel@tonic-gate 			(void) strncpy(&junkbuf[0], longname + j + 1,
19797c478bd9Sstevel@tonic-gate 			    strlen(longname + j + 1));
19807c478bd9Sstevel@tonic-gate 		name = junkbuf;
19817c478bd9Sstevel@tonic-gate 		prefix = goodbuf;
19827c478bd9Sstevel@tonic-gate 	} else {
19837c478bd9Sstevel@tonic-gate 		name = longname;
19847c478bd9Sstevel@tonic-gate 	}
19857c478bd9Sstevel@tonic-gate 	if (Aflag) {
19867c478bd9Sstevel@tonic-gate 		if ((prefix != NULL) && (*prefix != '\0'))
19877c478bd9Sstevel@tonic-gate 			while (*prefix == '/')
19887c478bd9Sstevel@tonic-gate 				++prefix;
19897c478bd9Sstevel@tonic-gate 		else
19907c478bd9Sstevel@tonic-gate 			while (*name == '/')
19917c478bd9Sstevel@tonic-gate 				++name;
19927c478bd9Sstevel@tonic-gate 	}
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate 	switch (stbuf.st_mode & S_IFMT) {
19957c478bd9Sstevel@tonic-gate 	case S_IFDIR:
19967c478bd9Sstevel@tonic-gate 		stbuf.st_size = (off_t)0;
19977c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(stbuf.st_size);
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate 		if (filetype != XATTR_FILE && Hiddendir == 0) {
20007c478bd9Sstevel@tonic-gate 			i = 0;
20017c478bd9Sstevel@tonic-gate 			cp = buf;
20027c478bd9Sstevel@tonic-gate 			while ((*cp++ = longname[i++]))
20037c478bd9Sstevel@tonic-gate 				;
20047c478bd9Sstevel@tonic-gate 			*--cp = '/';
20057c478bd9Sstevel@tonic-gate 			*++cp = 0;
20067c478bd9Sstevel@tonic-gate 		}
20077c478bd9Sstevel@tonic-gate 		if (!oflag) {
20087c478bd9Sstevel@tonic-gate 			tomodes(&stbuf);
20097c478bd9Sstevel@tonic-gate 			if (build_dblock(name, tchar, '5', filetype,
20107c478bd9Sstevel@tonic-gate 			    &stbuf, stbuf.st_dev, prefix) != 0) {
20117c478bd9Sstevel@tonic-gate 				goto out;
20127c478bd9Sstevel@tonic-gate 			}
20137c478bd9Sstevel@tonic-gate 			if (!Pflag) {
20147c478bd9Sstevel@tonic-gate 				/*
20157c478bd9Sstevel@tonic-gate 				 * Old archives require a slash at the end
20167c478bd9Sstevel@tonic-gate 				 * of a directory name.
20177c478bd9Sstevel@tonic-gate 				 *
20187c478bd9Sstevel@tonic-gate 				 * XXX
20197c478bd9Sstevel@tonic-gate 				 * If directory name is too long, will
20207c478bd9Sstevel@tonic-gate 				 * slash overfill field?
20217c478bd9Sstevel@tonic-gate 				 */
20227c478bd9Sstevel@tonic-gate 				if (strlen(name) > (unsigned)NAMSIZ-1) {
20237c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
20247c478bd9Sstevel@tonic-gate 					    "tar: %s: filename is greater "
20257c478bd9Sstevel@tonic-gate 					    "than %d\n"), name, NAMSIZ);
20267c478bd9Sstevel@tonic-gate 					if (errflag)
20277c478bd9Sstevel@tonic-gate 						exitflag = 1;
20287c478bd9Sstevel@tonic-gate 					Errflg = 1;
20297c478bd9Sstevel@tonic-gate 					goto out;
20307c478bd9Sstevel@tonic-gate 				} else {
20317c478bd9Sstevel@tonic-gate 					if (strlen(name) == (NAMSIZ - 1)) {
20327c478bd9Sstevel@tonic-gate 						(void) memcpy(dblock.dbuf.name,
20337c478bd9Sstevel@tonic-gate 						    name, NAMSIZ);
20347c478bd9Sstevel@tonic-gate 						dblock.dbuf.name[NAMSIZ-1]
20357c478bd9Sstevel@tonic-gate 						    = '/';
20367c478bd9Sstevel@tonic-gate 					} else
20377c478bd9Sstevel@tonic-gate 						(void) sprintf(dblock.dbuf.name,
20387c478bd9Sstevel@tonic-gate 						    "%s/", name);
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 					/*
20417c478bd9Sstevel@tonic-gate 					 * need to recalculate checksum
20427c478bd9Sstevel@tonic-gate 					 * because the name changed.
20437c478bd9Sstevel@tonic-gate 					 */
20447c478bd9Sstevel@tonic-gate 					(void) sprintf(dblock.dbuf.chksum,
20457c478bd9Sstevel@tonic-gate 					    "%07o", checksum(&dblock));
20467c478bd9Sstevel@tonic-gate 				}
20477c478bd9Sstevel@tonic-gate 			}
20487c478bd9Sstevel@tonic-gate 
2049da6c28aaSamw 			if (put_extra_attributes(longname, shortname,
2050da6c28aaSamw 			    longattrname, prefix, filetype, '5') != 0)
20517c478bd9Sstevel@tonic-gate 				goto out;
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
20547c478bd9Sstevel@tonic-gate 			/*
20557c478bd9Sstevel@tonic-gate 			 * Reset header typeflag when archiving directory, since
20567c478bd9Sstevel@tonic-gate 			 * build_dblock changed it on us.
20577c478bd9Sstevel@tonic-gate 			 */
20587c478bd9Sstevel@tonic-gate 			if (filetype == XATTR_FILE) {
20597c478bd9Sstevel@tonic-gate 				dblock.dbuf.typeflag = _XATTR_HDRTYPE;
20607c478bd9Sstevel@tonic-gate 			} else {
20617c478bd9Sstevel@tonic-gate 				dblock.dbuf.typeflag = '5';
20627c478bd9Sstevel@tonic-gate 			}
20637c478bd9Sstevel@tonic-gate #else
20647c478bd9Sstevel@tonic-gate 			dblock.dbuf.typeflag = '5';
20657c478bd9Sstevel@tonic-gate #endif
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 			(void) sprintf(dblock.dbuf.chksum, "%07o",
20687c478bd9Sstevel@tonic-gate 			    checksum(&dblock));
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 			(void) writetbuf((char *)&dblock, 1);
20717c478bd9Sstevel@tonic-gate 		}
20727c478bd9Sstevel@tonic-gate 		if (vflag) {
20737c478bd9Sstevel@tonic-gate #ifdef DEBUG
20747c478bd9Sstevel@tonic-gate 			if (NotTape)
20757c478bd9Sstevel@tonic-gate 				DEBUG("seek = %" FMT_blkcnt_t "K\t", K(tapepos),
20767c478bd9Sstevel@tonic-gate 				    0);
20777c478bd9Sstevel@tonic-gate #endif
20787c478bd9Sstevel@tonic-gate 			if (filetype == XATTR_FILE && Hiddendir) {
20798e4a71aeSRich Burridge 				(void) fprintf(vfile,
20808e4a71aeSRich Burridge 				    gettext("a %s attribute %s "),
2081da6c28aaSamw 				    longname, longattrname);
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate 			} else {
20847c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "a %s/ ", longname);
20857c478bd9Sstevel@tonic-gate 			}
20867c478bd9Sstevel@tonic-gate 			if (NotTape)
20877c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "%" FMT_blkcnt_t "K\n",
20887c478bd9Sstevel@tonic-gate 				    K(blocks));
20897c478bd9Sstevel@tonic-gate 			else
20907c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("%" FMT_blkcnt_t
20917c478bd9Sstevel@tonic-gate 				    " tape blocks\n"), blocks);
20927c478bd9Sstevel@tonic-gate 		}
20937c478bd9Sstevel@tonic-gate 
20947c478bd9Sstevel@tonic-gate 		/*
20957c478bd9Sstevel@tonic-gate 		 * If hidden dir then break now since xattrs_put() will do
20967c478bd9Sstevel@tonic-gate 		 * the iterating of the directory.
20977c478bd9Sstevel@tonic-gate 		 *
2098da6c28aaSamw 		 * At the moment, there can only be system attributes on
2099da6c28aaSamw 		 * attributes.  There can be no attributes on attributes or
2100da6c28aaSamw 		 * directories within the attributes hidden directory hierarchy.
21017c478bd9Sstevel@tonic-gate 		 */
21027c478bd9Sstevel@tonic-gate 		if (filetype == XATTR_FILE)
21037c478bd9Sstevel@tonic-gate 			break;
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 		if (*shortname != '/')
21067c478bd9Sstevel@tonic-gate 			(void) sprintf(newparent, "%s/%s", parent, shortname);
21077c478bd9Sstevel@tonic-gate 		else
21087c478bd9Sstevel@tonic-gate 			(void) sprintf(newparent, "%s", shortname);
21097c478bd9Sstevel@tonic-gate 
2110b25dbf38SRich Burridge 		if (tar_chdir(shortname) < 0) {
21117c478bd9Sstevel@tonic-gate 			vperror(0, "%s", newparent);
21127c478bd9Sstevel@tonic-gate 			goto out;
21137c478bd9Sstevel@tonic-gate 		}
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 		if ((dirp = opendir(".")) == NULL) {
21167c478bd9Sstevel@tonic-gate 			vperror(0, gettext(
21177c478bd9Sstevel@tonic-gate 			    "can't open directory %s"), longname);
2118b25dbf38SRich Burridge 			if (tar_chdir(parent) < 0)
21197c478bd9Sstevel@tonic-gate 				vperror(0, gettext("cannot change back?: %s"),
21207c478bd9Sstevel@tonic-gate 				    parent);
21217c478bd9Sstevel@tonic-gate 			goto out;
21227c478bd9Sstevel@tonic-gate 		}
21237c478bd9Sstevel@tonic-gate 
21245e2174acSceastha 		/*
21255e2174acSceastha 		 * Create a list of files (children) in this directory to avoid
21265e2174acSceastha 		 * having to perform telldir()/seekdir().
21275e2174acSceastha 		 */
21287c478bd9Sstevel@tonic-gate 		while ((dp = readdir(dirp)) != NULL && !term) {
21297c478bd9Sstevel@tonic-gate 			if ((strcmp(".", dp->d_name) == 0) ||
21307c478bd9Sstevel@tonic-gate 			    (strcmp("..", dp->d_name) == 0))
21317c478bd9Sstevel@tonic-gate 				continue;
21325e2174acSceastha 			if (((cptr = (file_list_t *)calloc(sizeof (char),
21335e2174acSceastha 			    sizeof (file_list_t))) == NULL) ||
21345e2174acSceastha 			    ((cptr->name = strdup(dp->d_name)) == NULL)) {
21355e2174acSceastha 				vperror(1, gettext(
21365e2174acSceastha 				    "Insufficient memory for directory "
21375e2174acSceastha 				    "list entry %s/%s\n"),
21385e2174acSceastha 				    newparent, dp->d_name);
21395e2174acSceastha 			}
21407c478bd9Sstevel@tonic-gate 
21415e2174acSceastha 			/* Add the file to the list */
21425e2174acSceastha 			if (child == NULL) {
21435e2174acSceastha 				child = cptr;
21445e2174acSceastha 			} else {
21455e2174acSceastha 				child_end->next = cptr;
21465e2174acSceastha 			}
21475e2174acSceastha 			child_end = cptr;
21485e2174acSceastha 		}
21495e2174acSceastha 		(void) closedir(dirp);
21505e2174acSceastha 
21515e2174acSceastha 		/*
21525e2174acSceastha 		 * Archive each of the files in the current directory.
21535e2174acSceastha 		 * If a file is a directory, putfile() is called
21545e2174acSceastha 		 * recursively to archive the file hierarchy of the
21555e2174acSceastha 		 * directory before archiving the next file in the
21565e2174acSceastha 		 * current directory.
21575e2174acSceastha 		 */
21585e2174acSceastha 		while ((child != NULL) && !term) {
21595e2174acSceastha 			(void) strcpy(cp, child->name);
2160da6c28aaSamw 			archtype = putfile(buf, cp, newparent, NULL,
21617c478bd9Sstevel@tonic-gate 			    NORMAL_FILE, lev + 1, symlink_lev);
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate 			if (!exitflag) {
2164da6c28aaSamw 				if ((atflag || saflag) &&
2165da6c28aaSamw 				    (archtype == PUT_NOTAS_LINK)) {
2166da6c28aaSamw 					xattrs_put(buf, cp, newparent, NULL);
21677c478bd9Sstevel@tonic-gate 				}
21687c478bd9Sstevel@tonic-gate 			}
21697c478bd9Sstevel@tonic-gate 			if (exitflag)
21707c478bd9Sstevel@tonic-gate 				break;
21717c478bd9Sstevel@tonic-gate 
21725e2174acSceastha 			/* Free each child as we are done processing it. */
21735e2174acSceastha 			cptr = child;
21745e2174acSceastha 			child = child->next;
21755e2174acSceastha 			free(cptr->name);
21765e2174acSceastha 			free(cptr);
21777c478bd9Sstevel@tonic-gate 		}
21785e2174acSceastha 		if ((child != NULL) && !term) {
21795e2174acSceastha 			free_children(child);
21807c478bd9Sstevel@tonic-gate 		}
21817c478bd9Sstevel@tonic-gate 
2182b25dbf38SRich Burridge 		if (tar_chdir(parent) < 0) {
21837c478bd9Sstevel@tonic-gate 			vperror(0, gettext("cannot change back?: %s"), parent);
21847c478bd9Sstevel@tonic-gate 		}
21857c478bd9Sstevel@tonic-gate 
21867c478bd9Sstevel@tonic-gate 		break;
21877c478bd9Sstevel@tonic-gate 
21887c478bd9Sstevel@tonic-gate 	case S_IFLNK:
21897c478bd9Sstevel@tonic-gate 		readlink_max = NAMSIZ;
21907c478bd9Sstevel@tonic-gate 		if (stbuf.st_size > NAMSIZ) {
21917c478bd9Sstevel@tonic-gate 			if (Eflag > 0) {
21927c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_LINKPATH;
21937c478bd9Sstevel@tonic-gate 				readlink_max = PATH_MAX;
21947c478bd9Sstevel@tonic-gate 			} else {
21957c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
21967c478bd9Sstevel@tonic-gate 				    "tar: %s: symbolic link too long\n"),
21977c478bd9Sstevel@tonic-gate 				    longname);
21987c478bd9Sstevel@tonic-gate 				if (errflag)
21997c478bd9Sstevel@tonic-gate 					exitflag = 1;
22007c478bd9Sstevel@tonic-gate 				Errflg = 1;
22017c478bd9Sstevel@tonic-gate 				goto out;
22027c478bd9Sstevel@tonic-gate 			}
22037c478bd9Sstevel@tonic-gate 		}
22047c478bd9Sstevel@tonic-gate 		/*
22057c478bd9Sstevel@tonic-gate 		 * Sym-links need header size of zero since you
22067c478bd9Sstevel@tonic-gate 		 * don't store any data for this type.
22077c478bd9Sstevel@tonic-gate 		 */
22087c478bd9Sstevel@tonic-gate 		stbuf.st_size = (off_t)0;
22097c478bd9Sstevel@tonic-gate 		tomodes(&stbuf);
22107c478bd9Sstevel@tonic-gate 		i = readlink(shortname, filetmp, readlink_max);
22117c478bd9Sstevel@tonic-gate 		if (i < 0) {
22127c478bd9Sstevel@tonic-gate 			vperror(0, gettext(
22137c478bd9Sstevel@tonic-gate 			    "can't read symbolic link %s"), longname);
22147c478bd9Sstevel@tonic-gate 			goto out;
22157c478bd9Sstevel@tonic-gate 		} else {
22167c478bd9Sstevel@tonic-gate 			filetmp[i] = 0;
22177c478bd9Sstevel@tonic-gate 		}
22187c478bd9Sstevel@tonic-gate 		if (vflag)
22197c478bd9Sstevel@tonic-gate 			(void) fprintf(vfile, gettext(
22207c478bd9Sstevel@tonic-gate 			    "a %s symbolic link to %s\n"),
22217c478bd9Sstevel@tonic-gate 			    longname, filetmp);
22227c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_LINKPATH) {
22237c478bd9Sstevel@tonic-gate 			Xtarhdr.x_linkpath = filetmp;
22247c478bd9Sstevel@tonic-gate 			if (build_dblock(name, tchar, '2', filetype, &stbuf,
22257c478bd9Sstevel@tonic-gate 			    stbuf.st_dev, prefix) != 0)
22267c478bd9Sstevel@tonic-gate 				goto out;
22277c478bd9Sstevel@tonic-gate 		} else
22287c478bd9Sstevel@tonic-gate 			if (build_dblock(name, filetmp, '2', filetype, &stbuf,
22297c478bd9Sstevel@tonic-gate 			    stbuf.st_dev, prefix) != 0)
22307c478bd9Sstevel@tonic-gate 				goto out;
22317c478bd9Sstevel@tonic-gate 		(void) writetbuf((char *)&dblock, 1);
22327c478bd9Sstevel@tonic-gate 		/*
22337c478bd9Sstevel@tonic-gate 		 * No acls for symlinks: mode is always 777
22347c478bd9Sstevel@tonic-gate 		 * dont call write ancillary
22357c478bd9Sstevel@tonic-gate 		 */
22367c478bd9Sstevel@tonic-gate 		rc = PUT_AS_LINK;
22377c478bd9Sstevel@tonic-gate 		break;
22387c478bd9Sstevel@tonic-gate 	case S_IFREG:
22397c478bd9Sstevel@tonic-gate 		if ((infile = openat(dirfd, shortname, 0)) < 0) {
22408e4a71aeSRich Burridge 			vperror(0, gettext("unable to open %s%s%s%s"), longname,
2241da6c28aaSamw 			    rw_sysattr ? gettext(" system") : "",
22427c478bd9Sstevel@tonic-gate 			    (filetype == XATTR_FILE) ?
22437c478bd9Sstevel@tonic-gate 			    gettext(" attribute ") : "",
2244da6c28aaSamw 			    (filetype == XATTR_FILE) ? (longattrname == NULL) ?
2245da6c28aaSamw 			    shortname : longattrname : "");
22467c478bd9Sstevel@tonic-gate 			goto out;
22477c478bd9Sstevel@tonic-gate 		}
22487c478bd9Sstevel@tonic-gate 
22497c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(stbuf.st_size);
22507c478bd9Sstevel@tonic-gate 
2251da6c28aaSamw 		if (put_link(name, longname, shortname, longattrname,
22527c478bd9Sstevel@tonic-gate 		    prefix, filetype, '1') == 0) {
22537c478bd9Sstevel@tonic-gate 			(void) close(infile);
22547c478bd9Sstevel@tonic-gate 			rc = PUT_AS_LINK;
22557c478bd9Sstevel@tonic-gate 			goto out;
22567c478bd9Sstevel@tonic-gate 		}
22577c478bd9Sstevel@tonic-gate 
22587c478bd9Sstevel@tonic-gate 		tomodes(&stbuf);
22597c478bd9Sstevel@tonic-gate 
22607c478bd9Sstevel@tonic-gate 		/* correctly handle end of volume */
22617c478bd9Sstevel@tonic-gate 		while (mulvol && tapepos + blocks + 1 > blocklim) {
22627c478bd9Sstevel@tonic-gate 			/* split if floppy has some room and file is large */
22637c478bd9Sstevel@tonic-gate 			if (((blocklim - tapepos) >= EXTMIN) &&
22647c478bd9Sstevel@tonic-gate 			    ((blocks + 1) >= blocklim/10)) {
22657c478bd9Sstevel@tonic-gate 				splitfile(longname, infile,
22667c478bd9Sstevel@tonic-gate 				    name, prefix, filetype);
22677c478bd9Sstevel@tonic-gate 				(void) close(dirfd);
22687c478bd9Sstevel@tonic-gate 				(void) close(infile);
22697c478bd9Sstevel@tonic-gate 				goto out;
22707c478bd9Sstevel@tonic-gate 			}
22717c478bd9Sstevel@tonic-gate 			newvol();	/* not worth it--just get new volume */
22727c478bd9Sstevel@tonic-gate 		}
22737c478bd9Sstevel@tonic-gate #ifdef DEBUG
22747c478bd9Sstevel@tonic-gate 		DEBUG("putfile: %s wants %" FMT_blkcnt_t " blocks\n", longname,
22757c478bd9Sstevel@tonic-gate 		    blocks);
22767c478bd9Sstevel@tonic-gate #endif
22777c478bd9Sstevel@tonic-gate 		if (build_dblock(name, tchar, '0', filetype,
22787c478bd9Sstevel@tonic-gate 		    &stbuf, stbuf.st_dev, prefix) != 0) {
22797c478bd9Sstevel@tonic-gate 			goto out;
22807c478bd9Sstevel@tonic-gate 		}
22817c478bd9Sstevel@tonic-gate 		if (vflag) {
22827c478bd9Sstevel@tonic-gate #ifdef DEBUG
22837c478bd9Sstevel@tonic-gate 			if (NotTape)
22847c478bd9Sstevel@tonic-gate 				DEBUG("seek = %" FMT_blkcnt_t "K\t", K(tapepos),
22857c478bd9Sstevel@tonic-gate 				    0);
22867c478bd9Sstevel@tonic-gate #endif
2287da6c28aaSamw 			(void) fprintf(vfile, "a %s%s%s%s ", longname,
2288da6c28aaSamw 			    rw_sysattr ? gettext(" system") : "",
2289da6c28aaSamw 			    (filetype == XATTR_FILE) ? gettext(
2290da6c28aaSamw 			    " attribute ") : "",
22917c478bd9Sstevel@tonic-gate 			    (filetype == XATTR_FILE) ?
2292da6c28aaSamw 			    longattrname : "");
22937c478bd9Sstevel@tonic-gate 			if (NotTape)
22947c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "%" FMT_blkcnt_t "K\n",
22957c478bd9Sstevel@tonic-gate 				    K(blocks));
22967c478bd9Sstevel@tonic-gate 			else
22977c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile,
22987c478bd9Sstevel@tonic-gate 				    gettext("%" FMT_blkcnt_t " tape blocks\n"),
22997c478bd9Sstevel@tonic-gate 				    blocks);
23007c478bd9Sstevel@tonic-gate 		}
23017c478bd9Sstevel@tonic-gate 
2302da6c28aaSamw 		if (put_extra_attributes(longname, shortname, longattrname,
2303da6c28aaSamw 		    prefix, filetype, '0') != 0)
23047c478bd9Sstevel@tonic-gate 			goto out;
23057c478bd9Sstevel@tonic-gate 
23067c478bd9Sstevel@tonic-gate 		/*
23077c478bd9Sstevel@tonic-gate 		 * No need to reset typeflag for extended attribute here, since
23087c478bd9Sstevel@tonic-gate 		 * put_extra_attributes already set it and we haven't called
23097c478bd9Sstevel@tonic-gate 		 * build_dblock().
23107c478bd9Sstevel@tonic-gate 		 */
23117c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
23127c478bd9Sstevel@tonic-gate 		hint = writetbuf((char *)&dblock, 1);
2313eace40a5Sceastha 		maxread = max(min(stbuf.st_blksize, stbuf.st_size),
2314eace40a5Sceastha 		    (nblock * TBLOCK));
23157c478bd9Sstevel@tonic-gate 		if ((bigbuf = calloc((unsigned)maxread, sizeof (char))) == 0) {
23167c478bd9Sstevel@tonic-gate 			maxread = TBLOCK;
23177c478bd9Sstevel@tonic-gate 			bigbuf = buf;
23187c478bd9Sstevel@tonic-gate 		}
23197c478bd9Sstevel@tonic-gate 
23207c478bd9Sstevel@tonic-gate 		while (((i = (int)
23217c478bd9Sstevel@tonic-gate 		    read(infile, bigbuf, min((hint*TBLOCK), maxread))) > 0) &&
23227c478bd9Sstevel@tonic-gate 		    blocks) {
23237c478bd9Sstevel@tonic-gate 			blkcnt_t nblks;
23247c478bd9Sstevel@tonic-gate 
23257c478bd9Sstevel@tonic-gate 			nblks = ((i-1)/TBLOCK)+1;
23267c478bd9Sstevel@tonic-gate 			if (nblks > blocks)
23277c478bd9Sstevel@tonic-gate 				nblks = blocks;
23287c478bd9Sstevel@tonic-gate 			hint = writetbuf(bigbuf, nblks);
23297c478bd9Sstevel@tonic-gate 			blocks -= nblks;
23307c478bd9Sstevel@tonic-gate 		}
23317c478bd9Sstevel@tonic-gate 		(void) close(infile);
23327c478bd9Sstevel@tonic-gate 		if (bigbuf != buf)
23337c478bd9Sstevel@tonic-gate 			free(bigbuf);
23347c478bd9Sstevel@tonic-gate 		if (i < 0)
23357c478bd9Sstevel@tonic-gate 			vperror(0, gettext("Read error on %s"), longname);
23367c478bd9Sstevel@tonic-gate 		else if (blocks != 0 || i != 0) {
23377c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
23387c478bd9Sstevel@tonic-gate 			"tar: %s: file changed size\n"), longname);
23397c478bd9Sstevel@tonic-gate 			if (errflag) {
23407c478bd9Sstevel@tonic-gate 				exitflag = 1;
23417c478bd9Sstevel@tonic-gate 				Errflg = 1;
23427c478bd9Sstevel@tonic-gate 			} else if (!Dflag) {
23437c478bd9Sstevel@tonic-gate 				Errflg = 1;
23447c478bd9Sstevel@tonic-gate 			}
23457c478bd9Sstevel@tonic-gate 		}
23467c478bd9Sstevel@tonic-gate 		putempty(blocks);
23477c478bd9Sstevel@tonic-gate 		break;
23487c478bd9Sstevel@tonic-gate 	case S_IFIFO:
23497c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(stbuf.st_size);
23507c478bd9Sstevel@tonic-gate 		stbuf.st_size = (off_t)0;
23517c478bd9Sstevel@tonic-gate 
2352da6c28aaSamw 		if (put_link(name, longname, shortname, longattrname,
23537c478bd9Sstevel@tonic-gate 		    prefix, filetype, '6') == 0) {
23547c478bd9Sstevel@tonic-gate 			rc = PUT_AS_LINK;
23557c478bd9Sstevel@tonic-gate 			goto out;
23567c478bd9Sstevel@tonic-gate 		}
23577c478bd9Sstevel@tonic-gate 		tomodes(&stbuf);
23587c478bd9Sstevel@tonic-gate 
23597c478bd9Sstevel@tonic-gate 		while (mulvol && tapepos + blocks + 1 > blocklim) {
23607c478bd9Sstevel@tonic-gate 			if (((blocklim - tapepos) >= EXTMIN) &&
23617c478bd9Sstevel@tonic-gate 			    ((blocks + 1) >= blocklim/10)) {
23627c478bd9Sstevel@tonic-gate 				splitfile(longname, infile, name,
23637c478bd9Sstevel@tonic-gate 				    prefix, filetype);
23647c478bd9Sstevel@tonic-gate 				(void) close(dirfd);
23657c478bd9Sstevel@tonic-gate 				(void) close(infile);
23667c478bd9Sstevel@tonic-gate 				goto out;
23677c478bd9Sstevel@tonic-gate 			}
23687c478bd9Sstevel@tonic-gate 			newvol();
23697c478bd9Sstevel@tonic-gate 		}
23707c478bd9Sstevel@tonic-gate #ifdef DEBUG
23717c478bd9Sstevel@tonic-gate 		DEBUG("putfile: %s wants %" FMT_blkcnt_t " blocks\n", longname,
23727c478bd9Sstevel@tonic-gate 		    blocks);
23737c478bd9Sstevel@tonic-gate #endif
23747c478bd9Sstevel@tonic-gate 		if (vflag) {
23757c478bd9Sstevel@tonic-gate #ifdef DEBUG
23767c478bd9Sstevel@tonic-gate 			if (NotTape)
23777c478bd9Sstevel@tonic-gate 				DEBUG("seek = %" FMT_blkcnt_t "K\t", K(tapepos),
23787c478bd9Sstevel@tonic-gate 				    0);
23797c478bd9Sstevel@tonic-gate #endif
23807c478bd9Sstevel@tonic-gate 			if (NotTape)
23817c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("a %s %"
23827c478bd9Sstevel@tonic-gate 				    FMT_blkcnt_t "K\n "), longname, K(blocks));
23837c478bd9Sstevel@tonic-gate 			else
23847c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext(
23857c478bd9Sstevel@tonic-gate 				    "a %s %" FMT_blkcnt_t " tape blocks\n"),
23867c478bd9Sstevel@tonic-gate 				    longname, blocks);
23877c478bd9Sstevel@tonic-gate 		}
23887c478bd9Sstevel@tonic-gate 		if (build_dblock(name, tchar, '6', filetype,
23897c478bd9Sstevel@tonic-gate 		    &stbuf, stbuf.st_dev, prefix) != 0)
23907c478bd9Sstevel@tonic-gate 			goto out;
23917c478bd9Sstevel@tonic-gate 
2392da6c28aaSamw 		if (put_extra_attributes(longname, shortname, longattrname,
2393da6c28aaSamw 		    prefix, filetype, '6') != 0)
23947c478bd9Sstevel@tonic-gate 			goto out;
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
23977c478bd9Sstevel@tonic-gate 		dblock.dbuf.typeflag = '6';
23987c478bd9Sstevel@tonic-gate 
23997c478bd9Sstevel@tonic-gate 		(void) writetbuf((char *)&dblock, 1);
24007c478bd9Sstevel@tonic-gate 		break;
24017c478bd9Sstevel@tonic-gate 	case S_IFCHR:
24027c478bd9Sstevel@tonic-gate 		stbuf.st_size = (off_t)0;
24037c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(stbuf.st_size);
2404da6c28aaSamw 		if (put_link(name, longname, shortname, longattrname,
2405da6c28aaSamw 		    prefix, filetype, '3') == 0) {
24067c478bd9Sstevel@tonic-gate 			rc = PUT_AS_LINK;
24077c478bd9Sstevel@tonic-gate 			goto out;
24087c478bd9Sstevel@tonic-gate 		}
24097c478bd9Sstevel@tonic-gate 		tomodes(&stbuf);
24107c478bd9Sstevel@tonic-gate 
24117c478bd9Sstevel@tonic-gate 		while (mulvol && tapepos + blocks + 1 > blocklim) {
24127c478bd9Sstevel@tonic-gate 			if (((blocklim - tapepos) >= EXTMIN) &&
24137c478bd9Sstevel@tonic-gate 			    ((blocks + 1) >= blocklim/10)) {
24147c478bd9Sstevel@tonic-gate 				splitfile(longname, infile, name,
24157c478bd9Sstevel@tonic-gate 				    prefix, filetype);
24167c478bd9Sstevel@tonic-gate 				(void) close(dirfd);
24177c478bd9Sstevel@tonic-gate 				goto out;
24187c478bd9Sstevel@tonic-gate 			}
24197c478bd9Sstevel@tonic-gate 			newvol();
24207c478bd9Sstevel@tonic-gate 		}
24217c478bd9Sstevel@tonic-gate #ifdef DEBUG
24227c478bd9Sstevel@tonic-gate 		DEBUG("putfile: %s wants %" FMT_blkcnt_t " blocks\n", longname,
24237c478bd9Sstevel@tonic-gate 		    blocks);
24247c478bd9Sstevel@tonic-gate #endif
24257c478bd9Sstevel@tonic-gate 		if (vflag) {
24267c478bd9Sstevel@tonic-gate #ifdef DEBUG
24277c478bd9Sstevel@tonic-gate 			if (NotTape)
24287c478bd9Sstevel@tonic-gate 				DEBUG("seek = %" FMT_blkcnt_t "K\t", K(tapepos),
24297c478bd9Sstevel@tonic-gate 				    0);
24307c478bd9Sstevel@tonic-gate #endif
24317c478bd9Sstevel@tonic-gate 			if (NotTape)
24327c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("a %s %"
24337c478bd9Sstevel@tonic-gate 				    FMT_blkcnt_t "K\n"), longname, K(blocks));
24347c478bd9Sstevel@tonic-gate 			else
24357c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("a %s %"
24367c478bd9Sstevel@tonic-gate 				    FMT_blkcnt_t " tape blocks\n"), longname,
24377c478bd9Sstevel@tonic-gate 				    blocks);
24387c478bd9Sstevel@tonic-gate 		}
24397c478bd9Sstevel@tonic-gate 		if (build_dblock(name, tchar, '3',
24407c478bd9Sstevel@tonic-gate 		    filetype, &stbuf, stbuf.st_rdev, prefix) != 0)
24417c478bd9Sstevel@tonic-gate 			goto out;
24427c478bd9Sstevel@tonic-gate 
2443da6c28aaSamw 		if (put_extra_attributes(longname, shortname, longattrname,
24447c478bd9Sstevel@tonic-gate 		    prefix, filetype, '3') != 0)
24457c478bd9Sstevel@tonic-gate 			goto out;
24467c478bd9Sstevel@tonic-gate 
24477c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
24487c478bd9Sstevel@tonic-gate 		dblock.dbuf.typeflag = '3';
24497c478bd9Sstevel@tonic-gate 
24507c478bd9Sstevel@tonic-gate 		(void) writetbuf((char *)&dblock, 1);
24517c478bd9Sstevel@tonic-gate 		break;
24527c478bd9Sstevel@tonic-gate 	case S_IFBLK:
24537c478bd9Sstevel@tonic-gate 		stbuf.st_size = (off_t)0;
24547c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(stbuf.st_size);
2455da6c28aaSamw 		if (put_link(name, longname, shortname, longattrname,
2456da6c28aaSamw 		    prefix, filetype, '4') == 0) {
24577c478bd9Sstevel@tonic-gate 			rc = PUT_AS_LINK;
24587c478bd9Sstevel@tonic-gate 			goto out;
24597c478bd9Sstevel@tonic-gate 		}
24607c478bd9Sstevel@tonic-gate 		tomodes(&stbuf);
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate 		while (mulvol && tapepos + blocks + 1 > blocklim) {
24637c478bd9Sstevel@tonic-gate 			if (((blocklim - tapepos) >= EXTMIN) &&
24647c478bd9Sstevel@tonic-gate 			    ((blocks + 1) >= blocklim/10)) {
24657c478bd9Sstevel@tonic-gate 				splitfile(longname, infile,
24667c478bd9Sstevel@tonic-gate 				    name, prefix, filetype);
24677c478bd9Sstevel@tonic-gate 				(void) close(dirfd);
24687c478bd9Sstevel@tonic-gate 				goto out;
24697c478bd9Sstevel@tonic-gate 			}
24707c478bd9Sstevel@tonic-gate 			newvol();
24717c478bd9Sstevel@tonic-gate 		}
24727c478bd9Sstevel@tonic-gate #ifdef DEBUG
24737c478bd9Sstevel@tonic-gate 		DEBUG("putfile: %s wants %" FMT_blkcnt_t " blocks\n", longname,
24747c478bd9Sstevel@tonic-gate 		    blocks);
24757c478bd9Sstevel@tonic-gate #endif
24767c478bd9Sstevel@tonic-gate 		if (vflag) {
24777c478bd9Sstevel@tonic-gate #ifdef DEBUG
24787c478bd9Sstevel@tonic-gate 			if (NotTape)
24797c478bd9Sstevel@tonic-gate 				DEBUG("seek = %" FMT_blkcnt_t "K\t", K(tapepos),
24807c478bd9Sstevel@tonic-gate 				    0);
24817c478bd9Sstevel@tonic-gate #endif
24827c478bd9Sstevel@tonic-gate 			(void) fprintf(vfile, "a %s ", longname);
24837c478bd9Sstevel@tonic-gate 			if (NotTape)
24847c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "%" FMT_blkcnt_t "K\n",
24857c478bd9Sstevel@tonic-gate 				    K(blocks));
24867c478bd9Sstevel@tonic-gate 			else
24877c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("%"
24887c478bd9Sstevel@tonic-gate 				    FMT_blkcnt_t " tape blocks\n"), blocks);
24897c478bd9Sstevel@tonic-gate 		}
24907c478bd9Sstevel@tonic-gate 		if (build_dblock(name, tchar, '4',
24917c478bd9Sstevel@tonic-gate 		    filetype, &stbuf, stbuf.st_rdev, prefix) != 0)
24927c478bd9Sstevel@tonic-gate 			goto out;
24937c478bd9Sstevel@tonic-gate 
2494da6c28aaSamw 		if (put_extra_attributes(longname, shortname, longattrname,
24957c478bd9Sstevel@tonic-gate 		    prefix, filetype, '4') != 0)
24967c478bd9Sstevel@tonic-gate 			goto out;
24977c478bd9Sstevel@tonic-gate 
24987c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
24997c478bd9Sstevel@tonic-gate 		dblock.dbuf.typeflag = '4';
25007c478bd9Sstevel@tonic-gate 
25017c478bd9Sstevel@tonic-gate 		(void) writetbuf((char *)&dblock, 1);
25027c478bd9Sstevel@tonic-gate 		break;
25037c478bd9Sstevel@tonic-gate 	default:
25047c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
25057c478bd9Sstevel@tonic-gate 		    "tar: %s is not a file. Not dumped\n"), longname);
25067c478bd9Sstevel@tonic-gate 		if (errflag)
25077c478bd9Sstevel@tonic-gate 			exitflag = 1;
25087c478bd9Sstevel@tonic-gate 		Errflg = 1;
25097c478bd9Sstevel@tonic-gate 		goto out;
25107c478bd9Sstevel@tonic-gate 	}
25117c478bd9Sstevel@tonic-gate 
25127c478bd9Sstevel@tonic-gate out:
2513da6c28aaSamw 	if ((dirfd != -1) && (filetype != XATTR_FILE)) {
25147c478bd9Sstevel@tonic-gate 		(void) close(dirfd);
25157c478bd9Sstevel@tonic-gate 	}
25167c478bd9Sstevel@tonic-gate 	return (rc);
25177c478bd9Sstevel@tonic-gate }
25187c478bd9Sstevel@tonic-gate 
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate /*
25217c478bd9Sstevel@tonic-gate  *	splitfile	dump a large file across volumes
25227c478bd9Sstevel@tonic-gate  *
25237c478bd9Sstevel@tonic-gate  *	splitfile(longname, fd);
25247c478bd9Sstevel@tonic-gate  *		char *longname;		full name of file
25257c478bd9Sstevel@tonic-gate  *		int ifd;		input file descriptor
25267c478bd9Sstevel@tonic-gate  *
25277c478bd9Sstevel@tonic-gate  *	NOTE:  only called by putfile() to dump a large file.
25287c478bd9Sstevel@tonic-gate  */
25297c478bd9Sstevel@tonic-gate 
25307c478bd9Sstevel@tonic-gate static void
splitfile(char * longname,int ifd,char * name,char * prefix,int filetype)25317c478bd9Sstevel@tonic-gate splitfile(char *longname, int ifd, char *name, char *prefix, int filetype)
25327c478bd9Sstevel@tonic-gate {
25337c478bd9Sstevel@tonic-gate 	blkcnt_t blocks;
25347c478bd9Sstevel@tonic-gate 	off_t bytes, s;
25357c478bd9Sstevel@tonic-gate 	char buf[TBLOCK];
25367c478bd9Sstevel@tonic-gate 	int i, extents;
25377c478bd9Sstevel@tonic-gate 
25387c478bd9Sstevel@tonic-gate 	blocks = TBLOCKS(stbuf.st_size);	/* blocks file needs */
25397c478bd9Sstevel@tonic-gate 
25407c478bd9Sstevel@tonic-gate 	/*
25417c478bd9Sstevel@tonic-gate 	 * # extents =
25427c478bd9Sstevel@tonic-gate 	 *	size of file after using up rest of this floppy
25437c478bd9Sstevel@tonic-gate 	 *		blocks - (blocklim - tapepos) + 1	(for header)
25447c478bd9Sstevel@tonic-gate 	 *	plus roundup value before divide by blocklim-1
25457c478bd9Sstevel@tonic-gate 	 *		+ (blocklim - 1) - 1
25467c478bd9Sstevel@tonic-gate 	 *	all divided by blocklim-1 (one block for each header).
25477c478bd9Sstevel@tonic-gate 	 * this gives
25487c478bd9Sstevel@tonic-gate 	 *	(blocks - blocklim + tapepos + 1 + blocklim - 2)/(blocklim-1)
25497c478bd9Sstevel@tonic-gate 	 * which reduces to the expression used.
25507c478bd9Sstevel@tonic-gate 	 * one is added to account for this first extent.
25517c478bd9Sstevel@tonic-gate 	 *
25527c478bd9Sstevel@tonic-gate 	 * When one is dealing with extremely large archives, one may want
25537c478bd9Sstevel@tonic-gate 	 * to allow for a large number of extents.  This code should be
25547c478bd9Sstevel@tonic-gate 	 * revisited to determine if extents should be changed to something
25557c478bd9Sstevel@tonic-gate 	 * larger than an int.
25567c478bd9Sstevel@tonic-gate 	 */
25577c478bd9Sstevel@tonic-gate 	extents = (int)((blocks + tapepos - 1ULL)/(blocklim - 1ULL) + 1);
25587c478bd9Sstevel@tonic-gate 
25597c478bd9Sstevel@tonic-gate 	if (extents < 2 || extents > MAXEXT) {	/* let's be reasonable */
25607c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
25617c478bd9Sstevel@tonic-gate 		    "tar: %s needs unusual number of volumes to split\n"
25627c478bd9Sstevel@tonic-gate 		    "tar: %s not dumped\n"), longname, longname);
25637c478bd9Sstevel@tonic-gate 		return;
25647c478bd9Sstevel@tonic-gate 	}
25657c478bd9Sstevel@tonic-gate 	if (build_dblock(name, tchar, '0', filetype,
25667c478bd9Sstevel@tonic-gate 	    &stbuf, stbuf.st_dev, prefix) != 0)
25677c478bd9Sstevel@tonic-gate 		return;
25687c478bd9Sstevel@tonic-gate 
25697c478bd9Sstevel@tonic-gate 	dblock.dbuf.extotal = extents;
25707c478bd9Sstevel@tonic-gate 	bytes = stbuf.st_size;
25717c478bd9Sstevel@tonic-gate 
25727c478bd9Sstevel@tonic-gate 	/*
25737c478bd9Sstevel@tonic-gate 	 * The value contained in dblock.dbuf.efsize was formerly used when the
25747c478bd9Sstevel@tonic-gate 	 * v flag was specified in conjunction with the t flag. Although it is
25757c478bd9Sstevel@tonic-gate 	 * no longer used, older versions of tar will expect the former
25767c478bd9Sstevel@tonic-gate 	 * behaviour, so we must continue to write it to the archive.
25777c478bd9Sstevel@tonic-gate 	 *
25787c478bd9Sstevel@tonic-gate 	 * Since dblock.dbuf.efsize is 10 chars in size, the maximum value it
25797c478bd9Sstevel@tonic-gate 	 * can store is TAR_EFSIZE_MAX. If bytes exceeds that value, simply
25807c478bd9Sstevel@tonic-gate 	 * store 0.
25817c478bd9Sstevel@tonic-gate 	 */
25827c478bd9Sstevel@tonic-gate 	if (bytes <= TAR_EFSIZE_MAX)
25837c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.efsize, "%9" FMT_off_t_o, bytes);
25847c478bd9Sstevel@tonic-gate 	else
25857c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.efsize, "%9" FMT_off_t_o, (off_t)0);
25867c478bd9Sstevel@tonic-gate 
25877c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
25887c478bd9Sstevel@tonic-gate 	    "tar: large file %s needs %d extents.\n"
25897c478bd9Sstevel@tonic-gate 	    "tar: current device seek position = %" FMT_blkcnt_t "K\n"),
25907c478bd9Sstevel@tonic-gate 	    longname, extents, K(tapepos));
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate 	s = (off_t)(blocklim - tapepos - 1) * TBLOCK;
25937c478bd9Sstevel@tonic-gate 	for (i = 1; i <= extents; i++) {
25947c478bd9Sstevel@tonic-gate 		if (i > 1) {
25957c478bd9Sstevel@tonic-gate 			newvol();
25967c478bd9Sstevel@tonic-gate 			if (i == extents)
25977c478bd9Sstevel@tonic-gate 				s = bytes;	/* last ext. gets true bytes */
25987c478bd9Sstevel@tonic-gate 			else
25997c478bd9Sstevel@tonic-gate 				s = (off_t)(blocklim - 1)*TBLOCK; /* all */
26007c478bd9Sstevel@tonic-gate 		}
26017c478bd9Sstevel@tonic-gate 		bytes -= s;
26027c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(s);
26037c478bd9Sstevel@tonic-gate 
26047c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.size, "%011" FMT_off_t_o, s);
26057c478bd9Sstevel@tonic-gate 		dblock.dbuf.extno = i;
26067c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
26077c478bd9Sstevel@tonic-gate 		(void) writetbuf((char *)&dblock, 1);
26087c478bd9Sstevel@tonic-gate 
26097c478bd9Sstevel@tonic-gate 		if (vflag)
26107c478bd9Sstevel@tonic-gate 			(void) fprintf(vfile,
26118e4a71aeSRich Burridge 			    gettext("+++ a %s %" FMT_blkcnt_t
26128e4a71aeSRich Burridge 			    "K [extent #%d of %d]\n"),
26137c478bd9Sstevel@tonic-gate 			    longname, K(blocks), i, extents);
26147c478bd9Sstevel@tonic-gate 		while (blocks && read(ifd, buf, TBLOCK) > 0) {
26157c478bd9Sstevel@tonic-gate 			blocks--;
26167c478bd9Sstevel@tonic-gate 			(void) writetbuf(buf, 1);
26177c478bd9Sstevel@tonic-gate 		}
26187c478bd9Sstevel@tonic-gate 		if (blocks != 0) {
26197c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
26207c478bd9Sstevel@tonic-gate 			    "tar: %s: file changed size\n"), longname);
26217c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
26227c478bd9Sstevel@tonic-gate 			    "tar: aborting split file %s\n"), longname);
26237c478bd9Sstevel@tonic-gate 			(void) close(ifd);
26247c478bd9Sstevel@tonic-gate 			return;
26257c478bd9Sstevel@tonic-gate 		}
26267c478bd9Sstevel@tonic-gate 	}
26277c478bd9Sstevel@tonic-gate 	(void) close(ifd);
26287c478bd9Sstevel@tonic-gate 	if (vflag)
26297c478bd9Sstevel@tonic-gate 		(void) fprintf(vfile, gettext("a %s %" FMT_off_t "K (in %d "
26307c478bd9Sstevel@tonic-gate 		    "extents)\n"), longname, K(TBLOCKS(stbuf.st_size)),
26317c478bd9Sstevel@tonic-gate 		    extents);
26327c478bd9Sstevel@tonic-gate }
26337c478bd9Sstevel@tonic-gate 
26347c478bd9Sstevel@tonic-gate /*
26357c478bd9Sstevel@tonic-gate  *	convtoreg - determines whether the file should be converted to a
26367c478bd9Sstevel@tonic-gate  *	            regular file when extracted
26377c478bd9Sstevel@tonic-gate  *
26387c478bd9Sstevel@tonic-gate  *	Returns 1 when file size > 0 and typeflag is not recognized
26397c478bd9Sstevel@tonic-gate  * 	Otherwise returns 0
26407c478bd9Sstevel@tonic-gate  */
26417c478bd9Sstevel@tonic-gate static int
convtoreg(off_t size)26427c478bd9Sstevel@tonic-gate convtoreg(off_t size)
26437c478bd9Sstevel@tonic-gate {
26447c478bd9Sstevel@tonic-gate 	if ((size > 0) && (dblock.dbuf.typeflag != '0') &&
26457c478bd9Sstevel@tonic-gate 	    (dblock.dbuf.typeflag != NULL) && (dblock.dbuf.typeflag != '1') &&
26467c478bd9Sstevel@tonic-gate 	    (dblock.dbuf.typeflag != '2') && (dblock.dbuf.typeflag != '3') &&
26477c478bd9Sstevel@tonic-gate 	    (dblock.dbuf.typeflag != '4') && (dblock.dbuf.typeflag != '5') &&
26487c478bd9Sstevel@tonic-gate 	    (dblock.dbuf.typeflag != '6') && (dblock.dbuf.typeflag != 'A') &&
2649e765faefSRich Burridge 	    (dblock.dbuf.typeflag != 'L') &&
26507c478bd9Sstevel@tonic-gate 	    (dblock.dbuf.typeflag != _XATTR_HDRTYPE) &&
26517c478bd9Sstevel@tonic-gate 	    (dblock.dbuf.typeflag != 'X')) {
26527c478bd9Sstevel@tonic-gate 		return (1);
26537c478bd9Sstevel@tonic-gate 	}
26547c478bd9Sstevel@tonic-gate 	return (0);
26557c478bd9Sstevel@tonic-gate }
26567c478bd9Sstevel@tonic-gate 
2657da6c28aaSamw #if defined(O_XATTR)
2658da6c28aaSamw static int
save_cwd(void)2659da6c28aaSamw save_cwd(void)
2660da6c28aaSamw {
2661da6c28aaSamw 	return (open(".", O_RDONLY));
2662da6c28aaSamw }
2663da6c28aaSamw #endif
2664da6c28aaSamw 
2665da6c28aaSamw #if defined(O_XATTR)
2666da6c28aaSamw static void
rest_cwd(int * cwd)2667da6c28aaSamw rest_cwd(int *cwd)
2668da6c28aaSamw {
2669da6c28aaSamw 	if (*cwd != -1) {
2670da6c28aaSamw 		if (fchdir(*cwd) < 0) {
2671da6c28aaSamw 			vperror(0, gettext(
2672da6c28aaSamw 			    "Cannot fchdir to attribute directory"));
2673da6c28aaSamw 			exit(1);
2674da6c28aaSamw 		}
2675da6c28aaSamw 		(void) close(*cwd);
2676da6c28aaSamw 		*cwd = -1;
2677da6c28aaSamw 	}
2678da6c28aaSamw }
2679da6c28aaSamw #endif
2680da6c28aaSamw 
2681da6c28aaSamw /*
2682da6c28aaSamw  * Verify the underlying file system supports the attribute type.
2683da6c28aaSamw  * Only archive extended attribute files when '-@' was specified.
2684da6c28aaSamw  * Only archive system extended attribute files if '-/' was specified.
2685da6c28aaSamw  */
2686da6c28aaSamw #if defined(O_XATTR)
2687da6c28aaSamw static attr_status_t
verify_attr_support(char * filename,int attrflg,arc_action_t actflag,int * ext_attrflg)2688ced83f9bSceastha verify_attr_support(char *filename, int attrflg, arc_action_t actflag,
2689ced83f9bSceastha     int *ext_attrflg)
2690da6c28aaSamw {
2691da6c28aaSamw 	/*
2692ced83f9bSceastha 	 * Verify extended attributes are supported/exist.  We only
2693ced83f9bSceastha 	 * need to check if we are processing a base file, not an
2694ced83f9bSceastha 	 * extended attribute.
2695da6c28aaSamw 	 */
2696ced83f9bSceastha 	if (attrflg) {
2697ced83f9bSceastha 		*ext_attrflg = (pathconf(filename, (actflag == ARC_CREATE) ?
2698ced83f9bSceastha 		    _PC_XATTR_EXISTS : _PC_XATTR_ENABLED) == 1);
2699ced83f9bSceastha 	}
2700ced83f9bSceastha 
2701da6c28aaSamw 	if (atflag) {
2702ced83f9bSceastha 		if (!*ext_attrflg) {
2703da6c28aaSamw #if defined(_PC_SATTR_ENABLED)
2704da6c28aaSamw 			if (saflag) {
2705da6c28aaSamw 				/* Verify system attributes are supported */
2706da6c28aaSamw 				if (sysattr_support(filename,
2707da6c28aaSamw 				    (actflag == ARC_CREATE) ? _PC_SATTR_EXISTS :
2708da6c28aaSamw 				    _PC_SATTR_ENABLED) != 1) {
2709da6c28aaSamw 					return (ATTR_SATTR_ERR);
2710da6c28aaSamw 				}
2711da6c28aaSamw 			} else
2712da6c28aaSamw 				return (ATTR_XATTR_ERR);
2713da6c28aaSamw #else
2714da6c28aaSamw 				return (ATTR_XATTR_ERR);
2715da6c28aaSamw #endif	/* _PC_SATTR_ENABLED */
2716da6c28aaSamw 		}
2717da6c28aaSamw 
2718da6c28aaSamw #if defined(_PC_SATTR_ENABLED)
2719da6c28aaSamw 	} else if (saflag) {
2720da6c28aaSamw 		/* Verify system attributes are supported */
2721da6c28aaSamw 		if (sysattr_support(filename, (actflag == ARC_CREATE) ?
2722da6c28aaSamw 		    _PC_SATTR_EXISTS : _PC_SATTR_ENABLED) != 1) {
2723da6c28aaSamw 			return (ATTR_SATTR_ERR);
2724da6c28aaSamw 		}
2725da6c28aaSamw #endif	/* _PC_SATTR_ENABLED */
2726da6c28aaSamw 	} else {
2727da6c28aaSamw 		return (ATTR_SKIP);
2728da6c28aaSamw 	}
2729da6c28aaSamw 
2730da6c28aaSamw 	return (ATTR_OK);
2731da6c28aaSamw }
2732da6c28aaSamw #endif
2733da6c28aaSamw 
2734da6c28aaSamw #if defined(O_XATTR)
2735da6c28aaSamw /*
2736da6c28aaSamw  * Recursively open attribute directories until the attribute directory
2737da6c28aaSamw  * containing the specified attribute, attrname, is opened.
2738da6c28aaSamw  *
2739da6c28aaSamw  * Currently, only 2 directory levels of attributes are supported, (i.e.,
2740da6c28aaSamw  * extended system attributes on extended attributes).  The following are
2741da6c28aaSamw  * the possible input combinations:
2742da6c28aaSamw  *	1.  Open the attribute directory of the base file (don't change
2743da6c28aaSamw  *	    into it).
2744da6c28aaSamw  *		attrinfo->parent = NULL
2745da6c28aaSamw  *		attrname = '.'
2746da6c28aaSamw  *	2.  Open the attribute directory of the base file and change into it.
2747da6c28aaSamw  *		attrinfo->parent = NULL
2748da6c28aaSamw  *		attrname = <attr> | <sys_attr>
2749da6c28aaSamw  *	3.  Open the attribute directory of the base file, change into it,
2750da6c28aaSamw  *	    then recursively call open_attr_dir() to open the attribute's
2751da6c28aaSamw  *	    parent directory (don't change into it).
2752da6c28aaSamw  *		attrinfo->parent = <attr>
2753da6c28aaSamw  *		attrname = '.'
2754da6c28aaSamw  *	4.  Open the attribute directory of the base file, change into it,
2755da6c28aaSamw  *	    then recursively call open_attr_dir() to open the attribute's
2756da6c28aaSamw  *	    parent directory and change into it.
2757da6c28aaSamw  *		attrinfo->parent = <attr>
2758da6c28aaSamw  *		attrname = <attr> | <sys_attr>
2759da6c28aaSamw  *
2760da6c28aaSamw  * An attribute directory will be opened only if the underlying file system
2761da6c28aaSamw  * supports the attribute type, and if the command line specifications (atflag
2762da6c28aaSamw  * and saflag) enable the processing of the attribute type.
2763da6c28aaSamw  *
2764da6c28aaSamw  * On succesful return, attrinfo->parentfd will be the file descriptor of the
2765da6c28aaSamw  * opened attribute directory.  In addition, if the attribute is a read-write
2766da6c28aaSamw  * extended system attribute, attrinfo->rw_sysattr will be set to 1, otherwise
2767da6c28aaSamw  * it will be set to 0.
2768da6c28aaSamw  *
2769da6c28aaSamw  * Possible return values:
2770da6c28aaSamw  * 	ATTR_OK		Successfully opened and, if needed, changed into the
2771da6c28aaSamw  *			attribute directory containing attrname.
2772da6c28aaSamw  *	ATTR_SKIP	The command line specifications don't enable the
2773da6c28aaSamw  *			processing of the attribute type.
2774da6c28aaSamw  * 	ATTR_CHDIR_ERR	An error occurred while trying to change into an
2775da6c28aaSamw  *			attribute directory.
2776da6c28aaSamw  * 	ATTR_OPEN_ERR	An error occurred while trying to open an
2777da6c28aaSamw  *			attribute directory.
2778da6c28aaSamw  *	ATTR_XATTR_ERR	The underlying file system doesn't support extended
2779da6c28aaSamw  *			attributes.
2780da6c28aaSamw  *	ATTR_SATTR_ERR	The underlying file system doesn't support extended
2781da6c28aaSamw  *			system attributes.
2782da6c28aaSamw  */
2783da6c28aaSamw static int
open_attr_dir(char * attrname,char * dirp,int cwd,attr_data_t * attrinfo)2784da6c28aaSamw open_attr_dir(char *attrname, char *dirp, int cwd, attr_data_t *attrinfo)
2785da6c28aaSamw {
2786da6c28aaSamw 	attr_status_t	rc;
2787da6c28aaSamw 	int		firsttime = (attrinfo->attr_parentfd == -1);
2788da6c28aaSamw 	int		saveerrno;
2789ced83f9bSceastha 	int		ext_attr;
2790da6c28aaSamw 
2791da6c28aaSamw 	/*
2792da6c28aaSamw 	 * open_attr_dir() was recursively called (input combination number 4),
2793da6c28aaSamw 	 * close the previously opened file descriptor as we've already changed
2794da6c28aaSamw 	 * into it.
2795da6c28aaSamw 	 */
2796da6c28aaSamw 	if (!firsttime) {
2797da6c28aaSamw 		(void) close(attrinfo->attr_parentfd);
2798da6c28aaSamw 		attrinfo->attr_parentfd = -1;
2799da6c28aaSamw 	}
2800da6c28aaSamw 
2801da6c28aaSamw 	/*
2802da6c28aaSamw 	 * Verify that the underlying file system supports the restoration
2803da6c28aaSamw 	 * of the attribute.
2804da6c28aaSamw 	 */
2805ced83f9bSceastha 	if ((rc = verify_attr_support(dirp, firsttime, ARC_RESTORE,
2806ced83f9bSceastha 	    &ext_attr)) != ATTR_OK) {
2807da6c28aaSamw 		return (rc);
2808da6c28aaSamw 	}
2809da6c28aaSamw 
2810da6c28aaSamw 	/* Open the base file's attribute directory */
2811da6c28aaSamw 	if ((attrinfo->attr_parentfd = attropen(dirp, ".", O_RDONLY)) == -1) {
2812da6c28aaSamw 		/*
2813da6c28aaSamw 		 * Save the errno from the attropen so it can be reported
2814da6c28aaSamw 		 * if the retry of the attropen fails.
2815da6c28aaSamw 		 */
2816da6c28aaSamw 		saveerrno = errno;
2817da6c28aaSamw 		if ((attrinfo->attr_parentfd = retry_open_attr(-1, cwd, dirp,
2818da6c28aaSamw 		    NULL, ".", O_RDONLY, 0)) == -1) {
2819da6c28aaSamw 			/*
2820da6c28aaSamw 			 * Reset typeflag back to real value so passtape
2821da6c28aaSamw 			 * will skip ahead correctly.
2822da6c28aaSamw 			 */
2823da6c28aaSamw 			dblock.dbuf.typeflag = _XATTR_HDRTYPE;
2824da6c28aaSamw 			(void) close(attrinfo->attr_parentfd);
2825da6c28aaSamw 			attrinfo->attr_parentfd = -1;
2826da6c28aaSamw 			errno = saveerrno;
2827da6c28aaSamw 			return (ATTR_OPEN_ERR);
2828da6c28aaSamw 		}
2829da6c28aaSamw 	}
2830da6c28aaSamw 
2831da6c28aaSamw 	/*
2832da6c28aaSamw 	 * Change into the parent attribute's directory unless we are
2833da6c28aaSamw 	 * processing the hidden attribute directory of the base file itself.
2834da6c28aaSamw 	 */
2835da6c28aaSamw 	if ((Hiddendir == 0) || (firsttime && attrinfo->attr_parent != NULL)) {
2836da6c28aaSamw 		if (fchdir(attrinfo->attr_parentfd) != 0) {
2837da6c28aaSamw 			saveerrno = errno;
2838da6c28aaSamw 			(void) close(attrinfo->attr_parentfd);
2839da6c28aaSamw 			attrinfo->attr_parentfd = -1;
2840da6c28aaSamw 			errno = saveerrno;
2841da6c28aaSamw 			return (ATTR_CHDIR_ERR);
2842da6c28aaSamw 		}
2843da6c28aaSamw 	}
2844da6c28aaSamw 
2845da6c28aaSamw 	/* Determine if the attribute should be processed */
2846da6c28aaSamw 	if ((rc = verify_attr(attrname, attrinfo->attr_parent, 1,
2847da6c28aaSamw 	    &attrinfo->attr_rw_sysattr)) != ATTR_OK) {
2848da6c28aaSamw 		saveerrno = errno;
2849da6c28aaSamw 		(void) close(attrinfo->attr_parentfd);
2850da6c28aaSamw 		attrinfo->attr_parentfd = -1;
2851da6c28aaSamw 		errno = saveerrno;
2852da6c28aaSamw 		return (rc);
2853da6c28aaSamw 	}
2854da6c28aaSamw 
2855da6c28aaSamw 	/*
2856da6c28aaSamw 	 * If the attribute is an extended attribute, or extended system
2857da6c28aaSamw 	 * attribute, of an attribute (i.e., <attr>/<sys_attr>), then
2858da6c28aaSamw 	 * recursively call open_attr_dir() to open the attribute directory
2859da6c28aaSamw 	 * of the parent attribute.
2860da6c28aaSamw 	 */
2861da6c28aaSamw 	if (firsttime && (attrinfo->attr_parent != NULL)) {
2862da6c28aaSamw 		return (open_attr_dir(attrname, attrinfo->attr_parent,
2863da6c28aaSamw 		    attrinfo->attr_parentfd, attrinfo));
2864da6c28aaSamw 	}
2865da6c28aaSamw 
2866da6c28aaSamw 	return (ATTR_OK);
2867da6c28aaSamw }
2868da6c28aaSamw #endif
2869da6c28aaSamw 
28707c478bd9Sstevel@tonic-gate static void
doxtract(char * argv[])28717c478bd9Sstevel@tonic-gate doxtract(char *argv[])
28727c478bd9Sstevel@tonic-gate {
28737c478bd9Sstevel@tonic-gate 	struct	stat	xtractbuf;	/* stat on file after extracting */
28747c478bd9Sstevel@tonic-gate 	blkcnt_t blocks;
28757c478bd9Sstevel@tonic-gate 	off_t bytes;
28767c478bd9Sstevel@tonic-gate 	int ofile;
28777c478bd9Sstevel@tonic-gate 	int newfile;			/* Does the file already exist  */
28787c478bd9Sstevel@tonic-gate 	int xcnt = 0;			/* count # files extracted */
28797c478bd9Sstevel@tonic-gate 	int fcnt = 0;			/* count # files in argv list */
28807c478bd9Sstevel@tonic-gate 	int dir;
28817c478bd9Sstevel@tonic-gate 	int dirfd = -1;
2882da6c28aaSamw 	int cwd = -1;
2883ced83f9bSceastha 	int rw_sysattr;
2884da6c28aaSamw 	int saveerrno;
28857c478bd9Sstevel@tonic-gate 	uid_t Uid;
28867c478bd9Sstevel@tonic-gate 	char *namep, *dirp, *comp, *linkp; /* for removing absolute paths */
28877c478bd9Sstevel@tonic-gate 	char dirname[PATH_MAX+1];
28887c478bd9Sstevel@tonic-gate 	char templink[PATH_MAX+1];	/* temp link with terminating NULL */
28897c478bd9Sstevel@tonic-gate 	int once = 1;
28907c478bd9Sstevel@tonic-gate 	int error;
28917c478bd9Sstevel@tonic-gate 	int symflag;
28927c478bd9Sstevel@tonic-gate 	int want;
2893da6c28aaSamw 	attr_data_t *attrinfo = NULL;	/* attribute info */
2894fa9e4066Sahrens 	acl_t	*aclp = NULL;	/* acl info */
289545916cd2Sjpk 	char dot[] = ".";		/* dirp for using realpath */
28967c478bd9Sstevel@tonic-gate 	timestruc_t	time_zero;	/* used for call to doDirTimes */
28977c478bd9Sstevel@tonic-gate 	int		dircreate;
28987c478bd9Sstevel@tonic-gate 	int convflag;
28997c478bd9Sstevel@tonic-gate 	time_zero.tv_sec = 0;
29007c478bd9Sstevel@tonic-gate 	time_zero.tv_nsec = 0;
29017c478bd9Sstevel@tonic-gate 
290245916cd2Sjpk 	/* reset Trusted Extensions variables */
290345916cd2Sjpk 	rpath_flag = 0;
290445916cd2Sjpk 	lk_rpath_flag = 0;
290545916cd2Sjpk 	dir_flag = 0;
290645916cd2Sjpk 	mld_flag = 0;
290745916cd2Sjpk 	bslundef(&bs_label);
290845916cd2Sjpk 	bsllow(&admin_low);
290945916cd2Sjpk 	bslhigh(&admin_high);
291045916cd2Sjpk 	orig_namep = 0;
291145916cd2Sjpk 
29127c478bd9Sstevel@tonic-gate 	dumping = 0;	/* for newvol(), et al:  we are not writing */
29137c478bd9Sstevel@tonic-gate 
29147c478bd9Sstevel@tonic-gate 	Uid = getuid();
29157c478bd9Sstevel@tonic-gate 
29167c478bd9Sstevel@tonic-gate 	for (;;) {
29177c478bd9Sstevel@tonic-gate 		convflag = 0;
29187c478bd9Sstevel@tonic-gate 		symflag = 0;
29197c478bd9Sstevel@tonic-gate 		dir = 0;
2920ced83f9bSceastha 		Hiddendir = 0;
2921ced83f9bSceastha 		rw_sysattr = 0;
29227c478bd9Sstevel@tonic-gate 		ofile = -1;
29237c478bd9Sstevel@tonic-gate 
2924da6c28aaSamw 		if (dirfd != -1) {
2925da6c28aaSamw 			(void) close(dirfd);
2926da6c28aaSamw 			dirfd = -1;
2927da6c28aaSamw 		}
2928da6c28aaSamw 		if (ofile != -1) {
2929da6c28aaSamw 			if (close(ofile) != 0)
2930da6c28aaSamw 				vperror(2, gettext("close error"));
2931da6c28aaSamw 		}
2932da6c28aaSamw 
29337c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
2934da6c28aaSamw 		if (cwd != -1) {
2935da6c28aaSamw 			rest_cwd(&cwd);
2936da6c28aaSamw 		}
2937da6c28aaSamw #endif
2938da6c28aaSamw 
2939da6c28aaSamw 		/* namep is set by wantit to point to the full name */
2940da6c28aaSamw 		if ((want = wantit(argv, &namep, &dirp, &comp,
2941da6c28aaSamw 		    &attrinfo)) == 0) {
2942da6c28aaSamw #if defined(O_XATTR)
2943da6c28aaSamw 			if (xattrp != NULL) {
29447c478bd9Sstevel@tonic-gate 				free(xattrhead);
29457c478bd9Sstevel@tonic-gate 				xattrp = NULL;
29467c478bd9Sstevel@tonic-gate 				xattr_linkp = NULL;
29477c478bd9Sstevel@tonic-gate 				xattrhead = NULL;
29487c478bd9Sstevel@tonic-gate 			}
29497c478bd9Sstevel@tonic-gate #endif
29507c478bd9Sstevel@tonic-gate 			continue;
29517c478bd9Sstevel@tonic-gate 		}
29527c478bd9Sstevel@tonic-gate 		if (want == -1)
29537c478bd9Sstevel@tonic-gate 			break;
29547c478bd9Sstevel@tonic-gate 
295545916cd2Sjpk /* Trusted Extensions */
295645916cd2Sjpk 		/*
295745916cd2Sjpk 		 * During tar extract (x):
295845916cd2Sjpk 		 * If the pathname of the restored file has been
295945916cd2Sjpk 		 * reconstructed from the ancillary file,
296045916cd2Sjpk 		 * use it to process the normal file.
296145916cd2Sjpk 		 */
296245916cd2Sjpk 		if (mld_flag) {		/* Skip over .MLD. directory */
296345916cd2Sjpk 			mld_flag = 0;
296445916cd2Sjpk 			passtape();
296545916cd2Sjpk 			continue;
296645916cd2Sjpk 		}
296745916cd2Sjpk 		orig_namep = namep;	/* save original */
296845916cd2Sjpk 		if (rpath_flag) {
296945916cd2Sjpk 			namep = real_path;	/* use zone path */
297045916cd2Sjpk 			comp = real_path;	/* use zone path */
297145916cd2Sjpk 			dirp = dot;		/* work from the top */
297245916cd2Sjpk 			rpath_flag = 0;		/* reset */
297345916cd2Sjpk 		}
297445916cd2Sjpk 
29757c478bd9Sstevel@tonic-gate 		if (dirfd != -1)
29767c478bd9Sstevel@tonic-gate 			(void) close(dirfd);
29777c478bd9Sstevel@tonic-gate 
29787c478bd9Sstevel@tonic-gate 		(void) strcpy(&dirname[0], namep);
29797c478bd9Sstevel@tonic-gate 		dircreate = checkdir(&dirname[0]);
29807c478bd9Sstevel@tonic-gate 
29817c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
2982da6c28aaSamw 		if (xattrp != NULL) {
2983da6c28aaSamw 			int	rc;
29847c478bd9Sstevel@tonic-gate 
2985da6c28aaSamw 			if (((cwd = save_cwd()) == -1) ||
2986da6c28aaSamw 			    ((rc = open_attr_dir(comp, dirp, cwd,
2987da6c28aaSamw 			    attrinfo)) != ATTR_OK)) {
2988da6c28aaSamw 				if (cwd == -1) {
2989da6c28aaSamw 					vperror(0, gettext(
2990da6c28aaSamw 					    "unable to save current working "
2991da6c28aaSamw 					    "directory while processing "
2992da6c28aaSamw 					    "attribute %s of %s"),
2993da6c28aaSamw 					    dirp, attrinfo->attr_path);
2994da6c28aaSamw 				} else if (rc != ATTR_SKIP) {
2995d2443e76Smarks 					(void) fprintf(vfile,
2996d2443e76Smarks 					    gettext("tar: cannot open "
2997da6c28aaSamw 					    "%sattribute %s of file %s: %s\n"),
2998da6c28aaSamw 					    attrinfo->attr_rw_sysattr ? gettext(
2999da6c28aaSamw 					    "system ") : "",
3000da6c28aaSamw 					    comp, dirp, strerror(errno));
3001da6c28aaSamw 				}
3002d2443e76Smarks 				free(xattrhead);
3003d2443e76Smarks 				xattrp = NULL;
3004d2443e76Smarks 				xattr_linkp = NULL;
3005d2443e76Smarks 				xattrhead = NULL;
3006da6c28aaSamw 
30077c478bd9Sstevel@tonic-gate 				passtape();
30087c478bd9Sstevel@tonic-gate 				continue;
3009da6c28aaSamw 			} else {
3010da6c28aaSamw 				dirfd = attrinfo->attr_parentfd;
3011da6c28aaSamw 				rw_sysattr = attrinfo->attr_rw_sysattr;
30127c478bd9Sstevel@tonic-gate 			}
3013da6c28aaSamw 		} else {
3014da6c28aaSamw 			dirfd = open(dirp, O_RDONLY);
3015da6c28aaSamw 		}
3016da6c28aaSamw #else
3017da6c28aaSamw 		dirfd = open(dirp, O_RDONLY);
3018da6c28aaSamw #endif
3019da6c28aaSamw 		if (dirfd == -1) {
3020da6c28aaSamw 			(void) fprintf(vfile, gettext(
3021da6c28aaSamw 			    "tar: cannot open %s: %s\n"),
3022da6c28aaSamw 			    dirp, strerror(errno));
3023da6c28aaSamw 			passtape();
3024da6c28aaSamw 			continue;
30257c478bd9Sstevel@tonic-gate 		}
30267c478bd9Sstevel@tonic-gate 
30277c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_LINKPATH)
30287c478bd9Sstevel@tonic-gate 			(void) strcpy(templink, Xtarhdr.x_linkpath);
30297c478bd9Sstevel@tonic-gate 		else {
30307c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
30317c478bd9Sstevel@tonic-gate 			if (xattrp && dblock.dbuf.typeflag == '1') {
30327c478bd9Sstevel@tonic-gate 				(void) sprintf(templink, "%.*s", NAMSIZ,
30337c478bd9Sstevel@tonic-gate 				    xattrp->h_names);
30347c478bd9Sstevel@tonic-gate 			} else {
30357c478bd9Sstevel@tonic-gate 				(void) sprintf(templink, "%.*s", NAMSIZ,
30367c478bd9Sstevel@tonic-gate 				    dblock.dbuf.linkname);
30377c478bd9Sstevel@tonic-gate 			}
30387c478bd9Sstevel@tonic-gate #else
30397c478bd9Sstevel@tonic-gate 			(void) sprintf(templink, "%.*s", NAMSIZ,
30407c478bd9Sstevel@tonic-gate 			    dblock.dbuf.linkname);
30417c478bd9Sstevel@tonic-gate #endif
30427c478bd9Sstevel@tonic-gate 		}
30437c478bd9Sstevel@tonic-gate 
30447c478bd9Sstevel@tonic-gate 		if (Fflag) {
30453a1dab68SRich Burridge 			if (checkf(namep, is_directory(namep), Fflag) == 0) {
30467c478bd9Sstevel@tonic-gate 				passtape();
30477c478bd9Sstevel@tonic-gate 				continue;
30487c478bd9Sstevel@tonic-gate 			}
30497c478bd9Sstevel@tonic-gate 		}
30507c478bd9Sstevel@tonic-gate 
30517c478bd9Sstevel@tonic-gate 		if (checkw('x', namep) == 0) {
30527c478bd9Sstevel@tonic-gate 			passtape();
30537c478bd9Sstevel@tonic-gate 			continue;
30547c478bd9Sstevel@tonic-gate 		}
30557c478bd9Sstevel@tonic-gate 		if (once) {
30567c478bd9Sstevel@tonic-gate 			if (strcmp(dblock.dbuf.magic, magic_type) == 0) {
30577c478bd9Sstevel@tonic-gate 				if (geteuid() == (uid_t)0) {
30587c478bd9Sstevel@tonic-gate 					checkflag = 1;
30597c478bd9Sstevel@tonic-gate 					pflag = 1;
30607c478bd9Sstevel@tonic-gate 				} else {
30617c478bd9Sstevel@tonic-gate 					/* get file creation mask */
30627c478bd9Sstevel@tonic-gate 					Oumask = umask(0);
30637c478bd9Sstevel@tonic-gate 					(void) umask(Oumask);
30647c478bd9Sstevel@tonic-gate 				}
30657c478bd9Sstevel@tonic-gate 				once = 0;
30667c478bd9Sstevel@tonic-gate 			} else {
30677c478bd9Sstevel@tonic-gate 				if (geteuid() == (uid_t)0) {
30687c478bd9Sstevel@tonic-gate 					pflag = 1;
30697c478bd9Sstevel@tonic-gate 					checkflag = 2;
30707c478bd9Sstevel@tonic-gate 				}
30717c478bd9Sstevel@tonic-gate 				if (!pflag) {
30727c478bd9Sstevel@tonic-gate 					/* get file creation mask */
30737c478bd9Sstevel@tonic-gate 					Oumask = umask(0);
30747c478bd9Sstevel@tonic-gate 					(void) umask(Oumask);
30757c478bd9Sstevel@tonic-gate 				}
30767c478bd9Sstevel@tonic-gate 				once = 0;
30777c478bd9Sstevel@tonic-gate 			}
30787c478bd9Sstevel@tonic-gate 		}
30797c478bd9Sstevel@tonic-gate 
30807c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
30817c478bd9Sstevel@tonic-gate 		/*
30827c478bd9Sstevel@tonic-gate 		 * Handle extraction of hidden attr dir.
30837c478bd9Sstevel@tonic-gate 		 * Dir is automatically created, we only
30847c478bd9Sstevel@tonic-gate 		 * need to update mode and perm's.
30857c478bd9Sstevel@tonic-gate 		 */
3086da6c28aaSamw 		if ((xattrp != NULL) && Hiddendir == 1) {
3087da6c28aaSamw 			bytes = stbuf.st_size;
3088da6c28aaSamw 			blocks = TBLOCKS(bytes);
3089da6c28aaSamw 			if (vflag) {
3090da6c28aaSamw 				(void) fprintf(vfile,
30918e4a71aeSRich Burridge 				    "x %s%s%s, %" FMT_off_t " %s, ", namep,
3092da6c28aaSamw 				    gettext(" attribute "),
30938e4a71aeSRich Burridge 				    xattrapath, bytes,
30948e4a71aeSRich Burridge 				    gettext("bytes"));
3095da6c28aaSamw 				if (NotTape)
3096da6c28aaSamw 					(void) fprintf(vfile,
3097da6c28aaSamw 					    "%" FMT_blkcnt_t "K\n", K(blocks));
3098da6c28aaSamw 				else
3099da6c28aaSamw 					(void) fprintf(vfile, gettext("%"
3100da6c28aaSamw 					    FMT_blkcnt_t " tape blocks\n"),
3101da6c28aaSamw 					    blocks);
3102da6c28aaSamw 			}
3103da6c28aaSamw 
3104da6c28aaSamw 			/*
3105da6c28aaSamw 			 * Set the permissions and mode of the attribute
3106da6c28aaSamw 			 * unless the attribute is a system attribute (can't
3107da6c28aaSamw 			 * successfully do this) or the hidden attribute
3108da6c28aaSamw 			 * directory (".") of an attribute (when the attribute
3109da6c28aaSamw 			 * is restored, the hidden attribute directory of an
3110da6c28aaSamw 			 * attribute is transient).  Note:  when the permissions
3111da6c28aaSamw 			 * and mode are set for the hidden attribute directory
3112da6c28aaSamw 			 * of a file on a system supporting extended system
3113da6c28aaSamw 			 * attributes, even though it returns successfully, it
3114da6c28aaSamw 			 * will not have any affect since the attribute
3115da6c28aaSamw 			 * directory is transient.
3116da6c28aaSamw 			 */
3117da6c28aaSamw 			if (attrinfo->attr_parent == NULL) {
31187c478bd9Sstevel@tonic-gate 				if (fchownat(dirfd, ".", stbuf.st_uid,
31197c478bd9Sstevel@tonic-gate 				    stbuf.st_gid, 0) != 0) {
31207c478bd9Sstevel@tonic-gate 					vperror(0, gettext(
3121da6c28aaSamw 					    "%s%s%s: failed to set ownership "
3122da6c28aaSamw 					    "of attribute directory"), namep,
3123da6c28aaSamw 					    gettext(" attribute "), xattrapath);
31247c478bd9Sstevel@tonic-gate 				}
31257c478bd9Sstevel@tonic-gate 
31267c478bd9Sstevel@tonic-gate 				if (fchmod(dirfd, stbuf.st_mode) != 0) {
31277c478bd9Sstevel@tonic-gate 					vperror(0, gettext(
3128da6c28aaSamw 					    "%s%s%s: failed to set permissions "
3129da6c28aaSamw 					    "of attribute directory"), namep,
3130da6c28aaSamw 					    gettext(" attribute "), xattrapath);
3131da6c28aaSamw 				}
31327c478bd9Sstevel@tonic-gate 			}
31337c478bd9Sstevel@tonic-gate 			goto filedone;
31347c478bd9Sstevel@tonic-gate 		}
31357c478bd9Sstevel@tonic-gate #endif
31367c478bd9Sstevel@tonic-gate 
31377c478bd9Sstevel@tonic-gate 		if (dircreate && (!is_posix || dblock.dbuf.typeflag == '5')) {
31387c478bd9Sstevel@tonic-gate 			dir = 1;
31397c478bd9Sstevel@tonic-gate 			if (vflag) {
31408e4a71aeSRich Burridge 				(void) fprintf(vfile, "x %s, 0 %s, ",
31418e4a71aeSRich Burridge 				    &dirname[0], gettext("bytes"));
31427c478bd9Sstevel@tonic-gate 				if (NotTape)
31437c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, "0K\n");
31447c478bd9Sstevel@tonic-gate 				else
31457c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext("%"
31467c478bd9Sstevel@tonic-gate 					    FMT_blkcnt_t " tape blocks\n"),
31477c478bd9Sstevel@tonic-gate 					    (blkcnt_t)0);
31487c478bd9Sstevel@tonic-gate 			}
31497c478bd9Sstevel@tonic-gate 			goto filedone;
31507c478bd9Sstevel@tonic-gate 		}
31517c478bd9Sstevel@tonic-gate 
31527c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '6') {	/* FIFO */
31537c478bd9Sstevel@tonic-gate 			if (rmdir(namep) < 0) {
31547c478bd9Sstevel@tonic-gate 				if (errno == ENOTDIR)
31557c478bd9Sstevel@tonic-gate 					(void) unlink(namep);
31567c478bd9Sstevel@tonic-gate 			}
31577c478bd9Sstevel@tonic-gate 			linkp = templink;
31587c478bd9Sstevel@tonic-gate 			if (*linkp !=  NULL) {
31597c478bd9Sstevel@tonic-gate 				if (Aflag && *linkp == '/')
31607c478bd9Sstevel@tonic-gate 					linkp++;
31617c478bd9Sstevel@tonic-gate 				if (link(linkp, namep) < 0) {
31627c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
31637c478bd9Sstevel@tonic-gate 					    "tar: %s: cannot link\n"), namep);
31647c478bd9Sstevel@tonic-gate 					continue;
31657c478bd9Sstevel@tonic-gate 				}
31667c478bd9Sstevel@tonic-gate 				if (vflag)
31677c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext(
3168da6c28aaSamw 					    "x %s linked to %s\n"), namep,
3169da6c28aaSamw 					    linkp);
31707c478bd9Sstevel@tonic-gate 				xcnt++;	 /* increment # files extracted */
31717c478bd9Sstevel@tonic-gate 				continue;
31727c478bd9Sstevel@tonic-gate 			}
31737c478bd9Sstevel@tonic-gate 			if (mknod(namep, (int)(Gen.g_mode|S_IFIFO),
31747c478bd9Sstevel@tonic-gate 			    (int)Gen.g_devmajor) < 0) {
31757c478bd9Sstevel@tonic-gate 				vperror(0, gettext("%s: mknod failed"), namep);
31767c478bd9Sstevel@tonic-gate 				continue;
31777c478bd9Sstevel@tonic-gate 			}
31787c478bd9Sstevel@tonic-gate 			bytes = stbuf.st_size;
31797c478bd9Sstevel@tonic-gate 			blocks = TBLOCKS(bytes);
31807c478bd9Sstevel@tonic-gate 			if (vflag) {
31817c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "x %s, %" FMT_off_t
31828e4a71aeSRich Burridge 				    " %s, ", namep, bytes, gettext("bytes"));
31837c478bd9Sstevel@tonic-gate 				if (NotTape)
31847c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, "%" FMT_blkcnt_t
31857c478bd9Sstevel@tonic-gate 					    "K\n", K(blocks));
31867c478bd9Sstevel@tonic-gate 				else
31877c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext("%"
31887c478bd9Sstevel@tonic-gate 					    FMT_blkcnt_t " tape blocks\n"),
31897c478bd9Sstevel@tonic-gate 					    blocks);
31907c478bd9Sstevel@tonic-gate 			}
31917c478bd9Sstevel@tonic-gate 			goto filedone;
31927c478bd9Sstevel@tonic-gate 		}
31937c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '3' && !Uid) { /* CHAR SPECIAL */
31947c478bd9Sstevel@tonic-gate 			if (rmdir(namep) < 0) {
31957c478bd9Sstevel@tonic-gate 				if (errno == ENOTDIR)
31967c478bd9Sstevel@tonic-gate 					(void) unlink(namep);
31977c478bd9Sstevel@tonic-gate 			}
31987c478bd9Sstevel@tonic-gate 			linkp = templink;
31997c478bd9Sstevel@tonic-gate 			if (*linkp != NULL) {
32007c478bd9Sstevel@tonic-gate 				if (Aflag && *linkp == '/')
32017c478bd9Sstevel@tonic-gate 					linkp++;
32027c478bd9Sstevel@tonic-gate 				if (link(linkp, namep) < 0) {
32037c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
32047c478bd9Sstevel@tonic-gate 					    "tar: %s: cannot link\n"), namep);
32057c478bd9Sstevel@tonic-gate 					continue;
32067c478bd9Sstevel@tonic-gate 				}
32077c478bd9Sstevel@tonic-gate 				if (vflag)
32087c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext(
3209da6c28aaSamw 					    "x %s linked to %s\n"), namep,
3210da6c28aaSamw 					    linkp);
32117c478bd9Sstevel@tonic-gate 				xcnt++;	 /* increment # files extracted */
32127c478bd9Sstevel@tonic-gate 				continue;
32137c478bd9Sstevel@tonic-gate 			}
32147c478bd9Sstevel@tonic-gate 			if (mknod(namep, (int)(Gen.g_mode|S_IFCHR),
32157c478bd9Sstevel@tonic-gate 			    (int)makedev(Gen.g_devmajor, Gen.g_devminor)) < 0) {
32167c478bd9Sstevel@tonic-gate 				vperror(0, gettext(
32177c478bd9Sstevel@tonic-gate 				    "%s: mknod failed"), namep);
32187c478bd9Sstevel@tonic-gate 				continue;
32197c478bd9Sstevel@tonic-gate 			}
32207c478bd9Sstevel@tonic-gate 			bytes = stbuf.st_size;
32217c478bd9Sstevel@tonic-gate 			blocks = TBLOCKS(bytes);
32227c478bd9Sstevel@tonic-gate 			if (vflag) {
32237c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "x %s, %" FMT_off_t
32248e4a71aeSRich Burridge 				    " %s, ", namep, bytes, gettext("bytes"));
32257c478bd9Sstevel@tonic-gate 				if (NotTape)
32267c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, "%" FMT_blkcnt_t
32277c478bd9Sstevel@tonic-gate 					    "K\n", K(blocks));
32287c478bd9Sstevel@tonic-gate 				else
32297c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext("%"
32307c478bd9Sstevel@tonic-gate 					    FMT_blkcnt_t " tape blocks\n"),
32317c478bd9Sstevel@tonic-gate 					    blocks);
32327c478bd9Sstevel@tonic-gate 			}
32337c478bd9Sstevel@tonic-gate 			goto filedone;
32347c478bd9Sstevel@tonic-gate 		} else if (dblock.dbuf.typeflag == '3' && Uid) {
32357c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
32367c478bd9Sstevel@tonic-gate 			    "Can't create special %s\n"), namep);
32377c478bd9Sstevel@tonic-gate 			continue;
32387c478bd9Sstevel@tonic-gate 		}
32397c478bd9Sstevel@tonic-gate 
32407c478bd9Sstevel@tonic-gate 		/* BLOCK SPECIAL */
32417c478bd9Sstevel@tonic-gate 
32427c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '4' && !Uid) {
32437c478bd9Sstevel@tonic-gate 			if (rmdir(namep) < 0) {
32447c478bd9Sstevel@tonic-gate 				if (errno == ENOTDIR)
32457c478bd9Sstevel@tonic-gate 					(void) unlink(namep);
32467c478bd9Sstevel@tonic-gate 			}
32477c478bd9Sstevel@tonic-gate 			linkp = templink;
32487c478bd9Sstevel@tonic-gate 			if (*linkp != NULL) {
32497c478bd9Sstevel@tonic-gate 				if (Aflag && *linkp == '/')
32507c478bd9Sstevel@tonic-gate 					linkp++;
32517c478bd9Sstevel@tonic-gate 				if (link(linkp, namep) < 0) {
32527c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
32537c478bd9Sstevel@tonic-gate 					    "tar: %s: cannot link\n"), namep);
32547c478bd9Sstevel@tonic-gate 					continue;
32557c478bd9Sstevel@tonic-gate 				}
32567c478bd9Sstevel@tonic-gate 				if (vflag)
32577c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext(
3258da6c28aaSamw 					    "x %s linked to %s\n"), namep,
3259da6c28aaSamw 					    linkp);
32607c478bd9Sstevel@tonic-gate 				xcnt++;	 /* increment # files extracted */
32617c478bd9Sstevel@tonic-gate 				continue;
32627c478bd9Sstevel@tonic-gate 			}
32637c478bd9Sstevel@tonic-gate 			if (mknod(namep, (int)(Gen.g_mode|S_IFBLK),
32647c478bd9Sstevel@tonic-gate 			    (int)makedev(Gen.g_devmajor, Gen.g_devminor)) < 0) {
32657c478bd9Sstevel@tonic-gate 				vperror(0, gettext("%s: mknod failed"), namep);
32667c478bd9Sstevel@tonic-gate 				continue;
32677c478bd9Sstevel@tonic-gate 			}
32687c478bd9Sstevel@tonic-gate 			bytes = stbuf.st_size;
32697c478bd9Sstevel@tonic-gate 			blocks = TBLOCKS(bytes);
32707c478bd9Sstevel@tonic-gate 			if (vflag) {
32717c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("x %s, %"
32727c478bd9Sstevel@tonic-gate 				    FMT_off_t " bytes, "), namep, bytes);
32737c478bd9Sstevel@tonic-gate 				if (NotTape)
32747c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, "%" FMT_blkcnt_t
32757c478bd9Sstevel@tonic-gate 					    "K\n", K(blocks));
32767c478bd9Sstevel@tonic-gate 				else
32777c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext("%"
32787c478bd9Sstevel@tonic-gate 					    FMT_blkcnt_t " tape blocks\n"),
32797c478bd9Sstevel@tonic-gate 					    blocks);
32807c478bd9Sstevel@tonic-gate 			}
32817c478bd9Sstevel@tonic-gate 			goto filedone;
32827c478bd9Sstevel@tonic-gate 		} else if (dblock.dbuf.typeflag == '4' && Uid) {
32837c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
32847c478bd9Sstevel@tonic-gate 			    gettext("Can't create special %s\n"), namep);
32857c478bd9Sstevel@tonic-gate 			continue;
32867c478bd9Sstevel@tonic-gate 		}
32877c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '2') {	/* symlink */
328845916cd2Sjpk 			if ((Tflag) && (lk_rpath_flag == 1))
328945916cd2Sjpk 				linkp = lk_real_path;
329045916cd2Sjpk 			else
32917c478bd9Sstevel@tonic-gate 				linkp = templink;
32927c478bd9Sstevel@tonic-gate 			if (Aflag && *linkp == '/')
32937c478bd9Sstevel@tonic-gate 				linkp++;
32947c478bd9Sstevel@tonic-gate 			if (rmdir(namep) < 0) {
32957c478bd9Sstevel@tonic-gate 				if (errno == ENOTDIR)
32967c478bd9Sstevel@tonic-gate 					(void) unlink(namep);
32977c478bd9Sstevel@tonic-gate 			}
32987c478bd9Sstevel@tonic-gate 			if (symlink(linkp, namep) < 0) {
32997c478bd9Sstevel@tonic-gate 				vperror(0, gettext("%s: symbolic link failed"),
33007c478bd9Sstevel@tonic-gate 				    namep);
33017c478bd9Sstevel@tonic-gate 				continue;
33027c478bd9Sstevel@tonic-gate 			}
33037c478bd9Sstevel@tonic-gate 			if (vflag)
33047c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext(
33057c478bd9Sstevel@tonic-gate 				    "x %s symbolic link to %s\n"),
33067c478bd9Sstevel@tonic-gate 				    namep, linkp);
33077c478bd9Sstevel@tonic-gate 
33087c478bd9Sstevel@tonic-gate 			symflag = AT_SYMLINK_NOFOLLOW;
33097c478bd9Sstevel@tonic-gate 			goto filedone;
33107c478bd9Sstevel@tonic-gate 		}
33117c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '1') {
33127c478bd9Sstevel@tonic-gate 			linkp = templink;
33137c478bd9Sstevel@tonic-gate 			if (Aflag && *linkp == '/')
33147c478bd9Sstevel@tonic-gate 				linkp++;
33157c478bd9Sstevel@tonic-gate 			if (unlinkat(dirfd, comp, AT_REMOVEDIR) < 0) {
33167c478bd9Sstevel@tonic-gate 				if (errno == ENOTDIR)
33177c478bd9Sstevel@tonic-gate 					(void) unlinkat(dirfd, comp, 0);
33187c478bd9Sstevel@tonic-gate 			}
33197c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
33207c478bd9Sstevel@tonic-gate 			if (xattrp && xattr_linkp) {
33217c478bd9Sstevel@tonic-gate 				if (fchdir(dirfd) < 0) {
33227c478bd9Sstevel@tonic-gate 					vperror(0, gettext(
33237c478bd9Sstevel@tonic-gate 					    "Cannot fchdir to attribute "
3324da6c28aaSamw 					    "directory %s"),
3325da6c28aaSamw 					    (attrinfo->attr_parent == NULL) ?
3326da6c28aaSamw 					    dirp : attrinfo->attr_parent);
33277c478bd9Sstevel@tonic-gate 					exit(1);
33287c478bd9Sstevel@tonic-gate 				}
33297c478bd9Sstevel@tonic-gate 
3330da6c28aaSamw 				error = link(xattr_linkaname, xattrapath);
33317c478bd9Sstevel@tonic-gate 			} else {
33327c478bd9Sstevel@tonic-gate 				error = link(linkp, namep);
33337c478bd9Sstevel@tonic-gate 			}
33347c478bd9Sstevel@tonic-gate #else
33357c478bd9Sstevel@tonic-gate 			error = link(linkp, namep);
33367c478bd9Sstevel@tonic-gate #endif
33377c478bd9Sstevel@tonic-gate 
33387c478bd9Sstevel@tonic-gate 			if (error < 0) {
33397c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
33407c478bd9Sstevel@tonic-gate 				    "tar: %s%s%s: cannot link\n"),
33417c478bd9Sstevel@tonic-gate 				    namep, (xattr_linkp != NULL) ?
33427c478bd9Sstevel@tonic-gate 				    gettext(" attribute ") : "",
33437c478bd9Sstevel@tonic-gate 				    (xattr_linkp != NULL) ?
3344da6c28aaSamw 				    xattrapath : "");
33457c478bd9Sstevel@tonic-gate 				continue;
33467c478bd9Sstevel@tonic-gate 			}
33477c478bd9Sstevel@tonic-gate 			if (vflag)
33487c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext(
3349da6c28aaSamw 				    "x %s%s%s linked to %s%s%s\n"), namep,
33507c478bd9Sstevel@tonic-gate 				    (xattr_linkp != NULL) ?
33517c478bd9Sstevel@tonic-gate 				    gettext(" attribute ") : "",
33527c478bd9Sstevel@tonic-gate 				    (xattr_linkp != NULL) ?
33537c478bd9Sstevel@tonic-gate 				    xattr_linkaname : "",
3354da6c28aaSamw 				    linkp,
33557c478bd9Sstevel@tonic-gate 				    (xattr_linkp != NULL) ?
3356da6c28aaSamw 				    gettext(" attribute ") : "",
3357da6c28aaSamw 				    (xattr_linkp != NULL) ? xattrapath : "");
33587c478bd9Sstevel@tonic-gate 			xcnt++;		/* increment # files extracted */
33597c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
3360da6c28aaSamw 			if (xattrp != NULL) {
33617c478bd9Sstevel@tonic-gate 				free(xattrhead);
33627c478bd9Sstevel@tonic-gate 				xattrp = NULL;
33637c478bd9Sstevel@tonic-gate 				xattr_linkp = NULL;
33647c478bd9Sstevel@tonic-gate 				xattrhead = NULL;
33657c478bd9Sstevel@tonic-gate 			}
33667c478bd9Sstevel@tonic-gate #endif
33677c478bd9Sstevel@tonic-gate 			continue;
33687c478bd9Sstevel@tonic-gate 		}
33697c478bd9Sstevel@tonic-gate 
33707c478bd9Sstevel@tonic-gate 		/* REGULAR FILES */
33717c478bd9Sstevel@tonic-gate 
33727c478bd9Sstevel@tonic-gate 		if (convtoreg(stbuf.st_size)) {
33737c478bd9Sstevel@tonic-gate 			convflag = 1;
33747c478bd9Sstevel@tonic-gate 			if (errflag) {
33757c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
33767c478bd9Sstevel@tonic-gate 				    "tar: %s: typeflag '%c' not recognized\n"),
33777c478bd9Sstevel@tonic-gate 				    namep, dblock.dbuf.typeflag);
33787c478bd9Sstevel@tonic-gate 				done(1);
33797c478bd9Sstevel@tonic-gate 			} else {
33807c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
33817c478bd9Sstevel@tonic-gate 				    "tar: %s: typeflag '%c' not recognized, "
33827c478bd9Sstevel@tonic-gate 				    "converting to regular file\n"), namep,
33837c478bd9Sstevel@tonic-gate 				    dblock.dbuf.typeflag);
33847c478bd9Sstevel@tonic-gate 				Errflg = 1;
33857c478bd9Sstevel@tonic-gate 			}
33867c478bd9Sstevel@tonic-gate 		}
33877c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '0' ||
33887c478bd9Sstevel@tonic-gate 		    dblock.dbuf.typeflag == NULL || convflag) {
33892c0f0499Slovely 			delete_target(dirfd, comp, namep);
33907c478bd9Sstevel@tonic-gate 			linkp = templink;
33917c478bd9Sstevel@tonic-gate 			if (*linkp != NULL) {
33927c478bd9Sstevel@tonic-gate 				if (Aflag && *linkp == '/')
33937c478bd9Sstevel@tonic-gate 					linkp++;
33947c478bd9Sstevel@tonic-gate 				if (link(linkp, comp) < 0) {
33957c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
33967c478bd9Sstevel@tonic-gate 					    "tar: %s: cannot link\n"), namep);
33977c478bd9Sstevel@tonic-gate 					continue;
33987c478bd9Sstevel@tonic-gate 				}
33997c478bd9Sstevel@tonic-gate 				if (vflag)
34007c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext(
3401da6c28aaSamw 					    "x %s linked to %s\n"), comp,
3402da6c28aaSamw 					    linkp);
34037c478bd9Sstevel@tonic-gate 				xcnt++;	 /* increment # files extracted */
3404da6c28aaSamw #if defined(O_XATTR)
3405da6c28aaSamw 				if (xattrp != NULL) {
3406da6c28aaSamw 					free(xattrhead);
3407da6c28aaSamw 					xattrp = NULL;
3408da6c28aaSamw 					xattr_linkp = NULL;
3409da6c28aaSamw 					xattrhead = NULL;
3410da6c28aaSamw 				}
3411da6c28aaSamw #endif
34127c478bd9Sstevel@tonic-gate 				continue;
34137c478bd9Sstevel@tonic-gate 			}
34147c478bd9Sstevel@tonic-gate 		newfile = ((fstatat(dirfd, comp,
34157c478bd9Sstevel@tonic-gate 		    &xtractbuf, 0) == -1) ? TRUE : FALSE);
3416da6c28aaSamw 		ofile = openat(dirfd, comp, O_RDWR|O_CREAT|O_TRUNC,
3417da6c28aaSamw 		    stbuf.st_mode & MODEMASK);
3418da6c28aaSamw 		saveerrno = errno;
3419da6c28aaSamw 
3420da6c28aaSamw #if defined(O_XATTR)
3421da6c28aaSamw 		if (xattrp != NULL) {
3422da6c28aaSamw 			if (ofile < 0) {
3423da6c28aaSamw 				ofile = retry_open_attr(dirfd, cwd,
3424da6c28aaSamw 				    dirp, attrinfo->attr_parent, comp,
3425da6c28aaSamw 				    O_RDWR|O_CREAT|O_TRUNC,
3426da6c28aaSamw 				    stbuf.st_mode & MODEMASK);
3427da6c28aaSamw 			}
3428da6c28aaSamw 		}
3429da6c28aaSamw #endif
3430da6c28aaSamw 		if (ofile < 0) {
3431da6c28aaSamw 			errno = saveerrno;
34327c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
3433da6c28aaSamw 			    "tar: %s%s%s%s - cannot create\n"),
3434da6c28aaSamw 			    (xattrp == NULL) ? "" : (rw_sysattr ?
34358e4a71aeSRich Burridge 			    gettext("system attribute ") :
3436da6c28aaSamw 			    gettext("attribute ")),
3437da6c28aaSamw 			    (xattrp == NULL) ? "" : xattrapath,
3438da6c28aaSamw 			    (xattrp == NULL) ? "" : gettext(" of "),
3439da6c28aaSamw 			    (xattrp == NULL) ? comp : namep);
34407c478bd9Sstevel@tonic-gate 			if (errflag)
34417c478bd9Sstevel@tonic-gate 				done(1);
34427c478bd9Sstevel@tonic-gate 			else
34437c478bd9Sstevel@tonic-gate 				Errflg = 1;
3444da6c28aaSamw #if defined(O_XATTR)
3445da6c28aaSamw 			if (xattrp != NULL) {
3446da6c28aaSamw 				dblock.dbuf.typeflag = _XATTR_HDRTYPE;
3447da6c28aaSamw 				free(xattrhead);
3448da6c28aaSamw 				xattrp = NULL;
3449da6c28aaSamw 				xattr_linkp = NULL;
3450da6c28aaSamw 				xattrhead = NULL;
3451da6c28aaSamw 			}
3452da6c28aaSamw #endif
34537c478bd9Sstevel@tonic-gate 			passtape();
34547c478bd9Sstevel@tonic-gate 			continue;
34557c478bd9Sstevel@tonic-gate 		}
34567c478bd9Sstevel@tonic-gate 
345745916cd2Sjpk 		if (Tflag && (check_ext_attr(namep) == 0)) {
345845916cd2Sjpk 			if (errflag)
345945916cd2Sjpk 				done(1);
346045916cd2Sjpk 			else
346145916cd2Sjpk 				Errflg = 1;
346245916cd2Sjpk 			passtape();
346345916cd2Sjpk 			continue;
346445916cd2Sjpk 		}
346545916cd2Sjpk 
34667c478bd9Sstevel@tonic-gate 		if (extno != 0) {	/* file is in pieces */
34677c478bd9Sstevel@tonic-gate 			if (extotal < 1 || extotal > MAXEXT)
34687c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
3469da6c28aaSamw 				    "tar: ignoring bad extent info for "
3470da6c28aaSamw 				    "%s%s%s%s\n"),
3471da6c28aaSamw 				    (xattrp == NULL) ? "" : (rw_sysattr ?
3472da6c28aaSamw 				    gettext("system attribute ") :
3473da6c28aaSamw 				    gettext("attribute ")),
3474da6c28aaSamw 				    (xattrp == NULL) ? "" : xattrapath,
3475da6c28aaSamw 				    (xattrp == NULL) ? "" : gettext(" of "),
3476da6c28aaSamw 				    (xattrp == NULL) ? comp : namep);
34777c478bd9Sstevel@tonic-gate 			else {
3478da6c28aaSamw 				/* extract it */
3479da6c28aaSamw 				(void) xsfile(rw_sysattr, ofile);
34807c478bd9Sstevel@tonic-gate 			}
34817c478bd9Sstevel@tonic-gate 		}
34827c478bd9Sstevel@tonic-gate 		extno = 0;	/* let everyone know file is not split */
34837c478bd9Sstevel@tonic-gate 		bytes = stbuf.st_size;
34847c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(bytes);
34857c478bd9Sstevel@tonic-gate 		if (vflag) {
34867c478bd9Sstevel@tonic-gate 			(void) fprintf(vfile,
34878e4a71aeSRich Burridge 			    "x %s%s%s, %" FMT_off_t " %s, ",
34887c478bd9Sstevel@tonic-gate 			    (xattrp == NULL) ? "" : dirp,
3489da6c28aaSamw 			    (xattrp == NULL) ? "" : (rw_sysattr ?
3490da6c28aaSamw 			    gettext(" system attribute ") :
3491da6c28aaSamw 			    gettext(" attribute ")),
34928e4a71aeSRich Burridge 			    (xattrp == NULL) ? namep : xattrapath, bytes,
34938e4a71aeSRich Burridge 			    gettext("bytes"));
34947c478bd9Sstevel@tonic-gate 			if (NotTape)
34957c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "%" FMT_blkcnt_t "K\n",
34967c478bd9Sstevel@tonic-gate 				    K(blocks));
34977c478bd9Sstevel@tonic-gate 			else
34987c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("%"
34997c478bd9Sstevel@tonic-gate 				    FMT_blkcnt_t " tape blocks\n"), blocks);
35007c478bd9Sstevel@tonic-gate 		}
35017c478bd9Sstevel@tonic-gate 
3502da6c28aaSamw 		if (xblocks(rw_sysattr, bytes, ofile) != 0) {
3503da6c28aaSamw #if defined(O_XATTR)
3504da6c28aaSamw 			if (xattrp != NULL) {
3505da6c28aaSamw 				free(xattrhead);
3506da6c28aaSamw 				xattrp = NULL;
3507da6c28aaSamw 				xattr_linkp = NULL;
3508da6c28aaSamw 				xattrhead = NULL;
3509da6c28aaSamw 			}
3510da6c28aaSamw #endif
3511da6c28aaSamw 			continue;
3512da6c28aaSamw 		}
35137c478bd9Sstevel@tonic-gate filedone:
35147c478bd9Sstevel@tonic-gate 		if (mflag == 0 && !symflag) {
35157c478bd9Sstevel@tonic-gate 			if (dir)
35167c478bd9Sstevel@tonic-gate 				doDirTimes(namep, stbuf.st_mtim);
3517da6c28aaSamw 
35187c478bd9Sstevel@tonic-gate 			else
3519da6c28aaSamw #if defined(O_XATTR)
3520da6c28aaSamw 				if (xattrp != NULL) {
3521da6c28aaSamw 					/*
3522da6c28aaSamw 					 * Set the time on the attribute unless
3523da6c28aaSamw 					 * the attribute is a system attribute
3524da6c28aaSamw 					 * (can't successfully do this) or the
3525da6c28aaSamw 					 * hidden attribute directory, "." (the
3526da6c28aaSamw 					 * time on the hidden attribute
3527da6c28aaSamw 					 * directory will be updated when
3528da6c28aaSamw 					 * attributes are restored, otherwise
3529da6c28aaSamw 					 * it's transient).
3530da6c28aaSamw 					 */
3531da6c28aaSamw 					if (!rw_sysattr && (Hiddendir == 0)) {
3532da6c28aaSamw 						setPathTimes(dirfd, comp,
3533da6c28aaSamw 						    stbuf.st_mtim);
3534da6c28aaSamw 					}
3535da6c28aaSamw 				} else
3536da6c28aaSamw 					setPathTimes(dirfd, comp,
3537da6c28aaSamw 					    stbuf.st_mtim);
3538da6c28aaSamw #else
35397c478bd9Sstevel@tonic-gate 				setPathTimes(dirfd, comp, stbuf.st_mtim);
3540da6c28aaSamw #endif
35417c478bd9Sstevel@tonic-gate 		}
35427c478bd9Sstevel@tonic-gate 
35437c478bd9Sstevel@tonic-gate 		/* moved this code from above */
35447c478bd9Sstevel@tonic-gate 		if (pflag && !symflag && Hiddendir == 0) {
3545da6c28aaSamw 			if (xattrp != NULL)
35467c478bd9Sstevel@tonic-gate 				(void) fchmod(ofile, stbuf.st_mode & MODEMASK);
35477c478bd9Sstevel@tonic-gate 			else
35487c478bd9Sstevel@tonic-gate 				(void) chmod(namep, stbuf.st_mode & MODEMASK);
35497c478bd9Sstevel@tonic-gate 		}
35507c478bd9Sstevel@tonic-gate 
35517c478bd9Sstevel@tonic-gate 
35527c478bd9Sstevel@tonic-gate 		/*
35537c478bd9Sstevel@tonic-gate 		 * Because ancillary file preceeds the normal file,
35547c478bd9Sstevel@tonic-gate 		 * acl info may have been retrieved (in aclp).
35557c478bd9Sstevel@tonic-gate 		 * All file types are directed here (go filedone).
35567c478bd9Sstevel@tonic-gate 		 * Always restore ACLs if there are ACLs.
35577c478bd9Sstevel@tonic-gate 		 */
35587c478bd9Sstevel@tonic-gate 		if (aclp != NULL) {
35597c478bd9Sstevel@tonic-gate 			int ret;
35607c478bd9Sstevel@tonic-gate 
35617c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
3562da6c28aaSamw 			if (xattrp != NULL) {
35637c478bd9Sstevel@tonic-gate 				if (Hiddendir)
3564fa9e4066Sahrens 					ret = facl_set(dirfd, aclp);
35657c478bd9Sstevel@tonic-gate 				else
3566fa9e4066Sahrens 					ret = facl_set(ofile, aclp);
35677c478bd9Sstevel@tonic-gate 			} else {
3568fa9e4066Sahrens 				ret = acl_set(namep, aclp);
35697c478bd9Sstevel@tonic-gate 			}
35707c478bd9Sstevel@tonic-gate #else
357145916cd2Sjpk 			ret = acl_set(namep, aclp);
35727c478bd9Sstevel@tonic-gate #endif
35737c478bd9Sstevel@tonic-gate 			if (ret < 0) {
35747c478bd9Sstevel@tonic-gate 				if (pflag) {
35757c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
3576da6c28aaSamw 					    "%s%s%s%s: failed to set acl "
3577da6c28aaSamw 					    "entries\n"), namep,
3578da6c28aaSamw 					    (xattrp == NULL) ? "" :
3579da6c28aaSamw 					    (rw_sysattr ? gettext(
3580da6c28aaSamw 					    " system attribute ") :
3581da6c28aaSamw 					    gettext(" attribute ")),
3582da6c28aaSamw 					    (xattrp == NULL) ? "" :
3583da6c28aaSamw 					    xattrapath);
35847c478bd9Sstevel@tonic-gate 				}
35857c478bd9Sstevel@tonic-gate 				/* else: silent and continue */
35867c478bd9Sstevel@tonic-gate 			}
3587fa9e4066Sahrens 			acl_free(aclp);
35887c478bd9Sstevel@tonic-gate 			aclp = NULL;
35897c478bd9Sstevel@tonic-gate 		}
35907c478bd9Sstevel@tonic-gate 
35917c478bd9Sstevel@tonic-gate 		if (!oflag)
3592cc22b130SRich Burridge 			/* set file ownership */
3593cc22b130SRich Burridge 			resugname(dirfd, comp, symflag);
35947c478bd9Sstevel@tonic-gate 
35957c478bd9Sstevel@tonic-gate 		if (pflag && newfile == TRUE && !dir &&
35967c478bd9Sstevel@tonic-gate 		    (dblock.dbuf.typeflag == '0' ||
35977c478bd9Sstevel@tonic-gate 		    dblock.dbuf.typeflag == NULL ||
35987c478bd9Sstevel@tonic-gate 		    convflag || dblock.dbuf.typeflag == '1')) {
35997c478bd9Sstevel@tonic-gate 			if (fstat(ofile, &xtractbuf) == -1)
36007c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
3601da6c28aaSamw 				    "tar: cannot stat extracted file "
3602da6c28aaSamw 				    "%s%s%s%s\n"),
3603da6c28aaSamw 				    (xattrp == NULL) ? "" : (rw_sysattr ?
3604da6c28aaSamw 				    gettext("system attribute ") :
3605da6c28aaSamw 				    gettext("attribute ")),
3606da6c28aaSamw 				    (xattrp == NULL) ? "" : xattrapath,
3607da6c28aaSamw 				    (xattrp == NULL) ? "" :
3608da6c28aaSamw 				    gettext(" of "), namep);
3609da6c28aaSamw 
36107c478bd9Sstevel@tonic-gate 			else if ((xtractbuf.st_mode & (MODEMASK & ~S_IFMT))
36117c478bd9Sstevel@tonic-gate 			    != (stbuf.st_mode & (MODEMASK & ~S_IFMT))) {
36127c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
36137c478bd9Sstevel@tonic-gate 				    "tar: warning - file permissions have "
3614da6c28aaSamw 				    "changed for %s%s%s%s (are 0%o, should be "
36157c478bd9Sstevel@tonic-gate 				    "0%o)\n"),
3616da6c28aaSamw 				    (xattrp == NULL) ? "" : (rw_sysattr ?
3617da6c28aaSamw 				    gettext("system attribute ") :
3618da6c28aaSamw 				    gettext("attribute ")),
3619da6c28aaSamw 				    (xattrp == NULL) ? "" : xattrapath,
3620da6c28aaSamw 				    (xattrp == NULL) ? "" :
3621da6c28aaSamw 				    gettext(" of "), namep,
3622da6c28aaSamw 				    xtractbuf.st_mode, stbuf.st_mode);
3623da6c28aaSamw 
36247c478bd9Sstevel@tonic-gate 			}
36257c478bd9Sstevel@tonic-gate 		}
3626ced83f9bSceastha #if defined(O_XATTR)
3627ced83f9bSceastha 		if (xattrp != NULL) {
3628ced83f9bSceastha 			free(xattrhead);
3629ced83f9bSceastha 			xattrp = NULL;
3630ced83f9bSceastha 			xattr_linkp = NULL;
3631ced83f9bSceastha 			xattrhead = NULL;
3632ced83f9bSceastha 		}
3633ced83f9bSceastha #endif
3634ced83f9bSceastha 
36357c478bd9Sstevel@tonic-gate 		if (ofile != -1) {
36367c478bd9Sstevel@tonic-gate 			(void) close(dirfd);
36377c478bd9Sstevel@tonic-gate 			dirfd = -1;
36387c478bd9Sstevel@tonic-gate 			if (close(ofile) != 0)
36397c478bd9Sstevel@tonic-gate 				vperror(2, gettext("close error"));
3640da6c28aaSamw 			ofile = -1;
36417c478bd9Sstevel@tonic-gate 		}
36427c478bd9Sstevel@tonic-gate 		xcnt++;			/* increment # files extracted */
36437c478bd9Sstevel@tonic-gate 		}
364445916cd2Sjpk 
364545916cd2Sjpk 		/*
364645916cd2Sjpk 		 * Process ancillary file.
364745916cd2Sjpk 		 *
364845916cd2Sjpk 		 */
364945916cd2Sjpk 
36507c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == 'A') {	/* acl info */
36517c478bd9Sstevel@tonic-gate 			char	buf[TBLOCK];
36527c478bd9Sstevel@tonic-gate 			char	*secp;
36537c478bd9Sstevel@tonic-gate 			char	*tp;
36547c478bd9Sstevel@tonic-gate 			int	attrsize;
36557c478bd9Sstevel@tonic-gate 			int	cnt;
36567c478bd9Sstevel@tonic-gate 
365745916cd2Sjpk 			/* reset Trusted Extensions flags */
365845916cd2Sjpk 			dir_flag = 0;
365945916cd2Sjpk 			mld_flag = 0;
366045916cd2Sjpk 			lk_rpath_flag = 0;
366145916cd2Sjpk 			rpath_flag = 0;
36627c478bd9Sstevel@tonic-gate 
36637c478bd9Sstevel@tonic-gate 			if (pflag) {
36647c478bd9Sstevel@tonic-gate 				bytes = stbuf.st_size;
36657c478bd9Sstevel@tonic-gate 				if ((secp = malloc((int)bytes)) == NULL) {
36667c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
36677c478bd9Sstevel@tonic-gate 					    "Insufficient memory for acl\n"));
36687c478bd9Sstevel@tonic-gate 					passtape();
36697c478bd9Sstevel@tonic-gate 					continue;
36707c478bd9Sstevel@tonic-gate 				}
36717c478bd9Sstevel@tonic-gate 				tp = secp;
36727c478bd9Sstevel@tonic-gate 				blocks = TBLOCKS(bytes);
367345916cd2Sjpk 
367445916cd2Sjpk 				/*
367545916cd2Sjpk 				 * Display a line for each ancillary file.
367645916cd2Sjpk 				 */
367745916cd2Sjpk 				if (vflag && Tflag)
367845916cd2Sjpk 					(void) fprintf(vfile, "x %s(A), %"
36798e4a71aeSRich Burridge 					    FMT_blkcnt_t " %s, %"
36808e4a71aeSRich Burridge 					    FMT_blkcnt_t " %s\n",
36818e4a71aeSRich Burridge 					    namep, bytes, gettext("bytes"),
36828e4a71aeSRich Burridge 					    blocks, gettext("tape blocks"));
368345916cd2Sjpk 
36847c478bd9Sstevel@tonic-gate 				while (blocks-- > 0) {
36857c478bd9Sstevel@tonic-gate 					readtape(buf);
36867c478bd9Sstevel@tonic-gate 					if (bytes <= TBLOCK) {
36877c478bd9Sstevel@tonic-gate 						(void) memcpy(tp, buf,
36887c478bd9Sstevel@tonic-gate 						    (size_t)bytes);
36897c478bd9Sstevel@tonic-gate 						break;
36907c478bd9Sstevel@tonic-gate 					} else {
36917c478bd9Sstevel@tonic-gate 						(void) memcpy(tp, buf,
36927c478bd9Sstevel@tonic-gate 						    TBLOCK);
36937c478bd9Sstevel@tonic-gate 						tp += TBLOCK;
36947c478bd9Sstevel@tonic-gate 					}
36957c478bd9Sstevel@tonic-gate 					bytes -= TBLOCK;
36967c478bd9Sstevel@tonic-gate 				}
3697fa9e4066Sahrens 				bytes = stbuf.st_size;
36987c478bd9Sstevel@tonic-gate 				/* got all attributes in secp */
36997c478bd9Sstevel@tonic-gate 				tp = secp;
37007c478bd9Sstevel@tonic-gate 				do {
37017c478bd9Sstevel@tonic-gate 					attr = (struct sec_attr *)tp;
37027c478bd9Sstevel@tonic-gate 					switch (attr->attr_type) {
37037c478bd9Sstevel@tonic-gate 					case UFSD_ACL:
3704fa9e4066Sahrens 					case ACE_ACL:
37057c478bd9Sstevel@tonic-gate 						(void) sscanf(attr->attr_len,
3706fa9e4066Sahrens 						    "%7o",
3707fa9e4066Sahrens 						    (uint_t *)
3708fa9e4066Sahrens 						    &cnt);
37097c478bd9Sstevel@tonic-gate 						/* header is 8 */
37107c478bd9Sstevel@tonic-gate 						attrsize = 8 + (int)strlen(
37117c478bd9Sstevel@tonic-gate 						    &attr->attr_info[0]) + 1;
3712fa9e4066Sahrens 						error =
3713fa9e4066Sahrens 						    acl_fromtext(
3714fa9e4066Sahrens 						    &attr->attr_info[0], &aclp);
3715fa9e4066Sahrens 
3716fa9e4066Sahrens 						if (error != 0) {
37177c478bd9Sstevel@tonic-gate 							(void) fprintf(stderr,
37187c478bd9Sstevel@tonic-gate 							    gettext(
37197c478bd9Sstevel@tonic-gate 							    "aclfromtext "
3720fa9e4066Sahrens 							    "failed: %s\n"),
3721fa9e4066Sahrens 							    acl_strerror(
3722fa9e4066Sahrens 							    error));
3723fa9e4066Sahrens 							bytes -= attrsize;
37247c478bd9Sstevel@tonic-gate 							break;
37257c478bd9Sstevel@tonic-gate 						}
3726fa9e4066Sahrens 						if (acl_cnt(aclp) != cnt) {
37277c478bd9Sstevel@tonic-gate 							(void) fprintf(stderr,
37287c478bd9Sstevel@tonic-gate 							    gettext(
37297c478bd9Sstevel@tonic-gate 							    "aclcnt error\n"));
3730fa9e4066Sahrens 							bytes -= attrsize;
37317c478bd9Sstevel@tonic-gate 							break;
37327c478bd9Sstevel@tonic-gate 						}
37337c478bd9Sstevel@tonic-gate 						bytes -= attrsize;
37347c478bd9Sstevel@tonic-gate 						break;
37357c478bd9Sstevel@tonic-gate 
373645916cd2Sjpk 					/* Trusted Extensions */
373745916cd2Sjpk 
373845916cd2Sjpk 					case DIR_TYPE:
373945916cd2Sjpk 					case LBL_TYPE:
374045916cd2Sjpk 					case APRIV_TYPE:
374145916cd2Sjpk 					case FPRIV_TYPE:
374245916cd2Sjpk 					case COMP_TYPE:
374345916cd2Sjpk 					case LK_COMP_TYPE:
374445916cd2Sjpk 					case ATTR_FLAG_TYPE:
374545916cd2Sjpk 						attrsize =
374645916cd2Sjpk 						    sizeof (struct sec_attr) +
374745916cd2Sjpk 						    strlen(&attr->attr_info[0]);
374845916cd2Sjpk 						bytes -= attrsize;
374945916cd2Sjpk 						if (Tflag)
375045916cd2Sjpk 							extract_attr(&namep,
375145916cd2Sjpk 							    attr);
375245916cd2Sjpk 						break;
37537c478bd9Sstevel@tonic-gate 
37547c478bd9Sstevel@tonic-gate 					default:
37557c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(
37567c478bd9Sstevel@tonic-gate 						    "unrecognized attr"
37577c478bd9Sstevel@tonic-gate 						    " type\n"));
37587c478bd9Sstevel@tonic-gate 						bytes = (off_t)0;
37597c478bd9Sstevel@tonic-gate 						break;
37607c478bd9Sstevel@tonic-gate 					}
37617c478bd9Sstevel@tonic-gate 
37627c478bd9Sstevel@tonic-gate 					/* next attributes */
37637c478bd9Sstevel@tonic-gate 					tp += attrsize;
37647c478bd9Sstevel@tonic-gate 				} while (bytes != 0);
37657c478bd9Sstevel@tonic-gate 				free(secp);
3766da6c28aaSamw 			} else {
37677c478bd9Sstevel@tonic-gate 				passtape();
3768da6c28aaSamw 			}
37697c478bd9Sstevel@tonic-gate 		} /* acl */
37707c478bd9Sstevel@tonic-gate 
37717c478bd9Sstevel@tonic-gate 	} /* for */
37727c478bd9Sstevel@tonic-gate 
37737c478bd9Sstevel@tonic-gate 	/*
37747c478bd9Sstevel@tonic-gate 	 *  Ensure that all the directories still on the directory stack
37757c478bd9Sstevel@tonic-gate 	 *  get their modification times set correctly by flushing the
37767c478bd9Sstevel@tonic-gate 	 *  stack.
37777c478bd9Sstevel@tonic-gate 	 */
37787c478bd9Sstevel@tonic-gate 
37797c478bd9Sstevel@tonic-gate 	doDirTimes(NULL, time_zero);
37807c478bd9Sstevel@tonic-gate 
3781da6c28aaSamw #if defined(O_XATTR)
3782da6c28aaSamw 		if (xattrp != NULL) {
3783da6c28aaSamw 			free(xattrhead);
3784da6c28aaSamw 			xattrp = NULL;
3785da6c28aaSamw 			xattr_linkp = NULL;
3786da6c28aaSamw 			xattrhead = NULL;
3787da6c28aaSamw 		}
3788da6c28aaSamw #endif
3789da6c28aaSamw 
37907c478bd9Sstevel@tonic-gate 	/*
37917c478bd9Sstevel@tonic-gate 	 * Check if the number of files extracted is different from the
37927c478bd9Sstevel@tonic-gate 	 * number of files listed on the command line
37937c478bd9Sstevel@tonic-gate 	 */
37947c478bd9Sstevel@tonic-gate 	if (fcnt > xcnt) {
37957c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
37967c478bd9Sstevel@tonic-gate 		    gettext("tar: %d file(s) not extracted\n"),
37977c478bd9Sstevel@tonic-gate 		    fcnt-xcnt);
37987c478bd9Sstevel@tonic-gate 		Errflg = 1;
37997c478bd9Sstevel@tonic-gate 	}
38007c478bd9Sstevel@tonic-gate }
38017c478bd9Sstevel@tonic-gate 
38027c478bd9Sstevel@tonic-gate /*
38037c478bd9Sstevel@tonic-gate  *	xblocks		extract file/extent from tape to output file
38047c478bd9Sstevel@tonic-gate  *
3805ced83f9bSceastha  *	xblocks(issysattr, bytes, ofile);
3806ced83f9bSceastha  *
3807ced83f9bSceastha  *	issysattr			flag set if the files being extracted
3808ced83f9bSceastha  *					is an extended system attribute file.
3809ced83f9bSceastha  *	unsigned long long bytes	size of extent or file to be extracted
3810ced83f9bSceastha  *	ofile				output file
38117c478bd9Sstevel@tonic-gate  *
38127c478bd9Sstevel@tonic-gate  *	called by doxtract() and xsfile()
38137c478bd9Sstevel@tonic-gate  */
38147c478bd9Sstevel@tonic-gate 
3815da6c28aaSamw static int
xblocks(int issysattr,off_t bytes,int ofile)3816da6c28aaSamw xblocks(int issysattr, off_t bytes, int ofile)
38177c478bd9Sstevel@tonic-gate {
3818ced83f9bSceastha 	char *buf;
38197c478bd9Sstevel@tonic-gate 	char tempname[NAMSIZ+1];
3820ced83f9bSceastha 	size_t maxwrite;
3821ced83f9bSceastha 	size_t bytesread;
3822ced83f9bSceastha 	size_t piosize;		/* preferred I/O size */
3823ced83f9bSceastha 	struct stat tsbuf;
38247c478bd9Sstevel@tonic-gate 
3825ced83f9bSceastha 	/* Don't need to do anything if this is a zero size file */
3826ced83f9bSceastha 	if (bytes <= 0) {
3827ced83f9bSceastha 		return (0);
3828ced83f9bSceastha 	}
3829ced83f9bSceastha 
3830ced83f9bSceastha 	/*
3831ced83f9bSceastha 	 * To figure out the size of the buffer used to accumulate data
3832ced83f9bSceastha 	 * from readtape() and to write to the file, we need to determine
3833ced83f9bSceastha 	 * the largest chunk of data to be written to the file at one time.
3834ced83f9bSceastha 	 * This is determined based on the smallest of the following two
3835ced83f9bSceastha 	 * things:
3836ced83f9bSceastha 	 *	1) The size of the archived file.
3837ced83f9bSceastha 	 *	2) The preferred I/O size of the file.
3838ced83f9bSceastha 	 */
3839ced83f9bSceastha 	if (issysattr || (bytes <= TBLOCK)) {
3840ced83f9bSceastha 		/*
3841ced83f9bSceastha 		 * Writes to system attribute files must be
3842ced83f9bSceastha 		 * performed in one operation.
3843ced83f9bSceastha 		 */
3844ced83f9bSceastha 		maxwrite = bytes;
3845ced83f9bSceastha 	} else {
3846ced83f9bSceastha 		/*
3847ced83f9bSceastha 		 * fstat() the file to get the preferred I/O size.
3848ced83f9bSceastha 		 * If it fails, then resort back to just writing
3849ced83f9bSceastha 		 * one block at a time.
3850ced83f9bSceastha 		 */
3851ced83f9bSceastha 		if (fstat(ofile, &tsbuf) == 0) {
3852ced83f9bSceastha 			piosize = tsbuf.st_blksize;
3853ced83f9bSceastha 		} else {
3854ced83f9bSceastha 			piosize = TBLOCK;
3855ced83f9bSceastha 		}
3856ced83f9bSceastha 		maxwrite = min(bytes, piosize);
3857ced83f9bSceastha 	}
3858ced83f9bSceastha 
3859ced83f9bSceastha 	/*
3860ced83f9bSceastha 	 * The buffer used to accumulate the data for the write operation
3861ced83f9bSceastha 	 * needs to be the maximum number of bytes to be written rounded up
3862ced83f9bSceastha 	 * to the nearest TBLOCK since readtape reads one block at a time.
3863ced83f9bSceastha 	 */
3864ced83f9bSceastha 	if ((buf = malloc(TBLOCKS(maxwrite) * TBLOCK)) == NULL) {
3865ced83f9bSceastha 		fatal(gettext("cannot allocate buffer"));
3866ced83f9bSceastha 	}
3867ced83f9bSceastha 
3868ced83f9bSceastha 	while (bytes > 0) {
3869ced83f9bSceastha 
3870ced83f9bSceastha 		/*
3871ced83f9bSceastha 		 * readtape() obtains one block (TBLOCK) of data at a time.
3872ced83f9bSceastha 		 * Accumulate as many blocks of data in buf as we can write
3873ced83f9bSceastha 		 * in one operation.
3874ced83f9bSceastha 		 */
3875ced83f9bSceastha 		for (bytesread = 0; bytesread < maxwrite; bytesread += TBLOCK) {
3876ced83f9bSceastha 			readtape(buf + bytesread);
3877ced83f9bSceastha 		}
3878ced83f9bSceastha 
3879ced83f9bSceastha 		if (write(ofile, buf, maxwrite) < 0) {
3880da6c28aaSamw 			int saveerrno = errno;
3881da6c28aaSamw 
38827c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_PATH)
3883ced83f9bSceastha 				(void) strlcpy(tempname, Xtarhdr.x_path,
3884ced83f9bSceastha 				    sizeof (tempname));
38857c478bd9Sstevel@tonic-gate 			else
38867c478bd9Sstevel@tonic-gate 				(void) sprintf(tempname, "%.*s", NAMSIZ,
38877c478bd9Sstevel@tonic-gate 				    dblock.dbuf.name);
3888da6c28aaSamw 			/*
3889da6c28aaSamw 			 * If the extended system attribute being extracted
3890da6c28aaSamw 			 * contains attributes that the user needs privileges
3891da6c28aaSamw 			 * for, then just display a warning message, skip
3892da6c28aaSamw 			 * the extraction of this file, and return.
3893da6c28aaSamw 			 */
3894da6c28aaSamw 			if ((saveerrno == EPERM) && issysattr) {
38957c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
3896da6c28aaSamw 				    "tar: unable to extract system attribute "
3897da6c28aaSamw 				    "%s: insufficient privileges\n"), tempname);
3898da6c28aaSamw 				Errflg = 1;
3899ced83f9bSceastha 				(void) free(buf);
3900da6c28aaSamw 				return (1);
3901da6c28aaSamw 			} else {
3902da6c28aaSamw 				(void) fprintf(stderr, gettext(
3903da6c28aaSamw 				    "tar: %s: HELP - extract write error\n"),
3904da6c28aaSamw 				    tempname);
39057c478bd9Sstevel@tonic-gate 				done(2);
39067c478bd9Sstevel@tonic-gate 			}
3907da6c28aaSamw 		}
3908ced83f9bSceastha 		bytes -= maxwrite;
3909ced83f9bSceastha 
3910ced83f9bSceastha 		/*
3911ced83f9bSceastha 		 * If we've reached this point and there is still data
3912ced83f9bSceastha 		 * to be written, maxwrite had to have been determined
3913ced83f9bSceastha 		 * by the preferred I/O size.  If the number of bytes
3914ced83f9bSceastha 		 * left to write is smaller than the preferred I/O size,
3915ced83f9bSceastha 		 * then we're about to do our final write to the file, so
3916ced83f9bSceastha 		 * just set maxwrite to the number of bytes left to write.
3917ced83f9bSceastha 		 */
3918ced83f9bSceastha 		if ((bytes > 0) && (bytes < maxwrite)) {
3919ced83f9bSceastha 			maxwrite = bytes;
39207c478bd9Sstevel@tonic-gate 		}
3921ced83f9bSceastha 	}
3922ced83f9bSceastha 	free(buf);
3923da6c28aaSamw 
3924da6c28aaSamw 	return (0);
39257c478bd9Sstevel@tonic-gate }
39267c478bd9Sstevel@tonic-gate 
39277c478bd9Sstevel@tonic-gate /*
39287c478bd9Sstevel@tonic-gate  * 	xsfile	extract split file
39297c478bd9Sstevel@tonic-gate  *
39307c478bd9Sstevel@tonic-gate  *	xsfile(ofd);	ofd = output file descriptor
39317c478bd9Sstevel@tonic-gate  *
39327c478bd9Sstevel@tonic-gate  *	file extracted and put in ofd via xblocks()
39337c478bd9Sstevel@tonic-gate  *
39347c478bd9Sstevel@tonic-gate  *	NOTE:  only called by doxtract() to extract one large file
39357c478bd9Sstevel@tonic-gate  */
39367c478bd9Sstevel@tonic-gate 
39377c478bd9Sstevel@tonic-gate static	union	hblock	savedblock;	/* to ensure same file across volumes */
39387c478bd9Sstevel@tonic-gate 
3939da6c28aaSamw static int
xsfile(int issysattr,int ofd)3940da6c28aaSamw xsfile(int issysattr, int ofd)
39417c478bd9Sstevel@tonic-gate {
39427c478bd9Sstevel@tonic-gate 	int i, c;
3943da6c28aaSamw 	int sysattrerr = 0;
39447c478bd9Sstevel@tonic-gate 	char name[PATH_MAX+1];	/* holds name for diagnostics */
39457c478bd9Sstevel@tonic-gate 	int extents, totalext;
39467c478bd9Sstevel@tonic-gate 	off_t bytes, totalbytes;
39477c478bd9Sstevel@tonic-gate 
39487c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_PATH)
39497c478bd9Sstevel@tonic-gate 		(void) strcpy(name, Xtarhdr.x_path);
39507c478bd9Sstevel@tonic-gate 	else
39517c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "%.*s", NAMSIZ, dblock.dbuf.name);
39527c478bd9Sstevel@tonic-gate 
39537c478bd9Sstevel@tonic-gate 	totalbytes = (off_t)0;		/* in case we read in half the file */
39547c478bd9Sstevel@tonic-gate 	totalext = 0;		/* these keep count */
39557c478bd9Sstevel@tonic-gate 
39567c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
39577c478bd9Sstevel@tonic-gate 	    "tar: %s split across %d volumes\n"), name, extotal);
39587c478bd9Sstevel@tonic-gate 
39597c478bd9Sstevel@tonic-gate 	/* make sure we do extractions in order */
39607c478bd9Sstevel@tonic-gate 	if (extno != 1) {	/* starting in middle of file? */
39617c478bd9Sstevel@tonic-gate 		(void) printf(gettext(
39627c478bd9Sstevel@tonic-gate 		    "tar: first extent read is not #1\n"
39633d63ea05Sas145665 		    "OK to read file beginning with extent #%d (%s/%s) ? "),
39643d63ea05Sas145665 		    extno, yesstr, nostr);
39653d63ea05Sas145665 		if (yes() == 0) {
39667c478bd9Sstevel@tonic-gate canit:
39677c478bd9Sstevel@tonic-gate 			passtape();
39687c478bd9Sstevel@tonic-gate 			if (close(ofd) != 0)
39697c478bd9Sstevel@tonic-gate 				vperror(2, gettext("close error"));
3970da6c28aaSamw 			if (sysattrerr) {
3971da6c28aaSamw 				return (1);
3972da6c28aaSamw 			} else {
3973da6c28aaSamw 				return (0);
3974da6c28aaSamw 			}
39757c478bd9Sstevel@tonic-gate 		}
39767c478bd9Sstevel@tonic-gate 	}
39777c478bd9Sstevel@tonic-gate 	extents = extotal;
39787c478bd9Sstevel@tonic-gate 	i = extno;
39797c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
39807c478bd9Sstevel@tonic-gate 	while (1) {
39817c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_SIZE) {
39827c478bd9Sstevel@tonic-gate 			bytes = extsize;
39837c478bd9Sstevel@tonic-gate 		} else {
39847c478bd9Sstevel@tonic-gate 			bytes = stbuf.st_size;
39857c478bd9Sstevel@tonic-gate 		}
39867c478bd9Sstevel@tonic-gate 
39877c478bd9Sstevel@tonic-gate 		if (vflag)
39888e4a71aeSRich Burridge 			(void) fprintf(vfile, "+++ x %s [%s #%d], %"
39898e4a71aeSRich Burridge 			    FMT_off_t " %s, %ldK\n",
39908e4a71aeSRich Burridge 			    name, gettext("extent"), extno,
39918e4a71aeSRich Burridge 			    bytes, gettext("bytes"),
39927c478bd9Sstevel@tonic-gate 			    (long)K(TBLOCKS(bytes)));
3993da6c28aaSamw 		if (xblocks(issysattr, bytes, ofd) != 0) {
3994da6c28aaSamw 			sysattrerr = 1;
3995da6c28aaSamw 			goto canit;
3996da6c28aaSamw 		}
39977c478bd9Sstevel@tonic-gate 
39987c478bd9Sstevel@tonic-gate 		totalbytes += bytes;
39997c478bd9Sstevel@tonic-gate 		totalext++;
40007c478bd9Sstevel@tonic-gate 		if (++i > extents)
40017c478bd9Sstevel@tonic-gate 			break;
40027c478bd9Sstevel@tonic-gate 
40037c478bd9Sstevel@tonic-gate 		/* get next volume and verify it's the right one */
40047c478bd9Sstevel@tonic-gate 		copy(&savedblock, &dblock);
40057c478bd9Sstevel@tonic-gate tryagain:
40067c478bd9Sstevel@tonic-gate 		newvol();
40077c478bd9Sstevel@tonic-gate 		xhdr_flgs = 0;
40087c478bd9Sstevel@tonic-gate 		getdir();
40097c478bd9Sstevel@tonic-gate 		if (Xhdrflag > 0)
40107c478bd9Sstevel@tonic-gate 			(void) get_xdata();	/* Get x-header & regular hdr */
4011123523f8Sas158974 		if ((dblock.dbuf.typeflag != 'A') && (xhdr_flgs != 0)) {
4012123523f8Sas158974 			load_info_from_xtarhdr(xhdr_flgs, &Xtarhdr);
4013123523f8Sas158974 			xhdr_flgs |= _X_XHDR;
4014123523f8Sas158974 		}
40157c478bd9Sstevel@tonic-gate 		if (endtape()) {	/* seemingly empty volume */
40167c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
40177c478bd9Sstevel@tonic-gate 			    "tar: first record is null\n"));
40187c478bd9Sstevel@tonic-gate asknicely:
40197c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
40207c478bd9Sstevel@tonic-gate 			    "tar: need volume with extent #%d of %s\n"),
40217c478bd9Sstevel@tonic-gate 			    i, name);
40227c478bd9Sstevel@tonic-gate 			goto tryagain;
40237c478bd9Sstevel@tonic-gate 		}
40247c478bd9Sstevel@tonic-gate 		if (notsame()) {
40257c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
40267c478bd9Sstevel@tonic-gate 			    "tar: first file on that volume is not "
40277c478bd9Sstevel@tonic-gate 			    "the same file\n"));
40287c478bd9Sstevel@tonic-gate 			goto asknicely;
40297c478bd9Sstevel@tonic-gate 		}
40307c478bd9Sstevel@tonic-gate 		if (i != extno) {
40317c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4032eace40a5Sceastha 			    "tar: extent #%d received out of order\ntar: "
4033eace40a5Sceastha 			    "should be #%d\n"), extno, i);
40347c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
40357c478bd9Sstevel@tonic-gate 			    "Ignore error, Abort this file, or "
40367c478bd9Sstevel@tonic-gate 			    "load New volume (i/a/n) ? "));
40377c478bd9Sstevel@tonic-gate 			c = response();
40387c478bd9Sstevel@tonic-gate 			if (c == 'a')
40397c478bd9Sstevel@tonic-gate 				goto canit;
40407c478bd9Sstevel@tonic-gate 			if (c != 'i')		/* default to new volume */
40417c478bd9Sstevel@tonic-gate 				goto asknicely;
40427c478bd9Sstevel@tonic-gate 			i = extno;		/* okay, start from there */
40437c478bd9Sstevel@tonic-gate 		}
40447c478bd9Sstevel@tonic-gate 	}
40457c478bd9Sstevel@tonic-gate 	if (vflag)
40467c478bd9Sstevel@tonic-gate 		(void) fprintf(vfile, gettext(
40477c478bd9Sstevel@tonic-gate 		    "x %s (in %d extents), %" FMT_off_t " bytes, %ldK\n"),
40487c478bd9Sstevel@tonic-gate 		    name, totalext, totalbytes, (long)K(TBLOCKS(totalbytes)));
4049da6c28aaSamw 
4050da6c28aaSamw 	return (0);
40517c478bd9Sstevel@tonic-gate }
40527c478bd9Sstevel@tonic-gate 
40537c478bd9Sstevel@tonic-gate 
40547c478bd9Sstevel@tonic-gate /*
40557c478bd9Sstevel@tonic-gate  *	notsame()	check if extract file extent is invalid
40567c478bd9Sstevel@tonic-gate  *
40577c478bd9Sstevel@tonic-gate  *	returns true if anything differs between savedblock and dblock
40587c478bd9Sstevel@tonic-gate  *	except extno (extent number), checksum, or size (extent size).
40597c478bd9Sstevel@tonic-gate  *	Determines if this header belongs to the same file as the one we're
40607c478bd9Sstevel@tonic-gate  *	extracting.
40617c478bd9Sstevel@tonic-gate  *
40627c478bd9Sstevel@tonic-gate  *	NOTE:	though rather bulky, it is only called once per file
40637c478bd9Sstevel@tonic-gate  *		extension, and it can withstand changes in the definition
40647c478bd9Sstevel@tonic-gate  *		of the header structure.
40657c478bd9Sstevel@tonic-gate  *
40667c478bd9Sstevel@tonic-gate  *	WARNING:	this routine is local to xsfile() above
40677c478bd9Sstevel@tonic-gate  */
40687c478bd9Sstevel@tonic-gate 
40697c478bd9Sstevel@tonic-gate static int
notsame(void)40707c478bd9Sstevel@tonic-gate notsame(void)
40717c478bd9Sstevel@tonic-gate {
40727c478bd9Sstevel@tonic-gate 	return (
40737c478bd9Sstevel@tonic-gate 	    (strncmp(savedblock.dbuf.name, dblock.dbuf.name, NAMSIZ)) ||
40747c478bd9Sstevel@tonic-gate 	    (strcmp(savedblock.dbuf.mode, dblock.dbuf.mode)) ||
40757c478bd9Sstevel@tonic-gate 	    (strcmp(savedblock.dbuf.uid, dblock.dbuf.uid)) ||
40767c478bd9Sstevel@tonic-gate 	    (strcmp(savedblock.dbuf.gid, dblock.dbuf.gid)) ||
40777c478bd9Sstevel@tonic-gate 	    (strcmp(savedblock.dbuf.mtime, dblock.dbuf.mtime)) ||
40787c478bd9Sstevel@tonic-gate 	    (savedblock.dbuf.typeflag != dblock.dbuf.typeflag) ||
40797c478bd9Sstevel@tonic-gate 	    (strncmp(savedblock.dbuf.linkname, dblock.dbuf.linkname, NAMSIZ)) ||
40807c478bd9Sstevel@tonic-gate 	    (savedblock.dbuf.extotal != dblock.dbuf.extotal) ||
40817c478bd9Sstevel@tonic-gate 	    (strcmp(savedblock.dbuf.efsize, dblock.dbuf.efsize)));
40827c478bd9Sstevel@tonic-gate }
40837c478bd9Sstevel@tonic-gate 
40847c478bd9Sstevel@tonic-gate static void
dotable(char * argv[])40857c478bd9Sstevel@tonic-gate dotable(char *argv[])
40867c478bd9Sstevel@tonic-gate {
4087cc22b130SRich Burridge 	int tcnt = 0;			/* count # files tabled */
4088cc22b130SRich Burridge 	int fcnt = 0;			/* count # files in argv list */
40897c478bd9Sstevel@tonic-gate 	char *namep, *dirp, *comp;
40907c478bd9Sstevel@tonic-gate 	int want;
40917c478bd9Sstevel@tonic-gate 	char aclchar = ' ';			/* either blank or '+' */
40927c478bd9Sstevel@tonic-gate 	char templink[PATH_MAX+1];
4093da6c28aaSamw 	attr_data_t *attrinfo = NULL;
40947c478bd9Sstevel@tonic-gate 
40957c478bd9Sstevel@tonic-gate 	dumping = 0;
40967c478bd9Sstevel@tonic-gate 
40977c478bd9Sstevel@tonic-gate 	/* if not on magtape, maximize seek speed */
40987c478bd9Sstevel@tonic-gate 	if (NotTape && !bflag) {
40997c478bd9Sstevel@tonic-gate #if SYS_BLOCK > TBLOCK
41007c478bd9Sstevel@tonic-gate 		nblock = SYS_BLOCK / TBLOCK;
41017c478bd9Sstevel@tonic-gate #else
41027c478bd9Sstevel@tonic-gate 		nblock = 1;
41037c478bd9Sstevel@tonic-gate #endif
41047c478bd9Sstevel@tonic-gate 	}
41057c478bd9Sstevel@tonic-gate 
41067c478bd9Sstevel@tonic-gate 	for (;;) {
41077c478bd9Sstevel@tonic-gate 
41087c478bd9Sstevel@tonic-gate 		/* namep is set by wantit to point to the full name */
4109da6c28aaSamw 		if ((want = wantit(argv, &namep, &dirp, &comp, &attrinfo)) == 0)
41107c478bd9Sstevel@tonic-gate 			continue;
41117c478bd9Sstevel@tonic-gate 		if (want == -1)
41127c478bd9Sstevel@tonic-gate 			break;
41137c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag != 'A')
41147c478bd9Sstevel@tonic-gate 			++tcnt;
41157c478bd9Sstevel@tonic-gate 
41163a1dab68SRich Burridge 		if (Fflag) {
41173a1dab68SRich Burridge 			if (checkf(namep, is_directory(namep), Fflag) == 0) {
41183a1dab68SRich Burridge 				passtape();
41193a1dab68SRich Burridge 				continue;
41203a1dab68SRich Burridge 			}
41213a1dab68SRich Burridge 		}
41227c478bd9Sstevel@tonic-gate 		/*
41237c478bd9Sstevel@tonic-gate 		 * ACL support:
41247c478bd9Sstevel@tonic-gate 		 * aclchar is introduced to indicate if there are
41257c478bd9Sstevel@tonic-gate 		 * acl entries. longt() now takes one extra argument.
41267c478bd9Sstevel@tonic-gate 		 */
41277c478bd9Sstevel@tonic-gate 		if (vflag) {
41287c478bd9Sstevel@tonic-gate 			if (dblock.dbuf.typeflag == 'A') {
41297c478bd9Sstevel@tonic-gate 				aclchar = '+';
41307c478bd9Sstevel@tonic-gate 				passtape();
41317c478bd9Sstevel@tonic-gate 				continue;
41327c478bd9Sstevel@tonic-gate 			}
41337c478bd9Sstevel@tonic-gate 			longt(&stbuf, aclchar);
41347c478bd9Sstevel@tonic-gate 			aclchar = ' ';
41357c478bd9Sstevel@tonic-gate 		}
41367c478bd9Sstevel@tonic-gate 
41377c478bd9Sstevel@tonic-gate 
41387c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
4139da6c28aaSamw 		if (xattrp != NULL) {
4140da6c28aaSamw 			int	issysattr;
4141da6c28aaSamw 			char	*bn = basename(attrinfo->attr_path);
41427c478bd9Sstevel@tonic-gate 
4143da6c28aaSamw 			/*
4144da6c28aaSamw 			 * We could use sysattr_type() to test whether or not
4145da6c28aaSamw 			 * the attribute we are processing is really an
4146da6c28aaSamw 			 * extended system attribute, which as of this writing
4147da6c28aaSamw 			 * just does a strcmp(), however, sysattr_type() may
4148da6c28aaSamw 			 * be changed to issue a pathconf() call instead, which
4149da6c28aaSamw 			 * would require being changed into the parent attribute
4150da6c28aaSamw 			 * directory.  So instead, just do simple string
4151da6c28aaSamw 			 * comparisons to see if we are processing an extended
4152da6c28aaSamw 			 * system attribute.
4153da6c28aaSamw 			 */
4154da6c28aaSamw 			issysattr = is_sysattr(bn);
4155da6c28aaSamw 
4156da6c28aaSamw 			(void) printf(gettext("%s %sattribute %s"),
4157da6c28aaSamw 			    xattrp->h_names,
4158da6c28aaSamw 			    issysattr ? gettext("system ") : "",
4159da6c28aaSamw 			    attrinfo->attr_path);
41607c478bd9Sstevel@tonic-gate 		} else {
41617c478bd9Sstevel@tonic-gate 			(void) printf("%s", namep);
41627c478bd9Sstevel@tonic-gate 		}
41637c478bd9Sstevel@tonic-gate #else
41647c478bd9Sstevel@tonic-gate 			(void) printf("%s", namep);
41657c478bd9Sstevel@tonic-gate #endif
41667c478bd9Sstevel@tonic-gate 
41677c478bd9Sstevel@tonic-gate 		if (extno != 0) {
41687c478bd9Sstevel@tonic-gate 			if (vflag) {
41697c478bd9Sstevel@tonic-gate 				/* keep the '\n' for backwards compatibility */
41707c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext(
41717c478bd9Sstevel@tonic-gate 				    "\n [extent #%d of %d]"), extno, extotal);
41727c478bd9Sstevel@tonic-gate 			} else {
41737c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext(
41747c478bd9Sstevel@tonic-gate 				    " [extent #%d of %d]"), extno, extotal);
41757c478bd9Sstevel@tonic-gate 			}
41767c478bd9Sstevel@tonic-gate 		}
41777c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_LINKPATH) {
41787c478bd9Sstevel@tonic-gate 			(void) strcpy(templink, Xtarhdr.x_linkpath);
41797c478bd9Sstevel@tonic-gate 		} else {
41807c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
4181da6c28aaSamw 			if (xattrp != NULL) {
41827c478bd9Sstevel@tonic-gate 				(void) sprintf(templink,
41837c478bd9Sstevel@tonic-gate 				    "file %.*s", NAMSIZ, xattrp->h_names);
41847c478bd9Sstevel@tonic-gate 			} else {
41857c478bd9Sstevel@tonic-gate 				(void) sprintf(templink, "%.*s", NAMSIZ,
41867c478bd9Sstevel@tonic-gate 				    dblock.dbuf.linkname);
41877c478bd9Sstevel@tonic-gate 			}
41887c478bd9Sstevel@tonic-gate #else
41897c478bd9Sstevel@tonic-gate 			(void) sprintf(templink, "%.*s", NAMSIZ,
41907c478bd9Sstevel@tonic-gate 			    dblock.dbuf.linkname);
41917c478bd9Sstevel@tonic-gate #endif
41927c478bd9Sstevel@tonic-gate 			templink[NAMSIZ] = '\0';
41937c478bd9Sstevel@tonic-gate 		}
41947c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '1') {
41957c478bd9Sstevel@tonic-gate 			/*
41967c478bd9Sstevel@tonic-gate 			 * TRANSLATION_NOTE
41977c478bd9Sstevel@tonic-gate 			 *	Subject is omitted here.
41987c478bd9Sstevel@tonic-gate 			 *	Translate this as if
41997c478bd9Sstevel@tonic-gate 			 *		<subject> linked to %s
42007c478bd9Sstevel@tonic-gate 			 */
42017c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
4202da6c28aaSamw 			if (xattrp != NULL) {
42037c478bd9Sstevel@tonic-gate 				(void) printf(
42047c478bd9Sstevel@tonic-gate 				    gettext(" linked to attribute %s"),
42057c478bd9Sstevel@tonic-gate 				    xattr_linkp->h_names +
42067c478bd9Sstevel@tonic-gate 				    strlen(xattr_linkp->h_names) + 1);
42077c478bd9Sstevel@tonic-gate 			} else {
42087c478bd9Sstevel@tonic-gate 				(void) printf(
42097c478bd9Sstevel@tonic-gate 				    gettext(" linked to %s"), templink);
42107c478bd9Sstevel@tonic-gate 			}
42117c478bd9Sstevel@tonic-gate #else
42127c478bd9Sstevel@tonic-gate 				(void) printf(
42137c478bd9Sstevel@tonic-gate 				    gettext(" linked to %s"), templink);
42147c478bd9Sstevel@tonic-gate 
42157c478bd9Sstevel@tonic-gate #endif
42167c478bd9Sstevel@tonic-gate 		}
42177c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '2')
42187c478bd9Sstevel@tonic-gate 			(void) printf(gettext(
42197c478bd9Sstevel@tonic-gate 			/*
42207c478bd9Sstevel@tonic-gate 			 * TRANSLATION_NOTE
42217c478bd9Sstevel@tonic-gate 			 *	Subject is omitted here.
42227c478bd9Sstevel@tonic-gate 			 *	Translate this as if
42237c478bd9Sstevel@tonic-gate 			 *		<subject> symbolic link to %s
42247c478bd9Sstevel@tonic-gate 			 */
42257c478bd9Sstevel@tonic-gate 			" symbolic link to %s"), templink);
42267c478bd9Sstevel@tonic-gate 		(void) printf("\n");
42277c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
4228da6c28aaSamw 		if (xattrp != NULL) {
42297c478bd9Sstevel@tonic-gate 			free(xattrhead);
42307c478bd9Sstevel@tonic-gate 			xattrp = NULL;
42317c478bd9Sstevel@tonic-gate 			xattrhead = NULL;
42327c478bd9Sstevel@tonic-gate 		}
42337c478bd9Sstevel@tonic-gate #endif
42347c478bd9Sstevel@tonic-gate 		passtape();
42357c478bd9Sstevel@tonic-gate 	}
42367c478bd9Sstevel@tonic-gate 	/*
42377c478bd9Sstevel@tonic-gate 	 * Check if the number of files tabled is different from the
42387c478bd9Sstevel@tonic-gate 	 * number of files listed on the command line
42397c478bd9Sstevel@tonic-gate 	 */
42407c478bd9Sstevel@tonic-gate 	if (fcnt > tcnt) {
42417c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
42427c478bd9Sstevel@tonic-gate 		    "tar: %d file(s) not found\n"), fcnt-tcnt);
42437c478bd9Sstevel@tonic-gate 		Errflg = 1;
42447c478bd9Sstevel@tonic-gate 	}
42457c478bd9Sstevel@tonic-gate }
42467c478bd9Sstevel@tonic-gate 
42477c478bd9Sstevel@tonic-gate static void
putempty(blkcnt_t n)42487c478bd9Sstevel@tonic-gate putempty(blkcnt_t n)
42497c478bd9Sstevel@tonic-gate {
42507c478bd9Sstevel@tonic-gate 	char buf[TBLOCK];
42517c478bd9Sstevel@tonic-gate 	char *cp;
42527c478bd9Sstevel@tonic-gate 
42537c478bd9Sstevel@tonic-gate 	for (cp = buf; cp < &buf[TBLOCK]; )
42547c478bd9Sstevel@tonic-gate 		*cp++ = '\0';
42557c478bd9Sstevel@tonic-gate 	while (n-- > 0)
42567c478bd9Sstevel@tonic-gate 		(void) writetbuf(buf, 1);
42577c478bd9Sstevel@tonic-gate }
42587c478bd9Sstevel@tonic-gate 
42597c478bd9Sstevel@tonic-gate static	ushort_t	Ftype = S_IFMT;
42607c478bd9Sstevel@tonic-gate 
42617c478bd9Sstevel@tonic-gate static	void
verbose(struct stat * st,char aclchar)42627c478bd9Sstevel@tonic-gate verbose(struct stat *st, char aclchar)
42637c478bd9Sstevel@tonic-gate {
42647c478bd9Sstevel@tonic-gate 	int i, j, temp;
42657c478bd9Sstevel@tonic-gate 	mode_t mode;
42667c478bd9Sstevel@tonic-gate 	char modestr[12];
42677c478bd9Sstevel@tonic-gate 
42687c478bd9Sstevel@tonic-gate 	for (i = 0; i < 11; i++)
42697c478bd9Sstevel@tonic-gate 		modestr[i] = '-';
42707c478bd9Sstevel@tonic-gate 	modestr[i] = '\0';
42717c478bd9Sstevel@tonic-gate 
42727c478bd9Sstevel@tonic-gate 	/* a '+' sign is printed if there is ACL */
42737c478bd9Sstevel@tonic-gate 	modestr[i-1] = aclchar;
42747c478bd9Sstevel@tonic-gate 
42757c478bd9Sstevel@tonic-gate 	mode = st->st_mode;
42767c478bd9Sstevel@tonic-gate 	for (i = 0; i < 3; i++) {
42777c478bd9Sstevel@tonic-gate 		temp = (mode >> (6 - (i * 3)));
42787c478bd9Sstevel@tonic-gate 		j = (i * 3) + 1;
42797c478bd9Sstevel@tonic-gate 		if (S_IROTH & temp)
42807c478bd9Sstevel@tonic-gate 			modestr[j] = 'r';
42817c478bd9Sstevel@tonic-gate 		if (S_IWOTH & temp)
42827c478bd9Sstevel@tonic-gate 			modestr[j + 1] = 'w';
42837c478bd9Sstevel@tonic-gate 		if (S_IXOTH & temp)
42847c478bd9Sstevel@tonic-gate 			modestr[j + 2] = 'x';
42857c478bd9Sstevel@tonic-gate 	}
42867c478bd9Sstevel@tonic-gate 	temp = st->st_mode & Ftype;
42877c478bd9Sstevel@tonic-gate 	switch (temp) {
42887c478bd9Sstevel@tonic-gate 	case (S_IFIFO):
42897c478bd9Sstevel@tonic-gate 		modestr[0] = 'p';
42907c478bd9Sstevel@tonic-gate 		break;
42917c478bd9Sstevel@tonic-gate 	case (S_IFCHR):
42927c478bd9Sstevel@tonic-gate 		modestr[0] = 'c';
42937c478bd9Sstevel@tonic-gate 		break;
42947c478bd9Sstevel@tonic-gate 	case (S_IFDIR):
42957c478bd9Sstevel@tonic-gate 		modestr[0] = 'd';
42967c478bd9Sstevel@tonic-gate 		break;
42977c478bd9Sstevel@tonic-gate 	case (S_IFBLK):
42987c478bd9Sstevel@tonic-gate 		modestr[0] = 'b';
42997c478bd9Sstevel@tonic-gate 		break;
43007c478bd9Sstevel@tonic-gate 	case (S_IFREG): /* was initialized to '-' */
43017c478bd9Sstevel@tonic-gate 		break;
43027c478bd9Sstevel@tonic-gate 	case (S_IFLNK):
43037c478bd9Sstevel@tonic-gate 		modestr[0] = 'l';
43047c478bd9Sstevel@tonic-gate 		break;
43057c478bd9Sstevel@tonic-gate 	default:
43067c478bd9Sstevel@tonic-gate 		/* This field may be zero in old archives. */
43077c478bd9Sstevel@tonic-gate 		if (is_posix && dblock.dbuf.typeflag != '1') {
43087c478bd9Sstevel@tonic-gate 			/*
43097c478bd9Sstevel@tonic-gate 			 * For POSIX compliant archives, the mode field
43107c478bd9Sstevel@tonic-gate 			 * consists of 12 bits, ie:  the file type bits
43117c478bd9Sstevel@tonic-gate 			 * are not stored in dblock.dbuf.mode.
43127c478bd9Sstevel@tonic-gate 			 * For files other than hard links, getdir() sets
43137c478bd9Sstevel@tonic-gate 			 * the file type bits in the st_mode field of the
43147c478bd9Sstevel@tonic-gate 			 * stat structure based upon dblock.dbuf.typeflag.
43157c478bd9Sstevel@tonic-gate 			 */
43167c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
43177c478bd9Sstevel@tonic-gate 			    "tar: impossible file type"));
43187c478bd9Sstevel@tonic-gate 		}
43197c478bd9Sstevel@tonic-gate 	}
43207c478bd9Sstevel@tonic-gate 
43217c478bd9Sstevel@tonic-gate 	if ((S_ISUID & Gen.g_mode) == S_ISUID)
43227c478bd9Sstevel@tonic-gate 		modestr[3] = 's';
43237c478bd9Sstevel@tonic-gate 	if ((S_ISVTX & Gen.g_mode) == S_ISVTX)
43247c478bd9Sstevel@tonic-gate 		modestr[9] = 't';
43257c478bd9Sstevel@tonic-gate 	if ((S_ISGID & Gen.g_mode) == S_ISGID && modestr[6] == 'x')
43267c478bd9Sstevel@tonic-gate 		modestr[6] = 's';
43277c478bd9Sstevel@tonic-gate 	else if ((S_ENFMT & Gen.g_mode) == S_ENFMT && modestr[6] != 'x')
43287c478bd9Sstevel@tonic-gate 		modestr[6] = 'l';
43297c478bd9Sstevel@tonic-gate 	(void) fprintf(vfile, "%s", modestr);
43307c478bd9Sstevel@tonic-gate }
43317c478bd9Sstevel@tonic-gate 
43327c478bd9Sstevel@tonic-gate static void
longt(struct stat * st,char aclchar)43337c478bd9Sstevel@tonic-gate longt(struct stat *st, char aclchar)
43347c478bd9Sstevel@tonic-gate {
43357c478bd9Sstevel@tonic-gate 	char fileDate[30];
43367c478bd9Sstevel@tonic-gate 	struct tm *tm;
43377c478bd9Sstevel@tonic-gate 
43387c478bd9Sstevel@tonic-gate 	verbose(st, aclchar);
43397c478bd9Sstevel@tonic-gate 	(void) fprintf(vfile, "%3ld/%-3ld", st->st_uid, st->st_gid);
43407c478bd9Sstevel@tonic-gate 
43417c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.typeflag == '2') {
43427c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_LINKPATH)
43437c478bd9Sstevel@tonic-gate 			st->st_size = (off_t)strlen(Xtarhdr.x_linkpath);
43447c478bd9Sstevel@tonic-gate 		else
43457c478bd9Sstevel@tonic-gate 			st->st_size = (off_t)(memchr(dblock.dbuf.linkname,
43467c478bd9Sstevel@tonic-gate 			    '\0', NAMSIZ) ?
43477c478bd9Sstevel@tonic-gate 			    (strlen(dblock.dbuf.linkname)) : (NAMSIZ));
43487c478bd9Sstevel@tonic-gate 	}
43497c478bd9Sstevel@tonic-gate 	(void) fprintf(vfile, " %6" FMT_off_t, st->st_size);
43507c478bd9Sstevel@tonic-gate 
43517c478bd9Sstevel@tonic-gate 	tm = localtime(&(st->st_mtime));
43527c478bd9Sstevel@tonic-gate 	(void) strftime(fileDate, sizeof (fileDate),
43537c478bd9Sstevel@tonic-gate 	    dcgettext((const char *)0, "%b %e %R %Y", LC_TIME), tm);
43547c478bd9Sstevel@tonic-gate 	(void) fprintf(vfile, " %s ", fileDate);
43557c478bd9Sstevel@tonic-gate }
43567c478bd9Sstevel@tonic-gate 
43577c478bd9Sstevel@tonic-gate 
43587c478bd9Sstevel@tonic-gate /*
43597c478bd9Sstevel@tonic-gate  *  checkdir - Attempt to ensure that the path represented in name
43607c478bd9Sstevel@tonic-gate  *             exists, and return 1 if this is true and name itself is a
43617c478bd9Sstevel@tonic-gate  *             directory.
43627c478bd9Sstevel@tonic-gate  *             Return 0 if this path cannot be created or if name is not
43637c478bd9Sstevel@tonic-gate  *             a directory.
43647c478bd9Sstevel@tonic-gate  */
43657c478bd9Sstevel@tonic-gate 
43667c478bd9Sstevel@tonic-gate static int
checkdir(char * name)43677c478bd9Sstevel@tonic-gate checkdir(char *name)
43687c478bd9Sstevel@tonic-gate {
43697c478bd9Sstevel@tonic-gate 	char lastChar;		   /* the last character in name */
43707c478bd9Sstevel@tonic-gate 	char *cp;		   /* scratch pointer into name */
43717c478bd9Sstevel@tonic-gate 	char *firstSlash = NULL;   /* first slash in name */
43727c478bd9Sstevel@tonic-gate 	char *lastSlash = NULL;	   /* last slash in name */
43737c478bd9Sstevel@tonic-gate 	int  nameLen;		   /* length of name */
43747c478bd9Sstevel@tonic-gate 	int  trailingSlash;	   /* true if name ends in slash */
43757c478bd9Sstevel@tonic-gate 	int  leadingSlash;	   /* true if name begins with slash */
43767c478bd9Sstevel@tonic-gate 	int  markedDir;		   /* true if name denotes a directory */
43777c478bd9Sstevel@tonic-gate 	int  success;		   /* status of makeDir call */
43787c478bd9Sstevel@tonic-gate 
43797c478bd9Sstevel@tonic-gate 
43807c478bd9Sstevel@tonic-gate 	/*
43817c478bd9Sstevel@tonic-gate 	 *  Scan through the name, and locate first and last slashes.
43827c478bd9Sstevel@tonic-gate 	 */
43837c478bd9Sstevel@tonic-gate 
43847c478bd9Sstevel@tonic-gate 	for (cp = name; *cp; cp++) {
43857c478bd9Sstevel@tonic-gate 		if (*cp == '/') {
43867c478bd9Sstevel@tonic-gate 			if (! firstSlash) {
43877c478bd9Sstevel@tonic-gate 				firstSlash = cp;
43887c478bd9Sstevel@tonic-gate 			}
43897c478bd9Sstevel@tonic-gate 			lastSlash = cp;
43907c478bd9Sstevel@tonic-gate 		}
43917c478bd9Sstevel@tonic-gate 	}
43927c478bd9Sstevel@tonic-gate 
43937c478bd9Sstevel@tonic-gate 	/*
43947c478bd9Sstevel@tonic-gate 	 *  Determine what you can from the proceeds of the scan.
43957c478bd9Sstevel@tonic-gate 	 */
43967c478bd9Sstevel@tonic-gate 
43977c478bd9Sstevel@tonic-gate 	lastChar	= *(cp - 1);
43987c478bd9Sstevel@tonic-gate 	nameLen		= (int)(cp - name);
43997c478bd9Sstevel@tonic-gate 	trailingSlash	= (lastChar == '/');
44007c478bd9Sstevel@tonic-gate 	leadingSlash	= (*name == '/');
44017c478bd9Sstevel@tonic-gate 	markedDir	= (dblock.dbuf.typeflag == '5' || trailingSlash);
44027c478bd9Sstevel@tonic-gate 
44037c478bd9Sstevel@tonic-gate 	if (! lastSlash && ! markedDir) {
44047c478bd9Sstevel@tonic-gate 		/*
44057c478bd9Sstevel@tonic-gate 		 *  The named file does not have any subdrectory
44067c478bd9Sstevel@tonic-gate 		 *  structure; just bail out.
44077c478bd9Sstevel@tonic-gate 		 */
44087c478bd9Sstevel@tonic-gate 
44097c478bd9Sstevel@tonic-gate 		return (0);
44107c478bd9Sstevel@tonic-gate 	}
44117c478bd9Sstevel@tonic-gate 
44127c478bd9Sstevel@tonic-gate 	/*
44137c478bd9Sstevel@tonic-gate 	 *  Make sure that name doesn`t end with slash for the loop.
44147c478bd9Sstevel@tonic-gate 	 *  This ensures that the makeDir attempt after the loop is
44157c478bd9Sstevel@tonic-gate 	 *  meaningful.
44167c478bd9Sstevel@tonic-gate 	 */
44177c478bd9Sstevel@tonic-gate 
44187c478bd9Sstevel@tonic-gate 	if (trailingSlash) {
44197c478bd9Sstevel@tonic-gate 		name[nameLen-1] = '\0';
44207c478bd9Sstevel@tonic-gate 	}
44217c478bd9Sstevel@tonic-gate 
44227c478bd9Sstevel@tonic-gate 	/*
44237c478bd9Sstevel@tonic-gate 	 *  Make the path one component at a time.
44247c478bd9Sstevel@tonic-gate 	 */
44257c478bd9Sstevel@tonic-gate 
44267c478bd9Sstevel@tonic-gate 	for (cp = strchr(leadingSlash ? name+1 : name, '/');
44277c478bd9Sstevel@tonic-gate 	    cp;
44287c478bd9Sstevel@tonic-gate 	    cp = strchr(cp+1, '/')) {
44297c478bd9Sstevel@tonic-gate 		*cp = '\0';
44307c478bd9Sstevel@tonic-gate 		success = makeDir(name);
44317c478bd9Sstevel@tonic-gate 		*cp = '/';
44327c478bd9Sstevel@tonic-gate 
44337c478bd9Sstevel@tonic-gate 		if (!success) {
44347c478bd9Sstevel@tonic-gate 			name[nameLen-1] = lastChar;
44357c478bd9Sstevel@tonic-gate 			return (0);
44367c478bd9Sstevel@tonic-gate 		}
44377c478bd9Sstevel@tonic-gate 	}
44387c478bd9Sstevel@tonic-gate 
44397c478bd9Sstevel@tonic-gate 	/*
44407c478bd9Sstevel@tonic-gate 	 *  This makes the last component of the name, if it is a
44417c478bd9Sstevel@tonic-gate 	 *  directory.
44427c478bd9Sstevel@tonic-gate 	 */
44437c478bd9Sstevel@tonic-gate 
44447c478bd9Sstevel@tonic-gate 	if (markedDir) {
44457c478bd9Sstevel@tonic-gate 		if (! makeDir(name)) {
44467c478bd9Sstevel@tonic-gate 			name[nameLen-1] = lastChar;
44477c478bd9Sstevel@tonic-gate 			return (0);
44487c478bd9Sstevel@tonic-gate 		}
44497c478bd9Sstevel@tonic-gate 	}
44507c478bd9Sstevel@tonic-gate 
44517c478bd9Sstevel@tonic-gate 	name[nameLen-1] = (lastChar == '/') ? '\0' : lastChar;
44527c478bd9Sstevel@tonic-gate 	return (markedDir);
44537c478bd9Sstevel@tonic-gate }
44547c478bd9Sstevel@tonic-gate 
44557c478bd9Sstevel@tonic-gate /*
44567c478bd9Sstevel@tonic-gate  * resugname - Restore the user name and group name.  Search the NIS
44577c478bd9Sstevel@tonic-gate  *             before using the uid and gid.
44587c478bd9Sstevel@tonic-gate  *             (It is presumed that an archive entry cannot be
44597c478bd9Sstevel@tonic-gate  *	       simultaneously a symlink and some other type.)
44607c478bd9Sstevel@tonic-gate  */
44617c478bd9Sstevel@tonic-gate 
44627c478bd9Sstevel@tonic-gate static void
resugname(int dirfd,char * name,int symflag)44637c478bd9Sstevel@tonic-gate resugname(int dirfd, 	/* dir fd file resides in */
44647c478bd9Sstevel@tonic-gate 	char *name,	/* name of the file to be modified */
44657c478bd9Sstevel@tonic-gate 	int symflag)	/* true if file is a symbolic link */
44667c478bd9Sstevel@tonic-gate {
44677c478bd9Sstevel@tonic-gate 	uid_t duid;
44687c478bd9Sstevel@tonic-gate 	gid_t dgid;
44697c478bd9Sstevel@tonic-gate 	struct stat *sp = &stbuf;
44707c478bd9Sstevel@tonic-gate 	char	*u_g_name;
44717c478bd9Sstevel@tonic-gate 
44727c478bd9Sstevel@tonic-gate 	if (checkflag == 1) { /* Extended tar format and euid == 0 */
44737c478bd9Sstevel@tonic-gate 
44747c478bd9Sstevel@tonic-gate 		/*
44757c478bd9Sstevel@tonic-gate 		 * Try and extract the intended uid and gid from the name
44767c478bd9Sstevel@tonic-gate 		 * service before believing the uid and gid in the header.
44777c478bd9Sstevel@tonic-gate 		 *
44787c478bd9Sstevel@tonic-gate 		 * In the case where we archived a setuid or setgid file
44797c478bd9Sstevel@tonic-gate 		 * owned by someone with a large uid, then it will
44807c478bd9Sstevel@tonic-gate 		 * have made it into the archive with a uid of nobody.  If
44817c478bd9Sstevel@tonic-gate 		 * the corresponding username doesn't appear to exist, then we
44827c478bd9Sstevel@tonic-gate 		 * want to make sure it *doesn't* end up as setuid nobody!
44837c478bd9Sstevel@tonic-gate 		 *
44847c478bd9Sstevel@tonic-gate 		 * Our caller will print an error message about the fact
44857c478bd9Sstevel@tonic-gate 		 * that the restore didn't work out quite right ..
44867c478bd9Sstevel@tonic-gate 		 */
44877c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_UNAME)
44887c478bd9Sstevel@tonic-gate 			u_g_name = Xtarhdr.x_uname;
44897c478bd9Sstevel@tonic-gate 		else
44907c478bd9Sstevel@tonic-gate 			u_g_name = dblock.dbuf.uname;
44917c478bd9Sstevel@tonic-gate 		if ((duid = getuidbyname(u_g_name)) == -1) {
44927c478bd9Sstevel@tonic-gate 			if (S_ISREG(sp->st_mode) && sp->st_uid == UID_NOBODY &&
44937c478bd9Sstevel@tonic-gate 			    (sp->st_mode & S_ISUID) == S_ISUID)
44947c478bd9Sstevel@tonic-gate 				(void) chmod(name,
44957c478bd9Sstevel@tonic-gate 				    MODEMASK & sp->st_mode & ~S_ISUID);
44967c478bd9Sstevel@tonic-gate 			duid = sp->st_uid;
44977c478bd9Sstevel@tonic-gate 		}
44987c478bd9Sstevel@tonic-gate 
44997c478bd9Sstevel@tonic-gate 		/* (Ditto for gids) */
45007c478bd9Sstevel@tonic-gate 
45017c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_GNAME)
45027c478bd9Sstevel@tonic-gate 			u_g_name = Xtarhdr.x_gname;
45037c478bd9Sstevel@tonic-gate 		else
45047c478bd9Sstevel@tonic-gate 			u_g_name = dblock.dbuf.gname;
45057c478bd9Sstevel@tonic-gate 		if ((dgid = getgidbyname(u_g_name)) == -1) {
45067c478bd9Sstevel@tonic-gate 			if (S_ISREG(sp->st_mode) && sp->st_gid == GID_NOBODY &&
45077c478bd9Sstevel@tonic-gate 			    (sp->st_mode & S_ISGID) == S_ISGID)
45087c478bd9Sstevel@tonic-gate 				(void) chmod(name,
45097c478bd9Sstevel@tonic-gate 				    MODEMASK & sp->st_mode & ~S_ISGID);
45107c478bd9Sstevel@tonic-gate 			dgid = sp->st_gid;
45117c478bd9Sstevel@tonic-gate 		}
45127c478bd9Sstevel@tonic-gate 	} else if (checkflag == 2) { /* tar format and euid == 0 */
45137c478bd9Sstevel@tonic-gate 		duid = sp->st_uid;
45147c478bd9Sstevel@tonic-gate 		dgid = sp->st_gid;
45157c478bd9Sstevel@tonic-gate 	}
45167c478bd9Sstevel@tonic-gate 	if ((checkflag == 1) || (checkflag == 2))
45177c478bd9Sstevel@tonic-gate 		(void) fchownat(dirfd, name, duid, dgid, symflag);
45187c478bd9Sstevel@tonic-gate }
45197c478bd9Sstevel@tonic-gate 
45207c478bd9Sstevel@tonic-gate /*ARGSUSED*/
45217c478bd9Sstevel@tonic-gate static void
onintr(int sig)45227c478bd9Sstevel@tonic-gate onintr(int sig)
45237c478bd9Sstevel@tonic-gate {
45247c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, SIG_IGN);
45257c478bd9Sstevel@tonic-gate 	term++;
45267c478bd9Sstevel@tonic-gate }
45277c478bd9Sstevel@tonic-gate 
45287c478bd9Sstevel@tonic-gate /*ARGSUSED*/
45297c478bd9Sstevel@tonic-gate static void
onquit(int sig)45307c478bd9Sstevel@tonic-gate onquit(int sig)
45317c478bd9Sstevel@tonic-gate {
45327c478bd9Sstevel@tonic-gate 	(void) signal(SIGQUIT, SIG_IGN);
45337c478bd9Sstevel@tonic-gate 	term++;
45347c478bd9Sstevel@tonic-gate }
45357c478bd9Sstevel@tonic-gate 
45367c478bd9Sstevel@tonic-gate /*ARGSUSED*/
45377c478bd9Sstevel@tonic-gate static void
onhup(int sig)45387c478bd9Sstevel@tonic-gate onhup(int sig)
45397c478bd9Sstevel@tonic-gate {
45407c478bd9Sstevel@tonic-gate 	(void) signal(SIGHUP, SIG_IGN);
45417c478bd9Sstevel@tonic-gate 	term++;
45427c478bd9Sstevel@tonic-gate }
45437c478bd9Sstevel@tonic-gate 
45447c478bd9Sstevel@tonic-gate static void
tomodes(struct stat * sp)45457c478bd9Sstevel@tonic-gate tomodes(struct stat *sp)
45467c478bd9Sstevel@tonic-gate {
45477c478bd9Sstevel@tonic-gate 	uid_t uid;
45487c478bd9Sstevel@tonic-gate 	gid_t gid;
45497c478bd9Sstevel@tonic-gate 
45507c478bd9Sstevel@tonic-gate 	bzero(dblock.dummy, TBLOCK);
45517c478bd9Sstevel@tonic-gate 
45527c478bd9Sstevel@tonic-gate 	/*
45537c478bd9Sstevel@tonic-gate 	 * If the uid or gid is too large, we can't put it into
45547c478bd9Sstevel@tonic-gate 	 * the archive.  We could fail to put anything in the
45557c478bd9Sstevel@tonic-gate 	 * archive at all .. but most of the time the name service
45567c478bd9Sstevel@tonic-gate 	 * will save the day when we do a lookup at restore time.
45577c478bd9Sstevel@tonic-gate 	 *
45587c478bd9Sstevel@tonic-gate 	 * Instead we choose a "safe" uid and gid, and fix up whether
45597c478bd9Sstevel@tonic-gate 	 * or not the setuid and setgid bits are left set to extraction
45607c478bd9Sstevel@tonic-gate 	 * time.
45617c478bd9Sstevel@tonic-gate 	 */
45627c478bd9Sstevel@tonic-gate 	if (Eflag) {
45637c478bd9Sstevel@tonic-gate 		if ((ulong_t)(uid = sp->st_uid) > (ulong_t)OCTAL7CHAR) {
45647c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_UID;
45657c478bd9Sstevel@tonic-gate 			Xtarhdr.x_uid = uid;
45667c478bd9Sstevel@tonic-gate 		}
45677c478bd9Sstevel@tonic-gate 		if ((ulong_t)(gid = sp->st_gid) > (ulong_t)OCTAL7CHAR) {
45687c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_GID;
45697c478bd9Sstevel@tonic-gate 			Xtarhdr.x_gid = gid;
45707c478bd9Sstevel@tonic-gate 		}
45717c478bd9Sstevel@tonic-gate 		if (sp->st_size > TAR_OFFSET_MAX) {
45727c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_SIZE;
45737c478bd9Sstevel@tonic-gate 			Xtarhdr.x_filesz = sp->st_size;
45747c478bd9Sstevel@tonic-gate 			(void) sprintf(dblock.dbuf.size, "%011" FMT_off_t_o,
45757c478bd9Sstevel@tonic-gate 			    (off_t)0);
45767c478bd9Sstevel@tonic-gate 		} else
45777c478bd9Sstevel@tonic-gate 			(void) sprintf(dblock.dbuf.size, "%011" FMT_off_t_o,
45787c478bd9Sstevel@tonic-gate 			    sp->st_size);
45797c478bd9Sstevel@tonic-gate 	} else {
45807c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.size, "%011" FMT_off_t_o,
45817c478bd9Sstevel@tonic-gate 		    sp->st_size);
45827c478bd9Sstevel@tonic-gate 	}
45837c478bd9Sstevel@tonic-gate 	if ((ulong_t)(uid = sp->st_uid) > (ulong_t)OCTAL7CHAR)
45847c478bd9Sstevel@tonic-gate 		uid = UID_NOBODY;
45857c478bd9Sstevel@tonic-gate 	if ((ulong_t)(gid = sp->st_gid) > (ulong_t)OCTAL7CHAR)
45867c478bd9Sstevel@tonic-gate 		gid = GID_NOBODY;
45877c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.gid, "%07lo", gid);
45887c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.uid, "%07lo", uid);
45897c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.mode, "%07lo", sp->st_mode & POSIXMODES);
45907c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.mtime, "%011lo", sp->st_mtime);
45917c478bd9Sstevel@tonic-gate }
45927c478bd9Sstevel@tonic-gate 
45937c478bd9Sstevel@tonic-gate static	int
45947c478bd9Sstevel@tonic-gate #ifdef	EUC
45957c478bd9Sstevel@tonic-gate /*
45967c478bd9Sstevel@tonic-gate  * Warning:  the result of this function depends whether 'char' is a
45977c478bd9Sstevel@tonic-gate  * signed or unsigned data type.  This a source of potential
45987c478bd9Sstevel@tonic-gate  * non-portability among heterogeneous systems.  It is retained here
45997c478bd9Sstevel@tonic-gate  * for backward compatibility.
46007c478bd9Sstevel@tonic-gate  */
checksum_signed(union hblock * dblockp)46017c478bd9Sstevel@tonic-gate checksum_signed(union hblock *dblockp)
46027c478bd9Sstevel@tonic-gate #else
46037c478bd9Sstevel@tonic-gate checksum(union hblock *dblockp)
46047c478bd9Sstevel@tonic-gate #endif	/* EUC */
46057c478bd9Sstevel@tonic-gate {
46067c478bd9Sstevel@tonic-gate 	int i;
46077c478bd9Sstevel@tonic-gate 	char *cp;
46087c478bd9Sstevel@tonic-gate 
46097c478bd9Sstevel@tonic-gate 	for (cp = dblockp->dbuf.chksum;
46107c478bd9Sstevel@tonic-gate 	    cp < &dblockp->dbuf.chksum[sizeof (dblockp->dbuf.chksum)]; cp++)
46117c478bd9Sstevel@tonic-gate 		*cp = ' ';
46127c478bd9Sstevel@tonic-gate 	i = 0;
46137c478bd9Sstevel@tonic-gate 	for (cp = dblockp->dummy; cp < &(dblockp->dummy[TBLOCK]); cp++)
46147c478bd9Sstevel@tonic-gate 		i += *cp;
46157c478bd9Sstevel@tonic-gate 	return (i);
46167c478bd9Sstevel@tonic-gate }
46177c478bd9Sstevel@tonic-gate 
46187c478bd9Sstevel@tonic-gate #ifdef	EUC
46197c478bd9Sstevel@tonic-gate /*
46207c478bd9Sstevel@tonic-gate  * Generate unsigned checksum, regardless of what C compiler is
46217c478bd9Sstevel@tonic-gate  * used.  Survives in the face of arbitrary 8-bit clean filenames,
46227c478bd9Sstevel@tonic-gate  * e.g., internationalized filenames.
46237c478bd9Sstevel@tonic-gate  */
46247c478bd9Sstevel@tonic-gate static int
checksum(union hblock * dblockp)46257c478bd9Sstevel@tonic-gate checksum(union hblock *dblockp)
46267c478bd9Sstevel@tonic-gate {
46277c478bd9Sstevel@tonic-gate 	unsigned i;
46287c478bd9Sstevel@tonic-gate 	unsigned char *cp;
46297c478bd9Sstevel@tonic-gate 
46307c478bd9Sstevel@tonic-gate 	for (cp = (unsigned char *) dblockp->dbuf.chksum;
46317c478bd9Sstevel@tonic-gate 	    cp < (unsigned char *)
46327c478bd9Sstevel@tonic-gate 	    &(dblockp->dbuf.chksum[sizeof (dblockp->dbuf.chksum)]); cp++)
46337c478bd9Sstevel@tonic-gate 		*cp = ' ';
46347c478bd9Sstevel@tonic-gate 	i = 0;
46357c478bd9Sstevel@tonic-gate 	for (cp = (unsigned char *) dblockp->dummy;
46367c478bd9Sstevel@tonic-gate 	    cp < (unsigned char *) &(dblockp->dummy[TBLOCK]); cp++)
46377c478bd9Sstevel@tonic-gate 		i += *cp;
46387c478bd9Sstevel@tonic-gate 
46397c478bd9Sstevel@tonic-gate 	return (i);
46407c478bd9Sstevel@tonic-gate }
46417c478bd9Sstevel@tonic-gate #endif	/* EUC */
46427c478bd9Sstevel@tonic-gate 
46437c478bd9Sstevel@tonic-gate /*
46447c478bd9Sstevel@tonic-gate  * If the w flag is set, output the action to be taken and the name of the
46457c478bd9Sstevel@tonic-gate  * file.  Perform the action if the user response is affirmative.
46467c478bd9Sstevel@tonic-gate  */
46477c478bd9Sstevel@tonic-gate 
46487c478bd9Sstevel@tonic-gate static int
checkw(char c,char * name)46497c478bd9Sstevel@tonic-gate checkw(char c, char *name)
46507c478bd9Sstevel@tonic-gate {
46517c478bd9Sstevel@tonic-gate 	if (wflag) {
46527c478bd9Sstevel@tonic-gate 		(void) fprintf(vfile, "%c ", c);
46537c478bd9Sstevel@tonic-gate 		if (vflag)
46547c478bd9Sstevel@tonic-gate 			longt(&stbuf, ' ');	/* do we have acl info here */
46557c478bd9Sstevel@tonic-gate 		(void) fprintf(vfile, "%s: ", name);
46563d63ea05Sas145665 		if (yes() == 1) {
46577c478bd9Sstevel@tonic-gate 			return (1);
46587c478bd9Sstevel@tonic-gate 		}
46597c478bd9Sstevel@tonic-gate 		return (0);
46607c478bd9Sstevel@tonic-gate 	}
46617c478bd9Sstevel@tonic-gate 	return (1);
46627c478bd9Sstevel@tonic-gate }
46637c478bd9Sstevel@tonic-gate 
46647c478bd9Sstevel@tonic-gate /*
46653a1dab68SRich Burridge  * When the F flag is set, exclude RCS and SCCS directories (and any files
46663a1dab68SRich Burridge  * or directories under them).  If F is set twice, also exclude .o files,
46673a1dab68SRich Burridge  * and files names errs, core, and a.out.
46683a1dab68SRich Burridge  *
46693a1dab68SRich Burridge  * Return 0 if file should be excluded, 1 otherwise.
46707c478bd9Sstevel@tonic-gate  */
46717c478bd9Sstevel@tonic-gate 
46727c478bd9Sstevel@tonic-gate static int
checkf(char * longname,int is_dir,int howmuch)46733a1dab68SRich Burridge checkf(char *longname, int is_dir, int howmuch)
46747c478bd9Sstevel@tonic-gate {
46753a1dab68SRich Burridge 	static char fullname[PATH_MAX + 1];
46763a1dab68SRich Burridge 	char *dir, *name;
46777c478bd9Sstevel@tonic-gate 
46783a1dab68SRich Burridge #if defined(O_XATTR)
46793a1dab68SRich Burridge 	/*
46803a1dab68SRich Burridge 	 * If there is an xattr_buf structure associated with this file,
46813a1dab68SRich Burridge 	 * always return 1.
46823a1dab68SRich Burridge 	 */
46833a1dab68SRich Burridge 	if (xattrp) {
46847c478bd9Sstevel@tonic-gate 		return (1);
46857c478bd9Sstevel@tonic-gate 	}
46863a1dab68SRich Burridge #endif
46873a1dab68SRich Burridge 
46883a1dab68SRich Burridge 	/*
46893a1dab68SRich Burridge 	 * First check to see if the base name is an RCS or SCCS directory.
46903a1dab68SRich Burridge 	 */
46913a1dab68SRich Burridge 	if (strlcpy(fullname, longname, sizeof (fullname)) >= sizeof (fullname))
46927c478bd9Sstevel@tonic-gate 		return (1);
46933a1dab68SRich Burridge 
46943a1dab68SRich Burridge 	name = basename(fullname);
46953a1dab68SRich Burridge 	if (is_dir) {
46963a1dab68SRich Burridge 		if ((strcmp(name, "SCCS") == 0) || (strcmp(name, "RCS") == 0))
46977c478bd9Sstevel@tonic-gate 			return (0);
46983a1dab68SRich Burridge 	}
46993a1dab68SRich Burridge 
47003a1dab68SRich Burridge 	/*
47013a1dab68SRich Burridge 	 * If two -F command line options were given then exclude .o files,
47023a1dab68SRich Burridge 	 * and files named errs, core, and a.out.
47033a1dab68SRich Burridge 	 */
47043a1dab68SRich Burridge 	if (howmuch > 1 && !is_dir) {
47053a1dab68SRich Burridge 		size_t l = strlen(name);
47063a1dab68SRich Burridge 
47073a1dab68SRich Burridge 		if (l >= 3 && name[l - 2] == '.' && name[l - 1] == 'o')
47083a1dab68SRich Burridge 			return (0);
47097c478bd9Sstevel@tonic-gate 		if (strcmp(name, "core") == 0 || strcmp(name, "errs") == 0 ||
47107c478bd9Sstevel@tonic-gate 		    strcmp(name, "a.out") == 0)
47117c478bd9Sstevel@tonic-gate 			return (0);
47127c478bd9Sstevel@tonic-gate 	}
47137c478bd9Sstevel@tonic-gate 
47143a1dab68SRich Burridge 	/*
47153a1dab68SRich Burridge 	 * At this point, check to see if this file has a parent directory
47163a1dab68SRich Burridge 	 * named RCS or SCCS.  If so, then this file should be excluded too.
47173a1dab68SRich Burridge 	 * The strcpy() operation is done again, because basename(3C) may
47183a1dab68SRich Burridge 	 * modify the path string passed to it.
47193a1dab68SRich Burridge 	 */
47203a1dab68SRich Burridge 	if (strlcpy(fullname, longname, sizeof (fullname)) >= sizeof (fullname))
47213a1dab68SRich Burridge 		return (1);
47223a1dab68SRich Burridge 
47233a1dab68SRich Burridge 	dir = dirname(fullname);
47243a1dab68SRich Burridge 	while (strcmp(dir, ".") != 0) {
47253a1dab68SRich Burridge 		name = basename(dir);
47263a1dab68SRich Burridge 		if ((strcmp(name, "SCCS") == 0) || (strcmp(name, "RCS") == 0))
47273a1dab68SRich Burridge 			return (0);
47283a1dab68SRich Burridge 		dir = dirname(dir);
47293a1dab68SRich Burridge 	}
47303a1dab68SRich Burridge 
47317c478bd9Sstevel@tonic-gate 	return (1);
47327c478bd9Sstevel@tonic-gate }
47337c478bd9Sstevel@tonic-gate 
47347c478bd9Sstevel@tonic-gate static int
response(void)47357c478bd9Sstevel@tonic-gate response(void)
47367c478bd9Sstevel@tonic-gate {
47377c478bd9Sstevel@tonic-gate 	int c;
47387c478bd9Sstevel@tonic-gate 
47397c478bd9Sstevel@tonic-gate 	c = getchar();
47407c478bd9Sstevel@tonic-gate 	if (c != '\n')
4741eace40a5Sceastha 		while (getchar() != '\n')
4742eace40a5Sceastha 			;
47437c478bd9Sstevel@tonic-gate 	else c = 'n';
47447c478bd9Sstevel@tonic-gate 	return ((c >= 'A' && c <= 'Z') ? c + ('a'-'A') : c);
47457c478bd9Sstevel@tonic-gate }
47467c478bd9Sstevel@tonic-gate 
47477c478bd9Sstevel@tonic-gate /* Has file been modified since being put into archive? If so, return > 0. */
47487c478bd9Sstevel@tonic-gate 
4749d67944fbSScott Rotondo static off_t	lookup(char *);
4750d67944fbSScott Rotondo 
47517c478bd9Sstevel@tonic-gate static int
checkupdate(char * arg)47527c478bd9Sstevel@tonic-gate checkupdate(char *arg)
47537c478bd9Sstevel@tonic-gate {
47547c478bd9Sstevel@tonic-gate 	char name[PATH_MAX+1];
47557c478bd9Sstevel@tonic-gate 	time_t	mtime;
47567c478bd9Sstevel@tonic-gate 	long nsecs;
47577c478bd9Sstevel@tonic-gate 	off_t seekp;
47587c478bd9Sstevel@tonic-gate 
47597c478bd9Sstevel@tonic-gate 	rewind(tfile);
47607c478bd9Sstevel@tonic-gate 	if ((seekp = lookup(arg)) < 0)
47617c478bd9Sstevel@tonic-gate 		return (1);
47627c478bd9Sstevel@tonic-gate 	(void) fseek(tfile, seekp, 0);
47637c478bd9Sstevel@tonic-gate 	(void) fscanf(tfile, "%s %ld.%ld", name, &mtime, &nsecs);
47647c478bd9Sstevel@tonic-gate 
47657c478bd9Sstevel@tonic-gate 	/*
47667c478bd9Sstevel@tonic-gate 	 * Unless nanoseconds were stored in the file, only use seconds for
47677c478bd9Sstevel@tonic-gate 	 * comparison of time.  Nanoseconds are stored when -E is specified.
47687c478bd9Sstevel@tonic-gate 	 */
47697c478bd9Sstevel@tonic-gate 	if (Eflag == 0)
47707c478bd9Sstevel@tonic-gate 		return (stbuf.st_mtime > mtime);
47717c478bd9Sstevel@tonic-gate 
47727c478bd9Sstevel@tonic-gate 	if ((stbuf.st_mtime < mtime) ||
47737c478bd9Sstevel@tonic-gate 	    ((stbuf.st_mtime == mtime) && (stbuf.st_mtim.tv_nsec <= nsecs)))
47747c478bd9Sstevel@tonic-gate 		return (0);
47757c478bd9Sstevel@tonic-gate 	return (1);
47767c478bd9Sstevel@tonic-gate }
47777c478bd9Sstevel@tonic-gate 
47787c478bd9Sstevel@tonic-gate 
47797c478bd9Sstevel@tonic-gate /*
47807c478bd9Sstevel@tonic-gate  *	newvol	get new floppy (or tape) volume
47817c478bd9Sstevel@tonic-gate  *
47827c478bd9Sstevel@tonic-gate  *	newvol();		resets tapepos and first to TRUE, prompts for
47837c478bd9Sstevel@tonic-gate  *				for new volume, and waits.
47847c478bd9Sstevel@tonic-gate  *	if dumping, end-of-file is written onto the tape.
47857c478bd9Sstevel@tonic-gate  */
47867c478bd9Sstevel@tonic-gate 
47877c478bd9Sstevel@tonic-gate static void
newvol(void)47887c478bd9Sstevel@tonic-gate newvol(void)
47897c478bd9Sstevel@tonic-gate {
47907c478bd9Sstevel@tonic-gate 	int c;
47917c478bd9Sstevel@tonic-gate 
47927c478bd9Sstevel@tonic-gate 	if (dumping) {
47937c478bd9Sstevel@tonic-gate #ifdef DEBUG
47947c478bd9Sstevel@tonic-gate 		DEBUG("newvol called with 'dumping' set\n", 0, 0);
47957c478bd9Sstevel@tonic-gate #endif
47967c478bd9Sstevel@tonic-gate 		putempty((blkcnt_t)2);	/* 2 EOT marks */
47977c478bd9Sstevel@tonic-gate 		closevol();
47987c478bd9Sstevel@tonic-gate 		flushtape();
47997c478bd9Sstevel@tonic-gate 		sync();
48007c478bd9Sstevel@tonic-gate 		tapepos = 0;
48017c478bd9Sstevel@tonic-gate 	} else
48027c478bd9Sstevel@tonic-gate 		first = TRUE;
48037c478bd9Sstevel@tonic-gate 	if (close(mt) != 0)
48047c478bd9Sstevel@tonic-gate 		vperror(2, gettext("close error"));
48057c478bd9Sstevel@tonic-gate 	mt = 0;
48067c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
48077c478bd9Sstevel@tonic-gate 	    "tar: \007please insert new volume, then press RETURN."));
48087c478bd9Sstevel@tonic-gate 	(void) fseek(stdin, (off_t)0, 2);	/* scan over read-ahead */
48097c478bd9Sstevel@tonic-gate 	while ((c = getchar()) != '\n' && ! term)
48107c478bd9Sstevel@tonic-gate 		if (c == EOF)
48117c478bd9Sstevel@tonic-gate 			done(Errflg);
48127c478bd9Sstevel@tonic-gate 	if (term)
48137c478bd9Sstevel@tonic-gate 		done(Errflg);
48147c478bd9Sstevel@tonic-gate 
48157c478bd9Sstevel@tonic-gate 	errno = 0;
48167c478bd9Sstevel@tonic-gate 
48177c478bd9Sstevel@tonic-gate 	if (strcmp(usefile, "-") == 0) {
48187c478bd9Sstevel@tonic-gate 		mt = dup(1);
48197c478bd9Sstevel@tonic-gate 	} else {
48207c478bd9Sstevel@tonic-gate 		mt = open(usefile, dumping ? update : 0);
48217c478bd9Sstevel@tonic-gate 	}
48227c478bd9Sstevel@tonic-gate 
48237c478bd9Sstevel@tonic-gate 	if (mt < 0) {
48247c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
48257c478bd9Sstevel@tonic-gate 		    "tar: cannot reopen %s (%s)\n"),
48267c478bd9Sstevel@tonic-gate 		    dumping ? gettext("output") : gettext("input"), usefile);
48277c478bd9Sstevel@tonic-gate 
48288e4a71aeSRich Burridge #ifdef DEBUG
48298e4a71aeSRich Burridge 		DEBUG("update=%d, usefile=%s ", update, usefile);
48308e4a71aeSRich Burridge 		DEBUG("mt=%d, [%s]\n", mt, strerror(errno));
48318e4a71aeSRich Burridge #endif
48327c478bd9Sstevel@tonic-gate 
48337c478bd9Sstevel@tonic-gate 		done(2);
48347c478bd9Sstevel@tonic-gate 	}
48357c478bd9Sstevel@tonic-gate }
48367c478bd9Sstevel@tonic-gate 
48377c478bd9Sstevel@tonic-gate /*
48387c478bd9Sstevel@tonic-gate  * Write a trailer portion to close out the current output volume.
48397c478bd9Sstevel@tonic-gate  */
48407c478bd9Sstevel@tonic-gate 
48417c478bd9Sstevel@tonic-gate static void
closevol(void)48427c478bd9Sstevel@tonic-gate closevol(void)
48437c478bd9Sstevel@tonic-gate {
48447c478bd9Sstevel@tonic-gate 	if (mulvol) {
48457c478bd9Sstevel@tonic-gate 		/*
48467c478bd9Sstevel@tonic-gate 		 * blocklim does not count the 2 EOT marks;
48477c478bd9Sstevel@tonic-gate 		 * tapepos  does count the 2 EOT marks;
48487c478bd9Sstevel@tonic-gate 		 * therefore we need the +2 below.
48497c478bd9Sstevel@tonic-gate 		 */
48507c478bd9Sstevel@tonic-gate 		putempty(blocklim + (blkcnt_t)2 - tapepos);
48517c478bd9Sstevel@tonic-gate 	}
48527c478bd9Sstevel@tonic-gate }
48537c478bd9Sstevel@tonic-gate 
48547c478bd9Sstevel@tonic-gate static void
done(int n)48557c478bd9Sstevel@tonic-gate done(int n)
48567c478bd9Sstevel@tonic-gate {
485731732068SRich Burridge 	/*
485831732068SRich Burridge 	 * If we were terminated in some way, and we would otherwise have
485931732068SRich Burridge 	 * exited with a value of 0, adjust to 1, so that external callers
486031732068SRich Burridge 	 * can determine this by looking at the exit status.
486131732068SRich Burridge 	 */
486231732068SRich Burridge 	if (term && n == 0)
486331732068SRich Burridge 		n = 1;
486431732068SRich Burridge 
48658f1d104bSRich Burridge 	if (tfile != NULL)
48667c478bd9Sstevel@tonic-gate 		(void) unlink(tname);
486736802407SRich Burridge 	if (compress_opt != NULL)
486836802407SRich Burridge 		(void) free(compress_opt);
48697c478bd9Sstevel@tonic-gate 	if (mt > 0) {
48707c478bd9Sstevel@tonic-gate 		if ((close(mt) != 0) || (fclose(stdout) != 0)) {
48717c478bd9Sstevel@tonic-gate 			perror(gettext("tar: close error"));
48727c478bd9Sstevel@tonic-gate 			exit(2);
48737c478bd9Sstevel@tonic-gate 		}
48747c478bd9Sstevel@tonic-gate 	}
4875*c536b1f9SRobert Mustacchi 	/*
4876*c536b1f9SRobert Mustacchi 	 * If we have a compression child, we should have a child process that
4877*c536b1f9SRobert Mustacchi 	 * we're waiting for to finish compressing or uncompressing the tar
4878*c536b1f9SRobert Mustacchi 	 * stream.
4879*c536b1f9SRobert Mustacchi 	 */
4880*c536b1f9SRobert Mustacchi 	if (comp_pid != 0)
4881*c536b1f9SRobert Mustacchi 		wait_pid(comp_pid);
48827c478bd9Sstevel@tonic-gate 	exit(n);
48837c478bd9Sstevel@tonic-gate }
48847c478bd9Sstevel@tonic-gate 
48857c478bd9Sstevel@tonic-gate /*
48867c478bd9Sstevel@tonic-gate  * Determine if s1 is a prefix portion of s2 (or the same as s2).
48877c478bd9Sstevel@tonic-gate  */
48887c478bd9Sstevel@tonic-gate 
48897c478bd9Sstevel@tonic-gate static	int
is_prefix(char * s1,char * s2)48907c478bd9Sstevel@tonic-gate is_prefix(char *s1, char *s2)
48917c478bd9Sstevel@tonic-gate {
48927c478bd9Sstevel@tonic-gate 	while (*s1)
48937c478bd9Sstevel@tonic-gate 		if (*s1++ != *s2++)
48947c478bd9Sstevel@tonic-gate 			return (0);
48957c478bd9Sstevel@tonic-gate 	if (*s2)
48967c478bd9Sstevel@tonic-gate 		return (*s2 == '/');
48977c478bd9Sstevel@tonic-gate 	return (1);
48987c478bd9Sstevel@tonic-gate }
48997c478bd9Sstevel@tonic-gate 
49007c478bd9Sstevel@tonic-gate /*
49017c478bd9Sstevel@tonic-gate  * lookup and bsrch look through tfile entries to find a match for a name.
49027c478bd9Sstevel@tonic-gate  * The name can be up to PATH_MAX bytes.  bsrch compares what it sees between
49037c478bd9Sstevel@tonic-gate  * a pair of newline chars, so the buffer it uses must be long enough for
49047c478bd9Sstevel@tonic-gate  * two lines:  name and modification time as well as period, newline and space.
49057c478bd9Sstevel@tonic-gate  *
49067c478bd9Sstevel@tonic-gate  * A kludge was added to bsrch to take care of matching on the first entry
49077c478bd9Sstevel@tonic-gate  * in the file--there is no leading newline.  So, if we are reading from the
49087c478bd9Sstevel@tonic-gate  * start of the file, read into byte two and set the first byte to a newline.
49097c478bd9Sstevel@tonic-gate  * Otherwise, the first entry cannot be matched.
49107c478bd9Sstevel@tonic-gate  *
49117c478bd9Sstevel@tonic-gate  */
49127c478bd9Sstevel@tonic-gate 
49137c478bd9Sstevel@tonic-gate #define	N	(2 * (PATH_MAX + TIME_MAX_DIGITS + LONG_MAX_DIGITS + 3))
49147c478bd9Sstevel@tonic-gate static	off_t
lookup(char * s)49157c478bd9Sstevel@tonic-gate lookup(char *s)
49167c478bd9Sstevel@tonic-gate {
49177c478bd9Sstevel@tonic-gate 	int i;
49187c478bd9Sstevel@tonic-gate 	off_t a;
49197c478bd9Sstevel@tonic-gate 
49207c478bd9Sstevel@tonic-gate 	for (i = 0; s[i]; i++)
49217c478bd9Sstevel@tonic-gate 		if (s[i] == ' ')
49227c478bd9Sstevel@tonic-gate 			break;
49237c478bd9Sstevel@tonic-gate 	a = bsrch(s, i, low, high);
49247c478bd9Sstevel@tonic-gate 	return (a);
49257c478bd9Sstevel@tonic-gate }
49267c478bd9Sstevel@tonic-gate 
49277c478bd9Sstevel@tonic-gate static off_t
bsrch(char * s,int n,off_t l,off_t h)49287c478bd9Sstevel@tonic-gate bsrch(char *s, int n, off_t l, off_t h)
49297c478bd9Sstevel@tonic-gate {
49307c478bd9Sstevel@tonic-gate 	int i, j;
49317c478bd9Sstevel@tonic-gate 	char b[N];
49327c478bd9Sstevel@tonic-gate 	off_t m, m1;
49337c478bd9Sstevel@tonic-gate 
49347c478bd9Sstevel@tonic-gate 
49357c478bd9Sstevel@tonic-gate loop:
49367c478bd9Sstevel@tonic-gate 	if (l >= h)
49377c478bd9Sstevel@tonic-gate 		return ((off_t)-1);
49387c478bd9Sstevel@tonic-gate 	m = l + (h-l)/2 - N/2;
49397c478bd9Sstevel@tonic-gate 	if (m < l)
49407c478bd9Sstevel@tonic-gate 		m = l;
49417c478bd9Sstevel@tonic-gate 	(void) fseek(tfile, m, 0);
49427c478bd9Sstevel@tonic-gate 	if (m == 0) {
49437c478bd9Sstevel@tonic-gate 		(void) fread(b+1, 1, N-1, tfile);
49447c478bd9Sstevel@tonic-gate 		b[0] = '\n';
49457c478bd9Sstevel@tonic-gate 		m--;
49467c478bd9Sstevel@tonic-gate 	} else
49477c478bd9Sstevel@tonic-gate 		(void) fread(b, 1, N, tfile);
49487c478bd9Sstevel@tonic-gate 	for (i = 0; i < N; i++) {
49497c478bd9Sstevel@tonic-gate 		if (b[i] == '\n')
49507c478bd9Sstevel@tonic-gate 			break;
49517c478bd9Sstevel@tonic-gate 		m++;
49527c478bd9Sstevel@tonic-gate 	}
49537c478bd9Sstevel@tonic-gate 	if (m >= h)
49547c478bd9Sstevel@tonic-gate 		return ((off_t)-1);
49557c478bd9Sstevel@tonic-gate 	m1 = m;
49567c478bd9Sstevel@tonic-gate 	j = i;
49577c478bd9Sstevel@tonic-gate 	for (i++; i < N; i++) {
49587c478bd9Sstevel@tonic-gate 		m1++;
49597c478bd9Sstevel@tonic-gate 		if (b[i] == '\n')
49607c478bd9Sstevel@tonic-gate 			break;
49617c478bd9Sstevel@tonic-gate 	}
49627c478bd9Sstevel@tonic-gate 	i = cmp(b+j, s, n);
49637c478bd9Sstevel@tonic-gate 	if (i < 0) {
49647c478bd9Sstevel@tonic-gate 		h = m;
49657c478bd9Sstevel@tonic-gate 		goto loop;
49667c478bd9Sstevel@tonic-gate 	}
49677c478bd9Sstevel@tonic-gate 	if (i > 0) {
49687c478bd9Sstevel@tonic-gate 		l = m1;
49697c478bd9Sstevel@tonic-gate 		goto loop;
49707c478bd9Sstevel@tonic-gate 	}
49717c478bd9Sstevel@tonic-gate 	if (m < 0)
49727c478bd9Sstevel@tonic-gate 		m = 0;
49737c478bd9Sstevel@tonic-gate 	return (m);
49747c478bd9Sstevel@tonic-gate }
49757c478bd9Sstevel@tonic-gate 
49767c478bd9Sstevel@tonic-gate static int
cmp(char * b,char * s,int n)49777c478bd9Sstevel@tonic-gate cmp(char *b, char *s, int n)
49787c478bd9Sstevel@tonic-gate {
49797c478bd9Sstevel@tonic-gate 	int i;
49807c478bd9Sstevel@tonic-gate 
49817c478bd9Sstevel@tonic-gate 	assert(b[0] == '\n');
49827c478bd9Sstevel@tonic-gate 
49837c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
49847c478bd9Sstevel@tonic-gate 		if (b[i+1] > s[i])
49857c478bd9Sstevel@tonic-gate 			return (-1);
49867c478bd9Sstevel@tonic-gate 		if (b[i+1] < s[i])
49877c478bd9Sstevel@tonic-gate 			return (1);
49887c478bd9Sstevel@tonic-gate 	}
49897c478bd9Sstevel@tonic-gate 	return (b[i+1] == ' '? 0 : -1);
49907c478bd9Sstevel@tonic-gate }
49917c478bd9Sstevel@tonic-gate 
49927c478bd9Sstevel@tonic-gate 
49937c478bd9Sstevel@tonic-gate /*
49947c478bd9Sstevel@tonic-gate  *	seekdisk	seek to next file on archive
49957c478bd9Sstevel@tonic-gate  *
49967c478bd9Sstevel@tonic-gate  *	called by passtape() only
49977c478bd9Sstevel@tonic-gate  *
49987c478bd9Sstevel@tonic-gate  *	WARNING: expects "nblock" to be set, that is, readtape() to have
49997c478bd9Sstevel@tonic-gate  *		already been called.  Since passtape() is only called
50007c478bd9Sstevel@tonic-gate  *		after a file header block has been read (why else would
50017c478bd9Sstevel@tonic-gate  *		we skip to next file?), this is currently safe.
50027c478bd9Sstevel@tonic-gate  *
50037c478bd9Sstevel@tonic-gate  *	changed to guarantee SYS_BLOCK boundary
50047c478bd9Sstevel@tonic-gate  */
50057c478bd9Sstevel@tonic-gate 
50067c478bd9Sstevel@tonic-gate static void
seekdisk(blkcnt_t blocks)50077c478bd9Sstevel@tonic-gate seekdisk(blkcnt_t blocks)
50087c478bd9Sstevel@tonic-gate {
50097c478bd9Sstevel@tonic-gate 	off_t seekval;
50107c478bd9Sstevel@tonic-gate #if SYS_BLOCK > TBLOCK
50117c478bd9Sstevel@tonic-gate 	/* handle non-multiple of SYS_BLOCK */
50127c478bd9Sstevel@tonic-gate 	blkcnt_t nxb;	/* # extra blocks */
50137c478bd9Sstevel@tonic-gate #endif
50147c478bd9Sstevel@tonic-gate 
50157c478bd9Sstevel@tonic-gate 	tapepos += blocks;
50167c478bd9Sstevel@tonic-gate #ifdef DEBUG
50177c478bd9Sstevel@tonic-gate 	DEBUG("seekdisk(%" FMT_blkcnt_t ") called\n", blocks, 0);
50187c478bd9Sstevel@tonic-gate #endif
50197c478bd9Sstevel@tonic-gate 	if (recno + blocks <= nblock) {
50207c478bd9Sstevel@tonic-gate 		recno += blocks;
50217c478bd9Sstevel@tonic-gate 		return;
50227c478bd9Sstevel@tonic-gate 	}
50237c478bd9Sstevel@tonic-gate 	if (recno > nblock)
50247c478bd9Sstevel@tonic-gate 		recno = nblock;
50257c478bd9Sstevel@tonic-gate 	seekval = (off_t)blocks - (nblock - recno);
50267c478bd9Sstevel@tonic-gate 	recno = nblock;	/* so readtape() reads next time through */
50277c478bd9Sstevel@tonic-gate #if SYS_BLOCK > TBLOCK
50287c478bd9Sstevel@tonic-gate 	nxb = (blkcnt_t)(seekval % (off_t)(SYS_BLOCK / TBLOCK));
50297c478bd9Sstevel@tonic-gate #ifdef DEBUG
50307c478bd9Sstevel@tonic-gate 	DEBUG("xtrablks=%" FMT_blkcnt_t " seekval=%" FMT_blkcnt_t " blks\n",
50317c478bd9Sstevel@tonic-gate 	    nxb, seekval);
50327c478bd9Sstevel@tonic-gate #endif
50337c478bd9Sstevel@tonic-gate 	if (nxb && nxb > seekval) /* don't seek--we'll read */
50347c478bd9Sstevel@tonic-gate 		goto noseek;
50357c478bd9Sstevel@tonic-gate 	seekval -=  nxb;	/* don't seek quite so far */
50367c478bd9Sstevel@tonic-gate #endif
50377c478bd9Sstevel@tonic-gate 	if (lseek(mt, (off_t)(TBLOCK * seekval), 1) == (off_t)-1) {
50387c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
50397c478bd9Sstevel@tonic-gate 		    "tar: device seek error\n"));
50407c478bd9Sstevel@tonic-gate 		done(3);
50417c478bd9Sstevel@tonic-gate 	}
50427c478bd9Sstevel@tonic-gate #if SYS_BLOCK > TBLOCK
50437c478bd9Sstevel@tonic-gate 	/* read those extra blocks */
50447c478bd9Sstevel@tonic-gate noseek:
50457c478bd9Sstevel@tonic-gate 	if (nxb) {
50467c478bd9Sstevel@tonic-gate #ifdef DEBUG
50477c478bd9Sstevel@tonic-gate 		DEBUG("reading extra blocks\n", 0, 0);
50487c478bd9Sstevel@tonic-gate #endif
50497c478bd9Sstevel@tonic-gate 		if (read(mt, tbuf, TBLOCK*nblock) < 0) {
50507c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
50517c478bd9Sstevel@tonic-gate 			    "tar: read error while skipping file\n"));
50527c478bd9Sstevel@tonic-gate 			done(8);
50537c478bd9Sstevel@tonic-gate 		}
50547c478bd9Sstevel@tonic-gate 		recno = nxb;	/* so we don't read in next readtape() */
50557c478bd9Sstevel@tonic-gate 	}
50567c478bd9Sstevel@tonic-gate #endif
50577c478bd9Sstevel@tonic-gate }
50587c478bd9Sstevel@tonic-gate 
50597c478bd9Sstevel@tonic-gate static void
readtape(char * buffer)50607c478bd9Sstevel@tonic-gate readtape(char *buffer)
50617c478bd9Sstevel@tonic-gate {
50627c478bd9Sstevel@tonic-gate 	int i, j;
50637c478bd9Sstevel@tonic-gate 
50647c478bd9Sstevel@tonic-gate 	++tapepos;
50657c478bd9Sstevel@tonic-gate 	if (recno >= nblock || first) {
50667c478bd9Sstevel@tonic-gate 		if (first) {
50677c478bd9Sstevel@tonic-gate 			/*
50687c478bd9Sstevel@tonic-gate 			 * set the number of blocks to read initially, based on
50697c478bd9Sstevel@tonic-gate 			 * the defined defaults for the device, or on the
50707c478bd9Sstevel@tonic-gate 			 * explicit block factor given.
50717c478bd9Sstevel@tonic-gate 			 */
507253329f75SRich Burridge 			if (bflag || defaults_used || NotTape)
50737c478bd9Sstevel@tonic-gate 				j = nblock;
50747c478bd9Sstevel@tonic-gate 			else
50757c478bd9Sstevel@tonic-gate 				j = NBLOCK;
50767c478bd9Sstevel@tonic-gate 		} else
50777c478bd9Sstevel@tonic-gate 			j = nblock;
50787c478bd9Sstevel@tonic-gate 
50797c478bd9Sstevel@tonic-gate 		if ((i = read(mt, tbuf, TBLOCK*j)) < 0) {
50807c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
50817c478bd9Sstevel@tonic-gate 			    "tar: tape read error\n"));
50827c478bd9Sstevel@tonic-gate 			done(3);
50837c478bd9Sstevel@tonic-gate 		/*
5084b35e803cSss161016 		 * i == 0 and !rflag means that EOF is reached and we are
5085b35e803cSss161016 		 * trying to update or replace an empty tar file, so exit
5086b35e803cSss161016 		 * with an error.
5087b35e803cSss161016 		 *
5088b35e803cSss161016 		 * If i == 0 and !first and NotTape, it means the pointer
5089b35e803cSss161016 		 * has gone past the EOF. It could happen if two processes
5090b35e803cSss161016 		 * try to update the same tar file simultaneously. So exit
5091b35e803cSss161016 		 * with an error.
50927c478bd9Sstevel@tonic-gate 		 */
5093b35e803cSss161016 
5094b35e803cSss161016 		} else if (i == 0) {
5095b35e803cSss161016 			if (first && !rflag) {
50967c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
50977c478bd9Sstevel@tonic-gate 				    "tar: blocksize = %d\n"), i);
50987c478bd9Sstevel@tonic-gate 				done(Errflg);
5099b35e803cSss161016 			} else if (!first && (!rflag || NotTape)) {
51007c478bd9Sstevel@tonic-gate 				mterr("read", 0, 2);
5101b35e803cSss161016 			}
51027c478bd9Sstevel@tonic-gate 		} else if ((!first || Bflag) && i != TBLOCK*j) {
51037c478bd9Sstevel@tonic-gate 			/*
51047c478bd9Sstevel@tonic-gate 			 * Short read - try to get the remaining bytes.
51057c478bd9Sstevel@tonic-gate 			 */
51067c478bd9Sstevel@tonic-gate 
51077c478bd9Sstevel@tonic-gate 			int remaining = (TBLOCK * j) - i;
51087c478bd9Sstevel@tonic-gate 			char *b = (char *)tbuf + i;
51097c478bd9Sstevel@tonic-gate 			int r;
51107c478bd9Sstevel@tonic-gate 
51117c478bd9Sstevel@tonic-gate 			do {
51127c478bd9Sstevel@tonic-gate 				if ((r = read(mt, b, remaining)) < 0) {
51137c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
51147c478bd9Sstevel@tonic-gate 					    gettext("tar: tape read error\n"));
51157c478bd9Sstevel@tonic-gate 					done(3);
51167c478bd9Sstevel@tonic-gate 				}
51177c478bd9Sstevel@tonic-gate 				b += r;
51187c478bd9Sstevel@tonic-gate 				remaining -= r;
51197c478bd9Sstevel@tonic-gate 				i += r;
51207c478bd9Sstevel@tonic-gate 			} while (remaining > 0 && r != 0);
51217c478bd9Sstevel@tonic-gate 		}
51227c478bd9Sstevel@tonic-gate 		if (first) {
51237c478bd9Sstevel@tonic-gate 			if ((i % TBLOCK) != 0) {
51247c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
51257c478bd9Sstevel@tonic-gate 				    "tar: tape blocksize error\n"));
51267c478bd9Sstevel@tonic-gate 				done(3);
51277c478bd9Sstevel@tonic-gate 			}
51287c478bd9Sstevel@tonic-gate 			i /= TBLOCK;
51297c478bd9Sstevel@tonic-gate 			if (vflag && i != nblock && i != 1) {
51307c478bd9Sstevel@tonic-gate 				if (!NotTape)
51317c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
51327c478bd9Sstevel@tonic-gate 					    "tar: blocksize = %d\n"), i);
51337c478bd9Sstevel@tonic-gate 			}
51347c478bd9Sstevel@tonic-gate 
51357c478bd9Sstevel@tonic-gate 			/*
51367c478bd9Sstevel@tonic-gate 			 * If we are reading a tape, then a short read is
51377c478bd9Sstevel@tonic-gate 			 * understood to signify that the amount read is
51387c478bd9Sstevel@tonic-gate 			 * the tape's actual blocking factor.  We adapt
51397c478bd9Sstevel@tonic-gate 			 * nblock accordingly.  There is no reason to do
51407c478bd9Sstevel@tonic-gate 			 * this when the device is not blocked.
51417c478bd9Sstevel@tonic-gate 			 */
51427c478bd9Sstevel@tonic-gate 
51437c478bd9Sstevel@tonic-gate 			if (!NotTape)
51447c478bd9Sstevel@tonic-gate 				nblock = i;
51457c478bd9Sstevel@tonic-gate 		}
51467c478bd9Sstevel@tonic-gate 		recno = 0;
51477c478bd9Sstevel@tonic-gate 	}
51487c478bd9Sstevel@tonic-gate 
51497c478bd9Sstevel@tonic-gate 	first = FALSE;
51507c478bd9Sstevel@tonic-gate 	copy(buffer, &tbuf[recno++]);
51517c478bd9Sstevel@tonic-gate }
51527c478bd9Sstevel@tonic-gate 
51537c478bd9Sstevel@tonic-gate 
51547c478bd9Sstevel@tonic-gate /*
51557c478bd9Sstevel@tonic-gate  * replacement for writetape.
51567c478bd9Sstevel@tonic-gate  */
51577c478bd9Sstevel@tonic-gate 
51587c478bd9Sstevel@tonic-gate static int
writetbuf(char * buffer,int n)51597c478bd9Sstevel@tonic-gate writetbuf(char *buffer, int n)
51607c478bd9Sstevel@tonic-gate {
51617c478bd9Sstevel@tonic-gate 	int i;
51627c478bd9Sstevel@tonic-gate 
51637c478bd9Sstevel@tonic-gate 	tapepos += n;		/* output block count */
51647c478bd9Sstevel@tonic-gate 
51657c478bd9Sstevel@tonic-gate 	if (recno >= nblock) {
51667c478bd9Sstevel@tonic-gate 		i = write(mt, (char *)tbuf, TBLOCK*nblock);
51677c478bd9Sstevel@tonic-gate 		if (i != TBLOCK*nblock)
51687c478bd9Sstevel@tonic-gate 			mterr("write", i, 2);
51697c478bd9Sstevel@tonic-gate 		recno = 0;
51707c478bd9Sstevel@tonic-gate 	}
51717c478bd9Sstevel@tonic-gate 
51727c478bd9Sstevel@tonic-gate 	/*
51737c478bd9Sstevel@tonic-gate 	 *  Special case:  We have an empty tape buffer, and the
51747c478bd9Sstevel@tonic-gate 	 *  users data size is >= the tape block size:  Avoid
51757c478bd9Sstevel@tonic-gate 	 *  the bcopy and dma direct to tape.  BIG WIN.  Add the
51767c478bd9Sstevel@tonic-gate 	 *  residual to the tape buffer.
51777c478bd9Sstevel@tonic-gate 	 */
51787c478bd9Sstevel@tonic-gate 	while (recno == 0 && n >= nblock) {
51797c478bd9Sstevel@tonic-gate 		i = (int)write(mt, buffer, TBLOCK*nblock);
51807c478bd9Sstevel@tonic-gate 		if (i != TBLOCK*nblock)
51817c478bd9Sstevel@tonic-gate 			mterr("write", i, 2);
51827c478bd9Sstevel@tonic-gate 		n -= nblock;
51837c478bd9Sstevel@tonic-gate 		buffer += (nblock * TBLOCK);
51847c478bd9Sstevel@tonic-gate 	}
51857c478bd9Sstevel@tonic-gate 
51867c478bd9Sstevel@tonic-gate 	while (n-- > 0) {
51877c478bd9Sstevel@tonic-gate 		(void) memcpy((char *)&tbuf[recno++], buffer, TBLOCK);
51887c478bd9Sstevel@tonic-gate 		buffer += TBLOCK;
51897c478bd9Sstevel@tonic-gate 		if (recno >= nblock) {
51907c478bd9Sstevel@tonic-gate 			i = (int)write(mt, (char *)tbuf, TBLOCK*nblock);
51917c478bd9Sstevel@tonic-gate 			if (i != TBLOCK*nblock)
51927c478bd9Sstevel@tonic-gate 				mterr("write", i, 2);
51937c478bd9Sstevel@tonic-gate 			recno = 0;
51947c478bd9Sstevel@tonic-gate 		}
51957c478bd9Sstevel@tonic-gate 	}
51967c478bd9Sstevel@tonic-gate 
51977c478bd9Sstevel@tonic-gate 	/* Tell the user how much to write to get in sync */
51987c478bd9Sstevel@tonic-gate 	return (nblock - recno);
51997c478bd9Sstevel@tonic-gate }
52007c478bd9Sstevel@tonic-gate 
52017c478bd9Sstevel@tonic-gate /*
52027c478bd9Sstevel@tonic-gate  *	backtape - reposition tape after reading soft "EOF" record
52037c478bd9Sstevel@tonic-gate  *
52047c478bd9Sstevel@tonic-gate  *	Backtape tries to reposition the tape back over the EOF
52057c478bd9Sstevel@tonic-gate  *	record.  This is for the 'u' and 'r' function letters so that the
52067c478bd9Sstevel@tonic-gate  *	tape can be extended.  This code is not well designed, but
52077c478bd9Sstevel@tonic-gate  *	I'm confident that the only callers who care about the
52087c478bd9Sstevel@tonic-gate  *	backspace-over-EOF feature are those involved in 'u' and 'r'.
52097c478bd9Sstevel@tonic-gate  *
52107c478bd9Sstevel@tonic-gate  *	The proper way to backup the tape is through the use of mtio.
52117c478bd9Sstevel@tonic-gate  *	Earlier spins used lseek combined with reads in a confusing
52127c478bd9Sstevel@tonic-gate  *	maneuver that only worked on 4.x, but shouldn't have, even
52137c478bd9Sstevel@tonic-gate  *	there.  Lseeks are explicitly not supported for tape devices.
52147c478bd9Sstevel@tonic-gate  */
52157c478bd9Sstevel@tonic-gate 
52167c478bd9Sstevel@tonic-gate static void
backtape(void)52177c478bd9Sstevel@tonic-gate backtape(void)
52187c478bd9Sstevel@tonic-gate {
52197c478bd9Sstevel@tonic-gate 	struct mtop mtcmd;
52207c478bd9Sstevel@tonic-gate #ifdef DEBUG
52217c478bd9Sstevel@tonic-gate 	DEBUG("backtape() called, recno=%" FMT_blkcnt_t " nblock=%d\n", recno,
52227c478bd9Sstevel@tonic-gate 	    nblock);
52237c478bd9Sstevel@tonic-gate #endif
52247c478bd9Sstevel@tonic-gate 	/*
52257c478bd9Sstevel@tonic-gate 	 * Backup to the position in the archive where the record
52267c478bd9Sstevel@tonic-gate 	 * currently sitting in the tbuf buffer is situated.
52277c478bd9Sstevel@tonic-gate 	 */
52287c478bd9Sstevel@tonic-gate 
52297c478bd9Sstevel@tonic-gate 	if (NotTape) {
52307c478bd9Sstevel@tonic-gate 		/*
52317c478bd9Sstevel@tonic-gate 		 * For non-tape devices, this means lseeking to the
52327c478bd9Sstevel@tonic-gate 		 * correct position.  The absolute location tapepos-recno
52337c478bd9Sstevel@tonic-gate 		 * should be the beginning of the current record.
52347c478bd9Sstevel@tonic-gate 		 */
52357c478bd9Sstevel@tonic-gate 
52367c478bd9Sstevel@tonic-gate 		if (lseek(mt, (off_t)(TBLOCK*(tapepos-recno)), SEEK_SET) ==
52377c478bd9Sstevel@tonic-gate 		    (off_t)-1) {
52387c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
52397c478bd9Sstevel@tonic-gate 			    gettext("tar: lseek to end of archive failed\n"));
52407c478bd9Sstevel@tonic-gate 			done(4);
52417c478bd9Sstevel@tonic-gate 		}
52427c478bd9Sstevel@tonic-gate 	} else {
52437c478bd9Sstevel@tonic-gate 		/*
52447c478bd9Sstevel@tonic-gate 		 * For tape devices, we backup over the most recently
52457c478bd9Sstevel@tonic-gate 		 * read record.
52467c478bd9Sstevel@tonic-gate 		 */
52477c478bd9Sstevel@tonic-gate 
52487c478bd9Sstevel@tonic-gate 		mtcmd.mt_op = MTBSR;
52497c478bd9Sstevel@tonic-gate 		mtcmd.mt_count = 1;
52507c478bd9Sstevel@tonic-gate 
52517c478bd9Sstevel@tonic-gate 		if (ioctl(mt, MTIOCTOP, &mtcmd) < 0) {
52527c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
52537c478bd9Sstevel@tonic-gate 			    gettext("tar: backspace over record failed\n"));
52547c478bd9Sstevel@tonic-gate 			done(4);
52557c478bd9Sstevel@tonic-gate 		}
52567c478bd9Sstevel@tonic-gate 	}
52577c478bd9Sstevel@tonic-gate 
52587c478bd9Sstevel@tonic-gate 	/*
52597c478bd9Sstevel@tonic-gate 	 * Decrement the tape and tbuf buffer indices to prepare for the
52607c478bd9Sstevel@tonic-gate 	 * coming write to overwrite the soft EOF record.
52617c478bd9Sstevel@tonic-gate 	 */
52627c478bd9Sstevel@tonic-gate 
52637c478bd9Sstevel@tonic-gate 	recno--;
52647c478bd9Sstevel@tonic-gate 	tapepos--;
52657c478bd9Sstevel@tonic-gate }
52667c478bd9Sstevel@tonic-gate 
52677c478bd9Sstevel@tonic-gate 
52687c478bd9Sstevel@tonic-gate /*
52697c478bd9Sstevel@tonic-gate  *	flushtape  write buffered block(s) onto tape
52707c478bd9Sstevel@tonic-gate  *
52717c478bd9Sstevel@tonic-gate  *      recno points to next free block in tbuf.  If nonzero, a write is done.
52727c478bd9Sstevel@tonic-gate  *	Care is taken to write in multiples of SYS_BLOCK when device is
52737c478bd9Sstevel@tonic-gate  *	non-magtape in case raw i/o is used.
52747c478bd9Sstevel@tonic-gate  *
52757c478bd9Sstevel@tonic-gate  *	NOTE: this is called by writetape() to do the actual writing
52767c478bd9Sstevel@tonic-gate  */
52777c478bd9Sstevel@tonic-gate 
52787c478bd9Sstevel@tonic-gate static void
flushtape(void)52797c478bd9Sstevel@tonic-gate flushtape(void)
52807c478bd9Sstevel@tonic-gate {
52817c478bd9Sstevel@tonic-gate #ifdef DEBUG
52827c478bd9Sstevel@tonic-gate 	DEBUG("flushtape() called, recno=%" FMT_blkcnt_t "\n", recno, 0);
52837c478bd9Sstevel@tonic-gate #endif
52847c478bd9Sstevel@tonic-gate 	if (recno > 0) {	/* anything buffered? */
52857c478bd9Sstevel@tonic-gate 		if (NotTape) {
52867c478bd9Sstevel@tonic-gate #if SYS_BLOCK > TBLOCK
52877c478bd9Sstevel@tonic-gate 			int i;
52887c478bd9Sstevel@tonic-gate 
52897c478bd9Sstevel@tonic-gate 			/*
52907c478bd9Sstevel@tonic-gate 			 * an odd-block write can only happen when
52917c478bd9Sstevel@tonic-gate 			 * we are at the end of a volume that is not a tape.
52927c478bd9Sstevel@tonic-gate 			 * Here we round recno up to an even SYS_BLOCK
52937c478bd9Sstevel@tonic-gate 			 * boundary.
52947c478bd9Sstevel@tonic-gate 			 */
52957c478bd9Sstevel@tonic-gate 			if ((i = recno % (SYS_BLOCK / TBLOCK)) != 0) {
52967c478bd9Sstevel@tonic-gate #ifdef DEBUG
52977c478bd9Sstevel@tonic-gate 				DEBUG("flushtape() %d rounding blocks\n", i, 0);
52987c478bd9Sstevel@tonic-gate #endif
52997c478bd9Sstevel@tonic-gate 				recno += i;	/* round up to even SYS_BLOCK */
53007c478bd9Sstevel@tonic-gate 			}
53017c478bd9Sstevel@tonic-gate #endif
53027c478bd9Sstevel@tonic-gate 			if (recno > nblock)
53037c478bd9Sstevel@tonic-gate 				recno = nblock;
53047c478bd9Sstevel@tonic-gate 		}
53057c478bd9Sstevel@tonic-gate #ifdef DEBUG
53067c478bd9Sstevel@tonic-gate 		DEBUG("writing out %" FMT_blkcnt_t " blocks of %" FMT_blkcnt_t
53077c478bd9Sstevel@tonic-gate 		    " bytes\n", (blkcnt_t)(NotTape ? recno : nblock),
53087c478bd9Sstevel@tonic-gate 		    (blkcnt_t)(NotTape ? recno : nblock) * TBLOCK);
53097c478bd9Sstevel@tonic-gate #endif
53107c478bd9Sstevel@tonic-gate 		if (write(mt, tbuf,
53117c478bd9Sstevel@tonic-gate 		    (size_t)(NotTape ? recno : nblock) * TBLOCK) < 0) {
53127c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
53137c478bd9Sstevel@tonic-gate 			    "tar: tape write error\n"));
53147c478bd9Sstevel@tonic-gate 			done(2);
53157c478bd9Sstevel@tonic-gate 		}
53167c478bd9Sstevel@tonic-gate 		recno = 0;
53177c478bd9Sstevel@tonic-gate 	}
53187c478bd9Sstevel@tonic-gate }
53197c478bd9Sstevel@tonic-gate 
53207c478bd9Sstevel@tonic-gate static void
copy(void * dst,void * src)53217c478bd9Sstevel@tonic-gate copy(void *dst, void *src)
53227c478bd9Sstevel@tonic-gate {
53237c478bd9Sstevel@tonic-gate 	(void) memcpy(dst, src, TBLOCK);
53247c478bd9Sstevel@tonic-gate }
53257c478bd9Sstevel@tonic-gate 
53267c478bd9Sstevel@tonic-gate /*
53277c478bd9Sstevel@tonic-gate  * kcheck()
53287c478bd9Sstevel@tonic-gate  *	- checks the validity of size values for non-tape devices
53297c478bd9Sstevel@tonic-gate  *	- if size is zero, mulvol tar is disabled and size is
53307c478bd9Sstevel@tonic-gate  *	  assumed to be infinite.
53317c478bd9Sstevel@tonic-gate  *	- returns volume size in TBLOCKS
53327c478bd9Sstevel@tonic-gate  */
53337c478bd9Sstevel@tonic-gate 
53347c478bd9Sstevel@tonic-gate static blkcnt_t
kcheck(char * kstr)53357c478bd9Sstevel@tonic-gate kcheck(char *kstr)
53367c478bd9Sstevel@tonic-gate {
53377c478bd9Sstevel@tonic-gate 	blkcnt_t kval;
53387c478bd9Sstevel@tonic-gate 
53397c478bd9Sstevel@tonic-gate 	kval = strtoll(kstr, NULL, 0);
53407c478bd9Sstevel@tonic-gate 	if (kval == (blkcnt_t)0) {	/* no multi-volume; size is infinity. */
53417c478bd9Sstevel@tonic-gate 		mulvol = 0;	/* definitely not mulvol, but we must  */
53427c478bd9Sstevel@tonic-gate 		return (0);	/* took out setting of NotTape */
53437c478bd9Sstevel@tonic-gate 	}
53447c478bd9Sstevel@tonic-gate 	if (kval < (blkcnt_t)MINSIZE) {
53457c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
53467c478bd9Sstevel@tonic-gate 		    "tar: sizes below %luK not supported (%" FMT_blkcnt_t
53477c478bd9Sstevel@tonic-gate 		    ").\n"), (ulong_t)MINSIZE, kval);
53487c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
53497c478bd9Sstevel@tonic-gate 		    "bad size entry for %s in %s.\n"),
53507c478bd9Sstevel@tonic-gate 		    archive, DEF_FILE);
53517c478bd9Sstevel@tonic-gate 		done(1);
53527c478bd9Sstevel@tonic-gate 	}
53537c478bd9Sstevel@tonic-gate 	mulvol++;
53547c478bd9Sstevel@tonic-gate 	NotTape++;			/* implies non-tape */
53557c478bd9Sstevel@tonic-gate 	return (kval * 1024 / TBLOCK);	/* convert to TBLOCKS */
53567c478bd9Sstevel@tonic-gate }
53577c478bd9Sstevel@tonic-gate 
53587c478bd9Sstevel@tonic-gate 
53597c478bd9Sstevel@tonic-gate /*
53607c478bd9Sstevel@tonic-gate  * bcheck()
53617c478bd9Sstevel@tonic-gate  *	- checks the validity of blocking factors
53627c478bd9Sstevel@tonic-gate  *	- returns blocking factor
53637c478bd9Sstevel@tonic-gate  */
53647c478bd9Sstevel@tonic-gate 
53657c478bd9Sstevel@tonic-gate static int
bcheck(char * bstr)53667c478bd9Sstevel@tonic-gate bcheck(char *bstr)
53677c478bd9Sstevel@tonic-gate {
53687c478bd9Sstevel@tonic-gate 	blkcnt_t bval;
53697c478bd9Sstevel@tonic-gate 
53707c478bd9Sstevel@tonic-gate 	bval = strtoll(bstr, NULL, 0);
53717c478bd9Sstevel@tonic-gate 	if ((bval <= 0) || (bval > INT_MAX / TBLOCK)) {
53727c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
53737c478bd9Sstevel@tonic-gate 		    "tar: invalid blocksize \"%s\".\n"), bstr);
53747c478bd9Sstevel@tonic-gate 		if (!bflag)
53757c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
53767c478bd9Sstevel@tonic-gate 			    "bad blocksize entry for '%s' in %s.\n"),
53777c478bd9Sstevel@tonic-gate 			    archive, DEF_FILE);
53787c478bd9Sstevel@tonic-gate 		done(1);
53797c478bd9Sstevel@tonic-gate 	}
53807c478bd9Sstevel@tonic-gate 
53817c478bd9Sstevel@tonic-gate 	return ((int)bval);
53827c478bd9Sstevel@tonic-gate }
53837c478bd9Sstevel@tonic-gate 
53847c478bd9Sstevel@tonic-gate 
53857c478bd9Sstevel@tonic-gate /*
53867c478bd9Sstevel@tonic-gate  * defset()
53877c478bd9Sstevel@tonic-gate  *	- reads DEF_FILE for the set of default values specified.
53887c478bd9Sstevel@tonic-gate  *	- initializes 'usefile', 'nblock', and 'blocklim', and 'NotTape'.
53897c478bd9Sstevel@tonic-gate  *	- 'usefile' points to static data, so will be overwritten
53907c478bd9Sstevel@tonic-gate  *	  if this routine is called a second time.
53917c478bd9Sstevel@tonic-gate  *	- the pattern specified by 'arch' must be followed by four
53927c478bd9Sstevel@tonic-gate  *	  blank-separated fields (1) device (2) blocking,
53937c478bd9Sstevel@tonic-gate  *				 (3) size(K), and (4) tape
53947c478bd9Sstevel@tonic-gate  *	  for example: archive0=/dev/fd 1 400 n
53957c478bd9Sstevel@tonic-gate  */
53967c478bd9Sstevel@tonic-gate 
53977c478bd9Sstevel@tonic-gate static int
defset(char * arch)53987c478bd9Sstevel@tonic-gate defset(char *arch)
53997c478bd9Sstevel@tonic-gate {
54007c478bd9Sstevel@tonic-gate 	char *bp;
54017c478bd9Sstevel@tonic-gate 
54027c478bd9Sstevel@tonic-gate 	if (defopen(DEF_FILE) != 0)
54037c478bd9Sstevel@tonic-gate 		return (FALSE);
54047c478bd9Sstevel@tonic-gate 	if (defcntl(DC_SETFLAGS, (DC_STD & ~(DC_CASE))) == -1) {
54057c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
54067c478bd9Sstevel@tonic-gate 		    "tar: error setting parameters for %s.\n"), DEF_FILE);
54077c478bd9Sstevel@tonic-gate 		return (FALSE);			/* & following ones too */
54087c478bd9Sstevel@tonic-gate 	}
54097c478bd9Sstevel@tonic-gate 	if ((bp = defread(arch)) == NULL) {
54107c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
54117c478bd9Sstevel@tonic-gate 		    "tar: missing or invalid '%s' entry in %s.\n"),
54127c478bd9Sstevel@tonic-gate 		    arch, DEF_FILE);
54137c478bd9Sstevel@tonic-gate 		return (FALSE);
54147c478bd9Sstevel@tonic-gate 	}
54157c478bd9Sstevel@tonic-gate 	if ((usefile = strtok(bp, " \t")) == NULL) {
54167c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
54177c478bd9Sstevel@tonic-gate 		    "tar: '%s' entry in %s is empty!\n"), arch, DEF_FILE);
54187c478bd9Sstevel@tonic-gate 		return (FALSE);
54197c478bd9Sstevel@tonic-gate 	}
54207c478bd9Sstevel@tonic-gate 	if ((bp = strtok(NULL, " \t")) == NULL) {
54217c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
54227c478bd9Sstevel@tonic-gate 		    "tar: block component missing in '%s' entry in %s.\n"),
54237c478bd9Sstevel@tonic-gate 		    arch, DEF_FILE);
54247c478bd9Sstevel@tonic-gate 		return (FALSE);
54257c478bd9Sstevel@tonic-gate 	}
54267c478bd9Sstevel@tonic-gate 	nblock = bcheck(bp);
54277c478bd9Sstevel@tonic-gate 	if ((bp = strtok(NULL, " \t")) == NULL) {
54287c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
54297c478bd9Sstevel@tonic-gate 		    "tar: size component missing in '%s' entry in %s.\n"),
54307c478bd9Sstevel@tonic-gate 		    arch, DEF_FILE);
54317c478bd9Sstevel@tonic-gate 		return (FALSE);
54327c478bd9Sstevel@tonic-gate 	}
54337c478bd9Sstevel@tonic-gate 	blocklim = kcheck(bp);
54347c478bd9Sstevel@tonic-gate 	if ((bp = strtok(NULL, " \t")) != NULL)
54357c478bd9Sstevel@tonic-gate 		NotTape = (*bp == 'n' || *bp == 'N');
54367c478bd9Sstevel@tonic-gate 	else
54377c478bd9Sstevel@tonic-gate 		NotTape = (blocklim != 0);
54387c478bd9Sstevel@tonic-gate 	(void) defopen(NULL);
54397c478bd9Sstevel@tonic-gate #ifdef DEBUG
54407c478bd9Sstevel@tonic-gate 	DEBUG("defset: archive='%s'; usefile='%s'\n", arch, usefile);
54417c478bd9Sstevel@tonic-gate 	DEBUG("defset: nblock='%d'; blocklim='%" FMT_blkcnt_t "'\n",
54427c478bd9Sstevel@tonic-gate 	    nblock, blocklim);
54437c478bd9Sstevel@tonic-gate 	DEBUG("defset: not tape = %d\n", NotTape, 0);
54447c478bd9Sstevel@tonic-gate #endif
54457c478bd9Sstevel@tonic-gate 	return (TRUE);
54467c478bd9Sstevel@tonic-gate }
54477c478bd9Sstevel@tonic-gate 
54487c478bd9Sstevel@tonic-gate 
54497c478bd9Sstevel@tonic-gate /*
54507c478bd9Sstevel@tonic-gate  * Following code handles excluded and included files.
54517c478bd9Sstevel@tonic-gate  * A hash table of file names to be {in,ex}cluded is built.
54527c478bd9Sstevel@tonic-gate  * For excluded files, before writing or extracting a file
54537c478bd9Sstevel@tonic-gate  * check to see if it is in the exclude_tbl.
54547c478bd9Sstevel@tonic-gate  * For included files, the wantit() procedure will check to
54557c478bd9Sstevel@tonic-gate  * see if the named file is in the include_tbl.
54567c478bd9Sstevel@tonic-gate  */
54577c478bd9Sstevel@tonic-gate 
54587c478bd9Sstevel@tonic-gate static void
build_table(file_list_t * table[],char * file)54595e2174acSceastha build_table(file_list_t *table[], char *file)
54607c478bd9Sstevel@tonic-gate {
54617c478bd9Sstevel@tonic-gate 	FILE	*fp;
54627c478bd9Sstevel@tonic-gate 	char	buf[PATH_MAX + 1];
54637c478bd9Sstevel@tonic-gate 
54647c478bd9Sstevel@tonic-gate 	if ((fp = fopen(file, "r")) == (FILE *)NULL)
54657c478bd9Sstevel@tonic-gate 		vperror(1, gettext("could not open %s"), file);
54667c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
54677c478bd9Sstevel@tonic-gate 		if (buf[strlen(buf) - 1] == '\n')
54687c478bd9Sstevel@tonic-gate 			buf[strlen(buf) - 1] = '\0';
54697c478bd9Sstevel@tonic-gate 		/* Only add to table if line has something in it */
54707c478bd9Sstevel@tonic-gate 		if (strspn(buf, " \t") != strlen(buf))
54717c478bd9Sstevel@tonic-gate 			add_file_to_table(table, buf);
54727c478bd9Sstevel@tonic-gate 	}
54737c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
54747c478bd9Sstevel@tonic-gate }
54757c478bd9Sstevel@tonic-gate 
54767c478bd9Sstevel@tonic-gate 
54777c478bd9Sstevel@tonic-gate /*
54787c478bd9Sstevel@tonic-gate  * Add a file name to the the specified table, if the file name has any
54797c478bd9Sstevel@tonic-gate  * trailing '/'s then delete them before inserting into the table
54807c478bd9Sstevel@tonic-gate  */
54817c478bd9Sstevel@tonic-gate 
54827c478bd9Sstevel@tonic-gate static void
add_file_to_table(file_list_t * table[],char * str)54835e2174acSceastha add_file_to_table(file_list_t *table[], char *str)
54847c478bd9Sstevel@tonic-gate {
54857c478bd9Sstevel@tonic-gate 	char	name[PATH_MAX + 1];
54867c478bd9Sstevel@tonic-gate 	unsigned int h;
54875e2174acSceastha 	file_list_t	*exp;
54887c478bd9Sstevel@tonic-gate 
54897c478bd9Sstevel@tonic-gate 	(void) strcpy(name, str);
54907c478bd9Sstevel@tonic-gate 	while (name[strlen(name) - 1] == '/') {
54917c478bd9Sstevel@tonic-gate 		name[strlen(name) - 1] = NULL;
54927c478bd9Sstevel@tonic-gate 	}
54937c478bd9Sstevel@tonic-gate 
54947c478bd9Sstevel@tonic-gate 	h = hash(name);
54955e2174acSceastha 	if ((exp = (file_list_t *)calloc(sizeof (file_list_t),
54967c478bd9Sstevel@tonic-gate 	    sizeof (char))) == NULL) {
54977c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
54987c478bd9Sstevel@tonic-gate 		    "tar: out of memory, exclude/include table(entry)\n"));
54997c478bd9Sstevel@tonic-gate 		exit(1);
55007c478bd9Sstevel@tonic-gate 	}
55017c478bd9Sstevel@tonic-gate 
55027c478bd9Sstevel@tonic-gate 	if ((exp->name = strdup(name)) == NULL) {
55037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
55047c478bd9Sstevel@tonic-gate 		    "tar: out of memory, exclude/include table(file name)\n"));
55057c478bd9Sstevel@tonic-gate 		exit(1);
55067c478bd9Sstevel@tonic-gate 	}
55077c478bd9Sstevel@tonic-gate 
55087c478bd9Sstevel@tonic-gate 	exp->next = table[h];
55097c478bd9Sstevel@tonic-gate 	table[h] = exp;
55107c478bd9Sstevel@tonic-gate }
55117c478bd9Sstevel@tonic-gate 
55127c478bd9Sstevel@tonic-gate 
55137c478bd9Sstevel@tonic-gate /*
55147c478bd9Sstevel@tonic-gate  * See if a file name or any of the file's parent directories is in the
55157c478bd9Sstevel@tonic-gate  * specified table, if the file name has any trailing '/'s then delete
55167c478bd9Sstevel@tonic-gate  * them before searching the table
55177c478bd9Sstevel@tonic-gate  */
55187c478bd9Sstevel@tonic-gate 
55197c478bd9Sstevel@tonic-gate static int
is_in_table(file_list_t * table[],char * str)55205e2174acSceastha is_in_table(file_list_t *table[], char *str)
55217c478bd9Sstevel@tonic-gate {
55227c478bd9Sstevel@tonic-gate 	char	name[PATH_MAX + 1];
55237c478bd9Sstevel@tonic-gate 	unsigned int	h;
55245e2174acSceastha 	file_list_t	*exp;
55257c478bd9Sstevel@tonic-gate 	char	*ptr;
55267c478bd9Sstevel@tonic-gate 
55277c478bd9Sstevel@tonic-gate 	(void) strcpy(name, str);
55287c478bd9Sstevel@tonic-gate 	while (name[strlen(name) - 1] == '/') {
55297c478bd9Sstevel@tonic-gate 		name[strlen(name) - 1] = NULL;
55307c478bd9Sstevel@tonic-gate 	}
55317c478bd9Sstevel@tonic-gate 
55327c478bd9Sstevel@tonic-gate 	/*
55337c478bd9Sstevel@tonic-gate 	 * check for the file name in the passed list
55347c478bd9Sstevel@tonic-gate 	 */
55357c478bd9Sstevel@tonic-gate 	h = hash(name);
55367c478bd9Sstevel@tonic-gate 	exp = table[h];
55377c478bd9Sstevel@tonic-gate 	while (exp != NULL) {
55387c478bd9Sstevel@tonic-gate 		if (strcmp(name, exp->name) == 0) {
55397c478bd9Sstevel@tonic-gate 			return (1);
55407c478bd9Sstevel@tonic-gate 		}
55417c478bd9Sstevel@tonic-gate 		exp = exp->next;
55427c478bd9Sstevel@tonic-gate 	}
55437c478bd9Sstevel@tonic-gate 
55447c478bd9Sstevel@tonic-gate 	/*
55457c478bd9Sstevel@tonic-gate 	 * check for any parent directories in the file list
55467c478bd9Sstevel@tonic-gate 	 */
55477c478bd9Sstevel@tonic-gate 	while ((ptr = strrchr(name, '/'))) {
55487c478bd9Sstevel@tonic-gate 		*ptr = NULL;
55497c478bd9Sstevel@tonic-gate 		h = hash(name);
55507c478bd9Sstevel@tonic-gate 		exp = table[h];
55517c478bd9Sstevel@tonic-gate 		while (exp != NULL) {
55527c478bd9Sstevel@tonic-gate 			if (strcmp(name, exp->name) == 0) {
55537c478bd9Sstevel@tonic-gate 				return (1);
55547c478bd9Sstevel@tonic-gate 			}
55557c478bd9Sstevel@tonic-gate 			exp = exp->next;
55567c478bd9Sstevel@tonic-gate 		}
55577c478bd9Sstevel@tonic-gate 	}
55587c478bd9Sstevel@tonic-gate 
55597c478bd9Sstevel@tonic-gate 	return (0);
55607c478bd9Sstevel@tonic-gate }
55617c478bd9Sstevel@tonic-gate 
55627c478bd9Sstevel@tonic-gate 
55637c478bd9Sstevel@tonic-gate /*
55647c478bd9Sstevel@tonic-gate  * Compute a hash from a string.
55657c478bd9Sstevel@tonic-gate  */
55667c478bd9Sstevel@tonic-gate 
55677c478bd9Sstevel@tonic-gate static unsigned int
hash(char * str)55687c478bd9Sstevel@tonic-gate hash(char *str)
55697c478bd9Sstevel@tonic-gate {
55707c478bd9Sstevel@tonic-gate 	char	*cp;
55717c478bd9Sstevel@tonic-gate 	unsigned int	h;
55727c478bd9Sstevel@tonic-gate 
55737c478bd9Sstevel@tonic-gate 	h = 0;
55747c478bd9Sstevel@tonic-gate 	for (cp = str; *cp; cp++) {
55757c478bd9Sstevel@tonic-gate 		h += *cp;
55767c478bd9Sstevel@tonic-gate 	}
55777c478bd9Sstevel@tonic-gate 	return (h % TABLE_SIZE);
55787c478bd9Sstevel@tonic-gate }
55797c478bd9Sstevel@tonic-gate 
55807c478bd9Sstevel@tonic-gate static	void *
getmem(size_t size)55817c478bd9Sstevel@tonic-gate getmem(size_t size)
55827c478bd9Sstevel@tonic-gate {
55837c478bd9Sstevel@tonic-gate 	void *p = calloc((unsigned)size, sizeof (char));
55847c478bd9Sstevel@tonic-gate 
55857c478bd9Sstevel@tonic-gate 	if (p == NULL && freemem) {
55867c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
55877c478bd9Sstevel@tonic-gate 		    "tar: out of memory, link and directory modtime "
55887c478bd9Sstevel@tonic-gate 		    "info lost\n"));
55897c478bd9Sstevel@tonic-gate 		freemem = 0;
55907c478bd9Sstevel@tonic-gate 		if (errflag)
55917c478bd9Sstevel@tonic-gate 			done(1);
55927c478bd9Sstevel@tonic-gate 		else
55937c478bd9Sstevel@tonic-gate 			Errflg = 1;
55947c478bd9Sstevel@tonic-gate 	}
55957c478bd9Sstevel@tonic-gate 	return (p);
55967c478bd9Sstevel@tonic-gate }
55977c478bd9Sstevel@tonic-gate 
55987c478bd9Sstevel@tonic-gate /*
55997c478bd9Sstevel@tonic-gate  * vperror() --variable argument perror.
56007c478bd9Sstevel@tonic-gate  * Takes 3 args: exit_status, formats, args.  If exit_status is 0, then
56017c478bd9Sstevel@tonic-gate  * the errflag (exit on error) is checked -- if it is non-zero, tar exits
56027c478bd9Sstevel@tonic-gate  * with the value of whatever "errno" is set to.  If exit_status is not
56037c478bd9Sstevel@tonic-gate  * zero, then tar exits with that error status. If errflag and exit_status
56047c478bd9Sstevel@tonic-gate  * are both zero, the routine returns to where it was called and sets Errflg
56057c478bd9Sstevel@tonic-gate  * to errno.
56067c478bd9Sstevel@tonic-gate  */
56077c478bd9Sstevel@tonic-gate 
56087c478bd9Sstevel@tonic-gate static void
vperror(int exit_status,char * fmt,...)56097c478bd9Sstevel@tonic-gate vperror(int exit_status, char *fmt, ...)
56107c478bd9Sstevel@tonic-gate {
56117c478bd9Sstevel@tonic-gate 	va_list	ap;
56127c478bd9Sstevel@tonic-gate 
56137c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
56147c478bd9Sstevel@tonic-gate 	(void) fputs("tar: ", stderr);
56157c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
56167c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, ": %s\n", strerror(errno));
56177c478bd9Sstevel@tonic-gate 	va_end(ap);
56187c478bd9Sstevel@tonic-gate 	if (exit_status)
56197c478bd9Sstevel@tonic-gate 		done(exit_status);
56207c478bd9Sstevel@tonic-gate 	else
56217c478bd9Sstevel@tonic-gate 		if (errflag)
56227c478bd9Sstevel@tonic-gate 			done(errno);
56237c478bd9Sstevel@tonic-gate 		else
56247c478bd9Sstevel@tonic-gate 			Errflg = errno;
56257c478bd9Sstevel@tonic-gate }
56267c478bd9Sstevel@tonic-gate 
56277c478bd9Sstevel@tonic-gate 
56287c478bd9Sstevel@tonic-gate static void
fatal(char * format,...)56297c478bd9Sstevel@tonic-gate fatal(char *format, ...)
56307c478bd9Sstevel@tonic-gate {
56317c478bd9Sstevel@tonic-gate 	va_list	ap;
56327c478bd9Sstevel@tonic-gate 
56337c478bd9Sstevel@tonic-gate 	va_start(ap, format);
56347c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "tar: ");
56357c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
56367c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
56377c478bd9Sstevel@tonic-gate 	va_end(ap);
56387c478bd9Sstevel@tonic-gate 	done(1);
56397c478bd9Sstevel@tonic-gate }
56407c478bd9Sstevel@tonic-gate 
56417c478bd9Sstevel@tonic-gate 
56427c478bd9Sstevel@tonic-gate /*
56437c478bd9Sstevel@tonic-gate  * Check to make sure that argument is a char * ptr.
56447c478bd9Sstevel@tonic-gate  * Actually, we just check to see that it is non-null.
56457c478bd9Sstevel@tonic-gate  * If it is null, print out the message and call usage(), bailing out.
56467c478bd9Sstevel@tonic-gate  */
56477c478bd9Sstevel@tonic-gate 
56487c478bd9Sstevel@tonic-gate static void
assert_string(char * s,char * msg)56497c478bd9Sstevel@tonic-gate assert_string(char *s, char *msg)
56507c478bd9Sstevel@tonic-gate {
56517c478bd9Sstevel@tonic-gate 	if (s == NULL) {
56527c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, msg);
56537c478bd9Sstevel@tonic-gate 		usage();
56547c478bd9Sstevel@tonic-gate 	}
56557c478bd9Sstevel@tonic-gate }
56567c478bd9Sstevel@tonic-gate 
56577c478bd9Sstevel@tonic-gate 
56587c478bd9Sstevel@tonic-gate static void
mterr(char * operation,int i,int exitcode)56597c478bd9Sstevel@tonic-gate mterr(char *operation, int i, int exitcode)
56607c478bd9Sstevel@tonic-gate {
56617c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
56627c478bd9Sstevel@tonic-gate 	    "tar: %s error: "), operation);
56637c478bd9Sstevel@tonic-gate 	if (i < 0)
56647c478bd9Sstevel@tonic-gate 		perror("");
56657c478bd9Sstevel@tonic-gate 	else
56667c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("unexpected EOF\n"));
56677c478bd9Sstevel@tonic-gate 	done(exitcode);
56687c478bd9Sstevel@tonic-gate }
56697c478bd9Sstevel@tonic-gate 
56707c478bd9Sstevel@tonic-gate static int
wantit(char * argv[],char ** namep,char ** dirp,char ** component,attr_data_t ** attrinfo)5671da6c28aaSamw wantit(char *argv[], char **namep, char **dirp, char **component,
5672da6c28aaSamw     attr_data_t **attrinfo)
56737c478bd9Sstevel@tonic-gate {
56747c478bd9Sstevel@tonic-gate 	char **cp;
56757c478bd9Sstevel@tonic-gate 	int gotit;		/* true if we've found a match */
5676123523f8Sas158974 	int ret;
56777c478bd9Sstevel@tonic-gate 
56787c478bd9Sstevel@tonic-gate top:
5679123523f8Sas158974 	if (xhdr_flgs & _X_XHDR) {
56807c478bd9Sstevel@tonic-gate 		xhdr_flgs = 0;
5681123523f8Sas158974 	}
56827c478bd9Sstevel@tonic-gate 	getdir();
56837c478bd9Sstevel@tonic-gate 	if (Xhdrflag > 0) {
5684123523f8Sas158974 		ret = get_xdata();
5685123523f8Sas158974 		if (ret != 0) {	/* Xhdr items and regular header */
5686123523f8Sas158974 			setbytes_to_skip(&stbuf, ret);
56877c478bd9Sstevel@tonic-gate 			passtape();
56887c478bd9Sstevel@tonic-gate 			return (0);	/* Error--don't want to extract  */
56897c478bd9Sstevel@tonic-gate 		}
56907c478bd9Sstevel@tonic-gate 	}
56917c478bd9Sstevel@tonic-gate 
5692123523f8Sas158974 	/*
5693123523f8Sas158974 	 * If typeflag is not 'A' and xhdr_flgs is set, then processing
5694123523f8Sas158974 	 * of ancillary file is either over or ancillary file
5695123523f8Sas158974 	 * processing is not required, load info from Xtarhdr and set
5696123523f8Sas158974 	 * _X_XHDR bit in xhdr_flgs.
5697123523f8Sas158974 	 */
5698123523f8Sas158974 	if ((dblock.dbuf.typeflag != 'A') && (xhdr_flgs != 0)) {
5699123523f8Sas158974 		load_info_from_xtarhdr(xhdr_flgs, &Xtarhdr);
5700123523f8Sas158974 		xhdr_flgs |= _X_XHDR;
5701123523f8Sas158974 	}
5702123523f8Sas158974 
57037c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
57047c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.typeflag == _XATTR_HDRTYPE && xattrbadhead == 0) {
5705da6c28aaSamw 		/*
5706da6c28aaSamw 		 * Always needs to read the extended header.  If atflag, saflag,
5707da6c28aaSamw 		 * or tflag isn't set, then we'll have the correct info for
5708da6c28aaSamw 		 * passtape() later.
5709da6c28aaSamw 		 */
5710da6c28aaSamw 		(void) read_xattr_hdr(attrinfo);
57117c478bd9Sstevel@tonic-gate 		goto top;
57127c478bd9Sstevel@tonic-gate 	}
5713da6c28aaSamw 	/*
5714ced83f9bSceastha 	 * Now that we've read the extended header, call passtape()
5715ced83f9bSceastha 	 * if we don't want to restore attributes or system attributes.
5716ced83f9bSceastha 	 * Don't restore the attribute if we are extracting
5717ced83f9bSceastha 	 * a file from an archive (as opposed to doing a table of
5718ced83f9bSceastha 	 * contents) and any of the following are true:
5719ced83f9bSceastha 	 * 1. neither -@ or -/ was specified.
5720ced83f9bSceastha 	 * 2. -@ was specified, -/ wasn't specified, and we're
5721ced83f9bSceastha 	 * processing a hidden attribute directory of an attribute
5722ced83f9bSceastha 	 * or we're processing a read-write system attribute file.
5723ced83f9bSceastha 	 * 3. -@ wasn't specified, -/ was specified, and the file
5724ced83f9bSceastha 	 * we're processing is not a read-write system attribute file,
5725ced83f9bSceastha 	 * or we're processing the hidden attribute directory of an
5726ced83f9bSceastha 	 * attribute.
5727ced83f9bSceastha 	 *
5728ced83f9bSceastha 	 * We always process the attributes if we're just generating
5729ced83f9bSceastha 	 * generating a table of contents, or if both -@ and -/ were
5730ced83f9bSceastha 	 * specified.
5731da6c28aaSamw 	 */
5732ced83f9bSceastha 	if (xattrp != NULL) {
5733ced83f9bSceastha 		attr_data_t *ainfo = *attrinfo;
5734ced83f9bSceastha 
5735ced83f9bSceastha 		if (!tflag &&
5736ced83f9bSceastha 		    ((!atflag && !saflag) ||
5737ced83f9bSceastha 		    (atflag && !saflag && ((ainfo->attr_parent != NULL) ||
5738ced83f9bSceastha 		    ainfo->attr_rw_sysattr)) ||
5739ced83f9bSceastha 		    (!atflag && saflag && ((ainfo->attr_parent != NULL) ||
5740ced83f9bSceastha 		    !ainfo->attr_rw_sysattr)))) {
5741da6c28aaSamw 			passtape();
5742da6c28aaSamw 			return (0);
5743da6c28aaSamw 		}
5744ced83f9bSceastha 	}
57457c478bd9Sstevel@tonic-gate #endif
57467c478bd9Sstevel@tonic-gate 
57477c478bd9Sstevel@tonic-gate 	/* sets *namep to point at the proper name */
5748da6c28aaSamw 	if (check_prefix(namep, dirp, component) != 0) {
5749da6c28aaSamw 		passtape();
5750da6c28aaSamw 		return (0);
5751da6c28aaSamw 	}
57527c478bd9Sstevel@tonic-gate 
57537c478bd9Sstevel@tonic-gate 	if (endtape()) {
57547c478bd9Sstevel@tonic-gate 		if (Bflag) {
57557c478bd9Sstevel@tonic-gate 			/*
57567c478bd9Sstevel@tonic-gate 			 * Logically at EOT - consume any extra blocks
57577c478bd9Sstevel@tonic-gate 			 * so that write to our stdin won't fail and
57587c478bd9Sstevel@tonic-gate 			 * emit an error message; otherwise something
57597c478bd9Sstevel@tonic-gate 			 * like "dd if=foo.tar | (cd bar; tar xvf -)"
57607c478bd9Sstevel@tonic-gate 			 * will produce a bogus error message from "dd".
57617c478bd9Sstevel@tonic-gate 			 */
57627c478bd9Sstevel@tonic-gate 
57637c478bd9Sstevel@tonic-gate 			while (read(mt, tbuf, TBLOCK*nblock) > 0) {
57647c478bd9Sstevel@tonic-gate 				/* empty body */
57657c478bd9Sstevel@tonic-gate 			}
57667c478bd9Sstevel@tonic-gate 		}
57677c478bd9Sstevel@tonic-gate 		return (-1);
57687c478bd9Sstevel@tonic-gate 	}
57697c478bd9Sstevel@tonic-gate 
57707c478bd9Sstevel@tonic-gate 	gotit = 0;
57717c478bd9Sstevel@tonic-gate 
57727c478bd9Sstevel@tonic-gate 	if ((Iflag && is_in_table(include_tbl, *namep)) ||
57737c478bd9Sstevel@tonic-gate 	    (! Iflag && *argv == NULL)) {
57747c478bd9Sstevel@tonic-gate 		gotit = 1;
57757c478bd9Sstevel@tonic-gate 	} else {
57767c478bd9Sstevel@tonic-gate 		for (cp = argv; *cp; cp++) {
57777c478bd9Sstevel@tonic-gate 			if (is_prefix(*cp, *namep)) {
57787c478bd9Sstevel@tonic-gate 				gotit = 1;
57797c478bd9Sstevel@tonic-gate 				break;
57807c478bd9Sstevel@tonic-gate 			}
57817c478bd9Sstevel@tonic-gate 		}
57827c478bd9Sstevel@tonic-gate 	}
57837c478bd9Sstevel@tonic-gate 
57847c478bd9Sstevel@tonic-gate 	if (! gotit) {
57857c478bd9Sstevel@tonic-gate 		passtape();
57867c478bd9Sstevel@tonic-gate 		return (0);
57877c478bd9Sstevel@tonic-gate 	}
57887c478bd9Sstevel@tonic-gate 
57897c478bd9Sstevel@tonic-gate 	if (Xflag && is_in_table(exclude_tbl, *namep)) {
57907c478bd9Sstevel@tonic-gate 		if (vflag) {
57917c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s excluded\n"),
57927c478bd9Sstevel@tonic-gate 			    *namep);
57937c478bd9Sstevel@tonic-gate 		}
57947c478bd9Sstevel@tonic-gate 		passtape();
57957c478bd9Sstevel@tonic-gate 		return (0);
57967c478bd9Sstevel@tonic-gate 	}
57977c478bd9Sstevel@tonic-gate 
57987c478bd9Sstevel@tonic-gate 	return (1);
57997c478bd9Sstevel@tonic-gate }
58007c478bd9Sstevel@tonic-gate 
5801123523f8Sas158974 
5802123523f8Sas158974 static void
setbytes_to_skip(struct stat * st,int err)5803123523f8Sas158974 setbytes_to_skip(struct stat *st, int err)
5804123523f8Sas158974 {
5805123523f8Sas158974 	/*
5806123523f8Sas158974 	 * In a scenario where a typeflag 'X' was followed by
5807123523f8Sas158974 	 * a typeflag 'A' and typeflag 'O', then the number of
5808123523f8Sas158974 	 * bytes to skip should be the size of ancillary file,
5809123523f8Sas158974 	 * plus the dblock for regular file, and the size
5810123523f8Sas158974 	 * from Xtarhdr. However, if the typeflag was just 'X'
5811123523f8Sas158974 	 * followed by typeflag 'O', then the number of bytes
5812123523f8Sas158974 	 * to skip should be the size from Xtarhdr.
5813123523f8Sas158974 	 */
5814123523f8Sas158974 	if ((err != 0) && (dblock.dbuf.typeflag == 'A') &&
5815257ece65SRalph Turner - Sun UK - Contractor 	    (xhdr_flgs & _X_SIZE)) {
5816ced83f9bSceastha 		st->st_size += TBLOCK + Xtarhdr.x_filesz;
5817123523f8Sas158974 		xhdr_flgs |= _X_XHDR;
5818123523f8Sas158974 	} else if ((dblock.dbuf.typeflag != 'A') &&
5819257ece65SRalph Turner - Sun UK - Contractor 	    (xhdr_flgs & _X_SIZE)) {
5820257ece65SRalph Turner - Sun UK - Contractor 		st->st_size += Xtarhdr.x_filesz;
5821123523f8Sas158974 		xhdr_flgs |= _X_XHDR;
5822123523f8Sas158974 	}
5823123523f8Sas158974 }
5824123523f8Sas158974 
5825da6c28aaSamw static int
fill_in_attr_info(char * attr,char * longname,char * attrparent,int atparentfd,int rw_sysattr,attr_data_t ** attrinfo)5826da6c28aaSamw fill_in_attr_info(char *attr, char *longname, char *attrparent, int atparentfd,
5827da6c28aaSamw     int rw_sysattr, attr_data_t **attrinfo)
5828da6c28aaSamw {
5829da6c28aaSamw 	size_t	pathlen;
5830da6c28aaSamw 	char	*tpath;
5831da6c28aaSamw 	char	*tparent;
5832da6c28aaSamw 
5833da6c28aaSamw 	/* parent info */
5834da6c28aaSamw 	if (attrparent != NULL) {
5835da6c28aaSamw 		if ((tparent = strdup(attrparent)) == NULL) {
5836da6c28aaSamw 			vperror(0, gettext(
5837da6c28aaSamw 			    "unable to allocate memory for attribute parent "
5838da6c28aaSamw 			    "name for %sattribute %s/%s of %s"),
5839da6c28aaSamw 			    rw_sysattr ? gettext("system ") : "",
5840da6c28aaSamw 			    attrparent, attr, longname);
5841da6c28aaSamw 			return (1);
5842da6c28aaSamw 		}
5843da6c28aaSamw 	} else {
5844da6c28aaSamw 		tparent = NULL;
5845da6c28aaSamw 	}
5846da6c28aaSamw 
5847da6c28aaSamw 	/* path info */
5848da6c28aaSamw 	pathlen = strlen(attr) + 1;
5849da6c28aaSamw 	if (attrparent != NULL) {
5850da6c28aaSamw 		pathlen += strlen(attrparent) + 1;	/* add 1 for '/' */
5851da6c28aaSamw 	}
5852da6c28aaSamw 	if ((tpath = calloc(1, pathlen)) == NULL) {
5853da6c28aaSamw 		vperror(0, gettext(
5854da6c28aaSamw 		    "unable to allocate memory for full "
5855da6c28aaSamw 		    "attribute path name for %sattribute %s%s%s of %s"),
5856da6c28aaSamw 		    rw_sysattr ? gettext("system ") : "",
5857da6c28aaSamw 		    (attrparent == NULL) ? "" : attrparent,
5858da6c28aaSamw 		    (attrparent == NULL) ? "" : "/",
5859da6c28aaSamw 		    attr, longname);
5860da6c28aaSamw 		if (tparent != NULL) {
5861da6c28aaSamw 			free(tparent);
5862da6c28aaSamw 		}
5863da6c28aaSamw 		return (1);
5864da6c28aaSamw 	}
5865da6c28aaSamw 	(void) snprintf(tpath, pathlen, "%s%s%s",
5866da6c28aaSamw 	    (attrparent == NULL) ? "" : attrparent,
5867da6c28aaSamw 	    (attrparent == NULL) ? "" : "/",
5868da6c28aaSamw 	    attr);
5869da6c28aaSamw 
5870da6c28aaSamw 	/* fill in the attribute info */
5871da6c28aaSamw 	if (*attrinfo == NULL) {
5872da6c28aaSamw 		if ((*attrinfo = malloc(sizeof (attr_data_t))) == NULL) {
5873da6c28aaSamw 			vperror(0, gettext(
5874da6c28aaSamw 			    "unable to allocate memory for attribute "
5875da6c28aaSamw 			    "information for %sattribute %s%s%s of %s"),
5876da6c28aaSamw 			    rw_sysattr ? gettext("system ") : "",
5877da6c28aaSamw 			    (attrparent == NULL) ? "" : attrparent,
5878da6c28aaSamw 			    (attrparent == NULL) ? "" : gettext("/"),
5879da6c28aaSamw 			    attr, longname);
5880da6c28aaSamw 			if (tparent != NULL) {
5881da6c28aaSamw 				free(tparent);
5882da6c28aaSamw 			}
5883da6c28aaSamw 			free(tpath);
5884da6c28aaSamw 			return (1);
5885da6c28aaSamw 		}
5886da6c28aaSamw 	} else {
5887da6c28aaSamw 		if ((*attrinfo)->attr_parent != NULL) {
5888da6c28aaSamw 			free((*attrinfo)->attr_parent);
5889da6c28aaSamw 		}
5890da6c28aaSamw 		if ((*attrinfo)->attr_path != NULL) {
5891da6c28aaSamw 			free((*attrinfo)->attr_path);
5892da6c28aaSamw 		}
5893da6c28aaSamw 		/*
5894da6c28aaSamw 		 * The parent file descriptor is passed in, so don't
5895da6c28aaSamw 		 * close it here as it should be closed by the function
5896da6c28aaSamw 		 * that opened it.
5897da6c28aaSamw 		 */
5898da6c28aaSamw 	}
5899da6c28aaSamw 	(*attrinfo)->attr_parent = tparent;
5900da6c28aaSamw 	(*attrinfo)->attr_path = tpath;
5901da6c28aaSamw 	(*attrinfo)->attr_rw_sysattr = rw_sysattr;
5902da6c28aaSamw 	(*attrinfo)->attr_parentfd = atparentfd;
5903da6c28aaSamw 
5904da6c28aaSamw 	return (0);
5905da6c28aaSamw }
59067c478bd9Sstevel@tonic-gate 
59077c478bd9Sstevel@tonic-gate /*
59083a1dab68SRich Burridge  * Test to see if name is a directory.
59093a1dab68SRich Burridge  *
59103a1dab68SRich Burridge  * Return 1 if true, 0 otherwise.
59113a1dab68SRich Burridge  */
59123a1dab68SRich Burridge 
59133a1dab68SRich Burridge static int
is_directory(char * name)59143a1dab68SRich Burridge is_directory(char *name)
59153a1dab68SRich Burridge {
59163a1dab68SRich Burridge #if defined(O_XATTR)
59173a1dab68SRich Burridge 	/*
59183a1dab68SRich Burridge 	 * If there is an xattr_buf structure associated with this file,
59193a1dab68SRich Burridge 	 * then the directory test is based on whether the name has a
59203a1dab68SRich Burridge 	 * trailing slash.
59213a1dab68SRich Burridge 	 */
59223a1dab68SRich Burridge 	if (xattrp)
59233a1dab68SRich Burridge 		return (name[strlen(name) - 1] == '/');
59243a1dab68SRich Burridge #endif
59253a1dab68SRich Burridge 	if (is_posix)
59263a1dab68SRich Burridge 		return (dblock.dbuf.typeflag == '5');
59273a1dab68SRich Burridge 	else
59283a1dab68SRich Burridge 		return (name[strlen(name) - 1] == '/');
59293a1dab68SRich Burridge }
59303a1dab68SRich Burridge 
59313a1dab68SRich Burridge /*
5932b25dbf38SRich Burridge  * Version of chdir that handles directory pathnames of greater than PATH_MAX
5933b25dbf38SRich Burridge  * length, by changing the working directory to manageable portions of the
5934b25dbf38SRich Burridge  * complete directory pathname. If any of these attempts fail, then it exits
5935b25dbf38SRich Burridge  * non-zero.
5936b25dbf38SRich Burridge  *
5937b25dbf38SRich Burridge  * If a segment (i.e. a portion of "path" between two "/"'s) of the overall
5938b25dbf38SRich Burridge  * pathname is greater than PATH_MAX, then this still won't work, and this
5939b25dbf38SRich Burridge  * routine will return -1 with errno set to ENAMETOOLONG.
5940b25dbf38SRich Burridge  *
5941b25dbf38SRich Burridge  * NOTE: this routine is semantically different to the system chdir in
5942b25dbf38SRich Burridge  * that it is remotely possible for the currently working directory to be
5943b25dbf38SRich Burridge  * changed to a different directory, if a chdir call fails when processing
5944b25dbf38SRich Burridge  * one of the segments of a path that is greater than PATH_MAX. This isn't
5945b25dbf38SRich Burridge  * a problem as this is tar's own specific version of chdir.
5946b25dbf38SRich Burridge  */
5947b25dbf38SRich Burridge 
5948b25dbf38SRich Burridge static int
tar_chdir(const char * path)5949b25dbf38SRich Burridge tar_chdir(const char *path) {
5950b25dbf38SRich Burridge 	const char *sep = "/";
5951b25dbf38SRich Burridge 	char *path_copy = NULL;
5952b25dbf38SRich Burridge 	char *ptr = NULL;
5953b25dbf38SRich Burridge 
5954b25dbf38SRich Burridge 	/* The trivial case. */
5955b25dbf38SRich Burridge 	if (chdir(path) == 0) {
5956b25dbf38SRich Burridge 		return (0);
5957b25dbf38SRich Burridge 	}
5958b25dbf38SRich Burridge 	if (errno == ENAMETOOLONG) {
5959b25dbf38SRich Burridge 		if (path[0] == '/' && chdir(sep) != 0)
5960b25dbf38SRich Burridge 			return (-1);
5961b25dbf38SRich Burridge 
5962b25dbf38SRich Burridge 		/* strtok(3C) modifies the string, so make a copy. */
5963b25dbf38SRich Burridge 		if ((path_copy = strdup(path)) == NULL) {
5964b25dbf38SRich Burridge 			return (-1);
5965b25dbf38SRich Burridge 		}
5966b25dbf38SRich Burridge 
5967b25dbf38SRich Burridge 		/* chdir(2) for every path element. */
5968b25dbf38SRich Burridge 		for (ptr = strtok(path_copy, sep);
5969b25dbf38SRich Burridge 			ptr != NULL;
5970b25dbf38SRich Burridge 			ptr = strtok(NULL, sep)) {
5971b25dbf38SRich Burridge 			if (chdir(ptr) != 0) {
5972b25dbf38SRich Burridge 				free(path_copy);
5973b25dbf38SRich Burridge 				return (-1);
5974b25dbf38SRich Burridge 			}
5975b25dbf38SRich Burridge 		}
5976b25dbf38SRich Burridge 		free(path_copy);
5977b25dbf38SRich Burridge 		return (0);
5978b25dbf38SRich Burridge 	}
5979b25dbf38SRich Burridge 
5980b25dbf38SRich Burridge 	/* If chdir fails for any reason except ENAMETOOLONG. */
5981b25dbf38SRich Burridge 	return (-1);
5982b25dbf38SRich Burridge }
5983b25dbf38SRich Burridge 
5984b25dbf38SRich Burridge /*
59851a431409SRich Burridge  * Test if name has a '..' sequence in it.
59861a431409SRich Burridge  *
59871a431409SRich Burridge  * Return 1 if found, 0 otherwise.
59881a431409SRich Burridge  */
59891a431409SRich Burridge 
59901a431409SRich Burridge static int
has_dot_dot(char * name)59911a431409SRich Burridge has_dot_dot(char *name)
59921a431409SRich Burridge {
59931a431409SRich Burridge 	char *s;
59941a431409SRich Burridge 	size_t name_len = strlen(name);
59951a431409SRich Burridge 
59961a431409SRich Burridge 	for (s = name; s < (name + name_len - 2); s++) {
59971a431409SRich Burridge 		if (s[0] == '.' && s[1] == '.' && ((s[2] == '/') || !s[2]))
59981a431409SRich Burridge 			return (1);
59991a431409SRich Burridge 
60001a431409SRich Burridge 		while (! (*s == '/')) {
60011a431409SRich Burridge 			if (! *s++)
60021a431409SRich Burridge 				return (0);
60031a431409SRich Burridge 		}
60041a431409SRich Burridge 	}
60051a431409SRich Burridge 
60061a431409SRich Burridge 	return (0);
60071a431409SRich Burridge }
60081a431409SRich Burridge 
60091a431409SRich Burridge /*
60101a431409SRich Burridge  * Test if name is an absolute path name.
60111a431409SRich Burridge  *
60121a431409SRich Burridge  * Return 1 if true, 0 otherwise.
60131a431409SRich Burridge  */
60141a431409SRich Burridge 
60151a431409SRich Burridge static int
is_absolute(char * name)60161a431409SRich Burridge is_absolute(char *name)
60171a431409SRich Burridge {
6018fe5a79a9SRich Burridge #if defined(O_XATTR)
6019fe5a79a9SRich Burridge 	/*
6020fe5a79a9SRich Burridge 	 * If this is an extended attribute (whose name will begin with
6021fe5a79a9SRich Burridge 	 * "/dev/null/", always return 0 as they should be extracted with
6022fe5a79a9SRich Burridge 	 * the name intact, to allow other tar archiving programs that
6023fe5a79a9SRich Burridge 	 * don't understand extended attributes, to correctly throw them away.
6024fe5a79a9SRich Burridge 	 */
6025fe5a79a9SRich Burridge 	if (xattrp)
6026fe5a79a9SRich Burridge 		return (0);
6027fe5a79a9SRich Burridge #endif
6028fe5a79a9SRich Burridge 
60291a431409SRich Burridge 	return (name[0] == '/');
60301a431409SRich Burridge }
60311a431409SRich Burridge 
60321a431409SRich Burridge /*
60331a431409SRich Burridge  * Adjust the pathname to make it a relative one. Strip off any leading
60341a431409SRich Burridge  * '/' characters and if the pathname contains any '..' sequences, strip
60351a431409SRich Burridge  * upto and including the last occurance of '../' (or '..' if found at
60361a431409SRich Burridge  * the very end of the pathname).
60371a431409SRich Burridge  *
60381a431409SRich Burridge  * Return the relative pathname. stripped_prefix will also return the
60391a431409SRich Burridge  * portion of name that was stripped off and should be freed by the
60401a431409SRich Burridge  * calling routine when no longer needed.
60411a431409SRich Burridge  */
60421a431409SRich Burridge 
60431a431409SRich Burridge static char *
make_relative_name(char * name,char ** stripped_prefix)60441a431409SRich Burridge make_relative_name(char *name, char **stripped_prefix)
60451a431409SRich Burridge {
60461a431409SRich Burridge 	char *s;
60471a431409SRich Burridge 	size_t prefix_len = 0;
60481a431409SRich Burridge 	size_t name_len = strlen(name);
60491a431409SRich Burridge 
60501a431409SRich Burridge 	for (s = name + prefix_len; s < (name + name_len - 2); ) {
60511a431409SRich Burridge 		if (s[0] == '.' && s[1] == '.' && ((s[2] == '/') || !s[2]))
60521a431409SRich Burridge 			prefix_len = s + 2 - name;
60531a431409SRich Burridge 
60541a431409SRich Burridge 		do {
60551a431409SRich Burridge 			char c = *s++;
60561a431409SRich Burridge 
60571a431409SRich Burridge 			if (c == '/')
60581a431409SRich Burridge 				break;
60591a431409SRich Burridge 		} while (*s);
60601a431409SRich Burridge 	}
60611a431409SRich Burridge 
60621a431409SRich Burridge 	for (s = name + prefix_len; *s == '/'; s++)
60631a431409SRich Burridge 		continue;
60641a431409SRich Burridge 	prefix_len = s - name;
60651a431409SRich Burridge 
60661a431409SRich Burridge 	/* Create the portion of the name that was stripped off. */
60671a431409SRich Burridge 	s = malloc(prefix_len + 1);
60681a431409SRich Burridge 	memcpy(s, name, prefix_len);
60691a431409SRich Burridge 	s[prefix_len] = 0;
60701a431409SRich Burridge 	*stripped_prefix = s;
60711a431409SRich Burridge 	s = &name[prefix_len];
60721a431409SRich Burridge 
60731a431409SRich Burridge 	return (s);
60741a431409SRich Burridge }
60751a431409SRich Burridge 
60761a431409SRich Burridge /*
60777c478bd9Sstevel@tonic-gate  *  Return through *namep a pointer to the proper fullname (i.e  "<name> |
60787c478bd9Sstevel@tonic-gate  *  <prefix>/<name>"), as represented in the header entry dblock.dbuf.
6079da6c28aaSamw  *
6080da6c28aaSamw  * Returns 0 if successful, otherwise returns 1.
60817c478bd9Sstevel@tonic-gate  */
60827c478bd9Sstevel@tonic-gate 
6083da6c28aaSamw static int
check_prefix(char ** namep,char ** dirp,char ** compp)60847c478bd9Sstevel@tonic-gate check_prefix(char **namep, char **dirp, char **compp)
60857c478bd9Sstevel@tonic-gate {
60867c478bd9Sstevel@tonic-gate 	static char fullname[PATH_MAX + 1];
60877c478bd9Sstevel@tonic-gate 	static char dir[PATH_MAX + 1];
60887c478bd9Sstevel@tonic-gate 	static char component[PATH_MAX + 1];
60897c478bd9Sstevel@tonic-gate 	static char savename[PATH_MAX + 1];
60907c478bd9Sstevel@tonic-gate 	char *s;
60917c478bd9Sstevel@tonic-gate 
60927c478bd9Sstevel@tonic-gate 	(void) memset(dir, 0, sizeof (dir));
60937c478bd9Sstevel@tonic-gate 	(void) memset(component, 0, sizeof (component));
60947c478bd9Sstevel@tonic-gate 
60957c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_PATH) {
60967c478bd9Sstevel@tonic-gate 		(void) strcpy(fullname, Xtarhdr.x_path);
60977c478bd9Sstevel@tonic-gate 	} else {
60987c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.prefix[0] != '\0')
60997c478bd9Sstevel@tonic-gate 			(void) sprintf(fullname, "%.*s/%.*s", PRESIZ,
61007c478bd9Sstevel@tonic-gate 			    dblock.dbuf.prefix, NAMSIZ, dblock.dbuf.name);
61017c478bd9Sstevel@tonic-gate 		else
61027c478bd9Sstevel@tonic-gate 			(void) sprintf(fullname, "%.*s", NAMSIZ,
61037c478bd9Sstevel@tonic-gate 			    dblock.dbuf.name);
61047c478bd9Sstevel@tonic-gate 	}
61057c478bd9Sstevel@tonic-gate 
61067c478bd9Sstevel@tonic-gate 	/*
61071a431409SRich Burridge 	 * If we are printing a table of contents or extracting an archive,
61081a431409SRich Burridge 	 * make absolute pathnames relative and prohibit the unpacking of
61091a431409SRich Burridge 	 * files contain ".." in their name (unless the user has supplied
61101a431409SRich Burridge 	 * the -P option).
61111a431409SRich Burridge 	 */
61121a431409SRich Burridge 	if ((tflag || xflag) && !Pflag) {
61131a431409SRich Burridge 		if (is_absolute(fullname) || has_dot_dot(fullname)) {
61141a431409SRich Burridge 			char *stripped_prefix;
61151a431409SRich Burridge 
61161a431409SRich Burridge 			(void) strcpy(savename, fullname);
61171a431409SRich Burridge 			strcpy(fullname,
61181a431409SRich Burridge 			    make_relative_name(savename, &stripped_prefix));
61191a431409SRich Burridge 			(void) fprintf(stderr,
61201a431409SRich Burridge 			    gettext("tar: Removing leading '%s' from '%s'\n"),
61211a431409SRich Burridge 			    stripped_prefix, savename);
61221a431409SRich Burridge 			free(stripped_prefix);
61231a431409SRich Burridge 		}
61241a431409SRich Burridge 	}
61251a431409SRich Burridge 
61261a431409SRich Burridge 	/*
61277c478bd9Sstevel@tonic-gate 	 * Set dir and component names
61287c478bd9Sstevel@tonic-gate 	 */
61297c478bd9Sstevel@tonic-gate 
61307c478bd9Sstevel@tonic-gate 	get_parent(fullname, dir);
61317c478bd9Sstevel@tonic-gate 
61327c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
6133da6c28aaSamw 	if (xattrp == NULL) {
61347c478bd9Sstevel@tonic-gate #endif
61357c478bd9Sstevel@tonic-gate 		/*
61367c478bd9Sstevel@tonic-gate 		 * Save of real name since were going to chop off the
61377c478bd9Sstevel@tonic-gate 		 * trailing slashes.
61387c478bd9Sstevel@tonic-gate 		 */
61397c478bd9Sstevel@tonic-gate 		(void) strcpy(savename, fullname);
61407c478bd9Sstevel@tonic-gate 		/*
61417c478bd9Sstevel@tonic-gate 		 * first strip of trailing slashes.
61427c478bd9Sstevel@tonic-gate 		 */
61437c478bd9Sstevel@tonic-gate 		chop_endslashes(savename);
61447c478bd9Sstevel@tonic-gate 		s = get_component(savename);
61457c478bd9Sstevel@tonic-gate 		(void) strcpy(component, s);
61467c478bd9Sstevel@tonic-gate 
61477c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
61487c478bd9Sstevel@tonic-gate 	} else {
61497c478bd9Sstevel@tonic-gate 		(void) strcpy(fullname, xattrp->h_names);
61507c478bd9Sstevel@tonic-gate 		(void) strcpy(dir, fullname);
6151da6c28aaSamw 		(void) strcpy(component, basename(xattrp->h_names +
6152da6c28aaSamw 		    strlen(xattrp->h_names) + 1));
61537c478bd9Sstevel@tonic-gate 	}
61547c478bd9Sstevel@tonic-gate #endif
61557c478bd9Sstevel@tonic-gate 	*namep = fullname;
61567c478bd9Sstevel@tonic-gate 	*dirp = dir;
61577c478bd9Sstevel@tonic-gate 	*compp = component;
6158da6c28aaSamw 
6159da6c28aaSamw 	return (0);
61607c478bd9Sstevel@tonic-gate }
61617c478bd9Sstevel@tonic-gate 
61627c478bd9Sstevel@tonic-gate /*
61637c478bd9Sstevel@tonic-gate  * Return true if the object indicated by the file descriptor and type
61647c478bd9Sstevel@tonic-gate  * is a tape device, false otherwise
61657c478bd9Sstevel@tonic-gate  */
61667c478bd9Sstevel@tonic-gate 
61677c478bd9Sstevel@tonic-gate static int
istape(int fd,int type)61687c478bd9Sstevel@tonic-gate istape(int fd, int type)
61697c478bd9Sstevel@tonic-gate {
61707c478bd9Sstevel@tonic-gate 	int result = 0;
61717c478bd9Sstevel@tonic-gate 
61724bc0a2efScasper 	if (S_ISCHR(type)) {
61737c478bd9Sstevel@tonic-gate 		struct mtget mtg;
61747c478bd9Sstevel@tonic-gate 
61757c478bd9Sstevel@tonic-gate 		if (ioctl(fd, MTIOCGET, &mtg) != -1) {
61767c478bd9Sstevel@tonic-gate 			result = 1;
61777c478bd9Sstevel@tonic-gate 		}
61787c478bd9Sstevel@tonic-gate 	}
61797c478bd9Sstevel@tonic-gate 
61807c478bd9Sstevel@tonic-gate 	return (result);
61817c478bd9Sstevel@tonic-gate }
61827c478bd9Sstevel@tonic-gate 
61837c478bd9Sstevel@tonic-gate #include <utmpx.h>
61847c478bd9Sstevel@tonic-gate 
61857c478bd9Sstevel@tonic-gate struct utmpx utmpx;
61867c478bd9Sstevel@tonic-gate 
61877c478bd9Sstevel@tonic-gate #define	NMAX	(sizeof (utmpx.ut_name))
61887c478bd9Sstevel@tonic-gate 
61897c478bd9Sstevel@tonic-gate typedef struct cachenode {	/* this struct must be zeroed before using */
61907c478bd9Sstevel@tonic-gate 	struct cachenode *next;	/* next in hash chain */
61917c478bd9Sstevel@tonic-gate 	int val;		/* the uid or gid of this entry */
61927c478bd9Sstevel@tonic-gate 	int namehash;		/* name's hash signature */
61937c478bd9Sstevel@tonic-gate 	char name[NMAX+1];	/* the string that val maps to */
61947c478bd9Sstevel@tonic-gate } cachenode_t;
61957c478bd9Sstevel@tonic-gate 
61967c478bd9Sstevel@tonic-gate #define	HASHSIZE	256
61977c478bd9Sstevel@tonic-gate 
61987c478bd9Sstevel@tonic-gate static cachenode_t *names[HASHSIZE];
61997c478bd9Sstevel@tonic-gate static cachenode_t *groups[HASHSIZE];
62007c478bd9Sstevel@tonic-gate static cachenode_t *uids[HASHSIZE];
62017c478bd9Sstevel@tonic-gate static cachenode_t *gids[HASHSIZE];
62027c478bd9Sstevel@tonic-gate 
62037c478bd9Sstevel@tonic-gate static int
hash_byname(char * name)62047c478bd9Sstevel@tonic-gate hash_byname(char *name)
62057c478bd9Sstevel@tonic-gate {
62067c478bd9Sstevel@tonic-gate 	int i, c, h = 0;
62077c478bd9Sstevel@tonic-gate 
62087c478bd9Sstevel@tonic-gate 	for (i = 0; i < NMAX; i++) {
62097c478bd9Sstevel@tonic-gate 		c = name[i];
62107c478bd9Sstevel@tonic-gate 		if (c == '\0')
62117c478bd9Sstevel@tonic-gate 			break;
62127c478bd9Sstevel@tonic-gate 		h = (h << 4) + h + c;
62137c478bd9Sstevel@tonic-gate 	}
62147c478bd9Sstevel@tonic-gate 	return (h);
62157c478bd9Sstevel@tonic-gate }
62167c478bd9Sstevel@tonic-gate 
62177c478bd9Sstevel@tonic-gate static cachenode_t *
hash_lookup_byval(cachenode_t * table[],int val)62187c478bd9Sstevel@tonic-gate hash_lookup_byval(cachenode_t *table[], int val)
62197c478bd9Sstevel@tonic-gate {
62207c478bd9Sstevel@tonic-gate 	int h = val;
62217c478bd9Sstevel@tonic-gate 	cachenode_t *c;
62227c478bd9Sstevel@tonic-gate 
62237c478bd9Sstevel@tonic-gate 	for (c = table[h & (HASHSIZE - 1)]; c != NULL; c = c->next) {
62247c478bd9Sstevel@tonic-gate 		if (c->val == val)
62257c478bd9Sstevel@tonic-gate 			return (c);
62267c478bd9Sstevel@tonic-gate 	}
62277c478bd9Sstevel@tonic-gate 	return (NULL);
62287c478bd9Sstevel@tonic-gate }
62297c478bd9Sstevel@tonic-gate 
62307c478bd9Sstevel@tonic-gate static cachenode_t *
hash_lookup_byname(cachenode_t * table[],char * name)62317c478bd9Sstevel@tonic-gate hash_lookup_byname(cachenode_t *table[], char *name)
62327c478bd9Sstevel@tonic-gate {
62337c478bd9Sstevel@tonic-gate 	int h = hash_byname(name);
62347c478bd9Sstevel@tonic-gate 	cachenode_t *c;
62357c478bd9Sstevel@tonic-gate 
62367c478bd9Sstevel@tonic-gate 	for (c = table[h & (HASHSIZE - 1)]; c != NULL; c = c->next) {
62377c478bd9Sstevel@tonic-gate 		if (c->namehash == h && strcmp(c->name, name) == 0)
62387c478bd9Sstevel@tonic-gate 			return (c);
62397c478bd9Sstevel@tonic-gate 	}
62407c478bd9Sstevel@tonic-gate 	return (NULL);
62417c478bd9Sstevel@tonic-gate }
62427c478bd9Sstevel@tonic-gate 
62437c478bd9Sstevel@tonic-gate static cachenode_t *
hash_insert(cachenode_t * table[],char * name,int value)62447c478bd9Sstevel@tonic-gate hash_insert(cachenode_t *table[], char *name, int value)
62457c478bd9Sstevel@tonic-gate {
62467c478bd9Sstevel@tonic-gate 	cachenode_t *c;
62477c478bd9Sstevel@tonic-gate 	int signature;
62487c478bd9Sstevel@tonic-gate 
62497c478bd9Sstevel@tonic-gate 	c = calloc(1, sizeof (cachenode_t));
62507c478bd9Sstevel@tonic-gate 	if (c == NULL) {
62517c478bd9Sstevel@tonic-gate 		perror("malloc");
62527c478bd9Sstevel@tonic-gate 		exit(1);
62537c478bd9Sstevel@tonic-gate 	}
62547c478bd9Sstevel@tonic-gate 	if (name != NULL) {
62557c478bd9Sstevel@tonic-gate 		(void) strncpy(c->name, name, NMAX);
62567c478bd9Sstevel@tonic-gate 		c->namehash = hash_byname(name);
62577c478bd9Sstevel@tonic-gate 	}
62587c478bd9Sstevel@tonic-gate 	c->val = value;
62597c478bd9Sstevel@tonic-gate 	if (table == uids || table == gids)
62607c478bd9Sstevel@tonic-gate 		signature = c->val;
62617c478bd9Sstevel@tonic-gate 	else
62627c478bd9Sstevel@tonic-gate 		signature = c->namehash;
62637c478bd9Sstevel@tonic-gate 	c->next = table[signature & (HASHSIZE - 1)];
62647c478bd9Sstevel@tonic-gate 	table[signature & (HASHSIZE - 1)] = c;
62657c478bd9Sstevel@tonic-gate 	return (c);
62667c478bd9Sstevel@tonic-gate }
62677c478bd9Sstevel@tonic-gate 
62687c478bd9Sstevel@tonic-gate static char *
getname(uid_t uid)62697c478bd9Sstevel@tonic-gate getname(uid_t uid)
62707c478bd9Sstevel@tonic-gate {
62717c478bd9Sstevel@tonic-gate 	cachenode_t *c;
62727c478bd9Sstevel@tonic-gate 
62737c478bd9Sstevel@tonic-gate 	if ((c = hash_lookup_byval(uids, uid)) == NULL) {
62747c478bd9Sstevel@tonic-gate 		struct passwd *pwent = getpwuid(uid);
62757c478bd9Sstevel@tonic-gate 		c = hash_insert(uids, pwent ? pwent->pw_name : NULL, uid);
62767c478bd9Sstevel@tonic-gate 	}
62777c478bd9Sstevel@tonic-gate 	return (c->name);
62787c478bd9Sstevel@tonic-gate }
62797c478bd9Sstevel@tonic-gate 
62807c478bd9Sstevel@tonic-gate static char *
getgroup(gid_t gid)62817c478bd9Sstevel@tonic-gate getgroup(gid_t gid)
62827c478bd9Sstevel@tonic-gate {
62837c478bd9Sstevel@tonic-gate 	cachenode_t *c;
62847c478bd9Sstevel@tonic-gate 
62857c478bd9Sstevel@tonic-gate 	if ((c = hash_lookup_byval(gids, gid)) == NULL) {
62867c478bd9Sstevel@tonic-gate 		struct group *grent = getgrgid(gid);
62877c478bd9Sstevel@tonic-gate 		c = hash_insert(gids, grent ? grent->gr_name : NULL, gid);
62887c478bd9Sstevel@tonic-gate 	}
62897c478bd9Sstevel@tonic-gate 	return (c->name);
62907c478bd9Sstevel@tonic-gate }
62917c478bd9Sstevel@tonic-gate 
62927c478bd9Sstevel@tonic-gate static uid_t
getuidbyname(char * name)62937c478bd9Sstevel@tonic-gate getuidbyname(char *name)
62947c478bd9Sstevel@tonic-gate {
62957c478bd9Sstevel@tonic-gate 	cachenode_t *c;
62967c478bd9Sstevel@tonic-gate 
62977c478bd9Sstevel@tonic-gate 	if ((c = hash_lookup_byname(names, name)) == NULL) {
62987c478bd9Sstevel@tonic-gate 		struct passwd *pwent = getpwnam(name);
62997c478bd9Sstevel@tonic-gate 		c = hash_insert(names, name, pwent ? (int)pwent->pw_uid : -1);
63007c478bd9Sstevel@tonic-gate 	}
63017c478bd9Sstevel@tonic-gate 	return ((uid_t)c->val);
63027c478bd9Sstevel@tonic-gate }
63037c478bd9Sstevel@tonic-gate 
63047c478bd9Sstevel@tonic-gate static gid_t
getgidbyname(char * group)63057c478bd9Sstevel@tonic-gate getgidbyname(char *group)
63067c478bd9Sstevel@tonic-gate {
63077c478bd9Sstevel@tonic-gate 	cachenode_t *c;
63087c478bd9Sstevel@tonic-gate 
63097c478bd9Sstevel@tonic-gate 	if ((c = hash_lookup_byname(groups, group)) == NULL) {
63107c478bd9Sstevel@tonic-gate 		struct group *grent = getgrnam(group);
63117c478bd9Sstevel@tonic-gate 		c = hash_insert(groups, group, grent ? (int)grent->gr_gid : -1);
63127c478bd9Sstevel@tonic-gate 	}
63137c478bd9Sstevel@tonic-gate 	return ((gid_t)c->val);
63147c478bd9Sstevel@tonic-gate }
63157c478bd9Sstevel@tonic-gate 
63167c478bd9Sstevel@tonic-gate /*
63177c478bd9Sstevel@tonic-gate  * Build the header.
63187c478bd9Sstevel@tonic-gate  * Determine whether or not an extended header is also needed.  If needed,
63197c478bd9Sstevel@tonic-gate  * create and write the extended header and its data.
63207c478bd9Sstevel@tonic-gate  * Writing of the extended header assumes that "tomodes" has been called and
63217c478bd9Sstevel@tonic-gate  * the relevant information has been placed in the header block.
63227c478bd9Sstevel@tonic-gate  */
63237c478bd9Sstevel@tonic-gate 
63247c478bd9Sstevel@tonic-gate static int
build_dblock(const char * name,const char * linkname,const char typeflag,const int filetype,const struct stat * sp,const dev_t device,const char * prefix)63257c478bd9Sstevel@tonic-gate build_dblock(
63267c478bd9Sstevel@tonic-gate 	const char		*name,
63277c478bd9Sstevel@tonic-gate 	const char		*linkname,
63287c478bd9Sstevel@tonic-gate 	const char		typeflag,
63297c478bd9Sstevel@tonic-gate 	const int		filetype,
63307c478bd9Sstevel@tonic-gate 	const struct stat	*sp,
63317c478bd9Sstevel@tonic-gate 	const dev_t		device,
63327c478bd9Sstevel@tonic-gate 	const char		*prefix)
63337c478bd9Sstevel@tonic-gate {
63347c478bd9Sstevel@tonic-gate 	int nblks;
63357c478bd9Sstevel@tonic-gate 	major_t		dev;
63367c478bd9Sstevel@tonic-gate 	const char	*filename;
63377c478bd9Sstevel@tonic-gate 	const char	*lastslash;
63387c478bd9Sstevel@tonic-gate 
63397c478bd9Sstevel@tonic-gate 	if (filetype == XATTR_FILE)
63407c478bd9Sstevel@tonic-gate 		dblock.dbuf.typeflag = _XATTR_HDRTYPE;
63417c478bd9Sstevel@tonic-gate 	else
63427c478bd9Sstevel@tonic-gate 		dblock.dbuf.typeflag = typeflag;
63437c478bd9Sstevel@tonic-gate 	(void) memset(dblock.dbuf.name, '\0', NAMSIZ);
63447c478bd9Sstevel@tonic-gate 	(void) memset(dblock.dbuf.linkname, '\0', NAMSIZ);
63457c478bd9Sstevel@tonic-gate 	(void) memset(dblock.dbuf.prefix, '\0', PRESIZ);
63467c478bd9Sstevel@tonic-gate 
63477c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_PATH)
63487c478bd9Sstevel@tonic-gate 		filename = Xtarhdr.x_path;
63497c478bd9Sstevel@tonic-gate 	else
63507c478bd9Sstevel@tonic-gate 		filename = name;
63517c478bd9Sstevel@tonic-gate 
63527c478bd9Sstevel@tonic-gate 	if ((dev = major(device)) > OCTAL7CHAR) {
63537c478bd9Sstevel@tonic-gate 		if (Eflag) {
63547c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_DEVMAJOR;
63557c478bd9Sstevel@tonic-gate 			Xtarhdr.x_devmajor = dev;
63567c478bd9Sstevel@tonic-gate 		} else {
63577c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
63587c478bd9Sstevel@tonic-gate 			    "Device major too large for %s.  Use -E flag."),
63597c478bd9Sstevel@tonic-gate 			    filename);
63607c478bd9Sstevel@tonic-gate 			if (errflag)
63617c478bd9Sstevel@tonic-gate 				done(1);
63627c478bd9Sstevel@tonic-gate 			else
63637c478bd9Sstevel@tonic-gate 				Errflg = 1;
63647c478bd9Sstevel@tonic-gate 		}
63657c478bd9Sstevel@tonic-gate 		dev = 0;
63667c478bd9Sstevel@tonic-gate 	}
63677c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.devmajor, "%07lo", dev);
63687c478bd9Sstevel@tonic-gate 	if ((dev = minor(device)) > OCTAL7CHAR) {
63697c478bd9Sstevel@tonic-gate 		if (Eflag) {
63707c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_DEVMINOR;
63717c478bd9Sstevel@tonic-gate 			Xtarhdr.x_devminor = dev;
63727c478bd9Sstevel@tonic-gate 		} else {
63737c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
63747c478bd9Sstevel@tonic-gate 			    "Device minor too large for %s.  Use -E flag."),
63757c478bd9Sstevel@tonic-gate 			    filename);
63767c478bd9Sstevel@tonic-gate 			if (errflag)
63777c478bd9Sstevel@tonic-gate 				done(1);
63787c478bd9Sstevel@tonic-gate 			else
63797c478bd9Sstevel@tonic-gate 				Errflg = 1;
63807c478bd9Sstevel@tonic-gate 		}
63817c478bd9Sstevel@tonic-gate 		dev = 0;
63827c478bd9Sstevel@tonic-gate 	}
63837c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.devminor, "%07lo", dev);
63847c478bd9Sstevel@tonic-gate 
63857c478bd9Sstevel@tonic-gate 	(void) strncpy(dblock.dbuf.name, name, NAMSIZ);
63867c478bd9Sstevel@tonic-gate 	(void) strncpy(dblock.dbuf.linkname, linkname, NAMSIZ);
63877c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.magic, "%.5s", magic_type);
63887c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.version, "00");
63897c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.uname, "%.31s", getname(sp->st_uid));
63907c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.gname, "%.31s", getgroup(sp->st_gid));
63917c478bd9Sstevel@tonic-gate 	(void) strncpy(dblock.dbuf.prefix, prefix, PRESIZ);
63927c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
63937c478bd9Sstevel@tonic-gate 
63947c478bd9Sstevel@tonic-gate 	if (Eflag) {
63957c478bd9Sstevel@tonic-gate 		(void) bcopy(dblock.dummy, xhdr_buf.dummy, TBLOCK);
63967c478bd9Sstevel@tonic-gate 		(void) memset(xhdr_buf.dbuf.name, '\0', NAMSIZ);
63977c478bd9Sstevel@tonic-gate 		lastslash = strrchr(name, '/');
63987c478bd9Sstevel@tonic-gate 		if (lastslash == NULL)
63997c478bd9Sstevel@tonic-gate 			lastslash = name;
64007c478bd9Sstevel@tonic-gate 		else
64017c478bd9Sstevel@tonic-gate 			lastslash++;
64027c478bd9Sstevel@tonic-gate 		(void) strcpy(xhdr_buf.dbuf.name, lastslash);
64037c478bd9Sstevel@tonic-gate 		(void) memset(xhdr_buf.dbuf.linkname, '\0', NAMSIZ);
64047c478bd9Sstevel@tonic-gate 		(void) memset(xhdr_buf.dbuf.prefix, '\0', PRESIZ);
64057c478bd9Sstevel@tonic-gate 		(void) strcpy(xhdr_buf.dbuf.prefix, xhdr_dirname);
64067c478bd9Sstevel@tonic-gate 		xhdr_count++;
64077c478bd9Sstevel@tonic-gate 		xrec_offset = 0;
64087c478bd9Sstevel@tonic-gate 		gen_date("mtime", sp->st_mtim);
64097c478bd9Sstevel@tonic-gate 		xhdr_buf.dbuf.typeflag = 'X';
64107c478bd9Sstevel@tonic-gate 		if (gen_utf8_names(filename) != 0)
64117c478bd9Sstevel@tonic-gate 			return (1);
64127c478bd9Sstevel@tonic-gate 
64137c478bd9Sstevel@tonic-gate #ifdef XHDR_DEBUG
64147c478bd9Sstevel@tonic-gate 		Xtarhdr.x_uname = dblock.dbuf.uname;
64157c478bd9Sstevel@tonic-gate 		Xtarhdr.x_gname = dblock.dbuf.gname;
64167c478bd9Sstevel@tonic-gate 		xhdr_flgs |= (_X_UNAME | _X_GNAME);
64177c478bd9Sstevel@tonic-gate #endif
64187c478bd9Sstevel@tonic-gate 		if (xhdr_flgs) {
64197c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_DEVMAJOR)
64207c478bd9Sstevel@tonic-gate 				gen_num("SUN.devmajor", Xtarhdr.x_devmajor);
64217c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_DEVMINOR)
64227c478bd9Sstevel@tonic-gate 				gen_num("SUN.devminor", Xtarhdr.x_devminor);
64237c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_GID)
64247c478bd9Sstevel@tonic-gate 				gen_num("gid", Xtarhdr.x_gid);
64257c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_UID)
64267c478bd9Sstevel@tonic-gate 				gen_num("uid", Xtarhdr.x_uid);
64277c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_SIZE)
64287c478bd9Sstevel@tonic-gate 				gen_num("size", Xtarhdr.x_filesz);
64297c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_PATH)
64307c478bd9Sstevel@tonic-gate 				gen_string("path", Xtarhdr.x_path);
64317c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_LINKPATH)
64327c478bd9Sstevel@tonic-gate 				gen_string("linkpath", Xtarhdr.x_linkpath);
64337c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_GNAME)
64347c478bd9Sstevel@tonic-gate 				gen_string("gname", Xtarhdr.x_gname);
64357c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_UNAME)
64367c478bd9Sstevel@tonic-gate 				gen_string("uname", Xtarhdr.x_uname);
64377c478bd9Sstevel@tonic-gate 		}
64387c478bd9Sstevel@tonic-gate 		(void) sprintf(xhdr_buf.dbuf.size,
64397c478bd9Sstevel@tonic-gate 		    "%011" FMT_off_t_o, xrec_offset);
64407c478bd9Sstevel@tonic-gate 		(void) sprintf(xhdr_buf.dbuf.chksum, "%07o",
64417c478bd9Sstevel@tonic-gate 		    checksum(&xhdr_buf));
64427c478bd9Sstevel@tonic-gate 		(void) writetbuf((char *)&xhdr_buf, 1);
64437c478bd9Sstevel@tonic-gate 		nblks = TBLOCKS(xrec_offset);
64447c478bd9Sstevel@tonic-gate 		(void) writetbuf(xrec_ptr, nblks);
64457c478bd9Sstevel@tonic-gate 	}
64467c478bd9Sstevel@tonic-gate 	return (0);
64477c478bd9Sstevel@tonic-gate }
64487c478bd9Sstevel@tonic-gate 
64497c478bd9Sstevel@tonic-gate 
64507c478bd9Sstevel@tonic-gate /*
64517c478bd9Sstevel@tonic-gate  *  makeDir - ensure that a directory with the pathname denoted by name
64527c478bd9Sstevel@tonic-gate  *            exists, and return 1 on success, and 0 on failure (e.g.,
64537c478bd9Sstevel@tonic-gate  *	      read-only file system, exists but not-a-directory).
64547c478bd9Sstevel@tonic-gate  */
64557c478bd9Sstevel@tonic-gate 
64567c478bd9Sstevel@tonic-gate static int
makeDir(char * name)64577c478bd9Sstevel@tonic-gate makeDir(char *name)
64587c478bd9Sstevel@tonic-gate {
64597c478bd9Sstevel@tonic-gate 	struct stat buf;
64607c478bd9Sstevel@tonic-gate 
64617c478bd9Sstevel@tonic-gate 	if (access(name, 0) < 0) {  /* name doesn't exist */
64627c478bd9Sstevel@tonic-gate 		if (mkdir(name, 0777) < 0) {
64637c478bd9Sstevel@tonic-gate 			vperror(0, "%s", name);
64647c478bd9Sstevel@tonic-gate 			return (0);
64657c478bd9Sstevel@tonic-gate 		}
64667c478bd9Sstevel@tonic-gate 	} else {		   /* name exists */
64677c478bd9Sstevel@tonic-gate 		if (stat(name, &buf) < 0) {
64687c478bd9Sstevel@tonic-gate 			vperror(0, "%s", name);
64697c478bd9Sstevel@tonic-gate 			return (0);
64707c478bd9Sstevel@tonic-gate 		}
64717c478bd9Sstevel@tonic-gate 
64727c478bd9Sstevel@tonic-gate 		return ((buf.st_mode & S_IFMT) == S_IFDIR);
64737c478bd9Sstevel@tonic-gate 	}
64747c478bd9Sstevel@tonic-gate 
64757c478bd9Sstevel@tonic-gate 	return (1);
64767c478bd9Sstevel@tonic-gate }
64777c478bd9Sstevel@tonic-gate 
64787c478bd9Sstevel@tonic-gate 
64797c478bd9Sstevel@tonic-gate /*
64807c478bd9Sstevel@tonic-gate  * Save this directory and its mtime on the stack, popping and setting
64817c478bd9Sstevel@tonic-gate  * the mtimes of any stacked dirs which aren't parents of this one.
64827c478bd9Sstevel@tonic-gate  * A null name causes the entire stack to be unwound and set.
64837c478bd9Sstevel@tonic-gate  *
64847c478bd9Sstevel@tonic-gate  * Since all the elements of the directory "stack" share a common
64857c478bd9Sstevel@tonic-gate  * prefix, we can make do with one string.  We keep only the current
64867c478bd9Sstevel@tonic-gate  * directory path, with an associated array of mtime's. A negative
64877c478bd9Sstevel@tonic-gate  * mtime means no mtime.
64887c478bd9Sstevel@tonic-gate  *
64897c478bd9Sstevel@tonic-gate  * This stack algorithm is not guaranteed to work for tapes created
64907c478bd9Sstevel@tonic-gate  * with the 'r' function letter, but the vast majority of tapes with
64917c478bd9Sstevel@tonic-gate  * directories are not.  This avoids saving every directory record on
64927c478bd9Sstevel@tonic-gate  * the tape and setting all the times at the end.
64937c478bd9Sstevel@tonic-gate  *
64947c478bd9Sstevel@tonic-gate  * (This was borrowed from the 4.1.3 source, and adapted to the 5.x
64957c478bd9Sstevel@tonic-gate  *  environment)
64967c478bd9Sstevel@tonic-gate  */
64977c478bd9Sstevel@tonic-gate 
64987c478bd9Sstevel@tonic-gate static void
doDirTimes(char * name,timestruc_t modTime)64997c478bd9Sstevel@tonic-gate doDirTimes(char *name, timestruc_t modTime)
65007c478bd9Sstevel@tonic-gate {
65017c478bd9Sstevel@tonic-gate 	static char dirstack[PATH_MAX+2];
65027c478bd9Sstevel@tonic-gate 			/* Add spaces for the last slash and last NULL */
65037c478bd9Sstevel@tonic-gate 	static timestruc_t	modtimes[PATH_MAX+1]; /* hash table */
65047c478bd9Sstevel@tonic-gate 	char *p = dirstack;
65057c478bd9Sstevel@tonic-gate 	char *q = name;
65067c478bd9Sstevel@tonic-gate 	char *savp;
65077c478bd9Sstevel@tonic-gate 
65087c478bd9Sstevel@tonic-gate 	if (q) {
65097c478bd9Sstevel@tonic-gate 		/*
65107c478bd9Sstevel@tonic-gate 		 * Find common prefix
65117c478bd9Sstevel@tonic-gate 		 */
65127c478bd9Sstevel@tonic-gate 
65137c478bd9Sstevel@tonic-gate 		while (*p == *q && *p) {
65147c478bd9Sstevel@tonic-gate 			p++; q++;
65157c478bd9Sstevel@tonic-gate 		}
65167c478bd9Sstevel@tonic-gate 	}
65177c478bd9Sstevel@tonic-gate 
65187c478bd9Sstevel@tonic-gate 	savp = p;
65197c478bd9Sstevel@tonic-gate 	while (*p) {
65207c478bd9Sstevel@tonic-gate 		/*
65217c478bd9Sstevel@tonic-gate 		 * Not a child: unwind the stack, setting the times.
65227c478bd9Sstevel@tonic-gate 		 * The order we do this doesn't matter, so we go "forward."
65237c478bd9Sstevel@tonic-gate 		 */
65247c478bd9Sstevel@tonic-gate 
65257c478bd9Sstevel@tonic-gate 		if (*p == '/')
65267c478bd9Sstevel@tonic-gate 			if (modtimes[p - dirstack].tv_sec >= 0) {
65277c478bd9Sstevel@tonic-gate 				*p = '\0';	 /* zap the slash */
65287c478bd9Sstevel@tonic-gate 				setPathTimes(AT_FDCWD, dirstack,
65297c478bd9Sstevel@tonic-gate 				    modtimes[p - dirstack]);
65307c478bd9Sstevel@tonic-gate 				*p = '/';
65317c478bd9Sstevel@tonic-gate 			}
65327c478bd9Sstevel@tonic-gate 		++p;
65337c478bd9Sstevel@tonic-gate 	}
65347c478bd9Sstevel@tonic-gate 
65357c478bd9Sstevel@tonic-gate 	p = savp;
65367c478bd9Sstevel@tonic-gate 
65377c478bd9Sstevel@tonic-gate 	/*
65387c478bd9Sstevel@tonic-gate 	 *  Push this one on the "stack"
65397c478bd9Sstevel@tonic-gate 	 */
65407c478bd9Sstevel@tonic-gate 
65417c478bd9Sstevel@tonic-gate 	if (q) {
65427c478bd9Sstevel@tonic-gate 
65437c478bd9Sstevel@tonic-gate 		/*
65447c478bd9Sstevel@tonic-gate 		 * Since the name parameter points the dir pathname
65457c478bd9Sstevel@tonic-gate 		 * which is limited only to contain PATH_MAX chars
65467c478bd9Sstevel@tonic-gate 		 * at maximum, we can ignore the overflow case of p.
65477c478bd9Sstevel@tonic-gate 		 */
65487c478bd9Sstevel@tonic-gate 
65497c478bd9Sstevel@tonic-gate 		while ((*p = *q++)) {	/* append the rest of the new dir */
65507c478bd9Sstevel@tonic-gate 			modtimes[p - dirstack].tv_sec = -1;
65517c478bd9Sstevel@tonic-gate 			p++;
65527c478bd9Sstevel@tonic-gate 		}
65537c478bd9Sstevel@tonic-gate 
65547c478bd9Sstevel@tonic-gate 		/*
65557c478bd9Sstevel@tonic-gate 		 * If the tar file had used 'P' or 'E' function modifier,
65567c478bd9Sstevel@tonic-gate 		 * append the last slash.
65577c478bd9Sstevel@tonic-gate 		 */
65587c478bd9Sstevel@tonic-gate 		if (*(p - 1) != '/') {
65597c478bd9Sstevel@tonic-gate 			*p++ = '/';
65607c478bd9Sstevel@tonic-gate 			*p = '\0';
65617c478bd9Sstevel@tonic-gate 		}
65627c478bd9Sstevel@tonic-gate 		/* overwrite the last one */
65637c478bd9Sstevel@tonic-gate 		modtimes[p - dirstack - 1] = modTime;
65647c478bd9Sstevel@tonic-gate 	}
65657c478bd9Sstevel@tonic-gate }
65667c478bd9Sstevel@tonic-gate 
65677c478bd9Sstevel@tonic-gate 
65687c478bd9Sstevel@tonic-gate /*
65697c478bd9Sstevel@tonic-gate  *  setPathTimes - set the modification time for given path.  Return 1 if
65707c478bd9Sstevel@tonic-gate  *                 successful and 0 if not successful.
65717c478bd9Sstevel@tonic-gate  */
65727c478bd9Sstevel@tonic-gate 
65737c478bd9Sstevel@tonic-gate static void
setPathTimes(int dirfd,char * path,timestruc_t modTime)65747c478bd9Sstevel@tonic-gate setPathTimes(int dirfd, char *path, timestruc_t modTime)
65757c478bd9Sstevel@tonic-gate 
65767c478bd9Sstevel@tonic-gate {
65777c478bd9Sstevel@tonic-gate 	struct timeval timebuf[2];
65787c478bd9Sstevel@tonic-gate 
65797c478bd9Sstevel@tonic-gate 	/*
65807c478bd9Sstevel@tonic-gate 	 * futimesat takes an array of two timeval structs.
65817c478bd9Sstevel@tonic-gate 	 * The first entry contains access time.
65827c478bd9Sstevel@tonic-gate 	 * The second entry contains modification time.
65837c478bd9Sstevel@tonic-gate 	 * Unlike a timestruc_t, which uses nanoseconds, timeval uses
65847c478bd9Sstevel@tonic-gate 	 * microseconds.
65857c478bd9Sstevel@tonic-gate 	 */
65867c478bd9Sstevel@tonic-gate 	timebuf[0].tv_sec = time((time_t *)0);
65877c478bd9Sstevel@tonic-gate 	timebuf[0].tv_usec = 0;
65887c478bd9Sstevel@tonic-gate 	timebuf[1].tv_sec = modTime.tv_sec;
65897c478bd9Sstevel@tonic-gate 
65907c478bd9Sstevel@tonic-gate 	/* Extended header: use microseconds */
65917c478bd9Sstevel@tonic-gate 	timebuf[1].tv_usec = (xhdr_flgs & _X_MTIME) ? modTime.tv_nsec/1000 : 0;
65927c478bd9Sstevel@tonic-gate 
65937c478bd9Sstevel@tonic-gate 	if (futimesat(dirfd, path, timebuf) < 0)
65948e4a71aeSRich Burridge 		vperror(0, gettext("can't set time on %s"), path);
65957c478bd9Sstevel@tonic-gate }
65967c478bd9Sstevel@tonic-gate 
65977c478bd9Sstevel@tonic-gate 
65987c478bd9Sstevel@tonic-gate /*
65997c478bd9Sstevel@tonic-gate  * If hflag is set then delete the symbolic link's target.
66007c478bd9Sstevel@tonic-gate  * If !hflag then delete the target.
66017c478bd9Sstevel@tonic-gate  */
66027c478bd9Sstevel@tonic-gate 
66037c478bd9Sstevel@tonic-gate static void
delete_target(int fd,char * comp,char * namep)66042c0f0499Slovely delete_target(int fd, char *comp, char *namep)
66057c478bd9Sstevel@tonic-gate {
66067c478bd9Sstevel@tonic-gate 	struct	stat	xtractbuf;
66077c478bd9Sstevel@tonic-gate 	char buf[PATH_MAX + 1];
66087c478bd9Sstevel@tonic-gate 	int n;
66097c478bd9Sstevel@tonic-gate 
66107c478bd9Sstevel@tonic-gate 
66112c0f0499Slovely 	if (unlinkat(fd, comp, AT_REMOVEDIR) < 0) {
66127c478bd9Sstevel@tonic-gate 		if (errno == ENOTDIR && !hflag) {
66132c0f0499Slovely 			(void) unlinkat(fd, comp, 0);
66147c478bd9Sstevel@tonic-gate 		} else if (errno == ENOTDIR && hflag) {
66157c478bd9Sstevel@tonic-gate 			if (!lstat(namep, &xtractbuf)) {
66167c478bd9Sstevel@tonic-gate 				if ((xtractbuf.st_mode & S_IFMT) != S_IFLNK) {
66172c0f0499Slovely 					(void) unlinkat(fd, comp, 0);
66187c478bd9Sstevel@tonic-gate 				} else if ((n = readlink(namep, buf,
66197c478bd9Sstevel@tonic-gate 				    PATH_MAX)) != -1) {
66207c478bd9Sstevel@tonic-gate 					buf[n] = (char)NULL;
66217c478bd9Sstevel@tonic-gate 					(void) unlinkat(fd, buf,
66227c478bd9Sstevel@tonic-gate 					    AT_REMOVEDIR);
66237c478bd9Sstevel@tonic-gate 					if (errno == ENOTDIR)
66247c478bd9Sstevel@tonic-gate 						(void) unlinkat(fd, buf, 0);
66257c478bd9Sstevel@tonic-gate 				} else {
66262c0f0499Slovely 					(void) unlinkat(fd, comp, 0);
66277c478bd9Sstevel@tonic-gate 				}
66287c478bd9Sstevel@tonic-gate 			} else {
66292c0f0499Slovely 				(void) unlinkat(fd, comp, 0);
66307c478bd9Sstevel@tonic-gate 			}
66317c478bd9Sstevel@tonic-gate 		}
66327c478bd9Sstevel@tonic-gate 	}
66337c478bd9Sstevel@tonic-gate }
66347c478bd9Sstevel@tonic-gate 
66357c478bd9Sstevel@tonic-gate 
66367c478bd9Sstevel@tonic-gate /*
66377c478bd9Sstevel@tonic-gate  * ACL changes:
66387c478bd9Sstevel@tonic-gate  *	putfile():
66397c478bd9Sstevel@tonic-gate  *		Get acl info after stat. Write out ancillary file
66407c478bd9Sstevel@tonic-gate  *		before the normal file, i.e. directory, regular, FIFO,
66417c478bd9Sstevel@tonic-gate  *		link, special. If acl count is less than 4, no need to
66427c478bd9Sstevel@tonic-gate  *		create ancillary file. (i.e. standard permission is in
66437c478bd9Sstevel@tonic-gate  *		use.
66447c478bd9Sstevel@tonic-gate  *	doxtract():
66457c478bd9Sstevel@tonic-gate  *		Process ancillary file. Read it in and set acl info.
66467c478bd9Sstevel@tonic-gate  *		watch out for 'o' function modifier.
66477c478bd9Sstevel@tonic-gate  *	't' function letter to display table
66487c478bd9Sstevel@tonic-gate  */
66497c478bd9Sstevel@tonic-gate 
66507c478bd9Sstevel@tonic-gate /*
66517c478bd9Sstevel@tonic-gate  * New functions for ACLs and other security attributes
66527c478bd9Sstevel@tonic-gate  */
66537c478bd9Sstevel@tonic-gate 
66547c478bd9Sstevel@tonic-gate /*
66557c478bd9Sstevel@tonic-gate  * The function appends the new security attribute info to the end of
66567c478bd9Sstevel@tonic-gate  * existing secinfo.
66577c478bd9Sstevel@tonic-gate  */
66587c478bd9Sstevel@tonic-gate int
append_secattr(char ** secinfo,int * secinfo_len,int size,char * attrtext,char attr_type)66597c478bd9Sstevel@tonic-gate append_secattr(
66607c478bd9Sstevel@tonic-gate 	char	 **secinfo,	/* existing security info */
66617c478bd9Sstevel@tonic-gate 	int	 *secinfo_len,	/* length of existing security info */
666245916cd2Sjpk 	int	 size,		/* new attribute size: unit depends on type */
666345916cd2Sjpk 	char	*attrtext,	/* new attribute text */
666445916cd2Sjpk 	char	 attr_type)	/* new attribute type */
66657c478bd9Sstevel@tonic-gate {
66667c478bd9Sstevel@tonic-gate 	char	*new_secinfo;
66677c478bd9Sstevel@tonic-gate 	int	newattrsize;
66687c478bd9Sstevel@tonic-gate 	int	oldsize;
666945916cd2Sjpk 	struct sec_attr	*attr;
66707c478bd9Sstevel@tonic-gate 
66717c478bd9Sstevel@tonic-gate 	/* no need to add */
667245916cd2Sjpk 	if (attr_type != DIR_TYPE) {
667345916cd2Sjpk 		if (attrtext == NULL)
66747c478bd9Sstevel@tonic-gate 			return (0);
667545916cd2Sjpk 	}
66767c478bd9Sstevel@tonic-gate 
667745916cd2Sjpk 	switch (attr_type) {
667845916cd2Sjpk 	case UFSD_ACL:
667945916cd2Sjpk 	case ACE_ACL:
66807c478bd9Sstevel@tonic-gate 		if (attrtext == NULL) {
66818e4a71aeSRich Burridge 			(void) fprintf(stderr, gettext("acltotext failed\n"));
66827c478bd9Sstevel@tonic-gate 			return (-1);
66837c478bd9Sstevel@tonic-gate 		}
66847c478bd9Sstevel@tonic-gate 		/* header: type + size = 8 */
66857c478bd9Sstevel@tonic-gate 		newattrsize = 8 + (int)strlen(attrtext) + 1;
66867c478bd9Sstevel@tonic-gate 		attr = (struct sec_attr *)malloc(newattrsize);
66877c478bd9Sstevel@tonic-gate 		if (attr == NULL) {
66888e4a71aeSRich Burridge 			(void) fprintf(stderr,
66898e4a71aeSRich Burridge 			    gettext("can't allocate memory\n"));
66907c478bd9Sstevel@tonic-gate 			return (-1);
66917c478bd9Sstevel@tonic-gate 		}
669245916cd2Sjpk 		attr->attr_type = attr_type;
66937c478bd9Sstevel@tonic-gate 		(void) sprintf(attr->attr_len,
669445916cd2Sjpk 		    "%06o", size); /* acl entry count */
66957c478bd9Sstevel@tonic-gate 		(void) strcpy((char *)&attr->attr_info[0], attrtext);
66967c478bd9Sstevel@tonic-gate 		free(attrtext);
66977c478bd9Sstevel@tonic-gate 		break;
66987c478bd9Sstevel@tonic-gate 
669945916cd2Sjpk 	/* Trusted Extensions */
670045916cd2Sjpk 	case DIR_TYPE:
670145916cd2Sjpk 	case LBL_TYPE:
670245916cd2Sjpk 		newattrsize = sizeof (struct sec_attr) + strlen(attrtext);
670345916cd2Sjpk 		attr = (struct sec_attr *)malloc(newattrsize);
670445916cd2Sjpk 		if (attr == NULL) {
670545916cd2Sjpk 			(void) fprintf(stderr,
670645916cd2Sjpk 			gettext("can't allocate memory\n"));
670745916cd2Sjpk 			return (-1);
670845916cd2Sjpk 		}
670945916cd2Sjpk 		attr->attr_type = attr_type;
671045916cd2Sjpk 		(void) sprintf(attr->attr_len,
671145916cd2Sjpk 		    "%06d", size); /* len of attr data */
671245916cd2Sjpk 		(void) strcpy((char *)&attr->attr_info[0], attrtext);
671345916cd2Sjpk 		break;
67147c478bd9Sstevel@tonic-gate 
67157c478bd9Sstevel@tonic-gate 	default:
67168e4a71aeSRich Burridge 		(void) fprintf(stderr,
67178e4a71aeSRich Burridge 		    gettext("unrecognized attribute type\n"));
67187c478bd9Sstevel@tonic-gate 		return (-1);
67197c478bd9Sstevel@tonic-gate 	}
67207c478bd9Sstevel@tonic-gate 
67217c478bd9Sstevel@tonic-gate 	/* old security info + new attr header(8) + new attr */
67227c478bd9Sstevel@tonic-gate 	oldsize = *secinfo_len;
67237c478bd9Sstevel@tonic-gate 	*secinfo_len += newattrsize;
67247c478bd9Sstevel@tonic-gate 	new_secinfo = (char *)malloc(*secinfo_len);
67257c478bd9Sstevel@tonic-gate 	if (new_secinfo == NULL) {
67268e4a71aeSRich Burridge 		(void) fprintf(stderr, gettext("can't allocate memory\n"));
67277c478bd9Sstevel@tonic-gate 		*secinfo_len -= newattrsize;
672845916cd2Sjpk 		free(attr);
67297c478bd9Sstevel@tonic-gate 		return (-1);
67307c478bd9Sstevel@tonic-gate 	}
67317c478bd9Sstevel@tonic-gate 
67327c478bd9Sstevel@tonic-gate 	(void) memcpy(new_secinfo, *secinfo, oldsize);
67337c478bd9Sstevel@tonic-gate 	(void) memcpy(new_secinfo + oldsize, attr, newattrsize);
67347c478bd9Sstevel@tonic-gate 
67357c478bd9Sstevel@tonic-gate 	free(*secinfo);
673645916cd2Sjpk 	free(attr);
67377c478bd9Sstevel@tonic-gate 	*secinfo = new_secinfo;
67387c478bd9Sstevel@tonic-gate 	return (0);
67397c478bd9Sstevel@tonic-gate }
67407c478bd9Sstevel@tonic-gate 
67417c478bd9Sstevel@tonic-gate /*
67427c478bd9Sstevel@tonic-gate  * write_ancillary(): write out an ancillary file.
67437c478bd9Sstevel@tonic-gate  *      The file has the same header as normal file except the type and size
67447c478bd9Sstevel@tonic-gate  *      fields. The type is 'A' and size is the sum of all attributes
67457c478bd9Sstevel@tonic-gate  *	in bytes.
67467c478bd9Sstevel@tonic-gate  *	The body contains a list of attribute type, size and info. Currently,
67477c478bd9Sstevel@tonic-gate  *	there is only ACL info.  This file is put before the normal file.
67487c478bd9Sstevel@tonic-gate  */
67497c478bd9Sstevel@tonic-gate void
write_ancillary(union hblock * dblockp,char * secinfo,int len,char hdrtype)67507c478bd9Sstevel@tonic-gate write_ancillary(union hblock *dblockp, char *secinfo, int len, char hdrtype)
67517c478bd9Sstevel@tonic-gate {
67527c478bd9Sstevel@tonic-gate 	long    blocks;
67537c478bd9Sstevel@tonic-gate 	int	savflag;
67547c478bd9Sstevel@tonic-gate 	int	savsize;
67557c478bd9Sstevel@tonic-gate 
67567c478bd9Sstevel@tonic-gate 	/* Just tranditional permissions or no security attribute info */
67577c478bd9Sstevel@tonic-gate 	if (len == 0 || secinfo == NULL)
67587c478bd9Sstevel@tonic-gate 		return;
67597c478bd9Sstevel@tonic-gate 
67607c478bd9Sstevel@tonic-gate 	/* save flag and size */
67617c478bd9Sstevel@tonic-gate 	savflag = (dblockp->dbuf).typeflag;
67627c478bd9Sstevel@tonic-gate 	(void) sscanf(dblockp->dbuf.size, "%12o", (uint_t *)&savsize);
67637c478bd9Sstevel@tonic-gate 
67647c478bd9Sstevel@tonic-gate 	/* special flag for ancillary file */
67657c478bd9Sstevel@tonic-gate 	if (hdrtype == _XATTR_HDRTYPE)
67667c478bd9Sstevel@tonic-gate 		dblockp->dbuf.typeflag = _XATTR_HDRTYPE;
67677c478bd9Sstevel@tonic-gate 	else
67687c478bd9Sstevel@tonic-gate 		dblockp->dbuf.typeflag = 'A';
67697c478bd9Sstevel@tonic-gate 
67707c478bd9Sstevel@tonic-gate 	/* for pre-2.5 versions of tar, need to make sure */
67717c478bd9Sstevel@tonic-gate 	/* the ACL file is readable			  */
67727c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.mode, "%07lo",
67737c478bd9Sstevel@tonic-gate 	    (stbuf.st_mode & POSIXMODES) | 0000200);
67747c478bd9Sstevel@tonic-gate 	(void) sprintf(dblockp->dbuf.size, "%011o", len);
67757c478bd9Sstevel@tonic-gate 	(void) sprintf(dblockp->dbuf.chksum, "%07o", checksum(dblockp));
67767c478bd9Sstevel@tonic-gate 
67777c478bd9Sstevel@tonic-gate 	/* write out the header */
67787c478bd9Sstevel@tonic-gate 	(void) writetbuf((char *)dblockp, 1);
67797c478bd9Sstevel@tonic-gate 
67807c478bd9Sstevel@tonic-gate 	/* write out security info */
67817c478bd9Sstevel@tonic-gate 	blocks = TBLOCKS(len);
67827c478bd9Sstevel@tonic-gate 	(void) writetbuf((char *)secinfo, (int)blocks);
67837c478bd9Sstevel@tonic-gate 
67847c478bd9Sstevel@tonic-gate 	/* restore mode, flag and size */
67857c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.mode, "%07lo", stbuf.st_mode & POSIXMODES);
67867c478bd9Sstevel@tonic-gate 	dblockp->dbuf.typeflag = savflag;
67877c478bd9Sstevel@tonic-gate 	(void) sprintf(dblockp->dbuf.size, "%011o", savsize);
67887c478bd9Sstevel@tonic-gate }
67897c478bd9Sstevel@tonic-gate 
67907c478bd9Sstevel@tonic-gate /*
67917c478bd9Sstevel@tonic-gate  * Read the data record for extended headers and then the regular header.
67927c478bd9Sstevel@tonic-gate  * The data are read into the buffer and then null-terminated.  Entries
6793e765faefSRich Burridge  * for typeflag 'X' extended headers are of the format:
67947c478bd9Sstevel@tonic-gate  * 	"%d %s=%s\n"
67957c478bd9Sstevel@tonic-gate  *
67967c478bd9Sstevel@tonic-gate  * When an extended header record is found, the extended header must
67977c478bd9Sstevel@tonic-gate  * be processed and its values used to override the values in the
67987c478bd9Sstevel@tonic-gate  * normal header.  The way this is done is to process the extended
67997c478bd9Sstevel@tonic-gate  * header data record and set the data values, then call getdir
68007c478bd9Sstevel@tonic-gate  * to process the regular header, then then to reconcile the two
68017c478bd9Sstevel@tonic-gate  * sets of data.
68027c478bd9Sstevel@tonic-gate  */
68037c478bd9Sstevel@tonic-gate 
68047c478bd9Sstevel@tonic-gate static int
get_xdata(void)68057c478bd9Sstevel@tonic-gate get_xdata(void)
68067c478bd9Sstevel@tonic-gate {
68077c478bd9Sstevel@tonic-gate 	struct keylist_pair {
68087c478bd9Sstevel@tonic-gate 		int keynum;
68097c478bd9Sstevel@tonic-gate 		char *keylist;
68107c478bd9Sstevel@tonic-gate 	}	keylist_pair[] = {	_X_DEVMAJOR, "SUN.devmajor",
68117c478bd9Sstevel@tonic-gate 					_X_DEVMINOR, "SUN.devminor",
68127c478bd9Sstevel@tonic-gate 					_X_GID, "gid",
68137c478bd9Sstevel@tonic-gate 					_X_GNAME, "gname",
68147c478bd9Sstevel@tonic-gate 					_X_LINKPATH, "linkpath",
68157c478bd9Sstevel@tonic-gate 					_X_PATH, "path",
68167c478bd9Sstevel@tonic-gate 					_X_SIZE, "size",
68177c478bd9Sstevel@tonic-gate 					_X_UID, "uid",
68187c478bd9Sstevel@tonic-gate 					_X_UNAME, "uname",
68197c478bd9Sstevel@tonic-gate 					_X_MTIME, "mtime",
68207c478bd9Sstevel@tonic-gate 					_X_LAST, "NULL" };
68217c478bd9Sstevel@tonic-gate 	char		*lineloc;
68227c478bd9Sstevel@tonic-gate 	int		length, i;
68237c478bd9Sstevel@tonic-gate 	char		*keyword, *value;
68247c478bd9Sstevel@tonic-gate 	blkcnt_t	nblocks;
68257c478bd9Sstevel@tonic-gate 	int		bufneeded;
68267c478bd9Sstevel@tonic-gate 	int		errors;
68277c478bd9Sstevel@tonic-gate 
6828123523f8Sas158974 	(void) memset(&Xtarhdr, 0, sizeof (Xtarhdr));
68297c478bd9Sstevel@tonic-gate 	xhdr_count++;
68307c478bd9Sstevel@tonic-gate 	errors = 0;
68317c478bd9Sstevel@tonic-gate 
68327c478bd9Sstevel@tonic-gate 	nblocks = TBLOCKS(stbuf.st_size);
68337c478bd9Sstevel@tonic-gate 	bufneeded = nblocks * TBLOCK;
68347c478bd9Sstevel@tonic-gate 	if (bufneeded >= xrec_size) {
68357c478bd9Sstevel@tonic-gate 		free(xrec_ptr);
68367c478bd9Sstevel@tonic-gate 		xrec_size = bufneeded + 1;
68377c478bd9Sstevel@tonic-gate 		if ((xrec_ptr = malloc(xrec_size)) == NULL)
68387c478bd9Sstevel@tonic-gate 			fatal(gettext("cannot allocate buffer"));
68397c478bd9Sstevel@tonic-gate 	}
68407c478bd9Sstevel@tonic-gate 
68417c478bd9Sstevel@tonic-gate 	lineloc = xrec_ptr;
68427c478bd9Sstevel@tonic-gate 
68437c478bd9Sstevel@tonic-gate 	while (nblocks-- > 0) {
68447c478bd9Sstevel@tonic-gate 		readtape(lineloc);
68457c478bd9Sstevel@tonic-gate 		lineloc += TBLOCK;
68467c478bd9Sstevel@tonic-gate 	}
68477c478bd9Sstevel@tonic-gate 	lineloc = xrec_ptr;
68487c478bd9Sstevel@tonic-gate 	xrec_ptr[stbuf.st_size] = '\0';
68497c478bd9Sstevel@tonic-gate 	while (lineloc < xrec_ptr + stbuf.st_size) {
6850e765faefSRich Burridge 		if (dblock.dbuf.typeflag == 'L') {
6851e765faefSRich Burridge 			length = xrec_size;
6852e765faefSRich Burridge 			keyword = "path";
6853e765faefSRich Burridge 			value = lineloc;
6854e765faefSRich Burridge 		} else {
68557c478bd9Sstevel@tonic-gate 			length = atoi(lineloc);
68567c478bd9Sstevel@tonic-gate 			*(lineloc + length - 1) = '\0';
68577c478bd9Sstevel@tonic-gate 			keyword = strchr(lineloc, ' ') + 1;
68587c478bd9Sstevel@tonic-gate 			value = strchr(keyword, '=') + 1;
68597c478bd9Sstevel@tonic-gate 			*(value - 1) = '\0';
6860e765faefSRich Burridge 		}
68617c478bd9Sstevel@tonic-gate 		i = 0;
68627c478bd9Sstevel@tonic-gate 		lineloc += length;
68637c478bd9Sstevel@tonic-gate 		while (keylist_pair[i].keynum != (int)_X_LAST) {
68647c478bd9Sstevel@tonic-gate 			if (strcmp(keyword, keylist_pair[i].keylist) == 0)
68657c478bd9Sstevel@tonic-gate 				break;
68667c478bd9Sstevel@tonic-gate 			i++;
68677c478bd9Sstevel@tonic-gate 		}
68687c478bd9Sstevel@tonic-gate 		errno = 0;
68697c478bd9Sstevel@tonic-gate 		switch (keylist_pair[i].keynum) {
68707c478bd9Sstevel@tonic-gate 		case _X_DEVMAJOR:
68717c478bd9Sstevel@tonic-gate 			Xtarhdr.x_devmajor = (major_t)strtoul(value, NULL, 0);
68727c478bd9Sstevel@tonic-gate 			if (errno) {
68737c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
68747c478bd9Sstevel@tonic-gate 				    "tar: Extended header major value error "
68757c478bd9Sstevel@tonic-gate 				    "for file # %llu.\n"), xhdr_count);
68767c478bd9Sstevel@tonic-gate 				errors++;
68777c478bd9Sstevel@tonic-gate 			} else
68787c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_DEVMAJOR;
68797c478bd9Sstevel@tonic-gate 			break;
68807c478bd9Sstevel@tonic-gate 		case _X_DEVMINOR:
68817c478bd9Sstevel@tonic-gate 			Xtarhdr.x_devminor = (minor_t)strtoul(value, NULL, 0);
68827c478bd9Sstevel@tonic-gate 			if (errno) {
68837c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
68847c478bd9Sstevel@tonic-gate 				    "tar: Extended header minor value error "
68857c478bd9Sstevel@tonic-gate 				    "for file # %llu.\n"), xhdr_count);
68867c478bd9Sstevel@tonic-gate 				errors++;
68877c478bd9Sstevel@tonic-gate 			} else
68887c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_DEVMINOR;
68897c478bd9Sstevel@tonic-gate 			break;
68907c478bd9Sstevel@tonic-gate 		case _X_GID:
68917c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_GID;
68927c478bd9Sstevel@tonic-gate 			Xtarhdr.x_gid = strtol(value, NULL, 0);
68937c478bd9Sstevel@tonic-gate 			if ((errno) || (Xtarhdr.x_gid > UID_MAX)) {
68947c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
68957c478bd9Sstevel@tonic-gate 				    "tar: Extended header gid value error "
68967c478bd9Sstevel@tonic-gate 				    "for file # %llu.\n"), xhdr_count);
68977c478bd9Sstevel@tonic-gate 				Xtarhdr.x_gid = GID_NOBODY;
68987c478bd9Sstevel@tonic-gate 			}
68997c478bd9Sstevel@tonic-gate 			break;
69007c478bd9Sstevel@tonic-gate 		case _X_GNAME:
69017c478bd9Sstevel@tonic-gate 			if (utf8_local("gname", &Xtarhdr.x_gname,
69027c478bd9Sstevel@tonic-gate 			    local_gname, value, _POSIX_NAME_MAX) == 0)
69037c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_GNAME;
69047c478bd9Sstevel@tonic-gate 			break;
69057c478bd9Sstevel@tonic-gate 		case _X_LINKPATH:
69067c478bd9Sstevel@tonic-gate 			if (utf8_local("linkpath", &Xtarhdr.x_linkpath,
69077c478bd9Sstevel@tonic-gate 			    local_linkpath, value, PATH_MAX) == 0)
69087c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_LINKPATH;
69097c478bd9Sstevel@tonic-gate 			else
69107c478bd9Sstevel@tonic-gate 				errors++;
69117c478bd9Sstevel@tonic-gate 			break;
69127c478bd9Sstevel@tonic-gate 		case _X_PATH:
69137c478bd9Sstevel@tonic-gate 			if (utf8_local("path", &Xtarhdr.x_path,
69147c478bd9Sstevel@tonic-gate 			    local_path, value, PATH_MAX) == 0)
69157c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_PATH;
69167c478bd9Sstevel@tonic-gate 			else
69177c478bd9Sstevel@tonic-gate 				errors++;
69187c478bd9Sstevel@tonic-gate 			break;
69197c478bd9Sstevel@tonic-gate 		case _X_SIZE:
69207c478bd9Sstevel@tonic-gate 			Xtarhdr.x_filesz = strtoull(value, NULL, 0);
69217c478bd9Sstevel@tonic-gate 			if (errno) {
69227c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
69237c478bd9Sstevel@tonic-gate 				    "tar: Extended header invalid filesize "
69247c478bd9Sstevel@tonic-gate 				    "for file # %llu.\n"), xhdr_count);
69257c478bd9Sstevel@tonic-gate 				errors++;
69267c478bd9Sstevel@tonic-gate 			} else
69277c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_SIZE;
69287c478bd9Sstevel@tonic-gate 			break;
69297c478bd9Sstevel@tonic-gate 		case _X_UID:
69307c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_UID;
69317c478bd9Sstevel@tonic-gate 			Xtarhdr.x_uid = strtol(value, NULL, 0);
69327c478bd9Sstevel@tonic-gate 			if ((errno) || (Xtarhdr.x_uid > UID_MAX)) {
69337c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
69347c478bd9Sstevel@tonic-gate 				    "tar: Extended header uid value error "
69357c478bd9Sstevel@tonic-gate 				    "for file # %llu.\n"), xhdr_count);
69367c478bd9Sstevel@tonic-gate 				Xtarhdr.x_uid = UID_NOBODY;
69377c478bd9Sstevel@tonic-gate 			}
69387c478bd9Sstevel@tonic-gate 			break;
69397c478bd9Sstevel@tonic-gate 		case _X_UNAME:
69407c478bd9Sstevel@tonic-gate 			if (utf8_local("uname", &Xtarhdr.x_uname,
69417c478bd9Sstevel@tonic-gate 			    local_uname, value, _POSIX_NAME_MAX) == 0)
69427c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_UNAME;
69437c478bd9Sstevel@tonic-gate 			break;
69447c478bd9Sstevel@tonic-gate 		case _X_MTIME:
69457c478bd9Sstevel@tonic-gate 			get_xtime(value, &(Xtarhdr.x_mtime));
69467c478bd9Sstevel@tonic-gate 			if (errno)
69477c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
69487c478bd9Sstevel@tonic-gate 				    "tar: Extended header modification time "
69497c478bd9Sstevel@tonic-gate 				    "value error for file # %llu.\n"),
69507c478bd9Sstevel@tonic-gate 				    xhdr_count);
69517c478bd9Sstevel@tonic-gate 			else
69527c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_MTIME;
69537c478bd9Sstevel@tonic-gate 			break;
69547c478bd9Sstevel@tonic-gate 		default:
69557c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
69567c478bd9Sstevel@tonic-gate 			    gettext("tar:  unrecognized extended"
69577c478bd9Sstevel@tonic-gate 			    " header keyword '%s'.  Ignored.\n"), keyword);
69587c478bd9Sstevel@tonic-gate 			break;
69597c478bd9Sstevel@tonic-gate 		}
69607c478bd9Sstevel@tonic-gate 	}
69617c478bd9Sstevel@tonic-gate 
69627c478bd9Sstevel@tonic-gate 	getdir();	/* get regular header */
69637c478bd9Sstevel@tonic-gate 	if (errors && errflag)
69647c478bd9Sstevel@tonic-gate 		done(1);
69657c478bd9Sstevel@tonic-gate 	else
69667c478bd9Sstevel@tonic-gate 		if (errors)
69677c478bd9Sstevel@tonic-gate 			Errflg = 1;
69687c478bd9Sstevel@tonic-gate 	return (errors);
69697c478bd9Sstevel@tonic-gate }
69707c478bd9Sstevel@tonic-gate 
69717c478bd9Sstevel@tonic-gate /*
6972123523f8Sas158974  * load_info_from_xtarhdr - sets Gen and stbuf variables from
6973123523f8Sas158974  *	extended header
6974123523f8Sas158974  *	load_info_from_xtarhdr(flag, xhdrp);
6975123523f8Sas158974  *	u_longlong_t flag;	xhdr_flgs
6976123523f8Sas158974  *	struct xtar_hdr *xhdrp; pointer to extended header
6977123523f8Sas158974  *	NOTE:	called when typeflag is not 'A' and xhdr_flgs
6978123523f8Sas158974  *		is set.
6979123523f8Sas158974  */
6980123523f8Sas158974 static void
load_info_from_xtarhdr(u_longlong_t flag,struct xtar_hdr * xhdrp)6981123523f8Sas158974 load_info_from_xtarhdr(u_longlong_t flag, struct xtar_hdr *xhdrp)
6982123523f8Sas158974 {
6983123523f8Sas158974 	if (flag & _X_DEVMAJOR) {
6984123523f8Sas158974 		Gen.g_devmajor = xhdrp->x_devmajor;
6985123523f8Sas158974 	}
6986123523f8Sas158974 	if (flag & _X_DEVMINOR) {
6987123523f8Sas158974 		Gen.g_devminor = xhdrp->x_devminor;
6988123523f8Sas158974 	}
6989123523f8Sas158974 	if (flag & _X_GID) {
6990123523f8Sas158974 		Gen.g_gid = xhdrp->x_gid;
6991123523f8Sas158974 		stbuf.st_gid = xhdrp->x_gid;
6992123523f8Sas158974 	}
6993123523f8Sas158974 	if (flag & _X_UID) {
6994123523f8Sas158974 		Gen.g_uid = xhdrp->x_uid;
6995123523f8Sas158974 		stbuf.st_uid  = xhdrp->x_uid;
6996123523f8Sas158974 	}
6997123523f8Sas158974 	if (flag & _X_SIZE) {
6998123523f8Sas158974 		Gen.g_filesz = xhdrp->x_filesz;
6999123523f8Sas158974 		stbuf.st_size = xhdrp->x_filesz;
7000123523f8Sas158974 	}
7001123523f8Sas158974 	if (flag & _X_MTIME) {
7002123523f8Sas158974 		Gen.g_mtime = xhdrp->x_mtime.tv_sec;
7003123523f8Sas158974 		stbuf.st_mtim.tv_sec = xhdrp->x_mtime.tv_sec;
7004123523f8Sas158974 		stbuf.st_mtim.tv_nsec = xhdrp->x_mtime.tv_nsec;
7005123523f8Sas158974 	}
7006123523f8Sas158974 }
7007123523f8Sas158974 
7008123523f8Sas158974 /*
70097c478bd9Sstevel@tonic-gate  * gen_num creates a string from a keyword and an usigned long long in the
70107c478bd9Sstevel@tonic-gate  * format:  %d %s=%s\n
70117c478bd9Sstevel@tonic-gate  * This is part of the extended header data record.
70127c478bd9Sstevel@tonic-gate  */
70137c478bd9Sstevel@tonic-gate 
70147c478bd9Sstevel@tonic-gate void
gen_num(const char * keyword,const u_longlong_t number)70157c478bd9Sstevel@tonic-gate gen_num(const char *keyword, const u_longlong_t number)
70167c478bd9Sstevel@tonic-gate {
70177c478bd9Sstevel@tonic-gate 	char	save_val[ULONGLONG_MAX_DIGITS + 1];
70187c478bd9Sstevel@tonic-gate 	int	len;
70197c478bd9Sstevel@tonic-gate 	char	*curr_ptr;
70207c478bd9Sstevel@tonic-gate 
70217c478bd9Sstevel@tonic-gate 	(void) sprintf(save_val, "%llu", number);
70227c478bd9Sstevel@tonic-gate 	/*
70237c478bd9Sstevel@tonic-gate 	 * len = length of entire line, including itself.  len will be
70247c478bd9Sstevel@tonic-gate 	 * two digits.  So, add the string lengths plus the length of len,
70257c478bd9Sstevel@tonic-gate 	 * plus a blank, an equal sign, and a newline.
70267c478bd9Sstevel@tonic-gate 	 */
70277c478bd9Sstevel@tonic-gate 	len = strlen(save_val) + strlen(keyword) + 5;
70287c478bd9Sstevel@tonic-gate 	if (xrec_offset + len > xrec_size) {
70297c478bd9Sstevel@tonic-gate 		if (((curr_ptr = realloc(xrec_ptr, 2 * xrec_size)) == NULL))
70307c478bd9Sstevel@tonic-gate 			fatal(gettext(
70317c478bd9Sstevel@tonic-gate 			    "cannot allocate extended header buffer"));
70327c478bd9Sstevel@tonic-gate 		xrec_ptr = curr_ptr;
70337c478bd9Sstevel@tonic-gate 		xrec_size *= 2;
70347c478bd9Sstevel@tonic-gate 	}
70357c478bd9Sstevel@tonic-gate 	(void) sprintf(&xrec_ptr[xrec_offset],
70367c478bd9Sstevel@tonic-gate 	    "%d %s=%s\n", len, keyword, save_val);
70377c478bd9Sstevel@tonic-gate 	xrec_offset += len;
70387c478bd9Sstevel@tonic-gate }
70397c478bd9Sstevel@tonic-gate 
70407c478bd9Sstevel@tonic-gate /*
70417c478bd9Sstevel@tonic-gate  * gen_date creates a string from a keyword and a timestruc_t in the
70427c478bd9Sstevel@tonic-gate  * format:  %d %s=%s\n
70437c478bd9Sstevel@tonic-gate  * This is part of the extended header data record.
70447c478bd9Sstevel@tonic-gate  * Currently, granularity is only microseconds, so the low-order three digits
70457c478bd9Sstevel@tonic-gate  * will be truncated.
70467c478bd9Sstevel@tonic-gate  */
70477c478bd9Sstevel@tonic-gate 
70487c478bd9Sstevel@tonic-gate void
gen_date(const char * keyword,const timestruc_t time_value)70497c478bd9Sstevel@tonic-gate gen_date(const char *keyword, const timestruc_t time_value)
70507c478bd9Sstevel@tonic-gate {
70517c478bd9Sstevel@tonic-gate 	/* Allow for <seconds>.<nanoseconds>\n */
70527c478bd9Sstevel@tonic-gate 	char	save_val[TIME_MAX_DIGITS + LONG_MAX_DIGITS + 2];
70537c478bd9Sstevel@tonic-gate 	int	len;
70547c478bd9Sstevel@tonic-gate 	char	*curr_ptr;
70557c478bd9Sstevel@tonic-gate 
70567c478bd9Sstevel@tonic-gate 	(void) sprintf(save_val, "%ld", time_value.tv_sec);
70577c478bd9Sstevel@tonic-gate 	len = strlen(save_val);
70587c478bd9Sstevel@tonic-gate 	save_val[len] = '.';
70597c478bd9Sstevel@tonic-gate 	(void) sprintf(&save_val[len + 1], "%9.9ld", time_value.tv_nsec);
70607c478bd9Sstevel@tonic-gate 
70617c478bd9Sstevel@tonic-gate 	/*
70627c478bd9Sstevel@tonic-gate 	 * len = length of entire line, including itself.  len will be
70637c478bd9Sstevel@tonic-gate 	 * two digits.  So, add the string lengths plus the length of len,
70647c478bd9Sstevel@tonic-gate 	 * plus a blank, an equal sign, and a newline.
70657c478bd9Sstevel@tonic-gate 	 */
70667c478bd9Sstevel@tonic-gate 	len = strlen(save_val) + strlen(keyword) + 5;
70677c478bd9Sstevel@tonic-gate 	if (xrec_offset + len > xrec_size) {
70687c478bd9Sstevel@tonic-gate 		if (((curr_ptr = realloc(xrec_ptr, 2 * xrec_size)) == NULL))
70697c478bd9Sstevel@tonic-gate 			fatal(gettext(
70707c478bd9Sstevel@tonic-gate 			    "cannot allocate extended header buffer"));
70717c478bd9Sstevel@tonic-gate 		xrec_ptr = curr_ptr;
70727c478bd9Sstevel@tonic-gate 		xrec_size *= 2;
70737c478bd9Sstevel@tonic-gate 	}
70747c478bd9Sstevel@tonic-gate 	(void) sprintf(&xrec_ptr[xrec_offset],
70757c478bd9Sstevel@tonic-gate 	    "%d %s=%s\n", len, keyword, save_val);
70767c478bd9Sstevel@tonic-gate 	xrec_offset += len;
70777c478bd9Sstevel@tonic-gate }
70787c478bd9Sstevel@tonic-gate 
70797c478bd9Sstevel@tonic-gate /*
70807c478bd9Sstevel@tonic-gate  * gen_string creates a string from a keyword and a char * in the
70817c478bd9Sstevel@tonic-gate  * format:  %d %s=%s\n
70827c478bd9Sstevel@tonic-gate  * This is part of the extended header data record.
70837c478bd9Sstevel@tonic-gate  */
70847c478bd9Sstevel@tonic-gate 
70857c478bd9Sstevel@tonic-gate void
gen_string(const char * keyword,const char * value)70867c478bd9Sstevel@tonic-gate gen_string(const char *keyword, const char *value)
70877c478bd9Sstevel@tonic-gate {
70887c478bd9Sstevel@tonic-gate 	int	len;
70897c478bd9Sstevel@tonic-gate 	char	*curr_ptr;
70907c478bd9Sstevel@tonic-gate 
70917c478bd9Sstevel@tonic-gate 	/*
70927c478bd9Sstevel@tonic-gate 	 * len = length of entire line, including itself.  The character length
70937c478bd9Sstevel@tonic-gate 	 * of len must be 1-4 characters, because the maximum size of the path
70947c478bd9Sstevel@tonic-gate 	 * or the name is PATH_MAX, which is 1024.  So, assume 1 character
70957c478bd9Sstevel@tonic-gate 	 * for len, one for the space, one for the "=", and one for the newline.
70967c478bd9Sstevel@tonic-gate 	 * Then adjust as needed.
70977c478bd9Sstevel@tonic-gate 	 */
70987c478bd9Sstevel@tonic-gate 	/* LINTED constant expression */
70997c478bd9Sstevel@tonic-gate 	assert(PATH_MAX <= 9996);
71007c478bd9Sstevel@tonic-gate 	len = strlen(value) + strlen(keyword) + 4;
71017c478bd9Sstevel@tonic-gate 	if (len > 997)
71027c478bd9Sstevel@tonic-gate 		len += 3;
71037c478bd9Sstevel@tonic-gate 	else if (len > 98)
71047c478bd9Sstevel@tonic-gate 		len += 2;
71057c478bd9Sstevel@tonic-gate 	else if (len > 9)
71067c478bd9Sstevel@tonic-gate 		len += 1;
71077c478bd9Sstevel@tonic-gate 	if (xrec_offset + len > xrec_size) {
71087c478bd9Sstevel@tonic-gate 		if (((curr_ptr = realloc(xrec_ptr, 2 * xrec_size)) == NULL))
71097c478bd9Sstevel@tonic-gate 			fatal(gettext(
71107c478bd9Sstevel@tonic-gate 			    "cannot allocate extended header buffer"));
71117c478bd9Sstevel@tonic-gate 		xrec_ptr = curr_ptr;
71127c478bd9Sstevel@tonic-gate 		xrec_size *= 2;
71137c478bd9Sstevel@tonic-gate 	}
71147c478bd9Sstevel@tonic-gate #ifdef XHDR_DEBUG
71157c478bd9Sstevel@tonic-gate 	if (strcmp(keyword+1, "name") != 0)
71167c478bd9Sstevel@tonic-gate #endif
71177c478bd9Sstevel@tonic-gate 	(void) sprintf(&xrec_ptr[xrec_offset],
71187c478bd9Sstevel@tonic-gate 	    "%d %s=%s\n", len, keyword, value);
71197c478bd9Sstevel@tonic-gate #ifdef XHDR_DEBUG
71207c478bd9Sstevel@tonic-gate 	else {
71217c478bd9Sstevel@tonic-gate 	len += 11;
71227c478bd9Sstevel@tonic-gate 	(void) sprintf(&xrec_ptr[xrec_offset],
71237c478bd9Sstevel@tonic-gate 	    "%d %s=%snametoolong\n", len, keyword, value);
71247c478bd9Sstevel@tonic-gate 	}
71257c478bd9Sstevel@tonic-gate #endif
71267c478bd9Sstevel@tonic-gate 	xrec_offset += len;
71277c478bd9Sstevel@tonic-gate }
71287c478bd9Sstevel@tonic-gate 
71297c478bd9Sstevel@tonic-gate /*
71307c478bd9Sstevel@tonic-gate  * Convert time found in the extended header data to seconds and nanoseconds.
71317c478bd9Sstevel@tonic-gate  */
71327c478bd9Sstevel@tonic-gate 
71337c478bd9Sstevel@tonic-gate void
get_xtime(char * value,timestruc_t * xtime)71347c478bd9Sstevel@tonic-gate get_xtime(char *value, timestruc_t *xtime)
71357c478bd9Sstevel@tonic-gate {
71367c478bd9Sstevel@tonic-gate 	char nanosec[10];
71377c478bd9Sstevel@tonic-gate 	char *period;
71387c478bd9Sstevel@tonic-gate 	int i;
71397c478bd9Sstevel@tonic-gate 
71407c478bd9Sstevel@tonic-gate 	(void) memset(nanosec, '0', 9);
71417c478bd9Sstevel@tonic-gate 	nanosec[9] = '\0';
71427c478bd9Sstevel@tonic-gate 
71437c478bd9Sstevel@tonic-gate 	period = strchr(value, '.');
71447c478bd9Sstevel@tonic-gate 	if (period != NULL)
71457c478bd9Sstevel@tonic-gate 		period[0] = '\0';
71467c478bd9Sstevel@tonic-gate 	xtime->tv_sec = strtol(value, NULL, 10);
71477c478bd9Sstevel@tonic-gate 	if (period == NULL)
71487c478bd9Sstevel@tonic-gate 		xtime->tv_nsec = 0;
71497c478bd9Sstevel@tonic-gate 	else {
71507c478bd9Sstevel@tonic-gate 		i = strlen(period +1);
71517c478bd9Sstevel@tonic-gate 		(void) strncpy(nanosec, period + 1, min(i, 9));
71527c478bd9Sstevel@tonic-gate 		xtime->tv_nsec = strtol(nanosec, NULL, 10);
71537c478bd9Sstevel@tonic-gate 	}
71547c478bd9Sstevel@tonic-gate }
71557c478bd9Sstevel@tonic-gate 
71567c478bd9Sstevel@tonic-gate /*
71577c478bd9Sstevel@tonic-gate  *	Check linkpath for length.
71587c478bd9Sstevel@tonic-gate  *	Emit an error message and return 1 if too long.
71597c478bd9Sstevel@tonic-gate  */
71607c478bd9Sstevel@tonic-gate 
71617c478bd9Sstevel@tonic-gate int
chk_path_build(char * name,char * longname,char * linkname,char * prefix,char type,int filetype)71627c478bd9Sstevel@tonic-gate chk_path_build(
71637c478bd9Sstevel@tonic-gate 	char	*name,
71647c478bd9Sstevel@tonic-gate 	char	*longname,
71657c478bd9Sstevel@tonic-gate 	char	*linkname,
71667c478bd9Sstevel@tonic-gate 	char	*prefix,
71677c478bd9Sstevel@tonic-gate 	char	type,
71687c478bd9Sstevel@tonic-gate 	int	filetype)
71697c478bd9Sstevel@tonic-gate {
71707c478bd9Sstevel@tonic-gate 
71717c478bd9Sstevel@tonic-gate 	if (strlen(linkname) > (size_t)NAMSIZ) {
71727c478bd9Sstevel@tonic-gate 		if (Eflag > 0) {
71737c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_LINKPATH;
71747c478bd9Sstevel@tonic-gate 			Xtarhdr.x_linkpath = linkname;
71757c478bd9Sstevel@tonic-gate 		} else {
71767c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
71777c478bd9Sstevel@tonic-gate 			    "tar: %s: linked to %s\n"), longname, linkname);
71787c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
71797c478bd9Sstevel@tonic-gate 			    "tar: %s: linked name too long\n"), linkname);
71807c478bd9Sstevel@tonic-gate 			if (errflag)
71817c478bd9Sstevel@tonic-gate 				done(1);
71827c478bd9Sstevel@tonic-gate 			else
71837c478bd9Sstevel@tonic-gate 				Errflg = 1;
71847c478bd9Sstevel@tonic-gate 			return (1);
71857c478bd9Sstevel@tonic-gate 		}
71867c478bd9Sstevel@tonic-gate 	}
71877c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_LINKPATH)
71887c478bd9Sstevel@tonic-gate 		return (build_dblock(name, tchar, type,
71897c478bd9Sstevel@tonic-gate 		    filetype, &stbuf, stbuf.st_dev,
71907c478bd9Sstevel@tonic-gate 		    prefix));
71917c478bd9Sstevel@tonic-gate 	else
71927c478bd9Sstevel@tonic-gate 		return (build_dblock(name, linkname, type,
71937c478bd9Sstevel@tonic-gate 		    filetype, &stbuf, stbuf.st_dev, prefix));
71947c478bd9Sstevel@tonic-gate }
71957c478bd9Sstevel@tonic-gate 
71967c478bd9Sstevel@tonic-gate /*
71977c478bd9Sstevel@tonic-gate  * Convert from UTF-8 to local character set.
71987c478bd9Sstevel@tonic-gate  */
71997c478bd9Sstevel@tonic-gate 
72007c478bd9Sstevel@tonic-gate static int
utf8_local(char * option,char ** Xhdr_ptrptr,char * target,const char * source,int max_val)72017c478bd9Sstevel@tonic-gate utf8_local(
72027c478bd9Sstevel@tonic-gate 	char		*option,
72037c478bd9Sstevel@tonic-gate 	char		**Xhdr_ptrptr,
72047c478bd9Sstevel@tonic-gate 	char		*target,
72057c478bd9Sstevel@tonic-gate 	const char	*source,
72067c478bd9Sstevel@tonic-gate 	int		max_val)
72077c478bd9Sstevel@tonic-gate {
72087c478bd9Sstevel@tonic-gate 	static	iconv_t	iconv_cd;
72097c478bd9Sstevel@tonic-gate 	char		*nl_target;
72107c478bd9Sstevel@tonic-gate 	const	char	*iconv_src;
72117c478bd9Sstevel@tonic-gate 	char		*iconv_trg;
7212eace40a5Sceastha 	size_t		inlen;
7213eace40a5Sceastha 	size_t		outlen;
72147c478bd9Sstevel@tonic-gate 
72157c478bd9Sstevel@tonic-gate 	if (charset_type == -1) {	/* iconv_open failed in earlier try */
72167c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
72177c478bd9Sstevel@tonic-gate 		    "tar:  file # %llu: (%s) UTF-8 conversion failed.\n"),
72187c478bd9Sstevel@tonic-gate 		    xhdr_count, source);
72197c478bd9Sstevel@tonic-gate 		return (1);
72207c478bd9Sstevel@tonic-gate 	} else if (charset_type == 0) {	/* iconv_open has not yet been done */
72217c478bd9Sstevel@tonic-gate 		nl_target = nl_langinfo(CODESET);
72227c478bd9Sstevel@tonic-gate 		if (strlen(nl_target) == 0)	/* locale using 7-bit codeset */
72237c478bd9Sstevel@tonic-gate 			nl_target = "646";
72247c478bd9Sstevel@tonic-gate 		if (strcmp(nl_target, "646") == 0)
72257c478bd9Sstevel@tonic-gate 			charset_type = 1;
72267c478bd9Sstevel@tonic-gate 		else if (strcmp(nl_target, "UTF-8") == 0)
72277c478bd9Sstevel@tonic-gate 			charset_type = 3;
72287c478bd9Sstevel@tonic-gate 		else {
72297c478bd9Sstevel@tonic-gate 			if (strncmp(nl_target, "ISO", 3) == 0)
72307c478bd9Sstevel@tonic-gate 				nl_target += 3;
72317c478bd9Sstevel@tonic-gate 			charset_type = 2;
72327c478bd9Sstevel@tonic-gate 			errno = 0;
72337c478bd9Sstevel@tonic-gate 			if ((iconv_cd = iconv_open(nl_target, "UTF-8")) ==
72347c478bd9Sstevel@tonic-gate 			    (iconv_t)-1) {
72357c478bd9Sstevel@tonic-gate 				if (errno == EINVAL)
72367c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
72377c478bd9Sstevel@tonic-gate 					    "tar: conversion routines not "
72387c478bd9Sstevel@tonic-gate 					    "available for current locale.  "));
72397c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
72407c478bd9Sstevel@tonic-gate 				    "file # %llu: (%s) UTF-8 conversion"
72417c478bd9Sstevel@tonic-gate 				    " failed.\n"), xhdr_count, source);
72427c478bd9Sstevel@tonic-gate 				charset_type = -1;
72437c478bd9Sstevel@tonic-gate 				return (1);
72447c478bd9Sstevel@tonic-gate 			}
72457c478bd9Sstevel@tonic-gate 		}
72467c478bd9Sstevel@tonic-gate 	}
72477c478bd9Sstevel@tonic-gate 
72487c478bd9Sstevel@tonic-gate 	/* locale using 7-bit codeset or UTF-8 locale */
72497c478bd9Sstevel@tonic-gate 	if (charset_type == 1 || charset_type == 3) {
72507c478bd9Sstevel@tonic-gate 		if (strlen(source) > max_val) {
72517c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
72527c478bd9Sstevel@tonic-gate 			    "tar: file # %llu: Extended header %s too long.\n"),
72537c478bd9Sstevel@tonic-gate 			    xhdr_count, option);
72547c478bd9Sstevel@tonic-gate 			return (1);
72557c478bd9Sstevel@tonic-gate 		}
72567c478bd9Sstevel@tonic-gate 		if (charset_type == 3)
72577c478bd9Sstevel@tonic-gate 			(void) strcpy(target, source);
72587c478bd9Sstevel@tonic-gate 		else if (c_utf8(target, source) != 0) {
72597c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
72607c478bd9Sstevel@tonic-gate 			    "tar:  file # %llu: (%s) UTF-8 conversion"
72617c478bd9Sstevel@tonic-gate 			    " failed.\n"), xhdr_count, source);
72627c478bd9Sstevel@tonic-gate 			return (1);
72637c478bd9Sstevel@tonic-gate 		}
72647c478bd9Sstevel@tonic-gate 		*Xhdr_ptrptr = target;
72657c478bd9Sstevel@tonic-gate 		return (0);
72667c478bd9Sstevel@tonic-gate 	}
72677c478bd9Sstevel@tonic-gate 
72687c478bd9Sstevel@tonic-gate 	iconv_src = source;
72697c478bd9Sstevel@tonic-gate 	iconv_trg = target;
72707c478bd9Sstevel@tonic-gate 	inlen = strlen(source);
72717c478bd9Sstevel@tonic-gate 	outlen = max_val * UTF_8_FACTOR;
72727c478bd9Sstevel@tonic-gate 	if (iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen) ==
72737c478bd9Sstevel@tonic-gate 	    (size_t)-1) {	/* Error occurred:  didn't convert */
72747c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
72757c478bd9Sstevel@tonic-gate 		    "tar:  file # %llu: (%s) UTF-8 conversion failed.\n"),
72767c478bd9Sstevel@tonic-gate 		    xhdr_count, source);
72777c478bd9Sstevel@tonic-gate 		/* Get remaining output; reinitialize conversion descriptor */
72787c478bd9Sstevel@tonic-gate 		iconv_src = (const char *)NULL;
72797c478bd9Sstevel@tonic-gate 		inlen = 0;
72807c478bd9Sstevel@tonic-gate 		(void) iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen);
72817c478bd9Sstevel@tonic-gate 		return (1);
72827c478bd9Sstevel@tonic-gate 	}
72837c478bd9Sstevel@tonic-gate 	/* Get remaining output; reinitialize conversion descriptor */
72847c478bd9Sstevel@tonic-gate 	iconv_src = (const char *)NULL;
72857c478bd9Sstevel@tonic-gate 	inlen = 0;
72867c478bd9Sstevel@tonic-gate 	if (iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen) ==
72877c478bd9Sstevel@tonic-gate 	    (size_t)-1) {	/* Error occurred:  didn't convert */
72887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
72897c478bd9Sstevel@tonic-gate 		    "tar:  file # %llu: (%s) UTF-8 conversion failed.\n"),
72907c478bd9Sstevel@tonic-gate 		    xhdr_count, source);
72917c478bd9Sstevel@tonic-gate 		return (1);
72927c478bd9Sstevel@tonic-gate 	}
72937c478bd9Sstevel@tonic-gate 
72947c478bd9Sstevel@tonic-gate 	*iconv_trg = '\0';	/* Null-terminate iconv output string */
72957c478bd9Sstevel@tonic-gate 	if (strlen(target) > max_val) {
72967c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
72977c478bd9Sstevel@tonic-gate 		    "tar: file # %llu: Extended header %s too long.\n"),
72987c478bd9Sstevel@tonic-gate 		    xhdr_count, option);
72997c478bd9Sstevel@tonic-gate 		return (1);
73007c478bd9Sstevel@tonic-gate 	}
73017c478bd9Sstevel@tonic-gate 	*Xhdr_ptrptr = target;
73027c478bd9Sstevel@tonic-gate 	return (0);
73037c478bd9Sstevel@tonic-gate }
73047c478bd9Sstevel@tonic-gate 
73057c478bd9Sstevel@tonic-gate /*
73067c478bd9Sstevel@tonic-gate  * Check gname, uname, path, and linkpath to see if they need to go in an
73077c478bd9Sstevel@tonic-gate  * extended header.  If they are already slated to be in an extended header,
73087c478bd9Sstevel@tonic-gate  * or if they are not ascii, then they need to be in the extended header.
73097c478bd9Sstevel@tonic-gate  * Then, convert all extended names to UTF-8.
73107c478bd9Sstevel@tonic-gate  */
73117c478bd9Sstevel@tonic-gate 
73127c478bd9Sstevel@tonic-gate int
gen_utf8_names(const char * filename)73137c478bd9Sstevel@tonic-gate gen_utf8_names(const char *filename)
73147c478bd9Sstevel@tonic-gate {
73157c478bd9Sstevel@tonic-gate 	static	iconv_t	iconv_cd;
73167c478bd9Sstevel@tonic-gate 	char		*nl_target;
73177c478bd9Sstevel@tonic-gate 	char		tempbuf[MAXNAM + 1];
7318eace40a5Sceastha 	int		nbytes;
7319eace40a5Sceastha 	int		errors;
73207c478bd9Sstevel@tonic-gate 
73217c478bd9Sstevel@tonic-gate 	if (charset_type == -1)	{	/* Previous failure to open. */
73227c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
73237c478bd9Sstevel@tonic-gate 		    "tar: file # %llu: UTF-8 conversion failed.\n"),
73247c478bd9Sstevel@tonic-gate 		    xhdr_count);
73257c478bd9Sstevel@tonic-gate 		return (1);
73267c478bd9Sstevel@tonic-gate 	}
73277c478bd9Sstevel@tonic-gate 
73287c478bd9Sstevel@tonic-gate 	if (charset_type == 0) {	/* Need to get conversion descriptor */
73297c478bd9Sstevel@tonic-gate 		nl_target = nl_langinfo(CODESET);
73307c478bd9Sstevel@tonic-gate 		if (strlen(nl_target) == 0)	/* locale using 7-bit codeset */
73317c478bd9Sstevel@tonic-gate 			nl_target = "646";
73327c478bd9Sstevel@tonic-gate 		if (strcmp(nl_target, "646") == 0)
73337c478bd9Sstevel@tonic-gate 			charset_type = 1;
73347c478bd9Sstevel@tonic-gate 		else if (strcmp(nl_target, "UTF-8") == 0)
73357c478bd9Sstevel@tonic-gate 			charset_type = 3;
73367c478bd9Sstevel@tonic-gate 		else {
73377c478bd9Sstevel@tonic-gate 			if (strncmp(nl_target, "ISO", 3) == 0)
73387c478bd9Sstevel@tonic-gate 				nl_target += 3;
73397c478bd9Sstevel@tonic-gate 			charset_type = 2;
73407c478bd9Sstevel@tonic-gate 			errno = 0;
73417c478bd9Sstevel@tonic-gate #ifdef ICONV_DEBUG
73427c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
73438e4a71aeSRich Burridge 			    gettext("Opening iconv_cd with target %s\n"),
73447c478bd9Sstevel@tonic-gate 			    nl_target);
73457c478bd9Sstevel@tonic-gate #endif
73467c478bd9Sstevel@tonic-gate 			if ((iconv_cd = iconv_open("UTF-8", nl_target)) ==
73477c478bd9Sstevel@tonic-gate 			    (iconv_t)-1) {
73487c478bd9Sstevel@tonic-gate 				if (errno == EINVAL)
73497c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
73507c478bd9Sstevel@tonic-gate 					    "tar: conversion routines not "
73517c478bd9Sstevel@tonic-gate 					    "available for current locale.  "));
73527c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
73537c478bd9Sstevel@tonic-gate 				    "file (%s): UTF-8 conversion failed.\n"),
73547c478bd9Sstevel@tonic-gate 				    filename);
73557c478bd9Sstevel@tonic-gate 				charset_type = -1;
73567c478bd9Sstevel@tonic-gate 				return (1);
73577c478bd9Sstevel@tonic-gate 			}
73587c478bd9Sstevel@tonic-gate 		}
73597c478bd9Sstevel@tonic-gate 	}
73607c478bd9Sstevel@tonic-gate 
73617c478bd9Sstevel@tonic-gate 	errors = 0;
73627c478bd9Sstevel@tonic-gate 
73637c478bd9Sstevel@tonic-gate 	errors += local_utf8(&Xtarhdr.x_gname, local_gname,
73647c478bd9Sstevel@tonic-gate 	    dblock.dbuf.gname, iconv_cd, _X_GNAME, _POSIX_NAME_MAX);
73657c478bd9Sstevel@tonic-gate 	errors += local_utf8(&Xtarhdr.x_uname, local_uname,
73667c478bd9Sstevel@tonic-gate 	    dblock.dbuf.uname, iconv_cd, _X_UNAME,  _POSIX_NAME_MAX);
73677c478bd9Sstevel@tonic-gate 	if ((xhdr_flgs & _X_LINKPATH) == 0) {	/* Need null-terminated str. */
73687c478bd9Sstevel@tonic-gate 		(void) strncpy(tempbuf, dblock.dbuf.linkname, NAMSIZ);
73697c478bd9Sstevel@tonic-gate 		tempbuf[NAMSIZ] = '\0';
73707c478bd9Sstevel@tonic-gate 	}
73717c478bd9Sstevel@tonic-gate 	errors += local_utf8(&Xtarhdr.x_linkpath, local_linkpath,
73727c478bd9Sstevel@tonic-gate 	    tempbuf, iconv_cd, _X_LINKPATH, PATH_MAX);
73737c478bd9Sstevel@tonic-gate 	if ((xhdr_flgs & _X_PATH) == 0) {	/* Concatenate prefix & name */
73747c478bd9Sstevel@tonic-gate 		(void) strncpy(tempbuf, dblock.dbuf.prefix, PRESIZ);
73750578ac30Ssr161167 		tempbuf[PRESIZ] = '\0';
73767c478bd9Sstevel@tonic-gate 		nbytes = strlen(tempbuf);
73777c478bd9Sstevel@tonic-gate 		if (nbytes > 0) {
73787c478bd9Sstevel@tonic-gate 			tempbuf[nbytes++] = '/';
73797c478bd9Sstevel@tonic-gate 			tempbuf[nbytes] = '\0';
73807c478bd9Sstevel@tonic-gate 		}
73810578ac30Ssr161167 		(void) strncat(tempbuf + nbytes, dblock.dbuf.name,
73820578ac30Ssr161167 		    (MAXNAM - nbytes));
73830578ac30Ssr161167 		tempbuf[MAXNAM] = '\0';
73847c478bd9Sstevel@tonic-gate 	}
73857c478bd9Sstevel@tonic-gate 	errors += local_utf8(&Xtarhdr.x_path, local_path,
73867c478bd9Sstevel@tonic-gate 	    tempbuf, iconv_cd, _X_PATH, PATH_MAX);
73877c478bd9Sstevel@tonic-gate 
73887c478bd9Sstevel@tonic-gate 	if (errors > 0)
73897c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
73907c478bd9Sstevel@tonic-gate 		    "tar: file (%s): UTF-8 conversion failed.\n"), filename);
73917c478bd9Sstevel@tonic-gate 
73927c478bd9Sstevel@tonic-gate 	if (errors && errflag)
73937c478bd9Sstevel@tonic-gate 		done(1);
73947c478bd9Sstevel@tonic-gate 	else
73957c478bd9Sstevel@tonic-gate 		if (errors)
73967c478bd9Sstevel@tonic-gate 			Errflg = 1;
73977c478bd9Sstevel@tonic-gate 	return (errors);
73987c478bd9Sstevel@tonic-gate }
73997c478bd9Sstevel@tonic-gate 
74007c478bd9Sstevel@tonic-gate static int
local_utf8(char ** Xhdr_ptrptr,char * target,const char * source,iconv_t iconv_cd,int xhdrflg,int max_val)74017c478bd9Sstevel@tonic-gate local_utf8(
74027c478bd9Sstevel@tonic-gate 		char	**Xhdr_ptrptr,
74037c478bd9Sstevel@tonic-gate 		char	*target,
74047c478bd9Sstevel@tonic-gate 		const	char	*source,
74057c478bd9Sstevel@tonic-gate 		iconv_t	iconv_cd,
74067c478bd9Sstevel@tonic-gate 		int	xhdrflg,
74077c478bd9Sstevel@tonic-gate 		int	max_val)
74087c478bd9Sstevel@tonic-gate {
74097c478bd9Sstevel@tonic-gate 	const	char	*iconv_src;
74107c478bd9Sstevel@tonic-gate 	const	char	*starting_src;
74117c478bd9Sstevel@tonic-gate 	char		*iconv_trg;
7412eace40a5Sceastha 	size_t		inlen;
7413eace40a5Sceastha 	size_t		outlen;
74147c478bd9Sstevel@tonic-gate #ifdef ICONV_DEBUG
74157c478bd9Sstevel@tonic-gate 	unsigned char	c_to_hex;
74167c478bd9Sstevel@tonic-gate #endif
74177c478bd9Sstevel@tonic-gate 
74187c478bd9Sstevel@tonic-gate 	/*
74197c478bd9Sstevel@tonic-gate 	 * If the item is already slated for extended format, get the string
74207c478bd9Sstevel@tonic-gate 	 * to convert from the extended header record.  Otherwise, get it from
74217c478bd9Sstevel@tonic-gate 	 * the regular (dblock) area.
74227c478bd9Sstevel@tonic-gate 	 */
74237c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & xhdrflg) {
74247c478bd9Sstevel@tonic-gate 		if (charset_type == 3) {	/* Already UTF-8, just copy */
74257c478bd9Sstevel@tonic-gate 			(void) strcpy(target, *Xhdr_ptrptr);
74267c478bd9Sstevel@tonic-gate 			*Xhdr_ptrptr = target;
74277c478bd9Sstevel@tonic-gate 			return (0);
74287c478bd9Sstevel@tonic-gate 		} else
74297c478bd9Sstevel@tonic-gate 			iconv_src = (const char *) *Xhdr_ptrptr;
74307c478bd9Sstevel@tonic-gate 	} else {
74317c478bd9Sstevel@tonic-gate 		if (charset_type == 3)		/* Already in UTF-8 format */
74327c478bd9Sstevel@tonic-gate 			return (0);		/* Don't create xhdr record */
74337c478bd9Sstevel@tonic-gate 		iconv_src = source;
74347c478bd9Sstevel@tonic-gate 	}
74357c478bd9Sstevel@tonic-gate 	starting_src = iconv_src;
74367c478bd9Sstevel@tonic-gate 	iconv_trg = target;
74377c478bd9Sstevel@tonic-gate 	if ((inlen = strlen(iconv_src)) == 0)
74387c478bd9Sstevel@tonic-gate 		return (0);
74397c478bd9Sstevel@tonic-gate 
74407c478bd9Sstevel@tonic-gate 	if (charset_type == 1) {	/* locale using 7-bit codeset */
74417c478bd9Sstevel@tonic-gate 		if (c_utf8(target, starting_src) != 0) {
74427c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
74437c478bd9Sstevel@tonic-gate 			    gettext("tar: invalid character in"
74447c478bd9Sstevel@tonic-gate 			    " UTF-8 conversion of '%s'\n"), starting_src);
74457c478bd9Sstevel@tonic-gate 			return (1);
74467c478bd9Sstevel@tonic-gate 		}
74477c478bd9Sstevel@tonic-gate 		return (0);
74487c478bd9Sstevel@tonic-gate 	}
74497c478bd9Sstevel@tonic-gate 
74507c478bd9Sstevel@tonic-gate 	outlen = max_val * UTF_8_FACTOR;
74517c478bd9Sstevel@tonic-gate 	errno = 0;
74527c478bd9Sstevel@tonic-gate 	if (iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen) ==
74537c478bd9Sstevel@tonic-gate 	    (size_t)-1) {
74547c478bd9Sstevel@tonic-gate 		/* An error occurred, or not all characters were converted */
74557c478bd9Sstevel@tonic-gate 		if (errno == EILSEQ)
74567c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
74577c478bd9Sstevel@tonic-gate 			    gettext("tar: invalid character in"
74587c478bd9Sstevel@tonic-gate 			    " UTF-8 conversion of '%s'\n"), starting_src);
74597c478bd9Sstevel@tonic-gate 		else
74607c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
74617c478bd9Sstevel@tonic-gate 			    "tar: conversion to UTF-8 aborted for '%s'.\n"),
74627c478bd9Sstevel@tonic-gate 			    starting_src);
74637c478bd9Sstevel@tonic-gate 		/* Get remaining output; reinitialize conversion descriptor */
74647c478bd9Sstevel@tonic-gate 		iconv_src = (const char *)NULL;
74657c478bd9Sstevel@tonic-gate 		inlen = 0;
74667c478bd9Sstevel@tonic-gate 		(void) iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen);
74677c478bd9Sstevel@tonic-gate 		return (1);
74687c478bd9Sstevel@tonic-gate 	}
74697c478bd9Sstevel@tonic-gate 	/* Get remaining output; reinitialize conversion descriptor */
74707c478bd9Sstevel@tonic-gate 	iconv_src = (const char *)NULL;
74717c478bd9Sstevel@tonic-gate 	inlen = 0;
74727c478bd9Sstevel@tonic-gate 	if (iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen) ==
74737c478bd9Sstevel@tonic-gate 	    (size_t)-1) {	/* Error occurred:  didn't convert */
74747c478bd9Sstevel@tonic-gate 		if (errno == EILSEQ)
74757c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
74767c478bd9Sstevel@tonic-gate 			    gettext("tar: invalid character in"
74777c478bd9Sstevel@tonic-gate 			    " UTF-8 conversion of '%s'\n"), starting_src);
74787c478bd9Sstevel@tonic-gate 		else
74797c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
74807c478bd9Sstevel@tonic-gate 			    "tar: conversion to UTF-8 aborted for '%s'.\n"),
74817c478bd9Sstevel@tonic-gate 			    starting_src);
74827c478bd9Sstevel@tonic-gate 		return (1);
74837c478bd9Sstevel@tonic-gate 	}
74847c478bd9Sstevel@tonic-gate 
74857c478bd9Sstevel@tonic-gate 	*iconv_trg = '\0';	/* Null-terminate iconv output string */
74867c478bd9Sstevel@tonic-gate 	if (strcmp(starting_src, target) != 0) {
74877c478bd9Sstevel@tonic-gate 		*Xhdr_ptrptr = target;
74887c478bd9Sstevel@tonic-gate 		xhdr_flgs |= xhdrflg;
74897c478bd9Sstevel@tonic-gate #ifdef ICONV_DEBUG
74907c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "***  inlen: %d %d; outlen: %d %d\n",
74917c478bd9Sstevel@tonic-gate 		    strlen(starting_src), inlen, max_val, outlen);
74927c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Input string:\n  ");
74937c478bd9Sstevel@tonic-gate 		for (inlen = 0; inlen < strlen(starting_src); inlen++) {
74947c478bd9Sstevel@tonic-gate 			c_to_hex = (unsigned char)starting_src[inlen];
74957c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, " %2.2x", c_to_hex);
74967c478bd9Sstevel@tonic-gate 			if (inlen % 20 == 19)
74977c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\n  ");
74987c478bd9Sstevel@tonic-gate 		}
74997c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\nOutput string:\n  ");
75007c478bd9Sstevel@tonic-gate 		for (inlen = 0; inlen < strlen(target); inlen++) {
75017c478bd9Sstevel@tonic-gate 			c_to_hex = (unsigned char)target[inlen];
75027c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, " %2.2x", c_to_hex);
75037c478bd9Sstevel@tonic-gate 			if (inlen % 20 == 19)
75047c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\n  ");
75057c478bd9Sstevel@tonic-gate 		}
75067c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
75077c478bd9Sstevel@tonic-gate #endif
75087c478bd9Sstevel@tonic-gate 	}
75097c478bd9Sstevel@tonic-gate 
75107c478bd9Sstevel@tonic-gate 	return (0);
75117c478bd9Sstevel@tonic-gate }
75127c478bd9Sstevel@tonic-gate 
75137c478bd9Sstevel@tonic-gate /*
75147c478bd9Sstevel@tonic-gate  *	Function to test each byte of the source string to make sure it is
75157c478bd9Sstevel@tonic-gate  *	in within bounds (value between 0 and 127).
75167c478bd9Sstevel@tonic-gate  *	If valid, copy source to target.
75177c478bd9Sstevel@tonic-gate  */
75187c478bd9Sstevel@tonic-gate 
75197c478bd9Sstevel@tonic-gate int
c_utf8(char * target,const char * source)75207c478bd9Sstevel@tonic-gate c_utf8(char *target, const char *source)
75217c478bd9Sstevel@tonic-gate {
75227c478bd9Sstevel@tonic-gate 	size_t		len;
75237c478bd9Sstevel@tonic-gate 	const char	*thischar;
75247c478bd9Sstevel@tonic-gate 
75257c478bd9Sstevel@tonic-gate 	len = strlen(source);
75267c478bd9Sstevel@tonic-gate 	thischar = source;
75277c478bd9Sstevel@tonic-gate 	while (len-- > 0) {
75287c478bd9Sstevel@tonic-gate 		if (!isascii((int)(*thischar++)))
75297c478bd9Sstevel@tonic-gate 			return (1);
75307c478bd9Sstevel@tonic-gate 	}
75317c478bd9Sstevel@tonic-gate 
75327c478bd9Sstevel@tonic-gate 	(void) strcpy(target, source);
75337c478bd9Sstevel@tonic-gate 	return (0);
75347c478bd9Sstevel@tonic-gate }
75357c478bd9Sstevel@tonic-gate 
75367c478bd9Sstevel@tonic-gate 
75377c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
75387c478bd9Sstevel@tonic-gate #define	ROUNDTOTBLOCK(a)	((a + (TBLOCK -1)) & ~(TBLOCK -1))
75397c478bd9Sstevel@tonic-gate 
75407c478bd9Sstevel@tonic-gate static void
prepare_xattr(char ** attrbuf,char * filename,char * attrpath,char typeflag,struct linkbuf * linkinfo,int * rlen)75417c478bd9Sstevel@tonic-gate prepare_xattr(
75427c478bd9Sstevel@tonic-gate 	char		**attrbuf,
75437c478bd9Sstevel@tonic-gate 	char		*filename,
7544da6c28aaSamw 	char		*attrpath,
75457c478bd9Sstevel@tonic-gate 	char		typeflag,
75467c478bd9Sstevel@tonic-gate 	struct linkbuf	*linkinfo,
75477c478bd9Sstevel@tonic-gate 	int		*rlen)
75487c478bd9Sstevel@tonic-gate {
75497c478bd9Sstevel@tonic-gate 	char			*bufhead;	/* ptr to full buffer */
7550da6c28aaSamw 	char			*aptr;
75517c478bd9Sstevel@tonic-gate 	struct xattr_hdr 	*hptr;		/* ptr to header in bufhead */
75527c478bd9Sstevel@tonic-gate 	struct xattr_buf	*tptr;		/* ptr to pathing pieces */
75537c478bd9Sstevel@tonic-gate 	int			totalen;	/* total buffer length */
75547c478bd9Sstevel@tonic-gate 	int			len;		/* length returned to user */
75557c478bd9Sstevel@tonic-gate 	int			stringlen;	/* length of filename + attr */
75567c478bd9Sstevel@tonic-gate 						/*
75577c478bd9Sstevel@tonic-gate 						 * length of filename + attr
75587c478bd9Sstevel@tonic-gate 						 * in link section
75597c478bd9Sstevel@tonic-gate 						 */
75607c478bd9Sstevel@tonic-gate 	int			linkstringlen;
75617c478bd9Sstevel@tonic-gate 	int			complen;	/* length of pathing section */
75627c478bd9Sstevel@tonic-gate 	int			linklen;	/* length of link section */
7563da6c28aaSamw 	int			attrnames_index; /* attrnames starting index */
75647c478bd9Sstevel@tonic-gate 
75657c478bd9Sstevel@tonic-gate 	/*
75667c478bd9Sstevel@tonic-gate 	 * Release previous buffer
75677c478bd9Sstevel@tonic-gate 	 */
75687c478bd9Sstevel@tonic-gate 
75697c478bd9Sstevel@tonic-gate 	if (*attrbuf != (char *)NULL) {
75707c478bd9Sstevel@tonic-gate 		free(*attrbuf);
75717c478bd9Sstevel@tonic-gate 		*attrbuf = NULL;
75727c478bd9Sstevel@tonic-gate 	}
75737c478bd9Sstevel@tonic-gate 
75747c478bd9Sstevel@tonic-gate 	/*
75757c478bd9Sstevel@tonic-gate 	 * First add in fixed size stuff
75767c478bd9Sstevel@tonic-gate 	 */
75777c478bd9Sstevel@tonic-gate 	len = sizeof (struct xattr_hdr) + sizeof (struct xattr_buf);
75787c478bd9Sstevel@tonic-gate 
75797c478bd9Sstevel@tonic-gate 	/*
75807c478bd9Sstevel@tonic-gate 	 * Add space for two nulls
75817c478bd9Sstevel@tonic-gate 	 */
7582da6c28aaSamw 	stringlen = strlen(attrpath) + strlen(filename) + 2;
75837c478bd9Sstevel@tonic-gate 	complen = stringlen + sizeof (struct xattr_buf);
75847c478bd9Sstevel@tonic-gate 
75857c478bd9Sstevel@tonic-gate 	len += stringlen;
75867c478bd9Sstevel@tonic-gate 
75877c478bd9Sstevel@tonic-gate 	/*
75887c478bd9Sstevel@tonic-gate 	 * Now add on space for link info if any
75897c478bd9Sstevel@tonic-gate 	 */
75907c478bd9Sstevel@tonic-gate 
75917c478bd9Sstevel@tonic-gate 	if (linkinfo != NULL) {
75927c478bd9Sstevel@tonic-gate 		/*
75937c478bd9Sstevel@tonic-gate 		 * Again add space for two nulls
75947c478bd9Sstevel@tonic-gate 		 */
75957c478bd9Sstevel@tonic-gate 		linkstringlen = strlen(linkinfo->pathname) +
75967c478bd9Sstevel@tonic-gate 		    strlen(linkinfo->attrname) + 2;
7597da6c28aaSamw 		linklen = linkstringlen + sizeof (struct xattr_buf);
7598da6c28aaSamw 		len += linklen;
7599da6c28aaSamw 	} else {
7600da6c28aaSamw 		linklen = 0;
76017c478bd9Sstevel@tonic-gate 	}
76027c478bd9Sstevel@tonic-gate 
76037c478bd9Sstevel@tonic-gate 	/*
76047c478bd9Sstevel@tonic-gate 	 * Now add padding to end to fill out TBLOCK
76057c478bd9Sstevel@tonic-gate 	 *
76067c478bd9Sstevel@tonic-gate 	 * Function returns size of real data and not size + padding.
76077c478bd9Sstevel@tonic-gate 	 */
76087c478bd9Sstevel@tonic-gate 
76097c478bd9Sstevel@tonic-gate 	totalen = ROUNDTOTBLOCK(len);
76107c478bd9Sstevel@tonic-gate 
76117c478bd9Sstevel@tonic-gate 	if ((bufhead = calloc(1, totalen)) == NULL) {
76127c478bd9Sstevel@tonic-gate 		fatal(gettext("Out of memory."));
76137c478bd9Sstevel@tonic-gate 	}
76147c478bd9Sstevel@tonic-gate 
76157c478bd9Sstevel@tonic-gate 
76167c478bd9Sstevel@tonic-gate 	/*
76177c478bd9Sstevel@tonic-gate 	 * Now we can fill in the necessary pieces
76187c478bd9Sstevel@tonic-gate 	 */
76197c478bd9Sstevel@tonic-gate 
76207c478bd9Sstevel@tonic-gate 	/*
76217c478bd9Sstevel@tonic-gate 	 * first fill in the fixed header
76227c478bd9Sstevel@tonic-gate 	 */
76237c478bd9Sstevel@tonic-gate 	hptr = (struct xattr_hdr *)bufhead;
76247c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_version, "%s", XATTR_ARCH_VERS);
76257c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_component_len, "%0*d",
76267c478bd9Sstevel@tonic-gate 	    sizeof (hptr->h_component_len) - 1, complen);
76277c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_link_component_len, "%0*d",
76287c478bd9Sstevel@tonic-gate 	    sizeof (hptr->h_link_component_len) - 1, linklen);
76297c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_size, "%0*d", sizeof (hptr->h_size) - 1, len);
76307c478bd9Sstevel@tonic-gate 
76317c478bd9Sstevel@tonic-gate 	/*
76327c478bd9Sstevel@tonic-gate 	 * Now fill in the filename + attrnames section
7633da6c28aaSamw 	 * The filename and attrnames section can be composed of two or more
7634da6c28aaSamw 	 * path segments separated by a null character.  The first segment
7635da6c28aaSamw 	 * is the path to the parent file that roots the entire sequence in
7636da6c28aaSamw 	 * the normal name space. The remaining segments describes a path
7637da6c28aaSamw 	 * rooted at the hidden extended attribute directory of the leaf file of
7638da6c28aaSamw 	 * the previous segment, making it possible to name attributes on
7639da6c28aaSamw 	 * attributes.  Thus, if we are just archiving an extended attribute,
7640da6c28aaSamw 	 * the second segment will contain the attribute name.  If we are
7641da6c28aaSamw 	 * archiving a system attribute of an extended attribute, then the
7642da6c28aaSamw 	 * second segment will contain the attribute name, and a third segment
7643da6c28aaSamw 	 * will contain the system attribute name.  The attribute pathing
7644da6c28aaSamw 	 * information is obtained from 'attrpath'.
76457c478bd9Sstevel@tonic-gate 	 */
76467c478bd9Sstevel@tonic-gate 
76477c478bd9Sstevel@tonic-gate 	tptr = (struct xattr_buf *)(bufhead + sizeof (struct xattr_hdr));
76487c478bd9Sstevel@tonic-gate 	(void) sprintf(tptr->h_namesz, "%0*d", sizeof (tptr->h_namesz) - 1,
76497c478bd9Sstevel@tonic-gate 	    stringlen);
76507c478bd9Sstevel@tonic-gate 	(void) strcpy(tptr->h_names, filename);
7651da6c28aaSamw 	attrnames_index = strlen(filename) + 1;
7652da6c28aaSamw 	(void) strcpy(&tptr->h_names[attrnames_index], attrpath);
76537c478bd9Sstevel@tonic-gate 	tptr->h_typeflag = typeflag;
76547c478bd9Sstevel@tonic-gate 
76557c478bd9Sstevel@tonic-gate 	/*
7656da6c28aaSamw 	 * Split the attrnames section into two segments if 'attrpath'
7657da6c28aaSamw 	 * contains pathing information for a system attribute of an
7658da6c28aaSamw 	 * extended attribute.  We split them by replacing the '/' with
7659da6c28aaSamw 	 * a '\0'.
7660da6c28aaSamw 	 */
7661da6c28aaSamw 	if ((aptr = strpbrk(&tptr->h_names[attrnames_index], "/")) != NULL) {
7662da6c28aaSamw 		*aptr = '\0';
7663da6c28aaSamw 	}
7664da6c28aaSamw 
7665da6c28aaSamw 	/*
76667c478bd9Sstevel@tonic-gate 	 * Now fill in the optional link section if we have one
76677c478bd9Sstevel@tonic-gate 	 */
76687c478bd9Sstevel@tonic-gate 
76697c478bd9Sstevel@tonic-gate 	if (linkinfo != (struct linkbuf *)NULL) {
76707c478bd9Sstevel@tonic-gate 		tptr = (struct xattr_buf *)(bufhead +
76717c478bd9Sstevel@tonic-gate 		    sizeof (struct xattr_hdr) + complen);
76727c478bd9Sstevel@tonic-gate 
76737c478bd9Sstevel@tonic-gate 		(void) sprintf(tptr->h_namesz, "%0*d",
76747c478bd9Sstevel@tonic-gate 		    sizeof (tptr->h_namesz) - 1, linkstringlen);
76757c478bd9Sstevel@tonic-gate 		(void) strcpy(tptr->h_names, linkinfo->pathname);
76767c478bd9Sstevel@tonic-gate 		(void) strcpy(
76777c478bd9Sstevel@tonic-gate 		    &tptr->h_names[strlen(linkinfo->pathname) + 1],
76787c478bd9Sstevel@tonic-gate 		    linkinfo->attrname);
76797c478bd9Sstevel@tonic-gate 		tptr->h_typeflag = typeflag;
76807c478bd9Sstevel@tonic-gate 	}
76817c478bd9Sstevel@tonic-gate 	*attrbuf = (char *)bufhead;
76827c478bd9Sstevel@tonic-gate 	*rlen = len;
76837c478bd9Sstevel@tonic-gate }
76847c478bd9Sstevel@tonic-gate 
76857c478bd9Sstevel@tonic-gate #else
76867c478bd9Sstevel@tonic-gate static void
prepare_xattr(char ** attrbuf,char * filename,char * attrname,char typeflag,struct linkbuf * linkinfo,int * rlen)76877c478bd9Sstevel@tonic-gate prepare_xattr(
76887c478bd9Sstevel@tonic-gate 	char		**attrbuf,
76897c478bd9Sstevel@tonic-gate 	char		*filename,
76907c478bd9Sstevel@tonic-gate 	char		*attrname,
76917c478bd9Sstevel@tonic-gate 	char		typeflag,
76927c478bd9Sstevel@tonic-gate 	struct linkbuf	*linkinfo,
76937c478bd9Sstevel@tonic-gate 	int		*rlen)
76947c478bd9Sstevel@tonic-gate {
76957c478bd9Sstevel@tonic-gate 	*attrbuf = NULL;
76967c478bd9Sstevel@tonic-gate 	*rlen = 0;
76977c478bd9Sstevel@tonic-gate }
76987c478bd9Sstevel@tonic-gate #endif
76997c478bd9Sstevel@tonic-gate 
77007c478bd9Sstevel@tonic-gate int
getstat(int dirfd,char * longname,char * shortname,char * attrparent)7701da6c28aaSamw getstat(int dirfd, char *longname, char *shortname, char *attrparent)
77027c478bd9Sstevel@tonic-gate {
77037c478bd9Sstevel@tonic-gate 
77047c478bd9Sstevel@tonic-gate 	int i, j;
77057c478bd9Sstevel@tonic-gate 	int	printerr;
77067c478bd9Sstevel@tonic-gate 	int	slnkerr;
77077c478bd9Sstevel@tonic-gate 	struct stat symlnbuf;
77087c478bd9Sstevel@tonic-gate 
77097c478bd9Sstevel@tonic-gate 	if (!hflag)
77107c478bd9Sstevel@tonic-gate 		i = fstatat(dirfd, shortname, &stbuf, AT_SYMLINK_NOFOLLOW);
77117c478bd9Sstevel@tonic-gate 	else
77127c478bd9Sstevel@tonic-gate 		i = fstatat(dirfd, shortname, &stbuf, 0);
77137c478bd9Sstevel@tonic-gate 
77147c478bd9Sstevel@tonic-gate 	if (i < 0) {
77157c478bd9Sstevel@tonic-gate 		/* Initialize flag to print error mesg. */
77167c478bd9Sstevel@tonic-gate 		printerr = 1;
77177c478bd9Sstevel@tonic-gate 		/*
77187c478bd9Sstevel@tonic-gate 		 * If stat is done, then need to do lstat
77197c478bd9Sstevel@tonic-gate 		 * to determine whether it's a sym link
77207c478bd9Sstevel@tonic-gate 		 */
77217c478bd9Sstevel@tonic-gate 		if (hflag) {
77227c478bd9Sstevel@tonic-gate 			/* Save returned error */
77237c478bd9Sstevel@tonic-gate 			slnkerr = errno;
77247c478bd9Sstevel@tonic-gate 
77257c478bd9Sstevel@tonic-gate 			j = fstatat(dirfd, shortname,
77267c478bd9Sstevel@tonic-gate 			    &symlnbuf, AT_SYMLINK_NOFOLLOW);
77277c478bd9Sstevel@tonic-gate 			/*
77287c478bd9Sstevel@tonic-gate 			 * Suppress error message when file is a symbolic link
77297c478bd9Sstevel@tonic-gate 			 * and function modifier 'l' is off.  Exception:  when
77307c478bd9Sstevel@tonic-gate 			 * a symlink points to a symlink points to a
77317c478bd9Sstevel@tonic-gate 			 * symlink ... and we get past MAXSYMLINKS.  That
77327c478bd9Sstevel@tonic-gate 			 * error will cause a file not to be archived, and
77337c478bd9Sstevel@tonic-gate 			 * needs to be printed.
77347c478bd9Sstevel@tonic-gate 			 */
77357c478bd9Sstevel@tonic-gate 			if ((j == 0) && (!linkerrok) && (slnkerr != ELOOP) &&
77367c478bd9Sstevel@tonic-gate 			    (S_ISLNK(symlnbuf.st_mode)))
77377c478bd9Sstevel@tonic-gate 				printerr = 0;
77387c478bd9Sstevel@tonic-gate 
77397c478bd9Sstevel@tonic-gate 			/*
77407c478bd9Sstevel@tonic-gate 			 * Restore errno in case the lstat
77417c478bd9Sstevel@tonic-gate 			 * on symbolic link change
77427c478bd9Sstevel@tonic-gate 			 */
77437c478bd9Sstevel@tonic-gate 			errno = slnkerr;
77447c478bd9Sstevel@tonic-gate 		}
77457c478bd9Sstevel@tonic-gate 
77467c478bd9Sstevel@tonic-gate 		if (printerr) {
77477c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
7748da6c28aaSamw 			    "tar: %s%s%s%s: %s\n"),
7749da6c28aaSamw 			    (attrparent == NULL) ? "" : gettext("attribute "),
7750da6c28aaSamw 			    (attrparent == NULL) ? "" : attrparent,
7751da6c28aaSamw 			    (attrparent == NULL) ? "" : gettext(" of "),
7752da6c28aaSamw 			    longname, strerror(errno));
77537c478bd9Sstevel@tonic-gate 			Errflg = 1;
77547c478bd9Sstevel@tonic-gate 		}
77557c478bd9Sstevel@tonic-gate 		return (1);
77567c478bd9Sstevel@tonic-gate 	}
77577c478bd9Sstevel@tonic-gate 	return (0);
77587c478bd9Sstevel@tonic-gate }
77597c478bd9Sstevel@tonic-gate 
7760da6c28aaSamw /*
7761da6c28aaSamw  * Recursively archive the extended attributes and/or extended system attributes
7762da6c28aaSamw  * of the base file, longname.  Note:  extended system attribute files will be
7763da6c28aaSamw  * archived only if the extended system attributes are not transient (i.e. the
7764da6c28aaSamw  * extended system attributes are other than the default values).
7765da6c28aaSamw  *
7766da6c28aaSamw  * If -@ was specified and the underlying file system supports it, archive the
7767da6c28aaSamw  * extended attributes, and if there is a system attribute associated with the
7768da6c28aaSamw  * extended attribute, then recursively call xattrs_put() to archive the
7769da6c28aaSamw  * hidden attribute directory and the extended system attribute.  If -/ was
7770da6c28aaSamw  * specified and the underlying file system supports it, archive the extended
7771da6c28aaSamw  * system attributes.  Read-only extended system attributes are never archived.
7772da6c28aaSamw  *
7773da6c28aaSamw  * Currently, there cannot be attributes on attributes; only system
7774da6c28aaSamw  * attributes on attributes.  In addition, there cannot be attributes on
7775da6c28aaSamw  * system attributes.  A file and it's attribute directory hierarchy looks as
7776da6c28aaSamw  * follows:
7777da6c28aaSamw  *	longname ---->	.	("." is the hidden attribute directory)
7778da6c28aaSamw  *			|
7779da6c28aaSamw  *	     ----------------------------
7780da6c28aaSamw  *	     |				|
7781da6c28aaSamw  *	<sys_attr_name>		   <attr_name> ---->	.
7782da6c28aaSamw  *							|
7783da6c28aaSamw  *						  <sys_attr_name>
7784da6c28aaSamw  *
7785da6c28aaSamw  */
77867c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
77877c478bd9Sstevel@tonic-gate static void
xattrs_put(char * longname,char * shortname,char * parent,char * attrparent)7788da6c28aaSamw xattrs_put(char *longname, char *shortname, char *parent, char *attrparent)
77897c478bd9Sstevel@tonic-gate {
7790da6c28aaSamw 	char *filename = (attrparent == NULL) ? shortname : attrparent;
7791da6c28aaSamw 	int arc_rwsysattr = 0;
77927c478bd9Sstevel@tonic-gate 	int dirfd;
7793da6c28aaSamw 	int fd = -1;
7794da6c28aaSamw 	int rw_sysattr = 0;
7795ced83f9bSceastha 	int ext_attr = 0;
7796da6c28aaSamw 	int rc;
77977c478bd9Sstevel@tonic-gate 	DIR *dirp;
77987c478bd9Sstevel@tonic-gate 	struct dirent *dp;
7799da6c28aaSamw 	attr_data_t *attrinfo = NULL;
78007c478bd9Sstevel@tonic-gate 
7801da6c28aaSamw 	/*
7802da6c28aaSamw 	 * If the underlying file system supports it, then archive the extended
7803da6c28aaSamw 	 * attributes if -@ was specified, and the extended system attributes
7804da6c28aaSamw 	 * if -/ was specified.
7805da6c28aaSamw 	 */
7806ced83f9bSceastha 	if (verify_attr_support(filename, (attrparent == NULL), ARC_CREATE,
7807ced83f9bSceastha 	    &ext_attr) != ATTR_OK) {
78087c478bd9Sstevel@tonic-gate 		return;
78097c478bd9Sstevel@tonic-gate 	}
78107c478bd9Sstevel@tonic-gate 
7811da6c28aaSamw 	/*
7812da6c28aaSamw 	 * Only want to archive a read-write extended system attribute file
7813da6c28aaSamw 	 * if it contains extended system attribute settings that are not the
7814da6c28aaSamw 	 * default values.
7815da6c28aaSamw 	 */
7816da6c28aaSamw #if defined(_PC_SATTR_ENABLED)
7817da6c28aaSamw 	if (saflag) {
7818da6c28aaSamw 		int	filefd;
7819da6c28aaSamw 		nvlist_t *slist = NULL;
7820da6c28aaSamw 
7821da6c28aaSamw 		/* Determine if there are non-transient system attributes */
7822da6c28aaSamw 		errno = 0;
7823da6c28aaSamw 		if ((filefd = open(filename, O_RDONLY)) == -1) {
7824da6c28aaSamw 			if (attrparent == NULL) {
7825da6c28aaSamw 				vperror(0, gettext(
7826da6c28aaSamw 				    "unable to open file %s"), longname);
7827da6c28aaSamw 			}
7828da6c28aaSamw 			return;
7829da6c28aaSamw 		}
7830da6c28aaSamw 		if (((slist = sysattr_list(basename(myname), filefd,
7831da6c28aaSamw 		    filename)) != NULL) || (errno != 0)) {
7832da6c28aaSamw 			arc_rwsysattr = 1;
7833da6c28aaSamw 		}
7834da6c28aaSamw 		if (slist != NULL) {
7835da6c28aaSamw 			(void) nvlist_free(slist);
7836da6c28aaSamw 			slist = NULL;
7837da6c28aaSamw 		}
7838da6c28aaSamw 		(void) close(filefd);
7839da6c28aaSamw 	}
7840ced83f9bSceastha 
7841ced83f9bSceastha 	/*
7842ced83f9bSceastha 	 * If we aren't archiving extended system attributes, and we are
7843ced83f9bSceastha 	 * processing an attribute, or if we are archiving extended system
7844ced83f9bSceastha 	 * attributes, and there are are no extended attributes, then there's
7845ced83f9bSceastha 	 * no need to open up the attribute directory of the file unless the
7846ced83f9bSceastha 	 * extended system attributes are not transient (i.e, the system
7847ced83f9bSceastha 	 * attributes are not the default values).
7848ced83f9bSceastha 	 */
7849ced83f9bSceastha 	if ((arc_rwsysattr == 0) && ((attrparent != NULL) ||
7850ced83f9bSceastha 	    (saflag && !ext_attr))) {
7851ced83f9bSceastha 		return;
7852ced83f9bSceastha 	}
7853da6c28aaSamw #endif	/* _PC_SATTR_ENABLED */
7854da6c28aaSamw 
7855da6c28aaSamw 	/* open the parent attribute directory */
7856da6c28aaSamw 	fd = attropen(filename, ".", O_RDONLY);
7857da6c28aaSamw 	if (fd < 0) {
7858da6c28aaSamw 		vperror(0, gettext(
7859da6c28aaSamw 		    "unable to open attribute directory for %s%s%sfile %s"),
7860da6c28aaSamw 		    (attrparent == NULL) ? "" : gettext("attribute "),
7861da6c28aaSamw 		    (attrparent == NULL) ? "" : attrparent,
7862da6c28aaSamw 		    (attrparent == NULL) ? "" : gettext(" of "),
78637c478bd9Sstevel@tonic-gate 		    longname);
78647c478bd9Sstevel@tonic-gate 		return;
78657c478bd9Sstevel@tonic-gate 	}
78667c478bd9Sstevel@tonic-gate 
7867da6c28aaSamw 	/*
7868da6c28aaSamw 	 * We need to change into the parent's attribute directory to determine
7869da6c28aaSamw 	 * if each of the attributes should be archived.
7870da6c28aaSamw 	 */
7871da6c28aaSamw 	if (fchdir(fd) < 0) {
7872da6c28aaSamw 		vperror(0, gettext(
7873da6c28aaSamw 		    "cannot change to attribute directory of %s%s%sfile %s"),
7874da6c28aaSamw 		    (attrparent == NULL) ? "" : gettext("attribute "),
7875da6c28aaSamw 		    (attrparent == NULL) ? "" : attrparent,
7876da6c28aaSamw 		    (attrparent == NULL) ? "" : gettext(" of "),
7877da6c28aaSamw 		    longname);
7878da6c28aaSamw 		(void) close(fd);
7879da6c28aaSamw 		return;
7880da6c28aaSamw 	}
7881da6c28aaSamw 
7882da6c28aaSamw 	if (((dirfd = dup(fd)) == -1) ||
7883da6c28aaSamw 	    ((dirp = fdopendir(dirfd)) == NULL)) {
78847c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
7885da6c28aaSamw 		    "tar: unable to open dir pointer for %s%s%sfile %s\n"),
7886da6c28aaSamw 		    (attrparent == NULL) ? "" : gettext("attribute "),
7887da6c28aaSamw 		    (attrparent == NULL) ? "" : attrparent,
7888da6c28aaSamw 		    (attrparent == NULL) ? "" : gettext(" of "),
7889da6c28aaSamw 		    longname);
7890da6c28aaSamw 		if (fd > 0) {
7891da6c28aaSamw 			(void) close(fd);
7892da6c28aaSamw 		}
78937c478bd9Sstevel@tonic-gate 		return;
78947c478bd9Sstevel@tonic-gate 	}
78957c478bd9Sstevel@tonic-gate 
7896*c536b1f9SRobert Mustacchi 	while ((dp = readdir(dirp)) != NULL) {
7897da6c28aaSamw 		if (strcmp(dp->d_name, "..") == 0) {
78987c478bd9Sstevel@tonic-gate 			continue;
7899da6c28aaSamw 		} else if (strcmp(dp->d_name, ".") == 0) {
79007c478bd9Sstevel@tonic-gate 			Hiddendir = 1;
7901da6c28aaSamw 		} else {
79027c478bd9Sstevel@tonic-gate 			Hiddendir = 0;
7903da6c28aaSamw 		}
79047c478bd9Sstevel@tonic-gate 
7905da6c28aaSamw 		/* Determine if this attribute should be archived */
7906da6c28aaSamw 		if (verify_attr(dp->d_name, attrparent, arc_rwsysattr,
7907da6c28aaSamw 		    &rw_sysattr) != ATTR_OK) {
7908da6c28aaSamw 			continue;
7909da6c28aaSamw 		}
7910da6c28aaSamw 
7911da6c28aaSamw 		/* gather the attribute's information to pass to putfile() */
7912da6c28aaSamw 		if ((fill_in_attr_info(dp->d_name, longname, attrparent,
7913da6c28aaSamw 		    fd, rw_sysattr, &attrinfo)) == 1) {
7914da6c28aaSamw 			continue;
7915da6c28aaSamw 		}
7916da6c28aaSamw 
7917da6c28aaSamw 		/* add the attribute to the archive */
7918da6c28aaSamw 		rc = putfile(longname, dp->d_name, parent, attrinfo,
79197c478bd9Sstevel@tonic-gate 		    XATTR_FILE, LEV0, SYMLINK_LEV0);
79207c478bd9Sstevel@tonic-gate 
7921da6c28aaSamw 		if (exitflag) {
79227c478bd9Sstevel@tonic-gate 			break;
79237c478bd9Sstevel@tonic-gate 		}
79247c478bd9Sstevel@tonic-gate 
7925da6c28aaSamw #if defined(_PC_SATTR_ENABLED)
7926da6c28aaSamw 		/*
7927da6c28aaSamw 		 * If both -/ and -@ were specified, then archive the
7928da6c28aaSamw 		 * attribute's extended system attributes and hidden directory
7929da6c28aaSamw 		 * by making a recursive call to xattrs_put().
7930da6c28aaSamw 		 */
7931da6c28aaSamw 		if (!rw_sysattr && saflag && atflag && (rc != PUT_AS_LINK) &&
7932da6c28aaSamw 		    (Hiddendir == 0)) {
7933da6c28aaSamw 
7934da6c28aaSamw 			xattrs_put(longname, shortname, parent, dp->d_name);
7935da6c28aaSamw 
7936da6c28aaSamw 			/*
7937da6c28aaSamw 			 * Change back to the parent's attribute directory
7938da6c28aaSamw 			 * to process any further attributes.
7939da6c28aaSamw 			 */
7940da6c28aaSamw 			if (fchdir(fd) < 0) {
7941da6c28aaSamw 				vperror(0, gettext(
7942da6c28aaSamw 				    "cannot change back to attribute directory "
7943da6c28aaSamw 				    "of file %s"), longname);
7944da6c28aaSamw 				break;
7945da6c28aaSamw 			}
7946da6c28aaSamw 		}
7947da6c28aaSamw #endif	/* _PC_SATTR_ENABLED */
7948da6c28aaSamw 	}
7949da6c28aaSamw 
7950da6c28aaSamw 	if (attrinfo != NULL) {
7951da6c28aaSamw 		if (attrinfo->attr_parent != NULL) {
7952da6c28aaSamw 			free(attrinfo->attr_parent);
7953da6c28aaSamw 		}
7954da6c28aaSamw 		free(attrinfo->attr_path);
7955da6c28aaSamw 		free(attrinfo);
7956da6c28aaSamw 	}
79577c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
7958da6c28aaSamw 	if (fd != -1) {
7959da6c28aaSamw 		(void) close(fd);
7960da6c28aaSamw 	}
7961da6c28aaSamw 
7962da6c28aaSamw 	/* Change back to the parent directory of the base file */
7963da6c28aaSamw 	if (attrparent == NULL) {
7964b25dbf38SRich Burridge 		(void) tar_chdir(parent);
7965da6c28aaSamw 	}
7966ced83f9bSceastha 	Hiddendir = 0;
79677c478bd9Sstevel@tonic-gate }
79687c478bd9Sstevel@tonic-gate #else
79697c478bd9Sstevel@tonic-gate static void
xattrs_put(char * longname,char * shortname,char * parent,char * attrppath)7970da6c28aaSamw xattrs_put(char *longname, char *shortname, char *parent, char *attrppath)
79717c478bd9Sstevel@tonic-gate {
79727c478bd9Sstevel@tonic-gate }
79737c478bd9Sstevel@tonic-gate #endif /* O_XATTR */
79747c478bd9Sstevel@tonic-gate 
79757c478bd9Sstevel@tonic-gate static int
put_link(char * name,char * longname,char * component,char * longattrname,char * prefix,int filetype,char type)7976da6c28aaSamw put_link(char *name, char *longname, char *component, char *longattrname,
79777c478bd9Sstevel@tonic-gate     char *prefix, int filetype, char type)
79787c478bd9Sstevel@tonic-gate {
79797c478bd9Sstevel@tonic-gate 
79807c478bd9Sstevel@tonic-gate 	if (stbuf.st_nlink > 1) {
79817c478bd9Sstevel@tonic-gate 		struct linkbuf *lp;
79827c478bd9Sstevel@tonic-gate 		int found = 0;
79837c478bd9Sstevel@tonic-gate 
79847c478bd9Sstevel@tonic-gate 		for (lp = ihead; lp != NULL; lp = lp->nextp)
79857c478bd9Sstevel@tonic-gate 			if (lp->inum == stbuf.st_ino &&
79867c478bd9Sstevel@tonic-gate 			    lp->devnum == stbuf.st_dev) {
79877c478bd9Sstevel@tonic-gate 				found++;
79887c478bd9Sstevel@tonic-gate 				break;
79897c478bd9Sstevel@tonic-gate 			}
79907c478bd9Sstevel@tonic-gate 		if (found) {
79917c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
79927c478bd9Sstevel@tonic-gate 			if (filetype == XATTR_FILE)
7993da6c28aaSamw 				if (put_xattr_hdr(longname, component,
7994da6c28aaSamw 				    longattrname, prefix, type, filetype, lp)) {
79957c478bd9Sstevel@tonic-gate 					goto out;
79967c478bd9Sstevel@tonic-gate 			}
79977c478bd9Sstevel@tonic-gate #endif
79987c478bd9Sstevel@tonic-gate 			stbuf.st_size = (off_t)0;
79997c478bd9Sstevel@tonic-gate 			if (filetype != XATTR_FILE) {
80007c478bd9Sstevel@tonic-gate 				tomodes(&stbuf);
80017c478bd9Sstevel@tonic-gate 				if (chk_path_build(name, longname, lp->pathname,
80027c478bd9Sstevel@tonic-gate 				    prefix, type, filetype) > 0) {
80037c478bd9Sstevel@tonic-gate 					goto out;
80047c478bd9Sstevel@tonic-gate 				}
80057c478bd9Sstevel@tonic-gate 			}
80067c478bd9Sstevel@tonic-gate 
80077c478bd9Sstevel@tonic-gate 			if (mulvol && tapepos + 1 >= blocklim)
80087c478bd9Sstevel@tonic-gate 				newvol();
80097c478bd9Sstevel@tonic-gate 			(void) writetbuf((char *)&dblock, 1);
80107c478bd9Sstevel@tonic-gate 			/*
80117c478bd9Sstevel@tonic-gate 			 * write_ancillary() is not needed here.
80127c478bd9Sstevel@tonic-gate 			 * The first link is handled in the following
80137c478bd9Sstevel@tonic-gate 			 * else statement. No need to process ACLs
80147c478bd9Sstevel@tonic-gate 			 * for other hard links since they are the
80157c478bd9Sstevel@tonic-gate 			 * same file.
80167c478bd9Sstevel@tonic-gate 			 */
80177c478bd9Sstevel@tonic-gate 
80187c478bd9Sstevel@tonic-gate 			if (vflag) {
80197c478bd9Sstevel@tonic-gate #ifdef DEBUG
80207c478bd9Sstevel@tonic-gate 				if (NotTape)
80217c478bd9Sstevel@tonic-gate 					DEBUG("seek = %" FMT_blkcnt_t
80227c478bd9Sstevel@tonic-gate 					    "K\t", K(tapepos), 0);
80237c478bd9Sstevel@tonic-gate #endif
80247c478bd9Sstevel@tonic-gate 				if (filetype == XATTR_FILE) {
80257c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext(
80267c478bd9Sstevel@tonic-gate 					    "a %s attribute %s link to "
8027da6c28aaSamw 					    "%s attribute %s\n"),
8028da6c28aaSamw 					    name, component, name,
8029da6c28aaSamw 					    lp->attrname);
80307c478bd9Sstevel@tonic-gate 				} else {
80317c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext(
80327c478bd9Sstevel@tonic-gate 					    "a %s link to %s\n"),
80337c478bd9Sstevel@tonic-gate 					    longname, lp->pathname);
80347c478bd9Sstevel@tonic-gate 				}
80357c478bd9Sstevel@tonic-gate 			}
80367c478bd9Sstevel@tonic-gate 			lp->count--;
80377c478bd9Sstevel@tonic-gate 			return (0);
80387c478bd9Sstevel@tonic-gate 		} else {
80397c478bd9Sstevel@tonic-gate 			lp = (struct linkbuf *)getmem(sizeof (*lp));
80407c478bd9Sstevel@tonic-gate 			if (lp != (struct linkbuf *)NULL) {
80417c478bd9Sstevel@tonic-gate 				lp->nextp = ihead;
80427c478bd9Sstevel@tonic-gate 				ihead = lp;
80437c478bd9Sstevel@tonic-gate 				lp->inum = stbuf.st_ino;
80447c478bd9Sstevel@tonic-gate 				lp->devnum = stbuf.st_dev;
80457c478bd9Sstevel@tonic-gate 				lp->count = stbuf.st_nlink - 1;
80467c478bd9Sstevel@tonic-gate 				if (filetype == XATTR_FILE) {
80477c478bd9Sstevel@tonic-gate 					(void) strcpy(lp->pathname, longname);
8048da6c28aaSamw 					(void) strcpy(lp->attrname,
8049da6c28aaSamw 					    component);
80507c478bd9Sstevel@tonic-gate 				} else {
80517c478bd9Sstevel@tonic-gate 					(void) strcpy(lp->pathname, longname);
80527c478bd9Sstevel@tonic-gate 					(void) strcpy(lp->attrname, "");
80537c478bd9Sstevel@tonic-gate 				}
80547c478bd9Sstevel@tonic-gate 			}
80557c478bd9Sstevel@tonic-gate 		}
80567c478bd9Sstevel@tonic-gate 	}
80577c478bd9Sstevel@tonic-gate 
80587c478bd9Sstevel@tonic-gate out:
80597c478bd9Sstevel@tonic-gate 	return (1);
80607c478bd9Sstevel@tonic-gate }
80617c478bd9Sstevel@tonic-gate 
80627c478bd9Sstevel@tonic-gate static int
put_extra_attributes(char * longname,char * shortname,char * longattrname,char * prefix,int filetype,char typeflag)8063da6c28aaSamw put_extra_attributes(char *longname, char *shortname, char *longattrname,
8064da6c28aaSamw     char *prefix, int filetype, char typeflag)
80657c478bd9Sstevel@tonic-gate {
8066fa9e4066Sahrens 	static acl_t *aclp = NULL;
8067fa9e4066Sahrens 	int error;
80687c478bd9Sstevel@tonic-gate 
8069fa9e4066Sahrens 	if (aclp != NULL) {
8070fa9e4066Sahrens 		acl_free(aclp);
80717c478bd9Sstevel@tonic-gate 		aclp = NULL;
80727c478bd9Sstevel@tonic-gate 	}
80737c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
8074da6c28aaSamw 	if ((atflag || saflag) && (filetype == XATTR_FILE)) {
8075da6c28aaSamw 		if (put_xattr_hdr(longname, shortname, longattrname, prefix,
80767c478bd9Sstevel@tonic-gate 		    typeflag, filetype, NULL)) {
80777c478bd9Sstevel@tonic-gate 			return (1);
80787c478bd9Sstevel@tonic-gate 		}
80797c478bd9Sstevel@tonic-gate 	}
80807c478bd9Sstevel@tonic-gate #endif
80817c478bd9Sstevel@tonic-gate 
80827c478bd9Sstevel@tonic-gate 	/* ACL support */
80837c478bd9Sstevel@tonic-gate 	if (pflag) {
80847c478bd9Sstevel@tonic-gate 		char	*secinfo = NULL;
80857c478bd9Sstevel@tonic-gate 		int	len = 0;
80867c478bd9Sstevel@tonic-gate 
80877c478bd9Sstevel@tonic-gate 		/* ACL support */
80887c478bd9Sstevel@tonic-gate 		if (((stbuf.st_mode & S_IFMT) != S_IFLNK)) {
80897c478bd9Sstevel@tonic-gate 			/*
80907c478bd9Sstevel@tonic-gate 			 * Get ACL info: dont bother allocating space if
8091fa9e4066Sahrens 			 * there is only a trivial ACL.
80927c478bd9Sstevel@tonic-gate 			 */
8093fa9e4066Sahrens 			if ((error = acl_get(shortname, ACL_NO_TRIVIAL,
8094fa9e4066Sahrens 			    &aclp)) != 0) {
80957c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
8096fa9e4066Sahrens 				    "%s: failed to retrieve acl : %s\n"),
8097fa9e4066Sahrens 				    longname, acl_strerror(error));
80987c478bd9Sstevel@tonic-gate 				return (1);
80997c478bd9Sstevel@tonic-gate 			}
81007c478bd9Sstevel@tonic-gate 		}
81017c478bd9Sstevel@tonic-gate 
81027c478bd9Sstevel@tonic-gate 		/* append security attributes if any */
8103fa9e4066Sahrens 		if (aclp != NULL) {
810445916cd2Sjpk 			(void) append_secattr(&secinfo, &len, acl_cnt(aclp),
8105b249c65cSmarks 			    acl_totext(aclp, ACL_APPEND_ID | ACL_COMPACT_FMT |
8106b249c65cSmarks 			    ACL_SID_FMT), (acl_type(aclp) == ACLENT_T) ?
8107b249c65cSmarks 			    UFSD_ACL : ACE_ACL);
810845916cd2Sjpk 		}
810945916cd2Sjpk 
811045916cd2Sjpk 		if (Tflag) {
811145916cd2Sjpk 			/* append Trusted Extensions extended attributes */
811245916cd2Sjpk 			append_ext_attr(shortname, &secinfo, &len);
811345916cd2Sjpk 			(void) write_ancillary(&dblock, secinfo, len, ACL_HDR);
811445916cd2Sjpk 
811545916cd2Sjpk 		} else if (aclp != NULL) {
81167c478bd9Sstevel@tonic-gate 			(void) write_ancillary(&dblock, secinfo, len, ACL_HDR);
81177c478bd9Sstevel@tonic-gate 		}
81187c478bd9Sstevel@tonic-gate 	}
81197c478bd9Sstevel@tonic-gate 	return (0);
81207c478bd9Sstevel@tonic-gate }
81217c478bd9Sstevel@tonic-gate 
81227c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
81237c478bd9Sstevel@tonic-gate static int
put_xattr_hdr(char * longname,char * shortname,char * longattrname,char * prefix,int typeflag,int filetype,struct linkbuf * lp)8124da6c28aaSamw put_xattr_hdr(char *longname, char *shortname, char *longattrname, char *prefix,
81257c478bd9Sstevel@tonic-gate 	int typeflag, int filetype, struct linkbuf *lp)
81267c478bd9Sstevel@tonic-gate {
81277c478bd9Sstevel@tonic-gate 	char *lname = NULL;
81287c478bd9Sstevel@tonic-gate 	char *sname = NULL;
81297c478bd9Sstevel@tonic-gate 	int  error = 0;
81307c478bd9Sstevel@tonic-gate 	static char *attrbuf = NULL;
81317c478bd9Sstevel@tonic-gate 	int attrlen;
81327c478bd9Sstevel@tonic-gate 
81337c478bd9Sstevel@tonic-gate 	lname = malloc(sizeof (char) * strlen("/dev/null") + 1 +
81347c478bd9Sstevel@tonic-gate 	    strlen(shortname) + strlen(".hdr") + 1);
81357c478bd9Sstevel@tonic-gate 
81367c478bd9Sstevel@tonic-gate 	if (lname == NULL) {
81377c478bd9Sstevel@tonic-gate 		fatal(gettext("Out of Memory."));
81387c478bd9Sstevel@tonic-gate 	}
81397c478bd9Sstevel@tonic-gate 	sname = malloc(sizeof (char) * strlen(shortname) +
8140da6c28aaSamw 	    strlen(".hdr") + 1);
81417c478bd9Sstevel@tonic-gate 	if (sname == NULL) {
81427c478bd9Sstevel@tonic-gate 		fatal(gettext("Out of Memory."));
81437c478bd9Sstevel@tonic-gate 	}
81447c478bd9Sstevel@tonic-gate 
81457c478bd9Sstevel@tonic-gate 	(void) sprintf(sname, "%s.hdr", shortname);
81467c478bd9Sstevel@tonic-gate 	(void) sprintf(lname, "/dev/null/%s", sname);
81477c478bd9Sstevel@tonic-gate 
81487c478bd9Sstevel@tonic-gate 	if (strlcpy(dblock.dbuf.name, lname, sizeof (dblock.dbuf.name)) >=
81497c478bd9Sstevel@tonic-gate 	    sizeof (dblock.dbuf.name)) {
81507c478bd9Sstevel@tonic-gate 		fatal(gettext(
81517c478bd9Sstevel@tonic-gate 		    "Buffer overflow writing extended attribute file name"));
81527c478bd9Sstevel@tonic-gate 	}
81537c478bd9Sstevel@tonic-gate 
81547c478bd9Sstevel@tonic-gate 	/*
81557c478bd9Sstevel@tonic-gate 	 * dump extended attr lookup info
81567c478bd9Sstevel@tonic-gate 	 */
8157da6c28aaSamw 	prepare_xattr(&attrbuf, longname, longattrname, typeflag, lp, &attrlen);
81587c478bd9Sstevel@tonic-gate 	write_ancillary(&dblock, attrbuf, attrlen, _XATTR_HDRTYPE);
81597c478bd9Sstevel@tonic-gate 
81607c478bd9Sstevel@tonic-gate 	(void) sprintf(lname, "/dev/null/%s", shortname);
81617c478bd9Sstevel@tonic-gate 	(void) strncpy(dblock.dbuf.name, sname, NAMSIZ);
81627c478bd9Sstevel@tonic-gate 
81637c478bd9Sstevel@tonic-gate 	/*
81647c478bd9Sstevel@tonic-gate 	 * Set up filename for attribute
81657c478bd9Sstevel@tonic-gate 	 */
81667c478bd9Sstevel@tonic-gate 
81677c478bd9Sstevel@tonic-gate 	error = build_dblock(lname, tchar, '0', filetype,
81687c478bd9Sstevel@tonic-gate 	    &stbuf, stbuf.st_dev, prefix);
81697c478bd9Sstevel@tonic-gate 	free(lname);
81707c478bd9Sstevel@tonic-gate 	free(sname);
81717c478bd9Sstevel@tonic-gate 
81727c478bd9Sstevel@tonic-gate 	return (error);
81737c478bd9Sstevel@tonic-gate }
81747c478bd9Sstevel@tonic-gate #endif
81757c478bd9Sstevel@tonic-gate 
81767c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
81777c478bd9Sstevel@tonic-gate static int
read_xattr_hdr(attr_data_t ** attrinfo)8178da6c28aaSamw read_xattr_hdr(attr_data_t **attrinfo)
81797c478bd9Sstevel@tonic-gate {
81807c478bd9Sstevel@tonic-gate 	char		buf[TBLOCK];
8181da6c28aaSamw 	char		*attrparent = NULL;
81827c478bd9Sstevel@tonic-gate 	blkcnt_t	blocks;
81837c478bd9Sstevel@tonic-gate 	char		*tp;
81847c478bd9Sstevel@tonic-gate 	off_t		bytes;
81857c478bd9Sstevel@tonic-gate 	int		comp_len, link_len;
81867c478bd9Sstevel@tonic-gate 	int		namelen;
8187da6c28aaSamw 	int		attrparentlen;
8188da6c28aaSamw 	int		parentfilelen;
81897c478bd9Sstevel@tonic-gate 
81907c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.typeflag != _XATTR_HDRTYPE)
81917c478bd9Sstevel@tonic-gate 		return (1);
81927c478bd9Sstevel@tonic-gate 
81937c478bd9Sstevel@tonic-gate 	bytes = stbuf.st_size;
81947c478bd9Sstevel@tonic-gate 	if ((xattrhead = calloc(1, (int)bytes)) == NULL) {
81957c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
81967c478bd9Sstevel@tonic-gate 		    "Insufficient memory for extended attribute\n"));
81977c478bd9Sstevel@tonic-gate 		return (1);
81987c478bd9Sstevel@tonic-gate 	}
81997c478bd9Sstevel@tonic-gate 
82007c478bd9Sstevel@tonic-gate 	tp = (char *)xattrhead;
82017c478bd9Sstevel@tonic-gate 	blocks = TBLOCKS(bytes);
82027c478bd9Sstevel@tonic-gate 	while (blocks-- > 0) {
82037c478bd9Sstevel@tonic-gate 		readtape(buf);
82047c478bd9Sstevel@tonic-gate 		if (bytes <= TBLOCK) {
82057c478bd9Sstevel@tonic-gate 			(void) memcpy(tp, buf, (size_t)bytes);
82067c478bd9Sstevel@tonic-gate 			break;
82077c478bd9Sstevel@tonic-gate 		} else {
82087c478bd9Sstevel@tonic-gate 			(void) memcpy(tp, buf, TBLOCK);
82097c478bd9Sstevel@tonic-gate 			tp += TBLOCK;
82107c478bd9Sstevel@tonic-gate 		}
82117c478bd9Sstevel@tonic-gate 		bytes -= TBLOCK;
82127c478bd9Sstevel@tonic-gate 	}
82137c478bd9Sstevel@tonic-gate 
82147c478bd9Sstevel@tonic-gate 	/*
82157c478bd9Sstevel@tonic-gate 	 * Validate that we can handle header format
82167c478bd9Sstevel@tonic-gate 	 */
82177c478bd9Sstevel@tonic-gate 	if (strcmp(xattrhead->h_version, XATTR_ARCH_VERS) != 0) {
82187c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
82197c478bd9Sstevel@tonic-gate 		    gettext("Unknown extended attribute format encountered\n"));
82207c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
82217c478bd9Sstevel@tonic-gate 		    gettext("Disabling extended attribute parsing\n"));
82227c478bd9Sstevel@tonic-gate 		xattrbadhead = 1;
82237c478bd9Sstevel@tonic-gate 		return (0);
82247c478bd9Sstevel@tonic-gate 	}
82257c478bd9Sstevel@tonic-gate 	(void) sscanf(xattrhead->h_component_len, "%10d", &comp_len);
82267c478bd9Sstevel@tonic-gate 	(void) sscanf(xattrhead->h_link_component_len,	"%10d", &link_len);
82277c478bd9Sstevel@tonic-gate 	xattrp = (struct xattr_buf *)(((char *)xattrhead) +
82287c478bd9Sstevel@tonic-gate 	    sizeof (struct xattr_hdr));
82297c478bd9Sstevel@tonic-gate 	(void) sscanf(xattrp->h_namesz, "%7d", &namelen);
82307c478bd9Sstevel@tonic-gate 	if (link_len > 0)
82317c478bd9Sstevel@tonic-gate 		xattr_linkp = (struct xattr_buf *)
82327c478bd9Sstevel@tonic-gate 		    ((int)xattrp + (int)comp_len);
82337c478bd9Sstevel@tonic-gate 	else
82347c478bd9Sstevel@tonic-gate 		xattr_linkp = NULL;
82357c478bd9Sstevel@tonic-gate 
8236da6c28aaSamw 	/*
8237da6c28aaSamw 	 * Gather the attribute path from the filename and attrnames section.
8238da6c28aaSamw 	 * The filename and attrnames section can be composed of two or more
8239da6c28aaSamw 	 * path segments separated by a null character.  The first segment
8240da6c28aaSamw 	 * is the path to the parent file that roots the entire sequence in
8241da6c28aaSamw 	 * the normal name space. The remaining segments describes a path
8242da6c28aaSamw 	 * rooted at the hidden extended attribute directory of the leaf file of
8243da6c28aaSamw 	 * the previous segment, making it possible to name attributes on
8244da6c28aaSamw 	 * attributes.
8245da6c28aaSamw 	 */
8246da6c28aaSamw 	parentfilelen = strlen(xattrp->h_names);
8247da6c28aaSamw 	xattrapath = xattrp->h_names + parentfilelen + 1;
8248da6c28aaSamw 	if ((strlen(xattrapath) + parentfilelen + 2) < namelen) {
8249da6c28aaSamw 		/*
8250da6c28aaSamw 		 * The attrnames section contains a system attribute on an
8251da6c28aaSamw 		 * attribute.  Save the name of the attribute for use later,
8252da6c28aaSamw 		 * and replace the null separating the attribute name from
8253da6c28aaSamw 		 * the system attribute name with a '/' so that xattrapath can
8254da6c28aaSamw 		 * be used to display messages with the full attribute path name
8255da6c28aaSamw 		 * rooted at the hidden attribute directory of the base file
8256da6c28aaSamw 		 * in normal name space.
8257da6c28aaSamw 		 */
8258da6c28aaSamw 		attrparent = strdup(xattrapath);
8259da6c28aaSamw 		attrparentlen = strlen(attrparent);
8260da6c28aaSamw 		xattrapath[attrparentlen] = '/';
8261da6c28aaSamw 	}
8262da6c28aaSamw 	if ((fill_in_attr_info((attrparent == NULL) ? xattrapath :
8263da6c28aaSamw 	    xattrapath + attrparentlen + 1, xattrapath, attrparent,
8264da6c28aaSamw 	    -1, 0, attrinfo)) == 1) {
8265da6c28aaSamw 		free(attrparent);
8266da6c28aaSamw 		return (1);
8267da6c28aaSamw 	}
8268da6c28aaSamw 
8269da6c28aaSamw 	/* Gather link info */
82707c478bd9Sstevel@tonic-gate 	if (xattr_linkp) {
82717c478bd9Sstevel@tonic-gate 		xattr_linkaname = xattr_linkp->h_names +
82727c478bd9Sstevel@tonic-gate 		    strlen(xattr_linkp->h_names) + 1;
82737c478bd9Sstevel@tonic-gate 	} else {
82747c478bd9Sstevel@tonic-gate 		xattr_linkaname = NULL;
82757c478bd9Sstevel@tonic-gate 	}
8276d2443e76Smarks 
82777c478bd9Sstevel@tonic-gate 	return (0);
82787c478bd9Sstevel@tonic-gate }
82797c478bd9Sstevel@tonic-gate #else
82807c478bd9Sstevel@tonic-gate static int
read_xattr_hdr(attr_data_t ** attrinfo)8281da6c28aaSamw read_xattr_hdr(attr_data_t **attrinfo)
82827c478bd9Sstevel@tonic-gate {
82837c478bd9Sstevel@tonic-gate 	return (0);
82847c478bd9Sstevel@tonic-gate }
82857c478bd9Sstevel@tonic-gate #endif
82867c478bd9Sstevel@tonic-gate 
82877c478bd9Sstevel@tonic-gate /*
82887c478bd9Sstevel@tonic-gate  * skip over extra slashes in string.
82897c478bd9Sstevel@tonic-gate  *
82907c478bd9Sstevel@tonic-gate  * For example:
82917c478bd9Sstevel@tonic-gate  * /usr/tmp/////
82927c478bd9Sstevel@tonic-gate  *
82937c478bd9Sstevel@tonic-gate  * would return pointer at
82947c478bd9Sstevel@tonic-gate  * /usr/tmp/////
82957c478bd9Sstevel@tonic-gate  *         ^
82967c478bd9Sstevel@tonic-gate  */
82977c478bd9Sstevel@tonic-gate static char *
skipslashes(char * string,char * start)82987c478bd9Sstevel@tonic-gate skipslashes(char *string, char *start)
82997c478bd9Sstevel@tonic-gate {
83007c478bd9Sstevel@tonic-gate 	while ((string > start) && *(string - 1) == '/') {
83017c478bd9Sstevel@tonic-gate 		string--;
83027c478bd9Sstevel@tonic-gate 	}
83037c478bd9Sstevel@tonic-gate 
83047c478bd9Sstevel@tonic-gate 	return (string);
83057c478bd9Sstevel@tonic-gate }
83067c478bd9Sstevel@tonic-gate 
83077c478bd9Sstevel@tonic-gate /*
83087c478bd9Sstevel@tonic-gate  * Return the parent directory of a given path.
83097c478bd9Sstevel@tonic-gate  *
83107c478bd9Sstevel@tonic-gate  * Examples:
83117c478bd9Sstevel@tonic-gate  * /usr/tmp return /usr
83127c478bd9Sstevel@tonic-gate  * /usr/tmp/file return /usr/tmp
83137c478bd9Sstevel@tonic-gate  * /  returns .
83147c478bd9Sstevel@tonic-gate  * /usr returns /
83157c478bd9Sstevel@tonic-gate  * file returns .
83167c478bd9Sstevel@tonic-gate  *
83177c478bd9Sstevel@tonic-gate  * dir is assumed to be at least as big as path.
83187c478bd9Sstevel@tonic-gate  */
83197c478bd9Sstevel@tonic-gate static void
get_parent(char * path,char * dir)83207c478bd9Sstevel@tonic-gate get_parent(char *path, char *dir)
83217c478bd9Sstevel@tonic-gate {
83227c478bd9Sstevel@tonic-gate 	char *s;
83237c478bd9Sstevel@tonic-gate 	char tmpdir[PATH_MAX + 1];
83247c478bd9Sstevel@tonic-gate 
83257c478bd9Sstevel@tonic-gate 	if (strlen(path) > PATH_MAX) {
83267c478bd9Sstevel@tonic-gate 		fatal(gettext("pathname is too long"));
83277c478bd9Sstevel@tonic-gate 	}
83287c478bd9Sstevel@tonic-gate 	(void) strcpy(tmpdir, path);
83297c478bd9Sstevel@tonic-gate 	chop_endslashes(tmpdir);
83307c478bd9Sstevel@tonic-gate 
83317c478bd9Sstevel@tonic-gate 	if ((s = strrchr(tmpdir, '/')) == NULL) {
83327c478bd9Sstevel@tonic-gate 		(void) strcpy(dir, ".");
83337c478bd9Sstevel@tonic-gate 	} else {
83347c478bd9Sstevel@tonic-gate 		s = skipslashes(s, tmpdir);
83357c478bd9Sstevel@tonic-gate 		*s = '\0';
83367c478bd9Sstevel@tonic-gate 		if (s == tmpdir)
83377c478bd9Sstevel@tonic-gate 			(void) strcpy(dir, "/");
83387c478bd9Sstevel@tonic-gate 		else
83397c478bd9Sstevel@tonic-gate 			(void) strcpy(dir, tmpdir);
83407c478bd9Sstevel@tonic-gate 	}
83417c478bd9Sstevel@tonic-gate }
83427c478bd9Sstevel@tonic-gate 
83437c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
83447c478bd9Sstevel@tonic-gate static char *
get_component(char * path)83457c478bd9Sstevel@tonic-gate get_component(char *path)
83467c478bd9Sstevel@tonic-gate {
83477c478bd9Sstevel@tonic-gate 	char *ptr;
83487c478bd9Sstevel@tonic-gate 
83497c478bd9Sstevel@tonic-gate 	ptr = strrchr(path, '/');
83507c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
83517c478bd9Sstevel@tonic-gate 		return (path);
83527c478bd9Sstevel@tonic-gate 	} else {
83537c478bd9Sstevel@tonic-gate 		/*
83547c478bd9Sstevel@tonic-gate 		 * Handle trailing slash
83557c478bd9Sstevel@tonic-gate 		 */
83567c478bd9Sstevel@tonic-gate 		if (*(ptr + 1) == '\0')
83577c478bd9Sstevel@tonic-gate 			return (ptr);
83587c478bd9Sstevel@tonic-gate 		else
83597c478bd9Sstevel@tonic-gate 			return (ptr + 1);
83607c478bd9Sstevel@tonic-gate 	}
83617c478bd9Sstevel@tonic-gate }
83627c478bd9Sstevel@tonic-gate #else
83637c478bd9Sstevel@tonic-gate static char *
get_component(char * path)83647c478bd9Sstevel@tonic-gate get_component(char *path)
83657c478bd9Sstevel@tonic-gate {
83667c478bd9Sstevel@tonic-gate 	return (path);
83677c478bd9Sstevel@tonic-gate }
83687c478bd9Sstevel@tonic-gate #endif
83697c478bd9Sstevel@tonic-gate 
8370da6c28aaSamw #if defined(O_XATTR)
83717c478bd9Sstevel@tonic-gate static int
retry_open_attr(int pdirfd,int cwd,char * dirp,char * pattr,char * name,int oflag,mode_t mode)8372da6c28aaSamw retry_open_attr(int pdirfd, int cwd, char *dirp, char *pattr, char *name,
8373da6c28aaSamw     int oflag, mode_t mode)
83747c478bd9Sstevel@tonic-gate {
8375da6c28aaSamw 	int dirfd;
8376da6c28aaSamw 	int ofilefd = -1;
83777c478bd9Sstevel@tonic-gate 	struct timeval times[2];
83787c478bd9Sstevel@tonic-gate 	mode_t newmode;
83797c478bd9Sstevel@tonic-gate 	struct stat parentstat;
8380d2443e76Smarks 	acl_t *aclp = NULL;
8381d2443e76Smarks 	int error;
83827c478bd9Sstevel@tonic-gate 
83837c478bd9Sstevel@tonic-gate 	/*
83847c478bd9Sstevel@tonic-gate 	 * We couldn't get to attrdir. See if its
83857c478bd9Sstevel@tonic-gate 	 * just a mode problem on the parent file.
83867c478bd9Sstevel@tonic-gate 	 * for example: a mode such as r-xr--r--
8387da6c28aaSamw 	 * on a ufs file system without extended
8388da6c28aaSamw 	 * system attribute support won't let us
8389da6c28aaSamw 	 * create an attribute dir if it doesn't
8390da6c28aaSamw 	 * already exist, and on a ufs file system
8391da6c28aaSamw 	 * with extended system attribute support
8392da6c28aaSamw 	 * won't let us open the attribute for
8393da6c28aaSamw 	 * write.
8394d2443e76Smarks 	 *
8395d2443e76Smarks 	 * If file has a non-trivial ACL, then save it
8396d2443e76Smarks 	 * off so that we can place it back on after doing
8397d2443e76Smarks 	 * chmod's.
83987c478bd9Sstevel@tonic-gate 	 */
8399da6c28aaSamw 	if ((dirfd = openat(cwd, (pattr == NULL) ? dirp : pattr,
8400da6c28aaSamw 	    O_RDONLY)) == -1) {
8401d2443e76Smarks 		return (-1);
84027c478bd9Sstevel@tonic-gate 	}
8403da6c28aaSamw 	if (fstat(dirfd, &parentstat) == -1) {
8404da6c28aaSamw 		(void) fprintf(stderr, gettext(
8405da6c28aaSamw 		    "tar: cannot stat %sfile %s: %s\n"),
8406da6c28aaSamw 		    (pdirfd == -1) ? "" : gettext("parent of "),
8407da6c28aaSamw 		    (pdirfd == -1) ? dirp : name, strerror(errno));
8408da6c28aaSamw 			return (-1);
8409da6c28aaSamw 	}
8410da6c28aaSamw 	if ((error = facl_get(dirfd, ACL_NO_TRIVIAL, &aclp)) != 0) {
8411da6c28aaSamw 		(void) fprintf(stderr, gettext(
8412da6c28aaSamw 		    "tar: failed to retrieve ACL on %sfile %s: %s\n"),
8413da6c28aaSamw 		    (pdirfd == -1) ? "" : gettext("parent of "),
8414da6c28aaSamw 		    (pdirfd == -1) ? dirp : name, strerror(errno));
8415d2443e76Smarks 			return (-1);
8416d2443e76Smarks 	}
8417d2443e76Smarks 
84187c478bd9Sstevel@tonic-gate 	newmode = S_IWUSR | parentstat.st_mode;
8419da6c28aaSamw 	if (fchmod(dirfd, newmode) == -1) {
84207c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
8421da6c28aaSamw 		    gettext(
8422da6c28aaSamw 		    "tar: cannot fchmod %sfile %s to %o: %s\n"),
8423da6c28aaSamw 		    (pdirfd == -1) ? "" : gettext("parent of "),
8424da6c28aaSamw 		    (pdirfd == -1) ? dirp : name, newmode, strerror(errno));
8425d2443e76Smarks 		if (aclp)
8426d2443e76Smarks 			acl_free(aclp);
8427d2443e76Smarks 		return (-1);
84287c478bd9Sstevel@tonic-gate 	}
84297c478bd9Sstevel@tonic-gate 
8430d2443e76Smarks 
8431da6c28aaSamw 	if (pdirfd == -1) {
8432d2443e76Smarks 		/*
8433da6c28aaSamw 		 * We weren't able to create the attribute directory before.
8434da6c28aaSamw 		 * Now try again.
8435d2443e76Smarks 		 */
8436da6c28aaSamw 		ofilefd = attropen(dirp, ".", oflag);
8437da6c28aaSamw 	} else {
8438da6c28aaSamw 		/*
8439da6c28aaSamw 		 * We weren't able to create open the attribute before.
8440da6c28aaSamw 		 * Now try again.
8441da6c28aaSamw 		 */
8442da6c28aaSamw 		ofilefd = openat(pdirfd, name, oflag, mode);
8443da6c28aaSamw 	}
84447c478bd9Sstevel@tonic-gate 
84457c478bd9Sstevel@tonic-gate 	/*
84467c478bd9Sstevel@tonic-gate 	 * Put mode back to original
84477c478bd9Sstevel@tonic-gate 	 */
8448da6c28aaSamw 	if (fchmod(dirfd, parentstat.st_mode) == -1) {
8449d2443e76Smarks 		(void) fprintf(stderr,
8450da6c28aaSamw 		    gettext("tar: cannot chmod %sfile %s to %o: %s\n"),
8451da6c28aaSamw 		    (pdirfd == -1) ? "" : gettext("parent of "),
8452da6c28aaSamw 		    (pdirfd == -1) ? dirp : name, newmode, strerror(errno));
8453d2443e76Smarks 	}
8454d2443e76Smarks 
8455d2443e76Smarks 	if (aclp) {
8456da6c28aaSamw 		error = facl_set(dirfd, aclp);
8457d2443e76Smarks 		if (error) {
8458d2443e76Smarks 			(void) fprintf(stderr,
8459da6c28aaSamw 			    gettext("tar: failed to set acl entries on "
8460da6c28aaSamw 			    "%sfile %s\n"),
8461da6c28aaSamw 			    (pdirfd == -1) ? "" : gettext("parent of "),
8462da6c28aaSamw 			    (pdirfd == -1) ? dirp : name);
8463d2443e76Smarks 		}
8464d2443e76Smarks 		acl_free(aclp);
8465d2443e76Smarks 	}
84667c478bd9Sstevel@tonic-gate 
84677c478bd9Sstevel@tonic-gate 	/*
84687c478bd9Sstevel@tonic-gate 	 * Put back time stamps
84697c478bd9Sstevel@tonic-gate 	 */
84707c478bd9Sstevel@tonic-gate 
84717c478bd9Sstevel@tonic-gate 	times[0].tv_sec = parentstat.st_atime;
84727c478bd9Sstevel@tonic-gate 	times[0].tv_usec = 0;
84737c478bd9Sstevel@tonic-gate 	times[1].tv_sec = parentstat.st_mtime;
84747c478bd9Sstevel@tonic-gate 	times[1].tv_usec = 0;
84757c478bd9Sstevel@tonic-gate 
8476da6c28aaSamw 	(void) futimesat(cwd, (pattr == NULL) ? dirp : pattr, times);
8477da6c28aaSamw 
8478da6c28aaSamw 	(void) close(dirfd);
8479da6c28aaSamw 
8480da6c28aaSamw 	return (ofilefd);
84817c478bd9Sstevel@tonic-gate }
8482da6c28aaSamw #endif
84837c478bd9Sstevel@tonic-gate 
84847c478bd9Sstevel@tonic-gate #if !defined(O_XATTR)
84857c478bd9Sstevel@tonic-gate static int
openat64(int fd,const char * name,int oflag,mode_t cmode)84867c478bd9Sstevel@tonic-gate openat64(int fd, const char *name, int oflag, mode_t cmode)
84877c478bd9Sstevel@tonic-gate {
84887c478bd9Sstevel@tonic-gate 	return (open64(name, oflag, cmode));
84897c478bd9Sstevel@tonic-gate }
84907c478bd9Sstevel@tonic-gate 
84917c478bd9Sstevel@tonic-gate static int
openat(int fd,const char * name,int oflag,mode_t cmode)84927c478bd9Sstevel@tonic-gate openat(int fd, const char *name, int oflag, mode_t cmode)
84937c478bd9Sstevel@tonic-gate {
84947c478bd9Sstevel@tonic-gate 	return (open(name, oflag, cmode));
84957c478bd9Sstevel@tonic-gate }
84967c478bd9Sstevel@tonic-gate 
84977c478bd9Sstevel@tonic-gate static int
fchownat(int fd,const char * name,uid_t owner,gid_t group,int flag)84987c478bd9Sstevel@tonic-gate fchownat(int fd, const char *name, uid_t owner, gid_t group, int flag)
84997c478bd9Sstevel@tonic-gate {
85007c478bd9Sstevel@tonic-gate 	if (flag == AT_SYMLINK_NOFOLLOW)
85017c478bd9Sstevel@tonic-gate 		return (lchown(name, owner, group));
85027c478bd9Sstevel@tonic-gate 	else
85037c478bd9Sstevel@tonic-gate 		return (chown(name, owner, group));
85047c478bd9Sstevel@tonic-gate }
85057c478bd9Sstevel@tonic-gate 
85067c478bd9Sstevel@tonic-gate static int
renameat(int fromfd,char * old,int tofd,char * new)85077c478bd9Sstevel@tonic-gate renameat(int fromfd, char *old, int tofd, char *new)
85087c478bd9Sstevel@tonic-gate {
85097c478bd9Sstevel@tonic-gate 	return (rename(old, new));
85107c478bd9Sstevel@tonic-gate }
85117c478bd9Sstevel@tonic-gate 
85127c478bd9Sstevel@tonic-gate static int
futimesat(int fd,char * path,struct timeval times[2])85137c478bd9Sstevel@tonic-gate futimesat(int fd, char *path, struct timeval times[2])
85147c478bd9Sstevel@tonic-gate {
85157c478bd9Sstevel@tonic-gate 	return (utimes(path, times));
85167c478bd9Sstevel@tonic-gate }
85177c478bd9Sstevel@tonic-gate 
85187c478bd9Sstevel@tonic-gate static int
unlinkat(int dirfd,char * path,int flag)85197c478bd9Sstevel@tonic-gate unlinkat(int dirfd, char *path, int flag)
85207c478bd9Sstevel@tonic-gate {
85217c478bd9Sstevel@tonic-gate 	if (flag == AT_REMOVEDIR)
85227c478bd9Sstevel@tonic-gate 		return (rmdir(path));
85237c478bd9Sstevel@tonic-gate 	else
85247c478bd9Sstevel@tonic-gate 		return (unlink(path));
85257c478bd9Sstevel@tonic-gate }
85267c478bd9Sstevel@tonic-gate 
85277c478bd9Sstevel@tonic-gate static int
fstatat(int fd,char * path,struct stat * buf,int flag)85287c478bd9Sstevel@tonic-gate fstatat(int fd, char *path, struct stat *buf, int flag)
85297c478bd9Sstevel@tonic-gate {
85307c478bd9Sstevel@tonic-gate 	if (flag == AT_SYMLINK_NOFOLLOW)
85317c478bd9Sstevel@tonic-gate 		return (lstat(path, buf));
85327c478bd9Sstevel@tonic-gate 	else
85337c478bd9Sstevel@tonic-gate 		return (stat(path, buf));
85347c478bd9Sstevel@tonic-gate }
85357c478bd9Sstevel@tonic-gate 
85367c478bd9Sstevel@tonic-gate static int
attropen(char * file,char * attr,int omode,mode_t cmode)85377c478bd9Sstevel@tonic-gate attropen(char *file, char *attr, int omode, mode_t cmode)
85387c478bd9Sstevel@tonic-gate {
85397c478bd9Sstevel@tonic-gate 	errno = ENOTSUP;
85407c478bd9Sstevel@tonic-gate 	return (-1);
85417c478bd9Sstevel@tonic-gate }
85427c478bd9Sstevel@tonic-gate #endif
85437c478bd9Sstevel@tonic-gate 
85447c478bd9Sstevel@tonic-gate static void
chop_endslashes(char * path)85457c478bd9Sstevel@tonic-gate chop_endslashes(char *path)
85467c478bd9Sstevel@tonic-gate {
85477c478bd9Sstevel@tonic-gate 	char *end, *ptr;
85487c478bd9Sstevel@tonic-gate 
85497c478bd9Sstevel@tonic-gate 	/*
85507c478bd9Sstevel@tonic-gate 	 * Chop of slashes, but not if all we have is slashes
85517c478bd9Sstevel@tonic-gate 	 * for example: ////
85527c478bd9Sstevel@tonic-gate 	 * should make no changes, otherwise it will screw up
85537c478bd9Sstevel@tonic-gate 	 * checkdir
85547c478bd9Sstevel@tonic-gate 	 */
85557c478bd9Sstevel@tonic-gate 	end = &path[strlen(path) -1];
85567c478bd9Sstevel@tonic-gate 	if (*end == '/' && end != path) {
85577c478bd9Sstevel@tonic-gate 		ptr = skipslashes(end, path);
85587c478bd9Sstevel@tonic-gate 		if (ptr != NULL && ptr != path) {
85597c478bd9Sstevel@tonic-gate 			*ptr = '\0';
85607c478bd9Sstevel@tonic-gate 		}
85617c478bd9Sstevel@tonic-gate 	}
85627c478bd9Sstevel@tonic-gate }
856345916cd2Sjpk /* Trusted Extensions */
856445916cd2Sjpk 
856545916cd2Sjpk /*
856645916cd2Sjpk  * append_ext_attr():
856745916cd2Sjpk  *
856845916cd2Sjpk  * Append extended attributes and other information into the buffer
856945916cd2Sjpk  * that gets written to the ancillary file.
857045916cd2Sjpk  *
857145916cd2Sjpk  * With option 'T', we create a tarfile which
857245916cd2Sjpk  * has an ancillary file each corresponding archived file.
857345916cd2Sjpk  * Each ancillary file contains 1 or more of the
857445916cd2Sjpk  * following attributes:
857545916cd2Sjpk  *
857645916cd2Sjpk  *	attribute type        attribute		process procedure
857745916cd2Sjpk  *	----------------      ----------------  --------------------------
857845916cd2Sjpk  *   	DIR_TYPE       = 'D'   directory flag	append if a directory
857945916cd2Sjpk  *    	LBL_TYPE       = 'L'   SL[IL] or SL	append ascii label
858045916cd2Sjpk  *
858145916cd2Sjpk  *
858245916cd2Sjpk  */
858345916cd2Sjpk static void
append_ext_attr(char * shortname,char ** secinfo,int * len)858445916cd2Sjpk append_ext_attr(char *shortname, char **secinfo, int *len)
858545916cd2Sjpk {
858645916cd2Sjpk 	bslabel_t	b_slabel;	/* binary sensitvity label */
858745916cd2Sjpk 	char		*ascii = NULL;	/* ascii label */
858845916cd2Sjpk 
858945916cd2Sjpk 	/*
859045916cd2Sjpk 	 * For each attribute type, append it if it is
859145916cd2Sjpk 	 * relevant to the file type.
859245916cd2Sjpk 	 */
859345916cd2Sjpk 
859445916cd2Sjpk 	/*
859545916cd2Sjpk 	 * For attribute type DIR_TYPE,
859645916cd2Sjpk 	 * append it to the following file type:
859745916cd2Sjpk 	 *
859845916cd2Sjpk 	 *	S_IFDIR: directories
859945916cd2Sjpk 	 */
860045916cd2Sjpk 
860145916cd2Sjpk 	/*
860245916cd2Sjpk 	 * For attribute type LBL_TYPE,
860345916cd2Sjpk 	 * append it to the following file type:
860445916cd2Sjpk 	 *
860545916cd2Sjpk 	 *	S_IFDIR: directories (including mld, sld)
860645916cd2Sjpk 	 *	S_IFLNK: symbolic link
860745916cd2Sjpk 	 *	S_IFREG: regular file but not hard link
860845916cd2Sjpk 	 *	S_IFIFO: FIFO file but not hard link
860945916cd2Sjpk 	 *	S_IFCHR: char special file but not hard link
861045916cd2Sjpk 	 *	S_IFBLK: block special file but not hard link
861145916cd2Sjpk 	 */
861245916cd2Sjpk 	switch (stbuf.st_mode & S_IFMT) {
861345916cd2Sjpk 
861445916cd2Sjpk 	case S_IFDIR:
861545916cd2Sjpk 
861645916cd2Sjpk 		/*
861745916cd2Sjpk 		 * append DIR_TYPE
861845916cd2Sjpk 		 */
861945916cd2Sjpk 		(void) append_secattr(secinfo, len, 1,
862045916cd2Sjpk 		    "\0", DIR_TYPE);
862145916cd2Sjpk 
862245916cd2Sjpk 		/*
862345916cd2Sjpk 		 * Get and append attribute types LBL_TYPE.
862445916cd2Sjpk 		 * For directories, LBL_TYPE contains SL.
862545916cd2Sjpk 		 */
862645916cd2Sjpk 		/* get binary sensitivity label */
862745916cd2Sjpk 		if (getlabel(shortname, &b_slabel) != 0) {
862845916cd2Sjpk 			(void) fprintf(stderr,
862945916cd2Sjpk 			    gettext("tar: can't get sensitvity label for "
863045916cd2Sjpk 			    " %s, getlabel() error: %s\n"),
863145916cd2Sjpk 			    shortname, strerror(errno));
863245916cd2Sjpk 		} else {
863345916cd2Sjpk 			/* get ascii SL */
863445916cd2Sjpk 			if (bsltos(&b_slabel, &ascii,
863545916cd2Sjpk 			    0, 0) <= 0) {
863645916cd2Sjpk 				(void) fprintf(stderr,
863745916cd2Sjpk 				    gettext("tar: can't get ascii SL for"
863845916cd2Sjpk 				    " %s\n"), shortname);
863945916cd2Sjpk 			} else {
864045916cd2Sjpk 				/* append LBL_TYPE */
864145916cd2Sjpk 				(void) append_secattr(secinfo, len,
864245916cd2Sjpk 				    strlen(ascii) + 1, ascii,
864345916cd2Sjpk 				    LBL_TYPE);
864445916cd2Sjpk 
864545916cd2Sjpk 				/* free storage */
864645916cd2Sjpk 				if (ascii != NULL) {
864745916cd2Sjpk 					free(ascii);
864845916cd2Sjpk 					ascii = (char *)0;
864945916cd2Sjpk 				}
865045916cd2Sjpk 			}
865145916cd2Sjpk 
865245916cd2Sjpk 		}
865345916cd2Sjpk 		break;
865445916cd2Sjpk 
865545916cd2Sjpk 	case S_IFLNK:
865645916cd2Sjpk 	case S_IFREG:
865745916cd2Sjpk 	case S_IFIFO:
865845916cd2Sjpk 	case S_IFCHR:
865945916cd2Sjpk 	case S_IFBLK:
866045916cd2Sjpk 
866145916cd2Sjpk 		/* get binary sensitivity label */
866245916cd2Sjpk 		if (getlabel(shortname, &b_slabel) != 0) {
866345916cd2Sjpk 			(void) fprintf(stderr,
866445916cd2Sjpk 			    gettext("tar: can't get sensitivty label for %s, "
866545916cd2Sjpk 			    "getlabel() error: %s\n"),
866645916cd2Sjpk 			    shortname, strerror(errno));
866745916cd2Sjpk 		} else {
866845916cd2Sjpk 			/* get ascii IL[SL] */
866945916cd2Sjpk 			if (bsltos(&b_slabel, &ascii, 0, 0) <= 0) {
867045916cd2Sjpk 				(void) fprintf(stderr,
867145916cd2Sjpk 				    gettext("tar: can't translate sensitivity "
867245916cd2Sjpk 				    " label for %s\n"), shortname);
867345916cd2Sjpk 			} else {
867445916cd2Sjpk 				char *cmw_label;
867545916cd2Sjpk 				size_t  cmw_length;
867645916cd2Sjpk 
867745916cd2Sjpk 				cmw_length = strlen("ADMIN_LOW [] ") +
867845916cd2Sjpk 				    strlen(ascii);
867945916cd2Sjpk 				if ((cmw_label = malloc(cmw_length)) == NULL) {
868045916cd2Sjpk 					(void) fprintf(stderr, gettext(
868145916cd2Sjpk 					    "Insufficient memory for label\n"));
868245916cd2Sjpk 					exit(1);
868345916cd2Sjpk 				}
868445916cd2Sjpk 				/* append LBL_TYPE */
868545916cd2Sjpk 				(void) snprintf(cmw_label, cmw_length,
868645916cd2Sjpk 				    "ADMIN_LOW [%s]", ascii);
868745916cd2Sjpk 				(void) append_secattr(secinfo, len,
868845916cd2Sjpk 				    strlen(cmw_label) + 1, cmw_label,
868945916cd2Sjpk 				    LBL_TYPE);
869045916cd2Sjpk 
869145916cd2Sjpk 				/* free storage */
869245916cd2Sjpk 				if (ascii != NULL) {
869345916cd2Sjpk 					free(cmw_label);
869445916cd2Sjpk 					free(ascii);
869545916cd2Sjpk 					ascii = (char *)0;
869645916cd2Sjpk 				}
869745916cd2Sjpk 			}
869845916cd2Sjpk 		}
869945916cd2Sjpk 		break;
870045916cd2Sjpk 
870145916cd2Sjpk 	default:
870245916cd2Sjpk 		break;
870345916cd2Sjpk 	} /* end switch for LBL_TYPE */
870445916cd2Sjpk 
870545916cd2Sjpk 
870645916cd2Sjpk 	/* DONE !! */
870745916cd2Sjpk 	return;
870845916cd2Sjpk 
870945916cd2Sjpk } /* end of append_ext_attr */
871045916cd2Sjpk 
871145916cd2Sjpk 
871245916cd2Sjpk /*
871345916cd2Sjpk  *	Name: extract_attr()
871445916cd2Sjpk  *
871545916cd2Sjpk  *	Description:
871645916cd2Sjpk  *		Process attributes from the ancillary file due to
871745916cd2Sjpk  *		the T option.
871845916cd2Sjpk  *
871945916cd2Sjpk  *	Call by doxtract() as part of the switch case structure.
872045916cd2Sjpk  *	Making this a separate routine because the nesting are too
872145916cd2Sjpk  *	deep in doxtract, thus, leaving very little space
872245916cd2Sjpk  *	on each line for instructions.
872345916cd2Sjpk  *
872445916cd2Sjpk  * With option 'T', we extract from a TS 8 or TS 2.5 ancillary file
872545916cd2Sjpk  *
872645916cd2Sjpk  * For option 'T', following are possible attributes in
872745916cd2Sjpk  * a TS 8 ancillary file: (NOTE: No IL support)
872845916cd2Sjpk  *
872945916cd2Sjpk  *	attribute type        attribute		process procedure
873045916cd2Sjpk  *	----------------      ----------------  -------------------------
873145916cd2Sjpk  *    #	LBL_TYPE       = 'L'   SL               construct binary label
873245916cd2Sjpk  *    #	APRIV_TYPE     = 'P'   allowed priv    	construct privileges
873345916cd2Sjpk  *    #	FPRIV_TYPE     = 'p'   forced priv	construct privileges
873445916cd2Sjpk  *    #	COMP_TYPE      = 'C'   path component	construct real path
873545916cd2Sjpk  *    #	DIR_TYPE       = 'D'   directory flag	note it is a directory
873645916cd2Sjpk  *    $	UFSD_ACL       = '1'   ACL data		construct ACL entries
873745916cd2Sjpk  *	ATTR_FLAG_TYPE = 'F'   file attr flags  construct binary flags
873845916cd2Sjpk  *	LK_COMP_TYPE   = 'K'   linked path comp construct linked real path
873945916cd2Sjpk  *
874045916cd2Sjpk  * note: # = attribute names common between TS 8 & TS 2.5 ancillary
874145916cd2Sjpk  *           files.
874245916cd2Sjpk  *       $ = ACL attribute is processed for the option 'p', it doesn't
874345916cd2Sjpk  *           need option 'T'.
874445916cd2Sjpk  *
874545916cd2Sjpk  * Trusted Extensions ignores APRIV_TYPE, FPRIV_TYPE, and ATTR_FLAG_TYPE
874645916cd2Sjpk  *
874745916cd2Sjpk  */
874845916cd2Sjpk static void
extract_attr(char ** file_ptr,struct sec_attr * attr)874945916cd2Sjpk extract_attr(char **file_ptr, struct sec_attr *attr)
875045916cd2Sjpk {
875145916cd2Sjpk 	int	reterr, err;
875245916cd2Sjpk 	char	*dummy_buf;	/* for attribute extract */
875345916cd2Sjpk 
875445916cd2Sjpk 	dummy_buf = attr->attr_info;
875545916cd2Sjpk 
875645916cd2Sjpk 	switch (attr->attr_type) {
875745916cd2Sjpk 
875845916cd2Sjpk 	case DIR_TYPE:
875945916cd2Sjpk 
876045916cd2Sjpk 		dir_flag++;
876145916cd2Sjpk 		break;
876245916cd2Sjpk 
876345916cd2Sjpk 	case LBL_TYPE:
876445916cd2Sjpk 
876545916cd2Sjpk 		/*
876645916cd2Sjpk 		 * LBL_TYPE is used to indicate SL for directory, and
876745916cd2Sjpk 		 * CMW label for other file types.
876845916cd2Sjpk 		 */
876945916cd2Sjpk 
877045916cd2Sjpk 		if (!dir_flag) { /* not directory */
877145916cd2Sjpk 			/* Skip over IL portion */
877245916cd2Sjpk 			char *sl_ptr = strchr(dummy_buf, '[');
877345916cd2Sjpk 
877445916cd2Sjpk 			if (sl_ptr == NULL)
877545916cd2Sjpk 				err = 0;
877645916cd2Sjpk 			else
877745916cd2Sjpk 				err = stobsl(sl_ptr, &bs_label,
877845916cd2Sjpk 				    NEW_LABEL, &reterr);
877945916cd2Sjpk 		} else { /* directory */
878045916cd2Sjpk 			err = stobsl(dummy_buf, &bs_label,
878145916cd2Sjpk 			    NEW_LABEL, &reterr);
878245916cd2Sjpk 		}
878345916cd2Sjpk 		if (err == 0) {
878445916cd2Sjpk 			(void) fprintf(stderr, gettext("tar: "
878545916cd2Sjpk 			    "can't convert %s to binary label\n"),
878645916cd2Sjpk 			    dummy_buf);
878745916cd2Sjpk 			bslundef(&bs_label);
878845916cd2Sjpk 		} else if (!blequal(&bs_label, &admin_low) &&
878945916cd2Sjpk 		    !blequal(&bs_label, &admin_high)) {
879045916cd2Sjpk 			bslabel_t *from_label;
879145916cd2Sjpk 			char *buf;
879245916cd2Sjpk 			char tempbuf[MAXPATHLEN];
879345916cd2Sjpk 
879445916cd2Sjpk 			if (*orig_namep != '/') {
879545916cd2Sjpk 				/* got relative linked to path */
879645916cd2Sjpk 				(void) getcwd(tempbuf, (sizeof (tempbuf)));
879745916cd2Sjpk 				(void) strncat(tempbuf, "/", MAXPATHLEN);
879845916cd2Sjpk 			} else
879945916cd2Sjpk 				*tempbuf = '\0';
880045916cd2Sjpk 
880145916cd2Sjpk 			buf = real_path;
880245916cd2Sjpk 			(void) strncat(tempbuf, orig_namep, MAXPATHLEN);
880345916cd2Sjpk 			from_label = getlabelbypath(tempbuf);
880445916cd2Sjpk 			if (from_label != NULL) {
880545916cd2Sjpk 				if (blequal(from_label, &admin_low)) {
880645916cd2Sjpk 					if ((getpathbylabel(tempbuf, buf,
880745916cd2Sjpk 					    MAXPATHLEN, &bs_label) == NULL)) {
880845916cd2Sjpk 						(void) fprintf(stderr,
880945916cd2Sjpk 						    gettext("tar: "
881045916cd2Sjpk 						"can't get zone root path for "
881145916cd2Sjpk 						"%s\n"), tempbuf);
881245916cd2Sjpk 					} else
881345916cd2Sjpk 						rpath_flag = 1;
881445916cd2Sjpk 				}
881545916cd2Sjpk 				free(from_label);
881645916cd2Sjpk 			}
881745916cd2Sjpk 		}
881845916cd2Sjpk 		break;
881945916cd2Sjpk 
882045916cd2Sjpk 	case COMP_TYPE:
882145916cd2Sjpk 
882245916cd2Sjpk 		rebuild_comp_path(dummy_buf, file_ptr);
882345916cd2Sjpk 		break;
882445916cd2Sjpk 
882545916cd2Sjpk 	case LK_COMP_TYPE:
882645916cd2Sjpk 
882745916cd2Sjpk 		if (rebuild_lk_comp_path(dummy_buf, file_ptr)
882845916cd2Sjpk 		    == 0) {
882945916cd2Sjpk 			lk_rpath_flag = 1;
883045916cd2Sjpk 		} else {
883145916cd2Sjpk 			(void) fprintf(stderr, gettext("tar: warning: link's "
883245916cd2Sjpk 			    "target pathname might be invalid.\n"));
883345916cd2Sjpk 			lk_rpath_flag = 0;
883445916cd2Sjpk 		}
883545916cd2Sjpk 		break;
883645916cd2Sjpk 	case APRIV_TYPE:
883745916cd2Sjpk 		ignored_aprivs++;
883845916cd2Sjpk 		break;
883945916cd2Sjpk 	case FPRIV_TYPE:
884045916cd2Sjpk 		ignored_fprivs++;
884145916cd2Sjpk 		break;
884245916cd2Sjpk 	case ATTR_FLAG_TYPE:
884345916cd2Sjpk 		ignored_fattrs++;
884445916cd2Sjpk 		break;
884545916cd2Sjpk 
884645916cd2Sjpk 	default:
884745916cd2Sjpk 
884845916cd2Sjpk 		break;
884945916cd2Sjpk 	}
885045916cd2Sjpk 
885145916cd2Sjpk 	/* done */
885245916cd2Sjpk 	return;
885345916cd2Sjpk 
885445916cd2Sjpk }	/* end extract_attr */
885545916cd2Sjpk 
885645916cd2Sjpk 
885745916cd2Sjpk 
885845916cd2Sjpk /*
885945916cd2Sjpk  *	Name:	rebuild_comp_path()
886045916cd2Sjpk  *
886145916cd2Sjpk  *	Description:
886245916cd2Sjpk  *		Take the string of components passed down by the calling
886345916cd2Sjpk  *		routine and parse the values and rebuild the path.
886445916cd2Sjpk  *		This routine no longer needs to produce a new real_path
886545916cd2Sjpk  *		string because it is produced when the 'L' LABEL_TYPE is
886645916cd2Sjpk  *		interpreted. So the only thing done here is to distinguish
886745916cd2Sjpk  *		between an SLD and an MLD entry. We only want one, so we
886845916cd2Sjpk  *		ignore the MLD entry by setting the mld_flag.
886945916cd2Sjpk  *
887045916cd2Sjpk  *	return value:
887145916cd2Sjpk  *		none
887245916cd2Sjpk  */
887345916cd2Sjpk static void
rebuild_comp_path(char * str,char ** namep)887445916cd2Sjpk rebuild_comp_path(char *str, char **namep)
887545916cd2Sjpk {
887645916cd2Sjpk 	char		*cp;
887745916cd2Sjpk 
887845916cd2Sjpk 	while (*str != '\0') {
887945916cd2Sjpk 
888045916cd2Sjpk 		switch (*str) {
888145916cd2Sjpk 
888245916cd2Sjpk 		case MLD_TYPE:
888345916cd2Sjpk 
888445916cd2Sjpk 			str++;
888545916cd2Sjpk 			if ((cp = strstr(str, ";;")) != NULL) {
888645916cd2Sjpk 				*cp = '\0';
888745916cd2Sjpk 				str = cp + 2;
888845916cd2Sjpk 				*cp = ';';
888945916cd2Sjpk 			}
889045916cd2Sjpk 			mld_flag = 1;
889145916cd2Sjpk 			break;
889245916cd2Sjpk 
889345916cd2Sjpk 		case SLD_TYPE:
889445916cd2Sjpk 
889545916cd2Sjpk 			str++;
889645916cd2Sjpk 			if ((cp = strstr(str, ";;")) != NULL) {
889745916cd2Sjpk 				*cp = '\0';
889845916cd2Sjpk 				str = cp + 2;
889945916cd2Sjpk 				*cp = ';';
890045916cd2Sjpk 			}
890145916cd2Sjpk 			mld_flag = 0;
890245916cd2Sjpk 			break;
890345916cd2Sjpk 
890445916cd2Sjpk 		case PATH_TYPE:
890545916cd2Sjpk 
890645916cd2Sjpk 			str++;
890745916cd2Sjpk 			if ((cp = strstr(str, ";;")) != NULL) {
890845916cd2Sjpk 				*cp = '\0';
890945916cd2Sjpk 				str = cp + 2;
891045916cd2Sjpk 				*cp = ';';
891145916cd2Sjpk 			}
891245916cd2Sjpk 			break;
891345916cd2Sjpk 		}
891445916cd2Sjpk 	}
891545916cd2Sjpk 	if (rpath_flag)
891645916cd2Sjpk 		*namep = real_path;
891745916cd2Sjpk 	return;
891845916cd2Sjpk 
891945916cd2Sjpk } /* end rebuild_comp_path() */
892045916cd2Sjpk 
892145916cd2Sjpk /*
892245916cd2Sjpk  *	Name:	rebuild_lk_comp_path()
892345916cd2Sjpk  *
892445916cd2Sjpk  *	Description:
892545916cd2Sjpk  *		Take the string of components passed down by the calling
892645916cd2Sjpk  *		routine and parse the values and rebuild the path.
892745916cd2Sjpk  *
892845916cd2Sjpk  *	return value:
892945916cd2Sjpk  *		0 = succeeded
893045916cd2Sjpk  *		-1 = failed
893145916cd2Sjpk  */
893245916cd2Sjpk static int
rebuild_lk_comp_path(char * str,char ** namep)893345916cd2Sjpk rebuild_lk_comp_path(char *str, char **namep)
893445916cd2Sjpk {
893545916cd2Sjpk 	char		*cp;
893645916cd2Sjpk 	int		reterr;
893745916cd2Sjpk 	bslabel_t	bslabel;
893845916cd2Sjpk 	char		*buf;
893945916cd2Sjpk 	char		pbuf[MAXPATHLEN];
894045916cd2Sjpk 	char		*ptr1, *ptr2;
894145916cd2Sjpk 	int		plen;
894245916cd2Sjpk 	int		use_pbuf;
894345916cd2Sjpk 	char		tempbuf[MAXPATHLEN];
894445916cd2Sjpk 	int		mismatch;
894545916cd2Sjpk 	bslabel_t	*from_label;
894645916cd2Sjpk 	char		zonename[ZONENAME_MAX];
894745916cd2Sjpk 	zoneid_t	zoneid;
894845916cd2Sjpk 
894945916cd2Sjpk 	/* init stuff */
895045916cd2Sjpk 	use_pbuf = 0;
895145916cd2Sjpk 	mismatch = 0;
895245916cd2Sjpk 
895345916cd2Sjpk 	/*
895445916cd2Sjpk 	 * For linked to pathname (LK_COMP_TYPE):
895545916cd2Sjpk 	 *  - If the linked to pathname is absolute (start with /), we
895645916cd2Sjpk 	 *    will use it as is.
895745916cd2Sjpk 	 *  - If it is a relative pathname then it is relative to 1 of 2
895845916cd2Sjpk 	 *    directories.  For a hardlink, it is relative to the current
895945916cd2Sjpk 	 *    directory.  For a symbolic link, it is relative to the
896045916cd2Sjpk 	 *    directory the symbolic link is in.  For the symbolic link
896145916cd2Sjpk 	 *    case, set a flag to indicate we need to use the prefix of
896245916cd2Sjpk 	 *    the restored file's pathname with the linked to pathname.
896345916cd2Sjpk 	 *
896445916cd2Sjpk 	 *    NOTE: At this point, we have no way to determine if we have
896545916cd2Sjpk 	 *    a hardlink or a symbolic link.  We will compare the 1st
896645916cd2Sjpk 	 *    component in the prefix portion of the restore file's
896745916cd2Sjpk 	 *    pathname to the 1st component in the attribute data
896845916cd2Sjpk 	 *    (the linked pathname).  If they are the same, we will assume
896945916cd2Sjpk 	 *    the link pathname to reconstruct is relative to the current
897045916cd2Sjpk 	 *    directory.  Otherwise, we will set a flag indicate we need
897145916cd2Sjpk 	 *    to use a prefix with the reconstructed name.  Need to compare
897245916cd2Sjpk 	 *    both the adorned and unadorned version before deciding a
897345916cd2Sjpk 	 *    mismatch.
897445916cd2Sjpk 	 */
897545916cd2Sjpk 
897645916cd2Sjpk 	buf = lk_real_path;
897745916cd2Sjpk 	if (*(str + 1) != '/') { /* got relative linked to path */
897845916cd2Sjpk 		ptr1 = orig_namep;
897945916cd2Sjpk 		ptr2 = strrchr(ptr1, '/');
898045916cd2Sjpk 		plen = ptr2 - ptr1;
898145916cd2Sjpk 		if (plen > 0) {
898245916cd2Sjpk 			pbuf[0] = '\0';
898345916cd2Sjpk 			plen++;		/* include '/' */
898445916cd2Sjpk 			(void) strncpy(pbuf, ptr1, plen);
898545916cd2Sjpk 			*(pbuf + plen) = '\0';
898645916cd2Sjpk 			ptr2 = strchr(pbuf, '/');
898745916cd2Sjpk 			if (strncmp(pbuf, str + 1, ptr2 - pbuf) != 0)
898845916cd2Sjpk 				mismatch = 1;
898945916cd2Sjpk 		}
899045916cd2Sjpk 
899145916cd2Sjpk 		if (mismatch == 1)
899245916cd2Sjpk 			use_pbuf = 1;
899345916cd2Sjpk 	}
899445916cd2Sjpk 
899545916cd2Sjpk 	buf[0] = '\0';
899645916cd2Sjpk 
899745916cd2Sjpk 	while (*str != '\0') {
899845916cd2Sjpk 
899945916cd2Sjpk 		switch (*str) {
900045916cd2Sjpk 
900145916cd2Sjpk 		case MLD_TYPE:
900245916cd2Sjpk 
900345916cd2Sjpk 			str++;
900445916cd2Sjpk 			if ((cp = strstr(str, ";;")) != NULL) {
900545916cd2Sjpk 				*cp = '\0';
900645916cd2Sjpk 
900745916cd2Sjpk 				/*
900845916cd2Sjpk 				 * Ignore attempts to backup over .MLD.
900945916cd2Sjpk 				 */
901045916cd2Sjpk 				if (strcmp(str, "../") != 0)
901145916cd2Sjpk 					(void) strncat(buf, str, MAXPATHLEN);
901245916cd2Sjpk 				str = cp + 2;
901345916cd2Sjpk 				*cp = ';';
901445916cd2Sjpk 			}
901545916cd2Sjpk 			break;
901645916cd2Sjpk 
901745916cd2Sjpk 		case SLD_TYPE:
901845916cd2Sjpk 
901945916cd2Sjpk 			str++;
902045916cd2Sjpk 			if ((cp = strstr(str, ";;")) != NULL) {
902145916cd2Sjpk 				*cp = '\0';
902245916cd2Sjpk 
902345916cd2Sjpk 				/*
902445916cd2Sjpk 				 * Use the path name in the header if
902545916cd2Sjpk 				 * error occurs when processing the
902645916cd2Sjpk 				 * SLD type.
902745916cd2Sjpk 				 */
902845916cd2Sjpk 
902945916cd2Sjpk 				if (!stobsl(str, &bslabel,
903045916cd2Sjpk 				    NO_CORRECTION, &reterr)) {
903145916cd2Sjpk 					(void) fprintf(stderr, gettext(
903245916cd2Sjpk 					    "tar: can't translate to binary"
903345916cd2Sjpk 					    "SL for SLD, stobsl() error:"
903445916cd2Sjpk 					    " %s\n"), strerror(errno));
903545916cd2Sjpk 					return (-1);
903645916cd2Sjpk 				}
903745916cd2Sjpk 
903845916cd2Sjpk 				str = cp + 2;
903945916cd2Sjpk 				*cp = ';';
904045916cd2Sjpk 
904145916cd2Sjpk 				if (use_pbuf == 1) {
904245916cd2Sjpk 					if (*pbuf != '/') {
904345916cd2Sjpk 						/* relative linked to path */
904445916cd2Sjpk 
904545916cd2Sjpk 						(void) getcwd(tempbuf,
904645916cd2Sjpk 						    (sizeof (tempbuf)));
904745916cd2Sjpk 						(void) strncat(tempbuf, "/",
904845916cd2Sjpk 						    MAXPATHLEN);
904945916cd2Sjpk 						(void) strncat(tempbuf, pbuf,
905045916cd2Sjpk 						    MAXPATHLEN);
905145916cd2Sjpk 					}
905245916cd2Sjpk 					else
905345916cd2Sjpk 						(void) strcpy(tempbuf, pbuf);
905445916cd2Sjpk 
905545916cd2Sjpk 				} else if (*buf != '/') {
905645916cd2Sjpk 					/* got relative linked to path */
905745916cd2Sjpk 
905845916cd2Sjpk 					(void) getcwd(tempbuf,
905945916cd2Sjpk 					    (sizeof (tempbuf)));
906045916cd2Sjpk 					(void) strncat(tempbuf, "/",
906145916cd2Sjpk 					    MAXPATHLEN);
906245916cd2Sjpk 				} else
906345916cd2Sjpk 					*tempbuf = '\0';
906445916cd2Sjpk 
906545916cd2Sjpk 				(void) strncat(tempbuf, buf, MAXPATHLEN);
906645916cd2Sjpk 				*buf = '\0';
906745916cd2Sjpk 
906845916cd2Sjpk 				if (blequal(&bslabel, &admin_high)) {
906945916cd2Sjpk 					bslabel = admin_low;
907045916cd2Sjpk 				}
907145916cd2Sjpk 
907245916cd2Sjpk 
907345916cd2Sjpk 				/*
907445916cd2Sjpk 				 * Check for cross-zone symbolic links
907545916cd2Sjpk 				 */
907645916cd2Sjpk 				from_label = getlabelbypath(real_path);
907745916cd2Sjpk 				if (rpath_flag && (from_label != NULL) &&
907845916cd2Sjpk 				    !blequal(&bslabel, from_label)) {
907945916cd2Sjpk 					if ((zoneid =
908045916cd2Sjpk 					    getzoneidbylabel(&bslabel)) == -1) {
908145916cd2Sjpk 						(void) fprintf(stderr,
908245916cd2Sjpk 						    gettext("tar: can't get "
908345916cd2Sjpk 						    "zone ID for %s\n"),
908445916cd2Sjpk 						    tempbuf);
908545916cd2Sjpk 						return (-1);
908645916cd2Sjpk 					}
908745916cd2Sjpk 					if (zone_getattr(zoneid, ZONE_ATTR_NAME,
908845916cd2Sjpk 					    &zonename, ZONENAME_MAX) == -1) {
908945916cd2Sjpk 						/* Badly configured zone info */
909045916cd2Sjpk 						(void) fprintf(stderr,
909145916cd2Sjpk 						    gettext("tar: can't get "
909245916cd2Sjpk 						    "zonename for %s\n"),
909345916cd2Sjpk 						    tempbuf);
909445916cd2Sjpk 						return (-1);
909545916cd2Sjpk 					}
909645916cd2Sjpk 					(void) strncpy(buf, AUTO_ZONE,
909745916cd2Sjpk 					    MAXPATHLEN);
909845916cd2Sjpk 					(void) strncat(buf, "/",
909945916cd2Sjpk 					    MAXPATHLEN);
910045916cd2Sjpk 					(void) strncat(buf, zonename,
910145916cd2Sjpk 					    MAXPATHLEN);
910245916cd2Sjpk 				}
910345916cd2Sjpk 				if (from_label != NULL)
910445916cd2Sjpk 					free(from_label);
910545916cd2Sjpk 				(void) strncat(buf, tempbuf, MAXPATHLEN);
910645916cd2Sjpk 				break;
910745916cd2Sjpk 			}
910845916cd2Sjpk 			mld_flag = 0;
910945916cd2Sjpk 			break;
911045916cd2Sjpk 
911145916cd2Sjpk 		case PATH_TYPE:
911245916cd2Sjpk 
911345916cd2Sjpk 			str++;
911445916cd2Sjpk 			if ((cp = strstr(str, ";;")) != NULL) {
911545916cd2Sjpk 				*cp = '\0';
911645916cd2Sjpk 				(void) strncat(buf, str, MAXPATHLEN);
911745916cd2Sjpk 				str = cp + 2;
911845916cd2Sjpk 				*cp = ';';
911945916cd2Sjpk 			}
912045916cd2Sjpk 			break;
912145916cd2Sjpk 
912245916cd2Sjpk 		default:
912345916cd2Sjpk 
912445916cd2Sjpk 			(void) fprintf(stderr, gettext(
912545916cd2Sjpk 			    "tar: error rebuilding path %s\n"),
912645916cd2Sjpk 			    *namep);
912745916cd2Sjpk 			*buf = '\0';
912845916cd2Sjpk 			str++;
912945916cd2Sjpk 			return (-1);
913045916cd2Sjpk 		}
913145916cd2Sjpk 	}
913245916cd2Sjpk 
913345916cd2Sjpk 	/*
913445916cd2Sjpk 	 * Done for LK_COMP_TYPE
913545916cd2Sjpk 	 */
913645916cd2Sjpk 
913745916cd2Sjpk 	return (0);    /* component path is rebuilt successfully */
913845916cd2Sjpk 
913945916cd2Sjpk } /* end rebuild_lk_comp_path() */
914045916cd2Sjpk 
914145916cd2Sjpk /*
914245916cd2Sjpk  *	Name: check_ext_attr()
914345916cd2Sjpk  *
914445916cd2Sjpk  *	Description:
914545916cd2Sjpk  *		Check the extended attributes for a file being extracted.
914645916cd2Sjpk  *		The attributes being checked here are CMW labels.
914745916cd2Sjpk  *		ACLs are not set here because they are set by the
914845916cd2Sjpk  *		pflag in doxtract().
914945916cd2Sjpk  *
915045916cd2Sjpk  *		If the label doesn't match, return 0
915145916cd2Sjpk  *		else return 1
915245916cd2Sjpk  */
915345916cd2Sjpk static int
check_ext_attr(char * filename)915445916cd2Sjpk check_ext_attr(char *filename)
915545916cd2Sjpk {
915645916cd2Sjpk 	bslabel_t	currentlabel;	/* label from zone */
915745916cd2Sjpk 
915845916cd2Sjpk 	if (bltype(&bs_label, SUN_SL_UN)) {
915945916cd2Sjpk 		/* No label check possible */
916045916cd2Sjpk 		return (0);
916145916cd2Sjpk 	}
916245916cd2Sjpk 	if (getlabel(filename, &currentlabel) != 0) {
916345916cd2Sjpk 		(void) fprintf(stderr,
916445916cd2Sjpk 		    gettext("tar: can't get label for "
916545916cd2Sjpk 		    " %s, getlabel() error: %s\n"),
916645916cd2Sjpk 		    filename, strerror(errno));
916745916cd2Sjpk 		return (0);
916845916cd2Sjpk 	} else if ((blequal(&currentlabel, &bs_label)) == 0) {
916945916cd2Sjpk 		char	*src_label = NULL;	/* ascii label */
917045916cd2Sjpk 
917145916cd2Sjpk 		/* get current src SL */
917245916cd2Sjpk 		if (bsltos(&bs_label, &src_label, 0, 0) <= 0) {
917345916cd2Sjpk 			(void) fprintf(stderr,
917445916cd2Sjpk 			    gettext("tar: can't interpret requested label for"
917545916cd2Sjpk 			    " %s\n"), filename);
917645916cd2Sjpk 		} else {
917745916cd2Sjpk 			(void) fprintf(stderr,
917845916cd2Sjpk 			    gettext("tar: can't apply label %s to %s\n"),
917945916cd2Sjpk 			    src_label, filename);
918045916cd2Sjpk 			free(src_label);
918145916cd2Sjpk 		}
918245916cd2Sjpk 		(void) fprintf(stderr,
918345916cd2Sjpk 		    gettext("tar: %s not restored\n"), filename);
918445916cd2Sjpk 		return (0);
918545916cd2Sjpk 	}
918645916cd2Sjpk 	return (1);
918745916cd2Sjpk 
918845916cd2Sjpk }	/* end check_ext_attr */
918936802407SRich Burridge 
919036802407SRich Burridge /* Compressing a tar file using compression method provided in 'opt' */
919136802407SRich Burridge 
919236802407SRich Burridge static void
compress_back()919336802407SRich Burridge compress_back()
919436802407SRich Burridge {
919536802407SRich Burridge 	pid_t	pid;
919636802407SRich Burridge 
919736802407SRich Burridge 	if (vflag) {
919836802407SRich Burridge 		(void) fprintf(vfile,
919936802407SRich Burridge 		    gettext("Compressing '%s' with '%s'...\n"),
920036802407SRich Burridge 		    usefile, compress_opt);
920136802407SRich Burridge 	}
920236802407SRich Burridge 	if ((pid = fork()) == 0) {
9203aa7166bdSMilan Jurik 		verify_compress_opt(compress_opt);
920436802407SRich Burridge 		(void) execlp(compress_opt, compress_opt,
920536802407SRich Burridge 		    usefile, NULL);
920636802407SRich Burridge 	} else if (pid == -1) {
920736802407SRich Burridge 		vperror(1, "%s", gettext("Could not fork"));
920836802407SRich Burridge 	}
920936802407SRich Burridge 	wait_pid(pid);
921036802407SRich Burridge 	if (suffix == 0) {
921136802407SRich Burridge 		(void) rename(tfname, usefile);
921236802407SRich Burridge 	}
921336802407SRich Burridge }
921436802407SRich Burridge 
921536802407SRich Burridge /* The magic numbers from /etc/magic */
921636802407SRich Burridge 
921736802407SRich Burridge #define	GZIP_MAGIC	"\037\213"
921836802407SRich Burridge #define	BZIP_MAGIC	"BZh"
921936802407SRich Burridge #define	COMP_MAGIC	"\037\235"
9220506ea5b8SMilan Jurik #define	XZ_MAGIC	"\375\067\172\130\132\000"
922136802407SRich Burridge 
922236802407SRich Burridge void
check_compression(void)9223506ea5b8SMilan Jurik check_compression(void)
922436802407SRich Burridge {
9225506ea5b8SMilan Jurik 	char 	magic[16];
922636802407SRich Burridge 	FILE	*fp;
922736802407SRich Burridge 
922836802407SRich Burridge 	if ((fp = fopen(usefile, "r")) != NULL) {
9229506ea5b8SMilan Jurik 		(void) fread(magic, sizeof (char), 6, fp);
923036802407SRich Burridge 		(void) fclose(fp);
923136802407SRich Burridge 	}
923236802407SRich Burridge 
923336802407SRich Burridge 	if (memcmp(magic, GZIP_MAGIC, 2) == 0) {
923436802407SRich Burridge 		if (xflag || tflag) {
923536802407SRich Burridge 			compress_opt = compress_malloc(strlen(GZCAT) + 1);
923636802407SRich Burridge 			(void) strcpy(compress_opt, GZCAT);
923736802407SRich Burridge 		} else if (uflag || rflag) {
923836802407SRich Burridge 			compress_opt = compress_malloc(strlen(GZIP) + 1);
923936802407SRich Burridge 			(void) strcpy(compress_opt, GZIP);
924036802407SRich Burridge 		}
924136802407SRich Burridge 	} else if (memcmp(magic, BZIP_MAGIC, 2) == 0) {
924236802407SRich Burridge 		if (xflag || tflag) {
924336802407SRich Burridge 			compress_opt = compress_malloc(strlen(BZCAT) + 1);
924436802407SRich Burridge 			(void) strcpy(compress_opt, BZCAT);
924536802407SRich Burridge 		} else if (uflag || rflag) {
924636802407SRich Burridge 			compress_opt = compress_malloc(strlen(BZIP) + 1);
924736802407SRich Burridge 			(void) strcpy(compress_opt, BZIP);
924836802407SRich Burridge 		}
924936802407SRich Burridge 	} else if (memcmp(magic, COMP_MAGIC, 2) == 0) {
925036802407SRich Burridge 		if (xflag || tflag) {
925136802407SRich Burridge 			compress_opt = compress_malloc(strlen(ZCAT) + 1);
925236802407SRich Burridge 			(void) strcpy(compress_opt, ZCAT);
925336802407SRich Burridge 		} else if (uflag || rflag) {
925436802407SRich Burridge 			compress_opt = compress_malloc(strlen(COMPRESS) + 1);
925536802407SRich Burridge 			(void) strcpy(compress_opt, COMPRESS);
925636802407SRich Burridge 		}
9257506ea5b8SMilan Jurik 	} else if (memcmp(magic, XZ_MAGIC, 6) == 0) {
9258506ea5b8SMilan Jurik 		if (xflag || tflag) {
9259506ea5b8SMilan Jurik 			compress_opt = compress_malloc(strlen(XZCAT) + 1);
9260506ea5b8SMilan Jurik 			(void) strcpy(compress_opt, XZCAT);
9261506ea5b8SMilan Jurik 		} else if (uflag || rflag) {
9262506ea5b8SMilan Jurik 			compress_opt = compress_malloc(strlen(XZ) + 1);
9263506ea5b8SMilan Jurik 			(void) strcpy(compress_opt, XZ);
9264506ea5b8SMilan Jurik 		}
926536802407SRich Burridge 	}
926636802407SRich Burridge }
926736802407SRich Burridge 
926836802407SRich Burridge char *
add_suffix()926936802407SRich Burridge add_suffix()
927036802407SRich Burridge {
927136802407SRich Burridge 	(void) strcpy(tfname, usefile);
927236802407SRich Burridge 	if (strcmp(compress_opt, GZIP) == 0) {
927336802407SRich Burridge 		if ((suffix = gz_suffix()) == NULL) {
927436802407SRich Burridge 			strlcat(tfname, gsuffix[0], sizeof (tfname));
927536802407SRich Burridge 			return (gsuffix[0]);
927636802407SRich Burridge 		}
927736802407SRich Burridge 	} else if (strcmp(compress_opt, COMPRESS) == 0) {
927836802407SRich Burridge 		if ((suffix = gz_suffix()) == NULL) {
927936802407SRich Burridge 			strlcat(tfname, gsuffix[6], sizeof (tfname));
928036802407SRich Burridge 			return (gsuffix[6]);
928136802407SRich Burridge 		}
928236802407SRich Burridge 	} else if (strcmp(compress_opt, BZIP) == 0) {
928336802407SRich Burridge 		if ((suffix = bz_suffix()) == NULL) {
928436802407SRich Burridge 			strlcat(tfname, bsuffix[0], sizeof (tfname));
928536802407SRich Burridge 			return (bsuffix[0]);
928636802407SRich Burridge 		}
9287506ea5b8SMilan Jurik 	} else if (strcmp(compress_opt, XZ) == 0) {
9288506ea5b8SMilan Jurik 		if ((suffix = xz_suffix()) == NULL) {
9289506ea5b8SMilan Jurik 			strlcat(tfname, xsuffix[0], sizeof (tfname));
9290506ea5b8SMilan Jurik 			return (xsuffix[0]);
9291506ea5b8SMilan Jurik 		}
929236802407SRich Burridge 	}
929336802407SRich Burridge 	return (NULL);
929436802407SRich Burridge }
929536802407SRich Burridge 
929636802407SRich Burridge /* Decompressing a tar file using compression method from the file type */
929736802407SRich Burridge void
decompress_file(void)929836802407SRich Burridge decompress_file(void)
929936802407SRich Burridge {
930036802407SRich Burridge 	pid_t 	pid;
930136802407SRich Burridge 	char	*added_suffix;
930236802407SRich Burridge 
930336802407SRich Burridge 
930436802407SRich Burridge 	added_suffix = add_suffix();
930536802407SRich Burridge 	if (added_suffix != NULL)  {
930636802407SRich Burridge 		(void) rename(usefile, tfname);
930736802407SRich Burridge 	}
930836802407SRich Burridge 	if ((pid = fork()) == 0) {
930936802407SRich Burridge 		if (vflag) {
931036802407SRich Burridge 			(void) fprintf(vfile,
931136802407SRich Burridge 			    gettext("Decompressing '%s' with "
931236802407SRich Burridge 			    "'%s'...\n"), usefile, compress_opt);
931336802407SRich Burridge 		}
9314aa7166bdSMilan Jurik 		verify_compress_opt(compress_opt);
931536802407SRich Burridge 		(void) execlp(compress_opt, compress_opt, "-df",
931636802407SRich Burridge 		    tfname, NULL);
931753329f75SRich Burridge 		vperror(1, gettext("Could not exec %s"), compress_opt);
931836802407SRich Burridge 	} else if (pid == -1) {
93198e4a71aeSRich Burridge 		vperror(1, gettext("Could not fork"));
932036802407SRich Burridge 	}
932136802407SRich Burridge 	wait_pid(pid);
932236802407SRich Burridge 	if (suffix != NULL) {
932336802407SRich Burridge 		/* restore the file name - original file was without suffix */
932436802407SRich Burridge 		*(usefile + strlen(usefile) - strlen(suffix)) = '\0';
932536802407SRich Burridge 	}
932636802407SRich Burridge }
932736802407SRich Burridge 
932836802407SRich Burridge /* Set the archive for writing and then compress the archive */
932936802407SRich Burridge pid_t
compress_file(void)933036802407SRich Burridge compress_file(void)
933136802407SRich Burridge {
933236802407SRich Burridge 	int fd[2];
933336802407SRich Burridge 	pid_t pid;
933436802407SRich Burridge 
933536802407SRich Burridge 	if (vflag) {
933636802407SRich Burridge 		(void) fprintf(vfile, gettext("Compressing '%s' with "
933736802407SRich Burridge 		    "'%s'...\n"), usefile, compress_opt);
933836802407SRich Burridge 	}
933936802407SRich Burridge 
934036802407SRich Burridge 	if (pipe(fd) < 0) {
934136802407SRich Burridge 		vperror(1, gettext("Could not create pipe"));
934236802407SRich Burridge 	}
9343*c536b1f9SRobert Mustacchi 	if ((pid = fork()) > 0) {
934436802407SRich Burridge 		mt = fd[1];
934536802407SRich Burridge 		(void) close(fd[0]);
934636802407SRich Burridge 		return (pid);
934736802407SRich Burridge 	}
934836802407SRich Burridge 	/* child */
934936802407SRich Burridge 	(void) dup2(fd[0], STDIN_FILENO);
935036802407SRich Burridge 	(void) close(fd[1]);
935136802407SRich Burridge 	(void) dup2(mt, STDOUT_FILENO);
9352aa7166bdSMilan Jurik 	verify_compress_opt(compress_opt);
935336802407SRich Burridge 	(void) execlp(compress_opt, compress_opt, NULL);
935453329f75SRich Burridge 	vperror(1, gettext("Could not exec %s"), compress_opt);
935536802407SRich Burridge 	return (0);	/*NOTREACHED*/
935636802407SRich Burridge }
935736802407SRich Burridge 
935836802407SRich Burridge pid_t
uncompress_file(void)935936802407SRich Burridge uncompress_file(void)
936036802407SRich Burridge {
936136802407SRich Burridge 	int fd[2];
936236802407SRich Burridge 	pid_t pid;
936336802407SRich Burridge 
936436802407SRich Burridge 	if (vflag) {
936536802407SRich Burridge 		(void) fprintf(vfile, gettext("Decompressing '%s' with "
936636802407SRich Burridge 		    "'%s'...\n"), usefile, compress_opt);
936736802407SRich Burridge 	}
936836802407SRich Burridge 
936936802407SRich Burridge 	if (pipe(fd) < 0) {
937036802407SRich Burridge 		vperror(1, gettext("Could not create pipe"));
937136802407SRich Burridge 	}
9372*c536b1f9SRobert Mustacchi 	if ((pid = fork()) > 0) {
937336802407SRich Burridge 		mt = fd[0];
937436802407SRich Burridge 		(void) close(fd[1]);
937536802407SRich Burridge 		return (pid);
937636802407SRich Burridge 	}
937736802407SRich Burridge 	/* child */
937836802407SRich Burridge 	(void) dup2(fd[1], STDOUT_FILENO);
937936802407SRich Burridge 	(void) close(fd[0]);
938036802407SRich Burridge 	(void) dup2(mt, STDIN_FILENO);
9381aa7166bdSMilan Jurik 	verify_compress_opt(compress_opt);
938236802407SRich Burridge 	(void) execlp(compress_opt, compress_opt, NULL);
938353329f75SRich Burridge 	vperror(1, gettext("Could not exec %s"), compress_opt);
938436802407SRich Burridge 	return (0);	/*NOTREACHED*/
938536802407SRich Burridge }
938636802407SRich Burridge 
9387506ea5b8SMilan Jurik /* Checking suffix validity */
938836802407SRich Burridge char *
check_suffix(char ** suf,int size)9389506ea5b8SMilan Jurik check_suffix(char **suf, int size)
939036802407SRich Burridge {
939136802407SRich Burridge 	int 	i;
939236802407SRich Burridge 	int	slen;
939336802407SRich Burridge 	int	nlen = strlen(usefile);
939436802407SRich Burridge 
9395506ea5b8SMilan Jurik 	for (i = 0; i < size; i++) {
9396506ea5b8SMilan Jurik 		slen = strlen(suf[i]);
939736802407SRich Burridge 		if (nlen < slen)
939836802407SRich Burridge 			return (NULL);
9399506ea5b8SMilan Jurik 		if (strcmp(usefile + nlen - slen, suf[i]) == 0)
9400506ea5b8SMilan Jurik 			return (suf[i]);
940136802407SRich Burridge 	}
940236802407SRich Burridge 	return (NULL);
9403506ea5b8SMilan Jurik }
9404506ea5b8SMilan Jurik 
9405506ea5b8SMilan Jurik /* Checking valid 'bzip2' suffix */
9406506ea5b8SMilan Jurik char *
bz_suffix(void)9407506ea5b8SMilan Jurik bz_suffix(void)
9408506ea5b8SMilan Jurik {
9409506ea5b8SMilan Jurik 	return (check_suffix(bsuffix, BSUF));
941036802407SRich Burridge }
941136802407SRich Burridge 
941236802407SRich Burridge /* Checking valid 'gzip' suffix */
941336802407SRich Burridge char *
gz_suffix(void)9414506ea5b8SMilan Jurik gz_suffix(void)
941536802407SRich Burridge {
9416506ea5b8SMilan Jurik 	return (check_suffix(gsuffix, GSUF));
941736802407SRich Burridge }
9418506ea5b8SMilan Jurik 
9419506ea5b8SMilan Jurik /* Checking valid 'xz' suffix */
9420506ea5b8SMilan Jurik char *
xz_suffix(void)9421506ea5b8SMilan Jurik xz_suffix(void)
9422506ea5b8SMilan Jurik {
9423506ea5b8SMilan Jurik 	return (check_suffix(xsuffix, XSUF));
942436802407SRich Burridge }
942536802407SRich Burridge 
942636802407SRich Burridge void *
compress_malloc(size_t size)942736802407SRich Burridge compress_malloc(size_t size)
942836802407SRich Burridge {
942936802407SRich Burridge 	void *opt;
943036802407SRich Burridge 
943136802407SRich Burridge 	if ((opt = malloc(size)) == NULL) {
943236802407SRich Burridge 		vperror(1, "%s",
943336802407SRich Burridge 		    gettext("Could not allocate compress buffer\n"));
943436802407SRich Burridge 	}
943536802407SRich Burridge 	return (opt);
943636802407SRich Burridge }
943736802407SRich Burridge 
943836802407SRich Burridge void
wait_pid(pid_t pid)943936802407SRich Burridge wait_pid(pid_t pid)
944036802407SRich Burridge {
944136802407SRich Burridge 	int status;
944236802407SRich Burridge 
944336802407SRich Burridge 	while (waitpid(pid, &status, 0) == -1 && errno == EINTR)
944436802407SRich Burridge 		;
944536802407SRich Burridge }
9446aa7166bdSMilan Jurik 
9447aa7166bdSMilan Jurik static void
verify_compress_opt(const char * t)9448aa7166bdSMilan Jurik verify_compress_opt(const char *t)
9449aa7166bdSMilan Jurik {
9450aa7166bdSMilan Jurik 	struct stat statbuf;
9451aa7166bdSMilan Jurik 
9452aa7166bdSMilan Jurik 	if (stat(t, &statbuf) == -1)
9453aa7166bdSMilan Jurik 		vperror(1, "%s %s: %s\n", gettext("Could not stat"),
9454aa7166bdSMilan Jurik 		    t, strerror(errno));
9455aa7166bdSMilan Jurik }
9456aa7166bdSMilan Jurik 
9457aa7166bdSMilan Jurik static void
detect_compress(void)9458aa7166bdSMilan Jurik detect_compress(void)
9459aa7166bdSMilan Jurik {
9460aa7166bdSMilan Jurik 	char *zsuf[] = {".Z"};
9461aa7166bdSMilan Jurik 	if (check_suffix(zsuf, 1) != NULL) {
9462aa7166bdSMilan Jurik 		Zflag = 1;
9463aa7166bdSMilan Jurik 	} else if (check_suffix(bsuffix, BSUF) != NULL) {
9464aa7166bdSMilan Jurik 		jflag = 1;
9465aa7166bdSMilan Jurik 	} else if (check_suffix(gsuffix, GSUF) != NULL) {
9466aa7166bdSMilan Jurik 		zflag = 1;
9467aa7166bdSMilan Jurik 	} else if (check_suffix(xsuffix, XSUF) != NULL) {
9468aa7166bdSMilan Jurik 		Jflag = 1;
9469aa7166bdSMilan Jurik 	} else {
9470aa7166bdSMilan Jurik 		vperror(1, "%s\n", gettext("No compression method detected"));
9471aa7166bdSMilan Jurik 	}
9472aa7166bdSMilan Jurik }
9473