xref: /titanic_51/usr/src/cmd/tar/tar.c (revision 03d7b3c0e29500d6f69fdb637d3b884fb0e8b5c2)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22257ece65SRalph Turner - Sun UK - Contractor  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
267c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
337c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <unistd.h>
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/param.h>
397c478bd9Sstevel@tonic-gate #include <sys/stat.h>
407c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
417c478bd9Sstevel@tonic-gate #include <sys/wait.h>
427c478bd9Sstevel@tonic-gate #include <dirent.h>
437c478bd9Sstevel@tonic-gate #include <errno.h>
447c478bd9Sstevel@tonic-gate #include <stdio.h>
457c478bd9Sstevel@tonic-gate #include <signal.h>
467c478bd9Sstevel@tonic-gate #include <ctype.h>
477c478bd9Sstevel@tonic-gate #include <locale.h>
487c478bd9Sstevel@tonic-gate #include <nl_types.h>
497c478bd9Sstevel@tonic-gate #include <langinfo.h>
507c478bd9Sstevel@tonic-gate #include <pwd.h>
517c478bd9Sstevel@tonic-gate #include <grp.h>
527c478bd9Sstevel@tonic-gate #include <fcntl.h>
537c478bd9Sstevel@tonic-gate #include <string.h>
547c478bd9Sstevel@tonic-gate #include <malloc.h>
557c478bd9Sstevel@tonic-gate #include <time.h>
567c478bd9Sstevel@tonic-gate #include <utime.h>
577c478bd9Sstevel@tonic-gate #include <stdlib.h>
587c478bd9Sstevel@tonic-gate #include <stdarg.h>
597c478bd9Sstevel@tonic-gate #include <widec.h>
607c478bd9Sstevel@tonic-gate #include <sys/mtio.h>
617c478bd9Sstevel@tonic-gate #include <sys/acl.h>
627c478bd9Sstevel@tonic-gate #include <strings.h>
637c478bd9Sstevel@tonic-gate #include <deflt.h>
647c478bd9Sstevel@tonic-gate #include <limits.h>
657c478bd9Sstevel@tonic-gate #include <iconv.h>
667c478bd9Sstevel@tonic-gate #include <assert.h>
67da6c28aaSamw #include <libgen.h>
68da6c28aaSamw #include <libintl.h>
69fa9e4066Sahrens #include <aclutils.h>
70da6c28aaSamw #include <libnvpair.h>
71da6c28aaSamw #include <archives.h>
723d63ea05Sas145665 
737c478bd9Sstevel@tonic-gate #if defined(__SunOS_5_6) || defined(__SunOS_5_7)
747c478bd9Sstevel@tonic-gate extern int defcntl();
757c478bd9Sstevel@tonic-gate #endif
76da6c28aaSamw #if defined(_PC_SATTR_ENABLED)
77da6c28aaSamw #include <attr.h>
78da6c28aaSamw #include <libcmdutils.h>
79da6c28aaSamw #endif
807c478bd9Sstevel@tonic-gate 
8145916cd2Sjpk /* Trusted Extensions */
8245916cd2Sjpk #include <zone.h>
8345916cd2Sjpk #include <tsol/label.h>
8445916cd2Sjpk #include <sys/tsol/label_macro.h>
8545916cd2Sjpk 
86da6c28aaSamw #include "getresponse.h"
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate  * Source compatibility
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate  * These constants come from archives.h and sys/fcntl.h
937c478bd9Sstevel@tonic-gate  * and were introduced by the extended attributes project
947c478bd9Sstevel@tonic-gate  * in Solaris 9.
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate #if !defined(O_XATTR)
977c478bd9Sstevel@tonic-gate #define	AT_SYMLINK_NOFOLLOW	0x1000
987c478bd9Sstevel@tonic-gate #define	AT_REMOVEDIR		0x1
997c478bd9Sstevel@tonic-gate #define	AT_FDCWD		0xffd19553
1007c478bd9Sstevel@tonic-gate #define	_XATTR_HDRTYPE		'E'
1017c478bd9Sstevel@tonic-gate static int attropen();
1027c478bd9Sstevel@tonic-gate static int fstatat();
1037c478bd9Sstevel@tonic-gate static int renameat();
1047c478bd9Sstevel@tonic-gate static int unlinkat();
1057c478bd9Sstevel@tonic-gate static int openat();
1067c478bd9Sstevel@tonic-gate static int fchownat();
1077c478bd9Sstevel@tonic-gate static int futimesat();
1087c478bd9Sstevel@tonic-gate #endif
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate  * Compiling with -D_XPG4_2 gets this but produces other problems, so
1127c478bd9Sstevel@tonic-gate  * instead of including sys/time.h and compiling with -D_XPG4_2, I'm
1137c478bd9Sstevel@tonic-gate  * explicitly doing the declaration here.
1147c478bd9Sstevel@tonic-gate  */
1157c478bd9Sstevel@tonic-gate int utimes(const char *path, const struct timeval timeval_ptr[]);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate #ifndef MINSIZE
1187c478bd9Sstevel@tonic-gate #define	MINSIZE 250
1197c478bd9Sstevel@tonic-gate #endif
1207c478bd9Sstevel@tonic-gate #define	DEF_FILE "/etc/default/tar"
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate #define	min(a, b)  ((a) < (b) ? (a) : (b))
1237c478bd9Sstevel@tonic-gate #define	max(a, b)  ((a) > (b) ? (a) : (b))
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate /* -DDEBUG	ONLY for debugging */
1267c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1277c478bd9Sstevel@tonic-gate #undef	DEBUG
1287c478bd9Sstevel@tonic-gate #define	DEBUG(a, b, c)\
1297c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "DEBUG - "), (void) fprintf(stderr, a, b, c)
1307c478bd9Sstevel@tonic-gate #endif
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate #define	TBLOCK	512	/* tape block size--should be universal */
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate #ifdef	BSIZE
1357c478bd9Sstevel@tonic-gate #define	SYS_BLOCK BSIZE	/* from sys/param.h:  secondary block size */
1367c478bd9Sstevel@tonic-gate #else	/* BSIZE */
1377c478bd9Sstevel@tonic-gate #define	SYS_BLOCK 512	/* default if no BSIZE in param.h */
1387c478bd9Sstevel@tonic-gate #endif	/* BSIZE */
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate #define	NBLOCK	20
1417c478bd9Sstevel@tonic-gate #define	NAMSIZ	100
1427c478bd9Sstevel@tonic-gate #define	PRESIZ	155
1437c478bd9Sstevel@tonic-gate #define	MAXNAM	256
1447c478bd9Sstevel@tonic-gate #define	MODEMASK 0777777	/* file creation mode mask */
1457c478bd9Sstevel@tonic-gate #define	POSIXMODES 07777	/* mask for POSIX mode bits */
1467c478bd9Sstevel@tonic-gate #define	MAXEXT	9	/* reasonable max # extents for a file */
1477c478bd9Sstevel@tonic-gate #define	EXTMIN	50	/* min blks left on floppy to split a file */
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate /* max value dblock.dbuf.efsize can store */
1507c478bd9Sstevel@tonic-gate #define	TAR_EFSIZE_MAX	 0777777777
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * Symbols which specify the values at which the use of the 'E' function
1547c478bd9Sstevel@tonic-gate  * modifier is required to properly store a file.
1557c478bd9Sstevel@tonic-gate  *
1567c478bd9Sstevel@tonic-gate  *     TAR_OFFSET_MAX    - the largest file size we can archive
1577c478bd9Sstevel@tonic-gate  *     OCTAL7CHAR        - the limit for ustar gid, uid, dev
1587c478bd9Sstevel@tonic-gate  */
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate #ifdef XHDR_DEBUG
1617c478bd9Sstevel@tonic-gate /* tiny values which force the creation of extended header entries */
1627c478bd9Sstevel@tonic-gate #define	TAR_OFFSET_MAX 9
1637c478bd9Sstevel@tonic-gate #define	OCTAL7CHAR 2
1647c478bd9Sstevel@tonic-gate #else
1657c478bd9Sstevel@tonic-gate /* normal values */
166b7d62af5Sceastha #define	TAR_OFFSET_MAX	077777777777ULL
1677c478bd9Sstevel@tonic-gate #define	OCTAL7CHAR	07777777
1687c478bd9Sstevel@tonic-gate #endif
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate #define	TBLOCKS(bytes)	(((bytes) + TBLOCK - 1) / TBLOCK)
1717c478bd9Sstevel@tonic-gate #define	K(tblocks)	((tblocks+1)/2)	/* tblocks to Kbytes for printing */
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate #define	MAXLEV	(PATH_MAX / 2)
1747c478bd9Sstevel@tonic-gate #define	LEV0	1
1757c478bd9Sstevel@tonic-gate #define	SYMLINK_LEV0	0
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate #define	TRUE	1
1787c478bd9Sstevel@tonic-gate #define	FALSE	0
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate #define	XATTR_FILE	1
1817c478bd9Sstevel@tonic-gate #define	NORMAL_FILE	0
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate #define	PUT_AS_LINK	1
1847c478bd9Sstevel@tonic-gate #define	PUT_NOTAS_LINK	0
1857c478bd9Sstevel@tonic-gate 
186da6c28aaSamw #ifndef VIEW_READONLY
187da6c28aaSamw #define	VIEW_READONLY	"SUNWattr_ro"
188da6c28aaSamw #endif
189da6c28aaSamw 
190da6c28aaSamw #ifndef VIEW_READWRITE
191da6c28aaSamw #define	VIEW_READWRITE	"SUNWattr_rw"
192da6c28aaSamw #endif
193da6c28aaSamw 
1947c478bd9Sstevel@tonic-gate #if _FILE_OFFSET_BITS == 64
1957c478bd9Sstevel@tonic-gate #define	FMT_off_t "lld"
1967c478bd9Sstevel@tonic-gate #define	FMT_off_t_o "llo"
1977c478bd9Sstevel@tonic-gate #define	FMT_blkcnt_t "lld"
1987c478bd9Sstevel@tonic-gate #else
1997c478bd9Sstevel@tonic-gate #define	FMT_off_t "ld"
2007c478bd9Sstevel@tonic-gate #define	FMT_off_t_o "lo"
2017c478bd9Sstevel@tonic-gate #define	FMT_blkcnt_t "ld"
2027c478bd9Sstevel@tonic-gate #endif
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate /* ACL support */
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate static
2077c478bd9Sstevel@tonic-gate struct	sec_attr {
2087c478bd9Sstevel@tonic-gate 	char	attr_type;
2097c478bd9Sstevel@tonic-gate 	char	attr_len[7];
2107c478bd9Sstevel@tonic-gate 	char	attr_info[1];
2117c478bd9Sstevel@tonic-gate } *attr;
2127c478bd9Sstevel@tonic-gate 
213da6c28aaSamw #if defined(O_XATTR)
214da6c28aaSamw typedef enum {
215da6c28aaSamw 	ATTR_OK,
216da6c28aaSamw 	ATTR_SKIP,
217da6c28aaSamw 	ATTR_CHDIR_ERR,
218da6c28aaSamw 	ATTR_OPEN_ERR,
219da6c28aaSamw 	ATTR_XATTR_ERR,
220da6c28aaSamw 	ATTR_SATTR_ERR
221da6c28aaSamw } attr_status_t;
222da6c28aaSamw #endif
223da6c28aaSamw 
224da6c28aaSamw #if defined(O_XATTR)
225da6c28aaSamw typedef enum {
226da6c28aaSamw 	ARC_CREATE,
227da6c28aaSamw 	ARC_RESTORE
228da6c28aaSamw } arc_action_t;
229da6c28aaSamw #endif
230da6c28aaSamw 
231da6c28aaSamw typedef struct attr_data {
232da6c28aaSamw 	char	*attr_parent;
233da6c28aaSamw 	char	*attr_path;
234da6c28aaSamw 	int	attr_parentfd;
235da6c28aaSamw 	int	attr_rw_sysattr;
236da6c28aaSamw } attr_data_t;
237da6c28aaSamw 
2387c478bd9Sstevel@tonic-gate /*
2397c478bd9Sstevel@tonic-gate  *
2407c478bd9Sstevel@tonic-gate  * Tar has been changed to support extended attributes.
2417c478bd9Sstevel@tonic-gate  *
2427c478bd9Sstevel@tonic-gate  * As part of this change tar now uses the new *at() syscalls
2437c478bd9Sstevel@tonic-gate  * such as openat, fchownat(), unlinkat()...
2447c478bd9Sstevel@tonic-gate  *
2457c478bd9Sstevel@tonic-gate  * This was done so that attributes can be handled with as few code changes
2467c478bd9Sstevel@tonic-gate  * as possible.
2477c478bd9Sstevel@tonic-gate  *
2487c478bd9Sstevel@tonic-gate  * What this means is that tar now opens the directory that a file or directory
2497c478bd9Sstevel@tonic-gate  * resides in and then performs *at() functions to manipulate the entry.
2507c478bd9Sstevel@tonic-gate  *
2517c478bd9Sstevel@tonic-gate  * For example a new file is now created like this:
2527c478bd9Sstevel@tonic-gate  *
2537c478bd9Sstevel@tonic-gate  * dfd = open(<some dir path>)
2547c478bd9Sstevel@tonic-gate  * fd = openat(dfd, <name>,....);
2557c478bd9Sstevel@tonic-gate  *
2567c478bd9Sstevel@tonic-gate  * or in the case of an extended attribute
2577c478bd9Sstevel@tonic-gate  *
2587c478bd9Sstevel@tonic-gate  * dfd = attropen(<pathname>, ".", ....)
2597c478bd9Sstevel@tonic-gate  *
2607c478bd9Sstevel@tonic-gate  * Once we have a directory file descriptor all of the *at() functions can
2617c478bd9Sstevel@tonic-gate  * be applied to it.
2627c478bd9Sstevel@tonic-gate  *
2637c478bd9Sstevel@tonic-gate  * unlinkat(dfd, <component name>,...)
2647c478bd9Sstevel@tonic-gate  * fchownat(dfd, <component name>,..)
2657c478bd9Sstevel@tonic-gate  *
2667c478bd9Sstevel@tonic-gate  * This works for both normal namespace files and extended attribute file
2677c478bd9Sstevel@tonic-gate  *
2687c478bd9Sstevel@tonic-gate  */
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate /*
2717c478bd9Sstevel@tonic-gate  *
2727c478bd9Sstevel@tonic-gate  * Extended attribute Format
2737c478bd9Sstevel@tonic-gate  *
2747c478bd9Sstevel@tonic-gate  * Extended attributes are stored in two pieces.
2757c478bd9Sstevel@tonic-gate  * 1. An attribute header which has information about
2767c478bd9Sstevel@tonic-gate  *    what file the attribute is for and what the attribute
2777c478bd9Sstevel@tonic-gate  *    is named.
2787c478bd9Sstevel@tonic-gate  * 2. The attribute record itself.  Stored as a normal file type
2797c478bd9Sstevel@tonic-gate  *    of entry.
2807c478bd9Sstevel@tonic-gate  * Both the header and attribute record have special modes/typeflags
2817c478bd9Sstevel@tonic-gate  * associated with them.
2827c478bd9Sstevel@tonic-gate  *
2837c478bd9Sstevel@tonic-gate  * The names of the header in the archive look like:
2847c478bd9Sstevel@tonic-gate  * /dev/null/attr.hdr
2857c478bd9Sstevel@tonic-gate  *
2867c478bd9Sstevel@tonic-gate  * The name of the attribute looks like:
2877c478bd9Sstevel@tonic-gate  * /dev/null/attr
2887c478bd9Sstevel@tonic-gate  *
2897c478bd9Sstevel@tonic-gate  * This is done so that an archiver that doesn't understand these formats
2907c478bd9Sstevel@tonic-gate  * can just dispose of the attribute records.
2917c478bd9Sstevel@tonic-gate  *
2927c478bd9Sstevel@tonic-gate  * The format is composed of a fixed size header followed
2937c478bd9Sstevel@tonic-gate  * by a variable sized xattr_buf. If the attribute is a hard link
2947c478bd9Sstevel@tonic-gate  * to another attribute then another xattr_buf section is included
2957c478bd9Sstevel@tonic-gate  * for the link.
2967c478bd9Sstevel@tonic-gate  *
2977c478bd9Sstevel@tonic-gate  * The xattr_buf is used to define the necessary "pathing" steps
2987c478bd9Sstevel@tonic-gate  * to get to the extended attribute.  This is necessary to support
2997c478bd9Sstevel@tonic-gate  * a fully recursive attribute model where an attribute may itself
3007c478bd9Sstevel@tonic-gate  * have an attribute.
3017c478bd9Sstevel@tonic-gate  *
3027c478bd9Sstevel@tonic-gate  * The basic layout looks like this.
3037c478bd9Sstevel@tonic-gate  *
3047c478bd9Sstevel@tonic-gate  *     --------------------------------
3057c478bd9Sstevel@tonic-gate  *     |                              |
3067c478bd9Sstevel@tonic-gate  *     |         xattr_hdr            |
3077c478bd9Sstevel@tonic-gate  *     |                              |
3087c478bd9Sstevel@tonic-gate  *     --------------------------------
3097c478bd9Sstevel@tonic-gate  *     --------------------------------
3107c478bd9Sstevel@tonic-gate  *     |                              |
3117c478bd9Sstevel@tonic-gate  *     |        xattr_buf             |
3127c478bd9Sstevel@tonic-gate  *     |                              |
3137c478bd9Sstevel@tonic-gate  *     --------------------------------
3147c478bd9Sstevel@tonic-gate  *     --------------------------------
3157c478bd9Sstevel@tonic-gate  *     |                              |
3167c478bd9Sstevel@tonic-gate  *     |      (optional link info)    |
3177c478bd9Sstevel@tonic-gate  *     |                              |
3187c478bd9Sstevel@tonic-gate  *     --------------------------------
3197c478bd9Sstevel@tonic-gate  *     --------------------------------
3207c478bd9Sstevel@tonic-gate  *     |                              |
3217c478bd9Sstevel@tonic-gate  *     |      attribute itself        |
3227c478bd9Sstevel@tonic-gate  *     |      stored as normal tar    |
3237c478bd9Sstevel@tonic-gate  *     |      or cpio data with       |
3247c478bd9Sstevel@tonic-gate  *     |      special mode or         |
3257c478bd9Sstevel@tonic-gate  *     |      typeflag                |
3267c478bd9Sstevel@tonic-gate  *     |                              |
3277c478bd9Sstevel@tonic-gate  *     --------------------------------
3287c478bd9Sstevel@tonic-gate  *
3297c478bd9Sstevel@tonic-gate  */
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate /*
3327c478bd9Sstevel@tonic-gate  * xattrhead is a pointer to the xattr_hdr
3337c478bd9Sstevel@tonic-gate  *
3347c478bd9Sstevel@tonic-gate  * xattrp is a pointer to the xattr_buf structure
3357c478bd9Sstevel@tonic-gate  * which contains the "pathing" steps to get to attributes
3367c478bd9Sstevel@tonic-gate  *
3377c478bd9Sstevel@tonic-gate  * xattr_linkp is a pointer to another xattr_buf structure that is
3387c478bd9Sstevel@tonic-gate  * only used when an attribute is actually linked to another attribute
3397c478bd9Sstevel@tonic-gate  *
3407c478bd9Sstevel@tonic-gate  */
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate static struct xattr_hdr *xattrhead;
3437c478bd9Sstevel@tonic-gate static struct xattr_buf *xattrp;
3447c478bd9Sstevel@tonic-gate static struct xattr_buf *xattr_linkp;	/* pointer to link info, if any */
345da6c28aaSamw static char *xattrapath;		/* attribute name */
3467c478bd9Sstevel@tonic-gate static char *xattr_linkaname;		/* attribute attribute is linked to */
3477c478bd9Sstevel@tonic-gate static char Hiddendir;			/* are we processing hidden xattr dir */
3487c478bd9Sstevel@tonic-gate static char xattrbadhead;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate /* Was statically allocated tbuf[NBLOCK] */
3517c478bd9Sstevel@tonic-gate static
3527c478bd9Sstevel@tonic-gate union hblock {
3537c478bd9Sstevel@tonic-gate 	char dummy[TBLOCK];
3547c478bd9Sstevel@tonic-gate 	struct header {
3557c478bd9Sstevel@tonic-gate 		char name[NAMSIZ];	/* If non-null prefix, path is	*/
3567c478bd9Sstevel@tonic-gate 					/* <prefix>/<name>;  otherwise	*/
3577c478bd9Sstevel@tonic-gate 					/* <name>			*/
3587c478bd9Sstevel@tonic-gate 		char mode[8];
3597c478bd9Sstevel@tonic-gate 		char uid[8];
3607c478bd9Sstevel@tonic-gate 		char gid[8];
3617c478bd9Sstevel@tonic-gate 		char size[12];		/* size of this extent if file split */
3627c478bd9Sstevel@tonic-gate 		char mtime[12];
3637c478bd9Sstevel@tonic-gate 		char chksum[8];
3647c478bd9Sstevel@tonic-gate 		char typeflag;
3657c478bd9Sstevel@tonic-gate 		char linkname[NAMSIZ];
3667c478bd9Sstevel@tonic-gate 		char magic[6];
3677c478bd9Sstevel@tonic-gate 		char version[2];
3687c478bd9Sstevel@tonic-gate 		char uname[32];
3697c478bd9Sstevel@tonic-gate 		char gname[32];
3707c478bd9Sstevel@tonic-gate 		char devmajor[8];
3717c478bd9Sstevel@tonic-gate 		char devminor[8];
3727c478bd9Sstevel@tonic-gate 		char prefix[PRESIZ];	/* Together with "name", the path of */
3737c478bd9Sstevel@tonic-gate 					/* the file:  <prefix>/<name>	*/
3747c478bd9Sstevel@tonic-gate 		char extno;		/* extent #, null if not split */
3757c478bd9Sstevel@tonic-gate 		char extotal;		/* total extents */
3767c478bd9Sstevel@tonic-gate 		char efsize[10];	/* size of entire file */
3777c478bd9Sstevel@tonic-gate 	} dbuf;
3787c478bd9Sstevel@tonic-gate } dblock, *tbuf, xhdr_buf;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate static
3817c478bd9Sstevel@tonic-gate struct xtar_hdr {
3827c478bd9Sstevel@tonic-gate 	uid_t		x_uid,		/* Uid of file */
3837c478bd9Sstevel@tonic-gate 			x_gid;		/* Gid of file */
3847c478bd9Sstevel@tonic-gate 	major_t		x_devmajor;	/* Device major node */
3857c478bd9Sstevel@tonic-gate 	minor_t		x_devminor;	/* Device minor node */
3867c478bd9Sstevel@tonic-gate 	off_t		x_filesz;	/* Length of file */
3877c478bd9Sstevel@tonic-gate 	char		*x_uname,	/* Pointer to name of user */
3887c478bd9Sstevel@tonic-gate 			*x_gname,	/* Pointer to gid of user */
3897c478bd9Sstevel@tonic-gate 			*x_linkpath,	/* Path for a hard/symbolic link */
3907c478bd9Sstevel@tonic-gate 			*x_path;	/* Path of file */
3917c478bd9Sstevel@tonic-gate 	timestruc_t	x_mtime;	/* Seconds and nanoseconds */
3927c478bd9Sstevel@tonic-gate } Xtarhdr;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate static
3957c478bd9Sstevel@tonic-gate struct gen_hdr {
3967c478bd9Sstevel@tonic-gate 	ulong_t		g_mode;		/* Mode of file */
3977c478bd9Sstevel@tonic-gate 	uid_t		g_uid,		/* Uid of file */
3987c478bd9Sstevel@tonic-gate 			g_gid;		/* Gid of file */
3997c478bd9Sstevel@tonic-gate 	off_t		g_filesz;	/* Length of file */
4007c478bd9Sstevel@tonic-gate 	time_t		g_mtime;	/* Modification time */
4017c478bd9Sstevel@tonic-gate 	uint_t		g_cksum;	/* Checksum of file */
4027c478bd9Sstevel@tonic-gate 	ulong_t		g_devmajor,	/* File system of file */
4037c478bd9Sstevel@tonic-gate 			g_devminor;	/* Major/minor of special files */
4047c478bd9Sstevel@tonic-gate } Gen;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate static
4077c478bd9Sstevel@tonic-gate struct linkbuf {
4087c478bd9Sstevel@tonic-gate 	ino_t	inum;
4097c478bd9Sstevel@tonic-gate 	dev_t	devnum;
4107c478bd9Sstevel@tonic-gate 	int	count;
4117c478bd9Sstevel@tonic-gate 	char	pathname[MAXNAM+1];	/* added 1 for last NULL */
4127c478bd9Sstevel@tonic-gate 	char 	attrname[MAXNAM+1];
4137c478bd9Sstevel@tonic-gate 	struct	linkbuf *nextp;
4147c478bd9Sstevel@tonic-gate } *ihead;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate /* see comments before build_table() */
4177c478bd9Sstevel@tonic-gate #define	TABLE_SIZE 512
4185e2174acSceastha typedef struct	file_list	{
4197c478bd9Sstevel@tonic-gate 	char	*name;			/* Name of file to {in,ex}clude */
4207c478bd9Sstevel@tonic-gate 	struct	file_list	*next;	/* Linked list */
4215e2174acSceastha } file_list_t;
4225e2174acSceastha static	file_list_t	*exclude_tbl[TABLE_SIZE],
4237c478bd9Sstevel@tonic-gate 			*include_tbl[TABLE_SIZE];
4247c478bd9Sstevel@tonic-gate 
42545916cd2Sjpk static int	append_secattr(char **, int *, int, char *, char);
4267c478bd9Sstevel@tonic-gate static void	write_ancillary(union hblock *, char *, int, char);
4277c478bd9Sstevel@tonic-gate 
4285e2174acSceastha static void add_file_to_table(file_list_t *table[], char *str);
4297c478bd9Sstevel@tonic-gate static void assert_string(char *s, char *msg);
4307c478bd9Sstevel@tonic-gate static int istape(int fd, int type);
4317c478bd9Sstevel@tonic-gate static void backtape(void);
4325e2174acSceastha static void build_table(file_list_t *table[], char *file);
433da6c28aaSamw static int check_prefix(char **namep, char **dirp, char **compp);
4347c478bd9Sstevel@tonic-gate static void closevol(void);
4357c478bd9Sstevel@tonic-gate static void copy(void *dst, void *src);
4367c478bd9Sstevel@tonic-gate static int convtoreg(off_t);
4372c0f0499Slovely static void delete_target(int fd, char *comp, char *namep);
4387c478bd9Sstevel@tonic-gate static void doDirTimes(char *name, timestruc_t modTime);
4397c478bd9Sstevel@tonic-gate static void done(int n);
4407c478bd9Sstevel@tonic-gate static void dorep(char *argv[]);
4417c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
4427c478bd9Sstevel@tonic-gate static void dotable(char *argv[], int cnt);
4437c478bd9Sstevel@tonic-gate static void doxtract(char *argv[], int cnt);
4447c478bd9Sstevel@tonic-gate #else
4457c478bd9Sstevel@tonic-gate static void dotable(char *argv[]);
4467c478bd9Sstevel@tonic-gate static void doxtract(char *argv[]);
4477c478bd9Sstevel@tonic-gate #endif
4483a1dab68SRich Burridge static int is_directory(char *name);
4491a431409SRich Burridge static int has_dot_dot(char *name);
4501a431409SRich Burridge static int is_absolute(char *name);
4511a431409SRich Burridge static char *make_relative_name(char *name, char **stripped_prefix);
4527c478bd9Sstevel@tonic-gate static void fatal(char *format, ...);
4537c478bd9Sstevel@tonic-gate static void vperror(int exit_status, char *fmt, ...);
4547c478bd9Sstevel@tonic-gate static void flushtape(void);
4557c478bd9Sstevel@tonic-gate static void getdir(void);
4567c478bd9Sstevel@tonic-gate static void *getmem(size_t);
4577c478bd9Sstevel@tonic-gate static void longt(struct stat *st, char aclchar);
458123523f8Sas158974 static void load_info_from_xtarhdr(u_longlong_t flag, struct xtar_hdr *xhdrp);
4597c478bd9Sstevel@tonic-gate static int makeDir(char *name);
4607c478bd9Sstevel@tonic-gate static void mterr(char *operation, int i, int exitcode);
4617c478bd9Sstevel@tonic-gate static void newvol(void);
4627c478bd9Sstevel@tonic-gate static void passtape(void);
4637c478bd9Sstevel@tonic-gate static void putempty(blkcnt_t n);
4647c478bd9Sstevel@tonic-gate static int putfile(char *longname, char *shortname, char *parent,
465da6c28aaSamw     attr_data_t *attrinfo, int filetype, int lev, int symlink_lev);
4667c478bd9Sstevel@tonic-gate static void readtape(char *buffer);
4677c478bd9Sstevel@tonic-gate static void seekdisk(blkcnt_t blocks);
4687c478bd9Sstevel@tonic-gate static void setPathTimes(int dirfd, char *path, timestruc_t modTime);
469123523f8Sas158974 static void setbytes_to_skip(struct stat *st, int err);
4707c478bd9Sstevel@tonic-gate static void splitfile(char *longname, int ifd, char *name,
4717c478bd9Sstevel@tonic-gate 	char *prefix, int filetype);
4727c478bd9Sstevel@tonic-gate static void tomodes(struct stat *sp);
4737c478bd9Sstevel@tonic-gate static void usage(void);
474da6c28aaSamw static int xblocks(int issysattr, off_t bytes, int ofile);
475da6c28aaSamw static int xsfile(int issysattr, int ofd);
4767c478bd9Sstevel@tonic-gate static void resugname(int dirfd, char *name, int symflag);
4777c478bd9Sstevel@tonic-gate static int bcheck(char *bstr);
4787c478bd9Sstevel@tonic-gate static int checkdir(char *name);
4797c478bd9Sstevel@tonic-gate static int checksum(union hblock *dblockp);
4807c478bd9Sstevel@tonic-gate #ifdef	EUC
4817c478bd9Sstevel@tonic-gate static int checksum_signed(union hblock *dblockp);
4827c478bd9Sstevel@tonic-gate #endif	/* EUC */
4837c478bd9Sstevel@tonic-gate static int checkupdate(char *arg);
4847c478bd9Sstevel@tonic-gate static int checkw(char c, char *name);
4857c478bd9Sstevel@tonic-gate static int cmp(char *b, char *s, int n);
4867c478bd9Sstevel@tonic-gate static int defset(char *arch);
4877c478bd9Sstevel@tonic-gate static int endtape(void);
4885e2174acSceastha static int is_in_table(file_list_t *table[], char *str);
4897c478bd9Sstevel@tonic-gate static int notsame(void);
4907c478bd9Sstevel@tonic-gate static int is_prefix(char *s1, char *s2);
4917c478bd9Sstevel@tonic-gate static int response(void);
4927c478bd9Sstevel@tonic-gate static int build_dblock(const char *, const char *, const char,
4937c478bd9Sstevel@tonic-gate 	const int filetype, const struct stat *, const dev_t, const char *);
4947c478bd9Sstevel@tonic-gate static unsigned int hash(char *str);
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
4977c478bd9Sstevel@tonic-gate static void initarg(char *argv[], char *file);
4987c478bd9Sstevel@tonic-gate static char *nextarg();
4997c478bd9Sstevel@tonic-gate #endif
5007c478bd9Sstevel@tonic-gate static blkcnt_t kcheck(char *kstr);
5017c478bd9Sstevel@tonic-gate static off_t bsrch(char *s, int n, off_t l, off_t h);
5027c478bd9Sstevel@tonic-gate static void onintr(int sig);
5037c478bd9Sstevel@tonic-gate static void onquit(int sig);
5047c478bd9Sstevel@tonic-gate static void onhup(int sig);
5057c478bd9Sstevel@tonic-gate static uid_t getuidbyname(char *);
5067c478bd9Sstevel@tonic-gate static gid_t getgidbyname(char *);
5077c478bd9Sstevel@tonic-gate static char *getname(gid_t);
5087c478bd9Sstevel@tonic-gate static char *getgroup(gid_t);
5097c478bd9Sstevel@tonic-gate static int checkf(char *name, int mode, int howmuch);
5107c478bd9Sstevel@tonic-gate static int writetbuf(char *buffer, int n);
511da6c28aaSamw static int wantit(char *argv[], char **namep, char **dirp, char **comp,
512da6c28aaSamw     attr_data_t **attrinfo);
51345916cd2Sjpk static void append_ext_attr(char *shortname, char **secinfo, int *len);
5147c478bd9Sstevel@tonic-gate static int get_xdata(void);
5157c478bd9Sstevel@tonic-gate static void gen_num(const char *keyword, const u_longlong_t number);
5167c478bd9Sstevel@tonic-gate static void gen_date(const char *keyword, const timestruc_t time_value);
5177c478bd9Sstevel@tonic-gate static void gen_string(const char *keyword, const char *value);
5187c478bd9Sstevel@tonic-gate static void get_xtime(char *value, timestruc_t *xtime);
5197c478bd9Sstevel@tonic-gate static int chk_path_build(char *name, char *longname, char *linkname,
5207c478bd9Sstevel@tonic-gate     char *prefix, char type, int filetype);
5217c478bd9Sstevel@tonic-gate static int gen_utf8_names(const char *filename);
5227c478bd9Sstevel@tonic-gate static int utf8_local(char *option, char **Xhdr_ptrptr, char *target,
5237c478bd9Sstevel@tonic-gate     const char *src, int max_val);
5247c478bd9Sstevel@tonic-gate static int local_utf8(char **Xhdr_ptrptr, char *target, const char *src,
5257c478bd9Sstevel@tonic-gate     iconv_t iconv_cd, int xhdrflg, int max_val);
5267c478bd9Sstevel@tonic-gate static int c_utf8(char *target, const char *source);
527da6c28aaSamw static int getstat(int dirfd, char *longname, char *shortname,
528da6c28aaSamw     char *attrparent);
529da6c28aaSamw static void xattrs_put(char *, char *, char *, char *);
5307c478bd9Sstevel@tonic-gate static void prepare_xattr(char **, char	*, char	*,
5317c478bd9Sstevel@tonic-gate     char, struct linkbuf *, int *);
532da6c28aaSamw static int put_link(char *name, char *longname, char *component,
533da6c28aaSamw     char *longattrname, char *prefix, int filetype, char typeflag);
5347c478bd9Sstevel@tonic-gate static int put_extra_attributes(char *longname, char *shortname,
535da6c28aaSamw     char *longattrname, char *prefix, int filetype, char typeflag);
536da6c28aaSamw static int put_xattr_hdr(char *longname, char *shortname, char *longattrname,
537da6c28aaSamw     char *prefix, int typeflag, int filetype, struct linkbuf *lp);
538da6c28aaSamw static int read_xattr_hdr(attr_data_t **attrinfo);
53945916cd2Sjpk 
54045916cd2Sjpk /* Trusted Extensions */
54145916cd2Sjpk #define	AUTO_ZONE	"/zone"
54245916cd2Sjpk 
54345916cd2Sjpk static void extract_attr(char **file_ptr, struct sec_attr *);
54445916cd2Sjpk static int check_ext_attr(char *filename);
54545916cd2Sjpk static void rebuild_comp_path(char *str, char **namep);
54645916cd2Sjpk static int rebuild_lk_comp_path(char *str, char **namep);
54745916cd2Sjpk 
5487c478bd9Sstevel@tonic-gate static void get_parent(char *path, char *dir);
5497c478bd9Sstevel@tonic-gate static char *get_component(char *path);
550da6c28aaSamw static int retry_open_attr(int pdirfd, int cwd, char *dirp, char *pattr,
551da6c28aaSamw     char *name, int oflag, mode_t mode);
5527c478bd9Sstevel@tonic-gate static char *skipslashes(char *string, char *start);
5537c478bd9Sstevel@tonic-gate static void chop_endslashes(char *path);
55436802407SRich Burridge static pid_t compress_file(void);
55536802407SRich Burridge static void compress_back(void);
55636802407SRich Burridge static void decompress_file(void);
55736802407SRich Burridge static pid_t uncompress_file(void);
55836802407SRich Burridge static void *compress_malloc(size_t);
55936802407SRich Burridge static void check_compression();
56036802407SRich Burridge static char *bz_suffix();
56136802407SRich Burridge static char *gz_suffix();
56236802407SRich Burridge static char *add_suffix();
56336802407SRich Burridge static void wait_pid(pid_t);
564fa9e4066Sahrens 
5657c478bd9Sstevel@tonic-gate static	struct stat stbuf;
5667c478bd9Sstevel@tonic-gate 
567da6c28aaSamw static	char	*myname;
5687c478bd9Sstevel@tonic-gate static	int	checkflag = 0;
5697c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
5707c478bd9Sstevel@tonic-gate static	int	Fileflag;
5717c478bd9Sstevel@tonic-gate char    *sysv3_env;
5727c478bd9Sstevel@tonic-gate #endif
5737c478bd9Sstevel@tonic-gate static	int	Xflag, Fflag, iflag, hflag, Bflag, Iflag;
57436802407SRich Burridge static	int	rflag, xflag, vflag, tflag, mt, svmt, cflag, mflag, pflag;
5757c478bd9Sstevel@tonic-gate static	int	uflag;
5767c478bd9Sstevel@tonic-gate static	int	eflag, errflag, qflag;
5777c478bd9Sstevel@tonic-gate static	int	oflag;
5787c478bd9Sstevel@tonic-gate static	int	bflag, kflag, Aflag;
5797c478bd9Sstevel@tonic-gate static 	int	Pflag;			/* POSIX conformant archive */
5807c478bd9Sstevel@tonic-gate static	int	Eflag;			/* Allow files greater than 8GB */
5817c478bd9Sstevel@tonic-gate static	int	atflag;			/* traverse extended attributes */
582da6c28aaSamw static	int	saflag;			/* traverse extended sys attributes */
5837c478bd9Sstevel@tonic-gate static	int	Dflag;			/* Data change flag */
58436802407SRich Burridge static	int	jflag;			/* flag to use 'bzip2' */
58536802407SRich Burridge static	int	zflag;			/* flag to use 'gzip' */
58636802407SRich Burridge static	int	Zflag;			/* flag to use 'compress' */
58736802407SRich Burridge 
58845916cd2Sjpk /* Trusted Extensions */
58945916cd2Sjpk static	int	Tflag;			/* Trusted Extensions attr flags */
59045916cd2Sjpk static	int	dir_flag;		/* for attribute extract */
59145916cd2Sjpk static	int	mld_flag;		/* for attribute extract */
59245916cd2Sjpk static	char	*orig_namep;		/* original namep - unadorned */
59345916cd2Sjpk static	int	rpath_flag;		/* MLD real path is rebuilt */
59445916cd2Sjpk static	char	real_path[MAXPATHLEN];	/* MLD real path */
59545916cd2Sjpk static	int	lk_rpath_flag;		/* linked to real path is rebuilt */
59645916cd2Sjpk static	char	lk_real_path[MAXPATHLEN]; /* linked real path */
59745916cd2Sjpk static	bslabel_t	bs_label;	/* for attribute extract */
59845916cd2Sjpk static	bslabel_t	admin_low;
59945916cd2Sjpk static	bslabel_t	admin_high;
60045916cd2Sjpk static	int	ignored_aprivs = 0;
60145916cd2Sjpk static	int	ignored_fprivs = 0;
60245916cd2Sjpk static	int	ignored_fattrs = 0;
60345916cd2Sjpk 
6047c478bd9Sstevel@tonic-gate static	int	term, chksum, wflag,
6057c478bd9Sstevel@tonic-gate 		first = TRUE, defaults_used = FALSE, linkerrok;
6067c478bd9Sstevel@tonic-gate static	blkcnt_t	recno;
6077c478bd9Sstevel@tonic-gate static	int	freemem = 1;
6087c478bd9Sstevel@tonic-gate static	int	nblock = NBLOCK;
6097c478bd9Sstevel@tonic-gate static	int	Errflg = 0;
6107c478bd9Sstevel@tonic-gate static	int	exitflag = 0;
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate static	dev_t	mt_dev;		/* device containing output file */
6137c478bd9Sstevel@tonic-gate static	ino_t	mt_ino;		/* inode number of output file */
6147c478bd9Sstevel@tonic-gate static	int	mt_devtype;	/* dev type of archive, from stat structure */
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate static	int update = 1;		/* for `open' call */
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate static	off_t	low;
6197c478bd9Sstevel@tonic-gate static	off_t	high;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate static	FILE	*tfile;
6227c478bd9Sstevel@tonic-gate static	FILE	*vfile = stdout;
62331a2de35SRich Burridge static	char	*tmpdir;
62431a2de35SRich Burridge static	char	*tmp_suffix = "/tarXXXXXX";
62531a2de35SRich Burridge static	char	*tname;
6267c478bd9Sstevel@tonic-gate static	char	archive[] = "archive0=";
6277c478bd9Sstevel@tonic-gate static	char	*Xfile;
6287c478bd9Sstevel@tonic-gate static	char	*usefile;
62936802407SRich Burridge static	char	tfname[1024];
6307c478bd9Sstevel@tonic-gate static	char	*Filefile;
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate static	int	mulvol;		/* multi-volume option selected */
6337c478bd9Sstevel@tonic-gate static	blkcnt_t	blocklim; /* number of blocks to accept per volume */
6347c478bd9Sstevel@tonic-gate static	blkcnt_t	tapepos; /* current block number to be written */
6357c478bd9Sstevel@tonic-gate static	int	NotTape;	/* true if tape is a disk */
6367c478bd9Sstevel@tonic-gate static	int	dumping;	/* true if writing a tape or other archive */
6377c478bd9Sstevel@tonic-gate static	int	extno;		/* number of extent:  starts at 1 */
6387c478bd9Sstevel@tonic-gate static	int	extotal;	/* total extents in this file */
6397c478bd9Sstevel@tonic-gate static	off_t	extsize;	/* size of current extent during extraction */
6407c478bd9Sstevel@tonic-gate static	ushort_t	Oumask = 0;	/* old umask value */
6417c478bd9Sstevel@tonic-gate static 	int is_posix;	/* true if archive we're reading is POSIX-conformant */
6427c478bd9Sstevel@tonic-gate static	const	char	*magic_type = "ustar";
6437c478bd9Sstevel@tonic-gate static	size_t	xrec_size = 8 * PATH_MAX;	/* extended rec initial size */
6447c478bd9Sstevel@tonic-gate static	char	*xrec_ptr;
6457c478bd9Sstevel@tonic-gate static	off_t	xrec_offset = 0;
6467c478bd9Sstevel@tonic-gate static	int	Xhdrflag;
6477c478bd9Sstevel@tonic-gate static	int	charset_type = 0;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate static	u_longlong_t	xhdr_flgs;	/* Bits set determine which items */
6507c478bd9Sstevel@tonic-gate 					/*   need to be in extended header. */
6517c478bd9Sstevel@tonic-gate #define	_X_DEVMAJOR	0x1
6527c478bd9Sstevel@tonic-gate #define	_X_DEVMINOR	0x2
6537c478bd9Sstevel@tonic-gate #define	_X_GID		0x4
6547c478bd9Sstevel@tonic-gate #define	_X_GNAME	0x8
6557c478bd9Sstevel@tonic-gate #define	_X_LINKPATH	0x10
6567c478bd9Sstevel@tonic-gate #define	_X_PATH		0x20
6577c478bd9Sstevel@tonic-gate #define	_X_SIZE		0x40
6587c478bd9Sstevel@tonic-gate #define	_X_UID		0x80
6597c478bd9Sstevel@tonic-gate #define	_X_UNAME	0x100
6607c478bd9Sstevel@tonic-gate #define	_X_ATIME	0x200
6617c478bd9Sstevel@tonic-gate #define	_X_CTIME	0x400
6627c478bd9Sstevel@tonic-gate #define	_X_MTIME	0x800
663123523f8Sas158974 #define	_X_XHDR		0x1000	/* Bit flag that determines whether 'X' */
664123523f8Sas158974 				/* typeflag was followed by 'A' or non 'A' */
665123523f8Sas158974 				/* typeflag. */
6667c478bd9Sstevel@tonic-gate #define	_X_LAST		0x40000000
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate #define	PID_MAX_DIGITS		(10 * sizeof (pid_t) / 4)
6697c478bd9Sstevel@tonic-gate #define	TIME_MAX_DIGITS		(10 * sizeof (time_t) / 4)
6707c478bd9Sstevel@tonic-gate #define	LONG_MAX_DIGITS		(10 * sizeof (long) / 4)
6717c478bd9Sstevel@tonic-gate #define	ULONGLONG_MAX_DIGITS	(10 * sizeof (u_longlong_t) / 4)
6727c478bd9Sstevel@tonic-gate /*
6737c478bd9Sstevel@tonic-gate  * UTF_8 encoding requires more space than the current codeset equivalent.
6747c478bd9Sstevel@tonic-gate  * Currently a factor of 2-3 would suffice, but it is possible for a factor
6757c478bd9Sstevel@tonic-gate  * of 6 to be needed in the future, so for saftey, we use that here.
6767c478bd9Sstevel@tonic-gate  */
6777c478bd9Sstevel@tonic-gate #define	UTF_8_FACTOR	6
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate static	u_longlong_t	xhdr_count = 0;
6807c478bd9Sstevel@tonic-gate static char		xhdr_dirname[PRESIZ + 1];
6817c478bd9Sstevel@tonic-gate static char		pidchars[PID_MAX_DIGITS + 1];
6827c478bd9Sstevel@tonic-gate static char		*tchar = "";		/* null linkpath */
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate static	char	local_path[UTF_8_FACTOR * PATH_MAX + 1];
6857c478bd9Sstevel@tonic-gate static	char	local_linkpath[UTF_8_FACTOR * PATH_MAX + 1];
6867c478bd9Sstevel@tonic-gate static	char	local_gname[UTF_8_FACTOR * _POSIX_NAME_MAX + 1];
6877c478bd9Sstevel@tonic-gate static	char	local_uname[UTF_8_FACTOR * _POSIX_NAME_MAX + 1];
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate /*
6907c478bd9Sstevel@tonic-gate  * The following mechanism is provided to allow us to debug tar in complicated
6917c478bd9Sstevel@tonic-gate  * situations, like when it is part of a pipe.  The idea is that you compile
69236802407SRich Burridge  * with -DWAITAROUND defined, and then add the 'D' function modifier to the
69336802407SRich Burridge  * target tar invocation, eg. "tar cDf tarfile file".  If stderr is available,
6947c478bd9Sstevel@tonic-gate  * it will tell you to which pid to attach the debugger; otherwise, use ps to
6957c478bd9Sstevel@tonic-gate  * find it.  Attach to the process from the debugger, and, *PRESTO*, you are
6967c478bd9Sstevel@tonic-gate  * there!
6977c478bd9Sstevel@tonic-gate  *
6987c478bd9Sstevel@tonic-gate  * Simply assign "waitaround = 0" once you attach to the process, and then
6997c478bd9Sstevel@tonic-gate  * proceed from there as usual.
7007c478bd9Sstevel@tonic-gate  */
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
7037c478bd9Sstevel@tonic-gate int waitaround = 0;		/* wait for rendezvous with the debugger */
7047c478bd9Sstevel@tonic-gate #endif
7057c478bd9Sstevel@tonic-gate 
70636802407SRich Burridge #define	BZIP		"/usr/bin/bzip2"
70736802407SRich Burridge #define	GZIP		"/usr/bin/gzip"
70836802407SRich Burridge #define	COMPRESS	"/usr/bin/compress"
70936802407SRich Burridge #define	BZCAT		"/usr/bin/bzcat"
71036802407SRich Burridge #define	GZCAT		"/usr/bin/gzcat"
71136802407SRich Burridge #define	ZCAT		"/usr/bin/zcat"
71236802407SRich Burridge #define	GS		8		/* number of valid 'gzip' sufixes */
71336802407SRich Burridge #define	BS		4		/* number of valid 'bzip2' sufixes */
71436802407SRich Burridge 
71536802407SRich Burridge static	char		*compress_opt; 	/* compression type */
71636802407SRich Burridge 
71736802407SRich Burridge static	char		*gsuffix[] = {".gz", "-gz", ".z", "-z", "_z", ".Z",
71836802407SRich Burridge 			".tgz", ".taz"};
71936802407SRich Burridge static	char		*bsuffix[] = {".bz2", ".bz", ".tbz2", ".tbz"};
72036802407SRich Burridge static	char		*suffix;
72136802407SRich Burridge 
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate int
7247c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
7257c478bd9Sstevel@tonic-gate {
7267c478bd9Sstevel@tonic-gate 	char		*cp;
7277c478bd9Sstevel@tonic-gate 	char		*tmpdirp;
7287c478bd9Sstevel@tonic-gate 	pid_t		thispid;
72936802407SRich Burridge 	pid_t		pid;
73036802407SRich Burridge 	int		wstat;
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
7337c478bd9Sstevel@tonic-gate 	int	tbl_cnt = 0;
7347c478bd9Sstevel@tonic-gate 	sysv3_env = getenv("SYSV3");
7357c478bd9Sstevel@tonic-gate #endif
7367c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
7377c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
7387c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
7397c478bd9Sstevel@tonic-gate #endif
7407c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
7417c478bd9Sstevel@tonic-gate 	if (argc < 2)
7427c478bd9Sstevel@tonic-gate 		usage();
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	tfile = NULL;
745da6c28aaSamw 	if ((myname = strdup(argv[0])) == NULL) {
746da6c28aaSamw 		(void) fprintf(stderr, gettext(
747da6c28aaSamw 		    "tar: cannot allocate program name\n"));
748da6c28aaSamw 		exit(1);
749da6c28aaSamw 	}
7507c478bd9Sstevel@tonic-gate 
7513d63ea05Sas145665 	if (init_yes() < 0) {
7523d63ea05Sas145665 		(void) fprintf(stderr, gettext(ERR_MSG_INIT_YES),
7533d63ea05Sas145665 		    strerror(errno));
7543d63ea05Sas145665 		exit(2);
7553d63ea05Sas145665 	}
7563d63ea05Sas145665 
7577c478bd9Sstevel@tonic-gate 	/*
7587c478bd9Sstevel@tonic-gate 	 *  For XPG4 compatibility, we must be able to accept the "--"
7597c478bd9Sstevel@tonic-gate 	 *  argument normally recognized by getopt; it is used to delimit
7607c478bd9Sstevel@tonic-gate 	 *  the end opt the options section, and so can only appear in
7617c478bd9Sstevel@tonic-gate 	 *  the position of the first argument.  We simply skip it.
7627c478bd9Sstevel@tonic-gate 	 */
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	if (strcmp(argv[1], "--") == 0) {
7657c478bd9Sstevel@tonic-gate 		argv++;
7667c478bd9Sstevel@tonic-gate 		argc--;
7677c478bd9Sstevel@tonic-gate 		if (argc < 3)
7687c478bd9Sstevel@tonic-gate 			usage();
7697c478bd9Sstevel@tonic-gate 	}
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	argv[argc] = NULL;
7727c478bd9Sstevel@tonic-gate 	argv++;
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	/*
7757c478bd9Sstevel@tonic-gate 	 * Set up default values.
7767c478bd9Sstevel@tonic-gate 	 * Search the operand string looking for the first digit or an 'f'.
7777c478bd9Sstevel@tonic-gate 	 * If you find a digit, use the 'archive#' entry in DEF_FILE.
7787c478bd9Sstevel@tonic-gate 	 * If 'f' is given, bypass looking in DEF_FILE altogether.
7797c478bd9Sstevel@tonic-gate 	 * If no digit or 'f' is given, still look in DEF_FILE but use '0'.
7807c478bd9Sstevel@tonic-gate 	 */
7817c478bd9Sstevel@tonic-gate 	if ((usefile = getenv("TAPE")) == (char *)NULL) {
7827c478bd9Sstevel@tonic-gate 		for (cp = *argv; *cp; ++cp)
7837c478bd9Sstevel@tonic-gate 			if (isdigit(*cp) || *cp == 'f')
7847c478bd9Sstevel@tonic-gate 				break;
7857c478bd9Sstevel@tonic-gate 		if (*cp != 'f') {
7867c478bd9Sstevel@tonic-gate 			archive[7] = (*cp)? *cp: '0';
7877c478bd9Sstevel@tonic-gate 			if (!(defaults_used = defset(archive))) {
7887c478bd9Sstevel@tonic-gate 				usefile = NULL;
7897c478bd9Sstevel@tonic-gate 				nblock = 1;
7907c478bd9Sstevel@tonic-gate 				blocklim = 0;
7917c478bd9Sstevel@tonic-gate 				NotTape = 0;
7927c478bd9Sstevel@tonic-gate 			}
7937c478bd9Sstevel@tonic-gate 		}
7947c478bd9Sstevel@tonic-gate 	}
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	for (cp = *argv++; *cp; cp++)
7977c478bd9Sstevel@tonic-gate 		switch (*cp) {
7987c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
79936802407SRich Burridge 		case 'D':
8007c478bd9Sstevel@tonic-gate 			/* rendezvous with the debugger */
8017c478bd9Sstevel@tonic-gate 			waitaround = 1;
8027c478bd9Sstevel@tonic-gate 			break;
8037c478bd9Sstevel@tonic-gate #endif
8047c478bd9Sstevel@tonic-gate 		case 'f':
8057c478bd9Sstevel@tonic-gate 			assert_string(*argv, gettext(
8067c478bd9Sstevel@tonic-gate 			    "tar: tarfile must be specified with 'f' "
8077c478bd9Sstevel@tonic-gate 			    "function modifier\n"));
8087c478bd9Sstevel@tonic-gate 			usefile = *argv++;
8097c478bd9Sstevel@tonic-gate 			break;
8107c478bd9Sstevel@tonic-gate 		case 'F':
8117c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
8127c478bd9Sstevel@tonic-gate 			if (sysv3_env) {
8137c478bd9Sstevel@tonic-gate 				assert_string(*argv, gettext(
8147c478bd9Sstevel@tonic-gate 				    "tar: 'F' requires a file name\n"));
8157c478bd9Sstevel@tonic-gate 				Filefile = *argv++;
8167c478bd9Sstevel@tonic-gate 				Fileflag++;
8177c478bd9Sstevel@tonic-gate 			} else
8187c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
8197c478bd9Sstevel@tonic-gate 				Fflag++;
8207c478bd9Sstevel@tonic-gate 			break;
8217c478bd9Sstevel@tonic-gate 		case 'c':
8227c478bd9Sstevel@tonic-gate 			cflag++;
8237c478bd9Sstevel@tonic-gate 			rflag++;
8247c478bd9Sstevel@tonic-gate 			update = 1;
8257c478bd9Sstevel@tonic-gate 			break;
8267c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
8277c478bd9Sstevel@tonic-gate 		case '@':
8287c478bd9Sstevel@tonic-gate 			atflag++;
8297c478bd9Sstevel@tonic-gate 			break;
830da6c28aaSamw #endif	/* O_XATTR */
831da6c28aaSamw #if defined(_PC_SATTR_ENABLED)
832da6c28aaSamw 		case '/':
833da6c28aaSamw 			saflag++;
834da6c28aaSamw 			break;
835da6c28aaSamw #endif	/* _PC_SATTR_ENABLED */
8367c478bd9Sstevel@tonic-gate 		case 'u':
8377c478bd9Sstevel@tonic-gate 			uflag++;	/* moved code after signals caught */
8387c478bd9Sstevel@tonic-gate 			rflag++;
8397c478bd9Sstevel@tonic-gate 			update = 2;
8407c478bd9Sstevel@tonic-gate 			break;
8417c478bd9Sstevel@tonic-gate 		case 'r':
8427c478bd9Sstevel@tonic-gate 			rflag++;
8437c478bd9Sstevel@tonic-gate 			update = 2;
8447c478bd9Sstevel@tonic-gate 			break;
8457c478bd9Sstevel@tonic-gate 		case 'v':
8467c478bd9Sstevel@tonic-gate 			vflag++;
8477c478bd9Sstevel@tonic-gate 			break;
8487c478bd9Sstevel@tonic-gate 		case 'w':
8497c478bd9Sstevel@tonic-gate 			wflag++;
8507c478bd9Sstevel@tonic-gate 			break;
8517c478bd9Sstevel@tonic-gate 		case 'x':
8527c478bd9Sstevel@tonic-gate 			xflag++;
8537c478bd9Sstevel@tonic-gate 			break;
8547c478bd9Sstevel@tonic-gate 		case 'X':
8557c478bd9Sstevel@tonic-gate 			assert_string(*argv, gettext(
8567c478bd9Sstevel@tonic-gate 			    "tar: exclude file must be specified with 'X' "
8577c478bd9Sstevel@tonic-gate 			    "function modifier\n"));
8587c478bd9Sstevel@tonic-gate 			Xflag = 1;
8597c478bd9Sstevel@tonic-gate 			Xfile = *argv++;
8607c478bd9Sstevel@tonic-gate 			build_table(exclude_tbl, Xfile);
8617c478bd9Sstevel@tonic-gate 			break;
8627c478bd9Sstevel@tonic-gate 		case 't':
8637c478bd9Sstevel@tonic-gate 			tflag++;
8647c478bd9Sstevel@tonic-gate 			break;
8657c478bd9Sstevel@tonic-gate 		case 'm':
8667c478bd9Sstevel@tonic-gate 			mflag++;
8677c478bd9Sstevel@tonic-gate 			break;
8687c478bd9Sstevel@tonic-gate 		case 'p':
8697c478bd9Sstevel@tonic-gate 			pflag++;
8707c478bd9Sstevel@tonic-gate 			break;
8717c478bd9Sstevel@tonic-gate 		case 'D':
8727c478bd9Sstevel@tonic-gate 			Dflag++;
8737c478bd9Sstevel@tonic-gate 			break;
8747c478bd9Sstevel@tonic-gate 		case '-':
8757c478bd9Sstevel@tonic-gate 			/* ignore this silently */
8767c478bd9Sstevel@tonic-gate 			break;
8777c478bd9Sstevel@tonic-gate 		case '0':	/* numeric entries used only for defaults */
8787c478bd9Sstevel@tonic-gate 		case '1':
8797c478bd9Sstevel@tonic-gate 		case '2':
8807c478bd9Sstevel@tonic-gate 		case '3':
8817c478bd9Sstevel@tonic-gate 		case '4':
8827c478bd9Sstevel@tonic-gate 		case '5':
8837c478bd9Sstevel@tonic-gate 		case '6':
8847c478bd9Sstevel@tonic-gate 		case '7':
8857c478bd9Sstevel@tonic-gate 			break;
8867c478bd9Sstevel@tonic-gate 		case 'b':
8877c478bd9Sstevel@tonic-gate 			assert_string(*argv, gettext(
8887c478bd9Sstevel@tonic-gate 			    "tar: blocking factor must be specified "
8897c478bd9Sstevel@tonic-gate 			    "with 'b' function modifier\n"));
8907c478bd9Sstevel@tonic-gate 			bflag++;
8917c478bd9Sstevel@tonic-gate 			nblock = bcheck(*argv++);
8927c478bd9Sstevel@tonic-gate 			break;
8937c478bd9Sstevel@tonic-gate 		case 'q':
8947c478bd9Sstevel@tonic-gate 			qflag++;
8957c478bd9Sstevel@tonic-gate 			break;
8967c478bd9Sstevel@tonic-gate 		case 'k':
8977c478bd9Sstevel@tonic-gate 			assert_string(*argv, gettext(
8987c478bd9Sstevel@tonic-gate 			    "tar: size value must be specified with 'k' "
8997c478bd9Sstevel@tonic-gate 			    "function modifier\n"));
9007c478bd9Sstevel@tonic-gate 			kflag++;
9017c478bd9Sstevel@tonic-gate 			blocklim = kcheck(*argv++);
9027c478bd9Sstevel@tonic-gate 			break;
9037c478bd9Sstevel@tonic-gate 		case 'n':		/* not a magtape (instead of 'k') */
9047c478bd9Sstevel@tonic-gate 			NotTape++;	/* assume non-magtape */
9057c478bd9Sstevel@tonic-gate 			break;
9067c478bd9Sstevel@tonic-gate 		case 'l':
9077c478bd9Sstevel@tonic-gate 			linkerrok++;
9087c478bd9Sstevel@tonic-gate 			break;
9097c478bd9Sstevel@tonic-gate 		case 'e':
9107c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
9117c478bd9Sstevel@tonic-gate 			/* If sysv3 IS set, don't be as verbose */
9127c478bd9Sstevel@tonic-gate 			if (!sysv3_env)
9137c478bd9Sstevel@tonic-gate #endif	/* _iBCS2 */
9147c478bd9Sstevel@tonic-gate 				errflag++;
9157c478bd9Sstevel@tonic-gate 			eflag++;
9167c478bd9Sstevel@tonic-gate 			break;
9177c478bd9Sstevel@tonic-gate 		case 'o':
9187c478bd9Sstevel@tonic-gate 			oflag++;
9197c478bd9Sstevel@tonic-gate 			break;
9207c478bd9Sstevel@tonic-gate 		case 'h':
9217c478bd9Sstevel@tonic-gate 			hflag++;
9227c478bd9Sstevel@tonic-gate 			break;
9237c478bd9Sstevel@tonic-gate 		case 'i':
9247c478bd9Sstevel@tonic-gate 			iflag++;
9257c478bd9Sstevel@tonic-gate 			break;
9267c478bd9Sstevel@tonic-gate 		case 'B':
9277c478bd9Sstevel@tonic-gate 			Bflag++;
9287c478bd9Sstevel@tonic-gate 			break;
9297c478bd9Sstevel@tonic-gate 		case 'P':
9307c478bd9Sstevel@tonic-gate 			Pflag++;
9317c478bd9Sstevel@tonic-gate 			break;
9327c478bd9Sstevel@tonic-gate 		case 'E':
9337c478bd9Sstevel@tonic-gate 			Eflag++;
9347c478bd9Sstevel@tonic-gate 			Pflag++;	/* Only POSIX archive made */
9357c478bd9Sstevel@tonic-gate 			break;
93645916cd2Sjpk 		case 'T':
93745916cd2Sjpk 			Tflag++;	/* Handle Trusted Extensions attrs */
93845916cd2Sjpk 			pflag++;	/* also set flag for ACL */
93945916cd2Sjpk 			break;
94036802407SRich Burridge 		case 'j':		/* compession "bzip2" */
94136802407SRich Burridge 			jflag++;
94236802407SRich Burridge 			break;
94336802407SRich Burridge 		case 'z':		/* compression "gzip" */
94436802407SRich Burridge 			zflag++;
94536802407SRich Burridge 			break;
94636802407SRich Burridge 		case 'Z':		/* compression "compress" */
94736802407SRich Burridge 			Zflag++;
94836802407SRich Burridge 			break;
9497c478bd9Sstevel@tonic-gate 		default:
9507c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
9517c478bd9Sstevel@tonic-gate 			"tar: %c: unknown function modifier\n"), *cp);
9527c478bd9Sstevel@tonic-gate 			usage();
9537c478bd9Sstevel@tonic-gate 		}
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
9567c478bd9Sstevel@tonic-gate 	if (Xflag && Fileflag) {
9577c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
9587c478bd9Sstevel@tonic-gate 		"tar: specify only one of X or F.\n"));
9597c478bd9Sstevel@tonic-gate 		usage();
9607c478bd9Sstevel@tonic-gate 	}
9617c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	if (!rflag && !xflag && !tflag)
9647c478bd9Sstevel@tonic-gate 		usage();
9657c478bd9Sstevel@tonic-gate 	if ((rflag && xflag) || (xflag && tflag) || (rflag && tflag)) {
9667c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
9677c478bd9Sstevel@tonic-gate 		"tar: specify only one of [ctxru].\n"));
9687c478bd9Sstevel@tonic-gate 		usage();
9697c478bd9Sstevel@tonic-gate 	}
97036802407SRich Burridge 	if (cflag) {
97136802407SRich Burridge 		if ((zflag && jflag) || (zflag && Zflag) ||
97236802407SRich Burridge 		    (jflag && Zflag)) {
97336802407SRich Burridge 			(void) fprintf(stderr, gettext(
97436802407SRich Burridge 			    "tar: specify only one of [jzZ] to "
97536802407SRich Burridge 			    "create a compressed file.\n"));
97636802407SRich Burridge 			usage();
97736802407SRich Burridge 		}
97836802407SRich Burridge 	}
97945916cd2Sjpk 	/* Trusted Extensions attribute handling */
98045916cd2Sjpk 	if (Tflag && ((getzoneid() != GLOBAL_ZONEID) ||
98145916cd2Sjpk 	    !is_system_labeled())) {
98245916cd2Sjpk 		(void) fprintf(stderr, gettext(
98345916cd2Sjpk 		"tar: the 'T' option is only available with "
98445916cd2Sjpk 		    "Trusted Extensions\nand must be run from "
98545916cd2Sjpk 		    "the global zone.\n"));
98645916cd2Sjpk 		usage();
98745916cd2Sjpk 	}
9887c478bd9Sstevel@tonic-gate 	if (cflag && *argv == NULL && Filefile == NULL)
9897c478bd9Sstevel@tonic-gate 		fatal(gettext("Missing filenames"));
9907c478bd9Sstevel@tonic-gate 	if (usefile == NULL)
9917c478bd9Sstevel@tonic-gate 		fatal(gettext("device argument required"));
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	/* alloc a buffer of the right size */
9947c478bd9Sstevel@tonic-gate 	if ((tbuf = (union hblock *)
9957c478bd9Sstevel@tonic-gate 	    calloc(sizeof (union hblock) * nblock, sizeof (char))) ==
9967c478bd9Sstevel@tonic-gate 	    (union hblock *)NULL) {
9977c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
9987c478bd9Sstevel@tonic-gate 		"tar: cannot allocate physio buffer\n"));
9997c478bd9Sstevel@tonic-gate 		exit(1);
10007c478bd9Sstevel@tonic-gate 	}
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	if ((xrec_ptr = malloc(xrec_size)) == NULL) {
10037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
10047c478bd9Sstevel@tonic-gate 		    "tar: cannot allocate extended header buffer\n"));
10057c478bd9Sstevel@tonic-gate 		exit(1);
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
10097c478bd9Sstevel@tonic-gate 	if (waitaround) {
10107c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Rendezvous with tar on pid"
10117c478bd9Sstevel@tonic-gate 		    " %d\n"), getpid());
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 		while (waitaround) {
10147c478bd9Sstevel@tonic-gate 			(void) sleep(10);
10157c478bd9Sstevel@tonic-gate 		}
10167c478bd9Sstevel@tonic-gate 	}
10177c478bd9Sstevel@tonic-gate #endif
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	thispid = getpid();
10207c478bd9Sstevel@tonic-gate 	(void) sprintf(pidchars, "%ld", thispid);
10217c478bd9Sstevel@tonic-gate 	thispid = strlen(pidchars);
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	if ((tmpdirp = getenv("TMPDIR")) == (char *)NULL)
10247c478bd9Sstevel@tonic-gate 		(void) strcpy(xhdr_dirname, "/tmp");
10257c478bd9Sstevel@tonic-gate 	else {
10267c478bd9Sstevel@tonic-gate 		/*
10277c478bd9Sstevel@tonic-gate 		 * Make sure that dir is no longer than what can
10287c478bd9Sstevel@tonic-gate 		 * fit in the prefix part of the header.
10297c478bd9Sstevel@tonic-gate 		 */
10307c478bd9Sstevel@tonic-gate 		if (strlen(tmpdirp) > (size_t)(PRESIZ - thispid - 12)) {
10317c478bd9Sstevel@tonic-gate 			(void) strcpy(xhdr_dirname, "/tmp");
10327c478bd9Sstevel@tonic-gate 			if ((vflag > 0) && (Eflag > 0))
10337c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
10347c478bd9Sstevel@tonic-gate 				    "Ignoring TMPDIR\n"));
10357c478bd9Sstevel@tonic-gate 		} else
10367c478bd9Sstevel@tonic-gate 			(void) strcpy(xhdr_dirname, tmpdirp);
10377c478bd9Sstevel@tonic-gate 	}
10387c478bd9Sstevel@tonic-gate 	(void) strcat(xhdr_dirname, "/PaxHeaders.");
10397c478bd9Sstevel@tonic-gate 	(void) strcat(xhdr_dirname, pidchars);
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	if (rflag) {
104236802407SRich Burridge 		if (cflag && usefile != NULL)  {
104336802407SRich Burridge 			/* Set the compression type */
104436802407SRich Burridge 			if (jflag) {
104536802407SRich Burridge 				compress_opt = compress_malloc(strlen(BZIP)
104636802407SRich Burridge 				    + 1);
104736802407SRich Burridge 				(void) strcpy(compress_opt, BZIP);
104836802407SRich Burridge 			} else if (zflag) {
104936802407SRich Burridge 				compress_opt = compress_malloc(strlen(GZIP)
105036802407SRich Burridge 				    + 1);
105136802407SRich Burridge 				(void) strcpy(compress_opt, GZIP);
105236802407SRich Burridge 			} else if (Zflag) {
105336802407SRich Burridge 				compress_opt =
105436802407SRich Burridge 				    compress_malloc(strlen(COMPRESS) + 1);
105536802407SRich Burridge 				(void) strcpy(compress_opt, COMPRESS);
105636802407SRich Burridge 			}
105736802407SRich Burridge 		} else {
105836802407SRich Burridge 			/*
105936802407SRich Burridge 			 * Decompress if the file is compressed for
106036802407SRich Burridge 			 * an update or replace.
106136802407SRich Burridge 			 */
106236802407SRich Burridge 			if (strcmp(usefile, "-") != 0) {
106336802407SRich Burridge 				check_compression();
106436802407SRich Burridge 				if (compress_opt != NULL) {
106536802407SRich Burridge 					decompress_file();
106636802407SRich Burridge 				}
106736802407SRich Burridge 			}
106836802407SRich Burridge 		}
106936802407SRich Burridge 
10707c478bd9Sstevel@tonic-gate 		if (cflag && tfile != NULL)
10717c478bd9Sstevel@tonic-gate 			usage();
10727c478bd9Sstevel@tonic-gate 		if (signal(SIGINT, SIG_IGN) != SIG_IGN)
10737c478bd9Sstevel@tonic-gate 			(void) signal(SIGINT, onintr);
10747c478bd9Sstevel@tonic-gate 		if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
10757c478bd9Sstevel@tonic-gate 			(void) signal(SIGHUP, onhup);
10767c478bd9Sstevel@tonic-gate 		if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
10777c478bd9Sstevel@tonic-gate 			(void) signal(SIGQUIT, onquit);
10787c478bd9Sstevel@tonic-gate 		if (uflag) {
10797c478bd9Sstevel@tonic-gate 			int tnum;
108031a2de35SRich Burridge 			struct stat sbuf;
108131a2de35SRich Burridge 
108231a2de35SRich Burridge 			tmpdir = getenv("TMPDIR");
108331a2de35SRich Burridge 			/*
108431a2de35SRich Burridge 			 * If the name is invalid or this isn't a directory,
108531a2de35SRich Burridge 			 * or the directory is not writable, then reset to
108631a2de35SRich Burridge 			 * a default temporary directory.
108731a2de35SRich Burridge 			 */
108831a2de35SRich Burridge 			if (tmpdir == NULL || *tmpdir == '\0' ||
108931a2de35SRich Burridge 			    (strlen(tmpdir) + strlen(tmp_suffix)) > PATH_MAX) {
109031a2de35SRich Burridge 				tmpdir = "/tmp";
109131a2de35SRich Burridge 			} else if (stat(tmpdir, &sbuf) < 0 ||
109231a2de35SRich Burridge 			    (sbuf.st_mode & S_IFMT) != S_IFDIR ||
109331a2de35SRich Burridge 			    (sbuf.st_mode & S_IWRITE) == 0) {
109431a2de35SRich Burridge 				tmpdir = "/tmp";
109531a2de35SRich Burridge 			}
109631a2de35SRich Burridge 
109731a2de35SRich Burridge 			if ((tname = calloc(1, strlen(tmpdir) +
109831a2de35SRich Burridge 			    strlen(tmp_suffix) + 1)) == NULL) {
109931a2de35SRich Burridge 				vperror(1, gettext("tar: out of memory, "
110031a2de35SRich Burridge 				    "cannot create temporary file\n"));
110131a2de35SRich Burridge 			}
110231a2de35SRich Burridge 			(void) strcpy(tname, tmpdir);
110331a2de35SRich Burridge 			(void) strcat(tname, tmp_suffix);
110431a2de35SRich Burridge 
11057c478bd9Sstevel@tonic-gate 			if ((tnum = mkstemp(tname)) == -1)
11067c478bd9Sstevel@tonic-gate 				vperror(1, "%s", tname);
11077c478bd9Sstevel@tonic-gate 			if ((tfile = fdopen(tnum, "w")) == NULL)
11087c478bd9Sstevel@tonic-gate 				vperror(1, "%s", tname);
11097c478bd9Sstevel@tonic-gate 		}
11107c478bd9Sstevel@tonic-gate 		if (strcmp(usefile, "-") == 0) {
11117c478bd9Sstevel@tonic-gate 			if (cflag == 0)
11127c478bd9Sstevel@tonic-gate 				fatal(gettext(
11137c478bd9Sstevel@tonic-gate 				"can only create standard output archives."));
11147c478bd9Sstevel@tonic-gate 			vfile = stderr;
11157c478bd9Sstevel@tonic-gate 			mt = dup(1);
11167c478bd9Sstevel@tonic-gate 			++bflag;
11177c478bd9Sstevel@tonic-gate 		} else {
11187c478bd9Sstevel@tonic-gate 			if (cflag)
11197c478bd9Sstevel@tonic-gate 				mt = open(usefile,
11207c478bd9Sstevel@tonic-gate 				    O_RDWR|O_CREAT|O_TRUNC, 0666);
11217c478bd9Sstevel@tonic-gate 			else
11227c478bd9Sstevel@tonic-gate 				mt = open(usefile, O_RDWR);
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 			if (mt < 0) {
11257c478bd9Sstevel@tonic-gate 				if (cflag == 0 || (mt =  creat(usefile, 0666))
11267c478bd9Sstevel@tonic-gate 				    < 0)
11277c478bd9Sstevel@tonic-gate 				vperror(1, "%s", usefile);
11287c478bd9Sstevel@tonic-gate 			}
11297c478bd9Sstevel@tonic-gate 		}
11307c478bd9Sstevel@tonic-gate 		/* Get inode and device number of output file */
11317c478bd9Sstevel@tonic-gate 		(void) fstat(mt, &stbuf);
11327c478bd9Sstevel@tonic-gate 		mt_ino = stbuf.st_ino;
11337c478bd9Sstevel@tonic-gate 		mt_dev = stbuf.st_dev;
11347c478bd9Sstevel@tonic-gate 		mt_devtype = stbuf.st_mode & S_IFMT;
11357c478bd9Sstevel@tonic-gate 		NotTape = !istape(mt, mt_devtype);
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 		if (rflag && !cflag && (mt_devtype == S_IFIFO))
11387c478bd9Sstevel@tonic-gate 			fatal(gettext("cannot append to pipe or FIFO."));
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 		if (Aflag && vflag)
11417c478bd9Sstevel@tonic-gate 			(void) printf(
11427c478bd9Sstevel@tonic-gate 			gettext("Suppressing absolute pathnames\n"));
114336802407SRich Burridge 		if (cflag && compress_opt != NULL) {
114436802407SRich Burridge 			pid = compress_file();
114536802407SRich Burridge 			wait_pid(pid);
114636802407SRich Burridge 		}
11477c478bd9Sstevel@tonic-gate 		dorep(argv);
114836802407SRich Burridge 		if (rflag && !cflag && (compress_opt != NULL))
114936802407SRich Burridge 			compress_back();
11507c478bd9Sstevel@tonic-gate 	} else if (xflag || tflag) {
11517c478bd9Sstevel@tonic-gate 		/*
11527c478bd9Sstevel@tonic-gate 		 * for each argument, check to see if there is a "-I file" pair.
11537c478bd9Sstevel@tonic-gate 		 * if so, move the 3rd argument into "-I"'s place, build_table()
11547c478bd9Sstevel@tonic-gate 		 * using "file"'s name and increment argc one (the second
11557c478bd9Sstevel@tonic-gate 		 * increment appears in the for loop) which removes the two
11567c478bd9Sstevel@tonic-gate 		 * args "-I" and "file" from the argument vector.
11577c478bd9Sstevel@tonic-gate 		 */
11587c478bd9Sstevel@tonic-gate 		for (argc = 0; argv[argc]; argc++) {
11597c478bd9Sstevel@tonic-gate 			if (strcmp(argv[argc], "-I") == 0) {
11607c478bd9Sstevel@tonic-gate 				if (!argv[argc+1]) {
11617c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
11627c478bd9Sstevel@tonic-gate 					"tar: missing argument for -I flag\n"));
11637c478bd9Sstevel@tonic-gate 					done(2);
11647c478bd9Sstevel@tonic-gate 				} else {
11657c478bd9Sstevel@tonic-gate 					Iflag = 1;
11667c478bd9Sstevel@tonic-gate 					argv[argc] = argv[argc+2];
11677c478bd9Sstevel@tonic-gate 					build_table(include_tbl, argv[++argc]);
11687c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
11697c478bd9Sstevel@tonic-gate 					if (Fileflag) {
11707c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(
11717c478bd9Sstevel@tonic-gate 						"tar: only one of I or F.\n"));
11727c478bd9Sstevel@tonic-gate 						usage();
11737c478bd9Sstevel@tonic-gate 					}
11747c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 				}
11777c478bd9Sstevel@tonic-gate 			}
11787c478bd9Sstevel@tonic-gate 		}
11797c478bd9Sstevel@tonic-gate 		if (strcmp(usefile, "-") == 0) {
11807c478bd9Sstevel@tonic-gate 			mt = dup(0);
11817c478bd9Sstevel@tonic-gate 			++bflag;
11827c478bd9Sstevel@tonic-gate 			/* try to recover from short reads when reading stdin */
11837c478bd9Sstevel@tonic-gate 			++Bflag;
11847c478bd9Sstevel@tonic-gate 		} else if ((mt = open(usefile, 0)) < 0)
11857c478bd9Sstevel@tonic-gate 			vperror(1, "%s", usefile);
11867c478bd9Sstevel@tonic-gate 
118736802407SRich Burridge 		/* Decompress if the file is compressed */
118836802407SRich Burridge 
118936802407SRich Burridge 		if (strcmp(usefile, "-") != 0) {
119036802407SRich Burridge 			check_compression();
119136802407SRich Burridge 			if (compress_opt != NULL) {
119236802407SRich Burridge 				pid = uncompress_file();
119336802407SRich Burridge 				wait_pid(pid);
119436802407SRich Burridge 			}
119536802407SRich Burridge 		}
11967c478bd9Sstevel@tonic-gate 		if (xflag) {
11977c478bd9Sstevel@tonic-gate 			if (Aflag && vflag)
1198eace40a5Sceastha 				(void) printf(gettext(
1199eace40a5Sceastha 				    "Suppressing absolute pathnames.\n"));
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
12027c478bd9Sstevel@tonic-gate 			doxtract(argv, tbl_cnt);
12037c478bd9Sstevel@tonic-gate #else
12047c478bd9Sstevel@tonic-gate 			doxtract(argv);
12057c478bd9Sstevel@tonic-gate #endif
12067c478bd9Sstevel@tonic-gate 		} else if (tflag)
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
12097c478bd9Sstevel@tonic-gate 			dotable(argv, tbl_cnt);
12107c478bd9Sstevel@tonic-gate #else
12117c478bd9Sstevel@tonic-gate 			dotable(argv);
12127c478bd9Sstevel@tonic-gate #endif
12137c478bd9Sstevel@tonic-gate 	}
12147c478bd9Sstevel@tonic-gate 	else
12157c478bd9Sstevel@tonic-gate 		usage();
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 	done(Errflg);
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 	/* Not reached:  keep compiler quiet */
12207c478bd9Sstevel@tonic-gate 	return (1);
12217c478bd9Sstevel@tonic-gate }
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate static void
12247c478bd9Sstevel@tonic-gate usage(void)
12257c478bd9Sstevel@tonic-gate {
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
12287c478bd9Sstevel@tonic-gate 	if (sysv3_env) {
12297c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
12307c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
1231da6c28aaSamw #if defined(_PC_SATTR_ENABLED)
1232da6c28aaSamw 		"Usage: tar {c|r|t|u|x}[BDeEhilmnopPqTvw@/[0-7]][bfFk][X...] "
1233da6c28aaSamw #else
123445916cd2Sjpk 		"Usage: tar {c|r|t|u|x}[BDeEhilmnopPqTvw@[0-7]][bfFk][X...] "
1235da6c28aaSamw #endif	/* _PC_SATTR_ENABLED */
12367c478bd9Sstevel@tonic-gate #else
123745916cd2Sjpk 		"Usage: tar {c|r|t|u|x}[BDeEhilmnopPqTvw[0-7]][bfFk][X...] "
1238da6c28aaSamw #endif	/* O_XATTR */
123936802407SRich Burridge 		"[j|z|Z] "
12407c478bd9Sstevel@tonic-gate 		"[blocksize] [tarfile] [filename] [size] [exclude-file...] "
12417c478bd9Sstevel@tonic-gate 		"{file | -I include-file | -C directory file}...\n"));
12427c478bd9Sstevel@tonic-gate 	} else
12437c478bd9Sstevel@tonic-gate #endif	/* _iBCS2 */
12447c478bd9Sstevel@tonic-gate 	{
12457c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
12467c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
1247da6c28aaSamw #if defined(_PC_SATTR_ENABLED)
1248da6c28aaSamw 		"Usage: tar {c|r|t|u|x}[BDeEFhilmnopPqTvw@/[0-7]][bfk][X...] "
1249da6c28aaSamw #else
125045916cd2Sjpk 		"Usage: tar {c|r|t|u|x}[BDeEFhilmnopPqTvw@[0-7]][bfk][X...] "
1251da6c28aaSamw #endif	/* _PC_SATTR_ENABLED */
12527c478bd9Sstevel@tonic-gate #else
125345916cd2Sjpk 		"Usage: tar {c|r|t|u|x}[BDeEFhilmnopPqTvw[0-7]][bfk][X...] "
1254da6c28aaSamw #endif	/* O_XATTR */
125536802407SRich Burridge 		"[j|z|Z] "
12567c478bd9Sstevel@tonic-gate 		"[blocksize] [tarfile] [size] [exclude-file...] "
12577c478bd9Sstevel@tonic-gate 		"{file | -I include-file | -C directory file}...\n"));
12587c478bd9Sstevel@tonic-gate 	}
12597c478bd9Sstevel@tonic-gate 	done(1);
12607c478bd9Sstevel@tonic-gate }
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate /*
12637c478bd9Sstevel@tonic-gate  * dorep - do "replacements"
12647c478bd9Sstevel@tonic-gate  *
12657c478bd9Sstevel@tonic-gate  *	Dorep is responsible for creating ('c'),  appending ('r')
12667c478bd9Sstevel@tonic-gate  *	and updating ('u');
12677c478bd9Sstevel@tonic-gate  */
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate static void
12707c478bd9Sstevel@tonic-gate dorep(char *argv[])
12717c478bd9Sstevel@tonic-gate {
12727c478bd9Sstevel@tonic-gate 	char *cp, *cp2, *p;
12737c478bd9Sstevel@tonic-gate 	char wdir[PATH_MAX+2], tempdir[PATH_MAX+2], *parent;
12747c478bd9Sstevel@tonic-gate 	char file[PATH_MAX*2], origdir[PATH_MAX+1];
12757c478bd9Sstevel@tonic-gate 	FILE *fp = (FILE *)NULL;
12767c478bd9Sstevel@tonic-gate 	FILE *ff = (FILE *)NULL;
12777c478bd9Sstevel@tonic-gate 	int archtype;
1278123523f8Sas158974 	int ret;
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 	if (!cflag) {
12827c478bd9Sstevel@tonic-gate 		xhdr_flgs = 0;
12837c478bd9Sstevel@tonic-gate 		getdir();			/* read header for next file */
12847c478bd9Sstevel@tonic-gate 		if (Xhdrflag > 0) {
12857c478bd9Sstevel@tonic-gate 			if (!Eflag)
12867c478bd9Sstevel@tonic-gate 				fatal(gettext("Archive contains extended"
12877c478bd9Sstevel@tonic-gate 				    " header.  -E flag required.\n"));
1288123523f8Sas158974 			ret = get_xdata();	/* Get extended header items */
12897c478bd9Sstevel@tonic-gate 						/*   and regular header */
12907c478bd9Sstevel@tonic-gate 		} else {
12917c478bd9Sstevel@tonic-gate 			if (Eflag)
12927c478bd9Sstevel@tonic-gate 				fatal(gettext("Archive contains no extended"
12937c478bd9Sstevel@tonic-gate 				    " header.  -E flag not allowed.\n"));
12947c478bd9Sstevel@tonic-gate 		}
12957c478bd9Sstevel@tonic-gate 		while (!endtape()) {		/* changed from a do while */
1296123523f8Sas158974 			setbytes_to_skip(&stbuf, ret);
12977c478bd9Sstevel@tonic-gate 			passtape();		/* skip the file data */
12987c478bd9Sstevel@tonic-gate 			if (term)
12997c478bd9Sstevel@tonic-gate 				done(Errflg);	/* received signal to stop */
13007c478bd9Sstevel@tonic-gate 			xhdr_flgs = 0;
13017c478bd9Sstevel@tonic-gate 			getdir();
13027c478bd9Sstevel@tonic-gate 			if (Xhdrflag > 0)
1303123523f8Sas158974 				ret = get_xdata();
1304123523f8Sas158974 		}
1305123523f8Sas158974 		if (ret == 0) {
1306123523f8Sas158974 			if ((dblock.dbuf.typeflag != 'A') &&
1307123523f8Sas158974 			    (xhdr_flgs != 0)) {
1308123523f8Sas158974 				load_info_from_xtarhdr(xhdr_flgs,
1309123523f8Sas158974 				    &Xtarhdr);
1310123523f8Sas158974 				xhdr_flgs |= _X_XHDR;
1311123523f8Sas158974 			}
13127c478bd9Sstevel@tonic-gate 		}
13137c478bd9Sstevel@tonic-gate 		backtape();			/* was called by endtape */
13147c478bd9Sstevel@tonic-gate 		if (tfile != NULL) {
131531a2de35SRich Burridge 			/*
131631a2de35SRich Burridge 			 * Buffer size is calculated to be the size of the
131731a2de35SRich Burridge 			 * tmpdir string, plus 6 times the size of the tname
131831a2de35SRich Burridge 			 * string, plus a value that is known to be greater
131931a2de35SRich Burridge 			 * than the command pipeline string.
132031a2de35SRich Burridge 			 */
132131a2de35SRich Burridge 			int buflen = strlen(tmpdir) + (6 * strlen(tname)) + 100;
132231a2de35SRich Burridge 			char *buf;
13237c478bd9Sstevel@tonic-gate 
132431a2de35SRich Burridge 			if ((buf = (char *)calloc(1, buflen)) == NULL) {
132531a2de35SRich Burridge 				vperror(1, gettext("tar: out of memory, "
132631a2de35SRich Burridge 				    "cannot create sort command file\n"));
132731a2de35SRich Burridge 			}
132831a2de35SRich Burridge 
132931a2de35SRich Burridge 			(void) snprintf(buf, buflen, "env 'TMPDIR=%s' "
133031a2de35SRich Burridge 			    "sort +0 -1 +1nr %s -o %s; awk '$1 "
13317c478bd9Sstevel@tonic-gate 			    "!= prev {print; prev=$1}' %s >%sX;mv %sX %s",
133231a2de35SRich Burridge 			    tmpdir, tname, tname, tname, tname, tname, tname);
13337c478bd9Sstevel@tonic-gate 			(void) fflush(tfile);
13347c478bd9Sstevel@tonic-gate 			(void) system(buf);
133531a2de35SRich Burridge 			free(buf);
13367c478bd9Sstevel@tonic-gate 			(void) freopen(tname, "r", tfile);
13377c478bd9Sstevel@tonic-gate 			(void) fstat(fileno(tfile), &stbuf);
13387c478bd9Sstevel@tonic-gate 			high = stbuf.st_size;
13397c478bd9Sstevel@tonic-gate 		}
13407c478bd9Sstevel@tonic-gate 	}
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 	dumping = 1;
13437c478bd9Sstevel@tonic-gate 	if (mulvol) {	/* SP-1 */
13447c478bd9Sstevel@tonic-gate 		if (nblock && (blocklim%nblock) != 0)
13457c478bd9Sstevel@tonic-gate 			fatal(gettext(
13467c478bd9Sstevel@tonic-gate 			"Volume size not a multiple of block size."));
13477c478bd9Sstevel@tonic-gate 		blocklim -= 2;			/* for trailer records */
13487c478bd9Sstevel@tonic-gate 		if (vflag)
13497c478bd9Sstevel@tonic-gate 			(void) fprintf(vfile, gettext("Volume ends at %"
13507c478bd9Sstevel@tonic-gate 			    FMT_blkcnt_t "K, blocking factor = %dK\n"),
13517c478bd9Sstevel@tonic-gate 			    K((blocklim - 1)), K(nblock));
13527c478bd9Sstevel@tonic-gate 	}
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
13557c478bd9Sstevel@tonic-gate 	if (Fileflag) {
13567c478bd9Sstevel@tonic-gate 		if (Filefile != NULL) {
13577c478bd9Sstevel@tonic-gate 			if ((ff = fopen(Filefile, "r")) == NULL)
13587c478bd9Sstevel@tonic-gate 				vperror(0, "%s", Filefile);
13597c478bd9Sstevel@tonic-gate 		} else {
13607c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
13617c478bd9Sstevel@tonic-gate 			    "tar: F requires a file name.\n"));
13627c478bd9Sstevel@tonic-gate 			usage();
13637c478bd9Sstevel@tonic-gate 		}
13647c478bd9Sstevel@tonic-gate 	}
13657c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 	/*
13687c478bd9Sstevel@tonic-gate 	 * Save the original directory before it gets
13697c478bd9Sstevel@tonic-gate 	 * changed.
13707c478bd9Sstevel@tonic-gate 	 */
13717c478bd9Sstevel@tonic-gate 	if (getcwd(origdir, (PATH_MAX+1)) == NULL) {
13727c478bd9Sstevel@tonic-gate 		vperror(0, gettext("A parent directory cannot be read"));
13737c478bd9Sstevel@tonic-gate 		exit(1);
13747c478bd9Sstevel@tonic-gate 	}
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 	(void) strcpy(wdir, origdir);
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate 	while ((*argv || fp || ff) && !term) {
13797c478bd9Sstevel@tonic-gate 		if (fp || (strcmp(*argv, "-I") == 0)) {
13807c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
13817c478bd9Sstevel@tonic-gate 			if (Fileflag) {
13827c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
13837c478bd9Sstevel@tonic-gate 				"tar: only one of I or F.\n"));
13847c478bd9Sstevel@tonic-gate 				usage();
13857c478bd9Sstevel@tonic-gate 			}
13867c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
13877c478bd9Sstevel@tonic-gate 			if (fp == NULL) {
13887c478bd9Sstevel@tonic-gate 				if (*++argv == NULL)
13897c478bd9Sstevel@tonic-gate 					fatal(gettext(
13907c478bd9Sstevel@tonic-gate 					    "missing file name for -I flag."));
13917c478bd9Sstevel@tonic-gate 				else if ((fp = fopen(*argv++, "r")) == NULL)
13927c478bd9Sstevel@tonic-gate 					vperror(0, "%s", argv[-1]);
13937c478bd9Sstevel@tonic-gate 				continue;
13947c478bd9Sstevel@tonic-gate 			} else if ((fgets(file, PATH_MAX-1, fp)) == NULL) {
13957c478bd9Sstevel@tonic-gate 				(void) fclose(fp);
13967c478bd9Sstevel@tonic-gate 				fp = NULL;
13977c478bd9Sstevel@tonic-gate 				continue;
13987c478bd9Sstevel@tonic-gate 			} else {
13997c478bd9Sstevel@tonic-gate 				cp = cp2 = file;
14007c478bd9Sstevel@tonic-gate 				if ((p = strchr(cp2, '\n')))
14017c478bd9Sstevel@tonic-gate 					*p = 0;
14027c478bd9Sstevel@tonic-gate 			}
14037c478bd9Sstevel@tonic-gate 		} else if ((strcmp(*argv, "-C") == 0) && argv[1]) {
14047c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
14057c478bd9Sstevel@tonic-gate 			if (Fileflag) {
14067c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
14077c478bd9Sstevel@tonic-gate 				"tar: only one of F or C\n"));
14087c478bd9Sstevel@tonic-gate 				usage();
14097c478bd9Sstevel@tonic-gate 			}
14107c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 			if (chdir(*++argv) < 0)
14137c478bd9Sstevel@tonic-gate 				vperror(0, gettext(
14147c478bd9Sstevel@tonic-gate 				    "can't change directories to %s"), *argv);
14157c478bd9Sstevel@tonic-gate 			else
14167c478bd9Sstevel@tonic-gate 				(void) getcwd(wdir, (sizeof (wdir)));
14177c478bd9Sstevel@tonic-gate 			argv++;
14187c478bd9Sstevel@tonic-gate 			continue;
14197c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
14207c478bd9Sstevel@tonic-gate 		} else if (Fileflag && (ff != NULL)) {
14217c478bd9Sstevel@tonic-gate 			if ((fgets(file, PATH_MAX-1, ff)) == NULL) {
14227c478bd9Sstevel@tonic-gate 				(void) fclose(ff);
14237c478bd9Sstevel@tonic-gate 				ff = NULL;
14247c478bd9Sstevel@tonic-gate 				continue;
14257c478bd9Sstevel@tonic-gate 			} else {
14267c478bd9Sstevel@tonic-gate 				cp = cp2 = file;
14277c478bd9Sstevel@tonic-gate 				if (p = strchr(cp2, '\n'))
14287c478bd9Sstevel@tonic-gate 					*p = 0;
14297c478bd9Sstevel@tonic-gate 			}
14307c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
14317c478bd9Sstevel@tonic-gate 		} else
14327c478bd9Sstevel@tonic-gate 			cp = cp2 = strcpy(file, *argv++);
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 		/*
14357c478bd9Sstevel@tonic-gate 		 * point cp2 to the last '/' in file, but not
14367c478bd9Sstevel@tonic-gate 		 * to a trailing '/'
14377c478bd9Sstevel@tonic-gate 		 */
14387c478bd9Sstevel@tonic-gate 		for (; *cp; cp++) {
14397c478bd9Sstevel@tonic-gate 			if (*cp == '/') {
14407c478bd9Sstevel@tonic-gate 				while (*(cp+1) == '/') {
14417c478bd9Sstevel@tonic-gate 					++cp;
14427c478bd9Sstevel@tonic-gate 				}
14437c478bd9Sstevel@tonic-gate 				if (*(cp+1) != '\0') {
14447c478bd9Sstevel@tonic-gate 					/* not trailing slash */
14457c478bd9Sstevel@tonic-gate 					cp2 = cp;
14467c478bd9Sstevel@tonic-gate 				}
14477c478bd9Sstevel@tonic-gate 			}
14487c478bd9Sstevel@tonic-gate 		}
14497c478bd9Sstevel@tonic-gate 		if (cp2 != file) {
14507c478bd9Sstevel@tonic-gate 			*cp2 = '\0';
14517c478bd9Sstevel@tonic-gate 			if (chdir(file) < 0) {
14527c478bd9Sstevel@tonic-gate 				vperror(0, gettext(
14537c478bd9Sstevel@tonic-gate 				    "can't change directories to %s"), file);
14547c478bd9Sstevel@tonic-gate 				continue;
14557c478bd9Sstevel@tonic-gate 			}
14567c478bd9Sstevel@tonic-gate 			*cp2 = '/';
14577c478bd9Sstevel@tonic-gate 			cp2++;
14587c478bd9Sstevel@tonic-gate 		}
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 		parent = getcwd(tempdir, (sizeof (tempdir)));
1461da6c28aaSamw 
1462da6c28aaSamw 		archtype = putfile(file, cp2, parent, NULL, NORMAL_FILE,
14637c478bd9Sstevel@tonic-gate 		    LEV0, SYMLINK_LEV0);
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
14667c478bd9Sstevel@tonic-gate 		if (!exitflag) {
1467da6c28aaSamw 			if ((atflag || saflag) &&
1468da6c28aaSamw 			    (archtype == PUT_NOTAS_LINK)) {
1469da6c28aaSamw 				xattrs_put(file, cp2, parent, NULL);
14707c478bd9Sstevel@tonic-gate 			}
14717c478bd9Sstevel@tonic-gate 		}
14727c478bd9Sstevel@tonic-gate #endif
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 		if (chdir(origdir) < 0)
14757c478bd9Sstevel@tonic-gate 			vperror(0, gettext("cannot change back?: %s"), origdir);
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate 		if (exitflag) {
14787c478bd9Sstevel@tonic-gate 			/*
14797c478bd9Sstevel@tonic-gate 			 * If e function modifier has been specified
14807c478bd9Sstevel@tonic-gate 			 * write the files (that are listed before the
14817c478bd9Sstevel@tonic-gate 			 * file causing the error) to tape.  exitflag is
14827c478bd9Sstevel@tonic-gate 			 * used because only some of the error conditions
14837c478bd9Sstevel@tonic-gate 			 * in putfile() recognize the e function modifier.
14847c478bd9Sstevel@tonic-gate 			 */
14857c478bd9Sstevel@tonic-gate 			break;
14867c478bd9Sstevel@tonic-gate 		}
14877c478bd9Sstevel@tonic-gate 	}
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate 	putempty((blkcnt_t)2);
14907c478bd9Sstevel@tonic-gate 	flushtape();
14917c478bd9Sstevel@tonic-gate 	closevol();	/* SP-1 */
14927c478bd9Sstevel@tonic-gate 	if (linkerrok == 1)
14937c478bd9Sstevel@tonic-gate 		for (; ihead != NULL; ihead = ihead->nextp) {
14947c478bd9Sstevel@tonic-gate 			if (ihead->count == 0)
14957c478bd9Sstevel@tonic-gate 				continue;
14967c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
14977c478bd9Sstevel@tonic-gate 			"tar: missing links to %s\n"), ihead->pathname);
14987c478bd9Sstevel@tonic-gate 			if (errflag)
14997c478bd9Sstevel@tonic-gate 				done(1);
15007c478bd9Sstevel@tonic-gate 			else
15017c478bd9Sstevel@tonic-gate 				Errflg = 1;
15027c478bd9Sstevel@tonic-gate 		}
15037c478bd9Sstevel@tonic-gate }
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate /*
15077c478bd9Sstevel@tonic-gate  * endtape - check for tape at end
15087c478bd9Sstevel@tonic-gate  *
15097c478bd9Sstevel@tonic-gate  *	endtape checks the entry in dblock.dbuf to see if its the
15107c478bd9Sstevel@tonic-gate  *	special EOT entry.  Endtape is usually called after getdir().
15117c478bd9Sstevel@tonic-gate  *
15127c478bd9Sstevel@tonic-gate  *	endtape used to call backtape; it no longer does, he who
15137c478bd9Sstevel@tonic-gate  *	wants it backed up must call backtape himself
15147c478bd9Sstevel@tonic-gate  *	RETURNS:	0 if not EOT, tape position unaffected
15157c478bd9Sstevel@tonic-gate  *			1 if	 EOT, tape position unaffected
15167c478bd9Sstevel@tonic-gate  */
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate static int
15197c478bd9Sstevel@tonic-gate endtape(void)
15207c478bd9Sstevel@tonic-gate {
15217c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.name[0] == '\0') {	/* null header = EOT */
15227c478bd9Sstevel@tonic-gate 		return (1);
15237c478bd9Sstevel@tonic-gate 	} else
15247c478bd9Sstevel@tonic-gate 		return (0);
15257c478bd9Sstevel@tonic-gate }
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate /*
15287c478bd9Sstevel@tonic-gate  *	getdir - get directory entry from tar tape
15297c478bd9Sstevel@tonic-gate  *
15307c478bd9Sstevel@tonic-gate  *	getdir reads the next tarblock off the tape and cracks
15317c478bd9Sstevel@tonic-gate  *	it as a directory. The checksum must match properly.
15327c478bd9Sstevel@tonic-gate  *
15337c478bd9Sstevel@tonic-gate  *	If tfile is non-null getdir writes the file name and mod date
15347c478bd9Sstevel@tonic-gate  *	to tfile.
15357c478bd9Sstevel@tonic-gate  */
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate static void
15387c478bd9Sstevel@tonic-gate getdir(void)
15397c478bd9Sstevel@tonic-gate {
15407c478bd9Sstevel@tonic-gate 	struct stat *sp;
15417c478bd9Sstevel@tonic-gate #ifdef EUC
15427c478bd9Sstevel@tonic-gate 	static int warn_chksum_sign = 0;
15437c478bd9Sstevel@tonic-gate #endif /* EUC */
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate top:
15467c478bd9Sstevel@tonic-gate 	readtape((char *)&dblock);
15477c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.name[0] == '\0')
15487c478bd9Sstevel@tonic-gate 		return;
15497c478bd9Sstevel@tonic-gate 	sp = &stbuf;
15507c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.mode, "%8lo", &Gen.g_mode);
15517c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.uid, "%8lo", (ulong_t *)&Gen.g_uid);
15527c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.gid, "%8lo", (ulong_t *)&Gen.g_gid);
15537c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.size, "%12" FMT_off_t_o, &Gen.g_filesz);
15547c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.mtime, "%12lo", (ulong_t *)&Gen.g_mtime);
15557c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.chksum, "%8o", &Gen.g_cksum);
15567c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.devmajor, "%8lo", &Gen.g_devmajor);
15577c478bd9Sstevel@tonic-gate 	(void) sscanf(dblock.dbuf.devminor, "%8lo", &Gen.g_devminor);
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	is_posix = (strcmp(dblock.dbuf.magic, magic_type) == 0);
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 	sp->st_mode = Gen.g_mode;
15627c478bd9Sstevel@tonic-gate 	if (is_posix && (sp->st_mode & S_IFMT) == 0)
15637c478bd9Sstevel@tonic-gate 		switch (dblock.dbuf.typeflag) {
15647c478bd9Sstevel@tonic-gate 		case '0': case 0: case _XATTR_HDRTYPE:
15657c478bd9Sstevel@tonic-gate 			sp->st_mode |= S_IFREG;
15667c478bd9Sstevel@tonic-gate 			break;
15677c478bd9Sstevel@tonic-gate 		case '1':	/* hard link */
15687c478bd9Sstevel@tonic-gate 			break;
15697c478bd9Sstevel@tonic-gate 		case '2':
15707c478bd9Sstevel@tonic-gate 			sp->st_mode |= S_IFLNK;
15717c478bd9Sstevel@tonic-gate 			break;
15727c478bd9Sstevel@tonic-gate 		case '3':
15737c478bd9Sstevel@tonic-gate 			sp->st_mode |= S_IFCHR;
15747c478bd9Sstevel@tonic-gate 			break;
15757c478bd9Sstevel@tonic-gate 		case '4':
15767c478bd9Sstevel@tonic-gate 			sp->st_mode |= S_IFBLK;
15777c478bd9Sstevel@tonic-gate 			break;
15787c478bd9Sstevel@tonic-gate 		case '5':
15797c478bd9Sstevel@tonic-gate 			sp->st_mode |= S_IFDIR;
15807c478bd9Sstevel@tonic-gate 			break;
15817c478bd9Sstevel@tonic-gate 		case '6':
15827c478bd9Sstevel@tonic-gate 			sp->st_mode |= S_IFIFO;
15837c478bd9Sstevel@tonic-gate 			break;
15847c478bd9Sstevel@tonic-gate 		default:
15857c478bd9Sstevel@tonic-gate 			if (convtoreg(Gen.g_filesz))
15867c478bd9Sstevel@tonic-gate 				sp->st_mode |= S_IFREG;
15877c478bd9Sstevel@tonic-gate 			break;
15887c478bd9Sstevel@tonic-gate 		}
15897c478bd9Sstevel@tonic-gate 
1590e765faefSRich Burridge 	if ((dblock.dbuf.typeflag == 'X') || (dblock.dbuf.typeflag == 'L')) {
15917c478bd9Sstevel@tonic-gate 		Xhdrflag = 1;	/* Currently processing extended header */
1592e765faefSRich Burridge 	} else {
15937c478bd9Sstevel@tonic-gate 		Xhdrflag = 0;
1594e765faefSRich Burridge 	}
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate 	sp->st_uid = Gen.g_uid;
15977c478bd9Sstevel@tonic-gate 	sp->st_gid = Gen.g_gid;
15987c478bd9Sstevel@tonic-gate 	sp->st_size = Gen.g_filesz;
15997c478bd9Sstevel@tonic-gate 	sp->st_mtime = Gen.g_mtime;
16007c478bd9Sstevel@tonic-gate 	chksum = Gen.g_cksum;
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.extno != '\0') {	/* split file? */
16037c478bd9Sstevel@tonic-gate 		extno = dblock.dbuf.extno;
16047c478bd9Sstevel@tonic-gate 		extsize = Gen.g_filesz;
16057c478bd9Sstevel@tonic-gate 		extotal = dblock.dbuf.extotal;
16067c478bd9Sstevel@tonic-gate 	} else {
16077c478bd9Sstevel@tonic-gate 		extno = 0;	/* tell others file not split */
16087c478bd9Sstevel@tonic-gate 		extsize = 0;
16097c478bd9Sstevel@tonic-gate 		extotal = 0;
16107c478bd9Sstevel@tonic-gate 	}
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate #ifdef	EUC
16137c478bd9Sstevel@tonic-gate 	if (chksum != checksum(&dblock)) {
16147c478bd9Sstevel@tonic-gate 		if (chksum != checksum_signed(&dblock)) {
16157c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
16167c478bd9Sstevel@tonic-gate 			    "tar: directory checksum error\n"));
1617*03d7b3c0SRich Burridge 			if (iflag) {
1618*03d7b3c0SRich Burridge 				Errflg = 2;
16197c478bd9Sstevel@tonic-gate 				goto top;
1620*03d7b3c0SRich Burridge 			}
16217c478bd9Sstevel@tonic-gate 			done(2);
16227c478bd9Sstevel@tonic-gate 		} else {
16237c478bd9Sstevel@tonic-gate 			if (! warn_chksum_sign) {
16247c478bd9Sstevel@tonic-gate 				warn_chksum_sign = 1;
16257c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
16267c478bd9Sstevel@tonic-gate 			"tar: warning: tar file made with signed checksum\n"));
16277c478bd9Sstevel@tonic-gate 			}
16287c478bd9Sstevel@tonic-gate 		}
16297c478bd9Sstevel@tonic-gate 	}
16307c478bd9Sstevel@tonic-gate #else
16317c478bd9Sstevel@tonic-gate 	if (chksum != checksum(&dblock)) {
16327c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
16337c478bd9Sstevel@tonic-gate 		"tar: directory checksum error\n"));
1634*03d7b3c0SRich Burridge 		if (iflag) {
1635*03d7b3c0SRich Burridge 			Errflg = 2;
16367c478bd9Sstevel@tonic-gate 			goto top;
1637*03d7b3c0SRich Burridge 		}
16387c478bd9Sstevel@tonic-gate 		done(2);
16397c478bd9Sstevel@tonic-gate 	}
16407c478bd9Sstevel@tonic-gate #endif	/* EUC */
16417c478bd9Sstevel@tonic-gate 	if (tfile != NULL && Xhdrflag == 0) {
16427c478bd9Sstevel@tonic-gate 		/*
16437c478bd9Sstevel@tonic-gate 		 * If an extended header is present, then time is available
16447c478bd9Sstevel@tonic-gate 		 * in nanoseconds in the extended header data, so set it.
16457c478bd9Sstevel@tonic-gate 		 * Otherwise, give an invalid value so that checkupdate will
16467c478bd9Sstevel@tonic-gate 		 * not test beyond seconds.
16477c478bd9Sstevel@tonic-gate 		 */
16487c478bd9Sstevel@tonic-gate 		if ((xhdr_flgs & _X_MTIME))
16497c478bd9Sstevel@tonic-gate 			sp->st_mtim.tv_nsec = Xtarhdr.x_mtime.tv_nsec;
16507c478bd9Sstevel@tonic-gate 		else
16517c478bd9Sstevel@tonic-gate 			sp->st_mtim.tv_nsec = -1;
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_PATH)
16547c478bd9Sstevel@tonic-gate 			(void) fprintf(tfile, "%s %10ld.%9.9ld\n",
16557c478bd9Sstevel@tonic-gate 			    Xtarhdr.x_path, sp->st_mtim.tv_sec,
16567c478bd9Sstevel@tonic-gate 			    sp->st_mtim.tv_nsec);
16577c478bd9Sstevel@tonic-gate 		else
16587c478bd9Sstevel@tonic-gate 			(void) fprintf(tfile, "%.*s %10ld.%9.9ld\n",
16597c478bd9Sstevel@tonic-gate 			    NAMSIZ, dblock.dbuf.name, sp->st_mtim.tv_sec,
16607c478bd9Sstevel@tonic-gate 			    sp->st_mtim.tv_nsec);
16617c478bd9Sstevel@tonic-gate 	}
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
16647c478bd9Sstevel@tonic-gate 	Hiddendir = 0;
16657c478bd9Sstevel@tonic-gate 	if (xattrp && dblock.dbuf.typeflag == _XATTR_HDRTYPE) {
16667c478bd9Sstevel@tonic-gate 		if (xattrbadhead) {
16677c478bd9Sstevel@tonic-gate 			free(xattrhead);
16687c478bd9Sstevel@tonic-gate 			xattrp = NULL;
16697c478bd9Sstevel@tonic-gate 			xattr_linkp = NULL;
16707c478bd9Sstevel@tonic-gate 			xattrhead = NULL;
16717c478bd9Sstevel@tonic-gate 		} else {
1672da6c28aaSamw 			char	*aname = basename(xattrapath);
1673da6c28aaSamw 			size_t	xindex  = aname - xattrapath;
1674da6c28aaSamw 
1675da6c28aaSamw 			if (xattrapath[xindex] == '.' &&
1676da6c28aaSamw 			    xattrapath[xindex + 1] == '\0' &&
16777c478bd9Sstevel@tonic-gate 			    xattrp->h_typeflag == '5') {
16787c478bd9Sstevel@tonic-gate 				Hiddendir = 1;
16797c478bd9Sstevel@tonic-gate 				sp->st_mode =
1680d2443e76Smarks 				    (S_IFDIR | (sp->st_mode & POSIXMODES));
16817c478bd9Sstevel@tonic-gate 			}
16827c478bd9Sstevel@tonic-gate 			dblock.dbuf.typeflag = xattrp->h_typeflag;
16837c478bd9Sstevel@tonic-gate 		}
16847c478bd9Sstevel@tonic-gate 	}
16857c478bd9Sstevel@tonic-gate #endif
16867c478bd9Sstevel@tonic-gate }
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate /*
16907c478bd9Sstevel@tonic-gate  *	passtape - skip over a file on the tape
16917c478bd9Sstevel@tonic-gate  *
16927c478bd9Sstevel@tonic-gate  *	passtape skips over the next data file on the tape.
16937c478bd9Sstevel@tonic-gate  *	The tape directory entry must be in dblock.dbuf. This
16947c478bd9Sstevel@tonic-gate  *	routine just eats the number of blocks computed from the
16957c478bd9Sstevel@tonic-gate  *	directory size entry; the tape must be (logically) positioned
16967c478bd9Sstevel@tonic-gate  *	right after thee directory info.
16977c478bd9Sstevel@tonic-gate  */
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate static void
17007c478bd9Sstevel@tonic-gate passtape(void)
17017c478bd9Sstevel@tonic-gate {
17027c478bd9Sstevel@tonic-gate 	blkcnt_t blocks;
17037c478bd9Sstevel@tonic-gate 	char buf[TBLOCK];
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 	/*
17067c478bd9Sstevel@tonic-gate 	 * Types link(1), sym-link(2), char special(3), blk special(4),
17077c478bd9Sstevel@tonic-gate 	 *  directory(5), and FIFO(6) do not have data blocks associated
17087c478bd9Sstevel@tonic-gate 	 *  with them so just skip reading the data block.
17097c478bd9Sstevel@tonic-gate 	 */
17107c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.typeflag == '1' || dblock.dbuf.typeflag == '2' ||
17117c478bd9Sstevel@tonic-gate 	    dblock.dbuf.typeflag == '3' || dblock.dbuf.typeflag == '4' ||
17127c478bd9Sstevel@tonic-gate 	    dblock.dbuf.typeflag == '5' || dblock.dbuf.typeflag == '6')
17137c478bd9Sstevel@tonic-gate 		return;
17147c478bd9Sstevel@tonic-gate 	blocks = TBLOCKS(stbuf.st_size);
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 	/* if operating on disk, seek instead of reading */
17177c478bd9Sstevel@tonic-gate 	if (NotTape)
17187c478bd9Sstevel@tonic-gate 		seekdisk(blocks);
17197c478bd9Sstevel@tonic-gate 	else
17207c478bd9Sstevel@tonic-gate 		while (blocks-- > 0)
17217c478bd9Sstevel@tonic-gate 			readtape(buf);
17227c478bd9Sstevel@tonic-gate }
17237c478bd9Sstevel@tonic-gate 
1724da6c28aaSamw #if defined(O_XATTR)
1725da6c28aaSamw static int
1726da6c28aaSamw is_sysattr(char *name)
1727da6c28aaSamw {
1728da6c28aaSamw 	return ((strcmp(name, VIEW_READONLY) == 0) ||
1729da6c28aaSamw 	    (strcmp(name, VIEW_READWRITE) == 0));
1730da6c28aaSamw }
1731da6c28aaSamw #endif
1732da6c28aaSamw 
1733da6c28aaSamw #if defined(O_XATTR)
1734da6c28aaSamw /*
1735da6c28aaSamw  * Verify the attribute, attrname, is an attribute we want to restore.
1736da6c28aaSamw  * Never restore read-only system attribute files.  Only restore read-write
1737da6c28aaSamw  * system attributes files when -/ was specified, and only traverse into
1738da6c28aaSamw  * the 2nd level attribute directory containing only system attributes if
1739da6c28aaSamw  * -@ was specified.  This keeps us from archiving
1740da6c28aaSamw  *	<attribute name>/<read-write system attribute file>
1741da6c28aaSamw  * when -/ was specified without -@.
1742da6c28aaSamw  *
1743da6c28aaSamw  * attrname	- attribute file name
1744da6c28aaSamw  * attrparent	- attribute's parent name within the base file's attribute
1745da6c28aaSamw  *		directory hierarchy
1746da6c28aaSamw  */
1747da6c28aaSamw static attr_status_t
1748da6c28aaSamw verify_attr(char *attrname, char *attrparent, int arc_rwsysattr,
1749da6c28aaSamw     int *rw_sysattr)
1750da6c28aaSamw {
1751da6c28aaSamw #if defined(_PC_SATTR_ENABLED)
1752da6c28aaSamw 	int	attr_supported;
1753da6c28aaSamw 
1754da6c28aaSamw 	/* Never restore read-only system attribute files */
1755da6c28aaSamw 	if ((attr_supported = sysattr_type(attrname)) == _RO_SATTR) {
1756da6c28aaSamw 		*rw_sysattr = 0;
1757da6c28aaSamw 		return (ATTR_SKIP);
1758da6c28aaSamw 	} else {
1759da6c28aaSamw 		*rw_sysattr = (attr_supported == _RW_SATTR);
1760da6c28aaSamw 	}
1761da6c28aaSamw #else
1762da6c28aaSamw 	/*
1763da6c28aaSamw 	 * Only need to check if this attribute is an extended system
1764da6c28aaSamw 	 * attribute.
1765da6c28aaSamw 	 */
1766da6c28aaSamw 	if (*rw_sysattr = is_sysattr(attrname)) {
1767da6c28aaSamw 		return (ATTR_SKIP);
1768da6c28aaSamw 	} else {
1769da6c28aaSamw 		return (ATTR_OK);
1770da6c28aaSamw 	}
1771da6c28aaSamw #endif	/* _PC_SATTR_ENABLED */
1772da6c28aaSamw 
1773da6c28aaSamw 	/*
1774da6c28aaSamw 	 * If the extended system attribute file is specified with the
1775da6c28aaSamw 	 * arc_rwsysattr flag, as being transient (default extended
1776da6c28aaSamw 	 * attributes), then don't archive it.
1777da6c28aaSamw 	 */
1778da6c28aaSamw 	if (*rw_sysattr && !arc_rwsysattr) {
1779da6c28aaSamw 		return (ATTR_SKIP);
1780da6c28aaSamw 	}
1781da6c28aaSamw 
1782da6c28aaSamw 	/*
1783da6c28aaSamw 	 * Only restore read-write system attribute files
1784da6c28aaSamw 	 * when -/ was specified.  Only restore extended
1785da6c28aaSamw 	 * attributes when -@ was specified.
1786da6c28aaSamw 	 */
1787da6c28aaSamw 	if (atflag) {
1788da6c28aaSamw 		if (!saflag) {
1789da6c28aaSamw 			/*
1790da6c28aaSamw 			 * Only archive/restore the hidden directory "." if
1791da6c28aaSamw 			 * we're processing the top level hidden attribute
1792da6c28aaSamw 			 * directory.  We don't want to process the
1793da6c28aaSamw 			 * hidden attribute directory of the attribute
1794da6c28aaSamw 			 * directory that contains only extended system
1795da6c28aaSamw 			 * attributes.
1796da6c28aaSamw 			 */
1797da6c28aaSamw 			if (*rw_sysattr || (Hiddendir &&
1798da6c28aaSamw 			    (attrparent != NULL))) {
1799da6c28aaSamw 				return (ATTR_SKIP);
1800da6c28aaSamw 			}
1801da6c28aaSamw 		}
1802da6c28aaSamw 	} else if (saflag) {
1803da6c28aaSamw 		/*
1804da6c28aaSamw 		 * Only archive/restore read-write extended system attribute
1805da6c28aaSamw 		 * files of the base file.
1806da6c28aaSamw 		 */
1807da6c28aaSamw 		if (!*rw_sysattr || (attrparent != NULL)) {
1808da6c28aaSamw 			return (ATTR_SKIP);
1809da6c28aaSamw 		}
1810da6c28aaSamw 	} else {
1811da6c28aaSamw 		return (ATTR_SKIP);
1812da6c28aaSamw 	}
1813da6c28aaSamw 
1814da6c28aaSamw 	return (ATTR_OK);
1815da6c28aaSamw }
1816da6c28aaSamw #endif
18177c478bd9Sstevel@tonic-gate 
18185e2174acSceastha static void
18195e2174acSceastha free_children(file_list_t *children)
18205e2174acSceastha {
18215e2174acSceastha 	file_list_t	*child = children;
18225e2174acSceastha 	file_list_t	*cptr;
18235e2174acSceastha 
18245e2174acSceastha 	while (child != NULL) {
18255e2174acSceastha 		cptr = child->next;
18265e2174acSceastha 		if (child->name != NULL) {
18275e2174acSceastha 			free(child->name);
18285e2174acSceastha 		}
18295e2174acSceastha 		child = cptr;
18305e2174acSceastha 	}
18315e2174acSceastha }
18325e2174acSceastha 
18337c478bd9Sstevel@tonic-gate static int
1834da6c28aaSamw putfile(char *longname, char *shortname, char *parent, attr_data_t *attrinfo,
18357c478bd9Sstevel@tonic-gate     int filetype, int lev, int symlink_lev)
18367c478bd9Sstevel@tonic-gate {
18377c478bd9Sstevel@tonic-gate 	int infile = -1;	/* deliberately invalid */
18387c478bd9Sstevel@tonic-gate 	blkcnt_t blocks;
18397c478bd9Sstevel@tonic-gate 	char buf[PATH_MAX + 2];	/* Add trailing slash and null */
18407c478bd9Sstevel@tonic-gate 	char *bigbuf;
18417c478bd9Sstevel@tonic-gate 	int	maxread;
18427c478bd9Sstevel@tonic-gate 	int	hint;		/* amount to write to get "in sync" */
18437c478bd9Sstevel@tonic-gate 	char filetmp[PATH_MAX + 1];
18447c478bd9Sstevel@tonic-gate 	char *cp;
18457c478bd9Sstevel@tonic-gate 	char *name;
1846da6c28aaSamw 	char *attrparent = NULL;
1847da6c28aaSamw 	char *longattrname = NULL;
18485e2174acSceastha 	file_list_t	*child = NULL;
18495e2174acSceastha 	file_list_t	*child_end = NULL;
18505e2174acSceastha 	file_list_t	*cptr;
18517c478bd9Sstevel@tonic-gate 	struct dirent *dp;
18527c478bd9Sstevel@tonic-gate 	DIR *dirp;
18537c478bd9Sstevel@tonic-gate 	int i;
18547c478bd9Sstevel@tonic-gate 	int split;
18557c478bd9Sstevel@tonic-gate 	int dirfd = -1;
18567c478bd9Sstevel@tonic-gate 	int rc = PUT_NOTAS_LINK;
18577c478bd9Sstevel@tonic-gate 	int archtype = 0;
1858da6c28aaSamw 	int rw_sysattr = 0;
18597c478bd9Sstevel@tonic-gate 	char newparent[PATH_MAX + MAXNAMLEN + 1];
18607c478bd9Sstevel@tonic-gate 	char *prefix = "";
18617c478bd9Sstevel@tonic-gate 	char *tmpbuf;
18627c478bd9Sstevel@tonic-gate 	char goodbuf[PRESIZ + 2];
18637c478bd9Sstevel@tonic-gate 	char junkbuf[MAXNAM+1];
18647c478bd9Sstevel@tonic-gate 	char *lastslash;
18657c478bd9Sstevel@tonic-gate 	int j;
18667c478bd9Sstevel@tonic-gate 	struct stat sbuf;
18677c478bd9Sstevel@tonic-gate 	int readlink_max;
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 	(void) memset(goodbuf, '\0', sizeof (goodbuf));
18707c478bd9Sstevel@tonic-gate 	(void) memset(junkbuf, '\0', sizeof (junkbuf));
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 	xhdr_flgs = 0;
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 	if (filetype == XATTR_FILE) {
1875da6c28aaSamw 		attrparent = attrinfo->attr_parent;
1876da6c28aaSamw 		longattrname = attrinfo->attr_path;
1877da6c28aaSamw 		dirfd = attrinfo->attr_parentfd;
1878ced83f9bSceastha 		rw_sysattr = attrinfo->attr_rw_sysattr;
18797c478bd9Sstevel@tonic-gate 	} else {
18807c478bd9Sstevel@tonic-gate 		dirfd = open(".", O_RDONLY);
18817c478bd9Sstevel@tonic-gate 	}
18827c478bd9Sstevel@tonic-gate 
18837c478bd9Sstevel@tonic-gate 	if (dirfd == -1) {
18847c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
1885da6c28aaSamw 		    "tar: unable to open%sdirectory %s%s%s%s\n"),
18867c478bd9Sstevel@tonic-gate 		    (filetype == XATTR_FILE) ? gettext(" attribute ") : " ",
1887da6c28aaSamw 		    (attrparent == NULL) ? "" : gettext("of attribute "),
1888da6c28aaSamw 		    (attrparent == NULL) ? "" : attrparent,
1889da6c28aaSamw 		    (attrparent == NULL) ? "" : gettext(" of "),
18907c478bd9Sstevel@tonic-gate 		    (filetype == XATTR_FILE) ? longname : parent);
18917c478bd9Sstevel@tonic-gate 		goto out;
18927c478bd9Sstevel@tonic-gate 	}
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate 	if (lev > MAXLEV) {
18957c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
18967c478bd9Sstevel@tonic-gate 		    gettext("tar: directory nesting too deep, %s not dumped\n"),
18977c478bd9Sstevel@tonic-gate 		    longname);
18987c478bd9Sstevel@tonic-gate 		goto out;
18997c478bd9Sstevel@tonic-gate 	}
19007c478bd9Sstevel@tonic-gate 
1901da6c28aaSamw 	if (getstat(dirfd, longname, shortname, attrparent))
19027c478bd9Sstevel@tonic-gate 		goto out;
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate 	if (hflag) {
19057c478bd9Sstevel@tonic-gate 		/*
19067c478bd9Sstevel@tonic-gate 		 * Catch nesting where a file is a symlink to its directory.
19077c478bd9Sstevel@tonic-gate 		 */
19087c478bd9Sstevel@tonic-gate 		j = fstatat(dirfd, shortname, &sbuf, AT_SYMLINK_NOFOLLOW);
19097c478bd9Sstevel@tonic-gate 		if (S_ISLNK(sbuf.st_mode)) {
19107c478bd9Sstevel@tonic-gate 			if (symlink_lev++ >= MAXSYMLINKS) {
19117c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
19127c478bd9Sstevel@tonic-gate 				    "tar: %s: Number of symbolic links "
19137c478bd9Sstevel@tonic-gate 				    "encountered during path name traversal "
19147c478bd9Sstevel@tonic-gate 				    "exceeds MAXSYMLINKS\n"), longname);
19157c478bd9Sstevel@tonic-gate 				Errflg = 1;
19167c478bd9Sstevel@tonic-gate 				goto out;
19177c478bd9Sstevel@tonic-gate 			}
19187c478bd9Sstevel@tonic-gate 		}
19197c478bd9Sstevel@tonic-gate 	}
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate 	/*
19227c478bd9Sstevel@tonic-gate 	 * Check if the input file is the same as the tar file we
19237c478bd9Sstevel@tonic-gate 	 * are creating
19247c478bd9Sstevel@tonic-gate 	 */
19257c478bd9Sstevel@tonic-gate 	if ((mt_ino == stbuf.st_ino) && (mt_dev == stbuf.st_dev)) {
19267c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
1927da6c28aaSamw 		    "tar: %s%s%s%s%s same as archive file\n"),
1928da6c28aaSamw 		    rw_sysattr ? gettext("system ") : "",
1929da6c28aaSamw 		    (longattrname == NULL) ? "" : gettext("attribute "),
1930da6c28aaSamw 		    (longattrname == NULL) ? "" : longattrname,
1931da6c28aaSamw 		    (longattrname == NULL) ? "" : gettext(" of "),
1932da6c28aaSamw 		    longname);
19337c478bd9Sstevel@tonic-gate 		Errflg = 1;
19347c478bd9Sstevel@tonic-gate 		goto out;
19357c478bd9Sstevel@tonic-gate 	}
19367c478bd9Sstevel@tonic-gate 	/*
19377c478bd9Sstevel@tonic-gate 	 * Check size limit - we can't archive files that
19387c478bd9Sstevel@tonic-gate 	 * exceed TAR_OFFSET_MAX bytes because of header
19397c478bd9Sstevel@tonic-gate 	 * limitations. Exclude file types that set
19407c478bd9Sstevel@tonic-gate 	 * st_size to zero below because they take no
19417c478bd9Sstevel@tonic-gate 	 * archive space to represent contents.
19427c478bd9Sstevel@tonic-gate 	 */
19437c478bd9Sstevel@tonic-gate 	if ((stbuf.st_size > (off_t)TAR_OFFSET_MAX) &&
19447c478bd9Sstevel@tonic-gate 	    !S_ISDIR(stbuf.st_mode) &&
19457c478bd9Sstevel@tonic-gate 	    !S_ISCHR(stbuf.st_mode) &&
19467c478bd9Sstevel@tonic-gate 	    !S_ISBLK(stbuf.st_mode) &&
19477c478bd9Sstevel@tonic-gate 	    (Eflag == 0)) {
19487c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
1949da6c28aaSamw 		    "tar: %s%s%s%s%s too large to archive.  "
1950da6c28aaSamw 		    "Use E function modifier.\n"),
1951da6c28aaSamw 		    rw_sysattr ? gettext("system ") : "",
1952da6c28aaSamw 		    (longattrname == NULL) ? "" : gettext("attribute "),
1953da6c28aaSamw 		    (longattrname == NULL) ? "" : longattrname,
1954da6c28aaSamw 		    (longattrname == NULL) ? "" : gettext(" of "),
1955da6c28aaSamw 		    longname);
19567c478bd9Sstevel@tonic-gate 		if (errflag)
19577c478bd9Sstevel@tonic-gate 			exitflag = 1;
19587c478bd9Sstevel@tonic-gate 		Errflg = 1;
19597c478bd9Sstevel@tonic-gate 		goto out;
19607c478bd9Sstevel@tonic-gate 	}
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate 	if (tfile != NULL && checkupdate(longname) == 0) {
19637c478bd9Sstevel@tonic-gate 		goto out;
19647c478bd9Sstevel@tonic-gate 	}
19657c478bd9Sstevel@tonic-gate 	if (checkw('r', longname) == 0) {
19667c478bd9Sstevel@tonic-gate 		goto out;
19677c478bd9Sstevel@tonic-gate 	}
19687c478bd9Sstevel@tonic-gate 
19693a1dab68SRich Burridge 	if (Fflag &&
19703a1dab68SRich Burridge 	    checkf(longname, (stbuf.st_mode & S_IFMT) == S_IFDIR, Fflag) == 0)
19717c478bd9Sstevel@tonic-gate 		goto out;
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 	if (Xflag) {
19747c478bd9Sstevel@tonic-gate 		if (is_in_table(exclude_tbl, longname)) {
19757c478bd9Sstevel@tonic-gate 			if (vflag) {
19767c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext(
19777c478bd9Sstevel@tonic-gate 				    "a %s excluded\n"), longname);
19787c478bd9Sstevel@tonic-gate 			}
19797c478bd9Sstevel@tonic-gate 			goto out;
19807c478bd9Sstevel@tonic-gate 		}
19817c478bd9Sstevel@tonic-gate 	}
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate 	/*
19847c478bd9Sstevel@tonic-gate 	 * If the length of the fullname is greater than MAXNAM,
19857c478bd9Sstevel@tonic-gate 	 * print out a message and return (unless extended headers are used,
19867c478bd9Sstevel@tonic-gate 	 * in which case fullname is limited to PATH_MAX).
19877c478bd9Sstevel@tonic-gate 	 */
19887c478bd9Sstevel@tonic-gate 
19897c478bd9Sstevel@tonic-gate 	if ((((split = (int)strlen(longname)) > MAXNAM) && (Eflag == 0)) ||
19907c478bd9Sstevel@tonic-gate 	    (split > PATH_MAX)) {
19917c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
19927c478bd9Sstevel@tonic-gate 		    "tar: %s: file name too long\n"), longname);
19937c478bd9Sstevel@tonic-gate 		if (errflag)
19947c478bd9Sstevel@tonic-gate 			exitflag = 1;
19957c478bd9Sstevel@tonic-gate 		Errflg = 1;
19967c478bd9Sstevel@tonic-gate 		goto out;
19977c478bd9Sstevel@tonic-gate 	}
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate 	/*
20007c478bd9Sstevel@tonic-gate 	 * We split the fullname into prefix and name components if any one
20017c478bd9Sstevel@tonic-gate 	 * of three conditions holds:
20027c478bd9Sstevel@tonic-gate 	 *	-- the length of the fullname exceeds NAMSIZ,
20037c478bd9Sstevel@tonic-gate 	 *	-- the length of the fullname equals NAMSIZ, and the shortname
20047c478bd9Sstevel@tonic-gate 	 *	   is less than NAMSIZ, (splitting in this case preserves
20057c478bd9Sstevel@tonic-gate 	 *	   compatibility with 5.6 and 5.5.1 tar), or
20067c478bd9Sstevel@tonic-gate 	 * 	-- the length of the fullname equals NAMSIZ, the file is a
20077c478bd9Sstevel@tonic-gate 	 *	   directory and we are not in POSIX-conformant mode (where
20087c478bd9Sstevel@tonic-gate 	 *	   trailing slashes are removed from directories).
20097c478bd9Sstevel@tonic-gate 	 */
20107c478bd9Sstevel@tonic-gate 	if ((split > NAMSIZ) ||
20117c478bd9Sstevel@tonic-gate 	    (split == NAMSIZ && strlen(shortname) < NAMSIZ) ||
20124bc0a2efScasper 	    (split == NAMSIZ && S_ISDIR(stbuf.st_mode) && !Pflag)) {
20137c478bd9Sstevel@tonic-gate 		/*
20147c478bd9Sstevel@tonic-gate 		 * Since path is limited to PRESIZ characters, look for the
20157c478bd9Sstevel@tonic-gate 		 * last slash within PRESIZ + 1 characters only.
20167c478bd9Sstevel@tonic-gate 		 */
20177c478bd9Sstevel@tonic-gate 		(void) strncpy(&goodbuf[0], longname, min(split, PRESIZ + 1));
20187c478bd9Sstevel@tonic-gate 		tmpbuf = goodbuf;
20197c478bd9Sstevel@tonic-gate 		lastslash = strrchr(tmpbuf, '/');
20207c478bd9Sstevel@tonic-gate 		if (lastslash == NULL) {
20217c478bd9Sstevel@tonic-gate 			i = split;		/* Length of name */
20227c478bd9Sstevel@tonic-gate 			j = 0;			/* Length of prefix */
20237c478bd9Sstevel@tonic-gate 			goodbuf[0] = '\0';
20247c478bd9Sstevel@tonic-gate 		} else {
20257c478bd9Sstevel@tonic-gate 			*lastslash = '\0';	/* Terminate the prefix */
20267c478bd9Sstevel@tonic-gate 			j = strlen(tmpbuf);
20277c478bd9Sstevel@tonic-gate 			i = split - j - 1;
20287c478bd9Sstevel@tonic-gate 		}
20297c478bd9Sstevel@tonic-gate 		/*
20307c478bd9Sstevel@tonic-gate 		 * If the filename is greater than NAMSIZ we can't
20317c478bd9Sstevel@tonic-gate 		 * archive the file unless we are using extended headers.
20327c478bd9Sstevel@tonic-gate 		 */
20334bc0a2efScasper 		if ((i > NAMSIZ) || (i == NAMSIZ && S_ISDIR(stbuf.st_mode) &&
20347c478bd9Sstevel@tonic-gate 		    !Pflag)) {
20357c478bd9Sstevel@tonic-gate 			/* Determine which (filename or path) is too long. */
20367c478bd9Sstevel@tonic-gate 			lastslash = strrchr(longname, '/');
20377c478bd9Sstevel@tonic-gate 			if (lastslash != NULL)
20387c478bd9Sstevel@tonic-gate 				i = strlen(lastslash + 1);
20397c478bd9Sstevel@tonic-gate 			if (Eflag > 0) {
20407c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_PATH;
20417c478bd9Sstevel@tonic-gate 				Xtarhdr.x_path = longname;
20427c478bd9Sstevel@tonic-gate 				if (i <= NAMSIZ)
20437c478bd9Sstevel@tonic-gate 					(void) strcpy(junkbuf, lastslash + 1);
20447c478bd9Sstevel@tonic-gate 				else
20457c478bd9Sstevel@tonic-gate 					(void) sprintf(junkbuf, "%llu",
20467c478bd9Sstevel@tonic-gate 					    xhdr_count + 1);
20477c478bd9Sstevel@tonic-gate 				if (split - i - 1 > PRESIZ)
20487c478bd9Sstevel@tonic-gate 					(void) strcpy(goodbuf, xhdr_dirname);
20497c478bd9Sstevel@tonic-gate 			} else {
20507c478bd9Sstevel@tonic-gate 				if ((i > NAMSIZ) || (i == NAMSIZ &&
20514bc0a2efScasper 				    S_ISDIR(stbuf.st_mode) && !Pflag))
20527c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
20537c478bd9Sstevel@tonic-gate 					    "tar: %s: filename is greater than "
20547c478bd9Sstevel@tonic-gate 					    "%d\n"), lastslash == NULL ?
20557c478bd9Sstevel@tonic-gate 					    longname : lastslash + 1, NAMSIZ);
20567c478bd9Sstevel@tonic-gate 				else
20577c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
20587c478bd9Sstevel@tonic-gate 					    "tar: %s: prefix is greater than %d"
20597c478bd9Sstevel@tonic-gate 					    "\n"), longname, PRESIZ);
20607c478bd9Sstevel@tonic-gate 				if (errflag)
20617c478bd9Sstevel@tonic-gate 					exitflag = 1;
20627c478bd9Sstevel@tonic-gate 				Errflg = 1;
20637c478bd9Sstevel@tonic-gate 				goto out;
20647c478bd9Sstevel@tonic-gate 			}
20657c478bd9Sstevel@tonic-gate 		} else
20667c478bd9Sstevel@tonic-gate 			(void) strncpy(&junkbuf[0], longname + j + 1,
20677c478bd9Sstevel@tonic-gate 			    strlen(longname + j + 1));
20687c478bd9Sstevel@tonic-gate 		name = junkbuf;
20697c478bd9Sstevel@tonic-gate 		prefix = goodbuf;
20707c478bd9Sstevel@tonic-gate 	} else {
20717c478bd9Sstevel@tonic-gate 		name = longname;
20727c478bd9Sstevel@tonic-gate 	}
20737c478bd9Sstevel@tonic-gate 	if (Aflag) {
20747c478bd9Sstevel@tonic-gate 		if ((prefix != NULL) && (*prefix != '\0'))
20757c478bd9Sstevel@tonic-gate 			while (*prefix == '/')
20767c478bd9Sstevel@tonic-gate 				++prefix;
20777c478bd9Sstevel@tonic-gate 		else
20787c478bd9Sstevel@tonic-gate 			while (*name == '/')
20797c478bd9Sstevel@tonic-gate 				++name;
20807c478bd9Sstevel@tonic-gate 	}
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate 	switch (stbuf.st_mode & S_IFMT) {
20837c478bd9Sstevel@tonic-gate 	case S_IFDIR:
20847c478bd9Sstevel@tonic-gate 		stbuf.st_size = (off_t)0;
20857c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(stbuf.st_size);
20867c478bd9Sstevel@tonic-gate 
20877c478bd9Sstevel@tonic-gate 		if (filetype != XATTR_FILE && Hiddendir == 0) {
20887c478bd9Sstevel@tonic-gate 			i = 0;
20897c478bd9Sstevel@tonic-gate 			cp = buf;
20907c478bd9Sstevel@tonic-gate 			while ((*cp++ = longname[i++]))
20917c478bd9Sstevel@tonic-gate 				;
20927c478bd9Sstevel@tonic-gate 			*--cp = '/';
20937c478bd9Sstevel@tonic-gate 			*++cp = 0;
20947c478bd9Sstevel@tonic-gate 		}
20957c478bd9Sstevel@tonic-gate 		if (!oflag) {
20967c478bd9Sstevel@tonic-gate 			tomodes(&stbuf);
20977c478bd9Sstevel@tonic-gate 			if (build_dblock(name, tchar, '5', filetype,
20987c478bd9Sstevel@tonic-gate 			    &stbuf, stbuf.st_dev, prefix) != 0) {
20997c478bd9Sstevel@tonic-gate 				goto out;
21007c478bd9Sstevel@tonic-gate 			}
21017c478bd9Sstevel@tonic-gate 			if (!Pflag) {
21027c478bd9Sstevel@tonic-gate 				/*
21037c478bd9Sstevel@tonic-gate 				 * Old archives require a slash at the end
21047c478bd9Sstevel@tonic-gate 				 * of a directory name.
21057c478bd9Sstevel@tonic-gate 				 *
21067c478bd9Sstevel@tonic-gate 				 * XXX
21077c478bd9Sstevel@tonic-gate 				 * If directory name is too long, will
21087c478bd9Sstevel@tonic-gate 				 * slash overfill field?
21097c478bd9Sstevel@tonic-gate 				 */
21107c478bd9Sstevel@tonic-gate 				if (strlen(name) > (unsigned)NAMSIZ-1) {
21117c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
21127c478bd9Sstevel@tonic-gate 					    "tar: %s: filename is greater "
21137c478bd9Sstevel@tonic-gate 					    "than %d\n"), name, NAMSIZ);
21147c478bd9Sstevel@tonic-gate 					if (errflag)
21157c478bd9Sstevel@tonic-gate 						exitflag = 1;
21167c478bd9Sstevel@tonic-gate 					Errflg = 1;
21177c478bd9Sstevel@tonic-gate 					goto out;
21187c478bd9Sstevel@tonic-gate 				} else {
21197c478bd9Sstevel@tonic-gate 					if (strlen(name) == (NAMSIZ - 1)) {
21207c478bd9Sstevel@tonic-gate 						(void) memcpy(dblock.dbuf.name,
21217c478bd9Sstevel@tonic-gate 						    name, NAMSIZ);
21227c478bd9Sstevel@tonic-gate 						dblock.dbuf.name[NAMSIZ-1]
21237c478bd9Sstevel@tonic-gate 						    = '/';
21247c478bd9Sstevel@tonic-gate 					} else
21257c478bd9Sstevel@tonic-gate 						(void) sprintf(dblock.dbuf.name,
21267c478bd9Sstevel@tonic-gate 						    "%s/", name);
21277c478bd9Sstevel@tonic-gate 
21287c478bd9Sstevel@tonic-gate 					/*
21297c478bd9Sstevel@tonic-gate 					 * need to recalculate checksum
21307c478bd9Sstevel@tonic-gate 					 * because the name changed.
21317c478bd9Sstevel@tonic-gate 					 */
21327c478bd9Sstevel@tonic-gate 					(void) sprintf(dblock.dbuf.chksum,
21337c478bd9Sstevel@tonic-gate 					    "%07o", checksum(&dblock));
21347c478bd9Sstevel@tonic-gate 				}
21357c478bd9Sstevel@tonic-gate 			}
21367c478bd9Sstevel@tonic-gate 
2137da6c28aaSamw 			if (put_extra_attributes(longname, shortname,
2138da6c28aaSamw 			    longattrname, prefix, filetype, '5') != 0)
21397c478bd9Sstevel@tonic-gate 				goto out;
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
21427c478bd9Sstevel@tonic-gate 			/*
21437c478bd9Sstevel@tonic-gate 			 * Reset header typeflag when archiving directory, since
21447c478bd9Sstevel@tonic-gate 			 * build_dblock changed it on us.
21457c478bd9Sstevel@tonic-gate 			 */
21467c478bd9Sstevel@tonic-gate 			if (filetype == XATTR_FILE) {
21477c478bd9Sstevel@tonic-gate 				dblock.dbuf.typeflag = _XATTR_HDRTYPE;
21487c478bd9Sstevel@tonic-gate 			} else {
21497c478bd9Sstevel@tonic-gate 				dblock.dbuf.typeflag = '5';
21507c478bd9Sstevel@tonic-gate 			}
21517c478bd9Sstevel@tonic-gate #else
21527c478bd9Sstevel@tonic-gate 			dblock.dbuf.typeflag = '5';
21537c478bd9Sstevel@tonic-gate #endif
21547c478bd9Sstevel@tonic-gate 
21557c478bd9Sstevel@tonic-gate 			(void) sprintf(dblock.dbuf.chksum, "%07o",
21567c478bd9Sstevel@tonic-gate 			    checksum(&dblock));
21577c478bd9Sstevel@tonic-gate 
21587c478bd9Sstevel@tonic-gate 			(void) writetbuf((char *)&dblock, 1);
21597c478bd9Sstevel@tonic-gate 		}
21607c478bd9Sstevel@tonic-gate 		if (vflag) {
21617c478bd9Sstevel@tonic-gate #ifdef DEBUG
21627c478bd9Sstevel@tonic-gate 			if (NotTape)
21637c478bd9Sstevel@tonic-gate 				DEBUG("seek = %" FMT_blkcnt_t "K\t", K(tapepos),
21647c478bd9Sstevel@tonic-gate 				    0);
21657c478bd9Sstevel@tonic-gate #endif
21667c478bd9Sstevel@tonic-gate 			if (filetype == XATTR_FILE && Hiddendir) {
21678e4a71aeSRich Burridge 				(void) fprintf(vfile,
21688e4a71aeSRich Burridge 				    gettext("a %s attribute %s "),
2169da6c28aaSamw 				    longname, longattrname);
21707c478bd9Sstevel@tonic-gate 
21717c478bd9Sstevel@tonic-gate 			} else {
21727c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "a %s/ ", longname);
21737c478bd9Sstevel@tonic-gate 			}
21747c478bd9Sstevel@tonic-gate 			if (NotTape)
21757c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "%" FMT_blkcnt_t "K\n",
21767c478bd9Sstevel@tonic-gate 				    K(blocks));
21777c478bd9Sstevel@tonic-gate 			else
21787c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("%" FMT_blkcnt_t
21797c478bd9Sstevel@tonic-gate 				    " tape blocks\n"), blocks);
21807c478bd9Sstevel@tonic-gate 		}
21817c478bd9Sstevel@tonic-gate 
21827c478bd9Sstevel@tonic-gate 		/*
21837c478bd9Sstevel@tonic-gate 		 * If hidden dir then break now since xattrs_put() will do
21847c478bd9Sstevel@tonic-gate 		 * the iterating of the directory.
21857c478bd9Sstevel@tonic-gate 		 *
2186da6c28aaSamw 		 * At the moment, there can only be system attributes on
2187da6c28aaSamw 		 * attributes.  There can be no attributes on attributes or
2188da6c28aaSamw 		 * directories within the attributes hidden directory hierarchy.
21897c478bd9Sstevel@tonic-gate 		 */
21907c478bd9Sstevel@tonic-gate 		if (filetype == XATTR_FILE)
21917c478bd9Sstevel@tonic-gate 			break;
21927c478bd9Sstevel@tonic-gate 
21937c478bd9Sstevel@tonic-gate 		if (*shortname != '/')
21947c478bd9Sstevel@tonic-gate 			(void) sprintf(newparent, "%s/%s", parent, shortname);
21957c478bd9Sstevel@tonic-gate 		else
21967c478bd9Sstevel@tonic-gate 			(void) sprintf(newparent, "%s", shortname);
21977c478bd9Sstevel@tonic-gate 
21987c478bd9Sstevel@tonic-gate 		if (chdir(shortname) < 0) {
21997c478bd9Sstevel@tonic-gate 			vperror(0, "%s", newparent);
22007c478bd9Sstevel@tonic-gate 			goto out;
22017c478bd9Sstevel@tonic-gate 		}
22027c478bd9Sstevel@tonic-gate 
22037c478bd9Sstevel@tonic-gate 		if ((dirp = opendir(".")) == NULL) {
22047c478bd9Sstevel@tonic-gate 			vperror(0, gettext(
22057c478bd9Sstevel@tonic-gate 			    "can't open directory %s"), longname);
22067c478bd9Sstevel@tonic-gate 			if (chdir(parent) < 0)
22077c478bd9Sstevel@tonic-gate 				vperror(0, gettext("cannot change back?: %s"),
22087c478bd9Sstevel@tonic-gate 				    parent);
22097c478bd9Sstevel@tonic-gate 			goto out;
22107c478bd9Sstevel@tonic-gate 		}
22117c478bd9Sstevel@tonic-gate 
22125e2174acSceastha 		/*
22135e2174acSceastha 		 * Create a list of files (children) in this directory to avoid
22145e2174acSceastha 		 * having to perform telldir()/seekdir().
22155e2174acSceastha 		 */
22167c478bd9Sstevel@tonic-gate 		while ((dp = readdir(dirp)) != NULL && !term) {
22177c478bd9Sstevel@tonic-gate 			if ((strcmp(".", dp->d_name) == 0) ||
22187c478bd9Sstevel@tonic-gate 			    (strcmp("..", dp->d_name) == 0))
22197c478bd9Sstevel@tonic-gate 				continue;
22205e2174acSceastha 			if (((cptr = (file_list_t *)calloc(sizeof (char),
22215e2174acSceastha 			    sizeof (file_list_t))) == NULL) ||
22225e2174acSceastha 			    ((cptr->name = strdup(dp->d_name)) == NULL)) {
22235e2174acSceastha 				vperror(1, gettext(
22245e2174acSceastha 				    "Insufficient memory for directory "
22255e2174acSceastha 				    "list entry %s/%s\n"),
22265e2174acSceastha 				    newparent, dp->d_name);
22275e2174acSceastha 			}
22287c478bd9Sstevel@tonic-gate 
22295e2174acSceastha 			/* Add the file to the list */
22305e2174acSceastha 			if (child == NULL) {
22315e2174acSceastha 				child = cptr;
22325e2174acSceastha 			} else {
22335e2174acSceastha 				child_end->next = cptr;
22345e2174acSceastha 			}
22355e2174acSceastha 			child_end = cptr;
22365e2174acSceastha 		}
22375e2174acSceastha 		(void) closedir(dirp);
22385e2174acSceastha 
22395e2174acSceastha 		/*
22405e2174acSceastha 		 * Archive each of the files in the current directory.
22415e2174acSceastha 		 * If a file is a directory, putfile() is called
22425e2174acSceastha 		 * recursively to archive the file hierarchy of the
22435e2174acSceastha 		 * directory before archiving the next file in the
22445e2174acSceastha 		 * current directory.
22455e2174acSceastha 		 */
22465e2174acSceastha 		while ((child != NULL) && !term) {
22475e2174acSceastha 			(void) strcpy(cp, child->name);
2248da6c28aaSamw 			archtype = putfile(buf, cp, newparent, NULL,
22497c478bd9Sstevel@tonic-gate 			    NORMAL_FILE, lev + 1, symlink_lev);
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate 			if (!exitflag) {
2252da6c28aaSamw 				if ((atflag || saflag) &&
2253da6c28aaSamw 				    (archtype == PUT_NOTAS_LINK)) {
2254da6c28aaSamw 					xattrs_put(buf, cp, newparent, NULL);
22557c478bd9Sstevel@tonic-gate 				}
22567c478bd9Sstevel@tonic-gate 			}
22577c478bd9Sstevel@tonic-gate 			if (exitflag)
22587c478bd9Sstevel@tonic-gate 				break;
22597c478bd9Sstevel@tonic-gate 
22605e2174acSceastha 			/* Free each child as we are done processing it. */
22615e2174acSceastha 			cptr = child;
22625e2174acSceastha 			child = child->next;
22635e2174acSceastha 			free(cptr->name);
22645e2174acSceastha 			free(cptr);
22657c478bd9Sstevel@tonic-gate 		}
22665e2174acSceastha 		if ((child != NULL) && !term) {
22675e2174acSceastha 			free_children(child);
22687c478bd9Sstevel@tonic-gate 		}
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate 		if (chdir(parent) < 0) {
22717c478bd9Sstevel@tonic-gate 			vperror(0, gettext("cannot change back?: %s"), parent);
22727c478bd9Sstevel@tonic-gate 		}
22737c478bd9Sstevel@tonic-gate 
22747c478bd9Sstevel@tonic-gate 		break;
22757c478bd9Sstevel@tonic-gate 
22767c478bd9Sstevel@tonic-gate 	case S_IFLNK:
22777c478bd9Sstevel@tonic-gate 		readlink_max = NAMSIZ;
22787c478bd9Sstevel@tonic-gate 		if (stbuf.st_size > NAMSIZ) {
22797c478bd9Sstevel@tonic-gate 			if (Eflag > 0) {
22807c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_LINKPATH;
22817c478bd9Sstevel@tonic-gate 				readlink_max = PATH_MAX;
22827c478bd9Sstevel@tonic-gate 			} else {
22837c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
22847c478bd9Sstevel@tonic-gate 				    "tar: %s: symbolic link too long\n"),
22857c478bd9Sstevel@tonic-gate 				    longname);
22867c478bd9Sstevel@tonic-gate 				if (errflag)
22877c478bd9Sstevel@tonic-gate 					exitflag = 1;
22887c478bd9Sstevel@tonic-gate 				Errflg = 1;
22897c478bd9Sstevel@tonic-gate 				goto out;
22907c478bd9Sstevel@tonic-gate 			}
22917c478bd9Sstevel@tonic-gate 		}
22927c478bd9Sstevel@tonic-gate 		/*
22937c478bd9Sstevel@tonic-gate 		 * Sym-links need header size of zero since you
22947c478bd9Sstevel@tonic-gate 		 * don't store any data for this type.
22957c478bd9Sstevel@tonic-gate 		 */
22967c478bd9Sstevel@tonic-gate 		stbuf.st_size = (off_t)0;
22977c478bd9Sstevel@tonic-gate 		tomodes(&stbuf);
22987c478bd9Sstevel@tonic-gate 		i = readlink(shortname, filetmp, readlink_max);
22997c478bd9Sstevel@tonic-gate 		if (i < 0) {
23007c478bd9Sstevel@tonic-gate 			vperror(0, gettext(
23017c478bd9Sstevel@tonic-gate 			    "can't read symbolic link %s"), longname);
23027c478bd9Sstevel@tonic-gate 			goto out;
23037c478bd9Sstevel@tonic-gate 		} else {
23047c478bd9Sstevel@tonic-gate 			filetmp[i] = 0;
23057c478bd9Sstevel@tonic-gate 		}
23067c478bd9Sstevel@tonic-gate 		if (vflag)
23077c478bd9Sstevel@tonic-gate 			(void) fprintf(vfile, gettext(
23087c478bd9Sstevel@tonic-gate 			    "a %s symbolic link to %s\n"),
23097c478bd9Sstevel@tonic-gate 			    longname, filetmp);
23107c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_LINKPATH) {
23117c478bd9Sstevel@tonic-gate 			Xtarhdr.x_linkpath = filetmp;
23127c478bd9Sstevel@tonic-gate 			if (build_dblock(name, tchar, '2', filetype, &stbuf,
23137c478bd9Sstevel@tonic-gate 			    stbuf.st_dev, prefix) != 0)
23147c478bd9Sstevel@tonic-gate 				goto out;
23157c478bd9Sstevel@tonic-gate 		} else
23167c478bd9Sstevel@tonic-gate 			if (build_dblock(name, filetmp, '2', filetype, &stbuf,
23177c478bd9Sstevel@tonic-gate 			    stbuf.st_dev, prefix) != 0)
23187c478bd9Sstevel@tonic-gate 				goto out;
23197c478bd9Sstevel@tonic-gate 		(void) writetbuf((char *)&dblock, 1);
23207c478bd9Sstevel@tonic-gate 		/*
23217c478bd9Sstevel@tonic-gate 		 * No acls for symlinks: mode is always 777
23227c478bd9Sstevel@tonic-gate 		 * dont call write ancillary
23237c478bd9Sstevel@tonic-gate 		 */
23247c478bd9Sstevel@tonic-gate 		rc = PUT_AS_LINK;
23257c478bd9Sstevel@tonic-gate 		break;
23267c478bd9Sstevel@tonic-gate 	case S_IFREG:
23277c478bd9Sstevel@tonic-gate 		if ((infile = openat(dirfd, shortname, 0)) < 0) {
23288e4a71aeSRich Burridge 			vperror(0, gettext("unable to open %s%s%s%s"), longname,
2329da6c28aaSamw 			    rw_sysattr ? gettext(" system") : "",
23307c478bd9Sstevel@tonic-gate 			    (filetype == XATTR_FILE) ?
23317c478bd9Sstevel@tonic-gate 			    gettext(" attribute ") : "",
2332da6c28aaSamw 			    (filetype == XATTR_FILE) ? (longattrname == NULL) ?
2333da6c28aaSamw 			    shortname : longattrname : "");
23347c478bd9Sstevel@tonic-gate 			goto out;
23357c478bd9Sstevel@tonic-gate 		}
23367c478bd9Sstevel@tonic-gate 
23377c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(stbuf.st_size);
23387c478bd9Sstevel@tonic-gate 
2339da6c28aaSamw 		if (put_link(name, longname, shortname, longattrname,
23407c478bd9Sstevel@tonic-gate 		    prefix, filetype, '1') == 0) {
23417c478bd9Sstevel@tonic-gate 			(void) close(infile);
23427c478bd9Sstevel@tonic-gate 			rc = PUT_AS_LINK;
23437c478bd9Sstevel@tonic-gate 			goto out;
23447c478bd9Sstevel@tonic-gate 		}
23457c478bd9Sstevel@tonic-gate 
23467c478bd9Sstevel@tonic-gate 		tomodes(&stbuf);
23477c478bd9Sstevel@tonic-gate 
23487c478bd9Sstevel@tonic-gate 		/* correctly handle end of volume */
23497c478bd9Sstevel@tonic-gate 		while (mulvol && tapepos + blocks + 1 > blocklim) {
23507c478bd9Sstevel@tonic-gate 			/* file won't fit */
23517c478bd9Sstevel@tonic-gate 			if (eflag) {
23527c478bd9Sstevel@tonic-gate 				if (blocks <= blocklim) {
23537c478bd9Sstevel@tonic-gate 					newvol();
23547c478bd9Sstevel@tonic-gate 					break;
23557c478bd9Sstevel@tonic-gate 				}
23567c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
23577c478bd9Sstevel@tonic-gate 				    "tar: Single file cannot fit on volume\n"));
23587c478bd9Sstevel@tonic-gate 				done(3);
23597c478bd9Sstevel@tonic-gate 			}
23607c478bd9Sstevel@tonic-gate 			/* split if floppy has some room and file is large */
23617c478bd9Sstevel@tonic-gate 			if (((blocklim - tapepos) >= EXTMIN) &&
23627c478bd9Sstevel@tonic-gate 			    ((blocks + 1) >= blocklim/10)) {
23637c478bd9Sstevel@tonic-gate 				splitfile(longname, infile,
23647c478bd9Sstevel@tonic-gate 				    name, prefix, filetype);
23657c478bd9Sstevel@tonic-gate 				(void) close(dirfd);
23667c478bd9Sstevel@tonic-gate 				(void) close(infile);
23677c478bd9Sstevel@tonic-gate 				goto out;
23687c478bd9Sstevel@tonic-gate 			}
23697c478bd9Sstevel@tonic-gate 			newvol();	/* not worth it--just get new volume */
23707c478bd9Sstevel@tonic-gate 		}
23717c478bd9Sstevel@tonic-gate #ifdef DEBUG
23727c478bd9Sstevel@tonic-gate 		DEBUG("putfile: %s wants %" FMT_blkcnt_t " blocks\n", longname,
23737c478bd9Sstevel@tonic-gate 		    blocks);
23747c478bd9Sstevel@tonic-gate #endif
23757c478bd9Sstevel@tonic-gate 		if (build_dblock(name, tchar, '0', filetype,
23767c478bd9Sstevel@tonic-gate 		    &stbuf, stbuf.st_dev, prefix) != 0) {
23777c478bd9Sstevel@tonic-gate 			goto out;
23787c478bd9Sstevel@tonic-gate 		}
23797c478bd9Sstevel@tonic-gate 		if (vflag) {
23807c478bd9Sstevel@tonic-gate #ifdef DEBUG
23817c478bd9Sstevel@tonic-gate 			if (NotTape)
23827c478bd9Sstevel@tonic-gate 				DEBUG("seek = %" FMT_blkcnt_t "K\t", K(tapepos),
23837c478bd9Sstevel@tonic-gate 				    0);
23847c478bd9Sstevel@tonic-gate #endif
2385da6c28aaSamw 			(void) fprintf(vfile, "a %s%s%s%s ", longname,
2386da6c28aaSamw 			    rw_sysattr ? gettext(" system") : "",
2387da6c28aaSamw 			    (filetype == XATTR_FILE) ? gettext(
2388da6c28aaSamw 			    " attribute ") : "",
23897c478bd9Sstevel@tonic-gate 			    (filetype == XATTR_FILE) ?
2390da6c28aaSamw 			    longattrname : "");
23917c478bd9Sstevel@tonic-gate 			if (NotTape)
23927c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "%" FMT_blkcnt_t "K\n",
23937c478bd9Sstevel@tonic-gate 				    K(blocks));
23947c478bd9Sstevel@tonic-gate 			else
23957c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile,
23967c478bd9Sstevel@tonic-gate 				    gettext("%" FMT_blkcnt_t " tape blocks\n"),
23977c478bd9Sstevel@tonic-gate 				    blocks);
23987c478bd9Sstevel@tonic-gate 		}
23997c478bd9Sstevel@tonic-gate 
2400da6c28aaSamw 		if (put_extra_attributes(longname, shortname, longattrname,
2401da6c28aaSamw 		    prefix, filetype, '0') != 0)
24027c478bd9Sstevel@tonic-gate 			goto out;
24037c478bd9Sstevel@tonic-gate 
24047c478bd9Sstevel@tonic-gate 		/*
24057c478bd9Sstevel@tonic-gate 		 * No need to reset typeflag for extended attribute here, since
24067c478bd9Sstevel@tonic-gate 		 * put_extra_attributes already set it and we haven't called
24077c478bd9Sstevel@tonic-gate 		 * build_dblock().
24087c478bd9Sstevel@tonic-gate 		 */
24097c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
24107c478bd9Sstevel@tonic-gate 		hint = writetbuf((char *)&dblock, 1);
2411eace40a5Sceastha 		maxread = max(min(stbuf.st_blksize, stbuf.st_size),
2412eace40a5Sceastha 		    (nblock * TBLOCK));
24137c478bd9Sstevel@tonic-gate 		if ((bigbuf = calloc((unsigned)maxread, sizeof (char))) == 0) {
24147c478bd9Sstevel@tonic-gate 			maxread = TBLOCK;
24157c478bd9Sstevel@tonic-gate 			bigbuf = buf;
24167c478bd9Sstevel@tonic-gate 		}
24177c478bd9Sstevel@tonic-gate 
24187c478bd9Sstevel@tonic-gate 		while (((i = (int)
24197c478bd9Sstevel@tonic-gate 		    read(infile, bigbuf, min((hint*TBLOCK), maxread))) > 0) &&
24207c478bd9Sstevel@tonic-gate 		    blocks) {
24217c478bd9Sstevel@tonic-gate 			blkcnt_t nblks;
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate 			nblks = ((i-1)/TBLOCK)+1;
24247c478bd9Sstevel@tonic-gate 			if (nblks > blocks)
24257c478bd9Sstevel@tonic-gate 				nblks = blocks;
24267c478bd9Sstevel@tonic-gate 			hint = writetbuf(bigbuf, nblks);
24277c478bd9Sstevel@tonic-gate 			blocks -= nblks;
24287c478bd9Sstevel@tonic-gate 		}
24297c478bd9Sstevel@tonic-gate 		(void) close(infile);
24307c478bd9Sstevel@tonic-gate 		if (bigbuf != buf)
24317c478bd9Sstevel@tonic-gate 			free(bigbuf);
24327c478bd9Sstevel@tonic-gate 		if (i < 0)
24337c478bd9Sstevel@tonic-gate 			vperror(0, gettext("Read error on %s"), longname);
24347c478bd9Sstevel@tonic-gate 		else if (blocks != 0 || i != 0) {
24357c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
24367c478bd9Sstevel@tonic-gate 			"tar: %s: file changed size\n"), longname);
24377c478bd9Sstevel@tonic-gate 			if (errflag) {
24387c478bd9Sstevel@tonic-gate 				exitflag = 1;
24397c478bd9Sstevel@tonic-gate 				Errflg = 1;
24407c478bd9Sstevel@tonic-gate 			} else if (!Dflag) {
24417c478bd9Sstevel@tonic-gate 				Errflg = 1;
24427c478bd9Sstevel@tonic-gate 			}
24437c478bd9Sstevel@tonic-gate 		}
24447c478bd9Sstevel@tonic-gate 		putempty(blocks);
24457c478bd9Sstevel@tonic-gate 		break;
24467c478bd9Sstevel@tonic-gate 	case S_IFIFO:
24477c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(stbuf.st_size);
24487c478bd9Sstevel@tonic-gate 		stbuf.st_size = (off_t)0;
24497c478bd9Sstevel@tonic-gate 
2450da6c28aaSamw 		if (put_link(name, longname, shortname, longattrname,
24517c478bd9Sstevel@tonic-gate 		    prefix, filetype, '6') == 0) {
24527c478bd9Sstevel@tonic-gate 			rc = PUT_AS_LINK;
24537c478bd9Sstevel@tonic-gate 			goto out;
24547c478bd9Sstevel@tonic-gate 		}
24557c478bd9Sstevel@tonic-gate 		tomodes(&stbuf);
24567c478bd9Sstevel@tonic-gate 
24577c478bd9Sstevel@tonic-gate 		while (mulvol && tapepos + blocks + 1 > blocklim) {
24587c478bd9Sstevel@tonic-gate 			if (eflag) {
24597c478bd9Sstevel@tonic-gate 				if (blocks <= blocklim) {
24607c478bd9Sstevel@tonic-gate 					newvol();
24617c478bd9Sstevel@tonic-gate 					break;
24627c478bd9Sstevel@tonic-gate 				}
24637c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
24647c478bd9Sstevel@tonic-gate 				    "tar: Single file cannot fit on volume\n"));
24657c478bd9Sstevel@tonic-gate 				done(3);
24667c478bd9Sstevel@tonic-gate 			}
24677c478bd9Sstevel@tonic-gate 
24687c478bd9Sstevel@tonic-gate 			if (((blocklim - tapepos) >= EXTMIN) &&
24697c478bd9Sstevel@tonic-gate 			    ((blocks + 1) >= blocklim/10)) {
24707c478bd9Sstevel@tonic-gate 				splitfile(longname, infile, name,
24717c478bd9Sstevel@tonic-gate 				    prefix, filetype);
24727c478bd9Sstevel@tonic-gate 				(void) close(dirfd);
24737c478bd9Sstevel@tonic-gate 				(void) close(infile);
24747c478bd9Sstevel@tonic-gate 				goto out;
24757c478bd9Sstevel@tonic-gate 			}
24767c478bd9Sstevel@tonic-gate 			newvol();
24777c478bd9Sstevel@tonic-gate 		}
24787c478bd9Sstevel@tonic-gate #ifdef DEBUG
24797c478bd9Sstevel@tonic-gate 		DEBUG("putfile: %s wants %" FMT_blkcnt_t " blocks\n", longname,
24807c478bd9Sstevel@tonic-gate 		    blocks);
24817c478bd9Sstevel@tonic-gate #endif
24827c478bd9Sstevel@tonic-gate 		if (vflag) {
24837c478bd9Sstevel@tonic-gate #ifdef DEBUG
24847c478bd9Sstevel@tonic-gate 			if (NotTape)
24857c478bd9Sstevel@tonic-gate 				DEBUG("seek = %" FMT_blkcnt_t "K\t", K(tapepos),
24867c478bd9Sstevel@tonic-gate 				    0);
24877c478bd9Sstevel@tonic-gate #endif
24887c478bd9Sstevel@tonic-gate 			if (NotTape)
24897c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("a %s %"
24907c478bd9Sstevel@tonic-gate 				    FMT_blkcnt_t "K\n "), longname, K(blocks));
24917c478bd9Sstevel@tonic-gate 			else
24927c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext(
24937c478bd9Sstevel@tonic-gate 				    "a %s %" FMT_blkcnt_t " tape blocks\n"),
24947c478bd9Sstevel@tonic-gate 				    longname, blocks);
24957c478bd9Sstevel@tonic-gate 		}
24967c478bd9Sstevel@tonic-gate 		if (build_dblock(name, tchar, '6', filetype,
24977c478bd9Sstevel@tonic-gate 		    &stbuf, stbuf.st_dev, prefix) != 0)
24987c478bd9Sstevel@tonic-gate 			goto out;
24997c478bd9Sstevel@tonic-gate 
2500da6c28aaSamw 		if (put_extra_attributes(longname, shortname, longattrname,
2501da6c28aaSamw 		    prefix, filetype, '6') != 0)
25027c478bd9Sstevel@tonic-gate 			goto out;
25037c478bd9Sstevel@tonic-gate 
25047c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
25057c478bd9Sstevel@tonic-gate 		dblock.dbuf.typeflag = '6';
25067c478bd9Sstevel@tonic-gate 
25077c478bd9Sstevel@tonic-gate 		(void) writetbuf((char *)&dblock, 1);
25087c478bd9Sstevel@tonic-gate 		break;
25097c478bd9Sstevel@tonic-gate 	case S_IFCHR:
25107c478bd9Sstevel@tonic-gate 		stbuf.st_size = (off_t)0;
25117c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(stbuf.st_size);
2512da6c28aaSamw 		if (put_link(name, longname, shortname, longattrname,
2513da6c28aaSamw 		    prefix, filetype, '3') == 0) {
25147c478bd9Sstevel@tonic-gate 			rc = PUT_AS_LINK;
25157c478bd9Sstevel@tonic-gate 			goto out;
25167c478bd9Sstevel@tonic-gate 		}
25177c478bd9Sstevel@tonic-gate 		tomodes(&stbuf);
25187c478bd9Sstevel@tonic-gate 
25197c478bd9Sstevel@tonic-gate 		while (mulvol && tapepos + blocks + 1 > blocklim) {
25207c478bd9Sstevel@tonic-gate 			if (eflag) {
25217c478bd9Sstevel@tonic-gate 				if (blocks <= blocklim) {
25227c478bd9Sstevel@tonic-gate 					newvol();
25237c478bd9Sstevel@tonic-gate 					break;
25247c478bd9Sstevel@tonic-gate 				}
25257c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
25267c478bd9Sstevel@tonic-gate 				    "tar: Single file cannot fit on volume\n"));
25277c478bd9Sstevel@tonic-gate 				done(3);
25287c478bd9Sstevel@tonic-gate 			}
25297c478bd9Sstevel@tonic-gate 
25307c478bd9Sstevel@tonic-gate 			if (((blocklim - tapepos) >= EXTMIN) &&
25317c478bd9Sstevel@tonic-gate 			    ((blocks + 1) >= blocklim/10)) {
25327c478bd9Sstevel@tonic-gate 				splitfile(longname, infile, name,
25337c478bd9Sstevel@tonic-gate 				    prefix, filetype);
25347c478bd9Sstevel@tonic-gate 				(void) close(dirfd);
25357c478bd9Sstevel@tonic-gate 				goto out;
25367c478bd9Sstevel@tonic-gate 			}
25377c478bd9Sstevel@tonic-gate 			newvol();
25387c478bd9Sstevel@tonic-gate 		}
25397c478bd9Sstevel@tonic-gate #ifdef DEBUG
25407c478bd9Sstevel@tonic-gate 		DEBUG("putfile: %s wants %" FMT_blkcnt_t " blocks\n", longname,
25417c478bd9Sstevel@tonic-gate 		    blocks);
25427c478bd9Sstevel@tonic-gate #endif
25437c478bd9Sstevel@tonic-gate 		if (vflag) {
25447c478bd9Sstevel@tonic-gate #ifdef DEBUG
25457c478bd9Sstevel@tonic-gate 			if (NotTape)
25467c478bd9Sstevel@tonic-gate 				DEBUG("seek = %" FMT_blkcnt_t "K\t", K(tapepos),
25477c478bd9Sstevel@tonic-gate 				    0);
25487c478bd9Sstevel@tonic-gate #endif
25497c478bd9Sstevel@tonic-gate 			if (NotTape)
25507c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("a %s %"
25517c478bd9Sstevel@tonic-gate 				    FMT_blkcnt_t "K\n"), longname, K(blocks));
25527c478bd9Sstevel@tonic-gate 			else
25537c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("a %s %"
25547c478bd9Sstevel@tonic-gate 				    FMT_blkcnt_t " tape blocks\n"), longname,
25557c478bd9Sstevel@tonic-gate 				    blocks);
25567c478bd9Sstevel@tonic-gate 		}
25577c478bd9Sstevel@tonic-gate 		if (build_dblock(name, tchar, '3',
25587c478bd9Sstevel@tonic-gate 		    filetype, &stbuf, stbuf.st_rdev, prefix) != 0)
25597c478bd9Sstevel@tonic-gate 			goto out;
25607c478bd9Sstevel@tonic-gate 
2561da6c28aaSamw 		if (put_extra_attributes(longname, shortname, longattrname,
25627c478bd9Sstevel@tonic-gate 		    prefix, filetype, '3') != 0)
25637c478bd9Sstevel@tonic-gate 			goto out;
25647c478bd9Sstevel@tonic-gate 
25657c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
25667c478bd9Sstevel@tonic-gate 		dblock.dbuf.typeflag = '3';
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate 		(void) writetbuf((char *)&dblock, 1);
25697c478bd9Sstevel@tonic-gate 		break;
25707c478bd9Sstevel@tonic-gate 	case S_IFBLK:
25717c478bd9Sstevel@tonic-gate 		stbuf.st_size = (off_t)0;
25727c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(stbuf.st_size);
2573da6c28aaSamw 		if (put_link(name, longname, shortname, longattrname,
2574da6c28aaSamw 		    prefix, filetype, '4') == 0) {
25757c478bd9Sstevel@tonic-gate 			rc = PUT_AS_LINK;
25767c478bd9Sstevel@tonic-gate 			goto out;
25777c478bd9Sstevel@tonic-gate 		}
25787c478bd9Sstevel@tonic-gate 		tomodes(&stbuf);
25797c478bd9Sstevel@tonic-gate 
25807c478bd9Sstevel@tonic-gate 		while (mulvol && tapepos + blocks + 1 > blocklim) {
25817c478bd9Sstevel@tonic-gate 			if (eflag) {
25827c478bd9Sstevel@tonic-gate 				if (blocks <= blocklim) {
25837c478bd9Sstevel@tonic-gate 					newvol();
25847c478bd9Sstevel@tonic-gate 					break;
25857c478bd9Sstevel@tonic-gate 				}
25867c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
25877c478bd9Sstevel@tonic-gate 				    "tar: Single file cannot fit on volume\n"));
25887c478bd9Sstevel@tonic-gate 				done(3);
25897c478bd9Sstevel@tonic-gate 			}
25907c478bd9Sstevel@tonic-gate 
25917c478bd9Sstevel@tonic-gate 			if (((blocklim - tapepos) >= EXTMIN) &&
25927c478bd9Sstevel@tonic-gate 			    ((blocks + 1) >= blocklim/10)) {
25937c478bd9Sstevel@tonic-gate 				splitfile(longname, infile,
25947c478bd9Sstevel@tonic-gate 				    name, prefix, filetype);
25957c478bd9Sstevel@tonic-gate 				(void) close(dirfd);
25967c478bd9Sstevel@tonic-gate 				goto out;
25977c478bd9Sstevel@tonic-gate 			}
25987c478bd9Sstevel@tonic-gate 			newvol();
25997c478bd9Sstevel@tonic-gate 		}
26007c478bd9Sstevel@tonic-gate #ifdef DEBUG
26017c478bd9Sstevel@tonic-gate 		DEBUG("putfile: %s wants %" FMT_blkcnt_t " blocks\n", longname,
26027c478bd9Sstevel@tonic-gate 		    blocks);
26037c478bd9Sstevel@tonic-gate #endif
26047c478bd9Sstevel@tonic-gate 		if (vflag) {
26057c478bd9Sstevel@tonic-gate #ifdef DEBUG
26067c478bd9Sstevel@tonic-gate 			if (NotTape)
26077c478bd9Sstevel@tonic-gate 				DEBUG("seek = %" FMT_blkcnt_t "K\t", K(tapepos),
26087c478bd9Sstevel@tonic-gate 				    0);
26097c478bd9Sstevel@tonic-gate #endif
26107c478bd9Sstevel@tonic-gate 			(void) fprintf(vfile, "a %s ", longname);
26117c478bd9Sstevel@tonic-gate 			if (NotTape)
26127c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "%" FMT_blkcnt_t "K\n",
26137c478bd9Sstevel@tonic-gate 				    K(blocks));
26147c478bd9Sstevel@tonic-gate 			else
26157c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("%"
26167c478bd9Sstevel@tonic-gate 				    FMT_blkcnt_t " tape blocks\n"), blocks);
26177c478bd9Sstevel@tonic-gate 		}
26187c478bd9Sstevel@tonic-gate 		if (build_dblock(name, tchar, '4',
26197c478bd9Sstevel@tonic-gate 		    filetype, &stbuf, stbuf.st_rdev, prefix) != 0)
26207c478bd9Sstevel@tonic-gate 			goto out;
26217c478bd9Sstevel@tonic-gate 
2622da6c28aaSamw 		if (put_extra_attributes(longname, shortname, longattrname,
26237c478bd9Sstevel@tonic-gate 		    prefix, filetype, '4') != 0)
26247c478bd9Sstevel@tonic-gate 			goto out;
26257c478bd9Sstevel@tonic-gate 
26267c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
26277c478bd9Sstevel@tonic-gate 		dblock.dbuf.typeflag = '4';
26287c478bd9Sstevel@tonic-gate 
26297c478bd9Sstevel@tonic-gate 		(void) writetbuf((char *)&dblock, 1);
26307c478bd9Sstevel@tonic-gate 		break;
26317c478bd9Sstevel@tonic-gate 	default:
26327c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
26337c478bd9Sstevel@tonic-gate 		    "tar: %s is not a file. Not dumped\n"), longname);
26347c478bd9Sstevel@tonic-gate 		if (errflag)
26357c478bd9Sstevel@tonic-gate 			exitflag = 1;
26367c478bd9Sstevel@tonic-gate 		Errflg = 1;
26377c478bd9Sstevel@tonic-gate 		goto out;
26387c478bd9Sstevel@tonic-gate 	}
26397c478bd9Sstevel@tonic-gate 
26407c478bd9Sstevel@tonic-gate out:
2641da6c28aaSamw 	if ((dirfd != -1) && (filetype != XATTR_FILE)) {
26427c478bd9Sstevel@tonic-gate 		(void) close(dirfd);
26437c478bd9Sstevel@tonic-gate 	}
26447c478bd9Sstevel@tonic-gate 	return (rc);
26457c478bd9Sstevel@tonic-gate }
26467c478bd9Sstevel@tonic-gate 
26477c478bd9Sstevel@tonic-gate 
26487c478bd9Sstevel@tonic-gate /*
26497c478bd9Sstevel@tonic-gate  *	splitfile	dump a large file across volumes
26507c478bd9Sstevel@tonic-gate  *
26517c478bd9Sstevel@tonic-gate  *	splitfile(longname, fd);
26527c478bd9Sstevel@tonic-gate  *		char *longname;		full name of file
26537c478bd9Sstevel@tonic-gate  *		int ifd;		input file descriptor
26547c478bd9Sstevel@tonic-gate  *
26557c478bd9Sstevel@tonic-gate  *	NOTE:  only called by putfile() to dump a large file.
26567c478bd9Sstevel@tonic-gate  */
26577c478bd9Sstevel@tonic-gate 
26587c478bd9Sstevel@tonic-gate static void
26597c478bd9Sstevel@tonic-gate splitfile(char *longname, int ifd, char *name, char *prefix, int filetype)
26607c478bd9Sstevel@tonic-gate {
26617c478bd9Sstevel@tonic-gate 	blkcnt_t blocks;
26627c478bd9Sstevel@tonic-gate 	off_t bytes, s;
26637c478bd9Sstevel@tonic-gate 	char buf[TBLOCK];
26647c478bd9Sstevel@tonic-gate 	int i, extents;
26657c478bd9Sstevel@tonic-gate 
26667c478bd9Sstevel@tonic-gate 	blocks = TBLOCKS(stbuf.st_size);	/* blocks file needs */
26677c478bd9Sstevel@tonic-gate 
26687c478bd9Sstevel@tonic-gate 	/*
26697c478bd9Sstevel@tonic-gate 	 * # extents =
26707c478bd9Sstevel@tonic-gate 	 *	size of file after using up rest of this floppy
26717c478bd9Sstevel@tonic-gate 	 *		blocks - (blocklim - tapepos) + 1	(for header)
26727c478bd9Sstevel@tonic-gate 	 *	plus roundup value before divide by blocklim-1
26737c478bd9Sstevel@tonic-gate 	 *		+ (blocklim - 1) - 1
26747c478bd9Sstevel@tonic-gate 	 *	all divided by blocklim-1 (one block for each header).
26757c478bd9Sstevel@tonic-gate 	 * this gives
26767c478bd9Sstevel@tonic-gate 	 *	(blocks - blocklim + tapepos + 1 + blocklim - 2)/(blocklim-1)
26777c478bd9Sstevel@tonic-gate 	 * which reduces to the expression used.
26787c478bd9Sstevel@tonic-gate 	 * one is added to account for this first extent.
26797c478bd9Sstevel@tonic-gate 	 *
26807c478bd9Sstevel@tonic-gate 	 * When one is dealing with extremely large archives, one may want
26817c478bd9Sstevel@tonic-gate 	 * to allow for a large number of extents.  This code should be
26827c478bd9Sstevel@tonic-gate 	 * revisited to determine if extents should be changed to something
26837c478bd9Sstevel@tonic-gate 	 * larger than an int.
26847c478bd9Sstevel@tonic-gate 	 */
26857c478bd9Sstevel@tonic-gate 	extents = (int)((blocks + tapepos - 1ULL)/(blocklim - 1ULL) + 1);
26867c478bd9Sstevel@tonic-gate 
26877c478bd9Sstevel@tonic-gate 	if (extents < 2 || extents > MAXEXT) {	/* let's be reasonable */
26887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
26897c478bd9Sstevel@tonic-gate 		    "tar: %s needs unusual number of volumes to split\n"
26907c478bd9Sstevel@tonic-gate 		    "tar: %s not dumped\n"), longname, longname);
26917c478bd9Sstevel@tonic-gate 		return;
26927c478bd9Sstevel@tonic-gate 	}
26937c478bd9Sstevel@tonic-gate 	if (build_dblock(name, tchar, '0', filetype,
26947c478bd9Sstevel@tonic-gate 	    &stbuf, stbuf.st_dev, prefix) != 0)
26957c478bd9Sstevel@tonic-gate 		return;
26967c478bd9Sstevel@tonic-gate 
26977c478bd9Sstevel@tonic-gate 	dblock.dbuf.extotal = extents;
26987c478bd9Sstevel@tonic-gate 	bytes = stbuf.st_size;
26997c478bd9Sstevel@tonic-gate 
27007c478bd9Sstevel@tonic-gate 	/*
27017c478bd9Sstevel@tonic-gate 	 * The value contained in dblock.dbuf.efsize was formerly used when the
27027c478bd9Sstevel@tonic-gate 	 * v flag was specified in conjunction with the t flag. Although it is
27037c478bd9Sstevel@tonic-gate 	 * no longer used, older versions of tar will expect the former
27047c478bd9Sstevel@tonic-gate 	 * behaviour, so we must continue to write it to the archive.
27057c478bd9Sstevel@tonic-gate 	 *
27067c478bd9Sstevel@tonic-gate 	 * Since dblock.dbuf.efsize is 10 chars in size, the maximum value it
27077c478bd9Sstevel@tonic-gate 	 * can store is TAR_EFSIZE_MAX. If bytes exceeds that value, simply
27087c478bd9Sstevel@tonic-gate 	 * store 0.
27097c478bd9Sstevel@tonic-gate 	 */
27107c478bd9Sstevel@tonic-gate 	if (bytes <= TAR_EFSIZE_MAX)
27117c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.efsize, "%9" FMT_off_t_o, bytes);
27127c478bd9Sstevel@tonic-gate 	else
27137c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.efsize, "%9" FMT_off_t_o, (off_t)0);
27147c478bd9Sstevel@tonic-gate 
27157c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
27167c478bd9Sstevel@tonic-gate 	    "tar: large file %s needs %d extents.\n"
27177c478bd9Sstevel@tonic-gate 	    "tar: current device seek position = %" FMT_blkcnt_t "K\n"),
27187c478bd9Sstevel@tonic-gate 	    longname, extents, K(tapepos));
27197c478bd9Sstevel@tonic-gate 
27207c478bd9Sstevel@tonic-gate 	s = (off_t)(blocklim - tapepos - 1) * TBLOCK;
27217c478bd9Sstevel@tonic-gate 	for (i = 1; i <= extents; i++) {
27227c478bd9Sstevel@tonic-gate 		if (i > 1) {
27237c478bd9Sstevel@tonic-gate 			newvol();
27247c478bd9Sstevel@tonic-gate 			if (i == extents)
27257c478bd9Sstevel@tonic-gate 				s = bytes;	/* last ext. gets true bytes */
27267c478bd9Sstevel@tonic-gate 			else
27277c478bd9Sstevel@tonic-gate 				s = (off_t)(blocklim - 1)*TBLOCK; /* all */
27287c478bd9Sstevel@tonic-gate 		}
27297c478bd9Sstevel@tonic-gate 		bytes -= s;
27307c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(s);
27317c478bd9Sstevel@tonic-gate 
27327c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.size, "%011" FMT_off_t_o, s);
27337c478bd9Sstevel@tonic-gate 		dblock.dbuf.extno = i;
27347c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
27357c478bd9Sstevel@tonic-gate 		(void) writetbuf((char *)&dblock, 1);
27367c478bd9Sstevel@tonic-gate 
27377c478bd9Sstevel@tonic-gate 		if (vflag)
27387c478bd9Sstevel@tonic-gate 			(void) fprintf(vfile,
27398e4a71aeSRich Burridge 			    gettext("+++ a %s %" FMT_blkcnt_t
27408e4a71aeSRich Burridge 			    "K [extent #%d of %d]\n"),
27417c478bd9Sstevel@tonic-gate 			    longname, K(blocks), i, extents);
27427c478bd9Sstevel@tonic-gate 		while (blocks && read(ifd, buf, TBLOCK) > 0) {
27437c478bd9Sstevel@tonic-gate 			blocks--;
27447c478bd9Sstevel@tonic-gate 			(void) writetbuf(buf, 1);
27457c478bd9Sstevel@tonic-gate 		}
27467c478bd9Sstevel@tonic-gate 		if (blocks != 0) {
27477c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
27487c478bd9Sstevel@tonic-gate 			    "tar: %s: file changed size\n"), longname);
27497c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
27507c478bd9Sstevel@tonic-gate 			    "tar: aborting split file %s\n"), longname);
27517c478bd9Sstevel@tonic-gate 			(void) close(ifd);
27527c478bd9Sstevel@tonic-gate 			return;
27537c478bd9Sstevel@tonic-gate 		}
27547c478bd9Sstevel@tonic-gate 	}
27557c478bd9Sstevel@tonic-gate 	(void) close(ifd);
27567c478bd9Sstevel@tonic-gate 	if (vflag)
27577c478bd9Sstevel@tonic-gate 		(void) fprintf(vfile, gettext("a %s %" FMT_off_t "K (in %d "
27587c478bd9Sstevel@tonic-gate 		    "extents)\n"), longname, K(TBLOCKS(stbuf.st_size)),
27597c478bd9Sstevel@tonic-gate 		    extents);
27607c478bd9Sstevel@tonic-gate }
27617c478bd9Sstevel@tonic-gate 
27627c478bd9Sstevel@tonic-gate /*
27637c478bd9Sstevel@tonic-gate  *	convtoreg - determines whether the file should be converted to a
27647c478bd9Sstevel@tonic-gate  *	            regular file when extracted
27657c478bd9Sstevel@tonic-gate  *
27667c478bd9Sstevel@tonic-gate  *	Returns 1 when file size > 0 and typeflag is not recognized
27677c478bd9Sstevel@tonic-gate  * 	Otherwise returns 0
27687c478bd9Sstevel@tonic-gate  */
27697c478bd9Sstevel@tonic-gate static int
27707c478bd9Sstevel@tonic-gate convtoreg(off_t size)
27717c478bd9Sstevel@tonic-gate {
27727c478bd9Sstevel@tonic-gate 	if ((size > 0) && (dblock.dbuf.typeflag != '0') &&
27737c478bd9Sstevel@tonic-gate 	    (dblock.dbuf.typeflag != NULL) && (dblock.dbuf.typeflag != '1') &&
27747c478bd9Sstevel@tonic-gate 	    (dblock.dbuf.typeflag != '2') && (dblock.dbuf.typeflag != '3') &&
27757c478bd9Sstevel@tonic-gate 	    (dblock.dbuf.typeflag != '4') && (dblock.dbuf.typeflag != '5') &&
27767c478bd9Sstevel@tonic-gate 	    (dblock.dbuf.typeflag != '6') && (dblock.dbuf.typeflag != 'A') &&
2777e765faefSRich Burridge 	    (dblock.dbuf.typeflag != 'L') &&
27787c478bd9Sstevel@tonic-gate 	    (dblock.dbuf.typeflag != _XATTR_HDRTYPE) &&
27797c478bd9Sstevel@tonic-gate 	    (dblock.dbuf.typeflag != 'X')) {
27807c478bd9Sstevel@tonic-gate 		return (1);
27817c478bd9Sstevel@tonic-gate 	}
27827c478bd9Sstevel@tonic-gate 	return (0);
27837c478bd9Sstevel@tonic-gate }
27847c478bd9Sstevel@tonic-gate 
2785da6c28aaSamw #if defined(O_XATTR)
2786da6c28aaSamw static int
2787da6c28aaSamw save_cwd(void)
2788da6c28aaSamw {
2789da6c28aaSamw 	return (open(".", O_RDONLY));
2790da6c28aaSamw }
2791da6c28aaSamw #endif
2792da6c28aaSamw 
2793da6c28aaSamw #if defined(O_XATTR)
2794da6c28aaSamw static void
2795da6c28aaSamw rest_cwd(int *cwd)
2796da6c28aaSamw {
2797da6c28aaSamw 	if (*cwd != -1) {
2798da6c28aaSamw 		if (fchdir(*cwd) < 0) {
2799da6c28aaSamw 			vperror(0, gettext(
2800da6c28aaSamw 			    "Cannot fchdir to attribute directory"));
2801da6c28aaSamw 			exit(1);
2802da6c28aaSamw 		}
2803da6c28aaSamw 		(void) close(*cwd);
2804da6c28aaSamw 		*cwd = -1;
2805da6c28aaSamw 	}
2806da6c28aaSamw }
2807da6c28aaSamw #endif
2808da6c28aaSamw 
2809da6c28aaSamw /*
2810da6c28aaSamw  * Verify the underlying file system supports the attribute type.
2811da6c28aaSamw  * Only archive extended attribute files when '-@' was specified.
2812da6c28aaSamw  * Only archive system extended attribute files if '-/' was specified.
2813da6c28aaSamw  */
2814da6c28aaSamw #if defined(O_XATTR)
2815da6c28aaSamw static attr_status_t
2816ced83f9bSceastha verify_attr_support(char *filename, int attrflg, arc_action_t actflag,
2817ced83f9bSceastha     int *ext_attrflg)
2818da6c28aaSamw {
2819da6c28aaSamw 	/*
2820ced83f9bSceastha 	 * Verify extended attributes are supported/exist.  We only
2821ced83f9bSceastha 	 * need to check if we are processing a base file, not an
2822ced83f9bSceastha 	 * extended attribute.
2823da6c28aaSamw 	 */
2824ced83f9bSceastha 	if (attrflg) {
2825ced83f9bSceastha 		*ext_attrflg = (pathconf(filename, (actflag == ARC_CREATE) ?
2826ced83f9bSceastha 		    _PC_XATTR_EXISTS : _PC_XATTR_ENABLED) == 1);
2827ced83f9bSceastha 	}
2828ced83f9bSceastha 
2829da6c28aaSamw 	if (atflag) {
2830ced83f9bSceastha 		if (!*ext_attrflg) {
2831da6c28aaSamw #if defined(_PC_SATTR_ENABLED)
2832da6c28aaSamw 			if (saflag) {
2833da6c28aaSamw 				/* Verify system attributes are supported */
2834da6c28aaSamw 				if (sysattr_support(filename,
2835da6c28aaSamw 				    (actflag == ARC_CREATE) ? _PC_SATTR_EXISTS :
2836da6c28aaSamw 				    _PC_SATTR_ENABLED) != 1) {
2837da6c28aaSamw 					return (ATTR_SATTR_ERR);
2838da6c28aaSamw 				}
2839da6c28aaSamw 			} else
2840da6c28aaSamw 				return (ATTR_XATTR_ERR);
2841da6c28aaSamw #else
2842da6c28aaSamw 				return (ATTR_XATTR_ERR);
2843da6c28aaSamw #endif	/* _PC_SATTR_ENABLED */
2844da6c28aaSamw 		}
2845da6c28aaSamw 
2846da6c28aaSamw #if defined(_PC_SATTR_ENABLED)
2847da6c28aaSamw 	} else if (saflag) {
2848da6c28aaSamw 		/* Verify system attributes are supported */
2849da6c28aaSamw 		if (sysattr_support(filename, (actflag == ARC_CREATE) ?
2850da6c28aaSamw 		    _PC_SATTR_EXISTS : _PC_SATTR_ENABLED) != 1) {
2851da6c28aaSamw 			return (ATTR_SATTR_ERR);
2852da6c28aaSamw 		}
2853da6c28aaSamw #endif	/* _PC_SATTR_ENABLED */
2854da6c28aaSamw 	} else {
2855da6c28aaSamw 		return (ATTR_SKIP);
2856da6c28aaSamw 	}
2857da6c28aaSamw 
2858da6c28aaSamw 	return (ATTR_OK);
2859da6c28aaSamw }
2860da6c28aaSamw #endif
2861da6c28aaSamw 
2862da6c28aaSamw #if defined(O_XATTR)
2863da6c28aaSamw /*
2864da6c28aaSamw  * Recursively open attribute directories until the attribute directory
2865da6c28aaSamw  * containing the specified attribute, attrname, is opened.
2866da6c28aaSamw  *
2867da6c28aaSamw  * Currently, only 2 directory levels of attributes are supported, (i.e.,
2868da6c28aaSamw  * extended system attributes on extended attributes).  The following are
2869da6c28aaSamw  * the possible input combinations:
2870da6c28aaSamw  *	1.  Open the attribute directory of the base file (don't change
2871da6c28aaSamw  *	    into it).
2872da6c28aaSamw  *		attrinfo->parent = NULL
2873da6c28aaSamw  *		attrname = '.'
2874da6c28aaSamw  *	2.  Open the attribute directory of the base file and change into it.
2875da6c28aaSamw  *		attrinfo->parent = NULL
2876da6c28aaSamw  *		attrname = <attr> | <sys_attr>
2877da6c28aaSamw  *	3.  Open the attribute directory of the base file, change into it,
2878da6c28aaSamw  *	    then recursively call open_attr_dir() to open the attribute's
2879da6c28aaSamw  *	    parent directory (don't change into it).
2880da6c28aaSamw  *		attrinfo->parent = <attr>
2881da6c28aaSamw  *		attrname = '.'
2882da6c28aaSamw  *	4.  Open the attribute directory of the base file, change into it,
2883da6c28aaSamw  *	    then recursively call open_attr_dir() to open the attribute's
2884da6c28aaSamw  *	    parent directory and change into it.
2885da6c28aaSamw  *		attrinfo->parent = <attr>
2886da6c28aaSamw  *		attrname = <attr> | <sys_attr>
2887da6c28aaSamw  *
2888da6c28aaSamw  * An attribute directory will be opened only if the underlying file system
2889da6c28aaSamw  * supports the attribute type, and if the command line specifications (atflag
2890da6c28aaSamw  * and saflag) enable the processing of the attribute type.
2891da6c28aaSamw  *
2892da6c28aaSamw  * On succesful return, attrinfo->parentfd will be the file descriptor of the
2893da6c28aaSamw  * opened attribute directory.  In addition, if the attribute is a read-write
2894da6c28aaSamw  * extended system attribute, attrinfo->rw_sysattr will be set to 1, otherwise
2895da6c28aaSamw  * it will be set to 0.
2896da6c28aaSamw  *
2897da6c28aaSamw  * Possible return values:
2898da6c28aaSamw  * 	ATTR_OK		Successfully opened and, if needed, changed into the
2899da6c28aaSamw  *			attribute directory containing attrname.
2900da6c28aaSamw  *	ATTR_SKIP	The command line specifications don't enable the
2901da6c28aaSamw  *			processing of the attribute type.
2902da6c28aaSamw  * 	ATTR_CHDIR_ERR	An error occurred while trying to change into an
2903da6c28aaSamw  *			attribute directory.
2904da6c28aaSamw  * 	ATTR_OPEN_ERR	An error occurred while trying to open an
2905da6c28aaSamw  *			attribute directory.
2906da6c28aaSamw  *	ATTR_XATTR_ERR	The underlying file system doesn't support extended
2907da6c28aaSamw  *			attributes.
2908da6c28aaSamw  *	ATTR_SATTR_ERR	The underlying file system doesn't support extended
2909da6c28aaSamw  *			system attributes.
2910da6c28aaSamw  */
2911da6c28aaSamw static int
2912da6c28aaSamw open_attr_dir(char *attrname, char *dirp, int cwd, attr_data_t *attrinfo)
2913da6c28aaSamw {
2914da6c28aaSamw 	attr_status_t	rc;
2915da6c28aaSamw 	int		firsttime = (attrinfo->attr_parentfd == -1);
2916da6c28aaSamw 	int		saveerrno;
2917ced83f9bSceastha 	int		ext_attr;
2918da6c28aaSamw 
2919da6c28aaSamw 	/*
2920da6c28aaSamw 	 * open_attr_dir() was recursively called (input combination number 4),
2921da6c28aaSamw 	 * close the previously opened file descriptor as we've already changed
2922da6c28aaSamw 	 * into it.
2923da6c28aaSamw 	 */
2924da6c28aaSamw 	if (!firsttime) {
2925da6c28aaSamw 		(void) close(attrinfo->attr_parentfd);
2926da6c28aaSamw 		attrinfo->attr_parentfd = -1;
2927da6c28aaSamw 	}
2928da6c28aaSamw 
2929da6c28aaSamw 	/*
2930da6c28aaSamw 	 * Verify that the underlying file system supports the restoration
2931da6c28aaSamw 	 * of the attribute.
2932da6c28aaSamw 	 */
2933ced83f9bSceastha 	if ((rc = verify_attr_support(dirp, firsttime, ARC_RESTORE,
2934ced83f9bSceastha 	    &ext_attr)) != ATTR_OK) {
2935da6c28aaSamw 		return (rc);
2936da6c28aaSamw 	}
2937da6c28aaSamw 
2938da6c28aaSamw 	/* Open the base file's attribute directory */
2939da6c28aaSamw 	if ((attrinfo->attr_parentfd = attropen(dirp, ".", O_RDONLY)) == -1) {
2940da6c28aaSamw 		/*
2941da6c28aaSamw 		 * Save the errno from the attropen so it can be reported
2942da6c28aaSamw 		 * if the retry of the attropen fails.
2943da6c28aaSamw 		 */
2944da6c28aaSamw 		saveerrno = errno;
2945da6c28aaSamw 		if ((attrinfo->attr_parentfd = retry_open_attr(-1, cwd, dirp,
2946da6c28aaSamw 		    NULL, ".", O_RDONLY, 0)) == -1) {
2947da6c28aaSamw 			/*
2948da6c28aaSamw 			 * Reset typeflag back to real value so passtape
2949da6c28aaSamw 			 * will skip ahead correctly.
2950da6c28aaSamw 			 */
2951da6c28aaSamw 			dblock.dbuf.typeflag = _XATTR_HDRTYPE;
2952da6c28aaSamw 			(void) close(attrinfo->attr_parentfd);
2953da6c28aaSamw 			attrinfo->attr_parentfd = -1;
2954da6c28aaSamw 			errno = saveerrno;
2955da6c28aaSamw 			return (ATTR_OPEN_ERR);
2956da6c28aaSamw 		}
2957da6c28aaSamw 	}
2958da6c28aaSamw 
2959da6c28aaSamw 	/*
2960da6c28aaSamw 	 * Change into the parent attribute's directory unless we are
2961da6c28aaSamw 	 * processing the hidden attribute directory of the base file itself.
2962da6c28aaSamw 	 */
2963da6c28aaSamw 	if ((Hiddendir == 0) || (firsttime && attrinfo->attr_parent != NULL)) {
2964da6c28aaSamw 		if (fchdir(attrinfo->attr_parentfd) != 0) {
2965da6c28aaSamw 			saveerrno = errno;
2966da6c28aaSamw 			(void) close(attrinfo->attr_parentfd);
2967da6c28aaSamw 			attrinfo->attr_parentfd = -1;
2968da6c28aaSamw 			errno = saveerrno;
2969da6c28aaSamw 			return (ATTR_CHDIR_ERR);
2970da6c28aaSamw 		}
2971da6c28aaSamw 	}
2972da6c28aaSamw 
2973da6c28aaSamw 	/* Determine if the attribute should be processed */
2974da6c28aaSamw 	if ((rc = verify_attr(attrname, attrinfo->attr_parent, 1,
2975da6c28aaSamw 	    &attrinfo->attr_rw_sysattr)) != ATTR_OK) {
2976da6c28aaSamw 		saveerrno = errno;
2977da6c28aaSamw 		(void) close(attrinfo->attr_parentfd);
2978da6c28aaSamw 		attrinfo->attr_parentfd = -1;
2979da6c28aaSamw 		errno = saveerrno;
2980da6c28aaSamw 		return (rc);
2981da6c28aaSamw 	}
2982da6c28aaSamw 
2983da6c28aaSamw 	/*
2984da6c28aaSamw 	 * If the attribute is an extended attribute, or extended system
2985da6c28aaSamw 	 * attribute, of an attribute (i.e., <attr>/<sys_attr>), then
2986da6c28aaSamw 	 * recursively call open_attr_dir() to open the attribute directory
2987da6c28aaSamw 	 * of the parent attribute.
2988da6c28aaSamw 	 */
2989da6c28aaSamw 	if (firsttime && (attrinfo->attr_parent != NULL)) {
2990da6c28aaSamw 		return (open_attr_dir(attrname, attrinfo->attr_parent,
2991da6c28aaSamw 		    attrinfo->attr_parentfd, attrinfo));
2992da6c28aaSamw 	}
2993da6c28aaSamw 
2994da6c28aaSamw 	return (ATTR_OK);
2995da6c28aaSamw }
2996da6c28aaSamw #endif
2997da6c28aaSamw 
29987c478bd9Sstevel@tonic-gate static void
29997c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
30007c478bd9Sstevel@tonic-gate doxtract(char *argv[], int tbl_cnt)
30017c478bd9Sstevel@tonic-gate #else
30027c478bd9Sstevel@tonic-gate doxtract(char *argv[])
30037c478bd9Sstevel@tonic-gate #endif
30047c478bd9Sstevel@tonic-gate {
30057c478bd9Sstevel@tonic-gate 	struct	stat	xtractbuf;	/* stat on file after extracting */
30067c478bd9Sstevel@tonic-gate 	blkcnt_t blocks;
30077c478bd9Sstevel@tonic-gate 	off_t bytes;
30087c478bd9Sstevel@tonic-gate 	int ofile;
30097c478bd9Sstevel@tonic-gate 	int newfile;			/* Does the file already exist  */
30107c478bd9Sstevel@tonic-gate 	int xcnt = 0;			/* count # files extracted */
30117c478bd9Sstevel@tonic-gate 	int fcnt = 0;			/* count # files in argv list */
30127c478bd9Sstevel@tonic-gate 	int dir;
30137c478bd9Sstevel@tonic-gate 	int dirfd = -1;
3014da6c28aaSamw 	int cwd = -1;
3015ced83f9bSceastha 	int rw_sysattr;
3016da6c28aaSamw 	int saveerrno;
30177c478bd9Sstevel@tonic-gate 	uid_t Uid;
30187c478bd9Sstevel@tonic-gate 	char *namep, *dirp, *comp, *linkp; /* for removing absolute paths */
30197c478bd9Sstevel@tonic-gate 	char dirname[PATH_MAX+1];
30207c478bd9Sstevel@tonic-gate 	char templink[PATH_MAX+1];	/* temp link with terminating NULL */
30217c478bd9Sstevel@tonic-gate 	int once = 1;
30227c478bd9Sstevel@tonic-gate 	int error;
30237c478bd9Sstevel@tonic-gate 	int symflag;
30247c478bd9Sstevel@tonic-gate 	int want;
3025da6c28aaSamw 	attr_data_t *attrinfo = NULL;	/* attribute info */
3026fa9e4066Sahrens 	acl_t	*aclp = NULL;	/* acl info */
302745916cd2Sjpk 	char dot[] = ".";		/* dirp for using realpath */
30287c478bd9Sstevel@tonic-gate 	timestruc_t	time_zero;	/* used for call to doDirTimes */
30297c478bd9Sstevel@tonic-gate 	int		dircreate;
30307c478bd9Sstevel@tonic-gate 	int convflag;
30317c478bd9Sstevel@tonic-gate 	time_zero.tv_sec = 0;
30327c478bd9Sstevel@tonic-gate 	time_zero.tv_nsec = 0;
30337c478bd9Sstevel@tonic-gate 
303445916cd2Sjpk 	/* reset Trusted Extensions variables */
303545916cd2Sjpk 	rpath_flag = 0;
303645916cd2Sjpk 	lk_rpath_flag = 0;
303745916cd2Sjpk 	dir_flag = 0;
303845916cd2Sjpk 	mld_flag = 0;
303945916cd2Sjpk 	bslundef(&bs_label);
304045916cd2Sjpk 	bsllow(&admin_low);
304145916cd2Sjpk 	bslhigh(&admin_high);
304245916cd2Sjpk 	orig_namep = 0;
304345916cd2Sjpk 
30447c478bd9Sstevel@tonic-gate 	dumping = 0;	/* for newvol(), et al:  we are not writing */
30457c478bd9Sstevel@tonic-gate 
30467c478bd9Sstevel@tonic-gate 	/*
30477c478bd9Sstevel@tonic-gate 	 * Count the number of files that are to be extracted
30487c478bd9Sstevel@tonic-gate 	 */
30497c478bd9Sstevel@tonic-gate 	Uid = getuid();
30507c478bd9Sstevel@tonic-gate 
30517c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
30527c478bd9Sstevel@tonic-gate 	initarg(argv, Filefile);
30537c478bd9Sstevel@tonic-gate 	while (nextarg() != NULL)
30547c478bd9Sstevel@tonic-gate 		++fcnt;
30557c478bd9Sstevel@tonic-gate 	fcnt += tbl_cnt;
30567c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
30577c478bd9Sstevel@tonic-gate 
30587c478bd9Sstevel@tonic-gate 	for (;;) {
30597c478bd9Sstevel@tonic-gate 		convflag = 0;
30607c478bd9Sstevel@tonic-gate 		symflag = 0;
30617c478bd9Sstevel@tonic-gate 		dir = 0;
3062ced83f9bSceastha 		Hiddendir = 0;
3063ced83f9bSceastha 		rw_sysattr = 0;
30647c478bd9Sstevel@tonic-gate 		ofile = -1;
30657c478bd9Sstevel@tonic-gate 
3066da6c28aaSamw 		if (dirfd != -1) {
3067da6c28aaSamw 			(void) close(dirfd);
3068da6c28aaSamw 			dirfd = -1;
3069da6c28aaSamw 		}
3070da6c28aaSamw 		if (ofile != -1) {
3071da6c28aaSamw 			if (close(ofile) != 0)
3072da6c28aaSamw 				vperror(2, gettext("close error"));
3073da6c28aaSamw 		}
3074da6c28aaSamw 
30757c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
3076da6c28aaSamw 		if (cwd != -1) {
3077da6c28aaSamw 			rest_cwd(&cwd);
3078da6c28aaSamw 		}
3079da6c28aaSamw #endif
3080da6c28aaSamw 
3081da6c28aaSamw 		/* namep is set by wantit to point to the full name */
3082da6c28aaSamw 		if ((want = wantit(argv, &namep, &dirp, &comp,
3083da6c28aaSamw 		    &attrinfo)) == 0) {
3084da6c28aaSamw #if defined(O_XATTR)
3085da6c28aaSamw 			if (xattrp != NULL) {
30867c478bd9Sstevel@tonic-gate 				free(xattrhead);
30877c478bd9Sstevel@tonic-gate 				xattrp = NULL;
30887c478bd9Sstevel@tonic-gate 				xattr_linkp = NULL;
30897c478bd9Sstevel@tonic-gate 				xattrhead = NULL;
30907c478bd9Sstevel@tonic-gate 			}
30917c478bd9Sstevel@tonic-gate #endif
30927c478bd9Sstevel@tonic-gate 			continue;
30937c478bd9Sstevel@tonic-gate 		}
30947c478bd9Sstevel@tonic-gate 		if (want == -1)
30957c478bd9Sstevel@tonic-gate 			break;
30967c478bd9Sstevel@tonic-gate 
309745916cd2Sjpk /* Trusted Extensions */
309845916cd2Sjpk 		/*
309945916cd2Sjpk 		 * During tar extract (x):
310045916cd2Sjpk 		 * If the pathname of the restored file has been
310145916cd2Sjpk 		 * reconstructed from the ancillary file,
310245916cd2Sjpk 		 * use it to process the normal file.
310345916cd2Sjpk 		 */
310445916cd2Sjpk 		if (mld_flag) {		/* Skip over .MLD. directory */
310545916cd2Sjpk 			mld_flag = 0;
310645916cd2Sjpk 			passtape();
310745916cd2Sjpk 			continue;
310845916cd2Sjpk 		}
310945916cd2Sjpk 		orig_namep = namep;	/* save original */
311045916cd2Sjpk 		if (rpath_flag) {
311145916cd2Sjpk 			namep = real_path;	/* use zone path */
311245916cd2Sjpk 			comp = real_path;	/* use zone path */
311345916cd2Sjpk 			dirp = dot;		/* work from the top */
311445916cd2Sjpk 			rpath_flag = 0;		/* reset */
311545916cd2Sjpk 		}
311645916cd2Sjpk 
31177c478bd9Sstevel@tonic-gate 		if (dirfd != -1)
31187c478bd9Sstevel@tonic-gate 			(void) close(dirfd);
31197c478bd9Sstevel@tonic-gate 
31207c478bd9Sstevel@tonic-gate 		(void) strcpy(&dirname[0], namep);
31217c478bd9Sstevel@tonic-gate 		dircreate = checkdir(&dirname[0]);
31227c478bd9Sstevel@tonic-gate 
31237c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
3124da6c28aaSamw 		if (xattrp != NULL) {
3125da6c28aaSamw 			int	rc;
31267c478bd9Sstevel@tonic-gate 
3127da6c28aaSamw 			if (((cwd = save_cwd()) == -1) ||
3128da6c28aaSamw 			    ((rc = open_attr_dir(comp, dirp, cwd,
3129da6c28aaSamw 			    attrinfo)) != ATTR_OK)) {
3130da6c28aaSamw 				if (cwd == -1) {
3131da6c28aaSamw 					vperror(0, gettext(
3132da6c28aaSamw 					    "unable to save current working "
3133da6c28aaSamw 					    "directory while processing "
3134da6c28aaSamw 					    "attribute %s of %s"),
3135da6c28aaSamw 					    dirp, attrinfo->attr_path);
3136da6c28aaSamw 				} else if (rc != ATTR_SKIP) {
3137d2443e76Smarks 					(void) fprintf(vfile,
3138d2443e76Smarks 					    gettext("tar: cannot open "
3139da6c28aaSamw 					    "%sattribute %s of file %s: %s\n"),
3140da6c28aaSamw 					    attrinfo->attr_rw_sysattr ? gettext(
3141da6c28aaSamw 					    "system ") : "",
3142da6c28aaSamw 					    comp, dirp, strerror(errno));
3143da6c28aaSamw 				}
3144d2443e76Smarks 				free(xattrhead);
3145d2443e76Smarks 				xattrp = NULL;
3146d2443e76Smarks 				xattr_linkp = NULL;
3147d2443e76Smarks 				xattrhead = NULL;
3148da6c28aaSamw 
31497c478bd9Sstevel@tonic-gate 				passtape();
31507c478bd9Sstevel@tonic-gate 				continue;
3151da6c28aaSamw 			} else {
3152da6c28aaSamw 				dirfd = attrinfo->attr_parentfd;
3153da6c28aaSamw 				rw_sysattr = attrinfo->attr_rw_sysattr;
31547c478bd9Sstevel@tonic-gate 			}
3155da6c28aaSamw 		} else {
3156da6c28aaSamw 			dirfd = open(dirp, O_RDONLY);
3157da6c28aaSamw 		}
3158da6c28aaSamw #else
3159da6c28aaSamw 		dirfd = open(dirp, O_RDONLY);
3160da6c28aaSamw #endif
3161da6c28aaSamw 		if (dirfd == -1) {
3162da6c28aaSamw 			(void) fprintf(vfile, gettext(
3163da6c28aaSamw 			    "tar: cannot open %s: %s\n"),
3164da6c28aaSamw 			    dirp, strerror(errno));
3165da6c28aaSamw 			passtape();
3166da6c28aaSamw 			continue;
31677c478bd9Sstevel@tonic-gate 		}
31687c478bd9Sstevel@tonic-gate 
31697c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_LINKPATH)
31707c478bd9Sstevel@tonic-gate 			(void) strcpy(templink, Xtarhdr.x_linkpath);
31717c478bd9Sstevel@tonic-gate 		else {
31727c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
31737c478bd9Sstevel@tonic-gate 			if (xattrp && dblock.dbuf.typeflag == '1') {
31747c478bd9Sstevel@tonic-gate 				(void) sprintf(templink, "%.*s", NAMSIZ,
31757c478bd9Sstevel@tonic-gate 				    xattrp->h_names);
31767c478bd9Sstevel@tonic-gate 			} else {
31777c478bd9Sstevel@tonic-gate 				(void) sprintf(templink, "%.*s", NAMSIZ,
31787c478bd9Sstevel@tonic-gate 				    dblock.dbuf.linkname);
31797c478bd9Sstevel@tonic-gate 			}
31807c478bd9Sstevel@tonic-gate #else
31817c478bd9Sstevel@tonic-gate 			(void) sprintf(templink, "%.*s", NAMSIZ,
31827c478bd9Sstevel@tonic-gate 			    dblock.dbuf.linkname);
31837c478bd9Sstevel@tonic-gate #endif
31847c478bd9Sstevel@tonic-gate 		}
31857c478bd9Sstevel@tonic-gate 
31867c478bd9Sstevel@tonic-gate 		if (Fflag) {
31873a1dab68SRich Burridge 			if (checkf(namep, is_directory(namep), Fflag) == 0) {
31887c478bd9Sstevel@tonic-gate 				passtape();
31897c478bd9Sstevel@tonic-gate 				continue;
31907c478bd9Sstevel@tonic-gate 			}
31917c478bd9Sstevel@tonic-gate 		}
31927c478bd9Sstevel@tonic-gate 
31937c478bd9Sstevel@tonic-gate 		if (checkw('x', namep) == 0) {
31947c478bd9Sstevel@tonic-gate 			passtape();
31957c478bd9Sstevel@tonic-gate 			continue;
31967c478bd9Sstevel@tonic-gate 		}
31977c478bd9Sstevel@tonic-gate 		if (once) {
31987c478bd9Sstevel@tonic-gate 			if (strcmp(dblock.dbuf.magic, magic_type) == 0) {
31997c478bd9Sstevel@tonic-gate 				if (geteuid() == (uid_t)0) {
32007c478bd9Sstevel@tonic-gate 					checkflag = 1;
32017c478bd9Sstevel@tonic-gate 					pflag = 1;
32027c478bd9Sstevel@tonic-gate 				} else {
32037c478bd9Sstevel@tonic-gate 					/* get file creation mask */
32047c478bd9Sstevel@tonic-gate 					Oumask = umask(0);
32057c478bd9Sstevel@tonic-gate 					(void) umask(Oumask);
32067c478bd9Sstevel@tonic-gate 				}
32077c478bd9Sstevel@tonic-gate 				once = 0;
32087c478bd9Sstevel@tonic-gate 			} else {
32097c478bd9Sstevel@tonic-gate 				if (geteuid() == (uid_t)0) {
32107c478bd9Sstevel@tonic-gate 					pflag = 1;
32117c478bd9Sstevel@tonic-gate 					checkflag = 2;
32127c478bd9Sstevel@tonic-gate 				}
32137c478bd9Sstevel@tonic-gate 				if (!pflag) {
32147c478bd9Sstevel@tonic-gate 					/* get file creation mask */
32157c478bd9Sstevel@tonic-gate 					Oumask = umask(0);
32167c478bd9Sstevel@tonic-gate 					(void) umask(Oumask);
32177c478bd9Sstevel@tonic-gate 				}
32187c478bd9Sstevel@tonic-gate 				once = 0;
32197c478bd9Sstevel@tonic-gate 			}
32207c478bd9Sstevel@tonic-gate 		}
32217c478bd9Sstevel@tonic-gate 
32227c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
32237c478bd9Sstevel@tonic-gate 		/*
32247c478bd9Sstevel@tonic-gate 		 * Handle extraction of hidden attr dir.
32257c478bd9Sstevel@tonic-gate 		 * Dir is automatically created, we only
32267c478bd9Sstevel@tonic-gate 		 * need to update mode and perm's.
32277c478bd9Sstevel@tonic-gate 		 */
3228da6c28aaSamw 		if ((xattrp != NULL) && Hiddendir == 1) {
3229da6c28aaSamw 			bytes = stbuf.st_size;
3230da6c28aaSamw 			blocks = TBLOCKS(bytes);
3231da6c28aaSamw 			if (vflag) {
3232da6c28aaSamw 				(void) fprintf(vfile,
32338e4a71aeSRich Burridge 				    "x %s%s%s, %" FMT_off_t " %s, ", namep,
3234da6c28aaSamw 				    gettext(" attribute "),
32358e4a71aeSRich Burridge 				    xattrapath, bytes,
32368e4a71aeSRich Burridge 				    gettext("bytes"));
3237da6c28aaSamw 				if (NotTape)
3238da6c28aaSamw 					(void) fprintf(vfile,
3239da6c28aaSamw 					    "%" FMT_blkcnt_t "K\n", K(blocks));
3240da6c28aaSamw 				else
3241da6c28aaSamw 					(void) fprintf(vfile, gettext("%"
3242da6c28aaSamw 					    FMT_blkcnt_t " tape blocks\n"),
3243da6c28aaSamw 					    blocks);
3244da6c28aaSamw 			}
3245da6c28aaSamw 
3246da6c28aaSamw 			/*
3247da6c28aaSamw 			 * Set the permissions and mode of the attribute
3248da6c28aaSamw 			 * unless the attribute is a system attribute (can't
3249da6c28aaSamw 			 * successfully do this) or the hidden attribute
3250da6c28aaSamw 			 * directory (".") of an attribute (when the attribute
3251da6c28aaSamw 			 * is restored, the hidden attribute directory of an
3252da6c28aaSamw 			 * attribute is transient).  Note:  when the permissions
3253da6c28aaSamw 			 * and mode are set for the hidden attribute directory
3254da6c28aaSamw 			 * of a file on a system supporting extended system
3255da6c28aaSamw 			 * attributes, even though it returns successfully, it
3256da6c28aaSamw 			 * will not have any affect since the attribute
3257da6c28aaSamw 			 * directory is transient.
3258da6c28aaSamw 			 */
3259da6c28aaSamw 			if (attrinfo->attr_parent == NULL) {
32607c478bd9Sstevel@tonic-gate 				if (fchownat(dirfd, ".", stbuf.st_uid,
32617c478bd9Sstevel@tonic-gate 				    stbuf.st_gid, 0) != 0) {
32627c478bd9Sstevel@tonic-gate 					vperror(0, gettext(
3263da6c28aaSamw 					    "%s%s%s: failed to set ownership "
3264da6c28aaSamw 					    "of attribute directory"), namep,
3265da6c28aaSamw 					    gettext(" attribute "), xattrapath);
32667c478bd9Sstevel@tonic-gate 				}
32677c478bd9Sstevel@tonic-gate 
32687c478bd9Sstevel@tonic-gate 				if (fchmod(dirfd, stbuf.st_mode) != 0) {
32697c478bd9Sstevel@tonic-gate 					vperror(0, gettext(
3270da6c28aaSamw 					    "%s%s%s: failed to set permissions "
3271da6c28aaSamw 					    "of attribute directory"), namep,
3272da6c28aaSamw 					    gettext(" attribute "), xattrapath);
3273da6c28aaSamw 				}
32747c478bd9Sstevel@tonic-gate 			}
32757c478bd9Sstevel@tonic-gate 			goto filedone;
32767c478bd9Sstevel@tonic-gate 		}
32777c478bd9Sstevel@tonic-gate #endif
32787c478bd9Sstevel@tonic-gate 
32797c478bd9Sstevel@tonic-gate 		if (dircreate && (!is_posix || dblock.dbuf.typeflag == '5')) {
32807c478bd9Sstevel@tonic-gate 			dir = 1;
32817c478bd9Sstevel@tonic-gate 			if (vflag) {
32828e4a71aeSRich Burridge 				(void) fprintf(vfile, "x %s, 0 %s, ",
32838e4a71aeSRich Burridge 				    &dirname[0], gettext("bytes"));
32847c478bd9Sstevel@tonic-gate 				if (NotTape)
32857c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, "0K\n");
32867c478bd9Sstevel@tonic-gate 				else
32877c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext("%"
32887c478bd9Sstevel@tonic-gate 					    FMT_blkcnt_t " tape blocks\n"),
32897c478bd9Sstevel@tonic-gate 					    (blkcnt_t)0);
32907c478bd9Sstevel@tonic-gate 			}
32917c478bd9Sstevel@tonic-gate 			goto filedone;
32927c478bd9Sstevel@tonic-gate 		}
32937c478bd9Sstevel@tonic-gate 
32947c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '6') {	/* FIFO */
32957c478bd9Sstevel@tonic-gate 			if (rmdir(namep) < 0) {
32967c478bd9Sstevel@tonic-gate 				if (errno == ENOTDIR)
32977c478bd9Sstevel@tonic-gate 					(void) unlink(namep);
32987c478bd9Sstevel@tonic-gate 			}
32997c478bd9Sstevel@tonic-gate 			linkp = templink;
33007c478bd9Sstevel@tonic-gate 			if (*linkp !=  NULL) {
33017c478bd9Sstevel@tonic-gate 				if (Aflag && *linkp == '/')
33027c478bd9Sstevel@tonic-gate 					linkp++;
33037c478bd9Sstevel@tonic-gate 				if (link(linkp, namep) < 0) {
33047c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
33057c478bd9Sstevel@tonic-gate 					    "tar: %s: cannot link\n"), namep);
33067c478bd9Sstevel@tonic-gate 					continue;
33077c478bd9Sstevel@tonic-gate 				}
33087c478bd9Sstevel@tonic-gate 				if (vflag)
33097c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext(
3310da6c28aaSamw 					    "x %s linked to %s\n"), namep,
3311da6c28aaSamw 					    linkp);
33127c478bd9Sstevel@tonic-gate 				xcnt++;	 /* increment # files extracted */
33137c478bd9Sstevel@tonic-gate 				continue;
33147c478bd9Sstevel@tonic-gate 			}
33157c478bd9Sstevel@tonic-gate 			if (mknod(namep, (int)(Gen.g_mode|S_IFIFO),
33167c478bd9Sstevel@tonic-gate 			    (int)Gen.g_devmajor) < 0) {
33177c478bd9Sstevel@tonic-gate 				vperror(0, gettext("%s: mknod failed"), namep);
33187c478bd9Sstevel@tonic-gate 				continue;
33197c478bd9Sstevel@tonic-gate 			}
33207c478bd9Sstevel@tonic-gate 			bytes = stbuf.st_size;
33217c478bd9Sstevel@tonic-gate 			blocks = TBLOCKS(bytes);
33227c478bd9Sstevel@tonic-gate 			if (vflag) {
33237c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "x %s, %" FMT_off_t
33248e4a71aeSRich Burridge 				    " %s, ", namep, bytes, gettext("bytes"));
33257c478bd9Sstevel@tonic-gate 				if (NotTape)
33267c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, "%" FMT_blkcnt_t
33277c478bd9Sstevel@tonic-gate 					    "K\n", K(blocks));
33287c478bd9Sstevel@tonic-gate 				else
33297c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext("%"
33307c478bd9Sstevel@tonic-gate 					    FMT_blkcnt_t " tape blocks\n"),
33317c478bd9Sstevel@tonic-gate 					    blocks);
33327c478bd9Sstevel@tonic-gate 			}
33337c478bd9Sstevel@tonic-gate 			goto filedone;
33347c478bd9Sstevel@tonic-gate 		}
33357c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '3' && !Uid) { /* CHAR SPECIAL */
33367c478bd9Sstevel@tonic-gate 			if (rmdir(namep) < 0) {
33377c478bd9Sstevel@tonic-gate 				if (errno == ENOTDIR)
33387c478bd9Sstevel@tonic-gate 					(void) unlink(namep);
33397c478bd9Sstevel@tonic-gate 			}
33407c478bd9Sstevel@tonic-gate 			linkp = templink;
33417c478bd9Sstevel@tonic-gate 			if (*linkp != NULL) {
33427c478bd9Sstevel@tonic-gate 				if (Aflag && *linkp == '/')
33437c478bd9Sstevel@tonic-gate 					linkp++;
33447c478bd9Sstevel@tonic-gate 				if (link(linkp, namep) < 0) {
33457c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
33467c478bd9Sstevel@tonic-gate 					    "tar: %s: cannot link\n"), namep);
33477c478bd9Sstevel@tonic-gate 					continue;
33487c478bd9Sstevel@tonic-gate 				}
33497c478bd9Sstevel@tonic-gate 				if (vflag)
33507c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext(
3351da6c28aaSamw 					    "x %s linked to %s\n"), namep,
3352da6c28aaSamw 					    linkp);
33537c478bd9Sstevel@tonic-gate 				xcnt++;	 /* increment # files extracted */
33547c478bd9Sstevel@tonic-gate 				continue;
33557c478bd9Sstevel@tonic-gate 			}
33567c478bd9Sstevel@tonic-gate 			if (mknod(namep, (int)(Gen.g_mode|S_IFCHR),
33577c478bd9Sstevel@tonic-gate 			    (int)makedev(Gen.g_devmajor, Gen.g_devminor)) < 0) {
33587c478bd9Sstevel@tonic-gate 				vperror(0, gettext(
33597c478bd9Sstevel@tonic-gate 				    "%s: mknod failed"), namep);
33607c478bd9Sstevel@tonic-gate 				continue;
33617c478bd9Sstevel@tonic-gate 			}
33627c478bd9Sstevel@tonic-gate 			bytes = stbuf.st_size;
33637c478bd9Sstevel@tonic-gate 			blocks = TBLOCKS(bytes);
33647c478bd9Sstevel@tonic-gate 			if (vflag) {
33657c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "x %s, %" FMT_off_t
33668e4a71aeSRich Burridge 				    " %s, ", namep, bytes, gettext("bytes"));
33677c478bd9Sstevel@tonic-gate 				if (NotTape)
33687c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, "%" FMT_blkcnt_t
33697c478bd9Sstevel@tonic-gate 					    "K\n", K(blocks));
33707c478bd9Sstevel@tonic-gate 				else
33717c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext("%"
33727c478bd9Sstevel@tonic-gate 					    FMT_blkcnt_t " tape blocks\n"),
33737c478bd9Sstevel@tonic-gate 					    blocks);
33747c478bd9Sstevel@tonic-gate 			}
33757c478bd9Sstevel@tonic-gate 			goto filedone;
33767c478bd9Sstevel@tonic-gate 		} else if (dblock.dbuf.typeflag == '3' && Uid) {
33777c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
33787c478bd9Sstevel@tonic-gate 			    "Can't create special %s\n"), namep);
33797c478bd9Sstevel@tonic-gate 			continue;
33807c478bd9Sstevel@tonic-gate 		}
33817c478bd9Sstevel@tonic-gate 
33827c478bd9Sstevel@tonic-gate 		/* BLOCK SPECIAL */
33837c478bd9Sstevel@tonic-gate 
33847c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '4' && !Uid) {
33857c478bd9Sstevel@tonic-gate 			if (rmdir(namep) < 0) {
33867c478bd9Sstevel@tonic-gate 				if (errno == ENOTDIR)
33877c478bd9Sstevel@tonic-gate 					(void) unlink(namep);
33887c478bd9Sstevel@tonic-gate 			}
33897c478bd9Sstevel@tonic-gate 			linkp = templink;
33907c478bd9Sstevel@tonic-gate 			if (*linkp != NULL) {
33917c478bd9Sstevel@tonic-gate 				if (Aflag && *linkp == '/')
33927c478bd9Sstevel@tonic-gate 					linkp++;
33937c478bd9Sstevel@tonic-gate 				if (link(linkp, namep) < 0) {
33947c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
33957c478bd9Sstevel@tonic-gate 					    "tar: %s: cannot link\n"), namep);
33967c478bd9Sstevel@tonic-gate 					continue;
33977c478bd9Sstevel@tonic-gate 				}
33987c478bd9Sstevel@tonic-gate 				if (vflag)
33997c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext(
3400da6c28aaSamw 					    "x %s linked to %s\n"), namep,
3401da6c28aaSamw 					    linkp);
34027c478bd9Sstevel@tonic-gate 				xcnt++;	 /* increment # files extracted */
34037c478bd9Sstevel@tonic-gate 				continue;
34047c478bd9Sstevel@tonic-gate 			}
34057c478bd9Sstevel@tonic-gate 			if (mknod(namep, (int)(Gen.g_mode|S_IFBLK),
34067c478bd9Sstevel@tonic-gate 			    (int)makedev(Gen.g_devmajor, Gen.g_devminor)) < 0) {
34077c478bd9Sstevel@tonic-gate 				vperror(0, gettext("%s: mknod failed"), namep);
34087c478bd9Sstevel@tonic-gate 				continue;
34097c478bd9Sstevel@tonic-gate 			}
34107c478bd9Sstevel@tonic-gate 			bytes = stbuf.st_size;
34117c478bd9Sstevel@tonic-gate 			blocks = TBLOCKS(bytes);
34127c478bd9Sstevel@tonic-gate 			if (vflag) {
34137c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("x %s, %"
34147c478bd9Sstevel@tonic-gate 				    FMT_off_t " bytes, "), namep, bytes);
34157c478bd9Sstevel@tonic-gate 				if (NotTape)
34167c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, "%" FMT_blkcnt_t
34177c478bd9Sstevel@tonic-gate 					    "K\n", K(blocks));
34187c478bd9Sstevel@tonic-gate 				else
34197c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext("%"
34207c478bd9Sstevel@tonic-gate 					    FMT_blkcnt_t " tape blocks\n"),
34217c478bd9Sstevel@tonic-gate 					    blocks);
34227c478bd9Sstevel@tonic-gate 			}
34237c478bd9Sstevel@tonic-gate 			goto filedone;
34247c478bd9Sstevel@tonic-gate 		} else if (dblock.dbuf.typeflag == '4' && Uid) {
34257c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
34267c478bd9Sstevel@tonic-gate 			    gettext("Can't create special %s\n"), namep);
34277c478bd9Sstevel@tonic-gate 			continue;
34287c478bd9Sstevel@tonic-gate 		}
34297c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '2') {	/* symlink */
343045916cd2Sjpk 			if ((Tflag) && (lk_rpath_flag == 1))
343145916cd2Sjpk 				linkp = lk_real_path;
343245916cd2Sjpk 			else
34337c478bd9Sstevel@tonic-gate 				linkp = templink;
34347c478bd9Sstevel@tonic-gate 			if (Aflag && *linkp == '/')
34357c478bd9Sstevel@tonic-gate 				linkp++;
34367c478bd9Sstevel@tonic-gate 			if (rmdir(namep) < 0) {
34377c478bd9Sstevel@tonic-gate 				if (errno == ENOTDIR)
34387c478bd9Sstevel@tonic-gate 					(void) unlink(namep);
34397c478bd9Sstevel@tonic-gate 			}
34407c478bd9Sstevel@tonic-gate 			if (symlink(linkp, namep) < 0) {
34417c478bd9Sstevel@tonic-gate 				vperror(0, gettext("%s: symbolic link failed"),
34427c478bd9Sstevel@tonic-gate 				    namep);
34437c478bd9Sstevel@tonic-gate 				continue;
34447c478bd9Sstevel@tonic-gate 			}
34457c478bd9Sstevel@tonic-gate 			if (vflag)
34467c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext(
34477c478bd9Sstevel@tonic-gate 				    "x %s symbolic link to %s\n"),
34487c478bd9Sstevel@tonic-gate 				    namep, linkp);
34497c478bd9Sstevel@tonic-gate 
34507c478bd9Sstevel@tonic-gate 			symflag = AT_SYMLINK_NOFOLLOW;
34517c478bd9Sstevel@tonic-gate 			goto filedone;
34527c478bd9Sstevel@tonic-gate 		}
34537c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '1') {
34547c478bd9Sstevel@tonic-gate 			linkp = templink;
34557c478bd9Sstevel@tonic-gate 			if (Aflag && *linkp == '/')
34567c478bd9Sstevel@tonic-gate 				linkp++;
34577c478bd9Sstevel@tonic-gate 			if (unlinkat(dirfd, comp, AT_REMOVEDIR) < 0) {
34587c478bd9Sstevel@tonic-gate 				if (errno == ENOTDIR)
34597c478bd9Sstevel@tonic-gate 					(void) unlinkat(dirfd, comp, 0);
34607c478bd9Sstevel@tonic-gate 			}
34617c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
34627c478bd9Sstevel@tonic-gate 			if (xattrp && xattr_linkp) {
34637c478bd9Sstevel@tonic-gate 				if (fchdir(dirfd) < 0) {
34647c478bd9Sstevel@tonic-gate 					vperror(0, gettext(
34657c478bd9Sstevel@tonic-gate 					    "Cannot fchdir to attribute "
3466da6c28aaSamw 					    "directory %s"),
3467da6c28aaSamw 					    (attrinfo->attr_parent == NULL) ?
3468da6c28aaSamw 					    dirp : attrinfo->attr_parent);
34697c478bd9Sstevel@tonic-gate 					exit(1);
34707c478bd9Sstevel@tonic-gate 				}
34717c478bd9Sstevel@tonic-gate 
3472da6c28aaSamw 				error = link(xattr_linkaname, xattrapath);
34737c478bd9Sstevel@tonic-gate 			} else {
34747c478bd9Sstevel@tonic-gate 				error = link(linkp, namep);
34757c478bd9Sstevel@tonic-gate 			}
34767c478bd9Sstevel@tonic-gate #else
34777c478bd9Sstevel@tonic-gate 			error = link(linkp, namep);
34787c478bd9Sstevel@tonic-gate #endif
34797c478bd9Sstevel@tonic-gate 
34807c478bd9Sstevel@tonic-gate 			if (error < 0) {
34817c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
34827c478bd9Sstevel@tonic-gate 				    "tar: %s%s%s: cannot link\n"),
34837c478bd9Sstevel@tonic-gate 				    namep, (xattr_linkp != NULL) ?
34847c478bd9Sstevel@tonic-gate 				    gettext(" attribute ") : "",
34857c478bd9Sstevel@tonic-gate 				    (xattr_linkp != NULL) ?
3486da6c28aaSamw 				    xattrapath : "");
34877c478bd9Sstevel@tonic-gate 				continue;
34887c478bd9Sstevel@tonic-gate 			}
34897c478bd9Sstevel@tonic-gate 			if (vflag)
34907c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext(
3491da6c28aaSamw 				    "x %s%s%s linked to %s%s%s\n"), namep,
34927c478bd9Sstevel@tonic-gate 				    (xattr_linkp != NULL) ?
34937c478bd9Sstevel@tonic-gate 				    gettext(" attribute ") : "",
34947c478bd9Sstevel@tonic-gate 				    (xattr_linkp != NULL) ?
34957c478bd9Sstevel@tonic-gate 				    xattr_linkaname : "",
3496da6c28aaSamw 				    linkp,
34977c478bd9Sstevel@tonic-gate 				    (xattr_linkp != NULL) ?
3498da6c28aaSamw 				    gettext(" attribute ") : "",
3499da6c28aaSamw 				    (xattr_linkp != NULL) ? xattrapath : "");
35007c478bd9Sstevel@tonic-gate 			xcnt++;		/* increment # files extracted */
35017c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
3502da6c28aaSamw 			if (xattrp != NULL) {
35037c478bd9Sstevel@tonic-gate 				free(xattrhead);
35047c478bd9Sstevel@tonic-gate 				xattrp = NULL;
35057c478bd9Sstevel@tonic-gate 				xattr_linkp = NULL;
35067c478bd9Sstevel@tonic-gate 				xattrhead = NULL;
35077c478bd9Sstevel@tonic-gate 			}
35087c478bd9Sstevel@tonic-gate #endif
35097c478bd9Sstevel@tonic-gate 			continue;
35107c478bd9Sstevel@tonic-gate 		}
35117c478bd9Sstevel@tonic-gate 
35127c478bd9Sstevel@tonic-gate 		/* REGULAR FILES */
35137c478bd9Sstevel@tonic-gate 
35147c478bd9Sstevel@tonic-gate 		if (convtoreg(stbuf.st_size)) {
35157c478bd9Sstevel@tonic-gate 			convflag = 1;
35167c478bd9Sstevel@tonic-gate 			if (errflag) {
35177c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
35187c478bd9Sstevel@tonic-gate 				    "tar: %s: typeflag '%c' not recognized\n"),
35197c478bd9Sstevel@tonic-gate 				    namep, dblock.dbuf.typeflag);
35207c478bd9Sstevel@tonic-gate 				done(1);
35217c478bd9Sstevel@tonic-gate 			} else {
35227c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
35237c478bd9Sstevel@tonic-gate 				    "tar: %s: typeflag '%c' not recognized, "
35247c478bd9Sstevel@tonic-gate 				    "converting to regular file\n"), namep,
35257c478bd9Sstevel@tonic-gate 				    dblock.dbuf.typeflag);
35267c478bd9Sstevel@tonic-gate 				Errflg = 1;
35277c478bd9Sstevel@tonic-gate 			}
35287c478bd9Sstevel@tonic-gate 		}
35297c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '0' ||
35307c478bd9Sstevel@tonic-gate 		    dblock.dbuf.typeflag == NULL || convflag) {
35312c0f0499Slovely 			delete_target(dirfd, comp, namep);
35327c478bd9Sstevel@tonic-gate 			linkp = templink;
35337c478bd9Sstevel@tonic-gate 			if (*linkp != NULL) {
35347c478bd9Sstevel@tonic-gate 				if (Aflag && *linkp == '/')
35357c478bd9Sstevel@tonic-gate 					linkp++;
35367c478bd9Sstevel@tonic-gate 				if (link(linkp, comp) < 0) {
35377c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
35387c478bd9Sstevel@tonic-gate 					    "tar: %s: cannot link\n"), namep);
35397c478bd9Sstevel@tonic-gate 					continue;
35407c478bd9Sstevel@tonic-gate 				}
35417c478bd9Sstevel@tonic-gate 				if (vflag)
35427c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext(
3543da6c28aaSamw 					    "x %s linked to %s\n"), comp,
3544da6c28aaSamw 					    linkp);
35457c478bd9Sstevel@tonic-gate 				xcnt++;	 /* increment # files extracted */
3546da6c28aaSamw #if defined(O_XATTR)
3547da6c28aaSamw 				if (xattrp != NULL) {
3548da6c28aaSamw 					free(xattrhead);
3549da6c28aaSamw 					xattrp = NULL;
3550da6c28aaSamw 					xattr_linkp = NULL;
3551da6c28aaSamw 					xattrhead = NULL;
3552da6c28aaSamw 				}
3553da6c28aaSamw #endif
35547c478bd9Sstevel@tonic-gate 				continue;
35557c478bd9Sstevel@tonic-gate 			}
35567c478bd9Sstevel@tonic-gate 		newfile = ((fstatat(dirfd, comp,
35577c478bd9Sstevel@tonic-gate 		    &xtractbuf, 0) == -1) ? TRUE : FALSE);
3558da6c28aaSamw 		ofile = openat(dirfd, comp, O_RDWR|O_CREAT|O_TRUNC,
3559da6c28aaSamw 		    stbuf.st_mode & MODEMASK);
3560da6c28aaSamw 		saveerrno = errno;
3561da6c28aaSamw 
3562da6c28aaSamw #if defined(O_XATTR)
3563da6c28aaSamw 		if (xattrp != NULL) {
3564da6c28aaSamw 			if (ofile < 0) {
3565da6c28aaSamw 				ofile = retry_open_attr(dirfd, cwd,
3566da6c28aaSamw 				    dirp, attrinfo->attr_parent, comp,
3567da6c28aaSamw 				    O_RDWR|O_CREAT|O_TRUNC,
3568da6c28aaSamw 				    stbuf.st_mode & MODEMASK);
3569da6c28aaSamw 			}
3570da6c28aaSamw 		}
3571da6c28aaSamw #endif
3572da6c28aaSamw 		if (ofile < 0) {
3573da6c28aaSamw 			errno = saveerrno;
35747c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
3575da6c28aaSamw 			    "tar: %s%s%s%s - cannot create\n"),
3576da6c28aaSamw 			    (xattrp == NULL) ? "" : (rw_sysattr ?
35778e4a71aeSRich Burridge 			    gettext("system attribute ") :
3578da6c28aaSamw 			    gettext("attribute ")),
3579da6c28aaSamw 			    (xattrp == NULL) ? "" : xattrapath,
3580da6c28aaSamw 			    (xattrp == NULL) ? "" : gettext(" of "),
3581da6c28aaSamw 			    (xattrp == NULL) ? comp : namep);
35827c478bd9Sstevel@tonic-gate 			if (errflag)
35837c478bd9Sstevel@tonic-gate 				done(1);
35847c478bd9Sstevel@tonic-gate 			else
35857c478bd9Sstevel@tonic-gate 				Errflg = 1;
3586da6c28aaSamw #if defined(O_XATTR)
3587da6c28aaSamw 			if (xattrp != NULL) {
3588da6c28aaSamw 				dblock.dbuf.typeflag = _XATTR_HDRTYPE;
3589da6c28aaSamw 				free(xattrhead);
3590da6c28aaSamw 				xattrp = NULL;
3591da6c28aaSamw 				xattr_linkp = NULL;
3592da6c28aaSamw 				xattrhead = NULL;
3593da6c28aaSamw 			}
3594da6c28aaSamw #endif
35957c478bd9Sstevel@tonic-gate 			passtape();
35967c478bd9Sstevel@tonic-gate 			continue;
35977c478bd9Sstevel@tonic-gate 		}
35987c478bd9Sstevel@tonic-gate 
359945916cd2Sjpk 		if (Tflag && (check_ext_attr(namep) == 0)) {
360045916cd2Sjpk 			if (errflag)
360145916cd2Sjpk 				done(1);
360245916cd2Sjpk 			else
360345916cd2Sjpk 				Errflg = 1;
360445916cd2Sjpk 			passtape();
360545916cd2Sjpk 			continue;
360645916cd2Sjpk 		}
360745916cd2Sjpk 
36087c478bd9Sstevel@tonic-gate 		if (extno != 0) {	/* file is in pieces */
36097c478bd9Sstevel@tonic-gate 			if (extotal < 1 || extotal > MAXEXT)
36107c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
3611da6c28aaSamw 				    "tar: ignoring bad extent info for "
3612da6c28aaSamw 				    "%s%s%s%s\n"),
3613da6c28aaSamw 				    (xattrp == NULL) ? "" : (rw_sysattr ?
3614da6c28aaSamw 				    gettext("system attribute ") :
3615da6c28aaSamw 				    gettext("attribute ")),
3616da6c28aaSamw 				    (xattrp == NULL) ? "" : xattrapath,
3617da6c28aaSamw 				    (xattrp == NULL) ? "" : gettext(" of "),
3618da6c28aaSamw 				    (xattrp == NULL) ? comp : namep);
36197c478bd9Sstevel@tonic-gate 			else {
3620da6c28aaSamw 				/* extract it */
3621da6c28aaSamw 				(void) xsfile(rw_sysattr, ofile);
36227c478bd9Sstevel@tonic-gate 			}
36237c478bd9Sstevel@tonic-gate 		}
36247c478bd9Sstevel@tonic-gate 		extno = 0;	/* let everyone know file is not split */
36257c478bd9Sstevel@tonic-gate 		bytes = stbuf.st_size;
36267c478bd9Sstevel@tonic-gate 		blocks = TBLOCKS(bytes);
36277c478bd9Sstevel@tonic-gate 		if (vflag) {
36287c478bd9Sstevel@tonic-gate 			(void) fprintf(vfile,
36298e4a71aeSRich Burridge 			    "x %s%s%s, %" FMT_off_t " %s, ",
36307c478bd9Sstevel@tonic-gate 			    (xattrp == NULL) ? "" : dirp,
3631da6c28aaSamw 			    (xattrp == NULL) ? "" : (rw_sysattr ?
3632da6c28aaSamw 			    gettext(" system attribute ") :
3633da6c28aaSamw 			    gettext(" attribute ")),
36348e4a71aeSRich Burridge 			    (xattrp == NULL) ? namep : xattrapath, bytes,
36358e4a71aeSRich Burridge 			    gettext("bytes"));
36367c478bd9Sstevel@tonic-gate 			if (NotTape)
36377c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, "%" FMT_blkcnt_t "K\n",
36387c478bd9Sstevel@tonic-gate 				    K(blocks));
36397c478bd9Sstevel@tonic-gate 			else
36407c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext("%"
36417c478bd9Sstevel@tonic-gate 				    FMT_blkcnt_t " tape blocks\n"), blocks);
36427c478bd9Sstevel@tonic-gate 		}
36437c478bd9Sstevel@tonic-gate 
3644da6c28aaSamw 		if (xblocks(rw_sysattr, bytes, ofile) != 0) {
3645da6c28aaSamw #if defined(O_XATTR)
3646da6c28aaSamw 			if (xattrp != NULL) {
3647da6c28aaSamw 				free(xattrhead);
3648da6c28aaSamw 				xattrp = NULL;
3649da6c28aaSamw 				xattr_linkp = NULL;
3650da6c28aaSamw 				xattrhead = NULL;
3651da6c28aaSamw 			}
3652da6c28aaSamw #endif
3653da6c28aaSamw 			continue;
3654da6c28aaSamw 		}
36557c478bd9Sstevel@tonic-gate filedone:
36567c478bd9Sstevel@tonic-gate 		if (mflag == 0 && !symflag) {
36577c478bd9Sstevel@tonic-gate 			if (dir)
36587c478bd9Sstevel@tonic-gate 				doDirTimes(namep, stbuf.st_mtim);
3659da6c28aaSamw 
36607c478bd9Sstevel@tonic-gate 			else
3661da6c28aaSamw #if defined(O_XATTR)
3662da6c28aaSamw 				if (xattrp != NULL) {
3663da6c28aaSamw 					/*
3664da6c28aaSamw 					 * Set the time on the attribute unless
3665da6c28aaSamw 					 * the attribute is a system attribute
3666da6c28aaSamw 					 * (can't successfully do this) or the
3667da6c28aaSamw 					 * hidden attribute directory, "." (the
3668da6c28aaSamw 					 * time on the hidden attribute
3669da6c28aaSamw 					 * directory will be updated when
3670da6c28aaSamw 					 * attributes are restored, otherwise
3671da6c28aaSamw 					 * it's transient).
3672da6c28aaSamw 					 */
3673da6c28aaSamw 					if (!rw_sysattr && (Hiddendir == 0)) {
3674da6c28aaSamw 						setPathTimes(dirfd, comp,
3675da6c28aaSamw 						    stbuf.st_mtim);
3676da6c28aaSamw 					}
3677da6c28aaSamw 				} else
3678da6c28aaSamw 					setPathTimes(dirfd, comp,
3679da6c28aaSamw 					    stbuf.st_mtim);
3680da6c28aaSamw #else
36817c478bd9Sstevel@tonic-gate 				setPathTimes(dirfd, comp, stbuf.st_mtim);
3682da6c28aaSamw #endif
36837c478bd9Sstevel@tonic-gate 		}
36847c478bd9Sstevel@tonic-gate 
36857c478bd9Sstevel@tonic-gate 		/* moved this code from above */
36867c478bd9Sstevel@tonic-gate 		if (pflag && !symflag && Hiddendir == 0) {
3687da6c28aaSamw 			if (xattrp != NULL)
36887c478bd9Sstevel@tonic-gate 				(void) fchmod(ofile, stbuf.st_mode & MODEMASK);
36897c478bd9Sstevel@tonic-gate 			else
36907c478bd9Sstevel@tonic-gate 				(void) chmod(namep, stbuf.st_mode & MODEMASK);
36917c478bd9Sstevel@tonic-gate 		}
36927c478bd9Sstevel@tonic-gate 
36937c478bd9Sstevel@tonic-gate 
36947c478bd9Sstevel@tonic-gate 		/*
36957c478bd9Sstevel@tonic-gate 		 * Because ancillary file preceeds the normal file,
36967c478bd9Sstevel@tonic-gate 		 * acl info may have been retrieved (in aclp).
36977c478bd9Sstevel@tonic-gate 		 * All file types are directed here (go filedone).
36987c478bd9Sstevel@tonic-gate 		 * Always restore ACLs if there are ACLs.
36997c478bd9Sstevel@tonic-gate 		 */
37007c478bd9Sstevel@tonic-gate 		if (aclp != NULL) {
37017c478bd9Sstevel@tonic-gate 			int ret;
37027c478bd9Sstevel@tonic-gate 
37037c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
3704da6c28aaSamw 			if (xattrp != NULL) {
37057c478bd9Sstevel@tonic-gate 				if (Hiddendir)
3706fa9e4066Sahrens 					ret = facl_set(dirfd, aclp);
37077c478bd9Sstevel@tonic-gate 				else
3708fa9e4066Sahrens 					ret = facl_set(ofile, aclp);
37097c478bd9Sstevel@tonic-gate 			} else {
3710fa9e4066Sahrens 				ret = acl_set(namep, aclp);
37117c478bd9Sstevel@tonic-gate 			}
37127c478bd9Sstevel@tonic-gate #else
371345916cd2Sjpk 			ret = acl_set(namep, aclp);
37147c478bd9Sstevel@tonic-gate #endif
37157c478bd9Sstevel@tonic-gate 			if (ret < 0) {
37167c478bd9Sstevel@tonic-gate 				if (pflag) {
37177c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
3718da6c28aaSamw 					    "%s%s%s%s: failed to set acl "
3719da6c28aaSamw 					    "entries\n"), namep,
3720da6c28aaSamw 					    (xattrp == NULL) ? "" :
3721da6c28aaSamw 					    (rw_sysattr ? gettext(
3722da6c28aaSamw 					    " system attribute ") :
3723da6c28aaSamw 					    gettext(" attribute ")),
3724da6c28aaSamw 					    (xattrp == NULL) ? "" :
3725da6c28aaSamw 					    xattrapath);
37267c478bd9Sstevel@tonic-gate 				}
37277c478bd9Sstevel@tonic-gate 				/* else: silent and continue */
37287c478bd9Sstevel@tonic-gate 			}
3729fa9e4066Sahrens 			acl_free(aclp);
37307c478bd9Sstevel@tonic-gate 			aclp = NULL;
37317c478bd9Sstevel@tonic-gate 		}
37327c478bd9Sstevel@tonic-gate 
37337c478bd9Sstevel@tonic-gate 		if (!oflag)
37347c478bd9Sstevel@tonic-gate 		    resugname(dirfd, comp, symflag); /* set file ownership */
37357c478bd9Sstevel@tonic-gate 
37367c478bd9Sstevel@tonic-gate 		if (pflag && newfile == TRUE && !dir &&
37377c478bd9Sstevel@tonic-gate 		    (dblock.dbuf.typeflag == '0' ||
37387c478bd9Sstevel@tonic-gate 		    dblock.dbuf.typeflag == NULL ||
37397c478bd9Sstevel@tonic-gate 		    convflag || dblock.dbuf.typeflag == '1')) {
37407c478bd9Sstevel@tonic-gate 			if (fstat(ofile, &xtractbuf) == -1)
37417c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
3742da6c28aaSamw 				    "tar: cannot stat extracted file "
3743da6c28aaSamw 				    "%s%s%s%s\n"),
3744da6c28aaSamw 				    (xattrp == NULL) ? "" : (rw_sysattr ?
3745da6c28aaSamw 				    gettext("system attribute ") :
3746da6c28aaSamw 				    gettext("attribute ")),
3747da6c28aaSamw 				    (xattrp == NULL) ? "" : xattrapath,
3748da6c28aaSamw 				    (xattrp == NULL) ? "" :
3749da6c28aaSamw 				    gettext(" of "), namep);
3750da6c28aaSamw 
37517c478bd9Sstevel@tonic-gate 			else if ((xtractbuf.st_mode & (MODEMASK & ~S_IFMT))
37527c478bd9Sstevel@tonic-gate 			    != (stbuf.st_mode & (MODEMASK & ~S_IFMT))) {
37537c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
37547c478bd9Sstevel@tonic-gate 				    "tar: warning - file permissions have "
3755da6c28aaSamw 				    "changed for %s%s%s%s (are 0%o, should be "
37567c478bd9Sstevel@tonic-gate 				    "0%o)\n"),
3757da6c28aaSamw 				    (xattrp == NULL) ? "" : (rw_sysattr ?
3758da6c28aaSamw 				    gettext("system attribute ") :
3759da6c28aaSamw 				    gettext("attribute ")),
3760da6c28aaSamw 				    (xattrp == NULL) ? "" : xattrapath,
3761da6c28aaSamw 				    (xattrp == NULL) ? "" :
3762da6c28aaSamw 				    gettext(" of "), namep,
3763da6c28aaSamw 				    xtractbuf.st_mode, stbuf.st_mode);
3764da6c28aaSamw 
37657c478bd9Sstevel@tonic-gate 			}
37667c478bd9Sstevel@tonic-gate 		}
3767ced83f9bSceastha #if defined(O_XATTR)
3768ced83f9bSceastha 		if (xattrp != NULL) {
3769ced83f9bSceastha 			free(xattrhead);
3770ced83f9bSceastha 			xattrp = NULL;
3771ced83f9bSceastha 			xattr_linkp = NULL;
3772ced83f9bSceastha 			xattrhead = NULL;
3773ced83f9bSceastha 		}
3774ced83f9bSceastha #endif
3775ced83f9bSceastha 
37767c478bd9Sstevel@tonic-gate 		if (ofile != -1) {
37777c478bd9Sstevel@tonic-gate 			(void) close(dirfd);
37787c478bd9Sstevel@tonic-gate 			dirfd = -1;
37797c478bd9Sstevel@tonic-gate 			if (close(ofile) != 0)
37807c478bd9Sstevel@tonic-gate 				vperror(2, gettext("close error"));
3781da6c28aaSamw 			ofile = -1;
37827c478bd9Sstevel@tonic-gate 		}
37837c478bd9Sstevel@tonic-gate 		xcnt++;			/* increment # files extracted */
37847c478bd9Sstevel@tonic-gate 		}
378545916cd2Sjpk 
378645916cd2Sjpk 		/*
378745916cd2Sjpk 		 * Process ancillary file.
378845916cd2Sjpk 		 *
378945916cd2Sjpk 		 */
379045916cd2Sjpk 
37917c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == 'A') {	/* acl info */
37927c478bd9Sstevel@tonic-gate 			char	buf[TBLOCK];
37937c478bd9Sstevel@tonic-gate 			char	*secp;
37947c478bd9Sstevel@tonic-gate 			char	*tp;
37957c478bd9Sstevel@tonic-gate 			int	attrsize;
37967c478bd9Sstevel@tonic-gate 			int	cnt;
37977c478bd9Sstevel@tonic-gate 
379845916cd2Sjpk 			/* reset Trusted Extensions flags */
379945916cd2Sjpk 			dir_flag = 0;
380045916cd2Sjpk 			mld_flag = 0;
380145916cd2Sjpk 			lk_rpath_flag = 0;
380245916cd2Sjpk 			rpath_flag = 0;
38037c478bd9Sstevel@tonic-gate 
38047c478bd9Sstevel@tonic-gate 			if (pflag) {
38057c478bd9Sstevel@tonic-gate 				bytes = stbuf.st_size;
38067c478bd9Sstevel@tonic-gate 				if ((secp = malloc((int)bytes)) == NULL) {
38077c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
38087c478bd9Sstevel@tonic-gate 					    "Insufficient memory for acl\n"));
38097c478bd9Sstevel@tonic-gate 					passtape();
38107c478bd9Sstevel@tonic-gate 					continue;
38117c478bd9Sstevel@tonic-gate 				}
38127c478bd9Sstevel@tonic-gate 				tp = secp;
38137c478bd9Sstevel@tonic-gate 				blocks = TBLOCKS(bytes);
381445916cd2Sjpk 
381545916cd2Sjpk 				/*
381645916cd2Sjpk 				 * Display a line for each ancillary file.
381745916cd2Sjpk 				 */
381845916cd2Sjpk 				if (vflag && Tflag)
381945916cd2Sjpk 					(void) fprintf(vfile, "x %s(A), %"
38208e4a71aeSRich Burridge 					    FMT_blkcnt_t " %s, %"
38218e4a71aeSRich Burridge 					    FMT_blkcnt_t " %s\n",
38228e4a71aeSRich Burridge 					    namep, bytes, gettext("bytes"),
38238e4a71aeSRich Burridge 					    blocks, gettext("tape blocks"));
382445916cd2Sjpk 
38257c478bd9Sstevel@tonic-gate 				while (blocks-- > 0) {
38267c478bd9Sstevel@tonic-gate 					readtape(buf);
38277c478bd9Sstevel@tonic-gate 					if (bytes <= TBLOCK) {
38287c478bd9Sstevel@tonic-gate 						(void) memcpy(tp, buf,
38297c478bd9Sstevel@tonic-gate 						    (size_t)bytes);
38307c478bd9Sstevel@tonic-gate 						break;
38317c478bd9Sstevel@tonic-gate 					} else {
38327c478bd9Sstevel@tonic-gate 						(void) memcpy(tp, buf,
38337c478bd9Sstevel@tonic-gate 						    TBLOCK);
38347c478bd9Sstevel@tonic-gate 						tp += TBLOCK;
38357c478bd9Sstevel@tonic-gate 					}
38367c478bd9Sstevel@tonic-gate 					bytes -= TBLOCK;
38377c478bd9Sstevel@tonic-gate 				}
3838fa9e4066Sahrens 				bytes = stbuf.st_size;
38397c478bd9Sstevel@tonic-gate 				/* got all attributes in secp */
38407c478bd9Sstevel@tonic-gate 				tp = secp;
38417c478bd9Sstevel@tonic-gate 				do {
38427c478bd9Sstevel@tonic-gate 					attr = (struct sec_attr *)tp;
38437c478bd9Sstevel@tonic-gate 					switch (attr->attr_type) {
38447c478bd9Sstevel@tonic-gate 					case UFSD_ACL:
3845fa9e4066Sahrens 					case ACE_ACL:
38467c478bd9Sstevel@tonic-gate 						(void) sscanf(attr->attr_len,
3847fa9e4066Sahrens 						    "%7o",
3848fa9e4066Sahrens 						    (uint_t *)
3849fa9e4066Sahrens 						    &cnt);
38507c478bd9Sstevel@tonic-gate 						/* header is 8 */
38517c478bd9Sstevel@tonic-gate 						attrsize = 8 + (int)strlen(
38527c478bd9Sstevel@tonic-gate 						    &attr->attr_info[0]) + 1;
3853fa9e4066Sahrens 						error =
3854fa9e4066Sahrens 						    acl_fromtext(
3855fa9e4066Sahrens 						    &attr->attr_info[0], &aclp);
3856fa9e4066Sahrens 
3857fa9e4066Sahrens 						if (error != 0) {
38587c478bd9Sstevel@tonic-gate 							(void) fprintf(stderr,
38597c478bd9Sstevel@tonic-gate 							    gettext(
38607c478bd9Sstevel@tonic-gate 							    "aclfromtext "
3861fa9e4066Sahrens 							    "failed: %s\n"),
3862fa9e4066Sahrens 							    acl_strerror(
3863fa9e4066Sahrens 							    error));
3864fa9e4066Sahrens 							bytes -= attrsize;
38657c478bd9Sstevel@tonic-gate 							break;
38667c478bd9Sstevel@tonic-gate 						}
3867fa9e4066Sahrens 						if (acl_cnt(aclp) != cnt) {
38687c478bd9Sstevel@tonic-gate 							(void) fprintf(stderr,
38697c478bd9Sstevel@tonic-gate 							    gettext(
38707c478bd9Sstevel@tonic-gate 							    "aclcnt error\n"));
3871fa9e4066Sahrens 							bytes -= attrsize;
38727c478bd9Sstevel@tonic-gate 							break;
38737c478bd9Sstevel@tonic-gate 						}
38747c478bd9Sstevel@tonic-gate 						bytes -= attrsize;
38757c478bd9Sstevel@tonic-gate 						break;
38767c478bd9Sstevel@tonic-gate 
387745916cd2Sjpk 					/* Trusted Extensions */
387845916cd2Sjpk 
387945916cd2Sjpk 					case DIR_TYPE:
388045916cd2Sjpk 					case LBL_TYPE:
388145916cd2Sjpk 					case APRIV_TYPE:
388245916cd2Sjpk 					case FPRIV_TYPE:
388345916cd2Sjpk 					case COMP_TYPE:
388445916cd2Sjpk 					case LK_COMP_TYPE:
388545916cd2Sjpk 					case ATTR_FLAG_TYPE:
388645916cd2Sjpk 						attrsize =
388745916cd2Sjpk 						    sizeof (struct sec_attr) +
388845916cd2Sjpk 						    strlen(&attr->attr_info[0]);
388945916cd2Sjpk 						bytes -= attrsize;
389045916cd2Sjpk 						if (Tflag)
389145916cd2Sjpk 							extract_attr(&namep,
389245916cd2Sjpk 							    attr);
389345916cd2Sjpk 						break;
38947c478bd9Sstevel@tonic-gate 
38957c478bd9Sstevel@tonic-gate 					default:
38967c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(
38977c478bd9Sstevel@tonic-gate 						    "unrecognized attr"
38987c478bd9Sstevel@tonic-gate 						    " type\n"));
38997c478bd9Sstevel@tonic-gate 						bytes = (off_t)0;
39007c478bd9Sstevel@tonic-gate 						break;
39017c478bd9Sstevel@tonic-gate 					}
39027c478bd9Sstevel@tonic-gate 
39037c478bd9Sstevel@tonic-gate 					/* next attributes */
39047c478bd9Sstevel@tonic-gate 					tp += attrsize;
39057c478bd9Sstevel@tonic-gate 				} while (bytes != 0);
39067c478bd9Sstevel@tonic-gate 				free(secp);
3907da6c28aaSamw 			} else {
39087c478bd9Sstevel@tonic-gate 				passtape();
3909da6c28aaSamw 			}
39107c478bd9Sstevel@tonic-gate 		} /* acl */
39117c478bd9Sstevel@tonic-gate 
39127c478bd9Sstevel@tonic-gate 	} /* for */
39137c478bd9Sstevel@tonic-gate 
39147c478bd9Sstevel@tonic-gate 	/*
39157c478bd9Sstevel@tonic-gate 	 *  Ensure that all the directories still on the directory stack
39167c478bd9Sstevel@tonic-gate 	 *  get their modification times set correctly by flushing the
39177c478bd9Sstevel@tonic-gate 	 *  stack.
39187c478bd9Sstevel@tonic-gate 	 */
39197c478bd9Sstevel@tonic-gate 
39207c478bd9Sstevel@tonic-gate 	doDirTimes(NULL, time_zero);
39217c478bd9Sstevel@tonic-gate 
3922da6c28aaSamw #if defined(O_XATTR)
3923da6c28aaSamw 		if (xattrp != NULL) {
3924da6c28aaSamw 			free(xattrhead);
3925da6c28aaSamw 			xattrp = NULL;
3926da6c28aaSamw 			xattr_linkp = NULL;
3927da6c28aaSamw 			xattrhead = NULL;
3928da6c28aaSamw 		}
3929da6c28aaSamw #endif
3930da6c28aaSamw 
39317c478bd9Sstevel@tonic-gate 	/*
39327c478bd9Sstevel@tonic-gate 	 * Check if the number of files extracted is different from the
39337c478bd9Sstevel@tonic-gate 	 * number of files listed on the command line
39347c478bd9Sstevel@tonic-gate 	 */
39357c478bd9Sstevel@tonic-gate 	if (fcnt > xcnt) {
39367c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
39377c478bd9Sstevel@tonic-gate 		    gettext("tar: %d file(s) not extracted\n"),
39387c478bd9Sstevel@tonic-gate 		fcnt-xcnt);
39397c478bd9Sstevel@tonic-gate 		Errflg = 1;
39407c478bd9Sstevel@tonic-gate 	}
39417c478bd9Sstevel@tonic-gate }
39427c478bd9Sstevel@tonic-gate 
39437c478bd9Sstevel@tonic-gate /*
39447c478bd9Sstevel@tonic-gate  *	xblocks		extract file/extent from tape to output file
39457c478bd9Sstevel@tonic-gate  *
3946ced83f9bSceastha  *	xblocks(issysattr, bytes, ofile);
3947ced83f9bSceastha  *
3948ced83f9bSceastha  *	issysattr			flag set if the files being extracted
3949ced83f9bSceastha  *					is an extended system attribute file.
3950ced83f9bSceastha  *	unsigned long long bytes	size of extent or file to be extracted
3951ced83f9bSceastha  *	ofile				output file
39527c478bd9Sstevel@tonic-gate  *
39537c478bd9Sstevel@tonic-gate  *	called by doxtract() and xsfile()
39547c478bd9Sstevel@tonic-gate  */
39557c478bd9Sstevel@tonic-gate 
3956da6c28aaSamw static int
3957da6c28aaSamw xblocks(int issysattr, off_t bytes, int ofile)
39587c478bd9Sstevel@tonic-gate {
3959ced83f9bSceastha 	char *buf;
39607c478bd9Sstevel@tonic-gate 	char tempname[NAMSIZ+1];
3961ced83f9bSceastha 	size_t maxwrite;
3962ced83f9bSceastha 	size_t bytesread;
3963ced83f9bSceastha 	size_t piosize;		/* preferred I/O size */
3964ced83f9bSceastha 	struct stat tsbuf;
39657c478bd9Sstevel@tonic-gate 
3966ced83f9bSceastha 	/* Don't need to do anything if this is a zero size file */
3967ced83f9bSceastha 	if (bytes <= 0) {
3968ced83f9bSceastha 		return (0);
3969ced83f9bSceastha 	}
3970ced83f9bSceastha 
3971ced83f9bSceastha 	/*
3972ced83f9bSceastha 	 * To figure out the size of the buffer used to accumulate data
3973ced83f9bSceastha 	 * from readtape() and to write to the file, we need to determine
3974ced83f9bSceastha 	 * the largest chunk of data to be written to the file at one time.
3975ced83f9bSceastha 	 * This is determined based on the smallest of the following two
3976ced83f9bSceastha 	 * things:
3977ced83f9bSceastha 	 *	1) The size of the archived file.
3978ced83f9bSceastha 	 *	2) The preferred I/O size of the file.
3979ced83f9bSceastha 	 */
3980ced83f9bSceastha 	if (issysattr || (bytes <= TBLOCK)) {
3981ced83f9bSceastha 		/*
3982ced83f9bSceastha 		 * Writes to system attribute files must be
3983ced83f9bSceastha 		 * performed in one operation.
3984ced83f9bSceastha 		 */
3985ced83f9bSceastha 		maxwrite = bytes;
3986ced83f9bSceastha 	} else {
3987ced83f9bSceastha 		/*
3988ced83f9bSceastha 		 * fstat() the file to get the preferred I/O size.
3989ced83f9bSceastha 		 * If it fails, then resort back to just writing
3990ced83f9bSceastha 		 * one block at a time.
3991ced83f9bSceastha 		 */
3992ced83f9bSceastha 		if (fstat(ofile, &tsbuf) == 0) {
3993ced83f9bSceastha 			piosize = tsbuf.st_blksize;
3994ced83f9bSceastha 		} else {
3995ced83f9bSceastha 			piosize = TBLOCK;
3996ced83f9bSceastha 		}
3997ced83f9bSceastha 		maxwrite = min(bytes, piosize);
3998ced83f9bSceastha 	}
3999ced83f9bSceastha 
4000ced83f9bSceastha 	/*
4001ced83f9bSceastha 	 * The buffer used to accumulate the data for the write operation
4002ced83f9bSceastha 	 * needs to be the maximum number of bytes to be written rounded up
4003ced83f9bSceastha 	 * to the nearest TBLOCK since readtape reads one block at a time.
4004ced83f9bSceastha 	 */
4005ced83f9bSceastha 	if ((buf = malloc(TBLOCKS(maxwrite) * TBLOCK)) == NULL) {
4006ced83f9bSceastha 		fatal(gettext("cannot allocate buffer"));
4007ced83f9bSceastha 	}
4008ced83f9bSceastha 
4009ced83f9bSceastha 	while (bytes > 0) {
4010ced83f9bSceastha 
4011ced83f9bSceastha 		/*
4012ced83f9bSceastha 		 * readtape() obtains one block (TBLOCK) of data at a time.
4013ced83f9bSceastha 		 * Accumulate as many blocks of data in buf as we can write
4014ced83f9bSceastha 		 * in one operation.
4015ced83f9bSceastha 		 */
4016ced83f9bSceastha 		for (bytesread = 0; bytesread < maxwrite; bytesread += TBLOCK) {
4017ced83f9bSceastha 			readtape(buf + bytesread);
4018ced83f9bSceastha 		}
4019ced83f9bSceastha 
4020ced83f9bSceastha 		if (write(ofile, buf, maxwrite) < 0) {
4021da6c28aaSamw 			int saveerrno = errno;
4022da6c28aaSamw 
40237c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_PATH)
4024ced83f9bSceastha 				(void) strlcpy(tempname, Xtarhdr.x_path,
4025ced83f9bSceastha 				    sizeof (tempname));
40267c478bd9Sstevel@tonic-gate 			else
40277c478bd9Sstevel@tonic-gate 				(void) sprintf(tempname, "%.*s", NAMSIZ,
40287c478bd9Sstevel@tonic-gate 				    dblock.dbuf.name);
4029da6c28aaSamw 			/*
4030da6c28aaSamw 			 * If the extended system attribute being extracted
4031da6c28aaSamw 			 * contains attributes that the user needs privileges
4032da6c28aaSamw 			 * for, then just display a warning message, skip
4033da6c28aaSamw 			 * the extraction of this file, and return.
4034da6c28aaSamw 			 */
4035da6c28aaSamw 			if ((saveerrno == EPERM) && issysattr) {
40367c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
4037da6c28aaSamw 				    "tar: unable to extract system attribute "
4038da6c28aaSamw 				    "%s: insufficient privileges\n"), tempname);
4039da6c28aaSamw 				Errflg = 1;
4040ced83f9bSceastha 				(void) free(buf);
4041da6c28aaSamw 				return (1);
4042da6c28aaSamw 			} else {
4043da6c28aaSamw 				(void) fprintf(stderr, gettext(
4044da6c28aaSamw 				    "tar: %s: HELP - extract write error\n"),
4045da6c28aaSamw 				    tempname);
40467c478bd9Sstevel@tonic-gate 				done(2);
40477c478bd9Sstevel@tonic-gate 			}
4048da6c28aaSamw 		}
4049ced83f9bSceastha 		bytes -= maxwrite;
4050ced83f9bSceastha 
4051ced83f9bSceastha 		/*
4052ced83f9bSceastha 		 * If we've reached this point and there is still data
4053ced83f9bSceastha 		 * to be written, maxwrite had to have been determined
4054ced83f9bSceastha 		 * by the preferred I/O size.  If the number of bytes
4055ced83f9bSceastha 		 * left to write is smaller than the preferred I/O size,
4056ced83f9bSceastha 		 * then we're about to do our final write to the file, so
4057ced83f9bSceastha 		 * just set maxwrite to the number of bytes left to write.
4058ced83f9bSceastha 		 */
4059ced83f9bSceastha 		if ((bytes > 0) && (bytes < maxwrite)) {
4060ced83f9bSceastha 			maxwrite = bytes;
40617c478bd9Sstevel@tonic-gate 		}
4062ced83f9bSceastha 	}
4063ced83f9bSceastha 	free(buf);
4064da6c28aaSamw 
4065da6c28aaSamw 	return (0);
40667c478bd9Sstevel@tonic-gate }
40677c478bd9Sstevel@tonic-gate 
40687c478bd9Sstevel@tonic-gate /*
40697c478bd9Sstevel@tonic-gate  * 	xsfile	extract split file
40707c478bd9Sstevel@tonic-gate  *
40717c478bd9Sstevel@tonic-gate  *	xsfile(ofd);	ofd = output file descriptor
40727c478bd9Sstevel@tonic-gate  *
40737c478bd9Sstevel@tonic-gate  *	file extracted and put in ofd via xblocks()
40747c478bd9Sstevel@tonic-gate  *
40757c478bd9Sstevel@tonic-gate  *	NOTE:  only called by doxtract() to extract one large file
40767c478bd9Sstevel@tonic-gate  */
40777c478bd9Sstevel@tonic-gate 
40787c478bd9Sstevel@tonic-gate static	union	hblock	savedblock;	/* to ensure same file across volumes */
40797c478bd9Sstevel@tonic-gate 
4080da6c28aaSamw static int
4081da6c28aaSamw xsfile(int issysattr, int ofd)
40827c478bd9Sstevel@tonic-gate {
40837c478bd9Sstevel@tonic-gate 	int i, c;
4084da6c28aaSamw 	int sysattrerr = 0;
40857c478bd9Sstevel@tonic-gate 	char name[PATH_MAX+1];	/* holds name for diagnostics */
40867c478bd9Sstevel@tonic-gate 	int extents, totalext;
40877c478bd9Sstevel@tonic-gate 	off_t bytes, totalbytes;
40887c478bd9Sstevel@tonic-gate 
40897c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_PATH)
40907c478bd9Sstevel@tonic-gate 		(void) strcpy(name, Xtarhdr.x_path);
40917c478bd9Sstevel@tonic-gate 	else
40927c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "%.*s", NAMSIZ, dblock.dbuf.name);
40937c478bd9Sstevel@tonic-gate 
40947c478bd9Sstevel@tonic-gate 	totalbytes = (off_t)0;		/* in case we read in half the file */
40957c478bd9Sstevel@tonic-gate 	totalext = 0;		/* these keep count */
40967c478bd9Sstevel@tonic-gate 
40977c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
40987c478bd9Sstevel@tonic-gate 	    "tar: %s split across %d volumes\n"), name, extotal);
40997c478bd9Sstevel@tonic-gate 
41007c478bd9Sstevel@tonic-gate 	/* make sure we do extractions in order */
41017c478bd9Sstevel@tonic-gate 	if (extno != 1) {	/* starting in middle of file? */
41027c478bd9Sstevel@tonic-gate 		(void) printf(gettext(
41037c478bd9Sstevel@tonic-gate 		    "tar: first extent read is not #1\n"
41043d63ea05Sas145665 		    "OK to read file beginning with extent #%d (%s/%s) ? "),
41053d63ea05Sas145665 		    extno, yesstr, nostr);
41063d63ea05Sas145665 		if (yes() == 0) {
41077c478bd9Sstevel@tonic-gate canit:
41087c478bd9Sstevel@tonic-gate 			passtape();
41097c478bd9Sstevel@tonic-gate 			if (close(ofd) != 0)
41107c478bd9Sstevel@tonic-gate 				vperror(2, gettext("close error"));
4111da6c28aaSamw 			if (sysattrerr) {
4112da6c28aaSamw 				return (1);
4113da6c28aaSamw 			} else {
4114da6c28aaSamw 				return (0);
4115da6c28aaSamw 			}
41167c478bd9Sstevel@tonic-gate 		}
41177c478bd9Sstevel@tonic-gate 	}
41187c478bd9Sstevel@tonic-gate 	extents = extotal;
41197c478bd9Sstevel@tonic-gate 	i = extno;
41207c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
41217c478bd9Sstevel@tonic-gate 	while (1) {
41227c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_SIZE) {
41237c478bd9Sstevel@tonic-gate 			bytes = extsize;
41247c478bd9Sstevel@tonic-gate 		} else {
41257c478bd9Sstevel@tonic-gate 			bytes = stbuf.st_size;
41267c478bd9Sstevel@tonic-gate 		}
41277c478bd9Sstevel@tonic-gate 
41287c478bd9Sstevel@tonic-gate 		if (vflag)
41298e4a71aeSRich Burridge 			(void) fprintf(vfile, "+++ x %s [%s #%d], %"
41308e4a71aeSRich Burridge 			    FMT_off_t " %s, %ldK\n",
41318e4a71aeSRich Burridge 			    name, gettext("extent"), extno,
41328e4a71aeSRich Burridge 			    bytes, gettext("bytes"),
41337c478bd9Sstevel@tonic-gate 			    (long)K(TBLOCKS(bytes)));
4134da6c28aaSamw 		if (xblocks(issysattr, bytes, ofd) != 0) {
4135da6c28aaSamw 			sysattrerr = 1;
4136da6c28aaSamw 			goto canit;
4137da6c28aaSamw 		}
41387c478bd9Sstevel@tonic-gate 
41397c478bd9Sstevel@tonic-gate 		totalbytes += bytes;
41407c478bd9Sstevel@tonic-gate 		totalext++;
41417c478bd9Sstevel@tonic-gate 		if (++i > extents)
41427c478bd9Sstevel@tonic-gate 			break;
41437c478bd9Sstevel@tonic-gate 
41447c478bd9Sstevel@tonic-gate 		/* get next volume and verify it's the right one */
41457c478bd9Sstevel@tonic-gate 		copy(&savedblock, &dblock);
41467c478bd9Sstevel@tonic-gate tryagain:
41477c478bd9Sstevel@tonic-gate 		newvol();
41487c478bd9Sstevel@tonic-gate 		xhdr_flgs = 0;
41497c478bd9Sstevel@tonic-gate 		getdir();
41507c478bd9Sstevel@tonic-gate 		if (Xhdrflag > 0)
41517c478bd9Sstevel@tonic-gate 			(void) get_xdata();	/* Get x-header & regular hdr */
4152123523f8Sas158974 		if ((dblock.dbuf.typeflag != 'A') && (xhdr_flgs != 0)) {
4153123523f8Sas158974 			load_info_from_xtarhdr(xhdr_flgs, &Xtarhdr);
4154123523f8Sas158974 			xhdr_flgs |= _X_XHDR;
4155123523f8Sas158974 		}
41567c478bd9Sstevel@tonic-gate 		if (endtape()) {	/* seemingly empty volume */
41577c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
41587c478bd9Sstevel@tonic-gate 			    "tar: first record is null\n"));
41597c478bd9Sstevel@tonic-gate asknicely:
41607c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
41617c478bd9Sstevel@tonic-gate 			    "tar: need volume with extent #%d of %s\n"),
41627c478bd9Sstevel@tonic-gate 			    i, name);
41637c478bd9Sstevel@tonic-gate 			goto tryagain;
41647c478bd9Sstevel@tonic-gate 		}
41657c478bd9Sstevel@tonic-gate 		if (notsame()) {
41667c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
41677c478bd9Sstevel@tonic-gate 			    "tar: first file on that volume is not "
41687c478bd9Sstevel@tonic-gate 			    "the same file\n"));
41697c478bd9Sstevel@tonic-gate 			goto asknicely;
41707c478bd9Sstevel@tonic-gate 		}
41717c478bd9Sstevel@tonic-gate 		if (i != extno) {
41727c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4173eace40a5Sceastha 			    "tar: extent #%d received out of order\ntar: "
4174eace40a5Sceastha 			    "should be #%d\n"), extno, i);
41757c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
41767c478bd9Sstevel@tonic-gate 			    "Ignore error, Abort this file, or "
41777c478bd9Sstevel@tonic-gate 			    "load New volume (i/a/n) ? "));
41787c478bd9Sstevel@tonic-gate 			c = response();
41797c478bd9Sstevel@tonic-gate 			if (c == 'a')
41807c478bd9Sstevel@tonic-gate 				goto canit;
41817c478bd9Sstevel@tonic-gate 			if (c != 'i')		/* default to new volume */
41827c478bd9Sstevel@tonic-gate 				goto asknicely;
41837c478bd9Sstevel@tonic-gate 			i = extno;		/* okay, start from there */
41847c478bd9Sstevel@tonic-gate 		}
41857c478bd9Sstevel@tonic-gate 	}
41867c478bd9Sstevel@tonic-gate 	if (vflag)
41877c478bd9Sstevel@tonic-gate 		(void) fprintf(vfile, gettext(
41887c478bd9Sstevel@tonic-gate 		    "x %s (in %d extents), %" FMT_off_t " bytes, %ldK\n"),
41897c478bd9Sstevel@tonic-gate 		    name, totalext, totalbytes, (long)K(TBLOCKS(totalbytes)));
4190da6c28aaSamw 
4191da6c28aaSamw 	return (0);
41927c478bd9Sstevel@tonic-gate }
41937c478bd9Sstevel@tonic-gate 
41947c478bd9Sstevel@tonic-gate 
41957c478bd9Sstevel@tonic-gate /*
41967c478bd9Sstevel@tonic-gate  *	notsame()	check if extract file extent is invalid
41977c478bd9Sstevel@tonic-gate  *
41987c478bd9Sstevel@tonic-gate  *	returns true if anything differs between savedblock and dblock
41997c478bd9Sstevel@tonic-gate  *	except extno (extent number), checksum, or size (extent size).
42007c478bd9Sstevel@tonic-gate  *	Determines if this header belongs to the same file as the one we're
42017c478bd9Sstevel@tonic-gate  *	extracting.
42027c478bd9Sstevel@tonic-gate  *
42037c478bd9Sstevel@tonic-gate  *	NOTE:	though rather bulky, it is only called once per file
42047c478bd9Sstevel@tonic-gate  *		extension, and it can withstand changes in the definition
42057c478bd9Sstevel@tonic-gate  *		of the header structure.
42067c478bd9Sstevel@tonic-gate  *
42077c478bd9Sstevel@tonic-gate  *	WARNING:	this routine is local to xsfile() above
42087c478bd9Sstevel@tonic-gate  */
42097c478bd9Sstevel@tonic-gate 
42107c478bd9Sstevel@tonic-gate static int
42117c478bd9Sstevel@tonic-gate notsame(void)
42127c478bd9Sstevel@tonic-gate {
42137c478bd9Sstevel@tonic-gate 	return (
42147c478bd9Sstevel@tonic-gate 	    (strncmp(savedblock.dbuf.name, dblock.dbuf.name, NAMSIZ)) ||
42157c478bd9Sstevel@tonic-gate 	    (strcmp(savedblock.dbuf.mode, dblock.dbuf.mode)) ||
42167c478bd9Sstevel@tonic-gate 	    (strcmp(savedblock.dbuf.uid, dblock.dbuf.uid)) ||
42177c478bd9Sstevel@tonic-gate 	    (strcmp(savedblock.dbuf.gid, dblock.dbuf.gid)) ||
42187c478bd9Sstevel@tonic-gate 	    (strcmp(savedblock.dbuf.mtime, dblock.dbuf.mtime)) ||
42197c478bd9Sstevel@tonic-gate 	    (savedblock.dbuf.typeflag != dblock.dbuf.typeflag) ||
42207c478bd9Sstevel@tonic-gate 	    (strncmp(savedblock.dbuf.linkname, dblock.dbuf.linkname, NAMSIZ)) ||
42217c478bd9Sstevel@tonic-gate 	    (savedblock.dbuf.extotal != dblock.dbuf.extotal) ||
42227c478bd9Sstevel@tonic-gate 	    (strcmp(savedblock.dbuf.efsize, dblock.dbuf.efsize)));
42237c478bd9Sstevel@tonic-gate }
42247c478bd9Sstevel@tonic-gate 
42257c478bd9Sstevel@tonic-gate static void
42267c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
42277c478bd9Sstevel@tonic-gate dotable(char *argv[], int tbl_cnt)
42287c478bd9Sstevel@tonic-gate #else
42297c478bd9Sstevel@tonic-gate dotable(char *argv[])
42307c478bd9Sstevel@tonic-gate #endif
42317c478bd9Sstevel@tonic-gate 
42327c478bd9Sstevel@tonic-gate {
42337c478bd9Sstevel@tonic-gate 	int tcnt;			/* count # files tabled */
42347c478bd9Sstevel@tonic-gate 	int fcnt;			/* count # files in argv list */
42357c478bd9Sstevel@tonic-gate 	char *namep, *dirp, *comp;
42367c478bd9Sstevel@tonic-gate 	int want;
42377c478bd9Sstevel@tonic-gate 	char aclchar = ' ';			/* either blank or '+' */
42387c478bd9Sstevel@tonic-gate 	char templink[PATH_MAX+1];
4239da6c28aaSamw 	attr_data_t *attrinfo = NULL;
42407c478bd9Sstevel@tonic-gate 
42417c478bd9Sstevel@tonic-gate 	dumping = 0;
42427c478bd9Sstevel@tonic-gate 
42437c478bd9Sstevel@tonic-gate 	/* if not on magtape, maximize seek speed */
42447c478bd9Sstevel@tonic-gate 	if (NotTape && !bflag) {
42457c478bd9Sstevel@tonic-gate #if SYS_BLOCK > TBLOCK
42467c478bd9Sstevel@tonic-gate 		nblock = SYS_BLOCK / TBLOCK;
42477c478bd9Sstevel@tonic-gate #else
42487c478bd9Sstevel@tonic-gate 		nblock = 1;
42497c478bd9Sstevel@tonic-gate #endif
42507c478bd9Sstevel@tonic-gate 	}
42517c478bd9Sstevel@tonic-gate 	/*
42527c478bd9Sstevel@tonic-gate 	 * Count the number of files that are to be tabled
42537c478bd9Sstevel@tonic-gate 	 */
42547c478bd9Sstevel@tonic-gate 	fcnt = tcnt = 0;
42557c478bd9Sstevel@tonic-gate 
42567c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
42577c478bd9Sstevel@tonic-gate 	initarg(argv, Filefile);
42587c478bd9Sstevel@tonic-gate 	while (nextarg() != NULL)
42597c478bd9Sstevel@tonic-gate 		++fcnt;
42607c478bd9Sstevel@tonic-gate 	fcnt += tbl_cnt;
42617c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
42627c478bd9Sstevel@tonic-gate 
42637c478bd9Sstevel@tonic-gate 	for (;;) {
42647c478bd9Sstevel@tonic-gate 
42657c478bd9Sstevel@tonic-gate 		/* namep is set by wantit to point to the full name */
4266da6c28aaSamw 		if ((want = wantit(argv, &namep, &dirp, &comp, &attrinfo)) == 0)
42677c478bd9Sstevel@tonic-gate 			continue;
42687c478bd9Sstevel@tonic-gate 		if (want == -1)
42697c478bd9Sstevel@tonic-gate 			break;
42707c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag != 'A')
42717c478bd9Sstevel@tonic-gate 			++tcnt;
42727c478bd9Sstevel@tonic-gate 
42733a1dab68SRich Burridge 		if (Fflag) {
42743a1dab68SRich Burridge 			if (checkf(namep, is_directory(namep), Fflag) == 0) {
42753a1dab68SRich Burridge 				passtape();
42763a1dab68SRich Burridge 				continue;
42773a1dab68SRich Burridge 			}
42783a1dab68SRich Burridge 		}
42797c478bd9Sstevel@tonic-gate 		/*
42807c478bd9Sstevel@tonic-gate 		 * ACL support:
42817c478bd9Sstevel@tonic-gate 		 * aclchar is introduced to indicate if there are
42827c478bd9Sstevel@tonic-gate 		 * acl entries. longt() now takes one extra argument.
42837c478bd9Sstevel@tonic-gate 		 */
42847c478bd9Sstevel@tonic-gate 		if (vflag) {
42857c478bd9Sstevel@tonic-gate 			if (dblock.dbuf.typeflag == 'A') {
42867c478bd9Sstevel@tonic-gate 				aclchar = '+';
42877c478bd9Sstevel@tonic-gate 				passtape();
42887c478bd9Sstevel@tonic-gate 				continue;
42897c478bd9Sstevel@tonic-gate 			}
42907c478bd9Sstevel@tonic-gate 			longt(&stbuf, aclchar);
42917c478bd9Sstevel@tonic-gate 			aclchar = ' ';
42927c478bd9Sstevel@tonic-gate 		}
42937c478bd9Sstevel@tonic-gate 
42947c478bd9Sstevel@tonic-gate 
42957c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
4296da6c28aaSamw 		if (xattrp != NULL) {
4297da6c28aaSamw 			int	issysattr;
4298da6c28aaSamw 			char	*bn = basename(attrinfo->attr_path);
42997c478bd9Sstevel@tonic-gate 
4300da6c28aaSamw 			/*
4301da6c28aaSamw 			 * We could use sysattr_type() to test whether or not
4302da6c28aaSamw 			 * the attribute we are processing is really an
4303da6c28aaSamw 			 * extended system attribute, which as of this writing
4304da6c28aaSamw 			 * just does a strcmp(), however, sysattr_type() may
4305da6c28aaSamw 			 * be changed to issue a pathconf() call instead, which
4306da6c28aaSamw 			 * would require being changed into the parent attribute
4307da6c28aaSamw 			 * directory.  So instead, just do simple string
4308da6c28aaSamw 			 * comparisons to see if we are processing an extended
4309da6c28aaSamw 			 * system attribute.
4310da6c28aaSamw 			 */
4311da6c28aaSamw 			issysattr = is_sysattr(bn);
4312da6c28aaSamw 
4313da6c28aaSamw 			(void) printf(gettext("%s %sattribute %s"),
4314da6c28aaSamw 			    xattrp->h_names,
4315da6c28aaSamw 			    issysattr ? gettext("system ") : "",
4316da6c28aaSamw 			    attrinfo->attr_path);
43177c478bd9Sstevel@tonic-gate 		} else {
43187c478bd9Sstevel@tonic-gate 			(void) printf("%s", namep);
43197c478bd9Sstevel@tonic-gate 		}
43207c478bd9Sstevel@tonic-gate #else
43217c478bd9Sstevel@tonic-gate 			(void) printf("%s", namep);
43227c478bd9Sstevel@tonic-gate #endif
43237c478bd9Sstevel@tonic-gate 
43247c478bd9Sstevel@tonic-gate 		if (extno != 0) {
43257c478bd9Sstevel@tonic-gate 			if (vflag) {
43267c478bd9Sstevel@tonic-gate 				/* keep the '\n' for backwards compatibility */
43277c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext(
43287c478bd9Sstevel@tonic-gate 				    "\n [extent #%d of %d]"), extno, extotal);
43297c478bd9Sstevel@tonic-gate 			} else {
43307c478bd9Sstevel@tonic-gate 				(void) fprintf(vfile, gettext(
43317c478bd9Sstevel@tonic-gate 				    " [extent #%d of %d]"), extno, extotal);
43327c478bd9Sstevel@tonic-gate 			}
43337c478bd9Sstevel@tonic-gate 		}
43347c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_LINKPATH) {
43357c478bd9Sstevel@tonic-gate 			(void) strcpy(templink, Xtarhdr.x_linkpath);
43367c478bd9Sstevel@tonic-gate 		} else {
43377c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
4338da6c28aaSamw 			if (xattrp != NULL) {
43397c478bd9Sstevel@tonic-gate 				(void) sprintf(templink,
43407c478bd9Sstevel@tonic-gate 				    "file %.*s", NAMSIZ, xattrp->h_names);
43417c478bd9Sstevel@tonic-gate 			} else {
43427c478bd9Sstevel@tonic-gate 				(void) sprintf(templink, "%.*s", NAMSIZ,
43437c478bd9Sstevel@tonic-gate 				    dblock.dbuf.linkname);
43447c478bd9Sstevel@tonic-gate 			}
43457c478bd9Sstevel@tonic-gate #else
43467c478bd9Sstevel@tonic-gate 			(void) sprintf(templink, "%.*s", NAMSIZ,
43477c478bd9Sstevel@tonic-gate 			    dblock.dbuf.linkname);
43487c478bd9Sstevel@tonic-gate #endif
43497c478bd9Sstevel@tonic-gate 			templink[NAMSIZ] = '\0';
43507c478bd9Sstevel@tonic-gate 		}
43517c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '1') {
43527c478bd9Sstevel@tonic-gate 			/*
43537c478bd9Sstevel@tonic-gate 			 * TRANSLATION_NOTE
43547c478bd9Sstevel@tonic-gate 			 *	Subject is omitted here.
43557c478bd9Sstevel@tonic-gate 			 *	Translate this as if
43567c478bd9Sstevel@tonic-gate 			 *		<subject> linked to %s
43577c478bd9Sstevel@tonic-gate 			 */
43587c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
4359da6c28aaSamw 			if (xattrp != NULL) {
43607c478bd9Sstevel@tonic-gate 				(void) printf(
43617c478bd9Sstevel@tonic-gate 				    gettext(" linked to attribute %s"),
43627c478bd9Sstevel@tonic-gate 				    xattr_linkp->h_names +
43637c478bd9Sstevel@tonic-gate 				    strlen(xattr_linkp->h_names) + 1);
43647c478bd9Sstevel@tonic-gate 			} else {
43657c478bd9Sstevel@tonic-gate 				(void) printf(
43667c478bd9Sstevel@tonic-gate 				    gettext(" linked to %s"), templink);
43677c478bd9Sstevel@tonic-gate 			}
43687c478bd9Sstevel@tonic-gate #else
43697c478bd9Sstevel@tonic-gate 				(void) printf(
43707c478bd9Sstevel@tonic-gate 				    gettext(" linked to %s"), templink);
43717c478bd9Sstevel@tonic-gate 
43727c478bd9Sstevel@tonic-gate #endif
43737c478bd9Sstevel@tonic-gate 		}
43747c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.typeflag == '2')
43757c478bd9Sstevel@tonic-gate 			(void) printf(gettext(
43767c478bd9Sstevel@tonic-gate 			/*
43777c478bd9Sstevel@tonic-gate 			 * TRANSLATION_NOTE
43787c478bd9Sstevel@tonic-gate 			 *	Subject is omitted here.
43797c478bd9Sstevel@tonic-gate 			 *	Translate this as if
43807c478bd9Sstevel@tonic-gate 			 *		<subject> symbolic link to %s
43817c478bd9Sstevel@tonic-gate 			 */
43827c478bd9Sstevel@tonic-gate 			" symbolic link to %s"), templink);
43837c478bd9Sstevel@tonic-gate 		(void) printf("\n");
43847c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
4385da6c28aaSamw 		if (xattrp != NULL) {
43867c478bd9Sstevel@tonic-gate 			free(xattrhead);
43877c478bd9Sstevel@tonic-gate 			xattrp = NULL;
43887c478bd9Sstevel@tonic-gate 			xattrhead = NULL;
43897c478bd9Sstevel@tonic-gate 		}
43907c478bd9Sstevel@tonic-gate #endif
43917c478bd9Sstevel@tonic-gate 		passtape();
43927c478bd9Sstevel@tonic-gate 	}
43937c478bd9Sstevel@tonic-gate 	/*
43947c478bd9Sstevel@tonic-gate 	 * Check if the number of files tabled is different from the
43957c478bd9Sstevel@tonic-gate 	 * number of files listed on the command line
43967c478bd9Sstevel@tonic-gate 	 */
43977c478bd9Sstevel@tonic-gate 	if (fcnt > tcnt) {
43987c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
43997c478bd9Sstevel@tonic-gate 		    "tar: %d file(s) not found\n"), fcnt-tcnt);
44007c478bd9Sstevel@tonic-gate 		Errflg = 1;
44017c478bd9Sstevel@tonic-gate 	}
44027c478bd9Sstevel@tonic-gate }
44037c478bd9Sstevel@tonic-gate 
44047c478bd9Sstevel@tonic-gate static void
44057c478bd9Sstevel@tonic-gate putempty(blkcnt_t n)
44067c478bd9Sstevel@tonic-gate {
44077c478bd9Sstevel@tonic-gate 	char buf[TBLOCK];
44087c478bd9Sstevel@tonic-gate 	char *cp;
44097c478bd9Sstevel@tonic-gate 
44107c478bd9Sstevel@tonic-gate 	for (cp = buf; cp < &buf[TBLOCK]; )
44117c478bd9Sstevel@tonic-gate 		*cp++ = '\0';
44127c478bd9Sstevel@tonic-gate 	while (n-- > 0)
44137c478bd9Sstevel@tonic-gate 		(void) writetbuf(buf, 1);
44147c478bd9Sstevel@tonic-gate }
44157c478bd9Sstevel@tonic-gate 
44167c478bd9Sstevel@tonic-gate static	ushort_t	Ftype = S_IFMT;
44177c478bd9Sstevel@tonic-gate 
44187c478bd9Sstevel@tonic-gate static	void
44197c478bd9Sstevel@tonic-gate verbose(struct stat *st, char aclchar)
44207c478bd9Sstevel@tonic-gate {
44217c478bd9Sstevel@tonic-gate 	int i, j, temp;
44227c478bd9Sstevel@tonic-gate 	mode_t mode;
44237c478bd9Sstevel@tonic-gate 	char modestr[12];
44247c478bd9Sstevel@tonic-gate 
44257c478bd9Sstevel@tonic-gate 	for (i = 0; i < 11; i++)
44267c478bd9Sstevel@tonic-gate 		modestr[i] = '-';
44277c478bd9Sstevel@tonic-gate 	modestr[i] = '\0';
44287c478bd9Sstevel@tonic-gate 
44297c478bd9Sstevel@tonic-gate 	/* a '+' sign is printed if there is ACL */
44307c478bd9Sstevel@tonic-gate 	modestr[i-1] = aclchar;
44317c478bd9Sstevel@tonic-gate 
44327c478bd9Sstevel@tonic-gate 	mode = st->st_mode;
44337c478bd9Sstevel@tonic-gate 	for (i = 0; i < 3; i++) {
44347c478bd9Sstevel@tonic-gate 		temp = (mode >> (6 - (i * 3)));
44357c478bd9Sstevel@tonic-gate 		j = (i * 3) + 1;
44367c478bd9Sstevel@tonic-gate 		if (S_IROTH & temp)
44377c478bd9Sstevel@tonic-gate 			modestr[j] = 'r';
44387c478bd9Sstevel@tonic-gate 		if (S_IWOTH & temp)
44397c478bd9Sstevel@tonic-gate 			modestr[j + 1] = 'w';
44407c478bd9Sstevel@tonic-gate 		if (S_IXOTH & temp)
44417c478bd9Sstevel@tonic-gate 			modestr[j + 2] = 'x';
44427c478bd9Sstevel@tonic-gate 	}
44437c478bd9Sstevel@tonic-gate 	temp = st->st_mode & Ftype;
44447c478bd9Sstevel@tonic-gate 	switch (temp) {
44457c478bd9Sstevel@tonic-gate 	case (S_IFIFO):
44467c478bd9Sstevel@tonic-gate 		modestr[0] = 'p';
44477c478bd9Sstevel@tonic-gate 		break;
44487c478bd9Sstevel@tonic-gate 	case (S_IFCHR):
44497c478bd9Sstevel@tonic-gate 		modestr[0] = 'c';
44507c478bd9Sstevel@tonic-gate 		break;
44517c478bd9Sstevel@tonic-gate 	case (S_IFDIR):
44527c478bd9Sstevel@tonic-gate 		modestr[0] = 'd';
44537c478bd9Sstevel@tonic-gate 		break;
44547c478bd9Sstevel@tonic-gate 	case (S_IFBLK):
44557c478bd9Sstevel@tonic-gate 		modestr[0] = 'b';
44567c478bd9Sstevel@tonic-gate 		break;
44577c478bd9Sstevel@tonic-gate 	case (S_IFREG): /* was initialized to '-' */
44587c478bd9Sstevel@tonic-gate 		break;
44597c478bd9Sstevel@tonic-gate 	case (S_IFLNK):
44607c478bd9Sstevel@tonic-gate 		modestr[0] = 'l';
44617c478bd9Sstevel@tonic-gate 		break;
44627c478bd9Sstevel@tonic-gate 	default:
44637c478bd9Sstevel@tonic-gate 		/* This field may be zero in old archives. */
44647c478bd9Sstevel@tonic-gate 		if (is_posix && dblock.dbuf.typeflag != '1') {
44657c478bd9Sstevel@tonic-gate 			/*
44667c478bd9Sstevel@tonic-gate 			 * For POSIX compliant archives, the mode field
44677c478bd9Sstevel@tonic-gate 			 * consists of 12 bits, ie:  the file type bits
44687c478bd9Sstevel@tonic-gate 			 * are not stored in dblock.dbuf.mode.
44697c478bd9Sstevel@tonic-gate 			 * For files other than hard links, getdir() sets
44707c478bd9Sstevel@tonic-gate 			 * the file type bits in the st_mode field of the
44717c478bd9Sstevel@tonic-gate 			 * stat structure based upon dblock.dbuf.typeflag.
44727c478bd9Sstevel@tonic-gate 			 */
44737c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
44747c478bd9Sstevel@tonic-gate 			    "tar: impossible file type"));
44757c478bd9Sstevel@tonic-gate 		}
44767c478bd9Sstevel@tonic-gate 	}
44777c478bd9Sstevel@tonic-gate 
44787c478bd9Sstevel@tonic-gate 	if ((S_ISUID & Gen.g_mode) == S_ISUID)
44797c478bd9Sstevel@tonic-gate 		modestr[3] = 's';
44807c478bd9Sstevel@tonic-gate 	if ((S_ISVTX & Gen.g_mode) == S_ISVTX)
44817c478bd9Sstevel@tonic-gate 		modestr[9] = 't';
44827c478bd9Sstevel@tonic-gate 	if ((S_ISGID & Gen.g_mode) == S_ISGID && modestr[6] == 'x')
44837c478bd9Sstevel@tonic-gate 		modestr[6] = 's';
44847c478bd9Sstevel@tonic-gate 	else if ((S_ENFMT & Gen.g_mode) == S_ENFMT && modestr[6] != 'x')
44857c478bd9Sstevel@tonic-gate 		modestr[6] = 'l';
44867c478bd9Sstevel@tonic-gate 	(void) fprintf(vfile, "%s", modestr);
44877c478bd9Sstevel@tonic-gate }
44887c478bd9Sstevel@tonic-gate 
44897c478bd9Sstevel@tonic-gate static void
44907c478bd9Sstevel@tonic-gate longt(struct stat *st, char aclchar)
44917c478bd9Sstevel@tonic-gate {
44927c478bd9Sstevel@tonic-gate 	char fileDate[30];
44937c478bd9Sstevel@tonic-gate 	struct tm *tm;
44947c478bd9Sstevel@tonic-gate 
44957c478bd9Sstevel@tonic-gate 	verbose(st, aclchar);
44967c478bd9Sstevel@tonic-gate 	(void) fprintf(vfile, "%3ld/%-3ld", st->st_uid, st->st_gid);
44977c478bd9Sstevel@tonic-gate 
44987c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.typeflag == '2') {
44997c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_LINKPATH)
45007c478bd9Sstevel@tonic-gate 			st->st_size = (off_t)strlen(Xtarhdr.x_linkpath);
45017c478bd9Sstevel@tonic-gate 		else
45027c478bd9Sstevel@tonic-gate 			st->st_size = (off_t)(memchr(dblock.dbuf.linkname,
45037c478bd9Sstevel@tonic-gate 			    '\0', NAMSIZ) ?
45047c478bd9Sstevel@tonic-gate 			    (strlen(dblock.dbuf.linkname)) : (NAMSIZ));
45057c478bd9Sstevel@tonic-gate 	}
45067c478bd9Sstevel@tonic-gate 	(void) fprintf(vfile, " %6" FMT_off_t, st->st_size);
45077c478bd9Sstevel@tonic-gate 
45087c478bd9Sstevel@tonic-gate 	tm = localtime(&(st->st_mtime));
45097c478bd9Sstevel@tonic-gate 	(void) strftime(fileDate, sizeof (fileDate),
45107c478bd9Sstevel@tonic-gate 	    dcgettext((const char *)0, "%b %e %R %Y", LC_TIME), tm);
45117c478bd9Sstevel@tonic-gate 	(void) fprintf(vfile, " %s ", fileDate);
45127c478bd9Sstevel@tonic-gate }
45137c478bd9Sstevel@tonic-gate 
45147c478bd9Sstevel@tonic-gate 
45157c478bd9Sstevel@tonic-gate /*
45167c478bd9Sstevel@tonic-gate  *  checkdir - Attempt to ensure that the path represented in name
45177c478bd9Sstevel@tonic-gate  *             exists, and return 1 if this is true and name itself is a
45187c478bd9Sstevel@tonic-gate  *             directory.
45197c478bd9Sstevel@tonic-gate  *             Return 0 if this path cannot be created or if name is not
45207c478bd9Sstevel@tonic-gate  *             a directory.
45217c478bd9Sstevel@tonic-gate  */
45227c478bd9Sstevel@tonic-gate 
45237c478bd9Sstevel@tonic-gate static int
45247c478bd9Sstevel@tonic-gate checkdir(char *name)
45257c478bd9Sstevel@tonic-gate {
45267c478bd9Sstevel@tonic-gate 	char lastChar;		   /* the last character in name */
45277c478bd9Sstevel@tonic-gate 	char *cp;		   /* scratch pointer into name */
45287c478bd9Sstevel@tonic-gate 	char *firstSlash = NULL;   /* first slash in name */
45297c478bd9Sstevel@tonic-gate 	char *lastSlash = NULL;	   /* last slash in name */
45307c478bd9Sstevel@tonic-gate 	int  nameLen;		   /* length of name */
45317c478bd9Sstevel@tonic-gate 	int  trailingSlash;	   /* true if name ends in slash */
45327c478bd9Sstevel@tonic-gate 	int  leadingSlash;	   /* true if name begins with slash */
45337c478bd9Sstevel@tonic-gate 	int  markedDir;		   /* true if name denotes a directory */
45347c478bd9Sstevel@tonic-gate 	int  success;		   /* status of makeDir call */
45357c478bd9Sstevel@tonic-gate 
45367c478bd9Sstevel@tonic-gate 
45377c478bd9Sstevel@tonic-gate 	/*
45387c478bd9Sstevel@tonic-gate 	 *  Scan through the name, and locate first and last slashes.
45397c478bd9Sstevel@tonic-gate 	 */
45407c478bd9Sstevel@tonic-gate 
45417c478bd9Sstevel@tonic-gate 	for (cp = name; *cp; cp++) {
45427c478bd9Sstevel@tonic-gate 		if (*cp == '/') {
45437c478bd9Sstevel@tonic-gate 			if (! firstSlash) {
45447c478bd9Sstevel@tonic-gate 				firstSlash = cp;
45457c478bd9Sstevel@tonic-gate 			}
45467c478bd9Sstevel@tonic-gate 			lastSlash = cp;
45477c478bd9Sstevel@tonic-gate 		}
45487c478bd9Sstevel@tonic-gate 	}
45497c478bd9Sstevel@tonic-gate 
45507c478bd9Sstevel@tonic-gate 	/*
45517c478bd9Sstevel@tonic-gate 	 *  Determine what you can from the proceeds of the scan.
45527c478bd9Sstevel@tonic-gate 	 */
45537c478bd9Sstevel@tonic-gate 
45547c478bd9Sstevel@tonic-gate 	lastChar	= *(cp - 1);
45557c478bd9Sstevel@tonic-gate 	nameLen		= (int)(cp - name);
45567c478bd9Sstevel@tonic-gate 	trailingSlash	= (lastChar == '/');
45577c478bd9Sstevel@tonic-gate 	leadingSlash	= (*name == '/');
45587c478bd9Sstevel@tonic-gate 	markedDir	= (dblock.dbuf.typeflag == '5' || trailingSlash);
45597c478bd9Sstevel@tonic-gate 
45607c478bd9Sstevel@tonic-gate 	if (! lastSlash && ! markedDir) {
45617c478bd9Sstevel@tonic-gate 		/*
45627c478bd9Sstevel@tonic-gate 		 *  The named file does not have any subdrectory
45637c478bd9Sstevel@tonic-gate 		 *  structure; just bail out.
45647c478bd9Sstevel@tonic-gate 		 */
45657c478bd9Sstevel@tonic-gate 
45667c478bd9Sstevel@tonic-gate 		return (0);
45677c478bd9Sstevel@tonic-gate 	}
45687c478bd9Sstevel@tonic-gate 
45697c478bd9Sstevel@tonic-gate 	/*
45707c478bd9Sstevel@tonic-gate 	 *  Make sure that name doesn`t end with slash for the loop.
45717c478bd9Sstevel@tonic-gate 	 *  This ensures that the makeDir attempt after the loop is
45727c478bd9Sstevel@tonic-gate 	 *  meaningful.
45737c478bd9Sstevel@tonic-gate 	 */
45747c478bd9Sstevel@tonic-gate 
45757c478bd9Sstevel@tonic-gate 	if (trailingSlash) {
45767c478bd9Sstevel@tonic-gate 		name[nameLen-1] = '\0';
45777c478bd9Sstevel@tonic-gate 	}
45787c478bd9Sstevel@tonic-gate 
45797c478bd9Sstevel@tonic-gate 	/*
45807c478bd9Sstevel@tonic-gate 	 *  Make the path one component at a time.
45817c478bd9Sstevel@tonic-gate 	 */
45827c478bd9Sstevel@tonic-gate 
45837c478bd9Sstevel@tonic-gate 	for (cp = strchr(leadingSlash ? name+1 : name, '/');
45847c478bd9Sstevel@tonic-gate 	    cp;
45857c478bd9Sstevel@tonic-gate 	    cp = strchr(cp+1, '/')) {
45867c478bd9Sstevel@tonic-gate 		*cp = '\0';
45877c478bd9Sstevel@tonic-gate 		success = makeDir(name);
45887c478bd9Sstevel@tonic-gate 		*cp = '/';
45897c478bd9Sstevel@tonic-gate 
45907c478bd9Sstevel@tonic-gate 		if (!success) {
45917c478bd9Sstevel@tonic-gate 			name[nameLen-1] = lastChar;
45927c478bd9Sstevel@tonic-gate 			return (0);
45937c478bd9Sstevel@tonic-gate 		}
45947c478bd9Sstevel@tonic-gate 	}
45957c478bd9Sstevel@tonic-gate 
45967c478bd9Sstevel@tonic-gate 	/*
45977c478bd9Sstevel@tonic-gate 	 *  This makes the last component of the name, if it is a
45987c478bd9Sstevel@tonic-gate 	 *  directory.
45997c478bd9Sstevel@tonic-gate 	 */
46007c478bd9Sstevel@tonic-gate 
46017c478bd9Sstevel@tonic-gate 	if (markedDir) {
46027c478bd9Sstevel@tonic-gate 		if (! makeDir(name)) {
46037c478bd9Sstevel@tonic-gate 			name[nameLen-1] = lastChar;
46047c478bd9Sstevel@tonic-gate 			return (0);
46057c478bd9Sstevel@tonic-gate 		}
46067c478bd9Sstevel@tonic-gate 	}
46077c478bd9Sstevel@tonic-gate 
46087c478bd9Sstevel@tonic-gate 	name[nameLen-1] = (lastChar == '/') ? '\0' : lastChar;
46097c478bd9Sstevel@tonic-gate 	return (markedDir);
46107c478bd9Sstevel@tonic-gate }
46117c478bd9Sstevel@tonic-gate 
46127c478bd9Sstevel@tonic-gate /*
46137c478bd9Sstevel@tonic-gate  * resugname - Restore the user name and group name.  Search the NIS
46147c478bd9Sstevel@tonic-gate  *             before using the uid and gid.
46157c478bd9Sstevel@tonic-gate  *             (It is presumed that an archive entry cannot be
46167c478bd9Sstevel@tonic-gate  *	       simultaneously a symlink and some other type.)
46177c478bd9Sstevel@tonic-gate  */
46187c478bd9Sstevel@tonic-gate 
46197c478bd9Sstevel@tonic-gate static void
46207c478bd9Sstevel@tonic-gate resugname(int dirfd, 	/* dir fd file resides in */
46217c478bd9Sstevel@tonic-gate 	char *name,	/* name of the file to be modified */
46227c478bd9Sstevel@tonic-gate 	int symflag)	/* true if file is a symbolic link */
46237c478bd9Sstevel@tonic-gate {
46247c478bd9Sstevel@tonic-gate 	uid_t duid;
46257c478bd9Sstevel@tonic-gate 	gid_t dgid;
46267c478bd9Sstevel@tonic-gate 	struct stat *sp = &stbuf;
46277c478bd9Sstevel@tonic-gate 	char	*u_g_name;
46287c478bd9Sstevel@tonic-gate 
46297c478bd9Sstevel@tonic-gate 	if (checkflag == 1) { /* Extended tar format and euid == 0 */
46307c478bd9Sstevel@tonic-gate 
46317c478bd9Sstevel@tonic-gate 		/*
46327c478bd9Sstevel@tonic-gate 		 * Try and extract the intended uid and gid from the name
46337c478bd9Sstevel@tonic-gate 		 * service before believing the uid and gid in the header.
46347c478bd9Sstevel@tonic-gate 		 *
46357c478bd9Sstevel@tonic-gate 		 * In the case where we archived a setuid or setgid file
46367c478bd9Sstevel@tonic-gate 		 * owned by someone with a large uid, then it will
46377c478bd9Sstevel@tonic-gate 		 * have made it into the archive with a uid of nobody.  If
46387c478bd9Sstevel@tonic-gate 		 * the corresponding username doesn't appear to exist, then we
46397c478bd9Sstevel@tonic-gate 		 * want to make sure it *doesn't* end up as setuid nobody!
46407c478bd9Sstevel@tonic-gate 		 *
46417c478bd9Sstevel@tonic-gate 		 * Our caller will print an error message about the fact
46427c478bd9Sstevel@tonic-gate 		 * that the restore didn't work out quite right ..
46437c478bd9Sstevel@tonic-gate 		 */
46447c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_UNAME)
46457c478bd9Sstevel@tonic-gate 			u_g_name = Xtarhdr.x_uname;
46467c478bd9Sstevel@tonic-gate 		else
46477c478bd9Sstevel@tonic-gate 			u_g_name = dblock.dbuf.uname;
46487c478bd9Sstevel@tonic-gate 		if ((duid = getuidbyname(u_g_name)) == -1) {
46497c478bd9Sstevel@tonic-gate 			if (S_ISREG(sp->st_mode) && sp->st_uid == UID_NOBODY &&
46507c478bd9Sstevel@tonic-gate 			    (sp->st_mode & S_ISUID) == S_ISUID)
46517c478bd9Sstevel@tonic-gate 				(void) chmod(name,
46527c478bd9Sstevel@tonic-gate 				    MODEMASK & sp->st_mode & ~S_ISUID);
46537c478bd9Sstevel@tonic-gate 			duid = sp->st_uid;
46547c478bd9Sstevel@tonic-gate 		}
46557c478bd9Sstevel@tonic-gate 
46567c478bd9Sstevel@tonic-gate 		/* (Ditto for gids) */
46577c478bd9Sstevel@tonic-gate 
46587c478bd9Sstevel@tonic-gate 		if (xhdr_flgs & _X_GNAME)
46597c478bd9Sstevel@tonic-gate 			u_g_name = Xtarhdr.x_gname;
46607c478bd9Sstevel@tonic-gate 		else
46617c478bd9Sstevel@tonic-gate 			u_g_name = dblock.dbuf.gname;
46627c478bd9Sstevel@tonic-gate 		if ((dgid = getgidbyname(u_g_name)) == -1) {
46637c478bd9Sstevel@tonic-gate 			if (S_ISREG(sp->st_mode) && sp->st_gid == GID_NOBODY &&
46647c478bd9Sstevel@tonic-gate 			    (sp->st_mode & S_ISGID) == S_ISGID)
46657c478bd9Sstevel@tonic-gate 				(void) chmod(name,
46667c478bd9Sstevel@tonic-gate 				    MODEMASK & sp->st_mode & ~S_ISGID);
46677c478bd9Sstevel@tonic-gate 			dgid = sp->st_gid;
46687c478bd9Sstevel@tonic-gate 		}
46697c478bd9Sstevel@tonic-gate 	} else if (checkflag == 2) { /* tar format and euid == 0 */
46707c478bd9Sstevel@tonic-gate 		duid = sp->st_uid;
46717c478bd9Sstevel@tonic-gate 		dgid = sp->st_gid;
46727c478bd9Sstevel@tonic-gate 	}
46737c478bd9Sstevel@tonic-gate 	if ((checkflag == 1) || (checkflag == 2))
46747c478bd9Sstevel@tonic-gate 		(void) fchownat(dirfd, name, duid, dgid, symflag);
46757c478bd9Sstevel@tonic-gate }
46767c478bd9Sstevel@tonic-gate 
46777c478bd9Sstevel@tonic-gate /*ARGSUSED*/
46787c478bd9Sstevel@tonic-gate static void
46797c478bd9Sstevel@tonic-gate onintr(int sig)
46807c478bd9Sstevel@tonic-gate {
46817c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, SIG_IGN);
46827c478bd9Sstevel@tonic-gate 	term++;
46837c478bd9Sstevel@tonic-gate }
46847c478bd9Sstevel@tonic-gate 
46857c478bd9Sstevel@tonic-gate /*ARGSUSED*/
46867c478bd9Sstevel@tonic-gate static void
46877c478bd9Sstevel@tonic-gate onquit(int sig)
46887c478bd9Sstevel@tonic-gate {
46897c478bd9Sstevel@tonic-gate 	(void) signal(SIGQUIT, SIG_IGN);
46907c478bd9Sstevel@tonic-gate 	term++;
46917c478bd9Sstevel@tonic-gate }
46927c478bd9Sstevel@tonic-gate 
46937c478bd9Sstevel@tonic-gate /*ARGSUSED*/
46947c478bd9Sstevel@tonic-gate static void
46957c478bd9Sstevel@tonic-gate onhup(int sig)
46967c478bd9Sstevel@tonic-gate {
46977c478bd9Sstevel@tonic-gate 	(void) signal(SIGHUP, SIG_IGN);
46987c478bd9Sstevel@tonic-gate 	term++;
46997c478bd9Sstevel@tonic-gate }
47007c478bd9Sstevel@tonic-gate 
47017c478bd9Sstevel@tonic-gate static void
47027c478bd9Sstevel@tonic-gate tomodes(struct stat *sp)
47037c478bd9Sstevel@tonic-gate {
47047c478bd9Sstevel@tonic-gate 	uid_t uid;
47057c478bd9Sstevel@tonic-gate 	gid_t gid;
47067c478bd9Sstevel@tonic-gate 
47077c478bd9Sstevel@tonic-gate 	bzero(dblock.dummy, TBLOCK);
47087c478bd9Sstevel@tonic-gate 
47097c478bd9Sstevel@tonic-gate 	/*
47107c478bd9Sstevel@tonic-gate 	 * If the uid or gid is too large, we can't put it into
47117c478bd9Sstevel@tonic-gate 	 * the archive.  We could fail to put anything in the
47127c478bd9Sstevel@tonic-gate 	 * archive at all .. but most of the time the name service
47137c478bd9Sstevel@tonic-gate 	 * will save the day when we do a lookup at restore time.
47147c478bd9Sstevel@tonic-gate 	 *
47157c478bd9Sstevel@tonic-gate 	 * Instead we choose a "safe" uid and gid, and fix up whether
47167c478bd9Sstevel@tonic-gate 	 * or not the setuid and setgid bits are left set to extraction
47177c478bd9Sstevel@tonic-gate 	 * time.
47187c478bd9Sstevel@tonic-gate 	 */
47197c478bd9Sstevel@tonic-gate 	if (Eflag) {
47207c478bd9Sstevel@tonic-gate 		if ((ulong_t)(uid = sp->st_uid) > (ulong_t)OCTAL7CHAR) {
47217c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_UID;
47227c478bd9Sstevel@tonic-gate 			Xtarhdr.x_uid = uid;
47237c478bd9Sstevel@tonic-gate 		}
47247c478bd9Sstevel@tonic-gate 		if ((ulong_t)(gid = sp->st_gid) > (ulong_t)OCTAL7CHAR) {
47257c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_GID;
47267c478bd9Sstevel@tonic-gate 			Xtarhdr.x_gid = gid;
47277c478bd9Sstevel@tonic-gate 		}
47287c478bd9Sstevel@tonic-gate 		if (sp->st_size > TAR_OFFSET_MAX) {
47297c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_SIZE;
47307c478bd9Sstevel@tonic-gate 			Xtarhdr.x_filesz = sp->st_size;
47317c478bd9Sstevel@tonic-gate 			(void) sprintf(dblock.dbuf.size, "%011" FMT_off_t_o,
47327c478bd9Sstevel@tonic-gate 			    (off_t)0);
47337c478bd9Sstevel@tonic-gate 		} else
47347c478bd9Sstevel@tonic-gate 			(void) sprintf(dblock.dbuf.size, "%011" FMT_off_t_o,
47357c478bd9Sstevel@tonic-gate 			    sp->st_size);
47367c478bd9Sstevel@tonic-gate 	} else {
47377c478bd9Sstevel@tonic-gate 		(void) sprintf(dblock.dbuf.size, "%011" FMT_off_t_o,
47387c478bd9Sstevel@tonic-gate 		    sp->st_size);
47397c478bd9Sstevel@tonic-gate 	}
47407c478bd9Sstevel@tonic-gate 	if ((ulong_t)(uid = sp->st_uid) > (ulong_t)OCTAL7CHAR)
47417c478bd9Sstevel@tonic-gate 		uid = UID_NOBODY;
47427c478bd9Sstevel@tonic-gate 	if ((ulong_t)(gid = sp->st_gid) > (ulong_t)OCTAL7CHAR)
47437c478bd9Sstevel@tonic-gate 		gid = GID_NOBODY;
47447c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.gid, "%07lo", gid);
47457c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.uid, "%07lo", uid);
47467c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.mode, "%07lo", sp->st_mode & POSIXMODES);
47477c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.mtime, "%011lo", sp->st_mtime);
47487c478bd9Sstevel@tonic-gate }
47497c478bd9Sstevel@tonic-gate 
47507c478bd9Sstevel@tonic-gate static	int
47517c478bd9Sstevel@tonic-gate #ifdef	EUC
47527c478bd9Sstevel@tonic-gate /*
47537c478bd9Sstevel@tonic-gate  * Warning:  the result of this function depends whether 'char' is a
47547c478bd9Sstevel@tonic-gate  * signed or unsigned data type.  This a source of potential
47557c478bd9Sstevel@tonic-gate  * non-portability among heterogeneous systems.  It is retained here
47567c478bd9Sstevel@tonic-gate  * for backward compatibility.
47577c478bd9Sstevel@tonic-gate  */
47587c478bd9Sstevel@tonic-gate checksum_signed(union hblock *dblockp)
47597c478bd9Sstevel@tonic-gate #else
47607c478bd9Sstevel@tonic-gate checksum(union hblock *dblockp)
47617c478bd9Sstevel@tonic-gate #endif	/* EUC */
47627c478bd9Sstevel@tonic-gate {
47637c478bd9Sstevel@tonic-gate 	int i;
47647c478bd9Sstevel@tonic-gate 	char *cp;
47657c478bd9Sstevel@tonic-gate 
47667c478bd9Sstevel@tonic-gate 	for (cp = dblockp->dbuf.chksum;
47677c478bd9Sstevel@tonic-gate 	    cp < &dblockp->dbuf.chksum[sizeof (dblockp->dbuf.chksum)]; cp++)
47687c478bd9Sstevel@tonic-gate 		*cp = ' ';
47697c478bd9Sstevel@tonic-gate 	i = 0;
47707c478bd9Sstevel@tonic-gate 	for (cp = dblockp->dummy; cp < &(dblockp->dummy[TBLOCK]); cp++)
47717c478bd9Sstevel@tonic-gate 		i += *cp;
47727c478bd9Sstevel@tonic-gate 	return (i);
47737c478bd9Sstevel@tonic-gate }
47747c478bd9Sstevel@tonic-gate 
47757c478bd9Sstevel@tonic-gate #ifdef	EUC
47767c478bd9Sstevel@tonic-gate /*
47777c478bd9Sstevel@tonic-gate  * Generate unsigned checksum, regardless of what C compiler is
47787c478bd9Sstevel@tonic-gate  * used.  Survives in the face of arbitrary 8-bit clean filenames,
47797c478bd9Sstevel@tonic-gate  * e.g., internationalized filenames.
47807c478bd9Sstevel@tonic-gate  */
47817c478bd9Sstevel@tonic-gate static int
47827c478bd9Sstevel@tonic-gate checksum(union hblock *dblockp)
47837c478bd9Sstevel@tonic-gate {
47847c478bd9Sstevel@tonic-gate 	unsigned i;
47857c478bd9Sstevel@tonic-gate 	unsigned char *cp;
47867c478bd9Sstevel@tonic-gate 
47877c478bd9Sstevel@tonic-gate 	for (cp = (unsigned char *) dblockp->dbuf.chksum;
47887c478bd9Sstevel@tonic-gate 	    cp < (unsigned char *)
47897c478bd9Sstevel@tonic-gate 	    &(dblockp->dbuf.chksum[sizeof (dblockp->dbuf.chksum)]); cp++)
47907c478bd9Sstevel@tonic-gate 		*cp = ' ';
47917c478bd9Sstevel@tonic-gate 	i = 0;
47927c478bd9Sstevel@tonic-gate 	for (cp = (unsigned char *) dblockp->dummy;
47937c478bd9Sstevel@tonic-gate 	    cp < (unsigned char *) &(dblockp->dummy[TBLOCK]); cp++)
47947c478bd9Sstevel@tonic-gate 		i += *cp;
47957c478bd9Sstevel@tonic-gate 
47967c478bd9Sstevel@tonic-gate 	return (i);
47977c478bd9Sstevel@tonic-gate }
47987c478bd9Sstevel@tonic-gate #endif	/* EUC */
47997c478bd9Sstevel@tonic-gate 
48007c478bd9Sstevel@tonic-gate /*
48017c478bd9Sstevel@tonic-gate  * If the w flag is set, output the action to be taken and the name of the
48027c478bd9Sstevel@tonic-gate  * file.  Perform the action if the user response is affirmative.
48037c478bd9Sstevel@tonic-gate  */
48047c478bd9Sstevel@tonic-gate 
48057c478bd9Sstevel@tonic-gate static int
48067c478bd9Sstevel@tonic-gate checkw(char c, char *name)
48077c478bd9Sstevel@tonic-gate {
48087c478bd9Sstevel@tonic-gate 	if (wflag) {
48097c478bd9Sstevel@tonic-gate 		(void) fprintf(vfile, "%c ", c);
48107c478bd9Sstevel@tonic-gate 		if (vflag)
48117c478bd9Sstevel@tonic-gate 			longt(&stbuf, ' ');	/* do we have acl info here */
48127c478bd9Sstevel@tonic-gate 		(void) fprintf(vfile, "%s: ", name);
48133d63ea05Sas145665 		if (yes() == 1) {
48147c478bd9Sstevel@tonic-gate 			return (1);
48157c478bd9Sstevel@tonic-gate 		}
48167c478bd9Sstevel@tonic-gate 		return (0);
48177c478bd9Sstevel@tonic-gate 	}
48187c478bd9Sstevel@tonic-gate 	return (1);
48197c478bd9Sstevel@tonic-gate }
48207c478bd9Sstevel@tonic-gate 
48217c478bd9Sstevel@tonic-gate /*
48223a1dab68SRich Burridge  * When the F flag is set, exclude RCS and SCCS directories (and any files
48233a1dab68SRich Burridge  * or directories under them).  If F is set twice, also exclude .o files,
48243a1dab68SRich Burridge  * and files names errs, core, and a.out.
48253a1dab68SRich Burridge  *
48263a1dab68SRich Burridge  * Return 0 if file should be excluded, 1 otherwise.
48277c478bd9Sstevel@tonic-gate  */
48287c478bd9Sstevel@tonic-gate 
48297c478bd9Sstevel@tonic-gate static int
48303a1dab68SRich Burridge checkf(char *longname, int is_dir, int howmuch)
48317c478bd9Sstevel@tonic-gate {
48323a1dab68SRich Burridge 	static char fullname[PATH_MAX + 1];
48333a1dab68SRich Burridge 	char *dir, *name;
48347c478bd9Sstevel@tonic-gate 
48353a1dab68SRich Burridge #if defined(O_XATTR)
48363a1dab68SRich Burridge 	/*
48373a1dab68SRich Burridge 	 * If there is an xattr_buf structure associated with this file,
48383a1dab68SRich Burridge 	 * always return 1.
48393a1dab68SRich Burridge 	 */
48403a1dab68SRich Burridge 	if (xattrp) {
48417c478bd9Sstevel@tonic-gate 		return (1);
48427c478bd9Sstevel@tonic-gate 	}
48433a1dab68SRich Burridge #endif
48443a1dab68SRich Burridge 
48453a1dab68SRich Burridge 	/*
48463a1dab68SRich Burridge 	 * First check to see if the base name is an RCS or SCCS directory.
48473a1dab68SRich Burridge 	 */
48483a1dab68SRich Burridge 	if (strlcpy(fullname, longname, sizeof (fullname)) >= sizeof (fullname))
48497c478bd9Sstevel@tonic-gate 		return (1);
48503a1dab68SRich Burridge 
48513a1dab68SRich Burridge 	name = basename(fullname);
48523a1dab68SRich Burridge 	if (is_dir) {
48533a1dab68SRich Burridge 		if ((strcmp(name, "SCCS") == 0) || (strcmp(name, "RCS") == 0))
48547c478bd9Sstevel@tonic-gate 			return (0);
48553a1dab68SRich Burridge 	}
48563a1dab68SRich Burridge 
48573a1dab68SRich Burridge 	/*
48583a1dab68SRich Burridge 	 * If two -F command line options were given then exclude .o files,
48593a1dab68SRich Burridge 	 * and files named errs, core, and a.out.
48603a1dab68SRich Burridge 	 */
48613a1dab68SRich Burridge 	if (howmuch > 1 && !is_dir) {
48623a1dab68SRich Burridge 		size_t l = strlen(name);
48633a1dab68SRich Burridge 
48643a1dab68SRich Burridge 		if (l >= 3 && name[l - 2] == '.' && name[l - 1] == 'o')
48653a1dab68SRich Burridge 			return (0);
48667c478bd9Sstevel@tonic-gate 		if (strcmp(name, "core") == 0 || strcmp(name, "errs") == 0 ||
48677c478bd9Sstevel@tonic-gate 		    strcmp(name, "a.out") == 0)
48687c478bd9Sstevel@tonic-gate 			return (0);
48697c478bd9Sstevel@tonic-gate 	}
48707c478bd9Sstevel@tonic-gate 
48713a1dab68SRich Burridge 	/*
48723a1dab68SRich Burridge 	 * At this point, check to see if this file has a parent directory
48733a1dab68SRich Burridge 	 * named RCS or SCCS.  If so, then this file should be excluded too.
48743a1dab68SRich Burridge 	 * The strcpy() operation is done again, because basename(3C) may
48753a1dab68SRich Burridge 	 * modify the path string passed to it.
48763a1dab68SRich Burridge 	 */
48773a1dab68SRich Burridge 	if (strlcpy(fullname, longname, sizeof (fullname)) >= sizeof (fullname))
48783a1dab68SRich Burridge 		return (1);
48793a1dab68SRich Burridge 
48803a1dab68SRich Burridge 	dir = dirname(fullname);
48813a1dab68SRich Burridge 	while (strcmp(dir, ".") != 0) {
48823a1dab68SRich Burridge 		name = basename(dir);
48833a1dab68SRich Burridge 		if ((strcmp(name, "SCCS") == 0) || (strcmp(name, "RCS") == 0))
48843a1dab68SRich Burridge 			return (0);
48853a1dab68SRich Burridge 		dir = dirname(dir);
48863a1dab68SRich Burridge 	}
48873a1dab68SRich Burridge 
48887c478bd9Sstevel@tonic-gate 	return (1);
48897c478bd9Sstevel@tonic-gate }
48907c478bd9Sstevel@tonic-gate 
48917c478bd9Sstevel@tonic-gate static int
48927c478bd9Sstevel@tonic-gate response(void)
48937c478bd9Sstevel@tonic-gate {
48947c478bd9Sstevel@tonic-gate 	int c;
48957c478bd9Sstevel@tonic-gate 
48967c478bd9Sstevel@tonic-gate 	c = getchar();
48977c478bd9Sstevel@tonic-gate 	if (c != '\n')
4898eace40a5Sceastha 		while (getchar() != '\n')
4899eace40a5Sceastha 			;
49007c478bd9Sstevel@tonic-gate 	else c = 'n';
49017c478bd9Sstevel@tonic-gate 	return ((c >= 'A' && c <= 'Z') ? c + ('a'-'A') : c);
49027c478bd9Sstevel@tonic-gate }
49037c478bd9Sstevel@tonic-gate 
49047c478bd9Sstevel@tonic-gate /* Has file been modified since being put into archive? If so, return > 0. */
49057c478bd9Sstevel@tonic-gate 
4906d67944fbSScott Rotondo static off_t	lookup(char *);
4907d67944fbSScott Rotondo 
49087c478bd9Sstevel@tonic-gate static int
49097c478bd9Sstevel@tonic-gate checkupdate(char *arg)
49107c478bd9Sstevel@tonic-gate {
49117c478bd9Sstevel@tonic-gate 	char name[PATH_MAX+1];
49127c478bd9Sstevel@tonic-gate 	time_t	mtime;
49137c478bd9Sstevel@tonic-gate 	long nsecs;
49147c478bd9Sstevel@tonic-gate 	off_t seekp;
49157c478bd9Sstevel@tonic-gate 
49167c478bd9Sstevel@tonic-gate 	rewind(tfile);
49177c478bd9Sstevel@tonic-gate 	if ((seekp = lookup(arg)) < 0)
49187c478bd9Sstevel@tonic-gate 		return (1);
49197c478bd9Sstevel@tonic-gate 	(void) fseek(tfile, seekp, 0);
49207c478bd9Sstevel@tonic-gate 	(void) fscanf(tfile, "%s %ld.%ld", name, &mtime, &nsecs);
49217c478bd9Sstevel@tonic-gate 
49227c478bd9Sstevel@tonic-gate 	/*
49237c478bd9Sstevel@tonic-gate 	 * Unless nanoseconds were stored in the file, only use seconds for
49247c478bd9Sstevel@tonic-gate 	 * comparison of time.  Nanoseconds are stored when -E is specified.
49257c478bd9Sstevel@tonic-gate 	 */
49267c478bd9Sstevel@tonic-gate 	if (Eflag == 0)
49277c478bd9Sstevel@tonic-gate 		return (stbuf.st_mtime > mtime);
49287c478bd9Sstevel@tonic-gate 
49297c478bd9Sstevel@tonic-gate 	if ((stbuf.st_mtime < mtime) ||
49307c478bd9Sstevel@tonic-gate 	    ((stbuf.st_mtime == mtime) && (stbuf.st_mtim.tv_nsec <= nsecs)))
49317c478bd9Sstevel@tonic-gate 		return (0);
49327c478bd9Sstevel@tonic-gate 	return (1);
49337c478bd9Sstevel@tonic-gate }
49347c478bd9Sstevel@tonic-gate 
49357c478bd9Sstevel@tonic-gate 
49367c478bd9Sstevel@tonic-gate /*
49377c478bd9Sstevel@tonic-gate  *	newvol	get new floppy (or tape) volume
49387c478bd9Sstevel@tonic-gate  *
49397c478bd9Sstevel@tonic-gate  *	newvol();		resets tapepos and first to TRUE, prompts for
49407c478bd9Sstevel@tonic-gate  *				for new volume, and waits.
49417c478bd9Sstevel@tonic-gate  *	if dumping, end-of-file is written onto the tape.
49427c478bd9Sstevel@tonic-gate  */
49437c478bd9Sstevel@tonic-gate 
49447c478bd9Sstevel@tonic-gate static void
49457c478bd9Sstevel@tonic-gate newvol(void)
49467c478bd9Sstevel@tonic-gate {
49477c478bd9Sstevel@tonic-gate 	int c;
49487c478bd9Sstevel@tonic-gate 
49497c478bd9Sstevel@tonic-gate 	if (dumping) {
49507c478bd9Sstevel@tonic-gate #ifdef DEBUG
49517c478bd9Sstevel@tonic-gate 		DEBUG("newvol called with 'dumping' set\n", 0, 0);
49527c478bd9Sstevel@tonic-gate #endif
49537c478bd9Sstevel@tonic-gate 		putempty((blkcnt_t)2);	/* 2 EOT marks */
49547c478bd9Sstevel@tonic-gate 		closevol();
49557c478bd9Sstevel@tonic-gate 		flushtape();
49567c478bd9Sstevel@tonic-gate 		sync();
49577c478bd9Sstevel@tonic-gate 		tapepos = 0;
49587c478bd9Sstevel@tonic-gate 	} else
49597c478bd9Sstevel@tonic-gate 		first = TRUE;
49607c478bd9Sstevel@tonic-gate 	if (close(mt) != 0)
49617c478bd9Sstevel@tonic-gate 		vperror(2, gettext("close error"));
49627c478bd9Sstevel@tonic-gate 	mt = 0;
49637c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
49647c478bd9Sstevel@tonic-gate 	    "tar: \007please insert new volume, then press RETURN."));
49657c478bd9Sstevel@tonic-gate 	(void) fseek(stdin, (off_t)0, 2);	/* scan over read-ahead */
49667c478bd9Sstevel@tonic-gate 	while ((c = getchar()) != '\n' && ! term)
49677c478bd9Sstevel@tonic-gate 		if (c == EOF)
49687c478bd9Sstevel@tonic-gate 			done(Errflg);
49697c478bd9Sstevel@tonic-gate 	if (term)
49707c478bd9Sstevel@tonic-gate 		done(Errflg);
49717c478bd9Sstevel@tonic-gate 
49727c478bd9Sstevel@tonic-gate 	errno = 0;
49737c478bd9Sstevel@tonic-gate 
49747c478bd9Sstevel@tonic-gate 	if (strcmp(usefile, "-") == 0) {
49757c478bd9Sstevel@tonic-gate 		mt = dup(1);
49767c478bd9Sstevel@tonic-gate 	} else {
49777c478bd9Sstevel@tonic-gate 		mt = open(usefile, dumping ? update : 0);
49787c478bd9Sstevel@tonic-gate 	}
49797c478bd9Sstevel@tonic-gate 
49807c478bd9Sstevel@tonic-gate 	if (mt < 0) {
49817c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
49827c478bd9Sstevel@tonic-gate 		    "tar: cannot reopen %s (%s)\n"),
49837c478bd9Sstevel@tonic-gate 		    dumping ? gettext("output") : gettext("input"), usefile);
49847c478bd9Sstevel@tonic-gate 
49858e4a71aeSRich Burridge #ifdef DEBUG
49868e4a71aeSRich Burridge 		DEBUG("update=%d, usefile=%s ", update, usefile);
49878e4a71aeSRich Burridge 		DEBUG("mt=%d, [%s]\n", mt, strerror(errno));
49888e4a71aeSRich Burridge #endif
49897c478bd9Sstevel@tonic-gate 
49907c478bd9Sstevel@tonic-gate 		done(2);
49917c478bd9Sstevel@tonic-gate 	}
49927c478bd9Sstevel@tonic-gate }
49937c478bd9Sstevel@tonic-gate 
49947c478bd9Sstevel@tonic-gate /*
49957c478bd9Sstevel@tonic-gate  * Write a trailer portion to close out the current output volume.
49967c478bd9Sstevel@tonic-gate  */
49977c478bd9Sstevel@tonic-gate 
49987c478bd9Sstevel@tonic-gate static void
49997c478bd9Sstevel@tonic-gate closevol(void)
50007c478bd9Sstevel@tonic-gate {
50017c478bd9Sstevel@tonic-gate 	if (mulvol) {
50027c478bd9Sstevel@tonic-gate 		/*
50037c478bd9Sstevel@tonic-gate 		 * blocklim does not count the 2 EOT marks;
50047c478bd9Sstevel@tonic-gate 		 * tapepos  does count the 2 EOT marks;
50057c478bd9Sstevel@tonic-gate 		 * therefore we need the +2 below.
50067c478bd9Sstevel@tonic-gate 		 */
50077c478bd9Sstevel@tonic-gate 		putempty(blocklim + (blkcnt_t)2 - tapepos);
50087c478bd9Sstevel@tonic-gate 	}
50097c478bd9Sstevel@tonic-gate }
50107c478bd9Sstevel@tonic-gate 
50117c478bd9Sstevel@tonic-gate static void
50127c478bd9Sstevel@tonic-gate done(int n)
50137c478bd9Sstevel@tonic-gate {
501431732068SRich Burridge 	/*
501531732068SRich Burridge 	 * If we were terminated in some way, and we would otherwise have
501631732068SRich Burridge 	 * exited with a value of 0, adjust to 1, so that external callers
501731732068SRich Burridge 	 * can determine this by looking at the exit status.
501831732068SRich Burridge 	 */
501931732068SRich Burridge 	if (term && n == 0)
502031732068SRich Burridge 		n = 1;
502131732068SRich Burridge 
50228f1d104bSRich Burridge 	if (tfile != NULL)
50237c478bd9Sstevel@tonic-gate 		(void) unlink(tname);
502436802407SRich Burridge 	if (compress_opt != NULL)
502536802407SRich Burridge 		(void) free(compress_opt);
50267c478bd9Sstevel@tonic-gate 	if (mt > 0) {
50277c478bd9Sstevel@tonic-gate 		if ((close(mt) != 0) || (fclose(stdout) != 0)) {
50287c478bd9Sstevel@tonic-gate 			perror(gettext("tar: close error"));
50297c478bd9Sstevel@tonic-gate 			exit(2);
50307c478bd9Sstevel@tonic-gate 		}
50317c478bd9Sstevel@tonic-gate 	}
50327c478bd9Sstevel@tonic-gate 	exit(n);
50337c478bd9Sstevel@tonic-gate }
50347c478bd9Sstevel@tonic-gate 
50357c478bd9Sstevel@tonic-gate /*
50367c478bd9Sstevel@tonic-gate  * Determine if s1 is a prefix portion of s2 (or the same as s2).
50377c478bd9Sstevel@tonic-gate  */
50387c478bd9Sstevel@tonic-gate 
50397c478bd9Sstevel@tonic-gate static	int
50407c478bd9Sstevel@tonic-gate is_prefix(char *s1, char *s2)
50417c478bd9Sstevel@tonic-gate {
50427c478bd9Sstevel@tonic-gate 	while (*s1)
50437c478bd9Sstevel@tonic-gate 		if (*s1++ != *s2++)
50447c478bd9Sstevel@tonic-gate 			return (0);
50457c478bd9Sstevel@tonic-gate 	if (*s2)
50467c478bd9Sstevel@tonic-gate 		return (*s2 == '/');
50477c478bd9Sstevel@tonic-gate 	return (1);
50487c478bd9Sstevel@tonic-gate }
50497c478bd9Sstevel@tonic-gate 
50507c478bd9Sstevel@tonic-gate /*
50517c478bd9Sstevel@tonic-gate  * lookup and bsrch look through tfile entries to find a match for a name.
50527c478bd9Sstevel@tonic-gate  * The name can be up to PATH_MAX bytes.  bsrch compares what it sees between
50537c478bd9Sstevel@tonic-gate  * a pair of newline chars, so the buffer it uses must be long enough for
50547c478bd9Sstevel@tonic-gate  * two lines:  name and modification time as well as period, newline and space.
50557c478bd9Sstevel@tonic-gate  *
50567c478bd9Sstevel@tonic-gate  * A kludge was added to bsrch to take care of matching on the first entry
50577c478bd9Sstevel@tonic-gate  * in the file--there is no leading newline.  So, if we are reading from the
50587c478bd9Sstevel@tonic-gate  * start of the file, read into byte two and set the first byte to a newline.
50597c478bd9Sstevel@tonic-gate  * Otherwise, the first entry cannot be matched.
50607c478bd9Sstevel@tonic-gate  *
50617c478bd9Sstevel@tonic-gate  */
50627c478bd9Sstevel@tonic-gate 
50637c478bd9Sstevel@tonic-gate #define	N	(2 * (PATH_MAX + TIME_MAX_DIGITS + LONG_MAX_DIGITS + 3))
50647c478bd9Sstevel@tonic-gate static	off_t
50657c478bd9Sstevel@tonic-gate lookup(char *s)
50667c478bd9Sstevel@tonic-gate {
50677c478bd9Sstevel@tonic-gate 	int i;
50687c478bd9Sstevel@tonic-gate 	off_t a;
50697c478bd9Sstevel@tonic-gate 
50707c478bd9Sstevel@tonic-gate 	for (i = 0; s[i]; i++)
50717c478bd9Sstevel@tonic-gate 		if (s[i] == ' ')
50727c478bd9Sstevel@tonic-gate 			break;
50737c478bd9Sstevel@tonic-gate 	a = bsrch(s, i, low, high);
50747c478bd9Sstevel@tonic-gate 	return (a);
50757c478bd9Sstevel@tonic-gate }
50767c478bd9Sstevel@tonic-gate 
50777c478bd9Sstevel@tonic-gate static off_t
50787c478bd9Sstevel@tonic-gate bsrch(char *s, int n, off_t l, off_t h)
50797c478bd9Sstevel@tonic-gate {
50807c478bd9Sstevel@tonic-gate 	int i, j;
50817c478bd9Sstevel@tonic-gate 	char b[N];
50827c478bd9Sstevel@tonic-gate 	off_t m, m1;
50837c478bd9Sstevel@tonic-gate 
50847c478bd9Sstevel@tonic-gate 
50857c478bd9Sstevel@tonic-gate loop:
50867c478bd9Sstevel@tonic-gate 	if (l >= h)
50877c478bd9Sstevel@tonic-gate 		return ((off_t)-1);
50887c478bd9Sstevel@tonic-gate 	m = l + (h-l)/2 - N/2;
50897c478bd9Sstevel@tonic-gate 	if (m < l)
50907c478bd9Sstevel@tonic-gate 		m = l;
50917c478bd9Sstevel@tonic-gate 	(void) fseek(tfile, m, 0);
50927c478bd9Sstevel@tonic-gate 	if (m == 0) {
50937c478bd9Sstevel@tonic-gate 		(void) fread(b+1, 1, N-1, tfile);
50947c478bd9Sstevel@tonic-gate 		b[0] = '\n';
50957c478bd9Sstevel@tonic-gate 		m--;
50967c478bd9Sstevel@tonic-gate 	} else
50977c478bd9Sstevel@tonic-gate 		(void) fread(b, 1, N, tfile);
50987c478bd9Sstevel@tonic-gate 	for (i = 0; i < N; i++) {
50997c478bd9Sstevel@tonic-gate 		if (b[i] == '\n')
51007c478bd9Sstevel@tonic-gate 			break;
51017c478bd9Sstevel@tonic-gate 		m++;
51027c478bd9Sstevel@tonic-gate 	}
51037c478bd9Sstevel@tonic-gate 	if (m >= h)
51047c478bd9Sstevel@tonic-gate 		return ((off_t)-1);
51057c478bd9Sstevel@tonic-gate 	m1 = m;
51067c478bd9Sstevel@tonic-gate 	j = i;
51077c478bd9Sstevel@tonic-gate 	for (i++; i < N; i++) {
51087c478bd9Sstevel@tonic-gate 		m1++;
51097c478bd9Sstevel@tonic-gate 		if (b[i] == '\n')
51107c478bd9Sstevel@tonic-gate 			break;
51117c478bd9Sstevel@tonic-gate 	}
51127c478bd9Sstevel@tonic-gate 	i = cmp(b+j, s, n);
51137c478bd9Sstevel@tonic-gate 	if (i < 0) {
51147c478bd9Sstevel@tonic-gate 		h = m;
51157c478bd9Sstevel@tonic-gate 		goto loop;
51167c478bd9Sstevel@tonic-gate 	}
51177c478bd9Sstevel@tonic-gate 	if (i > 0) {
51187c478bd9Sstevel@tonic-gate 		l = m1;
51197c478bd9Sstevel@tonic-gate 		goto loop;
51207c478bd9Sstevel@tonic-gate 	}
51217c478bd9Sstevel@tonic-gate 	if (m < 0)
51227c478bd9Sstevel@tonic-gate 		m = 0;
51237c478bd9Sstevel@tonic-gate 	return (m);
51247c478bd9Sstevel@tonic-gate }
51257c478bd9Sstevel@tonic-gate 
51267c478bd9Sstevel@tonic-gate static int
51277c478bd9Sstevel@tonic-gate cmp(char *b, char *s, int n)
51287c478bd9Sstevel@tonic-gate {
51297c478bd9Sstevel@tonic-gate 	int i;
51307c478bd9Sstevel@tonic-gate 
51317c478bd9Sstevel@tonic-gate 	assert(b[0] == '\n');
51327c478bd9Sstevel@tonic-gate 
51337c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
51347c478bd9Sstevel@tonic-gate 		if (b[i+1] > s[i])
51357c478bd9Sstevel@tonic-gate 			return (-1);
51367c478bd9Sstevel@tonic-gate 		if (b[i+1] < s[i])
51377c478bd9Sstevel@tonic-gate 			return (1);
51387c478bd9Sstevel@tonic-gate 	}
51397c478bd9Sstevel@tonic-gate 	return (b[i+1] == ' '? 0 : -1);
51407c478bd9Sstevel@tonic-gate }
51417c478bd9Sstevel@tonic-gate 
51427c478bd9Sstevel@tonic-gate 
51437c478bd9Sstevel@tonic-gate /*
51447c478bd9Sstevel@tonic-gate  *	seekdisk	seek to next file on archive
51457c478bd9Sstevel@tonic-gate  *
51467c478bd9Sstevel@tonic-gate  *	called by passtape() only
51477c478bd9Sstevel@tonic-gate  *
51487c478bd9Sstevel@tonic-gate  *	WARNING: expects "nblock" to be set, that is, readtape() to have
51497c478bd9Sstevel@tonic-gate  *		already been called.  Since passtape() is only called
51507c478bd9Sstevel@tonic-gate  *		after a file header block has been read (why else would
51517c478bd9Sstevel@tonic-gate  *		we skip to next file?), this is currently safe.
51527c478bd9Sstevel@tonic-gate  *
51537c478bd9Sstevel@tonic-gate  *	changed to guarantee SYS_BLOCK boundary
51547c478bd9Sstevel@tonic-gate  */
51557c478bd9Sstevel@tonic-gate 
51567c478bd9Sstevel@tonic-gate static void
51577c478bd9Sstevel@tonic-gate seekdisk(blkcnt_t blocks)
51587c478bd9Sstevel@tonic-gate {
51597c478bd9Sstevel@tonic-gate 	off_t seekval;
51607c478bd9Sstevel@tonic-gate #if SYS_BLOCK > TBLOCK
51617c478bd9Sstevel@tonic-gate 	/* handle non-multiple of SYS_BLOCK */
51627c478bd9Sstevel@tonic-gate 	blkcnt_t nxb;	/* # extra blocks */
51637c478bd9Sstevel@tonic-gate #endif
51647c478bd9Sstevel@tonic-gate 
51657c478bd9Sstevel@tonic-gate 	tapepos += blocks;
51667c478bd9Sstevel@tonic-gate #ifdef DEBUG
51677c478bd9Sstevel@tonic-gate 	DEBUG("seekdisk(%" FMT_blkcnt_t ") called\n", blocks, 0);
51687c478bd9Sstevel@tonic-gate #endif
51697c478bd9Sstevel@tonic-gate 	if (recno + blocks <= nblock) {
51707c478bd9Sstevel@tonic-gate 		recno += blocks;
51717c478bd9Sstevel@tonic-gate 		return;
51727c478bd9Sstevel@tonic-gate 	}
51737c478bd9Sstevel@tonic-gate 	if (recno > nblock)
51747c478bd9Sstevel@tonic-gate 		recno = nblock;
51757c478bd9Sstevel@tonic-gate 	seekval = (off_t)blocks - (nblock - recno);
51767c478bd9Sstevel@tonic-gate 	recno = nblock;	/* so readtape() reads next time through */
51777c478bd9Sstevel@tonic-gate #if SYS_BLOCK > TBLOCK
51787c478bd9Sstevel@tonic-gate 	nxb = (blkcnt_t)(seekval % (off_t)(SYS_BLOCK / TBLOCK));
51797c478bd9Sstevel@tonic-gate #ifdef DEBUG
51807c478bd9Sstevel@tonic-gate 	DEBUG("xtrablks=%" FMT_blkcnt_t " seekval=%" FMT_blkcnt_t " blks\n",
51817c478bd9Sstevel@tonic-gate 	    nxb, seekval);
51827c478bd9Sstevel@tonic-gate #endif
51837c478bd9Sstevel@tonic-gate 	if (nxb && nxb > seekval) /* don't seek--we'll read */
51847c478bd9Sstevel@tonic-gate 		goto noseek;
51857c478bd9Sstevel@tonic-gate 	seekval -=  nxb;	/* don't seek quite so far */
51867c478bd9Sstevel@tonic-gate #endif
51877c478bd9Sstevel@tonic-gate 	if (lseek(mt, (off_t)(TBLOCK * seekval), 1) == (off_t)-1) {
51887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
51897c478bd9Sstevel@tonic-gate 		    "tar: device seek error\n"));
51907c478bd9Sstevel@tonic-gate 		done(3);
51917c478bd9Sstevel@tonic-gate 	}
51927c478bd9Sstevel@tonic-gate #if SYS_BLOCK > TBLOCK
51937c478bd9Sstevel@tonic-gate 	/* read those extra blocks */
51947c478bd9Sstevel@tonic-gate noseek:
51957c478bd9Sstevel@tonic-gate 	if (nxb) {
51967c478bd9Sstevel@tonic-gate #ifdef DEBUG
51977c478bd9Sstevel@tonic-gate 		DEBUG("reading extra blocks\n", 0, 0);
51987c478bd9Sstevel@tonic-gate #endif
51997c478bd9Sstevel@tonic-gate 		if (read(mt, tbuf, TBLOCK*nblock) < 0) {
52007c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
52017c478bd9Sstevel@tonic-gate 			    "tar: read error while skipping file\n"));
52027c478bd9Sstevel@tonic-gate 			done(8);
52037c478bd9Sstevel@tonic-gate 		}
52047c478bd9Sstevel@tonic-gate 		recno = nxb;	/* so we don't read in next readtape() */
52057c478bd9Sstevel@tonic-gate 	}
52067c478bd9Sstevel@tonic-gate #endif
52077c478bd9Sstevel@tonic-gate }
52087c478bd9Sstevel@tonic-gate 
52097c478bd9Sstevel@tonic-gate static void
52107c478bd9Sstevel@tonic-gate readtape(char *buffer)
52117c478bd9Sstevel@tonic-gate {
52127c478bd9Sstevel@tonic-gate 	int i, j;
52137c478bd9Sstevel@tonic-gate 
52147c478bd9Sstevel@tonic-gate 	++tapepos;
52157c478bd9Sstevel@tonic-gate 	if (recno >= nblock || first) {
52167c478bd9Sstevel@tonic-gate 		if (first) {
52177c478bd9Sstevel@tonic-gate 			/*
52187c478bd9Sstevel@tonic-gate 			 * set the number of blocks to read initially, based on
52197c478bd9Sstevel@tonic-gate 			 * the defined defaults for the device, or on the
52207c478bd9Sstevel@tonic-gate 			 * explicit block factor given.
52217c478bd9Sstevel@tonic-gate 			 */
522253329f75SRich Burridge 			if (bflag || defaults_used || NotTape)
52237c478bd9Sstevel@tonic-gate 				j = nblock;
52247c478bd9Sstevel@tonic-gate 			else
52257c478bd9Sstevel@tonic-gate 				j = NBLOCK;
52267c478bd9Sstevel@tonic-gate 		} else
52277c478bd9Sstevel@tonic-gate 			j = nblock;
52287c478bd9Sstevel@tonic-gate 
52297c478bd9Sstevel@tonic-gate 		if ((i = read(mt, tbuf, TBLOCK*j)) < 0) {
52307c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
52317c478bd9Sstevel@tonic-gate 			    "tar: tape read error\n"));
52327c478bd9Sstevel@tonic-gate 			done(3);
52337c478bd9Sstevel@tonic-gate 		/*
5234b35e803cSss161016 		 * i == 0 and !rflag means that EOF is reached and we are
5235b35e803cSss161016 		 * trying to update or replace an empty tar file, so exit
5236b35e803cSss161016 		 * with an error.
5237b35e803cSss161016 		 *
5238b35e803cSss161016 		 * If i == 0 and !first and NotTape, it means the pointer
5239b35e803cSss161016 		 * has gone past the EOF. It could happen if two processes
5240b35e803cSss161016 		 * try to update the same tar file simultaneously. So exit
5241b35e803cSss161016 		 * with an error.
52427c478bd9Sstevel@tonic-gate 		 */
5243b35e803cSss161016 
5244b35e803cSss161016 		} else if (i == 0) {
5245b35e803cSss161016 			if (first && !rflag) {
52467c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
52477c478bd9Sstevel@tonic-gate 				    "tar: blocksize = %d\n"), i);
52487c478bd9Sstevel@tonic-gate 				done(Errflg);
5249b35e803cSss161016 			} else if (!first && (!rflag || NotTape)) {
52507c478bd9Sstevel@tonic-gate 				mterr("read", 0, 2);
5251b35e803cSss161016 			}
52527c478bd9Sstevel@tonic-gate 		} else if ((!first || Bflag) && i != TBLOCK*j) {
52537c478bd9Sstevel@tonic-gate 			/*
52547c478bd9Sstevel@tonic-gate 			 * Short read - try to get the remaining bytes.
52557c478bd9Sstevel@tonic-gate 			 */
52567c478bd9Sstevel@tonic-gate 
52577c478bd9Sstevel@tonic-gate 			int remaining = (TBLOCK * j) - i;
52587c478bd9Sstevel@tonic-gate 			char *b = (char *)tbuf + i;
52597c478bd9Sstevel@tonic-gate 			int r;
52607c478bd9Sstevel@tonic-gate 
52617c478bd9Sstevel@tonic-gate 			do {
52627c478bd9Sstevel@tonic-gate 				if ((r = read(mt, b, remaining)) < 0) {
52637c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
52647c478bd9Sstevel@tonic-gate 					    gettext("tar: tape read error\n"));
52657c478bd9Sstevel@tonic-gate 					done(3);
52667c478bd9Sstevel@tonic-gate 				}
52677c478bd9Sstevel@tonic-gate 				b += r;
52687c478bd9Sstevel@tonic-gate 				remaining -= r;
52697c478bd9Sstevel@tonic-gate 				i += r;
52707c478bd9Sstevel@tonic-gate 			} while (remaining > 0 && r != 0);
52717c478bd9Sstevel@tonic-gate 		}
52727c478bd9Sstevel@tonic-gate 		if (first) {
52737c478bd9Sstevel@tonic-gate 			if ((i % TBLOCK) != 0) {
52747c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
52757c478bd9Sstevel@tonic-gate 				    "tar: tape blocksize error\n"));
52767c478bd9Sstevel@tonic-gate 				done(3);
52777c478bd9Sstevel@tonic-gate 			}
52787c478bd9Sstevel@tonic-gate 			i /= TBLOCK;
52797c478bd9Sstevel@tonic-gate 			if (vflag && i != nblock && i != 1) {
52807c478bd9Sstevel@tonic-gate 				if (!NotTape)
52817c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
52827c478bd9Sstevel@tonic-gate 					    "tar: blocksize = %d\n"), i);
52837c478bd9Sstevel@tonic-gate 			}
52847c478bd9Sstevel@tonic-gate 
52857c478bd9Sstevel@tonic-gate 			/*
52867c478bd9Sstevel@tonic-gate 			 * If we are reading a tape, then a short read is
52877c478bd9Sstevel@tonic-gate 			 * understood to signify that the amount read is
52887c478bd9Sstevel@tonic-gate 			 * the tape's actual blocking factor.  We adapt
52897c478bd9Sstevel@tonic-gate 			 * nblock accordingly.  There is no reason to do
52907c478bd9Sstevel@tonic-gate 			 * this when the device is not blocked.
52917c478bd9Sstevel@tonic-gate 			 */
52927c478bd9Sstevel@tonic-gate 
52937c478bd9Sstevel@tonic-gate 			if (!NotTape)
52947c478bd9Sstevel@tonic-gate 				nblock = i;
52957c478bd9Sstevel@tonic-gate 		}
52967c478bd9Sstevel@tonic-gate 		recno = 0;
52977c478bd9Sstevel@tonic-gate 	}
52987c478bd9Sstevel@tonic-gate 
52997c478bd9Sstevel@tonic-gate 	first = FALSE;
53007c478bd9Sstevel@tonic-gate 	copy(buffer, &tbuf[recno++]);
53017c478bd9Sstevel@tonic-gate }
53027c478bd9Sstevel@tonic-gate 
53037c478bd9Sstevel@tonic-gate 
53047c478bd9Sstevel@tonic-gate /*
53057c478bd9Sstevel@tonic-gate  * replacement for writetape.
53067c478bd9Sstevel@tonic-gate  */
53077c478bd9Sstevel@tonic-gate 
53087c478bd9Sstevel@tonic-gate static int
53097c478bd9Sstevel@tonic-gate writetbuf(char *buffer, int n)
53107c478bd9Sstevel@tonic-gate {
53117c478bd9Sstevel@tonic-gate 	int i;
53127c478bd9Sstevel@tonic-gate 
53137c478bd9Sstevel@tonic-gate 	tapepos += n;		/* output block count */
53147c478bd9Sstevel@tonic-gate 
53157c478bd9Sstevel@tonic-gate 	if (recno >= nblock) {
53167c478bd9Sstevel@tonic-gate 		i = write(mt, (char *)tbuf, TBLOCK*nblock);
53177c478bd9Sstevel@tonic-gate 		if (i != TBLOCK*nblock)
53187c478bd9Sstevel@tonic-gate 			mterr("write", i, 2);
53197c478bd9Sstevel@tonic-gate 		recno = 0;
53207c478bd9Sstevel@tonic-gate 	}
53217c478bd9Sstevel@tonic-gate 
53227c478bd9Sstevel@tonic-gate 	/*
53237c478bd9Sstevel@tonic-gate 	 *  Special case:  We have an empty tape buffer, and the
53247c478bd9Sstevel@tonic-gate 	 *  users data size is >= the tape block size:  Avoid
53257c478bd9Sstevel@tonic-gate 	 *  the bcopy and dma direct to tape.  BIG WIN.  Add the
53267c478bd9Sstevel@tonic-gate 	 *  residual to the tape buffer.
53277c478bd9Sstevel@tonic-gate 	 */
53287c478bd9Sstevel@tonic-gate 	while (recno == 0 && n >= nblock) {
53297c478bd9Sstevel@tonic-gate 		i = (int)write(mt, buffer, TBLOCK*nblock);
53307c478bd9Sstevel@tonic-gate 		if (i != TBLOCK*nblock)
53317c478bd9Sstevel@tonic-gate 			mterr("write", i, 2);
53327c478bd9Sstevel@tonic-gate 		n -= nblock;
53337c478bd9Sstevel@tonic-gate 		buffer += (nblock * TBLOCK);
53347c478bd9Sstevel@tonic-gate 	}
53357c478bd9Sstevel@tonic-gate 
53367c478bd9Sstevel@tonic-gate 	while (n-- > 0) {
53377c478bd9Sstevel@tonic-gate 		(void) memcpy((char *)&tbuf[recno++], buffer, TBLOCK);
53387c478bd9Sstevel@tonic-gate 		buffer += TBLOCK;
53397c478bd9Sstevel@tonic-gate 		if (recno >= nblock) {
53407c478bd9Sstevel@tonic-gate 			i = (int)write(mt, (char *)tbuf, TBLOCK*nblock);
53417c478bd9Sstevel@tonic-gate 			if (i != TBLOCK*nblock)
53427c478bd9Sstevel@tonic-gate 				mterr("write", i, 2);
53437c478bd9Sstevel@tonic-gate 			recno = 0;
53447c478bd9Sstevel@tonic-gate 		}
53457c478bd9Sstevel@tonic-gate 	}
53467c478bd9Sstevel@tonic-gate 
53477c478bd9Sstevel@tonic-gate 	/* Tell the user how much to write to get in sync */
53487c478bd9Sstevel@tonic-gate 	return (nblock - recno);
53497c478bd9Sstevel@tonic-gate }
53507c478bd9Sstevel@tonic-gate 
53517c478bd9Sstevel@tonic-gate /*
53527c478bd9Sstevel@tonic-gate  *	backtape - reposition tape after reading soft "EOF" record
53537c478bd9Sstevel@tonic-gate  *
53547c478bd9Sstevel@tonic-gate  *	Backtape tries to reposition the tape back over the EOF
53557c478bd9Sstevel@tonic-gate  *	record.  This is for the 'u' and 'r' function letters so that the
53567c478bd9Sstevel@tonic-gate  *	tape can be extended.  This code is not well designed, but
53577c478bd9Sstevel@tonic-gate  *	I'm confident that the only callers who care about the
53587c478bd9Sstevel@tonic-gate  *	backspace-over-EOF feature are those involved in 'u' and 'r'.
53597c478bd9Sstevel@tonic-gate  *
53607c478bd9Sstevel@tonic-gate  *	The proper way to backup the tape is through the use of mtio.
53617c478bd9Sstevel@tonic-gate  *	Earlier spins used lseek combined with reads in a confusing
53627c478bd9Sstevel@tonic-gate  *	maneuver that only worked on 4.x, but shouldn't have, even
53637c478bd9Sstevel@tonic-gate  *	there.  Lseeks are explicitly not supported for tape devices.
53647c478bd9Sstevel@tonic-gate  */
53657c478bd9Sstevel@tonic-gate 
53667c478bd9Sstevel@tonic-gate static void
53677c478bd9Sstevel@tonic-gate backtape(void)
53687c478bd9Sstevel@tonic-gate {
53697c478bd9Sstevel@tonic-gate 	struct mtop mtcmd;
53707c478bd9Sstevel@tonic-gate #ifdef DEBUG
53717c478bd9Sstevel@tonic-gate 	DEBUG("backtape() called, recno=%" FMT_blkcnt_t " nblock=%d\n", recno,
53727c478bd9Sstevel@tonic-gate 	    nblock);
53737c478bd9Sstevel@tonic-gate #endif
53747c478bd9Sstevel@tonic-gate 	/*
53757c478bd9Sstevel@tonic-gate 	 * Backup to the position in the archive where the record
53767c478bd9Sstevel@tonic-gate 	 * currently sitting in the tbuf buffer is situated.
53777c478bd9Sstevel@tonic-gate 	 */
53787c478bd9Sstevel@tonic-gate 
53797c478bd9Sstevel@tonic-gate 	if (NotTape) {
53807c478bd9Sstevel@tonic-gate 		/*
53817c478bd9Sstevel@tonic-gate 		 * For non-tape devices, this means lseeking to the
53827c478bd9Sstevel@tonic-gate 		 * correct position.  The absolute location tapepos-recno
53837c478bd9Sstevel@tonic-gate 		 * should be the beginning of the current record.
53847c478bd9Sstevel@tonic-gate 		 */
53857c478bd9Sstevel@tonic-gate 
53867c478bd9Sstevel@tonic-gate 		if (lseek(mt, (off_t)(TBLOCK*(tapepos-recno)), SEEK_SET) ==
53877c478bd9Sstevel@tonic-gate 		    (off_t)-1) {
53887c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
53897c478bd9Sstevel@tonic-gate 			    gettext("tar: lseek to end of archive failed\n"));
53907c478bd9Sstevel@tonic-gate 			done(4);
53917c478bd9Sstevel@tonic-gate 		}
53927c478bd9Sstevel@tonic-gate 	} else {
53937c478bd9Sstevel@tonic-gate 		/*
53947c478bd9Sstevel@tonic-gate 		 * For tape devices, we backup over the most recently
53957c478bd9Sstevel@tonic-gate 		 * read record.
53967c478bd9Sstevel@tonic-gate 		 */
53977c478bd9Sstevel@tonic-gate 
53987c478bd9Sstevel@tonic-gate 		mtcmd.mt_op = MTBSR;
53997c478bd9Sstevel@tonic-gate 		mtcmd.mt_count = 1;
54007c478bd9Sstevel@tonic-gate 
54017c478bd9Sstevel@tonic-gate 		if (ioctl(mt, MTIOCTOP, &mtcmd) < 0) {
54027c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
54037c478bd9Sstevel@tonic-gate 			    gettext("tar: backspace over record failed\n"));
54047c478bd9Sstevel@tonic-gate 			done(4);
54057c478bd9Sstevel@tonic-gate 		}
54067c478bd9Sstevel@tonic-gate 	}
54077c478bd9Sstevel@tonic-gate 
54087c478bd9Sstevel@tonic-gate 	/*
54097c478bd9Sstevel@tonic-gate 	 * Decrement the tape and tbuf buffer indices to prepare for the
54107c478bd9Sstevel@tonic-gate 	 * coming write to overwrite the soft EOF record.
54117c478bd9Sstevel@tonic-gate 	 */
54127c478bd9Sstevel@tonic-gate 
54137c478bd9Sstevel@tonic-gate 	recno--;
54147c478bd9Sstevel@tonic-gate 	tapepos--;
54157c478bd9Sstevel@tonic-gate }
54167c478bd9Sstevel@tonic-gate 
54177c478bd9Sstevel@tonic-gate 
54187c478bd9Sstevel@tonic-gate /*
54197c478bd9Sstevel@tonic-gate  *	flushtape  write buffered block(s) onto tape
54207c478bd9Sstevel@tonic-gate  *
54217c478bd9Sstevel@tonic-gate  *      recno points to next free block in tbuf.  If nonzero, a write is done.
54227c478bd9Sstevel@tonic-gate  *	Care is taken to write in multiples of SYS_BLOCK when device is
54237c478bd9Sstevel@tonic-gate  *	non-magtape in case raw i/o is used.
54247c478bd9Sstevel@tonic-gate  *
54257c478bd9Sstevel@tonic-gate  *	NOTE: this is called by writetape() to do the actual writing
54267c478bd9Sstevel@tonic-gate  */
54277c478bd9Sstevel@tonic-gate 
54287c478bd9Sstevel@tonic-gate static void
54297c478bd9Sstevel@tonic-gate flushtape(void)
54307c478bd9Sstevel@tonic-gate {
54317c478bd9Sstevel@tonic-gate #ifdef DEBUG
54327c478bd9Sstevel@tonic-gate 	DEBUG("flushtape() called, recno=%" FMT_blkcnt_t "\n", recno, 0);
54337c478bd9Sstevel@tonic-gate #endif
54347c478bd9Sstevel@tonic-gate 	if (recno > 0) {	/* anything buffered? */
54357c478bd9Sstevel@tonic-gate 		if (NotTape) {
54367c478bd9Sstevel@tonic-gate #if SYS_BLOCK > TBLOCK
54377c478bd9Sstevel@tonic-gate 			int i;
54387c478bd9Sstevel@tonic-gate 
54397c478bd9Sstevel@tonic-gate 			/*
54407c478bd9Sstevel@tonic-gate 			 * an odd-block write can only happen when
54417c478bd9Sstevel@tonic-gate 			 * we are at the end of a volume that is not a tape.
54427c478bd9Sstevel@tonic-gate 			 * Here we round recno up to an even SYS_BLOCK
54437c478bd9Sstevel@tonic-gate 			 * boundary.
54447c478bd9Sstevel@tonic-gate 			 */
54457c478bd9Sstevel@tonic-gate 			if ((i = recno % (SYS_BLOCK / TBLOCK)) != 0) {
54467c478bd9Sstevel@tonic-gate #ifdef DEBUG
54477c478bd9Sstevel@tonic-gate 				DEBUG("flushtape() %d rounding blocks\n", i, 0);
54487c478bd9Sstevel@tonic-gate #endif
54497c478bd9Sstevel@tonic-gate 				recno += i;	/* round up to even SYS_BLOCK */
54507c478bd9Sstevel@tonic-gate 			}
54517c478bd9Sstevel@tonic-gate #endif
54527c478bd9Sstevel@tonic-gate 			if (recno > nblock)
54537c478bd9Sstevel@tonic-gate 				recno = nblock;
54547c478bd9Sstevel@tonic-gate 		}
54557c478bd9Sstevel@tonic-gate #ifdef DEBUG
54567c478bd9Sstevel@tonic-gate 		DEBUG("writing out %" FMT_blkcnt_t " blocks of %" FMT_blkcnt_t
54577c478bd9Sstevel@tonic-gate 		    " bytes\n", (blkcnt_t)(NotTape ? recno : nblock),
54587c478bd9Sstevel@tonic-gate 		    (blkcnt_t)(NotTape ? recno : nblock) * TBLOCK);
54597c478bd9Sstevel@tonic-gate #endif
54607c478bd9Sstevel@tonic-gate 		if (write(mt, tbuf,
54617c478bd9Sstevel@tonic-gate 		    (size_t)(NotTape ? recno : nblock) * TBLOCK) < 0) {
54627c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
54637c478bd9Sstevel@tonic-gate 			    "tar: tape write error\n"));
54647c478bd9Sstevel@tonic-gate 			done(2);
54657c478bd9Sstevel@tonic-gate 		}
54667c478bd9Sstevel@tonic-gate 		recno = 0;
54677c478bd9Sstevel@tonic-gate 	}
54687c478bd9Sstevel@tonic-gate }
54697c478bd9Sstevel@tonic-gate 
54707c478bd9Sstevel@tonic-gate static void
54717c478bd9Sstevel@tonic-gate copy(void *dst, void *src)
54727c478bd9Sstevel@tonic-gate {
54737c478bd9Sstevel@tonic-gate 	(void) memcpy(dst, src, TBLOCK);
54747c478bd9Sstevel@tonic-gate }
54757c478bd9Sstevel@tonic-gate 
54767c478bd9Sstevel@tonic-gate #ifdef	_iBCS2
54777c478bd9Sstevel@tonic-gate /*
54787c478bd9Sstevel@tonic-gate  *	initarg -- initialize things for nextarg.
54797c478bd9Sstevel@tonic-gate  *
54807c478bd9Sstevel@tonic-gate  *	argv		filename list, a la argv.
54817c478bd9Sstevel@tonic-gate  *	filefile	name of file containing filenames.  Unless doing
54827c478bd9Sstevel@tonic-gate  *		a create, seeks must be allowable (e.g. no named pipes).
54837c478bd9Sstevel@tonic-gate  *
54847c478bd9Sstevel@tonic-gate  *	- if filefile is non-NULL, it will be used first, and argv will
54857c478bd9Sstevel@tonic-gate  *	be used when the data in filefile are exhausted.
54867c478bd9Sstevel@tonic-gate  *	- otherwise argv will be used.
54877c478bd9Sstevel@tonic-gate  */
54887c478bd9Sstevel@tonic-gate static char **Cmdargv = NULL;
54897c478bd9Sstevel@tonic-gate static FILE *FILEFile = NULL;
54907c478bd9Sstevel@tonic-gate static long seekFile = -1;
54917c478bd9Sstevel@tonic-gate static char *ptrtoFile, *begofFile, *endofFile;
54927c478bd9Sstevel@tonic-gate 
54937c478bd9Sstevel@tonic-gate static	void
54947c478bd9Sstevel@tonic-gate initarg(char *argv[], char *filefile)
54957c478bd9Sstevel@tonic-gate {
54967c478bd9Sstevel@tonic-gate 	struct stat statbuf;
54977c478bd9Sstevel@tonic-gate 	char *p;
54987c478bd9Sstevel@tonic-gate 	int nbytes;
54997c478bd9Sstevel@tonic-gate 
55007c478bd9Sstevel@tonic-gate 	Cmdargv = argv;
55017c478bd9Sstevel@tonic-gate 	if (filefile == NULL)
55027c478bd9Sstevel@tonic-gate 		return;		/* no -F file */
55037c478bd9Sstevel@tonic-gate 	if (FILEFile != NULL) {
55047c478bd9Sstevel@tonic-gate 		/*
55057c478bd9Sstevel@tonic-gate 		 * need to REinitialize
55067c478bd9Sstevel@tonic-gate 		 */
55077c478bd9Sstevel@tonic-gate 		if (seekFile != -1)
55087c478bd9Sstevel@tonic-gate 			(void) fseek(FILEFile, seekFile, 0);
55097c478bd9Sstevel@tonic-gate 		ptrtoFile = begofFile;
55107c478bd9Sstevel@tonic-gate 		return;
55117c478bd9Sstevel@tonic-gate 	}
55127c478bd9Sstevel@tonic-gate 	/*
55137c478bd9Sstevel@tonic-gate 	 * first time initialization
55147c478bd9Sstevel@tonic-gate 	 */
55157c478bd9Sstevel@tonic-gate 	if ((FILEFile = fopen(filefile, "r")) == NULL)
55167c478bd9Sstevel@tonic-gate 		fatal(gettext("cannot open (%s)"), filefile);
55177c478bd9Sstevel@tonic-gate 	(void) fstat(fileno(FILEFile), &statbuf);
55187c478bd9Sstevel@tonic-gate 	if ((statbuf.st_mode & S_IFMT) != S_IFREG) {
55197c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
55207c478bd9Sstevel@tonic-gate 		    "tar: %s is not a regular file\n"), filefile);
55217c478bd9Sstevel@tonic-gate 		(void) fclose(FILEFile);
55227c478bd9Sstevel@tonic-gate 		done(1);
55237c478bd9Sstevel@tonic-gate 	}
55247c478bd9Sstevel@tonic-gate 	ptrtoFile = begofFile = endofFile;
55257c478bd9Sstevel@tonic-gate 	seekFile = 0;
55267c478bd9Sstevel@tonic-gate 	if (!xflag)
55277c478bd9Sstevel@tonic-gate 		return;		/* the file will be read only once anyway */
55287c478bd9Sstevel@tonic-gate 	nbytes = statbuf.st_size;
55297c478bd9Sstevel@tonic-gate 	while ((begofFile = calloc(nbytes, sizeof (char))) == NULL)
55307c478bd9Sstevel@tonic-gate 		nbytes -= 20;
55317c478bd9Sstevel@tonic-gate 	if (nbytes < 50) {
55327c478bd9Sstevel@tonic-gate 		free(begofFile);
55337c478bd9Sstevel@tonic-gate 		begofFile = endofFile;
55347c478bd9Sstevel@tonic-gate 		return;		/* no room so just do plain reads */
55357c478bd9Sstevel@tonic-gate 	}
55367c478bd9Sstevel@tonic-gate 	if (fread(begofFile, 1, nbytes, FILEFile) != nbytes)
55377c478bd9Sstevel@tonic-gate 		fatal(gettext("could not read %s"), filefile);
55387c478bd9Sstevel@tonic-gate 	ptrtoFile = begofFile;
55397c478bd9Sstevel@tonic-gate 	endofFile = begofFile + nbytes;
55407c478bd9Sstevel@tonic-gate 	for (p = begofFile; p < endofFile; ++p)
55417c478bd9Sstevel@tonic-gate 		if (*p == '\n')
55427c478bd9Sstevel@tonic-gate 			*p = '\0';
55437c478bd9Sstevel@tonic-gate 	if (nbytes != statbuf.st_size)
55447c478bd9Sstevel@tonic-gate 		seekFile = nbytes + 1;
55457c478bd9Sstevel@tonic-gate 	else
55467c478bd9Sstevel@tonic-gate 		(void) fclose(FILEFile);
55477c478bd9Sstevel@tonic-gate }
55487c478bd9Sstevel@tonic-gate 
55497c478bd9Sstevel@tonic-gate /*
55507c478bd9Sstevel@tonic-gate  *	nextarg -- get next argument of arglist.
55517c478bd9Sstevel@tonic-gate  *
55527c478bd9Sstevel@tonic-gate  *	The argument is taken from wherever is appropriate.
55537c478bd9Sstevel@tonic-gate  *
55547c478bd9Sstevel@tonic-gate  *	If the 'F file' function modifier has been specified, the argument
55557c478bd9Sstevel@tonic-gate  *	will be taken from the file, unless EOF has been reached.
55567c478bd9Sstevel@tonic-gate  *	Otherwise the argument will be taken from argv.
55577c478bd9Sstevel@tonic-gate  *
55587c478bd9Sstevel@tonic-gate  *	WARNING:
55597c478bd9Sstevel@tonic-gate  *	  Return value may point to static data, whose contents are over-
55607c478bd9Sstevel@tonic-gate  *	  written on each call.
55617c478bd9Sstevel@tonic-gate  */
55627c478bd9Sstevel@tonic-gate static	char  *
55637c478bd9Sstevel@tonic-gate nextarg(void)
55647c478bd9Sstevel@tonic-gate {
55657c478bd9Sstevel@tonic-gate 	static char nameFile[PATH_MAX + 1];
55667c478bd9Sstevel@tonic-gate 	int n;
55677c478bd9Sstevel@tonic-gate 	char *p;
55687c478bd9Sstevel@tonic-gate 
55697c478bd9Sstevel@tonic-gate 	if (FILEFile) {
55707c478bd9Sstevel@tonic-gate 		if (ptrtoFile < endofFile) {
55717c478bd9Sstevel@tonic-gate 			p = ptrtoFile;
55727c478bd9Sstevel@tonic-gate 			while (*ptrtoFile)
55737c478bd9Sstevel@tonic-gate 				++ptrtoFile;
55747c478bd9Sstevel@tonic-gate 			++ptrtoFile;
55757c478bd9Sstevel@tonic-gate 			return (p);
55767c478bd9Sstevel@tonic-gate 		}
55777c478bd9Sstevel@tonic-gate 		if (fgets(nameFile, PATH_MAX + 1, FILEFile) != NULL) {
55787c478bd9Sstevel@tonic-gate 			n = strlen(nameFile);
55797c478bd9Sstevel@tonic-gate 			if (n > 0 && nameFile[n-1] == '\n')
55807c478bd9Sstevel@tonic-gate 				nameFile[n-1] = '\0';
55817c478bd9Sstevel@tonic-gate 			return (nameFile);
55827c478bd9Sstevel@tonic-gate 		}
55837c478bd9Sstevel@tonic-gate 	}
55847c478bd9Sstevel@tonic-gate 	return (*Cmdargv++);
55857c478bd9Sstevel@tonic-gate }
55867c478bd9Sstevel@tonic-gate #endif	/*  _iBCS2 */
55877c478bd9Sstevel@tonic-gate 
55887c478bd9Sstevel@tonic-gate /*
55897c478bd9Sstevel@tonic-gate  * kcheck()
55907c478bd9Sstevel@tonic-gate  *	- checks the validity of size values for non-tape devices
55917c478bd9Sstevel@tonic-gate  *	- if size is zero, mulvol tar is disabled and size is
55927c478bd9Sstevel@tonic-gate  *	  assumed to be infinite.
55937c478bd9Sstevel@tonic-gate  *	- returns volume size in TBLOCKS
55947c478bd9Sstevel@tonic-gate  */
55957c478bd9Sstevel@tonic-gate 
55967c478bd9Sstevel@tonic-gate static blkcnt_t
55977c478bd9Sstevel@tonic-gate kcheck(char *kstr)
55987c478bd9Sstevel@tonic-gate {
55997c478bd9Sstevel@tonic-gate 	blkcnt_t kval;
56007c478bd9Sstevel@tonic-gate 
56017c478bd9Sstevel@tonic-gate 	kval = strtoll(kstr, NULL, 0);
56027c478bd9Sstevel@tonic-gate 	if (kval == (blkcnt_t)0) {	/* no multi-volume; size is infinity. */
56037c478bd9Sstevel@tonic-gate 		mulvol = 0;	/* definitely not mulvol, but we must  */
56047c478bd9Sstevel@tonic-gate 		return (0);	/* took out setting of NotTape */
56057c478bd9Sstevel@tonic-gate 	}
56067c478bd9Sstevel@tonic-gate 	if (kval < (blkcnt_t)MINSIZE) {
56077c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
56087c478bd9Sstevel@tonic-gate 		    "tar: sizes below %luK not supported (%" FMT_blkcnt_t
56097c478bd9Sstevel@tonic-gate 		    ").\n"), (ulong_t)MINSIZE, kval);
56107c478bd9Sstevel@tonic-gate 		if (!kflag)
56117c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
56127c478bd9Sstevel@tonic-gate 			    "bad size entry for %s in %s.\n"),
56137c478bd9Sstevel@tonic-gate 			    archive, DEF_FILE);
56147c478bd9Sstevel@tonic-gate 		done(1);
56157c478bd9Sstevel@tonic-gate 	}
56167c478bd9Sstevel@tonic-gate 	mulvol++;
56177c478bd9Sstevel@tonic-gate 	NotTape++;			/* implies non-tape */
56187c478bd9Sstevel@tonic-gate 	return (kval * 1024 / TBLOCK);	/* convert to TBLOCKS */
56197c478bd9Sstevel@tonic-gate }
56207c478bd9Sstevel@tonic-gate 
56217c478bd9Sstevel@tonic-gate 
56227c478bd9Sstevel@tonic-gate /*
56237c478bd9Sstevel@tonic-gate  * bcheck()
56247c478bd9Sstevel@tonic-gate  *	- checks the validity of blocking factors
56257c478bd9Sstevel@tonic-gate  *	- returns blocking factor
56267c478bd9Sstevel@tonic-gate  */
56277c478bd9Sstevel@tonic-gate 
56287c478bd9Sstevel@tonic-gate static int
56297c478bd9Sstevel@tonic-gate bcheck(char *bstr)
56307c478bd9Sstevel@tonic-gate {
56317c478bd9Sstevel@tonic-gate 	blkcnt_t bval;
56327c478bd9Sstevel@tonic-gate 
56337c478bd9Sstevel@tonic-gate 	bval = strtoll(bstr, NULL, 0);
56347c478bd9Sstevel@tonic-gate 	if ((bval <= 0) || (bval > INT_MAX / TBLOCK)) {
56357c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
56367c478bd9Sstevel@tonic-gate 		    "tar: invalid blocksize \"%s\".\n"), bstr);
56377c478bd9Sstevel@tonic-gate 		if (!bflag)
56387c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
56397c478bd9Sstevel@tonic-gate 			    "bad blocksize entry for '%s' in %s.\n"),
56407c478bd9Sstevel@tonic-gate 			    archive, DEF_FILE);
56417c478bd9Sstevel@tonic-gate 		done(1);
56427c478bd9Sstevel@tonic-gate 	}
56437c478bd9Sstevel@tonic-gate 
56447c478bd9Sstevel@tonic-gate 	return ((int)bval);
56457c478bd9Sstevel@tonic-gate }
56467c478bd9Sstevel@tonic-gate 
56477c478bd9Sstevel@tonic-gate 
56487c478bd9Sstevel@tonic-gate /*
56497c478bd9Sstevel@tonic-gate  * defset()
56507c478bd9Sstevel@tonic-gate  *	- reads DEF_FILE for the set of default values specified.
56517c478bd9Sstevel@tonic-gate  *	- initializes 'usefile', 'nblock', and 'blocklim', and 'NotTape'.
56527c478bd9Sstevel@tonic-gate  *	- 'usefile' points to static data, so will be overwritten
56537c478bd9Sstevel@tonic-gate  *	  if this routine is called a second time.
56547c478bd9Sstevel@tonic-gate  *	- the pattern specified by 'arch' must be followed by four
56557c478bd9Sstevel@tonic-gate  *	  blank-separated fields (1) device (2) blocking,
56567c478bd9Sstevel@tonic-gate  *				 (3) size(K), and (4) tape
56577c478bd9Sstevel@tonic-gate  *	  for example: archive0=/dev/fd 1 400 n
56587c478bd9Sstevel@tonic-gate  */
56597c478bd9Sstevel@tonic-gate 
56607c478bd9Sstevel@tonic-gate static int
56617c478bd9Sstevel@tonic-gate defset(char *arch)
56627c478bd9Sstevel@tonic-gate {
56637c478bd9Sstevel@tonic-gate 	char *bp;
56647c478bd9Sstevel@tonic-gate 
56657c478bd9Sstevel@tonic-gate 	if (defopen(DEF_FILE) != 0)
56667c478bd9Sstevel@tonic-gate 		return (FALSE);
56677c478bd9Sstevel@tonic-gate 	if (defcntl(DC_SETFLAGS, (DC_STD & ~(DC_CASE))) == -1) {
56687c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
56697c478bd9Sstevel@tonic-gate 		    "tar: error setting parameters for %s.\n"), DEF_FILE);
56707c478bd9Sstevel@tonic-gate 		return (FALSE);			/* & following ones too */
56717c478bd9Sstevel@tonic-gate 	}
56727c478bd9Sstevel@tonic-gate 	if ((bp = defread(arch)) == NULL) {
56737c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
56747c478bd9Sstevel@tonic-gate 		    "tar: missing or invalid '%s' entry in %s.\n"),
56757c478bd9Sstevel@tonic-gate 		    arch, DEF_FILE);
56767c478bd9Sstevel@tonic-gate 		return (FALSE);
56777c478bd9Sstevel@tonic-gate 	}
56787c478bd9Sstevel@tonic-gate 	if ((usefile = strtok(bp, " \t")) == NULL) {
56797c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
56807c478bd9Sstevel@tonic-gate 		    "tar: '%s' entry in %s is empty!\n"), arch, DEF_FILE);
56817c478bd9Sstevel@tonic-gate 		return (FALSE);
56827c478bd9Sstevel@tonic-gate 	}
56837c478bd9Sstevel@tonic-gate 	if ((bp = strtok(NULL, " \t")) == NULL) {
56847c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
56857c478bd9Sstevel@tonic-gate 		    "tar: block component missing in '%s' entry in %s.\n"),
56867c478bd9Sstevel@tonic-gate 		    arch, DEF_FILE);
56877c478bd9Sstevel@tonic-gate 		return (FALSE);
56887c478bd9Sstevel@tonic-gate 	}
56897c478bd9Sstevel@tonic-gate 	nblock = bcheck(bp);
56907c478bd9Sstevel@tonic-gate 	if ((bp = strtok(NULL, " \t")) == NULL) {
56917c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
56927c478bd9Sstevel@tonic-gate 		    "tar: size component missing in '%s' entry in %s.\n"),
56937c478bd9Sstevel@tonic-gate 		    arch, DEF_FILE);
56947c478bd9Sstevel@tonic-gate 		return (FALSE);
56957c478bd9Sstevel@tonic-gate 	}
56967c478bd9Sstevel@tonic-gate 	blocklim = kcheck(bp);
56977c478bd9Sstevel@tonic-gate 	if ((bp = strtok(NULL, " \t")) != NULL)
56987c478bd9Sstevel@tonic-gate 		NotTape = (*bp == 'n' || *bp == 'N');
56997c478bd9Sstevel@tonic-gate 	else
57007c478bd9Sstevel@tonic-gate 		NotTape = (blocklim != 0);
57017c478bd9Sstevel@tonic-gate 	(void) defopen(NULL);
57027c478bd9Sstevel@tonic-gate #ifdef DEBUG
57037c478bd9Sstevel@tonic-gate 	DEBUG("defset: archive='%s'; usefile='%s'\n", arch, usefile);
57047c478bd9Sstevel@tonic-gate 	DEBUG("defset: nblock='%d'; blocklim='%" FMT_blkcnt_t "'\n",
57057c478bd9Sstevel@tonic-gate 	    nblock, blocklim);
57067c478bd9Sstevel@tonic-gate 	DEBUG("defset: not tape = %d\n", NotTape, 0);
57077c478bd9Sstevel@tonic-gate #endif
57087c478bd9Sstevel@tonic-gate 	return (TRUE);
57097c478bd9Sstevel@tonic-gate }
57107c478bd9Sstevel@tonic-gate 
57117c478bd9Sstevel@tonic-gate 
57127c478bd9Sstevel@tonic-gate /*
57137c478bd9Sstevel@tonic-gate  * Following code handles excluded and included files.
57147c478bd9Sstevel@tonic-gate  * A hash table of file names to be {in,ex}cluded is built.
57157c478bd9Sstevel@tonic-gate  * For excluded files, before writing or extracting a file
57167c478bd9Sstevel@tonic-gate  * check to see if it is in the exclude_tbl.
57177c478bd9Sstevel@tonic-gate  * For included files, the wantit() procedure will check to
57187c478bd9Sstevel@tonic-gate  * see if the named file is in the include_tbl.
57197c478bd9Sstevel@tonic-gate  */
57207c478bd9Sstevel@tonic-gate 
57217c478bd9Sstevel@tonic-gate static void
57225e2174acSceastha build_table(file_list_t *table[], char *file)
57237c478bd9Sstevel@tonic-gate {
57247c478bd9Sstevel@tonic-gate 	FILE	*fp;
57257c478bd9Sstevel@tonic-gate 	char	buf[PATH_MAX + 1];
57267c478bd9Sstevel@tonic-gate 
57277c478bd9Sstevel@tonic-gate 	if ((fp = fopen(file, "r")) == (FILE *)NULL)
57287c478bd9Sstevel@tonic-gate 		vperror(1, gettext("could not open %s"), file);
57297c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
57307c478bd9Sstevel@tonic-gate 		if (buf[strlen(buf) - 1] == '\n')
57317c478bd9Sstevel@tonic-gate 			buf[strlen(buf) - 1] = '\0';
57327c478bd9Sstevel@tonic-gate 		/* Only add to table if line has something in it */
57337c478bd9Sstevel@tonic-gate 		if (strspn(buf, " \t") != strlen(buf))
57347c478bd9Sstevel@tonic-gate 			add_file_to_table(table, buf);
57357c478bd9Sstevel@tonic-gate 	}
57367c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
57377c478bd9Sstevel@tonic-gate }
57387c478bd9Sstevel@tonic-gate 
57397c478bd9Sstevel@tonic-gate 
57407c478bd9Sstevel@tonic-gate /*
57417c478bd9Sstevel@tonic-gate  * Add a file name to the the specified table, if the file name has any
57427c478bd9Sstevel@tonic-gate  * trailing '/'s then delete them before inserting into the table
57437c478bd9Sstevel@tonic-gate  */
57447c478bd9Sstevel@tonic-gate 
57457c478bd9Sstevel@tonic-gate static void
57465e2174acSceastha add_file_to_table(file_list_t *table[], char *str)
57477c478bd9Sstevel@tonic-gate {
57487c478bd9Sstevel@tonic-gate 	char	name[PATH_MAX + 1];
57497c478bd9Sstevel@tonic-gate 	unsigned int h;
57505e2174acSceastha 	file_list_t	*exp;
57517c478bd9Sstevel@tonic-gate 
57527c478bd9Sstevel@tonic-gate 	(void) strcpy(name, str);
57537c478bd9Sstevel@tonic-gate 	while (name[strlen(name) - 1] == '/') {
57547c478bd9Sstevel@tonic-gate 		name[strlen(name) - 1] = NULL;
57557c478bd9Sstevel@tonic-gate 	}
57567c478bd9Sstevel@tonic-gate 
57577c478bd9Sstevel@tonic-gate 	h = hash(name);
57585e2174acSceastha 	if ((exp = (file_list_t *)calloc(sizeof (file_list_t),
57597c478bd9Sstevel@tonic-gate 	    sizeof (char))) == NULL) {
57607c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
57617c478bd9Sstevel@tonic-gate 		    "tar: out of memory, exclude/include table(entry)\n"));
57627c478bd9Sstevel@tonic-gate 		exit(1);
57637c478bd9Sstevel@tonic-gate 	}
57647c478bd9Sstevel@tonic-gate 
57657c478bd9Sstevel@tonic-gate 	if ((exp->name = strdup(name)) == NULL) {
57667c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
57677c478bd9Sstevel@tonic-gate 		    "tar: out of memory, exclude/include table(file name)\n"));
57687c478bd9Sstevel@tonic-gate 		exit(1);
57697c478bd9Sstevel@tonic-gate 	}
57707c478bd9Sstevel@tonic-gate 
57717c478bd9Sstevel@tonic-gate 	exp->next = table[h];
57727c478bd9Sstevel@tonic-gate 	table[h] = exp;
57737c478bd9Sstevel@tonic-gate }
57747c478bd9Sstevel@tonic-gate 
57757c478bd9Sstevel@tonic-gate 
57767c478bd9Sstevel@tonic-gate /*
57777c478bd9Sstevel@tonic-gate  * See if a file name or any of the file's parent directories is in the
57787c478bd9Sstevel@tonic-gate  * specified table, if the file name has any trailing '/'s then delete
57797c478bd9Sstevel@tonic-gate  * them before searching the table
57807c478bd9Sstevel@tonic-gate  */
57817c478bd9Sstevel@tonic-gate 
57827c478bd9Sstevel@tonic-gate static int
57835e2174acSceastha is_in_table(file_list_t *table[], char *str)
57847c478bd9Sstevel@tonic-gate {
57857c478bd9Sstevel@tonic-gate 	char	name[PATH_MAX + 1];
57867c478bd9Sstevel@tonic-gate 	unsigned int	h;
57875e2174acSceastha 	file_list_t	*exp;
57887c478bd9Sstevel@tonic-gate 	char	*ptr;
57897c478bd9Sstevel@tonic-gate 
57907c478bd9Sstevel@tonic-gate 	(void) strcpy(name, str);
57917c478bd9Sstevel@tonic-gate 	while (name[strlen(name) - 1] == '/') {
57927c478bd9Sstevel@tonic-gate 		name[strlen(name) - 1] = NULL;
57937c478bd9Sstevel@tonic-gate 	}
57947c478bd9Sstevel@tonic-gate 
57957c478bd9Sstevel@tonic-gate 	/*
57967c478bd9Sstevel@tonic-gate 	 * check for the file name in the passed list
57977c478bd9Sstevel@tonic-gate 	 */
57987c478bd9Sstevel@tonic-gate 	h = hash(name);
57997c478bd9Sstevel@tonic-gate 	exp = table[h];
58007c478bd9Sstevel@tonic-gate 	while (exp != NULL) {
58017c478bd9Sstevel@tonic-gate 		if (strcmp(name, exp->name) == 0) {
58027c478bd9Sstevel@tonic-gate 			return (1);
58037c478bd9Sstevel@tonic-gate 		}
58047c478bd9Sstevel@tonic-gate 		exp = exp->next;
58057c478bd9Sstevel@tonic-gate 	}
58067c478bd9Sstevel@tonic-gate 
58077c478bd9Sstevel@tonic-gate 	/*
58087c478bd9Sstevel@tonic-gate 	 * check for any parent directories in the file list
58097c478bd9Sstevel@tonic-gate 	 */
58107c478bd9Sstevel@tonic-gate 	while ((ptr = strrchr(name, '/'))) {
58117c478bd9Sstevel@tonic-gate 		*ptr = NULL;
58127c478bd9Sstevel@tonic-gate 		h = hash(name);
58137c478bd9Sstevel@tonic-gate 		exp = table[h];
58147c478bd9Sstevel@tonic-gate 		while (exp != NULL) {
58157c478bd9Sstevel@tonic-gate 			if (strcmp(name, exp->name) == 0) {
58167c478bd9Sstevel@tonic-gate 				return (1);
58177c478bd9Sstevel@tonic-gate 			}
58187c478bd9Sstevel@tonic-gate 			exp = exp->next;
58197c478bd9Sstevel@tonic-gate 		}
58207c478bd9Sstevel@tonic-gate 	}
58217c478bd9Sstevel@tonic-gate 
58227c478bd9Sstevel@tonic-gate 	return (0);
58237c478bd9Sstevel@tonic-gate }
58247c478bd9Sstevel@tonic-gate 
58257c478bd9Sstevel@tonic-gate 
58267c478bd9Sstevel@tonic-gate /*
58277c478bd9Sstevel@tonic-gate  * Compute a hash from a string.
58287c478bd9Sstevel@tonic-gate  */
58297c478bd9Sstevel@tonic-gate 
58307c478bd9Sstevel@tonic-gate static unsigned int
58317c478bd9Sstevel@tonic-gate hash(char *str)
58327c478bd9Sstevel@tonic-gate {
58337c478bd9Sstevel@tonic-gate 	char	*cp;
58347c478bd9Sstevel@tonic-gate 	unsigned int	h;
58357c478bd9Sstevel@tonic-gate 
58367c478bd9Sstevel@tonic-gate 	h = 0;
58377c478bd9Sstevel@tonic-gate 	for (cp = str; *cp; cp++) {
58387c478bd9Sstevel@tonic-gate 		h += *cp;
58397c478bd9Sstevel@tonic-gate 	}
58407c478bd9Sstevel@tonic-gate 	return (h % TABLE_SIZE);
58417c478bd9Sstevel@tonic-gate }
58427c478bd9Sstevel@tonic-gate 
58437c478bd9Sstevel@tonic-gate static	void *
58447c478bd9Sstevel@tonic-gate getmem(size_t size)
58457c478bd9Sstevel@tonic-gate {
58467c478bd9Sstevel@tonic-gate 	void *p = calloc((unsigned)size, sizeof (char));
58477c478bd9Sstevel@tonic-gate 
58487c478bd9Sstevel@tonic-gate 	if (p == NULL && freemem) {
58497c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
58507c478bd9Sstevel@tonic-gate 		    "tar: out of memory, link and directory modtime "
58517c478bd9Sstevel@tonic-gate 		    "info lost\n"));
58527c478bd9Sstevel@tonic-gate 		freemem = 0;
58537c478bd9Sstevel@tonic-gate 		if (errflag)
58547c478bd9Sstevel@tonic-gate 			done(1);
58557c478bd9Sstevel@tonic-gate 		else
58567c478bd9Sstevel@tonic-gate 			Errflg = 1;
58577c478bd9Sstevel@tonic-gate 	}
58587c478bd9Sstevel@tonic-gate 	return (p);
58597c478bd9Sstevel@tonic-gate }
58607c478bd9Sstevel@tonic-gate 
58617c478bd9Sstevel@tonic-gate /*
58627c478bd9Sstevel@tonic-gate  * vperror() --variable argument perror.
58637c478bd9Sstevel@tonic-gate  * Takes 3 args: exit_status, formats, args.  If exit_status is 0, then
58647c478bd9Sstevel@tonic-gate  * the errflag (exit on error) is checked -- if it is non-zero, tar exits
58657c478bd9Sstevel@tonic-gate  * with the value of whatever "errno" is set to.  If exit_status is not
58667c478bd9Sstevel@tonic-gate  * zero, then tar exits with that error status. If errflag and exit_status
58677c478bd9Sstevel@tonic-gate  * are both zero, the routine returns to where it was called and sets Errflg
58687c478bd9Sstevel@tonic-gate  * to errno.
58697c478bd9Sstevel@tonic-gate  */
58707c478bd9Sstevel@tonic-gate 
58717c478bd9Sstevel@tonic-gate static void
58727c478bd9Sstevel@tonic-gate vperror(int exit_status, char *fmt, ...)
58737c478bd9Sstevel@tonic-gate {
58747c478bd9Sstevel@tonic-gate 	va_list	ap;
58757c478bd9Sstevel@tonic-gate 
58767c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
58777c478bd9Sstevel@tonic-gate 	(void) fputs("tar: ", stderr);
58787c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
58797c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, ": %s\n", strerror(errno));
58807c478bd9Sstevel@tonic-gate 	va_end(ap);
58817c478bd9Sstevel@tonic-gate 	if (exit_status)
58827c478bd9Sstevel@tonic-gate 		done(exit_status);
58837c478bd9Sstevel@tonic-gate 	else
58847c478bd9Sstevel@tonic-gate 		if (errflag)
58857c478bd9Sstevel@tonic-gate 			done(errno);
58867c478bd9Sstevel@tonic-gate 		else
58877c478bd9Sstevel@tonic-gate 			Errflg = errno;
58887c478bd9Sstevel@tonic-gate }
58897c478bd9Sstevel@tonic-gate 
58907c478bd9Sstevel@tonic-gate 
58917c478bd9Sstevel@tonic-gate static void
58927c478bd9Sstevel@tonic-gate fatal(char *format, ...)
58937c478bd9Sstevel@tonic-gate {
58947c478bd9Sstevel@tonic-gate 	va_list	ap;
58957c478bd9Sstevel@tonic-gate 
58967c478bd9Sstevel@tonic-gate 	va_start(ap, format);
58977c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "tar: ");
58987c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
58997c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
59007c478bd9Sstevel@tonic-gate 	va_end(ap);
59017c478bd9Sstevel@tonic-gate 	done(1);
59027c478bd9Sstevel@tonic-gate }
59037c478bd9Sstevel@tonic-gate 
59047c478bd9Sstevel@tonic-gate 
59057c478bd9Sstevel@tonic-gate /*
59067c478bd9Sstevel@tonic-gate  * Check to make sure that argument is a char * ptr.
59077c478bd9Sstevel@tonic-gate  * Actually, we just check to see that it is non-null.
59087c478bd9Sstevel@tonic-gate  * If it is null, print out the message and call usage(), bailing out.
59097c478bd9Sstevel@tonic-gate  */
59107c478bd9Sstevel@tonic-gate 
59117c478bd9Sstevel@tonic-gate static void
59127c478bd9Sstevel@tonic-gate assert_string(char *s, char *msg)
59137c478bd9Sstevel@tonic-gate {
59147c478bd9Sstevel@tonic-gate 	if (s == NULL) {
59157c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, msg);
59167c478bd9Sstevel@tonic-gate 		usage();
59177c478bd9Sstevel@tonic-gate 	}
59187c478bd9Sstevel@tonic-gate }
59197c478bd9Sstevel@tonic-gate 
59207c478bd9Sstevel@tonic-gate 
59217c478bd9Sstevel@tonic-gate static void
59227c478bd9Sstevel@tonic-gate mterr(char *operation, int i, int exitcode)
59237c478bd9Sstevel@tonic-gate {
59247c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
59257c478bd9Sstevel@tonic-gate 	    "tar: %s error: "), operation);
59267c478bd9Sstevel@tonic-gate 	if (i < 0)
59277c478bd9Sstevel@tonic-gate 		perror("");
59287c478bd9Sstevel@tonic-gate 	else
59297c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("unexpected EOF\n"));
59307c478bd9Sstevel@tonic-gate 	done(exitcode);
59317c478bd9Sstevel@tonic-gate }
59327c478bd9Sstevel@tonic-gate 
59337c478bd9Sstevel@tonic-gate static int
5934da6c28aaSamw wantit(char *argv[], char **namep, char **dirp, char **component,
5935da6c28aaSamw     attr_data_t **attrinfo)
59367c478bd9Sstevel@tonic-gate {
59377c478bd9Sstevel@tonic-gate 	char **cp;
59387c478bd9Sstevel@tonic-gate 	int gotit;		/* true if we've found a match */
5939123523f8Sas158974 	int ret;
59407c478bd9Sstevel@tonic-gate 
59417c478bd9Sstevel@tonic-gate top:
5942123523f8Sas158974 	if (xhdr_flgs & _X_XHDR) {
59437c478bd9Sstevel@tonic-gate 		xhdr_flgs = 0;
5944123523f8Sas158974 	}
59457c478bd9Sstevel@tonic-gate 	getdir();
59467c478bd9Sstevel@tonic-gate 	if (Xhdrflag > 0) {
5947123523f8Sas158974 		ret = get_xdata();
5948123523f8Sas158974 		if (ret != 0) {	/* Xhdr items and regular header */
5949123523f8Sas158974 			setbytes_to_skip(&stbuf, ret);
59507c478bd9Sstevel@tonic-gate 			passtape();
59517c478bd9Sstevel@tonic-gate 			return (0);	/* Error--don't want to extract  */
59527c478bd9Sstevel@tonic-gate 		}
59537c478bd9Sstevel@tonic-gate 	}
59547c478bd9Sstevel@tonic-gate 
5955123523f8Sas158974 	/*
5956123523f8Sas158974 	 * If typeflag is not 'A' and xhdr_flgs is set, then processing
5957123523f8Sas158974 	 * of ancillary file is either over or ancillary file
5958123523f8Sas158974 	 * processing is not required, load info from Xtarhdr and set
5959123523f8Sas158974 	 * _X_XHDR bit in xhdr_flgs.
5960123523f8Sas158974 	 */
5961123523f8Sas158974 	if ((dblock.dbuf.typeflag != 'A') && (xhdr_flgs != 0)) {
5962123523f8Sas158974 		load_info_from_xtarhdr(xhdr_flgs, &Xtarhdr);
5963123523f8Sas158974 		xhdr_flgs |= _X_XHDR;
5964123523f8Sas158974 	}
5965123523f8Sas158974 
59667c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
59677c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.typeflag == _XATTR_HDRTYPE && xattrbadhead == 0) {
5968da6c28aaSamw 		/*
5969da6c28aaSamw 		 * Always needs to read the extended header.  If atflag, saflag,
5970da6c28aaSamw 		 * or tflag isn't set, then we'll have the correct info for
5971da6c28aaSamw 		 * passtape() later.
5972da6c28aaSamw 		 */
5973da6c28aaSamw 		(void) read_xattr_hdr(attrinfo);
59747c478bd9Sstevel@tonic-gate 		goto top;
59757c478bd9Sstevel@tonic-gate 	}
5976da6c28aaSamw 	/*
5977ced83f9bSceastha 	 * Now that we've read the extended header, call passtape()
5978ced83f9bSceastha 	 * if we don't want to restore attributes or system attributes.
5979ced83f9bSceastha 	 * Don't restore the attribute if we are extracting
5980ced83f9bSceastha 	 * a file from an archive (as opposed to doing a table of
5981ced83f9bSceastha 	 * contents) and any of the following are true:
5982ced83f9bSceastha 	 * 1. neither -@ or -/ was specified.
5983ced83f9bSceastha 	 * 2. -@ was specified, -/ wasn't specified, and we're
5984ced83f9bSceastha 	 * processing a hidden attribute directory of an attribute
5985ced83f9bSceastha 	 * or we're processing a read-write system attribute file.
5986ced83f9bSceastha 	 * 3. -@ wasn't specified, -/ was specified, and the file
5987ced83f9bSceastha 	 * we're processing is not a read-write system attribute file,
5988ced83f9bSceastha 	 * or we're processing the hidden attribute directory of an
5989ced83f9bSceastha 	 * attribute.
5990ced83f9bSceastha 	 *
5991ced83f9bSceastha 	 * We always process the attributes if we're just generating
5992ced83f9bSceastha 	 * generating a table of contents, or if both -@ and -/ were
5993ced83f9bSceastha 	 * specified.
5994da6c28aaSamw 	 */
5995ced83f9bSceastha 	if (xattrp != NULL) {
5996ced83f9bSceastha 		attr_data_t *ainfo = *attrinfo;
5997ced83f9bSceastha 
5998ced83f9bSceastha 		if (!tflag &&
5999ced83f9bSceastha 		    ((!atflag && !saflag) ||
6000ced83f9bSceastha 		    (atflag && !saflag && ((ainfo->attr_parent != NULL) ||
6001ced83f9bSceastha 		    ainfo->attr_rw_sysattr)) ||
6002ced83f9bSceastha 		    (!atflag && saflag && ((ainfo->attr_parent != NULL) ||
6003ced83f9bSceastha 		    !ainfo->attr_rw_sysattr)))) {
6004da6c28aaSamw 			passtape();
6005da6c28aaSamw 			return (0);
6006da6c28aaSamw 		}
6007ced83f9bSceastha 	}
60087c478bd9Sstevel@tonic-gate #endif
60097c478bd9Sstevel@tonic-gate 
60107c478bd9Sstevel@tonic-gate 	/* sets *namep to point at the proper name */
6011da6c28aaSamw 	if (check_prefix(namep, dirp, component) != 0) {
6012da6c28aaSamw 		passtape();
6013da6c28aaSamw 		return (0);
6014da6c28aaSamw 	}
60157c478bd9Sstevel@tonic-gate 
60167c478bd9Sstevel@tonic-gate 	if (endtape()) {
60177c478bd9Sstevel@tonic-gate 		if (Bflag) {
60187c478bd9Sstevel@tonic-gate 			/*
60197c478bd9Sstevel@tonic-gate 			 * Logically at EOT - consume any extra blocks
60207c478bd9Sstevel@tonic-gate 			 * so that write to our stdin won't fail and
60217c478bd9Sstevel@tonic-gate 			 * emit an error message; otherwise something
60227c478bd9Sstevel@tonic-gate 			 * like "dd if=foo.tar | (cd bar; tar xvf -)"
60237c478bd9Sstevel@tonic-gate 			 * will produce a bogus error message from "dd".
60247c478bd9Sstevel@tonic-gate 			 */
60257c478bd9Sstevel@tonic-gate 
60267c478bd9Sstevel@tonic-gate 			while (read(mt, tbuf, TBLOCK*nblock) > 0) {
60277c478bd9Sstevel@tonic-gate 				/* empty body */
60287c478bd9Sstevel@tonic-gate 			}
60297c478bd9Sstevel@tonic-gate 		}
60307c478bd9Sstevel@tonic-gate 		return (-1);
60317c478bd9Sstevel@tonic-gate 	}
60327c478bd9Sstevel@tonic-gate 
60337c478bd9Sstevel@tonic-gate 	gotit = 0;
60347c478bd9Sstevel@tonic-gate 
60357c478bd9Sstevel@tonic-gate 	if ((Iflag && is_in_table(include_tbl, *namep)) ||
60367c478bd9Sstevel@tonic-gate 	    (! Iflag && *argv == NULL)) {
60377c478bd9Sstevel@tonic-gate 		gotit = 1;
60387c478bd9Sstevel@tonic-gate 	} else {
60397c478bd9Sstevel@tonic-gate 		for (cp = argv; *cp; cp++) {
60407c478bd9Sstevel@tonic-gate 			if (is_prefix(*cp, *namep)) {
60417c478bd9Sstevel@tonic-gate 				gotit = 1;
60427c478bd9Sstevel@tonic-gate 				break;
60437c478bd9Sstevel@tonic-gate 			}
60447c478bd9Sstevel@tonic-gate 		}
60457c478bd9Sstevel@tonic-gate 	}
60467c478bd9Sstevel@tonic-gate 
60477c478bd9Sstevel@tonic-gate 	if (! gotit) {
60487c478bd9Sstevel@tonic-gate 		passtape();
60497c478bd9Sstevel@tonic-gate 		return (0);
60507c478bd9Sstevel@tonic-gate 	}
60517c478bd9Sstevel@tonic-gate 
60527c478bd9Sstevel@tonic-gate 	if (Xflag && is_in_table(exclude_tbl, *namep)) {
60537c478bd9Sstevel@tonic-gate 		if (vflag) {
60547c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s excluded\n"),
60557c478bd9Sstevel@tonic-gate 			    *namep);
60567c478bd9Sstevel@tonic-gate 		}
60577c478bd9Sstevel@tonic-gate 		passtape();
60587c478bd9Sstevel@tonic-gate 		return (0);
60597c478bd9Sstevel@tonic-gate 	}
60607c478bd9Sstevel@tonic-gate 
60617c478bd9Sstevel@tonic-gate 	return (1);
60627c478bd9Sstevel@tonic-gate }
60637c478bd9Sstevel@tonic-gate 
6064123523f8Sas158974 
6065123523f8Sas158974 static void
6066123523f8Sas158974 setbytes_to_skip(struct stat *st, int err)
6067123523f8Sas158974 {
6068123523f8Sas158974 	/*
6069123523f8Sas158974 	 * In a scenario where a typeflag 'X' was followed by
6070123523f8Sas158974 	 * a typeflag 'A' and typeflag 'O', then the number of
6071123523f8Sas158974 	 * bytes to skip should be the size of ancillary file,
6072123523f8Sas158974 	 * plus the dblock for regular file, and the size
6073123523f8Sas158974 	 * from Xtarhdr. However, if the typeflag was just 'X'
6074123523f8Sas158974 	 * followed by typeflag 'O', then the number of bytes
6075123523f8Sas158974 	 * to skip should be the size from Xtarhdr.
6076123523f8Sas158974 	 */
6077123523f8Sas158974 	if ((err != 0) && (dblock.dbuf.typeflag == 'A') &&
6078257ece65SRalph Turner - Sun UK - Contractor 	    (xhdr_flgs & _X_SIZE)) {
6079ced83f9bSceastha 		st->st_size += TBLOCK + Xtarhdr.x_filesz;
6080123523f8Sas158974 		xhdr_flgs |= _X_XHDR;
6081123523f8Sas158974 	} else if ((dblock.dbuf.typeflag != 'A') &&
6082257ece65SRalph Turner - Sun UK - Contractor 	    (xhdr_flgs & _X_SIZE)) {
6083257ece65SRalph Turner - Sun UK - Contractor 		st->st_size += Xtarhdr.x_filesz;
6084123523f8Sas158974 		xhdr_flgs |= _X_XHDR;
6085123523f8Sas158974 	}
6086123523f8Sas158974 }
6087123523f8Sas158974 
6088da6c28aaSamw static int
6089da6c28aaSamw fill_in_attr_info(char *attr, char *longname, char *attrparent, int atparentfd,
6090da6c28aaSamw     int rw_sysattr, attr_data_t **attrinfo)
6091da6c28aaSamw {
6092da6c28aaSamw 	size_t	pathlen;
6093da6c28aaSamw 	char	*tpath;
6094da6c28aaSamw 	char	*tparent;
6095da6c28aaSamw 
6096da6c28aaSamw 	/* parent info */
6097da6c28aaSamw 	if (attrparent != NULL) {
6098da6c28aaSamw 		if ((tparent = strdup(attrparent)) == NULL) {
6099da6c28aaSamw 			vperror(0, gettext(
6100da6c28aaSamw 			    "unable to allocate memory for attribute parent "
6101da6c28aaSamw 			    "name for %sattribute %s/%s of %s"),
6102da6c28aaSamw 			    rw_sysattr ? gettext("system ") : "",
6103da6c28aaSamw 			    attrparent, attr, longname);
6104da6c28aaSamw 			return (1);
6105da6c28aaSamw 		}
6106da6c28aaSamw 	} else {
6107da6c28aaSamw 		tparent = NULL;
6108da6c28aaSamw 	}
6109da6c28aaSamw 
6110da6c28aaSamw 	/* path info */
6111da6c28aaSamw 	pathlen = strlen(attr) + 1;
6112da6c28aaSamw 	if (attrparent != NULL) {
6113da6c28aaSamw 		pathlen += strlen(attrparent) + 1;	/* add 1 for '/' */
6114da6c28aaSamw 	}
6115da6c28aaSamw 	if ((tpath = calloc(1, pathlen)) == NULL) {
6116da6c28aaSamw 		vperror(0, gettext(
6117da6c28aaSamw 		    "unable to allocate memory for full "
6118da6c28aaSamw 		    "attribute path name for %sattribute %s%s%s of %s"),
6119da6c28aaSamw 		    rw_sysattr ? gettext("system ") : "",
6120da6c28aaSamw 		    (attrparent == NULL) ? "" : attrparent,
6121da6c28aaSamw 		    (attrparent == NULL) ? "" : "/",
6122da6c28aaSamw 		    attr, longname);
6123da6c28aaSamw 		if (tparent != NULL) {
6124da6c28aaSamw 			free(tparent);
6125da6c28aaSamw 		}
6126da6c28aaSamw 		return (1);
6127da6c28aaSamw 	}
6128da6c28aaSamw 	(void) snprintf(tpath, pathlen, "%s%s%s",
6129da6c28aaSamw 	    (attrparent == NULL) ? "" : attrparent,
6130da6c28aaSamw 	    (attrparent == NULL) ? "" : "/",
6131da6c28aaSamw 	    attr);
6132da6c28aaSamw 
6133da6c28aaSamw 	/* fill in the attribute info */
6134da6c28aaSamw 	if (*attrinfo == NULL) {
6135da6c28aaSamw 		if ((*attrinfo = malloc(sizeof (attr_data_t))) == NULL) {
6136da6c28aaSamw 			vperror(0, gettext(
6137da6c28aaSamw 			    "unable to allocate memory for attribute "
6138da6c28aaSamw 			    "information for %sattribute %s%s%s of %s"),
6139da6c28aaSamw 			    rw_sysattr ? gettext("system ") : "",
6140da6c28aaSamw 			    (attrparent == NULL) ? "" : attrparent,
6141da6c28aaSamw 			    (attrparent == NULL) ? "" : gettext("/"),
6142da6c28aaSamw 			    attr, longname);
6143da6c28aaSamw 			if (tparent != NULL) {
6144da6c28aaSamw 				free(tparent);
6145da6c28aaSamw 			}
6146da6c28aaSamw 			free(tpath);
6147da6c28aaSamw 			return (1);
6148da6c28aaSamw 		}
6149da6c28aaSamw 	} else {
6150da6c28aaSamw 		if ((*attrinfo)->attr_parent != NULL) {
6151da6c28aaSamw 			free((*attrinfo)->attr_parent);
6152da6c28aaSamw 		}
6153da6c28aaSamw 		if ((*attrinfo)->attr_path != NULL) {
6154da6c28aaSamw 			free((*attrinfo)->attr_path);
6155da6c28aaSamw 		}
6156da6c28aaSamw 		/*
6157da6c28aaSamw 		 * The parent file descriptor is passed in, so don't
6158da6c28aaSamw 		 * close it here as it should be closed by the function
6159da6c28aaSamw 		 * that opened it.
6160da6c28aaSamw 		 */
6161da6c28aaSamw 	}
6162da6c28aaSamw 	(*attrinfo)->attr_parent = tparent;
6163da6c28aaSamw 	(*attrinfo)->attr_path = tpath;
6164da6c28aaSamw 	(*attrinfo)->attr_rw_sysattr = rw_sysattr;
6165da6c28aaSamw 	(*attrinfo)->attr_parentfd = atparentfd;
6166da6c28aaSamw 
6167da6c28aaSamw 	return (0);
6168da6c28aaSamw }
61697c478bd9Sstevel@tonic-gate 
61707c478bd9Sstevel@tonic-gate /*
61713a1dab68SRich Burridge  * Test to see if name is a directory.
61723a1dab68SRich Burridge  *
61733a1dab68SRich Burridge  * Return 1 if true, 0 otherwise.
61743a1dab68SRich Burridge  */
61753a1dab68SRich Burridge 
61763a1dab68SRich Burridge static int
61773a1dab68SRich Burridge is_directory(char *name)
61783a1dab68SRich Burridge {
61793a1dab68SRich Burridge #if defined(O_XATTR)
61803a1dab68SRich Burridge 	/*
61813a1dab68SRich Burridge 	 * If there is an xattr_buf structure associated with this file,
61823a1dab68SRich Burridge 	 * then the directory test is based on whether the name has a
61833a1dab68SRich Burridge 	 * trailing slash.
61843a1dab68SRich Burridge 	 */
61853a1dab68SRich Burridge 	if (xattrp)
61863a1dab68SRich Burridge 		return (name[strlen(name) - 1] == '/');
61873a1dab68SRich Burridge #endif
61883a1dab68SRich Burridge 	if (is_posix)
61893a1dab68SRich Burridge 		return (dblock.dbuf.typeflag == '5');
61903a1dab68SRich Burridge 	else
61913a1dab68SRich Burridge 		return (name[strlen(name) - 1] == '/');
61923a1dab68SRich Burridge }
61933a1dab68SRich Burridge 
61943a1dab68SRich Burridge /*
61951a431409SRich Burridge  * Test if name has a '..' sequence in it.
61961a431409SRich Burridge  *
61971a431409SRich Burridge  * Return 1 if found, 0 otherwise.
61981a431409SRich Burridge  */
61991a431409SRich Burridge 
62001a431409SRich Burridge static int
62011a431409SRich Burridge has_dot_dot(char *name)
62021a431409SRich Burridge {
62031a431409SRich Burridge 	char *s;
62041a431409SRich Burridge 	size_t name_len = strlen(name);
62051a431409SRich Burridge 
62061a431409SRich Burridge 	for (s = name; s < (name + name_len - 2); s++) {
62071a431409SRich Burridge 		if (s[0] == '.' && s[1] == '.' && ((s[2] == '/') || !s[2]))
62081a431409SRich Burridge 			return (1);
62091a431409SRich Burridge 
62101a431409SRich Burridge 		while (! (*s == '/')) {
62111a431409SRich Burridge 			if (! *s++)
62121a431409SRich Burridge 				return (0);
62131a431409SRich Burridge 		}
62141a431409SRich Burridge 	}
62151a431409SRich Burridge 
62161a431409SRich Burridge 	return (0);
62171a431409SRich Burridge }
62181a431409SRich Burridge 
62191a431409SRich Burridge /*
62201a431409SRich Burridge  * Test if name is an absolute path name.
62211a431409SRich Burridge  *
62221a431409SRich Burridge  * Return 1 if true, 0 otherwise.
62231a431409SRich Burridge  */
62241a431409SRich Burridge 
62251a431409SRich Burridge static int
62261a431409SRich Burridge is_absolute(char *name)
62271a431409SRich Burridge {
6228fe5a79a9SRich Burridge #if defined(O_XATTR)
6229fe5a79a9SRich Burridge 	/*
6230fe5a79a9SRich Burridge 	 * If this is an extended attribute (whose name will begin with
6231fe5a79a9SRich Burridge 	 * "/dev/null/", always return 0 as they should be extracted with
6232fe5a79a9SRich Burridge 	 * the name intact, to allow other tar archiving programs that
6233fe5a79a9SRich Burridge 	 * don't understand extended attributes, to correctly throw them away.
6234fe5a79a9SRich Burridge 	 */
6235fe5a79a9SRich Burridge 	if (xattrp)
6236fe5a79a9SRich Burridge 		return (0);
6237fe5a79a9SRich Burridge #endif
6238fe5a79a9SRich Burridge 
62391a431409SRich Burridge 	return (name[0] == '/');
62401a431409SRich Burridge }
62411a431409SRich Burridge 
62421a431409SRich Burridge /*
62431a431409SRich Burridge  * Adjust the pathname to make it a relative one. Strip off any leading
62441a431409SRich Burridge  * '/' characters and if the pathname contains any '..' sequences, strip
62451a431409SRich Burridge  * upto and including the last occurance of '../' (or '..' if found at
62461a431409SRich Burridge  * the very end of the pathname).
62471a431409SRich Burridge  *
62481a431409SRich Burridge  * Return the relative pathname. stripped_prefix will also return the
62491a431409SRich Burridge  * portion of name that was stripped off and should be freed by the
62501a431409SRich Burridge  * calling routine when no longer needed.
62511a431409SRich Burridge  */
62521a431409SRich Burridge 
62531a431409SRich Burridge static char *
62541a431409SRich Burridge make_relative_name(char *name, char **stripped_prefix)
62551a431409SRich Burridge {
62561a431409SRich Burridge 	char *s;
62571a431409SRich Burridge 	size_t prefix_len = 0;
62581a431409SRich Burridge 	size_t name_len = strlen(name);
62591a431409SRich Burridge 
62601a431409SRich Burridge 	for (s = name + prefix_len; s < (name + name_len - 2); ) {
62611a431409SRich Burridge 		if (s[0] == '.' && s[1] == '.' && ((s[2] == '/') || !s[2]))
62621a431409SRich Burridge 			prefix_len = s + 2 - name;
62631a431409SRich Burridge 
62641a431409SRich Burridge 		do {
62651a431409SRich Burridge 			char c = *s++;
62661a431409SRich Burridge 
62671a431409SRich Burridge 			if (c == '/')
62681a431409SRich Burridge 				break;
62691a431409SRich Burridge 		} while (*s);
62701a431409SRich Burridge 	}
62711a431409SRich Burridge 
62721a431409SRich Burridge 	for (s = name + prefix_len; *s == '/'; s++)
62731a431409SRich Burridge 		continue;
62741a431409SRich Burridge 	prefix_len = s - name;
62751a431409SRich Burridge 
62761a431409SRich Burridge 	/* Create the portion of the name that was stripped off. */
62771a431409SRich Burridge 	s = malloc(prefix_len + 1);
62781a431409SRich Burridge 	memcpy(s, name, prefix_len);
62791a431409SRich Burridge 	s[prefix_len] = 0;
62801a431409SRich Burridge 	*stripped_prefix = s;
62811a431409SRich Burridge 	s = &name[prefix_len];
62821a431409SRich Burridge 
62831a431409SRich Burridge 	return (s);
62841a431409SRich Burridge }
62851a431409SRich Burridge 
62861a431409SRich Burridge /*
62877c478bd9Sstevel@tonic-gate  *  Return through *namep a pointer to the proper fullname (i.e  "<name> |
62887c478bd9Sstevel@tonic-gate  *  <prefix>/<name>"), as represented in the header entry dblock.dbuf.
6289da6c28aaSamw  *
6290da6c28aaSamw  * Returns 0 if successful, otherwise returns 1.
62917c478bd9Sstevel@tonic-gate  */
62927c478bd9Sstevel@tonic-gate 
6293da6c28aaSamw static int
62947c478bd9Sstevel@tonic-gate check_prefix(char **namep, char **dirp, char **compp)
62957c478bd9Sstevel@tonic-gate {
62967c478bd9Sstevel@tonic-gate 	static char fullname[PATH_MAX + 1];
62977c478bd9Sstevel@tonic-gate 	static char dir[PATH_MAX + 1];
62987c478bd9Sstevel@tonic-gate 	static char component[PATH_MAX + 1];
62997c478bd9Sstevel@tonic-gate 	static char savename[PATH_MAX + 1];
63007c478bd9Sstevel@tonic-gate 	char *s;
63017c478bd9Sstevel@tonic-gate 
63027c478bd9Sstevel@tonic-gate 	(void) memset(dir, 0, sizeof (dir));
63037c478bd9Sstevel@tonic-gate 	(void) memset(component, 0, sizeof (component));
63047c478bd9Sstevel@tonic-gate 
63057c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_PATH) {
63067c478bd9Sstevel@tonic-gate 		(void) strcpy(fullname, Xtarhdr.x_path);
63077c478bd9Sstevel@tonic-gate 	} else {
63087c478bd9Sstevel@tonic-gate 		if (dblock.dbuf.prefix[0] != '\0')
63097c478bd9Sstevel@tonic-gate 			(void) sprintf(fullname, "%.*s/%.*s", PRESIZ,
63107c478bd9Sstevel@tonic-gate 			    dblock.dbuf.prefix, NAMSIZ, dblock.dbuf.name);
63117c478bd9Sstevel@tonic-gate 		else
63127c478bd9Sstevel@tonic-gate 			(void) sprintf(fullname, "%.*s", NAMSIZ,
63137c478bd9Sstevel@tonic-gate 			    dblock.dbuf.name);
63147c478bd9Sstevel@tonic-gate 	}
63157c478bd9Sstevel@tonic-gate 
63167c478bd9Sstevel@tonic-gate 	/*
63171a431409SRich Burridge 	 * If we are printing a table of contents or extracting an archive,
63181a431409SRich Burridge 	 * make absolute pathnames relative and prohibit the unpacking of
63191a431409SRich Burridge 	 * files contain ".." in their name (unless the user has supplied
63201a431409SRich Burridge 	 * the -P option).
63211a431409SRich Burridge 	 */
63221a431409SRich Burridge 	if ((tflag || xflag) && !Pflag) {
63231a431409SRich Burridge 		if (is_absolute(fullname) || has_dot_dot(fullname)) {
63241a431409SRich Burridge 			char *stripped_prefix;
63251a431409SRich Burridge 			size_t prefix_len = 0;
63261a431409SRich Burridge 
63271a431409SRich Burridge 			(void) strcpy(savename, fullname);
63281a431409SRich Burridge 			strcpy(fullname,
63291a431409SRich Burridge 			    make_relative_name(savename, &stripped_prefix));
63301a431409SRich Burridge 			(void) fprintf(stderr,
63311a431409SRich Burridge 			    gettext("tar: Removing leading '%s' from '%s'\n"),
63321a431409SRich Burridge 			    stripped_prefix, savename);
63331a431409SRich Burridge 			free(stripped_prefix);
63341a431409SRich Burridge 		}
63351a431409SRich Burridge 	}
63361a431409SRich Burridge 
63371a431409SRich Burridge 	/*
63387c478bd9Sstevel@tonic-gate 	 * Set dir and component names
63397c478bd9Sstevel@tonic-gate 	 */
63407c478bd9Sstevel@tonic-gate 
63417c478bd9Sstevel@tonic-gate 	get_parent(fullname, dir);
63427c478bd9Sstevel@tonic-gate 
63437c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
6344da6c28aaSamw 	if (xattrp == NULL) {
63457c478bd9Sstevel@tonic-gate #endif
63467c478bd9Sstevel@tonic-gate 		/*
63477c478bd9Sstevel@tonic-gate 		 * Save of real name since were going to chop off the
63487c478bd9Sstevel@tonic-gate 		 * trailing slashes.
63497c478bd9Sstevel@tonic-gate 		 */
63507c478bd9Sstevel@tonic-gate 		(void) strcpy(savename, fullname);
63517c478bd9Sstevel@tonic-gate 		/*
63527c478bd9Sstevel@tonic-gate 		 * first strip of trailing slashes.
63537c478bd9Sstevel@tonic-gate 		 */
63547c478bd9Sstevel@tonic-gate 		chop_endslashes(savename);
63557c478bd9Sstevel@tonic-gate 		s = get_component(savename);
63567c478bd9Sstevel@tonic-gate 		(void) strcpy(component, s);
63577c478bd9Sstevel@tonic-gate 
63587c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
63597c478bd9Sstevel@tonic-gate 	} else {
63607c478bd9Sstevel@tonic-gate 		(void) strcpy(fullname, xattrp->h_names);
63617c478bd9Sstevel@tonic-gate 		(void) strcpy(dir, fullname);
6362da6c28aaSamw 		(void) strcpy(component, basename(xattrp->h_names +
6363da6c28aaSamw 		    strlen(xattrp->h_names) + 1));
63647c478bd9Sstevel@tonic-gate 	}
63657c478bd9Sstevel@tonic-gate #endif
63667c478bd9Sstevel@tonic-gate 	*namep = fullname;
63677c478bd9Sstevel@tonic-gate 	*dirp = dir;
63687c478bd9Sstevel@tonic-gate 	*compp = component;
6369da6c28aaSamw 
6370da6c28aaSamw 	return (0);
63717c478bd9Sstevel@tonic-gate }
63727c478bd9Sstevel@tonic-gate 
63737c478bd9Sstevel@tonic-gate /*
63747c478bd9Sstevel@tonic-gate  * Return true if the object indicated by the file descriptor and type
63757c478bd9Sstevel@tonic-gate  * is a tape device, false otherwise
63767c478bd9Sstevel@tonic-gate  */
63777c478bd9Sstevel@tonic-gate 
63787c478bd9Sstevel@tonic-gate static int
63797c478bd9Sstevel@tonic-gate istape(int fd, int type)
63807c478bd9Sstevel@tonic-gate {
63817c478bd9Sstevel@tonic-gate 	int result = 0;
63827c478bd9Sstevel@tonic-gate 
63834bc0a2efScasper 	if (S_ISCHR(type)) {
63847c478bd9Sstevel@tonic-gate 		struct mtget mtg;
63857c478bd9Sstevel@tonic-gate 
63867c478bd9Sstevel@tonic-gate 		if (ioctl(fd, MTIOCGET, &mtg) != -1) {
63877c478bd9Sstevel@tonic-gate 			result = 1;
63887c478bd9Sstevel@tonic-gate 		}
63897c478bd9Sstevel@tonic-gate 	}
63907c478bd9Sstevel@tonic-gate 
63917c478bd9Sstevel@tonic-gate 	return (result);
63927c478bd9Sstevel@tonic-gate }
63937c478bd9Sstevel@tonic-gate 
63947c478bd9Sstevel@tonic-gate #include <utmpx.h>
63957c478bd9Sstevel@tonic-gate 
63967c478bd9Sstevel@tonic-gate struct utmpx utmpx;
63977c478bd9Sstevel@tonic-gate 
63987c478bd9Sstevel@tonic-gate #define	NMAX	(sizeof (utmpx.ut_name))
63997c478bd9Sstevel@tonic-gate 
64007c478bd9Sstevel@tonic-gate typedef struct cachenode {	/* this struct must be zeroed before using */
64017c478bd9Sstevel@tonic-gate 	struct cachenode *next;	/* next in hash chain */
64027c478bd9Sstevel@tonic-gate 	int val;		/* the uid or gid of this entry */
64037c478bd9Sstevel@tonic-gate 	int namehash;		/* name's hash signature */
64047c478bd9Sstevel@tonic-gate 	char name[NMAX+1];	/* the string that val maps to */
64057c478bd9Sstevel@tonic-gate } cachenode_t;
64067c478bd9Sstevel@tonic-gate 
64077c478bd9Sstevel@tonic-gate #define	HASHSIZE	256
64087c478bd9Sstevel@tonic-gate 
64097c478bd9Sstevel@tonic-gate static cachenode_t *names[HASHSIZE];
64107c478bd9Sstevel@tonic-gate static cachenode_t *groups[HASHSIZE];
64117c478bd9Sstevel@tonic-gate static cachenode_t *uids[HASHSIZE];
64127c478bd9Sstevel@tonic-gate static cachenode_t *gids[HASHSIZE];
64137c478bd9Sstevel@tonic-gate 
64147c478bd9Sstevel@tonic-gate static int
64157c478bd9Sstevel@tonic-gate hash_byname(char *name)
64167c478bd9Sstevel@tonic-gate {
64177c478bd9Sstevel@tonic-gate 	int i, c, h = 0;
64187c478bd9Sstevel@tonic-gate 
64197c478bd9Sstevel@tonic-gate 	for (i = 0; i < NMAX; i++) {
64207c478bd9Sstevel@tonic-gate 		c = name[i];
64217c478bd9Sstevel@tonic-gate 		if (c == '\0')
64227c478bd9Sstevel@tonic-gate 			break;
64237c478bd9Sstevel@tonic-gate 		h = (h << 4) + h + c;
64247c478bd9Sstevel@tonic-gate 	}
64257c478bd9Sstevel@tonic-gate 	return (h);
64267c478bd9Sstevel@tonic-gate }
64277c478bd9Sstevel@tonic-gate 
64287c478bd9Sstevel@tonic-gate static cachenode_t *
64297c478bd9Sstevel@tonic-gate hash_lookup_byval(cachenode_t *table[], int val)
64307c478bd9Sstevel@tonic-gate {
64317c478bd9Sstevel@tonic-gate 	int h = val;
64327c478bd9Sstevel@tonic-gate 	cachenode_t *c;
64337c478bd9Sstevel@tonic-gate 
64347c478bd9Sstevel@tonic-gate 	for (c = table[h & (HASHSIZE - 1)]; c != NULL; c = c->next) {
64357c478bd9Sstevel@tonic-gate 		if (c->val == val)
64367c478bd9Sstevel@tonic-gate 			return (c);
64377c478bd9Sstevel@tonic-gate 	}
64387c478bd9Sstevel@tonic-gate 	return (NULL);
64397c478bd9Sstevel@tonic-gate }
64407c478bd9Sstevel@tonic-gate 
64417c478bd9Sstevel@tonic-gate static cachenode_t *
64427c478bd9Sstevel@tonic-gate hash_lookup_byname(cachenode_t *table[], char *name)
64437c478bd9Sstevel@tonic-gate {
64447c478bd9Sstevel@tonic-gate 	int h = hash_byname(name);
64457c478bd9Sstevel@tonic-gate 	cachenode_t *c;
64467c478bd9Sstevel@tonic-gate 
64477c478bd9Sstevel@tonic-gate 	for (c = table[h & (HASHSIZE - 1)]; c != NULL; c = c->next) {
64487c478bd9Sstevel@tonic-gate 		if (c->namehash == h && strcmp(c->name, name) == 0)
64497c478bd9Sstevel@tonic-gate 			return (c);
64507c478bd9Sstevel@tonic-gate 	}
64517c478bd9Sstevel@tonic-gate 	return (NULL);
64527c478bd9Sstevel@tonic-gate }
64537c478bd9Sstevel@tonic-gate 
64547c478bd9Sstevel@tonic-gate static cachenode_t *
64557c478bd9Sstevel@tonic-gate hash_insert(cachenode_t *table[], char *name, int value)
64567c478bd9Sstevel@tonic-gate {
64577c478bd9Sstevel@tonic-gate 	cachenode_t *c;
64587c478bd9Sstevel@tonic-gate 	int signature;
64597c478bd9Sstevel@tonic-gate 
64607c478bd9Sstevel@tonic-gate 	c = calloc(1, sizeof (cachenode_t));
64617c478bd9Sstevel@tonic-gate 	if (c == NULL) {
64627c478bd9Sstevel@tonic-gate 		perror("malloc");
64637c478bd9Sstevel@tonic-gate 		exit(1);
64647c478bd9Sstevel@tonic-gate 	}
64657c478bd9Sstevel@tonic-gate 	if (name != NULL) {
64667c478bd9Sstevel@tonic-gate 		(void) strncpy(c->name, name, NMAX);
64677c478bd9Sstevel@tonic-gate 		c->namehash = hash_byname(name);
64687c478bd9Sstevel@tonic-gate 	}
64697c478bd9Sstevel@tonic-gate 	c->val = value;
64707c478bd9Sstevel@tonic-gate 	if (table == uids || table == gids)
64717c478bd9Sstevel@tonic-gate 		signature = c->val;
64727c478bd9Sstevel@tonic-gate 	else
64737c478bd9Sstevel@tonic-gate 		signature = c->namehash;
64747c478bd9Sstevel@tonic-gate 	c->next = table[signature & (HASHSIZE - 1)];
64757c478bd9Sstevel@tonic-gate 	table[signature & (HASHSIZE - 1)] = c;
64767c478bd9Sstevel@tonic-gate 	return (c);
64777c478bd9Sstevel@tonic-gate }
64787c478bd9Sstevel@tonic-gate 
64797c478bd9Sstevel@tonic-gate static char *
64807c478bd9Sstevel@tonic-gate getname(uid_t uid)
64817c478bd9Sstevel@tonic-gate {
64827c478bd9Sstevel@tonic-gate 	cachenode_t *c;
64837c478bd9Sstevel@tonic-gate 
64847c478bd9Sstevel@tonic-gate 	if ((c = hash_lookup_byval(uids, uid)) == NULL) {
64857c478bd9Sstevel@tonic-gate 		struct passwd *pwent = getpwuid(uid);
64867c478bd9Sstevel@tonic-gate 		c = hash_insert(uids, pwent ? pwent->pw_name : NULL, uid);
64877c478bd9Sstevel@tonic-gate 	}
64887c478bd9Sstevel@tonic-gate 	return (c->name);
64897c478bd9Sstevel@tonic-gate }
64907c478bd9Sstevel@tonic-gate 
64917c478bd9Sstevel@tonic-gate static char *
64927c478bd9Sstevel@tonic-gate getgroup(gid_t gid)
64937c478bd9Sstevel@tonic-gate {
64947c478bd9Sstevel@tonic-gate 	cachenode_t *c;
64957c478bd9Sstevel@tonic-gate 
64967c478bd9Sstevel@tonic-gate 	if ((c = hash_lookup_byval(gids, gid)) == NULL) {
64977c478bd9Sstevel@tonic-gate 		struct group *grent = getgrgid(gid);
64987c478bd9Sstevel@tonic-gate 		c = hash_insert(gids, grent ? grent->gr_name : NULL, gid);
64997c478bd9Sstevel@tonic-gate 	}
65007c478bd9Sstevel@tonic-gate 	return (c->name);
65017c478bd9Sstevel@tonic-gate }
65027c478bd9Sstevel@tonic-gate 
65037c478bd9Sstevel@tonic-gate static uid_t
65047c478bd9Sstevel@tonic-gate getuidbyname(char *name)
65057c478bd9Sstevel@tonic-gate {
65067c478bd9Sstevel@tonic-gate 	cachenode_t *c;
65077c478bd9Sstevel@tonic-gate 
65087c478bd9Sstevel@tonic-gate 	if ((c = hash_lookup_byname(names, name)) == NULL) {
65097c478bd9Sstevel@tonic-gate 		struct passwd *pwent = getpwnam(name);
65107c478bd9Sstevel@tonic-gate 		c = hash_insert(names, name, pwent ? (int)pwent->pw_uid : -1);
65117c478bd9Sstevel@tonic-gate 	}
65127c478bd9Sstevel@tonic-gate 	return ((uid_t)c->val);
65137c478bd9Sstevel@tonic-gate }
65147c478bd9Sstevel@tonic-gate 
65157c478bd9Sstevel@tonic-gate static gid_t
65167c478bd9Sstevel@tonic-gate getgidbyname(char *group)
65177c478bd9Sstevel@tonic-gate {
65187c478bd9Sstevel@tonic-gate 	cachenode_t *c;
65197c478bd9Sstevel@tonic-gate 
65207c478bd9Sstevel@tonic-gate 	if ((c = hash_lookup_byname(groups, group)) == NULL) {
65217c478bd9Sstevel@tonic-gate 		struct group *grent = getgrnam(group);
65227c478bd9Sstevel@tonic-gate 		c = hash_insert(groups, group, grent ? (int)grent->gr_gid : -1);
65237c478bd9Sstevel@tonic-gate 	}
65247c478bd9Sstevel@tonic-gate 	return ((gid_t)c->val);
65257c478bd9Sstevel@tonic-gate }
65267c478bd9Sstevel@tonic-gate 
65277c478bd9Sstevel@tonic-gate /*
65287c478bd9Sstevel@tonic-gate  * Build the header.
65297c478bd9Sstevel@tonic-gate  * Determine whether or not an extended header is also needed.  If needed,
65307c478bd9Sstevel@tonic-gate  * create and write the extended header and its data.
65317c478bd9Sstevel@tonic-gate  * Writing of the extended header assumes that "tomodes" has been called and
65327c478bd9Sstevel@tonic-gate  * the relevant information has been placed in the header block.
65337c478bd9Sstevel@tonic-gate  */
65347c478bd9Sstevel@tonic-gate 
65357c478bd9Sstevel@tonic-gate static int
65367c478bd9Sstevel@tonic-gate build_dblock(
65377c478bd9Sstevel@tonic-gate 	const char		*name,
65387c478bd9Sstevel@tonic-gate 	const char		*linkname,
65397c478bd9Sstevel@tonic-gate 	const char		typeflag,
65407c478bd9Sstevel@tonic-gate 	const int		filetype,
65417c478bd9Sstevel@tonic-gate 	const struct stat	*sp,
65427c478bd9Sstevel@tonic-gate 	const dev_t		device,
65437c478bd9Sstevel@tonic-gate 	const char		*prefix)
65447c478bd9Sstevel@tonic-gate {
65457c478bd9Sstevel@tonic-gate 	int nblks;
65467c478bd9Sstevel@tonic-gate 	major_t		dev;
65477c478bd9Sstevel@tonic-gate 	const char	*filename;
65487c478bd9Sstevel@tonic-gate 	const char	*lastslash;
65497c478bd9Sstevel@tonic-gate 
65507c478bd9Sstevel@tonic-gate 	if (filetype == XATTR_FILE)
65517c478bd9Sstevel@tonic-gate 		dblock.dbuf.typeflag = _XATTR_HDRTYPE;
65527c478bd9Sstevel@tonic-gate 	else
65537c478bd9Sstevel@tonic-gate 		dblock.dbuf.typeflag = typeflag;
65547c478bd9Sstevel@tonic-gate 	(void) memset(dblock.dbuf.name, '\0', NAMSIZ);
65557c478bd9Sstevel@tonic-gate 	(void) memset(dblock.dbuf.linkname, '\0', NAMSIZ);
65567c478bd9Sstevel@tonic-gate 	(void) memset(dblock.dbuf.prefix, '\0', PRESIZ);
65577c478bd9Sstevel@tonic-gate 
65587c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_PATH)
65597c478bd9Sstevel@tonic-gate 		filename = Xtarhdr.x_path;
65607c478bd9Sstevel@tonic-gate 	else
65617c478bd9Sstevel@tonic-gate 		filename = name;
65627c478bd9Sstevel@tonic-gate 
65637c478bd9Sstevel@tonic-gate 	if ((dev = major(device)) > OCTAL7CHAR) {
65647c478bd9Sstevel@tonic-gate 		if (Eflag) {
65657c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_DEVMAJOR;
65667c478bd9Sstevel@tonic-gate 			Xtarhdr.x_devmajor = dev;
65677c478bd9Sstevel@tonic-gate 		} else {
65687c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
65697c478bd9Sstevel@tonic-gate 			    "Device major too large for %s.  Use -E flag."),
65707c478bd9Sstevel@tonic-gate 			    filename);
65717c478bd9Sstevel@tonic-gate 			if (errflag)
65727c478bd9Sstevel@tonic-gate 				done(1);
65737c478bd9Sstevel@tonic-gate 			else
65747c478bd9Sstevel@tonic-gate 				Errflg = 1;
65757c478bd9Sstevel@tonic-gate 		}
65767c478bd9Sstevel@tonic-gate 		dev = 0;
65777c478bd9Sstevel@tonic-gate 	}
65787c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.devmajor, "%07lo", dev);
65797c478bd9Sstevel@tonic-gate 	if ((dev = minor(device)) > OCTAL7CHAR) {
65807c478bd9Sstevel@tonic-gate 		if (Eflag) {
65817c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_DEVMINOR;
65827c478bd9Sstevel@tonic-gate 			Xtarhdr.x_devminor = dev;
65837c478bd9Sstevel@tonic-gate 		} else {
65847c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
65857c478bd9Sstevel@tonic-gate 			    "Device minor too large for %s.  Use -E flag."),
65867c478bd9Sstevel@tonic-gate 			    filename);
65877c478bd9Sstevel@tonic-gate 			if (errflag)
65887c478bd9Sstevel@tonic-gate 				done(1);
65897c478bd9Sstevel@tonic-gate 			else
65907c478bd9Sstevel@tonic-gate 				Errflg = 1;
65917c478bd9Sstevel@tonic-gate 		}
65927c478bd9Sstevel@tonic-gate 		dev = 0;
65937c478bd9Sstevel@tonic-gate 	}
65947c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.devminor, "%07lo", dev);
65957c478bd9Sstevel@tonic-gate 
65967c478bd9Sstevel@tonic-gate 	(void) strncpy(dblock.dbuf.name, name, NAMSIZ);
65977c478bd9Sstevel@tonic-gate 	(void) strncpy(dblock.dbuf.linkname, linkname, NAMSIZ);
65987c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.magic, "%.5s", magic_type);
65997c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.version, "00");
66007c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.uname, "%.31s", getname(sp->st_uid));
66017c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.gname, "%.31s", getgroup(sp->st_gid));
66027c478bd9Sstevel@tonic-gate 	(void) strncpy(dblock.dbuf.prefix, prefix, PRESIZ);
66037c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
66047c478bd9Sstevel@tonic-gate 
66057c478bd9Sstevel@tonic-gate 	if (Eflag) {
66067c478bd9Sstevel@tonic-gate 		(void) bcopy(dblock.dummy, xhdr_buf.dummy, TBLOCK);
66077c478bd9Sstevel@tonic-gate 		(void) memset(xhdr_buf.dbuf.name, '\0', NAMSIZ);
66087c478bd9Sstevel@tonic-gate 		lastslash = strrchr(name, '/');
66097c478bd9Sstevel@tonic-gate 		if (lastslash == NULL)
66107c478bd9Sstevel@tonic-gate 			lastslash = name;
66117c478bd9Sstevel@tonic-gate 		else
66127c478bd9Sstevel@tonic-gate 			lastslash++;
66137c478bd9Sstevel@tonic-gate 		(void) strcpy(xhdr_buf.dbuf.name, lastslash);
66147c478bd9Sstevel@tonic-gate 		(void) memset(xhdr_buf.dbuf.linkname, '\0', NAMSIZ);
66157c478bd9Sstevel@tonic-gate 		(void) memset(xhdr_buf.dbuf.prefix, '\0', PRESIZ);
66167c478bd9Sstevel@tonic-gate 		(void) strcpy(xhdr_buf.dbuf.prefix, xhdr_dirname);
66177c478bd9Sstevel@tonic-gate 		xhdr_count++;
66187c478bd9Sstevel@tonic-gate 		xrec_offset = 0;
66197c478bd9Sstevel@tonic-gate 		gen_date("mtime", sp->st_mtim);
66207c478bd9Sstevel@tonic-gate 		xhdr_buf.dbuf.typeflag = 'X';
66217c478bd9Sstevel@tonic-gate 		if (gen_utf8_names(filename) != 0)
66227c478bd9Sstevel@tonic-gate 			return (1);
66237c478bd9Sstevel@tonic-gate 
66247c478bd9Sstevel@tonic-gate #ifdef XHDR_DEBUG
66257c478bd9Sstevel@tonic-gate 		Xtarhdr.x_uname = dblock.dbuf.uname;
66267c478bd9Sstevel@tonic-gate 		Xtarhdr.x_gname = dblock.dbuf.gname;
66277c478bd9Sstevel@tonic-gate 		xhdr_flgs |= (_X_UNAME | _X_GNAME);
66287c478bd9Sstevel@tonic-gate #endif
66297c478bd9Sstevel@tonic-gate 		if (xhdr_flgs) {
66307c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_DEVMAJOR)
66317c478bd9Sstevel@tonic-gate 				gen_num("SUN.devmajor", Xtarhdr.x_devmajor);
66327c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_DEVMINOR)
66337c478bd9Sstevel@tonic-gate 				gen_num("SUN.devminor", Xtarhdr.x_devminor);
66347c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_GID)
66357c478bd9Sstevel@tonic-gate 				gen_num("gid", Xtarhdr.x_gid);
66367c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_UID)
66377c478bd9Sstevel@tonic-gate 				gen_num("uid", Xtarhdr.x_uid);
66387c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_SIZE)
66397c478bd9Sstevel@tonic-gate 				gen_num("size", Xtarhdr.x_filesz);
66407c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_PATH)
66417c478bd9Sstevel@tonic-gate 				gen_string("path", Xtarhdr.x_path);
66427c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_LINKPATH)
66437c478bd9Sstevel@tonic-gate 				gen_string("linkpath", Xtarhdr.x_linkpath);
66447c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_GNAME)
66457c478bd9Sstevel@tonic-gate 				gen_string("gname", Xtarhdr.x_gname);
66467c478bd9Sstevel@tonic-gate 			if (xhdr_flgs & _X_UNAME)
66477c478bd9Sstevel@tonic-gate 				gen_string("uname", Xtarhdr.x_uname);
66487c478bd9Sstevel@tonic-gate 		}
66497c478bd9Sstevel@tonic-gate 		(void) sprintf(xhdr_buf.dbuf.size,
66507c478bd9Sstevel@tonic-gate 		    "%011" FMT_off_t_o, xrec_offset);
66517c478bd9Sstevel@tonic-gate 		(void) sprintf(xhdr_buf.dbuf.chksum, "%07o",
66527c478bd9Sstevel@tonic-gate 		    checksum(&xhdr_buf));
66537c478bd9Sstevel@tonic-gate 		(void) writetbuf((char *)&xhdr_buf, 1);
66547c478bd9Sstevel@tonic-gate 		nblks = TBLOCKS(xrec_offset);
66557c478bd9Sstevel@tonic-gate 		(void) writetbuf(xrec_ptr, nblks);
66567c478bd9Sstevel@tonic-gate 	}
66577c478bd9Sstevel@tonic-gate 	return (0);
66587c478bd9Sstevel@tonic-gate }
66597c478bd9Sstevel@tonic-gate 
66607c478bd9Sstevel@tonic-gate 
66617c478bd9Sstevel@tonic-gate /*
66627c478bd9Sstevel@tonic-gate  *  makeDir - ensure that a directory with the pathname denoted by name
66637c478bd9Sstevel@tonic-gate  *            exists, and return 1 on success, and 0 on failure (e.g.,
66647c478bd9Sstevel@tonic-gate  *	      read-only file system, exists but not-a-directory).
66657c478bd9Sstevel@tonic-gate  */
66667c478bd9Sstevel@tonic-gate 
66677c478bd9Sstevel@tonic-gate static int
66687c478bd9Sstevel@tonic-gate makeDir(char *name)
66697c478bd9Sstevel@tonic-gate {
66707c478bd9Sstevel@tonic-gate 	struct stat buf;
66717c478bd9Sstevel@tonic-gate 
66727c478bd9Sstevel@tonic-gate 	if (access(name, 0) < 0) {  /* name doesn't exist */
66737c478bd9Sstevel@tonic-gate 		if (mkdir(name, 0777) < 0) {
66747c478bd9Sstevel@tonic-gate 			vperror(0, "%s", name);
66757c478bd9Sstevel@tonic-gate 			return (0);
66767c478bd9Sstevel@tonic-gate 		}
66777c478bd9Sstevel@tonic-gate 	} else {		   /* name exists */
66787c478bd9Sstevel@tonic-gate 		if (stat(name, &buf) < 0) {
66797c478bd9Sstevel@tonic-gate 			vperror(0, "%s", name);
66807c478bd9Sstevel@tonic-gate 			return (0);
66817c478bd9Sstevel@tonic-gate 		}
66827c478bd9Sstevel@tonic-gate 
66837c478bd9Sstevel@tonic-gate 		return ((buf.st_mode & S_IFMT) == S_IFDIR);
66847c478bd9Sstevel@tonic-gate 	}
66857c478bd9Sstevel@tonic-gate 
66867c478bd9Sstevel@tonic-gate 	return (1);
66877c478bd9Sstevel@tonic-gate }
66887c478bd9Sstevel@tonic-gate 
66897c478bd9Sstevel@tonic-gate 
66907c478bd9Sstevel@tonic-gate /*
66917c478bd9Sstevel@tonic-gate  * Save this directory and its mtime on the stack, popping and setting
66927c478bd9Sstevel@tonic-gate  * the mtimes of any stacked dirs which aren't parents of this one.
66937c478bd9Sstevel@tonic-gate  * A null name causes the entire stack to be unwound and set.
66947c478bd9Sstevel@tonic-gate  *
66957c478bd9Sstevel@tonic-gate  * Since all the elements of the directory "stack" share a common
66967c478bd9Sstevel@tonic-gate  * prefix, we can make do with one string.  We keep only the current
66977c478bd9Sstevel@tonic-gate  * directory path, with an associated array of mtime's. A negative
66987c478bd9Sstevel@tonic-gate  * mtime means no mtime.
66997c478bd9Sstevel@tonic-gate  *
67007c478bd9Sstevel@tonic-gate  * This stack algorithm is not guaranteed to work for tapes created
67017c478bd9Sstevel@tonic-gate  * with the 'r' function letter, but the vast majority of tapes with
67027c478bd9Sstevel@tonic-gate  * directories are not.  This avoids saving every directory record on
67037c478bd9Sstevel@tonic-gate  * the tape and setting all the times at the end.
67047c478bd9Sstevel@tonic-gate  *
67057c478bd9Sstevel@tonic-gate  * (This was borrowed from the 4.1.3 source, and adapted to the 5.x
67067c478bd9Sstevel@tonic-gate  *  environment)
67077c478bd9Sstevel@tonic-gate  */
67087c478bd9Sstevel@tonic-gate 
67097c478bd9Sstevel@tonic-gate static void
67107c478bd9Sstevel@tonic-gate doDirTimes(char *name, timestruc_t modTime)
67117c478bd9Sstevel@tonic-gate {
67127c478bd9Sstevel@tonic-gate 	static char dirstack[PATH_MAX+2];
67137c478bd9Sstevel@tonic-gate 			/* Add spaces for the last slash and last NULL */
67147c478bd9Sstevel@tonic-gate 	static timestruc_t	modtimes[PATH_MAX+1]; /* hash table */
67157c478bd9Sstevel@tonic-gate 	char *p = dirstack;
67167c478bd9Sstevel@tonic-gate 	char *q = name;
67177c478bd9Sstevel@tonic-gate 	char *savp;
67187c478bd9Sstevel@tonic-gate 
67197c478bd9Sstevel@tonic-gate 	if (q) {
67207c478bd9Sstevel@tonic-gate 		/*
67217c478bd9Sstevel@tonic-gate 		 * Find common prefix
67227c478bd9Sstevel@tonic-gate 		 */
67237c478bd9Sstevel@tonic-gate 
67247c478bd9Sstevel@tonic-gate 		while (*p == *q && *p) {
67257c478bd9Sstevel@tonic-gate 			p++; q++;
67267c478bd9Sstevel@tonic-gate 		}
67277c478bd9Sstevel@tonic-gate 	}
67287c478bd9Sstevel@tonic-gate 
67297c478bd9Sstevel@tonic-gate 	savp = p;
67307c478bd9Sstevel@tonic-gate 	while (*p) {
67317c478bd9Sstevel@tonic-gate 		/*
67327c478bd9Sstevel@tonic-gate 		 * Not a child: unwind the stack, setting the times.
67337c478bd9Sstevel@tonic-gate 		 * The order we do this doesn't matter, so we go "forward."
67347c478bd9Sstevel@tonic-gate 		 */
67357c478bd9Sstevel@tonic-gate 
67367c478bd9Sstevel@tonic-gate 		if (*p == '/')
67377c478bd9Sstevel@tonic-gate 			if (modtimes[p - dirstack].tv_sec >= 0) {
67387c478bd9Sstevel@tonic-gate 				*p = '\0';	 /* zap the slash */
67397c478bd9Sstevel@tonic-gate 				setPathTimes(AT_FDCWD, dirstack,
67407c478bd9Sstevel@tonic-gate 				    modtimes[p - dirstack]);
67417c478bd9Sstevel@tonic-gate 				*p = '/';
67427c478bd9Sstevel@tonic-gate 			}
67437c478bd9Sstevel@tonic-gate 		++p;
67447c478bd9Sstevel@tonic-gate 	}
67457c478bd9Sstevel@tonic-gate 
67467c478bd9Sstevel@tonic-gate 	p = savp;
67477c478bd9Sstevel@tonic-gate 
67487c478bd9Sstevel@tonic-gate 	/*
67497c478bd9Sstevel@tonic-gate 	 *  Push this one on the "stack"
67507c478bd9Sstevel@tonic-gate 	 */
67517c478bd9Sstevel@tonic-gate 
67527c478bd9Sstevel@tonic-gate 	if (q) {
67537c478bd9Sstevel@tonic-gate 
67547c478bd9Sstevel@tonic-gate 		/*
67557c478bd9Sstevel@tonic-gate 		 * Since the name parameter points the dir pathname
67567c478bd9Sstevel@tonic-gate 		 * which is limited only to contain PATH_MAX chars
67577c478bd9Sstevel@tonic-gate 		 * at maximum, we can ignore the overflow case of p.
67587c478bd9Sstevel@tonic-gate 		 */
67597c478bd9Sstevel@tonic-gate 
67607c478bd9Sstevel@tonic-gate 		while ((*p = *q++)) {	/* append the rest of the new dir */
67617c478bd9Sstevel@tonic-gate 			modtimes[p - dirstack].tv_sec = -1;
67627c478bd9Sstevel@tonic-gate 			p++;
67637c478bd9Sstevel@tonic-gate 		}
67647c478bd9Sstevel@tonic-gate 
67657c478bd9Sstevel@tonic-gate 		/*
67667c478bd9Sstevel@tonic-gate 		 * If the tar file had used 'P' or 'E' function modifier,
67677c478bd9Sstevel@tonic-gate 		 * append the last slash.
67687c478bd9Sstevel@tonic-gate 		 */
67697c478bd9Sstevel@tonic-gate 		if (*(p - 1) != '/') {
67707c478bd9Sstevel@tonic-gate 			*p++ = '/';
67717c478bd9Sstevel@tonic-gate 			*p = '\0';
67727c478bd9Sstevel@tonic-gate 		}
67737c478bd9Sstevel@tonic-gate 		/* overwrite the last one */
67747c478bd9Sstevel@tonic-gate 		modtimes[p - dirstack - 1] = modTime;
67757c478bd9Sstevel@tonic-gate 	}
67767c478bd9Sstevel@tonic-gate }
67777c478bd9Sstevel@tonic-gate 
67787c478bd9Sstevel@tonic-gate 
67797c478bd9Sstevel@tonic-gate /*
67807c478bd9Sstevel@tonic-gate  *  setPathTimes - set the modification time for given path.  Return 1 if
67817c478bd9Sstevel@tonic-gate  *                 successful and 0 if not successful.
67827c478bd9Sstevel@tonic-gate  */
67837c478bd9Sstevel@tonic-gate 
67847c478bd9Sstevel@tonic-gate static void
67857c478bd9Sstevel@tonic-gate setPathTimes(int dirfd, char *path, timestruc_t modTime)
67867c478bd9Sstevel@tonic-gate 
67877c478bd9Sstevel@tonic-gate {
67887c478bd9Sstevel@tonic-gate 	struct timeval timebuf[2];
67897c478bd9Sstevel@tonic-gate 
67907c478bd9Sstevel@tonic-gate 	/*
67917c478bd9Sstevel@tonic-gate 	 * futimesat takes an array of two timeval structs.
67927c478bd9Sstevel@tonic-gate 	 * The first entry contains access time.
67937c478bd9Sstevel@tonic-gate 	 * The second entry contains modification time.
67947c478bd9Sstevel@tonic-gate 	 * Unlike a timestruc_t, which uses nanoseconds, timeval uses
67957c478bd9Sstevel@tonic-gate 	 * microseconds.
67967c478bd9Sstevel@tonic-gate 	 */
67977c478bd9Sstevel@tonic-gate 	timebuf[0].tv_sec = time((time_t *)0);
67987c478bd9Sstevel@tonic-gate 	timebuf[0].tv_usec = 0;
67997c478bd9Sstevel@tonic-gate 	timebuf[1].tv_sec = modTime.tv_sec;
68007c478bd9Sstevel@tonic-gate 
68017c478bd9Sstevel@tonic-gate 	/* Extended header: use microseconds */
68027c478bd9Sstevel@tonic-gate 	timebuf[1].tv_usec = (xhdr_flgs & _X_MTIME) ? modTime.tv_nsec/1000 : 0;
68037c478bd9Sstevel@tonic-gate 
68047c478bd9Sstevel@tonic-gate 	if (futimesat(dirfd, path, timebuf) < 0)
68058e4a71aeSRich Burridge 		vperror(0, gettext("can't set time on %s"), path);
68067c478bd9Sstevel@tonic-gate }
68077c478bd9Sstevel@tonic-gate 
68087c478bd9Sstevel@tonic-gate 
68097c478bd9Sstevel@tonic-gate /*
68107c478bd9Sstevel@tonic-gate  * If hflag is set then delete the symbolic link's target.
68117c478bd9Sstevel@tonic-gate  * If !hflag then delete the target.
68127c478bd9Sstevel@tonic-gate  */
68137c478bd9Sstevel@tonic-gate 
68147c478bd9Sstevel@tonic-gate static void
68152c0f0499Slovely delete_target(int fd, char *comp, char *namep)
68167c478bd9Sstevel@tonic-gate {
68177c478bd9Sstevel@tonic-gate 	struct	stat	xtractbuf;
68187c478bd9Sstevel@tonic-gate 	char buf[PATH_MAX + 1];
68197c478bd9Sstevel@tonic-gate 	int n;
68207c478bd9Sstevel@tonic-gate 
68217c478bd9Sstevel@tonic-gate 
68222c0f0499Slovely 	if (unlinkat(fd, comp, AT_REMOVEDIR) < 0) {
68237c478bd9Sstevel@tonic-gate 		if (errno == ENOTDIR && !hflag) {
68242c0f0499Slovely 			(void) unlinkat(fd, comp, 0);
68257c478bd9Sstevel@tonic-gate 		} else if (errno == ENOTDIR && hflag) {
68267c478bd9Sstevel@tonic-gate 			if (!lstat(namep, &xtractbuf)) {
68277c478bd9Sstevel@tonic-gate 				if ((xtractbuf.st_mode & S_IFMT) != S_IFLNK) {
68282c0f0499Slovely 					(void) unlinkat(fd, comp, 0);
68297c478bd9Sstevel@tonic-gate 				} else if ((n = readlink(namep, buf,
68307c478bd9Sstevel@tonic-gate 				    PATH_MAX)) != -1) {
68317c478bd9Sstevel@tonic-gate 					buf[n] = (char)NULL;
68327c478bd9Sstevel@tonic-gate 					(void) unlinkat(fd, buf,
68337c478bd9Sstevel@tonic-gate 					    AT_REMOVEDIR);
68347c478bd9Sstevel@tonic-gate 					if (errno == ENOTDIR)
68357c478bd9Sstevel@tonic-gate 						(void) unlinkat(fd, buf, 0);
68367c478bd9Sstevel@tonic-gate 				} else {
68372c0f0499Slovely 					(void) unlinkat(fd, comp, 0);
68387c478bd9Sstevel@tonic-gate 				}
68397c478bd9Sstevel@tonic-gate 			} else {
68402c0f0499Slovely 				(void) unlinkat(fd, comp, 0);
68417c478bd9Sstevel@tonic-gate 			}
68427c478bd9Sstevel@tonic-gate 		}
68437c478bd9Sstevel@tonic-gate 	}
68447c478bd9Sstevel@tonic-gate }
68457c478bd9Sstevel@tonic-gate 
68467c478bd9Sstevel@tonic-gate 
68477c478bd9Sstevel@tonic-gate /*
68487c478bd9Sstevel@tonic-gate  * ACL changes:
68497c478bd9Sstevel@tonic-gate  *	putfile():
68507c478bd9Sstevel@tonic-gate  *		Get acl info after stat. Write out ancillary file
68517c478bd9Sstevel@tonic-gate  *		before the normal file, i.e. directory, regular, FIFO,
68527c478bd9Sstevel@tonic-gate  *		link, special. If acl count is less than 4, no need to
68537c478bd9Sstevel@tonic-gate  *		create ancillary file. (i.e. standard permission is in
68547c478bd9Sstevel@tonic-gate  *		use.
68557c478bd9Sstevel@tonic-gate  *	doxtract():
68567c478bd9Sstevel@tonic-gate  *		Process ancillary file. Read it in and set acl info.
68577c478bd9Sstevel@tonic-gate  *		watch out for 'o' function modifier.
68587c478bd9Sstevel@tonic-gate  *	't' function letter to display table
68597c478bd9Sstevel@tonic-gate  */
68607c478bd9Sstevel@tonic-gate 
68617c478bd9Sstevel@tonic-gate /*
68627c478bd9Sstevel@tonic-gate  * New functions for ACLs and other security attributes
68637c478bd9Sstevel@tonic-gate  */
68647c478bd9Sstevel@tonic-gate 
68657c478bd9Sstevel@tonic-gate /*
68667c478bd9Sstevel@tonic-gate  * The function appends the new security attribute info to the end of
68677c478bd9Sstevel@tonic-gate  * existing secinfo.
68687c478bd9Sstevel@tonic-gate  */
68697c478bd9Sstevel@tonic-gate int
68707c478bd9Sstevel@tonic-gate append_secattr(
68717c478bd9Sstevel@tonic-gate 	char	 **secinfo,	/* existing security info */
68727c478bd9Sstevel@tonic-gate 	int	 *secinfo_len,	/* length of existing security info */
687345916cd2Sjpk 	int	 size,		/* new attribute size: unit depends on type */
687445916cd2Sjpk 	char	*attrtext,	/* new attribute text */
687545916cd2Sjpk 	char	 attr_type)	/* new attribute type */
68767c478bd9Sstevel@tonic-gate {
68777c478bd9Sstevel@tonic-gate 	char	*new_secinfo;
68787c478bd9Sstevel@tonic-gate 	int	newattrsize;
68797c478bd9Sstevel@tonic-gate 	int	oldsize;
688045916cd2Sjpk 	struct sec_attr	*attr;
68817c478bd9Sstevel@tonic-gate 
68827c478bd9Sstevel@tonic-gate 	/* no need to add */
688345916cd2Sjpk 	if (attr_type != DIR_TYPE) {
688445916cd2Sjpk 		if (attrtext == NULL)
68857c478bd9Sstevel@tonic-gate 			return (0);
688645916cd2Sjpk 	}
68877c478bd9Sstevel@tonic-gate 
688845916cd2Sjpk 	switch (attr_type) {
688945916cd2Sjpk 	case UFSD_ACL:
689045916cd2Sjpk 	case ACE_ACL:
68917c478bd9Sstevel@tonic-gate 		if (attrtext == NULL) {
68928e4a71aeSRich Burridge 			(void) fprintf(stderr, gettext("acltotext failed\n"));
68937c478bd9Sstevel@tonic-gate 			return (-1);
68947c478bd9Sstevel@tonic-gate 		}
68957c478bd9Sstevel@tonic-gate 		/* header: type + size = 8 */
68967c478bd9Sstevel@tonic-gate 		newattrsize = 8 + (int)strlen(attrtext) + 1;
68977c478bd9Sstevel@tonic-gate 		attr = (struct sec_attr *)malloc(newattrsize);
68987c478bd9Sstevel@tonic-gate 		if (attr == NULL) {
68998e4a71aeSRich Burridge 			(void) fprintf(stderr,
69008e4a71aeSRich Burridge 			    gettext("can't allocate memory\n"));
69017c478bd9Sstevel@tonic-gate 			return (-1);
69027c478bd9Sstevel@tonic-gate 		}
690345916cd2Sjpk 		attr->attr_type = attr_type;
69047c478bd9Sstevel@tonic-gate 		(void) sprintf(attr->attr_len,
690545916cd2Sjpk 		    "%06o", size); /* acl entry count */
69067c478bd9Sstevel@tonic-gate 		(void) strcpy((char *)&attr->attr_info[0], attrtext);
69077c478bd9Sstevel@tonic-gate 		free(attrtext);
69087c478bd9Sstevel@tonic-gate 		break;
69097c478bd9Sstevel@tonic-gate 
691045916cd2Sjpk 	/* Trusted Extensions */
691145916cd2Sjpk 	case DIR_TYPE:
691245916cd2Sjpk 	case LBL_TYPE:
691345916cd2Sjpk 		newattrsize = sizeof (struct sec_attr) + strlen(attrtext);
691445916cd2Sjpk 		attr = (struct sec_attr *)malloc(newattrsize);
691545916cd2Sjpk 		if (attr == NULL) {
691645916cd2Sjpk 			(void) fprintf(stderr,
691745916cd2Sjpk 			gettext("can't allocate memory\n"));
691845916cd2Sjpk 			return (-1);
691945916cd2Sjpk 		}
692045916cd2Sjpk 		attr->attr_type = attr_type;
692145916cd2Sjpk 		(void) sprintf(attr->attr_len,
692245916cd2Sjpk 		    "%06d", size); /* len of attr data */
692345916cd2Sjpk 		(void) strcpy((char *)&attr->attr_info[0], attrtext);
692445916cd2Sjpk 		break;
69257c478bd9Sstevel@tonic-gate 
69267c478bd9Sstevel@tonic-gate 	default:
69278e4a71aeSRich Burridge 		(void) fprintf(stderr,
69288e4a71aeSRich Burridge 		    gettext("unrecognized attribute type\n"));
69297c478bd9Sstevel@tonic-gate 		return (-1);
69307c478bd9Sstevel@tonic-gate 	}
69317c478bd9Sstevel@tonic-gate 
69327c478bd9Sstevel@tonic-gate 	/* old security info + new attr header(8) + new attr */
69337c478bd9Sstevel@tonic-gate 	oldsize = *secinfo_len;
69347c478bd9Sstevel@tonic-gate 	*secinfo_len += newattrsize;
69357c478bd9Sstevel@tonic-gate 	new_secinfo = (char *)malloc(*secinfo_len);
69367c478bd9Sstevel@tonic-gate 	if (new_secinfo == NULL) {
69378e4a71aeSRich Burridge 		(void) fprintf(stderr, gettext("can't allocate memory\n"));
69387c478bd9Sstevel@tonic-gate 		*secinfo_len -= newattrsize;
693945916cd2Sjpk 		free(attr);
69407c478bd9Sstevel@tonic-gate 		return (-1);
69417c478bd9Sstevel@tonic-gate 	}
69427c478bd9Sstevel@tonic-gate 
69437c478bd9Sstevel@tonic-gate 	(void) memcpy(new_secinfo, *secinfo, oldsize);
69447c478bd9Sstevel@tonic-gate 	(void) memcpy(new_secinfo + oldsize, attr, newattrsize);
69457c478bd9Sstevel@tonic-gate 
69467c478bd9Sstevel@tonic-gate 	free(*secinfo);
694745916cd2Sjpk 	free(attr);
69487c478bd9Sstevel@tonic-gate 	*secinfo = new_secinfo;
69497c478bd9Sstevel@tonic-gate 	return (0);
69507c478bd9Sstevel@tonic-gate }
69517c478bd9Sstevel@tonic-gate 
69527c478bd9Sstevel@tonic-gate /*
69537c478bd9Sstevel@tonic-gate  * write_ancillary(): write out an ancillary file.
69547c478bd9Sstevel@tonic-gate  *      The file has the same header as normal file except the type and size
69557c478bd9Sstevel@tonic-gate  *      fields. The type is 'A' and size is the sum of all attributes
69567c478bd9Sstevel@tonic-gate  *	in bytes.
69577c478bd9Sstevel@tonic-gate  *	The body contains a list of attribute type, size and info. Currently,
69587c478bd9Sstevel@tonic-gate  *	there is only ACL info.  This file is put before the normal file.
69597c478bd9Sstevel@tonic-gate  */
69607c478bd9Sstevel@tonic-gate void
69617c478bd9Sstevel@tonic-gate write_ancillary(union hblock *dblockp, char *secinfo, int len, char hdrtype)
69627c478bd9Sstevel@tonic-gate {
69637c478bd9Sstevel@tonic-gate 	long    blocks;
69647c478bd9Sstevel@tonic-gate 	int	savflag;
69657c478bd9Sstevel@tonic-gate 	int	savsize;
69667c478bd9Sstevel@tonic-gate 
69677c478bd9Sstevel@tonic-gate 	/* Just tranditional permissions or no security attribute info */
69687c478bd9Sstevel@tonic-gate 	if (len == 0 || secinfo == NULL)
69697c478bd9Sstevel@tonic-gate 		return;
69707c478bd9Sstevel@tonic-gate 
69717c478bd9Sstevel@tonic-gate 	/* save flag and size */
69727c478bd9Sstevel@tonic-gate 	savflag = (dblockp->dbuf).typeflag;
69737c478bd9Sstevel@tonic-gate 	(void) sscanf(dblockp->dbuf.size, "%12o", (uint_t *)&savsize);
69747c478bd9Sstevel@tonic-gate 
69757c478bd9Sstevel@tonic-gate 	/* special flag for ancillary file */
69767c478bd9Sstevel@tonic-gate 	if (hdrtype == _XATTR_HDRTYPE)
69777c478bd9Sstevel@tonic-gate 		dblockp->dbuf.typeflag = _XATTR_HDRTYPE;
69787c478bd9Sstevel@tonic-gate 	else
69797c478bd9Sstevel@tonic-gate 		dblockp->dbuf.typeflag = 'A';
69807c478bd9Sstevel@tonic-gate 
69817c478bd9Sstevel@tonic-gate 	/* for pre-2.5 versions of tar, need to make sure */
69827c478bd9Sstevel@tonic-gate 	/* the ACL file is readable			  */
69837c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.mode, "%07lo",
69847c478bd9Sstevel@tonic-gate 	    (stbuf.st_mode & POSIXMODES) | 0000200);
69857c478bd9Sstevel@tonic-gate 	(void) sprintf(dblockp->dbuf.size, "%011o", len);
69867c478bd9Sstevel@tonic-gate 	(void) sprintf(dblockp->dbuf.chksum, "%07o", checksum(dblockp));
69877c478bd9Sstevel@tonic-gate 
69887c478bd9Sstevel@tonic-gate 	/* write out the header */
69897c478bd9Sstevel@tonic-gate 	(void) writetbuf((char *)dblockp, 1);
69907c478bd9Sstevel@tonic-gate 
69917c478bd9Sstevel@tonic-gate 	/* write out security info */
69927c478bd9Sstevel@tonic-gate 	blocks = TBLOCKS(len);
69937c478bd9Sstevel@tonic-gate 	(void) writetbuf((char *)secinfo, (int)blocks);
69947c478bd9Sstevel@tonic-gate 
69957c478bd9Sstevel@tonic-gate 	/* restore mode, flag and size */
69967c478bd9Sstevel@tonic-gate 	(void) sprintf(dblock.dbuf.mode, "%07lo", stbuf.st_mode & POSIXMODES);
69977c478bd9Sstevel@tonic-gate 	dblockp->dbuf.typeflag = savflag;
69987c478bd9Sstevel@tonic-gate 	(void) sprintf(dblockp->dbuf.size, "%011o", savsize);
69997c478bd9Sstevel@tonic-gate }
70007c478bd9Sstevel@tonic-gate 
70017c478bd9Sstevel@tonic-gate /*
70027c478bd9Sstevel@tonic-gate  * Read the data record for extended headers and then the regular header.
70037c478bd9Sstevel@tonic-gate  * The data are read into the buffer and then null-terminated.  Entries
7004e765faefSRich Burridge  * for typeflag 'X' extended headers are of the format:
70057c478bd9Sstevel@tonic-gate  * 	"%d %s=%s\n"
70067c478bd9Sstevel@tonic-gate  *
70077c478bd9Sstevel@tonic-gate  * When an extended header record is found, the extended header must
70087c478bd9Sstevel@tonic-gate  * be processed and its values used to override the values in the
70097c478bd9Sstevel@tonic-gate  * normal header.  The way this is done is to process the extended
70107c478bd9Sstevel@tonic-gate  * header data record and set the data values, then call getdir
70117c478bd9Sstevel@tonic-gate  * to process the regular header, then then to reconcile the two
70127c478bd9Sstevel@tonic-gate  * sets of data.
70137c478bd9Sstevel@tonic-gate  */
70147c478bd9Sstevel@tonic-gate 
70157c478bd9Sstevel@tonic-gate static int
70167c478bd9Sstevel@tonic-gate get_xdata(void)
70177c478bd9Sstevel@tonic-gate {
70187c478bd9Sstevel@tonic-gate 	struct keylist_pair {
70197c478bd9Sstevel@tonic-gate 		int keynum;
70207c478bd9Sstevel@tonic-gate 		char *keylist;
70217c478bd9Sstevel@tonic-gate 	}	keylist_pair[] = {	_X_DEVMAJOR, "SUN.devmajor",
70227c478bd9Sstevel@tonic-gate 					_X_DEVMINOR, "SUN.devminor",
70237c478bd9Sstevel@tonic-gate 					_X_GID, "gid",
70247c478bd9Sstevel@tonic-gate 					_X_GNAME, "gname",
70257c478bd9Sstevel@tonic-gate 					_X_LINKPATH, "linkpath",
70267c478bd9Sstevel@tonic-gate 					_X_PATH, "path",
70277c478bd9Sstevel@tonic-gate 					_X_SIZE, "size",
70287c478bd9Sstevel@tonic-gate 					_X_UID, "uid",
70297c478bd9Sstevel@tonic-gate 					_X_UNAME, "uname",
70307c478bd9Sstevel@tonic-gate 					_X_MTIME, "mtime",
70317c478bd9Sstevel@tonic-gate 					_X_LAST, "NULL" };
70327c478bd9Sstevel@tonic-gate 	char		*lineloc;
70337c478bd9Sstevel@tonic-gate 	int		length, i;
70347c478bd9Sstevel@tonic-gate 	char		*keyword, *value;
70357c478bd9Sstevel@tonic-gate 	blkcnt_t	nblocks;
70367c478bd9Sstevel@tonic-gate 	int		bufneeded;
70377c478bd9Sstevel@tonic-gate 	int		errors;
70387c478bd9Sstevel@tonic-gate 
7039123523f8Sas158974 	(void) memset(&Xtarhdr, 0, sizeof (Xtarhdr));
70407c478bd9Sstevel@tonic-gate 	xhdr_count++;
70417c478bd9Sstevel@tonic-gate 	errors = 0;
70427c478bd9Sstevel@tonic-gate 
70437c478bd9Sstevel@tonic-gate 	nblocks = TBLOCKS(stbuf.st_size);
70447c478bd9Sstevel@tonic-gate 	bufneeded = nblocks * TBLOCK;
70457c478bd9Sstevel@tonic-gate 	if (bufneeded >= xrec_size) {
70467c478bd9Sstevel@tonic-gate 		free(xrec_ptr);
70477c478bd9Sstevel@tonic-gate 		xrec_size = bufneeded + 1;
70487c478bd9Sstevel@tonic-gate 		if ((xrec_ptr = malloc(xrec_size)) == NULL)
70497c478bd9Sstevel@tonic-gate 			fatal(gettext("cannot allocate buffer"));
70507c478bd9Sstevel@tonic-gate 	}
70517c478bd9Sstevel@tonic-gate 
70527c478bd9Sstevel@tonic-gate 	lineloc = xrec_ptr;
70537c478bd9Sstevel@tonic-gate 
70547c478bd9Sstevel@tonic-gate 	while (nblocks-- > 0) {
70557c478bd9Sstevel@tonic-gate 		readtape(lineloc);
70567c478bd9Sstevel@tonic-gate 		lineloc += TBLOCK;
70577c478bd9Sstevel@tonic-gate 	}
70587c478bd9Sstevel@tonic-gate 	lineloc = xrec_ptr;
70597c478bd9Sstevel@tonic-gate 	xrec_ptr[stbuf.st_size] = '\0';
70607c478bd9Sstevel@tonic-gate 	while (lineloc < xrec_ptr + stbuf.st_size) {
7061e765faefSRich Burridge 		if (dblock.dbuf.typeflag == 'L') {
7062e765faefSRich Burridge 			length = xrec_size;
7063e765faefSRich Burridge 			keyword = "path";
7064e765faefSRich Burridge 			value = lineloc;
7065e765faefSRich Burridge 		} else {
70667c478bd9Sstevel@tonic-gate 			length = atoi(lineloc);
70677c478bd9Sstevel@tonic-gate 			*(lineloc + length - 1) = '\0';
70687c478bd9Sstevel@tonic-gate 			keyword = strchr(lineloc, ' ') + 1;
70697c478bd9Sstevel@tonic-gate 			value = strchr(keyword, '=') + 1;
70707c478bd9Sstevel@tonic-gate 			*(value - 1) = '\0';
7071e765faefSRich Burridge 		}
70727c478bd9Sstevel@tonic-gate 		i = 0;
70737c478bd9Sstevel@tonic-gate 		lineloc += length;
70747c478bd9Sstevel@tonic-gate 		while (keylist_pair[i].keynum != (int)_X_LAST) {
70757c478bd9Sstevel@tonic-gate 			if (strcmp(keyword, keylist_pair[i].keylist) == 0)
70767c478bd9Sstevel@tonic-gate 				break;
70777c478bd9Sstevel@tonic-gate 			i++;
70787c478bd9Sstevel@tonic-gate 		}
70797c478bd9Sstevel@tonic-gate 		errno = 0;
70807c478bd9Sstevel@tonic-gate 		switch (keylist_pair[i].keynum) {
70817c478bd9Sstevel@tonic-gate 		case _X_DEVMAJOR:
70827c478bd9Sstevel@tonic-gate 			Xtarhdr.x_devmajor = (major_t)strtoul(value, NULL, 0);
70837c478bd9Sstevel@tonic-gate 			if (errno) {
70847c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
70857c478bd9Sstevel@tonic-gate 				    "tar: Extended header major value error "
70867c478bd9Sstevel@tonic-gate 				    "for file # %llu.\n"), xhdr_count);
70877c478bd9Sstevel@tonic-gate 				errors++;
70887c478bd9Sstevel@tonic-gate 			} else
70897c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_DEVMAJOR;
70907c478bd9Sstevel@tonic-gate 			break;
70917c478bd9Sstevel@tonic-gate 		case _X_DEVMINOR:
70927c478bd9Sstevel@tonic-gate 			Xtarhdr.x_devminor = (minor_t)strtoul(value, NULL, 0);
70937c478bd9Sstevel@tonic-gate 			if (errno) {
70947c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
70957c478bd9Sstevel@tonic-gate 				    "tar: Extended header minor value error "
70967c478bd9Sstevel@tonic-gate 				    "for file # %llu.\n"), xhdr_count);
70977c478bd9Sstevel@tonic-gate 				errors++;
70987c478bd9Sstevel@tonic-gate 			} else
70997c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_DEVMINOR;
71007c478bd9Sstevel@tonic-gate 			break;
71017c478bd9Sstevel@tonic-gate 		case _X_GID:
71027c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_GID;
71037c478bd9Sstevel@tonic-gate 			Xtarhdr.x_gid = strtol(value, NULL, 0);
71047c478bd9Sstevel@tonic-gate 			if ((errno) || (Xtarhdr.x_gid > UID_MAX)) {
71057c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
71067c478bd9Sstevel@tonic-gate 				    "tar: Extended header gid value error "
71077c478bd9Sstevel@tonic-gate 				    "for file # %llu.\n"), xhdr_count);
71087c478bd9Sstevel@tonic-gate 				Xtarhdr.x_gid = GID_NOBODY;
71097c478bd9Sstevel@tonic-gate 			}
71107c478bd9Sstevel@tonic-gate 			break;
71117c478bd9Sstevel@tonic-gate 		case _X_GNAME:
71127c478bd9Sstevel@tonic-gate 			if (utf8_local("gname", &Xtarhdr.x_gname,
71137c478bd9Sstevel@tonic-gate 			    local_gname, value, _POSIX_NAME_MAX) == 0)
71147c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_GNAME;
71157c478bd9Sstevel@tonic-gate 			break;
71167c478bd9Sstevel@tonic-gate 		case _X_LINKPATH:
71177c478bd9Sstevel@tonic-gate 			if (utf8_local("linkpath", &Xtarhdr.x_linkpath,
71187c478bd9Sstevel@tonic-gate 			    local_linkpath, value, PATH_MAX) == 0)
71197c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_LINKPATH;
71207c478bd9Sstevel@tonic-gate 			else
71217c478bd9Sstevel@tonic-gate 				errors++;
71227c478bd9Sstevel@tonic-gate 			break;
71237c478bd9Sstevel@tonic-gate 		case _X_PATH:
71247c478bd9Sstevel@tonic-gate 			if (utf8_local("path", &Xtarhdr.x_path,
71257c478bd9Sstevel@tonic-gate 			    local_path, value, PATH_MAX) == 0)
71267c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_PATH;
71277c478bd9Sstevel@tonic-gate 			else
71287c478bd9Sstevel@tonic-gate 				errors++;
71297c478bd9Sstevel@tonic-gate 			break;
71307c478bd9Sstevel@tonic-gate 		case _X_SIZE:
71317c478bd9Sstevel@tonic-gate 			Xtarhdr.x_filesz = strtoull(value, NULL, 0);
71327c478bd9Sstevel@tonic-gate 			if (errno) {
71337c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
71347c478bd9Sstevel@tonic-gate 				    "tar: Extended header invalid filesize "
71357c478bd9Sstevel@tonic-gate 				    "for file # %llu.\n"), xhdr_count);
71367c478bd9Sstevel@tonic-gate 				errors++;
71377c478bd9Sstevel@tonic-gate 			} else
71387c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_SIZE;
71397c478bd9Sstevel@tonic-gate 			break;
71407c478bd9Sstevel@tonic-gate 		case _X_UID:
71417c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_UID;
71427c478bd9Sstevel@tonic-gate 			Xtarhdr.x_uid = strtol(value, NULL, 0);
71437c478bd9Sstevel@tonic-gate 			if ((errno) || (Xtarhdr.x_uid > UID_MAX)) {
71447c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
71457c478bd9Sstevel@tonic-gate 				    "tar: Extended header uid value error "
71467c478bd9Sstevel@tonic-gate 				    "for file # %llu.\n"), xhdr_count);
71477c478bd9Sstevel@tonic-gate 				Xtarhdr.x_uid = UID_NOBODY;
71487c478bd9Sstevel@tonic-gate 			}
71497c478bd9Sstevel@tonic-gate 			break;
71507c478bd9Sstevel@tonic-gate 		case _X_UNAME:
71517c478bd9Sstevel@tonic-gate 			if (utf8_local("uname", &Xtarhdr.x_uname,
71527c478bd9Sstevel@tonic-gate 			    local_uname, value, _POSIX_NAME_MAX) == 0)
71537c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_UNAME;
71547c478bd9Sstevel@tonic-gate 			break;
71557c478bd9Sstevel@tonic-gate 		case _X_MTIME:
71567c478bd9Sstevel@tonic-gate 			get_xtime(value, &(Xtarhdr.x_mtime));
71577c478bd9Sstevel@tonic-gate 			if (errno)
71587c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
71597c478bd9Sstevel@tonic-gate 				    "tar: Extended header modification time "
71607c478bd9Sstevel@tonic-gate 				    "value error for file # %llu.\n"),
71617c478bd9Sstevel@tonic-gate 				    xhdr_count);
71627c478bd9Sstevel@tonic-gate 			else
71637c478bd9Sstevel@tonic-gate 				xhdr_flgs |= _X_MTIME;
71647c478bd9Sstevel@tonic-gate 			break;
71657c478bd9Sstevel@tonic-gate 		default:
71667c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
71677c478bd9Sstevel@tonic-gate 			    gettext("tar:  unrecognized extended"
71687c478bd9Sstevel@tonic-gate 			    " header keyword '%s'.  Ignored.\n"), keyword);
71697c478bd9Sstevel@tonic-gate 			break;
71707c478bd9Sstevel@tonic-gate 		}
71717c478bd9Sstevel@tonic-gate 	}
71727c478bd9Sstevel@tonic-gate 
71737c478bd9Sstevel@tonic-gate 	getdir();	/* get regular header */
71747c478bd9Sstevel@tonic-gate 	if (errors && errflag)
71757c478bd9Sstevel@tonic-gate 		done(1);
71767c478bd9Sstevel@tonic-gate 	else
71777c478bd9Sstevel@tonic-gate 		if (errors)
71787c478bd9Sstevel@tonic-gate 			Errflg = 1;
71797c478bd9Sstevel@tonic-gate 	return (errors);
71807c478bd9Sstevel@tonic-gate }
71817c478bd9Sstevel@tonic-gate 
71827c478bd9Sstevel@tonic-gate /*
7183123523f8Sas158974  * load_info_from_xtarhdr - sets Gen and stbuf variables from
7184123523f8Sas158974  *	extended header
7185123523f8Sas158974  *	load_info_from_xtarhdr(flag, xhdrp);
7186123523f8Sas158974  *	u_longlong_t flag;	xhdr_flgs
7187123523f8Sas158974  *	struct xtar_hdr *xhdrp; pointer to extended header
7188123523f8Sas158974  *	NOTE:	called when typeflag is not 'A' and xhdr_flgs
7189123523f8Sas158974  *		is set.
7190123523f8Sas158974  */
7191123523f8Sas158974 static void
7192123523f8Sas158974 load_info_from_xtarhdr(u_longlong_t flag, struct xtar_hdr *xhdrp)
7193123523f8Sas158974 {
7194123523f8Sas158974 	if (flag & _X_DEVMAJOR) {
7195123523f8Sas158974 		Gen.g_devmajor = xhdrp->x_devmajor;
7196123523f8Sas158974 	}
7197123523f8Sas158974 	if (flag & _X_DEVMINOR) {
7198123523f8Sas158974 		Gen.g_devminor = xhdrp->x_devminor;
7199123523f8Sas158974 	}
7200123523f8Sas158974 	if (flag & _X_GID) {
7201123523f8Sas158974 		Gen.g_gid = xhdrp->x_gid;
7202123523f8Sas158974 		stbuf.st_gid = xhdrp->x_gid;
7203123523f8Sas158974 	}
7204123523f8Sas158974 	if (flag & _X_UID) {
7205123523f8Sas158974 		Gen.g_uid = xhdrp->x_uid;
7206123523f8Sas158974 		stbuf.st_uid  = xhdrp->x_uid;
7207123523f8Sas158974 	}
7208123523f8Sas158974 	if (flag & _X_SIZE) {
7209123523f8Sas158974 		Gen.g_filesz = xhdrp->x_filesz;
7210123523f8Sas158974 		stbuf.st_size = xhdrp->x_filesz;
7211123523f8Sas158974 	}
7212123523f8Sas158974 	if (flag & _X_MTIME) {
7213123523f8Sas158974 		Gen.g_mtime = xhdrp->x_mtime.tv_sec;
7214123523f8Sas158974 		stbuf.st_mtim.tv_sec = xhdrp->x_mtime.tv_sec;
7215123523f8Sas158974 		stbuf.st_mtim.tv_nsec = xhdrp->x_mtime.tv_nsec;
7216123523f8Sas158974 	}
7217123523f8Sas158974 }
7218123523f8Sas158974 
7219123523f8Sas158974 /*
72207c478bd9Sstevel@tonic-gate  * gen_num creates a string from a keyword and an usigned long long in the
72217c478bd9Sstevel@tonic-gate  * format:  %d %s=%s\n
72227c478bd9Sstevel@tonic-gate  * This is part of the extended header data record.
72237c478bd9Sstevel@tonic-gate  */
72247c478bd9Sstevel@tonic-gate 
72257c478bd9Sstevel@tonic-gate void
72267c478bd9Sstevel@tonic-gate gen_num(const char *keyword, const u_longlong_t number)
72277c478bd9Sstevel@tonic-gate {
72287c478bd9Sstevel@tonic-gate 	char	save_val[ULONGLONG_MAX_DIGITS + 1];
72297c478bd9Sstevel@tonic-gate 	int	len;
72307c478bd9Sstevel@tonic-gate 	char	*curr_ptr;
72317c478bd9Sstevel@tonic-gate 
72327c478bd9Sstevel@tonic-gate 	(void) sprintf(save_val, "%llu", number);
72337c478bd9Sstevel@tonic-gate 	/*
72347c478bd9Sstevel@tonic-gate 	 * len = length of entire line, including itself.  len will be
72357c478bd9Sstevel@tonic-gate 	 * two digits.  So, add the string lengths plus the length of len,
72367c478bd9Sstevel@tonic-gate 	 * plus a blank, an equal sign, and a newline.
72377c478bd9Sstevel@tonic-gate 	 */
72387c478bd9Sstevel@tonic-gate 	len = strlen(save_val) + strlen(keyword) + 5;
72397c478bd9Sstevel@tonic-gate 	if (xrec_offset + len > xrec_size) {
72407c478bd9Sstevel@tonic-gate 		if (((curr_ptr = realloc(xrec_ptr, 2 * xrec_size)) == NULL))
72417c478bd9Sstevel@tonic-gate 			fatal(gettext(
72427c478bd9Sstevel@tonic-gate 			    "cannot allocate extended header buffer"));
72437c478bd9Sstevel@tonic-gate 		xrec_ptr = curr_ptr;
72447c478bd9Sstevel@tonic-gate 		xrec_size *= 2;
72457c478bd9Sstevel@tonic-gate 	}
72467c478bd9Sstevel@tonic-gate 	(void) sprintf(&xrec_ptr[xrec_offset],
72477c478bd9Sstevel@tonic-gate 	    "%d %s=%s\n", len, keyword, save_val);
72487c478bd9Sstevel@tonic-gate 	xrec_offset += len;
72497c478bd9Sstevel@tonic-gate }
72507c478bd9Sstevel@tonic-gate 
72517c478bd9Sstevel@tonic-gate /*
72527c478bd9Sstevel@tonic-gate  * gen_date creates a string from a keyword and a timestruc_t in the
72537c478bd9Sstevel@tonic-gate  * format:  %d %s=%s\n
72547c478bd9Sstevel@tonic-gate  * This is part of the extended header data record.
72557c478bd9Sstevel@tonic-gate  * Currently, granularity is only microseconds, so the low-order three digits
72567c478bd9Sstevel@tonic-gate  * will be truncated.
72577c478bd9Sstevel@tonic-gate  */
72587c478bd9Sstevel@tonic-gate 
72597c478bd9Sstevel@tonic-gate void
72607c478bd9Sstevel@tonic-gate gen_date(const char *keyword, const timestruc_t time_value)
72617c478bd9Sstevel@tonic-gate {
72627c478bd9Sstevel@tonic-gate 	/* Allow for <seconds>.<nanoseconds>\n */
72637c478bd9Sstevel@tonic-gate 	char	save_val[TIME_MAX_DIGITS + LONG_MAX_DIGITS + 2];
72647c478bd9Sstevel@tonic-gate 	int	len;
72657c478bd9Sstevel@tonic-gate 	char	*curr_ptr;
72667c478bd9Sstevel@tonic-gate 
72677c478bd9Sstevel@tonic-gate 	(void) sprintf(save_val, "%ld", time_value.tv_sec);
72687c478bd9Sstevel@tonic-gate 	len = strlen(save_val);
72697c478bd9Sstevel@tonic-gate 	save_val[len] = '.';
72707c478bd9Sstevel@tonic-gate 	(void) sprintf(&save_val[len + 1], "%9.9ld", time_value.tv_nsec);
72717c478bd9Sstevel@tonic-gate 
72727c478bd9Sstevel@tonic-gate 	/*
72737c478bd9Sstevel@tonic-gate 	 * len = length of entire line, including itself.  len will be
72747c478bd9Sstevel@tonic-gate 	 * two digits.  So, add the string lengths plus the length of len,
72757c478bd9Sstevel@tonic-gate 	 * plus a blank, an equal sign, and a newline.
72767c478bd9Sstevel@tonic-gate 	 */
72777c478bd9Sstevel@tonic-gate 	len = strlen(save_val) + strlen(keyword) + 5;
72787c478bd9Sstevel@tonic-gate 	if (xrec_offset + len > xrec_size) {
72797c478bd9Sstevel@tonic-gate 		if (((curr_ptr = realloc(xrec_ptr, 2 * xrec_size)) == NULL))
72807c478bd9Sstevel@tonic-gate 			fatal(gettext(
72817c478bd9Sstevel@tonic-gate 			    "cannot allocate extended header buffer"));
72827c478bd9Sstevel@tonic-gate 		xrec_ptr = curr_ptr;
72837c478bd9Sstevel@tonic-gate 		xrec_size *= 2;
72847c478bd9Sstevel@tonic-gate 	}
72857c478bd9Sstevel@tonic-gate 	(void) sprintf(&xrec_ptr[xrec_offset],
72867c478bd9Sstevel@tonic-gate 	    "%d %s=%s\n", len, keyword, save_val);
72877c478bd9Sstevel@tonic-gate 	xrec_offset += len;
72887c478bd9Sstevel@tonic-gate }
72897c478bd9Sstevel@tonic-gate 
72907c478bd9Sstevel@tonic-gate /*
72917c478bd9Sstevel@tonic-gate  * gen_string creates a string from a keyword and a char * in the
72927c478bd9Sstevel@tonic-gate  * format:  %d %s=%s\n
72937c478bd9Sstevel@tonic-gate  * This is part of the extended header data record.
72947c478bd9Sstevel@tonic-gate  */
72957c478bd9Sstevel@tonic-gate 
72967c478bd9Sstevel@tonic-gate void
72977c478bd9Sstevel@tonic-gate gen_string(const char *keyword, const char *value)
72987c478bd9Sstevel@tonic-gate {
72997c478bd9Sstevel@tonic-gate 	int	len;
73007c478bd9Sstevel@tonic-gate 	char	*curr_ptr;
73017c478bd9Sstevel@tonic-gate 
73027c478bd9Sstevel@tonic-gate 	/*
73037c478bd9Sstevel@tonic-gate 	 * len = length of entire line, including itself.  The character length
73047c478bd9Sstevel@tonic-gate 	 * of len must be 1-4 characters, because the maximum size of the path
73057c478bd9Sstevel@tonic-gate 	 * or the name is PATH_MAX, which is 1024.  So, assume 1 character
73067c478bd9Sstevel@tonic-gate 	 * for len, one for the space, one for the "=", and one for the newline.
73077c478bd9Sstevel@tonic-gate 	 * Then adjust as needed.
73087c478bd9Sstevel@tonic-gate 	 */
73097c478bd9Sstevel@tonic-gate 	/* LINTED constant expression */
73107c478bd9Sstevel@tonic-gate 	assert(PATH_MAX <= 9996);
73117c478bd9Sstevel@tonic-gate 	len = strlen(value) + strlen(keyword) + 4;
73127c478bd9Sstevel@tonic-gate 	if (len > 997)
73137c478bd9Sstevel@tonic-gate 		len += 3;
73147c478bd9Sstevel@tonic-gate 	else if (len > 98)
73157c478bd9Sstevel@tonic-gate 		len += 2;
73167c478bd9Sstevel@tonic-gate 	else if (len > 9)
73177c478bd9Sstevel@tonic-gate 		len += 1;
73187c478bd9Sstevel@tonic-gate 	if (xrec_offset + len > xrec_size) {
73197c478bd9Sstevel@tonic-gate 		if (((curr_ptr = realloc(xrec_ptr, 2 * xrec_size)) == NULL))
73207c478bd9Sstevel@tonic-gate 			fatal(gettext(
73217c478bd9Sstevel@tonic-gate 			    "cannot allocate extended header buffer"));
73227c478bd9Sstevel@tonic-gate 		xrec_ptr = curr_ptr;
73237c478bd9Sstevel@tonic-gate 		xrec_size *= 2;
73247c478bd9Sstevel@tonic-gate 	}
73257c478bd9Sstevel@tonic-gate #ifdef XHDR_DEBUG
73267c478bd9Sstevel@tonic-gate 	if (strcmp(keyword+1, "name") != 0)
73277c478bd9Sstevel@tonic-gate #endif
73287c478bd9Sstevel@tonic-gate 	(void) sprintf(&xrec_ptr[xrec_offset],
73297c478bd9Sstevel@tonic-gate 	    "%d %s=%s\n", len, keyword, value);
73307c478bd9Sstevel@tonic-gate #ifdef XHDR_DEBUG
73317c478bd9Sstevel@tonic-gate 	else {
73327c478bd9Sstevel@tonic-gate 	len += 11;
73337c478bd9Sstevel@tonic-gate 	(void) sprintf(&xrec_ptr[xrec_offset],
73347c478bd9Sstevel@tonic-gate 	    "%d %s=%snametoolong\n", len, keyword, value);
73357c478bd9Sstevel@tonic-gate 	}
73367c478bd9Sstevel@tonic-gate #endif
73377c478bd9Sstevel@tonic-gate 	xrec_offset += len;
73387c478bd9Sstevel@tonic-gate }
73397c478bd9Sstevel@tonic-gate 
73407c478bd9Sstevel@tonic-gate /*
73417c478bd9Sstevel@tonic-gate  * Convert time found in the extended header data to seconds and nanoseconds.
73427c478bd9Sstevel@tonic-gate  */
73437c478bd9Sstevel@tonic-gate 
73447c478bd9Sstevel@tonic-gate void
73457c478bd9Sstevel@tonic-gate get_xtime(char *value, timestruc_t *xtime)
73467c478bd9Sstevel@tonic-gate {
73477c478bd9Sstevel@tonic-gate 	char nanosec[10];
73487c478bd9Sstevel@tonic-gate 	char *period;
73497c478bd9Sstevel@tonic-gate 	int i;
73507c478bd9Sstevel@tonic-gate 
73517c478bd9Sstevel@tonic-gate 	(void) memset(nanosec, '0', 9);
73527c478bd9Sstevel@tonic-gate 	nanosec[9] = '\0';
73537c478bd9Sstevel@tonic-gate 
73547c478bd9Sstevel@tonic-gate 	period = strchr(value, '.');
73557c478bd9Sstevel@tonic-gate 	if (period != NULL)
73567c478bd9Sstevel@tonic-gate 		period[0] = '\0';
73577c478bd9Sstevel@tonic-gate 	xtime->tv_sec = strtol(value, NULL, 10);
73587c478bd9Sstevel@tonic-gate 	if (period == NULL)
73597c478bd9Sstevel@tonic-gate 		xtime->tv_nsec = 0;
73607c478bd9Sstevel@tonic-gate 	else {
73617c478bd9Sstevel@tonic-gate 		i = strlen(period +1);
73627c478bd9Sstevel@tonic-gate 		(void) strncpy(nanosec, period + 1, min(i, 9));
73637c478bd9Sstevel@tonic-gate 		xtime->tv_nsec = strtol(nanosec, NULL, 10);
73647c478bd9Sstevel@tonic-gate 	}
73657c478bd9Sstevel@tonic-gate }
73667c478bd9Sstevel@tonic-gate 
73677c478bd9Sstevel@tonic-gate /*
73687c478bd9Sstevel@tonic-gate  *	Check linkpath for length.
73697c478bd9Sstevel@tonic-gate  *	Emit an error message and return 1 if too long.
73707c478bd9Sstevel@tonic-gate  */
73717c478bd9Sstevel@tonic-gate 
73727c478bd9Sstevel@tonic-gate int
73737c478bd9Sstevel@tonic-gate chk_path_build(
73747c478bd9Sstevel@tonic-gate 	char	*name,
73757c478bd9Sstevel@tonic-gate 	char	*longname,
73767c478bd9Sstevel@tonic-gate 	char	*linkname,
73777c478bd9Sstevel@tonic-gate 	char	*prefix,
73787c478bd9Sstevel@tonic-gate 	char	type,
73797c478bd9Sstevel@tonic-gate 	int	filetype)
73807c478bd9Sstevel@tonic-gate {
73817c478bd9Sstevel@tonic-gate 
73827c478bd9Sstevel@tonic-gate 	if (strlen(linkname) > (size_t)NAMSIZ) {
73837c478bd9Sstevel@tonic-gate 		if (Eflag > 0) {
73847c478bd9Sstevel@tonic-gate 			xhdr_flgs |= _X_LINKPATH;
73857c478bd9Sstevel@tonic-gate 			Xtarhdr.x_linkpath = linkname;
73867c478bd9Sstevel@tonic-gate 		} else {
73877c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
73887c478bd9Sstevel@tonic-gate 			    "tar: %s: linked to %s\n"), longname, linkname);
73897c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
73907c478bd9Sstevel@tonic-gate 			    "tar: %s: linked name too long\n"), linkname);
73917c478bd9Sstevel@tonic-gate 			if (errflag)
73927c478bd9Sstevel@tonic-gate 				done(1);
73937c478bd9Sstevel@tonic-gate 			else
73947c478bd9Sstevel@tonic-gate 				Errflg = 1;
73957c478bd9Sstevel@tonic-gate 			return (1);
73967c478bd9Sstevel@tonic-gate 		}
73977c478bd9Sstevel@tonic-gate 	}
73987c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & _X_LINKPATH)
73997c478bd9Sstevel@tonic-gate 		return (build_dblock(name, tchar, type,
74007c478bd9Sstevel@tonic-gate 		    filetype, &stbuf, stbuf.st_dev,
74017c478bd9Sstevel@tonic-gate 		    prefix));
74027c478bd9Sstevel@tonic-gate 	else
74037c478bd9Sstevel@tonic-gate 		return (build_dblock(name, linkname, type,
74047c478bd9Sstevel@tonic-gate 		    filetype, &stbuf, stbuf.st_dev, prefix));
74057c478bd9Sstevel@tonic-gate }
74067c478bd9Sstevel@tonic-gate 
74077c478bd9Sstevel@tonic-gate /*
74087c478bd9Sstevel@tonic-gate  * Convert from UTF-8 to local character set.
74097c478bd9Sstevel@tonic-gate  */
74107c478bd9Sstevel@tonic-gate 
74117c478bd9Sstevel@tonic-gate static int
74127c478bd9Sstevel@tonic-gate utf8_local(
74137c478bd9Sstevel@tonic-gate 	char		*option,
74147c478bd9Sstevel@tonic-gate 	char		**Xhdr_ptrptr,
74157c478bd9Sstevel@tonic-gate 	char		*target,
74167c478bd9Sstevel@tonic-gate 	const char	*source,
74177c478bd9Sstevel@tonic-gate 	int		max_val)
74187c478bd9Sstevel@tonic-gate {
74197c478bd9Sstevel@tonic-gate 	static	iconv_t	iconv_cd;
74207c478bd9Sstevel@tonic-gate 	char		*nl_target;
74217c478bd9Sstevel@tonic-gate 	const	char	*iconv_src;
74227c478bd9Sstevel@tonic-gate 	char		*iconv_trg;
7423eace40a5Sceastha 	size_t		inlen;
7424eace40a5Sceastha 	size_t		outlen;
74257c478bd9Sstevel@tonic-gate 
74267c478bd9Sstevel@tonic-gate 	if (charset_type == -1) {	/* iconv_open failed in earlier try */
74277c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
74287c478bd9Sstevel@tonic-gate 		    "tar:  file # %llu: (%s) UTF-8 conversion failed.\n"),
74297c478bd9Sstevel@tonic-gate 		    xhdr_count, source);
74307c478bd9Sstevel@tonic-gate 		return (1);
74317c478bd9Sstevel@tonic-gate 	} else if (charset_type == 0) {	/* iconv_open has not yet been done */
74327c478bd9Sstevel@tonic-gate 		nl_target = nl_langinfo(CODESET);
74337c478bd9Sstevel@tonic-gate 		if (strlen(nl_target) == 0)	/* locale using 7-bit codeset */
74347c478bd9Sstevel@tonic-gate 			nl_target = "646";
74357c478bd9Sstevel@tonic-gate 		if (strcmp(nl_target, "646") == 0)
74367c478bd9Sstevel@tonic-gate 			charset_type = 1;
74377c478bd9Sstevel@tonic-gate 		else if (strcmp(nl_target, "UTF-8") == 0)
74387c478bd9Sstevel@tonic-gate 			charset_type = 3;
74397c478bd9Sstevel@tonic-gate 		else {
74407c478bd9Sstevel@tonic-gate 			if (strncmp(nl_target, "ISO", 3) == 0)
74417c478bd9Sstevel@tonic-gate 				nl_target += 3;
74427c478bd9Sstevel@tonic-gate 			charset_type = 2;
74437c478bd9Sstevel@tonic-gate 			errno = 0;
74447c478bd9Sstevel@tonic-gate 			if ((iconv_cd = iconv_open(nl_target, "UTF-8")) ==
74457c478bd9Sstevel@tonic-gate 			    (iconv_t)-1) {
74467c478bd9Sstevel@tonic-gate 				if (errno == EINVAL)
74477c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
74487c478bd9Sstevel@tonic-gate 					    "tar: conversion routines not "
74497c478bd9Sstevel@tonic-gate 					    "available for current locale.  "));
74507c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
74517c478bd9Sstevel@tonic-gate 				    "file # %llu: (%s) UTF-8 conversion"
74527c478bd9Sstevel@tonic-gate 				    " failed.\n"), xhdr_count, source);
74537c478bd9Sstevel@tonic-gate 				charset_type = -1;
74547c478bd9Sstevel@tonic-gate 				return (1);
74557c478bd9Sstevel@tonic-gate 			}
74567c478bd9Sstevel@tonic-gate 		}
74577c478bd9Sstevel@tonic-gate 	}
74587c478bd9Sstevel@tonic-gate 
74597c478bd9Sstevel@tonic-gate 	/* locale using 7-bit codeset or UTF-8 locale */
74607c478bd9Sstevel@tonic-gate 	if (charset_type == 1 || charset_type == 3) {
74617c478bd9Sstevel@tonic-gate 		if (strlen(source) > max_val) {
74627c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
74637c478bd9Sstevel@tonic-gate 			    "tar: file # %llu: Extended header %s too long.\n"),
74647c478bd9Sstevel@tonic-gate 			    xhdr_count, option);
74657c478bd9Sstevel@tonic-gate 			return (1);
74667c478bd9Sstevel@tonic-gate 		}
74677c478bd9Sstevel@tonic-gate 		if (charset_type == 3)
74687c478bd9Sstevel@tonic-gate 			(void) strcpy(target, source);
74697c478bd9Sstevel@tonic-gate 		else if (c_utf8(target, source) != 0) {
74707c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
74717c478bd9Sstevel@tonic-gate 			    "tar:  file # %llu: (%s) UTF-8 conversion"
74727c478bd9Sstevel@tonic-gate 			    " failed.\n"), xhdr_count, source);
74737c478bd9Sstevel@tonic-gate 			return (1);
74747c478bd9Sstevel@tonic-gate 		}
74757c478bd9Sstevel@tonic-gate 		*Xhdr_ptrptr = target;
74767c478bd9Sstevel@tonic-gate 		return (0);
74777c478bd9Sstevel@tonic-gate 	}
74787c478bd9Sstevel@tonic-gate 
74797c478bd9Sstevel@tonic-gate 	iconv_src = source;
74807c478bd9Sstevel@tonic-gate 	iconv_trg = target;
74817c478bd9Sstevel@tonic-gate 	inlen = strlen(source);
74827c478bd9Sstevel@tonic-gate 	outlen = max_val * UTF_8_FACTOR;
74837c478bd9Sstevel@tonic-gate 	if (iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen) ==
74847c478bd9Sstevel@tonic-gate 	    (size_t)-1) {	/* Error occurred:  didn't convert */
74857c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
74867c478bd9Sstevel@tonic-gate 		    "tar:  file # %llu: (%s) UTF-8 conversion failed.\n"),
74877c478bd9Sstevel@tonic-gate 		    xhdr_count, source);
74887c478bd9Sstevel@tonic-gate 		/* Get remaining output; reinitialize conversion descriptor */
74897c478bd9Sstevel@tonic-gate 		iconv_src = (const char *)NULL;
74907c478bd9Sstevel@tonic-gate 		inlen = 0;
74917c478bd9Sstevel@tonic-gate 		(void) iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen);
74927c478bd9Sstevel@tonic-gate 		return (1);
74937c478bd9Sstevel@tonic-gate 	}
74947c478bd9Sstevel@tonic-gate 	/* Get remaining output; reinitialize conversion descriptor */
74957c478bd9Sstevel@tonic-gate 	iconv_src = (const char *)NULL;
74967c478bd9Sstevel@tonic-gate 	inlen = 0;
74977c478bd9Sstevel@tonic-gate 	if (iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen) ==
74987c478bd9Sstevel@tonic-gate 	    (size_t)-1) {	/* Error occurred:  didn't convert */
74997c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
75007c478bd9Sstevel@tonic-gate 		    "tar:  file # %llu: (%s) UTF-8 conversion failed.\n"),
75017c478bd9Sstevel@tonic-gate 		    xhdr_count, source);
75027c478bd9Sstevel@tonic-gate 		return (1);
75037c478bd9Sstevel@tonic-gate 	}
75047c478bd9Sstevel@tonic-gate 
75057c478bd9Sstevel@tonic-gate 	*iconv_trg = '\0';	/* Null-terminate iconv output string */
75067c478bd9Sstevel@tonic-gate 	if (strlen(target) > max_val) {
75077c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
75087c478bd9Sstevel@tonic-gate 		    "tar: file # %llu: Extended header %s too long.\n"),
75097c478bd9Sstevel@tonic-gate 		    xhdr_count, option);
75107c478bd9Sstevel@tonic-gate 		return (1);
75117c478bd9Sstevel@tonic-gate 	}
75127c478bd9Sstevel@tonic-gate 	*Xhdr_ptrptr = target;
75137c478bd9Sstevel@tonic-gate 	return (0);
75147c478bd9Sstevel@tonic-gate }
75157c478bd9Sstevel@tonic-gate 
75167c478bd9Sstevel@tonic-gate /*
75177c478bd9Sstevel@tonic-gate  * Check gname, uname, path, and linkpath to see if they need to go in an
75187c478bd9Sstevel@tonic-gate  * extended header.  If they are already slated to be in an extended header,
75197c478bd9Sstevel@tonic-gate  * or if they are not ascii, then they need to be in the extended header.
75207c478bd9Sstevel@tonic-gate  * Then, convert all extended names to UTF-8.
75217c478bd9Sstevel@tonic-gate  */
75227c478bd9Sstevel@tonic-gate 
75237c478bd9Sstevel@tonic-gate int
75247c478bd9Sstevel@tonic-gate gen_utf8_names(const char *filename)
75257c478bd9Sstevel@tonic-gate {
75267c478bd9Sstevel@tonic-gate 	static	iconv_t	iconv_cd;
75277c478bd9Sstevel@tonic-gate 	char		*nl_target;
75287c478bd9Sstevel@tonic-gate 	char		tempbuf[MAXNAM + 1];
7529eace40a5Sceastha 	int		nbytes;
7530eace40a5Sceastha 	int		errors;
75317c478bd9Sstevel@tonic-gate 
75327c478bd9Sstevel@tonic-gate 	if (charset_type == -1)	{	/* Previous failure to open. */
75337c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
75347c478bd9Sstevel@tonic-gate 		    "tar: file # %llu: UTF-8 conversion failed.\n"),
75357c478bd9Sstevel@tonic-gate 		    xhdr_count);
75367c478bd9Sstevel@tonic-gate 		return (1);
75377c478bd9Sstevel@tonic-gate 	}
75387c478bd9Sstevel@tonic-gate 
75397c478bd9Sstevel@tonic-gate 	if (charset_type == 0) {	/* Need to get conversion descriptor */
75407c478bd9Sstevel@tonic-gate 		nl_target = nl_langinfo(CODESET);
75417c478bd9Sstevel@tonic-gate 		if (strlen(nl_target) == 0)	/* locale using 7-bit codeset */
75427c478bd9Sstevel@tonic-gate 			nl_target = "646";
75437c478bd9Sstevel@tonic-gate 		if (strcmp(nl_target, "646") == 0)
75447c478bd9Sstevel@tonic-gate 			charset_type = 1;
75457c478bd9Sstevel@tonic-gate 		else if (strcmp(nl_target, "UTF-8") == 0)
75467c478bd9Sstevel@tonic-gate 			charset_type = 3;
75477c478bd9Sstevel@tonic-gate 		else {
75487c478bd9Sstevel@tonic-gate 			if (strncmp(nl_target, "ISO", 3) == 0)
75497c478bd9Sstevel@tonic-gate 				nl_target += 3;
75507c478bd9Sstevel@tonic-gate 			charset_type = 2;
75517c478bd9Sstevel@tonic-gate 			errno = 0;
75527c478bd9Sstevel@tonic-gate #ifdef ICONV_DEBUG
75537c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
75548e4a71aeSRich Burridge 			    gettext("Opening iconv_cd with target %s\n"),
75557c478bd9Sstevel@tonic-gate 			    nl_target);
75567c478bd9Sstevel@tonic-gate #endif
75577c478bd9Sstevel@tonic-gate 			if ((iconv_cd = iconv_open("UTF-8", nl_target)) ==
75587c478bd9Sstevel@tonic-gate 			    (iconv_t)-1) {
75597c478bd9Sstevel@tonic-gate 				if (errno == EINVAL)
75607c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
75617c478bd9Sstevel@tonic-gate 					    "tar: conversion routines not "
75627c478bd9Sstevel@tonic-gate 					    "available for current locale.  "));
75637c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
75647c478bd9Sstevel@tonic-gate 				    "file (%s): UTF-8 conversion failed.\n"),
75657c478bd9Sstevel@tonic-gate 				    filename);
75667c478bd9Sstevel@tonic-gate 				charset_type = -1;
75677c478bd9Sstevel@tonic-gate 				return (1);
75687c478bd9Sstevel@tonic-gate 			}
75697c478bd9Sstevel@tonic-gate 		}
75707c478bd9Sstevel@tonic-gate 	}
75717c478bd9Sstevel@tonic-gate 
75727c478bd9Sstevel@tonic-gate 	errors = 0;
75737c478bd9Sstevel@tonic-gate 
75747c478bd9Sstevel@tonic-gate 	errors += local_utf8(&Xtarhdr.x_gname, local_gname,
75757c478bd9Sstevel@tonic-gate 	    dblock.dbuf.gname, iconv_cd, _X_GNAME, _POSIX_NAME_MAX);
75767c478bd9Sstevel@tonic-gate 	errors += local_utf8(&Xtarhdr.x_uname, local_uname,
75777c478bd9Sstevel@tonic-gate 	    dblock.dbuf.uname, iconv_cd, _X_UNAME,  _POSIX_NAME_MAX);
75787c478bd9Sstevel@tonic-gate 	if ((xhdr_flgs & _X_LINKPATH) == 0) {	/* Need null-terminated str. */
75797c478bd9Sstevel@tonic-gate 		(void) strncpy(tempbuf, dblock.dbuf.linkname, NAMSIZ);
75807c478bd9Sstevel@tonic-gate 		tempbuf[NAMSIZ] = '\0';
75817c478bd9Sstevel@tonic-gate 	}
75827c478bd9Sstevel@tonic-gate 	errors += local_utf8(&Xtarhdr.x_linkpath, local_linkpath,
75837c478bd9Sstevel@tonic-gate 	    tempbuf, iconv_cd, _X_LINKPATH, PATH_MAX);
75847c478bd9Sstevel@tonic-gate 	if ((xhdr_flgs & _X_PATH) == 0) {	/* Concatenate prefix & name */
75857c478bd9Sstevel@tonic-gate 		(void) strncpy(tempbuf, dblock.dbuf.prefix, PRESIZ);
75860578ac30Ssr161167 		tempbuf[PRESIZ] = '\0';
75877c478bd9Sstevel@tonic-gate 		nbytes = strlen(tempbuf);
75887c478bd9Sstevel@tonic-gate 		if (nbytes > 0) {
75897c478bd9Sstevel@tonic-gate 			tempbuf[nbytes++] = '/';
75907c478bd9Sstevel@tonic-gate 			tempbuf[nbytes] = '\0';
75917c478bd9Sstevel@tonic-gate 		}
75920578ac30Ssr161167 		(void) strncat(tempbuf + nbytes, dblock.dbuf.name,
75930578ac30Ssr161167 		    (MAXNAM - nbytes));
75940578ac30Ssr161167 		tempbuf[MAXNAM] = '\0';
75957c478bd9Sstevel@tonic-gate 	}
75967c478bd9Sstevel@tonic-gate 	errors += local_utf8(&Xtarhdr.x_path, local_path,
75977c478bd9Sstevel@tonic-gate 	    tempbuf, iconv_cd, _X_PATH, PATH_MAX);
75987c478bd9Sstevel@tonic-gate 
75997c478bd9Sstevel@tonic-gate 	if (errors > 0)
76007c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
76017c478bd9Sstevel@tonic-gate 		    "tar: file (%s): UTF-8 conversion failed.\n"), filename);
76027c478bd9Sstevel@tonic-gate 
76037c478bd9Sstevel@tonic-gate 	if (errors && errflag)
76047c478bd9Sstevel@tonic-gate 		done(1);
76057c478bd9Sstevel@tonic-gate 	else
76067c478bd9Sstevel@tonic-gate 		if (errors)
76077c478bd9Sstevel@tonic-gate 			Errflg = 1;
76087c478bd9Sstevel@tonic-gate 	return (errors);
76097c478bd9Sstevel@tonic-gate }
76107c478bd9Sstevel@tonic-gate 
76117c478bd9Sstevel@tonic-gate static int
76127c478bd9Sstevel@tonic-gate local_utf8(
76137c478bd9Sstevel@tonic-gate 		char	**Xhdr_ptrptr,
76147c478bd9Sstevel@tonic-gate 		char	*target,
76157c478bd9Sstevel@tonic-gate 		const	char	*source,
76167c478bd9Sstevel@tonic-gate 		iconv_t	iconv_cd,
76177c478bd9Sstevel@tonic-gate 		int	xhdrflg,
76187c478bd9Sstevel@tonic-gate 		int	max_val)
76197c478bd9Sstevel@tonic-gate {
76207c478bd9Sstevel@tonic-gate 	const	char	*iconv_src;
76217c478bd9Sstevel@tonic-gate 	const	char	*starting_src;
76227c478bd9Sstevel@tonic-gate 	char		*iconv_trg;
7623eace40a5Sceastha 	size_t		inlen;
7624eace40a5Sceastha 	size_t		outlen;
76257c478bd9Sstevel@tonic-gate #ifdef ICONV_DEBUG
76267c478bd9Sstevel@tonic-gate 	unsigned char	c_to_hex;
76277c478bd9Sstevel@tonic-gate #endif
76287c478bd9Sstevel@tonic-gate 
76297c478bd9Sstevel@tonic-gate 	/*
76307c478bd9Sstevel@tonic-gate 	 * If the item is already slated for extended format, get the string
76317c478bd9Sstevel@tonic-gate 	 * to convert from the extended header record.  Otherwise, get it from
76327c478bd9Sstevel@tonic-gate 	 * the regular (dblock) area.
76337c478bd9Sstevel@tonic-gate 	 */
76347c478bd9Sstevel@tonic-gate 	if (xhdr_flgs & xhdrflg) {
76357c478bd9Sstevel@tonic-gate 		if (charset_type == 3) {	/* Already UTF-8, just copy */
76367c478bd9Sstevel@tonic-gate 			(void) strcpy(target, *Xhdr_ptrptr);
76377c478bd9Sstevel@tonic-gate 			*Xhdr_ptrptr = target;
76387c478bd9Sstevel@tonic-gate 			return (0);
76397c478bd9Sstevel@tonic-gate 		} else
76407c478bd9Sstevel@tonic-gate 			iconv_src = (const char *) *Xhdr_ptrptr;
76417c478bd9Sstevel@tonic-gate 	} else {
76427c478bd9Sstevel@tonic-gate 		if (charset_type == 3)		/* Already in UTF-8 format */
76437c478bd9Sstevel@tonic-gate 			return (0);		/* Don't create xhdr record */
76447c478bd9Sstevel@tonic-gate 		iconv_src = source;
76457c478bd9Sstevel@tonic-gate 	}
76467c478bd9Sstevel@tonic-gate 	starting_src = iconv_src;
76477c478bd9Sstevel@tonic-gate 	iconv_trg = target;
76487c478bd9Sstevel@tonic-gate 	if ((inlen = strlen(iconv_src)) == 0)
76497c478bd9Sstevel@tonic-gate 		return (0);
76507c478bd9Sstevel@tonic-gate 
76517c478bd9Sstevel@tonic-gate 	if (charset_type == 1) {	/* locale using 7-bit codeset */
76527c478bd9Sstevel@tonic-gate 		if (c_utf8(target, starting_src) != 0) {
76537c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
76547c478bd9Sstevel@tonic-gate 			    gettext("tar: invalid character in"
76557c478bd9Sstevel@tonic-gate 			    " UTF-8 conversion of '%s'\n"), starting_src);
76567c478bd9Sstevel@tonic-gate 			return (1);
76577c478bd9Sstevel@tonic-gate 		}
76587c478bd9Sstevel@tonic-gate 		return (0);
76597c478bd9Sstevel@tonic-gate 	}
76607c478bd9Sstevel@tonic-gate 
76617c478bd9Sstevel@tonic-gate 	outlen = max_val * UTF_8_FACTOR;
76627c478bd9Sstevel@tonic-gate 	errno = 0;
76637c478bd9Sstevel@tonic-gate 	if (iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen) ==
76647c478bd9Sstevel@tonic-gate 	    (size_t)-1) {
76657c478bd9Sstevel@tonic-gate 		/* An error occurred, or not all characters were converted */
76667c478bd9Sstevel@tonic-gate 		if (errno == EILSEQ)
76677c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
76687c478bd9Sstevel@tonic-gate 			    gettext("tar: invalid character in"
76697c478bd9Sstevel@tonic-gate 			    " UTF-8 conversion of '%s'\n"), starting_src);
76707c478bd9Sstevel@tonic-gate 		else
76717c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
76727c478bd9Sstevel@tonic-gate 			    "tar: conversion to UTF-8 aborted for '%s'.\n"),
76737c478bd9Sstevel@tonic-gate 			    starting_src);
76747c478bd9Sstevel@tonic-gate 		/* Get remaining output; reinitialize conversion descriptor */
76757c478bd9Sstevel@tonic-gate 		iconv_src = (const char *)NULL;
76767c478bd9Sstevel@tonic-gate 		inlen = 0;
76777c478bd9Sstevel@tonic-gate 		(void) iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen);
76787c478bd9Sstevel@tonic-gate 		return (1);
76797c478bd9Sstevel@tonic-gate 	}
76807c478bd9Sstevel@tonic-gate 	/* Get remaining output; reinitialize conversion descriptor */
76817c478bd9Sstevel@tonic-gate 	iconv_src = (const char *)NULL;
76827c478bd9Sstevel@tonic-gate 	inlen = 0;
76837c478bd9Sstevel@tonic-gate 	if (iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen) ==
76847c478bd9Sstevel@tonic-gate 	    (size_t)-1) {	/* Error occurred:  didn't convert */
76857c478bd9Sstevel@tonic-gate 		if (errno == EILSEQ)
76867c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
76877c478bd9Sstevel@tonic-gate 			    gettext("tar: invalid character in"
76887c478bd9Sstevel@tonic-gate 			    " UTF-8 conversion of '%s'\n"), starting_src);
76897c478bd9Sstevel@tonic-gate 		else
76907c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
76917c478bd9Sstevel@tonic-gate 			    "tar: conversion to UTF-8 aborted for '%s'.\n"),
76927c478bd9Sstevel@tonic-gate 			    starting_src);
76937c478bd9Sstevel@tonic-gate 		return (1);
76947c478bd9Sstevel@tonic-gate 	}
76957c478bd9Sstevel@tonic-gate 
76967c478bd9Sstevel@tonic-gate 	*iconv_trg = '\0';	/* Null-terminate iconv output string */
76977c478bd9Sstevel@tonic-gate 	if (strcmp(starting_src, target) != 0) {
76987c478bd9Sstevel@tonic-gate 		*Xhdr_ptrptr = target;
76997c478bd9Sstevel@tonic-gate 		xhdr_flgs |= xhdrflg;
77007c478bd9Sstevel@tonic-gate #ifdef ICONV_DEBUG
77017c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "***  inlen: %d %d; outlen: %d %d\n",
77027c478bd9Sstevel@tonic-gate 		    strlen(starting_src), inlen, max_val, outlen);
77037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Input string:\n  ");
77047c478bd9Sstevel@tonic-gate 		for (inlen = 0; inlen < strlen(starting_src); inlen++) {
77057c478bd9Sstevel@tonic-gate 			c_to_hex = (unsigned char)starting_src[inlen];
77067c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, " %2.2x", c_to_hex);
77077c478bd9Sstevel@tonic-gate 			if (inlen % 20 == 19)
77087c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\n  ");
77097c478bd9Sstevel@tonic-gate 		}
77107c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\nOutput string:\n  ");
77117c478bd9Sstevel@tonic-gate 		for (inlen = 0; inlen < strlen(target); inlen++) {
77127c478bd9Sstevel@tonic-gate 			c_to_hex = (unsigned char)target[inlen];
77137c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, " %2.2x", c_to_hex);
77147c478bd9Sstevel@tonic-gate 			if (inlen % 20 == 19)
77157c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\n  ");
77167c478bd9Sstevel@tonic-gate 		}
77177c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
77187c478bd9Sstevel@tonic-gate #endif
77197c478bd9Sstevel@tonic-gate 	}
77207c478bd9Sstevel@tonic-gate 
77217c478bd9Sstevel@tonic-gate 	return (0);
77227c478bd9Sstevel@tonic-gate }
77237c478bd9Sstevel@tonic-gate 
77247c478bd9Sstevel@tonic-gate /*
77257c478bd9Sstevel@tonic-gate  *	Function to test each byte of the source string to make sure it is
77267c478bd9Sstevel@tonic-gate  *	in within bounds (value between 0 and 127).
77277c478bd9Sstevel@tonic-gate  *	If valid, copy source to target.
77287c478bd9Sstevel@tonic-gate  */
77297c478bd9Sstevel@tonic-gate 
77307c478bd9Sstevel@tonic-gate int
77317c478bd9Sstevel@tonic-gate c_utf8(char *target, const char *source)
77327c478bd9Sstevel@tonic-gate {
77337c478bd9Sstevel@tonic-gate 	size_t		len;
77347c478bd9Sstevel@tonic-gate 	const char	*thischar;
77357c478bd9Sstevel@tonic-gate 
77367c478bd9Sstevel@tonic-gate 	len = strlen(source);
77377c478bd9Sstevel@tonic-gate 	thischar = source;
77387c478bd9Sstevel@tonic-gate 	while (len-- > 0) {
77397c478bd9Sstevel@tonic-gate 		if (!isascii((int)(*thischar++)))
77407c478bd9Sstevel@tonic-gate 			return (1);
77417c478bd9Sstevel@tonic-gate 	}
77427c478bd9Sstevel@tonic-gate 
77437c478bd9Sstevel@tonic-gate 	(void) strcpy(target, source);
77447c478bd9Sstevel@tonic-gate 	return (0);
77457c478bd9Sstevel@tonic-gate }
77467c478bd9Sstevel@tonic-gate 
77477c478bd9Sstevel@tonic-gate 
77487c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
77497c478bd9Sstevel@tonic-gate #define	ROUNDTOTBLOCK(a)	((a + (TBLOCK -1)) & ~(TBLOCK -1))
77507c478bd9Sstevel@tonic-gate 
77517c478bd9Sstevel@tonic-gate static void
77527c478bd9Sstevel@tonic-gate prepare_xattr(
77537c478bd9Sstevel@tonic-gate 	char		**attrbuf,
77547c478bd9Sstevel@tonic-gate 	char		*filename,
7755da6c28aaSamw 	char		*attrpath,
77567c478bd9Sstevel@tonic-gate 	char		typeflag,
77577c478bd9Sstevel@tonic-gate 	struct linkbuf	*linkinfo,
77587c478bd9Sstevel@tonic-gate 	int		*rlen)
77597c478bd9Sstevel@tonic-gate {
77607c478bd9Sstevel@tonic-gate 	char			*bufhead;	/* ptr to full buffer */
7761da6c28aaSamw 	char			*aptr;
77627c478bd9Sstevel@tonic-gate 	struct xattr_hdr 	*hptr;		/* ptr to header in bufhead */
77637c478bd9Sstevel@tonic-gate 	struct xattr_buf	*tptr;		/* ptr to pathing pieces */
77647c478bd9Sstevel@tonic-gate 	int			totalen;	/* total buffer length */
77657c478bd9Sstevel@tonic-gate 	int			len;		/* length returned to user */
77667c478bd9Sstevel@tonic-gate 	int			stringlen;	/* length of filename + attr */
77677c478bd9Sstevel@tonic-gate 						/*
77687c478bd9Sstevel@tonic-gate 						 * length of filename + attr
77697c478bd9Sstevel@tonic-gate 						 * in link section
77707c478bd9Sstevel@tonic-gate 						 */
77717c478bd9Sstevel@tonic-gate 	int			linkstringlen;
77727c478bd9Sstevel@tonic-gate 	int			complen;	/* length of pathing section */
77737c478bd9Sstevel@tonic-gate 	int			linklen;	/* length of link section */
7774da6c28aaSamw 	int			attrnames_index; /* attrnames starting index */
77757c478bd9Sstevel@tonic-gate 
77767c478bd9Sstevel@tonic-gate 	/*
77777c478bd9Sstevel@tonic-gate 	 * Release previous buffer
77787c478bd9Sstevel@tonic-gate 	 */
77797c478bd9Sstevel@tonic-gate 
77807c478bd9Sstevel@tonic-gate 	if (*attrbuf != (char *)NULL) {
77817c478bd9Sstevel@tonic-gate 		free(*attrbuf);
77827c478bd9Sstevel@tonic-gate 		*attrbuf = NULL;
77837c478bd9Sstevel@tonic-gate 	}
77847c478bd9Sstevel@tonic-gate 
77857c478bd9Sstevel@tonic-gate 	/*
77867c478bd9Sstevel@tonic-gate 	 * First add in fixed size stuff
77877c478bd9Sstevel@tonic-gate 	 */
77887c478bd9Sstevel@tonic-gate 	len = sizeof (struct xattr_hdr) + sizeof (struct xattr_buf);
77897c478bd9Sstevel@tonic-gate 
77907c478bd9Sstevel@tonic-gate 	/*
77917c478bd9Sstevel@tonic-gate 	 * Add space for two nulls
77927c478bd9Sstevel@tonic-gate 	 */
7793da6c28aaSamw 	stringlen = strlen(attrpath) + strlen(filename) + 2;
77947c478bd9Sstevel@tonic-gate 	complen = stringlen + sizeof (struct xattr_buf);
77957c478bd9Sstevel@tonic-gate 
77967c478bd9Sstevel@tonic-gate 	len += stringlen;
77977c478bd9Sstevel@tonic-gate 
77987c478bd9Sstevel@tonic-gate 	/*
77997c478bd9Sstevel@tonic-gate 	 * Now add on space for link info if any
78007c478bd9Sstevel@tonic-gate 	 */
78017c478bd9Sstevel@tonic-gate 
78027c478bd9Sstevel@tonic-gate 	if (linkinfo != NULL) {
78037c478bd9Sstevel@tonic-gate 		/*
78047c478bd9Sstevel@tonic-gate 		 * Again add space for two nulls
78057c478bd9Sstevel@tonic-gate 		 */
78067c478bd9Sstevel@tonic-gate 		linkstringlen = strlen(linkinfo->pathname) +
78077c478bd9Sstevel@tonic-gate 		    strlen(linkinfo->attrname) + 2;
7808da6c28aaSamw 		linklen = linkstringlen + sizeof (struct xattr_buf);
7809da6c28aaSamw 		len += linklen;
7810da6c28aaSamw 	} else {
7811da6c28aaSamw 		linklen = 0;
78127c478bd9Sstevel@tonic-gate 	}
78137c478bd9Sstevel@tonic-gate 
78147c478bd9Sstevel@tonic-gate 	/*
78157c478bd9Sstevel@tonic-gate 	 * Now add padding to end to fill out TBLOCK
78167c478bd9Sstevel@tonic-gate 	 *
78177c478bd9Sstevel@tonic-gate 	 * Function returns size of real data and not size + padding.
78187c478bd9Sstevel@tonic-gate 	 */
78197c478bd9Sstevel@tonic-gate 
78207c478bd9Sstevel@tonic-gate 	totalen = ROUNDTOTBLOCK(len);
78217c478bd9Sstevel@tonic-gate 
78227c478bd9Sstevel@tonic-gate 	if ((bufhead = calloc(1, totalen)) == NULL) {
78237c478bd9Sstevel@tonic-gate 		fatal(gettext("Out of memory."));
78247c478bd9Sstevel@tonic-gate 	}
78257c478bd9Sstevel@tonic-gate 
78267c478bd9Sstevel@tonic-gate 
78277c478bd9Sstevel@tonic-gate 	/*
78287c478bd9Sstevel@tonic-gate 	 * Now we can fill in the necessary pieces
78297c478bd9Sstevel@tonic-gate 	 */
78307c478bd9Sstevel@tonic-gate 
78317c478bd9Sstevel@tonic-gate 	/*
78327c478bd9Sstevel@tonic-gate 	 * first fill in the fixed header
78337c478bd9Sstevel@tonic-gate 	 */
78347c478bd9Sstevel@tonic-gate 	hptr = (struct xattr_hdr *)bufhead;
78357c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_version, "%s", XATTR_ARCH_VERS);
78367c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_component_len, "%0*d",
78377c478bd9Sstevel@tonic-gate 	    sizeof (hptr->h_component_len) - 1, complen);
78387c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_link_component_len, "%0*d",
78397c478bd9Sstevel@tonic-gate 	    sizeof (hptr->h_link_component_len) - 1, linklen);
78407c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_size, "%0*d", sizeof (hptr->h_size) - 1, len);
78417c478bd9Sstevel@tonic-gate 
78427c478bd9Sstevel@tonic-gate 	/*
78437c478bd9Sstevel@tonic-gate 	 * Now fill in the filename + attrnames section
7844da6c28aaSamw 	 * The filename and attrnames section can be composed of two or more
7845da6c28aaSamw 	 * path segments separated by a null character.  The first segment
7846da6c28aaSamw 	 * is the path to the parent file that roots the entire sequence in
7847da6c28aaSamw 	 * the normal name space. The remaining segments describes a path
7848da6c28aaSamw 	 * rooted at the hidden extended attribute directory of the leaf file of
7849da6c28aaSamw 	 * the previous segment, making it possible to name attributes on
7850da6c28aaSamw 	 * attributes.  Thus, if we are just archiving an extended attribute,
7851da6c28aaSamw 	 * the second segment will contain the attribute name.  If we are
7852da6c28aaSamw 	 * archiving a system attribute of an extended attribute, then the
7853da6c28aaSamw 	 * second segment will contain the attribute name, and a third segment
7854da6c28aaSamw 	 * will contain the system attribute name.  The attribute pathing
7855da6c28aaSamw 	 * information is obtained from 'attrpath'.
78567c478bd9Sstevel@tonic-gate 	 */
78577c478bd9Sstevel@tonic-gate 
78587c478bd9Sstevel@tonic-gate 	tptr = (struct xattr_buf *)(bufhead + sizeof (struct xattr_hdr));
78597c478bd9Sstevel@tonic-gate 	(void) sprintf(tptr->h_namesz, "%0*d", sizeof (tptr->h_namesz) - 1,
78607c478bd9Sstevel@tonic-gate 	    stringlen);
78617c478bd9Sstevel@tonic-gate 	(void) strcpy(tptr->h_names, filename);
7862da6c28aaSamw 	attrnames_index = strlen(filename) + 1;
7863da6c28aaSamw 	(void) strcpy(&tptr->h_names[attrnames_index], attrpath);
78647c478bd9Sstevel@tonic-gate 	tptr->h_typeflag = typeflag;
78657c478bd9Sstevel@tonic-gate 
78667c478bd9Sstevel@tonic-gate 	/*
7867da6c28aaSamw 	 * Split the attrnames section into two segments if 'attrpath'
7868da6c28aaSamw 	 * contains pathing information for a system attribute of an
7869da6c28aaSamw 	 * extended attribute.  We split them by replacing the '/' with
7870da6c28aaSamw 	 * a '\0'.
7871da6c28aaSamw 	 */
7872da6c28aaSamw 	if ((aptr = strpbrk(&tptr->h_names[attrnames_index], "/")) != NULL) {
7873da6c28aaSamw 		*aptr = '\0';
7874da6c28aaSamw 	}
7875da6c28aaSamw 
7876da6c28aaSamw 	/*
78777c478bd9Sstevel@tonic-gate 	 * Now fill in the optional link section if we have one
78787c478bd9Sstevel@tonic-gate 	 */
78797c478bd9Sstevel@tonic-gate 
78807c478bd9Sstevel@tonic-gate 	if (linkinfo != (struct linkbuf *)NULL) {
78817c478bd9Sstevel@tonic-gate 		tptr = (struct xattr_buf *)(bufhead +
78827c478bd9Sstevel@tonic-gate 		    sizeof (struct xattr_hdr) + complen);
78837c478bd9Sstevel@tonic-gate 
78847c478bd9Sstevel@tonic-gate 		(void) sprintf(tptr->h_namesz, "%0*d",
78857c478bd9Sstevel@tonic-gate 		    sizeof (tptr->h_namesz) - 1, linkstringlen);
78867c478bd9Sstevel@tonic-gate 		(void) strcpy(tptr->h_names, linkinfo->pathname);
78877c478bd9Sstevel@tonic-gate 		(void) strcpy(
78887c478bd9Sstevel@tonic-gate 		    &tptr->h_names[strlen(linkinfo->pathname) + 1],
78897c478bd9Sstevel@tonic-gate 		    linkinfo->attrname);
78907c478bd9Sstevel@tonic-gate 		tptr->h_typeflag = typeflag;
78917c478bd9Sstevel@tonic-gate 	}
78927c478bd9Sstevel@tonic-gate 	*attrbuf = (char *)bufhead;
78937c478bd9Sstevel@tonic-gate 	*rlen = len;
78947c478bd9Sstevel@tonic-gate }
78957c478bd9Sstevel@tonic-gate 
78967c478bd9Sstevel@tonic-gate #else
78977c478bd9Sstevel@tonic-gate static void
78987c478bd9Sstevel@tonic-gate prepare_xattr(
78997c478bd9Sstevel@tonic-gate 	char		**attrbuf,
79007c478bd9Sstevel@tonic-gate 	char		*filename,
79017c478bd9Sstevel@tonic-gate 	char		*attrname,
79027c478bd9Sstevel@tonic-gate 	char		typeflag,
79037c478bd9Sstevel@tonic-gate 	struct linkbuf	*linkinfo,
79047c478bd9Sstevel@tonic-gate 	int		*rlen)
79057c478bd9Sstevel@tonic-gate {
79067c478bd9Sstevel@tonic-gate 	*attrbuf = NULL;
79077c478bd9Sstevel@tonic-gate 	*rlen = 0;
79087c478bd9Sstevel@tonic-gate }
79097c478bd9Sstevel@tonic-gate #endif
79107c478bd9Sstevel@tonic-gate 
79117c478bd9Sstevel@tonic-gate int
7912da6c28aaSamw getstat(int dirfd, char *longname, char *shortname, char *attrparent)
79137c478bd9Sstevel@tonic-gate {
79147c478bd9Sstevel@tonic-gate 
79157c478bd9Sstevel@tonic-gate 	int i, j;
79167c478bd9Sstevel@tonic-gate 	int	printerr;
79177c478bd9Sstevel@tonic-gate 	int	slnkerr;
79187c478bd9Sstevel@tonic-gate 	struct stat symlnbuf;
79197c478bd9Sstevel@tonic-gate 
79207c478bd9Sstevel@tonic-gate 	if (!hflag)
79217c478bd9Sstevel@tonic-gate 		i = fstatat(dirfd, shortname, &stbuf, AT_SYMLINK_NOFOLLOW);
79227c478bd9Sstevel@tonic-gate 	else
79237c478bd9Sstevel@tonic-gate 		i = fstatat(dirfd, shortname, &stbuf, 0);
79247c478bd9Sstevel@tonic-gate 
79257c478bd9Sstevel@tonic-gate 	if (i < 0) {
79267c478bd9Sstevel@tonic-gate 		/* Initialize flag to print error mesg. */
79277c478bd9Sstevel@tonic-gate 		printerr = 1;
79287c478bd9Sstevel@tonic-gate 		/*
79297c478bd9Sstevel@tonic-gate 		 * If stat is done, then need to do lstat
79307c478bd9Sstevel@tonic-gate 		 * to determine whether it's a sym link
79317c478bd9Sstevel@tonic-gate 		 */
79327c478bd9Sstevel@tonic-gate 		if (hflag) {
79337c478bd9Sstevel@tonic-gate 			/* Save returned error */
79347c478bd9Sstevel@tonic-gate 			slnkerr = errno;
79357c478bd9Sstevel@tonic-gate 
79367c478bd9Sstevel@tonic-gate 			j = fstatat(dirfd, shortname,
79377c478bd9Sstevel@tonic-gate 			    &symlnbuf, AT_SYMLINK_NOFOLLOW);
79387c478bd9Sstevel@tonic-gate 			/*
79397c478bd9Sstevel@tonic-gate 			 * Suppress error message when file is a symbolic link
79407c478bd9Sstevel@tonic-gate 			 * and function modifier 'l' is off.  Exception:  when
79417c478bd9Sstevel@tonic-gate 			 * a symlink points to a symlink points to a
79427c478bd9Sstevel@tonic-gate 			 * symlink ... and we get past MAXSYMLINKS.  That
79437c478bd9Sstevel@tonic-gate 			 * error will cause a file not to be archived, and
79447c478bd9Sstevel@tonic-gate 			 * needs to be printed.
79457c478bd9Sstevel@tonic-gate 			 */
79467c478bd9Sstevel@tonic-gate 			if ((j == 0) && (!linkerrok) && (slnkerr != ELOOP) &&
79477c478bd9Sstevel@tonic-gate 			    (S_ISLNK(symlnbuf.st_mode)))
79487c478bd9Sstevel@tonic-gate 				printerr = 0;
79497c478bd9Sstevel@tonic-gate 
79507c478bd9Sstevel@tonic-gate 			/*
79517c478bd9Sstevel@tonic-gate 			 * Restore errno in case the lstat
79527c478bd9Sstevel@tonic-gate 			 * on symbolic link change
79537c478bd9Sstevel@tonic-gate 			 */
79547c478bd9Sstevel@tonic-gate 			errno = slnkerr;
79557c478bd9Sstevel@tonic-gate 		}
79567c478bd9Sstevel@tonic-gate 
79577c478bd9Sstevel@tonic-gate 		if (printerr) {
79587c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
7959da6c28aaSamw 			    "tar: %s%s%s%s: %s\n"),
7960da6c28aaSamw 			    (attrparent == NULL) ? "" : gettext("attribute "),
7961da6c28aaSamw 			    (attrparent == NULL) ? "" : attrparent,
7962da6c28aaSamw 			    (attrparent == NULL) ? "" : gettext(" of "),
7963da6c28aaSamw 			    longname, strerror(errno));
79647c478bd9Sstevel@tonic-gate 			Errflg = 1;
79657c478bd9Sstevel@tonic-gate 		}
79667c478bd9Sstevel@tonic-gate 		return (1);
79677c478bd9Sstevel@tonic-gate 	}
79687c478bd9Sstevel@tonic-gate 	return (0);
79697c478bd9Sstevel@tonic-gate }
79707c478bd9Sstevel@tonic-gate 
7971da6c28aaSamw /*
7972da6c28aaSamw  * Recursively archive the extended attributes and/or extended system attributes
7973da6c28aaSamw  * of the base file, longname.  Note:  extended system attribute files will be
7974da6c28aaSamw  * archived only if the extended system attributes are not transient (i.e. the
7975da6c28aaSamw  * extended system attributes are other than the default values).
7976da6c28aaSamw  *
7977da6c28aaSamw  * If -@ was specified and the underlying file system supports it, archive the
7978da6c28aaSamw  * extended attributes, and if there is a system attribute associated with the
7979da6c28aaSamw  * extended attribute, then recursively call xattrs_put() to archive the
7980da6c28aaSamw  * hidden attribute directory and the extended system attribute.  If -/ was
7981da6c28aaSamw  * specified and the underlying file system supports it, archive the extended
7982da6c28aaSamw  * system attributes.  Read-only extended system attributes are never archived.
7983da6c28aaSamw  *
7984da6c28aaSamw  * Currently, there cannot be attributes on attributes; only system
7985da6c28aaSamw  * attributes on attributes.  In addition, there cannot be attributes on
7986da6c28aaSamw  * system attributes.  A file and it's attribute directory hierarchy looks as
7987da6c28aaSamw  * follows:
7988da6c28aaSamw  *	longname ---->	.	("." is the hidden attribute directory)
7989da6c28aaSamw  *			|
7990da6c28aaSamw  *	     ----------------------------
7991da6c28aaSamw  *	     |				|
7992da6c28aaSamw  *	<sys_attr_name>		   <attr_name> ---->	.
7993da6c28aaSamw  *							|
7994da6c28aaSamw  *						  <sys_attr_name>
7995da6c28aaSamw  *
7996da6c28aaSamw  */
79977c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
79987c478bd9Sstevel@tonic-gate static void
7999da6c28aaSamw xattrs_put(char *longname, char *shortname, char *parent, char *attrparent)
80007c478bd9Sstevel@tonic-gate {
8001da6c28aaSamw 	char *filename = (attrparent == NULL) ? shortname : attrparent;
8002da6c28aaSamw 	int arc_rwsysattr = 0;
80037c478bd9Sstevel@tonic-gate 	int dirfd;
8004da6c28aaSamw 	int fd = -1;
8005da6c28aaSamw 	int rw_sysattr = 0;
8006ced83f9bSceastha 	int ext_attr = 0;
8007da6c28aaSamw 	int rc;
80087c478bd9Sstevel@tonic-gate 	DIR *dirp;
80097c478bd9Sstevel@tonic-gate 	struct dirent *dp;
8010da6c28aaSamw 	attr_data_t *attrinfo = NULL;
80117c478bd9Sstevel@tonic-gate 
8012da6c28aaSamw 	/*
8013da6c28aaSamw 	 * If the underlying file system supports it, then archive the extended
8014da6c28aaSamw 	 * attributes if -@ was specified, and the extended system attributes
8015da6c28aaSamw 	 * if -/ was specified.
8016da6c28aaSamw 	 */
8017ced83f9bSceastha 	if (verify_attr_support(filename, (attrparent == NULL), ARC_CREATE,
8018ced83f9bSceastha 	    &ext_attr) != ATTR_OK) {
80197c478bd9Sstevel@tonic-gate 		return;
80207c478bd9Sstevel@tonic-gate 	}
80217c478bd9Sstevel@tonic-gate 
8022da6c28aaSamw 	/*
8023da6c28aaSamw 	 * Only want to archive a read-write extended system attribute file
8024da6c28aaSamw 	 * if it contains extended system attribute settings that are not the
8025da6c28aaSamw 	 * default values.
8026da6c28aaSamw 	 */
8027da6c28aaSamw #if defined(_PC_SATTR_ENABLED)
8028da6c28aaSamw 	if (saflag) {
8029da6c28aaSamw 		int	filefd;
8030da6c28aaSamw 		nvlist_t *slist = NULL;
8031da6c28aaSamw 
8032da6c28aaSamw 		/* Determine if there are non-transient system attributes */
8033da6c28aaSamw 		errno = 0;
8034da6c28aaSamw 		if ((filefd = open(filename, O_RDONLY)) == -1) {
8035da6c28aaSamw 			if (attrparent == NULL) {
8036da6c28aaSamw 				vperror(0, gettext(
8037da6c28aaSamw 				    "unable to open file %s"), longname);
8038da6c28aaSamw 			}
8039da6c28aaSamw 			return;
8040da6c28aaSamw 		}
8041da6c28aaSamw 		if (((slist = sysattr_list(basename(myname), filefd,
8042da6c28aaSamw 		    filename)) != NULL) || (errno != 0)) {
8043da6c28aaSamw 			arc_rwsysattr = 1;
8044da6c28aaSamw 		}
8045da6c28aaSamw 		if (slist != NULL) {
8046da6c28aaSamw 			(void) nvlist_free(slist);
8047da6c28aaSamw 			slist = NULL;
8048da6c28aaSamw 		}
8049da6c28aaSamw 		(void) close(filefd);
8050da6c28aaSamw 	}
8051ced83f9bSceastha 
8052ced83f9bSceastha 	/*
8053ced83f9bSceastha 	 * If we aren't archiving extended system attributes, and we are
8054ced83f9bSceastha 	 * processing an attribute, or if we are archiving extended system
8055ced83f9bSceastha 	 * attributes, and there are are no extended attributes, then there's
8056ced83f9bSceastha 	 * no need to open up the attribute directory of the file unless the
8057ced83f9bSceastha 	 * extended system attributes are not transient (i.e, the system
8058ced83f9bSceastha 	 * attributes are not the default values).
8059ced83f9bSceastha 	 */
8060ced83f9bSceastha 	if ((arc_rwsysattr == 0) && ((attrparent != NULL) ||
8061ced83f9bSceastha 	    (saflag && !ext_attr))) {
8062ced83f9bSceastha 		return;
8063ced83f9bSceastha 	}
8064da6c28aaSamw #endif	/* _PC_SATTR_ENABLED */
8065da6c28aaSamw 
8066da6c28aaSamw 	/* open the parent attribute directory */
8067da6c28aaSamw 	fd = attropen(filename, ".", O_RDONLY);
8068da6c28aaSamw 	if (fd < 0) {
8069da6c28aaSamw 		vperror(0, gettext(
8070da6c28aaSamw 		    "unable to open attribute directory for %s%s%sfile %s"),
8071da6c28aaSamw 		    (attrparent == NULL) ? "" : gettext("attribute "),
8072da6c28aaSamw 		    (attrparent == NULL) ? "" : attrparent,
8073da6c28aaSamw 		    (attrparent == NULL) ? "" : gettext(" of "),
80747c478bd9Sstevel@tonic-gate 		    longname);
80757c478bd9Sstevel@tonic-gate 		return;
80767c478bd9Sstevel@tonic-gate 	}
80777c478bd9Sstevel@tonic-gate 
8078da6c28aaSamw 	/*
8079da6c28aaSamw 	 * We need to change into the parent's attribute directory to determine
8080da6c28aaSamw 	 * if each of the attributes should be archived.
8081da6c28aaSamw 	 */
8082da6c28aaSamw 	if (fchdir(fd) < 0) {
8083da6c28aaSamw 		vperror(0, gettext(
8084da6c28aaSamw 		    "cannot change to attribute directory of %s%s%sfile %s"),
8085da6c28aaSamw 		    (attrparent == NULL) ? "" : gettext("attribute "),
8086da6c28aaSamw 		    (attrparent == NULL) ? "" : attrparent,
8087da6c28aaSamw 		    (attrparent == NULL) ? "" : gettext(" of "),
8088da6c28aaSamw 		    longname);
8089da6c28aaSamw 		(void) close(fd);
8090da6c28aaSamw 		return;
8091da6c28aaSamw 	}
8092da6c28aaSamw 
8093da6c28aaSamw 	if (((dirfd = dup(fd)) == -1) ||
8094da6c28aaSamw 	    ((dirp = fdopendir(dirfd)) == NULL)) {
80957c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
8096da6c28aaSamw 		    "tar: unable to open dir pointer for %s%s%sfile %s\n"),
8097da6c28aaSamw 		    (attrparent == NULL) ? "" : gettext("attribute "),
8098da6c28aaSamw 		    (attrparent == NULL) ? "" : attrparent,
8099da6c28aaSamw 		    (attrparent == NULL) ? "" : gettext(" of "),
8100da6c28aaSamw 		    longname);
8101da6c28aaSamw 		if (fd > 0) {
8102da6c28aaSamw 			(void) close(fd);
8103da6c28aaSamw 		}
81047c478bd9Sstevel@tonic-gate 		return;
81057c478bd9Sstevel@tonic-gate 	}
81067c478bd9Sstevel@tonic-gate 
81077c478bd9Sstevel@tonic-gate 	while (dp = readdir(dirp)) {
8108da6c28aaSamw 		if (strcmp(dp->d_name, "..") == 0) {
81097c478bd9Sstevel@tonic-gate 			continue;
8110da6c28aaSamw 		} else if (strcmp(dp->d_name, ".") == 0) {
81117c478bd9Sstevel@tonic-gate 			Hiddendir = 1;
8112da6c28aaSamw 		} else {
81137c478bd9Sstevel@tonic-gate 			Hiddendir = 0;
8114da6c28aaSamw 		}
81157c478bd9Sstevel@tonic-gate 
8116da6c28aaSamw 		/* Determine if this attribute should be archived */
8117da6c28aaSamw 		if (verify_attr(dp->d_name, attrparent, arc_rwsysattr,
8118da6c28aaSamw 		    &rw_sysattr) != ATTR_OK) {
8119da6c28aaSamw 			continue;
8120da6c28aaSamw 		}
8121da6c28aaSamw 
8122da6c28aaSamw 		/* gather the attribute's information to pass to putfile() */
8123da6c28aaSamw 		if ((fill_in_attr_info(dp->d_name, longname, attrparent,
8124da6c28aaSamw 		    fd, rw_sysattr, &attrinfo)) == 1) {
8125da6c28aaSamw 			continue;
8126da6c28aaSamw 		}
8127da6c28aaSamw 
8128da6c28aaSamw 		/* add the attribute to the archive */
8129da6c28aaSamw 		rc = putfile(longname, dp->d_name, parent, attrinfo,
81307c478bd9Sstevel@tonic-gate 		    XATTR_FILE, LEV0, SYMLINK_LEV0);
81317c478bd9Sstevel@tonic-gate 
8132da6c28aaSamw 		if (exitflag) {
81337c478bd9Sstevel@tonic-gate 			break;
81347c478bd9Sstevel@tonic-gate 		}
81357c478bd9Sstevel@tonic-gate 
8136da6c28aaSamw #if defined(_PC_SATTR_ENABLED)
8137da6c28aaSamw 		/*
8138da6c28aaSamw 		 * If both -/ and -@ were specified, then archive the
8139da6c28aaSamw 		 * attribute's extended system attributes and hidden directory
8140da6c28aaSamw 		 * by making a recursive call to xattrs_put().
8141da6c28aaSamw 		 */
8142da6c28aaSamw 		if (!rw_sysattr && saflag && atflag && (rc != PUT_AS_LINK) &&
8143da6c28aaSamw 		    (Hiddendir == 0)) {
8144da6c28aaSamw 
8145da6c28aaSamw 			xattrs_put(longname, shortname, parent, dp->d_name);
8146da6c28aaSamw 
8147da6c28aaSamw 			/*
8148da6c28aaSamw 			 * Change back to the parent's attribute directory
8149da6c28aaSamw 			 * to process any further attributes.
8150da6c28aaSamw 			 */
8151da6c28aaSamw 			if (fchdir(fd) < 0) {
8152da6c28aaSamw 				vperror(0, gettext(
8153da6c28aaSamw 				    "cannot change back to attribute directory "
8154da6c28aaSamw 				    "of file %s"), longname);
8155da6c28aaSamw 				break;
8156da6c28aaSamw 			}
8157da6c28aaSamw 		}
8158da6c28aaSamw #endif	/* _PC_SATTR_ENABLED */
8159da6c28aaSamw 	}
8160da6c28aaSamw 
8161da6c28aaSamw 	if (attrinfo != NULL) {
8162da6c28aaSamw 		if (attrinfo->attr_parent != NULL) {
8163da6c28aaSamw 			free(attrinfo->attr_parent);
8164da6c28aaSamw 		}
8165da6c28aaSamw 		free(attrinfo->attr_path);
8166da6c28aaSamw 		free(attrinfo);
8167da6c28aaSamw 	}
81687c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
8169da6c28aaSamw 	if (fd != -1) {
8170da6c28aaSamw 		(void) close(fd);
8171da6c28aaSamw 	}
8172da6c28aaSamw 
8173da6c28aaSamw 	/* Change back to the parent directory of the base file */
8174da6c28aaSamw 	if (attrparent == NULL) {
8175da6c28aaSamw 		(void) chdir(parent);
8176da6c28aaSamw 	}
8177ced83f9bSceastha 	Hiddendir = 0;
81787c478bd9Sstevel@tonic-gate }
81797c478bd9Sstevel@tonic-gate #else
81807c478bd9Sstevel@tonic-gate static void
8181da6c28aaSamw xattrs_put(char *longname, char *shortname, char *parent, char *attrppath)
81827c478bd9Sstevel@tonic-gate {
81837c478bd9Sstevel@tonic-gate }
81847c478bd9Sstevel@tonic-gate #endif /* O_XATTR */
81857c478bd9Sstevel@tonic-gate 
81867c478bd9Sstevel@tonic-gate static int
8187da6c28aaSamw put_link(char *name, char *longname, char *component, char *longattrname,
81887c478bd9Sstevel@tonic-gate     char *prefix, int filetype, char type)
81897c478bd9Sstevel@tonic-gate {
81907c478bd9Sstevel@tonic-gate 
81917c478bd9Sstevel@tonic-gate 	if (stbuf.st_nlink > 1) {
81927c478bd9Sstevel@tonic-gate 		struct linkbuf *lp;
81937c478bd9Sstevel@tonic-gate 		int found = 0;
81947c478bd9Sstevel@tonic-gate 
81957c478bd9Sstevel@tonic-gate 		for (lp = ihead; lp != NULL; lp = lp->nextp)
81967c478bd9Sstevel@tonic-gate 			if (lp->inum == stbuf.st_ino &&
81977c478bd9Sstevel@tonic-gate 			    lp->devnum == stbuf.st_dev) {
81987c478bd9Sstevel@tonic-gate 				found++;
81997c478bd9Sstevel@tonic-gate 				break;
82007c478bd9Sstevel@tonic-gate 			}
82017c478bd9Sstevel@tonic-gate 		if (found) {
82027c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
82037c478bd9Sstevel@tonic-gate 			if (filetype == XATTR_FILE)
8204da6c28aaSamw 				if (put_xattr_hdr(longname, component,
8205da6c28aaSamw 				    longattrname, prefix, type, filetype, lp)) {
82067c478bd9Sstevel@tonic-gate 					goto out;
82077c478bd9Sstevel@tonic-gate 			}
82087c478bd9Sstevel@tonic-gate #endif
82097c478bd9Sstevel@tonic-gate 			stbuf.st_size = (off_t)0;
82107c478bd9Sstevel@tonic-gate 			if (filetype != XATTR_FILE) {
82117c478bd9Sstevel@tonic-gate 				tomodes(&stbuf);
82127c478bd9Sstevel@tonic-gate 				if (chk_path_build(name, longname, lp->pathname,
82137c478bd9Sstevel@tonic-gate 				    prefix, type, filetype) > 0) {
82147c478bd9Sstevel@tonic-gate 					goto out;
82157c478bd9Sstevel@tonic-gate 				}
82167c478bd9Sstevel@tonic-gate 			}
82177c478bd9Sstevel@tonic-gate 
82187c478bd9Sstevel@tonic-gate 			if (mulvol && tapepos + 1 >= blocklim)
82197c478bd9Sstevel@tonic-gate 				newvol();
82207c478bd9Sstevel@tonic-gate 			(void) writetbuf((char *)&dblock, 1);
82217c478bd9Sstevel@tonic-gate 			/*
82227c478bd9Sstevel@tonic-gate 			 * write_ancillary() is not needed here.
82237c478bd9Sstevel@tonic-gate 			 * The first link is handled in the following
82247c478bd9Sstevel@tonic-gate 			 * else statement. No need to process ACLs
82257c478bd9Sstevel@tonic-gate 			 * for other hard links since they are the
82267c478bd9Sstevel@tonic-gate 			 * same file.
82277c478bd9Sstevel@tonic-gate 			 */
82287c478bd9Sstevel@tonic-gate 
82297c478bd9Sstevel@tonic-gate 			if (vflag) {
82307c478bd9Sstevel@tonic-gate #ifdef DEBUG
82317c478bd9Sstevel@tonic-gate 				if (NotTape)
82327c478bd9Sstevel@tonic-gate 					DEBUG("seek = %" FMT_blkcnt_t
82337c478bd9Sstevel@tonic-gate 					    "K\t", K(tapepos), 0);
82347c478bd9Sstevel@tonic-gate #endif
82357c478bd9Sstevel@tonic-gate 				if (filetype == XATTR_FILE) {
82367c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext(
82377c478bd9Sstevel@tonic-gate 					    "a %s attribute %s link to "
8238da6c28aaSamw 					    "%s attribute %s\n"),
8239da6c28aaSamw 					    name, component, name,
8240da6c28aaSamw 					    lp->attrname);
82417c478bd9Sstevel@tonic-gate 				} else {
82427c478bd9Sstevel@tonic-gate 					(void) fprintf(vfile, gettext(
82437c478bd9Sstevel@tonic-gate 					    "a %s link to %s\n"),
82447c478bd9Sstevel@tonic-gate 					    longname, lp->pathname);
82457c478bd9Sstevel@tonic-gate 				}
82467c478bd9Sstevel@tonic-gate 			}
82477c478bd9Sstevel@tonic-gate 			lp->count--;
82487c478bd9Sstevel@tonic-gate 			return (0);
82497c478bd9Sstevel@tonic-gate 		} else {
82507c478bd9Sstevel@tonic-gate 			lp = (struct linkbuf *)getmem(sizeof (*lp));
82517c478bd9Sstevel@tonic-gate 			if (lp != (struct linkbuf *)NULL) {
82527c478bd9Sstevel@tonic-gate 				lp->nextp = ihead;
82537c478bd9Sstevel@tonic-gate 				ihead = lp;
82547c478bd9Sstevel@tonic-gate 				lp->inum = stbuf.st_ino;
82557c478bd9Sstevel@tonic-gate 				lp->devnum = stbuf.st_dev;
82567c478bd9Sstevel@tonic-gate 				lp->count = stbuf.st_nlink - 1;
82577c478bd9Sstevel@tonic-gate 				if (filetype == XATTR_FILE) {
82587c478bd9Sstevel@tonic-gate 					(void) strcpy(lp->pathname, longname);
8259da6c28aaSamw 					(void) strcpy(lp->attrname,
8260da6c28aaSamw 					    component);
82617c478bd9Sstevel@tonic-gate 				} else {
82627c478bd9Sstevel@tonic-gate 					(void) strcpy(lp->pathname, longname);
82637c478bd9Sstevel@tonic-gate 					(void) strcpy(lp->attrname, "");
82647c478bd9Sstevel@tonic-gate 				}
82657c478bd9Sstevel@tonic-gate 			}
82667c478bd9Sstevel@tonic-gate 		}
82677c478bd9Sstevel@tonic-gate 	}
82687c478bd9Sstevel@tonic-gate 
82697c478bd9Sstevel@tonic-gate out:
82707c478bd9Sstevel@tonic-gate 	return (1);
82717c478bd9Sstevel@tonic-gate }
82727c478bd9Sstevel@tonic-gate 
82737c478bd9Sstevel@tonic-gate static int
8274da6c28aaSamw put_extra_attributes(char *longname, char *shortname, char *longattrname,
8275da6c28aaSamw     char *prefix, int filetype, char typeflag)
82767c478bd9Sstevel@tonic-gate {
8277fa9e4066Sahrens 	static acl_t *aclp = NULL;
8278fa9e4066Sahrens 	int error;
82797c478bd9Sstevel@tonic-gate 
8280fa9e4066Sahrens 	if (aclp != NULL) {
8281fa9e4066Sahrens 		acl_free(aclp);
82827c478bd9Sstevel@tonic-gate 		aclp = NULL;
82837c478bd9Sstevel@tonic-gate 	}
82847c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
8285da6c28aaSamw 	if ((atflag || saflag) && (filetype == XATTR_FILE)) {
8286da6c28aaSamw 		if (put_xattr_hdr(longname, shortname, longattrname, prefix,
82877c478bd9Sstevel@tonic-gate 		    typeflag, filetype, NULL)) {
82887c478bd9Sstevel@tonic-gate 			return (1);
82897c478bd9Sstevel@tonic-gate 		}
82907c478bd9Sstevel@tonic-gate 	}
82917c478bd9Sstevel@tonic-gate #endif
82927c478bd9Sstevel@tonic-gate 
82937c478bd9Sstevel@tonic-gate 	/* ACL support */
82947c478bd9Sstevel@tonic-gate 	if (pflag) {
82957c478bd9Sstevel@tonic-gate 		char	*secinfo = NULL;
82967c478bd9Sstevel@tonic-gate 		int	len = 0;
82977c478bd9Sstevel@tonic-gate 
82987c478bd9Sstevel@tonic-gate 		/* ACL support */
82997c478bd9Sstevel@tonic-gate 		if (((stbuf.st_mode & S_IFMT) != S_IFLNK)) {
83007c478bd9Sstevel@tonic-gate 			/*
83017c478bd9Sstevel@tonic-gate 			 * Get ACL info: dont bother allocating space if
8302fa9e4066Sahrens 			 * there is only a trivial ACL.
83037c478bd9Sstevel@tonic-gate 			 */
8304fa9e4066Sahrens 			if ((error = acl_get(shortname, ACL_NO_TRIVIAL,
8305fa9e4066Sahrens 			    &aclp)) != 0) {
83067c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
8307fa9e4066Sahrens 				    "%s: failed to retrieve acl : %s\n"),
8308fa9e4066Sahrens 				    longname, acl_strerror(error));
83097c478bd9Sstevel@tonic-gate 				return (1);
83107c478bd9Sstevel@tonic-gate 			}
83117c478bd9Sstevel@tonic-gate 		}
83127c478bd9Sstevel@tonic-gate 
83137c478bd9Sstevel@tonic-gate 		/* append security attributes if any */
8314fa9e4066Sahrens 		if (aclp != NULL) {
831545916cd2Sjpk 			(void) append_secattr(&secinfo, &len, acl_cnt(aclp),
8316b249c65cSmarks 			    acl_totext(aclp, ACL_APPEND_ID | ACL_COMPACT_FMT |
8317b249c65cSmarks 			    ACL_SID_FMT), (acl_type(aclp) == ACLENT_T) ?
8318b249c65cSmarks 			    UFSD_ACL : ACE_ACL);
831945916cd2Sjpk 		}
832045916cd2Sjpk 
832145916cd2Sjpk 		if (Tflag) {
832245916cd2Sjpk 			/* append Trusted Extensions extended attributes */
832345916cd2Sjpk 			append_ext_attr(shortname, &secinfo, &len);
832445916cd2Sjpk 			(void) write_ancillary(&dblock, secinfo, len, ACL_HDR);
832545916cd2Sjpk 
832645916cd2Sjpk 		} else if (aclp != NULL) {
83277c478bd9Sstevel@tonic-gate 			(void) write_ancillary(&dblock, secinfo, len, ACL_HDR);
83287c478bd9Sstevel@tonic-gate 		}
83297c478bd9Sstevel@tonic-gate 	}
83307c478bd9Sstevel@tonic-gate 	return (0);
83317c478bd9Sstevel@tonic-gate }
83327c478bd9Sstevel@tonic-gate 
83337c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
83347c478bd9Sstevel@tonic-gate static int
8335da6c28aaSamw put_xattr_hdr(char *longname, char *shortname, char *longattrname, char *prefix,
83367c478bd9Sstevel@tonic-gate 	int typeflag, int filetype, struct linkbuf *lp)
83377c478bd9Sstevel@tonic-gate {
83387c478bd9Sstevel@tonic-gate 	char *lname = NULL;
83397c478bd9Sstevel@tonic-gate 	char *sname = NULL;
83407c478bd9Sstevel@tonic-gate 	int  error = 0;
83417c478bd9Sstevel@tonic-gate 	static char *attrbuf = NULL;
83427c478bd9Sstevel@tonic-gate 	int attrlen;
83437c478bd9Sstevel@tonic-gate 
83447c478bd9Sstevel@tonic-gate 	lname = malloc(sizeof (char) * strlen("/dev/null") + 1 +
83457c478bd9Sstevel@tonic-gate 	    strlen(shortname) + strlen(".hdr") + 1);
83467c478bd9Sstevel@tonic-gate 
83477c478bd9Sstevel@tonic-gate 	if (lname == NULL) {
83487c478bd9Sstevel@tonic-gate 		fatal(gettext("Out of Memory."));
83497c478bd9Sstevel@tonic-gate 	}
83507c478bd9Sstevel@tonic-gate 	sname = malloc(sizeof (char) * strlen(shortname) +
8351da6c28aaSamw 	    strlen(".hdr") + 1);
83527c478bd9Sstevel@tonic-gate 	if (sname == NULL) {
83537c478bd9Sstevel@tonic-gate 		fatal(gettext("Out of Memory."));
83547c478bd9Sstevel@tonic-gate 	}
83557c478bd9Sstevel@tonic-gate 
83567c478bd9Sstevel@tonic-gate 	(void) sprintf(sname, "%s.hdr", shortname);
83577c478bd9Sstevel@tonic-gate 	(void) sprintf(lname, "/dev/null/%s", sname);
83587c478bd9Sstevel@tonic-gate 
83597c478bd9Sstevel@tonic-gate 	if (strlcpy(dblock.dbuf.name, lname, sizeof (dblock.dbuf.name)) >=
83607c478bd9Sstevel@tonic-gate 	    sizeof (dblock.dbuf.name)) {
83617c478bd9Sstevel@tonic-gate 		fatal(gettext(
83627c478bd9Sstevel@tonic-gate 		    "Buffer overflow writing extended attribute file name"));
83637c478bd9Sstevel@tonic-gate 	}
83647c478bd9Sstevel@tonic-gate 
83657c478bd9Sstevel@tonic-gate 	/*
83667c478bd9Sstevel@tonic-gate 	 * dump extended attr lookup info
83677c478bd9Sstevel@tonic-gate 	 */
8368da6c28aaSamw 	prepare_xattr(&attrbuf, longname, longattrname, typeflag, lp, &attrlen);
83697c478bd9Sstevel@tonic-gate 	write_ancillary(&dblock, attrbuf, attrlen, _XATTR_HDRTYPE);
83707c478bd9Sstevel@tonic-gate 
83717c478bd9Sstevel@tonic-gate 	(void) sprintf(lname, "/dev/null/%s", shortname);
83727c478bd9Sstevel@tonic-gate 	(void) strncpy(dblock.dbuf.name, sname, NAMSIZ);
83737c478bd9Sstevel@tonic-gate 
83747c478bd9Sstevel@tonic-gate 	/*
83757c478bd9Sstevel@tonic-gate 	 * Set up filename for attribute
83767c478bd9Sstevel@tonic-gate 	 */
83777c478bd9Sstevel@tonic-gate 
83787c478bd9Sstevel@tonic-gate 	error = build_dblock(lname, tchar, '0', filetype,
83797c478bd9Sstevel@tonic-gate 	    &stbuf, stbuf.st_dev, prefix);
83807c478bd9Sstevel@tonic-gate 	free(lname);
83817c478bd9Sstevel@tonic-gate 	free(sname);
83827c478bd9Sstevel@tonic-gate 
83837c478bd9Sstevel@tonic-gate 	return (error);
83847c478bd9Sstevel@tonic-gate }
83857c478bd9Sstevel@tonic-gate #endif
83867c478bd9Sstevel@tonic-gate 
83877c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
83887c478bd9Sstevel@tonic-gate static int
8389da6c28aaSamw read_xattr_hdr(attr_data_t **attrinfo)
83907c478bd9Sstevel@tonic-gate {
83917c478bd9Sstevel@tonic-gate 	char		buf[TBLOCK];
8392da6c28aaSamw 	char		*attrparent = NULL;
83937c478bd9Sstevel@tonic-gate 	blkcnt_t	blocks;
83947c478bd9Sstevel@tonic-gate 	char		*tp;
83957c478bd9Sstevel@tonic-gate 	off_t		bytes;
83967c478bd9Sstevel@tonic-gate 	int		comp_len, link_len;
83977c478bd9Sstevel@tonic-gate 	int		namelen;
8398da6c28aaSamw 	int		attrparentlen;
8399da6c28aaSamw 	int		parentfilelen;
84007c478bd9Sstevel@tonic-gate 
84017c478bd9Sstevel@tonic-gate 	if (dblock.dbuf.typeflag != _XATTR_HDRTYPE)
84027c478bd9Sstevel@tonic-gate 		return (1);
84037c478bd9Sstevel@tonic-gate 
84047c478bd9Sstevel@tonic-gate 	bytes = stbuf.st_size;
84057c478bd9Sstevel@tonic-gate 	if ((xattrhead = calloc(1, (int)bytes)) == NULL) {
84067c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
84077c478bd9Sstevel@tonic-gate 		    "Insufficient memory for extended attribute\n"));
84087c478bd9Sstevel@tonic-gate 		return (1);
84097c478bd9Sstevel@tonic-gate 	}
84107c478bd9Sstevel@tonic-gate 
84117c478bd9Sstevel@tonic-gate 	tp = (char *)xattrhead;
84127c478bd9Sstevel@tonic-gate 	blocks = TBLOCKS(bytes);
84137c478bd9Sstevel@tonic-gate 	while (blocks-- > 0) {
84147c478bd9Sstevel@tonic-gate 		readtape(buf);
84157c478bd9Sstevel@tonic-gate 		if (bytes <= TBLOCK) {
84167c478bd9Sstevel@tonic-gate 			(void) memcpy(tp, buf, (size_t)bytes);
84177c478bd9Sstevel@tonic-gate 			break;
84187c478bd9Sstevel@tonic-gate 		} else {
84197c478bd9Sstevel@tonic-gate 			(void) memcpy(tp, buf, TBLOCK);
84207c478bd9Sstevel@tonic-gate 			tp += TBLOCK;
84217c478bd9Sstevel@tonic-gate 		}
84227c478bd9Sstevel@tonic-gate 		bytes -= TBLOCK;
84237c478bd9Sstevel@tonic-gate 	}
84247c478bd9Sstevel@tonic-gate 
84257c478bd9Sstevel@tonic-gate 	/*
84267c478bd9Sstevel@tonic-gate 	 * Validate that we can handle header format
84277c478bd9Sstevel@tonic-gate 	 */
84287c478bd9Sstevel@tonic-gate 	if (strcmp(xattrhead->h_version, XATTR_ARCH_VERS) != 0) {
84297c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
84307c478bd9Sstevel@tonic-gate 		    gettext("Unknown extended attribute format encountered\n"));
84317c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
84327c478bd9Sstevel@tonic-gate 		    gettext("Disabling extended attribute parsing\n"));
84337c478bd9Sstevel@tonic-gate 		xattrbadhead = 1;
84347c478bd9Sstevel@tonic-gate 		return (0);
84357c478bd9Sstevel@tonic-gate 	}
84367c478bd9Sstevel@tonic-gate 	(void) sscanf(xattrhead->h_component_len, "%10d", &comp_len);
84377c478bd9Sstevel@tonic-gate 	(void) sscanf(xattrhead->h_link_component_len,	"%10d", &link_len);
84387c478bd9Sstevel@tonic-gate 	xattrp = (struct xattr_buf *)(((char *)xattrhead) +
84397c478bd9Sstevel@tonic-gate 	    sizeof (struct xattr_hdr));
84407c478bd9Sstevel@tonic-gate 	(void) sscanf(xattrp->h_namesz, "%7d", &namelen);
84417c478bd9Sstevel@tonic-gate 	if (link_len > 0)
84427c478bd9Sstevel@tonic-gate 		xattr_linkp = (struct xattr_buf *)
84437c478bd9Sstevel@tonic-gate 		    ((int)xattrp + (int)comp_len);
84447c478bd9Sstevel@tonic-gate 	else
84457c478bd9Sstevel@tonic-gate 		xattr_linkp = NULL;
84467c478bd9Sstevel@tonic-gate 
8447da6c28aaSamw 	/*
8448da6c28aaSamw 	 * Gather the attribute path from the filename and attrnames section.
8449da6c28aaSamw 	 * The filename and attrnames section can be composed of two or more
8450da6c28aaSamw 	 * path segments separated by a null character.  The first segment
8451da6c28aaSamw 	 * is the path to the parent file that roots the entire sequence in
8452da6c28aaSamw 	 * the normal name space. The remaining segments describes a path
8453da6c28aaSamw 	 * rooted at the hidden extended attribute directory of the leaf file of
8454da6c28aaSamw 	 * the previous segment, making it possible to name attributes on
8455da6c28aaSamw 	 * attributes.
8456da6c28aaSamw 	 */
8457da6c28aaSamw 	parentfilelen = strlen(xattrp->h_names);
8458da6c28aaSamw 	xattrapath = xattrp->h_names + parentfilelen + 1;
8459da6c28aaSamw 	if ((strlen(xattrapath) + parentfilelen + 2) < namelen) {
8460da6c28aaSamw 		/*
8461da6c28aaSamw 		 * The attrnames section contains a system attribute on an
8462da6c28aaSamw 		 * attribute.  Save the name of the attribute for use later,
8463da6c28aaSamw 		 * and replace the null separating the attribute name from
8464da6c28aaSamw 		 * the system attribute name with a '/' so that xattrapath can
8465da6c28aaSamw 		 * be used to display messages with the full attribute path name
8466da6c28aaSamw 		 * rooted at the hidden attribute directory of the base file
8467da6c28aaSamw 		 * in normal name space.
8468da6c28aaSamw 		 */
8469da6c28aaSamw 		attrparent = strdup(xattrapath);
8470da6c28aaSamw 		attrparentlen = strlen(attrparent);
8471da6c28aaSamw 		xattrapath[attrparentlen] = '/';
8472da6c28aaSamw 	}
8473da6c28aaSamw 	if ((fill_in_attr_info((attrparent == NULL) ? xattrapath :
8474da6c28aaSamw 	    xattrapath + attrparentlen + 1, xattrapath, attrparent,
8475da6c28aaSamw 	    -1, 0, attrinfo)) == 1) {
8476da6c28aaSamw 		free(attrparent);
8477da6c28aaSamw 		return (1);
8478da6c28aaSamw 	}
8479da6c28aaSamw 
8480da6c28aaSamw 	/* Gather link info */
84817c478bd9Sstevel@tonic-gate 	if (xattr_linkp) {
84827c478bd9Sstevel@tonic-gate 		xattr_linkaname = xattr_linkp->h_names +
84837c478bd9Sstevel@tonic-gate 		    strlen(xattr_linkp->h_names) + 1;
84847c478bd9Sstevel@tonic-gate 	} else {
84857c478bd9Sstevel@tonic-gate 		xattr_linkaname = NULL;
84867c478bd9Sstevel@tonic-gate 	}
8487d2443e76Smarks 
84887c478bd9Sstevel@tonic-gate 	return (0);
84897c478bd9Sstevel@tonic-gate }
84907c478bd9Sstevel@tonic-gate #else
84917c478bd9Sstevel@tonic-gate static int
8492da6c28aaSamw read_xattr_hdr(attr_data_t **attrinfo)
84937c478bd9Sstevel@tonic-gate {
84947c478bd9Sstevel@tonic-gate 	return (0);
84957c478bd9Sstevel@tonic-gate }
84967c478bd9Sstevel@tonic-gate #endif
84977c478bd9Sstevel@tonic-gate 
84987c478bd9Sstevel@tonic-gate /*
84997c478bd9Sstevel@tonic-gate  * skip over extra slashes in string.
85007c478bd9Sstevel@tonic-gate  *
85017c478bd9Sstevel@tonic-gate  * For example:
85027c478bd9Sstevel@tonic-gate  * /usr/tmp/////
85037c478bd9Sstevel@tonic-gate  *
85047c478bd9Sstevel@tonic-gate  * would return pointer at
85057c478bd9Sstevel@tonic-gate  * /usr/tmp/////
85067c478bd9Sstevel@tonic-gate  *         ^
85077c478bd9Sstevel@tonic-gate  */
85087c478bd9Sstevel@tonic-gate static char *
85097c478bd9Sstevel@tonic-gate skipslashes(char *string, char *start)
85107c478bd9Sstevel@tonic-gate {
85117c478bd9Sstevel@tonic-gate 	while ((string > start) && *(string - 1) == '/') {
85127c478bd9Sstevel@tonic-gate 		string--;
85137c478bd9Sstevel@tonic-gate 	}
85147c478bd9Sstevel@tonic-gate 
85157c478bd9Sstevel@tonic-gate 	return (string);
85167c478bd9Sstevel@tonic-gate }
85177c478bd9Sstevel@tonic-gate 
85187c478bd9Sstevel@tonic-gate /*
85197c478bd9Sstevel@tonic-gate  * Return the parent directory of a given path.
85207c478bd9Sstevel@tonic-gate  *
85217c478bd9Sstevel@tonic-gate  * Examples:
85227c478bd9Sstevel@tonic-gate  * /usr/tmp return /usr
85237c478bd9Sstevel@tonic-gate  * /usr/tmp/file return /usr/tmp
85247c478bd9Sstevel@tonic-gate  * /  returns .
85257c478bd9Sstevel@tonic-gate  * /usr returns /
85267c478bd9Sstevel@tonic-gate  * file returns .
85277c478bd9Sstevel@tonic-gate  *
85287c478bd9Sstevel@tonic-gate  * dir is assumed to be at least as big as path.
85297c478bd9Sstevel@tonic-gate  */
85307c478bd9Sstevel@tonic-gate static void
85317c478bd9Sstevel@tonic-gate get_parent(char *path, char *dir)
85327c478bd9Sstevel@tonic-gate {
85337c478bd9Sstevel@tonic-gate 	char *s;
85347c478bd9Sstevel@tonic-gate 	char tmpdir[PATH_MAX + 1];
85357c478bd9Sstevel@tonic-gate 
85367c478bd9Sstevel@tonic-gate 	if (strlen(path) > PATH_MAX) {
85377c478bd9Sstevel@tonic-gate 		fatal(gettext("pathname is too long"));
85387c478bd9Sstevel@tonic-gate 	}
85397c478bd9Sstevel@tonic-gate 	(void) strcpy(tmpdir, path);
85407c478bd9Sstevel@tonic-gate 	chop_endslashes(tmpdir);
85417c478bd9Sstevel@tonic-gate 
85427c478bd9Sstevel@tonic-gate 	if ((s = strrchr(tmpdir, '/')) == NULL) {
85437c478bd9Sstevel@tonic-gate 		(void) strcpy(dir, ".");
85447c478bd9Sstevel@tonic-gate 	} else {
85457c478bd9Sstevel@tonic-gate 		s = skipslashes(s, tmpdir);
85467c478bd9Sstevel@tonic-gate 		*s = '\0';
85477c478bd9Sstevel@tonic-gate 		if (s == tmpdir)
85487c478bd9Sstevel@tonic-gate 			(void) strcpy(dir, "/");
85497c478bd9Sstevel@tonic-gate 		else
85507c478bd9Sstevel@tonic-gate 			(void) strcpy(dir, tmpdir);
85517c478bd9Sstevel@tonic-gate 	}
85527c478bd9Sstevel@tonic-gate }
85537c478bd9Sstevel@tonic-gate 
85547c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
85557c478bd9Sstevel@tonic-gate static char *
85567c478bd9Sstevel@tonic-gate get_component(char *path)
85577c478bd9Sstevel@tonic-gate {
85587c478bd9Sstevel@tonic-gate 	char *ptr;
85597c478bd9Sstevel@tonic-gate 
85607c478bd9Sstevel@tonic-gate 	ptr = strrchr(path, '/');
85617c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
85627c478bd9Sstevel@tonic-gate 		return (path);
85637c478bd9Sstevel@tonic-gate 	} else {
85647c478bd9Sstevel@tonic-gate 		/*
85657c478bd9Sstevel@tonic-gate 		 * Handle trailing slash
85667c478bd9Sstevel@tonic-gate 		 */
85677c478bd9Sstevel@tonic-gate 		if (*(ptr + 1) == '\0')
85687c478bd9Sstevel@tonic-gate 			return (ptr);
85697c478bd9Sstevel@tonic-gate 		else
85707c478bd9Sstevel@tonic-gate 			return (ptr + 1);
85717c478bd9Sstevel@tonic-gate 	}
85727c478bd9Sstevel@tonic-gate }
85737c478bd9Sstevel@tonic-gate #else
85747c478bd9Sstevel@tonic-gate static char *
85757c478bd9Sstevel@tonic-gate get_component(char *path)
85767c478bd9Sstevel@tonic-gate {
85777c478bd9Sstevel@tonic-gate 	return (path);
85787c478bd9Sstevel@tonic-gate }
85797c478bd9Sstevel@tonic-gate #endif
85807c478bd9Sstevel@tonic-gate 
8581da6c28aaSamw #if defined(O_XATTR)
85827c478bd9Sstevel@tonic-gate static int
8583da6c28aaSamw retry_open_attr(int pdirfd, int cwd, char *dirp, char *pattr, char *name,
8584da6c28aaSamw     int oflag, mode_t mode)
85857c478bd9Sstevel@tonic-gate {
8586da6c28aaSamw 	int dirfd;
8587da6c28aaSamw 	int ofilefd = -1;
85887c478bd9Sstevel@tonic-gate 	struct timeval times[2];
85897c478bd9Sstevel@tonic-gate 	mode_t newmode;
85907c478bd9Sstevel@tonic-gate 	struct stat parentstat;
8591d2443e76Smarks 	acl_t *aclp = NULL;
8592d2443e76Smarks 	int error;
85937c478bd9Sstevel@tonic-gate 
85947c478bd9Sstevel@tonic-gate 	/*
85957c478bd9Sstevel@tonic-gate 	 * We couldn't get to attrdir. See if its
85967c478bd9Sstevel@tonic-gate 	 * just a mode problem on the parent file.
85977c478bd9Sstevel@tonic-gate 	 * for example: a mode such as r-xr--r--
8598da6c28aaSamw 	 * on a ufs file system without extended
8599da6c28aaSamw 	 * system attribute support won't let us
8600da6c28aaSamw 	 * create an attribute dir if it doesn't
8601da6c28aaSamw 	 * already exist, and on a ufs file system
8602da6c28aaSamw 	 * with extended system attribute support
8603da6c28aaSamw 	 * won't let us open the attribute for
8604da6c28aaSamw 	 * write.
8605d2443e76Smarks 	 *
8606d2443e76Smarks 	 * If file has a non-trivial ACL, then save it
8607d2443e76Smarks 	 * off so that we can place it back on after doing
8608d2443e76Smarks 	 * chmod's.
86097c478bd9Sstevel@tonic-gate 	 */
8610da6c28aaSamw 	if ((dirfd = openat(cwd, (pattr == NULL) ? dirp : pattr,
8611da6c28aaSamw 	    O_RDONLY)) == -1) {
8612d2443e76Smarks 		return (-1);
86137c478bd9Sstevel@tonic-gate 	}
8614da6c28aaSamw 	if (fstat(dirfd, &parentstat) == -1) {
8615da6c28aaSamw 		(void) fprintf(stderr, gettext(
8616da6c28aaSamw 		    "tar: cannot stat %sfile %s: %s\n"),
8617da6c28aaSamw 		    (pdirfd == -1) ? "" : gettext("parent of "),
8618da6c28aaSamw 		    (pdirfd == -1) ? dirp : name, strerror(errno));
8619da6c28aaSamw 			return (-1);
8620da6c28aaSamw 	}
8621da6c28aaSamw 	if ((error = facl_get(dirfd, ACL_NO_TRIVIAL, &aclp)) != 0) {
8622da6c28aaSamw 		(void) fprintf(stderr, gettext(
8623da6c28aaSamw 		    "tar: failed to retrieve ACL on %sfile %s: %s\n"),
8624da6c28aaSamw 		    (pdirfd == -1) ? "" : gettext("parent of "),
8625da6c28aaSamw 		    (pdirfd == -1) ? dirp : name, strerror(errno));
8626d2443e76Smarks 			return (-1);
8627d2443e76Smarks 	}
8628d2443e76Smarks 
86297c478bd9Sstevel@tonic-gate 	newmode = S_IWUSR | parentstat.st_mode;
8630da6c28aaSamw 	if (fchmod(dirfd, newmode) == -1) {
86317c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
8632da6c28aaSamw 		    gettext(
8633da6c28aaSamw 		    "tar: cannot fchmod %sfile %s to %o: %s\n"),
8634da6c28aaSamw 		    (pdirfd == -1) ? "" : gettext("parent of "),
8635da6c28aaSamw 		    (pdirfd == -1) ? dirp : name, newmode, strerror(errno));
8636d2443e76Smarks 		if (aclp)
8637d2443e76Smarks 			acl_free(aclp);
8638d2443e76Smarks 		return (-1);
86397c478bd9Sstevel@tonic-gate 	}
86407c478bd9Sstevel@tonic-gate 
8641d2443e76Smarks 
8642da6c28aaSamw 	if (pdirfd == -1) {
8643d2443e76Smarks 		/*
8644da6c28aaSamw 		 * We weren't able to create the attribute directory before.
8645da6c28aaSamw 		 * Now try again.
8646d2443e76Smarks 		 */
8647da6c28aaSamw 		ofilefd = attropen(dirp, ".", oflag);
8648da6c28aaSamw 	} else {
8649da6c28aaSamw 		/*
8650da6c28aaSamw 		 * We weren't able to create open the attribute before.
8651da6c28aaSamw 		 * Now try again.
8652da6c28aaSamw 		 */
8653da6c28aaSamw 		ofilefd = openat(pdirfd, name, oflag, mode);
8654da6c28aaSamw 	}
86557c478bd9Sstevel@tonic-gate 
86567c478bd9Sstevel@tonic-gate 	/*
86577c478bd9Sstevel@tonic-gate 	 * Put mode back to original
86587c478bd9Sstevel@tonic-gate 	 */
8659da6c28aaSamw 	if (fchmod(dirfd, parentstat.st_mode) == -1) {
8660d2443e76Smarks 		(void) fprintf(stderr,
8661da6c28aaSamw 		    gettext("tar: cannot chmod %sfile %s to %o: %s\n"),
8662da6c28aaSamw 		    (pdirfd == -1) ? "" : gettext("parent of "),
8663da6c28aaSamw 		    (pdirfd == -1) ? dirp : name, newmode, strerror(errno));
8664d2443e76Smarks 	}
8665d2443e76Smarks 
8666d2443e76Smarks 	if (aclp) {
8667da6c28aaSamw 		error = facl_set(dirfd, aclp);
8668d2443e76Smarks 		if (error) {
8669d2443e76Smarks 			(void) fprintf(stderr,
8670da6c28aaSamw 			    gettext("tar: failed to set acl entries on "
8671da6c28aaSamw 			    "%sfile %s\n"),
8672da6c28aaSamw 			    (pdirfd == -1) ? "" : gettext("parent of "),
8673da6c28aaSamw 			    (pdirfd == -1) ? dirp : name);
8674d2443e76Smarks 		}
8675d2443e76Smarks 		acl_free(aclp);
8676d2443e76Smarks 	}
86777c478bd9Sstevel@tonic-gate 
86787c478bd9Sstevel@tonic-gate 	/*
86797c478bd9Sstevel@tonic-gate 	 * Put back time stamps
86807c478bd9Sstevel@tonic-gate 	 */
86817c478bd9Sstevel@tonic-gate 
86827c478bd9Sstevel@tonic-gate 	times[0].tv_sec = parentstat.st_atime;
86837c478bd9Sstevel@tonic-gate 	times[0].tv_usec = 0;
86847c478bd9Sstevel@tonic-gate 	times[1].tv_sec = parentstat.st_mtime;
86857c478bd9Sstevel@tonic-gate 	times[1].tv_usec = 0;
86867c478bd9Sstevel@tonic-gate 
8687da6c28aaSamw 	(void) futimesat(cwd, (pattr == NULL) ? dirp : pattr, times);
8688da6c28aaSamw 
8689da6c28aaSamw 	(void) close(dirfd);
8690da6c28aaSamw 
8691da6c28aaSamw 	return (ofilefd);
86927c478bd9Sstevel@tonic-gate }
8693da6c28aaSamw #endif
86947c478bd9Sstevel@tonic-gate 
86957c478bd9Sstevel@tonic-gate #if !defined(O_XATTR)
86967c478bd9Sstevel@tonic-gate static int
86977c478bd9Sstevel@tonic-gate openat64(int fd, const char *name, int oflag, mode_t cmode)
86987c478bd9Sstevel@tonic-gate {
86997c478bd9Sstevel@tonic-gate 	return (open64(name, oflag, cmode));
87007c478bd9Sstevel@tonic-gate }
87017c478bd9Sstevel@tonic-gate 
87027c478bd9Sstevel@tonic-gate static int
87037c478bd9Sstevel@tonic-gate openat(int fd, const char *name, int oflag, mode_t cmode)
87047c478bd9Sstevel@tonic-gate {
87057c478bd9Sstevel@tonic-gate 	return (open(name, oflag, cmode));
87067c478bd9Sstevel@tonic-gate }
87077c478bd9Sstevel@tonic-gate 
87087c478bd9Sstevel@tonic-gate static int
87097c478bd9Sstevel@tonic-gate fchownat(int fd, const char *name, uid_t owner, gid_t group, int flag)
87107c478bd9Sstevel@tonic-gate {
87117c478bd9Sstevel@tonic-gate 	if (flag == AT_SYMLINK_NOFOLLOW)
87127c478bd9Sstevel@tonic-gate 		return (lchown(name, owner, group));
87137c478bd9Sstevel@tonic-gate 	else
87147c478bd9Sstevel@tonic-gate 		return (chown(name, owner, group));
87157c478bd9Sstevel@tonic-gate }
87167c478bd9Sstevel@tonic-gate 
87177c478bd9Sstevel@tonic-gate static int
87187c478bd9Sstevel@tonic-gate renameat(int fromfd, char *old, int tofd, char *new)
87197c478bd9Sstevel@tonic-gate {
87207c478bd9Sstevel@tonic-gate 	return (rename(old, new));
87217c478bd9Sstevel@tonic-gate }
87227c478bd9Sstevel@tonic-gate 
87237c478bd9Sstevel@tonic-gate static int
87247c478bd9Sstevel@tonic-gate futimesat(int fd, char *path, struct timeval times[2])
87257c478bd9Sstevel@tonic-gate {
87267c478bd9Sstevel@tonic-gate 	return (utimes(path, times));
87277c478bd9Sstevel@tonic-gate }
87287c478bd9Sstevel@tonic-gate 
87297c478bd9Sstevel@tonic-gate static int
87307c478bd9Sstevel@tonic-gate unlinkat(int dirfd, char *path, int flag)
87317c478bd9Sstevel@tonic-gate {
87327c478bd9Sstevel@tonic-gate 	if (flag == AT_REMOVEDIR)
87337c478bd9Sstevel@tonic-gate 		return (rmdir(path));
87347c478bd9Sstevel@tonic-gate 	else
87357c478bd9Sstevel@tonic-gate 		return (unlink(path));
87367c478bd9Sstevel@tonic-gate }
87377c478bd9Sstevel@tonic-gate 
87387c478bd9Sstevel@tonic-gate static int
87397c478bd9Sstevel@tonic-gate fstatat(int fd, char *path, struct stat *buf, int flag)
87407c478bd9Sstevel@tonic-gate {
87417c478bd9Sstevel@tonic-gate 	if (flag == AT_SYMLINK_NOFOLLOW)
87427c478bd9Sstevel@tonic-gate 		return (lstat(path, buf));
87437c478bd9Sstevel@tonic-gate 	else
87447c478bd9Sstevel@tonic-gate 		return (stat(path, buf));
87457c478bd9Sstevel@tonic-gate }
87467c478bd9Sstevel@tonic-gate 
87477c478bd9Sstevel@tonic-gate static int
87487c478bd9Sstevel@tonic-gate attropen(char *file, char *attr, int omode, mode_t cmode)
87497c478bd9Sstevel@tonic-gate {
87507c478bd9Sstevel@tonic-gate 	errno = ENOTSUP;
87517c478bd9Sstevel@tonic-gate 	return (-1);
87527c478bd9Sstevel@tonic-gate }
87537c478bd9Sstevel@tonic-gate #endif
87547c478bd9Sstevel@tonic-gate 
87557c478bd9Sstevel@tonic-gate static void
87567c478bd9Sstevel@tonic-gate chop_endslashes(char *path)
87577c478bd9Sstevel@tonic-gate {
87587c478bd9Sstevel@tonic-gate 	char *end, *ptr;
87597c478bd9Sstevel@tonic-gate 
87607c478bd9Sstevel@tonic-gate 	/*
87617c478bd9Sstevel@tonic-gate 	 * Chop of slashes, but not if all we have is slashes
87627c478bd9Sstevel@tonic-gate 	 * for example: ////
87637c478bd9Sstevel@tonic-gate 	 * should make no changes, otherwise it will screw up
87647c478bd9Sstevel@tonic-gate 	 * checkdir
87657c478bd9Sstevel@tonic-gate 	 */
87667c478bd9Sstevel@tonic-gate 	end = &path[strlen(path) -1];
87677c478bd9Sstevel@tonic-gate 	if (*end == '/' && end != path) {
87687c478bd9Sstevel@tonic-gate 		ptr = skipslashes(end, path);
87697c478bd9Sstevel@tonic-gate 		if (ptr != NULL && ptr != path) {
87707c478bd9Sstevel@tonic-gate 			*ptr = '\0';
87717c478bd9Sstevel@tonic-gate 		}
87727c478bd9Sstevel@tonic-gate 	}
87737c478bd9Sstevel@tonic-gate }
877445916cd2Sjpk /* Trusted Extensions */
877545916cd2Sjpk 
877645916cd2Sjpk /*
877745916cd2Sjpk  * append_ext_attr():
877845916cd2Sjpk  *
877945916cd2Sjpk  * Append extended attributes and other information into the buffer
878045916cd2Sjpk  * that gets written to the ancillary file.
878145916cd2Sjpk  *
878245916cd2Sjpk  * With option 'T', we create a tarfile which
878345916cd2Sjpk  * has an ancillary file each corresponding archived file.
878445916cd2Sjpk  * Each ancillary file contains 1 or more of the
878545916cd2Sjpk  * following attributes:
878645916cd2Sjpk  *
878745916cd2Sjpk  *	attribute type        attribute		process procedure
878845916cd2Sjpk  *	----------------      ----------------  --------------------------
878945916cd2Sjpk  *   	DIR_TYPE       = 'D'   directory flag	append if a directory
879045916cd2Sjpk  *    	LBL_TYPE       = 'L'   SL[IL] or SL	append ascii label
879145916cd2Sjpk  *
879245916cd2Sjpk  *
879345916cd2Sjpk  */
879445916cd2Sjpk static void
879545916cd2Sjpk append_ext_attr(char *shortname, char **secinfo, int *len)
879645916cd2Sjpk {
879745916cd2Sjpk 	bslabel_t	b_slabel;	/* binary sensitvity label */
879845916cd2Sjpk 	char		*ascii = NULL;	/* ascii label */
879945916cd2Sjpk 
880045916cd2Sjpk 	/*
880145916cd2Sjpk 	 * For each attribute type, append it if it is
880245916cd2Sjpk 	 * relevant to the file type.
880345916cd2Sjpk 	 */
880445916cd2Sjpk 
880545916cd2Sjpk 	/*
880645916cd2Sjpk 	 * For attribute type DIR_TYPE,
880745916cd2Sjpk 	 * append it to the following file type:
880845916cd2Sjpk 	 *
880945916cd2Sjpk 	 *	S_IFDIR: directories
881045916cd2Sjpk 	 */
881145916cd2Sjpk 
881245916cd2Sjpk 	/*
881345916cd2Sjpk 	 * For attribute type LBL_TYPE,
881445916cd2Sjpk 	 * append it to the following file type:
881545916cd2Sjpk 	 *
881645916cd2Sjpk 	 *	S_IFDIR: directories (including mld, sld)
881745916cd2Sjpk 	 *	S_IFLNK: symbolic link
881845916cd2Sjpk 	 *	S_IFREG: regular file but not hard link
881945916cd2Sjpk 	 *	S_IFIFO: FIFO file but not hard link
882045916cd2Sjpk 	 *	S_IFCHR: char special file but not hard link
882145916cd2Sjpk 	 *	S_IFBLK: block special file but not hard link
882245916cd2Sjpk 	 */
882345916cd2Sjpk 	switch (stbuf.st_mode & S_IFMT) {
882445916cd2Sjpk 
882545916cd2Sjpk 	case S_IFDIR:
882645916cd2Sjpk 
882745916cd2Sjpk 		/*
882845916cd2Sjpk 		 * append DIR_TYPE
882945916cd2Sjpk 		 */
883045916cd2Sjpk 		(void) append_secattr(secinfo, len, 1,
883145916cd2Sjpk 		    "\0", DIR_TYPE);
883245916cd2Sjpk 
883345916cd2Sjpk 		/*
883445916cd2Sjpk 		 * Get and append attribute types LBL_TYPE.
883545916cd2Sjpk 		 * For directories, LBL_TYPE contains SL.
883645916cd2Sjpk 		 */
883745916cd2Sjpk 		/* get binary sensitivity label */
883845916cd2Sjpk 		if (getlabel(shortname, &b_slabel) != 0) {
883945916cd2Sjpk 			(void) fprintf(stderr,
884045916cd2Sjpk 			    gettext("tar: can't get sensitvity label for "
884145916cd2Sjpk 			    " %s, getlabel() error: %s\n"),
884245916cd2Sjpk 			    shortname, strerror(errno));
884345916cd2Sjpk 		} else {
884445916cd2Sjpk 			/* get ascii SL */
884545916cd2Sjpk 			if (bsltos(&b_slabel, &ascii,
884645916cd2Sjpk 			    0, 0) <= 0) {
884745916cd2Sjpk 				(void) fprintf(stderr,
884845916cd2Sjpk 				    gettext("tar: can't get ascii SL for"
884945916cd2Sjpk 				    " %s\n"), shortname);
885045916cd2Sjpk 			} else {
885145916cd2Sjpk 				/* append LBL_TYPE */
885245916cd2Sjpk 				(void) append_secattr(secinfo, len,
885345916cd2Sjpk 				    strlen(ascii) + 1, ascii,
885445916cd2Sjpk 				    LBL_TYPE);
885545916cd2Sjpk 
885645916cd2Sjpk 				/* free storage */
885745916cd2Sjpk 				if (ascii != NULL) {
885845916cd2Sjpk 					free(ascii);
885945916cd2Sjpk 					ascii = (char *)0;
886045916cd2Sjpk 				}
886145916cd2Sjpk 			}
886245916cd2Sjpk 
886345916cd2Sjpk 		}
886445916cd2Sjpk 		break;
886545916cd2Sjpk 
886645916cd2Sjpk 	case S_IFLNK:
886745916cd2Sjpk 	case S_IFREG:
886845916cd2Sjpk 	case S_IFIFO:
886945916cd2Sjpk 	case S_IFCHR:
887045916cd2Sjpk 	case S_IFBLK:
887145916cd2Sjpk 
887245916cd2Sjpk 		/* get binary sensitivity label */
887345916cd2Sjpk 		if (getlabel(shortname, &b_slabel) != 0) {
887445916cd2Sjpk 			(void) fprintf(stderr,
887545916cd2Sjpk 			    gettext("tar: can't get sensitivty label for %s, "
887645916cd2Sjpk 			    "getlabel() error: %s\n"),
887745916cd2Sjpk 			    shortname, strerror(errno));
887845916cd2Sjpk 		} else {
887945916cd2Sjpk 			/* get ascii IL[SL] */
888045916cd2Sjpk 			if (bsltos(&b_slabel, &ascii, 0, 0) <= 0) {
888145916cd2Sjpk 				(void) fprintf(stderr,
888245916cd2Sjpk 				    gettext("tar: can't translate sensitivity "
888345916cd2Sjpk 				    " label for %s\n"), shortname);
888445916cd2Sjpk 			} else {
888545916cd2Sjpk 				char *cmw_label;
888645916cd2Sjpk 				size_t  cmw_length;
888745916cd2Sjpk 
888845916cd2Sjpk 				cmw_length = strlen("ADMIN_LOW [] ") +
888945916cd2Sjpk 				    strlen(ascii);
889045916cd2Sjpk 				if ((cmw_label = malloc(cmw_length)) == NULL) {
889145916cd2Sjpk 					(void) fprintf(stderr, gettext(
889245916cd2Sjpk 					    "Insufficient memory for label\n"));
889345916cd2Sjpk 					exit(1);
889445916cd2Sjpk 				}
889545916cd2Sjpk 				/* append LBL_TYPE */
889645916cd2Sjpk 				(void) snprintf(cmw_label, cmw_length,
889745916cd2Sjpk 				    "ADMIN_LOW [%s]", ascii);
889845916cd2Sjpk 				(void) append_secattr(secinfo, len,
889945916cd2Sjpk 				    strlen(cmw_label) + 1, cmw_label,
890045916cd2Sjpk 				    LBL_TYPE);
890145916cd2Sjpk 
890245916cd2Sjpk 				/* free storage */
890345916cd2Sjpk 				if (ascii != NULL) {
890445916cd2Sjpk 					free(cmw_label);
890545916cd2Sjpk 					free(ascii);
890645916cd2Sjpk 					ascii = (char *)0;
890745916cd2Sjpk 				}
890845916cd2Sjpk 			}
890945916cd2Sjpk 		}
891045916cd2Sjpk 		break;
891145916cd2Sjpk 
891245916cd2Sjpk 	default:
891345916cd2Sjpk 		break;
891445916cd2Sjpk 	} /* end switch for LBL_TYPE */
891545916cd2Sjpk 
891645916cd2Sjpk 
891745916cd2Sjpk 	/* DONE !! */
891845916cd2Sjpk 	return;
891945916cd2Sjpk 
892045916cd2Sjpk } /* end of append_ext_attr */
892145916cd2Sjpk 
892245916cd2Sjpk 
892345916cd2Sjpk /*
892445916cd2Sjpk  *	Name: extract_attr()
892545916cd2Sjpk  *
892645916cd2Sjpk  *	Description:
892745916cd2Sjpk  *		Process attributes from the ancillary file due to
892845916cd2Sjpk  *		the T option.
892945916cd2Sjpk  *
893045916cd2Sjpk  *	Call by doxtract() as part of the switch case structure.
893145916cd2Sjpk  *	Making this a separate routine because the nesting are too
893245916cd2Sjpk  *	deep in doxtract, thus, leaving very little space
893345916cd2Sjpk  *	on each line for instructions.
893445916cd2Sjpk  *
893545916cd2Sjpk  * With option 'T', we extract from a TS 8 or TS 2.5 ancillary file
893645916cd2Sjpk  *
893745916cd2Sjpk  * For option 'T', following are possible attributes in
893845916cd2Sjpk  * a TS 8 ancillary file: (NOTE: No IL support)
893945916cd2Sjpk  *
894045916cd2Sjpk  *	attribute type        attribute		process procedure
894145916cd2Sjpk  *	----------------      ----------------  -------------------------
894245916cd2Sjpk  *    #	LBL_TYPE       = 'L'   SL               construct binary label
894345916cd2Sjpk  *    #	APRIV_TYPE     = 'P'   allowed priv    	construct privileges
894445916cd2Sjpk  *    #	FPRIV_TYPE     = 'p'   forced priv	construct privileges
894545916cd2Sjpk  *    #	COMP_TYPE      = 'C'   path component	construct real path
894645916cd2Sjpk  *    #	DIR_TYPE       = 'D'   directory flag	note it is a directory
894745916cd2Sjpk  *    $	UFSD_ACL       = '1'   ACL data		construct ACL entries
894845916cd2Sjpk  *	ATTR_FLAG_TYPE = 'F'   file attr flags  construct binary flags
894945916cd2Sjpk  *	LK_COMP_TYPE   = 'K'   linked path comp construct linked real path
895045916cd2Sjpk  *
895145916cd2Sjpk  * note: # = attribute names common between TS 8 & TS 2.5 ancillary
895245916cd2Sjpk  *           files.
895345916cd2Sjpk  *       $ = ACL attribute is processed for the option 'p', it doesn't
895445916cd2Sjpk  *           need option 'T'.
895545916cd2Sjpk  *
895645916cd2Sjpk  * Trusted Extensions ignores APRIV_TYPE, FPRIV_TYPE, and ATTR_FLAG_TYPE
895745916cd2Sjpk  *
895845916cd2Sjpk  */
895945916cd2Sjpk static void
896045916cd2Sjpk extract_attr(char **file_ptr, struct sec_attr *attr)
896145916cd2Sjpk {
896245916cd2Sjpk 	int	reterr, err;
896345916cd2Sjpk 	char	*dummy_buf;	/* for attribute extract */
896445916cd2Sjpk 
896545916cd2Sjpk 	dummy_buf = attr->attr_info;
896645916cd2Sjpk 
896745916cd2Sjpk 	switch (attr->attr_type) {
896845916cd2Sjpk 
896945916cd2Sjpk 	case DIR_TYPE:
897045916cd2Sjpk 
897145916cd2Sjpk 		dir_flag++;
897245916cd2Sjpk 		break;
897345916cd2Sjpk 
897445916cd2Sjpk 	case LBL_TYPE:
897545916cd2Sjpk 
897645916cd2Sjpk 		/*
897745916cd2Sjpk 		 * LBL_TYPE is used to indicate SL for directory, and
897845916cd2Sjpk 		 * CMW label for other file types.
897945916cd2Sjpk 		 */
898045916cd2Sjpk 
898145916cd2Sjpk 		if (!dir_flag) { /* not directory */
898245916cd2Sjpk 			/* Skip over IL portion */
898345916cd2Sjpk 			char *sl_ptr = strchr(dummy_buf, '[');
898445916cd2Sjpk 
898545916cd2Sjpk 			if (sl_ptr == NULL)
898645916cd2Sjpk 				err = 0;
898745916cd2Sjpk 			else
898845916cd2Sjpk 				err = stobsl(sl_ptr, &bs_label,
898945916cd2Sjpk 				    NEW_LABEL, &reterr);
899045916cd2Sjpk 		} else { /* directory */
899145916cd2Sjpk 			err = stobsl(dummy_buf, &bs_label,
899245916cd2Sjpk 			    NEW_LABEL, &reterr);
899345916cd2Sjpk 		}
899445916cd2Sjpk 		if (err == 0) {
899545916cd2Sjpk 			(void) fprintf(stderr, gettext("tar: "
899645916cd2Sjpk 			    "can't convert %s to binary label\n"),
899745916cd2Sjpk 			    dummy_buf);
899845916cd2Sjpk 			bslundef(&bs_label);
899945916cd2Sjpk 		} else if (!blequal(&bs_label, &admin_low) &&
900045916cd2Sjpk 		    !blequal(&bs_label, &admin_high)) {
900145916cd2Sjpk 			bslabel_t *from_label;
900245916cd2Sjpk 			char *buf;
900345916cd2Sjpk 			char tempbuf[MAXPATHLEN];
900445916cd2Sjpk 
900545916cd2Sjpk 			if (*orig_namep != '/') {
900645916cd2Sjpk 				/* got relative linked to path */
900745916cd2Sjpk 				(void) getcwd(tempbuf, (sizeof (tempbuf)));
900845916cd2Sjpk 				(void) strncat(tempbuf, "/", MAXPATHLEN);
900945916cd2Sjpk 			} else
901045916cd2Sjpk 				*tempbuf = '\0';
901145916cd2Sjpk 
901245916cd2Sjpk 			buf = real_path;
901345916cd2Sjpk 			(void) strncat(tempbuf, orig_namep, MAXPATHLEN);
901445916cd2Sjpk 			from_label = getlabelbypath(tempbuf);
901545916cd2Sjpk 			if (from_label != NULL) {
901645916cd2Sjpk 				if (blequal(from_label, &admin_low)) {
901745916cd2Sjpk 					if ((getpathbylabel(tempbuf, buf,
901845916cd2Sjpk 					    MAXPATHLEN, &bs_label) == NULL)) {
901945916cd2Sjpk 						(void) fprintf(stderr,
902045916cd2Sjpk 						    gettext("tar: "
902145916cd2Sjpk 						"can't get zone root path for "
902245916cd2Sjpk 						"%s\n"), tempbuf);
902345916cd2Sjpk 					} else
902445916cd2Sjpk 						rpath_flag = 1;
902545916cd2Sjpk 				}
902645916cd2Sjpk 				free(from_label);
902745916cd2Sjpk 			}
902845916cd2Sjpk 		}
902945916cd2Sjpk 		break;
903045916cd2Sjpk 
903145916cd2Sjpk 	case COMP_TYPE:
903245916cd2Sjpk 
903345916cd2Sjpk 		rebuild_comp_path(dummy_buf, file_ptr);
903445916cd2Sjpk 		break;
903545916cd2Sjpk 
903645916cd2Sjpk 	case LK_COMP_TYPE:
903745916cd2Sjpk 
903845916cd2Sjpk 		if (rebuild_lk_comp_path(dummy_buf, file_ptr)
903945916cd2Sjpk 		    == 0) {
904045916cd2Sjpk 			lk_rpath_flag = 1;
904145916cd2Sjpk 		} else {
904245916cd2Sjpk 			(void) fprintf(stderr, gettext("tar: warning: link's "
904345916cd2Sjpk 			    "target pathname might be invalid.\n"));
904445916cd2Sjpk 			lk_rpath_flag = 0;
904545916cd2Sjpk 		}
904645916cd2Sjpk 		break;
904745916cd2Sjpk 	case APRIV_TYPE:
904845916cd2Sjpk 		ignored_aprivs++;
904945916cd2Sjpk 		break;
905045916cd2Sjpk 	case FPRIV_TYPE:
905145916cd2Sjpk 		ignored_fprivs++;
905245916cd2Sjpk 		break;
905345916cd2Sjpk 	case ATTR_FLAG_TYPE:
905445916cd2Sjpk 		ignored_fattrs++;
905545916cd2Sjpk 		break;
905645916cd2Sjpk 
905745916cd2Sjpk 	default:
905845916cd2Sjpk 
905945916cd2Sjpk 		break;
906045916cd2Sjpk 	}
906145916cd2Sjpk 
906245916cd2Sjpk 	/* done */
906345916cd2Sjpk 	return;
906445916cd2Sjpk 
906545916cd2Sjpk }	/* end extract_attr */
906645916cd2Sjpk 
906745916cd2Sjpk 
906845916cd2Sjpk 
906945916cd2Sjpk /*
907045916cd2Sjpk  *	Name:	rebuild_comp_path()
907145916cd2Sjpk  *
907245916cd2Sjpk  *	Description:
907345916cd2Sjpk  *		Take the string of components passed down by the calling
907445916cd2Sjpk  *		routine and parse the values and rebuild the path.
907545916cd2Sjpk  *		This routine no longer needs to produce a new real_path
907645916cd2Sjpk  *		string because it is produced when the 'L' LABEL_TYPE is
907745916cd2Sjpk  *		interpreted. So the only thing done here is to distinguish
907845916cd2Sjpk  *		between an SLD and an MLD entry. We only want one, so we
907945916cd2Sjpk  *		ignore the MLD entry by setting the mld_flag.
908045916cd2Sjpk  *
908145916cd2Sjpk  *	return value:
908245916cd2Sjpk  *		none
908345916cd2Sjpk  */
908445916cd2Sjpk static void
908545916cd2Sjpk rebuild_comp_path(char *str, char **namep)
908645916cd2Sjpk {
908745916cd2Sjpk 	char		*cp;
908845916cd2Sjpk 
908945916cd2Sjpk 	while (*str != '\0') {
909045916cd2Sjpk 
909145916cd2Sjpk 		switch (*str) {
909245916cd2Sjpk 
909345916cd2Sjpk 		case MLD_TYPE:
909445916cd2Sjpk 
909545916cd2Sjpk 			str++;
909645916cd2Sjpk 			if ((cp = strstr(str, ";;")) != NULL) {
909745916cd2Sjpk 				*cp = '\0';
909845916cd2Sjpk 				str = cp + 2;
909945916cd2Sjpk 				*cp = ';';
910045916cd2Sjpk 			}
910145916cd2Sjpk 			mld_flag = 1;
910245916cd2Sjpk 			break;
910345916cd2Sjpk 
910445916cd2Sjpk 		case SLD_TYPE:
910545916cd2Sjpk 
910645916cd2Sjpk 			str++;
910745916cd2Sjpk 			if ((cp = strstr(str, ";;")) != NULL) {
910845916cd2Sjpk 				*cp = '\0';
910945916cd2Sjpk 				str = cp + 2;
911045916cd2Sjpk 				*cp = ';';
911145916cd2Sjpk 			}
911245916cd2Sjpk 			mld_flag = 0;
911345916cd2Sjpk 			break;
911445916cd2Sjpk 
911545916cd2Sjpk 		case PATH_TYPE:
911645916cd2Sjpk 
911745916cd2Sjpk 			str++;
911845916cd2Sjpk 			if ((cp = strstr(str, ";;")) != NULL) {
911945916cd2Sjpk 				*cp = '\0';
912045916cd2Sjpk 				str = cp + 2;
912145916cd2Sjpk 				*cp = ';';
912245916cd2Sjpk 			}
912345916cd2Sjpk 			break;
912445916cd2Sjpk 		}
912545916cd2Sjpk 	}
912645916cd2Sjpk 	if (rpath_flag)
912745916cd2Sjpk 		*namep = real_path;
912845916cd2Sjpk 	return;
912945916cd2Sjpk 
913045916cd2Sjpk } /* end rebuild_comp_path() */
913145916cd2Sjpk 
913245916cd2Sjpk /*
913345916cd2Sjpk  *	Name:	rebuild_lk_comp_path()
913445916cd2Sjpk  *
913545916cd2Sjpk  *	Description:
913645916cd2Sjpk  *		Take the string of components passed down by the calling
913745916cd2Sjpk  *		routine and parse the values and rebuild the path.
913845916cd2Sjpk  *
913945916cd2Sjpk  *	return value:
914045916cd2Sjpk  *		0 = succeeded
914145916cd2Sjpk  *		-1 = failed
914245916cd2Sjpk  */
914345916cd2Sjpk static int
914445916cd2Sjpk rebuild_lk_comp_path(char *str, char **namep)
914545916cd2Sjpk {
914645916cd2Sjpk 	char		*cp;
914745916cd2Sjpk 	int		reterr;
914845916cd2Sjpk 	bslabel_t	bslabel;
914945916cd2Sjpk 	char		*buf;
915045916cd2Sjpk 	char		pbuf[MAXPATHLEN];
915145916cd2Sjpk 	char		*ptr1, *ptr2;
915245916cd2Sjpk 	int		plen;
915345916cd2Sjpk 	int		use_pbuf;
915445916cd2Sjpk 	char		tempbuf[MAXPATHLEN];
915545916cd2Sjpk 	int		mismatch;
915645916cd2Sjpk 	bslabel_t	*from_label;
915745916cd2Sjpk 	char		zonename[ZONENAME_MAX];
915845916cd2Sjpk 	zoneid_t	zoneid;
915945916cd2Sjpk 
916045916cd2Sjpk 	/* init stuff */
916145916cd2Sjpk 	use_pbuf = 0;
916245916cd2Sjpk 	mismatch = 0;
916345916cd2Sjpk 
916445916cd2Sjpk 	/*
916545916cd2Sjpk 	 * For linked to pathname (LK_COMP_TYPE):
916645916cd2Sjpk 	 *  - If the linked to pathname is absolute (start with /), we
916745916cd2Sjpk 	 *    will use it as is.
916845916cd2Sjpk 	 *  - If it is a relative pathname then it is relative to 1 of 2
916945916cd2Sjpk 	 *    directories.  For a hardlink, it is relative to the current
917045916cd2Sjpk 	 *    directory.  For a symbolic link, it is relative to the
917145916cd2Sjpk 	 *    directory the symbolic link is in.  For the symbolic link
917245916cd2Sjpk 	 *    case, set a flag to indicate we need to use the prefix of
917345916cd2Sjpk 	 *    the restored file's pathname with the linked to pathname.
917445916cd2Sjpk 	 *
917545916cd2Sjpk 	 *    NOTE: At this point, we have no way to determine if we have
917645916cd2Sjpk 	 *    a hardlink or a symbolic link.  We will compare the 1st
917745916cd2Sjpk 	 *    component in the prefix portion of the restore file's
917845916cd2Sjpk 	 *    pathname to the 1st component in the attribute data
917945916cd2Sjpk 	 *    (the linked pathname).  If they are the same, we will assume
918045916cd2Sjpk 	 *    the link pathname to reconstruct is relative to the current
918145916cd2Sjpk 	 *    directory.  Otherwise, we will set a flag indicate we need
918245916cd2Sjpk 	 *    to use a prefix with the reconstructed name.  Need to compare
918345916cd2Sjpk 	 *    both the adorned and unadorned version before deciding a
918445916cd2Sjpk 	 *    mismatch.
918545916cd2Sjpk 	 */
918645916cd2Sjpk 
918745916cd2Sjpk 	buf = lk_real_path;
918845916cd2Sjpk 	if (*(str + 1) != '/') { /* got relative linked to path */
918945916cd2Sjpk 		ptr1 = orig_namep;
919045916cd2Sjpk 		ptr2 = strrchr(ptr1, '/');
919145916cd2Sjpk 		plen = ptr2 - ptr1;
919245916cd2Sjpk 		if (plen > 0) {
919345916cd2Sjpk 			pbuf[0] = '\0';
919445916cd2Sjpk 			plen++;		/* include '/' */
919545916cd2Sjpk 			(void) strncpy(pbuf, ptr1, plen);
919645916cd2Sjpk 			*(pbuf + plen) = '\0';
919745916cd2Sjpk 			ptr2 = strchr(pbuf, '/');
919845916cd2Sjpk 			if (strncmp(pbuf, str + 1, ptr2 - pbuf) != 0)
919945916cd2Sjpk 				mismatch = 1;
920045916cd2Sjpk 		}
920145916cd2Sjpk 
920245916cd2Sjpk 		if (mismatch == 1)
920345916cd2Sjpk 			use_pbuf = 1;
920445916cd2Sjpk 	}
920545916cd2Sjpk 
920645916cd2Sjpk 	buf[0] = '\0';
920745916cd2Sjpk 
920845916cd2Sjpk 	while (*str != '\0') {
920945916cd2Sjpk 
921045916cd2Sjpk 		switch (*str) {
921145916cd2Sjpk 
921245916cd2Sjpk 		case MLD_TYPE:
921345916cd2Sjpk 
921445916cd2Sjpk 			str++;
921545916cd2Sjpk 			if ((cp = strstr(str, ";;")) != NULL) {
921645916cd2Sjpk 				*cp = '\0';
921745916cd2Sjpk 
921845916cd2Sjpk 				/*
921945916cd2Sjpk 				 * Ignore attempts to backup over .MLD.
922045916cd2Sjpk 				 */
922145916cd2Sjpk 				if (strcmp(str, "../") != 0)
922245916cd2Sjpk 					(void) strncat(buf, str, MAXPATHLEN);
922345916cd2Sjpk 				str = cp + 2;
922445916cd2Sjpk 				*cp = ';';
922545916cd2Sjpk 			}
922645916cd2Sjpk 			break;
922745916cd2Sjpk 
922845916cd2Sjpk 		case SLD_TYPE:
922945916cd2Sjpk 
923045916cd2Sjpk 			str++;
923145916cd2Sjpk 			if ((cp = strstr(str, ";;")) != NULL) {
923245916cd2Sjpk 				*cp = '\0';
923345916cd2Sjpk 
923445916cd2Sjpk 				/*
923545916cd2Sjpk 				 * Use the path name in the header if
923645916cd2Sjpk 				 * error occurs when processing the
923745916cd2Sjpk 				 * SLD type.
923845916cd2Sjpk 				 */
923945916cd2Sjpk 
924045916cd2Sjpk 				if (!stobsl(str, &bslabel,
924145916cd2Sjpk 				    NO_CORRECTION, &reterr)) {
924245916cd2Sjpk 					(void) fprintf(stderr, gettext(
924345916cd2Sjpk 					    "tar: can't translate to binary"
924445916cd2Sjpk 					    "SL for SLD, stobsl() error:"
924545916cd2Sjpk 					    " %s\n"), strerror(errno));
924645916cd2Sjpk 					return (-1);
924745916cd2Sjpk 				}
924845916cd2Sjpk 
924945916cd2Sjpk 				str = cp + 2;
925045916cd2Sjpk 				*cp = ';';
925145916cd2Sjpk 
925245916cd2Sjpk 				if (use_pbuf == 1) {
925345916cd2Sjpk 					if (*pbuf != '/') {
925445916cd2Sjpk 						/* relative linked to path */
925545916cd2Sjpk 
925645916cd2Sjpk 						(void) getcwd(tempbuf,
925745916cd2Sjpk 						    (sizeof (tempbuf)));
925845916cd2Sjpk 						(void) strncat(tempbuf, "/",
925945916cd2Sjpk 						    MAXPATHLEN);
926045916cd2Sjpk 						(void) strncat(tempbuf, pbuf,
926145916cd2Sjpk 						    MAXPATHLEN);
926245916cd2Sjpk 					}
926345916cd2Sjpk 					else
926445916cd2Sjpk 						(void) strcpy(tempbuf, pbuf);
926545916cd2Sjpk 
926645916cd2Sjpk 				} else if (*buf != '/') {
926745916cd2Sjpk 					/* got relative linked to path */
926845916cd2Sjpk 
926945916cd2Sjpk 					(void) getcwd(tempbuf,
927045916cd2Sjpk 					    (sizeof (tempbuf)));
927145916cd2Sjpk 					(void) strncat(tempbuf, "/",
927245916cd2Sjpk 					    MAXPATHLEN);
927345916cd2Sjpk 				} else
927445916cd2Sjpk 					*tempbuf = '\0';
927545916cd2Sjpk 
927645916cd2Sjpk 				(void) strncat(tempbuf, buf, MAXPATHLEN);
927745916cd2Sjpk 				*buf = '\0';
927845916cd2Sjpk 
927945916cd2Sjpk 				if (blequal(&bslabel, &admin_high)) {
928045916cd2Sjpk 					bslabel = admin_low;
928145916cd2Sjpk 				}
928245916cd2Sjpk 
928345916cd2Sjpk 
928445916cd2Sjpk 				/*
928545916cd2Sjpk 				 * Check for cross-zone symbolic links
928645916cd2Sjpk 				 */
928745916cd2Sjpk 				from_label = getlabelbypath(real_path);
928845916cd2Sjpk 				if (rpath_flag && (from_label != NULL) &&
928945916cd2Sjpk 				    !blequal(&bslabel, from_label)) {
929045916cd2Sjpk 					if ((zoneid =
929145916cd2Sjpk 					    getzoneidbylabel(&bslabel)) == -1) {
929245916cd2Sjpk 						(void) fprintf(stderr,
929345916cd2Sjpk 						    gettext("tar: can't get "
929445916cd2Sjpk 						    "zone ID for %s\n"),
929545916cd2Sjpk 						    tempbuf);
929645916cd2Sjpk 						return (-1);
929745916cd2Sjpk 					}
929845916cd2Sjpk 					if (zone_getattr(zoneid, ZONE_ATTR_NAME,
929945916cd2Sjpk 					    &zonename, ZONENAME_MAX) == -1) {
930045916cd2Sjpk 						/* Badly configured zone info */
930145916cd2Sjpk 						(void) fprintf(stderr,
930245916cd2Sjpk 						    gettext("tar: can't get "
930345916cd2Sjpk 						    "zonename for %s\n"),
930445916cd2Sjpk 						    tempbuf);
930545916cd2Sjpk 						return (-1);
930645916cd2Sjpk 					}
930745916cd2Sjpk 					(void) strncpy(buf, AUTO_ZONE,
930845916cd2Sjpk 					    MAXPATHLEN);
930945916cd2Sjpk 					(void) strncat(buf, "/",
931045916cd2Sjpk 					    MAXPATHLEN);
931145916cd2Sjpk 					(void) strncat(buf, zonename,
931245916cd2Sjpk 					    MAXPATHLEN);
931345916cd2Sjpk 				}
931445916cd2Sjpk 				if (from_label != NULL)
931545916cd2Sjpk 					free(from_label);
931645916cd2Sjpk 				(void) strncat(buf, tempbuf, MAXPATHLEN);
931745916cd2Sjpk 				break;
931845916cd2Sjpk 			}
931945916cd2Sjpk 			mld_flag = 0;
932045916cd2Sjpk 			break;
932145916cd2Sjpk 
932245916cd2Sjpk 		case PATH_TYPE:
932345916cd2Sjpk 
932445916cd2Sjpk 			str++;
932545916cd2Sjpk 			if ((cp = strstr(str, ";;")) != NULL) {
932645916cd2Sjpk 				*cp = '\0';
932745916cd2Sjpk 				(void) strncat(buf, str, MAXPATHLEN);
932845916cd2Sjpk 				str = cp + 2;
932945916cd2Sjpk 				*cp = ';';
933045916cd2Sjpk 			}
933145916cd2Sjpk 			break;
933245916cd2Sjpk 
933345916cd2Sjpk 		default:
933445916cd2Sjpk 
933545916cd2Sjpk 			(void) fprintf(stderr, gettext(
933645916cd2Sjpk 			    "tar: error rebuilding path %s\n"),
933745916cd2Sjpk 			    *namep);
933845916cd2Sjpk 			*buf = '\0';
933945916cd2Sjpk 			str++;
934045916cd2Sjpk 			return (-1);
934145916cd2Sjpk 		}
934245916cd2Sjpk 	}
934345916cd2Sjpk 
934445916cd2Sjpk 	/*
934545916cd2Sjpk 	 * Done for LK_COMP_TYPE
934645916cd2Sjpk 	 */
934745916cd2Sjpk 
934845916cd2Sjpk 	return (0);    /* component path is rebuilt successfully */
934945916cd2Sjpk 
935045916cd2Sjpk } /* end rebuild_lk_comp_path() */
935145916cd2Sjpk 
935245916cd2Sjpk /*
935345916cd2Sjpk  *	Name: check_ext_attr()
935445916cd2Sjpk  *
935545916cd2Sjpk  *	Description:
935645916cd2Sjpk  *		Check the extended attributes for a file being extracted.
935745916cd2Sjpk  *		The attributes being checked here are CMW labels.
935845916cd2Sjpk  *		ACLs are not set here because they are set by the
935945916cd2Sjpk  *		pflag in doxtract().
936045916cd2Sjpk  *
936145916cd2Sjpk  *		If the label doesn't match, return 0
936245916cd2Sjpk  *		else return 1
936345916cd2Sjpk  */
936445916cd2Sjpk static int
936545916cd2Sjpk check_ext_attr(char *filename)
936645916cd2Sjpk {
936745916cd2Sjpk 	bslabel_t	currentlabel;	/* label from zone */
936845916cd2Sjpk 
936945916cd2Sjpk 	if (bltype(&bs_label, SUN_SL_UN)) {
937045916cd2Sjpk 		/* No label check possible */
937145916cd2Sjpk 		return (0);
937245916cd2Sjpk 	}
937345916cd2Sjpk 	if (getlabel(filename, &currentlabel) != 0) {
937445916cd2Sjpk 		(void) fprintf(stderr,
937545916cd2Sjpk 		    gettext("tar: can't get label for "
937645916cd2Sjpk 		    " %s, getlabel() error: %s\n"),
937745916cd2Sjpk 		    filename, strerror(errno));
937845916cd2Sjpk 		return (0);
937945916cd2Sjpk 	} else if ((blequal(&currentlabel, &bs_label)) == 0) {
938045916cd2Sjpk 		char	*src_label = NULL;	/* ascii label */
938145916cd2Sjpk 
938245916cd2Sjpk 		/* get current src SL */
938345916cd2Sjpk 		if (bsltos(&bs_label, &src_label, 0, 0) <= 0) {
938445916cd2Sjpk 			(void) fprintf(stderr,
938545916cd2Sjpk 			    gettext("tar: can't interpret requested label for"
938645916cd2Sjpk 			    " %s\n"), filename);
938745916cd2Sjpk 		} else {
938845916cd2Sjpk 			(void) fprintf(stderr,
938945916cd2Sjpk 			    gettext("tar: can't apply label %s to %s\n"),
939045916cd2Sjpk 			    src_label, filename);
939145916cd2Sjpk 			free(src_label);
939245916cd2Sjpk 		}
939345916cd2Sjpk 		(void) fprintf(stderr,
939445916cd2Sjpk 		    gettext("tar: %s not restored\n"), filename);
939545916cd2Sjpk 		return (0);
939645916cd2Sjpk 	}
939745916cd2Sjpk 	return (1);
939845916cd2Sjpk 
939945916cd2Sjpk }	/* end check_ext_attr */
940036802407SRich Burridge 
940136802407SRich Burridge /* Compressing a tar file using compression method provided in 'opt' */
940236802407SRich Burridge 
940336802407SRich Burridge static void
940436802407SRich Burridge compress_back()
940536802407SRich Burridge {
940636802407SRich Burridge 	pid_t	pid;
940736802407SRich Burridge 	int status;
940836802407SRich Burridge 	int wret;
940936802407SRich Burridge 	struct	stat statb;
941036802407SRich Burridge 
941136802407SRich Burridge 	if (vflag) {
941236802407SRich Burridge 		(void) fprintf(vfile,
941336802407SRich Burridge 		    gettext("Compressing '%s' with '%s'...\n"),
941436802407SRich Burridge 		    usefile, compress_opt);
941536802407SRich Burridge 	}
941636802407SRich Burridge 	if ((pid = fork()) == 0) {
941736802407SRich Burridge 		(void) execlp(compress_opt, compress_opt,
941836802407SRich Burridge 		    usefile, NULL);
941936802407SRich Burridge 	} else if (pid == -1) {
942036802407SRich Burridge 		vperror(1, "%s", gettext("Could not fork"));
942136802407SRich Burridge 	}
942236802407SRich Burridge 	wait_pid(pid);
942336802407SRich Burridge 	if (suffix == 0) {
942436802407SRich Burridge 		(void) rename(tfname, usefile);
942536802407SRich Burridge 	}
942636802407SRich Burridge }
942736802407SRich Burridge 
942836802407SRich Burridge /* The magic numbers from /etc/magic */
942936802407SRich Burridge 
943036802407SRich Burridge #define	GZIP_MAGIC	"\037\213"
943136802407SRich Burridge #define	BZIP_MAGIC	"BZh"
943236802407SRich Burridge #define	COMP_MAGIC	"\037\235"
943336802407SRich Burridge 
943436802407SRich Burridge void
943536802407SRich Burridge check_compression()
943636802407SRich Burridge {
943736802407SRich Burridge 	char 	magic[2];
943836802407SRich Burridge 	char	buf[16];
943936802407SRich Burridge 	FILE	*fp;
944036802407SRich Burridge 
944136802407SRich Burridge 	if ((fp = fopen(usefile, "r")) != NULL) {
944236802407SRich Burridge 		(void) fread(buf, sizeof (char), 6, fp);
944336802407SRich Burridge 		magic[0] = buf[0];
944436802407SRich Burridge 		magic[1] = buf[1];
944536802407SRich Burridge 		(void) fclose(fp);
944636802407SRich Burridge 	}
944736802407SRich Burridge 
944836802407SRich Burridge 	if (memcmp(magic, GZIP_MAGIC, 2) == 0) {
944936802407SRich Burridge 		if (xflag || tflag) {
945036802407SRich Burridge 			compress_opt = compress_malloc(strlen(GZCAT) + 1);
945136802407SRich Burridge 			(void) strcpy(compress_opt, GZCAT);
945236802407SRich Burridge 		} else if (uflag || rflag) {
945336802407SRich Burridge 			compress_opt = compress_malloc(strlen(GZIP) + 1);
945436802407SRich Burridge 			(void) strcpy(compress_opt, GZIP);
945536802407SRich Burridge 		}
945636802407SRich Burridge 	} else if (memcmp(magic, BZIP_MAGIC, 2) == 0) {
945736802407SRich Burridge 		if (xflag || tflag) {
945836802407SRich Burridge 			compress_opt = compress_malloc(strlen(BZCAT) + 1);
945936802407SRich Burridge 			(void) strcpy(compress_opt, BZCAT);
946036802407SRich Burridge 		} else if (uflag || rflag) {
946136802407SRich Burridge 			compress_opt = compress_malloc(strlen(BZIP) + 1);
946236802407SRich Burridge 			(void) strcpy(compress_opt, BZIP);
946336802407SRich Burridge 		}
946436802407SRich Burridge 	} else if (memcmp(magic, COMP_MAGIC, 2) == 0) {
946536802407SRich Burridge 		if (xflag || tflag) {
946636802407SRich Burridge 			compress_opt = compress_malloc(strlen(ZCAT) + 1);
946736802407SRich Burridge 			(void) strcpy(compress_opt, ZCAT);
946836802407SRich Burridge 		} else if (uflag || rflag) {
946936802407SRich Burridge 			compress_opt = compress_malloc(strlen(COMPRESS) + 1);
947036802407SRich Burridge 			(void) strcpy(compress_opt, COMPRESS);
947136802407SRich Burridge 		}
947236802407SRich Burridge 	}
947336802407SRich Burridge }
947436802407SRich Burridge 
947536802407SRich Burridge char *
947636802407SRich Burridge add_suffix()
947736802407SRich Burridge {
947836802407SRich Burridge 	(void) strcpy(tfname, usefile);
947936802407SRich Burridge 	if (strcmp(compress_opt, GZIP) == 0) {
948036802407SRich Burridge 		if ((suffix = gz_suffix()) == NULL) {
948136802407SRich Burridge 			strlcat(tfname, gsuffix[0], sizeof (tfname));
948236802407SRich Burridge 			return (gsuffix[0]);
948336802407SRich Burridge 		}
948436802407SRich Burridge 	} else if (strcmp(compress_opt, COMPRESS) == 0) {
948536802407SRich Burridge 		if ((suffix = gz_suffix()) == NULL) {
948636802407SRich Burridge 			strlcat(tfname, gsuffix[6], sizeof (tfname));
948736802407SRich Burridge 			return (gsuffix[6]);
948836802407SRich Burridge 		}
948936802407SRich Burridge 	} else if (strcmp(compress_opt, BZIP) == 0) {
949036802407SRich Burridge 		if ((suffix = bz_suffix()) == NULL) {
949136802407SRich Burridge 			strlcat(tfname, bsuffix[0], sizeof (tfname));
949236802407SRich Burridge 			return (bsuffix[0]);
949336802407SRich Burridge 		}
949436802407SRich Burridge 	}
949536802407SRich Burridge 	return (NULL);
949636802407SRich Burridge }
949736802407SRich Burridge 
949836802407SRich Burridge /* Decompressing a tar file using compression method from the file type */
949936802407SRich Burridge void
950036802407SRich Burridge decompress_file(void)
950136802407SRich Burridge {
950236802407SRich Burridge 	pid_t 	pid;
950336802407SRich Burridge 	int	status;
950436802407SRich Burridge 	char	cmdstr[PATH_MAX];
950536802407SRich Burridge 	char	fname[PATH_MAX];
950636802407SRich Burridge 	char	*added_suffix;
950736802407SRich Burridge 
950836802407SRich Burridge 
950936802407SRich Burridge 	added_suffix = add_suffix();
951036802407SRich Burridge 	if (added_suffix != NULL)  {
951136802407SRich Burridge 		(void) rename(usefile, tfname);
951236802407SRich Burridge 	}
951336802407SRich Burridge 	if ((pid = fork()) == 0) {
951436802407SRich Burridge 		if (vflag) {
951536802407SRich Burridge 			(void) fprintf(vfile,
951636802407SRich Burridge 			    gettext("Decompressing '%s' with "
951736802407SRich Burridge 			    "'%s'...\n"), usefile, compress_opt);
951836802407SRich Burridge 		}
951936802407SRich Burridge 		(void) execlp(compress_opt, compress_opt, "-df",
952036802407SRich Burridge 		    tfname, NULL);
952153329f75SRich Burridge 		vperror(1, gettext("Could not exec %s"), compress_opt);
952236802407SRich Burridge 	} else if (pid == -1) {
95238e4a71aeSRich Burridge 		vperror(1, gettext("Could not fork"));
952436802407SRich Burridge 	}
952536802407SRich Burridge 	wait_pid(pid);
952636802407SRich Burridge 	if (suffix != NULL) {
952736802407SRich Burridge 		/* restore the file name - original file was without suffix */
952836802407SRich Burridge 		*(usefile + strlen(usefile) - strlen(suffix)) = '\0';
952936802407SRich Burridge 	}
953036802407SRich Burridge }
953136802407SRich Burridge 
953236802407SRich Burridge /* Set the archive for writing and then compress the archive */
953336802407SRich Burridge pid_t
953436802407SRich Burridge compress_file(void)
953536802407SRich Burridge {
953636802407SRich Burridge 	int fd[2];
953736802407SRich Burridge 	pid_t pid;
953836802407SRich Burridge 
953936802407SRich Burridge 	if (vflag) {
954036802407SRich Burridge 		(void) fprintf(vfile, gettext("Compressing '%s' with "
954136802407SRich Burridge 		    "'%s'...\n"), usefile, compress_opt);
954236802407SRich Burridge 	}
954336802407SRich Burridge 
954436802407SRich Burridge 	if (pipe(fd) < 0) {
954536802407SRich Burridge 		vperror(1, gettext("Could not create pipe"));
954636802407SRich Burridge 	}
954736802407SRich Burridge 	if (pid = fork() > 0) {
954836802407SRich Burridge 		mt = fd[1];
954936802407SRich Burridge 		(void) close(fd[0]);
955036802407SRich Burridge 		return (pid);
955136802407SRich Burridge 	}
955236802407SRich Burridge 	/* child */
955336802407SRich Burridge 	(void) dup2(fd[0], STDIN_FILENO);
955436802407SRich Burridge 	(void) close(fd[1]);
955536802407SRich Burridge 	(void) dup2(mt, STDOUT_FILENO);
955636802407SRich Burridge 	(void) execlp(compress_opt, compress_opt, NULL);
955753329f75SRich Burridge 	vperror(1, gettext("Could not exec %s"), compress_opt);
955836802407SRich Burridge 	return (0);	/*NOTREACHED*/
955936802407SRich Burridge }
956036802407SRich Burridge 
956136802407SRich Burridge pid_t
956236802407SRich Burridge uncompress_file(void)
956336802407SRich Burridge {
956436802407SRich Burridge 	int fd[2];
956536802407SRich Burridge 	pid_t pid;
956636802407SRich Burridge 
956736802407SRich Burridge 	if (vflag) {
956836802407SRich Burridge 		(void) fprintf(vfile, gettext("Decompressing '%s' with "
956936802407SRich Burridge 		    "'%s'...\n"), usefile, compress_opt);
957036802407SRich Burridge 	}
957136802407SRich Burridge 
957236802407SRich Burridge 	if (pipe(fd) < 0) {
957336802407SRich Burridge 		vperror(1, gettext("Could not create pipe"));
957436802407SRich Burridge 	}
957536802407SRich Burridge 	if (pid = fork() > 0) {
957636802407SRich Burridge 		mt = fd[0];
957736802407SRich Burridge 		(void) close(fd[1]);
957836802407SRich Burridge 		return (pid);
957936802407SRich Burridge 	}
958036802407SRich Burridge 	/* child */
958136802407SRich Burridge 	(void) dup2(fd[1], STDOUT_FILENO);
958236802407SRich Burridge 	(void) close(fd[0]);
958336802407SRich Burridge 	(void) dup2(mt, STDIN_FILENO);
958436802407SRich Burridge 	(void) execlp(compress_opt, compress_opt, NULL);
958553329f75SRich Burridge 	vperror(1, gettext("Could not exec %s"), compress_opt);
958636802407SRich Burridge 	return (0);	/*NOTREACHED*/
958736802407SRich Burridge }
958836802407SRich Burridge 
958936802407SRich Burridge /* Checking valid 'bzip2' suffix */
959036802407SRich Burridge char *
959136802407SRich Burridge bz_suffix()
959236802407SRich Burridge {
959336802407SRich Burridge 	int 	i;
959436802407SRich Burridge 	int	slen;
959536802407SRich Burridge 	int	nlen = strlen(usefile);
959636802407SRich Burridge 
959736802407SRich Burridge 	for (i = 0; i < BS; i++) {
959836802407SRich Burridge 		slen = strlen(bsuffix[i]);
959936802407SRich Burridge 		if (nlen < slen)
960036802407SRich Burridge 			return (NULL);
960136802407SRich Burridge 		if (strcmp(usefile + nlen - slen, bsuffix[i]) == 0)
960236802407SRich Burridge 			return (bsuffix[i]);
960336802407SRich Burridge 	}
960436802407SRich Burridge 	return (NULL);
960536802407SRich Burridge }
960636802407SRich Burridge 
960736802407SRich Burridge /* Checking valid 'gzip' suffix */
960836802407SRich Burridge char *
960936802407SRich Burridge gz_suffix()
961036802407SRich Burridge {
961136802407SRich Burridge 	int 	i;
961236802407SRich Burridge 	int	slen;
961336802407SRich Burridge 	int	nlen = strlen(usefile);
961436802407SRich Burridge 
961536802407SRich Burridge 	for (i = 0; i < GS; i++) {
961636802407SRich Burridge 		slen = strlen(gsuffix[i]);
961736802407SRich Burridge 		if (nlen < slen)
961836802407SRich Burridge 			return (NULL);
961936802407SRich Burridge 		if (strcmp(usefile + nlen - slen, gsuffix[i]) == 0)
962036802407SRich Burridge 			return (gsuffix[i]);
962136802407SRich Burridge 	}
962236802407SRich Burridge 	return (NULL);
962336802407SRich Burridge }
962436802407SRich Burridge 
962536802407SRich Burridge void *
962636802407SRich Burridge compress_malloc(size_t size)
962736802407SRich Burridge {
962836802407SRich Burridge 	void *opt;
962936802407SRich Burridge 
963036802407SRich Burridge 	if ((opt = malloc(size)) == NULL) {
963136802407SRich Burridge 		vperror(1, "%s",
963236802407SRich Burridge 		    gettext("Could not allocate compress buffer\n"));
963336802407SRich Burridge 	}
963436802407SRich Burridge 	return (opt);
963536802407SRich Burridge }
963636802407SRich Burridge 
963736802407SRich Burridge void
963836802407SRich Burridge wait_pid(pid_t pid)
963936802407SRich Burridge {
964036802407SRich Burridge 	int status;
964136802407SRich Burridge 
964236802407SRich Burridge 	while (waitpid(pid, &status, 0) == -1 && errno == EINTR)
964336802407SRich Burridge 		;
964436802407SRich Burridge }
9645