xref: /onnv-gate/usr/src/cmd/cpio/cpio.c (revision 36:0bca19d5fad9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*36Schin  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	All Rights Reserved					*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
320Sstevel@tonic-gate  * under license from the Regents of the University of California.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <stdio.h>
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <errno.h>
400Sstevel@tonic-gate #include <unistd.h>
410Sstevel@tonic-gate #include <stdlib.h>
420Sstevel@tonic-gate #include <fcntl.h>
430Sstevel@tonic-gate #include <memory.h>
440Sstevel@tonic-gate #include <string.h>
450Sstevel@tonic-gate #include <stdarg.h>
460Sstevel@tonic-gate #include <sys/stat.h>
470Sstevel@tonic-gate #include <sys/statvfs.h>
480Sstevel@tonic-gate #include <sys/mkdev.h>
490Sstevel@tonic-gate #include <sys/param.h>
500Sstevel@tonic-gate #include <utime.h>
510Sstevel@tonic-gate #include <pwd.h>
520Sstevel@tonic-gate #include <grp.h>
530Sstevel@tonic-gate #include <signal.h>
540Sstevel@tonic-gate #include <ctype.h>
550Sstevel@tonic-gate #include <archives.h>
560Sstevel@tonic-gate #include <locale.h>
570Sstevel@tonic-gate #include <sys/ioctl.h>
580Sstevel@tonic-gate #include <sys/mtio.h>
590Sstevel@tonic-gate #include <sys/fdio.h>
600Sstevel@tonic-gate #include "cpio.h"
610Sstevel@tonic-gate #include <sys/acl.h>
620Sstevel@tonic-gate #include <sys/time.h>
630Sstevel@tonic-gate #include <sys/resource.h>
640Sstevel@tonic-gate #include <fnmatch.h>
650Sstevel@tonic-gate #include <libgen.h>
660Sstevel@tonic-gate #include <libintl.h>
670Sstevel@tonic-gate #include <dirent.h>
680Sstevel@tonic-gate #include <limits.h>
690Sstevel@tonic-gate 
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate  * Special kludge for off_t being a signed quantity.
720Sstevel@tonic-gate  */
730Sstevel@tonic-gate #if _FILE_OFFSET_BITS == 64
740Sstevel@tonic-gate typedef	u_longlong_t	u_off_t;
750Sstevel@tonic-gate #else
760Sstevel@tonic-gate typedef	ulong_t		u_off_t;
770Sstevel@tonic-gate #endif
780Sstevel@tonic-gate 
790Sstevel@tonic-gate #define	SECMODE	0xe080
800Sstevel@tonic-gate 
810Sstevel@tonic-gate #define	DEVNULL		"/dev/null"
820Sstevel@tonic-gate #define	XATTRHDR	".hdr"
830Sstevel@tonic-gate 
840Sstevel@tonic-gate #define	NAMELEN		32
850Sstevel@tonic-gate #define	TYPELEN 	16
860Sstevel@tonic-gate #define	PERMLEN		4
870Sstevel@tonic-gate 
880Sstevel@tonic-gate #define	FILE_COPIED	1
890Sstevel@tonic-gate #define	FILE_LINKED	2
900Sstevel@tonic-gate #define	FILE_PASS_ERR	-1
910Sstevel@tonic-gate 
920Sstevel@tonic-gate #define	ARCHIVE_NORMAL	0
930Sstevel@tonic-gate #define	ARCHIVE_ACL	1
940Sstevel@tonic-gate #define	ARCHIVE_XATTR	2
950Sstevel@tonic-gate 
960Sstevel@tonic-gate #define	LSTAT(dir, path, statbuf) fstatat(dir, \
970Sstevel@tonic-gate     get_component((Gen.g_attrnam_p == (char *)NULL) ? \
980Sstevel@tonic-gate     path : Gen.g_attrnam_p), statbuf, AT_SYMLINK_NOFOLLOW)
990Sstevel@tonic-gate #define	STAT(dir, path, statbuf) fstatat(dir, \
1000Sstevel@tonic-gate     get_component((Gen.g_attrnam_p == (char *)NULL) ? \
1010Sstevel@tonic-gate     path : Gen.g_attrnam_p), statbuf, 0)
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate  *	These limits reflect the maximum size regular file that
1050Sstevel@tonic-gate  *	can be archived, depending on the archive type. For archives
1060Sstevel@tonic-gate  *	with character-format headers (odc, tar, ustar) we use
107*36Schin  *	CHAR_OFFSET_MAX.  For archives with SVR4 ASCII headers (-c, -H crc)
108*36Schin  *	we store filesize in an 8-char hexadecimal string and use
109*36Schin  *	ASC_OFFSET_MAX.  Otherwise, we are limited to the size that will
110*36Schin  *	fit in a signed long value.
1110Sstevel@tonic-gate  */
112*36Schin #define	CHAR_OFFSET_MAX	077777777777ULL	/* 11 octal digits */
113*36Schin #define	ASC_OFFSET_MAX	0XFFFFFFFF	/* 8 hexadecimal digits */
1140Sstevel@tonic-gate #define	BIN_OFFSET_MAX	LONG_MAX	/* signed long max value */
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate static char	aclchar = ' ';
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate static struct Lnk *add_lnk(struct Lnk **);
1190Sstevel@tonic-gate static int bfill(void);
1200Sstevel@tonic-gate static void bflush(void);
1210Sstevel@tonic-gate static int chgreel(int dir);
1220Sstevel@tonic-gate static int ckname(int);
1230Sstevel@tonic-gate static void ckopts(long mask);
1240Sstevel@tonic-gate static long cksum(char hdr, int byt_cnt, int *err);
1250Sstevel@tonic-gate static int creat_hdr(void);
1260Sstevel@tonic-gate static int creat_lnk(int dirfd, char *name1_p, char *name2_p);
1270Sstevel@tonic-gate static int creat_spec(int dirfd);
1280Sstevel@tonic-gate static int creat_tmp(char *nam_p);
1290Sstevel@tonic-gate static void data_in(int proc_mode);
1300Sstevel@tonic-gate static void data_out(void);
1310Sstevel@tonic-gate static void data_pass(void);
1320Sstevel@tonic-gate static void file_in(void);
1330Sstevel@tonic-gate static int file_out(void);
1340Sstevel@tonic-gate static int file_pass(void);
1350Sstevel@tonic-gate static void flush_lnks(void);
1360Sstevel@tonic-gate static int gethdr(void);
1370Sstevel@tonic-gate static int getname(void);
1380Sstevel@tonic-gate static void getpats(int largc, char **largv);
1390Sstevel@tonic-gate static void ioerror(int dir);
1400Sstevel@tonic-gate static int matched(void);
1410Sstevel@tonic-gate static int missdir(char *nam_p);
1420Sstevel@tonic-gate static long mklong(short v[]);
1430Sstevel@tonic-gate static void mkshort(short sval[], long v);
1440Sstevel@tonic-gate static void msg(int severity, const char *fmt, ...);
1450Sstevel@tonic-gate static int openout(int dirfd);
1460Sstevel@tonic-gate static int read_hdr(int hdr);
1470Sstevel@tonic-gate static void reclaim(struct Lnk *l_p);
1480Sstevel@tonic-gate static void rstbuf(void);
1490Sstevel@tonic-gate static void setpasswd(char *nam);
1500Sstevel@tonic-gate static void rstfiles(int over, int dirfd);
1510Sstevel@tonic-gate static void scan4trail(void);
1520Sstevel@tonic-gate static void setup(int largc, char **largv);
1530Sstevel@tonic-gate static void set_tym(int dirfd, char *nam_p, time_t atime, time_t mtime);
1540Sstevel@tonic-gate static void sigint(int sig);
1550Sstevel@tonic-gate static void swap(char *buf_p, int cnt);
1560Sstevel@tonic-gate static void usage(void);
1570Sstevel@tonic-gate static void verbose(char *nam_p);
1580Sstevel@tonic-gate static void write_hdr(int secflag, off_t len);
1590Sstevel@tonic-gate static void write_trail(void);
1600Sstevel@tonic-gate static int ustar_dir(void);
1610Sstevel@tonic-gate static int ustar_spec(void);
1620Sstevel@tonic-gate static struct stat *convert_to_old_stat(struct stat *, char *, char *);
1630Sstevel@tonic-gate static void read_bar_vol_hdr(void);
1640Sstevel@tonic-gate static void read_bar_file_hdr(void);
1650Sstevel@tonic-gate static void setup_uncompress(FILE **);
1660Sstevel@tonic-gate static void skip_bar_volhdr(void);
1670Sstevel@tonic-gate static void bar_file_in(void);
1680Sstevel@tonic-gate static int g_init(int *devtype, int *fdes);
1690Sstevel@tonic-gate static int g_read(int, int, char *, unsigned);
1700Sstevel@tonic-gate static int g_write(int, int, char *, unsigned);
1710Sstevel@tonic-gate static int is_floppy(int);
1720Sstevel@tonic-gate static int is_tape(int);
1730Sstevel@tonic-gate static int append_secattr(char **, int *, int, char *, char);
1740Sstevel@tonic-gate static void write_ancillary(char *secinfo, int len);
1750Sstevel@tonic-gate static int remove_dir(char *);
1760Sstevel@tonic-gate static int save_cwd(void);
1770Sstevel@tonic-gate static void rest_cwd(int cwd);
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate static void xattrs_out(int (*func)());
1800Sstevel@tonic-gate static void get_parent(char *path, char *dir);
1810Sstevel@tonic-gate static void prepare_xattr_hdr(char **attrbuf, char *filename,
1820Sstevel@tonic-gate     char *attrname, char typeflag, struct Lnk *linkinfo, int *rlen);
1830Sstevel@tonic-gate static char tartype(int type);
1840Sstevel@tonic-gate static int openfile(int omode);
1850Sstevel@tonic-gate static mode_t attrmode(char type);
1860Sstevel@tonic-gate static char *get_component(char *path);
1870Sstevel@tonic-gate static int open_dir(char *name);
1880Sstevel@tonic-gate static int open_dirfd();
1890Sstevel@tonic-gate static void close_dirfd();
1900Sstevel@tonic-gate static void write_xattr_hdr();
1910Sstevel@tonic-gate static int retry_attrdir_open(char *name);
1920Sstevel@tonic-gate static char *skipslashes(char *string, char *start);
1930Sstevel@tonic-gate static int read_xattr_hdr();
1940Sstevel@tonic-gate static void chop_endslashes(char *path);
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate /* helpful types */
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate static
2000Sstevel@tonic-gate struct passwd	*Curpw_p,	/* Current password entry for -t option */
2010Sstevel@tonic-gate 		*Rpw_p,		/* Password entry for -R option */
2020Sstevel@tonic-gate 		*dpasswd;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate static
2050Sstevel@tonic-gate struct group	*Curgr_p,	/* Current group entry for -t option */
2060Sstevel@tonic-gate 		*dgroup;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate /* Data structure for buffered I/O. */
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate static
2110Sstevel@tonic-gate struct buf_info {
2120Sstevel@tonic-gate 	char	*b_base_p,	/* Pointer to base of buffer */
2130Sstevel@tonic-gate 		*b_out_p,	/* Position to take bytes from buffer at */
2140Sstevel@tonic-gate 		*b_in_p,	/* Position to put bytes into buffer at */
2150Sstevel@tonic-gate 		*b_end_p;	/* Pointer to end of buffer */
2160Sstevel@tonic-gate 	long	b_cnt,		/* Count of unprocessed bytes */
2170Sstevel@tonic-gate 		b_size;		/* Size of buffer in bytes */
2180Sstevel@tonic-gate } Buffr;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate /* Generic header format */
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate static
2230Sstevel@tonic-gate struct gen_hdr {
2240Sstevel@tonic-gate 	ulong_t	g_magic,	/* Magic number field */
2250Sstevel@tonic-gate 		g_ino,		/* Inode number of file */
2260Sstevel@tonic-gate 		g_mode,		/* Mode of file */
2270Sstevel@tonic-gate 		g_uid,		/* Uid of file */
2280Sstevel@tonic-gate 		g_gid,		/* Gid of file */
2290Sstevel@tonic-gate 		g_nlink,	/* Number of links */
2300Sstevel@tonic-gate 		g_mtime;	/* Modification time */
2310Sstevel@tonic-gate 	off_t	g_filesz;	/* Length of file */
2320Sstevel@tonic-gate 	ulong_t	g_dev,		/* File system of file */
2330Sstevel@tonic-gate 		g_rdev,		/* Major/minor numbers of special files */
2340Sstevel@tonic-gate 		g_namesz,	/* Length of filename */
2350Sstevel@tonic-gate 		g_cksum;	/* Checksum of file */
2360Sstevel@tonic-gate 	char	g_gname[32],
2370Sstevel@tonic-gate 		g_uname[32],
2380Sstevel@tonic-gate 		g_version[2],
2390Sstevel@tonic-gate 		g_tmagic[6],
2400Sstevel@tonic-gate 		g_typeflag;
2410Sstevel@tonic-gate 	char	*g_tname,
2420Sstevel@tonic-gate 		*g_prefix,
2430Sstevel@tonic-gate 		*g_nam_p,	/* Filename */
2440Sstevel@tonic-gate 		*g_attrnam_p,	/* attribute */
2450Sstevel@tonic-gate 		*g_attrfnam_p,  /* Real file name attr belongs to */
2460Sstevel@tonic-gate 		*g_linktoattrfnam_p, /* file linked attribute belongs to */
2470Sstevel@tonic-gate 		*g_linktoattrnam_p,  /* attribute g_attrnam_p is linked to */
2480Sstevel@tonic-gate 		*g_dirpath;	/* dirname currently opened */
2490Sstevel@tonic-gate 	int	g_dirfd;	/* directory file descriptor */
2500Sstevel@tonic-gate 	int	g_passdirfd;	/* directory fd to pass to */
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate } Gen, *G_p;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate /* Data structure for handling multiply-linked files */
2550Sstevel@tonic-gate static
2560Sstevel@tonic-gate char	prebuf[PRESIZ+1],
2570Sstevel@tonic-gate 	nambuf[NAMSIZ+1],
2580Sstevel@tonic-gate 	fullnam[MAXNAM+1];
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate static
2620Sstevel@tonic-gate struct Lnk {
2630Sstevel@tonic-gate 	short	L_cnt,		/* Number of links encountered */
2640Sstevel@tonic-gate 		L_data;		/* Data has been encountered if 1 */
2650Sstevel@tonic-gate 	struct gen_hdr	L_gen;	/* gen_hdr information for this file */
2660Sstevel@tonic-gate 	struct Lnk	*L_nxt_p,	/* Next file in list */
2670Sstevel@tonic-gate 			*L_bck_p,	/* Previous file in list */
2680Sstevel@tonic-gate 			*L_lnk_p;	/* Next link for this file */
2690Sstevel@tonic-gate } Lnk_hd;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate static
2720Sstevel@tonic-gate struct hdr_cpio	Hdr;
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate /*
2750Sstevel@tonic-gate  * -------------------------------------------------------------------------
2760Sstevel@tonic-gate  *		   Stuff needed to pre-view the name stream
2770Sstevel@tonic-gate  *
2780Sstevel@tonic-gate  * issymlink is used to remember that the current file is a symlink between
2790Sstevel@tonic-gate  * getname() and file_pass(); the former trashes this information immediately
2800Sstevel@tonic-gate  * when -L is specified.
2810Sstevel@tonic-gate  */
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate static
2840Sstevel@tonic-gate int	issymlink = 0;
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate static
2870Sstevel@tonic-gate FILE	*In_p = stdin;		/* Where the input comes from */
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate typedef struct sl_info
2900Sstevel@tonic-gate {
2910Sstevel@tonic-gate 	struct sl_info *llink;	/* Left subtree ptr (tree depth in *sl_head) */
2920Sstevel@tonic-gate 	struct sl_info *rlink;	/* Right subtree ptr */
2930Sstevel@tonic-gate 	int bal;		/* Subtree balance factor */
2940Sstevel@tonic-gate 	ulong_t	sl_count;	/* Number of symlinks */
2950Sstevel@tonic-gate 	ino_t	sl_ino;		/* Inode of file */
2960Sstevel@tonic-gate 	ino_t	sl_ino2;	/* alternate inode for -Hodc */
2970Sstevel@tonic-gate } sl_info_t;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate /*
3000Sstevel@tonic-gate  * The following structure maintains a hash entry for the
3010Sstevel@tonic-gate  * balancing trees which are allocated for each device nodes.
3020Sstevel@tonic-gate  */
3030Sstevel@tonic-gate typedef struct sl_info_link
3040Sstevel@tonic-gate {
3050Sstevel@tonic-gate 	dev_t		dev;
3060Sstevel@tonic-gate 	sl_info_t	*head;
3070Sstevel@tonic-gate 	struct sl_info_link *next;
3080Sstevel@tonic-gate } sl_info_link_t;
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate #define	SL_INFO_ALLOC_CHUNK	1024
3110Sstevel@tonic-gate #define	NDEVHENTRY		0x40
3120Sstevel@tonic-gate #define	DEV_HASHKEY(x)		((x) & (NDEVHENTRY -1))
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate /*
3150Sstevel@tonic-gate  * For remapping dev,inode for -Hodc archives.
3160Sstevel@tonic-gate  */
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate typedef struct sl_remap
3190Sstevel@tonic-gate {
3200Sstevel@tonic-gate 	dev_t			dev;		/* device */
3210Sstevel@tonic-gate 	int			inode_count;	/* # inodes seen on dev */
3220Sstevel@tonic-gate 	struct sl_remap 	*next;		/* next in the chain */
3230Sstevel@tonic-gate } sl_remap_t;
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate /* forward declarations */
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate static sl_info_t 	*sl_info_alloc(void);
3280Sstevel@tonic-gate static sl_info_t 	*sl_insert(dev_t, ino_t);
3290Sstevel@tonic-gate static ulong_t		sl_numlinks(dev_t, ino_t);
3300Sstevel@tonic-gate static void		sl_preview_synonyms(void);
3310Sstevel@tonic-gate static void		sl_remember_tgt(const struct stat *, int);
3320Sstevel@tonic-gate static sl_info_t 	*sl_search(dev_t, ino_t);
3330Sstevel@tonic-gate static sl_info_t	*sl_devhash_lookup(dev_t);
3340Sstevel@tonic-gate static void		sl_devhash_insert(dev_t, sl_info_t *);
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate extern int		sl_compare(ino_t, ino_t);
3370Sstevel@tonic-gate #define	sl_compare(lino, rino)	(lino < rino ? -1 : (lino > rino ? 1 : 0))
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate /* global storage */
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate static sl_remap_t  *sl_remap_head = NULL; /* head of the inode-remap list */
3420Sstevel@tonic-gate static sl_info_link_t	*sl_devhash[NDEVHENTRY]; /* hash table */
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate /*
3450Sstevel@tonic-gate  * -------------------------------------------------------------------------
3460Sstevel@tonic-gate  */
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate static
3490Sstevel@tonic-gate struct stat	ArchSt,	/* stat(2) information of the archive */
3500Sstevel@tonic-gate 		SrcSt,	/* stat(2) information of source file */
3510Sstevel@tonic-gate 		DesSt,	/* stat(2) of destination file */
3520Sstevel@tonic-gate 		*OldSt = NULL;	/* stat info converted to svr32 format */
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate /*
3550Sstevel@tonic-gate  * bin_mag: Used to validate a binary magic number,
3560Sstevel@tonic-gate  * by combining to bytes into an unsigned short.
3570Sstevel@tonic-gate  */
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate static
3600Sstevel@tonic-gate union bin_mag {
3610Sstevel@tonic-gate 	unsigned char b_byte[2];
3620Sstevel@tonic-gate 	ushort_t b_half;
3630Sstevel@tonic-gate } Binmag;
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate static
3660Sstevel@tonic-gate union tblock *Thdr_p;	/* TAR header pointer */
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate static union b_block *bar_Vhdr;
3690Sstevel@tonic-gate static struct gen_hdr Gen_bar_vol;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate /*
3720Sstevel@tonic-gate  * swpbuf: Used in swap() to swap bytes within a halfword,
3730Sstevel@tonic-gate  * halfwords within a word, or to reverse the order of the
3740Sstevel@tonic-gate  * bytes within a word.  Also used in mklong() and mkshort().
3750Sstevel@tonic-gate  */
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate static
3780Sstevel@tonic-gate union swpbuf {
3790Sstevel@tonic-gate 	unsigned char	s_byte[4];
3800Sstevel@tonic-gate 	ushort_t	s_half[2];
3810Sstevel@tonic-gate 	ulong_t	s_word;
3820Sstevel@tonic-gate } *Swp_p;
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate static
3850Sstevel@tonic-gate char	Adir,			/* Flags object as a directory */
3860Sstevel@tonic-gate 	Hiddendir,		/* Processing hidden attribute directory */
3870Sstevel@tonic-gate 	Aspec,			/* Flags object as a special file */
3880Sstevel@tonic-gate 	Do_rename,		/* Indicates rename() is to be used */
3890Sstevel@tonic-gate 	Time[50],		/* Array to hold date and time */
3900Sstevel@tonic-gate 	Ttyname[] = "/dev/tty",	/* Controlling console */
3910Sstevel@tonic-gate 	T_lname[MAXPATHLEN],	/* Array to hold links name for tar */
3920Sstevel@tonic-gate 	*Buf_p,			/* Buffer for file system I/O */
3930Sstevel@tonic-gate 	*Empty,			/* Empty block for TARTYP headers */
3940Sstevel@tonic-gate 	*Full_p,		/* Pointer to full pathname */
3950Sstevel@tonic-gate 	*Efil_p,		/* -E pattern file string */
3960Sstevel@tonic-gate 	*Eom_p = "Change to part %d and press RETURN key. [q] ",
3970Sstevel@tonic-gate 	*Fullnam_p,		/* Full pathname */
3980Sstevel@tonic-gate 	*Attrfile_p,		/* attribute file */
3990Sstevel@tonic-gate 	*Hdr_p,			/* -H header type string */
4000Sstevel@tonic-gate 	*IOfil_p,		/* -I/-O input/output archive string */
4010Sstevel@tonic-gate 	*Lnkend_p,		/* Pointer to end of Lnknam_p */
4020Sstevel@tonic-gate 	*Lnknam_p,		/* Buffer for linking files with -p option */
4030Sstevel@tonic-gate 	*Nam_p,			/* Array to hold filename */
4040Sstevel@tonic-gate 	*Savenam_p,		/* copy of filename xattr belongs to */
4050Sstevel@tonic-gate 	*Own_p,			/* New owner login id string */
4060Sstevel@tonic-gate 	*Renam_p,		/* Buffer for renaming files */
4070Sstevel@tonic-gate 	*Renametmp_p,		/* Tmp Buffer for renaming files */
4080Sstevel@tonic-gate 	*Symlnk_p,		/* Buffer for holding symbolic link name */
4090Sstevel@tonic-gate 	*Over_p,		/* Holds temporary filename when overwriting */
4100Sstevel@tonic-gate 	**Pat_pp = 0,		/* Pattern strings */
4110Sstevel@tonic-gate 	bar_linkflag,		/* flag to indicate if the file is a link */
4120Sstevel@tonic-gate 	bar_linkname[MAXPATHLEN]; /* store the name of the link */
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate static
4150Sstevel@tonic-gate int	Append = 0,	/* Flag set while searching to end of archive */
4160Sstevel@tonic-gate 	Archive,	/* File descriptor of the archive */
4170Sstevel@tonic-gate 	Buf_error = 0,	/* I/O error occured during buffer fill */
4180Sstevel@tonic-gate 	Def_mode = 0777,	/* Default file/directory protection modes */
4190Sstevel@tonic-gate 	Device,		/* Device type being accessed (used with libgenIO) */
4200Sstevel@tonic-gate 	Error_cnt = 0,	/* Cumulative count of I/O errors */
4210Sstevel@tonic-gate 	Finished = 1,	/* Indicates that a file transfer has completed */
4220Sstevel@tonic-gate 	Hdrsz = ASCSZ,	/* Fixed length portion of the header */
4230Sstevel@tonic-gate 	Hdr_type,		/* Flag to indicate type of header selected */
4240Sstevel@tonic-gate 	Ifile,		/* File des. of file being archived */
4250Sstevel@tonic-gate 	Ofile,		/* File des. of file being extracted from archive */
4260Sstevel@tonic-gate 	Use_old_stat = 0,    /* Create an old style -Hodc hdr (small dev's) */
4270Sstevel@tonic-gate 	Onecopy = 0,	/* Flags old vs. new link handling */
4280Sstevel@tonic-gate 	Pad_val = 0,	/* Indicates the number of bytes to pad (if any) */
4290Sstevel@tonic-gate 	PageSize = 0,	/* The native page size, used for figuring block size */
4300Sstevel@tonic-gate 	Volcnt = 1,	/* Number of archive volumes processed */
4310Sstevel@tonic-gate 	Verbcnt = 0,	/* Count of number of dots '.' output */
4320Sstevel@tonic-gate 	Eomflag = 0,
4330Sstevel@tonic-gate 	Dflag = 0,
4340Sstevel@tonic-gate 	Atflag = 0,	/* Archive/restore extended attributes */
4350Sstevel@tonic-gate 	Compressed,	/* Flag to indicate if the bar archive is compressed */
4360Sstevel@tonic-gate 	Bar_vol_num = 0; /* Volume number count for bar archive */
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate static
4400Sstevel@tonic-gate gid_t	Lastgid = -1;	/* Used with -t & -v to record current gid */
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate static
4430Sstevel@tonic-gate uid_t	Lastuid = -1;	/* Used with -t & -v to record current uid */
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate static
4460Sstevel@tonic-gate long	Args,		/* Mask of selected options */
4470Sstevel@tonic-gate 	Max_namesz = CPATH;	/* Maximum size of pathnames/filenames */
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate static
4500Sstevel@tonic-gate int	Bufsize = BUFSZ;	/* Default block size */
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate static u_longlong_t    Blocks;	/* full blocks transferred */
4540Sstevel@tonic-gate static u_longlong_t    SBlocks;	/* cumulative char count from short reads */
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate static off_t	Max_offset = BIN_OFFSET_MAX;	/* largest file size */
4580Sstevel@tonic-gate static off_t	Max_filesz;			/* from getrlimit */
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate static
4610Sstevel@tonic-gate FILE	*Ef_p,			/* File pointer of pattern input file */
4620Sstevel@tonic-gate 	*Err_p = stderr,	/* File pointer for error reporting */
4630Sstevel@tonic-gate 	*Out_p = stdout,	/* File pointer for non-archive output */
4640Sstevel@tonic-gate 	*Rtty_p,		/* Input file pointer for interactive rename */
4650Sstevel@tonic-gate 	*Wtty_p;		/* Output file ptr for interactive rename */
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate static
4680Sstevel@tonic-gate ushort_t	Ftype = S_IFMT;	/* File type mask */
4690Sstevel@tonic-gate static
4700Sstevel@tonic-gate uid_t	Euid;		/* Effective uid of invoker */
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate /* ACL support */
4730Sstevel@tonic-gate static struct sec_attr {
4740Sstevel@tonic-gate 	char	attr_type;
4750Sstevel@tonic-gate 	char	attr_len[7];
4760Sstevel@tonic-gate 	char	attr_info[1];
4770Sstevel@tonic-gate } *attr;
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate static int	Pflag = 0;	/* flag indicates that acl is preserved */
4800Sstevel@tonic-gate static int	aclcnt = 0;	/* acl entry count */
4810Sstevel@tonic-gate static aclent_t *aclp = NULL;	/* pointer to ACL */
4820Sstevel@tonic-gate static int	acl_set = 0;	/* True if an acl was set on the file */
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate /*
4850Sstevel@tonic-gate  *
4860Sstevel@tonic-gate  * cpio has been changed to support extended attributes.
4870Sstevel@tonic-gate  *
4880Sstevel@tonic-gate  * As part of this change cpio has been changed to use the new *at() syscalls
4890Sstevel@tonic-gate  * such as openat, fchownat(), unlinkat()...
4900Sstevel@tonic-gate  *
4910Sstevel@tonic-gate  * This was done so that attributes can be handled with as few code changes
4920Sstevel@tonic-gate  * as possible.
4930Sstevel@tonic-gate  *
4940Sstevel@tonic-gate  * What this means is that cpio now opens the directory that a file or directory
4950Sstevel@tonic-gate  * resides in and then performs *at() functions to manipulate the entry.
4960Sstevel@tonic-gate  *
4970Sstevel@tonic-gate  * For example a new file is now created like this:
4980Sstevel@tonic-gate  *
4990Sstevel@tonic-gate  * dfd = open(<some dir path>)
5000Sstevel@tonic-gate  * fd = openat(dfd, <name>,....);
5010Sstevel@tonic-gate  *
5020Sstevel@tonic-gate  * or in the case of an extended attribute
5030Sstevel@tonic-gate  *
5040Sstevel@tonic-gate  * dfd = attropen(<pathname>, ".", ....)
5050Sstevel@tonic-gate  *
5060Sstevel@tonic-gate  * Once we have a directory file descriptor all of the *at() functions can
5070Sstevel@tonic-gate  * be applied to it.
5080Sstevel@tonic-gate  *
5090Sstevel@tonic-gate  * unlinkat(dfd, <component name>,...)
5100Sstevel@tonic-gate  * fchownat(dfd, <component name>,..)
5110Sstevel@tonic-gate  *
5120Sstevel@tonic-gate  * This works for both normal namespace files and extended attribute file
5130Sstevel@tonic-gate  *
5140Sstevel@tonic-gate  */
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate /*
5170Sstevel@tonic-gate  * Extended attribute layout
5180Sstevel@tonic-gate  *
5190Sstevel@tonic-gate  * Extended attributes are stored in two pieces.
5200Sstevel@tonic-gate  * 1. An attribute header which has information about
5210Sstevel@tonic-gate  *    what file the attribute is for and what the attribute
5220Sstevel@tonic-gate  *    is named.
5230Sstevel@tonic-gate  * 2. The attribute record itself.  Stored as a normal file type
5240Sstevel@tonic-gate  *    of entry.
5250Sstevel@tonic-gate  * Both the header and attribute record have special modes/typeflags
5260Sstevel@tonic-gate  * associated with them.
5270Sstevel@tonic-gate  *
5280Sstevel@tonic-gate  * The names of the header in the archive look like:
5290Sstevel@tonic-gate  * /dev/null/attr.hdr
5300Sstevel@tonic-gate  *
5310Sstevel@tonic-gate  * The name of the attribute looks like:
5320Sstevel@tonic-gate  * /dev/null/attr.
5330Sstevel@tonic-gate  *
5340Sstevel@tonic-gate  * This is done so that an archiver that doesn't understand these formats
5350Sstevel@tonic-gate  * can just dispose of the attribute records unless the user chooses to
5360Sstevel@tonic-gate  * rename them via cpio -r or pax -i
5370Sstevel@tonic-gate  *
5380Sstevel@tonic-gate  * The format is composed of a fixed size header followed
5390Sstevel@tonic-gate  * by a variable sized xattr_buf. If the attribute is a hard link
5400Sstevel@tonic-gate  * to another attribute, then another xattr_buf section is included
5410Sstevel@tonic-gate  * for the link.
5420Sstevel@tonic-gate  *
5430Sstevel@tonic-gate  * The xattr_buf is used to define the necessary "pathing" steps
5440Sstevel@tonic-gate  * to get to the extended attribute.  This is necessary to support
5450Sstevel@tonic-gate  * a fully recursive attribute model where an attribute may itself
5460Sstevel@tonic-gate  * have an attribute.
5470Sstevel@tonic-gate  *
5480Sstevel@tonic-gate  * The basic layout looks like this.
5490Sstevel@tonic-gate  *
5500Sstevel@tonic-gate  *     --------------------------------
5510Sstevel@tonic-gate  *     |                              |
5520Sstevel@tonic-gate  *     |         xattr_hdr            |
5530Sstevel@tonic-gate  *     |                              |
5540Sstevel@tonic-gate  *     --------------------------------
5550Sstevel@tonic-gate  *     --------------------------------
5560Sstevel@tonic-gate  *     |                              |
5570Sstevel@tonic-gate  *     |        xattr_buf             |
5580Sstevel@tonic-gate  *     |                              |
5590Sstevel@tonic-gate  *     --------------------------------
5600Sstevel@tonic-gate  *     --------------------------------
5610Sstevel@tonic-gate  *     |                              |
5620Sstevel@tonic-gate  *     |      (optional link info)    |
5630Sstevel@tonic-gate  *     |                              |
5640Sstevel@tonic-gate  *     --------------------------------
5650Sstevel@tonic-gate  *     --------------------------------
5660Sstevel@tonic-gate  *     |                              |
5670Sstevel@tonic-gate  *     |      attribute itself        |
5680Sstevel@tonic-gate  *     |      stored as normal tar    |
5690Sstevel@tonic-gate  *     |      or cpio data with       |
5700Sstevel@tonic-gate  *     |      special mode or         |
5710Sstevel@tonic-gate  *     |      typeflag                |
5720Sstevel@tonic-gate  *     |                              |
5730Sstevel@tonic-gate  *     --------------------------------
5740Sstevel@tonic-gate  *
5750Sstevel@tonic-gate  */
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate /*
5780Sstevel@tonic-gate  * Extended attributes structures
5790Sstevel@tonic-gate  *
5800Sstevel@tonic-gate  * xattrhead is the complete extended attribute header, as read of off
5810Sstevel@tonic-gate  * disk/tape. It includes the variable xattr_buf portion.
5820Sstevel@tonic-gate  *
5830Sstevel@tonic-gate  * xattrp is basically an offset into xattrhead that points to the
5840Sstevel@tonic-gate  * "pathing" section which defines how to get to the attribute.
5850Sstevel@tonic-gate  *
5860Sstevel@tonic-gate  * xattr_linkp is identical to xattrp except that it is used for linked
5870Sstevel@tonic-gate  * attributes.  It provides the pathing steps to get to the linked
5880Sstevel@tonic-gate  * attribute.
5890Sstevel@tonic-gate  *
5900Sstevel@tonic-gate  * These structures are updated when an extended attribute header is read off
5910Sstevel@tonic-gate  * of disk/tape.
5920Sstevel@tonic-gate  */
5930Sstevel@tonic-gate static struct xattr_hdr	*xattrhead;
5940Sstevel@tonic-gate static struct xattr_buf	*xattrp;
5950Sstevel@tonic-gate static struct xattr_buf	*xattr_linkp;
5960Sstevel@tonic-gate static int 		xattrbadhead;	/* is extended attribute header bad? */
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate static int	append_secattr(char **, int *, int, char *, char);
5990Sstevel@tonic-gate static void	write_ancillary(char *, int);
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate /*
6020Sstevel@tonic-gate  * Note regarding cpio and changes to ensure cpio doesn't try to second
6030Sstevel@tonic-gate  * guess whether it runs with sufficient privileges or not:
6040Sstevel@tonic-gate  *
6050Sstevel@tonic-gate  * cpio has been changed so that it doesn't carry a second implementation of
6060Sstevel@tonic-gate  * the kernel's policy with respect to privileges.  Instead of attempting
6070Sstevel@tonic-gate  * to restore uid and gid from an archive only if cpio is run as uid 0,
6080Sstevel@tonic-gate  * cpio now *always* tries to restore the uid and gid from the archive.
6090Sstevel@tonic-gate  * If the restoration of the uid and gid are unsuccessful, then an error
6100Sstevel@tonic-gate  * message will only be received if cpio is being run with root privileges,
6110Sstevel@tonic-gate  * i.e., effective uid (euid) is 0, or if -R was specified.
6120Sstevel@tonic-gate  *
6130Sstevel@tonic-gate  * Note regarding debugging mechanism for cpio:
6140Sstevel@tonic-gate  *
6150Sstevel@tonic-gate  * The following mechanism is provided to allow us to debug cpio in complicated
6160Sstevel@tonic-gate  * situations, like when it is part of a pipe.  The idea is that you compile
6170Sstevel@tonic-gate  * with -DWAITAROUND defined, and then add the "-z" command line option to the
6180Sstevel@tonic-gate  * target cpio invocation.  If stderr is available, it will tell you to which
6190Sstevel@tonic-gate  * pid to attach the debugger; otherwise, use ps to find it.  Attach to the
6200Sstevel@tonic-gate  * process from the debugger, and, *PRESTO*, you are there!
6210Sstevel@tonic-gate  *
6220Sstevel@tonic-gate  * Simply assign "waitaround = 0" once you attach to the process, and then
6230Sstevel@tonic-gate  * proceed from there as usual.
6240Sstevel@tonic-gate  */
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate #ifdef WAITAROUND
6270Sstevel@tonic-gate int waitaround = 0;		/* wait for rendezvous with the debugger */
6280Sstevel@tonic-gate #endif
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate /*
6310Sstevel@tonic-gate  * Allocation wrappers and their flags
6320Sstevel@tonic-gate  */
6330Sstevel@tonic-gate #define	E_NORMAL	0x0	/* Return NULL if allocation fails */
6340Sstevel@tonic-gate #define	E_EXIT		0x1	/* Exit if allocation fails */
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate static void *e_realloc(int flag, void *old, size_t newsize);
6370Sstevel@tonic-gate static char *e_strdup(int flag, const char *arg);
6380Sstevel@tonic-gate static void *e_valloc(int flag, size_t size);
6390Sstevel@tonic-gate static void *e_zalloc(int flag, size_t size);
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate #define	EXIT_CODE	(Error_cnt > 255 ? 255 : Error_cnt)
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate /*
6440Sstevel@tonic-gate  * main: Call setup() to process options and perform initializations,
6450Sstevel@tonic-gate  * and then select either copy in (-i), copy out (-o), or pass (-p) action.
6460Sstevel@tonic-gate  */
6470Sstevel@tonic-gate 
648*36Schin int
6490Sstevel@tonic-gate main(int argc, char **argv)
6500Sstevel@tonic-gate {
6510Sstevel@tonic-gate 	int i;
6520Sstevel@tonic-gate 	int passret;
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
6550Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
6560Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
6570Sstevel@tonic-gate #endif
6580Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	(void) memset(&Gen, 0, sizeof (Gen));
6610Sstevel@tonic-gate 	setup(argc, argv);
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 	if (signal(SIGINT, sigint) == SIG_IGN)
6640Sstevel@tonic-gate 		(void) signal(SIGINT, SIG_IGN);
6650Sstevel@tonic-gate 	switch (Args & (OCi | OCo | OCp)) {
6660Sstevel@tonic-gate 	case OCi: /* COPY IN */
6670Sstevel@tonic-gate 		Hdr_type = NONE;
6680Sstevel@tonic-gate 		while ((i = gethdr()) != 0) {
6690Sstevel@tonic-gate 			Gen.g_dirfd = -1;
6700Sstevel@tonic-gate 			if (i == 1) {
6710Sstevel@tonic-gate 				file_in();
6720Sstevel@tonic-gate 				/*
6730Sstevel@tonic-gate 				 * Any ACL info for this file would or should
6740Sstevel@tonic-gate 				 * have been used after file_in(); clear out
6750Sstevel@tonic-gate 				 * aclp so it is is not erroneously used on
6760Sstevel@tonic-gate 				 * the next file.
6770Sstevel@tonic-gate 				 */
6780Sstevel@tonic-gate 				if (aclp != NULL) {
6790Sstevel@tonic-gate 					free(aclp);
6800Sstevel@tonic-gate 					aclcnt = 0;
6810Sstevel@tonic-gate 					aclp = NULL;
6820Sstevel@tonic-gate 				}
6830Sstevel@tonic-gate 				acl_set = 0;
6840Sstevel@tonic-gate 			}
6850Sstevel@tonic-gate 			(void) memset(&Gen, 0, sizeof (Gen));
6860Sstevel@tonic-gate 		}
6870Sstevel@tonic-gate 		/* Do not count "extra" "read-ahead" buffered data */
6880Sstevel@tonic-gate 		if (Buffr.b_cnt > Bufsize)
6890Sstevel@tonic-gate 			Blocks -=  (u_longlong_t)(Buffr.b_cnt / Bufsize);
6900Sstevel@tonic-gate 		break;
6910Sstevel@tonic-gate 	case OCo: /* COPY OUT */
6920Sstevel@tonic-gate 		if (Args & OCA) {
6930Sstevel@tonic-gate 			scan4trail();
6940Sstevel@tonic-gate 		}
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 		Gen.g_dirfd = -1;
6970Sstevel@tonic-gate 		Gen.g_dirpath = NULL;
6980Sstevel@tonic-gate 		sl_preview_synonyms();
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 		while ((i = getname()) != 0) {
7010Sstevel@tonic-gate 			if (i == 1) {
7020Sstevel@tonic-gate 				(void) file_out();
7030Sstevel@tonic-gate 				if (Atflag) {
7040Sstevel@tonic-gate 					if (Gen.g_dirfd != -1) {
7050Sstevel@tonic-gate 						(void) close(Gen.g_dirfd);
7060Sstevel@tonic-gate 					}
7070Sstevel@tonic-gate 					Gen.g_dirfd = -1;
7080Sstevel@tonic-gate 					xattrs_out(file_out);
7090Sstevel@tonic-gate 				}
7100Sstevel@tonic-gate 				Hiddendir = 0;
7110Sstevel@tonic-gate 			}
7120Sstevel@tonic-gate 			if (aclp != NULL) {
7130Sstevel@tonic-gate 				free(aclp);
7140Sstevel@tonic-gate 				aclcnt = 0;
7150Sstevel@tonic-gate 				aclp = NULL;
7160Sstevel@tonic-gate 				acl_set = 0;
7170Sstevel@tonic-gate 			}
7180Sstevel@tonic-gate 		}
7190Sstevel@tonic-gate 		write_trail();
7200Sstevel@tonic-gate 		break;
7210Sstevel@tonic-gate 	case OCp: /* PASS */
7220Sstevel@tonic-gate 		sl_preview_synonyms();
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 		Gen.g_dirfd = -1;
7250Sstevel@tonic-gate 		Gen.g_passdirfd = -1;
7260Sstevel@tonic-gate 		Gen.g_dirpath = NULL;
7270Sstevel@tonic-gate 		while (getname()) {
7280Sstevel@tonic-gate 			/*
7290Sstevel@tonic-gate 			 * If file is a fully qualified path then
7300Sstevel@tonic-gate 			 * file_pass will strip off the leading '/'
7310Sstevel@tonic-gate 			 * and we need to save off the unstripped
7320Sstevel@tonic-gate 			 * name for attribute traversal.
7330Sstevel@tonic-gate 			 */
7340Sstevel@tonic-gate 			if (Atflag) {
7350Sstevel@tonic-gate 				(void) strcpy(Savenam_p, Gen.g_nam_p);
7360Sstevel@tonic-gate 			}
7370Sstevel@tonic-gate 			passret = file_pass();
7380Sstevel@tonic-gate 			if (aclp != NULL) {
7390Sstevel@tonic-gate 				free(aclp);
7400Sstevel@tonic-gate 				aclcnt = 0;
7410Sstevel@tonic-gate 				aclp = NULL;
7420Sstevel@tonic-gate 				acl_set = 0;
7430Sstevel@tonic-gate 			}
7440Sstevel@tonic-gate 			if (Gen.g_passdirfd != -1)
7450Sstevel@tonic-gate 				(void) close(Gen.g_passdirfd);
7460Sstevel@tonic-gate 			Gen.g_passdirfd = -1;
7470Sstevel@tonic-gate 			if (Atflag) {
7480Sstevel@tonic-gate 				if (Gen.g_dirfd != -1) {
7490Sstevel@tonic-gate 					(void) close(Gen.g_dirfd);
7500Sstevel@tonic-gate 				}
7510Sstevel@tonic-gate 				Gen.g_dirfd = -1;
7520Sstevel@tonic-gate 				if (passret != FILE_LINKED) {
7530Sstevel@tonic-gate 					Gen.g_nam_p = Savenam_p;
7540Sstevel@tonic-gate 					xattrs_out(file_pass);
7550Sstevel@tonic-gate 				}
7560Sstevel@tonic-gate 			}
7570Sstevel@tonic-gate 		}
7580Sstevel@tonic-gate 		break;
7590Sstevel@tonic-gate 	default:
7600Sstevel@tonic-gate 		msg(EXT, "Impossible action.");
7610Sstevel@tonic-gate 	}
7620Sstevel@tonic-gate 	if (Ofile > 0) {
7630Sstevel@tonic-gate 		if (close(Ofile) != 0)
7640Sstevel@tonic-gate 			msg(EXTN, "close error");
7650Sstevel@tonic-gate 	}
7660Sstevel@tonic-gate 	if (Archive > 0) {
7670Sstevel@tonic-gate 		if (close(Archive) != 0)
7680Sstevel@tonic-gate 			msg(EXTN, "close error");
7690Sstevel@tonic-gate 	}
7700Sstevel@tonic-gate 	Blocks = (u_longlong_t)(Blocks * Bufsize + SBlocks + 0x1FF) >> 9;
7710Sstevel@tonic-gate 	msg(EPOST, "%lld blocks", Blocks);
7720Sstevel@tonic-gate 	if (Error_cnt)
7730Sstevel@tonic-gate 		msg(EPOST, "%d error(s)", Error_cnt);
7740Sstevel@tonic-gate 	return (EXIT_CODE);
7750Sstevel@tonic-gate }
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate /*
7780Sstevel@tonic-gate  * add_lnk: Add a linked file's header to the linked file data structure, by
7790Sstevel@tonic-gate  * either adding it to the end of an existing sub-list or starting
7800Sstevel@tonic-gate  * a new sub-list.  Each sub-list saves the links to a given file.
7810Sstevel@tonic-gate  *
7820Sstevel@tonic-gate  * Directly returns a pointer to the new entry; returns a pointer to the head
7830Sstevel@tonic-gate  * of the sub-list in which that entry is located through the argument.
7840Sstevel@tonic-gate  */
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate static struct Lnk *
7870Sstevel@tonic-gate add_lnk(struct Lnk **sublist_return)
7880Sstevel@tonic-gate {
7890Sstevel@tonic-gate 	struct Lnk *new_entry, *sublist;
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	for (sublist = Lnk_hd.L_nxt_p;
7920Sstevel@tonic-gate 	    sublist != &Lnk_hd;
7930Sstevel@tonic-gate 	    sublist = sublist->L_nxt_p) {
7940Sstevel@tonic-gate 		if (sublist->L_gen.g_ino == G_p->g_ino &&
7950Sstevel@tonic-gate 		    sublist->L_gen.g_dev == G_p->g_dev) {
7960Sstevel@tonic-gate 			/* found */
7970Sstevel@tonic-gate 			break;
7980Sstevel@tonic-gate 		}
7990Sstevel@tonic-gate 	}
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	new_entry = e_zalloc(E_EXIT, sizeof (struct Lnk));
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	new_entry->L_lnk_p = NULL;
8040Sstevel@tonic-gate 	new_entry->L_gen = *G_p; /* structure copy */
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	new_entry->L_gen.g_nam_p = e_zalloc(E_EXIT, (size_t)G_p->g_namesz);
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	(void) strcpy(new_entry->L_gen.g_nam_p, G_p->g_nam_p);
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	if (sublist == &Lnk_hd) {
8110Sstevel@tonic-gate 		/* start new sub-list */
8120Sstevel@tonic-gate 		new_entry->L_nxt_p = &Lnk_hd;
8130Sstevel@tonic-gate 		new_entry->L_bck_p = Lnk_hd.L_bck_p;
8140Sstevel@tonic-gate 		Lnk_hd.L_bck_p = new_entry->L_bck_p->L_nxt_p = new_entry;
8150Sstevel@tonic-gate 		new_entry->L_lnk_p = NULL;
8160Sstevel@tonic-gate 		new_entry->L_cnt = 1;
8170Sstevel@tonic-gate 		new_entry->L_data = Onecopy ? 0 : 1;
8180Sstevel@tonic-gate 		sublist = new_entry;
8190Sstevel@tonic-gate 	} else {
8200Sstevel@tonic-gate 		/* add to existing sub-list */
8210Sstevel@tonic-gate 		struct Lnk *ptr;
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 		sublist->L_cnt++;
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 		for (ptr = sublist;
8260Sstevel@tonic-gate 		    ptr->L_lnk_p != NULL;
8270Sstevel@tonic-gate 		    ptr = ptr->L_lnk_p) {
8280Sstevel@tonic-gate 			ptr->L_gen.g_filesz = G_p->g_filesz;
8290Sstevel@tonic-gate 		}
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 		ptr->L_gen.g_filesz = G_p->g_filesz;
8320Sstevel@tonic-gate 		ptr->L_lnk_p = new_entry;
8330Sstevel@tonic-gate 	}
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	*sublist_return = sublist;
8360Sstevel@tonic-gate 	return (new_entry);
8370Sstevel@tonic-gate }
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate /*
8400Sstevel@tonic-gate  * bfill: Read req_cnt bytes (out of filelen bytes) from the I/O buffer,
8410Sstevel@tonic-gate  * moving them to rd_buf_p.  When there are no bytes left in the I/O buffer,
8420Sstevel@tonic-gate  * Fillbuf is set and the I/O buffer is filled.  The variable dist is the
8430Sstevel@tonic-gate  * distance to lseek if an I/O error is encountered with the -k option set
8440Sstevel@tonic-gate  * (converted to a multiple of Bufsize).
8450Sstevel@tonic-gate  */
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate static int
8480Sstevel@tonic-gate bfill(void)
8490Sstevel@tonic-gate {
8500Sstevel@tonic-gate 	int i = 0, rv;
8510Sstevel@tonic-gate 	static int eof = 0;
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	if (!Dflag) {
8540Sstevel@tonic-gate 	while ((Buffr.b_end_p - Buffr.b_in_p) >= Bufsize) {
8550Sstevel@tonic-gate 		errno = 0;
8560Sstevel@tonic-gate 		if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0) {
8570Sstevel@tonic-gate 			if (((Buffr.b_end_p - Buffr.b_in_p) >= Bufsize) &&
8580Sstevel@tonic-gate 			    (Eomflag == 0)) {
8590Sstevel@tonic-gate 				Eomflag = 1;
8600Sstevel@tonic-gate 				return (1);
8610Sstevel@tonic-gate 			}
8620Sstevel@tonic-gate 			if (errno == ENOSPC) {
8630Sstevel@tonic-gate 				(void) chgreel(INPUT);
8640Sstevel@tonic-gate 				if (Hdr_type == BAR) {
8650Sstevel@tonic-gate 					skip_bar_volhdr();
8660Sstevel@tonic-gate 				}
8670Sstevel@tonic-gate 				continue;
8680Sstevel@tonic-gate 			} else if (Args & OCk) {
8690Sstevel@tonic-gate 				if (i++ > MX_SEEKS)
8700Sstevel@tonic-gate 					msg(EXT, "Cannot recover.");
8710Sstevel@tonic-gate 				if (lseek(Archive, Bufsize, SEEK_REL) < 0)
8720Sstevel@tonic-gate 					msg(EXTN, "Cannot lseek()");
8730Sstevel@tonic-gate 				Error_cnt++;
8740Sstevel@tonic-gate 				Buf_error++;
8750Sstevel@tonic-gate 				rv = 0;
8760Sstevel@tonic-gate 				continue;
8770Sstevel@tonic-gate 			} else
8780Sstevel@tonic-gate 				ioerror(INPUT);
8790Sstevel@tonic-gate 		} /* (rv = g_read(Device, Archive ... */
8800Sstevel@tonic-gate 		if (Hdr_type != BAR || rv == Bufsize) {
8810Sstevel@tonic-gate 			Buffr.b_in_p += rv;
8820Sstevel@tonic-gate 			Buffr.b_cnt += (long)rv;
8830Sstevel@tonic-gate 		}
8840Sstevel@tonic-gate 		if (rv == Bufsize) {
8850Sstevel@tonic-gate 			eof = 0;
8860Sstevel@tonic-gate 			Blocks++;
8870Sstevel@tonic-gate 		} else if (rv == 0) {
8880Sstevel@tonic-gate 			if (!eof) {
8890Sstevel@tonic-gate 				eof = 1;
8900Sstevel@tonic-gate 				break;
8910Sstevel@tonic-gate 			}
8920Sstevel@tonic-gate 			(void) chgreel(INPUT);
8930Sstevel@tonic-gate 			eof = 0;	/* reset the eof after chgreel	*/
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 			/*
8960Sstevel@tonic-gate 			 * if spans multiple volume, skip the volume header of
8970Sstevel@tonic-gate 			 * the next volume so that the file currently being
8980Sstevel@tonic-gate 			 * extracted can continue to be extracted.
8990Sstevel@tonic-gate 			 */
9000Sstevel@tonic-gate 			if (Hdr_type == BAR) {
9010Sstevel@tonic-gate 				skip_bar_volhdr();
9020Sstevel@tonic-gate 			}
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 			continue;
9050Sstevel@tonic-gate 		} else {
9060Sstevel@tonic-gate 			eof = 0;
9070Sstevel@tonic-gate 			SBlocks += (u_longlong_t)rv;
9080Sstevel@tonic-gate 		}
9090Sstevel@tonic-gate 	} /* (Buffr.b_end_p - Buffr.b_in_p) <= Bufsize */
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 	} else {			/* Dflag */
9120Sstevel@tonic-gate 		errno = 0;
9130Sstevel@tonic-gate 		if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0) {
9140Sstevel@tonic-gate 			return (-1);
9150Sstevel@tonic-gate 		} /* (rv = g_read(Device, Archive ... */
9160Sstevel@tonic-gate 		Buffr.b_in_p += rv;
9170Sstevel@tonic-gate 		Buffr.b_cnt += (long)rv;
9180Sstevel@tonic-gate 		if (rv == Bufsize) {
9190Sstevel@tonic-gate 			eof = 0;
9200Sstevel@tonic-gate 			Blocks++;
9210Sstevel@tonic-gate 		} else if (!rv) {
9220Sstevel@tonic-gate 			if (!eof) {
9230Sstevel@tonic-gate 				eof = 1;
9240Sstevel@tonic-gate 				return (rv);
9250Sstevel@tonic-gate 			}
9260Sstevel@tonic-gate 			return (-1);
9270Sstevel@tonic-gate 		} else {
9280Sstevel@tonic-gate 			eof = 0;
9290Sstevel@tonic-gate 			SBlocks += (u_longlong_t)rv;
9300Sstevel@tonic-gate 		}
9310Sstevel@tonic-gate 	}
9320Sstevel@tonic-gate 	return (rv);
9330Sstevel@tonic-gate }
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate /*
9360Sstevel@tonic-gate  * bflush: Move wr_cnt bytes from data_p into the I/O buffer.  When the
9370Sstevel@tonic-gate  * I/O buffer is full, Flushbuf is set and the buffer is written out.
9380Sstevel@tonic-gate  */
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate static void
9410Sstevel@tonic-gate bflush(void)
9420Sstevel@tonic-gate {
9430Sstevel@tonic-gate 	int rv;
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	while (Buffr.b_cnt >= Bufsize) {
9460Sstevel@tonic-gate 		errno = 0;
9470Sstevel@tonic-gate 		if ((rv = g_write(Device, Archive, Buffr.b_out_p,
9480Sstevel@tonic-gate 		    Bufsize)) < 0) {
9490Sstevel@tonic-gate 			if (errno == ENOSPC && !Dflag)
9500Sstevel@tonic-gate 				rv = chgreel(OUTPUT);
9510Sstevel@tonic-gate 			else
9520Sstevel@tonic-gate 				ioerror(OUTPUT);
9530Sstevel@tonic-gate 		}
9540Sstevel@tonic-gate 		Buffr.b_out_p += rv;
9550Sstevel@tonic-gate 		Buffr.b_cnt -= (long)rv;
9560Sstevel@tonic-gate 		if (rv == Bufsize)
9570Sstevel@tonic-gate 			Blocks++;
9580Sstevel@tonic-gate 		else if (rv > 0)
9590Sstevel@tonic-gate 			SBlocks += (u_longlong_t)rv;
9600Sstevel@tonic-gate 	}
9610Sstevel@tonic-gate 	rstbuf();
9620Sstevel@tonic-gate }
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate /*
9650Sstevel@tonic-gate  * chgreel: Determine if end-of-medium has been reached.  If it has,
9660Sstevel@tonic-gate  * close the current medium and prompt the user for the next medium.
9670Sstevel@tonic-gate  */
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate static int
9700Sstevel@tonic-gate chgreel(int dir)
9710Sstevel@tonic-gate {
9720Sstevel@tonic-gate 	int lastchar, tryagain, askagain, rv;
9730Sstevel@tonic-gate 	int tmpdev;
9740Sstevel@tonic-gate 	char str[APATH];
9750Sstevel@tonic-gate 	struct stat statb;
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 	rv = 0;
9780Sstevel@tonic-gate 	if (fstat(Archive, &statb) < 0)
9790Sstevel@tonic-gate 		msg(EXTN, "Error during stat() of archive");
9800Sstevel@tonic-gate 	if ((statb.st_mode & S_IFMT) != S_IFCHR) {
9810Sstevel@tonic-gate 		if (dir == INPUT) {
9820Sstevel@tonic-gate 			msg(EXT, "%s%s\n",
9830Sstevel@tonic-gate 				"Can't read input:  end of file encountered ",
9840Sstevel@tonic-gate 				"prior to expected end of archive.");
9850Sstevel@tonic-gate 		}
9860Sstevel@tonic-gate 	}
9870Sstevel@tonic-gate 	msg(EPOST, "\007End of medium on \"%s\".", dir ? "output" : "input");
9880Sstevel@tonic-gate 	if (is_floppy(Archive))
9890Sstevel@tonic-gate 		(void) ioctl(Archive, FDEJECT, NULL);
9900Sstevel@tonic-gate 	if ((close(Archive) != 0) && (dir == OUTPUT))
9910Sstevel@tonic-gate 		msg(EXTN, "close error");
9920Sstevel@tonic-gate 	Archive = 0;
9930Sstevel@tonic-gate 	Volcnt++;
9940Sstevel@tonic-gate 	for (;;) {
9950Sstevel@tonic-gate 		if (Rtty_p == (FILE *)NULL)
9960Sstevel@tonic-gate 			Rtty_p = fopen(Ttyname, "r");
9970Sstevel@tonic-gate 		do { /* tryagain */
9980Sstevel@tonic-gate 			if (IOfil_p) {
9990Sstevel@tonic-gate 				do {
10000Sstevel@tonic-gate 					msg(EPOST, gettext(Eom_p), Volcnt);
10010Sstevel@tonic-gate 					if (!Rtty_p || fgets(str, sizeof (str),
10020Sstevel@tonic-gate 					    Rtty_p) == (char *)NULL)
10030Sstevel@tonic-gate 						msg(EXT, "Cannot read tty.");
10040Sstevel@tonic-gate 					askagain = 0;
10050Sstevel@tonic-gate 					switch (*str) {
10060Sstevel@tonic-gate 					case '\n':
10070Sstevel@tonic-gate 						(void) strcpy(str, IOfil_p);
10080Sstevel@tonic-gate 						break;
10090Sstevel@tonic-gate 					case 'q':
10100Sstevel@tonic-gate 						exit(EXIT_CODE);
10110Sstevel@tonic-gate 					default:
10120Sstevel@tonic-gate 						askagain = 1;
10130Sstevel@tonic-gate 					}
10140Sstevel@tonic-gate 				} while (askagain);
10150Sstevel@tonic-gate 			} else {
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 				if (Hdr_type == BAR)
10180Sstevel@tonic-gate 					Bar_vol_num++;
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 				msg(EPOST,
10210Sstevel@tonic-gate 				    "To continue, type device/file name when "
10220Sstevel@tonic-gate 				    "ready.");
10230Sstevel@tonic-gate 				if (!Rtty_p || fgets(str, sizeof (str),
10240Sstevel@tonic-gate 				    Rtty_p) == (char *)NULL)
10250Sstevel@tonic-gate 					msg(EXT, "Cannot read tty.");
10260Sstevel@tonic-gate 				lastchar = strlen(str) - 1;
10270Sstevel@tonic-gate 				if (*(str + lastchar) == '\n') /* remove '\n' */
10280Sstevel@tonic-gate 					*(str + lastchar) = '\0';
10290Sstevel@tonic-gate 				if (!*str)
10300Sstevel@tonic-gate 					exit(EXIT_CODE);
10310Sstevel@tonic-gate 			}
10320Sstevel@tonic-gate 			tryagain = 0;
10330Sstevel@tonic-gate 			if ((Archive = open(str, dir)) < 0) {
10340Sstevel@tonic-gate 				msg(ERRN, "Cannot open \"%s\"", str);
10350Sstevel@tonic-gate 				tryagain = 1;
10360Sstevel@tonic-gate 			}
10370Sstevel@tonic-gate 		} while (tryagain);
10380Sstevel@tonic-gate 		(void) g_init(&tmpdev, &Archive);
10390Sstevel@tonic-gate 		if (tmpdev != Device)
10400Sstevel@tonic-gate 			msg(EXT, "Cannot change media types in mid-stream.");
10410Sstevel@tonic-gate 		if (dir == INPUT)
10420Sstevel@tonic-gate 			break;
10430Sstevel@tonic-gate 		else { /* dir == OUTPUT */
10440Sstevel@tonic-gate 			errno = 0;
10450Sstevel@tonic-gate 			if ((rv = g_write(Device, Archive, Buffr.b_out_p,
10460Sstevel@tonic-gate 			    Bufsize)) == Bufsize)
10470Sstevel@tonic-gate 				break;
10480Sstevel@tonic-gate 			else
10490Sstevel@tonic-gate 				msg(ERR,
10500Sstevel@tonic-gate 				    "Unable to write this medium, try "
10510Sstevel@tonic-gate 				    "another.");
10520Sstevel@tonic-gate 		}
10530Sstevel@tonic-gate 	} /* ;; */
10540Sstevel@tonic-gate 	Eomflag = 0;
10550Sstevel@tonic-gate 	return (rv);
10560Sstevel@tonic-gate }
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate /*
10590Sstevel@tonic-gate  * ckname: Check filenames against user specified patterns,
10600Sstevel@tonic-gate  * and/or ask the user for new name when -r is used.
10610Sstevel@tonic-gate  */
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate static int
10640Sstevel@tonic-gate ckname(int flag)
10650Sstevel@tonic-gate {
10660Sstevel@tonic-gate 	int lastchar, len;
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	if (Hdr_type != TAR && Hdr_type != USTAR && Hdr_type != BAR) {
10690Sstevel@tonic-gate 		/* Re-visit tar size issues later */
10700Sstevel@tonic-gate 		if (G_p->g_namesz - 1 > Max_namesz) {
10710Sstevel@tonic-gate 			msg(ERR, "Name exceeds maximum length - skipped.");
10720Sstevel@tonic-gate 			return (F_SKIP);
10730Sstevel@tonic-gate 		}
10740Sstevel@tonic-gate 	}
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 	if (Pat_pp && !matched())
10770Sstevel@tonic-gate 		return (F_SKIP);
10780Sstevel@tonic-gate 	if ((Args & OCr) && !Adir) { /* rename interactively */
10790Sstevel@tonic-gate 		(void) fprintf(Wtty_p, gettext("Rename \"%s%s%s\"? "),
10800Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
10810Sstevel@tonic-gate 		    G_p->g_nam_p : Renam_p,
10820Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
10830Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
10840Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
10850Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p);
10860Sstevel@tonic-gate 		(void) fflush(Wtty_p);
10870Sstevel@tonic-gate 		if (fgets(Renametmp_p, Max_namesz + 1, Rtty_p) == (char *)NULL)
10880Sstevel@tonic-gate 			msg(EXT, "Cannot read tty.");
10890Sstevel@tonic-gate 		if (feof(Rtty_p))
10900Sstevel@tonic-gate 			exit(EXIT_CODE);
10910Sstevel@tonic-gate 		lastchar = strlen(Renametmp_p) - 1;
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 		/* remove trailing '\n' */
10940Sstevel@tonic-gate 		if (*(Renametmp_p + lastchar) == '\n')
10950Sstevel@tonic-gate 			*(Renametmp_p + lastchar) = '\0';
10960Sstevel@tonic-gate 		if (*Renametmp_p == '\0') {
10970Sstevel@tonic-gate 			msg(POST, "%s%s%s Skipped.",
10980Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
10990Sstevel@tonic-gate 			    G_p->g_nam_p : G_p->g_attrfnam_p,
11000Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
11010Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
11020Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
11030Sstevel@tonic-gate 			    "" : G_p->g_attrnam_p);
11040Sstevel@tonic-gate 			*G_p->g_nam_p = '\0';
11050Sstevel@tonic-gate 			return (F_SKIP);
11060Sstevel@tonic-gate 		} else if (strcmp(Renametmp_p, ".") != 0) {
11070Sstevel@tonic-gate 			len = strlen(Renametmp_p);
11080Sstevel@tonic-gate 			if (len > (int)strlen(G_p->g_nam_p)) {
11090Sstevel@tonic-gate 				if ((G_p->g_nam_p != &nambuf[0]) &&
11100Sstevel@tonic-gate 				    (G_p->g_nam_p != &fullnam[0]))
11110Sstevel@tonic-gate 					free(G_p->g_nam_p);
11120Sstevel@tonic-gate 				G_p->g_nam_p = e_zalloc(E_EXIT, len + 1);
11130Sstevel@tonic-gate 			}
11140Sstevel@tonic-gate 			if (G_p->g_attrnam_p != (char *)NULL) {
11150Sstevel@tonic-gate 				free(G_p->g_attrnam_p);
11160Sstevel@tonic-gate 				G_p->g_attrnam_p = e_strdup(E_EXIT,
11170Sstevel@tonic-gate 				    Renametmp_p);
11180Sstevel@tonic-gate 				(void) strcpy(G_p->g_nam_p, Renam_p);
11190Sstevel@tonic-gate 			} else {
11200Sstevel@tonic-gate 				(void) strcpy(Renam_p, Renametmp_p);
11210Sstevel@tonic-gate 				(void) strcpy(G_p->g_nam_p, Renametmp_p);
11220Sstevel@tonic-gate 			}
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 		}
11250Sstevel@tonic-gate 	}
11260Sstevel@tonic-gate 	if (flag != 0 || Onecopy == 0) {
11270Sstevel@tonic-gate 		VERBOSE((Args & OCt), G_p->g_nam_p);
11280Sstevel@tonic-gate 	}
11290Sstevel@tonic-gate 	if (Args & OCt)
11300Sstevel@tonic-gate 		return (F_SKIP);
11310Sstevel@tonic-gate 	return (F_EXTR);
11320Sstevel@tonic-gate }
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate /*
11350Sstevel@tonic-gate  * ckopts: Check the validity of all command line options.
11360Sstevel@tonic-gate  */
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate static void
11390Sstevel@tonic-gate ckopts(long mask)
11400Sstevel@tonic-gate {
11410Sstevel@tonic-gate 	int oflag;
11420Sstevel@tonic-gate 	char *t_p;
11430Sstevel@tonic-gate 	long errmsk;
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 	if (mask & OCi) {
11460Sstevel@tonic-gate 		errmsk = mask & INV_MSK4i;
11470Sstevel@tonic-gate 	} else if (mask & OCo) {
11480Sstevel@tonic-gate 		errmsk = mask & INV_MSK4o;
11490Sstevel@tonic-gate 	} else if (mask & OCp) {
11500Sstevel@tonic-gate 		errmsk = mask & INV_MSK4p;
11510Sstevel@tonic-gate 	} else {
11520Sstevel@tonic-gate 		msg(ERR, "One of -i, -o or -p must be specified.");
11530Sstevel@tonic-gate 		errmsk = 0;
11540Sstevel@tonic-gate 	}
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 	if (errmsk) {
11570Sstevel@tonic-gate 		/* if non-zero, invalid options were specified */
11580Sstevel@tonic-gate 		Error_cnt++;
11590Sstevel@tonic-gate 	}
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 	if ((mask & OCa) && (mask & OCm) && ((mask & OCi) ||
11620Sstevel@tonic-gate 		(mask & OCo))) {
11630Sstevel@tonic-gate 		msg(ERR, "-a and -m are mutually exclusive.");
11640Sstevel@tonic-gate 	}
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 	if ((mask & OCc) && (mask & OCH) && (strcmp("odc", Hdr_p))) {
11670Sstevel@tonic-gate 		msg(ERR, "-c and -H are mutually exclusive.");
11680Sstevel@tonic-gate 	}
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 	if ((mask & OCv) && (mask & OCV)) {
11710Sstevel@tonic-gate 		msg(ERR, "-v and -V are mutually exclusive.");
11720Sstevel@tonic-gate 	}
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	if ((mask & OCt) && (mask & OCV)) {
11750Sstevel@tonic-gate 		msg(ERR, "-t and -V are mutually exclusive.");
11760Sstevel@tonic-gate 	}
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	if ((mask & OCB) && (mask & OCC)) {
11790Sstevel@tonic-gate 		msg(ERR, "-B and -C are mutually exclusive.");
11800Sstevel@tonic-gate 	}
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	if ((mask & OCH) && (mask & OC6)) {
11830Sstevel@tonic-gate 		msg(ERR, "-H and -6 are mutually exclusive.");
11840Sstevel@tonic-gate 	}
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 	if ((mask & OCM) && !((mask & OCI) || (mask & OCO))) {
11870Sstevel@tonic-gate 		msg(ERR, "-M not meaningful without -O or -I.");
11880Sstevel@tonic-gate 	}
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	if ((mask & OCA) && !(mask & OCO)) {
11910Sstevel@tonic-gate 		msg(ERR, "-A requires the -O option.");
11920Sstevel@tonic-gate 	}
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 	if (Bufsize <= 0) {
11950Sstevel@tonic-gate 		msg(ERR, "Illegal size given for -C option.");
11960Sstevel@tonic-gate 	}
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 	if (mask & OCH) {
11990Sstevel@tonic-gate 		t_p = Hdr_p;
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 		while (*t_p != NULL) {
12020Sstevel@tonic-gate 			if (isupper(*t_p)) {
12030Sstevel@tonic-gate 				*t_p = 'a' + (*t_p - 'A');
12040Sstevel@tonic-gate 			}
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 			t_p++;
12070Sstevel@tonic-gate 		}
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 		if (!(strcmp("odc", Hdr_p))) {
12100Sstevel@tonic-gate 			Hdr_type = CHR;
12110Sstevel@tonic-gate 			Max_namesz = CPATH;
12120Sstevel@tonic-gate 			Onecopy = 0;
12130Sstevel@tonic-gate 			Use_old_stat = 1;
12140Sstevel@tonic-gate 		} else if (!(strcmp("crc", Hdr_p))) {
12150Sstevel@tonic-gate 			Hdr_type = CRC;
12160Sstevel@tonic-gate 			Max_namesz = APATH;
12170Sstevel@tonic-gate 			Onecopy = 1;
12180Sstevel@tonic-gate 		} else if (!(strcmp("tar", Hdr_p))) {
12190Sstevel@tonic-gate 			if (Args & OCo) {
12200Sstevel@tonic-gate 				Hdr_type = USTAR;
12210Sstevel@tonic-gate 				Max_namesz = HNAMLEN - 1;
12220Sstevel@tonic-gate 			} else {
12230Sstevel@tonic-gate 				Hdr_type = TAR;
12240Sstevel@tonic-gate 				Max_namesz = TNAMLEN - 1;
12250Sstevel@tonic-gate 			}
12260Sstevel@tonic-gate 			Onecopy = 0;
12270Sstevel@tonic-gate 		} else if (!(strcmp("ustar", Hdr_p))) {
12280Sstevel@tonic-gate 			Hdr_type = USTAR;
12290Sstevel@tonic-gate 			Max_namesz = HNAMLEN - 1;
12300Sstevel@tonic-gate 			Onecopy = 0;
12310Sstevel@tonic-gate 		} else if (!(strcmp("bar", Hdr_p))) {
12320Sstevel@tonic-gate 			if ((Args & OCo) || (Args & OCp)) {
12330Sstevel@tonic-gate 				msg(ERR,
12340Sstevel@tonic-gate 				    "Header type bar can only be used with -i");
12350Sstevel@tonic-gate 			}
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 			if (Args & OCP) {
12380Sstevel@tonic-gate 				msg(ERR,
12390Sstevel@tonic-gate 				    "Can't preserve using bar header");
12400Sstevel@tonic-gate 			}
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 			Hdr_type = BAR;
12430Sstevel@tonic-gate 			Max_namesz = TNAMLEN - 1;
12440Sstevel@tonic-gate 			Onecopy = 0;
12450Sstevel@tonic-gate 		} else {
12460Sstevel@tonic-gate 			msg(ERR, "Invalid header \"%s\" specified", Hdr_p);
12470Sstevel@tonic-gate 		}
12480Sstevel@tonic-gate 	}
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate 	if (mask & OCr) {
12510Sstevel@tonic-gate 		Rtty_p = fopen(Ttyname, "r");
12520Sstevel@tonic-gate 		Wtty_p = fopen(Ttyname, "w");
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 		if (Rtty_p == (FILE *)NULL || Wtty_p == (FILE *)NULL) {
12550Sstevel@tonic-gate 			msg(ERR, "Cannot rename, \"%s\" missing", Ttyname);
12560Sstevel@tonic-gate 		}
12570Sstevel@tonic-gate 	}
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate 	if ((mask & OCE) && (Ef_p = fopen(Efil_p, "r")) == (FILE *)NULL) {
12600Sstevel@tonic-gate 		msg(ERR, "Cannot open \"%s\" to read patterns", Efil_p);
12610Sstevel@tonic-gate 	}
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 	if ((mask & OCI) && (Archive = open(IOfil_p, O_RDONLY)) < 0) {
12640Sstevel@tonic-gate 		msg(ERR, "Cannot open \"%s\" for input", IOfil_p);
12650Sstevel@tonic-gate 	}
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	if (mask & OCO) {
12680Sstevel@tonic-gate 		if (mask & OCA) {
12690Sstevel@tonic-gate 			if ((Archive = open(IOfil_p, O_RDWR)) < 0) {
12700Sstevel@tonic-gate 				msg(ERR,
12710Sstevel@tonic-gate 				    "Cannot open \"%s\" for append",
12720Sstevel@tonic-gate 				    IOfil_p);
12730Sstevel@tonic-gate 			}
12740Sstevel@tonic-gate 		} else {
12750Sstevel@tonic-gate 			oflag = (O_WRONLY | O_CREAT | O_TRUNC);
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 			if ((Archive = open(IOfil_p, oflag, 0777)) < 0) {
12780Sstevel@tonic-gate 				msg(ERR,
12790Sstevel@tonic-gate 				    "Cannot open \"%s\" for output",
12800Sstevel@tonic-gate 				    IOfil_p);
12810Sstevel@tonic-gate 			}
12820Sstevel@tonic-gate 		}
12830Sstevel@tonic-gate 	}
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 	if (mask & OCR) {
12860Sstevel@tonic-gate 		if ((Rpw_p = getpwnam(Own_p)) == (struct passwd *)NULL) {
12870Sstevel@tonic-gate 			msg(ERR, "\"%s\" is not a valid user id", Own_p);
12880Sstevel@tonic-gate 		}
12890Sstevel@tonic-gate 	}
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate 	if ((mask & OCo) && !(mask & OCO)) {
12920Sstevel@tonic-gate 		Out_p = stderr;
12930Sstevel@tonic-gate 	}
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate 	if ((mask & OCp) && ((mask & (OCB|OCC)) == 0)) {
12960Sstevel@tonic-gate 		/*
12970Sstevel@tonic-gate 		 * We are in pass mode with no block size specified.  Use the
12980Sstevel@tonic-gate 		 * larger of the native page size and 8192.
12990Sstevel@tonic-gate 		 */
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate 		Bufsize = (PageSize > 8192) ? PageSize : 8192;
13020Sstevel@tonic-gate 	}
13030Sstevel@tonic-gate }
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate /*
13060Sstevel@tonic-gate  * cksum: Calculate the simple checksum of a file (CRC) or header
13070Sstevel@tonic-gate  * (TARTYP (TAR and USTAR)).  For -o and the CRC header, the file is opened and
13080Sstevel@tonic-gate  * the checksum is calculated.  For -i and the CRC header, the checksum
13090Sstevel@tonic-gate  * is calculated as each block is transferred from the archive I/O buffer
13100Sstevel@tonic-gate  * to the file system I/O buffer.  The TARTYP (TAR and USTAR) headers calculate
13110Sstevel@tonic-gate  * the simple checksum of the header (with the checksum field of the
13120Sstevel@tonic-gate  * header initialized to all spaces (\040).
13130Sstevel@tonic-gate  */
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate static long
13160Sstevel@tonic-gate cksum(char hdr, int byt_cnt, int *err)
13170Sstevel@tonic-gate {
13180Sstevel@tonic-gate 	char *crc_p, *end_p;
13190Sstevel@tonic-gate 	int cnt;
13200Sstevel@tonic-gate 	long checksum = 0L, have;
13210Sstevel@tonic-gate 	off_t lcnt;
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate 	if (err != NULL)
13240Sstevel@tonic-gate 		*err = 0;
13250Sstevel@tonic-gate 	switch (hdr) {
13260Sstevel@tonic-gate 	case CRC:
13270Sstevel@tonic-gate 		if (Args & OCi) { /* do running checksum */
13280Sstevel@tonic-gate 			end_p = Buffr.b_out_p + byt_cnt;
13290Sstevel@tonic-gate 			for (crc_p = Buffr.b_out_p; crc_p < end_p; crc_p++)
13300Sstevel@tonic-gate 				checksum += (long)*crc_p;
13310Sstevel@tonic-gate 			break;
13320Sstevel@tonic-gate 		}
13330Sstevel@tonic-gate 		/* OCo - do checksum of file */
13340Sstevel@tonic-gate 		lcnt = G_p->g_filesz;
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 		while (lcnt > 0) {
13370Sstevel@tonic-gate 			have = (lcnt < Bufsize) ? lcnt : Bufsize;
13380Sstevel@tonic-gate 			errno = 0;
13390Sstevel@tonic-gate 			if (read(Ifile, Buf_p, have) != have) {
13400Sstevel@tonic-gate 				msg(ERR, "Error computing checksum.");
13410Sstevel@tonic-gate 				if (err != NULL)
13420Sstevel@tonic-gate 					*err = 1;
13430Sstevel@tonic-gate 				break;
13440Sstevel@tonic-gate 			}
13450Sstevel@tonic-gate 			end_p = Buf_p + have;
13460Sstevel@tonic-gate 			for (crc_p = Buf_p; crc_p < end_p; crc_p++)
13470Sstevel@tonic-gate 				checksum += (long)*crc_p;
13480Sstevel@tonic-gate 			lcnt -= have;
13490Sstevel@tonic-gate 		}
13500Sstevel@tonic-gate 		if (lseek(Ifile, (off_t)0, SEEK_ABS) < 0)
13510Sstevel@tonic-gate 			msg(ERRN, "Cannot reset file after checksum");
13520Sstevel@tonic-gate 		break;
13530Sstevel@tonic-gate 	case TARTYP: /* TAR and USTAR */
13540Sstevel@tonic-gate 		crc_p = Thdr_p->tbuf.t_cksum;
13550Sstevel@tonic-gate 		for (cnt = 0; cnt < TCRCLEN; cnt++) {
13560Sstevel@tonic-gate 			*crc_p = '\040';
13570Sstevel@tonic-gate 			crc_p++;
13580Sstevel@tonic-gate 		}
13590Sstevel@tonic-gate 		crc_p = (char *)Thdr_p;
13600Sstevel@tonic-gate 		for (cnt = 0; cnt < TARSZ; cnt++) {
13610Sstevel@tonic-gate 			/*
13620Sstevel@tonic-gate 			 * tar uses unsigned checksum, so we must use unsigned
13630Sstevel@tonic-gate 			 * here in order to be able to read tar archives.
13640Sstevel@tonic-gate 			 */
13650Sstevel@tonic-gate 			checksum += (long)((unsigned char)(*crc_p));
13660Sstevel@tonic-gate 			crc_p++;
13670Sstevel@tonic-gate 		}
13680Sstevel@tonic-gate 		break;
13690Sstevel@tonic-gate 	default:
13700Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
13710Sstevel@tonic-gate 	} /* hdr */
13720Sstevel@tonic-gate 	return (checksum);
13730Sstevel@tonic-gate }
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate /*
13760Sstevel@tonic-gate  * creat_hdr: Fill in the generic header structure with the specific
13770Sstevel@tonic-gate  *            header information based on the value of Hdr_type.
13780Sstevel@tonic-gate  *
13790Sstevel@tonic-gate  *            return (1) if this process was successful, and (0) otherwise.
13800Sstevel@tonic-gate  */
13810Sstevel@tonic-gate 
13820Sstevel@tonic-gate static int
13830Sstevel@tonic-gate creat_hdr(void)
13840Sstevel@tonic-gate {
13850Sstevel@tonic-gate 	ushort_t ftype;
13860Sstevel@tonic-gate 	int fullnamesize;
13870Sstevel@tonic-gate 	dev_t dev;
13880Sstevel@tonic-gate 	ino_t ino;
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate 	ftype = SrcSt.st_mode & Ftype;
13910Sstevel@tonic-gate 	Adir = (ftype == S_IFDIR);
13920Sstevel@tonic-gate 	Aspec = (ftype == S_IFBLK || ftype == S_IFCHR || ftype == S_IFIFO);
13930Sstevel@tonic-gate 	switch (Hdr_type) {
13940Sstevel@tonic-gate 		case BIN:
13950Sstevel@tonic-gate 			Gen.g_magic = CMN_BIN;
13960Sstevel@tonic-gate 			break;
13970Sstevel@tonic-gate 		case CHR:
13980Sstevel@tonic-gate 			Gen.g_magic = CMN_BIN;
13990Sstevel@tonic-gate 			break;
14000Sstevel@tonic-gate 		case ASC:
14010Sstevel@tonic-gate 			Gen.g_magic = CMN_ASC;
14020Sstevel@tonic-gate 			break;
14030Sstevel@tonic-gate 		case CRC:
14040Sstevel@tonic-gate 			Gen.g_magic = CMN_CRC;
14050Sstevel@tonic-gate 			break;
14060Sstevel@tonic-gate 		case USTAR:
14070Sstevel@tonic-gate 			/*
14080Sstevel@tonic-gate 			 * If the length of the full name is greater than 256,
14090Sstevel@tonic-gate 			 * print out a message and return.
14100Sstevel@tonic-gate 			 */
14110Sstevel@tonic-gate 			if ((fullnamesize = strlen(Gen.g_nam_p)) > MAXNAM) {
14120Sstevel@tonic-gate 				msg(ERR,
14130Sstevel@tonic-gate 				    "%s: file name too long", Gen.g_nam_p);
14140Sstevel@tonic-gate 				return (0);
14150Sstevel@tonic-gate 			} else if (fullnamesize > NAMSIZ) {
14160Sstevel@tonic-gate 				/*
14170Sstevel@tonic-gate 				 * The length of the full name is greater than
14180Sstevel@tonic-gate 				 * 100, so we must split the filename from the
14190Sstevel@tonic-gate 				 * path
14200Sstevel@tonic-gate 				 */
14210Sstevel@tonic-gate 				char namebuff[NAMSIZ+1];
14220Sstevel@tonic-gate 				char prebuff[PRESIZ+1];
14230Sstevel@tonic-gate 				char *lastslash;
14240Sstevel@tonic-gate 				int presize, namesize;
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 				(void) memset(namebuff, '\0',
14270Sstevel@tonic-gate 				    sizeof (namebuff));
14280Sstevel@tonic-gate 				(void) memset(prebuff, '\0', sizeof (prebuff));
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate 				lastslash = strrchr(Gen.g_nam_p, '/');
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 				if (lastslash != NULL) {
14330Sstevel@tonic-gate 					namesize = strlen(++lastslash);
14340Sstevel@tonic-gate 					presize = fullnamesize - namesize - 1;
14350Sstevel@tonic-gate 				} else {
14360Sstevel@tonic-gate 					namesize = fullnamesize;
14370Sstevel@tonic-gate 					lastslash = Gen.g_nam_p;
14380Sstevel@tonic-gate 					presize = 0;
14390Sstevel@tonic-gate 				}
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate 				/*
14420Sstevel@tonic-gate 				 * If the filename is greater than 100 we can't
14430Sstevel@tonic-gate 				 * archive the file
14440Sstevel@tonic-gate 				 */
14450Sstevel@tonic-gate 				if (namesize > NAMSIZ) {
14460Sstevel@tonic-gate 					msg(ERR,
14470Sstevel@tonic-gate 					    "%s: filename is greater than %d",
14480Sstevel@tonic-gate 					    lastslash, NAMSIZ);
14490Sstevel@tonic-gate 					return (0);
14500Sstevel@tonic-gate 				}
14510Sstevel@tonic-gate 				(void) strncpy(&namebuff[0], lastslash,
14520Sstevel@tonic-gate 				    namesize);
14530Sstevel@tonic-gate 				/*
14540Sstevel@tonic-gate 				 * If the prefix is greater than 155 we can't
14550Sstevel@tonic-gate 				 * archive the file.
14560Sstevel@tonic-gate 				 */
14570Sstevel@tonic-gate 				if (presize > PRESIZ) {
14580Sstevel@tonic-gate 					msg(ERR,
14590Sstevel@tonic-gate 					    "%s: prefix is greater than %d",
14600Sstevel@tonic-gate 					    Gen.g_nam_p, PRESIZ);
14610Sstevel@tonic-gate 					return (0);
14620Sstevel@tonic-gate 				}
14630Sstevel@tonic-gate 				(void) strncpy(&prebuff[0], Gen.g_nam_p,
14640Sstevel@tonic-gate 				    presize);
14650Sstevel@tonic-gate 
14660Sstevel@tonic-gate 				Gen.g_tname = e_zalloc(E_EXIT, namesize + 1);
14670Sstevel@tonic-gate 				(void) strcpy(Gen.g_tname, namebuff);
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate 				Gen.g_prefix = e_zalloc(E_EXIT, presize + 1);
14700Sstevel@tonic-gate 				(void) strcpy(Gen.g_prefix, prebuff);
14710Sstevel@tonic-gate 			} else {
14720Sstevel@tonic-gate 				Gen.g_tname = Gen.g_nam_p;
14730Sstevel@tonic-gate 			}
14740Sstevel@tonic-gate 			(void) strcpy(Gen.g_tmagic, "ustar");
14750Sstevel@tonic-gate 			(void) strcpy(Gen.g_version, "00");
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate 			dpasswd = getpwuid(SrcSt.st_uid);
14780Sstevel@tonic-gate 			if (dpasswd == (struct passwd *)NULL) {
14790Sstevel@tonic-gate 				msg(EPOST,
14800Sstevel@tonic-gate 				    "cpio: could not get passwd information "
14810Sstevel@tonic-gate 				    "for %s%s%s",
14820Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
14830Sstevel@tonic-gate 				    Gen.g_nam_p : Gen.g_attrfnam_p,
14840Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
14850Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
14860Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
14870Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
14880Sstevel@tonic-gate 				/* make name null string */
14890Sstevel@tonic-gate 				Gen.g_uname[0] = '\0';
14900Sstevel@tonic-gate 			} else {
14910Sstevel@tonic-gate 				(void) strncpy(&Gen.g_uname[0],
14920Sstevel@tonic-gate 				    dpasswd->pw_name, 32);
14930Sstevel@tonic-gate 			}
14940Sstevel@tonic-gate 			dgroup = getgrgid(SrcSt.st_gid);
14950Sstevel@tonic-gate 			if (dgroup == (struct group *)NULL) {
14960Sstevel@tonic-gate 				msg(EPOST,
14970Sstevel@tonic-gate 				    "cpio: could not get group information "
14980Sstevel@tonic-gate 				    "for %s%s%S",
14990Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
15000Sstevel@tonic-gate 				    Gen.g_nam_p : Gen.g_attrfnam_p,
15010Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
15020Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
15030Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
15040Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
15050Sstevel@tonic-gate 				/* make name null string */
15060Sstevel@tonic-gate 				Gen.g_gname[0] = '\0';
15070Sstevel@tonic-gate 			} else {
15080Sstevel@tonic-gate 				(void) strncpy(&Gen.g_gname[0],
15090Sstevel@tonic-gate 				    dgroup->gr_name, 32);
15100Sstevel@tonic-gate 			}
15110Sstevel@tonic-gate 			Gen.g_typeflag = tartype(ftype);
15120Sstevel@tonic-gate 			/* FALLTHROUGH */
15130Sstevel@tonic-gate 		case TAR:
15140Sstevel@tonic-gate 			(void) memset(T_lname, '\0', sizeof (T_lname));
15150Sstevel@tonic-gate 			break;
15160Sstevel@tonic-gate 		default:
15170Sstevel@tonic-gate 			msg(EXT, "Impossible header type.");
15180Sstevel@tonic-gate 	}
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 	dev = SrcSt.st_dev;
15210Sstevel@tonic-gate 	ino = SrcSt.st_ino;
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 	if (Use_old_stat) {
15240Sstevel@tonic-gate 		SrcSt = *OldSt;
15250Sstevel@tonic-gate 	}
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 	Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
15280Sstevel@tonic-gate 	Gen.g_uid = SrcSt.st_uid;
15290Sstevel@tonic-gate 	Gen.g_gid = SrcSt.st_gid;
15300Sstevel@tonic-gate 	Gen.g_dev = SrcSt.st_dev;
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 	if (Use_old_stat) {
15330Sstevel@tonic-gate 		/* -Hodc */
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate 		sl_info_t *p = sl_search(dev, ino);
15360Sstevel@tonic-gate 		Gen.g_ino = p ? p->sl_ino2 : -1;
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 		if (Gen.g_ino == (ulong_t)-1) {
15390Sstevel@tonic-gate 			msg(ERR, "%s%s%s: cannot be archived - inode too big "
15400Sstevel@tonic-gate 			    "for -Hodc format",
15410Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
15420Sstevel@tonic-gate 			    Gen.g_nam_p : Gen.g_attrfnam_p,
15430Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
15440Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
15450Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
15460Sstevel@tonic-gate 			    "" : Gen.g_attrnam_p);
15470Sstevel@tonic-gate 			return (0);
15480Sstevel@tonic-gate 		}
15490Sstevel@tonic-gate 	} else {
15500Sstevel@tonic-gate 		Gen.g_ino = SrcSt.st_ino;
15510Sstevel@tonic-gate 	}
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 	Gen.g_mode = SrcSt.st_mode;
15540Sstevel@tonic-gate 	Gen.g_mtime = SrcSt.st_mtime;
15550Sstevel@tonic-gate 	Gen.g_nlink = (Adir) ? SrcSt.st_nlink
15560Sstevel@tonic-gate 	    : sl_numlinks(dev, ino);
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 	if (ftype == S_IFREG || ftype == S_IFLNK)
15590Sstevel@tonic-gate 		Gen.g_filesz = (off_t)SrcSt.st_size;
15600Sstevel@tonic-gate 	else
15610Sstevel@tonic-gate 		Gen.g_filesz = (off_t)0;
15620Sstevel@tonic-gate 	Gen.g_rdev = SrcSt.st_rdev;
15630Sstevel@tonic-gate 	return (1);
15640Sstevel@tonic-gate }
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate /*
15670Sstevel@tonic-gate  * creat_lnk: Create a link from the existing name1_p to name2_p.
15680Sstevel@tonic-gate  */
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate static
15710Sstevel@tonic-gate int
15720Sstevel@tonic-gate creat_lnk(int dirfd, char *name1_p, char *name2_p)
15730Sstevel@tonic-gate {
15740Sstevel@tonic-gate 	int cnt = 0;
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate 	do {
15770Sstevel@tonic-gate 		errno = 0;
15780Sstevel@tonic-gate 		if (!link(name1_p, name2_p)) {
15790Sstevel@tonic-gate 			if (aclp != NULL) {
15800Sstevel@tonic-gate 				free(aclp);
15810Sstevel@tonic-gate 				aclp = NULL;
15820Sstevel@tonic-gate 				acl_set = 0;
15830Sstevel@tonic-gate 			}
15840Sstevel@tonic-gate 			cnt = 0;
15850Sstevel@tonic-gate 			break;
15860Sstevel@tonic-gate 		} else if (errno == EEXIST) {
15870Sstevel@tonic-gate 			if (cnt == 0) {
15880Sstevel@tonic-gate 				struct stat lsb1;
15890Sstevel@tonic-gate 				struct stat lsb2;
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate 				/*
15920Sstevel@tonic-gate 				 * Check to see if we are trying to link this
15930Sstevel@tonic-gate 				 * file to itself.  If so, count the effort as
15940Sstevel@tonic-gate 				 * successful.  If the two files are different,
15950Sstevel@tonic-gate 				 * or if either lstat is unsuccessful, proceed
15960Sstevel@tonic-gate 				 * as we would have otherwise; the appropriate
15970Sstevel@tonic-gate 				 * error will be reported subsequently.
15980Sstevel@tonic-gate 				 */
15990Sstevel@tonic-gate 
16000Sstevel@tonic-gate 				if (lstat(name1_p, &lsb1) != 0) {
16010Sstevel@tonic-gate 					msg(ERR, "Cannot lstat source file %s",
16020Sstevel@tonic-gate 					    name1_p);
16030Sstevel@tonic-gate 				} else {
16040Sstevel@tonic-gate 					if (lstat(name2_p, &lsb2) != 0) {
16050Sstevel@tonic-gate 						msg(ERR, "Cannot lstat "
16060Sstevel@tonic-gate 						    "destination file %s",
16070Sstevel@tonic-gate 						    name2_p);
16080Sstevel@tonic-gate 					} else {
16090Sstevel@tonic-gate 						if (lsb1.st_dev ==
16100Sstevel@tonic-gate 						    lsb2.st_dev &&
16110Sstevel@tonic-gate 						    lsb1.st_ino ==
16120Sstevel@tonic-gate 						    lsb2.st_ino) {
16130Sstevel@tonic-gate 							VERBOSE((Args &
16140Sstevel@tonic-gate 							    (OCv | OCV)),
16150Sstevel@tonic-gate 							    name2_p);
16160Sstevel@tonic-gate 							return (0);
16170Sstevel@tonic-gate 						}
16180Sstevel@tonic-gate 					}
16190Sstevel@tonic-gate 				}
16200Sstevel@tonic-gate 			}
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate 			if (!(Args & OCu) && G_p->g_mtime <= DesSt.st_mtime)
16230Sstevel@tonic-gate 				msg(ERR,
16240Sstevel@tonic-gate 				    "Existing \"%s\" same age or newer",
16250Sstevel@tonic-gate 				    name2_p);
16260Sstevel@tonic-gate 			else if (unlinkat(dirfd, get_component(name2_p), 0) < 0)
16270Sstevel@tonic-gate 				msg(ERRN, "Error cannot unlink \"%s\"",
16280Sstevel@tonic-gate 				    name2_p);
16290Sstevel@tonic-gate 		}
16300Sstevel@tonic-gate 		cnt++;
16310Sstevel@tonic-gate 	} while ((cnt < 2) && missdir(name2_p) == 0);
16320Sstevel@tonic-gate 	if (!cnt) {
16330Sstevel@tonic-gate 		char *newname;
16340Sstevel@tonic-gate 		char *fromname;
16350Sstevel@tonic-gate 		char *attrname;
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate 		newname = name2_p;
16380Sstevel@tonic-gate 		fromname = name1_p;
16390Sstevel@tonic-gate 		attrname = Gen.g_attrnam_p;
16400Sstevel@tonic-gate 		if (attrname) {
16410Sstevel@tonic-gate 			if (Args & OCp) {
16420Sstevel@tonic-gate 				newname = fromname = Fullnam_p;
16430Sstevel@tonic-gate 			} else {
16440Sstevel@tonic-gate 				newname = Gen.g_attrfnam_p;
16450Sstevel@tonic-gate 			}
16460Sstevel@tonic-gate 		}
16470Sstevel@tonic-gate 		if (Args & OCv) {
16480Sstevel@tonic-gate 			(void) fprintf(Err_p,
16490Sstevel@tonic-gate 			    gettext("%s%s%s linked to %s%s%s\n"), newname,
16500Sstevel@tonic-gate 			    (attrname == (char *)NULL) ?
16510Sstevel@tonic-gate 			    "" : gettext(" attribute "),
16520Sstevel@tonic-gate 			    (attrname == (char *)NULL) ?
16530Sstevel@tonic-gate 			    "" : attrname, fromname,
16540Sstevel@tonic-gate 			    (attrname == (char *)NULL) ?
16550Sstevel@tonic-gate 			    "" : gettext(" attribute "),
16560Sstevel@tonic-gate 			    (attrname == (char *)NULL) ?
16570Sstevel@tonic-gate 			    "" : name1_p);
16580Sstevel@tonic-gate 		}
16590Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), newname);
16600Sstevel@tonic-gate 	} else if (cnt == 1)
16610Sstevel@tonic-gate 		msg(ERRN,
16620Sstevel@tonic-gate 		    "Unable to create directory for \"%s\"", name2_p);
16630Sstevel@tonic-gate 	else if (cnt == 2)
16640Sstevel@tonic-gate 		msg(ERRN,
16650Sstevel@tonic-gate 		    "Cannot link \"%s\" and \"%s\"", name1_p, name2_p);
16660Sstevel@tonic-gate 	return (cnt);
16670Sstevel@tonic-gate }
16680Sstevel@tonic-gate 
16690Sstevel@tonic-gate /*
16700Sstevel@tonic-gate  * creat_spec:
16710Sstevel@tonic-gate  *   Create one of the following:
16720Sstevel@tonic-gate  *       directory
16730Sstevel@tonic-gate  *       character special file
16740Sstevel@tonic-gate  *       block special file
16750Sstevel@tonic-gate  *       fifo
16760Sstevel@tonic-gate  */
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate static int
16790Sstevel@tonic-gate creat_spec(int dirfd)
16800Sstevel@tonic-gate {
16810Sstevel@tonic-gate 	char *nam_p;
16820Sstevel@tonic-gate 	int cnt, result, rv = 0;
16830Sstevel@tonic-gate 	char *curdir;
16840Sstevel@tonic-gate 	char *lastslash;
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 	Do_rename = 0;	/* creat_tmp() may reset this */
16870Sstevel@tonic-gate 
16880Sstevel@tonic-gate 	if (Args & OCp) {
16890Sstevel@tonic-gate 		nam_p = Fullnam_p;
16900Sstevel@tonic-gate 	} else {
16910Sstevel@tonic-gate 		nam_p = G_p->g_nam_p;
16920Sstevel@tonic-gate 	}
16930Sstevel@tonic-gate 
16940Sstevel@tonic-gate 	/*
16950Sstevel@tonic-gate 	 * Is this the extraction of the hidden attribute directory?
16960Sstevel@tonic-gate 	 * If so then just set the mode/times correctly, and return.
16970Sstevel@tonic-gate 	 */
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate 	if (Hiddendir) {
17000Sstevel@tonic-gate 		if (fchownat(dirfd, ".", G_p->g_uid, G_p->g_gid, 0) != 0) {
17010Sstevel@tonic-gate 			msg(ERRN,
17020Sstevel@tonic-gate 			    "Cannot chown() \"attribute directory of "
17030Sstevel@tonic-gate 			    "file %s\"", G_p->g_attrfnam_p);
17040Sstevel@tonic-gate 		}
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate 		if (fchmod(dirfd, G_p->g_mode) != 0) {
17070Sstevel@tonic-gate 			msg(ERRN,
17080Sstevel@tonic-gate 			    "Cannot chmod() \"attribute directory of "
17090Sstevel@tonic-gate 			    "file %s\"", G_p->g_attrfnam_p);
17100Sstevel@tonic-gate 		}
17110Sstevel@tonic-gate 
17120Sstevel@tonic-gate 		acl_set = 0;
17130Sstevel@tonic-gate 		if (Pflag && aclp != NULL) {
17140Sstevel@tonic-gate 			if (facl(dirfd, SETACL, aclcnt, aclp) < 0) {
17150Sstevel@tonic-gate 				msg(ERRN,
17160Sstevel@tonic-gate 				    "failed to set acl on attribute"
17170Sstevel@tonic-gate 				    " directory of %s ", G_p->g_attrfnam_p);
17180Sstevel@tonic-gate 			} else {
17190Sstevel@tonic-gate 				acl_set = 1;
17200Sstevel@tonic-gate 			}
17210Sstevel@tonic-gate 			free(aclp);
17220Sstevel@tonic-gate 			aclp = NULL;
17230Sstevel@tonic-gate 		}
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate 		return (1);
17260Sstevel@tonic-gate 	}
17270Sstevel@tonic-gate 
17280Sstevel@tonic-gate 	result = stat(nam_p, &DesSt);
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate 	if (ustar_dir() || Adir) {
17310Sstevel@tonic-gate 		/*
17320Sstevel@tonic-gate 		 *  The archive file is a directory.
17330Sstevel@tonic-gate 		 *  Skip "." and ".."
17340Sstevel@tonic-gate 		 */
17350Sstevel@tonic-gate 
17360Sstevel@tonic-gate 		curdir = strrchr(nam_p, '.');
17370Sstevel@tonic-gate 
17380Sstevel@tonic-gate 		if (curdir != NULL && curdir[1] == NULL) {
17390Sstevel@tonic-gate 			lastslash = strrchr(nam_p, '/');
17400Sstevel@tonic-gate 
17410Sstevel@tonic-gate 			if (lastslash != NULL) {
17420Sstevel@tonic-gate 				lastslash++;
17430Sstevel@tonic-gate 			} else {
17440Sstevel@tonic-gate 				lastslash = nam_p;
17450Sstevel@tonic-gate 			}
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate 			if (!(strcmp(lastslash, ".")) ||
17480Sstevel@tonic-gate 			    !(strcmp(lastslash, ".."))) {
17490Sstevel@tonic-gate 				return (1);
17500Sstevel@tonic-gate 			}
17510Sstevel@tonic-gate 		}
17520Sstevel@tonic-gate 
17530Sstevel@tonic-gate 		if (result == 0) {
17540Sstevel@tonic-gate 			/* A file by the same name exists. */
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate 			/* Take care of ACLs */
17570Sstevel@tonic-gate 			acl_set = 0;
17580Sstevel@tonic-gate 
17590Sstevel@tonic-gate 			if (Pflag && aclp != NULL) {
17600Sstevel@tonic-gate 				if (acl(nam_p, SETACL, aclcnt, aclp) < 0) {
17610Sstevel@tonic-gate 					msg(ERRN,
17620Sstevel@tonic-gate 					    "\"%s\": failed to set acl",
17630Sstevel@tonic-gate 					    nam_p);
17640Sstevel@tonic-gate 				} else {
17650Sstevel@tonic-gate 					acl_set = 1;
17660Sstevel@tonic-gate 				}
17670Sstevel@tonic-gate 
17680Sstevel@tonic-gate 				free(aclp);
17690Sstevel@tonic-gate 				aclp = NULL;
17700Sstevel@tonic-gate 			}
17710Sstevel@tonic-gate 			if (Args & OCd) {
17720Sstevel@tonic-gate 				/*
17730Sstevel@tonic-gate 				 * We are creating directories.  Keep the
17740Sstevel@tonic-gate 				 * existing file.
17750Sstevel@tonic-gate 				 */
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate 				rstfiles(U_KEEP, dirfd);
17780Sstevel@tonic-gate 			}
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate 			/* Report success. */
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate 			return (1);
17830Sstevel@tonic-gate 		}
17840Sstevel@tonic-gate 	} else {
17850Sstevel@tonic-gate 		/* The archive file is not a directory. */
17860Sstevel@tonic-gate 
17870Sstevel@tonic-gate 		if (result == 0) {
17880Sstevel@tonic-gate 			/*
17890Sstevel@tonic-gate 			 * A file by the same name exists.  Move it to a
17900Sstevel@tonic-gate 			 * temporary file.
17910Sstevel@tonic-gate 			 */
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 			if (creat_tmp(nam_p) < 0) {
17940Sstevel@tonic-gate 				/*
17950Sstevel@tonic-gate 				 * We weren't able to create the temp file.
17960Sstevel@tonic-gate 				 * Report failure.
17970Sstevel@tonic-gate 				 */
17980Sstevel@tonic-gate 
17990Sstevel@tonic-gate 				return (0);
18000Sstevel@tonic-gate 			}
18010Sstevel@tonic-gate 		}
18020Sstevel@tonic-gate 	}
18030Sstevel@tonic-gate 
18040Sstevel@tonic-gate 	/*
18050Sstevel@tonic-gate 	 * This pile tries to create the file directly, and, if there is a
18060Sstevel@tonic-gate 	 * problem, creates missing directories, and then tries to create the
18070Sstevel@tonic-gate 	 * file again.  Two strikes and you're out.
18080Sstevel@tonic-gate 	 */
18090Sstevel@tonic-gate 
18100Sstevel@tonic-gate 	cnt = 0;
18110Sstevel@tonic-gate 
18120Sstevel@tonic-gate 	do {
18130Sstevel@tonic-gate 		if (ustar_dir() || Adir) {
18140Sstevel@tonic-gate 			/* The archive file is a directory. */
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate 			result = mkdir(nam_p, G_p->g_mode);
18170Sstevel@tonic-gate 		} else if (ustar_spec() || Aspec) {
18180Sstevel@tonic-gate 			/*
18190Sstevel@tonic-gate 			 * The archive file is block special,
18200Sstevel@tonic-gate 			 * char special or a fifo.
18210Sstevel@tonic-gate 			 */
18220Sstevel@tonic-gate 
18230Sstevel@tonic-gate 			result = mknod(nam_p, (int)G_p->g_mode,
18240Sstevel@tonic-gate 			    (int)G_p->g_rdev);
18250Sstevel@tonic-gate 		}
18260Sstevel@tonic-gate 
18270Sstevel@tonic-gate 		if (result >= 0) {
18280Sstevel@tonic-gate 			/*
18290Sstevel@tonic-gate 			 * The file creation succeeded.  Take care of the ACLs.
18300Sstevel@tonic-gate 			 */
18310Sstevel@tonic-gate 
18320Sstevel@tonic-gate 			acl_set = 0;
18330Sstevel@tonic-gate 
18340Sstevel@tonic-gate 			if (Pflag && aclp != NULL) {
18350Sstevel@tonic-gate 				if (acl(nam_p, SETACL, aclcnt, aclp) < 0) {
18360Sstevel@tonic-gate 					msg(ERRN,
18370Sstevel@tonic-gate 					    "\"%s\": failed to set acl", nam_p);
18380Sstevel@tonic-gate 				} else {
18390Sstevel@tonic-gate 					acl_set = 1;
18400Sstevel@tonic-gate 				}
18410Sstevel@tonic-gate 
18420Sstevel@tonic-gate 				free(aclp);
18430Sstevel@tonic-gate 				aclp = NULL;
18440Sstevel@tonic-gate 			}
18450Sstevel@tonic-gate 
18460Sstevel@tonic-gate 			cnt = 0;
18470Sstevel@tonic-gate 			break;
18480Sstevel@tonic-gate 		}
18490Sstevel@tonic-gate 
18500Sstevel@tonic-gate 		cnt++;
18510Sstevel@tonic-gate 	} while (cnt < 2 && missdir(nam_p) == 0);
18520Sstevel@tonic-gate 
18530Sstevel@tonic-gate 	switch (cnt) {
18540Sstevel@tonic-gate 	case 0:
18550Sstevel@tonic-gate 		rv = 1;
18560Sstevel@tonic-gate 		rstfiles(U_OVER, dirfd);
18570Sstevel@tonic-gate 		break;
18580Sstevel@tonic-gate 
18590Sstevel@tonic-gate 	case 1:
18600Sstevel@tonic-gate 		msg(ERRN,
18610Sstevel@tonic-gate 		    "Cannot create directory for \"%s\"", nam_p);
18620Sstevel@tonic-gate 
18630Sstevel@tonic-gate 		if (*Over_p == '\0') {
18640Sstevel@tonic-gate 			rstfiles(U_KEEP, dirfd);
18650Sstevel@tonic-gate 		}
18660Sstevel@tonic-gate 
18670Sstevel@tonic-gate 		break;
18680Sstevel@tonic-gate 
18690Sstevel@tonic-gate 	case 2:
18700Sstevel@tonic-gate 		if (ustar_dir() || Adir) {
18710Sstevel@tonic-gate 			msg(ERRN, "Cannot create directory \"%s\"", nam_p);
18720Sstevel@tonic-gate 		} else if (ustar_spec() || Aspec) {
18730Sstevel@tonic-gate 			msg(ERRN, "Cannot mknod() \"%s\"", nam_p);
18740Sstevel@tonic-gate 		}
18750Sstevel@tonic-gate 
18760Sstevel@tonic-gate 		if (*Over_p == '\0') {
18770Sstevel@tonic-gate 			rstfiles(U_KEEP, dirfd);
18780Sstevel@tonic-gate 		}
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate 		break;
18810Sstevel@tonic-gate 
18820Sstevel@tonic-gate 	default:
18830Sstevel@tonic-gate 		msg(EXT, "Impossible case.");
18840Sstevel@tonic-gate 	}
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate 	return (rv);
18870Sstevel@tonic-gate }
18880Sstevel@tonic-gate 
18890Sstevel@tonic-gate /*
18900Sstevel@tonic-gate  * creat_tmp:
18910Sstevel@tonic-gate  */
18920Sstevel@tonic-gate 
18930Sstevel@tonic-gate static int
18940Sstevel@tonic-gate creat_tmp(char *nam_p)
18950Sstevel@tonic-gate {
18960Sstevel@tonic-gate 	char *t_p;
18970Sstevel@tonic-gate 	int	cwd;
18980Sstevel@tonic-gate 
18990Sstevel@tonic-gate 	if ((Args & OCp) && G_p->g_ino == DesSt.st_ino &&
19000Sstevel@tonic-gate 	    G_p->g_dev == DesSt.st_dev) {
19010Sstevel@tonic-gate 		msg(ERR, "Attempt to pass a file to itself.");
19020Sstevel@tonic-gate 		return (-1);
19030Sstevel@tonic-gate 	}
19040Sstevel@tonic-gate 
19050Sstevel@tonic-gate 	if (G_p->g_mtime <= DesSt.st_mtime && !(Args & OCu)) {
19060Sstevel@tonic-gate 		msg(ERR, "Existing \"%s\" same age or newer", nam_p);
19070Sstevel@tonic-gate 		return (-1);
19080Sstevel@tonic-gate 	}
19090Sstevel@tonic-gate 
19100Sstevel@tonic-gate 	if (Euid && Aspec) {
19110Sstevel@tonic-gate 		msg(ERR, "Cannot overwrite \"%s\"", nam_p);
19120Sstevel@tonic-gate 		return (-1);
19130Sstevel@tonic-gate 	}
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate 	/* Make the temporary file name. */
19160Sstevel@tonic-gate 
19170Sstevel@tonic-gate 	(void) strcpy(Over_p, nam_p);
19180Sstevel@tonic-gate 	t_p = Over_p + strlen(Over_p);
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate 	while (t_p != Over_p) {
19210Sstevel@tonic-gate 		if (*(t_p - 1) == '/')
19220Sstevel@tonic-gate 			break;
19230Sstevel@tonic-gate 		t_p--;
19240Sstevel@tonic-gate 	}
19250Sstevel@tonic-gate 
19260Sstevel@tonic-gate 	(void) strcpy(t_p, "XXXXXX");
19270Sstevel@tonic-gate 
19280Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
19290Sstevel@tonic-gate 		/*
19300Sstevel@tonic-gate 		 * Save our current directory, so we can go into
19310Sstevel@tonic-gate 		 * the attribute directory to make the temp file
19320Sstevel@tonic-gate 		 * and then return.
19330Sstevel@tonic-gate 		 */
19340Sstevel@tonic-gate 
19350Sstevel@tonic-gate 		cwd = save_cwd();
19360Sstevel@tonic-gate 		(void) fchdir(G_p->g_dirfd);
19370Sstevel@tonic-gate 	}
19380Sstevel@tonic-gate 
19390Sstevel@tonic-gate 	(void) mktemp(Over_p);
19400Sstevel@tonic-gate 
19410Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
19420Sstevel@tonic-gate 		/* Return to the current directory. */
19430Sstevel@tonic-gate 
19440Sstevel@tonic-gate 		rest_cwd(cwd);
19450Sstevel@tonic-gate 	}
19460Sstevel@tonic-gate 
19470Sstevel@tonic-gate 	if (*Over_p == '\0') {
19480Sstevel@tonic-gate 		/* mktemp reports a failure. */
19490Sstevel@tonic-gate 
19500Sstevel@tonic-gate 		msg(ERR, "Cannot get temporary file name.");
19510Sstevel@tonic-gate 		return (-1);
19520Sstevel@tonic-gate 	}
19530Sstevel@tonic-gate 
19540Sstevel@tonic-gate 	/*
19550Sstevel@tonic-gate 	 * If it's a regular file, write to the temporary file, and then rename
19560Sstevel@tonic-gate 	 * in order to accomodate potential executables.
19570Sstevel@tonic-gate 	 *
19580Sstevel@tonic-gate 	 * Note: g_typeflag is only defined (set) for USTAR archive types.  It
19590Sstevel@tonic-gate 	 * defaults to 0 in the cpio-format-regular file case, so this test
19600Sstevel@tonic-gate 	 * succeeds.
19610Sstevel@tonic-gate 	 */
19620Sstevel@tonic-gate 
19630Sstevel@tonic-gate 	if (G_p->g_typeflag == 0 &&
19640Sstevel@tonic-gate 	    (DesSt.st_mode & (ulong_t)Ftype) == S_IFREG &&
19650Sstevel@tonic-gate 	    (G_p->g_mode & (ulong_t)Ftype) == S_IFREG) {
19660Sstevel@tonic-gate 		/*
19670Sstevel@tonic-gate 		 * The archive file and the filesystem file are both regular
19680Sstevel@tonic-gate 		 * files.  We write to the temporary file in this case.
19690Sstevel@tonic-gate 		 */
19700Sstevel@tonic-gate 
19710Sstevel@tonic-gate 		if (Args & OCp) {
19720Sstevel@tonic-gate 			if (G_p->g_attrnam_p == (char *)NULL) {
19730Sstevel@tonic-gate 				Fullnam_p = Over_p;
19740Sstevel@tonic-gate 			} else {
19750Sstevel@tonic-gate 				Attrfile_p = Over_p;
19760Sstevel@tonic-gate 			}
19770Sstevel@tonic-gate 		} else {
19780Sstevel@tonic-gate 			G_p->g_nam_p = Over_p;
19790Sstevel@tonic-gate 			if (G_p->g_attrnam_p != (char *)NULL) {
19800Sstevel@tonic-gate 				Attrfile_p = Over_p;
19810Sstevel@tonic-gate 			}
19820Sstevel@tonic-gate 		}
19830Sstevel@tonic-gate 
19840Sstevel@tonic-gate 		if (G_p->g_attrnam_p == (char *)NULL) {
19850Sstevel@tonic-gate 			Over_p = nam_p;
19860Sstevel@tonic-gate 		} else {
19870Sstevel@tonic-gate 			Over_p = G_p->g_attrnam_p;
19880Sstevel@tonic-gate 		}
19890Sstevel@tonic-gate 
19900Sstevel@tonic-gate 		Do_rename = 1;
19910Sstevel@tonic-gate 	} else {
19920Sstevel@tonic-gate 		/*
19930Sstevel@tonic-gate 		 * Either the archive file or the filesystem file is not a
19940Sstevel@tonic-gate 		 * regular file.
19950Sstevel@tonic-gate 		 */
19960Sstevel@tonic-gate 
19970Sstevel@tonic-gate 		Do_rename = 0;
19980Sstevel@tonic-gate 
19990Sstevel@tonic-gate 		if (S_ISDIR(DesSt.st_mode)) {
20000Sstevel@tonic-gate 			/*
20010Sstevel@tonic-gate 			 * The filesystem file is a directory.
20020Sstevel@tonic-gate 			 *
20030Sstevel@tonic-gate 			 * Save the current working directory because we will
20040Sstevel@tonic-gate 			 * want to restore it back just in case remove_dir()
20050Sstevel@tonic-gate 			 * fails or get confused about where we should be.
20060Sstevel@tonic-gate 			 */
20070Sstevel@tonic-gate 
20080Sstevel@tonic-gate 			*Over_p = '\0';
20090Sstevel@tonic-gate 			cwd = save_cwd();
20100Sstevel@tonic-gate 
20110Sstevel@tonic-gate 			if (remove_dir(nam_p) < 0) {
20120Sstevel@tonic-gate 				msg(ERRN,
20130Sstevel@tonic-gate 				    "Cannot remove the directory \"%s\"",
20140Sstevel@tonic-gate 				    nam_p);
20150Sstevel@tonic-gate 				/*
20160Sstevel@tonic-gate 				 * Restore working directory back to the one
20170Sstevel@tonic-gate 				 * saved earlier.
20180Sstevel@tonic-gate 				 */
20190Sstevel@tonic-gate 
20200Sstevel@tonic-gate 				rest_cwd(cwd);
20210Sstevel@tonic-gate 				return (-1);
20220Sstevel@tonic-gate 			}
20230Sstevel@tonic-gate 
20240Sstevel@tonic-gate 			/*
20250Sstevel@tonic-gate 			 * Restore working directory back to the one
20260Sstevel@tonic-gate 			 * saved earlier
20270Sstevel@tonic-gate 			 */
20280Sstevel@tonic-gate 
20290Sstevel@tonic-gate 			rest_cwd(cwd);
20300Sstevel@tonic-gate 		} else {
20310Sstevel@tonic-gate 			/*
20320Sstevel@tonic-gate 			 * The file is not a directory. Will use the original
20330Sstevel@tonic-gate 			 * link/unlink construct, however, if the file is
20340Sstevel@tonic-gate 			 * namefs, link would fail with EXDEV. Therefore, we
20350Sstevel@tonic-gate 			 * use rename() first to back up the file.
20360Sstevel@tonic-gate 			 */
20370Sstevel@tonic-gate 			if (rename(nam_p, Over_p) < 0) {
20380Sstevel@tonic-gate 				/*
20390Sstevel@tonic-gate 				 * If rename failed, try old construction
20400Sstevel@tonic-gate 				 * method.
20410Sstevel@tonic-gate 				 */
20420Sstevel@tonic-gate 				if (link(nam_p, Over_p) < 0) {
20430Sstevel@tonic-gate 					msg(ERRN,
20440Sstevel@tonic-gate 					    "Cannot create temporary file");
20450Sstevel@tonic-gate 					*Over_p = '\0';
20460Sstevel@tonic-gate 					return (-1);
20470Sstevel@tonic-gate 				}
20480Sstevel@tonic-gate 
20490Sstevel@tonic-gate 				if (unlink(nam_p) < 0) {
20500Sstevel@tonic-gate 					msg(ERRN,
20510Sstevel@tonic-gate 					    "Cannot unlink() current \"%s\"",
20520Sstevel@tonic-gate 					    nam_p);
20530Sstevel@tonic-gate 					(void) unlink(Over_p);
20540Sstevel@tonic-gate 					*Over_p = '\0';
20550Sstevel@tonic-gate 					return (-1);
20560Sstevel@tonic-gate 				}
20570Sstevel@tonic-gate 			}
20580Sstevel@tonic-gate 		}
20590Sstevel@tonic-gate 	}
20600Sstevel@tonic-gate 
20610Sstevel@tonic-gate 	return (1);
20620Sstevel@tonic-gate }
20630Sstevel@tonic-gate 
20640Sstevel@tonic-gate /*
20650Sstevel@tonic-gate  * data_in:  If proc_mode == P_PROC, bread() the file's data from the archive
20660Sstevel@tonic-gate  * and write(2) it to the open fdes gotten from openout().  If proc_mode ==
20670Sstevel@tonic-gate  * P_SKIP, or becomes P_SKIP (due to errors etc), bread(2) the file's data
20680Sstevel@tonic-gate  * and ignore it.  If the user specified any of the "swap" options (b, s or S),
20690Sstevel@tonic-gate  * and the length of the file is not appropriate for that action, do not
20700Sstevel@tonic-gate  * perform the "swap", otherwise perform the action on a buffer by buffer basis.
20710Sstevel@tonic-gate  * If the CRC header was selected, calculate a running checksum as each buffer
20720Sstevel@tonic-gate  * is processed.
20730Sstevel@tonic-gate  */
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate static void
20760Sstevel@tonic-gate data_in(int proc_mode)
20770Sstevel@tonic-gate {
20780Sstevel@tonic-gate 	char *nam_p;
20790Sstevel@tonic-gate 	int cnt, pad;
20800Sstevel@tonic-gate 	long cksumval = 0L;
20810Sstevel@tonic-gate 	off_t filesz;
20820Sstevel@tonic-gate 	int rv, swapfile = 0;
20830Sstevel@tonic-gate 	int compress_flag = 0;	/* if the file is compressed */
20840Sstevel@tonic-gate 	int cstatus = 0;
20850Sstevel@tonic-gate 	FILE *pipef;		/* pipe for bar to do de-compression */
20860Sstevel@tonic-gate 
20870Sstevel@tonic-gate 
20880Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
20890Sstevel@tonic-gate 		nam_p = G_p->g_attrnam_p;
20900Sstevel@tonic-gate 	} else {
20910Sstevel@tonic-gate 		nam_p = G_p->g_nam_p;
20920Sstevel@tonic-gate 	}
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate 
20950Sstevel@tonic-gate 	if (((G_p->g_mode & Ftype) == S_IFLNK && proc_mode != P_SKIP) ||
20960Sstevel@tonic-gate 	    (Hdr_type == BAR && bar_linkflag == '2' && proc_mode != P_SKIP)) {
20970Sstevel@tonic-gate 		proc_mode = P_SKIP;
20980Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
20990Sstevel@tonic-gate 	}
21000Sstevel@tonic-gate 	if (Args & (OCb | OCs | OCS)) { /* verfify that swapping is possible */
21010Sstevel@tonic-gate 		swapfile = 1;
21020Sstevel@tonic-gate 		if (Args & (OCs | OCb) && G_p->g_filesz % 2) {
21030Sstevel@tonic-gate 			msg(ERR,
21040Sstevel@tonic-gate 			    "Cannot swap bytes of \"%s\", odd number of bytes",
21050Sstevel@tonic-gate 			    nam_p);
21060Sstevel@tonic-gate 			swapfile = 0;
21070Sstevel@tonic-gate 		}
21080Sstevel@tonic-gate 		if (Args & (OCS | OCb) && G_p->g_filesz % 4) {
21090Sstevel@tonic-gate 			msg(ERR,
21100Sstevel@tonic-gate 			    "Cannot swap halfwords of \"%s\", odd number "
21110Sstevel@tonic-gate 			    "of halfwords", nam_p);
21120Sstevel@tonic-gate 			swapfile = 0;
21130Sstevel@tonic-gate 		}
21140Sstevel@tonic-gate 	}
21150Sstevel@tonic-gate 	filesz = G_p->g_filesz;
21160Sstevel@tonic-gate 
21170Sstevel@tonic-gate 	/* This writes out the file from the archive */
21180Sstevel@tonic-gate 
21190Sstevel@tonic-gate 	while (filesz > 0) {
21200Sstevel@tonic-gate 		cnt = (int)(filesz > CPIOBSZ) ? CPIOBSZ : filesz;
21210Sstevel@tonic-gate 		FILL(cnt);
21220Sstevel@tonic-gate 		if (proc_mode != P_SKIP) {
21230Sstevel@tonic-gate 			if (Hdr_type == CRC)
21240Sstevel@tonic-gate 				cksumval += cksum(CRC, cnt, NULL);
21250Sstevel@tonic-gate 			if (swapfile)
21260Sstevel@tonic-gate 				swap(Buffr.b_out_p, cnt);
21270Sstevel@tonic-gate 			errno = 0;
21280Sstevel@tonic-gate 
21290Sstevel@tonic-gate 			/*
21300Sstevel@tonic-gate 			 * if the bar archive is compressed, set up a pipe and
21310Sstevel@tonic-gate 			 * do the de-compression while reading in the file
21320Sstevel@tonic-gate 			 */
21330Sstevel@tonic-gate 			if (Hdr_type == BAR) {
21340Sstevel@tonic-gate 				if (compress_flag == 0 && Compressed) {
21350Sstevel@tonic-gate 					setup_uncompress(&pipef);
21360Sstevel@tonic-gate 					compress_flag++;
21370Sstevel@tonic-gate 				}
21380Sstevel@tonic-gate 
21390Sstevel@tonic-gate 			}
21400Sstevel@tonic-gate 
21410Sstevel@tonic-gate 			rv = write(Ofile, Buffr.b_out_p, cnt);
21420Sstevel@tonic-gate 			if (rv < cnt) {
21430Sstevel@tonic-gate 				if (rv < 0)
21440Sstevel@tonic-gate 					msg(ERRN, "Cannot write \"%s\"", nam_p);
21450Sstevel@tonic-gate 				else
21460Sstevel@tonic-gate 					msg(EXTN, "Cannot write \"%s\"", nam_p);
21470Sstevel@tonic-gate 				proc_mode = P_SKIP;
21480Sstevel@tonic-gate 				rstfiles(U_KEEP, G_p->g_dirfd);
21490Sstevel@tonic-gate 				cstatus = close(Ofile);
21500Sstevel@tonic-gate 				Ofile = 0;
21510Sstevel@tonic-gate 				if (cstatus != 0) {
21520Sstevel@tonic-gate 					msg(EXTN, "close error");
21530Sstevel@tonic-gate 				}
21540Sstevel@tonic-gate 			}
21550Sstevel@tonic-gate 		}
21560Sstevel@tonic-gate 		Buffr.b_out_p += cnt;
21570Sstevel@tonic-gate 		Buffr.b_cnt -= (long)cnt;
21580Sstevel@tonic-gate 		filesz -= (off_t)cnt;
21590Sstevel@tonic-gate 	} /* filesz */
21600Sstevel@tonic-gate 
21610Sstevel@tonic-gate 	pad = (Pad_val + 1 - (G_p->g_filesz & Pad_val)) & Pad_val;
21620Sstevel@tonic-gate 	if (pad != 0) {
21630Sstevel@tonic-gate 		FILL(pad);
21640Sstevel@tonic-gate 		Buffr.b_out_p += pad;
21650Sstevel@tonic-gate 		Buffr.b_cnt -= pad;
21660Sstevel@tonic-gate 	}
21670Sstevel@tonic-gate 	if (proc_mode != P_SKIP) {
21680Sstevel@tonic-gate 		if (Hdr_type == CRC && Gen.g_cksum != cksumval) {
21690Sstevel@tonic-gate 			msg(ERR, "\"%s\" - checksum error", nam_p);
21700Sstevel@tonic-gate 			rstfiles(U_KEEP, G_p->g_dirfd);
21710Sstevel@tonic-gate 		} else
21720Sstevel@tonic-gate 			rstfiles(U_OVER, G_p->g_dirfd);
21730Sstevel@tonic-gate 		if (Hdr_type == BAR && compress_flag) {
21740Sstevel@tonic-gate 			(void) pclose(pipef);
21750Sstevel@tonic-gate 		} else {
21760Sstevel@tonic-gate 			cstatus = close(Ofile);
21770Sstevel@tonic-gate 		}
21780Sstevel@tonic-gate 		Ofile = 0;
21790Sstevel@tonic-gate 		if (cstatus != 0) {
21800Sstevel@tonic-gate 			msg(EXTN, "close error");
21810Sstevel@tonic-gate 		}
21820Sstevel@tonic-gate 	}
21830Sstevel@tonic-gate 	VERBOSE((proc_mode != P_SKIP && (Args & (OCv | OCV))), G_p->g_nam_p);
21840Sstevel@tonic-gate 	Finished = 1;
21850Sstevel@tonic-gate }
21860Sstevel@tonic-gate 
21870Sstevel@tonic-gate /*
21880Sstevel@tonic-gate  * data_out:  open(2) the file to be archived, compute the checksum
21890Sstevel@tonic-gate  * of it's data if the CRC header was specified and write the header.
21900Sstevel@tonic-gate  * read(2) each block of data and bwrite() it to the archive.  For TARTYP (TAR
21910Sstevel@tonic-gate  * and USTAR) archives, pad the data with NULLs to the next 512 byte boundary.
21920Sstevel@tonic-gate  */
21930Sstevel@tonic-gate 
21940Sstevel@tonic-gate static void
21950Sstevel@tonic-gate data_out(void)
21960Sstevel@tonic-gate {
21970Sstevel@tonic-gate 	char *nam_p;
21980Sstevel@tonic-gate 	int cnt, amount_read, pad;
21990Sstevel@tonic-gate 	off_t amt_to_read, real_filesz;
22000Sstevel@tonic-gate 	int	errret = 0;
22010Sstevel@tonic-gate 
22020Sstevel@tonic-gate 	nam_p = G_p->g_nam_p;
22030Sstevel@tonic-gate 	if (Aspec) {
22040Sstevel@tonic-gate 		if (Pflag && aclp != NULL) {
22050Sstevel@tonic-gate 			char    *secinfo = NULL;
22060Sstevel@tonic-gate 			int	len = 0;
22070Sstevel@tonic-gate 
22080Sstevel@tonic-gate 			/* append security attributes */
22090Sstevel@tonic-gate 			if (append_secattr(&secinfo, &len, aclcnt,
22100Sstevel@tonic-gate 				(char *)aclp, UFSD_ACL) == -1) {
22110Sstevel@tonic-gate 				msg(ERR,
22120Sstevel@tonic-gate 				    "can create security information");
22130Sstevel@tonic-gate 			}
22140Sstevel@tonic-gate 			/* call append_secattr() if more than one */
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate 			if (len > 0) {
22170Sstevel@tonic-gate 			/* write ancillary only if there is sec info */
22180Sstevel@tonic-gate 				(void) write_hdr(ARCHIVE_ACL, (off_t)len);
22190Sstevel@tonic-gate 				(void) write_ancillary(secinfo, len);
22200Sstevel@tonic-gate 			}
22210Sstevel@tonic-gate 		}
22220Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
22230Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_dirfd);
22240Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
22250Sstevel@tonic-gate 		return;
22260Sstevel@tonic-gate 	}
22270Sstevel@tonic-gate 	if ((G_p->g_mode & Ftype) == S_IFLNK && (Hdr_type !=
22280Sstevel@tonic-gate 	    USTAR && Hdr_type != TAR)) { /* symbolic link */
22290Sstevel@tonic-gate 		int size;
22300Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
22310Sstevel@tonic-gate 
22320Sstevel@tonic-gate 		FLUSH(G_p->g_filesz);
22330Sstevel@tonic-gate 		errno = 0;
22340Sstevel@tonic-gate 
22350Sstevel@tonic-gate 		/* Note that "size" and G_p->g_filesz are the same number */
22360Sstevel@tonic-gate 
22370Sstevel@tonic-gate 		if ((size = readlink(nam_p, Buffr.b_in_p, G_p->g_filesz)) <
22380Sstevel@tonic-gate 		    0) {
22390Sstevel@tonic-gate 			msg(ERRN, "Cannot read symbolic link \"%s\"", nam_p);
22400Sstevel@tonic-gate 			return;
22410Sstevel@tonic-gate 		}
22420Sstevel@tonic-gate 
22430Sstevel@tonic-gate 		/*
22440Sstevel@tonic-gate 		 * Note that it is OK not to add the NUL after the name read by
22450Sstevel@tonic-gate 		 * readlink, because it is not being used subsequently.
22460Sstevel@tonic-gate 		 */
22470Sstevel@tonic-gate 
22480Sstevel@tonic-gate 		Buffr.b_in_p += size;
22490Sstevel@tonic-gate 		Buffr.b_cnt += size;
22500Sstevel@tonic-gate 		pad = (Pad_val + 1 - (size & Pad_val)) & Pad_val;
22510Sstevel@tonic-gate 		if (pad != 0) {
22520Sstevel@tonic-gate 			FLUSH(pad);
22530Sstevel@tonic-gate 			(void) memcpy(Buffr.b_in_p, Empty, pad);
22540Sstevel@tonic-gate 			Buffr.b_in_p += pad;
22550Sstevel@tonic-gate 			Buffr.b_cnt += pad;
22560Sstevel@tonic-gate 		}
22570Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
22580Sstevel@tonic-gate 		return;
22590Sstevel@tonic-gate 	} else if ((G_p->g_mode & Ftype) == S_IFLNK &&
22600Sstevel@tonic-gate 		    (Hdr_type == USTAR || Hdr_type == TAR)) {
22610Sstevel@tonic-gate 		int size;
22620Sstevel@tonic-gate 
22630Sstevel@tonic-gate 		/*
22640Sstevel@tonic-gate 		 * G_p->g_filesz is the length of the right-hand side of
22650Sstevel@tonic-gate 		 * the symlink "x -> y".
22660Sstevel@tonic-gate 		 * The tar link field is only NAMSIZ long.
22670Sstevel@tonic-gate 		 */
22680Sstevel@tonic-gate 
22690Sstevel@tonic-gate 		if (G_p->g_filesz > NAMSIZ) {
22700Sstevel@tonic-gate 			msg(ERRN,
22710Sstevel@tonic-gate 			    "Symbolic link too long \"%s\"", nam_p);
22720Sstevel@tonic-gate 			return;
22730Sstevel@tonic-gate 		}
22740Sstevel@tonic-gate 		if ((size = readlink(nam_p, T_lname, G_p->g_filesz)) < 0) {
22750Sstevel@tonic-gate 			msg(ERRN,
22760Sstevel@tonic-gate 			    "Cannot read symbolic link \"%s\"", nam_p);
22770Sstevel@tonic-gate 			return;
22780Sstevel@tonic-gate 		}
22790Sstevel@tonic-gate 		T_lname[size] = '\0';
22800Sstevel@tonic-gate 		G_p->g_filesz = (off_t)0;
22810Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
22820Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
22830Sstevel@tonic-gate 		return;
22840Sstevel@tonic-gate 	}
22850Sstevel@tonic-gate 	if ((Ifile = openfile(O_RDONLY)) < 0) {
22860Sstevel@tonic-gate 		msg(ERR, "\"%s%s%s\" ?",
22870Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
22880Sstevel@tonic-gate 		    nam_p : Gen.g_attrfnam_p,
22890Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
22900Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
22910Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
22920Sstevel@tonic-gate 		    "" : Gen.g_attrnam_p);
22930Sstevel@tonic-gate 		return;
22940Sstevel@tonic-gate 	}
22950Sstevel@tonic-gate 
22960Sstevel@tonic-gate 	/*
22970Sstevel@tonic-gate 	 * Dump extended attribute header.
22980Sstevel@tonic-gate 	 */
22990Sstevel@tonic-gate 
23000Sstevel@tonic-gate 	if (Gen.g_attrnam_p != (char *)NULL) {
23010Sstevel@tonic-gate 		write_xattr_hdr();
23020Sstevel@tonic-gate 	}
23030Sstevel@tonic-gate 
23040Sstevel@tonic-gate 	if (Hdr_type == CRC) {
23050Sstevel@tonic-gate 		long csum = cksum(CRC, 0, &errret);
23060Sstevel@tonic-gate 		if (errret != 0) {
23070Sstevel@tonic-gate 			G_p->g_cksum = (ulong_t)-1;
23080Sstevel@tonic-gate 			msg(POST, "\"%s%s%s\" skipped",
23090Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
23100Sstevel@tonic-gate 			    nam_p : Gen.g_attrfnam_p,
23110Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
23120Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
23130Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
23140Sstevel@tonic-gate 			    "" : nam_p);
23150Sstevel@tonic-gate 			(void) close(Ifile);
23160Sstevel@tonic-gate 			return;
23170Sstevel@tonic-gate 		}
23180Sstevel@tonic-gate 		G_p->g_cksum = csum;
23190Sstevel@tonic-gate 	} else {
23200Sstevel@tonic-gate 		G_p->g_cksum = 0;
23210Sstevel@tonic-gate 	}
23220Sstevel@tonic-gate 
23230Sstevel@tonic-gate 	/*
23240Sstevel@tonic-gate 	 * ACL has been retrieved in getname().
23250Sstevel@tonic-gate 	 */
23260Sstevel@tonic-gate 	if (Pflag) {
23270Sstevel@tonic-gate 		char    *secinfo = NULL;
23280Sstevel@tonic-gate 		int	len = 0;
23290Sstevel@tonic-gate 
23300Sstevel@tonic-gate 		/* append security attributes */
23310Sstevel@tonic-gate 		if ((append_secattr(&secinfo, &len, aclcnt, (char *)aclp,
23320Sstevel@tonic-gate 		    UFSD_ACL)) == -1)
23330Sstevel@tonic-gate 			msg(ERR, "can create security information");
23340Sstevel@tonic-gate 
23350Sstevel@tonic-gate 		/* call append_secattr() if more than one */
23360Sstevel@tonic-gate 
23370Sstevel@tonic-gate 		if (len > 0) {
23380Sstevel@tonic-gate 		/* write ancillary only if there is sec info */
23390Sstevel@tonic-gate 			(void) write_hdr(ARCHIVE_ACL, (off_t)len);
23400Sstevel@tonic-gate 			(void) write_ancillary(secinfo, len);
23410Sstevel@tonic-gate 		}
23420Sstevel@tonic-gate 	}
23430Sstevel@tonic-gate 
23440Sstevel@tonic-gate 	write_hdr(ARCHIVE_NORMAL, (off_t)0);
23450Sstevel@tonic-gate 
23460Sstevel@tonic-gate 	real_filesz = 0;
23470Sstevel@tonic-gate 
23480Sstevel@tonic-gate 	for (amt_to_read = G_p->g_filesz;
23490Sstevel@tonic-gate 	    amt_to_read > 0;
23500Sstevel@tonic-gate 	    amt_to_read -= (off_t)amount_read) {
23510Sstevel@tonic-gate 		FLUSH(CPIOBSZ);
23520Sstevel@tonic-gate 		errno = 0;
23530Sstevel@tonic-gate 
23540Sstevel@tonic-gate 		if ((amount_read = read(Ifile, Buffr.b_in_p, CPIOBSZ)) < 0) {
23550Sstevel@tonic-gate 			msg(EXTN, "Cannot read \"%s%s%s\"",
23560Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
23570Sstevel@tonic-gate 			    nam_p : Gen.g_attrfnam_p,
23580Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
23590Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
23600Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
23610Sstevel@tonic-gate 			    "" : nam_p);
23620Sstevel@tonic-gate 			break;
23630Sstevel@tonic-gate 		}
23640Sstevel@tonic-gate 
23650Sstevel@tonic-gate 		if (amount_read == 0) {
23660Sstevel@tonic-gate 			/* the file has shrunk */
23670Sstevel@tonic-gate 			real_filesz = G_p->g_filesz - amt_to_read;
23680Sstevel@tonic-gate 			break;
23690Sstevel@tonic-gate 		} else if (amount_read > amt_to_read) {
23700Sstevel@tonic-gate 			/* the file has grown */
23710Sstevel@tonic-gate 			real_filesz = G_p->g_filesz +
23720Sstevel@tonic-gate 				(amount_read - amt_to_read);
23730Sstevel@tonic-gate 			amount_read = amt_to_read;
23740Sstevel@tonic-gate 		} else if (amount_read == amt_to_read) {
23750Sstevel@tonic-gate 			/* the file is the same size */
23760Sstevel@tonic-gate 			real_filesz = G_p->g_filesz;
23770Sstevel@tonic-gate 		}
23780Sstevel@tonic-gate 
23790Sstevel@tonic-gate 		Buffr.b_in_p += amount_read;
23800Sstevel@tonic-gate 		Buffr.b_cnt += (long)amount_read;
23810Sstevel@tonic-gate 	}
23820Sstevel@tonic-gate 
23830Sstevel@tonic-gate 	while (amt_to_read > 0) {
23840Sstevel@tonic-gate 		cnt = (amt_to_read > CPIOBSZ) ? CPIOBSZ : (int)amt_to_read;
23850Sstevel@tonic-gate 		FLUSH(cnt);
23860Sstevel@tonic-gate 		(void) memset(Buffr.b_in_p, NULL, cnt);
23870Sstevel@tonic-gate 		Buffr.b_in_p += cnt;
23880Sstevel@tonic-gate 		Buffr.b_cnt += cnt;
23890Sstevel@tonic-gate 		amt_to_read -= cnt;
23900Sstevel@tonic-gate 	}
23910Sstevel@tonic-gate 
23920Sstevel@tonic-gate 	pad = (Pad_val + 1 - (G_p->g_filesz & Pad_val)) & Pad_val;
23930Sstevel@tonic-gate 	if (pad != 0) {
23940Sstevel@tonic-gate 		FLUSH(pad);
23950Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, Empty, pad);
23960Sstevel@tonic-gate 		Buffr.b_in_p += pad;
23970Sstevel@tonic-gate 		Buffr.b_cnt += pad;
23980Sstevel@tonic-gate 	}
23990Sstevel@tonic-gate 
24000Sstevel@tonic-gate 	if (real_filesz > G_p->g_filesz) {
24010Sstevel@tonic-gate 		msg(ERR, "File size of \"%s%s%s\" has increased by %lld",
24020Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
24030Sstevel@tonic-gate 		    G_p->g_nam_p : Gen.g_attrfnam_p,
24040Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
24050Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
24060Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
24070Sstevel@tonic-gate 		    "" : G_p->g_nam_p,
24080Sstevel@tonic-gate 		    (real_filesz - G_p->g_filesz));
24090Sstevel@tonic-gate 	}
24100Sstevel@tonic-gate 	if (real_filesz < G_p->g_filesz) {
24110Sstevel@tonic-gate 		msg(ERR, "File size of \"%s%s%s\" has decreased by %lld",
24120Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
24130Sstevel@tonic-gate 		    G_p->g_nam_p : Gen.g_attrfnam_p,
24140Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
24150Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
24160Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
24170Sstevel@tonic-gate 		    "" : G_p->g_nam_p,
24180Sstevel@tonic-gate 		    (G_p->g_filesz - real_filesz));
24190Sstevel@tonic-gate 	}
24200Sstevel@tonic-gate 
24210Sstevel@tonic-gate 	(void) close(Ifile);
24220Sstevel@tonic-gate 	rstfiles(U_KEEP, G_p->g_dirfd);
24230Sstevel@tonic-gate 	VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
24240Sstevel@tonic-gate }
24250Sstevel@tonic-gate 
24260Sstevel@tonic-gate /*
24270Sstevel@tonic-gate  * data_pass:  If not a special file (Aspec), open(2) the file to be
24280Sstevel@tonic-gate  * transferred, read(2) each block of data and write(2) it to the output file
24290Sstevel@tonic-gate  * Ofile, which was opened in file_pass().
24300Sstevel@tonic-gate  */
24310Sstevel@tonic-gate 
24320Sstevel@tonic-gate static void
24330Sstevel@tonic-gate data_pass(void)
24340Sstevel@tonic-gate {
24350Sstevel@tonic-gate 	int cnt, done = 1;
24360Sstevel@tonic-gate 	int cstatus;
24370Sstevel@tonic-gate 	off_t filesz;
24380Sstevel@tonic-gate 	char *namep = Nam_p;
24390Sstevel@tonic-gate 
24400Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
24410Sstevel@tonic-gate 		namep = G_p->g_attrnam_p;
24420Sstevel@tonic-gate 	}
24430Sstevel@tonic-gate 	if (Aspec) {
24440Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_passdirfd);
24450Sstevel@tonic-gate 		cstatus = close(Ofile);
24460Sstevel@tonic-gate 		Ofile = 0;
24470Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), Nam_p);
24480Sstevel@tonic-gate 		if (cstatus != 0) {
24490Sstevel@tonic-gate 			msg(EXTN, "close error");
24500Sstevel@tonic-gate 		}
24510Sstevel@tonic-gate 		return;
24520Sstevel@tonic-gate 	}
24530Sstevel@tonic-gate 	if ((Ifile = openat(G_p->g_dirfd, get_component(namep), 0)) < 0) {
24540Sstevel@tonic-gate 		msg(ERRN, "Cannot open \"%s%s%s\", skipped",
24550Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
24560Sstevel@tonic-gate 		    Nam_p : G_p->g_attrfnam_p,
24570Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
24580Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
24590Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
24600Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p);
24610Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_passdirfd);
24620Sstevel@tonic-gate 		cstatus = close(Ofile);
24630Sstevel@tonic-gate 		Ofile = 0;
24640Sstevel@tonic-gate 		if (cstatus != 0) {
24650Sstevel@tonic-gate 			msg(EXTN, "close error");
24660Sstevel@tonic-gate 		}
24670Sstevel@tonic-gate 		return;
24680Sstevel@tonic-gate 	}
24690Sstevel@tonic-gate 	filesz = G_p->g_filesz;
24700Sstevel@tonic-gate 
24710Sstevel@tonic-gate 	while (filesz > 0) {
24720Sstevel@tonic-gate 		cnt = (unsigned)(filesz > Bufsize) ? Bufsize : filesz;
24730Sstevel@tonic-gate 		errno = 0;
24740Sstevel@tonic-gate 		if (read(Ifile, Buf_p, (unsigned)cnt) < 0) {
24750Sstevel@tonic-gate 			msg(ERRN, "Cannot read \"%s%s%s\"",
24760Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
24770Sstevel@tonic-gate 			    Nam_p : G_p->g_attrfnam_p,
24780Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
24790Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
24800Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
24810Sstevel@tonic-gate 			    "" : G_p->g_attrnam_p);
24820Sstevel@tonic-gate 			done = 0;
24830Sstevel@tonic-gate 			break;
24840Sstevel@tonic-gate 		}
24850Sstevel@tonic-gate 		errno = 0;
24860Sstevel@tonic-gate 		if (write(Ofile, Buf_p, (unsigned)cnt) < 0) {
24870Sstevel@tonic-gate 			if (Do_rename) {
24880Sstevel@tonic-gate 				msg(ERRN, "Cannot write \"%s%s%s\"", Over_p,
24890Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
24900Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
24910Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
24920Sstevel@tonic-gate 				    "" : Over_p);
24930Sstevel@tonic-gate 			} else {
24940Sstevel@tonic-gate 				msg(ERRN, "Cannot write \"%s%s%s\"",
24950Sstevel@tonic-gate 				    Fullnam_p,
24960Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
24970Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
24980Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
24990Sstevel@tonic-gate 				    "" : G_p->g_attrnam_p);
25000Sstevel@tonic-gate 			}
25010Sstevel@tonic-gate 
25020Sstevel@tonic-gate 			done = 0;
25030Sstevel@tonic-gate 			break;
25040Sstevel@tonic-gate 		}
25050Sstevel@tonic-gate 		Blocks += (u_longlong_t)((cnt + (Bufsize - 1)) / Bufsize);
25060Sstevel@tonic-gate 		filesz -= (off_t)cnt;
25070Sstevel@tonic-gate 	}
25080Sstevel@tonic-gate 	if (done) {
25090Sstevel@tonic-gate 		rstfiles(U_OVER, G_p->g_passdirfd);
25100Sstevel@tonic-gate 	} else {
25110Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_passdirfd);
25120Sstevel@tonic-gate 	}
25130Sstevel@tonic-gate 	(void) close(Ifile);
25140Sstevel@tonic-gate 	cstatus = close(Ofile);
25150Sstevel@tonic-gate 	Ofile = 0;
25160Sstevel@tonic-gate 	if (cstatus != 0) {
25170Sstevel@tonic-gate 		msg(EXTN, "close error");
25180Sstevel@tonic-gate 	}
25190Sstevel@tonic-gate 	VERBOSE((Args & (OCv | OCV)), Fullnam_p);
25200Sstevel@tonic-gate 	Finished = 1;
25210Sstevel@tonic-gate }
25220Sstevel@tonic-gate 
25230Sstevel@tonic-gate /*
25240Sstevel@tonic-gate  * Allocation wrappers.  Used to centralize error handling for
25250Sstevel@tonic-gate  * failed allocations.
25260Sstevel@tonic-gate  */
25270Sstevel@tonic-gate static void *
25280Sstevel@tonic-gate e_alloc_fail(int flag)
25290Sstevel@tonic-gate {
25300Sstevel@tonic-gate 	if (flag == E_EXIT) {
25310Sstevel@tonic-gate 		msg(EXTN, "Out of memory");
25320Sstevel@tonic-gate 	}
25330Sstevel@tonic-gate 
25340Sstevel@tonic-gate 	return (NULL);
25350Sstevel@tonic-gate }
25360Sstevel@tonic-gate 
25370Sstevel@tonic-gate /*
25380Sstevel@tonic-gate  *  Note: unlike the other e_*lloc functions, e_realloc does not zero out the
25390Sstevel@tonic-gate  *  additional memory it returns.  Ensure that you do not trust its contents
25400Sstevel@tonic-gate  *  when you call it.
25410Sstevel@tonic-gate  */
25420Sstevel@tonic-gate 
25430Sstevel@tonic-gate static void *
25440Sstevel@tonic-gate e_realloc(int flag, void *old, size_t newsize)
25450Sstevel@tonic-gate {
25460Sstevel@tonic-gate 	void *ret = realloc(old, newsize);
25470Sstevel@tonic-gate 
25480Sstevel@tonic-gate 	if (ret == NULL) {
25490Sstevel@tonic-gate 		return (e_alloc_fail(flag));
25500Sstevel@tonic-gate 	}
25510Sstevel@tonic-gate 
25520Sstevel@tonic-gate 	return (ret);
25530Sstevel@tonic-gate }
25540Sstevel@tonic-gate 
25550Sstevel@tonic-gate static char *
25560Sstevel@tonic-gate e_strdup(int flag, const char *arg)
25570Sstevel@tonic-gate {
25580Sstevel@tonic-gate 	char *ret = strdup(arg);
25590Sstevel@tonic-gate 
25600Sstevel@tonic-gate 	if (ret == NULL) {
25610Sstevel@tonic-gate 		return (e_alloc_fail(flag));
25620Sstevel@tonic-gate 	}
25630Sstevel@tonic-gate 
25640Sstevel@tonic-gate 	return (ret);
25650Sstevel@tonic-gate }
25660Sstevel@tonic-gate 
25670Sstevel@tonic-gate static void *
25680Sstevel@tonic-gate e_valloc(int flag, size_t size)
25690Sstevel@tonic-gate {
25700Sstevel@tonic-gate 	void *ret = valloc(size);
25710Sstevel@tonic-gate 
25720Sstevel@tonic-gate 	if (ret == NULL) {
25730Sstevel@tonic-gate 		return (e_alloc_fail(flag));
25740Sstevel@tonic-gate 	}
25750Sstevel@tonic-gate 
25760Sstevel@tonic-gate 	return (ret);
25770Sstevel@tonic-gate }
25780Sstevel@tonic-gate 
25790Sstevel@tonic-gate static void *
25800Sstevel@tonic-gate e_zalloc(int flag, size_t size)
25810Sstevel@tonic-gate {
25820Sstevel@tonic-gate 	void *ret = malloc(size);
25830Sstevel@tonic-gate 
25840Sstevel@tonic-gate 	if (ret == NULL) {
25850Sstevel@tonic-gate 		return (e_alloc_fail(flag));
25860Sstevel@tonic-gate 	}
25870Sstevel@tonic-gate 
25880Sstevel@tonic-gate 	(void) memset(ret, 0, size);
25890Sstevel@tonic-gate 	return (ret);
25900Sstevel@tonic-gate }
25910Sstevel@tonic-gate 
25920Sstevel@tonic-gate /*
25930Sstevel@tonic-gate  * file_in:  Process an object from the archive.  If a TARTYP (TAR or USTAR)
25940Sstevel@tonic-gate  * archive and g_nlink == 1, link this file to the file name in t_linkname
25950Sstevel@tonic-gate  * and return.  Handle linked files in one of two ways.  If Onecopy == 0, this
25960Sstevel@tonic-gate  * is an old style (binary or -c) archive, create and extract the data for the
25970Sstevel@tonic-gate  * first link found, link all subsequent links to this file and skip their data.
25980Sstevel@tonic-gate  * If Oncecopy == 1, save links until all have been processed, and then
25990Sstevel@tonic-gate  * process the links first to last checking their names against the patterns
26000Sstevel@tonic-gate  * and/or asking the user to rename them.  The first link that is accepted
26010Sstevel@tonic-gate  * for xtraction is created and the data is read from the archive.
26020Sstevel@tonic-gate  * All subsequent links that are accepted are linked to this file.
26030Sstevel@tonic-gate  */
26040Sstevel@tonic-gate 
26050Sstevel@tonic-gate static void
26060Sstevel@tonic-gate file_in(void)
26070Sstevel@tonic-gate {
26080Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
26090Sstevel@tonic-gate 	int lnkem = 0, cleanup = 0;
26100Sstevel@tonic-gate 	int proc_file;
26110Sstevel@tonic-gate 	struct Lnk *ttl_p;
26120Sstevel@tonic-gate 	int typeflag;
26130Sstevel@tonic-gate 	char savacl;
26140Sstevel@tonic-gate 	int cwd;
26150Sstevel@tonic-gate 
26160Sstevel@tonic-gate 	G_p = &Gen;
26170Sstevel@tonic-gate 
26180Sstevel@tonic-gate 	/*
26190Sstevel@tonic-gate 	 * Open target directory if this isn't a skipped file
26200Sstevel@tonic-gate 	 * and g_nlink == 1
26210Sstevel@tonic-gate 	 *
26220Sstevel@tonic-gate 	 * Links are handled further down in this function.
26230Sstevel@tonic-gate 	 */
26240Sstevel@tonic-gate 
26250Sstevel@tonic-gate 	proc_file = ckname(0);
26260Sstevel@tonic-gate 
26270Sstevel@tonic-gate 	if (proc_file == F_SKIP && G_p->g_nlink == 1) {
26280Sstevel@tonic-gate 		/*
26290Sstevel@tonic-gate 		 * Normally ckname() prints out the file as a side
26300Sstevel@tonic-gate 		 * effect except for table of contents listing
26310Sstevel@tonic-gate 		 * when its parameter is zero and Onecopy isn't
26320Sstevel@tonic-gate 		 * Zero.  Due to this we need to force the name
26330Sstevel@tonic-gate 		 * to be printed here.
26340Sstevel@tonic-gate 		 */
26350Sstevel@tonic-gate 		if (Onecopy == 1) {
26360Sstevel@tonic-gate 			VERBOSE((Args & OCt), G_p->g_nam_p);
26370Sstevel@tonic-gate 		}
26380Sstevel@tonic-gate 		data_in(P_SKIP);
26390Sstevel@tonic-gate 		return;
26400Sstevel@tonic-gate 	}
26410Sstevel@tonic-gate 
26420Sstevel@tonic-gate 	if (proc_file != F_SKIP && open_dirfd() != 0) {
26430Sstevel@tonic-gate 		data_in(P_SKIP);
26440Sstevel@tonic-gate 		return;
26450Sstevel@tonic-gate 	}
26460Sstevel@tonic-gate 
26470Sstevel@tonic-gate 	if (Hdr_type == BAR) {
26480Sstevel@tonic-gate 		bar_file_in();
26490Sstevel@tonic-gate 		close_dirfd();
26500Sstevel@tonic-gate 		return;
26510Sstevel@tonic-gate 	}
26520Sstevel@tonic-gate 
26530Sstevel@tonic-gate 	/*
26540Sstevel@tonic-gate 	 * For archives in USTAR format, the files are extracted according
26550Sstevel@tonic-gate 	 * to the typeflag.
26560Sstevel@tonic-gate 	 */
26570Sstevel@tonic-gate 	if (Hdr_type == USTAR || Hdr_type == TAR) {
26580Sstevel@tonic-gate 		typeflag = Thdr_p->tbuf.t_typeflag;
26590Sstevel@tonic-gate 		if (G_p->g_nlink == 1) {		/* hard link */
26600Sstevel@tonic-gate 			if (proc_file != F_SKIP) {
26610Sstevel@tonic-gate 			    int i;
26620Sstevel@tonic-gate 			    char lname[NAMSIZ+1];
26630Sstevel@tonic-gate 			    (void) memset(lname, '\0', sizeof (lname));
26640Sstevel@tonic-gate 
26650Sstevel@tonic-gate 			    (void) strncpy(lname, Thdr_p->tbuf.t_linkname,
26660Sstevel@tonic-gate 				NAMSIZ);
26670Sstevel@tonic-gate 			    for (i = 0; i <= NAMSIZ && lname[i] != 0; i++)
26680Sstevel@tonic-gate 				;
26690Sstevel@tonic-gate 
26700Sstevel@tonic-gate 			    lname[i] = 0;
26710Sstevel@tonic-gate 			    (void) creat_lnk(G_p->g_dirfd,
26720Sstevel@tonic-gate 				&lname[0], G_p->g_nam_p);
26730Sstevel@tonic-gate 			}
26740Sstevel@tonic-gate 			close_dirfd();
26750Sstevel@tonic-gate 			return;
26760Sstevel@tonic-gate 		}
26770Sstevel@tonic-gate 		if (typeflag == '3' || typeflag == '4' || typeflag == '5' ||
26780Sstevel@tonic-gate 		    typeflag == '6') {
26790Sstevel@tonic-gate 			if (proc_file != F_SKIP &&
26800Sstevel@tonic-gate 			    creat_spec(G_p->g_dirfd) > 0) {
26810Sstevel@tonic-gate 				VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
26820Sstevel@tonic-gate 			}
26830Sstevel@tonic-gate 			close_dirfd();
26840Sstevel@tonic-gate 			return;
26850Sstevel@tonic-gate 		} else if (Adir || Aspec) {
26860Sstevel@tonic-gate 			if ((proc_file == F_SKIP) ||
26870Sstevel@tonic-gate 					(Ofile = openout(G_p->g_dirfd)) < 0) {
26880Sstevel@tonic-gate 				data_in(P_SKIP);
26890Sstevel@tonic-gate 			} else {
26900Sstevel@tonic-gate 				data_in(P_PROC);
26910Sstevel@tonic-gate 			}
26920Sstevel@tonic-gate 			close_dirfd();
26930Sstevel@tonic-gate 			return;
26940Sstevel@tonic-gate 		}
26950Sstevel@tonic-gate 	}
26960Sstevel@tonic-gate 
26970Sstevel@tonic-gate 	if (Adir) {
26980Sstevel@tonic-gate 		if (proc_file != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
26990Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
27000Sstevel@tonic-gate 		}
27010Sstevel@tonic-gate 		close_dirfd();
27020Sstevel@tonic-gate 		if (Onecopy == 1) {
27030Sstevel@tonic-gate 			VERBOSE((Args & OCt), G_p->g_nam_p);
27040Sstevel@tonic-gate 		}
27050Sstevel@tonic-gate 		return;
27060Sstevel@tonic-gate 	}
27070Sstevel@tonic-gate 	if (G_p->g_nlink == 1 || (Hdr_type == TAR ||
27080Sstevel@tonic-gate 	    Hdr_type == USTAR)) {
27090Sstevel@tonic-gate 		if (Aspec) {
27100Sstevel@tonic-gate 			if (proc_file != F_SKIP && creat_spec(G_p->g_dirfd) > 0)
27110Sstevel@tonic-gate 				VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
27120Sstevel@tonic-gate 		} else {
27130Sstevel@tonic-gate 			if ((proc_file == F_SKIP) ||
27140Sstevel@tonic-gate 					(Ofile = openout(G_p->g_dirfd)) < 0) {
27150Sstevel@tonic-gate 				data_in(P_SKIP);
27160Sstevel@tonic-gate 			} else {
27170Sstevel@tonic-gate 				data_in(P_PROC);
27180Sstevel@tonic-gate 			}
27190Sstevel@tonic-gate 		}
27200Sstevel@tonic-gate 		close_dirfd();
27210Sstevel@tonic-gate 		return;
27220Sstevel@tonic-gate 	}
27230Sstevel@tonic-gate 	close_dirfd();
27240Sstevel@tonic-gate 
27250Sstevel@tonic-gate 	tl_p = add_lnk(&ttl_p);
27260Sstevel@tonic-gate 	l_p = ttl_p;
27270Sstevel@tonic-gate 	if (l_p->L_cnt == l_p->L_gen.g_nlink)
27280Sstevel@tonic-gate 		cleanup = 1;
27290Sstevel@tonic-gate 	if (!Onecopy || G_p->g_attrnam_p != (char *)NULL) {
27300Sstevel@tonic-gate 		lnkem = (tl_p != l_p) ? 1 : 0;
27310Sstevel@tonic-gate 		G_p = &tl_p->L_gen;
27320Sstevel@tonic-gate 		if (proc_file == F_SKIP) {
27330Sstevel@tonic-gate 			data_in(P_SKIP);
27340Sstevel@tonic-gate 		} else {
27350Sstevel@tonic-gate 			if (open_dirfd() != 0)
27360Sstevel@tonic-gate 				return;
27370Sstevel@tonic-gate 			if (!lnkem) {
27380Sstevel@tonic-gate 				if (Aspec) {
27390Sstevel@tonic-gate 					if (creat_spec(G_p->g_dirfd) > 0)
27400Sstevel@tonic-gate 						VERBOSE((Args & (OCv | OCV)),
27410Sstevel@tonic-gate 						    G_p->g_nam_p);
27420Sstevel@tonic-gate 				} else if ((Ofile =
27430Sstevel@tonic-gate 						openout(G_p->g_dirfd)) < 0) {
27440Sstevel@tonic-gate 					data_in(P_SKIP);
27450Sstevel@tonic-gate 					close_dirfd();
27460Sstevel@tonic-gate 					reclaim(l_p);
27470Sstevel@tonic-gate 				} else {
27480Sstevel@tonic-gate 					data_in(P_PROC);
27490Sstevel@tonic-gate 					close_dirfd();
27500Sstevel@tonic-gate 				}
27510Sstevel@tonic-gate 			} else {
27520Sstevel@tonic-gate 				/*
27530Sstevel@tonic-gate 				 * Are we linking an attribute?
27540Sstevel@tonic-gate 				 */
27550Sstevel@tonic-gate 				cwd = -1;
27560Sstevel@tonic-gate 				if (l_p->L_gen.g_attrnam_p != (char *)NULL) {
27570Sstevel@tonic-gate 					(void) strcpy(Lnkend_p,
27580Sstevel@tonic-gate 					    l_p->L_gen.g_attrnam_p);
27590Sstevel@tonic-gate 					(void) strcpy(Full_p,
27600Sstevel@tonic-gate 					    tl_p->L_gen.g_attrnam_p);
27610Sstevel@tonic-gate 					cwd = save_cwd();
27620Sstevel@tonic-gate 					(void) fchdir(G_p->g_dirfd);
27630Sstevel@tonic-gate 				} else {
27640Sstevel@tonic-gate 					(void) strcpy(Lnkend_p,
27650Sstevel@tonic-gate 					    l_p->L_gen.g_nam_p);
27660Sstevel@tonic-gate 					(void) strcpy(Full_p,
27670Sstevel@tonic-gate 					    tl_p->L_gen.g_nam_p);
27680Sstevel@tonic-gate 				}
27690Sstevel@tonic-gate 				(void) creat_lnk(G_p->g_dirfd,
27700Sstevel@tonic-gate 				    Lnkend_p, Full_p);
27710Sstevel@tonic-gate 				data_in(P_SKIP);
27720Sstevel@tonic-gate 				close_dirfd();
27730Sstevel@tonic-gate 				l_p->L_lnk_p = (struct Lnk *)NULL;
27740Sstevel@tonic-gate 				free(tl_p->L_gen.g_nam_p);
27750Sstevel@tonic-gate 				free(tl_p);
27760Sstevel@tonic-gate 				if (cwd != -1)
27770Sstevel@tonic-gate 					rest_cwd(cwd);
27780Sstevel@tonic-gate 			}
27790Sstevel@tonic-gate 		}
27800Sstevel@tonic-gate 	} else { /* Onecopy */
27810Sstevel@tonic-gate 		if (tl_p->L_gen.g_filesz)
27820Sstevel@tonic-gate 			cleanup = 1;
27830Sstevel@tonic-gate 		if (!cleanup) {
27840Sstevel@tonic-gate 			close_dirfd();
27850Sstevel@tonic-gate 			return; /* don't do anything yet */
27860Sstevel@tonic-gate 		}
27870Sstevel@tonic-gate 		tl_p = l_p;
27880Sstevel@tonic-gate 		/*
27890Sstevel@tonic-gate 		 * ckname will clear aclchar. We need to keep aclchar for
27900Sstevel@tonic-gate 		 * all links.
27910Sstevel@tonic-gate 		 */
27920Sstevel@tonic-gate 		savacl = aclchar;
27930Sstevel@tonic-gate 		while (tl_p != (struct Lnk *)NULL) {
27940Sstevel@tonic-gate 			G_p = &tl_p->L_gen;
27950Sstevel@tonic-gate 			aclchar = savacl;
27960Sstevel@tonic-gate 			if ((proc_file = ckname(1)) != F_SKIP) {
27970Sstevel@tonic-gate 				if (open_dirfd() != 0) {
27980Sstevel@tonic-gate 					return;
27990Sstevel@tonic-gate 				}
28000Sstevel@tonic-gate 				if (l_p->L_data) {
28010Sstevel@tonic-gate 					(void) creat_lnk(G_p->g_dirfd,
28020Sstevel@tonic-gate 					    l_p->L_gen.g_nam_p,
28030Sstevel@tonic-gate 					    G_p->g_nam_p);
28040Sstevel@tonic-gate 				} else if (Aspec) {
28050Sstevel@tonic-gate 					(void) creat_spec(G_p->g_dirfd);
28060Sstevel@tonic-gate 					l_p->L_data = 1;
28070Sstevel@tonic-gate 					VERBOSE((Args & (OCv | OCV)),
28080Sstevel@tonic-gate 					    G_p->g_nam_p);
28090Sstevel@tonic-gate 				} else if ((Ofile =
28100Sstevel@tonic-gate 				    openout(G_p->g_dirfd)) < 0) {
28110Sstevel@tonic-gate 					proc_file = F_SKIP;
28120Sstevel@tonic-gate 				} else {
28130Sstevel@tonic-gate 					data_in(P_PROC);
28140Sstevel@tonic-gate 					l_p->L_data = 1;
28150Sstevel@tonic-gate 				}
28160Sstevel@tonic-gate 			} /* (proc_file = ckname(1)) != F_SKIP */
28170Sstevel@tonic-gate 
28180Sstevel@tonic-gate 			tl_p = tl_p->L_lnk_p;
28190Sstevel@tonic-gate 
28200Sstevel@tonic-gate 			close_dirfd();
28210Sstevel@tonic-gate 
28220Sstevel@tonic-gate 			if (proc_file == F_SKIP && !cleanup) {
28230Sstevel@tonic-gate 				tl_p->L_nxt_p = l_p->L_nxt_p;
28240Sstevel@tonic-gate 				tl_p->L_bck_p = l_p->L_bck_p;
28250Sstevel@tonic-gate 				l_p->L_bck_p->L_nxt_p = tl_p;
28260Sstevel@tonic-gate 				l_p->L_nxt_p->L_bck_p = tl_p;
28270Sstevel@tonic-gate 				free(l_p->L_gen.g_nam_p);
28280Sstevel@tonic-gate 				free(l_p);
28290Sstevel@tonic-gate 			}
28300Sstevel@tonic-gate 		} /* tl_p->L_lnk_p != (struct Lnk *)NULL */
28310Sstevel@tonic-gate 		if (l_p->L_data == 0) {
28320Sstevel@tonic-gate 			data_in(P_SKIP);
28330Sstevel@tonic-gate 		}
28340Sstevel@tonic-gate 	}
28350Sstevel@tonic-gate 	if (cleanup) {
28360Sstevel@tonic-gate 		reclaim(l_p);
28370Sstevel@tonic-gate 	}
28380Sstevel@tonic-gate }
28390Sstevel@tonic-gate 
28400Sstevel@tonic-gate /*
28410Sstevel@tonic-gate  * file_out:  If the current file is not a special file (!Aspec) and it
28420Sstevel@tonic-gate  * is identical to the archive, skip it (do not archive the archive if it
28430Sstevel@tonic-gate  * is a regular file).  If creating a TARTYP (TAR or USTAR) archive, the first
28440Sstevel@tonic-gate  * time a link to a file is encountered, write the header and file out normally.
28450Sstevel@tonic-gate  * Subsequent links to this file put this file name in their t_linkname field.
28460Sstevel@tonic-gate  * Otherwise, links are handled in one of two ways, for the old headers
28470Sstevel@tonic-gate  * (i.e. binary and -c), linked files are written out as they are encountered.
28480Sstevel@tonic-gate  * For the new headers (ASC and CRC), links are saved up until all the links
28490Sstevel@tonic-gate  * to each file are found.  For a file with n links, write n - 1 headers with
28500Sstevel@tonic-gate  * g_filesz set to 0, write the final (nth) header with the correct g_filesz
28510Sstevel@tonic-gate  * value and write the data for the file to the archive.
28520Sstevel@tonic-gate  */
28530Sstevel@tonic-gate 
28540Sstevel@tonic-gate static
28550Sstevel@tonic-gate int
28560Sstevel@tonic-gate file_out(void)
28570Sstevel@tonic-gate {
28580Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
28590Sstevel@tonic-gate 	int cleanup = 0;
28600Sstevel@tonic-gate 	struct Lnk *ttl_p;
28610Sstevel@tonic-gate 
28620Sstevel@tonic-gate 	G_p = &Gen;
28630Sstevel@tonic-gate 	if (!Aspec && IDENT(SrcSt, ArchSt))
28640Sstevel@tonic-gate 		return (1); /* do not archive the archive if it's a reg file */
28650Sstevel@tonic-gate 	if (G_p->g_filesz > Max_offset) {
28660Sstevel@tonic-gate 		msg(ERR, "cpio: %s%s%s: too large to archive in current mode",
28670Sstevel@tonic-gate 		    G_p->g_nam_p,
28680Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
28690Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
28700Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
28710Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p);
28720Sstevel@tonic-gate 		return (1); /* do not archive if it's too big */
28730Sstevel@tonic-gate 	}
28740Sstevel@tonic-gate 	if (Hdr_type == TAR || Hdr_type == USTAR) { /* TAR and USTAR */
28750Sstevel@tonic-gate 		if (Adir) {
28760Sstevel@tonic-gate 			if (Gen.g_attrnam_p != (char *)NULL) {
28770Sstevel@tonic-gate 				write_xattr_hdr();
28780Sstevel@tonic-gate 			}
28790Sstevel@tonic-gate 			write_hdr(ARCHIVE_NORMAL, 0);
28800Sstevel@tonic-gate 			return (0);
28810Sstevel@tonic-gate 		}
28820Sstevel@tonic-gate 		if (G_p->g_nlink == 1) {
28830Sstevel@tonic-gate 			data_out();
28840Sstevel@tonic-gate 			return (0);
28850Sstevel@tonic-gate 		}
28860Sstevel@tonic-gate 		tl_p = add_lnk(&ttl_p);
28870Sstevel@tonic-gate 		l_p = ttl_p;
28880Sstevel@tonic-gate 		if (tl_p == l_p) { /* first link to this file encountered */
28890Sstevel@tonic-gate 			data_out();
28900Sstevel@tonic-gate 			return (0);
28910Sstevel@tonic-gate 		}
28920Sstevel@tonic-gate 		(void) strncpy(T_lname, l_p->L_gen.g_nam_p,
28930Sstevel@tonic-gate 		    l_p->L_gen.g_namesz);
28940Sstevel@tonic-gate 
28950Sstevel@tonic-gate 		/*
28960Sstevel@tonic-gate 		 * check if linkname is greater than 100 characters
28970Sstevel@tonic-gate 		 */
28980Sstevel@tonic-gate 		if (strlen(T_lname) > NAMSIZ) {
28990Sstevel@tonic-gate 			msg(EPOST, "cpio: %s: linkname %s is greater than %d",
29000Sstevel@tonic-gate 			    G_p->g_nam_p, T_lname, NAMSIZ);
29010Sstevel@tonic-gate 			return (1);
29020Sstevel@tonic-gate 		}
29030Sstevel@tonic-gate 
29040Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
29050Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), tl_p->L_gen.g_nam_p);
29060Sstevel@tonic-gate 
29070Sstevel@tonic-gate 		/* find the lnk entry in sublist, unlink it, and free it */
29080Sstevel@tonic-gate 		for (; ttl_p->L_lnk_p != NULL;
29090Sstevel@tonic-gate 		    ttl_p = ttl_p->L_lnk_p) {
29100Sstevel@tonic-gate 			if (ttl_p->L_lnk_p == tl_p) {
29110Sstevel@tonic-gate 				ttl_p->L_lnk_p = tl_p->L_lnk_p;
29120Sstevel@tonic-gate 				free(tl_p->L_gen.g_nam_p);
29130Sstevel@tonic-gate 				free(tl_p);
29140Sstevel@tonic-gate 				break;
29150Sstevel@tonic-gate 			}
29160Sstevel@tonic-gate 		}
29170Sstevel@tonic-gate 
29180Sstevel@tonic-gate 		return (0);
29190Sstevel@tonic-gate 	}
29200Sstevel@tonic-gate 	if (Adir) {
29210Sstevel@tonic-gate 		/*
29220Sstevel@tonic-gate 		 * ACL has been retrieved in getname().
29230Sstevel@tonic-gate 		 */
29240Sstevel@tonic-gate 		if (Pflag) {
29250Sstevel@tonic-gate 			char    *secinfo = NULL;
29260Sstevel@tonic-gate 			int	len = 0;
29270Sstevel@tonic-gate 
29280Sstevel@tonic-gate 			/* append security attributes */
29290Sstevel@tonic-gate 			if ((append_secattr(&secinfo, &len, aclcnt,
29300Sstevel@tonic-gate 			    (char *)aclp, UFSD_ACL)) == -1)
29310Sstevel@tonic-gate 				msg(ERR, "can create security information");
29320Sstevel@tonic-gate 
29330Sstevel@tonic-gate 			/* call append_secattr() if more than one */
29340Sstevel@tonic-gate 
29350Sstevel@tonic-gate 			if (len > 0) {
29360Sstevel@tonic-gate 			/* write ancillary */
29370Sstevel@tonic-gate 				(void) write_hdr(ARCHIVE_ACL, (off_t)len);
29380Sstevel@tonic-gate 				(void) write_ancillary(secinfo, len);
29390Sstevel@tonic-gate 			}
29400Sstevel@tonic-gate 		}
29410Sstevel@tonic-gate 
29420Sstevel@tonic-gate 		if (Gen.g_attrnam_p != (char *)NULL) {
29430Sstevel@tonic-gate 			write_xattr_hdr();
29440Sstevel@tonic-gate 		}
29450Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
29460Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
29470Sstevel@tonic-gate 		return (0);
29480Sstevel@tonic-gate 	}
29490Sstevel@tonic-gate 	if (G_p->g_nlink == 1) {
29500Sstevel@tonic-gate 		data_out();
29510Sstevel@tonic-gate 		return (0);
29520Sstevel@tonic-gate 	} else {
29530Sstevel@tonic-gate 		tl_p = add_lnk(&ttl_p);
29540Sstevel@tonic-gate 		l_p = ttl_p;
29550Sstevel@tonic-gate 
29560Sstevel@tonic-gate 		if (l_p->L_cnt == l_p->L_gen.g_nlink)
29570Sstevel@tonic-gate 			cleanup = 1;
29580Sstevel@tonic-gate 		else if (Onecopy && G_p->g_attrnam_p == (char *)NULL) {
29590Sstevel@tonic-gate 			return (0); /* don't process data yet */
29600Sstevel@tonic-gate 		}
29610Sstevel@tonic-gate 	}
29620Sstevel@tonic-gate 	if (Onecopy && G_p->g_attrnam_p == (char *)NULL) {
29630Sstevel@tonic-gate 		tl_p = l_p;
29640Sstevel@tonic-gate 		while (tl_p->L_lnk_p != (struct Lnk *)NULL) {
29650Sstevel@tonic-gate 			G_p = &tl_p->L_gen;
29660Sstevel@tonic-gate 			G_p->g_filesz = (off_t)0;
29670Sstevel@tonic-gate 			/* one link with the acl is sufficient */
29680Sstevel@tonic-gate 			write_hdr(ARCHIVE_NORMAL, (off_t)0);
29690Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
29700Sstevel@tonic-gate 			tl_p = tl_p->L_lnk_p;
29710Sstevel@tonic-gate 		}
29720Sstevel@tonic-gate 		G_p = &tl_p->L_gen;
29730Sstevel@tonic-gate 		if (open_dirfd() != 0)
29740Sstevel@tonic-gate 			return (1);
29750Sstevel@tonic-gate 	}
29760Sstevel@tonic-gate 	/* old style: has acl and data for every link */
29770Sstevel@tonic-gate 	data_out();
29780Sstevel@tonic-gate 	if (cleanup)
29790Sstevel@tonic-gate 		reclaim(l_p);
29800Sstevel@tonic-gate 	return (0);
29810Sstevel@tonic-gate }
29820Sstevel@tonic-gate 
29830Sstevel@tonic-gate /*
29840Sstevel@tonic-gate  * file_pass:  If the -l option is set (link files when possible), and the
29850Sstevel@tonic-gate  * source and destination file systems are the same, link the source file
29860Sstevel@tonic-gate  * (G_p->g_nam_p) to the destination file (Fullnam) and return.  If not a
29870Sstevel@tonic-gate  * linked file, transfer the data.  Otherwise, the first link to a file
29880Sstevel@tonic-gate  * encountered is transferred normally and subsequent links are linked to it.
29890Sstevel@tonic-gate  */
29900Sstevel@tonic-gate 
29910Sstevel@tonic-gate static int
29920Sstevel@tonic-gate file_pass(void)
29930Sstevel@tonic-gate {
29940Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
29950Sstevel@tonic-gate 	struct Lnk *ttl_p;
29960Sstevel@tonic-gate 	char *save_name;
29970Sstevel@tonic-gate 	int size;
29980Sstevel@tonic-gate 	int cwd;
29990Sstevel@tonic-gate 	char *lfrom, *lto;
30000Sstevel@tonic-gate 
30010Sstevel@tonic-gate 	G_p = &Gen;
30020Sstevel@tonic-gate 
30030Sstevel@tonic-gate 	if (Adir && !(Args & OCd)) {
30040Sstevel@tonic-gate 		msg(ERR, "Use -d option to copy \"%s\"", G_p->g_nam_p);
30050Sstevel@tonic-gate 		return (FILE_PASS_ERR);
30060Sstevel@tonic-gate 	}
30070Sstevel@tonic-gate 
30080Sstevel@tonic-gate 	save_name = G_p->g_nam_p;
30090Sstevel@tonic-gate 
30100Sstevel@tonic-gate 	while (*(G_p->g_nam_p) == '/') {
30110Sstevel@tonic-gate 		G_p->g_nam_p++;
30120Sstevel@tonic-gate 	}
30130Sstevel@tonic-gate 
30140Sstevel@tonic-gate 	(void) strcpy(Full_p,
30150Sstevel@tonic-gate 	    (G_p->g_attrfnam_p == (char *)NULL) ?
30160Sstevel@tonic-gate 	    G_p->g_nam_p : G_p->g_attrfnam_p);
30170Sstevel@tonic-gate 
30180Sstevel@tonic-gate 	if (G_p->g_attrnam_p == (char *)NULL) {
30190Sstevel@tonic-gate 		G_p->g_passdirfd = open_dir(Fullnam_p);
30200Sstevel@tonic-gate 
30210Sstevel@tonic-gate 		if (G_p->g_passdirfd == -1) {
30220Sstevel@tonic-gate 			msg(ERRN,
30230Sstevel@tonic-gate 			    "Cannot open/create \"%s\"", Fullnam_p);
30240Sstevel@tonic-gate 			return (FILE_PASS_ERR);
30250Sstevel@tonic-gate 		}
30260Sstevel@tonic-gate 	} else {
30270Sstevel@tonic-gate 		G_p->g_passdirfd = attropen(Fullnam_p, ".", O_RDONLY);
30280Sstevel@tonic-gate 
30290Sstevel@tonic-gate 		if (G_p->g_passdirfd == -1) {
30300Sstevel@tonic-gate 			G_p->g_passdirfd = retry_attrdir_open(Fullnam_p);
30310Sstevel@tonic-gate 
30320Sstevel@tonic-gate 			if (G_p->g_passdirfd == -1) {
30330Sstevel@tonic-gate 				msg(ERRN,
30340Sstevel@tonic-gate 				    "Cannot open attribute directory of"
30350Sstevel@tonic-gate 				    " file \"%s\"", Fullnam_p);
30360Sstevel@tonic-gate 				return (FILE_PASS_ERR);
30370Sstevel@tonic-gate 			}
30380Sstevel@tonic-gate 		}
30390Sstevel@tonic-gate 	}
30400Sstevel@tonic-gate 
30410Sstevel@tonic-gate 	if (Args & OCl) {
30420Sstevel@tonic-gate 		/* We are linking back to the source directory. */
30430Sstevel@tonic-gate 
30440Sstevel@tonic-gate 		if (!Adir) {
30450Sstevel@tonic-gate 			char *existingfile = save_name;
30460Sstevel@tonic-gate 
30470Sstevel@tonic-gate 			if ((Args & OCL) && issymlink) {
30480Sstevel@tonic-gate 				/* We are chasing symlinks. */
30490Sstevel@tonic-gate 
30500Sstevel@tonic-gate 				if ((size = readlink(save_name, Symlnk_p,
30510Sstevel@tonic-gate 				    MAXPATHLEN)) < 0) {
30520Sstevel@tonic-gate 					msg(ERRN,
30530Sstevel@tonic-gate 					    "Cannot read symbolic link \"%s\"",
30540Sstevel@tonic-gate 					    save_name);
30550Sstevel@tonic-gate 					return (FILE_PASS_ERR);
30560Sstevel@tonic-gate 				}
30570Sstevel@tonic-gate 
30580Sstevel@tonic-gate 				Symlnk_p[size] = '\0';
30590Sstevel@tonic-gate 				existingfile = Symlnk_p;
30600Sstevel@tonic-gate 			}
30610Sstevel@tonic-gate 
30620Sstevel@tonic-gate 			if (G_p->g_attrnam_p == (char *)NULL) {
30630Sstevel@tonic-gate 				if (creat_lnk(G_p->g_passdirfd,
30640Sstevel@tonic-gate 				    existingfile, Fullnam_p) == 0) {
30650Sstevel@tonic-gate 					return (FILE_LINKED);
30660Sstevel@tonic-gate 				}
30670Sstevel@tonic-gate 			}
30680Sstevel@tonic-gate 		}
30690Sstevel@tonic-gate 	}
30700Sstevel@tonic-gate 
30710Sstevel@tonic-gate 	if ((G_p->g_mode & Ftype) == S_IFLNK && !(Args & OCL)) {
30720Sstevel@tonic-gate 		/* The archive file is a symlink. */
30730Sstevel@tonic-gate 
30740Sstevel@tonic-gate 		errno = 0;
30750Sstevel@tonic-gate 
30760Sstevel@tonic-gate 		if ((size = readlink(save_name, Symlnk_p, MAXPATHLEN)) < 0) {
30770Sstevel@tonic-gate 			msg(ERRN,
30780Sstevel@tonic-gate 			    "Cannot read symbolic link \"%s\"", save_name);
30790Sstevel@tonic-gate 			return (FILE_PASS_ERR);
30800Sstevel@tonic-gate 		}
30810Sstevel@tonic-gate 
30820Sstevel@tonic-gate 		errno = 0;
30830Sstevel@tonic-gate 		(void) missdir(Fullnam_p);
30840Sstevel@tonic-gate 		*(Symlnk_p + size) = '\0';
30850Sstevel@tonic-gate 
30860Sstevel@tonic-gate 		if (symlink(Symlnk_p, Fullnam_p) < 0) {
30870Sstevel@tonic-gate 			if (errno == EEXIST) {
30880Sstevel@tonic-gate 				if (openout(G_p->g_passdirfd) < 0) {
30890Sstevel@tonic-gate 					return (FILE_PASS_ERR);
30900Sstevel@tonic-gate 				}
30910Sstevel@tonic-gate 			} else {
30920Sstevel@tonic-gate 				msg(ERRN, "Cannot create \"%s\"", Fullnam_p);
30930Sstevel@tonic-gate 				return (FILE_PASS_ERR);
30940Sstevel@tonic-gate 			}
30950Sstevel@tonic-gate 		}
30960Sstevel@tonic-gate 
30970Sstevel@tonic-gate 		if (lchown(Fullnam_p, (int)G_p->g_uid, (int)G_p->g_gid) < 0) {
30980Sstevel@tonic-gate 			if ((Euid == 0) || (Args & OCR)) {
30990Sstevel@tonic-gate 				msg(ERRN,
31000Sstevel@tonic-gate 				    "Error during chown() of \"%s\"",
31010Sstevel@tonic-gate 				    Fullnam_p);
31020Sstevel@tonic-gate 			}
31030Sstevel@tonic-gate 		}
31040Sstevel@tonic-gate 
31050Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), Fullnam_p);
31060Sstevel@tonic-gate 		return (FILE_PASS_ERR);
31070Sstevel@tonic-gate 	}
31080Sstevel@tonic-gate 
31090Sstevel@tonic-gate 	if (!Adir && G_p->g_nlink > 1) {
31100Sstevel@tonic-gate 		/* The archive file has hard links. */
31110Sstevel@tonic-gate 
31120Sstevel@tonic-gate 		tl_p = add_lnk(&ttl_p);
31130Sstevel@tonic-gate 		l_p = ttl_p;
31140Sstevel@tonic-gate 
31150Sstevel@tonic-gate 		if (tl_p == l_p) {
31160Sstevel@tonic-gate 			/* The archive file was not found. */
31170Sstevel@tonic-gate 
31180Sstevel@tonic-gate 			G_p = &tl_p->L_gen;
31190Sstevel@tonic-gate 		} else {
31200Sstevel@tonic-gate 			/* The archive file was found. */
31210Sstevel@tonic-gate 
31220Sstevel@tonic-gate 			cwd = -1;
31230Sstevel@tonic-gate 
31240Sstevel@tonic-gate 			if (l_p->L_gen.g_attrnam_p != (char *)NULL) {
31250Sstevel@tonic-gate 				/* We are linking an attribute */
31260Sstevel@tonic-gate 
31270Sstevel@tonic-gate 				(void) strcpy(Lnkend_p, l_p->L_gen.g_attrnam_p);
31280Sstevel@tonic-gate 				cwd = save_cwd();
31290Sstevel@tonic-gate 				(void) fchdir(G_p->g_passdirfd);
31300Sstevel@tonic-gate 				lfrom = get_component(Lnknam_p);
31310Sstevel@tonic-gate 				lto = tl_p->L_gen.g_attrnam_p;
31320Sstevel@tonic-gate 			} else {
31330Sstevel@tonic-gate 				/* We are not linking an attribute */
31340Sstevel@tonic-gate 
31350Sstevel@tonic-gate 				(void) strcpy(Lnkend_p, l_p->L_gen.g_nam_p);
31360Sstevel@tonic-gate 				(void) strcpy(Full_p, tl_p->L_gen.g_nam_p);
31370Sstevel@tonic-gate 				lfrom = Lnknam_p;
31380Sstevel@tonic-gate 				lto = Fullnam_p;
31390Sstevel@tonic-gate 			}
31400Sstevel@tonic-gate 
31410Sstevel@tonic-gate 			(void) creat_lnk(G_p->g_passdirfd, lfrom, lto);
31420Sstevel@tonic-gate 
31430Sstevel@tonic-gate 			if (cwd) {
31440Sstevel@tonic-gate 				rest_cwd(cwd);
31450Sstevel@tonic-gate 			}
31460Sstevel@tonic-gate 
31470Sstevel@tonic-gate 			l_p->L_lnk_p = (struct Lnk *)NULL;
31480Sstevel@tonic-gate 			free(tl_p->L_gen.g_nam_p);
31490Sstevel@tonic-gate 			free(tl_p);
31500Sstevel@tonic-gate 
31510Sstevel@tonic-gate 			if (l_p->L_cnt == G_p->g_nlink) {
31520Sstevel@tonic-gate 				reclaim(l_p);
31530Sstevel@tonic-gate 			}
31540Sstevel@tonic-gate 
31550Sstevel@tonic-gate 			return (FILE_LINKED);
31560Sstevel@tonic-gate 		}
31570Sstevel@tonic-gate 	}
31580Sstevel@tonic-gate 
31590Sstevel@tonic-gate 	if (Adir || Aspec) {
31600Sstevel@tonic-gate 		/*
31610Sstevel@tonic-gate 		 * The archive file is a directory,  block special, char
31620Sstevel@tonic-gate 		 * special or a fifo.
31630Sstevel@tonic-gate 		 */
31640Sstevel@tonic-gate 
31650Sstevel@tonic-gate 		if (creat_spec(G_p->g_passdirfd) > 0) {
31660Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), Fullnam_p);
31670Sstevel@tonic-gate 		}
31680Sstevel@tonic-gate 	} else if ((Ofile = openout(G_p->g_passdirfd)) > 0) {
31690Sstevel@tonic-gate 		data_pass();
31700Sstevel@tonic-gate 	}
31710Sstevel@tonic-gate 
31720Sstevel@tonic-gate 	return (FILE_COPIED);
31730Sstevel@tonic-gate }
31740Sstevel@tonic-gate 
31750Sstevel@tonic-gate /*
31760Sstevel@tonic-gate  * flush_lnks: With new linked file handling, linked files are not archived
31770Sstevel@tonic-gate  * until all links have been collected.  When the end of the list of filenames
31780Sstevel@tonic-gate  * to archive has been reached, all files that did not encounter all their links
31790Sstevel@tonic-gate  * are written out with actual (encountered) link counts.  A file with n links
31800Sstevel@tonic-gate  * (that are archived) will be represented by n headers (one for each link (the
31810Sstevel@tonic-gate  * first n - 1 have g_filesz set to 0)) followed by the data for the file.
31820Sstevel@tonic-gate  */
31830Sstevel@tonic-gate 
31840Sstevel@tonic-gate static void
31850Sstevel@tonic-gate flush_lnks(void)
31860Sstevel@tonic-gate {
31870Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
31880Sstevel@tonic-gate 	off_t tfsize;
31890Sstevel@tonic-gate 
31900Sstevel@tonic-gate 	l_p = Lnk_hd.L_nxt_p;
31910Sstevel@tonic-gate 	while (l_p != &Lnk_hd) {
31920Sstevel@tonic-gate 		(void) strcpy(Gen.g_nam_p, l_p->L_gen.g_nam_p);
31930Sstevel@tonic-gate 		if (stat(Gen.g_nam_p, &SrcSt) == 0) { /* check if file exists */
31940Sstevel@tonic-gate 			tl_p = l_p;
31950Sstevel@tonic-gate 			(void) creat_hdr();
31960Sstevel@tonic-gate 			Gen.g_nlink = l_p->L_cnt; /* "actual" link count */
31970Sstevel@tonic-gate 			tfsize = Gen.g_filesz;
31980Sstevel@tonic-gate 			Gen.g_filesz = (off_t)0;
31990Sstevel@tonic-gate 			G_p = &Gen;
32000Sstevel@tonic-gate 			while (tl_p != (struct Lnk *)NULL) {
32010Sstevel@tonic-gate 				Gen.g_nam_p = tl_p->L_gen.g_nam_p;
32020Sstevel@tonic-gate 				Gen.g_namesz = tl_p->L_gen.g_namesz;
32030Sstevel@tonic-gate 				if (tl_p->L_lnk_p == (struct Lnk *)NULL) {
32040Sstevel@tonic-gate 					Gen.g_filesz = tfsize;
32050Sstevel@tonic-gate 					if (open_dirfd() != 0) {
32060Sstevel@tonic-gate 						break;
32070Sstevel@tonic-gate 					}
32080Sstevel@tonic-gate 					data_out();
32090Sstevel@tonic-gate 					break;
32100Sstevel@tonic-gate 				}
32110Sstevel@tonic-gate 				write_hdr(ARCHIVE_NORMAL,
32120Sstevel@tonic-gate 				    (off_t)0); /* header only */
32130Sstevel@tonic-gate 				VERBOSE((Args & (OCv | OCV)), Gen.g_nam_p);
32140Sstevel@tonic-gate 				tl_p = tl_p->L_lnk_p;
32150Sstevel@tonic-gate 			}
32160Sstevel@tonic-gate 			Gen.g_nam_p = Nam_p;
32170Sstevel@tonic-gate 		} else /* stat(Gen.g_nam_p, &SrcSt) == 0 */
32180Sstevel@tonic-gate 			msg(ERR, "\"%s%s%s\" has disappeared",
32190Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
32200Sstevel@tonic-gate 			    Gen.g_nam_p : Gen.g_attrfnam_p,
32210Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
32220Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
32230Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
32240Sstevel@tonic-gate 			    "" : Gen.g_attrnam_p);
32250Sstevel@tonic-gate 		tl_p = l_p;
32260Sstevel@tonic-gate 		l_p = l_p->L_nxt_p;
32270Sstevel@tonic-gate 		reclaim(tl_p);
32280Sstevel@tonic-gate 	} /* l_p != &Lnk_hd */
32290Sstevel@tonic-gate }
32300Sstevel@tonic-gate 
32310Sstevel@tonic-gate /*
32320Sstevel@tonic-gate  * gethdr: Get a header from the archive, validate it and check for the trailer.
32330Sstevel@tonic-gate  * Any user specified Hdr_type is ignored (set to NONE in main).  Hdr_type is
32340Sstevel@tonic-gate  * set appropriately after a valid header is found.  Unless the -k option is
32350Sstevel@tonic-gate  * set a corrupted header causes an exit with an error.  I/O errors during
32360Sstevel@tonic-gate  * examination of any part of the header cause gethdr to throw away any current
32370Sstevel@tonic-gate  * data and start over.  Other errors during examination of any part of the
32380Sstevel@tonic-gate  * header cause gethdr to advance one byte and continue the examination.
32390Sstevel@tonic-gate  */
32400Sstevel@tonic-gate 
32410Sstevel@tonic-gate static int
32420Sstevel@tonic-gate gethdr(void)
32430Sstevel@tonic-gate {
32440Sstevel@tonic-gate 	ushort_t ftype;
32450Sstevel@tonic-gate 	int hit = NONE, cnt = 0;
32460Sstevel@tonic-gate 	int goodhdr, hsize, offset;
32470Sstevel@tonic-gate 	int bswap = 0;
32480Sstevel@tonic-gate 	char *preptr;
32490Sstevel@tonic-gate 	int k = 0;
32500Sstevel@tonic-gate 	int j;
32510Sstevel@tonic-gate 
32520Sstevel@tonic-gate 	Gen.g_nam_p = Nam_p;
32530Sstevel@tonic-gate 	do { /* hit == NONE && (Args & OCk) && Buffr.b_cnt > 0 */
32540Sstevel@tonic-gate 		FILL(Hdrsz);
32550Sstevel@tonic-gate 		switch (Hdr_type) {
32560Sstevel@tonic-gate 		case NONE:
32570Sstevel@tonic-gate 		case BIN:
32580Sstevel@tonic-gate 			Binmag.b_byte[0] = Buffr.b_out_p[0];
32590Sstevel@tonic-gate 			Binmag.b_byte[1] = Buffr.b_out_p[1];
32600Sstevel@tonic-gate 			if ((Binmag.b_half == CMN_BIN) ||
32610Sstevel@tonic-gate 			    (Binmag.b_half == CMN_BBS)) {
32620Sstevel@tonic-gate 				hit = read_hdr(BIN);
32630Sstevel@tonic-gate 				if (Hdr_type == NONE)
32640Sstevel@tonic-gate 					bswap = 1;
32650Sstevel@tonic-gate 				hsize = HDRSZ + Gen.g_namesz;
32660Sstevel@tonic-gate 				break;
32670Sstevel@tonic-gate 			}
32680Sstevel@tonic-gate 			if (Hdr_type != NONE)
32690Sstevel@tonic-gate 				break;
32700Sstevel@tonic-gate 			/*FALLTHROUGH*/
32710Sstevel@tonic-gate 		case CHR:
32720Sstevel@tonic-gate 			if (!(strncmp(Buffr.b_out_p, CMS_CHR, CMS_LEN))) {
32730Sstevel@tonic-gate 				hit = read_hdr(CHR);
32740Sstevel@tonic-gate 				hsize = CHRSZ + Gen.g_namesz;
32750Sstevel@tonic-gate 				break;
32760Sstevel@tonic-gate 			}
32770Sstevel@tonic-gate 			if (Hdr_type != NONE)
32780Sstevel@tonic-gate 				break;
32790Sstevel@tonic-gate 			/*FALLTHROUGH*/
32800Sstevel@tonic-gate 		case ASC:
32810Sstevel@tonic-gate 			if (!(strncmp(Buffr.b_out_p, CMS_ASC, CMS_LEN))) {
32820Sstevel@tonic-gate 				hit = read_hdr(ASC);
32830Sstevel@tonic-gate 				hsize = ASCSZ + Gen.g_namesz;
32840Sstevel@tonic-gate 				Max_namesz = APATH;
32850Sstevel@tonic-gate 				break;
32860Sstevel@tonic-gate 			}
32870Sstevel@tonic-gate 			if (Hdr_type != NONE)
32880Sstevel@tonic-gate 				break;
32890Sstevel@tonic-gate 			/*FALLTHROUGH*/
32900Sstevel@tonic-gate 		case CRC:
32910Sstevel@tonic-gate 			if (!(strncmp(Buffr.b_out_p, CMS_CRC, CMS_LEN))) {
32920Sstevel@tonic-gate 				hit = read_hdr(CRC);
32930Sstevel@tonic-gate 				hsize = ASCSZ + Gen.g_namesz;
32940Sstevel@tonic-gate 				Max_namesz = APATH;
32950Sstevel@tonic-gate 				break;
32960Sstevel@tonic-gate 			}
32970Sstevel@tonic-gate 			if (Hdr_type != NONE)
32980Sstevel@tonic-gate 				break;
32990Sstevel@tonic-gate 			/*FALLTHROUGH*/
33000Sstevel@tonic-gate 
33010Sstevel@tonic-gate 		case BAR:
33020Sstevel@tonic-gate 			if (Hdr_p != NULL && strcmp(Hdr_p, "bar") == 0) {
33030Sstevel@tonic-gate 				Hdrsz = BARSZ;
33040Sstevel@tonic-gate 				FILL(Hdrsz);
33050Sstevel@tonic-gate 				if ((hit = read_hdr(BAR)) == NONE) {
33060Sstevel@tonic-gate 					Hdrsz = ASCSZ;
33070Sstevel@tonic-gate 					break;
33080Sstevel@tonic-gate 				}
33090Sstevel@tonic-gate 				hit = BAR;
33100Sstevel@tonic-gate 				hsize = BARSZ;
33110Sstevel@tonic-gate 				break;
33120Sstevel@tonic-gate 			}
33130Sstevel@tonic-gate 			/*FALLTHROUGH*/
33140Sstevel@tonic-gate 
33150Sstevel@tonic-gate 		case USTAR:
33160Sstevel@tonic-gate 			if (Hdr_p != NULL && strcmp(Hdr_p, "ustar") == 0) {
33170Sstevel@tonic-gate 				Hdrsz = TARSZ;
33180Sstevel@tonic-gate 				FILL(Hdrsz);
33190Sstevel@tonic-gate 				if ((hit = read_hdr(USTAR)) == NONE) {
33200Sstevel@tonic-gate 					Hdrsz = ASCSZ;
33210Sstevel@tonic-gate 					break;
33220Sstevel@tonic-gate 				}
33230Sstevel@tonic-gate 				hit = USTAR;
33240Sstevel@tonic-gate 				hsize = TARSZ;
33250Sstevel@tonic-gate 				break;
33260Sstevel@tonic-gate 			}
33270Sstevel@tonic-gate 			/*FALLTHROUGH*/
33280Sstevel@tonic-gate 		case TAR:
33290Sstevel@tonic-gate 			if (Hdr_p != NULL && strcmp(Hdr_p, "tar") == 0) {
33300Sstevel@tonic-gate 				Hdrsz = TARSZ;
33310Sstevel@tonic-gate 				FILL(Hdrsz);
33320Sstevel@tonic-gate 				if ((hit = read_hdr(TAR)) == NONE) {
33330Sstevel@tonic-gate 					Hdrsz = ASCSZ;
33340Sstevel@tonic-gate 					break;
33350Sstevel@tonic-gate 				}
33360Sstevel@tonic-gate 				hit = TAR;
33370Sstevel@tonic-gate 				hsize = TARSZ;
33380Sstevel@tonic-gate 				break;
33390Sstevel@tonic-gate 			}
33400Sstevel@tonic-gate 			/*FALLTHROUGH*/
33410Sstevel@tonic-gate 		default:
33420Sstevel@tonic-gate 			msg(EXT, "Impossible header type.");
33430Sstevel@tonic-gate 		} /* Hdr_type */
33440Sstevel@tonic-gate 
33450Sstevel@tonic-gate 		if (hit == TAR || hit == USTAR) {
33460Sstevel@tonic-gate 			Gen.g_nam_p = &nambuf[0];
33470Sstevel@tonic-gate 		}
33480Sstevel@tonic-gate 
33490Sstevel@tonic-gate 		if (hit != NONE) {
33500Sstevel@tonic-gate 			FILL(hsize);
33510Sstevel@tonic-gate 			goodhdr = 1;
33520Sstevel@tonic-gate 			if (Gen.g_filesz < (off_t)0 || Gen.g_namesz < 1)
33530Sstevel@tonic-gate 				goodhdr = 0;
33540Sstevel@tonic-gate 			if ((hit != USTAR) && (hit != TAR))
33550Sstevel@tonic-gate 				if (Gen.g_namesz - 1 > Max_namesz)
33560Sstevel@tonic-gate 					goodhdr = 0;
33570Sstevel@tonic-gate 			/* TAR and USTAR */
33580Sstevel@tonic-gate 			if ((hit == USTAR) || (hit == TAR)) {
33590Sstevel@tonic-gate 				if (*Gen.g_nam_p == '\0') { /* tar trailer */
33600Sstevel@tonic-gate 					goodhdr = 1;
33610Sstevel@tonic-gate 				} else {
33620Sstevel@tonic-gate 
33630Sstevel@tonic-gate 					G_p = &Gen;
33640Sstevel@tonic-gate 					if (G_p->g_cksum !=
33650Sstevel@tonic-gate 					    cksum(TARTYP, 0, NULL)) {
33660Sstevel@tonic-gate 						goodhdr = 0;
33670Sstevel@tonic-gate 						msg(ERR,
33680Sstevel@tonic-gate 						    "Bad header - checksum "
33690Sstevel@tonic-gate 						    "error.");
33700Sstevel@tonic-gate 					}
33710Sstevel@tonic-gate 				}
33720Sstevel@tonic-gate 			} else if (hit != BAR) { /* binary, -c, ASC and CRC */
33730Sstevel@tonic-gate 				if (Gen.g_nlink <= (ulong_t)0)
33740Sstevel@tonic-gate 					goodhdr = 0;
33750Sstevel@tonic-gate 				if (*(Buffr.b_out_p + hsize - 1) != '\0')
33760Sstevel@tonic-gate 					goodhdr = 0;
33770Sstevel@tonic-gate 			}
33780Sstevel@tonic-gate 			if (!goodhdr) {
33790Sstevel@tonic-gate 				hit = NONE;
33800Sstevel@tonic-gate 				if (!(Args & OCk))
33810Sstevel@tonic-gate 					break;
33820Sstevel@tonic-gate 				msg(ERR,
33830Sstevel@tonic-gate 				    "Corrupt header, file(s) may be lost.");
33840Sstevel@tonic-gate 			} else {
33850Sstevel@tonic-gate 				FILL(hsize);
33860Sstevel@tonic-gate 			}
33870Sstevel@tonic-gate 		} /* hit != NONE */
33880Sstevel@tonic-gate 		if (hit == NONE) {
33890Sstevel@tonic-gate 			Buffr.b_out_p++;
33900Sstevel@tonic-gate 			Buffr.b_cnt--;
33910Sstevel@tonic-gate 			if (!(Args & OCk))
33920Sstevel@tonic-gate 				break;
33930Sstevel@tonic-gate 			if (!cnt++)
33940Sstevel@tonic-gate 				msg(ERR, "Searching for magic number/header.");
33950Sstevel@tonic-gate 		}
33960Sstevel@tonic-gate 	} while (hit == NONE);
33970Sstevel@tonic-gate 	if (hit == NONE) {
33980Sstevel@tonic-gate 		if (Hdr_type == NONE)
33990Sstevel@tonic-gate 			msg(EXT, "Not a cpio file, bad header.");
34000Sstevel@tonic-gate 		else
34010Sstevel@tonic-gate 			msg(EXT, "Bad magic number/header.");
34020Sstevel@tonic-gate 	} else if (cnt > 0) {
34030Sstevel@tonic-gate 		msg(EPOST, "Re-synchronized on magic number/header.");
34040Sstevel@tonic-gate 	}
34050Sstevel@tonic-gate 	if (Hdr_type == NONE) {
34060Sstevel@tonic-gate 		Hdr_type = hit;
34070Sstevel@tonic-gate 		switch (Hdr_type) {
34080Sstevel@tonic-gate 		case BIN:
34090Sstevel@tonic-gate 			if (bswap)
34100Sstevel@tonic-gate 				Args |= BSM;
34110Sstevel@tonic-gate 			Hdrsz = HDRSZ;
34120Sstevel@tonic-gate 			Max_namesz = CPATH;
34130Sstevel@tonic-gate 			Pad_val = HALFWD;
34140Sstevel@tonic-gate 			Onecopy = 0;
34150Sstevel@tonic-gate 			break;
34160Sstevel@tonic-gate 		case CHR:
34170Sstevel@tonic-gate 			Hdrsz = CHRSZ;
34180Sstevel@tonic-gate 			Max_namesz = CPATH;
34190Sstevel@tonic-gate 			Pad_val = 0;
34200Sstevel@tonic-gate 			Onecopy = 0;
34210Sstevel@tonic-gate 			break;
34220Sstevel@tonic-gate 		case ASC:
34230Sstevel@tonic-gate 		case CRC:
34240Sstevel@tonic-gate 			Hdrsz = ASCSZ;
34250Sstevel@tonic-gate 			Max_namesz = APATH;
34260Sstevel@tonic-gate 			Pad_val = FULLWD;
34270Sstevel@tonic-gate 			Onecopy = 1;
34280Sstevel@tonic-gate 			break;
34290Sstevel@tonic-gate 		case USTAR:
34300Sstevel@tonic-gate 			Hdrsz = TARSZ;
34310Sstevel@tonic-gate 			Max_namesz = HNAMLEN - 1;
34320Sstevel@tonic-gate 			Pad_val = FULLBK;
34330Sstevel@tonic-gate 			Onecopy = 0;
34340Sstevel@tonic-gate 			break;
34350Sstevel@tonic-gate 		case BAR:
34360Sstevel@tonic-gate 		case TAR:
34370Sstevel@tonic-gate 			Hdrsz = TARSZ;
34380Sstevel@tonic-gate 			Max_namesz = TNAMLEN - 1;
34390Sstevel@tonic-gate 			Pad_val = FULLBK;
34400Sstevel@tonic-gate 			Onecopy = 0;
34410Sstevel@tonic-gate 			break;
34420Sstevel@tonic-gate 		default:
34430Sstevel@tonic-gate 			msg(EXT, "Impossible header type.");
34440Sstevel@tonic-gate 		} /* Hdr_type */
34450Sstevel@tonic-gate 	} /* Hdr_type == NONE */
34460Sstevel@tonic-gate 	if ((Hdr_type == USTAR) || (Hdr_type == TAR) ||
34470Sstevel@tonic-gate 	    (Hdr_type == BAR)) {			/* TAR, USTAR, BAR */
34480Sstevel@tonic-gate 		Gen.g_namesz = 0;
34490Sstevel@tonic-gate 		if (Gen.g_nam_p[0] == '\0')
34500Sstevel@tonic-gate 			return (0);
34510Sstevel@tonic-gate 		else {
34520Sstevel@tonic-gate 			preptr = &prebuf[0];
34530Sstevel@tonic-gate 			if (*preptr != (char)NULL) {
34540Sstevel@tonic-gate 				k = strlen(&prebuf[0]);
34550Sstevel@tonic-gate 				if (k < PRESIZ) {
34560Sstevel@tonic-gate 					(void) strcpy(&fullnam[0], &prebuf[0]);
34570Sstevel@tonic-gate 					j = 0;
34580Sstevel@tonic-gate 					fullnam[k++] = '/';
34590Sstevel@tonic-gate 					while ((j < NAMSIZ) && (&nambuf[j] !=
34600Sstevel@tonic-gate 					    (char)NULL)) {
34610Sstevel@tonic-gate 						fullnam[k] = nambuf[j];
34620Sstevel@tonic-gate 						k++; j++;
34630Sstevel@tonic-gate 					}
34640Sstevel@tonic-gate 					fullnam[k] = '\0';
34650Sstevel@tonic-gate 				} else if (k >= PRESIZ) {
34660Sstevel@tonic-gate 					k = 0;
34670Sstevel@tonic-gate 					while ((k < PRESIZ) && (prebuf[k] !=
34680Sstevel@tonic-gate 					    '\0')) {
34690Sstevel@tonic-gate 						fullnam[k] = prebuf[k];
34700Sstevel@tonic-gate 						k++;
34710Sstevel@tonic-gate 					}
34720Sstevel@tonic-gate 					fullnam[k++] = '/';
34730Sstevel@tonic-gate 					j = 0;
34740Sstevel@tonic-gate 					while ((j < NAMSIZ) && (nambuf[j] !=
34750Sstevel@tonic-gate 					    '\0')) {
34760Sstevel@tonic-gate 						fullnam[k] = nambuf[j];
34770Sstevel@tonic-gate 						k++; j++;
34780Sstevel@tonic-gate 					}
34790Sstevel@tonic-gate 					fullnam[k] = '\0';
34800Sstevel@tonic-gate 				}
34810Sstevel@tonic-gate 				Gen.g_nam_p = &fullnam[0];
34820Sstevel@tonic-gate 			} else
34830Sstevel@tonic-gate 				Gen.g_nam_p = &nambuf[0];
34840Sstevel@tonic-gate 
34850Sstevel@tonic-gate 			/*
34860Sstevel@tonic-gate 			 * initialize the buffer so that the prefix will not
34870Sstevel@tonic-gate 			 * applied to the next entry in the archive
34880Sstevel@tonic-gate 			 */
34890Sstevel@tonic-gate 			(void) memset(prebuf, 0, sizeof (prebuf));
34900Sstevel@tonic-gate 		}
34910Sstevel@tonic-gate 	} else if (Hdr_type != BAR) {
34920Sstevel@tonic-gate 		(void) memcpy(Gen.g_nam_p, Buffr.b_out_p + Hdrsz, Gen.g_namesz);
34930Sstevel@tonic-gate 		if (!(strcmp(Gen.g_nam_p, "TRAILER!!!")))
34940Sstevel@tonic-gate 			return (0);
34950Sstevel@tonic-gate 	}
34960Sstevel@tonic-gate 	offset = ((hsize + Pad_val) & ~Pad_val);
34970Sstevel@tonic-gate 	FILL(offset + Hdrsz);
34980Sstevel@tonic-gate 	Thdr_p = (union tblock *)Buffr.b_out_p;
34990Sstevel@tonic-gate 	Buffr.b_out_p += offset;
35000Sstevel@tonic-gate 	Buffr.b_cnt -= (off_t)offset;
35010Sstevel@tonic-gate 	ftype = Gen.g_mode & Ftype;
35020Sstevel@tonic-gate 
35030Sstevel@tonic-gate #if defined(O_XATTR)
35040Sstevel@tonic-gate 	/* extended attribute support */
35050Sstevel@tonic-gate 	if (((Gen.g_mode & S_IFMT) == _XATTR_CPIO_MODE) ||
35060Sstevel@tonic-gate 	    ((Hdr_type == USTAR || Hdr_type == TAR) &&
35070Sstevel@tonic-gate 	    Thdr_p->tbuf.t_typeflag == _XATTR_HDRTYPE)) {
35080Sstevel@tonic-gate 		if (xattrp != (struct xattr_buf *)NULL) {
35090Sstevel@tonic-gate 			if (xattrbadhead) {
35100Sstevel@tonic-gate 				free(xattrhead);
35110Sstevel@tonic-gate 				xattrp = NULL;
35120Sstevel@tonic-gate 				xattr_linkp = NULL;
35130Sstevel@tonic-gate 				xattrhead = NULL;
35140Sstevel@tonic-gate 				return (1);
35150Sstevel@tonic-gate 			}
35160Sstevel@tonic-gate 			if (Atflag == 0 && ((Args & OCt) == 0)) {
35170Sstevel@tonic-gate 				data_in(P_SKIP);
35180Sstevel@tonic-gate 				free(xattrhead);
35190Sstevel@tonic-gate 				xattrhead = NULL;
35200Sstevel@tonic-gate 				xattrp = NULL;
35210Sstevel@tonic-gate 				Gen.g_attrfnam_p = (char *)NULL;
35220Sstevel@tonic-gate 				Gen.g_attrnam_p = (char *)NULL;
35230Sstevel@tonic-gate 				return (2);
35240Sstevel@tonic-gate 			}
35250Sstevel@tonic-gate 
35260Sstevel@tonic-gate 			if (Gen.g_attrfnam_p != (char *)NULL) {
35270Sstevel@tonic-gate 				free(Gen.g_attrfnam_p);
35280Sstevel@tonic-gate 				Gen.g_attrfnam_p = (char *)NULL;
35290Sstevel@tonic-gate 			}
35300Sstevel@tonic-gate 			if (Gen.g_attrnam_p != (char *)NULL) {
35310Sstevel@tonic-gate 				free(Gen.g_attrnam_p);
35320Sstevel@tonic-gate 				Gen.g_attrnam_p = (char *)NULL;
35330Sstevel@tonic-gate 			}
35340Sstevel@tonic-gate 			if (Renam_p && Renam_p[0] != '\0') {
35350Sstevel@tonic-gate 				Gen.g_attrfnam_p = e_strdup(E_EXIT, Renam_p);
35360Sstevel@tonic-gate 			} else {
35370Sstevel@tonic-gate 				Gen.g_attrfnam_p = e_strdup(E_EXIT,
35380Sstevel@tonic-gate 				    xattrp->h_names);
35390Sstevel@tonic-gate 			}
35400Sstevel@tonic-gate 
35410Sstevel@tonic-gate 			Gen.g_attrnam_p = e_strdup(E_EXIT,
35420Sstevel@tonic-gate 			    xattrp->h_names + strlen(xattrp->h_names) + 1);
35430Sstevel@tonic-gate 
35440Sstevel@tonic-gate 			if (Hdr_type != USTAR && Hdr_type != TAR) {
35450Sstevel@tonic-gate 				Gen.g_mode = Gen.g_mode & (~_XATTR_CPIO_MODE);
35460Sstevel@tonic-gate 				Gen.g_mode |= attrmode(xattrp->h_typeflag);
35470Sstevel@tonic-gate 
35480Sstevel@tonic-gate 			} else if (Hdr_type == USTAR || Hdr_type == TAR) {
35490Sstevel@tonic-gate 				Thdr_p->tbuf.t_typeflag = xattrp->h_typeflag;
35500Sstevel@tonic-gate 			}
35510Sstevel@tonic-gate 			if (xattr_linkp != (struct xattr_buf *)NULL) {
35520Sstevel@tonic-gate 				if (Gen.g_linktoattrfnam_p != (char *)NULL) {
35530Sstevel@tonic-gate 					free(Gen.g_linktoattrfnam_p);
35540Sstevel@tonic-gate 					Gen.g_linktoattrfnam_p = NULL;
35550Sstevel@tonic-gate 				}
35560Sstevel@tonic-gate 				if (Gen.g_linktoattrnam_p != (char *)NULL) {
35570Sstevel@tonic-gate 					free(Gen.g_linktoattrnam_p);
35580Sstevel@tonic-gate 					Gen.g_linktoattrnam_p = NULL;
35590Sstevel@tonic-gate 				}
35600Sstevel@tonic-gate 				Gen.g_linktoattrfnam_p = e_strdup(E_EXIT,
35610Sstevel@tonic-gate 				    xattr_linkp->h_names);
35620Sstevel@tonic-gate 				Gen.g_linktoattrnam_p = e_strdup(E_EXIT,
35630Sstevel@tonic-gate 				    xattr_linkp->h_names +
35640Sstevel@tonic-gate 				    strlen(xattr_linkp->h_names) + 1);
35650Sstevel@tonic-gate 				xattr_linkp = NULL;
35660Sstevel@tonic-gate 			}
35670Sstevel@tonic-gate 			ftype = Gen.g_mode & Ftype;
35680Sstevel@tonic-gate 			Adir = ftype == S_IFDIR;
35690Sstevel@tonic-gate 			Aspec = (ftype == S_IFBLK ||
35700Sstevel@tonic-gate 			    ftype == S_IFCHR || ftype == S_IFIFO);
35710Sstevel@tonic-gate 
35720Sstevel@tonic-gate 			if (Gen.g_attrnam_p[0] == '.' &&
35730Sstevel@tonic-gate 			    Gen.g_attrnam_p[1] == '\0' &&
35740Sstevel@tonic-gate 			    xattrp->h_typeflag == DIRTYPE) {
35750Sstevel@tonic-gate 				Hiddendir = 1;
35760Sstevel@tonic-gate 			} else {
35770Sstevel@tonic-gate 				Hiddendir = 0;
35780Sstevel@tonic-gate 			}
35790Sstevel@tonic-gate 
35800Sstevel@tonic-gate 			free(xattrhead);
35810Sstevel@tonic-gate 			xattrhead = NULL;
35820Sstevel@tonic-gate 			xattrp = NULL;
35830Sstevel@tonic-gate 		} else {
35840Sstevel@tonic-gate 			if (xattrbadhead == 0) {
35850Sstevel@tonic-gate 				(void) read_xattr_hdr();
35860Sstevel@tonic-gate 				return (2);
35870Sstevel@tonic-gate 			}
35880Sstevel@tonic-gate 		}
35890Sstevel@tonic-gate 	}
35900Sstevel@tonic-gate #endif /* O_XATTR */
35910Sstevel@tonic-gate 
35920Sstevel@tonic-gate 	/* acl support: grab acl info */
35930Sstevel@tonic-gate 	if ((Gen.g_mode == SECMODE) || ((Hdr_type == USTAR ||
35940Sstevel@tonic-gate 	    Hdr_type == TAR) && Thdr_p->tbuf.t_typeflag == 'A')) {
35950Sstevel@tonic-gate 		/* this is an ancillary file */
35960Sstevel@tonic-gate 		off_t	bytes;
35970Sstevel@tonic-gate 		char	*secp;
35980Sstevel@tonic-gate 		int	pad;
35990Sstevel@tonic-gate 		int	cnt;
36000Sstevel@tonic-gate 		char	*tp;
36010Sstevel@tonic-gate 		int	attrsize;
36020Sstevel@tonic-gate 
36030Sstevel@tonic-gate 		if (Pflag) {
36040Sstevel@tonic-gate 			bytes = Gen.g_filesz;
36050Sstevel@tonic-gate 			secp = e_zalloc(E_EXIT, (uint_t)bytes);
36060Sstevel@tonic-gate 			tp = secp;
36070Sstevel@tonic-gate 
36080Sstevel@tonic-gate 			while (bytes > 0) {
36090Sstevel@tonic-gate 				cnt = (int)(bytes > CPIOBSZ) ? CPIOBSZ : bytes;
36100Sstevel@tonic-gate 				FILL(cnt);
36110Sstevel@tonic-gate 				(void) memcpy(tp, Buffr.b_out_p, cnt);
36120Sstevel@tonic-gate 				tp += cnt;
36130Sstevel@tonic-gate 				Buffr.b_out_p += cnt;
36140Sstevel@tonic-gate 				Buffr.b_cnt -= (off_t)cnt;
36150Sstevel@tonic-gate 				bytes -= (off_t)cnt;
36160Sstevel@tonic-gate 			}
36170Sstevel@tonic-gate 
36180Sstevel@tonic-gate 			pad = (Pad_val + 1 - (Gen.g_filesz & Pad_val)) &
36190Sstevel@tonic-gate 			    Pad_val;
36200Sstevel@tonic-gate 			if (pad != 0) {
36210Sstevel@tonic-gate 				FILL(pad);
36220Sstevel@tonic-gate 				Buffr.b_out_p += pad;
36230Sstevel@tonic-gate 				Buffr.b_cnt -= (off_t)pad;
36240Sstevel@tonic-gate 			}
36250Sstevel@tonic-gate 
36260Sstevel@tonic-gate 			/* got all attributes in secp */
36270Sstevel@tonic-gate 			tp = secp;
36280Sstevel@tonic-gate 			do {
36290Sstevel@tonic-gate 				attr = (struct sec_attr *)tp;
36300Sstevel@tonic-gate 				switch (attr->attr_type) {
36310Sstevel@tonic-gate 				case UFSD_ACL:
36320Sstevel@tonic-gate 					(void) sscanf(attr->attr_len, "%7lo",
36330Sstevel@tonic-gate 					    (ulong_t *)&aclcnt);
36340Sstevel@tonic-gate 				/* header is 8 */
36350Sstevel@tonic-gate 					attrsize = 8 +
36360Sstevel@tonic-gate 					    strlen(&attr->attr_info[0])
36370Sstevel@tonic-gate 					    + 1;
36380Sstevel@tonic-gate 					aclp = aclfromtext(&attr->attr_info[0],
36390Sstevel@tonic-gate 					    &cnt);
36400Sstevel@tonic-gate 					if (aclp == NULL) {
36410Sstevel@tonic-gate 						msg(ERR, "aclfromtext failed");
36420Sstevel@tonic-gate 						break;
36430Sstevel@tonic-gate 					}
36440Sstevel@tonic-gate 					if (aclcnt != cnt) {
36450Sstevel@tonic-gate 						msg(ERR, "acl count error");
36460Sstevel@tonic-gate 						break;
36470Sstevel@tonic-gate 					}
36480Sstevel@tonic-gate 					bytes -= attrsize;
36490Sstevel@tonic-gate 					break;
36500Sstevel@tonic-gate 
36510Sstevel@tonic-gate 				/* SunFed case goes here */
36520Sstevel@tonic-gate 
36530Sstevel@tonic-gate 				default:
36540Sstevel@tonic-gate 					msg(EXT, "unrecognized attr type");
36550Sstevel@tonic-gate 					break;
36560Sstevel@tonic-gate 			}
36570Sstevel@tonic-gate 			/* next attributes */
36580Sstevel@tonic-gate 			tp += attrsize;
36590Sstevel@tonic-gate 			} while (bytes > 0);
36600Sstevel@tonic-gate 			free(secp);
36610Sstevel@tonic-gate 		} else {
36620Sstevel@tonic-gate 			/* skip security info */
36630Sstevel@tonic-gate 			G_p = &Gen;
36640Sstevel@tonic-gate 			data_in(P_SKIP);
36650Sstevel@tonic-gate 		}
36660Sstevel@tonic-gate 		/*
36670Sstevel@tonic-gate 		 * We already got the file content, dont call file_in()
36680Sstevel@tonic-gate 		 * when return. The new return code(2) is used to
36690Sstevel@tonic-gate 		 *  indicate that.
36700Sstevel@tonic-gate 		 */
36710Sstevel@tonic-gate 		VERBOSE((Args & OCt), Gen.g_nam_p);
36720Sstevel@tonic-gate 		return (2);
36730Sstevel@tonic-gate 	} /* acl */
36740Sstevel@tonic-gate 
36750Sstevel@tonic-gate 	Adir = (ftype == S_IFDIR);
36760Sstevel@tonic-gate 	Aspec = (ftype == S_IFBLK || ftype == S_IFCHR || ftype == S_IFIFO);
36770Sstevel@tonic-gate 
36780Sstevel@tonic-gate 	/*
36790Sstevel@tonic-gate 	 * Skip any trailing slashes
36800Sstevel@tonic-gate 	 */
36810Sstevel@tonic-gate 	chop_endslashes(Gen.g_nam_p);
36820Sstevel@tonic-gate 	return (1);
36830Sstevel@tonic-gate }
36840Sstevel@tonic-gate 
36850Sstevel@tonic-gate /*
36860Sstevel@tonic-gate  * getname: Get file names for inclusion in the archive.  When end of file
36870Sstevel@tonic-gate  * on the input stream of file names is reached, flush the link buffer out.
36880Sstevel@tonic-gate  * For each filename, remove leading "./"s and multiple "/"s, and remove
36890Sstevel@tonic-gate  * any trailing newline "\n".  Finally, verify the existance of the file,
36900Sstevel@tonic-gate  * and call creat_hdr() to fill in the gen_hdr structure.
36910Sstevel@tonic-gate  */
36920Sstevel@tonic-gate 
36930Sstevel@tonic-gate static int
36940Sstevel@tonic-gate getname(void)
36950Sstevel@tonic-gate {
36960Sstevel@tonic-gate 	int goodfile = 0, lastchar, err;
36970Sstevel@tonic-gate 	char *s;
36980Sstevel@tonic-gate 	char *dir;
36990Sstevel@tonic-gate 
37000Sstevel@tonic-gate 	Gen.g_nam_p = Nam_p;
37010Sstevel@tonic-gate 
37020Sstevel@tonic-gate 	while (!goodfile) {
37030Sstevel@tonic-gate 		err = 0;
37040Sstevel@tonic-gate 
37050Sstevel@tonic-gate 		while ((s = fgets(Gen.g_nam_p, APATH+1, In_p))
37060Sstevel@tonic-gate 		    != NULL) {
37070Sstevel@tonic-gate 			lastchar = strlen(s) - 1;
37080Sstevel@tonic-gate 			issymlink = 0;
37090Sstevel@tonic-gate 
37100Sstevel@tonic-gate 			if (s[lastchar] != '\n') {
37110Sstevel@tonic-gate 				if (lastchar == APATH - 1) {
37120Sstevel@tonic-gate 					if (!err) {
37130Sstevel@tonic-gate 						msg(ERR,
37140Sstevel@tonic-gate 						    "%s name too long.",
37150Sstevel@tonic-gate 						    Nam_p);
37160Sstevel@tonic-gate 					}
37170Sstevel@tonic-gate 					goodfile = 0;
37180Sstevel@tonic-gate 					err = 1;
37190Sstevel@tonic-gate 				} else {
37200Sstevel@tonic-gate 					break;
37210Sstevel@tonic-gate 				}
37220Sstevel@tonic-gate 			} else {
37230Sstevel@tonic-gate 				s[lastchar] = '\0';
37240Sstevel@tonic-gate 				break;
37250Sstevel@tonic-gate 			}
37260Sstevel@tonic-gate 		}
37270Sstevel@tonic-gate 
37280Sstevel@tonic-gate 		if (s == (char *)NULL) {
37290Sstevel@tonic-gate 			if (Gen.g_dirfd != -1) {
37300Sstevel@tonic-gate 				(void) close(Gen.g_dirfd);
37310Sstevel@tonic-gate 				Gen.g_dirfd = -1;
37320Sstevel@tonic-gate 			}
37330Sstevel@tonic-gate 			if (Onecopy && (Args & OCo)) {
37340Sstevel@tonic-gate 				flush_lnks();
37350Sstevel@tonic-gate 			}
37360Sstevel@tonic-gate 			return (0);
37370Sstevel@tonic-gate 		}
37380Sstevel@tonic-gate 
37390Sstevel@tonic-gate 		while (*Gen.g_nam_p == '.' && Gen.g_nam_p[1] == '/') {
37400Sstevel@tonic-gate 			Gen.g_nam_p += 2;
37410Sstevel@tonic-gate 			while (*Gen.g_nam_p == '/')
37420Sstevel@tonic-gate 				Gen.g_nam_p++;
37430Sstevel@tonic-gate 		}
37440Sstevel@tonic-gate 
37450Sstevel@tonic-gate 		/*
37460Sstevel@tonic-gate 		 * Skip any trailing slashes
37470Sstevel@tonic-gate 		 */
37480Sstevel@tonic-gate 		chop_endslashes(Gen.g_nam_p);
37490Sstevel@tonic-gate 
37500Sstevel@tonic-gate 		/*
37510Sstevel@tonic-gate 		 * Figure out parent directory
37520Sstevel@tonic-gate 		 */
37530Sstevel@tonic-gate 
37540Sstevel@tonic-gate 		if (Gen.g_attrnam_p != (char *)NULL) {
37550Sstevel@tonic-gate 			if (Gen.g_dirfd != -1) {
37560Sstevel@tonic-gate 				(void) close(Gen.g_dirfd);
37570Sstevel@tonic-gate 			}
37580Sstevel@tonic-gate 			Gen.g_dirfd = attropen(Gen.g_attrfnam_p, ".", O_RDONLY);
37590Sstevel@tonic-gate 			if (Gen.g_dirfd == -1) {
37600Sstevel@tonic-gate 				msg(ERRN,
37610Sstevel@tonic-gate 				    "Cannot open attribute directory"
37620Sstevel@tonic-gate 				    " of file %s", Gen.g_attrfnam_p);
37630Sstevel@tonic-gate 				continue;
37640Sstevel@tonic-gate 			}
37650Sstevel@tonic-gate 		} else {
37660Sstevel@tonic-gate #ifdef O_XATTR
37670Sstevel@tonic-gate 			char dirpath[PATH_MAX];
37680Sstevel@tonic-gate 
37690Sstevel@tonic-gate 			get_parent(Gen.g_nam_p, dirpath);
37700Sstevel@tonic-gate 			if (Atflag) {
37710Sstevel@tonic-gate 				dir = dirpath;
37720Sstevel@tonic-gate 				if (Gen.g_dirfd != -1) {
37730Sstevel@tonic-gate 					(void) close(Gen.g_dirfd);
37740Sstevel@tonic-gate 				}
37750Sstevel@tonic-gate 				Gen.g_dirfd = open(dir, O_RDONLY);
37760Sstevel@tonic-gate 				if (Gen.g_dirfd == -1) {
37770Sstevel@tonic-gate 					msg(ERRN,
37780Sstevel@tonic-gate 					    "Cannot open directory %s", dir);
37790Sstevel@tonic-gate 					continue;
37800Sstevel@tonic-gate 				}
37810Sstevel@tonic-gate 			} else {
37820Sstevel@tonic-gate 				/*
37830Sstevel@tonic-gate 				 * g_dirpath is the pathname cache maintaining
37840Sstevel@tonic-gate 				 * the dirname which is currently opened.
37850Sstevel@tonic-gate 				 * We first check the g_dirpath to see if the
37860Sstevel@tonic-gate 				 * given dirname matches. If so, we don't need
37870Sstevel@tonic-gate 				 * to open the dir, but we can use the g_dirfd
37880Sstevel@tonic-gate 				 * as is if it is still available.
37890Sstevel@tonic-gate 				 */
37900Sstevel@tonic-gate 				dir = NULL;
37910Sstevel@tonic-gate 				if (Gen.g_dirpath == NULL ||
37920Sstevel@tonic-gate 				    Gen.g_dirfd == -1) {
37930Sstevel@tonic-gate 					/*
37940Sstevel@tonic-gate 					 * It's the first time or it has
37950Sstevel@tonic-gate 					 * all gone.
37960Sstevel@tonic-gate 					 */
37970Sstevel@tonic-gate 					dir = e_strdup(E_EXIT, dirpath);
37980Sstevel@tonic-gate 				} else {
37990Sstevel@tonic-gate 					if (strcmp(Gen.g_dirpath,
38000Sstevel@tonic-gate 					    dirpath) != 0) {
38010Sstevel@tonic-gate 						/* different directory */
38020Sstevel@tonic-gate 						dir = e_strdup(E_EXIT, dirpath);
38030Sstevel@tonic-gate 					}
38040Sstevel@tonic-gate 				}
38050Sstevel@tonic-gate 				if (dir != NULL) {
38060Sstevel@tonic-gate 					/*
38070Sstevel@tonic-gate 					 * We need to open the new directory.
38080Sstevel@tonic-gate 					 * discard the pathname and dirfd
38090Sstevel@tonic-gate 					 * for the previous directory.
38100Sstevel@tonic-gate 					 */
38110Sstevel@tonic-gate 					if (Gen.g_dirpath != NULL) {
38120Sstevel@tonic-gate 						free(Gen.g_dirpath);
38130Sstevel@tonic-gate 						Gen.g_dirpath = NULL;
38140Sstevel@tonic-gate 					}
38150Sstevel@tonic-gate 					if (Gen.g_dirfd != -1) {
38160Sstevel@tonic-gate 						(void) close(Gen.g_dirfd);
38170Sstevel@tonic-gate 					}
38180Sstevel@tonic-gate 					/* open the new dir */
38190Sstevel@tonic-gate 					Gen.g_dirfd = open(dir, O_RDONLY);
38200Sstevel@tonic-gate 					if (Gen.g_dirfd == -1) {
38210Sstevel@tonic-gate 						msg(ERRN, "Cannot open "
38220Sstevel@tonic-gate 							"directory %s", dir);
38230Sstevel@tonic-gate 						continue;
38240Sstevel@tonic-gate 					}
38250Sstevel@tonic-gate 					Gen.g_dirpath = dir;
38260Sstevel@tonic-gate 				}
38270Sstevel@tonic-gate 			}
38280Sstevel@tonic-gate #else
38290Sstevel@tonic-gate 			Gen.g_dirfd = -1;
38300Sstevel@tonic-gate #endif
38310Sstevel@tonic-gate 		}
38320Sstevel@tonic-gate 
38330Sstevel@tonic-gate 		/* creat_hdr checks for USTAR filename length */
38340Sstevel@tonic-gate 
38350Sstevel@tonic-gate 		if (Hdr_type != USTAR && strlen(Gen.g_nam_p) >
38360Sstevel@tonic-gate 		    Max_namesz) {
38370Sstevel@tonic-gate 			if (!err) {
38380Sstevel@tonic-gate 				msg(ERR, "%s%s%s name too long.",
38390Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
38400Sstevel@tonic-gate 				    Nam_p : Gen.g_attrfnam_p,
38410Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
38420Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
38430Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
38440Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
38450Sstevel@tonic-gate 			}
38460Sstevel@tonic-gate 			goodfile = 0;
38470Sstevel@tonic-gate 			err = 1;
38480Sstevel@tonic-gate 		}
38490Sstevel@tonic-gate 
38500Sstevel@tonic-gate 		if (err) {
38510Sstevel@tonic-gate 			continue;
38520Sstevel@tonic-gate 		} else {
38530Sstevel@tonic-gate 			G_p = &Gen;
38540Sstevel@tonic-gate 			if (!LSTAT(Gen.g_dirfd, Gen.g_nam_p, &SrcSt)) {
38550Sstevel@tonic-gate 				goodfile = 1;
38560Sstevel@tonic-gate 
38570Sstevel@tonic-gate 				if ((SrcSt.st_mode & Ftype) == S_IFLNK) {
38580Sstevel@tonic-gate 					issymlink = 1;
38590Sstevel@tonic-gate 
38600Sstevel@tonic-gate 					if ((Args & OCL)) {
38610Sstevel@tonic-gate 						errno = 0;
38620Sstevel@tonic-gate 						if (STAT(Gen.g_dirfd,
38630Sstevel@tonic-gate 						    G_p->g_nam_p,
38640Sstevel@tonic-gate 						    &SrcSt) < 0) {
38650Sstevel@tonic-gate 							msg(ERRN,
38660Sstevel@tonic-gate 							    "Cannot follow"
38670Sstevel@tonic-gate 							    " \"%s%s%s\"",
38680Sstevel@tonic-gate 							    (Gen.g_attrnam_p ==
38690Sstevel@tonic-gate 							    (char *)NULL) ?
38700Sstevel@tonic-gate 							    Gen.g_nam_p :
38710Sstevel@tonic-gate 							    Gen.g_attrfnam_p,
38720Sstevel@tonic-gate 							    (Gen.g_attrnam_p ==
38730Sstevel@tonic-gate 							    (char *)NULL) ?
38740Sstevel@tonic-gate 							    "" :
38750Sstevel@tonic-gate 							    gettext(
38760Sstevel@tonic-gate 							    " Attribute "),
38770Sstevel@tonic-gate 							    (Gen.g_attrnam_p ==
38780Sstevel@tonic-gate 							    (char *)NULL) ?
38790Sstevel@tonic-gate 							    "" :
38800Sstevel@tonic-gate 							    Gen.g_attrnam_p);
38810Sstevel@tonic-gate 							goodfile = 0;
38820Sstevel@tonic-gate 						}
38830Sstevel@tonic-gate 					}
38840Sstevel@tonic-gate 				}
38850Sstevel@tonic-gate 
38860Sstevel@tonic-gate 				if (Use_old_stat) {
38870Sstevel@tonic-gate 					OldSt = convert_to_old_stat(&SrcSt,
38880Sstevel@tonic-gate 					    Gen.g_nam_p, Gen.g_attrnam_p);
38890Sstevel@tonic-gate 
38900Sstevel@tonic-gate 					if (OldSt == NULL) {
38910Sstevel@tonic-gate 						goodfile = 0;
38920Sstevel@tonic-gate 					}
38930Sstevel@tonic-gate 				}
38940Sstevel@tonic-gate 			} else {
38950Sstevel@tonic-gate 				msg(ERRN,
38960Sstevel@tonic-gate 				    "Error with fstatat() of \"%s%s%s\"",
38970Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
38980Sstevel@tonic-gate 				    Gen.g_nam_p : Gen.g_attrfnam_p,
38990Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
39000Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
39010Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
39020Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
39030Sstevel@tonic-gate 			}
39040Sstevel@tonic-gate 		}
39050Sstevel@tonic-gate 	}
39060Sstevel@tonic-gate 
39070Sstevel@tonic-gate 	/*
39080Sstevel@tonic-gate 	 * Get ACL info: dont bother allocating space if there are only
39090Sstevel@tonic-gate 	 * standard permissions, i.e. ACL count < 4
39100Sstevel@tonic-gate 	 */
39110Sstevel@tonic-gate 	if ((SrcSt.st_mode & Ftype) != S_IFLNK && Pflag) {
39120Sstevel@tonic-gate 		if ((aclcnt = acl(Gen.g_nam_p, GETACLCNT, 0, NULL)) < 0)
39130Sstevel@tonic-gate 			msg(ERRN, "Error with acl() of \"%s\"", Gen.g_nam_p);
39140Sstevel@tonic-gate 		if (aclcnt > MIN_ACL_ENTRIES) {
39150Sstevel@tonic-gate 			aclp = e_zalloc(E_EXIT, sizeof (aclent_t) * aclcnt);
39160Sstevel@tonic-gate 
39170Sstevel@tonic-gate 			if (acl(Gen.g_nam_p, GETACL, aclcnt, aclp) < 0) {
39180Sstevel@tonic-gate 				msg(ERRN,
39190Sstevel@tonic-gate 				    "Error with getacl() of \"%s\"",
39200Sstevel@tonic-gate 				    Gen.g_nam_p);
39210Sstevel@tonic-gate 				free(aclp);
39220Sstevel@tonic-gate 				aclp = NULL;
39230Sstevel@tonic-gate 			}
39240Sstevel@tonic-gate 		}
39250Sstevel@tonic-gate 	/* else: only traditional permissions, so proceed as usual */
39260Sstevel@tonic-gate 	}
39270Sstevel@tonic-gate 	if (creat_hdr())
39280Sstevel@tonic-gate 		return (1);
39290Sstevel@tonic-gate 	else return (2);
39300Sstevel@tonic-gate }
39310Sstevel@tonic-gate 
39320Sstevel@tonic-gate /*
39330Sstevel@tonic-gate  * getpats: Save any filenames/patterns specified as arguments.
39340Sstevel@tonic-gate  * Read additional filenames/patterns from the file specified by the
39350Sstevel@tonic-gate  * user.  The filenames/patterns must occur one per line.
39360Sstevel@tonic-gate  */
39370Sstevel@tonic-gate 
39380Sstevel@tonic-gate static void
39390Sstevel@tonic-gate getpats(int largc, char **largv)
39400Sstevel@tonic-gate {
39410Sstevel@tonic-gate 	char **t_pp;
39420Sstevel@tonic-gate 	size_t len;
39430Sstevel@tonic-gate 	unsigned numpat = largc, maxpat = largc + 2;
39440Sstevel@tonic-gate 
39450Sstevel@tonic-gate 	Pat_pp = e_zalloc(E_EXIT, maxpat * sizeof (char *));
39460Sstevel@tonic-gate 	t_pp = Pat_pp;
39470Sstevel@tonic-gate 	while (*largv) {
39480Sstevel@tonic-gate 		*t_pp = e_zalloc(E_EXIT, strlen(*largv) + 1);
39490Sstevel@tonic-gate 		(void) strcpy(*t_pp, *largv);
39500Sstevel@tonic-gate 		t_pp++;
39510Sstevel@tonic-gate 		largv++;
39520Sstevel@tonic-gate 	}
39530Sstevel@tonic-gate 	while (fgets(Nam_p, Max_namesz + 1, Ef_p) != (char *)NULL) {
39540Sstevel@tonic-gate 		if (numpat == maxpat - 1) {
39550Sstevel@tonic-gate 			maxpat += 10;
39560Sstevel@tonic-gate 			Pat_pp = e_realloc(E_EXIT, Pat_pp,
39570Sstevel@tonic-gate 			    maxpat * sizeof (char *));
39580Sstevel@tonic-gate 			t_pp = Pat_pp + numpat;
39590Sstevel@tonic-gate 		}
39600Sstevel@tonic-gate 		len = strlen(Nam_p); /* includes the \n */
39610Sstevel@tonic-gate 		*(Nam_p + len - 1) = '\0'; /* remove the \n */
39620Sstevel@tonic-gate 		*t_pp = e_zalloc(E_EXIT, len);
39630Sstevel@tonic-gate 		(void) strcpy(*t_pp, Nam_p);
39640Sstevel@tonic-gate 		t_pp++;
39650Sstevel@tonic-gate 		numpat++;
39660Sstevel@tonic-gate 	}
39670Sstevel@tonic-gate 	*t_pp = (char *)NULL;
39680Sstevel@tonic-gate }
39690Sstevel@tonic-gate 
39700Sstevel@tonic-gate static void
39710Sstevel@tonic-gate ioerror(int dir)
39720Sstevel@tonic-gate {
39730Sstevel@tonic-gate 	int t_errno;
39740Sstevel@tonic-gate 
39750Sstevel@tonic-gate 	t_errno = errno;
39760Sstevel@tonic-gate 	errno = 0;
39770Sstevel@tonic-gate 	if (fstat(Archive, &ArchSt) < 0)
39780Sstevel@tonic-gate 		msg(EXTN, "Error during stat() of archive");
39790Sstevel@tonic-gate 	errno = t_errno;
39800Sstevel@tonic-gate 	if ((ArchSt.st_mode & Ftype) != S_IFCHR) {
39810Sstevel@tonic-gate 		if (dir) {
39820Sstevel@tonic-gate 			if (errno == EFBIG)
39830Sstevel@tonic-gate 				msg(EXT, "ulimit reached for output file.");
39840Sstevel@tonic-gate 			else if (errno == ENOSPC)
39850Sstevel@tonic-gate 				msg(EXT, "No space left for output file.");
39860Sstevel@tonic-gate 			else
39870Sstevel@tonic-gate 				msg(EXTN, "I/O error - cannot continue");
39880Sstevel@tonic-gate 		} else
39890Sstevel@tonic-gate 			msg(EXT, "Unexpected end-of-file encountered.");
39900Sstevel@tonic-gate 	} else
39910Sstevel@tonic-gate 		msg(EXTN, "\007I/O error on \"%s\"", dir ? "output" : "input");
39920Sstevel@tonic-gate }
39930Sstevel@tonic-gate 
39940Sstevel@tonic-gate /*
39950Sstevel@tonic-gate  * matched: Determine if a filename matches the specified pattern(s).  If the
39960Sstevel@tonic-gate  * pattern is matched (the second return), return 0 if -f was specified, else
39970Sstevel@tonic-gate  * return != 0.  If the pattern is not matched (the first and third
39980Sstevel@tonic-gate  * returns), return 0 if -f was not specified, else return != 0.
39990Sstevel@tonic-gate  */
40000Sstevel@tonic-gate 
40010Sstevel@tonic-gate static int
40020Sstevel@tonic-gate matched(void)
40030Sstevel@tonic-gate {
40040Sstevel@tonic-gate 	char *str_p = G_p->g_nam_p;
40050Sstevel@tonic-gate 	char **pat_pp = Pat_pp;
40060Sstevel@tonic-gate 	int negatep, result;
40070Sstevel@tonic-gate 
40080Sstevel@tonic-gate 	/*
40090Sstevel@tonic-gate 	 * Check for attribute
40100Sstevel@tonic-gate 	 */
40110Sstevel@tonic-gate 	if (G_p->g_attrfnam_p != (char *)NULL)
40120Sstevel@tonic-gate 		str_p = G_p->g_attrfnam_p;
40130Sstevel@tonic-gate 
40140Sstevel@tonic-gate 	for (pat_pp = Pat_pp; *pat_pp; pat_pp++) {
40150Sstevel@tonic-gate 		negatep = (**pat_pp == '!');
40160Sstevel@tonic-gate 
40170Sstevel@tonic-gate 		result = fnmatch(negatep ? (*pat_pp+1) : *pat_pp, str_p, 0);
40180Sstevel@tonic-gate 
40190Sstevel@tonic-gate 		if (result != 0 && result != FNM_NOMATCH) {
40200Sstevel@tonic-gate 			msg(POST, "error matching file %s with pattern"
40210Sstevel@tonic-gate 			    " %s\n", str_p, *pat_pp);
40220Sstevel@tonic-gate 			return (Args & OCf);
40230Sstevel@tonic-gate 		}
40240Sstevel@tonic-gate 
40250Sstevel@tonic-gate 		if ((result == 0 && ! negatep) ||
40260Sstevel@tonic-gate 		    (result == FNM_NOMATCH && negatep)) {
40270Sstevel@tonic-gate 			/* match occured */
40280Sstevel@tonic-gate 			return (!(Args & OCf));
40290Sstevel@tonic-gate 		}
40300Sstevel@tonic-gate 	}
40310Sstevel@tonic-gate 	return (Args & OCf); /* not matched */
40320Sstevel@tonic-gate }
40330Sstevel@tonic-gate 
40340Sstevel@tonic-gate /*
40350Sstevel@tonic-gate  * missdir: Create missing directories for files.
40360Sstevel@tonic-gate  * (Possible future performance enhancement, if missdir is called, we know
40370Sstevel@tonic-gate  * that at least the very last directory of the path does not exist, therefore,
40380Sstevel@tonic-gate  * scan the path from the end
40390Sstevel@tonic-gate  */
40400Sstevel@tonic-gate 
40410Sstevel@tonic-gate static int
40420Sstevel@tonic-gate missdir(char *nam_p)
40430Sstevel@tonic-gate {
40440Sstevel@tonic-gate 	char *c_p;
40450Sstevel@tonic-gate 	int cnt = 2;
40460Sstevel@tonic-gate 	char *lastp;
40470Sstevel@tonic-gate 
40480Sstevel@tonic-gate 	if (*(c_p = nam_p) == '/') /* skip over 'root slash' */
40490Sstevel@tonic-gate 		c_p++;
40500Sstevel@tonic-gate 
40510Sstevel@tonic-gate 	lastp = c_p + strlen(nam_p) - 1;
40520Sstevel@tonic-gate 	if (*lastp == '/')
40530Sstevel@tonic-gate 		*lastp = '\0';
40540Sstevel@tonic-gate 
40550Sstevel@tonic-gate 	for (; *c_p; ++c_p) {
40560Sstevel@tonic-gate 		if (*c_p == '/') {
40570Sstevel@tonic-gate 			*c_p = '\0';
40580Sstevel@tonic-gate 			if (stat(nam_p, &DesSt) < 0) {
40590Sstevel@tonic-gate 				if (Args & OCd) {
40600Sstevel@tonic-gate 					cnt = mkdir(nam_p, Def_mode);
40610Sstevel@tonic-gate 					if (cnt != 0) {
40620Sstevel@tonic-gate 						*c_p = '/';
40630Sstevel@tonic-gate 						return (cnt);
40640Sstevel@tonic-gate 					}
40650Sstevel@tonic-gate 				} else {
40660Sstevel@tonic-gate 					msg(ERR, "Missing -d option.");
40670Sstevel@tonic-gate 					*c_p = '/';
40680Sstevel@tonic-gate 					return (-1);
40690Sstevel@tonic-gate 				}
40700Sstevel@tonic-gate 			}
40710Sstevel@tonic-gate 			*c_p = '/';
40720Sstevel@tonic-gate 		}
40730Sstevel@tonic-gate 	}
40740Sstevel@tonic-gate 	if (cnt == 2) /* the file already exists */
40750Sstevel@tonic-gate 		cnt = 0;
40760Sstevel@tonic-gate 	return (cnt);
40770Sstevel@tonic-gate }
40780Sstevel@tonic-gate 
40790Sstevel@tonic-gate /*
40800Sstevel@tonic-gate  * mklong: Convert two shorts into one long.  For VAX, Interdata ...
40810Sstevel@tonic-gate  */
40820Sstevel@tonic-gate 
40830Sstevel@tonic-gate static long
40840Sstevel@tonic-gate mklong(short v[])
40850Sstevel@tonic-gate {
40860Sstevel@tonic-gate 
40870Sstevel@tonic-gate 	union swpbuf swp_b;
40880Sstevel@tonic-gate 
40890Sstevel@tonic-gate 	swp_b.s_word = 1;
40900Sstevel@tonic-gate 	if (swp_b.s_byte[0]) {
40910Sstevel@tonic-gate 		swp_b.s_half[0] = v[1];
40920Sstevel@tonic-gate 		swp_b.s_half[1] = v[0];
40930Sstevel@tonic-gate 	} else {
40940Sstevel@tonic-gate 		swp_b.s_half[0] = v[0];
40950Sstevel@tonic-gate 		swp_b.s_half[1] = v[1];
40960Sstevel@tonic-gate 	}
40970Sstevel@tonic-gate 	return (swp_b.s_word);
40980Sstevel@tonic-gate }
40990Sstevel@tonic-gate 
41000Sstevel@tonic-gate /*
41010Sstevel@tonic-gate  * mkshort: Convert a long into 2 shorts, for VAX, Interdata ...
41020Sstevel@tonic-gate  */
41030Sstevel@tonic-gate 
41040Sstevel@tonic-gate static void
41050Sstevel@tonic-gate mkshort(short sval[], long v)
41060Sstevel@tonic-gate {
41070Sstevel@tonic-gate 	union swpbuf *swp_p, swp_b;
41080Sstevel@tonic-gate 
41090Sstevel@tonic-gate 	/* LINTED alignment */
41100Sstevel@tonic-gate 	swp_p = (union swpbuf *)sval;
41110Sstevel@tonic-gate 	swp_b.s_word = 1;
41120Sstevel@tonic-gate 	if (swp_b.s_byte[0]) {
41130Sstevel@tonic-gate 		swp_b.s_word = v;
41140Sstevel@tonic-gate 		swp_p->s_half[0] = swp_b.s_half[1];
41150Sstevel@tonic-gate 		swp_p->s_half[1] = swp_b.s_half[0];
41160Sstevel@tonic-gate 	} else {
41170Sstevel@tonic-gate 		swp_b.s_word = v;
41180Sstevel@tonic-gate 		swp_p->s_half[0] = swp_b.s_half[0];
41190Sstevel@tonic-gate 		swp_p->s_half[1] = swp_b.s_half[1];
41200Sstevel@tonic-gate 	}
41210Sstevel@tonic-gate }
41220Sstevel@tonic-gate 
41230Sstevel@tonic-gate /*
41240Sstevel@tonic-gate  * msg: Print either a message (no error) (POST), an error message with or
41250Sstevel@tonic-gate  * without the errno (ERRN or ERR), or print an error message with or without
41260Sstevel@tonic-gate  * the errno and exit (EXTN or EXT).
41270Sstevel@tonic-gate  */
41280Sstevel@tonic-gate 
41290Sstevel@tonic-gate static void
41300Sstevel@tonic-gate msg(int severity, const char *fmt, ...)
41310Sstevel@tonic-gate {
41320Sstevel@tonic-gate 	FILE *file_p;
41330Sstevel@tonic-gate 	va_list ap;
41340Sstevel@tonic-gate 
41350Sstevel@tonic-gate 	if ((Args & OCV) && Verbcnt) { /* clear current line of dots */
41360Sstevel@tonic-gate 		(void) fputc('\n', Out_p);
41370Sstevel@tonic-gate 		Verbcnt = 0;
41380Sstevel@tonic-gate 	}
41390Sstevel@tonic-gate 	va_start(ap, fmt);
41400Sstevel@tonic-gate 	if (severity == POST)
41410Sstevel@tonic-gate 		file_p = Out_p;
41420Sstevel@tonic-gate 	else
41430Sstevel@tonic-gate 		if (severity == EPOST)
41440Sstevel@tonic-gate 			file_p = Err_p;
41450Sstevel@tonic-gate 		else {
41460Sstevel@tonic-gate 			file_p = Err_p;
41470Sstevel@tonic-gate 			Error_cnt++;
41480Sstevel@tonic-gate 		}
41490Sstevel@tonic-gate 	(void) fflush(Out_p);
41500Sstevel@tonic-gate 	(void) fflush(Err_p);
41510Sstevel@tonic-gate 	if ((severity != POST) && (severity != EPOST))
41520Sstevel@tonic-gate 		(void) fprintf(file_p, "cpio: ");
41530Sstevel@tonic-gate 
41540Sstevel@tonic-gate 	/* gettext replaces version of string */
41550Sstevel@tonic-gate 
41560Sstevel@tonic-gate 	(void) vfprintf(file_p, gettext(fmt), ap);
41570Sstevel@tonic-gate 	if (severity == ERRN || severity == EXTN) {
41580Sstevel@tonic-gate 		(void) fprintf(file_p, ", errno %d, ", errno);
41590Sstevel@tonic-gate 		perror("");
41600Sstevel@tonic-gate 	} else
41610Sstevel@tonic-gate 		(void) fprintf(file_p, "\n");
41620Sstevel@tonic-gate 	(void) fflush(file_p);
41630Sstevel@tonic-gate 	va_end(ap);
41640Sstevel@tonic-gate 	if (severity == EXT || severity == EXTN) {
41650Sstevel@tonic-gate 		(void) fprintf(file_p, gettext("%d errors\n"), Error_cnt);
41660Sstevel@tonic-gate 		exit(EXIT_CODE);
41670Sstevel@tonic-gate 	}
41680Sstevel@tonic-gate }
41690Sstevel@tonic-gate 
41700Sstevel@tonic-gate /*
41710Sstevel@tonic-gate  * openout: Open files for output and set all necessary information.
41720Sstevel@tonic-gate  * If the u option is set (unconditionally overwrite existing files),
41730Sstevel@tonic-gate  * and the current file exists, get a temporary file name from mktemp(3C),
41740Sstevel@tonic-gate  * link the temporary file to the existing file, and remove the existing file.
41750Sstevel@tonic-gate  * Finally either creat(2), mkdir(2) or mknod(2) as appropriate.
41760Sstevel@tonic-gate  *
41770Sstevel@tonic-gate  */
41780Sstevel@tonic-gate 
41790Sstevel@tonic-gate static int
41800Sstevel@tonic-gate openout(int dirfd)
41810Sstevel@tonic-gate {
41820Sstevel@tonic-gate 	char *nam_p;
41830Sstevel@tonic-gate 	int cnt, result;
41840Sstevel@tonic-gate 
41850Sstevel@tonic-gate 	Do_rename = 0;	/* creat_tmp() may reset this */
41860Sstevel@tonic-gate 
41870Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
41880Sstevel@tonic-gate 		nam_p = G_p->g_attrnam_p;
41890Sstevel@tonic-gate 	} else {
41900Sstevel@tonic-gate 		if (Args & OCp) {
41910Sstevel@tonic-gate 			nam_p = Fullnam_p;
41920Sstevel@tonic-gate 		} else {
41930Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
41940Sstevel@tonic-gate 		}
41950Sstevel@tonic-gate 	}
41960Sstevel@tonic-gate 
41970Sstevel@tonic-gate 
41980Sstevel@tonic-gate 	if ((Max_filesz != RLIM_INFINITY) &&
41990Sstevel@tonic-gate 	    (Max_filesz < (G_p->g_filesz >> 9))) {
42000Sstevel@tonic-gate 		/* ... divided by 512 ... */
42010Sstevel@tonic-gate 		msg(ERR, "Skipping \"%s%s%s\": exceeds ulimit by %lld bytes",
42020Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
42030Sstevel@tonic-gate 		    nam_p : G_p->g_attrfnam_p,
42040Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
42050Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
42060Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
42070Sstevel@tonic-gate 		    "" : nam_p,
42080Sstevel@tonic-gate 		    (off_t)(G_p->g_filesz - (Max_filesz << 9)));
42090Sstevel@tonic-gate 		return (-1);
42100Sstevel@tonic-gate 	}
42110Sstevel@tonic-gate 
42120Sstevel@tonic-gate 	if (LSTAT(dirfd, nam_p, &DesSt) == 0) {
42130Sstevel@tonic-gate 		/*
42140Sstevel@tonic-gate 		 * A file by the same name exists.  Move it to a temporary
42150Sstevel@tonic-gate 		 * file.
42160Sstevel@tonic-gate 		 */
42170Sstevel@tonic-gate 
42180Sstevel@tonic-gate 		if (creat_tmp(nam_p) < 0) {
42190Sstevel@tonic-gate 			/*
42200Sstevel@tonic-gate 			 * We weren't able to create the temp file.  Report
42210Sstevel@tonic-gate 			 * failure.
42220Sstevel@tonic-gate 			 */
42230Sstevel@tonic-gate 
42240Sstevel@tonic-gate 			return (-1);
42250Sstevel@tonic-gate 		}
42260Sstevel@tonic-gate 	}
42270Sstevel@tonic-gate 
42280Sstevel@tonic-gate 	if (Do_rename) {
42290Sstevel@tonic-gate 		/* nam_p was changed by creat_tmp() above. */
42300Sstevel@tonic-gate 
42310Sstevel@tonic-gate 		if (Args & OCp) {
42320Sstevel@tonic-gate 			if (G_p->g_attrnam_p != (char *)NULL) {
42330Sstevel@tonic-gate 				nam_p = Attrfile_p;
42340Sstevel@tonic-gate 			} else {
42350Sstevel@tonic-gate 				nam_p = Fullnam_p;
42360Sstevel@tonic-gate 			}
42370Sstevel@tonic-gate 		} else {
42380Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
42390Sstevel@tonic-gate 		}
42400Sstevel@tonic-gate 	}
42410Sstevel@tonic-gate 
42420Sstevel@tonic-gate 	/*
42430Sstevel@tonic-gate 	 * This pile tries to create the file directly, and, if there is a
42440Sstevel@tonic-gate 	 * problem, creates missing directories, and then tries to create the
42450Sstevel@tonic-gate 	 * file again.  Two strikes and you're out.
42460Sstevel@tonic-gate 	 *
42470Sstevel@tonic-gate 	 * On XATTR system, the directory has already been created by
42480Sstevel@tonic-gate 	 * open_dirfd(), so error shouldn't happen in the loop. However,
42490Sstevel@tonic-gate 	 * on non-XATTR system, symlink/open may fail with ENOENT. In such
42500Sstevel@tonic-gate 	 * case, we go to create missing directories.
42510Sstevel@tonic-gate 	 */
42520Sstevel@tonic-gate 
42530Sstevel@tonic-gate 	cnt = 0;
42540Sstevel@tonic-gate 
42550Sstevel@tonic-gate 	do {
42560Sstevel@tonic-gate 		errno = 0;
42570Sstevel@tonic-gate 
42580Sstevel@tonic-gate 		if (Hdr_type == TAR && Thdr_p->tbuf.t_typeflag == SYMTYPE) {
42590Sstevel@tonic-gate 			/* The archive file is a TAR symlink. */
42600Sstevel@tonic-gate 			if ((result =
42610Sstevel@tonic-gate 			    symlink(Thdr_p->tbuf.t_linkname, nam_p)) >= 0) {
42620Sstevel@tonic-gate 				cnt = 0;
42630Sstevel@tonic-gate 				if (Over_p != NULL) {
42640Sstevel@tonic-gate 					(void) unlinkat(dirfd,
42650Sstevel@tonic-gate 					    get_component(Over_p), 0);
42660Sstevel@tonic-gate 					*Over_p = '\0';
42670Sstevel@tonic-gate 				}
42680Sstevel@tonic-gate 				break;
42690Sstevel@tonic-gate 			} else if (errno != ENOENT) {
42700Sstevel@tonic-gate 				/* The attempt to symlink failed. */
42710Sstevel@tonic-gate 				msg(ERRN,
42720Sstevel@tonic-gate 				    "Cannot create symbolic link \"%s\" -> "
42730Sstevel@tonic-gate 				    "\"%s\"",
42740Sstevel@tonic-gate 				    Thdr_p->tbuf.t_linkname, nam_p);
42750Sstevel@tonic-gate 
42760Sstevel@tonic-gate 				if (*Over_p != '\0') {
42770Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
42780Sstevel@tonic-gate 				}
42790Sstevel@tonic-gate 				return (-1);
42800Sstevel@tonic-gate 			}
42810Sstevel@tonic-gate 		} else if (Hdr_type == BAR && bar_linkflag == SYMTYPE) {
42820Sstevel@tonic-gate 			if ((result = symlink(bar_linkname, nam_p)) >= 0) {
42830Sstevel@tonic-gate 				cnt = 0;
42840Sstevel@tonic-gate 				if (Over_p != NULL) {
42850Sstevel@tonic-gate 					(void) unlinkat(dirfd,
42860Sstevel@tonic-gate 					    get_component(Over_p), 0);
42870Sstevel@tonic-gate 					*Over_p = '\0';
42880Sstevel@tonic-gate 				}
42890Sstevel@tonic-gate 				break;
42900Sstevel@tonic-gate 			} else if (errno != ENOENT) {
42910Sstevel@tonic-gate 				/* The attempt to symlink failed. */
42920Sstevel@tonic-gate 				msg(ERRN,
42930Sstevel@tonic-gate 				    "Cannot create symbolic link \"%s\" -> "
42940Sstevel@tonic-gate 				    "\"%s\"",
42950Sstevel@tonic-gate 				    bar_linkname, nam_p);
42960Sstevel@tonic-gate 				if (*Over_p != '\0') {
42970Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
42980Sstevel@tonic-gate 				}
42990Sstevel@tonic-gate 				return (-1);
43000Sstevel@tonic-gate 			}
43010Sstevel@tonic-gate 		} else if ((G_p->g_mode & Ftype) == S_IFLNK) {
43020Sstevel@tonic-gate 			if ((!(Args & OCp)) && !(Hdr_type == USTAR)) {
43030Sstevel@tonic-gate 				(void) strncpy(Symlnk_p,
43040Sstevel@tonic-gate 				    Buffr.b_out_p, G_p->g_filesz);
43050Sstevel@tonic-gate 				*(Symlnk_p + G_p->g_filesz) = '\0';
43060Sstevel@tonic-gate 			} else if ((!(Args & OCp)) && (Hdr_type == USTAR)) {
43070Sstevel@tonic-gate 				Symlnk_p[NAMSIZ] = '\0';
43080Sstevel@tonic-gate 				(void) strncpy(Symlnk_p,
43090Sstevel@tonic-gate 				    &Thdr_p->tbuf.t_linkname[0], NAMSIZ);
43100Sstevel@tonic-gate 			}
43110Sstevel@tonic-gate 			if ((result = symlink(Symlnk_p, nam_p)) >= 0) {
43120Sstevel@tonic-gate 				cnt = 0;
43130Sstevel@tonic-gate 				if (Over_p != NULL) {
43140Sstevel@tonic-gate 					(void) unlinkat(dirfd,
43150Sstevel@tonic-gate 					    get_component(Over_p), 0);
43160Sstevel@tonic-gate 					*Over_p = '\0';
43170Sstevel@tonic-gate 				}
43180Sstevel@tonic-gate 				break;
43190Sstevel@tonic-gate 			} else if (errno != ENOENT) {
43200Sstevel@tonic-gate 				/* The attempt to symlink failed. */
43210Sstevel@tonic-gate 				msg(ERRN,
43220Sstevel@tonic-gate 				    "Cannot create symbolic link \"%s\" -> "
43230Sstevel@tonic-gate 				    "\"%s\"",
43240Sstevel@tonic-gate 				    Symlnk_p, nam_p);
43250Sstevel@tonic-gate 
43260Sstevel@tonic-gate 				if (*Over_p != '\0') {
43270Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
43280Sstevel@tonic-gate 				}
43290Sstevel@tonic-gate 				return (-1);
43300Sstevel@tonic-gate 			}
43310Sstevel@tonic-gate 		} else {
43320Sstevel@tonic-gate 			if ((result = openat(dirfd, get_component(nam_p),
43330Sstevel@tonic-gate 			    O_CREAT|O_RDWR|O_TRUNC, (int)G_p->g_mode)) >= 0) {
43340Sstevel@tonic-gate 				/* acl support */
43350Sstevel@tonic-gate 				acl_set = 0;
43360Sstevel@tonic-gate 				if (Pflag && aclp != NULL) {
43370Sstevel@tonic-gate 					if (facl(result, SETACL, aclcnt, aclp)
43380Sstevel@tonic-gate 					    < 0) {
43390Sstevel@tonic-gate 						msg(ERRN,
43400Sstevel@tonic-gate 						    "\"%s\": failed to set acl",
43410Sstevel@tonic-gate 						    nam_p);
43420Sstevel@tonic-gate 					} else {
43430Sstevel@tonic-gate 						acl_set = 1;
43440Sstevel@tonic-gate 					}
43450Sstevel@tonic-gate 					free(aclp);
43460Sstevel@tonic-gate 					aclp = NULL;
43470Sstevel@tonic-gate 				}
43480Sstevel@tonic-gate 				cnt = 0;
43490Sstevel@tonic-gate 				break;
43500Sstevel@tonic-gate 			} else if (errno != ENOENT) {
43510Sstevel@tonic-gate 				/* The attempt to open failed. */
43520Sstevel@tonic-gate 				msg(ERRN, "Cannot open file \"%s\"", nam_p);
43530Sstevel@tonic-gate 				if (*Over_p != '\0') {
43540Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
43550Sstevel@tonic-gate 				}
43560Sstevel@tonic-gate 				return (-1);
43570Sstevel@tonic-gate 			}
43580Sstevel@tonic-gate 		}
43590Sstevel@tonic-gate 		cnt++;
43600Sstevel@tonic-gate 	} while (cnt < 2 && missdir(nam_p) == 0);
43610Sstevel@tonic-gate 
43620Sstevel@tonic-gate 	switch (cnt) {
43630Sstevel@tonic-gate 	case 0:
43640Sstevel@tonic-gate 		if ((Args & OCi) && (Hdr_type == USTAR)) {
43650Sstevel@tonic-gate 			setpasswd(nam_p);
43660Sstevel@tonic-gate 		}
43670Sstevel@tonic-gate 
43680Sstevel@tonic-gate 		if ((G_p->g_mode & Ftype) == S_IFLNK ||
43690Sstevel@tonic-gate 		    (Hdr_type == BAR && bar_linkflag == SYMTYPE)) {
43700Sstevel@tonic-gate 			if (fchownat(dirfd, get_component(nam_p),
43710Sstevel@tonic-gate 			    (int)G_p->g_uid, (int)G_p->g_gid,
43720Sstevel@tonic-gate 			    AT_SYMLINK_NOFOLLOW) < 0) {
43730Sstevel@tonic-gate 				if ((Euid == 0) || (Args & OCR)) {
43740Sstevel@tonic-gate 					msg(ERRN,
43750Sstevel@tonic-gate 					    "Error during chown() of "
43760Sstevel@tonic-gate 					    "\"%s%s%s\"",
43770Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
43780Sstevel@tonic-gate 					    nam_p : G_p->g_attrfnam_p,
43790Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
43800Sstevel@tonic-gate 					    "" : gettext(" Attribute "),
43810Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
43820Sstevel@tonic-gate 					    "" : nam_p);
43830Sstevel@tonic-gate 				}
43840Sstevel@tonic-gate 			}
43850Sstevel@tonic-gate 
43860Sstevel@tonic-gate 			break;
43870Sstevel@tonic-gate 		}
43880Sstevel@tonic-gate 
43890Sstevel@tonic-gate 		if (fchownat(dirfd, get_component(nam_p),
43900Sstevel@tonic-gate 		    (int)G_p->g_uid, (int)G_p->g_gid, 0) < 0) {
43910Sstevel@tonic-gate 			if ((Euid == 0) || (Args & OCR)) {
43920Sstevel@tonic-gate 				msg(ERRN,
43930Sstevel@tonic-gate 				    "Error during chown() of \"%s%s%s\"",
43940Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
43950Sstevel@tonic-gate 				    nam_p : G_p->g_attrfnam_p,
43960Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
43970Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
43980Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
43990Sstevel@tonic-gate 				    "" : nam_p);
44000Sstevel@tonic-gate 			}
44010Sstevel@tonic-gate 		}
44020Sstevel@tonic-gate 
44030Sstevel@tonic-gate 		break;
44040Sstevel@tonic-gate 
44050Sstevel@tonic-gate 	case 1:
44060Sstevel@tonic-gate 		if (Do_rename) {
44070Sstevel@tonic-gate 			msg(ERRN, "Cannot create directory for \"%s%s%s\"",
44080Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? Over_p :
44090Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
44100Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
44110Sstevel@tonic-gate 			    gettext(" Attribute "),
44120Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" : Over_p);
44130Sstevel@tonic-gate 		} else {
44140Sstevel@tonic-gate 			msg(ERRN, "Cannot create directory for \"%s%s%s\"",
44150Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? nam_p :
44160Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
44170Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
44180Sstevel@tonic-gate 			    gettext(" Attribute "),
44190Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" : nam_p);
44200Sstevel@tonic-gate 		}
44210Sstevel@tonic-gate 		break;
44220Sstevel@tonic-gate 
44230Sstevel@tonic-gate 	case 2:
44240Sstevel@tonic-gate 		if (Do_rename) {
44250Sstevel@tonic-gate 			msg(ERRN, "Cannot create \"%s%s%s\"",
44260Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? Over_p :
44270Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
44280Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
44290Sstevel@tonic-gate 			    gettext(" Attribute "),
44300Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
44310Sstevel@tonic-gate 			    Over_p);
44320Sstevel@tonic-gate 		} else {
44330Sstevel@tonic-gate 			msg(ERRN, "Cannot create \"%s%s%s\"",
44340Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? nam_p :
44350Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
44360Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
44370Sstevel@tonic-gate 			    gettext(" Attribute "),
44380Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
44390Sstevel@tonic-gate 			    nam_p);
44400Sstevel@tonic-gate 		}
44410Sstevel@tonic-gate 		break;
44420Sstevel@tonic-gate 
44430Sstevel@tonic-gate 	default:
44440Sstevel@tonic-gate 		msg(EXT, "Impossible case.");
44450Sstevel@tonic-gate 	}
44460Sstevel@tonic-gate 
44470Sstevel@tonic-gate 	Finished = 0;
44480Sstevel@tonic-gate 	return (result);
44490Sstevel@tonic-gate }
44500Sstevel@tonic-gate 
44510Sstevel@tonic-gate /*
44520Sstevel@tonic-gate  * read_hdr: Transfer headers from the selected format
44530Sstevel@tonic-gate  * in the archive I/O buffer to the generic structure.
44540Sstevel@tonic-gate  */
44550Sstevel@tonic-gate 
44560Sstevel@tonic-gate static
44570Sstevel@tonic-gate int
44580Sstevel@tonic-gate read_hdr(int hdr)
44590Sstevel@tonic-gate {
44600Sstevel@tonic-gate 	int rv = NONE;
44610Sstevel@tonic-gate 	major_t maj, rmaj;
44620Sstevel@tonic-gate 	minor_t min, rmin;
44630Sstevel@tonic-gate 	char tmpnull;
44640Sstevel@tonic-gate 	static int bar_read_cnt = 0;
44650Sstevel@tonic-gate 
44660Sstevel@tonic-gate 	if (hdr != BAR) {
44670Sstevel@tonic-gate 		if (Buffr.b_end_p != (Buffr.b_out_p + Hdrsz)) {
44680Sstevel@tonic-gate 			tmpnull = *(Buffr.b_out_p + Hdrsz);
44690Sstevel@tonic-gate 			*(Buffr.b_out_p + Hdrsz) = '\0';
44700Sstevel@tonic-gate 		}
44710Sstevel@tonic-gate 	}
44720Sstevel@tonic-gate 
44730Sstevel@tonic-gate 	switch (hdr) {
44740Sstevel@tonic-gate 	case BIN:
44750Sstevel@tonic-gate 		(void) memcpy(&Hdr, Buffr.b_out_p, HDRSZ);
44760Sstevel@tonic-gate 		if (Hdr.h_magic == (short)CMN_BBS) {
44770Sstevel@tonic-gate 			swap((char *)&Hdr, HDRSZ);
44780Sstevel@tonic-gate 		}
44790Sstevel@tonic-gate 		Gen.g_magic = Hdr.h_magic;
44800Sstevel@tonic-gate 		Gen.g_mode = Hdr.h_mode;
44810Sstevel@tonic-gate 		Gen.g_uid = Hdr.h_uid;
44820Sstevel@tonic-gate 		Gen.g_gid = Hdr.h_gid;
44830Sstevel@tonic-gate 		Gen.g_nlink = Hdr.h_nlink;
44840Sstevel@tonic-gate 		Gen.g_mtime = mklong(Hdr.h_mtime);
44850Sstevel@tonic-gate 		Gen.g_ino = Hdr.h_ino;
44860Sstevel@tonic-gate 		Gen.g_dev = Hdr.h_dev;
44870Sstevel@tonic-gate 		Gen.g_rdev = Hdr.h_rdev;
44880Sstevel@tonic-gate 		Gen.g_cksum = 0L;
44890Sstevel@tonic-gate 		Gen.g_filesz = (off_t)mklong(Hdr.h_filesize);
44900Sstevel@tonic-gate 		Gen.g_namesz = Hdr.h_namesize;
44910Sstevel@tonic-gate 		rv = BIN;
44920Sstevel@tonic-gate 		break;
44930Sstevel@tonic-gate 	case CHR:
44940Sstevel@tonic-gate 		if (sscanf(Buffr.b_out_p,
44950Sstevel@tonic-gate 		    "%6lo%6lo%6lo%6lo%6lo%6lo%6lo%6lo%11lo%6o%11llo",
44960Sstevel@tonic-gate 		    &Gen.g_magic, &Gen.g_dev, &Gen.g_ino, &Gen.g_mode,
44970Sstevel@tonic-gate 		    &Gen.g_uid, &Gen.g_gid, &Gen.g_nlink, &Gen.g_rdev,
44980Sstevel@tonic-gate 		    (ulong_t *)&Gen.g_mtime, (uint_t *)&Gen.g_namesz,
44990Sstevel@tonic-gate 		    (u_off_t *)&Gen.g_filesz) == CHR_CNT) {
45000Sstevel@tonic-gate 			rv = CHR;
45010Sstevel@tonic-gate #define	cpioMAJOR(x)	(int)(((unsigned)x >> 8) & 0x7F)
45020Sstevel@tonic-gate #define	cpioMINOR(x)	(int)(x & 0xFF)
45030Sstevel@tonic-gate 			maj = cpioMAJOR(Gen.g_dev);
45040Sstevel@tonic-gate 			rmaj = cpioMAJOR(Gen.g_rdev);
45050Sstevel@tonic-gate 			min = cpioMINOR(Gen.g_dev);
45060Sstevel@tonic-gate 			rmin = cpioMINOR(Gen.g_rdev);
45070Sstevel@tonic-gate 			if (Use_old_stat) {
45080Sstevel@tonic-gate 				/* needs error checking */
45090Sstevel@tonic-gate 				Gen.g_dev = (maj << 8) | min;
45100Sstevel@tonic-gate 				Gen.g_rdev = (rmaj << 8) | rmin;
45110Sstevel@tonic-gate 			} else {
45120Sstevel@tonic-gate 				Gen.g_dev = makedev(maj, min);
45130Sstevel@tonic-gate 				Gen.g_rdev = makedev(rmaj, rmin);
45140Sstevel@tonic-gate 			}
45150Sstevel@tonic-gate 		}
45160Sstevel@tonic-gate 		break;
45170Sstevel@tonic-gate 	case ASC:
45180Sstevel@tonic-gate 	case CRC:
45190Sstevel@tonic-gate 		if (sscanf(Buffr.b_out_p,
45200Sstevel@tonic-gate 		    "%6lx%8lx%8lx%8lx%8lx%8lx%8lx%8llx%8x%8x%8x%8x%8x%8lx",
45210Sstevel@tonic-gate 		    &Gen.g_magic, &Gen.g_ino, &Gen.g_mode, &Gen.g_uid,
45220Sstevel@tonic-gate 		    &Gen.g_gid, &Gen.g_nlink, &Gen.g_mtime,
45230Sstevel@tonic-gate 		    (u_off_t *)&Gen.g_filesz, (uint_t *)&maj, (uint_t *)&min,
45240Sstevel@tonic-gate 		    (uint_t *)&rmaj, (uint_t *)&rmin, (uint_t *)&Gen.g_namesz,
45250Sstevel@tonic-gate 		    &Gen.g_cksum) == ASC_CNT) {
45260Sstevel@tonic-gate 			Gen.g_dev = makedev(maj, min);
45270Sstevel@tonic-gate 			Gen.g_rdev = makedev(rmaj, rmin);
45280Sstevel@tonic-gate 			rv = hdr;
45290Sstevel@tonic-gate 		}
45300Sstevel@tonic-gate 		break;
45310Sstevel@tonic-gate 	case USTAR: /* TAR and USTAR */
45320Sstevel@tonic-gate 		if (*Buffr.b_out_p == '\0') {
45330Sstevel@tonic-gate 			*Gen.g_nam_p = '\0';
45340Sstevel@tonic-gate 			nambuf[0] = '\0';
45350Sstevel@tonic-gate 		} else {
45360Sstevel@tonic-gate 			Thdr_p = (union tblock *)Buffr.b_out_p;
45370Sstevel@tonic-gate 			Gen.g_nam_p[0] = '\0';
45380Sstevel@tonic-gate 			(void) strncpy((char *)&nambuf,
45390Sstevel@tonic-gate 			    Thdr_p->tbuf.t_name, NAMSIZ);
45400Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mode, "%8lo",
45410Sstevel@tonic-gate 			    &Gen.g_mode);
45420Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_uid, "%8lo", &Gen.g_uid);
45430Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_gid, "%8lo", &Gen.g_gid);
45440Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_size, "%11llo",
45450Sstevel@tonic-gate 			    (u_off_t *)&Gen.g_filesz);
45460Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mtime, "%12lo",
45470Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_mtime);
45480Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_cksum, "%8lo",
45490Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_cksum);
45500Sstevel@tonic-gate 			if (Thdr_p->tbuf.t_linkname[0] != (char)NULL)
45510Sstevel@tonic-gate 				Gen.g_nlink = 1;
45520Sstevel@tonic-gate 			else
45530Sstevel@tonic-gate 				Gen.g_nlink = 0;
45540Sstevel@tonic-gate 
45550Sstevel@tonic-gate 			switch (Thdr_p->tbuf.t_typeflag) {
45560Sstevel@tonic-gate 			case SYMTYPE:
45570Sstevel@tonic-gate 				/* Symbolic Link */
45580Sstevel@tonic-gate 				Gen.g_nlink = 2;
45590Sstevel@tonic-gate 				break;
45600Sstevel@tonic-gate 			case CHRTYPE:
45610Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFCHR);
45620Sstevel@tonic-gate 				break;
45630Sstevel@tonic-gate 			case BLKTYPE:
45640Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFBLK);
45650Sstevel@tonic-gate 				break;
45660Sstevel@tonic-gate 			case DIRTYPE:
45670Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFDIR);
45680Sstevel@tonic-gate 				break;
45690Sstevel@tonic-gate 			case FIFOTYPE:
45700Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFIFO);
45710Sstevel@tonic-gate 				break;
45720Sstevel@tonic-gate 			}
45730Sstevel@tonic-gate 
45740Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_magic, "%8lo",
45750Sstevel@tonic-gate 			    /* LINTED alignment */
45760Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_tmagic);
45770Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_version, "%8lo",
45780Sstevel@tonic-gate 			    /* LINTED alignment */
45790Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_version);
45800Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_uname, "%32s",
45810Sstevel@tonic-gate 			    (char *)&Gen.g_uname);
45820Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_gname, "%32s",
45830Sstevel@tonic-gate 			    (char *)&Gen.g_gname);
45840Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_devmajor, "%8lo",
45850Sstevel@tonic-gate 			    &Gen.g_dev);
45860Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_devminor, "%8lo",
45870Sstevel@tonic-gate 			    &Gen.g_rdev);
45880Sstevel@tonic-gate 			(void) strncpy((char *)&prebuf,
45890Sstevel@tonic-gate 			    Thdr_p->tbuf.t_prefix, PRESIZ);
45900Sstevel@tonic-gate 			Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
45910Sstevel@tonic-gate 			Gen.g_dev = makedev(maj, min);
45920Sstevel@tonic-gate 		}
45930Sstevel@tonic-gate 		rv = USTAR;
45940Sstevel@tonic-gate 		break;
45950Sstevel@tonic-gate 	case TAR:
45960Sstevel@tonic-gate 		if (*Buffr.b_out_p == '\0') {
45970Sstevel@tonic-gate 			*Gen.g_nam_p = '\0';
45980Sstevel@tonic-gate 			nambuf[0] = '\0';
45990Sstevel@tonic-gate 		} else {
46000Sstevel@tonic-gate 			Thdr_p = (union tblock *)Buffr.b_out_p;
46010Sstevel@tonic-gate 			Gen.g_nam_p[0] = '\0';
46020Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mode, "%lo", &Gen.g_mode);
46030Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_uid, "%lo", &Gen.g_uid);
46040Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_gid, "%lo", &Gen.g_gid);
46050Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_size, "%llo",
46060Sstevel@tonic-gate 			    (u_off_t *)&Gen.g_filesz);
46070Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mtime, "%lo",
46080Sstevel@tonic-gate 			    &Gen.g_mtime);
46090Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_cksum, "%lo",
46100Sstevel@tonic-gate 			    &Gen.g_cksum);
46110Sstevel@tonic-gate 			if (Thdr_p->tbuf.t_typeflag == '1')	/* hardlink */
46120Sstevel@tonic-gate 				Gen.g_nlink = 1;
46130Sstevel@tonic-gate 			else
46140Sstevel@tonic-gate 				Gen.g_nlink = 0;
46150Sstevel@tonic-gate 			(void) strncpy(Gen.g_nam_p,
46160Sstevel@tonic-gate 			    Thdr_p->tbuf.t_name, NAMSIZ);
46170Sstevel@tonic-gate 			Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
46180Sstevel@tonic-gate 			(void) strcpy(nambuf, Gen.g_nam_p);
46190Sstevel@tonic-gate 		}
46200Sstevel@tonic-gate 		rv = TAR;
46210Sstevel@tonic-gate 		break;
46220Sstevel@tonic-gate 	case BAR:
46230Sstevel@tonic-gate 		if (Bar_vol_num == 0 && bar_read_cnt == 0) {
46240Sstevel@tonic-gate 			read_bar_vol_hdr();
46250Sstevel@tonic-gate 			bar_read_cnt++;
46260Sstevel@tonic-gate 		}
46270Sstevel@tonic-gate 		else
46280Sstevel@tonic-gate 			read_bar_file_hdr();
46290Sstevel@tonic-gate 		rv = BAR;
46300Sstevel@tonic-gate 		break;
46310Sstevel@tonic-gate 	default:
46320Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
46330Sstevel@tonic-gate 	}
46340Sstevel@tonic-gate 
46350Sstevel@tonic-gate 	if (hdr != BAR) {
46360Sstevel@tonic-gate 		if (Buffr.b_end_p != (Buffr.b_out_p + Hdrsz))
46370Sstevel@tonic-gate 			*(Buffr.b_out_p + Hdrsz) = tmpnull;
46380Sstevel@tonic-gate 	}
46390Sstevel@tonic-gate 
46400Sstevel@tonic-gate 	return (rv);
46410Sstevel@tonic-gate }
46420Sstevel@tonic-gate 
46430Sstevel@tonic-gate /*
46440Sstevel@tonic-gate  * reclaim: Reclaim linked file structure storage.
46450Sstevel@tonic-gate  */
46460Sstevel@tonic-gate 
46470Sstevel@tonic-gate static void
46480Sstevel@tonic-gate reclaim(struct Lnk *p)
46490Sstevel@tonic-gate {
46500Sstevel@tonic-gate 	p->L_bck_p->L_nxt_p = p->L_nxt_p;
46510Sstevel@tonic-gate 	p->L_nxt_p->L_bck_p = p->L_bck_p;
46520Sstevel@tonic-gate 
46530Sstevel@tonic-gate 	while (p != NULL) {
46540Sstevel@tonic-gate 		struct Lnk *new_p = p->L_lnk_p;
46550Sstevel@tonic-gate 
46560Sstevel@tonic-gate 		free(p->L_gen.g_nam_p);
46570Sstevel@tonic-gate 		free(p);
46580Sstevel@tonic-gate 		p = new_p;
46590Sstevel@tonic-gate 	}
46600Sstevel@tonic-gate }
46610Sstevel@tonic-gate 
46620Sstevel@tonic-gate /*
46630Sstevel@tonic-gate  * rstbuf: Reset the I/O buffer, move incomplete potential headers to
46640Sstevel@tonic-gate  * the front of the buffer and force bread() to refill the buffer.  The
46650Sstevel@tonic-gate  * return value from bread() is returned (to identify I/O errors).  On the
46660Sstevel@tonic-gate  * 3B2, reads must begin on a word boundary, therefore, with the -i option,
46670Sstevel@tonic-gate  * any remaining bytes in the buffer must be moved to the base of the buffer
46680Sstevel@tonic-gate  * in such a way that the destination locations of subsequent reads are
46690Sstevel@tonic-gate  * word aligned.
46700Sstevel@tonic-gate  */
46710Sstevel@tonic-gate 
46720Sstevel@tonic-gate static void
46730Sstevel@tonic-gate rstbuf(void)
46740Sstevel@tonic-gate {
46750Sstevel@tonic-gate 	int pad;
46760Sstevel@tonic-gate 
46770Sstevel@tonic-gate 	if ((Args & OCi) || Append) {
46780Sstevel@tonic-gate 		if (Buffr.b_out_p != Buffr.b_base_p) {
46790Sstevel@tonic-gate 			pad = ((Buffr.b_cnt + FULLWD) & ~FULLWD);
46800Sstevel@tonic-gate 			Buffr.b_in_p = Buffr.b_base_p + pad;
46810Sstevel@tonic-gate 			pad -= Buffr.b_cnt;
46820Sstevel@tonic-gate 			(void) memcpy(Buffr.b_base_p + pad, Buffr.b_out_p,
46830Sstevel@tonic-gate 			    (int)Buffr.b_cnt);
46840Sstevel@tonic-gate 			Buffr.b_out_p = Buffr.b_base_p + pad;
46850Sstevel@tonic-gate 		}
46860Sstevel@tonic-gate 		if (bfill() < 0)
46870Sstevel@tonic-gate 			msg(EXT, "Unexpected end-of-archive encountered.");
46880Sstevel@tonic-gate 	} else { /* OCo */
46890Sstevel@tonic-gate 		(void) memcpy(Buffr.b_base_p, Buffr.b_out_p, (int)Buffr.b_cnt);
46900Sstevel@tonic-gate 		Buffr.b_out_p = Buffr.b_base_p;
46910Sstevel@tonic-gate 		Buffr.b_in_p = Buffr.b_base_p + Buffr.b_cnt;
46920Sstevel@tonic-gate 	}
46930Sstevel@tonic-gate }
46940Sstevel@tonic-gate 
46950Sstevel@tonic-gate static void
46960Sstevel@tonic-gate setpasswd(char *nam)
46970Sstevel@tonic-gate {
46980Sstevel@tonic-gate 	if ((dpasswd = getpwnam(&Gen.g_uname[0])) == (struct passwd *)NULL) {
46990Sstevel@tonic-gate 		msg(EPOST, "cpio: problem reading passwd entry");
47000Sstevel@tonic-gate 		msg(EPOST, "cpio: %s: owner not changed", nam);
47010Sstevel@tonic-gate 		if (Gen.g_uid == UID_NOBODY && S_ISREG(Gen.g_mode))
47020Sstevel@tonic-gate 			Gen.g_mode &= ~S_ISUID;
47030Sstevel@tonic-gate 	} else
47040Sstevel@tonic-gate 		Gen.g_uid = dpasswd->pw_uid;
47050Sstevel@tonic-gate 
47060Sstevel@tonic-gate 	if ((dgroup = getgrnam(&Gen.g_gname[0])) == (struct group *)NULL) {
47070Sstevel@tonic-gate 		msg(EPOST, "cpio: problem reading group entry");
47080Sstevel@tonic-gate 		msg(EPOST, "cpio: %s: group not changed", nam);
47090Sstevel@tonic-gate 		if (Gen.g_gid == GID_NOBODY && S_ISREG(Gen.g_mode))
47100Sstevel@tonic-gate 			Gen.g_mode &= ~S_ISGID;
47110Sstevel@tonic-gate 	} else
47120Sstevel@tonic-gate 		Gen.g_gid = dgroup->gr_gid;
47130Sstevel@tonic-gate 	G_p = &Gen;
47140Sstevel@tonic-gate }
47150Sstevel@tonic-gate 
47160Sstevel@tonic-gate /*
47170Sstevel@tonic-gate  * rstfiles:  Perform final changes to the file.  If the -u option is set,
47180Sstevel@tonic-gate  * and overwrite == U_OVER, remove the temporary file, else if overwrite
47190Sstevel@tonic-gate  * == U_KEEP, unlink the current file, and restore the existing version
47200Sstevel@tonic-gate  * of the file.  In addition, where appropriate, set the access or modification
47210Sstevel@tonic-gate  * times, change the owner and change the modes of the file.
47220Sstevel@tonic-gate  *
47230Sstevel@tonic-gate  * Note that if Do_rename is set, then the roles of original and temporary
47240Sstevel@tonic-gate  * file are reversed. If all went well, we will rename() the temporary file
47250Sstevel@tonic-gate  * over the original in order to accomodate potentially executing files.
47260Sstevel@tonic-gate  */
47270Sstevel@tonic-gate static void
47280Sstevel@tonic-gate rstfiles(int over, int dirfd)
47290Sstevel@tonic-gate {
47300Sstevel@tonic-gate 	char *inam_p, *onam_p, *nam_p;
47310Sstevel@tonic-gate 	int error;
47320Sstevel@tonic-gate 
47330Sstevel@tonic-gate 	if (Args & OCp) {
47340Sstevel@tonic-gate 		if (G_p->g_attrnam_p == (char *)NULL) {
47350Sstevel@tonic-gate 			nam_p = Fullnam_p;
47360Sstevel@tonic-gate 		} else {
47370Sstevel@tonic-gate 			nam_p = G_p->g_attrnam_p;
47380Sstevel@tonic-gate 		}
47390Sstevel@tonic-gate 	} else {
47400Sstevel@tonic-gate 		if (Gen.g_nlink > (ulong_t)0) {
47410Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
47420Sstevel@tonic-gate 		} else {
47430Sstevel@tonic-gate 			nam_p = Gen.g_nam_p;
47440Sstevel@tonic-gate 		}
47450Sstevel@tonic-gate 	}
47460Sstevel@tonic-gate 	if (Gen.g_attrnam_p != (char *)NULL) {
47470Sstevel@tonic-gate 		nam_p = Gen.g_attrnam_p;
47480Sstevel@tonic-gate 	}
47490Sstevel@tonic-gate 
47500Sstevel@tonic-gate 	if ((Args & OCi) && (Hdr_type == USTAR)) {
47510Sstevel@tonic-gate 		setpasswd(nam_p);
47520Sstevel@tonic-gate 	}
47530Sstevel@tonic-gate 	if (over == U_KEEP && *Over_p != '\0') {
47540Sstevel@tonic-gate 		if (Do_rename) {
47550Sstevel@tonic-gate 			msg(POST, "Restoring existing \"%s%s%s\"",
47560Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
47570Sstevel@tonic-gate 			    Over_p : Fullnam_p,
47580Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
47590Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
47600Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
47610Sstevel@tonic-gate 			    "" : Over_p);
47620Sstevel@tonic-gate 		} else {
47630Sstevel@tonic-gate 			msg(POST, "Restoring existing \"%s%s%s\"",
47640Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
47650Sstevel@tonic-gate 			    nam_p : Fullnam_p,
47660Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
47670Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
47680Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
47690Sstevel@tonic-gate 			    "" : nam_p);
47700Sstevel@tonic-gate 		}
47710Sstevel@tonic-gate 
47720Sstevel@tonic-gate 		/* delete what we just built */
47730Sstevel@tonic-gate 		(void) unlinkat(dirfd, get_component(nam_p), 0);
47740Sstevel@tonic-gate 
47750Sstevel@tonic-gate 		/* If the old file needs restoring, do the necessary links */
47760Sstevel@tonic-gate 		if (Do_rename) {
47770Sstevel@tonic-gate 			char *tmp_ptr;
47780Sstevel@tonic-gate 
47790Sstevel@tonic-gate 			if (Args & OCp) {
47800Sstevel@tonic-gate 				tmp_ptr = Fullnam_p;
47810Sstevel@tonic-gate 				Fullnam_p = Over_p;
47820Sstevel@tonic-gate 			} else {
47830Sstevel@tonic-gate 				tmp_ptr = G_p->g_nam_p;
47840Sstevel@tonic-gate 				G_p->g_nam_p = Over_p;
47850Sstevel@tonic-gate 			}
47860Sstevel@tonic-gate 			Over_p = tmp_ptr;
47870Sstevel@tonic-gate 
47880Sstevel@tonic-gate 			Do_rename = 0;	/* names now have original values */
47890Sstevel@tonic-gate 		} else {
47900Sstevel@tonic-gate 			if (rename(Over_p, nam_p) < 0) {
47910Sstevel@tonic-gate 				if (link(Over_p, nam_p) < 0) {
47920Sstevel@tonic-gate 					msg(EXTN,
47930Sstevel@tonic-gate 					    "Cannot recover original version"
47940Sstevel@tonic-gate 					    " of \"%s%s%s\"",
47950Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
47960Sstevel@tonic-gate 					    nam_p : Fullnam_p,
47970Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
47980Sstevel@tonic-gate 					    "" : gettext(" Attribute "),
47990Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
48000Sstevel@tonic-gate 					    "" : nam_p);
48010Sstevel@tonic-gate 				}
48020Sstevel@tonic-gate 				if (unlinkat(dirfd, get_component(Over_p), 0)) {
48030Sstevel@tonic-gate 					msg(ERRN,
48040Sstevel@tonic-gate 					    "Cannot remove temp file "
48050Sstevel@tonic-gate 					    "\"%s%s%s\"",
48060Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
48070Sstevel@tonic-gate 					    Over_p : Fullnam_p,
48080Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
48090Sstevel@tonic-gate 					    "" : gettext(" Attribute "),
48100Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
48110Sstevel@tonic-gate 					    "" : Over_p);
48120Sstevel@tonic-gate 				}
48130Sstevel@tonic-gate 			}
48140Sstevel@tonic-gate 		}
48150Sstevel@tonic-gate 		*Over_p = '\0';
48160Sstevel@tonic-gate 		return;
48170Sstevel@tonic-gate 	} else if (over == U_OVER && *Over_p != '\0') {
48180Sstevel@tonic-gate 		if (Do_rename) {
48190Sstevel@tonic-gate 			char *tmp_ptr;
48200Sstevel@tonic-gate 
48210Sstevel@tonic-gate 			(void) renameat(dirfd, get_component(nam_p),
48220Sstevel@tonic-gate 			    dirfd, get_component(Over_p));
48230Sstevel@tonic-gate 			if (Args & OCp) {
48240Sstevel@tonic-gate 				if (G_p->g_attrnam_p == (char *)NULL) {
48250Sstevel@tonic-gate 					tmp_ptr = Fullnam_p;
48260Sstevel@tonic-gate 					Fullnam_p = Over_p;
48270Sstevel@tonic-gate 					Over_p = tmp_ptr;
48280Sstevel@tonic-gate 				} else {
48290Sstevel@tonic-gate 					/*
48300Sstevel@tonic-gate 					 * Over_p is pointing at g_attrnam_p
48310Sstevel@tonic-gate 					 * which must be preserved.
48320Sstevel@tonic-gate 					 *
48330Sstevel@tonic-gate 					 * We don't want the tmp_ptr and so
48340Sstevel@tonic-gate 					 * on to throw away our only copy of
48350Sstevel@tonic-gate 					 * the name.
48360Sstevel@tonic-gate 					 */
48370Sstevel@tonic-gate 					Over_p = Attrfile_p;
48380Sstevel@tonic-gate 				}
48390Sstevel@tonic-gate 			} else {
48400Sstevel@tonic-gate 				tmp_ptr = G_p->g_nam_p;
48410Sstevel@tonic-gate 				G_p->g_nam_p = Over_p;
48420Sstevel@tonic-gate 				Over_p = tmp_ptr;
48430Sstevel@tonic-gate 			}
48440Sstevel@tonic-gate 			Do_rename = 0;	/* names now have original values */
48450Sstevel@tonic-gate 		} else {
48460Sstevel@tonic-gate 			if (unlinkat(dirfd, get_component(Over_p), 0) < 0) {
48470Sstevel@tonic-gate 				msg(ERRN,
48480Sstevel@tonic-gate 				    "Cannot unlink() temp file \"%s%s%s\"",
48490Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
48500Sstevel@tonic-gate 				    Over_p : Fullnam_p,
48510Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
48520Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
48530Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
48540Sstevel@tonic-gate 				    "" : Over_p);
48550Sstevel@tonic-gate 			}
48560Sstevel@tonic-gate 		}
48570Sstevel@tonic-gate 		*Over_p = '\0';
48580Sstevel@tonic-gate 	}
48590Sstevel@tonic-gate 	if (Args & OCp) {
48600Sstevel@tonic-gate 		if (G_p->g_attrnam_p != (char *)NULL) {
48610Sstevel@tonic-gate 			inam_p = G_p->g_attrfnam_p;
48620Sstevel@tonic-gate 			onam_p = G_p->g_attrnam_p;
48630Sstevel@tonic-gate 		} else {
48640Sstevel@tonic-gate 			inam_p = Nam_p;
48650Sstevel@tonic-gate 			onam_p = Fullnam_p;
48660Sstevel@tonic-gate 		}
48670Sstevel@tonic-gate 	} else /* OCi only uses onam_p, OCo only uses inam_p */
48680Sstevel@tonic-gate 		if (G_p->g_attrnam_p != (char *)NULL) {
48690Sstevel@tonic-gate 			inam_p = onam_p = G_p->g_attrnam_p;
48700Sstevel@tonic-gate 		} else {
48710Sstevel@tonic-gate 			inam_p = onam_p = G_p->g_nam_p;
48720Sstevel@tonic-gate 		}
48730Sstevel@tonic-gate 
48740Sstevel@tonic-gate 	/*
48750Sstevel@tonic-gate 	 * change the modes back to the ones originally created in the
48760Sstevel@tonic-gate 	 * archive
48770Sstevel@tonic-gate 	 */
48780Sstevel@tonic-gate 	if (Args & OCi) {
48790Sstevel@tonic-gate 		mode_t orig_mask, new_mask;
48800Sstevel@tonic-gate 		struct stat sbuf;
48810Sstevel@tonic-gate 
48820Sstevel@tonic-gate 		if (!(Pflag && acl_set)) {
48830Sstevel@tonic-gate 			/* Acl was not set, so we must chmod */
48840Sstevel@tonic-gate 			if (LSTAT(dirfd, G_p->g_nam_p, &sbuf) == 0) {
48850Sstevel@tonic-gate 				if ((sbuf.st_mode & Ftype) != S_IFLNK) {
48860Sstevel@tonic-gate 					orig_mask = umask(0);
48870Sstevel@tonic-gate 					new_mask = G_p->g_mode & ~orig_mask;
48880Sstevel@tonic-gate 
48890Sstevel@tonic-gate 					/*
48900Sstevel@tonic-gate 					 * use fchmod for attributes, since
48910Sstevel@tonic-gate 					 * we known they are always regular
48920Sstevel@tonic-gate 					 * files, whereas when it isn't an
48930Sstevel@tonic-gate 					 * attribute it could be for a fifo
48940Sstevel@tonic-gate 					 * or something other that we don't
48950Sstevel@tonic-gate 					 * open and don't have a valid Ofile
48960Sstevel@tonic-gate 					 * for.
48970Sstevel@tonic-gate 					 */
48980Sstevel@tonic-gate 					if (G_p->g_attrnam_p != (char *)NULL) {
48990Sstevel@tonic-gate 						error = fchmod(Ofile, new_mask);
49000Sstevel@tonic-gate 					} else {
49010Sstevel@tonic-gate 						error = chmod(G_p->g_nam_p,
49020Sstevel@tonic-gate 							new_mask);
49030Sstevel@tonic-gate 					}
49040Sstevel@tonic-gate 					if (error < 0) {
49050Sstevel@tonic-gate 						msg(ERRN,
49060Sstevel@tonic-gate 						    "Cannot chmod() \"%s%s%s\"",
49070Sstevel@tonic-gate 						    (G_p->g_attrnam_p ==
49080Sstevel@tonic-gate 						    (char *)NULL) ?
49090Sstevel@tonic-gate 						    G_p->g_nam_p :
49100Sstevel@tonic-gate 						    G_p->g_attrfnam_p,
49110Sstevel@tonic-gate 						    (G_p->g_attrnam_p ==
49120Sstevel@tonic-gate 						    (char *)NULL) ?
49130Sstevel@tonic-gate 						    "" : gettext(
49140Sstevel@tonic-gate 						    " Attribute "),
49150Sstevel@tonic-gate 						    (G_p->g_attrnam_p ==
49160Sstevel@tonic-gate 						    (char *)NULL) ?
49170Sstevel@tonic-gate 						    "" : G_p->g_attrnam_p);
49180Sstevel@tonic-gate 					}
49190Sstevel@tonic-gate 					(void) umask(orig_mask);
49200Sstevel@tonic-gate 				}
49210Sstevel@tonic-gate 			}
49220Sstevel@tonic-gate 		}
49230Sstevel@tonic-gate 	}
49240Sstevel@tonic-gate 
49250Sstevel@tonic-gate 	if (!(Args & OCo)) {
49260Sstevel@tonic-gate 		if (Args & OCm) {
49270Sstevel@tonic-gate 			set_tym(dirfd, get_component(onam_p),
49280Sstevel@tonic-gate 			    G_p->g_mtime, G_p->g_mtime);
49290Sstevel@tonic-gate 		}
49300Sstevel@tonic-gate 		if (!acl_set) {
49310Sstevel@tonic-gate 			if (G_p->g_attrnam_p != (char *)NULL) {
49320Sstevel@tonic-gate 				error = fchmod(Ofile, (int)G_p->g_mode);
49330Sstevel@tonic-gate 			} else {
49340Sstevel@tonic-gate 				error = chmod(onam_p, (int)G_p->g_mode);
49350Sstevel@tonic-gate 			}
49360Sstevel@tonic-gate 			if ((error < 0) && ((Euid == 0) || (Args & OCR))) {
49370Sstevel@tonic-gate 				msg(ERRN, "Cannot chmod() \"%s%s%s\"", onam_p,
49380Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
49390Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
49400Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
49410Sstevel@tonic-gate 				    "" : onam_p);
49420Sstevel@tonic-gate 			}
49430Sstevel@tonic-gate 		}
49440Sstevel@tonic-gate 		if ((Args & OCR) &&
49450Sstevel@tonic-gate 		    (fchownat(dirfd, get_component(onam_p), Rpw_p->pw_uid,
49460Sstevel@tonic-gate 			Rpw_p->pw_gid, 0) < 0)) {
49470Sstevel@tonic-gate 			msg(ERRN, "Cannot chown() \"%s%s%s\"",
49480Sstevel@tonic-gate 			    onam_p,
49490Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
49500Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
49510Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
49520Sstevel@tonic-gate 			    "" : onam_p);
49530Sstevel@tonic-gate 		}
49540Sstevel@tonic-gate 	}
49550Sstevel@tonic-gate 
49560Sstevel@tonic-gate 	if (!(Args & OCi) && (Args & OCa)) {
49570Sstevel@tonic-gate 		/*
49580Sstevel@tonic-gate 		 * Use dirfd since we are updating original file
49590Sstevel@tonic-gate 		 * and not just created file
49600Sstevel@tonic-gate 		 */
49610Sstevel@tonic-gate 		set_tym(G_p->g_dirfd, get_component(inam_p),
49620Sstevel@tonic-gate 		    (ulong_t)SrcSt.st_atime, (ulong_t)SrcSt.st_mtime);
49630Sstevel@tonic-gate 	}
49640Sstevel@tonic-gate 
49650Sstevel@tonic-gate 	if ((Args & OCp) && !(Args & OCR)) {
49660Sstevel@tonic-gate 		if (fchownat(dirfd, get_component(onam_p),
49670Sstevel@tonic-gate 		    G_p->g_uid, G_p->g_gid, 0) < 0) {
49680Sstevel@tonic-gate 			if (Euid == 0) {
49690Sstevel@tonic-gate 				msg(ERRN, "Cannot chown() \"%s%s%s\"",
49700Sstevel@tonic-gate 				    onam_p,
49710Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
49720Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
49730Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
49740Sstevel@tonic-gate 				    "" : onam_p);
49750Sstevel@tonic-gate 			}
49760Sstevel@tonic-gate 		}
49770Sstevel@tonic-gate 	} else {
49780Sstevel@tonic-gate 		/* OCi only uses onam_p, OCo only uses inam_p */
49790Sstevel@tonic-gate 		if (!(Args & OCR)) {
49800Sstevel@tonic-gate 			if ((Args & OCi) && (fchownat(dirfd,
49810Sstevel@tonic-gate 			    get_component(inam_p), G_p->g_uid,
49820Sstevel@tonic-gate 			    G_p->g_gid, 0) < 0)) {
49830Sstevel@tonic-gate 				if (Euid == 0) {
49840Sstevel@tonic-gate 					msg(ERRN, "Cannot chown() \"%s%s%s\"",
49850Sstevel@tonic-gate 					    onam_p,
49860Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
49870Sstevel@tonic-gate 					    "" : gettext(" Attribute "),
49880Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
49890Sstevel@tonic-gate 					    "" : onam_p);
49900Sstevel@tonic-gate 				}
49910Sstevel@tonic-gate 			}
49920Sstevel@tonic-gate 		}
49930Sstevel@tonic-gate 	}
49940Sstevel@tonic-gate }
49950Sstevel@tonic-gate 
49960Sstevel@tonic-gate /*
49970Sstevel@tonic-gate  * scan4trail: Scan the archive looking for the trailer.
49980Sstevel@tonic-gate  * When found, back the archive up over the trailer and overwrite
49990Sstevel@tonic-gate  * the trailer with the files to be added to the archive.
50000Sstevel@tonic-gate  */
50010Sstevel@tonic-gate 
50020Sstevel@tonic-gate static void
50030Sstevel@tonic-gate scan4trail(void)
50040Sstevel@tonic-gate {
50050Sstevel@tonic-gate 	int rv;
50060Sstevel@tonic-gate 	off_t off1, off2;
50070Sstevel@tonic-gate 
50080Sstevel@tonic-gate 	Append = 1;
50090Sstevel@tonic-gate 	Hdr_type = NONE;
50100Sstevel@tonic-gate 	G_p = (struct gen_hdr *)NULL;
50110Sstevel@tonic-gate 	while (gethdr()) {
50120Sstevel@tonic-gate 		G_p = &Gen;
50130Sstevel@tonic-gate 		data_in(P_SKIP);
50140Sstevel@tonic-gate 	}
50150Sstevel@tonic-gate 	off1 = Buffr.b_cnt;
50160Sstevel@tonic-gate 	off2 = Bufsize - (Buffr.b_cnt % Bufsize);
50170Sstevel@tonic-gate 	Buffr.b_out_p = Buffr.b_in_p = Buffr.b_base_p;
50180Sstevel@tonic-gate 	Buffr.b_cnt = (off_t)0;
50190Sstevel@tonic-gate 	if (lseek(Archive, -(off1 + off2), SEEK_REL) < 0)
50200Sstevel@tonic-gate 		msg(EXTN, "Unable to append to this archive");
50210Sstevel@tonic-gate 	if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0)
50220Sstevel@tonic-gate 		msg(EXTN, "Cannot append to this archive");
50230Sstevel@tonic-gate 	if (lseek(Archive, (off_t)-rv, SEEK_REL) < 0)
50240Sstevel@tonic-gate 		msg(EXTN, "Unable to append to this archive");
50250Sstevel@tonic-gate 	Buffr.b_cnt = off2;
50260Sstevel@tonic-gate 	Buffr.b_in_p = Buffr.b_base_p + Buffr.b_cnt;
50270Sstevel@tonic-gate 	Append = 0;
50280Sstevel@tonic-gate }
50290Sstevel@tonic-gate 
50300Sstevel@tonic-gate /*
50310Sstevel@tonic-gate  * setup:  Perform setup and initialization functions.  Parse the options
50320Sstevel@tonic-gate  * using getopt(3C), call ckopts to check the options and initialize various
50330Sstevel@tonic-gate  * structures and pointers.  Specifically, for the -i option, save any
50340Sstevel@tonic-gate  * patterns, for the -o option, check (via stat(2)) the archive, and for
50350Sstevel@tonic-gate  * the -p option, validate the destination directory.
50360Sstevel@tonic-gate  */
50370Sstevel@tonic-gate 
50380Sstevel@tonic-gate static void
50390Sstevel@tonic-gate setup(int largc, char **largv)
50400Sstevel@tonic-gate {
50410Sstevel@tonic-gate 	extern int optind;
50420Sstevel@tonic-gate 	extern char *optarg;
50430Sstevel@tonic-gate 
50440Sstevel@tonic-gate #if defined(O_XATTR)
50450Sstevel@tonic-gate #ifdef WAITAROUND
50460Sstevel@tonic-gate 	char	*opts_p = "zabcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6@";
50470Sstevel@tonic-gate #else
50480Sstevel@tonic-gate 	char	*opts_p = "abcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6@";
50490Sstevel@tonic-gate #endif
50500Sstevel@tonic-gate #else
50510Sstevel@tonic-gate #ifdef WAITAROUND
50520Sstevel@tonic-gate 	char	*opts_p = "zabcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6";
50530Sstevel@tonic-gate #else
50540Sstevel@tonic-gate 	char	*opts_p = "abcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6";
50550Sstevel@tonic-gate #endif
50560Sstevel@tonic-gate #endif
50570Sstevel@tonic-gate 
50580Sstevel@tonic-gate 	char   *dupl_p = "Only one occurrence of -%c allowed";
50590Sstevel@tonic-gate 	int option;
50600Sstevel@tonic-gate 	int blk_cnt, blk_cnt_max;
50610Sstevel@tonic-gate 	struct rlimit rlim;
50620Sstevel@tonic-gate 
50630Sstevel@tonic-gate 	/* Remember the native page size. */
50640Sstevel@tonic-gate 
50650Sstevel@tonic-gate 	PageSize = sysconf(_SC_PAGESIZE);
50660Sstevel@tonic-gate 
50670Sstevel@tonic-gate 	if (PageSize == -1) {
50680Sstevel@tonic-gate 		/*
50690Sstevel@tonic-gate 		 * This sysconf call will almost certainly never fail.  The
50700Sstevel@tonic-gate 		 * symbol PAGESIZE itself resolves to the above sysconf call,
50710Sstevel@tonic-gate 		 * so we should go ahead and define our own constant.
50720Sstevel@tonic-gate 		 */
50730Sstevel@tonic-gate 		PageSize = 8192;
50740Sstevel@tonic-gate 	}
50750Sstevel@tonic-gate 
50760Sstevel@tonic-gate 	Euid = geteuid();
50770Sstevel@tonic-gate 	Hdr_type = BIN;
50780Sstevel@tonic-gate 	Max_offset = (off_t)(BIN_OFFSET_MAX);
50790Sstevel@tonic-gate 	Efil_p = Hdr_p = Own_p = IOfil_p = NULL;
50800Sstevel@tonic-gate 	while ((option = getopt(largc, largv, opts_p)) != EOF) {
50810Sstevel@tonic-gate 		switch (option) {
50820Sstevel@tonic-gate #ifdef WAITAROUND
50830Sstevel@tonic-gate 		case 'z':
50840Sstevel@tonic-gate 			/* rendezvous with the debugger */
50850Sstevel@tonic-gate 			waitaround = 1;
50860Sstevel@tonic-gate 			break;
50870Sstevel@tonic-gate #endif
50880Sstevel@tonic-gate 		case 'a':	/* reset access time */
50890Sstevel@tonic-gate 			Args |= OCa;
50900Sstevel@tonic-gate 			break;
50910Sstevel@tonic-gate 		case 'b':	/* swap bytes and halfwords */
50920Sstevel@tonic-gate 			Args |= OCb;
50930Sstevel@tonic-gate 			break;
50940Sstevel@tonic-gate 		case 'c':	/* select character header */
50950Sstevel@tonic-gate 			Args |= OCc;
50960Sstevel@tonic-gate 			Hdr_type = ASC;
50970Sstevel@tonic-gate 			Max_namesz = APATH;
50980Sstevel@tonic-gate 			Onecopy = 1;
50990Sstevel@tonic-gate 			break;
51000Sstevel@tonic-gate 		case 'd':	/* create directories as needed */
51010Sstevel@tonic-gate 			Args |= OCd;
51020Sstevel@tonic-gate 			break;
51030Sstevel@tonic-gate 		case 'f':	/* select files not in patterns */
51040Sstevel@tonic-gate 			Args |= OCf;
51050Sstevel@tonic-gate 			break;
51060Sstevel@tonic-gate 		case 'i':	/* "copy in" */
51070Sstevel@tonic-gate 			Args |= OCi;
51080Sstevel@tonic-gate 			Archive = 0;
51090Sstevel@tonic-gate 			break;
51100Sstevel@tonic-gate 		case 'k':	/* retry after I/O errors */
51110Sstevel@tonic-gate 			Args |= OCk;
51120Sstevel@tonic-gate 			break;
51130Sstevel@tonic-gate 		case 'l':	/* link files when possible */
51140Sstevel@tonic-gate 			Args |= OCl;
51150Sstevel@tonic-gate 			break;
51160Sstevel@tonic-gate 		case 'm':	/* retain modification time */
51170Sstevel@tonic-gate 			Args |= OCm;
51180Sstevel@tonic-gate 			break;
51190Sstevel@tonic-gate 		case 'o':	/* "copy out" */
51200Sstevel@tonic-gate 			Args |= OCo;
51210Sstevel@tonic-gate 			Archive = 1;
51220Sstevel@tonic-gate 			break;
51230Sstevel@tonic-gate 		case 'p':	/* "pass" */
51240Sstevel@tonic-gate 			Max_namesz = APATH;
51250Sstevel@tonic-gate 			Args |= OCp;
51260Sstevel@tonic-gate 			break;
51270Sstevel@tonic-gate 		case 'r':	/* rename files interactively */
51280Sstevel@tonic-gate 			Args |= OCr;
51290Sstevel@tonic-gate 			break;
51300Sstevel@tonic-gate 		case 's':	/* swap bytes */
51310Sstevel@tonic-gate 			Args |= OCs;
51320Sstevel@tonic-gate 			break;
51330Sstevel@tonic-gate 		case 't':	/* table of contents */
51340Sstevel@tonic-gate 			Args |= OCt;
51350Sstevel@tonic-gate 			break;
51360Sstevel@tonic-gate 		case 'u':	/* copy unconditionally */
51370Sstevel@tonic-gate 			Args |= OCu;
51380Sstevel@tonic-gate 			break;
51390Sstevel@tonic-gate 		case 'v':	/* verbose - print file names */
51400Sstevel@tonic-gate 			Args |= OCv;
51410Sstevel@tonic-gate 			break;
51420Sstevel@tonic-gate 		case 'A':	/* append to existing archive */
51430Sstevel@tonic-gate 			Args |= OCA;
51440Sstevel@tonic-gate 			break;
51450Sstevel@tonic-gate 		case 'B':	/* set block size to 5120 bytes */
51460Sstevel@tonic-gate 			Args |= OCB;
51470Sstevel@tonic-gate 			Bufsize = 5120;
51480Sstevel@tonic-gate 			break;
51490Sstevel@tonic-gate 		case 'C':	/* set arbitrary block size */
51500Sstevel@tonic-gate 			if (Args & OCC)
51510Sstevel@tonic-gate 				msg(ERR, dupl_p, 'C');
51520Sstevel@tonic-gate 			else {
51530Sstevel@tonic-gate 				Args |= OCC;
51540Sstevel@tonic-gate 				Bufsize = atoi(optarg);
51550Sstevel@tonic-gate 			}
51560Sstevel@tonic-gate 			break;
51570Sstevel@tonic-gate 		case 'D':
51580Sstevel@tonic-gate 			Dflag = 1;
51590Sstevel@tonic-gate 			break;
51600Sstevel@tonic-gate 		case 'E':	/* alternate file for pattern input */
51610Sstevel@tonic-gate 			if (Args & OCE)
51620Sstevel@tonic-gate 				msg(ERR, dupl_p, 'E');
51630Sstevel@tonic-gate 			else {
51640Sstevel@tonic-gate 				Args |= OCE;
51650Sstevel@tonic-gate 				Efil_p = optarg;
51660Sstevel@tonic-gate 			}
51670Sstevel@tonic-gate 			break;
51680Sstevel@tonic-gate 		case 'H':	/* select header type */
51690Sstevel@tonic-gate 			if (Args & OCH)
51700Sstevel@tonic-gate 				msg(ERR, dupl_p, 'H');
51710Sstevel@tonic-gate 			else {
51720Sstevel@tonic-gate 				Args |= OCH;
51730Sstevel@tonic-gate 				Hdr_p = optarg;
51740Sstevel@tonic-gate 			}
51750Sstevel@tonic-gate 			break;
51760Sstevel@tonic-gate 		case 'I':	/* alternate file for archive input */
51770Sstevel@tonic-gate 			if (Args & OCI)
51780Sstevel@tonic-gate 				msg(ERR, dupl_p, 'I');
51790Sstevel@tonic-gate 			else {
51800Sstevel@tonic-gate 				Args |= OCI;
51810Sstevel@tonic-gate 				IOfil_p = optarg;
51820Sstevel@tonic-gate 			}
51830Sstevel@tonic-gate 			break;
51840Sstevel@tonic-gate 		case 'L':	/* follow symbolic links */
51850Sstevel@tonic-gate 			Args |= OCL;
51860Sstevel@tonic-gate 			break;
51870Sstevel@tonic-gate 		case 'M':	/* specify new end-of-media message */
51880Sstevel@tonic-gate 			if (Args & OCM)
51890Sstevel@tonic-gate 				msg(ERR, dupl_p, 'M');
51900Sstevel@tonic-gate 			else {
51910Sstevel@tonic-gate 				Args |= OCM;
51920Sstevel@tonic-gate 				Eom_p = optarg;
51930Sstevel@tonic-gate 			}
51940Sstevel@tonic-gate 			break;
51950Sstevel@tonic-gate 		case 'O':	/* alternate file for archive output */
51960Sstevel@tonic-gate 			if (Args & OCO)
51970Sstevel@tonic-gate 				msg(ERR, dupl_p, 'O');
51980Sstevel@tonic-gate 			else {
51990Sstevel@tonic-gate 				Args |= OCO;
52000Sstevel@tonic-gate 				IOfil_p = optarg;
52010Sstevel@tonic-gate 			}
52020Sstevel@tonic-gate 			break;
52030Sstevel@tonic-gate 		case 'P':	/* preserve acls */
52040Sstevel@tonic-gate 			Args |= OCP;
52050Sstevel@tonic-gate 			Pflag++;
52060Sstevel@tonic-gate 			break;
52070Sstevel@tonic-gate 		case 'R':	/* change owner/group of files */
52080Sstevel@tonic-gate 			if (Args & OCR)
52090Sstevel@tonic-gate 				msg(ERR, dupl_p, 'R');
52100Sstevel@tonic-gate 			else {
52110Sstevel@tonic-gate 				Args |= OCR;
52120Sstevel@tonic-gate 				Own_p = optarg;
52130Sstevel@tonic-gate 			}
52140Sstevel@tonic-gate 			break;
52150Sstevel@tonic-gate 		case 'S':	/* swap halfwords */
52160Sstevel@tonic-gate 			Args |= OCS;
52170Sstevel@tonic-gate 			break;
52180Sstevel@tonic-gate 		case 'V':	/* print a dot '.' for each file */
52190Sstevel@tonic-gate 			Args |= OCV;
52200Sstevel@tonic-gate 			break;
52210Sstevel@tonic-gate 		case '6':	/* for old, sixth-edition files */
52220Sstevel@tonic-gate 			Args |= OC6;
52230Sstevel@tonic-gate 			Ftype = SIXTH;
52240Sstevel@tonic-gate 			break;
52250Sstevel@tonic-gate #if defined(O_XATTR)
52260Sstevel@tonic-gate 		case '@':
52270Sstevel@tonic-gate 			Atflag++;
52280Sstevel@tonic-gate 			break;
52290Sstevel@tonic-gate #endif
52300Sstevel@tonic-gate 		default:
52310Sstevel@tonic-gate 			Error_cnt++;
52320Sstevel@tonic-gate 		} /* option */
52330Sstevel@tonic-gate 	} /* (option = getopt(largc, largv, opts_p)) != EOF */
52340Sstevel@tonic-gate 
52350Sstevel@tonic-gate #ifdef WAITAROUND
52360Sstevel@tonic-gate 	if (waitaround) {
52370Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Rendezvous with cpio on pid"
52380Sstevel@tonic-gate 		    " %d\n"), getpid());
52390Sstevel@tonic-gate 
52400Sstevel@tonic-gate 		while (waitaround) {
52410Sstevel@tonic-gate 			(void) sleep(10);
52420Sstevel@tonic-gate 		}
52430Sstevel@tonic-gate 	}
52440Sstevel@tonic-gate #endif
52450Sstevel@tonic-gate 
52460Sstevel@tonic-gate 	largc -= optind;
52470Sstevel@tonic-gate 	largv += optind;
52480Sstevel@tonic-gate 	ckopts(Args);
52490Sstevel@tonic-gate 	if (!Error_cnt) {
52500Sstevel@tonic-gate 		Empty = e_valloc(E_EXIT, TARSZ);
52510Sstevel@tonic-gate 		if (Args & OCr) {
52520Sstevel@tonic-gate 			Renam_p = e_zalloc(E_EXIT, APATH + 1);
52530Sstevel@tonic-gate 			Renametmp_p = e_zalloc(E_EXIT, APATH + 1);
52540Sstevel@tonic-gate 		}
52550Sstevel@tonic-gate 		Symlnk_p = e_zalloc(E_EXIT, APATH);
52560Sstevel@tonic-gate 		Over_p = e_zalloc(E_EXIT, APATH);
52570Sstevel@tonic-gate 		Nam_p = e_zalloc(E_EXIT, APATH + 1);
52580Sstevel@tonic-gate 		if (Args & OCp) {
52590Sstevel@tonic-gate 			Savenam_p = e_zalloc(E_EXIT, APATH + 1);
52600Sstevel@tonic-gate 		}
52610Sstevel@tonic-gate 		Fullnam_p = e_zalloc(E_EXIT, APATH);
52620Sstevel@tonic-gate 		Lnknam_p = e_zalloc(E_EXIT, APATH);
52630Sstevel@tonic-gate 		Gen.g_nam_p = Nam_p;
52640Sstevel@tonic-gate 		if ((Fullnam_p = getcwd((char *)NULL, APATH)) == (char *)NULL)
52650Sstevel@tonic-gate 			msg(EXT, "Unable to determine current directory.");
52660Sstevel@tonic-gate 		if (Args & OCi) {
52670Sstevel@tonic-gate 			if (largc > 0) /* save patterns for -i option, if any */
52680Sstevel@tonic-gate 				Pat_pp = largv;
52690Sstevel@tonic-gate 			if (Args & OCE)
52700Sstevel@tonic-gate 				getpats(largc, largv);
52710Sstevel@tonic-gate 		} else if (Args & OCo) {
52720Sstevel@tonic-gate 			if (largc != 0) /* error if arguments left with -o */
52730Sstevel@tonic-gate 				Error_cnt++;
52740Sstevel@tonic-gate 			else if (fstat(Archive, &ArchSt) < 0)
52750Sstevel@tonic-gate 				msg(ERRN, "Error during stat() of archive");
52760Sstevel@tonic-gate 			switch (Hdr_type) {
52770Sstevel@tonic-gate 			case BIN:
52780Sstevel@tonic-gate 				Hdrsz = HDRSZ;
52790Sstevel@tonic-gate 				Pad_val = HALFWD;
52800Sstevel@tonic-gate 				break;
52810Sstevel@tonic-gate 			case CHR:
52820Sstevel@tonic-gate 				Hdrsz = CHRSZ;
52830Sstevel@tonic-gate 				Pad_val = 0;
52840Sstevel@tonic-gate 				Max_offset = (off_t)(CHAR_OFFSET_MAX);
52850Sstevel@tonic-gate 				break;
52860Sstevel@tonic-gate 			case ASC:
52870Sstevel@tonic-gate 			case CRC:
52880Sstevel@tonic-gate 				Hdrsz = ASCSZ;
52890Sstevel@tonic-gate 				Pad_val = FULLWD;
5290*36Schin 				Max_offset = (off_t)(ASC_OFFSET_MAX);
52910Sstevel@tonic-gate 				break;
52920Sstevel@tonic-gate 			case TAR:
52930Sstevel@tonic-gate 			/* FALLTHROUGH */
52940Sstevel@tonic-gate 			case USTAR: /* TAR and USTAR */
52950Sstevel@tonic-gate 				Hdrsz = TARSZ;
52960Sstevel@tonic-gate 				Pad_val = FULLBK;
52970Sstevel@tonic-gate 				Max_offset = (off_t)(CHAR_OFFSET_MAX);
52980Sstevel@tonic-gate 				break;
52990Sstevel@tonic-gate 			default:
53000Sstevel@tonic-gate 				msg(EXT, "Impossible header type.");
53010Sstevel@tonic-gate 			}
53020Sstevel@tonic-gate 		} else { /* directory must be specified */
53030Sstevel@tonic-gate 			if (largc != 1)
53040Sstevel@tonic-gate 				Error_cnt++;
53050Sstevel@tonic-gate 			else if (access(*largv, 2) < 0)
53060Sstevel@tonic-gate 				msg(ERRN,
53070Sstevel@tonic-gate 				    "Error during access() of \"%s\"", *largv);
53080Sstevel@tonic-gate 		}
53090Sstevel@tonic-gate 	}
53100Sstevel@tonic-gate 	if (Error_cnt)
53110Sstevel@tonic-gate 		usage(); /* exits! */
53120Sstevel@tonic-gate 	if (Args & (OCi | OCo)) {
53130Sstevel@tonic-gate 		if (!Dflag) {
53140Sstevel@tonic-gate 			if (Args & (OCB | OCC)) {
53150Sstevel@tonic-gate 				if (g_init(&Device, &Archive) < 0)
53160Sstevel@tonic-gate 					msg(EXTN,
53170Sstevel@tonic-gate 					    "Error during initialization");
53180Sstevel@tonic-gate 			} else {
53190Sstevel@tonic-gate 				if ((Bufsize = g_init(&Device, &Archive)) < 0)
53200Sstevel@tonic-gate 					msg(EXTN,
53210Sstevel@tonic-gate 					    "Error during initialization");
53220Sstevel@tonic-gate 			}
53230Sstevel@tonic-gate 		}
53240Sstevel@tonic-gate 
53250Sstevel@tonic-gate 		blk_cnt_max = _20K / Bufsize;
53260Sstevel@tonic-gate 		if (blk_cnt_max < MX_BUFS) {
53270Sstevel@tonic-gate 			blk_cnt_max = MX_BUFS;
53280Sstevel@tonic-gate 		}
53290Sstevel@tonic-gate 
53300Sstevel@tonic-gate 		Buffr.b_base_p = NULL;
53310Sstevel@tonic-gate 
53320Sstevel@tonic-gate 		for (blk_cnt = blk_cnt_max; blk_cnt > 1; blk_cnt--) {
53330Sstevel@tonic-gate 			Buffr.b_size = (size_t)(Bufsize * blk_cnt);
53340Sstevel@tonic-gate 			Buffr.b_base_p = e_valloc(E_NORMAL, Buffr.b_size);
53350Sstevel@tonic-gate 			if (Buffr.b_base_p != NULL) {
53360Sstevel@tonic-gate 				break;
53370Sstevel@tonic-gate 			}
53380Sstevel@tonic-gate 		}
53390Sstevel@tonic-gate 		if (Buffr.b_base_p == NULL || Buffr.b_size < (2 * CPIOBSZ)) {
53400Sstevel@tonic-gate 			msg(EXT, "Out of memory");
53410Sstevel@tonic-gate 		}
53420Sstevel@tonic-gate 
53430Sstevel@tonic-gate 		Buffr.b_out_p = Buffr.b_in_p = Buffr.b_base_p;
53440Sstevel@tonic-gate 		Buffr.b_cnt = 0L;
53450Sstevel@tonic-gate 		Buffr.b_end_p = Buffr.b_base_p + Buffr.b_size;
53460Sstevel@tonic-gate 	}
53470Sstevel@tonic-gate 
53480Sstevel@tonic-gate 	/*
53490Sstevel@tonic-gate 	 * Now that Bufsize has stabilized, we can allocate our i/o buffer
53500Sstevel@tonic-gate 	 */
53510Sstevel@tonic-gate 	Buf_p = e_valloc(E_EXIT, Bufsize);
53520Sstevel@tonic-gate 
53530Sstevel@tonic-gate 	if (Args & OCp) { /* get destination directory */
53540Sstevel@tonic-gate 		(void) strcpy(Fullnam_p, *largv);
53550Sstevel@tonic-gate 		if (stat(Fullnam_p, &DesSt) < 0)
53560Sstevel@tonic-gate 			msg(EXTN, "Error during stat() of \"%s\"", Fullnam_p);
53570Sstevel@tonic-gate 		if ((DesSt.st_mode & Ftype) != S_IFDIR)
53580Sstevel@tonic-gate 			msg(EXT, "\"%s\" is not a directory", Fullnam_p);
53590Sstevel@tonic-gate 	}
53600Sstevel@tonic-gate 	Full_p = Fullnam_p + strlen(Fullnam_p) - 1;
53610Sstevel@tonic-gate 	if (*Full_p != '/') {
53620Sstevel@tonic-gate 		Full_p++;
53630Sstevel@tonic-gate 		*Full_p = '/';
53640Sstevel@tonic-gate 	}
53650Sstevel@tonic-gate 	Full_p++;
53660Sstevel@tonic-gate 	*Full_p = '\0';
53670Sstevel@tonic-gate 	(void) strcpy(Lnknam_p, Fullnam_p);
53680Sstevel@tonic-gate 	Lnkend_p = Lnknam_p + strlen(Lnknam_p);
53690Sstevel@tonic-gate 	(void) getrlimit(RLIMIT_FSIZE, &rlim);
53700Sstevel@tonic-gate 	Max_filesz = (off_t)rlim.rlim_cur;
53710Sstevel@tonic-gate 	Lnk_hd.L_nxt_p = Lnk_hd.L_bck_p = &Lnk_hd;
53720Sstevel@tonic-gate 	Lnk_hd.L_lnk_p = (struct Lnk *)NULL;
53730Sstevel@tonic-gate }
53740Sstevel@tonic-gate 
53750Sstevel@tonic-gate /*
53760Sstevel@tonic-gate  * set_tym: Set the access and/or modification times for a file.
53770Sstevel@tonic-gate  */
53780Sstevel@tonic-gate 
53790Sstevel@tonic-gate static void
53800Sstevel@tonic-gate set_tym(int dirfd, char *nam_p, time_t atime, time_t mtime)
53810Sstevel@tonic-gate {
53820Sstevel@tonic-gate 	struct timeval times[2];
53830Sstevel@tonic-gate 
53840Sstevel@tonic-gate 	times[0].tv_sec = atime;
53850Sstevel@tonic-gate 	times[0].tv_usec = 0;
53860Sstevel@tonic-gate 	times[1].tv_sec = mtime;
53870Sstevel@tonic-gate 	times[1].tv_usec = 0;
53880Sstevel@tonic-gate 
53890Sstevel@tonic-gate 	if (futimesat(dirfd, nam_p, times) < 0) {
53900Sstevel@tonic-gate 		if (Args & OCa) {
53910Sstevel@tonic-gate 			msg(ERRN,
53920Sstevel@tonic-gate 			    "Unable to reset access time for \"%s%s%s\"",
53930Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
53940Sstevel@tonic-gate 			    nam_p : Fullnam_p,
53950Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
53960Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
53970Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
53980Sstevel@tonic-gate 			    "" : nam_p);
53990Sstevel@tonic-gate 		} else {
54000Sstevel@tonic-gate 			msg(ERRN,
54010Sstevel@tonic-gate 			    "Unable to reset modification time for \"%s%s%s\"",
54020Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
54030Sstevel@tonic-gate 			    nam_p : Fullnam_p,
54040Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
54050Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
54060Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
54070Sstevel@tonic-gate 			    "" : nam_p);
54080Sstevel@tonic-gate 		}
54090Sstevel@tonic-gate 	}
54100Sstevel@tonic-gate }
54110Sstevel@tonic-gate 
54120Sstevel@tonic-gate /*
54130Sstevel@tonic-gate  * sigint:  Catch interrupts.  If an interrupt occurs during the extraction
54140Sstevel@tonic-gate  * of a file from the archive with the -u option set, and the filename did
54150Sstevel@tonic-gate  * exist, remove the current file and restore the original file.  Then exit.
54160Sstevel@tonic-gate  */
54170Sstevel@tonic-gate 
54180Sstevel@tonic-gate /*ARGSUSED*/
54190Sstevel@tonic-gate static void
54200Sstevel@tonic-gate sigint(int sig)
54210Sstevel@tonic-gate {
54220Sstevel@tonic-gate 	char *nam_p;
54230Sstevel@tonic-gate 
54240Sstevel@tonic-gate 	(void) signal(SIGINT, SIG_IGN); /* block further signals */
54250Sstevel@tonic-gate 	if (!Finished) {
54260Sstevel@tonic-gate 		if (Args & OCi)
54270Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
54280Sstevel@tonic-gate 		else /* OCp */
54290Sstevel@tonic-gate 			nam_p = Fullnam_p;
54300Sstevel@tonic-gate 		if (*Over_p != '\0') { /* There is a temp file */
54310Sstevel@tonic-gate 			if (unlink(nam_p) < 0) {
54320Sstevel@tonic-gate 				msg(ERRN,
54330Sstevel@tonic-gate 				    "Cannot remove incomplete \"%s\"", nam_p);
54340Sstevel@tonic-gate 			}
54350Sstevel@tonic-gate 			if (rename(Over_p, nam_p) < 0) {
54360Sstevel@tonic-gate 				if (link(Over_p, nam_p) < 0) {
54370Sstevel@tonic-gate 					msg(ERRN,
54380Sstevel@tonic-gate 					    "Cannot recover original \"%s\"",
54390Sstevel@tonic-gate 					    nam_p);
54400Sstevel@tonic-gate 				}
54410Sstevel@tonic-gate 				if (unlink(Over_p)) {
54420Sstevel@tonic-gate 					msg(ERRN,
54430Sstevel@tonic-gate 					    "Cannot remove temp file \"%s\"",
54440Sstevel@tonic-gate 					    Over_p);
54450Sstevel@tonic-gate 				}
54460Sstevel@tonic-gate 			}
54470Sstevel@tonic-gate 		} else if (unlink(nam_p))
54480Sstevel@tonic-gate 			msg(ERRN,
54490Sstevel@tonic-gate 			    "Cannot remove incomplete \"%s\"", nam_p);
54500Sstevel@tonic-gate 			*Over_p = '\0';
54510Sstevel@tonic-gate 	}
54520Sstevel@tonic-gate 	exit(EXIT_CODE);
54530Sstevel@tonic-gate }
54540Sstevel@tonic-gate 
54550Sstevel@tonic-gate /*
54560Sstevel@tonic-gate  * swap: Swap bytes (-s), halfwords (-S) or or both halfwords and bytes (-b).
54570Sstevel@tonic-gate  */
54580Sstevel@tonic-gate 
54590Sstevel@tonic-gate static void
54600Sstevel@tonic-gate swap(char *buf_p, int cnt)
54610Sstevel@tonic-gate {
54620Sstevel@tonic-gate 	unsigned char tbyte;
54630Sstevel@tonic-gate 	int tcnt;
54640Sstevel@tonic-gate 	int rcnt;
54650Sstevel@tonic-gate 	ushort_t thalf;
54660Sstevel@tonic-gate 
54670Sstevel@tonic-gate 	rcnt = cnt % 4;
54680Sstevel@tonic-gate 	cnt /= 4;
54690Sstevel@tonic-gate 	if (Args & (OCb | OCs | BSM)) {
54700Sstevel@tonic-gate 		tcnt = cnt;
54710Sstevel@tonic-gate 		/* LINTED alignment */
54720Sstevel@tonic-gate 		Swp_p = (union swpbuf *)buf_p;
54730Sstevel@tonic-gate 		while (tcnt-- > 0) {
54740Sstevel@tonic-gate 			tbyte = Swp_p->s_byte[0];
54750Sstevel@tonic-gate 			Swp_p->s_byte[0] = Swp_p->s_byte[1];
54760Sstevel@tonic-gate 			Swp_p->s_byte[1] = tbyte;
54770Sstevel@tonic-gate 			tbyte = Swp_p->s_byte[2];
54780Sstevel@tonic-gate 			Swp_p->s_byte[2] = Swp_p->s_byte[3];
54790Sstevel@tonic-gate 			Swp_p->s_byte[3] = tbyte;
54800Sstevel@tonic-gate 			Swp_p++;
54810Sstevel@tonic-gate 		}
54820Sstevel@tonic-gate 		if (rcnt >= 2) {
54830Sstevel@tonic-gate 		tbyte = Swp_p->s_byte[0];
54840Sstevel@tonic-gate 		Swp_p->s_byte[0] = Swp_p->s_byte[1];
54850Sstevel@tonic-gate 		Swp_p->s_byte[1] = tbyte;
54860Sstevel@tonic-gate 		tbyte = Swp_p->s_byte[2];
54870Sstevel@tonic-gate 		}
54880Sstevel@tonic-gate 	}
54890Sstevel@tonic-gate 	if (Args & (OCb | OCS)) {
54900Sstevel@tonic-gate 		tcnt = cnt;
54910Sstevel@tonic-gate 		/* LINTED alignment */
54920Sstevel@tonic-gate 		Swp_p = (union swpbuf *)buf_p;
54930Sstevel@tonic-gate 		while (tcnt-- > 0) {
54940Sstevel@tonic-gate 			thalf = Swp_p->s_half[0];
54950Sstevel@tonic-gate 			Swp_p->s_half[0] = Swp_p->s_half[1];
54960Sstevel@tonic-gate 			Swp_p->s_half[1] = thalf;
54970Sstevel@tonic-gate 			Swp_p++;
54980Sstevel@tonic-gate 		}
54990Sstevel@tonic-gate 	}
55000Sstevel@tonic-gate }
55010Sstevel@tonic-gate 
55020Sstevel@tonic-gate /*
55030Sstevel@tonic-gate  * usage: Print the usage message on stderr and exit.
55040Sstevel@tonic-gate  */
55050Sstevel@tonic-gate 
55060Sstevel@tonic-gate static void
55070Sstevel@tonic-gate usage(void)
55080Sstevel@tonic-gate {
55090Sstevel@tonic-gate 
55100Sstevel@tonic-gate 	(void) fflush(stdout);
55110Sstevel@tonic-gate #if defined(O_XATTR)
55120Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("USAGE:\n"
55130Sstevel@tonic-gate 				"\tcpio -i[bcdfkmrstuv@BSV6] [-C size] "
55140Sstevel@tonic-gate 				"[-E file] [-H hdr] [-I file [-M msg]] "
55150Sstevel@tonic-gate 				"[-R id] [patterns]\n"
55160Sstevel@tonic-gate 				"\tcpio -o[acv@ABLV] [-C size] "
55170Sstevel@tonic-gate 				"[-H hdr] [-O file [-M msg]]\n"
55180Sstevel@tonic-gate 				"\tcpio -p[adlmuv@LV] [-R id] directory\n"));
55190Sstevel@tonic-gate #else
55200Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("USAGE:\n"
55210Sstevel@tonic-gate 				"\tcpio -i[bcdfkmrstuvBSV6] [-C size] "
55220Sstevel@tonic-gate 				"[-E file] [-H hdr] [-I file [-M msg]] "
55230Sstevel@tonic-gate 				"[-R id] [patterns]\n"
55240Sstevel@tonic-gate 				"\tcpio -o[acvABLV] [-C size] "
55250Sstevel@tonic-gate 				"[-H hdr] [-O file [-M msg]]\n"
55260Sstevel@tonic-gate 				"\tcpio -p[adlmuvLV] [-R id] directory\n"));
55270Sstevel@tonic-gate #endif
55280Sstevel@tonic-gate 	(void) fflush(stderr);
55290Sstevel@tonic-gate 	exit(EXIT_CODE);
55300Sstevel@tonic-gate }
55310Sstevel@tonic-gate 
55320Sstevel@tonic-gate /*
55330Sstevel@tonic-gate  * verbose: For each file, print either the filename (-v) or a dot (-V).
55340Sstevel@tonic-gate  * If the -t option (table of contents) is set, print either the filename,
55350Sstevel@tonic-gate  * or if the -v option is also set, print an "ls -l"-like listing.
55360Sstevel@tonic-gate  */
55370Sstevel@tonic-gate 
55380Sstevel@tonic-gate static void
55390Sstevel@tonic-gate verbose(char *nam_p)
55400Sstevel@tonic-gate {
55410Sstevel@tonic-gate 	int i, j, temp;
55420Sstevel@tonic-gate 	mode_t mode;
55430Sstevel@tonic-gate 	char modestr[12];
55440Sstevel@tonic-gate 
55450Sstevel@tonic-gate 	/*
55460Sstevel@tonic-gate 	 * The printf format and associated arguments to print the current
55470Sstevel@tonic-gate 	 * filename.  Normally, just nam_p.  If we're processing an extended
55480Sstevel@tonic-gate 	 * attribute, these are overridden.
55490Sstevel@tonic-gate 	 */
55500Sstevel@tonic-gate 	char *name_fmt = "%s";
55510Sstevel@tonic-gate 	const char *name = nam_p;
55520Sstevel@tonic-gate 	const char *attribute = NULL;
55530Sstevel@tonic-gate 
55540Sstevel@tonic-gate 	if (Gen.g_attrnam_p != (char *)NULL) {
55550Sstevel@tonic-gate 		/*
55560Sstevel@tonic-gate 		 * Translation note:
55570Sstevel@tonic-gate 		 * 'attribute' is a noun.
55580Sstevel@tonic-gate 		 */
55590Sstevel@tonic-gate 		name_fmt = gettext("%s attribute %s");
55600Sstevel@tonic-gate 
55610Sstevel@tonic-gate 		name = (Args & OCp) ? nam_p : Gen.g_attrfnam_p;
55620Sstevel@tonic-gate 		attribute = Gen.g_attrnam_p;
55630Sstevel@tonic-gate 	}
55640Sstevel@tonic-gate 
55650Sstevel@tonic-gate 	if ((Gen.g_mode == SECMODE) || ((Hdr_type == USTAR ||
55660Sstevel@tonic-gate 	    Hdr_type == TAR) && Thdr_p->tbuf.t_typeflag == 'A')) {
55670Sstevel@tonic-gate 		/* dont print ancillary file */
55680Sstevel@tonic-gate 		aclchar = '+';
55690Sstevel@tonic-gate 		return;
55700Sstevel@tonic-gate 	}
55710Sstevel@tonic-gate 	for (i = 0; i < 11; i++)
55720Sstevel@tonic-gate 		modestr[i] = '-';
55730Sstevel@tonic-gate 	modestr[i] = '\0';
55740Sstevel@tonic-gate 	modestr[i-1] = aclchar;
55750Sstevel@tonic-gate 	aclchar = ' ';
55760Sstevel@tonic-gate 
55770Sstevel@tonic-gate 	if ((Args & OCt) && (Args & OCv)) {
55780Sstevel@tonic-gate 		mode = Gen.g_mode;
55790Sstevel@tonic-gate 		for (i = 0; i < 3; i++) {
55800Sstevel@tonic-gate 			temp = (mode >> (6 - (i * 3)));
55810Sstevel@tonic-gate 			j = (i * 3) + 1;
55820Sstevel@tonic-gate 			if (S_IROTH & temp)
55830Sstevel@tonic-gate 				modestr[j] = 'r';
55840Sstevel@tonic-gate 			if (S_IWOTH & temp)
55850Sstevel@tonic-gate 				modestr[j + 1] = 'w';
55860Sstevel@tonic-gate 			if (S_IXOTH & temp)
55870Sstevel@tonic-gate 				modestr[j + 2] = 'x';
55880Sstevel@tonic-gate 		}
55890Sstevel@tonic-gate 
55900Sstevel@tonic-gate 		if (Hdr_type != BAR) {
55910Sstevel@tonic-gate 			temp = Gen.g_mode & Ftype;
55920Sstevel@tonic-gate 			switch (temp) {
55930Sstevel@tonic-gate 			case (S_IFIFO):
55940Sstevel@tonic-gate 				modestr[0] = 'p';
55950Sstevel@tonic-gate 				break;
55960Sstevel@tonic-gate 			case (S_IFCHR):
55970Sstevel@tonic-gate 				modestr[0] = 'c';
55980Sstevel@tonic-gate 				break;
55990Sstevel@tonic-gate 			case (S_IFDIR):
56000Sstevel@tonic-gate 				modestr[0] = 'd';
56010Sstevel@tonic-gate 				break;
56020Sstevel@tonic-gate 			case (S_IFBLK):
56030Sstevel@tonic-gate 				modestr[0] = 'b';
56040Sstevel@tonic-gate 				break;
56050Sstevel@tonic-gate 			case (S_IFREG): /* was initialized to '-' */
56060Sstevel@tonic-gate 				break;
56070Sstevel@tonic-gate 			case (S_IFLNK):
56080Sstevel@tonic-gate 				modestr[0] = 'l';
56090Sstevel@tonic-gate 				break;
56100Sstevel@tonic-gate 			default:
56110Sstevel@tonic-gate 				msg(ERR, "Impossible file type");
56120Sstevel@tonic-gate 			}
56130Sstevel@tonic-gate 		} else {		/* bar */
56140Sstevel@tonic-gate 			temp = Gen.g_mode & Ftype;
56150Sstevel@tonic-gate 			switch (temp) {
56160Sstevel@tonic-gate 			case (S_IFIFO):
56170Sstevel@tonic-gate 				modestr[0] = 'p';
56180Sstevel@tonic-gate 				break;
56190Sstevel@tonic-gate 			case (S_IFCHR):
56200Sstevel@tonic-gate 				modestr[0] = 'c';
56210Sstevel@tonic-gate 				break;
56220Sstevel@tonic-gate 			case (S_IFDIR):
56230Sstevel@tonic-gate 				modestr[0] = 'd';
56240Sstevel@tonic-gate 				break;
56250Sstevel@tonic-gate 			case (S_IFBLK):
56260Sstevel@tonic-gate 				modestr[0] = 'b';
56270Sstevel@tonic-gate 				break;
56280Sstevel@tonic-gate 			}
56290Sstevel@tonic-gate 			if (bar_linkflag == SYMTYPE)
56300Sstevel@tonic-gate 				modestr[0] = 'l';
56310Sstevel@tonic-gate 		}
56320Sstevel@tonic-gate 		if ((S_ISUID & Gen.g_mode) == S_ISUID)
56330Sstevel@tonic-gate 			modestr[3] = 's';
56340Sstevel@tonic-gate 		if ((S_ISVTX & Gen.g_mode) == S_ISVTX)
56350Sstevel@tonic-gate 			modestr[9] = 't';
56360Sstevel@tonic-gate 		if ((S_ISGID & G_p->g_mode) == S_ISGID && modestr[6] == 'x')
56370Sstevel@tonic-gate 			modestr[6] = 's';
56380Sstevel@tonic-gate 		else if ((S_ENFMT & Gen.g_mode) == S_ENFMT && modestr[6] != 'x')
56390Sstevel@tonic-gate 			modestr[6] = 'l';
56400Sstevel@tonic-gate 		if ((Hdr_type == TAR || Hdr_type == USTAR) && Gen.g_nlink == 0)
56410Sstevel@tonic-gate 			(void) printf("%s%4d ", modestr, (int)Gen.g_nlink+1);
56420Sstevel@tonic-gate 		else
56430Sstevel@tonic-gate 			(void) printf("%s%4d ", modestr, (int)Gen.g_nlink);
56440Sstevel@tonic-gate 		if (Lastuid == (int)Gen.g_uid) {
56450Sstevel@tonic-gate 			if (Lastuid == -1)
56460Sstevel@tonic-gate 				(void) printf("-1       ");
56470Sstevel@tonic-gate 			else
56480Sstevel@tonic-gate 				(void) printf("%-9s", Curpw_p->pw_name);
56490Sstevel@tonic-gate 		} else {
56500Sstevel@tonic-gate 			if (Curpw_p = getpwuid((int)Gen.g_uid)) {
56510Sstevel@tonic-gate 				(void) printf("%-9s", Curpw_p->pw_name);
56520Sstevel@tonic-gate 				Lastuid = (int)Gen.g_uid;
56530Sstevel@tonic-gate 			} else {
56540Sstevel@tonic-gate 				(void) printf("%-9d", (int)Gen.g_uid);
56550Sstevel@tonic-gate 				Lastuid = -1;
56560Sstevel@tonic-gate 			}
56570Sstevel@tonic-gate 		}
56580Sstevel@tonic-gate 		if (Lastgid == (int)Gen.g_gid) {
56590Sstevel@tonic-gate 			if (Lastgid == -1)
56600Sstevel@tonic-gate 				(void) printf("-1       ");
56610Sstevel@tonic-gate 			else
56620Sstevel@tonic-gate 				(void) printf("%-9s", Curgr_p->gr_name);
56630Sstevel@tonic-gate 		} else {
56640Sstevel@tonic-gate 			if (Curgr_p = getgrgid((int)Gen.g_gid)) {
56650Sstevel@tonic-gate 				(void) printf("%-9s", Curgr_p->gr_name);
56660Sstevel@tonic-gate 				Lastgid = (int)Gen.g_gid;
56670Sstevel@tonic-gate 			} else {
56680Sstevel@tonic-gate 				(void) printf("%-9d", (int)Gen.g_gid);
56690Sstevel@tonic-gate 				Lastgid = -1;
56700Sstevel@tonic-gate 			}
56710Sstevel@tonic-gate 		}
56720Sstevel@tonic-gate 
56730Sstevel@tonic-gate 		/* print file size */
56740Sstevel@tonic-gate 		if (!Aspec || ((Gen.g_mode & Ftype) == S_IFIFO) ||
56750Sstevel@tonic-gate 		    (Hdr_type == BAR && bar_linkflag == SYMTYPE)) {
56760Sstevel@tonic-gate 			if (Gen.g_filesz < (1LL << 31))
56770Sstevel@tonic-gate 				(void) printf("%7lld ",
56780Sstevel@tonic-gate 				    (offset_t)Gen.g_filesz);
56790Sstevel@tonic-gate 			else
56800Sstevel@tonic-gate 				(void) printf("%11lld ",
56810Sstevel@tonic-gate 				    (offset_t)Gen.g_filesz);
56820Sstevel@tonic-gate 		} else
56830Sstevel@tonic-gate 			(void) printf("%3d,%3d ", (int)major(Gen.g_rdev),
56840Sstevel@tonic-gate 			    (int)minor(Gen.g_rdev));
56850Sstevel@tonic-gate 		(void) cftime(Time, dcgettext(NULL, FORMAT, LC_TIME),
56860Sstevel@tonic-gate 		    (time_t *)&Gen.g_mtime);
56870Sstevel@tonic-gate 		(void) printf("%s, ", Time);
56880Sstevel@tonic-gate 		(void) printf(name_fmt, name, attribute);
56890Sstevel@tonic-gate 		if ((Gen.g_mode & Ftype) == S_IFLNK) {
56900Sstevel@tonic-gate 			if (Hdr_type == USTAR || Hdr_type == TAR)
56910Sstevel@tonic-gate 				(void) strcpy(Symlnk_p,
56920Sstevel@tonic-gate 				    Thdr_p->tbuf.t_linkname);
56930Sstevel@tonic-gate 			else {
56940Sstevel@tonic-gate 				(void) strncpy(Symlnk_p, Buffr.b_out_p,
56950Sstevel@tonic-gate 				    Gen.g_filesz);
56960Sstevel@tonic-gate 				*(Symlnk_p + Gen.g_filesz) = '\0';
56970Sstevel@tonic-gate 			}
56980Sstevel@tonic-gate 			(void) printf(" -> %s", Symlnk_p);
56990Sstevel@tonic-gate 		}
57000Sstevel@tonic-gate 		if (Hdr_type == BAR) {
57010Sstevel@tonic-gate 			if (bar_linkflag == SYMTYPE)
57020Sstevel@tonic-gate 				(void) printf(gettext(" symbolic link to %s"),
57030Sstevel@tonic-gate 				    bar_linkname);
57040Sstevel@tonic-gate 			else if (bar_linkflag == '1')
57050Sstevel@tonic-gate 				(void) printf(gettext(" linked to %s"),
57060Sstevel@tonic-gate 				    bar_linkname);
57070Sstevel@tonic-gate 		}
57080Sstevel@tonic-gate 		if ((Hdr_type == USTAR || Hdr_type == TAR) &&
57090Sstevel@tonic-gate 		    Thdr_p->tbuf.t_typeflag == '1') {
57100Sstevel@tonic-gate 			(void) printf(gettext(" linked to %s%s%s"),
57110Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
57120Sstevel@tonic-gate 			    Thdr_p->tbuf.t_linkname : Gen.g_attrfnam_p,
57130Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
57140Sstevel@tonic-gate 			    "" : gettext(" attribute "),
57150Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
57160Sstevel@tonic-gate 			    "" : Gen.g_linktoattrnam_p);
57170Sstevel@tonic-gate 		}
57180Sstevel@tonic-gate 		(void) printf("\n");
57190Sstevel@tonic-gate 	} else if ((Args & OCt) || (Args & OCv)) {
57200Sstevel@tonic-gate 		(void) fprintf(Out_p, name_fmt, name, attribute);
57210Sstevel@tonic-gate 		(void) fputc('\n', Out_p);
57220Sstevel@tonic-gate 	} else { /* OCV */
57230Sstevel@tonic-gate 		(void) fputc('.', Out_p);
57240Sstevel@tonic-gate 		if (Verbcnt++ >= 49) { /* start a new line of dots */
57250Sstevel@tonic-gate 			Verbcnt = 0;
57260Sstevel@tonic-gate 			(void) fputc('\n', Out_p);
57270Sstevel@tonic-gate 		}
57280Sstevel@tonic-gate 	}
57290Sstevel@tonic-gate 	(void) fflush(Out_p);
57300Sstevel@tonic-gate }
57310Sstevel@tonic-gate 
57320Sstevel@tonic-gate #define	MK_USHORT(a)	(a & 00000177777)
57330Sstevel@tonic-gate 
57340Sstevel@tonic-gate /*
57350Sstevel@tonic-gate  * write_hdr: Transfer header information for the generic structure
57360Sstevel@tonic-gate  * into the format for the selected header and bwrite() the header.
57370Sstevel@tonic-gate  * ACL support: add two new argumnets. secflag indicates that it's an
57380Sstevel@tonic-gate  *	ancillary file. len is the size of the file (incl. all security
57390Sstevel@tonic-gate  *	attributes). We only have acls now.
57400Sstevel@tonic-gate  */
57410Sstevel@tonic-gate 
57420Sstevel@tonic-gate static void
57430Sstevel@tonic-gate write_hdr(int secflag, off_t len)
57440Sstevel@tonic-gate {
57450Sstevel@tonic-gate 	int cnt, pad;
57460Sstevel@tonic-gate 	mode_t mode;
57470Sstevel@tonic-gate 	uid_t uid;
57480Sstevel@tonic-gate 	gid_t gid;
57490Sstevel@tonic-gate 	const char warnfmt[] = "%s%s%s : %s";
57500Sstevel@tonic-gate 
57510Sstevel@tonic-gate 	if (secflag == ARCHIVE_ACL) {
57520Sstevel@tonic-gate 		mode = SECMODE;
57530Sstevel@tonic-gate 	} else {
57540Sstevel@tonic-gate 		/*
57550Sstevel@tonic-gate 		 * If attribute is being archived in cpio format then
57560Sstevel@tonic-gate 		 * zap off the file type bits since those are truly a
57570Sstevel@tonic-gate 		 * mask and reset them with _XATTR_CPIO_MODE
57580Sstevel@tonic-gate 		 */
57590Sstevel@tonic-gate 
57600Sstevel@tonic-gate 		/*
57610Sstevel@tonic-gate 		 * len is the value of g_filesz for normal files
57620Sstevel@tonic-gate 		 * and the length of the special header buffer in
57630Sstevel@tonic-gate 		 * the case of acl and xattr headers.
57640Sstevel@tonic-gate 		 */
57650Sstevel@tonic-gate 		if (G_p->g_attrnam_p != (char *)NULL && Hdr_type != USTAR &&
57660Sstevel@tonic-gate 		    Hdr_type != TAR) {
57670Sstevel@tonic-gate 			mode = (G_p->g_mode & S_IAMB) | _XATTR_CPIO_MODE;
57680Sstevel@tonic-gate 		} else {
57690Sstevel@tonic-gate 			mode = G_p->g_mode;
57700Sstevel@tonic-gate 		}
57710Sstevel@tonic-gate 		if (secflag != ARCHIVE_XATTR) {
57720Sstevel@tonic-gate 			len = G_p->g_filesz;
57730Sstevel@tonic-gate 		}
57740Sstevel@tonic-gate 	}
57750Sstevel@tonic-gate 
57760Sstevel@tonic-gate 	uid = G_p->g_uid;
57770Sstevel@tonic-gate 	gid = G_p->g_gid;
57780Sstevel@tonic-gate 	/*
57790Sstevel@tonic-gate 	 * Handle EFT uids and gids.  If they get too big
57800Sstevel@tonic-gate 	 * to be represented in a particular format, force 'em to 'nobody'.
57810Sstevel@tonic-gate 	 */
57820Sstevel@tonic-gate 	switch (Hdr_type) {
57830Sstevel@tonic-gate 	case BIN:			/* 16-bits of u_short */
57840Sstevel@tonic-gate 		if ((ulong_t)uid > (ulong_t)USHRT_MAX)
57850Sstevel@tonic-gate 			uid = UID_NOBODY;
57860Sstevel@tonic-gate 		if ((ulong_t)gid > (ulong_t)USHRT_MAX)
57870Sstevel@tonic-gate 			gid = GID_NOBODY;
57880Sstevel@tonic-gate 		break;
57890Sstevel@tonic-gate 	case CHR:			/* %.6lo => 262143 base 10 */
57900Sstevel@tonic-gate 		if ((ulong_t)uid > (ulong_t)0777777)
57910Sstevel@tonic-gate 			uid = UID_NOBODY;
57920Sstevel@tonic-gate 		if ((ulong_t)gid > (ulong_t)0777777)
57930Sstevel@tonic-gate 			gid = GID_NOBODY;
57940Sstevel@tonic-gate 		break;
57950Sstevel@tonic-gate 	case ASC:			/* %.8lx => full 32 bits */
57960Sstevel@tonic-gate 	case CRC:
57970Sstevel@tonic-gate 		break;
57980Sstevel@tonic-gate 	case USTAR:
57990Sstevel@tonic-gate 	case TAR:			/* %.7lo => 2097151 base 10 */
58000Sstevel@tonic-gate 		if ((ulong_t)uid > (ulong_t)07777777)
58010Sstevel@tonic-gate 			uid = UID_NOBODY;
58020Sstevel@tonic-gate 		if ((ulong_t)gid > (ulong_t)07777777)
58030Sstevel@tonic-gate 			gid = GID_NOBODY;
58040Sstevel@tonic-gate 		break;
58050Sstevel@tonic-gate 	default:
58060Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
58070Sstevel@tonic-gate 	}
58080Sstevel@tonic-gate 
58090Sstevel@tonic-gate 	/*
58100Sstevel@tonic-gate 	 * Since cpio formats -don't- encode the symbolic names, print
58110Sstevel@tonic-gate 	 * a warning message when we map the uid or gid this way.
58120Sstevel@tonic-gate 	 * Also, if the ownership just changed, clear set[ug]id bits
58130Sstevel@tonic-gate 	 *
58140Sstevel@tonic-gate 	 * (Except for USTAR format of course, where we have a string
58150Sstevel@tonic-gate 	 * representation of the username embedded in the header)
58160Sstevel@tonic-gate 	 */
58170Sstevel@tonic-gate 	if (uid != G_p->g_uid && Hdr_type != USTAR) {
58180Sstevel@tonic-gate 		msg(ERR, warnfmt,
58190Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
58200Sstevel@tonic-gate 		    G_p->g_nam_p : G_p->g_attrfnam_p,
58210Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
58220Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
58230Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
58240Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p,
58250Sstevel@tonic-gate 		    gettext("uid too large for archive format"));
58260Sstevel@tonic-gate 		if (S_ISREG(mode))
58270Sstevel@tonic-gate 			mode &= ~S_ISUID;
58280Sstevel@tonic-gate 	}
58290Sstevel@tonic-gate 	if (gid != G_p->g_gid && Hdr_type != USTAR) {
58300Sstevel@tonic-gate 		msg(ERR, warnfmt,
58310Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
58320Sstevel@tonic-gate 		    G_p->g_nam_p : G_p->g_attrfnam_p,
58330Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
58340Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
58350Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
58360Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p,
58370Sstevel@tonic-gate 		    gettext("gid too large for archive format"));
58380Sstevel@tonic-gate 		if (S_ISREG(mode))
58390Sstevel@tonic-gate 			mode &= ~S_ISGID;
58400Sstevel@tonic-gate 	}
58410Sstevel@tonic-gate 
58420Sstevel@tonic-gate 	switch (Hdr_type) {
58430Sstevel@tonic-gate 	case BIN:
58440Sstevel@tonic-gate 	case CHR:
58450Sstevel@tonic-gate 	case ASC:
58460Sstevel@tonic-gate 	case CRC:
58470Sstevel@tonic-gate 		cnt = Hdrsz + G_p->g_namesz;
58480Sstevel@tonic-gate 		break;
58490Sstevel@tonic-gate 	case TAR:
58500Sstevel@tonic-gate 		/*FALLTHROUGH*/
58510Sstevel@tonic-gate 	case USTAR: /* TAR and USTAR */
58520Sstevel@tonic-gate 		cnt = TARSZ;
58530Sstevel@tonic-gate 		break;
58540Sstevel@tonic-gate 	default:
58550Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
58560Sstevel@tonic-gate 	}
58570Sstevel@tonic-gate 	FLUSH(cnt);
58580Sstevel@tonic-gate 
58590Sstevel@tonic-gate 	switch (Hdr_type) {
58600Sstevel@tonic-gate 	case BIN:
58610Sstevel@tonic-gate 		Hdr.h_magic = (short)G_p->g_magic;
58620Sstevel@tonic-gate 		Hdr.h_dev = G_p->g_dev;
58630Sstevel@tonic-gate 		Hdr.h_ino = G_p->g_ino;
58640Sstevel@tonic-gate 		Hdr.h_uid = uid;
58650Sstevel@tonic-gate 		Hdr.h_gid = gid;
58660Sstevel@tonic-gate 		Hdr.h_mode = mode;
58670Sstevel@tonic-gate 		Hdr.h_nlink = G_p->g_nlink;
58680Sstevel@tonic-gate 		Hdr.h_rdev = G_p->g_rdev;
58690Sstevel@tonic-gate 		mkshort(Hdr.h_mtime, (long)G_p->g_mtime);
58700Sstevel@tonic-gate 		Hdr.h_namesize = (short)G_p->g_namesz;
58710Sstevel@tonic-gate 		mkshort(Hdr.h_filesize, (long)len);
58720Sstevel@tonic-gate 		(void) strcpy(Hdr.h_name, G_p->g_nam_p);
58730Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, &Hdr, cnt);
58740Sstevel@tonic-gate 		break;
58750Sstevel@tonic-gate 	case CHR:
58760Sstevel@tonic-gate 		(void) sprintf(Buffr.b_in_p,
58770Sstevel@tonic-gate 		    "%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.11lo%.6lo%."
58780Sstevel@tonic-gate 		    "11llo%s", G_p->g_magic, G_p->g_dev, G_p->g_ino, mode,
58790Sstevel@tonic-gate 		    uid, gid, G_p->g_nlink, MK_USHORT(G_p->g_rdev),
58800Sstevel@tonic-gate 		    G_p->g_mtime, (long)G_p->g_namesz, (offset_t)len,
58810Sstevel@tonic-gate 		    G_p->g_nam_p);
58820Sstevel@tonic-gate 		break;
58830Sstevel@tonic-gate 	case ASC:
58840Sstevel@tonic-gate 	case CRC:
58850Sstevel@tonic-gate 		(void) sprintf(Buffr.b_in_p,
58860Sstevel@tonic-gate 		    "%.6lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%."
58870Sstevel@tonic-gate 		    "8lx%.8lx%.8lx%.8lx%s",
58880Sstevel@tonic-gate 			G_p->g_magic, G_p->g_ino, mode, G_p->g_uid,
5889*36Schin 			G_p->g_gid, G_p->g_nlink, G_p->g_mtime, (ulong_t)len,
58900Sstevel@tonic-gate 			major(G_p->g_dev), minor(G_p->g_dev),
58910Sstevel@tonic-gate 			major(G_p->g_rdev), minor(G_p->g_rdev),
58920Sstevel@tonic-gate 			G_p->g_namesz, G_p->g_cksum, G_p->g_nam_p);
58930Sstevel@tonic-gate 		break;
58940Sstevel@tonic-gate 	case USTAR:
58950Sstevel@tonic-gate 		Thdr_p = (union tblock *)Buffr.b_in_p;
58960Sstevel@tonic-gate 		(void) memcpy(Thdr_p, Empty, TARSZ);
58970Sstevel@tonic-gate 		(void) strncpy(Thdr_p->tbuf.t_name, G_p->g_tname,
58980Sstevel@tonic-gate 		    (int)strlen(G_p->g_tname));
58990Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mode, "%07o", (int)mode);
59000Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_uid, "%07o", (int)uid);
59010Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_gid, "%07o", (int)gid);
59020Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_size, "%011llo",
59030Sstevel@tonic-gate 		    (offset_t)len);
59040Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mtime, "%011lo", G_p->g_mtime);
59050Sstevel@tonic-gate 		if (secflag == ARCHIVE_ACL) {
59060Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = 'A';	/* ACL file type */
59070Sstevel@tonic-gate 		} else if (secflag == ARCHIVE_XATTR ||
59080Sstevel@tonic-gate 		    (G_p->g_attrnam_p != (char *)NULL)) {
59090Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = _XATTR_HDRTYPE;
59100Sstevel@tonic-gate 		} else {
59110Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = G_p->g_typeflag;
59120Sstevel@tonic-gate 		}
59130Sstevel@tonic-gate 		if (T_lname[0] != '\0') {
59140Sstevel@tonic-gate 			/*
59150Sstevel@tonic-gate 			 * if not a symbolic link
59160Sstevel@tonic-gate 			 */
59170Sstevel@tonic-gate 			if (((G_p->g_mode & Ftype) != S_IFLNK) &&
59180Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL)) {
59190Sstevel@tonic-gate 				Thdr_p->tbuf.t_typeflag = LNKTYPE;
59200Sstevel@tonic-gate 				(void) sprintf(Thdr_p->tbuf.t_size,
59210Sstevel@tonic-gate 				    "%011lo", 0L);
59220Sstevel@tonic-gate 			}
59230Sstevel@tonic-gate 			(void) strncpy(Thdr_p->tbuf.t_linkname, T_lname,
59240Sstevel@tonic-gate 			    strlen(T_lname));
59250Sstevel@tonic-gate 		}
59260Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_magic, "%s", TMAGIC);
59270Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_version, "%2s", TVERSION);
59280Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_uname, "%s",  G_p->g_uname);
59290Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_gname, "%s", G_p->g_gname);
59300Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_devmajor, "%07o",
59310Sstevel@tonic-gate 		    (int)major(G_p->g_rdev));
59320Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_devminor, "%07o",
59330Sstevel@tonic-gate 		    (int)minor(G_p->g_rdev));
59340Sstevel@tonic-gate 		if (Gen.g_prefix) {
59350Sstevel@tonic-gate 			(void) sprintf(Thdr_p->tbuf.t_prefix, "%s",
59360Sstevel@tonic-gate 			    Gen.g_prefix);
59370Sstevel@tonic-gate 			free(Gen.g_prefix);
59380Sstevel@tonic-gate 			Gen.g_prefix = NULL;
59390Sstevel@tonic-gate 		} else {
59400Sstevel@tonic-gate 			(void) sprintf(Thdr_p->tbuf.t_prefix, "%s", "");
59410Sstevel@tonic-gate 		}
59420Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_cksum, "%07o",
59430Sstevel@tonic-gate 		    (int)cksum(TARTYP, 0, NULL));
59440Sstevel@tonic-gate 		break;
59450Sstevel@tonic-gate 	case TAR:
59460Sstevel@tonic-gate 		Thdr_p = (union tblock *)Buffr.b_in_p;
59470Sstevel@tonic-gate 		(void) memcpy(Thdr_p, Empty, TARSZ);
59480Sstevel@tonic-gate 		(void) strncpy(Thdr_p->tbuf.t_name, G_p->g_nam_p,
59490Sstevel@tonic-gate 		    G_p->g_namesz);
59500Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mode, "%07o ", (int)mode);
59510Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_uid, "%07o ", (int)uid);
59520Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_gid, "%07o ", (int)gid);
59530Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_size, "%011llo ",
59540Sstevel@tonic-gate 		    (offset_t)len);
59550Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mtime, "%011o ",
59560Sstevel@tonic-gate 		    (int)G_p->g_mtime);
59570Sstevel@tonic-gate 		if (T_lname[0] != '\0') {
59580Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = '1';
59590Sstevel@tonic-gate 		} else {
59600Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = '\0';
59610Sstevel@tonic-gate 		}
59620Sstevel@tonic-gate 		(void) strncpy(Thdr_p->tbuf.t_linkname, T_lname,
59630Sstevel@tonic-gate 		    strlen(T_lname));
59640Sstevel@tonic-gate 		break;
59650Sstevel@tonic-gate 	default:
59660Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
59670Sstevel@tonic-gate 	} /* Hdr_type */
59680Sstevel@tonic-gate 
59690Sstevel@tonic-gate 	Buffr.b_in_p += cnt;
59700Sstevel@tonic-gate 	Buffr.b_cnt += cnt;
59710Sstevel@tonic-gate 	pad = ((cnt + Pad_val) & ~Pad_val) - cnt;
59720Sstevel@tonic-gate 	if (pad != 0) {
59730Sstevel@tonic-gate 		FLUSH(pad);
59740Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, Empty, pad);
59750Sstevel@tonic-gate 		Buffr.b_in_p += pad;
59760Sstevel@tonic-gate 		Buffr.b_cnt += pad;
59770Sstevel@tonic-gate 	}
59780Sstevel@tonic-gate }
59790Sstevel@tonic-gate 
59800Sstevel@tonic-gate /*
59810Sstevel@tonic-gate  * write_trail: Create the appropriate trailer for the selected header type
59820Sstevel@tonic-gate  * and bwrite the trailer.  Pad the buffer with nulls out to the next Bufsize
59830Sstevel@tonic-gate  * boundary, and force a write.  If the write completes, or if the trailer is
59840Sstevel@tonic-gate  * completely written (but not all of the padding nulls (as can happen on end
59850Sstevel@tonic-gate  * of medium)) return.  Otherwise, the trailer was not completely written out,
59860Sstevel@tonic-gate  * so re-pad the buffer with nulls and try again.
59870Sstevel@tonic-gate  */
59880Sstevel@tonic-gate 
59890Sstevel@tonic-gate static void
59900Sstevel@tonic-gate write_trail(void)
59910Sstevel@tonic-gate {
59920Sstevel@tonic-gate 	int cnt, need;
59930Sstevel@tonic-gate 
59940Sstevel@tonic-gate 	switch (Hdr_type) {
59950Sstevel@tonic-gate 	case BIN:
59960Sstevel@tonic-gate 		Gen.g_magic = CMN_BIN;
59970Sstevel@tonic-gate 		break;
59980Sstevel@tonic-gate 	case CHR:
59990Sstevel@tonic-gate 		Gen.g_magic = CMN_BIN;
60000Sstevel@tonic-gate 		break;
60010Sstevel@tonic-gate 	case ASC:
60020Sstevel@tonic-gate 		Gen.g_magic = CMN_ASC;
60030Sstevel@tonic-gate 		break;
60040Sstevel@tonic-gate 	case CRC:
60050Sstevel@tonic-gate 		Gen.g_magic = CMN_CRC;
60060Sstevel@tonic-gate 		break;
60070Sstevel@tonic-gate 	}
60080Sstevel@tonic-gate 
60090Sstevel@tonic-gate 	switch (Hdr_type) {
60100Sstevel@tonic-gate 	case BIN:
60110Sstevel@tonic-gate 	case CHR:
60120Sstevel@tonic-gate 	case ASC:
60130Sstevel@tonic-gate 	case CRC:
60140Sstevel@tonic-gate 		Gen.g_mode = Gen.g_uid = Gen.g_gid = 0;
60150Sstevel@tonic-gate 		Gen.g_nlink = 1;
60160Sstevel@tonic-gate 		Gen.g_mtime = Gen.g_ino = Gen.g_dev = 0;
60170Sstevel@tonic-gate 		Gen.g_rdev = Gen.g_cksum = 0;
60180Sstevel@tonic-gate 		Gen.g_filesz = (off_t)0;
60190Sstevel@tonic-gate 		Gen.g_namesz = strlen("TRAILER!!!") + 1;
60200Sstevel@tonic-gate 		(void) strcpy(Gen.g_nam_p, "TRAILER!!!");
60210Sstevel@tonic-gate 		G_p = &Gen;
60220Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
60230Sstevel@tonic-gate 		break;
60240Sstevel@tonic-gate 	case TAR:
60250Sstevel@tonic-gate 	/*FALLTHROUGH*/
60260Sstevel@tonic-gate 	case USTAR: /* TAR and USTAR */
60270Sstevel@tonic-gate 		for (cnt = 0; cnt < 3; cnt++) {
60280Sstevel@tonic-gate 			FLUSH(TARSZ);
60290Sstevel@tonic-gate 			(void) memcpy(Buffr.b_in_p, Empty, TARSZ);
60300Sstevel@tonic-gate 			Buffr.b_in_p += TARSZ;
60310Sstevel@tonic-gate 			Buffr.b_cnt += TARSZ;
60320Sstevel@tonic-gate 		}
60330Sstevel@tonic-gate 		break;
60340Sstevel@tonic-gate 	default:
60350Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
60360Sstevel@tonic-gate 	}
60370Sstevel@tonic-gate 	need = Bufsize - (Buffr.b_cnt % Bufsize);
60380Sstevel@tonic-gate 	if (need == Bufsize)
60390Sstevel@tonic-gate 		need = 0;
60400Sstevel@tonic-gate 
60410Sstevel@tonic-gate 	while (Buffr.b_cnt > 0) {
60420Sstevel@tonic-gate 		while (need > 0) {
60430Sstevel@tonic-gate 			cnt = (need < TARSZ) ? need : TARSZ;
60440Sstevel@tonic-gate 			need -= cnt;
60450Sstevel@tonic-gate 			FLUSH(cnt);
60460Sstevel@tonic-gate 			(void) memcpy(Buffr.b_in_p, Empty, cnt);
60470Sstevel@tonic-gate 			Buffr.b_in_p += cnt;
60480Sstevel@tonic-gate 			Buffr.b_cnt += cnt;
60490Sstevel@tonic-gate 		}
60500Sstevel@tonic-gate 		bflush();
60510Sstevel@tonic-gate 	}
60520Sstevel@tonic-gate }
60530Sstevel@tonic-gate 
60540Sstevel@tonic-gate /*
60550Sstevel@tonic-gate  * if archives in USTAR format, check if typeflag == '5' for directories
60560Sstevel@tonic-gate  */
60570Sstevel@tonic-gate static int
60580Sstevel@tonic-gate ustar_dir(void)
60590Sstevel@tonic-gate {
60600Sstevel@tonic-gate 	if (Hdr_type == USTAR || Hdr_type == TAR) {
60610Sstevel@tonic-gate 		if (Thdr_p->tbuf.t_typeflag == '5')
60620Sstevel@tonic-gate 			return (1);
60630Sstevel@tonic-gate 	}
60640Sstevel@tonic-gate 	return (0);
60650Sstevel@tonic-gate }
60660Sstevel@tonic-gate 
60670Sstevel@tonic-gate /*
60680Sstevel@tonic-gate  * if archives in USTAR format, check if typeflag == '3' || '4' || '6'
60690Sstevel@tonic-gate  * for character, block, fifo special files
60700Sstevel@tonic-gate  */
60710Sstevel@tonic-gate static int
60720Sstevel@tonic-gate ustar_spec(void)
60730Sstevel@tonic-gate {
60740Sstevel@tonic-gate 	int typeflag;
60750Sstevel@tonic-gate 
60760Sstevel@tonic-gate 	if (Hdr_type == USTAR || Hdr_type == TAR) {
60770Sstevel@tonic-gate 		typeflag = Thdr_p->tbuf.t_typeflag;
60780Sstevel@tonic-gate 		if (typeflag == '3' || typeflag == '4' || typeflag == '6')
60790Sstevel@tonic-gate 			return (1);
60800Sstevel@tonic-gate 	}
60810Sstevel@tonic-gate 	return (0);
60820Sstevel@tonic-gate }
60830Sstevel@tonic-gate 
60840Sstevel@tonic-gate /*
60850Sstevel@tonic-gate  * The return value is a pointer to a converted copy of the information in
60860Sstevel@tonic-gate  * FromStat if the file is representable in -Hodc format, and NULL otherwise.
60870Sstevel@tonic-gate  */
60880Sstevel@tonic-gate 
60890Sstevel@tonic-gate static struct stat *
60900Sstevel@tonic-gate convert_to_old_stat(struct stat *FromStat, char *namep, char *attrp)
60910Sstevel@tonic-gate {
60920Sstevel@tonic-gate 	static struct stat ToSt;
60930Sstevel@tonic-gate 	cpioinfo_t TmpSt;
60940Sstevel@tonic-gate 
60950Sstevel@tonic-gate 	(void) memset(&TmpSt, 0, sizeof (cpioinfo_t));
60960Sstevel@tonic-gate 	stat_to_svr32_stat(&TmpSt, FromStat);
60970Sstevel@tonic-gate 	(void) memset(&ToSt, 0, sizeof (ToSt));
60980Sstevel@tonic-gate 
60990Sstevel@tonic-gate 	if (TmpSt.st_rdev == (o_dev_t)NODEV &&
61000Sstevel@tonic-gate 	    (((TmpSt.st_mode & Ftype) == S_IFCHR) ||
61010Sstevel@tonic-gate 	    ((TmpSt.st_mode & Ftype) == S_IFBLK))) {
61020Sstevel@tonic-gate 		/*
61030Sstevel@tonic-gate 		 * Encountered a problem representing the rdev information.
61040Sstevel@tonic-gate 		 * Don't archive it.
61050Sstevel@tonic-gate 		 */
61060Sstevel@tonic-gate 
61070Sstevel@tonic-gate 		msg(ERR, "Error -Hodc format can't support expanded"
61080Sstevel@tonic-gate 		    "types on %s%s%s",
61090Sstevel@tonic-gate 		    namep,
61100Sstevel@tonic-gate 		    (attrp == NULL) ? "" : gettext(" Attribute"),
61110Sstevel@tonic-gate 		    (attrp == NULL) ? "" : attrp);
61120Sstevel@tonic-gate 		return (NULL);
61130Sstevel@tonic-gate 	}
61140Sstevel@tonic-gate 
61150Sstevel@tonic-gate 	if (TmpSt.st_dev == (o_dev_t)NODEV) {
61160Sstevel@tonic-gate 		/*
61170Sstevel@tonic-gate 		 * Having trouble representing the device/inode pair.  We can't
61180Sstevel@tonic-gate 		 * track links in this case; break them all into separate
61190Sstevel@tonic-gate 		 * files.
61200Sstevel@tonic-gate 		 */
61210Sstevel@tonic-gate 
61220Sstevel@tonic-gate 		TmpSt.st_ino = 0;
61230Sstevel@tonic-gate 
61240Sstevel@tonic-gate 		if (((TmpSt.st_mode & Ftype) != S_IFDIR) &&
61250Sstevel@tonic-gate 		    TmpSt.st_nlink > 1)
61260Sstevel@tonic-gate 			msg(POST,
61270Sstevel@tonic-gate 			    "Warning: file %s%s%s has large "
61280Sstevel@tonic-gate 			    "device number - linked "
61290Sstevel@tonic-gate 			    "files will be restored as "
61300Sstevel@tonic-gate 			    "separate files",
61310Sstevel@tonic-gate 			    namep,
61320Sstevel@tonic-gate 			    (attrp == NULL) ? "" : gettext(" Attribute"),
61330Sstevel@tonic-gate 			    (attrp == NULL) ? "" : attrp);
61340Sstevel@tonic-gate 
61350Sstevel@tonic-gate 		/* ensure no links */
61360Sstevel@tonic-gate 
61370Sstevel@tonic-gate 		TmpSt.st_nlink = 1;
61380Sstevel@tonic-gate 	}
61390Sstevel@tonic-gate 
61400Sstevel@tonic-gate 	/* Start converting values */
61410Sstevel@tonic-gate 
61420Sstevel@tonic-gate 	if (TmpSt.st_dev < 0) {
61430Sstevel@tonic-gate 		ToSt.st_dev = 0;
61440Sstevel@tonic-gate 	} else {
61450Sstevel@tonic-gate 		ToSt.st_dev = (dev_t)TmpSt.st_dev;
61460Sstevel@tonic-gate 	}
61470Sstevel@tonic-gate 
61480Sstevel@tonic-gate 	/* -actual- not truncated uid */
61490Sstevel@tonic-gate 
61500Sstevel@tonic-gate 	ToSt.st_uid = TmpSt.st_uid;
61510Sstevel@tonic-gate 
61520Sstevel@tonic-gate 	/* -actual- not truncated gid */
61530Sstevel@tonic-gate 
61540Sstevel@tonic-gate 	ToSt.st_gid = TmpSt.st_gid;
61550Sstevel@tonic-gate 	ToSt.st_ino = (ino_t)TmpSt.st_ino;
61560Sstevel@tonic-gate 	ToSt.st_mode = (mode_t)TmpSt.st_mode;
61570Sstevel@tonic-gate 	ToSt.st_mtime = (ulong_t)TmpSt.st_modtime;
61580Sstevel@tonic-gate 	ToSt.st_nlink = (nlink_t)TmpSt.st_nlink;
61590Sstevel@tonic-gate 	ToSt.st_size = (off_t)TmpSt.st_size;
61600Sstevel@tonic-gate 	ToSt.st_rdev = (dev_t)TmpSt.st_rdev;
61610Sstevel@tonic-gate 
61620Sstevel@tonic-gate 	return (&ToSt);
61630Sstevel@tonic-gate }
61640Sstevel@tonic-gate 
61650Sstevel@tonic-gate /*
61660Sstevel@tonic-gate  * In the beginning of each bar archive, there is a header which describes the
61670Sstevel@tonic-gate  * current volume being created, followed by a header which describes the
61680Sstevel@tonic-gate  * current file being created, followed by the file itself.  If there is
61690Sstevel@tonic-gate  * more than one file to be created, a separate header will be created for
61700Sstevel@tonic-gate  * each additional file.  This structure may be repeated if the bar archive
61710Sstevel@tonic-gate  * contains multiple volumes.  If a file spans across volumes, its header
61720Sstevel@tonic-gate  * will not be repeated in the next volume.
61730Sstevel@tonic-gate  *               +------------------+
61740Sstevel@tonic-gate  *               |    vol header    |
61750Sstevel@tonic-gate  *               |------------------|
61760Sstevel@tonic-gate  *               |   file header i  |     i = 0
61770Sstevel@tonic-gate  *               |------------------|
61780Sstevel@tonic-gate  *               |     <file i>     |
61790Sstevel@tonic-gate  *               |------------------|
61800Sstevel@tonic-gate  *               |  file header i+1 |
61810Sstevel@tonic-gate  *               |------------------|
61820Sstevel@tonic-gate  *               |    <file i+1>    |
61830Sstevel@tonic-gate  *               |------------------|
61840Sstevel@tonic-gate  *               |        .         |
61850Sstevel@tonic-gate  *               |        .         |
61860Sstevel@tonic-gate  *               |        .         |
61870Sstevel@tonic-gate  *               +------------------+
61880Sstevel@tonic-gate  */
61890Sstevel@tonic-gate 
61900Sstevel@tonic-gate /*
61910Sstevel@tonic-gate  * read in the header that describes the current volume of the bar archive
61920Sstevel@tonic-gate  * to be extracted.
61930Sstevel@tonic-gate  */
61940Sstevel@tonic-gate static void
61950Sstevel@tonic-gate read_bar_vol_hdr(void)
61960Sstevel@tonic-gate {
61970Sstevel@tonic-gate 	union b_block *tmp_hdr;
61980Sstevel@tonic-gate 
61990Sstevel@tonic-gate 	tmp_hdr = (union b_block *)Buffr.b_out_p;
62000Sstevel@tonic-gate 	if (tmp_hdr->dbuf.bar_magic[0] == BAR_VOLUME_MAGIC) {
62010Sstevel@tonic-gate 
62020Sstevel@tonic-gate 		if (bar_Vhdr == NULL) {
62030Sstevel@tonic-gate 			bar_Vhdr = e_zalloc(E_EXIT, TBLOCK);
62040Sstevel@tonic-gate 		}
62050Sstevel@tonic-gate 		(void) memcpy(&(bar_Vhdr->dbuf), &(tmp_hdr->dbuf), TBLOCK);
62060Sstevel@tonic-gate 	} else {
62070Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
62080Sstevel@tonic-gate 		    "bar error: cannot read volume header\n"));
62090Sstevel@tonic-gate 		exit(1);
62100Sstevel@tonic-gate 	}
62110Sstevel@tonic-gate 
62120Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.mode, "%8lo", &Gen_bar_vol.g_mode);
62130Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.uid, "%8d", (int *)&Gen_bar_vol.g_uid);
62140Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.gid, "%8d", (int *)&Gen_bar_vol.g_gid);
62150Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.size, "%12llo",
62160Sstevel@tonic-gate 	    (u_off_t *)&Gen_bar_vol.g_filesz);
62170Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.mtime, "%12lo", &Gen_bar_vol.g_mtime);
62180Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.chksum, "%8lo", &Gen_bar_vol.g_cksum);
62190Sstevel@tonic-gate 
62200Sstevel@tonic-gate 	/* set the compress flag */
62210Sstevel@tonic-gate 	if (bar_Vhdr->dbuf.compressed == '1')
62220Sstevel@tonic-gate 		Compressed = 1;
62230Sstevel@tonic-gate 	else
62240Sstevel@tonic-gate 		Compressed = 0;
62250Sstevel@tonic-gate 
62260Sstevel@tonic-gate 	Buffr.b_out_p += 512;
62270Sstevel@tonic-gate 	Buffr.b_cnt -= 512;
62280Sstevel@tonic-gate 
62290Sstevel@tonic-gate 	/*
62300Sstevel@tonic-gate 	 * not the first volume; exit
62310Sstevel@tonic-gate 	 */
62320Sstevel@tonic-gate 	if (strcmp(bar_Vhdr->dbuf.volume_num, "1") != 0) {
62330Sstevel@tonic-gate 		(void) fprintf(stderr,
62340Sstevel@tonic-gate 		    gettext("error: This is not volume 1.  "));
62350Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("This is volume %s.  "),
62360Sstevel@tonic-gate 		    bar_Vhdr->dbuf.volume_num);
62370Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Please insert volume 1.\n"));
62380Sstevel@tonic-gate 		exit(1);
62390Sstevel@tonic-gate 	}
62400Sstevel@tonic-gate 
62410Sstevel@tonic-gate 	read_bar_file_hdr();
62420Sstevel@tonic-gate }
62430Sstevel@tonic-gate 
62440Sstevel@tonic-gate /*
62450Sstevel@tonic-gate  * read in the header that describes the current file to be extracted
62460Sstevel@tonic-gate  */
62470Sstevel@tonic-gate static void
62480Sstevel@tonic-gate read_bar_file_hdr(void)
62490Sstevel@tonic-gate {
62500Sstevel@tonic-gate 	union b_block *tmp_hdr;
62510Sstevel@tonic-gate 	char *start_of_name, *name_p;
62520Sstevel@tonic-gate 	char *tmp;
62530Sstevel@tonic-gate 
62540Sstevel@tonic-gate 	if (*Buffr.b_out_p == '\0') {
62550Sstevel@tonic-gate 		*Gen.g_nam_p = '\0';
62560Sstevel@tonic-gate 		exit(0);
62570Sstevel@tonic-gate 	}
62580Sstevel@tonic-gate 
62590Sstevel@tonic-gate 	tmp_hdr = (union b_block *)Buffr.b_out_p;
62600Sstevel@tonic-gate 
62610Sstevel@tonic-gate 	tmp = &tmp_hdr->dbuf.mode[1];
62620Sstevel@tonic-gate 	(void) sscanf(tmp, "%8lo", &Gen.g_mode);
62630Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.uid, "%8lo", &Gen.g_uid);
62640Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.gid, "%8lo", &Gen.g_gid);
62650Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.size, "%12llo",
62660Sstevel@tonic-gate 	    (u_off_t *)&Gen.g_filesz);
62670Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.mtime, "%12lo", &Gen.g_mtime);
62680Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.chksum, "%8lo", &Gen.g_cksum);
62690Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.rdev, "%8lo", &Gen.g_rdev);
62700Sstevel@tonic-gate 
62710Sstevel@tonic-gate #define	to_new_major(x)	(int)((unsigned)((x) & OMAXMAJ) << NBITSMINOR)
62720Sstevel@tonic-gate #define	to_new_minor(x)	(int)((x) & OMAXMIN)
62730Sstevel@tonic-gate 	Gen.g_rdev = to_new_major(Gen.g_rdev) | to_new_minor(Gen.g_rdev);
62740Sstevel@tonic-gate 	bar_linkflag = tmp_hdr->dbuf.linkflag;
62750Sstevel@tonic-gate 	start_of_name = &tmp_hdr->dbuf.start_of_name;
62760Sstevel@tonic-gate 
62770Sstevel@tonic-gate 
62780Sstevel@tonic-gate 	name_p = Gen.g_nam_p;
62790Sstevel@tonic-gate 	while (*name_p++ = *start_of_name++)
62800Sstevel@tonic-gate 		;
62810Sstevel@tonic-gate 	*name_p = '\0';
62820Sstevel@tonic-gate 	if (bar_linkflag == LNKTYPE || bar_linkflag == SYMTYPE)
62830Sstevel@tonic-gate 		(void) strcpy(bar_linkname, start_of_name);
62840Sstevel@tonic-gate 
62850Sstevel@tonic-gate 	Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
62860Sstevel@tonic-gate 	(void) strcpy(nambuf, Gen.g_nam_p);
62870Sstevel@tonic-gate }
62880Sstevel@tonic-gate 
62890Sstevel@tonic-gate /*
62900Sstevel@tonic-gate  * if the bar archive is compressed, set up a pipe and do the de-compression
62910Sstevel@tonic-gate  * as the compressed file is read in.
62920Sstevel@tonic-gate  */
62930Sstevel@tonic-gate static void
62940Sstevel@tonic-gate setup_uncompress(FILE **pipef)
62950Sstevel@tonic-gate {
62960Sstevel@tonic-gate 	char *cmd_buf;
62970Sstevel@tonic-gate 	size_t cmdlen;
62980Sstevel@tonic-gate 
62990Sstevel@tonic-gate 	cmd_buf = e_zalloc(E_EXIT, MAXPATHLEN * 2);
63000Sstevel@tonic-gate 
63010Sstevel@tonic-gate 	if (access(Gen.g_nam_p, W_OK) != 0) {
63020Sstevel@tonic-gate 		cmdlen = snprintf(cmd_buf, MAXPATHLEN * 2,
63030Sstevel@tonic-gate 		    "chmod +w '%s'; uncompress -c > '%s'; "
63040Sstevel@tonic-gate 		    "chmod 0%o '%s'",
63050Sstevel@tonic-gate 		    Gen.g_nam_p, Gen.g_nam_p, (int)G_p->g_mode, Gen.g_nam_p);
63060Sstevel@tonic-gate 	} else {
63070Sstevel@tonic-gate 		cmdlen = snprintf(cmd_buf, MAXPATHLEN * 2,
63080Sstevel@tonic-gate 		    "uncompress -c > '%s'", Gen.g_nam_p);
63090Sstevel@tonic-gate 	}
63100Sstevel@tonic-gate 
63110Sstevel@tonic-gate 	if (cmdlen >= MAXPATHLEN * 2 ||
63120Sstevel@tonic-gate 	    (*pipef = popen(cmd_buf, "w")) == NULL) {
63130Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("error\n"));
63140Sstevel@tonic-gate 		exit(1);
63150Sstevel@tonic-gate 	}
63160Sstevel@tonic-gate 
63170Sstevel@tonic-gate 	if (close(Ofile) != 0)
63180Sstevel@tonic-gate 		msg(EXTN, "close error");
63190Sstevel@tonic-gate 	Ofile = fileno(*pipef);
63200Sstevel@tonic-gate 
63210Sstevel@tonic-gate 	free(cmd_buf);
63220Sstevel@tonic-gate }
63230Sstevel@tonic-gate 
63240Sstevel@tonic-gate /*
63250Sstevel@tonic-gate  * if the bar archive spans multiple volumes, read in the header that
63260Sstevel@tonic-gate  * describes the next volume.
63270Sstevel@tonic-gate  */
63280Sstevel@tonic-gate static void
63290Sstevel@tonic-gate skip_bar_volhdr(void)
63300Sstevel@tonic-gate {
63310Sstevel@tonic-gate 	char *buff;
63320Sstevel@tonic-gate 	union b_block *tmp_hdr;
63330Sstevel@tonic-gate 
63340Sstevel@tonic-gate 	buff = e_zalloc(E_EXIT, (uint_t)Bufsize);
63350Sstevel@tonic-gate 
63360Sstevel@tonic-gate 	if (g_read(Device, Archive, buff, Bufsize) < 0) {
63370Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
63380Sstevel@tonic-gate 		    "error in skip_bar_volhdr\n"));
63390Sstevel@tonic-gate 	} else {
63400Sstevel@tonic-gate 
63410Sstevel@tonic-gate 		tmp_hdr = (union b_block *)buff;
63420Sstevel@tonic-gate 		if (tmp_hdr->dbuf.bar_magic[0] == BAR_VOLUME_MAGIC) {
63430Sstevel@tonic-gate 
63440Sstevel@tonic-gate 			if (bar_Vhdr == NULL) {
63450Sstevel@tonic-gate 				bar_Vhdr = e_zalloc(E_EXIT, TBLOCK);
63460Sstevel@tonic-gate 			}
63470Sstevel@tonic-gate 			(void) memcpy(&(bar_Vhdr->dbuf),
63480Sstevel@tonic-gate 			    &(tmp_hdr->dbuf), TBLOCK);
63490Sstevel@tonic-gate 		} else {
63500Sstevel@tonic-gate 			(void) fprintf(stderr,
63510Sstevel@tonic-gate 			    gettext("cpio error: cannot read bar volume "
63520Sstevel@tonic-gate 			    "header\n"));
63530Sstevel@tonic-gate 			exit(1);
63540Sstevel@tonic-gate 		}
63550Sstevel@tonic-gate 
63560Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.mode, "%8lo",
63570Sstevel@tonic-gate 		    &Gen_bar_vol.g_mode);
63580Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.uid, "%8lo",
63590Sstevel@tonic-gate 		    &Gen_bar_vol.g_uid);
63600Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.gid, "%8lo",
63610Sstevel@tonic-gate 		    &Gen_bar_vol.g_gid);
63620Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.size, "%12llo",
63630Sstevel@tonic-gate 		    (u_off_t *)&Gen_bar_vol.g_filesz);
63640Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.mtime, "%12lo",
63650Sstevel@tonic-gate 		    &Gen_bar_vol.g_mtime);
63660Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.chksum, "%8lo",
63670Sstevel@tonic-gate 		    &Gen_bar_vol.g_cksum);
63680Sstevel@tonic-gate 		if (bar_Vhdr->dbuf.compressed == '1')
63690Sstevel@tonic-gate 			Compressed = 1;
63700Sstevel@tonic-gate 		else
63710Sstevel@tonic-gate 			Compressed = 0;
63720Sstevel@tonic-gate 	}
63730Sstevel@tonic-gate 
63740Sstevel@tonic-gate 	/*
63750Sstevel@tonic-gate 	 * Now put the rest of the bytes read in into the data buffer.
63760Sstevel@tonic-gate 	 */
63770Sstevel@tonic-gate 	(void) memcpy(Buffr.b_in_p, &buff[512], (Bufsize - 512));
63780Sstevel@tonic-gate 	Buffr.b_in_p += (Bufsize - 512);
63790Sstevel@tonic-gate 	Buffr.b_cnt += (long)(Bufsize - 512);
63800Sstevel@tonic-gate 
63810Sstevel@tonic-gate 	free(buff);
63820Sstevel@tonic-gate }
63830Sstevel@tonic-gate 
63840Sstevel@tonic-gate /*
63850Sstevel@tonic-gate  * check the linkflag which indicates the type of the file to be extracted,
63860Sstevel@tonic-gate  * invoke the corresponding routine to extract the file.
63870Sstevel@tonic-gate  */
63880Sstevel@tonic-gate static void
63890Sstevel@tonic-gate bar_file_in(void)
63900Sstevel@tonic-gate {
63910Sstevel@tonic-gate 	/*
63920Sstevel@tonic-gate 	 * the file is a directory
63930Sstevel@tonic-gate 	 */
63940Sstevel@tonic-gate 	if (Adir) {
63950Sstevel@tonic-gate 		if (ckname(1) != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
63960Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
63970Sstevel@tonic-gate 		}
63980Sstevel@tonic-gate 		return;
63990Sstevel@tonic-gate 	}
64000Sstevel@tonic-gate 
64010Sstevel@tonic-gate 	switch (bar_linkflag) {
64020Sstevel@tonic-gate 	case REGTYPE:
64030Sstevel@tonic-gate 		/* regular file */
64040Sstevel@tonic-gate 		if ((ckname(1) == F_SKIP) ||
64050Sstevel@tonic-gate 		    (Ofile = openout(G_p->g_dirfd)) < 0) {
64060Sstevel@tonic-gate 			data_in(P_SKIP);
64070Sstevel@tonic-gate 		} else {
64080Sstevel@tonic-gate 			data_in(P_PROC);
64090Sstevel@tonic-gate 		}
64100Sstevel@tonic-gate 		break;
64110Sstevel@tonic-gate 	case LNKTYPE:
64120Sstevel@tonic-gate 		/* hard link */
64130Sstevel@tonic-gate 		if (ckname(1) == F_SKIP) {
64140Sstevel@tonic-gate 			break;
64150Sstevel@tonic-gate 		}
64160Sstevel@tonic-gate 		(void) creat_lnk(G_p->g_dirfd, bar_linkname, G_p->g_nam_p);
64170Sstevel@tonic-gate 		break;
64180Sstevel@tonic-gate 	case SYMTYPE:
64190Sstevel@tonic-gate 		/* symbolic link */
64200Sstevel@tonic-gate 		if ((ckname(1) == F_SKIP) ||
64210Sstevel@tonic-gate 		    (Ofile = openout(G_p->g_dirfd)) < 0) {
64220Sstevel@tonic-gate 			data_in(P_SKIP);
64230Sstevel@tonic-gate 		} else {
64240Sstevel@tonic-gate 			data_in(P_PROC);
64250Sstevel@tonic-gate 		}
64260Sstevel@tonic-gate 		break;
64270Sstevel@tonic-gate 	case CHRTYPE:
64280Sstevel@tonic-gate 		/* character device or FIFO */
64290Sstevel@tonic-gate 		if (ckname(1) != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
64300Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
64310Sstevel@tonic-gate 		}
64320Sstevel@tonic-gate 		break;
64330Sstevel@tonic-gate 	default:
64340Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("error: unknown file type\n"));
64350Sstevel@tonic-gate 		break;
64360Sstevel@tonic-gate 	}
64370Sstevel@tonic-gate }
64380Sstevel@tonic-gate 
64390Sstevel@tonic-gate 
64400Sstevel@tonic-gate /*
64410Sstevel@tonic-gate  * This originally came from libgenIO/g_init.c
64420Sstevel@tonic-gate  * XXX	And it is very broken.
64430Sstevel@tonic-gate  */
64440Sstevel@tonic-gate 
64450Sstevel@tonic-gate /* #include <sys/statvfs.h> */
64460Sstevel@tonic-gate #include <ftw.h>
64470Sstevel@tonic-gate /* #include <libgenIO.h> */
64480Sstevel@tonic-gate #define	G_TM_TAPE	1	/* Tapemaster controller    */
64490Sstevel@tonic-gate #define	G_XY_DISK	3	/* xy disks		*/
64500Sstevel@tonic-gate #define	G_SD_DISK	7	/* scsi sd disk		*/
64510Sstevel@tonic-gate #define	G_XT_TAPE	8	/* xt tapes		*/
64520Sstevel@tonic-gate #define	G_SF_FLOPPY	9	/* sf floppy		*/
64530Sstevel@tonic-gate #define	G_XD_DISK	10	/* xd disks		*/
64540Sstevel@tonic-gate #define	G_ST_TAPE	11	/* scsi tape		*/
64550Sstevel@tonic-gate #define	G_NS		12	/* noswap pseudo-dev	*/
64560Sstevel@tonic-gate #define	G_RAM		13	/* ram pseudo-dev	*/
64570Sstevel@tonic-gate #define	G_FT		14	/* tftp			*/
64580Sstevel@tonic-gate #define	G_HD		15	/* 386 network disk	*/
64590Sstevel@tonic-gate #define	G_FD		16	/* 386 AT disk		*/
64600Sstevel@tonic-gate #define	G_FILE		28	/* file, not a device	*/
64610Sstevel@tonic-gate #define	G_NO_DEV	29	/* device does not require special treatment */
64620Sstevel@tonic-gate #define	G_DEV_MAX	30	/* last valid device type */
64630Sstevel@tonic-gate 
64640Sstevel@tonic-gate /*
64650Sstevel@tonic-gate  * g_init: Determine the device being accessed, set the buffer size,
64660Sstevel@tonic-gate  * and perform any device specific initialization. Since at this point
64670Sstevel@tonic-gate  * Sun has no system call to read the configuration, the major numbers
64680Sstevel@tonic-gate  * are assumed to be static and types are figured out as such. However,
64690Sstevel@tonic-gate  * as a rough estimate, the buffer size for all types is set to 512
64700Sstevel@tonic-gate  * as a default.
64710Sstevel@tonic-gate  */
64720Sstevel@tonic-gate 
64730Sstevel@tonic-gate static int
64740Sstevel@tonic-gate g_init(int *devtype, int *fdes)
64750Sstevel@tonic-gate {
64760Sstevel@tonic-gate 	int bufsize;
64770Sstevel@tonic-gate 	struct stat st_buf;
64780Sstevel@tonic-gate 	struct statvfs stfs_buf;
64790Sstevel@tonic-gate 
64800Sstevel@tonic-gate 	*devtype = G_NO_DEV;
64810Sstevel@tonic-gate 	bufsize = -1;
64820Sstevel@tonic-gate 	if (fstat(*fdes, &st_buf) == -1)
64830Sstevel@tonic-gate 		return (-1);
64840Sstevel@tonic-gate 	if (!(st_buf.st_mode & S_IFCHR) || !(st_buf.st_mode & S_IFBLK)) {
64850Sstevel@tonic-gate 		if (st_buf.st_mode & S_IFIFO) {
64860Sstevel@tonic-gate 			bufsize = 512;
64870Sstevel@tonic-gate 		} else {
64880Sstevel@tonic-gate 			/* find block size for this file system */
64890Sstevel@tonic-gate 			*devtype = G_FILE;
64900Sstevel@tonic-gate 			if (fstatvfs(*fdes, &stfs_buf) < 0) {
64910Sstevel@tonic-gate 					bufsize = -1;
64920Sstevel@tonic-gate 					errno = ENODEV;
64930Sstevel@tonic-gate 			} else
64940Sstevel@tonic-gate 				bufsize = stfs_buf.f_bsize;
64950Sstevel@tonic-gate 		}
64960Sstevel@tonic-gate 
64970Sstevel@tonic-gate 		return (bufsize);
64980Sstevel@tonic-gate 
64990Sstevel@tonic-gate 	/*
65000Sstevel@tonic-gate 	 * We'll have to add a remote attribute to stat but this
65010Sstevel@tonic-gate 	 * should work for now.
65020Sstevel@tonic-gate 	 */
65030Sstevel@tonic-gate 	} else if (st_buf.st_dev & 0x8000)	/* if remote  rdev */
65040Sstevel@tonic-gate 		return (512);
65050Sstevel@tonic-gate 
65060Sstevel@tonic-gate 	bufsize = 512;
65070Sstevel@tonic-gate 
65080Sstevel@tonic-gate 	if (Hdr_type == BAR) {
65090Sstevel@tonic-gate 		if (is_tape(*fdes)) {
65100Sstevel@tonic-gate 			bufsize = BAR_TAPE_SIZE;
65110Sstevel@tonic-gate 			(void) fprintf(stderr, "Archiving to tape");
65120Sstevel@tonic-gate 			(void) fprintf(stderr, " blocking factor 126\n");
65130Sstevel@tonic-gate 		} else if (is_floppy(*fdes)) {
65140Sstevel@tonic-gate 			bufsize = BAR_FLOPPY_SIZE;
65150Sstevel@tonic-gate 			(void) fprintf(stderr, "Archiving to floppy");
65160Sstevel@tonic-gate 			(void) fprintf(stderr, " blocking factor 18\n");
65170Sstevel@tonic-gate 		}
65180Sstevel@tonic-gate 	}
65190Sstevel@tonic-gate 
65200Sstevel@tonic-gate 	return (bufsize);
65210Sstevel@tonic-gate }
65220Sstevel@tonic-gate 
65230Sstevel@tonic-gate /*
65240Sstevel@tonic-gate  * This originally came from libgenIO/g_read.c
65250Sstevel@tonic-gate  */
65260Sstevel@tonic-gate 
65270Sstevel@tonic-gate /*
65280Sstevel@tonic-gate  * g_read: Read nbytes of data from fdes (of type devtype) and place
65290Sstevel@tonic-gate  * data in location pointed to by buf.  In case of end of medium,
65300Sstevel@tonic-gate  * translate (where necessary) device specific EOM indications into
65310Sstevel@tonic-gate  * the generic EOM indication of rv = -1, errno = ENOSPC.
65320Sstevel@tonic-gate  */
65330Sstevel@tonic-gate 
65340Sstevel@tonic-gate static int
65350Sstevel@tonic-gate g_read(int devtype, int fdes, char *buf, unsigned nbytes)
65360Sstevel@tonic-gate {
65370Sstevel@tonic-gate 	int rv;
65380Sstevel@tonic-gate 
65390Sstevel@tonic-gate 	if (devtype < 0 || devtype >= G_DEV_MAX) {
65400Sstevel@tonic-gate 		errno = ENODEV;
65410Sstevel@tonic-gate 		return (-1);
65420Sstevel@tonic-gate 	}
65430Sstevel@tonic-gate 
65440Sstevel@tonic-gate 	rv = read(fdes, buf, nbytes);
65450Sstevel@tonic-gate 
65460Sstevel@tonic-gate 	/* st devices return 0 when no space left */
65470Sstevel@tonic-gate 	if ((rv == 0 && errno == 0 && Hdr_type != BAR) ||
65480Sstevel@tonic-gate 	    (rv == -1 && errno == EIO)) {
65490Sstevel@tonic-gate 		errno = 0;
65500Sstevel@tonic-gate 		rv = 0;
65510Sstevel@tonic-gate 	}
65520Sstevel@tonic-gate 
65530Sstevel@tonic-gate 	return (rv);
65540Sstevel@tonic-gate }
65550Sstevel@tonic-gate 
65560Sstevel@tonic-gate /*
65570Sstevel@tonic-gate  * This originally came from libgenIO/g_write.c
65580Sstevel@tonic-gate  */
65590Sstevel@tonic-gate 
65600Sstevel@tonic-gate /*
65610Sstevel@tonic-gate  * g_write: Write nbytes of data to fdes (of type devtype) from
65620Sstevel@tonic-gate  * the location pointed to by buf.  In case of end of medium,
65630Sstevel@tonic-gate  * translate (where necessary) device specific EOM indications into
65640Sstevel@tonic-gate  * the generic EOM indication of rv = -1, errno = ENOSPC.
65650Sstevel@tonic-gate  */
65660Sstevel@tonic-gate 
65670Sstevel@tonic-gate static int
65680Sstevel@tonic-gate g_write(int devtype, int fdes, char *buf, unsigned nbytes)
65690Sstevel@tonic-gate {
65700Sstevel@tonic-gate 	int rv;
65710Sstevel@tonic-gate 
65720Sstevel@tonic-gate 	if (devtype < 0 || devtype >= G_DEV_MAX) {
65730Sstevel@tonic-gate 		errno = ENODEV;
65740Sstevel@tonic-gate 		return (-1);
65750Sstevel@tonic-gate 	}
65760Sstevel@tonic-gate 
65770Sstevel@tonic-gate 	rv = write(fdes, buf, nbytes);
65780Sstevel@tonic-gate 
65790Sstevel@tonic-gate 	/* st devices return 0 when no more space left */
65800Sstevel@tonic-gate 	if ((rv == 0 && errno == 0) || (rv == -1 && errno == EIO)) {
65810Sstevel@tonic-gate 		errno = ENOSPC;
65820Sstevel@tonic-gate 		rv = -1;
65830Sstevel@tonic-gate 	}
65840Sstevel@tonic-gate 
65850Sstevel@tonic-gate 	return (rv);
65860Sstevel@tonic-gate }
65870Sstevel@tonic-gate 
65880Sstevel@tonic-gate /*
65890Sstevel@tonic-gate  * Test for tape
65900Sstevel@tonic-gate  */
65910Sstevel@tonic-gate 
65920Sstevel@tonic-gate static int
65930Sstevel@tonic-gate is_tape(int fd)
65940Sstevel@tonic-gate {
65950Sstevel@tonic-gate 	struct mtget stuff;
65960Sstevel@tonic-gate 
65970Sstevel@tonic-gate 	/*
65980Sstevel@tonic-gate 	 * try to do a generic tape ioctl, just to see if
65990Sstevel@tonic-gate 	 * the thing is in fact a tape drive(er).
66000Sstevel@tonic-gate 	 */
66010Sstevel@tonic-gate 	if (ioctl(fd, MTIOCGET, &stuff) != -1) {
66020Sstevel@tonic-gate 		/* the ioctl succeeded, must have been a tape */
66030Sstevel@tonic-gate 		return (1);
66040Sstevel@tonic-gate 	}
66050Sstevel@tonic-gate 	return (0);
66060Sstevel@tonic-gate }
66070Sstevel@tonic-gate 
66080Sstevel@tonic-gate /*
66090Sstevel@tonic-gate  * Test for floppy
66100Sstevel@tonic-gate  */
66110Sstevel@tonic-gate 
66120Sstevel@tonic-gate static int
66130Sstevel@tonic-gate is_floppy(int fd)
66140Sstevel@tonic-gate {
66150Sstevel@tonic-gate 	struct fd_char stuff;
66160Sstevel@tonic-gate 
66170Sstevel@tonic-gate 	/*
66180Sstevel@tonic-gate 	 * try to get the floppy drive characteristics, just to see if
66190Sstevel@tonic-gate 	 * the thing is in fact a floppy drive(er).
66200Sstevel@tonic-gate 	 */
66210Sstevel@tonic-gate 	if (ioctl(fd, FDIOGCHAR, &stuff) != -1) {
66220Sstevel@tonic-gate 		/* the ioctl succeeded, must have been a floppy */
66230Sstevel@tonic-gate 		return (1);
66240Sstevel@tonic-gate 	}
66250Sstevel@tonic-gate 
66260Sstevel@tonic-gate 	return (0);
66270Sstevel@tonic-gate }
66280Sstevel@tonic-gate 
66290Sstevel@tonic-gate /*
66300Sstevel@tonic-gate  * New functions for ACLs and other security attributes
66310Sstevel@tonic-gate  */
66320Sstevel@tonic-gate 
66330Sstevel@tonic-gate /*
66340Sstevel@tonic-gate  * The function appends the new security attribute info to the end of
66350Sstevel@tonic-gate  * existing secinfo.
66360Sstevel@tonic-gate  */
66370Sstevel@tonic-gate static int
66380Sstevel@tonic-gate append_secattr(
66390Sstevel@tonic-gate 	char	**secinfo,	/* existing security info */
66400Sstevel@tonic-gate 	int	*secinfo_len,	/* length of existing security info */
66410Sstevel@tonic-gate 	int	size,		/* new attribute size: unit depends on type */
66420Sstevel@tonic-gate 	char	*attrp,		/* new attribute data pointer */
66430Sstevel@tonic-gate 	char	attr_type)	/* new attribute type */
66440Sstevel@tonic-gate {
66450Sstevel@tonic-gate 	char	*new_secinfo;
66460Sstevel@tonic-gate 	char	*attrtext;
66470Sstevel@tonic-gate 	size_t	newattrsize;
66480Sstevel@tonic-gate 	int	oldsize;
66490Sstevel@tonic-gate 
66500Sstevel@tonic-gate 	/* no need to add */
66510Sstevel@tonic-gate 	if (attrp == (char *)NULL) {
66520Sstevel@tonic-gate 		return (0);
66530Sstevel@tonic-gate 	}
66540Sstevel@tonic-gate 
66550Sstevel@tonic-gate 	switch (attr_type) {
66560Sstevel@tonic-gate 	case UFSD_ACL:
66570Sstevel@tonic-gate 		/* LINTED alignment */
66580Sstevel@tonic-gate 		attrtext = acltotext((aclent_t *)attrp, size);
66590Sstevel@tonic-gate 		if (attrtext == NULL) {
66600Sstevel@tonic-gate 			(void) fprintf(stderr, "acltotext failed\n");
66610Sstevel@tonic-gate 			return (-1);
66620Sstevel@tonic-gate 		}
66630Sstevel@tonic-gate 		/* header: type + size = 8 */
66640Sstevel@tonic-gate 		newattrsize = 8 + strlen(attrtext) + 1;
66650Sstevel@tonic-gate 		attr = e_zalloc(E_NORMAL, newattrsize);
66660Sstevel@tonic-gate 		if (attr == NULL) {
66670Sstevel@tonic-gate 			(void) fprintf(stderr, "can't allocate memory\n");
66680Sstevel@tonic-gate 			return (-1);
66690Sstevel@tonic-gate 		}
66700Sstevel@tonic-gate 		attr->attr_type = '1';		/* UFSD_ACL */
66710Sstevel@tonic-gate 		/* acl entry count */
66720Sstevel@tonic-gate 		(void) sprintf(attr->attr_len, "%06o", size);
66730Sstevel@tonic-gate 		(void) strcpy((char *)&attr->attr_info[0], attrtext);
66740Sstevel@tonic-gate 		free(attrtext);
66750Sstevel@tonic-gate 		break;
66760Sstevel@tonic-gate 
66770Sstevel@tonic-gate 		/* SunFed's case goes here */
66780Sstevel@tonic-gate 
66790Sstevel@tonic-gate 	default:
66800Sstevel@tonic-gate 		(void) fprintf(stderr, "unrecognized attribute type\n");
66810Sstevel@tonic-gate 		return (-1);
66820Sstevel@tonic-gate 	}
66830Sstevel@tonic-gate 
66840Sstevel@tonic-gate 	/* old security info + new attr header(8) + new attr */
66850Sstevel@tonic-gate 	oldsize = *secinfo_len;
66860Sstevel@tonic-gate 	*secinfo_len += newattrsize;
66870Sstevel@tonic-gate 	new_secinfo = e_zalloc(E_NORMAL, (uint_t)*secinfo_len);
66880Sstevel@tonic-gate 	if (new_secinfo == NULL) {
66890Sstevel@tonic-gate 		(void) fprintf(stderr, "can't allocate memory\n");
66900Sstevel@tonic-gate 		*secinfo_len -= newattrsize;
66910Sstevel@tonic-gate 		return (-1);
66920Sstevel@tonic-gate 	}
66930Sstevel@tonic-gate 
66940Sstevel@tonic-gate 	(void) memcpy(new_secinfo, *secinfo, oldsize);
66950Sstevel@tonic-gate 	(void) memcpy(new_secinfo + oldsize, attr, newattrsize);
66960Sstevel@tonic-gate 
66970Sstevel@tonic-gate 	free(*secinfo);
66980Sstevel@tonic-gate 	*secinfo = new_secinfo;
66990Sstevel@tonic-gate 	return (0);
67000Sstevel@tonic-gate }
67010Sstevel@tonic-gate 
67020Sstevel@tonic-gate static void
67030Sstevel@tonic-gate write_ancillary(char *secinfo, int len)
67040Sstevel@tonic-gate {
67050Sstevel@tonic-gate 	long    pad;
67060Sstevel@tonic-gate 	long    cnt;
67070Sstevel@tonic-gate 
67080Sstevel@tonic-gate 	/* Just tranditional permissions or no security attribute info */
67090Sstevel@tonic-gate 	if (len == 0) {
67100Sstevel@tonic-gate 		return;
67110Sstevel@tonic-gate 	}
67120Sstevel@tonic-gate 
67130Sstevel@tonic-gate 	/* write out security info */
67140Sstevel@tonic-gate 	while (len > 0) {
67150Sstevel@tonic-gate 		cnt = (unsigned)(len > CPIOBSZ) ? CPIOBSZ : len;
67160Sstevel@tonic-gate 		FLUSH(cnt);
67170Sstevel@tonic-gate 		errno = 0;
67180Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, secinfo, (unsigned)cnt);
67190Sstevel@tonic-gate 		Buffr.b_in_p += cnt;
67200Sstevel@tonic-gate 		Buffr.b_cnt += (long)cnt;
67210Sstevel@tonic-gate 		len -= (long)cnt;
67220Sstevel@tonic-gate 	}
67230Sstevel@tonic-gate 	pad = (Pad_val + 1 - (cnt & Pad_val)) & Pad_val;
67240Sstevel@tonic-gate 	if (pad != 0) {
67250Sstevel@tonic-gate 		FLUSH(pad);
67260Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, Empty, pad);
67270Sstevel@tonic-gate 		Buffr.b_in_p += pad;
67280Sstevel@tonic-gate 		Buffr.b_cnt += pad;
67290Sstevel@tonic-gate 	}
67300Sstevel@tonic-gate }
67310Sstevel@tonic-gate 
67320Sstevel@tonic-gate static int
67330Sstevel@tonic-gate remove_dir(char *path)
67340Sstevel@tonic-gate {
67350Sstevel@tonic-gate 	DIR		*name;
67360Sstevel@tonic-gate 	struct dirent	*direct;
67370Sstevel@tonic-gate 	struct stat	sbuf;
67380Sstevel@tonic-gate 	char		*path_copy;
67390Sstevel@tonic-gate 
67400Sstevel@tonic-gate #define	MSG1	"remove_dir() failed to stat(\"%s\") "
67410Sstevel@tonic-gate #define	MSG2	"remove_dir() failed to remove_dir(\"%s\") "
67420Sstevel@tonic-gate #define	MSG3	"remove_dir() failed to unlink(\"%s\") "
67430Sstevel@tonic-gate 
67440Sstevel@tonic-gate 	/*
67450Sstevel@tonic-gate 	 * Open the directory for reading.
67460Sstevel@tonic-gate 	 */
67470Sstevel@tonic-gate 	if ((name = opendir(path)) == NULL) {
67480Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to opendir(\"%s\") ", path);
67490Sstevel@tonic-gate 		return (-1);
67500Sstevel@tonic-gate 	}
67510Sstevel@tonic-gate 
67520Sstevel@tonic-gate 	if (chdir(path) == -1) {
67530Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to chdir(\"%s\") ", path);
67540Sstevel@tonic-gate 		return (-1);
67550Sstevel@tonic-gate 	}
67560Sstevel@tonic-gate 
67570Sstevel@tonic-gate 	/*
67580Sstevel@tonic-gate 	 * Read every directory entry.
67590Sstevel@tonic-gate 	 */
67600Sstevel@tonic-gate 	while ((direct = readdir(name)) != NULL) {
67610Sstevel@tonic-gate 		/*
67620Sstevel@tonic-gate 		 * Ignore "." and ".." entries.
67630Sstevel@tonic-gate 		 */
67640Sstevel@tonic-gate 		if (strcmp(direct->d_name, ".") == 0 ||
67650Sstevel@tonic-gate 		    strcmp(direct->d_name, "..") == 0)
67660Sstevel@tonic-gate 			continue;
67670Sstevel@tonic-gate 
67680Sstevel@tonic-gate 			if (lstat(direct->d_name, &sbuf) == -1) {
67690Sstevel@tonic-gate 				msg(ERRN, MSG1, direct->d_name);
67700Sstevel@tonic-gate 				(void) closedir(name);
67710Sstevel@tonic-gate 			return (-1);
67720Sstevel@tonic-gate 		}
67730Sstevel@tonic-gate 
67740Sstevel@tonic-gate 		if (S_ISDIR(sbuf.st_mode)) {
67750Sstevel@tonic-gate 			if (remove_dir(direct->d_name) == -1) {
67760Sstevel@tonic-gate 				msg(ERRN, MSG2, direct->d_name);
67770Sstevel@tonic-gate 				(void) closedir(name);
67780Sstevel@tonic-gate 				return (-1);
67790Sstevel@tonic-gate 			}
67800Sstevel@tonic-gate 		} else {
67810Sstevel@tonic-gate 			if (unlink(direct->d_name) == -1) {
67820Sstevel@tonic-gate 				msg(ERRN, MSG3, direct->d_name);
67830Sstevel@tonic-gate 				(void) closedir(name);
67840Sstevel@tonic-gate 				return (-1);
67850Sstevel@tonic-gate 			}
67860Sstevel@tonic-gate 		}
67870Sstevel@tonic-gate 
67880Sstevel@tonic-gate 	}
67890Sstevel@tonic-gate 
67900Sstevel@tonic-gate 	/*
67910Sstevel@tonic-gate 	 * Close the directory we just finished reading.
67920Sstevel@tonic-gate 	 */
67930Sstevel@tonic-gate 	(void) closedir(name);
67940Sstevel@tonic-gate 
67950Sstevel@tonic-gate 	/*
67960Sstevel@tonic-gate 	 * Change directory to the parent directory...
67970Sstevel@tonic-gate 	 */
67980Sstevel@tonic-gate 	if (chdir("..") == -1) {
67990Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to chdir(\"..\") ");
68000Sstevel@tonic-gate 		return (-1);
68010Sstevel@tonic-gate 	}
68020Sstevel@tonic-gate 
68030Sstevel@tonic-gate 	/*
68040Sstevel@tonic-gate 	 * ...and finally remove the directory; note we have to
68050Sstevel@tonic-gate 	 * make a copy since basename is free to modify its input.
68060Sstevel@tonic-gate 	 */
68070Sstevel@tonic-gate 	path_copy = e_strdup(E_NORMAL, path);
68080Sstevel@tonic-gate 	if (path_copy == NULL) {
68090Sstevel@tonic-gate 		msg(ERRN, "cannot strdup() the directory pathname ");
68100Sstevel@tonic-gate 		return (-1);
68110Sstevel@tonic-gate 	}
68120Sstevel@tonic-gate 
68130Sstevel@tonic-gate 	if (rmdir(basename(path_copy)) == -1) {
68140Sstevel@tonic-gate 		free(path_copy);
68150Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to rmdir(\"%s\") ", path);
68160Sstevel@tonic-gate 		return (-1);
68170Sstevel@tonic-gate 	}
68180Sstevel@tonic-gate 
68190Sstevel@tonic-gate 	free(path_copy);
68200Sstevel@tonic-gate 	return (0);
68210Sstevel@tonic-gate 
68220Sstevel@tonic-gate }
68230Sstevel@tonic-gate 
68240Sstevel@tonic-gate static int
68250Sstevel@tonic-gate save_cwd(void)
68260Sstevel@tonic-gate {
68270Sstevel@tonic-gate 	return (open(".", O_RDONLY));
68280Sstevel@tonic-gate }
68290Sstevel@tonic-gate 
68300Sstevel@tonic-gate static void
68310Sstevel@tonic-gate rest_cwd(int cwd)
68320Sstevel@tonic-gate {
68330Sstevel@tonic-gate 	(void) fchdir(cwd);
68340Sstevel@tonic-gate 	(void) close(cwd);
68350Sstevel@tonic-gate }
68360Sstevel@tonic-gate 
68370Sstevel@tonic-gate #if defined(O_XATTR)
68380Sstevel@tonic-gate static void
68390Sstevel@tonic-gate xattrs_out(int (*func)())
68400Sstevel@tonic-gate {
68410Sstevel@tonic-gate 	int dirpfd;
68420Sstevel@tonic-gate 	int filefd;
68430Sstevel@tonic-gate 	DIR *dirp;
68440Sstevel@tonic-gate 	struct dirent *dp;
68450Sstevel@tonic-gate 	int slen;
68460Sstevel@tonic-gate 	char *namep, *savenamep;
68470Sstevel@tonic-gate 
68480Sstevel@tonic-gate 	if (pathconf(G_p->g_nam_p, _PC_XATTR_EXISTS) != 1) {
68490Sstevel@tonic-gate 		return;
68500Sstevel@tonic-gate 	}
68510Sstevel@tonic-gate 
68520Sstevel@tonic-gate 	/*
68530Sstevel@tonic-gate 	 * If aclp still exists then free it since it is was set when base
68540Sstevel@tonic-gate 	 * file was extracted.
68550Sstevel@tonic-gate 	 */
68560Sstevel@tonic-gate 	if (aclp != (aclent_t *)NULL) {
68570Sstevel@tonic-gate 		free(aclp);
68580Sstevel@tonic-gate 		aclcnt = 0;
68590Sstevel@tonic-gate 		aclp = NULL;
68600Sstevel@tonic-gate 		acl_set = 0;
68610Sstevel@tonic-gate 	}
68620Sstevel@tonic-gate 
68630Sstevel@tonic-gate 	Gen.g_dirfd = attropen(G_p->g_nam_p, ".", O_RDONLY);
68640Sstevel@tonic-gate 	if (Gen.g_dirfd == -1) {
68650Sstevel@tonic-gate 		msg(ERRN, "Cannot open attribute directory of file \"%s\"",
68660Sstevel@tonic-gate 		    G_p->g_nam_p);
68670Sstevel@tonic-gate 		return;
68680Sstevel@tonic-gate 
68690Sstevel@tonic-gate 	}
68700Sstevel@tonic-gate 	savenamep = G_p->g_nam_p;
68710Sstevel@tonic-gate 
68720Sstevel@tonic-gate 	if ((dirpfd = dup(Gen.g_dirfd)) == -1)  {
68730Sstevel@tonic-gate 		msg(ERRN, "Cannot dup(2) attribute directory descriptor");
68740Sstevel@tonic-gate 		return;
68750Sstevel@tonic-gate 	}
68760Sstevel@tonic-gate 
68770Sstevel@tonic-gate 	if ((dirp = fdopendir(dirpfd)) == (DIR *)NULL) {
68780Sstevel@tonic-gate 		msg(ERRN, "Cannot fdopendir(2) directory file descriptor");
68790Sstevel@tonic-gate 		return;
68800Sstevel@tonic-gate 	}
68810Sstevel@tonic-gate 
68820Sstevel@tonic-gate 	while ((dp = readdir(dirp)) != (struct dirent *)NULL) {
68830Sstevel@tonic-gate 		if (strcmp(dp->d_name, "..") == 0) {
68840Sstevel@tonic-gate 			continue;
68850Sstevel@tonic-gate 		}
68860Sstevel@tonic-gate 
68870Sstevel@tonic-gate 		if (strcmp(dp->d_name, ".") == 0) {
68880Sstevel@tonic-gate 			Hiddendir = 1;
68890Sstevel@tonic-gate 		} else {
68900Sstevel@tonic-gate 			Hiddendir = 0;
68910Sstevel@tonic-gate 		}
68920Sstevel@tonic-gate 
68930Sstevel@tonic-gate 		Gen.g_attrnam_p = dp->d_name;
68940Sstevel@tonic-gate 
68950Sstevel@tonic-gate 		if (STAT(Gen.g_dirfd, Gen.g_nam_p, &SrcSt) == -1) {
68960Sstevel@tonic-gate 			msg(ERRN,
68970Sstevel@tonic-gate 			    "Could not fstatat(2) attribute \"%s\" of"
68980Sstevel@tonic-gate 			    " file \"%s\"", dp->d_name, savenamep);
68990Sstevel@tonic-gate 			continue;
69000Sstevel@tonic-gate 		}
69010Sstevel@tonic-gate 
69020Sstevel@tonic-gate 		if (Use_old_stat) {
69030Sstevel@tonic-gate 			OldSt = convert_to_old_stat(&SrcSt,
69040Sstevel@tonic-gate 			    Gen.g_nam_p, Gen.g_attrnam_p);
69050Sstevel@tonic-gate 
69060Sstevel@tonic-gate 			if (OldSt == NULL) {
69070Sstevel@tonic-gate 				msg(ERRN,
69080Sstevel@tonic-gate 				    "Could not convert to old stat format");
69090Sstevel@tonic-gate 				continue;
69100Sstevel@tonic-gate 			}
69110Sstevel@tonic-gate 		}
69120Sstevel@tonic-gate 
69130Sstevel@tonic-gate 		Gen.g_attrfnam_p = savenamep;
69140Sstevel@tonic-gate 
69150Sstevel@tonic-gate 		/*
69160Sstevel@tonic-gate 		 * Set up dummy header name
69170Sstevel@tonic-gate 		 *
69180Sstevel@tonic-gate 		 * One piece is written with .hdr, which
69190Sstevel@tonic-gate 		 * contains the actual xattr hdr or pathing information
69200Sstevel@tonic-gate 		 * then the name is updated to drop the .hdr off
69210Sstevel@tonic-gate 		 * and the actual file itself is archived.
69220Sstevel@tonic-gate 		 */
69230Sstevel@tonic-gate 		slen = strlen(Gen.g_attrnam_p) + strlen(DEVNULL) +
69240Sstevel@tonic-gate 		    strlen(XATTRHDR) + 1;
69250Sstevel@tonic-gate 		if ((namep = e_zalloc(E_NORMAL, slen)) == (char *)NULL) {
69260Sstevel@tonic-gate 			msg(ERRN, "Could not calloc memory for attribute name");
69270Sstevel@tonic-gate 			continue;
69280Sstevel@tonic-gate 		}
69290Sstevel@tonic-gate 		(void) sprintf(namep, "%s/%s%s",
69300Sstevel@tonic-gate 		    DEVNULL, Gen.g_attrnam_p, XATTRHDR);
69310Sstevel@tonic-gate 		Gen.g_nam_p = namep;
69320Sstevel@tonic-gate 
69330Sstevel@tonic-gate 		/*
69340Sstevel@tonic-gate 		 * Get attribute's ACL info: don't bother allocating space
69350Sstevel@tonic-gate 		 * if there are only standard permissions, i.e. ACL count < 4
69360Sstevel@tonic-gate 		 */
69370Sstevel@tonic-gate 		if (Pflag) {
69380Sstevel@tonic-gate 			filefd = openat(Gen.g_dirfd, dp->d_name, O_RDONLY);
69390Sstevel@tonic-gate 			if (filefd == -1) {
69400Sstevel@tonic-gate 				msg(ERRN,
69410Sstevel@tonic-gate 				    "Could not open attribute \"%s\" of"
69420Sstevel@tonic-gate 				    " file \"%s\"", dp->d_name, savenamep);
69430Sstevel@tonic-gate 				free(namep);
69440Sstevel@tonic-gate 				continue;
69450Sstevel@tonic-gate 			}
69460Sstevel@tonic-gate 			if ((aclcnt = facl(filefd, GETACLCNT,
69470Sstevel@tonic-gate 			    0, NULL)) < 0) {
69480Sstevel@tonic-gate 				msg(ERRN,
69490Sstevel@tonic-gate 				    "Error with acl() on %s",
69500Sstevel@tonic-gate 				    Gen.g_nam_p);
69510Sstevel@tonic-gate 			}
69520Sstevel@tonic-gate 			if (aclcnt > MIN_ACL_ENTRIES) {
69530Sstevel@tonic-gate 				aclp = e_zalloc(E_EXIT,
69540Sstevel@tonic-gate 					sizeof (aclent_t) * aclcnt);
69550Sstevel@tonic-gate 
69560Sstevel@tonic-gate 				if (facl(filefd, GETACL, aclcnt, aclp) < 0) {
69570Sstevel@tonic-gate 					msg(ERRN,
69580Sstevel@tonic-gate 					    "Error with getacl() on %s",
69590Sstevel@tonic-gate 					    Gen.g_nam_p);
69600Sstevel@tonic-gate 					free(aclp);
69610Sstevel@tonic-gate 					aclp = NULL;
69620Sstevel@tonic-gate 				}
69630Sstevel@tonic-gate 			}
69640Sstevel@tonic-gate 			(void) close(filefd);
69650Sstevel@tonic-gate 		}
69660Sstevel@tonic-gate 		(void) creat_hdr();
69670Sstevel@tonic-gate 		(void) (*func)();
69680Sstevel@tonic-gate 		if (Gen.g_passdirfd != -1) {
69690Sstevel@tonic-gate 			(void) close(Gen.g_passdirfd);
69700Sstevel@tonic-gate 			Gen.g_passdirfd = -1;
69710Sstevel@tonic-gate 		}
69720Sstevel@tonic-gate 		Gen.g_attrnam_p = (char *)NULL;
69730Sstevel@tonic-gate 		Gen.g_attrfnam_p = (char *)NULL;
69740Sstevel@tonic-gate 		Gen.g_linktoattrfnam_p = (char *)NULL;
69750Sstevel@tonic-gate 		Gen.g_linktoattrnam_p = (char *)NULL;
69760Sstevel@tonic-gate 		if (aclp != (aclent_t *)NULL) {
69770Sstevel@tonic-gate 			free(aclp);
69780Sstevel@tonic-gate 			aclcnt = 0;
69790Sstevel@tonic-gate 			aclp = NULL;
69800Sstevel@tonic-gate 			acl_set = 0;
69810Sstevel@tonic-gate 		}
69820Sstevel@tonic-gate 		free(namep);
69830Sstevel@tonic-gate 	}
69840Sstevel@tonic-gate 
69850Sstevel@tonic-gate 	(void) closedir(dirp);
69860Sstevel@tonic-gate 	(void) close(Gen.g_dirfd);
69870Sstevel@tonic-gate 	Gen.g_dirfd = -1;
69880Sstevel@tonic-gate }
69890Sstevel@tonic-gate #else
69900Sstevel@tonic-gate static void
69910Sstevel@tonic-gate xattrs_out(int (*func)())
69920Sstevel@tonic-gate {
69930Sstevel@tonic-gate }
69940Sstevel@tonic-gate #endif
69950Sstevel@tonic-gate 
69960Sstevel@tonic-gate /*
69970Sstevel@tonic-gate  * Return the parent directory of a given path.
69980Sstevel@tonic-gate  *
69990Sstevel@tonic-gate  * Examples:
70000Sstevel@tonic-gate  * /usr/tmp return /usr
70010Sstevel@tonic-gate  * /usr/tmp/file return /usr/tmp
70020Sstevel@tonic-gate  * /  returns .
70030Sstevel@tonic-gate  * /usr returns /
70040Sstevel@tonic-gate  * file returns .
70050Sstevel@tonic-gate  *
70060Sstevel@tonic-gate  * dir is assumed to be at least as big as path.
70070Sstevel@tonic-gate  */
70080Sstevel@tonic-gate static void
70090Sstevel@tonic-gate get_parent(char *path, char *dir)
70100Sstevel@tonic-gate {
70110Sstevel@tonic-gate 	char *s;
70120Sstevel@tonic-gate 	char tmpdir[PATH_MAX + 1];
70130Sstevel@tonic-gate 
70140Sstevel@tonic-gate 	if (strlen(path) > PATH_MAX) {
70150Sstevel@tonic-gate 		msg(EXT, "pathname is too long");
70160Sstevel@tonic-gate 	}
70170Sstevel@tonic-gate 	(void) strcpy(tmpdir, path);
70180Sstevel@tonic-gate 	chop_endslashes(tmpdir);
70190Sstevel@tonic-gate 
70200Sstevel@tonic-gate 	if ((s = strrchr(tmpdir, '/')) == NULL) {
70210Sstevel@tonic-gate 		(void) strcpy(dir, ".");
70220Sstevel@tonic-gate 	} else {
70230Sstevel@tonic-gate 		s = skipslashes(s, tmpdir);
70240Sstevel@tonic-gate 		*s = '\0';
70250Sstevel@tonic-gate 		if (s == tmpdir)
70260Sstevel@tonic-gate 			(void) strcpy(dir, "/");
70270Sstevel@tonic-gate 		else
70280Sstevel@tonic-gate 			(void) strcpy(dir, tmpdir);
70290Sstevel@tonic-gate 	}
70300Sstevel@tonic-gate }
70310Sstevel@tonic-gate 
70320Sstevel@tonic-gate #if defined(O_XATTR)
70330Sstevel@tonic-gate #define	ROUNDTOTBLOCK(a)		((a + (TBLOCK -1)) & ~(TBLOCK -1))
70340Sstevel@tonic-gate 
70350Sstevel@tonic-gate static void
70360Sstevel@tonic-gate prepare_xattr_hdr(
70370Sstevel@tonic-gate 	char		**attrbuf,
70380Sstevel@tonic-gate 	char		*filename,
70390Sstevel@tonic-gate 	char		*attrname,
70400Sstevel@tonic-gate 	char		typeflag,
70410Sstevel@tonic-gate 	struct Lnk	*linkinfo,
70420Sstevel@tonic-gate 	int		*rlen)
70430Sstevel@tonic-gate {
70440Sstevel@tonic-gate 	char			*bufhead;	/* ptr to full buffer */
70450Sstevel@tonic-gate 	struct xattr_hdr 	*hptr;		/* ptr to header in bufhead */
70460Sstevel@tonic-gate 	struct xattr_buf	*tptr;		/* ptr to pathing pieces */
70470Sstevel@tonic-gate 	int			totalen;	/* total buffer length */
70480Sstevel@tonic-gate 	int			len;		/* length returned to user */
70490Sstevel@tonic-gate 	int			stringlen;	/* length of filename + attr */
70500Sstevel@tonic-gate 	int			linkstringlen;	/* ditto in link section */
70510Sstevel@tonic-gate 	int			complen;	/* length of pathing section */
70520Sstevel@tonic-gate 	int			linklen;	/* length of link section */
70530Sstevel@tonic-gate 
70540Sstevel@tonic-gate 
70550Sstevel@tonic-gate 	/*
70560Sstevel@tonic-gate 	 * Release previous buffer if any.
70570Sstevel@tonic-gate 	 */
70580Sstevel@tonic-gate 
70590Sstevel@tonic-gate 	if (*attrbuf != (char *)NULL) {
70600Sstevel@tonic-gate 		free(*attrbuf);
70610Sstevel@tonic-gate 		*attrbuf = NULL;
70620Sstevel@tonic-gate 	}
70630Sstevel@tonic-gate 
70640Sstevel@tonic-gate 	/*
70650Sstevel@tonic-gate 	 * First add in fixed size stuff
70660Sstevel@tonic-gate 	 */
70670Sstevel@tonic-gate 	len = sizeof (struct xattr_hdr) + sizeof (struct xattr_buf);
70680Sstevel@tonic-gate 
70690Sstevel@tonic-gate 	/*
70700Sstevel@tonic-gate 	 * Add space for two nulls
70710Sstevel@tonic-gate 	 */
70720Sstevel@tonic-gate 	stringlen = strlen(attrname) + strlen(filename) + 2;
70730Sstevel@tonic-gate 	complen = stringlen + sizeof (struct xattr_buf);
70740Sstevel@tonic-gate 
70750Sstevel@tonic-gate 	len += stringlen;
70760Sstevel@tonic-gate 
70770Sstevel@tonic-gate 	/*
70780Sstevel@tonic-gate 	 * Now add on space for link info if any
70790Sstevel@tonic-gate 	 */
70800Sstevel@tonic-gate 
70810Sstevel@tonic-gate 	if (linkinfo != NULL) {
70820Sstevel@tonic-gate 		/*
70830Sstevel@tonic-gate 		 * Again add space for two nulls
70840Sstevel@tonic-gate 		 */
70850Sstevel@tonic-gate 		linkstringlen = strlen(linkinfo->L_gen.g_attrfnam_p) +
70860Sstevel@tonic-gate 		    strlen(linkinfo->L_gen.g_attrnam_p) + 2;
70870Sstevel@tonic-gate 		len += linkstringlen;
70880Sstevel@tonic-gate 	}
70890Sstevel@tonic-gate 
70900Sstevel@tonic-gate 	/*
70910Sstevel@tonic-gate 	 * Now add padding to end to fill out TBLOCK
70920Sstevel@tonic-gate 	 *
70930Sstevel@tonic-gate 	 * Function returns size of real data and not size + padding.
70940Sstevel@tonic-gate 	 */
70950Sstevel@tonic-gate 
70960Sstevel@tonic-gate 	totalen = ROUNDTOTBLOCK(len);
70970Sstevel@tonic-gate 	bufhead = e_zalloc(E_EXIT, totalen);
70980Sstevel@tonic-gate 
70990Sstevel@tonic-gate 	/*
71000Sstevel@tonic-gate 	 * Now we can fill in the necessary pieces
71010Sstevel@tonic-gate 	 */
71020Sstevel@tonic-gate 
71030Sstevel@tonic-gate 	if (linkinfo != (struct Lnk *)NULL) {
71040Sstevel@tonic-gate 		linklen = linkstringlen + (sizeof (struct xattr_buf));
71050Sstevel@tonic-gate 	} else {
71060Sstevel@tonic-gate 		linklen = 0;
71070Sstevel@tonic-gate 	}
71080Sstevel@tonic-gate 
71090Sstevel@tonic-gate 	/*
71100Sstevel@tonic-gate 	 * first fill in the fixed header
71110Sstevel@tonic-gate 	 */
71120Sstevel@tonic-gate 	hptr = (struct xattr_hdr *)bufhead;
71130Sstevel@tonic-gate 	(void) sprintf(hptr->h_version, "%s", XATTR_ARCH_VERS);
71140Sstevel@tonic-gate 	(void) sprintf(hptr->h_component_len, "%0*d",
71150Sstevel@tonic-gate 	    sizeof (hptr->h_component_len) - 1, complen);
71160Sstevel@tonic-gate 	(void) sprintf(hptr->h_link_component_len, "%0*d",
71170Sstevel@tonic-gate 	    sizeof (hptr->h_link_component_len) - 1, linklen);
71180Sstevel@tonic-gate 	(void) sprintf(hptr->h_size, "%0*d", sizeof (hptr->h_size) - 1, len);
71190Sstevel@tonic-gate 
71200Sstevel@tonic-gate 	/*
71210Sstevel@tonic-gate 	 * Now fill in the filename + attrnames section
71220Sstevel@tonic-gate 	 */
71230Sstevel@tonic-gate 
71240Sstevel@tonic-gate 	tptr = (struct xattr_buf *)(bufhead + sizeof (struct xattr_hdr));
71250Sstevel@tonic-gate 	(void) sprintf(tptr->h_namesz, "%0*d", sizeof (tptr->h_namesz) - 1,
71260Sstevel@tonic-gate 	    stringlen);
71270Sstevel@tonic-gate 	(void) strcpy(tptr->h_names, filename);
71280Sstevel@tonic-gate 	(void) strcpy(&tptr->h_names[strlen(filename) + 1], attrname);
71290Sstevel@tonic-gate 	tptr->h_typeflag = typeflag;
71300Sstevel@tonic-gate 
71310Sstevel@tonic-gate 	/*
71320Sstevel@tonic-gate 	 * Now fill in the optional link section if we have one
71330Sstevel@tonic-gate 	 */
71340Sstevel@tonic-gate 
71350Sstevel@tonic-gate 	if (linkinfo != (struct Lnk *)NULL) {
71360Sstevel@tonic-gate 		tptr = (struct xattr_buf *)(bufhead +
71370Sstevel@tonic-gate 		    sizeof (struct xattr_hdr) + complen);
71380Sstevel@tonic-gate 
71390Sstevel@tonic-gate 		(void) sprintf(tptr->h_namesz, "%0*d",
71400Sstevel@tonic-gate 		    sizeof (tptr->h_namesz) - 1, linkstringlen);
71410Sstevel@tonic-gate 		(void) strcpy(tptr->h_names, linkinfo->L_gen.g_attrfnam_p);
71420Sstevel@tonic-gate 		(void) strcpy(
71430Sstevel@tonic-gate 		    &tptr->h_names[strlen(linkinfo->L_gen.g_attrfnam_p) + 1],
71440Sstevel@tonic-gate 		    linkinfo->L_gen.g_attrnam_p);
71450Sstevel@tonic-gate 		tptr->h_typeflag = typeflag;
71460Sstevel@tonic-gate 	}
71470Sstevel@tonic-gate 	*attrbuf = (char *)bufhead;
71480Sstevel@tonic-gate 	*rlen = len;
71490Sstevel@tonic-gate }
71500Sstevel@tonic-gate #endif /* O_XATTR */
71510Sstevel@tonic-gate 
71520Sstevel@tonic-gate static char
71530Sstevel@tonic-gate tartype(int type)
71540Sstevel@tonic-gate {
71550Sstevel@tonic-gate 	switch (type) {
71560Sstevel@tonic-gate 
71570Sstevel@tonic-gate 	case S_IFDIR:
71580Sstevel@tonic-gate 		return (DIRTYPE);
71590Sstevel@tonic-gate 
71600Sstevel@tonic-gate 	case S_IFLNK:
71610Sstevel@tonic-gate 		return (SYMTYPE);
71620Sstevel@tonic-gate 
71630Sstevel@tonic-gate 	case S_IFIFO:
71640Sstevel@tonic-gate 		return (FIFOTYPE);
71650Sstevel@tonic-gate 
71660Sstevel@tonic-gate 	case S_IFCHR:
71670Sstevel@tonic-gate 		return (CHRTYPE);
71680Sstevel@tonic-gate 
71690Sstevel@tonic-gate 	case S_IFBLK:
71700Sstevel@tonic-gate 		return (BLKTYPE);
71710Sstevel@tonic-gate 
71720Sstevel@tonic-gate 	case S_IFREG:
71730Sstevel@tonic-gate 		return (REGTYPE);
71740Sstevel@tonic-gate 
71750Sstevel@tonic-gate 	default:
71760Sstevel@tonic-gate 		return ('\0');
71770Sstevel@tonic-gate 	}
71780Sstevel@tonic-gate }
71790Sstevel@tonic-gate 
71800Sstevel@tonic-gate #if defined(O_XATTR)
71810Sstevel@tonic-gate static int
71820Sstevel@tonic-gate openfile(int omode)
71830Sstevel@tonic-gate {
71840Sstevel@tonic-gate 
71850Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
71860Sstevel@tonic-gate 		return (attropen(G_p->g_attrfnam_p, G_p->g_attrnam_p, omode));
71870Sstevel@tonic-gate 	} else {
71880Sstevel@tonic-gate 		return (openat(G_p->g_dirfd,
71890Sstevel@tonic-gate 		    get_component(G_p->g_nam_p), omode));
71900Sstevel@tonic-gate 	}
71910Sstevel@tonic-gate }
71920Sstevel@tonic-gate #else
71930Sstevel@tonic-gate static int
71940Sstevel@tonic-gate openfile(int omode)
71950Sstevel@tonic-gate {
71960Sstevel@tonic-gate 	return (openat(G_p->g_dirfd, get_component(G_p->g_nam_p), omode));
71970Sstevel@tonic-gate }
71980Sstevel@tonic-gate #endif
71990Sstevel@tonic-gate 
72000Sstevel@tonic-gate #if defined(O_XATTR)
72010Sstevel@tonic-gate static int
72020Sstevel@tonic-gate read_xattr_hdr()
72030Sstevel@tonic-gate {
72040Sstevel@tonic-gate 	off_t		bytes;
72050Sstevel@tonic-gate 	int		comp_len, link_len;
72060Sstevel@tonic-gate 	int		namelen;
72070Sstevel@tonic-gate 	int		cnt;
72080Sstevel@tonic-gate 	char		*tp;
72090Sstevel@tonic-gate 	int		pad;
72100Sstevel@tonic-gate 
72110Sstevel@tonic-gate 	/*
72120Sstevel@tonic-gate 	 * Include any padding in the read.  We need to be positioned
72130Sstevel@tonic-gate 	 * at beginning of next header.
72140Sstevel@tonic-gate 	 */
72150Sstevel@tonic-gate 
72160Sstevel@tonic-gate 	bytes = Gen.g_filesz;
72170Sstevel@tonic-gate 
72180Sstevel@tonic-gate 	if ((xattrhead = e_zalloc(E_NORMAL, (size_t)bytes)) == NULL) {
72190Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
72200Sstevel@tonic-gate 		    "Insufficient memory for extended attribute\n"));
72210Sstevel@tonic-gate 		return (1);
72220Sstevel@tonic-gate 	}
72230Sstevel@tonic-gate 
72240Sstevel@tonic-gate 	tp = (char *)xattrhead;
72250Sstevel@tonic-gate 	while (bytes > 0) {
72260Sstevel@tonic-gate 		cnt = (int)(bytes > CPIOBSZ) ? CPIOBSZ : bytes;
72270Sstevel@tonic-gate 		FILL(cnt);
72280Sstevel@tonic-gate 		(void) memcpy(tp, Buffr.b_out_p, cnt);
72290Sstevel@tonic-gate 		tp += cnt;
72300Sstevel@tonic-gate 		Buffr.b_out_p += cnt;
72310Sstevel@tonic-gate 		Buffr.b_cnt -= (off_t)cnt;
72320Sstevel@tonic-gate 		bytes -= (off_t)cnt;
72330Sstevel@tonic-gate 	}
72340Sstevel@tonic-gate 
72350Sstevel@tonic-gate 	pad = (Pad_val + 1 - (Gen.g_filesz & Pad_val)) &
72360Sstevel@tonic-gate 	    Pad_val;
72370Sstevel@tonic-gate 	if (pad != 0) {
72380Sstevel@tonic-gate 		FILL(pad);
72390Sstevel@tonic-gate 		Buffr.b_out_p += pad;
72400Sstevel@tonic-gate 		Buffr.b_cnt -= (off_t)pad;
72410Sstevel@tonic-gate 	}
72420Sstevel@tonic-gate 
72430Sstevel@tonic-gate 	/*
72440Sstevel@tonic-gate 	 * Validate that we can handle header format
72450Sstevel@tonic-gate 	 */
72460Sstevel@tonic-gate 
72470Sstevel@tonic-gate 	if (strcmp(xattrhead->h_version, XATTR_ARCH_VERS) != 0) {
72480Sstevel@tonic-gate 		(void) fprintf(stderr,
72490Sstevel@tonic-gate 		    gettext("Unknown extended attribute format encountered\n"));
72500Sstevel@tonic-gate 		(void) fprintf(stderr,
72510Sstevel@tonic-gate 		    gettext("Disabling extended attribute header parsing\n"));
72520Sstevel@tonic-gate 		xattrbadhead = 1;
72530Sstevel@tonic-gate 		return (1);
72540Sstevel@tonic-gate 	}
72550Sstevel@tonic-gate 	(void) sscanf(xattrhead->h_component_len, "%10d", &comp_len);
72560Sstevel@tonic-gate 	(void) sscanf(xattrhead->h_link_component_len, "%10d", &link_len);
72570Sstevel@tonic-gate 	xattrp = (struct xattr_buf *)(((char *)xattrhead) +
72580Sstevel@tonic-gate 	    sizeof (struct xattr_hdr));
72590Sstevel@tonic-gate 	(void) sscanf(xattrp->h_namesz, "%7d", &namelen);
72600Sstevel@tonic-gate 	if (link_len > 0) {
72610Sstevel@tonic-gate 		xattr_linkp = (struct xattr_buf *)((int)xattrp + (int)comp_len);
72620Sstevel@tonic-gate 	} else {
72630Sstevel@tonic-gate 		xattr_linkp = NULL;
72640Sstevel@tonic-gate 	}
72650Sstevel@tonic-gate 
72660Sstevel@tonic-gate 	return (0);
72670Sstevel@tonic-gate }
72680Sstevel@tonic-gate #endif
72690Sstevel@tonic-gate 
72700Sstevel@tonic-gate static mode_t
72710Sstevel@tonic-gate attrmode(char type)
72720Sstevel@tonic-gate {
72730Sstevel@tonic-gate 	mode_t mode;
72740Sstevel@tonic-gate 
72750Sstevel@tonic-gate 	switch (type) {
72760Sstevel@tonic-gate 	case '\0':
72770Sstevel@tonic-gate 	case REGTYPE:
72780Sstevel@tonic-gate 	case LNKTYPE:
72790Sstevel@tonic-gate 		mode = S_IFREG;
72800Sstevel@tonic-gate 		break;
72810Sstevel@tonic-gate 
72820Sstevel@tonic-gate 	case SYMTYPE:
72830Sstevel@tonic-gate 		mode = S_IFLNK;
72840Sstevel@tonic-gate 		break;
72850Sstevel@tonic-gate 
72860Sstevel@tonic-gate 	case CHRTYPE:
72870Sstevel@tonic-gate 		mode = S_IFCHR;
72880Sstevel@tonic-gate 		break;
72890Sstevel@tonic-gate 	case BLKTYPE:
72900Sstevel@tonic-gate 		mode = S_IFBLK;
72910Sstevel@tonic-gate 		break;
72920Sstevel@tonic-gate 	case DIRTYPE:
72930Sstevel@tonic-gate 		mode = S_IFDIR;
72940Sstevel@tonic-gate 		break;
72950Sstevel@tonic-gate 	case FIFOTYPE:
72960Sstevel@tonic-gate 		mode = S_IFIFO;
72970Sstevel@tonic-gate 		break;
72980Sstevel@tonic-gate 	case CONTTYPE:
72990Sstevel@tonic-gate 	default:
73000Sstevel@tonic-gate 		mode = 0;
73010Sstevel@tonic-gate 	}
73020Sstevel@tonic-gate 
73030Sstevel@tonic-gate 	return (mode);
73040Sstevel@tonic-gate }
73050Sstevel@tonic-gate 
73060Sstevel@tonic-gate #if defined(O_XATTR)
73070Sstevel@tonic-gate static char *
73080Sstevel@tonic-gate get_component(char *path)
73090Sstevel@tonic-gate {
73100Sstevel@tonic-gate 	char *ptr;
73110Sstevel@tonic-gate 
73120Sstevel@tonic-gate 	ptr = strrchr(path, '/');
73130Sstevel@tonic-gate 	if (ptr == NULL) {
73140Sstevel@tonic-gate 		return (path);
73150Sstevel@tonic-gate 	} else {
73160Sstevel@tonic-gate 		/*
73170Sstevel@tonic-gate 		 * Handle trailing slash
73180Sstevel@tonic-gate 		 */
73190Sstevel@tonic-gate 		if (*(ptr + 1) == '\0')
73200Sstevel@tonic-gate 			return (ptr);
73210Sstevel@tonic-gate 		else
73220Sstevel@tonic-gate 			return (ptr + 1);
73230Sstevel@tonic-gate 	}
73240Sstevel@tonic-gate }
73250Sstevel@tonic-gate #else
73260Sstevel@tonic-gate static char *
73270Sstevel@tonic-gate get_component(char *path)
73280Sstevel@tonic-gate {
73290Sstevel@tonic-gate 	return (path);
73300Sstevel@tonic-gate }
73310Sstevel@tonic-gate #endif
73320Sstevel@tonic-gate 
73330Sstevel@tonic-gate static int
73340Sstevel@tonic-gate open_dir(char *name)
73350Sstevel@tonic-gate {
73360Sstevel@tonic-gate 	int fd = -1;
73370Sstevel@tonic-gate 	int cnt = 0;
73380Sstevel@tonic-gate 	char *dir;
73390Sstevel@tonic-gate 
73400Sstevel@tonic-gate 	dir = e_zalloc(E_EXIT, strlen(name) + 1);
73410Sstevel@tonic-gate 
73420Sstevel@tonic-gate 	/*
73430Sstevel@tonic-gate 	 * open directory; creating missing directories along the way.
73440Sstevel@tonic-gate 	 */
73450Sstevel@tonic-gate 	get_parent(name, dir);
73460Sstevel@tonic-gate 	do {
73470Sstevel@tonic-gate 		fd = open(dir, O_RDONLY);
73480Sstevel@tonic-gate 		if (fd != -1) {
73490Sstevel@tonic-gate 			free(dir);
73500Sstevel@tonic-gate 			return (fd);
73510Sstevel@tonic-gate 		}
73520Sstevel@tonic-gate 		cnt++;
73530Sstevel@tonic-gate 	} while (cnt <= 1 && missdir(name) == 0);
73540Sstevel@tonic-gate 
73550Sstevel@tonic-gate 	free(dir);
73560Sstevel@tonic-gate 	return (-1);
73570Sstevel@tonic-gate }
73580Sstevel@tonic-gate 
73590Sstevel@tonic-gate static int
73600Sstevel@tonic-gate open_dirfd()
73610Sstevel@tonic-gate {
73620Sstevel@tonic-gate #ifdef O_XATTR
73630Sstevel@tonic-gate 	if ((Args & OCt) == 0) {
73640Sstevel@tonic-gate 		close_dirfd();
73650Sstevel@tonic-gate 		if (G_p->g_attrnam_p != (char *)NULL) {
73660Sstevel@tonic-gate 			G_p->g_dirfd = attropen(G_p->g_attrfnam_p,
73670Sstevel@tonic-gate 			    ".", O_RDONLY);
73680Sstevel@tonic-gate 			if (G_p->g_dirfd == -1 && (Args & (OCi | OCp))) {
73690Sstevel@tonic-gate 				G_p->g_dirfd =
73700Sstevel@tonic-gate 				    retry_attrdir_open(G_p->g_attrfnam_p);
73710Sstevel@tonic-gate 				if (G_p->g_dirfd == -1) {
73720Sstevel@tonic-gate 					msg(ERRN,
73730Sstevel@tonic-gate 					    "Cannot open attribute"
73740Sstevel@tonic-gate 					    " directory of file %s",
73750Sstevel@tonic-gate 					    G_p->g_attrfnam_p);
73760Sstevel@tonic-gate 					return (1);
73770Sstevel@tonic-gate 				}
73780Sstevel@tonic-gate 			}
73790Sstevel@tonic-gate 		} else {
73800Sstevel@tonic-gate 			G_p->g_dirfd = open_dir(G_p->g_nam_p);
73810Sstevel@tonic-gate 			if (G_p->g_dirfd == -1) {
73820Sstevel@tonic-gate 				msg(ERRN,
73830Sstevel@tonic-gate 				    "Cannot open/create %s", G_p->g_nam_p);
73840Sstevel@tonic-gate 				return (1);
73850Sstevel@tonic-gate 			}
73860Sstevel@tonic-gate 		}
73870Sstevel@tonic-gate 	} else {
73880Sstevel@tonic-gate 		G_p->g_dirfd = -1;
73890Sstevel@tonic-gate 	}
73900Sstevel@tonic-gate #else
73910Sstevel@tonic-gate 	G_p->g_dirfd = -1;
73920Sstevel@tonic-gate #endif
73930Sstevel@tonic-gate 	return (0);
73940Sstevel@tonic-gate }
73950Sstevel@tonic-gate 
73960Sstevel@tonic-gate static void
73970Sstevel@tonic-gate close_dirfd()
73980Sstevel@tonic-gate {
73990Sstevel@tonic-gate 	if (G_p->g_dirfd != -1) {
74000Sstevel@tonic-gate 		(void) close(G_p->g_dirfd);
74010Sstevel@tonic-gate 		G_p->g_dirfd = -1;
74020Sstevel@tonic-gate 	}
74030Sstevel@tonic-gate }
74040Sstevel@tonic-gate 
74050Sstevel@tonic-gate static void
74060Sstevel@tonic-gate write_xattr_hdr()
74070Sstevel@tonic-gate {
74080Sstevel@tonic-gate 	char *attrbuf = NULL;
74090Sstevel@tonic-gate 	int  attrlen = 0;
74100Sstevel@tonic-gate 	char *namep;
74110Sstevel@tonic-gate 	struct Lnk *tl_p, *linkinfo;
74120Sstevel@tonic-gate 
74130Sstevel@tonic-gate 
74140Sstevel@tonic-gate 	/*
74150Sstevel@tonic-gate 	 * namep was allocated in xattrs_out.  It is big enough to hold
74160Sstevel@tonic-gate 	 * either the name + .hdr on the end or just the attr name
74170Sstevel@tonic-gate 	 */
74180Sstevel@tonic-gate 
74190Sstevel@tonic-gate #if defined(O_XATTR)
74200Sstevel@tonic-gate 	namep = Gen.g_nam_p;
74210Sstevel@tonic-gate 	(void) creat_hdr();
74220Sstevel@tonic-gate 
74230Sstevel@tonic-gate 
74240Sstevel@tonic-gate 	if (Args & OCo) {
74250Sstevel@tonic-gate 		linkinfo = NULL;
74260Sstevel@tonic-gate 		tl_p = Lnk_hd.L_nxt_p;
74270Sstevel@tonic-gate 		while (tl_p != &Lnk_hd) {
74280Sstevel@tonic-gate 			if (tl_p->L_gen.g_ino == G_p->g_ino &&
74290Sstevel@tonic-gate 				tl_p->L_gen.g_dev == G_p->g_dev) {
74300Sstevel@tonic-gate 					linkinfo = tl_p;
74310Sstevel@tonic-gate 					break; /* found */
74320Sstevel@tonic-gate 			}
74330Sstevel@tonic-gate 			tl_p = tl_p->L_nxt_p;
74340Sstevel@tonic-gate 		}
74350Sstevel@tonic-gate 		prepare_xattr_hdr(&attrbuf, Gen.g_attrfnam_p,
74360Sstevel@tonic-gate 		    Gen.g_attrnam_p,
74370Sstevel@tonic-gate 		    (linkinfo == (struct Lnk *)NULL) ?
74380Sstevel@tonic-gate 		    tartype(Gen.g_mode & Ftype) : LNKTYPE,
74390Sstevel@tonic-gate 		    linkinfo, &attrlen);
74400Sstevel@tonic-gate 		Gen.g_filesz = attrlen;
74410Sstevel@tonic-gate 		write_hdr(ARCHIVE_XATTR, (off_t)attrlen);
74420Sstevel@tonic-gate 		(void) sprintf(namep, "%s/%s", DEVNULL, Gen.g_attrnam_p);
74430Sstevel@tonic-gate 		(void) write_ancillary(attrbuf, attrlen);
74440Sstevel@tonic-gate 	}
74450Sstevel@tonic-gate 
74460Sstevel@tonic-gate 	(void) creat_hdr();
74470Sstevel@tonic-gate #endif
74480Sstevel@tonic-gate }
74490Sstevel@tonic-gate 
74500Sstevel@tonic-gate static int
74510Sstevel@tonic-gate retry_attrdir_open(char *name)
74520Sstevel@tonic-gate {
74530Sstevel@tonic-gate 	int dirfd = -1;
74540Sstevel@tonic-gate 	struct timeval times[2];
74550Sstevel@tonic-gate 	mode_t newmode;
74560Sstevel@tonic-gate 	struct stat parentstat;
74570Sstevel@tonic-gate 
74580Sstevel@tonic-gate 	/*
74590Sstevel@tonic-gate 	 * We couldn't get to attrdir. See if its
74600Sstevel@tonic-gate 	 * just a mode problem on the parent file.
74610Sstevel@tonic-gate 	 * for example: a mode such as r-xr--r--
74620Sstevel@tonic-gate 	 * won't let us create an attribute dir
74630Sstevel@tonic-gate 	 * if it doesn't already exist.
74640Sstevel@tonic-gate 	 */
74650Sstevel@tonic-gate 
74660Sstevel@tonic-gate 	if (stat(name, &parentstat) == -1) {
74670Sstevel@tonic-gate 		msg(ERRN, "Cannot stat file %s", name);
74680Sstevel@tonic-gate 		return (-1);
74690Sstevel@tonic-gate 	}
74700Sstevel@tonic-gate 	newmode = S_IWUSR | parentstat.st_mode;
74710Sstevel@tonic-gate 	if (chmod(name, newmode) == -1) {
74720Sstevel@tonic-gate 		msg(ERRN, "Cannot change mode of file %s to %o", name, newmode);
74730Sstevel@tonic-gate 		return (-1);
74740Sstevel@tonic-gate 	}
74750Sstevel@tonic-gate 
74760Sstevel@tonic-gate 	dirfd = attropen(name, ".", O_RDONLY);
74770Sstevel@tonic-gate 	if (dirfd == -1) {
74780Sstevel@tonic-gate 		msg(ERRN, "Cannot open attribute directory of file %s", name);
74790Sstevel@tonic-gate 		return (-1);
74800Sstevel@tonic-gate 	} else {
74810Sstevel@tonic-gate 
74820Sstevel@tonic-gate 		/*
74830Sstevel@tonic-gate 		 * Put mode back to original
74840Sstevel@tonic-gate 		 */
74850Sstevel@tonic-gate 		if (chmod(name, parentstat.st_mode) != 0) {
74860Sstevel@tonic-gate 			msg(ERRN, "Cannot restore permissions of file %s to %o",
74870Sstevel@tonic-gate 			    name, parentstat.st_mode);
74880Sstevel@tonic-gate 		}
74890Sstevel@tonic-gate 
74900Sstevel@tonic-gate 		/*
74910Sstevel@tonic-gate 		 * Put back time stamps
74920Sstevel@tonic-gate 		 */
74930Sstevel@tonic-gate 
74940Sstevel@tonic-gate 		times[0].tv_sec = parentstat.st_atime;
74950Sstevel@tonic-gate 		times[0].tv_usec = 0;
74960Sstevel@tonic-gate 		times[1].tv_sec = parentstat.st_mtime;
74970Sstevel@tonic-gate 		times[1].tv_usec = 0;
74980Sstevel@tonic-gate 		if (utimes(name, times) != 0) {
74990Sstevel@tonic-gate 			msg(ERRN, "Cannot reset timestamps on file %s");
75000Sstevel@tonic-gate 		}
75010Sstevel@tonic-gate 	}
75020Sstevel@tonic-gate 
75030Sstevel@tonic-gate 	return (dirfd);
75040Sstevel@tonic-gate }
75050Sstevel@tonic-gate 
75060Sstevel@tonic-gate /*
75070Sstevel@tonic-gate  * skip over extra slashes in string.
75080Sstevel@tonic-gate  *
75090Sstevel@tonic-gate  * For example:
75100Sstevel@tonic-gate  * /usr/tmp/////
75110Sstevel@tonic-gate  *
75120Sstevel@tonic-gate  * would return pointer at
75130Sstevel@tonic-gate  * /usr/tmp/////
75140Sstevel@tonic-gate  *         ^
75150Sstevel@tonic-gate  */
75160Sstevel@tonic-gate static char *
75170Sstevel@tonic-gate skipslashes(char *string, char *start)
75180Sstevel@tonic-gate {
75190Sstevel@tonic-gate 	while ((string > start) && *(string - 1) == '/') {
75200Sstevel@tonic-gate 		string--;
75210Sstevel@tonic-gate 	}
75220Sstevel@tonic-gate 
75230Sstevel@tonic-gate 	return (string);
75240Sstevel@tonic-gate }
75250Sstevel@tonic-gate 
75260Sstevel@tonic-gate static sl_info_t *
75270Sstevel@tonic-gate sl_info_alloc(void)
75280Sstevel@tonic-gate {
75290Sstevel@tonic-gate 	static int num_left;
75300Sstevel@tonic-gate 	static sl_info_t *slipool;
75310Sstevel@tonic-gate 
75320Sstevel@tonic-gate 	if (num_left > 0) {
75330Sstevel@tonic-gate 		return (&slipool[--num_left]);
75340Sstevel@tonic-gate 	}
75350Sstevel@tonic-gate 	num_left = SL_INFO_ALLOC_CHUNK;
75360Sstevel@tonic-gate 	slipool = e_zalloc(E_EXIT, sizeof (sl_info_t) * num_left);
75370Sstevel@tonic-gate 	return (&slipool[--num_left]);
75380Sstevel@tonic-gate }
75390Sstevel@tonic-gate 
75400Sstevel@tonic-gate /*
75410Sstevel@tonic-gate  * If a match for the key values was found in the tree, return a pointer to it.
75420Sstevel@tonic-gate  * If a match was not found, insert it and return a pointer to it.  This is
75430Sstevel@tonic-gate  * based on Knuth's Algorithm A in Vol 3, section 6.2.3.
75440Sstevel@tonic-gate  */
75450Sstevel@tonic-gate 
75460Sstevel@tonic-gate sl_info_t *
75470Sstevel@tonic-gate sl_insert(dev_t device, ino_t inode)
75480Sstevel@tonic-gate {
75490Sstevel@tonic-gate 	sl_info_t *p;		/* moves down the tree */
75500Sstevel@tonic-gate 	sl_info_t *q;		/* scratch */
75510Sstevel@tonic-gate 	sl_info_t *r;		/* scratch */
75520Sstevel@tonic-gate 	sl_info_t *s;		/* pt where rebalancing may be needed */
75530Sstevel@tonic-gate 	sl_info_t *t;		/* father of s */
75540Sstevel@tonic-gate 	sl_info_t *head;
75550Sstevel@tonic-gate 
75560Sstevel@tonic-gate 	int a;			/* used to hold balance factors */
75570Sstevel@tonic-gate 	int done;		/* loop control */
75580Sstevel@tonic-gate 	int cmpflg;		/* used to hold the result of a comparison */
75590Sstevel@tonic-gate 
75600Sstevel@tonic-gate 	/* initialize */
75610Sstevel@tonic-gate 
75620Sstevel@tonic-gate 	head = sl_devhash_lookup(device);
75630Sstevel@tonic-gate 
75640Sstevel@tonic-gate 	if (head == NULL) {
75650Sstevel@tonic-gate 		head = sl_info_alloc();
75660Sstevel@tonic-gate 		head->llink = NULL;
75670Sstevel@tonic-gate 		head->bal = 0;
75680Sstevel@tonic-gate 
75690Sstevel@tonic-gate 		p = head->rlink = sl_info_alloc();
75700Sstevel@tonic-gate 		p->sl_ino = inode;
75710Sstevel@tonic-gate 		p->sl_count = 0;
75720Sstevel@tonic-gate 		p->bal = 0;
75730Sstevel@tonic-gate 		p->llink = NULL;
75740Sstevel@tonic-gate 		p->rlink = NULL;
75750Sstevel@tonic-gate 		sl_devhash_insert(device, head);
75760Sstevel@tonic-gate 		return (p);
75770Sstevel@tonic-gate 	}
75780Sstevel@tonic-gate 
75790Sstevel@tonic-gate 	t = head;
75800Sstevel@tonic-gate 	s = p = head->rlink;
75810Sstevel@tonic-gate 
75820Sstevel@tonic-gate 	/* compare */
75830Sstevel@tonic-gate 
75840Sstevel@tonic-gate 	for (done = 0; ! done; ) {
75850Sstevel@tonic-gate 		switch (sl_compare(inode, p->sl_ino)) {
75860Sstevel@tonic-gate 			case -1:
75870Sstevel@tonic-gate 				/* move left */
75880Sstevel@tonic-gate 
75890Sstevel@tonic-gate 				q = p->llink;
75900Sstevel@tonic-gate 
75910Sstevel@tonic-gate 				if (q == NULL) {
75920Sstevel@tonic-gate 					q = sl_info_alloc();
75930Sstevel@tonic-gate 					p->llink = q;
75940Sstevel@tonic-gate 					done = 1;
75950Sstevel@tonic-gate 					continue;
75960Sstevel@tonic-gate 				}
75970Sstevel@tonic-gate 
75980Sstevel@tonic-gate 				break;
75990Sstevel@tonic-gate 
76000Sstevel@tonic-gate 			case 0:
76010Sstevel@tonic-gate 				/* found it */
76020Sstevel@tonic-gate 				return (p);
76030Sstevel@tonic-gate 				break;
76040Sstevel@tonic-gate 
76050Sstevel@tonic-gate 			case 1:
76060Sstevel@tonic-gate 				/* move right */
76070Sstevel@tonic-gate 
76080Sstevel@tonic-gate 				q = p->rlink;
76090Sstevel@tonic-gate 
76100Sstevel@tonic-gate 				if (q == NULL) {
76110Sstevel@tonic-gate 					q = sl_info_alloc();
76120Sstevel@tonic-gate 					p->rlink = q;
76130Sstevel@tonic-gate 					done = 1;
76140Sstevel@tonic-gate 					continue;
76150Sstevel@tonic-gate 				}
76160Sstevel@tonic-gate 
76170Sstevel@tonic-gate 				break;
76180Sstevel@tonic-gate 		}
76190Sstevel@tonic-gate 
76200Sstevel@tonic-gate 		if (q->bal != 0) {
76210Sstevel@tonic-gate 			t = p;
76220Sstevel@tonic-gate 			s = q;
76230Sstevel@tonic-gate 		}
76240Sstevel@tonic-gate 
76250Sstevel@tonic-gate 		p = q;
76260Sstevel@tonic-gate 	}
76270Sstevel@tonic-gate 
76280Sstevel@tonic-gate 	/* insert */
76290Sstevel@tonic-gate 
76300Sstevel@tonic-gate 	q->sl_ino = inode;
76310Sstevel@tonic-gate 	q->sl_count = 0;
76320Sstevel@tonic-gate 	q->llink = q->rlink = NULL;
76330Sstevel@tonic-gate 	q->bal = 0;
76340Sstevel@tonic-gate 
76350Sstevel@tonic-gate 	/* adjust balance factors */
76360Sstevel@tonic-gate 
76370Sstevel@tonic-gate 	if ((cmpflg = sl_compare(inode, s->sl_ino)) < 0) {
76380Sstevel@tonic-gate 		r = p = s->llink;
76390Sstevel@tonic-gate 	} else {
76400Sstevel@tonic-gate 		r = p = s->rlink;
76410Sstevel@tonic-gate 	}
76420Sstevel@tonic-gate 
76430Sstevel@tonic-gate 	while (p != q) {
76440Sstevel@tonic-gate 		switch (sl_compare(inode, p->sl_ino)) {
76450Sstevel@tonic-gate 			case -1:
76460Sstevel@tonic-gate 				p->bal = -1;
76470Sstevel@tonic-gate 				p = p->llink;
76480Sstevel@tonic-gate 				break;
76490Sstevel@tonic-gate 
76500Sstevel@tonic-gate 			case 0:
76510Sstevel@tonic-gate 				break;
76520Sstevel@tonic-gate 
76530Sstevel@tonic-gate 			case 1:
76540Sstevel@tonic-gate 				p->bal = 1;
76550Sstevel@tonic-gate 				p = p->rlink;
76560Sstevel@tonic-gate 				break;
76570Sstevel@tonic-gate 		}
76580Sstevel@tonic-gate 	}
76590Sstevel@tonic-gate 
76600Sstevel@tonic-gate 	/* balancing act */
76610Sstevel@tonic-gate 
76620Sstevel@tonic-gate 	if (cmpflg < 0) {
76630Sstevel@tonic-gate 		a = -1;
76640Sstevel@tonic-gate 	} else {
76650Sstevel@tonic-gate 		a = 1;
76660Sstevel@tonic-gate 	}
76670Sstevel@tonic-gate 
76680Sstevel@tonic-gate 	if (s->bal == 0) {
76690Sstevel@tonic-gate 		s->bal = a;
76700Sstevel@tonic-gate 		head->llink = (sl_info_t *)((int)head->llink + 1);
76710Sstevel@tonic-gate 		return (q);
76720Sstevel@tonic-gate 	} else if (s->bal == -a) {
76730Sstevel@tonic-gate 		s->bal = 0;
76740Sstevel@tonic-gate 		return (q);
76750Sstevel@tonic-gate 	}
76760Sstevel@tonic-gate 
76770Sstevel@tonic-gate 	/*
76780Sstevel@tonic-gate 	 * (s->bal == a)
76790Sstevel@tonic-gate 	 */
76800Sstevel@tonic-gate 
76810Sstevel@tonic-gate 	if (r->bal == a) {
76820Sstevel@tonic-gate 		/* single rotation */
76830Sstevel@tonic-gate 
76840Sstevel@tonic-gate 		p = r;
76850Sstevel@tonic-gate 
76860Sstevel@tonic-gate 		if (a == -1) {
76870Sstevel@tonic-gate 			s->llink = r->rlink;
76880Sstevel@tonic-gate 			r->rlink = s;
76890Sstevel@tonic-gate 		} else if (a == 1) {
76900Sstevel@tonic-gate 			s->rlink = r->llink;
76910Sstevel@tonic-gate 			r->llink = s;
76920Sstevel@tonic-gate 		}
76930Sstevel@tonic-gate 
76940Sstevel@tonic-gate 		s->bal = r->bal = 0;
76950Sstevel@tonic-gate 
76960Sstevel@tonic-gate 	} else if (r->bal == -a) {
76970Sstevel@tonic-gate 		/* double rotation */
76980Sstevel@tonic-gate 
76990Sstevel@tonic-gate 		if (a == -1) {
77000Sstevel@tonic-gate 			p = r->rlink;
77010Sstevel@tonic-gate 			r->rlink = p->llink;
77020Sstevel@tonic-gate 			p->llink = r;
77030Sstevel@tonic-gate 			s->llink = p->rlink;
77040Sstevel@tonic-gate 			p->rlink = s;
77050Sstevel@tonic-gate 		} else if (a == 1) {
77060Sstevel@tonic-gate 			p = r->llink;
77070Sstevel@tonic-gate 			r->llink = p->rlink;
77080Sstevel@tonic-gate 			p->rlink = r;
77090Sstevel@tonic-gate 			s->rlink = p->llink;
77100Sstevel@tonic-gate 			p->llink = s;
77110Sstevel@tonic-gate 		}
77120Sstevel@tonic-gate 
77130Sstevel@tonic-gate 		if (p->bal == 0) {
77140Sstevel@tonic-gate 			s->bal = 0;
77150Sstevel@tonic-gate 			r->bal = 0;
77160Sstevel@tonic-gate 		} else if (p->bal == -a) {
77170Sstevel@tonic-gate 			s->bal = 0;
77180Sstevel@tonic-gate 			r->bal = a;
77190Sstevel@tonic-gate 		} else if (p->bal == a) {
77200Sstevel@tonic-gate 			s->bal = -a;
77210Sstevel@tonic-gate 			r->bal = 0;
77220Sstevel@tonic-gate 		}
77230Sstevel@tonic-gate 
77240Sstevel@tonic-gate 		p->bal = 0;
77250Sstevel@tonic-gate 	}
77260Sstevel@tonic-gate 
77270Sstevel@tonic-gate 	/* finishing touch */
77280Sstevel@tonic-gate 
77290Sstevel@tonic-gate 	if (s == t->rlink) {
77300Sstevel@tonic-gate 		t->rlink = p;
77310Sstevel@tonic-gate 	} else {
77320Sstevel@tonic-gate 		t->llink = p;
77330Sstevel@tonic-gate 	}
77340Sstevel@tonic-gate 
77350Sstevel@tonic-gate 	return (q);
77360Sstevel@tonic-gate }
77370Sstevel@tonic-gate 
77380Sstevel@tonic-gate /*
77390Sstevel@tonic-gate  * sl_numlinks: return the number of links that we saw during our preview.
77400Sstevel@tonic-gate  */
77410Sstevel@tonic-gate 
77420Sstevel@tonic-gate static ulong_t
77430Sstevel@tonic-gate sl_numlinks(dev_t device, ino_t inode)
77440Sstevel@tonic-gate {
77450Sstevel@tonic-gate 	sl_info_t *p = sl_search(device, inode);
77460Sstevel@tonic-gate 
77470Sstevel@tonic-gate 	if (p) {
77480Sstevel@tonic-gate 	    return (p->sl_count);
77490Sstevel@tonic-gate 	} else {
77500Sstevel@tonic-gate 	    return (1);
77510Sstevel@tonic-gate 	}
77520Sstevel@tonic-gate }
77530Sstevel@tonic-gate 
77540Sstevel@tonic-gate /*
77550Sstevel@tonic-gate  * sl_preview_synonyms:  Read the file list from the input stream, remembering
77560Sstevel@tonic-gate  * each reference to each file.
77570Sstevel@tonic-gate  */
77580Sstevel@tonic-gate 
77590Sstevel@tonic-gate static void
77600Sstevel@tonic-gate sl_preview_synonyms(void)
77610Sstevel@tonic-gate {
77620Sstevel@tonic-gate 	char buf [APATH+1];
77630Sstevel@tonic-gate 	char *s;
77640Sstevel@tonic-gate 
77650Sstevel@tonic-gate 	char *suffix = "/cpioXXXXXX";
77660Sstevel@tonic-gate 	char *tmpdir = getenv("TMPDIR");
77670Sstevel@tonic-gate 	int    tmpfd, islnk;
77680Sstevel@tonic-gate 	FILE *tmpfile;
77690Sstevel@tonic-gate 	char *tmpfname;
77700Sstevel@tonic-gate 
77710Sstevel@tonic-gate 	if (tmpdir == NULL || *tmpdir == '\0' ||
77720Sstevel@tonic-gate 	    (strlen(tmpdir) + strlen(suffix)) > APATH) {
77730Sstevel@tonic-gate 		struct statvfs tdsb;
77740Sstevel@tonic-gate 
77750Sstevel@tonic-gate 		tmpdir = "/var/tmp";
77760Sstevel@tonic-gate 
77770Sstevel@tonic-gate 		/* /var/tmp is read-only in the mini-root environment */
77780Sstevel@tonic-gate 
77790Sstevel@tonic-gate 		if (statvfs(tmpdir, &tdsb) == -1 || tdsb.f_flag & ST_RDONLY) {
77800Sstevel@tonic-gate 			tmpdir = "/tmp";
77810Sstevel@tonic-gate 		}
77820Sstevel@tonic-gate 	}
77830Sstevel@tonic-gate 
77840Sstevel@tonic-gate 	tmpfname = e_zalloc(E_EXIT, strlen(tmpdir) + strlen(suffix) + 1);
77850Sstevel@tonic-gate 
77860Sstevel@tonic-gate 	(void) strcpy(tmpfname, tmpdir);
77870Sstevel@tonic-gate 	(void) strcat(tmpfname, suffix);
77880Sstevel@tonic-gate 
77890Sstevel@tonic-gate 	if ((tmpfd = mkstemp(tmpfname)) == -1) {
77900Sstevel@tonic-gate 		msg(EXTN, "cannot open tmpfile %s", tmpfname);
77910Sstevel@tonic-gate 	}
77920Sstevel@tonic-gate 
77930Sstevel@tonic-gate 	if (unlink(tmpfname) == -1) {
77940Sstevel@tonic-gate 		msg(EXTN, "cannot unlink tmpfile %s", tmpfname);
77950Sstevel@tonic-gate 	}
77960Sstevel@tonic-gate 
77970Sstevel@tonic-gate 	if ((tmpfile = fdopen(tmpfd, "w+")) == NULL) {
77980Sstevel@tonic-gate 		msg(EXTN, "cannot fdopen tmpfile %s", tmpfname);
77990Sstevel@tonic-gate 	}
78000Sstevel@tonic-gate 
78010Sstevel@tonic-gate 	while ((s = fgets(buf, APATH+1, In_p)) != NULL) {
78020Sstevel@tonic-gate 		size_t lastchar;
78030Sstevel@tonic-gate 		struct stat sb;
78040Sstevel@tonic-gate 
78050Sstevel@tonic-gate 		if (fputs(buf, tmpfile) == EOF) {
78060Sstevel@tonic-gate 			msg(EXTN, "problem writing to tmpfile %s", tmpfname);
78070Sstevel@tonic-gate 		}
78080Sstevel@tonic-gate 
78090Sstevel@tonic-gate 		/* pre-process the name */
78100Sstevel@tonic-gate 
78110Sstevel@tonic-gate 		lastchar = strlen(s) - 1;
78120Sstevel@tonic-gate 
78130Sstevel@tonic-gate 		if (s[lastchar] != '\n' && lastchar == APATH - 1) {
78140Sstevel@tonic-gate 			continue;
78150Sstevel@tonic-gate 		} else {
78160Sstevel@tonic-gate 			s[lastchar] = '\0';
78170Sstevel@tonic-gate 		}
78180Sstevel@tonic-gate 
78190Sstevel@tonic-gate 		while (s[0] == '.' && s[1] == '/') {
78200Sstevel@tonic-gate 			s += 2;
78210Sstevel@tonic-gate 			while (s[0] == '/') {
78220Sstevel@tonic-gate 				s++;
78230Sstevel@tonic-gate 			}
78240Sstevel@tonic-gate 		}
78250Sstevel@tonic-gate 
78260Sstevel@tonic-gate 		if (lstat(s, &sb) < 0) {
78270Sstevel@tonic-gate 			continue;
78280Sstevel@tonic-gate 		}
78290Sstevel@tonic-gate 		islnk = 0;
78300Sstevel@tonic-gate 		if (S_ISLNK(sb.st_mode)) {
78310Sstevel@tonic-gate 			islnk = 1;
78320Sstevel@tonic-gate 			if (Args & OCL) {
78330Sstevel@tonic-gate 				if (stat(s, &sb) < 0) {
78340Sstevel@tonic-gate 					continue;
78350Sstevel@tonic-gate 				}
78360Sstevel@tonic-gate 			}
78370Sstevel@tonic-gate 		}
78380Sstevel@tonic-gate 		sl_remember_tgt(&sb, islnk);
78390Sstevel@tonic-gate 
78400Sstevel@tonic-gate #if defined(O_XATTR)
78410Sstevel@tonic-gate 		if (Atflag) {
78420Sstevel@tonic-gate 			int  dirfd;
78430Sstevel@tonic-gate 			DIR  *dirp;
78440Sstevel@tonic-gate 			struct dirent *dp;
78450Sstevel@tonic-gate 
78460Sstevel@tonic-gate 			if (pathconf(s, _PC_XATTR_EXISTS) != 1)
78470Sstevel@tonic-gate 				continue;
78480Sstevel@tonic-gate 
78490Sstevel@tonic-gate 			dirfd = attropen(s, ".", O_RDONLY);
78500Sstevel@tonic-gate 			if (dirfd == -1)
78510Sstevel@tonic-gate 				continue;
78520Sstevel@tonic-gate 
78530Sstevel@tonic-gate 			tmpfd = dup(dirfd);
78540Sstevel@tonic-gate 			if (tmpfd == -1) {
78550Sstevel@tonic-gate 				(void) close(dirfd);
78560Sstevel@tonic-gate 				continue;
78570Sstevel@tonic-gate 			}
78580Sstevel@tonic-gate 			dirp = fdopendir(tmpfd);
78590Sstevel@tonic-gate 			if (dirp == NULL) {
78600Sstevel@tonic-gate 				(void) close(dirfd);
78610Sstevel@tonic-gate 				(void) close(tmpfd);
78620Sstevel@tonic-gate 				continue;
78630Sstevel@tonic-gate 			}
78640Sstevel@tonic-gate 
78650Sstevel@tonic-gate 			while (dp = readdir(dirp)) {
78660Sstevel@tonic-gate 				if ((dp->d_name[0] == '.' &&
78670Sstevel@tonic-gate 				    dp->d_name[1] == '\0') ||
78680Sstevel@tonic-gate 				    (dp->d_name[0] == '.' &&
78690Sstevel@tonic-gate 				    dp->d_name[1] == '.' &&
78700Sstevel@tonic-gate 				    dp->d_name[2] == '\0'))
78710Sstevel@tonic-gate 					continue;
78720Sstevel@tonic-gate 
78730Sstevel@tonic-gate 				if (fstatat(dirfd, dp->d_name, &sb,
78740Sstevel@tonic-gate 				    AT_SYMLINK_NOFOLLOW) < 0) {
78750Sstevel@tonic-gate 					continue;
78760Sstevel@tonic-gate 				}
78770Sstevel@tonic-gate 				islnk = 0;
78780Sstevel@tonic-gate 				if (S_ISLNK(sb.st_mode)) {
78790Sstevel@tonic-gate 					islnk = 1;
78800Sstevel@tonic-gate 					if (Args & OCL) {
78810Sstevel@tonic-gate 						if (fstatat(dirfd, dp->d_name,
78820Sstevel@tonic-gate 						    &sb, 0) < 0) {
78830Sstevel@tonic-gate 							continue;
78840Sstevel@tonic-gate 						}
78850Sstevel@tonic-gate 					}
78860Sstevel@tonic-gate 				}
78870Sstevel@tonic-gate 				sl_remember_tgt(&sb, islnk);
78880Sstevel@tonic-gate 			}
78890Sstevel@tonic-gate 			(void) closedir(dirp);
78900Sstevel@tonic-gate 			(void) close(dirfd);
78910Sstevel@tonic-gate 		}
78920Sstevel@tonic-gate #endif
78930Sstevel@tonic-gate 	}
78940Sstevel@tonic-gate 
78950Sstevel@tonic-gate 	if (ferror(In_p)) {
78960Sstevel@tonic-gate 		msg(EXTN, "error reading stdin");
78970Sstevel@tonic-gate 	}
78980Sstevel@tonic-gate 
78990Sstevel@tonic-gate 	if (fseek(tmpfile, 0L, SEEK_SET) == -1) {
79000Sstevel@tonic-gate 		msg(EXTN, "cannot fseek on tmpfile %s", tmpfname);
79010Sstevel@tonic-gate 	}
79020Sstevel@tonic-gate 
79030Sstevel@tonic-gate 	In_p = tmpfile;
79040Sstevel@tonic-gate 	free(tmpfname);
79050Sstevel@tonic-gate }
79060Sstevel@tonic-gate 
79070Sstevel@tonic-gate /*
79080Sstevel@tonic-gate  * sl_remember_tgt: Add the device/inode for lstat or stat info to the list of
79090Sstevel@tonic-gate  * those we've seen before.
79100Sstevel@tonic-gate  *
79110Sstevel@tonic-gate  * This tree (rooted under head) is keyed by the device/inode of the file
79120Sstevel@tonic-gate  * being pointed to.  A count is kept of the number of references encountered
79130Sstevel@tonic-gate  * so far.
79140Sstevel@tonic-gate  */
79150Sstevel@tonic-gate 
79160Sstevel@tonic-gate static void
79170Sstevel@tonic-gate sl_remember_tgt(const struct stat *sbp, int isSymlink)
79180Sstevel@tonic-gate {
79190Sstevel@tonic-gate 	sl_info_t *p;
79200Sstevel@tonic-gate 	dev_t device;
79210Sstevel@tonic-gate 	ino_t inode;
79220Sstevel@tonic-gate 
79230Sstevel@tonic-gate 	device = sbp->st_dev;
79240Sstevel@tonic-gate 	inode  = sbp->st_ino;
79250Sstevel@tonic-gate 
79260Sstevel@tonic-gate 	/* Determine whether we've seen this one before */
79270Sstevel@tonic-gate 
79280Sstevel@tonic-gate 	p = sl_insert(device, inode);
79290Sstevel@tonic-gate 
79300Sstevel@tonic-gate 	if (p->sl_count > 0) {
79310Sstevel@tonic-gate 		/*
79320Sstevel@tonic-gate 		 * We have seen this file before.
79330Sstevel@tonic-gate 		 * Note that if we are not chasing symlinks, and this one is a
79340Sstevel@tonic-gate 		 * symlink, it is identically the one we saw before (you cannot
79350Sstevel@tonic-gate 		 * have hard links to symlinks); in this case, we leave the
79360Sstevel@tonic-gate 		 * count alone, so that we don't wind up archiving a symlink to
79370Sstevel@tonic-gate 		 * itself.
79380Sstevel@tonic-gate 		 */
79390Sstevel@tonic-gate 
79400Sstevel@tonic-gate 		if ((Args & OCL) || (! isSymlink)) {
79410Sstevel@tonic-gate 			p->sl_count++;
79420Sstevel@tonic-gate 		}
79430Sstevel@tonic-gate 	} else {
79440Sstevel@tonic-gate 		/* We have not seen this file before */
79450Sstevel@tonic-gate 
79460Sstevel@tonic-gate 		p->sl_count = 1;
79470Sstevel@tonic-gate 
79480Sstevel@tonic-gate 		if (Use_old_stat) {
79490Sstevel@tonic-gate 			/* -Hodc: remap inode (-1 on overflow) */
79500Sstevel@tonic-gate 
79510Sstevel@tonic-gate 			sl_remap_t  *q;
79520Sstevel@tonic-gate 
79530Sstevel@tonic-gate 			for (q = sl_remap_head; q && (q->dev != device);
79540Sstevel@tonic-gate 			    q = q->next) {
79550Sstevel@tonic-gate 				/* do nothing */
79560Sstevel@tonic-gate 			}
79570Sstevel@tonic-gate 
79580Sstevel@tonic-gate 			if (q == NULL) {
79590Sstevel@tonic-gate 				q = e_zalloc(E_EXIT, sizeof (sl_remap_t));
79600Sstevel@tonic-gate 				q->dev = device;
79610Sstevel@tonic-gate 				p->sl_ino2 = q->inode_count = 1;
79620Sstevel@tonic-gate 
79630Sstevel@tonic-gate 				q->next = (sl_remap_head) ?
79640Sstevel@tonic-gate 				    sl_remap_head->next : NULL;
79650Sstevel@tonic-gate 				sl_remap_head = q;
79660Sstevel@tonic-gate 			} else {
79670Sstevel@tonic-gate 				if ((size_t)q->inode_count <=
79680Sstevel@tonic-gate 				    ((1 << (sizeof (o_ino_t) * 8)) - 1)) {
79690Sstevel@tonic-gate 					/* fits in o_ino_t */
79700Sstevel@tonic-gate 					p->sl_ino2 = ++(q->inode_count);
79710Sstevel@tonic-gate 				} else {
79720Sstevel@tonic-gate 					p->sl_ino2 = (ino_t)-1;
79730Sstevel@tonic-gate 				}
79740Sstevel@tonic-gate 			}
79750Sstevel@tonic-gate 		}
79760Sstevel@tonic-gate 	}
79770Sstevel@tonic-gate }
79780Sstevel@tonic-gate 
79790Sstevel@tonic-gate /*
79800Sstevel@tonic-gate  * A faster search, which does not insert the key values into the tree.
79810Sstevel@tonic-gate  * If the a match was found in the tree, return a pointer to it.  If it was not
79820Sstevel@tonic-gate  * found, return NULL.
79830Sstevel@tonic-gate  */
79840Sstevel@tonic-gate 
79850Sstevel@tonic-gate sl_info_t *
79860Sstevel@tonic-gate sl_search(dev_t device, ino_t inode)
79870Sstevel@tonic-gate {
79880Sstevel@tonic-gate 	sl_info_t *p;		/* moves down the tree */
79890Sstevel@tonic-gate 	int c;			/* comparison value */
79900Sstevel@tonic-gate 	sl_info_t *retval = NULL; /* return value */
79910Sstevel@tonic-gate 	sl_info_t *head;
79920Sstevel@tonic-gate 
79930Sstevel@tonic-gate 	head = sl_devhash_lookup(device);
79940Sstevel@tonic-gate 	if (head != NULL) {
79950Sstevel@tonic-gate 		for (p = head->rlink; p; ) {
79960Sstevel@tonic-gate 			if ((c = sl_compare(inode, p->sl_ino)) == 0) {
79970Sstevel@tonic-gate 				retval = p;
79980Sstevel@tonic-gate 				break;
79990Sstevel@tonic-gate 			} else if (c < 0) {
80000Sstevel@tonic-gate 				p = p->llink;
80010Sstevel@tonic-gate 			} else {
80020Sstevel@tonic-gate 				p = p->rlink;
80030Sstevel@tonic-gate 			}
80040Sstevel@tonic-gate 		}
80050Sstevel@tonic-gate 	}
80060Sstevel@tonic-gate 
80070Sstevel@tonic-gate 	return (retval);
80080Sstevel@tonic-gate }
80090Sstevel@tonic-gate 
80100Sstevel@tonic-gate static sl_info_t *
80110Sstevel@tonic-gate sl_devhash_lookup(dev_t device)
80120Sstevel@tonic-gate {
80130Sstevel@tonic-gate 	int key;
80140Sstevel@tonic-gate 	sl_info_link_t *lp;
80150Sstevel@tonic-gate 	static sl_info_link_t *devcache;
80160Sstevel@tonic-gate 
80170Sstevel@tonic-gate 	if (devcache != NULL && devcache->dev == device) {
80180Sstevel@tonic-gate 		return (devcache->head);
80190Sstevel@tonic-gate 	}
80200Sstevel@tonic-gate 
80210Sstevel@tonic-gate 	key = DEV_HASHKEY(device);
80220Sstevel@tonic-gate 	for (lp = sl_devhash[key]; lp; lp = lp->next) {
80230Sstevel@tonic-gate 		if (lp->dev == device) {
80240Sstevel@tonic-gate 			devcache = lp;
80250Sstevel@tonic-gate 			return (lp->head);
80260Sstevel@tonic-gate 		}
80270Sstevel@tonic-gate 	}
80280Sstevel@tonic-gate 	return (NULL);
80290Sstevel@tonic-gate }
80300Sstevel@tonic-gate 
80310Sstevel@tonic-gate static void
80320Sstevel@tonic-gate sl_devhash_insert(dev_t device, sl_info_t *head)
80330Sstevel@tonic-gate {
80340Sstevel@tonic-gate 	int key = DEV_HASHKEY(device);
80350Sstevel@tonic-gate 	sl_info_link_t *lp;
80360Sstevel@tonic-gate 
80370Sstevel@tonic-gate 	lp = e_zalloc(E_EXIT, sizeof (sl_info_link_t));
80380Sstevel@tonic-gate 	lp->dev = device;
80390Sstevel@tonic-gate 	lp->head = head;
80400Sstevel@tonic-gate 	lp->next = sl_devhash[key];
80410Sstevel@tonic-gate 	sl_devhash[key] = lp;
80420Sstevel@tonic-gate }
80430Sstevel@tonic-gate 
80440Sstevel@tonic-gate static void
80450Sstevel@tonic-gate chop_endslashes(char *path)
80460Sstevel@tonic-gate {
80470Sstevel@tonic-gate 	char *end, *ptr;
80480Sstevel@tonic-gate 
80490Sstevel@tonic-gate 	end = &path[strlen(path) -1];
80500Sstevel@tonic-gate 	if (*end == '/' && end != path) {
80510Sstevel@tonic-gate 		ptr = skipslashes(end, path);
80520Sstevel@tonic-gate 		if (ptr != NULL && ptr != path) {
80530Sstevel@tonic-gate 			*ptr = '\0';
80540Sstevel@tonic-gate 		}
80550Sstevel@tonic-gate 	}
80560Sstevel@tonic-gate }
80570Sstevel@tonic-gate 
80580Sstevel@tonic-gate #if !defined(O_XATTR)
80590Sstevel@tonic-gate int
80600Sstevel@tonic-gate openat64(int fd, char *name, int oflag, mode_t cmode)
80610Sstevel@tonic-gate {
80620Sstevel@tonic-gate 	return (open64(name, oflag, cmode));
80630Sstevel@tonic-gate }
80640Sstevel@tonic-gate 
80650Sstevel@tonic-gate int
80660Sstevel@tonic-gate openat(int fd, char *name, int oflag, mode_t cmode)
80670Sstevel@tonic-gate {
80680Sstevel@tonic-gate 	return (open(name, oflag, cmode));
80690Sstevel@tonic-gate }
80700Sstevel@tonic-gate 
80710Sstevel@tonic-gate int
80720Sstevel@tonic-gate fchownat(int fd, char *name, uid_t owner, gid_t group, int flag)
80730Sstevel@tonic-gate {
80740Sstevel@tonic-gate 	if (flag == AT_SYMLINK_NOFOLLOW)
80750Sstevel@tonic-gate 		return (lchown(name, owner, group));
80760Sstevel@tonic-gate 	else
80770Sstevel@tonic-gate 		return (chown(name, owner, group));
80780Sstevel@tonic-gate }
80790Sstevel@tonic-gate 
80800Sstevel@tonic-gate int
80810Sstevel@tonic-gate renameat(int fromfd, char *old, int tofd, char *new)
80820Sstevel@tonic-gate {
80830Sstevel@tonic-gate 	return (rename(old, new));
80840Sstevel@tonic-gate }
80850Sstevel@tonic-gate 
80860Sstevel@tonic-gate int
80870Sstevel@tonic-gate futimesat(int fd, char *path, struct timeval times[2])
80880Sstevel@tonic-gate {
80890Sstevel@tonic-gate 	return (utimes(path, times));
80900Sstevel@tonic-gate }
80910Sstevel@tonic-gate 
80920Sstevel@tonic-gate int
80930Sstevel@tonic-gate unlinkat(int dirfd, char *path, int flag)
80940Sstevel@tonic-gate {
80950Sstevel@tonic-gate 	if (flag == AT_REMOVEDIR) {
80960Sstevel@tonic-gate 		return (rmdir(path));
80970Sstevel@tonic-gate 	} else {
80980Sstevel@tonic-gate 		return (unlink(path));
80990Sstevel@tonic-gate 	}
81000Sstevel@tonic-gate }
81010Sstevel@tonic-gate 
81020Sstevel@tonic-gate int
81030Sstevel@tonic-gate fstatat(int fd, char *path, struct stat *buf, int flag)
81040Sstevel@tonic-gate {
81050Sstevel@tonic-gate 	if (flag == AT_SYMLINK_NOFOLLOW)
81060Sstevel@tonic-gate 		return (lstat(path, buf));
81070Sstevel@tonic-gate 	else
81080Sstevel@tonic-gate 		return (stat(path, buf));
81090Sstevel@tonic-gate }
81100Sstevel@tonic-gate 
81110Sstevel@tonic-gate int
81120Sstevel@tonic-gate attropen(char *file, char *attr, int omode, mode_t cmode)
81130Sstevel@tonic-gate {
81140Sstevel@tonic-gate 	errno = ENOTSUP;
81150Sstevel@tonic-gate 	return (-1);
81160Sstevel@tonic-gate }
81170Sstevel@tonic-gate #endif
8118