xref: /titanic_50/usr/src/cmd/tar/tar.c (revision 3d63ea05cb8474d8036d3588cf8299306a994b8c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
222c0f0499Slovely  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
307c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
347c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <unistd.h>
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/param.h>
427c478bd9Sstevel@tonic-gate #include <sys/stat.h>
437c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
447c478bd9Sstevel@tonic-gate #include <sys/wait.h>
457c478bd9Sstevel@tonic-gate #include <dirent.h>
467c478bd9Sstevel@tonic-gate #include <errno.h>
477c478bd9Sstevel@tonic-gate #include <stdio.h>
487c478bd9Sstevel@tonic-gate #include <signal.h>
497c478bd9Sstevel@tonic-gate #include <ctype.h>
507c478bd9Sstevel@tonic-gate #include <locale.h>
517c478bd9Sstevel@tonic-gate #include <nl_types.h>
527c478bd9Sstevel@tonic-gate #include <langinfo.h>
537c478bd9Sstevel@tonic-gate #include <pwd.h>
547c478bd9Sstevel@tonic-gate #include <grp.h>
557c478bd9Sstevel@tonic-gate #include <fcntl.h>
567c478bd9Sstevel@tonic-gate #include <string.h>
577c478bd9Sstevel@tonic-gate #include <malloc.h>
587c478bd9Sstevel@tonic-gate #include <time.h>
597c478bd9Sstevel@tonic-gate #include <utime.h>
607c478bd9Sstevel@tonic-gate #include <stdlib.h>
617c478bd9Sstevel@tonic-gate #include <stdarg.h>
627c478bd9Sstevel@tonic-gate #include <widec.h>
637c478bd9Sstevel@tonic-gate #include <sys/mtio.h>
647c478bd9Sstevel@tonic-gate #include <libintl.h>
657c478bd9Sstevel@tonic-gate #include <sys/acl.h>
667c478bd9Sstevel@tonic-gate #include <strings.h>
677c478bd9Sstevel@tonic-gate #include <deflt.h>
687c478bd9Sstevel@tonic-gate #include <limits.h>
697c478bd9Sstevel@tonic-gate #include <iconv.h>
707c478bd9Sstevel@tonic-gate #include <assert.h>
71fa9e4066Sahrens #include <aclutils.h>
72*3d63ea05Sas145665 #include "getresponse.h"
73*3d63ea05Sas145665 
747c478bd9Sstevel@tonic-gate #if defined(__SunOS_5_6) || defined(__SunOS_5_7)
757c478bd9Sstevel@tonic-gate extern int defcntl();
767c478bd9Sstevel@tonic-gate #endif
777c478bd9Sstevel@tonic-gate #include <archives.h>
787c478bd9Sstevel@tonic-gate 
7945916cd2Sjpk /* Trusted Extensions */
8045916cd2Sjpk #include <zone.h>
8145916cd2Sjpk #include <tsol/label.h>
8245916cd2Sjpk #include <sys/tsol/label_macro.h>
8345916cd2Sjpk 
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate  * Source compatibility
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  * These constants come from archives.h and sys/fcntl.h
907c478bd9Sstevel@tonic-gate  * and were introduced by the extended attributes project
917c478bd9Sstevel@tonic-gate  * in Solaris 9.
927c478bd9Sstevel@tonic-gate  */
937c478bd9Sstevel@tonic-gate #if !defined(O_XATTR)
947c478bd9Sstevel@tonic-gate #define	AT_SYMLINK_NOFOLLOW	0x1000
957c478bd9Sstevel@tonic-gate #define	AT_REMOVEDIR		0x1
967c478bd9Sstevel@tonic-gate #define	AT_FDCWD		0xffd19553
977c478bd9Sstevel@tonic-gate #define	_XATTR_HDRTYPE		'E'
987c478bd9Sstevel@tonic-gate static int attropen();
997c478bd9Sstevel@tonic-gate static int fstatat();
1007c478bd9Sstevel@tonic-gate static int renameat();
1017c478bd9Sstevel@tonic-gate static int unlinkat();
1027c478bd9Sstevel@tonic-gate static int openat();
1037c478bd9Sstevel@tonic-gate static int fchownat();
1047c478bd9Sstevel@tonic-gate static int futimesat();
1057c478bd9Sstevel@tonic-gate #endif
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  * Compiling with -D_XPG4_2 gets this but produces other problems, so
1097c478bd9Sstevel@tonic-gate  * instead of including sys/time.h and compiling with -D_XPG4_2, I'm
1107c478bd9Sstevel@tonic-gate  * explicitly doing the declaration here.
1117c478bd9Sstevel@tonic-gate  */
1127c478bd9Sstevel@tonic-gate int utimes(const char *path, const struct timeval timeval_ptr[]);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate #ifndef MINSIZE
1157c478bd9Sstevel@tonic-gate #define	MINSIZE 250
1167c478bd9Sstevel@tonic-gate #endif
1177c478bd9Sstevel@tonic-gate #define	DEF_FILE "/etc/default/tar"
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate #define	min(a, b)  ((a) < (b) ? (a) : (b))
1207c478bd9Sstevel@tonic-gate #define	max(a, b)  ((a) > (b) ? (a) : (b))
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /* -DDEBUG	ONLY for debugging */
1237c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1247c478bd9Sstevel@tonic-gate #undef	DEBUG
1257c478bd9Sstevel@tonic-gate #define	DEBUG(a, b, c)\
1267c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "DEBUG - "), (void) fprintf(stderr, a, b, c)
1277c478bd9Sstevel@tonic-gate #endif
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate #define	TBLOCK	512	/* tape block size--should be universal */
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate #ifdef	BSIZE
1327c478bd9Sstevel@tonic-gate #define	SYS_BLOCK BSIZE	/* from sys/param.h:  secondary block size */
1337c478bd9Sstevel@tonic-gate #else	/* BSIZE */
1347c478bd9Sstevel@tonic-gate #define	SYS_BLOCK 512	/* default if no BSIZE in param.h */
1357c478bd9Sstevel@tonic-gate #endif	/* BSIZE */
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate #define	NBLOCK	20
1387c478bd9Sstevel@tonic-gate #define	NAMSIZ	100
1397c478bd9Sstevel@tonic-gate #define	PRESIZ	155
1407c478bd9Sstevel@tonic-gate #define	MAXNAM	256
1417c478bd9Sstevel@tonic-gate #define	MODEMASK 0777777	/* file creation mode mask */
1427c478bd9Sstevel@tonic-gate #define	POSIXMODES 07777	/* mask for POSIX mode bits */
1437c478bd9Sstevel@tonic-gate #define	MAXEXT	9	/* reasonable max # extents for a file */
1447c478bd9Sstevel@tonic-gate #define	EXTMIN	50	/* min blks left on floppy to split a file */
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /* max value dblock.dbuf.efsize can store */
1477c478bd9Sstevel@tonic-gate #define	TAR_EFSIZE_MAX	 0777777777
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate /*
1507c478bd9Sstevel@tonic-gate  * Symbols which specify the values at which the use of the 'E' function
1517c478bd9Sstevel@tonic-gate  * modifier is required to properly store a file.
1527c478bd9Sstevel@tonic-gate  *
1537c478bd9Sstevel@tonic-gate  *     TAR_OFFSET_MAX    - the largest file size we can archive
1547c478bd9Sstevel@tonic-gate  *     OCTAL7CHAR        - the limit for ustar gid, uid, dev
1557c478bd9Sstevel@tonic-gate  */
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate #ifdef XHDR_DEBUG
1587c478bd9Sstevel@tonic-gate /* tiny values which force the creation of extended header entries */
1597c478bd9Sstevel@tonic-gate #define	TAR_OFFSET_MAX 9
1607c478bd9Sstevel@tonic-gate #define	OCTAL7CHAR 2
1617c478bd9Sstevel@tonic-gate #else
1627c478bd9Sstevel@tonic-gate /* normal values */
163b7d62af5Sceastha #define	TAR_OFFSET_MAX	077777777777ULL
1647c478bd9Sstevel@tonic-gate #define	OCTAL7CHAR	07777777
1657c478bd9Sstevel@tonic-gate #endif
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate #define	TBLOCKS(bytes)	(((bytes) + TBLOCK - 1) / TBLOCK)
1687c478bd9Sstevel@tonic-gate #define	K(tblocks)	((tblocks+1)/2)	/* tblocks to Kbytes for printing */
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate #define	MAXLEV	(PATH_MAX / 2)
1717c478bd9Sstevel@tonic-gate #define	LEV0	1
1727c478bd9Sstevel@tonic-gate #define	SYMLINK_LEV0	0
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate #define	TRUE	1
1757c478bd9Sstevel@tonic-gate #define	FALSE	0
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate #define	XATTR_FILE	1
1787c478bd9Sstevel@tonic-gate #define	NORMAL_FILE	0
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate #define	PUT_AS_LINK	1
1817c478bd9Sstevel@tonic-gate #define	PUT_NOTAS_LINK	0
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate #if _FILE_OFFSET_BITS == 64
1847c478bd9Sstevel@tonic-gate #define	FMT_off_t "lld"
1857c478bd9Sstevel@tonic-gate #define	FMT_off_t_o "llo"
1867c478bd9Sstevel@tonic-gate #define	FMT_blkcnt_t "lld"
1877c478bd9Sstevel@tonic-gate #else
1887c478bd9Sstevel@tonic-gate #define	FMT_off_t "ld"
1897c478bd9Sstevel@tonic-gate #define	FMT_off_t_o "lo"
1907c478bd9Sstevel@tonic-gate #define	FMT_blkcnt_t "ld"
1917c478bd9Sstevel@tonic-gate #endif
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate /* ACL support */
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate static
1967c478bd9Sstevel@tonic-gate struct	sec_attr {
1977c478bd9Sstevel@tonic-gate 	char	attr_type;
1987c478bd9Sstevel@tonic-gate 	char	attr_len[7];
1997c478bd9Sstevel@tonic-gate 	char	attr_info[1];
2007c478bd9Sstevel@tonic-gate } *attr;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate /*
2037c478bd9Sstevel@tonic-gate  *
2047c478bd9Sstevel@tonic-gate  * Tar has been changed to support extended attributes.
2057c478bd9Sstevel@tonic-gate  *
2067c478bd9Sstevel@tonic-gate  * As part of this change tar now uses the new *at() syscalls
2077c478bd9Sstevel@tonic-gate  * such as openat, fchownat(), unlinkat()...
2087c478bd9Sstevel@tonic-gate  *
2097c478bd9Sstevel@tonic-gate  * This was done so that attributes can be handled with as few code changes
2107c478bd9Sstevel@tonic-gate  * as possible.
2117c478bd9Sstevel@tonic-gate  *
2127c478bd9Sstevel@tonic-gate  * What this means is that tar now opens the directory that a file or directory
2137c478bd9Sstevel@tonic-gate  * resides in and then performs *at() functions to manipulate the entry.
2147c478bd9Sstevel@tonic-gate  *
2157c478bd9Sstevel@tonic-gate  * For example a new file is now created like this:
2167c478bd9Sstevel@tonic-gate  *
2177c478bd9Sstevel@tonic-gate  * dfd = open(<some dir path>)
2187c478bd9Sstevel@tonic-gate  * fd = openat(dfd, <name>,....);
2197c478bd9Sstevel@tonic-gate  *
2207c478bd9Sstevel@tonic-gate  * or in the case of an extended attribute
2217c478bd9Sstevel@tonic-gate  *
2227c478bd9Sstevel@tonic-gate  * dfd = attropen(<pathname>, ".", ....)
2237c478bd9Sstevel@tonic-gate  *
2247c478bd9Sstevel@tonic-gate  * Once we have a directory file descriptor all of the *at() functions can
2257c478bd9Sstevel@tonic-gate  * be applied to it.
2267c478bd9Sstevel@tonic-gate  *
2277c478bd9Sstevel@tonic-gate  * unlinkat(dfd, <component name>,...)
2287c478bd9Sstevel@tonic-gate  * fchownat(dfd, <component name>,..)
2297c478bd9Sstevel@tonic-gate  *
2307c478bd9Sstevel@tonic-gate  * This works for both normal namespace files and extended attribute file
2317c478bd9Sstevel@tonic-gate  *
2327c478bd9Sstevel@tonic-gate  */
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate /*
2357c478bd9Sstevel@tonic-gate  *
2367c478bd9Sstevel@tonic-gate  * Extended attribute Format
2377c478bd9Sstevel@tonic-gate  *
2387c478bd9Sstevel@tonic-gate  * Extended attributes are stored in two pieces.
2397c478bd9Sstevel@tonic-gate  * 1. An attribute header which has information about
2407c478bd9Sstevel@tonic-gate  *    what file the attribute is for and what the attribute
2417c478bd9Sstevel@tonic-gate  *    is named.
2427c478bd9Sstevel@tonic-gate  * 2. The attribute record itself.  Stored as a normal file type
2437c478bd9Sstevel@tonic-gate  *    of entry.
2447c478bd9Sstevel@tonic-gate  * Both the header and attribute record have special modes/typeflags
2457c478bd9Sstevel@tonic-gate  * associated with them.
2467c478bd9Sstevel@tonic-gate  *
2477c478bd9Sstevel@tonic-gate  * The names of the header in the archive look like:
2487c478bd9Sstevel@tonic-gate  * /dev/null/attr.hdr
2497c478bd9Sstevel@tonic-gate  *
2507c478bd9Sstevel@tonic-gate  * The name of the attribute looks like:
2517c478bd9Sstevel@tonic-gate  * /dev/null/attr
2527c478bd9Sstevel@tonic-gate  *
2537c478bd9Sstevel@tonic-gate  * This is done so that an archiver that doesn't understand these formats
2547c478bd9Sstevel@tonic-gate  * can just dispose of the attribute records.
2557c478bd9Sstevel@tonic-gate  *
2567c478bd9Sstevel@tonic-gate  * The format is composed of a fixed size header followed
2577c478bd9Sstevel@tonic-gate  * by a variable sized xattr_buf. If the attribute is a hard link
2587c478bd9Sstevel@tonic-gate  * to another attribute then another xattr_buf section is included
2597c478bd9Sstevel@tonic-gate  * for the link.
2607c478bd9Sstevel@tonic-gate  *
2617c478bd9Sstevel@tonic-gate  * The xattr_buf is used to define the necessary "pathing" steps
2627c478bd9Sstevel@tonic-gate  * to get to the extended attribute.  This is necessary to support
2637c478bd9Sstevel@tonic-gate  * a fully recursive attribute model where an attribute may itself
2647c478bd9Sstevel@tonic-gate  * have an attribute.
2657c478bd9Sstevel@tonic-gate  *
2667c478bd9Sstevel@tonic-gate  * The basic layout looks like this.
2677c478bd9Sstevel@tonic-gate  *
2687c478bd9Sstevel@tonic-gate  *     --------------------------------
2697c478bd9Sstevel@tonic-gate  *     |                              |
2707c478bd9Sstevel@tonic-gate  *     |         xattr_hdr            |
2717c478bd9Sstevel@tonic-gate  *     |                              |
2727c478bd9Sstevel@tonic-gate  *     --------------------------------
2737c478bd9Sstevel@tonic-gate  *     --------------------------------
2747c478bd9Sstevel@tonic-gate  *     |                              |
2757c478bd9Sstevel@tonic-gate  *     |        xattr_buf             |
2767c478bd9Sstevel@tonic-gate  *     |                              |
2777c478bd9Sstevel@tonic-gate  *     --------------------------------
2787c478bd9Sstevel@tonic-gate  *     --------------------------------
2797c478bd9Sstevel@tonic-gate  *     |                              |
2807c478bd9Sstevel@tonic-gate  *     |      (optional link info)    |
2817c478bd9Sstevel@tonic-gate  *     |                              |
2827c478bd9Sstevel@tonic-gate  *     --------------------------------
2837c478bd9Sstevel@tonic-gate  *     --------------------------------
2847c478bd9Sstevel@tonic-gate  *     |                              |
2857c478bd9Sstevel@tonic-gate  *     |      attribute itself        |
2867c478bd9Sstevel@tonic-gate  *     |      stored as normal tar    |
2877c478bd9Sstevel@tonic-gate  *     |      or cpio data with       |
2887c478bd9Sstevel@tonic-gate  *     |      special mode or         |
2897c478bd9Sstevel@tonic-gate  *     |      typeflag                |
2907c478bd9Sstevel@tonic-gate  *     |                              |
2917c478bd9Sstevel@tonic-gate  *     --------------------------------
2927c478bd9Sstevel@tonic-gate  *
2937c478bd9Sstevel@tonic-gate  */
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate /*
2967c478bd9Sstevel@tonic-gate  * xattrhead is a pointer to the xattr_hdr
2977c478bd9Sstevel@tonic-gate  *
2987c478bd9Sstevel@tonic-gate  * xattrp is a pointer to the xattr_buf structure
2997c478bd9Sstevel@tonic-gate  * which contains the "pathing" steps to get to attributes
3007c478bd9Sstevel@tonic-gate  *
3017c478bd9Sstevel@tonic-gate  * xattr_linkp is a pointer to another xattr_buf structure that is
3027c478bd9Sstevel@tonic-gate  * only used when an attribute is actually linked to another attribute
3037c478bd9Sstevel@tonic-gate  *
3047c478bd9Sstevel@tonic-gate  */
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate static struct xattr_hdr *xattrhead;
3077c478bd9Sstevel@tonic-gate static struct xattr_buf *xattrp;
3087c478bd9Sstevel@tonic-gate static struct xattr_buf *xattr_linkp;	/* pointer to link info, if any */
3097c478bd9Sstevel@tonic-gate static char *xattraname;		/* attribute name */
3107c478bd9Sstevel@tonic-gate static char *xattr_linkaname;		/* attribute attribute is linked to */
3117c478bd9Sstevel@tonic-gate static char Hiddendir;			/* are we processing hidden xattr dir */
3127c478bd9Sstevel@tonic-gate static char xattrbadhead;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate /* Was statically allocated tbuf[NBLOCK] */
3157c478bd9Sstevel@tonic-gate static
3167c478bd9Sstevel@tonic-gate union hblock {
3177c478bd9Sstevel@tonic-gate 	char dummy[TBLOCK];
3187c478bd9Sstevel@tonic-gate 	struct header {
3197c478bd9Sstevel@tonic-gate 		char name[NAMSIZ];	/* If non-null prefix, path is	*/
3207c478bd9Sstevel@tonic-gate 					/* <prefix>/<name>;  otherwise	*/
3217c478bd9Sstevel@tonic-gate 					/* <name>			*/
3227c478bd9Sstevel@tonic-gate 		char mode[8];
3237c478bd9Sstevel@tonic-gate 		char uid[8];
3247c478bd9Sstevel@tonic-gate 		char gid[8];
3257c478bd9Sstevel@tonic-gate 		char size[12];		/* size of this extent if file split */
3267c478bd9Sstevel@tonic-gate 		char mtime[12];
3277c478bd9Sstevel@tonic-gate 		char chksum[8];
3287c478bd9Sstevel@tonic-gate 		char typeflag;
3297c478bd9Sstevel@tonic-gate 		char linkname[NAMSIZ];
3307c478bd9Sstevel@tonic-gate 		char magic[6];
3317c478bd9Sstevel@tonic-gate 		char version[2];
3327c478bd9Sstevel@tonic-gate 		char uname[32];
3337c478bd9Sstevel@tonic-gate 		char gname[32];
3347c478bd9Sstevel@tonic-gate 		char devmajor[8];
3357c478bd9Sstevel@tonic-gate 		char devminor[8];
3367c478bd9Sstevel@tonic-gate 		char prefix[PRESIZ];	/* Together with "name", the path of */
3377c478bd9Sstevel@tonic-gate 					/* the file:  <prefix>/<name>	*/
3387c478bd9Sstevel@tonic-gate 		char extno;		/* extent #, null if not split */
3397c478bd9Sstevel@tonic-gate 		char extotal;		/* total extents */
3407c478bd9Sstevel@tonic-gate 		char efsize[10];	/* size of entire file */
3417c478bd9Sstevel@tonic-gate 	} dbuf;
3427c478bd9Sstevel@tonic-gate } dblock, *tbuf, xhdr_buf;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate static
3457c478bd9Sstevel@tonic-gate struct xtar_hdr {
3467c478bd9Sstevel@tonic-gate 	uid_t		x_uid,		/* Uid of file */
3477c478bd9Sstevel@tonic-gate 			x_gid;		/* Gid of file */
3487c478bd9Sstevel@tonic-gate 	major_t		x_devmajor;	/* Device major node */
3497c478bd9Sstevel@tonic-gate 	minor_t		x_devminor;	/* Device minor node */
3507c478bd9Sstevel@tonic-gate 	off_t		x_filesz;	/* Length of file */
3517c478bd9Sstevel@tonic-gate 	char		*x_uname,	/* Pointer to name of user */
3527c478bd9Sstevel@tonic-gate 			*x_gname,	/* Pointer to gid of user */
3537c478bd9Sstevel@tonic-gate 			*x_linkpath,	/* Path for a hard/symbolic link */
3547c478bd9Sstevel@tonic-gate 			*x_path;	/* Path of file */
3557c478bd9Sstevel@tonic-gate 	timestruc_t	x_mtime;	/* Seconds and nanoseconds */
3567c478bd9Sstevel@tonic-gate } Xtarhdr;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate static
3597c478bd9Sstevel@tonic-gate struct gen_hdr {
3607c478bd9Sstevel@tonic-gate 	ulong_t		g_mode;		/* Mode of file */
3617c478bd9Sstevel@tonic-gate 	uid_t		g_uid,		/* Uid of file */
3627c478bd9Sstevel@tonic-gate 			g_gid;		/* Gid of file */
3637c478bd9Sstevel@tonic-gate 	off_t		g_filesz;	/* Length of file */
3647c478bd9Sstevel@tonic-gate 	time_t		g_mtime;	/* Modification time */
3657c478bd9Sstevel@tonic-gate 	uint_t		g_cksum;	/* Checksum of file */
3667c478bd9Sstevel@tonic-gate 	ulong_t		g_devmajor,	/* File system of file */
3677c478bd9Sstevel@tonic-gate 			g_devminor;	/* Major/minor of special files */
3687c478bd9Sstevel@tonic-gate } Gen;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate static
3717c478bd9Sstevel@tonic-gate struct linkbuf {
3727c478bd9Sstevel@tonic-gate 	ino_t	inum;
3737c478bd9Sstevel@tonic-gate 	dev_t	devnum;
3747c478bd9Sstevel@tonic-gate 	int	count;
3757c478bd9Sstevel@tonic-gate 	char	pathname[MAXNAM+1];	/* added 1 for last NULL */
3767c478bd9Sstevel@tonic-gate 	char 	attrname[MAXNAM+1];
3777c478bd9Sstevel@tonic-gate 	struct	linkbuf *nextp;
3787c478bd9Sstevel@tonic-gate } *ihead;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate /* see comments before build_table() */
3817c478bd9Sstevel@tonic-gate #define	TABLE_SIZE 512
3827c478bd9Sstevel@tonic-gate struct	file_list	{
3837c478bd9Sstevel@tonic-gate 	char	*name;			/* Name of file to {in,ex}clude */
3847c478bd9Sstevel@tonic-gate 	struct	file_list	*next;	/* Linked list */
3857c478bd9Sstevel@tonic-gate };
3867c478bd9Sstevel@tonic-gate static	struct	file_list	*exclude_tbl[TABLE_SIZE],
3877c478bd9Sstevel@tonic-gate 				*include_tbl[TABLE_SIZE];
3887c478bd9Sstevel@tonic-gate 
38945916cd2Sjpk static int	append_secattr(char **, int *, int, char *, char);
3907c478bd9Sstevel@tonic-gate static void	write_ancillary(union hblock *, char *, int, char);
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate static void add_file_to_table(struct file_list *table[], char *str);
3937c478bd9Sstevel@tonic-gate static void assert_string(char *s, char *msg);
3947c478bd9Sstevel@tonic-gate static int istape(int fd, int type);
3957c478bd9Sstevel@tonic-gate static void backtape(void);
3967c478bd9Sstevel@tonic-gate static void build_table(struct file_list *table[], char *file);
3977c478bd9Sstevel@tonic-gate static void check_prefix(char **namep, char **dirp, char **compp);
3987c478bd9Sstevel@tonic-gate static void closevol(void);
3997c478bd9Sstevel@tonic-gate static void copy(void *dst, void *src);
4007c478bd9Sstevel@tonic-gate static int convtoreg(off_t);
4012c0f0499Slovely static void delete_target(int fd, char *comp, char *namep);
4027c478bd9Sstevel@tonic-gate static void doDirTimes(char *name, timestruc_t modTime);
4037c478bd9Sstevel@tonic-gate static void done(int n);
4047c478bd9Sstevel@tonic-gate static void dorep(char *argv[]);
4057c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
4067c478bd9Sstevel@tonic-gate static void dotable(char *argv[], int cnt);
4077c478bd9Sstevel@tonic-gate static void doxtract(char *argv[], int cnt);
4087c478bd9Sstevel@tonic-gate #else
4097c478bd9Sstevel@tonic-gate static void dotable(char *argv[]);
4107c478bd9Sstevel@tonic-gate static void doxtract(char *argv[]);
4117c478bd9Sstevel@tonic-gate #endif
4127c478bd9Sstevel@tonic-gate static void fatal(char *format, ...);
4137c478bd9Sstevel@tonic-gate static void vperror(int exit_status, char *fmt, ...);
4147c478bd9Sstevel@tonic-gate static void flushtape(void);
4157c478bd9Sstevel@tonic-gate static void getdir(void);
4167c478bd9Sstevel@tonic-gate static void *getmem(size_t);
4177c478bd9Sstevel@tonic-gate static void longt(struct stat *st, char aclchar);
4187c478bd9Sstevel@tonic-gate static int makeDir(char *name);
4197c478bd9Sstevel@tonic-gate static void mterr(char *operation, int i, int exitcode);
4207c478bd9Sstevel@tonic-gate static void newvol(void);
4217c478bd9Sstevel@tonic-gate static void passtape(void);
4227c478bd9Sstevel@tonic-gate static void putempty(blkcnt_t n);
4237c478bd9Sstevel@tonic-gate static int putfile(char *longname, char *shortname, char *parent,
4247c478bd9Sstevel@tonic-gate 		int filetype, int lev, int symlink_lev);
4257c478bd9Sstevel@tonic-gate static void readtape(char *buffer);
4267c478bd9Sstevel@tonic-gate static void seekdisk(blkcnt_t blocks);
4277c478bd9Sstevel@tonic-gate static void setPathTimes(int dirfd, char *path, timestruc_t modTime);
4287c478bd9Sstevel@tonic-gate static void splitfile(char *longname, int ifd, char *name,
4297c478bd9Sstevel@tonic-gate 	char *prefix, int filetype);
4307c478bd9Sstevel@tonic-gate static void tomodes(struct stat *sp);
4317c478bd9Sstevel@tonic-gate static void usage(void);
4327c478bd9Sstevel@tonic-gate static void xblocks(off_t bytes, int ofile);
4337c478bd9Sstevel@tonic-gate static void xsfile(int ofd);
4347c478bd9Sstevel@tonic-gate static void resugname(int dirfd, char *name, int symflag);
4357c478bd9Sstevel@tonic-gate static int bcheck(char *bstr);
4367c478bd9Sstevel@tonic-gate static int checkdir(char *name);
4377c478bd9Sstevel@tonic-gate static int checksum(union hblock *dblockp);
4387c478bd9Sstevel@tonic-gate #ifdef	EUC
4397c478bd9Sstevel@tonic-gate static int checksum_signed(union hblock *dblockp);
4407c478bd9Sstevel@tonic-gate #endif	/* EUC */
4417c478bd9Sstevel@tonic-gate static int checkupdate(char *arg);
4427c478bd9Sstevel@tonic-gate static int checkw(char c, char *name);
4437c478bd9Sstevel@tonic-gate static int cmp(char *b, char *s, int n);
4447c478bd9Sstevel@tonic-gate static int defset(char *arch);
4457c478bd9Sstevel@tonic-gate static int endtape(void);
4467c478bd9Sstevel@tonic-gate static int is_in_table(struct file_list *table[], char *str);
4477c478bd9Sstevel@tonic-gate static int notsame(void);
4487c478bd9Sstevel@tonic-gate static int is_prefix(char *s1, char *s2);
4497c478bd9Sstevel@tonic-gate static int response(void);
4507c478bd9Sstevel@tonic-gate static int build_dblock(const char *, const char *, const char,
4517c478bd9Sstevel@tonic-gate 	const int filetype, const struct stat *, const dev_t, const char *);
4527c478bd9Sstevel@tonic-gate static unsigned int hash(char *str);
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
4557c478bd9Sstevel@tonic-gate static void initarg(char *argv[], char *file);
4567c478bd9Sstevel@tonic-gate static char *nextarg();
4577c478bd9Sstevel@tonic-gate #endif
4587c478bd9Sstevel@tonic-gate static blkcnt_t kcheck(char *kstr);
4597c478bd9Sstevel@tonic-gate static off_t bsrch(char *s, int n, off_t l, off_t h);
4607c478bd9Sstevel@tonic-gate static void onintr(int sig);
4617c478bd9Sstevel@tonic-gate static void onquit(int sig);
4627c478bd9Sstevel@tonic-gate static void onhup(int sig);
4637c478bd9Sstevel@tonic-gate static uid_t getuidbyname(char *);
4647c478bd9Sstevel@tonic-gate static gid_t getgidbyname(char *);
4657c478bd9Sstevel@tonic-gate static char *getname(gid_t);
4667c478bd9Sstevel@tonic-gate static char *getgroup(gid_t);
4677c478bd9Sstevel@tonic-gate static int checkf(char *name, int mode, int howmuch);
4687c478bd9Sstevel@tonic-gate static int writetbuf(char *buffer, int n);
4697c478bd9Sstevel@tonic-gate static int wantit(char *argv[], char **namep, char **dirp, char **comp);
47045916cd2Sjpk static void append_ext_attr(char *shortname, char **secinfo, int *len);
4717c478bd9Sstevel@tonic-gate static int get_xdata(void);
4727c478bd9Sstevel@tonic-gate static void gen_num(const char *keyword, const u_longlong_t number);
4737c478bd9Sstevel@tonic-gate static void gen_date(const char *keyword, const timestruc_t time_value);
4747c478bd9Sstevel@tonic-gate static void gen_string(const char *keyword, const char *value);
4757c478bd9Sstevel@tonic-gate static void get_xtime(char *value, timestruc_t *xtime);
4767c478bd9Sstevel@tonic-gate static int chk_path_build(char *name, char *longname, char *linkname,
4777c478bd9Sstevel@tonic-gate     char *prefix, char type, int filetype);
4787c478bd9Sstevel@tonic-gate static int gen_utf8_names(const char *filename);
4797c478bd9Sstevel@tonic-gate static int utf8_local(char *option, char **Xhdr_ptrptr, char *target,
4807c478bd9Sstevel@tonic-gate     const char *src, int max_val);
4817c478bd9Sstevel@tonic-gate static int local_utf8(char **Xhdr_ptrptr, char *target, const char *src,
4827c478bd9Sstevel@tonic-gate     iconv_t iconv_cd, int xhdrflg, int max_val);
4837c478bd9Sstevel@tonic-gate static int c_utf8(char *target, const char *source);
4847c478bd9Sstevel@tonic-gate static int getstat(int dirfd, char *longname, char *shortname);
4857c478bd9Sstevel@tonic-gate static void xattrs_put(char *, char *, char *);
4867c478bd9Sstevel@tonic-gate static void prepare_xattr(char **, char	*, char	*,
4877c478bd9Sstevel@tonic-gate     char, struct linkbuf *, int *);
4887c478bd9Sstevel@tonic-gate static int put_link(char *name, char *longname, char *component, char *prefix,
4897c478bd9Sstevel@tonic-gate     int filetype, char typeflag);
4907c478bd9Sstevel@tonic-gate static int put_extra_attributes(char *longname, char *shortname,
4917c478bd9Sstevel@tonic-gate     char *prefix, int filetype, char typeflag);
4927c478bd9Sstevel@tonic-gate static int put_xattr_hdr(char *longname, char *shortname, char *prefix,
4937c478bd9Sstevel@tonic-gate     int typeflag, int filetype, struct linkbuf *lp);
4947c478bd9Sstevel@tonic-gate static int read_xattr_hdr();
49545916cd2Sjpk 
49645916cd2Sjpk /* Trusted Extensions */
49745916cd2Sjpk #define	AUTO_ZONE	"/zone"
49845916cd2Sjpk 
49945916cd2Sjpk static void extract_attr(char **file_ptr, struct sec_attr *);
50045916cd2Sjpk static int check_ext_attr(char *filename);
50145916cd2Sjpk static void rebuild_comp_path(char *str, char **namep);
50245916cd2Sjpk static int rebuild_lk_comp_path(char *str, char **namep);
50345916cd2Sjpk 
5047c478bd9Sstevel@tonic-gate static void get_parent(char *path, char *dir);
5057c478bd9Sstevel@tonic-gate static char *get_component(char *path);
5067c478bd9Sstevel@tonic-gate static int retry_attrdir_open(char *name);
5077c478bd9Sstevel@tonic-gate static char *skipslashes(char *string, char *start);
5087c478bd9Sstevel@tonic-gate static void chop_endslashes(char *path);
509fa9e4066Sahrens 
5107c478bd9Sstevel@tonic-gate static	struct stat stbuf;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate static	int	checkflag = 0;
5137c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
5147c478bd9Sstevel@tonic-gate static	int	Fileflag;
5157c478bd9Sstevel@tonic-gate char    *sysv3_env;
5167c478bd9Sstevel@tonic-gate #endif
5177c478bd9Sstevel@tonic-gate static	int	Xflag, Fflag, iflag, hflag, Bflag, Iflag;
5187c478bd9Sstevel@tonic-gate static	int	rflag, xflag, vflag, tflag, mt, cflag, mflag, pflag;
5197c478bd9Sstevel@tonic-gate static	int	uflag;
5207c478bd9Sstevel@tonic-gate static	int	eflag, errflag, qflag;
5217c478bd9Sstevel@tonic-gate static	int	oflag;
5227c478bd9Sstevel@tonic-gate static	int	bflag, kflag, Aflag;
5237c478bd9Sstevel@tonic-gate static 	int	Pflag;			/* POSIX conformant archive */
5247c478bd9Sstevel@tonic-gate static	int	Eflag;			/* Allow files greater than 8GB */
5257c478bd9Sstevel@tonic-gate static	int	atflag;			/* traverse extended attributes */
5267c478bd9Sstevel@tonic-gate static	int	Dflag;			/* Data change flag */
52745916cd2Sjpk /* Trusted Extensions */
52845916cd2Sjpk static	int	Tflag;			/* Trusted Extensions attr flags */
52945916cd2Sjpk static	int	dir_flag;		/* for attribute extract */
53045916cd2Sjpk static	int	mld_flag;		/* for attribute extract */
53145916cd2Sjpk static	char	*orig_namep;		/* original namep - unadorned */
53245916cd2Sjpk static	int	rpath_flag;		/* MLD real path is rebuilt */
53345916cd2Sjpk static	char	real_path[MAXPATHLEN];	/* MLD real path */
53445916cd2Sjpk static	int	lk_rpath_flag;		/* linked to real path is rebuilt */
53545916cd2Sjpk static	char	lk_real_path[MAXPATHLEN]; /* linked real path */
53645916cd2Sjpk static	bslabel_t	bs_label;	/* for attribute extract */
53745916cd2Sjpk static	bslabel_t	admin_low;
53845916cd2Sjpk static	bslabel_t	admin_high;
53945916cd2Sjpk static	int	ignored_aprivs = 0;
54045916cd2Sjpk static	int	ignored_fprivs = 0;
54145916cd2Sjpk static	int	ignored_fattrs = 0;
54245916cd2Sjpk 
5437c478bd9Sstevel@tonic-gate static	int	term, chksum, wflag,
5447c478bd9Sstevel@tonic-gate 		first = TRUE, defaults_used = FALSE, linkerrok;
5457c478bd9Sstevel@tonic-gate static	blkcnt_t	recno;
5467c478bd9Sstevel@tonic-gate static	int	freemem = 1;
5477c478bd9Sstevel@tonic-gate static	int	nblock = NBLOCK;
5487c478bd9Sstevel@tonic-gate static	int	Errflg = 0;
5497c478bd9Sstevel@tonic-gate static	int	exitflag = 0;
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate static	dev_t	mt_dev;		/* device containing output file */
5527c478bd9Sstevel@tonic-gate static	ino_t	mt_ino;		/* inode number of output file */
5537c478bd9Sstevel@tonic-gate static	int	mt_devtype;	/* dev type of archive, from stat structure */
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate static	int update = 1;		/* for `open' call */
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate static	off_t	low;
5587c478bd9Sstevel@tonic-gate static	off_t	high;
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate static	FILE	*tfile;
5617c478bd9Sstevel@tonic-gate static	FILE	*vfile = stdout;
5627c478bd9Sstevel@tonic-gate static	char	tname[] = "/tmp/tarXXXXXX";
5637c478bd9Sstevel@tonic-gate static	char	archive[] = "archive0=";
5647c478bd9Sstevel@tonic-gate static	char	*Xfile;
5657c478bd9Sstevel@tonic-gate static	char	*usefile;
5667c478bd9Sstevel@tonic-gate static	char	*Filefile;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate static	int	mulvol;		/* multi-volume option selected */
5697c478bd9Sstevel@tonic-gate static	blkcnt_t	blocklim; /* number of blocks to accept per volume */
5707c478bd9Sstevel@tonic-gate static	blkcnt_t	tapepos; /* current block number to be written */
5717c478bd9Sstevel@tonic-gate static	int	NotTape;	/* true if tape is a disk */
5727c478bd9Sstevel@tonic-gate static	int	dumping;	/* true if writing a tape or other archive */
5737c478bd9Sstevel@tonic-gate static	int	extno;		/* number of extent:  starts at 1 */
5747c478bd9Sstevel@tonic-gate static	int	extotal;	/* total extents in this file */
5757c478bd9Sstevel@tonic-gate static	off_t	extsize;	/* size of current extent during extraction */
5767c478bd9Sstevel@tonic-gate static	ushort_t	Oumask = 0;	/* old umask value */
5777c478bd9Sstevel@tonic-gate static 	int is_posix;	/* true if archive we're reading is POSIX-conformant */
5787c478bd9Sstevel@tonic-gate static	const	char	*magic_type = "ustar";
5797c478bd9Sstevel@tonic-gate static	size_t	xrec_size = 8 * PATH_MAX;	/* extended rec initial size */
5807c478bd9Sstevel@tonic-gate static	char	*xrec_ptr;
5817c478bd9Sstevel@tonic-gate static	off_t	xrec_offset = 0;
5827c478bd9Sstevel@tonic-gate static	int	Xhdrflag;
5837c478bd9Sstevel@tonic-gate static	int	charset_type = 0;
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate static	u_longlong_t	xhdr_flgs;	/* Bits set determine which items */
5867c478bd9Sstevel@tonic-gate 					/*   need to be in extended header. */
5877c478bd9Sstevel@tonic-gate #define	_X_DEVMAJOR	0x1
5887c478bd9Sstevel@tonic-gate #define	_X_DEVMINOR	0x2
5897c478bd9Sstevel@tonic-gate #define	_X_GID		0x4
5907c478bd9Sstevel@tonic-gate #define	_X_GNAME	0x8
5917c478bd9Sstevel@tonic-gate #define	_X_LINKPATH	0x10
5927c478bd9Sstevel@tonic-gate #define	_X_PATH		0x20
5937c478bd9Sstevel@tonic-gate #define	_X_SIZE		0x40
5947c478bd9Sstevel@tonic-gate #define	_X_UID		0x80
5957c478bd9Sstevel@tonic-gate #define	_X_UNAME	0x100
5967c478bd9Sstevel@tonic-gate #define	_X_ATIME	0x200
5977c478bd9Sstevel@tonic-gate #define	_X_CTIME	0x400
5987c478bd9Sstevel@tonic-gate #define	_X_MTIME	0x800
5997c478bd9Sstevel@tonic-gate #define	_X_LAST		0x40000000
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate #define	PID_MAX_DIGITS		(10 * sizeof (pid_t) / 4)
6027c478bd9Sstevel@tonic-gate #define	TIME_MAX_DIGITS		(10 * sizeof (time_t) / 4)
6037c478bd9Sstevel@tonic-gate #define	LONG_MAX_DIGITS		(10 * sizeof (long) / 4)
6047c478bd9Sstevel@tonic-gate #define	ULONGLONG_MAX_DIGITS	(10 * sizeof (u_longlong_t) / 4)
6057c478bd9Sstevel@tonic-gate /*
6067c478bd9Sstevel@tonic-gate  * UTF_8 encoding requires more space than the current codeset equivalent.
6077c478bd9Sstevel@tonic-gate  * Currently a factor of 2-3 would suffice, but it is possible for a factor
6087c478bd9Sstevel@tonic-gate  * of 6 to be needed in the future, so for saftey, we use that here.
6097c478bd9Sstevel@tonic-gate  */
6107c478bd9Sstevel@tonic-gate #define	UTF_8_FACTOR	6
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate static	u_longlong_t	xhdr_count = 0;
6137c478bd9Sstevel@tonic-gate static char		xhdr_dirname[PRESIZ + 1];
6147c478bd9Sstevel@tonic-gate static char		pidchars[PID_MAX_DIGITS + 1];
6157c478bd9Sstevel@tonic-gate static char		*tchar = "";		/* null linkpath */
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate static	char	local_path[UTF_8_FACTOR * PATH_MAX + 1];
6187c478bd9Sstevel@tonic-gate static	char	local_linkpath[UTF_8_FACTOR * PATH_MAX + 1];
6197c478bd9Sstevel@tonic-gate static	char	local_gname[UTF_8_FACTOR * _POSIX_NAME_MAX + 1];
6207c478bd9Sstevel@tonic-gate static	char	local_uname[UTF_8_FACTOR * _POSIX_NAME_MAX + 1];
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate /*
6237c478bd9Sstevel@tonic-gate  * The following mechanism is provided to allow us to debug tar in complicated
6247c478bd9Sstevel@tonic-gate  * situations, like when it is part of a pipe.  The idea is that you compile
6257c478bd9Sstevel@tonic-gate  * with -DWAITAROUND defined, and then add the 'z' function modifier to the
6267c478bd9Sstevel@tonic-gate  * target tar invocation, eg. "tar czf tarfile file".  If stderr is available,
6277c478bd9Sstevel@tonic-gate  * it will tell you to which pid to attach the debugger; otherwise, use ps to
6287c478bd9Sstevel@tonic-gate  * find it.  Attach to the process from the debugger, and, *PRESTO*, you are
6297c478bd9Sstevel@tonic-gate  * there!
6307c478bd9Sstevel@tonic-gate  *
6317c478bd9Sstevel@tonic-gate  * Simply assign "waitaround = 0" once you attach to the process, and then
6327c478bd9Sstevel@tonic-gate  * proceed from there as usual.
6337c478bd9Sstevel@tonic-gate  */
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
6367c478bd9Sstevel@tonic-gate int waitaround = 0;		/* wait for rendezvous with the debugger */
6377c478bd9Sstevel@tonic-gate #endif
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate int
6417c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
6427c478bd9Sstevel@tonic-gate {
6437c478bd9Sstevel@tonic-gate 	char		*cp;
6447c478bd9Sstevel@tonic-gate 	char		*tmpdirp;
6457c478bd9Sstevel@tonic-gate 	pid_t		thispid;
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
6487c478bd9Sstevel@tonic-gate 	int	tbl_cnt = 0;
6497c478bd9Sstevel@tonic-gate 	sysv3_env = getenv("SYSV3");
6507c478bd9Sstevel@tonic-gate #endif
6517c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
6527c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
6537c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
6547c478bd9Sstevel@tonic-gate #endif
6557c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
6567c478bd9Sstevel@tonic-gate 	if (argc < 2)
6577c478bd9Sstevel@tonic-gate 		usage();
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	tfile = NULL;
6607c478bd9Sstevel@tonic-gate 
661*3d63ea05Sas145665 	if (init_yes() < 0) {
662*3d63ea05Sas145665 		(void) fprintf(stderr, gettext(ERR_MSG_INIT_YES),
663*3d63ea05Sas145665 		    strerror(errno));
664*3d63ea05Sas145665 		exit(2);
665*3d63ea05Sas145665 	}
666*3d63ea05Sas145665 
6677c478bd9Sstevel@tonic-gate 	/*
6687c478bd9Sstevel@tonic-gate 	 *  For XPG4 compatibility, we must be able to accept the "--"
6697c478bd9Sstevel@tonic-gate 	 *  argument normally recognized by getopt; it is used to delimit
6707c478bd9Sstevel@tonic-gate 	 *  the end opt the options section, and so can only appear in
6717c478bd9Sstevel@tonic-gate 	 *  the position of the first argument.  We simply skip it.
6727c478bd9Sstevel@tonic-gate 	 */
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	if (strcmp(argv[1], "--") == 0) {
6757c478bd9Sstevel@tonic-gate 		argv++;
6767c478bd9Sstevel@tonic-gate 		argc--;
6777c478bd9Sstevel@tonic-gate 		if (argc < 3)
6787c478bd9Sstevel@tonic-gate 			usage();
6797c478bd9Sstevel@tonic-gate 	}
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	argv[argc] = NULL;
6827c478bd9Sstevel@tonic-gate 	argv++;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	/*
6857c478bd9Sstevel@tonic-gate 	 * Set up default values.
6867c478bd9Sstevel@tonic-gate 	 * Search the operand string looking for the first digit or an 'f'.
6877c478bd9Sstevel@tonic-gate 	 * If you find a digit, use the 'archive#' entry in DEF_FILE.
6887c478bd9Sstevel@tonic-gate 	 * If 'f' is given, bypass looking in DEF_FILE altogether.
6897c478bd9Sstevel@tonic-gate 	 * If no digit or 'f' is given, still look in DEF_FILE but use '0'.
6907c478bd9Sstevel@tonic-gate 	 */
6917c478bd9Sstevel@tonic-gate 	if ((usefile = getenv("TAPE")) == (char *)NULL) {
6927c478bd9Sstevel@tonic-gate 		for (cp = *argv; *cp; ++cp)
6937c478bd9Sstevel@tonic-gate 			if (isdigit(*cp) || *cp == 'f')
6947c478bd9Sstevel@tonic-gate 				break;
6957c478bd9Sstevel@tonic-gate 		if (*cp != 'f') {
6967c478bd9Sstevel@tonic-gate 			archive[7] = (*cp)? *cp: '0';
6977c478bd9Sstevel@tonic-gate 			if (!(defaults_used = defset(archive))) {
6987c478bd9Sstevel@tonic-gate 				usefile = NULL;
6997c478bd9Sstevel@tonic-gate 				nblock = 1;
7007c478bd9Sstevel@tonic-gate 				blocklim = 0;
7017c478bd9Sstevel@tonic-gate 				NotTape = 0;
7027c478bd9Sstevel@tonic-gate 			}
7037c478bd9Sstevel@tonic-gate 		}
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	for (cp = *argv++; *cp; cp++)
7077c478bd9Sstevel@tonic-gate 		switch (*cp) {
7087c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
7097c478bd9Sstevel@tonic-gate 		case 'z':
7107c478bd9Sstevel@tonic-gate 			/* rendezvous with the debugger */
7117c478bd9Sstevel@tonic-gate 			waitaround = 1;
7127c478bd9Sstevel@tonic-gate 			break;
7137c478bd9Sstevel@tonic-gate #endif
7147c478bd9Sstevel@tonic-gate 		case 'f':
7157c478bd9Sstevel@tonic-gate 			assert_string(*argv, gettext(
7167c478bd9Sstevel@tonic-gate 			    "tar: tarfile must be specified with 'f' "
7177c478bd9Sstevel@tonic-gate 			    "function modifier\n"));
7187c478bd9Sstevel@tonic-gate 			usefile = *argv++;
7197c478bd9Sstevel@tonic-gate 			break;
7207c478bd9Sstevel@tonic-gate 		case 'F':
7217c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
7227c478bd9Sstevel@tonic-gate 			if (sysv3_env) {
7237c478bd9Sstevel@tonic-gate 				assert_string(*argv, gettext(
7247c478bd9Sstevel@tonic-gate 				    "tar: 'F' requires a file name\n"));
7257c478bd9Sstevel@tonic-gate 				Filefile = *argv++;
7267c478bd9Sstevel@tonic-gate 				Fileflag++;
7277c478bd9Sstevel@tonic-gate 			} else
7287c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
7297c478bd9Sstevel@tonic-gate 				Fflag++;
7307c478bd9Sstevel@tonic-gate 			break;
7317c478bd9Sstevel@tonic-gate 		case 'c':
7327c478bd9Sstevel@tonic-gate 			cflag++;
7337c478bd9Sstevel@tonic-gate 			rflag++;
7347c478bd9Sstevel@tonic-gate 			update = 1;
7357c478bd9Sstevel@tonic-gate 			break;
7367c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
7377c478bd9Sstevel@tonic-gate 		case '@':
7387c478bd9Sstevel@tonic-gate 			atflag++;
7397c478bd9Sstevel@tonic-gate 			break;
7407c478bd9Sstevel@tonic-gate #endif
7417c478bd9Sstevel@tonic-gate 		case 'u':
7427c478bd9Sstevel@tonic-gate 			uflag++;	/* moved code after signals caught */
7437c478bd9Sstevel@tonic-gate 			rflag++;
7447c478bd9Sstevel@tonic-gate 			update = 2;
7457c478bd9Sstevel@tonic-gate 			break;
7467c478bd9Sstevel@tonic-gate 		case 'r':
7477c478bd9Sstevel@tonic-gate 			rflag++;
7487c478bd9Sstevel@tonic-gate 			update = 2;
7497c478bd9Sstevel@tonic-gate 			break;
7507c478bd9Sstevel@tonic-gate 		case 'v':
7517c478bd9Sstevel@tonic-gate 			vflag++;
7527c478bd9Sstevel@tonic-gate 			break;
7537c478bd9Sstevel@tonic-gate 		case 'w':
7547c478bd9Sstevel@tonic-gate 			wflag++;
7557c478bd9Sstevel@tonic-gate 			break;
7567c478bd9Sstevel@tonic-gate 		case 'x':
7577c478bd9Sstevel@tonic-gate 			xflag++;
7587c478bd9Sstevel@tonic-gate 			break;
7597c478bd9Sstevel@tonic-gate 		case 'X':
7607c478bd9Sstevel@tonic-gate 			assert_string(*argv, gettext(
7617c478bd9Sstevel@tonic-gate 			    "tar: exclude file must be specified with 'X' "
7627c478bd9Sstevel@tonic-gate 			    "function modifier\n"));
7637c478bd9Sstevel@tonic-gate 			Xflag = 1;
7647c478bd9Sstevel@tonic-gate 			Xfile = *argv++;
7657c478bd9Sstevel@tonic-gate 			build_table(exclude_tbl, Xfile);
7667c478bd9Sstevel@tonic-gate 			break;
7677c478bd9Sstevel@tonic-gate 		case 't':
7687c478bd9Sstevel@tonic-gate 			tflag++;
7697c478bd9Sstevel@tonic-gate 			break;
7707c478bd9Sstevel@tonic-gate 		case 'm':
7717c478bd9Sstevel@tonic-gate 			mflag++;
7727c478bd9Sstevel@tonic-gate 			break;
7737c478bd9Sstevel@tonic-gate 		case 'p':
7747c478bd9Sstevel@tonic-gate 			pflag++;
7757c478bd9Sstevel@tonic-gate 			break;
7767c478bd9Sstevel@tonic-gate 		case 'D':
7777c478bd9Sstevel@tonic-gate 			Dflag++;
7787c478bd9Sstevel@tonic-gate 			break;
7797c478bd9Sstevel@tonic-gate 		case '-':
7807c478bd9Sstevel@tonic-gate 			/* ignore this silently */
7817c478bd9Sstevel@tonic-gate 			break;
7827c478bd9Sstevel@tonic-gate 		case '0':	/* numeric entries used only for defaults */
7837c478bd9Sstevel@tonic-gate 		case '1':
7847c478bd9Sstevel@tonic-gate 		case '2':
7857c478bd9Sstevel@tonic-gate 		case '3':
7867c478bd9Sstevel@tonic-gate 		case '4':
7877c478bd9Sstevel@tonic-gate 		case '5':
7887c478bd9Sstevel@tonic-gate 		case '6':
7897c478bd9Sstevel@tonic-gate 		case '7':
7907c478bd9Sstevel@tonic-gate 			break;
7917c478bd9Sstevel@tonic-gate 		case 'b':
7927c478bd9Sstevel@tonic-gate 			assert_string(*argv, gettext(
7937c478bd9Sstevel@tonic-gate 			    "tar: blocking factor must be specified "
7947c478bd9Sstevel@tonic-gate 			    "with 'b' function modifier\n"));
7957c478bd9Sstevel@tonic-gate 			bflag++;
7967c478bd9Sstevel@tonic-gate 			nblock = bcheck(*argv++);
7977c478bd9Sstevel@tonic-gate 			break;
7987c478bd9Sstevel@tonic-gate 		case 'q':
7997c478bd9Sstevel@tonic-gate 			qflag++;
8007c478bd9Sstevel@tonic-gate 			break;
8017c478bd9Sstevel@tonic-gate 		case 'k':
8027c478bd9Sstevel@tonic-gate 			assert_string(*argv, gettext(
8037c478bd9Sstevel@tonic-gate 			    "tar: size value must be specified with 'k' "
8047c478bd9Sstevel@tonic-gate 			    "function modifier\n"));
8057c478bd9Sstevel@tonic-gate 			kflag++;
8067c478bd9Sstevel@tonic-gate 			blocklim = kcheck(*argv++);
8077c478bd9Sstevel@tonic-gate 			break;
8087c478bd9Sstevel@tonic-gate 		case 'n':		/* not a magtape (instead of 'k') */
8097c478bd9Sstevel@tonic-gate 			NotTape++;	/* assume non-magtape */
8107c478bd9Sstevel@tonic-gate 			break;
8117c478bd9Sstevel@tonic-gate 		case 'l':
8127c478bd9Sstevel@tonic-gate 			linkerrok++;
8137c478bd9Sstevel@tonic-gate 			break;
8147c478bd9Sstevel@tonic-gate 		case 'e':
8157c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
8167c478bd9Sstevel@tonic-gate 			/* If sysv3 IS set, don't be as verbose */
8177c478bd9Sstevel@tonic-gate 			if (!sysv3_env)
8187c478bd9Sstevel@tonic-gate #endif	/* _iBCS2 */
8197c478bd9Sstevel@tonic-gate 				errflag++;
8207c478bd9Sstevel@tonic-gate 			eflag++;
8217c478bd9Sstevel@tonic-gate 			break;
8227c478bd9Sstevel@tonic-gate 		case 'o':
8237c478bd9Sstevel@tonic-gate 			oflag++;
8247c478bd9Sstevel@tonic-gate 			break;
8257c478bd9Sstevel@tonic-gate 		case 'h':
8267c478bd9Sstevel@tonic-gate 			hflag++;
8277c478bd9Sstevel@tonic-gate 			break;
8287c478bd9Sstevel@tonic-gate 		case 'i':
8297c478bd9Sstevel@tonic-gate 			iflag++;
8307c478bd9Sstevel@tonic-gate 			break;
8317c478bd9Sstevel@tonic-gate 		case 'B':
8327c478bd9Sstevel@tonic-gate 			Bflag++;
8337c478bd9Sstevel@tonic-gate 			break;
8347c478bd9Sstevel@tonic-gate 		case 'P':
8357c478bd9Sstevel@tonic-gate 			Pflag++;
8367c478bd9Sstevel@tonic-gate 			break;
8377c478bd9Sstevel@tonic-gate 		case 'E':
8387c478bd9Sstevel@tonic-gate 			Eflag++;
8397c478bd9Sstevel@tonic-gate 			Pflag++;	/* Only POSIX archive made */
8407c478bd9Sstevel@tonic-gate 			break;
84145916cd2Sjpk 		case 'T':
84245916cd2Sjpk 			Tflag++;	/* Handle Trusted Extensions attrs */
84345916cd2Sjpk 			pflag++;	/* also set flag for ACL */
84445916cd2Sjpk 			break;
8457c478bd9Sstevel@tonic-gate 		default:
8467c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
8477c478bd9Sstevel@tonic-gate 			"tar: %c: unknown function modifier\n"), *cp);
8487c478bd9Sstevel@tonic-gate 			usage();
8497c478bd9Sstevel@tonic-gate 		}
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
8527c478bd9Sstevel@tonic-gate 	if (Xflag && Fileflag) {
8537c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
8547c478bd9Sstevel@tonic-gate 		"tar: specify only one of X or F.\n"));
8557c478bd9Sstevel@tonic-gate 		usage();
8567c478bd9Sstevel@tonic-gate 	}
8577c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	if (!rflag && !xflag && !tflag)
8607c478bd9Sstevel@tonic-gate 		usage();
8617c478bd9Sstevel@tonic-gate 	if ((rflag && xflag) || (xflag && tflag) || (rflag && tflag)) {
8627c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
8637c478bd9Sstevel@tonic-gate 		"tar: specify only one of [ctxru].\n"));
8647c478bd9Sstevel@tonic-gate 		usage();
8657c478bd9Sstevel@tonic-gate 	}
86645916cd2Sjpk 	/* Trusted Extensions attribute handling */
86745916cd2Sjpk 	if (Tflag && ((getzoneid() != GLOBAL_ZONEID) ||
86845916cd2Sjpk 	    !is_system_labeled())) {
86945916cd2Sjpk 		(void) fprintf(stderr, gettext(
87045916cd2Sjpk 		"tar: the 'T' option is only available with "
87145916cd2Sjpk 		    "Trusted Extensions\nand must be run from "
87245916cd2Sjpk 		    "the global zone.\n"));
87345916cd2Sjpk 		usage();
87445916cd2Sjpk 	}
8757c478bd9Sstevel@tonic-gate 	if (cflag && *argv == NULL && Filefile == NULL)
8767c478bd9Sstevel@tonic-gate 		fatal(gettext("Missing filenames"));
8777c478bd9Sstevel@tonic-gate 	if (usefile == NULL)
8787c478bd9Sstevel@tonic-gate 		fatal(gettext("device argument required"));
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	/* alloc a buffer of the right size */
8817c478bd9Sstevel@tonic-gate 	if ((tbuf = (union hblock *)
8827c478bd9Sstevel@tonic-gate 	    calloc(sizeof (union hblock) * nblock, sizeof (char))) ==
8837c478bd9Sstevel@tonic-gate 	    (union hblock *)NULL) {
8847c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
8857c478bd9Sstevel@tonic-gate 		"tar: cannot allocate physio buffer\n"));
8867c478bd9Sstevel@tonic-gate 		exit(1);
8877c478bd9Sstevel@tonic-gate 	}
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	if ((xrec_ptr = malloc(xrec_size)) == NULL) {
8907c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
8917c478bd9Sstevel@tonic-gate 		    "tar: cannot allocate extended header buffer\n"));
8927c478bd9Sstevel@tonic-gate 		exit(1);
8937c478bd9Sstevel@tonic-gate 	}
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
8967c478bd9Sstevel@tonic-gate 	if (waitaround) {
8977c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Rendezvous with tar on pid"
8987c478bd9Sstevel@tonic-gate 		    " %d\n"), getpid());
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 		while (waitaround) {
9017c478bd9Sstevel@tonic-gate 			(void) sleep(10);
9027c478bd9Sstevel@tonic-gate 		}
9037c478bd9Sstevel@tonic-gate 	}
9047c478bd9Sstevel@tonic-gate #endif
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	thispid = getpid();
9077c478bd9Sstevel@tonic-gate 	(void) sprintf(pidchars, "%ld", thispid);
9087c478bd9Sstevel@tonic-gate 	thispid = strlen(pidchars);
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	if ((tmpdirp = getenv("TMPDIR")) == (char *)NULL)
9117c478bd9Sstevel@tonic-gate 		(void) strcpy(xhdr_dirname, "/tmp");
9127c478bd9Sstevel@tonic-gate 	else {
9137c478bd9Sstevel@tonic-gate 		/*
9147c478bd9Sstevel@tonic-gate 		 * Make sure that dir is no longer than what can
9157c478bd9Sstevel@tonic-gate 		 * fit in the prefix part of the header.
9167c478bd9Sstevel@tonic-gate 		 */
9177c478bd9Sstevel@tonic-gate 		if (strlen(tmpdirp) > (size_t)(PRESIZ - thispid - 12)) {
9187c478bd9Sstevel@tonic-gate 			(void) strcpy(xhdr_dirname, "/tmp");
9197c478bd9Sstevel@tonic-gate 			if ((vflag > 0) && (Eflag > 0))
9207c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
9217c478bd9Sstevel@tonic-gate 				    "Ignoring TMPDIR\n"));
9227c478bd9Sstevel@tonic-gate 		} else
9237c478bd9Sstevel@tonic-gate 			(void) strcpy(xhdr_dirname, tmpdirp);
9247c478bd9Sstevel@tonic-gate 	}
9257c478bd9Sstevel@tonic-gate 	(void) strcat(xhdr_dirname, "/PaxHeaders.");
9267c478bd9Sstevel@tonic-gate 	(void) strcat(xhdr_dirname, pidchars);
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	if (rflag) {
9297c478bd9Sstevel@tonic-gate 		if (cflag && tfile != NULL)
9307c478bd9Sstevel@tonic-gate 			usage();
9317c478bd9Sstevel@tonic-gate 		if (signal(SIGINT, SIG_IGN) != SIG_IGN)
9327c478bd9Sstevel@tonic-gate 			(void) signal(SIGINT, onintr);
9337c478bd9Sstevel@tonic-gate 		if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
9347c478bd9Sstevel@tonic-gate 			(void) signal(SIGHUP, onhup);
9357c478bd9Sstevel@tonic-gate 		if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
9367c478bd9Sstevel@tonic-gate 			(void) signal(SIGQUIT, onquit);
9377c478bd9Sstevel@tonic-gate 		if (uflag) {
9387c478bd9Sstevel@tonic-gate 			int tnum;
9397c478bd9Sstevel@tonic-gate 			if ((tnum = mkstemp(tname)) == -1)
9407c478bd9Sstevel@tonic-gate 				vperror(1, "%s", tname);
9417c478bd9Sstevel@tonic-gate 			if ((tfile = fdopen(tnum, "w")) == NULL)
9427c478bd9Sstevel@tonic-gate 				vperror(1, "%s", tname);
9437c478bd9Sstevel@tonic-gate 		}
9447c478bd9Sstevel@tonic-gate 		if (strcmp(usefile, "-") == 0) {
9457c478bd9Sstevel@tonic-gate 			if (cflag == 0)
9467c478bd9Sstevel@tonic-gate 				fatal(gettext(
9477c478bd9Sstevel@tonic-gate 				"can only create standard output archives."));
9487c478bd9Sstevel@tonic-gate 			vfile = stderr;
9497c478bd9Sstevel@tonic-gate 			mt = dup(1);
9507c478bd9Sstevel@tonic-gate 			++bflag;
9517c478bd9Sstevel@tonic-gate 		} else {
9527c478bd9Sstevel@tonic-gate 			if (cflag)
9537c478bd9Sstevel@tonic-gate 				mt = open(usefile,
9547c478bd9Sstevel@tonic-gate 				    O_RDWR|O_CREAT|O_TRUNC, 0666);
9557c478bd9Sstevel@tonic-gate 			else
9567c478bd9Sstevel@tonic-gate 				mt = open(usefile, O_RDWR);
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 			if (mt < 0) {
9597c478bd9Sstevel@tonic-gate 				if (cflag == 0 || (mt =  creat(usefile, 0666))
9607c478bd9Sstevel@tonic-gate 				    < 0)
9617c478bd9Sstevel@tonic-gate 				vperror(1, "%s", usefile);
9627c478bd9Sstevel@tonic-gate 			}
9637c478bd9Sstevel@tonic-gate 		}
9647c478bd9Sstevel@tonic-gate 		/* Get inode and device number of output file */
9657c478bd9Sstevel@tonic-gate 		(void) fstat(mt, &stbuf);
9667c478bd9Sstevel@tonic-gate 		mt_ino = stbuf.st_ino;
9677c478bd9Sstevel@tonic-gate 		mt_dev = stbuf.st_dev;
9687c478bd9Sstevel@tonic-gate 		mt_devtype = stbuf.st_mode & S_IFMT;
9697c478bd9Sstevel@tonic-gate 		NotTape = !istape(mt, mt_devtype);
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 		if (rflag && !cflag && (mt_devtype == S_IFIFO))
9727c478bd9Sstevel@tonic-gate 			fatal(gettext("cannot append to pipe or FIFO."));
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 		if (Aflag && vflag)
9757c478bd9Sstevel@tonic-gate 			(void) printf(
9767c478bd9Sstevel@tonic-gate 			gettext("Suppressing absolute pathnames\n"));
9777c478bd9Sstevel@tonic-gate 		dorep(argv);
9787c478bd9Sstevel@tonic-gate 	} else if (xflag || tflag) {
9797c478bd9Sstevel@tonic-gate 		/*
9807c478bd9Sstevel@tonic-gate 		 * for each argument, check to see if there is a "-I file" pair.
9817c478bd9Sstevel@tonic-gate 		 * if so, move the 3rd argument into "-I"'s place, build_table()
9827c478bd9Sstevel@tonic-gate 		 * using "file"'s name and increment argc one (the second
9837c478bd9Sstevel@tonic-gate 		 * increment appears in the for loop) which removes the two
9847c478bd9Sstevel@tonic-gate 		 * args "-I" and "file" from the argument vector.
9857c478bd9Sstevel@tonic-gate 		 */
9867c478bd9Sstevel@tonic-gate 		for (argc = 0; argv[argc]; argc++) {
9877c478bd9Sstevel@tonic-gate 			if (strcmp(argv[argc], "-I") == 0) {
9887c478bd9Sstevel@tonic-gate 				if (!argv[argc+1]) {
9897c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
9907c478bd9Sstevel@tonic-gate 					"tar: missing argument for -I flag\n"));
9917c478bd9Sstevel@tonic-gate 					done(2);
9927c478bd9Sstevel@tonic-gate 				} else {
9937c478bd9Sstevel@tonic-gate 					Iflag = 1;
9947c478bd9Sstevel@tonic-gate 					argv[argc] = argv[argc+2];
9957c478bd9Sstevel@tonic-gate 					build_table(include_tbl, argv[++argc]);
9967c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
9977c478bd9Sstevel@tonic-gate 					if (Fileflag) {
9987c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(
9997c478bd9Sstevel@tonic-gate 						"tar: only one of I or F.\n"));
10007c478bd9Sstevel@tonic-gate 						usage();
10017c478bd9Sstevel@tonic-gate 					}
10027c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 				}
10057c478bd9Sstevel@tonic-gate 			}
10067c478bd9Sstevel@tonic-gate 		}
10077c478bd9Sstevel@tonic-gate 		if (strcmp(usefile, "-") == 0) {
10087c478bd9Sstevel@tonic-gate 			mt = dup(0);
10097c478bd9Sstevel@tonic-gate 			++bflag;
10107c478bd9Sstevel@tonic-gate 			/* try to recover from short reads when reading stdin */
10117c478bd9Sstevel@tonic-gate 			++Bflag;
10127c478bd9Sstevel@tonic-gate 		} else if ((mt = open(usefile, 0)) < 0)
10137c478bd9Sstevel@tonic-gate 			vperror(1, "%s", usefile);
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 		if (xflag) {
10167c478bd9Sstevel@tonic-gate 			if (Aflag && vflag)
1017eace40a5Sceastha 				(void) printf(gettext(
1018eace40a5Sceastha 				    "Suppressing absolute pathnames.\n"));
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
10217c478bd9Sstevel@tonic-gate 			doxtract(argv, tbl_cnt);
10227c478bd9Sstevel@tonic-gate #else
10237c478bd9Sstevel@tonic-gate 			doxtract(argv);
10247c478bd9Sstevel@tonic-gate #endif
10257c478bd9Sstevel@tonic-gate 		} else if (tflag)
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
10287c478bd9Sstevel@tonic-gate 			dotable(argv, tbl_cnt);
10297c478bd9Sstevel@tonic-gate #else
10307c478bd9Sstevel@tonic-gate 			dotable(argv);
10317c478bd9Sstevel@tonic-gate #endif
10327c478bd9Sstevel@tonic-gate 	}
10337c478bd9Sstevel@tonic-gate 	else
10347c478bd9Sstevel@tonic-gate 		usage();
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	done(Errflg);
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	/* Not reached:  keep compiler quiet */
10397c478bd9Sstevel@tonic-gate 	return (1);
10407c478bd9Sstevel@tonic-gate }
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate static void
10437c478bd9Sstevel@tonic-gate usage(void)
10447c478bd9Sstevel@tonic-gate {
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
10477c478bd9Sstevel@tonic-gate 	if (sysv3_env) {
10487c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
10497c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
105045916cd2Sjpk 		"Usage: tar {c|r|t|u|x}[BDeEhilmnopPqTvw@[0-7]][bfFk][X...] "
10517c478bd9Sstevel@tonic-gate #else
105245916cd2Sjpk 		"Usage: tar {c|r|t|u|x}[BDeEhilmnopPqTvw[0-7]][bfFk][X...] "
10537c478bd9Sstevel@tonic-gate #endif
10547c478bd9Sstevel@tonic-gate 		"[blocksize] [tarfile] [filename] [size] [exclude-file...] "
10557c478bd9Sstevel@tonic-gate 		"{file | -I include-file | -C directory file}...\n"));
10567c478bd9Sstevel@tonic-gate 	} else
10577c478bd9Sstevel@tonic-gate #endif	/* _iBCS2 */
10587c478bd9Sstevel@tonic-gate 	{
10597c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
10607c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
106145916cd2Sjpk 		"Usage: tar {c|r|t|u|x}[BDeEFhilmnopPqTvw@[0-7]][bfk][X...] "
10627c478bd9Sstevel@tonic-gate #else
106345916cd2Sjpk 		"Usage: tar {c|r|t|u|x}[BDeEFhilmnopPqTvw[0-7]][bfk][X...] "
10647c478bd9Sstevel@tonic-gate #endif
10657c478bd9Sstevel@tonic-gate 		"[blocksize] [tarfile] [size] [exclude-file...] "
10667c478bd9Sstevel@tonic-gate 		"{file | -I include-file | -C directory file}...\n"));
10677c478bd9Sstevel@tonic-gate 	}
10687c478bd9Sstevel@tonic-gate 	done(1);
10697c478bd9Sstevel@tonic-gate }
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate /*
10727c478bd9Sstevel@tonic-gate  * dorep - do "replacements"
10737c478bd9Sstevel@tonic-gate  *
10747c478bd9Sstevel@tonic-gate  *	Dorep is responsible for creating ('c'),  appending ('r')
10757c478bd9Sstevel@tonic-gate  *	and updating ('u');
10767c478bd9Sstevel@tonic-gate  */
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate static void
10797c478bd9Sstevel@tonic-gate dorep(char *argv[])
10807c478bd9Sstevel@tonic-gate {
10817c478bd9Sstevel@tonic-gate 	char *cp, *cp2, *p;
10827c478bd9Sstevel@tonic-gate 	char wdir[PATH_MAX+2], tempdir[PATH_MAX+2], *parent;
10837c478bd9Sstevel@tonic-gate 	char file[PATH_MAX*2], origdir[PATH_MAX+1];
10847c478bd9Sstevel@tonic-gate 	FILE *fp = (FILE *)NULL;
10857c478bd9Sstevel@tonic-gate 	FILE *ff = (FILE *)NULL;
10867c478bd9Sstevel@tonic-gate 	int archtype;
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 	if (!cflag) {
10907c478bd9Sstevel@tonic-gate 		xhdr_flgs = 0;
10917c478bd9Sstevel@tonic-gate 		getdir();			/* read header for next file */
10927c478bd9Sstevel@tonic-gate 		if (Xhdrflag > 0) {
10937c478bd9Sstevel@tonic-gate 			if (!Eflag)
10947c478bd9Sstevel@tonic-gate 				fatal(gettext("Archive contains extended"
10957c478bd9Sstevel@tonic-gate 				    " header.  -E flag required.\n"));
10967c478bd9Sstevel@tonic-gate 			(void) get_xdata();	/* Get extended header items */
10977c478bd9Sstevel@tonic-gate 						/*   and regular header */
10987c478bd9Sstevel@tonic-gate 		} else {
10997c478bd9Sstevel@tonic-gate 			if (Eflag)
11007c478bd9Sstevel@tonic-gate 				fatal(gettext("Archive contains no extended"
11017c478bd9Sstevel@tonic-gate 				    " header.  -E flag not allowed.\n"));
11027c478bd9Sstevel@tonic-gate 		}
11037c478bd9Sstevel@tonic-gate 		while (!endtape()) {		/* changed from a do while */
11047c478bd9Sstevel@tonic-gate 			passtape();		/* skip the file data */
11057c478bd9Sstevel@tonic-gate 			if (term)
11067c478bd9Sstevel@tonic-gate 				done(Errflg);	/* received signal to stop */
11077c478bd9Sstevel@tonic-gate 			xhdr_flgs = 0;
11087c478bd9Sstevel@tonic-gate 			getdir();
11097c478bd9Sstevel@tonic-gate 			if (Xhdrflag > 0)
11107c478bd9Sstevel@tonic-gate 				(void) get_xdata();
11117c478bd9Sstevel@tonic-gate 		}
11127c478bd9Sstevel@tonic-gate 		backtape();			/* was called by endtape */
11137c478bd9Sstevel@tonic-gate 		if (tfile != NULL) {
11147c478bd9Sstevel@tonic-gate 			char buf[200];
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 			(void) sprintf(buf, "sort +0 -1 +1nr %s -o %s; awk '$1 "
11177c478bd9Sstevel@tonic-gate 			    "!= prev {print; prev=$1}' %s >%sX;mv %sX %s",
11187c478bd9Sstevel@tonic-gate 			    tname, tname, tname, tname, tname, tname);
11197c478bd9Sstevel@tonic-gate 			(void) fflush(tfile);
11207c478bd9Sstevel@tonic-gate 			(void) system(buf);
11217c478bd9Sstevel@tonic-gate 			(void) freopen(tname, "r", tfile);
11227c478bd9Sstevel@tonic-gate 			(void) fstat(fileno(tfile), &stbuf);
11237c478bd9Sstevel@tonic-gate 			high = stbuf.st_size;
11247c478bd9Sstevel@tonic-gate 		}
11257c478bd9Sstevel@tonic-gate 	}
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	dumping = 1;
11287c478bd9Sstevel@tonic-gate 	if (mulvol) {	/* SP-1 */
11297c478bd9Sstevel@tonic-gate 		if (nblock && (blocklim%nblock) != 0)
11307c478bd9Sstevel@tonic-gate 			fatal(gettext(
11317c478bd9Sstevel@tonic-gate 			"Volume size not a multiple of block size."));
11327c478bd9Sstevel@tonic-gate 		blocklim -= 2;			/* for trailer records */
11337c478bd9Sstevel@tonic-gate 		if (vflag)
11347c478bd9Sstevel@tonic-gate 			(void) fprintf(vfile, gettext("Volume ends at %"
11357c478bd9Sstevel@tonic-gate 			    FMT_blkcnt_t "K, blocking factor = %dK\n"),
11367c478bd9Sstevel@tonic-gate 			    K((blocklim - 1)), K(nblock));
11377c478bd9Sstevel@tonic-gate 	}
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
11407c478bd9Sstevel@tonic-gate 	if (Fileflag) {
11417c478bd9Sstevel@tonic-gate 		if (Filefile != NULL) {
11427c478bd9Sstevel@tonic-gate 			if ((ff = fopen(Filefile, "r")) == NULL)
11437c478bd9Sstevel@tonic-gate 				vperror(0, "%s", Filefile);
11447c478bd9Sstevel@tonic-gate 		} else {
11457c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
11467c478bd9Sstevel@tonic-gate 			    "tar: F requires a file name.\n"));
11477c478bd9Sstevel@tonic-gate 			usage();
11487c478bd9Sstevel@tonic-gate 		}
11497c478bd9Sstevel@tonic-gate 	}
11507c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 	/*
11537c478bd9Sstevel@tonic-gate 	 * Save the original directory before it gets
11547c478bd9Sstevel@tonic-gate 	 * changed.
11557c478bd9Sstevel@tonic-gate 	 */
11567c478bd9Sstevel@tonic-gate 	if (getcwd(origdir, (PATH_MAX+1)) == NULL) {
11577c478bd9Sstevel@tonic-gate 		vperror(0, gettext("A parent directory cannot be read"));
11587c478bd9Sstevel@tonic-gate 		exit(1);
11597c478bd9Sstevel@tonic-gate 	}
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 	(void) strcpy(wdir, origdir);
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	while ((*argv || fp || ff) && !term) {
11647c478bd9Sstevel@tonic-gate 		if (fp || (strcmp(*argv, "-I") == 0)) {
11657c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
11667c478bd9Sstevel@tonic-gate 			if (Fileflag) {
11677c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
11687c478bd9Sstevel@tonic-gate 				"tar: only one of I or F.\n"));
11697c478bd9Sstevel@tonic-gate 				usage();
11707c478bd9Sstevel@tonic-gate 			}
11717c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
11727c478bd9Sstevel@tonic-gate 			if (fp == NULL) {
11737c478bd9Sstevel@tonic-gate 				if (*++argv == NULL)
11747c478bd9Sstevel@tonic-gate 					fatal(gettext(
11757c478bd9Sstevel@tonic-gate 					    "missing file name for -I flag."));
11767c478bd9Sstevel@tonic-gate 				else if ((fp = fopen(*argv++, "r")) == NULL)
11777c478bd9Sstevel@tonic-gate 					vperror(0, "%s", argv[-1]);
11787c478bd9Sstevel@tonic-gate 				continue;
11797c478bd9Sstevel@tonic-gate 			} else if ((fgets(file, PATH_MAX-1, fp)) == NULL) {
11807c478bd9Sstevel@tonic-gate 				(void) fclose(fp);
11817c478bd9Sstevel@tonic-gate 				fp = NULL;
11827c478bd9Sstevel@tonic-gate 				continue;
11837c478bd9Sstevel@tonic-gate 			} else {
11847c478bd9Sstevel@tonic-gate 				cp = cp2 = file;
11857c478bd9Sstevel@tonic-gate 				if ((p = strchr(cp2, '\n')))
11867c478bd9Sstevel@tonic-gate 					*p = 0;
11877c478bd9Sstevel@tonic-gate 			}
11887c478bd9Sstevel@tonic-gate 		} else if ((strcmp(*argv, "-C") == 0) && argv[1]) {
11897c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
11907c478bd9Sstevel@tonic-gate 			if (Fileflag) {
11917c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
11927c478bd9Sstevel@tonic-gate 				"tar: only one of F or C\n"));
11937c478bd9Sstevel@tonic-gate 				usage();
11947c478bd9Sstevel@tonic-gate 			}
11957c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 			if (chdir(*++argv) < 0)
11987c478bd9Sstevel@tonic-gate 				vperror(0, gettext(
11997c478bd9Sstevel@tonic-gate 				"can't change directories to %s"), *argv);
12007c478bd9Sstevel@tonic-gate 			else
12017c478bd9Sstevel@tonic-gate 				(void) getcwd(wdir, (sizeof (wdir)));
12027c478bd9Sstevel@tonic-gate 			argv++;
12037c478bd9Sstevel@tonic-gate 			continue;
12047c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
12057c478bd9Sstevel@tonic-gate 		} else if (Fileflag && (ff != NULL)) {
12067c478bd9Sstevel@tonic-gate 			if ((fgets(file, PATH_MAX-1, ff)) == NULL) {
12077c478bd9Sstevel@tonic-gate 				(void) fclose(ff);
12087c478bd9Sstevel@tonic-gate 				ff = NULL;
12097c478bd9Sstevel@tonic-gate 				continue;
12107c478bd9Sstevel@tonic-gate 			} else {
12117c478bd9Sstevel@tonic-gate 				cp = cp2 = file;
12127c478bd9Sstevel@tonic-gate 				if (p = strchr(cp2, '\n'))
12137c478bd9Sstevel@tonic-gate 					*p = 0;
12147c478bd9Sstevel@tonic-gate 			}
12157c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
12167c478bd9Sstevel@tonic-gate 		} else
12177c478bd9Sstevel@tonic-gate 			cp = cp2 = strcpy(file, *argv++);
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 		/*
12207c478bd9Sstevel@tonic-gate 		 * point cp2 to the last '/' in file, but not
12217c478bd9Sstevel@tonic-gate 		 * to a trailing '/'
12227c478bd9Sstevel@tonic-gate 		 */
12237c478bd9Sstevel@tonic-gate 		for (; *cp; cp++) {
12247c478bd9Sstevel@tonic-gate 			if (*cp == '/') {
12257c478bd9Sstevel@tonic-gate 				while (*(cp+1) == '/') {
12267c478bd9Sstevel@tonic-gate 					++cp;
12277c478bd9Sstevel@tonic-gate 				}
12287c478bd9Sstevel@tonic-gate 				if (*(cp+1) != '\0') {
12297c478bd9Sstevel@tonic-gate 					/* not trailing slash */
12307c478bd9Sstevel@tonic-gate 					cp2 = cp;
12317c478bd9Sstevel@tonic-gate 				}
12327c478bd9Sstevel@tonic-gate 			}
12337c478bd9Sstevel@tonic-gate 		}
12347c478bd9Sstevel@tonic-gate 		if (cp2 != file) {
12357c478bd9Sstevel@tonic-gate 			*cp2 = '\0';
12367c478bd9Sstevel@tonic-gate 			if (chdir(file) < 0) {
12377c478bd9Sstevel@tonic-gate 				vperror(0, gettext(
12387c478bd9Sstevel@tonic-gate 				"can't change directories to %s"), file);
12397c478bd9Sstevel@tonic-gate 				continue;
12407c478bd9Sstevel@tonic-gate 			}
12417c478bd9Sstevel@tonic-gate 			*cp2 = '/';
12427c478bd9Sstevel@tonic-gate 			cp2++;
12437c478bd9Sstevel@tonic-gate 		}
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 		parent = getcwd(tempdir, (sizeof (tempdir)));
12467c478bd9Sstevel@tonic-gate 		archtype = putfile(file, cp2, parent, NORMAL_FILE,
12477c478bd9Sstevel@tonic-gate 		    LEV0, SYMLINK_LEV0);
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
12507c478bd9Sstevel@tonic-gate 		if (!exitflag) {
12517c478bd9Sstevel@tonic-gate 			if (atflag && archtype == PUT_NOTAS_LINK) {
12527c478bd9Sstevel@tonic-gate 				xattrs_put(file, cp2, parent);
12537c478bd9Sstevel@tonic-gate 			}
12547c478bd9Sstevel@tonic-gate 		}
12557c478bd9Sstevel@tonic-gate #endif
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 		if (chdir(origdir) < 0)
12587c478bd9Sstevel@tonic-gate 			vperror(0, gettext("cannot change back?: %s"), origdir);
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 		if (exitflag) {
12617c478bd9Sstevel@tonic-gate 			/*
12627c478bd9Sstevel@tonic-gate 			 * If e function modifier has been specified
12637c478bd9Sstevel@tonic-gate 			 * write the files (that are listed before the
12647c478bd9Sstevel@tonic-gate 			 * file causing the error) to tape.  exitflag is
12657c478bd9Sstevel@tonic-gate 			 * used because only some of the error conditions
12667c478bd9Sstevel@tonic-gate 			 * in putfile() recognize the e function modifier.
12677c478bd9Sstevel@tonic-gate 			 */
12687c478bd9Sstevel@tonic-gate 			break;
12697c478bd9Sstevel@tonic-gate 		}
12707c478bd9Sstevel@tonic-gate 	}
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 	putempty((blkcnt_t)2);
12737c478bd9Sstevel@tonic-gate 	flushtape();
12747c478bd9Sstevel@tonic-gate 	closevol();	/* SP-1 */
12757c478bd9Sstevel@tonic-gate 	if (linkerrok == 1)
12767c478bd9Sstevel@tonic-gate 		for (; ihead != NULL; ihead = ihead->nextp) {
12777c478bd9Sstevel@tonic-gate 			if (ihead->count == 0)
12787c478bd9Sstevel@tonic-gate 				continue;
12797c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
12807c478bd9Sstevel@tonic-gate 			"tar: missing links to %s\n"), ihead->pathname);
12817c478bd9Sstevel@tonic-gate 			if (errflag)
12827c478bd9Sstevel@tonic-gate 				done(1);
12837c478bd9Sstevel@tonic-gate 			else
12847c478bd9Sstevel@tonic-gate 				Errflg = 1;
12857c478bd9Sstevel@tonic-gate 		}
12867c478bd9Sstevel@tonic-gate }
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate /*
12907c478bd9Sstevel@tonic-gate  * endtape - check for tape at end
12917c478bd9Sstevel@tonic-gate  *
12927c478bd9Sstevel@tonic-gate  *	endtape checks the entry in dblock.dbuf to see if its the
12937c478bd9Sstevel@tonic-gate  *	special EOT entry.  Endtape is usually called after getdir().
12947c478bd9Sstevel@tonic-gate  *
12957c478bd9Sstevel@tonic-gate  *	endtape used to call backtape; it no longer does, he who
12967c478bd9Sstevel@tonic-gate  *	wants it backed up must call backtape himself
12977c478bd9Sstevel@tonic-gate  *	RETURNS:	0 if not EOT, tape position unaffected
12987c478bd9Sstevel@tonic-gate  *			1 if	 EOT, tape position unaffected
12997c478bd9Sstevel@tonic-gate  */
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate static int
13027c478bd9Sstevel@tonic-gate endtape(void)
13037c478bd9Sstevel@tonic-gate {
13047c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.name[0] == '\0') {	/* null header = EOT */
13057c478bd9Sstevel@tonic-gate 		return (1);
13067c478bd9Sstevel@tonic-gate 	} else
13077c478bd9Sstevel@tonic-gate 		return (0);
13087c478bd9Sstevel@tonic-gate }
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate /*
13117c478bd9Sstevel@tonic-gate  *	getdir - get directory entry from tar tape
13127c478bd9Sstevel@tonic-gate  *
13137c478bd9Sstevel@tonic-gate  *	getdir reads the next tarblock off the tape and cracks
13147c478bd9Sstevel@tonic-gate  *	it as a directory. The checksum must match properly.
13157c478bd9Sstevel@tonic-gate  *
13167c478bd9Sstevel@tonic-gate  *	If tfile is non-null getdir writes the file name and mod date
13177c478bd9Sstevel@tonic-gate  *	to tfile.
13187c478bd9Sstevel@tonic-gate  */
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate static void
13217c478bd9Sstevel@tonic-gate getdir(void)
13227c478bd9Sstevel@tonic-gate {
13237c478bd9Sstevel@tonic-gate 	struct stat *sp;
13247c478bd9Sstevel@tonic-gate #ifdef EUC
13257c478bd9Sstevel@tonic-gate 	static int warn_chksum_sign = 0;
13267c478bd9Sstevel@tonic-gate #endif /* EUC */
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate top:
13297c478bd9Sstevel@tonic-gate 	readtape((char *)&dblock);
13307c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.name[0] == '\0')
13317c478bd9Sstevel@tonic-gate 		return;
13327c478bd9Sstevel@tonic-gate 	sp = &stbuf;
13337c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.mode, "%8lo", &Gen.g_mode);
13347c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.uid, "%8lo", (ulong_t *)&Gen.g_uid);
13357c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.gid, "%8lo", (ulong_t *)&Gen.g_gid);
13367c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.size, "%12" FMT_off_t_o, &Gen.g_filesz);
13377c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.mtime, "%12lo", (ulong_t *)&Gen.g_mtime);
13387c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.chksum, "%8o", &Gen.g_cksum);
13397c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.devmajor, "%8lo", &Gen.g_devmajor);
13407c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.devminor, "%8lo", &Gen.g_devminor);
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 	is_posix = (strcmp(dblock.dbuf.magic, magic_type) == 0);
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 	sp->st_mode = Gen.g_mode;
13457c478bd9Sstevel@tonic-gate 	if (is_posix && (sp->st_mode & S_IFMT) == 0)
13467c478bd9Sstevel@tonic-gate 		switch (dblock.dbuf.typeflag) {
13477c478bd9Sstevel@tonic-gate 		case '0': case 0: case _XATTR_HDRTYPE:
13487c478bd9Sstevel@tonic-gate 			sp->st_mode |= S_IFREG;
13497c478bd9Sstevel@tonic-gate 			break;
13507c478bd9Sstevel@tonic-gate 		case '1':	/* hard link */
13517c478bd9Sstevel@tonic-gate 			break;
13527c478bd9Sstevel@tonic-gate 		case '2':
13537c478bd9Sstevel@tonic-gate 			sp->st_mode |= S_IFLNK;
13547c478bd9Sstevel@tonic-gate 			break;
13557c478bd9Sstevel@tonic-gate 		case '3':
13567c478bd9Sstevel@tonic-gate 			sp->st_mode |= S_IFCHR;
13577c478bd9Sstevel@tonic-gate 			break;
13587c478bd9Sstevel@tonic-gate 		case '4':
13597c478bd9Sstevel@tonic-gate 			sp->st_mode |= S_IFBLK;
13607c478bd9Sstevel@tonic-gate 			break;
13617c478bd9Sstevel@tonic-gate 		case '5':
13627c478bd9Sstevel@tonic-gate 			sp->st_mode |= S_IFDIR;
13637c478bd9Sstevel@tonic-gate 			break;
13647c478bd9Sstevel@tonic-gate 		case '6':
13657c478bd9Sstevel@tonic-gate 			sp->st_mode |= S_IFIFO;
13667c478bd9Sstevel@tonic-gate 			break;
13677c478bd9Sstevel@tonic-gate 		default:
13687c478bd9Sstevel@tonic-gate 			if (convtoreg(Gen.g_filesz))
13697c478bd9Sstevel@tonic-gate 				sp->st_mode |= S_IFREG;
13707c478bd9Sstevel@tonic-gate 			break;
13717c478bd9Sstevel@tonic-gate 		}
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.typeflag == 'X')
13747c478bd9Sstevel@tonic-gate 		Xhdrflag = 1;	/* Currently processing extended header */
13757c478bd9Sstevel@tonic-gate 	else
13767c478bd9Sstevel@tonic-gate 		Xhdrflag = 0;
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate 	sp->st_uid = Gen.g_uid;
13797c478bd9Sstevel@tonic-gate 	sp->st_gid = Gen.g_gid;
13807c478bd9Sstevel@tonic-gate 	sp->st_size = Gen.g_filesz;
13817c478bd9Sstevel@tonic-gate 	sp->st_mtime = Gen.g_mtime;
13827c478bd9Sstevel@tonic-gate 	chksum = Gen.g_cksum;
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.extno != '\0') {	/* split file? */
13857c478bd9Sstevel@tonic-gate 		extno = dblock.dbuf.extno;
13867c478bd9Sstevel@tonic-gate 		extsize = Gen.g_filesz;
13877c478bd9Sstevel@tonic-gate 		extotal = dblock.dbuf.extotal;
13887c478bd9Sstevel@tonic-gate 	} else {
13897c478bd9Sstevel@tonic-gate 		extno = 0;	/* tell others file not split */
13907c478bd9Sstevel@tonic-gate 		extsize = 0;
13917c478bd9Sstevel@tonic-gate 		extotal = 0;
13927c478bd9Sstevel@tonic-gate 	}
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate #ifdef	EUC
13957c478bd9Sstevel@tonic-gate 	if (chksum != checksum(&dblock)) {
13967c478bd9Sstevel@tonic-gate 		if (chksum != checksum_signed(&dblock)) {
13977c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
13987c478bd9Sstevel@tonic-gate 			    "tar: directory checksum error\n"));
13997c478bd9Sstevel@tonic-gate 			if (iflag)
14007c478bd9Sstevel@tonic-gate 				goto top;
14017c478bd9Sstevel@tonic-gate 			done(2);
14027c478bd9Sstevel@tonic-gate 		} else {
14037c478bd9Sstevel@tonic-gate 			if (! warn_chksum_sign) {
14047c478bd9Sstevel@tonic-gate 				warn_chksum_sign = 1;
14057c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
14067c478bd9Sstevel@tonic-gate 			"tar: warning: tar file made with signed checksum\n"));
14077c478bd9Sstevel@tonic-gate 			}
14087c478bd9Sstevel@tonic-gate 		}
14097c478bd9Sstevel@tonic-gate 	}
14107c478bd9Sstevel@tonic-gate #else
14117c478bd9Sstevel@tonic-gate 	if (chksum != checksum(&dblock)) {
14127c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
14137c478bd9Sstevel@tonic-gate 		"tar: directory checksum error\n"));
14147c478bd9Sstevel@tonic-gate 		if (iflag)
14157c478bd9Sstevel@tonic-gate 			goto top;
14167c478bd9Sstevel@tonic-gate 		done(2);
14177c478bd9Sstevel@tonic-gate 	}
14187c478bd9Sstevel@tonic-gate #endif	/* EUC */
14197c478bd9Sstevel@tonic-gate 	if (tfile != NULL && Xhdrflag == 0) {
14207c478bd9Sstevel@tonic-gate 		/*
14217c478bd9Sstevel@tonic-gate 		 * If an extended header is present, then time is available
14227c478bd9Sstevel@tonic-gate 		 * in nanoseconds in the extended header data, so set it.
14237c478bd9Sstevel@tonic-gate 		 * Otherwise, give an invalid value so that checkupdate will
14247c478bd9Sstevel@tonic-gate 		 * not test beyond seconds.
14257c478bd9Sstevel@tonic-gate 		 */
14267c478bd9Sstevel@tonic-gate 		if ((xhdr_flgs & _X_MTIME))
14277c478bd9Sstevel@tonic-gate 			sp->st_mtim.tv_nsec = Xtarhdr.x_mtime.tv_nsec;
14287c478bd9Sstevel@tonic-gate 		else
14297c478bd9Sstevel@tonic-gate 			sp->st_mtim.tv_nsec = -1;
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_PATH)
14327c478bd9Sstevel@tonic-gate 			(void) fprintf(tfile, "%s %10ld.%9.9ld\n",
14337c478bd9Sstevel@tonic-gate 			    Xtarhdr.x_path, sp->st_mtim.tv_sec,
14347c478bd9Sstevel@tonic-gate 			    sp->st_mtim.tv_nsec);
14357c478bd9Sstevel@tonic-gate 		else
14367c478bd9Sstevel@tonic-gate 			(void) fprintf(tfile, "%.*s %10ld.%9.9ld\n",
14377c478bd9Sstevel@tonic-gate 			    NAMSIZ, dblock.dbuf.name, sp->st_mtim.tv_sec,
14387c478bd9Sstevel@tonic-gate 			    sp->st_mtim.tv_nsec);
14397c478bd9Sstevel@tonic-gate 	}
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
14427c478bd9Sstevel@tonic-gate 	Hiddendir = 0;
14437c478bd9Sstevel@tonic-gate 	if (xattrp && dblock.dbuf.typeflag == _XATTR_HDRTYPE) {
14447c478bd9Sstevel@tonic-gate 		if (xattrbadhead) {
14457c478bd9Sstevel@tonic-gate 			free(xattrhead);
14467c478bd9Sstevel@tonic-gate 			xattrp = NULL;
14477c478bd9Sstevel@tonic-gate 			xattr_linkp = NULL;
14487c478bd9Sstevel@tonic-gate 			xattrhead = NULL;
14497c478bd9Sstevel@tonic-gate 		} else {
14507c478bd9Sstevel@tonic-gate 			if (xattraname[0] == '.' && xattraname[1] == '\0' &&
14517c478bd9Sstevel@tonic-gate 			    xattrp->h_typeflag == '5') {
14527c478bd9Sstevel@tonic-gate 				Hiddendir = 1;
14537c478bd9Sstevel@tonic-gate 				sp->st_mode =
1454d2443e76Smarks 				    (S_IFDIR | (sp->st_mode & POSIXMODES));
14557c478bd9Sstevel@tonic-gate 			}
14567c478bd9Sstevel@tonic-gate 			dblock.dbuf.typeflag = xattrp->h_typeflag;
14577c478bd9Sstevel@tonic-gate 		}
14587c478bd9Sstevel@tonic-gate 	}
14597c478bd9Sstevel@tonic-gate #endif
14607c478bd9Sstevel@tonic-gate }
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate /*
14647c478bd9Sstevel@tonic-gate  *	passtape - skip over a file on the tape
14657c478bd9Sstevel@tonic-gate  *
14667c478bd9Sstevel@tonic-gate  *	passtape skips over the next data file on the tape.
14677c478bd9Sstevel@tonic-gate  *	The tape directory entry must be in dblock.dbuf. This
14687c478bd9Sstevel@tonic-gate  *	routine just eats the number of blocks computed from the
14697c478bd9Sstevel@tonic-gate  *	directory size entry; the tape must be (logically) positioned
14707c478bd9Sstevel@tonic-gate  *	right after thee directory info.
14717c478bd9Sstevel@tonic-gate  */
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate static void
14747c478bd9Sstevel@tonic-gate passtape(void)
14757c478bd9Sstevel@tonic-gate {
14767c478bd9Sstevel@tonic-gate 	blkcnt_t blocks;
14777c478bd9Sstevel@tonic-gate 	char buf[TBLOCK];
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 	/*
14807c478bd9Sstevel@tonic-gate 	 * Types link(1), sym-link(2), char special(3), blk special(4),
14817c478bd9Sstevel@tonic-gate 	 *  directory(5), and FIFO(6) do not have data blocks associated
14827c478bd9Sstevel@tonic-gate 	 *  with them so just skip reading the data block.
14837c478bd9Sstevel@tonic-gate 	 */
14847c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.typeflag == '1' || dblock.dbuf.typeflag == '2' ||
14857c478bd9Sstevel@tonic-gate 	    dblock.dbuf.typeflag == '3' || dblock.dbuf.typeflag == '4' ||
14867c478bd9Sstevel@tonic-gate 	    dblock.dbuf.typeflag == '5' || dblock.dbuf.typeflag == '6')
14877c478bd9Sstevel@tonic-gate 		return;
14887c478bd9Sstevel@tonic-gate 	blocks = TBLOCKS(stbuf.st_size);
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate 	/* if operating on disk, seek instead of reading */
14917c478bd9Sstevel@tonic-gate 	if (NotTape)
14927c478bd9Sstevel@tonic-gate 		seekdisk(blocks);
14937c478bd9Sstevel@tonic-gate 	else
14947c478bd9Sstevel@tonic-gate 		while (blocks-- > 0)
14957c478bd9Sstevel@tonic-gate 			readtape(buf);
14967c478bd9Sstevel@tonic-gate }
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate static int
15007c478bd9Sstevel@tonic-gate putfile(char *longname, char *shortname, char *parent,
15017c478bd9Sstevel@tonic-gate     int filetype, int lev, int symlink_lev)
15027c478bd9Sstevel@tonic-gate {
15037c478bd9Sstevel@tonic-gate 	int infile = -1;	/* deliberately invalid */
15047c478bd9Sstevel@tonic-gate 	blkcnt_t blocks;
15057c478bd9Sstevel@tonic-gate 	char buf[PATH_MAX + 2];	/* Add trailing slash and null */
15067c478bd9Sstevel@tonic-gate 	char *bigbuf;
15077c478bd9Sstevel@tonic-gate 	int	maxread;
15087c478bd9Sstevel@tonic-gate 	int	hint;		/* amount to write to get "in sync" */
15097c478bd9Sstevel@tonic-gate 	char filetmp[PATH_MAX + 1];
15107c478bd9Sstevel@tonic-gate 	char *cp;
15117c478bd9Sstevel@tonic-gate 	char *name;
15127c478bd9Sstevel@tonic-gate 	struct dirent *dp;
15137c478bd9Sstevel@tonic-gate 	DIR *dirp;
15147c478bd9Sstevel@tonic-gate 	int i;
15157c478bd9Sstevel@tonic-gate 	long l;
15167c478bd9Sstevel@tonic-gate 	int split;
15177c478bd9Sstevel@tonic-gate 	int dirfd = -1;
15187c478bd9Sstevel@tonic-gate 	int rc = PUT_NOTAS_LINK;
15197c478bd9Sstevel@tonic-gate 	int archtype = 0;
15207c478bd9Sstevel@tonic-gate 	char newparent[PATH_MAX + MAXNAMLEN + 1];
15217c478bd9Sstevel@tonic-gate 	char *prefix = "";
15227c478bd9Sstevel@tonic-gate 	char *tmpbuf;
15237c478bd9Sstevel@tonic-gate 	char goodbuf[PRESIZ + 2];
15247c478bd9Sstevel@tonic-gate 	char junkbuf[MAXNAM+1];
15257c478bd9Sstevel@tonic-gate 	char *lastslash;
15267c478bd9Sstevel@tonic-gate 	int j;
15277c478bd9Sstevel@tonic-gate 	struct stat sbuf;
15287c478bd9Sstevel@tonic-gate 	int readlink_max;
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 	(void) memset(goodbuf, '\0', sizeof (goodbuf));
15317c478bd9Sstevel@tonic-gate 	(void) memset(junkbuf, '\0', sizeof (junkbuf));
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 	xhdr_flgs = 0;
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 	if (filetype == XATTR_FILE) {
15367c478bd9Sstevel@tonic-gate 		dirfd = attropen(get_component(longname), ".", O_RDONLY);
15377c478bd9Sstevel@tonic-gate 	} else {
15387c478bd9Sstevel@tonic-gate 		dirfd = open(".", O_RDONLY);
15397c478bd9Sstevel@tonic-gate 	}
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	if (dirfd == -1) {
15427c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
15437c478bd9Sstevel@tonic-gate 		    "tar: unable to open%sdirectory %s\n"),
15447c478bd9Sstevel@tonic-gate 		    (filetype == XATTR_FILE) ? gettext(" attribute ") : " ",
15457c478bd9Sstevel@tonic-gate 		    (filetype == XATTR_FILE) ? longname : parent);
15467c478bd9Sstevel@tonic-gate 		goto out;
15477c478bd9Sstevel@tonic-gate 	}
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate 	if (filetype == XATTR_FILE) {
15507c478bd9Sstevel@tonic-gate 		if (fchdir(dirfd) < 0) {
15517c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
15527c478bd9Sstevel@tonic-gate 			    "tar: unable to fchdir into attribute directory"
15537c478bd9Sstevel@tonic-gate 			    " of file %s\n"), longname);
15547c478bd9Sstevel@tonic-gate 			goto out;
15557c478bd9Sstevel@tonic-gate 		}
15567c478bd9Sstevel@tonic-gate 	}
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate 	if (lev > MAXLEV) {
15597c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
15607c478bd9Sstevel@tonic-gate 		    gettext("tar: directory nesting too deep, %s not dumped\n"),
15617c478bd9Sstevel@tonic-gate 		    longname);
15627c478bd9Sstevel@tonic-gate 		goto out;
15637c478bd9Sstevel@tonic-gate 	}
15647c478bd9Sstevel@tonic-gate 
15657c478bd9Sstevel@tonic-gate 	if (getstat(dirfd, longname, shortname))
15667c478bd9Sstevel@tonic-gate 		goto out;
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 	if (hflag) {
15697c478bd9Sstevel@tonic-gate 		/*
15707c478bd9Sstevel@tonic-gate 		 * Catch nesting where a file is a symlink to its directory.
15717c478bd9Sstevel@tonic-gate 		 */
15727c478bd9Sstevel@tonic-gate 		j = fstatat(dirfd, shortname, &sbuf, AT_SYMLINK_NOFOLLOW);
15737c478bd9Sstevel@tonic-gate 		if (S_ISLNK(sbuf.st_mode)) {
15747c478bd9Sstevel@tonic-gate 			if (symlink_lev++ >= MAXSYMLINKS) {
15757c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
15767c478bd9Sstevel@tonic-gate 				    "tar: %s: Number of symbolic links "
15777c478bd9Sstevel@tonic-gate 				    "encountered during path name traversal "
15787c478bd9Sstevel@tonic-gate 				    "exceeds MAXSYMLINKS\n"), longname);
15797c478bd9Sstevel@tonic-gate 				Errflg = 1;
15807c478bd9Sstevel@tonic-gate 				goto out;
15817c478bd9Sstevel@tonic-gate 			}
15827c478bd9Sstevel@tonic-gate 		}
15837c478bd9Sstevel@tonic-gate 	}
15847c478bd9Sstevel@tonic-gate 
15857c478bd9Sstevel@tonic-gate 	/*
15867c478bd9Sstevel@tonic-gate 	 * Check if the input file is the same as the tar file we
15877c478bd9Sstevel@tonic-gate 	 * are creating
15887c478bd9Sstevel@tonic-gate 	 */
15897c478bd9Sstevel@tonic-gate 	if ((mt_ino == stbuf.st_ino) && (mt_dev == stbuf.st_dev)) {
15907c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
15917c478bd9Sstevel@tonic-gate 		    "tar: %s same as archive file\n"), longname);
15927c478bd9Sstevel@tonic-gate 		Errflg = 1;
15937c478bd9Sstevel@tonic-gate 		goto out;
15947c478bd9Sstevel@tonic-gate 	}
15957c478bd9Sstevel@tonic-gate 	/*
15967c478bd9Sstevel@tonic-gate 	 * Check size limit - we can't archive files that
15977c478bd9Sstevel@tonic-gate 	 * exceed TAR_OFFSET_MAX bytes because of header
15987c478bd9Sstevel@tonic-gate 	 * limitations. Exclude file types that set
15997c478bd9Sstevel@tonic-gate 	 * st_size to zero below because they take no
16007c478bd9Sstevel@tonic-gate 	 * archive space to represent contents.
16017c478bd9Sstevel@tonic-gate 	 */
16027c478bd9Sstevel@tonic-gate 	if ((stbuf.st_size > (off_t)TAR_OFFSET_MAX) &&
16037c478bd9Sstevel@tonic-gate 	    !S_ISDIR(stbuf.st_mode) &&
16047c478bd9Sstevel@tonic-gate 	    !S_ISCHR(stbuf.st_mode) &&
16057c478bd9Sstevel@tonic-gate 	    !S_ISBLK(stbuf.st_mode) &&
16067c478bd9Sstevel@tonic-gate 	    (Eflag == 0)) {
16077c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
16087c478bd9Sstevel@tonic-gate 		    "tar: %s too large to archive.  "
16097c478bd9Sstevel@tonic-gate 		    "Use E function modifier.\n"), longname);
16107c478bd9Sstevel@tonic-gate 		if (errflag)
16117c478bd9Sstevel@tonic-gate 			exitflag = 1;
16127c478bd9Sstevel@tonic-gate 		Errflg = 1;
16137c478bd9Sstevel@tonic-gate 		goto out;
16147c478bd9Sstevel@tonic-gate 	}
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate 	if (tfile != NULL && checkupdate(longname) == 0) {
16177c478bd9Sstevel@tonic-gate 		goto out;
16187c478bd9Sstevel@tonic-gate 	}
16197c478bd9Sstevel@tonic-gate 	if (checkw('r', longname) == 0) {
16207c478bd9Sstevel@tonic-gate 		goto out;
16217c478bd9Sstevel@tonic-gate 	}
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 	if (Fflag && checkf(shortname, stbuf.st_mode, Fflag) == 0)
16247c478bd9Sstevel@tonic-gate 		goto out;
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate 	if (Xflag) {
16277c478bd9Sstevel@tonic-gate 		if (is_in_table(exclude_tbl, longname)) {
16287c478bd9Sstevel@tonic-gate 			if (vflag) {
16297c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext(
16307c478bd9Sstevel@tonic-gate 				    "a %s excluded\n"), longname);
16317c478bd9Sstevel@tonic-gate 			}
16327c478bd9Sstevel@tonic-gate 			goto out;
16337c478bd9Sstevel@tonic-gate 		}
16347c478bd9Sstevel@tonic-gate 	}
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate 	/*
16377c478bd9Sstevel@tonic-gate 	 * If the length of the fullname is greater than MAXNAM,
16387c478bd9Sstevel@tonic-gate 	 * print out a message and return (unless extended headers are used,
16397c478bd9Sstevel@tonic-gate 	 * in which case fullname is limited to PATH_MAX).
16407c478bd9Sstevel@tonic-gate 	 */
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate 	if ((((split = (int)strlen(longname)) > MAXNAM) && (Eflag == 0)) ||
16437c478bd9Sstevel@tonic-gate 	    (split > PATH_MAX)) {
16447c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
16457c478bd9Sstevel@tonic-gate 		    "tar: %s: file name too long\n"), longname);
16467c478bd9Sstevel@tonic-gate 		if (errflag)
16477c478bd9Sstevel@tonic-gate 			exitflag = 1;
16487c478bd9Sstevel@tonic-gate 		Errflg = 1;
16497c478bd9Sstevel@tonic-gate 		goto out;
16507c478bd9Sstevel@tonic-gate 	}
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 	/*
16537c478bd9Sstevel@tonic-gate 	 * We split the fullname into prefix and name components if any one
16547c478bd9Sstevel@tonic-gate 	 * of three conditions holds:
16557c478bd9Sstevel@tonic-gate 	 *	-- the length of the fullname exceeds NAMSIZ,
16567c478bd9Sstevel@tonic-gate 	 *	-- the length of the fullname equals NAMSIZ, and the shortname
16577c478bd9Sstevel@tonic-gate 	 *	   is less than NAMSIZ, (splitting in this case preserves
16587c478bd9Sstevel@tonic-gate 	 *	   compatibility with 5.6 and 5.5.1 tar), or
16597c478bd9Sstevel@tonic-gate 	 * 	-- the length of the fullname equals NAMSIZ, the file is a
16607c478bd9Sstevel@tonic-gate 	 *	   directory and we are not in POSIX-conformant mode (where
16617c478bd9Sstevel@tonic-gate 	 *	   trailing slashes are removed from directories).
16627c478bd9Sstevel@tonic-gate 	 */
16637c478bd9Sstevel@tonic-gate 	if ((split > NAMSIZ) ||
16647c478bd9Sstevel@tonic-gate 	    (split == NAMSIZ && strlen(shortname) < NAMSIZ) ||
16654bc0a2efScasper 	    (split == NAMSIZ && S_ISDIR(stbuf.st_mode) && !Pflag)) {
16667c478bd9Sstevel@tonic-gate 		/*
16677c478bd9Sstevel@tonic-gate 		 * Since path is limited to PRESIZ characters, look for the
16687c478bd9Sstevel@tonic-gate 		 * last slash within PRESIZ + 1 characters only.
16697c478bd9Sstevel@tonic-gate 		 */
16707c478bd9Sstevel@tonic-gate 		(void) strncpy(&goodbuf[0], longname, min(split, PRESIZ + 1));
16717c478bd9Sstevel@tonic-gate 		tmpbuf = goodbuf;
16727c478bd9Sstevel@tonic-gate 		lastslash = strrchr(tmpbuf, '/');
16737c478bd9Sstevel@tonic-gate 		if (lastslash == NULL) {
16747c478bd9Sstevel@tonic-gate 			i = split;		/* Length of name */
16757c478bd9Sstevel@tonic-gate 			j = 0;			/* Length of prefix */
16767c478bd9Sstevel@tonic-gate 			goodbuf[0] = '\0';
16777c478bd9Sstevel@tonic-gate 		} else {
16787c478bd9Sstevel@tonic-gate 			*lastslash = '\0';	/* Terminate the prefix */
16797c478bd9Sstevel@tonic-gate 			j = strlen(tmpbuf);
16807c478bd9Sstevel@tonic-gate 			i = split - j - 1;
16817c478bd9Sstevel@tonic-gate 		}
16827c478bd9Sstevel@tonic-gate 		/*
16837c478bd9Sstevel@tonic-gate 		 * If the filename is greater than NAMSIZ we can't
16847c478bd9Sstevel@tonic-gate 		 * archive the file unless we are using extended headers.
16857c478bd9Sstevel@tonic-gate 		 */
16864bc0a2efScasper 		if ((i > NAMSIZ) || (i == NAMSIZ && S_ISDIR(stbuf.st_mode) &&
16877c478bd9Sstevel@tonic-gate 		    !Pflag)) {
16887c478bd9Sstevel@tonic-gate 			/* Determine which (filename or path) is too long. */
16897c478bd9Sstevel@tonic-gate 			lastslash = strrchr(longname, '/');
16907c478bd9Sstevel@tonic-gate 			if (lastslash != NULL)
16917c478bd9Sstevel@tonic-gate 				i = strlen(lastslash + 1);
16927c478bd9Sstevel@tonic-gate 			if (Eflag > 0) {
16937c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_PATH;
16947c478bd9Sstevel@tonic-gate 				Xtarhdr.x_path = longname;
16957c478bd9Sstevel@tonic-gate 				if (i <= NAMSIZ)
16967c478bd9Sstevel@tonic-gate 					(void) strcpy(junkbuf, lastslash + 1);
16977c478bd9Sstevel@tonic-gate 				else
16987c478bd9Sstevel@tonic-gate 					(void) sprintf(junkbuf, "%llu",
16997c478bd9Sstevel@tonic-gate 					    xhdr_count + 1);
17007c478bd9Sstevel@tonic-gate 				if (split - i - 1 > PRESIZ)
17017c478bd9Sstevel@tonic-gate 					(void) strcpy(goodbuf, xhdr_dirname);
17027c478bd9Sstevel@tonic-gate 			} else {
17037c478bd9Sstevel@tonic-gate 				if ((i > NAMSIZ) || (i == NAMSIZ &&
17044bc0a2efScasper 				    S_ISDIR(stbuf.st_mode) && !Pflag))
17057c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
17067c478bd9Sstevel@tonic-gate 					    "tar: %s: filename is greater than "
17077c478bd9Sstevel@tonic-gate 					    "%d\n"), lastslash == NULL ?
17087c478bd9Sstevel@tonic-gate 					    longname : lastslash + 1, NAMSIZ);
17097c478bd9Sstevel@tonic-gate 				else
17107c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
17117c478bd9Sstevel@tonic-gate 					    "tar: %s: prefix is greater than %d"
17127c478bd9Sstevel@tonic-gate 					    "\n"), longname, PRESIZ);
17137c478bd9Sstevel@tonic-gate 				if (errflag)
17147c478bd9Sstevel@tonic-gate 					exitflag = 1;
17157c478bd9Sstevel@tonic-gate 				Errflg = 1;
17167c478bd9Sstevel@tonic-gate 				goto out;
17177c478bd9Sstevel@tonic-gate 			}
17187c478bd9Sstevel@tonic-gate 		} else
17197c478bd9Sstevel@tonic-gate 			(void) strncpy(&junkbuf[0], longname + j + 1,
17207c478bd9Sstevel@tonic-gate 			    strlen(longname + j + 1));
17217c478bd9Sstevel@tonic-gate 		name = junkbuf;
17227c478bd9Sstevel@tonic-gate 		prefix = goodbuf;
17237c478bd9Sstevel@tonic-gate 	} else {
17247c478bd9Sstevel@tonic-gate 		name = longname;
17257c478bd9Sstevel@tonic-gate 	}
17267c478bd9Sstevel@tonic-gate 	if (Aflag) {
17277c478bd9Sstevel@tonic-gate 		if ((prefix != NULL) && (*prefix != '\0'))
17287c478bd9Sstevel@tonic-gate 			while (*prefix == '/')
17297c478bd9Sstevel@tonic-gate 				++prefix;
17307c478bd9Sstevel@tonic-gate 		else
17317c478bd9Sstevel@tonic-gate 			while (*name == '/')
17327c478bd9Sstevel@tonic-gate 				++name;
17337c478bd9Sstevel@tonic-gate 	}
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 	switch (stbuf.st_mode & S_IFMT) {
17367c478bd9Sstevel@tonic-gate 	case S_IFDIR:
17377c478bd9Sstevel@tonic-gate 		stbuf.st_size = (off_t)0;
17387c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(stbuf.st_size);
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate 		if (filetype != XATTR_FILE && Hiddendir == 0) {
17417c478bd9Sstevel@tonic-gate 			i = 0;
17427c478bd9Sstevel@tonic-gate 			cp = buf;
17437c478bd9Sstevel@tonic-gate 			while ((*cp++ = longname[i++]))
17447c478bd9Sstevel@tonic-gate 				;
17457c478bd9Sstevel@tonic-gate 			*--cp = '/';
17467c478bd9Sstevel@tonic-gate 			*++cp = 0;
17477c478bd9Sstevel@tonic-gate 		}
17487c478bd9Sstevel@tonic-gate 		if (!oflag) {
17497c478bd9Sstevel@tonic-gate 			tomodes(&stbuf);
17507c478bd9Sstevel@tonic-gate 			if (build_dblock(name, tchar, '5', filetype,
17517c478bd9Sstevel@tonic-gate 			    &stbuf, stbuf.st_dev, prefix) != 0) {
17527c478bd9Sstevel@tonic-gate 				goto out;
17537c478bd9Sstevel@tonic-gate 			}
17547c478bd9Sstevel@tonic-gate 			if (!Pflag) {
17557c478bd9Sstevel@tonic-gate 				/*
17567c478bd9Sstevel@tonic-gate 				 * Old archives require a slash at the end
17577c478bd9Sstevel@tonic-gate 				 * of a directory name.
17587c478bd9Sstevel@tonic-gate 				 *
17597c478bd9Sstevel@tonic-gate 				 * XXX
17607c478bd9Sstevel@tonic-gate 				 * If directory name is too long, will
17617c478bd9Sstevel@tonic-gate 				 * slash overfill field?
17627c478bd9Sstevel@tonic-gate 				 */
17637c478bd9Sstevel@tonic-gate 				if (strlen(name) > (unsigned)NAMSIZ-1) {
17647c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
17657c478bd9Sstevel@tonic-gate 					    "tar: %s: filename is greater "
17667c478bd9Sstevel@tonic-gate 					    "than %d\n"), name, NAMSIZ);
17677c478bd9Sstevel@tonic-gate 					if (errflag)
17687c478bd9Sstevel@tonic-gate 						exitflag = 1;
17697c478bd9Sstevel@tonic-gate 					Errflg = 1;
17707c478bd9Sstevel@tonic-gate 					goto out;
17717c478bd9Sstevel@tonic-gate 				} else {
17727c478bd9Sstevel@tonic-gate 					if (strlen(name) == (NAMSIZ - 1)) {
17737c478bd9Sstevel@tonic-gate 						(void) memcpy(dblock.dbuf.name,
17747c478bd9Sstevel@tonic-gate 						    name, NAMSIZ);
17757c478bd9Sstevel@tonic-gate 						dblock.dbuf.name[NAMSIZ-1]
17767c478bd9Sstevel@tonic-gate 						    = '/';
17777c478bd9Sstevel@tonic-gate 					} else
17787c478bd9Sstevel@tonic-gate 						(void) sprintf(dblock.dbuf.name,
17797c478bd9Sstevel@tonic-gate 						    "%s/", name);
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate 					/*
17827c478bd9Sstevel@tonic-gate 					 * need to recalculate checksum
17837c478bd9Sstevel@tonic-gate 					 * because the name changed.
17847c478bd9Sstevel@tonic-gate 					 */
17857c478bd9Sstevel@tonic-gate 					(void) sprintf(dblock.dbuf.chksum,
17867c478bd9Sstevel@tonic-gate 					    "%07o", checksum(&dblock));
17877c478bd9Sstevel@tonic-gate 				}
17887c478bd9Sstevel@tonic-gate 			}
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate 			if (put_extra_attributes(longname, shortname, prefix,
17917c478bd9Sstevel@tonic-gate 			    filetype, '5') != 0)
17927c478bd9Sstevel@tonic-gate 				goto out;
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
17957c478bd9Sstevel@tonic-gate 			/*
17967c478bd9Sstevel@tonic-gate 			 * Reset header typeflag when archiving directory, since
17977c478bd9Sstevel@tonic-gate 			 * build_dblock changed it on us.
17987c478bd9Sstevel@tonic-gate 			 */
17997c478bd9Sstevel@tonic-gate 			if (filetype == XATTR_FILE) {
18007c478bd9Sstevel@tonic-gate 				dblock.dbuf.typeflag = _XATTR_HDRTYPE;
18017c478bd9Sstevel@tonic-gate 			} else {
18027c478bd9Sstevel@tonic-gate 				dblock.dbuf.typeflag = '5';
18037c478bd9Sstevel@tonic-gate 			}
18047c478bd9Sstevel@tonic-gate #else
18057c478bd9Sstevel@tonic-gate 			dblock.dbuf.typeflag = '5';
18067c478bd9Sstevel@tonic-gate #endif
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 			(void) sprintf(dblock.dbuf.chksum, "%07o",
18097c478bd9Sstevel@tonic-gate 			    checksum(&dblock));
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate 			(void) writetbuf((char *)&dblock, 1);
18127c478bd9Sstevel@tonic-gate 		}
18137c478bd9Sstevel@tonic-gate 		if (vflag) {
18147c478bd9Sstevel@tonic-gate #ifdef DEBUG
18157c478bd9Sstevel@tonic-gate 			if (NotTape)
18167c478bd9Sstevel@tonic-gate 				DEBUG("seek = %" FMT_blkcnt_t "K\t", K(tapepos),
18177c478bd9Sstevel@tonic-gate 				    0);
18187c478bd9Sstevel@tonic-gate #endif
18197c478bd9Sstevel@tonic-gate 			if (filetype == XATTR_FILE && Hiddendir) {
18207c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "a %s attribute . ",
18217c478bd9Sstevel@tonic-gate 				    longname);
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 			} else {
18247c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "a %s/ ", longname);
18257c478bd9Sstevel@tonic-gate 			}
18267c478bd9Sstevel@tonic-gate 			if (NotTape)
18277c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "%" FMT_blkcnt_t "K\n",
18287c478bd9Sstevel@tonic-gate 				    K(blocks));
18297c478bd9Sstevel@tonic-gate 			else
18307c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("%" FMT_blkcnt_t
18317c478bd9Sstevel@tonic-gate 				    " tape blocks\n"), blocks);
18327c478bd9Sstevel@tonic-gate 		}
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 		/*
18357c478bd9Sstevel@tonic-gate 		 * If hidden dir then break now since xattrs_put() will do
18367c478bd9Sstevel@tonic-gate 		 * the iterating of the directory.
18377c478bd9Sstevel@tonic-gate 		 *
18387c478bd9Sstevel@tonic-gate 		 * At the moment, there can't be attributes on attributes
18397c478bd9Sstevel@tonic-gate 		 * or directories within the attributes hidden directory
18407c478bd9Sstevel@tonic-gate 		 * hierarchy.
18417c478bd9Sstevel@tonic-gate 		 */
18427c478bd9Sstevel@tonic-gate 		if (filetype == XATTR_FILE)
18437c478bd9Sstevel@tonic-gate 			break;
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 		if (*shortname != '/')
18467c478bd9Sstevel@tonic-gate 			(void) sprintf(newparent, "%s/%s", parent, shortname);
18477c478bd9Sstevel@tonic-gate 		else
18487c478bd9Sstevel@tonic-gate 			(void) sprintf(newparent, "%s", shortname);
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 		if (chdir(shortname) < 0) {
18517c478bd9Sstevel@tonic-gate 			vperror(0, "%s", newparent);
18527c478bd9Sstevel@tonic-gate 			goto out;
18537c478bd9Sstevel@tonic-gate 		}
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 		if ((dirp = opendir(".")) == NULL) {
18567c478bd9Sstevel@tonic-gate 			vperror(0, gettext(
18577c478bd9Sstevel@tonic-gate 			"can't open directory %s"), longname);
18587c478bd9Sstevel@tonic-gate 			if (chdir(parent) < 0)
18597c478bd9Sstevel@tonic-gate 				vperror(0, gettext("cannot change back?: %s"),
18607c478bd9Sstevel@tonic-gate 				    parent);
18617c478bd9Sstevel@tonic-gate 			goto out;
18627c478bd9Sstevel@tonic-gate 		}
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 		while ((dp = readdir(dirp)) != NULL && !term) {
18657c478bd9Sstevel@tonic-gate 			if ((strcmp(".", dp->d_name) == 0) ||
18667c478bd9Sstevel@tonic-gate 			    (strcmp("..", dp->d_name) == 0))
18677c478bd9Sstevel@tonic-gate 				continue;
18687c478bd9Sstevel@tonic-gate 			(void) strcpy(cp, dp->d_name);
18697c478bd9Sstevel@tonic-gate 			if (stat(dp->d_name, &sbuf) < 0 ||
18707c478bd9Sstevel@tonic-gate 			    (sbuf.st_mode & S_IFMT) == S_IFDIR) {
18717c478bd9Sstevel@tonic-gate 				l = telldir(dirp);
18727c478bd9Sstevel@tonic-gate 				(void) closedir(dirp);
18737c478bd9Sstevel@tonic-gate 			} else
18747c478bd9Sstevel@tonic-gate 				l = -1;
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 			archtype = putfile(buf, cp, newparent,
18777c478bd9Sstevel@tonic-gate 			    NORMAL_FILE, lev + 1, symlink_lev);
18787c478bd9Sstevel@tonic-gate 
18797c478bd9Sstevel@tonic-gate 			if (!exitflag) {
18807c478bd9Sstevel@tonic-gate 				if (atflag && archtype == PUT_NOTAS_LINK) {
18817c478bd9Sstevel@tonic-gate 					xattrs_put(buf, cp, newparent);
18827c478bd9Sstevel@tonic-gate 				}
18837c478bd9Sstevel@tonic-gate 			}
18847c478bd9Sstevel@tonic-gate 			if (exitflag)
18857c478bd9Sstevel@tonic-gate 				break;
18867c478bd9Sstevel@tonic-gate 
18877c478bd9Sstevel@tonic-gate 			/*
18887c478bd9Sstevel@tonic-gate 			 * If the directory was not closed, then it does
18897c478bd9Sstevel@tonic-gate 			 * not need to be reopened.
18907c478bd9Sstevel@tonic-gate 			 */
18917c478bd9Sstevel@tonic-gate 			if (l < 0)
18927c478bd9Sstevel@tonic-gate 				continue;
18937c478bd9Sstevel@tonic-gate 			if ((dirp = opendir(".")) == NULL) {
18947c478bd9Sstevel@tonic-gate 				vperror(0, gettext(
18957c478bd9Sstevel@tonic-gate 				    "can't open directory %s"), longname);
18967c478bd9Sstevel@tonic-gate 				if (chdir(parent) < 0)
18977c478bd9Sstevel@tonic-gate 					vperror(0,
18987c478bd9Sstevel@tonic-gate 					    gettext("cannot change back?: %s"),
18997c478bd9Sstevel@tonic-gate 					    parent);
19007c478bd9Sstevel@tonic-gate 				goto out;
19017c478bd9Sstevel@tonic-gate 			}
19027c478bd9Sstevel@tonic-gate 			seekdir(dirp, l);
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate 		}
19057c478bd9Sstevel@tonic-gate 		(void) closedir(dirp);
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate 		if (chdir(parent) < 0) {
19087c478bd9Sstevel@tonic-gate 			vperror(0, gettext("cannot change back?: %s"), parent);
19097c478bd9Sstevel@tonic-gate 		}
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate 		break;
19127c478bd9Sstevel@tonic-gate 
19137c478bd9Sstevel@tonic-gate 	case S_IFLNK:
19147c478bd9Sstevel@tonic-gate 		readlink_max = NAMSIZ;
19157c478bd9Sstevel@tonic-gate 		if (stbuf.st_size > NAMSIZ) {
19167c478bd9Sstevel@tonic-gate 			if (Eflag > 0) {
19177c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_LINKPATH;
19187c478bd9Sstevel@tonic-gate 				readlink_max = PATH_MAX;
19197c478bd9Sstevel@tonic-gate 			} else {
19207c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
19217c478bd9Sstevel@tonic-gate 				    "tar: %s: symbolic link too long\n"),
19227c478bd9Sstevel@tonic-gate 				    longname);
19237c478bd9Sstevel@tonic-gate 				if (errflag)
19247c478bd9Sstevel@tonic-gate 					exitflag = 1;
19257c478bd9Sstevel@tonic-gate 				Errflg = 1;
19267c478bd9Sstevel@tonic-gate 				goto out;
19277c478bd9Sstevel@tonic-gate 			}
19287c478bd9Sstevel@tonic-gate 		}
19297c478bd9Sstevel@tonic-gate 		/*
19307c478bd9Sstevel@tonic-gate 		 * Sym-links need header size of zero since you
19317c478bd9Sstevel@tonic-gate 		 * don't store any data for this type.
19327c478bd9Sstevel@tonic-gate 		 */
19337c478bd9Sstevel@tonic-gate 		stbuf.st_size = (off_t)0;
19347c478bd9Sstevel@tonic-gate 		tomodes(&stbuf);
19357c478bd9Sstevel@tonic-gate 		i = readlink(shortname, filetmp, readlink_max);
19367c478bd9Sstevel@tonic-gate 		if (i < 0) {
19377c478bd9Sstevel@tonic-gate 			vperror(0, gettext(
19387c478bd9Sstevel@tonic-gate 			    "can't read symbolic link %s"), longname);
19397c478bd9Sstevel@tonic-gate 			goto out;
19407c478bd9Sstevel@tonic-gate 		} else {
19417c478bd9Sstevel@tonic-gate 			filetmp[i] = 0;
19427c478bd9Sstevel@tonic-gate 		}
19437c478bd9Sstevel@tonic-gate 		if (vflag)
19447c478bd9Sstevel@tonic-gate 			(void) fprintf(vfile, gettext(
19457c478bd9Sstevel@tonic-gate 			    "a %s symbolic link to %s\n"),
19467c478bd9Sstevel@tonic-gate 			    longname, filetmp);
19477c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_LINKPATH) {
19487c478bd9Sstevel@tonic-gate 			Xtarhdr.x_linkpath = filetmp;
19497c478bd9Sstevel@tonic-gate 			if (build_dblock(name, tchar, '2', filetype, &stbuf,
19507c478bd9Sstevel@tonic-gate 			    stbuf.st_dev, prefix) != 0)
19517c478bd9Sstevel@tonic-gate 				goto out;
19527c478bd9Sstevel@tonic-gate 		} else
19537c478bd9Sstevel@tonic-gate 			if (build_dblock(name, filetmp, '2', filetype, &stbuf,
19547c478bd9Sstevel@tonic-gate 			    stbuf.st_dev, prefix) != 0)
19557c478bd9Sstevel@tonic-gate 				goto out;
19567c478bd9Sstevel@tonic-gate 		(void) writetbuf((char *)&dblock, 1);
19577c478bd9Sstevel@tonic-gate 		/*
19587c478bd9Sstevel@tonic-gate 		 * No acls for symlinks: mode is always 777
19597c478bd9Sstevel@tonic-gate 		 * dont call write ancillary
19607c478bd9Sstevel@tonic-gate 		 */
19617c478bd9Sstevel@tonic-gate 		rc = PUT_AS_LINK;
19627c478bd9Sstevel@tonic-gate 		break;
19637c478bd9Sstevel@tonic-gate 	case S_IFREG:
19647c478bd9Sstevel@tonic-gate 		if ((infile = openat(dirfd, shortname, 0)) < 0) {
19657c478bd9Sstevel@tonic-gate 			vperror(0, "%s%s%s", longname,
19667c478bd9Sstevel@tonic-gate 			    (filetype == XATTR_FILE) ?
19677c478bd9Sstevel@tonic-gate 			    gettext(" attribute ") : "",
19687c478bd9Sstevel@tonic-gate 			    (filetype == XATTR_FILE) ?
19697c478bd9Sstevel@tonic-gate 			    shortname : "");
19707c478bd9Sstevel@tonic-gate 			goto out;
19717c478bd9Sstevel@tonic-gate 		}
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(stbuf.st_size);
19747c478bd9Sstevel@tonic-gate 
19757c478bd9Sstevel@tonic-gate 		if (put_link(name, longname, shortname,
19767c478bd9Sstevel@tonic-gate 		    prefix, filetype, '1') == 0) {
19777c478bd9Sstevel@tonic-gate 			(void) close(infile);
19787c478bd9Sstevel@tonic-gate 			rc = PUT_AS_LINK;
19797c478bd9Sstevel@tonic-gate 			goto out;
19807c478bd9Sstevel@tonic-gate 		}
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 		tomodes(&stbuf);
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate 		/* correctly handle end of volume */
19857c478bd9Sstevel@tonic-gate 		while (mulvol && tapepos + blocks + 1 > blocklim) {
19867c478bd9Sstevel@tonic-gate 			/* file won't fit */
19877c478bd9Sstevel@tonic-gate 			if (eflag) {
19887c478bd9Sstevel@tonic-gate 				if (blocks <= blocklim) {
19897c478bd9Sstevel@tonic-gate 					newvol();
19907c478bd9Sstevel@tonic-gate 					break;
19917c478bd9Sstevel@tonic-gate 				}
19927c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
19937c478bd9Sstevel@tonic-gate 				    "tar: Single file cannot fit on volume\n"));
19947c478bd9Sstevel@tonic-gate 				done(3);
19957c478bd9Sstevel@tonic-gate 			}
19967c478bd9Sstevel@tonic-gate 			/* split if floppy has some room and file is large */
19977c478bd9Sstevel@tonic-gate 			if (((blocklim - tapepos) >= EXTMIN) &&
19987c478bd9Sstevel@tonic-gate 			    ((blocks + 1) >= blocklim/10)) {
19997c478bd9Sstevel@tonic-gate 				splitfile(longname, infile,
20007c478bd9Sstevel@tonic-gate 				    name, prefix, filetype);
20017c478bd9Sstevel@tonic-gate 				(void) close(dirfd);
20027c478bd9Sstevel@tonic-gate 				(void) close(infile);
20037c478bd9Sstevel@tonic-gate 				goto out;
20047c478bd9Sstevel@tonic-gate 			}
20057c478bd9Sstevel@tonic-gate 			newvol();	/* not worth it--just get new volume */
20067c478bd9Sstevel@tonic-gate 		}
20077c478bd9Sstevel@tonic-gate #ifdef DEBUG
20087c478bd9Sstevel@tonic-gate 		DEBUG("putfile: %s wants %" FMT_blkcnt_t " blocks\n", longname,
20097c478bd9Sstevel@tonic-gate 		    blocks);
20107c478bd9Sstevel@tonic-gate #endif
20117c478bd9Sstevel@tonic-gate 		if (build_dblock(name, tchar, '0', filetype,
20127c478bd9Sstevel@tonic-gate 		    &stbuf, stbuf.st_dev, prefix) != 0) {
20137c478bd9Sstevel@tonic-gate 			goto out;
20147c478bd9Sstevel@tonic-gate 		}
20157c478bd9Sstevel@tonic-gate 		if (vflag) {
20167c478bd9Sstevel@tonic-gate #ifdef DEBUG
20177c478bd9Sstevel@tonic-gate 			if (NotTape)
20187c478bd9Sstevel@tonic-gate 				DEBUG("seek = %" FMT_blkcnt_t "K\t", K(tapepos),
20197c478bd9Sstevel@tonic-gate 				    0);
20207c478bd9Sstevel@tonic-gate #endif
20217c478bd9Sstevel@tonic-gate 			(void) fprintf(vfile, "a %s%s%s ", longname,
20227c478bd9Sstevel@tonic-gate 			    (filetype == XATTR_FILE) ?
20237c478bd9Sstevel@tonic-gate 			    gettext(" attribute ") : "",
20247c478bd9Sstevel@tonic-gate 			    (filetype == XATTR_FILE) ?
20257c478bd9Sstevel@tonic-gate 			    shortname : "");
20267c478bd9Sstevel@tonic-gate 			if (NotTape)
20277c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "%" FMT_blkcnt_t "K\n",
20287c478bd9Sstevel@tonic-gate 				    K(blocks));
20297c478bd9Sstevel@tonic-gate 			else
20307c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile,
20317c478bd9Sstevel@tonic-gate 				    gettext("%" FMT_blkcnt_t " tape blocks\n"),
20327c478bd9Sstevel@tonic-gate 				    blocks);
20337c478bd9Sstevel@tonic-gate 		}
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate 		if (put_extra_attributes(longname, shortname, prefix,
20367c478bd9Sstevel@tonic-gate 		    filetype, '0') != 0)
20377c478bd9Sstevel@tonic-gate 			goto out;
20387c478bd9Sstevel@tonic-gate 
20397c478bd9Sstevel@tonic-gate 		/*
20407c478bd9Sstevel@tonic-gate 		 * No need to reset typeflag for extended attribute here, since
20417c478bd9Sstevel@tonic-gate 		 * put_extra_attributes already set it and we haven't called
20427c478bd9Sstevel@tonic-gate 		 * build_dblock().
20437c478bd9Sstevel@tonic-gate 		 */
20447c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
20457c478bd9Sstevel@tonic-gate 		hint = writetbuf((char *)&dblock, 1);
2046eace40a5Sceastha 		maxread = max(min(stbuf.st_blksize, stbuf.st_size),
2047eace40a5Sceastha 		    (nblock * TBLOCK));
20487c478bd9Sstevel@tonic-gate 		if ((bigbuf = calloc((unsigned)maxread, sizeof (char))) == 0) {
20497c478bd9Sstevel@tonic-gate 			maxread = TBLOCK;
20507c478bd9Sstevel@tonic-gate 			bigbuf = buf;
20517c478bd9Sstevel@tonic-gate 		}
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate 		while (((i = (int)
20547c478bd9Sstevel@tonic-gate 		    read(infile, bigbuf, min((hint*TBLOCK), maxread))) > 0) &&
20557c478bd9Sstevel@tonic-gate 		    blocks) {
20567c478bd9Sstevel@tonic-gate 			blkcnt_t nblks;
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 			nblks = ((i-1)/TBLOCK)+1;
20597c478bd9Sstevel@tonic-gate 			if (nblks > blocks)
20607c478bd9Sstevel@tonic-gate 				nblks = blocks;
20617c478bd9Sstevel@tonic-gate 			hint = writetbuf(bigbuf, nblks);
20627c478bd9Sstevel@tonic-gate 			blocks -= nblks;
20637c478bd9Sstevel@tonic-gate 		}
20647c478bd9Sstevel@tonic-gate 		(void) close(infile);
20657c478bd9Sstevel@tonic-gate 		if (bigbuf != buf)
20667c478bd9Sstevel@tonic-gate 			free(bigbuf);
20677c478bd9Sstevel@tonic-gate 		if (i < 0)
20687c478bd9Sstevel@tonic-gate 			vperror(0, gettext("Read error on %s"), longname);
20697c478bd9Sstevel@tonic-gate 		else if (blocks != 0 || i != 0) {
20707c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
20717c478bd9Sstevel@tonic-gate 			"tar: %s: file changed size\n"), longname);
20727c478bd9Sstevel@tonic-gate 			if (errflag) {
20737c478bd9Sstevel@tonic-gate 				exitflag = 1;
20747c478bd9Sstevel@tonic-gate 				Errflg = 1;
20757c478bd9Sstevel@tonic-gate 			} else if (!Dflag) {
20767c478bd9Sstevel@tonic-gate 				Errflg = 1;
20777c478bd9Sstevel@tonic-gate 			}
20787c478bd9Sstevel@tonic-gate 		}
20797c478bd9Sstevel@tonic-gate 		putempty(blocks);
20807c478bd9Sstevel@tonic-gate 		break;
20817c478bd9Sstevel@tonic-gate 	case S_IFIFO:
20827c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(stbuf.st_size);
20837c478bd9Sstevel@tonic-gate 		stbuf.st_size = (off_t)0;
20847c478bd9Sstevel@tonic-gate 
20857c478bd9Sstevel@tonic-gate 		if (put_link(name, longname, shortname,
20867c478bd9Sstevel@tonic-gate 		    prefix, filetype, '6') == 0) {
20877c478bd9Sstevel@tonic-gate 			rc = PUT_AS_LINK;
20887c478bd9Sstevel@tonic-gate 			goto out;
20897c478bd9Sstevel@tonic-gate 		}
20907c478bd9Sstevel@tonic-gate 		tomodes(&stbuf);
20917c478bd9Sstevel@tonic-gate 
20927c478bd9Sstevel@tonic-gate 		while (mulvol && tapepos + blocks + 1 > blocklim) {
20937c478bd9Sstevel@tonic-gate 			if (eflag) {
20947c478bd9Sstevel@tonic-gate 				if (blocks <= blocklim) {
20957c478bd9Sstevel@tonic-gate 					newvol();
20967c478bd9Sstevel@tonic-gate 					break;
20977c478bd9Sstevel@tonic-gate 				}
20987c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
20997c478bd9Sstevel@tonic-gate 				    "tar: Single file cannot fit on volume\n"));
21007c478bd9Sstevel@tonic-gate 				done(3);
21017c478bd9Sstevel@tonic-gate 			}
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate 			if (((blocklim - tapepos) >= EXTMIN) &&
21047c478bd9Sstevel@tonic-gate 			    ((blocks + 1) >= blocklim/10)) {
21057c478bd9Sstevel@tonic-gate 				splitfile(longname, infile, name,
21067c478bd9Sstevel@tonic-gate 				    prefix, filetype);
21077c478bd9Sstevel@tonic-gate 				(void) close(dirfd);
21087c478bd9Sstevel@tonic-gate 				(void) close(infile);
21097c478bd9Sstevel@tonic-gate 				goto out;
21107c478bd9Sstevel@tonic-gate 			}
21117c478bd9Sstevel@tonic-gate 			newvol();
21127c478bd9Sstevel@tonic-gate 		}
21137c478bd9Sstevel@tonic-gate #ifdef DEBUG
21147c478bd9Sstevel@tonic-gate 		DEBUG("putfile: %s wants %" FMT_blkcnt_t " blocks\n", longname,
21157c478bd9Sstevel@tonic-gate 		    blocks);
21167c478bd9Sstevel@tonic-gate #endif
21177c478bd9Sstevel@tonic-gate 		if (vflag) {
21187c478bd9Sstevel@tonic-gate #ifdef DEBUG
21197c478bd9Sstevel@tonic-gate 			if (NotTape)
21207c478bd9Sstevel@tonic-gate 				DEBUG("seek = %" FMT_blkcnt_t "K\t", K(tapepos),
21217c478bd9Sstevel@tonic-gate 				    0);
21227c478bd9Sstevel@tonic-gate #endif
21237c478bd9Sstevel@tonic-gate 			if (NotTape)
21247c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("a %s %"
21257c478bd9Sstevel@tonic-gate 				    FMT_blkcnt_t "K\n "), longname, K(blocks));
21267c478bd9Sstevel@tonic-gate 			else
21277c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext(
21287c478bd9Sstevel@tonic-gate 				    "a %s %" FMT_blkcnt_t " tape blocks\n"),
21297c478bd9Sstevel@tonic-gate 				    longname, blocks);
21307c478bd9Sstevel@tonic-gate 		}
21317c478bd9Sstevel@tonic-gate 		if (build_dblock(name, tchar, '6', filetype,
21327c478bd9Sstevel@tonic-gate 		    &stbuf, stbuf.st_dev, prefix) != 0)
21337c478bd9Sstevel@tonic-gate 			goto out;
21347c478bd9Sstevel@tonic-gate 
21357c478bd9Sstevel@tonic-gate 		if (put_extra_attributes(longname, shortname, prefix,
21367c478bd9Sstevel@tonic-gate 		    filetype, '6') != 0)
21377c478bd9Sstevel@tonic-gate 			goto out;
21387c478bd9Sstevel@tonic-gate 
21397c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
21407c478bd9Sstevel@tonic-gate 		dblock.dbuf.typeflag = '6';
21417c478bd9Sstevel@tonic-gate 
21427c478bd9Sstevel@tonic-gate 		(void) writetbuf((char *)&dblock, 1);
21437c478bd9Sstevel@tonic-gate 		break;
21447c478bd9Sstevel@tonic-gate 	case S_IFCHR:
21457c478bd9Sstevel@tonic-gate 		stbuf.st_size = (off_t)0;
21467c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(stbuf.st_size);
21477c478bd9Sstevel@tonic-gate 		if (put_link(name, longname,
21487c478bd9Sstevel@tonic-gate 		    shortname, prefix, filetype, '3') == 0) {
21497c478bd9Sstevel@tonic-gate 			rc = PUT_AS_LINK;
21507c478bd9Sstevel@tonic-gate 			goto out;
21517c478bd9Sstevel@tonic-gate 		}
21527c478bd9Sstevel@tonic-gate 		tomodes(&stbuf);
21537c478bd9Sstevel@tonic-gate 
21547c478bd9Sstevel@tonic-gate 		while (mulvol && tapepos + blocks + 1 > blocklim) {
21557c478bd9Sstevel@tonic-gate 			if (eflag) {
21567c478bd9Sstevel@tonic-gate 				if (blocks <= blocklim) {
21577c478bd9Sstevel@tonic-gate 					newvol();
21587c478bd9Sstevel@tonic-gate 					break;
21597c478bd9Sstevel@tonic-gate 				}
21607c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
21617c478bd9Sstevel@tonic-gate 				    "tar: Single file cannot fit on volume\n"));
21627c478bd9Sstevel@tonic-gate 				done(3);
21637c478bd9Sstevel@tonic-gate 			}
21647c478bd9Sstevel@tonic-gate 
21657c478bd9Sstevel@tonic-gate 			if (((blocklim - tapepos) >= EXTMIN) &&
21667c478bd9Sstevel@tonic-gate 			    ((blocks + 1) >= blocklim/10)) {
21677c478bd9Sstevel@tonic-gate 				splitfile(longname, infile, name,
21687c478bd9Sstevel@tonic-gate 				    prefix, filetype);
21697c478bd9Sstevel@tonic-gate 				(void) close(dirfd);
21707c478bd9Sstevel@tonic-gate 				goto out;
21717c478bd9Sstevel@tonic-gate 			}
21727c478bd9Sstevel@tonic-gate 			newvol();
21737c478bd9Sstevel@tonic-gate 		}
21747c478bd9Sstevel@tonic-gate #ifdef DEBUG
21757c478bd9Sstevel@tonic-gate 		DEBUG("putfile: %s wants %" FMT_blkcnt_t " blocks\n", longname,
21767c478bd9Sstevel@tonic-gate 		    blocks);
21777c478bd9Sstevel@tonic-gate #endif
21787c478bd9Sstevel@tonic-gate 		if (vflag) {
21797c478bd9Sstevel@tonic-gate #ifdef DEBUG
21807c478bd9Sstevel@tonic-gate 			if (NotTape)
21817c478bd9Sstevel@tonic-gate 				DEBUG("seek = %" FMT_blkcnt_t "K\t", K(tapepos),
21827c478bd9Sstevel@tonic-gate 				    0);
21837c478bd9Sstevel@tonic-gate #endif
21847c478bd9Sstevel@tonic-gate 			if (NotTape)
21857c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("a %s %"
21867c478bd9Sstevel@tonic-gate 				    FMT_blkcnt_t "K\n"), longname, K(blocks));
21877c478bd9Sstevel@tonic-gate 			else
21887c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("a %s %"
21897c478bd9Sstevel@tonic-gate 				    FMT_blkcnt_t " tape blocks\n"), longname,
21907c478bd9Sstevel@tonic-gate 				    blocks);
21917c478bd9Sstevel@tonic-gate 		}
21927c478bd9Sstevel@tonic-gate 		if (build_dblock(name, tchar, '3',
21937c478bd9Sstevel@tonic-gate 		    filetype, &stbuf, stbuf.st_rdev, prefix) != 0)
21947c478bd9Sstevel@tonic-gate 			goto out;
21957c478bd9Sstevel@tonic-gate 
21967c478bd9Sstevel@tonic-gate 		if (put_extra_attributes(longname, shortname,
21977c478bd9Sstevel@tonic-gate 		    prefix, filetype, '3') != 0)
21987c478bd9Sstevel@tonic-gate 			goto out;
21997c478bd9Sstevel@tonic-gate 
22007c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
22017c478bd9Sstevel@tonic-gate 		dblock.dbuf.typeflag = '3';
22027c478bd9Sstevel@tonic-gate 
22037c478bd9Sstevel@tonic-gate 		(void) writetbuf((char *)&dblock, 1);
22047c478bd9Sstevel@tonic-gate 		break;
22057c478bd9Sstevel@tonic-gate 	case S_IFBLK:
22067c478bd9Sstevel@tonic-gate 		stbuf.st_size = (off_t)0;
22077c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(stbuf.st_size);
22087c478bd9Sstevel@tonic-gate 		if (put_link(name, longname,
22097c478bd9Sstevel@tonic-gate 		    shortname, prefix, filetype, '4') == 0) {
22107c478bd9Sstevel@tonic-gate 			rc = PUT_AS_LINK;
22117c478bd9Sstevel@tonic-gate 			goto out;
22127c478bd9Sstevel@tonic-gate 		}
22137c478bd9Sstevel@tonic-gate 		tomodes(&stbuf);
22147c478bd9Sstevel@tonic-gate 
22157c478bd9Sstevel@tonic-gate 		while (mulvol && tapepos + blocks + 1 > blocklim) {
22167c478bd9Sstevel@tonic-gate 			if (eflag) {
22177c478bd9Sstevel@tonic-gate 				if (blocks <= blocklim) {
22187c478bd9Sstevel@tonic-gate 					newvol();
22197c478bd9Sstevel@tonic-gate 					break;
22207c478bd9Sstevel@tonic-gate 				}
22217c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
22227c478bd9Sstevel@tonic-gate 				    "tar: Single file cannot fit on volume\n"));
22237c478bd9Sstevel@tonic-gate 				done(3);
22247c478bd9Sstevel@tonic-gate 			}
22257c478bd9Sstevel@tonic-gate 
22267c478bd9Sstevel@tonic-gate 			if (((blocklim - tapepos) >= EXTMIN) &&
22277c478bd9Sstevel@tonic-gate 			    ((blocks + 1) >= blocklim/10)) {
22287c478bd9Sstevel@tonic-gate 				splitfile(longname, infile,
22297c478bd9Sstevel@tonic-gate 				    name, prefix, filetype);
22307c478bd9Sstevel@tonic-gate 				(void) close(dirfd);
22317c478bd9Sstevel@tonic-gate 				goto out;
22327c478bd9Sstevel@tonic-gate 			}
22337c478bd9Sstevel@tonic-gate 			newvol();
22347c478bd9Sstevel@tonic-gate 		}
22357c478bd9Sstevel@tonic-gate #ifdef DEBUG
22367c478bd9Sstevel@tonic-gate 		DEBUG("putfile: %s wants %" FMT_blkcnt_t " blocks\n", longname,
22377c478bd9Sstevel@tonic-gate 		    blocks);
22387c478bd9Sstevel@tonic-gate #endif
22397c478bd9Sstevel@tonic-gate 		if (vflag) {
22407c478bd9Sstevel@tonic-gate #ifdef DEBUG
22417c478bd9Sstevel@tonic-gate 			if (NotTape)
22427c478bd9Sstevel@tonic-gate 				DEBUG("seek = %" FMT_blkcnt_t "K\t", K(tapepos),
22437c478bd9Sstevel@tonic-gate 				    0);
22447c478bd9Sstevel@tonic-gate #endif
22457c478bd9Sstevel@tonic-gate 			(void) fprintf(vfile, "a %s ", longname);
22467c478bd9Sstevel@tonic-gate 			if (NotTape)
22477c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "%" FMT_blkcnt_t "K\n",
22487c478bd9Sstevel@tonic-gate 				    K(blocks));
22497c478bd9Sstevel@tonic-gate 			else
22507c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("%"
22517c478bd9Sstevel@tonic-gate 				    FMT_blkcnt_t " tape blocks\n"), blocks);
22527c478bd9Sstevel@tonic-gate 		}
22537c478bd9Sstevel@tonic-gate 		if (build_dblock(name, tchar, '4',
22547c478bd9Sstevel@tonic-gate 		    filetype, &stbuf, stbuf.st_rdev, prefix) != 0)
22557c478bd9Sstevel@tonic-gate 			goto out;
22567c478bd9Sstevel@tonic-gate 
22577c478bd9Sstevel@tonic-gate 		if (put_extra_attributes(longname, shortname,
22587c478bd9Sstevel@tonic-gate 		    prefix, filetype, '4') != 0)
22597c478bd9Sstevel@tonic-gate 			goto out;
22607c478bd9Sstevel@tonic-gate 
22617c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
22627c478bd9Sstevel@tonic-gate 		dblock.dbuf.typeflag = '4';
22637c478bd9Sstevel@tonic-gate 
22647c478bd9Sstevel@tonic-gate 		(void) writetbuf((char *)&dblock, 1);
22657c478bd9Sstevel@tonic-gate 		break;
22667c478bd9Sstevel@tonic-gate 	default:
22677c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
22687c478bd9Sstevel@tonic-gate 		    "tar: %s is not a file. Not dumped\n"), longname);
22697c478bd9Sstevel@tonic-gate 		if (errflag)
22707c478bd9Sstevel@tonic-gate 			exitflag = 1;
22717c478bd9Sstevel@tonic-gate 		Errflg = 1;
22727c478bd9Sstevel@tonic-gate 		goto out;
22737c478bd9Sstevel@tonic-gate 	}
22747c478bd9Sstevel@tonic-gate 
22757c478bd9Sstevel@tonic-gate out:
22767c478bd9Sstevel@tonic-gate 	if (dirfd != -1) {
22777c478bd9Sstevel@tonic-gate 		if (filetype == XATTR_FILE)
22787c478bd9Sstevel@tonic-gate 			(void) chdir(parent);
22797c478bd9Sstevel@tonic-gate 		(void) close(dirfd);
22807c478bd9Sstevel@tonic-gate 	}
22817c478bd9Sstevel@tonic-gate 	return (rc);
22827c478bd9Sstevel@tonic-gate }
22837c478bd9Sstevel@tonic-gate 
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate /*
22867c478bd9Sstevel@tonic-gate  *	splitfile	dump a large file across volumes
22877c478bd9Sstevel@tonic-gate  *
22887c478bd9Sstevel@tonic-gate  *	splitfile(longname, fd);
22897c478bd9Sstevel@tonic-gate  *		char *longname;		full name of file
22907c478bd9Sstevel@tonic-gate  *		int ifd;		input file descriptor
22917c478bd9Sstevel@tonic-gate  *
22927c478bd9Sstevel@tonic-gate  *	NOTE:  only called by putfile() to dump a large file.
22937c478bd9Sstevel@tonic-gate  */
22947c478bd9Sstevel@tonic-gate 
22957c478bd9Sstevel@tonic-gate static void
22967c478bd9Sstevel@tonic-gate splitfile(char *longname, int ifd, char *name, char *prefix, int filetype)
22977c478bd9Sstevel@tonic-gate {
22987c478bd9Sstevel@tonic-gate 	blkcnt_t blocks;
22997c478bd9Sstevel@tonic-gate 	off_t bytes, s;
23007c478bd9Sstevel@tonic-gate 	char buf[TBLOCK];
23017c478bd9Sstevel@tonic-gate 	int i, extents;
23027c478bd9Sstevel@tonic-gate 
23037c478bd9Sstevel@tonic-gate 	blocks = TBLOCKS(stbuf.st_size);	/* blocks file needs */
23047c478bd9Sstevel@tonic-gate 
23057c478bd9Sstevel@tonic-gate 	/*
23067c478bd9Sstevel@tonic-gate 	 * # extents =
23077c478bd9Sstevel@tonic-gate 	 *	size of file after using up rest of this floppy
23087c478bd9Sstevel@tonic-gate 	 *		blocks - (blocklim - tapepos) + 1	(for header)
23097c478bd9Sstevel@tonic-gate 	 *	plus roundup value before divide by blocklim-1
23107c478bd9Sstevel@tonic-gate 	 *		+ (blocklim - 1) - 1
23117c478bd9Sstevel@tonic-gate 	 *	all divided by blocklim-1 (one block for each header).
23127c478bd9Sstevel@tonic-gate 	 * this gives
23137c478bd9Sstevel@tonic-gate 	 *	(blocks - blocklim + tapepos + 1 + blocklim - 2)/(blocklim-1)
23147c478bd9Sstevel@tonic-gate 	 * which reduces to the expression used.
23157c478bd9Sstevel@tonic-gate 	 * one is added to account for this first extent.
23167c478bd9Sstevel@tonic-gate 	 *
23177c478bd9Sstevel@tonic-gate 	 * When one is dealing with extremely large archives, one may want
23187c478bd9Sstevel@tonic-gate 	 * to allow for a large number of extents.  This code should be
23197c478bd9Sstevel@tonic-gate 	 * revisited to determine if extents should be changed to something
23207c478bd9Sstevel@tonic-gate 	 * larger than an int.
23217c478bd9Sstevel@tonic-gate 	 */
23227c478bd9Sstevel@tonic-gate 	extents = (int)((blocks + tapepos - 1ULL)/(blocklim - 1ULL) + 1);
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate 	if (extents < 2 || extents > MAXEXT) {	/* let's be reasonable */
23257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
23267c478bd9Sstevel@tonic-gate 		    "tar: %s needs unusual number of volumes to split\n"
23277c478bd9Sstevel@tonic-gate 		    "tar: %s not dumped\n"), longname, longname);
23287c478bd9Sstevel@tonic-gate 		return;
23297c478bd9Sstevel@tonic-gate 	}
23307c478bd9Sstevel@tonic-gate 	if (build_dblock(name, tchar, '0', filetype,
23317c478bd9Sstevel@tonic-gate 	    &stbuf, stbuf.st_dev, prefix) != 0)
23327c478bd9Sstevel@tonic-gate 		return;
23337c478bd9Sstevel@tonic-gate 
23347c478bd9Sstevel@tonic-gate 	dblock.dbuf.extotal = extents;
23357c478bd9Sstevel@tonic-gate 	bytes = stbuf.st_size;
23367c478bd9Sstevel@tonic-gate 
23377c478bd9Sstevel@tonic-gate 	/*
23387c478bd9Sstevel@tonic-gate 	 * The value contained in dblock.dbuf.efsize was formerly used when the
23397c478bd9Sstevel@tonic-gate 	 * v flag was specified in conjunction with the t flag. Although it is
23407c478bd9Sstevel@tonic-gate 	 * no longer used, older versions of tar will expect the former
23417c478bd9Sstevel@tonic-gate 	 * behaviour, so we must continue to write it to the archive.
23427c478bd9Sstevel@tonic-gate 	 *
23437c478bd9Sstevel@tonic-gate 	 * Since dblock.dbuf.efsize is 10 chars in size, the maximum value it
23447c478bd9Sstevel@tonic-gate 	 * can store is TAR_EFSIZE_MAX. If bytes exceeds that value, simply
23457c478bd9Sstevel@tonic-gate 	 * store 0.
23467c478bd9Sstevel@tonic-gate 	 */
23477c478bd9Sstevel@tonic-gate 	if (bytes <= TAR_EFSIZE_MAX)
23487c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.efsize, "%9" FMT_off_t_o, bytes);
23497c478bd9Sstevel@tonic-gate 	else
23507c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.efsize, "%9" FMT_off_t_o, (off_t)0);
23517c478bd9Sstevel@tonic-gate 
23527c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
23537c478bd9Sstevel@tonic-gate 	    "tar: large file %s needs %d extents.\n"
23547c478bd9Sstevel@tonic-gate 	    "tar: current device seek position = %" FMT_blkcnt_t "K\n"),
23557c478bd9Sstevel@tonic-gate 	    longname, extents, K(tapepos));
23567c478bd9Sstevel@tonic-gate 
23577c478bd9Sstevel@tonic-gate 	s = (off_t)(blocklim - tapepos - 1) * TBLOCK;
23587c478bd9Sstevel@tonic-gate 	for (i = 1; i <= extents; i++) {
23597c478bd9Sstevel@tonic-gate 		if (i > 1) {
23607c478bd9Sstevel@tonic-gate 			newvol();
23617c478bd9Sstevel@tonic-gate 			if (i == extents)
23627c478bd9Sstevel@tonic-gate 				s = bytes;	/* last ext. gets true bytes */
23637c478bd9Sstevel@tonic-gate 			else
23647c478bd9Sstevel@tonic-gate 				s = (off_t)(blocklim - 1)*TBLOCK; /* all */
23657c478bd9Sstevel@tonic-gate 		}
23667c478bd9Sstevel@tonic-gate 		bytes -= s;
23677c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(s);
23687c478bd9Sstevel@tonic-gate 
23697c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.size, "%011" FMT_off_t_o, s);
23707c478bd9Sstevel@tonic-gate 		dblock.dbuf.extno = i;
23717c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
23727c478bd9Sstevel@tonic-gate 		(void) writetbuf((char *)&dblock, 1);
23737c478bd9Sstevel@tonic-gate 
23747c478bd9Sstevel@tonic-gate 		if (vflag)
23757c478bd9Sstevel@tonic-gate 			(void) fprintf(vfile,
23767c478bd9Sstevel@tonic-gate 			    "+++ a %s %" FMT_blkcnt_t "K [extent #%d of %d]\n",
23777c478bd9Sstevel@tonic-gate 			    longname, K(blocks), i, extents);
23787c478bd9Sstevel@tonic-gate 		while (blocks && read(ifd, buf, TBLOCK) > 0) {
23797c478bd9Sstevel@tonic-gate 			blocks--;
23807c478bd9Sstevel@tonic-gate 			(void) writetbuf(buf, 1);
23817c478bd9Sstevel@tonic-gate 		}
23827c478bd9Sstevel@tonic-gate 		if (blocks != 0) {
23837c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
23847c478bd9Sstevel@tonic-gate 			    "tar: %s: file changed size\n"), longname);
23857c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
23867c478bd9Sstevel@tonic-gate 			    "tar: aborting split file %s\n"), longname);
23877c478bd9Sstevel@tonic-gate 			(void) close(ifd);
23887c478bd9Sstevel@tonic-gate 			return;
23897c478bd9Sstevel@tonic-gate 		}
23907c478bd9Sstevel@tonic-gate 	}
23917c478bd9Sstevel@tonic-gate 	(void) close(ifd);
23927c478bd9Sstevel@tonic-gate 	if (vflag)
23937c478bd9Sstevel@tonic-gate 		(void) fprintf(vfile, gettext("a %s %" FMT_off_t "K (in %d "
23947c478bd9Sstevel@tonic-gate 		    "extents)\n"), longname, K(TBLOCKS(stbuf.st_size)),
23957c478bd9Sstevel@tonic-gate 		    extents);
23967c478bd9Sstevel@tonic-gate }
23977c478bd9Sstevel@tonic-gate 
23987c478bd9Sstevel@tonic-gate /*
23997c478bd9Sstevel@tonic-gate  *	convtoreg - determines whether the file should be converted to a
24007c478bd9Sstevel@tonic-gate  *	            regular file when extracted
24017c478bd9Sstevel@tonic-gate  *
24027c478bd9Sstevel@tonic-gate  *	Returns 1 when file size > 0 and typeflag is not recognized
24037c478bd9Sstevel@tonic-gate  * 	Otherwise returns 0
24047c478bd9Sstevel@tonic-gate  */
24057c478bd9Sstevel@tonic-gate static int
24067c478bd9Sstevel@tonic-gate convtoreg(off_t size)
24077c478bd9Sstevel@tonic-gate {
24087c478bd9Sstevel@tonic-gate 	if ((size > 0) && (dblock.dbuf.typeflag != '0') &&
24097c478bd9Sstevel@tonic-gate 	    (dblock.dbuf.typeflag != NULL) && (dblock.dbuf.typeflag != '1') &&
24107c478bd9Sstevel@tonic-gate 	    (dblock.dbuf.typeflag != '2') && (dblock.dbuf.typeflag != '3') &&
24117c478bd9Sstevel@tonic-gate 	    (dblock.dbuf.typeflag != '4') && (dblock.dbuf.typeflag != '5') &&
24127c478bd9Sstevel@tonic-gate 	    (dblock.dbuf.typeflag != '6') && (dblock.dbuf.typeflag != 'A') &&
24137c478bd9Sstevel@tonic-gate 	    (dblock.dbuf.typeflag != _XATTR_HDRTYPE) &&
24147c478bd9Sstevel@tonic-gate 	    (dblock.dbuf.typeflag != 'X')) {
24157c478bd9Sstevel@tonic-gate 		return (1);
24167c478bd9Sstevel@tonic-gate 	}
24177c478bd9Sstevel@tonic-gate 	return (0);
24187c478bd9Sstevel@tonic-gate }
24197c478bd9Sstevel@tonic-gate 
24207c478bd9Sstevel@tonic-gate static void
24217c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
24227c478bd9Sstevel@tonic-gate doxtract(char *argv[], int tbl_cnt)
24237c478bd9Sstevel@tonic-gate #else
24247c478bd9Sstevel@tonic-gate doxtract(char *argv[])
24257c478bd9Sstevel@tonic-gate #endif
24267c478bd9Sstevel@tonic-gate {
24277c478bd9Sstevel@tonic-gate 	struct	stat	xtractbuf;	/* stat on file after extracting */
24287c478bd9Sstevel@tonic-gate 	blkcnt_t blocks;
24297c478bd9Sstevel@tonic-gate 	off_t bytes;
24307c478bd9Sstevel@tonic-gate 	int ofile;
24317c478bd9Sstevel@tonic-gate 	int newfile;			/* Does the file already exist  */
24327c478bd9Sstevel@tonic-gate 	int xcnt = 0;			/* count # files extracted */
24337c478bd9Sstevel@tonic-gate 	int fcnt = 0;			/* count # files in argv list */
24347c478bd9Sstevel@tonic-gate 	int dir;
24357c478bd9Sstevel@tonic-gate 	int dirfd = -1;
24367c478bd9Sstevel@tonic-gate 	uid_t Uid;
24377c478bd9Sstevel@tonic-gate 	char *namep, *dirp, *comp, *linkp; /* for removing absolute paths */
24387c478bd9Sstevel@tonic-gate 	char dirname[PATH_MAX+1];
24397c478bd9Sstevel@tonic-gate 	char templink[PATH_MAX+1];	/* temp link with terminating NULL */
24407c478bd9Sstevel@tonic-gate 	char origdir[PATH_MAX+1];
24417c478bd9Sstevel@tonic-gate 	int once = 1;
24427c478bd9Sstevel@tonic-gate 	int error;
24437c478bd9Sstevel@tonic-gate 	int symflag;
24447c478bd9Sstevel@tonic-gate 	int want;
2445fa9e4066Sahrens 	acl_t	*aclp = NULL;	/* acl info */
244645916cd2Sjpk 	char dot[] = ".";		/* dirp for using realpath */
24477c478bd9Sstevel@tonic-gate 	timestruc_t	time_zero;	/* used for call to doDirTimes */
24487c478bd9Sstevel@tonic-gate 	int		dircreate;
24497c478bd9Sstevel@tonic-gate 	int convflag;
24507c478bd9Sstevel@tonic-gate 	time_zero.tv_sec = 0;
24517c478bd9Sstevel@tonic-gate 	time_zero.tv_nsec = 0;
24527c478bd9Sstevel@tonic-gate 
245345916cd2Sjpk 	/* reset Trusted Extensions variables */
245445916cd2Sjpk 	rpath_flag = 0;
245545916cd2Sjpk 	lk_rpath_flag = 0;
245645916cd2Sjpk 	dir_flag = 0;
245745916cd2Sjpk 	mld_flag = 0;
245845916cd2Sjpk 	bslundef(&bs_label);
245945916cd2Sjpk 	bsllow(&admin_low);
246045916cd2Sjpk 	bslhigh(&admin_high);
246145916cd2Sjpk 	orig_namep = 0;
246245916cd2Sjpk 
24637c478bd9Sstevel@tonic-gate 	dumping = 0;	/* for newvol(), et al:  we are not writing */
24647c478bd9Sstevel@tonic-gate 
24657c478bd9Sstevel@tonic-gate 	/*
24667c478bd9Sstevel@tonic-gate 	 * Count the number of files that are to be extracted
24677c478bd9Sstevel@tonic-gate 	 */
24687c478bd9Sstevel@tonic-gate 	Uid = getuid();
24697c478bd9Sstevel@tonic-gate 
24707c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
24717c478bd9Sstevel@tonic-gate 	initarg(argv, Filefile);
24727c478bd9Sstevel@tonic-gate 	while (nextarg() != NULL)
24737c478bd9Sstevel@tonic-gate 		++fcnt;
24747c478bd9Sstevel@tonic-gate 	fcnt += tbl_cnt;
24757c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
24767c478bd9Sstevel@tonic-gate 
24777c478bd9Sstevel@tonic-gate 	for (;;) {
24787c478bd9Sstevel@tonic-gate 		convflag = 0;
24797c478bd9Sstevel@tonic-gate 		symflag = 0;
24807c478bd9Sstevel@tonic-gate 		dir = 0;
24817c478bd9Sstevel@tonic-gate 		ofile = -1;
24827c478bd9Sstevel@tonic-gate 
24837c478bd9Sstevel@tonic-gate 		/* namep is set by wantit to point to the full name */
24847c478bd9Sstevel@tonic-gate 		if ((want = wantit(argv, &namep, &dirp, &comp)) == 0) {
24857c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
24867c478bd9Sstevel@tonic-gate 			if (xattrp != (struct xattr_buf *)NULL) {
24877c478bd9Sstevel@tonic-gate 				free(xattrhead);
24887c478bd9Sstevel@tonic-gate 				xattrp = NULL;
24897c478bd9Sstevel@tonic-gate 				xattr_linkp = NULL;
24907c478bd9Sstevel@tonic-gate 				xattrhead = NULL;
24917c478bd9Sstevel@tonic-gate 			}
24927c478bd9Sstevel@tonic-gate #endif
24937c478bd9Sstevel@tonic-gate 			continue;
24947c478bd9Sstevel@tonic-gate 		}
24957c478bd9Sstevel@tonic-gate 		if (want == -1)
24967c478bd9Sstevel@tonic-gate 			break;
24977c478bd9Sstevel@tonic-gate 
249845916cd2Sjpk /* Trusted Extensions */
249945916cd2Sjpk 		/*
250045916cd2Sjpk 		 * During tar extract (x):
250145916cd2Sjpk 		 * If the pathname of the restored file has been
250245916cd2Sjpk 		 * reconstructed from the ancillary file,
250345916cd2Sjpk 		 * use it to process the normal file.
250445916cd2Sjpk 		 */
250545916cd2Sjpk 		if (mld_flag) {		/* Skip over .MLD. directory */
250645916cd2Sjpk 			mld_flag = 0;
250745916cd2Sjpk 			passtape();
250845916cd2Sjpk 			continue;
250945916cd2Sjpk 		}
251045916cd2Sjpk 		orig_namep = namep;	/* save original */
251145916cd2Sjpk 		if (rpath_flag) {
251245916cd2Sjpk 			namep = real_path;	/* use zone path */
251345916cd2Sjpk 			comp = real_path;	/* use zone path */
251445916cd2Sjpk 			dirp = dot;		/* work from the top */
251545916cd2Sjpk 			rpath_flag = 0;		/* reset */
251645916cd2Sjpk 		}
251745916cd2Sjpk 
25187c478bd9Sstevel@tonic-gate 		if (dirfd != -1)
25197c478bd9Sstevel@tonic-gate 			(void) close(dirfd);
25207c478bd9Sstevel@tonic-gate 
25217c478bd9Sstevel@tonic-gate 		(void) strcpy(&dirname[0], namep);
25227c478bd9Sstevel@tonic-gate 		dircreate = checkdir(&dirname[0]);
25237c478bd9Sstevel@tonic-gate 
25247c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
25257c478bd9Sstevel@tonic-gate 		if (xattrp != (struct xattr_buf *)NULL) {
25267c478bd9Sstevel@tonic-gate 			dirfd = attropen(dirp, ".", O_RDONLY);
25277c478bd9Sstevel@tonic-gate 		} else {
25287c478bd9Sstevel@tonic-gate 			dirfd = open(dirp, O_RDONLY);
25297c478bd9Sstevel@tonic-gate 		}
25307c478bd9Sstevel@tonic-gate #else
25317c478bd9Sstevel@tonic-gate 		dirfd = open(dirp, O_RDONLY);
25327c478bd9Sstevel@tonic-gate #endif
25337c478bd9Sstevel@tonic-gate 
25347c478bd9Sstevel@tonic-gate 		if (dirfd == -1) {
25357c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
25367c478bd9Sstevel@tonic-gate 			if (xattrp) {
25377c478bd9Sstevel@tonic-gate 				dirfd = retry_attrdir_open(dirp);
25387c478bd9Sstevel@tonic-gate 			}
25397c478bd9Sstevel@tonic-gate #endif
25407c478bd9Sstevel@tonic-gate 			if (dirfd == -1) {
2541d2443e76Smarks #if defined(O_XATTR)
2542d2443e76Smarks 				if (xattrp) {
2543d2443e76Smarks 					(void) fprintf(vfile,
2544d2443e76Smarks 					    gettext("tar: cannot open "
2545d2443e76Smarks 					    "attribute %s of file %s: %s\n"),
2546d2443e76Smarks 					    xattraname, dirp, strerror(errno));
2547d2443e76Smarks 					/*
2548d2443e76Smarks 					 * Reset typeflag back to real
2549d2443e76Smarks 					 * value so passtape will skip
2550d2443e76Smarks 					 * ahead correctly.
2551d2443e76Smarks 					 */
2552d2443e76Smarks 					dblock.dbuf.typeflag = _XATTR_HDRTYPE;
2553d2443e76Smarks 					free(xattrhead);
2554d2443e76Smarks 					xattrp = NULL;
2555d2443e76Smarks 					xattr_linkp = NULL;
2556d2443e76Smarks 					xattrhead = NULL;
2557d2443e76Smarks 				} else {
255845916cd2Sjpk 					(void) fprintf(vfile,
2559d2443e76Smarks 					    gettext("tar: cannot open %s %s\n"),
2560d2443e76Smarks 					    dirp, strerror(errno));
2561d2443e76Smarks 				}
2562d2443e76Smarks #else
256345916cd2Sjpk 				(void) fprintf(vfile,
2564d2443e76Smarks 				    gettext("tar: cannot open %s %s\n"),
2565d2443e76Smarks 				    dirp, strerror(errno));
2566d2443e76Smarks #endif
25677c478bd9Sstevel@tonic-gate 				passtape();
25687c478bd9Sstevel@tonic-gate 				continue;
25697c478bd9Sstevel@tonic-gate 			}
25707c478bd9Sstevel@tonic-gate 		}
25717c478bd9Sstevel@tonic-gate 
25727c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_LINKPATH)
25737c478bd9Sstevel@tonic-gate 			(void) strcpy(templink, Xtarhdr.x_linkpath);
25747c478bd9Sstevel@tonic-gate 		else {
25757c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
25767c478bd9Sstevel@tonic-gate 			if (xattrp && dblock.dbuf.typeflag == '1') {
25777c478bd9Sstevel@tonic-gate 				(void) sprintf(templink, "%.*s", NAMSIZ,
25787c478bd9Sstevel@tonic-gate 				    xattrp->h_names);
25797c478bd9Sstevel@tonic-gate 			} else {
25807c478bd9Sstevel@tonic-gate 				(void) sprintf(templink, "%.*s", NAMSIZ,
25817c478bd9Sstevel@tonic-gate 				    dblock.dbuf.linkname);
25827c478bd9Sstevel@tonic-gate 			}
25837c478bd9Sstevel@tonic-gate #else
25847c478bd9Sstevel@tonic-gate 			(void) sprintf(templink, "%.*s", NAMSIZ,
25857c478bd9Sstevel@tonic-gate 			    dblock.dbuf.linkname);
25867c478bd9Sstevel@tonic-gate #endif
25877c478bd9Sstevel@tonic-gate 		}
25887c478bd9Sstevel@tonic-gate 
25897c478bd9Sstevel@tonic-gate 		if (Fflag) {
25907c478bd9Sstevel@tonic-gate 			char *s;
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate 			if ((s = strrchr(namep, '/')) == 0)
25937c478bd9Sstevel@tonic-gate 				s = namep;
25947c478bd9Sstevel@tonic-gate 
25957c478bd9Sstevel@tonic-gate 			else
25967c478bd9Sstevel@tonic-gate 				s++;
25977c478bd9Sstevel@tonic-gate 			if (checkf(s, stbuf.st_mode, Fflag) == 0) {
25987c478bd9Sstevel@tonic-gate 				passtape();
25997c478bd9Sstevel@tonic-gate 				continue;
26007c478bd9Sstevel@tonic-gate 			}
26017c478bd9Sstevel@tonic-gate 		}
26027c478bd9Sstevel@tonic-gate 
26037c478bd9Sstevel@tonic-gate 		if (checkw('x', namep) == 0) {
26047c478bd9Sstevel@tonic-gate 			passtape();
26057c478bd9Sstevel@tonic-gate 			continue;
26067c478bd9Sstevel@tonic-gate 		}
26077c478bd9Sstevel@tonic-gate 		if (once) {
26087c478bd9Sstevel@tonic-gate 			if (strcmp(dblock.dbuf.magic, magic_type) == 0) {
26097c478bd9Sstevel@tonic-gate 				if (geteuid() == (uid_t)0) {
26107c478bd9Sstevel@tonic-gate 					checkflag = 1;
26117c478bd9Sstevel@tonic-gate 					pflag = 1;
26127c478bd9Sstevel@tonic-gate 				} else {
26137c478bd9Sstevel@tonic-gate 					/* get file creation mask */
26147c478bd9Sstevel@tonic-gate 					Oumask = umask(0);
26157c478bd9Sstevel@tonic-gate 					(void) umask(Oumask);
26167c478bd9Sstevel@tonic-gate 				}
26177c478bd9Sstevel@tonic-gate 				once = 0;
26187c478bd9Sstevel@tonic-gate 			} else {
26197c478bd9Sstevel@tonic-gate 				if (geteuid() == (uid_t)0) {
26207c478bd9Sstevel@tonic-gate 					pflag = 1;
26217c478bd9Sstevel@tonic-gate 					checkflag = 2;
26227c478bd9Sstevel@tonic-gate 				}
26237c478bd9Sstevel@tonic-gate 				if (!pflag) {
26247c478bd9Sstevel@tonic-gate 					/* get file creation mask */
26257c478bd9Sstevel@tonic-gate 					Oumask = umask(0);
26267c478bd9Sstevel@tonic-gate 					(void) umask(Oumask);
26277c478bd9Sstevel@tonic-gate 				}
26287c478bd9Sstevel@tonic-gate 				once = 0;
26297c478bd9Sstevel@tonic-gate 			}
26307c478bd9Sstevel@tonic-gate 		}
26317c478bd9Sstevel@tonic-gate 
26327c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
26337c478bd9Sstevel@tonic-gate 		/*
26347c478bd9Sstevel@tonic-gate 		 * Handle extraction of hidden attr dir.
26357c478bd9Sstevel@tonic-gate 		 * Dir is automatically created, we only
26367c478bd9Sstevel@tonic-gate 		 * need to update mode and perm's.
26377c478bd9Sstevel@tonic-gate 		 */
26387c478bd9Sstevel@tonic-gate 		if ((xattrp != (struct xattr_buf *)NULL) && Hiddendir == 1) {
26397c478bd9Sstevel@tonic-gate 			if (fchownat(dirfd, ".", stbuf.st_uid,
26407c478bd9Sstevel@tonic-gate 			    stbuf.st_gid, 0) != 0) {
26417c478bd9Sstevel@tonic-gate 				vperror(0, gettext(
26427c478bd9Sstevel@tonic-gate 				    "%s: failed to set ownership of attribute"
26437c478bd9Sstevel@tonic-gate 				    " directory"), namep);
26447c478bd9Sstevel@tonic-gate 			}
26457c478bd9Sstevel@tonic-gate 
26467c478bd9Sstevel@tonic-gate 			if (fchmod(dirfd, stbuf.st_mode) != 0) {
26477c478bd9Sstevel@tonic-gate 				vperror(0, gettext(
26487c478bd9Sstevel@tonic-gate 				    "%s: failed to set permissions of"
26497c478bd9Sstevel@tonic-gate 				    " attribute directory"), namep);
26507c478bd9Sstevel@tonic-gate 			}
26517c478bd9Sstevel@tonic-gate 			goto filedone;
26527c478bd9Sstevel@tonic-gate 		}
26537c478bd9Sstevel@tonic-gate #endif
26547c478bd9Sstevel@tonic-gate 
26557c478bd9Sstevel@tonic-gate 		if (dircreate && (!is_posix || dblock.dbuf.typeflag == '5')) {
26567c478bd9Sstevel@tonic-gate 			dir = 1;
26577c478bd9Sstevel@tonic-gate 			if (vflag) {
26587c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "x %s, 0 bytes, ",
26597c478bd9Sstevel@tonic-gate 				    &dirname[0]);
26607c478bd9Sstevel@tonic-gate 				if (NotTape)
26617c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, "0K\n");
26627c478bd9Sstevel@tonic-gate 				else
26637c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext("%"
26647c478bd9Sstevel@tonic-gate 					    FMT_blkcnt_t " tape blocks\n"),
26657c478bd9Sstevel@tonic-gate 					    (blkcnt_t)0);
26667c478bd9Sstevel@tonic-gate 			}
26677c478bd9Sstevel@tonic-gate 			goto filedone;
26687c478bd9Sstevel@tonic-gate 		}
26697c478bd9Sstevel@tonic-gate 
26707c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '6') {	/* FIFO */
26717c478bd9Sstevel@tonic-gate 			if (rmdir(namep) < 0) {
26727c478bd9Sstevel@tonic-gate 				if (errno == ENOTDIR)
26737c478bd9Sstevel@tonic-gate 					(void) unlink(namep);
26747c478bd9Sstevel@tonic-gate 			}
26757c478bd9Sstevel@tonic-gate 			linkp = templink;
26767c478bd9Sstevel@tonic-gate 			if (*linkp !=  NULL) {
26777c478bd9Sstevel@tonic-gate 				if (Aflag && *linkp == '/')
26787c478bd9Sstevel@tonic-gate 					linkp++;
26797c478bd9Sstevel@tonic-gate 				if (link(linkp, namep) < 0) {
26807c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
26817c478bd9Sstevel@tonic-gate 					    "tar: %s: cannot link\n"), namep);
26827c478bd9Sstevel@tonic-gate 					continue;
26837c478bd9Sstevel@tonic-gate 				}
26847c478bd9Sstevel@tonic-gate 				if (vflag)
26857c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext(
26867c478bd9Sstevel@tonic-gate 					    "%s linked to %s\n"), namep, linkp);
26877c478bd9Sstevel@tonic-gate 				xcnt++;	 /* increment # files extracted */
26887c478bd9Sstevel@tonic-gate 				continue;
26897c478bd9Sstevel@tonic-gate 			}
26907c478bd9Sstevel@tonic-gate 			if (mknod(namep, (int)(Gen.g_mode|S_IFIFO),
26917c478bd9Sstevel@tonic-gate 			    (int)Gen.g_devmajor) < 0) {
26927c478bd9Sstevel@tonic-gate 				vperror(0, gettext("%s: mknod failed"), namep);
26937c478bd9Sstevel@tonic-gate 				continue;
26947c478bd9Sstevel@tonic-gate 			}
26957c478bd9Sstevel@tonic-gate 			bytes = stbuf.st_size;
26967c478bd9Sstevel@tonic-gate 			blocks = TBLOCKS(bytes);
26977c478bd9Sstevel@tonic-gate 			if (vflag) {
26987c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "x %s, %" FMT_off_t
26997c478bd9Sstevel@tonic-gate 				    " bytes, ", namep, bytes);
27007c478bd9Sstevel@tonic-gate 				if (NotTape)
27017c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, "%" FMT_blkcnt_t
27027c478bd9Sstevel@tonic-gate 					    "K\n", K(blocks));
27037c478bd9Sstevel@tonic-gate 				else
27047c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext("%"
27057c478bd9Sstevel@tonic-gate 					    FMT_blkcnt_t " tape blocks\n"),
27067c478bd9Sstevel@tonic-gate 					    blocks);
27077c478bd9Sstevel@tonic-gate 			}
27087c478bd9Sstevel@tonic-gate 			goto filedone;
27097c478bd9Sstevel@tonic-gate 		}
27107c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '3' && !Uid) { /* CHAR SPECIAL */
27117c478bd9Sstevel@tonic-gate 			if (rmdir(namep) < 0) {
27127c478bd9Sstevel@tonic-gate 				if (errno == ENOTDIR)
27137c478bd9Sstevel@tonic-gate 					(void) unlink(namep);
27147c478bd9Sstevel@tonic-gate 			}
27157c478bd9Sstevel@tonic-gate 			linkp = templink;
27167c478bd9Sstevel@tonic-gate 			if (*linkp != NULL) {
27177c478bd9Sstevel@tonic-gate 				if (Aflag && *linkp == '/')
27187c478bd9Sstevel@tonic-gate 					linkp++;
27197c478bd9Sstevel@tonic-gate 				if (link(linkp, namep) < 0) {
27207c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
27217c478bd9Sstevel@tonic-gate 					    "tar: %s: cannot link\n"), namep);
27227c478bd9Sstevel@tonic-gate 					continue;
27237c478bd9Sstevel@tonic-gate 				}
27247c478bd9Sstevel@tonic-gate 				if (vflag)
27257c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext(
27267c478bd9Sstevel@tonic-gate 					    "%s linked to %s\n"), namep, linkp);
27277c478bd9Sstevel@tonic-gate 				xcnt++;	 /* increment # files extracted */
27287c478bd9Sstevel@tonic-gate 				continue;
27297c478bd9Sstevel@tonic-gate 			}
27307c478bd9Sstevel@tonic-gate 			if (mknod(namep, (int)(Gen.g_mode|S_IFCHR),
27317c478bd9Sstevel@tonic-gate 			    (int)makedev(Gen.g_devmajor, Gen.g_devminor)) < 0) {
27327c478bd9Sstevel@tonic-gate 				vperror(0, gettext(
27337c478bd9Sstevel@tonic-gate 				"%s: mknod failed"), namep);
27347c478bd9Sstevel@tonic-gate 				continue;
27357c478bd9Sstevel@tonic-gate 			}
27367c478bd9Sstevel@tonic-gate 			bytes = stbuf.st_size;
27377c478bd9Sstevel@tonic-gate 			blocks = TBLOCKS(bytes);
27387c478bd9Sstevel@tonic-gate 			if (vflag) {
27397c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "x %s, %" FMT_off_t
27407c478bd9Sstevel@tonic-gate 				    " bytes, ", namep, bytes);
27417c478bd9Sstevel@tonic-gate 				if (NotTape)
27427c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, "%" FMT_blkcnt_t
27437c478bd9Sstevel@tonic-gate 					    "K\n", K(blocks));
27447c478bd9Sstevel@tonic-gate 				else
27457c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext("%"
27467c478bd9Sstevel@tonic-gate 					    FMT_blkcnt_t " tape blocks\n"),
27477c478bd9Sstevel@tonic-gate 					    blocks);
27487c478bd9Sstevel@tonic-gate 			}
27497c478bd9Sstevel@tonic-gate 			goto filedone;
27507c478bd9Sstevel@tonic-gate 		} else if (dblock.dbuf.typeflag == '3' && Uid) {
27517c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
27527c478bd9Sstevel@tonic-gate 			    "Can't create special %s\n"), namep);
27537c478bd9Sstevel@tonic-gate 			continue;
27547c478bd9Sstevel@tonic-gate 		}
27557c478bd9Sstevel@tonic-gate 
27567c478bd9Sstevel@tonic-gate 		/* BLOCK SPECIAL */
27577c478bd9Sstevel@tonic-gate 
27587c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '4' && !Uid) {
27597c478bd9Sstevel@tonic-gate 			if (rmdir(namep) < 0) {
27607c478bd9Sstevel@tonic-gate 				if (errno == ENOTDIR)
27617c478bd9Sstevel@tonic-gate 					(void) unlink(namep);
27627c478bd9Sstevel@tonic-gate 			}
27637c478bd9Sstevel@tonic-gate 			linkp = templink;
27647c478bd9Sstevel@tonic-gate 			if (*linkp != NULL) {
27657c478bd9Sstevel@tonic-gate 				if (Aflag && *linkp == '/')
27667c478bd9Sstevel@tonic-gate 					linkp++;
27677c478bd9Sstevel@tonic-gate 				if (link(linkp, namep) < 0) {
27687c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
27697c478bd9Sstevel@tonic-gate 					    "tar: %s: cannot link\n"), namep);
27707c478bd9Sstevel@tonic-gate 					continue;
27717c478bd9Sstevel@tonic-gate 				}
27727c478bd9Sstevel@tonic-gate 				if (vflag)
27737c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext(
27747c478bd9Sstevel@tonic-gate 					    "%s linked to %s\n"), namep, linkp);
27757c478bd9Sstevel@tonic-gate 				xcnt++;	 /* increment # files extracted */
27767c478bd9Sstevel@tonic-gate 				continue;
27777c478bd9Sstevel@tonic-gate 			}
27787c478bd9Sstevel@tonic-gate 			if (mknod(namep, (int)(Gen.g_mode|S_IFBLK),
27797c478bd9Sstevel@tonic-gate 			    (int)makedev(Gen.g_devmajor, Gen.g_devminor)) < 0) {
27807c478bd9Sstevel@tonic-gate 				vperror(0, gettext("%s: mknod failed"), namep);
27817c478bd9Sstevel@tonic-gate 				continue;
27827c478bd9Sstevel@tonic-gate 			}
27837c478bd9Sstevel@tonic-gate 			bytes = stbuf.st_size;
27847c478bd9Sstevel@tonic-gate 			blocks = TBLOCKS(bytes);
27857c478bd9Sstevel@tonic-gate 			if (vflag) {
27867c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("x %s, %"
27877c478bd9Sstevel@tonic-gate 				    FMT_off_t " bytes, "), namep, bytes);
27887c478bd9Sstevel@tonic-gate 				if (NotTape)
27897c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, "%" FMT_blkcnt_t
27907c478bd9Sstevel@tonic-gate 					    "K\n", K(blocks));
27917c478bd9Sstevel@tonic-gate 				else
27927c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext("%"
27937c478bd9Sstevel@tonic-gate 					    FMT_blkcnt_t " tape blocks\n"),
27947c478bd9Sstevel@tonic-gate 					    blocks);
27957c478bd9Sstevel@tonic-gate 			}
27967c478bd9Sstevel@tonic-gate 			goto filedone;
27977c478bd9Sstevel@tonic-gate 		} else if (dblock.dbuf.typeflag == '4' && Uid) {
27987c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
27997c478bd9Sstevel@tonic-gate 			    gettext("Can't create special %s\n"), namep);
28007c478bd9Sstevel@tonic-gate 			continue;
28017c478bd9Sstevel@tonic-gate 		}
28027c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '2') {	/* symlink */
280345916cd2Sjpk 			if ((Tflag) && (lk_rpath_flag == 1))
280445916cd2Sjpk 				linkp = lk_real_path;
280545916cd2Sjpk 			else
28067c478bd9Sstevel@tonic-gate 				linkp = templink;
28077c478bd9Sstevel@tonic-gate 			if (Aflag && *linkp == '/')
28087c478bd9Sstevel@tonic-gate 				linkp++;
28097c478bd9Sstevel@tonic-gate 			if (rmdir(namep) < 0) {
28107c478bd9Sstevel@tonic-gate 				if (errno == ENOTDIR)
28117c478bd9Sstevel@tonic-gate 					(void) unlink(namep);
28127c478bd9Sstevel@tonic-gate 			}
28137c478bd9Sstevel@tonic-gate 			if (symlink(linkp, namep) < 0) {
28147c478bd9Sstevel@tonic-gate 				vperror(0, gettext("%s: symbolic link failed"),
28157c478bd9Sstevel@tonic-gate 				    namep);
28167c478bd9Sstevel@tonic-gate 				continue;
28177c478bd9Sstevel@tonic-gate 			}
28187c478bd9Sstevel@tonic-gate 			if (vflag)
28197c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext(
28207c478bd9Sstevel@tonic-gate 				    "x %s symbolic link to %s\n"),
28217c478bd9Sstevel@tonic-gate 				    namep, linkp);
28227c478bd9Sstevel@tonic-gate 
28237c478bd9Sstevel@tonic-gate 			symflag = AT_SYMLINK_NOFOLLOW;
28247c478bd9Sstevel@tonic-gate 			goto filedone;
28257c478bd9Sstevel@tonic-gate 		}
28267c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '1') {
28277c478bd9Sstevel@tonic-gate 			linkp = templink;
28287c478bd9Sstevel@tonic-gate 			if (Aflag && *linkp == '/')
28297c478bd9Sstevel@tonic-gate 				linkp++;
28307c478bd9Sstevel@tonic-gate 			if (unlinkat(dirfd, comp, AT_REMOVEDIR) < 0) {
28317c478bd9Sstevel@tonic-gate 				if (errno == ENOTDIR)
28327c478bd9Sstevel@tonic-gate 					(void) unlinkat(dirfd, comp, 0);
28337c478bd9Sstevel@tonic-gate 			}
28347c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
28357c478bd9Sstevel@tonic-gate 			if (xattrp && xattr_linkp) {
28367c478bd9Sstevel@tonic-gate 				if (getcwd(origdir, (PATH_MAX+1)) ==
28377c478bd9Sstevel@tonic-gate 				    (char *)NULL) {
28387c478bd9Sstevel@tonic-gate 					vperror(0, gettext(
28397c478bd9Sstevel@tonic-gate 					    "A parent directory cannot"
28407c478bd9Sstevel@tonic-gate 					    " be read"));
28417c478bd9Sstevel@tonic-gate 					exit(1);
28427c478bd9Sstevel@tonic-gate 				}
28437c478bd9Sstevel@tonic-gate 
28447c478bd9Sstevel@tonic-gate 				if (fchdir(dirfd) < 0) {
28457c478bd9Sstevel@tonic-gate 					vperror(0, gettext(
28467c478bd9Sstevel@tonic-gate 					    "Cannot fchdir to attribute "
28477c478bd9Sstevel@tonic-gate 					    "directory"));
28487c478bd9Sstevel@tonic-gate 					exit(1);
28497c478bd9Sstevel@tonic-gate 				}
28507c478bd9Sstevel@tonic-gate 
28517c478bd9Sstevel@tonic-gate 				error = link(xattr_linkaname, xattraname);
28527c478bd9Sstevel@tonic-gate 				if (chdir(origdir) < 0) {
28537c478bd9Sstevel@tonic-gate 					vperror(0, gettext(
28547c478bd9Sstevel@tonic-gate 					    "Cannot chdir out of attribute "
28557c478bd9Sstevel@tonic-gate 					    "directory"));
28567c478bd9Sstevel@tonic-gate 					exit(1);
28577c478bd9Sstevel@tonic-gate 				}
28587c478bd9Sstevel@tonic-gate 			} else {
28597c478bd9Sstevel@tonic-gate 				error = link(linkp, namep);
28607c478bd9Sstevel@tonic-gate 			}
28617c478bd9Sstevel@tonic-gate #else
28627c478bd9Sstevel@tonic-gate 			error = link(linkp, namep);
28637c478bd9Sstevel@tonic-gate #endif
28647c478bd9Sstevel@tonic-gate 
28657c478bd9Sstevel@tonic-gate 			if (error < 0) {
28667c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
28677c478bd9Sstevel@tonic-gate 				    "tar: %s%s%s: cannot link\n"),
28687c478bd9Sstevel@tonic-gate 				    namep, (xattr_linkp != NULL) ?
28697c478bd9Sstevel@tonic-gate 				    gettext(" attribute ") : "",
28707c478bd9Sstevel@tonic-gate 				    (xattr_linkp != NULL) ?
28717c478bd9Sstevel@tonic-gate 				    xattraname : "");
28727c478bd9Sstevel@tonic-gate 				continue;
28737c478bd9Sstevel@tonic-gate 			}
28747c478bd9Sstevel@tonic-gate 			if (vflag)
28757c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext(
28767c478bd9Sstevel@tonic-gate 				    "%s%s%s linked to %s%s%s\n"), namep,
28777c478bd9Sstevel@tonic-gate 				    (xattr_linkp != NULL) ?
28787c478bd9Sstevel@tonic-gate 				    gettext(" attribute ") : "",
28797c478bd9Sstevel@tonic-gate 				    (xattr_linkp != NULL) ?
28807c478bd9Sstevel@tonic-gate 				    xattr_linkaname : "",
28817c478bd9Sstevel@tonic-gate 				    linkp, (xattr_linkp != NULL) ?
28827c478bd9Sstevel@tonic-gate 				    gettext(" attribute ") : "",
28837c478bd9Sstevel@tonic-gate 				    (xattr_linkp != NULL) ?
28847c478bd9Sstevel@tonic-gate 				    xattraname : "");
28857c478bd9Sstevel@tonic-gate 			xcnt++;		/* increment # files extracted */
28867c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
28877c478bd9Sstevel@tonic-gate 			if (xattrp != (struct xattr_buf *)NULL) {
28887c478bd9Sstevel@tonic-gate 				free(xattrhead);
28897c478bd9Sstevel@tonic-gate 				xattrp = NULL;
28907c478bd9Sstevel@tonic-gate 				xattr_linkp = NULL;
28917c478bd9Sstevel@tonic-gate 				xattrhead = NULL;
28927c478bd9Sstevel@tonic-gate 			}
28937c478bd9Sstevel@tonic-gate #endif
28947c478bd9Sstevel@tonic-gate 			continue;
28957c478bd9Sstevel@tonic-gate 		}
28967c478bd9Sstevel@tonic-gate 
28977c478bd9Sstevel@tonic-gate 		/* REGULAR FILES */
28987c478bd9Sstevel@tonic-gate 
28997c478bd9Sstevel@tonic-gate 		if (convtoreg(stbuf.st_size)) {
29007c478bd9Sstevel@tonic-gate 			convflag = 1;
29017c478bd9Sstevel@tonic-gate 			if (errflag) {
29027c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
29037c478bd9Sstevel@tonic-gate 				    "tar: %s: typeflag '%c' not recognized\n"),
29047c478bd9Sstevel@tonic-gate 				    namep, dblock.dbuf.typeflag);
29057c478bd9Sstevel@tonic-gate 				done(1);
29067c478bd9Sstevel@tonic-gate 			} else {
29077c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
29087c478bd9Sstevel@tonic-gate 				    "tar: %s: typeflag '%c' not recognized, "
29097c478bd9Sstevel@tonic-gate 				    "converting to regular file\n"), namep,
29107c478bd9Sstevel@tonic-gate 				    dblock.dbuf.typeflag);
29117c478bd9Sstevel@tonic-gate 				Errflg = 1;
29127c478bd9Sstevel@tonic-gate 			}
29137c478bd9Sstevel@tonic-gate 		}
29147c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '0' ||
29157c478bd9Sstevel@tonic-gate 		    dblock.dbuf.typeflag == NULL || convflag) {
29162c0f0499Slovely 			delete_target(dirfd, comp, namep);
29177c478bd9Sstevel@tonic-gate 			linkp = templink;
29187c478bd9Sstevel@tonic-gate 			if (*linkp != NULL) {
29197c478bd9Sstevel@tonic-gate 				if (Aflag && *linkp == '/')
29207c478bd9Sstevel@tonic-gate 					linkp++;
29217c478bd9Sstevel@tonic-gate 				if (link(linkp, comp) < 0) {
29227c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
29237c478bd9Sstevel@tonic-gate 					    "tar: %s: cannot link\n"), namep);
29247c478bd9Sstevel@tonic-gate 					continue;
29257c478bd9Sstevel@tonic-gate 				}
29267c478bd9Sstevel@tonic-gate 				if (vflag)
29277c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext(
29287c478bd9Sstevel@tonic-gate 					    "%s linked to %s\n"), comp, linkp);
29297c478bd9Sstevel@tonic-gate 				xcnt++;	 /* increment # files extracted */
29307c478bd9Sstevel@tonic-gate 				continue;
29317c478bd9Sstevel@tonic-gate 			}
29327c478bd9Sstevel@tonic-gate 		newfile = ((fstatat(dirfd, comp,
29337c478bd9Sstevel@tonic-gate 		    &xtractbuf, 0) == -1) ? TRUE : FALSE);
29347c478bd9Sstevel@tonic-gate 		if ((ofile = openat(dirfd, comp, O_RDWR|O_CREAT|O_TRUNC,
29357c478bd9Sstevel@tonic-gate 		    stbuf.st_mode & MODEMASK)) < 0) {
29367c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
29377c478bd9Sstevel@tonic-gate 			    "tar: %s - cannot create\n"), comp);
29387c478bd9Sstevel@tonic-gate 			if (errflag)
29397c478bd9Sstevel@tonic-gate 				done(1);
29407c478bd9Sstevel@tonic-gate 			else
29417c478bd9Sstevel@tonic-gate 				Errflg = 1;
29427c478bd9Sstevel@tonic-gate 			passtape();
29437c478bd9Sstevel@tonic-gate 			continue;
29447c478bd9Sstevel@tonic-gate 		}
29457c478bd9Sstevel@tonic-gate 
294645916cd2Sjpk 		if (Tflag && (check_ext_attr(namep) == 0)) {
294745916cd2Sjpk 			if (errflag)
294845916cd2Sjpk 				done(1);
294945916cd2Sjpk 			else
295045916cd2Sjpk 				Errflg = 1;
295145916cd2Sjpk 			passtape();
295245916cd2Sjpk 			continue;
295345916cd2Sjpk 		}
295445916cd2Sjpk 
29557c478bd9Sstevel@tonic-gate 		if (extno != 0) {	/* file is in pieces */
29567c478bd9Sstevel@tonic-gate 			if (extotal < 1 || extotal > MAXEXT)
29577c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
29587c478bd9Sstevel@tonic-gate 				    "tar: ignoring bad extent info for %s\n"),
29597c478bd9Sstevel@tonic-gate 				    comp);
29607c478bd9Sstevel@tonic-gate 			else {
29617c478bd9Sstevel@tonic-gate 				xsfile(ofile);	/* extract it */
29627c478bd9Sstevel@tonic-gate 				goto filedone;
29637c478bd9Sstevel@tonic-gate 			}
29647c478bd9Sstevel@tonic-gate 		}
29657c478bd9Sstevel@tonic-gate 		extno = 0;	/* let everyone know file is not split */
29667c478bd9Sstevel@tonic-gate 		bytes = stbuf.st_size;
29677c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(bytes);
29687c478bd9Sstevel@tonic-gate 		if (vflag) {
29697c478bd9Sstevel@tonic-gate 			(void) fprintf(vfile,
29707c478bd9Sstevel@tonic-gate 			    "x %s%s%s, %" FMT_off_t " bytes, ",
29717c478bd9Sstevel@tonic-gate 			    (xattrp == NULL) ? "" : dirp,
29727c478bd9Sstevel@tonic-gate 			    (xattrp == NULL) ? "" : gettext(" attribute "),
29737c478bd9Sstevel@tonic-gate 			    (xattrp == NULL) ? namep : comp, bytes);
29747c478bd9Sstevel@tonic-gate 			if (NotTape)
29757c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "%" FMT_blkcnt_t "K\n",
29767c478bd9Sstevel@tonic-gate 				    K(blocks));
29777c478bd9Sstevel@tonic-gate 			else
29787c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("%"
29797c478bd9Sstevel@tonic-gate 				    FMT_blkcnt_t " tape blocks\n"), blocks);
29807c478bd9Sstevel@tonic-gate 		}
29817c478bd9Sstevel@tonic-gate 
29827c478bd9Sstevel@tonic-gate 		xblocks(bytes, ofile);
29837c478bd9Sstevel@tonic-gate filedone:
29847c478bd9Sstevel@tonic-gate 		if (mflag == 0 && !symflag) {
29857c478bd9Sstevel@tonic-gate 			if (dir)
29867c478bd9Sstevel@tonic-gate 				doDirTimes(namep, stbuf.st_mtim);
29877c478bd9Sstevel@tonic-gate 			else
29887c478bd9Sstevel@tonic-gate 				setPathTimes(dirfd, comp, stbuf.st_mtim);
29897c478bd9Sstevel@tonic-gate 		}
29907c478bd9Sstevel@tonic-gate 
29917c478bd9Sstevel@tonic-gate 		/* moved this code from above */
29927c478bd9Sstevel@tonic-gate 		if (pflag && !symflag && Hiddendir == 0) {
29937c478bd9Sstevel@tonic-gate 			if (xattrp != (struct xattr_buf *)NULL)
29947c478bd9Sstevel@tonic-gate 				(void) fchmod(ofile, stbuf.st_mode & MODEMASK);
29957c478bd9Sstevel@tonic-gate 			else
29967c478bd9Sstevel@tonic-gate 				(void) chmod(namep, stbuf.st_mode & MODEMASK);
29977c478bd9Sstevel@tonic-gate 		}
29987c478bd9Sstevel@tonic-gate 
29997c478bd9Sstevel@tonic-gate 
30007c478bd9Sstevel@tonic-gate 		/*
30017c478bd9Sstevel@tonic-gate 		 * Because ancillary file preceeds the normal file,
30027c478bd9Sstevel@tonic-gate 		 * acl info may have been retrieved (in aclp).
30037c478bd9Sstevel@tonic-gate 		 * All file types are directed here (go filedone).
30047c478bd9Sstevel@tonic-gate 		 * Always restore ACLs if there are ACLs.
30057c478bd9Sstevel@tonic-gate 		 */
30067c478bd9Sstevel@tonic-gate 		if (aclp != NULL) {
30077c478bd9Sstevel@tonic-gate 			int ret;
30087c478bd9Sstevel@tonic-gate 
30097c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
30107c478bd9Sstevel@tonic-gate 			if (xattrp != (struct xattr_buf *)NULL) {
30117c478bd9Sstevel@tonic-gate 				if (Hiddendir)
3012fa9e4066Sahrens 					ret = facl_set(dirfd, aclp);
30137c478bd9Sstevel@tonic-gate 				else
3014fa9e4066Sahrens 					ret = facl_set(ofile, aclp);
30157c478bd9Sstevel@tonic-gate 			} else {
3016fa9e4066Sahrens 				ret = acl_set(namep, aclp);
30177c478bd9Sstevel@tonic-gate 			}
30187c478bd9Sstevel@tonic-gate #else
301945916cd2Sjpk 			ret = acl_set(namep, aclp);
30207c478bd9Sstevel@tonic-gate #endif
30217c478bd9Sstevel@tonic-gate 			if (ret < 0) {
30227c478bd9Sstevel@tonic-gate 				if (pflag) {
30237c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
30247c478bd9Sstevel@tonic-gate 					    "%s: failed to set acl entries\n"),
30257c478bd9Sstevel@tonic-gate 					    namep);
30267c478bd9Sstevel@tonic-gate 				}
30277c478bd9Sstevel@tonic-gate 				/* else: silent and continue */
30287c478bd9Sstevel@tonic-gate 			}
3029fa9e4066Sahrens 			acl_free(aclp);
30307c478bd9Sstevel@tonic-gate 			aclp = NULL;
30317c478bd9Sstevel@tonic-gate 		}
30327c478bd9Sstevel@tonic-gate 
30337c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
30347c478bd9Sstevel@tonic-gate 		if (xattrp != (struct xattr_buf *)NULL) {
30357c478bd9Sstevel@tonic-gate 			free(xattrhead);
30367c478bd9Sstevel@tonic-gate 			xattrp = NULL;
30377c478bd9Sstevel@tonic-gate 			xattr_linkp = NULL;
30387c478bd9Sstevel@tonic-gate 			xattrhead = NULL;
30397c478bd9Sstevel@tonic-gate 		}
30407c478bd9Sstevel@tonic-gate #endif
30417c478bd9Sstevel@tonic-gate 
30427c478bd9Sstevel@tonic-gate 		if (!oflag)
30437c478bd9Sstevel@tonic-gate 		    resugname(dirfd, comp, symflag); /* set file ownership */
30447c478bd9Sstevel@tonic-gate 
30457c478bd9Sstevel@tonic-gate 		if (pflag && newfile == TRUE && !dir &&
30467c478bd9Sstevel@tonic-gate 		    (dblock.dbuf.typeflag == '0' ||
30477c478bd9Sstevel@tonic-gate 		    dblock.dbuf.typeflag == NULL ||
30487c478bd9Sstevel@tonic-gate 		    convflag || dblock.dbuf.typeflag == '1')) {
30497c478bd9Sstevel@tonic-gate 			if (fstat(ofile, &xtractbuf) == -1)
30507c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
30517c478bd9Sstevel@tonic-gate 				    "tar: cannot stat extracted file %s\n"),
30527c478bd9Sstevel@tonic-gate 				    namep);
30537c478bd9Sstevel@tonic-gate 			else if ((xtractbuf.st_mode & (MODEMASK & ~S_IFMT))
30547c478bd9Sstevel@tonic-gate 			    != (stbuf.st_mode & (MODEMASK & ~S_IFMT))) {
30557c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
30567c478bd9Sstevel@tonic-gate 				    "tar: warning - file permissions have "
30577c478bd9Sstevel@tonic-gate 				    "changed for %s (are 0%o, should be "
30587c478bd9Sstevel@tonic-gate 				    "0%o)\n"),
30597c478bd9Sstevel@tonic-gate 				    namep, xtractbuf.st_mode, stbuf.st_mode);
30607c478bd9Sstevel@tonic-gate 			}
30617c478bd9Sstevel@tonic-gate 		}
30627c478bd9Sstevel@tonic-gate 		if (ofile != -1) {
30637c478bd9Sstevel@tonic-gate 			(void) close(dirfd);
30647c478bd9Sstevel@tonic-gate 			dirfd = -1;
30657c478bd9Sstevel@tonic-gate 			if (close(ofile) != 0)
30667c478bd9Sstevel@tonic-gate 				vperror(2, gettext("close error"));
30677c478bd9Sstevel@tonic-gate 		}
30687c478bd9Sstevel@tonic-gate 		xcnt++;			/* increment # files extracted */
30697c478bd9Sstevel@tonic-gate 		}
307045916cd2Sjpk 
307145916cd2Sjpk 		/*
307245916cd2Sjpk 		 * Process ancillary file.
307345916cd2Sjpk 		 *
307445916cd2Sjpk 		 */
307545916cd2Sjpk 
30767c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == 'A') {	/* acl info */
30777c478bd9Sstevel@tonic-gate 			char	buf[TBLOCK];
30787c478bd9Sstevel@tonic-gate 			char	*secp;
30797c478bd9Sstevel@tonic-gate 			char	*tp;
30807c478bd9Sstevel@tonic-gate 			int	attrsize;
30817c478bd9Sstevel@tonic-gate 			int	cnt;
30827c478bd9Sstevel@tonic-gate 
308345916cd2Sjpk 			/* reset Trusted Extensions flags */
308445916cd2Sjpk 			dir_flag = 0;
308545916cd2Sjpk 			mld_flag = 0;
308645916cd2Sjpk 			lk_rpath_flag = 0;
308745916cd2Sjpk 			rpath_flag = 0;
30887c478bd9Sstevel@tonic-gate 
30897c478bd9Sstevel@tonic-gate 			if (pflag) {
30907c478bd9Sstevel@tonic-gate 				bytes = stbuf.st_size;
30917c478bd9Sstevel@tonic-gate 				if ((secp = malloc((int)bytes)) == NULL) {
30927c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
30937c478bd9Sstevel@tonic-gate 					    "Insufficient memory for acl\n"));
30947c478bd9Sstevel@tonic-gate 					passtape();
30957c478bd9Sstevel@tonic-gate 					continue;
30967c478bd9Sstevel@tonic-gate 				}
30977c478bd9Sstevel@tonic-gate 				tp = secp;
30987c478bd9Sstevel@tonic-gate 				blocks = TBLOCKS(bytes);
309945916cd2Sjpk 
310045916cd2Sjpk 				/*
310145916cd2Sjpk 				 * Display a line for each ancillary file.
310245916cd2Sjpk 				 */
310345916cd2Sjpk 				if (vflag && Tflag)
310445916cd2Sjpk 					(void) fprintf(vfile, "x %s(A), %"
310545916cd2Sjpk 					    FMT_blkcnt_t " bytes, %"
310645916cd2Sjpk 					    FMT_blkcnt_t " tape blocks\n",
310745916cd2Sjpk 					    namep, bytes, blocks);
310845916cd2Sjpk 
31097c478bd9Sstevel@tonic-gate 				while (blocks-- > 0) {
31107c478bd9Sstevel@tonic-gate 					readtape(buf);
31117c478bd9Sstevel@tonic-gate 					if (bytes <= TBLOCK) {
31127c478bd9Sstevel@tonic-gate 						(void) memcpy(tp, buf,
31137c478bd9Sstevel@tonic-gate 						    (size_t)bytes);
31147c478bd9Sstevel@tonic-gate 						break;
31157c478bd9Sstevel@tonic-gate 					} else {
31167c478bd9Sstevel@tonic-gate 						(void) memcpy(tp, buf,
31177c478bd9Sstevel@tonic-gate 						    TBLOCK);
31187c478bd9Sstevel@tonic-gate 						tp += TBLOCK;
31197c478bd9Sstevel@tonic-gate 					}
31207c478bd9Sstevel@tonic-gate 					bytes -= TBLOCK;
31217c478bd9Sstevel@tonic-gate 				}
3122fa9e4066Sahrens 				bytes = stbuf.st_size;
31237c478bd9Sstevel@tonic-gate 				/* got all attributes in secp */
31247c478bd9Sstevel@tonic-gate 				tp = secp;
31257c478bd9Sstevel@tonic-gate 				do {
31267c478bd9Sstevel@tonic-gate 					attr = (struct sec_attr *)tp;
31277c478bd9Sstevel@tonic-gate 					switch (attr->attr_type) {
31287c478bd9Sstevel@tonic-gate 					case UFSD_ACL:
3129fa9e4066Sahrens 					case ACE_ACL:
31307c478bd9Sstevel@tonic-gate 						(void) sscanf(attr->attr_len,
3131fa9e4066Sahrens 						    "%7o",
3132fa9e4066Sahrens 						    (uint_t *)
3133fa9e4066Sahrens 						    &cnt);
31347c478bd9Sstevel@tonic-gate 						/* header is 8 */
31357c478bd9Sstevel@tonic-gate 						attrsize = 8 + (int)strlen(
31367c478bd9Sstevel@tonic-gate 						    &attr->attr_info[0]) + 1;
3137fa9e4066Sahrens 						error =
3138fa9e4066Sahrens 						    acl_fromtext(
3139fa9e4066Sahrens 						    &attr->attr_info[0], &aclp);
3140fa9e4066Sahrens 
3141fa9e4066Sahrens 						if (error != 0) {
31427c478bd9Sstevel@tonic-gate 							(void) fprintf(stderr,
31437c478bd9Sstevel@tonic-gate 							    gettext(
31447c478bd9Sstevel@tonic-gate 							    "aclfromtext "
3145fa9e4066Sahrens 							    "failed: %s\n"),
3146fa9e4066Sahrens 							    acl_strerror(
3147fa9e4066Sahrens 							    error));
3148fa9e4066Sahrens 							bytes -= attrsize;
31497c478bd9Sstevel@tonic-gate 							break;
31507c478bd9Sstevel@tonic-gate 						}
3151fa9e4066Sahrens 						if (acl_cnt(aclp) != cnt) {
31527c478bd9Sstevel@tonic-gate 							(void) fprintf(stderr,
31537c478bd9Sstevel@tonic-gate 							    gettext(
31547c478bd9Sstevel@tonic-gate 							    "aclcnt error\n"));
3155fa9e4066Sahrens 							bytes -= attrsize;
31567c478bd9Sstevel@tonic-gate 							break;
31577c478bd9Sstevel@tonic-gate 						}
31587c478bd9Sstevel@tonic-gate 						bytes -= attrsize;
31597c478bd9Sstevel@tonic-gate 						break;
31607c478bd9Sstevel@tonic-gate 
316145916cd2Sjpk 					/* Trusted Extensions */
316245916cd2Sjpk 
316345916cd2Sjpk 					case DIR_TYPE:
316445916cd2Sjpk 					case LBL_TYPE:
316545916cd2Sjpk 					case APRIV_TYPE:
316645916cd2Sjpk 					case FPRIV_TYPE:
316745916cd2Sjpk 					case COMP_TYPE:
316845916cd2Sjpk 					case LK_COMP_TYPE:
316945916cd2Sjpk 					case ATTR_FLAG_TYPE:
317045916cd2Sjpk 						attrsize =
317145916cd2Sjpk 						    sizeof (struct sec_attr) +
317245916cd2Sjpk 						    strlen(&attr->attr_info[0]);
317345916cd2Sjpk 						bytes -= attrsize;
317445916cd2Sjpk 						if (Tflag)
317545916cd2Sjpk 							extract_attr(&namep,
317645916cd2Sjpk 							    attr);
317745916cd2Sjpk 						break;
31787c478bd9Sstevel@tonic-gate 
31797c478bd9Sstevel@tonic-gate 					default:
31807c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(
31817c478bd9Sstevel@tonic-gate 						    "unrecognized attr"
31827c478bd9Sstevel@tonic-gate 						    " type\n"));
31837c478bd9Sstevel@tonic-gate 						bytes = (off_t)0;
31847c478bd9Sstevel@tonic-gate 						break;
31857c478bd9Sstevel@tonic-gate 					}
31867c478bd9Sstevel@tonic-gate 
31877c478bd9Sstevel@tonic-gate 					/* next attributes */
31887c478bd9Sstevel@tonic-gate 					tp += attrsize;
31897c478bd9Sstevel@tonic-gate 				} while (bytes != 0);
31907c478bd9Sstevel@tonic-gate 				free(secp);
31917c478bd9Sstevel@tonic-gate 			} else
31927c478bd9Sstevel@tonic-gate 				passtape();
31937c478bd9Sstevel@tonic-gate 		} /* acl */
31947c478bd9Sstevel@tonic-gate 
31957c478bd9Sstevel@tonic-gate 	} /* for */
31967c478bd9Sstevel@tonic-gate 
31977c478bd9Sstevel@tonic-gate 	/*
31987c478bd9Sstevel@tonic-gate 	 *  Ensure that all the directories still on the directory stack
31997c478bd9Sstevel@tonic-gate 	 *  get their modification times set correctly by flushing the
32007c478bd9Sstevel@tonic-gate 	 *  stack.
32017c478bd9Sstevel@tonic-gate 	 */
32027c478bd9Sstevel@tonic-gate 
32037c478bd9Sstevel@tonic-gate 	doDirTimes(NULL, time_zero);
32047c478bd9Sstevel@tonic-gate 
32057c478bd9Sstevel@tonic-gate 	/*
32067c478bd9Sstevel@tonic-gate 	 * Check if the number of files extracted is different from the
32077c478bd9Sstevel@tonic-gate 	 * number of files listed on the command line
32087c478bd9Sstevel@tonic-gate 	 */
32097c478bd9Sstevel@tonic-gate 	if (fcnt > xcnt) {
32107c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
32117c478bd9Sstevel@tonic-gate 		    gettext("tar: %d file(s) not extracted\n"),
32127c478bd9Sstevel@tonic-gate 		fcnt-xcnt);
32137c478bd9Sstevel@tonic-gate 		Errflg = 1;
32147c478bd9Sstevel@tonic-gate 	}
32157c478bd9Sstevel@tonic-gate }
32167c478bd9Sstevel@tonic-gate 
32177c478bd9Sstevel@tonic-gate /*
32187c478bd9Sstevel@tonic-gate  *	xblocks		extract file/extent from tape to output file
32197c478bd9Sstevel@tonic-gate  *
32207c478bd9Sstevel@tonic-gate  *	xblocks(bytes, ofile);
32217c478bd9Sstevel@tonic-gate  *	unsigned long long bytes;	size of extent or file to be extracted
32227c478bd9Sstevel@tonic-gate  *
32237c478bd9Sstevel@tonic-gate  *	called by doxtract() and xsfile()
32247c478bd9Sstevel@tonic-gate  */
32257c478bd9Sstevel@tonic-gate 
32267c478bd9Sstevel@tonic-gate static void
32277c478bd9Sstevel@tonic-gate xblocks(off_t bytes, int ofile)
32287c478bd9Sstevel@tonic-gate {
32297c478bd9Sstevel@tonic-gate 	blkcnt_t blocks;
32307c478bd9Sstevel@tonic-gate 	char buf[TBLOCK];
32317c478bd9Sstevel@tonic-gate 	char tempname[NAMSIZ+1];
32327c478bd9Sstevel@tonic-gate 	int write_count;
32337c478bd9Sstevel@tonic-gate 
32347c478bd9Sstevel@tonic-gate 	blocks = TBLOCKS(bytes);
32357c478bd9Sstevel@tonic-gate 	while (blocks-- > 0) {
32367c478bd9Sstevel@tonic-gate 		readtape(buf);
32377c478bd9Sstevel@tonic-gate 		if (bytes > TBLOCK)
32387c478bd9Sstevel@tonic-gate 			write_count = TBLOCK;
32397c478bd9Sstevel@tonic-gate 		else
32407c478bd9Sstevel@tonic-gate 			write_count = bytes;
32417c478bd9Sstevel@tonic-gate 		if (write(ofile, buf, write_count) < 0) {
32427c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_PATH)
32437c478bd9Sstevel@tonic-gate 				(void) strcpy(tempname, Xtarhdr.x_path);
32447c478bd9Sstevel@tonic-gate 			else
32457c478bd9Sstevel@tonic-gate 				(void) sprintf(tempname, "%.*s", NAMSIZ,
32467c478bd9Sstevel@tonic-gate 				    dblock.dbuf.name);
32477c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
32487c478bd9Sstevel@tonic-gate 			    "tar: %s: HELP - extract write error\n"), tempname);
32497c478bd9Sstevel@tonic-gate 			done(2);
32507c478bd9Sstevel@tonic-gate 		}
32517c478bd9Sstevel@tonic-gate 		bytes -= TBLOCK;
32527c478bd9Sstevel@tonic-gate 	}
32537c478bd9Sstevel@tonic-gate }
32547c478bd9Sstevel@tonic-gate 
32557c478bd9Sstevel@tonic-gate 
32567c478bd9Sstevel@tonic-gate /*
32577c478bd9Sstevel@tonic-gate  * 	xsfile	extract split file
32587c478bd9Sstevel@tonic-gate  *
32597c478bd9Sstevel@tonic-gate  *	xsfile(ofd);	ofd = output file descriptor
32607c478bd9Sstevel@tonic-gate  *
32617c478bd9Sstevel@tonic-gate  *	file extracted and put in ofd via xblocks()
32627c478bd9Sstevel@tonic-gate  *
32637c478bd9Sstevel@tonic-gate  *	NOTE:  only called by doxtract() to extract one large file
32647c478bd9Sstevel@tonic-gate  */
32657c478bd9Sstevel@tonic-gate 
32667c478bd9Sstevel@tonic-gate static	union	hblock	savedblock;	/* to ensure same file across volumes */
32677c478bd9Sstevel@tonic-gate 
32687c478bd9Sstevel@tonic-gate static void
32697c478bd9Sstevel@tonic-gate xsfile(int ofd)
32707c478bd9Sstevel@tonic-gate {
32717c478bd9Sstevel@tonic-gate 	int i, c;
32727c478bd9Sstevel@tonic-gate 	char name[PATH_MAX+1];	/* holds name for diagnostics */
32737c478bd9Sstevel@tonic-gate 	int extents, totalext;
32747c478bd9Sstevel@tonic-gate 	off_t bytes, totalbytes;
32757c478bd9Sstevel@tonic-gate 
32767c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_PATH)
32777c478bd9Sstevel@tonic-gate 		(void) strcpy(name, Xtarhdr.x_path);
32787c478bd9Sstevel@tonic-gate 	else
32797c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "%.*s", NAMSIZ, dblock.dbuf.name);
32807c478bd9Sstevel@tonic-gate 
32817c478bd9Sstevel@tonic-gate 	totalbytes = (off_t)0;		/* in case we read in half the file */
32827c478bd9Sstevel@tonic-gate 	totalext = 0;		/* these keep count */
32837c478bd9Sstevel@tonic-gate 
32847c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
32857c478bd9Sstevel@tonic-gate 	    "tar: %s split across %d volumes\n"), name, extotal);
32867c478bd9Sstevel@tonic-gate 
32877c478bd9Sstevel@tonic-gate 	/* make sure we do extractions in order */
32887c478bd9Sstevel@tonic-gate 	if (extno != 1) {	/* starting in middle of file? */
32897c478bd9Sstevel@tonic-gate 		(void) printf(gettext(
32907c478bd9Sstevel@tonic-gate 		    "tar: first extent read is not #1\n"
3291*3d63ea05Sas145665 		    "OK to read file beginning with extent #%d (%s/%s) ? "),
3292*3d63ea05Sas145665 		    extno, yesstr, nostr);
3293*3d63ea05Sas145665 		if (yes() == 0) {
32947c478bd9Sstevel@tonic-gate canit:
32957c478bd9Sstevel@tonic-gate 			passtape();
32967c478bd9Sstevel@tonic-gate 			if (close(ofd) != 0)
32977c478bd9Sstevel@tonic-gate 				vperror(2, gettext("close error"));
32987c478bd9Sstevel@tonic-gate 			return;
32997c478bd9Sstevel@tonic-gate 		}
33007c478bd9Sstevel@tonic-gate 	}
33017c478bd9Sstevel@tonic-gate 	extents = extotal;
33027c478bd9Sstevel@tonic-gate 	i = extno;
33037c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
33047c478bd9Sstevel@tonic-gate 	while (1) {
33057c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_SIZE) {
33067c478bd9Sstevel@tonic-gate 			bytes = extsize;
33077c478bd9Sstevel@tonic-gate 		} else {
33087c478bd9Sstevel@tonic-gate 			bytes = stbuf.st_size;
33097c478bd9Sstevel@tonic-gate 		}
33107c478bd9Sstevel@tonic-gate 
33117c478bd9Sstevel@tonic-gate 		if (vflag)
33127c478bd9Sstevel@tonic-gate 			(void) fprintf(vfile, "+++ x %s [extent #%d], %"
33137c478bd9Sstevel@tonic-gate 			    FMT_off_t " bytes, %ldK\n", name, extno, bytes,
33147c478bd9Sstevel@tonic-gate 			    (long)K(TBLOCKS(bytes)));
33157c478bd9Sstevel@tonic-gate 		xblocks(bytes, ofd);
33167c478bd9Sstevel@tonic-gate 
33177c478bd9Sstevel@tonic-gate 		totalbytes += bytes;
33187c478bd9Sstevel@tonic-gate 		totalext++;
33197c478bd9Sstevel@tonic-gate 		if (++i > extents)
33207c478bd9Sstevel@tonic-gate 			break;
33217c478bd9Sstevel@tonic-gate 
33227c478bd9Sstevel@tonic-gate 		/* get next volume and verify it's the right one */
33237c478bd9Sstevel@tonic-gate 		copy(&savedblock, &dblock);
33247c478bd9Sstevel@tonic-gate tryagain:
33257c478bd9Sstevel@tonic-gate 		newvol();
33267c478bd9Sstevel@tonic-gate 		xhdr_flgs = 0;
33277c478bd9Sstevel@tonic-gate 		getdir();
33287c478bd9Sstevel@tonic-gate 		if (Xhdrflag > 0)
33297c478bd9Sstevel@tonic-gate 			(void) get_xdata();	/* Get x-header & regular hdr */
33307c478bd9Sstevel@tonic-gate 		if (endtape()) {	/* seemingly empty volume */
33317c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
33327c478bd9Sstevel@tonic-gate 			    "tar: first record is null\n"));
33337c478bd9Sstevel@tonic-gate asknicely:
33347c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
33357c478bd9Sstevel@tonic-gate 			    "tar: need volume with extent #%d of %s\n"),
33367c478bd9Sstevel@tonic-gate 			    i, name);
33377c478bd9Sstevel@tonic-gate 			goto tryagain;
33387c478bd9Sstevel@tonic-gate 		}
33397c478bd9Sstevel@tonic-gate 		if (notsame()) {
33407c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
33417c478bd9Sstevel@tonic-gate 			    "tar: first file on that volume is not "
33427c478bd9Sstevel@tonic-gate 			    "the same file\n"));
33437c478bd9Sstevel@tonic-gate 			goto asknicely;
33447c478bd9Sstevel@tonic-gate 		}
33457c478bd9Sstevel@tonic-gate 		if (i != extno) {
33467c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
3347eace40a5Sceastha 			    "tar: extent #%d received out of order\ntar: "
3348eace40a5Sceastha 			    "should be #%d\n"), extno, i);
33497c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
33507c478bd9Sstevel@tonic-gate 			    "Ignore error, Abort this file, or "
33517c478bd9Sstevel@tonic-gate 			    "load New volume (i/a/n) ? "));
33527c478bd9Sstevel@tonic-gate 			c = response();
33537c478bd9Sstevel@tonic-gate 			if (c == 'a')
33547c478bd9Sstevel@tonic-gate 				goto canit;
33557c478bd9Sstevel@tonic-gate 			if (c != 'i')		/* default to new volume */
33567c478bd9Sstevel@tonic-gate 				goto asknicely;
33577c478bd9Sstevel@tonic-gate 			i = extno;		/* okay, start from there */
33587c478bd9Sstevel@tonic-gate 		}
33597c478bd9Sstevel@tonic-gate 	}
33607c478bd9Sstevel@tonic-gate 	if (vflag)
33617c478bd9Sstevel@tonic-gate 		(void) fprintf(vfile, gettext(
33627c478bd9Sstevel@tonic-gate 		    "x %s (in %d extents), %" FMT_off_t " bytes, %ldK\n"),
33637c478bd9Sstevel@tonic-gate 		    name, totalext, totalbytes, (long)K(TBLOCKS(totalbytes)));
33647c478bd9Sstevel@tonic-gate }
33657c478bd9Sstevel@tonic-gate 
33667c478bd9Sstevel@tonic-gate 
33677c478bd9Sstevel@tonic-gate /*
33687c478bd9Sstevel@tonic-gate  *	notsame()	check if extract file extent is invalid
33697c478bd9Sstevel@tonic-gate  *
33707c478bd9Sstevel@tonic-gate  *	returns true if anything differs between savedblock and dblock
33717c478bd9Sstevel@tonic-gate  *	except extno (extent number), checksum, or size (extent size).
33727c478bd9Sstevel@tonic-gate  *	Determines if this header belongs to the same file as the one we're
33737c478bd9Sstevel@tonic-gate  *	extracting.
33747c478bd9Sstevel@tonic-gate  *
33757c478bd9Sstevel@tonic-gate  *	NOTE:	though rather bulky, it is only called once per file
33767c478bd9Sstevel@tonic-gate  *		extension, and it can withstand changes in the definition
33777c478bd9Sstevel@tonic-gate  *		of the header structure.
33787c478bd9Sstevel@tonic-gate  *
33797c478bd9Sstevel@tonic-gate  *	WARNING:	this routine is local to xsfile() above
33807c478bd9Sstevel@tonic-gate  */
33817c478bd9Sstevel@tonic-gate 
33827c478bd9Sstevel@tonic-gate static int
33837c478bd9Sstevel@tonic-gate notsame(void)
33847c478bd9Sstevel@tonic-gate {
33857c478bd9Sstevel@tonic-gate 	return (
33867c478bd9Sstevel@tonic-gate 	    (strncmp(savedblock.dbuf.name, dblock.dbuf.name, NAMSIZ)) ||
33877c478bd9Sstevel@tonic-gate 	    (strcmp(savedblock.dbuf.mode, dblock.dbuf.mode)) ||
33887c478bd9Sstevel@tonic-gate 	    (strcmp(savedblock.dbuf.uid, dblock.dbuf.uid)) ||
33897c478bd9Sstevel@tonic-gate 	    (strcmp(savedblock.dbuf.gid, dblock.dbuf.gid)) ||
33907c478bd9Sstevel@tonic-gate 	    (strcmp(savedblock.dbuf.mtime, dblock.dbuf.mtime)) ||
33917c478bd9Sstevel@tonic-gate 	    (savedblock.dbuf.typeflag != dblock.dbuf.typeflag) ||
33927c478bd9Sstevel@tonic-gate 	    (strncmp(savedblock.dbuf.linkname, dblock.dbuf.linkname, NAMSIZ)) ||
33937c478bd9Sstevel@tonic-gate 	    (savedblock.dbuf.extotal != dblock.dbuf.extotal) ||
33947c478bd9Sstevel@tonic-gate 	    (strcmp(savedblock.dbuf.efsize, dblock.dbuf.efsize)));
33957c478bd9Sstevel@tonic-gate }
33967c478bd9Sstevel@tonic-gate 
33977c478bd9Sstevel@tonic-gate 
33987c478bd9Sstevel@tonic-gate static void
33997c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
34007c478bd9Sstevel@tonic-gate dotable(char *argv[], int tbl_cnt)
34017c478bd9Sstevel@tonic-gate #else
34027c478bd9Sstevel@tonic-gate dotable(char *argv[])
34037c478bd9Sstevel@tonic-gate #endif
34047c478bd9Sstevel@tonic-gate 
34057c478bd9Sstevel@tonic-gate {
34067c478bd9Sstevel@tonic-gate 	int tcnt;			/* count # files tabled */
34077c478bd9Sstevel@tonic-gate 	int fcnt;			/* count # files in argv list */
34087c478bd9Sstevel@tonic-gate 	char *namep, *dirp, *comp;
34097c478bd9Sstevel@tonic-gate 	int want;
34107c478bd9Sstevel@tonic-gate 	char aclchar = ' ';			/* either blank or '+' */
34117c478bd9Sstevel@tonic-gate 	char templink[PATH_MAX+1];
34127c478bd9Sstevel@tonic-gate 	char  *np;
34137c478bd9Sstevel@tonic-gate 
34147c478bd9Sstevel@tonic-gate 	dumping = 0;
34157c478bd9Sstevel@tonic-gate 
34167c478bd9Sstevel@tonic-gate 	/* if not on magtape, maximize seek speed */
34177c478bd9Sstevel@tonic-gate 	if (NotTape && !bflag) {
34187c478bd9Sstevel@tonic-gate #if SYS_BLOCK > TBLOCK
34197c478bd9Sstevel@tonic-gate 		nblock = SYS_BLOCK / TBLOCK;
34207c478bd9Sstevel@tonic-gate #else
34217c478bd9Sstevel@tonic-gate 		nblock = 1;
34227c478bd9Sstevel@tonic-gate #endif
34237c478bd9Sstevel@tonic-gate 	}
34247c478bd9Sstevel@tonic-gate 	/*
34257c478bd9Sstevel@tonic-gate 	 * Count the number of files that are to be tabled
34267c478bd9Sstevel@tonic-gate 	 */
34277c478bd9Sstevel@tonic-gate 	fcnt = tcnt = 0;
34287c478bd9Sstevel@tonic-gate 
34297c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
34307c478bd9Sstevel@tonic-gate 	initarg(argv, Filefile);
34317c478bd9Sstevel@tonic-gate 	while (nextarg() != NULL)
34327c478bd9Sstevel@tonic-gate 		++fcnt;
34337c478bd9Sstevel@tonic-gate 	fcnt += tbl_cnt;
34347c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
34357c478bd9Sstevel@tonic-gate 
34367c478bd9Sstevel@tonic-gate 	for (;;) {
34377c478bd9Sstevel@tonic-gate 
34387c478bd9Sstevel@tonic-gate 		/* namep is set by wantit to point to the full name */
34397c478bd9Sstevel@tonic-gate 		if ((want = wantit(argv, &namep, &dirp, &comp)) == 0)
34407c478bd9Sstevel@tonic-gate 			continue;
34417c478bd9Sstevel@tonic-gate 		if (want == -1)
34427c478bd9Sstevel@tonic-gate 			break;
34437c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag != 'A')
34447c478bd9Sstevel@tonic-gate 			++tcnt;
34457c478bd9Sstevel@tonic-gate 
34467c478bd9Sstevel@tonic-gate 		/*
34477c478bd9Sstevel@tonic-gate 		 * ACL support:
34487c478bd9Sstevel@tonic-gate 		 * aclchar is introduced to indicate if there are
34497c478bd9Sstevel@tonic-gate 		 * acl entries. longt() now takes one extra argument.
34507c478bd9Sstevel@tonic-gate 		 */
34517c478bd9Sstevel@tonic-gate 		if (vflag) {
34527c478bd9Sstevel@tonic-gate 			if (dblock.dbuf.typeflag == 'A') {
34537c478bd9Sstevel@tonic-gate 				aclchar = '+';
34547c478bd9Sstevel@tonic-gate 				passtape();
34557c478bd9Sstevel@tonic-gate 				continue;
34567c478bd9Sstevel@tonic-gate 			}
34577c478bd9Sstevel@tonic-gate 			longt(&stbuf, aclchar);
34587c478bd9Sstevel@tonic-gate 			aclchar = ' ';
34597c478bd9Sstevel@tonic-gate 		}
34607c478bd9Sstevel@tonic-gate 
34617c478bd9Sstevel@tonic-gate 
34627c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
34637c478bd9Sstevel@tonic-gate 		if (xattrp != (struct xattr_buf *)NULL) {
34647c478bd9Sstevel@tonic-gate 			np = xattrp->h_names + strlen(xattrp->h_names) + 1;
34657c478bd9Sstevel@tonic-gate 			(void) printf(gettext("%s attribute %s"),
34667c478bd9Sstevel@tonic-gate 			    xattrp->h_names, np);
34677c478bd9Sstevel@tonic-gate 
34687c478bd9Sstevel@tonic-gate 		} else {
34697c478bd9Sstevel@tonic-gate 			(void) printf("%s", namep);
34707c478bd9Sstevel@tonic-gate 		}
34717c478bd9Sstevel@tonic-gate #else
34727c478bd9Sstevel@tonic-gate 			(void) printf("%s", namep);
34737c478bd9Sstevel@tonic-gate #endif
34747c478bd9Sstevel@tonic-gate 
34757c478bd9Sstevel@tonic-gate 		if (extno != 0) {
34767c478bd9Sstevel@tonic-gate 			if (vflag) {
34777c478bd9Sstevel@tonic-gate 				/* keep the '\n' for backwards compatibility */
34787c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext(
34797c478bd9Sstevel@tonic-gate 				    "\n [extent #%d of %d]"), extno, extotal);
34807c478bd9Sstevel@tonic-gate 			} else {
34817c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext(
34827c478bd9Sstevel@tonic-gate 				    " [extent #%d of %d]"), extno, extotal);
34837c478bd9Sstevel@tonic-gate 			}
34847c478bd9Sstevel@tonic-gate 		}
34857c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_LINKPATH) {
34867c478bd9Sstevel@tonic-gate 			(void) strcpy(templink, Xtarhdr.x_linkpath);
34877c478bd9Sstevel@tonic-gate 		} else {
34887c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
34897c478bd9Sstevel@tonic-gate 			if (xattrp != (struct xattr_buf *)NULL) {
34907c478bd9Sstevel@tonic-gate 				(void) sprintf(templink,
34917c478bd9Sstevel@tonic-gate 				    "file %.*s", NAMSIZ, xattrp->h_names);
34927c478bd9Sstevel@tonic-gate 			} else {
34937c478bd9Sstevel@tonic-gate 				(void) sprintf(templink, "%.*s", NAMSIZ,
34947c478bd9Sstevel@tonic-gate 				    dblock.dbuf.linkname);
34957c478bd9Sstevel@tonic-gate 			}
34967c478bd9Sstevel@tonic-gate #else
34977c478bd9Sstevel@tonic-gate 			(void) sprintf(templink, "%.*s", NAMSIZ,
34987c478bd9Sstevel@tonic-gate 			    dblock.dbuf.linkname);
34997c478bd9Sstevel@tonic-gate #endif
35007c478bd9Sstevel@tonic-gate 			templink[NAMSIZ] = '\0';
35017c478bd9Sstevel@tonic-gate 		}
35027c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '1') {
35037c478bd9Sstevel@tonic-gate 			/*
35047c478bd9Sstevel@tonic-gate 			 * TRANSLATION_NOTE
35057c478bd9Sstevel@tonic-gate 			 *	Subject is omitted here.
35067c478bd9Sstevel@tonic-gate 			 *	Translate this as if
35077c478bd9Sstevel@tonic-gate 			 *		<subject> linked to %s
35087c478bd9Sstevel@tonic-gate 			 */
35097c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
35107c478bd9Sstevel@tonic-gate 			if (xattrp != (struct xattr_buf *)NULL) {
35117c478bd9Sstevel@tonic-gate 				(void) printf(
35127c478bd9Sstevel@tonic-gate 				    gettext(" linked to attribute %s"),
35137c478bd9Sstevel@tonic-gate 				    xattr_linkp->h_names +
35147c478bd9Sstevel@tonic-gate 				    strlen(xattr_linkp->h_names) + 1);
35157c478bd9Sstevel@tonic-gate 			} else {
35167c478bd9Sstevel@tonic-gate 				(void) printf(
35177c478bd9Sstevel@tonic-gate 				    gettext(" linked to %s"), templink);
35187c478bd9Sstevel@tonic-gate 			}
35197c478bd9Sstevel@tonic-gate #else
35207c478bd9Sstevel@tonic-gate 				(void) printf(
35217c478bd9Sstevel@tonic-gate 				    gettext(" linked to %s"), templink);
35227c478bd9Sstevel@tonic-gate 
35237c478bd9Sstevel@tonic-gate #endif
35247c478bd9Sstevel@tonic-gate 		}
35257c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '2')
35267c478bd9Sstevel@tonic-gate 			(void) printf(gettext(
35277c478bd9Sstevel@tonic-gate 			/*
35287c478bd9Sstevel@tonic-gate 			 * TRANSLATION_NOTE
35297c478bd9Sstevel@tonic-gate 			 *	Subject is omitted here.
35307c478bd9Sstevel@tonic-gate 			 *	Translate this as if
35317c478bd9Sstevel@tonic-gate 			 *		<subject> symbolic link to %s
35327c478bd9Sstevel@tonic-gate 			 */
35337c478bd9Sstevel@tonic-gate 			" symbolic link to %s"), templink);
35347c478bd9Sstevel@tonic-gate 		(void) printf("\n");
35357c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
35367c478bd9Sstevel@tonic-gate 		if (xattrp != (struct xattr_buf *)NULL) {
35377c478bd9Sstevel@tonic-gate 			free(xattrhead);
35387c478bd9Sstevel@tonic-gate 			xattrp = NULL;
35397c478bd9Sstevel@tonic-gate 			xattrhead = NULL;
35407c478bd9Sstevel@tonic-gate 		}
35417c478bd9Sstevel@tonic-gate #endif
35427c478bd9Sstevel@tonic-gate 		passtape();
35437c478bd9Sstevel@tonic-gate 	}
35447c478bd9Sstevel@tonic-gate 	/*
35457c478bd9Sstevel@tonic-gate 	 * Check if the number of files tabled is different from the
35467c478bd9Sstevel@tonic-gate 	 * number of files listed on the command line
35477c478bd9Sstevel@tonic-gate 	 */
35487c478bd9Sstevel@tonic-gate 	if (fcnt > tcnt) {
35497c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
35507c478bd9Sstevel@tonic-gate 		    "tar: %d file(s) not found\n"), fcnt-tcnt);
35517c478bd9Sstevel@tonic-gate 		Errflg = 1;
35527c478bd9Sstevel@tonic-gate 	}
35537c478bd9Sstevel@tonic-gate }
35547c478bd9Sstevel@tonic-gate 
35557c478bd9Sstevel@tonic-gate static void
35567c478bd9Sstevel@tonic-gate putempty(blkcnt_t n)
35577c478bd9Sstevel@tonic-gate {
35587c478bd9Sstevel@tonic-gate 	char buf[TBLOCK];
35597c478bd9Sstevel@tonic-gate 	char *cp;
35607c478bd9Sstevel@tonic-gate 
35617c478bd9Sstevel@tonic-gate 	for (cp = buf; cp < &buf[TBLOCK]; )
35627c478bd9Sstevel@tonic-gate 		*cp++ = '\0';
35637c478bd9Sstevel@tonic-gate 	while (n-- > 0)
35647c478bd9Sstevel@tonic-gate 		(void) writetbuf(buf, 1);
35657c478bd9Sstevel@tonic-gate }
35667c478bd9Sstevel@tonic-gate 
35677c478bd9Sstevel@tonic-gate static	ushort_t	Ftype = S_IFMT;
35687c478bd9Sstevel@tonic-gate 
35697c478bd9Sstevel@tonic-gate static	void
35707c478bd9Sstevel@tonic-gate verbose(struct stat *st, char aclchar)
35717c478bd9Sstevel@tonic-gate {
35727c478bd9Sstevel@tonic-gate 	int i, j, temp;
35737c478bd9Sstevel@tonic-gate 	mode_t mode;
35747c478bd9Sstevel@tonic-gate 	char modestr[12];
35757c478bd9Sstevel@tonic-gate 
35767c478bd9Sstevel@tonic-gate 	for (i = 0; i < 11; i++)
35777c478bd9Sstevel@tonic-gate 		modestr[i] = '-';
35787c478bd9Sstevel@tonic-gate 	modestr[i] = '\0';
35797c478bd9Sstevel@tonic-gate 
35807c478bd9Sstevel@tonic-gate 	/* a '+' sign is printed if there is ACL */
35817c478bd9Sstevel@tonic-gate 	modestr[i-1] = aclchar;
35827c478bd9Sstevel@tonic-gate 
35837c478bd9Sstevel@tonic-gate 	mode = st->st_mode;
35847c478bd9Sstevel@tonic-gate 	for (i = 0; i < 3; i++) {
35857c478bd9Sstevel@tonic-gate 		temp = (mode >> (6 - (i * 3)));
35867c478bd9Sstevel@tonic-gate 		j = (i * 3) + 1;
35877c478bd9Sstevel@tonic-gate 		if (S_IROTH & temp)
35887c478bd9Sstevel@tonic-gate 			modestr[j] = 'r';
35897c478bd9Sstevel@tonic-gate 		if (S_IWOTH & temp)
35907c478bd9Sstevel@tonic-gate 			modestr[j + 1] = 'w';
35917c478bd9Sstevel@tonic-gate 		if (S_IXOTH & temp)
35927c478bd9Sstevel@tonic-gate 			modestr[j + 2] = 'x';
35937c478bd9Sstevel@tonic-gate 	}
35947c478bd9Sstevel@tonic-gate 	temp = st->st_mode & Ftype;
35957c478bd9Sstevel@tonic-gate 	switch (temp) {
35967c478bd9Sstevel@tonic-gate 	case (S_IFIFO):
35977c478bd9Sstevel@tonic-gate 		modestr[0] = 'p';
35987c478bd9Sstevel@tonic-gate 		break;
35997c478bd9Sstevel@tonic-gate 	case (S_IFCHR):
36007c478bd9Sstevel@tonic-gate 		modestr[0] = 'c';
36017c478bd9Sstevel@tonic-gate 		break;
36027c478bd9Sstevel@tonic-gate 	case (S_IFDIR):
36037c478bd9Sstevel@tonic-gate 		modestr[0] = 'd';
36047c478bd9Sstevel@tonic-gate 		break;
36057c478bd9Sstevel@tonic-gate 	case (S_IFBLK):
36067c478bd9Sstevel@tonic-gate 		modestr[0] = 'b';
36077c478bd9Sstevel@tonic-gate 		break;
36087c478bd9Sstevel@tonic-gate 	case (S_IFREG): /* was initialized to '-' */
36097c478bd9Sstevel@tonic-gate 		break;
36107c478bd9Sstevel@tonic-gate 	case (S_IFLNK):
36117c478bd9Sstevel@tonic-gate 		modestr[0] = 'l';
36127c478bd9Sstevel@tonic-gate 		break;
36137c478bd9Sstevel@tonic-gate 	default:
36147c478bd9Sstevel@tonic-gate 		/* This field may be zero in old archives. */
36157c478bd9Sstevel@tonic-gate 		if (is_posix && dblock.dbuf.typeflag != '1') {
36167c478bd9Sstevel@tonic-gate 			/*
36177c478bd9Sstevel@tonic-gate 			 * For POSIX compliant archives, the mode field
36187c478bd9Sstevel@tonic-gate 			 * consists of 12 bits, ie:  the file type bits
36197c478bd9Sstevel@tonic-gate 			 * are not stored in dblock.dbuf.mode.
36207c478bd9Sstevel@tonic-gate 			 * For files other than hard links, getdir() sets
36217c478bd9Sstevel@tonic-gate 			 * the file type bits in the st_mode field of the
36227c478bd9Sstevel@tonic-gate 			 * stat structure based upon dblock.dbuf.typeflag.
36237c478bd9Sstevel@tonic-gate 			 */
36247c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
36257c478bd9Sstevel@tonic-gate 			    "tar: impossible file type"));
36267c478bd9Sstevel@tonic-gate 		}
36277c478bd9Sstevel@tonic-gate 	}
36287c478bd9Sstevel@tonic-gate 
36297c478bd9Sstevel@tonic-gate 	if ((S_ISUID & Gen.g_mode) == S_ISUID)
36307c478bd9Sstevel@tonic-gate 		modestr[3] = 's';
36317c478bd9Sstevel@tonic-gate 	if ((S_ISVTX & Gen.g_mode) == S_ISVTX)
36327c478bd9Sstevel@tonic-gate 		modestr[9] = 't';
36337c478bd9Sstevel@tonic-gate 	if ((S_ISGID & Gen.g_mode) == S_ISGID && modestr[6] == 'x')
36347c478bd9Sstevel@tonic-gate 		modestr[6] = 's';
36357c478bd9Sstevel@tonic-gate 	else if ((S_ENFMT & Gen.g_mode) == S_ENFMT && modestr[6] != 'x')
36367c478bd9Sstevel@tonic-gate 		modestr[6] = 'l';
36377c478bd9Sstevel@tonic-gate 	(void) fprintf(vfile, "%s", modestr);
36387c478bd9Sstevel@tonic-gate }
36397c478bd9Sstevel@tonic-gate 
36407c478bd9Sstevel@tonic-gate static void
36417c478bd9Sstevel@tonic-gate longt(struct stat *st, char aclchar)
36427c478bd9Sstevel@tonic-gate {
36437c478bd9Sstevel@tonic-gate 	char fileDate[30];
36447c478bd9Sstevel@tonic-gate 	struct tm *tm;
36457c478bd9Sstevel@tonic-gate 
36467c478bd9Sstevel@tonic-gate 	verbose(st, aclchar);
36477c478bd9Sstevel@tonic-gate 	(void) fprintf(vfile, "%3ld/%-3ld", st->st_uid, st->st_gid);
36487c478bd9Sstevel@tonic-gate 
36497c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.typeflag == '2') {
36507c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_LINKPATH)
36517c478bd9Sstevel@tonic-gate 			st->st_size = (off_t)strlen(Xtarhdr.x_linkpath);
36527c478bd9Sstevel@tonic-gate 		else
36537c478bd9Sstevel@tonic-gate 			st->st_size = (off_t)(memchr(dblock.dbuf.linkname,
36547c478bd9Sstevel@tonic-gate 			    '\0', NAMSIZ) ?
36557c478bd9Sstevel@tonic-gate 			    (strlen(dblock.dbuf.linkname)) : (NAMSIZ));
36567c478bd9Sstevel@tonic-gate 	}
36577c478bd9Sstevel@tonic-gate 	(void) fprintf(vfile, " %6" FMT_off_t, st->st_size);
36587c478bd9Sstevel@tonic-gate 
36597c478bd9Sstevel@tonic-gate 	tm = localtime(&(st->st_mtime));
36607c478bd9Sstevel@tonic-gate 	(void) strftime(fileDate, sizeof (fileDate),
36617c478bd9Sstevel@tonic-gate 	    dcgettext((const char *)0, "%b %e %R %Y", LC_TIME), tm);
36627c478bd9Sstevel@tonic-gate 	(void) fprintf(vfile, " %s ", fileDate);
36637c478bd9Sstevel@tonic-gate }
36647c478bd9Sstevel@tonic-gate 
36657c478bd9Sstevel@tonic-gate 
36667c478bd9Sstevel@tonic-gate /*
36677c478bd9Sstevel@tonic-gate  *  checkdir - Attempt to ensure that the path represented in name
36687c478bd9Sstevel@tonic-gate  *             exists, and return 1 if this is true and name itself is a
36697c478bd9Sstevel@tonic-gate  *             directory.
36707c478bd9Sstevel@tonic-gate  *             Return 0 if this path cannot be created or if name is not
36717c478bd9Sstevel@tonic-gate  *             a directory.
36727c478bd9Sstevel@tonic-gate  */
36737c478bd9Sstevel@tonic-gate 
36747c478bd9Sstevel@tonic-gate static int
36757c478bd9Sstevel@tonic-gate checkdir(char *name)
36767c478bd9Sstevel@tonic-gate {
36777c478bd9Sstevel@tonic-gate 	char lastChar;		   /* the last character in name */
36787c478bd9Sstevel@tonic-gate 	char *cp;		   /* scratch pointer into name */
36797c478bd9Sstevel@tonic-gate 	char *firstSlash = NULL;   /* first slash in name */
36807c478bd9Sstevel@tonic-gate 	char *lastSlash = NULL;	   /* last slash in name */
36817c478bd9Sstevel@tonic-gate 	int  nameLen;		   /* length of name */
36827c478bd9Sstevel@tonic-gate 	int  trailingSlash;	   /* true if name ends in slash */
36837c478bd9Sstevel@tonic-gate 	int  leadingSlash;	   /* true if name begins with slash */
36847c478bd9Sstevel@tonic-gate 	int  markedDir;		   /* true if name denotes a directory */
36857c478bd9Sstevel@tonic-gate 	int  success;		   /* status of makeDir call */
36867c478bd9Sstevel@tonic-gate 
36877c478bd9Sstevel@tonic-gate 
36887c478bd9Sstevel@tonic-gate 	/*
36897c478bd9Sstevel@tonic-gate 	 *  Scan through the name, and locate first and last slashes.
36907c478bd9Sstevel@tonic-gate 	 */
36917c478bd9Sstevel@tonic-gate 
36927c478bd9Sstevel@tonic-gate 	for (cp = name; *cp; cp++) {
36937c478bd9Sstevel@tonic-gate 		if (*cp == '/') {
36947c478bd9Sstevel@tonic-gate 			if (! firstSlash) {
36957c478bd9Sstevel@tonic-gate 				firstSlash = cp;
36967c478bd9Sstevel@tonic-gate 			}
36977c478bd9Sstevel@tonic-gate 			lastSlash = cp;
36987c478bd9Sstevel@tonic-gate 		}
36997c478bd9Sstevel@tonic-gate 	}
37007c478bd9Sstevel@tonic-gate 
37017c478bd9Sstevel@tonic-gate 	/*
37027c478bd9Sstevel@tonic-gate 	 *  Determine what you can from the proceeds of the scan.
37037c478bd9Sstevel@tonic-gate 	 */
37047c478bd9Sstevel@tonic-gate 
37057c478bd9Sstevel@tonic-gate 	lastChar	= *(cp - 1);
37067c478bd9Sstevel@tonic-gate 	nameLen		= (int)(cp - name);
37077c478bd9Sstevel@tonic-gate 	trailingSlash	= (lastChar == '/');
37087c478bd9Sstevel@tonic-gate 	leadingSlash	= (*name == '/');
37097c478bd9Sstevel@tonic-gate 	markedDir	= (dblock.dbuf.typeflag == '5' || trailingSlash);
37107c478bd9Sstevel@tonic-gate 
37117c478bd9Sstevel@tonic-gate 	if (! lastSlash && ! markedDir) {
37127c478bd9Sstevel@tonic-gate 		/*
37137c478bd9Sstevel@tonic-gate 		 *  The named file does not have any subdrectory
37147c478bd9Sstevel@tonic-gate 		 *  structure; just bail out.
37157c478bd9Sstevel@tonic-gate 		 */
37167c478bd9Sstevel@tonic-gate 
37177c478bd9Sstevel@tonic-gate 		return (0);
37187c478bd9Sstevel@tonic-gate 	}
37197c478bd9Sstevel@tonic-gate 
37207c478bd9Sstevel@tonic-gate 	/*
37217c478bd9Sstevel@tonic-gate 	 *  Make sure that name doesn`t end with slash for the loop.
37227c478bd9Sstevel@tonic-gate 	 *  This ensures that the makeDir attempt after the loop is
37237c478bd9Sstevel@tonic-gate 	 *  meaningful.
37247c478bd9Sstevel@tonic-gate 	 */
37257c478bd9Sstevel@tonic-gate 
37267c478bd9Sstevel@tonic-gate 	if (trailingSlash) {
37277c478bd9Sstevel@tonic-gate 		name[nameLen-1] = '\0';
37287c478bd9Sstevel@tonic-gate 	}
37297c478bd9Sstevel@tonic-gate 
37307c478bd9Sstevel@tonic-gate 	/*
37317c478bd9Sstevel@tonic-gate 	 *  Make the path one component at a time.
37327c478bd9Sstevel@tonic-gate 	 */
37337c478bd9Sstevel@tonic-gate 
37347c478bd9Sstevel@tonic-gate 	for (cp = strchr(leadingSlash ? name+1 : name, '/');
37357c478bd9Sstevel@tonic-gate 	    cp;
37367c478bd9Sstevel@tonic-gate 	    cp = strchr(cp+1, '/')) {
37377c478bd9Sstevel@tonic-gate 		*cp = '\0';
37387c478bd9Sstevel@tonic-gate 		success = makeDir(name);
37397c478bd9Sstevel@tonic-gate 		*cp = '/';
37407c478bd9Sstevel@tonic-gate 
37417c478bd9Sstevel@tonic-gate 		if (!success) {
37427c478bd9Sstevel@tonic-gate 			name[nameLen-1] = lastChar;
37437c478bd9Sstevel@tonic-gate 			return (0);
37447c478bd9Sstevel@tonic-gate 		}
37457c478bd9Sstevel@tonic-gate 	}
37467c478bd9Sstevel@tonic-gate 
37477c478bd9Sstevel@tonic-gate 	/*
37487c478bd9Sstevel@tonic-gate 	 *  This makes the last component of the name, if it is a
37497c478bd9Sstevel@tonic-gate 	 *  directory.
37507c478bd9Sstevel@tonic-gate 	 */
37517c478bd9Sstevel@tonic-gate 
37527c478bd9Sstevel@tonic-gate 	if (markedDir) {
37537c478bd9Sstevel@tonic-gate 		if (! makeDir(name)) {
37547c478bd9Sstevel@tonic-gate 			name[nameLen-1] = lastChar;
37557c478bd9Sstevel@tonic-gate 			return (0);
37567c478bd9Sstevel@tonic-gate 		}
37577c478bd9Sstevel@tonic-gate 	}
37587c478bd9Sstevel@tonic-gate 
37597c478bd9Sstevel@tonic-gate 	name[nameLen-1] = (lastChar == '/') ? '\0' : lastChar;
37607c478bd9Sstevel@tonic-gate 	return (markedDir);
37617c478bd9Sstevel@tonic-gate }
37627c478bd9Sstevel@tonic-gate 
37637c478bd9Sstevel@tonic-gate /*
37647c478bd9Sstevel@tonic-gate  * resugname - Restore the user name and group name.  Search the NIS
37657c478bd9Sstevel@tonic-gate  *             before using the uid and gid.
37667c478bd9Sstevel@tonic-gate  *             (It is presumed that an archive entry cannot be
37677c478bd9Sstevel@tonic-gate  *	       simultaneously a symlink and some other type.)
37687c478bd9Sstevel@tonic-gate  */
37697c478bd9Sstevel@tonic-gate 
37707c478bd9Sstevel@tonic-gate static void
37717c478bd9Sstevel@tonic-gate resugname(int dirfd, 	/* dir fd file resides in */
37727c478bd9Sstevel@tonic-gate 	char *name,	/* name of the file to be modified */
37737c478bd9Sstevel@tonic-gate 	int symflag)	/* true if file is a symbolic link */
37747c478bd9Sstevel@tonic-gate {
37757c478bd9Sstevel@tonic-gate 	uid_t duid;
37767c478bd9Sstevel@tonic-gate 	gid_t dgid;
37777c478bd9Sstevel@tonic-gate 	struct stat *sp = &stbuf;
37787c478bd9Sstevel@tonic-gate 	char	*u_g_name;
37797c478bd9Sstevel@tonic-gate 
37807c478bd9Sstevel@tonic-gate 	if (checkflag == 1) { /* Extended tar format and euid == 0 */
37817c478bd9Sstevel@tonic-gate 
37827c478bd9Sstevel@tonic-gate 		/*
37837c478bd9Sstevel@tonic-gate 		 * Try and extract the intended uid and gid from the name
37847c478bd9Sstevel@tonic-gate 		 * service before believing the uid and gid in the header.
37857c478bd9Sstevel@tonic-gate 		 *
37867c478bd9Sstevel@tonic-gate 		 * In the case where we archived a setuid or setgid file
37877c478bd9Sstevel@tonic-gate 		 * owned by someone with a large uid, then it will
37887c478bd9Sstevel@tonic-gate 		 * have made it into the archive with a uid of nobody.  If
37897c478bd9Sstevel@tonic-gate 		 * the corresponding username doesn't appear to exist, then we
37907c478bd9Sstevel@tonic-gate 		 * want to make sure it *doesn't* end up as setuid nobody!
37917c478bd9Sstevel@tonic-gate 		 *
37927c478bd9Sstevel@tonic-gate 		 * Our caller will print an error message about the fact
37937c478bd9Sstevel@tonic-gate 		 * that the restore didn't work out quite right ..
37947c478bd9Sstevel@tonic-gate 		 */
37957c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_UNAME)
37967c478bd9Sstevel@tonic-gate 			u_g_name = Xtarhdr.x_uname;
37977c478bd9Sstevel@tonic-gate 		else
37987c478bd9Sstevel@tonic-gate 			u_g_name = dblock.dbuf.uname;
37997c478bd9Sstevel@tonic-gate 		if ((duid = getuidbyname(u_g_name)) == -1) {
38007c478bd9Sstevel@tonic-gate 			if (S_ISREG(sp->st_mode) && sp->st_uid == UID_NOBODY &&
38017c478bd9Sstevel@tonic-gate 			    (sp->st_mode & S_ISUID) == S_ISUID)
38027c478bd9Sstevel@tonic-gate 				(void) chmod(name,
38037c478bd9Sstevel@tonic-gate 				    MODEMASK & sp->st_mode & ~S_ISUID);
38047c478bd9Sstevel@tonic-gate 			duid = sp->st_uid;
38057c478bd9Sstevel@tonic-gate 		}
38067c478bd9Sstevel@tonic-gate 
38077c478bd9Sstevel@tonic-gate 		/* (Ditto for gids) */
38087c478bd9Sstevel@tonic-gate 
38097c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_GNAME)
38107c478bd9Sstevel@tonic-gate 			u_g_name = Xtarhdr.x_gname;
38117c478bd9Sstevel@tonic-gate 		else
38127c478bd9Sstevel@tonic-gate 			u_g_name = dblock.dbuf.gname;
38137c478bd9Sstevel@tonic-gate 		if ((dgid = getgidbyname(u_g_name)) == -1) {
38147c478bd9Sstevel@tonic-gate 			if (S_ISREG(sp->st_mode) && sp->st_gid == GID_NOBODY &&
38157c478bd9Sstevel@tonic-gate 			    (sp->st_mode & S_ISGID) == S_ISGID)
38167c478bd9Sstevel@tonic-gate 				(void) chmod(name,
38177c478bd9Sstevel@tonic-gate 				    MODEMASK & sp->st_mode & ~S_ISGID);
38187c478bd9Sstevel@tonic-gate 			dgid = sp->st_gid;
38197c478bd9Sstevel@tonic-gate 		}
38207c478bd9Sstevel@tonic-gate 	} else if (checkflag == 2) { /* tar format and euid == 0 */
38217c478bd9Sstevel@tonic-gate 		duid = sp->st_uid;
38227c478bd9Sstevel@tonic-gate 		dgid = sp->st_gid;
38237c478bd9Sstevel@tonic-gate 	}
38247c478bd9Sstevel@tonic-gate 	if ((checkflag == 1) || (checkflag == 2))
38257c478bd9Sstevel@tonic-gate 		(void) fchownat(dirfd, name, duid, dgid, symflag);
38267c478bd9Sstevel@tonic-gate }
38277c478bd9Sstevel@tonic-gate 
38287c478bd9Sstevel@tonic-gate /*ARGSUSED*/
38297c478bd9Sstevel@tonic-gate static void
38307c478bd9Sstevel@tonic-gate onintr(int sig)
38317c478bd9Sstevel@tonic-gate {
38327c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, SIG_IGN);
38337c478bd9Sstevel@tonic-gate 	term++;
38347c478bd9Sstevel@tonic-gate }
38357c478bd9Sstevel@tonic-gate 
38367c478bd9Sstevel@tonic-gate /*ARGSUSED*/
38377c478bd9Sstevel@tonic-gate static void
38387c478bd9Sstevel@tonic-gate onquit(int sig)
38397c478bd9Sstevel@tonic-gate {
38407c478bd9Sstevel@tonic-gate 	(void) signal(SIGQUIT, SIG_IGN);
38417c478bd9Sstevel@tonic-gate 	term++;
38427c478bd9Sstevel@tonic-gate }
38437c478bd9Sstevel@tonic-gate 
38447c478bd9Sstevel@tonic-gate /*ARGSUSED*/
38457c478bd9Sstevel@tonic-gate static void
38467c478bd9Sstevel@tonic-gate onhup(int sig)
38477c478bd9Sstevel@tonic-gate {
38487c478bd9Sstevel@tonic-gate 	(void) signal(SIGHUP, SIG_IGN);
38497c478bd9Sstevel@tonic-gate 	term++;
38507c478bd9Sstevel@tonic-gate }
38517c478bd9Sstevel@tonic-gate 
38527c478bd9Sstevel@tonic-gate static void
38537c478bd9Sstevel@tonic-gate tomodes(struct stat *sp)
38547c478bd9Sstevel@tonic-gate {
38557c478bd9Sstevel@tonic-gate 	uid_t uid;
38567c478bd9Sstevel@tonic-gate 	gid_t gid;
38577c478bd9Sstevel@tonic-gate 
38587c478bd9Sstevel@tonic-gate 	bzero(dblock.dummy, TBLOCK);
38597c478bd9Sstevel@tonic-gate 
38607c478bd9Sstevel@tonic-gate 	/*
38617c478bd9Sstevel@tonic-gate 	 * If the uid or gid is too large, we can't put it into
38627c478bd9Sstevel@tonic-gate 	 * the archive.  We could fail to put anything in the
38637c478bd9Sstevel@tonic-gate 	 * archive at all .. but most of the time the name service
38647c478bd9Sstevel@tonic-gate 	 * will save the day when we do a lookup at restore time.
38657c478bd9Sstevel@tonic-gate 	 *
38667c478bd9Sstevel@tonic-gate 	 * Instead we choose a "safe" uid and gid, and fix up whether
38677c478bd9Sstevel@tonic-gate 	 * or not the setuid and setgid bits are left set to extraction
38687c478bd9Sstevel@tonic-gate 	 * time.
38697c478bd9Sstevel@tonic-gate 	 */
38707c478bd9Sstevel@tonic-gate 	if (Eflag) {
38717c478bd9Sstevel@tonic-gate 		if ((ulong_t)(uid = sp->st_uid) > (ulong_t)OCTAL7CHAR) {
38727c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_UID;
38737c478bd9Sstevel@tonic-gate 			Xtarhdr.x_uid = uid;
38747c478bd9Sstevel@tonic-gate 		}
38757c478bd9Sstevel@tonic-gate 		if ((ulong_t)(gid = sp->st_gid) > (ulong_t)OCTAL7CHAR) {
38767c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_GID;
38777c478bd9Sstevel@tonic-gate 			Xtarhdr.x_gid = gid;
38787c478bd9Sstevel@tonic-gate 		}
38797c478bd9Sstevel@tonic-gate 		if (sp->st_size > TAR_OFFSET_MAX) {
38807c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_SIZE;
38817c478bd9Sstevel@tonic-gate 			Xtarhdr.x_filesz = sp->st_size;
38827c478bd9Sstevel@tonic-gate 			(void) sprintf(dblock.dbuf.size, "%011" FMT_off_t_o,
38837c478bd9Sstevel@tonic-gate 			    (off_t)0);
38847c478bd9Sstevel@tonic-gate 		} else
38857c478bd9Sstevel@tonic-gate 			(void) sprintf(dblock.dbuf.size, "%011" FMT_off_t_o,
38867c478bd9Sstevel@tonic-gate 			    sp->st_size);
38877c478bd9Sstevel@tonic-gate 	} else {
38887c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.size, "%011" FMT_off_t_o,
38897c478bd9Sstevel@tonic-gate 		    sp->st_size);
38907c478bd9Sstevel@tonic-gate 	}
38917c478bd9Sstevel@tonic-gate 	if ((ulong_t)(uid = sp->st_uid) > (ulong_t)OCTAL7CHAR)
38927c478bd9Sstevel@tonic-gate 		uid = UID_NOBODY;
38937c478bd9Sstevel@tonic-gate 	if ((ulong_t)(gid = sp->st_gid) > (ulong_t)OCTAL7CHAR)
38947c478bd9Sstevel@tonic-gate 		gid = GID_NOBODY;
38957c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.gid, "%07lo", gid);
38967c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.uid, "%07lo", uid);
38977c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.mode, "%07lo", sp->st_mode & POSIXMODES);
38987c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.mtime, "%011lo", sp->st_mtime);
38997c478bd9Sstevel@tonic-gate }
39007c478bd9Sstevel@tonic-gate 
39017c478bd9Sstevel@tonic-gate static	int
39027c478bd9Sstevel@tonic-gate #ifdef	EUC
39037c478bd9Sstevel@tonic-gate /*
39047c478bd9Sstevel@tonic-gate  * Warning:  the result of this function depends whether 'char' is a
39057c478bd9Sstevel@tonic-gate  * signed or unsigned data type.  This a source of potential
39067c478bd9Sstevel@tonic-gate  * non-portability among heterogeneous systems.  It is retained here
39077c478bd9Sstevel@tonic-gate  * for backward compatibility.
39087c478bd9Sstevel@tonic-gate  */
39097c478bd9Sstevel@tonic-gate checksum_signed(union hblock *dblockp)
39107c478bd9Sstevel@tonic-gate #else
39117c478bd9Sstevel@tonic-gate checksum(union hblock *dblockp)
39127c478bd9Sstevel@tonic-gate #endif	/* EUC */
39137c478bd9Sstevel@tonic-gate {
39147c478bd9Sstevel@tonic-gate 	int i;
39157c478bd9Sstevel@tonic-gate 	char *cp;
39167c478bd9Sstevel@tonic-gate 
39177c478bd9Sstevel@tonic-gate 	for (cp = dblockp->dbuf.chksum;
39187c478bd9Sstevel@tonic-gate 	    cp < &dblockp->dbuf.chksum[sizeof (dblockp->dbuf.chksum)]; cp++)
39197c478bd9Sstevel@tonic-gate 		*cp = ' ';
39207c478bd9Sstevel@tonic-gate 	i = 0;
39217c478bd9Sstevel@tonic-gate 	for (cp = dblockp->dummy; cp < &(dblockp->dummy[TBLOCK]); cp++)
39227c478bd9Sstevel@tonic-gate 		i += *cp;
39237c478bd9Sstevel@tonic-gate 	return (i);
39247c478bd9Sstevel@tonic-gate }
39257c478bd9Sstevel@tonic-gate 
39267c478bd9Sstevel@tonic-gate #ifdef	EUC
39277c478bd9Sstevel@tonic-gate /*
39287c478bd9Sstevel@tonic-gate  * Generate unsigned checksum, regardless of what C compiler is
39297c478bd9Sstevel@tonic-gate  * used.  Survives in the face of arbitrary 8-bit clean filenames,
39307c478bd9Sstevel@tonic-gate  * e.g., internationalized filenames.
39317c478bd9Sstevel@tonic-gate  */
39327c478bd9Sstevel@tonic-gate static int
39337c478bd9Sstevel@tonic-gate checksum(union hblock *dblockp)
39347c478bd9Sstevel@tonic-gate {
39357c478bd9Sstevel@tonic-gate 	unsigned i;
39367c478bd9Sstevel@tonic-gate 	unsigned char *cp;
39377c478bd9Sstevel@tonic-gate 
39387c478bd9Sstevel@tonic-gate 	for (cp = (unsigned char *) dblockp->dbuf.chksum;
39397c478bd9Sstevel@tonic-gate 	    cp < (unsigned char *)
39407c478bd9Sstevel@tonic-gate 	    &(dblockp->dbuf.chksum[sizeof (dblockp->dbuf.chksum)]); cp++)
39417c478bd9Sstevel@tonic-gate 		*cp = ' ';
39427c478bd9Sstevel@tonic-gate 	i = 0;
39437c478bd9Sstevel@tonic-gate 	for (cp = (unsigned char *) dblockp->dummy;
39447c478bd9Sstevel@tonic-gate 	    cp < (unsigned char *) &(dblockp->dummy[TBLOCK]); cp++)
39457c478bd9Sstevel@tonic-gate 		i += *cp;
39467c478bd9Sstevel@tonic-gate 
39477c478bd9Sstevel@tonic-gate 	return (i);
39487c478bd9Sstevel@tonic-gate }
39497c478bd9Sstevel@tonic-gate #endif	/* EUC */
39507c478bd9Sstevel@tonic-gate 
39517c478bd9Sstevel@tonic-gate /*
39527c478bd9Sstevel@tonic-gate  * If the w flag is set, output the action to be taken and the name of the
39537c478bd9Sstevel@tonic-gate  * file.  Perform the action if the user response is affirmative.
39547c478bd9Sstevel@tonic-gate  */
39557c478bd9Sstevel@tonic-gate 
39567c478bd9Sstevel@tonic-gate static int
39577c478bd9Sstevel@tonic-gate checkw(char c, char *name)
39587c478bd9Sstevel@tonic-gate {
39597c478bd9Sstevel@tonic-gate 	if (wflag) {
39607c478bd9Sstevel@tonic-gate 		(void) fprintf(vfile, "%c ", c);
39617c478bd9Sstevel@tonic-gate 		if (vflag)
39627c478bd9Sstevel@tonic-gate 			longt(&stbuf, ' ');	/* do we have acl info here */
39637c478bd9Sstevel@tonic-gate 		(void) fprintf(vfile, "%s: ", name);
3964*3d63ea05Sas145665 		if (yes() == 1) {
39657c478bd9Sstevel@tonic-gate 			return (1);
39667c478bd9Sstevel@tonic-gate 		}
39677c478bd9Sstevel@tonic-gate 		return (0);
39687c478bd9Sstevel@tonic-gate 	}
39697c478bd9Sstevel@tonic-gate 	return (1);
39707c478bd9Sstevel@tonic-gate }
39717c478bd9Sstevel@tonic-gate 
39727c478bd9Sstevel@tonic-gate /*
39737c478bd9Sstevel@tonic-gate  * When the F flag is set, exclude RCS and SCCS directories.  If F is set
39747c478bd9Sstevel@tonic-gate  * twice, also exclude .o files, and files names errs, core, and a.out.
39757c478bd9Sstevel@tonic-gate  */
39767c478bd9Sstevel@tonic-gate 
39777c478bd9Sstevel@tonic-gate static int
39787c478bd9Sstevel@tonic-gate checkf(char *name, int mode, int howmuch)
39797c478bd9Sstevel@tonic-gate {
39807c478bd9Sstevel@tonic-gate 	int l;
39817c478bd9Sstevel@tonic-gate 
39827c478bd9Sstevel@tonic-gate 	if ((mode & S_IFMT) == S_IFDIR) {
39837c478bd9Sstevel@tonic-gate 		if ((strcmp(name, "SCCS") == 0) || (strcmp(name, "RCS") == 0))
39847c478bd9Sstevel@tonic-gate 			return (0);
39857c478bd9Sstevel@tonic-gate 		return (1);
39867c478bd9Sstevel@tonic-gate 	}
39877c478bd9Sstevel@tonic-gate 	if ((l = (int)strlen(name)) < 3)
39887c478bd9Sstevel@tonic-gate 		return (1);
39897c478bd9Sstevel@tonic-gate 	if (howmuch > 1 && name[l-2] == '.' && name[l-1] == 'o')
39907c478bd9Sstevel@tonic-gate 		return (0);
39917c478bd9Sstevel@tonic-gate 	if (howmuch > 1) {
39927c478bd9Sstevel@tonic-gate 		if (strcmp(name, "core") == 0 || strcmp(name, "errs") == 0 ||
39937c478bd9Sstevel@tonic-gate 		    strcmp(name, "a.out") == 0)
39947c478bd9Sstevel@tonic-gate 			return (0);
39957c478bd9Sstevel@tonic-gate 	}
39967c478bd9Sstevel@tonic-gate 
39977c478bd9Sstevel@tonic-gate 	/* SHOULD CHECK IF IT IS EXECUTABLE */
39987c478bd9Sstevel@tonic-gate 	return (1);
39997c478bd9Sstevel@tonic-gate }
40007c478bd9Sstevel@tonic-gate 
40017c478bd9Sstevel@tonic-gate static int
40027c478bd9Sstevel@tonic-gate response(void)
40037c478bd9Sstevel@tonic-gate {
40047c478bd9Sstevel@tonic-gate 	int c;
40057c478bd9Sstevel@tonic-gate 
40067c478bd9Sstevel@tonic-gate 	c = getchar();
40077c478bd9Sstevel@tonic-gate 	if (c != '\n')
4008eace40a5Sceastha 		while (getchar() != '\n')
4009eace40a5Sceastha 			;
40107c478bd9Sstevel@tonic-gate 	else c = 'n';
40117c478bd9Sstevel@tonic-gate 	return ((c >= 'A' && c <= 'Z') ? c + ('a'-'A') : c);
40127c478bd9Sstevel@tonic-gate }
40137c478bd9Sstevel@tonic-gate 
40147c478bd9Sstevel@tonic-gate /* Has file been modified since being put into archive? If so, return > 0. */
40157c478bd9Sstevel@tonic-gate 
40167c478bd9Sstevel@tonic-gate static int
40177c478bd9Sstevel@tonic-gate checkupdate(char *arg)
40187c478bd9Sstevel@tonic-gate {
40197c478bd9Sstevel@tonic-gate 	char name[PATH_MAX+1];
40207c478bd9Sstevel@tonic-gate 	time_t	mtime;
40217c478bd9Sstevel@tonic-gate 	long nsecs;
40227c478bd9Sstevel@tonic-gate 	off_t seekp;
40237c478bd9Sstevel@tonic-gate 	static off_t	lookup(char *);
40247c478bd9Sstevel@tonic-gate 
40257c478bd9Sstevel@tonic-gate 	rewind(tfile);
40267c478bd9Sstevel@tonic-gate 	if ((seekp = lookup(arg)) < 0)
40277c478bd9Sstevel@tonic-gate 		return (1);
40287c478bd9Sstevel@tonic-gate 	(void) fseek(tfile, seekp, 0);
40297c478bd9Sstevel@tonic-gate 	(void) fscanf(tfile, "%s %ld.%ld", name, &mtime, &nsecs);
40307c478bd9Sstevel@tonic-gate 
40317c478bd9Sstevel@tonic-gate 	/*
40327c478bd9Sstevel@tonic-gate 	 * Unless nanoseconds were stored in the file, only use seconds for
40337c478bd9Sstevel@tonic-gate 	 * comparison of time.  Nanoseconds are stored when -E is specified.
40347c478bd9Sstevel@tonic-gate 	 */
40357c478bd9Sstevel@tonic-gate 	if (Eflag == 0)
40367c478bd9Sstevel@tonic-gate 		return (stbuf.st_mtime > mtime);
40377c478bd9Sstevel@tonic-gate 
40387c478bd9Sstevel@tonic-gate 	if ((stbuf.st_mtime < mtime) ||
40397c478bd9Sstevel@tonic-gate 	    ((stbuf.st_mtime == mtime) && (stbuf.st_mtim.tv_nsec <= nsecs)))
40407c478bd9Sstevel@tonic-gate 		return (0);
40417c478bd9Sstevel@tonic-gate 	return (1);
40427c478bd9Sstevel@tonic-gate }
40437c478bd9Sstevel@tonic-gate 
40447c478bd9Sstevel@tonic-gate 
40457c478bd9Sstevel@tonic-gate /*
40467c478bd9Sstevel@tonic-gate  *	newvol	get new floppy (or tape) volume
40477c478bd9Sstevel@tonic-gate  *
40487c478bd9Sstevel@tonic-gate  *	newvol();		resets tapepos and first to TRUE, prompts for
40497c478bd9Sstevel@tonic-gate  *				for new volume, and waits.
40507c478bd9Sstevel@tonic-gate  *	if dumping, end-of-file is written onto the tape.
40517c478bd9Sstevel@tonic-gate  */
40527c478bd9Sstevel@tonic-gate 
40537c478bd9Sstevel@tonic-gate static void
40547c478bd9Sstevel@tonic-gate newvol(void)
40557c478bd9Sstevel@tonic-gate {
40567c478bd9Sstevel@tonic-gate 	int c;
40577c478bd9Sstevel@tonic-gate 
40587c478bd9Sstevel@tonic-gate 	if (dumping) {
40597c478bd9Sstevel@tonic-gate #ifdef DEBUG
40607c478bd9Sstevel@tonic-gate 		DEBUG("newvol called with 'dumping' set\n", 0, 0);
40617c478bd9Sstevel@tonic-gate #endif
40627c478bd9Sstevel@tonic-gate 		putempty((blkcnt_t)2);	/* 2 EOT marks */
40637c478bd9Sstevel@tonic-gate 		closevol();
40647c478bd9Sstevel@tonic-gate 		flushtape();
40657c478bd9Sstevel@tonic-gate 		sync();
40667c478bd9Sstevel@tonic-gate 		tapepos = 0;
40677c478bd9Sstevel@tonic-gate 	} else
40687c478bd9Sstevel@tonic-gate 		first = TRUE;
40697c478bd9Sstevel@tonic-gate 	if (close(mt) != 0)
40707c478bd9Sstevel@tonic-gate 		vperror(2, gettext("close error"));
40717c478bd9Sstevel@tonic-gate 	mt = 0;
40727c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
40737c478bd9Sstevel@tonic-gate 	    "tar: \007please insert new volume, then press RETURN."));
40747c478bd9Sstevel@tonic-gate 	(void) fseek(stdin, (off_t)0, 2);	/* scan over read-ahead */
40757c478bd9Sstevel@tonic-gate 	while ((c = getchar()) != '\n' && ! term)
40767c478bd9Sstevel@tonic-gate 		if (c == EOF)
40777c478bd9Sstevel@tonic-gate 			done(Errflg);
40787c478bd9Sstevel@tonic-gate 	if (term)
40797c478bd9Sstevel@tonic-gate 		done(Errflg);
40807c478bd9Sstevel@tonic-gate 
40817c478bd9Sstevel@tonic-gate 	errno = 0;
40827c478bd9Sstevel@tonic-gate 
40837c478bd9Sstevel@tonic-gate 	if (strcmp(usefile, "-") == 0) {
40847c478bd9Sstevel@tonic-gate 		mt = dup(1);
40857c478bd9Sstevel@tonic-gate 	} else {
40867c478bd9Sstevel@tonic-gate 		mt = open(usefile, dumping ? update : 0);
40877c478bd9Sstevel@tonic-gate 	}
40887c478bd9Sstevel@tonic-gate 
40897c478bd9Sstevel@tonic-gate 	if (mt < 0) {
40907c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
40917c478bd9Sstevel@tonic-gate 		    "tar: cannot reopen %s (%s)\n"),
40927c478bd9Sstevel@tonic-gate 		    dumping ? gettext("output") : gettext("input"), usefile);
40937c478bd9Sstevel@tonic-gate 
40947c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "update=%d, usefile=%s, mt=%d, [%s]\n",
40957c478bd9Sstevel@tonic-gate 		    update, usefile, mt, strerror(errno));
40967c478bd9Sstevel@tonic-gate 
40977c478bd9Sstevel@tonic-gate 		done(2);
40987c478bd9Sstevel@tonic-gate 	}
40997c478bd9Sstevel@tonic-gate }
41007c478bd9Sstevel@tonic-gate 
41017c478bd9Sstevel@tonic-gate /*
41027c478bd9Sstevel@tonic-gate  * Write a trailer portion to close out the current output volume.
41037c478bd9Sstevel@tonic-gate  */
41047c478bd9Sstevel@tonic-gate 
41057c478bd9Sstevel@tonic-gate static void
41067c478bd9Sstevel@tonic-gate closevol(void)
41077c478bd9Sstevel@tonic-gate {
41087c478bd9Sstevel@tonic-gate 	if (mulvol) {
41097c478bd9Sstevel@tonic-gate 		/*
41107c478bd9Sstevel@tonic-gate 		 * blocklim does not count the 2 EOT marks;
41117c478bd9Sstevel@tonic-gate 		 * tapepos  does count the 2 EOT marks;
41127c478bd9Sstevel@tonic-gate 		 * therefore we need the +2 below.
41137c478bd9Sstevel@tonic-gate 		 */
41147c478bd9Sstevel@tonic-gate 		putempty(blocklim + (blkcnt_t)2 - tapepos);
41157c478bd9Sstevel@tonic-gate 	}
41167c478bd9Sstevel@tonic-gate }
41177c478bd9Sstevel@tonic-gate 
41187c478bd9Sstevel@tonic-gate static void
41197c478bd9Sstevel@tonic-gate done(int n)
41207c478bd9Sstevel@tonic-gate {
41217c478bd9Sstevel@tonic-gate 	(void) unlink(tname);
41227c478bd9Sstevel@tonic-gate 	if (mt > 0) {
41237c478bd9Sstevel@tonic-gate 		if ((close(mt) != 0) || (fclose(stdout) != 0)) {
41247c478bd9Sstevel@tonic-gate 			perror(gettext("tar: close error"));
41257c478bd9Sstevel@tonic-gate 			exit(2);
41267c478bd9Sstevel@tonic-gate 		}
41277c478bd9Sstevel@tonic-gate 	}
41287c478bd9Sstevel@tonic-gate 	exit(n);
41297c478bd9Sstevel@tonic-gate }
41307c478bd9Sstevel@tonic-gate 
41317c478bd9Sstevel@tonic-gate /*
41327c478bd9Sstevel@tonic-gate  * Determine if s1 is a prefix portion of s2 (or the same as s2).
41337c478bd9Sstevel@tonic-gate  */
41347c478bd9Sstevel@tonic-gate 
41357c478bd9Sstevel@tonic-gate static	int
41367c478bd9Sstevel@tonic-gate is_prefix(char *s1, char *s2)
41377c478bd9Sstevel@tonic-gate {
41387c478bd9Sstevel@tonic-gate 	while (*s1)
41397c478bd9Sstevel@tonic-gate 		if (*s1++ != *s2++)
41407c478bd9Sstevel@tonic-gate 			return (0);
41417c478bd9Sstevel@tonic-gate 	if (*s2)
41427c478bd9Sstevel@tonic-gate 		return (*s2 == '/');
41437c478bd9Sstevel@tonic-gate 	return (1);
41447c478bd9Sstevel@tonic-gate }
41457c478bd9Sstevel@tonic-gate 
41467c478bd9Sstevel@tonic-gate /*
41477c478bd9Sstevel@tonic-gate  * lookup and bsrch look through tfile entries to find a match for a name.
41487c478bd9Sstevel@tonic-gate  * The name can be up to PATH_MAX bytes.  bsrch compares what it sees between
41497c478bd9Sstevel@tonic-gate  * a pair of newline chars, so the buffer it uses must be long enough for
41507c478bd9Sstevel@tonic-gate  * two lines:  name and modification time as well as period, newline and space.
41517c478bd9Sstevel@tonic-gate  *
41527c478bd9Sstevel@tonic-gate  * A kludge was added to bsrch to take care of matching on the first entry
41537c478bd9Sstevel@tonic-gate  * in the file--there is no leading newline.  So, if we are reading from the
41547c478bd9Sstevel@tonic-gate  * start of the file, read into byte two and set the first byte to a newline.
41557c478bd9Sstevel@tonic-gate  * Otherwise, the first entry cannot be matched.
41567c478bd9Sstevel@tonic-gate  *
41577c478bd9Sstevel@tonic-gate  */
41587c478bd9Sstevel@tonic-gate 
41597c478bd9Sstevel@tonic-gate #define	N	(2 * (PATH_MAX + TIME_MAX_DIGITS + LONG_MAX_DIGITS + 3))
41607c478bd9Sstevel@tonic-gate static	off_t
41617c478bd9Sstevel@tonic-gate lookup(char *s)
41627c478bd9Sstevel@tonic-gate {
41637c478bd9Sstevel@tonic-gate 	int i;
41647c478bd9Sstevel@tonic-gate 	off_t a;
41657c478bd9Sstevel@tonic-gate 
41667c478bd9Sstevel@tonic-gate 	for (i = 0; s[i]; i++)
41677c478bd9Sstevel@tonic-gate 		if (s[i] == ' ')
41687c478bd9Sstevel@tonic-gate 			break;
41697c478bd9Sstevel@tonic-gate 	a = bsrch(s, i, low, high);
41707c478bd9Sstevel@tonic-gate 	return (a);
41717c478bd9Sstevel@tonic-gate }
41727c478bd9Sstevel@tonic-gate 
41737c478bd9Sstevel@tonic-gate static off_t
41747c478bd9Sstevel@tonic-gate bsrch(char *s, int n, off_t l, off_t h)
41757c478bd9Sstevel@tonic-gate {
41767c478bd9Sstevel@tonic-gate 	int i, j;
41777c478bd9Sstevel@tonic-gate 	char b[N];
41787c478bd9Sstevel@tonic-gate 	off_t m, m1;
41797c478bd9Sstevel@tonic-gate 
41807c478bd9Sstevel@tonic-gate 
41817c478bd9Sstevel@tonic-gate loop:
41827c478bd9Sstevel@tonic-gate 	if (l >= h)
41837c478bd9Sstevel@tonic-gate 		return ((off_t)-1);
41847c478bd9Sstevel@tonic-gate 	m = l + (h-l)/2 - N/2;
41857c478bd9Sstevel@tonic-gate 	if (m < l)
41867c478bd9Sstevel@tonic-gate 		m = l;
41877c478bd9Sstevel@tonic-gate 	(void) fseek(tfile, m, 0);
41887c478bd9Sstevel@tonic-gate 	if (m == 0) {
41897c478bd9Sstevel@tonic-gate 		(void) fread(b+1, 1, N-1, tfile);
41907c478bd9Sstevel@tonic-gate 		b[0] = '\n';
41917c478bd9Sstevel@tonic-gate 		m--;
41927c478bd9Sstevel@tonic-gate 	} else
41937c478bd9Sstevel@tonic-gate 		(void) fread(b, 1, N, tfile);
41947c478bd9Sstevel@tonic-gate 	for (i = 0; i < N; i++) {
41957c478bd9Sstevel@tonic-gate 		if (b[i] == '\n')
41967c478bd9Sstevel@tonic-gate 			break;
41977c478bd9Sstevel@tonic-gate 		m++;
41987c478bd9Sstevel@tonic-gate 	}
41997c478bd9Sstevel@tonic-gate 	if (m >= h)
42007c478bd9Sstevel@tonic-gate 		return ((off_t)-1);
42017c478bd9Sstevel@tonic-gate 	m1 = m;
42027c478bd9Sstevel@tonic-gate 	j = i;
42037c478bd9Sstevel@tonic-gate 	for (i++; i < N; i++) {
42047c478bd9Sstevel@tonic-gate 		m1++;
42057c478bd9Sstevel@tonic-gate 		if (b[i] == '\n')
42067c478bd9Sstevel@tonic-gate 			break;
42077c478bd9Sstevel@tonic-gate 	}
42087c478bd9Sstevel@tonic-gate 	i = cmp(b+j, s, n);
42097c478bd9Sstevel@tonic-gate 	if (i < 0) {
42107c478bd9Sstevel@tonic-gate 		h = m;
42117c478bd9Sstevel@tonic-gate 		goto loop;
42127c478bd9Sstevel@tonic-gate 	}
42137c478bd9Sstevel@tonic-gate 	if (i > 0) {
42147c478bd9Sstevel@tonic-gate 		l = m1;
42157c478bd9Sstevel@tonic-gate 		goto loop;
42167c478bd9Sstevel@tonic-gate 	}
42177c478bd9Sstevel@tonic-gate 	if (m < 0)
42187c478bd9Sstevel@tonic-gate 		m = 0;
42197c478bd9Sstevel@tonic-gate 	return (m);
42207c478bd9Sstevel@tonic-gate }
42217c478bd9Sstevel@tonic-gate 
42227c478bd9Sstevel@tonic-gate static int
42237c478bd9Sstevel@tonic-gate cmp(char *b, char *s, int n)
42247c478bd9Sstevel@tonic-gate {
42257c478bd9Sstevel@tonic-gate 	int i;
42267c478bd9Sstevel@tonic-gate 
42277c478bd9Sstevel@tonic-gate 	assert(b[0] == '\n');
42287c478bd9Sstevel@tonic-gate 
42297c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
42307c478bd9Sstevel@tonic-gate 		if (b[i+1] > s[i])
42317c478bd9Sstevel@tonic-gate 			return (-1);
42327c478bd9Sstevel@tonic-gate 		if (b[i+1] < s[i])
42337c478bd9Sstevel@tonic-gate 			return (1);
42347c478bd9Sstevel@tonic-gate 	}
42357c478bd9Sstevel@tonic-gate 	return (b[i+1] == ' '? 0 : -1);
42367c478bd9Sstevel@tonic-gate }
42377c478bd9Sstevel@tonic-gate 
42387c478bd9Sstevel@tonic-gate 
42397c478bd9Sstevel@tonic-gate /*
42407c478bd9Sstevel@tonic-gate  *	seekdisk	seek to next file on archive
42417c478bd9Sstevel@tonic-gate  *
42427c478bd9Sstevel@tonic-gate  *	called by passtape() only
42437c478bd9Sstevel@tonic-gate  *
42447c478bd9Sstevel@tonic-gate  *	WARNING: expects "nblock" to be set, that is, readtape() to have
42457c478bd9Sstevel@tonic-gate  *		already been called.  Since passtape() is only called
42467c478bd9Sstevel@tonic-gate  *		after a file header block has been read (why else would
42477c478bd9Sstevel@tonic-gate  *		we skip to next file?), this is currently safe.
42487c478bd9Sstevel@tonic-gate  *
42497c478bd9Sstevel@tonic-gate  *	changed to guarantee SYS_BLOCK boundary
42507c478bd9Sstevel@tonic-gate  */
42517c478bd9Sstevel@tonic-gate 
42527c478bd9Sstevel@tonic-gate static void
42537c478bd9Sstevel@tonic-gate seekdisk(blkcnt_t blocks)
42547c478bd9Sstevel@tonic-gate {
42557c478bd9Sstevel@tonic-gate 	off_t seekval;
42567c478bd9Sstevel@tonic-gate #if SYS_BLOCK > TBLOCK
42577c478bd9Sstevel@tonic-gate 	/* handle non-multiple of SYS_BLOCK */
42587c478bd9Sstevel@tonic-gate 	blkcnt_t nxb;	/* # extra blocks */
42597c478bd9Sstevel@tonic-gate #endif
42607c478bd9Sstevel@tonic-gate 
42617c478bd9Sstevel@tonic-gate 	tapepos += blocks;
42627c478bd9Sstevel@tonic-gate #ifdef DEBUG
42637c478bd9Sstevel@tonic-gate 	DEBUG("seekdisk(%" FMT_blkcnt_t ") called\n", blocks, 0);
42647c478bd9Sstevel@tonic-gate #endif
42657c478bd9Sstevel@tonic-gate 	if (recno + blocks <= nblock) {
42667c478bd9Sstevel@tonic-gate 		recno += blocks;
42677c478bd9Sstevel@tonic-gate 		return;
42687c478bd9Sstevel@tonic-gate 	}
42697c478bd9Sstevel@tonic-gate 	if (recno > nblock)
42707c478bd9Sstevel@tonic-gate 		recno = nblock;
42717c478bd9Sstevel@tonic-gate 	seekval = (off_t)blocks - (nblock - recno);
42727c478bd9Sstevel@tonic-gate 	recno = nblock;	/* so readtape() reads next time through */
42737c478bd9Sstevel@tonic-gate #if SYS_BLOCK > TBLOCK
42747c478bd9Sstevel@tonic-gate 	nxb = (blkcnt_t)(seekval % (off_t)(SYS_BLOCK / TBLOCK));
42757c478bd9Sstevel@tonic-gate #ifdef DEBUG
42767c478bd9Sstevel@tonic-gate 	DEBUG("xtrablks=%" FMT_blkcnt_t " seekval=%" FMT_blkcnt_t " blks\n",
42777c478bd9Sstevel@tonic-gate 	    nxb, seekval);
42787c478bd9Sstevel@tonic-gate #endif
42797c478bd9Sstevel@tonic-gate 	if (nxb && nxb > seekval) /* don't seek--we'll read */
42807c478bd9Sstevel@tonic-gate 		goto noseek;
42817c478bd9Sstevel@tonic-gate 	seekval -=  nxb;	/* don't seek quite so far */
42827c478bd9Sstevel@tonic-gate #endif
42837c478bd9Sstevel@tonic-gate 	if (lseek(mt, (off_t)(TBLOCK * seekval), 1) == (off_t)-1) {
42847c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
42857c478bd9Sstevel@tonic-gate 		    "tar: device seek error\n"));
42867c478bd9Sstevel@tonic-gate 		done(3);
42877c478bd9Sstevel@tonic-gate 	}
42887c478bd9Sstevel@tonic-gate #if SYS_BLOCK > TBLOCK
42897c478bd9Sstevel@tonic-gate 	/* read those extra blocks */
42907c478bd9Sstevel@tonic-gate noseek:
42917c478bd9Sstevel@tonic-gate 	if (nxb) {
42927c478bd9Sstevel@tonic-gate #ifdef DEBUG
42937c478bd9Sstevel@tonic-gate 		DEBUG("reading extra blocks\n", 0, 0);
42947c478bd9Sstevel@tonic-gate #endif
42957c478bd9Sstevel@tonic-gate 		if (read(mt, tbuf, TBLOCK*nblock) < 0) {
42967c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
42977c478bd9Sstevel@tonic-gate 			    "tar: read error while skipping file\n"));
42987c478bd9Sstevel@tonic-gate 			done(8);
42997c478bd9Sstevel@tonic-gate 		}
43007c478bd9Sstevel@tonic-gate 		recno = nxb;	/* so we don't read in next readtape() */
43017c478bd9Sstevel@tonic-gate 	}
43027c478bd9Sstevel@tonic-gate #endif
43037c478bd9Sstevel@tonic-gate }
43047c478bd9Sstevel@tonic-gate 
43057c478bd9Sstevel@tonic-gate static void
43067c478bd9Sstevel@tonic-gate readtape(char *buffer)
43077c478bd9Sstevel@tonic-gate {
43087c478bd9Sstevel@tonic-gate 	int i, j;
43097c478bd9Sstevel@tonic-gate 
43107c478bd9Sstevel@tonic-gate 	++tapepos;
43117c478bd9Sstevel@tonic-gate 	if (recno >= nblock || first) {
43127c478bd9Sstevel@tonic-gate 		if (first) {
43137c478bd9Sstevel@tonic-gate 			/*
43147c478bd9Sstevel@tonic-gate 			 * set the number of blocks to read initially, based on
43157c478bd9Sstevel@tonic-gate 			 * the defined defaults for the device, or on the
43167c478bd9Sstevel@tonic-gate 			 * explicit block factor given.
43177c478bd9Sstevel@tonic-gate 			 */
43187c478bd9Sstevel@tonic-gate 			if (bflag || defaults_used)
43197c478bd9Sstevel@tonic-gate 				j = nblock;
43207c478bd9Sstevel@tonic-gate 			else
43217c478bd9Sstevel@tonic-gate 				j = NBLOCK;
43227c478bd9Sstevel@tonic-gate 		} else
43237c478bd9Sstevel@tonic-gate 			j = nblock;
43247c478bd9Sstevel@tonic-gate 
43257c478bd9Sstevel@tonic-gate 		if ((i = read(mt, tbuf, TBLOCK*j)) < 0) {
43267c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
43277c478bd9Sstevel@tonic-gate 			    "tar: tape read error\n"));
43287c478bd9Sstevel@tonic-gate 			done(3);
43297c478bd9Sstevel@tonic-gate 		/*
4330b35e803cSss161016 		 * i == 0 and !rflag means that EOF is reached and we are
4331b35e803cSss161016 		 * trying to update or replace an empty tar file, so exit
4332b35e803cSss161016 		 * with an error.
4333b35e803cSss161016 		 *
4334b35e803cSss161016 		 * If i == 0 and !first and NotTape, it means the pointer
4335b35e803cSss161016 		 * has gone past the EOF. It could happen if two processes
4336b35e803cSss161016 		 * try to update the same tar file simultaneously. So exit
4337b35e803cSss161016 		 * with an error.
43387c478bd9Sstevel@tonic-gate 		 */
4339b35e803cSss161016 
4340b35e803cSss161016 		} else if (i == 0) {
4341b35e803cSss161016 			if (first && !rflag) {
43427c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
43437c478bd9Sstevel@tonic-gate 				    "tar: blocksize = %d\n"), i);
43447c478bd9Sstevel@tonic-gate 				done(Errflg);
4345b35e803cSss161016 			} else if (!first && (!rflag || NotTape)) {
43467c478bd9Sstevel@tonic-gate 				mterr("read", 0, 2);
4347b35e803cSss161016 			}
43487c478bd9Sstevel@tonic-gate 		} else if ((!first || Bflag) && i != TBLOCK*j) {
43497c478bd9Sstevel@tonic-gate 			/*
43507c478bd9Sstevel@tonic-gate 			 * Short read - try to get the remaining bytes.
43517c478bd9Sstevel@tonic-gate 			 */
43527c478bd9Sstevel@tonic-gate 
43537c478bd9Sstevel@tonic-gate 			int remaining = (TBLOCK * j) - i;
43547c478bd9Sstevel@tonic-gate 			char *b = (char *)tbuf + i;
43557c478bd9Sstevel@tonic-gate 			int r;
43567c478bd9Sstevel@tonic-gate 
43577c478bd9Sstevel@tonic-gate 			do {
43587c478bd9Sstevel@tonic-gate 				if ((r = read(mt, b, remaining)) < 0) {
43597c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
43607c478bd9Sstevel@tonic-gate 					    gettext("tar: tape read error\n"));
43617c478bd9Sstevel@tonic-gate 					done(3);
43627c478bd9Sstevel@tonic-gate 				}
43637c478bd9Sstevel@tonic-gate 				b += r;
43647c478bd9Sstevel@tonic-gate 				remaining -= r;
43657c478bd9Sstevel@tonic-gate 				i += r;
43667c478bd9Sstevel@tonic-gate 			} while (remaining > 0 && r != 0);
43677c478bd9Sstevel@tonic-gate 		}
43687c478bd9Sstevel@tonic-gate 		if (first) {
43697c478bd9Sstevel@tonic-gate 			if ((i % TBLOCK) != 0) {
43707c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
43717c478bd9Sstevel@tonic-gate 				    "tar: tape blocksize error\n"));
43727c478bd9Sstevel@tonic-gate 				done(3);
43737c478bd9Sstevel@tonic-gate 			}
43747c478bd9Sstevel@tonic-gate 			i /= TBLOCK;
43757c478bd9Sstevel@tonic-gate 			if (vflag && i != nblock && i != 1) {
43767c478bd9Sstevel@tonic-gate 				if (!NotTape)
43777c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
43787c478bd9Sstevel@tonic-gate 					    "tar: blocksize = %d\n"), i);
43797c478bd9Sstevel@tonic-gate 			}
43807c478bd9Sstevel@tonic-gate 
43817c478bd9Sstevel@tonic-gate 			/*
43827c478bd9Sstevel@tonic-gate 			 * If we are reading a tape, then a short read is
43837c478bd9Sstevel@tonic-gate 			 * understood to signify that the amount read is
43847c478bd9Sstevel@tonic-gate 			 * the tape's actual blocking factor.  We adapt
43857c478bd9Sstevel@tonic-gate 			 * nblock accordingly.  There is no reason to do
43867c478bd9Sstevel@tonic-gate 			 * this when the device is not blocked.
43877c478bd9Sstevel@tonic-gate 			 */
43887c478bd9Sstevel@tonic-gate 
43897c478bd9Sstevel@tonic-gate 			if (!NotTape)
43907c478bd9Sstevel@tonic-gate 				nblock = i;
43917c478bd9Sstevel@tonic-gate 		}
43927c478bd9Sstevel@tonic-gate 		recno = 0;
43937c478bd9Sstevel@tonic-gate 	}
43947c478bd9Sstevel@tonic-gate 
43957c478bd9Sstevel@tonic-gate 	first = FALSE;
43967c478bd9Sstevel@tonic-gate 	copy(buffer, &tbuf[recno++]);
43977c478bd9Sstevel@tonic-gate }
43987c478bd9Sstevel@tonic-gate 
43997c478bd9Sstevel@tonic-gate 
44007c478bd9Sstevel@tonic-gate /*
44017c478bd9Sstevel@tonic-gate  * replacement for writetape.
44027c478bd9Sstevel@tonic-gate  */
44037c478bd9Sstevel@tonic-gate 
44047c478bd9Sstevel@tonic-gate static int
44057c478bd9Sstevel@tonic-gate writetbuf(char *buffer, int n)
44067c478bd9Sstevel@tonic-gate {
44077c478bd9Sstevel@tonic-gate 	int i;
44087c478bd9Sstevel@tonic-gate 
44097c478bd9Sstevel@tonic-gate 	tapepos += n;		/* output block count */
44107c478bd9Sstevel@tonic-gate 
44117c478bd9Sstevel@tonic-gate 	if (recno >= nblock) {
44127c478bd9Sstevel@tonic-gate 		i = write(mt, (char *)tbuf, TBLOCK*nblock);
44137c478bd9Sstevel@tonic-gate 		if (i != TBLOCK*nblock)
44147c478bd9Sstevel@tonic-gate 			mterr("write", i, 2);
44157c478bd9Sstevel@tonic-gate 		recno = 0;
44167c478bd9Sstevel@tonic-gate 	}
44177c478bd9Sstevel@tonic-gate 
44187c478bd9Sstevel@tonic-gate 	/*
44197c478bd9Sstevel@tonic-gate 	 *  Special case:  We have an empty tape buffer, and the
44207c478bd9Sstevel@tonic-gate 	 *  users data size is >= the tape block size:  Avoid
44217c478bd9Sstevel@tonic-gate 	 *  the bcopy and dma direct to tape.  BIG WIN.  Add the
44227c478bd9Sstevel@tonic-gate 	 *  residual to the tape buffer.
44237c478bd9Sstevel@tonic-gate 	 */
44247c478bd9Sstevel@tonic-gate 	while (recno == 0 && n >= nblock) {
44257c478bd9Sstevel@tonic-gate 		i = (int)write(mt, buffer, TBLOCK*nblock);
44267c478bd9Sstevel@tonic-gate 		if (i != TBLOCK*nblock)
44277c478bd9Sstevel@tonic-gate 			mterr("write", i, 2);
44287c478bd9Sstevel@tonic-gate 		n -= nblock;
44297c478bd9Sstevel@tonic-gate 		buffer += (nblock * TBLOCK);
44307c478bd9Sstevel@tonic-gate 	}
44317c478bd9Sstevel@tonic-gate 
44327c478bd9Sstevel@tonic-gate 	while (n-- > 0) {
44337c478bd9Sstevel@tonic-gate 		(void) memcpy((char *)&tbuf[recno++], buffer, TBLOCK);
44347c478bd9Sstevel@tonic-gate 		buffer += TBLOCK;
44357c478bd9Sstevel@tonic-gate 		if (recno >= nblock) {
44367c478bd9Sstevel@tonic-gate 			i = (int)write(mt, (char *)tbuf, TBLOCK*nblock);
44377c478bd9Sstevel@tonic-gate 			if (i != TBLOCK*nblock)
44387c478bd9Sstevel@tonic-gate 				mterr("write", i, 2);
44397c478bd9Sstevel@tonic-gate 			recno = 0;
44407c478bd9Sstevel@tonic-gate 		}
44417c478bd9Sstevel@tonic-gate 	}
44427c478bd9Sstevel@tonic-gate 
44437c478bd9Sstevel@tonic-gate 	/* Tell the user how much to write to get in sync */
44447c478bd9Sstevel@tonic-gate 	return (nblock - recno);
44457c478bd9Sstevel@tonic-gate }
44467c478bd9Sstevel@tonic-gate 
44477c478bd9Sstevel@tonic-gate /*
44487c478bd9Sstevel@tonic-gate  *	backtape - reposition tape after reading soft "EOF" record
44497c478bd9Sstevel@tonic-gate  *
44507c478bd9Sstevel@tonic-gate  *	Backtape tries to reposition the tape back over the EOF
44517c478bd9Sstevel@tonic-gate  *	record.  This is for the 'u' and 'r' function letters so that the
44527c478bd9Sstevel@tonic-gate  *	tape can be extended.  This code is not well designed, but
44537c478bd9Sstevel@tonic-gate  *	I'm confident that the only callers who care about the
44547c478bd9Sstevel@tonic-gate  *	backspace-over-EOF feature are those involved in 'u' and 'r'.
44557c478bd9Sstevel@tonic-gate  *
44567c478bd9Sstevel@tonic-gate  *	The proper way to backup the tape is through the use of mtio.
44577c478bd9Sstevel@tonic-gate  *	Earlier spins used lseek combined with reads in a confusing
44587c478bd9Sstevel@tonic-gate  *	maneuver that only worked on 4.x, but shouldn't have, even
44597c478bd9Sstevel@tonic-gate  *	there.  Lseeks are explicitly not supported for tape devices.
44607c478bd9Sstevel@tonic-gate  */
44617c478bd9Sstevel@tonic-gate 
44627c478bd9Sstevel@tonic-gate static void
44637c478bd9Sstevel@tonic-gate backtape(void)
44647c478bd9Sstevel@tonic-gate {
44657c478bd9Sstevel@tonic-gate 	struct mtop mtcmd;
44667c478bd9Sstevel@tonic-gate #ifdef DEBUG
44677c478bd9Sstevel@tonic-gate 	DEBUG("backtape() called, recno=%" FMT_blkcnt_t " nblock=%d\n", recno,
44687c478bd9Sstevel@tonic-gate 	    nblock);
44697c478bd9Sstevel@tonic-gate #endif
44707c478bd9Sstevel@tonic-gate 	/*
44717c478bd9Sstevel@tonic-gate 	 * Backup to the position in the archive where the record
44727c478bd9Sstevel@tonic-gate 	 * currently sitting in the tbuf buffer is situated.
44737c478bd9Sstevel@tonic-gate 	 */
44747c478bd9Sstevel@tonic-gate 
44757c478bd9Sstevel@tonic-gate 	if (NotTape) {
44767c478bd9Sstevel@tonic-gate 		/*
44777c478bd9Sstevel@tonic-gate 		 * For non-tape devices, this means lseeking to the
44787c478bd9Sstevel@tonic-gate 		 * correct position.  The absolute location tapepos-recno
44797c478bd9Sstevel@tonic-gate 		 * should be the beginning of the current record.
44807c478bd9Sstevel@tonic-gate 		 */
44817c478bd9Sstevel@tonic-gate 
44827c478bd9Sstevel@tonic-gate 		if (lseek(mt, (off_t)(TBLOCK*(tapepos-recno)), SEEK_SET) ==
44837c478bd9Sstevel@tonic-gate 		    (off_t)-1) {
44847c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
44857c478bd9Sstevel@tonic-gate 			    gettext("tar: lseek to end of archive failed\n"));
44867c478bd9Sstevel@tonic-gate 			done(4);
44877c478bd9Sstevel@tonic-gate 		}
44887c478bd9Sstevel@tonic-gate 	} else {
44897c478bd9Sstevel@tonic-gate 		/*
44907c478bd9Sstevel@tonic-gate 		 * For tape devices, we backup over the most recently
44917c478bd9Sstevel@tonic-gate 		 * read record.
44927c478bd9Sstevel@tonic-gate 		 */
44937c478bd9Sstevel@tonic-gate 
44947c478bd9Sstevel@tonic-gate 		mtcmd.mt_op = MTBSR;
44957c478bd9Sstevel@tonic-gate 		mtcmd.mt_count = 1;
44967c478bd9Sstevel@tonic-gate 
44977c478bd9Sstevel@tonic-gate 		if (ioctl(mt, MTIOCTOP, &mtcmd) < 0) {
44987c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
44997c478bd9Sstevel@tonic-gate 			    gettext("tar: backspace over record failed\n"));
45007c478bd9Sstevel@tonic-gate 			done(4);
45017c478bd9Sstevel@tonic-gate 		}
45027c478bd9Sstevel@tonic-gate 	}
45037c478bd9Sstevel@tonic-gate 
45047c478bd9Sstevel@tonic-gate 	/*
45057c478bd9Sstevel@tonic-gate 	 * Decrement the tape and tbuf buffer indices to prepare for the
45067c478bd9Sstevel@tonic-gate 	 * coming write to overwrite the soft EOF record.
45077c478bd9Sstevel@tonic-gate 	 */
45087c478bd9Sstevel@tonic-gate 
45097c478bd9Sstevel@tonic-gate 	recno--;
45107c478bd9Sstevel@tonic-gate 	tapepos--;
45117c478bd9Sstevel@tonic-gate }
45127c478bd9Sstevel@tonic-gate 
45137c478bd9Sstevel@tonic-gate 
45147c478bd9Sstevel@tonic-gate /*
45157c478bd9Sstevel@tonic-gate  *	flushtape  write buffered block(s) onto tape
45167c478bd9Sstevel@tonic-gate  *
45177c478bd9Sstevel@tonic-gate  *      recno points to next free block in tbuf.  If nonzero, a write is done.
45187c478bd9Sstevel@tonic-gate  *	Care is taken to write in multiples of SYS_BLOCK when device is
45197c478bd9Sstevel@tonic-gate  *	non-magtape in case raw i/o is used.
45207c478bd9Sstevel@tonic-gate  *
45217c478bd9Sstevel@tonic-gate  *	NOTE: this is called by writetape() to do the actual writing
45227c478bd9Sstevel@tonic-gate  */
45237c478bd9Sstevel@tonic-gate 
45247c478bd9Sstevel@tonic-gate static void
45257c478bd9Sstevel@tonic-gate flushtape(void)
45267c478bd9Sstevel@tonic-gate {
45277c478bd9Sstevel@tonic-gate #ifdef DEBUG
45287c478bd9Sstevel@tonic-gate 	DEBUG("flushtape() called, recno=%" FMT_blkcnt_t "\n", recno, 0);
45297c478bd9Sstevel@tonic-gate #endif
45307c478bd9Sstevel@tonic-gate 	if (recno > 0) {	/* anything buffered? */
45317c478bd9Sstevel@tonic-gate 		if (NotTape) {
45327c478bd9Sstevel@tonic-gate #if SYS_BLOCK > TBLOCK
45337c478bd9Sstevel@tonic-gate 			int i;
45347c478bd9Sstevel@tonic-gate 
45357c478bd9Sstevel@tonic-gate 			/*
45367c478bd9Sstevel@tonic-gate 			 * an odd-block write can only happen when
45377c478bd9Sstevel@tonic-gate 			 * we are at the end of a volume that is not a tape.
45387c478bd9Sstevel@tonic-gate 			 * Here we round recno up to an even SYS_BLOCK
45397c478bd9Sstevel@tonic-gate 			 * boundary.
45407c478bd9Sstevel@tonic-gate 			 */
45417c478bd9Sstevel@tonic-gate 			if ((i = recno % (SYS_BLOCK / TBLOCK)) != 0) {
45427c478bd9Sstevel@tonic-gate #ifdef DEBUG
45437c478bd9Sstevel@tonic-gate 				DEBUG("flushtape() %d rounding blocks\n", i, 0);
45447c478bd9Sstevel@tonic-gate #endif
45457c478bd9Sstevel@tonic-gate 				recno += i;	/* round up to even SYS_BLOCK */
45467c478bd9Sstevel@tonic-gate 			}
45477c478bd9Sstevel@tonic-gate #endif
45487c478bd9Sstevel@tonic-gate 			if (recno > nblock)
45497c478bd9Sstevel@tonic-gate 				recno = nblock;
45507c478bd9Sstevel@tonic-gate 		}
45517c478bd9Sstevel@tonic-gate #ifdef DEBUG
45527c478bd9Sstevel@tonic-gate 		DEBUG("writing out %" FMT_blkcnt_t " blocks of %" FMT_blkcnt_t
45537c478bd9Sstevel@tonic-gate 		    " bytes\n", (blkcnt_t)(NotTape ? recno : nblock),
45547c478bd9Sstevel@tonic-gate 		    (blkcnt_t)(NotTape ? recno : nblock) * TBLOCK);
45557c478bd9Sstevel@tonic-gate #endif
45567c478bd9Sstevel@tonic-gate 		if (write(mt, tbuf,
45577c478bd9Sstevel@tonic-gate 		    (size_t)(NotTape ? recno : nblock) * TBLOCK) < 0) {
45587c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
45597c478bd9Sstevel@tonic-gate 			    "tar: tape write error\n"));
45607c478bd9Sstevel@tonic-gate 			done(2);
45617c478bd9Sstevel@tonic-gate 		}
45627c478bd9Sstevel@tonic-gate 		recno = 0;
45637c478bd9Sstevel@tonic-gate 	}
45647c478bd9Sstevel@tonic-gate }
45657c478bd9Sstevel@tonic-gate 
45667c478bd9Sstevel@tonic-gate static void
45677c478bd9Sstevel@tonic-gate copy(void *dst, void *src)
45687c478bd9Sstevel@tonic-gate {
45697c478bd9Sstevel@tonic-gate 	(void) memcpy(dst, src, TBLOCK);
45707c478bd9Sstevel@tonic-gate }
45717c478bd9Sstevel@tonic-gate 
45727c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
45737c478bd9Sstevel@tonic-gate /*
45747c478bd9Sstevel@tonic-gate  *	initarg -- initialize things for nextarg.
45757c478bd9Sstevel@tonic-gate  *
45767c478bd9Sstevel@tonic-gate  *	argv		filename list, a la argv.
45777c478bd9Sstevel@tonic-gate  *	filefile	name of file containing filenames.  Unless doing
45787c478bd9Sstevel@tonic-gate  *		a create, seeks must be allowable (e.g. no named pipes).
45797c478bd9Sstevel@tonic-gate  *
45807c478bd9Sstevel@tonic-gate  *	- if filefile is non-NULL, it will be used first, and argv will
45817c478bd9Sstevel@tonic-gate  *	be used when the data in filefile are exhausted.
45827c478bd9Sstevel@tonic-gate  *	- otherwise argv will be used.
45837c478bd9Sstevel@tonic-gate  */
45847c478bd9Sstevel@tonic-gate static char **Cmdargv = NULL;
45857c478bd9Sstevel@tonic-gate static FILE *FILEFile = NULL;
45867c478bd9Sstevel@tonic-gate static long seekFile = -1;
45877c478bd9Sstevel@tonic-gate static char *ptrtoFile, *begofFile, *endofFile;
45887c478bd9Sstevel@tonic-gate 
45897c478bd9Sstevel@tonic-gate static	void
45907c478bd9Sstevel@tonic-gate initarg(char *argv[], char *filefile)
45917c478bd9Sstevel@tonic-gate {
45927c478bd9Sstevel@tonic-gate 	struct stat statbuf;
45937c478bd9Sstevel@tonic-gate 	char *p;
45947c478bd9Sstevel@tonic-gate 	int nbytes;
45957c478bd9Sstevel@tonic-gate 
45967c478bd9Sstevel@tonic-gate 	Cmdargv = argv;
45977c478bd9Sstevel@tonic-gate 	if (filefile == NULL)
45987c478bd9Sstevel@tonic-gate 		return;		/* no -F file */
45997c478bd9Sstevel@tonic-gate 	if (FILEFile != NULL) {
46007c478bd9Sstevel@tonic-gate 		/*
46017c478bd9Sstevel@tonic-gate 		 * need to REinitialize
46027c478bd9Sstevel@tonic-gate 		 */
46037c478bd9Sstevel@tonic-gate 		if (seekFile != -1)
46047c478bd9Sstevel@tonic-gate 			(void) fseek(FILEFile, seekFile, 0);
46057c478bd9Sstevel@tonic-gate 		ptrtoFile = begofFile;
46067c478bd9Sstevel@tonic-gate 		return;
46077c478bd9Sstevel@tonic-gate 	}
46087c478bd9Sstevel@tonic-gate 	/*
46097c478bd9Sstevel@tonic-gate 	 * first time initialization
46107c478bd9Sstevel@tonic-gate 	 */
46117c478bd9Sstevel@tonic-gate 	if ((FILEFile = fopen(filefile, "r")) == NULL)
46127c478bd9Sstevel@tonic-gate 		fatal(gettext("cannot open (%s)"), filefile);
46137c478bd9Sstevel@tonic-gate 	(void) fstat(fileno(FILEFile), &statbuf);
46147c478bd9Sstevel@tonic-gate 	if ((statbuf.st_mode & S_IFMT) != S_IFREG) {
46157c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
46167c478bd9Sstevel@tonic-gate 		    "tar: %s is not a regular file\n"), filefile);
46177c478bd9Sstevel@tonic-gate 		(void) fclose(FILEFile);
46187c478bd9Sstevel@tonic-gate 		done(1);
46197c478bd9Sstevel@tonic-gate 	}
46207c478bd9Sstevel@tonic-gate 	ptrtoFile = begofFile = endofFile;
46217c478bd9Sstevel@tonic-gate 	seekFile = 0;
46227c478bd9Sstevel@tonic-gate 	if (!xflag)
46237c478bd9Sstevel@tonic-gate 		return;		/* the file will be read only once anyway */
46247c478bd9Sstevel@tonic-gate 	nbytes = statbuf.st_size;
46257c478bd9Sstevel@tonic-gate 	while ((begofFile = calloc(nbytes, sizeof (char))) == NULL)
46267c478bd9Sstevel@tonic-gate 		nbytes -= 20;
46277c478bd9Sstevel@tonic-gate 	if (nbytes < 50) {
46287c478bd9Sstevel@tonic-gate 		free(begofFile);
46297c478bd9Sstevel@tonic-gate 		begofFile = endofFile;
46307c478bd9Sstevel@tonic-gate 		return;		/* no room so just do plain reads */
46317c478bd9Sstevel@tonic-gate 	}
46327c478bd9Sstevel@tonic-gate 	if (fread(begofFile, 1, nbytes, FILEFile) != nbytes)
46337c478bd9Sstevel@tonic-gate 		fatal(gettext("could not read %s"), filefile);
46347c478bd9Sstevel@tonic-gate 	ptrtoFile = begofFile;
46357c478bd9Sstevel@tonic-gate 	endofFile = begofFile + nbytes;
46367c478bd9Sstevel@tonic-gate 	for (p = begofFile; p < endofFile; ++p)
46377c478bd9Sstevel@tonic-gate 		if (*p == '\n')
46387c478bd9Sstevel@tonic-gate 			*p = '\0';
46397c478bd9Sstevel@tonic-gate 	if (nbytes != statbuf.st_size)
46407c478bd9Sstevel@tonic-gate 		seekFile = nbytes + 1;
46417c478bd9Sstevel@tonic-gate 	else
46427c478bd9Sstevel@tonic-gate 		(void) fclose(FILEFile);
46437c478bd9Sstevel@tonic-gate }
46447c478bd9Sstevel@tonic-gate 
46457c478bd9Sstevel@tonic-gate /*
46467c478bd9Sstevel@tonic-gate  *	nextarg -- get next argument of arglist.
46477c478bd9Sstevel@tonic-gate  *
46487c478bd9Sstevel@tonic-gate  *	The argument is taken from wherever is appropriate.
46497c478bd9Sstevel@tonic-gate  *
46507c478bd9Sstevel@tonic-gate  *	If the 'F file' function modifier has been specified, the argument
46517c478bd9Sstevel@tonic-gate  *	will be taken from the file, unless EOF has been reached.
46527c478bd9Sstevel@tonic-gate  *	Otherwise the argument will be taken from argv.
46537c478bd9Sstevel@tonic-gate  *
46547c478bd9Sstevel@tonic-gate  *	WARNING:
46557c478bd9Sstevel@tonic-gate  *	  Return value may point to static data, whose contents are over-
46567c478bd9Sstevel@tonic-gate  *	  written on each call.
46577c478bd9Sstevel@tonic-gate  */
46587c478bd9Sstevel@tonic-gate static	char  *
46597c478bd9Sstevel@tonic-gate nextarg(void)
46607c478bd9Sstevel@tonic-gate {
46617c478bd9Sstevel@tonic-gate 	static char nameFile[PATH_MAX + 1];
46627c478bd9Sstevel@tonic-gate 	int n;
46637c478bd9Sstevel@tonic-gate 	char *p;
46647c478bd9Sstevel@tonic-gate 
46657c478bd9Sstevel@tonic-gate 	if (FILEFile) {
46667c478bd9Sstevel@tonic-gate 		if (ptrtoFile < endofFile) {
46677c478bd9Sstevel@tonic-gate 			p = ptrtoFile;
46687c478bd9Sstevel@tonic-gate 			while (*ptrtoFile)
46697c478bd9Sstevel@tonic-gate 				++ptrtoFile;
46707c478bd9Sstevel@tonic-gate 			++ptrtoFile;
46717c478bd9Sstevel@tonic-gate 			return (p);
46727c478bd9Sstevel@tonic-gate 		}
46737c478bd9Sstevel@tonic-gate 		if (fgets(nameFile, PATH_MAX + 1, FILEFile) != NULL) {
46747c478bd9Sstevel@tonic-gate 			n = strlen(nameFile);
46757c478bd9Sstevel@tonic-gate 			if (n > 0 && nameFile[n-1] == '\n')
46767c478bd9Sstevel@tonic-gate 				nameFile[n-1] = '\0';
46777c478bd9Sstevel@tonic-gate 			return (nameFile);
46787c478bd9Sstevel@tonic-gate 		}
46797c478bd9Sstevel@tonic-gate 	}
46807c478bd9Sstevel@tonic-gate 	return (*Cmdargv++);
46817c478bd9Sstevel@tonic-gate }
46827c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
46837c478bd9Sstevel@tonic-gate 
46847c478bd9Sstevel@tonic-gate /*
46857c478bd9Sstevel@tonic-gate  * kcheck()
46867c478bd9Sstevel@tonic-gate  *	- checks the validity of size values for non-tape devices
46877c478bd9Sstevel@tonic-gate  *	- if size is zero, mulvol tar is disabled and size is
46887c478bd9Sstevel@tonic-gate  *	  assumed to be infinite.
46897c478bd9Sstevel@tonic-gate  *	- returns volume size in TBLOCKS
46907c478bd9Sstevel@tonic-gate  */
46917c478bd9Sstevel@tonic-gate 
46927c478bd9Sstevel@tonic-gate static blkcnt_t
46937c478bd9Sstevel@tonic-gate kcheck(char *kstr)
46947c478bd9Sstevel@tonic-gate {
46957c478bd9Sstevel@tonic-gate 	blkcnt_t kval;
46967c478bd9Sstevel@tonic-gate 
46977c478bd9Sstevel@tonic-gate 	kval = strtoll(kstr, NULL, 0);
46987c478bd9Sstevel@tonic-gate 	if (kval == (blkcnt_t)0) {	/* no multi-volume; size is infinity. */
46997c478bd9Sstevel@tonic-gate 		mulvol = 0;	/* definitely not mulvol, but we must  */
47007c478bd9Sstevel@tonic-gate 		return (0);	/* took out setting of NotTape */
47017c478bd9Sstevel@tonic-gate 	}
47027c478bd9Sstevel@tonic-gate 	if (kval < (blkcnt_t)MINSIZE) {
47037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
47047c478bd9Sstevel@tonic-gate 		    "tar: sizes below %luK not supported (%" FMT_blkcnt_t
47057c478bd9Sstevel@tonic-gate 		    ").\n"), (ulong_t)MINSIZE, kval);
47067c478bd9Sstevel@tonic-gate 		if (!kflag)
47077c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
47087c478bd9Sstevel@tonic-gate 			    "bad size entry for %s in %s.\n"),
47097c478bd9Sstevel@tonic-gate 			    archive, DEF_FILE);
47107c478bd9Sstevel@tonic-gate 		done(1);
47117c478bd9Sstevel@tonic-gate 	}
47127c478bd9Sstevel@tonic-gate 	mulvol++;
47137c478bd9Sstevel@tonic-gate 	NotTape++;			/* implies non-tape */
47147c478bd9Sstevel@tonic-gate 	return (kval * 1024 / TBLOCK);	/* convert to TBLOCKS */
47157c478bd9Sstevel@tonic-gate }
47167c478bd9Sstevel@tonic-gate 
47177c478bd9Sstevel@tonic-gate 
47187c478bd9Sstevel@tonic-gate /*
47197c478bd9Sstevel@tonic-gate  * bcheck()
47207c478bd9Sstevel@tonic-gate  *	- checks the validity of blocking factors
47217c478bd9Sstevel@tonic-gate  *	- returns blocking factor
47227c478bd9Sstevel@tonic-gate  */
47237c478bd9Sstevel@tonic-gate 
47247c478bd9Sstevel@tonic-gate static int
47257c478bd9Sstevel@tonic-gate bcheck(char *bstr)
47267c478bd9Sstevel@tonic-gate {
47277c478bd9Sstevel@tonic-gate 	blkcnt_t bval;
47287c478bd9Sstevel@tonic-gate 
47297c478bd9Sstevel@tonic-gate 	bval = strtoll(bstr, NULL, 0);
47307c478bd9Sstevel@tonic-gate 	if ((bval <= 0) || (bval > INT_MAX / TBLOCK)) {
47317c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
47327c478bd9Sstevel@tonic-gate 		    "tar: invalid blocksize \"%s\".\n"), bstr);
47337c478bd9Sstevel@tonic-gate 		if (!bflag)
47347c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
47357c478bd9Sstevel@tonic-gate 			    "bad blocksize entry for '%s' in %s.\n"),
47367c478bd9Sstevel@tonic-gate 			    archive, DEF_FILE);
47377c478bd9Sstevel@tonic-gate 		done(1);
47387c478bd9Sstevel@tonic-gate 	}
47397c478bd9Sstevel@tonic-gate 
47407c478bd9Sstevel@tonic-gate 	return ((int)bval);
47417c478bd9Sstevel@tonic-gate }
47427c478bd9Sstevel@tonic-gate 
47437c478bd9Sstevel@tonic-gate 
47447c478bd9Sstevel@tonic-gate /*
47457c478bd9Sstevel@tonic-gate  * defset()
47467c478bd9Sstevel@tonic-gate  *	- reads DEF_FILE for the set of default values specified.
47477c478bd9Sstevel@tonic-gate  *	- initializes 'usefile', 'nblock', and 'blocklim', and 'NotTape'.
47487c478bd9Sstevel@tonic-gate  *	- 'usefile' points to static data, so will be overwritten
47497c478bd9Sstevel@tonic-gate  *	  if this routine is called a second time.
47507c478bd9Sstevel@tonic-gate  *	- the pattern specified by 'arch' must be followed by four
47517c478bd9Sstevel@tonic-gate  *	  blank-separated fields (1) device (2) blocking,
47527c478bd9Sstevel@tonic-gate  *				 (3) size(K), and (4) tape
47537c478bd9Sstevel@tonic-gate  *	  for example: archive0=/dev/fd 1 400 n
47547c478bd9Sstevel@tonic-gate  */
47557c478bd9Sstevel@tonic-gate 
47567c478bd9Sstevel@tonic-gate static int
47577c478bd9Sstevel@tonic-gate defset(char *arch)
47587c478bd9Sstevel@tonic-gate {
47597c478bd9Sstevel@tonic-gate 	char *bp;
47607c478bd9Sstevel@tonic-gate 
47617c478bd9Sstevel@tonic-gate 	if (defopen(DEF_FILE) != 0)
47627c478bd9Sstevel@tonic-gate 		return (FALSE);
47637c478bd9Sstevel@tonic-gate 	if (defcntl(DC_SETFLAGS, (DC_STD & ~(DC_CASE))) == -1) {
47647c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
47657c478bd9Sstevel@tonic-gate 		    "tar: error setting parameters for %s.\n"), DEF_FILE);
47667c478bd9Sstevel@tonic-gate 		return (FALSE);			/* & following ones too */
47677c478bd9Sstevel@tonic-gate 	}
47687c478bd9Sstevel@tonic-gate 	if ((bp = defread(arch)) == NULL) {
47697c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
47707c478bd9Sstevel@tonic-gate 		    "tar: missing or invalid '%s' entry in %s.\n"),
47717c478bd9Sstevel@tonic-gate 		    arch, DEF_FILE);
47727c478bd9Sstevel@tonic-gate 		return (FALSE);
47737c478bd9Sstevel@tonic-gate 	}
47747c478bd9Sstevel@tonic-gate 	if ((usefile = strtok(bp, " \t")) == NULL) {
47757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
47767c478bd9Sstevel@tonic-gate 		    "tar: '%s' entry in %s is empty!\n"), arch, DEF_FILE);
47777c478bd9Sstevel@tonic-gate 		return (FALSE);
47787c478bd9Sstevel@tonic-gate 	}
47797c478bd9Sstevel@tonic-gate 	if ((bp = strtok(NULL, " \t")) == NULL) {
47807c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
47817c478bd9Sstevel@tonic-gate 		    "tar: block component missing in '%s' entry in %s.\n"),
47827c478bd9Sstevel@tonic-gate 		    arch, DEF_FILE);
47837c478bd9Sstevel@tonic-gate 		return (FALSE);
47847c478bd9Sstevel@tonic-gate 	}
47857c478bd9Sstevel@tonic-gate 	nblock = bcheck(bp);
47867c478bd9Sstevel@tonic-gate 	if ((bp = strtok(NULL, " \t")) == NULL) {
47877c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
47887c478bd9Sstevel@tonic-gate 		    "tar: size component missing in '%s' entry in %s.\n"),
47897c478bd9Sstevel@tonic-gate 		    arch, DEF_FILE);
47907c478bd9Sstevel@tonic-gate 		return (FALSE);
47917c478bd9Sstevel@tonic-gate 	}
47927c478bd9Sstevel@tonic-gate 	blocklim = kcheck(bp);
47937c478bd9Sstevel@tonic-gate 	if ((bp = strtok(NULL, " \t")) != NULL)
47947c478bd9Sstevel@tonic-gate 		NotTape = (*bp == 'n' || *bp == 'N');
47957c478bd9Sstevel@tonic-gate 	else
47967c478bd9Sstevel@tonic-gate 		NotTape = (blocklim != 0);
47977c478bd9Sstevel@tonic-gate 	(void) defopen(NULL);
47987c478bd9Sstevel@tonic-gate #ifdef DEBUG
47997c478bd9Sstevel@tonic-gate 	DEBUG("defset: archive='%s'; usefile='%s'\n", arch, usefile);
48007c478bd9Sstevel@tonic-gate 	DEBUG("defset: nblock='%d'; blocklim='%" FMT_blkcnt_t "'\n",
48017c478bd9Sstevel@tonic-gate 	    nblock, blocklim);
48027c478bd9Sstevel@tonic-gate 	DEBUG("defset: not tape = %d\n", NotTape, 0);
48037c478bd9Sstevel@tonic-gate #endif
48047c478bd9Sstevel@tonic-gate 	return (TRUE);
48057c478bd9Sstevel@tonic-gate }
48067c478bd9Sstevel@tonic-gate 
48077c478bd9Sstevel@tonic-gate 
48087c478bd9Sstevel@tonic-gate /*
48097c478bd9Sstevel@tonic-gate  * Following code handles excluded and included files.
48107c478bd9Sstevel@tonic-gate  * A hash table of file names to be {in,ex}cluded is built.
48117c478bd9Sstevel@tonic-gate  * For excluded files, before writing or extracting a file
48127c478bd9Sstevel@tonic-gate  * check to see if it is in the exclude_tbl.
48137c478bd9Sstevel@tonic-gate  * For included files, the wantit() procedure will check to
48147c478bd9Sstevel@tonic-gate  * see if the named file is in the include_tbl.
48157c478bd9Sstevel@tonic-gate  */
48167c478bd9Sstevel@tonic-gate 
48177c478bd9Sstevel@tonic-gate static void
48187c478bd9Sstevel@tonic-gate build_table(struct file_list *table[], char *file)
48197c478bd9Sstevel@tonic-gate {
48207c478bd9Sstevel@tonic-gate 	FILE	*fp;
48217c478bd9Sstevel@tonic-gate 	char	buf[PATH_MAX + 1];
48227c478bd9Sstevel@tonic-gate 
48237c478bd9Sstevel@tonic-gate 	if ((fp = fopen(file, "r")) == (FILE *)NULL)
48247c478bd9Sstevel@tonic-gate 		vperror(1, gettext("could not open %s"), file);
48257c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
48267c478bd9Sstevel@tonic-gate 		if (buf[strlen(buf) - 1] == '\n')
48277c478bd9Sstevel@tonic-gate 			buf[strlen(buf) - 1] = '\0';
48287c478bd9Sstevel@tonic-gate 		/* Only add to table if line has something in it */
48297c478bd9Sstevel@tonic-gate 		if (strspn(buf, " \t") != strlen(buf))
48307c478bd9Sstevel@tonic-gate 			add_file_to_table(table, buf);
48317c478bd9Sstevel@tonic-gate 	}
48327c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
48337c478bd9Sstevel@tonic-gate }
48347c478bd9Sstevel@tonic-gate 
48357c478bd9Sstevel@tonic-gate 
48367c478bd9Sstevel@tonic-gate /*
48377c478bd9Sstevel@tonic-gate  * Add a file name to the the specified table, if the file name has any
48387c478bd9Sstevel@tonic-gate  * trailing '/'s then delete them before inserting into the table
48397c478bd9Sstevel@tonic-gate  */
48407c478bd9Sstevel@tonic-gate 
48417c478bd9Sstevel@tonic-gate static void
48427c478bd9Sstevel@tonic-gate add_file_to_table(struct file_list *table[], char *str)
48437c478bd9Sstevel@tonic-gate {
48447c478bd9Sstevel@tonic-gate 	char	name[PATH_MAX + 1];
48457c478bd9Sstevel@tonic-gate 	unsigned int h;
48467c478bd9Sstevel@tonic-gate 	struct	file_list	*exp;
48477c478bd9Sstevel@tonic-gate 
48487c478bd9Sstevel@tonic-gate 	(void) strcpy(name, str);
48497c478bd9Sstevel@tonic-gate 	while (name[strlen(name) - 1] == '/') {
48507c478bd9Sstevel@tonic-gate 		name[strlen(name) - 1] = NULL;
48517c478bd9Sstevel@tonic-gate 	}
48527c478bd9Sstevel@tonic-gate 
48537c478bd9Sstevel@tonic-gate 	h = hash(name);
48547c478bd9Sstevel@tonic-gate 	if ((exp = (struct file_list *)calloc(sizeof (struct file_list),
48557c478bd9Sstevel@tonic-gate 	    sizeof (char))) == NULL) {
48567c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
48577c478bd9Sstevel@tonic-gate 		    "tar: out of memory, exclude/include table(entry)\n"));
48587c478bd9Sstevel@tonic-gate 		exit(1);
48597c478bd9Sstevel@tonic-gate 	}
48607c478bd9Sstevel@tonic-gate 
48617c478bd9Sstevel@tonic-gate 	if ((exp->name = strdup(name)) == NULL) {
48627c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
48637c478bd9Sstevel@tonic-gate 		    "tar: out of memory, exclude/include table(file name)\n"));
48647c478bd9Sstevel@tonic-gate 		exit(1);
48657c478bd9Sstevel@tonic-gate 	}
48667c478bd9Sstevel@tonic-gate 
48677c478bd9Sstevel@tonic-gate 	exp->next = table[h];
48687c478bd9Sstevel@tonic-gate 	table[h] = exp;
48697c478bd9Sstevel@tonic-gate }
48707c478bd9Sstevel@tonic-gate 
48717c478bd9Sstevel@tonic-gate 
48727c478bd9Sstevel@tonic-gate /*
48737c478bd9Sstevel@tonic-gate  * See if a file name or any of the file's parent directories is in the
48747c478bd9Sstevel@tonic-gate  * specified table, if the file name has any trailing '/'s then delete
48757c478bd9Sstevel@tonic-gate  * them before searching the table
48767c478bd9Sstevel@tonic-gate  */
48777c478bd9Sstevel@tonic-gate 
48787c478bd9Sstevel@tonic-gate static int
48797c478bd9Sstevel@tonic-gate is_in_table(struct file_list *table[], char *str)
48807c478bd9Sstevel@tonic-gate {
48817c478bd9Sstevel@tonic-gate 	char	name[PATH_MAX + 1];
48827c478bd9Sstevel@tonic-gate 	unsigned int	h;
48837c478bd9Sstevel@tonic-gate 	struct	file_list	*exp;
48847c478bd9Sstevel@tonic-gate 	char	*ptr;
48857c478bd9Sstevel@tonic-gate 
48867c478bd9Sstevel@tonic-gate 	(void) strcpy(name, str);
48877c478bd9Sstevel@tonic-gate 	while (name[strlen(name) - 1] == '/') {
48887c478bd9Sstevel@tonic-gate 		name[strlen(name) - 1] = NULL;
48897c478bd9Sstevel@tonic-gate 	}
48907c478bd9Sstevel@tonic-gate 
48917c478bd9Sstevel@tonic-gate 	/*
48927c478bd9Sstevel@tonic-gate 	 * check for the file name in the passed list
48937c478bd9Sstevel@tonic-gate 	 */
48947c478bd9Sstevel@tonic-gate 	h = hash(name);
48957c478bd9Sstevel@tonic-gate 	exp = table[h];
48967c478bd9Sstevel@tonic-gate 	while (exp != NULL) {
48977c478bd9Sstevel@tonic-gate 		if (strcmp(name, exp->name) == 0) {
48987c478bd9Sstevel@tonic-gate 			return (1);
48997c478bd9Sstevel@tonic-gate 		}
49007c478bd9Sstevel@tonic-gate 		exp = exp->next;
49017c478bd9Sstevel@tonic-gate 	}
49027c478bd9Sstevel@tonic-gate 
49037c478bd9Sstevel@tonic-gate 	/*
49047c478bd9Sstevel@tonic-gate 	 * check for any parent directories in the file list
49057c478bd9Sstevel@tonic-gate 	 */
49067c478bd9Sstevel@tonic-gate 	while ((ptr = strrchr(name, '/'))) {
49077c478bd9Sstevel@tonic-gate 		*ptr = NULL;
49087c478bd9Sstevel@tonic-gate 		h = hash(name);
49097c478bd9Sstevel@tonic-gate 		exp = table[h];
49107c478bd9Sstevel@tonic-gate 		while (exp != NULL) {
49117c478bd9Sstevel@tonic-gate 			if (strcmp(name, exp->name) == 0) {
49127c478bd9Sstevel@tonic-gate 				return (1);
49137c478bd9Sstevel@tonic-gate 			}
49147c478bd9Sstevel@tonic-gate 			exp = exp->next;
49157c478bd9Sstevel@tonic-gate 		}
49167c478bd9Sstevel@tonic-gate 	}
49177c478bd9Sstevel@tonic-gate 
49187c478bd9Sstevel@tonic-gate 	return (0);
49197c478bd9Sstevel@tonic-gate }
49207c478bd9Sstevel@tonic-gate 
49217c478bd9Sstevel@tonic-gate 
49227c478bd9Sstevel@tonic-gate /*
49237c478bd9Sstevel@tonic-gate  * Compute a hash from a string.
49247c478bd9Sstevel@tonic-gate  */
49257c478bd9Sstevel@tonic-gate 
49267c478bd9Sstevel@tonic-gate static unsigned int
49277c478bd9Sstevel@tonic-gate hash(char *str)
49287c478bd9Sstevel@tonic-gate {
49297c478bd9Sstevel@tonic-gate 	char	*cp;
49307c478bd9Sstevel@tonic-gate 	unsigned int	h;
49317c478bd9Sstevel@tonic-gate 
49327c478bd9Sstevel@tonic-gate 	h = 0;
49337c478bd9Sstevel@tonic-gate 	for (cp = str; *cp; cp++) {
49347c478bd9Sstevel@tonic-gate 		h += *cp;
49357c478bd9Sstevel@tonic-gate 	}
49367c478bd9Sstevel@tonic-gate 	return (h % TABLE_SIZE);
49377c478bd9Sstevel@tonic-gate }
49387c478bd9Sstevel@tonic-gate 
49397c478bd9Sstevel@tonic-gate static	void *
49407c478bd9Sstevel@tonic-gate getmem(size_t size)
49417c478bd9Sstevel@tonic-gate {
49427c478bd9Sstevel@tonic-gate 	void *p = calloc((unsigned)size, sizeof (char));
49437c478bd9Sstevel@tonic-gate 
49447c478bd9Sstevel@tonic-gate 	if (p == NULL && freemem) {
49457c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
49467c478bd9Sstevel@tonic-gate 		    "tar: out of memory, link and directory modtime "
49477c478bd9Sstevel@tonic-gate 		    "info lost\n"));
49487c478bd9Sstevel@tonic-gate 		freemem = 0;
49497c478bd9Sstevel@tonic-gate 		if (errflag)
49507c478bd9Sstevel@tonic-gate 			done(1);
49517c478bd9Sstevel@tonic-gate 		else
49527c478bd9Sstevel@tonic-gate 			Errflg = 1;
49537c478bd9Sstevel@tonic-gate 	}
49547c478bd9Sstevel@tonic-gate 	return (p);
49557c478bd9Sstevel@tonic-gate }
49567c478bd9Sstevel@tonic-gate 
49577c478bd9Sstevel@tonic-gate /*
49587c478bd9Sstevel@tonic-gate  * vperror() --variable argument perror.
49597c478bd9Sstevel@tonic-gate  * Takes 3 args: exit_status, formats, args.  If exit_status is 0, then
49607c478bd9Sstevel@tonic-gate  * the errflag (exit on error) is checked -- if it is non-zero, tar exits
49617c478bd9Sstevel@tonic-gate  * with the value of whatever "errno" is set to.  If exit_status is not
49627c478bd9Sstevel@tonic-gate  * zero, then tar exits with that error status. If errflag and exit_status
49637c478bd9Sstevel@tonic-gate  * are both zero, the routine returns to where it was called and sets Errflg
49647c478bd9Sstevel@tonic-gate  * to errno.
49657c478bd9Sstevel@tonic-gate  */
49667c478bd9Sstevel@tonic-gate 
49677c478bd9Sstevel@tonic-gate static void
49687c478bd9Sstevel@tonic-gate vperror(int exit_status, char *fmt, ...)
49697c478bd9Sstevel@tonic-gate {
49707c478bd9Sstevel@tonic-gate 	va_list	ap;
49717c478bd9Sstevel@tonic-gate 
49727c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
49737c478bd9Sstevel@tonic-gate 	(void) fputs("tar: ", stderr);
49747c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
49757c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, ": %s\n", strerror(errno));
49767c478bd9Sstevel@tonic-gate 	va_end(ap);
49777c478bd9Sstevel@tonic-gate 	if (exit_status)
49787c478bd9Sstevel@tonic-gate 		done(exit_status);
49797c478bd9Sstevel@tonic-gate 	else
49807c478bd9Sstevel@tonic-gate 		if (errflag)
49817c478bd9Sstevel@tonic-gate 			done(errno);
49827c478bd9Sstevel@tonic-gate 		else
49837c478bd9Sstevel@tonic-gate 			Errflg = errno;
49847c478bd9Sstevel@tonic-gate }
49857c478bd9Sstevel@tonic-gate 
49867c478bd9Sstevel@tonic-gate 
49877c478bd9Sstevel@tonic-gate static void
49887c478bd9Sstevel@tonic-gate fatal(char *format, ...)
49897c478bd9Sstevel@tonic-gate {
49907c478bd9Sstevel@tonic-gate 	va_list	ap;
49917c478bd9Sstevel@tonic-gate 
49927c478bd9Sstevel@tonic-gate 	va_start(ap, format);
49937c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "tar: ");
49947c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
49957c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
49967c478bd9Sstevel@tonic-gate 	va_end(ap);
49977c478bd9Sstevel@tonic-gate 	done(1);
49987c478bd9Sstevel@tonic-gate }
49997c478bd9Sstevel@tonic-gate 
50007c478bd9Sstevel@tonic-gate 
50017c478bd9Sstevel@tonic-gate /*
50027c478bd9Sstevel@tonic-gate  * Check to make sure that argument is a char * ptr.
50037c478bd9Sstevel@tonic-gate  * Actually, we just check to see that it is non-null.
50047c478bd9Sstevel@tonic-gate  * If it is null, print out the message and call usage(), bailing out.
50057c478bd9Sstevel@tonic-gate  */
50067c478bd9Sstevel@tonic-gate 
50077c478bd9Sstevel@tonic-gate static void
50087c478bd9Sstevel@tonic-gate assert_string(char *s, char *msg)
50097c478bd9Sstevel@tonic-gate {
50107c478bd9Sstevel@tonic-gate 	if (s == NULL) {
50117c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, msg);
50127c478bd9Sstevel@tonic-gate 		usage();
50137c478bd9Sstevel@tonic-gate 	}
50147c478bd9Sstevel@tonic-gate }
50157c478bd9Sstevel@tonic-gate 
50167c478bd9Sstevel@tonic-gate 
50177c478bd9Sstevel@tonic-gate static void
50187c478bd9Sstevel@tonic-gate mterr(char *operation, int i, int exitcode)
50197c478bd9Sstevel@tonic-gate {
50207c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
50217c478bd9Sstevel@tonic-gate 	    "tar: %s error: "), operation);
50227c478bd9Sstevel@tonic-gate 	if (i < 0)
50237c478bd9Sstevel@tonic-gate 		perror("");
50247c478bd9Sstevel@tonic-gate 	else
50257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("unexpected EOF\n"));
50267c478bd9Sstevel@tonic-gate 	done(exitcode);
50277c478bd9Sstevel@tonic-gate }
50287c478bd9Sstevel@tonic-gate 
50297c478bd9Sstevel@tonic-gate static int
50307c478bd9Sstevel@tonic-gate wantit(char *argv[], char **namep, char **dirp, char **component)
50317c478bd9Sstevel@tonic-gate {
50327c478bd9Sstevel@tonic-gate 	char **cp;
50337c478bd9Sstevel@tonic-gate 	int gotit;		/* true if we've found a match */
50347c478bd9Sstevel@tonic-gate 
50357c478bd9Sstevel@tonic-gate top:
50367c478bd9Sstevel@tonic-gate 	xhdr_flgs = 0;
50377c478bd9Sstevel@tonic-gate 	getdir();
50387c478bd9Sstevel@tonic-gate 	if (Xhdrflag > 0) {
50397c478bd9Sstevel@tonic-gate 		if (get_xdata() != 0) {	/* Xhdr items and regular header */
50407c478bd9Sstevel@tonic-gate 			passtape();
50417c478bd9Sstevel@tonic-gate 			return (0);	/* Error--don't want to extract  */
50427c478bd9Sstevel@tonic-gate 		}
50437c478bd9Sstevel@tonic-gate 	}
50447c478bd9Sstevel@tonic-gate 
50457c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
50467c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.typeflag == _XATTR_HDRTYPE && xattrbadhead == 0) {
50477c478bd9Sstevel@tonic-gate 		if (atflag || tflag) {
50487c478bd9Sstevel@tonic-gate 			(void) read_xattr_hdr();
50497c478bd9Sstevel@tonic-gate 		} else {
50507c478bd9Sstevel@tonic-gate 			passtape();
50517c478bd9Sstevel@tonic-gate 		}
50527c478bd9Sstevel@tonic-gate 		goto top;
50537c478bd9Sstevel@tonic-gate 	}
50547c478bd9Sstevel@tonic-gate #endif
50557c478bd9Sstevel@tonic-gate 
50567c478bd9Sstevel@tonic-gate 	/* sets *namep to point at the proper name */
50577c478bd9Sstevel@tonic-gate 	check_prefix(namep, dirp, component);
50587c478bd9Sstevel@tonic-gate 
50597c478bd9Sstevel@tonic-gate 	if (endtape()) {
50607c478bd9Sstevel@tonic-gate 		if (Bflag) {
50617c478bd9Sstevel@tonic-gate 			/*
50627c478bd9Sstevel@tonic-gate 			 * Logically at EOT - consume any extra blocks
50637c478bd9Sstevel@tonic-gate 			 * so that write to our stdin won't fail and
50647c478bd9Sstevel@tonic-gate 			 * emit an error message; otherwise something
50657c478bd9Sstevel@tonic-gate 			 * like "dd if=foo.tar | (cd bar; tar xvf -)"
50667c478bd9Sstevel@tonic-gate 			 * will produce a bogus error message from "dd".
50677c478bd9Sstevel@tonic-gate 			 */
50687c478bd9Sstevel@tonic-gate 
50697c478bd9Sstevel@tonic-gate 			while (read(mt, tbuf, TBLOCK*nblock) > 0) {
50707c478bd9Sstevel@tonic-gate 				/* empty body */
50717c478bd9Sstevel@tonic-gate 			}
50727c478bd9Sstevel@tonic-gate 		}
50737c478bd9Sstevel@tonic-gate 		return (-1);
50747c478bd9Sstevel@tonic-gate 	}
50757c478bd9Sstevel@tonic-gate 
50767c478bd9Sstevel@tonic-gate 	gotit = 0;
50777c478bd9Sstevel@tonic-gate 
50787c478bd9Sstevel@tonic-gate 	if ((Iflag && is_in_table(include_tbl, *namep)) ||
50797c478bd9Sstevel@tonic-gate 	    (! Iflag && *argv == NULL)) {
50807c478bd9Sstevel@tonic-gate 		gotit = 1;
50817c478bd9Sstevel@tonic-gate 	} else {
50827c478bd9Sstevel@tonic-gate 		for (cp = argv; *cp; cp++) {
50837c478bd9Sstevel@tonic-gate 			if (is_prefix(*cp, *namep)) {
50847c478bd9Sstevel@tonic-gate 				gotit = 1;
50857c478bd9Sstevel@tonic-gate 				break;
50867c478bd9Sstevel@tonic-gate 			}
50877c478bd9Sstevel@tonic-gate 		}
50887c478bd9Sstevel@tonic-gate 	}
50897c478bd9Sstevel@tonic-gate 
50907c478bd9Sstevel@tonic-gate 	if (! gotit) {
50917c478bd9Sstevel@tonic-gate 		passtape();
50927c478bd9Sstevel@tonic-gate 		return (0);
50937c478bd9Sstevel@tonic-gate 	}
50947c478bd9Sstevel@tonic-gate 
50957c478bd9Sstevel@tonic-gate 	if (Xflag && is_in_table(exclude_tbl, *namep)) {
50967c478bd9Sstevel@tonic-gate 		if (vflag) {
50977c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s excluded\n"),
50987c478bd9Sstevel@tonic-gate 			    *namep);
50997c478bd9Sstevel@tonic-gate 		}
51007c478bd9Sstevel@tonic-gate 		passtape();
51017c478bd9Sstevel@tonic-gate 		return (0);
51027c478bd9Sstevel@tonic-gate 	}
51037c478bd9Sstevel@tonic-gate 
51047c478bd9Sstevel@tonic-gate 	return (1);
51057c478bd9Sstevel@tonic-gate }
51067c478bd9Sstevel@tonic-gate 
51077c478bd9Sstevel@tonic-gate 
51087c478bd9Sstevel@tonic-gate /*
51097c478bd9Sstevel@tonic-gate  *  Return through *namep a pointer to the proper fullname (i.e  "<name> |
51107c478bd9Sstevel@tonic-gate  *  <prefix>/<name>"), as represented in the header entry dblock.dbuf.
51117c478bd9Sstevel@tonic-gate  */
51127c478bd9Sstevel@tonic-gate 
51137c478bd9Sstevel@tonic-gate static void
51147c478bd9Sstevel@tonic-gate check_prefix(char **namep, char **dirp, char **compp)
51157c478bd9Sstevel@tonic-gate {
51167c478bd9Sstevel@tonic-gate 	static char fullname[PATH_MAX + 1];
51177c478bd9Sstevel@tonic-gate 	static char dir[PATH_MAX + 1];
51187c478bd9Sstevel@tonic-gate 	static char component[PATH_MAX + 1];
51197c478bd9Sstevel@tonic-gate 	static char savename[PATH_MAX + 1];
51207c478bd9Sstevel@tonic-gate 	char *s;
51217c478bd9Sstevel@tonic-gate 
51227c478bd9Sstevel@tonic-gate 	(void) memset(dir, 0, sizeof (dir));
51237c478bd9Sstevel@tonic-gate 	(void) memset(component, 0, sizeof (component));
51247c478bd9Sstevel@tonic-gate 
51257c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_PATH) {
51267c478bd9Sstevel@tonic-gate 		(void) strcpy(fullname, Xtarhdr.x_path);
51277c478bd9Sstevel@tonic-gate 	} else {
51287c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.prefix[0] != '\0')
51297c478bd9Sstevel@tonic-gate 			(void) sprintf(fullname, "%.*s/%.*s", PRESIZ,
51307c478bd9Sstevel@tonic-gate 			    dblock.dbuf.prefix, NAMSIZ, dblock.dbuf.name);
51317c478bd9Sstevel@tonic-gate 		else
51327c478bd9Sstevel@tonic-gate 			(void) sprintf(fullname, "%.*s", NAMSIZ,
51337c478bd9Sstevel@tonic-gate 			    dblock.dbuf.name);
51347c478bd9Sstevel@tonic-gate 	}
51357c478bd9Sstevel@tonic-gate 
51367c478bd9Sstevel@tonic-gate 	/*
51377c478bd9Sstevel@tonic-gate 	 * Set dir and component names
51387c478bd9Sstevel@tonic-gate 	 */
51397c478bd9Sstevel@tonic-gate 
51407c478bd9Sstevel@tonic-gate 	get_parent(fullname, dir);
51417c478bd9Sstevel@tonic-gate 
51427c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
51437c478bd9Sstevel@tonic-gate 	if (xattrp == (struct xattr_buf *)NULL) {
51447c478bd9Sstevel@tonic-gate #endif
51457c478bd9Sstevel@tonic-gate 		/*
51467c478bd9Sstevel@tonic-gate 		 * Save of real name since were going to chop off the
51477c478bd9Sstevel@tonic-gate 		 * trailing slashes.
51487c478bd9Sstevel@tonic-gate 		 */
51497c478bd9Sstevel@tonic-gate 		(void) strcpy(savename, fullname);
51507c478bd9Sstevel@tonic-gate 		/*
51517c478bd9Sstevel@tonic-gate 		 * first strip of trailing slashes.
51527c478bd9Sstevel@tonic-gate 		 */
51537c478bd9Sstevel@tonic-gate 		chop_endslashes(savename);
51547c478bd9Sstevel@tonic-gate 		s = get_component(savename);
51557c478bd9Sstevel@tonic-gate 		(void) strcpy(component, s);
51567c478bd9Sstevel@tonic-gate 
51577c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
51587c478bd9Sstevel@tonic-gate 	} else {
51597c478bd9Sstevel@tonic-gate 		(void) strcpy(fullname, xattrp->h_names);
51607c478bd9Sstevel@tonic-gate 		(void) strcpy(dir, fullname);
51617c478bd9Sstevel@tonic-gate 		(void) strcpy(component, xattrp->h_names +
51627c478bd9Sstevel@tonic-gate 		    strlen(xattrp->h_names) + 1);
51637c478bd9Sstevel@tonic-gate 	}
51647c478bd9Sstevel@tonic-gate #endif
51657c478bd9Sstevel@tonic-gate 	*namep = fullname;
51667c478bd9Sstevel@tonic-gate 	*dirp = dir;
51677c478bd9Sstevel@tonic-gate 	*compp = component;
51687c478bd9Sstevel@tonic-gate }
51697c478bd9Sstevel@tonic-gate 
51707c478bd9Sstevel@tonic-gate /*
51717c478bd9Sstevel@tonic-gate  * Return true if the object indicated by the file descriptor and type
51727c478bd9Sstevel@tonic-gate  * is a tape device, false otherwise
51737c478bd9Sstevel@tonic-gate  */
51747c478bd9Sstevel@tonic-gate 
51757c478bd9Sstevel@tonic-gate static int
51767c478bd9Sstevel@tonic-gate istape(int fd, int type)
51777c478bd9Sstevel@tonic-gate {
51787c478bd9Sstevel@tonic-gate 	int result = 0;
51797c478bd9Sstevel@tonic-gate 
51804bc0a2efScasper 	if (S_ISCHR(type)) {
51817c478bd9Sstevel@tonic-gate 		struct mtget mtg;
51827c478bd9Sstevel@tonic-gate 
51837c478bd9Sstevel@tonic-gate 		if (ioctl(fd, MTIOCGET, &mtg) != -1) {
51847c478bd9Sstevel@tonic-gate 			result = 1;
51857c478bd9Sstevel@tonic-gate 		}
51867c478bd9Sstevel@tonic-gate 	}
51877c478bd9Sstevel@tonic-gate 
51887c478bd9Sstevel@tonic-gate 	return (result);
51897c478bd9Sstevel@tonic-gate }
51907c478bd9Sstevel@tonic-gate 
51917c478bd9Sstevel@tonic-gate #include <utmpx.h>
51927c478bd9Sstevel@tonic-gate 
51937c478bd9Sstevel@tonic-gate struct utmpx utmpx;
51947c478bd9Sstevel@tonic-gate 
51957c478bd9Sstevel@tonic-gate #define	NMAX	(sizeof (utmpx.ut_name))
51967c478bd9Sstevel@tonic-gate 
51977c478bd9Sstevel@tonic-gate typedef struct cachenode {	/* this struct must be zeroed before using */
51987c478bd9Sstevel@tonic-gate 	struct cachenode *next;	/* next in hash chain */
51997c478bd9Sstevel@tonic-gate 	int val;		/* the uid or gid of this entry */
52007c478bd9Sstevel@tonic-gate 	int namehash;		/* name's hash signature */
52017c478bd9Sstevel@tonic-gate 	char name[NMAX+1];	/* the string that val maps to */
52027c478bd9Sstevel@tonic-gate } cachenode_t;
52037c478bd9Sstevel@tonic-gate 
52047c478bd9Sstevel@tonic-gate #define	HASHSIZE	256
52057c478bd9Sstevel@tonic-gate 
52067c478bd9Sstevel@tonic-gate static cachenode_t *names[HASHSIZE];
52077c478bd9Sstevel@tonic-gate static cachenode_t *groups[HASHSIZE];
52087c478bd9Sstevel@tonic-gate static cachenode_t *uids[HASHSIZE];
52097c478bd9Sstevel@tonic-gate static cachenode_t *gids[HASHSIZE];
52107c478bd9Sstevel@tonic-gate 
52117c478bd9Sstevel@tonic-gate static int
52127c478bd9Sstevel@tonic-gate hash_byname(char *name)
52137c478bd9Sstevel@tonic-gate {
52147c478bd9Sstevel@tonic-gate 	int i, c, h = 0;
52157c478bd9Sstevel@tonic-gate 
52167c478bd9Sstevel@tonic-gate 	for (i = 0; i < NMAX; i++) {
52177c478bd9Sstevel@tonic-gate 		c = name[i];
52187c478bd9Sstevel@tonic-gate 		if (c == '\0')
52197c478bd9Sstevel@tonic-gate 			break;
52207c478bd9Sstevel@tonic-gate 		h = (h << 4) + h + c;
52217c478bd9Sstevel@tonic-gate 	}
52227c478bd9Sstevel@tonic-gate 	return (h);
52237c478bd9Sstevel@tonic-gate }
52247c478bd9Sstevel@tonic-gate 
52257c478bd9Sstevel@tonic-gate static cachenode_t *
52267c478bd9Sstevel@tonic-gate hash_lookup_byval(cachenode_t *table[], int val)
52277c478bd9Sstevel@tonic-gate {
52287c478bd9Sstevel@tonic-gate 	int h = val;
52297c478bd9Sstevel@tonic-gate 	cachenode_t *c;
52307c478bd9Sstevel@tonic-gate 
52317c478bd9Sstevel@tonic-gate 	for (c = table[h & (HASHSIZE - 1)]; c != NULL; c = c->next) {
52327c478bd9Sstevel@tonic-gate 		if (c->val == val)
52337c478bd9Sstevel@tonic-gate 			return (c);
52347c478bd9Sstevel@tonic-gate 	}
52357c478bd9Sstevel@tonic-gate 	return (NULL);
52367c478bd9Sstevel@tonic-gate }
52377c478bd9Sstevel@tonic-gate 
52387c478bd9Sstevel@tonic-gate static cachenode_t *
52397c478bd9Sstevel@tonic-gate hash_lookup_byname(cachenode_t *table[], char *name)
52407c478bd9Sstevel@tonic-gate {
52417c478bd9Sstevel@tonic-gate 	int h = hash_byname(name);
52427c478bd9Sstevel@tonic-gate 	cachenode_t *c;
52437c478bd9Sstevel@tonic-gate 
52447c478bd9Sstevel@tonic-gate 	for (c = table[h & (HASHSIZE - 1)]; c != NULL; c = c->next) {
52457c478bd9Sstevel@tonic-gate 		if (c->namehash == h && strcmp(c->name, name) == 0)
52467c478bd9Sstevel@tonic-gate 			return (c);
52477c478bd9Sstevel@tonic-gate 	}
52487c478bd9Sstevel@tonic-gate 	return (NULL);
52497c478bd9Sstevel@tonic-gate }
52507c478bd9Sstevel@tonic-gate 
52517c478bd9Sstevel@tonic-gate static cachenode_t *
52527c478bd9Sstevel@tonic-gate hash_insert(cachenode_t *table[], char *name, int value)
52537c478bd9Sstevel@tonic-gate {
52547c478bd9Sstevel@tonic-gate 	cachenode_t *c;
52557c478bd9Sstevel@tonic-gate 	int signature;
52567c478bd9Sstevel@tonic-gate 
52577c478bd9Sstevel@tonic-gate 	c = calloc(1, sizeof (cachenode_t));
52587c478bd9Sstevel@tonic-gate 	if (c == NULL) {
52597c478bd9Sstevel@tonic-gate 		perror("malloc");
52607c478bd9Sstevel@tonic-gate 		exit(1);
52617c478bd9Sstevel@tonic-gate 	}
52627c478bd9Sstevel@tonic-gate 	if (name != NULL) {
52637c478bd9Sstevel@tonic-gate 		(void) strncpy(c->name, name, NMAX);
52647c478bd9Sstevel@tonic-gate 		c->namehash = hash_byname(name);
52657c478bd9Sstevel@tonic-gate 	}
52667c478bd9Sstevel@tonic-gate 	c->val = value;
52677c478bd9Sstevel@tonic-gate 	if (table == uids || table == gids)
52687c478bd9Sstevel@tonic-gate 		signature = c->val;
52697c478bd9Sstevel@tonic-gate 	else
52707c478bd9Sstevel@tonic-gate 		signature = c->namehash;
52717c478bd9Sstevel@tonic-gate 	c->next = table[signature & (HASHSIZE - 1)];
52727c478bd9Sstevel@tonic-gate 	table[signature & (HASHSIZE - 1)] = c;
52737c478bd9Sstevel@tonic-gate 	return (c);
52747c478bd9Sstevel@tonic-gate }
52757c478bd9Sstevel@tonic-gate 
52767c478bd9Sstevel@tonic-gate static char *
52777c478bd9Sstevel@tonic-gate getname(uid_t uid)
52787c478bd9Sstevel@tonic-gate {
52797c478bd9Sstevel@tonic-gate 	cachenode_t *c;
52807c478bd9Sstevel@tonic-gate 
52817c478bd9Sstevel@tonic-gate 	if ((c = hash_lookup_byval(uids, uid)) == NULL) {
52827c478bd9Sstevel@tonic-gate 		struct passwd *pwent = getpwuid(uid);
52837c478bd9Sstevel@tonic-gate 		c = hash_insert(uids, pwent ? pwent->pw_name : NULL, uid);
52847c478bd9Sstevel@tonic-gate 	}
52857c478bd9Sstevel@tonic-gate 	return (c->name);
52867c478bd9Sstevel@tonic-gate }
52877c478bd9Sstevel@tonic-gate 
52887c478bd9Sstevel@tonic-gate static char *
52897c478bd9Sstevel@tonic-gate getgroup(gid_t gid)
52907c478bd9Sstevel@tonic-gate {
52917c478bd9Sstevel@tonic-gate 	cachenode_t *c;
52927c478bd9Sstevel@tonic-gate 
52937c478bd9Sstevel@tonic-gate 	if ((c = hash_lookup_byval(gids, gid)) == NULL) {
52947c478bd9Sstevel@tonic-gate 		struct group *grent = getgrgid(gid);
52957c478bd9Sstevel@tonic-gate 		c = hash_insert(gids, grent ? grent->gr_name : NULL, gid);
52967c478bd9Sstevel@tonic-gate 	}
52977c478bd9Sstevel@tonic-gate 	return (c->name);
52987c478bd9Sstevel@tonic-gate }
52997c478bd9Sstevel@tonic-gate 
53007c478bd9Sstevel@tonic-gate static uid_t
53017c478bd9Sstevel@tonic-gate getuidbyname(char *name)
53027c478bd9Sstevel@tonic-gate {
53037c478bd9Sstevel@tonic-gate 	cachenode_t *c;
53047c478bd9Sstevel@tonic-gate 
53057c478bd9Sstevel@tonic-gate 	if ((c = hash_lookup_byname(names, name)) == NULL) {
53067c478bd9Sstevel@tonic-gate 		struct passwd *pwent = getpwnam(name);
53077c478bd9Sstevel@tonic-gate 		c = hash_insert(names, name, pwent ? (int)pwent->pw_uid : -1);
53087c478bd9Sstevel@tonic-gate 	}
53097c478bd9Sstevel@tonic-gate 	return ((uid_t)c->val);
53107c478bd9Sstevel@tonic-gate }
53117c478bd9Sstevel@tonic-gate 
53127c478bd9Sstevel@tonic-gate static gid_t
53137c478bd9Sstevel@tonic-gate getgidbyname(char *group)
53147c478bd9Sstevel@tonic-gate {
53157c478bd9Sstevel@tonic-gate 	cachenode_t *c;
53167c478bd9Sstevel@tonic-gate 
53177c478bd9Sstevel@tonic-gate 	if ((c = hash_lookup_byname(groups, group)) == NULL) {
53187c478bd9Sstevel@tonic-gate 		struct group *grent = getgrnam(group);
53197c478bd9Sstevel@tonic-gate 		c = hash_insert(groups, group, grent ? (int)grent->gr_gid : -1);
53207c478bd9Sstevel@tonic-gate 	}
53217c478bd9Sstevel@tonic-gate 	return ((gid_t)c->val);
53227c478bd9Sstevel@tonic-gate }
53237c478bd9Sstevel@tonic-gate 
53247c478bd9Sstevel@tonic-gate /*
53257c478bd9Sstevel@tonic-gate  * Build the header.
53267c478bd9Sstevel@tonic-gate  * Determine whether or not an extended header is also needed.  If needed,
53277c478bd9Sstevel@tonic-gate  * create and write the extended header and its data.
53287c478bd9Sstevel@tonic-gate  * Writing of the extended header assumes that "tomodes" has been called and
53297c478bd9Sstevel@tonic-gate  * the relevant information has been placed in the header block.
53307c478bd9Sstevel@tonic-gate  */
53317c478bd9Sstevel@tonic-gate 
53327c478bd9Sstevel@tonic-gate static int
53337c478bd9Sstevel@tonic-gate build_dblock(
53347c478bd9Sstevel@tonic-gate 	const char		*name,
53357c478bd9Sstevel@tonic-gate 	const char		*linkname,
53367c478bd9Sstevel@tonic-gate 	const char		typeflag,
53377c478bd9Sstevel@tonic-gate 	const int		filetype,
53387c478bd9Sstevel@tonic-gate 	const struct stat	*sp,
53397c478bd9Sstevel@tonic-gate 	const dev_t		device,
53407c478bd9Sstevel@tonic-gate 	const char		*prefix)
53417c478bd9Sstevel@tonic-gate {
53427c478bd9Sstevel@tonic-gate 	int nblks;
53437c478bd9Sstevel@tonic-gate 	major_t		dev;
53447c478bd9Sstevel@tonic-gate 	const char	*filename;
53457c478bd9Sstevel@tonic-gate 	const char	*lastslash;
53467c478bd9Sstevel@tonic-gate 
53477c478bd9Sstevel@tonic-gate 	if (filetype == XATTR_FILE)
53487c478bd9Sstevel@tonic-gate 		dblock.dbuf.typeflag = _XATTR_HDRTYPE;
53497c478bd9Sstevel@tonic-gate 	else
53507c478bd9Sstevel@tonic-gate 		dblock.dbuf.typeflag = typeflag;
53517c478bd9Sstevel@tonic-gate 	(void) memset(dblock.dbuf.name, '\0', NAMSIZ);
53527c478bd9Sstevel@tonic-gate 	(void) memset(dblock.dbuf.linkname, '\0', NAMSIZ);
53537c478bd9Sstevel@tonic-gate 	(void) memset(dblock.dbuf.prefix, '\0', PRESIZ);
53547c478bd9Sstevel@tonic-gate 
53557c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_PATH)
53567c478bd9Sstevel@tonic-gate 		filename = Xtarhdr.x_path;
53577c478bd9Sstevel@tonic-gate 	else
53587c478bd9Sstevel@tonic-gate 		filename = name;
53597c478bd9Sstevel@tonic-gate 
53607c478bd9Sstevel@tonic-gate 	if ((dev = major(device)) > OCTAL7CHAR) {
53617c478bd9Sstevel@tonic-gate 		if (Eflag) {
53627c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_DEVMAJOR;
53637c478bd9Sstevel@tonic-gate 			Xtarhdr.x_devmajor = dev;
53647c478bd9Sstevel@tonic-gate 		} else {
53657c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
53667c478bd9Sstevel@tonic-gate 			    "Device major too large for %s.  Use -E flag."),
53677c478bd9Sstevel@tonic-gate 			    filename);
53687c478bd9Sstevel@tonic-gate 			if (errflag)
53697c478bd9Sstevel@tonic-gate 				done(1);
53707c478bd9Sstevel@tonic-gate 			else
53717c478bd9Sstevel@tonic-gate 				Errflg = 1;
53727c478bd9Sstevel@tonic-gate 		}
53737c478bd9Sstevel@tonic-gate 		dev = 0;
53747c478bd9Sstevel@tonic-gate 	}
53757c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.devmajor, "%07lo", dev);
53767c478bd9Sstevel@tonic-gate 	if ((dev = minor(device)) > OCTAL7CHAR) {
53777c478bd9Sstevel@tonic-gate 		if (Eflag) {
53787c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_DEVMINOR;
53797c478bd9Sstevel@tonic-gate 			Xtarhdr.x_devminor = dev;
53807c478bd9Sstevel@tonic-gate 		} else {
53817c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
53827c478bd9Sstevel@tonic-gate 			    "Device minor too large for %s.  Use -E flag."),
53837c478bd9Sstevel@tonic-gate 			    filename);
53847c478bd9Sstevel@tonic-gate 			if (errflag)
53857c478bd9Sstevel@tonic-gate 				done(1);
53867c478bd9Sstevel@tonic-gate 			else
53877c478bd9Sstevel@tonic-gate 				Errflg = 1;
53887c478bd9Sstevel@tonic-gate 		}
53897c478bd9Sstevel@tonic-gate 		dev = 0;
53907c478bd9Sstevel@tonic-gate 	}
53917c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.devminor, "%07lo", dev);
53927c478bd9Sstevel@tonic-gate 
53937c478bd9Sstevel@tonic-gate 	(void) strncpy(dblock.dbuf.name, name, NAMSIZ);
53947c478bd9Sstevel@tonic-gate 	(void) strncpy(dblock.dbuf.linkname, linkname, NAMSIZ);
53957c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.magic, "%.5s", magic_type);
53967c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.version, "00");
53977c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.uname, "%.31s", getname(sp->st_uid));
53987c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.gname, "%.31s", getgroup(sp->st_gid));
53997c478bd9Sstevel@tonic-gate 	(void) strncpy(dblock.dbuf.prefix, prefix, PRESIZ);
54007c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
54017c478bd9Sstevel@tonic-gate 
54027c478bd9Sstevel@tonic-gate 	if (Eflag) {
54037c478bd9Sstevel@tonic-gate 		(void) bcopy(dblock.dummy, xhdr_buf.dummy, TBLOCK);
54047c478bd9Sstevel@tonic-gate 		(void) memset(xhdr_buf.dbuf.name, '\0', NAMSIZ);
54057c478bd9Sstevel@tonic-gate 		lastslash = strrchr(name, '/');
54067c478bd9Sstevel@tonic-gate 		if (lastslash == NULL)
54077c478bd9Sstevel@tonic-gate 			lastslash = name;
54087c478bd9Sstevel@tonic-gate 		else
54097c478bd9Sstevel@tonic-gate 			lastslash++;
54107c478bd9Sstevel@tonic-gate 		(void) strcpy(xhdr_buf.dbuf.name, lastslash);
54117c478bd9Sstevel@tonic-gate 		(void) memset(xhdr_buf.dbuf.linkname, '\0', NAMSIZ);
54127c478bd9Sstevel@tonic-gate 		(void) memset(xhdr_buf.dbuf.prefix, '\0', PRESIZ);
54137c478bd9Sstevel@tonic-gate 		(void) strcpy(xhdr_buf.dbuf.prefix, xhdr_dirname);
54147c478bd9Sstevel@tonic-gate 		xhdr_count++;
54157c478bd9Sstevel@tonic-gate 		xrec_offset = 0;
54167c478bd9Sstevel@tonic-gate 		gen_date("mtime", sp->st_mtim);
54177c478bd9Sstevel@tonic-gate 		xhdr_buf.dbuf.typeflag = 'X';
54187c478bd9Sstevel@tonic-gate 		if (gen_utf8_names(filename) != 0)
54197c478bd9Sstevel@tonic-gate 			return (1);
54207c478bd9Sstevel@tonic-gate 
54217c478bd9Sstevel@tonic-gate #ifdef XHDR_DEBUG
54227c478bd9Sstevel@tonic-gate 		Xtarhdr.x_uname = dblock.dbuf.uname;
54237c478bd9Sstevel@tonic-gate 		Xtarhdr.x_gname = dblock.dbuf.gname;
54247c478bd9Sstevel@tonic-gate 		xhdr_flgs |= (_X_UNAME | _X_GNAME);
54257c478bd9Sstevel@tonic-gate #endif
54267c478bd9Sstevel@tonic-gate 		if (xhdr_flgs) {
54277c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_DEVMAJOR)
54287c478bd9Sstevel@tonic-gate 				gen_num("SUN.devmajor", Xtarhdr.x_devmajor);
54297c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_DEVMINOR)
54307c478bd9Sstevel@tonic-gate 				gen_num("SUN.devminor", Xtarhdr.x_devminor);
54317c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_GID)
54327c478bd9Sstevel@tonic-gate 				gen_num("gid", Xtarhdr.x_gid);
54337c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_UID)
54347c478bd9Sstevel@tonic-gate 				gen_num("uid", Xtarhdr.x_uid);
54357c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_SIZE)
54367c478bd9Sstevel@tonic-gate 				gen_num("size", Xtarhdr.x_filesz);
54377c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_PATH)
54387c478bd9Sstevel@tonic-gate 				gen_string("path", Xtarhdr.x_path);
54397c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_LINKPATH)
54407c478bd9Sstevel@tonic-gate 				gen_string("linkpath", Xtarhdr.x_linkpath);
54417c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_GNAME)
54427c478bd9Sstevel@tonic-gate 				gen_string("gname", Xtarhdr.x_gname);
54437c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_UNAME)
54447c478bd9Sstevel@tonic-gate 				gen_string("uname", Xtarhdr.x_uname);
54457c478bd9Sstevel@tonic-gate 		}
54467c478bd9Sstevel@tonic-gate 		(void) sprintf(xhdr_buf.dbuf.size,
54477c478bd9Sstevel@tonic-gate 		    "%011" FMT_off_t_o, xrec_offset);
54487c478bd9Sstevel@tonic-gate 		(void) sprintf(xhdr_buf.dbuf.chksum, "%07o",
54497c478bd9Sstevel@tonic-gate 		    checksum(&xhdr_buf));
54507c478bd9Sstevel@tonic-gate 		(void) writetbuf((char *)&xhdr_buf, 1);
54517c478bd9Sstevel@tonic-gate 		nblks = TBLOCKS(xrec_offset);
54527c478bd9Sstevel@tonic-gate 		(void) writetbuf(xrec_ptr, nblks);
54537c478bd9Sstevel@tonic-gate 	}
54547c478bd9Sstevel@tonic-gate 	return (0);
54557c478bd9Sstevel@tonic-gate }
54567c478bd9Sstevel@tonic-gate 
54577c478bd9Sstevel@tonic-gate 
54587c478bd9Sstevel@tonic-gate /*
54597c478bd9Sstevel@tonic-gate  *  makeDir - ensure that a directory with the pathname denoted by name
54607c478bd9Sstevel@tonic-gate  *            exists, and return 1 on success, and 0 on failure (e.g.,
54617c478bd9Sstevel@tonic-gate  *	      read-only file system, exists but not-a-directory).
54627c478bd9Sstevel@tonic-gate  */
54637c478bd9Sstevel@tonic-gate 
54647c478bd9Sstevel@tonic-gate static int
54657c478bd9Sstevel@tonic-gate makeDir(char *name)
54667c478bd9Sstevel@tonic-gate {
54677c478bd9Sstevel@tonic-gate 	struct stat buf;
54687c478bd9Sstevel@tonic-gate 
54697c478bd9Sstevel@tonic-gate 	if (access(name, 0) < 0) {  /* name doesn't exist */
54707c478bd9Sstevel@tonic-gate 		if (mkdir(name, 0777) < 0) {
54717c478bd9Sstevel@tonic-gate 			vperror(0, "%s", name);
54727c478bd9Sstevel@tonic-gate 			return (0);
54737c478bd9Sstevel@tonic-gate 		}
54747c478bd9Sstevel@tonic-gate 	} else {		   /* name exists */
54757c478bd9Sstevel@tonic-gate 		if (stat(name, &buf) < 0) {
54767c478bd9Sstevel@tonic-gate 			vperror(0, "%s", name);
54777c478bd9Sstevel@tonic-gate 			return (0);
54787c478bd9Sstevel@tonic-gate 		}
54797c478bd9Sstevel@tonic-gate 
54807c478bd9Sstevel@tonic-gate 		return ((buf.st_mode & S_IFMT) == S_IFDIR);
54817c478bd9Sstevel@tonic-gate 	}
54827c478bd9Sstevel@tonic-gate 
54837c478bd9Sstevel@tonic-gate 	return (1);
54847c478bd9Sstevel@tonic-gate }
54857c478bd9Sstevel@tonic-gate 
54867c478bd9Sstevel@tonic-gate 
54877c478bd9Sstevel@tonic-gate /*
54887c478bd9Sstevel@tonic-gate  * Save this directory and its mtime on the stack, popping and setting
54897c478bd9Sstevel@tonic-gate  * the mtimes of any stacked dirs which aren't parents of this one.
54907c478bd9Sstevel@tonic-gate  * A null name causes the entire stack to be unwound and set.
54917c478bd9Sstevel@tonic-gate  *
54927c478bd9Sstevel@tonic-gate  * Since all the elements of the directory "stack" share a common
54937c478bd9Sstevel@tonic-gate  * prefix, we can make do with one string.  We keep only the current
54947c478bd9Sstevel@tonic-gate  * directory path, with an associated array of mtime's. A negative
54957c478bd9Sstevel@tonic-gate  * mtime means no mtime.
54967c478bd9Sstevel@tonic-gate  *
54977c478bd9Sstevel@tonic-gate  * This stack algorithm is not guaranteed to work for tapes created
54987c478bd9Sstevel@tonic-gate  * with the 'r' function letter, but the vast majority of tapes with
54997c478bd9Sstevel@tonic-gate  * directories are not.  This avoids saving every directory record on
55007c478bd9Sstevel@tonic-gate  * the tape and setting all the times at the end.
55017c478bd9Sstevel@tonic-gate  *
55027c478bd9Sstevel@tonic-gate  * (This was borrowed from the 4.1.3 source, and adapted to the 5.x
55037c478bd9Sstevel@tonic-gate  *  environment)
55047c478bd9Sstevel@tonic-gate  */
55057c478bd9Sstevel@tonic-gate 
55067c478bd9Sstevel@tonic-gate static void
55077c478bd9Sstevel@tonic-gate doDirTimes(char *name, timestruc_t modTime)
55087c478bd9Sstevel@tonic-gate {
55097c478bd9Sstevel@tonic-gate 	static char dirstack[PATH_MAX+2];
55107c478bd9Sstevel@tonic-gate 			/* Add spaces for the last slash and last NULL */
55117c478bd9Sstevel@tonic-gate 	static timestruc_t	modtimes[PATH_MAX+1]; /* hash table */
55127c478bd9Sstevel@tonic-gate 	char *p = dirstack;
55137c478bd9Sstevel@tonic-gate 	char *q = name;
55147c478bd9Sstevel@tonic-gate 	char *savp;
55157c478bd9Sstevel@tonic-gate 
55167c478bd9Sstevel@tonic-gate 	if (q) {
55177c478bd9Sstevel@tonic-gate 		/*
55187c478bd9Sstevel@tonic-gate 		 * Find common prefix
55197c478bd9Sstevel@tonic-gate 		 */
55207c478bd9Sstevel@tonic-gate 
55217c478bd9Sstevel@tonic-gate 		while (*p == *q && *p) {
55227c478bd9Sstevel@tonic-gate 			p++; q++;
55237c478bd9Sstevel@tonic-gate 		}
55247c478bd9Sstevel@tonic-gate 	}
55257c478bd9Sstevel@tonic-gate 
55267c478bd9Sstevel@tonic-gate 	savp = p;
55277c478bd9Sstevel@tonic-gate 	while (*p) {
55287c478bd9Sstevel@tonic-gate 		/*
55297c478bd9Sstevel@tonic-gate 		 * Not a child: unwind the stack, setting the times.
55307c478bd9Sstevel@tonic-gate 		 * The order we do this doesn't matter, so we go "forward."
55317c478bd9Sstevel@tonic-gate 		 */
55327c478bd9Sstevel@tonic-gate 
55337c478bd9Sstevel@tonic-gate 		if (*p == '/')
55347c478bd9Sstevel@tonic-gate 			if (modtimes[p - dirstack].tv_sec >= 0) {
55357c478bd9Sstevel@tonic-gate 				*p = '\0';	 /* zap the slash */
55367c478bd9Sstevel@tonic-gate 				setPathTimes(AT_FDCWD, dirstack,
55377c478bd9Sstevel@tonic-gate 				    modtimes[p - dirstack]);
55387c478bd9Sstevel@tonic-gate 				*p = '/';
55397c478bd9Sstevel@tonic-gate 			}
55407c478bd9Sstevel@tonic-gate 		++p;
55417c478bd9Sstevel@tonic-gate 	}
55427c478bd9Sstevel@tonic-gate 
55437c478bd9Sstevel@tonic-gate 	p = savp;
55447c478bd9Sstevel@tonic-gate 
55457c478bd9Sstevel@tonic-gate 	/*
55467c478bd9Sstevel@tonic-gate 	 *  Push this one on the "stack"
55477c478bd9Sstevel@tonic-gate 	 */
55487c478bd9Sstevel@tonic-gate 
55497c478bd9Sstevel@tonic-gate 	if (q) {
55507c478bd9Sstevel@tonic-gate 
55517c478bd9Sstevel@tonic-gate 		/*
55527c478bd9Sstevel@tonic-gate 		 * Since the name parameter points the dir pathname
55537c478bd9Sstevel@tonic-gate 		 * which is limited only to contain PATH_MAX chars
55547c478bd9Sstevel@tonic-gate 		 * at maximum, we can ignore the overflow case of p.
55557c478bd9Sstevel@tonic-gate 		 */
55567c478bd9Sstevel@tonic-gate 
55577c478bd9Sstevel@tonic-gate 		while ((*p = *q++)) {	/* append the rest of the new dir */
55587c478bd9Sstevel@tonic-gate 			modtimes[p - dirstack].tv_sec = -1;
55597c478bd9Sstevel@tonic-gate 			p++;
55607c478bd9Sstevel@tonic-gate 		}
55617c478bd9Sstevel@tonic-gate 
55627c478bd9Sstevel@tonic-gate 		/*
55637c478bd9Sstevel@tonic-gate 		 * If the tar file had used 'P' or 'E' function modifier,
55647c478bd9Sstevel@tonic-gate 		 * append the last slash.
55657c478bd9Sstevel@tonic-gate 		 */
55667c478bd9Sstevel@tonic-gate 		if (*(p - 1) != '/') {
55677c478bd9Sstevel@tonic-gate 			*p++ = '/';
55687c478bd9Sstevel@tonic-gate 			*p = '\0';
55697c478bd9Sstevel@tonic-gate 		}
55707c478bd9Sstevel@tonic-gate 		/* overwrite the last one */
55717c478bd9Sstevel@tonic-gate 		modtimes[p - dirstack - 1] = modTime;
55727c478bd9Sstevel@tonic-gate 	}
55737c478bd9Sstevel@tonic-gate }
55747c478bd9Sstevel@tonic-gate 
55757c478bd9Sstevel@tonic-gate 
55767c478bd9Sstevel@tonic-gate /*
55777c478bd9Sstevel@tonic-gate  *  setPathTimes - set the modification time for given path.  Return 1 if
55787c478bd9Sstevel@tonic-gate  *                 successful and 0 if not successful.
55797c478bd9Sstevel@tonic-gate  */
55807c478bd9Sstevel@tonic-gate 
55817c478bd9Sstevel@tonic-gate static void
55827c478bd9Sstevel@tonic-gate setPathTimes(int dirfd, char *path, timestruc_t modTime)
55837c478bd9Sstevel@tonic-gate 
55847c478bd9Sstevel@tonic-gate {
55857c478bd9Sstevel@tonic-gate 	struct timeval timebuf[2];
55867c478bd9Sstevel@tonic-gate 
55877c478bd9Sstevel@tonic-gate 	/*
55887c478bd9Sstevel@tonic-gate 	 * futimesat takes an array of two timeval structs.
55897c478bd9Sstevel@tonic-gate 	 * The first entry contains access time.
55907c478bd9Sstevel@tonic-gate 	 * The second entry contains modification time.
55917c478bd9Sstevel@tonic-gate 	 * Unlike a timestruc_t, which uses nanoseconds, timeval uses
55927c478bd9Sstevel@tonic-gate 	 * microseconds.
55937c478bd9Sstevel@tonic-gate 	 */
55947c478bd9Sstevel@tonic-gate 	timebuf[0].tv_sec = time((time_t *)0);
55957c478bd9Sstevel@tonic-gate 	timebuf[0].tv_usec = 0;
55967c478bd9Sstevel@tonic-gate 	timebuf[1].tv_sec = modTime.tv_sec;
55977c478bd9Sstevel@tonic-gate 
55987c478bd9Sstevel@tonic-gate 	/* Extended header: use microseconds */
55997c478bd9Sstevel@tonic-gate 	timebuf[1].tv_usec = (xhdr_flgs & _X_MTIME) ? modTime.tv_nsec/1000 : 0;
56007c478bd9Sstevel@tonic-gate 
56017c478bd9Sstevel@tonic-gate 	if (futimesat(dirfd, path, timebuf) < 0)
56027c478bd9Sstevel@tonic-gate 		vperror(0, "can't set time on %s", path);
56037c478bd9Sstevel@tonic-gate }
56047c478bd9Sstevel@tonic-gate 
56057c478bd9Sstevel@tonic-gate 
56067c478bd9Sstevel@tonic-gate /*
56077c478bd9Sstevel@tonic-gate  * If hflag is set then delete the symbolic link's target.
56087c478bd9Sstevel@tonic-gate  * If !hflag then delete the target.
56097c478bd9Sstevel@tonic-gate  */
56107c478bd9Sstevel@tonic-gate 
56117c478bd9Sstevel@tonic-gate static void
56122c0f0499Slovely delete_target(int fd, char *comp, char *namep)
56137c478bd9Sstevel@tonic-gate {
56147c478bd9Sstevel@tonic-gate 	struct	stat	xtractbuf;
56157c478bd9Sstevel@tonic-gate 	char buf[PATH_MAX + 1];
56167c478bd9Sstevel@tonic-gate 	int n;
56177c478bd9Sstevel@tonic-gate 
56187c478bd9Sstevel@tonic-gate 
56192c0f0499Slovely 	if (unlinkat(fd, comp, AT_REMOVEDIR) < 0) {
56207c478bd9Sstevel@tonic-gate 		if (errno == ENOTDIR && !hflag) {
56212c0f0499Slovely 			(void) unlinkat(fd, comp, 0);
56227c478bd9Sstevel@tonic-gate 		} else if (errno == ENOTDIR && hflag) {
56237c478bd9Sstevel@tonic-gate 			if (!lstat(namep, &xtractbuf)) {
56247c478bd9Sstevel@tonic-gate 				if ((xtractbuf.st_mode & S_IFMT) != S_IFLNK) {
56252c0f0499Slovely 					(void) unlinkat(fd, comp, 0);
56267c478bd9Sstevel@tonic-gate 				} else if ((n = readlink(namep, buf,
56277c478bd9Sstevel@tonic-gate 				    PATH_MAX)) != -1) {
56287c478bd9Sstevel@tonic-gate 					buf[n] = (char)NULL;
56297c478bd9Sstevel@tonic-gate 					(void) unlinkat(fd, buf,
56307c478bd9Sstevel@tonic-gate 					    AT_REMOVEDIR);
56317c478bd9Sstevel@tonic-gate 					if (errno == ENOTDIR)
56327c478bd9Sstevel@tonic-gate 						(void) unlinkat(fd, buf, 0);
56337c478bd9Sstevel@tonic-gate 				} else {
56342c0f0499Slovely 					(void) unlinkat(fd, comp, 0);
56357c478bd9Sstevel@tonic-gate 				}
56367c478bd9Sstevel@tonic-gate 			} else {
56372c0f0499Slovely 				(void) unlinkat(fd, comp, 0);
56387c478bd9Sstevel@tonic-gate 			}
56397c478bd9Sstevel@tonic-gate 		}
56407c478bd9Sstevel@tonic-gate 	}
56417c478bd9Sstevel@tonic-gate }
56427c478bd9Sstevel@tonic-gate 
56437c478bd9Sstevel@tonic-gate 
56447c478bd9Sstevel@tonic-gate /*
56457c478bd9Sstevel@tonic-gate  * ACL changes:
56467c478bd9Sstevel@tonic-gate  *	putfile():
56477c478bd9Sstevel@tonic-gate  *		Get acl info after stat. Write out ancillary file
56487c478bd9Sstevel@tonic-gate  *		before the normal file, i.e. directory, regular, FIFO,
56497c478bd9Sstevel@tonic-gate  *		link, special. If acl count is less than 4, no need to
56507c478bd9Sstevel@tonic-gate  *		create ancillary file. (i.e. standard permission is in
56517c478bd9Sstevel@tonic-gate  *		use.
56527c478bd9Sstevel@tonic-gate  *	doxtract():
56537c478bd9Sstevel@tonic-gate  *		Process ancillary file. Read it in and set acl info.
56547c478bd9Sstevel@tonic-gate  *		watch out for 'o' function modifier.
56557c478bd9Sstevel@tonic-gate  *	't' function letter to display table
56567c478bd9Sstevel@tonic-gate  */
56577c478bd9Sstevel@tonic-gate 
56587c478bd9Sstevel@tonic-gate /*
56597c478bd9Sstevel@tonic-gate  * New functions for ACLs and other security attributes
56607c478bd9Sstevel@tonic-gate  */
56617c478bd9Sstevel@tonic-gate 
56627c478bd9Sstevel@tonic-gate /*
56637c478bd9Sstevel@tonic-gate  * The function appends the new security attribute info to the end of
56647c478bd9Sstevel@tonic-gate  * existing secinfo.
56657c478bd9Sstevel@tonic-gate  */
56667c478bd9Sstevel@tonic-gate int
56677c478bd9Sstevel@tonic-gate append_secattr(
56687c478bd9Sstevel@tonic-gate 	char	 **secinfo,	/* existing security info */
56697c478bd9Sstevel@tonic-gate 	int	 *secinfo_len,	/* length of existing security info */
567045916cd2Sjpk 	int	 size,		/* new attribute size: unit depends on type */
567145916cd2Sjpk 	char	*attrtext,	/* new attribute text */
567245916cd2Sjpk 	char	 attr_type)	/* new attribute type */
56737c478bd9Sstevel@tonic-gate {
56747c478bd9Sstevel@tonic-gate 	char	*new_secinfo;
56757c478bd9Sstevel@tonic-gate 	int	newattrsize;
56767c478bd9Sstevel@tonic-gate 	int	oldsize;
567745916cd2Sjpk 	struct sec_attr	*attr;
56787c478bd9Sstevel@tonic-gate 
56797c478bd9Sstevel@tonic-gate 	/* no need to add */
568045916cd2Sjpk 	if (attr_type != DIR_TYPE) {
568145916cd2Sjpk 		if (attrtext == NULL)
56827c478bd9Sstevel@tonic-gate 			return (0);
568345916cd2Sjpk 	}
56847c478bd9Sstevel@tonic-gate 
568545916cd2Sjpk 	switch (attr_type) {
568645916cd2Sjpk 	case UFSD_ACL:
568745916cd2Sjpk 	case ACE_ACL:
56887c478bd9Sstevel@tonic-gate 		if (attrtext == NULL) {
56897c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "acltotext failed\n");
56907c478bd9Sstevel@tonic-gate 			return (-1);
56917c478bd9Sstevel@tonic-gate 		}
56927c478bd9Sstevel@tonic-gate 		/* header: type + size = 8 */
56937c478bd9Sstevel@tonic-gate 		newattrsize = 8 + (int)strlen(attrtext) + 1;
56947c478bd9Sstevel@tonic-gate 		attr = (struct sec_attr *)malloc(newattrsize);
56957c478bd9Sstevel@tonic-gate 		if (attr == NULL) {
56967c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "can't allocate memory\n");
56977c478bd9Sstevel@tonic-gate 			return (-1);
56987c478bd9Sstevel@tonic-gate 		}
569945916cd2Sjpk 		attr->attr_type = attr_type;
57007c478bd9Sstevel@tonic-gate 		(void) sprintf(attr->attr_len,
570145916cd2Sjpk 		    "%06o", size); /* acl entry count */
57027c478bd9Sstevel@tonic-gate 		(void) strcpy((char *)&attr->attr_info[0], attrtext);
57037c478bd9Sstevel@tonic-gate 		free(attrtext);
57047c478bd9Sstevel@tonic-gate 		break;
57057c478bd9Sstevel@tonic-gate 
570645916cd2Sjpk 	/* Trusted Extensions */
570745916cd2Sjpk 	case DIR_TYPE:
570845916cd2Sjpk 	case LBL_TYPE:
570945916cd2Sjpk 		newattrsize = sizeof (struct sec_attr) + strlen(attrtext);
571045916cd2Sjpk 		attr = (struct sec_attr *)malloc(newattrsize);
571145916cd2Sjpk 		if (attr == NULL) {
571245916cd2Sjpk 			(void) fprintf(stderr,
571345916cd2Sjpk 			gettext("can't allocate memory\n"));
571445916cd2Sjpk 			return (-1);
571545916cd2Sjpk 		}
571645916cd2Sjpk 		attr->attr_type = attr_type;
571745916cd2Sjpk 		(void) sprintf(attr->attr_len,
571845916cd2Sjpk 		    "%06d", size); /* len of attr data */
571945916cd2Sjpk 		(void) strcpy((char *)&attr->attr_info[0], attrtext);
572045916cd2Sjpk 		break;
57217c478bd9Sstevel@tonic-gate 
57227c478bd9Sstevel@tonic-gate 	default:
57237c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "unrecognized attribute type\n");
57247c478bd9Sstevel@tonic-gate 		return (-1);
57257c478bd9Sstevel@tonic-gate 	}
57267c478bd9Sstevel@tonic-gate 
57277c478bd9Sstevel@tonic-gate 	/* old security info + new attr header(8) + new attr */
57287c478bd9Sstevel@tonic-gate 	oldsize = *secinfo_len;
57297c478bd9Sstevel@tonic-gate 	*secinfo_len += newattrsize;
57307c478bd9Sstevel@tonic-gate 	new_secinfo = (char *)malloc(*secinfo_len);
57317c478bd9Sstevel@tonic-gate 	if (new_secinfo == NULL) {
57327c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "can't allocate memory\n");
57337c478bd9Sstevel@tonic-gate 		*secinfo_len -= newattrsize;
573445916cd2Sjpk 		free(attr);
57357c478bd9Sstevel@tonic-gate 		return (-1);
57367c478bd9Sstevel@tonic-gate 	}
57377c478bd9Sstevel@tonic-gate 
57387c478bd9Sstevel@tonic-gate 	(void) memcpy(new_secinfo, *secinfo, oldsize);
57397c478bd9Sstevel@tonic-gate 	(void) memcpy(new_secinfo + oldsize, attr, newattrsize);
57407c478bd9Sstevel@tonic-gate 
57417c478bd9Sstevel@tonic-gate 	free(*secinfo);
574245916cd2Sjpk 	free(attr);
57437c478bd9Sstevel@tonic-gate 	*secinfo = new_secinfo;
57447c478bd9Sstevel@tonic-gate 	return (0);
57457c478bd9Sstevel@tonic-gate }
57467c478bd9Sstevel@tonic-gate 
57477c478bd9Sstevel@tonic-gate /*
57487c478bd9Sstevel@tonic-gate  * write_ancillary(): write out an ancillary file.
57497c478bd9Sstevel@tonic-gate  *      The file has the same header as normal file except the type and size
57507c478bd9Sstevel@tonic-gate  *      fields. The type is 'A' and size is the sum of all attributes
57517c478bd9Sstevel@tonic-gate  *	in bytes.
57527c478bd9Sstevel@tonic-gate  *	The body contains a list of attribute type, size and info. Currently,
57537c478bd9Sstevel@tonic-gate  *	there is only ACL info.  This file is put before the normal file.
57547c478bd9Sstevel@tonic-gate  */
57557c478bd9Sstevel@tonic-gate void
57567c478bd9Sstevel@tonic-gate write_ancillary(union hblock *dblockp, char *secinfo, int len, char hdrtype)
57577c478bd9Sstevel@tonic-gate {
57587c478bd9Sstevel@tonic-gate 	long    blocks;
57597c478bd9Sstevel@tonic-gate 	int	savflag;
57607c478bd9Sstevel@tonic-gate 	int	savsize;
57617c478bd9Sstevel@tonic-gate 
57627c478bd9Sstevel@tonic-gate 	/* Just tranditional permissions or no security attribute info */
57637c478bd9Sstevel@tonic-gate 	if (len == 0 || secinfo == NULL)
57647c478bd9Sstevel@tonic-gate 		return;
57657c478bd9Sstevel@tonic-gate 
57667c478bd9Sstevel@tonic-gate 	/* save flag and size */
57677c478bd9Sstevel@tonic-gate 	savflag = (dblockp->dbuf).typeflag;
57687c478bd9Sstevel@tonic-gate 	(void) sscanf(dblockp->dbuf.size, "%12o", (uint_t *)&savsize);
57697c478bd9Sstevel@tonic-gate 
57707c478bd9Sstevel@tonic-gate 	/* special flag for ancillary file */
57717c478bd9Sstevel@tonic-gate 	if (hdrtype == _XATTR_HDRTYPE)
57727c478bd9Sstevel@tonic-gate 		dblockp->dbuf.typeflag = _XATTR_HDRTYPE;
57737c478bd9Sstevel@tonic-gate 	else
57747c478bd9Sstevel@tonic-gate 		dblockp->dbuf.typeflag = 'A';
57757c478bd9Sstevel@tonic-gate 
57767c478bd9Sstevel@tonic-gate 	/* for pre-2.5 versions of tar, need to make sure */
57777c478bd9Sstevel@tonic-gate 	/* the ACL file is readable			  */
57787c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.mode, "%07lo",
57797c478bd9Sstevel@tonic-gate 	    (stbuf.st_mode & POSIXMODES) | 0000200);
57807c478bd9Sstevel@tonic-gate 	(void) sprintf(dblockp->dbuf.size, "%011o", len);
57817c478bd9Sstevel@tonic-gate 	(void) sprintf(dblockp->dbuf.chksum, "%07o", checksum(dblockp));
57827c478bd9Sstevel@tonic-gate 
57837c478bd9Sstevel@tonic-gate 	/* write out the header */
57847c478bd9Sstevel@tonic-gate 	(void) writetbuf((char *)dblockp, 1);
57857c478bd9Sstevel@tonic-gate 
57867c478bd9Sstevel@tonic-gate 	/* write out security info */
57877c478bd9Sstevel@tonic-gate 	blocks = TBLOCKS(len);
57887c478bd9Sstevel@tonic-gate 	(void) writetbuf((char *)secinfo, (int)blocks);
57897c478bd9Sstevel@tonic-gate 
57907c478bd9Sstevel@tonic-gate 	/* restore mode, flag and size */
57917c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.mode, "%07lo", stbuf.st_mode & POSIXMODES);
57927c478bd9Sstevel@tonic-gate 	dblockp->dbuf.typeflag = savflag;
57937c478bd9Sstevel@tonic-gate 	(void) sprintf(dblockp->dbuf.size, "%011o", savsize);
57947c478bd9Sstevel@tonic-gate }
57957c478bd9Sstevel@tonic-gate 
57967c478bd9Sstevel@tonic-gate /*
57977c478bd9Sstevel@tonic-gate  * Read the data record for extended headers and then the regular header.
57987c478bd9Sstevel@tonic-gate  * The data are read into the buffer and then null-terminated.  Entries
57997c478bd9Sstevel@tonic-gate  * are of the format:
58007c478bd9Sstevel@tonic-gate  * 	"%d %s=%s\n"
58017c478bd9Sstevel@tonic-gate  *
58027c478bd9Sstevel@tonic-gate  * When an extended header record is found, the extended header must
58037c478bd9Sstevel@tonic-gate  * be processed and its values used to override the values in the
58047c478bd9Sstevel@tonic-gate  * normal header.  The way this is done is to process the extended
58057c478bd9Sstevel@tonic-gate  * header data record and set the data values, then call getdir
58067c478bd9Sstevel@tonic-gate  * to process the regular header, then then to reconcile the two
58077c478bd9Sstevel@tonic-gate  * sets of data.
58087c478bd9Sstevel@tonic-gate  */
58097c478bd9Sstevel@tonic-gate 
58107c478bd9Sstevel@tonic-gate static int
58117c478bd9Sstevel@tonic-gate get_xdata(void)
58127c478bd9Sstevel@tonic-gate {
58137c478bd9Sstevel@tonic-gate 	struct keylist_pair {
58147c478bd9Sstevel@tonic-gate 		int keynum;
58157c478bd9Sstevel@tonic-gate 		char *keylist;
58167c478bd9Sstevel@tonic-gate 	}	keylist_pair[] = {	_X_DEVMAJOR, "SUN.devmajor",
58177c478bd9Sstevel@tonic-gate 					_X_DEVMINOR, "SUN.devminor",
58187c478bd9Sstevel@tonic-gate 					_X_GID, "gid",
58197c478bd9Sstevel@tonic-gate 					_X_GNAME, "gname",
58207c478bd9Sstevel@tonic-gate 					_X_LINKPATH, "linkpath",
58217c478bd9Sstevel@tonic-gate 					_X_PATH, "path",
58227c478bd9Sstevel@tonic-gate 					_X_SIZE, "size",
58237c478bd9Sstevel@tonic-gate 					_X_UID, "uid",
58247c478bd9Sstevel@tonic-gate 					_X_UNAME, "uname",
58257c478bd9Sstevel@tonic-gate 					_X_MTIME, "mtime",
58267c478bd9Sstevel@tonic-gate 					_X_LAST, "NULL" };
58277c478bd9Sstevel@tonic-gate 	char		*lineloc;
58287c478bd9Sstevel@tonic-gate 	int		length, i;
58297c478bd9Sstevel@tonic-gate 	char		*keyword, *value;
58307c478bd9Sstevel@tonic-gate 	blkcnt_t	nblocks;
58317c478bd9Sstevel@tonic-gate 	int		bufneeded;
58327c478bd9Sstevel@tonic-gate 	struct stat	*sp = &stbuf;
58337c478bd9Sstevel@tonic-gate 	int		errors;
58347c478bd9Sstevel@tonic-gate 
58357c478bd9Sstevel@tonic-gate 	Xtarhdr.x_uid = 0;
58367c478bd9Sstevel@tonic-gate 	Xtarhdr.x_gid = 0;
58377c478bd9Sstevel@tonic-gate 	Xtarhdr.x_devmajor = 0;
58387c478bd9Sstevel@tonic-gate 	Xtarhdr.x_devminor = 0;
58397c478bd9Sstevel@tonic-gate 	Xtarhdr.x_filesz = 0;
58407c478bd9Sstevel@tonic-gate 	Xtarhdr.x_uname = NULL;
58417c478bd9Sstevel@tonic-gate 	Xtarhdr.x_gname = NULL;
58427c478bd9Sstevel@tonic-gate 	Xtarhdr.x_linkpath = NULL;
58437c478bd9Sstevel@tonic-gate 	Xtarhdr.x_path = NULL;
58447c478bd9Sstevel@tonic-gate 	Xtarhdr.x_mtime.tv_sec = 0;
58457c478bd9Sstevel@tonic-gate 	Xtarhdr.x_mtime.tv_nsec = 0;
58467c478bd9Sstevel@tonic-gate 	xhdr_count++;
58477c478bd9Sstevel@tonic-gate 	errors = 0;
58487c478bd9Sstevel@tonic-gate 
58497c478bd9Sstevel@tonic-gate 	nblocks = TBLOCKS(stbuf.st_size);
58507c478bd9Sstevel@tonic-gate 	bufneeded = nblocks * TBLOCK;
58517c478bd9Sstevel@tonic-gate 	if (bufneeded >= xrec_size) {
58527c478bd9Sstevel@tonic-gate 		free(xrec_ptr);
58537c478bd9Sstevel@tonic-gate 		xrec_size = bufneeded + 1;
58547c478bd9Sstevel@tonic-gate 		if ((xrec_ptr = malloc(xrec_size)) == NULL)
58557c478bd9Sstevel@tonic-gate 			fatal(gettext("cannot allocate buffer"));
58567c478bd9Sstevel@tonic-gate 	}
58577c478bd9Sstevel@tonic-gate 
58587c478bd9Sstevel@tonic-gate 	lineloc = xrec_ptr;
58597c478bd9Sstevel@tonic-gate 
58607c478bd9Sstevel@tonic-gate 	while (nblocks-- > 0) {
58617c478bd9Sstevel@tonic-gate 		readtape(lineloc);
58627c478bd9Sstevel@tonic-gate 		lineloc += TBLOCK;
58637c478bd9Sstevel@tonic-gate 	}
58647c478bd9Sstevel@tonic-gate 	lineloc = xrec_ptr;
58657c478bd9Sstevel@tonic-gate 	xrec_ptr[stbuf.st_size] = '\0';
58667c478bd9Sstevel@tonic-gate 	while (lineloc < xrec_ptr + stbuf.st_size) {
58677c478bd9Sstevel@tonic-gate 		length = atoi(lineloc);
58687c478bd9Sstevel@tonic-gate 		*(lineloc + length - 1) = '\0';
58697c478bd9Sstevel@tonic-gate 		keyword = strchr(lineloc, ' ') + 1;
58707c478bd9Sstevel@tonic-gate 		value = strchr(keyword, '=') + 1;
58717c478bd9Sstevel@tonic-gate 		*(value - 1) = '\0';
58727c478bd9Sstevel@tonic-gate 		i = 0;
58737c478bd9Sstevel@tonic-gate 		lineloc += length;
58747c478bd9Sstevel@tonic-gate 		while (keylist_pair[i].keynum != (int)_X_LAST) {
58757c478bd9Sstevel@tonic-gate 			if (strcmp(keyword, keylist_pair[i].keylist) == 0)
58767c478bd9Sstevel@tonic-gate 				break;
58777c478bd9Sstevel@tonic-gate 			i++;
58787c478bd9Sstevel@tonic-gate 		}
58797c478bd9Sstevel@tonic-gate 		errno = 0;
58807c478bd9Sstevel@tonic-gate 		switch (keylist_pair[i].keynum) {
58817c478bd9Sstevel@tonic-gate 		case _X_DEVMAJOR:
58827c478bd9Sstevel@tonic-gate 			Xtarhdr.x_devmajor = (major_t)strtoul(value, NULL, 0);
58837c478bd9Sstevel@tonic-gate 			if (errno) {
58847c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
58857c478bd9Sstevel@tonic-gate 				    "tar: Extended header major value error "
58867c478bd9Sstevel@tonic-gate 				    "for file # %llu.\n"), xhdr_count);
58877c478bd9Sstevel@tonic-gate 				errors++;
58887c478bd9Sstevel@tonic-gate 			} else
58897c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_DEVMAJOR;
58907c478bd9Sstevel@tonic-gate 			break;
58917c478bd9Sstevel@tonic-gate 		case _X_DEVMINOR:
58927c478bd9Sstevel@tonic-gate 			Xtarhdr.x_devminor = (minor_t)strtoul(value, NULL, 0);
58937c478bd9Sstevel@tonic-gate 			if (errno) {
58947c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
58957c478bd9Sstevel@tonic-gate 				    "tar: Extended header minor value error "
58967c478bd9Sstevel@tonic-gate 				    "for file # %llu.\n"), xhdr_count);
58977c478bd9Sstevel@tonic-gate 				errors++;
58987c478bd9Sstevel@tonic-gate 			} else
58997c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_DEVMINOR;
59007c478bd9Sstevel@tonic-gate 			break;
59017c478bd9Sstevel@tonic-gate 		case _X_GID:
59027c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_GID;
59037c478bd9Sstevel@tonic-gate 			Xtarhdr.x_gid = strtol(value, NULL, 0);
59047c478bd9Sstevel@tonic-gate 			if ((errno) || (Xtarhdr.x_gid > UID_MAX)) {
59057c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
59067c478bd9Sstevel@tonic-gate 				    "tar: Extended header gid value error "
59077c478bd9Sstevel@tonic-gate 				    "for file # %llu.\n"), xhdr_count);
59087c478bd9Sstevel@tonic-gate 				Xtarhdr.x_gid = GID_NOBODY;
59097c478bd9Sstevel@tonic-gate 			}
59107c478bd9Sstevel@tonic-gate 			break;
59117c478bd9Sstevel@tonic-gate 		case _X_GNAME:
59127c478bd9Sstevel@tonic-gate 			if (utf8_local("gname", &Xtarhdr.x_gname,
59137c478bd9Sstevel@tonic-gate 			    local_gname, value, _POSIX_NAME_MAX) == 0)
59147c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_GNAME;
59157c478bd9Sstevel@tonic-gate 			break;
59167c478bd9Sstevel@tonic-gate 		case _X_LINKPATH:
59177c478bd9Sstevel@tonic-gate 			if (utf8_local("linkpath", &Xtarhdr.x_linkpath,
59187c478bd9Sstevel@tonic-gate 			    local_linkpath, value, PATH_MAX) == 0)
59197c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_LINKPATH;
59207c478bd9Sstevel@tonic-gate 			else
59217c478bd9Sstevel@tonic-gate 				errors++;
59227c478bd9Sstevel@tonic-gate 			break;
59237c478bd9Sstevel@tonic-gate 		case _X_PATH:
59247c478bd9Sstevel@tonic-gate 			if (utf8_local("path", &Xtarhdr.x_path,
59257c478bd9Sstevel@tonic-gate 			    local_path, value, PATH_MAX) == 0)
59267c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_PATH;
59277c478bd9Sstevel@tonic-gate 			else
59287c478bd9Sstevel@tonic-gate 				errors++;
59297c478bd9Sstevel@tonic-gate 			break;
59307c478bd9Sstevel@tonic-gate 		case _X_SIZE:
59317c478bd9Sstevel@tonic-gate 			Xtarhdr.x_filesz = strtoull(value, NULL, 0);
59327c478bd9Sstevel@tonic-gate 			if (errno) {
59337c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
59347c478bd9Sstevel@tonic-gate 				    "tar: Extended header invalid filesize "
59357c478bd9Sstevel@tonic-gate 				    "for file # %llu.\n"), xhdr_count);
59367c478bd9Sstevel@tonic-gate 				errors++;
59377c478bd9Sstevel@tonic-gate 			} else
59387c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_SIZE;
59397c478bd9Sstevel@tonic-gate 			break;
59407c478bd9Sstevel@tonic-gate 		case _X_UID:
59417c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_UID;
59427c478bd9Sstevel@tonic-gate 			Xtarhdr.x_uid = strtol(value, NULL, 0);
59437c478bd9Sstevel@tonic-gate 			if ((errno) || (Xtarhdr.x_uid > UID_MAX)) {
59447c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
59457c478bd9Sstevel@tonic-gate 				    "tar: Extended header uid value error "
59467c478bd9Sstevel@tonic-gate 				    "for file # %llu.\n"), xhdr_count);
59477c478bd9Sstevel@tonic-gate 				Xtarhdr.x_uid = UID_NOBODY;
59487c478bd9Sstevel@tonic-gate 			}
59497c478bd9Sstevel@tonic-gate 			break;
59507c478bd9Sstevel@tonic-gate 		case _X_UNAME:
59517c478bd9Sstevel@tonic-gate 			if (utf8_local("uname", &Xtarhdr.x_uname,
59527c478bd9Sstevel@tonic-gate 			    local_uname, value, _POSIX_NAME_MAX) == 0)
59537c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_UNAME;
59547c478bd9Sstevel@tonic-gate 			break;
59557c478bd9Sstevel@tonic-gate 		case _X_MTIME:
59567c478bd9Sstevel@tonic-gate 			get_xtime(value, &(Xtarhdr.x_mtime));
59577c478bd9Sstevel@tonic-gate 			if (errno)
59587c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
59597c478bd9Sstevel@tonic-gate 				    "tar: Extended header modification time "
59607c478bd9Sstevel@tonic-gate 				    "value error for file # %llu.\n"),
59617c478bd9Sstevel@tonic-gate 				    xhdr_count);
59627c478bd9Sstevel@tonic-gate 			else
59637c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_MTIME;
59647c478bd9Sstevel@tonic-gate 			break;
59657c478bd9Sstevel@tonic-gate 		default:
59667c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
59677c478bd9Sstevel@tonic-gate 			    gettext("tar:  unrecognized extended"
59687c478bd9Sstevel@tonic-gate 			    " header keyword '%s'.  Ignored.\n"), keyword);
59697c478bd9Sstevel@tonic-gate 			break;
59707c478bd9Sstevel@tonic-gate 		}
59717c478bd9Sstevel@tonic-gate 	}
59727c478bd9Sstevel@tonic-gate 
59737c478bd9Sstevel@tonic-gate 	getdir();	/* get regular header */
59747c478bd9Sstevel@tonic-gate 
59757c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_DEVMAJOR) {
59767c478bd9Sstevel@tonic-gate 		Gen.g_devmajor = Xtarhdr.x_devmajor;
59777c478bd9Sstevel@tonic-gate 	}
59787c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_DEVMINOR) {
59797c478bd9Sstevel@tonic-gate 		Gen.g_devminor = Xtarhdr.x_devminor;
59807c478bd9Sstevel@tonic-gate 	}
59817c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_GID) {
59827c478bd9Sstevel@tonic-gate 		Gen.g_gid = Xtarhdr.x_gid;
59837c478bd9Sstevel@tonic-gate 		sp->st_gid = Gen.g_gid;
59847c478bd9Sstevel@tonic-gate 	}
59857c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_UID) {
59867c478bd9Sstevel@tonic-gate 		Gen.g_uid = Xtarhdr.x_uid;
59877c478bd9Sstevel@tonic-gate 		sp->st_uid = Gen.g_uid;
59887c478bd9Sstevel@tonic-gate 	}
59897c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_SIZE) {
59907c478bd9Sstevel@tonic-gate 		Gen.g_filesz = Xtarhdr.x_filesz;
59917c478bd9Sstevel@tonic-gate 		sp->st_size = Gen.g_filesz;
59927c478bd9Sstevel@tonic-gate 	}
59937c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_MTIME) {
59947c478bd9Sstevel@tonic-gate 		Gen.g_mtime = Xtarhdr.x_mtime.tv_sec;
59957c478bd9Sstevel@tonic-gate 		sp->st_mtim.tv_sec = Gen.g_mtime;
59967c478bd9Sstevel@tonic-gate 		sp->st_mtim.tv_nsec = Xtarhdr.x_mtime.tv_nsec;
59977c478bd9Sstevel@tonic-gate 	}
59987c478bd9Sstevel@tonic-gate 
59997c478bd9Sstevel@tonic-gate 	if (errors && errflag)
60007c478bd9Sstevel@tonic-gate 		done(1);
60017c478bd9Sstevel@tonic-gate 	else
60027c478bd9Sstevel@tonic-gate 		if (errors)
60037c478bd9Sstevel@tonic-gate 			Errflg = 1;
60047c478bd9Sstevel@tonic-gate 	return (errors);
60057c478bd9Sstevel@tonic-gate }
60067c478bd9Sstevel@tonic-gate 
60077c478bd9Sstevel@tonic-gate /*
60087c478bd9Sstevel@tonic-gate  * gen_num creates a string from a keyword and an usigned long long in the
60097c478bd9Sstevel@tonic-gate  * format:  %d %s=%s\n
60107c478bd9Sstevel@tonic-gate  * This is part of the extended header data record.
60117c478bd9Sstevel@tonic-gate  */
60127c478bd9Sstevel@tonic-gate 
60137c478bd9Sstevel@tonic-gate void
60147c478bd9Sstevel@tonic-gate gen_num(const char *keyword, const u_longlong_t number)
60157c478bd9Sstevel@tonic-gate {
60167c478bd9Sstevel@tonic-gate 	char	save_val[ULONGLONG_MAX_DIGITS + 1];
60177c478bd9Sstevel@tonic-gate 	int	len;
60187c478bd9Sstevel@tonic-gate 	char	*curr_ptr;
60197c478bd9Sstevel@tonic-gate 
60207c478bd9Sstevel@tonic-gate 	(void) sprintf(save_val, "%llu", number);
60217c478bd9Sstevel@tonic-gate 	/*
60227c478bd9Sstevel@tonic-gate 	 * len = length of entire line, including itself.  len will be
60237c478bd9Sstevel@tonic-gate 	 * two digits.  So, add the string lengths plus the length of len,
60247c478bd9Sstevel@tonic-gate 	 * plus a blank, an equal sign, and a newline.
60257c478bd9Sstevel@tonic-gate 	 */
60267c478bd9Sstevel@tonic-gate 	len = strlen(save_val) + strlen(keyword) + 5;
60277c478bd9Sstevel@tonic-gate 	if (xrec_offset + len > xrec_size) {
60287c478bd9Sstevel@tonic-gate 		if (((curr_ptr = realloc(xrec_ptr, 2 * xrec_size)) == NULL))
60297c478bd9Sstevel@tonic-gate 			fatal(gettext(
60307c478bd9Sstevel@tonic-gate 			    "cannot allocate extended header buffer"));
60317c478bd9Sstevel@tonic-gate 		xrec_ptr = curr_ptr;
60327c478bd9Sstevel@tonic-gate 		xrec_size *= 2;
60337c478bd9Sstevel@tonic-gate 	}
60347c478bd9Sstevel@tonic-gate 	(void) sprintf(&xrec_ptr[xrec_offset],
60357c478bd9Sstevel@tonic-gate 	    "%d %s=%s\n", len, keyword, save_val);
60367c478bd9Sstevel@tonic-gate 	xrec_offset += len;
60377c478bd9Sstevel@tonic-gate }
60387c478bd9Sstevel@tonic-gate 
60397c478bd9Sstevel@tonic-gate /*
60407c478bd9Sstevel@tonic-gate  * gen_date creates a string from a keyword and a timestruc_t in the
60417c478bd9Sstevel@tonic-gate  * format:  %d %s=%s\n
60427c478bd9Sstevel@tonic-gate  * This is part of the extended header data record.
60437c478bd9Sstevel@tonic-gate  * Currently, granularity is only microseconds, so the low-order three digits
60447c478bd9Sstevel@tonic-gate  * will be truncated.
60457c478bd9Sstevel@tonic-gate  */
60467c478bd9Sstevel@tonic-gate 
60477c478bd9Sstevel@tonic-gate void
60487c478bd9Sstevel@tonic-gate gen_date(const char *keyword, const timestruc_t time_value)
60497c478bd9Sstevel@tonic-gate {
60507c478bd9Sstevel@tonic-gate 	/* Allow for <seconds>.<nanoseconds>\n */
60517c478bd9Sstevel@tonic-gate 	char	save_val[TIME_MAX_DIGITS + LONG_MAX_DIGITS + 2];
60527c478bd9Sstevel@tonic-gate 	int	len;
60537c478bd9Sstevel@tonic-gate 	char	*curr_ptr;
60547c478bd9Sstevel@tonic-gate 
60557c478bd9Sstevel@tonic-gate 	(void) sprintf(save_val, "%ld", time_value.tv_sec);
60567c478bd9Sstevel@tonic-gate 	len = strlen(save_val);
60577c478bd9Sstevel@tonic-gate 	save_val[len] = '.';
60587c478bd9Sstevel@tonic-gate 	(void) sprintf(&save_val[len + 1], "%9.9ld", time_value.tv_nsec);
60597c478bd9Sstevel@tonic-gate 
60607c478bd9Sstevel@tonic-gate 	/*
60617c478bd9Sstevel@tonic-gate 	 * len = length of entire line, including itself.  len will be
60627c478bd9Sstevel@tonic-gate 	 * two digits.  So, add the string lengths plus the length of len,
60637c478bd9Sstevel@tonic-gate 	 * plus a blank, an equal sign, and a newline.
60647c478bd9Sstevel@tonic-gate 	 */
60657c478bd9Sstevel@tonic-gate 	len = strlen(save_val) + strlen(keyword) + 5;
60667c478bd9Sstevel@tonic-gate 	if (xrec_offset + len > xrec_size) {
60677c478bd9Sstevel@tonic-gate 		if (((curr_ptr = realloc(xrec_ptr, 2 * xrec_size)) == NULL))
60687c478bd9Sstevel@tonic-gate 			fatal(gettext(
60697c478bd9Sstevel@tonic-gate 			    "cannot allocate extended header buffer"));
60707c478bd9Sstevel@tonic-gate 		xrec_ptr = curr_ptr;
60717c478bd9Sstevel@tonic-gate 		xrec_size *= 2;
60727c478bd9Sstevel@tonic-gate 	}
60737c478bd9Sstevel@tonic-gate 	(void) sprintf(&xrec_ptr[xrec_offset],
60747c478bd9Sstevel@tonic-gate 	    "%d %s=%s\n", len, keyword, save_val);
60757c478bd9Sstevel@tonic-gate 	xrec_offset += len;
60767c478bd9Sstevel@tonic-gate }
60777c478bd9Sstevel@tonic-gate 
60787c478bd9Sstevel@tonic-gate /*
60797c478bd9Sstevel@tonic-gate  * gen_string creates a string from a keyword and a char * in the
60807c478bd9Sstevel@tonic-gate  * format:  %d %s=%s\n
60817c478bd9Sstevel@tonic-gate  * This is part of the extended header data record.
60827c478bd9Sstevel@tonic-gate  */
60837c478bd9Sstevel@tonic-gate 
60847c478bd9Sstevel@tonic-gate void
60857c478bd9Sstevel@tonic-gate gen_string(const char *keyword, const char *value)
60867c478bd9Sstevel@tonic-gate {
60877c478bd9Sstevel@tonic-gate 	int	len;
60887c478bd9Sstevel@tonic-gate 	char	*curr_ptr;
60897c478bd9Sstevel@tonic-gate 
60907c478bd9Sstevel@tonic-gate 	/*
60917c478bd9Sstevel@tonic-gate 	 * len = length of entire line, including itself.  The character length
60927c478bd9Sstevel@tonic-gate 	 * of len must be 1-4 characters, because the maximum size of the path
60937c478bd9Sstevel@tonic-gate 	 * or the name is PATH_MAX, which is 1024.  So, assume 1 character
60947c478bd9Sstevel@tonic-gate 	 * for len, one for the space, one for the "=", and one for the newline.
60957c478bd9Sstevel@tonic-gate 	 * Then adjust as needed.
60967c478bd9Sstevel@tonic-gate 	 */
60977c478bd9Sstevel@tonic-gate 	/* LINTED constant expression */
60987c478bd9Sstevel@tonic-gate 	assert(PATH_MAX <= 9996);
60997c478bd9Sstevel@tonic-gate 	len = strlen(value) + strlen(keyword) + 4;
61007c478bd9Sstevel@tonic-gate 	if (len > 997)
61017c478bd9Sstevel@tonic-gate 		len += 3;
61027c478bd9Sstevel@tonic-gate 	else if (len > 98)
61037c478bd9Sstevel@tonic-gate 		len += 2;
61047c478bd9Sstevel@tonic-gate 	else if (len > 9)
61057c478bd9Sstevel@tonic-gate 		len += 1;
61067c478bd9Sstevel@tonic-gate 	if (xrec_offset + len > xrec_size) {
61077c478bd9Sstevel@tonic-gate 		if (((curr_ptr = realloc(xrec_ptr, 2 * xrec_size)) == NULL))
61087c478bd9Sstevel@tonic-gate 			fatal(gettext(
61097c478bd9Sstevel@tonic-gate 			    "cannot allocate extended header buffer"));
61107c478bd9Sstevel@tonic-gate 		xrec_ptr = curr_ptr;
61117c478bd9Sstevel@tonic-gate 		xrec_size *= 2;
61127c478bd9Sstevel@tonic-gate 	}
61137c478bd9Sstevel@tonic-gate #ifdef XHDR_DEBUG
61147c478bd9Sstevel@tonic-gate 	if (strcmp(keyword+1, "name") != 0)
61157c478bd9Sstevel@tonic-gate #endif
61167c478bd9Sstevel@tonic-gate 	(void) sprintf(&xrec_ptr[xrec_offset],
61177c478bd9Sstevel@tonic-gate 	    "%d %s=%s\n", len, keyword, value);
61187c478bd9Sstevel@tonic-gate #ifdef XHDR_DEBUG
61197c478bd9Sstevel@tonic-gate 	else {
61207c478bd9Sstevel@tonic-gate 	len += 11;
61217c478bd9Sstevel@tonic-gate 	(void) sprintf(&xrec_ptr[xrec_offset],
61227c478bd9Sstevel@tonic-gate 	    "%d %s=%snametoolong\n", len, keyword, value);
61237c478bd9Sstevel@tonic-gate 	}
61247c478bd9Sstevel@tonic-gate #endif
61257c478bd9Sstevel@tonic-gate 	xrec_offset += len;
61267c478bd9Sstevel@tonic-gate }
61277c478bd9Sstevel@tonic-gate 
61287c478bd9Sstevel@tonic-gate /*
61297c478bd9Sstevel@tonic-gate  * Convert time found in the extended header data to seconds and nanoseconds.
61307c478bd9Sstevel@tonic-gate  */
61317c478bd9Sstevel@tonic-gate 
61327c478bd9Sstevel@tonic-gate void
61337c478bd9Sstevel@tonic-gate get_xtime(char *value, timestruc_t *xtime)
61347c478bd9Sstevel@tonic-gate {
61357c478bd9Sstevel@tonic-gate 	char nanosec[10];
61367c478bd9Sstevel@tonic-gate 	char *period;
61377c478bd9Sstevel@tonic-gate 	int i;
61387c478bd9Sstevel@tonic-gate 
61397c478bd9Sstevel@tonic-gate 	(void) memset(nanosec, '0', 9);
61407c478bd9Sstevel@tonic-gate 	nanosec[9] = '\0';
61417c478bd9Sstevel@tonic-gate 
61427c478bd9Sstevel@tonic-gate 	period = strchr(value, '.');
61437c478bd9Sstevel@tonic-gate 	if (period != NULL)
61447c478bd9Sstevel@tonic-gate 		period[0] = '\0';
61457c478bd9Sstevel@tonic-gate 	xtime->tv_sec = strtol(value, NULL, 10);
61467c478bd9Sstevel@tonic-gate 	if (period == NULL)
61477c478bd9Sstevel@tonic-gate 		xtime->tv_nsec = 0;
61487c478bd9Sstevel@tonic-gate 	else {
61497c478bd9Sstevel@tonic-gate 		i = strlen(period +1);
61507c478bd9Sstevel@tonic-gate 		(void) strncpy(nanosec, period + 1, min(i, 9));
61517c478bd9Sstevel@tonic-gate 		xtime->tv_nsec = strtol(nanosec, NULL, 10);
61527c478bd9Sstevel@tonic-gate 	}
61537c478bd9Sstevel@tonic-gate }
61547c478bd9Sstevel@tonic-gate 
61557c478bd9Sstevel@tonic-gate /*
61567c478bd9Sstevel@tonic-gate  *	Check linkpath for length.
61577c478bd9Sstevel@tonic-gate  *	Emit an error message and return 1 if too long.
61587c478bd9Sstevel@tonic-gate  */
61597c478bd9Sstevel@tonic-gate 
61607c478bd9Sstevel@tonic-gate int
61617c478bd9Sstevel@tonic-gate chk_path_build(
61627c478bd9Sstevel@tonic-gate 	char	*name,
61637c478bd9Sstevel@tonic-gate 	char	*longname,
61647c478bd9Sstevel@tonic-gate 	char	*linkname,
61657c478bd9Sstevel@tonic-gate 	char	*prefix,
61667c478bd9Sstevel@tonic-gate 	char	type,
61677c478bd9Sstevel@tonic-gate 	int	filetype)
61687c478bd9Sstevel@tonic-gate {
61697c478bd9Sstevel@tonic-gate 
61707c478bd9Sstevel@tonic-gate 	if (strlen(linkname) > (size_t)NAMSIZ) {
61717c478bd9Sstevel@tonic-gate 		if (Eflag > 0) {
61727c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_LINKPATH;
61737c478bd9Sstevel@tonic-gate 			Xtarhdr.x_linkpath = linkname;
61747c478bd9Sstevel@tonic-gate 		} else {
61757c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
61767c478bd9Sstevel@tonic-gate 			    "tar: %s: linked to %s\n"), longname, linkname);
61777c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
61787c478bd9Sstevel@tonic-gate 			    "tar: %s: linked name too long\n"), linkname);
61797c478bd9Sstevel@tonic-gate 			if (errflag)
61807c478bd9Sstevel@tonic-gate 				done(1);
61817c478bd9Sstevel@tonic-gate 			else
61827c478bd9Sstevel@tonic-gate 				Errflg = 1;
61837c478bd9Sstevel@tonic-gate 			return (1);
61847c478bd9Sstevel@tonic-gate 		}
61857c478bd9Sstevel@tonic-gate 	}
61867c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_LINKPATH)
61877c478bd9Sstevel@tonic-gate 		return (build_dblock(name, tchar, type,
61887c478bd9Sstevel@tonic-gate 		    filetype, &stbuf, stbuf.st_dev,
61897c478bd9Sstevel@tonic-gate 		    prefix));
61907c478bd9Sstevel@tonic-gate 	else
61917c478bd9Sstevel@tonic-gate 		return (build_dblock(name, linkname, type,
61927c478bd9Sstevel@tonic-gate 		    filetype, &stbuf, stbuf.st_dev, prefix));
61937c478bd9Sstevel@tonic-gate }
61947c478bd9Sstevel@tonic-gate 
61957c478bd9Sstevel@tonic-gate /*
61967c478bd9Sstevel@tonic-gate  * Convert from UTF-8 to local character set.
61977c478bd9Sstevel@tonic-gate  */
61987c478bd9Sstevel@tonic-gate 
61997c478bd9Sstevel@tonic-gate static int
62007c478bd9Sstevel@tonic-gate utf8_local(
62017c478bd9Sstevel@tonic-gate 	char		*option,
62027c478bd9Sstevel@tonic-gate 	char		**Xhdr_ptrptr,
62037c478bd9Sstevel@tonic-gate 	char		*target,
62047c478bd9Sstevel@tonic-gate 	const char	*source,
62057c478bd9Sstevel@tonic-gate 	int		max_val)
62067c478bd9Sstevel@tonic-gate {
62077c478bd9Sstevel@tonic-gate 	static	iconv_t	iconv_cd;
62087c478bd9Sstevel@tonic-gate 	char		*nl_target;
62097c478bd9Sstevel@tonic-gate 	const	char	*iconv_src;
62107c478bd9Sstevel@tonic-gate 	char		*iconv_trg;
6211eace40a5Sceastha 	size_t		inlen;
6212eace40a5Sceastha 	size_t		outlen;
62137c478bd9Sstevel@tonic-gate 
62147c478bd9Sstevel@tonic-gate 	if (charset_type == -1) {	/* iconv_open failed in earlier try */
62157c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
62167c478bd9Sstevel@tonic-gate 		    "tar:  file # %llu: (%s) UTF-8 conversion failed.\n"),
62177c478bd9Sstevel@tonic-gate 		    xhdr_count, source);
62187c478bd9Sstevel@tonic-gate 		return (1);
62197c478bd9Sstevel@tonic-gate 	} else if (charset_type == 0) {	/* iconv_open has not yet been done */
62207c478bd9Sstevel@tonic-gate 		nl_target = nl_langinfo(CODESET);
62217c478bd9Sstevel@tonic-gate 		if (strlen(nl_target) == 0)	/* locale using 7-bit codeset */
62227c478bd9Sstevel@tonic-gate 			nl_target = "646";
62237c478bd9Sstevel@tonic-gate 		if (strcmp(nl_target, "646") == 0)
62247c478bd9Sstevel@tonic-gate 			charset_type = 1;
62257c478bd9Sstevel@tonic-gate 		else if (strcmp(nl_target, "UTF-8") == 0)
62267c478bd9Sstevel@tonic-gate 			charset_type = 3;
62277c478bd9Sstevel@tonic-gate 		else {
62287c478bd9Sstevel@tonic-gate 			if (strncmp(nl_target, "ISO", 3) == 0)
62297c478bd9Sstevel@tonic-gate 				nl_target += 3;
62307c478bd9Sstevel@tonic-gate 			charset_type = 2;
62317c478bd9Sstevel@tonic-gate 			errno = 0;
62327c478bd9Sstevel@tonic-gate 			if ((iconv_cd = iconv_open(nl_target, "UTF-8")) ==
62337c478bd9Sstevel@tonic-gate 			    (iconv_t)-1) {
62347c478bd9Sstevel@tonic-gate 				if (errno == EINVAL)
62357c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
62367c478bd9Sstevel@tonic-gate 					    "tar: conversion routines not "
62377c478bd9Sstevel@tonic-gate 					    "available for current locale.  "));
62387c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
62397c478bd9Sstevel@tonic-gate 				    "file # %llu: (%s) UTF-8 conversion"
62407c478bd9Sstevel@tonic-gate 				    " failed.\n"), xhdr_count, source);
62417c478bd9Sstevel@tonic-gate 				charset_type = -1;
62427c478bd9Sstevel@tonic-gate 				return (1);
62437c478bd9Sstevel@tonic-gate 			}
62447c478bd9Sstevel@tonic-gate 		}
62457c478bd9Sstevel@tonic-gate 	}
62467c478bd9Sstevel@tonic-gate 
62477c478bd9Sstevel@tonic-gate 	/* locale using 7-bit codeset or UTF-8 locale */
62487c478bd9Sstevel@tonic-gate 	if (charset_type == 1 || charset_type == 3) {
62497c478bd9Sstevel@tonic-gate 		if (strlen(source) > max_val) {
62507c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
62517c478bd9Sstevel@tonic-gate 			    "tar: file # %llu: Extended header %s too long.\n"),
62527c478bd9Sstevel@tonic-gate 			    xhdr_count, option);
62537c478bd9Sstevel@tonic-gate 			return (1);
62547c478bd9Sstevel@tonic-gate 		}
62557c478bd9Sstevel@tonic-gate 		if (charset_type == 3)
62567c478bd9Sstevel@tonic-gate 			(void) strcpy(target, source);
62577c478bd9Sstevel@tonic-gate 		else if (c_utf8(target, source) != 0) {
62587c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
62597c478bd9Sstevel@tonic-gate 			    "tar:  file # %llu: (%s) UTF-8 conversion"
62607c478bd9Sstevel@tonic-gate 			    " failed.\n"), xhdr_count, source);
62617c478bd9Sstevel@tonic-gate 			return (1);
62627c478bd9Sstevel@tonic-gate 		}
62637c478bd9Sstevel@tonic-gate 		*Xhdr_ptrptr = target;
62647c478bd9Sstevel@tonic-gate 		return (0);
62657c478bd9Sstevel@tonic-gate 	}
62667c478bd9Sstevel@tonic-gate 
62677c478bd9Sstevel@tonic-gate 	iconv_src = source;
62687c478bd9Sstevel@tonic-gate 	iconv_trg = target;
62697c478bd9Sstevel@tonic-gate 	inlen = strlen(source);
62707c478bd9Sstevel@tonic-gate 	outlen = max_val * UTF_8_FACTOR;
62717c478bd9Sstevel@tonic-gate 	if (iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen) ==
62727c478bd9Sstevel@tonic-gate 	    (size_t)-1) {	/* Error occurred:  didn't convert */
62737c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
62747c478bd9Sstevel@tonic-gate 		    "tar:  file # %llu: (%s) UTF-8 conversion failed.\n"),
62757c478bd9Sstevel@tonic-gate 		    xhdr_count, source);
62767c478bd9Sstevel@tonic-gate 		/* Get remaining output; reinitialize conversion descriptor */
62777c478bd9Sstevel@tonic-gate 		iconv_src = (const char *)NULL;
62787c478bd9Sstevel@tonic-gate 		inlen = 0;
62797c478bd9Sstevel@tonic-gate 		(void) iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen);
62807c478bd9Sstevel@tonic-gate 		return (1);
62817c478bd9Sstevel@tonic-gate 	}
62827c478bd9Sstevel@tonic-gate 	/* Get remaining output; reinitialize conversion descriptor */
62837c478bd9Sstevel@tonic-gate 	iconv_src = (const char *)NULL;
62847c478bd9Sstevel@tonic-gate 	inlen = 0;
62857c478bd9Sstevel@tonic-gate 	if (iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen) ==
62867c478bd9Sstevel@tonic-gate 	    (size_t)-1) {	/* Error occurred:  didn't convert */
62877c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
62887c478bd9Sstevel@tonic-gate 		    "tar:  file # %llu: (%s) UTF-8 conversion failed.\n"),
62897c478bd9Sstevel@tonic-gate 		    xhdr_count, source);
62907c478bd9Sstevel@tonic-gate 		return (1);
62917c478bd9Sstevel@tonic-gate 	}
62927c478bd9Sstevel@tonic-gate 
62937c478bd9Sstevel@tonic-gate 	*iconv_trg = '\0';	/* Null-terminate iconv output string */
62947c478bd9Sstevel@tonic-gate 	if (strlen(target) > max_val) {
62957c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
62967c478bd9Sstevel@tonic-gate 		    "tar: file # %llu: Extended header %s too long.\n"),
62977c478bd9Sstevel@tonic-gate 		    xhdr_count, option);
62987c478bd9Sstevel@tonic-gate 		return (1);
62997c478bd9Sstevel@tonic-gate 	}
63007c478bd9Sstevel@tonic-gate 	*Xhdr_ptrptr = target;
63017c478bd9Sstevel@tonic-gate 	return (0);
63027c478bd9Sstevel@tonic-gate }
63037c478bd9Sstevel@tonic-gate 
63047c478bd9Sstevel@tonic-gate /*
63057c478bd9Sstevel@tonic-gate  * Check gname, uname, path, and linkpath to see if they need to go in an
63067c478bd9Sstevel@tonic-gate  * extended header.  If they are already slated to be in an extended header,
63077c478bd9Sstevel@tonic-gate  * or if they are not ascii, then they need to be in the extended header.
63087c478bd9Sstevel@tonic-gate  * Then, convert all extended names to UTF-8.
63097c478bd9Sstevel@tonic-gate  */
63107c478bd9Sstevel@tonic-gate 
63117c478bd9Sstevel@tonic-gate int
63127c478bd9Sstevel@tonic-gate gen_utf8_names(const char *filename)
63137c478bd9Sstevel@tonic-gate {
63147c478bd9Sstevel@tonic-gate 	static	iconv_t	iconv_cd;
63157c478bd9Sstevel@tonic-gate 	char		*nl_target;
63167c478bd9Sstevel@tonic-gate 	char		tempbuf[MAXNAM + 1];
6317eace40a5Sceastha 	int		nbytes;
6318eace40a5Sceastha 	int		errors;
63197c478bd9Sstevel@tonic-gate 
63207c478bd9Sstevel@tonic-gate 	if (charset_type == -1)	{	/* Previous failure to open. */
63217c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
63227c478bd9Sstevel@tonic-gate 		    "tar: file # %llu: UTF-8 conversion failed.\n"),
63237c478bd9Sstevel@tonic-gate 		    xhdr_count);
63247c478bd9Sstevel@tonic-gate 		return (1);
63257c478bd9Sstevel@tonic-gate 	}
63267c478bd9Sstevel@tonic-gate 
63277c478bd9Sstevel@tonic-gate 	if (charset_type == 0) {	/* Need to get conversion descriptor */
63287c478bd9Sstevel@tonic-gate 		nl_target = nl_langinfo(CODESET);
63297c478bd9Sstevel@tonic-gate 		if (strlen(nl_target) == 0)	/* locale using 7-bit codeset */
63307c478bd9Sstevel@tonic-gate 			nl_target = "646";
63317c478bd9Sstevel@tonic-gate 		if (strcmp(nl_target, "646") == 0)
63327c478bd9Sstevel@tonic-gate 			charset_type = 1;
63337c478bd9Sstevel@tonic-gate 		else if (strcmp(nl_target, "UTF-8") == 0)
63347c478bd9Sstevel@tonic-gate 			charset_type = 3;
63357c478bd9Sstevel@tonic-gate 		else {
63367c478bd9Sstevel@tonic-gate 			if (strncmp(nl_target, "ISO", 3) == 0)
63377c478bd9Sstevel@tonic-gate 				nl_target += 3;
63387c478bd9Sstevel@tonic-gate 			charset_type = 2;
63397c478bd9Sstevel@tonic-gate 			errno = 0;
63407c478bd9Sstevel@tonic-gate #ifdef ICONV_DEBUG
63417c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
63427c478bd9Sstevel@tonic-gate 			    "Opening iconv_cd with target %s\n",
63437c478bd9Sstevel@tonic-gate 			    nl_target);
63447c478bd9Sstevel@tonic-gate #endif
63457c478bd9Sstevel@tonic-gate 			if ((iconv_cd = iconv_open("UTF-8", nl_target)) ==
63467c478bd9Sstevel@tonic-gate 			    (iconv_t)-1) {
63477c478bd9Sstevel@tonic-gate 				if (errno == EINVAL)
63487c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
63497c478bd9Sstevel@tonic-gate 					    "tar: conversion routines not "
63507c478bd9Sstevel@tonic-gate 					    "available for current locale.  "));
63517c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
63527c478bd9Sstevel@tonic-gate 				    "file (%s): UTF-8 conversion failed.\n"),
63537c478bd9Sstevel@tonic-gate 				    filename);
63547c478bd9Sstevel@tonic-gate 				charset_type = -1;
63557c478bd9Sstevel@tonic-gate 				return (1);
63567c478bd9Sstevel@tonic-gate 			}
63577c478bd9Sstevel@tonic-gate 		}
63587c478bd9Sstevel@tonic-gate 	}
63597c478bd9Sstevel@tonic-gate 
63607c478bd9Sstevel@tonic-gate 	errors = 0;
63617c478bd9Sstevel@tonic-gate 
63627c478bd9Sstevel@tonic-gate 	errors += local_utf8(&Xtarhdr.x_gname, local_gname,
63637c478bd9Sstevel@tonic-gate 	    dblock.dbuf.gname, iconv_cd, _X_GNAME, _POSIX_NAME_MAX);
63647c478bd9Sstevel@tonic-gate 	errors += local_utf8(&Xtarhdr.x_uname, local_uname,
63657c478bd9Sstevel@tonic-gate 	    dblock.dbuf.uname, iconv_cd, _X_UNAME,  _POSIX_NAME_MAX);
63667c478bd9Sstevel@tonic-gate 	if ((xhdr_flgs & _X_LINKPATH) == 0) {	/* Need null-terminated str. */
63677c478bd9Sstevel@tonic-gate 		(void) strncpy(tempbuf, dblock.dbuf.linkname, NAMSIZ);
63687c478bd9Sstevel@tonic-gate 		tempbuf[NAMSIZ] = '\0';
63697c478bd9Sstevel@tonic-gate 	}
63707c478bd9Sstevel@tonic-gate 	errors += local_utf8(&Xtarhdr.x_linkpath, local_linkpath,
63717c478bd9Sstevel@tonic-gate 	    tempbuf, iconv_cd, _X_LINKPATH, PATH_MAX);
63727c478bd9Sstevel@tonic-gate 	if ((xhdr_flgs & _X_PATH) == 0) {	/* Concatenate prefix & name */
63737c478bd9Sstevel@tonic-gate 		(void) strncpy(tempbuf, dblock.dbuf.prefix, PRESIZ);
63740578ac30Ssr161167 		tempbuf[PRESIZ] = '\0';
63757c478bd9Sstevel@tonic-gate 		nbytes = strlen(tempbuf);
63767c478bd9Sstevel@tonic-gate 		if (nbytes > 0) {
63777c478bd9Sstevel@tonic-gate 			tempbuf[nbytes++] = '/';
63787c478bd9Sstevel@tonic-gate 			tempbuf[nbytes] = '\0';
63797c478bd9Sstevel@tonic-gate 		}
63800578ac30Ssr161167 		(void) strncat(tempbuf + nbytes, dblock.dbuf.name,
63810578ac30Ssr161167 		    (MAXNAM - nbytes));
63820578ac30Ssr161167 		tempbuf[MAXNAM] = '\0';
63837c478bd9Sstevel@tonic-gate 	}
63847c478bd9Sstevel@tonic-gate 	errors += local_utf8(&Xtarhdr.x_path, local_path,
63857c478bd9Sstevel@tonic-gate 	    tempbuf, iconv_cd, _X_PATH, PATH_MAX);
63867c478bd9Sstevel@tonic-gate 
63877c478bd9Sstevel@tonic-gate 	if (errors > 0)
63887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
63897c478bd9Sstevel@tonic-gate 		    "tar: file (%s): UTF-8 conversion failed.\n"), filename);
63907c478bd9Sstevel@tonic-gate 
63917c478bd9Sstevel@tonic-gate 	if (errors && errflag)
63927c478bd9Sstevel@tonic-gate 		done(1);
63937c478bd9Sstevel@tonic-gate 	else
63947c478bd9Sstevel@tonic-gate 		if (errors)
63957c478bd9Sstevel@tonic-gate 			Errflg = 1;
63967c478bd9Sstevel@tonic-gate 	return (errors);
63977c478bd9Sstevel@tonic-gate }
63987c478bd9Sstevel@tonic-gate 
63997c478bd9Sstevel@tonic-gate static int
64007c478bd9Sstevel@tonic-gate local_utf8(
64017c478bd9Sstevel@tonic-gate 		char	**Xhdr_ptrptr,
64027c478bd9Sstevel@tonic-gate 		char	*target,
64037c478bd9Sstevel@tonic-gate 		const	char	*source,
64047c478bd9Sstevel@tonic-gate 		iconv_t	iconv_cd,
64057c478bd9Sstevel@tonic-gate 		int	xhdrflg,
64067c478bd9Sstevel@tonic-gate 		int	max_val)
64077c478bd9Sstevel@tonic-gate {
64087c478bd9Sstevel@tonic-gate 	const	char	*iconv_src;
64097c478bd9Sstevel@tonic-gate 	const	char	*starting_src;
64107c478bd9Sstevel@tonic-gate 	char		*iconv_trg;
6411eace40a5Sceastha 	size_t		inlen;
6412eace40a5Sceastha 	size_t		outlen;
64137c478bd9Sstevel@tonic-gate #ifdef ICONV_DEBUG
64147c478bd9Sstevel@tonic-gate 	unsigned char	c_to_hex;
64157c478bd9Sstevel@tonic-gate #endif
64167c478bd9Sstevel@tonic-gate 
64177c478bd9Sstevel@tonic-gate 	/*
64187c478bd9Sstevel@tonic-gate 	 * If the item is already slated for extended format, get the string
64197c478bd9Sstevel@tonic-gate 	 * to convert from the extended header record.  Otherwise, get it from
64207c478bd9Sstevel@tonic-gate 	 * the regular (dblock) area.
64217c478bd9Sstevel@tonic-gate 	 */
64227c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & xhdrflg) {
64237c478bd9Sstevel@tonic-gate 		if (charset_type == 3) {	/* Already UTF-8, just copy */
64247c478bd9Sstevel@tonic-gate 			(void) strcpy(target, *Xhdr_ptrptr);
64257c478bd9Sstevel@tonic-gate 			*Xhdr_ptrptr = target;
64267c478bd9Sstevel@tonic-gate 			return (0);
64277c478bd9Sstevel@tonic-gate 		} else
64287c478bd9Sstevel@tonic-gate 			iconv_src = (const char *) *Xhdr_ptrptr;
64297c478bd9Sstevel@tonic-gate 	} else {
64307c478bd9Sstevel@tonic-gate 		if (charset_type == 3)		/* Already in UTF-8 format */
64317c478bd9Sstevel@tonic-gate 			return (0);		/* Don't create xhdr record */
64327c478bd9Sstevel@tonic-gate 		iconv_src = source;
64337c478bd9Sstevel@tonic-gate 	}
64347c478bd9Sstevel@tonic-gate 	starting_src = iconv_src;
64357c478bd9Sstevel@tonic-gate 	iconv_trg = target;
64367c478bd9Sstevel@tonic-gate 	if ((inlen = strlen(iconv_src)) == 0)
64377c478bd9Sstevel@tonic-gate 		return (0);
64387c478bd9Sstevel@tonic-gate 
64397c478bd9Sstevel@tonic-gate 	if (charset_type == 1) {	/* locale using 7-bit codeset */
64407c478bd9Sstevel@tonic-gate 		if (c_utf8(target, starting_src) != 0) {
64417c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
64427c478bd9Sstevel@tonic-gate 			    gettext("tar: invalid character in"
64437c478bd9Sstevel@tonic-gate 			    " UTF-8 conversion of '%s'\n"), starting_src);
64447c478bd9Sstevel@tonic-gate 			return (1);
64457c478bd9Sstevel@tonic-gate 		}
64467c478bd9Sstevel@tonic-gate 		return (0);
64477c478bd9Sstevel@tonic-gate 	}
64487c478bd9Sstevel@tonic-gate 
64497c478bd9Sstevel@tonic-gate 	outlen = max_val * UTF_8_FACTOR;
64507c478bd9Sstevel@tonic-gate 	errno = 0;
64517c478bd9Sstevel@tonic-gate 	if (iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen) ==
64527c478bd9Sstevel@tonic-gate 	    (size_t)-1) {
64537c478bd9Sstevel@tonic-gate 		/* An error occurred, or not all characters were converted */
64547c478bd9Sstevel@tonic-gate 		if (errno == EILSEQ)
64557c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
64567c478bd9Sstevel@tonic-gate 			    gettext("tar: invalid character in"
64577c478bd9Sstevel@tonic-gate 			    " UTF-8 conversion of '%s'\n"), starting_src);
64587c478bd9Sstevel@tonic-gate 		else
64597c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
64607c478bd9Sstevel@tonic-gate 			    "tar: conversion to UTF-8 aborted for '%s'.\n"),
64617c478bd9Sstevel@tonic-gate 			    starting_src);
64627c478bd9Sstevel@tonic-gate 		/* Get remaining output; reinitialize conversion descriptor */
64637c478bd9Sstevel@tonic-gate 		iconv_src = (const char *)NULL;
64647c478bd9Sstevel@tonic-gate 		inlen = 0;
64657c478bd9Sstevel@tonic-gate 		(void) iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen);
64667c478bd9Sstevel@tonic-gate 		return (1);
64677c478bd9Sstevel@tonic-gate 	}
64687c478bd9Sstevel@tonic-gate 	/* Get remaining output; reinitialize conversion descriptor */
64697c478bd9Sstevel@tonic-gate 	iconv_src = (const char *)NULL;
64707c478bd9Sstevel@tonic-gate 	inlen = 0;
64717c478bd9Sstevel@tonic-gate 	if (iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen) ==
64727c478bd9Sstevel@tonic-gate 	    (size_t)-1) {	/* Error occurred:  didn't convert */
64737c478bd9Sstevel@tonic-gate 		if (errno == EILSEQ)
64747c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
64757c478bd9Sstevel@tonic-gate 			    gettext("tar: invalid character in"
64767c478bd9Sstevel@tonic-gate 			    " UTF-8 conversion of '%s'\n"), starting_src);
64777c478bd9Sstevel@tonic-gate 		else
64787c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
64797c478bd9Sstevel@tonic-gate 			    "tar: conversion to UTF-8 aborted for '%s'.\n"),
64807c478bd9Sstevel@tonic-gate 			    starting_src);
64817c478bd9Sstevel@tonic-gate 		return (1);
64827c478bd9Sstevel@tonic-gate 	}
64837c478bd9Sstevel@tonic-gate 
64847c478bd9Sstevel@tonic-gate 	*iconv_trg = '\0';	/* Null-terminate iconv output string */
64857c478bd9Sstevel@tonic-gate 	if (strcmp(starting_src, target) != 0) {
64867c478bd9Sstevel@tonic-gate 		*Xhdr_ptrptr = target;
64877c478bd9Sstevel@tonic-gate 		xhdr_flgs |= xhdrflg;
64887c478bd9Sstevel@tonic-gate #ifdef ICONV_DEBUG
64897c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "***  inlen: %d %d; outlen: %d %d\n",
64907c478bd9Sstevel@tonic-gate 		    strlen(starting_src), inlen, max_val, outlen);
64917c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Input string:\n  ");
64927c478bd9Sstevel@tonic-gate 		for (inlen = 0; inlen < strlen(starting_src); inlen++) {
64937c478bd9Sstevel@tonic-gate 			c_to_hex = (unsigned char)starting_src[inlen];
64947c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, " %2.2x", c_to_hex);
64957c478bd9Sstevel@tonic-gate 			if (inlen % 20 == 19)
64967c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\n  ");
64977c478bd9Sstevel@tonic-gate 		}
64987c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\nOutput string:\n  ");
64997c478bd9Sstevel@tonic-gate 		for (inlen = 0; inlen < strlen(target); inlen++) {
65007c478bd9Sstevel@tonic-gate 			c_to_hex = (unsigned char)target[inlen];
65017c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, " %2.2x", c_to_hex);
65027c478bd9Sstevel@tonic-gate 			if (inlen % 20 == 19)
65037c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\n  ");
65047c478bd9Sstevel@tonic-gate 		}
65057c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
65067c478bd9Sstevel@tonic-gate #endif
65077c478bd9Sstevel@tonic-gate 	}
65087c478bd9Sstevel@tonic-gate 
65097c478bd9Sstevel@tonic-gate 	return (0);
65107c478bd9Sstevel@tonic-gate }
65117c478bd9Sstevel@tonic-gate 
65127c478bd9Sstevel@tonic-gate /*
65137c478bd9Sstevel@tonic-gate  *	Function to test each byte of the source string to make sure it is
65147c478bd9Sstevel@tonic-gate  *	in within bounds (value between 0 and 127).
65157c478bd9Sstevel@tonic-gate  *	If valid, copy source to target.
65167c478bd9Sstevel@tonic-gate  */
65177c478bd9Sstevel@tonic-gate 
65187c478bd9Sstevel@tonic-gate int
65197c478bd9Sstevel@tonic-gate c_utf8(char *target, const char *source)
65207c478bd9Sstevel@tonic-gate {
65217c478bd9Sstevel@tonic-gate 	size_t		len;
65227c478bd9Sstevel@tonic-gate 	const char	*thischar;
65237c478bd9Sstevel@tonic-gate 
65247c478bd9Sstevel@tonic-gate 	len = strlen(source);
65257c478bd9Sstevel@tonic-gate 	thischar = source;
65267c478bd9Sstevel@tonic-gate 	while (len-- > 0) {
65277c478bd9Sstevel@tonic-gate 		if (!isascii((int)(*thischar++)))
65287c478bd9Sstevel@tonic-gate 			return (1);
65297c478bd9Sstevel@tonic-gate 	}
65307c478bd9Sstevel@tonic-gate 
65317c478bd9Sstevel@tonic-gate 	(void) strcpy(target, source);
65327c478bd9Sstevel@tonic-gate 	return (0);
65337c478bd9Sstevel@tonic-gate }
65347c478bd9Sstevel@tonic-gate 
65357c478bd9Sstevel@tonic-gate 
65367c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
65377c478bd9Sstevel@tonic-gate #define	ROUNDTOTBLOCK(a)	((a + (TBLOCK -1)) & ~(TBLOCK -1))
65387c478bd9Sstevel@tonic-gate 
65397c478bd9Sstevel@tonic-gate static void
65407c478bd9Sstevel@tonic-gate prepare_xattr(
65417c478bd9Sstevel@tonic-gate 	char		**attrbuf,
65427c478bd9Sstevel@tonic-gate 	char		*filename,
65437c478bd9Sstevel@tonic-gate 	char		*attrname,
65447c478bd9Sstevel@tonic-gate 	char		typeflag,
65457c478bd9Sstevel@tonic-gate 	struct linkbuf	*linkinfo,
65467c478bd9Sstevel@tonic-gate 	int		*rlen)
65477c478bd9Sstevel@tonic-gate {
65487c478bd9Sstevel@tonic-gate 	char			*bufhead;	/* ptr to full buffer */
65497c478bd9Sstevel@tonic-gate 	struct xattr_hdr 	*hptr;		/* ptr to header in bufhead */
65507c478bd9Sstevel@tonic-gate 	struct xattr_buf	*tptr;		/* ptr to pathing pieces */
65517c478bd9Sstevel@tonic-gate 	int			totalen;	/* total buffer length */
65527c478bd9Sstevel@tonic-gate 	int			len;		/* length returned to user */
65537c478bd9Sstevel@tonic-gate 	int			stringlen;	/* length of filename + attr */
65547c478bd9Sstevel@tonic-gate 						/*
65557c478bd9Sstevel@tonic-gate 						 * length of filename + attr
65567c478bd9Sstevel@tonic-gate 						 * in link section
65577c478bd9Sstevel@tonic-gate 						 */
65587c478bd9Sstevel@tonic-gate 	int			linkstringlen;
65597c478bd9Sstevel@tonic-gate 	int			complen;	/* length of pathing section */
65607c478bd9Sstevel@tonic-gate 	int			linklen;	/* length of link section */
65617c478bd9Sstevel@tonic-gate 
65627c478bd9Sstevel@tonic-gate 	/*
65637c478bd9Sstevel@tonic-gate 	 * Release previous buffer
65647c478bd9Sstevel@tonic-gate 	 */
65657c478bd9Sstevel@tonic-gate 
65667c478bd9Sstevel@tonic-gate 	if (*attrbuf != (char *)NULL) {
65677c478bd9Sstevel@tonic-gate 		free(*attrbuf);
65687c478bd9Sstevel@tonic-gate 		*attrbuf = NULL;
65697c478bd9Sstevel@tonic-gate 	}
65707c478bd9Sstevel@tonic-gate 
65717c478bd9Sstevel@tonic-gate 	/*
65727c478bd9Sstevel@tonic-gate 	 * First add in fixed size stuff
65737c478bd9Sstevel@tonic-gate 	 */
65747c478bd9Sstevel@tonic-gate 	len = sizeof (struct xattr_hdr) + sizeof (struct xattr_buf);
65757c478bd9Sstevel@tonic-gate 
65767c478bd9Sstevel@tonic-gate 	/*
65777c478bd9Sstevel@tonic-gate 	 * Add space for two nulls
65787c478bd9Sstevel@tonic-gate 	 */
65797c478bd9Sstevel@tonic-gate 	stringlen = strlen(attrname) + strlen(filename) + 2;
65807c478bd9Sstevel@tonic-gate 	complen = stringlen + sizeof (struct xattr_buf);
65817c478bd9Sstevel@tonic-gate 
65827c478bd9Sstevel@tonic-gate 	len += stringlen;
65837c478bd9Sstevel@tonic-gate 
65847c478bd9Sstevel@tonic-gate 	/*
65857c478bd9Sstevel@tonic-gate 	 * Now add on space for link info if any
65867c478bd9Sstevel@tonic-gate 	 */
65877c478bd9Sstevel@tonic-gate 
65887c478bd9Sstevel@tonic-gate 	if (linkinfo != NULL) {
65897c478bd9Sstevel@tonic-gate 		/*
65907c478bd9Sstevel@tonic-gate 		 * Again add space for two nulls
65917c478bd9Sstevel@tonic-gate 		 */
65927c478bd9Sstevel@tonic-gate 		linkstringlen = strlen(linkinfo->pathname) +
65937c478bd9Sstevel@tonic-gate 		    strlen(linkinfo->attrname) + 2;
65947c478bd9Sstevel@tonic-gate 		len += linkstringlen;
65957c478bd9Sstevel@tonic-gate 	}
65967c478bd9Sstevel@tonic-gate 
65977c478bd9Sstevel@tonic-gate 	/*
65987c478bd9Sstevel@tonic-gate 	 * Now add padding to end to fill out TBLOCK
65997c478bd9Sstevel@tonic-gate 	 *
66007c478bd9Sstevel@tonic-gate 	 * Function returns size of real data and not size + padding.
66017c478bd9Sstevel@tonic-gate 	 */
66027c478bd9Sstevel@tonic-gate 
66037c478bd9Sstevel@tonic-gate 	totalen = ROUNDTOTBLOCK(len);
66047c478bd9Sstevel@tonic-gate 
66057c478bd9Sstevel@tonic-gate 	if ((bufhead = calloc(1, totalen)) == NULL) {
66067c478bd9Sstevel@tonic-gate 		fatal(gettext("Out of memory."));
66077c478bd9Sstevel@tonic-gate 	}
66087c478bd9Sstevel@tonic-gate 
66097c478bd9Sstevel@tonic-gate 
66107c478bd9Sstevel@tonic-gate 	/*
66117c478bd9Sstevel@tonic-gate 	 * Now we can fill in the necessary pieces
66127c478bd9Sstevel@tonic-gate 	 */
66137c478bd9Sstevel@tonic-gate 
66147c478bd9Sstevel@tonic-gate 	if (linkinfo != (struct linkbuf *)NULL) {
66157c478bd9Sstevel@tonic-gate 		linklen = linkstringlen + (sizeof (struct xattr_buf));
66167c478bd9Sstevel@tonic-gate 	} else {
66177c478bd9Sstevel@tonic-gate 		linklen = 0;
66187c478bd9Sstevel@tonic-gate 	}
66197c478bd9Sstevel@tonic-gate 
66207c478bd9Sstevel@tonic-gate 	/*
66217c478bd9Sstevel@tonic-gate 	 * first fill in the fixed header
66227c478bd9Sstevel@tonic-gate 	 */
66237c478bd9Sstevel@tonic-gate 	hptr = (struct xattr_hdr *)bufhead;
66247c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_version, "%s", XATTR_ARCH_VERS);
66257c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_component_len, "%0*d",
66267c478bd9Sstevel@tonic-gate 	    sizeof (hptr->h_component_len) - 1, complen);
66277c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_link_component_len, "%0*d",
66287c478bd9Sstevel@tonic-gate 	    sizeof (hptr->h_link_component_len) - 1, linklen);
66297c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_size, "%0*d", sizeof (hptr->h_size) - 1, len);
66307c478bd9Sstevel@tonic-gate 
66317c478bd9Sstevel@tonic-gate 	/*
66327c478bd9Sstevel@tonic-gate 	 * Now fill in the filename + attrnames section
66337c478bd9Sstevel@tonic-gate 	 */
66347c478bd9Sstevel@tonic-gate 
66357c478bd9Sstevel@tonic-gate 	tptr = (struct xattr_buf *)(bufhead + sizeof (struct xattr_hdr));
66367c478bd9Sstevel@tonic-gate 	(void) sprintf(tptr->h_namesz, "%0*d", sizeof (tptr->h_namesz) - 1,
66377c478bd9Sstevel@tonic-gate 	    stringlen);
66387c478bd9Sstevel@tonic-gate 	(void) strcpy(tptr->h_names, filename);
66397c478bd9Sstevel@tonic-gate 	(void) strcpy(&tptr->h_names[strlen(filename) + 1], attrname);
66407c478bd9Sstevel@tonic-gate 	tptr->h_typeflag = typeflag;
66417c478bd9Sstevel@tonic-gate 
66427c478bd9Sstevel@tonic-gate 	/*
66437c478bd9Sstevel@tonic-gate 	 * Now fill in the optional link section if we have one
66447c478bd9Sstevel@tonic-gate 	 */
66457c478bd9Sstevel@tonic-gate 
66467c478bd9Sstevel@tonic-gate 	if (linkinfo != (struct linkbuf *)NULL) {
66477c478bd9Sstevel@tonic-gate 		tptr = (struct xattr_buf *)(bufhead +
66487c478bd9Sstevel@tonic-gate 		    sizeof (struct xattr_hdr) + complen);
66497c478bd9Sstevel@tonic-gate 
66507c478bd9Sstevel@tonic-gate 		(void) sprintf(tptr->h_namesz, "%0*d",
66517c478bd9Sstevel@tonic-gate 		    sizeof (tptr->h_namesz) - 1, linkstringlen);
66527c478bd9Sstevel@tonic-gate 		(void) strcpy(tptr->h_names, linkinfo->pathname);
66537c478bd9Sstevel@tonic-gate 		(void) strcpy(
66547c478bd9Sstevel@tonic-gate 		    &tptr->h_names[strlen(linkinfo->pathname) + 1],
66557c478bd9Sstevel@tonic-gate 		    linkinfo->attrname);
66567c478bd9Sstevel@tonic-gate 		tptr->h_typeflag = typeflag;
66577c478bd9Sstevel@tonic-gate 	}
66587c478bd9Sstevel@tonic-gate 	*attrbuf = (char *)bufhead;
66597c478bd9Sstevel@tonic-gate 	*rlen = len;
66607c478bd9Sstevel@tonic-gate }
66617c478bd9Sstevel@tonic-gate 
66627c478bd9Sstevel@tonic-gate #else
66637c478bd9Sstevel@tonic-gate static void
66647c478bd9Sstevel@tonic-gate prepare_xattr(
66657c478bd9Sstevel@tonic-gate 	char		**attrbuf,
66667c478bd9Sstevel@tonic-gate 	char		*filename,
66677c478bd9Sstevel@tonic-gate 	char		*attrname,
66687c478bd9Sstevel@tonic-gate 	char		typeflag,
66697c478bd9Sstevel@tonic-gate 	struct linkbuf	*linkinfo,
66707c478bd9Sstevel@tonic-gate 	int		*rlen)
66717c478bd9Sstevel@tonic-gate {
66727c478bd9Sstevel@tonic-gate 	*attrbuf = NULL;
66737c478bd9Sstevel@tonic-gate 	*rlen = 0;
66747c478bd9Sstevel@tonic-gate }
66757c478bd9Sstevel@tonic-gate #endif
66767c478bd9Sstevel@tonic-gate 
66777c478bd9Sstevel@tonic-gate int
66787c478bd9Sstevel@tonic-gate getstat(int dirfd, char *longname, char *shortname)
66797c478bd9Sstevel@tonic-gate {
66807c478bd9Sstevel@tonic-gate 
66817c478bd9Sstevel@tonic-gate 	int i, j;
66827c478bd9Sstevel@tonic-gate 	int	printerr;
66837c478bd9Sstevel@tonic-gate 	int	slnkerr;
66847c478bd9Sstevel@tonic-gate 	struct stat symlnbuf;
66857c478bd9Sstevel@tonic-gate 
66867c478bd9Sstevel@tonic-gate 	if (!hflag)
66877c478bd9Sstevel@tonic-gate 		i = fstatat(dirfd, shortname, &stbuf, AT_SYMLINK_NOFOLLOW);
66887c478bd9Sstevel@tonic-gate 	else
66897c478bd9Sstevel@tonic-gate 		i = fstatat(dirfd, shortname, &stbuf, 0);
66907c478bd9Sstevel@tonic-gate 
66917c478bd9Sstevel@tonic-gate 	if (i < 0) {
66927c478bd9Sstevel@tonic-gate 		/* Initialize flag to print error mesg. */
66937c478bd9Sstevel@tonic-gate 		printerr = 1;
66947c478bd9Sstevel@tonic-gate 		/*
66957c478bd9Sstevel@tonic-gate 		 * If stat is done, then need to do lstat
66967c478bd9Sstevel@tonic-gate 		 * to determine whether it's a sym link
66977c478bd9Sstevel@tonic-gate 		 */
66987c478bd9Sstevel@tonic-gate 		if (hflag) {
66997c478bd9Sstevel@tonic-gate 			/* Save returned error */
67007c478bd9Sstevel@tonic-gate 			slnkerr = errno;
67017c478bd9Sstevel@tonic-gate 
67027c478bd9Sstevel@tonic-gate 			j = fstatat(dirfd, shortname,
67037c478bd9Sstevel@tonic-gate 			    &symlnbuf, AT_SYMLINK_NOFOLLOW);
67047c478bd9Sstevel@tonic-gate 			/*
67057c478bd9Sstevel@tonic-gate 			 * Suppress error message when file is a symbolic link
67067c478bd9Sstevel@tonic-gate 			 * and function modifier 'l' is off.  Exception:  when
67077c478bd9Sstevel@tonic-gate 			 * a symlink points to a symlink points to a
67087c478bd9Sstevel@tonic-gate 			 * symlink ... and we get past MAXSYMLINKS.  That
67097c478bd9Sstevel@tonic-gate 			 * error will cause a file not to be archived, and
67107c478bd9Sstevel@tonic-gate 			 * needs to be printed.
67117c478bd9Sstevel@tonic-gate 			 */
67127c478bd9Sstevel@tonic-gate 			if ((j == 0) && (!linkerrok) && (slnkerr != ELOOP) &&
67137c478bd9Sstevel@tonic-gate 			    (S_ISLNK(symlnbuf.st_mode)))
67147c478bd9Sstevel@tonic-gate 				printerr = 0;
67157c478bd9Sstevel@tonic-gate 
67167c478bd9Sstevel@tonic-gate 			/*
67177c478bd9Sstevel@tonic-gate 			 * Restore errno in case the lstat
67187c478bd9Sstevel@tonic-gate 			 * on symbolic link change
67197c478bd9Sstevel@tonic-gate 			 */
67207c478bd9Sstevel@tonic-gate 			errno = slnkerr;
67217c478bd9Sstevel@tonic-gate 		}
67227c478bd9Sstevel@tonic-gate 
67237c478bd9Sstevel@tonic-gate 		if (printerr) {
67247c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
67257c478bd9Sstevel@tonic-gate 			    "tar: %s: %s\n"), longname, strerror(errno));
67267c478bd9Sstevel@tonic-gate 			Errflg = 1;
67277c478bd9Sstevel@tonic-gate 		}
67287c478bd9Sstevel@tonic-gate 		return (1);
67297c478bd9Sstevel@tonic-gate 	}
67307c478bd9Sstevel@tonic-gate 	return (0);
67317c478bd9Sstevel@tonic-gate }
67327c478bd9Sstevel@tonic-gate 
67337c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
67347c478bd9Sstevel@tonic-gate static void
67357c478bd9Sstevel@tonic-gate xattrs_put(char *longname, char *shortname, char *parent)
67367c478bd9Sstevel@tonic-gate {
67377c478bd9Sstevel@tonic-gate 	int dirfd;
67387c478bd9Sstevel@tonic-gate 	DIR *dirp;
67397c478bd9Sstevel@tonic-gate 	struct dirent *dp;
67407c478bd9Sstevel@tonic-gate 
67417c478bd9Sstevel@tonic-gate 	if (pathconf(shortname, _PC_XATTR_EXISTS) != 1) {
67427c478bd9Sstevel@tonic-gate 		return;
67437c478bd9Sstevel@tonic-gate 	}
67447c478bd9Sstevel@tonic-gate 
67457c478bd9Sstevel@tonic-gate 	if ((dirfd = attropen(shortname, ".", O_RDONLY)) < 0) {
67467c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
67477c478bd9Sstevel@tonic-gate 		    "tar: unable to open attribute directory for file %s\n"),
67487c478bd9Sstevel@tonic-gate 		    longname);
67497c478bd9Sstevel@tonic-gate 		return;
67507c478bd9Sstevel@tonic-gate 	}
67517c478bd9Sstevel@tonic-gate 
67527c478bd9Sstevel@tonic-gate 	if ((dirp = fdopendir(dirfd)) == NULL) {
67537c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
67547c478bd9Sstevel@tonic-gate 		    "tar: unable to open dir pointer for file %s\n"), longname);
67557c478bd9Sstevel@tonic-gate 		return;
67567c478bd9Sstevel@tonic-gate 	}
67577c478bd9Sstevel@tonic-gate 
67587c478bd9Sstevel@tonic-gate 	while (dp = readdir(dirp)) {
67597c478bd9Sstevel@tonic-gate 		if (dp->d_name[0] == '.' && dp->d_name[1] == '.' &&
67607c478bd9Sstevel@tonic-gate 		    dp->d_name[2] == '\0')
67617c478bd9Sstevel@tonic-gate 			continue;
67627c478bd9Sstevel@tonic-gate 
67637c478bd9Sstevel@tonic-gate 		if (dp->d_name[0] == '.' && dp->d_name[1] == '\0')
67647c478bd9Sstevel@tonic-gate 			Hiddendir = 1;
67657c478bd9Sstevel@tonic-gate 		else
67667c478bd9Sstevel@tonic-gate 			Hiddendir = 0;
67677c478bd9Sstevel@tonic-gate 
67687c478bd9Sstevel@tonic-gate 		(void) putfile(longname, dp->d_name, parent,
67697c478bd9Sstevel@tonic-gate 		    XATTR_FILE, LEV0, SYMLINK_LEV0);
67707c478bd9Sstevel@tonic-gate 
67717c478bd9Sstevel@tonic-gate 		if (exitflag)
67727c478bd9Sstevel@tonic-gate 			break;
67737c478bd9Sstevel@tonic-gate 	}
67747c478bd9Sstevel@tonic-gate 
67757c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
67767c478bd9Sstevel@tonic-gate }
67777c478bd9Sstevel@tonic-gate #else
67787c478bd9Sstevel@tonic-gate static void
67797c478bd9Sstevel@tonic-gate xattrs_put(char *longname, char *shortname, char *parent)
67807c478bd9Sstevel@tonic-gate {
67817c478bd9Sstevel@tonic-gate }
67827c478bd9Sstevel@tonic-gate #endif /* O_XATTR */
67837c478bd9Sstevel@tonic-gate 
67847c478bd9Sstevel@tonic-gate static int
67857c478bd9Sstevel@tonic-gate put_link(char *name, char *longname, char *component,
67867c478bd9Sstevel@tonic-gate 		char *prefix, int filetype, char type)
67877c478bd9Sstevel@tonic-gate {
67887c478bd9Sstevel@tonic-gate 
67897c478bd9Sstevel@tonic-gate 	if (stbuf.st_nlink > 1) {
67907c478bd9Sstevel@tonic-gate 		struct linkbuf *lp;
67917c478bd9Sstevel@tonic-gate 		int found = 0;
67927c478bd9Sstevel@tonic-gate 
67937c478bd9Sstevel@tonic-gate 		for (lp = ihead; lp != NULL; lp = lp->nextp)
67947c478bd9Sstevel@tonic-gate 			if (lp->inum == stbuf.st_ino &&
67957c478bd9Sstevel@tonic-gate 			    lp->devnum == stbuf.st_dev) {
67967c478bd9Sstevel@tonic-gate 				found++;
67977c478bd9Sstevel@tonic-gate 				break;
67987c478bd9Sstevel@tonic-gate 			}
67997c478bd9Sstevel@tonic-gate 		if (found) {
68007c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
68017c478bd9Sstevel@tonic-gate 			if (filetype == XATTR_FILE)
68027c478bd9Sstevel@tonic-gate 				if (put_xattr_hdr(longname, component, prefix,
68037c478bd9Sstevel@tonic-gate 				    type, filetype, lp)) {
68047c478bd9Sstevel@tonic-gate 					goto out;
68057c478bd9Sstevel@tonic-gate 			}
68067c478bd9Sstevel@tonic-gate #endif
68077c478bd9Sstevel@tonic-gate 			stbuf.st_size = (off_t)0;
68087c478bd9Sstevel@tonic-gate 			if (filetype != XATTR_FILE) {
68097c478bd9Sstevel@tonic-gate 				tomodes(&stbuf);
68107c478bd9Sstevel@tonic-gate 				if (chk_path_build(name, longname, lp->pathname,
68117c478bd9Sstevel@tonic-gate 				    prefix, type, filetype) > 0) {
68127c478bd9Sstevel@tonic-gate 					goto out;
68137c478bd9Sstevel@tonic-gate 				}
68147c478bd9Sstevel@tonic-gate 			}
68157c478bd9Sstevel@tonic-gate 
68167c478bd9Sstevel@tonic-gate 			if (mulvol && tapepos + 1 >= blocklim)
68177c478bd9Sstevel@tonic-gate 				newvol();
68187c478bd9Sstevel@tonic-gate 			(void) writetbuf((char *)&dblock, 1);
68197c478bd9Sstevel@tonic-gate 			/*
68207c478bd9Sstevel@tonic-gate 			 * write_ancillary() is not needed here.
68217c478bd9Sstevel@tonic-gate 			 * The first link is handled in the following
68227c478bd9Sstevel@tonic-gate 			 * else statement. No need to process ACLs
68237c478bd9Sstevel@tonic-gate 			 * for other hard links since they are the
68247c478bd9Sstevel@tonic-gate 			 * same file.
68257c478bd9Sstevel@tonic-gate 			 */
68267c478bd9Sstevel@tonic-gate 
68277c478bd9Sstevel@tonic-gate 			if (vflag) {
68287c478bd9Sstevel@tonic-gate #ifdef DEBUG
68297c478bd9Sstevel@tonic-gate 				if (NotTape)
68307c478bd9Sstevel@tonic-gate 					DEBUG("seek = %" FMT_blkcnt_t
68317c478bd9Sstevel@tonic-gate 					    "K\t", K(tapepos), 0);
68327c478bd9Sstevel@tonic-gate #endif
68337c478bd9Sstevel@tonic-gate 				if (filetype == XATTR_FILE) {
68347c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext(
68357c478bd9Sstevel@tonic-gate 					    "a %s attribute %s link to "
68367c478bd9Sstevel@tonic-gate 					    "attribute %s\n"),
68377c478bd9Sstevel@tonic-gate 					    name, component, lp->attrname);
68387c478bd9Sstevel@tonic-gate 				} else {
68397c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext(
68407c478bd9Sstevel@tonic-gate 					    "a %s link to %s\n"),
68417c478bd9Sstevel@tonic-gate 					    longname, lp->pathname);
68427c478bd9Sstevel@tonic-gate 				}
68437c478bd9Sstevel@tonic-gate 			}
68447c478bd9Sstevel@tonic-gate 			lp->count--;
68457c478bd9Sstevel@tonic-gate 			return (0);
68467c478bd9Sstevel@tonic-gate 		} else {
68477c478bd9Sstevel@tonic-gate 			lp = (struct linkbuf *)getmem(sizeof (*lp));
68487c478bd9Sstevel@tonic-gate 			if (lp != (struct linkbuf *)NULL) {
68497c478bd9Sstevel@tonic-gate 				lp->nextp = ihead;
68507c478bd9Sstevel@tonic-gate 				ihead = lp;
68517c478bd9Sstevel@tonic-gate 				lp->inum = stbuf.st_ino;
68527c478bd9Sstevel@tonic-gate 				lp->devnum = stbuf.st_dev;
68537c478bd9Sstevel@tonic-gate 				lp->count = stbuf.st_nlink - 1;
68547c478bd9Sstevel@tonic-gate 				if (filetype == XATTR_FILE) {
68557c478bd9Sstevel@tonic-gate 					(void) strcpy(lp->pathname, longname);
68567c478bd9Sstevel@tonic-gate 					(void) strcpy(lp->attrname, component);
68577c478bd9Sstevel@tonic-gate 				} else {
68587c478bd9Sstevel@tonic-gate 					(void) strcpy(lp->pathname, longname);
68597c478bd9Sstevel@tonic-gate 					(void) strcpy(lp->attrname, "");
68607c478bd9Sstevel@tonic-gate 				}
68617c478bd9Sstevel@tonic-gate 			}
68627c478bd9Sstevel@tonic-gate 		}
68637c478bd9Sstevel@tonic-gate 	}
68647c478bd9Sstevel@tonic-gate 
68657c478bd9Sstevel@tonic-gate out:
68667c478bd9Sstevel@tonic-gate 	return (1);
68677c478bd9Sstevel@tonic-gate }
68687c478bd9Sstevel@tonic-gate 
68697c478bd9Sstevel@tonic-gate static int
68707c478bd9Sstevel@tonic-gate put_extra_attributes(char *longname, char *shortname, char *prefix,
68717c478bd9Sstevel@tonic-gate 		int filetype, char typeflag)
68727c478bd9Sstevel@tonic-gate {
6873fa9e4066Sahrens 	static acl_t *aclp = NULL;
6874fa9e4066Sahrens 	int error;
68757c478bd9Sstevel@tonic-gate 
6876fa9e4066Sahrens 	if (aclp != NULL) {
6877fa9e4066Sahrens 		acl_free(aclp);
68787c478bd9Sstevel@tonic-gate 		aclp = NULL;
68797c478bd9Sstevel@tonic-gate 	}
68807c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
68817c478bd9Sstevel@tonic-gate 	if (atflag && filetype == XATTR_FILE) {
68827c478bd9Sstevel@tonic-gate 		if (put_xattr_hdr(longname, shortname, prefix,
68837c478bd9Sstevel@tonic-gate 		    typeflag, filetype, NULL)) {
68847c478bd9Sstevel@tonic-gate 			return (1);
68857c478bd9Sstevel@tonic-gate 		}
68867c478bd9Sstevel@tonic-gate 	}
68877c478bd9Sstevel@tonic-gate #endif
68887c478bd9Sstevel@tonic-gate 
68897c478bd9Sstevel@tonic-gate 	/* ACL support */
68907c478bd9Sstevel@tonic-gate 	if (pflag) {
68917c478bd9Sstevel@tonic-gate 		char	*secinfo = NULL;
68927c478bd9Sstevel@tonic-gate 		int	len = 0;
68937c478bd9Sstevel@tonic-gate 
68947c478bd9Sstevel@tonic-gate 		/* ACL support */
68957c478bd9Sstevel@tonic-gate 		if (((stbuf.st_mode & S_IFMT) != S_IFLNK)) {
68967c478bd9Sstevel@tonic-gate 			/*
68977c478bd9Sstevel@tonic-gate 			 * Get ACL info: dont bother allocating space if
6898fa9e4066Sahrens 			 * there is only a trivial ACL.
68997c478bd9Sstevel@tonic-gate 			 */
6900fa9e4066Sahrens 			if ((error = acl_get(shortname, ACL_NO_TRIVIAL,
6901fa9e4066Sahrens 			    &aclp)) != 0) {
69027c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
6903fa9e4066Sahrens 				    "%s: failed to retrieve acl : %s\n"),
6904fa9e4066Sahrens 				    longname, acl_strerror(error));
69057c478bd9Sstevel@tonic-gate 				return (1);
69067c478bd9Sstevel@tonic-gate 			}
69077c478bd9Sstevel@tonic-gate 		}
69087c478bd9Sstevel@tonic-gate 
69097c478bd9Sstevel@tonic-gate 		/* append security attributes if any */
6910fa9e4066Sahrens 		if (aclp != NULL) {
691145916cd2Sjpk 			(void) append_secattr(&secinfo, &len, acl_cnt(aclp),
691245916cd2Sjpk 			    acl_totext(aclp, ACL_APPEND_ID | ACL_COMPACT_FMT),
691345916cd2Sjpk 			    (acl_type(aclp) == ACLENT_T) ? UFSD_ACL : ACE_ACL);
691445916cd2Sjpk 		}
691545916cd2Sjpk 
691645916cd2Sjpk 		if (Tflag) {
691745916cd2Sjpk 			/* append Trusted Extensions extended attributes */
691845916cd2Sjpk 			append_ext_attr(shortname, &secinfo, &len);
691945916cd2Sjpk 			(void) write_ancillary(&dblock, secinfo, len, ACL_HDR);
692045916cd2Sjpk 
692145916cd2Sjpk 		} else if (aclp != NULL) {
69227c478bd9Sstevel@tonic-gate 			(void) write_ancillary(&dblock, secinfo, len, ACL_HDR);
69237c478bd9Sstevel@tonic-gate 		}
69247c478bd9Sstevel@tonic-gate 	}
69257c478bd9Sstevel@tonic-gate 	return (0);
69267c478bd9Sstevel@tonic-gate }
69277c478bd9Sstevel@tonic-gate 
69287c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
69297c478bd9Sstevel@tonic-gate static int
69307c478bd9Sstevel@tonic-gate put_xattr_hdr(char *longname, char *shortname, char *prefix,
69317c478bd9Sstevel@tonic-gate 	int typeflag, int filetype, struct linkbuf *lp)
69327c478bd9Sstevel@tonic-gate {
69337c478bd9Sstevel@tonic-gate 	char *lname = NULL;
69347c478bd9Sstevel@tonic-gate 	char *sname = NULL;
69357c478bd9Sstevel@tonic-gate 	int  error = 0;
69367c478bd9Sstevel@tonic-gate 	static char *attrbuf = NULL;
69377c478bd9Sstevel@tonic-gate 	int attrlen;
69387c478bd9Sstevel@tonic-gate 
69397c478bd9Sstevel@tonic-gate 	lname = malloc(sizeof (char) * strlen("/dev/null") + 1 +
69407c478bd9Sstevel@tonic-gate 	    strlen(shortname) + strlen(".hdr") + 1);
69417c478bd9Sstevel@tonic-gate 
69427c478bd9Sstevel@tonic-gate 	if (lname == NULL) {
69437c478bd9Sstevel@tonic-gate 		fatal(gettext("Out of Memory."));
69447c478bd9Sstevel@tonic-gate 	}
69457c478bd9Sstevel@tonic-gate 	sname = malloc(sizeof (char) * strlen(shortname) +
69467c478bd9Sstevel@tonic-gate 	    strlen(".hdr"));
69477c478bd9Sstevel@tonic-gate 	if (sname == NULL) {
69487c478bd9Sstevel@tonic-gate 		fatal(gettext("Out of Memory."));
69497c478bd9Sstevel@tonic-gate 	}
69507c478bd9Sstevel@tonic-gate 
69517c478bd9Sstevel@tonic-gate 	(void) sprintf(sname, "%s.hdr", shortname);
69527c478bd9Sstevel@tonic-gate 	(void) sprintf(lname, "/dev/null/%s", sname);
69537c478bd9Sstevel@tonic-gate 
69547c478bd9Sstevel@tonic-gate 	if (strlcpy(dblock.dbuf.name, lname, sizeof (dblock.dbuf.name)) >=
69557c478bd9Sstevel@tonic-gate 	    sizeof (dblock.dbuf.name)) {
69567c478bd9Sstevel@tonic-gate 		fatal(gettext(
69577c478bd9Sstevel@tonic-gate 		    "Buffer overflow writing extended attribute file name"));
69587c478bd9Sstevel@tonic-gate 	}
69597c478bd9Sstevel@tonic-gate 
69607c478bd9Sstevel@tonic-gate 	/*
69617c478bd9Sstevel@tonic-gate 	 * dump extended attr lookup info
69627c478bd9Sstevel@tonic-gate 	 */
69637c478bd9Sstevel@tonic-gate 	prepare_xattr(&attrbuf, longname, shortname, typeflag, lp, &attrlen);
69647c478bd9Sstevel@tonic-gate 	write_ancillary(&dblock, attrbuf, attrlen, _XATTR_HDRTYPE);
69657c478bd9Sstevel@tonic-gate 
69667c478bd9Sstevel@tonic-gate 	(void) sprintf(lname, "/dev/null/%s", shortname);
69677c478bd9Sstevel@tonic-gate 	(void) strncpy(dblock.dbuf.name, sname, NAMSIZ);
69687c478bd9Sstevel@tonic-gate 
69697c478bd9Sstevel@tonic-gate 	/*
69707c478bd9Sstevel@tonic-gate 	 * Set up filename for attribute
69717c478bd9Sstevel@tonic-gate 	 */
69727c478bd9Sstevel@tonic-gate 
69737c478bd9Sstevel@tonic-gate 	error = build_dblock(lname, tchar, '0', filetype,
69747c478bd9Sstevel@tonic-gate 	    &stbuf, stbuf.st_dev, prefix);
69757c478bd9Sstevel@tonic-gate 	free(lname);
69767c478bd9Sstevel@tonic-gate 	free(sname);
69777c478bd9Sstevel@tonic-gate 
69787c478bd9Sstevel@tonic-gate 	return (error);
69797c478bd9Sstevel@tonic-gate }
69807c478bd9Sstevel@tonic-gate #endif
69817c478bd9Sstevel@tonic-gate 
69827c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
69837c478bd9Sstevel@tonic-gate static int
69847c478bd9Sstevel@tonic-gate read_xattr_hdr()
69857c478bd9Sstevel@tonic-gate {
69867c478bd9Sstevel@tonic-gate 	char		buf[TBLOCK];
69877c478bd9Sstevel@tonic-gate 	blkcnt_t	blocks;
69887c478bd9Sstevel@tonic-gate 	char		*tp;
69897c478bd9Sstevel@tonic-gate 	off_t		bytes;
69907c478bd9Sstevel@tonic-gate 	int		comp_len, link_len;
69917c478bd9Sstevel@tonic-gate 	int		namelen;
69927c478bd9Sstevel@tonic-gate 
69937c478bd9Sstevel@tonic-gate 
69947c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.typeflag != _XATTR_HDRTYPE)
69957c478bd9Sstevel@tonic-gate 		return (1);
69967c478bd9Sstevel@tonic-gate 
69977c478bd9Sstevel@tonic-gate 	bytes = stbuf.st_size;
69987c478bd9Sstevel@tonic-gate 	if ((xattrhead = calloc(1, (int)bytes)) == NULL) {
69997c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
70007c478bd9Sstevel@tonic-gate 		    "Insufficient memory for extended attribute\n"));
70017c478bd9Sstevel@tonic-gate 		return (1);
70027c478bd9Sstevel@tonic-gate 	}
70037c478bd9Sstevel@tonic-gate 
70047c478bd9Sstevel@tonic-gate 	tp = (char *)xattrhead;
70057c478bd9Sstevel@tonic-gate 	blocks = TBLOCKS(bytes);
70067c478bd9Sstevel@tonic-gate 	while (blocks-- > 0) {
70077c478bd9Sstevel@tonic-gate 		readtape(buf);
70087c478bd9Sstevel@tonic-gate 		if (bytes <= TBLOCK) {
70097c478bd9Sstevel@tonic-gate 			(void) memcpy(tp, buf, (size_t)bytes);
70107c478bd9Sstevel@tonic-gate 			break;
70117c478bd9Sstevel@tonic-gate 		} else {
70127c478bd9Sstevel@tonic-gate 			(void) memcpy(tp, buf, TBLOCK);
70137c478bd9Sstevel@tonic-gate 			tp += TBLOCK;
70147c478bd9Sstevel@tonic-gate 		}
70157c478bd9Sstevel@tonic-gate 		bytes -= TBLOCK;
70167c478bd9Sstevel@tonic-gate 	}
70177c478bd9Sstevel@tonic-gate 
70187c478bd9Sstevel@tonic-gate 	/*
70197c478bd9Sstevel@tonic-gate 	 * Validate that we can handle header format
70207c478bd9Sstevel@tonic-gate 	 */
70217c478bd9Sstevel@tonic-gate 	if (strcmp(xattrhead->h_version, XATTR_ARCH_VERS) != 0) {
70227c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
70237c478bd9Sstevel@tonic-gate 		    gettext("Unknown extended attribute format encountered\n"));
70247c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
70257c478bd9Sstevel@tonic-gate 		    gettext("Disabling extended attribute parsing\n"));
70267c478bd9Sstevel@tonic-gate 		xattrbadhead = 1;
70277c478bd9Sstevel@tonic-gate 		return (0);
70287c478bd9Sstevel@tonic-gate 	}
70297c478bd9Sstevel@tonic-gate 	(void) sscanf(xattrhead->h_component_len, "%10d", &comp_len);
70307c478bd9Sstevel@tonic-gate 	(void) sscanf(xattrhead->h_link_component_len,	"%10d", &link_len);
70317c478bd9Sstevel@tonic-gate 	xattrp = (struct xattr_buf *)(((char *)xattrhead) +
70327c478bd9Sstevel@tonic-gate 	    sizeof (struct xattr_hdr));
70337c478bd9Sstevel@tonic-gate 	(void) sscanf(xattrp->h_namesz, "%7d", &namelen);
70347c478bd9Sstevel@tonic-gate 	if (link_len > 0)
70357c478bd9Sstevel@tonic-gate 		xattr_linkp = (struct xattr_buf *)
70367c478bd9Sstevel@tonic-gate 		    ((int)xattrp + (int)comp_len);
70377c478bd9Sstevel@tonic-gate 	else
70387c478bd9Sstevel@tonic-gate 		xattr_linkp = NULL;
70397c478bd9Sstevel@tonic-gate 
70407c478bd9Sstevel@tonic-gate 	xattraname = xattrp->h_names + strlen(xattrp->h_names) + 1;
70417c478bd9Sstevel@tonic-gate 	if (xattr_linkp) {
70427c478bd9Sstevel@tonic-gate 		xattr_linkaname = xattr_linkp->h_names +
70437c478bd9Sstevel@tonic-gate 		    strlen(xattr_linkp->h_names) + 1;
70447c478bd9Sstevel@tonic-gate 	} else {
70457c478bd9Sstevel@tonic-gate 		xattr_linkaname = NULL;
70467c478bd9Sstevel@tonic-gate 	}
7047d2443e76Smarks 
70487c478bd9Sstevel@tonic-gate 	return (0);
70497c478bd9Sstevel@tonic-gate }
70507c478bd9Sstevel@tonic-gate #else
70517c478bd9Sstevel@tonic-gate static int
70527c478bd9Sstevel@tonic-gate read_xattr_hdr()
70537c478bd9Sstevel@tonic-gate {
70547c478bd9Sstevel@tonic-gate 	return (0);
70557c478bd9Sstevel@tonic-gate }
70567c478bd9Sstevel@tonic-gate #endif
70577c478bd9Sstevel@tonic-gate 
70587c478bd9Sstevel@tonic-gate /*
70597c478bd9Sstevel@tonic-gate  * skip over extra slashes in string.
70607c478bd9Sstevel@tonic-gate  *
70617c478bd9Sstevel@tonic-gate  * For example:
70627c478bd9Sstevel@tonic-gate  * /usr/tmp/////
70637c478bd9Sstevel@tonic-gate  *
70647c478bd9Sstevel@tonic-gate  * would return pointer at
70657c478bd9Sstevel@tonic-gate  * /usr/tmp/////
70667c478bd9Sstevel@tonic-gate  *         ^
70677c478bd9Sstevel@tonic-gate  */
70687c478bd9Sstevel@tonic-gate static char *
70697c478bd9Sstevel@tonic-gate skipslashes(char *string, char *start)
70707c478bd9Sstevel@tonic-gate {
70717c478bd9Sstevel@tonic-gate 	while ((string > start) && *(string - 1) == '/') {
70727c478bd9Sstevel@tonic-gate 		string--;
70737c478bd9Sstevel@tonic-gate 	}
70747c478bd9Sstevel@tonic-gate 
70757c478bd9Sstevel@tonic-gate 	return (string);
70767c478bd9Sstevel@tonic-gate }
70777c478bd9Sstevel@tonic-gate 
70787c478bd9Sstevel@tonic-gate /*
70797c478bd9Sstevel@tonic-gate  * Return the parent directory of a given path.
70807c478bd9Sstevel@tonic-gate  *
70817c478bd9Sstevel@tonic-gate  * Examples:
70827c478bd9Sstevel@tonic-gate  * /usr/tmp return /usr
70837c478bd9Sstevel@tonic-gate  * /usr/tmp/file return /usr/tmp
70847c478bd9Sstevel@tonic-gate  * /  returns .
70857c478bd9Sstevel@tonic-gate  * /usr returns /
70867c478bd9Sstevel@tonic-gate  * file returns .
70877c478bd9Sstevel@tonic-gate  *
70887c478bd9Sstevel@tonic-gate  * dir is assumed to be at least as big as path.
70897c478bd9Sstevel@tonic-gate  */
70907c478bd9Sstevel@tonic-gate static void
70917c478bd9Sstevel@tonic-gate get_parent(char *path, char *dir)
70927c478bd9Sstevel@tonic-gate {
70937c478bd9Sstevel@tonic-gate 	char *s;
70947c478bd9Sstevel@tonic-gate 	char tmpdir[PATH_MAX + 1];
70957c478bd9Sstevel@tonic-gate 
70967c478bd9Sstevel@tonic-gate 	if (strlen(path) > PATH_MAX) {
70977c478bd9Sstevel@tonic-gate 		fatal(gettext("pathname is too long"));
70987c478bd9Sstevel@tonic-gate 	}
70997c478bd9Sstevel@tonic-gate 	(void) strcpy(tmpdir, path);
71007c478bd9Sstevel@tonic-gate 	chop_endslashes(tmpdir);
71017c478bd9Sstevel@tonic-gate 
71027c478bd9Sstevel@tonic-gate 	if ((s = strrchr(tmpdir, '/')) == NULL) {
71037c478bd9Sstevel@tonic-gate 		(void) strcpy(dir, ".");
71047c478bd9Sstevel@tonic-gate 	} else {
71057c478bd9Sstevel@tonic-gate 		s = skipslashes(s, tmpdir);
71067c478bd9Sstevel@tonic-gate 		*s = '\0';
71077c478bd9Sstevel@tonic-gate 		if (s == tmpdir)
71087c478bd9Sstevel@tonic-gate 			(void) strcpy(dir, "/");
71097c478bd9Sstevel@tonic-gate 		else
71107c478bd9Sstevel@tonic-gate 			(void) strcpy(dir, tmpdir);
71117c478bd9Sstevel@tonic-gate 	}
71127c478bd9Sstevel@tonic-gate }
71137c478bd9Sstevel@tonic-gate 
71147c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
71157c478bd9Sstevel@tonic-gate static char *
71167c478bd9Sstevel@tonic-gate get_component(char *path)
71177c478bd9Sstevel@tonic-gate {
71187c478bd9Sstevel@tonic-gate 	char *ptr;
71197c478bd9Sstevel@tonic-gate 
71207c478bd9Sstevel@tonic-gate 	ptr = strrchr(path, '/');
71217c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
71227c478bd9Sstevel@tonic-gate 		return (path);
71237c478bd9Sstevel@tonic-gate 	} else {
71247c478bd9Sstevel@tonic-gate 		/*
71257c478bd9Sstevel@tonic-gate 		 * Handle trailing slash
71267c478bd9Sstevel@tonic-gate 		 */
71277c478bd9Sstevel@tonic-gate 		if (*(ptr + 1) == '\0')
71287c478bd9Sstevel@tonic-gate 			return (ptr);
71297c478bd9Sstevel@tonic-gate 		else
71307c478bd9Sstevel@tonic-gate 			return (ptr + 1);
71317c478bd9Sstevel@tonic-gate 	}
71327c478bd9Sstevel@tonic-gate }
71337c478bd9Sstevel@tonic-gate #else
71347c478bd9Sstevel@tonic-gate static char *
71357c478bd9Sstevel@tonic-gate get_component(char *path)
71367c478bd9Sstevel@tonic-gate {
71377c478bd9Sstevel@tonic-gate 	return (path);
71387c478bd9Sstevel@tonic-gate }
71397c478bd9Sstevel@tonic-gate #endif
71407c478bd9Sstevel@tonic-gate 
71417c478bd9Sstevel@tonic-gate static int
71427c478bd9Sstevel@tonic-gate retry_attrdir_open(char *name)
71437c478bd9Sstevel@tonic-gate {
71447c478bd9Sstevel@tonic-gate 	int dirfd = -1;
71457c478bd9Sstevel@tonic-gate 	struct timeval times[2];
71467c478bd9Sstevel@tonic-gate 	mode_t newmode;
71477c478bd9Sstevel@tonic-gate 	struct stat parentstat;
7148d2443e76Smarks 	acl_t *aclp = NULL;
7149d2443e76Smarks 	int error;
71507c478bd9Sstevel@tonic-gate 
71517c478bd9Sstevel@tonic-gate 	/*
71527c478bd9Sstevel@tonic-gate 	 * We couldn't get to attrdir. See if its
71537c478bd9Sstevel@tonic-gate 	 * just a mode problem on the parent file.
71547c478bd9Sstevel@tonic-gate 	 * for example: a mode such as r-xr--r--
71557c478bd9Sstevel@tonic-gate 	 * won't let us create an attribute dir
71567c478bd9Sstevel@tonic-gate 	 * if it doesn't already exist.
7157d2443e76Smarks 	 *
7158d2443e76Smarks 	 * If file has a non-trivial ACL, then save it
7159d2443e76Smarks 	 * off so that we can place it back on after doing
7160d2443e76Smarks 	 * chmod's.
71617c478bd9Sstevel@tonic-gate 	 */
71627c478bd9Sstevel@tonic-gate 
71637c478bd9Sstevel@tonic-gate 	if (stat(name, &parentstat) == -1) {
7164d2443e76Smarks 		(void) fprintf(stderr, gettext("tar: cannot stat file %s %s\n"),
71657c478bd9Sstevel@tonic-gate 		    name, strerror(errno));
7166d2443e76Smarks 			return (-1);
71677c478bd9Sstevel@tonic-gate 	}
7168d2443e76Smarks 	if ((error = acl_get(name, ACL_NO_TRIVIAL, &aclp)) != 0) {
7169d2443e76Smarks 		(void) fprintf(stderr, gettext("tar: failed to retrieve ACL on"
7170d2443e76Smarks 		    " %s %s\n"), name, strerror(errno));
7171d2443e76Smarks 			return (-1);
7172d2443e76Smarks 	}
7173d2443e76Smarks 
71747c478bd9Sstevel@tonic-gate 	newmode = S_IWUSR | parentstat.st_mode;
71757c478bd9Sstevel@tonic-gate 	if (chmod(name, newmode) == -1) {
71767c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
7177d2443e76Smarks 		    gettext("tar: cannot chmod file %s to %o %s\n"),
71787c478bd9Sstevel@tonic-gate 		    name, newmode, strerror(errno));
7179d2443e76Smarks 		if (aclp)
7180d2443e76Smarks 			acl_free(aclp);
7181d2443e76Smarks 		return (-1);
71827c478bd9Sstevel@tonic-gate 	}
71837c478bd9Sstevel@tonic-gate 
71847c478bd9Sstevel@tonic-gate 	dirfd = attropen(name, ".", O_RDONLY);
7185d2443e76Smarks 
7186d2443e76Smarks 	/*
7187d2443e76Smarks 	 * Don't print error message if attropen() failed,
7188d2443e76Smarks 	 * caller will print message.
7189d2443e76Smarks 	 */
71907c478bd9Sstevel@tonic-gate 
71917c478bd9Sstevel@tonic-gate 	/*
71927c478bd9Sstevel@tonic-gate 	 * Put mode back to original
71937c478bd9Sstevel@tonic-gate 	 */
7194d2443e76Smarks 	if (chmod(name, parentstat.st_mode) == -1) {
7195d2443e76Smarks 		(void) fprintf(stderr,
7196d2443e76Smarks 		    gettext("tar: cannot chmod file %s to %o %s\n"),
7197d2443e76Smarks 		    name, newmode, strerror(errno));
7198d2443e76Smarks 	}
7199d2443e76Smarks 
7200d2443e76Smarks 	if (aclp) {
7201d2443e76Smarks 		error = acl_set(name, aclp);
7202d2443e76Smarks 		if (error) {
7203d2443e76Smarks 			(void) fprintf(stderr,
7204d2443e76Smarks 			    gettext("tar: %s: failed to set acl entries\n"),
7205d2443e76Smarks 			    name);
7206d2443e76Smarks 		}
7207d2443e76Smarks 		acl_free(aclp);
7208d2443e76Smarks 	}
72097c478bd9Sstevel@tonic-gate 
72107c478bd9Sstevel@tonic-gate 	/*
72117c478bd9Sstevel@tonic-gate 	 * Put back time stamps
72127c478bd9Sstevel@tonic-gate 	 */
72137c478bd9Sstevel@tonic-gate 
72147c478bd9Sstevel@tonic-gate 	times[0].tv_sec = parentstat.st_atime;
72157c478bd9Sstevel@tonic-gate 	times[0].tv_usec = 0;
72167c478bd9Sstevel@tonic-gate 	times[1].tv_sec = parentstat.st_mtime;
72177c478bd9Sstevel@tonic-gate 	times[1].tv_usec = 0;
72187c478bd9Sstevel@tonic-gate 	(void) utimes(name, times);
72197c478bd9Sstevel@tonic-gate 
72207c478bd9Sstevel@tonic-gate 	return (dirfd);
72217c478bd9Sstevel@tonic-gate }
72227c478bd9Sstevel@tonic-gate 
72237c478bd9Sstevel@tonic-gate #if !defined(O_XATTR)
72247c478bd9Sstevel@tonic-gate static int
72257c478bd9Sstevel@tonic-gate openat64(int fd, const char *name, int oflag, mode_t cmode)
72267c478bd9Sstevel@tonic-gate {
72277c478bd9Sstevel@tonic-gate 	return (open64(name, oflag, cmode));
72287c478bd9Sstevel@tonic-gate }
72297c478bd9Sstevel@tonic-gate 
72307c478bd9Sstevel@tonic-gate static int
72317c478bd9Sstevel@tonic-gate openat(int fd, const char *name, int oflag, mode_t cmode)
72327c478bd9Sstevel@tonic-gate {
72337c478bd9Sstevel@tonic-gate 	return (open(name, oflag, cmode));
72347c478bd9Sstevel@tonic-gate }
72357c478bd9Sstevel@tonic-gate 
72367c478bd9Sstevel@tonic-gate static int
72377c478bd9Sstevel@tonic-gate fchownat(int fd, const char *name, uid_t owner, gid_t group, int flag)
72387c478bd9Sstevel@tonic-gate {
72397c478bd9Sstevel@tonic-gate 	if (flag == AT_SYMLINK_NOFOLLOW)
72407c478bd9Sstevel@tonic-gate 		return (lchown(name, owner, group));
72417c478bd9Sstevel@tonic-gate 	else
72427c478bd9Sstevel@tonic-gate 		return (chown(name, owner, group));
72437c478bd9Sstevel@tonic-gate }
72447c478bd9Sstevel@tonic-gate 
72457c478bd9Sstevel@tonic-gate static int
72467c478bd9Sstevel@tonic-gate renameat(int fromfd, char *old, int tofd, char *new)
72477c478bd9Sstevel@tonic-gate {
72487c478bd9Sstevel@tonic-gate 	return (rename(old, new));
72497c478bd9Sstevel@tonic-gate }
72507c478bd9Sstevel@tonic-gate 
72517c478bd9Sstevel@tonic-gate static int
72527c478bd9Sstevel@tonic-gate futimesat(int fd, char *path, struct timeval times[2])
72537c478bd9Sstevel@tonic-gate {
72547c478bd9Sstevel@tonic-gate 	return (utimes(path, times));
72557c478bd9Sstevel@tonic-gate }
72567c478bd9Sstevel@tonic-gate 
72577c478bd9Sstevel@tonic-gate static int
72587c478bd9Sstevel@tonic-gate unlinkat(int dirfd, char *path, int flag)
72597c478bd9Sstevel@tonic-gate {
72607c478bd9Sstevel@tonic-gate 	if (flag == AT_REMOVEDIR)
72617c478bd9Sstevel@tonic-gate 		return (rmdir(path));
72627c478bd9Sstevel@tonic-gate 	else
72637c478bd9Sstevel@tonic-gate 		return (unlink(path));
72647c478bd9Sstevel@tonic-gate }
72657c478bd9Sstevel@tonic-gate 
72667c478bd9Sstevel@tonic-gate static int
72677c478bd9Sstevel@tonic-gate fstatat(int fd, char *path, struct stat *buf, int flag)
72687c478bd9Sstevel@tonic-gate {
72697c478bd9Sstevel@tonic-gate 	if (flag == AT_SYMLINK_NOFOLLOW)
72707c478bd9Sstevel@tonic-gate 		return (lstat(path, buf));
72717c478bd9Sstevel@tonic-gate 	else
72727c478bd9Sstevel@tonic-gate 		return (stat(path, buf));
72737c478bd9Sstevel@tonic-gate }
72747c478bd9Sstevel@tonic-gate 
72757c478bd9Sstevel@tonic-gate static int
72767c478bd9Sstevel@tonic-gate attropen(char *file, char *attr, int omode, mode_t cmode)
72777c478bd9Sstevel@tonic-gate {
72787c478bd9Sstevel@tonic-gate 	errno = ENOTSUP;
72797c478bd9Sstevel@tonic-gate 	return (-1);
72807c478bd9Sstevel@tonic-gate }
72817c478bd9Sstevel@tonic-gate #endif
72827c478bd9Sstevel@tonic-gate 
72837c478bd9Sstevel@tonic-gate static void
72847c478bd9Sstevel@tonic-gate chop_endslashes(char *path)
72857c478bd9Sstevel@tonic-gate {
72867c478bd9Sstevel@tonic-gate 	char *end, *ptr;
72877c478bd9Sstevel@tonic-gate 
72887c478bd9Sstevel@tonic-gate 	/*
72897c478bd9Sstevel@tonic-gate 	 * Chop of slashes, but not if all we have is slashes
72907c478bd9Sstevel@tonic-gate 	 * for example: ////
72917c478bd9Sstevel@tonic-gate 	 * should make no changes, otherwise it will screw up
72927c478bd9Sstevel@tonic-gate 	 * checkdir
72937c478bd9Sstevel@tonic-gate 	 */
72947c478bd9Sstevel@tonic-gate 	end = &path[strlen(path) -1];
72957c478bd9Sstevel@tonic-gate 	if (*end == '/' && end != path) {
72967c478bd9Sstevel@tonic-gate 		ptr = skipslashes(end, path);
72977c478bd9Sstevel@tonic-gate 		if (ptr != NULL && ptr != path) {
72987c478bd9Sstevel@tonic-gate 			*ptr = '\0';
72997c478bd9Sstevel@tonic-gate 		}
73007c478bd9Sstevel@tonic-gate 	}
73017c478bd9Sstevel@tonic-gate }
730245916cd2Sjpk /* Trusted Extensions */
730345916cd2Sjpk 
730445916cd2Sjpk /*
730545916cd2Sjpk  * append_ext_attr():
730645916cd2Sjpk  *
730745916cd2Sjpk  * Append extended attributes and other information into the buffer
730845916cd2Sjpk  * that gets written to the ancillary file.
730945916cd2Sjpk  *
731045916cd2Sjpk  * With option 'T', we create a tarfile which
731145916cd2Sjpk  * has an ancillary file each corresponding archived file.
731245916cd2Sjpk  * Each ancillary file contains 1 or more of the
731345916cd2Sjpk  * following attributes:
731445916cd2Sjpk  *
731545916cd2Sjpk  *	attribute type        attribute		process procedure
731645916cd2Sjpk  *	----------------      ----------------  --------------------------
731745916cd2Sjpk  *   	DIR_TYPE       = 'D'   directory flag	append if a directory
731845916cd2Sjpk  *    	LBL_TYPE       = 'L'   SL[IL] or SL	append ascii label
731945916cd2Sjpk  *
732045916cd2Sjpk  *
732145916cd2Sjpk  */
732245916cd2Sjpk static void
732345916cd2Sjpk append_ext_attr(char *shortname, char **secinfo, int *len)
732445916cd2Sjpk {
732545916cd2Sjpk 	bslabel_t	b_slabel;	/* binary sensitvity label */
732645916cd2Sjpk 	char		*ascii = NULL;	/* ascii label */
732745916cd2Sjpk 
732845916cd2Sjpk 	/*
732945916cd2Sjpk 	 * For each attribute type, append it if it is
733045916cd2Sjpk 	 * relevant to the file type.
733145916cd2Sjpk 	 */
733245916cd2Sjpk 
733345916cd2Sjpk 	/*
733445916cd2Sjpk 	 * For attribute type DIR_TYPE,
733545916cd2Sjpk 	 * append it to the following file type:
733645916cd2Sjpk 	 *
733745916cd2Sjpk 	 *	S_IFDIR: directories
733845916cd2Sjpk 	 */
733945916cd2Sjpk 
734045916cd2Sjpk 	/*
734145916cd2Sjpk 	 * For attribute type LBL_TYPE,
734245916cd2Sjpk 	 * append it to the following file type:
734345916cd2Sjpk 	 *
734445916cd2Sjpk 	 *	S_IFDIR: directories (including mld, sld)
734545916cd2Sjpk 	 *	S_IFLNK: symbolic link
734645916cd2Sjpk 	 *	S_IFREG: regular file but not hard link
734745916cd2Sjpk 	 *	S_IFIFO: FIFO file but not hard link
734845916cd2Sjpk 	 *	S_IFCHR: char special file but not hard link
734945916cd2Sjpk 	 *	S_IFBLK: block special file but not hard link
735045916cd2Sjpk 	 */
735145916cd2Sjpk 	switch (stbuf.st_mode & S_IFMT) {
735245916cd2Sjpk 
735345916cd2Sjpk 	case S_IFDIR:
735445916cd2Sjpk 
735545916cd2Sjpk 		/*
735645916cd2Sjpk 		 * append DIR_TYPE
735745916cd2Sjpk 		 */
735845916cd2Sjpk 		(void) append_secattr(secinfo, len, 1,
735945916cd2Sjpk 		    "\0", DIR_TYPE);
736045916cd2Sjpk 
736145916cd2Sjpk 		/*
736245916cd2Sjpk 		 * Get and append attribute types LBL_TYPE.
736345916cd2Sjpk 		 * For directories, LBL_TYPE contains SL.
736445916cd2Sjpk 		 */
736545916cd2Sjpk 		/* get binary sensitivity label */
736645916cd2Sjpk 		if (getlabel(shortname, &b_slabel) != 0) {
736745916cd2Sjpk 			(void) fprintf(stderr,
736845916cd2Sjpk 			    gettext("tar: can't get sensitvity label for "
736945916cd2Sjpk 			    " %s, getlabel() error: %s\n"),
737045916cd2Sjpk 			    shortname, strerror(errno));
737145916cd2Sjpk 		} else {
737245916cd2Sjpk 			/* get ascii SL */
737345916cd2Sjpk 			if (bsltos(&b_slabel, &ascii,
737445916cd2Sjpk 			    0, 0) <= 0) {
737545916cd2Sjpk 				(void) fprintf(stderr,
737645916cd2Sjpk 				    gettext("tar: can't get ascii SL for"
737745916cd2Sjpk 				    " %s\n"), shortname);
737845916cd2Sjpk 			} else {
737945916cd2Sjpk 				/* append LBL_TYPE */
738045916cd2Sjpk 				(void) append_secattr(secinfo, len,
738145916cd2Sjpk 				    strlen(ascii) + 1, ascii,
738245916cd2Sjpk 				    LBL_TYPE);
738345916cd2Sjpk 
738445916cd2Sjpk 				/* free storage */
738545916cd2Sjpk 				if (ascii != NULL) {
738645916cd2Sjpk 					free(ascii);
738745916cd2Sjpk 					ascii = (char *)0;
738845916cd2Sjpk 				}
738945916cd2Sjpk 			}
739045916cd2Sjpk 
739145916cd2Sjpk 		}
739245916cd2Sjpk 		break;
739345916cd2Sjpk 
739445916cd2Sjpk 	case S_IFLNK:
739545916cd2Sjpk 	case S_IFREG:
739645916cd2Sjpk 	case S_IFIFO:
739745916cd2Sjpk 	case S_IFCHR:
739845916cd2Sjpk 	case S_IFBLK:
739945916cd2Sjpk 
740045916cd2Sjpk 		/* get binary sensitivity label */
740145916cd2Sjpk 		if (getlabel(shortname, &b_slabel) != 0) {
740245916cd2Sjpk 			(void) fprintf(stderr,
740345916cd2Sjpk 			    gettext("tar: can't get sensitivty label for %s, "
740445916cd2Sjpk 			    "getlabel() error: %s\n"),
740545916cd2Sjpk 			    shortname, strerror(errno));
740645916cd2Sjpk 		} else {
740745916cd2Sjpk 			/* get ascii IL[SL] */
740845916cd2Sjpk 			if (bsltos(&b_slabel, &ascii, 0, 0) <= 0) {
740945916cd2Sjpk 				(void) fprintf(stderr,
741045916cd2Sjpk 				    gettext("tar: can't translate sensitivity "
741145916cd2Sjpk 				    " label for %s\n"), shortname);
741245916cd2Sjpk 			} else {
741345916cd2Sjpk 				char *cmw_label;
741445916cd2Sjpk 				size_t  cmw_length;
741545916cd2Sjpk 
741645916cd2Sjpk 				cmw_length = strlen("ADMIN_LOW [] ") +
741745916cd2Sjpk 				    strlen(ascii);
741845916cd2Sjpk 				if ((cmw_label = malloc(cmw_length)) == NULL) {
741945916cd2Sjpk 					(void) fprintf(stderr, gettext(
742045916cd2Sjpk 					    "Insufficient memory for label\n"));
742145916cd2Sjpk 					exit(1);
742245916cd2Sjpk 				}
742345916cd2Sjpk 				/* append LBL_TYPE */
742445916cd2Sjpk 				(void) snprintf(cmw_label, cmw_length,
742545916cd2Sjpk 				    "ADMIN_LOW [%s]", ascii);
742645916cd2Sjpk 				(void) append_secattr(secinfo, len,
742745916cd2Sjpk 				    strlen(cmw_label) + 1, cmw_label,
742845916cd2Sjpk 				    LBL_TYPE);
742945916cd2Sjpk 
743045916cd2Sjpk 				/* free storage */
743145916cd2Sjpk 				if (ascii != NULL) {
743245916cd2Sjpk 					free(cmw_label);
743345916cd2Sjpk 					free(ascii);
743445916cd2Sjpk 					ascii = (char *)0;
743545916cd2Sjpk 				}
743645916cd2Sjpk 			}
743745916cd2Sjpk 		}
743845916cd2Sjpk 		break;
743945916cd2Sjpk 
744045916cd2Sjpk 	default:
744145916cd2Sjpk 		break;
744245916cd2Sjpk 	} /* end switch for LBL_TYPE */
744345916cd2Sjpk 
744445916cd2Sjpk 
744545916cd2Sjpk 	/* DONE !! */
744645916cd2Sjpk 	return;
744745916cd2Sjpk 
744845916cd2Sjpk } /* end of append_ext_attr */
744945916cd2Sjpk 
745045916cd2Sjpk 
745145916cd2Sjpk /*
745245916cd2Sjpk  *	Name: extract_attr()
745345916cd2Sjpk  *
745445916cd2Sjpk  *	Description:
745545916cd2Sjpk  *		Process attributes from the ancillary file due to
745645916cd2Sjpk  *		the T option.
745745916cd2Sjpk  *
745845916cd2Sjpk  *	Call by doxtract() as part of the switch case structure.
745945916cd2Sjpk  *	Making this a separate routine because the nesting are too
746045916cd2Sjpk  *	deep in doxtract, thus, leaving very little space
746145916cd2Sjpk  *	on each line for instructions.
746245916cd2Sjpk  *
746345916cd2Sjpk  * With option 'T', we extract from a TS 8 or TS 2.5 ancillary file
746445916cd2Sjpk  *
746545916cd2Sjpk  * For option 'T', following are possible attributes in
746645916cd2Sjpk  * a TS 8 ancillary file: (NOTE: No IL support)
746745916cd2Sjpk  *
746845916cd2Sjpk  *	attribute type        attribute		process procedure
746945916cd2Sjpk  *	----------------      ----------------  -------------------------
747045916cd2Sjpk  *    #	LBL_TYPE       = 'L'   SL               construct binary label
747145916cd2Sjpk  *    #	APRIV_TYPE     = 'P'   allowed priv    	construct privileges
747245916cd2Sjpk  *    #	FPRIV_TYPE     = 'p'   forced priv	construct privileges
747345916cd2Sjpk  *    #	COMP_TYPE      = 'C'   path component	construct real path
747445916cd2Sjpk  *    #	DIR_TYPE       = 'D'   directory flag	note it is a directory
747545916cd2Sjpk  *    $	UFSD_ACL       = '1'   ACL data		construct ACL entries
747645916cd2Sjpk  *	ATTR_FLAG_TYPE = 'F'   file attr flags  construct binary flags
747745916cd2Sjpk  *	LK_COMP_TYPE   = 'K'   linked path comp construct linked real path
747845916cd2Sjpk  *
747945916cd2Sjpk  * note: # = attribute names common between TS 8 & TS 2.5 ancillary
748045916cd2Sjpk  *           files.
748145916cd2Sjpk  *       $ = ACL attribute is processed for the option 'p', it doesn't
748245916cd2Sjpk  *           need option 'T'.
748345916cd2Sjpk  *
748445916cd2Sjpk  * Trusted Extensions ignores APRIV_TYPE, FPRIV_TYPE, and ATTR_FLAG_TYPE
748545916cd2Sjpk  *
748645916cd2Sjpk  */
748745916cd2Sjpk static void
748845916cd2Sjpk extract_attr(char **file_ptr, struct sec_attr *attr)
748945916cd2Sjpk {
749045916cd2Sjpk 	int	reterr, err;
749145916cd2Sjpk 	char	*dummy_buf;	/* for attribute extract */
749245916cd2Sjpk 
749345916cd2Sjpk 	dummy_buf = attr->attr_info;
749445916cd2Sjpk 
749545916cd2Sjpk 	switch (attr->attr_type) {
749645916cd2Sjpk 
749745916cd2Sjpk 	case DIR_TYPE:
749845916cd2Sjpk 
749945916cd2Sjpk 		dir_flag++;
750045916cd2Sjpk 		break;
750145916cd2Sjpk 
750245916cd2Sjpk 	case LBL_TYPE:
750345916cd2Sjpk 
750445916cd2Sjpk 		/*
750545916cd2Sjpk 		 * LBL_TYPE is used to indicate SL for directory, and
750645916cd2Sjpk 		 * CMW label for other file types.
750745916cd2Sjpk 		 */
750845916cd2Sjpk 
750945916cd2Sjpk 		if (!dir_flag) { /* not directory */
751045916cd2Sjpk 			/* Skip over IL portion */
751145916cd2Sjpk 			char *sl_ptr = strchr(dummy_buf, '[');
751245916cd2Sjpk 
751345916cd2Sjpk 			if (sl_ptr == NULL)
751445916cd2Sjpk 				err = 0;
751545916cd2Sjpk 			else
751645916cd2Sjpk 				err = stobsl(sl_ptr, &bs_label,
751745916cd2Sjpk 				    NEW_LABEL, &reterr);
751845916cd2Sjpk 		} else { /* directory */
751945916cd2Sjpk 			err = stobsl(dummy_buf, &bs_label,
752045916cd2Sjpk 			    NEW_LABEL, &reterr);
752145916cd2Sjpk 		}
752245916cd2Sjpk 		if (err == 0) {
752345916cd2Sjpk 			(void) fprintf(stderr, gettext("tar: "
752445916cd2Sjpk 			    "can't convert %s to binary label\n"),
752545916cd2Sjpk 			    dummy_buf);
752645916cd2Sjpk 			bslundef(&bs_label);
752745916cd2Sjpk 		} else if (!blequal(&bs_label, &admin_low) &&
752845916cd2Sjpk 		    !blequal(&bs_label, &admin_high)) {
752945916cd2Sjpk 			bslabel_t *from_label;
753045916cd2Sjpk 			char *buf;
753145916cd2Sjpk 			char tempbuf[MAXPATHLEN];
753245916cd2Sjpk 
753345916cd2Sjpk 			if (*orig_namep != '/') {
753445916cd2Sjpk 				/* got relative linked to path */
753545916cd2Sjpk 				(void) getcwd(tempbuf, (sizeof (tempbuf)));
753645916cd2Sjpk 				(void) strncat(tempbuf, "/", MAXPATHLEN);
753745916cd2Sjpk 			} else
753845916cd2Sjpk 				*tempbuf = '\0';
753945916cd2Sjpk 
754045916cd2Sjpk 			buf = real_path;
754145916cd2Sjpk 			(void) strncat(tempbuf, orig_namep, MAXPATHLEN);
754245916cd2Sjpk 			from_label = getlabelbypath(tempbuf);
754345916cd2Sjpk 			if (from_label != NULL) {
754445916cd2Sjpk 				if (blequal(from_label, &admin_low)) {
754545916cd2Sjpk 					if ((getpathbylabel(tempbuf, buf,
754645916cd2Sjpk 					    MAXPATHLEN, &bs_label) == NULL)) {
754745916cd2Sjpk 						(void) fprintf(stderr,
754845916cd2Sjpk 						    gettext("tar: "
754945916cd2Sjpk 						"can't get zone root path for "
755045916cd2Sjpk 						"%s\n"), tempbuf);
755145916cd2Sjpk 					} else
755245916cd2Sjpk 						rpath_flag = 1;
755345916cd2Sjpk 				}
755445916cd2Sjpk 				free(from_label);
755545916cd2Sjpk 			}
755645916cd2Sjpk 		}
755745916cd2Sjpk 		break;
755845916cd2Sjpk 
755945916cd2Sjpk 	case COMP_TYPE:
756045916cd2Sjpk 
756145916cd2Sjpk 		rebuild_comp_path(dummy_buf, file_ptr);
756245916cd2Sjpk 		break;
756345916cd2Sjpk 
756445916cd2Sjpk 	case LK_COMP_TYPE:
756545916cd2Sjpk 
756645916cd2Sjpk 		if (rebuild_lk_comp_path(dummy_buf, file_ptr)
756745916cd2Sjpk 		    == 0) {
756845916cd2Sjpk 			lk_rpath_flag = 1;
756945916cd2Sjpk 		} else {
757045916cd2Sjpk 			(void) fprintf(stderr, gettext("tar: warning: link's "
757145916cd2Sjpk 			    "target pathname might be invalid.\n"));
757245916cd2Sjpk 			lk_rpath_flag = 0;
757345916cd2Sjpk 		}
757445916cd2Sjpk 		break;
757545916cd2Sjpk 	case APRIV_TYPE:
757645916cd2Sjpk 		ignored_aprivs++;
757745916cd2Sjpk 		break;
757845916cd2Sjpk 	case FPRIV_TYPE:
757945916cd2Sjpk 		ignored_fprivs++;
758045916cd2Sjpk 		break;
758145916cd2Sjpk 	case ATTR_FLAG_TYPE:
758245916cd2Sjpk 		ignored_fattrs++;
758345916cd2Sjpk 		break;
758445916cd2Sjpk 
758545916cd2Sjpk 	default:
758645916cd2Sjpk 
758745916cd2Sjpk 		break;
758845916cd2Sjpk 	}
758945916cd2Sjpk 
759045916cd2Sjpk 	/* done */
759145916cd2Sjpk 	return;
759245916cd2Sjpk 
759345916cd2Sjpk }	/* end extract_attr */
759445916cd2Sjpk 
759545916cd2Sjpk 
759645916cd2Sjpk 
759745916cd2Sjpk /*
759845916cd2Sjpk  *	Name:	rebuild_comp_path()
759945916cd2Sjpk  *
760045916cd2Sjpk  *	Description:
760145916cd2Sjpk  *		Take the string of components passed down by the calling
760245916cd2Sjpk  *		routine and parse the values and rebuild the path.
760345916cd2Sjpk  *		This routine no longer needs to produce a new real_path
760445916cd2Sjpk  *		string because it is produced when the 'L' LABEL_TYPE is
760545916cd2Sjpk  *		interpreted. So the only thing done here is to distinguish
760645916cd2Sjpk  *		between an SLD and an MLD entry. We only want one, so we
760745916cd2Sjpk  *		ignore the MLD entry by setting the mld_flag.
760845916cd2Sjpk  *
760945916cd2Sjpk  *	return value:
761045916cd2Sjpk  *		none
761145916cd2Sjpk  */
761245916cd2Sjpk static void
761345916cd2Sjpk rebuild_comp_path(char *str, char **namep)
761445916cd2Sjpk {
761545916cd2Sjpk 	char		*cp;
761645916cd2Sjpk 
761745916cd2Sjpk 	while (*str != '\0') {
761845916cd2Sjpk 
761945916cd2Sjpk 		switch (*str) {
762045916cd2Sjpk 
762145916cd2Sjpk 		case MLD_TYPE:
762245916cd2Sjpk 
762345916cd2Sjpk 			str++;
762445916cd2Sjpk 			if ((cp = strstr(str, ";;")) != NULL) {
762545916cd2Sjpk 				*cp = '\0';
762645916cd2Sjpk 				str = cp + 2;
762745916cd2Sjpk 				*cp = ';';
762845916cd2Sjpk 			}
762945916cd2Sjpk 			mld_flag = 1;
763045916cd2Sjpk 			break;
763145916cd2Sjpk 
763245916cd2Sjpk 		case SLD_TYPE:
763345916cd2Sjpk 
763445916cd2Sjpk 			str++;
763545916cd2Sjpk 			if ((cp = strstr(str, ";;")) != NULL) {
763645916cd2Sjpk 				*cp = '\0';
763745916cd2Sjpk 				str = cp + 2;
763845916cd2Sjpk 				*cp = ';';
763945916cd2Sjpk 			}
764045916cd2Sjpk 			mld_flag = 0;
764145916cd2Sjpk 			break;
764245916cd2Sjpk 
764345916cd2Sjpk 		case PATH_TYPE:
764445916cd2Sjpk 
764545916cd2Sjpk 			str++;
764645916cd2Sjpk 			if ((cp = strstr(str, ";;")) != NULL) {
764745916cd2Sjpk 				*cp = '\0';
764845916cd2Sjpk 				str = cp + 2;
764945916cd2Sjpk 				*cp = ';';
765045916cd2Sjpk 			}
765145916cd2Sjpk 			break;
765245916cd2Sjpk 		}
765345916cd2Sjpk 	}
765445916cd2Sjpk 	if (rpath_flag)
765545916cd2Sjpk 		*namep = real_path;
765645916cd2Sjpk 	return;
765745916cd2Sjpk 
765845916cd2Sjpk } /* end rebuild_comp_path() */
765945916cd2Sjpk 
766045916cd2Sjpk /*
766145916cd2Sjpk  *	Name:	rebuild_lk_comp_path()
766245916cd2Sjpk  *
766345916cd2Sjpk  *	Description:
766445916cd2Sjpk  *		Take the string of components passed down by the calling
766545916cd2Sjpk  *		routine and parse the values and rebuild the path.
766645916cd2Sjpk  *
766745916cd2Sjpk  *	return value:
766845916cd2Sjpk  *		0 = succeeded
766945916cd2Sjpk  *		-1 = failed
767045916cd2Sjpk  */
767145916cd2Sjpk static int
767245916cd2Sjpk rebuild_lk_comp_path(char *str, char **namep)
767345916cd2Sjpk {
767445916cd2Sjpk 	char		*cp;
767545916cd2Sjpk 	int		reterr;
767645916cd2Sjpk 	bslabel_t	bslabel;
767745916cd2Sjpk 	char		*buf;
767845916cd2Sjpk 	char		pbuf[MAXPATHLEN];
767945916cd2Sjpk 	char		*ptr1, *ptr2;
768045916cd2Sjpk 	int		plen;
768145916cd2Sjpk 	int		use_pbuf;
768245916cd2Sjpk 	char		tempbuf[MAXPATHLEN];
768345916cd2Sjpk 	int		mismatch;
768445916cd2Sjpk 	bslabel_t	*from_label;
768545916cd2Sjpk 	char		zonename[ZONENAME_MAX];
768645916cd2Sjpk 	zoneid_t	zoneid;
768745916cd2Sjpk 
768845916cd2Sjpk 	/* init stuff */
768945916cd2Sjpk 	use_pbuf = 0;
769045916cd2Sjpk 	mismatch = 0;
769145916cd2Sjpk 
769245916cd2Sjpk 	/*
769345916cd2Sjpk 	 * For linked to pathname (LK_COMP_TYPE):
769445916cd2Sjpk 	 *  - If the linked to pathname is absolute (start with /), we
769545916cd2Sjpk 	 *    will use it as is.
769645916cd2Sjpk 	 *  - If it is a relative pathname then it is relative to 1 of 2
769745916cd2Sjpk 	 *    directories.  For a hardlink, it is relative to the current
769845916cd2Sjpk 	 *    directory.  For a symbolic link, it is relative to the
769945916cd2Sjpk 	 *    directory the symbolic link is in.  For the symbolic link
770045916cd2Sjpk 	 *    case, set a flag to indicate we need to use the prefix of
770145916cd2Sjpk 	 *    the restored file's pathname with the linked to pathname.
770245916cd2Sjpk 	 *
770345916cd2Sjpk 	 *    NOTE: At this point, we have no way to determine if we have
770445916cd2Sjpk 	 *    a hardlink or a symbolic link.  We will compare the 1st
770545916cd2Sjpk 	 *    component in the prefix portion of the restore file's
770645916cd2Sjpk 	 *    pathname to the 1st component in the attribute data
770745916cd2Sjpk 	 *    (the linked pathname).  If they are the same, we will assume
770845916cd2Sjpk 	 *    the link pathname to reconstruct is relative to the current
770945916cd2Sjpk 	 *    directory.  Otherwise, we will set a flag indicate we need
771045916cd2Sjpk 	 *    to use a prefix with the reconstructed name.  Need to compare
771145916cd2Sjpk 	 *    both the adorned and unadorned version before deciding a
771245916cd2Sjpk 	 *    mismatch.
771345916cd2Sjpk 	 */
771445916cd2Sjpk 
771545916cd2Sjpk 	buf = lk_real_path;
771645916cd2Sjpk 	if (*(str + 1) != '/') { /* got relative linked to path */
771745916cd2Sjpk 		ptr1 = orig_namep;
771845916cd2Sjpk 		ptr2 = strrchr(ptr1, '/');
771945916cd2Sjpk 		plen = ptr2 - ptr1;
772045916cd2Sjpk 		if (plen > 0) {
772145916cd2Sjpk 			pbuf[0] = '\0';
772245916cd2Sjpk 			plen++;		/* include '/' */
772345916cd2Sjpk 			(void) strncpy(pbuf, ptr1, plen);
772445916cd2Sjpk 			*(pbuf + plen) = '\0';
772545916cd2Sjpk 			ptr2 = strchr(pbuf, '/');
772645916cd2Sjpk 			if (strncmp(pbuf, str + 1, ptr2 - pbuf) != 0)
772745916cd2Sjpk 				mismatch = 1;
772845916cd2Sjpk 		}
772945916cd2Sjpk 
773045916cd2Sjpk 		if (mismatch == 1)
773145916cd2Sjpk 			use_pbuf = 1;
773245916cd2Sjpk 	}
773345916cd2Sjpk 
773445916cd2Sjpk 	buf[0] = '\0';
773545916cd2Sjpk 
773645916cd2Sjpk 	while (*str != '\0') {
773745916cd2Sjpk 
773845916cd2Sjpk 		switch (*str) {
773945916cd2Sjpk 
774045916cd2Sjpk 		case MLD_TYPE:
774145916cd2Sjpk 
774245916cd2Sjpk 			str++;
774345916cd2Sjpk 			if ((cp = strstr(str, ";;")) != NULL) {
774445916cd2Sjpk 				*cp = '\0';
774545916cd2Sjpk 
774645916cd2Sjpk 				/*
774745916cd2Sjpk 				 * Ignore attempts to backup over .MLD.
774845916cd2Sjpk 				 */
774945916cd2Sjpk 				if (strcmp(str, "../") != 0)
775045916cd2Sjpk 					(void) strncat(buf, str, MAXPATHLEN);
775145916cd2Sjpk 				str = cp + 2;
775245916cd2Sjpk 				*cp = ';';
775345916cd2Sjpk 			}
775445916cd2Sjpk 			break;
775545916cd2Sjpk 
775645916cd2Sjpk 		case SLD_TYPE:
775745916cd2Sjpk 
775845916cd2Sjpk 			str++;
775945916cd2Sjpk 			if ((cp = strstr(str, ";;")) != NULL) {
776045916cd2Sjpk 				*cp = '\0';
776145916cd2Sjpk 
776245916cd2Sjpk 				/*
776345916cd2Sjpk 				 * Use the path name in the header if
776445916cd2Sjpk 				 * error occurs when processing the
776545916cd2Sjpk 				 * SLD type.
776645916cd2Sjpk 				 */
776745916cd2Sjpk 
776845916cd2Sjpk 				if (!stobsl(str, &bslabel,
776945916cd2Sjpk 				    NO_CORRECTION, &reterr)) {
777045916cd2Sjpk 					(void) fprintf(stderr, gettext(
777145916cd2Sjpk 					    "tar: can't translate to binary"
777245916cd2Sjpk 					    "SL for SLD, stobsl() error:"
777345916cd2Sjpk 					    " %s\n"), strerror(errno));
777445916cd2Sjpk 					return (-1);
777545916cd2Sjpk 				}
777645916cd2Sjpk 
777745916cd2Sjpk 				str = cp + 2;
777845916cd2Sjpk 				*cp = ';';
777945916cd2Sjpk 
778045916cd2Sjpk 				if (use_pbuf == 1) {
778145916cd2Sjpk 					if (*pbuf != '/') {
778245916cd2Sjpk 						/* relative linked to path */
778345916cd2Sjpk 
778445916cd2Sjpk 						(void) getcwd(tempbuf,
778545916cd2Sjpk 						    (sizeof (tempbuf)));
778645916cd2Sjpk 						(void) strncat(tempbuf, "/",
778745916cd2Sjpk 						    MAXPATHLEN);
778845916cd2Sjpk 						(void) strncat(tempbuf, pbuf,
778945916cd2Sjpk 						    MAXPATHLEN);
779045916cd2Sjpk 					}
779145916cd2Sjpk 					else
779245916cd2Sjpk 						(void) strcpy(tempbuf, pbuf);
779345916cd2Sjpk 
779445916cd2Sjpk 				} else if (*buf != '/') {
779545916cd2Sjpk 					/* got relative linked to path */
779645916cd2Sjpk 
779745916cd2Sjpk 					(void) getcwd(tempbuf,
779845916cd2Sjpk 					    (sizeof (tempbuf)));
779945916cd2Sjpk 					(void) strncat(tempbuf, "/",
780045916cd2Sjpk 					    MAXPATHLEN);
780145916cd2Sjpk 				} else
780245916cd2Sjpk 					*tempbuf = '\0';
780345916cd2Sjpk 
780445916cd2Sjpk 				(void) strncat(tempbuf, buf, MAXPATHLEN);
780545916cd2Sjpk 				*buf = '\0';
780645916cd2Sjpk 
780745916cd2Sjpk 				if (blequal(&bslabel, &admin_high)) {
780845916cd2Sjpk 					bslabel = admin_low;
780945916cd2Sjpk 				}
781045916cd2Sjpk 
781145916cd2Sjpk 
781245916cd2Sjpk 				/*
781345916cd2Sjpk 				 * Check for cross-zone symbolic links
781445916cd2Sjpk 				 */
781545916cd2Sjpk 				from_label = getlabelbypath(real_path);
781645916cd2Sjpk 				if (rpath_flag && (from_label != NULL) &&
781745916cd2Sjpk 				    !blequal(&bslabel, from_label)) {
781845916cd2Sjpk 					if ((zoneid =
781945916cd2Sjpk 					    getzoneidbylabel(&bslabel)) == -1) {
782045916cd2Sjpk 						(void) fprintf(stderr,
782145916cd2Sjpk 						    gettext("tar: can't get "
782245916cd2Sjpk 						    "zone ID for %s\n"),
782345916cd2Sjpk 						    tempbuf);
782445916cd2Sjpk 						return (-1);
782545916cd2Sjpk 					}
782645916cd2Sjpk 					if (zone_getattr(zoneid, ZONE_ATTR_NAME,
782745916cd2Sjpk 					    &zonename, ZONENAME_MAX) == -1) {
782845916cd2Sjpk 						/* Badly configured zone info */
782945916cd2Sjpk 						(void) fprintf(stderr,
783045916cd2Sjpk 						    gettext("tar: can't get "
783145916cd2Sjpk 						    "zonename for %s\n"),
783245916cd2Sjpk 						    tempbuf);
783345916cd2Sjpk 						return (-1);
783445916cd2Sjpk 					}
783545916cd2Sjpk 					(void) strncpy(buf, AUTO_ZONE,
783645916cd2Sjpk 					    MAXPATHLEN);
783745916cd2Sjpk 					(void) strncat(buf, "/",
783845916cd2Sjpk 					    MAXPATHLEN);
783945916cd2Sjpk 					(void) strncat(buf, zonename,
784045916cd2Sjpk 					    MAXPATHLEN);
784145916cd2Sjpk 				}
784245916cd2Sjpk 				if (from_label != NULL)
784345916cd2Sjpk 					free(from_label);
784445916cd2Sjpk 				(void) strncat(buf, tempbuf, MAXPATHLEN);
784545916cd2Sjpk 				break;
784645916cd2Sjpk 			}
784745916cd2Sjpk 			mld_flag = 0;
784845916cd2Sjpk 			break;
784945916cd2Sjpk 
785045916cd2Sjpk 		case PATH_TYPE:
785145916cd2Sjpk 
785245916cd2Sjpk 			str++;
785345916cd2Sjpk 			if ((cp = strstr(str, ";;")) != NULL) {
785445916cd2Sjpk 				*cp = '\0';
785545916cd2Sjpk 				(void) strncat(buf, str, MAXPATHLEN);
785645916cd2Sjpk 				str = cp + 2;
785745916cd2Sjpk 				*cp = ';';
785845916cd2Sjpk 			}
785945916cd2Sjpk 			break;
786045916cd2Sjpk 
786145916cd2Sjpk 		default:
786245916cd2Sjpk 
786345916cd2Sjpk 			(void) fprintf(stderr, gettext(
786445916cd2Sjpk 			    "tar: error rebuilding path %s\n"),
786545916cd2Sjpk 			    *namep);
786645916cd2Sjpk 			*buf = '\0';
786745916cd2Sjpk 			str++;
786845916cd2Sjpk 			return (-1);
786945916cd2Sjpk 		}
787045916cd2Sjpk 	}
787145916cd2Sjpk 
787245916cd2Sjpk 	/*
787345916cd2Sjpk 	 * Done for LK_COMP_TYPE
787445916cd2Sjpk 	 */
787545916cd2Sjpk 
787645916cd2Sjpk 	return (0);    /* component path is rebuilt successfully */
787745916cd2Sjpk 
787845916cd2Sjpk } /* end rebuild_lk_comp_path() */
787945916cd2Sjpk 
788045916cd2Sjpk /*
788145916cd2Sjpk  *	Name: check_ext_attr()
788245916cd2Sjpk  *
788345916cd2Sjpk  *	Description:
788445916cd2Sjpk  *		Check the extended attributes for a file being extracted.
788545916cd2Sjpk  *		The attributes being checked here are CMW labels.
788645916cd2Sjpk  *		ACLs are not set here because they are set by the
788745916cd2Sjpk  *		pflag in doxtract().
788845916cd2Sjpk  *
788945916cd2Sjpk  *		If the label doesn't match, return 0
789045916cd2Sjpk  *		else return 1
789145916cd2Sjpk  */
789245916cd2Sjpk static int
789345916cd2Sjpk check_ext_attr(char *filename)
789445916cd2Sjpk {
789545916cd2Sjpk 	bslabel_t	currentlabel;	/* label from zone */
789645916cd2Sjpk 
789745916cd2Sjpk 	if (bltype(&bs_label, SUN_SL_UN)) {
789845916cd2Sjpk 		/* No label check possible */
789945916cd2Sjpk 		return (0);
790045916cd2Sjpk 	}
790145916cd2Sjpk 	if (getlabel(filename, &currentlabel) != 0) {
790245916cd2Sjpk 		(void) fprintf(stderr,
790345916cd2Sjpk 		    gettext("tar: can't get label for "
790445916cd2Sjpk 		    " %s, getlabel() error: %s\n"),
790545916cd2Sjpk 		    filename, strerror(errno));
790645916cd2Sjpk 		return (0);
790745916cd2Sjpk 	} else if ((blequal(&currentlabel, &bs_label)) == 0) {
790845916cd2Sjpk 		char	*src_label = NULL;	/* ascii label */
790945916cd2Sjpk 
791045916cd2Sjpk 		/* get current src SL */
791145916cd2Sjpk 		if (bsltos(&bs_label, &src_label, 0, 0) <= 0) {
791245916cd2Sjpk 			(void) fprintf(stderr,
791345916cd2Sjpk 			    gettext("tar: can't interpret requested label for"
791445916cd2Sjpk 			    " %s\n"), filename);
791545916cd2Sjpk 		} else {
791645916cd2Sjpk 			(void) fprintf(stderr,
791745916cd2Sjpk 			    gettext("tar: can't apply label %s to %s\n"),
791845916cd2Sjpk 			    src_label, filename);
791945916cd2Sjpk 			free(src_label);
792045916cd2Sjpk 		}
792145916cd2Sjpk 		(void) fprintf(stderr,
792245916cd2Sjpk 		    gettext("tar: %s not restored\n"), filename);
792345916cd2Sjpk 		return (0);
792445916cd2Sjpk 	}
792545916cd2Sjpk 	return (1);
792645916cd2Sjpk 
792745916cd2Sjpk }	/* end check_ext_attr */
7928