xref: /onnv-gate/usr/src/cmd/cpio/cpio.c (revision 4301:641e8bb76038)
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
53919Sceastha  * Common Development and Distribution License (the "License").
63919Sceastha  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
223919Sceastha  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	All Rights Reserved					*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
310Sstevel@tonic-gate  * under license from the Regents of the University of California.
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <stdio.h>
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <errno.h>
390Sstevel@tonic-gate #include <unistd.h>
400Sstevel@tonic-gate #include <stdlib.h>
410Sstevel@tonic-gate #include <fcntl.h>
420Sstevel@tonic-gate #include <memory.h>
430Sstevel@tonic-gate #include <string.h>
440Sstevel@tonic-gate #include <stdarg.h>
450Sstevel@tonic-gate #include <sys/stat.h>
460Sstevel@tonic-gate #include <sys/statvfs.h>
470Sstevel@tonic-gate #include <sys/mkdev.h>
480Sstevel@tonic-gate #include <sys/param.h>
490Sstevel@tonic-gate #include <utime.h>
500Sstevel@tonic-gate #include <pwd.h>
510Sstevel@tonic-gate #include <grp.h>
520Sstevel@tonic-gate #include <signal.h>
530Sstevel@tonic-gate #include <ctype.h>
540Sstevel@tonic-gate #include <archives.h>
550Sstevel@tonic-gate #include <locale.h>
560Sstevel@tonic-gate #include <sys/ioctl.h>
570Sstevel@tonic-gate #include <sys/mtio.h>
580Sstevel@tonic-gate #include <sys/fdio.h>
590Sstevel@tonic-gate #include "cpio.h"
600Sstevel@tonic-gate #include <sys/acl.h>
610Sstevel@tonic-gate #include <sys/time.h>
620Sstevel@tonic-gate #include <sys/resource.h>
630Sstevel@tonic-gate #include <fnmatch.h>
640Sstevel@tonic-gate #include <libgen.h>
650Sstevel@tonic-gate #include <libintl.h>
660Sstevel@tonic-gate #include <dirent.h>
670Sstevel@tonic-gate #include <limits.h>
68789Sahrens #include <aclutils.h>
691134Sceastha #ifdef SOLARIS_PRIVS
701134Sceastha #include <priv.h>
711134Sceastha #endif	/* SOLARIS_PRIVS */
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * Special kludge for off_t being a signed quantity.
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate #if _FILE_OFFSET_BITS == 64
770Sstevel@tonic-gate typedef	u_longlong_t	u_off_t;
780Sstevel@tonic-gate #else
790Sstevel@tonic-gate typedef	ulong_t		u_off_t;
800Sstevel@tonic-gate #endif
810Sstevel@tonic-gate 
820Sstevel@tonic-gate #define	SECMODE	0xe080
830Sstevel@tonic-gate 
840Sstevel@tonic-gate #define	DEVNULL		"/dev/null"
850Sstevel@tonic-gate #define	XATTRHDR	".hdr"
860Sstevel@tonic-gate 
870Sstevel@tonic-gate #define	NAMELEN		32
880Sstevel@tonic-gate #define	TYPELEN 	16
890Sstevel@tonic-gate #define	PERMLEN		4
900Sstevel@tonic-gate 
910Sstevel@tonic-gate #define	FILE_COPIED	1
920Sstevel@tonic-gate #define	FILE_LINKED	2
930Sstevel@tonic-gate #define	FILE_PASS_ERR	-1
940Sstevel@tonic-gate 
950Sstevel@tonic-gate #define	ARCHIVE_NORMAL	0
960Sstevel@tonic-gate #define	ARCHIVE_ACL	1
970Sstevel@tonic-gate #define	ARCHIVE_XATTR	2
980Sstevel@tonic-gate 
990Sstevel@tonic-gate #define	LSTAT(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, AT_SYMLINK_NOFOLLOW)
1020Sstevel@tonic-gate #define	STAT(dir, path, statbuf) fstatat(dir, \
1030Sstevel@tonic-gate     get_component((Gen.g_attrnam_p == (char *)NULL) ? \
1040Sstevel@tonic-gate     path : Gen.g_attrnam_p), statbuf, 0)
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate /*
1070Sstevel@tonic-gate  *	These limits reflect the maximum size regular file that
1080Sstevel@tonic-gate  *	can be archived, depending on the archive type. For archives
1090Sstevel@tonic-gate  *	with character-format headers (odc, tar, ustar) we use
11036Schin  *	CHAR_OFFSET_MAX.  For archives with SVR4 ASCII headers (-c, -H crc)
11136Schin  *	we store filesize in an 8-char hexadecimal string and use
11236Schin  *	ASC_OFFSET_MAX.  Otherwise, we are limited to the size that will
11336Schin  *	fit in a signed long value.
1140Sstevel@tonic-gate  */
11536Schin #define	CHAR_OFFSET_MAX	077777777777ULL	/* 11 octal digits */
11636Schin #define	ASC_OFFSET_MAX	0XFFFFFFFF	/* 8 hexadecimal digits */
1170Sstevel@tonic-gate #define	BIN_OFFSET_MAX	LONG_MAX	/* signed long max value */
1180Sstevel@tonic-gate 
1191231Smarks #define	POSIXMODES	07777
1201231Smarks 
1210Sstevel@tonic-gate static char	aclchar = ' ';
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate static struct Lnk *add_lnk(struct Lnk **);
1240Sstevel@tonic-gate static int bfill(void);
1250Sstevel@tonic-gate static void bflush(void);
1260Sstevel@tonic-gate static int chgreel(int dir);
1270Sstevel@tonic-gate static int ckname(int);
1280Sstevel@tonic-gate static void ckopts(long mask);
1290Sstevel@tonic-gate static long cksum(char hdr, int byt_cnt, int *err);
1300Sstevel@tonic-gate static int creat_hdr(void);
1310Sstevel@tonic-gate static int creat_lnk(int dirfd, char *name1_p, char *name2_p);
1320Sstevel@tonic-gate static int creat_spec(int dirfd);
1330Sstevel@tonic-gate static int creat_tmp(char *nam_p);
1340Sstevel@tonic-gate static void data_in(int proc_mode);
1350Sstevel@tonic-gate static void data_out(void);
1360Sstevel@tonic-gate static void data_pass(void);
1370Sstevel@tonic-gate static void file_in(void);
1380Sstevel@tonic-gate static int file_out(void);
1390Sstevel@tonic-gate static int file_pass(void);
1400Sstevel@tonic-gate static void flush_lnks(void);
1410Sstevel@tonic-gate static int gethdr(void);
1420Sstevel@tonic-gate static int getname(void);
1430Sstevel@tonic-gate static void getpats(int largc, char **largv);
1440Sstevel@tonic-gate static void ioerror(int dir);
1450Sstevel@tonic-gate static int matched(void);
1460Sstevel@tonic-gate static int missdir(char *nam_p);
1470Sstevel@tonic-gate static long mklong(short v[]);
1480Sstevel@tonic-gate static void mkshort(short sval[], long v);
1490Sstevel@tonic-gate static void msg(int severity, const char *fmt, ...);
1500Sstevel@tonic-gate static int openout(int dirfd);
1510Sstevel@tonic-gate static int read_hdr(int hdr);
1520Sstevel@tonic-gate static void reclaim(struct Lnk *l_p);
1530Sstevel@tonic-gate static void rstbuf(void);
1540Sstevel@tonic-gate static void setpasswd(char *nam);
1550Sstevel@tonic-gate static void rstfiles(int over, int dirfd);
1560Sstevel@tonic-gate static void scan4trail(void);
1570Sstevel@tonic-gate static void setup(int largc, char **largv);
1580Sstevel@tonic-gate static void set_tym(int dirfd, char *nam_p, time_t atime, time_t mtime);
1590Sstevel@tonic-gate static void sigint(int sig);
1600Sstevel@tonic-gate static void swap(char *buf_p, int cnt);
1610Sstevel@tonic-gate static void usage(void);
1620Sstevel@tonic-gate static void verbose(char *nam_p);
1630Sstevel@tonic-gate static void write_hdr(int secflag, off_t len);
1640Sstevel@tonic-gate static void write_trail(void);
1650Sstevel@tonic-gate static int ustar_dir(void);
1660Sstevel@tonic-gate static int ustar_spec(void);
1670Sstevel@tonic-gate static struct stat *convert_to_old_stat(struct stat *, char *, char *);
1680Sstevel@tonic-gate static void read_bar_vol_hdr(void);
1690Sstevel@tonic-gate static void read_bar_file_hdr(void);
1700Sstevel@tonic-gate static void setup_uncompress(FILE **);
1710Sstevel@tonic-gate static void skip_bar_volhdr(void);
1720Sstevel@tonic-gate static void bar_file_in(void);
1730Sstevel@tonic-gate static int g_init(int *devtype, int *fdes);
1740Sstevel@tonic-gate static int g_read(int, int, char *, unsigned);
1750Sstevel@tonic-gate static int g_write(int, int, char *, unsigned);
1760Sstevel@tonic-gate static int is_floppy(int);
1770Sstevel@tonic-gate static int is_tape(int);
1780Sstevel@tonic-gate static void write_ancillary(char *secinfo, int len);
1790Sstevel@tonic-gate static int remove_dir(char *);
1800Sstevel@tonic-gate static int save_cwd(void);
1810Sstevel@tonic-gate static void rest_cwd(int cwd);
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate static void xattrs_out(int (*func)());
1840Sstevel@tonic-gate static void get_parent(char *path, char *dir);
1850Sstevel@tonic-gate static void prepare_xattr_hdr(char **attrbuf, char *filename,
1860Sstevel@tonic-gate     char *attrname, char typeflag, struct Lnk *linkinfo, int *rlen);
1870Sstevel@tonic-gate static char tartype(int type);
1880Sstevel@tonic-gate static int openfile(int omode);
1890Sstevel@tonic-gate static mode_t attrmode(char type);
1900Sstevel@tonic-gate static char *get_component(char *path);
1910Sstevel@tonic-gate static int open_dir(char *name);
1920Sstevel@tonic-gate static int open_dirfd();
1930Sstevel@tonic-gate static void close_dirfd();
1940Sstevel@tonic-gate static void write_xattr_hdr();
1950Sstevel@tonic-gate static int retry_attrdir_open(char *name);
1960Sstevel@tonic-gate static char *skipslashes(char *string, char *start);
1970Sstevel@tonic-gate static int read_xattr_hdr();
1980Sstevel@tonic-gate static void chop_endslashes(char *path);
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate /* helpful types */
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate static
2040Sstevel@tonic-gate struct passwd	*Curpw_p,	/* Current password entry for -t option */
2050Sstevel@tonic-gate 		*Rpw_p,		/* Password entry for -R option */
2060Sstevel@tonic-gate 		*dpasswd;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate static
2090Sstevel@tonic-gate struct group	*Curgr_p,	/* Current group entry for -t option */
2100Sstevel@tonic-gate 		*dgroup;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate /* Data structure for buffered I/O. */
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate static
2150Sstevel@tonic-gate struct buf_info {
2160Sstevel@tonic-gate 	char	*b_base_p,	/* Pointer to base of buffer */
2170Sstevel@tonic-gate 		*b_out_p,	/* Position to take bytes from buffer at */
2180Sstevel@tonic-gate 		*b_in_p,	/* Position to put bytes into buffer at */
2190Sstevel@tonic-gate 		*b_end_p;	/* Pointer to end of buffer */
2200Sstevel@tonic-gate 	long	b_cnt,		/* Count of unprocessed bytes */
2210Sstevel@tonic-gate 		b_size;		/* Size of buffer in bytes */
2220Sstevel@tonic-gate } Buffr;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate /* Generic header format */
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate static
2270Sstevel@tonic-gate struct gen_hdr {
2280Sstevel@tonic-gate 	ulong_t	g_magic,	/* Magic number field */
2290Sstevel@tonic-gate 		g_ino,		/* Inode number of file */
2300Sstevel@tonic-gate 		g_mode,		/* Mode of file */
2310Sstevel@tonic-gate 		g_uid,		/* Uid of file */
2320Sstevel@tonic-gate 		g_gid,		/* Gid of file */
2330Sstevel@tonic-gate 		g_nlink,	/* Number of links */
2340Sstevel@tonic-gate 		g_mtime;	/* Modification time */
2350Sstevel@tonic-gate 	off_t	g_filesz;	/* Length of file */
2360Sstevel@tonic-gate 	ulong_t	g_dev,		/* File system of file */
2370Sstevel@tonic-gate 		g_rdev,		/* Major/minor numbers of special files */
2380Sstevel@tonic-gate 		g_namesz,	/* Length of filename */
2390Sstevel@tonic-gate 		g_cksum;	/* Checksum of file */
2400Sstevel@tonic-gate 	char	g_gname[32],
2410Sstevel@tonic-gate 		g_uname[32],
2420Sstevel@tonic-gate 		g_version[2],
2430Sstevel@tonic-gate 		g_tmagic[6],
2440Sstevel@tonic-gate 		g_typeflag;
2450Sstevel@tonic-gate 	char	*g_tname,
2460Sstevel@tonic-gate 		*g_prefix,
2470Sstevel@tonic-gate 		*g_nam_p,	/* Filename */
2480Sstevel@tonic-gate 		*g_attrnam_p,	/* attribute */
2490Sstevel@tonic-gate 		*g_attrfnam_p,  /* Real file name attr belongs to */
2500Sstevel@tonic-gate 		*g_linktoattrfnam_p, /* file linked attribute belongs to */
2510Sstevel@tonic-gate 		*g_linktoattrnam_p,  /* attribute g_attrnam_p is linked to */
2520Sstevel@tonic-gate 		*g_dirpath;	/* dirname currently opened */
2530Sstevel@tonic-gate 	int	g_dirfd;	/* directory file descriptor */
2540Sstevel@tonic-gate 	int	g_passdirfd;	/* directory fd to pass to */
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate } Gen, *G_p;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate /* Data structure for handling multiply-linked files */
2590Sstevel@tonic-gate static
2600Sstevel@tonic-gate char	prebuf[PRESIZ+1],
2610Sstevel@tonic-gate 	nambuf[NAMSIZ+1],
2620Sstevel@tonic-gate 	fullnam[MAXNAM+1];
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate static
2660Sstevel@tonic-gate struct Lnk {
2670Sstevel@tonic-gate 	short	L_cnt,		/* Number of links encountered */
2680Sstevel@tonic-gate 		L_data;		/* Data has been encountered if 1 */
2690Sstevel@tonic-gate 	struct gen_hdr	L_gen;	/* gen_hdr information for this file */
2700Sstevel@tonic-gate 	struct Lnk	*L_nxt_p,	/* Next file in list */
2710Sstevel@tonic-gate 			*L_bck_p,	/* Previous file in list */
2720Sstevel@tonic-gate 			*L_lnk_p;	/* Next link for this file */
2730Sstevel@tonic-gate } Lnk_hd;
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate static
2760Sstevel@tonic-gate struct hdr_cpio	Hdr;
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate /*
2790Sstevel@tonic-gate  * -------------------------------------------------------------------------
2800Sstevel@tonic-gate  *		   Stuff needed to pre-view the name stream
2810Sstevel@tonic-gate  *
2820Sstevel@tonic-gate  * issymlink is used to remember that the current file is a symlink between
2830Sstevel@tonic-gate  * getname() and file_pass(); the former trashes this information immediately
2840Sstevel@tonic-gate  * when -L is specified.
2850Sstevel@tonic-gate  */
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate static
2880Sstevel@tonic-gate int	issymlink = 0;
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate static
2910Sstevel@tonic-gate FILE	*In_p = stdin;		/* Where the input comes from */
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate typedef struct sl_info
2940Sstevel@tonic-gate {
2950Sstevel@tonic-gate 	struct sl_info *llink;	/* Left subtree ptr (tree depth in *sl_head) */
2960Sstevel@tonic-gate 	struct sl_info *rlink;	/* Right subtree ptr */
2970Sstevel@tonic-gate 	int bal;		/* Subtree balance factor */
2980Sstevel@tonic-gate 	ulong_t	sl_count;	/* Number of symlinks */
2990Sstevel@tonic-gate 	ino_t	sl_ino;		/* Inode of file */
3000Sstevel@tonic-gate 	ino_t	sl_ino2;	/* alternate inode for -Hodc */
3010Sstevel@tonic-gate } sl_info_t;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate /*
3040Sstevel@tonic-gate  * The following structure maintains a hash entry for the
3050Sstevel@tonic-gate  * balancing trees which are allocated for each device nodes.
3060Sstevel@tonic-gate  */
3070Sstevel@tonic-gate typedef struct sl_info_link
3080Sstevel@tonic-gate {
3090Sstevel@tonic-gate 	dev_t		dev;
3100Sstevel@tonic-gate 	sl_info_t	*head;
3110Sstevel@tonic-gate 	struct sl_info_link *next;
3120Sstevel@tonic-gate } sl_info_link_t;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate #define	SL_INFO_ALLOC_CHUNK	1024
3150Sstevel@tonic-gate #define	NDEVHENTRY		0x40
3160Sstevel@tonic-gate #define	DEV_HASHKEY(x)		((x) & (NDEVHENTRY -1))
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate /*
3190Sstevel@tonic-gate  * For remapping dev,inode for -Hodc archives.
3200Sstevel@tonic-gate  */
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate typedef struct sl_remap
3230Sstevel@tonic-gate {
3240Sstevel@tonic-gate 	dev_t			dev;		/* device */
3250Sstevel@tonic-gate 	int			inode_count;	/* # inodes seen on dev */
3260Sstevel@tonic-gate 	struct sl_remap 	*next;		/* next in the chain */
3270Sstevel@tonic-gate } sl_remap_t;
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate /* forward declarations */
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate static sl_info_t 	*sl_info_alloc(void);
3320Sstevel@tonic-gate static sl_info_t 	*sl_insert(dev_t, ino_t);
3330Sstevel@tonic-gate static ulong_t		sl_numlinks(dev_t, ino_t);
3340Sstevel@tonic-gate static void		sl_preview_synonyms(void);
3350Sstevel@tonic-gate static void		sl_remember_tgt(const struct stat *, int);
3360Sstevel@tonic-gate static sl_info_t 	*sl_search(dev_t, ino_t);
3370Sstevel@tonic-gate static sl_info_t	*sl_devhash_lookup(dev_t);
3380Sstevel@tonic-gate static void		sl_devhash_insert(dev_t, sl_info_t *);
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate extern int		sl_compare(ino_t, ino_t);
3410Sstevel@tonic-gate #define	sl_compare(lino, rino)	(lino < rino ? -1 : (lino > rino ? 1 : 0))
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate /* global storage */
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate static sl_remap_t  *sl_remap_head = NULL; /* head of the inode-remap list */
3460Sstevel@tonic-gate static sl_info_link_t	*sl_devhash[NDEVHENTRY]; /* hash table */
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate /*
3490Sstevel@tonic-gate  * -------------------------------------------------------------------------
3500Sstevel@tonic-gate  */
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate static
3530Sstevel@tonic-gate struct stat	ArchSt,	/* stat(2) information of the archive */
3540Sstevel@tonic-gate 		SrcSt,	/* stat(2) information of source file */
3550Sstevel@tonic-gate 		DesSt,	/* stat(2) of destination file */
3560Sstevel@tonic-gate 		*OldSt = NULL;	/* stat info converted to svr32 format */
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate /*
3590Sstevel@tonic-gate  * bin_mag: Used to validate a binary magic number,
3600Sstevel@tonic-gate  * by combining to bytes into an unsigned short.
3610Sstevel@tonic-gate  */
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate static
3640Sstevel@tonic-gate union bin_mag {
3650Sstevel@tonic-gate 	unsigned char b_byte[2];
3660Sstevel@tonic-gate 	ushort_t b_half;
3670Sstevel@tonic-gate } Binmag;
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate static
3700Sstevel@tonic-gate union tblock *Thdr_p;	/* TAR header pointer */
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate static union b_block *bar_Vhdr;
3730Sstevel@tonic-gate static struct gen_hdr Gen_bar_vol;
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate /*
3760Sstevel@tonic-gate  * swpbuf: Used in swap() to swap bytes within a halfword,
3770Sstevel@tonic-gate  * halfwords within a word, or to reverse the order of the
3780Sstevel@tonic-gate  * bytes within a word.  Also used in mklong() and mkshort().
3790Sstevel@tonic-gate  */
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate static
3820Sstevel@tonic-gate union swpbuf {
3830Sstevel@tonic-gate 	unsigned char	s_byte[4];
3840Sstevel@tonic-gate 	ushort_t	s_half[2];
3850Sstevel@tonic-gate 	ulong_t	s_word;
3860Sstevel@tonic-gate } *Swp_p;
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate static
3890Sstevel@tonic-gate char	Adir,			/* Flags object as a directory */
3900Sstevel@tonic-gate 	Hiddendir,		/* Processing hidden attribute directory */
3910Sstevel@tonic-gate 	Aspec,			/* Flags object as a special file */
3920Sstevel@tonic-gate 	Do_rename,		/* Indicates rename() is to be used */
3930Sstevel@tonic-gate 	Time[50],		/* Array to hold date and time */
3940Sstevel@tonic-gate 	Ttyname[] = "/dev/tty",	/* Controlling console */
3950Sstevel@tonic-gate 	T_lname[MAXPATHLEN],	/* Array to hold links name for tar */
3960Sstevel@tonic-gate 	*Buf_p,			/* Buffer for file system I/O */
3970Sstevel@tonic-gate 	*Empty,			/* Empty block for TARTYP headers */
3980Sstevel@tonic-gate 	*Full_p,		/* Pointer to full pathname */
3990Sstevel@tonic-gate 	*Efil_p,		/* -E pattern file string */
4000Sstevel@tonic-gate 	*Eom_p = "Change to part %d and press RETURN key. [q] ",
4010Sstevel@tonic-gate 	*Fullnam_p,		/* Full pathname */
4020Sstevel@tonic-gate 	*Attrfile_p,		/* attribute file */
4030Sstevel@tonic-gate 	*Hdr_p,			/* -H header type string */
4040Sstevel@tonic-gate 	*IOfil_p,		/* -I/-O input/output archive string */
4050Sstevel@tonic-gate 	*Lnkend_p,		/* Pointer to end of Lnknam_p */
4060Sstevel@tonic-gate 	*Lnknam_p,		/* Buffer for linking files with -p option */
4070Sstevel@tonic-gate 	*Nam_p,			/* Array to hold filename */
4080Sstevel@tonic-gate 	*Savenam_p,		/* copy of filename xattr belongs to */
4090Sstevel@tonic-gate 	*Own_p,			/* New owner login id string */
4100Sstevel@tonic-gate 	*Renam_p,		/* Buffer for renaming files */
4110Sstevel@tonic-gate 	*Renametmp_p,		/* Tmp Buffer for renaming files */
4120Sstevel@tonic-gate 	*Symlnk_p,		/* Buffer for holding symbolic link name */
4130Sstevel@tonic-gate 	*Over_p,		/* Holds temporary filename when overwriting */
4140Sstevel@tonic-gate 	**Pat_pp = 0,		/* Pattern strings */
4150Sstevel@tonic-gate 	bar_linkflag,		/* flag to indicate if the file is a link */
4160Sstevel@tonic-gate 	bar_linkname[MAXPATHLEN]; /* store the name of the link */
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate static
4190Sstevel@tonic-gate int	Append = 0,	/* Flag set while searching to end of archive */
4200Sstevel@tonic-gate 	Archive,	/* File descriptor of the archive */
4210Sstevel@tonic-gate 	Buf_error = 0,	/* I/O error occured during buffer fill */
4220Sstevel@tonic-gate 	Def_mode = 0777,	/* Default file/directory protection modes */
4230Sstevel@tonic-gate 	Device,		/* Device type being accessed (used with libgenIO) */
4240Sstevel@tonic-gate 	Error_cnt = 0,	/* Cumulative count of I/O errors */
4250Sstevel@tonic-gate 	Finished = 1,	/* Indicates that a file transfer has completed */
4260Sstevel@tonic-gate 	Hdrsz = ASCSZ,	/* Fixed length portion of the header */
4270Sstevel@tonic-gate 	Hdr_type,		/* Flag to indicate type of header selected */
4280Sstevel@tonic-gate 	Ifile,		/* File des. of file being archived */
4290Sstevel@tonic-gate 	Ofile,		/* File des. of file being extracted from archive */
4300Sstevel@tonic-gate 	Use_old_stat = 0,    /* Create an old style -Hodc hdr (small dev's) */
4310Sstevel@tonic-gate 	Onecopy = 0,	/* Flags old vs. new link handling */
4320Sstevel@tonic-gate 	Pad_val = 0,	/* Indicates the number of bytes to pad (if any) */
4330Sstevel@tonic-gate 	PageSize = 0,	/* The native page size, used for figuring block size */
4340Sstevel@tonic-gate 	Volcnt = 1,	/* Number of archive volumes processed */
4350Sstevel@tonic-gate 	Verbcnt = 0,	/* Count of number of dots '.' output */
4360Sstevel@tonic-gate 	Eomflag = 0,
4370Sstevel@tonic-gate 	Dflag = 0,
4380Sstevel@tonic-gate 	Atflag = 0,	/* Archive/restore extended attributes */
4390Sstevel@tonic-gate 	Compressed,	/* Flag to indicate if the bar archive is compressed */
4401134Sceastha 	Bar_vol_num = 0, /* Volume number count for bar archive */
4411134Sceastha 	privileged = 0;	/* Flag set if running with higher privileges */
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate static
4450Sstevel@tonic-gate gid_t	Lastgid = -1;	/* Used with -t & -v to record current gid */
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate static
4480Sstevel@tonic-gate uid_t	Lastuid = -1;	/* Used with -t & -v to record current uid */
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate static
4510Sstevel@tonic-gate long	Args,		/* Mask of selected options */
4520Sstevel@tonic-gate 	Max_namesz = CPATH;	/* Maximum size of pathnames/filenames */
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate static
4550Sstevel@tonic-gate int	Bufsize = BUFSZ;	/* Default block size */
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate static u_longlong_t    Blocks;	/* full blocks transferred */
4590Sstevel@tonic-gate static u_longlong_t    SBlocks;	/* cumulative char count from short reads */
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate static off_t	Max_offset = BIN_OFFSET_MAX;	/* largest file size */
4630Sstevel@tonic-gate static off_t	Max_filesz;			/* from getrlimit */
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate static
4660Sstevel@tonic-gate FILE	*Ef_p,			/* File pointer of pattern input file */
4670Sstevel@tonic-gate 	*Err_p = stderr,	/* File pointer for error reporting */
4680Sstevel@tonic-gate 	*Out_p = stdout,	/* File pointer for non-archive output */
4690Sstevel@tonic-gate 	*Rtty_p,		/* Input file pointer for interactive rename */
4700Sstevel@tonic-gate 	*Wtty_p;		/* Output file ptr for interactive rename */
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate static
4730Sstevel@tonic-gate ushort_t	Ftype = S_IFMT;	/* File type mask */
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate /* ACL support */
4760Sstevel@tonic-gate static struct sec_attr {
4770Sstevel@tonic-gate 	char	attr_type;
4780Sstevel@tonic-gate 	char	attr_len[7];
4790Sstevel@tonic-gate 	char	attr_info[1];
4800Sstevel@tonic-gate } *attr;
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate static int	Pflag = 0;	/* flag indicates that acl is preserved */
483789Sahrens static int	acl_is_set = 0; /* True if an acl was set on the file */
484789Sahrens 
485789Sahrens acl_t *aclp;
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate /*
4880Sstevel@tonic-gate  *
4890Sstevel@tonic-gate  * cpio has been changed to support extended attributes.
4900Sstevel@tonic-gate  *
4910Sstevel@tonic-gate  * As part of this change cpio has been changed to use the new *at() syscalls
4920Sstevel@tonic-gate  * such as openat, fchownat(), unlinkat()...
4930Sstevel@tonic-gate  *
4940Sstevel@tonic-gate  * This was done so that attributes can be handled with as few code changes
4950Sstevel@tonic-gate  * as possible.
4960Sstevel@tonic-gate  *
4970Sstevel@tonic-gate  * What this means is that cpio now opens the directory that a file or directory
4980Sstevel@tonic-gate  * resides in and then performs *at() functions to manipulate the entry.
4990Sstevel@tonic-gate  *
5000Sstevel@tonic-gate  * For example a new file is now created like this:
5010Sstevel@tonic-gate  *
5020Sstevel@tonic-gate  * dfd = open(<some dir path>)
5030Sstevel@tonic-gate  * fd = openat(dfd, <name>,....);
5040Sstevel@tonic-gate  *
5050Sstevel@tonic-gate  * or in the case of an extended attribute
5060Sstevel@tonic-gate  *
5070Sstevel@tonic-gate  * dfd = attropen(<pathname>, ".", ....)
5080Sstevel@tonic-gate  *
5090Sstevel@tonic-gate  * Once we have a directory file descriptor all of the *at() functions can
5100Sstevel@tonic-gate  * be applied to it.
5110Sstevel@tonic-gate  *
5120Sstevel@tonic-gate  * unlinkat(dfd, <component name>,...)
5130Sstevel@tonic-gate  * fchownat(dfd, <component name>,..)
5140Sstevel@tonic-gate  *
5150Sstevel@tonic-gate  * This works for both normal namespace files and extended attribute file
5160Sstevel@tonic-gate  *
5170Sstevel@tonic-gate  */
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate /*
5200Sstevel@tonic-gate  * Extended attribute layout
5210Sstevel@tonic-gate  *
5220Sstevel@tonic-gate  * Extended attributes are stored in two pieces.
5230Sstevel@tonic-gate  * 1. An attribute header which has information about
5240Sstevel@tonic-gate  *    what file the attribute is for and what the attribute
5250Sstevel@tonic-gate  *    is named.
5260Sstevel@tonic-gate  * 2. The attribute record itself.  Stored as a normal file type
5270Sstevel@tonic-gate  *    of entry.
5280Sstevel@tonic-gate  * Both the header and attribute record have special modes/typeflags
5290Sstevel@tonic-gate  * associated with them.
5300Sstevel@tonic-gate  *
5310Sstevel@tonic-gate  * The names of the header in the archive look like:
5320Sstevel@tonic-gate  * /dev/null/attr.hdr
5330Sstevel@tonic-gate  *
5340Sstevel@tonic-gate  * The name of the attribute looks like:
5350Sstevel@tonic-gate  * /dev/null/attr.
5360Sstevel@tonic-gate  *
5370Sstevel@tonic-gate  * This is done so that an archiver that doesn't understand these formats
5380Sstevel@tonic-gate  * can just dispose of the attribute records unless the user chooses to
5390Sstevel@tonic-gate  * rename them via cpio -r or pax -i
5400Sstevel@tonic-gate  *
5410Sstevel@tonic-gate  * The format is composed of a fixed size header followed
5420Sstevel@tonic-gate  * by a variable sized xattr_buf. If the attribute is a hard link
5430Sstevel@tonic-gate  * to another attribute, then another xattr_buf section is included
5440Sstevel@tonic-gate  * for the link.
5450Sstevel@tonic-gate  *
5460Sstevel@tonic-gate  * The xattr_buf is used to define the necessary "pathing" steps
5470Sstevel@tonic-gate  * to get to the extended attribute.  This is necessary to support
5480Sstevel@tonic-gate  * a fully recursive attribute model where an attribute may itself
5490Sstevel@tonic-gate  * have an attribute.
5500Sstevel@tonic-gate  *
5510Sstevel@tonic-gate  * The basic layout looks like this.
5520Sstevel@tonic-gate  *
5530Sstevel@tonic-gate  *     --------------------------------
5540Sstevel@tonic-gate  *     |                              |
5550Sstevel@tonic-gate  *     |         xattr_hdr            |
5560Sstevel@tonic-gate  *     |                              |
5570Sstevel@tonic-gate  *     --------------------------------
5580Sstevel@tonic-gate  *     --------------------------------
5590Sstevel@tonic-gate  *     |                              |
5600Sstevel@tonic-gate  *     |        xattr_buf             |
5610Sstevel@tonic-gate  *     |                              |
5620Sstevel@tonic-gate  *     --------------------------------
5630Sstevel@tonic-gate  *     --------------------------------
5640Sstevel@tonic-gate  *     |                              |
5650Sstevel@tonic-gate  *     |      (optional link info)    |
5660Sstevel@tonic-gate  *     |                              |
5670Sstevel@tonic-gate  *     --------------------------------
5680Sstevel@tonic-gate  *     --------------------------------
5690Sstevel@tonic-gate  *     |                              |
5700Sstevel@tonic-gate  *     |      attribute itself        |
5710Sstevel@tonic-gate  *     |      stored as normal tar    |
5720Sstevel@tonic-gate  *     |      or cpio data with       |
5730Sstevel@tonic-gate  *     |      special mode or         |
5740Sstevel@tonic-gate  *     |      typeflag                |
5750Sstevel@tonic-gate  *     |                              |
5760Sstevel@tonic-gate  *     --------------------------------
5770Sstevel@tonic-gate  *
5780Sstevel@tonic-gate  */
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate /*
5810Sstevel@tonic-gate  * Extended attributes structures
5820Sstevel@tonic-gate  *
5830Sstevel@tonic-gate  * xattrhead is the complete extended attribute header, as read of off
5840Sstevel@tonic-gate  * disk/tape. It includes the variable xattr_buf portion.
5850Sstevel@tonic-gate  *
5860Sstevel@tonic-gate  * xattrp is basically an offset into xattrhead that points to the
5870Sstevel@tonic-gate  * "pathing" section which defines how to get to the attribute.
5880Sstevel@tonic-gate  *
5890Sstevel@tonic-gate  * xattr_linkp is identical to xattrp except that it is used for linked
5900Sstevel@tonic-gate  * attributes.  It provides the pathing steps to get to the linked
5910Sstevel@tonic-gate  * attribute.
5920Sstevel@tonic-gate  *
5930Sstevel@tonic-gate  * These structures are updated when an extended attribute header is read off
5940Sstevel@tonic-gate  * of disk/tape.
5950Sstevel@tonic-gate  */
5960Sstevel@tonic-gate static struct xattr_hdr	*xattrhead;
5970Sstevel@tonic-gate static struct xattr_buf	*xattrp;
5980Sstevel@tonic-gate static struct xattr_buf	*xattr_linkp;
5990Sstevel@tonic-gate static int 		xattrbadhead;	/* is extended attribute header bad? */
6000Sstevel@tonic-gate 
601789Sahrens static int	append_secattr(char **, int *, acl_t *);
6020Sstevel@tonic-gate static void	write_ancillary(char *, int);
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate /*
6050Sstevel@tonic-gate  * Note regarding cpio and changes to ensure cpio doesn't try to second
6060Sstevel@tonic-gate  * guess whether it runs with sufficient privileges or not:
6070Sstevel@tonic-gate  *
6080Sstevel@tonic-gate  * cpio has been changed so that it doesn't carry a second implementation of
6090Sstevel@tonic-gate  * the kernel's policy with respect to privileges.  Instead of attempting
6100Sstevel@tonic-gate  * to restore uid and gid from an archive only if cpio is run as uid 0,
6111134Sceastha  * cpio now *always* tries to restore the uid and gid from the archive
6121134Sceastha  * except when the -R option is specified.  When the -R is specified,
6131134Sceastha  * the uid and gid of the restored file will be changed to those of the
6141134Sceastha  * login id specified.  In addition, chown(), set_tym(), and chmod() should
6151134Sceastha  * only be executed once during archive extraction, and to ensure
6161134Sceastha  * setuid/setgid bits are restored properly, chown() should always be
6171134Sceastha  * executed before chmod().
6180Sstevel@tonic-gate  *
6190Sstevel@tonic-gate  * Note regarding debugging mechanism for cpio:
6200Sstevel@tonic-gate  *
6210Sstevel@tonic-gate  * The following mechanism is provided to allow us to debug cpio in complicated
6220Sstevel@tonic-gate  * situations, like when it is part of a pipe.  The idea is that you compile
6230Sstevel@tonic-gate  * with -DWAITAROUND defined, and then add the "-z" command line option to the
6240Sstevel@tonic-gate  * target cpio invocation.  If stderr is available, it will tell you to which
6250Sstevel@tonic-gate  * pid to attach the debugger; otherwise, use ps to find it.  Attach to the
6260Sstevel@tonic-gate  * process from the debugger, and, *PRESTO*, you are there!
6270Sstevel@tonic-gate  *
6280Sstevel@tonic-gate  * Simply assign "waitaround = 0" once you attach to the process, and then
6290Sstevel@tonic-gate  * proceed from there as usual.
6300Sstevel@tonic-gate  */
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate #ifdef WAITAROUND
6330Sstevel@tonic-gate int waitaround = 0;		/* wait for rendezvous with the debugger */
6340Sstevel@tonic-gate #endif
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate /*
6370Sstevel@tonic-gate  * Allocation wrappers and their flags
6380Sstevel@tonic-gate  */
6390Sstevel@tonic-gate #define	E_NORMAL	0x0	/* Return NULL if allocation fails */
6400Sstevel@tonic-gate #define	E_EXIT		0x1	/* Exit if allocation fails */
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate static void *e_realloc(int flag, void *old, size_t newsize);
6430Sstevel@tonic-gate static char *e_strdup(int flag, const char *arg);
6440Sstevel@tonic-gate static void *e_valloc(int flag, size_t size);
6450Sstevel@tonic-gate static void *e_zalloc(int flag, size_t size);
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate #define	EXIT_CODE	(Error_cnt > 255 ? 255 : Error_cnt)
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate /*
6500Sstevel@tonic-gate  * main: Call setup() to process options and perform initializations,
6510Sstevel@tonic-gate  * and then select either copy in (-i), copy out (-o), or pass (-p) action.
6520Sstevel@tonic-gate  */
6530Sstevel@tonic-gate 
65436Schin int
6550Sstevel@tonic-gate main(int argc, char **argv)
6560Sstevel@tonic-gate {
6570Sstevel@tonic-gate 	int i;
6580Sstevel@tonic-gate 	int passret;
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
6610Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
6620Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
6630Sstevel@tonic-gate #endif
6640Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	(void) memset(&Gen, 0, sizeof (Gen));
6670Sstevel@tonic-gate 	setup(argc, argv);
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	if (signal(SIGINT, sigint) == SIG_IGN)
6700Sstevel@tonic-gate 		(void) signal(SIGINT, SIG_IGN);
6710Sstevel@tonic-gate 	switch (Args & (OCi | OCo | OCp)) {
6720Sstevel@tonic-gate 	case OCi: /* COPY IN */
6730Sstevel@tonic-gate 		Hdr_type = NONE;
6740Sstevel@tonic-gate 		while ((i = gethdr()) != 0) {
6750Sstevel@tonic-gate 			Gen.g_dirfd = -1;
6760Sstevel@tonic-gate 			if (i == 1) {
6770Sstevel@tonic-gate 				file_in();
6780Sstevel@tonic-gate 				/*
6790Sstevel@tonic-gate 				 * Any ACL info for this file would or should
6800Sstevel@tonic-gate 				 * have been used after file_in(); clear out
6810Sstevel@tonic-gate 				 * aclp so it is is not erroneously used on
6820Sstevel@tonic-gate 				 * the next file.
6830Sstevel@tonic-gate 				 */
6840Sstevel@tonic-gate 				if (aclp != NULL) {
685789Sahrens 					acl_free(aclp);
6860Sstevel@tonic-gate 					aclp = NULL;
6870Sstevel@tonic-gate 				}
688789Sahrens 				acl_is_set = 0;
6890Sstevel@tonic-gate 			}
6900Sstevel@tonic-gate 			(void) memset(&Gen, 0, sizeof (Gen));
6910Sstevel@tonic-gate 		}
6920Sstevel@tonic-gate 		/* Do not count "extra" "read-ahead" buffered data */
6930Sstevel@tonic-gate 		if (Buffr.b_cnt > Bufsize)
6940Sstevel@tonic-gate 			Blocks -=  (u_longlong_t)(Buffr.b_cnt / Bufsize);
6950Sstevel@tonic-gate 		break;
6960Sstevel@tonic-gate 	case OCo: /* COPY OUT */
6970Sstevel@tonic-gate 		if (Args & OCA) {
6980Sstevel@tonic-gate 			scan4trail();
6990Sstevel@tonic-gate 		}
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 		Gen.g_dirfd = -1;
7020Sstevel@tonic-gate 		Gen.g_dirpath = NULL;
7030Sstevel@tonic-gate 		sl_preview_synonyms();
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 		while ((i = getname()) != 0) {
7060Sstevel@tonic-gate 			if (i == 1) {
7070Sstevel@tonic-gate 				(void) file_out();
7080Sstevel@tonic-gate 				if (Atflag) {
7090Sstevel@tonic-gate 					if (Gen.g_dirfd != -1) {
7100Sstevel@tonic-gate 						(void) close(Gen.g_dirfd);
7110Sstevel@tonic-gate 					}
7120Sstevel@tonic-gate 					Gen.g_dirfd = -1;
7130Sstevel@tonic-gate 					xattrs_out(file_out);
7140Sstevel@tonic-gate 				}
7150Sstevel@tonic-gate 				Hiddendir = 0;
7160Sstevel@tonic-gate 			}
7170Sstevel@tonic-gate 			if (aclp != NULL) {
718789Sahrens 				acl_free(aclp);
7190Sstevel@tonic-gate 				aclp = NULL;
720789Sahrens 				acl_is_set = 0;
7210Sstevel@tonic-gate 			}
7220Sstevel@tonic-gate 		}
7230Sstevel@tonic-gate 		write_trail();
7240Sstevel@tonic-gate 		break;
7250Sstevel@tonic-gate 	case OCp: /* PASS */
7260Sstevel@tonic-gate 		sl_preview_synonyms();
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 		Gen.g_dirfd = -1;
7290Sstevel@tonic-gate 		Gen.g_passdirfd = -1;
7300Sstevel@tonic-gate 		Gen.g_dirpath = NULL;
7310Sstevel@tonic-gate 		while (getname()) {
7320Sstevel@tonic-gate 			/*
7330Sstevel@tonic-gate 			 * If file is a fully qualified path then
7340Sstevel@tonic-gate 			 * file_pass will strip off the leading '/'
7350Sstevel@tonic-gate 			 * and we need to save off the unstripped
7360Sstevel@tonic-gate 			 * name for attribute traversal.
7370Sstevel@tonic-gate 			 */
7380Sstevel@tonic-gate 			if (Atflag) {
7390Sstevel@tonic-gate 				(void) strcpy(Savenam_p, Gen.g_nam_p);
7400Sstevel@tonic-gate 			}
7410Sstevel@tonic-gate 			passret = file_pass();
7420Sstevel@tonic-gate 			if (aclp != NULL) {
743789Sahrens 				acl_free(aclp);
7440Sstevel@tonic-gate 				aclp = NULL;
745789Sahrens 				acl_is_set = 0;
7460Sstevel@tonic-gate 			}
7470Sstevel@tonic-gate 			if (Gen.g_passdirfd != -1)
7480Sstevel@tonic-gate 				(void) close(Gen.g_passdirfd);
7490Sstevel@tonic-gate 			Gen.g_passdirfd = -1;
7500Sstevel@tonic-gate 			if (Atflag) {
7510Sstevel@tonic-gate 				if (Gen.g_dirfd != -1) {
7520Sstevel@tonic-gate 					(void) close(Gen.g_dirfd);
7530Sstevel@tonic-gate 				}
7540Sstevel@tonic-gate 				Gen.g_dirfd = -1;
7550Sstevel@tonic-gate 				if (passret != FILE_LINKED) {
7560Sstevel@tonic-gate 					Gen.g_nam_p = Savenam_p;
7570Sstevel@tonic-gate 					xattrs_out(file_pass);
7580Sstevel@tonic-gate 				}
7590Sstevel@tonic-gate 			}
7600Sstevel@tonic-gate 		}
7610Sstevel@tonic-gate 		break;
7620Sstevel@tonic-gate 	default:
7630Sstevel@tonic-gate 		msg(EXT, "Impossible action.");
7640Sstevel@tonic-gate 	}
7650Sstevel@tonic-gate 	if (Ofile > 0) {
7660Sstevel@tonic-gate 		if (close(Ofile) != 0)
7670Sstevel@tonic-gate 			msg(EXTN, "close error");
7680Sstevel@tonic-gate 	}
7690Sstevel@tonic-gate 	if (Archive > 0) {
7700Sstevel@tonic-gate 		if (close(Archive) != 0)
7710Sstevel@tonic-gate 			msg(EXTN, "close error");
7720Sstevel@tonic-gate 	}
7730Sstevel@tonic-gate 	Blocks = (u_longlong_t)(Blocks * Bufsize + SBlocks + 0x1FF) >> 9;
7740Sstevel@tonic-gate 	msg(EPOST, "%lld blocks", Blocks);
7750Sstevel@tonic-gate 	if (Error_cnt)
7760Sstevel@tonic-gate 		msg(EPOST, "%d error(s)", Error_cnt);
7770Sstevel@tonic-gate 	return (EXIT_CODE);
7780Sstevel@tonic-gate }
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate /*
7810Sstevel@tonic-gate  * add_lnk: Add a linked file's header to the linked file data structure, by
7820Sstevel@tonic-gate  * either adding it to the end of an existing sub-list or starting
7830Sstevel@tonic-gate  * a new sub-list.  Each sub-list saves the links to a given file.
7840Sstevel@tonic-gate  *
7850Sstevel@tonic-gate  * Directly returns a pointer to the new entry; returns a pointer to the head
7860Sstevel@tonic-gate  * of the sub-list in which that entry is located through the argument.
7870Sstevel@tonic-gate  */
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate static struct Lnk *
7900Sstevel@tonic-gate add_lnk(struct Lnk **sublist_return)
7910Sstevel@tonic-gate {
7920Sstevel@tonic-gate 	struct Lnk *new_entry, *sublist;
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	for (sublist = Lnk_hd.L_nxt_p;
7950Sstevel@tonic-gate 	    sublist != &Lnk_hd;
7960Sstevel@tonic-gate 	    sublist = sublist->L_nxt_p) {
7970Sstevel@tonic-gate 		if (sublist->L_gen.g_ino == G_p->g_ino &&
7980Sstevel@tonic-gate 		    sublist->L_gen.g_dev == G_p->g_dev) {
7990Sstevel@tonic-gate 			/* found */
8000Sstevel@tonic-gate 			break;
8010Sstevel@tonic-gate 		}
8020Sstevel@tonic-gate 	}
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	new_entry = e_zalloc(E_EXIT, sizeof (struct Lnk));
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	new_entry->L_lnk_p = NULL;
8070Sstevel@tonic-gate 	new_entry->L_gen = *G_p; /* structure copy */
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	new_entry->L_gen.g_nam_p = e_zalloc(E_EXIT, (size_t)G_p->g_namesz);
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	(void) strcpy(new_entry->L_gen.g_nam_p, G_p->g_nam_p);
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	if (sublist == &Lnk_hd) {
8140Sstevel@tonic-gate 		/* start new sub-list */
8150Sstevel@tonic-gate 		new_entry->L_nxt_p = &Lnk_hd;
8160Sstevel@tonic-gate 		new_entry->L_bck_p = Lnk_hd.L_bck_p;
8170Sstevel@tonic-gate 		Lnk_hd.L_bck_p = new_entry->L_bck_p->L_nxt_p = new_entry;
8180Sstevel@tonic-gate 		new_entry->L_lnk_p = NULL;
8190Sstevel@tonic-gate 		new_entry->L_cnt = 1;
8200Sstevel@tonic-gate 		new_entry->L_data = Onecopy ? 0 : 1;
8210Sstevel@tonic-gate 		sublist = new_entry;
8220Sstevel@tonic-gate 	} else {
8230Sstevel@tonic-gate 		/* add to existing sub-list */
8240Sstevel@tonic-gate 		struct Lnk *ptr;
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 		sublist->L_cnt++;
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 		for (ptr = sublist;
8290Sstevel@tonic-gate 		    ptr->L_lnk_p != NULL;
8300Sstevel@tonic-gate 		    ptr = ptr->L_lnk_p) {
8310Sstevel@tonic-gate 			ptr->L_gen.g_filesz = G_p->g_filesz;
8320Sstevel@tonic-gate 		}
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 		ptr->L_gen.g_filesz = G_p->g_filesz;
8350Sstevel@tonic-gate 		ptr->L_lnk_p = new_entry;
8360Sstevel@tonic-gate 	}
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 	*sublist_return = sublist;
8390Sstevel@tonic-gate 	return (new_entry);
8400Sstevel@tonic-gate }
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate /*
8430Sstevel@tonic-gate  * bfill: Read req_cnt bytes (out of filelen bytes) from the I/O buffer,
8440Sstevel@tonic-gate  * moving them to rd_buf_p.  When there are no bytes left in the I/O buffer,
8450Sstevel@tonic-gate  * Fillbuf is set and the I/O buffer is filled.  The variable dist is the
8460Sstevel@tonic-gate  * distance to lseek if an I/O error is encountered with the -k option set
8470Sstevel@tonic-gate  * (converted to a multiple of Bufsize).
8480Sstevel@tonic-gate  */
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate static int
8510Sstevel@tonic-gate bfill(void)
8520Sstevel@tonic-gate {
8530Sstevel@tonic-gate 	int i = 0, rv;
8540Sstevel@tonic-gate 	static int eof = 0;
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 	if (!Dflag) {
8570Sstevel@tonic-gate 	while ((Buffr.b_end_p - Buffr.b_in_p) >= Bufsize) {
8580Sstevel@tonic-gate 		errno = 0;
8590Sstevel@tonic-gate 		if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0) {
8600Sstevel@tonic-gate 			if (((Buffr.b_end_p - Buffr.b_in_p) >= Bufsize) &&
8610Sstevel@tonic-gate 			    (Eomflag == 0)) {
8620Sstevel@tonic-gate 				Eomflag = 1;
8630Sstevel@tonic-gate 				return (1);
8640Sstevel@tonic-gate 			}
8650Sstevel@tonic-gate 			if (errno == ENOSPC) {
8660Sstevel@tonic-gate 				(void) chgreel(INPUT);
8670Sstevel@tonic-gate 				if (Hdr_type == BAR) {
8680Sstevel@tonic-gate 					skip_bar_volhdr();
8690Sstevel@tonic-gate 				}
8700Sstevel@tonic-gate 				continue;
8710Sstevel@tonic-gate 			} else if (Args & OCk) {
8720Sstevel@tonic-gate 				if (i++ > MX_SEEKS)
8730Sstevel@tonic-gate 					msg(EXT, "Cannot recover.");
8740Sstevel@tonic-gate 				if (lseek(Archive, Bufsize, SEEK_REL) < 0)
8750Sstevel@tonic-gate 					msg(EXTN, "Cannot lseek()");
8760Sstevel@tonic-gate 				Error_cnt++;
8770Sstevel@tonic-gate 				Buf_error++;
8780Sstevel@tonic-gate 				rv = 0;
8790Sstevel@tonic-gate 				continue;
8800Sstevel@tonic-gate 			} else
8810Sstevel@tonic-gate 				ioerror(INPUT);
8820Sstevel@tonic-gate 		} /* (rv = g_read(Device, Archive ... */
8830Sstevel@tonic-gate 		if (Hdr_type != BAR || rv == Bufsize) {
8840Sstevel@tonic-gate 			Buffr.b_in_p += rv;
8850Sstevel@tonic-gate 			Buffr.b_cnt += (long)rv;
8860Sstevel@tonic-gate 		}
8870Sstevel@tonic-gate 		if (rv == Bufsize) {
8880Sstevel@tonic-gate 			eof = 0;
8890Sstevel@tonic-gate 			Blocks++;
8900Sstevel@tonic-gate 		} else if (rv == 0) {
8910Sstevel@tonic-gate 			if (!eof) {
8920Sstevel@tonic-gate 				eof = 1;
8930Sstevel@tonic-gate 				break;
8940Sstevel@tonic-gate 			}
8950Sstevel@tonic-gate 			(void) chgreel(INPUT);
8960Sstevel@tonic-gate 			eof = 0;	/* reset the eof after chgreel	*/
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 			/*
8990Sstevel@tonic-gate 			 * if spans multiple volume, skip the volume header of
9000Sstevel@tonic-gate 			 * the next volume so that the file currently being
9010Sstevel@tonic-gate 			 * extracted can continue to be extracted.
9020Sstevel@tonic-gate 			 */
9030Sstevel@tonic-gate 			if (Hdr_type == BAR) {
9040Sstevel@tonic-gate 				skip_bar_volhdr();
9050Sstevel@tonic-gate 			}
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 			continue;
9080Sstevel@tonic-gate 		} else {
9090Sstevel@tonic-gate 			eof = 0;
9100Sstevel@tonic-gate 			SBlocks += (u_longlong_t)rv;
9110Sstevel@tonic-gate 		}
9120Sstevel@tonic-gate 	} /* (Buffr.b_end_p - Buffr.b_in_p) <= Bufsize */
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 	} else {			/* Dflag */
9150Sstevel@tonic-gate 		errno = 0;
9160Sstevel@tonic-gate 		if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0) {
9170Sstevel@tonic-gate 			return (-1);
9180Sstevel@tonic-gate 		} /* (rv = g_read(Device, Archive ... */
9190Sstevel@tonic-gate 		Buffr.b_in_p += rv;
9200Sstevel@tonic-gate 		Buffr.b_cnt += (long)rv;
9210Sstevel@tonic-gate 		if (rv == Bufsize) {
9220Sstevel@tonic-gate 			eof = 0;
9230Sstevel@tonic-gate 			Blocks++;
9240Sstevel@tonic-gate 		} else if (!rv) {
9250Sstevel@tonic-gate 			if (!eof) {
9260Sstevel@tonic-gate 				eof = 1;
9270Sstevel@tonic-gate 				return (rv);
9280Sstevel@tonic-gate 			}
9290Sstevel@tonic-gate 			return (-1);
9300Sstevel@tonic-gate 		} else {
9310Sstevel@tonic-gate 			eof = 0;
9320Sstevel@tonic-gate 			SBlocks += (u_longlong_t)rv;
9330Sstevel@tonic-gate 		}
9340Sstevel@tonic-gate 	}
9350Sstevel@tonic-gate 	return (rv);
9360Sstevel@tonic-gate }
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate /*
9390Sstevel@tonic-gate  * bflush: Move wr_cnt bytes from data_p into the I/O buffer.  When the
9400Sstevel@tonic-gate  * I/O buffer is full, Flushbuf is set and the buffer is written out.
9410Sstevel@tonic-gate  */
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate static void
9440Sstevel@tonic-gate bflush(void)
9450Sstevel@tonic-gate {
9460Sstevel@tonic-gate 	int rv;
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 	while (Buffr.b_cnt >= Bufsize) {
9490Sstevel@tonic-gate 		errno = 0;
9500Sstevel@tonic-gate 		if ((rv = g_write(Device, Archive, Buffr.b_out_p,
9510Sstevel@tonic-gate 		    Bufsize)) < 0) {
9520Sstevel@tonic-gate 			if (errno == ENOSPC && !Dflag)
9530Sstevel@tonic-gate 				rv = chgreel(OUTPUT);
9540Sstevel@tonic-gate 			else
9550Sstevel@tonic-gate 				ioerror(OUTPUT);
9560Sstevel@tonic-gate 		}
9570Sstevel@tonic-gate 		Buffr.b_out_p += rv;
9580Sstevel@tonic-gate 		Buffr.b_cnt -= (long)rv;
9590Sstevel@tonic-gate 		if (rv == Bufsize)
9600Sstevel@tonic-gate 			Blocks++;
9610Sstevel@tonic-gate 		else if (rv > 0)
9620Sstevel@tonic-gate 			SBlocks += (u_longlong_t)rv;
9630Sstevel@tonic-gate 	}
9640Sstevel@tonic-gate 	rstbuf();
9650Sstevel@tonic-gate }
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate /*
9680Sstevel@tonic-gate  * chgreel: Determine if end-of-medium has been reached.  If it has,
9690Sstevel@tonic-gate  * close the current medium and prompt the user for the next medium.
9700Sstevel@tonic-gate  */
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate static int
9730Sstevel@tonic-gate chgreel(int dir)
9740Sstevel@tonic-gate {
9750Sstevel@tonic-gate 	int lastchar, tryagain, askagain, rv;
9760Sstevel@tonic-gate 	int tmpdev;
9770Sstevel@tonic-gate 	char str[APATH];
9780Sstevel@tonic-gate 	struct stat statb;
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	rv = 0;
9810Sstevel@tonic-gate 	if (fstat(Archive, &statb) < 0)
9820Sstevel@tonic-gate 		msg(EXTN, "Error during stat() of archive");
9830Sstevel@tonic-gate 	if ((statb.st_mode & S_IFMT) != S_IFCHR) {
9840Sstevel@tonic-gate 		if (dir == INPUT) {
9850Sstevel@tonic-gate 			msg(EXT, "%s%s\n",
9861134Sceastha 			    "Can't read input:  end of file encountered ",
9871134Sceastha 			    "prior to expected end of archive.");
9880Sstevel@tonic-gate 		}
9890Sstevel@tonic-gate 	}
9900Sstevel@tonic-gate 	msg(EPOST, "\007End of medium on \"%s\".", dir ? "output" : "input");
9910Sstevel@tonic-gate 	if (is_floppy(Archive))
9920Sstevel@tonic-gate 		(void) ioctl(Archive, FDEJECT, NULL);
9930Sstevel@tonic-gate 	if ((close(Archive) != 0) && (dir == OUTPUT))
9940Sstevel@tonic-gate 		msg(EXTN, "close error");
9950Sstevel@tonic-gate 	Archive = 0;
9960Sstevel@tonic-gate 	Volcnt++;
9970Sstevel@tonic-gate 	for (;;) {
9980Sstevel@tonic-gate 		if (Rtty_p == (FILE *)NULL)
9990Sstevel@tonic-gate 			Rtty_p = fopen(Ttyname, "r");
10000Sstevel@tonic-gate 		do { /* tryagain */
10010Sstevel@tonic-gate 			if (IOfil_p) {
10020Sstevel@tonic-gate 				do {
10030Sstevel@tonic-gate 					msg(EPOST, gettext(Eom_p), Volcnt);
10040Sstevel@tonic-gate 					if (!Rtty_p || fgets(str, sizeof (str),
10050Sstevel@tonic-gate 					    Rtty_p) == (char *)NULL)
10060Sstevel@tonic-gate 						msg(EXT, "Cannot read tty.");
10070Sstevel@tonic-gate 					askagain = 0;
10080Sstevel@tonic-gate 					switch (*str) {
10090Sstevel@tonic-gate 					case '\n':
10100Sstevel@tonic-gate 						(void) strcpy(str, IOfil_p);
10110Sstevel@tonic-gate 						break;
10120Sstevel@tonic-gate 					case 'q':
10130Sstevel@tonic-gate 						exit(EXIT_CODE);
10140Sstevel@tonic-gate 					default:
10150Sstevel@tonic-gate 						askagain = 1;
10160Sstevel@tonic-gate 					}
10170Sstevel@tonic-gate 				} while (askagain);
10180Sstevel@tonic-gate 			} else {
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 				if (Hdr_type == BAR)
10210Sstevel@tonic-gate 					Bar_vol_num++;
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 				msg(EPOST,
10240Sstevel@tonic-gate 				    "To continue, type device/file name when "
10250Sstevel@tonic-gate 				    "ready.");
10260Sstevel@tonic-gate 				if (!Rtty_p || fgets(str, sizeof (str),
10270Sstevel@tonic-gate 				    Rtty_p) == (char *)NULL)
10280Sstevel@tonic-gate 					msg(EXT, "Cannot read tty.");
10290Sstevel@tonic-gate 				lastchar = strlen(str) - 1;
10300Sstevel@tonic-gate 				if (*(str + lastchar) == '\n') /* remove '\n' */
10310Sstevel@tonic-gate 					*(str + lastchar) = '\0';
10320Sstevel@tonic-gate 				if (!*str)
10330Sstevel@tonic-gate 					exit(EXIT_CODE);
10340Sstevel@tonic-gate 			}
10350Sstevel@tonic-gate 			tryagain = 0;
10360Sstevel@tonic-gate 			if ((Archive = open(str, dir)) < 0) {
10370Sstevel@tonic-gate 				msg(ERRN, "Cannot open \"%s\"", str);
10380Sstevel@tonic-gate 				tryagain = 1;
10390Sstevel@tonic-gate 			}
10400Sstevel@tonic-gate 		} while (tryagain);
10410Sstevel@tonic-gate 		(void) g_init(&tmpdev, &Archive);
10420Sstevel@tonic-gate 		if (tmpdev != Device)
10430Sstevel@tonic-gate 			msg(EXT, "Cannot change media types in mid-stream.");
10440Sstevel@tonic-gate 		if (dir == INPUT)
10450Sstevel@tonic-gate 			break;
10460Sstevel@tonic-gate 		else { /* dir == OUTPUT */
10470Sstevel@tonic-gate 			errno = 0;
10480Sstevel@tonic-gate 			if ((rv = g_write(Device, Archive, Buffr.b_out_p,
10490Sstevel@tonic-gate 			    Bufsize)) == Bufsize)
10500Sstevel@tonic-gate 				break;
10510Sstevel@tonic-gate 			else
10520Sstevel@tonic-gate 				msg(ERR,
10530Sstevel@tonic-gate 				    "Unable to write this medium, try "
10540Sstevel@tonic-gate 				    "another.");
10550Sstevel@tonic-gate 		}
10560Sstevel@tonic-gate 	} /* ;; */
10570Sstevel@tonic-gate 	Eomflag = 0;
10580Sstevel@tonic-gate 	return (rv);
10590Sstevel@tonic-gate }
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate /*
10620Sstevel@tonic-gate  * ckname: Check filenames against user specified patterns,
10630Sstevel@tonic-gate  * and/or ask the user for new name when -r is used.
10640Sstevel@tonic-gate  */
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate static int
10670Sstevel@tonic-gate ckname(int flag)
10680Sstevel@tonic-gate {
10690Sstevel@tonic-gate 	int lastchar, len;
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 	if (Hdr_type != TAR && Hdr_type != USTAR && Hdr_type != BAR) {
10720Sstevel@tonic-gate 		/* Re-visit tar size issues later */
10730Sstevel@tonic-gate 		if (G_p->g_namesz - 1 > Max_namesz) {
10740Sstevel@tonic-gate 			msg(ERR, "Name exceeds maximum length - skipped.");
10750Sstevel@tonic-gate 			return (F_SKIP);
10760Sstevel@tonic-gate 		}
10770Sstevel@tonic-gate 	}
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 	if (Pat_pp && !matched())
10800Sstevel@tonic-gate 		return (F_SKIP);
10810Sstevel@tonic-gate 	if ((Args & OCr) && !Adir) { /* rename interactively */
10820Sstevel@tonic-gate 		(void) fprintf(Wtty_p, gettext("Rename \"%s%s%s\"? "),
10830Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
10840Sstevel@tonic-gate 		    G_p->g_nam_p : Renam_p,
10850Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
10860Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
10870Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
10880Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p);
10890Sstevel@tonic-gate 		(void) fflush(Wtty_p);
10900Sstevel@tonic-gate 		if (fgets(Renametmp_p, Max_namesz + 1, Rtty_p) == (char *)NULL)
10910Sstevel@tonic-gate 			msg(EXT, "Cannot read tty.");
10920Sstevel@tonic-gate 		if (feof(Rtty_p))
10930Sstevel@tonic-gate 			exit(EXIT_CODE);
10940Sstevel@tonic-gate 		lastchar = strlen(Renametmp_p) - 1;
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 		/* remove trailing '\n' */
10970Sstevel@tonic-gate 		if (*(Renametmp_p + lastchar) == '\n')
10980Sstevel@tonic-gate 			*(Renametmp_p + lastchar) = '\0';
10990Sstevel@tonic-gate 		if (*Renametmp_p == '\0') {
11000Sstevel@tonic-gate 			msg(POST, "%s%s%s Skipped.",
11010Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
11020Sstevel@tonic-gate 			    G_p->g_nam_p : G_p->g_attrfnam_p,
11030Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
11040Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
11050Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
11060Sstevel@tonic-gate 			    "" : G_p->g_attrnam_p);
11070Sstevel@tonic-gate 			*G_p->g_nam_p = '\0';
11080Sstevel@tonic-gate 			return (F_SKIP);
11090Sstevel@tonic-gate 		} else if (strcmp(Renametmp_p, ".") != 0) {
11100Sstevel@tonic-gate 			len = strlen(Renametmp_p);
11110Sstevel@tonic-gate 			if (len > (int)strlen(G_p->g_nam_p)) {
11120Sstevel@tonic-gate 				if ((G_p->g_nam_p != &nambuf[0]) &&
11130Sstevel@tonic-gate 				    (G_p->g_nam_p != &fullnam[0]))
11140Sstevel@tonic-gate 					free(G_p->g_nam_p);
11150Sstevel@tonic-gate 				G_p->g_nam_p = e_zalloc(E_EXIT, len + 1);
11160Sstevel@tonic-gate 			}
11170Sstevel@tonic-gate 			if (G_p->g_attrnam_p != (char *)NULL) {
11180Sstevel@tonic-gate 				free(G_p->g_attrnam_p);
11190Sstevel@tonic-gate 				G_p->g_attrnam_p = e_strdup(E_EXIT,
11200Sstevel@tonic-gate 				    Renametmp_p);
11210Sstevel@tonic-gate 				(void) strcpy(G_p->g_nam_p, Renam_p);
11220Sstevel@tonic-gate 			} else {
11230Sstevel@tonic-gate 				(void) strcpy(Renam_p, Renametmp_p);
11240Sstevel@tonic-gate 				(void) strcpy(G_p->g_nam_p, Renametmp_p);
11250Sstevel@tonic-gate 			}
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 		}
11280Sstevel@tonic-gate 	}
11290Sstevel@tonic-gate 	if (flag != 0 || Onecopy == 0) {
11300Sstevel@tonic-gate 		VERBOSE((Args & OCt), G_p->g_nam_p);
11310Sstevel@tonic-gate 	}
11320Sstevel@tonic-gate 	if (Args & OCt)
11330Sstevel@tonic-gate 		return (F_SKIP);
11340Sstevel@tonic-gate 	return (F_EXTR);
11350Sstevel@tonic-gate }
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate /*
11380Sstevel@tonic-gate  * ckopts: Check the validity of all command line options.
11390Sstevel@tonic-gate  */
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate static void
11420Sstevel@tonic-gate ckopts(long mask)
11430Sstevel@tonic-gate {
11440Sstevel@tonic-gate 	int oflag;
11450Sstevel@tonic-gate 	char *t_p;
11460Sstevel@tonic-gate 	long errmsk;
11471134Sceastha 	uid_t	Euid = geteuid();	/* Effective uid of invoker */
11481134Sceastha #ifdef SOLARIS_PRIVS
11491134Sceastha 	priv_set_t *privset;
11503919Sceastha 	priv_set_t *zones_privset;
11511134Sceastha #endif	/* SOLARIS_PRIVS */
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 	if (mask & OCi) {
11540Sstevel@tonic-gate 		errmsk = mask & INV_MSK4i;
11550Sstevel@tonic-gate 	} else if (mask & OCo) {
11560Sstevel@tonic-gate 		errmsk = mask & INV_MSK4o;
11570Sstevel@tonic-gate 	} else if (mask & OCp) {
11580Sstevel@tonic-gate 		errmsk = mask & INV_MSK4p;
11590Sstevel@tonic-gate 	} else {
11600Sstevel@tonic-gate 		msg(ERR, "One of -i, -o or -p must be specified.");
11610Sstevel@tonic-gate 		errmsk = 0;
11620Sstevel@tonic-gate 	}
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 	if (errmsk) {
11650Sstevel@tonic-gate 		/* if non-zero, invalid options were specified */
11660Sstevel@tonic-gate 		Error_cnt++;
11670Sstevel@tonic-gate 	}
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 	if ((mask & OCa) && (mask & OCm) && ((mask & OCi) ||
11701134Sceastha 	    (mask & OCo))) {
11710Sstevel@tonic-gate 		msg(ERR, "-a and -m are mutually exclusive.");
11720Sstevel@tonic-gate 	}
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	if ((mask & OCc) && (mask & OCH) && (strcmp("odc", Hdr_p))) {
11750Sstevel@tonic-gate 		msg(ERR, "-c and -H are mutually exclusive.");
11760Sstevel@tonic-gate 	}
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	if ((mask & OCv) && (mask & OCV)) {
11790Sstevel@tonic-gate 		msg(ERR, "-v and -V are mutually exclusive.");
11800Sstevel@tonic-gate 	}
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	if ((mask & OCt) && (mask & OCV)) {
11830Sstevel@tonic-gate 		msg(ERR, "-t and -V are mutually exclusive.");
11840Sstevel@tonic-gate 	}
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 	if ((mask & OCB) && (mask & OCC)) {
11870Sstevel@tonic-gate 		msg(ERR, "-B and -C are mutually exclusive.");
11880Sstevel@tonic-gate 	}
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	if ((mask & OCH) && (mask & OC6)) {
11910Sstevel@tonic-gate 		msg(ERR, "-H and -6 are mutually exclusive.");
11920Sstevel@tonic-gate 	}
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 	if ((mask & OCM) && !((mask & OCI) || (mask & OCO))) {
11950Sstevel@tonic-gate 		msg(ERR, "-M not meaningful without -O or -I.");
11960Sstevel@tonic-gate 	}
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 	if ((mask & OCA) && !(mask & OCO)) {
11990Sstevel@tonic-gate 		msg(ERR, "-A requires the -O option.");
12000Sstevel@tonic-gate 	}
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 	if (Bufsize <= 0) {
12030Sstevel@tonic-gate 		msg(ERR, "Illegal size given for -C option.");
12040Sstevel@tonic-gate 	}
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 	if (mask & OCH) {
12070Sstevel@tonic-gate 		t_p = Hdr_p;
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 		while (*t_p != NULL) {
12100Sstevel@tonic-gate 			if (isupper(*t_p)) {
12110Sstevel@tonic-gate 				*t_p = 'a' + (*t_p - 'A');
12120Sstevel@tonic-gate 			}
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 			t_p++;
12150Sstevel@tonic-gate 		}
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 		if (!(strcmp("odc", Hdr_p))) {
12180Sstevel@tonic-gate 			Hdr_type = CHR;
12190Sstevel@tonic-gate 			Max_namesz = CPATH;
12200Sstevel@tonic-gate 			Onecopy = 0;
12210Sstevel@tonic-gate 			Use_old_stat = 1;
12220Sstevel@tonic-gate 		} else if (!(strcmp("crc", Hdr_p))) {
12230Sstevel@tonic-gate 			Hdr_type = CRC;
12240Sstevel@tonic-gate 			Max_namesz = APATH;
12250Sstevel@tonic-gate 			Onecopy = 1;
12260Sstevel@tonic-gate 		} else if (!(strcmp("tar", Hdr_p))) {
12270Sstevel@tonic-gate 			if (Args & OCo) {
12280Sstevel@tonic-gate 				Hdr_type = USTAR;
12290Sstevel@tonic-gate 				Max_namesz = HNAMLEN - 1;
12300Sstevel@tonic-gate 			} else {
12310Sstevel@tonic-gate 				Hdr_type = TAR;
12320Sstevel@tonic-gate 				Max_namesz = TNAMLEN - 1;
12330Sstevel@tonic-gate 			}
12340Sstevel@tonic-gate 			Onecopy = 0;
12350Sstevel@tonic-gate 		} else if (!(strcmp("ustar", Hdr_p))) {
12360Sstevel@tonic-gate 			Hdr_type = USTAR;
12370Sstevel@tonic-gate 			Max_namesz = HNAMLEN - 1;
12380Sstevel@tonic-gate 			Onecopy = 0;
12390Sstevel@tonic-gate 		} else if (!(strcmp("bar", Hdr_p))) {
12400Sstevel@tonic-gate 			if ((Args & OCo) || (Args & OCp)) {
12410Sstevel@tonic-gate 				msg(ERR,
12420Sstevel@tonic-gate 				    "Header type bar can only be used with -i");
12430Sstevel@tonic-gate 			}
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate 			if (Args & OCP) {
12460Sstevel@tonic-gate 				msg(ERR,
12470Sstevel@tonic-gate 				    "Can't preserve using bar header");
12480Sstevel@tonic-gate 			}
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate 			Hdr_type = BAR;
12510Sstevel@tonic-gate 			Max_namesz = TNAMLEN - 1;
12520Sstevel@tonic-gate 			Onecopy = 0;
12530Sstevel@tonic-gate 		} else {
12540Sstevel@tonic-gate 			msg(ERR, "Invalid header \"%s\" specified", Hdr_p);
12550Sstevel@tonic-gate 		}
12560Sstevel@tonic-gate 	}
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 	if (mask & OCr) {
12590Sstevel@tonic-gate 		Rtty_p = fopen(Ttyname, "r");
12600Sstevel@tonic-gate 		Wtty_p = fopen(Ttyname, "w");
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 		if (Rtty_p == (FILE *)NULL || Wtty_p == (FILE *)NULL) {
12630Sstevel@tonic-gate 			msg(ERR, "Cannot rename, \"%s\" missing", Ttyname);
12640Sstevel@tonic-gate 		}
12650Sstevel@tonic-gate 	}
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	if ((mask & OCE) && (Ef_p = fopen(Efil_p, "r")) == (FILE *)NULL) {
12680Sstevel@tonic-gate 		msg(ERR, "Cannot open \"%s\" to read patterns", Efil_p);
12690Sstevel@tonic-gate 	}
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 	if ((mask & OCI) && (Archive = open(IOfil_p, O_RDONLY)) < 0) {
12720Sstevel@tonic-gate 		msg(ERR, "Cannot open \"%s\" for input", IOfil_p);
12730Sstevel@tonic-gate 	}
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate 	if (mask & OCO) {
12760Sstevel@tonic-gate 		if (mask & OCA) {
12770Sstevel@tonic-gate 			if ((Archive = open(IOfil_p, O_RDWR)) < 0) {
12780Sstevel@tonic-gate 				msg(ERR,
12790Sstevel@tonic-gate 				    "Cannot open \"%s\" for append",
12800Sstevel@tonic-gate 				    IOfil_p);
12810Sstevel@tonic-gate 			}
12820Sstevel@tonic-gate 		} else {
12830Sstevel@tonic-gate 			oflag = (O_WRONLY | O_CREAT | O_TRUNC);
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 			if ((Archive = open(IOfil_p, oflag, 0777)) < 0) {
12860Sstevel@tonic-gate 				msg(ERR,
12870Sstevel@tonic-gate 				    "Cannot open \"%s\" for output",
12880Sstevel@tonic-gate 				    IOfil_p);
12890Sstevel@tonic-gate 			}
12900Sstevel@tonic-gate 		}
12910Sstevel@tonic-gate 	}
12920Sstevel@tonic-gate 
12931134Sceastha #ifdef SOLARIS_PRIVS
12941134Sceastha 	if ((privset = priv_allocset()) == NULL) {
12951134Sceastha 		msg(ERR, "Unable to allocate privilege set");
12961134Sceastha 	} else if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
12971134Sceastha 		msg(ERR, "Unable to obtain privilege set");
12981134Sceastha 	} else {
12993919Sceastha 		zones_privset = priv_str_to_set("zone", "", NULL);
13003919Sceastha 		if (zones_privset != NULL) {
13013919Sceastha 			privileged = (priv_issubset(zones_privset,
13023919Sceastha 			    privset) == B_TRUE);
13033919Sceastha 			priv_freeset(zones_privset);
13043919Sceastha 		} else {
13053919Sceastha 			msg(ERR, "Unable to map privilege to privilege set");
13063919Sceastha 		}
13071134Sceastha 	}
13081134Sceastha 	if (privset != NULL) {
13091134Sceastha 		priv_freeset(privset);
13101134Sceastha 	}
13111134Sceastha #else
13121134Sceastha 	privileged = (Euid == 0);
13131134Sceastha #endif	/* SOLARIS_PRIVS */
13141134Sceastha 
13150Sstevel@tonic-gate 	if (mask & OCR) {
13160Sstevel@tonic-gate 		if ((Rpw_p = getpwnam(Own_p)) == (struct passwd *)NULL) {
13170Sstevel@tonic-gate 			msg(ERR, "\"%s\" is not a valid user id", Own_p);
13181134Sceastha 		} else if ((Euid != Rpw_p->pw_uid) && !privileged) {
13191134Sceastha 			msg(ERR, "R option only valid for super-user or "
13201134Sceastha 			    "id matches login id of user executing cpio");
13210Sstevel@tonic-gate 		}
13220Sstevel@tonic-gate 	}
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate 	if ((mask & OCo) && !(mask & OCO)) {
13250Sstevel@tonic-gate 		Out_p = stderr;
13260Sstevel@tonic-gate 	}
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate 	if ((mask & OCp) && ((mask & (OCB|OCC)) == 0)) {
13290Sstevel@tonic-gate 		/*
13300Sstevel@tonic-gate 		 * We are in pass mode with no block size specified.  Use the
13310Sstevel@tonic-gate 		 * larger of the native page size and 8192.
13320Sstevel@tonic-gate 		 */
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate 		Bufsize = (PageSize > 8192) ? PageSize : 8192;
13350Sstevel@tonic-gate 	}
13360Sstevel@tonic-gate }
13370Sstevel@tonic-gate 
13380Sstevel@tonic-gate /*
13390Sstevel@tonic-gate  * cksum: Calculate the simple checksum of a file (CRC) or header
13400Sstevel@tonic-gate  * (TARTYP (TAR and USTAR)).  For -o and the CRC header, the file is opened and
13410Sstevel@tonic-gate  * the checksum is calculated.  For -i and the CRC header, the checksum
13420Sstevel@tonic-gate  * is calculated as each block is transferred from the archive I/O buffer
13430Sstevel@tonic-gate  * to the file system I/O buffer.  The TARTYP (TAR and USTAR) headers calculate
13440Sstevel@tonic-gate  * the simple checksum of the header (with the checksum field of the
13450Sstevel@tonic-gate  * header initialized to all spaces (\040).
13460Sstevel@tonic-gate  */
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate static long
13490Sstevel@tonic-gate cksum(char hdr, int byt_cnt, int *err)
13500Sstevel@tonic-gate {
13510Sstevel@tonic-gate 	char *crc_p, *end_p;
13520Sstevel@tonic-gate 	int cnt;
13530Sstevel@tonic-gate 	long checksum = 0L, have;
13540Sstevel@tonic-gate 	off_t lcnt;
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 	if (err != NULL)
13570Sstevel@tonic-gate 		*err = 0;
13580Sstevel@tonic-gate 	switch (hdr) {
13590Sstevel@tonic-gate 	case CRC:
13600Sstevel@tonic-gate 		if (Args & OCi) { /* do running checksum */
13610Sstevel@tonic-gate 			end_p = Buffr.b_out_p + byt_cnt;
13620Sstevel@tonic-gate 			for (crc_p = Buffr.b_out_p; crc_p < end_p; crc_p++)
13630Sstevel@tonic-gate 				checksum += (long)*crc_p;
13640Sstevel@tonic-gate 			break;
13650Sstevel@tonic-gate 		}
13660Sstevel@tonic-gate 		/* OCo - do checksum of file */
13670Sstevel@tonic-gate 		lcnt = G_p->g_filesz;
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate 		while (lcnt > 0) {
13700Sstevel@tonic-gate 			have = (lcnt < Bufsize) ? lcnt : Bufsize;
13710Sstevel@tonic-gate 			errno = 0;
13720Sstevel@tonic-gate 			if (read(Ifile, Buf_p, have) != have) {
13730Sstevel@tonic-gate 				msg(ERR, "Error computing checksum.");
13740Sstevel@tonic-gate 				if (err != NULL)
13750Sstevel@tonic-gate 					*err = 1;
13760Sstevel@tonic-gate 				break;
13770Sstevel@tonic-gate 			}
13780Sstevel@tonic-gate 			end_p = Buf_p + have;
13790Sstevel@tonic-gate 			for (crc_p = Buf_p; crc_p < end_p; crc_p++)
13800Sstevel@tonic-gate 				checksum += (long)*crc_p;
13810Sstevel@tonic-gate 			lcnt -= have;
13820Sstevel@tonic-gate 		}
13830Sstevel@tonic-gate 		if (lseek(Ifile, (off_t)0, SEEK_ABS) < 0)
13840Sstevel@tonic-gate 			msg(ERRN, "Cannot reset file after checksum");
13850Sstevel@tonic-gate 		break;
13860Sstevel@tonic-gate 	case TARTYP: /* TAR and USTAR */
13870Sstevel@tonic-gate 		crc_p = Thdr_p->tbuf.t_cksum;
13880Sstevel@tonic-gate 		for (cnt = 0; cnt < TCRCLEN; cnt++) {
13890Sstevel@tonic-gate 			*crc_p = '\040';
13900Sstevel@tonic-gate 			crc_p++;
13910Sstevel@tonic-gate 		}
13920Sstevel@tonic-gate 		crc_p = (char *)Thdr_p;
13930Sstevel@tonic-gate 		for (cnt = 0; cnt < TARSZ; cnt++) {
13940Sstevel@tonic-gate 			/*
13950Sstevel@tonic-gate 			 * tar uses unsigned checksum, so we must use unsigned
13960Sstevel@tonic-gate 			 * here in order to be able to read tar archives.
13970Sstevel@tonic-gate 			 */
13980Sstevel@tonic-gate 			checksum += (long)((unsigned char)(*crc_p));
13990Sstevel@tonic-gate 			crc_p++;
14000Sstevel@tonic-gate 		}
14010Sstevel@tonic-gate 		break;
14020Sstevel@tonic-gate 	default:
14030Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
14040Sstevel@tonic-gate 	} /* hdr */
14050Sstevel@tonic-gate 	return (checksum);
14060Sstevel@tonic-gate }
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate /*
14090Sstevel@tonic-gate  * creat_hdr: Fill in the generic header structure with the specific
14100Sstevel@tonic-gate  *            header information based on the value of Hdr_type.
14110Sstevel@tonic-gate  *
14120Sstevel@tonic-gate  *            return (1) if this process was successful, and (0) otherwise.
14130Sstevel@tonic-gate  */
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate static int
14160Sstevel@tonic-gate creat_hdr(void)
14170Sstevel@tonic-gate {
14180Sstevel@tonic-gate 	ushort_t ftype;
14190Sstevel@tonic-gate 	int fullnamesize;
14200Sstevel@tonic-gate 	dev_t dev;
14210Sstevel@tonic-gate 	ino_t ino;
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 	ftype = SrcSt.st_mode & Ftype;
14240Sstevel@tonic-gate 	Adir = (ftype == S_IFDIR);
14250Sstevel@tonic-gate 	Aspec = (ftype == S_IFBLK || ftype == S_IFCHR || ftype == S_IFIFO);
14260Sstevel@tonic-gate 	switch (Hdr_type) {
14270Sstevel@tonic-gate 		case BIN:
14280Sstevel@tonic-gate 			Gen.g_magic = CMN_BIN;
14290Sstevel@tonic-gate 			break;
14300Sstevel@tonic-gate 		case CHR:
14310Sstevel@tonic-gate 			Gen.g_magic = CMN_BIN;
14320Sstevel@tonic-gate 			break;
14330Sstevel@tonic-gate 		case ASC:
14340Sstevel@tonic-gate 			Gen.g_magic = CMN_ASC;
14350Sstevel@tonic-gate 			break;
14360Sstevel@tonic-gate 		case CRC:
14370Sstevel@tonic-gate 			Gen.g_magic = CMN_CRC;
14380Sstevel@tonic-gate 			break;
14390Sstevel@tonic-gate 		case USTAR:
14400Sstevel@tonic-gate 			/*
14410Sstevel@tonic-gate 			 * If the length of the full name is greater than 256,
14420Sstevel@tonic-gate 			 * print out a message and return.
14430Sstevel@tonic-gate 			 */
14440Sstevel@tonic-gate 			if ((fullnamesize = strlen(Gen.g_nam_p)) > MAXNAM) {
14450Sstevel@tonic-gate 				msg(ERR,
14460Sstevel@tonic-gate 				    "%s: file name too long", Gen.g_nam_p);
14470Sstevel@tonic-gate 				return (0);
14480Sstevel@tonic-gate 			} else if (fullnamesize > NAMSIZ) {
14490Sstevel@tonic-gate 				/*
14500Sstevel@tonic-gate 				 * The length of the full name is greater than
14510Sstevel@tonic-gate 				 * 100, so we must split the filename from the
14520Sstevel@tonic-gate 				 * path
14530Sstevel@tonic-gate 				 */
14540Sstevel@tonic-gate 				char namebuff[NAMSIZ+1];
14550Sstevel@tonic-gate 				char prebuff[PRESIZ+1];
14560Sstevel@tonic-gate 				char *lastslash;
14570Sstevel@tonic-gate 				int presize, namesize;
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate 				(void) memset(namebuff, '\0',
14600Sstevel@tonic-gate 				    sizeof (namebuff));
14610Sstevel@tonic-gate 				(void) memset(prebuff, '\0', sizeof (prebuff));
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate 				lastslash = strrchr(Gen.g_nam_p, '/');
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 				if (lastslash != NULL) {
14660Sstevel@tonic-gate 					namesize = strlen(++lastslash);
14670Sstevel@tonic-gate 					presize = fullnamesize - namesize - 1;
14680Sstevel@tonic-gate 				} else {
14690Sstevel@tonic-gate 					namesize = fullnamesize;
14700Sstevel@tonic-gate 					lastslash = Gen.g_nam_p;
14710Sstevel@tonic-gate 					presize = 0;
14720Sstevel@tonic-gate 				}
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 				/*
14750Sstevel@tonic-gate 				 * If the filename is greater than 100 we can't
14760Sstevel@tonic-gate 				 * archive the file
14770Sstevel@tonic-gate 				 */
14780Sstevel@tonic-gate 				if (namesize > NAMSIZ) {
14790Sstevel@tonic-gate 					msg(ERR,
14800Sstevel@tonic-gate 					    "%s: filename is greater than %d",
14810Sstevel@tonic-gate 					    lastslash, NAMSIZ);
14820Sstevel@tonic-gate 					return (0);
14830Sstevel@tonic-gate 				}
14840Sstevel@tonic-gate 				(void) strncpy(&namebuff[0], lastslash,
14850Sstevel@tonic-gate 				    namesize);
14860Sstevel@tonic-gate 				/*
14870Sstevel@tonic-gate 				 * If the prefix is greater than 155 we can't
14880Sstevel@tonic-gate 				 * archive the file.
14890Sstevel@tonic-gate 				 */
14900Sstevel@tonic-gate 				if (presize > PRESIZ) {
14910Sstevel@tonic-gate 					msg(ERR,
14920Sstevel@tonic-gate 					    "%s: prefix is greater than %d",
14930Sstevel@tonic-gate 					    Gen.g_nam_p, PRESIZ);
14940Sstevel@tonic-gate 					return (0);
14950Sstevel@tonic-gate 				}
14960Sstevel@tonic-gate 				(void) strncpy(&prebuff[0], Gen.g_nam_p,
14970Sstevel@tonic-gate 				    presize);
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 				Gen.g_tname = e_zalloc(E_EXIT, namesize + 1);
15000Sstevel@tonic-gate 				(void) strcpy(Gen.g_tname, namebuff);
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 				Gen.g_prefix = e_zalloc(E_EXIT, presize + 1);
15030Sstevel@tonic-gate 				(void) strcpy(Gen.g_prefix, prebuff);
15040Sstevel@tonic-gate 			} else {
15050Sstevel@tonic-gate 				Gen.g_tname = Gen.g_nam_p;
15060Sstevel@tonic-gate 			}
15070Sstevel@tonic-gate 			(void) strcpy(Gen.g_tmagic, "ustar");
15080Sstevel@tonic-gate 			(void) strcpy(Gen.g_version, "00");
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 			dpasswd = getpwuid(SrcSt.st_uid);
15110Sstevel@tonic-gate 			if (dpasswd == (struct passwd *)NULL) {
15120Sstevel@tonic-gate 				msg(EPOST,
15130Sstevel@tonic-gate 				    "cpio: could not get passwd information "
15140Sstevel@tonic-gate 				    "for %s%s%s",
15150Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
15160Sstevel@tonic-gate 				    Gen.g_nam_p : Gen.g_attrfnam_p,
15170Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
15180Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
15190Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
15200Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
15210Sstevel@tonic-gate 				/* make name null string */
15220Sstevel@tonic-gate 				Gen.g_uname[0] = '\0';
15230Sstevel@tonic-gate 			} else {
15240Sstevel@tonic-gate 				(void) strncpy(&Gen.g_uname[0],
15250Sstevel@tonic-gate 				    dpasswd->pw_name, 32);
15260Sstevel@tonic-gate 			}
15270Sstevel@tonic-gate 			dgroup = getgrgid(SrcSt.st_gid);
15280Sstevel@tonic-gate 			if (dgroup == (struct group *)NULL) {
15290Sstevel@tonic-gate 				msg(EPOST,
15300Sstevel@tonic-gate 				    "cpio: could not get group information "
15310Sstevel@tonic-gate 				    "for %s%s%S",
15320Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
15330Sstevel@tonic-gate 				    Gen.g_nam_p : Gen.g_attrfnam_p,
15340Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
15350Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
15360Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
15370Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
15380Sstevel@tonic-gate 				/* make name null string */
15390Sstevel@tonic-gate 				Gen.g_gname[0] = '\0';
15400Sstevel@tonic-gate 			} else {
15410Sstevel@tonic-gate 				(void) strncpy(&Gen.g_gname[0],
15420Sstevel@tonic-gate 				    dgroup->gr_name, 32);
15430Sstevel@tonic-gate 			}
15440Sstevel@tonic-gate 			Gen.g_typeflag = tartype(ftype);
15450Sstevel@tonic-gate 			/* FALLTHROUGH */
15460Sstevel@tonic-gate 		case TAR:
15470Sstevel@tonic-gate 			(void) memset(T_lname, '\0', sizeof (T_lname));
15480Sstevel@tonic-gate 			break;
15490Sstevel@tonic-gate 		default:
15500Sstevel@tonic-gate 			msg(EXT, "Impossible header type.");
15510Sstevel@tonic-gate 	}
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 	dev = SrcSt.st_dev;
15540Sstevel@tonic-gate 	ino = SrcSt.st_ino;
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate 	if (Use_old_stat) {
15570Sstevel@tonic-gate 		SrcSt = *OldSt;
15580Sstevel@tonic-gate 	}
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate 	Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
15610Sstevel@tonic-gate 	Gen.g_uid = SrcSt.st_uid;
15620Sstevel@tonic-gate 	Gen.g_gid = SrcSt.st_gid;
15630Sstevel@tonic-gate 	Gen.g_dev = SrcSt.st_dev;
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 	if (Use_old_stat) {
15660Sstevel@tonic-gate 		/* -Hodc */
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate 		sl_info_t *p = sl_search(dev, ino);
15690Sstevel@tonic-gate 		Gen.g_ino = p ? p->sl_ino2 : -1;
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate 		if (Gen.g_ino == (ulong_t)-1) {
15720Sstevel@tonic-gate 			msg(ERR, "%s%s%s: cannot be archived - inode too big "
15730Sstevel@tonic-gate 			    "for -Hodc format",
15740Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
15750Sstevel@tonic-gate 			    Gen.g_nam_p : Gen.g_attrfnam_p,
15760Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
15770Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
15780Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
15790Sstevel@tonic-gate 			    "" : Gen.g_attrnam_p);
15800Sstevel@tonic-gate 			return (0);
15810Sstevel@tonic-gate 		}
15820Sstevel@tonic-gate 	} else {
15830Sstevel@tonic-gate 		Gen.g_ino = SrcSt.st_ino;
15840Sstevel@tonic-gate 	}
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 	Gen.g_mode = SrcSt.st_mode;
15870Sstevel@tonic-gate 	Gen.g_mtime = SrcSt.st_mtime;
15880Sstevel@tonic-gate 	Gen.g_nlink = (Adir) ? SrcSt.st_nlink
15890Sstevel@tonic-gate 	    : sl_numlinks(dev, ino);
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate 	if (ftype == S_IFREG || ftype == S_IFLNK)
15920Sstevel@tonic-gate 		Gen.g_filesz = (off_t)SrcSt.st_size;
15930Sstevel@tonic-gate 	else
15940Sstevel@tonic-gate 		Gen.g_filesz = (off_t)0;
15950Sstevel@tonic-gate 	Gen.g_rdev = SrcSt.st_rdev;
15960Sstevel@tonic-gate 	return (1);
15970Sstevel@tonic-gate }
15980Sstevel@tonic-gate 
15990Sstevel@tonic-gate /*
16000Sstevel@tonic-gate  * creat_lnk: Create a link from the existing name1_p to name2_p.
16010Sstevel@tonic-gate  */
16020Sstevel@tonic-gate 
16030Sstevel@tonic-gate static
16040Sstevel@tonic-gate int
16050Sstevel@tonic-gate creat_lnk(int dirfd, char *name1_p, char *name2_p)
16060Sstevel@tonic-gate {
16070Sstevel@tonic-gate 	int cnt = 0;
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 	do {
16100Sstevel@tonic-gate 		errno = 0;
16110Sstevel@tonic-gate 		if (!link(name1_p, name2_p)) {
16120Sstevel@tonic-gate 			if (aclp != NULL) {
1613789Sahrens 				acl_free(aclp);
16140Sstevel@tonic-gate 				aclp = NULL;
1615789Sahrens 				acl_is_set = 0;
16160Sstevel@tonic-gate 			}
16170Sstevel@tonic-gate 			cnt = 0;
16180Sstevel@tonic-gate 			break;
16191134Sceastha 		} else if ((errno == EEXIST) && (cnt == 0)) {
16201134Sceastha 			struct stat lsb1;
16211134Sceastha 			struct stat lsb2;
16221134Sceastha 
16231134Sceastha 			/*
16241134Sceastha 			 * Check to see if we are trying to link this
16251134Sceastha 			 * file to itself.  If so, count the effort as
16261134Sceastha 			 * successful.  If the two files are different,
16271134Sceastha 			 * or if either lstat is unsuccessful, proceed
16281134Sceastha 			 * as we would have otherwise; the appropriate
16291134Sceastha 			 * error will be reported subsequently.
16301134Sceastha 			 */
16311134Sceastha 
16321134Sceastha 			if (lstat(name1_p, &lsb1) != 0) {
16331134Sceastha 				msg(ERR, "Cannot lstat source file %s",
16341134Sceastha 				    name1_p);
16351134Sceastha 			} else {
16361134Sceastha 				if (lstat(name2_p, &lsb2) != 0) {
16371134Sceastha 					msg(ERR, "Cannot lstat "
16381134Sceastha 					    "destination file %s", name2_p);
16390Sstevel@tonic-gate 				} else {
16401134Sceastha 					if (lsb1.st_dev == lsb2.st_dev &&
16411134Sceastha 					    lsb1.st_ino == lsb2.st_ino) {
16421134Sceastha 						VERBOSE((Args & (OCv | OCV)),
16430Sstevel@tonic-gate 						    name2_p);
16441134Sceastha 						return (0);
16450Sstevel@tonic-gate 					}
16460Sstevel@tonic-gate 				}
16470Sstevel@tonic-gate 			}
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate 			if (!(Args & OCu) && G_p->g_mtime <= DesSt.st_mtime)
16501134Sceastha 				msg(ERR, "Existing \"%s\" same age or newer",
16510Sstevel@tonic-gate 				    name2_p);
16520Sstevel@tonic-gate 			else if (unlinkat(dirfd, get_component(name2_p), 0) < 0)
16530Sstevel@tonic-gate 				msg(ERRN, "Error cannot unlink \"%s\"",
16540Sstevel@tonic-gate 				    name2_p);
16550Sstevel@tonic-gate 		}
16560Sstevel@tonic-gate 		cnt++;
16570Sstevel@tonic-gate 	} while ((cnt < 2) && missdir(name2_p) == 0);
16580Sstevel@tonic-gate 	if (!cnt) {
16590Sstevel@tonic-gate 		char *newname;
16600Sstevel@tonic-gate 		char *fromname;
16610Sstevel@tonic-gate 		char *attrname;
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate 		newname = name2_p;
16640Sstevel@tonic-gate 		fromname = name1_p;
16650Sstevel@tonic-gate 		attrname = Gen.g_attrnam_p;
16660Sstevel@tonic-gate 		if (attrname) {
16670Sstevel@tonic-gate 			if (Args & OCp) {
16680Sstevel@tonic-gate 				newname = fromname = Fullnam_p;
16690Sstevel@tonic-gate 			} else {
16700Sstevel@tonic-gate 				newname = Gen.g_attrfnam_p;
16710Sstevel@tonic-gate 			}
16720Sstevel@tonic-gate 		}
16730Sstevel@tonic-gate 		if (Args & OCv) {
16740Sstevel@tonic-gate 			(void) fprintf(Err_p,
16750Sstevel@tonic-gate 			    gettext("%s%s%s linked to %s%s%s\n"), newname,
16760Sstevel@tonic-gate 			    (attrname == (char *)NULL) ?
16770Sstevel@tonic-gate 			    "" : gettext(" attribute "),
16780Sstevel@tonic-gate 			    (attrname == (char *)NULL) ?
16790Sstevel@tonic-gate 			    "" : attrname, fromname,
16800Sstevel@tonic-gate 			    (attrname == (char *)NULL) ?
16810Sstevel@tonic-gate 			    "" : gettext(" attribute "),
16820Sstevel@tonic-gate 			    (attrname == (char *)NULL) ?
16830Sstevel@tonic-gate 			    "" : name1_p);
16840Sstevel@tonic-gate 		}
16850Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), newname);
16860Sstevel@tonic-gate 	} else if (cnt == 1)
16870Sstevel@tonic-gate 		msg(ERRN,
16880Sstevel@tonic-gate 		    "Unable to create directory for \"%s\"", name2_p);
16890Sstevel@tonic-gate 	else if (cnt == 2)
16900Sstevel@tonic-gate 		msg(ERRN,
16910Sstevel@tonic-gate 		    "Cannot link \"%s\" and \"%s\"", name1_p, name2_p);
16920Sstevel@tonic-gate 	return (cnt);
16930Sstevel@tonic-gate }
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate /*
16960Sstevel@tonic-gate  * creat_spec:
16970Sstevel@tonic-gate  *   Create one of the following:
16980Sstevel@tonic-gate  *       directory
16990Sstevel@tonic-gate  *       character special file
17000Sstevel@tonic-gate  *       block special file
17010Sstevel@tonic-gate  *       fifo
17020Sstevel@tonic-gate  */
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate static int
17050Sstevel@tonic-gate creat_spec(int dirfd)
17060Sstevel@tonic-gate {
17070Sstevel@tonic-gate 	char *nam_p;
17080Sstevel@tonic-gate 	int cnt, result, rv = 0;
17090Sstevel@tonic-gate 	char *curdir;
17100Sstevel@tonic-gate 	char *lastslash;
17110Sstevel@tonic-gate 
17120Sstevel@tonic-gate 	Do_rename = 0;	/* creat_tmp() may reset this */
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 	if (Args & OCp) {
17150Sstevel@tonic-gate 		nam_p = Fullnam_p;
17160Sstevel@tonic-gate 	} else {
17170Sstevel@tonic-gate 		nam_p = G_p->g_nam_p;
17180Sstevel@tonic-gate 	}
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate 	/*
17210Sstevel@tonic-gate 	 * Is this the extraction of the hidden attribute directory?
17220Sstevel@tonic-gate 	 * If so then just set the mode/times correctly, and return.
17230Sstevel@tonic-gate 	 */
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate 	if (Hiddendir) {
17261134Sceastha 		if (Args & OCR) {
17271134Sceastha 			if (fchownat(dirfd, ".", Rpw_p->pw_uid,
17281134Sceastha 			    Rpw_p->pw_gid, 0) != 0) {
17291134Sceastha 				msg(ERRN,
17301134Sceastha 				    "Cannot chown() \"attribute directory of "
17311134Sceastha 				    "file %s\"", G_p->g_attrfnam_p);
17321134Sceastha 			}
17331134Sceastha 		} else if ((fchownat(dirfd, ".", G_p->g_uid,
17341134Sceastha 		    G_p->g_gid, 0) != 0) && privileged) {
17350Sstevel@tonic-gate 			msg(ERRN,
17360Sstevel@tonic-gate 			    "Cannot chown() \"attribute directory of "
17370Sstevel@tonic-gate 			    "file %s\"", G_p->g_attrfnam_p);
17380Sstevel@tonic-gate 		}
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate 		if (fchmod(dirfd, G_p->g_mode) != 0) {
17410Sstevel@tonic-gate 			msg(ERRN,
17420Sstevel@tonic-gate 			    "Cannot chmod() \"attribute directory of "
17430Sstevel@tonic-gate 			    "file %s\"", G_p->g_attrfnam_p);
17440Sstevel@tonic-gate 		}
17450Sstevel@tonic-gate 
1746789Sahrens 		acl_is_set = 0;
17470Sstevel@tonic-gate 		if (Pflag && aclp != NULL) {
1748789Sahrens 			if (facl_set(dirfd, aclp) < 0) {
17490Sstevel@tonic-gate 				msg(ERRN,
17500Sstevel@tonic-gate 				    "failed to set acl on attribute"
17510Sstevel@tonic-gate 				    " directory of %s ", G_p->g_attrfnam_p);
17520Sstevel@tonic-gate 			} else {
1753789Sahrens 				acl_is_set = 1;
1754789Sahrens 			}
1755789Sahrens 			acl_free(aclp);
17560Sstevel@tonic-gate 			aclp = NULL;
17570Sstevel@tonic-gate 		}
17580Sstevel@tonic-gate 
17590Sstevel@tonic-gate 		return (1);
17600Sstevel@tonic-gate 	}
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate 	result = stat(nam_p, &DesSt);
17630Sstevel@tonic-gate 
17640Sstevel@tonic-gate 	if (ustar_dir() || Adir) {
17650Sstevel@tonic-gate 		/*
17660Sstevel@tonic-gate 		 *  The archive file is a directory.
17670Sstevel@tonic-gate 		 *  Skip "." and ".."
17680Sstevel@tonic-gate 		 */
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate 		curdir = strrchr(nam_p, '.');
17710Sstevel@tonic-gate 
17720Sstevel@tonic-gate 		if (curdir != NULL && curdir[1] == NULL) {
17730Sstevel@tonic-gate 			lastslash = strrchr(nam_p, '/');
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate 			if (lastslash != NULL) {
17760Sstevel@tonic-gate 				lastslash++;
17770Sstevel@tonic-gate 			} else {
17780Sstevel@tonic-gate 				lastslash = nam_p;
17790Sstevel@tonic-gate 			}
17800Sstevel@tonic-gate 
17810Sstevel@tonic-gate 			if (!(strcmp(lastslash, ".")) ||
17820Sstevel@tonic-gate 			    !(strcmp(lastslash, ".."))) {
17830Sstevel@tonic-gate 				return (1);
17840Sstevel@tonic-gate 			}
17850Sstevel@tonic-gate 		}
17860Sstevel@tonic-gate 
17870Sstevel@tonic-gate 		if (result == 0) {
17880Sstevel@tonic-gate 			/* A file by the same name exists. */
17890Sstevel@tonic-gate 
17900Sstevel@tonic-gate 			/* Take care of ACLs */
1791789Sahrens 			acl_is_set = 0;
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 			if (Pflag && aclp != NULL) {
1794789Sahrens 				if (acl_set(nam_p, aclp) < 0) {
17950Sstevel@tonic-gate 					msg(ERRN,
17960Sstevel@tonic-gate 					    "\"%s\": failed to set acl",
17970Sstevel@tonic-gate 					    nam_p);
17980Sstevel@tonic-gate 				} else {
1799789Sahrens 					acl_is_set = 1;
18000Sstevel@tonic-gate 				}
18010Sstevel@tonic-gate 
1802789Sahrens 				acl_free(aclp);
18030Sstevel@tonic-gate 				aclp = NULL;
18040Sstevel@tonic-gate 			}
18050Sstevel@tonic-gate 			if (Args & OCd) {
18060Sstevel@tonic-gate 				/*
18070Sstevel@tonic-gate 				 * We are creating directories.  Keep the
18080Sstevel@tonic-gate 				 * existing file.
18090Sstevel@tonic-gate 				 */
18100Sstevel@tonic-gate 
18110Sstevel@tonic-gate 				rstfiles(U_KEEP, dirfd);
18120Sstevel@tonic-gate 			}
18130Sstevel@tonic-gate 
18140Sstevel@tonic-gate 			/* Report success. */
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate 			return (1);
18170Sstevel@tonic-gate 		}
18180Sstevel@tonic-gate 	} else {
18190Sstevel@tonic-gate 		/* The archive file is not a directory. */
18200Sstevel@tonic-gate 
18210Sstevel@tonic-gate 		if (result == 0) {
18220Sstevel@tonic-gate 			/*
18230Sstevel@tonic-gate 			 * A file by the same name exists.  Move it to a
18240Sstevel@tonic-gate 			 * temporary file.
18250Sstevel@tonic-gate 			 */
18260Sstevel@tonic-gate 
18270Sstevel@tonic-gate 			if (creat_tmp(nam_p) < 0) {
18280Sstevel@tonic-gate 				/*
18290Sstevel@tonic-gate 				 * We weren't able to create the temp file.
18300Sstevel@tonic-gate 				 * Report failure.
18310Sstevel@tonic-gate 				 */
18320Sstevel@tonic-gate 
18330Sstevel@tonic-gate 				return (0);
18340Sstevel@tonic-gate 			}
18350Sstevel@tonic-gate 		}
18360Sstevel@tonic-gate 	}
18370Sstevel@tonic-gate 
18380Sstevel@tonic-gate 	/*
18390Sstevel@tonic-gate 	 * This pile tries to create the file directly, and, if there is a
18400Sstevel@tonic-gate 	 * problem, creates missing directories, and then tries to create the
18410Sstevel@tonic-gate 	 * file again.  Two strikes and you're out.
18420Sstevel@tonic-gate 	 */
18430Sstevel@tonic-gate 
18440Sstevel@tonic-gate 	cnt = 0;
18450Sstevel@tonic-gate 
18460Sstevel@tonic-gate 	do {
18470Sstevel@tonic-gate 		if (ustar_dir() || Adir) {
18480Sstevel@tonic-gate 			/* The archive file is a directory. */
18490Sstevel@tonic-gate 
18500Sstevel@tonic-gate 			result = mkdir(nam_p, G_p->g_mode);
18510Sstevel@tonic-gate 		} else if (ustar_spec() || Aspec) {
18520Sstevel@tonic-gate 			/*
18530Sstevel@tonic-gate 			 * The archive file is block special,
18540Sstevel@tonic-gate 			 * char special or a fifo.
18550Sstevel@tonic-gate 			 */
18560Sstevel@tonic-gate 
18570Sstevel@tonic-gate 			result = mknod(nam_p, (int)G_p->g_mode,
18580Sstevel@tonic-gate 			    (int)G_p->g_rdev);
18590Sstevel@tonic-gate 		}
18600Sstevel@tonic-gate 
18610Sstevel@tonic-gate 		if (result >= 0) {
18620Sstevel@tonic-gate 			/*
18630Sstevel@tonic-gate 			 * The file creation succeeded.  Take care of the ACLs.
18640Sstevel@tonic-gate 			 */
18650Sstevel@tonic-gate 
1866789Sahrens 			acl_is_set = 0;
18670Sstevel@tonic-gate 
18680Sstevel@tonic-gate 			if (Pflag && aclp != NULL) {
1869789Sahrens 				if (acl_set(nam_p, aclp) < 0) {
18700Sstevel@tonic-gate 					msg(ERRN,
18710Sstevel@tonic-gate 					    "\"%s\": failed to set acl", nam_p);
18720Sstevel@tonic-gate 				} else {
1873789Sahrens 					acl_is_set = 1;
18740Sstevel@tonic-gate 				}
18750Sstevel@tonic-gate 
1876789Sahrens 				acl_free(aclp);
18770Sstevel@tonic-gate 				aclp = NULL;
18780Sstevel@tonic-gate 			}
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate 			cnt = 0;
18810Sstevel@tonic-gate 			break;
18820Sstevel@tonic-gate 		}
18830Sstevel@tonic-gate 
18840Sstevel@tonic-gate 		cnt++;
18850Sstevel@tonic-gate 	} while (cnt < 2 && missdir(nam_p) == 0);
18860Sstevel@tonic-gate 
18870Sstevel@tonic-gate 	switch (cnt) {
18880Sstevel@tonic-gate 	case 0:
18890Sstevel@tonic-gate 		rv = 1;
18900Sstevel@tonic-gate 		rstfiles(U_OVER, dirfd);
18910Sstevel@tonic-gate 		break;
18920Sstevel@tonic-gate 
18930Sstevel@tonic-gate 	case 1:
18940Sstevel@tonic-gate 		msg(ERRN,
18950Sstevel@tonic-gate 		    "Cannot create directory for \"%s\"", nam_p);
18960Sstevel@tonic-gate 
18970Sstevel@tonic-gate 		if (*Over_p == '\0') {
18980Sstevel@tonic-gate 			rstfiles(U_KEEP, dirfd);
18990Sstevel@tonic-gate 		}
19000Sstevel@tonic-gate 
19010Sstevel@tonic-gate 		break;
19020Sstevel@tonic-gate 
19030Sstevel@tonic-gate 	case 2:
19040Sstevel@tonic-gate 		if (ustar_dir() || Adir) {
19050Sstevel@tonic-gate 			msg(ERRN, "Cannot create directory \"%s\"", nam_p);
19060Sstevel@tonic-gate 		} else if (ustar_spec() || Aspec) {
19070Sstevel@tonic-gate 			msg(ERRN, "Cannot mknod() \"%s\"", nam_p);
19080Sstevel@tonic-gate 		}
19090Sstevel@tonic-gate 
19100Sstevel@tonic-gate 		if (*Over_p == '\0') {
19110Sstevel@tonic-gate 			rstfiles(U_KEEP, dirfd);
19120Sstevel@tonic-gate 		}
19130Sstevel@tonic-gate 
19140Sstevel@tonic-gate 		break;
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate 	default:
19170Sstevel@tonic-gate 		msg(EXT, "Impossible case.");
19180Sstevel@tonic-gate 	}
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate 	return (rv);
19210Sstevel@tonic-gate }
19220Sstevel@tonic-gate 
19230Sstevel@tonic-gate /*
19240Sstevel@tonic-gate  * creat_tmp:
19250Sstevel@tonic-gate  */
19260Sstevel@tonic-gate 
19270Sstevel@tonic-gate static int
19280Sstevel@tonic-gate creat_tmp(char *nam_p)
19290Sstevel@tonic-gate {
19300Sstevel@tonic-gate 	char *t_p;
19310Sstevel@tonic-gate 	int	cwd;
19320Sstevel@tonic-gate 
19330Sstevel@tonic-gate 	if ((Args & OCp) && G_p->g_ino == DesSt.st_ino &&
19340Sstevel@tonic-gate 	    G_p->g_dev == DesSt.st_dev) {
19350Sstevel@tonic-gate 		msg(ERR, "Attempt to pass a file to itself.");
19360Sstevel@tonic-gate 		return (-1);
19370Sstevel@tonic-gate 	}
19380Sstevel@tonic-gate 
19390Sstevel@tonic-gate 	if (G_p->g_mtime <= DesSt.st_mtime && !(Args & OCu)) {
19400Sstevel@tonic-gate 		msg(ERR, "Existing \"%s\" same age or newer", nam_p);
19410Sstevel@tonic-gate 		return (-1);
19420Sstevel@tonic-gate 	}
19430Sstevel@tonic-gate 
19440Sstevel@tonic-gate 	/* Make the temporary file name. */
19450Sstevel@tonic-gate 
19460Sstevel@tonic-gate 	(void) strcpy(Over_p, nam_p);
19470Sstevel@tonic-gate 	t_p = Over_p + strlen(Over_p);
19480Sstevel@tonic-gate 
19490Sstevel@tonic-gate 	while (t_p != Over_p) {
19500Sstevel@tonic-gate 		if (*(t_p - 1) == '/')
19510Sstevel@tonic-gate 			break;
19520Sstevel@tonic-gate 		t_p--;
19530Sstevel@tonic-gate 	}
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate 	(void) strcpy(t_p, "XXXXXX");
19560Sstevel@tonic-gate 
19570Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
19580Sstevel@tonic-gate 		/*
19590Sstevel@tonic-gate 		 * Save our current directory, so we can go into
19600Sstevel@tonic-gate 		 * the attribute directory to make the temp file
19610Sstevel@tonic-gate 		 * and then return.
19620Sstevel@tonic-gate 		 */
19630Sstevel@tonic-gate 
19640Sstevel@tonic-gate 		cwd = save_cwd();
19650Sstevel@tonic-gate 		(void) fchdir(G_p->g_dirfd);
19660Sstevel@tonic-gate 	}
19670Sstevel@tonic-gate 
19680Sstevel@tonic-gate 	(void) mktemp(Over_p);
19690Sstevel@tonic-gate 
19700Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
19710Sstevel@tonic-gate 		/* Return to the current directory. */
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate 		rest_cwd(cwd);
19740Sstevel@tonic-gate 	}
19750Sstevel@tonic-gate 
19760Sstevel@tonic-gate 	if (*Over_p == '\0') {
19770Sstevel@tonic-gate 		/* mktemp reports a failure. */
19780Sstevel@tonic-gate 
19790Sstevel@tonic-gate 		msg(ERR, "Cannot get temporary file name.");
19800Sstevel@tonic-gate 		return (-1);
19810Sstevel@tonic-gate 	}
19820Sstevel@tonic-gate 
19830Sstevel@tonic-gate 	/*
19840Sstevel@tonic-gate 	 * If it's a regular file, write to the temporary file, and then rename
19850Sstevel@tonic-gate 	 * in order to accomodate potential executables.
19860Sstevel@tonic-gate 	 *
19870Sstevel@tonic-gate 	 * Note: g_typeflag is only defined (set) for USTAR archive types.  It
19880Sstevel@tonic-gate 	 * defaults to 0 in the cpio-format-regular file case, so this test
19890Sstevel@tonic-gate 	 * succeeds.
19900Sstevel@tonic-gate 	 */
19910Sstevel@tonic-gate 
19920Sstevel@tonic-gate 	if (G_p->g_typeflag == 0 &&
19930Sstevel@tonic-gate 	    (DesSt.st_mode & (ulong_t)Ftype) == S_IFREG &&
19940Sstevel@tonic-gate 	    (G_p->g_mode & (ulong_t)Ftype) == S_IFREG) {
19950Sstevel@tonic-gate 		/*
19960Sstevel@tonic-gate 		 * The archive file and the filesystem file are both regular
19970Sstevel@tonic-gate 		 * files.  We write to the temporary file in this case.
19980Sstevel@tonic-gate 		 */
19990Sstevel@tonic-gate 
20000Sstevel@tonic-gate 		if (Args & OCp) {
20010Sstevel@tonic-gate 			if (G_p->g_attrnam_p == (char *)NULL) {
20020Sstevel@tonic-gate 				Fullnam_p = Over_p;
20030Sstevel@tonic-gate 			} else {
20040Sstevel@tonic-gate 				Attrfile_p = Over_p;
20050Sstevel@tonic-gate 			}
20060Sstevel@tonic-gate 		} else {
20070Sstevel@tonic-gate 			G_p->g_nam_p = Over_p;
20080Sstevel@tonic-gate 			if (G_p->g_attrnam_p != (char *)NULL) {
20090Sstevel@tonic-gate 				Attrfile_p = Over_p;
20100Sstevel@tonic-gate 			}
20110Sstevel@tonic-gate 		}
20120Sstevel@tonic-gate 
20130Sstevel@tonic-gate 		if (G_p->g_attrnam_p == (char *)NULL) {
20140Sstevel@tonic-gate 			Over_p = nam_p;
20150Sstevel@tonic-gate 		} else {
20160Sstevel@tonic-gate 			Over_p = G_p->g_attrnam_p;
20170Sstevel@tonic-gate 		}
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate 		Do_rename = 1;
20200Sstevel@tonic-gate 	} else {
20210Sstevel@tonic-gate 		/*
20220Sstevel@tonic-gate 		 * Either the archive file or the filesystem file is not a
20230Sstevel@tonic-gate 		 * regular file.
20240Sstevel@tonic-gate 		 */
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate 		Do_rename = 0;
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate 		if (S_ISDIR(DesSt.st_mode)) {
20290Sstevel@tonic-gate 			/*
20300Sstevel@tonic-gate 			 * The filesystem file is a directory.
20310Sstevel@tonic-gate 			 *
20320Sstevel@tonic-gate 			 * Save the current working directory because we will
20330Sstevel@tonic-gate 			 * want to restore it back just in case remove_dir()
20340Sstevel@tonic-gate 			 * fails or get confused about where we should be.
20350Sstevel@tonic-gate 			 */
20360Sstevel@tonic-gate 
20370Sstevel@tonic-gate 			*Over_p = '\0';
20380Sstevel@tonic-gate 			cwd = save_cwd();
20390Sstevel@tonic-gate 
20400Sstevel@tonic-gate 			if (remove_dir(nam_p) < 0) {
20410Sstevel@tonic-gate 				msg(ERRN,
20420Sstevel@tonic-gate 				    "Cannot remove the directory \"%s\"",
20430Sstevel@tonic-gate 				    nam_p);
20440Sstevel@tonic-gate 				/*
20450Sstevel@tonic-gate 				 * Restore working directory back to the one
20460Sstevel@tonic-gate 				 * saved earlier.
20470Sstevel@tonic-gate 				 */
20480Sstevel@tonic-gate 
20490Sstevel@tonic-gate 				rest_cwd(cwd);
20500Sstevel@tonic-gate 				return (-1);
20510Sstevel@tonic-gate 			}
20520Sstevel@tonic-gate 
20530Sstevel@tonic-gate 			/*
20540Sstevel@tonic-gate 			 * Restore working directory back to the one
20550Sstevel@tonic-gate 			 * saved earlier
20560Sstevel@tonic-gate 			 */
20570Sstevel@tonic-gate 
20580Sstevel@tonic-gate 			rest_cwd(cwd);
20590Sstevel@tonic-gate 		} else {
20600Sstevel@tonic-gate 			/*
20610Sstevel@tonic-gate 			 * The file is not a directory. Will use the original
20620Sstevel@tonic-gate 			 * link/unlink construct, however, if the file is
20630Sstevel@tonic-gate 			 * namefs, link would fail with EXDEV. Therefore, we
20640Sstevel@tonic-gate 			 * use rename() first to back up the file.
20650Sstevel@tonic-gate 			 */
20660Sstevel@tonic-gate 			if (rename(nam_p, Over_p) < 0) {
20670Sstevel@tonic-gate 				/*
20680Sstevel@tonic-gate 				 * If rename failed, try old construction
20690Sstevel@tonic-gate 				 * method.
20700Sstevel@tonic-gate 				 */
20710Sstevel@tonic-gate 				if (link(nam_p, Over_p) < 0) {
20720Sstevel@tonic-gate 					msg(ERRN,
20730Sstevel@tonic-gate 					    "Cannot create temporary file");
20740Sstevel@tonic-gate 					*Over_p = '\0';
20750Sstevel@tonic-gate 					return (-1);
20760Sstevel@tonic-gate 				}
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate 				if (unlink(nam_p) < 0) {
20790Sstevel@tonic-gate 					msg(ERRN,
20800Sstevel@tonic-gate 					    "Cannot unlink() current \"%s\"",
20810Sstevel@tonic-gate 					    nam_p);
20820Sstevel@tonic-gate 					(void) unlink(Over_p);
20830Sstevel@tonic-gate 					*Over_p = '\0';
20840Sstevel@tonic-gate 					return (-1);
20850Sstevel@tonic-gate 				}
20860Sstevel@tonic-gate 			}
20870Sstevel@tonic-gate 		}
20880Sstevel@tonic-gate 	}
20890Sstevel@tonic-gate 
20900Sstevel@tonic-gate 	return (1);
20910Sstevel@tonic-gate }
20920Sstevel@tonic-gate 
20930Sstevel@tonic-gate /*
20940Sstevel@tonic-gate  * data_in:  If proc_mode == P_PROC, bread() the file's data from the archive
20950Sstevel@tonic-gate  * and write(2) it to the open fdes gotten from openout().  If proc_mode ==
20960Sstevel@tonic-gate  * P_SKIP, or becomes P_SKIP (due to errors etc), bread(2) the file's data
20970Sstevel@tonic-gate  * and ignore it.  If the user specified any of the "swap" options (b, s or S),
20980Sstevel@tonic-gate  * and the length of the file is not appropriate for that action, do not
20990Sstevel@tonic-gate  * perform the "swap", otherwise perform the action on a buffer by buffer basis.
21000Sstevel@tonic-gate  * If the CRC header was selected, calculate a running checksum as each buffer
21010Sstevel@tonic-gate  * is processed.
21020Sstevel@tonic-gate  */
21030Sstevel@tonic-gate 
21040Sstevel@tonic-gate static void
21050Sstevel@tonic-gate data_in(int proc_mode)
21060Sstevel@tonic-gate {
21070Sstevel@tonic-gate 	char *nam_p;
21080Sstevel@tonic-gate 	int cnt, pad;
21090Sstevel@tonic-gate 	long cksumval = 0L;
21100Sstevel@tonic-gate 	off_t filesz;
21110Sstevel@tonic-gate 	int rv, swapfile = 0;
21120Sstevel@tonic-gate 	int compress_flag = 0;	/* if the file is compressed */
21130Sstevel@tonic-gate 	int cstatus = 0;
21140Sstevel@tonic-gate 	FILE *pipef;		/* pipe for bar to do de-compression */
21150Sstevel@tonic-gate 
21160Sstevel@tonic-gate 
21170Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
21180Sstevel@tonic-gate 		nam_p = G_p->g_attrnam_p;
21190Sstevel@tonic-gate 	} else {
21200Sstevel@tonic-gate 		nam_p = G_p->g_nam_p;
21210Sstevel@tonic-gate 	}
21220Sstevel@tonic-gate 
21230Sstevel@tonic-gate 
21240Sstevel@tonic-gate 	if (((G_p->g_mode & Ftype) == S_IFLNK && proc_mode != P_SKIP) ||
21250Sstevel@tonic-gate 	    (Hdr_type == BAR && bar_linkflag == '2' && proc_mode != P_SKIP)) {
21260Sstevel@tonic-gate 		proc_mode = P_SKIP;
21270Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
21280Sstevel@tonic-gate 	}
21290Sstevel@tonic-gate 	if (Args & (OCb | OCs | OCS)) { /* verfify that swapping is possible */
21300Sstevel@tonic-gate 		swapfile = 1;
21310Sstevel@tonic-gate 		if (Args & (OCs | OCb) && G_p->g_filesz % 2) {
21320Sstevel@tonic-gate 			msg(ERR,
21330Sstevel@tonic-gate 			    "Cannot swap bytes of \"%s\", odd number of bytes",
21340Sstevel@tonic-gate 			    nam_p);
21350Sstevel@tonic-gate 			swapfile = 0;
21360Sstevel@tonic-gate 		}
21370Sstevel@tonic-gate 		if (Args & (OCS | OCb) && G_p->g_filesz % 4) {
21380Sstevel@tonic-gate 			msg(ERR,
21390Sstevel@tonic-gate 			    "Cannot swap halfwords of \"%s\", odd number "
21400Sstevel@tonic-gate 			    "of halfwords", nam_p);
21410Sstevel@tonic-gate 			swapfile = 0;
21420Sstevel@tonic-gate 		}
21430Sstevel@tonic-gate 	}
21440Sstevel@tonic-gate 	filesz = G_p->g_filesz;
21450Sstevel@tonic-gate 
21460Sstevel@tonic-gate 	/* This writes out the file from the archive */
21470Sstevel@tonic-gate 
21480Sstevel@tonic-gate 	while (filesz > 0) {
21490Sstevel@tonic-gate 		cnt = (int)(filesz > CPIOBSZ) ? CPIOBSZ : filesz;
21500Sstevel@tonic-gate 		FILL(cnt);
21510Sstevel@tonic-gate 		if (proc_mode != P_SKIP) {
21520Sstevel@tonic-gate 			if (Hdr_type == CRC)
21530Sstevel@tonic-gate 				cksumval += cksum(CRC, cnt, NULL);
21540Sstevel@tonic-gate 			if (swapfile)
21550Sstevel@tonic-gate 				swap(Buffr.b_out_p, cnt);
21560Sstevel@tonic-gate 			errno = 0;
21570Sstevel@tonic-gate 
21580Sstevel@tonic-gate 			/*
21590Sstevel@tonic-gate 			 * if the bar archive is compressed, set up a pipe and
21600Sstevel@tonic-gate 			 * do the de-compression while reading in the file
21610Sstevel@tonic-gate 			 */
21620Sstevel@tonic-gate 			if (Hdr_type == BAR) {
21630Sstevel@tonic-gate 				if (compress_flag == 0 && Compressed) {
21640Sstevel@tonic-gate 					setup_uncompress(&pipef);
21650Sstevel@tonic-gate 					compress_flag++;
21660Sstevel@tonic-gate 				}
21670Sstevel@tonic-gate 
21680Sstevel@tonic-gate 			}
21690Sstevel@tonic-gate 
21700Sstevel@tonic-gate 			rv = write(Ofile, Buffr.b_out_p, cnt);
21710Sstevel@tonic-gate 			if (rv < cnt) {
21720Sstevel@tonic-gate 				if (rv < 0)
21730Sstevel@tonic-gate 					msg(ERRN, "Cannot write \"%s\"", nam_p);
21740Sstevel@tonic-gate 				else
21750Sstevel@tonic-gate 					msg(EXTN, "Cannot write \"%s\"", nam_p);
21760Sstevel@tonic-gate 				proc_mode = P_SKIP;
21770Sstevel@tonic-gate 				rstfiles(U_KEEP, G_p->g_dirfd);
21780Sstevel@tonic-gate 				cstatus = close(Ofile);
21790Sstevel@tonic-gate 				Ofile = 0;
21800Sstevel@tonic-gate 				if (cstatus != 0) {
21810Sstevel@tonic-gate 					msg(EXTN, "close error");
21820Sstevel@tonic-gate 				}
21830Sstevel@tonic-gate 			}
21840Sstevel@tonic-gate 		}
21850Sstevel@tonic-gate 		Buffr.b_out_p += cnt;
21860Sstevel@tonic-gate 		Buffr.b_cnt -= (long)cnt;
21870Sstevel@tonic-gate 		filesz -= (off_t)cnt;
21880Sstevel@tonic-gate 	} /* filesz */
21890Sstevel@tonic-gate 
21900Sstevel@tonic-gate 	pad = (Pad_val + 1 - (G_p->g_filesz & Pad_val)) & Pad_val;
21910Sstevel@tonic-gate 	if (pad != 0) {
21920Sstevel@tonic-gate 		FILL(pad);
21930Sstevel@tonic-gate 		Buffr.b_out_p += pad;
21940Sstevel@tonic-gate 		Buffr.b_cnt -= pad;
21950Sstevel@tonic-gate 	}
21960Sstevel@tonic-gate 	if (proc_mode != P_SKIP) {
21970Sstevel@tonic-gate 		if (Hdr_type == CRC && Gen.g_cksum != cksumval) {
21980Sstevel@tonic-gate 			msg(ERR, "\"%s\" - checksum error", nam_p);
21990Sstevel@tonic-gate 			rstfiles(U_KEEP, G_p->g_dirfd);
22000Sstevel@tonic-gate 		} else
22010Sstevel@tonic-gate 			rstfiles(U_OVER, G_p->g_dirfd);
22020Sstevel@tonic-gate 		if (Hdr_type == BAR && compress_flag) {
22030Sstevel@tonic-gate 			(void) pclose(pipef);
22040Sstevel@tonic-gate 		} else {
22050Sstevel@tonic-gate 			cstatus = close(Ofile);
22060Sstevel@tonic-gate 		}
22070Sstevel@tonic-gate 		Ofile = 0;
22080Sstevel@tonic-gate 		if (cstatus != 0) {
22090Sstevel@tonic-gate 			msg(EXTN, "close error");
22100Sstevel@tonic-gate 		}
22110Sstevel@tonic-gate 	}
22120Sstevel@tonic-gate 	VERBOSE((proc_mode != P_SKIP && (Args & (OCv | OCV))), G_p->g_nam_p);
22130Sstevel@tonic-gate 	Finished = 1;
22140Sstevel@tonic-gate }
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate /*
22170Sstevel@tonic-gate  * data_out:  open(2) the file to be archived, compute the checksum
22180Sstevel@tonic-gate  * of it's data if the CRC header was specified and write the header.
22190Sstevel@tonic-gate  * read(2) each block of data and bwrite() it to the archive.  For TARTYP (TAR
22200Sstevel@tonic-gate  * and USTAR) archives, pad the data with NULLs to the next 512 byte boundary.
22210Sstevel@tonic-gate  */
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate static void
22240Sstevel@tonic-gate data_out(void)
22250Sstevel@tonic-gate {
22260Sstevel@tonic-gate 	char *nam_p;
22270Sstevel@tonic-gate 	int cnt, amount_read, pad;
22280Sstevel@tonic-gate 	off_t amt_to_read, real_filesz;
22290Sstevel@tonic-gate 	int	errret = 0;
22300Sstevel@tonic-gate 
22310Sstevel@tonic-gate 	nam_p = G_p->g_nam_p;
22320Sstevel@tonic-gate 	if (Aspec) {
22330Sstevel@tonic-gate 		if (Pflag && aclp != NULL) {
22340Sstevel@tonic-gate 			char    *secinfo = NULL;
22350Sstevel@tonic-gate 			int	len = 0;
22360Sstevel@tonic-gate 
22370Sstevel@tonic-gate 			/* append security attributes */
2238789Sahrens 			if (append_secattr(&secinfo, &len, aclp) == -1) {
22390Sstevel@tonic-gate 				msg(ERR,
22400Sstevel@tonic-gate 				    "can create security information");
22410Sstevel@tonic-gate 			}
22420Sstevel@tonic-gate 			/* call append_secattr() if more than one */
22430Sstevel@tonic-gate 
22440Sstevel@tonic-gate 			if (len > 0) {
22450Sstevel@tonic-gate 			/* write ancillary only if there is sec info */
22460Sstevel@tonic-gate 				(void) write_hdr(ARCHIVE_ACL, (off_t)len);
22470Sstevel@tonic-gate 				(void) write_ancillary(secinfo, len);
22480Sstevel@tonic-gate 			}
22490Sstevel@tonic-gate 		}
22500Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
22510Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_dirfd);
22520Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
22530Sstevel@tonic-gate 		return;
22540Sstevel@tonic-gate 	}
22550Sstevel@tonic-gate 	if ((G_p->g_mode & Ftype) == S_IFLNK && (Hdr_type !=
22560Sstevel@tonic-gate 	    USTAR && Hdr_type != TAR)) { /* symbolic link */
22570Sstevel@tonic-gate 		int size;
22580Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
22590Sstevel@tonic-gate 
22600Sstevel@tonic-gate 		FLUSH(G_p->g_filesz);
22610Sstevel@tonic-gate 		errno = 0;
22620Sstevel@tonic-gate 
22630Sstevel@tonic-gate 		/* Note that "size" and G_p->g_filesz are the same number */
22640Sstevel@tonic-gate 
22650Sstevel@tonic-gate 		if ((size = readlink(nam_p, Buffr.b_in_p, G_p->g_filesz)) <
22660Sstevel@tonic-gate 		    0) {
22670Sstevel@tonic-gate 			msg(ERRN, "Cannot read symbolic link \"%s\"", nam_p);
22680Sstevel@tonic-gate 			return;
22690Sstevel@tonic-gate 		}
22700Sstevel@tonic-gate 
22710Sstevel@tonic-gate 		/*
22720Sstevel@tonic-gate 		 * Note that it is OK not to add the NUL after the name read by
22730Sstevel@tonic-gate 		 * readlink, because it is not being used subsequently.
22740Sstevel@tonic-gate 		 */
22750Sstevel@tonic-gate 
22760Sstevel@tonic-gate 		Buffr.b_in_p += size;
22770Sstevel@tonic-gate 		Buffr.b_cnt += size;
22780Sstevel@tonic-gate 		pad = (Pad_val + 1 - (size & Pad_val)) & Pad_val;
22790Sstevel@tonic-gate 		if (pad != 0) {
22800Sstevel@tonic-gate 			FLUSH(pad);
22810Sstevel@tonic-gate 			(void) memcpy(Buffr.b_in_p, Empty, pad);
22820Sstevel@tonic-gate 			Buffr.b_in_p += pad;
22830Sstevel@tonic-gate 			Buffr.b_cnt += pad;
22840Sstevel@tonic-gate 		}
22850Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
22860Sstevel@tonic-gate 		return;
22870Sstevel@tonic-gate 	} else if ((G_p->g_mode & Ftype) == S_IFLNK &&
22881134Sceastha 	    (Hdr_type == USTAR || Hdr_type == TAR)) {
22890Sstevel@tonic-gate 		int size;
22900Sstevel@tonic-gate 
22910Sstevel@tonic-gate 		/*
22920Sstevel@tonic-gate 		 * G_p->g_filesz is the length of the right-hand side of
22930Sstevel@tonic-gate 		 * the symlink "x -> y".
22940Sstevel@tonic-gate 		 * The tar link field is only NAMSIZ long.
22950Sstevel@tonic-gate 		 */
22960Sstevel@tonic-gate 
22970Sstevel@tonic-gate 		if (G_p->g_filesz > NAMSIZ) {
22980Sstevel@tonic-gate 			msg(ERRN,
22990Sstevel@tonic-gate 			    "Symbolic link too long \"%s\"", nam_p);
23000Sstevel@tonic-gate 			return;
23010Sstevel@tonic-gate 		}
23020Sstevel@tonic-gate 		if ((size = readlink(nam_p, T_lname, G_p->g_filesz)) < 0) {
23030Sstevel@tonic-gate 			msg(ERRN,
23040Sstevel@tonic-gate 			    "Cannot read symbolic link \"%s\"", nam_p);
23050Sstevel@tonic-gate 			return;
23060Sstevel@tonic-gate 		}
23070Sstevel@tonic-gate 		T_lname[size] = '\0';
23080Sstevel@tonic-gate 		G_p->g_filesz = (off_t)0;
23090Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
23100Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
23110Sstevel@tonic-gate 		return;
23120Sstevel@tonic-gate 	}
23130Sstevel@tonic-gate 	if ((Ifile = openfile(O_RDONLY)) < 0) {
23140Sstevel@tonic-gate 		msg(ERR, "\"%s%s%s\" ?",
23150Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
23160Sstevel@tonic-gate 		    nam_p : Gen.g_attrfnam_p,
23170Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
23180Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
23190Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
23200Sstevel@tonic-gate 		    "" : Gen.g_attrnam_p);
23210Sstevel@tonic-gate 		return;
23220Sstevel@tonic-gate 	}
23230Sstevel@tonic-gate 
23240Sstevel@tonic-gate 	/*
23250Sstevel@tonic-gate 	 * Dump extended attribute header.
23260Sstevel@tonic-gate 	 */
23270Sstevel@tonic-gate 
23280Sstevel@tonic-gate 	if (Gen.g_attrnam_p != (char *)NULL) {
23290Sstevel@tonic-gate 		write_xattr_hdr();
23300Sstevel@tonic-gate 	}
23310Sstevel@tonic-gate 
23320Sstevel@tonic-gate 	if (Hdr_type == CRC) {
23330Sstevel@tonic-gate 		long csum = cksum(CRC, 0, &errret);
23340Sstevel@tonic-gate 		if (errret != 0) {
23350Sstevel@tonic-gate 			G_p->g_cksum = (ulong_t)-1;
23360Sstevel@tonic-gate 			msg(POST, "\"%s%s%s\" skipped",
23370Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
23380Sstevel@tonic-gate 			    nam_p : Gen.g_attrfnam_p,
23390Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
23400Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
23410Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
23420Sstevel@tonic-gate 			    "" : nam_p);
23430Sstevel@tonic-gate 			(void) close(Ifile);
23440Sstevel@tonic-gate 			return;
23450Sstevel@tonic-gate 		}
23460Sstevel@tonic-gate 		G_p->g_cksum = csum;
23470Sstevel@tonic-gate 	} else {
23480Sstevel@tonic-gate 		G_p->g_cksum = 0;
23490Sstevel@tonic-gate 	}
23500Sstevel@tonic-gate 
23510Sstevel@tonic-gate 	/*
23520Sstevel@tonic-gate 	 * ACL has been retrieved in getname().
23530Sstevel@tonic-gate 	 */
23540Sstevel@tonic-gate 	if (Pflag) {
23550Sstevel@tonic-gate 		char    *secinfo = NULL;
23560Sstevel@tonic-gate 		int	len = 0;
23570Sstevel@tonic-gate 
23580Sstevel@tonic-gate 		/* append security attributes */
2359789Sahrens 		if ((append_secattr(&secinfo, &len, aclp)) == -1)
23600Sstevel@tonic-gate 			msg(ERR, "can create security information");
23610Sstevel@tonic-gate 
23620Sstevel@tonic-gate 		/* call append_secattr() if more than one */
23630Sstevel@tonic-gate 
23640Sstevel@tonic-gate 		if (len > 0) {
23650Sstevel@tonic-gate 		/* write ancillary only if there is sec info */
23660Sstevel@tonic-gate 			(void) write_hdr(ARCHIVE_ACL, (off_t)len);
23670Sstevel@tonic-gate 			(void) write_ancillary(secinfo, len);
23680Sstevel@tonic-gate 		}
23690Sstevel@tonic-gate 	}
23700Sstevel@tonic-gate 
23710Sstevel@tonic-gate 	write_hdr(ARCHIVE_NORMAL, (off_t)0);
23720Sstevel@tonic-gate 
23730Sstevel@tonic-gate 	real_filesz = 0;
23740Sstevel@tonic-gate 
23750Sstevel@tonic-gate 	for (amt_to_read = G_p->g_filesz;
23760Sstevel@tonic-gate 	    amt_to_read > 0;
23770Sstevel@tonic-gate 	    amt_to_read -= (off_t)amount_read) {
23780Sstevel@tonic-gate 		FLUSH(CPIOBSZ);
23790Sstevel@tonic-gate 		errno = 0;
23800Sstevel@tonic-gate 
23810Sstevel@tonic-gate 		if ((amount_read = read(Ifile, Buffr.b_in_p, CPIOBSZ)) < 0) {
23820Sstevel@tonic-gate 			msg(EXTN, "Cannot read \"%s%s%s\"",
23830Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
23840Sstevel@tonic-gate 			    nam_p : Gen.g_attrfnam_p,
23850Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
23860Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
23870Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
23880Sstevel@tonic-gate 			    "" : nam_p);
23890Sstevel@tonic-gate 			break;
23900Sstevel@tonic-gate 		}
23910Sstevel@tonic-gate 
23920Sstevel@tonic-gate 		if (amount_read == 0) {
23930Sstevel@tonic-gate 			/* the file has shrunk */
23940Sstevel@tonic-gate 			real_filesz = G_p->g_filesz - amt_to_read;
23950Sstevel@tonic-gate 			break;
23960Sstevel@tonic-gate 		} else if (amount_read > amt_to_read) {
23970Sstevel@tonic-gate 			/* the file has grown */
23980Sstevel@tonic-gate 			real_filesz = G_p->g_filesz +
23991134Sceastha 			    (amount_read - amt_to_read);
24000Sstevel@tonic-gate 			amount_read = amt_to_read;
24010Sstevel@tonic-gate 		} else if (amount_read == amt_to_read) {
24020Sstevel@tonic-gate 			/* the file is the same size */
24030Sstevel@tonic-gate 			real_filesz = G_p->g_filesz;
24040Sstevel@tonic-gate 		}
24050Sstevel@tonic-gate 
24060Sstevel@tonic-gate 		Buffr.b_in_p += amount_read;
24070Sstevel@tonic-gate 		Buffr.b_cnt += (long)amount_read;
24080Sstevel@tonic-gate 	}
24090Sstevel@tonic-gate 
24100Sstevel@tonic-gate 	while (amt_to_read > 0) {
24110Sstevel@tonic-gate 		cnt = (amt_to_read > CPIOBSZ) ? CPIOBSZ : (int)amt_to_read;
24120Sstevel@tonic-gate 		FLUSH(cnt);
24130Sstevel@tonic-gate 		(void) memset(Buffr.b_in_p, NULL, cnt);
24140Sstevel@tonic-gate 		Buffr.b_in_p += cnt;
24150Sstevel@tonic-gate 		Buffr.b_cnt += cnt;
24160Sstevel@tonic-gate 		amt_to_read -= cnt;
24170Sstevel@tonic-gate 	}
24180Sstevel@tonic-gate 
24190Sstevel@tonic-gate 	pad = (Pad_val + 1 - (G_p->g_filesz & Pad_val)) & Pad_val;
24200Sstevel@tonic-gate 	if (pad != 0) {
24210Sstevel@tonic-gate 		FLUSH(pad);
24220Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, Empty, pad);
24230Sstevel@tonic-gate 		Buffr.b_in_p += pad;
24240Sstevel@tonic-gate 		Buffr.b_cnt += pad;
24250Sstevel@tonic-gate 	}
24260Sstevel@tonic-gate 
24270Sstevel@tonic-gate 	if (real_filesz > G_p->g_filesz) {
24280Sstevel@tonic-gate 		msg(ERR, "File size of \"%s%s%s\" has increased by %lld",
24290Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
24300Sstevel@tonic-gate 		    G_p->g_nam_p : Gen.g_attrfnam_p,
24310Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
24320Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
24330Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
24340Sstevel@tonic-gate 		    "" : G_p->g_nam_p,
24350Sstevel@tonic-gate 		    (real_filesz - G_p->g_filesz));
24360Sstevel@tonic-gate 	}
24370Sstevel@tonic-gate 	if (real_filesz < G_p->g_filesz) {
24380Sstevel@tonic-gate 		msg(ERR, "File size of \"%s%s%s\" has decreased by %lld",
24390Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
24400Sstevel@tonic-gate 		    G_p->g_nam_p : Gen.g_attrfnam_p,
24410Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
24420Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
24430Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
24440Sstevel@tonic-gate 		    "" : G_p->g_nam_p,
24450Sstevel@tonic-gate 		    (G_p->g_filesz - real_filesz));
24460Sstevel@tonic-gate 	}
24470Sstevel@tonic-gate 
24480Sstevel@tonic-gate 	(void) close(Ifile);
24490Sstevel@tonic-gate 	rstfiles(U_KEEP, G_p->g_dirfd);
24500Sstevel@tonic-gate 	VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
24510Sstevel@tonic-gate }
24520Sstevel@tonic-gate 
24530Sstevel@tonic-gate /*
24540Sstevel@tonic-gate  * data_pass:  If not a special file (Aspec), open(2) the file to be
24550Sstevel@tonic-gate  * transferred, read(2) each block of data and write(2) it to the output file
24560Sstevel@tonic-gate  * Ofile, which was opened in file_pass().
24570Sstevel@tonic-gate  */
24580Sstevel@tonic-gate 
24590Sstevel@tonic-gate static void
24600Sstevel@tonic-gate data_pass(void)
24610Sstevel@tonic-gate {
24620Sstevel@tonic-gate 	int cnt, done = 1;
24630Sstevel@tonic-gate 	int cstatus;
24640Sstevel@tonic-gate 	off_t filesz;
24650Sstevel@tonic-gate 	char *namep = Nam_p;
24660Sstevel@tonic-gate 
24670Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
24680Sstevel@tonic-gate 		namep = G_p->g_attrnam_p;
24690Sstevel@tonic-gate 	}
24700Sstevel@tonic-gate 	if (Aspec) {
24710Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_passdirfd);
24720Sstevel@tonic-gate 		cstatus = close(Ofile);
24730Sstevel@tonic-gate 		Ofile = 0;
24740Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), Nam_p);
24750Sstevel@tonic-gate 		if (cstatus != 0) {
24760Sstevel@tonic-gate 			msg(EXTN, "close error");
24770Sstevel@tonic-gate 		}
24780Sstevel@tonic-gate 		return;
24790Sstevel@tonic-gate 	}
24800Sstevel@tonic-gate 	if ((Ifile = openat(G_p->g_dirfd, get_component(namep), 0)) < 0) {
24810Sstevel@tonic-gate 		msg(ERRN, "Cannot open \"%s%s%s\", skipped",
24820Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
24830Sstevel@tonic-gate 		    Nam_p : G_p->g_attrfnam_p,
24840Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
24850Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
24860Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
24870Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p);
24880Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_passdirfd);
24890Sstevel@tonic-gate 		cstatus = close(Ofile);
24900Sstevel@tonic-gate 		Ofile = 0;
24910Sstevel@tonic-gate 		if (cstatus != 0) {
24920Sstevel@tonic-gate 			msg(EXTN, "close error");
24930Sstevel@tonic-gate 		}
24940Sstevel@tonic-gate 		return;
24950Sstevel@tonic-gate 	}
24960Sstevel@tonic-gate 	filesz = G_p->g_filesz;
24970Sstevel@tonic-gate 
24980Sstevel@tonic-gate 	while (filesz > 0) {
24990Sstevel@tonic-gate 		cnt = (unsigned)(filesz > Bufsize) ? Bufsize : filesz;
25000Sstevel@tonic-gate 		errno = 0;
25010Sstevel@tonic-gate 		if (read(Ifile, Buf_p, (unsigned)cnt) < 0) {
25020Sstevel@tonic-gate 			msg(ERRN, "Cannot read \"%s%s%s\"",
25030Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
25040Sstevel@tonic-gate 			    Nam_p : G_p->g_attrfnam_p,
25050Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
25060Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
25070Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
25080Sstevel@tonic-gate 			    "" : G_p->g_attrnam_p);
25090Sstevel@tonic-gate 			done = 0;
25100Sstevel@tonic-gate 			break;
25110Sstevel@tonic-gate 		}
25120Sstevel@tonic-gate 		errno = 0;
25130Sstevel@tonic-gate 		if (write(Ofile, Buf_p, (unsigned)cnt) < 0) {
25140Sstevel@tonic-gate 			if (Do_rename) {
25150Sstevel@tonic-gate 				msg(ERRN, "Cannot write \"%s%s%s\"", Over_p,
25160Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
25170Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
25180Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
25190Sstevel@tonic-gate 				    "" : Over_p);
25200Sstevel@tonic-gate 			} else {
25210Sstevel@tonic-gate 				msg(ERRN, "Cannot write \"%s%s%s\"",
25220Sstevel@tonic-gate 				    Fullnam_p,
25230Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
25240Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
25250Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
25260Sstevel@tonic-gate 				    "" : G_p->g_attrnam_p);
25270Sstevel@tonic-gate 			}
25280Sstevel@tonic-gate 
25290Sstevel@tonic-gate 			done = 0;
25300Sstevel@tonic-gate 			break;
25310Sstevel@tonic-gate 		}
25320Sstevel@tonic-gate 		Blocks += (u_longlong_t)((cnt + (Bufsize - 1)) / Bufsize);
25330Sstevel@tonic-gate 		filesz -= (off_t)cnt;
25340Sstevel@tonic-gate 	}
25350Sstevel@tonic-gate 	if (done) {
25360Sstevel@tonic-gate 		rstfiles(U_OVER, G_p->g_passdirfd);
25370Sstevel@tonic-gate 	} else {
25380Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_passdirfd);
25390Sstevel@tonic-gate 	}
25400Sstevel@tonic-gate 	(void) close(Ifile);
25410Sstevel@tonic-gate 	cstatus = close(Ofile);
25420Sstevel@tonic-gate 	Ofile = 0;
25430Sstevel@tonic-gate 	if (cstatus != 0) {
25440Sstevel@tonic-gate 		msg(EXTN, "close error");
25450Sstevel@tonic-gate 	}
25460Sstevel@tonic-gate 	VERBOSE((Args & (OCv | OCV)), Fullnam_p);
25470Sstevel@tonic-gate 	Finished = 1;
25480Sstevel@tonic-gate }
25490Sstevel@tonic-gate 
25500Sstevel@tonic-gate /*
25510Sstevel@tonic-gate  * Allocation wrappers.  Used to centralize error handling for
25520Sstevel@tonic-gate  * failed allocations.
25530Sstevel@tonic-gate  */
25540Sstevel@tonic-gate static void *
25550Sstevel@tonic-gate e_alloc_fail(int flag)
25560Sstevel@tonic-gate {
25570Sstevel@tonic-gate 	if (flag == E_EXIT) {
25580Sstevel@tonic-gate 		msg(EXTN, "Out of memory");
25590Sstevel@tonic-gate 	}
25600Sstevel@tonic-gate 
25610Sstevel@tonic-gate 	return (NULL);
25620Sstevel@tonic-gate }
25630Sstevel@tonic-gate 
25640Sstevel@tonic-gate /*
25650Sstevel@tonic-gate  *  Note: unlike the other e_*lloc functions, e_realloc does not zero out the
25660Sstevel@tonic-gate  *  additional memory it returns.  Ensure that you do not trust its contents
25670Sstevel@tonic-gate  *  when you call it.
25680Sstevel@tonic-gate  */
25690Sstevel@tonic-gate 
25700Sstevel@tonic-gate static void *
25710Sstevel@tonic-gate e_realloc(int flag, void *old, size_t newsize)
25720Sstevel@tonic-gate {
25730Sstevel@tonic-gate 	void *ret = realloc(old, newsize);
25740Sstevel@tonic-gate 
25750Sstevel@tonic-gate 	if (ret == NULL) {
25760Sstevel@tonic-gate 		return (e_alloc_fail(flag));
25770Sstevel@tonic-gate 	}
25780Sstevel@tonic-gate 
25790Sstevel@tonic-gate 	return (ret);
25800Sstevel@tonic-gate }
25810Sstevel@tonic-gate 
25820Sstevel@tonic-gate static char *
25830Sstevel@tonic-gate e_strdup(int flag, const char *arg)
25840Sstevel@tonic-gate {
25850Sstevel@tonic-gate 	char *ret = strdup(arg);
25860Sstevel@tonic-gate 
25870Sstevel@tonic-gate 	if (ret == NULL) {
25880Sstevel@tonic-gate 		return (e_alloc_fail(flag));
25890Sstevel@tonic-gate 	}
25900Sstevel@tonic-gate 
25910Sstevel@tonic-gate 	return (ret);
25920Sstevel@tonic-gate }
25930Sstevel@tonic-gate 
25940Sstevel@tonic-gate static void *
25950Sstevel@tonic-gate e_valloc(int flag, size_t size)
25960Sstevel@tonic-gate {
25970Sstevel@tonic-gate 	void *ret = valloc(size);
25980Sstevel@tonic-gate 
25990Sstevel@tonic-gate 	if (ret == NULL) {
26000Sstevel@tonic-gate 		return (e_alloc_fail(flag));
26010Sstevel@tonic-gate 	}
26020Sstevel@tonic-gate 
26030Sstevel@tonic-gate 	return (ret);
26040Sstevel@tonic-gate }
26050Sstevel@tonic-gate 
26060Sstevel@tonic-gate static void *
26070Sstevel@tonic-gate e_zalloc(int flag, size_t size)
26080Sstevel@tonic-gate {
26090Sstevel@tonic-gate 	void *ret = malloc(size);
26100Sstevel@tonic-gate 
26110Sstevel@tonic-gate 	if (ret == NULL) {
26120Sstevel@tonic-gate 		return (e_alloc_fail(flag));
26130Sstevel@tonic-gate 	}
26140Sstevel@tonic-gate 
26150Sstevel@tonic-gate 	(void) memset(ret, 0, size);
26160Sstevel@tonic-gate 	return (ret);
26170Sstevel@tonic-gate }
26180Sstevel@tonic-gate 
26190Sstevel@tonic-gate /*
26200Sstevel@tonic-gate  * file_in:  Process an object from the archive.  If a TARTYP (TAR or USTAR)
26210Sstevel@tonic-gate  * archive and g_nlink == 1, link this file to the file name in t_linkname
26220Sstevel@tonic-gate  * and return.  Handle linked files in one of two ways.  If Onecopy == 0, this
26230Sstevel@tonic-gate  * is an old style (binary or -c) archive, create and extract the data for the
26240Sstevel@tonic-gate  * first link found, link all subsequent links to this file and skip their data.
26250Sstevel@tonic-gate  * If Oncecopy == 1, save links until all have been processed, and then
26260Sstevel@tonic-gate  * process the links first to last checking their names against the patterns
26270Sstevel@tonic-gate  * and/or asking the user to rename them.  The first link that is accepted
26280Sstevel@tonic-gate  * for xtraction is created and the data is read from the archive.
26290Sstevel@tonic-gate  * All subsequent links that are accepted are linked to this file.
26300Sstevel@tonic-gate  */
26310Sstevel@tonic-gate 
26320Sstevel@tonic-gate static void
26330Sstevel@tonic-gate file_in(void)
26340Sstevel@tonic-gate {
26350Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
26360Sstevel@tonic-gate 	int lnkem = 0, cleanup = 0;
26370Sstevel@tonic-gate 	int proc_file;
26380Sstevel@tonic-gate 	struct Lnk *ttl_p;
26390Sstevel@tonic-gate 	int typeflag;
26400Sstevel@tonic-gate 	char savacl;
26410Sstevel@tonic-gate 	int cwd;
26420Sstevel@tonic-gate 
26430Sstevel@tonic-gate 	G_p = &Gen;
26440Sstevel@tonic-gate 
26450Sstevel@tonic-gate 	/*
26460Sstevel@tonic-gate 	 * Open target directory if this isn't a skipped file
26470Sstevel@tonic-gate 	 * and g_nlink == 1
26480Sstevel@tonic-gate 	 *
26490Sstevel@tonic-gate 	 * Links are handled further down in this function.
26500Sstevel@tonic-gate 	 */
26510Sstevel@tonic-gate 
26520Sstevel@tonic-gate 	proc_file = ckname(0);
26530Sstevel@tonic-gate 
26540Sstevel@tonic-gate 	if (proc_file == F_SKIP && G_p->g_nlink == 1) {
26550Sstevel@tonic-gate 		/*
26560Sstevel@tonic-gate 		 * Normally ckname() prints out the file as a side
26570Sstevel@tonic-gate 		 * effect except for table of contents listing
26580Sstevel@tonic-gate 		 * when its parameter is zero and Onecopy isn't
26590Sstevel@tonic-gate 		 * Zero.  Due to this we need to force the name
26600Sstevel@tonic-gate 		 * to be printed here.
26610Sstevel@tonic-gate 		 */
26620Sstevel@tonic-gate 		if (Onecopy == 1) {
26630Sstevel@tonic-gate 			VERBOSE((Args & OCt), G_p->g_nam_p);
26640Sstevel@tonic-gate 		}
26650Sstevel@tonic-gate 		data_in(P_SKIP);
26660Sstevel@tonic-gate 		return;
26670Sstevel@tonic-gate 	}
26680Sstevel@tonic-gate 
26690Sstevel@tonic-gate 	if (proc_file != F_SKIP && open_dirfd() != 0) {
26700Sstevel@tonic-gate 		data_in(P_SKIP);
26710Sstevel@tonic-gate 		return;
26720Sstevel@tonic-gate 	}
26730Sstevel@tonic-gate 
26740Sstevel@tonic-gate 	if (Hdr_type == BAR) {
26750Sstevel@tonic-gate 		bar_file_in();
26760Sstevel@tonic-gate 		close_dirfd();
26770Sstevel@tonic-gate 		return;
26780Sstevel@tonic-gate 	}
26790Sstevel@tonic-gate 
26800Sstevel@tonic-gate 	/*
26810Sstevel@tonic-gate 	 * For archives in USTAR format, the files are extracted according
26820Sstevel@tonic-gate 	 * to the typeflag.
26830Sstevel@tonic-gate 	 */
26840Sstevel@tonic-gate 	if (Hdr_type == USTAR || Hdr_type == TAR) {
26850Sstevel@tonic-gate 		typeflag = Thdr_p->tbuf.t_typeflag;
26860Sstevel@tonic-gate 		if (G_p->g_nlink == 1) {		/* hard link */
26870Sstevel@tonic-gate 			if (proc_file != F_SKIP) {
26881134Sceastha 				int i;
26891134Sceastha 				char lname[NAMSIZ+1];
26901134Sceastha 				(void) memset(lname, '\0', sizeof (lname));
26911134Sceastha 
26921134Sceastha 				(void) strncpy(lname, Thdr_p->tbuf.t_linkname,
26931134Sceastha 				    NAMSIZ);
26941134Sceastha 				for (i = 0; i <= NAMSIZ && lname[i] != 0; i++)
26951134Sceastha 					;
26961134Sceastha 
26971134Sceastha 				lname[i] = 0;
26981134Sceastha 				(void) creat_lnk(G_p->g_dirfd,
26991134Sceastha 				    &lname[0], G_p->g_nam_p);
27000Sstevel@tonic-gate 			}
27010Sstevel@tonic-gate 			close_dirfd();
27020Sstevel@tonic-gate 			return;
27030Sstevel@tonic-gate 		}
27040Sstevel@tonic-gate 		if (typeflag == '3' || typeflag == '4' || typeflag == '5' ||
27050Sstevel@tonic-gate 		    typeflag == '6') {
27060Sstevel@tonic-gate 			if (proc_file != F_SKIP &&
27070Sstevel@tonic-gate 			    creat_spec(G_p->g_dirfd) > 0) {
27080Sstevel@tonic-gate 				VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
27090Sstevel@tonic-gate 			}
27100Sstevel@tonic-gate 			close_dirfd();
27110Sstevel@tonic-gate 			return;
27120Sstevel@tonic-gate 		} else if (Adir || Aspec) {
27130Sstevel@tonic-gate 			if ((proc_file == F_SKIP) ||
27141134Sceastha 			    (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 			close_dirfd();
27200Sstevel@tonic-gate 			return;
27210Sstevel@tonic-gate 		}
27220Sstevel@tonic-gate 	}
27230Sstevel@tonic-gate 
27240Sstevel@tonic-gate 	if (Adir) {
27250Sstevel@tonic-gate 		if (proc_file != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
27260Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
27270Sstevel@tonic-gate 		}
27280Sstevel@tonic-gate 		close_dirfd();
27290Sstevel@tonic-gate 		if (Onecopy == 1) {
27300Sstevel@tonic-gate 			VERBOSE((Args & OCt), G_p->g_nam_p);
27310Sstevel@tonic-gate 		}
27320Sstevel@tonic-gate 		return;
27330Sstevel@tonic-gate 	}
27340Sstevel@tonic-gate 	if (G_p->g_nlink == 1 || (Hdr_type == TAR ||
27350Sstevel@tonic-gate 	    Hdr_type == USTAR)) {
27360Sstevel@tonic-gate 		if (Aspec) {
27370Sstevel@tonic-gate 			if (proc_file != F_SKIP && creat_spec(G_p->g_dirfd) > 0)
27380Sstevel@tonic-gate 				VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
27390Sstevel@tonic-gate 		} else {
27400Sstevel@tonic-gate 			if ((proc_file == F_SKIP) ||
27411134Sceastha 			    (Ofile = openout(G_p->g_dirfd)) < 0) {
27420Sstevel@tonic-gate 				data_in(P_SKIP);
27430Sstevel@tonic-gate 			} else {
27440Sstevel@tonic-gate 				data_in(P_PROC);
27450Sstevel@tonic-gate 			}
27460Sstevel@tonic-gate 		}
27470Sstevel@tonic-gate 		close_dirfd();
27480Sstevel@tonic-gate 		return;
27490Sstevel@tonic-gate 	}
27500Sstevel@tonic-gate 	close_dirfd();
27510Sstevel@tonic-gate 
27520Sstevel@tonic-gate 	tl_p = add_lnk(&ttl_p);
27530Sstevel@tonic-gate 	l_p = ttl_p;
27540Sstevel@tonic-gate 	if (l_p->L_cnt == l_p->L_gen.g_nlink)
27550Sstevel@tonic-gate 		cleanup = 1;
27560Sstevel@tonic-gate 	if (!Onecopy || G_p->g_attrnam_p != (char *)NULL) {
27570Sstevel@tonic-gate 		lnkem = (tl_p != l_p) ? 1 : 0;
27580Sstevel@tonic-gate 		G_p = &tl_p->L_gen;
27590Sstevel@tonic-gate 		if (proc_file == F_SKIP) {
27600Sstevel@tonic-gate 			data_in(P_SKIP);
27610Sstevel@tonic-gate 		} else {
27620Sstevel@tonic-gate 			if (open_dirfd() != 0)
27630Sstevel@tonic-gate 				return;
27640Sstevel@tonic-gate 			if (!lnkem) {
27650Sstevel@tonic-gate 				if (Aspec) {
27660Sstevel@tonic-gate 					if (creat_spec(G_p->g_dirfd) > 0)
27670Sstevel@tonic-gate 						VERBOSE((Args & (OCv | OCV)),
27680Sstevel@tonic-gate 						    G_p->g_nam_p);
27690Sstevel@tonic-gate 				} else if ((Ofile =
27701134Sceastha 				    openout(G_p->g_dirfd)) < 0) {
27710Sstevel@tonic-gate 					data_in(P_SKIP);
27720Sstevel@tonic-gate 					close_dirfd();
27730Sstevel@tonic-gate 					reclaim(l_p);
27740Sstevel@tonic-gate 				} else {
27750Sstevel@tonic-gate 					data_in(P_PROC);
27760Sstevel@tonic-gate 					close_dirfd();
27770Sstevel@tonic-gate 				}
27780Sstevel@tonic-gate 			} else {
27790Sstevel@tonic-gate 				/*
27800Sstevel@tonic-gate 				 * Are we linking an attribute?
27810Sstevel@tonic-gate 				 */
27820Sstevel@tonic-gate 				cwd = -1;
27830Sstevel@tonic-gate 				if (l_p->L_gen.g_attrnam_p != (char *)NULL) {
27840Sstevel@tonic-gate 					(void) strcpy(Lnkend_p,
27850Sstevel@tonic-gate 					    l_p->L_gen.g_attrnam_p);
27860Sstevel@tonic-gate 					(void) strcpy(Full_p,
27870Sstevel@tonic-gate 					    tl_p->L_gen.g_attrnam_p);
27880Sstevel@tonic-gate 					cwd = save_cwd();
27890Sstevel@tonic-gate 					(void) fchdir(G_p->g_dirfd);
27900Sstevel@tonic-gate 				} else {
27910Sstevel@tonic-gate 					(void) strcpy(Lnkend_p,
27920Sstevel@tonic-gate 					    l_p->L_gen.g_nam_p);
27930Sstevel@tonic-gate 					(void) strcpy(Full_p,
27940Sstevel@tonic-gate 					    tl_p->L_gen.g_nam_p);
27950Sstevel@tonic-gate 				}
27960Sstevel@tonic-gate 				(void) creat_lnk(G_p->g_dirfd,
27970Sstevel@tonic-gate 				    Lnkend_p, Full_p);
27980Sstevel@tonic-gate 				data_in(P_SKIP);
27990Sstevel@tonic-gate 				close_dirfd();
28000Sstevel@tonic-gate 				l_p->L_lnk_p = (struct Lnk *)NULL;
28010Sstevel@tonic-gate 				free(tl_p->L_gen.g_nam_p);
28020Sstevel@tonic-gate 				free(tl_p);
28030Sstevel@tonic-gate 				if (cwd != -1)
28040Sstevel@tonic-gate 					rest_cwd(cwd);
28050Sstevel@tonic-gate 			}
28060Sstevel@tonic-gate 		}
28070Sstevel@tonic-gate 	} else { /* Onecopy */
28080Sstevel@tonic-gate 		if (tl_p->L_gen.g_filesz)
28090Sstevel@tonic-gate 			cleanup = 1;
28100Sstevel@tonic-gate 		if (!cleanup) {
28110Sstevel@tonic-gate 			close_dirfd();
28120Sstevel@tonic-gate 			return; /* don't do anything yet */
28130Sstevel@tonic-gate 		}
28140Sstevel@tonic-gate 		tl_p = l_p;
28150Sstevel@tonic-gate 		/*
28160Sstevel@tonic-gate 		 * ckname will clear aclchar. We need to keep aclchar for
28170Sstevel@tonic-gate 		 * all links.
28180Sstevel@tonic-gate 		 */
28190Sstevel@tonic-gate 		savacl = aclchar;
28200Sstevel@tonic-gate 		while (tl_p != (struct Lnk *)NULL) {
28210Sstevel@tonic-gate 			G_p = &tl_p->L_gen;
28220Sstevel@tonic-gate 			aclchar = savacl;
28230Sstevel@tonic-gate 			if ((proc_file = ckname(1)) != F_SKIP) {
28240Sstevel@tonic-gate 				if (open_dirfd() != 0) {
28250Sstevel@tonic-gate 					return;
28260Sstevel@tonic-gate 				}
28270Sstevel@tonic-gate 				if (l_p->L_data) {
28280Sstevel@tonic-gate 					(void) creat_lnk(G_p->g_dirfd,
28290Sstevel@tonic-gate 					    l_p->L_gen.g_nam_p,
28300Sstevel@tonic-gate 					    G_p->g_nam_p);
28310Sstevel@tonic-gate 				} else if (Aspec) {
28320Sstevel@tonic-gate 					(void) creat_spec(G_p->g_dirfd);
28330Sstevel@tonic-gate 					l_p->L_data = 1;
28340Sstevel@tonic-gate 					VERBOSE((Args & (OCv | OCV)),
28350Sstevel@tonic-gate 					    G_p->g_nam_p);
28360Sstevel@tonic-gate 				} else if ((Ofile =
28370Sstevel@tonic-gate 				    openout(G_p->g_dirfd)) < 0) {
28380Sstevel@tonic-gate 					proc_file = F_SKIP;
28390Sstevel@tonic-gate 				} else {
28400Sstevel@tonic-gate 					data_in(P_PROC);
28410Sstevel@tonic-gate 					l_p->L_data = 1;
28420Sstevel@tonic-gate 				}
28430Sstevel@tonic-gate 			} /* (proc_file = ckname(1)) != F_SKIP */
28440Sstevel@tonic-gate 
28450Sstevel@tonic-gate 			tl_p = tl_p->L_lnk_p;
28460Sstevel@tonic-gate 
28470Sstevel@tonic-gate 			close_dirfd();
28480Sstevel@tonic-gate 
28490Sstevel@tonic-gate 			if (proc_file == F_SKIP && !cleanup) {
28500Sstevel@tonic-gate 				tl_p->L_nxt_p = l_p->L_nxt_p;
28510Sstevel@tonic-gate 				tl_p->L_bck_p = l_p->L_bck_p;
28520Sstevel@tonic-gate 				l_p->L_bck_p->L_nxt_p = tl_p;
28530Sstevel@tonic-gate 				l_p->L_nxt_p->L_bck_p = tl_p;
28540Sstevel@tonic-gate 				free(l_p->L_gen.g_nam_p);
28550Sstevel@tonic-gate 				free(l_p);
28560Sstevel@tonic-gate 			}
28570Sstevel@tonic-gate 		} /* tl_p->L_lnk_p != (struct Lnk *)NULL */
28580Sstevel@tonic-gate 		if (l_p->L_data == 0) {
28590Sstevel@tonic-gate 			data_in(P_SKIP);
28600Sstevel@tonic-gate 		}
28610Sstevel@tonic-gate 	}
28620Sstevel@tonic-gate 	if (cleanup) {
28630Sstevel@tonic-gate 		reclaim(l_p);
28640Sstevel@tonic-gate 	}
28650Sstevel@tonic-gate }
28660Sstevel@tonic-gate 
28670Sstevel@tonic-gate /*
28680Sstevel@tonic-gate  * file_out:  If the current file is not a special file (!Aspec) and it
28690Sstevel@tonic-gate  * is identical to the archive, skip it (do not archive the archive if it
28700Sstevel@tonic-gate  * is a regular file).  If creating a TARTYP (TAR or USTAR) archive, the first
28710Sstevel@tonic-gate  * time a link to a file is encountered, write the header and file out normally.
28720Sstevel@tonic-gate  * Subsequent links to this file put this file name in their t_linkname field.
28730Sstevel@tonic-gate  * Otherwise, links are handled in one of two ways, for the old headers
28740Sstevel@tonic-gate  * (i.e. binary and -c), linked files are written out as they are encountered.
28750Sstevel@tonic-gate  * For the new headers (ASC and CRC), links are saved up until all the links
28760Sstevel@tonic-gate  * to each file are found.  For a file with n links, write n - 1 headers with
28770Sstevel@tonic-gate  * g_filesz set to 0, write the final (nth) header with the correct g_filesz
28780Sstevel@tonic-gate  * value and write the data for the file to the archive.
28790Sstevel@tonic-gate  */
28800Sstevel@tonic-gate 
28810Sstevel@tonic-gate static
28820Sstevel@tonic-gate int
28830Sstevel@tonic-gate file_out(void)
28840Sstevel@tonic-gate {
28850Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
28860Sstevel@tonic-gate 	int cleanup = 0;
28870Sstevel@tonic-gate 	struct Lnk *ttl_p;
28880Sstevel@tonic-gate 
28890Sstevel@tonic-gate 	G_p = &Gen;
28900Sstevel@tonic-gate 	if (!Aspec && IDENT(SrcSt, ArchSt))
28910Sstevel@tonic-gate 		return (1); /* do not archive the archive if it's a reg file */
28920Sstevel@tonic-gate 	if (G_p->g_filesz > Max_offset) {
28930Sstevel@tonic-gate 		msg(ERR, "cpio: %s%s%s: too large to archive in current mode",
28940Sstevel@tonic-gate 		    G_p->g_nam_p,
28950Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
28960Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
28970Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
28980Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p);
28990Sstevel@tonic-gate 		return (1); /* do not archive if it's too big */
29000Sstevel@tonic-gate 	}
29010Sstevel@tonic-gate 	if (Hdr_type == TAR || Hdr_type == USTAR) { /* TAR and USTAR */
29020Sstevel@tonic-gate 		if (Adir) {
29030Sstevel@tonic-gate 			if (Gen.g_attrnam_p != (char *)NULL) {
29040Sstevel@tonic-gate 				write_xattr_hdr();
29050Sstevel@tonic-gate 			}
29060Sstevel@tonic-gate 			write_hdr(ARCHIVE_NORMAL, 0);
29070Sstevel@tonic-gate 			return (0);
29080Sstevel@tonic-gate 		}
29090Sstevel@tonic-gate 		if (G_p->g_nlink == 1) {
29100Sstevel@tonic-gate 			data_out();
29110Sstevel@tonic-gate 			return (0);
29120Sstevel@tonic-gate 		}
29130Sstevel@tonic-gate 		tl_p = add_lnk(&ttl_p);
29140Sstevel@tonic-gate 		l_p = ttl_p;
29150Sstevel@tonic-gate 		if (tl_p == l_p) { /* first link to this file encountered */
29160Sstevel@tonic-gate 			data_out();
29170Sstevel@tonic-gate 			return (0);
29180Sstevel@tonic-gate 		}
29190Sstevel@tonic-gate 		(void) strncpy(T_lname, l_p->L_gen.g_nam_p,
29200Sstevel@tonic-gate 		    l_p->L_gen.g_namesz);
29210Sstevel@tonic-gate 
29220Sstevel@tonic-gate 		/*
29230Sstevel@tonic-gate 		 * check if linkname is greater than 100 characters
29240Sstevel@tonic-gate 		 */
29250Sstevel@tonic-gate 		if (strlen(T_lname) > NAMSIZ) {
29260Sstevel@tonic-gate 			msg(EPOST, "cpio: %s: linkname %s is greater than %d",
29270Sstevel@tonic-gate 			    G_p->g_nam_p, T_lname, NAMSIZ);
29280Sstevel@tonic-gate 			return (1);
29290Sstevel@tonic-gate 		}
29300Sstevel@tonic-gate 
29310Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
29320Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), tl_p->L_gen.g_nam_p);
29330Sstevel@tonic-gate 
29340Sstevel@tonic-gate 		/* find the lnk entry in sublist, unlink it, and free it */
29350Sstevel@tonic-gate 		for (; ttl_p->L_lnk_p != NULL;
29360Sstevel@tonic-gate 		    ttl_p = ttl_p->L_lnk_p) {
29370Sstevel@tonic-gate 			if (ttl_p->L_lnk_p == tl_p) {
29380Sstevel@tonic-gate 				ttl_p->L_lnk_p = tl_p->L_lnk_p;
29390Sstevel@tonic-gate 				free(tl_p->L_gen.g_nam_p);
29400Sstevel@tonic-gate 				free(tl_p);
29410Sstevel@tonic-gate 				break;
29420Sstevel@tonic-gate 			}
29430Sstevel@tonic-gate 		}
29440Sstevel@tonic-gate 
29450Sstevel@tonic-gate 		return (0);
29460Sstevel@tonic-gate 	}
29470Sstevel@tonic-gate 	if (Adir) {
29480Sstevel@tonic-gate 		/*
29490Sstevel@tonic-gate 		 * ACL has been retrieved in getname().
29500Sstevel@tonic-gate 		 */
29510Sstevel@tonic-gate 		if (Pflag) {
29520Sstevel@tonic-gate 			char    *secinfo = NULL;
29530Sstevel@tonic-gate 			int	len = 0;
29540Sstevel@tonic-gate 
29550Sstevel@tonic-gate 			/* append security attributes */
2956789Sahrens 			if ((append_secattr(&secinfo, &len, aclp)) == -1)
29570Sstevel@tonic-gate 				msg(ERR, "can create security information");
29580Sstevel@tonic-gate 
29590Sstevel@tonic-gate 			/* call append_secattr() if more than one */
29600Sstevel@tonic-gate 
29610Sstevel@tonic-gate 			if (len > 0) {
29620Sstevel@tonic-gate 			/* write ancillary */
29630Sstevel@tonic-gate 				(void) write_hdr(ARCHIVE_ACL, (off_t)len);
29640Sstevel@tonic-gate 				(void) write_ancillary(secinfo, len);
29650Sstevel@tonic-gate 			}
29660Sstevel@tonic-gate 		}
29670Sstevel@tonic-gate 
29680Sstevel@tonic-gate 		if (Gen.g_attrnam_p != (char *)NULL) {
29690Sstevel@tonic-gate 			write_xattr_hdr();
29700Sstevel@tonic-gate 		}
29710Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
29720Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
29730Sstevel@tonic-gate 		return (0);
29740Sstevel@tonic-gate 	}
29750Sstevel@tonic-gate 	if (G_p->g_nlink == 1) {
29760Sstevel@tonic-gate 		data_out();
29770Sstevel@tonic-gate 		return (0);
29780Sstevel@tonic-gate 	} else {
29790Sstevel@tonic-gate 		tl_p = add_lnk(&ttl_p);
29800Sstevel@tonic-gate 		l_p = ttl_p;
29810Sstevel@tonic-gate 
29820Sstevel@tonic-gate 		if (l_p->L_cnt == l_p->L_gen.g_nlink)
29830Sstevel@tonic-gate 			cleanup = 1;
29840Sstevel@tonic-gate 		else if (Onecopy && G_p->g_attrnam_p == (char *)NULL) {
29850Sstevel@tonic-gate 			return (0); /* don't process data yet */
29860Sstevel@tonic-gate 		}
29870Sstevel@tonic-gate 	}
29880Sstevel@tonic-gate 	if (Onecopy && G_p->g_attrnam_p == (char *)NULL) {
29890Sstevel@tonic-gate 		tl_p = l_p;
29900Sstevel@tonic-gate 		while (tl_p->L_lnk_p != (struct Lnk *)NULL) {
29910Sstevel@tonic-gate 			G_p = &tl_p->L_gen;
29920Sstevel@tonic-gate 			G_p->g_filesz = (off_t)0;
29930Sstevel@tonic-gate 			/* one link with the acl is sufficient */
29940Sstevel@tonic-gate 			write_hdr(ARCHIVE_NORMAL, (off_t)0);
29950Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
29960Sstevel@tonic-gate 			tl_p = tl_p->L_lnk_p;
29970Sstevel@tonic-gate 		}
29980Sstevel@tonic-gate 		G_p = &tl_p->L_gen;
29990Sstevel@tonic-gate 		if (open_dirfd() != 0)
30000Sstevel@tonic-gate 			return (1);
30010Sstevel@tonic-gate 	}
30020Sstevel@tonic-gate 	/* old style: has acl and data for every link */
30030Sstevel@tonic-gate 	data_out();
30040Sstevel@tonic-gate 	if (cleanup)
30050Sstevel@tonic-gate 		reclaim(l_p);
30060Sstevel@tonic-gate 	return (0);
30070Sstevel@tonic-gate }
30080Sstevel@tonic-gate 
30090Sstevel@tonic-gate /*
30100Sstevel@tonic-gate  * file_pass:  If the -l option is set (link files when possible), and the
30110Sstevel@tonic-gate  * source and destination file systems are the same, link the source file
30120Sstevel@tonic-gate  * (G_p->g_nam_p) to the destination file (Fullnam) and return.  If not a
30130Sstevel@tonic-gate  * linked file, transfer the data.  Otherwise, the first link to a file
30140Sstevel@tonic-gate  * encountered is transferred normally and subsequent links are linked to it.
30150Sstevel@tonic-gate  */
30160Sstevel@tonic-gate 
30170Sstevel@tonic-gate static int
30180Sstevel@tonic-gate file_pass(void)
30190Sstevel@tonic-gate {
30200Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
30210Sstevel@tonic-gate 	struct Lnk *ttl_p;
30220Sstevel@tonic-gate 	char *save_name;
30230Sstevel@tonic-gate 	int size;
30240Sstevel@tonic-gate 	int cwd;
30250Sstevel@tonic-gate 	char *lfrom, *lto;
30260Sstevel@tonic-gate 
30270Sstevel@tonic-gate 	G_p = &Gen;
30280Sstevel@tonic-gate 
30290Sstevel@tonic-gate 	if (Adir && !(Args & OCd)) {
30300Sstevel@tonic-gate 		msg(ERR, "Use -d option to copy \"%s\"", G_p->g_nam_p);
30310Sstevel@tonic-gate 		return (FILE_PASS_ERR);
30320Sstevel@tonic-gate 	}
30330Sstevel@tonic-gate 
30340Sstevel@tonic-gate 	save_name = G_p->g_nam_p;
30350Sstevel@tonic-gate 
30360Sstevel@tonic-gate 	while (*(G_p->g_nam_p) == '/') {
30370Sstevel@tonic-gate 		G_p->g_nam_p++;
30380Sstevel@tonic-gate 	}
30390Sstevel@tonic-gate 
30400Sstevel@tonic-gate 	(void) strcpy(Full_p,
30410Sstevel@tonic-gate 	    (G_p->g_attrfnam_p == (char *)NULL) ?
30420Sstevel@tonic-gate 	    G_p->g_nam_p : G_p->g_attrfnam_p);
30430Sstevel@tonic-gate 
30440Sstevel@tonic-gate 	if (G_p->g_attrnam_p == (char *)NULL) {
30450Sstevel@tonic-gate 		G_p->g_passdirfd = open_dir(Fullnam_p);
30460Sstevel@tonic-gate 
30470Sstevel@tonic-gate 		if (G_p->g_passdirfd == -1) {
30480Sstevel@tonic-gate 			msg(ERRN,
30490Sstevel@tonic-gate 			    "Cannot open/create \"%s\"", Fullnam_p);
30500Sstevel@tonic-gate 			return (FILE_PASS_ERR);
30510Sstevel@tonic-gate 		}
30520Sstevel@tonic-gate 	} else {
30530Sstevel@tonic-gate 		G_p->g_passdirfd = attropen(Fullnam_p, ".", O_RDONLY);
30540Sstevel@tonic-gate 
30550Sstevel@tonic-gate 		if (G_p->g_passdirfd == -1) {
30560Sstevel@tonic-gate 			G_p->g_passdirfd = retry_attrdir_open(Fullnam_p);
30570Sstevel@tonic-gate 
30580Sstevel@tonic-gate 			if (G_p->g_passdirfd == -1) {
30590Sstevel@tonic-gate 				msg(ERRN,
30600Sstevel@tonic-gate 				    "Cannot open attribute directory of"
30610Sstevel@tonic-gate 				    " file \"%s\"", Fullnam_p);
30620Sstevel@tonic-gate 				return (FILE_PASS_ERR);
30630Sstevel@tonic-gate 			}
30640Sstevel@tonic-gate 		}
30650Sstevel@tonic-gate 	}
30660Sstevel@tonic-gate 
30670Sstevel@tonic-gate 	if (Args & OCl) {
30680Sstevel@tonic-gate 		/* We are linking back to the source directory. */
30690Sstevel@tonic-gate 
30700Sstevel@tonic-gate 		if (!Adir) {
30710Sstevel@tonic-gate 			char *existingfile = save_name;
30720Sstevel@tonic-gate 
30730Sstevel@tonic-gate 			if ((Args & OCL) && issymlink) {
30740Sstevel@tonic-gate 				/* We are chasing symlinks. */
30750Sstevel@tonic-gate 
30760Sstevel@tonic-gate 				if ((size = readlink(save_name, Symlnk_p,
30770Sstevel@tonic-gate 				    MAXPATHLEN)) < 0) {
30780Sstevel@tonic-gate 					msg(ERRN,
30790Sstevel@tonic-gate 					    "Cannot read symbolic link \"%s\"",
30800Sstevel@tonic-gate 					    save_name);
30810Sstevel@tonic-gate 					return (FILE_PASS_ERR);
30820Sstevel@tonic-gate 				}
30830Sstevel@tonic-gate 
30840Sstevel@tonic-gate 				Symlnk_p[size] = '\0';
30850Sstevel@tonic-gate 				existingfile = Symlnk_p;
30860Sstevel@tonic-gate 			}
30870Sstevel@tonic-gate 
30880Sstevel@tonic-gate 			if (G_p->g_attrnam_p == (char *)NULL) {
30890Sstevel@tonic-gate 				if (creat_lnk(G_p->g_passdirfd,
30900Sstevel@tonic-gate 				    existingfile, Fullnam_p) == 0) {
30910Sstevel@tonic-gate 					return (FILE_LINKED);
30920Sstevel@tonic-gate 				}
30930Sstevel@tonic-gate 			}
30940Sstevel@tonic-gate 		}
30950Sstevel@tonic-gate 	}
30960Sstevel@tonic-gate 
30970Sstevel@tonic-gate 	if ((G_p->g_mode & Ftype) == S_IFLNK && !(Args & OCL)) {
30980Sstevel@tonic-gate 		/* The archive file is a symlink. */
30990Sstevel@tonic-gate 
31000Sstevel@tonic-gate 		errno = 0;
31010Sstevel@tonic-gate 
31020Sstevel@tonic-gate 		if ((size = readlink(save_name, Symlnk_p, MAXPATHLEN)) < 0) {
31030Sstevel@tonic-gate 			msg(ERRN,
31040Sstevel@tonic-gate 			    "Cannot read symbolic link \"%s\"", save_name);
31050Sstevel@tonic-gate 			return (FILE_PASS_ERR);
31060Sstevel@tonic-gate 		}
31070Sstevel@tonic-gate 
31080Sstevel@tonic-gate 		errno = 0;
31090Sstevel@tonic-gate 		(void) missdir(Fullnam_p);
31100Sstevel@tonic-gate 		*(Symlnk_p + size) = '\0';
31110Sstevel@tonic-gate 
31120Sstevel@tonic-gate 		if (symlink(Symlnk_p, Fullnam_p) < 0) {
31130Sstevel@tonic-gate 			if (errno == EEXIST) {
31140Sstevel@tonic-gate 				if (openout(G_p->g_passdirfd) < 0) {
31151134Sceastha 					if (errno != EEXIST) {
31161134Sceastha 						msg(ERRN,
31171134Sceastha 						    "Cannot create \"%s\"",
31181134Sceastha 						    Fullnam_p);
31191134Sceastha 					}
31200Sstevel@tonic-gate 					return (FILE_PASS_ERR);
31210Sstevel@tonic-gate 				}
31220Sstevel@tonic-gate 			} else {
31230Sstevel@tonic-gate 				msg(ERRN, "Cannot create \"%s\"", Fullnam_p);
31240Sstevel@tonic-gate 				return (FILE_PASS_ERR);
31250Sstevel@tonic-gate 			}
31261134Sceastha 		} else {
31271134Sceastha 			if (Args & OCR) {
31281134Sceastha 				if (lchown(Fullnam_p, (int)Rpw_p->pw_uid,
31291134Sceastha 				    (int)Rpw_p->pw_gid) < 0) {
31301134Sceastha 					msg(ERRN,
31311134Sceastha 					    "Error during chown() of \"%s\"",
31321134Sceastha 					    Fullnam_p);
31331134Sceastha 				}
31341134Sceastha 			} else if ((lchown(Fullnam_p, (int)G_p->g_uid,
31351134Sceastha 			    (int)G_p->g_gid) < 0) && privileged) {
31360Sstevel@tonic-gate 				msg(ERRN,
31370Sstevel@tonic-gate 				    "Error during chown() of \"%s\"",
31380Sstevel@tonic-gate 				    Fullnam_p);
31390Sstevel@tonic-gate 			}
31400Sstevel@tonic-gate 		}
31410Sstevel@tonic-gate 
31420Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), Fullnam_p);
31430Sstevel@tonic-gate 		return (FILE_PASS_ERR);
31440Sstevel@tonic-gate 	}
31450Sstevel@tonic-gate 
31460Sstevel@tonic-gate 	if (!Adir && G_p->g_nlink > 1) {
31470Sstevel@tonic-gate 		/* The archive file has hard links. */
31480Sstevel@tonic-gate 
31490Sstevel@tonic-gate 		tl_p = add_lnk(&ttl_p);
31500Sstevel@tonic-gate 		l_p = ttl_p;
31510Sstevel@tonic-gate 
31520Sstevel@tonic-gate 		if (tl_p == l_p) {
31530Sstevel@tonic-gate 			/* The archive file was not found. */
31540Sstevel@tonic-gate 
31550Sstevel@tonic-gate 			G_p = &tl_p->L_gen;
31560Sstevel@tonic-gate 		} else {
31570Sstevel@tonic-gate 			/* The archive file was found. */
31580Sstevel@tonic-gate 
31590Sstevel@tonic-gate 			cwd = -1;
31600Sstevel@tonic-gate 
31610Sstevel@tonic-gate 			if (l_p->L_gen.g_attrnam_p != (char *)NULL) {
31620Sstevel@tonic-gate 				/* We are linking an attribute */
31630Sstevel@tonic-gate 
31640Sstevel@tonic-gate 				(void) strcpy(Lnkend_p, l_p->L_gen.g_attrnam_p);
31650Sstevel@tonic-gate 				cwd = save_cwd();
31660Sstevel@tonic-gate 				(void) fchdir(G_p->g_passdirfd);
31670Sstevel@tonic-gate 				lfrom = get_component(Lnknam_p);
31680Sstevel@tonic-gate 				lto = tl_p->L_gen.g_attrnam_p;
31690Sstevel@tonic-gate 			} else {
31700Sstevel@tonic-gate 				/* We are not linking an attribute */
31710Sstevel@tonic-gate 
31720Sstevel@tonic-gate 				(void) strcpy(Lnkend_p, l_p->L_gen.g_nam_p);
31730Sstevel@tonic-gate 				(void) strcpy(Full_p, tl_p->L_gen.g_nam_p);
31740Sstevel@tonic-gate 				lfrom = Lnknam_p;
31750Sstevel@tonic-gate 				lto = Fullnam_p;
31760Sstevel@tonic-gate 			}
31770Sstevel@tonic-gate 
31780Sstevel@tonic-gate 			(void) creat_lnk(G_p->g_passdirfd, lfrom, lto);
31790Sstevel@tonic-gate 
31800Sstevel@tonic-gate 			if (cwd) {
31810Sstevel@tonic-gate 				rest_cwd(cwd);
31820Sstevel@tonic-gate 			}
31830Sstevel@tonic-gate 
31840Sstevel@tonic-gate 			l_p->L_lnk_p = (struct Lnk *)NULL;
31850Sstevel@tonic-gate 			free(tl_p->L_gen.g_nam_p);
31860Sstevel@tonic-gate 			free(tl_p);
31870Sstevel@tonic-gate 
31880Sstevel@tonic-gate 			if (l_p->L_cnt == G_p->g_nlink) {
31890Sstevel@tonic-gate 				reclaim(l_p);
31900Sstevel@tonic-gate 			}
31910Sstevel@tonic-gate 
31920Sstevel@tonic-gate 			return (FILE_LINKED);
31930Sstevel@tonic-gate 		}
31940Sstevel@tonic-gate 	}
31950Sstevel@tonic-gate 
31960Sstevel@tonic-gate 	if (Adir || Aspec) {
31970Sstevel@tonic-gate 		/*
31980Sstevel@tonic-gate 		 * The archive file is a directory,  block special, char
31990Sstevel@tonic-gate 		 * special or a fifo.
32000Sstevel@tonic-gate 		 */
32010Sstevel@tonic-gate 
32020Sstevel@tonic-gate 		if (creat_spec(G_p->g_passdirfd) > 0) {
32030Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), Fullnam_p);
32040Sstevel@tonic-gate 		}
32050Sstevel@tonic-gate 	} else if ((Ofile = openout(G_p->g_passdirfd)) > 0) {
32060Sstevel@tonic-gate 		data_pass();
32070Sstevel@tonic-gate 	}
32080Sstevel@tonic-gate 
32090Sstevel@tonic-gate 	return (FILE_COPIED);
32100Sstevel@tonic-gate }
32110Sstevel@tonic-gate 
32120Sstevel@tonic-gate /*
32130Sstevel@tonic-gate  * flush_lnks: With new linked file handling, linked files are not archived
32140Sstevel@tonic-gate  * until all links have been collected.  When the end of the list of filenames
32150Sstevel@tonic-gate  * to archive has been reached, all files that did not encounter all their links
32160Sstevel@tonic-gate  * are written out with actual (encountered) link counts.  A file with n links
32170Sstevel@tonic-gate  * (that are archived) will be represented by n headers (one for each link (the
32180Sstevel@tonic-gate  * first n - 1 have g_filesz set to 0)) followed by the data for the file.
32190Sstevel@tonic-gate  */
32200Sstevel@tonic-gate 
32210Sstevel@tonic-gate static void
32220Sstevel@tonic-gate flush_lnks(void)
32230Sstevel@tonic-gate {
32240Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
32250Sstevel@tonic-gate 	off_t tfsize;
32260Sstevel@tonic-gate 
32270Sstevel@tonic-gate 	l_p = Lnk_hd.L_nxt_p;
32280Sstevel@tonic-gate 	while (l_p != &Lnk_hd) {
32290Sstevel@tonic-gate 		(void) strcpy(Gen.g_nam_p, l_p->L_gen.g_nam_p);
32300Sstevel@tonic-gate 		if (stat(Gen.g_nam_p, &SrcSt) == 0) { /* check if file exists */
32310Sstevel@tonic-gate 			tl_p = l_p;
32320Sstevel@tonic-gate 			(void) creat_hdr();
32330Sstevel@tonic-gate 			Gen.g_nlink = l_p->L_cnt; /* "actual" link count */
32340Sstevel@tonic-gate 			tfsize = Gen.g_filesz;
32350Sstevel@tonic-gate 			Gen.g_filesz = (off_t)0;
32360Sstevel@tonic-gate 			G_p = &Gen;
32370Sstevel@tonic-gate 			while (tl_p != (struct Lnk *)NULL) {
32380Sstevel@tonic-gate 				Gen.g_nam_p = tl_p->L_gen.g_nam_p;
32390Sstevel@tonic-gate 				Gen.g_namesz = tl_p->L_gen.g_namesz;
32400Sstevel@tonic-gate 				if (tl_p->L_lnk_p == (struct Lnk *)NULL) {
32410Sstevel@tonic-gate 					Gen.g_filesz = tfsize;
32420Sstevel@tonic-gate 					if (open_dirfd() != 0) {
32430Sstevel@tonic-gate 						break;
32440Sstevel@tonic-gate 					}
32450Sstevel@tonic-gate 					data_out();
32460Sstevel@tonic-gate 					break;
32470Sstevel@tonic-gate 				}
32480Sstevel@tonic-gate 				write_hdr(ARCHIVE_NORMAL,
32490Sstevel@tonic-gate 				    (off_t)0); /* header only */
32500Sstevel@tonic-gate 				VERBOSE((Args & (OCv | OCV)), Gen.g_nam_p);
32510Sstevel@tonic-gate 				tl_p = tl_p->L_lnk_p;
32520Sstevel@tonic-gate 			}
32530Sstevel@tonic-gate 			Gen.g_nam_p = Nam_p;
32540Sstevel@tonic-gate 		} else /* stat(Gen.g_nam_p, &SrcSt) == 0 */
32550Sstevel@tonic-gate 			msg(ERR, "\"%s%s%s\" has disappeared",
32560Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
32570Sstevel@tonic-gate 			    Gen.g_nam_p : Gen.g_attrfnam_p,
32580Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
32590Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
32600Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
32610Sstevel@tonic-gate 			    "" : Gen.g_attrnam_p);
32620Sstevel@tonic-gate 		tl_p = l_p;
32630Sstevel@tonic-gate 		l_p = l_p->L_nxt_p;
32640Sstevel@tonic-gate 		reclaim(tl_p);
32650Sstevel@tonic-gate 	} /* l_p != &Lnk_hd */
32660Sstevel@tonic-gate }
32670Sstevel@tonic-gate 
32680Sstevel@tonic-gate /*
32690Sstevel@tonic-gate  * gethdr: Get a header from the archive, validate it and check for the trailer.
32700Sstevel@tonic-gate  * Any user specified Hdr_type is ignored (set to NONE in main).  Hdr_type is
32710Sstevel@tonic-gate  * set appropriately after a valid header is found.  Unless the -k option is
32720Sstevel@tonic-gate  * set a corrupted header causes an exit with an error.  I/O errors during
32730Sstevel@tonic-gate  * examination of any part of the header cause gethdr to throw away any current
32740Sstevel@tonic-gate  * data and start over.  Other errors during examination of any part of the
32750Sstevel@tonic-gate  * header cause gethdr to advance one byte and continue the examination.
32760Sstevel@tonic-gate  */
32770Sstevel@tonic-gate 
32780Sstevel@tonic-gate static int
32790Sstevel@tonic-gate gethdr(void)
32800Sstevel@tonic-gate {
32810Sstevel@tonic-gate 	ushort_t ftype;
32820Sstevel@tonic-gate 	int hit = NONE, cnt = 0;
32830Sstevel@tonic-gate 	int goodhdr, hsize, offset;
32840Sstevel@tonic-gate 	int bswap = 0;
32850Sstevel@tonic-gate 	char *preptr;
32860Sstevel@tonic-gate 	int k = 0;
32870Sstevel@tonic-gate 	int j;
3288789Sahrens 	int error;
3289789Sahrens 	int aclcnt;
32900Sstevel@tonic-gate 
32910Sstevel@tonic-gate 	Gen.g_nam_p = Nam_p;
32920Sstevel@tonic-gate 	do { /* hit == NONE && (Args & OCk) && Buffr.b_cnt > 0 */
32930Sstevel@tonic-gate 		FILL(Hdrsz);
32940Sstevel@tonic-gate 		switch (Hdr_type) {
32950Sstevel@tonic-gate 		case NONE:
32960Sstevel@tonic-gate 		case BIN:
32970Sstevel@tonic-gate 			Binmag.b_byte[0] = Buffr.b_out_p[0];
32980Sstevel@tonic-gate 			Binmag.b_byte[1] = Buffr.b_out_p[1];
32990Sstevel@tonic-gate 			if ((Binmag.b_half == CMN_BIN) ||
33000Sstevel@tonic-gate 			    (Binmag.b_half == CMN_BBS)) {
33010Sstevel@tonic-gate 				hit = read_hdr(BIN);
33020Sstevel@tonic-gate 				if (Hdr_type == NONE)
33030Sstevel@tonic-gate 					bswap = 1;
33040Sstevel@tonic-gate 				hsize = HDRSZ + Gen.g_namesz;
33050Sstevel@tonic-gate 				break;
33060Sstevel@tonic-gate 			}
33070Sstevel@tonic-gate 			if (Hdr_type != NONE)
33080Sstevel@tonic-gate 				break;
33090Sstevel@tonic-gate 			/*FALLTHROUGH*/
33100Sstevel@tonic-gate 		case CHR:
33110Sstevel@tonic-gate 			if (!(strncmp(Buffr.b_out_p, CMS_CHR, CMS_LEN))) {
33120Sstevel@tonic-gate 				hit = read_hdr(CHR);
33130Sstevel@tonic-gate 				hsize = CHRSZ + Gen.g_namesz;
33140Sstevel@tonic-gate 				break;
33150Sstevel@tonic-gate 			}
33160Sstevel@tonic-gate 			if (Hdr_type != NONE)
33170Sstevel@tonic-gate 				break;
33180Sstevel@tonic-gate 			/*FALLTHROUGH*/
33190Sstevel@tonic-gate 		case ASC:
33200Sstevel@tonic-gate 			if (!(strncmp(Buffr.b_out_p, CMS_ASC, CMS_LEN))) {
33210Sstevel@tonic-gate 				hit = read_hdr(ASC);
33220Sstevel@tonic-gate 				hsize = ASCSZ + Gen.g_namesz;
33230Sstevel@tonic-gate 				Max_namesz = APATH;
33240Sstevel@tonic-gate 				break;
33250Sstevel@tonic-gate 			}
33260Sstevel@tonic-gate 			if (Hdr_type != NONE)
33270Sstevel@tonic-gate 				break;
33280Sstevel@tonic-gate 			/*FALLTHROUGH*/
33290Sstevel@tonic-gate 		case CRC:
33300Sstevel@tonic-gate 			if (!(strncmp(Buffr.b_out_p, CMS_CRC, CMS_LEN))) {
33310Sstevel@tonic-gate 				hit = read_hdr(CRC);
33320Sstevel@tonic-gate 				hsize = ASCSZ + Gen.g_namesz;
33330Sstevel@tonic-gate 				Max_namesz = APATH;
33340Sstevel@tonic-gate 				break;
33350Sstevel@tonic-gate 			}
33360Sstevel@tonic-gate 			if (Hdr_type != NONE)
33370Sstevel@tonic-gate 				break;
33380Sstevel@tonic-gate 			/*FALLTHROUGH*/
33390Sstevel@tonic-gate 
33400Sstevel@tonic-gate 		case BAR:
33410Sstevel@tonic-gate 			if (Hdr_p != NULL && strcmp(Hdr_p, "bar") == 0) {
33420Sstevel@tonic-gate 				Hdrsz = BARSZ;
33430Sstevel@tonic-gate 				FILL(Hdrsz);
33440Sstevel@tonic-gate 				if ((hit = read_hdr(BAR)) == NONE) {
33450Sstevel@tonic-gate 					Hdrsz = ASCSZ;
33460Sstevel@tonic-gate 					break;
33470Sstevel@tonic-gate 				}
33480Sstevel@tonic-gate 				hit = BAR;
33490Sstevel@tonic-gate 				hsize = BARSZ;
33500Sstevel@tonic-gate 				break;
33510Sstevel@tonic-gate 			}
33520Sstevel@tonic-gate 			/*FALLTHROUGH*/
33530Sstevel@tonic-gate 
33540Sstevel@tonic-gate 		case USTAR:
33550Sstevel@tonic-gate 			if (Hdr_p != NULL && strcmp(Hdr_p, "ustar") == 0) {
33560Sstevel@tonic-gate 				Hdrsz = TARSZ;
33570Sstevel@tonic-gate 				FILL(Hdrsz);
33580Sstevel@tonic-gate 				if ((hit = read_hdr(USTAR)) == NONE) {
33590Sstevel@tonic-gate 					Hdrsz = ASCSZ;
33600Sstevel@tonic-gate 					break;
33610Sstevel@tonic-gate 				}
33620Sstevel@tonic-gate 				hit = USTAR;
33630Sstevel@tonic-gate 				hsize = TARSZ;
33640Sstevel@tonic-gate 				break;
33650Sstevel@tonic-gate 			}
33660Sstevel@tonic-gate 			/*FALLTHROUGH*/
33670Sstevel@tonic-gate 		case TAR:
33680Sstevel@tonic-gate 			if (Hdr_p != NULL && strcmp(Hdr_p, "tar") == 0) {
33690Sstevel@tonic-gate 				Hdrsz = TARSZ;
33700Sstevel@tonic-gate 				FILL(Hdrsz);
33710Sstevel@tonic-gate 				if ((hit = read_hdr(TAR)) == NONE) {
33720Sstevel@tonic-gate 					Hdrsz = ASCSZ;
33730Sstevel@tonic-gate 					break;
33740Sstevel@tonic-gate 				}
33750Sstevel@tonic-gate 				hit = TAR;
33760Sstevel@tonic-gate 				hsize = TARSZ;
33770Sstevel@tonic-gate 				break;
33780Sstevel@tonic-gate 			}
33790Sstevel@tonic-gate 			/*FALLTHROUGH*/
33800Sstevel@tonic-gate 		default:
33810Sstevel@tonic-gate 			msg(EXT, "Impossible header type.");
33820Sstevel@tonic-gate 		} /* Hdr_type */
33830Sstevel@tonic-gate 
33840Sstevel@tonic-gate 		if (hit == TAR || hit == USTAR) {
33850Sstevel@tonic-gate 			Gen.g_nam_p = &nambuf[0];
33860Sstevel@tonic-gate 		}
33870Sstevel@tonic-gate 
33880Sstevel@tonic-gate 		if (hit != NONE) {
33890Sstevel@tonic-gate 			FILL(hsize);
33900Sstevel@tonic-gate 			goodhdr = 1;
33910Sstevel@tonic-gate 			if (Gen.g_filesz < (off_t)0 || Gen.g_namesz < 1)
33920Sstevel@tonic-gate 				goodhdr = 0;
33930Sstevel@tonic-gate 			if ((hit != USTAR) && (hit != TAR))
33940Sstevel@tonic-gate 				if (Gen.g_namesz - 1 > Max_namesz)
33950Sstevel@tonic-gate 					goodhdr = 0;
33960Sstevel@tonic-gate 			/* TAR and USTAR */
33970Sstevel@tonic-gate 			if ((hit == USTAR) || (hit == TAR)) {
33980Sstevel@tonic-gate 				if (*Gen.g_nam_p == '\0') { /* tar trailer */
33990Sstevel@tonic-gate 					goodhdr = 1;
34000Sstevel@tonic-gate 				} else {
34010Sstevel@tonic-gate 
34020Sstevel@tonic-gate 					G_p = &Gen;
34030Sstevel@tonic-gate 					if (G_p->g_cksum !=
34040Sstevel@tonic-gate 					    cksum(TARTYP, 0, NULL)) {
34050Sstevel@tonic-gate 						goodhdr = 0;
34060Sstevel@tonic-gate 						msg(ERR,
34070Sstevel@tonic-gate 						    "Bad header - checksum "
34080Sstevel@tonic-gate 						    "error.");
34090Sstevel@tonic-gate 					}
34100Sstevel@tonic-gate 				}
34110Sstevel@tonic-gate 			} else if (hit != BAR) { /* binary, -c, ASC and CRC */
34120Sstevel@tonic-gate 				if (Gen.g_nlink <= (ulong_t)0)
34130Sstevel@tonic-gate 					goodhdr = 0;
34140Sstevel@tonic-gate 				if (*(Buffr.b_out_p + hsize - 1) != '\0')
34150Sstevel@tonic-gate 					goodhdr = 0;
34160Sstevel@tonic-gate 			}
34170Sstevel@tonic-gate 			if (!goodhdr) {
34180Sstevel@tonic-gate 				hit = NONE;
34190Sstevel@tonic-gate 				if (!(Args & OCk))
34200Sstevel@tonic-gate 					break;
34210Sstevel@tonic-gate 				msg(ERR,
34220Sstevel@tonic-gate 				    "Corrupt header, file(s) may be lost.");
34230Sstevel@tonic-gate 			} else {
34240Sstevel@tonic-gate 				FILL(hsize);
34250Sstevel@tonic-gate 			}
34260Sstevel@tonic-gate 		} /* hit != NONE */
34270Sstevel@tonic-gate 		if (hit == NONE) {
34280Sstevel@tonic-gate 			Buffr.b_out_p++;
34290Sstevel@tonic-gate 			Buffr.b_cnt--;
34300Sstevel@tonic-gate 			if (!(Args & OCk))
34310Sstevel@tonic-gate 				break;
34320Sstevel@tonic-gate 			if (!cnt++)
34330Sstevel@tonic-gate 				msg(ERR, "Searching for magic number/header.");
34340Sstevel@tonic-gate 		}
34350Sstevel@tonic-gate 	} while (hit == NONE);
34360Sstevel@tonic-gate 	if (hit == NONE) {
34370Sstevel@tonic-gate 		if (Hdr_type == NONE)
34380Sstevel@tonic-gate 			msg(EXT, "Not a cpio file, bad header.");
34390Sstevel@tonic-gate 		else
34400Sstevel@tonic-gate 			msg(EXT, "Bad magic number/header.");
34410Sstevel@tonic-gate 	} else if (cnt > 0) {
34420Sstevel@tonic-gate 		msg(EPOST, "Re-synchronized on magic number/header.");
34430Sstevel@tonic-gate 	}
34440Sstevel@tonic-gate 	if (Hdr_type == NONE) {
34450Sstevel@tonic-gate 		Hdr_type = hit;
34460Sstevel@tonic-gate 		switch (Hdr_type) {
34470Sstevel@tonic-gate 		case BIN:
34480Sstevel@tonic-gate 			if (bswap)
34490Sstevel@tonic-gate 				Args |= BSM;
34500Sstevel@tonic-gate 			Hdrsz = HDRSZ;
34510Sstevel@tonic-gate 			Max_namesz = CPATH;
34520Sstevel@tonic-gate 			Pad_val = HALFWD;
34530Sstevel@tonic-gate 			Onecopy = 0;
34540Sstevel@tonic-gate 			break;
34550Sstevel@tonic-gate 		case CHR:
34560Sstevel@tonic-gate 			Hdrsz = CHRSZ;
34570Sstevel@tonic-gate 			Max_namesz = CPATH;
34580Sstevel@tonic-gate 			Pad_val = 0;
34590Sstevel@tonic-gate 			Onecopy = 0;
34600Sstevel@tonic-gate 			break;
34610Sstevel@tonic-gate 		case ASC:
34620Sstevel@tonic-gate 		case CRC:
34630Sstevel@tonic-gate 			Hdrsz = ASCSZ;
34640Sstevel@tonic-gate 			Max_namesz = APATH;
34650Sstevel@tonic-gate 			Pad_val = FULLWD;
34660Sstevel@tonic-gate 			Onecopy = 1;
34670Sstevel@tonic-gate 			break;
34680Sstevel@tonic-gate 		case USTAR:
34690Sstevel@tonic-gate 			Hdrsz = TARSZ;
34700Sstevel@tonic-gate 			Max_namesz = HNAMLEN - 1;
34710Sstevel@tonic-gate 			Pad_val = FULLBK;
34720Sstevel@tonic-gate 			Onecopy = 0;
34730Sstevel@tonic-gate 			break;
34740Sstevel@tonic-gate 		case BAR:
34750Sstevel@tonic-gate 		case TAR:
34760Sstevel@tonic-gate 			Hdrsz = TARSZ;
34770Sstevel@tonic-gate 			Max_namesz = TNAMLEN - 1;
34780Sstevel@tonic-gate 			Pad_val = FULLBK;
34790Sstevel@tonic-gate 			Onecopy = 0;
34800Sstevel@tonic-gate 			break;
34810Sstevel@tonic-gate 		default:
34820Sstevel@tonic-gate 			msg(EXT, "Impossible header type.");
34830Sstevel@tonic-gate 		} /* Hdr_type */
34840Sstevel@tonic-gate 	} /* Hdr_type == NONE */
34850Sstevel@tonic-gate 	if ((Hdr_type == USTAR) || (Hdr_type == TAR) ||
34860Sstevel@tonic-gate 	    (Hdr_type == BAR)) {			/* TAR, USTAR, BAR */
34870Sstevel@tonic-gate 		Gen.g_namesz = 0;
34880Sstevel@tonic-gate 		if (Gen.g_nam_p[0] == '\0')
34890Sstevel@tonic-gate 			return (0);
34900Sstevel@tonic-gate 		else {
34910Sstevel@tonic-gate 			preptr = &prebuf[0];
34920Sstevel@tonic-gate 			if (*preptr != (char)NULL) {
34930Sstevel@tonic-gate 				k = strlen(&prebuf[0]);
34940Sstevel@tonic-gate 				if (k < PRESIZ) {
34950Sstevel@tonic-gate 					(void) strcpy(&fullnam[0], &prebuf[0]);
34960Sstevel@tonic-gate 					j = 0;
34970Sstevel@tonic-gate 					fullnam[k++] = '/';
34980Sstevel@tonic-gate 					while ((j < NAMSIZ) && (&nambuf[j] !=
34990Sstevel@tonic-gate 					    (char)NULL)) {
35000Sstevel@tonic-gate 						fullnam[k] = nambuf[j];
35010Sstevel@tonic-gate 						k++; j++;
35020Sstevel@tonic-gate 					}
35030Sstevel@tonic-gate 					fullnam[k] = '\0';
35040Sstevel@tonic-gate 				} else if (k >= PRESIZ) {
35050Sstevel@tonic-gate 					k = 0;
35060Sstevel@tonic-gate 					while ((k < PRESIZ) && (prebuf[k] !=
35070Sstevel@tonic-gate 					    '\0')) {
35080Sstevel@tonic-gate 						fullnam[k] = prebuf[k];
35090Sstevel@tonic-gate 						k++;
35100Sstevel@tonic-gate 					}
35110Sstevel@tonic-gate 					fullnam[k++] = '/';
35120Sstevel@tonic-gate 					j = 0;
35130Sstevel@tonic-gate 					while ((j < NAMSIZ) && (nambuf[j] !=
35140Sstevel@tonic-gate 					    '\0')) {
35150Sstevel@tonic-gate 						fullnam[k] = nambuf[j];
35160Sstevel@tonic-gate 						k++; j++;
35170Sstevel@tonic-gate 					}
35180Sstevel@tonic-gate 					fullnam[k] = '\0';
35190Sstevel@tonic-gate 				}
35200Sstevel@tonic-gate 				Gen.g_nam_p = &fullnam[0];
35210Sstevel@tonic-gate 			} else
35220Sstevel@tonic-gate 				Gen.g_nam_p = &nambuf[0];
35230Sstevel@tonic-gate 
35240Sstevel@tonic-gate 			/*
35250Sstevel@tonic-gate 			 * initialize the buffer so that the prefix will not
35260Sstevel@tonic-gate 			 * applied to the next entry in the archive
35270Sstevel@tonic-gate 			 */
35280Sstevel@tonic-gate 			(void) memset(prebuf, 0, sizeof (prebuf));
35290Sstevel@tonic-gate 		}
35300Sstevel@tonic-gate 	} else if (Hdr_type != BAR) {
35310Sstevel@tonic-gate 		(void) memcpy(Gen.g_nam_p, Buffr.b_out_p + Hdrsz, Gen.g_namesz);
35320Sstevel@tonic-gate 		if (!(strcmp(Gen.g_nam_p, "TRAILER!!!")))
35330Sstevel@tonic-gate 			return (0);
35340Sstevel@tonic-gate 	}
35350Sstevel@tonic-gate 	offset = ((hsize + Pad_val) & ~Pad_val);
35360Sstevel@tonic-gate 	FILL(offset + Hdrsz);
35370Sstevel@tonic-gate 	Thdr_p = (union tblock *)Buffr.b_out_p;
35380Sstevel@tonic-gate 	Buffr.b_out_p += offset;
35390Sstevel@tonic-gate 	Buffr.b_cnt -= (off_t)offset;
35400Sstevel@tonic-gate 	ftype = Gen.g_mode & Ftype;
35410Sstevel@tonic-gate 
35420Sstevel@tonic-gate #if defined(O_XATTR)
35430Sstevel@tonic-gate 	/* extended attribute support */
35440Sstevel@tonic-gate 	if (((Gen.g_mode & S_IFMT) == _XATTR_CPIO_MODE) ||
35450Sstevel@tonic-gate 	    ((Hdr_type == USTAR || Hdr_type == TAR) &&
35460Sstevel@tonic-gate 	    Thdr_p->tbuf.t_typeflag == _XATTR_HDRTYPE)) {
35470Sstevel@tonic-gate 		if (xattrp != (struct xattr_buf *)NULL) {
35480Sstevel@tonic-gate 			if (xattrbadhead) {
35490Sstevel@tonic-gate 				free(xattrhead);
35500Sstevel@tonic-gate 				xattrp = NULL;
35510Sstevel@tonic-gate 				xattr_linkp = NULL;
35520Sstevel@tonic-gate 				xattrhead = NULL;
35530Sstevel@tonic-gate 				return (1);
35540Sstevel@tonic-gate 			}
35550Sstevel@tonic-gate 			if (Atflag == 0 && ((Args & OCt) == 0)) {
35560Sstevel@tonic-gate 				data_in(P_SKIP);
35570Sstevel@tonic-gate 				free(xattrhead);
35580Sstevel@tonic-gate 				xattrhead = NULL;
35590Sstevel@tonic-gate 				xattrp = NULL;
35600Sstevel@tonic-gate 				Gen.g_attrfnam_p = (char *)NULL;
35610Sstevel@tonic-gate 				Gen.g_attrnam_p = (char *)NULL;
35620Sstevel@tonic-gate 				return (2);
35630Sstevel@tonic-gate 			}
35640Sstevel@tonic-gate 
35650Sstevel@tonic-gate 			if (Gen.g_attrfnam_p != (char *)NULL) {
35660Sstevel@tonic-gate 				free(Gen.g_attrfnam_p);
35670Sstevel@tonic-gate 				Gen.g_attrfnam_p = (char *)NULL;
35680Sstevel@tonic-gate 			}
35690Sstevel@tonic-gate 			if (Gen.g_attrnam_p != (char *)NULL) {
35700Sstevel@tonic-gate 				free(Gen.g_attrnam_p);
35710Sstevel@tonic-gate 				Gen.g_attrnam_p = (char *)NULL;
35720Sstevel@tonic-gate 			}
35730Sstevel@tonic-gate 			if (Renam_p && Renam_p[0] != '\0') {
35740Sstevel@tonic-gate 				Gen.g_attrfnam_p = e_strdup(E_EXIT, Renam_p);
35750Sstevel@tonic-gate 			} else {
35760Sstevel@tonic-gate 				Gen.g_attrfnam_p = e_strdup(E_EXIT,
35770Sstevel@tonic-gate 				    xattrp->h_names);
35780Sstevel@tonic-gate 			}
35790Sstevel@tonic-gate 
35800Sstevel@tonic-gate 			Gen.g_attrnam_p = e_strdup(E_EXIT,
35810Sstevel@tonic-gate 			    xattrp->h_names + strlen(xattrp->h_names) + 1);
35820Sstevel@tonic-gate 
35830Sstevel@tonic-gate 			if (Hdr_type != USTAR && Hdr_type != TAR) {
35840Sstevel@tonic-gate 				Gen.g_mode = Gen.g_mode & (~_XATTR_CPIO_MODE);
35850Sstevel@tonic-gate 				Gen.g_mode |= attrmode(xattrp->h_typeflag);
35860Sstevel@tonic-gate 			} else if (Hdr_type == USTAR || Hdr_type == TAR) {
35870Sstevel@tonic-gate 				Thdr_p->tbuf.t_typeflag = xattrp->h_typeflag;
35880Sstevel@tonic-gate 			}
35890Sstevel@tonic-gate 			if (xattr_linkp != (struct xattr_buf *)NULL) {
35900Sstevel@tonic-gate 				if (Gen.g_linktoattrfnam_p != (char *)NULL) {
35910Sstevel@tonic-gate 					free(Gen.g_linktoattrfnam_p);
35920Sstevel@tonic-gate 					Gen.g_linktoattrfnam_p = NULL;
35930Sstevel@tonic-gate 				}
35940Sstevel@tonic-gate 				if (Gen.g_linktoattrnam_p != (char *)NULL) {
35950Sstevel@tonic-gate 					free(Gen.g_linktoattrnam_p);
35960Sstevel@tonic-gate 					Gen.g_linktoattrnam_p = NULL;
35970Sstevel@tonic-gate 				}
35980Sstevel@tonic-gate 				Gen.g_linktoattrfnam_p = e_strdup(E_EXIT,
35990Sstevel@tonic-gate 				    xattr_linkp->h_names);
36000Sstevel@tonic-gate 				Gen.g_linktoattrnam_p = e_strdup(E_EXIT,
36010Sstevel@tonic-gate 				    xattr_linkp->h_names +
36020Sstevel@tonic-gate 				    strlen(xattr_linkp->h_names) + 1);
36030Sstevel@tonic-gate 				xattr_linkp = NULL;
36040Sstevel@tonic-gate 			}
36050Sstevel@tonic-gate 			ftype = Gen.g_mode & Ftype;
36060Sstevel@tonic-gate 			Adir = ftype == S_IFDIR;
36070Sstevel@tonic-gate 			Aspec = (ftype == S_IFBLK ||
36080Sstevel@tonic-gate 			    ftype == S_IFCHR || ftype == S_IFIFO);
36090Sstevel@tonic-gate 
36100Sstevel@tonic-gate 			if (Gen.g_attrnam_p[0] == '.' &&
36110Sstevel@tonic-gate 			    Gen.g_attrnam_p[1] == '\0' &&
36120Sstevel@tonic-gate 			    xattrp->h_typeflag == DIRTYPE) {
36130Sstevel@tonic-gate 				Hiddendir = 1;
36140Sstevel@tonic-gate 			} else {
36150Sstevel@tonic-gate 				Hiddendir = 0;
36160Sstevel@tonic-gate 			}
36170Sstevel@tonic-gate 
36180Sstevel@tonic-gate 			free(xattrhead);
36190Sstevel@tonic-gate 			xattrhead = NULL;
36200Sstevel@tonic-gate 			xattrp = NULL;
36210Sstevel@tonic-gate 		} else {
36220Sstevel@tonic-gate 			if (xattrbadhead == 0) {
36230Sstevel@tonic-gate 				(void) read_xattr_hdr();
36240Sstevel@tonic-gate 				return (2);
36250Sstevel@tonic-gate 			}
36260Sstevel@tonic-gate 		}
36270Sstevel@tonic-gate 	}
36280Sstevel@tonic-gate #endif /* O_XATTR */
36290Sstevel@tonic-gate 
36300Sstevel@tonic-gate 	/* acl support: grab acl info */
36310Sstevel@tonic-gate 	if ((Gen.g_mode == SECMODE) || ((Hdr_type == USTAR ||
36320Sstevel@tonic-gate 	    Hdr_type == TAR) && Thdr_p->tbuf.t_typeflag == 'A')) {
36330Sstevel@tonic-gate 		/* this is an ancillary file */
36340Sstevel@tonic-gate 		off_t	bytes;
36350Sstevel@tonic-gate 		char	*secp;
36360Sstevel@tonic-gate 		int	pad;
36370Sstevel@tonic-gate 		int	cnt;
36380Sstevel@tonic-gate 		char	*tp;
36390Sstevel@tonic-gate 		int	attrsize;
36400Sstevel@tonic-gate 
36410Sstevel@tonic-gate 		if (Pflag) {
36420Sstevel@tonic-gate 			bytes = Gen.g_filesz;
36430Sstevel@tonic-gate 			secp = e_zalloc(E_EXIT, (uint_t)bytes);
36440Sstevel@tonic-gate 			tp = secp;
36450Sstevel@tonic-gate 
36460Sstevel@tonic-gate 			while (bytes > 0) {
36470Sstevel@tonic-gate 				cnt = (int)(bytes > CPIOBSZ) ? CPIOBSZ : bytes;
36480Sstevel@tonic-gate 				FILL(cnt);
36490Sstevel@tonic-gate 				(void) memcpy(tp, Buffr.b_out_p, cnt);
36500Sstevel@tonic-gate 				tp += cnt;
36510Sstevel@tonic-gate 				Buffr.b_out_p += cnt;
36520Sstevel@tonic-gate 				Buffr.b_cnt -= (off_t)cnt;
36530Sstevel@tonic-gate 				bytes -= (off_t)cnt;
36540Sstevel@tonic-gate 			}
36550Sstevel@tonic-gate 
36560Sstevel@tonic-gate 			pad = (Pad_val + 1 - (Gen.g_filesz & Pad_val)) &
36570Sstevel@tonic-gate 			    Pad_val;
36580Sstevel@tonic-gate 			if (pad != 0) {
36590Sstevel@tonic-gate 				FILL(pad);
36600Sstevel@tonic-gate 				Buffr.b_out_p += pad;
36610Sstevel@tonic-gate 				Buffr.b_cnt -= (off_t)pad;
36620Sstevel@tonic-gate 			}
36630Sstevel@tonic-gate 
36640Sstevel@tonic-gate 			/* got all attributes in secp */
36650Sstevel@tonic-gate 			tp = secp;
36660Sstevel@tonic-gate 			do {
36670Sstevel@tonic-gate 				attr = (struct sec_attr *)tp;
36680Sstevel@tonic-gate 				switch (attr->attr_type) {
36690Sstevel@tonic-gate 				case UFSD_ACL:
3670789Sahrens 				case ACE_ACL:
36710Sstevel@tonic-gate 					(void) sscanf(attr->attr_len, "%7lo",
36720Sstevel@tonic-gate 					    (ulong_t *)&aclcnt);
3673789Sahrens 					/* header is 8 */
36740Sstevel@tonic-gate 					attrsize = 8 +
36750Sstevel@tonic-gate 					    strlen(&attr->attr_info[0])
36760Sstevel@tonic-gate 					    + 1;
3677789Sahrens 
3678789Sahrens 					error =
3679789Sahrens 					    acl_fromtext(&attr->attr_info[0],
3680789Sahrens 					    &aclp);
3681789Sahrens 
3682789Sahrens 					if (error != 0) {
3683789Sahrens 						msg(ERR,
3684789Sahrens 						    "aclfromtext failed: %s",
3685789Sahrens 						    acl_strerror(error));
3686789Sahrens 						bytes -= attrsize;
36870Sstevel@tonic-gate 						break;
36880Sstevel@tonic-gate 					}
3689789Sahrens 
3690789Sahrens 					if (aclcnt != acl_cnt(aclp)) {
36910Sstevel@tonic-gate 						msg(ERR, "acl count error");
3692789Sahrens 						bytes -= attrsize;
36930Sstevel@tonic-gate 						break;
36940Sstevel@tonic-gate 					}
36950Sstevel@tonic-gate 					bytes -= attrsize;
36960Sstevel@tonic-gate 					break;
36970Sstevel@tonic-gate 
36980Sstevel@tonic-gate 				/* SunFed case goes here */
36990Sstevel@tonic-gate 
37000Sstevel@tonic-gate 				default:
37010Sstevel@tonic-gate 					msg(EXT, "unrecognized attr type");
37020Sstevel@tonic-gate 					break;
37030Sstevel@tonic-gate 			}
37040Sstevel@tonic-gate 			/* next attributes */
37050Sstevel@tonic-gate 			tp += attrsize;
37060Sstevel@tonic-gate 			} while (bytes > 0);
37070Sstevel@tonic-gate 			free(secp);
37080Sstevel@tonic-gate 		} else {
37090Sstevel@tonic-gate 			/* skip security info */
37100Sstevel@tonic-gate 			G_p = &Gen;
37110Sstevel@tonic-gate 			data_in(P_SKIP);
37120Sstevel@tonic-gate 		}
37130Sstevel@tonic-gate 		/*
37140Sstevel@tonic-gate 		 * We already got the file content, dont call file_in()
37150Sstevel@tonic-gate 		 * when return. The new return code(2) is used to
37160Sstevel@tonic-gate 		 *  indicate that.
37170Sstevel@tonic-gate 		 */
37180Sstevel@tonic-gate 		VERBOSE((Args & OCt), Gen.g_nam_p);
37190Sstevel@tonic-gate 		return (2);
37200Sstevel@tonic-gate 	} /* acl */
37210Sstevel@tonic-gate 
37220Sstevel@tonic-gate 	Adir = (ftype == S_IFDIR);
37230Sstevel@tonic-gate 	Aspec = (ftype == S_IFBLK || ftype == S_IFCHR || ftype == S_IFIFO);
37240Sstevel@tonic-gate 
37250Sstevel@tonic-gate 	/*
37260Sstevel@tonic-gate 	 * Skip any trailing slashes
37270Sstevel@tonic-gate 	 */
37280Sstevel@tonic-gate 	chop_endslashes(Gen.g_nam_p);
37290Sstevel@tonic-gate 	return (1);
37300Sstevel@tonic-gate }
37310Sstevel@tonic-gate 
37320Sstevel@tonic-gate /*
37330Sstevel@tonic-gate  * getname: Get file names for inclusion in the archive.  When end of file
37340Sstevel@tonic-gate  * on the input stream of file names is reached, flush the link buffer out.
37350Sstevel@tonic-gate  * For each filename, remove leading "./"s and multiple "/"s, and remove
37360Sstevel@tonic-gate  * any trailing newline "\n".  Finally, verify the existance of the file,
37370Sstevel@tonic-gate  * and call creat_hdr() to fill in the gen_hdr structure.
37380Sstevel@tonic-gate  */
37390Sstevel@tonic-gate 
37400Sstevel@tonic-gate static int
37410Sstevel@tonic-gate getname(void)
37420Sstevel@tonic-gate {
37430Sstevel@tonic-gate 	int goodfile = 0, lastchar, err;
37440Sstevel@tonic-gate 	char *s;
37450Sstevel@tonic-gate 	char *dir;
37460Sstevel@tonic-gate 
37470Sstevel@tonic-gate 	Gen.g_nam_p = Nam_p;
37480Sstevel@tonic-gate 
37490Sstevel@tonic-gate 	while (!goodfile) {
37500Sstevel@tonic-gate 		err = 0;
37510Sstevel@tonic-gate 
37520Sstevel@tonic-gate 		while ((s = fgets(Gen.g_nam_p, APATH+1, In_p))
37530Sstevel@tonic-gate 		    != NULL) {
37540Sstevel@tonic-gate 			lastchar = strlen(s) - 1;
37550Sstevel@tonic-gate 			issymlink = 0;
37560Sstevel@tonic-gate 
37570Sstevel@tonic-gate 			if (s[lastchar] != '\n') {
37580Sstevel@tonic-gate 				if (lastchar == APATH - 1) {
37590Sstevel@tonic-gate 					if (!err) {
37600Sstevel@tonic-gate 						msg(ERR,
37610Sstevel@tonic-gate 						    "%s name too long.",
37620Sstevel@tonic-gate 						    Nam_p);
37630Sstevel@tonic-gate 					}
37640Sstevel@tonic-gate 					goodfile = 0;
37650Sstevel@tonic-gate 					err = 1;
37660Sstevel@tonic-gate 				} else {
37670Sstevel@tonic-gate 					break;
37680Sstevel@tonic-gate 				}
37690Sstevel@tonic-gate 			} else {
37700Sstevel@tonic-gate 				s[lastchar] = '\0';
37710Sstevel@tonic-gate 				break;
37720Sstevel@tonic-gate 			}
37730Sstevel@tonic-gate 		}
37740Sstevel@tonic-gate 
37750Sstevel@tonic-gate 		if (s == (char *)NULL) {
37760Sstevel@tonic-gate 			if (Gen.g_dirfd != -1) {
37770Sstevel@tonic-gate 				(void) close(Gen.g_dirfd);
37780Sstevel@tonic-gate 				Gen.g_dirfd = -1;
37790Sstevel@tonic-gate 			}
37800Sstevel@tonic-gate 			if (Onecopy && (Args & OCo)) {
37810Sstevel@tonic-gate 				flush_lnks();
37820Sstevel@tonic-gate 			}
37830Sstevel@tonic-gate 			return (0);
37840Sstevel@tonic-gate 		}
37850Sstevel@tonic-gate 
37860Sstevel@tonic-gate 		while (*Gen.g_nam_p == '.' && Gen.g_nam_p[1] == '/') {
37870Sstevel@tonic-gate 			Gen.g_nam_p += 2;
37880Sstevel@tonic-gate 			while (*Gen.g_nam_p == '/')
37890Sstevel@tonic-gate 				Gen.g_nam_p++;
37900Sstevel@tonic-gate 		}
37910Sstevel@tonic-gate 
37920Sstevel@tonic-gate 		/*
37930Sstevel@tonic-gate 		 * Skip any trailing slashes
37940Sstevel@tonic-gate 		 */
37950Sstevel@tonic-gate 		chop_endslashes(Gen.g_nam_p);
37960Sstevel@tonic-gate 
37970Sstevel@tonic-gate 		/*
37980Sstevel@tonic-gate 		 * Figure out parent directory
37990Sstevel@tonic-gate 		 */
38000Sstevel@tonic-gate 
38010Sstevel@tonic-gate 		if (Gen.g_attrnam_p != (char *)NULL) {
38020Sstevel@tonic-gate 			if (Gen.g_dirfd != -1) {
38030Sstevel@tonic-gate 				(void) close(Gen.g_dirfd);
38040Sstevel@tonic-gate 			}
38050Sstevel@tonic-gate 			Gen.g_dirfd = attropen(Gen.g_attrfnam_p, ".", O_RDONLY);
38060Sstevel@tonic-gate 			if (Gen.g_dirfd == -1) {
38070Sstevel@tonic-gate 				msg(ERRN,
38080Sstevel@tonic-gate 				    "Cannot open attribute directory"
38090Sstevel@tonic-gate 				    " of file %s", Gen.g_attrfnam_p);
38100Sstevel@tonic-gate 				continue;
38110Sstevel@tonic-gate 			}
38120Sstevel@tonic-gate 		} else {
38130Sstevel@tonic-gate #ifdef O_XATTR
38140Sstevel@tonic-gate 			char dirpath[PATH_MAX];
38150Sstevel@tonic-gate 
38160Sstevel@tonic-gate 			get_parent(Gen.g_nam_p, dirpath);
38170Sstevel@tonic-gate 			if (Atflag) {
38180Sstevel@tonic-gate 				dir = dirpath;
38190Sstevel@tonic-gate 				if (Gen.g_dirfd != -1) {
38200Sstevel@tonic-gate 					(void) close(Gen.g_dirfd);
38210Sstevel@tonic-gate 				}
38220Sstevel@tonic-gate 				Gen.g_dirfd = open(dir, O_RDONLY);
38230Sstevel@tonic-gate 				if (Gen.g_dirfd == -1) {
38240Sstevel@tonic-gate 					msg(ERRN,
38250Sstevel@tonic-gate 					    "Cannot open directory %s", dir);
38260Sstevel@tonic-gate 					continue;
38270Sstevel@tonic-gate 				}
38280Sstevel@tonic-gate 			} else {
38290Sstevel@tonic-gate 				/*
38300Sstevel@tonic-gate 				 * g_dirpath is the pathname cache maintaining
38310Sstevel@tonic-gate 				 * the dirname which is currently opened.
38320Sstevel@tonic-gate 				 * We first check the g_dirpath to see if the
38330Sstevel@tonic-gate 				 * given dirname matches. If so, we don't need
38340Sstevel@tonic-gate 				 * to open the dir, but we can use the g_dirfd
38350Sstevel@tonic-gate 				 * as is if it is still available.
38360Sstevel@tonic-gate 				 */
38370Sstevel@tonic-gate 				dir = NULL;
38380Sstevel@tonic-gate 				if (Gen.g_dirpath == NULL ||
38390Sstevel@tonic-gate 				    Gen.g_dirfd == -1) {
38400Sstevel@tonic-gate 					/*
38410Sstevel@tonic-gate 					 * It's the first time or it has
38420Sstevel@tonic-gate 					 * all gone.
38430Sstevel@tonic-gate 					 */
38440Sstevel@tonic-gate 					dir = e_strdup(E_EXIT, dirpath);
38450Sstevel@tonic-gate 				} else {
38460Sstevel@tonic-gate 					if (strcmp(Gen.g_dirpath,
38470Sstevel@tonic-gate 					    dirpath) != 0) {
38480Sstevel@tonic-gate 						/* different directory */
38490Sstevel@tonic-gate 						dir = e_strdup(E_EXIT, dirpath);
38500Sstevel@tonic-gate 					}
38510Sstevel@tonic-gate 				}
38520Sstevel@tonic-gate 				if (dir != NULL) {
38530Sstevel@tonic-gate 					/*
38540Sstevel@tonic-gate 					 * We need to open the new directory.
38550Sstevel@tonic-gate 					 * discard the pathname and dirfd
38560Sstevel@tonic-gate 					 * for the previous directory.
38570Sstevel@tonic-gate 					 */
38580Sstevel@tonic-gate 					if (Gen.g_dirpath != NULL) {
38590Sstevel@tonic-gate 						free(Gen.g_dirpath);
38600Sstevel@tonic-gate 						Gen.g_dirpath = NULL;
38610Sstevel@tonic-gate 					}
38620Sstevel@tonic-gate 					if (Gen.g_dirfd != -1) {
38630Sstevel@tonic-gate 						(void) close(Gen.g_dirfd);
38640Sstevel@tonic-gate 					}
38650Sstevel@tonic-gate 					/* open the new dir */
38660Sstevel@tonic-gate 					Gen.g_dirfd = open(dir, O_RDONLY);
38670Sstevel@tonic-gate 					if (Gen.g_dirfd == -1) {
38680Sstevel@tonic-gate 						msg(ERRN, "Cannot open "
38691134Sceastha 						    "directory %s", dir);
38700Sstevel@tonic-gate 						continue;
38710Sstevel@tonic-gate 					}
38720Sstevel@tonic-gate 					Gen.g_dirpath = dir;
38730Sstevel@tonic-gate 				}
38740Sstevel@tonic-gate 			}
38750Sstevel@tonic-gate #else
38760Sstevel@tonic-gate 			Gen.g_dirfd = -1;
38770Sstevel@tonic-gate #endif
38780Sstevel@tonic-gate 		}
38790Sstevel@tonic-gate 
38800Sstevel@tonic-gate 		/* creat_hdr checks for USTAR filename length */
38810Sstevel@tonic-gate 
38820Sstevel@tonic-gate 		if (Hdr_type != USTAR && strlen(Gen.g_nam_p) >
38830Sstevel@tonic-gate 		    Max_namesz) {
38840Sstevel@tonic-gate 			if (!err) {
38850Sstevel@tonic-gate 				msg(ERR, "%s%s%s name too long.",
38860Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
38870Sstevel@tonic-gate 				    Nam_p : Gen.g_attrfnam_p,
38880Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
38890Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
38900Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
38910Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
38920Sstevel@tonic-gate 			}
38930Sstevel@tonic-gate 			goodfile = 0;
38940Sstevel@tonic-gate 			err = 1;
38950Sstevel@tonic-gate 		}
38960Sstevel@tonic-gate 
38970Sstevel@tonic-gate 		if (err) {
38980Sstevel@tonic-gate 			continue;
38990Sstevel@tonic-gate 		} else {
39000Sstevel@tonic-gate 			G_p = &Gen;
39010Sstevel@tonic-gate 			if (!LSTAT(Gen.g_dirfd, Gen.g_nam_p, &SrcSt)) {
39020Sstevel@tonic-gate 				goodfile = 1;
39030Sstevel@tonic-gate 
39040Sstevel@tonic-gate 				if ((SrcSt.st_mode & Ftype) == S_IFLNK) {
39050Sstevel@tonic-gate 					issymlink = 1;
39060Sstevel@tonic-gate 
39070Sstevel@tonic-gate 					if ((Args & OCL)) {
39080Sstevel@tonic-gate 						errno = 0;
39090Sstevel@tonic-gate 						if (STAT(Gen.g_dirfd,
39100Sstevel@tonic-gate 						    G_p->g_nam_p,
39110Sstevel@tonic-gate 						    &SrcSt) < 0) {
39120Sstevel@tonic-gate 							msg(ERRN,
39130Sstevel@tonic-gate 							    "Cannot follow"
39140Sstevel@tonic-gate 							    " \"%s%s%s\"",
39150Sstevel@tonic-gate 							    (Gen.g_attrnam_p ==
39160Sstevel@tonic-gate 							    (char *)NULL) ?
39170Sstevel@tonic-gate 							    Gen.g_nam_p :
39180Sstevel@tonic-gate 							    Gen.g_attrfnam_p,
39190Sstevel@tonic-gate 							    (Gen.g_attrnam_p ==
39200Sstevel@tonic-gate 							    (char *)NULL) ?
39210Sstevel@tonic-gate 							    "" :
39220Sstevel@tonic-gate 							    gettext(
39230Sstevel@tonic-gate 							    " Attribute "),
39240Sstevel@tonic-gate 							    (Gen.g_attrnam_p ==
39250Sstevel@tonic-gate 							    (char *)NULL) ?
39260Sstevel@tonic-gate 							    "" :
39270Sstevel@tonic-gate 							    Gen.g_attrnam_p);
39280Sstevel@tonic-gate 							goodfile = 0;
39290Sstevel@tonic-gate 						}
39300Sstevel@tonic-gate 					}
39310Sstevel@tonic-gate 				}
39320Sstevel@tonic-gate 
39330Sstevel@tonic-gate 				if (Use_old_stat) {
39340Sstevel@tonic-gate 					OldSt = convert_to_old_stat(&SrcSt,
39350Sstevel@tonic-gate 					    Gen.g_nam_p, Gen.g_attrnam_p);
39360Sstevel@tonic-gate 
39370Sstevel@tonic-gate 					if (OldSt == NULL) {
39380Sstevel@tonic-gate 						goodfile = 0;
39390Sstevel@tonic-gate 					}
39400Sstevel@tonic-gate 				}
39410Sstevel@tonic-gate 			} else {
39420Sstevel@tonic-gate 				msg(ERRN,
39430Sstevel@tonic-gate 				    "Error with fstatat() of \"%s%s%s\"",
39440Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
39450Sstevel@tonic-gate 				    Gen.g_nam_p : Gen.g_attrfnam_p,
39460Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
39470Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
39480Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
39490Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
39500Sstevel@tonic-gate 			}
39510Sstevel@tonic-gate 		}
39520Sstevel@tonic-gate 	}
39530Sstevel@tonic-gate 
39540Sstevel@tonic-gate 	/*
39550Sstevel@tonic-gate 	 * Get ACL info: dont bother allocating space if there are only
39560Sstevel@tonic-gate 	 * standard permissions, i.e. ACL count < 4
39570Sstevel@tonic-gate 	 */
39580Sstevel@tonic-gate 	if ((SrcSt.st_mode & Ftype) != S_IFLNK && Pflag) {
3959789Sahrens 		if (acl_get(Gen.g_nam_p, ACL_NO_TRIVIAL, &aclp) != 0)
39600Sstevel@tonic-gate 			msg(ERRN, "Error with acl() of \"%s\"", Gen.g_nam_p);
3961789Sahrens 	}
39620Sstevel@tonic-gate 	/* else: only traditional permissions, so proceed as usual */
39630Sstevel@tonic-gate 	if (creat_hdr())
39640Sstevel@tonic-gate 		return (1);
39650Sstevel@tonic-gate 	else return (2);
39660Sstevel@tonic-gate }
39670Sstevel@tonic-gate 
39680Sstevel@tonic-gate /*
39690Sstevel@tonic-gate  * getpats: Save any filenames/patterns specified as arguments.
39700Sstevel@tonic-gate  * Read additional filenames/patterns from the file specified by the
39710Sstevel@tonic-gate  * user.  The filenames/patterns must occur one per line.
39720Sstevel@tonic-gate  */
39730Sstevel@tonic-gate 
39740Sstevel@tonic-gate static void
39750Sstevel@tonic-gate getpats(int largc, char **largv)
39760Sstevel@tonic-gate {
39770Sstevel@tonic-gate 	char **t_pp;
39780Sstevel@tonic-gate 	size_t len;
39790Sstevel@tonic-gate 	unsigned numpat = largc, maxpat = largc + 2;
39800Sstevel@tonic-gate 
39810Sstevel@tonic-gate 	Pat_pp = e_zalloc(E_EXIT, maxpat * sizeof (char *));
39820Sstevel@tonic-gate 	t_pp = Pat_pp;
39830Sstevel@tonic-gate 	while (*largv) {
39840Sstevel@tonic-gate 		*t_pp = e_zalloc(E_EXIT, strlen(*largv) + 1);
39850Sstevel@tonic-gate 		(void) strcpy(*t_pp, *largv);
39860Sstevel@tonic-gate 		t_pp++;
39870Sstevel@tonic-gate 		largv++;
39880Sstevel@tonic-gate 	}
39890Sstevel@tonic-gate 	while (fgets(Nam_p, Max_namesz + 1, Ef_p) != (char *)NULL) {
39900Sstevel@tonic-gate 		if (numpat == maxpat - 1) {
39910Sstevel@tonic-gate 			maxpat += 10;
39920Sstevel@tonic-gate 			Pat_pp = e_realloc(E_EXIT, Pat_pp,
39930Sstevel@tonic-gate 			    maxpat * sizeof (char *));
39940Sstevel@tonic-gate 			t_pp = Pat_pp + numpat;
39950Sstevel@tonic-gate 		}
39960Sstevel@tonic-gate 		len = strlen(Nam_p); /* includes the \n */
39970Sstevel@tonic-gate 		*(Nam_p + len - 1) = '\0'; /* remove the \n */
39980Sstevel@tonic-gate 		*t_pp = e_zalloc(E_EXIT, len);
39990Sstevel@tonic-gate 		(void) strcpy(*t_pp, Nam_p);
40000Sstevel@tonic-gate 		t_pp++;
40010Sstevel@tonic-gate 		numpat++;
40020Sstevel@tonic-gate 	}
40030Sstevel@tonic-gate 	*t_pp = (char *)NULL;
40040Sstevel@tonic-gate }
40050Sstevel@tonic-gate 
40060Sstevel@tonic-gate static void
40070Sstevel@tonic-gate ioerror(int dir)
40080Sstevel@tonic-gate {
40090Sstevel@tonic-gate 	int t_errno;
40100Sstevel@tonic-gate 
40110Sstevel@tonic-gate 	t_errno = errno;
40120Sstevel@tonic-gate 	errno = 0;
40130Sstevel@tonic-gate 	if (fstat(Archive, &ArchSt) < 0)
40140Sstevel@tonic-gate 		msg(EXTN, "Error during stat() of archive");
40150Sstevel@tonic-gate 	errno = t_errno;
40160Sstevel@tonic-gate 	if ((ArchSt.st_mode & Ftype) != S_IFCHR) {
40170Sstevel@tonic-gate 		if (dir) {
40180Sstevel@tonic-gate 			if (errno == EFBIG)
40190Sstevel@tonic-gate 				msg(EXT, "ulimit reached for output file.");
40200Sstevel@tonic-gate 			else if (errno == ENOSPC)
40210Sstevel@tonic-gate 				msg(EXT, "No space left for output file.");
40220Sstevel@tonic-gate 			else
40230Sstevel@tonic-gate 				msg(EXTN, "I/O error - cannot continue");
40240Sstevel@tonic-gate 		} else
40250Sstevel@tonic-gate 			msg(EXT, "Unexpected end-of-file encountered.");
40260Sstevel@tonic-gate 	} else
40270Sstevel@tonic-gate 		msg(EXTN, "\007I/O error on \"%s\"", dir ? "output" : "input");
40280Sstevel@tonic-gate }
40290Sstevel@tonic-gate 
40300Sstevel@tonic-gate /*
40310Sstevel@tonic-gate  * matched: Determine if a filename matches the specified pattern(s).  If the
40320Sstevel@tonic-gate  * pattern is matched (the second return), return 0 if -f was specified, else
40330Sstevel@tonic-gate  * return != 0.  If the pattern is not matched (the first and third
40340Sstevel@tonic-gate  * returns), return 0 if -f was not specified, else return != 0.
40350Sstevel@tonic-gate  */
40360Sstevel@tonic-gate 
40370Sstevel@tonic-gate static int
40380Sstevel@tonic-gate matched(void)
40390Sstevel@tonic-gate {
40400Sstevel@tonic-gate 	char *str_p = G_p->g_nam_p;
40410Sstevel@tonic-gate 	char **pat_pp = Pat_pp;
40420Sstevel@tonic-gate 	int negatep, result;
40430Sstevel@tonic-gate 
40440Sstevel@tonic-gate 	/*
40450Sstevel@tonic-gate 	 * Check for attribute
40460Sstevel@tonic-gate 	 */
40470Sstevel@tonic-gate 	if (G_p->g_attrfnam_p != (char *)NULL)
40480Sstevel@tonic-gate 		str_p = G_p->g_attrfnam_p;
40490Sstevel@tonic-gate 
40500Sstevel@tonic-gate 	for (pat_pp = Pat_pp; *pat_pp; pat_pp++) {
40510Sstevel@tonic-gate 		negatep = (**pat_pp == '!');
40520Sstevel@tonic-gate 
40530Sstevel@tonic-gate 		result = fnmatch(negatep ? (*pat_pp+1) : *pat_pp, str_p, 0);
40540Sstevel@tonic-gate 
40550Sstevel@tonic-gate 		if (result != 0 && result != FNM_NOMATCH) {
40560Sstevel@tonic-gate 			msg(POST, "error matching file %s with pattern"
40570Sstevel@tonic-gate 			    " %s\n", str_p, *pat_pp);
40580Sstevel@tonic-gate 			return (Args & OCf);
40590Sstevel@tonic-gate 		}
40600Sstevel@tonic-gate 
40610Sstevel@tonic-gate 		if ((result == 0 && ! negatep) ||
40620Sstevel@tonic-gate 		    (result == FNM_NOMATCH && negatep)) {
40630Sstevel@tonic-gate 			/* match occured */
40640Sstevel@tonic-gate 			return (!(Args & OCf));
40650Sstevel@tonic-gate 		}
40660Sstevel@tonic-gate 	}
40670Sstevel@tonic-gate 	return (Args & OCf); /* not matched */
40680Sstevel@tonic-gate }
40690Sstevel@tonic-gate 
40700Sstevel@tonic-gate /*
40710Sstevel@tonic-gate  * missdir: Create missing directories for files.
40720Sstevel@tonic-gate  * (Possible future performance enhancement, if missdir is called, we know
40730Sstevel@tonic-gate  * that at least the very last directory of the path does not exist, therefore,
40740Sstevel@tonic-gate  * scan the path from the end
40750Sstevel@tonic-gate  */
40760Sstevel@tonic-gate 
40770Sstevel@tonic-gate static int
40780Sstevel@tonic-gate missdir(char *nam_p)
40790Sstevel@tonic-gate {
40800Sstevel@tonic-gate 	char *c_p;
40810Sstevel@tonic-gate 	int cnt = 2;
40820Sstevel@tonic-gate 	char *lastp;
40830Sstevel@tonic-gate 
40840Sstevel@tonic-gate 	if (*(c_p = nam_p) == '/') /* skip over 'root slash' */
40850Sstevel@tonic-gate 		c_p++;
40860Sstevel@tonic-gate 
40870Sstevel@tonic-gate 	lastp = c_p + strlen(nam_p) - 1;
40880Sstevel@tonic-gate 	if (*lastp == '/')
40890Sstevel@tonic-gate 		*lastp = '\0';
40900Sstevel@tonic-gate 
40910Sstevel@tonic-gate 	for (; *c_p; ++c_p) {
40920Sstevel@tonic-gate 		if (*c_p == '/') {
40930Sstevel@tonic-gate 			*c_p = '\0';
40940Sstevel@tonic-gate 			if (stat(nam_p, &DesSt) < 0) {
40950Sstevel@tonic-gate 				if (Args & OCd) {
40960Sstevel@tonic-gate 					cnt = mkdir(nam_p, Def_mode);
40970Sstevel@tonic-gate 					if (cnt != 0) {
40980Sstevel@tonic-gate 						*c_p = '/';
40990Sstevel@tonic-gate 						return (cnt);
41000Sstevel@tonic-gate 					}
41010Sstevel@tonic-gate 				} else {
41020Sstevel@tonic-gate 					msg(ERR, "Missing -d option.");
41030Sstevel@tonic-gate 					*c_p = '/';
41040Sstevel@tonic-gate 					return (-1);
41050Sstevel@tonic-gate 				}
41060Sstevel@tonic-gate 			}
41070Sstevel@tonic-gate 			*c_p = '/';
41080Sstevel@tonic-gate 		}
41090Sstevel@tonic-gate 	}
41100Sstevel@tonic-gate 	if (cnt == 2) /* the file already exists */
41110Sstevel@tonic-gate 		cnt = 0;
41120Sstevel@tonic-gate 	return (cnt);
41130Sstevel@tonic-gate }
41140Sstevel@tonic-gate 
41150Sstevel@tonic-gate /*
41160Sstevel@tonic-gate  * mklong: Convert two shorts into one long.  For VAX, Interdata ...
41170Sstevel@tonic-gate  */
41180Sstevel@tonic-gate 
41190Sstevel@tonic-gate static long
41200Sstevel@tonic-gate mklong(short v[])
41210Sstevel@tonic-gate {
41220Sstevel@tonic-gate 
41230Sstevel@tonic-gate 	union swpbuf swp_b;
41240Sstevel@tonic-gate 
41250Sstevel@tonic-gate 	swp_b.s_word = 1;
41260Sstevel@tonic-gate 	if (swp_b.s_byte[0]) {
41270Sstevel@tonic-gate 		swp_b.s_half[0] = v[1];
41280Sstevel@tonic-gate 		swp_b.s_half[1] = v[0];
41290Sstevel@tonic-gate 	} else {
41300Sstevel@tonic-gate 		swp_b.s_half[0] = v[0];
41310Sstevel@tonic-gate 		swp_b.s_half[1] = v[1];
41320Sstevel@tonic-gate 	}
41330Sstevel@tonic-gate 	return (swp_b.s_word);
41340Sstevel@tonic-gate }
41350Sstevel@tonic-gate 
41360Sstevel@tonic-gate /*
41370Sstevel@tonic-gate  * mkshort: Convert a long into 2 shorts, for VAX, Interdata ...
41380Sstevel@tonic-gate  */
41390Sstevel@tonic-gate 
41400Sstevel@tonic-gate static void
41410Sstevel@tonic-gate mkshort(short sval[], long v)
41420Sstevel@tonic-gate {
41430Sstevel@tonic-gate 	union swpbuf *swp_p, swp_b;
41440Sstevel@tonic-gate 
41450Sstevel@tonic-gate 	/* LINTED alignment */
41460Sstevel@tonic-gate 	swp_p = (union swpbuf *)sval;
41470Sstevel@tonic-gate 	swp_b.s_word = 1;
41480Sstevel@tonic-gate 	if (swp_b.s_byte[0]) {
41490Sstevel@tonic-gate 		swp_b.s_word = v;
41500Sstevel@tonic-gate 		swp_p->s_half[0] = swp_b.s_half[1];
41510Sstevel@tonic-gate 		swp_p->s_half[1] = swp_b.s_half[0];
41520Sstevel@tonic-gate 	} else {
41530Sstevel@tonic-gate 		swp_b.s_word = v;
41540Sstevel@tonic-gate 		swp_p->s_half[0] = swp_b.s_half[0];
41550Sstevel@tonic-gate 		swp_p->s_half[1] = swp_b.s_half[1];
41560Sstevel@tonic-gate 	}
41570Sstevel@tonic-gate }
41580Sstevel@tonic-gate 
41590Sstevel@tonic-gate /*
41600Sstevel@tonic-gate  * msg: Print either a message (no error) (POST), an error message with or
41610Sstevel@tonic-gate  * without the errno (ERRN or ERR), or print an error message with or without
41620Sstevel@tonic-gate  * the errno and exit (EXTN or EXT).
41630Sstevel@tonic-gate  */
41640Sstevel@tonic-gate 
41650Sstevel@tonic-gate static void
41660Sstevel@tonic-gate msg(int severity, const char *fmt, ...)
41670Sstevel@tonic-gate {
41680Sstevel@tonic-gate 	FILE *file_p;
41690Sstevel@tonic-gate 	va_list ap;
41700Sstevel@tonic-gate 
41710Sstevel@tonic-gate 	if ((Args & OCV) && Verbcnt) { /* clear current line of dots */
41720Sstevel@tonic-gate 		(void) fputc('\n', Out_p);
41730Sstevel@tonic-gate 		Verbcnt = 0;
41740Sstevel@tonic-gate 	}
41750Sstevel@tonic-gate 	va_start(ap, fmt);
41760Sstevel@tonic-gate 	if (severity == POST)
41770Sstevel@tonic-gate 		file_p = Out_p;
41780Sstevel@tonic-gate 	else
41790Sstevel@tonic-gate 		if (severity == EPOST)
41800Sstevel@tonic-gate 			file_p = Err_p;
41810Sstevel@tonic-gate 		else {
41820Sstevel@tonic-gate 			file_p = Err_p;
41830Sstevel@tonic-gate 			Error_cnt++;
41840Sstevel@tonic-gate 		}
41850Sstevel@tonic-gate 	(void) fflush(Out_p);
41860Sstevel@tonic-gate 	(void) fflush(Err_p);
41870Sstevel@tonic-gate 	if ((severity != POST) && (severity != EPOST))
41880Sstevel@tonic-gate 		(void) fprintf(file_p, "cpio: ");
41890Sstevel@tonic-gate 
41900Sstevel@tonic-gate 	/* gettext replaces version of string */
41910Sstevel@tonic-gate 
41920Sstevel@tonic-gate 	(void) vfprintf(file_p, gettext(fmt), ap);
41930Sstevel@tonic-gate 	if (severity == ERRN || severity == EXTN) {
41940Sstevel@tonic-gate 		(void) fprintf(file_p, ", errno %d, ", errno);
41950Sstevel@tonic-gate 		perror("");
41960Sstevel@tonic-gate 	} else
41970Sstevel@tonic-gate 		(void) fprintf(file_p, "\n");
41980Sstevel@tonic-gate 	(void) fflush(file_p);
41990Sstevel@tonic-gate 	va_end(ap);
42000Sstevel@tonic-gate 	if (severity == EXT || severity == EXTN) {
42010Sstevel@tonic-gate 		(void) fprintf(file_p, gettext("%d errors\n"), Error_cnt);
42020Sstevel@tonic-gate 		exit(EXIT_CODE);
42030Sstevel@tonic-gate 	}
42040Sstevel@tonic-gate }
42050Sstevel@tonic-gate 
42060Sstevel@tonic-gate /*
42070Sstevel@tonic-gate  * openout: Open files for output and set all necessary information.
42080Sstevel@tonic-gate  * If the u option is set (unconditionally overwrite existing files),
42090Sstevel@tonic-gate  * and the current file exists, get a temporary file name from mktemp(3C),
42100Sstevel@tonic-gate  * link the temporary file to the existing file, and remove the existing file.
42110Sstevel@tonic-gate  * Finally either creat(2), mkdir(2) or mknod(2) as appropriate.
42120Sstevel@tonic-gate  *
42130Sstevel@tonic-gate  */
42140Sstevel@tonic-gate 
42150Sstevel@tonic-gate static int
42160Sstevel@tonic-gate openout(int dirfd)
42170Sstevel@tonic-gate {
42180Sstevel@tonic-gate 	char *nam_p;
42190Sstevel@tonic-gate 	int cnt, result;
42200Sstevel@tonic-gate 
42210Sstevel@tonic-gate 	Do_rename = 0;	/* creat_tmp() may reset this */
42220Sstevel@tonic-gate 
42230Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
42240Sstevel@tonic-gate 		nam_p = G_p->g_attrnam_p;
42250Sstevel@tonic-gate 	} else {
42260Sstevel@tonic-gate 		if (Args & OCp) {
42270Sstevel@tonic-gate 			nam_p = Fullnam_p;
42280Sstevel@tonic-gate 		} else {
42290Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
42300Sstevel@tonic-gate 		}
42310Sstevel@tonic-gate 	}
42320Sstevel@tonic-gate 
42330Sstevel@tonic-gate 
42340Sstevel@tonic-gate 	if ((Max_filesz != RLIM_INFINITY) &&
42350Sstevel@tonic-gate 	    (Max_filesz < (G_p->g_filesz >> 9))) {
42360Sstevel@tonic-gate 		/* ... divided by 512 ... */
42370Sstevel@tonic-gate 		msg(ERR, "Skipping \"%s%s%s\": exceeds ulimit by %lld bytes",
42380Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
42390Sstevel@tonic-gate 		    nam_p : G_p->g_attrfnam_p,
42400Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
42410Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
42420Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
42430Sstevel@tonic-gate 		    "" : nam_p,
42440Sstevel@tonic-gate 		    (off_t)(G_p->g_filesz - (Max_filesz << 9)));
42450Sstevel@tonic-gate 		return (-1);
42460Sstevel@tonic-gate 	}
42470Sstevel@tonic-gate 
42480Sstevel@tonic-gate 	if (LSTAT(dirfd, nam_p, &DesSt) == 0) {
42490Sstevel@tonic-gate 		/*
42500Sstevel@tonic-gate 		 * A file by the same name exists.  Move it to a temporary
42510Sstevel@tonic-gate 		 * file.
42520Sstevel@tonic-gate 		 */
42530Sstevel@tonic-gate 
42540Sstevel@tonic-gate 		if (creat_tmp(nam_p) < 0) {
42550Sstevel@tonic-gate 			/*
42560Sstevel@tonic-gate 			 * We weren't able to create the temp file.  Report
42570Sstevel@tonic-gate 			 * failure.
42580Sstevel@tonic-gate 			 */
42590Sstevel@tonic-gate 
42600Sstevel@tonic-gate 			return (-1);
42610Sstevel@tonic-gate 		}
42620Sstevel@tonic-gate 	}
42630Sstevel@tonic-gate 
42640Sstevel@tonic-gate 	if (Do_rename) {
42650Sstevel@tonic-gate 		/* nam_p was changed by creat_tmp() above. */
42660Sstevel@tonic-gate 
42670Sstevel@tonic-gate 		if (Args & OCp) {
42680Sstevel@tonic-gate 			if (G_p->g_attrnam_p != (char *)NULL) {
42690Sstevel@tonic-gate 				nam_p = Attrfile_p;
42700Sstevel@tonic-gate 			} else {
42710Sstevel@tonic-gate 				nam_p = Fullnam_p;
42720Sstevel@tonic-gate 			}
42730Sstevel@tonic-gate 		} else {
42740Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
42750Sstevel@tonic-gate 		}
42760Sstevel@tonic-gate 	}
42770Sstevel@tonic-gate 
42780Sstevel@tonic-gate 	/*
42790Sstevel@tonic-gate 	 * This pile tries to create the file directly, and, if there is a
42800Sstevel@tonic-gate 	 * problem, creates missing directories, and then tries to create the
42810Sstevel@tonic-gate 	 * file again.  Two strikes and you're out.
42820Sstevel@tonic-gate 	 *
42830Sstevel@tonic-gate 	 * On XATTR system, the directory has already been created by
42840Sstevel@tonic-gate 	 * open_dirfd(), so error shouldn't happen in the loop. However,
42850Sstevel@tonic-gate 	 * on non-XATTR system, symlink/open may fail with ENOENT. In such
42860Sstevel@tonic-gate 	 * case, we go to create missing directories.
42870Sstevel@tonic-gate 	 */
42880Sstevel@tonic-gate 
42890Sstevel@tonic-gate 	cnt = 0;
42900Sstevel@tonic-gate 
42910Sstevel@tonic-gate 	do {
42920Sstevel@tonic-gate 		errno = 0;
42930Sstevel@tonic-gate 
42940Sstevel@tonic-gate 		if (Hdr_type == TAR && Thdr_p->tbuf.t_typeflag == SYMTYPE) {
42950Sstevel@tonic-gate 			/* The archive file is a TAR symlink. */
42960Sstevel@tonic-gate 			if ((result =
42970Sstevel@tonic-gate 			    symlink(Thdr_p->tbuf.t_linkname, nam_p)) >= 0) {
42980Sstevel@tonic-gate 				cnt = 0;
42990Sstevel@tonic-gate 				if (Over_p != NULL) {
43000Sstevel@tonic-gate 					(void) unlinkat(dirfd,
43010Sstevel@tonic-gate 					    get_component(Over_p), 0);
43020Sstevel@tonic-gate 					*Over_p = '\0';
43030Sstevel@tonic-gate 				}
43040Sstevel@tonic-gate 				break;
43050Sstevel@tonic-gate 			} else if (errno != ENOENT) {
43060Sstevel@tonic-gate 				/* The attempt to symlink failed. */
43070Sstevel@tonic-gate 				msg(ERRN,
43080Sstevel@tonic-gate 				    "Cannot create symbolic link \"%s\" -> "
43090Sstevel@tonic-gate 				    "\"%s\"",
43100Sstevel@tonic-gate 				    Thdr_p->tbuf.t_linkname, nam_p);
43110Sstevel@tonic-gate 
43120Sstevel@tonic-gate 				if (*Over_p != '\0') {
43130Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
43140Sstevel@tonic-gate 				}
43150Sstevel@tonic-gate 				return (-1);
43160Sstevel@tonic-gate 			}
43170Sstevel@tonic-gate 		} else if (Hdr_type == BAR && bar_linkflag == SYMTYPE) {
43180Sstevel@tonic-gate 			if ((result = symlink(bar_linkname, nam_p)) >= 0) {
43190Sstevel@tonic-gate 				cnt = 0;
43200Sstevel@tonic-gate 				if (Over_p != NULL) {
43210Sstevel@tonic-gate 					(void) unlinkat(dirfd,
43220Sstevel@tonic-gate 					    get_component(Over_p), 0);
43230Sstevel@tonic-gate 					*Over_p = '\0';
43240Sstevel@tonic-gate 				}
43250Sstevel@tonic-gate 				break;
43260Sstevel@tonic-gate 			} else if (errno != ENOENT) {
43270Sstevel@tonic-gate 				/* The attempt to symlink failed. */
43280Sstevel@tonic-gate 				msg(ERRN,
43290Sstevel@tonic-gate 				    "Cannot create symbolic link \"%s\" -> "
43300Sstevel@tonic-gate 				    "\"%s\"",
43310Sstevel@tonic-gate 				    bar_linkname, nam_p);
43320Sstevel@tonic-gate 				if (*Over_p != '\0') {
43330Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
43340Sstevel@tonic-gate 				}
43350Sstevel@tonic-gate 				return (-1);
43360Sstevel@tonic-gate 			}
43370Sstevel@tonic-gate 		} else if ((G_p->g_mode & Ftype) == S_IFLNK) {
43380Sstevel@tonic-gate 			if ((!(Args & OCp)) && !(Hdr_type == USTAR)) {
43390Sstevel@tonic-gate 				(void) strncpy(Symlnk_p,
43400Sstevel@tonic-gate 				    Buffr.b_out_p, G_p->g_filesz);
43410Sstevel@tonic-gate 				*(Symlnk_p + G_p->g_filesz) = '\0';
43420Sstevel@tonic-gate 			} else if ((!(Args & OCp)) && (Hdr_type == USTAR)) {
43430Sstevel@tonic-gate 				Symlnk_p[NAMSIZ] = '\0';
43440Sstevel@tonic-gate 				(void) strncpy(Symlnk_p,
43450Sstevel@tonic-gate 				    &Thdr_p->tbuf.t_linkname[0], NAMSIZ);
43460Sstevel@tonic-gate 			}
43470Sstevel@tonic-gate 			if ((result = symlink(Symlnk_p, nam_p)) >= 0) {
43480Sstevel@tonic-gate 				cnt = 0;
43490Sstevel@tonic-gate 				if (Over_p != NULL) {
43500Sstevel@tonic-gate 					(void) unlinkat(dirfd,
43510Sstevel@tonic-gate 					    get_component(Over_p), 0);
43520Sstevel@tonic-gate 					*Over_p = '\0';
43530Sstevel@tonic-gate 				}
43540Sstevel@tonic-gate 				break;
43550Sstevel@tonic-gate 			} else if (errno != ENOENT) {
43560Sstevel@tonic-gate 				/* The attempt to symlink failed. */
43570Sstevel@tonic-gate 				msg(ERRN,
43580Sstevel@tonic-gate 				    "Cannot create symbolic link \"%s\" -> "
43590Sstevel@tonic-gate 				    "\"%s\"",
43600Sstevel@tonic-gate 				    Symlnk_p, nam_p);
43610Sstevel@tonic-gate 
43620Sstevel@tonic-gate 				if (*Over_p != '\0') {
43630Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
43640Sstevel@tonic-gate 				}
43650Sstevel@tonic-gate 				return (-1);
43660Sstevel@tonic-gate 			}
43670Sstevel@tonic-gate 		} else {
43680Sstevel@tonic-gate 			if ((result = openat(dirfd, get_component(nam_p),
43690Sstevel@tonic-gate 			    O_CREAT|O_RDWR|O_TRUNC, (int)G_p->g_mode)) >= 0) {
43700Sstevel@tonic-gate 				/* acl support */
4371789Sahrens 				acl_is_set = 0;
43720Sstevel@tonic-gate 				if (Pflag && aclp != NULL) {
4373789Sahrens 					if (facl_set(result, aclp) < 0) {
43740Sstevel@tonic-gate 						msg(ERRN,
43750Sstevel@tonic-gate 						    "\"%s\": failed to set acl",
43760Sstevel@tonic-gate 						    nam_p);
43770Sstevel@tonic-gate 					} else {
4378789Sahrens 						acl_is_set = 1;
43790Sstevel@tonic-gate 					}
4380789Sahrens 					acl_free(aclp);
43810Sstevel@tonic-gate 					aclp = NULL;
43820Sstevel@tonic-gate 				}
43830Sstevel@tonic-gate 				cnt = 0;
43840Sstevel@tonic-gate 				break;
43850Sstevel@tonic-gate 			} else if (errno != ENOENT) {
43860Sstevel@tonic-gate 				/* The attempt to open failed. */
43870Sstevel@tonic-gate 				msg(ERRN, "Cannot open file \"%s\"", nam_p);
43880Sstevel@tonic-gate 				if (*Over_p != '\0') {
43890Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
43900Sstevel@tonic-gate 				}
43910Sstevel@tonic-gate 				return (-1);
43920Sstevel@tonic-gate 			}
43930Sstevel@tonic-gate 		}
43940Sstevel@tonic-gate 		cnt++;
43950Sstevel@tonic-gate 	} while (cnt < 2 && missdir(nam_p) == 0);
43960Sstevel@tonic-gate 
43970Sstevel@tonic-gate 	switch (cnt) {
43980Sstevel@tonic-gate 	case 0:
43990Sstevel@tonic-gate 		if ((Args & OCi) && (Hdr_type == USTAR)) {
44000Sstevel@tonic-gate 			setpasswd(nam_p);
44010Sstevel@tonic-gate 		}
44020Sstevel@tonic-gate 		if ((G_p->g_mode & Ftype) == S_IFLNK ||
44030Sstevel@tonic-gate 		    (Hdr_type == BAR && bar_linkflag == SYMTYPE)) {
44041134Sceastha 			if (Args & OCR) {
44051134Sceastha 				if (fchownat(dirfd,
44061134Sceastha 				    get_component(nam_p),
44071134Sceastha 				    (int)Rpw_p->pw_uid,
44081134Sceastha 				    (int)Rpw_p->pw_gid,
44091134Sceastha 				    AT_SYMLINK_NOFOLLOW) < 0) {
44100Sstevel@tonic-gate 					msg(ERRN,
44110Sstevel@tonic-gate 					    "Error during chown() of "
44120Sstevel@tonic-gate 					    "\"%s%s%s\"",
44131134Sceastha 					    (G_p->g_attrnam_p ==
44141134Sceastha 					    (char *)NULL) ?
44150Sstevel@tonic-gate 					    nam_p : G_p->g_attrfnam_p,
44161134Sceastha 					    (G_p->g_attrnam_p ==
44171134Sceastha 					    (char *)NULL) ?
44180Sstevel@tonic-gate 					    "" : gettext(" Attribute "),
44191134Sceastha 					    (G_p->g_attrnam_p ==
44201134Sceastha 					    (char *)NULL) ?
44210Sstevel@tonic-gate 					    "" : nam_p);
44220Sstevel@tonic-gate 				}
44231134Sceastha 			} else if ((fchownat(dirfd, get_component(nam_p),
44241134Sceastha 			    (int)G_p->g_uid, (int)G_p->g_gid,
44251134Sceastha 			    AT_SYMLINK_NOFOLLOW) < 0) && privileged) {
44260Sstevel@tonic-gate 				msg(ERRN,
44270Sstevel@tonic-gate 				    "Error during chown() of \"%s%s%s\"",
44280Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
44290Sstevel@tonic-gate 				    nam_p : G_p->g_attrfnam_p,
44300Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
44310Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
44320Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
44330Sstevel@tonic-gate 				    "" : nam_p);
44340Sstevel@tonic-gate 			}
44350Sstevel@tonic-gate 		}
44360Sstevel@tonic-gate 		break;
44370Sstevel@tonic-gate 
44380Sstevel@tonic-gate 	case 1:
44390Sstevel@tonic-gate 		if (Do_rename) {
44400Sstevel@tonic-gate 			msg(ERRN, "Cannot create directory for \"%s%s%s\"",
44410Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? Over_p :
44420Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
44430Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
44440Sstevel@tonic-gate 			    gettext(" Attribute "),
44450Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" : Over_p);
44460Sstevel@tonic-gate 		} else {
44470Sstevel@tonic-gate 			msg(ERRN, "Cannot create directory for \"%s%s%s\"",
44480Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? nam_p :
44490Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
44500Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
44510Sstevel@tonic-gate 			    gettext(" Attribute "),
44520Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" : nam_p);
44530Sstevel@tonic-gate 		}
44540Sstevel@tonic-gate 		break;
44550Sstevel@tonic-gate 
44560Sstevel@tonic-gate 	case 2:
44570Sstevel@tonic-gate 		if (Do_rename) {
44580Sstevel@tonic-gate 			msg(ERRN, "Cannot create \"%s%s%s\"",
44590Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? Over_p :
44600Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
44610Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
44620Sstevel@tonic-gate 			    gettext(" Attribute "),
44630Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
44640Sstevel@tonic-gate 			    Over_p);
44650Sstevel@tonic-gate 		} else {
44660Sstevel@tonic-gate 			msg(ERRN, "Cannot create \"%s%s%s\"",
44670Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? nam_p :
44680Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
44690Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
44700Sstevel@tonic-gate 			    gettext(" Attribute "),
44710Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
44720Sstevel@tonic-gate 			    nam_p);
44730Sstevel@tonic-gate 		}
44740Sstevel@tonic-gate 		break;
44750Sstevel@tonic-gate 
44760Sstevel@tonic-gate 	default:
44770Sstevel@tonic-gate 		msg(EXT, "Impossible case.");
44780Sstevel@tonic-gate 	}
44790Sstevel@tonic-gate 
44800Sstevel@tonic-gate 	Finished = 0;
44810Sstevel@tonic-gate 	return (result);
44820Sstevel@tonic-gate }
44830Sstevel@tonic-gate 
44840Sstevel@tonic-gate /*
44850Sstevel@tonic-gate  * read_hdr: Transfer headers from the selected format
44860Sstevel@tonic-gate  * in the archive I/O buffer to the generic structure.
44870Sstevel@tonic-gate  */
44880Sstevel@tonic-gate 
44890Sstevel@tonic-gate static
44900Sstevel@tonic-gate int
44910Sstevel@tonic-gate read_hdr(int hdr)
44920Sstevel@tonic-gate {
44930Sstevel@tonic-gate 	int rv = NONE;
44940Sstevel@tonic-gate 	major_t maj, rmaj;
44950Sstevel@tonic-gate 	minor_t min, rmin;
44960Sstevel@tonic-gate 	char tmpnull;
44970Sstevel@tonic-gate 	static int bar_read_cnt = 0;
44980Sstevel@tonic-gate 
44990Sstevel@tonic-gate 	if (hdr != BAR) {
45000Sstevel@tonic-gate 		if (Buffr.b_end_p != (Buffr.b_out_p + Hdrsz)) {
45010Sstevel@tonic-gate 			tmpnull = *(Buffr.b_out_p + Hdrsz);
45020Sstevel@tonic-gate 			*(Buffr.b_out_p + Hdrsz) = '\0';
45030Sstevel@tonic-gate 		}
45040Sstevel@tonic-gate 	}
45050Sstevel@tonic-gate 
45060Sstevel@tonic-gate 	switch (hdr) {
45070Sstevel@tonic-gate 	case BIN:
45080Sstevel@tonic-gate 		(void) memcpy(&Hdr, Buffr.b_out_p, HDRSZ);
45090Sstevel@tonic-gate 		if (Hdr.h_magic == (short)CMN_BBS) {
45100Sstevel@tonic-gate 			swap((char *)&Hdr, HDRSZ);
45110Sstevel@tonic-gate 		}
45120Sstevel@tonic-gate 		Gen.g_magic = Hdr.h_magic;
45130Sstevel@tonic-gate 		Gen.g_mode = Hdr.h_mode;
45140Sstevel@tonic-gate 		Gen.g_uid = Hdr.h_uid;
45150Sstevel@tonic-gate 		Gen.g_gid = Hdr.h_gid;
45160Sstevel@tonic-gate 		Gen.g_nlink = Hdr.h_nlink;
45170Sstevel@tonic-gate 		Gen.g_mtime = mklong(Hdr.h_mtime);
45180Sstevel@tonic-gate 		Gen.g_ino = Hdr.h_ino;
45190Sstevel@tonic-gate 		Gen.g_dev = Hdr.h_dev;
45200Sstevel@tonic-gate 		Gen.g_rdev = Hdr.h_rdev;
45210Sstevel@tonic-gate 		Gen.g_cksum = 0L;
45220Sstevel@tonic-gate 		Gen.g_filesz = (off_t)mklong(Hdr.h_filesize);
45230Sstevel@tonic-gate 		Gen.g_namesz = Hdr.h_namesize;
45240Sstevel@tonic-gate 		rv = BIN;
45250Sstevel@tonic-gate 		break;
45260Sstevel@tonic-gate 	case CHR:
45270Sstevel@tonic-gate 		if (sscanf(Buffr.b_out_p,
45280Sstevel@tonic-gate 		    "%6lo%6lo%6lo%6lo%6lo%6lo%6lo%6lo%11lo%6o%11llo",
45290Sstevel@tonic-gate 		    &Gen.g_magic, &Gen.g_dev, &Gen.g_ino, &Gen.g_mode,
45300Sstevel@tonic-gate 		    &Gen.g_uid, &Gen.g_gid, &Gen.g_nlink, &Gen.g_rdev,
45310Sstevel@tonic-gate 		    (ulong_t *)&Gen.g_mtime, (uint_t *)&Gen.g_namesz,
45320Sstevel@tonic-gate 		    (u_off_t *)&Gen.g_filesz) == CHR_CNT) {
45330Sstevel@tonic-gate 			rv = CHR;
45340Sstevel@tonic-gate #define	cpioMAJOR(x)	(int)(((unsigned)x >> 8) & 0x7F)
45350Sstevel@tonic-gate #define	cpioMINOR(x)	(int)(x & 0xFF)
45360Sstevel@tonic-gate 			maj = cpioMAJOR(Gen.g_dev);
45370Sstevel@tonic-gate 			rmaj = cpioMAJOR(Gen.g_rdev);
45380Sstevel@tonic-gate 			min = cpioMINOR(Gen.g_dev);
45390Sstevel@tonic-gate 			rmin = cpioMINOR(Gen.g_rdev);
45400Sstevel@tonic-gate 			if (Use_old_stat) {
45410Sstevel@tonic-gate 				/* needs error checking */
45420Sstevel@tonic-gate 				Gen.g_dev = (maj << 8) | min;
45430Sstevel@tonic-gate 				Gen.g_rdev = (rmaj << 8) | rmin;
45440Sstevel@tonic-gate 			} else {
45450Sstevel@tonic-gate 				Gen.g_dev = makedev(maj, min);
45460Sstevel@tonic-gate 				Gen.g_rdev = makedev(rmaj, rmin);
45470Sstevel@tonic-gate 			}
45480Sstevel@tonic-gate 		}
45490Sstevel@tonic-gate 		break;
45500Sstevel@tonic-gate 	case ASC:
45510Sstevel@tonic-gate 	case CRC:
45520Sstevel@tonic-gate 		if (sscanf(Buffr.b_out_p,
45530Sstevel@tonic-gate 		    "%6lx%8lx%8lx%8lx%8lx%8lx%8lx%8llx%8x%8x%8x%8x%8x%8lx",
45540Sstevel@tonic-gate 		    &Gen.g_magic, &Gen.g_ino, &Gen.g_mode, &Gen.g_uid,
45550Sstevel@tonic-gate 		    &Gen.g_gid, &Gen.g_nlink, &Gen.g_mtime,
45560Sstevel@tonic-gate 		    (u_off_t *)&Gen.g_filesz, (uint_t *)&maj, (uint_t *)&min,
45570Sstevel@tonic-gate 		    (uint_t *)&rmaj, (uint_t *)&rmin, (uint_t *)&Gen.g_namesz,
45580Sstevel@tonic-gate 		    &Gen.g_cksum) == ASC_CNT) {
45590Sstevel@tonic-gate 			Gen.g_dev = makedev(maj, min);
45600Sstevel@tonic-gate 			Gen.g_rdev = makedev(rmaj, rmin);
45610Sstevel@tonic-gate 			rv = hdr;
45620Sstevel@tonic-gate 		}
45630Sstevel@tonic-gate 		break;
45640Sstevel@tonic-gate 	case USTAR: /* TAR and USTAR */
45650Sstevel@tonic-gate 		if (*Buffr.b_out_p == '\0') {
45660Sstevel@tonic-gate 			*Gen.g_nam_p = '\0';
45670Sstevel@tonic-gate 			nambuf[0] = '\0';
45680Sstevel@tonic-gate 		} else {
45690Sstevel@tonic-gate 			Thdr_p = (union tblock *)Buffr.b_out_p;
45700Sstevel@tonic-gate 			Gen.g_nam_p[0] = '\0';
45710Sstevel@tonic-gate 			(void) strncpy((char *)&nambuf,
45720Sstevel@tonic-gate 			    Thdr_p->tbuf.t_name, NAMSIZ);
45730Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mode, "%8lo",
45740Sstevel@tonic-gate 			    &Gen.g_mode);
45750Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_uid, "%8lo", &Gen.g_uid);
45760Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_gid, "%8lo", &Gen.g_gid);
45770Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_size, "%11llo",
45780Sstevel@tonic-gate 			    (u_off_t *)&Gen.g_filesz);
45790Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mtime, "%12lo",
45800Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_mtime);
45810Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_cksum, "%8lo",
45820Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_cksum);
45830Sstevel@tonic-gate 			if (Thdr_p->tbuf.t_linkname[0] != (char)NULL)
45840Sstevel@tonic-gate 				Gen.g_nlink = 1;
45850Sstevel@tonic-gate 			else
45860Sstevel@tonic-gate 				Gen.g_nlink = 0;
45870Sstevel@tonic-gate 
45880Sstevel@tonic-gate 			switch (Thdr_p->tbuf.t_typeflag) {
45890Sstevel@tonic-gate 			case SYMTYPE:
45900Sstevel@tonic-gate 				/* Symbolic Link */
45910Sstevel@tonic-gate 				Gen.g_nlink = 2;
45920Sstevel@tonic-gate 				break;
45930Sstevel@tonic-gate 			case CHRTYPE:
45940Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFCHR);
45950Sstevel@tonic-gate 				break;
45960Sstevel@tonic-gate 			case BLKTYPE:
45970Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFBLK);
45980Sstevel@tonic-gate 				break;
45990Sstevel@tonic-gate 			case DIRTYPE:
46000Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFDIR);
46010Sstevel@tonic-gate 				break;
46020Sstevel@tonic-gate 			case FIFOTYPE:
46030Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFIFO);
46040Sstevel@tonic-gate 				break;
46050Sstevel@tonic-gate 			}
46060Sstevel@tonic-gate 
46070Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_magic, "%8lo",
46080Sstevel@tonic-gate 			    /* LINTED alignment */
46090Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_tmagic);
46100Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_version, "%8lo",
46110Sstevel@tonic-gate 			    /* LINTED alignment */
46120Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_version);
46130Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_uname, "%32s",
46140Sstevel@tonic-gate 			    (char *)&Gen.g_uname);
46150Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_gname, "%32s",
46160Sstevel@tonic-gate 			    (char *)&Gen.g_gname);
46170Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_devmajor, "%8lo",
46180Sstevel@tonic-gate 			    &Gen.g_dev);
46190Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_devminor, "%8lo",
46200Sstevel@tonic-gate 			    &Gen.g_rdev);
46210Sstevel@tonic-gate 			(void) strncpy((char *)&prebuf,
46220Sstevel@tonic-gate 			    Thdr_p->tbuf.t_prefix, PRESIZ);
46230Sstevel@tonic-gate 			Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
46240Sstevel@tonic-gate 			Gen.g_dev = makedev(maj, min);
46250Sstevel@tonic-gate 		}
46260Sstevel@tonic-gate 		rv = USTAR;
46270Sstevel@tonic-gate 		break;
46280Sstevel@tonic-gate 	case TAR:
46290Sstevel@tonic-gate 		if (*Buffr.b_out_p == '\0') {
46300Sstevel@tonic-gate 			*Gen.g_nam_p = '\0';
46310Sstevel@tonic-gate 			nambuf[0] = '\0';
46320Sstevel@tonic-gate 		} else {
46330Sstevel@tonic-gate 			Thdr_p = (union tblock *)Buffr.b_out_p;
46340Sstevel@tonic-gate 			Gen.g_nam_p[0] = '\0';
46350Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mode, "%lo", &Gen.g_mode);
46360Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_uid, "%lo", &Gen.g_uid);
46370Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_gid, "%lo", &Gen.g_gid);
46380Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_size, "%llo",
46390Sstevel@tonic-gate 			    (u_off_t *)&Gen.g_filesz);
46400Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mtime, "%lo",
46410Sstevel@tonic-gate 			    &Gen.g_mtime);
46420Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_cksum, "%lo",
46430Sstevel@tonic-gate 			    &Gen.g_cksum);
46440Sstevel@tonic-gate 			if (Thdr_p->tbuf.t_typeflag == '1')	/* hardlink */
46450Sstevel@tonic-gate 				Gen.g_nlink = 1;
46460Sstevel@tonic-gate 			else
46470Sstevel@tonic-gate 				Gen.g_nlink = 0;
46480Sstevel@tonic-gate 			(void) strncpy(Gen.g_nam_p,
46490Sstevel@tonic-gate 			    Thdr_p->tbuf.t_name, NAMSIZ);
46500Sstevel@tonic-gate 			Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
46510Sstevel@tonic-gate 			(void) strcpy(nambuf, Gen.g_nam_p);
46520Sstevel@tonic-gate 		}
46530Sstevel@tonic-gate 		rv = TAR;
46540Sstevel@tonic-gate 		break;
46550Sstevel@tonic-gate 	case BAR:
46560Sstevel@tonic-gate 		if (Bar_vol_num == 0 && bar_read_cnt == 0) {
46570Sstevel@tonic-gate 			read_bar_vol_hdr();
46580Sstevel@tonic-gate 			bar_read_cnt++;
46590Sstevel@tonic-gate 		}
46600Sstevel@tonic-gate 		else
46610Sstevel@tonic-gate 			read_bar_file_hdr();
46620Sstevel@tonic-gate 		rv = BAR;
46630Sstevel@tonic-gate 		break;
46640Sstevel@tonic-gate 	default:
46650Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
46660Sstevel@tonic-gate 	}
46670Sstevel@tonic-gate 
46680Sstevel@tonic-gate 	if (hdr != BAR) {
46690Sstevel@tonic-gate 		if (Buffr.b_end_p != (Buffr.b_out_p + Hdrsz))
46700Sstevel@tonic-gate 			*(Buffr.b_out_p + Hdrsz) = tmpnull;
46710Sstevel@tonic-gate 	}
46720Sstevel@tonic-gate 
46730Sstevel@tonic-gate 	return (rv);
46740Sstevel@tonic-gate }
46750Sstevel@tonic-gate 
46760Sstevel@tonic-gate /*
46770Sstevel@tonic-gate  * reclaim: Reclaim linked file structure storage.
46780Sstevel@tonic-gate  */
46790Sstevel@tonic-gate 
46800Sstevel@tonic-gate static void
46810Sstevel@tonic-gate reclaim(struct Lnk *p)
46820Sstevel@tonic-gate {
46830Sstevel@tonic-gate 	p->L_bck_p->L_nxt_p = p->L_nxt_p;
46840Sstevel@tonic-gate 	p->L_nxt_p->L_bck_p = p->L_bck_p;
46850Sstevel@tonic-gate 
46860Sstevel@tonic-gate 	while (p != NULL) {
46870Sstevel@tonic-gate 		struct Lnk *new_p = p->L_lnk_p;
46880Sstevel@tonic-gate 
46890Sstevel@tonic-gate 		free(p->L_gen.g_nam_p);
46900Sstevel@tonic-gate 		free(p);
46910Sstevel@tonic-gate 		p = new_p;
46920Sstevel@tonic-gate 	}
46930Sstevel@tonic-gate }
46940Sstevel@tonic-gate 
46950Sstevel@tonic-gate /*
46960Sstevel@tonic-gate  * rstbuf: Reset the I/O buffer, move incomplete potential headers to
46970Sstevel@tonic-gate  * the front of the buffer and force bread() to refill the buffer.  The
46980Sstevel@tonic-gate  * return value from bread() is returned (to identify I/O errors).  On the
46990Sstevel@tonic-gate  * 3B2, reads must begin on a word boundary, therefore, with the -i option,
47000Sstevel@tonic-gate  * any remaining bytes in the buffer must be moved to the base of the buffer
47010Sstevel@tonic-gate  * in such a way that the destination locations of subsequent reads are
47020Sstevel@tonic-gate  * word aligned.
47030Sstevel@tonic-gate  */
47040Sstevel@tonic-gate 
47050Sstevel@tonic-gate static void
47060Sstevel@tonic-gate rstbuf(void)
47070Sstevel@tonic-gate {
47080Sstevel@tonic-gate 	int pad;
47090Sstevel@tonic-gate 
47100Sstevel@tonic-gate 	if ((Args & OCi) || Append) {
47110Sstevel@tonic-gate 		if (Buffr.b_out_p != Buffr.b_base_p) {
47120Sstevel@tonic-gate 			pad = ((Buffr.b_cnt + FULLWD) & ~FULLWD);
47130Sstevel@tonic-gate 			Buffr.b_in_p = Buffr.b_base_p + pad;
47140Sstevel@tonic-gate 			pad -= Buffr.b_cnt;
47150Sstevel@tonic-gate 			(void) memcpy(Buffr.b_base_p + pad, Buffr.b_out_p,
47160Sstevel@tonic-gate 			    (int)Buffr.b_cnt);
47170Sstevel@tonic-gate 			Buffr.b_out_p = Buffr.b_base_p + pad;
47180Sstevel@tonic-gate 		}
47190Sstevel@tonic-gate 		if (bfill() < 0)
47200Sstevel@tonic-gate 			msg(EXT, "Unexpected end-of-archive encountered.");
47210Sstevel@tonic-gate 	} else { /* OCo */
47220Sstevel@tonic-gate 		(void) memcpy(Buffr.b_base_p, Buffr.b_out_p, (int)Buffr.b_cnt);
47230Sstevel@tonic-gate 		Buffr.b_out_p = Buffr.b_base_p;
47240Sstevel@tonic-gate 		Buffr.b_in_p = Buffr.b_base_p + Buffr.b_cnt;
47250Sstevel@tonic-gate 	}
47260Sstevel@tonic-gate }
47270Sstevel@tonic-gate 
47280Sstevel@tonic-gate static void
47290Sstevel@tonic-gate setpasswd(char *nam)
47300Sstevel@tonic-gate {
47310Sstevel@tonic-gate 	if ((dpasswd = getpwnam(&Gen.g_uname[0])) == (struct passwd *)NULL) {
47320Sstevel@tonic-gate 		msg(EPOST, "cpio: problem reading passwd entry");
47330Sstevel@tonic-gate 		msg(EPOST, "cpio: %s: owner not changed", nam);
47340Sstevel@tonic-gate 		if (Gen.g_uid == UID_NOBODY && S_ISREG(Gen.g_mode))
47350Sstevel@tonic-gate 			Gen.g_mode &= ~S_ISUID;
47360Sstevel@tonic-gate 	} else
47370Sstevel@tonic-gate 		Gen.g_uid = dpasswd->pw_uid;
47380Sstevel@tonic-gate 
47390Sstevel@tonic-gate 	if ((dgroup = getgrnam(&Gen.g_gname[0])) == (struct group *)NULL) {
47400Sstevel@tonic-gate 		msg(EPOST, "cpio: problem reading group entry");
47410Sstevel@tonic-gate 		msg(EPOST, "cpio: %s: group not changed", nam);
47420Sstevel@tonic-gate 		if (Gen.g_gid == GID_NOBODY && S_ISREG(Gen.g_mode))
47430Sstevel@tonic-gate 			Gen.g_mode &= ~S_ISGID;
47440Sstevel@tonic-gate 	} else
47450Sstevel@tonic-gate 		Gen.g_gid = dgroup->gr_gid;
47460Sstevel@tonic-gate 	G_p = &Gen;
47470Sstevel@tonic-gate }
47480Sstevel@tonic-gate 
47490Sstevel@tonic-gate /*
47500Sstevel@tonic-gate  * rstfiles:  Perform final changes to the file.  If the -u option is set,
47510Sstevel@tonic-gate  * and overwrite == U_OVER, remove the temporary file, else if overwrite
47520Sstevel@tonic-gate  * == U_KEEP, unlink the current file, and restore the existing version
47530Sstevel@tonic-gate  * of the file.  In addition, where appropriate, set the access or modification
47540Sstevel@tonic-gate  * times, change the owner and change the modes of the file.
47550Sstevel@tonic-gate  *
47560Sstevel@tonic-gate  * Note that if Do_rename is set, then the roles of original and temporary
47570Sstevel@tonic-gate  * file are reversed. If all went well, we will rename() the temporary file
47580Sstevel@tonic-gate  * over the original in order to accomodate potentially executing files.
47590Sstevel@tonic-gate  */
47600Sstevel@tonic-gate static void
47610Sstevel@tonic-gate rstfiles(int over, int dirfd)
47620Sstevel@tonic-gate {
47630Sstevel@tonic-gate 	char *inam_p, *onam_p, *nam_p;
47640Sstevel@tonic-gate 	int error;
47650Sstevel@tonic-gate 
47660Sstevel@tonic-gate 	if (Args & OCp) {
47670Sstevel@tonic-gate 		if (G_p->g_attrnam_p == (char *)NULL) {
47680Sstevel@tonic-gate 			nam_p = Fullnam_p;
47690Sstevel@tonic-gate 		} else {
47700Sstevel@tonic-gate 			nam_p = G_p->g_attrnam_p;
47710Sstevel@tonic-gate 		}
47720Sstevel@tonic-gate 	} else {
47730Sstevel@tonic-gate 		if (Gen.g_nlink > (ulong_t)0) {
47740Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
47750Sstevel@tonic-gate 		} else {
47760Sstevel@tonic-gate 			nam_p = Gen.g_nam_p;
47770Sstevel@tonic-gate 		}
47780Sstevel@tonic-gate 	}
47790Sstevel@tonic-gate 	if (Gen.g_attrnam_p != (char *)NULL) {
47800Sstevel@tonic-gate 		nam_p = Gen.g_attrnam_p;
47810Sstevel@tonic-gate 	}
47820Sstevel@tonic-gate 
47830Sstevel@tonic-gate 	if ((Args & OCi) && (Hdr_type == USTAR)) {
47840Sstevel@tonic-gate 		setpasswd(nam_p);
47850Sstevel@tonic-gate 	}
47860Sstevel@tonic-gate 	if (over == U_KEEP && *Over_p != '\0') {
47870Sstevel@tonic-gate 		if (Do_rename) {
47880Sstevel@tonic-gate 			msg(POST, "Restoring existing \"%s%s%s\"",
47890Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
47900Sstevel@tonic-gate 			    Over_p : Fullnam_p,
47910Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
47920Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
47930Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
47940Sstevel@tonic-gate 			    "" : Over_p);
47950Sstevel@tonic-gate 		} else {
47960Sstevel@tonic-gate 			msg(POST, "Restoring existing \"%s%s%s\"",
47970Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
47980Sstevel@tonic-gate 			    nam_p : Fullnam_p,
47990Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
48000Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
48010Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
48020Sstevel@tonic-gate 			    "" : nam_p);
48030Sstevel@tonic-gate 		}
48040Sstevel@tonic-gate 
48050Sstevel@tonic-gate 		/* delete what we just built */
48060Sstevel@tonic-gate 		(void) unlinkat(dirfd, get_component(nam_p), 0);
48070Sstevel@tonic-gate 
48080Sstevel@tonic-gate 		/* If the old file needs restoring, do the necessary links */
48090Sstevel@tonic-gate 		if (Do_rename) {
48100Sstevel@tonic-gate 			char *tmp_ptr;
48110Sstevel@tonic-gate 
48120Sstevel@tonic-gate 			if (Args & OCp) {
48130Sstevel@tonic-gate 				tmp_ptr = Fullnam_p;
48140Sstevel@tonic-gate 				Fullnam_p = Over_p;
48150Sstevel@tonic-gate 			} else {
48160Sstevel@tonic-gate 				tmp_ptr = G_p->g_nam_p;
48170Sstevel@tonic-gate 				G_p->g_nam_p = Over_p;
48180Sstevel@tonic-gate 			}
48190Sstevel@tonic-gate 			Over_p = tmp_ptr;
48200Sstevel@tonic-gate 
48210Sstevel@tonic-gate 			Do_rename = 0;	/* names now have original values */
48220Sstevel@tonic-gate 		} else {
48230Sstevel@tonic-gate 			if (rename(Over_p, nam_p) < 0) {
48240Sstevel@tonic-gate 				if (link(Over_p, nam_p) < 0) {
48250Sstevel@tonic-gate 					msg(EXTN,
48260Sstevel@tonic-gate 					    "Cannot recover original version"
48270Sstevel@tonic-gate 					    " of \"%s%s%s\"",
48280Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
48290Sstevel@tonic-gate 					    nam_p : Fullnam_p,
48300Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
48310Sstevel@tonic-gate 					    "" : gettext(" Attribute "),
48320Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
48330Sstevel@tonic-gate 					    "" : nam_p);
48340Sstevel@tonic-gate 				}
48350Sstevel@tonic-gate 				if (unlinkat(dirfd, get_component(Over_p), 0)) {
48360Sstevel@tonic-gate 					msg(ERRN,
48370Sstevel@tonic-gate 					    "Cannot remove temp file "
48380Sstevel@tonic-gate 					    "\"%s%s%s\"",
48390Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
48400Sstevel@tonic-gate 					    Over_p : Fullnam_p,
48410Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
48420Sstevel@tonic-gate 					    "" : gettext(" Attribute "),
48430Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
48440Sstevel@tonic-gate 					    "" : Over_p);
48450Sstevel@tonic-gate 				}
48460Sstevel@tonic-gate 			}
48470Sstevel@tonic-gate 		}
48480Sstevel@tonic-gate 		*Over_p = '\0';
48490Sstevel@tonic-gate 		return;
48500Sstevel@tonic-gate 	} else if (over == U_OVER && *Over_p != '\0') {
48510Sstevel@tonic-gate 		if (Do_rename) {
48520Sstevel@tonic-gate 			char *tmp_ptr;
48530Sstevel@tonic-gate 
48540Sstevel@tonic-gate 			(void) renameat(dirfd, get_component(nam_p),
48550Sstevel@tonic-gate 			    dirfd, get_component(Over_p));
48560Sstevel@tonic-gate 			if (Args & OCp) {
48570Sstevel@tonic-gate 				if (G_p->g_attrnam_p == (char *)NULL) {
48580Sstevel@tonic-gate 					tmp_ptr = Fullnam_p;
48590Sstevel@tonic-gate 					Fullnam_p = Over_p;
48600Sstevel@tonic-gate 					Over_p = tmp_ptr;
48610Sstevel@tonic-gate 				} else {
48620Sstevel@tonic-gate 					/*
48630Sstevel@tonic-gate 					 * Over_p is pointing at g_attrnam_p
48640Sstevel@tonic-gate 					 * which must be preserved.
48650Sstevel@tonic-gate 					 *
48660Sstevel@tonic-gate 					 * We don't want the tmp_ptr and so
48670Sstevel@tonic-gate 					 * on to throw away our only copy of
48680Sstevel@tonic-gate 					 * the name.
48690Sstevel@tonic-gate 					 */
48700Sstevel@tonic-gate 					Over_p = Attrfile_p;
48710Sstevel@tonic-gate 				}
48720Sstevel@tonic-gate 			} else {
48730Sstevel@tonic-gate 				tmp_ptr = G_p->g_nam_p;
48740Sstevel@tonic-gate 				G_p->g_nam_p = Over_p;
48750Sstevel@tonic-gate 				Over_p = tmp_ptr;
48760Sstevel@tonic-gate 			}
48770Sstevel@tonic-gate 			Do_rename = 0;	/* names now have original values */
48780Sstevel@tonic-gate 		} else {
48790Sstevel@tonic-gate 			if (unlinkat(dirfd, get_component(Over_p), 0) < 0) {
48800Sstevel@tonic-gate 				msg(ERRN,
48810Sstevel@tonic-gate 				    "Cannot unlink() temp file \"%s%s%s\"",
48820Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
48830Sstevel@tonic-gate 				    Over_p : Fullnam_p,
48840Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
48850Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
48860Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
48870Sstevel@tonic-gate 				    "" : Over_p);
48880Sstevel@tonic-gate 			}
48890Sstevel@tonic-gate 		}
48900Sstevel@tonic-gate 		*Over_p = '\0';
48910Sstevel@tonic-gate 	}
48920Sstevel@tonic-gate 	if (Args & OCp) {
48930Sstevel@tonic-gate 		if (G_p->g_attrnam_p != (char *)NULL) {
48940Sstevel@tonic-gate 			inam_p = G_p->g_attrfnam_p;
48950Sstevel@tonic-gate 			onam_p = G_p->g_attrnam_p;
48960Sstevel@tonic-gate 		} else {
48970Sstevel@tonic-gate 			inam_p = Nam_p;
48980Sstevel@tonic-gate 			onam_p = Fullnam_p;
48990Sstevel@tonic-gate 		}
49000Sstevel@tonic-gate 	} else /* OCi only uses onam_p, OCo only uses inam_p */
49010Sstevel@tonic-gate 		if (G_p->g_attrnam_p != (char *)NULL) {
49020Sstevel@tonic-gate 			inam_p = onam_p = G_p->g_attrnam_p;
49030Sstevel@tonic-gate 		} else {
49040Sstevel@tonic-gate 			inam_p = onam_p = G_p->g_nam_p;
49050Sstevel@tonic-gate 		}
49060Sstevel@tonic-gate 
49070Sstevel@tonic-gate 	/*
49081134Sceastha 	 * Change the owner, time, and mode to those of the file
49091134Sceastha 	 * originally created in the archive.  Note: time and
49101134Sceastha 	 * mode do not need to be restored for a symbolic link
49111134Sceastha 	 * since rstfiles() is not called when the archived file
49121134Sceastha 	 * is a symlink.
49130Sstevel@tonic-gate 	 */
49140Sstevel@tonic-gate 	if (!(Args & OCo)) {
49151134Sceastha 		if (Args & OCR) {
49161134Sceastha 			if (fchownat(dirfd, get_component(onam_p),
49171134Sceastha 			    Rpw_p->pw_uid, Rpw_p->pw_gid,
49181134Sceastha 			    AT_SYMLINK_NOFOLLOW) < 0) {
49191134Sceastha 				msg(ERRN, "Cannot chown() \"%s%s%s\"",
49201134Sceastha 				    onam_p,
49211134Sceastha 				    (G_p->g_attrnam_p == (char *)NULL) ?
49221134Sceastha 				    "" : gettext(" Attribute "),
49231134Sceastha 				    (G_p->g_attrnam_p == (char *)NULL) ?
49241134Sceastha 				    "" : onam_p);
49251134Sceastha 			}
49261134Sceastha 		} else {
49271134Sceastha 			if ((fchownat(dirfd, get_component(onam_p),
49281134Sceastha 			    G_p->g_uid, G_p->g_gid,
49291134Sceastha 			    AT_SYMLINK_NOFOLLOW) < 0) && privileged) {
49301134Sceastha 				msg(ERRN, "Cannot chown() \"%s%s%s\"",
49311134Sceastha 				    onam_p,
49320Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
49330Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
49340Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
49350Sstevel@tonic-gate 				    "" : onam_p);
49360Sstevel@tonic-gate 			}
49370Sstevel@tonic-gate 		}
49381134Sceastha 
49391134Sceastha 		if (Args & OCm) {
49401134Sceastha 			set_tym(dirfd, get_component(onam_p),
49411134Sceastha 			    G_p->g_mtime, G_p->g_mtime);
49421134Sceastha 		}
49431134Sceastha 
49441134Sceastha 		/* Acl was not set, so we must chmod */
49451134Sceastha 		if (!acl_is_set) {
49461134Sceastha 			mode_t orig_mask, new_mask;
49471134Sceastha 
49481134Sceastha 			/*
49491134Sceastha 			 * use fchmod for attributes, since
49501134Sceastha 			 * we known they are always regular
49511134Sceastha 			 * files, whereas when it isn't an
49521134Sceastha 			 * attribute it could be for a fifo
49531134Sceastha 			 * or something other that we don't
49541134Sceastha 			 * open and don't have a valid Ofile
49551134Sceastha 			 * for.
49561134Sceastha 			 */
49571134Sceastha 			if (privileged) {
49581134Sceastha 				new_mask = G_p->g_mode;
49591134Sceastha 			} else {
49601134Sceastha 				orig_mask = umask(0);
49611134Sceastha 				new_mask = G_p->g_mode & ~orig_mask;
49621134Sceastha 			}
49631134Sceastha 
49641134Sceastha 			if (G_p->g_attrnam_p != (char *)NULL) {
49651134Sceastha 				error = fchmod(Ofile, new_mask);
49661134Sceastha 			} else {
49671134Sceastha 				error = chmod(onam_p, new_mask);
49681134Sceastha 			}
49691134Sceastha 			if (error < 0) {
49701134Sceastha 				msg(ERRN,
49711134Sceastha 				    "Cannot chmod() \"%s%s%s\"",
49721134Sceastha 				    (G_p->g_attrnam_p == (char *)NULL) ?
49731134Sceastha 				    onam_p : G_p->g_attrfnam_p,
49741134Sceastha 				    (G_p->g_attrnam_p == (char *)NULL) ?
49751134Sceastha 				    "" : gettext(" Attribute "),
49761134Sceastha 				    (G_p->g_attrnam_p == (char *)NULL) ?
49771134Sceastha 				    "" : onam_p);
49781134Sceastha 			}
49791134Sceastha 			if (!privileged) {
49801134Sceastha 				(void) umask(orig_mask);
49811134Sceastha 			}
49820Sstevel@tonic-gate 		}
49830Sstevel@tonic-gate 	}
49840Sstevel@tonic-gate 
49850Sstevel@tonic-gate 	if (!(Args & OCi) && (Args & OCa)) {
49860Sstevel@tonic-gate 		/*
49870Sstevel@tonic-gate 		 * Use dirfd since we are updating original file
49880Sstevel@tonic-gate 		 * and not just created file
49890Sstevel@tonic-gate 		 */
49900Sstevel@tonic-gate 		set_tym(G_p->g_dirfd, get_component(inam_p),
49910Sstevel@tonic-gate 		    (ulong_t)SrcSt.st_atime, (ulong_t)SrcSt.st_mtime);
49920Sstevel@tonic-gate 	}
49930Sstevel@tonic-gate }
49940Sstevel@tonic-gate 
49950Sstevel@tonic-gate /*
49960Sstevel@tonic-gate  * scan4trail: Scan the archive looking for the trailer.
49970Sstevel@tonic-gate  * When found, back the archive up over the trailer and overwrite
49980Sstevel@tonic-gate  * the trailer with the files to be added to the archive.
49990Sstevel@tonic-gate  */
50000Sstevel@tonic-gate 
50010Sstevel@tonic-gate static void
50020Sstevel@tonic-gate scan4trail(void)
50030Sstevel@tonic-gate {
50040Sstevel@tonic-gate 	int rv;
50050Sstevel@tonic-gate 	off_t off1, off2;
50060Sstevel@tonic-gate 
50070Sstevel@tonic-gate 	Append = 1;
50080Sstevel@tonic-gate 	Hdr_type = NONE;
50090Sstevel@tonic-gate 	G_p = (struct gen_hdr *)NULL;
50100Sstevel@tonic-gate 	while (gethdr()) {
50110Sstevel@tonic-gate 		G_p = &Gen;
50120Sstevel@tonic-gate 		data_in(P_SKIP);
50130Sstevel@tonic-gate 	}
50140Sstevel@tonic-gate 	off1 = Buffr.b_cnt;
50150Sstevel@tonic-gate 	off2 = Bufsize - (Buffr.b_cnt % Bufsize);
50160Sstevel@tonic-gate 	Buffr.b_out_p = Buffr.b_in_p = Buffr.b_base_p;
50170Sstevel@tonic-gate 	Buffr.b_cnt = (off_t)0;
50180Sstevel@tonic-gate 	if (lseek(Archive, -(off1 + off2), SEEK_REL) < 0)
50190Sstevel@tonic-gate 		msg(EXTN, "Unable to append to this archive");
50200Sstevel@tonic-gate 	if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0)
50210Sstevel@tonic-gate 		msg(EXTN, "Cannot append to this archive");
50220Sstevel@tonic-gate 	if (lseek(Archive, (off_t)-rv, SEEK_REL) < 0)
50230Sstevel@tonic-gate 		msg(EXTN, "Unable to append to this archive");
50240Sstevel@tonic-gate 	Buffr.b_cnt = off2;
50250Sstevel@tonic-gate 	Buffr.b_in_p = Buffr.b_base_p + Buffr.b_cnt;
50260Sstevel@tonic-gate 	Append = 0;
50270Sstevel@tonic-gate }
50280Sstevel@tonic-gate 
50290Sstevel@tonic-gate /*
50300Sstevel@tonic-gate  * setup:  Perform setup and initialization functions.  Parse the options
50310Sstevel@tonic-gate  * using getopt(3C), call ckopts to check the options and initialize various
50320Sstevel@tonic-gate  * structures and pointers.  Specifically, for the -i option, save any
50330Sstevel@tonic-gate  * patterns, for the -o option, check (via stat(2)) the archive, and for
50340Sstevel@tonic-gate  * the -p option, validate the destination directory.
50350Sstevel@tonic-gate  */
50360Sstevel@tonic-gate 
50370Sstevel@tonic-gate static void
50380Sstevel@tonic-gate setup(int largc, char **largv)
50390Sstevel@tonic-gate {
50400Sstevel@tonic-gate 	extern int optind;
50410Sstevel@tonic-gate 	extern char *optarg;
50420Sstevel@tonic-gate 
50430Sstevel@tonic-gate #if defined(O_XATTR)
50440Sstevel@tonic-gate #ifdef WAITAROUND
50450Sstevel@tonic-gate 	char	*opts_p = "zabcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6@";
50460Sstevel@tonic-gate #else
50470Sstevel@tonic-gate 	char	*opts_p = "abcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6@";
50480Sstevel@tonic-gate #endif
50490Sstevel@tonic-gate #else
50500Sstevel@tonic-gate #ifdef WAITAROUND
50510Sstevel@tonic-gate 	char	*opts_p = "zabcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6";
50520Sstevel@tonic-gate #else
50530Sstevel@tonic-gate 	char	*opts_p = "abcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6";
50540Sstevel@tonic-gate #endif
50550Sstevel@tonic-gate #endif
50560Sstevel@tonic-gate 
50570Sstevel@tonic-gate 	char   *dupl_p = "Only one occurrence of -%c allowed";
50580Sstevel@tonic-gate 	int option;
50590Sstevel@tonic-gate 	int blk_cnt, blk_cnt_max;
50600Sstevel@tonic-gate 	struct rlimit rlim;
50610Sstevel@tonic-gate 
50620Sstevel@tonic-gate 	/* Remember the native page size. */
50630Sstevel@tonic-gate 
50640Sstevel@tonic-gate 	PageSize = sysconf(_SC_PAGESIZE);
50650Sstevel@tonic-gate 
50660Sstevel@tonic-gate 	if (PageSize == -1) {
50670Sstevel@tonic-gate 		/*
50680Sstevel@tonic-gate 		 * This sysconf call will almost certainly never fail.  The
50690Sstevel@tonic-gate 		 * symbol PAGESIZE itself resolves to the above sysconf call,
50700Sstevel@tonic-gate 		 * so we should go ahead and define our own constant.
50710Sstevel@tonic-gate 		 */
50720Sstevel@tonic-gate 		PageSize = 8192;
50730Sstevel@tonic-gate 	}
50740Sstevel@tonic-gate 
50750Sstevel@tonic-gate 	Hdr_type = BIN;
50760Sstevel@tonic-gate 	Max_offset = (off_t)(BIN_OFFSET_MAX);
50770Sstevel@tonic-gate 	Efil_p = Hdr_p = Own_p = IOfil_p = NULL;
50780Sstevel@tonic-gate 	while ((option = getopt(largc, largv, opts_p)) != EOF) {
50790Sstevel@tonic-gate 		switch (option) {
50800Sstevel@tonic-gate #ifdef WAITAROUND
50810Sstevel@tonic-gate 		case 'z':
50820Sstevel@tonic-gate 			/* rendezvous with the debugger */
50830Sstevel@tonic-gate 			waitaround = 1;
50840Sstevel@tonic-gate 			break;
50850Sstevel@tonic-gate #endif
50860Sstevel@tonic-gate 		case 'a':	/* reset access time */
50870Sstevel@tonic-gate 			Args |= OCa;
50880Sstevel@tonic-gate 			break;
50890Sstevel@tonic-gate 		case 'b':	/* swap bytes and halfwords */
50900Sstevel@tonic-gate 			Args |= OCb;
50910Sstevel@tonic-gate 			break;
50920Sstevel@tonic-gate 		case 'c':	/* select character header */
50930Sstevel@tonic-gate 			Args |= OCc;
50940Sstevel@tonic-gate 			Hdr_type = ASC;
50950Sstevel@tonic-gate 			Max_namesz = APATH;
50960Sstevel@tonic-gate 			Onecopy = 1;
50970Sstevel@tonic-gate 			break;
50980Sstevel@tonic-gate 		case 'd':	/* create directories as needed */
50990Sstevel@tonic-gate 			Args |= OCd;
51000Sstevel@tonic-gate 			break;
51010Sstevel@tonic-gate 		case 'f':	/* select files not in patterns */
51020Sstevel@tonic-gate 			Args |= OCf;
51030Sstevel@tonic-gate 			break;
51040Sstevel@tonic-gate 		case 'i':	/* "copy in" */
51050Sstevel@tonic-gate 			Args |= OCi;
51060Sstevel@tonic-gate 			Archive = 0;
51070Sstevel@tonic-gate 			break;
51080Sstevel@tonic-gate 		case 'k':	/* retry after I/O errors */
51090Sstevel@tonic-gate 			Args |= OCk;
51100Sstevel@tonic-gate 			break;
51110Sstevel@tonic-gate 		case 'l':	/* link files when possible */
51120Sstevel@tonic-gate 			Args |= OCl;
51130Sstevel@tonic-gate 			break;
51140Sstevel@tonic-gate 		case 'm':	/* retain modification time */
51150Sstevel@tonic-gate 			Args |= OCm;
51160Sstevel@tonic-gate 			break;
51170Sstevel@tonic-gate 		case 'o':	/* "copy out" */
51180Sstevel@tonic-gate 			Args |= OCo;
51190Sstevel@tonic-gate 			Archive = 1;
51200Sstevel@tonic-gate 			break;
51210Sstevel@tonic-gate 		case 'p':	/* "pass" */
51220Sstevel@tonic-gate 			Max_namesz = APATH;
51230Sstevel@tonic-gate 			Args |= OCp;
51240Sstevel@tonic-gate 			break;
51250Sstevel@tonic-gate 		case 'r':	/* rename files interactively */
51260Sstevel@tonic-gate 			Args |= OCr;
51270Sstevel@tonic-gate 			break;
51280Sstevel@tonic-gate 		case 's':	/* swap bytes */
51290Sstevel@tonic-gate 			Args |= OCs;
51300Sstevel@tonic-gate 			break;
51310Sstevel@tonic-gate 		case 't':	/* table of contents */
51320Sstevel@tonic-gate 			Args |= OCt;
51330Sstevel@tonic-gate 			break;
51340Sstevel@tonic-gate 		case 'u':	/* copy unconditionally */
51350Sstevel@tonic-gate 			Args |= OCu;
51360Sstevel@tonic-gate 			break;
51370Sstevel@tonic-gate 		case 'v':	/* verbose - print file names */
51380Sstevel@tonic-gate 			Args |= OCv;
51390Sstevel@tonic-gate 			break;
51400Sstevel@tonic-gate 		case 'A':	/* append to existing archive */
51410Sstevel@tonic-gate 			Args |= OCA;
51420Sstevel@tonic-gate 			break;
51430Sstevel@tonic-gate 		case 'B':	/* set block size to 5120 bytes */
51440Sstevel@tonic-gate 			Args |= OCB;
51450Sstevel@tonic-gate 			Bufsize = 5120;
51460Sstevel@tonic-gate 			break;
51470Sstevel@tonic-gate 		case 'C':	/* set arbitrary block size */
51480Sstevel@tonic-gate 			if (Args & OCC)
51490Sstevel@tonic-gate 				msg(ERR, dupl_p, 'C');
51500Sstevel@tonic-gate 			else {
51510Sstevel@tonic-gate 				Args |= OCC;
51520Sstevel@tonic-gate 				Bufsize = atoi(optarg);
51530Sstevel@tonic-gate 			}
51540Sstevel@tonic-gate 			break;
51550Sstevel@tonic-gate 		case 'D':
51560Sstevel@tonic-gate 			Dflag = 1;
51570Sstevel@tonic-gate 			break;
51580Sstevel@tonic-gate 		case 'E':	/* alternate file for pattern input */
51590Sstevel@tonic-gate 			if (Args & OCE)
51600Sstevel@tonic-gate 				msg(ERR, dupl_p, 'E');
51610Sstevel@tonic-gate 			else {
51620Sstevel@tonic-gate 				Args |= OCE;
51630Sstevel@tonic-gate 				Efil_p = optarg;
51640Sstevel@tonic-gate 			}
51650Sstevel@tonic-gate 			break;
51660Sstevel@tonic-gate 		case 'H':	/* select header type */
51670Sstevel@tonic-gate 			if (Args & OCH)
51680Sstevel@tonic-gate 				msg(ERR, dupl_p, 'H');
51690Sstevel@tonic-gate 			else {
51700Sstevel@tonic-gate 				Args |= OCH;
51710Sstevel@tonic-gate 				Hdr_p = optarg;
51720Sstevel@tonic-gate 			}
51730Sstevel@tonic-gate 			break;
51740Sstevel@tonic-gate 		case 'I':	/* alternate file for archive input */
51750Sstevel@tonic-gate 			if (Args & OCI)
51760Sstevel@tonic-gate 				msg(ERR, dupl_p, 'I');
51770Sstevel@tonic-gate 			else {
51780Sstevel@tonic-gate 				Args |= OCI;
51790Sstevel@tonic-gate 				IOfil_p = optarg;
51800Sstevel@tonic-gate 			}
51810Sstevel@tonic-gate 			break;
51820Sstevel@tonic-gate 		case 'L':	/* follow symbolic links */
51830Sstevel@tonic-gate 			Args |= OCL;
51840Sstevel@tonic-gate 			break;
51850Sstevel@tonic-gate 		case 'M':	/* specify new end-of-media message */
51860Sstevel@tonic-gate 			if (Args & OCM)
51870Sstevel@tonic-gate 				msg(ERR, dupl_p, 'M');
51880Sstevel@tonic-gate 			else {
51890Sstevel@tonic-gate 				Args |= OCM;
51900Sstevel@tonic-gate 				Eom_p = optarg;
51910Sstevel@tonic-gate 			}
51920Sstevel@tonic-gate 			break;
51930Sstevel@tonic-gate 		case 'O':	/* alternate file for archive output */
51940Sstevel@tonic-gate 			if (Args & OCO)
51950Sstevel@tonic-gate 				msg(ERR, dupl_p, 'O');
51960Sstevel@tonic-gate 			else {
51970Sstevel@tonic-gate 				Args |= OCO;
51980Sstevel@tonic-gate 				IOfil_p = optarg;
51990Sstevel@tonic-gate 			}
52000Sstevel@tonic-gate 			break;
52010Sstevel@tonic-gate 		case 'P':	/* preserve acls */
52020Sstevel@tonic-gate 			Args |= OCP;
52030Sstevel@tonic-gate 			Pflag++;
52040Sstevel@tonic-gate 			break;
52050Sstevel@tonic-gate 		case 'R':	/* change owner/group of files */
52060Sstevel@tonic-gate 			if (Args & OCR)
52070Sstevel@tonic-gate 				msg(ERR, dupl_p, 'R');
52080Sstevel@tonic-gate 			else {
52090Sstevel@tonic-gate 				Args |= OCR;
52100Sstevel@tonic-gate 				Own_p = optarg;
52110Sstevel@tonic-gate 			}
52120Sstevel@tonic-gate 			break;
52130Sstevel@tonic-gate 		case 'S':	/* swap halfwords */
52140Sstevel@tonic-gate 			Args |= OCS;
52150Sstevel@tonic-gate 			break;
52160Sstevel@tonic-gate 		case 'V':	/* print a dot '.' for each file */
52170Sstevel@tonic-gate 			Args |= OCV;
52180Sstevel@tonic-gate 			break;
52190Sstevel@tonic-gate 		case '6':	/* for old, sixth-edition files */
52200Sstevel@tonic-gate 			Args |= OC6;
52210Sstevel@tonic-gate 			Ftype = SIXTH;
52220Sstevel@tonic-gate 			break;
52230Sstevel@tonic-gate #if defined(O_XATTR)
52240Sstevel@tonic-gate 		case '@':
52250Sstevel@tonic-gate 			Atflag++;
52260Sstevel@tonic-gate 			break;
52270Sstevel@tonic-gate #endif
52280Sstevel@tonic-gate 		default:
52290Sstevel@tonic-gate 			Error_cnt++;
52300Sstevel@tonic-gate 		} /* option */
52310Sstevel@tonic-gate 	} /* (option = getopt(largc, largv, opts_p)) != EOF */
52320Sstevel@tonic-gate 
52330Sstevel@tonic-gate #ifdef WAITAROUND
52340Sstevel@tonic-gate 	if (waitaround) {
52350Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Rendezvous with cpio on pid"
52360Sstevel@tonic-gate 		    " %d\n"), getpid());
52370Sstevel@tonic-gate 
52380Sstevel@tonic-gate 		while (waitaround) {
52390Sstevel@tonic-gate 			(void) sleep(10);
52400Sstevel@tonic-gate 		}
52410Sstevel@tonic-gate 	}
52420Sstevel@tonic-gate #endif
52430Sstevel@tonic-gate 
52440Sstevel@tonic-gate 	largc -= optind;
52450Sstevel@tonic-gate 	largv += optind;
52460Sstevel@tonic-gate 	ckopts(Args);
52470Sstevel@tonic-gate 	if (!Error_cnt) {
52480Sstevel@tonic-gate 		Empty = e_valloc(E_EXIT, TARSZ);
52490Sstevel@tonic-gate 		if (Args & OCr) {
52500Sstevel@tonic-gate 			Renam_p = e_zalloc(E_EXIT, APATH + 1);
52510Sstevel@tonic-gate 			Renametmp_p = e_zalloc(E_EXIT, APATH + 1);
52520Sstevel@tonic-gate 		}
52530Sstevel@tonic-gate 		Symlnk_p = e_zalloc(E_EXIT, APATH);
52540Sstevel@tonic-gate 		Over_p = e_zalloc(E_EXIT, APATH);
52550Sstevel@tonic-gate 		Nam_p = e_zalloc(E_EXIT, APATH + 1);
52560Sstevel@tonic-gate 		if (Args & OCp) {
52570Sstevel@tonic-gate 			Savenam_p = e_zalloc(E_EXIT, APATH + 1);
52580Sstevel@tonic-gate 		}
52590Sstevel@tonic-gate 		Fullnam_p = e_zalloc(E_EXIT, APATH);
52600Sstevel@tonic-gate 		Lnknam_p = e_zalloc(E_EXIT, APATH);
52610Sstevel@tonic-gate 		Gen.g_nam_p = Nam_p;
52620Sstevel@tonic-gate 		if ((Fullnam_p = getcwd((char *)NULL, APATH)) == (char *)NULL)
52630Sstevel@tonic-gate 			msg(EXT, "Unable to determine current directory.");
52640Sstevel@tonic-gate 		if (Args & OCi) {
52650Sstevel@tonic-gate 			if (largc > 0) /* save patterns for -i option, if any */
52660Sstevel@tonic-gate 				Pat_pp = largv;
52670Sstevel@tonic-gate 			if (Args & OCE)
52680Sstevel@tonic-gate 				getpats(largc, largv);
52690Sstevel@tonic-gate 		} else if (Args & OCo) {
52700Sstevel@tonic-gate 			if (largc != 0) /* error if arguments left with -o */
52710Sstevel@tonic-gate 				Error_cnt++;
52720Sstevel@tonic-gate 			else if (fstat(Archive, &ArchSt) < 0)
52730Sstevel@tonic-gate 				msg(ERRN, "Error during stat() of archive");
52740Sstevel@tonic-gate 			switch (Hdr_type) {
52750Sstevel@tonic-gate 			case BIN:
52760Sstevel@tonic-gate 				Hdrsz = HDRSZ;
52770Sstevel@tonic-gate 				Pad_val = HALFWD;
52780Sstevel@tonic-gate 				break;
52790Sstevel@tonic-gate 			case CHR:
52800Sstevel@tonic-gate 				Hdrsz = CHRSZ;
52810Sstevel@tonic-gate 				Pad_val = 0;
52820Sstevel@tonic-gate 				Max_offset = (off_t)(CHAR_OFFSET_MAX);
52830Sstevel@tonic-gate 				break;
52840Sstevel@tonic-gate 			case ASC:
52850Sstevel@tonic-gate 			case CRC:
52860Sstevel@tonic-gate 				Hdrsz = ASCSZ;
52870Sstevel@tonic-gate 				Pad_val = FULLWD;
528836Schin 				Max_offset = (off_t)(ASC_OFFSET_MAX);
52890Sstevel@tonic-gate 				break;
52900Sstevel@tonic-gate 			case TAR:
52910Sstevel@tonic-gate 			/* FALLTHROUGH */
52920Sstevel@tonic-gate 			case USTAR: /* TAR and USTAR */
52930Sstevel@tonic-gate 				Hdrsz = TARSZ;
52940Sstevel@tonic-gate 				Pad_val = FULLBK;
52950Sstevel@tonic-gate 				Max_offset = (off_t)(CHAR_OFFSET_MAX);
52960Sstevel@tonic-gate 				break;
52970Sstevel@tonic-gate 			default:
52980Sstevel@tonic-gate 				msg(EXT, "Impossible header type.");
52990Sstevel@tonic-gate 			}
53000Sstevel@tonic-gate 		} else { /* directory must be specified */
53010Sstevel@tonic-gate 			if (largc != 1)
53020Sstevel@tonic-gate 				Error_cnt++;
5303*4301Sjs198686 			else if (access(*largv, 2) < 0 && (errno != EACCES))
5304*4301Sjs198686 				/*
5305*4301Sjs198686 				 * EACCES is ignored here as it may occur
5306*4301Sjs198686 				 * when any directory component of the path
5307*4301Sjs198686 				 * does not have write permission, even though
5308*4301Sjs198686 				 * the destination subdirectory has write
5309*4301Sjs198686 				 * access. Writing to a read only directory
5310*4301Sjs198686 				 * is handled later, as in "copy in" mode.
5311*4301Sjs198686 				 */
53120Sstevel@tonic-gate 				msg(ERRN,
53130Sstevel@tonic-gate 				    "Error during access() of \"%s\"", *largv);
53140Sstevel@tonic-gate 		}
53150Sstevel@tonic-gate 	}
53160Sstevel@tonic-gate 	if (Error_cnt)
53170Sstevel@tonic-gate 		usage(); /* exits! */
53180Sstevel@tonic-gate 	if (Args & (OCi | OCo)) {
53190Sstevel@tonic-gate 		if (!Dflag) {
53200Sstevel@tonic-gate 			if (Args & (OCB | OCC)) {
53210Sstevel@tonic-gate 				if (g_init(&Device, &Archive) < 0)
53220Sstevel@tonic-gate 					msg(EXTN,
53230Sstevel@tonic-gate 					    "Error during initialization");
53240Sstevel@tonic-gate 			} else {
53250Sstevel@tonic-gate 				if ((Bufsize = g_init(&Device, &Archive)) < 0)
53260Sstevel@tonic-gate 					msg(EXTN,
53270Sstevel@tonic-gate 					    "Error during initialization");
53280Sstevel@tonic-gate 			}
53290Sstevel@tonic-gate 		}
53300Sstevel@tonic-gate 
53310Sstevel@tonic-gate 		blk_cnt_max = _20K / Bufsize;
53320Sstevel@tonic-gate 		if (blk_cnt_max < MX_BUFS) {
53330Sstevel@tonic-gate 			blk_cnt_max = MX_BUFS;
53340Sstevel@tonic-gate 		}
53350Sstevel@tonic-gate 
53360Sstevel@tonic-gate 		Buffr.b_base_p = NULL;
53370Sstevel@tonic-gate 
53380Sstevel@tonic-gate 		for (blk_cnt = blk_cnt_max; blk_cnt > 1; blk_cnt--) {
53390Sstevel@tonic-gate 			Buffr.b_size = (size_t)(Bufsize * blk_cnt);
53400Sstevel@tonic-gate 			Buffr.b_base_p = e_valloc(E_NORMAL, Buffr.b_size);
53410Sstevel@tonic-gate 			if (Buffr.b_base_p != NULL) {
53420Sstevel@tonic-gate 				break;
53430Sstevel@tonic-gate 			}
53440Sstevel@tonic-gate 		}
53450Sstevel@tonic-gate 		if (Buffr.b_base_p == NULL || Buffr.b_size < (2 * CPIOBSZ)) {
53460Sstevel@tonic-gate 			msg(EXT, "Out of memory");
53470Sstevel@tonic-gate 		}
53480Sstevel@tonic-gate 
53490Sstevel@tonic-gate 		Buffr.b_out_p = Buffr.b_in_p = Buffr.b_base_p;
53500Sstevel@tonic-gate 		Buffr.b_cnt = 0L;
53510Sstevel@tonic-gate 		Buffr.b_end_p = Buffr.b_base_p + Buffr.b_size;
53520Sstevel@tonic-gate 	}
53530Sstevel@tonic-gate 
53540Sstevel@tonic-gate 	/*
53550Sstevel@tonic-gate 	 * Now that Bufsize has stabilized, we can allocate our i/o buffer
53560Sstevel@tonic-gate 	 */
53570Sstevel@tonic-gate 	Buf_p = e_valloc(E_EXIT, Bufsize);
53580Sstevel@tonic-gate 
53590Sstevel@tonic-gate 	if (Args & OCp) { /* get destination directory */
53600Sstevel@tonic-gate 		(void) strcpy(Fullnam_p, *largv);
53610Sstevel@tonic-gate 		if (stat(Fullnam_p, &DesSt) < 0)
53620Sstevel@tonic-gate 			msg(EXTN, "Error during stat() of \"%s\"", Fullnam_p);
53630Sstevel@tonic-gate 		if ((DesSt.st_mode & Ftype) != S_IFDIR)
53640Sstevel@tonic-gate 			msg(EXT, "\"%s\" is not a directory", Fullnam_p);
53650Sstevel@tonic-gate 	}
53660Sstevel@tonic-gate 	Full_p = Fullnam_p + strlen(Fullnam_p) - 1;
53670Sstevel@tonic-gate 	if (*Full_p != '/') {
53680Sstevel@tonic-gate 		Full_p++;
53690Sstevel@tonic-gate 		*Full_p = '/';
53700Sstevel@tonic-gate 	}
53710Sstevel@tonic-gate 	Full_p++;
53720Sstevel@tonic-gate 	*Full_p = '\0';
53730Sstevel@tonic-gate 	(void) strcpy(Lnknam_p, Fullnam_p);
53740Sstevel@tonic-gate 	Lnkend_p = Lnknam_p + strlen(Lnknam_p);
53750Sstevel@tonic-gate 	(void) getrlimit(RLIMIT_FSIZE, &rlim);
53760Sstevel@tonic-gate 	Max_filesz = (off_t)rlim.rlim_cur;
53770Sstevel@tonic-gate 	Lnk_hd.L_nxt_p = Lnk_hd.L_bck_p = &Lnk_hd;
53780Sstevel@tonic-gate 	Lnk_hd.L_lnk_p = (struct Lnk *)NULL;
53790Sstevel@tonic-gate }
53800Sstevel@tonic-gate 
53810Sstevel@tonic-gate /*
53820Sstevel@tonic-gate  * set_tym: Set the access and/or modification times for a file.
53830Sstevel@tonic-gate  */
53840Sstevel@tonic-gate 
53850Sstevel@tonic-gate static void
53860Sstevel@tonic-gate set_tym(int dirfd, char *nam_p, time_t atime, time_t mtime)
53870Sstevel@tonic-gate {
53880Sstevel@tonic-gate 	struct timeval times[2];
53890Sstevel@tonic-gate 
53900Sstevel@tonic-gate 	times[0].tv_sec = atime;
53910Sstevel@tonic-gate 	times[0].tv_usec = 0;
53920Sstevel@tonic-gate 	times[1].tv_sec = mtime;
53930Sstevel@tonic-gate 	times[1].tv_usec = 0;
53940Sstevel@tonic-gate 
53950Sstevel@tonic-gate 	if (futimesat(dirfd, nam_p, times) < 0) {
53960Sstevel@tonic-gate 		if (Args & OCa) {
53970Sstevel@tonic-gate 			msg(ERRN,
53980Sstevel@tonic-gate 			    "Unable to reset access time for \"%s%s%s\"",
53990Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
54000Sstevel@tonic-gate 			    nam_p : Fullnam_p,
54010Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
54020Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
54030Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
54040Sstevel@tonic-gate 			    "" : nam_p);
54050Sstevel@tonic-gate 		} else {
54060Sstevel@tonic-gate 			msg(ERRN,
54070Sstevel@tonic-gate 			    "Unable to reset modification time for \"%s%s%s\"",
54080Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
54090Sstevel@tonic-gate 			    nam_p : Fullnam_p,
54100Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
54110Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
54120Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
54130Sstevel@tonic-gate 			    "" : nam_p);
54140Sstevel@tonic-gate 		}
54150Sstevel@tonic-gate 	}
54160Sstevel@tonic-gate }
54170Sstevel@tonic-gate 
54180Sstevel@tonic-gate /*
54190Sstevel@tonic-gate  * sigint:  Catch interrupts.  If an interrupt occurs during the extraction
54200Sstevel@tonic-gate  * of a file from the archive with the -u option set, and the filename did
54210Sstevel@tonic-gate  * exist, remove the current file and restore the original file.  Then exit.
54220Sstevel@tonic-gate  */
54230Sstevel@tonic-gate 
54240Sstevel@tonic-gate /*ARGSUSED*/
54250Sstevel@tonic-gate static void
54260Sstevel@tonic-gate sigint(int sig)
54270Sstevel@tonic-gate {
54280Sstevel@tonic-gate 	char *nam_p;
54290Sstevel@tonic-gate 
54300Sstevel@tonic-gate 	(void) signal(SIGINT, SIG_IGN); /* block further signals */
54310Sstevel@tonic-gate 	if (!Finished) {
54320Sstevel@tonic-gate 		if (Args & OCi)
54330Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
54340Sstevel@tonic-gate 		else /* OCp */
54350Sstevel@tonic-gate 			nam_p = Fullnam_p;
54360Sstevel@tonic-gate 		if (*Over_p != '\0') { /* There is a temp file */
54370Sstevel@tonic-gate 			if (unlink(nam_p) < 0) {
54380Sstevel@tonic-gate 				msg(ERRN,
54390Sstevel@tonic-gate 				    "Cannot remove incomplete \"%s\"", nam_p);
54400Sstevel@tonic-gate 			}
54410Sstevel@tonic-gate 			if (rename(Over_p, nam_p) < 0) {
54420Sstevel@tonic-gate 				if (link(Over_p, nam_p) < 0) {
54430Sstevel@tonic-gate 					msg(ERRN,
54440Sstevel@tonic-gate 					    "Cannot recover original \"%s\"",
54450Sstevel@tonic-gate 					    nam_p);
54460Sstevel@tonic-gate 				}
54470Sstevel@tonic-gate 				if (unlink(Over_p)) {
54480Sstevel@tonic-gate 					msg(ERRN,
54490Sstevel@tonic-gate 					    "Cannot remove temp file \"%s\"",
54500Sstevel@tonic-gate 					    Over_p);
54510Sstevel@tonic-gate 				}
54520Sstevel@tonic-gate 			}
54530Sstevel@tonic-gate 		} else if (unlink(nam_p))
54540Sstevel@tonic-gate 			msg(ERRN,
54550Sstevel@tonic-gate 			    "Cannot remove incomplete \"%s\"", nam_p);
54560Sstevel@tonic-gate 			*Over_p = '\0';
54570Sstevel@tonic-gate 	}
54580Sstevel@tonic-gate 	exit(EXIT_CODE);
54590Sstevel@tonic-gate }
54600Sstevel@tonic-gate 
54610Sstevel@tonic-gate /*
54620Sstevel@tonic-gate  * swap: Swap bytes (-s), halfwords (-S) or or both halfwords and bytes (-b).
54630Sstevel@tonic-gate  */
54640Sstevel@tonic-gate 
54650Sstevel@tonic-gate static void
54660Sstevel@tonic-gate swap(char *buf_p, int cnt)
54670Sstevel@tonic-gate {
54680Sstevel@tonic-gate 	unsigned char tbyte;
54690Sstevel@tonic-gate 	int tcnt;
54700Sstevel@tonic-gate 	int rcnt;
54710Sstevel@tonic-gate 	ushort_t thalf;
54720Sstevel@tonic-gate 
54730Sstevel@tonic-gate 	rcnt = cnt % 4;
54740Sstevel@tonic-gate 	cnt /= 4;
54750Sstevel@tonic-gate 	if (Args & (OCb | OCs | BSM)) {
54760Sstevel@tonic-gate 		tcnt = cnt;
54770Sstevel@tonic-gate 		/* LINTED alignment */
54780Sstevel@tonic-gate 		Swp_p = (union swpbuf *)buf_p;
54790Sstevel@tonic-gate 		while (tcnt-- > 0) {
54800Sstevel@tonic-gate 			tbyte = Swp_p->s_byte[0];
54810Sstevel@tonic-gate 			Swp_p->s_byte[0] = Swp_p->s_byte[1];
54820Sstevel@tonic-gate 			Swp_p->s_byte[1] = tbyte;
54830Sstevel@tonic-gate 			tbyte = Swp_p->s_byte[2];
54840Sstevel@tonic-gate 			Swp_p->s_byte[2] = Swp_p->s_byte[3];
54850Sstevel@tonic-gate 			Swp_p->s_byte[3] = tbyte;
54860Sstevel@tonic-gate 			Swp_p++;
54870Sstevel@tonic-gate 		}
54880Sstevel@tonic-gate 		if (rcnt >= 2) {
54890Sstevel@tonic-gate 		tbyte = Swp_p->s_byte[0];
54900Sstevel@tonic-gate 		Swp_p->s_byte[0] = Swp_p->s_byte[1];
54910Sstevel@tonic-gate 		Swp_p->s_byte[1] = tbyte;
54920Sstevel@tonic-gate 		tbyte = Swp_p->s_byte[2];
54930Sstevel@tonic-gate 		}
54940Sstevel@tonic-gate 	}
54950Sstevel@tonic-gate 	if (Args & (OCb | OCS)) {
54960Sstevel@tonic-gate 		tcnt = cnt;
54970Sstevel@tonic-gate 		/* LINTED alignment */
54980Sstevel@tonic-gate 		Swp_p = (union swpbuf *)buf_p;
54990Sstevel@tonic-gate 		while (tcnt-- > 0) {
55000Sstevel@tonic-gate 			thalf = Swp_p->s_half[0];
55010Sstevel@tonic-gate 			Swp_p->s_half[0] = Swp_p->s_half[1];
55020Sstevel@tonic-gate 			Swp_p->s_half[1] = thalf;
55030Sstevel@tonic-gate 			Swp_p++;
55040Sstevel@tonic-gate 		}
55050Sstevel@tonic-gate 	}
55060Sstevel@tonic-gate }
55070Sstevel@tonic-gate 
55080Sstevel@tonic-gate /*
55090Sstevel@tonic-gate  * usage: Print the usage message on stderr and exit.
55100Sstevel@tonic-gate  */
55110Sstevel@tonic-gate 
55120Sstevel@tonic-gate static void
55130Sstevel@tonic-gate usage(void)
55140Sstevel@tonic-gate {
55150Sstevel@tonic-gate 
55160Sstevel@tonic-gate 	(void) fflush(stdout);
55170Sstevel@tonic-gate #if defined(O_XATTR)
55180Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("USAGE:\n"
55191134Sceastha 	    "\tcpio -i[bcdfkmrstuv@BSV6] [-C size] "
55201134Sceastha 	    "[-E file] [-H hdr] [-I file [-M msg]] "
55211134Sceastha 	    "[-R id] [patterns]\n"
55221134Sceastha 	    "\tcpio -o[acv@ABLV] [-C size] "
55231134Sceastha 	    "[-H hdr] [-O file [-M msg]]\n"
55241134Sceastha 	    "\tcpio -p[adlmuv@LV] [-R id] directory\n"));
55250Sstevel@tonic-gate #else
55260Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("USAGE:\n"
55271134Sceastha 	    "\tcpio -i[bcdfkmrstuvBSV6] [-C size] "
55281134Sceastha 	    "[-E file] [-H hdr] [-I file [-M msg]] "
55291134Sceastha 	    "[-R id] [patterns]\n"
55301134Sceastha 	    "\tcpio -o[acvABLV] [-C size] "
55311134Sceastha 	    "[-H hdr] [-O file [-M msg]]\n"
55321134Sceastha 	    "\tcpio -p[adlmuvLV] [-R id] directory\n"));
55330Sstevel@tonic-gate #endif
55340Sstevel@tonic-gate 	(void) fflush(stderr);
55350Sstevel@tonic-gate 	exit(EXIT_CODE);
55360Sstevel@tonic-gate }
55370Sstevel@tonic-gate 
55380Sstevel@tonic-gate /*
55390Sstevel@tonic-gate  * verbose: For each file, print either the filename (-v) or a dot (-V).
55400Sstevel@tonic-gate  * If the -t option (table of contents) is set, print either the filename,
55410Sstevel@tonic-gate  * or if the -v option is also set, print an "ls -l"-like listing.
55420Sstevel@tonic-gate  */
55430Sstevel@tonic-gate 
55440Sstevel@tonic-gate static void
55450Sstevel@tonic-gate verbose(char *nam_p)
55460Sstevel@tonic-gate {
55470Sstevel@tonic-gate 	int i, j, temp;
55480Sstevel@tonic-gate 	mode_t mode;
55490Sstevel@tonic-gate 	char modestr[12];
55500Sstevel@tonic-gate 
55510Sstevel@tonic-gate 	/*
55520Sstevel@tonic-gate 	 * The printf format and associated arguments to print the current
55530Sstevel@tonic-gate 	 * filename.  Normally, just nam_p.  If we're processing an extended
55540Sstevel@tonic-gate 	 * attribute, these are overridden.
55550Sstevel@tonic-gate 	 */
55560Sstevel@tonic-gate 	char *name_fmt = "%s";
55570Sstevel@tonic-gate 	const char *name = nam_p;
55580Sstevel@tonic-gate 	const char *attribute = NULL;
55590Sstevel@tonic-gate 
55600Sstevel@tonic-gate 	if (Gen.g_attrnam_p != (char *)NULL) {
55610Sstevel@tonic-gate 		/*
55620Sstevel@tonic-gate 		 * Translation note:
55630Sstevel@tonic-gate 		 * 'attribute' is a noun.
55640Sstevel@tonic-gate 		 */
55650Sstevel@tonic-gate 		name_fmt = gettext("%s attribute %s");
55660Sstevel@tonic-gate 
55670Sstevel@tonic-gate 		name = (Args & OCp) ? nam_p : Gen.g_attrfnam_p;
55680Sstevel@tonic-gate 		attribute = Gen.g_attrnam_p;
55690Sstevel@tonic-gate 	}
55700Sstevel@tonic-gate 
55710Sstevel@tonic-gate 	if ((Gen.g_mode == SECMODE) || ((Hdr_type == USTAR ||
55720Sstevel@tonic-gate 	    Hdr_type == TAR) && Thdr_p->tbuf.t_typeflag == 'A')) {
55730Sstevel@tonic-gate 		/* dont print ancillary file */
55740Sstevel@tonic-gate 		aclchar = '+';
55750Sstevel@tonic-gate 		return;
55760Sstevel@tonic-gate 	}
55770Sstevel@tonic-gate 	for (i = 0; i < 11; i++)
55780Sstevel@tonic-gate 		modestr[i] = '-';
55790Sstevel@tonic-gate 	modestr[i] = '\0';
55800Sstevel@tonic-gate 	modestr[i-1] = aclchar;
55810Sstevel@tonic-gate 	aclchar = ' ';
55820Sstevel@tonic-gate 
55830Sstevel@tonic-gate 	if ((Args & OCt) && (Args & OCv)) {
55840Sstevel@tonic-gate 		mode = Gen.g_mode;
55850Sstevel@tonic-gate 		for (i = 0; i < 3; i++) {
55860Sstevel@tonic-gate 			temp = (mode >> (6 - (i * 3)));
55870Sstevel@tonic-gate 			j = (i * 3) + 1;
55880Sstevel@tonic-gate 			if (S_IROTH & temp)
55890Sstevel@tonic-gate 				modestr[j] = 'r';
55900Sstevel@tonic-gate 			if (S_IWOTH & temp)
55910Sstevel@tonic-gate 				modestr[j + 1] = 'w';
55920Sstevel@tonic-gate 			if (S_IXOTH & temp)
55930Sstevel@tonic-gate 				modestr[j + 2] = 'x';
55940Sstevel@tonic-gate 		}
55950Sstevel@tonic-gate 
55960Sstevel@tonic-gate 		if (Hdr_type != BAR) {
55970Sstevel@tonic-gate 			temp = Gen.g_mode & Ftype;
55980Sstevel@tonic-gate 			switch (temp) {
55990Sstevel@tonic-gate 			case (S_IFIFO):
56000Sstevel@tonic-gate 				modestr[0] = 'p';
56010Sstevel@tonic-gate 				break;
56020Sstevel@tonic-gate 			case (S_IFCHR):
56030Sstevel@tonic-gate 				modestr[0] = 'c';
56040Sstevel@tonic-gate 				break;
56050Sstevel@tonic-gate 			case (S_IFDIR):
56060Sstevel@tonic-gate 				modestr[0] = 'd';
56070Sstevel@tonic-gate 				break;
56080Sstevel@tonic-gate 			case (S_IFBLK):
56090Sstevel@tonic-gate 				modestr[0] = 'b';
56100Sstevel@tonic-gate 				break;
56110Sstevel@tonic-gate 			case (S_IFREG): /* was initialized to '-' */
56120Sstevel@tonic-gate 				break;
56130Sstevel@tonic-gate 			case (S_IFLNK):
56140Sstevel@tonic-gate 				modestr[0] = 'l';
56150Sstevel@tonic-gate 				break;
56160Sstevel@tonic-gate 			default:
56170Sstevel@tonic-gate 				msg(ERR, "Impossible file type");
56180Sstevel@tonic-gate 			}
56190Sstevel@tonic-gate 		} else {		/* bar */
56200Sstevel@tonic-gate 			temp = Gen.g_mode & Ftype;
56210Sstevel@tonic-gate 			switch (temp) {
56220Sstevel@tonic-gate 			case (S_IFIFO):
56230Sstevel@tonic-gate 				modestr[0] = 'p';
56240Sstevel@tonic-gate 				break;
56250Sstevel@tonic-gate 			case (S_IFCHR):
56260Sstevel@tonic-gate 				modestr[0] = 'c';
56270Sstevel@tonic-gate 				break;
56280Sstevel@tonic-gate 			case (S_IFDIR):
56290Sstevel@tonic-gate 				modestr[0] = 'd';
56300Sstevel@tonic-gate 				break;
56310Sstevel@tonic-gate 			case (S_IFBLK):
56320Sstevel@tonic-gate 				modestr[0] = 'b';
56330Sstevel@tonic-gate 				break;
56340Sstevel@tonic-gate 			}
56350Sstevel@tonic-gate 			if (bar_linkflag == SYMTYPE)
56360Sstevel@tonic-gate 				modestr[0] = 'l';
56370Sstevel@tonic-gate 		}
56380Sstevel@tonic-gate 		if ((S_ISUID & Gen.g_mode) == S_ISUID)
56390Sstevel@tonic-gate 			modestr[3] = 's';
56400Sstevel@tonic-gate 		if ((S_ISVTX & Gen.g_mode) == S_ISVTX)
56410Sstevel@tonic-gate 			modestr[9] = 't';
56420Sstevel@tonic-gate 		if ((S_ISGID & G_p->g_mode) == S_ISGID && modestr[6] == 'x')
56430Sstevel@tonic-gate 			modestr[6] = 's';
56440Sstevel@tonic-gate 		else if ((S_ENFMT & Gen.g_mode) == S_ENFMT && modestr[6] != 'x')
56450Sstevel@tonic-gate 			modestr[6] = 'l';
56460Sstevel@tonic-gate 		if ((Hdr_type == TAR || Hdr_type == USTAR) && Gen.g_nlink == 0)
56470Sstevel@tonic-gate 			(void) printf("%s%4d ", modestr, (int)Gen.g_nlink+1);
56480Sstevel@tonic-gate 		else
56490Sstevel@tonic-gate 			(void) printf("%s%4d ", modestr, (int)Gen.g_nlink);
56500Sstevel@tonic-gate 		if (Lastuid == (int)Gen.g_uid) {
56510Sstevel@tonic-gate 			if (Lastuid == -1)
56520Sstevel@tonic-gate 				(void) printf("-1       ");
56530Sstevel@tonic-gate 			else
56540Sstevel@tonic-gate 				(void) printf("%-9s", Curpw_p->pw_name);
56550Sstevel@tonic-gate 		} else {
56560Sstevel@tonic-gate 			if (Curpw_p = getpwuid((int)Gen.g_uid)) {
56570Sstevel@tonic-gate 				(void) printf("%-9s", Curpw_p->pw_name);
56580Sstevel@tonic-gate 				Lastuid = (int)Gen.g_uid;
56590Sstevel@tonic-gate 			} else {
56600Sstevel@tonic-gate 				(void) printf("%-9d", (int)Gen.g_uid);
56610Sstevel@tonic-gate 				Lastuid = -1;
56620Sstevel@tonic-gate 			}
56630Sstevel@tonic-gate 		}
56640Sstevel@tonic-gate 		if (Lastgid == (int)Gen.g_gid) {
56650Sstevel@tonic-gate 			if (Lastgid == -1)
56660Sstevel@tonic-gate 				(void) printf("-1       ");
56670Sstevel@tonic-gate 			else
56680Sstevel@tonic-gate 				(void) printf("%-9s", Curgr_p->gr_name);
56690Sstevel@tonic-gate 		} else {
56700Sstevel@tonic-gate 			if (Curgr_p = getgrgid((int)Gen.g_gid)) {
56710Sstevel@tonic-gate 				(void) printf("%-9s", Curgr_p->gr_name);
56720Sstevel@tonic-gate 				Lastgid = (int)Gen.g_gid;
56730Sstevel@tonic-gate 			} else {
56740Sstevel@tonic-gate 				(void) printf("%-9d", (int)Gen.g_gid);
56750Sstevel@tonic-gate 				Lastgid = -1;
56760Sstevel@tonic-gate 			}
56770Sstevel@tonic-gate 		}
56780Sstevel@tonic-gate 
56790Sstevel@tonic-gate 		/* print file size */
56800Sstevel@tonic-gate 		if (!Aspec || ((Gen.g_mode & Ftype) == S_IFIFO) ||
56810Sstevel@tonic-gate 		    (Hdr_type == BAR && bar_linkflag == SYMTYPE)) {
56820Sstevel@tonic-gate 			if (Gen.g_filesz < (1LL << 31))
56830Sstevel@tonic-gate 				(void) printf("%7lld ",
56840Sstevel@tonic-gate 				    (offset_t)Gen.g_filesz);
56850Sstevel@tonic-gate 			else
56860Sstevel@tonic-gate 				(void) printf("%11lld ",
56870Sstevel@tonic-gate 				    (offset_t)Gen.g_filesz);
56880Sstevel@tonic-gate 		} else
56890Sstevel@tonic-gate 			(void) printf("%3d,%3d ", (int)major(Gen.g_rdev),
56900Sstevel@tonic-gate 			    (int)minor(Gen.g_rdev));
56910Sstevel@tonic-gate 		(void) cftime(Time, dcgettext(NULL, FORMAT, LC_TIME),
56920Sstevel@tonic-gate 		    (time_t *)&Gen.g_mtime);
56930Sstevel@tonic-gate 		(void) printf("%s, ", Time);
56940Sstevel@tonic-gate 		(void) printf(name_fmt, name, attribute);
56950Sstevel@tonic-gate 		if ((Gen.g_mode & Ftype) == S_IFLNK) {
56960Sstevel@tonic-gate 			if (Hdr_type == USTAR || Hdr_type == TAR)
56970Sstevel@tonic-gate 				(void) strcpy(Symlnk_p,
56980Sstevel@tonic-gate 				    Thdr_p->tbuf.t_linkname);
56990Sstevel@tonic-gate 			else {
57000Sstevel@tonic-gate 				(void) strncpy(Symlnk_p, Buffr.b_out_p,
57010Sstevel@tonic-gate 				    Gen.g_filesz);
57020Sstevel@tonic-gate 				*(Symlnk_p + Gen.g_filesz) = '\0';
57030Sstevel@tonic-gate 			}
57040Sstevel@tonic-gate 			(void) printf(" -> %s", Symlnk_p);
57050Sstevel@tonic-gate 		}
57060Sstevel@tonic-gate 		if (Hdr_type == BAR) {
57070Sstevel@tonic-gate 			if (bar_linkflag == SYMTYPE)
57080Sstevel@tonic-gate 				(void) printf(gettext(" symbolic link to %s"),
57090Sstevel@tonic-gate 				    bar_linkname);
57100Sstevel@tonic-gate 			else if (bar_linkflag == '1')
57110Sstevel@tonic-gate 				(void) printf(gettext(" linked to %s"),
57120Sstevel@tonic-gate 				    bar_linkname);
57130Sstevel@tonic-gate 		}
57140Sstevel@tonic-gate 		if ((Hdr_type == USTAR || Hdr_type == TAR) &&
57150Sstevel@tonic-gate 		    Thdr_p->tbuf.t_typeflag == '1') {
57160Sstevel@tonic-gate 			(void) printf(gettext(" linked to %s%s%s"),
57170Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
57180Sstevel@tonic-gate 			    Thdr_p->tbuf.t_linkname : Gen.g_attrfnam_p,
57190Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
57200Sstevel@tonic-gate 			    "" : gettext(" attribute "),
57210Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
57220Sstevel@tonic-gate 			    "" : Gen.g_linktoattrnam_p);
57230Sstevel@tonic-gate 		}
57240Sstevel@tonic-gate 		(void) printf("\n");
57250Sstevel@tonic-gate 	} else if ((Args & OCt) || (Args & OCv)) {
57260Sstevel@tonic-gate 		(void) fprintf(Out_p, name_fmt, name, attribute);
57270Sstevel@tonic-gate 		(void) fputc('\n', Out_p);
57280Sstevel@tonic-gate 	} else { /* OCV */
57290Sstevel@tonic-gate 		(void) fputc('.', Out_p);
57300Sstevel@tonic-gate 		if (Verbcnt++ >= 49) { /* start a new line of dots */
57310Sstevel@tonic-gate 			Verbcnt = 0;
57320Sstevel@tonic-gate 			(void) fputc('\n', Out_p);
57330Sstevel@tonic-gate 		}
57340Sstevel@tonic-gate 	}
57350Sstevel@tonic-gate 	(void) fflush(Out_p);
57360Sstevel@tonic-gate }
57370Sstevel@tonic-gate 
57380Sstevel@tonic-gate #define	MK_USHORT(a)	(a & 00000177777)
57390Sstevel@tonic-gate 
57400Sstevel@tonic-gate /*
57410Sstevel@tonic-gate  * write_hdr: Transfer header information for the generic structure
57420Sstevel@tonic-gate  * into the format for the selected header and bwrite() the header.
57430Sstevel@tonic-gate  * ACL support: add two new argumnets. secflag indicates that it's an
57440Sstevel@tonic-gate  *	ancillary file. len is the size of the file (incl. all security
57450Sstevel@tonic-gate  *	attributes). We only have acls now.
57460Sstevel@tonic-gate  */
57470Sstevel@tonic-gate 
57480Sstevel@tonic-gate static void
57490Sstevel@tonic-gate write_hdr(int secflag, off_t len)
57500Sstevel@tonic-gate {
57510Sstevel@tonic-gate 	int cnt, pad;
57520Sstevel@tonic-gate 	mode_t mode;
57530Sstevel@tonic-gate 	uid_t uid;
57540Sstevel@tonic-gate 	gid_t gid;
57550Sstevel@tonic-gate 	const char warnfmt[] = "%s%s%s : %s";
57560Sstevel@tonic-gate 
57570Sstevel@tonic-gate 	if (secflag == ARCHIVE_ACL) {
57580Sstevel@tonic-gate 		mode = SECMODE;
57590Sstevel@tonic-gate 	} else {
57600Sstevel@tonic-gate 		/*
57610Sstevel@tonic-gate 		 * If attribute is being archived in cpio format then
57620Sstevel@tonic-gate 		 * zap off the file type bits since those are truly a
57630Sstevel@tonic-gate 		 * mask and reset them with _XATTR_CPIO_MODE
57640Sstevel@tonic-gate 		 */
57650Sstevel@tonic-gate 
57660Sstevel@tonic-gate 		/*
57670Sstevel@tonic-gate 		 * len is the value of g_filesz for normal files
57680Sstevel@tonic-gate 		 * and the length of the special header buffer in
57690Sstevel@tonic-gate 		 * the case of acl and xattr headers.
57700Sstevel@tonic-gate 		 */
57710Sstevel@tonic-gate 		if (G_p->g_attrnam_p != (char *)NULL && Hdr_type != USTAR &&
57720Sstevel@tonic-gate 		    Hdr_type != TAR) {
57731231Smarks 			mode = (G_p->g_mode & POSIXMODES) | _XATTR_CPIO_MODE;
57740Sstevel@tonic-gate 		} else {
57750Sstevel@tonic-gate 			mode = G_p->g_mode;
57760Sstevel@tonic-gate 		}
57770Sstevel@tonic-gate 		if (secflag != ARCHIVE_XATTR) {
57780Sstevel@tonic-gate 			len = G_p->g_filesz;
57790Sstevel@tonic-gate 		}
57800Sstevel@tonic-gate 	}
57810Sstevel@tonic-gate 
57820Sstevel@tonic-gate 	uid = G_p->g_uid;
57830Sstevel@tonic-gate 	gid = G_p->g_gid;
57840Sstevel@tonic-gate 	/*
57850Sstevel@tonic-gate 	 * Handle EFT uids and gids.  If they get too big
57860Sstevel@tonic-gate 	 * to be represented in a particular format, force 'em to 'nobody'.
57870Sstevel@tonic-gate 	 */
57880Sstevel@tonic-gate 	switch (Hdr_type) {
57890Sstevel@tonic-gate 	case BIN:			/* 16-bits of u_short */
57900Sstevel@tonic-gate 		if ((ulong_t)uid > (ulong_t)USHRT_MAX)
57910Sstevel@tonic-gate 			uid = UID_NOBODY;
57920Sstevel@tonic-gate 		if ((ulong_t)gid > (ulong_t)USHRT_MAX)
57930Sstevel@tonic-gate 			gid = GID_NOBODY;
57940Sstevel@tonic-gate 		break;
57950Sstevel@tonic-gate 	case CHR:			/* %.6lo => 262143 base 10 */
57960Sstevel@tonic-gate 		if ((ulong_t)uid > (ulong_t)0777777)
57970Sstevel@tonic-gate 			uid = UID_NOBODY;
57980Sstevel@tonic-gate 		if ((ulong_t)gid > (ulong_t)0777777)
57990Sstevel@tonic-gate 			gid = GID_NOBODY;
58000Sstevel@tonic-gate 		break;
58010Sstevel@tonic-gate 	case ASC:			/* %.8lx => full 32 bits */
58020Sstevel@tonic-gate 	case CRC:
58030Sstevel@tonic-gate 		break;
58040Sstevel@tonic-gate 	case USTAR:
58050Sstevel@tonic-gate 	case TAR:			/* %.7lo => 2097151 base 10 */
58060Sstevel@tonic-gate 		if ((ulong_t)uid > (ulong_t)07777777)
58070Sstevel@tonic-gate 			uid = UID_NOBODY;
58080Sstevel@tonic-gate 		if ((ulong_t)gid > (ulong_t)07777777)
58090Sstevel@tonic-gate 			gid = GID_NOBODY;
58100Sstevel@tonic-gate 		break;
58110Sstevel@tonic-gate 	default:
58120Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
58130Sstevel@tonic-gate 	}
58140Sstevel@tonic-gate 
58150Sstevel@tonic-gate 	/*
58160Sstevel@tonic-gate 	 * Since cpio formats -don't- encode the symbolic names, print
58170Sstevel@tonic-gate 	 * a warning message when we map the uid or gid this way.
58180Sstevel@tonic-gate 	 * Also, if the ownership just changed, clear set[ug]id bits
58190Sstevel@tonic-gate 	 *
58200Sstevel@tonic-gate 	 * (Except for USTAR format of course, where we have a string
58210Sstevel@tonic-gate 	 * representation of the username embedded in the header)
58220Sstevel@tonic-gate 	 */
58230Sstevel@tonic-gate 	if (uid != G_p->g_uid && Hdr_type != USTAR) {
58240Sstevel@tonic-gate 		msg(ERR, warnfmt,
58250Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
58260Sstevel@tonic-gate 		    G_p->g_nam_p : G_p->g_attrfnam_p,
58270Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
58280Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
58290Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
58300Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p,
58310Sstevel@tonic-gate 		    gettext("uid too large for archive format"));
58320Sstevel@tonic-gate 		if (S_ISREG(mode))
58330Sstevel@tonic-gate 			mode &= ~S_ISUID;
58340Sstevel@tonic-gate 	}
58350Sstevel@tonic-gate 	if (gid != G_p->g_gid && Hdr_type != USTAR) {
58360Sstevel@tonic-gate 		msg(ERR, warnfmt,
58370Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
58380Sstevel@tonic-gate 		    G_p->g_nam_p : G_p->g_attrfnam_p,
58390Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
58400Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
58410Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
58420Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p,
58430Sstevel@tonic-gate 		    gettext("gid too large for archive format"));
58440Sstevel@tonic-gate 		if (S_ISREG(mode))
58450Sstevel@tonic-gate 			mode &= ~S_ISGID;
58460Sstevel@tonic-gate 	}
58470Sstevel@tonic-gate 
58480Sstevel@tonic-gate 	switch (Hdr_type) {
58490Sstevel@tonic-gate 	case BIN:
58500Sstevel@tonic-gate 	case CHR:
58510Sstevel@tonic-gate 	case ASC:
58520Sstevel@tonic-gate 	case CRC:
58530Sstevel@tonic-gate 		cnt = Hdrsz + G_p->g_namesz;
58540Sstevel@tonic-gate 		break;
58550Sstevel@tonic-gate 	case TAR:
58560Sstevel@tonic-gate 		/*FALLTHROUGH*/
58570Sstevel@tonic-gate 	case USTAR: /* TAR and USTAR */
58580Sstevel@tonic-gate 		cnt = TARSZ;
58590Sstevel@tonic-gate 		break;
58600Sstevel@tonic-gate 	default:
58610Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
58620Sstevel@tonic-gate 	}
58630Sstevel@tonic-gate 	FLUSH(cnt);
58640Sstevel@tonic-gate 
58650Sstevel@tonic-gate 	switch (Hdr_type) {
58660Sstevel@tonic-gate 	case BIN:
58670Sstevel@tonic-gate 		Hdr.h_magic = (short)G_p->g_magic;
58680Sstevel@tonic-gate 		Hdr.h_dev = G_p->g_dev;
58690Sstevel@tonic-gate 		Hdr.h_ino = G_p->g_ino;
58700Sstevel@tonic-gate 		Hdr.h_uid = uid;
58710Sstevel@tonic-gate 		Hdr.h_gid = gid;
58720Sstevel@tonic-gate 		Hdr.h_mode = mode;
58730Sstevel@tonic-gate 		Hdr.h_nlink = G_p->g_nlink;
58740Sstevel@tonic-gate 		Hdr.h_rdev = G_p->g_rdev;
58750Sstevel@tonic-gate 		mkshort(Hdr.h_mtime, (long)G_p->g_mtime);
58760Sstevel@tonic-gate 		Hdr.h_namesize = (short)G_p->g_namesz;
58770Sstevel@tonic-gate 		mkshort(Hdr.h_filesize, (long)len);
58780Sstevel@tonic-gate 		(void) strcpy(Hdr.h_name, G_p->g_nam_p);
58790Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, &Hdr, cnt);
58800Sstevel@tonic-gate 		break;
58810Sstevel@tonic-gate 	case CHR:
58820Sstevel@tonic-gate 		(void) sprintf(Buffr.b_in_p,
58830Sstevel@tonic-gate 		    "%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.11lo%.6lo%."
58840Sstevel@tonic-gate 		    "11llo%s", G_p->g_magic, G_p->g_dev, G_p->g_ino, mode,
58850Sstevel@tonic-gate 		    uid, gid, G_p->g_nlink, MK_USHORT(G_p->g_rdev),
58860Sstevel@tonic-gate 		    G_p->g_mtime, (long)G_p->g_namesz, (offset_t)len,
58870Sstevel@tonic-gate 		    G_p->g_nam_p);
58880Sstevel@tonic-gate 		break;
58890Sstevel@tonic-gate 	case ASC:
58900Sstevel@tonic-gate 	case CRC:
58910Sstevel@tonic-gate 		(void) sprintf(Buffr.b_in_p,
58920Sstevel@tonic-gate 		    "%.6lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%."
58930Sstevel@tonic-gate 		    "8lx%.8lx%.8lx%.8lx%s",
58941134Sceastha 		    G_p->g_magic, G_p->g_ino, mode, G_p->g_uid,
58951134Sceastha 		    G_p->g_gid, G_p->g_nlink, G_p->g_mtime, (ulong_t)len,
58961134Sceastha 		    major(G_p->g_dev), minor(G_p->g_dev),
58971134Sceastha 		    major(G_p->g_rdev), minor(G_p->g_rdev),
58981134Sceastha 		    G_p->g_namesz, G_p->g_cksum, G_p->g_nam_p);
58990Sstevel@tonic-gate 		break;
59000Sstevel@tonic-gate 	case USTAR:
59010Sstevel@tonic-gate 		Thdr_p = (union tblock *)Buffr.b_in_p;
59020Sstevel@tonic-gate 		(void) memcpy(Thdr_p, Empty, TARSZ);
59030Sstevel@tonic-gate 		(void) strncpy(Thdr_p->tbuf.t_name, G_p->g_tname,
59040Sstevel@tonic-gate 		    (int)strlen(G_p->g_tname));
59050Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mode, "%07o", (int)mode);
59060Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_uid, "%07o", (int)uid);
59070Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_gid, "%07o", (int)gid);
59080Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_size, "%011llo",
59090Sstevel@tonic-gate 		    (offset_t)len);
59100Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mtime, "%011lo", G_p->g_mtime);
59110Sstevel@tonic-gate 		if (secflag == ARCHIVE_ACL) {
59120Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = 'A';	/* ACL file type */
59130Sstevel@tonic-gate 		} else if (secflag == ARCHIVE_XATTR ||
59140Sstevel@tonic-gate 		    (G_p->g_attrnam_p != (char *)NULL)) {
59150Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = _XATTR_HDRTYPE;
59160Sstevel@tonic-gate 		} else {
59170Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = G_p->g_typeflag;
59180Sstevel@tonic-gate 		}
59190Sstevel@tonic-gate 		if (T_lname[0] != '\0') {
59200Sstevel@tonic-gate 			/*
59210Sstevel@tonic-gate 			 * if not a symbolic link
59220Sstevel@tonic-gate 			 */
59230Sstevel@tonic-gate 			if (((G_p->g_mode & Ftype) != S_IFLNK) &&
59240Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL)) {
59250Sstevel@tonic-gate 				Thdr_p->tbuf.t_typeflag = LNKTYPE;
59260Sstevel@tonic-gate 				(void) sprintf(Thdr_p->tbuf.t_size,
59270Sstevel@tonic-gate 				    "%011lo", 0L);
59280Sstevel@tonic-gate 			}
59290Sstevel@tonic-gate 			(void) strncpy(Thdr_p->tbuf.t_linkname, T_lname,
59300Sstevel@tonic-gate 			    strlen(T_lname));
59310Sstevel@tonic-gate 		}
59320Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_magic, "%s", TMAGIC);
59330Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_version, "%2s", TVERSION);
59340Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_uname, "%s",  G_p->g_uname);
59350Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_gname, "%s", G_p->g_gname);
59360Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_devmajor, "%07o",
59370Sstevel@tonic-gate 		    (int)major(G_p->g_rdev));
59380Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_devminor, "%07o",
59390Sstevel@tonic-gate 		    (int)minor(G_p->g_rdev));
59400Sstevel@tonic-gate 		if (Gen.g_prefix) {
59410Sstevel@tonic-gate 			(void) sprintf(Thdr_p->tbuf.t_prefix, "%s",
59420Sstevel@tonic-gate 			    Gen.g_prefix);
59430Sstevel@tonic-gate 			free(Gen.g_prefix);
59440Sstevel@tonic-gate 			Gen.g_prefix = NULL;
59450Sstevel@tonic-gate 		} else {
59460Sstevel@tonic-gate 			(void) sprintf(Thdr_p->tbuf.t_prefix, "%s", "");
59470Sstevel@tonic-gate 		}
59480Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_cksum, "%07o",
59490Sstevel@tonic-gate 		    (int)cksum(TARTYP, 0, NULL));
59500Sstevel@tonic-gate 		break;
59510Sstevel@tonic-gate 	case TAR:
59520Sstevel@tonic-gate 		Thdr_p = (union tblock *)Buffr.b_in_p;
59530Sstevel@tonic-gate 		(void) memcpy(Thdr_p, Empty, TARSZ);
59540Sstevel@tonic-gate 		(void) strncpy(Thdr_p->tbuf.t_name, G_p->g_nam_p,
59550Sstevel@tonic-gate 		    G_p->g_namesz);
59560Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mode, "%07o ", (int)mode);
59570Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_uid, "%07o ", (int)uid);
59580Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_gid, "%07o ", (int)gid);
59590Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_size, "%011llo ",
59600Sstevel@tonic-gate 		    (offset_t)len);
59610Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mtime, "%011o ",
59620Sstevel@tonic-gate 		    (int)G_p->g_mtime);
59630Sstevel@tonic-gate 		if (T_lname[0] != '\0') {
59640Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = '1';
59650Sstevel@tonic-gate 		} else {
59660Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = '\0';
59670Sstevel@tonic-gate 		}
59680Sstevel@tonic-gate 		(void) strncpy(Thdr_p->tbuf.t_linkname, T_lname,
59690Sstevel@tonic-gate 		    strlen(T_lname));
59700Sstevel@tonic-gate 		break;
59710Sstevel@tonic-gate 	default:
59720Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
59730Sstevel@tonic-gate 	} /* Hdr_type */
59740Sstevel@tonic-gate 
59750Sstevel@tonic-gate 	Buffr.b_in_p += cnt;
59760Sstevel@tonic-gate 	Buffr.b_cnt += cnt;
59770Sstevel@tonic-gate 	pad = ((cnt + Pad_val) & ~Pad_val) - cnt;
59780Sstevel@tonic-gate 	if (pad != 0) {
59790Sstevel@tonic-gate 		FLUSH(pad);
59800Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, Empty, pad);
59810Sstevel@tonic-gate 		Buffr.b_in_p += pad;
59820Sstevel@tonic-gate 		Buffr.b_cnt += pad;
59830Sstevel@tonic-gate 	}
59840Sstevel@tonic-gate }
59850Sstevel@tonic-gate 
59860Sstevel@tonic-gate /*
59870Sstevel@tonic-gate  * write_trail: Create the appropriate trailer for the selected header type
59880Sstevel@tonic-gate  * and bwrite the trailer.  Pad the buffer with nulls out to the next Bufsize
59890Sstevel@tonic-gate  * boundary, and force a write.  If the write completes, or if the trailer is
59900Sstevel@tonic-gate  * completely written (but not all of the padding nulls (as can happen on end
59910Sstevel@tonic-gate  * of medium)) return.  Otherwise, the trailer was not completely written out,
59920Sstevel@tonic-gate  * so re-pad the buffer with nulls and try again.
59930Sstevel@tonic-gate  */
59940Sstevel@tonic-gate 
59950Sstevel@tonic-gate static void
59960Sstevel@tonic-gate write_trail(void)
59970Sstevel@tonic-gate {
59980Sstevel@tonic-gate 	int cnt, need;
59990Sstevel@tonic-gate 
60000Sstevel@tonic-gate 	switch (Hdr_type) {
60010Sstevel@tonic-gate 	case BIN:
60020Sstevel@tonic-gate 		Gen.g_magic = CMN_BIN;
60030Sstevel@tonic-gate 		break;
60040Sstevel@tonic-gate 	case CHR:
60050Sstevel@tonic-gate 		Gen.g_magic = CMN_BIN;
60060Sstevel@tonic-gate 		break;
60070Sstevel@tonic-gate 	case ASC:
60080Sstevel@tonic-gate 		Gen.g_magic = CMN_ASC;
60090Sstevel@tonic-gate 		break;
60100Sstevel@tonic-gate 	case CRC:
60110Sstevel@tonic-gate 		Gen.g_magic = CMN_CRC;
60120Sstevel@tonic-gate 		break;
60130Sstevel@tonic-gate 	}
60140Sstevel@tonic-gate 
60150Sstevel@tonic-gate 	switch (Hdr_type) {
60160Sstevel@tonic-gate 	case BIN:
60170Sstevel@tonic-gate 	case CHR:
60180Sstevel@tonic-gate 	case ASC:
60190Sstevel@tonic-gate 	case CRC:
60200Sstevel@tonic-gate 		Gen.g_mode = Gen.g_uid = Gen.g_gid = 0;
60210Sstevel@tonic-gate 		Gen.g_nlink = 1;
60220Sstevel@tonic-gate 		Gen.g_mtime = Gen.g_ino = Gen.g_dev = 0;
60230Sstevel@tonic-gate 		Gen.g_rdev = Gen.g_cksum = 0;
60240Sstevel@tonic-gate 		Gen.g_filesz = (off_t)0;
60250Sstevel@tonic-gate 		Gen.g_namesz = strlen("TRAILER!!!") + 1;
60260Sstevel@tonic-gate 		(void) strcpy(Gen.g_nam_p, "TRAILER!!!");
60270Sstevel@tonic-gate 		G_p = &Gen;
60280Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
60290Sstevel@tonic-gate 		break;
60300Sstevel@tonic-gate 	case TAR:
60310Sstevel@tonic-gate 	/*FALLTHROUGH*/
60320Sstevel@tonic-gate 	case USTAR: /* TAR and USTAR */
60330Sstevel@tonic-gate 		for (cnt = 0; cnt < 3; cnt++) {
60340Sstevel@tonic-gate 			FLUSH(TARSZ);
60350Sstevel@tonic-gate 			(void) memcpy(Buffr.b_in_p, Empty, TARSZ);
60360Sstevel@tonic-gate 			Buffr.b_in_p += TARSZ;
60370Sstevel@tonic-gate 			Buffr.b_cnt += TARSZ;
60380Sstevel@tonic-gate 		}
60390Sstevel@tonic-gate 		break;
60400Sstevel@tonic-gate 	default:
60410Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
60420Sstevel@tonic-gate 	}
60430Sstevel@tonic-gate 	need = Bufsize - (Buffr.b_cnt % Bufsize);
60440Sstevel@tonic-gate 	if (need == Bufsize)
60450Sstevel@tonic-gate 		need = 0;
60460Sstevel@tonic-gate 
60470Sstevel@tonic-gate 	while (Buffr.b_cnt > 0) {
60480Sstevel@tonic-gate 		while (need > 0) {
60490Sstevel@tonic-gate 			cnt = (need < TARSZ) ? need : TARSZ;
60500Sstevel@tonic-gate 			need -= cnt;
60510Sstevel@tonic-gate 			FLUSH(cnt);
60520Sstevel@tonic-gate 			(void) memcpy(Buffr.b_in_p, Empty, cnt);
60530Sstevel@tonic-gate 			Buffr.b_in_p += cnt;
60540Sstevel@tonic-gate 			Buffr.b_cnt += cnt;
60550Sstevel@tonic-gate 		}
60560Sstevel@tonic-gate 		bflush();
60570Sstevel@tonic-gate 	}
60580Sstevel@tonic-gate }
60590Sstevel@tonic-gate 
60600Sstevel@tonic-gate /*
60610Sstevel@tonic-gate  * if archives in USTAR format, check if typeflag == '5' for directories
60620Sstevel@tonic-gate  */
60630Sstevel@tonic-gate static int
60640Sstevel@tonic-gate ustar_dir(void)
60650Sstevel@tonic-gate {
60660Sstevel@tonic-gate 	if (Hdr_type == USTAR || Hdr_type == TAR) {
60670Sstevel@tonic-gate 		if (Thdr_p->tbuf.t_typeflag == '5')
60680Sstevel@tonic-gate 			return (1);
60690Sstevel@tonic-gate 	}
60700Sstevel@tonic-gate 	return (0);
60710Sstevel@tonic-gate }
60720Sstevel@tonic-gate 
60730Sstevel@tonic-gate /*
60740Sstevel@tonic-gate  * if archives in USTAR format, check if typeflag == '3' || '4' || '6'
60750Sstevel@tonic-gate  * for character, block, fifo special files
60760Sstevel@tonic-gate  */
60770Sstevel@tonic-gate static int
60780Sstevel@tonic-gate ustar_spec(void)
60790Sstevel@tonic-gate {
60800Sstevel@tonic-gate 	int typeflag;
60810Sstevel@tonic-gate 
60820Sstevel@tonic-gate 	if (Hdr_type == USTAR || Hdr_type == TAR) {
60830Sstevel@tonic-gate 		typeflag = Thdr_p->tbuf.t_typeflag;
60840Sstevel@tonic-gate 		if (typeflag == '3' || typeflag == '4' || typeflag == '6')
60850Sstevel@tonic-gate 			return (1);
60860Sstevel@tonic-gate 	}
60870Sstevel@tonic-gate 	return (0);
60880Sstevel@tonic-gate }
60890Sstevel@tonic-gate 
60900Sstevel@tonic-gate /*
60910Sstevel@tonic-gate  * The return value is a pointer to a converted copy of the information in
60920Sstevel@tonic-gate  * FromStat if the file is representable in -Hodc format, and NULL otherwise.
60930Sstevel@tonic-gate  */
60940Sstevel@tonic-gate 
60950Sstevel@tonic-gate static struct stat *
60960Sstevel@tonic-gate convert_to_old_stat(struct stat *FromStat, char *namep, char *attrp)
60970Sstevel@tonic-gate {
60980Sstevel@tonic-gate 	static struct stat ToSt;
60990Sstevel@tonic-gate 	cpioinfo_t TmpSt;
61000Sstevel@tonic-gate 
61010Sstevel@tonic-gate 	(void) memset(&TmpSt, 0, sizeof (cpioinfo_t));
61020Sstevel@tonic-gate 	stat_to_svr32_stat(&TmpSt, FromStat);
61030Sstevel@tonic-gate 	(void) memset(&ToSt, 0, sizeof (ToSt));
61040Sstevel@tonic-gate 
61050Sstevel@tonic-gate 	if (TmpSt.st_rdev == (o_dev_t)NODEV &&
61060Sstevel@tonic-gate 	    (((TmpSt.st_mode & Ftype) == S_IFCHR) ||
61070Sstevel@tonic-gate 	    ((TmpSt.st_mode & Ftype) == S_IFBLK))) {
61080Sstevel@tonic-gate 		/*
61090Sstevel@tonic-gate 		 * Encountered a problem representing the rdev information.
61100Sstevel@tonic-gate 		 * Don't archive it.
61110Sstevel@tonic-gate 		 */
61120Sstevel@tonic-gate 
61130Sstevel@tonic-gate 		msg(ERR, "Error -Hodc format can't support expanded"
61140Sstevel@tonic-gate 		    "types on %s%s%s",
61150Sstevel@tonic-gate 		    namep,
61160Sstevel@tonic-gate 		    (attrp == NULL) ? "" : gettext(" Attribute"),
61170Sstevel@tonic-gate 		    (attrp == NULL) ? "" : attrp);
61180Sstevel@tonic-gate 		return (NULL);
61190Sstevel@tonic-gate 	}
61200Sstevel@tonic-gate 
61210Sstevel@tonic-gate 	if (TmpSt.st_dev == (o_dev_t)NODEV) {
61220Sstevel@tonic-gate 		/*
61230Sstevel@tonic-gate 		 * Having trouble representing the device/inode pair.  We can't
61240Sstevel@tonic-gate 		 * track links in this case; break them all into separate
61250Sstevel@tonic-gate 		 * files.
61260Sstevel@tonic-gate 		 */
61270Sstevel@tonic-gate 
61280Sstevel@tonic-gate 		TmpSt.st_ino = 0;
61290Sstevel@tonic-gate 
61300Sstevel@tonic-gate 		if (((TmpSt.st_mode & Ftype) != S_IFDIR) &&
61310Sstevel@tonic-gate 		    TmpSt.st_nlink > 1)
61320Sstevel@tonic-gate 			msg(POST,
61330Sstevel@tonic-gate 			    "Warning: file %s%s%s has large "
61340Sstevel@tonic-gate 			    "device number - linked "
61350Sstevel@tonic-gate 			    "files will be restored as "
61360Sstevel@tonic-gate 			    "separate files",
61370Sstevel@tonic-gate 			    namep,
61380Sstevel@tonic-gate 			    (attrp == NULL) ? "" : gettext(" Attribute"),
61390Sstevel@tonic-gate 			    (attrp == NULL) ? "" : attrp);
61400Sstevel@tonic-gate 
61410Sstevel@tonic-gate 		/* ensure no links */
61420Sstevel@tonic-gate 
61430Sstevel@tonic-gate 		TmpSt.st_nlink = 1;
61440Sstevel@tonic-gate 	}
61450Sstevel@tonic-gate 
61460Sstevel@tonic-gate 	/* Start converting values */
61470Sstevel@tonic-gate 
61480Sstevel@tonic-gate 	if (TmpSt.st_dev < 0) {
61490Sstevel@tonic-gate 		ToSt.st_dev = 0;
61500Sstevel@tonic-gate 	} else {
61510Sstevel@tonic-gate 		ToSt.st_dev = (dev_t)TmpSt.st_dev;
61520Sstevel@tonic-gate 	}
61530Sstevel@tonic-gate 
61540Sstevel@tonic-gate 	/* -actual- not truncated uid */
61550Sstevel@tonic-gate 
61560Sstevel@tonic-gate 	ToSt.st_uid = TmpSt.st_uid;
61570Sstevel@tonic-gate 
61580Sstevel@tonic-gate 	/* -actual- not truncated gid */
61590Sstevel@tonic-gate 
61600Sstevel@tonic-gate 	ToSt.st_gid = TmpSt.st_gid;
61610Sstevel@tonic-gate 	ToSt.st_ino = (ino_t)TmpSt.st_ino;
61620Sstevel@tonic-gate 	ToSt.st_mode = (mode_t)TmpSt.st_mode;
61630Sstevel@tonic-gate 	ToSt.st_mtime = (ulong_t)TmpSt.st_modtime;
61640Sstevel@tonic-gate 	ToSt.st_nlink = (nlink_t)TmpSt.st_nlink;
61650Sstevel@tonic-gate 	ToSt.st_size = (off_t)TmpSt.st_size;
61660Sstevel@tonic-gate 	ToSt.st_rdev = (dev_t)TmpSt.st_rdev;
61670Sstevel@tonic-gate 
61680Sstevel@tonic-gate 	return (&ToSt);
61690Sstevel@tonic-gate }
61700Sstevel@tonic-gate 
61710Sstevel@tonic-gate /*
61720Sstevel@tonic-gate  * In the beginning of each bar archive, there is a header which describes the
61730Sstevel@tonic-gate  * current volume being created, followed by a header which describes the
61740Sstevel@tonic-gate  * current file being created, followed by the file itself.  If there is
61750Sstevel@tonic-gate  * more than one file to be created, a separate header will be created for
61760Sstevel@tonic-gate  * each additional file.  This structure may be repeated if the bar archive
61770Sstevel@tonic-gate  * contains multiple volumes.  If a file spans across volumes, its header
61780Sstevel@tonic-gate  * will not be repeated in the next volume.
61790Sstevel@tonic-gate  *               +------------------+
61800Sstevel@tonic-gate  *               |    vol header    |
61810Sstevel@tonic-gate  *               |------------------|
61820Sstevel@tonic-gate  *               |   file header i  |     i = 0
61830Sstevel@tonic-gate  *               |------------------|
61840Sstevel@tonic-gate  *               |     <file i>     |
61850Sstevel@tonic-gate  *               |------------------|
61860Sstevel@tonic-gate  *               |  file header i+1 |
61870Sstevel@tonic-gate  *               |------------------|
61880Sstevel@tonic-gate  *               |    <file i+1>    |
61890Sstevel@tonic-gate  *               |------------------|
61900Sstevel@tonic-gate  *               |        .         |
61910Sstevel@tonic-gate  *               |        .         |
61920Sstevel@tonic-gate  *               |        .         |
61930Sstevel@tonic-gate  *               +------------------+
61940Sstevel@tonic-gate  */
61950Sstevel@tonic-gate 
61960Sstevel@tonic-gate /*
61970Sstevel@tonic-gate  * read in the header that describes the current volume of the bar archive
61980Sstevel@tonic-gate  * to be extracted.
61990Sstevel@tonic-gate  */
62000Sstevel@tonic-gate static void
62010Sstevel@tonic-gate read_bar_vol_hdr(void)
62020Sstevel@tonic-gate {
62030Sstevel@tonic-gate 	union b_block *tmp_hdr;
62040Sstevel@tonic-gate 
62050Sstevel@tonic-gate 	tmp_hdr = (union b_block *)Buffr.b_out_p;
62060Sstevel@tonic-gate 	if (tmp_hdr->dbuf.bar_magic[0] == BAR_VOLUME_MAGIC) {
62070Sstevel@tonic-gate 
62080Sstevel@tonic-gate 		if (bar_Vhdr == NULL) {
62090Sstevel@tonic-gate 			bar_Vhdr = e_zalloc(E_EXIT, TBLOCK);
62100Sstevel@tonic-gate 		}
62110Sstevel@tonic-gate 		(void) memcpy(&(bar_Vhdr->dbuf), &(tmp_hdr->dbuf), TBLOCK);
62120Sstevel@tonic-gate 	} else {
62130Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
62140Sstevel@tonic-gate 		    "bar error: cannot read volume header\n"));
62150Sstevel@tonic-gate 		exit(1);
62160Sstevel@tonic-gate 	}
62170Sstevel@tonic-gate 
62180Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.mode, "%8lo", &Gen_bar_vol.g_mode);
62190Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.uid, "%8d", (int *)&Gen_bar_vol.g_uid);
62200Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.gid, "%8d", (int *)&Gen_bar_vol.g_gid);
62210Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.size, "%12llo",
62220Sstevel@tonic-gate 	    (u_off_t *)&Gen_bar_vol.g_filesz);
62230Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.mtime, "%12lo", &Gen_bar_vol.g_mtime);
62240Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.chksum, "%8lo", &Gen_bar_vol.g_cksum);
62250Sstevel@tonic-gate 
62260Sstevel@tonic-gate 	/* set the compress flag */
62270Sstevel@tonic-gate 	if (bar_Vhdr->dbuf.compressed == '1')
62280Sstevel@tonic-gate 		Compressed = 1;
62290Sstevel@tonic-gate 	else
62300Sstevel@tonic-gate 		Compressed = 0;
62310Sstevel@tonic-gate 
62320Sstevel@tonic-gate 	Buffr.b_out_p += 512;
62330Sstevel@tonic-gate 	Buffr.b_cnt -= 512;
62340Sstevel@tonic-gate 
62350Sstevel@tonic-gate 	/*
62360Sstevel@tonic-gate 	 * not the first volume; exit
62370Sstevel@tonic-gate 	 */
62380Sstevel@tonic-gate 	if (strcmp(bar_Vhdr->dbuf.volume_num, "1") != 0) {
62390Sstevel@tonic-gate 		(void) fprintf(stderr,
62400Sstevel@tonic-gate 		    gettext("error: This is not volume 1.  "));
62410Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("This is volume %s.  "),
62420Sstevel@tonic-gate 		    bar_Vhdr->dbuf.volume_num);
62430Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Please insert volume 1.\n"));
62440Sstevel@tonic-gate 		exit(1);
62450Sstevel@tonic-gate 	}
62460Sstevel@tonic-gate 
62470Sstevel@tonic-gate 	read_bar_file_hdr();
62480Sstevel@tonic-gate }
62490Sstevel@tonic-gate 
62500Sstevel@tonic-gate /*
62510Sstevel@tonic-gate  * read in the header that describes the current file to be extracted
62520Sstevel@tonic-gate  */
62530Sstevel@tonic-gate static void
62540Sstevel@tonic-gate read_bar_file_hdr(void)
62550Sstevel@tonic-gate {
62560Sstevel@tonic-gate 	union b_block *tmp_hdr;
62570Sstevel@tonic-gate 	char *start_of_name, *name_p;
62580Sstevel@tonic-gate 	char *tmp;
62590Sstevel@tonic-gate 
62600Sstevel@tonic-gate 	if (*Buffr.b_out_p == '\0') {
62610Sstevel@tonic-gate 		*Gen.g_nam_p = '\0';
62620Sstevel@tonic-gate 		exit(0);
62630Sstevel@tonic-gate 	}
62640Sstevel@tonic-gate 
62650Sstevel@tonic-gate 	tmp_hdr = (union b_block *)Buffr.b_out_p;
62660Sstevel@tonic-gate 
62670Sstevel@tonic-gate 	tmp = &tmp_hdr->dbuf.mode[1];
62680Sstevel@tonic-gate 	(void) sscanf(tmp, "%8lo", &Gen.g_mode);
62690Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.uid, "%8lo", &Gen.g_uid);
62700Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.gid, "%8lo", &Gen.g_gid);
62710Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.size, "%12llo",
62720Sstevel@tonic-gate 	    (u_off_t *)&Gen.g_filesz);
62730Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.mtime, "%12lo", &Gen.g_mtime);
62740Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.chksum, "%8lo", &Gen.g_cksum);
62750Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.rdev, "%8lo", &Gen.g_rdev);
62760Sstevel@tonic-gate 
62770Sstevel@tonic-gate #define	to_new_major(x)	(int)((unsigned)((x) & OMAXMAJ) << NBITSMINOR)
62780Sstevel@tonic-gate #define	to_new_minor(x)	(int)((x) & OMAXMIN)
62790Sstevel@tonic-gate 	Gen.g_rdev = to_new_major(Gen.g_rdev) | to_new_minor(Gen.g_rdev);
62800Sstevel@tonic-gate 	bar_linkflag = tmp_hdr->dbuf.linkflag;
62810Sstevel@tonic-gate 	start_of_name = &tmp_hdr->dbuf.start_of_name;
62820Sstevel@tonic-gate 
62830Sstevel@tonic-gate 
62840Sstevel@tonic-gate 	name_p = Gen.g_nam_p;
62850Sstevel@tonic-gate 	while (*name_p++ = *start_of_name++)
62860Sstevel@tonic-gate 		;
62870Sstevel@tonic-gate 	*name_p = '\0';
62880Sstevel@tonic-gate 	if (bar_linkflag == LNKTYPE || bar_linkflag == SYMTYPE)
62890Sstevel@tonic-gate 		(void) strcpy(bar_linkname, start_of_name);
62900Sstevel@tonic-gate 
62910Sstevel@tonic-gate 	Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
62920Sstevel@tonic-gate 	(void) strcpy(nambuf, Gen.g_nam_p);
62930Sstevel@tonic-gate }
62940Sstevel@tonic-gate 
62950Sstevel@tonic-gate /*
62960Sstevel@tonic-gate  * if the bar archive is compressed, set up a pipe and do the de-compression
62970Sstevel@tonic-gate  * as the compressed file is read in.
62980Sstevel@tonic-gate  */
62990Sstevel@tonic-gate static void
63000Sstevel@tonic-gate setup_uncompress(FILE **pipef)
63010Sstevel@tonic-gate {
63020Sstevel@tonic-gate 	char *cmd_buf;
63030Sstevel@tonic-gate 	size_t cmdlen;
63040Sstevel@tonic-gate 
63050Sstevel@tonic-gate 	cmd_buf = e_zalloc(E_EXIT, MAXPATHLEN * 2);
63060Sstevel@tonic-gate 
63070Sstevel@tonic-gate 	if (access(Gen.g_nam_p, W_OK) != 0) {
63080Sstevel@tonic-gate 		cmdlen = snprintf(cmd_buf, MAXPATHLEN * 2,
63090Sstevel@tonic-gate 		    "chmod +w '%s'; uncompress -c > '%s'; "
63100Sstevel@tonic-gate 		    "chmod 0%o '%s'",
63110Sstevel@tonic-gate 		    Gen.g_nam_p, Gen.g_nam_p, (int)G_p->g_mode, Gen.g_nam_p);
63120Sstevel@tonic-gate 	} else {
63130Sstevel@tonic-gate 		cmdlen = snprintf(cmd_buf, MAXPATHLEN * 2,
63140Sstevel@tonic-gate 		    "uncompress -c > '%s'", Gen.g_nam_p);
63150Sstevel@tonic-gate 	}
63160Sstevel@tonic-gate 
63170Sstevel@tonic-gate 	if (cmdlen >= MAXPATHLEN * 2 ||
63180Sstevel@tonic-gate 	    (*pipef = popen(cmd_buf, "w")) == NULL) {
63190Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("error\n"));
63200Sstevel@tonic-gate 		exit(1);
63210Sstevel@tonic-gate 	}
63220Sstevel@tonic-gate 
63230Sstevel@tonic-gate 	if (close(Ofile) != 0)
63240Sstevel@tonic-gate 		msg(EXTN, "close error");
63250Sstevel@tonic-gate 	Ofile = fileno(*pipef);
63260Sstevel@tonic-gate 
63270Sstevel@tonic-gate 	free(cmd_buf);
63280Sstevel@tonic-gate }
63290Sstevel@tonic-gate 
63300Sstevel@tonic-gate /*
63310Sstevel@tonic-gate  * if the bar archive spans multiple volumes, read in the header that
63320Sstevel@tonic-gate  * describes the next volume.
63330Sstevel@tonic-gate  */
63340Sstevel@tonic-gate static void
63350Sstevel@tonic-gate skip_bar_volhdr(void)
63360Sstevel@tonic-gate {
63370Sstevel@tonic-gate 	char *buff;
63380Sstevel@tonic-gate 	union b_block *tmp_hdr;
63390Sstevel@tonic-gate 
63400Sstevel@tonic-gate 	buff = e_zalloc(E_EXIT, (uint_t)Bufsize);
63410Sstevel@tonic-gate 
63420Sstevel@tonic-gate 	if (g_read(Device, Archive, buff, Bufsize) < 0) {
63430Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
63440Sstevel@tonic-gate 		    "error in skip_bar_volhdr\n"));
63450Sstevel@tonic-gate 	} else {
63460Sstevel@tonic-gate 
63470Sstevel@tonic-gate 		tmp_hdr = (union b_block *)buff;
63480Sstevel@tonic-gate 		if (tmp_hdr->dbuf.bar_magic[0] == BAR_VOLUME_MAGIC) {
63490Sstevel@tonic-gate 
63500Sstevel@tonic-gate 			if (bar_Vhdr == NULL) {
63510Sstevel@tonic-gate 				bar_Vhdr = e_zalloc(E_EXIT, TBLOCK);
63520Sstevel@tonic-gate 			}
63530Sstevel@tonic-gate 			(void) memcpy(&(bar_Vhdr->dbuf),
63540Sstevel@tonic-gate 			    &(tmp_hdr->dbuf), TBLOCK);
63550Sstevel@tonic-gate 		} else {
63560Sstevel@tonic-gate 			(void) fprintf(stderr,
63570Sstevel@tonic-gate 			    gettext("cpio error: cannot read bar volume "
63580Sstevel@tonic-gate 			    "header\n"));
63590Sstevel@tonic-gate 			exit(1);
63600Sstevel@tonic-gate 		}
63610Sstevel@tonic-gate 
63620Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.mode, "%8lo",
63630Sstevel@tonic-gate 		    &Gen_bar_vol.g_mode);
63640Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.uid, "%8lo",
63650Sstevel@tonic-gate 		    &Gen_bar_vol.g_uid);
63660Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.gid, "%8lo",
63670Sstevel@tonic-gate 		    &Gen_bar_vol.g_gid);
63680Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.size, "%12llo",
63690Sstevel@tonic-gate 		    (u_off_t *)&Gen_bar_vol.g_filesz);
63700Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.mtime, "%12lo",
63710Sstevel@tonic-gate 		    &Gen_bar_vol.g_mtime);
63720Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.chksum, "%8lo",
63730Sstevel@tonic-gate 		    &Gen_bar_vol.g_cksum);
63740Sstevel@tonic-gate 		if (bar_Vhdr->dbuf.compressed == '1')
63750Sstevel@tonic-gate 			Compressed = 1;
63760Sstevel@tonic-gate 		else
63770Sstevel@tonic-gate 			Compressed = 0;
63780Sstevel@tonic-gate 	}
63790Sstevel@tonic-gate 
63800Sstevel@tonic-gate 	/*
63810Sstevel@tonic-gate 	 * Now put the rest of the bytes read in into the data buffer.
63820Sstevel@tonic-gate 	 */
63830Sstevel@tonic-gate 	(void) memcpy(Buffr.b_in_p, &buff[512], (Bufsize - 512));
63840Sstevel@tonic-gate 	Buffr.b_in_p += (Bufsize - 512);
63850Sstevel@tonic-gate 	Buffr.b_cnt += (long)(Bufsize - 512);
63860Sstevel@tonic-gate 
63870Sstevel@tonic-gate 	free(buff);
63880Sstevel@tonic-gate }
63890Sstevel@tonic-gate 
63900Sstevel@tonic-gate /*
63910Sstevel@tonic-gate  * check the linkflag which indicates the type of the file to be extracted,
63920Sstevel@tonic-gate  * invoke the corresponding routine to extract the file.
63930Sstevel@tonic-gate  */
63940Sstevel@tonic-gate static void
63950Sstevel@tonic-gate bar_file_in(void)
63960Sstevel@tonic-gate {
63970Sstevel@tonic-gate 	/*
63980Sstevel@tonic-gate 	 * the file is a directory
63990Sstevel@tonic-gate 	 */
64000Sstevel@tonic-gate 	if (Adir) {
64010Sstevel@tonic-gate 		if (ckname(1) != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
64020Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
64030Sstevel@tonic-gate 		}
64040Sstevel@tonic-gate 		return;
64050Sstevel@tonic-gate 	}
64060Sstevel@tonic-gate 
64070Sstevel@tonic-gate 	switch (bar_linkflag) {
64080Sstevel@tonic-gate 	case REGTYPE:
64090Sstevel@tonic-gate 		/* regular file */
64100Sstevel@tonic-gate 		if ((ckname(1) == F_SKIP) ||
64110Sstevel@tonic-gate 		    (Ofile = openout(G_p->g_dirfd)) < 0) {
64120Sstevel@tonic-gate 			data_in(P_SKIP);
64130Sstevel@tonic-gate 		} else {
64140Sstevel@tonic-gate 			data_in(P_PROC);
64150Sstevel@tonic-gate 		}
64160Sstevel@tonic-gate 		break;
64170Sstevel@tonic-gate 	case LNKTYPE:
64180Sstevel@tonic-gate 		/* hard link */
64190Sstevel@tonic-gate 		if (ckname(1) == F_SKIP) {
64200Sstevel@tonic-gate 			break;
64210Sstevel@tonic-gate 		}
64220Sstevel@tonic-gate 		(void) creat_lnk(G_p->g_dirfd, bar_linkname, G_p->g_nam_p);
64230Sstevel@tonic-gate 		break;
64240Sstevel@tonic-gate 	case SYMTYPE:
64250Sstevel@tonic-gate 		/* symbolic link */
64260Sstevel@tonic-gate 		if ((ckname(1) == F_SKIP) ||
64270Sstevel@tonic-gate 		    (Ofile = openout(G_p->g_dirfd)) < 0) {
64280Sstevel@tonic-gate 			data_in(P_SKIP);
64290Sstevel@tonic-gate 		} else {
64300Sstevel@tonic-gate 			data_in(P_PROC);
64310Sstevel@tonic-gate 		}
64320Sstevel@tonic-gate 		break;
64330Sstevel@tonic-gate 	case CHRTYPE:
64340Sstevel@tonic-gate 		/* character device or FIFO */
64350Sstevel@tonic-gate 		if (ckname(1) != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
64360Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
64370Sstevel@tonic-gate 		}
64380Sstevel@tonic-gate 		break;
64390Sstevel@tonic-gate 	default:
64400Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("error: unknown file type\n"));
64410Sstevel@tonic-gate 		break;
64420Sstevel@tonic-gate 	}
64430Sstevel@tonic-gate }
64440Sstevel@tonic-gate 
64450Sstevel@tonic-gate 
64460Sstevel@tonic-gate /*
64470Sstevel@tonic-gate  * This originally came from libgenIO/g_init.c
64480Sstevel@tonic-gate  * XXX	And it is very broken.
64490Sstevel@tonic-gate  */
64500Sstevel@tonic-gate 
64510Sstevel@tonic-gate /* #include <sys/statvfs.h> */
64520Sstevel@tonic-gate #include <ftw.h>
64530Sstevel@tonic-gate /* #include <libgenIO.h> */
64540Sstevel@tonic-gate #define	G_TM_TAPE	1	/* Tapemaster controller    */
64550Sstevel@tonic-gate #define	G_XY_DISK	3	/* xy disks		*/
64560Sstevel@tonic-gate #define	G_SD_DISK	7	/* scsi sd disk		*/
64570Sstevel@tonic-gate #define	G_XT_TAPE	8	/* xt tapes		*/
64580Sstevel@tonic-gate #define	G_SF_FLOPPY	9	/* sf floppy		*/
64590Sstevel@tonic-gate #define	G_XD_DISK	10	/* xd disks		*/
64600Sstevel@tonic-gate #define	G_ST_TAPE	11	/* scsi tape		*/
64610Sstevel@tonic-gate #define	G_NS		12	/* noswap pseudo-dev	*/
64620Sstevel@tonic-gate #define	G_RAM		13	/* ram pseudo-dev	*/
64630Sstevel@tonic-gate #define	G_FT		14	/* tftp			*/
64640Sstevel@tonic-gate #define	G_HD		15	/* 386 network disk	*/
64650Sstevel@tonic-gate #define	G_FD		16	/* 386 AT disk		*/
64660Sstevel@tonic-gate #define	G_FILE		28	/* file, not a device	*/
64670Sstevel@tonic-gate #define	G_NO_DEV	29	/* device does not require special treatment */
64680Sstevel@tonic-gate #define	G_DEV_MAX	30	/* last valid device type */
64690Sstevel@tonic-gate 
64700Sstevel@tonic-gate /*
64710Sstevel@tonic-gate  * g_init: Determine the device being accessed, set the buffer size,
64720Sstevel@tonic-gate  * and perform any device specific initialization. Since at this point
64730Sstevel@tonic-gate  * Sun has no system call to read the configuration, the major numbers
64740Sstevel@tonic-gate  * are assumed to be static and types are figured out as such. However,
64750Sstevel@tonic-gate  * as a rough estimate, the buffer size for all types is set to 512
64760Sstevel@tonic-gate  * as a default.
64770Sstevel@tonic-gate  */
64780Sstevel@tonic-gate 
64790Sstevel@tonic-gate static int
64800Sstevel@tonic-gate g_init(int *devtype, int *fdes)
64810Sstevel@tonic-gate {
64820Sstevel@tonic-gate 	int bufsize;
64830Sstevel@tonic-gate 	struct stat st_buf;
64840Sstevel@tonic-gate 	struct statvfs stfs_buf;
64850Sstevel@tonic-gate 
64860Sstevel@tonic-gate 	*devtype = G_NO_DEV;
64870Sstevel@tonic-gate 	bufsize = -1;
64880Sstevel@tonic-gate 	if (fstat(*fdes, &st_buf) == -1)
64890Sstevel@tonic-gate 		return (-1);
6490871Scasper 	if (!S_ISCHR(st_buf.st_mode) && !S_ISBLK(st_buf.st_mode)) {
6491871Scasper 		if (S_ISFIFO(st_buf.st_mode)) {
64920Sstevel@tonic-gate 			bufsize = 512;
64930Sstevel@tonic-gate 		} else {
64940Sstevel@tonic-gate 			/* find block size for this file system */
64950Sstevel@tonic-gate 			*devtype = G_FILE;
64960Sstevel@tonic-gate 			if (fstatvfs(*fdes, &stfs_buf) < 0) {
64970Sstevel@tonic-gate 					bufsize = -1;
64980Sstevel@tonic-gate 					errno = ENODEV;
64990Sstevel@tonic-gate 			} else
65000Sstevel@tonic-gate 				bufsize = stfs_buf.f_bsize;
65010Sstevel@tonic-gate 		}
65020Sstevel@tonic-gate 
65030Sstevel@tonic-gate 		return (bufsize);
65040Sstevel@tonic-gate 
65050Sstevel@tonic-gate 	/*
65060Sstevel@tonic-gate 	 * We'll have to add a remote attribute to stat but this
65070Sstevel@tonic-gate 	 * should work for now.
65080Sstevel@tonic-gate 	 */
65090Sstevel@tonic-gate 	} else if (st_buf.st_dev & 0x8000)	/* if remote  rdev */
65100Sstevel@tonic-gate 		return (512);
65110Sstevel@tonic-gate 
65120Sstevel@tonic-gate 	bufsize = 512;
65130Sstevel@tonic-gate 
65140Sstevel@tonic-gate 	if (Hdr_type == BAR) {
65150Sstevel@tonic-gate 		if (is_tape(*fdes)) {
65160Sstevel@tonic-gate 			bufsize = BAR_TAPE_SIZE;
65170Sstevel@tonic-gate 			(void) fprintf(stderr, "Archiving to tape");
65180Sstevel@tonic-gate 			(void) fprintf(stderr, " blocking factor 126\n");
65190Sstevel@tonic-gate 		} else if (is_floppy(*fdes)) {
65200Sstevel@tonic-gate 			bufsize = BAR_FLOPPY_SIZE;
65210Sstevel@tonic-gate 			(void) fprintf(stderr, "Archiving to floppy");
65220Sstevel@tonic-gate 			(void) fprintf(stderr, " blocking factor 18\n");
65230Sstevel@tonic-gate 		}
65240Sstevel@tonic-gate 	}
65250Sstevel@tonic-gate 
65260Sstevel@tonic-gate 	return (bufsize);
65270Sstevel@tonic-gate }
65280Sstevel@tonic-gate 
65290Sstevel@tonic-gate /*
65300Sstevel@tonic-gate  * This originally came from libgenIO/g_read.c
65310Sstevel@tonic-gate  */
65320Sstevel@tonic-gate 
65330Sstevel@tonic-gate /*
65340Sstevel@tonic-gate  * g_read: Read nbytes of data from fdes (of type devtype) and place
65350Sstevel@tonic-gate  * data in location pointed to by buf.  In case of end of medium,
65360Sstevel@tonic-gate  * translate (where necessary) device specific EOM indications into
65370Sstevel@tonic-gate  * the generic EOM indication of rv = -1, errno = ENOSPC.
65380Sstevel@tonic-gate  */
65390Sstevel@tonic-gate 
65400Sstevel@tonic-gate static int
65410Sstevel@tonic-gate g_read(int devtype, int fdes, char *buf, unsigned nbytes)
65420Sstevel@tonic-gate {
65430Sstevel@tonic-gate 	int rv;
65440Sstevel@tonic-gate 
65450Sstevel@tonic-gate 	if (devtype < 0 || devtype >= G_DEV_MAX) {
65460Sstevel@tonic-gate 		errno = ENODEV;
65470Sstevel@tonic-gate 		return (-1);
65480Sstevel@tonic-gate 	}
65490Sstevel@tonic-gate 
65500Sstevel@tonic-gate 	rv = read(fdes, buf, nbytes);
65510Sstevel@tonic-gate 
65520Sstevel@tonic-gate 	/* st devices return 0 when no space left */
65530Sstevel@tonic-gate 	if ((rv == 0 && errno == 0 && Hdr_type != BAR) ||
65540Sstevel@tonic-gate 	    (rv == -1 && errno == EIO)) {
65550Sstevel@tonic-gate 		errno = 0;
65560Sstevel@tonic-gate 		rv = 0;
65570Sstevel@tonic-gate 	}
65580Sstevel@tonic-gate 
65590Sstevel@tonic-gate 	return (rv);
65600Sstevel@tonic-gate }
65610Sstevel@tonic-gate 
65620Sstevel@tonic-gate /*
65630Sstevel@tonic-gate  * This originally came from libgenIO/g_write.c
65640Sstevel@tonic-gate  */
65650Sstevel@tonic-gate 
65660Sstevel@tonic-gate /*
65670Sstevel@tonic-gate  * g_write: Write nbytes of data to fdes (of type devtype) from
65680Sstevel@tonic-gate  * the location pointed to by buf.  In case of end of medium,
65690Sstevel@tonic-gate  * translate (where necessary) device specific EOM indications into
65700Sstevel@tonic-gate  * the generic EOM indication of rv = -1, errno = ENOSPC.
65710Sstevel@tonic-gate  */
65720Sstevel@tonic-gate 
65730Sstevel@tonic-gate static int
65740Sstevel@tonic-gate g_write(int devtype, int fdes, char *buf, unsigned nbytes)
65750Sstevel@tonic-gate {
65760Sstevel@tonic-gate 	int rv;
65770Sstevel@tonic-gate 
65780Sstevel@tonic-gate 	if (devtype < 0 || devtype >= G_DEV_MAX) {
65790Sstevel@tonic-gate 		errno = ENODEV;
65800Sstevel@tonic-gate 		return (-1);
65810Sstevel@tonic-gate 	}
65820Sstevel@tonic-gate 
65830Sstevel@tonic-gate 	rv = write(fdes, buf, nbytes);
65840Sstevel@tonic-gate 
65850Sstevel@tonic-gate 	/* st devices return 0 when no more space left */
65860Sstevel@tonic-gate 	if ((rv == 0 && errno == 0) || (rv == -1 && errno == EIO)) {
65870Sstevel@tonic-gate 		errno = ENOSPC;
65880Sstevel@tonic-gate 		rv = -1;
65890Sstevel@tonic-gate 	}
65900Sstevel@tonic-gate 
65910Sstevel@tonic-gate 	return (rv);
65920Sstevel@tonic-gate }
65930Sstevel@tonic-gate 
65940Sstevel@tonic-gate /*
65950Sstevel@tonic-gate  * Test for tape
65960Sstevel@tonic-gate  */
65970Sstevel@tonic-gate 
65980Sstevel@tonic-gate static int
65990Sstevel@tonic-gate is_tape(int fd)
66000Sstevel@tonic-gate {
66010Sstevel@tonic-gate 	struct mtget stuff;
66020Sstevel@tonic-gate 
66030Sstevel@tonic-gate 	/*
66040Sstevel@tonic-gate 	 * try to do a generic tape ioctl, just to see if
66050Sstevel@tonic-gate 	 * the thing is in fact a tape drive(er).
66060Sstevel@tonic-gate 	 */
66070Sstevel@tonic-gate 	if (ioctl(fd, MTIOCGET, &stuff) != -1) {
66080Sstevel@tonic-gate 		/* the ioctl succeeded, must have been a tape */
66090Sstevel@tonic-gate 		return (1);
66100Sstevel@tonic-gate 	}
66110Sstevel@tonic-gate 	return (0);
66120Sstevel@tonic-gate }
66130Sstevel@tonic-gate 
66140Sstevel@tonic-gate /*
66150Sstevel@tonic-gate  * Test for floppy
66160Sstevel@tonic-gate  */
66170Sstevel@tonic-gate 
66180Sstevel@tonic-gate static int
66190Sstevel@tonic-gate is_floppy(int fd)
66200Sstevel@tonic-gate {
66210Sstevel@tonic-gate 	struct fd_char stuff;
66220Sstevel@tonic-gate 
66230Sstevel@tonic-gate 	/*
66240Sstevel@tonic-gate 	 * try to get the floppy drive characteristics, just to see if
66250Sstevel@tonic-gate 	 * the thing is in fact a floppy drive(er).
66260Sstevel@tonic-gate 	 */
66270Sstevel@tonic-gate 	if (ioctl(fd, FDIOGCHAR, &stuff) != -1) {
66280Sstevel@tonic-gate 		/* the ioctl succeeded, must have been a floppy */
66290Sstevel@tonic-gate 		return (1);
66300Sstevel@tonic-gate 	}
66310Sstevel@tonic-gate 
66320Sstevel@tonic-gate 	return (0);
66330Sstevel@tonic-gate }
66340Sstevel@tonic-gate 
66350Sstevel@tonic-gate /*
66360Sstevel@tonic-gate  * New functions for ACLs and other security attributes
66370Sstevel@tonic-gate  */
66380Sstevel@tonic-gate 
66390Sstevel@tonic-gate /*
66400Sstevel@tonic-gate  * The function appends the new security attribute info to the end of
66410Sstevel@tonic-gate  * existing secinfo.
66420Sstevel@tonic-gate  */
66430Sstevel@tonic-gate static int
66440Sstevel@tonic-gate append_secattr(
6645789Sahrens 	char		**secinfo,	/* existing security info */
6646789Sahrens 	int		*secinfo_len,	/* length of existing security info */
6647789Sahrens 	acl_t		*aclp) 	/* new attribute data pointer */
66480Sstevel@tonic-gate {
66490Sstevel@tonic-gate 	char	*new_secinfo;
66500Sstevel@tonic-gate 	char	*attrtext;
66510Sstevel@tonic-gate 	size_t	newattrsize;
66520Sstevel@tonic-gate 	int	oldsize;
66530Sstevel@tonic-gate 
66540Sstevel@tonic-gate 	/* no need to add */
6655789Sahrens 	if (aclp == NULL) {
66560Sstevel@tonic-gate 		return (0);
66570Sstevel@tonic-gate 	}
66580Sstevel@tonic-gate 
6659789Sahrens 	switch (acl_type(aclp)) {
6660789Sahrens 	case ACLENT_T:
6661789Sahrens 	case ACE_T:
66620Sstevel@tonic-gate 		/* LINTED alignment */
66631420Smarks 		attrtext = acl_totext(aclp, ACL_APPEND_ID | ACL_COMPACT_FMT);
66640Sstevel@tonic-gate 		if (attrtext == NULL) {
66650Sstevel@tonic-gate 			(void) fprintf(stderr, "acltotext failed\n");
66660Sstevel@tonic-gate 			return (-1);
66670Sstevel@tonic-gate 		}
66680Sstevel@tonic-gate 		/* header: type + size = 8 */
66690Sstevel@tonic-gate 		newattrsize = 8 + strlen(attrtext) + 1;
66700Sstevel@tonic-gate 		attr = e_zalloc(E_NORMAL, newattrsize);
66710Sstevel@tonic-gate 		if (attr == NULL) {
66720Sstevel@tonic-gate 			(void) fprintf(stderr, "can't allocate memory\n");
66730Sstevel@tonic-gate 			return (-1);
66740Sstevel@tonic-gate 		}
6675789Sahrens 		attr->attr_type = (acl_type(aclp) == ACLENT_T) ?
6676789Sahrens 		    UFSD_ACL : ACE_ACL;
66770Sstevel@tonic-gate 		/* acl entry count */
6678789Sahrens 		(void) sprintf(attr->attr_len, "%06o", acl_cnt(aclp));
66790Sstevel@tonic-gate 		(void) strcpy((char *)&attr->attr_info[0], attrtext);
66800Sstevel@tonic-gate 		free(attrtext);
66810Sstevel@tonic-gate 		break;
66820Sstevel@tonic-gate 
66830Sstevel@tonic-gate 		/* SunFed's case goes here */
66840Sstevel@tonic-gate 
66850Sstevel@tonic-gate 	default:
66860Sstevel@tonic-gate 		(void) fprintf(stderr, "unrecognized attribute type\n");
66870Sstevel@tonic-gate 		return (-1);
66880Sstevel@tonic-gate 	}
66890Sstevel@tonic-gate 
66900Sstevel@tonic-gate 	/* old security info + new attr header(8) + new attr */
66910Sstevel@tonic-gate 	oldsize = *secinfo_len;
66920Sstevel@tonic-gate 	*secinfo_len += newattrsize;
66930Sstevel@tonic-gate 	new_secinfo = e_zalloc(E_NORMAL, (uint_t)*secinfo_len);
66940Sstevel@tonic-gate 	if (new_secinfo == NULL) {
66950Sstevel@tonic-gate 		(void) fprintf(stderr, "can't allocate memory\n");
66960Sstevel@tonic-gate 		*secinfo_len -= newattrsize;
66970Sstevel@tonic-gate 		return (-1);
66980Sstevel@tonic-gate 	}
66990Sstevel@tonic-gate 
67000Sstevel@tonic-gate 	(void) memcpy(new_secinfo, *secinfo, oldsize);
67010Sstevel@tonic-gate 	(void) memcpy(new_secinfo + oldsize, attr, newattrsize);
67020Sstevel@tonic-gate 
67030Sstevel@tonic-gate 	free(*secinfo);
67040Sstevel@tonic-gate 	*secinfo = new_secinfo;
67050Sstevel@tonic-gate 	return (0);
67060Sstevel@tonic-gate }
67070Sstevel@tonic-gate 
67080Sstevel@tonic-gate static void
67090Sstevel@tonic-gate write_ancillary(char *secinfo, int len)
67100Sstevel@tonic-gate {
67110Sstevel@tonic-gate 	long    pad;
67120Sstevel@tonic-gate 	long    cnt;
67130Sstevel@tonic-gate 
67140Sstevel@tonic-gate 	/* Just tranditional permissions or no security attribute info */
67150Sstevel@tonic-gate 	if (len == 0) {
67160Sstevel@tonic-gate 		return;
67170Sstevel@tonic-gate 	}
67180Sstevel@tonic-gate 
67190Sstevel@tonic-gate 	/* write out security info */
67200Sstevel@tonic-gate 	while (len > 0) {
67210Sstevel@tonic-gate 		cnt = (unsigned)(len > CPIOBSZ) ? CPIOBSZ : len;
67220Sstevel@tonic-gate 		FLUSH(cnt);
67230Sstevel@tonic-gate 		errno = 0;
67240Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, secinfo, (unsigned)cnt);
67250Sstevel@tonic-gate 		Buffr.b_in_p += cnt;
67260Sstevel@tonic-gate 		Buffr.b_cnt += (long)cnt;
67270Sstevel@tonic-gate 		len -= (long)cnt;
67280Sstevel@tonic-gate 	}
67290Sstevel@tonic-gate 	pad = (Pad_val + 1 - (cnt & Pad_val)) & Pad_val;
67300Sstevel@tonic-gate 	if (pad != 0) {
67310Sstevel@tonic-gate 		FLUSH(pad);
67320Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, Empty, pad);
67330Sstevel@tonic-gate 		Buffr.b_in_p += pad;
67340Sstevel@tonic-gate 		Buffr.b_cnt += pad;
67350Sstevel@tonic-gate 	}
67360Sstevel@tonic-gate }
67370Sstevel@tonic-gate 
67380Sstevel@tonic-gate static int
67390Sstevel@tonic-gate remove_dir(char *path)
67400Sstevel@tonic-gate {
67410Sstevel@tonic-gate 	DIR		*name;
67420Sstevel@tonic-gate 	struct dirent	*direct;
67430Sstevel@tonic-gate 	struct stat	sbuf;
67440Sstevel@tonic-gate 	char		*path_copy;
67450Sstevel@tonic-gate 
67460Sstevel@tonic-gate #define	MSG1	"remove_dir() failed to stat(\"%s\") "
67470Sstevel@tonic-gate #define	MSG2	"remove_dir() failed to remove_dir(\"%s\") "
67480Sstevel@tonic-gate #define	MSG3	"remove_dir() failed to unlink(\"%s\") "
67490Sstevel@tonic-gate 
67500Sstevel@tonic-gate 	/*
67510Sstevel@tonic-gate 	 * Open the directory for reading.
67520Sstevel@tonic-gate 	 */
67530Sstevel@tonic-gate 	if ((name = opendir(path)) == NULL) {
67540Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to opendir(\"%s\") ", path);
67550Sstevel@tonic-gate 		return (-1);
67560Sstevel@tonic-gate 	}
67570Sstevel@tonic-gate 
67580Sstevel@tonic-gate 	if (chdir(path) == -1) {
67590Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to chdir(\"%s\") ", path);
67600Sstevel@tonic-gate 		return (-1);
67610Sstevel@tonic-gate 	}
67620Sstevel@tonic-gate 
67630Sstevel@tonic-gate 	/*
67640Sstevel@tonic-gate 	 * Read every directory entry.
67650Sstevel@tonic-gate 	 */
67660Sstevel@tonic-gate 	while ((direct = readdir(name)) != NULL) {
67670Sstevel@tonic-gate 		/*
67680Sstevel@tonic-gate 		 * Ignore "." and ".." entries.
67690Sstevel@tonic-gate 		 */
67700Sstevel@tonic-gate 		if (strcmp(direct->d_name, ".") == 0 ||
67710Sstevel@tonic-gate 		    strcmp(direct->d_name, "..") == 0)
67720Sstevel@tonic-gate 			continue;
67730Sstevel@tonic-gate 
67740Sstevel@tonic-gate 			if (lstat(direct->d_name, &sbuf) == -1) {
67750Sstevel@tonic-gate 				msg(ERRN, MSG1, direct->d_name);
67760Sstevel@tonic-gate 				(void) closedir(name);
67770Sstevel@tonic-gate 			return (-1);
67780Sstevel@tonic-gate 		}
67790Sstevel@tonic-gate 
67800Sstevel@tonic-gate 		if (S_ISDIR(sbuf.st_mode)) {
67810Sstevel@tonic-gate 			if (remove_dir(direct->d_name) == -1) {
67820Sstevel@tonic-gate 				msg(ERRN, MSG2, direct->d_name);
67830Sstevel@tonic-gate 				(void) closedir(name);
67840Sstevel@tonic-gate 				return (-1);
67850Sstevel@tonic-gate 			}
67860Sstevel@tonic-gate 		} else {
67870Sstevel@tonic-gate 			if (unlink(direct->d_name) == -1) {
67880Sstevel@tonic-gate 				msg(ERRN, MSG3, direct->d_name);
67890Sstevel@tonic-gate 				(void) closedir(name);
67900Sstevel@tonic-gate 				return (-1);
67910Sstevel@tonic-gate 			}
67920Sstevel@tonic-gate 		}
67930Sstevel@tonic-gate 
67940Sstevel@tonic-gate 	}
67950Sstevel@tonic-gate 
67960Sstevel@tonic-gate 	/*
67970Sstevel@tonic-gate 	 * Close the directory we just finished reading.
67980Sstevel@tonic-gate 	 */
67990Sstevel@tonic-gate 	(void) closedir(name);
68000Sstevel@tonic-gate 
68010Sstevel@tonic-gate 	/*
68020Sstevel@tonic-gate 	 * Change directory to the parent directory...
68030Sstevel@tonic-gate 	 */
68040Sstevel@tonic-gate 	if (chdir("..") == -1) {
68050Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to chdir(\"..\") ");
68060Sstevel@tonic-gate 		return (-1);
68070Sstevel@tonic-gate 	}
68080Sstevel@tonic-gate 
68090Sstevel@tonic-gate 	/*
68100Sstevel@tonic-gate 	 * ...and finally remove the directory; note we have to
68110Sstevel@tonic-gate 	 * make a copy since basename is free to modify its input.
68120Sstevel@tonic-gate 	 */
68130Sstevel@tonic-gate 	path_copy = e_strdup(E_NORMAL, path);
68140Sstevel@tonic-gate 	if (path_copy == NULL) {
68150Sstevel@tonic-gate 		msg(ERRN, "cannot strdup() the directory pathname ");
68160Sstevel@tonic-gate 		return (-1);
68170Sstevel@tonic-gate 	}
68180Sstevel@tonic-gate 
68190Sstevel@tonic-gate 	if (rmdir(basename(path_copy)) == -1) {
68200Sstevel@tonic-gate 		free(path_copy);
68210Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to rmdir(\"%s\") ", path);
68220Sstevel@tonic-gate 		return (-1);
68230Sstevel@tonic-gate 	}
68240Sstevel@tonic-gate 
68250Sstevel@tonic-gate 	free(path_copy);
68260Sstevel@tonic-gate 	return (0);
68270Sstevel@tonic-gate 
68280Sstevel@tonic-gate }
68290Sstevel@tonic-gate 
68300Sstevel@tonic-gate static int
68310Sstevel@tonic-gate save_cwd(void)
68320Sstevel@tonic-gate {
68330Sstevel@tonic-gate 	return (open(".", O_RDONLY));
68340Sstevel@tonic-gate }
68350Sstevel@tonic-gate 
68360Sstevel@tonic-gate static void
68370Sstevel@tonic-gate rest_cwd(int cwd)
68380Sstevel@tonic-gate {
68390Sstevel@tonic-gate 	(void) fchdir(cwd);
68400Sstevel@tonic-gate 	(void) close(cwd);
68410Sstevel@tonic-gate }
68420Sstevel@tonic-gate 
68430Sstevel@tonic-gate #if defined(O_XATTR)
68440Sstevel@tonic-gate static void
68450Sstevel@tonic-gate xattrs_out(int (*func)())
68460Sstevel@tonic-gate {
68470Sstevel@tonic-gate 	int dirpfd;
68480Sstevel@tonic-gate 	int filefd;
68490Sstevel@tonic-gate 	DIR *dirp;
68500Sstevel@tonic-gate 	struct dirent *dp;
68510Sstevel@tonic-gate 	int slen;
68520Sstevel@tonic-gate 	char *namep, *savenamep;
68530Sstevel@tonic-gate 
68540Sstevel@tonic-gate 	if (pathconf(G_p->g_nam_p, _PC_XATTR_EXISTS) != 1) {
68550Sstevel@tonic-gate 		return;
68560Sstevel@tonic-gate 	}
68570Sstevel@tonic-gate 
68580Sstevel@tonic-gate 	/*
68590Sstevel@tonic-gate 	 * If aclp still exists then free it since it is was set when base
68600Sstevel@tonic-gate 	 * file was extracted.
68610Sstevel@tonic-gate 	 */
6862789Sahrens 	if (aclp != NULL) {
6863789Sahrens 		acl_free(aclp);
68640Sstevel@tonic-gate 		aclp = NULL;
6865789Sahrens 		acl_is_set = 0;
68660Sstevel@tonic-gate 	}
68670Sstevel@tonic-gate 
68680Sstevel@tonic-gate 	Gen.g_dirfd = attropen(G_p->g_nam_p, ".", O_RDONLY);
68690Sstevel@tonic-gate 	if (Gen.g_dirfd == -1) {
68700Sstevel@tonic-gate 		msg(ERRN, "Cannot open attribute directory of file \"%s\"",
68710Sstevel@tonic-gate 		    G_p->g_nam_p);
68720Sstevel@tonic-gate 		return;
68730Sstevel@tonic-gate 
68740Sstevel@tonic-gate 	}
68750Sstevel@tonic-gate 	savenamep = G_p->g_nam_p;
68760Sstevel@tonic-gate 
68770Sstevel@tonic-gate 	if ((dirpfd = dup(Gen.g_dirfd)) == -1)  {
68780Sstevel@tonic-gate 		msg(ERRN, "Cannot dup(2) attribute directory descriptor");
68790Sstevel@tonic-gate 		return;
68800Sstevel@tonic-gate 	}
68810Sstevel@tonic-gate 
68820Sstevel@tonic-gate 	if ((dirp = fdopendir(dirpfd)) == (DIR *)NULL) {
68830Sstevel@tonic-gate 		msg(ERRN, "Cannot fdopendir(2) directory file descriptor");
68840Sstevel@tonic-gate 		return;
68850Sstevel@tonic-gate 	}
68860Sstevel@tonic-gate 
68870Sstevel@tonic-gate 	while ((dp = readdir(dirp)) != (struct dirent *)NULL) {
68880Sstevel@tonic-gate 		if (strcmp(dp->d_name, "..") == 0) {
68890Sstevel@tonic-gate 			continue;
68900Sstevel@tonic-gate 		}
68910Sstevel@tonic-gate 
68920Sstevel@tonic-gate 		if (strcmp(dp->d_name, ".") == 0) {
68930Sstevel@tonic-gate 			Hiddendir = 1;
68940Sstevel@tonic-gate 		} else {
68950Sstevel@tonic-gate 			Hiddendir = 0;
68960Sstevel@tonic-gate 		}
68970Sstevel@tonic-gate 
68980Sstevel@tonic-gate 		Gen.g_attrnam_p = dp->d_name;
68990Sstevel@tonic-gate 
69000Sstevel@tonic-gate 		if (STAT(Gen.g_dirfd, Gen.g_nam_p, &SrcSt) == -1) {
69010Sstevel@tonic-gate 			msg(ERRN,
69020Sstevel@tonic-gate 			    "Could not fstatat(2) attribute \"%s\" of"
69030Sstevel@tonic-gate 			    " file \"%s\"", dp->d_name, savenamep);
69040Sstevel@tonic-gate 			continue;
69050Sstevel@tonic-gate 		}
69060Sstevel@tonic-gate 
69070Sstevel@tonic-gate 		if (Use_old_stat) {
69080Sstevel@tonic-gate 			OldSt = convert_to_old_stat(&SrcSt,
69090Sstevel@tonic-gate 			    Gen.g_nam_p, Gen.g_attrnam_p);
69100Sstevel@tonic-gate 
69110Sstevel@tonic-gate 			if (OldSt == NULL) {
69120Sstevel@tonic-gate 				msg(ERRN,
69130Sstevel@tonic-gate 				    "Could not convert to old stat format");
69140Sstevel@tonic-gate 				continue;
69150Sstevel@tonic-gate 			}
69160Sstevel@tonic-gate 		}
69170Sstevel@tonic-gate 
69180Sstevel@tonic-gate 		Gen.g_attrfnam_p = savenamep;
69190Sstevel@tonic-gate 
69200Sstevel@tonic-gate 		/*
69210Sstevel@tonic-gate 		 * Set up dummy header name
69220Sstevel@tonic-gate 		 *
69230Sstevel@tonic-gate 		 * One piece is written with .hdr, which
69240Sstevel@tonic-gate 		 * contains the actual xattr hdr or pathing information
69250Sstevel@tonic-gate 		 * then the name is updated to drop the .hdr off
69260Sstevel@tonic-gate 		 * and the actual file itself is archived.
69270Sstevel@tonic-gate 		 */
69280Sstevel@tonic-gate 		slen = strlen(Gen.g_attrnam_p) + strlen(DEVNULL) +
69290Sstevel@tonic-gate 		    strlen(XATTRHDR) + 1;
69300Sstevel@tonic-gate 		if ((namep = e_zalloc(E_NORMAL, slen)) == (char *)NULL) {
69310Sstevel@tonic-gate 			msg(ERRN, "Could not calloc memory for attribute name");
69320Sstevel@tonic-gate 			continue;
69330Sstevel@tonic-gate 		}
69340Sstevel@tonic-gate 		(void) sprintf(namep, "%s/%s%s",
69350Sstevel@tonic-gate 		    DEVNULL, Gen.g_attrnam_p, XATTRHDR);
69360Sstevel@tonic-gate 		Gen.g_nam_p = namep;
69370Sstevel@tonic-gate 
69380Sstevel@tonic-gate 		/*
69390Sstevel@tonic-gate 		 * Get attribute's ACL info: don't bother allocating space
69400Sstevel@tonic-gate 		 * if there are only standard permissions, i.e. ACL count < 4
69410Sstevel@tonic-gate 		 */
69420Sstevel@tonic-gate 		if (Pflag) {
69430Sstevel@tonic-gate 			filefd = openat(Gen.g_dirfd, dp->d_name, O_RDONLY);
69440Sstevel@tonic-gate 			if (filefd == -1) {
69450Sstevel@tonic-gate 				msg(ERRN,
69460Sstevel@tonic-gate 				    "Could not open attribute \"%s\" of"
69470Sstevel@tonic-gate 				    " file \"%s\"", dp->d_name, savenamep);
69480Sstevel@tonic-gate 				free(namep);
69490Sstevel@tonic-gate 				continue;
69500Sstevel@tonic-gate 			}
6951789Sahrens 			if (facl_get(filefd, ACL_NO_TRIVIAL, &aclp) != 0) {
69520Sstevel@tonic-gate 				msg(ERRN,
69530Sstevel@tonic-gate 				    "Error with acl() on %s",
69540Sstevel@tonic-gate 				    Gen.g_nam_p);
69550Sstevel@tonic-gate 			}
69560Sstevel@tonic-gate 			(void) close(filefd);
69570Sstevel@tonic-gate 		}
69580Sstevel@tonic-gate 		(void) creat_hdr();
69590Sstevel@tonic-gate 		(void) (*func)();
69600Sstevel@tonic-gate 		if (Gen.g_passdirfd != -1) {
69610Sstevel@tonic-gate 			(void) close(Gen.g_passdirfd);
69620Sstevel@tonic-gate 			Gen.g_passdirfd = -1;
69630Sstevel@tonic-gate 		}
69640Sstevel@tonic-gate 		Gen.g_attrnam_p = (char *)NULL;
69650Sstevel@tonic-gate 		Gen.g_attrfnam_p = (char *)NULL;
69660Sstevel@tonic-gate 		Gen.g_linktoattrfnam_p = (char *)NULL;
69670Sstevel@tonic-gate 		Gen.g_linktoattrnam_p = (char *)NULL;
6968789Sahrens 		if (aclp != NULL) {
6969789Sahrens 			acl_free(aclp);
69700Sstevel@tonic-gate 			aclp = NULL;
6971789Sahrens 			acl_is_set = 0;
69720Sstevel@tonic-gate 		}
69730Sstevel@tonic-gate 		free(namep);
69740Sstevel@tonic-gate 	}
69750Sstevel@tonic-gate 
69760Sstevel@tonic-gate 	(void) closedir(dirp);
69770Sstevel@tonic-gate 	(void) close(Gen.g_dirfd);
69780Sstevel@tonic-gate 	Gen.g_dirfd = -1;
69790Sstevel@tonic-gate }
69800Sstevel@tonic-gate #else
69810Sstevel@tonic-gate static void
69820Sstevel@tonic-gate xattrs_out(int (*func)())
69830Sstevel@tonic-gate {
69840Sstevel@tonic-gate }
69850Sstevel@tonic-gate #endif
69860Sstevel@tonic-gate 
69870Sstevel@tonic-gate /*
69880Sstevel@tonic-gate  * Return the parent directory of a given path.
69890Sstevel@tonic-gate  *
69900Sstevel@tonic-gate  * Examples:
69910Sstevel@tonic-gate  * /usr/tmp return /usr
69920Sstevel@tonic-gate  * /usr/tmp/file return /usr/tmp
69930Sstevel@tonic-gate  * /  returns .
69940Sstevel@tonic-gate  * /usr returns /
69950Sstevel@tonic-gate  * file returns .
69960Sstevel@tonic-gate  *
69970Sstevel@tonic-gate  * dir is assumed to be at least as big as path.
69980Sstevel@tonic-gate  */
69990Sstevel@tonic-gate static void
70000Sstevel@tonic-gate get_parent(char *path, char *dir)
70010Sstevel@tonic-gate {
70020Sstevel@tonic-gate 	char *s;
70030Sstevel@tonic-gate 	char tmpdir[PATH_MAX + 1];
70040Sstevel@tonic-gate 
70050Sstevel@tonic-gate 	if (strlen(path) > PATH_MAX) {
70060Sstevel@tonic-gate 		msg(EXT, "pathname is too long");
70070Sstevel@tonic-gate 	}
70080Sstevel@tonic-gate 	(void) strcpy(tmpdir, path);
70090Sstevel@tonic-gate 	chop_endslashes(tmpdir);
70100Sstevel@tonic-gate 
70110Sstevel@tonic-gate 	if ((s = strrchr(tmpdir, '/')) == NULL) {
70120Sstevel@tonic-gate 		(void) strcpy(dir, ".");
70130Sstevel@tonic-gate 	} else {
70140Sstevel@tonic-gate 		s = skipslashes(s, tmpdir);
70150Sstevel@tonic-gate 		*s = '\0';
70160Sstevel@tonic-gate 		if (s == tmpdir)
70170Sstevel@tonic-gate 			(void) strcpy(dir, "/");
70180Sstevel@tonic-gate 		else
70190Sstevel@tonic-gate 			(void) strcpy(dir, tmpdir);
70200Sstevel@tonic-gate 	}
70210Sstevel@tonic-gate }
70220Sstevel@tonic-gate 
70230Sstevel@tonic-gate #if defined(O_XATTR)
70240Sstevel@tonic-gate #define	ROUNDTOTBLOCK(a)		((a + (TBLOCK -1)) & ~(TBLOCK -1))
70250Sstevel@tonic-gate 
70260Sstevel@tonic-gate static void
70270Sstevel@tonic-gate prepare_xattr_hdr(
70280Sstevel@tonic-gate 	char		**attrbuf,
70290Sstevel@tonic-gate 	char		*filename,
70300Sstevel@tonic-gate 	char		*attrname,
70310Sstevel@tonic-gate 	char		typeflag,
70320Sstevel@tonic-gate 	struct Lnk	*linkinfo,
70330Sstevel@tonic-gate 	int		*rlen)
70340Sstevel@tonic-gate {
70350Sstevel@tonic-gate 	char			*bufhead;	/* ptr to full buffer */
70360Sstevel@tonic-gate 	struct xattr_hdr 	*hptr;		/* ptr to header in bufhead */
70370Sstevel@tonic-gate 	struct xattr_buf	*tptr;		/* ptr to pathing pieces */
70380Sstevel@tonic-gate 	int			totalen;	/* total buffer length */
70390Sstevel@tonic-gate 	int			len;		/* length returned to user */
70400Sstevel@tonic-gate 	int			stringlen;	/* length of filename + attr */
70410Sstevel@tonic-gate 	int			linkstringlen;	/* ditto in link section */
70420Sstevel@tonic-gate 	int			complen;	/* length of pathing section */
70430Sstevel@tonic-gate 	int			linklen;	/* length of link section */
70440Sstevel@tonic-gate 
70450Sstevel@tonic-gate 
70460Sstevel@tonic-gate 	/*
70470Sstevel@tonic-gate 	 * Release previous buffer if any.
70480Sstevel@tonic-gate 	 */
70490Sstevel@tonic-gate 
70500Sstevel@tonic-gate 	if (*attrbuf != (char *)NULL) {
70510Sstevel@tonic-gate 		free(*attrbuf);
70520Sstevel@tonic-gate 		*attrbuf = NULL;
70530Sstevel@tonic-gate 	}
70540Sstevel@tonic-gate 
70550Sstevel@tonic-gate 	/*
70560Sstevel@tonic-gate 	 * First add in fixed size stuff
70570Sstevel@tonic-gate 	 */
70580Sstevel@tonic-gate 	len = sizeof (struct xattr_hdr) + sizeof (struct xattr_buf);
70590Sstevel@tonic-gate 
70600Sstevel@tonic-gate 	/*
70610Sstevel@tonic-gate 	 * Add space for two nulls
70620Sstevel@tonic-gate 	 */
70630Sstevel@tonic-gate 	stringlen = strlen(attrname) + strlen(filename) + 2;
70640Sstevel@tonic-gate 	complen = stringlen + sizeof (struct xattr_buf);
70650Sstevel@tonic-gate 
70660Sstevel@tonic-gate 	len += stringlen;
70670Sstevel@tonic-gate 
70680Sstevel@tonic-gate 	/*
70690Sstevel@tonic-gate 	 * Now add on space for link info if any
70700Sstevel@tonic-gate 	 */
70710Sstevel@tonic-gate 
70720Sstevel@tonic-gate 	if (linkinfo != NULL) {
70730Sstevel@tonic-gate 		/*
70740Sstevel@tonic-gate 		 * Again add space for two nulls
70750Sstevel@tonic-gate 		 */
70760Sstevel@tonic-gate 		linkstringlen = strlen(linkinfo->L_gen.g_attrfnam_p) +
70770Sstevel@tonic-gate 		    strlen(linkinfo->L_gen.g_attrnam_p) + 2;
70780Sstevel@tonic-gate 		len += linkstringlen;
70790Sstevel@tonic-gate 	}
70800Sstevel@tonic-gate 
70810Sstevel@tonic-gate 	/*
70820Sstevel@tonic-gate 	 * Now add padding to end to fill out TBLOCK
70830Sstevel@tonic-gate 	 *
70840Sstevel@tonic-gate 	 * Function returns size of real data and not size + padding.
70850Sstevel@tonic-gate 	 */
70860Sstevel@tonic-gate 
70870Sstevel@tonic-gate 	totalen = ROUNDTOTBLOCK(len);
70880Sstevel@tonic-gate 	bufhead = e_zalloc(E_EXIT, totalen);
70890Sstevel@tonic-gate 
70900Sstevel@tonic-gate 	/*
70910Sstevel@tonic-gate 	 * Now we can fill in the necessary pieces
70920Sstevel@tonic-gate 	 */
70930Sstevel@tonic-gate 
70940Sstevel@tonic-gate 	if (linkinfo != (struct Lnk *)NULL) {
70950Sstevel@tonic-gate 		linklen = linkstringlen + (sizeof (struct xattr_buf));
70960Sstevel@tonic-gate 	} else {
70970Sstevel@tonic-gate 		linklen = 0;
70980Sstevel@tonic-gate 	}
70990Sstevel@tonic-gate 
71000Sstevel@tonic-gate 	/*
71010Sstevel@tonic-gate 	 * first fill in the fixed header
71020Sstevel@tonic-gate 	 */
71030Sstevel@tonic-gate 	hptr = (struct xattr_hdr *)bufhead;
71040Sstevel@tonic-gate 	(void) sprintf(hptr->h_version, "%s", XATTR_ARCH_VERS);
71050Sstevel@tonic-gate 	(void) sprintf(hptr->h_component_len, "%0*d",
71060Sstevel@tonic-gate 	    sizeof (hptr->h_component_len) - 1, complen);
71070Sstevel@tonic-gate 	(void) sprintf(hptr->h_link_component_len, "%0*d",
71080Sstevel@tonic-gate 	    sizeof (hptr->h_link_component_len) - 1, linklen);
71090Sstevel@tonic-gate 	(void) sprintf(hptr->h_size, "%0*d", sizeof (hptr->h_size) - 1, len);
71100Sstevel@tonic-gate 
71110Sstevel@tonic-gate 	/*
71120Sstevel@tonic-gate 	 * Now fill in the filename + attrnames section
71130Sstevel@tonic-gate 	 */
71140Sstevel@tonic-gate 
71150Sstevel@tonic-gate 	tptr = (struct xattr_buf *)(bufhead + sizeof (struct xattr_hdr));
71160Sstevel@tonic-gate 	(void) sprintf(tptr->h_namesz, "%0*d", sizeof (tptr->h_namesz) - 1,
71170Sstevel@tonic-gate 	    stringlen);
71180Sstevel@tonic-gate 	(void) strcpy(tptr->h_names, filename);
71190Sstevel@tonic-gate 	(void) strcpy(&tptr->h_names[strlen(filename) + 1], attrname);
71200Sstevel@tonic-gate 	tptr->h_typeflag = typeflag;
71210Sstevel@tonic-gate 
71220Sstevel@tonic-gate 	/*
71230Sstevel@tonic-gate 	 * Now fill in the optional link section if we have one
71240Sstevel@tonic-gate 	 */
71250Sstevel@tonic-gate 
71260Sstevel@tonic-gate 	if (linkinfo != (struct Lnk *)NULL) {
71270Sstevel@tonic-gate 		tptr = (struct xattr_buf *)(bufhead +
71280Sstevel@tonic-gate 		    sizeof (struct xattr_hdr) + complen);
71290Sstevel@tonic-gate 
71300Sstevel@tonic-gate 		(void) sprintf(tptr->h_namesz, "%0*d",
71310Sstevel@tonic-gate 		    sizeof (tptr->h_namesz) - 1, linkstringlen);
71320Sstevel@tonic-gate 		(void) strcpy(tptr->h_names, linkinfo->L_gen.g_attrfnam_p);
71330Sstevel@tonic-gate 		(void) strcpy(
71340Sstevel@tonic-gate 		    &tptr->h_names[strlen(linkinfo->L_gen.g_attrfnam_p) + 1],
71350Sstevel@tonic-gate 		    linkinfo->L_gen.g_attrnam_p);
71360Sstevel@tonic-gate 		tptr->h_typeflag = typeflag;
71370Sstevel@tonic-gate 	}
71380Sstevel@tonic-gate 	*attrbuf = (char *)bufhead;
71390Sstevel@tonic-gate 	*rlen = len;
71400Sstevel@tonic-gate }
71410Sstevel@tonic-gate #endif /* O_XATTR */
71420Sstevel@tonic-gate 
71430Sstevel@tonic-gate static char
71440Sstevel@tonic-gate tartype(int type)
71450Sstevel@tonic-gate {
71460Sstevel@tonic-gate 	switch (type) {
71470Sstevel@tonic-gate 
71480Sstevel@tonic-gate 	case S_IFDIR:
71490Sstevel@tonic-gate 		return (DIRTYPE);
71500Sstevel@tonic-gate 
71510Sstevel@tonic-gate 	case S_IFLNK:
71520Sstevel@tonic-gate 		return (SYMTYPE);
71530Sstevel@tonic-gate 
71540Sstevel@tonic-gate 	case S_IFIFO:
71550Sstevel@tonic-gate 		return (FIFOTYPE);
71560Sstevel@tonic-gate 
71570Sstevel@tonic-gate 	case S_IFCHR:
71580Sstevel@tonic-gate 		return (CHRTYPE);
71590Sstevel@tonic-gate 
71600Sstevel@tonic-gate 	case S_IFBLK:
71610Sstevel@tonic-gate 		return (BLKTYPE);
71620Sstevel@tonic-gate 
71630Sstevel@tonic-gate 	case S_IFREG:
71640Sstevel@tonic-gate 		return (REGTYPE);
71650Sstevel@tonic-gate 
71660Sstevel@tonic-gate 	default:
71670Sstevel@tonic-gate 		return ('\0');
71680Sstevel@tonic-gate 	}
71690Sstevel@tonic-gate }
71700Sstevel@tonic-gate 
71710Sstevel@tonic-gate #if defined(O_XATTR)
71720Sstevel@tonic-gate static int
71730Sstevel@tonic-gate openfile(int omode)
71740Sstevel@tonic-gate {
71750Sstevel@tonic-gate 
71760Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
71770Sstevel@tonic-gate 		return (attropen(G_p->g_attrfnam_p, G_p->g_attrnam_p, omode));
71780Sstevel@tonic-gate 	} else {
71790Sstevel@tonic-gate 		return (openat(G_p->g_dirfd,
71800Sstevel@tonic-gate 		    get_component(G_p->g_nam_p), omode));
71810Sstevel@tonic-gate 	}
71820Sstevel@tonic-gate }
71830Sstevel@tonic-gate #else
71840Sstevel@tonic-gate static int
71850Sstevel@tonic-gate openfile(int omode)
71860Sstevel@tonic-gate {
71870Sstevel@tonic-gate 	return (openat(G_p->g_dirfd, get_component(G_p->g_nam_p), omode));
71880Sstevel@tonic-gate }
71890Sstevel@tonic-gate #endif
71900Sstevel@tonic-gate 
71910Sstevel@tonic-gate #if defined(O_XATTR)
71920Sstevel@tonic-gate static int
71930Sstevel@tonic-gate read_xattr_hdr()
71940Sstevel@tonic-gate {
71950Sstevel@tonic-gate 	off_t		bytes;
71960Sstevel@tonic-gate 	int		comp_len, link_len;
71970Sstevel@tonic-gate 	int		namelen;
71980Sstevel@tonic-gate 	int		cnt;
71990Sstevel@tonic-gate 	char		*tp;
72000Sstevel@tonic-gate 	int		pad;
72010Sstevel@tonic-gate 
72020Sstevel@tonic-gate 	/*
72030Sstevel@tonic-gate 	 * Include any padding in the read.  We need to be positioned
72040Sstevel@tonic-gate 	 * at beginning of next header.
72050Sstevel@tonic-gate 	 */
72060Sstevel@tonic-gate 
72070Sstevel@tonic-gate 	bytes = Gen.g_filesz;
72080Sstevel@tonic-gate 
72090Sstevel@tonic-gate 	if ((xattrhead = e_zalloc(E_NORMAL, (size_t)bytes)) == NULL) {
72100Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
72110Sstevel@tonic-gate 		    "Insufficient memory for extended attribute\n"));
72120Sstevel@tonic-gate 		return (1);
72130Sstevel@tonic-gate 	}
72140Sstevel@tonic-gate 
72150Sstevel@tonic-gate 	tp = (char *)xattrhead;
72160Sstevel@tonic-gate 	while (bytes > 0) {
72170Sstevel@tonic-gate 		cnt = (int)(bytes > CPIOBSZ) ? CPIOBSZ : bytes;
72180Sstevel@tonic-gate 		FILL(cnt);
72190Sstevel@tonic-gate 		(void) memcpy(tp, Buffr.b_out_p, cnt);
72200Sstevel@tonic-gate 		tp += cnt;
72210Sstevel@tonic-gate 		Buffr.b_out_p += cnt;
72220Sstevel@tonic-gate 		Buffr.b_cnt -= (off_t)cnt;
72230Sstevel@tonic-gate 		bytes -= (off_t)cnt;
72240Sstevel@tonic-gate 	}
72250Sstevel@tonic-gate 
72260Sstevel@tonic-gate 	pad = (Pad_val + 1 - (Gen.g_filesz & Pad_val)) &
72270Sstevel@tonic-gate 	    Pad_val;
72280Sstevel@tonic-gate 	if (pad != 0) {
72290Sstevel@tonic-gate 		FILL(pad);
72300Sstevel@tonic-gate 		Buffr.b_out_p += pad;
72310Sstevel@tonic-gate 		Buffr.b_cnt -= (off_t)pad;
72320Sstevel@tonic-gate 	}
72330Sstevel@tonic-gate 
72340Sstevel@tonic-gate 	/*
72350Sstevel@tonic-gate 	 * Validate that we can handle header format
72360Sstevel@tonic-gate 	 */
72370Sstevel@tonic-gate 
72380Sstevel@tonic-gate 	if (strcmp(xattrhead->h_version, XATTR_ARCH_VERS) != 0) {
72390Sstevel@tonic-gate 		(void) fprintf(stderr,
72400Sstevel@tonic-gate 		    gettext("Unknown extended attribute format encountered\n"));
72410Sstevel@tonic-gate 		(void) fprintf(stderr,
72420Sstevel@tonic-gate 		    gettext("Disabling extended attribute header parsing\n"));
72430Sstevel@tonic-gate 		xattrbadhead = 1;
72440Sstevel@tonic-gate 		return (1);
72450Sstevel@tonic-gate 	}
72460Sstevel@tonic-gate 	(void) sscanf(xattrhead->h_component_len, "%10d", &comp_len);
72470Sstevel@tonic-gate 	(void) sscanf(xattrhead->h_link_component_len, "%10d", &link_len);
72480Sstevel@tonic-gate 	xattrp = (struct xattr_buf *)(((char *)xattrhead) +
72490Sstevel@tonic-gate 	    sizeof (struct xattr_hdr));
72500Sstevel@tonic-gate 	(void) sscanf(xattrp->h_namesz, "%7d", &namelen);
72510Sstevel@tonic-gate 	if (link_len > 0) {
72520Sstevel@tonic-gate 		xattr_linkp = (struct xattr_buf *)((int)xattrp + (int)comp_len);
72530Sstevel@tonic-gate 	} else {
72540Sstevel@tonic-gate 		xattr_linkp = NULL;
72550Sstevel@tonic-gate 	}
72560Sstevel@tonic-gate 
72570Sstevel@tonic-gate 	return (0);
72580Sstevel@tonic-gate }
72590Sstevel@tonic-gate #endif
72600Sstevel@tonic-gate 
72610Sstevel@tonic-gate static mode_t
72620Sstevel@tonic-gate attrmode(char type)
72630Sstevel@tonic-gate {
72640Sstevel@tonic-gate 	mode_t mode;
72650Sstevel@tonic-gate 
72660Sstevel@tonic-gate 	switch (type) {
72670Sstevel@tonic-gate 	case '\0':
72680Sstevel@tonic-gate 	case REGTYPE:
72690Sstevel@tonic-gate 	case LNKTYPE:
72700Sstevel@tonic-gate 		mode = S_IFREG;
72710Sstevel@tonic-gate 		break;
72720Sstevel@tonic-gate 
72730Sstevel@tonic-gate 	case SYMTYPE:
72740Sstevel@tonic-gate 		mode = S_IFLNK;
72750Sstevel@tonic-gate 		break;
72760Sstevel@tonic-gate 
72770Sstevel@tonic-gate 	case CHRTYPE:
72780Sstevel@tonic-gate 		mode = S_IFCHR;
72790Sstevel@tonic-gate 		break;
72800Sstevel@tonic-gate 	case BLKTYPE:
72810Sstevel@tonic-gate 		mode = S_IFBLK;
72820Sstevel@tonic-gate 		break;
72830Sstevel@tonic-gate 	case DIRTYPE:
72840Sstevel@tonic-gate 		mode = S_IFDIR;
72850Sstevel@tonic-gate 		break;
72860Sstevel@tonic-gate 	case FIFOTYPE:
72870Sstevel@tonic-gate 		mode = S_IFIFO;
72880Sstevel@tonic-gate 		break;
72890Sstevel@tonic-gate 	case CONTTYPE:
72900Sstevel@tonic-gate 	default:
72910Sstevel@tonic-gate 		mode = 0;
72920Sstevel@tonic-gate 	}
72930Sstevel@tonic-gate 
72940Sstevel@tonic-gate 	return (mode);
72950Sstevel@tonic-gate }
72960Sstevel@tonic-gate 
72970Sstevel@tonic-gate #if defined(O_XATTR)
72980Sstevel@tonic-gate static char *
72990Sstevel@tonic-gate get_component(char *path)
73000Sstevel@tonic-gate {
73010Sstevel@tonic-gate 	char *ptr;
73020Sstevel@tonic-gate 
73030Sstevel@tonic-gate 	ptr = strrchr(path, '/');
73040Sstevel@tonic-gate 	if (ptr == NULL) {
73050Sstevel@tonic-gate 		return (path);
73060Sstevel@tonic-gate 	} else {
73070Sstevel@tonic-gate 		/*
73080Sstevel@tonic-gate 		 * Handle trailing slash
73090Sstevel@tonic-gate 		 */
73100Sstevel@tonic-gate 		if (*(ptr + 1) == '\0')
73110Sstevel@tonic-gate 			return (ptr);
73120Sstevel@tonic-gate 		else
73130Sstevel@tonic-gate 			return (ptr + 1);
73140Sstevel@tonic-gate 	}
73150Sstevel@tonic-gate }
73160Sstevel@tonic-gate #else
73170Sstevel@tonic-gate static char *
73180Sstevel@tonic-gate get_component(char *path)
73190Sstevel@tonic-gate {
73200Sstevel@tonic-gate 	return (path);
73210Sstevel@tonic-gate }
73220Sstevel@tonic-gate #endif
73230Sstevel@tonic-gate 
73240Sstevel@tonic-gate static int
73250Sstevel@tonic-gate open_dir(char *name)
73260Sstevel@tonic-gate {
73270Sstevel@tonic-gate 	int fd = -1;
73280Sstevel@tonic-gate 	int cnt = 0;
73290Sstevel@tonic-gate 	char *dir;
73300Sstevel@tonic-gate 
73310Sstevel@tonic-gate 	dir = e_zalloc(E_EXIT, strlen(name) + 1);
73320Sstevel@tonic-gate 
73330Sstevel@tonic-gate 	/*
73340Sstevel@tonic-gate 	 * open directory; creating missing directories along the way.
73350Sstevel@tonic-gate 	 */
73360Sstevel@tonic-gate 	get_parent(name, dir);
73370Sstevel@tonic-gate 	do {
73380Sstevel@tonic-gate 		fd = open(dir, O_RDONLY);
73390Sstevel@tonic-gate 		if (fd != -1) {
73400Sstevel@tonic-gate 			free(dir);
73410Sstevel@tonic-gate 			return (fd);
73420Sstevel@tonic-gate 		}
73430Sstevel@tonic-gate 		cnt++;
73440Sstevel@tonic-gate 	} while (cnt <= 1 && missdir(name) == 0);
73450Sstevel@tonic-gate 
73460Sstevel@tonic-gate 	free(dir);
73470Sstevel@tonic-gate 	return (-1);
73480Sstevel@tonic-gate }
73490Sstevel@tonic-gate 
73500Sstevel@tonic-gate static int
73510Sstevel@tonic-gate open_dirfd()
73520Sstevel@tonic-gate {
73530Sstevel@tonic-gate #ifdef O_XATTR
73540Sstevel@tonic-gate 	if ((Args & OCt) == 0) {
73550Sstevel@tonic-gate 		close_dirfd();
73560Sstevel@tonic-gate 		if (G_p->g_attrnam_p != (char *)NULL) {
73570Sstevel@tonic-gate 			G_p->g_dirfd = attropen(G_p->g_attrfnam_p,
73580Sstevel@tonic-gate 			    ".", O_RDONLY);
73590Sstevel@tonic-gate 			if (G_p->g_dirfd == -1 && (Args & (OCi | OCp))) {
73600Sstevel@tonic-gate 				G_p->g_dirfd =
73610Sstevel@tonic-gate 				    retry_attrdir_open(G_p->g_attrfnam_p);
73620Sstevel@tonic-gate 				if (G_p->g_dirfd == -1) {
73630Sstevel@tonic-gate 					msg(ERRN,
73640Sstevel@tonic-gate 					    "Cannot open attribute"
73650Sstevel@tonic-gate 					    " directory of file %s",
73660Sstevel@tonic-gate 					    G_p->g_attrfnam_p);
73670Sstevel@tonic-gate 					return (1);
73680Sstevel@tonic-gate 				}
73690Sstevel@tonic-gate 			}
73700Sstevel@tonic-gate 		} else {
73710Sstevel@tonic-gate 			G_p->g_dirfd = open_dir(G_p->g_nam_p);
73720Sstevel@tonic-gate 			if (G_p->g_dirfd == -1) {
73730Sstevel@tonic-gate 				msg(ERRN,
73740Sstevel@tonic-gate 				    "Cannot open/create %s", G_p->g_nam_p);
73750Sstevel@tonic-gate 				return (1);
73760Sstevel@tonic-gate 			}
73770Sstevel@tonic-gate 		}
73780Sstevel@tonic-gate 	} else {
73790Sstevel@tonic-gate 		G_p->g_dirfd = -1;
73800Sstevel@tonic-gate 	}
73810Sstevel@tonic-gate #else
73820Sstevel@tonic-gate 	G_p->g_dirfd = -1;
73830Sstevel@tonic-gate #endif
73840Sstevel@tonic-gate 	return (0);
73850Sstevel@tonic-gate }
73860Sstevel@tonic-gate 
73870Sstevel@tonic-gate static void
73880Sstevel@tonic-gate close_dirfd()
73890Sstevel@tonic-gate {
73900Sstevel@tonic-gate 	if (G_p->g_dirfd != -1) {
73910Sstevel@tonic-gate 		(void) close(G_p->g_dirfd);
73920Sstevel@tonic-gate 		G_p->g_dirfd = -1;
73930Sstevel@tonic-gate 	}
73940Sstevel@tonic-gate }
73950Sstevel@tonic-gate 
73960Sstevel@tonic-gate static void
73970Sstevel@tonic-gate write_xattr_hdr()
73980Sstevel@tonic-gate {
73990Sstevel@tonic-gate 	char *attrbuf = NULL;
74000Sstevel@tonic-gate 	int  attrlen = 0;
74010Sstevel@tonic-gate 	char *namep;
74020Sstevel@tonic-gate 	struct Lnk *tl_p, *linkinfo;
74030Sstevel@tonic-gate 
74040Sstevel@tonic-gate 
74050Sstevel@tonic-gate 	/*
74060Sstevel@tonic-gate 	 * namep was allocated in xattrs_out.  It is big enough to hold
74070Sstevel@tonic-gate 	 * either the name + .hdr on the end or just the attr name
74080Sstevel@tonic-gate 	 */
74090Sstevel@tonic-gate 
74100Sstevel@tonic-gate #if defined(O_XATTR)
74110Sstevel@tonic-gate 	namep = Gen.g_nam_p;
74120Sstevel@tonic-gate 	(void) creat_hdr();
74130Sstevel@tonic-gate 
74140Sstevel@tonic-gate 
74150Sstevel@tonic-gate 	if (Args & OCo) {
74160Sstevel@tonic-gate 		linkinfo = NULL;
74170Sstevel@tonic-gate 		tl_p = Lnk_hd.L_nxt_p;
74180Sstevel@tonic-gate 		while (tl_p != &Lnk_hd) {
74190Sstevel@tonic-gate 			if (tl_p->L_gen.g_ino == G_p->g_ino &&
74201134Sceastha 			    tl_p->L_gen.g_dev == G_p->g_dev) {
74210Sstevel@tonic-gate 					linkinfo = tl_p;
74220Sstevel@tonic-gate 					break; /* found */
74230Sstevel@tonic-gate 			}
74240Sstevel@tonic-gate 			tl_p = tl_p->L_nxt_p;
74250Sstevel@tonic-gate 		}
74260Sstevel@tonic-gate 		prepare_xattr_hdr(&attrbuf, Gen.g_attrfnam_p,
74270Sstevel@tonic-gate 		    Gen.g_attrnam_p,
74280Sstevel@tonic-gate 		    (linkinfo == (struct Lnk *)NULL) ?
74290Sstevel@tonic-gate 		    tartype(Gen.g_mode & Ftype) : LNKTYPE,
74300Sstevel@tonic-gate 		    linkinfo, &attrlen);
74310Sstevel@tonic-gate 		Gen.g_filesz = attrlen;
74320Sstevel@tonic-gate 		write_hdr(ARCHIVE_XATTR, (off_t)attrlen);
74330Sstevel@tonic-gate 		(void) sprintf(namep, "%s/%s", DEVNULL, Gen.g_attrnam_p);
74340Sstevel@tonic-gate 		(void) write_ancillary(attrbuf, attrlen);
74350Sstevel@tonic-gate 	}
74360Sstevel@tonic-gate 
74370Sstevel@tonic-gate 	(void) creat_hdr();
74380Sstevel@tonic-gate #endif
74390Sstevel@tonic-gate }
74400Sstevel@tonic-gate 
74410Sstevel@tonic-gate static int
74420Sstevel@tonic-gate retry_attrdir_open(char *name)
74430Sstevel@tonic-gate {
74440Sstevel@tonic-gate 	int dirfd = -1;
74450Sstevel@tonic-gate 	struct timeval times[2];
74460Sstevel@tonic-gate 	mode_t newmode;
74470Sstevel@tonic-gate 	struct stat parentstat;
74481231Smarks 	acl_t *aclp = NULL;
74491231Smarks 	int error;
74500Sstevel@tonic-gate 
74510Sstevel@tonic-gate 	/*
74520Sstevel@tonic-gate 	 * We couldn't get to attrdir. See if its
74530Sstevel@tonic-gate 	 * just a mode problem on the parent file.
74540Sstevel@tonic-gate 	 * for example: a mode such as r-xr--r--
74550Sstevel@tonic-gate 	 * won't let us create an attribute dir
74560Sstevel@tonic-gate 	 * if it doesn't already exist.
74571231Smarks 	 *
74581231Smarks 	 * If file has a non-trivial ACL, then save it
74591231Smarks 	 * off so that we can place it back on after doing
74601231Smarks 	 * chmod's.
74610Sstevel@tonic-gate 	 */
74620Sstevel@tonic-gate 
74630Sstevel@tonic-gate 	if (stat(name, &parentstat) == -1) {
74640Sstevel@tonic-gate 		msg(ERRN, "Cannot stat file %s", name);
74650Sstevel@tonic-gate 		return (-1);
74660Sstevel@tonic-gate 	}
74671231Smarks 
74681231Smarks 	if ((error = acl_get(name, ACL_NO_TRIVIAL, &aclp)) != 0) {
74691231Smarks 		msg(ERRN,
74701231Smarks 		    "Failed to retrieve ACL on %s %s", name, strerror(errno));
74711231Smarks 		return (-1);
74721231Smarks 	}
74731231Smarks 
74740Sstevel@tonic-gate 	newmode = S_IWUSR | parentstat.st_mode;
74750Sstevel@tonic-gate 	if (chmod(name, newmode) == -1) {
74760Sstevel@tonic-gate 		msg(ERRN, "Cannot change mode of file %s to %o", name, newmode);
74771231Smarks 		if (aclp)
74781231Smarks 			acl_free(aclp);
74790Sstevel@tonic-gate 		return (-1);
74800Sstevel@tonic-gate 	}
74810Sstevel@tonic-gate 
74820Sstevel@tonic-gate 	dirfd = attropen(name, ".", O_RDONLY);
74831231Smarks 
74841231Smarks 	/*
74851231Smarks 	 * Don't print error here, caller will handle printing out
74861231Smarks 	 * can't open message.
74871231Smarks 	 */
74881231Smarks 
74891231Smarks 	/*
74901231Smarks 	 * Put mode back to original
74911231Smarks 	 */
74921231Smarks 	if (chmod(name, parentstat.st_mode) != 0) {
74931231Smarks 		msg(ERRN, "Cannot restore permissions of file %s to %o",
74941231Smarks 		    name, parentstat.st_mode);
74951231Smarks 	}
74961231Smarks 
74971231Smarks 	if (aclp) {
74981231Smarks 		error = acl_set(name, aclp);
74991231Smarks 		if (error) {
75001231Smarks 			msg(ERRN, "failed to set ACL on %s", name);
75011231Smarks 		}
75021231Smarks 		acl_free(aclp);
75031231Smarks 	}
75041231Smarks 	/*
75051231Smarks 	 * Put back time stamps
75061231Smarks 	 */
75071231Smarks 
75081231Smarks 	times[0].tv_sec = parentstat.st_atime;
75091231Smarks 	times[0].tv_usec = 0;
75101231Smarks 	times[1].tv_sec = parentstat.st_mtime;
75111231Smarks 	times[1].tv_usec = 0;
75121231Smarks 	if (utimes(name, times) != 0) {
75131231Smarks 		msg(ERRN, "Cannot reset timestamps on file %s");
75140Sstevel@tonic-gate 	}
75150Sstevel@tonic-gate 
75160Sstevel@tonic-gate 	return (dirfd);
75170Sstevel@tonic-gate }
75180Sstevel@tonic-gate 
75190Sstevel@tonic-gate /*
75200Sstevel@tonic-gate  * skip over extra slashes in string.
75210Sstevel@tonic-gate  *
75220Sstevel@tonic-gate  * For example:
75230Sstevel@tonic-gate  * /usr/tmp/////
75240Sstevel@tonic-gate  *
75250Sstevel@tonic-gate  * would return pointer at
75260Sstevel@tonic-gate  * /usr/tmp/////
75270Sstevel@tonic-gate  *         ^
75280Sstevel@tonic-gate  */
75290Sstevel@tonic-gate static char *
75300Sstevel@tonic-gate skipslashes(char *string, char *start)
75310Sstevel@tonic-gate {
75320Sstevel@tonic-gate 	while ((string > start) && *(string - 1) == '/') {
75330Sstevel@tonic-gate 		string--;
75340Sstevel@tonic-gate 	}
75350Sstevel@tonic-gate 
75360Sstevel@tonic-gate 	return (string);
75370Sstevel@tonic-gate }
75380Sstevel@tonic-gate 
75390Sstevel@tonic-gate static sl_info_t *
75400Sstevel@tonic-gate sl_info_alloc(void)
75410Sstevel@tonic-gate {
75420Sstevel@tonic-gate 	static int num_left;
75430Sstevel@tonic-gate 	static sl_info_t *slipool;
75440Sstevel@tonic-gate 
75450Sstevel@tonic-gate 	if (num_left > 0) {
75460Sstevel@tonic-gate 		return (&slipool[--num_left]);
75470Sstevel@tonic-gate 	}
75480Sstevel@tonic-gate 	num_left = SL_INFO_ALLOC_CHUNK;
75490Sstevel@tonic-gate 	slipool = e_zalloc(E_EXIT, sizeof (sl_info_t) * num_left);
75500Sstevel@tonic-gate 	return (&slipool[--num_left]);
75510Sstevel@tonic-gate }
75520Sstevel@tonic-gate 
75530Sstevel@tonic-gate /*
75540Sstevel@tonic-gate  * If a match for the key values was found in the tree, return a pointer to it.
75550Sstevel@tonic-gate  * If a match was not found, insert it and return a pointer to it.  This is
75560Sstevel@tonic-gate  * based on Knuth's Algorithm A in Vol 3, section 6.2.3.
75570Sstevel@tonic-gate  */
75580Sstevel@tonic-gate 
75590Sstevel@tonic-gate sl_info_t *
75600Sstevel@tonic-gate sl_insert(dev_t device, ino_t inode)
75610Sstevel@tonic-gate {
75620Sstevel@tonic-gate 	sl_info_t *p;		/* moves down the tree */
75630Sstevel@tonic-gate 	sl_info_t *q;		/* scratch */
75640Sstevel@tonic-gate 	sl_info_t *r;		/* scratch */
75650Sstevel@tonic-gate 	sl_info_t *s;		/* pt where rebalancing may be needed */
75660Sstevel@tonic-gate 	sl_info_t *t;		/* father of s */
75670Sstevel@tonic-gate 	sl_info_t *head;
75680Sstevel@tonic-gate 
75690Sstevel@tonic-gate 	int a;			/* used to hold balance factors */
75700Sstevel@tonic-gate 	int done;		/* loop control */
75710Sstevel@tonic-gate 	int cmpflg;		/* used to hold the result of a comparison */
75720Sstevel@tonic-gate 
75730Sstevel@tonic-gate 	/* initialize */
75740Sstevel@tonic-gate 
75750Sstevel@tonic-gate 	head = sl_devhash_lookup(device);
75760Sstevel@tonic-gate 
75770Sstevel@tonic-gate 	if (head == NULL) {
75780Sstevel@tonic-gate 		head = sl_info_alloc();
75790Sstevel@tonic-gate 		head->llink = NULL;
75800Sstevel@tonic-gate 		head->bal = 0;
75810Sstevel@tonic-gate 
75820Sstevel@tonic-gate 		p = head->rlink = sl_info_alloc();
75830Sstevel@tonic-gate 		p->sl_ino = inode;
75840Sstevel@tonic-gate 		p->sl_count = 0;
75850Sstevel@tonic-gate 		p->bal = 0;
75860Sstevel@tonic-gate 		p->llink = NULL;
75870Sstevel@tonic-gate 		p->rlink = NULL;
75880Sstevel@tonic-gate 		sl_devhash_insert(device, head);
75890Sstevel@tonic-gate 		return (p);
75900Sstevel@tonic-gate 	}
75910Sstevel@tonic-gate 
75920Sstevel@tonic-gate 	t = head;
75930Sstevel@tonic-gate 	s = p = head->rlink;
75940Sstevel@tonic-gate 
75950Sstevel@tonic-gate 	/* compare */
75960Sstevel@tonic-gate 
75970Sstevel@tonic-gate 	for (done = 0; ! done; ) {
75980Sstevel@tonic-gate 		switch (sl_compare(inode, p->sl_ino)) {
75990Sstevel@tonic-gate 			case -1:
76000Sstevel@tonic-gate 				/* move left */
76010Sstevel@tonic-gate 
76020Sstevel@tonic-gate 				q = p->llink;
76030Sstevel@tonic-gate 
76040Sstevel@tonic-gate 				if (q == NULL) {
76050Sstevel@tonic-gate 					q = sl_info_alloc();
76060Sstevel@tonic-gate 					p->llink = q;
76070Sstevel@tonic-gate 					done = 1;
76080Sstevel@tonic-gate 					continue;
76090Sstevel@tonic-gate 				}
76100Sstevel@tonic-gate 
76110Sstevel@tonic-gate 				break;
76120Sstevel@tonic-gate 
76130Sstevel@tonic-gate 			case 0:
76140Sstevel@tonic-gate 				/* found it */
76150Sstevel@tonic-gate 				return (p);
76160Sstevel@tonic-gate 				break;
76170Sstevel@tonic-gate 
76180Sstevel@tonic-gate 			case 1:
76190Sstevel@tonic-gate 				/* move right */
76200Sstevel@tonic-gate 
76210Sstevel@tonic-gate 				q = p->rlink;
76220Sstevel@tonic-gate 
76230Sstevel@tonic-gate 				if (q == NULL) {
76240Sstevel@tonic-gate 					q = sl_info_alloc();
76250Sstevel@tonic-gate 					p->rlink = q;
76260Sstevel@tonic-gate 					done = 1;
76270Sstevel@tonic-gate 					continue;
76280Sstevel@tonic-gate 				}
76290Sstevel@tonic-gate 
76300Sstevel@tonic-gate 				break;
76310Sstevel@tonic-gate 		}
76320Sstevel@tonic-gate 
76330Sstevel@tonic-gate 		if (q->bal != 0) {
76340Sstevel@tonic-gate 			t = p;
76350Sstevel@tonic-gate 			s = q;
76360Sstevel@tonic-gate 		}
76370Sstevel@tonic-gate 
76380Sstevel@tonic-gate 		p = q;
76390Sstevel@tonic-gate 	}
76400Sstevel@tonic-gate 
76410Sstevel@tonic-gate 	/* insert */
76420Sstevel@tonic-gate 
76430Sstevel@tonic-gate 	q->sl_ino = inode;
76440Sstevel@tonic-gate 	q->sl_count = 0;
76450Sstevel@tonic-gate 	q->llink = q->rlink = NULL;
76460Sstevel@tonic-gate 	q->bal = 0;
76470Sstevel@tonic-gate 
76480Sstevel@tonic-gate 	/* adjust balance factors */
76490Sstevel@tonic-gate 
76500Sstevel@tonic-gate 	if ((cmpflg = sl_compare(inode, s->sl_ino)) < 0) {
76510Sstevel@tonic-gate 		r = p = s->llink;
76520Sstevel@tonic-gate 	} else {
76530Sstevel@tonic-gate 		r = p = s->rlink;
76540Sstevel@tonic-gate 	}
76550Sstevel@tonic-gate 
76560Sstevel@tonic-gate 	while (p != q) {
76570Sstevel@tonic-gate 		switch (sl_compare(inode, p->sl_ino)) {
76580Sstevel@tonic-gate 			case -1:
76590Sstevel@tonic-gate 				p->bal = -1;
76600Sstevel@tonic-gate 				p = p->llink;
76610Sstevel@tonic-gate 				break;
76620Sstevel@tonic-gate 
76630Sstevel@tonic-gate 			case 0:
76640Sstevel@tonic-gate 				break;
76650Sstevel@tonic-gate 
76660Sstevel@tonic-gate 			case 1:
76670Sstevel@tonic-gate 				p->bal = 1;
76680Sstevel@tonic-gate 				p = p->rlink;
76690Sstevel@tonic-gate 				break;
76700Sstevel@tonic-gate 		}
76710Sstevel@tonic-gate 	}
76720Sstevel@tonic-gate 
76730Sstevel@tonic-gate 	/* balancing act */
76740Sstevel@tonic-gate 
76750Sstevel@tonic-gate 	if (cmpflg < 0) {
76760Sstevel@tonic-gate 		a = -1;
76770Sstevel@tonic-gate 	} else {
76780Sstevel@tonic-gate 		a = 1;
76790Sstevel@tonic-gate 	}
76800Sstevel@tonic-gate 
76810Sstevel@tonic-gate 	if (s->bal == 0) {
76820Sstevel@tonic-gate 		s->bal = a;
76830Sstevel@tonic-gate 		head->llink = (sl_info_t *)((int)head->llink + 1);
76840Sstevel@tonic-gate 		return (q);
76850Sstevel@tonic-gate 	} else if (s->bal == -a) {
76860Sstevel@tonic-gate 		s->bal = 0;
76870Sstevel@tonic-gate 		return (q);
76880Sstevel@tonic-gate 	}
76890Sstevel@tonic-gate 
76900Sstevel@tonic-gate 	/*
76910Sstevel@tonic-gate 	 * (s->bal == a)
76920Sstevel@tonic-gate 	 */
76930Sstevel@tonic-gate 
76940Sstevel@tonic-gate 	if (r->bal == a) {
76950Sstevel@tonic-gate 		/* single rotation */
76960Sstevel@tonic-gate 
76970Sstevel@tonic-gate 		p = r;
76980Sstevel@tonic-gate 
76990Sstevel@tonic-gate 		if (a == -1) {
77000Sstevel@tonic-gate 			s->llink = r->rlink;
77010Sstevel@tonic-gate 			r->rlink = s;
77020Sstevel@tonic-gate 		} else if (a == 1) {
77030Sstevel@tonic-gate 			s->rlink = r->llink;
77040Sstevel@tonic-gate 			r->llink = s;
77050Sstevel@tonic-gate 		}
77060Sstevel@tonic-gate 
77070Sstevel@tonic-gate 		s->bal = r->bal = 0;
77080Sstevel@tonic-gate 
77090Sstevel@tonic-gate 	} else if (r->bal == -a) {
77100Sstevel@tonic-gate 		/* double rotation */
77110Sstevel@tonic-gate 
77120Sstevel@tonic-gate 		if (a == -1) {
77130Sstevel@tonic-gate 			p = r->rlink;
77140Sstevel@tonic-gate 			r->rlink = p->llink;
77150Sstevel@tonic-gate 			p->llink = r;
77160Sstevel@tonic-gate 			s->llink = p->rlink;
77170Sstevel@tonic-gate 			p->rlink = s;
77180Sstevel@tonic-gate 		} else if (a == 1) {
77190Sstevel@tonic-gate 			p = r->llink;
77200Sstevel@tonic-gate 			r->llink = p->rlink;
77210Sstevel@tonic-gate 			p->rlink = r;
77220Sstevel@tonic-gate 			s->rlink = p->llink;
77230Sstevel@tonic-gate 			p->llink = s;
77240Sstevel@tonic-gate 		}
77250Sstevel@tonic-gate 
77260Sstevel@tonic-gate 		if (p->bal == 0) {
77270Sstevel@tonic-gate 			s->bal = 0;
77280Sstevel@tonic-gate 			r->bal = 0;
77290Sstevel@tonic-gate 		} else if (p->bal == -a) {
77300Sstevel@tonic-gate 			s->bal = 0;
77310Sstevel@tonic-gate 			r->bal = a;
77320Sstevel@tonic-gate 		} else if (p->bal == a) {
77330Sstevel@tonic-gate 			s->bal = -a;
77340Sstevel@tonic-gate 			r->bal = 0;
77350Sstevel@tonic-gate 		}
77360Sstevel@tonic-gate 
77370Sstevel@tonic-gate 		p->bal = 0;
77380Sstevel@tonic-gate 	}
77390Sstevel@tonic-gate 
77400Sstevel@tonic-gate 	/* finishing touch */
77410Sstevel@tonic-gate 
77420Sstevel@tonic-gate 	if (s == t->rlink) {
77430Sstevel@tonic-gate 		t->rlink = p;
77440Sstevel@tonic-gate 	} else {
77450Sstevel@tonic-gate 		t->llink = p;
77460Sstevel@tonic-gate 	}
77470Sstevel@tonic-gate 
77480Sstevel@tonic-gate 	return (q);
77490Sstevel@tonic-gate }
77500Sstevel@tonic-gate 
77510Sstevel@tonic-gate /*
77520Sstevel@tonic-gate  * sl_numlinks: return the number of links that we saw during our preview.
77530Sstevel@tonic-gate  */
77540Sstevel@tonic-gate 
77550Sstevel@tonic-gate static ulong_t
77560Sstevel@tonic-gate sl_numlinks(dev_t device, ino_t inode)
77570Sstevel@tonic-gate {
77580Sstevel@tonic-gate 	sl_info_t *p = sl_search(device, inode);
77590Sstevel@tonic-gate 
77600Sstevel@tonic-gate 	if (p) {
77611134Sceastha 		return (p->sl_count);
77620Sstevel@tonic-gate 	} else {
77631134Sceastha 		return (1);
77640Sstevel@tonic-gate 	}
77650Sstevel@tonic-gate }
77660Sstevel@tonic-gate 
77670Sstevel@tonic-gate /*
77680Sstevel@tonic-gate  * sl_preview_synonyms:  Read the file list from the input stream, remembering
77690Sstevel@tonic-gate  * each reference to each file.
77700Sstevel@tonic-gate  */
77710Sstevel@tonic-gate 
77720Sstevel@tonic-gate static void
77730Sstevel@tonic-gate sl_preview_synonyms(void)
77740Sstevel@tonic-gate {
77750Sstevel@tonic-gate 	char buf [APATH+1];
77760Sstevel@tonic-gate 	char *s;
77770Sstevel@tonic-gate 
77780Sstevel@tonic-gate 	char *suffix = "/cpioXXXXXX";
77790Sstevel@tonic-gate 	char *tmpdir = getenv("TMPDIR");
77800Sstevel@tonic-gate 	int    tmpfd, islnk;
77810Sstevel@tonic-gate 	FILE *tmpfile;
77820Sstevel@tonic-gate 	char *tmpfname;
77830Sstevel@tonic-gate 
77840Sstevel@tonic-gate 	if (tmpdir == NULL || *tmpdir == '\0' ||
77850Sstevel@tonic-gate 	    (strlen(tmpdir) + strlen(suffix)) > APATH) {
77860Sstevel@tonic-gate 		struct statvfs tdsb;
77870Sstevel@tonic-gate 
77880Sstevel@tonic-gate 		tmpdir = "/var/tmp";
77890Sstevel@tonic-gate 
77900Sstevel@tonic-gate 		/* /var/tmp is read-only in the mini-root environment */
77910Sstevel@tonic-gate 
77920Sstevel@tonic-gate 		if (statvfs(tmpdir, &tdsb) == -1 || tdsb.f_flag & ST_RDONLY) {
77930Sstevel@tonic-gate 			tmpdir = "/tmp";
77940Sstevel@tonic-gate 		}
77950Sstevel@tonic-gate 	}
77960Sstevel@tonic-gate 
77970Sstevel@tonic-gate 	tmpfname = e_zalloc(E_EXIT, strlen(tmpdir) + strlen(suffix) + 1);
77980Sstevel@tonic-gate 
77990Sstevel@tonic-gate 	(void) strcpy(tmpfname, tmpdir);
78000Sstevel@tonic-gate 	(void) strcat(tmpfname, suffix);
78010Sstevel@tonic-gate 
78020Sstevel@tonic-gate 	if ((tmpfd = mkstemp(tmpfname)) == -1) {
78030Sstevel@tonic-gate 		msg(EXTN, "cannot open tmpfile %s", tmpfname);
78040Sstevel@tonic-gate 	}
78050Sstevel@tonic-gate 
78060Sstevel@tonic-gate 	if (unlink(tmpfname) == -1) {
78070Sstevel@tonic-gate 		msg(EXTN, "cannot unlink tmpfile %s", tmpfname);
78080Sstevel@tonic-gate 	}
78090Sstevel@tonic-gate 
78100Sstevel@tonic-gate 	if ((tmpfile = fdopen(tmpfd, "w+")) == NULL) {
78110Sstevel@tonic-gate 		msg(EXTN, "cannot fdopen tmpfile %s", tmpfname);
78120Sstevel@tonic-gate 	}
78130Sstevel@tonic-gate 
78140Sstevel@tonic-gate 	while ((s = fgets(buf, APATH+1, In_p)) != NULL) {
78150Sstevel@tonic-gate 		size_t lastchar;
78160Sstevel@tonic-gate 		struct stat sb;
78170Sstevel@tonic-gate 
78180Sstevel@tonic-gate 		if (fputs(buf, tmpfile) == EOF) {
78190Sstevel@tonic-gate 			msg(EXTN, "problem writing to tmpfile %s", tmpfname);
78200Sstevel@tonic-gate 		}
78210Sstevel@tonic-gate 
78220Sstevel@tonic-gate 		/* pre-process the name */
78230Sstevel@tonic-gate 
78240Sstevel@tonic-gate 		lastchar = strlen(s) - 1;
78250Sstevel@tonic-gate 
78260Sstevel@tonic-gate 		if (s[lastchar] != '\n' && lastchar == APATH - 1) {
78270Sstevel@tonic-gate 			continue;
78280Sstevel@tonic-gate 		} else {
78290Sstevel@tonic-gate 			s[lastchar] = '\0';
78300Sstevel@tonic-gate 		}
78310Sstevel@tonic-gate 
78320Sstevel@tonic-gate 		while (s[0] == '.' && s[1] == '/') {
78330Sstevel@tonic-gate 			s += 2;
78340Sstevel@tonic-gate 			while (s[0] == '/') {
78350Sstevel@tonic-gate 				s++;
78360Sstevel@tonic-gate 			}
78370Sstevel@tonic-gate 		}
78380Sstevel@tonic-gate 
78390Sstevel@tonic-gate 		if (lstat(s, &sb) < 0) {
78400Sstevel@tonic-gate 			continue;
78410Sstevel@tonic-gate 		}
78420Sstevel@tonic-gate 		islnk = 0;
78430Sstevel@tonic-gate 		if (S_ISLNK(sb.st_mode)) {
78440Sstevel@tonic-gate 			islnk = 1;
78450Sstevel@tonic-gate 			if (Args & OCL) {
78460Sstevel@tonic-gate 				if (stat(s, &sb) < 0) {
78470Sstevel@tonic-gate 					continue;
78480Sstevel@tonic-gate 				}
78490Sstevel@tonic-gate 			}
78500Sstevel@tonic-gate 		}
78510Sstevel@tonic-gate 		sl_remember_tgt(&sb, islnk);
78520Sstevel@tonic-gate 
78530Sstevel@tonic-gate #if defined(O_XATTR)
78540Sstevel@tonic-gate 		if (Atflag) {
78550Sstevel@tonic-gate 			int  dirfd;
78560Sstevel@tonic-gate 			DIR  *dirp;
78570Sstevel@tonic-gate 			struct dirent *dp;
78580Sstevel@tonic-gate 
78590Sstevel@tonic-gate 			if (pathconf(s, _PC_XATTR_EXISTS) != 1)
78600Sstevel@tonic-gate 				continue;
78610Sstevel@tonic-gate 
78620Sstevel@tonic-gate 			dirfd = attropen(s, ".", O_RDONLY);
78630Sstevel@tonic-gate 			if (dirfd == -1)
78640Sstevel@tonic-gate 				continue;
78650Sstevel@tonic-gate 
78660Sstevel@tonic-gate 			tmpfd = dup(dirfd);
78670Sstevel@tonic-gate 			if (tmpfd == -1) {
78680Sstevel@tonic-gate 				(void) close(dirfd);
78690Sstevel@tonic-gate 				continue;
78700Sstevel@tonic-gate 			}
78710Sstevel@tonic-gate 			dirp = fdopendir(tmpfd);
78720Sstevel@tonic-gate 			if (dirp == NULL) {
78730Sstevel@tonic-gate 				(void) close(dirfd);
78740Sstevel@tonic-gate 				(void) close(tmpfd);
78750Sstevel@tonic-gate 				continue;
78760Sstevel@tonic-gate 			}
78770Sstevel@tonic-gate 
78780Sstevel@tonic-gate 			while (dp = readdir(dirp)) {
78790Sstevel@tonic-gate 				if ((dp->d_name[0] == '.' &&
78800Sstevel@tonic-gate 				    dp->d_name[1] == '\0') ||
78810Sstevel@tonic-gate 				    (dp->d_name[0] == '.' &&
78820Sstevel@tonic-gate 				    dp->d_name[1] == '.' &&
78830Sstevel@tonic-gate 				    dp->d_name[2] == '\0'))
78840Sstevel@tonic-gate 					continue;
78850Sstevel@tonic-gate 
78860Sstevel@tonic-gate 				if (fstatat(dirfd, dp->d_name, &sb,
78870Sstevel@tonic-gate 				    AT_SYMLINK_NOFOLLOW) < 0) {
78880Sstevel@tonic-gate 					continue;
78890Sstevel@tonic-gate 				}
78900Sstevel@tonic-gate 				islnk = 0;
78910Sstevel@tonic-gate 				if (S_ISLNK(sb.st_mode)) {
78920Sstevel@tonic-gate 					islnk = 1;
78930Sstevel@tonic-gate 					if (Args & OCL) {
78940Sstevel@tonic-gate 						if (fstatat(dirfd, dp->d_name,
78950Sstevel@tonic-gate 						    &sb, 0) < 0) {
78960Sstevel@tonic-gate 							continue;
78970Sstevel@tonic-gate 						}
78980Sstevel@tonic-gate 					}
78990Sstevel@tonic-gate 				}
79000Sstevel@tonic-gate 				sl_remember_tgt(&sb, islnk);
79010Sstevel@tonic-gate 			}
79020Sstevel@tonic-gate 			(void) closedir(dirp);
79030Sstevel@tonic-gate 			(void) close(dirfd);
79040Sstevel@tonic-gate 		}
79050Sstevel@tonic-gate #endif
79060Sstevel@tonic-gate 	}
79070Sstevel@tonic-gate 
79080Sstevel@tonic-gate 	if (ferror(In_p)) {
79090Sstevel@tonic-gate 		msg(EXTN, "error reading stdin");
79100Sstevel@tonic-gate 	}
79110Sstevel@tonic-gate 
79120Sstevel@tonic-gate 	if (fseek(tmpfile, 0L, SEEK_SET) == -1) {
79130Sstevel@tonic-gate 		msg(EXTN, "cannot fseek on tmpfile %s", tmpfname);
79140Sstevel@tonic-gate 	}
79150Sstevel@tonic-gate 
79160Sstevel@tonic-gate 	In_p = tmpfile;
79170Sstevel@tonic-gate 	free(tmpfname);
79180Sstevel@tonic-gate }
79190Sstevel@tonic-gate 
79200Sstevel@tonic-gate /*
79210Sstevel@tonic-gate  * sl_remember_tgt: Add the device/inode for lstat or stat info to the list of
79220Sstevel@tonic-gate  * those we've seen before.
79230Sstevel@tonic-gate  *
79240Sstevel@tonic-gate  * This tree (rooted under head) is keyed by the device/inode of the file
79250Sstevel@tonic-gate  * being pointed to.  A count is kept of the number of references encountered
79260Sstevel@tonic-gate  * so far.
79270Sstevel@tonic-gate  */
79280Sstevel@tonic-gate 
79290Sstevel@tonic-gate static void
79300Sstevel@tonic-gate sl_remember_tgt(const struct stat *sbp, int isSymlink)
79310Sstevel@tonic-gate {
79320Sstevel@tonic-gate 	sl_info_t *p;
79330Sstevel@tonic-gate 	dev_t device;
79340Sstevel@tonic-gate 	ino_t inode;
79350Sstevel@tonic-gate 
79360Sstevel@tonic-gate 	device = sbp->st_dev;
79370Sstevel@tonic-gate 	inode  = sbp->st_ino;
79380Sstevel@tonic-gate 
79390Sstevel@tonic-gate 	/* Determine whether we've seen this one before */
79400Sstevel@tonic-gate 
79410Sstevel@tonic-gate 	p = sl_insert(device, inode);
79420Sstevel@tonic-gate 
79430Sstevel@tonic-gate 	if (p->sl_count > 0) {
79440Sstevel@tonic-gate 		/*
79450Sstevel@tonic-gate 		 * We have seen this file before.
79460Sstevel@tonic-gate 		 * Note that if we are not chasing symlinks, and this one is a
79470Sstevel@tonic-gate 		 * symlink, it is identically the one we saw before (you cannot
79480Sstevel@tonic-gate 		 * have hard links to symlinks); in this case, we leave the
79490Sstevel@tonic-gate 		 * count alone, so that we don't wind up archiving a symlink to
79500Sstevel@tonic-gate 		 * itself.
79510Sstevel@tonic-gate 		 */
79520Sstevel@tonic-gate 
79530Sstevel@tonic-gate 		if ((Args & OCL) || (! isSymlink)) {
79540Sstevel@tonic-gate 			p->sl_count++;
79550Sstevel@tonic-gate 		}
79560Sstevel@tonic-gate 	} else {
79570Sstevel@tonic-gate 		/* We have not seen this file before */
79580Sstevel@tonic-gate 
79590Sstevel@tonic-gate 		p->sl_count = 1;
79600Sstevel@tonic-gate 
79610Sstevel@tonic-gate 		if (Use_old_stat) {
79620Sstevel@tonic-gate 			/* -Hodc: remap inode (-1 on overflow) */
79630Sstevel@tonic-gate 
79640Sstevel@tonic-gate 			sl_remap_t  *q;
79650Sstevel@tonic-gate 
79660Sstevel@tonic-gate 			for (q = sl_remap_head; q && (q->dev != device);
79670Sstevel@tonic-gate 			    q = q->next) {
79680Sstevel@tonic-gate 				/* do nothing */
79690Sstevel@tonic-gate 			}
79700Sstevel@tonic-gate 
79710Sstevel@tonic-gate 			if (q == NULL) {
79720Sstevel@tonic-gate 				q = e_zalloc(E_EXIT, sizeof (sl_remap_t));
79730Sstevel@tonic-gate 				q->dev = device;
79740Sstevel@tonic-gate 				p->sl_ino2 = q->inode_count = 1;
79750Sstevel@tonic-gate 
79760Sstevel@tonic-gate 				q->next = (sl_remap_head) ?
79770Sstevel@tonic-gate 				    sl_remap_head->next : NULL;
79780Sstevel@tonic-gate 				sl_remap_head = q;
79790Sstevel@tonic-gate 			} else {
79800Sstevel@tonic-gate 				if ((size_t)q->inode_count <=
79810Sstevel@tonic-gate 				    ((1 << (sizeof (o_ino_t) * 8)) - 1)) {
79820Sstevel@tonic-gate 					/* fits in o_ino_t */
79830Sstevel@tonic-gate 					p->sl_ino2 = ++(q->inode_count);
79840Sstevel@tonic-gate 				} else {
79850Sstevel@tonic-gate 					p->sl_ino2 = (ino_t)-1;
79860Sstevel@tonic-gate 				}
79870Sstevel@tonic-gate 			}
79880Sstevel@tonic-gate 		}
79890Sstevel@tonic-gate 	}
79900Sstevel@tonic-gate }
79910Sstevel@tonic-gate 
79920Sstevel@tonic-gate /*
79930Sstevel@tonic-gate  * A faster search, which does not insert the key values into the tree.
79940Sstevel@tonic-gate  * If the a match was found in the tree, return a pointer to it.  If it was not
79950Sstevel@tonic-gate  * found, return NULL.
79960Sstevel@tonic-gate  */
79970Sstevel@tonic-gate 
79980Sstevel@tonic-gate sl_info_t *
79990Sstevel@tonic-gate sl_search(dev_t device, ino_t inode)
80000Sstevel@tonic-gate {
80010Sstevel@tonic-gate 	sl_info_t *p;		/* moves down the tree */
80020Sstevel@tonic-gate 	int c;			/* comparison value */
80030Sstevel@tonic-gate 	sl_info_t *retval = NULL; /* return value */
80040Sstevel@tonic-gate 	sl_info_t *head;
80050Sstevel@tonic-gate 
80060Sstevel@tonic-gate 	head = sl_devhash_lookup(device);
80070Sstevel@tonic-gate 	if (head != NULL) {
80080Sstevel@tonic-gate 		for (p = head->rlink; p; ) {
80090Sstevel@tonic-gate 			if ((c = sl_compare(inode, p->sl_ino)) == 0) {
80100Sstevel@tonic-gate 				retval = p;
80110Sstevel@tonic-gate 				break;
80120Sstevel@tonic-gate 			} else if (c < 0) {
80130Sstevel@tonic-gate 				p = p->llink;
80140Sstevel@tonic-gate 			} else {
80150Sstevel@tonic-gate 				p = p->rlink;
80160Sstevel@tonic-gate 			}
80170Sstevel@tonic-gate 		}
80180Sstevel@tonic-gate 	}
80190Sstevel@tonic-gate 
80200Sstevel@tonic-gate 	return (retval);
80210Sstevel@tonic-gate }
80220Sstevel@tonic-gate 
80230Sstevel@tonic-gate static sl_info_t *
80240Sstevel@tonic-gate sl_devhash_lookup(dev_t device)
80250Sstevel@tonic-gate {
80260Sstevel@tonic-gate 	int key;
80270Sstevel@tonic-gate 	sl_info_link_t *lp;
80280Sstevel@tonic-gate 	static sl_info_link_t *devcache;
80290Sstevel@tonic-gate 
80300Sstevel@tonic-gate 	if (devcache != NULL && devcache->dev == device) {
80310Sstevel@tonic-gate 		return (devcache->head);
80320Sstevel@tonic-gate 	}
80330Sstevel@tonic-gate 
80340Sstevel@tonic-gate 	key = DEV_HASHKEY(device);
80350Sstevel@tonic-gate 	for (lp = sl_devhash[key]; lp; lp = lp->next) {
80360Sstevel@tonic-gate 		if (lp->dev == device) {
80370Sstevel@tonic-gate 			devcache = lp;
80380Sstevel@tonic-gate 			return (lp->head);
80390Sstevel@tonic-gate 		}
80400Sstevel@tonic-gate 	}
80410Sstevel@tonic-gate 	return (NULL);
80420Sstevel@tonic-gate }
80430Sstevel@tonic-gate 
80440Sstevel@tonic-gate static void
80450Sstevel@tonic-gate sl_devhash_insert(dev_t device, sl_info_t *head)
80460Sstevel@tonic-gate {
80470Sstevel@tonic-gate 	int key = DEV_HASHKEY(device);
80480Sstevel@tonic-gate 	sl_info_link_t *lp;
80490Sstevel@tonic-gate 
80500Sstevel@tonic-gate 	lp = e_zalloc(E_EXIT, sizeof (sl_info_link_t));
80510Sstevel@tonic-gate 	lp->dev = device;
80520Sstevel@tonic-gate 	lp->head = head;
80530Sstevel@tonic-gate 	lp->next = sl_devhash[key];
80540Sstevel@tonic-gate 	sl_devhash[key] = lp;
80550Sstevel@tonic-gate }
80560Sstevel@tonic-gate 
80570Sstevel@tonic-gate static void
80580Sstevel@tonic-gate chop_endslashes(char *path)
80590Sstevel@tonic-gate {
80600Sstevel@tonic-gate 	char *end, *ptr;
80610Sstevel@tonic-gate 
80620Sstevel@tonic-gate 	end = &path[strlen(path) -1];
80630Sstevel@tonic-gate 	if (*end == '/' && end != path) {
80640Sstevel@tonic-gate 		ptr = skipslashes(end, path);
80650Sstevel@tonic-gate 		if (ptr != NULL && ptr != path) {
80660Sstevel@tonic-gate 			*ptr = '\0';
80670Sstevel@tonic-gate 		}
80680Sstevel@tonic-gate 	}
80690Sstevel@tonic-gate }
80700Sstevel@tonic-gate 
80710Sstevel@tonic-gate #if !defined(O_XATTR)
80720Sstevel@tonic-gate int
80730Sstevel@tonic-gate openat64(int fd, char *name, int oflag, mode_t cmode)
80740Sstevel@tonic-gate {
80750Sstevel@tonic-gate 	return (open64(name, oflag, cmode));
80760Sstevel@tonic-gate }
80770Sstevel@tonic-gate 
80780Sstevel@tonic-gate int
80790Sstevel@tonic-gate openat(int fd, char *name, int oflag, mode_t cmode)
80800Sstevel@tonic-gate {
80810Sstevel@tonic-gate 	return (open(name, oflag, cmode));
80820Sstevel@tonic-gate }
80830Sstevel@tonic-gate 
80840Sstevel@tonic-gate int
80850Sstevel@tonic-gate fchownat(int fd, char *name, uid_t owner, gid_t group, int flag)
80860Sstevel@tonic-gate {
80870Sstevel@tonic-gate 	if (flag == AT_SYMLINK_NOFOLLOW)
80880Sstevel@tonic-gate 		return (lchown(name, owner, group));
80890Sstevel@tonic-gate 	else
80900Sstevel@tonic-gate 		return (chown(name, owner, group));
80910Sstevel@tonic-gate }
80920Sstevel@tonic-gate 
80930Sstevel@tonic-gate int
80940Sstevel@tonic-gate renameat(int fromfd, char *old, int tofd, char *new)
80950Sstevel@tonic-gate {
80960Sstevel@tonic-gate 	return (rename(old, new));
80970Sstevel@tonic-gate }
80980Sstevel@tonic-gate 
80990Sstevel@tonic-gate int
81000Sstevel@tonic-gate futimesat(int fd, char *path, struct timeval times[2])
81010Sstevel@tonic-gate {
81020Sstevel@tonic-gate 	return (utimes(path, times));
81030Sstevel@tonic-gate }
81040Sstevel@tonic-gate 
81050Sstevel@tonic-gate int
81060Sstevel@tonic-gate unlinkat(int dirfd, char *path, int flag)
81070Sstevel@tonic-gate {
81080Sstevel@tonic-gate 	if (flag == AT_REMOVEDIR) {
81090Sstevel@tonic-gate 		return (rmdir(path));
81100Sstevel@tonic-gate 	} else {
81110Sstevel@tonic-gate 		return (unlink(path));
81120Sstevel@tonic-gate 	}
81130Sstevel@tonic-gate }
81140Sstevel@tonic-gate 
81150Sstevel@tonic-gate int
81160Sstevel@tonic-gate fstatat(int fd, char *path, struct stat *buf, int flag)
81170Sstevel@tonic-gate {
81180Sstevel@tonic-gate 	if (flag == AT_SYMLINK_NOFOLLOW)
81190Sstevel@tonic-gate 		return (lstat(path, buf));
81200Sstevel@tonic-gate 	else
81210Sstevel@tonic-gate 		return (stat(path, buf));
81220Sstevel@tonic-gate }
81230Sstevel@tonic-gate 
81240Sstevel@tonic-gate int
81250Sstevel@tonic-gate attropen(char *file, char *attr, int omode, mode_t cmode)
81260Sstevel@tonic-gate {
81270Sstevel@tonic-gate 	errno = ENOTSUP;
81280Sstevel@tonic-gate 	return (-1);
81290Sstevel@tonic-gate }
81300Sstevel@tonic-gate #endif
8131