xref: /onnv-gate/usr/src/cmd/cpio/cpio.c (revision 1231)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*1231Smarks  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	All Rights Reserved					*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
320Sstevel@tonic-gate  * under license from the Regents of the University of California.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <stdio.h>
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <errno.h>
400Sstevel@tonic-gate #include <unistd.h>
410Sstevel@tonic-gate #include <stdlib.h>
420Sstevel@tonic-gate #include <fcntl.h>
430Sstevel@tonic-gate #include <memory.h>
440Sstevel@tonic-gate #include <string.h>
450Sstevel@tonic-gate #include <stdarg.h>
460Sstevel@tonic-gate #include <sys/stat.h>
470Sstevel@tonic-gate #include <sys/statvfs.h>
480Sstevel@tonic-gate #include <sys/mkdev.h>
490Sstevel@tonic-gate #include <sys/param.h>
500Sstevel@tonic-gate #include <utime.h>
510Sstevel@tonic-gate #include <pwd.h>
520Sstevel@tonic-gate #include <grp.h>
530Sstevel@tonic-gate #include <signal.h>
540Sstevel@tonic-gate #include <ctype.h>
550Sstevel@tonic-gate #include <archives.h>
560Sstevel@tonic-gate #include <locale.h>
570Sstevel@tonic-gate #include <sys/ioctl.h>
580Sstevel@tonic-gate #include <sys/mtio.h>
590Sstevel@tonic-gate #include <sys/fdio.h>
600Sstevel@tonic-gate #include "cpio.h"
610Sstevel@tonic-gate #include <sys/acl.h>
620Sstevel@tonic-gate #include <sys/time.h>
630Sstevel@tonic-gate #include <sys/resource.h>
640Sstevel@tonic-gate #include <fnmatch.h>
650Sstevel@tonic-gate #include <libgen.h>
660Sstevel@tonic-gate #include <libintl.h>
670Sstevel@tonic-gate #include <dirent.h>
680Sstevel@tonic-gate #include <limits.h>
69789Sahrens #include <aclutils.h>
701134Sceastha #ifdef SOLARIS_PRIVS
711134Sceastha #include <priv.h>
721134Sceastha #endif	/* SOLARIS_PRIVS */
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate  * Special kludge for off_t being a signed quantity.
760Sstevel@tonic-gate  */
770Sstevel@tonic-gate #if _FILE_OFFSET_BITS == 64
780Sstevel@tonic-gate typedef	u_longlong_t	u_off_t;
790Sstevel@tonic-gate #else
800Sstevel@tonic-gate typedef	ulong_t		u_off_t;
810Sstevel@tonic-gate #endif
820Sstevel@tonic-gate 
830Sstevel@tonic-gate #define	SECMODE	0xe080
840Sstevel@tonic-gate 
850Sstevel@tonic-gate #define	DEVNULL		"/dev/null"
860Sstevel@tonic-gate #define	XATTRHDR	".hdr"
870Sstevel@tonic-gate 
880Sstevel@tonic-gate #define	NAMELEN		32
890Sstevel@tonic-gate #define	TYPELEN 	16
900Sstevel@tonic-gate #define	PERMLEN		4
910Sstevel@tonic-gate 
920Sstevel@tonic-gate #define	FILE_COPIED	1
930Sstevel@tonic-gate #define	FILE_LINKED	2
940Sstevel@tonic-gate #define	FILE_PASS_ERR	-1
950Sstevel@tonic-gate 
960Sstevel@tonic-gate #define	ARCHIVE_NORMAL	0
970Sstevel@tonic-gate #define	ARCHIVE_ACL	1
980Sstevel@tonic-gate #define	ARCHIVE_XATTR	2
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate #define	LSTAT(dir, path, statbuf) fstatat(dir, \
1010Sstevel@tonic-gate     get_component((Gen.g_attrnam_p == (char *)NULL) ? \
1020Sstevel@tonic-gate     path : Gen.g_attrnam_p), statbuf, AT_SYMLINK_NOFOLLOW)
1030Sstevel@tonic-gate #define	STAT(dir, path, statbuf) fstatat(dir, \
1040Sstevel@tonic-gate     get_component((Gen.g_attrnam_p == (char *)NULL) ? \
1050Sstevel@tonic-gate     path : Gen.g_attrnam_p), statbuf, 0)
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate /*
1080Sstevel@tonic-gate  *	These limits reflect the maximum size regular file that
1090Sstevel@tonic-gate  *	can be archived, depending on the archive type. For archives
1100Sstevel@tonic-gate  *	with character-format headers (odc, tar, ustar) we use
11136Schin  *	CHAR_OFFSET_MAX.  For archives with SVR4 ASCII headers (-c, -H crc)
11236Schin  *	we store filesize in an 8-char hexadecimal string and use
11336Schin  *	ASC_OFFSET_MAX.  Otherwise, we are limited to the size that will
11436Schin  *	fit in a signed long value.
1150Sstevel@tonic-gate  */
11636Schin #define	CHAR_OFFSET_MAX	077777777777ULL	/* 11 octal digits */
11736Schin #define	ASC_OFFSET_MAX	0XFFFFFFFF	/* 8 hexadecimal digits */
1180Sstevel@tonic-gate #define	BIN_OFFSET_MAX	LONG_MAX	/* signed long max value */
1190Sstevel@tonic-gate 
120*1231Smarks #define	POSIXMODES	07777
121*1231Smarks 
1220Sstevel@tonic-gate static char	aclchar = ' ';
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate static struct Lnk *add_lnk(struct Lnk **);
1250Sstevel@tonic-gate static int bfill(void);
1260Sstevel@tonic-gate static void bflush(void);
1270Sstevel@tonic-gate static int chgreel(int dir);
1280Sstevel@tonic-gate static int ckname(int);
1290Sstevel@tonic-gate static void ckopts(long mask);
1300Sstevel@tonic-gate static long cksum(char hdr, int byt_cnt, int *err);
1310Sstevel@tonic-gate static int creat_hdr(void);
1320Sstevel@tonic-gate static int creat_lnk(int dirfd, char *name1_p, char *name2_p);
1330Sstevel@tonic-gate static int creat_spec(int dirfd);
1340Sstevel@tonic-gate static int creat_tmp(char *nam_p);
1350Sstevel@tonic-gate static void data_in(int proc_mode);
1360Sstevel@tonic-gate static void data_out(void);
1370Sstevel@tonic-gate static void data_pass(void);
1380Sstevel@tonic-gate static void file_in(void);
1390Sstevel@tonic-gate static int file_out(void);
1400Sstevel@tonic-gate static int file_pass(void);
1410Sstevel@tonic-gate static void flush_lnks(void);
1420Sstevel@tonic-gate static int gethdr(void);
1430Sstevel@tonic-gate static int getname(void);
1440Sstevel@tonic-gate static void getpats(int largc, char **largv);
1450Sstevel@tonic-gate static void ioerror(int dir);
1460Sstevel@tonic-gate static int matched(void);
1470Sstevel@tonic-gate static int missdir(char *nam_p);
1480Sstevel@tonic-gate static long mklong(short v[]);
1490Sstevel@tonic-gate static void mkshort(short sval[], long v);
1500Sstevel@tonic-gate static void msg(int severity, const char *fmt, ...);
1510Sstevel@tonic-gate static int openout(int dirfd);
1520Sstevel@tonic-gate static int read_hdr(int hdr);
1530Sstevel@tonic-gate static void reclaim(struct Lnk *l_p);
1540Sstevel@tonic-gate static void rstbuf(void);
1550Sstevel@tonic-gate static void setpasswd(char *nam);
1560Sstevel@tonic-gate static void rstfiles(int over, int dirfd);
1570Sstevel@tonic-gate static void scan4trail(void);
1580Sstevel@tonic-gate static void setup(int largc, char **largv);
1590Sstevel@tonic-gate static void set_tym(int dirfd, char *nam_p, time_t atime, time_t mtime);
1600Sstevel@tonic-gate static void sigint(int sig);
1610Sstevel@tonic-gate static void swap(char *buf_p, int cnt);
1620Sstevel@tonic-gate static void usage(void);
1630Sstevel@tonic-gate static void verbose(char *nam_p);
1640Sstevel@tonic-gate static void write_hdr(int secflag, off_t len);
1650Sstevel@tonic-gate static void write_trail(void);
1660Sstevel@tonic-gate static int ustar_dir(void);
1670Sstevel@tonic-gate static int ustar_spec(void);
1680Sstevel@tonic-gate static struct stat *convert_to_old_stat(struct stat *, char *, char *);
1690Sstevel@tonic-gate static void read_bar_vol_hdr(void);
1700Sstevel@tonic-gate static void read_bar_file_hdr(void);
1710Sstevel@tonic-gate static void setup_uncompress(FILE **);
1720Sstevel@tonic-gate static void skip_bar_volhdr(void);
1730Sstevel@tonic-gate static void bar_file_in(void);
1740Sstevel@tonic-gate static int g_init(int *devtype, int *fdes);
1750Sstevel@tonic-gate static int g_read(int, int, char *, unsigned);
1760Sstevel@tonic-gate static int g_write(int, int, char *, unsigned);
1770Sstevel@tonic-gate static int is_floppy(int);
1780Sstevel@tonic-gate static int is_tape(int);
1790Sstevel@tonic-gate static void write_ancillary(char *secinfo, int len);
1800Sstevel@tonic-gate static int remove_dir(char *);
1810Sstevel@tonic-gate static int save_cwd(void);
1820Sstevel@tonic-gate static void rest_cwd(int cwd);
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate static void xattrs_out(int (*func)());
1850Sstevel@tonic-gate static void get_parent(char *path, char *dir);
1860Sstevel@tonic-gate static void prepare_xattr_hdr(char **attrbuf, char *filename,
1870Sstevel@tonic-gate     char *attrname, char typeflag, struct Lnk *linkinfo, int *rlen);
1880Sstevel@tonic-gate static char tartype(int type);
1890Sstevel@tonic-gate static int openfile(int omode);
1900Sstevel@tonic-gate static mode_t attrmode(char type);
1910Sstevel@tonic-gate static char *get_component(char *path);
1920Sstevel@tonic-gate static int open_dir(char *name);
1930Sstevel@tonic-gate static int open_dirfd();
1940Sstevel@tonic-gate static void close_dirfd();
1950Sstevel@tonic-gate static void write_xattr_hdr();
1960Sstevel@tonic-gate static int retry_attrdir_open(char *name);
1970Sstevel@tonic-gate static char *skipslashes(char *string, char *start);
1980Sstevel@tonic-gate static int read_xattr_hdr();
1990Sstevel@tonic-gate static void chop_endslashes(char *path);
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate /* helpful types */
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate static
2050Sstevel@tonic-gate struct passwd	*Curpw_p,	/* Current password entry for -t option */
2060Sstevel@tonic-gate 		*Rpw_p,		/* Password entry for -R option */
2070Sstevel@tonic-gate 		*dpasswd;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate static
2100Sstevel@tonic-gate struct group	*Curgr_p,	/* Current group entry for -t option */
2110Sstevel@tonic-gate 		*dgroup;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate /* Data structure for buffered I/O. */
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate static
2160Sstevel@tonic-gate struct buf_info {
2170Sstevel@tonic-gate 	char	*b_base_p,	/* Pointer to base of buffer */
2180Sstevel@tonic-gate 		*b_out_p,	/* Position to take bytes from buffer at */
2190Sstevel@tonic-gate 		*b_in_p,	/* Position to put bytes into buffer at */
2200Sstevel@tonic-gate 		*b_end_p;	/* Pointer to end of buffer */
2210Sstevel@tonic-gate 	long	b_cnt,		/* Count of unprocessed bytes */
2220Sstevel@tonic-gate 		b_size;		/* Size of buffer in bytes */
2230Sstevel@tonic-gate } Buffr;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate /* Generic header format */
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate static
2280Sstevel@tonic-gate struct gen_hdr {
2290Sstevel@tonic-gate 	ulong_t	g_magic,	/* Magic number field */
2300Sstevel@tonic-gate 		g_ino,		/* Inode number of file */
2310Sstevel@tonic-gate 		g_mode,		/* Mode of file */
2320Sstevel@tonic-gate 		g_uid,		/* Uid of file */
2330Sstevel@tonic-gate 		g_gid,		/* Gid of file */
2340Sstevel@tonic-gate 		g_nlink,	/* Number of links */
2350Sstevel@tonic-gate 		g_mtime;	/* Modification time */
2360Sstevel@tonic-gate 	off_t	g_filesz;	/* Length of file */
2370Sstevel@tonic-gate 	ulong_t	g_dev,		/* File system of file */
2380Sstevel@tonic-gate 		g_rdev,		/* Major/minor numbers of special files */
2390Sstevel@tonic-gate 		g_namesz,	/* Length of filename */
2400Sstevel@tonic-gate 		g_cksum;	/* Checksum of file */
2410Sstevel@tonic-gate 	char	g_gname[32],
2420Sstevel@tonic-gate 		g_uname[32],
2430Sstevel@tonic-gate 		g_version[2],
2440Sstevel@tonic-gate 		g_tmagic[6],
2450Sstevel@tonic-gate 		g_typeflag;
2460Sstevel@tonic-gate 	char	*g_tname,
2470Sstevel@tonic-gate 		*g_prefix,
2480Sstevel@tonic-gate 		*g_nam_p,	/* Filename */
2490Sstevel@tonic-gate 		*g_attrnam_p,	/* attribute */
2500Sstevel@tonic-gate 		*g_attrfnam_p,  /* Real file name attr belongs to */
2510Sstevel@tonic-gate 		*g_linktoattrfnam_p, /* file linked attribute belongs to */
2520Sstevel@tonic-gate 		*g_linktoattrnam_p,  /* attribute g_attrnam_p is linked to */
2530Sstevel@tonic-gate 		*g_dirpath;	/* dirname currently opened */
2540Sstevel@tonic-gate 	int	g_dirfd;	/* directory file descriptor */
2550Sstevel@tonic-gate 	int	g_passdirfd;	/* directory fd to pass to */
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate } Gen, *G_p;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate /* Data structure for handling multiply-linked files */
2600Sstevel@tonic-gate static
2610Sstevel@tonic-gate char	prebuf[PRESIZ+1],
2620Sstevel@tonic-gate 	nambuf[NAMSIZ+1],
2630Sstevel@tonic-gate 	fullnam[MAXNAM+1];
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate static
2670Sstevel@tonic-gate struct Lnk {
2680Sstevel@tonic-gate 	short	L_cnt,		/* Number of links encountered */
2690Sstevel@tonic-gate 		L_data;		/* Data has been encountered if 1 */
2700Sstevel@tonic-gate 	struct gen_hdr	L_gen;	/* gen_hdr information for this file */
2710Sstevel@tonic-gate 	struct Lnk	*L_nxt_p,	/* Next file in list */
2720Sstevel@tonic-gate 			*L_bck_p,	/* Previous file in list */
2730Sstevel@tonic-gate 			*L_lnk_p;	/* Next link for this file */
2740Sstevel@tonic-gate } Lnk_hd;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate static
2770Sstevel@tonic-gate struct hdr_cpio	Hdr;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate /*
2800Sstevel@tonic-gate  * -------------------------------------------------------------------------
2810Sstevel@tonic-gate  *		   Stuff needed to pre-view the name stream
2820Sstevel@tonic-gate  *
2830Sstevel@tonic-gate  * issymlink is used to remember that the current file is a symlink between
2840Sstevel@tonic-gate  * getname() and file_pass(); the former trashes this information immediately
2850Sstevel@tonic-gate  * when -L is specified.
2860Sstevel@tonic-gate  */
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate static
2890Sstevel@tonic-gate int	issymlink = 0;
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate static
2920Sstevel@tonic-gate FILE	*In_p = stdin;		/* Where the input comes from */
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate typedef struct sl_info
2950Sstevel@tonic-gate {
2960Sstevel@tonic-gate 	struct sl_info *llink;	/* Left subtree ptr (tree depth in *sl_head) */
2970Sstevel@tonic-gate 	struct sl_info *rlink;	/* Right subtree ptr */
2980Sstevel@tonic-gate 	int bal;		/* Subtree balance factor */
2990Sstevel@tonic-gate 	ulong_t	sl_count;	/* Number of symlinks */
3000Sstevel@tonic-gate 	ino_t	sl_ino;		/* Inode of file */
3010Sstevel@tonic-gate 	ino_t	sl_ino2;	/* alternate inode for -Hodc */
3020Sstevel@tonic-gate } sl_info_t;
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate /*
3050Sstevel@tonic-gate  * The following structure maintains a hash entry for the
3060Sstevel@tonic-gate  * balancing trees which are allocated for each device nodes.
3070Sstevel@tonic-gate  */
3080Sstevel@tonic-gate typedef struct sl_info_link
3090Sstevel@tonic-gate {
3100Sstevel@tonic-gate 	dev_t		dev;
3110Sstevel@tonic-gate 	sl_info_t	*head;
3120Sstevel@tonic-gate 	struct sl_info_link *next;
3130Sstevel@tonic-gate } sl_info_link_t;
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate #define	SL_INFO_ALLOC_CHUNK	1024
3160Sstevel@tonic-gate #define	NDEVHENTRY		0x40
3170Sstevel@tonic-gate #define	DEV_HASHKEY(x)		((x) & (NDEVHENTRY -1))
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate /*
3200Sstevel@tonic-gate  * For remapping dev,inode for -Hodc archives.
3210Sstevel@tonic-gate  */
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate typedef struct sl_remap
3240Sstevel@tonic-gate {
3250Sstevel@tonic-gate 	dev_t			dev;		/* device */
3260Sstevel@tonic-gate 	int			inode_count;	/* # inodes seen on dev */
3270Sstevel@tonic-gate 	struct sl_remap 	*next;		/* next in the chain */
3280Sstevel@tonic-gate } sl_remap_t;
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate /* forward declarations */
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate static sl_info_t 	*sl_info_alloc(void);
3330Sstevel@tonic-gate static sl_info_t 	*sl_insert(dev_t, ino_t);
3340Sstevel@tonic-gate static ulong_t		sl_numlinks(dev_t, ino_t);
3350Sstevel@tonic-gate static void		sl_preview_synonyms(void);
3360Sstevel@tonic-gate static void		sl_remember_tgt(const struct stat *, int);
3370Sstevel@tonic-gate static sl_info_t 	*sl_search(dev_t, ino_t);
3380Sstevel@tonic-gate static sl_info_t	*sl_devhash_lookup(dev_t);
3390Sstevel@tonic-gate static void		sl_devhash_insert(dev_t, sl_info_t *);
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate extern int		sl_compare(ino_t, ino_t);
3420Sstevel@tonic-gate #define	sl_compare(lino, rino)	(lino < rino ? -1 : (lino > rino ? 1 : 0))
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate /* global storage */
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate static sl_remap_t  *sl_remap_head = NULL; /* head of the inode-remap list */
3470Sstevel@tonic-gate static sl_info_link_t	*sl_devhash[NDEVHENTRY]; /* hash table */
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate /*
3500Sstevel@tonic-gate  * -------------------------------------------------------------------------
3510Sstevel@tonic-gate  */
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate static
3540Sstevel@tonic-gate struct stat	ArchSt,	/* stat(2) information of the archive */
3550Sstevel@tonic-gate 		SrcSt,	/* stat(2) information of source file */
3560Sstevel@tonic-gate 		DesSt,	/* stat(2) of destination file */
3570Sstevel@tonic-gate 		*OldSt = NULL;	/* stat info converted to svr32 format */
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate /*
3600Sstevel@tonic-gate  * bin_mag: Used to validate a binary magic number,
3610Sstevel@tonic-gate  * by combining to bytes into an unsigned short.
3620Sstevel@tonic-gate  */
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate static
3650Sstevel@tonic-gate union bin_mag {
3660Sstevel@tonic-gate 	unsigned char b_byte[2];
3670Sstevel@tonic-gate 	ushort_t b_half;
3680Sstevel@tonic-gate } Binmag;
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate static
3710Sstevel@tonic-gate union tblock *Thdr_p;	/* TAR header pointer */
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate static union b_block *bar_Vhdr;
3740Sstevel@tonic-gate static struct gen_hdr Gen_bar_vol;
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate /*
3770Sstevel@tonic-gate  * swpbuf: Used in swap() to swap bytes within a halfword,
3780Sstevel@tonic-gate  * halfwords within a word, or to reverse the order of the
3790Sstevel@tonic-gate  * bytes within a word.  Also used in mklong() and mkshort().
3800Sstevel@tonic-gate  */
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate static
3830Sstevel@tonic-gate union swpbuf {
3840Sstevel@tonic-gate 	unsigned char	s_byte[4];
3850Sstevel@tonic-gate 	ushort_t	s_half[2];
3860Sstevel@tonic-gate 	ulong_t	s_word;
3870Sstevel@tonic-gate } *Swp_p;
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate static
3900Sstevel@tonic-gate char	Adir,			/* Flags object as a directory */
3910Sstevel@tonic-gate 	Hiddendir,		/* Processing hidden attribute directory */
3920Sstevel@tonic-gate 	Aspec,			/* Flags object as a special file */
3930Sstevel@tonic-gate 	Do_rename,		/* Indicates rename() is to be used */
3940Sstevel@tonic-gate 	Time[50],		/* Array to hold date and time */
3950Sstevel@tonic-gate 	Ttyname[] = "/dev/tty",	/* Controlling console */
3960Sstevel@tonic-gate 	T_lname[MAXPATHLEN],	/* Array to hold links name for tar */
3970Sstevel@tonic-gate 	*Buf_p,			/* Buffer for file system I/O */
3980Sstevel@tonic-gate 	*Empty,			/* Empty block for TARTYP headers */
3990Sstevel@tonic-gate 	*Full_p,		/* Pointer to full pathname */
4000Sstevel@tonic-gate 	*Efil_p,		/* -E pattern file string */
4010Sstevel@tonic-gate 	*Eom_p = "Change to part %d and press RETURN key. [q] ",
4020Sstevel@tonic-gate 	*Fullnam_p,		/* Full pathname */
4030Sstevel@tonic-gate 	*Attrfile_p,		/* attribute file */
4040Sstevel@tonic-gate 	*Hdr_p,			/* -H header type string */
4050Sstevel@tonic-gate 	*IOfil_p,		/* -I/-O input/output archive string */
4060Sstevel@tonic-gate 	*Lnkend_p,		/* Pointer to end of Lnknam_p */
4070Sstevel@tonic-gate 	*Lnknam_p,		/* Buffer for linking files with -p option */
4080Sstevel@tonic-gate 	*Nam_p,			/* Array to hold filename */
4090Sstevel@tonic-gate 	*Savenam_p,		/* copy of filename xattr belongs to */
4100Sstevel@tonic-gate 	*Own_p,			/* New owner login id string */
4110Sstevel@tonic-gate 	*Renam_p,		/* Buffer for renaming files */
4120Sstevel@tonic-gate 	*Renametmp_p,		/* Tmp Buffer for renaming files */
4130Sstevel@tonic-gate 	*Symlnk_p,		/* Buffer for holding symbolic link name */
4140Sstevel@tonic-gate 	*Over_p,		/* Holds temporary filename when overwriting */
4150Sstevel@tonic-gate 	**Pat_pp = 0,		/* Pattern strings */
4160Sstevel@tonic-gate 	bar_linkflag,		/* flag to indicate if the file is a link */
4170Sstevel@tonic-gate 	bar_linkname[MAXPATHLEN]; /* store the name of the link */
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate static
4200Sstevel@tonic-gate int	Append = 0,	/* Flag set while searching to end of archive */
4210Sstevel@tonic-gate 	Archive,	/* File descriptor of the archive */
4220Sstevel@tonic-gate 	Buf_error = 0,	/* I/O error occured during buffer fill */
4230Sstevel@tonic-gate 	Def_mode = 0777,	/* Default file/directory protection modes */
4240Sstevel@tonic-gate 	Device,		/* Device type being accessed (used with libgenIO) */
4250Sstevel@tonic-gate 	Error_cnt = 0,	/* Cumulative count of I/O errors */
4260Sstevel@tonic-gate 	Finished = 1,	/* Indicates that a file transfer has completed */
4270Sstevel@tonic-gate 	Hdrsz = ASCSZ,	/* Fixed length portion of the header */
4280Sstevel@tonic-gate 	Hdr_type,		/* Flag to indicate type of header selected */
4290Sstevel@tonic-gate 	Ifile,		/* File des. of file being archived */
4300Sstevel@tonic-gate 	Ofile,		/* File des. of file being extracted from archive */
4310Sstevel@tonic-gate 	Use_old_stat = 0,    /* Create an old style -Hodc hdr (small dev's) */
4320Sstevel@tonic-gate 	Onecopy = 0,	/* Flags old vs. new link handling */
4330Sstevel@tonic-gate 	Pad_val = 0,	/* Indicates the number of bytes to pad (if any) */
4340Sstevel@tonic-gate 	PageSize = 0,	/* The native page size, used for figuring block size */
4350Sstevel@tonic-gate 	Volcnt = 1,	/* Number of archive volumes processed */
4360Sstevel@tonic-gate 	Verbcnt = 0,	/* Count of number of dots '.' output */
4370Sstevel@tonic-gate 	Eomflag = 0,
4380Sstevel@tonic-gate 	Dflag = 0,
4390Sstevel@tonic-gate 	Atflag = 0,	/* Archive/restore extended attributes */
4400Sstevel@tonic-gate 	Compressed,	/* Flag to indicate if the bar archive is compressed */
4411134Sceastha 	Bar_vol_num = 0, /* Volume number count for bar archive */
4421134Sceastha 	privileged = 0;	/* Flag set if running with higher privileges */
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate static
4460Sstevel@tonic-gate gid_t	Lastgid = -1;	/* Used with -t & -v to record current gid */
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate static
4490Sstevel@tonic-gate uid_t	Lastuid = -1;	/* Used with -t & -v to record current uid */
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate static
4520Sstevel@tonic-gate long	Args,		/* Mask of selected options */
4530Sstevel@tonic-gate 	Max_namesz = CPATH;	/* Maximum size of pathnames/filenames */
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate static
4560Sstevel@tonic-gate int	Bufsize = BUFSZ;	/* Default block size */
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate static u_longlong_t    Blocks;	/* full blocks transferred */
4600Sstevel@tonic-gate static u_longlong_t    SBlocks;	/* cumulative char count from short reads */
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate static off_t	Max_offset = BIN_OFFSET_MAX;	/* largest file size */
4640Sstevel@tonic-gate static off_t	Max_filesz;			/* from getrlimit */
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate static
4670Sstevel@tonic-gate FILE	*Ef_p,			/* File pointer of pattern input file */
4680Sstevel@tonic-gate 	*Err_p = stderr,	/* File pointer for error reporting */
4690Sstevel@tonic-gate 	*Out_p = stdout,	/* File pointer for non-archive output */
4700Sstevel@tonic-gate 	*Rtty_p,		/* Input file pointer for interactive rename */
4710Sstevel@tonic-gate 	*Wtty_p;		/* Output file ptr for interactive rename */
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate static
4740Sstevel@tonic-gate ushort_t	Ftype = S_IFMT;	/* File type mask */
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate /* ACL support */
4770Sstevel@tonic-gate static struct sec_attr {
4780Sstevel@tonic-gate 	char	attr_type;
4790Sstevel@tonic-gate 	char	attr_len[7];
4800Sstevel@tonic-gate 	char	attr_info[1];
4810Sstevel@tonic-gate } *attr;
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate static int	Pflag = 0;	/* flag indicates that acl is preserved */
484789Sahrens static int	acl_is_set = 0; /* True if an acl was set on the file */
485789Sahrens 
486789Sahrens acl_t *aclp;
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate /*
4890Sstevel@tonic-gate  *
4900Sstevel@tonic-gate  * cpio has been changed to support extended attributes.
4910Sstevel@tonic-gate  *
4920Sstevel@tonic-gate  * As part of this change cpio has been changed to use the new *at() syscalls
4930Sstevel@tonic-gate  * such as openat, fchownat(), unlinkat()...
4940Sstevel@tonic-gate  *
4950Sstevel@tonic-gate  * This was done so that attributes can be handled with as few code changes
4960Sstevel@tonic-gate  * as possible.
4970Sstevel@tonic-gate  *
4980Sstevel@tonic-gate  * What this means is that cpio now opens the directory that a file or directory
4990Sstevel@tonic-gate  * resides in and then performs *at() functions to manipulate the entry.
5000Sstevel@tonic-gate  *
5010Sstevel@tonic-gate  * For example a new file is now created like this:
5020Sstevel@tonic-gate  *
5030Sstevel@tonic-gate  * dfd = open(<some dir path>)
5040Sstevel@tonic-gate  * fd = openat(dfd, <name>,....);
5050Sstevel@tonic-gate  *
5060Sstevel@tonic-gate  * or in the case of an extended attribute
5070Sstevel@tonic-gate  *
5080Sstevel@tonic-gate  * dfd = attropen(<pathname>, ".", ....)
5090Sstevel@tonic-gate  *
5100Sstevel@tonic-gate  * Once we have a directory file descriptor all of the *at() functions can
5110Sstevel@tonic-gate  * be applied to it.
5120Sstevel@tonic-gate  *
5130Sstevel@tonic-gate  * unlinkat(dfd, <component name>,...)
5140Sstevel@tonic-gate  * fchownat(dfd, <component name>,..)
5150Sstevel@tonic-gate  *
5160Sstevel@tonic-gate  * This works for both normal namespace files and extended attribute file
5170Sstevel@tonic-gate  *
5180Sstevel@tonic-gate  */
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate /*
5210Sstevel@tonic-gate  * Extended attribute layout
5220Sstevel@tonic-gate  *
5230Sstevel@tonic-gate  * Extended attributes are stored in two pieces.
5240Sstevel@tonic-gate  * 1. An attribute header which has information about
5250Sstevel@tonic-gate  *    what file the attribute is for and what the attribute
5260Sstevel@tonic-gate  *    is named.
5270Sstevel@tonic-gate  * 2. The attribute record itself.  Stored as a normal file type
5280Sstevel@tonic-gate  *    of entry.
5290Sstevel@tonic-gate  * Both the header and attribute record have special modes/typeflags
5300Sstevel@tonic-gate  * associated with them.
5310Sstevel@tonic-gate  *
5320Sstevel@tonic-gate  * The names of the header in the archive look like:
5330Sstevel@tonic-gate  * /dev/null/attr.hdr
5340Sstevel@tonic-gate  *
5350Sstevel@tonic-gate  * The name of the attribute looks like:
5360Sstevel@tonic-gate  * /dev/null/attr.
5370Sstevel@tonic-gate  *
5380Sstevel@tonic-gate  * This is done so that an archiver that doesn't understand these formats
5390Sstevel@tonic-gate  * can just dispose of the attribute records unless the user chooses to
5400Sstevel@tonic-gate  * rename them via cpio -r or pax -i
5410Sstevel@tonic-gate  *
5420Sstevel@tonic-gate  * The format is composed of a fixed size header followed
5430Sstevel@tonic-gate  * by a variable sized xattr_buf. If the attribute is a hard link
5440Sstevel@tonic-gate  * to another attribute, then another xattr_buf section is included
5450Sstevel@tonic-gate  * for the link.
5460Sstevel@tonic-gate  *
5470Sstevel@tonic-gate  * The xattr_buf is used to define the necessary "pathing" steps
5480Sstevel@tonic-gate  * to get to the extended attribute.  This is necessary to support
5490Sstevel@tonic-gate  * a fully recursive attribute model where an attribute may itself
5500Sstevel@tonic-gate  * have an attribute.
5510Sstevel@tonic-gate  *
5520Sstevel@tonic-gate  * The basic layout looks like this.
5530Sstevel@tonic-gate  *
5540Sstevel@tonic-gate  *     --------------------------------
5550Sstevel@tonic-gate  *     |                              |
5560Sstevel@tonic-gate  *     |         xattr_hdr            |
5570Sstevel@tonic-gate  *     |                              |
5580Sstevel@tonic-gate  *     --------------------------------
5590Sstevel@tonic-gate  *     --------------------------------
5600Sstevel@tonic-gate  *     |                              |
5610Sstevel@tonic-gate  *     |        xattr_buf             |
5620Sstevel@tonic-gate  *     |                              |
5630Sstevel@tonic-gate  *     --------------------------------
5640Sstevel@tonic-gate  *     --------------------------------
5650Sstevel@tonic-gate  *     |                              |
5660Sstevel@tonic-gate  *     |      (optional link info)    |
5670Sstevel@tonic-gate  *     |                              |
5680Sstevel@tonic-gate  *     --------------------------------
5690Sstevel@tonic-gate  *     --------------------------------
5700Sstevel@tonic-gate  *     |                              |
5710Sstevel@tonic-gate  *     |      attribute itself        |
5720Sstevel@tonic-gate  *     |      stored as normal tar    |
5730Sstevel@tonic-gate  *     |      or cpio data with       |
5740Sstevel@tonic-gate  *     |      special mode or         |
5750Sstevel@tonic-gate  *     |      typeflag                |
5760Sstevel@tonic-gate  *     |                              |
5770Sstevel@tonic-gate  *     --------------------------------
5780Sstevel@tonic-gate  *
5790Sstevel@tonic-gate  */
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate /*
5820Sstevel@tonic-gate  * Extended attributes structures
5830Sstevel@tonic-gate  *
5840Sstevel@tonic-gate  * xattrhead is the complete extended attribute header, as read of off
5850Sstevel@tonic-gate  * disk/tape. It includes the variable xattr_buf portion.
5860Sstevel@tonic-gate  *
5870Sstevel@tonic-gate  * xattrp is basically an offset into xattrhead that points to the
5880Sstevel@tonic-gate  * "pathing" section which defines how to get to the attribute.
5890Sstevel@tonic-gate  *
5900Sstevel@tonic-gate  * xattr_linkp is identical to xattrp except that it is used for linked
5910Sstevel@tonic-gate  * attributes.  It provides the pathing steps to get to the linked
5920Sstevel@tonic-gate  * attribute.
5930Sstevel@tonic-gate  *
5940Sstevel@tonic-gate  * These structures are updated when an extended attribute header is read off
5950Sstevel@tonic-gate  * of disk/tape.
5960Sstevel@tonic-gate  */
5970Sstevel@tonic-gate static struct xattr_hdr	*xattrhead;
5980Sstevel@tonic-gate static struct xattr_buf	*xattrp;
5990Sstevel@tonic-gate static struct xattr_buf	*xattr_linkp;
6000Sstevel@tonic-gate static int 		xattrbadhead;	/* is extended attribute header bad? */
6010Sstevel@tonic-gate 
602789Sahrens static int	append_secattr(char **, int *, acl_t *);
6030Sstevel@tonic-gate static void	write_ancillary(char *, int);
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate /*
6060Sstevel@tonic-gate  * Note regarding cpio and changes to ensure cpio doesn't try to second
6070Sstevel@tonic-gate  * guess whether it runs with sufficient privileges or not:
6080Sstevel@tonic-gate  *
6090Sstevel@tonic-gate  * cpio has been changed so that it doesn't carry a second implementation of
6100Sstevel@tonic-gate  * the kernel's policy with respect to privileges.  Instead of attempting
6110Sstevel@tonic-gate  * to restore uid and gid from an archive only if cpio is run as uid 0,
6121134Sceastha  * cpio now *always* tries to restore the uid and gid from the archive
6131134Sceastha  * except when the -R option is specified.  When the -R is specified,
6141134Sceastha  * the uid and gid of the restored file will be changed to those of the
6151134Sceastha  * login id specified.  In addition, chown(), set_tym(), and chmod() should
6161134Sceastha  * only be executed once during archive extraction, and to ensure
6171134Sceastha  * setuid/setgid bits are restored properly, chown() should always be
6181134Sceastha  * executed before chmod().
6190Sstevel@tonic-gate  *
6200Sstevel@tonic-gate  * Note regarding debugging mechanism for cpio:
6210Sstevel@tonic-gate  *
6220Sstevel@tonic-gate  * The following mechanism is provided to allow us to debug cpio in complicated
6230Sstevel@tonic-gate  * situations, like when it is part of a pipe.  The idea is that you compile
6240Sstevel@tonic-gate  * with -DWAITAROUND defined, and then add the "-z" command line option to the
6250Sstevel@tonic-gate  * target cpio invocation.  If stderr is available, it will tell you to which
6260Sstevel@tonic-gate  * pid to attach the debugger; otherwise, use ps to find it.  Attach to the
6270Sstevel@tonic-gate  * process from the debugger, and, *PRESTO*, you are there!
6280Sstevel@tonic-gate  *
6290Sstevel@tonic-gate  * Simply assign "waitaround = 0" once you attach to the process, and then
6300Sstevel@tonic-gate  * proceed from there as usual.
6310Sstevel@tonic-gate  */
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate #ifdef WAITAROUND
6340Sstevel@tonic-gate int waitaround = 0;		/* wait for rendezvous with the debugger */
6350Sstevel@tonic-gate #endif
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate /*
6380Sstevel@tonic-gate  * Allocation wrappers and their flags
6390Sstevel@tonic-gate  */
6400Sstevel@tonic-gate #define	E_NORMAL	0x0	/* Return NULL if allocation fails */
6410Sstevel@tonic-gate #define	E_EXIT		0x1	/* Exit if allocation fails */
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate static void *e_realloc(int flag, void *old, size_t newsize);
6440Sstevel@tonic-gate static char *e_strdup(int flag, const char *arg);
6450Sstevel@tonic-gate static void *e_valloc(int flag, size_t size);
6460Sstevel@tonic-gate static void *e_zalloc(int flag, size_t size);
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate #define	EXIT_CODE	(Error_cnt > 255 ? 255 : Error_cnt)
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate /*
6510Sstevel@tonic-gate  * main: Call setup() to process options and perform initializations,
6520Sstevel@tonic-gate  * and then select either copy in (-i), copy out (-o), or pass (-p) action.
6530Sstevel@tonic-gate  */
6540Sstevel@tonic-gate 
65536Schin int
6560Sstevel@tonic-gate main(int argc, char **argv)
6570Sstevel@tonic-gate {
6580Sstevel@tonic-gate 	int i;
6590Sstevel@tonic-gate 	int passret;
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
6620Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
6630Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
6640Sstevel@tonic-gate #endif
6650Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	(void) memset(&Gen, 0, sizeof (Gen));
6680Sstevel@tonic-gate 	setup(argc, argv);
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	if (signal(SIGINT, sigint) == SIG_IGN)
6710Sstevel@tonic-gate 		(void) signal(SIGINT, SIG_IGN);
6720Sstevel@tonic-gate 	switch (Args & (OCi | OCo | OCp)) {
6730Sstevel@tonic-gate 	case OCi: /* COPY IN */
6740Sstevel@tonic-gate 		Hdr_type = NONE;
6750Sstevel@tonic-gate 		while ((i = gethdr()) != 0) {
6760Sstevel@tonic-gate 			Gen.g_dirfd = -1;
6770Sstevel@tonic-gate 			if (i == 1) {
6780Sstevel@tonic-gate 				file_in();
6790Sstevel@tonic-gate 				/*
6800Sstevel@tonic-gate 				 * Any ACL info for this file would or should
6810Sstevel@tonic-gate 				 * have been used after file_in(); clear out
6820Sstevel@tonic-gate 				 * aclp so it is is not erroneously used on
6830Sstevel@tonic-gate 				 * the next file.
6840Sstevel@tonic-gate 				 */
6850Sstevel@tonic-gate 				if (aclp != NULL) {
686789Sahrens 					acl_free(aclp);
6870Sstevel@tonic-gate 					aclp = NULL;
6880Sstevel@tonic-gate 				}
689789Sahrens 				acl_is_set = 0;
6900Sstevel@tonic-gate 			}
6910Sstevel@tonic-gate 			(void) memset(&Gen, 0, sizeof (Gen));
6920Sstevel@tonic-gate 		}
6930Sstevel@tonic-gate 		/* Do not count "extra" "read-ahead" buffered data */
6940Sstevel@tonic-gate 		if (Buffr.b_cnt > Bufsize)
6950Sstevel@tonic-gate 			Blocks -=  (u_longlong_t)(Buffr.b_cnt / Bufsize);
6960Sstevel@tonic-gate 		break;
6970Sstevel@tonic-gate 	case OCo: /* COPY OUT */
6980Sstevel@tonic-gate 		if (Args & OCA) {
6990Sstevel@tonic-gate 			scan4trail();
7000Sstevel@tonic-gate 		}
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 		Gen.g_dirfd = -1;
7030Sstevel@tonic-gate 		Gen.g_dirpath = NULL;
7040Sstevel@tonic-gate 		sl_preview_synonyms();
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 		while ((i = getname()) != 0) {
7070Sstevel@tonic-gate 			if (i == 1) {
7080Sstevel@tonic-gate 				(void) file_out();
7090Sstevel@tonic-gate 				if (Atflag) {
7100Sstevel@tonic-gate 					if (Gen.g_dirfd != -1) {
7110Sstevel@tonic-gate 						(void) close(Gen.g_dirfd);
7120Sstevel@tonic-gate 					}
7130Sstevel@tonic-gate 					Gen.g_dirfd = -1;
7140Sstevel@tonic-gate 					xattrs_out(file_out);
7150Sstevel@tonic-gate 				}
7160Sstevel@tonic-gate 				Hiddendir = 0;
7170Sstevel@tonic-gate 			}
7180Sstevel@tonic-gate 			if (aclp != NULL) {
719789Sahrens 				acl_free(aclp);
7200Sstevel@tonic-gate 				aclp = NULL;
721789Sahrens 				acl_is_set = 0;
7220Sstevel@tonic-gate 			}
7230Sstevel@tonic-gate 		}
7240Sstevel@tonic-gate 		write_trail();
7250Sstevel@tonic-gate 		break;
7260Sstevel@tonic-gate 	case OCp: /* PASS */
7270Sstevel@tonic-gate 		sl_preview_synonyms();
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 		Gen.g_dirfd = -1;
7300Sstevel@tonic-gate 		Gen.g_passdirfd = -1;
7310Sstevel@tonic-gate 		Gen.g_dirpath = NULL;
7320Sstevel@tonic-gate 		while (getname()) {
7330Sstevel@tonic-gate 			/*
7340Sstevel@tonic-gate 			 * If file is a fully qualified path then
7350Sstevel@tonic-gate 			 * file_pass will strip off the leading '/'
7360Sstevel@tonic-gate 			 * and we need to save off the unstripped
7370Sstevel@tonic-gate 			 * name for attribute traversal.
7380Sstevel@tonic-gate 			 */
7390Sstevel@tonic-gate 			if (Atflag) {
7400Sstevel@tonic-gate 				(void) strcpy(Savenam_p, Gen.g_nam_p);
7410Sstevel@tonic-gate 			}
7420Sstevel@tonic-gate 			passret = file_pass();
7430Sstevel@tonic-gate 			if (aclp != NULL) {
744789Sahrens 				acl_free(aclp);
7450Sstevel@tonic-gate 				aclp = NULL;
746789Sahrens 				acl_is_set = 0;
7470Sstevel@tonic-gate 			}
7480Sstevel@tonic-gate 			if (Gen.g_passdirfd != -1)
7490Sstevel@tonic-gate 				(void) close(Gen.g_passdirfd);
7500Sstevel@tonic-gate 			Gen.g_passdirfd = -1;
7510Sstevel@tonic-gate 			if (Atflag) {
7520Sstevel@tonic-gate 				if (Gen.g_dirfd != -1) {
7530Sstevel@tonic-gate 					(void) close(Gen.g_dirfd);
7540Sstevel@tonic-gate 				}
7550Sstevel@tonic-gate 				Gen.g_dirfd = -1;
7560Sstevel@tonic-gate 				if (passret != FILE_LINKED) {
7570Sstevel@tonic-gate 					Gen.g_nam_p = Savenam_p;
7580Sstevel@tonic-gate 					xattrs_out(file_pass);
7590Sstevel@tonic-gate 				}
7600Sstevel@tonic-gate 			}
7610Sstevel@tonic-gate 		}
7620Sstevel@tonic-gate 		break;
7630Sstevel@tonic-gate 	default:
7640Sstevel@tonic-gate 		msg(EXT, "Impossible action.");
7650Sstevel@tonic-gate 	}
7660Sstevel@tonic-gate 	if (Ofile > 0) {
7670Sstevel@tonic-gate 		if (close(Ofile) != 0)
7680Sstevel@tonic-gate 			msg(EXTN, "close error");
7690Sstevel@tonic-gate 	}
7700Sstevel@tonic-gate 	if (Archive > 0) {
7710Sstevel@tonic-gate 		if (close(Archive) != 0)
7720Sstevel@tonic-gate 			msg(EXTN, "close error");
7730Sstevel@tonic-gate 	}
7740Sstevel@tonic-gate 	Blocks = (u_longlong_t)(Blocks * Bufsize + SBlocks + 0x1FF) >> 9;
7750Sstevel@tonic-gate 	msg(EPOST, "%lld blocks", Blocks);
7760Sstevel@tonic-gate 	if (Error_cnt)
7770Sstevel@tonic-gate 		msg(EPOST, "%d error(s)", Error_cnt);
7780Sstevel@tonic-gate 	return (EXIT_CODE);
7790Sstevel@tonic-gate }
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate /*
7820Sstevel@tonic-gate  * add_lnk: Add a linked file's header to the linked file data structure, by
7830Sstevel@tonic-gate  * either adding it to the end of an existing sub-list or starting
7840Sstevel@tonic-gate  * a new sub-list.  Each sub-list saves the links to a given file.
7850Sstevel@tonic-gate  *
7860Sstevel@tonic-gate  * Directly returns a pointer to the new entry; returns a pointer to the head
7870Sstevel@tonic-gate  * of the sub-list in which that entry is located through the argument.
7880Sstevel@tonic-gate  */
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate static struct Lnk *
7910Sstevel@tonic-gate add_lnk(struct Lnk **sublist_return)
7920Sstevel@tonic-gate {
7930Sstevel@tonic-gate 	struct Lnk *new_entry, *sublist;
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	for (sublist = Lnk_hd.L_nxt_p;
7960Sstevel@tonic-gate 	    sublist != &Lnk_hd;
7970Sstevel@tonic-gate 	    sublist = sublist->L_nxt_p) {
7980Sstevel@tonic-gate 		if (sublist->L_gen.g_ino == G_p->g_ino &&
7990Sstevel@tonic-gate 		    sublist->L_gen.g_dev == G_p->g_dev) {
8000Sstevel@tonic-gate 			/* found */
8010Sstevel@tonic-gate 			break;
8020Sstevel@tonic-gate 		}
8030Sstevel@tonic-gate 	}
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	new_entry = e_zalloc(E_EXIT, sizeof (struct Lnk));
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	new_entry->L_lnk_p = NULL;
8080Sstevel@tonic-gate 	new_entry->L_gen = *G_p; /* structure copy */
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	new_entry->L_gen.g_nam_p = e_zalloc(E_EXIT, (size_t)G_p->g_namesz);
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	(void) strcpy(new_entry->L_gen.g_nam_p, G_p->g_nam_p);
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	if (sublist == &Lnk_hd) {
8150Sstevel@tonic-gate 		/* start new sub-list */
8160Sstevel@tonic-gate 		new_entry->L_nxt_p = &Lnk_hd;
8170Sstevel@tonic-gate 		new_entry->L_bck_p = Lnk_hd.L_bck_p;
8180Sstevel@tonic-gate 		Lnk_hd.L_bck_p = new_entry->L_bck_p->L_nxt_p = new_entry;
8190Sstevel@tonic-gate 		new_entry->L_lnk_p = NULL;
8200Sstevel@tonic-gate 		new_entry->L_cnt = 1;
8210Sstevel@tonic-gate 		new_entry->L_data = Onecopy ? 0 : 1;
8220Sstevel@tonic-gate 		sublist = new_entry;
8230Sstevel@tonic-gate 	} else {
8240Sstevel@tonic-gate 		/* add to existing sub-list */
8250Sstevel@tonic-gate 		struct Lnk *ptr;
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 		sublist->L_cnt++;
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 		for (ptr = sublist;
8300Sstevel@tonic-gate 		    ptr->L_lnk_p != NULL;
8310Sstevel@tonic-gate 		    ptr = ptr->L_lnk_p) {
8320Sstevel@tonic-gate 			ptr->L_gen.g_filesz = G_p->g_filesz;
8330Sstevel@tonic-gate 		}
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 		ptr->L_gen.g_filesz = G_p->g_filesz;
8360Sstevel@tonic-gate 		ptr->L_lnk_p = new_entry;
8370Sstevel@tonic-gate 	}
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	*sublist_return = sublist;
8400Sstevel@tonic-gate 	return (new_entry);
8410Sstevel@tonic-gate }
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate /*
8440Sstevel@tonic-gate  * bfill: Read req_cnt bytes (out of filelen bytes) from the I/O buffer,
8450Sstevel@tonic-gate  * moving them to rd_buf_p.  When there are no bytes left in the I/O buffer,
8460Sstevel@tonic-gate  * Fillbuf is set and the I/O buffer is filled.  The variable dist is the
8470Sstevel@tonic-gate  * distance to lseek if an I/O error is encountered with the -k option set
8480Sstevel@tonic-gate  * (converted to a multiple of Bufsize).
8490Sstevel@tonic-gate  */
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate static int
8520Sstevel@tonic-gate bfill(void)
8530Sstevel@tonic-gate {
8540Sstevel@tonic-gate 	int i = 0, rv;
8550Sstevel@tonic-gate 	static int eof = 0;
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	if (!Dflag) {
8580Sstevel@tonic-gate 	while ((Buffr.b_end_p - Buffr.b_in_p) >= Bufsize) {
8590Sstevel@tonic-gate 		errno = 0;
8600Sstevel@tonic-gate 		if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0) {
8610Sstevel@tonic-gate 			if (((Buffr.b_end_p - Buffr.b_in_p) >= Bufsize) &&
8620Sstevel@tonic-gate 			    (Eomflag == 0)) {
8630Sstevel@tonic-gate 				Eomflag = 1;
8640Sstevel@tonic-gate 				return (1);
8650Sstevel@tonic-gate 			}
8660Sstevel@tonic-gate 			if (errno == ENOSPC) {
8670Sstevel@tonic-gate 				(void) chgreel(INPUT);
8680Sstevel@tonic-gate 				if (Hdr_type == BAR) {
8690Sstevel@tonic-gate 					skip_bar_volhdr();
8700Sstevel@tonic-gate 				}
8710Sstevel@tonic-gate 				continue;
8720Sstevel@tonic-gate 			} else if (Args & OCk) {
8730Sstevel@tonic-gate 				if (i++ > MX_SEEKS)
8740Sstevel@tonic-gate 					msg(EXT, "Cannot recover.");
8750Sstevel@tonic-gate 				if (lseek(Archive, Bufsize, SEEK_REL) < 0)
8760Sstevel@tonic-gate 					msg(EXTN, "Cannot lseek()");
8770Sstevel@tonic-gate 				Error_cnt++;
8780Sstevel@tonic-gate 				Buf_error++;
8790Sstevel@tonic-gate 				rv = 0;
8800Sstevel@tonic-gate 				continue;
8810Sstevel@tonic-gate 			} else
8820Sstevel@tonic-gate 				ioerror(INPUT);
8830Sstevel@tonic-gate 		} /* (rv = g_read(Device, Archive ... */
8840Sstevel@tonic-gate 		if (Hdr_type != BAR || rv == Bufsize) {
8850Sstevel@tonic-gate 			Buffr.b_in_p += rv;
8860Sstevel@tonic-gate 			Buffr.b_cnt += (long)rv;
8870Sstevel@tonic-gate 		}
8880Sstevel@tonic-gate 		if (rv == Bufsize) {
8890Sstevel@tonic-gate 			eof = 0;
8900Sstevel@tonic-gate 			Blocks++;
8910Sstevel@tonic-gate 		} else if (rv == 0) {
8920Sstevel@tonic-gate 			if (!eof) {
8930Sstevel@tonic-gate 				eof = 1;
8940Sstevel@tonic-gate 				break;
8950Sstevel@tonic-gate 			}
8960Sstevel@tonic-gate 			(void) chgreel(INPUT);
8970Sstevel@tonic-gate 			eof = 0;	/* reset the eof after chgreel	*/
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 			/*
9000Sstevel@tonic-gate 			 * if spans multiple volume, skip the volume header of
9010Sstevel@tonic-gate 			 * the next volume so that the file currently being
9020Sstevel@tonic-gate 			 * extracted can continue to be extracted.
9030Sstevel@tonic-gate 			 */
9040Sstevel@tonic-gate 			if (Hdr_type == BAR) {
9050Sstevel@tonic-gate 				skip_bar_volhdr();
9060Sstevel@tonic-gate 			}
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 			continue;
9090Sstevel@tonic-gate 		} else {
9100Sstevel@tonic-gate 			eof = 0;
9110Sstevel@tonic-gate 			SBlocks += (u_longlong_t)rv;
9120Sstevel@tonic-gate 		}
9130Sstevel@tonic-gate 	} /* (Buffr.b_end_p - Buffr.b_in_p) <= Bufsize */
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 	} else {			/* Dflag */
9160Sstevel@tonic-gate 		errno = 0;
9170Sstevel@tonic-gate 		if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0) {
9180Sstevel@tonic-gate 			return (-1);
9190Sstevel@tonic-gate 		} /* (rv = g_read(Device, Archive ... */
9200Sstevel@tonic-gate 		Buffr.b_in_p += rv;
9210Sstevel@tonic-gate 		Buffr.b_cnt += (long)rv;
9220Sstevel@tonic-gate 		if (rv == Bufsize) {
9230Sstevel@tonic-gate 			eof = 0;
9240Sstevel@tonic-gate 			Blocks++;
9250Sstevel@tonic-gate 		} else if (!rv) {
9260Sstevel@tonic-gate 			if (!eof) {
9270Sstevel@tonic-gate 				eof = 1;
9280Sstevel@tonic-gate 				return (rv);
9290Sstevel@tonic-gate 			}
9300Sstevel@tonic-gate 			return (-1);
9310Sstevel@tonic-gate 		} else {
9320Sstevel@tonic-gate 			eof = 0;
9330Sstevel@tonic-gate 			SBlocks += (u_longlong_t)rv;
9340Sstevel@tonic-gate 		}
9350Sstevel@tonic-gate 	}
9360Sstevel@tonic-gate 	return (rv);
9370Sstevel@tonic-gate }
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate /*
9400Sstevel@tonic-gate  * bflush: Move wr_cnt bytes from data_p into the I/O buffer.  When the
9410Sstevel@tonic-gate  * I/O buffer is full, Flushbuf is set and the buffer is written out.
9420Sstevel@tonic-gate  */
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate static void
9450Sstevel@tonic-gate bflush(void)
9460Sstevel@tonic-gate {
9470Sstevel@tonic-gate 	int rv;
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	while (Buffr.b_cnt >= Bufsize) {
9500Sstevel@tonic-gate 		errno = 0;
9510Sstevel@tonic-gate 		if ((rv = g_write(Device, Archive, Buffr.b_out_p,
9520Sstevel@tonic-gate 		    Bufsize)) < 0) {
9530Sstevel@tonic-gate 			if (errno == ENOSPC && !Dflag)
9540Sstevel@tonic-gate 				rv = chgreel(OUTPUT);
9550Sstevel@tonic-gate 			else
9560Sstevel@tonic-gate 				ioerror(OUTPUT);
9570Sstevel@tonic-gate 		}
9580Sstevel@tonic-gate 		Buffr.b_out_p += rv;
9590Sstevel@tonic-gate 		Buffr.b_cnt -= (long)rv;
9600Sstevel@tonic-gate 		if (rv == Bufsize)
9610Sstevel@tonic-gate 			Blocks++;
9620Sstevel@tonic-gate 		else if (rv > 0)
9630Sstevel@tonic-gate 			SBlocks += (u_longlong_t)rv;
9640Sstevel@tonic-gate 	}
9650Sstevel@tonic-gate 	rstbuf();
9660Sstevel@tonic-gate }
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate /*
9690Sstevel@tonic-gate  * chgreel: Determine if end-of-medium has been reached.  If it has,
9700Sstevel@tonic-gate  * close the current medium and prompt the user for the next medium.
9710Sstevel@tonic-gate  */
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate static int
9740Sstevel@tonic-gate chgreel(int dir)
9750Sstevel@tonic-gate {
9760Sstevel@tonic-gate 	int lastchar, tryagain, askagain, rv;
9770Sstevel@tonic-gate 	int tmpdev;
9780Sstevel@tonic-gate 	char str[APATH];
9790Sstevel@tonic-gate 	struct stat statb;
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	rv = 0;
9820Sstevel@tonic-gate 	if (fstat(Archive, &statb) < 0)
9830Sstevel@tonic-gate 		msg(EXTN, "Error during stat() of archive");
9840Sstevel@tonic-gate 	if ((statb.st_mode & S_IFMT) != S_IFCHR) {
9850Sstevel@tonic-gate 		if (dir == INPUT) {
9860Sstevel@tonic-gate 			msg(EXT, "%s%s\n",
9871134Sceastha 			    "Can't read input:  end of file encountered ",
9881134Sceastha 			    "prior to expected end of archive.");
9890Sstevel@tonic-gate 		}
9900Sstevel@tonic-gate 	}
9910Sstevel@tonic-gate 	msg(EPOST, "\007End of medium on \"%s\".", dir ? "output" : "input");
9920Sstevel@tonic-gate 	if (is_floppy(Archive))
9930Sstevel@tonic-gate 		(void) ioctl(Archive, FDEJECT, NULL);
9940Sstevel@tonic-gate 	if ((close(Archive) != 0) && (dir == OUTPUT))
9950Sstevel@tonic-gate 		msg(EXTN, "close error");
9960Sstevel@tonic-gate 	Archive = 0;
9970Sstevel@tonic-gate 	Volcnt++;
9980Sstevel@tonic-gate 	for (;;) {
9990Sstevel@tonic-gate 		if (Rtty_p == (FILE *)NULL)
10000Sstevel@tonic-gate 			Rtty_p = fopen(Ttyname, "r");
10010Sstevel@tonic-gate 		do { /* tryagain */
10020Sstevel@tonic-gate 			if (IOfil_p) {
10030Sstevel@tonic-gate 				do {
10040Sstevel@tonic-gate 					msg(EPOST, gettext(Eom_p), Volcnt);
10050Sstevel@tonic-gate 					if (!Rtty_p || fgets(str, sizeof (str),
10060Sstevel@tonic-gate 					    Rtty_p) == (char *)NULL)
10070Sstevel@tonic-gate 						msg(EXT, "Cannot read tty.");
10080Sstevel@tonic-gate 					askagain = 0;
10090Sstevel@tonic-gate 					switch (*str) {
10100Sstevel@tonic-gate 					case '\n':
10110Sstevel@tonic-gate 						(void) strcpy(str, IOfil_p);
10120Sstevel@tonic-gate 						break;
10130Sstevel@tonic-gate 					case 'q':
10140Sstevel@tonic-gate 						exit(EXIT_CODE);
10150Sstevel@tonic-gate 					default:
10160Sstevel@tonic-gate 						askagain = 1;
10170Sstevel@tonic-gate 					}
10180Sstevel@tonic-gate 				} while (askagain);
10190Sstevel@tonic-gate 			} else {
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 				if (Hdr_type == BAR)
10220Sstevel@tonic-gate 					Bar_vol_num++;
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 				msg(EPOST,
10250Sstevel@tonic-gate 				    "To continue, type device/file name when "
10260Sstevel@tonic-gate 				    "ready.");
10270Sstevel@tonic-gate 				if (!Rtty_p || fgets(str, sizeof (str),
10280Sstevel@tonic-gate 				    Rtty_p) == (char *)NULL)
10290Sstevel@tonic-gate 					msg(EXT, "Cannot read tty.");
10300Sstevel@tonic-gate 				lastchar = strlen(str) - 1;
10310Sstevel@tonic-gate 				if (*(str + lastchar) == '\n') /* remove '\n' */
10320Sstevel@tonic-gate 					*(str + lastchar) = '\0';
10330Sstevel@tonic-gate 				if (!*str)
10340Sstevel@tonic-gate 					exit(EXIT_CODE);
10350Sstevel@tonic-gate 			}
10360Sstevel@tonic-gate 			tryagain = 0;
10370Sstevel@tonic-gate 			if ((Archive = open(str, dir)) < 0) {
10380Sstevel@tonic-gate 				msg(ERRN, "Cannot open \"%s\"", str);
10390Sstevel@tonic-gate 				tryagain = 1;
10400Sstevel@tonic-gate 			}
10410Sstevel@tonic-gate 		} while (tryagain);
10420Sstevel@tonic-gate 		(void) g_init(&tmpdev, &Archive);
10430Sstevel@tonic-gate 		if (tmpdev != Device)
10440Sstevel@tonic-gate 			msg(EXT, "Cannot change media types in mid-stream.");
10450Sstevel@tonic-gate 		if (dir == INPUT)
10460Sstevel@tonic-gate 			break;
10470Sstevel@tonic-gate 		else { /* dir == OUTPUT */
10480Sstevel@tonic-gate 			errno = 0;
10490Sstevel@tonic-gate 			if ((rv = g_write(Device, Archive, Buffr.b_out_p,
10500Sstevel@tonic-gate 			    Bufsize)) == Bufsize)
10510Sstevel@tonic-gate 				break;
10520Sstevel@tonic-gate 			else
10530Sstevel@tonic-gate 				msg(ERR,
10540Sstevel@tonic-gate 				    "Unable to write this medium, try "
10550Sstevel@tonic-gate 				    "another.");
10560Sstevel@tonic-gate 		}
10570Sstevel@tonic-gate 	} /* ;; */
10580Sstevel@tonic-gate 	Eomflag = 0;
10590Sstevel@tonic-gate 	return (rv);
10600Sstevel@tonic-gate }
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate /*
10630Sstevel@tonic-gate  * ckname: Check filenames against user specified patterns,
10640Sstevel@tonic-gate  * and/or ask the user for new name when -r is used.
10650Sstevel@tonic-gate  */
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate static int
10680Sstevel@tonic-gate ckname(int flag)
10690Sstevel@tonic-gate {
10700Sstevel@tonic-gate 	int lastchar, len;
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	if (Hdr_type != TAR && Hdr_type != USTAR && Hdr_type != BAR) {
10730Sstevel@tonic-gate 		/* Re-visit tar size issues later */
10740Sstevel@tonic-gate 		if (G_p->g_namesz - 1 > Max_namesz) {
10750Sstevel@tonic-gate 			msg(ERR, "Name exceeds maximum length - skipped.");
10760Sstevel@tonic-gate 			return (F_SKIP);
10770Sstevel@tonic-gate 		}
10780Sstevel@tonic-gate 	}
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	if (Pat_pp && !matched())
10810Sstevel@tonic-gate 		return (F_SKIP);
10820Sstevel@tonic-gate 	if ((Args & OCr) && !Adir) { /* rename interactively */
10830Sstevel@tonic-gate 		(void) fprintf(Wtty_p, gettext("Rename \"%s%s%s\"? "),
10840Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
10850Sstevel@tonic-gate 		    G_p->g_nam_p : Renam_p,
10860Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
10870Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
10880Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
10890Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p);
10900Sstevel@tonic-gate 		(void) fflush(Wtty_p);
10910Sstevel@tonic-gate 		if (fgets(Renametmp_p, Max_namesz + 1, Rtty_p) == (char *)NULL)
10920Sstevel@tonic-gate 			msg(EXT, "Cannot read tty.");
10930Sstevel@tonic-gate 		if (feof(Rtty_p))
10940Sstevel@tonic-gate 			exit(EXIT_CODE);
10950Sstevel@tonic-gate 		lastchar = strlen(Renametmp_p) - 1;
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 		/* remove trailing '\n' */
10980Sstevel@tonic-gate 		if (*(Renametmp_p + lastchar) == '\n')
10990Sstevel@tonic-gate 			*(Renametmp_p + lastchar) = '\0';
11000Sstevel@tonic-gate 		if (*Renametmp_p == '\0') {
11010Sstevel@tonic-gate 			msg(POST, "%s%s%s Skipped.",
11020Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
11030Sstevel@tonic-gate 			    G_p->g_nam_p : G_p->g_attrfnam_p,
11040Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
11050Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
11060Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
11070Sstevel@tonic-gate 			    "" : G_p->g_attrnam_p);
11080Sstevel@tonic-gate 			*G_p->g_nam_p = '\0';
11090Sstevel@tonic-gate 			return (F_SKIP);
11100Sstevel@tonic-gate 		} else if (strcmp(Renametmp_p, ".") != 0) {
11110Sstevel@tonic-gate 			len = strlen(Renametmp_p);
11120Sstevel@tonic-gate 			if (len > (int)strlen(G_p->g_nam_p)) {
11130Sstevel@tonic-gate 				if ((G_p->g_nam_p != &nambuf[0]) &&
11140Sstevel@tonic-gate 				    (G_p->g_nam_p != &fullnam[0]))
11150Sstevel@tonic-gate 					free(G_p->g_nam_p);
11160Sstevel@tonic-gate 				G_p->g_nam_p = e_zalloc(E_EXIT, len + 1);
11170Sstevel@tonic-gate 			}
11180Sstevel@tonic-gate 			if (G_p->g_attrnam_p != (char *)NULL) {
11190Sstevel@tonic-gate 				free(G_p->g_attrnam_p);
11200Sstevel@tonic-gate 				G_p->g_attrnam_p = e_strdup(E_EXIT,
11210Sstevel@tonic-gate 				    Renametmp_p);
11220Sstevel@tonic-gate 				(void) strcpy(G_p->g_nam_p, Renam_p);
11230Sstevel@tonic-gate 			} else {
11240Sstevel@tonic-gate 				(void) strcpy(Renam_p, Renametmp_p);
11250Sstevel@tonic-gate 				(void) strcpy(G_p->g_nam_p, Renametmp_p);
11260Sstevel@tonic-gate 			}
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 		}
11290Sstevel@tonic-gate 	}
11300Sstevel@tonic-gate 	if (flag != 0 || Onecopy == 0) {
11310Sstevel@tonic-gate 		VERBOSE((Args & OCt), G_p->g_nam_p);
11320Sstevel@tonic-gate 	}
11330Sstevel@tonic-gate 	if (Args & OCt)
11340Sstevel@tonic-gate 		return (F_SKIP);
11350Sstevel@tonic-gate 	return (F_EXTR);
11360Sstevel@tonic-gate }
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate /*
11390Sstevel@tonic-gate  * ckopts: Check the validity of all command line options.
11400Sstevel@tonic-gate  */
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate static void
11430Sstevel@tonic-gate ckopts(long mask)
11440Sstevel@tonic-gate {
11450Sstevel@tonic-gate 	int oflag;
11460Sstevel@tonic-gate 	char *t_p;
11470Sstevel@tonic-gate 	long errmsk;
11481134Sceastha 	uid_t	Euid = geteuid();	/* Effective uid of invoker */
11491134Sceastha #ifdef SOLARIS_PRIVS
11501134Sceastha 	priv_set_t *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 {
12991134Sceastha 		privileged = (priv_isfullset(privset) == B_TRUE);
13001134Sceastha 	}
13011134Sceastha 	if (privset != NULL) {
13021134Sceastha 		priv_freeset(privset);
13031134Sceastha 	}
13041134Sceastha #else
13051134Sceastha 	privileged = (Euid == 0);
13061134Sceastha #endif	/* SOLARIS_PRIVS */
13071134Sceastha 
13080Sstevel@tonic-gate 	if (mask & OCR) {
13090Sstevel@tonic-gate 		if ((Rpw_p = getpwnam(Own_p)) == (struct passwd *)NULL) {
13100Sstevel@tonic-gate 			msg(ERR, "\"%s\" is not a valid user id", Own_p);
13111134Sceastha 		} else if ((Euid != Rpw_p->pw_uid) && !privileged) {
13121134Sceastha 			msg(ERR, "R option only valid for super-user or "
13131134Sceastha 			    "id matches login id of user executing cpio");
13140Sstevel@tonic-gate 		}
13150Sstevel@tonic-gate 	}
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 	if ((mask & OCo) && !(mask & OCO)) {
13180Sstevel@tonic-gate 		Out_p = stderr;
13190Sstevel@tonic-gate 	}
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate 	if ((mask & OCp) && ((mask & (OCB|OCC)) == 0)) {
13220Sstevel@tonic-gate 		/*
13230Sstevel@tonic-gate 		 * We are in pass mode with no block size specified.  Use the
13240Sstevel@tonic-gate 		 * larger of the native page size and 8192.
13250Sstevel@tonic-gate 		 */
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 		Bufsize = (PageSize > 8192) ? PageSize : 8192;
13280Sstevel@tonic-gate 	}
13290Sstevel@tonic-gate }
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate /*
13320Sstevel@tonic-gate  * cksum: Calculate the simple checksum of a file (CRC) or header
13330Sstevel@tonic-gate  * (TARTYP (TAR and USTAR)).  For -o and the CRC header, the file is opened and
13340Sstevel@tonic-gate  * the checksum is calculated.  For -i and the CRC header, the checksum
13350Sstevel@tonic-gate  * is calculated as each block is transferred from the archive I/O buffer
13360Sstevel@tonic-gate  * to the file system I/O buffer.  The TARTYP (TAR and USTAR) headers calculate
13370Sstevel@tonic-gate  * the simple checksum of the header (with the checksum field of the
13380Sstevel@tonic-gate  * header initialized to all spaces (\040).
13390Sstevel@tonic-gate  */
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate static long
13420Sstevel@tonic-gate cksum(char hdr, int byt_cnt, int *err)
13430Sstevel@tonic-gate {
13440Sstevel@tonic-gate 	char *crc_p, *end_p;
13450Sstevel@tonic-gate 	int cnt;
13460Sstevel@tonic-gate 	long checksum = 0L, have;
13470Sstevel@tonic-gate 	off_t lcnt;
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 	if (err != NULL)
13500Sstevel@tonic-gate 		*err = 0;
13510Sstevel@tonic-gate 	switch (hdr) {
13520Sstevel@tonic-gate 	case CRC:
13530Sstevel@tonic-gate 		if (Args & OCi) { /* do running checksum */
13540Sstevel@tonic-gate 			end_p = Buffr.b_out_p + byt_cnt;
13550Sstevel@tonic-gate 			for (crc_p = Buffr.b_out_p; crc_p < end_p; crc_p++)
13560Sstevel@tonic-gate 				checksum += (long)*crc_p;
13570Sstevel@tonic-gate 			break;
13580Sstevel@tonic-gate 		}
13590Sstevel@tonic-gate 		/* OCo - do checksum of file */
13600Sstevel@tonic-gate 		lcnt = G_p->g_filesz;
13610Sstevel@tonic-gate 
13620Sstevel@tonic-gate 		while (lcnt > 0) {
13630Sstevel@tonic-gate 			have = (lcnt < Bufsize) ? lcnt : Bufsize;
13640Sstevel@tonic-gate 			errno = 0;
13650Sstevel@tonic-gate 			if (read(Ifile, Buf_p, have) != have) {
13660Sstevel@tonic-gate 				msg(ERR, "Error computing checksum.");
13670Sstevel@tonic-gate 				if (err != NULL)
13680Sstevel@tonic-gate 					*err = 1;
13690Sstevel@tonic-gate 				break;
13700Sstevel@tonic-gate 			}
13710Sstevel@tonic-gate 			end_p = Buf_p + have;
13720Sstevel@tonic-gate 			for (crc_p = Buf_p; crc_p < end_p; crc_p++)
13730Sstevel@tonic-gate 				checksum += (long)*crc_p;
13740Sstevel@tonic-gate 			lcnt -= have;
13750Sstevel@tonic-gate 		}
13760Sstevel@tonic-gate 		if (lseek(Ifile, (off_t)0, SEEK_ABS) < 0)
13770Sstevel@tonic-gate 			msg(ERRN, "Cannot reset file after checksum");
13780Sstevel@tonic-gate 		break;
13790Sstevel@tonic-gate 	case TARTYP: /* TAR and USTAR */
13800Sstevel@tonic-gate 		crc_p = Thdr_p->tbuf.t_cksum;
13810Sstevel@tonic-gate 		for (cnt = 0; cnt < TCRCLEN; cnt++) {
13820Sstevel@tonic-gate 			*crc_p = '\040';
13830Sstevel@tonic-gate 			crc_p++;
13840Sstevel@tonic-gate 		}
13850Sstevel@tonic-gate 		crc_p = (char *)Thdr_p;
13860Sstevel@tonic-gate 		for (cnt = 0; cnt < TARSZ; cnt++) {
13870Sstevel@tonic-gate 			/*
13880Sstevel@tonic-gate 			 * tar uses unsigned checksum, so we must use unsigned
13890Sstevel@tonic-gate 			 * here in order to be able to read tar archives.
13900Sstevel@tonic-gate 			 */
13910Sstevel@tonic-gate 			checksum += (long)((unsigned char)(*crc_p));
13920Sstevel@tonic-gate 			crc_p++;
13930Sstevel@tonic-gate 		}
13940Sstevel@tonic-gate 		break;
13950Sstevel@tonic-gate 	default:
13960Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
13970Sstevel@tonic-gate 	} /* hdr */
13980Sstevel@tonic-gate 	return (checksum);
13990Sstevel@tonic-gate }
14000Sstevel@tonic-gate 
14010Sstevel@tonic-gate /*
14020Sstevel@tonic-gate  * creat_hdr: Fill in the generic header structure with the specific
14030Sstevel@tonic-gate  *            header information based on the value of Hdr_type.
14040Sstevel@tonic-gate  *
14050Sstevel@tonic-gate  *            return (1) if this process was successful, and (0) otherwise.
14060Sstevel@tonic-gate  */
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate static int
14090Sstevel@tonic-gate creat_hdr(void)
14100Sstevel@tonic-gate {
14110Sstevel@tonic-gate 	ushort_t ftype;
14120Sstevel@tonic-gate 	int fullnamesize;
14130Sstevel@tonic-gate 	dev_t dev;
14140Sstevel@tonic-gate 	ino_t ino;
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 	ftype = SrcSt.st_mode & Ftype;
14170Sstevel@tonic-gate 	Adir = (ftype == S_IFDIR);
14180Sstevel@tonic-gate 	Aspec = (ftype == S_IFBLK || ftype == S_IFCHR || ftype == S_IFIFO);
14190Sstevel@tonic-gate 	switch (Hdr_type) {
14200Sstevel@tonic-gate 		case BIN:
14210Sstevel@tonic-gate 			Gen.g_magic = CMN_BIN;
14220Sstevel@tonic-gate 			break;
14230Sstevel@tonic-gate 		case CHR:
14240Sstevel@tonic-gate 			Gen.g_magic = CMN_BIN;
14250Sstevel@tonic-gate 			break;
14260Sstevel@tonic-gate 		case ASC:
14270Sstevel@tonic-gate 			Gen.g_magic = CMN_ASC;
14280Sstevel@tonic-gate 			break;
14290Sstevel@tonic-gate 		case CRC:
14300Sstevel@tonic-gate 			Gen.g_magic = CMN_CRC;
14310Sstevel@tonic-gate 			break;
14320Sstevel@tonic-gate 		case USTAR:
14330Sstevel@tonic-gate 			/*
14340Sstevel@tonic-gate 			 * If the length of the full name is greater than 256,
14350Sstevel@tonic-gate 			 * print out a message and return.
14360Sstevel@tonic-gate 			 */
14370Sstevel@tonic-gate 			if ((fullnamesize = strlen(Gen.g_nam_p)) > MAXNAM) {
14380Sstevel@tonic-gate 				msg(ERR,
14390Sstevel@tonic-gate 				    "%s: file name too long", Gen.g_nam_p);
14400Sstevel@tonic-gate 				return (0);
14410Sstevel@tonic-gate 			} else if (fullnamesize > NAMSIZ) {
14420Sstevel@tonic-gate 				/*
14430Sstevel@tonic-gate 				 * The length of the full name is greater than
14440Sstevel@tonic-gate 				 * 100, so we must split the filename from the
14450Sstevel@tonic-gate 				 * path
14460Sstevel@tonic-gate 				 */
14470Sstevel@tonic-gate 				char namebuff[NAMSIZ+1];
14480Sstevel@tonic-gate 				char prebuff[PRESIZ+1];
14490Sstevel@tonic-gate 				char *lastslash;
14500Sstevel@tonic-gate 				int presize, namesize;
14510Sstevel@tonic-gate 
14520Sstevel@tonic-gate 				(void) memset(namebuff, '\0',
14530Sstevel@tonic-gate 				    sizeof (namebuff));
14540Sstevel@tonic-gate 				(void) memset(prebuff, '\0', sizeof (prebuff));
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 				lastslash = strrchr(Gen.g_nam_p, '/');
14570Sstevel@tonic-gate 
14580Sstevel@tonic-gate 				if (lastslash != NULL) {
14590Sstevel@tonic-gate 					namesize = strlen(++lastslash);
14600Sstevel@tonic-gate 					presize = fullnamesize - namesize - 1;
14610Sstevel@tonic-gate 				} else {
14620Sstevel@tonic-gate 					namesize = fullnamesize;
14630Sstevel@tonic-gate 					lastslash = Gen.g_nam_p;
14640Sstevel@tonic-gate 					presize = 0;
14650Sstevel@tonic-gate 				}
14660Sstevel@tonic-gate 
14670Sstevel@tonic-gate 				/*
14680Sstevel@tonic-gate 				 * If the filename is greater than 100 we can't
14690Sstevel@tonic-gate 				 * archive the file
14700Sstevel@tonic-gate 				 */
14710Sstevel@tonic-gate 				if (namesize > NAMSIZ) {
14720Sstevel@tonic-gate 					msg(ERR,
14730Sstevel@tonic-gate 					    "%s: filename is greater than %d",
14740Sstevel@tonic-gate 					    lastslash, NAMSIZ);
14750Sstevel@tonic-gate 					return (0);
14760Sstevel@tonic-gate 				}
14770Sstevel@tonic-gate 				(void) strncpy(&namebuff[0], lastslash,
14780Sstevel@tonic-gate 				    namesize);
14790Sstevel@tonic-gate 				/*
14800Sstevel@tonic-gate 				 * If the prefix is greater than 155 we can't
14810Sstevel@tonic-gate 				 * archive the file.
14820Sstevel@tonic-gate 				 */
14830Sstevel@tonic-gate 				if (presize > PRESIZ) {
14840Sstevel@tonic-gate 					msg(ERR,
14850Sstevel@tonic-gate 					    "%s: prefix is greater than %d",
14860Sstevel@tonic-gate 					    Gen.g_nam_p, PRESIZ);
14870Sstevel@tonic-gate 					return (0);
14880Sstevel@tonic-gate 				}
14890Sstevel@tonic-gate 				(void) strncpy(&prebuff[0], Gen.g_nam_p,
14900Sstevel@tonic-gate 				    presize);
14910Sstevel@tonic-gate 
14920Sstevel@tonic-gate 				Gen.g_tname = e_zalloc(E_EXIT, namesize + 1);
14930Sstevel@tonic-gate 				(void) strcpy(Gen.g_tname, namebuff);
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate 				Gen.g_prefix = e_zalloc(E_EXIT, presize + 1);
14960Sstevel@tonic-gate 				(void) strcpy(Gen.g_prefix, prebuff);
14970Sstevel@tonic-gate 			} else {
14980Sstevel@tonic-gate 				Gen.g_tname = Gen.g_nam_p;
14990Sstevel@tonic-gate 			}
15000Sstevel@tonic-gate 			(void) strcpy(Gen.g_tmagic, "ustar");
15010Sstevel@tonic-gate 			(void) strcpy(Gen.g_version, "00");
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate 			dpasswd = getpwuid(SrcSt.st_uid);
15040Sstevel@tonic-gate 			if (dpasswd == (struct passwd *)NULL) {
15050Sstevel@tonic-gate 				msg(EPOST,
15060Sstevel@tonic-gate 				    "cpio: could not get passwd information "
15070Sstevel@tonic-gate 				    "for %s%s%s",
15080Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
15090Sstevel@tonic-gate 				    Gen.g_nam_p : Gen.g_attrfnam_p,
15100Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
15110Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
15120Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
15130Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
15140Sstevel@tonic-gate 				/* make name null string */
15150Sstevel@tonic-gate 				Gen.g_uname[0] = '\0';
15160Sstevel@tonic-gate 			} else {
15170Sstevel@tonic-gate 				(void) strncpy(&Gen.g_uname[0],
15180Sstevel@tonic-gate 				    dpasswd->pw_name, 32);
15190Sstevel@tonic-gate 			}
15200Sstevel@tonic-gate 			dgroup = getgrgid(SrcSt.st_gid);
15210Sstevel@tonic-gate 			if (dgroup == (struct group *)NULL) {
15220Sstevel@tonic-gate 				msg(EPOST,
15230Sstevel@tonic-gate 				    "cpio: could not get group information "
15240Sstevel@tonic-gate 				    "for %s%s%S",
15250Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
15260Sstevel@tonic-gate 				    Gen.g_nam_p : Gen.g_attrfnam_p,
15270Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
15280Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
15290Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
15300Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
15310Sstevel@tonic-gate 				/* make name null string */
15320Sstevel@tonic-gate 				Gen.g_gname[0] = '\0';
15330Sstevel@tonic-gate 			} else {
15340Sstevel@tonic-gate 				(void) strncpy(&Gen.g_gname[0],
15350Sstevel@tonic-gate 				    dgroup->gr_name, 32);
15360Sstevel@tonic-gate 			}
15370Sstevel@tonic-gate 			Gen.g_typeflag = tartype(ftype);
15380Sstevel@tonic-gate 			/* FALLTHROUGH */
15390Sstevel@tonic-gate 		case TAR:
15400Sstevel@tonic-gate 			(void) memset(T_lname, '\0', sizeof (T_lname));
15410Sstevel@tonic-gate 			break;
15420Sstevel@tonic-gate 		default:
15430Sstevel@tonic-gate 			msg(EXT, "Impossible header type.");
15440Sstevel@tonic-gate 	}
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate 	dev = SrcSt.st_dev;
15470Sstevel@tonic-gate 	ino = SrcSt.st_ino;
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 	if (Use_old_stat) {
15500Sstevel@tonic-gate 		SrcSt = *OldSt;
15510Sstevel@tonic-gate 	}
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 	Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
15540Sstevel@tonic-gate 	Gen.g_uid = SrcSt.st_uid;
15550Sstevel@tonic-gate 	Gen.g_gid = SrcSt.st_gid;
15560Sstevel@tonic-gate 	Gen.g_dev = SrcSt.st_dev;
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 	if (Use_old_stat) {
15590Sstevel@tonic-gate 		/* -Hodc */
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 		sl_info_t *p = sl_search(dev, ino);
15620Sstevel@tonic-gate 		Gen.g_ino = p ? p->sl_ino2 : -1;
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate 		if (Gen.g_ino == (ulong_t)-1) {
15650Sstevel@tonic-gate 			msg(ERR, "%s%s%s: cannot be archived - inode too big "
15660Sstevel@tonic-gate 			    "for -Hodc format",
15670Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
15680Sstevel@tonic-gate 			    Gen.g_nam_p : Gen.g_attrfnam_p,
15690Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
15700Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
15710Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
15720Sstevel@tonic-gate 			    "" : Gen.g_attrnam_p);
15730Sstevel@tonic-gate 			return (0);
15740Sstevel@tonic-gate 		}
15750Sstevel@tonic-gate 	} else {
15760Sstevel@tonic-gate 		Gen.g_ino = SrcSt.st_ino;
15770Sstevel@tonic-gate 	}
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate 	Gen.g_mode = SrcSt.st_mode;
15800Sstevel@tonic-gate 	Gen.g_mtime = SrcSt.st_mtime;
15810Sstevel@tonic-gate 	Gen.g_nlink = (Adir) ? SrcSt.st_nlink
15820Sstevel@tonic-gate 	    : sl_numlinks(dev, ino);
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 	if (ftype == S_IFREG || ftype == S_IFLNK)
15850Sstevel@tonic-gate 		Gen.g_filesz = (off_t)SrcSt.st_size;
15860Sstevel@tonic-gate 	else
15870Sstevel@tonic-gate 		Gen.g_filesz = (off_t)0;
15880Sstevel@tonic-gate 	Gen.g_rdev = SrcSt.st_rdev;
15890Sstevel@tonic-gate 	return (1);
15900Sstevel@tonic-gate }
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate /*
15930Sstevel@tonic-gate  * creat_lnk: Create a link from the existing name1_p to name2_p.
15940Sstevel@tonic-gate  */
15950Sstevel@tonic-gate 
15960Sstevel@tonic-gate static
15970Sstevel@tonic-gate int
15980Sstevel@tonic-gate creat_lnk(int dirfd, char *name1_p, char *name2_p)
15990Sstevel@tonic-gate {
16000Sstevel@tonic-gate 	int cnt = 0;
16010Sstevel@tonic-gate 
16020Sstevel@tonic-gate 	do {
16030Sstevel@tonic-gate 		errno = 0;
16040Sstevel@tonic-gate 		if (!link(name1_p, name2_p)) {
16050Sstevel@tonic-gate 			if (aclp != NULL) {
1606789Sahrens 				acl_free(aclp);
16070Sstevel@tonic-gate 				aclp = NULL;
1608789Sahrens 				acl_is_set = 0;
16090Sstevel@tonic-gate 			}
16100Sstevel@tonic-gate 			cnt = 0;
16110Sstevel@tonic-gate 			break;
16121134Sceastha 		} else if ((errno == EEXIST) && (cnt == 0)) {
16131134Sceastha 			struct stat lsb1;
16141134Sceastha 			struct stat lsb2;
16151134Sceastha 
16161134Sceastha 			/*
16171134Sceastha 			 * Check to see if we are trying to link this
16181134Sceastha 			 * file to itself.  If so, count the effort as
16191134Sceastha 			 * successful.  If the two files are different,
16201134Sceastha 			 * or if either lstat is unsuccessful, proceed
16211134Sceastha 			 * as we would have otherwise; the appropriate
16221134Sceastha 			 * error will be reported subsequently.
16231134Sceastha 			 */
16241134Sceastha 
16251134Sceastha 			if (lstat(name1_p, &lsb1) != 0) {
16261134Sceastha 				msg(ERR, "Cannot lstat source file %s",
16271134Sceastha 				    name1_p);
16281134Sceastha 			} else {
16291134Sceastha 				if (lstat(name2_p, &lsb2) != 0) {
16301134Sceastha 					msg(ERR, "Cannot lstat "
16311134Sceastha 					    "destination file %s", name2_p);
16320Sstevel@tonic-gate 				} else {
16331134Sceastha 					if (lsb1.st_dev == lsb2.st_dev &&
16341134Sceastha 					    lsb1.st_ino == lsb2.st_ino) {
16351134Sceastha 						VERBOSE((Args & (OCv | OCV)),
16360Sstevel@tonic-gate 						    name2_p);
16371134Sceastha 						return (0);
16380Sstevel@tonic-gate 					}
16390Sstevel@tonic-gate 				}
16400Sstevel@tonic-gate 			}
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate 			if (!(Args & OCu) && G_p->g_mtime <= DesSt.st_mtime)
16431134Sceastha 				msg(ERR, "Existing \"%s\" same age or newer",
16440Sstevel@tonic-gate 				    name2_p);
16450Sstevel@tonic-gate 			else if (unlinkat(dirfd, get_component(name2_p), 0) < 0)
16460Sstevel@tonic-gate 				msg(ERRN, "Error cannot unlink \"%s\"",
16470Sstevel@tonic-gate 				    name2_p);
16480Sstevel@tonic-gate 		}
16490Sstevel@tonic-gate 		cnt++;
16500Sstevel@tonic-gate 	} while ((cnt < 2) && missdir(name2_p) == 0);
16510Sstevel@tonic-gate 	if (!cnt) {
16520Sstevel@tonic-gate 		char *newname;
16530Sstevel@tonic-gate 		char *fromname;
16540Sstevel@tonic-gate 		char *attrname;
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate 		newname = name2_p;
16570Sstevel@tonic-gate 		fromname = name1_p;
16580Sstevel@tonic-gate 		attrname = Gen.g_attrnam_p;
16590Sstevel@tonic-gate 		if (attrname) {
16600Sstevel@tonic-gate 			if (Args & OCp) {
16610Sstevel@tonic-gate 				newname = fromname = Fullnam_p;
16620Sstevel@tonic-gate 			} else {
16630Sstevel@tonic-gate 				newname = Gen.g_attrfnam_p;
16640Sstevel@tonic-gate 			}
16650Sstevel@tonic-gate 		}
16660Sstevel@tonic-gate 		if (Args & OCv) {
16670Sstevel@tonic-gate 			(void) fprintf(Err_p,
16680Sstevel@tonic-gate 			    gettext("%s%s%s linked to %s%s%s\n"), newname,
16690Sstevel@tonic-gate 			    (attrname == (char *)NULL) ?
16700Sstevel@tonic-gate 			    "" : gettext(" attribute "),
16710Sstevel@tonic-gate 			    (attrname == (char *)NULL) ?
16720Sstevel@tonic-gate 			    "" : attrname, fromname,
16730Sstevel@tonic-gate 			    (attrname == (char *)NULL) ?
16740Sstevel@tonic-gate 			    "" : gettext(" attribute "),
16750Sstevel@tonic-gate 			    (attrname == (char *)NULL) ?
16760Sstevel@tonic-gate 			    "" : name1_p);
16770Sstevel@tonic-gate 		}
16780Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), newname);
16790Sstevel@tonic-gate 	} else if (cnt == 1)
16800Sstevel@tonic-gate 		msg(ERRN,
16810Sstevel@tonic-gate 		    "Unable to create directory for \"%s\"", name2_p);
16820Sstevel@tonic-gate 	else if (cnt == 2)
16830Sstevel@tonic-gate 		msg(ERRN,
16840Sstevel@tonic-gate 		    "Cannot link \"%s\" and \"%s\"", name1_p, name2_p);
16850Sstevel@tonic-gate 	return (cnt);
16860Sstevel@tonic-gate }
16870Sstevel@tonic-gate 
16880Sstevel@tonic-gate /*
16890Sstevel@tonic-gate  * creat_spec:
16900Sstevel@tonic-gate  *   Create one of the following:
16910Sstevel@tonic-gate  *       directory
16920Sstevel@tonic-gate  *       character special file
16930Sstevel@tonic-gate  *       block special file
16940Sstevel@tonic-gate  *       fifo
16950Sstevel@tonic-gate  */
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate static int
16980Sstevel@tonic-gate creat_spec(int dirfd)
16990Sstevel@tonic-gate {
17000Sstevel@tonic-gate 	char *nam_p;
17010Sstevel@tonic-gate 	int cnt, result, rv = 0;
17020Sstevel@tonic-gate 	char *curdir;
17030Sstevel@tonic-gate 	char *lastslash;
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate 	Do_rename = 0;	/* creat_tmp() may reset this */
17060Sstevel@tonic-gate 
17070Sstevel@tonic-gate 	if (Args & OCp) {
17080Sstevel@tonic-gate 		nam_p = Fullnam_p;
17090Sstevel@tonic-gate 	} else {
17100Sstevel@tonic-gate 		nam_p = G_p->g_nam_p;
17110Sstevel@tonic-gate 	}
17120Sstevel@tonic-gate 
17130Sstevel@tonic-gate 	/*
17140Sstevel@tonic-gate 	 * Is this the extraction of the hidden attribute directory?
17150Sstevel@tonic-gate 	 * If so then just set the mode/times correctly, and return.
17160Sstevel@tonic-gate 	 */
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate 	if (Hiddendir) {
17191134Sceastha 		if (Args & OCR) {
17201134Sceastha 			if (fchownat(dirfd, ".", Rpw_p->pw_uid,
17211134Sceastha 			    Rpw_p->pw_gid, 0) != 0) {
17221134Sceastha 				msg(ERRN,
17231134Sceastha 				    "Cannot chown() \"attribute directory of "
17241134Sceastha 				    "file %s\"", G_p->g_attrfnam_p);
17251134Sceastha 			}
17261134Sceastha 		} else if ((fchownat(dirfd, ".", G_p->g_uid,
17271134Sceastha 		    G_p->g_gid, 0) != 0) && privileged) {
17280Sstevel@tonic-gate 			msg(ERRN,
17290Sstevel@tonic-gate 			    "Cannot chown() \"attribute directory of "
17300Sstevel@tonic-gate 			    "file %s\"", G_p->g_attrfnam_p);
17310Sstevel@tonic-gate 		}
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate 		if (fchmod(dirfd, G_p->g_mode) != 0) {
17340Sstevel@tonic-gate 			msg(ERRN,
17350Sstevel@tonic-gate 			    "Cannot chmod() \"attribute directory of "
17360Sstevel@tonic-gate 			    "file %s\"", G_p->g_attrfnam_p);
17370Sstevel@tonic-gate 		}
17380Sstevel@tonic-gate 
1739789Sahrens 		acl_is_set = 0;
17400Sstevel@tonic-gate 		if (Pflag && aclp != NULL) {
1741789Sahrens 			if (facl_set(dirfd, aclp) < 0) {
17420Sstevel@tonic-gate 				msg(ERRN,
17430Sstevel@tonic-gate 				    "failed to set acl on attribute"
17440Sstevel@tonic-gate 				    " directory of %s ", G_p->g_attrfnam_p);
17450Sstevel@tonic-gate 			} else {
1746789Sahrens 				acl_is_set = 1;
1747789Sahrens 			}
1748789Sahrens 			acl_free(aclp);
17490Sstevel@tonic-gate 			aclp = NULL;
17500Sstevel@tonic-gate 		}
17510Sstevel@tonic-gate 
17520Sstevel@tonic-gate 		return (1);
17530Sstevel@tonic-gate 	}
17540Sstevel@tonic-gate 
17550Sstevel@tonic-gate 	result = stat(nam_p, &DesSt);
17560Sstevel@tonic-gate 
17570Sstevel@tonic-gate 	if (ustar_dir() || Adir) {
17580Sstevel@tonic-gate 		/*
17590Sstevel@tonic-gate 		 *  The archive file is a directory.
17600Sstevel@tonic-gate 		 *  Skip "." and ".."
17610Sstevel@tonic-gate 		 */
17620Sstevel@tonic-gate 
17630Sstevel@tonic-gate 		curdir = strrchr(nam_p, '.');
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate 		if (curdir != NULL && curdir[1] == NULL) {
17660Sstevel@tonic-gate 			lastslash = strrchr(nam_p, '/');
17670Sstevel@tonic-gate 
17680Sstevel@tonic-gate 			if (lastslash != NULL) {
17690Sstevel@tonic-gate 				lastslash++;
17700Sstevel@tonic-gate 			} else {
17710Sstevel@tonic-gate 				lastslash = nam_p;
17720Sstevel@tonic-gate 			}
17730Sstevel@tonic-gate 
17740Sstevel@tonic-gate 			if (!(strcmp(lastslash, ".")) ||
17750Sstevel@tonic-gate 			    !(strcmp(lastslash, ".."))) {
17760Sstevel@tonic-gate 				return (1);
17770Sstevel@tonic-gate 			}
17780Sstevel@tonic-gate 		}
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate 		if (result == 0) {
17810Sstevel@tonic-gate 			/* A file by the same name exists. */
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate 			/* Take care of ACLs */
1784789Sahrens 			acl_is_set = 0;
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate 			if (Pflag && aclp != NULL) {
1787789Sahrens 				if (acl_set(nam_p, aclp) < 0) {
17880Sstevel@tonic-gate 					msg(ERRN,
17890Sstevel@tonic-gate 					    "\"%s\": failed to set acl",
17900Sstevel@tonic-gate 					    nam_p);
17910Sstevel@tonic-gate 				} else {
1792789Sahrens 					acl_is_set = 1;
17930Sstevel@tonic-gate 				}
17940Sstevel@tonic-gate 
1795789Sahrens 				acl_free(aclp);
17960Sstevel@tonic-gate 				aclp = NULL;
17970Sstevel@tonic-gate 			}
17980Sstevel@tonic-gate 			if (Args & OCd) {
17990Sstevel@tonic-gate 				/*
18000Sstevel@tonic-gate 				 * We are creating directories.  Keep the
18010Sstevel@tonic-gate 				 * existing file.
18020Sstevel@tonic-gate 				 */
18030Sstevel@tonic-gate 
18040Sstevel@tonic-gate 				rstfiles(U_KEEP, dirfd);
18050Sstevel@tonic-gate 			}
18060Sstevel@tonic-gate 
18070Sstevel@tonic-gate 			/* Report success. */
18080Sstevel@tonic-gate 
18090Sstevel@tonic-gate 			return (1);
18100Sstevel@tonic-gate 		}
18110Sstevel@tonic-gate 	} else {
18120Sstevel@tonic-gate 		/* The archive file is not a directory. */
18130Sstevel@tonic-gate 
18140Sstevel@tonic-gate 		if (result == 0) {
18150Sstevel@tonic-gate 			/*
18160Sstevel@tonic-gate 			 * A file by the same name exists.  Move it to a
18170Sstevel@tonic-gate 			 * temporary file.
18180Sstevel@tonic-gate 			 */
18190Sstevel@tonic-gate 
18200Sstevel@tonic-gate 			if (creat_tmp(nam_p) < 0) {
18210Sstevel@tonic-gate 				/*
18220Sstevel@tonic-gate 				 * We weren't able to create the temp file.
18230Sstevel@tonic-gate 				 * Report failure.
18240Sstevel@tonic-gate 				 */
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate 				return (0);
18270Sstevel@tonic-gate 			}
18280Sstevel@tonic-gate 		}
18290Sstevel@tonic-gate 	}
18300Sstevel@tonic-gate 
18310Sstevel@tonic-gate 	/*
18320Sstevel@tonic-gate 	 * This pile tries to create the file directly, and, if there is a
18330Sstevel@tonic-gate 	 * problem, creates missing directories, and then tries to create the
18340Sstevel@tonic-gate 	 * file again.  Two strikes and you're out.
18350Sstevel@tonic-gate 	 */
18360Sstevel@tonic-gate 
18370Sstevel@tonic-gate 	cnt = 0;
18380Sstevel@tonic-gate 
18390Sstevel@tonic-gate 	do {
18400Sstevel@tonic-gate 		if (ustar_dir() || Adir) {
18410Sstevel@tonic-gate 			/* The archive file is a directory. */
18420Sstevel@tonic-gate 
18430Sstevel@tonic-gate 			result = mkdir(nam_p, G_p->g_mode);
18440Sstevel@tonic-gate 		} else if (ustar_spec() || Aspec) {
18450Sstevel@tonic-gate 			/*
18460Sstevel@tonic-gate 			 * The archive file is block special,
18470Sstevel@tonic-gate 			 * char special or a fifo.
18480Sstevel@tonic-gate 			 */
18490Sstevel@tonic-gate 
18500Sstevel@tonic-gate 			result = mknod(nam_p, (int)G_p->g_mode,
18510Sstevel@tonic-gate 			    (int)G_p->g_rdev);
18520Sstevel@tonic-gate 		}
18530Sstevel@tonic-gate 
18540Sstevel@tonic-gate 		if (result >= 0) {
18550Sstevel@tonic-gate 			/*
18560Sstevel@tonic-gate 			 * The file creation succeeded.  Take care of the ACLs.
18570Sstevel@tonic-gate 			 */
18580Sstevel@tonic-gate 
1859789Sahrens 			acl_is_set = 0;
18600Sstevel@tonic-gate 
18610Sstevel@tonic-gate 			if (Pflag && aclp != NULL) {
1862789Sahrens 				if (acl_set(nam_p, aclp) < 0) {
18630Sstevel@tonic-gate 					msg(ERRN,
18640Sstevel@tonic-gate 					    "\"%s\": failed to set acl", nam_p);
18650Sstevel@tonic-gate 				} else {
1866789Sahrens 					acl_is_set = 1;
18670Sstevel@tonic-gate 				}
18680Sstevel@tonic-gate 
1869789Sahrens 				acl_free(aclp);
18700Sstevel@tonic-gate 				aclp = NULL;
18710Sstevel@tonic-gate 			}
18720Sstevel@tonic-gate 
18730Sstevel@tonic-gate 			cnt = 0;
18740Sstevel@tonic-gate 			break;
18750Sstevel@tonic-gate 		}
18760Sstevel@tonic-gate 
18770Sstevel@tonic-gate 		cnt++;
18780Sstevel@tonic-gate 	} while (cnt < 2 && missdir(nam_p) == 0);
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate 	switch (cnt) {
18810Sstevel@tonic-gate 	case 0:
18820Sstevel@tonic-gate 		rv = 1;
18830Sstevel@tonic-gate 		rstfiles(U_OVER, dirfd);
18840Sstevel@tonic-gate 		break;
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate 	case 1:
18870Sstevel@tonic-gate 		msg(ERRN,
18880Sstevel@tonic-gate 		    "Cannot create directory for \"%s\"", nam_p);
18890Sstevel@tonic-gate 
18900Sstevel@tonic-gate 		if (*Over_p == '\0') {
18910Sstevel@tonic-gate 			rstfiles(U_KEEP, dirfd);
18920Sstevel@tonic-gate 		}
18930Sstevel@tonic-gate 
18940Sstevel@tonic-gate 		break;
18950Sstevel@tonic-gate 
18960Sstevel@tonic-gate 	case 2:
18970Sstevel@tonic-gate 		if (ustar_dir() || Adir) {
18980Sstevel@tonic-gate 			msg(ERRN, "Cannot create directory \"%s\"", nam_p);
18990Sstevel@tonic-gate 		} else if (ustar_spec() || Aspec) {
19000Sstevel@tonic-gate 			msg(ERRN, "Cannot mknod() \"%s\"", nam_p);
19010Sstevel@tonic-gate 		}
19020Sstevel@tonic-gate 
19030Sstevel@tonic-gate 		if (*Over_p == '\0') {
19040Sstevel@tonic-gate 			rstfiles(U_KEEP, dirfd);
19050Sstevel@tonic-gate 		}
19060Sstevel@tonic-gate 
19070Sstevel@tonic-gate 		break;
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 	default:
19100Sstevel@tonic-gate 		msg(EXT, "Impossible case.");
19110Sstevel@tonic-gate 	}
19120Sstevel@tonic-gate 
19130Sstevel@tonic-gate 	return (rv);
19140Sstevel@tonic-gate }
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate /*
19170Sstevel@tonic-gate  * creat_tmp:
19180Sstevel@tonic-gate  */
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate static int
19210Sstevel@tonic-gate creat_tmp(char *nam_p)
19220Sstevel@tonic-gate {
19230Sstevel@tonic-gate 	char *t_p;
19240Sstevel@tonic-gate 	int	cwd;
19250Sstevel@tonic-gate 
19260Sstevel@tonic-gate 	if ((Args & OCp) && G_p->g_ino == DesSt.st_ino &&
19270Sstevel@tonic-gate 	    G_p->g_dev == DesSt.st_dev) {
19280Sstevel@tonic-gate 		msg(ERR, "Attempt to pass a file to itself.");
19290Sstevel@tonic-gate 		return (-1);
19300Sstevel@tonic-gate 	}
19310Sstevel@tonic-gate 
19320Sstevel@tonic-gate 	if (G_p->g_mtime <= DesSt.st_mtime && !(Args & OCu)) {
19330Sstevel@tonic-gate 		msg(ERR, "Existing \"%s\" same age or newer", nam_p);
19340Sstevel@tonic-gate 		return (-1);
19350Sstevel@tonic-gate 	}
19360Sstevel@tonic-gate 
19370Sstevel@tonic-gate 	/* Make the temporary file name. */
19380Sstevel@tonic-gate 
19390Sstevel@tonic-gate 	(void) strcpy(Over_p, nam_p);
19400Sstevel@tonic-gate 	t_p = Over_p + strlen(Over_p);
19410Sstevel@tonic-gate 
19420Sstevel@tonic-gate 	while (t_p != Over_p) {
19430Sstevel@tonic-gate 		if (*(t_p - 1) == '/')
19440Sstevel@tonic-gate 			break;
19450Sstevel@tonic-gate 		t_p--;
19460Sstevel@tonic-gate 	}
19470Sstevel@tonic-gate 
19480Sstevel@tonic-gate 	(void) strcpy(t_p, "XXXXXX");
19490Sstevel@tonic-gate 
19500Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
19510Sstevel@tonic-gate 		/*
19520Sstevel@tonic-gate 		 * Save our current directory, so we can go into
19530Sstevel@tonic-gate 		 * the attribute directory to make the temp file
19540Sstevel@tonic-gate 		 * and then return.
19550Sstevel@tonic-gate 		 */
19560Sstevel@tonic-gate 
19570Sstevel@tonic-gate 		cwd = save_cwd();
19580Sstevel@tonic-gate 		(void) fchdir(G_p->g_dirfd);
19590Sstevel@tonic-gate 	}
19600Sstevel@tonic-gate 
19610Sstevel@tonic-gate 	(void) mktemp(Over_p);
19620Sstevel@tonic-gate 
19630Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
19640Sstevel@tonic-gate 		/* Return to the current directory. */
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate 		rest_cwd(cwd);
19670Sstevel@tonic-gate 	}
19680Sstevel@tonic-gate 
19690Sstevel@tonic-gate 	if (*Over_p == '\0') {
19700Sstevel@tonic-gate 		/* mktemp reports a failure. */
19710Sstevel@tonic-gate 
19720Sstevel@tonic-gate 		msg(ERR, "Cannot get temporary file name.");
19730Sstevel@tonic-gate 		return (-1);
19740Sstevel@tonic-gate 	}
19750Sstevel@tonic-gate 
19760Sstevel@tonic-gate 	/*
19770Sstevel@tonic-gate 	 * If it's a regular file, write to the temporary file, and then rename
19780Sstevel@tonic-gate 	 * in order to accomodate potential executables.
19790Sstevel@tonic-gate 	 *
19800Sstevel@tonic-gate 	 * Note: g_typeflag is only defined (set) for USTAR archive types.  It
19810Sstevel@tonic-gate 	 * defaults to 0 in the cpio-format-regular file case, so this test
19820Sstevel@tonic-gate 	 * succeeds.
19830Sstevel@tonic-gate 	 */
19840Sstevel@tonic-gate 
19850Sstevel@tonic-gate 	if (G_p->g_typeflag == 0 &&
19860Sstevel@tonic-gate 	    (DesSt.st_mode & (ulong_t)Ftype) == S_IFREG &&
19870Sstevel@tonic-gate 	    (G_p->g_mode & (ulong_t)Ftype) == S_IFREG) {
19880Sstevel@tonic-gate 		/*
19890Sstevel@tonic-gate 		 * The archive file and the filesystem file are both regular
19900Sstevel@tonic-gate 		 * files.  We write to the temporary file in this case.
19910Sstevel@tonic-gate 		 */
19920Sstevel@tonic-gate 
19930Sstevel@tonic-gate 		if (Args & OCp) {
19940Sstevel@tonic-gate 			if (G_p->g_attrnam_p == (char *)NULL) {
19950Sstevel@tonic-gate 				Fullnam_p = Over_p;
19960Sstevel@tonic-gate 			} else {
19970Sstevel@tonic-gate 				Attrfile_p = Over_p;
19980Sstevel@tonic-gate 			}
19990Sstevel@tonic-gate 		} else {
20000Sstevel@tonic-gate 			G_p->g_nam_p = Over_p;
20010Sstevel@tonic-gate 			if (G_p->g_attrnam_p != (char *)NULL) {
20020Sstevel@tonic-gate 				Attrfile_p = Over_p;
20030Sstevel@tonic-gate 			}
20040Sstevel@tonic-gate 		}
20050Sstevel@tonic-gate 
20060Sstevel@tonic-gate 		if (G_p->g_attrnam_p == (char *)NULL) {
20070Sstevel@tonic-gate 			Over_p = nam_p;
20080Sstevel@tonic-gate 		} else {
20090Sstevel@tonic-gate 			Over_p = G_p->g_attrnam_p;
20100Sstevel@tonic-gate 		}
20110Sstevel@tonic-gate 
20120Sstevel@tonic-gate 		Do_rename = 1;
20130Sstevel@tonic-gate 	} else {
20140Sstevel@tonic-gate 		/*
20150Sstevel@tonic-gate 		 * Either the archive file or the filesystem file is not a
20160Sstevel@tonic-gate 		 * regular file.
20170Sstevel@tonic-gate 		 */
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate 		Do_rename = 0;
20200Sstevel@tonic-gate 
20210Sstevel@tonic-gate 		if (S_ISDIR(DesSt.st_mode)) {
20220Sstevel@tonic-gate 			/*
20230Sstevel@tonic-gate 			 * The filesystem file is a directory.
20240Sstevel@tonic-gate 			 *
20250Sstevel@tonic-gate 			 * Save the current working directory because we will
20260Sstevel@tonic-gate 			 * want to restore it back just in case remove_dir()
20270Sstevel@tonic-gate 			 * fails or get confused about where we should be.
20280Sstevel@tonic-gate 			 */
20290Sstevel@tonic-gate 
20300Sstevel@tonic-gate 			*Over_p = '\0';
20310Sstevel@tonic-gate 			cwd = save_cwd();
20320Sstevel@tonic-gate 
20330Sstevel@tonic-gate 			if (remove_dir(nam_p) < 0) {
20340Sstevel@tonic-gate 				msg(ERRN,
20350Sstevel@tonic-gate 				    "Cannot remove the directory \"%s\"",
20360Sstevel@tonic-gate 				    nam_p);
20370Sstevel@tonic-gate 				/*
20380Sstevel@tonic-gate 				 * Restore working directory back to the one
20390Sstevel@tonic-gate 				 * saved earlier.
20400Sstevel@tonic-gate 				 */
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate 				rest_cwd(cwd);
20430Sstevel@tonic-gate 				return (-1);
20440Sstevel@tonic-gate 			}
20450Sstevel@tonic-gate 
20460Sstevel@tonic-gate 			/*
20470Sstevel@tonic-gate 			 * Restore working directory back to the one
20480Sstevel@tonic-gate 			 * saved earlier
20490Sstevel@tonic-gate 			 */
20500Sstevel@tonic-gate 
20510Sstevel@tonic-gate 			rest_cwd(cwd);
20520Sstevel@tonic-gate 		} else {
20530Sstevel@tonic-gate 			/*
20540Sstevel@tonic-gate 			 * The file is not a directory. Will use the original
20550Sstevel@tonic-gate 			 * link/unlink construct, however, if the file is
20560Sstevel@tonic-gate 			 * namefs, link would fail with EXDEV. Therefore, we
20570Sstevel@tonic-gate 			 * use rename() first to back up the file.
20580Sstevel@tonic-gate 			 */
20590Sstevel@tonic-gate 			if (rename(nam_p, Over_p) < 0) {
20600Sstevel@tonic-gate 				/*
20610Sstevel@tonic-gate 				 * If rename failed, try old construction
20620Sstevel@tonic-gate 				 * method.
20630Sstevel@tonic-gate 				 */
20640Sstevel@tonic-gate 				if (link(nam_p, Over_p) < 0) {
20650Sstevel@tonic-gate 					msg(ERRN,
20660Sstevel@tonic-gate 					    "Cannot create temporary file");
20670Sstevel@tonic-gate 					*Over_p = '\0';
20680Sstevel@tonic-gate 					return (-1);
20690Sstevel@tonic-gate 				}
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate 				if (unlink(nam_p) < 0) {
20720Sstevel@tonic-gate 					msg(ERRN,
20730Sstevel@tonic-gate 					    "Cannot unlink() current \"%s\"",
20740Sstevel@tonic-gate 					    nam_p);
20750Sstevel@tonic-gate 					(void) unlink(Over_p);
20760Sstevel@tonic-gate 					*Over_p = '\0';
20770Sstevel@tonic-gate 					return (-1);
20780Sstevel@tonic-gate 				}
20790Sstevel@tonic-gate 			}
20800Sstevel@tonic-gate 		}
20810Sstevel@tonic-gate 	}
20820Sstevel@tonic-gate 
20830Sstevel@tonic-gate 	return (1);
20840Sstevel@tonic-gate }
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate /*
20870Sstevel@tonic-gate  * data_in:  If proc_mode == P_PROC, bread() the file's data from the archive
20880Sstevel@tonic-gate  * and write(2) it to the open fdes gotten from openout().  If proc_mode ==
20890Sstevel@tonic-gate  * P_SKIP, or becomes P_SKIP (due to errors etc), bread(2) the file's data
20900Sstevel@tonic-gate  * and ignore it.  If the user specified any of the "swap" options (b, s or S),
20910Sstevel@tonic-gate  * and the length of the file is not appropriate for that action, do not
20920Sstevel@tonic-gate  * perform the "swap", otherwise perform the action on a buffer by buffer basis.
20930Sstevel@tonic-gate  * If the CRC header was selected, calculate a running checksum as each buffer
20940Sstevel@tonic-gate  * is processed.
20950Sstevel@tonic-gate  */
20960Sstevel@tonic-gate 
20970Sstevel@tonic-gate static void
20980Sstevel@tonic-gate data_in(int proc_mode)
20990Sstevel@tonic-gate {
21000Sstevel@tonic-gate 	char *nam_p;
21010Sstevel@tonic-gate 	int cnt, pad;
21020Sstevel@tonic-gate 	long cksumval = 0L;
21030Sstevel@tonic-gate 	off_t filesz;
21040Sstevel@tonic-gate 	int rv, swapfile = 0;
21050Sstevel@tonic-gate 	int compress_flag = 0;	/* if the file is compressed */
21060Sstevel@tonic-gate 	int cstatus = 0;
21070Sstevel@tonic-gate 	FILE *pipef;		/* pipe for bar to do de-compression */
21080Sstevel@tonic-gate 
21090Sstevel@tonic-gate 
21100Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
21110Sstevel@tonic-gate 		nam_p = G_p->g_attrnam_p;
21120Sstevel@tonic-gate 	} else {
21130Sstevel@tonic-gate 		nam_p = G_p->g_nam_p;
21140Sstevel@tonic-gate 	}
21150Sstevel@tonic-gate 
21160Sstevel@tonic-gate 
21170Sstevel@tonic-gate 	if (((G_p->g_mode & Ftype) == S_IFLNK && proc_mode != P_SKIP) ||
21180Sstevel@tonic-gate 	    (Hdr_type == BAR && bar_linkflag == '2' && proc_mode != P_SKIP)) {
21190Sstevel@tonic-gate 		proc_mode = P_SKIP;
21200Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
21210Sstevel@tonic-gate 	}
21220Sstevel@tonic-gate 	if (Args & (OCb | OCs | OCS)) { /* verfify that swapping is possible */
21230Sstevel@tonic-gate 		swapfile = 1;
21240Sstevel@tonic-gate 		if (Args & (OCs | OCb) && G_p->g_filesz % 2) {
21250Sstevel@tonic-gate 			msg(ERR,
21260Sstevel@tonic-gate 			    "Cannot swap bytes of \"%s\", odd number of bytes",
21270Sstevel@tonic-gate 			    nam_p);
21280Sstevel@tonic-gate 			swapfile = 0;
21290Sstevel@tonic-gate 		}
21300Sstevel@tonic-gate 		if (Args & (OCS | OCb) && G_p->g_filesz % 4) {
21310Sstevel@tonic-gate 			msg(ERR,
21320Sstevel@tonic-gate 			    "Cannot swap halfwords of \"%s\", odd number "
21330Sstevel@tonic-gate 			    "of halfwords", nam_p);
21340Sstevel@tonic-gate 			swapfile = 0;
21350Sstevel@tonic-gate 		}
21360Sstevel@tonic-gate 	}
21370Sstevel@tonic-gate 	filesz = G_p->g_filesz;
21380Sstevel@tonic-gate 
21390Sstevel@tonic-gate 	/* This writes out the file from the archive */
21400Sstevel@tonic-gate 
21410Sstevel@tonic-gate 	while (filesz > 0) {
21420Sstevel@tonic-gate 		cnt = (int)(filesz > CPIOBSZ) ? CPIOBSZ : filesz;
21430Sstevel@tonic-gate 		FILL(cnt);
21440Sstevel@tonic-gate 		if (proc_mode != P_SKIP) {
21450Sstevel@tonic-gate 			if (Hdr_type == CRC)
21460Sstevel@tonic-gate 				cksumval += cksum(CRC, cnt, NULL);
21470Sstevel@tonic-gate 			if (swapfile)
21480Sstevel@tonic-gate 				swap(Buffr.b_out_p, cnt);
21490Sstevel@tonic-gate 			errno = 0;
21500Sstevel@tonic-gate 
21510Sstevel@tonic-gate 			/*
21520Sstevel@tonic-gate 			 * if the bar archive is compressed, set up a pipe and
21530Sstevel@tonic-gate 			 * do the de-compression while reading in the file
21540Sstevel@tonic-gate 			 */
21550Sstevel@tonic-gate 			if (Hdr_type == BAR) {
21560Sstevel@tonic-gate 				if (compress_flag == 0 && Compressed) {
21570Sstevel@tonic-gate 					setup_uncompress(&pipef);
21580Sstevel@tonic-gate 					compress_flag++;
21590Sstevel@tonic-gate 				}
21600Sstevel@tonic-gate 
21610Sstevel@tonic-gate 			}
21620Sstevel@tonic-gate 
21630Sstevel@tonic-gate 			rv = write(Ofile, Buffr.b_out_p, cnt);
21640Sstevel@tonic-gate 			if (rv < cnt) {
21650Sstevel@tonic-gate 				if (rv < 0)
21660Sstevel@tonic-gate 					msg(ERRN, "Cannot write \"%s\"", nam_p);
21670Sstevel@tonic-gate 				else
21680Sstevel@tonic-gate 					msg(EXTN, "Cannot write \"%s\"", nam_p);
21690Sstevel@tonic-gate 				proc_mode = P_SKIP;
21700Sstevel@tonic-gate 				rstfiles(U_KEEP, G_p->g_dirfd);
21710Sstevel@tonic-gate 				cstatus = close(Ofile);
21720Sstevel@tonic-gate 				Ofile = 0;
21730Sstevel@tonic-gate 				if (cstatus != 0) {
21740Sstevel@tonic-gate 					msg(EXTN, "close error");
21750Sstevel@tonic-gate 				}
21760Sstevel@tonic-gate 			}
21770Sstevel@tonic-gate 		}
21780Sstevel@tonic-gate 		Buffr.b_out_p += cnt;
21790Sstevel@tonic-gate 		Buffr.b_cnt -= (long)cnt;
21800Sstevel@tonic-gate 		filesz -= (off_t)cnt;
21810Sstevel@tonic-gate 	} /* filesz */
21820Sstevel@tonic-gate 
21830Sstevel@tonic-gate 	pad = (Pad_val + 1 - (G_p->g_filesz & Pad_val)) & Pad_val;
21840Sstevel@tonic-gate 	if (pad != 0) {
21850Sstevel@tonic-gate 		FILL(pad);
21860Sstevel@tonic-gate 		Buffr.b_out_p += pad;
21870Sstevel@tonic-gate 		Buffr.b_cnt -= pad;
21880Sstevel@tonic-gate 	}
21890Sstevel@tonic-gate 	if (proc_mode != P_SKIP) {
21900Sstevel@tonic-gate 		if (Hdr_type == CRC && Gen.g_cksum != cksumval) {
21910Sstevel@tonic-gate 			msg(ERR, "\"%s\" - checksum error", nam_p);
21920Sstevel@tonic-gate 			rstfiles(U_KEEP, G_p->g_dirfd);
21930Sstevel@tonic-gate 		} else
21940Sstevel@tonic-gate 			rstfiles(U_OVER, G_p->g_dirfd);
21950Sstevel@tonic-gate 		if (Hdr_type == BAR && compress_flag) {
21960Sstevel@tonic-gate 			(void) pclose(pipef);
21970Sstevel@tonic-gate 		} else {
21980Sstevel@tonic-gate 			cstatus = close(Ofile);
21990Sstevel@tonic-gate 		}
22000Sstevel@tonic-gate 		Ofile = 0;
22010Sstevel@tonic-gate 		if (cstatus != 0) {
22020Sstevel@tonic-gate 			msg(EXTN, "close error");
22030Sstevel@tonic-gate 		}
22040Sstevel@tonic-gate 	}
22050Sstevel@tonic-gate 	VERBOSE((proc_mode != P_SKIP && (Args & (OCv | OCV))), G_p->g_nam_p);
22060Sstevel@tonic-gate 	Finished = 1;
22070Sstevel@tonic-gate }
22080Sstevel@tonic-gate 
22090Sstevel@tonic-gate /*
22100Sstevel@tonic-gate  * data_out:  open(2) the file to be archived, compute the checksum
22110Sstevel@tonic-gate  * of it's data if the CRC header was specified and write the header.
22120Sstevel@tonic-gate  * read(2) each block of data and bwrite() it to the archive.  For TARTYP (TAR
22130Sstevel@tonic-gate  * and USTAR) archives, pad the data with NULLs to the next 512 byte boundary.
22140Sstevel@tonic-gate  */
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate static void
22170Sstevel@tonic-gate data_out(void)
22180Sstevel@tonic-gate {
22190Sstevel@tonic-gate 	char *nam_p;
22200Sstevel@tonic-gate 	int cnt, amount_read, pad;
22210Sstevel@tonic-gate 	off_t amt_to_read, real_filesz;
22220Sstevel@tonic-gate 	int	errret = 0;
22230Sstevel@tonic-gate 
22240Sstevel@tonic-gate 	nam_p = G_p->g_nam_p;
22250Sstevel@tonic-gate 	if (Aspec) {
22260Sstevel@tonic-gate 		if (Pflag && aclp != NULL) {
22270Sstevel@tonic-gate 			char    *secinfo = NULL;
22280Sstevel@tonic-gate 			int	len = 0;
22290Sstevel@tonic-gate 
22300Sstevel@tonic-gate 			/* append security attributes */
2231789Sahrens 			if (append_secattr(&secinfo, &len, aclp) == -1) {
22320Sstevel@tonic-gate 				msg(ERR,
22330Sstevel@tonic-gate 				    "can create security information");
22340Sstevel@tonic-gate 			}
22350Sstevel@tonic-gate 			/* call append_secattr() if more than one */
22360Sstevel@tonic-gate 
22370Sstevel@tonic-gate 			if (len > 0) {
22380Sstevel@tonic-gate 			/* write ancillary only if there is sec info */
22390Sstevel@tonic-gate 				(void) write_hdr(ARCHIVE_ACL, (off_t)len);
22400Sstevel@tonic-gate 				(void) write_ancillary(secinfo, len);
22410Sstevel@tonic-gate 			}
22420Sstevel@tonic-gate 		}
22430Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
22440Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_dirfd);
22450Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
22460Sstevel@tonic-gate 		return;
22470Sstevel@tonic-gate 	}
22480Sstevel@tonic-gate 	if ((G_p->g_mode & Ftype) == S_IFLNK && (Hdr_type !=
22490Sstevel@tonic-gate 	    USTAR && Hdr_type != TAR)) { /* symbolic link */
22500Sstevel@tonic-gate 		int size;
22510Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
22520Sstevel@tonic-gate 
22530Sstevel@tonic-gate 		FLUSH(G_p->g_filesz);
22540Sstevel@tonic-gate 		errno = 0;
22550Sstevel@tonic-gate 
22560Sstevel@tonic-gate 		/* Note that "size" and G_p->g_filesz are the same number */
22570Sstevel@tonic-gate 
22580Sstevel@tonic-gate 		if ((size = readlink(nam_p, Buffr.b_in_p, G_p->g_filesz)) <
22590Sstevel@tonic-gate 		    0) {
22600Sstevel@tonic-gate 			msg(ERRN, "Cannot read symbolic link \"%s\"", nam_p);
22610Sstevel@tonic-gate 			return;
22620Sstevel@tonic-gate 		}
22630Sstevel@tonic-gate 
22640Sstevel@tonic-gate 		/*
22650Sstevel@tonic-gate 		 * Note that it is OK not to add the NUL after the name read by
22660Sstevel@tonic-gate 		 * readlink, because it is not being used subsequently.
22670Sstevel@tonic-gate 		 */
22680Sstevel@tonic-gate 
22690Sstevel@tonic-gate 		Buffr.b_in_p += size;
22700Sstevel@tonic-gate 		Buffr.b_cnt += size;
22710Sstevel@tonic-gate 		pad = (Pad_val + 1 - (size & Pad_val)) & Pad_val;
22720Sstevel@tonic-gate 		if (pad != 0) {
22730Sstevel@tonic-gate 			FLUSH(pad);
22740Sstevel@tonic-gate 			(void) memcpy(Buffr.b_in_p, Empty, pad);
22750Sstevel@tonic-gate 			Buffr.b_in_p += pad;
22760Sstevel@tonic-gate 			Buffr.b_cnt += pad;
22770Sstevel@tonic-gate 		}
22780Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
22790Sstevel@tonic-gate 		return;
22800Sstevel@tonic-gate 	} else if ((G_p->g_mode & Ftype) == S_IFLNK &&
22811134Sceastha 	    (Hdr_type == USTAR || Hdr_type == TAR)) {
22820Sstevel@tonic-gate 		int size;
22830Sstevel@tonic-gate 
22840Sstevel@tonic-gate 		/*
22850Sstevel@tonic-gate 		 * G_p->g_filesz is the length of the right-hand side of
22860Sstevel@tonic-gate 		 * the symlink "x -> y".
22870Sstevel@tonic-gate 		 * The tar link field is only NAMSIZ long.
22880Sstevel@tonic-gate 		 */
22890Sstevel@tonic-gate 
22900Sstevel@tonic-gate 		if (G_p->g_filesz > NAMSIZ) {
22910Sstevel@tonic-gate 			msg(ERRN,
22920Sstevel@tonic-gate 			    "Symbolic link too long \"%s\"", nam_p);
22930Sstevel@tonic-gate 			return;
22940Sstevel@tonic-gate 		}
22950Sstevel@tonic-gate 		if ((size = readlink(nam_p, T_lname, G_p->g_filesz)) < 0) {
22960Sstevel@tonic-gate 			msg(ERRN,
22970Sstevel@tonic-gate 			    "Cannot read symbolic link \"%s\"", nam_p);
22980Sstevel@tonic-gate 			return;
22990Sstevel@tonic-gate 		}
23000Sstevel@tonic-gate 		T_lname[size] = '\0';
23010Sstevel@tonic-gate 		G_p->g_filesz = (off_t)0;
23020Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
23030Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
23040Sstevel@tonic-gate 		return;
23050Sstevel@tonic-gate 	}
23060Sstevel@tonic-gate 	if ((Ifile = openfile(O_RDONLY)) < 0) {
23070Sstevel@tonic-gate 		msg(ERR, "\"%s%s%s\" ?",
23080Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
23090Sstevel@tonic-gate 		    nam_p : Gen.g_attrfnam_p,
23100Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
23110Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
23120Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
23130Sstevel@tonic-gate 		    "" : Gen.g_attrnam_p);
23140Sstevel@tonic-gate 		return;
23150Sstevel@tonic-gate 	}
23160Sstevel@tonic-gate 
23170Sstevel@tonic-gate 	/*
23180Sstevel@tonic-gate 	 * Dump extended attribute header.
23190Sstevel@tonic-gate 	 */
23200Sstevel@tonic-gate 
23210Sstevel@tonic-gate 	if (Gen.g_attrnam_p != (char *)NULL) {
23220Sstevel@tonic-gate 		write_xattr_hdr();
23230Sstevel@tonic-gate 	}
23240Sstevel@tonic-gate 
23250Sstevel@tonic-gate 	if (Hdr_type == CRC) {
23260Sstevel@tonic-gate 		long csum = cksum(CRC, 0, &errret);
23270Sstevel@tonic-gate 		if (errret != 0) {
23280Sstevel@tonic-gate 			G_p->g_cksum = (ulong_t)-1;
23290Sstevel@tonic-gate 			msg(POST, "\"%s%s%s\" skipped",
23300Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
23310Sstevel@tonic-gate 			    nam_p : Gen.g_attrfnam_p,
23320Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
23330Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
23340Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
23350Sstevel@tonic-gate 			    "" : nam_p);
23360Sstevel@tonic-gate 			(void) close(Ifile);
23370Sstevel@tonic-gate 			return;
23380Sstevel@tonic-gate 		}
23390Sstevel@tonic-gate 		G_p->g_cksum = csum;
23400Sstevel@tonic-gate 	} else {
23410Sstevel@tonic-gate 		G_p->g_cksum = 0;
23420Sstevel@tonic-gate 	}
23430Sstevel@tonic-gate 
23440Sstevel@tonic-gate 	/*
23450Sstevel@tonic-gate 	 * ACL has been retrieved in getname().
23460Sstevel@tonic-gate 	 */
23470Sstevel@tonic-gate 	if (Pflag) {
23480Sstevel@tonic-gate 		char    *secinfo = NULL;
23490Sstevel@tonic-gate 		int	len = 0;
23500Sstevel@tonic-gate 
23510Sstevel@tonic-gate 		/* append security attributes */
2352789Sahrens 		if ((append_secattr(&secinfo, &len, aclp)) == -1)
23530Sstevel@tonic-gate 			msg(ERR, "can create security information");
23540Sstevel@tonic-gate 
23550Sstevel@tonic-gate 		/* call append_secattr() if more than one */
23560Sstevel@tonic-gate 
23570Sstevel@tonic-gate 		if (len > 0) {
23580Sstevel@tonic-gate 		/* write ancillary only if there is sec info */
23590Sstevel@tonic-gate 			(void) write_hdr(ARCHIVE_ACL, (off_t)len);
23600Sstevel@tonic-gate 			(void) write_ancillary(secinfo, len);
23610Sstevel@tonic-gate 		}
23620Sstevel@tonic-gate 	}
23630Sstevel@tonic-gate 
23640Sstevel@tonic-gate 	write_hdr(ARCHIVE_NORMAL, (off_t)0);
23650Sstevel@tonic-gate 
23660Sstevel@tonic-gate 	real_filesz = 0;
23670Sstevel@tonic-gate 
23680Sstevel@tonic-gate 	for (amt_to_read = G_p->g_filesz;
23690Sstevel@tonic-gate 	    amt_to_read > 0;
23700Sstevel@tonic-gate 	    amt_to_read -= (off_t)amount_read) {
23710Sstevel@tonic-gate 		FLUSH(CPIOBSZ);
23720Sstevel@tonic-gate 		errno = 0;
23730Sstevel@tonic-gate 
23740Sstevel@tonic-gate 		if ((amount_read = read(Ifile, Buffr.b_in_p, CPIOBSZ)) < 0) {
23750Sstevel@tonic-gate 			msg(EXTN, "Cannot read \"%s%s%s\"",
23760Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
23770Sstevel@tonic-gate 			    nam_p : Gen.g_attrfnam_p,
23780Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
23790Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
23800Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
23810Sstevel@tonic-gate 			    "" : nam_p);
23820Sstevel@tonic-gate 			break;
23830Sstevel@tonic-gate 		}
23840Sstevel@tonic-gate 
23850Sstevel@tonic-gate 		if (amount_read == 0) {
23860Sstevel@tonic-gate 			/* the file has shrunk */
23870Sstevel@tonic-gate 			real_filesz = G_p->g_filesz - amt_to_read;
23880Sstevel@tonic-gate 			break;
23890Sstevel@tonic-gate 		} else if (amount_read > amt_to_read) {
23900Sstevel@tonic-gate 			/* the file has grown */
23910Sstevel@tonic-gate 			real_filesz = G_p->g_filesz +
23921134Sceastha 			    (amount_read - amt_to_read);
23930Sstevel@tonic-gate 			amount_read = amt_to_read;
23940Sstevel@tonic-gate 		} else if (amount_read == amt_to_read) {
23950Sstevel@tonic-gate 			/* the file is the same size */
23960Sstevel@tonic-gate 			real_filesz = G_p->g_filesz;
23970Sstevel@tonic-gate 		}
23980Sstevel@tonic-gate 
23990Sstevel@tonic-gate 		Buffr.b_in_p += amount_read;
24000Sstevel@tonic-gate 		Buffr.b_cnt += (long)amount_read;
24010Sstevel@tonic-gate 	}
24020Sstevel@tonic-gate 
24030Sstevel@tonic-gate 	while (amt_to_read > 0) {
24040Sstevel@tonic-gate 		cnt = (amt_to_read > CPIOBSZ) ? CPIOBSZ : (int)amt_to_read;
24050Sstevel@tonic-gate 		FLUSH(cnt);
24060Sstevel@tonic-gate 		(void) memset(Buffr.b_in_p, NULL, cnt);
24070Sstevel@tonic-gate 		Buffr.b_in_p += cnt;
24080Sstevel@tonic-gate 		Buffr.b_cnt += cnt;
24090Sstevel@tonic-gate 		amt_to_read -= cnt;
24100Sstevel@tonic-gate 	}
24110Sstevel@tonic-gate 
24120Sstevel@tonic-gate 	pad = (Pad_val + 1 - (G_p->g_filesz & Pad_val)) & Pad_val;
24130Sstevel@tonic-gate 	if (pad != 0) {
24140Sstevel@tonic-gate 		FLUSH(pad);
24150Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, Empty, pad);
24160Sstevel@tonic-gate 		Buffr.b_in_p += pad;
24170Sstevel@tonic-gate 		Buffr.b_cnt += pad;
24180Sstevel@tonic-gate 	}
24190Sstevel@tonic-gate 
24200Sstevel@tonic-gate 	if (real_filesz > G_p->g_filesz) {
24210Sstevel@tonic-gate 		msg(ERR, "File size of \"%s%s%s\" has increased by %lld",
24220Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
24230Sstevel@tonic-gate 		    G_p->g_nam_p : Gen.g_attrfnam_p,
24240Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
24250Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
24260Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
24270Sstevel@tonic-gate 		    "" : G_p->g_nam_p,
24280Sstevel@tonic-gate 		    (real_filesz - G_p->g_filesz));
24290Sstevel@tonic-gate 	}
24300Sstevel@tonic-gate 	if (real_filesz < G_p->g_filesz) {
24310Sstevel@tonic-gate 		msg(ERR, "File size of \"%s%s%s\" has decreased by %lld",
24320Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
24330Sstevel@tonic-gate 		    G_p->g_nam_p : Gen.g_attrfnam_p,
24340Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
24350Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
24360Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
24370Sstevel@tonic-gate 		    "" : G_p->g_nam_p,
24380Sstevel@tonic-gate 		    (G_p->g_filesz - real_filesz));
24390Sstevel@tonic-gate 	}
24400Sstevel@tonic-gate 
24410Sstevel@tonic-gate 	(void) close(Ifile);
24420Sstevel@tonic-gate 	rstfiles(U_KEEP, G_p->g_dirfd);
24430Sstevel@tonic-gate 	VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
24440Sstevel@tonic-gate }
24450Sstevel@tonic-gate 
24460Sstevel@tonic-gate /*
24470Sstevel@tonic-gate  * data_pass:  If not a special file (Aspec), open(2) the file to be
24480Sstevel@tonic-gate  * transferred, read(2) each block of data and write(2) it to the output file
24490Sstevel@tonic-gate  * Ofile, which was opened in file_pass().
24500Sstevel@tonic-gate  */
24510Sstevel@tonic-gate 
24520Sstevel@tonic-gate static void
24530Sstevel@tonic-gate data_pass(void)
24540Sstevel@tonic-gate {
24550Sstevel@tonic-gate 	int cnt, done = 1;
24560Sstevel@tonic-gate 	int cstatus;
24570Sstevel@tonic-gate 	off_t filesz;
24580Sstevel@tonic-gate 	char *namep = Nam_p;
24590Sstevel@tonic-gate 
24600Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
24610Sstevel@tonic-gate 		namep = G_p->g_attrnam_p;
24620Sstevel@tonic-gate 	}
24630Sstevel@tonic-gate 	if (Aspec) {
24640Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_passdirfd);
24650Sstevel@tonic-gate 		cstatus = close(Ofile);
24660Sstevel@tonic-gate 		Ofile = 0;
24670Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), Nam_p);
24680Sstevel@tonic-gate 		if (cstatus != 0) {
24690Sstevel@tonic-gate 			msg(EXTN, "close error");
24700Sstevel@tonic-gate 		}
24710Sstevel@tonic-gate 		return;
24720Sstevel@tonic-gate 	}
24730Sstevel@tonic-gate 	if ((Ifile = openat(G_p->g_dirfd, get_component(namep), 0)) < 0) {
24740Sstevel@tonic-gate 		msg(ERRN, "Cannot open \"%s%s%s\", skipped",
24750Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
24760Sstevel@tonic-gate 		    Nam_p : G_p->g_attrfnam_p,
24770Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
24780Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
24790Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
24800Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p);
24810Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_passdirfd);
24820Sstevel@tonic-gate 		cstatus = close(Ofile);
24830Sstevel@tonic-gate 		Ofile = 0;
24840Sstevel@tonic-gate 		if (cstatus != 0) {
24850Sstevel@tonic-gate 			msg(EXTN, "close error");
24860Sstevel@tonic-gate 		}
24870Sstevel@tonic-gate 		return;
24880Sstevel@tonic-gate 	}
24890Sstevel@tonic-gate 	filesz = G_p->g_filesz;
24900Sstevel@tonic-gate 
24910Sstevel@tonic-gate 	while (filesz > 0) {
24920Sstevel@tonic-gate 		cnt = (unsigned)(filesz > Bufsize) ? Bufsize : filesz;
24930Sstevel@tonic-gate 		errno = 0;
24940Sstevel@tonic-gate 		if (read(Ifile, Buf_p, (unsigned)cnt) < 0) {
24950Sstevel@tonic-gate 			msg(ERRN, "Cannot read \"%s%s%s\"",
24960Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
24970Sstevel@tonic-gate 			    Nam_p : G_p->g_attrfnam_p,
24980Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
24990Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
25000Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
25010Sstevel@tonic-gate 			    "" : G_p->g_attrnam_p);
25020Sstevel@tonic-gate 			done = 0;
25030Sstevel@tonic-gate 			break;
25040Sstevel@tonic-gate 		}
25050Sstevel@tonic-gate 		errno = 0;
25060Sstevel@tonic-gate 		if (write(Ofile, Buf_p, (unsigned)cnt) < 0) {
25070Sstevel@tonic-gate 			if (Do_rename) {
25080Sstevel@tonic-gate 				msg(ERRN, "Cannot write \"%s%s%s\"", Over_p,
25090Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
25100Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
25110Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
25120Sstevel@tonic-gate 				    "" : Over_p);
25130Sstevel@tonic-gate 			} else {
25140Sstevel@tonic-gate 				msg(ERRN, "Cannot write \"%s%s%s\"",
25150Sstevel@tonic-gate 				    Fullnam_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 				    "" : G_p->g_attrnam_p);
25200Sstevel@tonic-gate 			}
25210Sstevel@tonic-gate 
25220Sstevel@tonic-gate 			done = 0;
25230Sstevel@tonic-gate 			break;
25240Sstevel@tonic-gate 		}
25250Sstevel@tonic-gate 		Blocks += (u_longlong_t)((cnt + (Bufsize - 1)) / Bufsize);
25260Sstevel@tonic-gate 		filesz -= (off_t)cnt;
25270Sstevel@tonic-gate 	}
25280Sstevel@tonic-gate 	if (done) {
25290Sstevel@tonic-gate 		rstfiles(U_OVER, G_p->g_passdirfd);
25300Sstevel@tonic-gate 	} else {
25310Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_passdirfd);
25320Sstevel@tonic-gate 	}
25330Sstevel@tonic-gate 	(void) close(Ifile);
25340Sstevel@tonic-gate 	cstatus = close(Ofile);
25350Sstevel@tonic-gate 	Ofile = 0;
25360Sstevel@tonic-gate 	if (cstatus != 0) {
25370Sstevel@tonic-gate 		msg(EXTN, "close error");
25380Sstevel@tonic-gate 	}
25390Sstevel@tonic-gate 	VERBOSE((Args & (OCv | OCV)), Fullnam_p);
25400Sstevel@tonic-gate 	Finished = 1;
25410Sstevel@tonic-gate }
25420Sstevel@tonic-gate 
25430Sstevel@tonic-gate /*
25440Sstevel@tonic-gate  * Allocation wrappers.  Used to centralize error handling for
25450Sstevel@tonic-gate  * failed allocations.
25460Sstevel@tonic-gate  */
25470Sstevel@tonic-gate static void *
25480Sstevel@tonic-gate e_alloc_fail(int flag)
25490Sstevel@tonic-gate {
25500Sstevel@tonic-gate 	if (flag == E_EXIT) {
25510Sstevel@tonic-gate 		msg(EXTN, "Out of memory");
25520Sstevel@tonic-gate 	}
25530Sstevel@tonic-gate 
25540Sstevel@tonic-gate 	return (NULL);
25550Sstevel@tonic-gate }
25560Sstevel@tonic-gate 
25570Sstevel@tonic-gate /*
25580Sstevel@tonic-gate  *  Note: unlike the other e_*lloc functions, e_realloc does not zero out the
25590Sstevel@tonic-gate  *  additional memory it returns.  Ensure that you do not trust its contents
25600Sstevel@tonic-gate  *  when you call it.
25610Sstevel@tonic-gate  */
25620Sstevel@tonic-gate 
25630Sstevel@tonic-gate static void *
25640Sstevel@tonic-gate e_realloc(int flag, void *old, size_t newsize)
25650Sstevel@tonic-gate {
25660Sstevel@tonic-gate 	void *ret = realloc(old, newsize);
25670Sstevel@tonic-gate 
25680Sstevel@tonic-gate 	if (ret == NULL) {
25690Sstevel@tonic-gate 		return (e_alloc_fail(flag));
25700Sstevel@tonic-gate 	}
25710Sstevel@tonic-gate 
25720Sstevel@tonic-gate 	return (ret);
25730Sstevel@tonic-gate }
25740Sstevel@tonic-gate 
25750Sstevel@tonic-gate static char *
25760Sstevel@tonic-gate e_strdup(int flag, const char *arg)
25770Sstevel@tonic-gate {
25780Sstevel@tonic-gate 	char *ret = strdup(arg);
25790Sstevel@tonic-gate 
25800Sstevel@tonic-gate 	if (ret == NULL) {
25810Sstevel@tonic-gate 		return (e_alloc_fail(flag));
25820Sstevel@tonic-gate 	}
25830Sstevel@tonic-gate 
25840Sstevel@tonic-gate 	return (ret);
25850Sstevel@tonic-gate }
25860Sstevel@tonic-gate 
25870Sstevel@tonic-gate static void *
25880Sstevel@tonic-gate e_valloc(int flag, size_t size)
25890Sstevel@tonic-gate {
25900Sstevel@tonic-gate 	void *ret = valloc(size);
25910Sstevel@tonic-gate 
25920Sstevel@tonic-gate 	if (ret == NULL) {
25930Sstevel@tonic-gate 		return (e_alloc_fail(flag));
25940Sstevel@tonic-gate 	}
25950Sstevel@tonic-gate 
25960Sstevel@tonic-gate 	return (ret);
25970Sstevel@tonic-gate }
25980Sstevel@tonic-gate 
25990Sstevel@tonic-gate static void *
26000Sstevel@tonic-gate e_zalloc(int flag, size_t size)
26010Sstevel@tonic-gate {
26020Sstevel@tonic-gate 	void *ret = malloc(size);
26030Sstevel@tonic-gate 
26040Sstevel@tonic-gate 	if (ret == NULL) {
26050Sstevel@tonic-gate 		return (e_alloc_fail(flag));
26060Sstevel@tonic-gate 	}
26070Sstevel@tonic-gate 
26080Sstevel@tonic-gate 	(void) memset(ret, 0, size);
26090Sstevel@tonic-gate 	return (ret);
26100Sstevel@tonic-gate }
26110Sstevel@tonic-gate 
26120Sstevel@tonic-gate /*
26130Sstevel@tonic-gate  * file_in:  Process an object from the archive.  If a TARTYP (TAR or USTAR)
26140Sstevel@tonic-gate  * archive and g_nlink == 1, link this file to the file name in t_linkname
26150Sstevel@tonic-gate  * and return.  Handle linked files in one of two ways.  If Onecopy == 0, this
26160Sstevel@tonic-gate  * is an old style (binary or -c) archive, create and extract the data for the
26170Sstevel@tonic-gate  * first link found, link all subsequent links to this file and skip their data.
26180Sstevel@tonic-gate  * If Oncecopy == 1, save links until all have been processed, and then
26190Sstevel@tonic-gate  * process the links first to last checking their names against the patterns
26200Sstevel@tonic-gate  * and/or asking the user to rename them.  The first link that is accepted
26210Sstevel@tonic-gate  * for xtraction is created and the data is read from the archive.
26220Sstevel@tonic-gate  * All subsequent links that are accepted are linked to this file.
26230Sstevel@tonic-gate  */
26240Sstevel@tonic-gate 
26250Sstevel@tonic-gate static void
26260Sstevel@tonic-gate file_in(void)
26270Sstevel@tonic-gate {
26280Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
26290Sstevel@tonic-gate 	int lnkem = 0, cleanup = 0;
26300Sstevel@tonic-gate 	int proc_file;
26310Sstevel@tonic-gate 	struct Lnk *ttl_p;
26320Sstevel@tonic-gate 	int typeflag;
26330Sstevel@tonic-gate 	char savacl;
26340Sstevel@tonic-gate 	int cwd;
26350Sstevel@tonic-gate 
26360Sstevel@tonic-gate 	G_p = &Gen;
26370Sstevel@tonic-gate 
26380Sstevel@tonic-gate 	/*
26390Sstevel@tonic-gate 	 * Open target directory if this isn't a skipped file
26400Sstevel@tonic-gate 	 * and g_nlink == 1
26410Sstevel@tonic-gate 	 *
26420Sstevel@tonic-gate 	 * Links are handled further down in this function.
26430Sstevel@tonic-gate 	 */
26440Sstevel@tonic-gate 
26450Sstevel@tonic-gate 	proc_file = ckname(0);
26460Sstevel@tonic-gate 
26470Sstevel@tonic-gate 	if (proc_file == F_SKIP && G_p->g_nlink == 1) {
26480Sstevel@tonic-gate 		/*
26490Sstevel@tonic-gate 		 * Normally ckname() prints out the file as a side
26500Sstevel@tonic-gate 		 * effect except for table of contents listing
26510Sstevel@tonic-gate 		 * when its parameter is zero and Onecopy isn't
26520Sstevel@tonic-gate 		 * Zero.  Due to this we need to force the name
26530Sstevel@tonic-gate 		 * to be printed here.
26540Sstevel@tonic-gate 		 */
26550Sstevel@tonic-gate 		if (Onecopy == 1) {
26560Sstevel@tonic-gate 			VERBOSE((Args & OCt), G_p->g_nam_p);
26570Sstevel@tonic-gate 		}
26580Sstevel@tonic-gate 		data_in(P_SKIP);
26590Sstevel@tonic-gate 		return;
26600Sstevel@tonic-gate 	}
26610Sstevel@tonic-gate 
26620Sstevel@tonic-gate 	if (proc_file != F_SKIP && open_dirfd() != 0) {
26630Sstevel@tonic-gate 		data_in(P_SKIP);
26640Sstevel@tonic-gate 		return;
26650Sstevel@tonic-gate 	}
26660Sstevel@tonic-gate 
26670Sstevel@tonic-gate 	if (Hdr_type == BAR) {
26680Sstevel@tonic-gate 		bar_file_in();
26690Sstevel@tonic-gate 		close_dirfd();
26700Sstevel@tonic-gate 		return;
26710Sstevel@tonic-gate 	}
26720Sstevel@tonic-gate 
26730Sstevel@tonic-gate 	/*
26740Sstevel@tonic-gate 	 * For archives in USTAR format, the files are extracted according
26750Sstevel@tonic-gate 	 * to the typeflag.
26760Sstevel@tonic-gate 	 */
26770Sstevel@tonic-gate 	if (Hdr_type == USTAR || Hdr_type == TAR) {
26780Sstevel@tonic-gate 		typeflag = Thdr_p->tbuf.t_typeflag;
26790Sstevel@tonic-gate 		if (G_p->g_nlink == 1) {		/* hard link */
26800Sstevel@tonic-gate 			if (proc_file != F_SKIP) {
26811134Sceastha 				int i;
26821134Sceastha 				char lname[NAMSIZ+1];
26831134Sceastha 				(void) memset(lname, '\0', sizeof (lname));
26841134Sceastha 
26851134Sceastha 				(void) strncpy(lname, Thdr_p->tbuf.t_linkname,
26861134Sceastha 				    NAMSIZ);
26871134Sceastha 				for (i = 0; i <= NAMSIZ && lname[i] != 0; i++)
26881134Sceastha 					;
26891134Sceastha 
26901134Sceastha 				lname[i] = 0;
26911134Sceastha 				(void) creat_lnk(G_p->g_dirfd,
26921134Sceastha 				    &lname[0], G_p->g_nam_p);
26930Sstevel@tonic-gate 			}
26940Sstevel@tonic-gate 			close_dirfd();
26950Sstevel@tonic-gate 			return;
26960Sstevel@tonic-gate 		}
26970Sstevel@tonic-gate 		if (typeflag == '3' || typeflag == '4' || typeflag == '5' ||
26980Sstevel@tonic-gate 		    typeflag == '6') {
26990Sstevel@tonic-gate 			if (proc_file != F_SKIP &&
27000Sstevel@tonic-gate 			    creat_spec(G_p->g_dirfd) > 0) {
27010Sstevel@tonic-gate 				VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
27020Sstevel@tonic-gate 			}
27030Sstevel@tonic-gate 			close_dirfd();
27040Sstevel@tonic-gate 			return;
27050Sstevel@tonic-gate 		} else if (Adir || Aspec) {
27060Sstevel@tonic-gate 			if ((proc_file == F_SKIP) ||
27071134Sceastha 			    (Ofile = openout(G_p->g_dirfd)) < 0) {
27080Sstevel@tonic-gate 				data_in(P_SKIP);
27090Sstevel@tonic-gate 			} else {
27100Sstevel@tonic-gate 				data_in(P_PROC);
27110Sstevel@tonic-gate 			}
27120Sstevel@tonic-gate 			close_dirfd();
27130Sstevel@tonic-gate 			return;
27140Sstevel@tonic-gate 		}
27150Sstevel@tonic-gate 	}
27160Sstevel@tonic-gate 
27170Sstevel@tonic-gate 	if (Adir) {
27180Sstevel@tonic-gate 		if (proc_file != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
27190Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
27200Sstevel@tonic-gate 		}
27210Sstevel@tonic-gate 		close_dirfd();
27220Sstevel@tonic-gate 		if (Onecopy == 1) {
27230Sstevel@tonic-gate 			VERBOSE((Args & OCt), G_p->g_nam_p);
27240Sstevel@tonic-gate 		}
27250Sstevel@tonic-gate 		return;
27260Sstevel@tonic-gate 	}
27270Sstevel@tonic-gate 	if (G_p->g_nlink == 1 || (Hdr_type == TAR ||
27280Sstevel@tonic-gate 	    Hdr_type == USTAR)) {
27290Sstevel@tonic-gate 		if (Aspec) {
27300Sstevel@tonic-gate 			if (proc_file != F_SKIP && creat_spec(G_p->g_dirfd) > 0)
27310Sstevel@tonic-gate 				VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
27320Sstevel@tonic-gate 		} else {
27330Sstevel@tonic-gate 			if ((proc_file == F_SKIP) ||
27341134Sceastha 			    (Ofile = openout(G_p->g_dirfd)) < 0) {
27350Sstevel@tonic-gate 				data_in(P_SKIP);
27360Sstevel@tonic-gate 			} else {
27370Sstevel@tonic-gate 				data_in(P_PROC);
27380Sstevel@tonic-gate 			}
27390Sstevel@tonic-gate 		}
27400Sstevel@tonic-gate 		close_dirfd();
27410Sstevel@tonic-gate 		return;
27420Sstevel@tonic-gate 	}
27430Sstevel@tonic-gate 	close_dirfd();
27440Sstevel@tonic-gate 
27450Sstevel@tonic-gate 	tl_p = add_lnk(&ttl_p);
27460Sstevel@tonic-gate 	l_p = ttl_p;
27470Sstevel@tonic-gate 	if (l_p->L_cnt == l_p->L_gen.g_nlink)
27480Sstevel@tonic-gate 		cleanup = 1;
27490Sstevel@tonic-gate 	if (!Onecopy || G_p->g_attrnam_p != (char *)NULL) {
27500Sstevel@tonic-gate 		lnkem = (tl_p != l_p) ? 1 : 0;
27510Sstevel@tonic-gate 		G_p = &tl_p->L_gen;
27520Sstevel@tonic-gate 		if (proc_file == F_SKIP) {
27530Sstevel@tonic-gate 			data_in(P_SKIP);
27540Sstevel@tonic-gate 		} else {
27550Sstevel@tonic-gate 			if (open_dirfd() != 0)
27560Sstevel@tonic-gate 				return;
27570Sstevel@tonic-gate 			if (!lnkem) {
27580Sstevel@tonic-gate 				if (Aspec) {
27590Sstevel@tonic-gate 					if (creat_spec(G_p->g_dirfd) > 0)
27600Sstevel@tonic-gate 						VERBOSE((Args & (OCv | OCV)),
27610Sstevel@tonic-gate 						    G_p->g_nam_p);
27620Sstevel@tonic-gate 				} else if ((Ofile =
27631134Sceastha 				    openout(G_p->g_dirfd)) < 0) {
27640Sstevel@tonic-gate 					data_in(P_SKIP);
27650Sstevel@tonic-gate 					close_dirfd();
27660Sstevel@tonic-gate 					reclaim(l_p);
27670Sstevel@tonic-gate 				} else {
27680Sstevel@tonic-gate 					data_in(P_PROC);
27690Sstevel@tonic-gate 					close_dirfd();
27700Sstevel@tonic-gate 				}
27710Sstevel@tonic-gate 			} else {
27720Sstevel@tonic-gate 				/*
27730Sstevel@tonic-gate 				 * Are we linking an attribute?
27740Sstevel@tonic-gate 				 */
27750Sstevel@tonic-gate 				cwd = -1;
27760Sstevel@tonic-gate 				if (l_p->L_gen.g_attrnam_p != (char *)NULL) {
27770Sstevel@tonic-gate 					(void) strcpy(Lnkend_p,
27780Sstevel@tonic-gate 					    l_p->L_gen.g_attrnam_p);
27790Sstevel@tonic-gate 					(void) strcpy(Full_p,
27800Sstevel@tonic-gate 					    tl_p->L_gen.g_attrnam_p);
27810Sstevel@tonic-gate 					cwd = save_cwd();
27820Sstevel@tonic-gate 					(void) fchdir(G_p->g_dirfd);
27830Sstevel@tonic-gate 				} else {
27840Sstevel@tonic-gate 					(void) strcpy(Lnkend_p,
27850Sstevel@tonic-gate 					    l_p->L_gen.g_nam_p);
27860Sstevel@tonic-gate 					(void) strcpy(Full_p,
27870Sstevel@tonic-gate 					    tl_p->L_gen.g_nam_p);
27880Sstevel@tonic-gate 				}
27890Sstevel@tonic-gate 				(void) creat_lnk(G_p->g_dirfd,
27900Sstevel@tonic-gate 				    Lnkend_p, Full_p);
27910Sstevel@tonic-gate 				data_in(P_SKIP);
27920Sstevel@tonic-gate 				close_dirfd();
27930Sstevel@tonic-gate 				l_p->L_lnk_p = (struct Lnk *)NULL;
27940Sstevel@tonic-gate 				free(tl_p->L_gen.g_nam_p);
27950Sstevel@tonic-gate 				free(tl_p);
27960Sstevel@tonic-gate 				if (cwd != -1)
27970Sstevel@tonic-gate 					rest_cwd(cwd);
27980Sstevel@tonic-gate 			}
27990Sstevel@tonic-gate 		}
28000Sstevel@tonic-gate 	} else { /* Onecopy */
28010Sstevel@tonic-gate 		if (tl_p->L_gen.g_filesz)
28020Sstevel@tonic-gate 			cleanup = 1;
28030Sstevel@tonic-gate 		if (!cleanup) {
28040Sstevel@tonic-gate 			close_dirfd();
28050Sstevel@tonic-gate 			return; /* don't do anything yet */
28060Sstevel@tonic-gate 		}
28070Sstevel@tonic-gate 		tl_p = l_p;
28080Sstevel@tonic-gate 		/*
28090Sstevel@tonic-gate 		 * ckname will clear aclchar. We need to keep aclchar for
28100Sstevel@tonic-gate 		 * all links.
28110Sstevel@tonic-gate 		 */
28120Sstevel@tonic-gate 		savacl = aclchar;
28130Sstevel@tonic-gate 		while (tl_p != (struct Lnk *)NULL) {
28140Sstevel@tonic-gate 			G_p = &tl_p->L_gen;
28150Sstevel@tonic-gate 			aclchar = savacl;
28160Sstevel@tonic-gate 			if ((proc_file = ckname(1)) != F_SKIP) {
28170Sstevel@tonic-gate 				if (open_dirfd() != 0) {
28180Sstevel@tonic-gate 					return;
28190Sstevel@tonic-gate 				}
28200Sstevel@tonic-gate 				if (l_p->L_data) {
28210Sstevel@tonic-gate 					(void) creat_lnk(G_p->g_dirfd,
28220Sstevel@tonic-gate 					    l_p->L_gen.g_nam_p,
28230Sstevel@tonic-gate 					    G_p->g_nam_p);
28240Sstevel@tonic-gate 				} else if (Aspec) {
28250Sstevel@tonic-gate 					(void) creat_spec(G_p->g_dirfd);
28260Sstevel@tonic-gate 					l_p->L_data = 1;
28270Sstevel@tonic-gate 					VERBOSE((Args & (OCv | OCV)),
28280Sstevel@tonic-gate 					    G_p->g_nam_p);
28290Sstevel@tonic-gate 				} else if ((Ofile =
28300Sstevel@tonic-gate 				    openout(G_p->g_dirfd)) < 0) {
28310Sstevel@tonic-gate 					proc_file = F_SKIP;
28320Sstevel@tonic-gate 				} else {
28330Sstevel@tonic-gate 					data_in(P_PROC);
28340Sstevel@tonic-gate 					l_p->L_data = 1;
28350Sstevel@tonic-gate 				}
28360Sstevel@tonic-gate 			} /* (proc_file = ckname(1)) != F_SKIP */
28370Sstevel@tonic-gate 
28380Sstevel@tonic-gate 			tl_p = tl_p->L_lnk_p;
28390Sstevel@tonic-gate 
28400Sstevel@tonic-gate 			close_dirfd();
28410Sstevel@tonic-gate 
28420Sstevel@tonic-gate 			if (proc_file == F_SKIP && !cleanup) {
28430Sstevel@tonic-gate 				tl_p->L_nxt_p = l_p->L_nxt_p;
28440Sstevel@tonic-gate 				tl_p->L_bck_p = l_p->L_bck_p;
28450Sstevel@tonic-gate 				l_p->L_bck_p->L_nxt_p = tl_p;
28460Sstevel@tonic-gate 				l_p->L_nxt_p->L_bck_p = tl_p;
28470Sstevel@tonic-gate 				free(l_p->L_gen.g_nam_p);
28480Sstevel@tonic-gate 				free(l_p);
28490Sstevel@tonic-gate 			}
28500Sstevel@tonic-gate 		} /* tl_p->L_lnk_p != (struct Lnk *)NULL */
28510Sstevel@tonic-gate 		if (l_p->L_data == 0) {
28520Sstevel@tonic-gate 			data_in(P_SKIP);
28530Sstevel@tonic-gate 		}
28540Sstevel@tonic-gate 	}
28550Sstevel@tonic-gate 	if (cleanup) {
28560Sstevel@tonic-gate 		reclaim(l_p);
28570Sstevel@tonic-gate 	}
28580Sstevel@tonic-gate }
28590Sstevel@tonic-gate 
28600Sstevel@tonic-gate /*
28610Sstevel@tonic-gate  * file_out:  If the current file is not a special file (!Aspec) and it
28620Sstevel@tonic-gate  * is identical to the archive, skip it (do not archive the archive if it
28630Sstevel@tonic-gate  * is a regular file).  If creating a TARTYP (TAR or USTAR) archive, the first
28640Sstevel@tonic-gate  * time a link to a file is encountered, write the header and file out normally.
28650Sstevel@tonic-gate  * Subsequent links to this file put this file name in their t_linkname field.
28660Sstevel@tonic-gate  * Otherwise, links are handled in one of two ways, for the old headers
28670Sstevel@tonic-gate  * (i.e. binary and -c), linked files are written out as they are encountered.
28680Sstevel@tonic-gate  * For the new headers (ASC and CRC), links are saved up until all the links
28690Sstevel@tonic-gate  * to each file are found.  For a file with n links, write n - 1 headers with
28700Sstevel@tonic-gate  * g_filesz set to 0, write the final (nth) header with the correct g_filesz
28710Sstevel@tonic-gate  * value and write the data for the file to the archive.
28720Sstevel@tonic-gate  */
28730Sstevel@tonic-gate 
28740Sstevel@tonic-gate static
28750Sstevel@tonic-gate int
28760Sstevel@tonic-gate file_out(void)
28770Sstevel@tonic-gate {
28780Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
28790Sstevel@tonic-gate 	int cleanup = 0;
28800Sstevel@tonic-gate 	struct Lnk *ttl_p;
28810Sstevel@tonic-gate 
28820Sstevel@tonic-gate 	G_p = &Gen;
28830Sstevel@tonic-gate 	if (!Aspec && IDENT(SrcSt, ArchSt))
28840Sstevel@tonic-gate 		return (1); /* do not archive the archive if it's a reg file */
28850Sstevel@tonic-gate 	if (G_p->g_filesz > Max_offset) {
28860Sstevel@tonic-gate 		msg(ERR, "cpio: %s%s%s: too large to archive in current mode",
28870Sstevel@tonic-gate 		    G_p->g_nam_p,
28880Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
28890Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
28900Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
28910Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p);
28920Sstevel@tonic-gate 		return (1); /* do not archive if it's too big */
28930Sstevel@tonic-gate 	}
28940Sstevel@tonic-gate 	if (Hdr_type == TAR || Hdr_type == USTAR) { /* TAR and USTAR */
28950Sstevel@tonic-gate 		if (Adir) {
28960Sstevel@tonic-gate 			if (Gen.g_attrnam_p != (char *)NULL) {
28970Sstevel@tonic-gate 				write_xattr_hdr();
28980Sstevel@tonic-gate 			}
28990Sstevel@tonic-gate 			write_hdr(ARCHIVE_NORMAL, 0);
29000Sstevel@tonic-gate 			return (0);
29010Sstevel@tonic-gate 		}
29020Sstevel@tonic-gate 		if (G_p->g_nlink == 1) {
29030Sstevel@tonic-gate 			data_out();
29040Sstevel@tonic-gate 			return (0);
29050Sstevel@tonic-gate 		}
29060Sstevel@tonic-gate 		tl_p = add_lnk(&ttl_p);
29070Sstevel@tonic-gate 		l_p = ttl_p;
29080Sstevel@tonic-gate 		if (tl_p == l_p) { /* first link to this file encountered */
29090Sstevel@tonic-gate 			data_out();
29100Sstevel@tonic-gate 			return (0);
29110Sstevel@tonic-gate 		}
29120Sstevel@tonic-gate 		(void) strncpy(T_lname, l_p->L_gen.g_nam_p,
29130Sstevel@tonic-gate 		    l_p->L_gen.g_namesz);
29140Sstevel@tonic-gate 
29150Sstevel@tonic-gate 		/*
29160Sstevel@tonic-gate 		 * check if linkname is greater than 100 characters
29170Sstevel@tonic-gate 		 */
29180Sstevel@tonic-gate 		if (strlen(T_lname) > NAMSIZ) {
29190Sstevel@tonic-gate 			msg(EPOST, "cpio: %s: linkname %s is greater than %d",
29200Sstevel@tonic-gate 			    G_p->g_nam_p, T_lname, NAMSIZ);
29210Sstevel@tonic-gate 			return (1);
29220Sstevel@tonic-gate 		}
29230Sstevel@tonic-gate 
29240Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
29250Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), tl_p->L_gen.g_nam_p);
29260Sstevel@tonic-gate 
29270Sstevel@tonic-gate 		/* find the lnk entry in sublist, unlink it, and free it */
29280Sstevel@tonic-gate 		for (; ttl_p->L_lnk_p != NULL;
29290Sstevel@tonic-gate 		    ttl_p = ttl_p->L_lnk_p) {
29300Sstevel@tonic-gate 			if (ttl_p->L_lnk_p == tl_p) {
29310Sstevel@tonic-gate 				ttl_p->L_lnk_p = tl_p->L_lnk_p;
29320Sstevel@tonic-gate 				free(tl_p->L_gen.g_nam_p);
29330Sstevel@tonic-gate 				free(tl_p);
29340Sstevel@tonic-gate 				break;
29350Sstevel@tonic-gate 			}
29360Sstevel@tonic-gate 		}
29370Sstevel@tonic-gate 
29380Sstevel@tonic-gate 		return (0);
29390Sstevel@tonic-gate 	}
29400Sstevel@tonic-gate 	if (Adir) {
29410Sstevel@tonic-gate 		/*
29420Sstevel@tonic-gate 		 * ACL has been retrieved in getname().
29430Sstevel@tonic-gate 		 */
29440Sstevel@tonic-gate 		if (Pflag) {
29450Sstevel@tonic-gate 			char    *secinfo = NULL;
29460Sstevel@tonic-gate 			int	len = 0;
29470Sstevel@tonic-gate 
29480Sstevel@tonic-gate 			/* append security attributes */
2949789Sahrens 			if ((append_secattr(&secinfo, &len, aclp)) == -1)
29500Sstevel@tonic-gate 				msg(ERR, "can create security information");
29510Sstevel@tonic-gate 
29520Sstevel@tonic-gate 			/* call append_secattr() if more than one */
29530Sstevel@tonic-gate 
29540Sstevel@tonic-gate 			if (len > 0) {
29550Sstevel@tonic-gate 			/* write ancillary */
29560Sstevel@tonic-gate 				(void) write_hdr(ARCHIVE_ACL, (off_t)len);
29570Sstevel@tonic-gate 				(void) write_ancillary(secinfo, len);
29580Sstevel@tonic-gate 			}
29590Sstevel@tonic-gate 		}
29600Sstevel@tonic-gate 
29610Sstevel@tonic-gate 		if (Gen.g_attrnam_p != (char *)NULL) {
29620Sstevel@tonic-gate 			write_xattr_hdr();
29630Sstevel@tonic-gate 		}
29640Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
29650Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
29660Sstevel@tonic-gate 		return (0);
29670Sstevel@tonic-gate 	}
29680Sstevel@tonic-gate 	if (G_p->g_nlink == 1) {
29690Sstevel@tonic-gate 		data_out();
29700Sstevel@tonic-gate 		return (0);
29710Sstevel@tonic-gate 	} else {
29720Sstevel@tonic-gate 		tl_p = add_lnk(&ttl_p);
29730Sstevel@tonic-gate 		l_p = ttl_p;
29740Sstevel@tonic-gate 
29750Sstevel@tonic-gate 		if (l_p->L_cnt == l_p->L_gen.g_nlink)
29760Sstevel@tonic-gate 			cleanup = 1;
29770Sstevel@tonic-gate 		else if (Onecopy && G_p->g_attrnam_p == (char *)NULL) {
29780Sstevel@tonic-gate 			return (0); /* don't process data yet */
29790Sstevel@tonic-gate 		}
29800Sstevel@tonic-gate 	}
29810Sstevel@tonic-gate 	if (Onecopy && G_p->g_attrnam_p == (char *)NULL) {
29820Sstevel@tonic-gate 		tl_p = l_p;
29830Sstevel@tonic-gate 		while (tl_p->L_lnk_p != (struct Lnk *)NULL) {
29840Sstevel@tonic-gate 			G_p = &tl_p->L_gen;
29850Sstevel@tonic-gate 			G_p->g_filesz = (off_t)0;
29860Sstevel@tonic-gate 			/* one link with the acl is sufficient */
29870Sstevel@tonic-gate 			write_hdr(ARCHIVE_NORMAL, (off_t)0);
29880Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
29890Sstevel@tonic-gate 			tl_p = tl_p->L_lnk_p;
29900Sstevel@tonic-gate 		}
29910Sstevel@tonic-gate 		G_p = &tl_p->L_gen;
29920Sstevel@tonic-gate 		if (open_dirfd() != 0)
29930Sstevel@tonic-gate 			return (1);
29940Sstevel@tonic-gate 	}
29950Sstevel@tonic-gate 	/* old style: has acl and data for every link */
29960Sstevel@tonic-gate 	data_out();
29970Sstevel@tonic-gate 	if (cleanup)
29980Sstevel@tonic-gate 		reclaim(l_p);
29990Sstevel@tonic-gate 	return (0);
30000Sstevel@tonic-gate }
30010Sstevel@tonic-gate 
30020Sstevel@tonic-gate /*
30030Sstevel@tonic-gate  * file_pass:  If the -l option is set (link files when possible), and the
30040Sstevel@tonic-gate  * source and destination file systems are the same, link the source file
30050Sstevel@tonic-gate  * (G_p->g_nam_p) to the destination file (Fullnam) and return.  If not a
30060Sstevel@tonic-gate  * linked file, transfer the data.  Otherwise, the first link to a file
30070Sstevel@tonic-gate  * encountered is transferred normally and subsequent links are linked to it.
30080Sstevel@tonic-gate  */
30090Sstevel@tonic-gate 
30100Sstevel@tonic-gate static int
30110Sstevel@tonic-gate file_pass(void)
30120Sstevel@tonic-gate {
30130Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
30140Sstevel@tonic-gate 	struct Lnk *ttl_p;
30150Sstevel@tonic-gate 	char *save_name;
30160Sstevel@tonic-gate 	int size;
30170Sstevel@tonic-gate 	int cwd;
30180Sstevel@tonic-gate 	char *lfrom, *lto;
30190Sstevel@tonic-gate 
30200Sstevel@tonic-gate 	G_p = &Gen;
30210Sstevel@tonic-gate 
30220Sstevel@tonic-gate 	if (Adir && !(Args & OCd)) {
30230Sstevel@tonic-gate 		msg(ERR, "Use -d option to copy \"%s\"", G_p->g_nam_p);
30240Sstevel@tonic-gate 		return (FILE_PASS_ERR);
30250Sstevel@tonic-gate 	}
30260Sstevel@tonic-gate 
30270Sstevel@tonic-gate 	save_name = G_p->g_nam_p;
30280Sstevel@tonic-gate 
30290Sstevel@tonic-gate 	while (*(G_p->g_nam_p) == '/') {
30300Sstevel@tonic-gate 		G_p->g_nam_p++;
30310Sstevel@tonic-gate 	}
30320Sstevel@tonic-gate 
30330Sstevel@tonic-gate 	(void) strcpy(Full_p,
30340Sstevel@tonic-gate 	    (G_p->g_attrfnam_p == (char *)NULL) ?
30350Sstevel@tonic-gate 	    G_p->g_nam_p : G_p->g_attrfnam_p);
30360Sstevel@tonic-gate 
30370Sstevel@tonic-gate 	if (G_p->g_attrnam_p == (char *)NULL) {
30380Sstevel@tonic-gate 		G_p->g_passdirfd = open_dir(Fullnam_p);
30390Sstevel@tonic-gate 
30400Sstevel@tonic-gate 		if (G_p->g_passdirfd == -1) {
30410Sstevel@tonic-gate 			msg(ERRN,
30420Sstevel@tonic-gate 			    "Cannot open/create \"%s\"", Fullnam_p);
30430Sstevel@tonic-gate 			return (FILE_PASS_ERR);
30440Sstevel@tonic-gate 		}
30450Sstevel@tonic-gate 	} else {
30460Sstevel@tonic-gate 		G_p->g_passdirfd = attropen(Fullnam_p, ".", O_RDONLY);
30470Sstevel@tonic-gate 
30480Sstevel@tonic-gate 		if (G_p->g_passdirfd == -1) {
30490Sstevel@tonic-gate 			G_p->g_passdirfd = retry_attrdir_open(Fullnam_p);
30500Sstevel@tonic-gate 
30510Sstevel@tonic-gate 			if (G_p->g_passdirfd == -1) {
30520Sstevel@tonic-gate 				msg(ERRN,
30530Sstevel@tonic-gate 				    "Cannot open attribute directory of"
30540Sstevel@tonic-gate 				    " file \"%s\"", Fullnam_p);
30550Sstevel@tonic-gate 				return (FILE_PASS_ERR);
30560Sstevel@tonic-gate 			}
30570Sstevel@tonic-gate 		}
30580Sstevel@tonic-gate 	}
30590Sstevel@tonic-gate 
30600Sstevel@tonic-gate 	if (Args & OCl) {
30610Sstevel@tonic-gate 		/* We are linking back to the source directory. */
30620Sstevel@tonic-gate 
30630Sstevel@tonic-gate 		if (!Adir) {
30640Sstevel@tonic-gate 			char *existingfile = save_name;
30650Sstevel@tonic-gate 
30660Sstevel@tonic-gate 			if ((Args & OCL) && issymlink) {
30670Sstevel@tonic-gate 				/* We are chasing symlinks. */
30680Sstevel@tonic-gate 
30690Sstevel@tonic-gate 				if ((size = readlink(save_name, Symlnk_p,
30700Sstevel@tonic-gate 				    MAXPATHLEN)) < 0) {
30710Sstevel@tonic-gate 					msg(ERRN,
30720Sstevel@tonic-gate 					    "Cannot read symbolic link \"%s\"",
30730Sstevel@tonic-gate 					    save_name);
30740Sstevel@tonic-gate 					return (FILE_PASS_ERR);
30750Sstevel@tonic-gate 				}
30760Sstevel@tonic-gate 
30770Sstevel@tonic-gate 				Symlnk_p[size] = '\0';
30780Sstevel@tonic-gate 				existingfile = Symlnk_p;
30790Sstevel@tonic-gate 			}
30800Sstevel@tonic-gate 
30810Sstevel@tonic-gate 			if (G_p->g_attrnam_p == (char *)NULL) {
30820Sstevel@tonic-gate 				if (creat_lnk(G_p->g_passdirfd,
30830Sstevel@tonic-gate 				    existingfile, Fullnam_p) == 0) {
30840Sstevel@tonic-gate 					return (FILE_LINKED);
30850Sstevel@tonic-gate 				}
30860Sstevel@tonic-gate 			}
30870Sstevel@tonic-gate 		}
30880Sstevel@tonic-gate 	}
30890Sstevel@tonic-gate 
30900Sstevel@tonic-gate 	if ((G_p->g_mode & Ftype) == S_IFLNK && !(Args & OCL)) {
30910Sstevel@tonic-gate 		/* The archive file is a symlink. */
30920Sstevel@tonic-gate 
30930Sstevel@tonic-gate 		errno = 0;
30940Sstevel@tonic-gate 
30950Sstevel@tonic-gate 		if ((size = readlink(save_name, Symlnk_p, MAXPATHLEN)) < 0) {
30960Sstevel@tonic-gate 			msg(ERRN,
30970Sstevel@tonic-gate 			    "Cannot read symbolic link \"%s\"", save_name);
30980Sstevel@tonic-gate 			return (FILE_PASS_ERR);
30990Sstevel@tonic-gate 		}
31000Sstevel@tonic-gate 
31010Sstevel@tonic-gate 		errno = 0;
31020Sstevel@tonic-gate 		(void) missdir(Fullnam_p);
31030Sstevel@tonic-gate 		*(Symlnk_p + size) = '\0';
31040Sstevel@tonic-gate 
31050Sstevel@tonic-gate 		if (symlink(Symlnk_p, Fullnam_p) < 0) {
31060Sstevel@tonic-gate 			if (errno == EEXIST) {
31070Sstevel@tonic-gate 				if (openout(G_p->g_passdirfd) < 0) {
31081134Sceastha 					if (errno != EEXIST) {
31091134Sceastha 						msg(ERRN,
31101134Sceastha 						    "Cannot create \"%s\"",
31111134Sceastha 						    Fullnam_p);
31121134Sceastha 					}
31130Sstevel@tonic-gate 					return (FILE_PASS_ERR);
31140Sstevel@tonic-gate 				}
31150Sstevel@tonic-gate 			} else {
31160Sstevel@tonic-gate 				msg(ERRN, "Cannot create \"%s\"", Fullnam_p);
31170Sstevel@tonic-gate 				return (FILE_PASS_ERR);
31180Sstevel@tonic-gate 			}
31191134Sceastha 		} else {
31201134Sceastha 			if (Args & OCR) {
31211134Sceastha 				if (lchown(Fullnam_p, (int)Rpw_p->pw_uid,
31221134Sceastha 				    (int)Rpw_p->pw_gid) < 0) {
31231134Sceastha 					msg(ERRN,
31241134Sceastha 					    "Error during chown() of \"%s\"",
31251134Sceastha 					    Fullnam_p);
31261134Sceastha 				}
31271134Sceastha 			} else if ((lchown(Fullnam_p, (int)G_p->g_uid,
31281134Sceastha 			    (int)G_p->g_gid) < 0) && privileged) {
31290Sstevel@tonic-gate 				msg(ERRN,
31300Sstevel@tonic-gate 				    "Error during chown() of \"%s\"",
31310Sstevel@tonic-gate 				    Fullnam_p);
31320Sstevel@tonic-gate 			}
31330Sstevel@tonic-gate 		}
31340Sstevel@tonic-gate 
31350Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), Fullnam_p);
31360Sstevel@tonic-gate 		return (FILE_PASS_ERR);
31370Sstevel@tonic-gate 	}
31380Sstevel@tonic-gate 
31390Sstevel@tonic-gate 	if (!Adir && G_p->g_nlink > 1) {
31400Sstevel@tonic-gate 		/* The archive file has hard links. */
31410Sstevel@tonic-gate 
31420Sstevel@tonic-gate 		tl_p = add_lnk(&ttl_p);
31430Sstevel@tonic-gate 		l_p = ttl_p;
31440Sstevel@tonic-gate 
31450Sstevel@tonic-gate 		if (tl_p == l_p) {
31460Sstevel@tonic-gate 			/* The archive file was not found. */
31470Sstevel@tonic-gate 
31480Sstevel@tonic-gate 			G_p = &tl_p->L_gen;
31490Sstevel@tonic-gate 		} else {
31500Sstevel@tonic-gate 			/* The archive file was found. */
31510Sstevel@tonic-gate 
31520Sstevel@tonic-gate 			cwd = -1;
31530Sstevel@tonic-gate 
31540Sstevel@tonic-gate 			if (l_p->L_gen.g_attrnam_p != (char *)NULL) {
31550Sstevel@tonic-gate 				/* We are linking an attribute */
31560Sstevel@tonic-gate 
31570Sstevel@tonic-gate 				(void) strcpy(Lnkend_p, l_p->L_gen.g_attrnam_p);
31580Sstevel@tonic-gate 				cwd = save_cwd();
31590Sstevel@tonic-gate 				(void) fchdir(G_p->g_passdirfd);
31600Sstevel@tonic-gate 				lfrom = get_component(Lnknam_p);
31610Sstevel@tonic-gate 				lto = tl_p->L_gen.g_attrnam_p;
31620Sstevel@tonic-gate 			} else {
31630Sstevel@tonic-gate 				/* We are not linking an attribute */
31640Sstevel@tonic-gate 
31650Sstevel@tonic-gate 				(void) strcpy(Lnkend_p, l_p->L_gen.g_nam_p);
31660Sstevel@tonic-gate 				(void) strcpy(Full_p, tl_p->L_gen.g_nam_p);
31670Sstevel@tonic-gate 				lfrom = Lnknam_p;
31680Sstevel@tonic-gate 				lto = Fullnam_p;
31690Sstevel@tonic-gate 			}
31700Sstevel@tonic-gate 
31710Sstevel@tonic-gate 			(void) creat_lnk(G_p->g_passdirfd, lfrom, lto);
31720Sstevel@tonic-gate 
31730Sstevel@tonic-gate 			if (cwd) {
31740Sstevel@tonic-gate 				rest_cwd(cwd);
31750Sstevel@tonic-gate 			}
31760Sstevel@tonic-gate 
31770Sstevel@tonic-gate 			l_p->L_lnk_p = (struct Lnk *)NULL;
31780Sstevel@tonic-gate 			free(tl_p->L_gen.g_nam_p);
31790Sstevel@tonic-gate 			free(tl_p);
31800Sstevel@tonic-gate 
31810Sstevel@tonic-gate 			if (l_p->L_cnt == G_p->g_nlink) {
31820Sstevel@tonic-gate 				reclaim(l_p);
31830Sstevel@tonic-gate 			}
31840Sstevel@tonic-gate 
31850Sstevel@tonic-gate 			return (FILE_LINKED);
31860Sstevel@tonic-gate 		}
31870Sstevel@tonic-gate 	}
31880Sstevel@tonic-gate 
31890Sstevel@tonic-gate 	if (Adir || Aspec) {
31900Sstevel@tonic-gate 		/*
31910Sstevel@tonic-gate 		 * The archive file is a directory,  block special, char
31920Sstevel@tonic-gate 		 * special or a fifo.
31930Sstevel@tonic-gate 		 */
31940Sstevel@tonic-gate 
31950Sstevel@tonic-gate 		if (creat_spec(G_p->g_passdirfd) > 0) {
31960Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), Fullnam_p);
31970Sstevel@tonic-gate 		}
31980Sstevel@tonic-gate 	} else if ((Ofile = openout(G_p->g_passdirfd)) > 0) {
31990Sstevel@tonic-gate 		data_pass();
32000Sstevel@tonic-gate 	}
32010Sstevel@tonic-gate 
32020Sstevel@tonic-gate 	return (FILE_COPIED);
32030Sstevel@tonic-gate }
32040Sstevel@tonic-gate 
32050Sstevel@tonic-gate /*
32060Sstevel@tonic-gate  * flush_lnks: With new linked file handling, linked files are not archived
32070Sstevel@tonic-gate  * until all links have been collected.  When the end of the list of filenames
32080Sstevel@tonic-gate  * to archive has been reached, all files that did not encounter all their links
32090Sstevel@tonic-gate  * are written out with actual (encountered) link counts.  A file with n links
32100Sstevel@tonic-gate  * (that are archived) will be represented by n headers (one for each link (the
32110Sstevel@tonic-gate  * first n - 1 have g_filesz set to 0)) followed by the data for the file.
32120Sstevel@tonic-gate  */
32130Sstevel@tonic-gate 
32140Sstevel@tonic-gate static void
32150Sstevel@tonic-gate flush_lnks(void)
32160Sstevel@tonic-gate {
32170Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
32180Sstevel@tonic-gate 	off_t tfsize;
32190Sstevel@tonic-gate 
32200Sstevel@tonic-gate 	l_p = Lnk_hd.L_nxt_p;
32210Sstevel@tonic-gate 	while (l_p != &Lnk_hd) {
32220Sstevel@tonic-gate 		(void) strcpy(Gen.g_nam_p, l_p->L_gen.g_nam_p);
32230Sstevel@tonic-gate 		if (stat(Gen.g_nam_p, &SrcSt) == 0) { /* check if file exists */
32240Sstevel@tonic-gate 			tl_p = l_p;
32250Sstevel@tonic-gate 			(void) creat_hdr();
32260Sstevel@tonic-gate 			Gen.g_nlink = l_p->L_cnt; /* "actual" link count */
32270Sstevel@tonic-gate 			tfsize = Gen.g_filesz;
32280Sstevel@tonic-gate 			Gen.g_filesz = (off_t)0;
32290Sstevel@tonic-gate 			G_p = &Gen;
32300Sstevel@tonic-gate 			while (tl_p != (struct Lnk *)NULL) {
32310Sstevel@tonic-gate 				Gen.g_nam_p = tl_p->L_gen.g_nam_p;
32320Sstevel@tonic-gate 				Gen.g_namesz = tl_p->L_gen.g_namesz;
32330Sstevel@tonic-gate 				if (tl_p->L_lnk_p == (struct Lnk *)NULL) {
32340Sstevel@tonic-gate 					Gen.g_filesz = tfsize;
32350Sstevel@tonic-gate 					if (open_dirfd() != 0) {
32360Sstevel@tonic-gate 						break;
32370Sstevel@tonic-gate 					}
32380Sstevel@tonic-gate 					data_out();
32390Sstevel@tonic-gate 					break;
32400Sstevel@tonic-gate 				}
32410Sstevel@tonic-gate 				write_hdr(ARCHIVE_NORMAL,
32420Sstevel@tonic-gate 				    (off_t)0); /* header only */
32430Sstevel@tonic-gate 				VERBOSE((Args & (OCv | OCV)), Gen.g_nam_p);
32440Sstevel@tonic-gate 				tl_p = tl_p->L_lnk_p;
32450Sstevel@tonic-gate 			}
32460Sstevel@tonic-gate 			Gen.g_nam_p = Nam_p;
32470Sstevel@tonic-gate 		} else /* stat(Gen.g_nam_p, &SrcSt) == 0 */
32480Sstevel@tonic-gate 			msg(ERR, "\"%s%s%s\" has disappeared",
32490Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
32500Sstevel@tonic-gate 			    Gen.g_nam_p : Gen.g_attrfnam_p,
32510Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
32520Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
32530Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
32540Sstevel@tonic-gate 			    "" : Gen.g_attrnam_p);
32550Sstevel@tonic-gate 		tl_p = l_p;
32560Sstevel@tonic-gate 		l_p = l_p->L_nxt_p;
32570Sstevel@tonic-gate 		reclaim(tl_p);
32580Sstevel@tonic-gate 	} /* l_p != &Lnk_hd */
32590Sstevel@tonic-gate }
32600Sstevel@tonic-gate 
32610Sstevel@tonic-gate /*
32620Sstevel@tonic-gate  * gethdr: Get a header from the archive, validate it and check for the trailer.
32630Sstevel@tonic-gate  * Any user specified Hdr_type is ignored (set to NONE in main).  Hdr_type is
32640Sstevel@tonic-gate  * set appropriately after a valid header is found.  Unless the -k option is
32650Sstevel@tonic-gate  * set a corrupted header causes an exit with an error.  I/O errors during
32660Sstevel@tonic-gate  * examination of any part of the header cause gethdr to throw away any current
32670Sstevel@tonic-gate  * data and start over.  Other errors during examination of any part of the
32680Sstevel@tonic-gate  * header cause gethdr to advance one byte and continue the examination.
32690Sstevel@tonic-gate  */
32700Sstevel@tonic-gate 
32710Sstevel@tonic-gate static int
32720Sstevel@tonic-gate gethdr(void)
32730Sstevel@tonic-gate {
32740Sstevel@tonic-gate 	ushort_t ftype;
32750Sstevel@tonic-gate 	int hit = NONE, cnt = 0;
32760Sstevel@tonic-gate 	int goodhdr, hsize, offset;
32770Sstevel@tonic-gate 	int bswap = 0;
32780Sstevel@tonic-gate 	char *preptr;
32790Sstevel@tonic-gate 	int k = 0;
32800Sstevel@tonic-gate 	int j;
3281789Sahrens 	int error;
3282789Sahrens 	int aclcnt;
32830Sstevel@tonic-gate 
32840Sstevel@tonic-gate 	Gen.g_nam_p = Nam_p;
32850Sstevel@tonic-gate 	do { /* hit == NONE && (Args & OCk) && Buffr.b_cnt > 0 */
32860Sstevel@tonic-gate 		FILL(Hdrsz);
32870Sstevel@tonic-gate 		switch (Hdr_type) {
32880Sstevel@tonic-gate 		case NONE:
32890Sstevel@tonic-gate 		case BIN:
32900Sstevel@tonic-gate 			Binmag.b_byte[0] = Buffr.b_out_p[0];
32910Sstevel@tonic-gate 			Binmag.b_byte[1] = Buffr.b_out_p[1];
32920Sstevel@tonic-gate 			if ((Binmag.b_half == CMN_BIN) ||
32930Sstevel@tonic-gate 			    (Binmag.b_half == CMN_BBS)) {
32940Sstevel@tonic-gate 				hit = read_hdr(BIN);
32950Sstevel@tonic-gate 				if (Hdr_type == NONE)
32960Sstevel@tonic-gate 					bswap = 1;
32970Sstevel@tonic-gate 				hsize = HDRSZ + Gen.g_namesz;
32980Sstevel@tonic-gate 				break;
32990Sstevel@tonic-gate 			}
33000Sstevel@tonic-gate 			if (Hdr_type != NONE)
33010Sstevel@tonic-gate 				break;
33020Sstevel@tonic-gate 			/*FALLTHROUGH*/
33030Sstevel@tonic-gate 		case CHR:
33040Sstevel@tonic-gate 			if (!(strncmp(Buffr.b_out_p, CMS_CHR, CMS_LEN))) {
33050Sstevel@tonic-gate 				hit = read_hdr(CHR);
33060Sstevel@tonic-gate 				hsize = CHRSZ + Gen.g_namesz;
33070Sstevel@tonic-gate 				break;
33080Sstevel@tonic-gate 			}
33090Sstevel@tonic-gate 			if (Hdr_type != NONE)
33100Sstevel@tonic-gate 				break;
33110Sstevel@tonic-gate 			/*FALLTHROUGH*/
33120Sstevel@tonic-gate 		case ASC:
33130Sstevel@tonic-gate 			if (!(strncmp(Buffr.b_out_p, CMS_ASC, CMS_LEN))) {
33140Sstevel@tonic-gate 				hit = read_hdr(ASC);
33150Sstevel@tonic-gate 				hsize = ASCSZ + Gen.g_namesz;
33160Sstevel@tonic-gate 				Max_namesz = APATH;
33170Sstevel@tonic-gate 				break;
33180Sstevel@tonic-gate 			}
33190Sstevel@tonic-gate 			if (Hdr_type != NONE)
33200Sstevel@tonic-gate 				break;
33210Sstevel@tonic-gate 			/*FALLTHROUGH*/
33220Sstevel@tonic-gate 		case CRC:
33230Sstevel@tonic-gate 			if (!(strncmp(Buffr.b_out_p, CMS_CRC, CMS_LEN))) {
33240Sstevel@tonic-gate 				hit = read_hdr(CRC);
33250Sstevel@tonic-gate 				hsize = ASCSZ + Gen.g_namesz;
33260Sstevel@tonic-gate 				Max_namesz = APATH;
33270Sstevel@tonic-gate 				break;
33280Sstevel@tonic-gate 			}
33290Sstevel@tonic-gate 			if (Hdr_type != NONE)
33300Sstevel@tonic-gate 				break;
33310Sstevel@tonic-gate 			/*FALLTHROUGH*/
33320Sstevel@tonic-gate 
33330Sstevel@tonic-gate 		case BAR:
33340Sstevel@tonic-gate 			if (Hdr_p != NULL && strcmp(Hdr_p, "bar") == 0) {
33350Sstevel@tonic-gate 				Hdrsz = BARSZ;
33360Sstevel@tonic-gate 				FILL(Hdrsz);
33370Sstevel@tonic-gate 				if ((hit = read_hdr(BAR)) == NONE) {
33380Sstevel@tonic-gate 					Hdrsz = ASCSZ;
33390Sstevel@tonic-gate 					break;
33400Sstevel@tonic-gate 				}
33410Sstevel@tonic-gate 				hit = BAR;
33420Sstevel@tonic-gate 				hsize = BARSZ;
33430Sstevel@tonic-gate 				break;
33440Sstevel@tonic-gate 			}
33450Sstevel@tonic-gate 			/*FALLTHROUGH*/
33460Sstevel@tonic-gate 
33470Sstevel@tonic-gate 		case USTAR:
33480Sstevel@tonic-gate 			if (Hdr_p != NULL && strcmp(Hdr_p, "ustar") == 0) {
33490Sstevel@tonic-gate 				Hdrsz = TARSZ;
33500Sstevel@tonic-gate 				FILL(Hdrsz);
33510Sstevel@tonic-gate 				if ((hit = read_hdr(USTAR)) == NONE) {
33520Sstevel@tonic-gate 					Hdrsz = ASCSZ;
33530Sstevel@tonic-gate 					break;
33540Sstevel@tonic-gate 				}
33550Sstevel@tonic-gate 				hit = USTAR;
33560Sstevel@tonic-gate 				hsize = TARSZ;
33570Sstevel@tonic-gate 				break;
33580Sstevel@tonic-gate 			}
33590Sstevel@tonic-gate 			/*FALLTHROUGH*/
33600Sstevel@tonic-gate 		case TAR:
33610Sstevel@tonic-gate 			if (Hdr_p != NULL && strcmp(Hdr_p, "tar") == 0) {
33620Sstevel@tonic-gate 				Hdrsz = TARSZ;
33630Sstevel@tonic-gate 				FILL(Hdrsz);
33640Sstevel@tonic-gate 				if ((hit = read_hdr(TAR)) == NONE) {
33650Sstevel@tonic-gate 					Hdrsz = ASCSZ;
33660Sstevel@tonic-gate 					break;
33670Sstevel@tonic-gate 				}
33680Sstevel@tonic-gate 				hit = TAR;
33690Sstevel@tonic-gate 				hsize = TARSZ;
33700Sstevel@tonic-gate 				break;
33710Sstevel@tonic-gate 			}
33720Sstevel@tonic-gate 			/*FALLTHROUGH*/
33730Sstevel@tonic-gate 		default:
33740Sstevel@tonic-gate 			msg(EXT, "Impossible header type.");
33750Sstevel@tonic-gate 		} /* Hdr_type */
33760Sstevel@tonic-gate 
33770Sstevel@tonic-gate 		if (hit == TAR || hit == USTAR) {
33780Sstevel@tonic-gate 			Gen.g_nam_p = &nambuf[0];
33790Sstevel@tonic-gate 		}
33800Sstevel@tonic-gate 
33810Sstevel@tonic-gate 		if (hit != NONE) {
33820Sstevel@tonic-gate 			FILL(hsize);
33830Sstevel@tonic-gate 			goodhdr = 1;
33840Sstevel@tonic-gate 			if (Gen.g_filesz < (off_t)0 || Gen.g_namesz < 1)
33850Sstevel@tonic-gate 				goodhdr = 0;
33860Sstevel@tonic-gate 			if ((hit != USTAR) && (hit != TAR))
33870Sstevel@tonic-gate 				if (Gen.g_namesz - 1 > Max_namesz)
33880Sstevel@tonic-gate 					goodhdr = 0;
33890Sstevel@tonic-gate 			/* TAR and USTAR */
33900Sstevel@tonic-gate 			if ((hit == USTAR) || (hit == TAR)) {
33910Sstevel@tonic-gate 				if (*Gen.g_nam_p == '\0') { /* tar trailer */
33920Sstevel@tonic-gate 					goodhdr = 1;
33930Sstevel@tonic-gate 				} else {
33940Sstevel@tonic-gate 
33950Sstevel@tonic-gate 					G_p = &Gen;
33960Sstevel@tonic-gate 					if (G_p->g_cksum !=
33970Sstevel@tonic-gate 					    cksum(TARTYP, 0, NULL)) {
33980Sstevel@tonic-gate 						goodhdr = 0;
33990Sstevel@tonic-gate 						msg(ERR,
34000Sstevel@tonic-gate 						    "Bad header - checksum "
34010Sstevel@tonic-gate 						    "error.");
34020Sstevel@tonic-gate 					}
34030Sstevel@tonic-gate 				}
34040Sstevel@tonic-gate 			} else if (hit != BAR) { /* binary, -c, ASC and CRC */
34050Sstevel@tonic-gate 				if (Gen.g_nlink <= (ulong_t)0)
34060Sstevel@tonic-gate 					goodhdr = 0;
34070Sstevel@tonic-gate 				if (*(Buffr.b_out_p + hsize - 1) != '\0')
34080Sstevel@tonic-gate 					goodhdr = 0;
34090Sstevel@tonic-gate 			}
34100Sstevel@tonic-gate 			if (!goodhdr) {
34110Sstevel@tonic-gate 				hit = NONE;
34120Sstevel@tonic-gate 				if (!(Args & OCk))
34130Sstevel@tonic-gate 					break;
34140Sstevel@tonic-gate 				msg(ERR,
34150Sstevel@tonic-gate 				    "Corrupt header, file(s) may be lost.");
34160Sstevel@tonic-gate 			} else {
34170Sstevel@tonic-gate 				FILL(hsize);
34180Sstevel@tonic-gate 			}
34190Sstevel@tonic-gate 		} /* hit != NONE */
34200Sstevel@tonic-gate 		if (hit == NONE) {
34210Sstevel@tonic-gate 			Buffr.b_out_p++;
34220Sstevel@tonic-gate 			Buffr.b_cnt--;
34230Sstevel@tonic-gate 			if (!(Args & OCk))
34240Sstevel@tonic-gate 				break;
34250Sstevel@tonic-gate 			if (!cnt++)
34260Sstevel@tonic-gate 				msg(ERR, "Searching for magic number/header.");
34270Sstevel@tonic-gate 		}
34280Sstevel@tonic-gate 	} while (hit == NONE);
34290Sstevel@tonic-gate 	if (hit == NONE) {
34300Sstevel@tonic-gate 		if (Hdr_type == NONE)
34310Sstevel@tonic-gate 			msg(EXT, "Not a cpio file, bad header.");
34320Sstevel@tonic-gate 		else
34330Sstevel@tonic-gate 			msg(EXT, "Bad magic number/header.");
34340Sstevel@tonic-gate 	} else if (cnt > 0) {
34350Sstevel@tonic-gate 		msg(EPOST, "Re-synchronized on magic number/header.");
34360Sstevel@tonic-gate 	}
34370Sstevel@tonic-gate 	if (Hdr_type == NONE) {
34380Sstevel@tonic-gate 		Hdr_type = hit;
34390Sstevel@tonic-gate 		switch (Hdr_type) {
34400Sstevel@tonic-gate 		case BIN:
34410Sstevel@tonic-gate 			if (bswap)
34420Sstevel@tonic-gate 				Args |= BSM;
34430Sstevel@tonic-gate 			Hdrsz = HDRSZ;
34440Sstevel@tonic-gate 			Max_namesz = CPATH;
34450Sstevel@tonic-gate 			Pad_val = HALFWD;
34460Sstevel@tonic-gate 			Onecopy = 0;
34470Sstevel@tonic-gate 			break;
34480Sstevel@tonic-gate 		case CHR:
34490Sstevel@tonic-gate 			Hdrsz = CHRSZ;
34500Sstevel@tonic-gate 			Max_namesz = CPATH;
34510Sstevel@tonic-gate 			Pad_val = 0;
34520Sstevel@tonic-gate 			Onecopy = 0;
34530Sstevel@tonic-gate 			break;
34540Sstevel@tonic-gate 		case ASC:
34550Sstevel@tonic-gate 		case CRC:
34560Sstevel@tonic-gate 			Hdrsz = ASCSZ;
34570Sstevel@tonic-gate 			Max_namesz = APATH;
34580Sstevel@tonic-gate 			Pad_val = FULLWD;
34590Sstevel@tonic-gate 			Onecopy = 1;
34600Sstevel@tonic-gate 			break;
34610Sstevel@tonic-gate 		case USTAR:
34620Sstevel@tonic-gate 			Hdrsz = TARSZ;
34630Sstevel@tonic-gate 			Max_namesz = HNAMLEN - 1;
34640Sstevel@tonic-gate 			Pad_val = FULLBK;
34650Sstevel@tonic-gate 			Onecopy = 0;
34660Sstevel@tonic-gate 			break;
34670Sstevel@tonic-gate 		case BAR:
34680Sstevel@tonic-gate 		case TAR:
34690Sstevel@tonic-gate 			Hdrsz = TARSZ;
34700Sstevel@tonic-gate 			Max_namesz = TNAMLEN - 1;
34710Sstevel@tonic-gate 			Pad_val = FULLBK;
34720Sstevel@tonic-gate 			Onecopy = 0;
34730Sstevel@tonic-gate 			break;
34740Sstevel@tonic-gate 		default:
34750Sstevel@tonic-gate 			msg(EXT, "Impossible header type.");
34760Sstevel@tonic-gate 		} /* Hdr_type */
34770Sstevel@tonic-gate 	} /* Hdr_type == NONE */
34780Sstevel@tonic-gate 	if ((Hdr_type == USTAR) || (Hdr_type == TAR) ||
34790Sstevel@tonic-gate 	    (Hdr_type == BAR)) {			/* TAR, USTAR, BAR */
34800Sstevel@tonic-gate 		Gen.g_namesz = 0;
34810Sstevel@tonic-gate 		if (Gen.g_nam_p[0] == '\0')
34820Sstevel@tonic-gate 			return (0);
34830Sstevel@tonic-gate 		else {
34840Sstevel@tonic-gate 			preptr = &prebuf[0];
34850Sstevel@tonic-gate 			if (*preptr != (char)NULL) {
34860Sstevel@tonic-gate 				k = strlen(&prebuf[0]);
34870Sstevel@tonic-gate 				if (k < PRESIZ) {
34880Sstevel@tonic-gate 					(void) strcpy(&fullnam[0], &prebuf[0]);
34890Sstevel@tonic-gate 					j = 0;
34900Sstevel@tonic-gate 					fullnam[k++] = '/';
34910Sstevel@tonic-gate 					while ((j < NAMSIZ) && (&nambuf[j] !=
34920Sstevel@tonic-gate 					    (char)NULL)) {
34930Sstevel@tonic-gate 						fullnam[k] = nambuf[j];
34940Sstevel@tonic-gate 						k++; j++;
34950Sstevel@tonic-gate 					}
34960Sstevel@tonic-gate 					fullnam[k] = '\0';
34970Sstevel@tonic-gate 				} else if (k >= PRESIZ) {
34980Sstevel@tonic-gate 					k = 0;
34990Sstevel@tonic-gate 					while ((k < PRESIZ) && (prebuf[k] !=
35000Sstevel@tonic-gate 					    '\0')) {
35010Sstevel@tonic-gate 						fullnam[k] = prebuf[k];
35020Sstevel@tonic-gate 						k++;
35030Sstevel@tonic-gate 					}
35040Sstevel@tonic-gate 					fullnam[k++] = '/';
35050Sstevel@tonic-gate 					j = 0;
35060Sstevel@tonic-gate 					while ((j < NAMSIZ) && (nambuf[j] !=
35070Sstevel@tonic-gate 					    '\0')) {
35080Sstevel@tonic-gate 						fullnam[k] = nambuf[j];
35090Sstevel@tonic-gate 						k++; j++;
35100Sstevel@tonic-gate 					}
35110Sstevel@tonic-gate 					fullnam[k] = '\0';
35120Sstevel@tonic-gate 				}
35130Sstevel@tonic-gate 				Gen.g_nam_p = &fullnam[0];
35140Sstevel@tonic-gate 			} else
35150Sstevel@tonic-gate 				Gen.g_nam_p = &nambuf[0];
35160Sstevel@tonic-gate 
35170Sstevel@tonic-gate 			/*
35180Sstevel@tonic-gate 			 * initialize the buffer so that the prefix will not
35190Sstevel@tonic-gate 			 * applied to the next entry in the archive
35200Sstevel@tonic-gate 			 */
35210Sstevel@tonic-gate 			(void) memset(prebuf, 0, sizeof (prebuf));
35220Sstevel@tonic-gate 		}
35230Sstevel@tonic-gate 	} else if (Hdr_type != BAR) {
35240Sstevel@tonic-gate 		(void) memcpy(Gen.g_nam_p, Buffr.b_out_p + Hdrsz, Gen.g_namesz);
35250Sstevel@tonic-gate 		if (!(strcmp(Gen.g_nam_p, "TRAILER!!!")))
35260Sstevel@tonic-gate 			return (0);
35270Sstevel@tonic-gate 	}
35280Sstevel@tonic-gate 	offset = ((hsize + Pad_val) & ~Pad_val);
35290Sstevel@tonic-gate 	FILL(offset + Hdrsz);
35300Sstevel@tonic-gate 	Thdr_p = (union tblock *)Buffr.b_out_p;
35310Sstevel@tonic-gate 	Buffr.b_out_p += offset;
35320Sstevel@tonic-gate 	Buffr.b_cnt -= (off_t)offset;
35330Sstevel@tonic-gate 	ftype = Gen.g_mode & Ftype;
35340Sstevel@tonic-gate 
35350Sstevel@tonic-gate #if defined(O_XATTR)
35360Sstevel@tonic-gate 	/* extended attribute support */
35370Sstevel@tonic-gate 	if (((Gen.g_mode & S_IFMT) == _XATTR_CPIO_MODE) ||
35380Sstevel@tonic-gate 	    ((Hdr_type == USTAR || Hdr_type == TAR) &&
35390Sstevel@tonic-gate 	    Thdr_p->tbuf.t_typeflag == _XATTR_HDRTYPE)) {
35400Sstevel@tonic-gate 		if (xattrp != (struct xattr_buf *)NULL) {
35410Sstevel@tonic-gate 			if (xattrbadhead) {
35420Sstevel@tonic-gate 				free(xattrhead);
35430Sstevel@tonic-gate 				xattrp = NULL;
35440Sstevel@tonic-gate 				xattr_linkp = NULL;
35450Sstevel@tonic-gate 				xattrhead = NULL;
35460Sstevel@tonic-gate 				return (1);
35470Sstevel@tonic-gate 			}
35480Sstevel@tonic-gate 			if (Atflag == 0 && ((Args & OCt) == 0)) {
35490Sstevel@tonic-gate 				data_in(P_SKIP);
35500Sstevel@tonic-gate 				free(xattrhead);
35510Sstevel@tonic-gate 				xattrhead = NULL;
35520Sstevel@tonic-gate 				xattrp = NULL;
35530Sstevel@tonic-gate 				Gen.g_attrfnam_p = (char *)NULL;
35540Sstevel@tonic-gate 				Gen.g_attrnam_p = (char *)NULL;
35550Sstevel@tonic-gate 				return (2);
35560Sstevel@tonic-gate 			}
35570Sstevel@tonic-gate 
35580Sstevel@tonic-gate 			if (Gen.g_attrfnam_p != (char *)NULL) {
35590Sstevel@tonic-gate 				free(Gen.g_attrfnam_p);
35600Sstevel@tonic-gate 				Gen.g_attrfnam_p = (char *)NULL;
35610Sstevel@tonic-gate 			}
35620Sstevel@tonic-gate 			if (Gen.g_attrnam_p != (char *)NULL) {
35630Sstevel@tonic-gate 				free(Gen.g_attrnam_p);
35640Sstevel@tonic-gate 				Gen.g_attrnam_p = (char *)NULL;
35650Sstevel@tonic-gate 			}
35660Sstevel@tonic-gate 			if (Renam_p && Renam_p[0] != '\0') {
35670Sstevel@tonic-gate 				Gen.g_attrfnam_p = e_strdup(E_EXIT, Renam_p);
35680Sstevel@tonic-gate 			} else {
35690Sstevel@tonic-gate 				Gen.g_attrfnam_p = e_strdup(E_EXIT,
35700Sstevel@tonic-gate 				    xattrp->h_names);
35710Sstevel@tonic-gate 			}
35720Sstevel@tonic-gate 
35730Sstevel@tonic-gate 			Gen.g_attrnam_p = e_strdup(E_EXIT,
35740Sstevel@tonic-gate 			    xattrp->h_names + strlen(xattrp->h_names) + 1);
35750Sstevel@tonic-gate 
35760Sstevel@tonic-gate 			if (Hdr_type != USTAR && Hdr_type != TAR) {
35770Sstevel@tonic-gate 				Gen.g_mode = Gen.g_mode & (~_XATTR_CPIO_MODE);
35780Sstevel@tonic-gate 				Gen.g_mode |= attrmode(xattrp->h_typeflag);
35790Sstevel@tonic-gate 			} else if (Hdr_type == USTAR || Hdr_type == TAR) {
35800Sstevel@tonic-gate 				Thdr_p->tbuf.t_typeflag = xattrp->h_typeflag;
35810Sstevel@tonic-gate 			}
35820Sstevel@tonic-gate 			if (xattr_linkp != (struct xattr_buf *)NULL) {
35830Sstevel@tonic-gate 				if (Gen.g_linktoattrfnam_p != (char *)NULL) {
35840Sstevel@tonic-gate 					free(Gen.g_linktoattrfnam_p);
35850Sstevel@tonic-gate 					Gen.g_linktoattrfnam_p = NULL;
35860Sstevel@tonic-gate 				}
35870Sstevel@tonic-gate 				if (Gen.g_linktoattrnam_p != (char *)NULL) {
35880Sstevel@tonic-gate 					free(Gen.g_linktoattrnam_p);
35890Sstevel@tonic-gate 					Gen.g_linktoattrnam_p = NULL;
35900Sstevel@tonic-gate 				}
35910Sstevel@tonic-gate 				Gen.g_linktoattrfnam_p = e_strdup(E_EXIT,
35920Sstevel@tonic-gate 				    xattr_linkp->h_names);
35930Sstevel@tonic-gate 				Gen.g_linktoattrnam_p = e_strdup(E_EXIT,
35940Sstevel@tonic-gate 				    xattr_linkp->h_names +
35950Sstevel@tonic-gate 				    strlen(xattr_linkp->h_names) + 1);
35960Sstevel@tonic-gate 				xattr_linkp = NULL;
35970Sstevel@tonic-gate 			}
35980Sstevel@tonic-gate 			ftype = Gen.g_mode & Ftype;
35990Sstevel@tonic-gate 			Adir = ftype == S_IFDIR;
36000Sstevel@tonic-gate 			Aspec = (ftype == S_IFBLK ||
36010Sstevel@tonic-gate 			    ftype == S_IFCHR || ftype == S_IFIFO);
36020Sstevel@tonic-gate 
36030Sstevel@tonic-gate 			if (Gen.g_attrnam_p[0] == '.' &&
36040Sstevel@tonic-gate 			    Gen.g_attrnam_p[1] == '\0' &&
36050Sstevel@tonic-gate 			    xattrp->h_typeflag == DIRTYPE) {
36060Sstevel@tonic-gate 				Hiddendir = 1;
36070Sstevel@tonic-gate 			} else {
36080Sstevel@tonic-gate 				Hiddendir = 0;
36090Sstevel@tonic-gate 			}
36100Sstevel@tonic-gate 
36110Sstevel@tonic-gate 			free(xattrhead);
36120Sstevel@tonic-gate 			xattrhead = NULL;
36130Sstevel@tonic-gate 			xattrp = NULL;
36140Sstevel@tonic-gate 		} else {
36150Sstevel@tonic-gate 			if (xattrbadhead == 0) {
36160Sstevel@tonic-gate 				(void) read_xattr_hdr();
36170Sstevel@tonic-gate 				return (2);
36180Sstevel@tonic-gate 			}
36190Sstevel@tonic-gate 		}
36200Sstevel@tonic-gate 	}
36210Sstevel@tonic-gate #endif /* O_XATTR */
36220Sstevel@tonic-gate 
36230Sstevel@tonic-gate 	/* acl support: grab acl info */
36240Sstevel@tonic-gate 	if ((Gen.g_mode == SECMODE) || ((Hdr_type == USTAR ||
36250Sstevel@tonic-gate 	    Hdr_type == TAR) && Thdr_p->tbuf.t_typeflag == 'A')) {
36260Sstevel@tonic-gate 		/* this is an ancillary file */
36270Sstevel@tonic-gate 		off_t	bytes;
36280Sstevel@tonic-gate 		char	*secp;
36290Sstevel@tonic-gate 		int	pad;
36300Sstevel@tonic-gate 		int	cnt;
36310Sstevel@tonic-gate 		char	*tp;
36320Sstevel@tonic-gate 		int	attrsize;
36330Sstevel@tonic-gate 
36340Sstevel@tonic-gate 		if (Pflag) {
36350Sstevel@tonic-gate 			bytes = Gen.g_filesz;
36360Sstevel@tonic-gate 			secp = e_zalloc(E_EXIT, (uint_t)bytes);
36370Sstevel@tonic-gate 			tp = secp;
36380Sstevel@tonic-gate 
36390Sstevel@tonic-gate 			while (bytes > 0) {
36400Sstevel@tonic-gate 				cnt = (int)(bytes > CPIOBSZ) ? CPIOBSZ : bytes;
36410Sstevel@tonic-gate 				FILL(cnt);
36420Sstevel@tonic-gate 				(void) memcpy(tp, Buffr.b_out_p, cnt);
36430Sstevel@tonic-gate 				tp += cnt;
36440Sstevel@tonic-gate 				Buffr.b_out_p += cnt;
36450Sstevel@tonic-gate 				Buffr.b_cnt -= (off_t)cnt;
36460Sstevel@tonic-gate 				bytes -= (off_t)cnt;
36470Sstevel@tonic-gate 			}
36480Sstevel@tonic-gate 
36490Sstevel@tonic-gate 			pad = (Pad_val + 1 - (Gen.g_filesz & Pad_val)) &
36500Sstevel@tonic-gate 			    Pad_val;
36510Sstevel@tonic-gate 			if (pad != 0) {
36520Sstevel@tonic-gate 				FILL(pad);
36530Sstevel@tonic-gate 				Buffr.b_out_p += pad;
36540Sstevel@tonic-gate 				Buffr.b_cnt -= (off_t)pad;
36550Sstevel@tonic-gate 			}
36560Sstevel@tonic-gate 
36570Sstevel@tonic-gate 			/* got all attributes in secp */
36580Sstevel@tonic-gate 			tp = secp;
36590Sstevel@tonic-gate 			do {
36600Sstevel@tonic-gate 				attr = (struct sec_attr *)tp;
36610Sstevel@tonic-gate 				switch (attr->attr_type) {
36620Sstevel@tonic-gate 				case UFSD_ACL:
3663789Sahrens 				case ACE_ACL:
36640Sstevel@tonic-gate 					(void) sscanf(attr->attr_len, "%7lo",
36650Sstevel@tonic-gate 					    (ulong_t *)&aclcnt);
3666789Sahrens 					/* header is 8 */
36670Sstevel@tonic-gate 					attrsize = 8 +
36680Sstevel@tonic-gate 					    strlen(&attr->attr_info[0])
36690Sstevel@tonic-gate 					    + 1;
3670789Sahrens 
3671789Sahrens 					error =
3672789Sahrens 					    acl_fromtext(&attr->attr_info[0],
3673789Sahrens 					    &aclp);
3674789Sahrens 
3675789Sahrens 					if (error != 0) {
3676789Sahrens 						msg(ERR,
3677789Sahrens 						    "aclfromtext failed: %s",
3678789Sahrens 						    acl_strerror(error));
3679789Sahrens 						bytes -= attrsize;
36800Sstevel@tonic-gate 						break;
36810Sstevel@tonic-gate 					}
3682789Sahrens 
3683789Sahrens 					if (aclcnt != acl_cnt(aclp)) {
36840Sstevel@tonic-gate 						msg(ERR, "acl count error");
3685789Sahrens 						bytes -= attrsize;
36860Sstevel@tonic-gate 						break;
36870Sstevel@tonic-gate 					}
36880Sstevel@tonic-gate 					bytes -= attrsize;
36890Sstevel@tonic-gate 					break;
36900Sstevel@tonic-gate 
36910Sstevel@tonic-gate 				/* SunFed case goes here */
36920Sstevel@tonic-gate 
36930Sstevel@tonic-gate 				default:
36940Sstevel@tonic-gate 					msg(EXT, "unrecognized attr type");
36950Sstevel@tonic-gate 					break;
36960Sstevel@tonic-gate 			}
36970Sstevel@tonic-gate 			/* next attributes */
36980Sstevel@tonic-gate 			tp += attrsize;
36990Sstevel@tonic-gate 			} while (bytes > 0);
37000Sstevel@tonic-gate 			free(secp);
37010Sstevel@tonic-gate 		} else {
37020Sstevel@tonic-gate 			/* skip security info */
37030Sstevel@tonic-gate 			G_p = &Gen;
37040Sstevel@tonic-gate 			data_in(P_SKIP);
37050Sstevel@tonic-gate 		}
37060Sstevel@tonic-gate 		/*
37070Sstevel@tonic-gate 		 * We already got the file content, dont call file_in()
37080Sstevel@tonic-gate 		 * when return. The new return code(2) is used to
37090Sstevel@tonic-gate 		 *  indicate that.
37100Sstevel@tonic-gate 		 */
37110Sstevel@tonic-gate 		VERBOSE((Args & OCt), Gen.g_nam_p);
37120Sstevel@tonic-gate 		return (2);
37130Sstevel@tonic-gate 	} /* acl */
37140Sstevel@tonic-gate 
37150Sstevel@tonic-gate 	Adir = (ftype == S_IFDIR);
37160Sstevel@tonic-gate 	Aspec = (ftype == S_IFBLK || ftype == S_IFCHR || ftype == S_IFIFO);
37170Sstevel@tonic-gate 
37180Sstevel@tonic-gate 	/*
37190Sstevel@tonic-gate 	 * Skip any trailing slashes
37200Sstevel@tonic-gate 	 */
37210Sstevel@tonic-gate 	chop_endslashes(Gen.g_nam_p);
37220Sstevel@tonic-gate 	return (1);
37230Sstevel@tonic-gate }
37240Sstevel@tonic-gate 
37250Sstevel@tonic-gate /*
37260Sstevel@tonic-gate  * getname: Get file names for inclusion in the archive.  When end of file
37270Sstevel@tonic-gate  * on the input stream of file names is reached, flush the link buffer out.
37280Sstevel@tonic-gate  * For each filename, remove leading "./"s and multiple "/"s, and remove
37290Sstevel@tonic-gate  * any trailing newline "\n".  Finally, verify the existance of the file,
37300Sstevel@tonic-gate  * and call creat_hdr() to fill in the gen_hdr structure.
37310Sstevel@tonic-gate  */
37320Sstevel@tonic-gate 
37330Sstevel@tonic-gate static int
37340Sstevel@tonic-gate getname(void)
37350Sstevel@tonic-gate {
37360Sstevel@tonic-gate 	int goodfile = 0, lastchar, err;
37370Sstevel@tonic-gate 	char *s;
37380Sstevel@tonic-gate 	char *dir;
37390Sstevel@tonic-gate 
37400Sstevel@tonic-gate 	Gen.g_nam_p = Nam_p;
37410Sstevel@tonic-gate 
37420Sstevel@tonic-gate 	while (!goodfile) {
37430Sstevel@tonic-gate 		err = 0;
37440Sstevel@tonic-gate 
37450Sstevel@tonic-gate 		while ((s = fgets(Gen.g_nam_p, APATH+1, In_p))
37460Sstevel@tonic-gate 		    != NULL) {
37470Sstevel@tonic-gate 			lastchar = strlen(s) - 1;
37480Sstevel@tonic-gate 			issymlink = 0;
37490Sstevel@tonic-gate 
37500Sstevel@tonic-gate 			if (s[lastchar] != '\n') {
37510Sstevel@tonic-gate 				if (lastchar == APATH - 1) {
37520Sstevel@tonic-gate 					if (!err) {
37530Sstevel@tonic-gate 						msg(ERR,
37540Sstevel@tonic-gate 						    "%s name too long.",
37550Sstevel@tonic-gate 						    Nam_p);
37560Sstevel@tonic-gate 					}
37570Sstevel@tonic-gate 					goodfile = 0;
37580Sstevel@tonic-gate 					err = 1;
37590Sstevel@tonic-gate 				} else {
37600Sstevel@tonic-gate 					break;
37610Sstevel@tonic-gate 				}
37620Sstevel@tonic-gate 			} else {
37630Sstevel@tonic-gate 				s[lastchar] = '\0';
37640Sstevel@tonic-gate 				break;
37650Sstevel@tonic-gate 			}
37660Sstevel@tonic-gate 		}
37670Sstevel@tonic-gate 
37680Sstevel@tonic-gate 		if (s == (char *)NULL) {
37690Sstevel@tonic-gate 			if (Gen.g_dirfd != -1) {
37700Sstevel@tonic-gate 				(void) close(Gen.g_dirfd);
37710Sstevel@tonic-gate 				Gen.g_dirfd = -1;
37720Sstevel@tonic-gate 			}
37730Sstevel@tonic-gate 			if (Onecopy && (Args & OCo)) {
37740Sstevel@tonic-gate 				flush_lnks();
37750Sstevel@tonic-gate 			}
37760Sstevel@tonic-gate 			return (0);
37770Sstevel@tonic-gate 		}
37780Sstevel@tonic-gate 
37790Sstevel@tonic-gate 		while (*Gen.g_nam_p == '.' && Gen.g_nam_p[1] == '/') {
37800Sstevel@tonic-gate 			Gen.g_nam_p += 2;
37810Sstevel@tonic-gate 			while (*Gen.g_nam_p == '/')
37820Sstevel@tonic-gate 				Gen.g_nam_p++;
37830Sstevel@tonic-gate 		}
37840Sstevel@tonic-gate 
37850Sstevel@tonic-gate 		/*
37860Sstevel@tonic-gate 		 * Skip any trailing slashes
37870Sstevel@tonic-gate 		 */
37880Sstevel@tonic-gate 		chop_endslashes(Gen.g_nam_p);
37890Sstevel@tonic-gate 
37900Sstevel@tonic-gate 		/*
37910Sstevel@tonic-gate 		 * Figure out parent directory
37920Sstevel@tonic-gate 		 */
37930Sstevel@tonic-gate 
37940Sstevel@tonic-gate 		if (Gen.g_attrnam_p != (char *)NULL) {
37950Sstevel@tonic-gate 			if (Gen.g_dirfd != -1) {
37960Sstevel@tonic-gate 				(void) close(Gen.g_dirfd);
37970Sstevel@tonic-gate 			}
37980Sstevel@tonic-gate 			Gen.g_dirfd = attropen(Gen.g_attrfnam_p, ".", O_RDONLY);
37990Sstevel@tonic-gate 			if (Gen.g_dirfd == -1) {
38000Sstevel@tonic-gate 				msg(ERRN,
38010Sstevel@tonic-gate 				    "Cannot open attribute directory"
38020Sstevel@tonic-gate 				    " of file %s", Gen.g_attrfnam_p);
38030Sstevel@tonic-gate 				continue;
38040Sstevel@tonic-gate 			}
38050Sstevel@tonic-gate 		} else {
38060Sstevel@tonic-gate #ifdef O_XATTR
38070Sstevel@tonic-gate 			char dirpath[PATH_MAX];
38080Sstevel@tonic-gate 
38090Sstevel@tonic-gate 			get_parent(Gen.g_nam_p, dirpath);
38100Sstevel@tonic-gate 			if (Atflag) {
38110Sstevel@tonic-gate 				dir = dirpath;
38120Sstevel@tonic-gate 				if (Gen.g_dirfd != -1) {
38130Sstevel@tonic-gate 					(void) close(Gen.g_dirfd);
38140Sstevel@tonic-gate 				}
38150Sstevel@tonic-gate 				Gen.g_dirfd = open(dir, O_RDONLY);
38160Sstevel@tonic-gate 				if (Gen.g_dirfd == -1) {
38170Sstevel@tonic-gate 					msg(ERRN,
38180Sstevel@tonic-gate 					    "Cannot open directory %s", dir);
38190Sstevel@tonic-gate 					continue;
38200Sstevel@tonic-gate 				}
38210Sstevel@tonic-gate 			} else {
38220Sstevel@tonic-gate 				/*
38230Sstevel@tonic-gate 				 * g_dirpath is the pathname cache maintaining
38240Sstevel@tonic-gate 				 * the dirname which is currently opened.
38250Sstevel@tonic-gate 				 * We first check the g_dirpath to see if the
38260Sstevel@tonic-gate 				 * given dirname matches. If so, we don't need
38270Sstevel@tonic-gate 				 * to open the dir, but we can use the g_dirfd
38280Sstevel@tonic-gate 				 * as is if it is still available.
38290Sstevel@tonic-gate 				 */
38300Sstevel@tonic-gate 				dir = NULL;
38310Sstevel@tonic-gate 				if (Gen.g_dirpath == NULL ||
38320Sstevel@tonic-gate 				    Gen.g_dirfd == -1) {
38330Sstevel@tonic-gate 					/*
38340Sstevel@tonic-gate 					 * It's the first time or it has
38350Sstevel@tonic-gate 					 * all gone.
38360Sstevel@tonic-gate 					 */
38370Sstevel@tonic-gate 					dir = e_strdup(E_EXIT, dirpath);
38380Sstevel@tonic-gate 				} else {
38390Sstevel@tonic-gate 					if (strcmp(Gen.g_dirpath,
38400Sstevel@tonic-gate 					    dirpath) != 0) {
38410Sstevel@tonic-gate 						/* different directory */
38420Sstevel@tonic-gate 						dir = e_strdup(E_EXIT, dirpath);
38430Sstevel@tonic-gate 					}
38440Sstevel@tonic-gate 				}
38450Sstevel@tonic-gate 				if (dir != NULL) {
38460Sstevel@tonic-gate 					/*
38470Sstevel@tonic-gate 					 * We need to open the new directory.
38480Sstevel@tonic-gate 					 * discard the pathname and dirfd
38490Sstevel@tonic-gate 					 * for the previous directory.
38500Sstevel@tonic-gate 					 */
38510Sstevel@tonic-gate 					if (Gen.g_dirpath != NULL) {
38520Sstevel@tonic-gate 						free(Gen.g_dirpath);
38530Sstevel@tonic-gate 						Gen.g_dirpath = NULL;
38540Sstevel@tonic-gate 					}
38550Sstevel@tonic-gate 					if (Gen.g_dirfd != -1) {
38560Sstevel@tonic-gate 						(void) close(Gen.g_dirfd);
38570Sstevel@tonic-gate 					}
38580Sstevel@tonic-gate 					/* open the new dir */
38590Sstevel@tonic-gate 					Gen.g_dirfd = open(dir, O_RDONLY);
38600Sstevel@tonic-gate 					if (Gen.g_dirfd == -1) {
38610Sstevel@tonic-gate 						msg(ERRN, "Cannot open "
38621134Sceastha 						    "directory %s", dir);
38630Sstevel@tonic-gate 						continue;
38640Sstevel@tonic-gate 					}
38650Sstevel@tonic-gate 					Gen.g_dirpath = dir;
38660Sstevel@tonic-gate 				}
38670Sstevel@tonic-gate 			}
38680Sstevel@tonic-gate #else
38690Sstevel@tonic-gate 			Gen.g_dirfd = -1;
38700Sstevel@tonic-gate #endif
38710Sstevel@tonic-gate 		}
38720Sstevel@tonic-gate 
38730Sstevel@tonic-gate 		/* creat_hdr checks for USTAR filename length */
38740Sstevel@tonic-gate 
38750Sstevel@tonic-gate 		if (Hdr_type != USTAR && strlen(Gen.g_nam_p) >
38760Sstevel@tonic-gate 		    Max_namesz) {
38770Sstevel@tonic-gate 			if (!err) {
38780Sstevel@tonic-gate 				msg(ERR, "%s%s%s name too long.",
38790Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
38800Sstevel@tonic-gate 				    Nam_p : Gen.g_attrfnam_p,
38810Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
38820Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
38830Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
38840Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
38850Sstevel@tonic-gate 			}
38860Sstevel@tonic-gate 			goodfile = 0;
38870Sstevel@tonic-gate 			err = 1;
38880Sstevel@tonic-gate 		}
38890Sstevel@tonic-gate 
38900Sstevel@tonic-gate 		if (err) {
38910Sstevel@tonic-gate 			continue;
38920Sstevel@tonic-gate 		} else {
38930Sstevel@tonic-gate 			G_p = &Gen;
38940Sstevel@tonic-gate 			if (!LSTAT(Gen.g_dirfd, Gen.g_nam_p, &SrcSt)) {
38950Sstevel@tonic-gate 				goodfile = 1;
38960Sstevel@tonic-gate 
38970Sstevel@tonic-gate 				if ((SrcSt.st_mode & Ftype) == S_IFLNK) {
38980Sstevel@tonic-gate 					issymlink = 1;
38990Sstevel@tonic-gate 
39000Sstevel@tonic-gate 					if ((Args & OCL)) {
39010Sstevel@tonic-gate 						errno = 0;
39020Sstevel@tonic-gate 						if (STAT(Gen.g_dirfd,
39030Sstevel@tonic-gate 						    G_p->g_nam_p,
39040Sstevel@tonic-gate 						    &SrcSt) < 0) {
39050Sstevel@tonic-gate 							msg(ERRN,
39060Sstevel@tonic-gate 							    "Cannot follow"
39070Sstevel@tonic-gate 							    " \"%s%s%s\"",
39080Sstevel@tonic-gate 							    (Gen.g_attrnam_p ==
39090Sstevel@tonic-gate 							    (char *)NULL) ?
39100Sstevel@tonic-gate 							    Gen.g_nam_p :
39110Sstevel@tonic-gate 							    Gen.g_attrfnam_p,
39120Sstevel@tonic-gate 							    (Gen.g_attrnam_p ==
39130Sstevel@tonic-gate 							    (char *)NULL) ?
39140Sstevel@tonic-gate 							    "" :
39150Sstevel@tonic-gate 							    gettext(
39160Sstevel@tonic-gate 							    " Attribute "),
39170Sstevel@tonic-gate 							    (Gen.g_attrnam_p ==
39180Sstevel@tonic-gate 							    (char *)NULL) ?
39190Sstevel@tonic-gate 							    "" :
39200Sstevel@tonic-gate 							    Gen.g_attrnam_p);
39210Sstevel@tonic-gate 							goodfile = 0;
39220Sstevel@tonic-gate 						}
39230Sstevel@tonic-gate 					}
39240Sstevel@tonic-gate 				}
39250Sstevel@tonic-gate 
39260Sstevel@tonic-gate 				if (Use_old_stat) {
39270Sstevel@tonic-gate 					OldSt = convert_to_old_stat(&SrcSt,
39280Sstevel@tonic-gate 					    Gen.g_nam_p, Gen.g_attrnam_p);
39290Sstevel@tonic-gate 
39300Sstevel@tonic-gate 					if (OldSt == NULL) {
39310Sstevel@tonic-gate 						goodfile = 0;
39320Sstevel@tonic-gate 					}
39330Sstevel@tonic-gate 				}
39340Sstevel@tonic-gate 			} else {
39350Sstevel@tonic-gate 				msg(ERRN,
39360Sstevel@tonic-gate 				    "Error with fstatat() of \"%s%s%s\"",
39370Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
39380Sstevel@tonic-gate 				    Gen.g_nam_p : Gen.g_attrfnam_p,
39390Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
39400Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
39410Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
39420Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
39430Sstevel@tonic-gate 			}
39440Sstevel@tonic-gate 		}
39450Sstevel@tonic-gate 	}
39460Sstevel@tonic-gate 
39470Sstevel@tonic-gate 	/*
39480Sstevel@tonic-gate 	 * Get ACL info: dont bother allocating space if there are only
39490Sstevel@tonic-gate 	 * standard permissions, i.e. ACL count < 4
39500Sstevel@tonic-gate 	 */
39510Sstevel@tonic-gate 	if ((SrcSt.st_mode & Ftype) != S_IFLNK && Pflag) {
3952789Sahrens 		if (acl_get(Gen.g_nam_p, ACL_NO_TRIVIAL, &aclp) != 0)
39530Sstevel@tonic-gate 			msg(ERRN, "Error with acl() of \"%s\"", Gen.g_nam_p);
3954789Sahrens 	}
39550Sstevel@tonic-gate 	/* else: only traditional permissions, so proceed as usual */
39560Sstevel@tonic-gate 	if (creat_hdr())
39570Sstevel@tonic-gate 		return (1);
39580Sstevel@tonic-gate 	else return (2);
39590Sstevel@tonic-gate }
39600Sstevel@tonic-gate 
39610Sstevel@tonic-gate /*
39620Sstevel@tonic-gate  * getpats: Save any filenames/patterns specified as arguments.
39630Sstevel@tonic-gate  * Read additional filenames/patterns from the file specified by the
39640Sstevel@tonic-gate  * user.  The filenames/patterns must occur one per line.
39650Sstevel@tonic-gate  */
39660Sstevel@tonic-gate 
39670Sstevel@tonic-gate static void
39680Sstevel@tonic-gate getpats(int largc, char **largv)
39690Sstevel@tonic-gate {
39700Sstevel@tonic-gate 	char **t_pp;
39710Sstevel@tonic-gate 	size_t len;
39720Sstevel@tonic-gate 	unsigned numpat = largc, maxpat = largc + 2;
39730Sstevel@tonic-gate 
39740Sstevel@tonic-gate 	Pat_pp = e_zalloc(E_EXIT, maxpat * sizeof (char *));
39750Sstevel@tonic-gate 	t_pp = Pat_pp;
39760Sstevel@tonic-gate 	while (*largv) {
39770Sstevel@tonic-gate 		*t_pp = e_zalloc(E_EXIT, strlen(*largv) + 1);
39780Sstevel@tonic-gate 		(void) strcpy(*t_pp, *largv);
39790Sstevel@tonic-gate 		t_pp++;
39800Sstevel@tonic-gate 		largv++;
39810Sstevel@tonic-gate 	}
39820Sstevel@tonic-gate 	while (fgets(Nam_p, Max_namesz + 1, Ef_p) != (char *)NULL) {
39830Sstevel@tonic-gate 		if (numpat == maxpat - 1) {
39840Sstevel@tonic-gate 			maxpat += 10;
39850Sstevel@tonic-gate 			Pat_pp = e_realloc(E_EXIT, Pat_pp,
39860Sstevel@tonic-gate 			    maxpat * sizeof (char *));
39870Sstevel@tonic-gate 			t_pp = Pat_pp + numpat;
39880Sstevel@tonic-gate 		}
39890Sstevel@tonic-gate 		len = strlen(Nam_p); /* includes the \n */
39900Sstevel@tonic-gate 		*(Nam_p + len - 1) = '\0'; /* remove the \n */
39910Sstevel@tonic-gate 		*t_pp = e_zalloc(E_EXIT, len);
39920Sstevel@tonic-gate 		(void) strcpy(*t_pp, Nam_p);
39930Sstevel@tonic-gate 		t_pp++;
39940Sstevel@tonic-gate 		numpat++;
39950Sstevel@tonic-gate 	}
39960Sstevel@tonic-gate 	*t_pp = (char *)NULL;
39970Sstevel@tonic-gate }
39980Sstevel@tonic-gate 
39990Sstevel@tonic-gate static void
40000Sstevel@tonic-gate ioerror(int dir)
40010Sstevel@tonic-gate {
40020Sstevel@tonic-gate 	int t_errno;
40030Sstevel@tonic-gate 
40040Sstevel@tonic-gate 	t_errno = errno;
40050Sstevel@tonic-gate 	errno = 0;
40060Sstevel@tonic-gate 	if (fstat(Archive, &ArchSt) < 0)
40070Sstevel@tonic-gate 		msg(EXTN, "Error during stat() of archive");
40080Sstevel@tonic-gate 	errno = t_errno;
40090Sstevel@tonic-gate 	if ((ArchSt.st_mode & Ftype) != S_IFCHR) {
40100Sstevel@tonic-gate 		if (dir) {
40110Sstevel@tonic-gate 			if (errno == EFBIG)
40120Sstevel@tonic-gate 				msg(EXT, "ulimit reached for output file.");
40130Sstevel@tonic-gate 			else if (errno == ENOSPC)
40140Sstevel@tonic-gate 				msg(EXT, "No space left for output file.");
40150Sstevel@tonic-gate 			else
40160Sstevel@tonic-gate 				msg(EXTN, "I/O error - cannot continue");
40170Sstevel@tonic-gate 		} else
40180Sstevel@tonic-gate 			msg(EXT, "Unexpected end-of-file encountered.");
40190Sstevel@tonic-gate 	} else
40200Sstevel@tonic-gate 		msg(EXTN, "\007I/O error on \"%s\"", dir ? "output" : "input");
40210Sstevel@tonic-gate }
40220Sstevel@tonic-gate 
40230Sstevel@tonic-gate /*
40240Sstevel@tonic-gate  * matched: Determine if a filename matches the specified pattern(s).  If the
40250Sstevel@tonic-gate  * pattern is matched (the second return), return 0 if -f was specified, else
40260Sstevel@tonic-gate  * return != 0.  If the pattern is not matched (the first and third
40270Sstevel@tonic-gate  * returns), return 0 if -f was not specified, else return != 0.
40280Sstevel@tonic-gate  */
40290Sstevel@tonic-gate 
40300Sstevel@tonic-gate static int
40310Sstevel@tonic-gate matched(void)
40320Sstevel@tonic-gate {
40330Sstevel@tonic-gate 	char *str_p = G_p->g_nam_p;
40340Sstevel@tonic-gate 	char **pat_pp = Pat_pp;
40350Sstevel@tonic-gate 	int negatep, result;
40360Sstevel@tonic-gate 
40370Sstevel@tonic-gate 	/*
40380Sstevel@tonic-gate 	 * Check for attribute
40390Sstevel@tonic-gate 	 */
40400Sstevel@tonic-gate 	if (G_p->g_attrfnam_p != (char *)NULL)
40410Sstevel@tonic-gate 		str_p = G_p->g_attrfnam_p;
40420Sstevel@tonic-gate 
40430Sstevel@tonic-gate 	for (pat_pp = Pat_pp; *pat_pp; pat_pp++) {
40440Sstevel@tonic-gate 		negatep = (**pat_pp == '!');
40450Sstevel@tonic-gate 
40460Sstevel@tonic-gate 		result = fnmatch(negatep ? (*pat_pp+1) : *pat_pp, str_p, 0);
40470Sstevel@tonic-gate 
40480Sstevel@tonic-gate 		if (result != 0 && result != FNM_NOMATCH) {
40490Sstevel@tonic-gate 			msg(POST, "error matching file %s with pattern"
40500Sstevel@tonic-gate 			    " %s\n", str_p, *pat_pp);
40510Sstevel@tonic-gate 			return (Args & OCf);
40520Sstevel@tonic-gate 		}
40530Sstevel@tonic-gate 
40540Sstevel@tonic-gate 		if ((result == 0 && ! negatep) ||
40550Sstevel@tonic-gate 		    (result == FNM_NOMATCH && negatep)) {
40560Sstevel@tonic-gate 			/* match occured */
40570Sstevel@tonic-gate 			return (!(Args & OCf));
40580Sstevel@tonic-gate 		}
40590Sstevel@tonic-gate 	}
40600Sstevel@tonic-gate 	return (Args & OCf); /* not matched */
40610Sstevel@tonic-gate }
40620Sstevel@tonic-gate 
40630Sstevel@tonic-gate /*
40640Sstevel@tonic-gate  * missdir: Create missing directories for files.
40650Sstevel@tonic-gate  * (Possible future performance enhancement, if missdir is called, we know
40660Sstevel@tonic-gate  * that at least the very last directory of the path does not exist, therefore,
40670Sstevel@tonic-gate  * scan the path from the end
40680Sstevel@tonic-gate  */
40690Sstevel@tonic-gate 
40700Sstevel@tonic-gate static int
40710Sstevel@tonic-gate missdir(char *nam_p)
40720Sstevel@tonic-gate {
40730Sstevel@tonic-gate 	char *c_p;
40740Sstevel@tonic-gate 	int cnt = 2;
40750Sstevel@tonic-gate 	char *lastp;
40760Sstevel@tonic-gate 
40770Sstevel@tonic-gate 	if (*(c_p = nam_p) == '/') /* skip over 'root slash' */
40780Sstevel@tonic-gate 		c_p++;
40790Sstevel@tonic-gate 
40800Sstevel@tonic-gate 	lastp = c_p + strlen(nam_p) - 1;
40810Sstevel@tonic-gate 	if (*lastp == '/')
40820Sstevel@tonic-gate 		*lastp = '\0';
40830Sstevel@tonic-gate 
40840Sstevel@tonic-gate 	for (; *c_p; ++c_p) {
40850Sstevel@tonic-gate 		if (*c_p == '/') {
40860Sstevel@tonic-gate 			*c_p = '\0';
40870Sstevel@tonic-gate 			if (stat(nam_p, &DesSt) < 0) {
40880Sstevel@tonic-gate 				if (Args & OCd) {
40890Sstevel@tonic-gate 					cnt = mkdir(nam_p, Def_mode);
40900Sstevel@tonic-gate 					if (cnt != 0) {
40910Sstevel@tonic-gate 						*c_p = '/';
40920Sstevel@tonic-gate 						return (cnt);
40930Sstevel@tonic-gate 					}
40940Sstevel@tonic-gate 				} else {
40950Sstevel@tonic-gate 					msg(ERR, "Missing -d option.");
40960Sstevel@tonic-gate 					*c_p = '/';
40970Sstevel@tonic-gate 					return (-1);
40980Sstevel@tonic-gate 				}
40990Sstevel@tonic-gate 			}
41000Sstevel@tonic-gate 			*c_p = '/';
41010Sstevel@tonic-gate 		}
41020Sstevel@tonic-gate 	}
41030Sstevel@tonic-gate 	if (cnt == 2) /* the file already exists */
41040Sstevel@tonic-gate 		cnt = 0;
41050Sstevel@tonic-gate 	return (cnt);
41060Sstevel@tonic-gate }
41070Sstevel@tonic-gate 
41080Sstevel@tonic-gate /*
41090Sstevel@tonic-gate  * mklong: Convert two shorts into one long.  For VAX, Interdata ...
41100Sstevel@tonic-gate  */
41110Sstevel@tonic-gate 
41120Sstevel@tonic-gate static long
41130Sstevel@tonic-gate mklong(short v[])
41140Sstevel@tonic-gate {
41150Sstevel@tonic-gate 
41160Sstevel@tonic-gate 	union swpbuf swp_b;
41170Sstevel@tonic-gate 
41180Sstevel@tonic-gate 	swp_b.s_word = 1;
41190Sstevel@tonic-gate 	if (swp_b.s_byte[0]) {
41200Sstevel@tonic-gate 		swp_b.s_half[0] = v[1];
41210Sstevel@tonic-gate 		swp_b.s_half[1] = v[0];
41220Sstevel@tonic-gate 	} else {
41230Sstevel@tonic-gate 		swp_b.s_half[0] = v[0];
41240Sstevel@tonic-gate 		swp_b.s_half[1] = v[1];
41250Sstevel@tonic-gate 	}
41260Sstevel@tonic-gate 	return (swp_b.s_word);
41270Sstevel@tonic-gate }
41280Sstevel@tonic-gate 
41290Sstevel@tonic-gate /*
41300Sstevel@tonic-gate  * mkshort: Convert a long into 2 shorts, for VAX, Interdata ...
41310Sstevel@tonic-gate  */
41320Sstevel@tonic-gate 
41330Sstevel@tonic-gate static void
41340Sstevel@tonic-gate mkshort(short sval[], long v)
41350Sstevel@tonic-gate {
41360Sstevel@tonic-gate 	union swpbuf *swp_p, swp_b;
41370Sstevel@tonic-gate 
41380Sstevel@tonic-gate 	/* LINTED alignment */
41390Sstevel@tonic-gate 	swp_p = (union swpbuf *)sval;
41400Sstevel@tonic-gate 	swp_b.s_word = 1;
41410Sstevel@tonic-gate 	if (swp_b.s_byte[0]) {
41420Sstevel@tonic-gate 		swp_b.s_word = v;
41430Sstevel@tonic-gate 		swp_p->s_half[0] = swp_b.s_half[1];
41440Sstevel@tonic-gate 		swp_p->s_half[1] = swp_b.s_half[0];
41450Sstevel@tonic-gate 	} else {
41460Sstevel@tonic-gate 		swp_b.s_word = v;
41470Sstevel@tonic-gate 		swp_p->s_half[0] = swp_b.s_half[0];
41480Sstevel@tonic-gate 		swp_p->s_half[1] = swp_b.s_half[1];
41490Sstevel@tonic-gate 	}
41500Sstevel@tonic-gate }
41510Sstevel@tonic-gate 
41520Sstevel@tonic-gate /*
41530Sstevel@tonic-gate  * msg: Print either a message (no error) (POST), an error message with or
41540Sstevel@tonic-gate  * without the errno (ERRN or ERR), or print an error message with or without
41550Sstevel@tonic-gate  * the errno and exit (EXTN or EXT).
41560Sstevel@tonic-gate  */
41570Sstevel@tonic-gate 
41580Sstevel@tonic-gate static void
41590Sstevel@tonic-gate msg(int severity, const char *fmt, ...)
41600Sstevel@tonic-gate {
41610Sstevel@tonic-gate 	FILE *file_p;
41620Sstevel@tonic-gate 	va_list ap;
41630Sstevel@tonic-gate 
41640Sstevel@tonic-gate 	if ((Args & OCV) && Verbcnt) { /* clear current line of dots */
41650Sstevel@tonic-gate 		(void) fputc('\n', Out_p);
41660Sstevel@tonic-gate 		Verbcnt = 0;
41670Sstevel@tonic-gate 	}
41680Sstevel@tonic-gate 	va_start(ap, fmt);
41690Sstevel@tonic-gate 	if (severity == POST)
41700Sstevel@tonic-gate 		file_p = Out_p;
41710Sstevel@tonic-gate 	else
41720Sstevel@tonic-gate 		if (severity == EPOST)
41730Sstevel@tonic-gate 			file_p = Err_p;
41740Sstevel@tonic-gate 		else {
41750Sstevel@tonic-gate 			file_p = Err_p;
41760Sstevel@tonic-gate 			Error_cnt++;
41770Sstevel@tonic-gate 		}
41780Sstevel@tonic-gate 	(void) fflush(Out_p);
41790Sstevel@tonic-gate 	(void) fflush(Err_p);
41800Sstevel@tonic-gate 	if ((severity != POST) && (severity != EPOST))
41810Sstevel@tonic-gate 		(void) fprintf(file_p, "cpio: ");
41820Sstevel@tonic-gate 
41830Sstevel@tonic-gate 	/* gettext replaces version of string */
41840Sstevel@tonic-gate 
41850Sstevel@tonic-gate 	(void) vfprintf(file_p, gettext(fmt), ap);
41860Sstevel@tonic-gate 	if (severity == ERRN || severity == EXTN) {
41870Sstevel@tonic-gate 		(void) fprintf(file_p, ", errno %d, ", errno);
41880Sstevel@tonic-gate 		perror("");
41890Sstevel@tonic-gate 	} else
41900Sstevel@tonic-gate 		(void) fprintf(file_p, "\n");
41910Sstevel@tonic-gate 	(void) fflush(file_p);
41920Sstevel@tonic-gate 	va_end(ap);
41930Sstevel@tonic-gate 	if (severity == EXT || severity == EXTN) {
41940Sstevel@tonic-gate 		(void) fprintf(file_p, gettext("%d errors\n"), Error_cnt);
41950Sstevel@tonic-gate 		exit(EXIT_CODE);
41960Sstevel@tonic-gate 	}
41970Sstevel@tonic-gate }
41980Sstevel@tonic-gate 
41990Sstevel@tonic-gate /*
42000Sstevel@tonic-gate  * openout: Open files for output and set all necessary information.
42010Sstevel@tonic-gate  * If the u option is set (unconditionally overwrite existing files),
42020Sstevel@tonic-gate  * and the current file exists, get a temporary file name from mktemp(3C),
42030Sstevel@tonic-gate  * link the temporary file to the existing file, and remove the existing file.
42040Sstevel@tonic-gate  * Finally either creat(2), mkdir(2) or mknod(2) as appropriate.
42050Sstevel@tonic-gate  *
42060Sstevel@tonic-gate  */
42070Sstevel@tonic-gate 
42080Sstevel@tonic-gate static int
42090Sstevel@tonic-gate openout(int dirfd)
42100Sstevel@tonic-gate {
42110Sstevel@tonic-gate 	char *nam_p;
42120Sstevel@tonic-gate 	int cnt, result;
42130Sstevel@tonic-gate 
42140Sstevel@tonic-gate 	Do_rename = 0;	/* creat_tmp() may reset this */
42150Sstevel@tonic-gate 
42160Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
42170Sstevel@tonic-gate 		nam_p = G_p->g_attrnam_p;
42180Sstevel@tonic-gate 	} else {
42190Sstevel@tonic-gate 		if (Args & OCp) {
42200Sstevel@tonic-gate 			nam_p = Fullnam_p;
42210Sstevel@tonic-gate 		} else {
42220Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
42230Sstevel@tonic-gate 		}
42240Sstevel@tonic-gate 	}
42250Sstevel@tonic-gate 
42260Sstevel@tonic-gate 
42270Sstevel@tonic-gate 	if ((Max_filesz != RLIM_INFINITY) &&
42280Sstevel@tonic-gate 	    (Max_filesz < (G_p->g_filesz >> 9))) {
42290Sstevel@tonic-gate 		/* ... divided by 512 ... */
42300Sstevel@tonic-gate 		msg(ERR, "Skipping \"%s%s%s\": exceeds ulimit by %lld bytes",
42310Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
42320Sstevel@tonic-gate 		    nam_p : G_p->g_attrfnam_p,
42330Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
42340Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
42350Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
42360Sstevel@tonic-gate 		    "" : nam_p,
42370Sstevel@tonic-gate 		    (off_t)(G_p->g_filesz - (Max_filesz << 9)));
42380Sstevel@tonic-gate 		return (-1);
42390Sstevel@tonic-gate 	}
42400Sstevel@tonic-gate 
42410Sstevel@tonic-gate 	if (LSTAT(dirfd, nam_p, &DesSt) == 0) {
42420Sstevel@tonic-gate 		/*
42430Sstevel@tonic-gate 		 * A file by the same name exists.  Move it to a temporary
42440Sstevel@tonic-gate 		 * file.
42450Sstevel@tonic-gate 		 */
42460Sstevel@tonic-gate 
42470Sstevel@tonic-gate 		if (creat_tmp(nam_p) < 0) {
42480Sstevel@tonic-gate 			/*
42490Sstevel@tonic-gate 			 * We weren't able to create the temp file.  Report
42500Sstevel@tonic-gate 			 * failure.
42510Sstevel@tonic-gate 			 */
42520Sstevel@tonic-gate 
42530Sstevel@tonic-gate 			return (-1);
42540Sstevel@tonic-gate 		}
42550Sstevel@tonic-gate 	}
42560Sstevel@tonic-gate 
42570Sstevel@tonic-gate 	if (Do_rename) {
42580Sstevel@tonic-gate 		/* nam_p was changed by creat_tmp() above. */
42590Sstevel@tonic-gate 
42600Sstevel@tonic-gate 		if (Args & OCp) {
42610Sstevel@tonic-gate 			if (G_p->g_attrnam_p != (char *)NULL) {
42620Sstevel@tonic-gate 				nam_p = Attrfile_p;
42630Sstevel@tonic-gate 			} else {
42640Sstevel@tonic-gate 				nam_p = Fullnam_p;
42650Sstevel@tonic-gate 			}
42660Sstevel@tonic-gate 		} else {
42670Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
42680Sstevel@tonic-gate 		}
42690Sstevel@tonic-gate 	}
42700Sstevel@tonic-gate 
42710Sstevel@tonic-gate 	/*
42720Sstevel@tonic-gate 	 * This pile tries to create the file directly, and, if there is a
42730Sstevel@tonic-gate 	 * problem, creates missing directories, and then tries to create the
42740Sstevel@tonic-gate 	 * file again.  Two strikes and you're out.
42750Sstevel@tonic-gate 	 *
42760Sstevel@tonic-gate 	 * On XATTR system, the directory has already been created by
42770Sstevel@tonic-gate 	 * open_dirfd(), so error shouldn't happen in the loop. However,
42780Sstevel@tonic-gate 	 * on non-XATTR system, symlink/open may fail with ENOENT. In such
42790Sstevel@tonic-gate 	 * case, we go to create missing directories.
42800Sstevel@tonic-gate 	 */
42810Sstevel@tonic-gate 
42820Sstevel@tonic-gate 	cnt = 0;
42830Sstevel@tonic-gate 
42840Sstevel@tonic-gate 	do {
42850Sstevel@tonic-gate 		errno = 0;
42860Sstevel@tonic-gate 
42870Sstevel@tonic-gate 		if (Hdr_type == TAR && Thdr_p->tbuf.t_typeflag == SYMTYPE) {
42880Sstevel@tonic-gate 			/* The archive file is a TAR symlink. */
42890Sstevel@tonic-gate 			if ((result =
42900Sstevel@tonic-gate 			    symlink(Thdr_p->tbuf.t_linkname, nam_p)) >= 0) {
42910Sstevel@tonic-gate 				cnt = 0;
42920Sstevel@tonic-gate 				if (Over_p != NULL) {
42930Sstevel@tonic-gate 					(void) unlinkat(dirfd,
42940Sstevel@tonic-gate 					    get_component(Over_p), 0);
42950Sstevel@tonic-gate 					*Over_p = '\0';
42960Sstevel@tonic-gate 				}
42970Sstevel@tonic-gate 				break;
42980Sstevel@tonic-gate 			} else if (errno != ENOENT) {
42990Sstevel@tonic-gate 				/* The attempt to symlink failed. */
43000Sstevel@tonic-gate 				msg(ERRN,
43010Sstevel@tonic-gate 				    "Cannot create symbolic link \"%s\" -> "
43020Sstevel@tonic-gate 				    "\"%s\"",
43030Sstevel@tonic-gate 				    Thdr_p->tbuf.t_linkname, nam_p);
43040Sstevel@tonic-gate 
43050Sstevel@tonic-gate 				if (*Over_p != '\0') {
43060Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
43070Sstevel@tonic-gate 				}
43080Sstevel@tonic-gate 				return (-1);
43090Sstevel@tonic-gate 			}
43100Sstevel@tonic-gate 		} else if (Hdr_type == BAR && bar_linkflag == SYMTYPE) {
43110Sstevel@tonic-gate 			if ((result = symlink(bar_linkname, nam_p)) >= 0) {
43120Sstevel@tonic-gate 				cnt = 0;
43130Sstevel@tonic-gate 				if (Over_p != NULL) {
43140Sstevel@tonic-gate 					(void) unlinkat(dirfd,
43150Sstevel@tonic-gate 					    get_component(Over_p), 0);
43160Sstevel@tonic-gate 					*Over_p = '\0';
43170Sstevel@tonic-gate 				}
43180Sstevel@tonic-gate 				break;
43190Sstevel@tonic-gate 			} else if (errno != ENOENT) {
43200Sstevel@tonic-gate 				/* The attempt to symlink failed. */
43210Sstevel@tonic-gate 				msg(ERRN,
43220Sstevel@tonic-gate 				    "Cannot create symbolic link \"%s\" -> "
43230Sstevel@tonic-gate 				    "\"%s\"",
43240Sstevel@tonic-gate 				    bar_linkname, nam_p);
43250Sstevel@tonic-gate 				if (*Over_p != '\0') {
43260Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
43270Sstevel@tonic-gate 				}
43280Sstevel@tonic-gate 				return (-1);
43290Sstevel@tonic-gate 			}
43300Sstevel@tonic-gate 		} else if ((G_p->g_mode & Ftype) == S_IFLNK) {
43310Sstevel@tonic-gate 			if ((!(Args & OCp)) && !(Hdr_type == USTAR)) {
43320Sstevel@tonic-gate 				(void) strncpy(Symlnk_p,
43330Sstevel@tonic-gate 				    Buffr.b_out_p, G_p->g_filesz);
43340Sstevel@tonic-gate 				*(Symlnk_p + G_p->g_filesz) = '\0';
43350Sstevel@tonic-gate 			} else if ((!(Args & OCp)) && (Hdr_type == USTAR)) {
43360Sstevel@tonic-gate 				Symlnk_p[NAMSIZ] = '\0';
43370Sstevel@tonic-gate 				(void) strncpy(Symlnk_p,
43380Sstevel@tonic-gate 				    &Thdr_p->tbuf.t_linkname[0], NAMSIZ);
43390Sstevel@tonic-gate 			}
43400Sstevel@tonic-gate 			if ((result = symlink(Symlnk_p, nam_p)) >= 0) {
43410Sstevel@tonic-gate 				cnt = 0;
43420Sstevel@tonic-gate 				if (Over_p != NULL) {
43430Sstevel@tonic-gate 					(void) unlinkat(dirfd,
43440Sstevel@tonic-gate 					    get_component(Over_p), 0);
43450Sstevel@tonic-gate 					*Over_p = '\0';
43460Sstevel@tonic-gate 				}
43470Sstevel@tonic-gate 				break;
43480Sstevel@tonic-gate 			} else if (errno != ENOENT) {
43490Sstevel@tonic-gate 				/* The attempt to symlink failed. */
43500Sstevel@tonic-gate 				msg(ERRN,
43510Sstevel@tonic-gate 				    "Cannot create symbolic link \"%s\" -> "
43520Sstevel@tonic-gate 				    "\"%s\"",
43530Sstevel@tonic-gate 				    Symlnk_p, nam_p);
43540Sstevel@tonic-gate 
43550Sstevel@tonic-gate 				if (*Over_p != '\0') {
43560Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
43570Sstevel@tonic-gate 				}
43580Sstevel@tonic-gate 				return (-1);
43590Sstevel@tonic-gate 			}
43600Sstevel@tonic-gate 		} else {
43610Sstevel@tonic-gate 			if ((result = openat(dirfd, get_component(nam_p),
43620Sstevel@tonic-gate 			    O_CREAT|O_RDWR|O_TRUNC, (int)G_p->g_mode)) >= 0) {
43630Sstevel@tonic-gate 				/* acl support */
4364789Sahrens 				acl_is_set = 0;
43650Sstevel@tonic-gate 				if (Pflag && aclp != NULL) {
4366789Sahrens 					if (facl_set(result, aclp) < 0) {
43670Sstevel@tonic-gate 						msg(ERRN,
43680Sstevel@tonic-gate 						    "\"%s\": failed to set acl",
43690Sstevel@tonic-gate 						    nam_p);
43700Sstevel@tonic-gate 					} else {
4371789Sahrens 						acl_is_set = 1;
43720Sstevel@tonic-gate 					}
4373789Sahrens 					acl_free(aclp);
43740Sstevel@tonic-gate 					aclp = NULL;
43750Sstevel@tonic-gate 				}
43760Sstevel@tonic-gate 				cnt = 0;
43770Sstevel@tonic-gate 				break;
43780Sstevel@tonic-gate 			} else if (errno != ENOENT) {
43790Sstevel@tonic-gate 				/* The attempt to open failed. */
43800Sstevel@tonic-gate 				msg(ERRN, "Cannot open file \"%s\"", nam_p);
43810Sstevel@tonic-gate 				if (*Over_p != '\0') {
43820Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
43830Sstevel@tonic-gate 				}
43840Sstevel@tonic-gate 				return (-1);
43850Sstevel@tonic-gate 			}
43860Sstevel@tonic-gate 		}
43870Sstevel@tonic-gate 		cnt++;
43880Sstevel@tonic-gate 	} while (cnt < 2 && missdir(nam_p) == 0);
43890Sstevel@tonic-gate 
43900Sstevel@tonic-gate 	switch (cnt) {
43910Sstevel@tonic-gate 	case 0:
43920Sstevel@tonic-gate 		if ((Args & OCi) && (Hdr_type == USTAR)) {
43930Sstevel@tonic-gate 			setpasswd(nam_p);
43940Sstevel@tonic-gate 		}
43950Sstevel@tonic-gate 		if ((G_p->g_mode & Ftype) == S_IFLNK ||
43960Sstevel@tonic-gate 		    (Hdr_type == BAR && bar_linkflag == SYMTYPE)) {
43971134Sceastha 			if (Args & OCR) {
43981134Sceastha 				if (fchownat(dirfd,
43991134Sceastha 				    get_component(nam_p),
44001134Sceastha 				    (int)Rpw_p->pw_uid,
44011134Sceastha 				    (int)Rpw_p->pw_gid,
44021134Sceastha 				    AT_SYMLINK_NOFOLLOW) < 0) {
44030Sstevel@tonic-gate 					msg(ERRN,
44040Sstevel@tonic-gate 					    "Error during chown() of "
44050Sstevel@tonic-gate 					    "\"%s%s%s\"",
44061134Sceastha 					    (G_p->g_attrnam_p ==
44071134Sceastha 					    (char *)NULL) ?
44080Sstevel@tonic-gate 					    nam_p : G_p->g_attrfnam_p,
44091134Sceastha 					    (G_p->g_attrnam_p ==
44101134Sceastha 					    (char *)NULL) ?
44110Sstevel@tonic-gate 					    "" : gettext(" Attribute "),
44121134Sceastha 					    (G_p->g_attrnam_p ==
44131134Sceastha 					    (char *)NULL) ?
44140Sstevel@tonic-gate 					    "" : nam_p);
44150Sstevel@tonic-gate 				}
44161134Sceastha 			} else if ((fchownat(dirfd, get_component(nam_p),
44171134Sceastha 			    (int)G_p->g_uid, (int)G_p->g_gid,
44181134Sceastha 			    AT_SYMLINK_NOFOLLOW) < 0) && privileged) {
44190Sstevel@tonic-gate 				msg(ERRN,
44200Sstevel@tonic-gate 				    "Error during chown() of \"%s%s%s\"",
44210Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
44220Sstevel@tonic-gate 				    nam_p : G_p->g_attrfnam_p,
44230Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
44240Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
44250Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
44260Sstevel@tonic-gate 				    "" : nam_p);
44270Sstevel@tonic-gate 			}
44280Sstevel@tonic-gate 		}
44290Sstevel@tonic-gate 		break;
44300Sstevel@tonic-gate 
44310Sstevel@tonic-gate 	case 1:
44320Sstevel@tonic-gate 		if (Do_rename) {
44330Sstevel@tonic-gate 			msg(ERRN, "Cannot create directory for \"%s%s%s\"",
44340Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? Over_p :
44350Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
44360Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
44370Sstevel@tonic-gate 			    gettext(" Attribute "),
44380Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" : Over_p);
44390Sstevel@tonic-gate 		} else {
44400Sstevel@tonic-gate 			msg(ERRN, "Cannot create directory for \"%s%s%s\"",
44410Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? nam_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) ? "" : nam_p);
44460Sstevel@tonic-gate 		}
44470Sstevel@tonic-gate 		break;
44480Sstevel@tonic-gate 
44490Sstevel@tonic-gate 	case 2:
44500Sstevel@tonic-gate 		if (Do_rename) {
44510Sstevel@tonic-gate 			msg(ERRN, "Cannot create \"%s%s%s\"",
44520Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? Over_p :
44530Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
44540Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
44550Sstevel@tonic-gate 			    gettext(" Attribute "),
44560Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
44570Sstevel@tonic-gate 			    Over_p);
44580Sstevel@tonic-gate 		} else {
44590Sstevel@tonic-gate 			msg(ERRN, "Cannot create \"%s%s%s\"",
44600Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? nam_p :
44610Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
44620Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
44630Sstevel@tonic-gate 			    gettext(" Attribute "),
44640Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
44650Sstevel@tonic-gate 			    nam_p);
44660Sstevel@tonic-gate 		}
44670Sstevel@tonic-gate 		break;
44680Sstevel@tonic-gate 
44690Sstevel@tonic-gate 	default:
44700Sstevel@tonic-gate 		msg(EXT, "Impossible case.");
44710Sstevel@tonic-gate 	}
44720Sstevel@tonic-gate 
44730Sstevel@tonic-gate 	Finished = 0;
44740Sstevel@tonic-gate 	return (result);
44750Sstevel@tonic-gate }
44760Sstevel@tonic-gate 
44770Sstevel@tonic-gate /*
44780Sstevel@tonic-gate  * read_hdr: Transfer headers from the selected format
44790Sstevel@tonic-gate  * in the archive I/O buffer to the generic structure.
44800Sstevel@tonic-gate  */
44810Sstevel@tonic-gate 
44820Sstevel@tonic-gate static
44830Sstevel@tonic-gate int
44840Sstevel@tonic-gate read_hdr(int hdr)
44850Sstevel@tonic-gate {
44860Sstevel@tonic-gate 	int rv = NONE;
44870Sstevel@tonic-gate 	major_t maj, rmaj;
44880Sstevel@tonic-gate 	minor_t min, rmin;
44890Sstevel@tonic-gate 	char tmpnull;
44900Sstevel@tonic-gate 	static int bar_read_cnt = 0;
44910Sstevel@tonic-gate 
44920Sstevel@tonic-gate 	if (hdr != BAR) {
44930Sstevel@tonic-gate 		if (Buffr.b_end_p != (Buffr.b_out_p + Hdrsz)) {
44940Sstevel@tonic-gate 			tmpnull = *(Buffr.b_out_p + Hdrsz);
44950Sstevel@tonic-gate 			*(Buffr.b_out_p + Hdrsz) = '\0';
44960Sstevel@tonic-gate 		}
44970Sstevel@tonic-gate 	}
44980Sstevel@tonic-gate 
44990Sstevel@tonic-gate 	switch (hdr) {
45000Sstevel@tonic-gate 	case BIN:
45010Sstevel@tonic-gate 		(void) memcpy(&Hdr, Buffr.b_out_p, HDRSZ);
45020Sstevel@tonic-gate 		if (Hdr.h_magic == (short)CMN_BBS) {
45030Sstevel@tonic-gate 			swap((char *)&Hdr, HDRSZ);
45040Sstevel@tonic-gate 		}
45050Sstevel@tonic-gate 		Gen.g_magic = Hdr.h_magic;
45060Sstevel@tonic-gate 		Gen.g_mode = Hdr.h_mode;
45070Sstevel@tonic-gate 		Gen.g_uid = Hdr.h_uid;
45080Sstevel@tonic-gate 		Gen.g_gid = Hdr.h_gid;
45090Sstevel@tonic-gate 		Gen.g_nlink = Hdr.h_nlink;
45100Sstevel@tonic-gate 		Gen.g_mtime = mklong(Hdr.h_mtime);
45110Sstevel@tonic-gate 		Gen.g_ino = Hdr.h_ino;
45120Sstevel@tonic-gate 		Gen.g_dev = Hdr.h_dev;
45130Sstevel@tonic-gate 		Gen.g_rdev = Hdr.h_rdev;
45140Sstevel@tonic-gate 		Gen.g_cksum = 0L;
45150Sstevel@tonic-gate 		Gen.g_filesz = (off_t)mklong(Hdr.h_filesize);
45160Sstevel@tonic-gate 		Gen.g_namesz = Hdr.h_namesize;
45170Sstevel@tonic-gate 		rv = BIN;
45180Sstevel@tonic-gate 		break;
45190Sstevel@tonic-gate 	case CHR:
45200Sstevel@tonic-gate 		if (sscanf(Buffr.b_out_p,
45210Sstevel@tonic-gate 		    "%6lo%6lo%6lo%6lo%6lo%6lo%6lo%6lo%11lo%6o%11llo",
45220Sstevel@tonic-gate 		    &Gen.g_magic, &Gen.g_dev, &Gen.g_ino, &Gen.g_mode,
45230Sstevel@tonic-gate 		    &Gen.g_uid, &Gen.g_gid, &Gen.g_nlink, &Gen.g_rdev,
45240Sstevel@tonic-gate 		    (ulong_t *)&Gen.g_mtime, (uint_t *)&Gen.g_namesz,
45250Sstevel@tonic-gate 		    (u_off_t *)&Gen.g_filesz) == CHR_CNT) {
45260Sstevel@tonic-gate 			rv = CHR;
45270Sstevel@tonic-gate #define	cpioMAJOR(x)	(int)(((unsigned)x >> 8) & 0x7F)
45280Sstevel@tonic-gate #define	cpioMINOR(x)	(int)(x & 0xFF)
45290Sstevel@tonic-gate 			maj = cpioMAJOR(Gen.g_dev);
45300Sstevel@tonic-gate 			rmaj = cpioMAJOR(Gen.g_rdev);
45310Sstevel@tonic-gate 			min = cpioMINOR(Gen.g_dev);
45320Sstevel@tonic-gate 			rmin = cpioMINOR(Gen.g_rdev);
45330Sstevel@tonic-gate 			if (Use_old_stat) {
45340Sstevel@tonic-gate 				/* needs error checking */
45350Sstevel@tonic-gate 				Gen.g_dev = (maj << 8) | min;
45360Sstevel@tonic-gate 				Gen.g_rdev = (rmaj << 8) | rmin;
45370Sstevel@tonic-gate 			} else {
45380Sstevel@tonic-gate 				Gen.g_dev = makedev(maj, min);
45390Sstevel@tonic-gate 				Gen.g_rdev = makedev(rmaj, rmin);
45400Sstevel@tonic-gate 			}
45410Sstevel@tonic-gate 		}
45420Sstevel@tonic-gate 		break;
45430Sstevel@tonic-gate 	case ASC:
45440Sstevel@tonic-gate 	case CRC:
45450Sstevel@tonic-gate 		if (sscanf(Buffr.b_out_p,
45460Sstevel@tonic-gate 		    "%6lx%8lx%8lx%8lx%8lx%8lx%8lx%8llx%8x%8x%8x%8x%8x%8lx",
45470Sstevel@tonic-gate 		    &Gen.g_magic, &Gen.g_ino, &Gen.g_mode, &Gen.g_uid,
45480Sstevel@tonic-gate 		    &Gen.g_gid, &Gen.g_nlink, &Gen.g_mtime,
45490Sstevel@tonic-gate 		    (u_off_t *)&Gen.g_filesz, (uint_t *)&maj, (uint_t *)&min,
45500Sstevel@tonic-gate 		    (uint_t *)&rmaj, (uint_t *)&rmin, (uint_t *)&Gen.g_namesz,
45510Sstevel@tonic-gate 		    &Gen.g_cksum) == ASC_CNT) {
45520Sstevel@tonic-gate 			Gen.g_dev = makedev(maj, min);
45530Sstevel@tonic-gate 			Gen.g_rdev = makedev(rmaj, rmin);
45540Sstevel@tonic-gate 			rv = hdr;
45550Sstevel@tonic-gate 		}
45560Sstevel@tonic-gate 		break;
45570Sstevel@tonic-gate 	case USTAR: /* TAR and USTAR */
45580Sstevel@tonic-gate 		if (*Buffr.b_out_p == '\0') {
45590Sstevel@tonic-gate 			*Gen.g_nam_p = '\0';
45600Sstevel@tonic-gate 			nambuf[0] = '\0';
45610Sstevel@tonic-gate 		} else {
45620Sstevel@tonic-gate 			Thdr_p = (union tblock *)Buffr.b_out_p;
45630Sstevel@tonic-gate 			Gen.g_nam_p[0] = '\0';
45640Sstevel@tonic-gate 			(void) strncpy((char *)&nambuf,
45650Sstevel@tonic-gate 			    Thdr_p->tbuf.t_name, NAMSIZ);
45660Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mode, "%8lo",
45670Sstevel@tonic-gate 			    &Gen.g_mode);
45680Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_uid, "%8lo", &Gen.g_uid);
45690Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_gid, "%8lo", &Gen.g_gid);
45700Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_size, "%11llo",
45710Sstevel@tonic-gate 			    (u_off_t *)&Gen.g_filesz);
45720Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mtime, "%12lo",
45730Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_mtime);
45740Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_cksum, "%8lo",
45750Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_cksum);
45760Sstevel@tonic-gate 			if (Thdr_p->tbuf.t_linkname[0] != (char)NULL)
45770Sstevel@tonic-gate 				Gen.g_nlink = 1;
45780Sstevel@tonic-gate 			else
45790Sstevel@tonic-gate 				Gen.g_nlink = 0;
45800Sstevel@tonic-gate 
45810Sstevel@tonic-gate 			switch (Thdr_p->tbuf.t_typeflag) {
45820Sstevel@tonic-gate 			case SYMTYPE:
45830Sstevel@tonic-gate 				/* Symbolic Link */
45840Sstevel@tonic-gate 				Gen.g_nlink = 2;
45850Sstevel@tonic-gate 				break;
45860Sstevel@tonic-gate 			case CHRTYPE:
45870Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFCHR);
45880Sstevel@tonic-gate 				break;
45890Sstevel@tonic-gate 			case BLKTYPE:
45900Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFBLK);
45910Sstevel@tonic-gate 				break;
45920Sstevel@tonic-gate 			case DIRTYPE:
45930Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFDIR);
45940Sstevel@tonic-gate 				break;
45950Sstevel@tonic-gate 			case FIFOTYPE:
45960Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFIFO);
45970Sstevel@tonic-gate 				break;
45980Sstevel@tonic-gate 			}
45990Sstevel@tonic-gate 
46000Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_magic, "%8lo",
46010Sstevel@tonic-gate 			    /* LINTED alignment */
46020Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_tmagic);
46030Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_version, "%8lo",
46040Sstevel@tonic-gate 			    /* LINTED alignment */
46050Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_version);
46060Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_uname, "%32s",
46070Sstevel@tonic-gate 			    (char *)&Gen.g_uname);
46080Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_gname, "%32s",
46090Sstevel@tonic-gate 			    (char *)&Gen.g_gname);
46100Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_devmajor, "%8lo",
46110Sstevel@tonic-gate 			    &Gen.g_dev);
46120Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_devminor, "%8lo",
46130Sstevel@tonic-gate 			    &Gen.g_rdev);
46140Sstevel@tonic-gate 			(void) strncpy((char *)&prebuf,
46150Sstevel@tonic-gate 			    Thdr_p->tbuf.t_prefix, PRESIZ);
46160Sstevel@tonic-gate 			Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
46170Sstevel@tonic-gate 			Gen.g_dev = makedev(maj, min);
46180Sstevel@tonic-gate 		}
46190Sstevel@tonic-gate 		rv = USTAR;
46200Sstevel@tonic-gate 		break;
46210Sstevel@tonic-gate 	case TAR:
46220Sstevel@tonic-gate 		if (*Buffr.b_out_p == '\0') {
46230Sstevel@tonic-gate 			*Gen.g_nam_p = '\0';
46240Sstevel@tonic-gate 			nambuf[0] = '\0';
46250Sstevel@tonic-gate 		} else {
46260Sstevel@tonic-gate 			Thdr_p = (union tblock *)Buffr.b_out_p;
46270Sstevel@tonic-gate 			Gen.g_nam_p[0] = '\0';
46280Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mode, "%lo", &Gen.g_mode);
46290Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_uid, "%lo", &Gen.g_uid);
46300Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_gid, "%lo", &Gen.g_gid);
46310Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_size, "%llo",
46320Sstevel@tonic-gate 			    (u_off_t *)&Gen.g_filesz);
46330Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mtime, "%lo",
46340Sstevel@tonic-gate 			    &Gen.g_mtime);
46350Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_cksum, "%lo",
46360Sstevel@tonic-gate 			    &Gen.g_cksum);
46370Sstevel@tonic-gate 			if (Thdr_p->tbuf.t_typeflag == '1')	/* hardlink */
46380Sstevel@tonic-gate 				Gen.g_nlink = 1;
46390Sstevel@tonic-gate 			else
46400Sstevel@tonic-gate 				Gen.g_nlink = 0;
46410Sstevel@tonic-gate 			(void) strncpy(Gen.g_nam_p,
46420Sstevel@tonic-gate 			    Thdr_p->tbuf.t_name, NAMSIZ);
46430Sstevel@tonic-gate 			Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
46440Sstevel@tonic-gate 			(void) strcpy(nambuf, Gen.g_nam_p);
46450Sstevel@tonic-gate 		}
46460Sstevel@tonic-gate 		rv = TAR;
46470Sstevel@tonic-gate 		break;
46480Sstevel@tonic-gate 	case BAR:
46490Sstevel@tonic-gate 		if (Bar_vol_num == 0 && bar_read_cnt == 0) {
46500Sstevel@tonic-gate 			read_bar_vol_hdr();
46510Sstevel@tonic-gate 			bar_read_cnt++;
46520Sstevel@tonic-gate 		}
46530Sstevel@tonic-gate 		else
46540Sstevel@tonic-gate 			read_bar_file_hdr();
46550Sstevel@tonic-gate 		rv = BAR;
46560Sstevel@tonic-gate 		break;
46570Sstevel@tonic-gate 	default:
46580Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
46590Sstevel@tonic-gate 	}
46600Sstevel@tonic-gate 
46610Sstevel@tonic-gate 	if (hdr != BAR) {
46620Sstevel@tonic-gate 		if (Buffr.b_end_p != (Buffr.b_out_p + Hdrsz))
46630Sstevel@tonic-gate 			*(Buffr.b_out_p + Hdrsz) = tmpnull;
46640Sstevel@tonic-gate 	}
46650Sstevel@tonic-gate 
46660Sstevel@tonic-gate 	return (rv);
46670Sstevel@tonic-gate }
46680Sstevel@tonic-gate 
46690Sstevel@tonic-gate /*
46700Sstevel@tonic-gate  * reclaim: Reclaim linked file structure storage.
46710Sstevel@tonic-gate  */
46720Sstevel@tonic-gate 
46730Sstevel@tonic-gate static void
46740Sstevel@tonic-gate reclaim(struct Lnk *p)
46750Sstevel@tonic-gate {
46760Sstevel@tonic-gate 	p->L_bck_p->L_nxt_p = p->L_nxt_p;
46770Sstevel@tonic-gate 	p->L_nxt_p->L_bck_p = p->L_bck_p;
46780Sstevel@tonic-gate 
46790Sstevel@tonic-gate 	while (p != NULL) {
46800Sstevel@tonic-gate 		struct Lnk *new_p = p->L_lnk_p;
46810Sstevel@tonic-gate 
46820Sstevel@tonic-gate 		free(p->L_gen.g_nam_p);
46830Sstevel@tonic-gate 		free(p);
46840Sstevel@tonic-gate 		p = new_p;
46850Sstevel@tonic-gate 	}
46860Sstevel@tonic-gate }
46870Sstevel@tonic-gate 
46880Sstevel@tonic-gate /*
46890Sstevel@tonic-gate  * rstbuf: Reset the I/O buffer, move incomplete potential headers to
46900Sstevel@tonic-gate  * the front of the buffer and force bread() to refill the buffer.  The
46910Sstevel@tonic-gate  * return value from bread() is returned (to identify I/O errors).  On the
46920Sstevel@tonic-gate  * 3B2, reads must begin on a word boundary, therefore, with the -i option,
46930Sstevel@tonic-gate  * any remaining bytes in the buffer must be moved to the base of the buffer
46940Sstevel@tonic-gate  * in such a way that the destination locations of subsequent reads are
46950Sstevel@tonic-gate  * word aligned.
46960Sstevel@tonic-gate  */
46970Sstevel@tonic-gate 
46980Sstevel@tonic-gate static void
46990Sstevel@tonic-gate rstbuf(void)
47000Sstevel@tonic-gate {
47010Sstevel@tonic-gate 	int pad;
47020Sstevel@tonic-gate 
47030Sstevel@tonic-gate 	if ((Args & OCi) || Append) {
47040Sstevel@tonic-gate 		if (Buffr.b_out_p != Buffr.b_base_p) {
47050Sstevel@tonic-gate 			pad = ((Buffr.b_cnt + FULLWD) & ~FULLWD);
47060Sstevel@tonic-gate 			Buffr.b_in_p = Buffr.b_base_p + pad;
47070Sstevel@tonic-gate 			pad -= Buffr.b_cnt;
47080Sstevel@tonic-gate 			(void) memcpy(Buffr.b_base_p + pad, Buffr.b_out_p,
47090Sstevel@tonic-gate 			    (int)Buffr.b_cnt);
47100Sstevel@tonic-gate 			Buffr.b_out_p = Buffr.b_base_p + pad;
47110Sstevel@tonic-gate 		}
47120Sstevel@tonic-gate 		if (bfill() < 0)
47130Sstevel@tonic-gate 			msg(EXT, "Unexpected end-of-archive encountered.");
47140Sstevel@tonic-gate 	} else { /* OCo */
47150Sstevel@tonic-gate 		(void) memcpy(Buffr.b_base_p, Buffr.b_out_p, (int)Buffr.b_cnt);
47160Sstevel@tonic-gate 		Buffr.b_out_p = Buffr.b_base_p;
47170Sstevel@tonic-gate 		Buffr.b_in_p = Buffr.b_base_p + Buffr.b_cnt;
47180Sstevel@tonic-gate 	}
47190Sstevel@tonic-gate }
47200Sstevel@tonic-gate 
47210Sstevel@tonic-gate static void
47220Sstevel@tonic-gate setpasswd(char *nam)
47230Sstevel@tonic-gate {
47240Sstevel@tonic-gate 	if ((dpasswd = getpwnam(&Gen.g_uname[0])) == (struct passwd *)NULL) {
47250Sstevel@tonic-gate 		msg(EPOST, "cpio: problem reading passwd entry");
47260Sstevel@tonic-gate 		msg(EPOST, "cpio: %s: owner not changed", nam);
47270Sstevel@tonic-gate 		if (Gen.g_uid == UID_NOBODY && S_ISREG(Gen.g_mode))
47280Sstevel@tonic-gate 			Gen.g_mode &= ~S_ISUID;
47290Sstevel@tonic-gate 	} else
47300Sstevel@tonic-gate 		Gen.g_uid = dpasswd->pw_uid;
47310Sstevel@tonic-gate 
47320Sstevel@tonic-gate 	if ((dgroup = getgrnam(&Gen.g_gname[0])) == (struct group *)NULL) {
47330Sstevel@tonic-gate 		msg(EPOST, "cpio: problem reading group entry");
47340Sstevel@tonic-gate 		msg(EPOST, "cpio: %s: group not changed", nam);
47350Sstevel@tonic-gate 		if (Gen.g_gid == GID_NOBODY && S_ISREG(Gen.g_mode))
47360Sstevel@tonic-gate 			Gen.g_mode &= ~S_ISGID;
47370Sstevel@tonic-gate 	} else
47380Sstevel@tonic-gate 		Gen.g_gid = dgroup->gr_gid;
47390Sstevel@tonic-gate 	G_p = &Gen;
47400Sstevel@tonic-gate }
47410Sstevel@tonic-gate 
47420Sstevel@tonic-gate /*
47430Sstevel@tonic-gate  * rstfiles:  Perform final changes to the file.  If the -u option is set,
47440Sstevel@tonic-gate  * and overwrite == U_OVER, remove the temporary file, else if overwrite
47450Sstevel@tonic-gate  * == U_KEEP, unlink the current file, and restore the existing version
47460Sstevel@tonic-gate  * of the file.  In addition, where appropriate, set the access or modification
47470Sstevel@tonic-gate  * times, change the owner and change the modes of the file.
47480Sstevel@tonic-gate  *
47490Sstevel@tonic-gate  * Note that if Do_rename is set, then the roles of original and temporary
47500Sstevel@tonic-gate  * file are reversed. If all went well, we will rename() the temporary file
47510Sstevel@tonic-gate  * over the original in order to accomodate potentially executing files.
47520Sstevel@tonic-gate  */
47530Sstevel@tonic-gate static void
47540Sstevel@tonic-gate rstfiles(int over, int dirfd)
47550Sstevel@tonic-gate {
47560Sstevel@tonic-gate 	char *inam_p, *onam_p, *nam_p;
47570Sstevel@tonic-gate 	int error;
47580Sstevel@tonic-gate 
47590Sstevel@tonic-gate 	if (Args & OCp) {
47600Sstevel@tonic-gate 		if (G_p->g_attrnam_p == (char *)NULL) {
47610Sstevel@tonic-gate 			nam_p = Fullnam_p;
47620Sstevel@tonic-gate 		} else {
47630Sstevel@tonic-gate 			nam_p = G_p->g_attrnam_p;
47640Sstevel@tonic-gate 		}
47650Sstevel@tonic-gate 	} else {
47660Sstevel@tonic-gate 		if (Gen.g_nlink > (ulong_t)0) {
47670Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
47680Sstevel@tonic-gate 		} else {
47690Sstevel@tonic-gate 			nam_p = Gen.g_nam_p;
47700Sstevel@tonic-gate 		}
47710Sstevel@tonic-gate 	}
47720Sstevel@tonic-gate 	if (Gen.g_attrnam_p != (char *)NULL) {
47730Sstevel@tonic-gate 		nam_p = Gen.g_attrnam_p;
47740Sstevel@tonic-gate 	}
47750Sstevel@tonic-gate 
47760Sstevel@tonic-gate 	if ((Args & OCi) && (Hdr_type == USTAR)) {
47770Sstevel@tonic-gate 		setpasswd(nam_p);
47780Sstevel@tonic-gate 	}
47790Sstevel@tonic-gate 	if (over == U_KEEP && *Over_p != '\0') {
47800Sstevel@tonic-gate 		if (Do_rename) {
47810Sstevel@tonic-gate 			msg(POST, "Restoring existing \"%s%s%s\"",
47820Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
47830Sstevel@tonic-gate 			    Over_p : Fullnam_p,
47840Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
47850Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
47860Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
47870Sstevel@tonic-gate 			    "" : Over_p);
47880Sstevel@tonic-gate 		} else {
47890Sstevel@tonic-gate 			msg(POST, "Restoring existing \"%s%s%s\"",
47900Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
47910Sstevel@tonic-gate 			    nam_p : Fullnam_p,
47920Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
47930Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
47940Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
47950Sstevel@tonic-gate 			    "" : nam_p);
47960Sstevel@tonic-gate 		}
47970Sstevel@tonic-gate 
47980Sstevel@tonic-gate 		/* delete what we just built */
47990Sstevel@tonic-gate 		(void) unlinkat(dirfd, get_component(nam_p), 0);
48000Sstevel@tonic-gate 
48010Sstevel@tonic-gate 		/* If the old file needs restoring, do the necessary links */
48020Sstevel@tonic-gate 		if (Do_rename) {
48030Sstevel@tonic-gate 			char *tmp_ptr;
48040Sstevel@tonic-gate 
48050Sstevel@tonic-gate 			if (Args & OCp) {
48060Sstevel@tonic-gate 				tmp_ptr = Fullnam_p;
48070Sstevel@tonic-gate 				Fullnam_p = Over_p;
48080Sstevel@tonic-gate 			} else {
48090Sstevel@tonic-gate 				tmp_ptr = G_p->g_nam_p;
48100Sstevel@tonic-gate 				G_p->g_nam_p = Over_p;
48110Sstevel@tonic-gate 			}
48120Sstevel@tonic-gate 			Over_p = tmp_ptr;
48130Sstevel@tonic-gate 
48140Sstevel@tonic-gate 			Do_rename = 0;	/* names now have original values */
48150Sstevel@tonic-gate 		} else {
48160Sstevel@tonic-gate 			if (rename(Over_p, nam_p) < 0) {
48170Sstevel@tonic-gate 				if (link(Over_p, nam_p) < 0) {
48180Sstevel@tonic-gate 					msg(EXTN,
48190Sstevel@tonic-gate 					    "Cannot recover original version"
48200Sstevel@tonic-gate 					    " of \"%s%s%s\"",
48210Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
48220Sstevel@tonic-gate 					    nam_p : Fullnam_p,
48230Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
48240Sstevel@tonic-gate 					    "" : gettext(" Attribute "),
48250Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
48260Sstevel@tonic-gate 					    "" : nam_p);
48270Sstevel@tonic-gate 				}
48280Sstevel@tonic-gate 				if (unlinkat(dirfd, get_component(Over_p), 0)) {
48290Sstevel@tonic-gate 					msg(ERRN,
48300Sstevel@tonic-gate 					    "Cannot remove temp file "
48310Sstevel@tonic-gate 					    "\"%s%s%s\"",
48320Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
48330Sstevel@tonic-gate 					    Over_p : Fullnam_p,
48340Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
48350Sstevel@tonic-gate 					    "" : gettext(" Attribute "),
48360Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
48370Sstevel@tonic-gate 					    "" : Over_p);
48380Sstevel@tonic-gate 				}
48390Sstevel@tonic-gate 			}
48400Sstevel@tonic-gate 		}
48410Sstevel@tonic-gate 		*Over_p = '\0';
48420Sstevel@tonic-gate 		return;
48430Sstevel@tonic-gate 	} else if (over == U_OVER && *Over_p != '\0') {
48440Sstevel@tonic-gate 		if (Do_rename) {
48450Sstevel@tonic-gate 			char *tmp_ptr;
48460Sstevel@tonic-gate 
48470Sstevel@tonic-gate 			(void) renameat(dirfd, get_component(nam_p),
48480Sstevel@tonic-gate 			    dirfd, get_component(Over_p));
48490Sstevel@tonic-gate 			if (Args & OCp) {
48500Sstevel@tonic-gate 				if (G_p->g_attrnam_p == (char *)NULL) {
48510Sstevel@tonic-gate 					tmp_ptr = Fullnam_p;
48520Sstevel@tonic-gate 					Fullnam_p = Over_p;
48530Sstevel@tonic-gate 					Over_p = tmp_ptr;
48540Sstevel@tonic-gate 				} else {
48550Sstevel@tonic-gate 					/*
48560Sstevel@tonic-gate 					 * Over_p is pointing at g_attrnam_p
48570Sstevel@tonic-gate 					 * which must be preserved.
48580Sstevel@tonic-gate 					 *
48590Sstevel@tonic-gate 					 * We don't want the tmp_ptr and so
48600Sstevel@tonic-gate 					 * on to throw away our only copy of
48610Sstevel@tonic-gate 					 * the name.
48620Sstevel@tonic-gate 					 */
48630Sstevel@tonic-gate 					Over_p = Attrfile_p;
48640Sstevel@tonic-gate 				}
48650Sstevel@tonic-gate 			} else {
48660Sstevel@tonic-gate 				tmp_ptr = G_p->g_nam_p;
48670Sstevel@tonic-gate 				G_p->g_nam_p = Over_p;
48680Sstevel@tonic-gate 				Over_p = tmp_ptr;
48690Sstevel@tonic-gate 			}
48700Sstevel@tonic-gate 			Do_rename = 0;	/* names now have original values */
48710Sstevel@tonic-gate 		} else {
48720Sstevel@tonic-gate 			if (unlinkat(dirfd, get_component(Over_p), 0) < 0) {
48730Sstevel@tonic-gate 				msg(ERRN,
48740Sstevel@tonic-gate 				    "Cannot unlink() temp file \"%s%s%s\"",
48750Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
48760Sstevel@tonic-gate 				    Over_p : Fullnam_p,
48770Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
48780Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
48790Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
48800Sstevel@tonic-gate 				    "" : Over_p);
48810Sstevel@tonic-gate 			}
48820Sstevel@tonic-gate 		}
48830Sstevel@tonic-gate 		*Over_p = '\0';
48840Sstevel@tonic-gate 	}
48850Sstevel@tonic-gate 	if (Args & OCp) {
48860Sstevel@tonic-gate 		if (G_p->g_attrnam_p != (char *)NULL) {
48870Sstevel@tonic-gate 			inam_p = G_p->g_attrfnam_p;
48880Sstevel@tonic-gate 			onam_p = G_p->g_attrnam_p;
48890Sstevel@tonic-gate 		} else {
48900Sstevel@tonic-gate 			inam_p = Nam_p;
48910Sstevel@tonic-gate 			onam_p = Fullnam_p;
48920Sstevel@tonic-gate 		}
48930Sstevel@tonic-gate 	} else /* OCi only uses onam_p, OCo only uses inam_p */
48940Sstevel@tonic-gate 		if (G_p->g_attrnam_p != (char *)NULL) {
48950Sstevel@tonic-gate 			inam_p = onam_p = G_p->g_attrnam_p;
48960Sstevel@tonic-gate 		} else {
48970Sstevel@tonic-gate 			inam_p = onam_p = G_p->g_nam_p;
48980Sstevel@tonic-gate 		}
48990Sstevel@tonic-gate 
49000Sstevel@tonic-gate 	/*
49011134Sceastha 	 * Change the owner, time, and mode to those of the file
49021134Sceastha 	 * originally created in the archive.  Note: time and
49031134Sceastha 	 * mode do not need to be restored for a symbolic link
49041134Sceastha 	 * since rstfiles() is not called when the archived file
49051134Sceastha 	 * is a symlink.
49060Sstevel@tonic-gate 	 */
49070Sstevel@tonic-gate 	if (!(Args & OCo)) {
49081134Sceastha 		if (Args & OCR) {
49091134Sceastha 			if (fchownat(dirfd, get_component(onam_p),
49101134Sceastha 			    Rpw_p->pw_uid, Rpw_p->pw_gid,
49111134Sceastha 			    AT_SYMLINK_NOFOLLOW) < 0) {
49121134Sceastha 				msg(ERRN, "Cannot chown() \"%s%s%s\"",
49131134Sceastha 				    onam_p,
49141134Sceastha 				    (G_p->g_attrnam_p == (char *)NULL) ?
49151134Sceastha 				    "" : gettext(" Attribute "),
49161134Sceastha 				    (G_p->g_attrnam_p == (char *)NULL) ?
49171134Sceastha 				    "" : onam_p);
49181134Sceastha 			}
49191134Sceastha 		} else {
49201134Sceastha 			if ((fchownat(dirfd, get_component(onam_p),
49211134Sceastha 			    G_p->g_uid, G_p->g_gid,
49221134Sceastha 			    AT_SYMLINK_NOFOLLOW) < 0) && privileged) {
49231134Sceastha 				msg(ERRN, "Cannot chown() \"%s%s%s\"",
49241134Sceastha 				    onam_p,
49250Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
49260Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
49270Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
49280Sstevel@tonic-gate 				    "" : onam_p);
49290Sstevel@tonic-gate 			}
49300Sstevel@tonic-gate 		}
49311134Sceastha 
49321134Sceastha 		if (Args & OCm) {
49331134Sceastha 			set_tym(dirfd, get_component(onam_p),
49341134Sceastha 			    G_p->g_mtime, G_p->g_mtime);
49351134Sceastha 		}
49361134Sceastha 
49371134Sceastha 		/* Acl was not set, so we must chmod */
49381134Sceastha 		if (!acl_is_set) {
49391134Sceastha 			mode_t orig_mask, new_mask;
49401134Sceastha 
49411134Sceastha 			/*
49421134Sceastha 			 * use fchmod for attributes, since
49431134Sceastha 			 * we known they are always regular
49441134Sceastha 			 * files, whereas when it isn't an
49451134Sceastha 			 * attribute it could be for a fifo
49461134Sceastha 			 * or something other that we don't
49471134Sceastha 			 * open and don't have a valid Ofile
49481134Sceastha 			 * for.
49491134Sceastha 			 */
49501134Sceastha 			if (privileged) {
49511134Sceastha 				new_mask = G_p->g_mode;
49521134Sceastha 			} else {
49531134Sceastha 				orig_mask = umask(0);
49541134Sceastha 				new_mask = G_p->g_mode & ~orig_mask;
49551134Sceastha 			}
49561134Sceastha 
49571134Sceastha 			if (G_p->g_attrnam_p != (char *)NULL) {
49581134Sceastha 				error = fchmod(Ofile, new_mask);
49591134Sceastha 			} else {
49601134Sceastha 				error = chmod(onam_p, new_mask);
49611134Sceastha 			}
49621134Sceastha 			if (error < 0) {
49631134Sceastha 				msg(ERRN,
49641134Sceastha 				    "Cannot chmod() \"%s%s%s\"",
49651134Sceastha 				    (G_p->g_attrnam_p == (char *)NULL) ?
49661134Sceastha 				    onam_p : G_p->g_attrfnam_p,
49671134Sceastha 				    (G_p->g_attrnam_p == (char *)NULL) ?
49681134Sceastha 				    "" : gettext(" Attribute "),
49691134Sceastha 				    (G_p->g_attrnam_p == (char *)NULL) ?
49701134Sceastha 				    "" : onam_p);
49711134Sceastha 			}
49721134Sceastha 			if (!privileged) {
49731134Sceastha 				(void) umask(orig_mask);
49741134Sceastha 			}
49750Sstevel@tonic-gate 		}
49760Sstevel@tonic-gate 	}
49770Sstevel@tonic-gate 
49780Sstevel@tonic-gate 	if (!(Args & OCi) && (Args & OCa)) {
49790Sstevel@tonic-gate 		/*
49800Sstevel@tonic-gate 		 * Use dirfd since we are updating original file
49810Sstevel@tonic-gate 		 * and not just created file
49820Sstevel@tonic-gate 		 */
49830Sstevel@tonic-gate 		set_tym(G_p->g_dirfd, get_component(inam_p),
49840Sstevel@tonic-gate 		    (ulong_t)SrcSt.st_atime, (ulong_t)SrcSt.st_mtime);
49850Sstevel@tonic-gate 	}
49860Sstevel@tonic-gate }
49870Sstevel@tonic-gate 
49880Sstevel@tonic-gate /*
49890Sstevel@tonic-gate  * scan4trail: Scan the archive looking for the trailer.
49900Sstevel@tonic-gate  * When found, back the archive up over the trailer and overwrite
49910Sstevel@tonic-gate  * the trailer with the files to be added to the archive.
49920Sstevel@tonic-gate  */
49930Sstevel@tonic-gate 
49940Sstevel@tonic-gate static void
49950Sstevel@tonic-gate scan4trail(void)
49960Sstevel@tonic-gate {
49970Sstevel@tonic-gate 	int rv;
49980Sstevel@tonic-gate 	off_t off1, off2;
49990Sstevel@tonic-gate 
50000Sstevel@tonic-gate 	Append = 1;
50010Sstevel@tonic-gate 	Hdr_type = NONE;
50020Sstevel@tonic-gate 	G_p = (struct gen_hdr *)NULL;
50030Sstevel@tonic-gate 	while (gethdr()) {
50040Sstevel@tonic-gate 		G_p = &Gen;
50050Sstevel@tonic-gate 		data_in(P_SKIP);
50060Sstevel@tonic-gate 	}
50070Sstevel@tonic-gate 	off1 = Buffr.b_cnt;
50080Sstevel@tonic-gate 	off2 = Bufsize - (Buffr.b_cnt % Bufsize);
50090Sstevel@tonic-gate 	Buffr.b_out_p = Buffr.b_in_p = Buffr.b_base_p;
50100Sstevel@tonic-gate 	Buffr.b_cnt = (off_t)0;
50110Sstevel@tonic-gate 	if (lseek(Archive, -(off1 + off2), SEEK_REL) < 0)
50120Sstevel@tonic-gate 		msg(EXTN, "Unable to append to this archive");
50130Sstevel@tonic-gate 	if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0)
50140Sstevel@tonic-gate 		msg(EXTN, "Cannot append to this archive");
50150Sstevel@tonic-gate 	if (lseek(Archive, (off_t)-rv, SEEK_REL) < 0)
50160Sstevel@tonic-gate 		msg(EXTN, "Unable to append to this archive");
50170Sstevel@tonic-gate 	Buffr.b_cnt = off2;
50180Sstevel@tonic-gate 	Buffr.b_in_p = Buffr.b_base_p + Buffr.b_cnt;
50190Sstevel@tonic-gate 	Append = 0;
50200Sstevel@tonic-gate }
50210Sstevel@tonic-gate 
50220Sstevel@tonic-gate /*
50230Sstevel@tonic-gate  * setup:  Perform setup and initialization functions.  Parse the options
50240Sstevel@tonic-gate  * using getopt(3C), call ckopts to check the options and initialize various
50250Sstevel@tonic-gate  * structures and pointers.  Specifically, for the -i option, save any
50260Sstevel@tonic-gate  * patterns, for the -o option, check (via stat(2)) the archive, and for
50270Sstevel@tonic-gate  * the -p option, validate the destination directory.
50280Sstevel@tonic-gate  */
50290Sstevel@tonic-gate 
50300Sstevel@tonic-gate static void
50310Sstevel@tonic-gate setup(int largc, char **largv)
50320Sstevel@tonic-gate {
50330Sstevel@tonic-gate 	extern int optind;
50340Sstevel@tonic-gate 	extern char *optarg;
50350Sstevel@tonic-gate 
50360Sstevel@tonic-gate #if defined(O_XATTR)
50370Sstevel@tonic-gate #ifdef WAITAROUND
50380Sstevel@tonic-gate 	char	*opts_p = "zabcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6@";
50390Sstevel@tonic-gate #else
50400Sstevel@tonic-gate 	char	*opts_p = "abcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6@";
50410Sstevel@tonic-gate #endif
50420Sstevel@tonic-gate #else
50430Sstevel@tonic-gate #ifdef WAITAROUND
50440Sstevel@tonic-gate 	char	*opts_p = "zabcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6";
50450Sstevel@tonic-gate #else
50460Sstevel@tonic-gate 	char	*opts_p = "abcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6";
50470Sstevel@tonic-gate #endif
50480Sstevel@tonic-gate #endif
50490Sstevel@tonic-gate 
50500Sstevel@tonic-gate 	char   *dupl_p = "Only one occurrence of -%c allowed";
50510Sstevel@tonic-gate 	int option;
50520Sstevel@tonic-gate 	int blk_cnt, blk_cnt_max;
50530Sstevel@tonic-gate 	struct rlimit rlim;
50540Sstevel@tonic-gate 
50550Sstevel@tonic-gate 	/* Remember the native page size. */
50560Sstevel@tonic-gate 
50570Sstevel@tonic-gate 	PageSize = sysconf(_SC_PAGESIZE);
50580Sstevel@tonic-gate 
50590Sstevel@tonic-gate 	if (PageSize == -1) {
50600Sstevel@tonic-gate 		/*
50610Sstevel@tonic-gate 		 * This sysconf call will almost certainly never fail.  The
50620Sstevel@tonic-gate 		 * symbol PAGESIZE itself resolves to the above sysconf call,
50630Sstevel@tonic-gate 		 * so we should go ahead and define our own constant.
50640Sstevel@tonic-gate 		 */
50650Sstevel@tonic-gate 		PageSize = 8192;
50660Sstevel@tonic-gate 	}
50670Sstevel@tonic-gate 
50680Sstevel@tonic-gate 	Hdr_type = BIN;
50690Sstevel@tonic-gate 	Max_offset = (off_t)(BIN_OFFSET_MAX);
50700Sstevel@tonic-gate 	Efil_p = Hdr_p = Own_p = IOfil_p = NULL;
50710Sstevel@tonic-gate 	while ((option = getopt(largc, largv, opts_p)) != EOF) {
50720Sstevel@tonic-gate 		switch (option) {
50730Sstevel@tonic-gate #ifdef WAITAROUND
50740Sstevel@tonic-gate 		case 'z':
50750Sstevel@tonic-gate 			/* rendezvous with the debugger */
50760Sstevel@tonic-gate 			waitaround = 1;
50770Sstevel@tonic-gate 			break;
50780Sstevel@tonic-gate #endif
50790Sstevel@tonic-gate 		case 'a':	/* reset access time */
50800Sstevel@tonic-gate 			Args |= OCa;
50810Sstevel@tonic-gate 			break;
50820Sstevel@tonic-gate 		case 'b':	/* swap bytes and halfwords */
50830Sstevel@tonic-gate 			Args |= OCb;
50840Sstevel@tonic-gate 			break;
50850Sstevel@tonic-gate 		case 'c':	/* select character header */
50860Sstevel@tonic-gate 			Args |= OCc;
50870Sstevel@tonic-gate 			Hdr_type = ASC;
50880Sstevel@tonic-gate 			Max_namesz = APATH;
50890Sstevel@tonic-gate 			Onecopy = 1;
50900Sstevel@tonic-gate 			break;
50910Sstevel@tonic-gate 		case 'd':	/* create directories as needed */
50920Sstevel@tonic-gate 			Args |= OCd;
50930Sstevel@tonic-gate 			break;
50940Sstevel@tonic-gate 		case 'f':	/* select files not in patterns */
50950Sstevel@tonic-gate 			Args |= OCf;
50960Sstevel@tonic-gate 			break;
50970Sstevel@tonic-gate 		case 'i':	/* "copy in" */
50980Sstevel@tonic-gate 			Args |= OCi;
50990Sstevel@tonic-gate 			Archive = 0;
51000Sstevel@tonic-gate 			break;
51010Sstevel@tonic-gate 		case 'k':	/* retry after I/O errors */
51020Sstevel@tonic-gate 			Args |= OCk;
51030Sstevel@tonic-gate 			break;
51040Sstevel@tonic-gate 		case 'l':	/* link files when possible */
51050Sstevel@tonic-gate 			Args |= OCl;
51060Sstevel@tonic-gate 			break;
51070Sstevel@tonic-gate 		case 'm':	/* retain modification time */
51080Sstevel@tonic-gate 			Args |= OCm;
51090Sstevel@tonic-gate 			break;
51100Sstevel@tonic-gate 		case 'o':	/* "copy out" */
51110Sstevel@tonic-gate 			Args |= OCo;
51120Sstevel@tonic-gate 			Archive = 1;
51130Sstevel@tonic-gate 			break;
51140Sstevel@tonic-gate 		case 'p':	/* "pass" */
51150Sstevel@tonic-gate 			Max_namesz = APATH;
51160Sstevel@tonic-gate 			Args |= OCp;
51170Sstevel@tonic-gate 			break;
51180Sstevel@tonic-gate 		case 'r':	/* rename files interactively */
51190Sstevel@tonic-gate 			Args |= OCr;
51200Sstevel@tonic-gate 			break;
51210Sstevel@tonic-gate 		case 's':	/* swap bytes */
51220Sstevel@tonic-gate 			Args |= OCs;
51230Sstevel@tonic-gate 			break;
51240Sstevel@tonic-gate 		case 't':	/* table of contents */
51250Sstevel@tonic-gate 			Args |= OCt;
51260Sstevel@tonic-gate 			break;
51270Sstevel@tonic-gate 		case 'u':	/* copy unconditionally */
51280Sstevel@tonic-gate 			Args |= OCu;
51290Sstevel@tonic-gate 			break;
51300Sstevel@tonic-gate 		case 'v':	/* verbose - print file names */
51310Sstevel@tonic-gate 			Args |= OCv;
51320Sstevel@tonic-gate 			break;
51330Sstevel@tonic-gate 		case 'A':	/* append to existing archive */
51340Sstevel@tonic-gate 			Args |= OCA;
51350Sstevel@tonic-gate 			break;
51360Sstevel@tonic-gate 		case 'B':	/* set block size to 5120 bytes */
51370Sstevel@tonic-gate 			Args |= OCB;
51380Sstevel@tonic-gate 			Bufsize = 5120;
51390Sstevel@tonic-gate 			break;
51400Sstevel@tonic-gate 		case 'C':	/* set arbitrary block size */
51410Sstevel@tonic-gate 			if (Args & OCC)
51420Sstevel@tonic-gate 				msg(ERR, dupl_p, 'C');
51430Sstevel@tonic-gate 			else {
51440Sstevel@tonic-gate 				Args |= OCC;
51450Sstevel@tonic-gate 				Bufsize = atoi(optarg);
51460Sstevel@tonic-gate 			}
51470Sstevel@tonic-gate 			break;
51480Sstevel@tonic-gate 		case 'D':
51490Sstevel@tonic-gate 			Dflag = 1;
51500Sstevel@tonic-gate 			break;
51510Sstevel@tonic-gate 		case 'E':	/* alternate file for pattern input */
51520Sstevel@tonic-gate 			if (Args & OCE)
51530Sstevel@tonic-gate 				msg(ERR, dupl_p, 'E');
51540Sstevel@tonic-gate 			else {
51550Sstevel@tonic-gate 				Args |= OCE;
51560Sstevel@tonic-gate 				Efil_p = optarg;
51570Sstevel@tonic-gate 			}
51580Sstevel@tonic-gate 			break;
51590Sstevel@tonic-gate 		case 'H':	/* select header type */
51600Sstevel@tonic-gate 			if (Args & OCH)
51610Sstevel@tonic-gate 				msg(ERR, dupl_p, 'H');
51620Sstevel@tonic-gate 			else {
51630Sstevel@tonic-gate 				Args |= OCH;
51640Sstevel@tonic-gate 				Hdr_p = optarg;
51650Sstevel@tonic-gate 			}
51660Sstevel@tonic-gate 			break;
51670Sstevel@tonic-gate 		case 'I':	/* alternate file for archive input */
51680Sstevel@tonic-gate 			if (Args & OCI)
51690Sstevel@tonic-gate 				msg(ERR, dupl_p, 'I');
51700Sstevel@tonic-gate 			else {
51710Sstevel@tonic-gate 				Args |= OCI;
51720Sstevel@tonic-gate 				IOfil_p = optarg;
51730Sstevel@tonic-gate 			}
51740Sstevel@tonic-gate 			break;
51750Sstevel@tonic-gate 		case 'L':	/* follow symbolic links */
51760Sstevel@tonic-gate 			Args |= OCL;
51770Sstevel@tonic-gate 			break;
51780Sstevel@tonic-gate 		case 'M':	/* specify new end-of-media message */
51790Sstevel@tonic-gate 			if (Args & OCM)
51800Sstevel@tonic-gate 				msg(ERR, dupl_p, 'M');
51810Sstevel@tonic-gate 			else {
51820Sstevel@tonic-gate 				Args |= OCM;
51830Sstevel@tonic-gate 				Eom_p = optarg;
51840Sstevel@tonic-gate 			}
51850Sstevel@tonic-gate 			break;
51860Sstevel@tonic-gate 		case 'O':	/* alternate file for archive output */
51870Sstevel@tonic-gate 			if (Args & OCO)
51880Sstevel@tonic-gate 				msg(ERR, dupl_p, 'O');
51890Sstevel@tonic-gate 			else {
51900Sstevel@tonic-gate 				Args |= OCO;
51910Sstevel@tonic-gate 				IOfil_p = optarg;
51920Sstevel@tonic-gate 			}
51930Sstevel@tonic-gate 			break;
51940Sstevel@tonic-gate 		case 'P':	/* preserve acls */
51950Sstevel@tonic-gate 			Args |= OCP;
51960Sstevel@tonic-gate 			Pflag++;
51970Sstevel@tonic-gate 			break;
51980Sstevel@tonic-gate 		case 'R':	/* change owner/group of files */
51990Sstevel@tonic-gate 			if (Args & OCR)
52000Sstevel@tonic-gate 				msg(ERR, dupl_p, 'R');
52010Sstevel@tonic-gate 			else {
52020Sstevel@tonic-gate 				Args |= OCR;
52030Sstevel@tonic-gate 				Own_p = optarg;
52040Sstevel@tonic-gate 			}
52050Sstevel@tonic-gate 			break;
52060Sstevel@tonic-gate 		case 'S':	/* swap halfwords */
52070Sstevel@tonic-gate 			Args |= OCS;
52080Sstevel@tonic-gate 			break;
52090Sstevel@tonic-gate 		case 'V':	/* print a dot '.' for each file */
52100Sstevel@tonic-gate 			Args |= OCV;
52110Sstevel@tonic-gate 			break;
52120Sstevel@tonic-gate 		case '6':	/* for old, sixth-edition files */
52130Sstevel@tonic-gate 			Args |= OC6;
52140Sstevel@tonic-gate 			Ftype = SIXTH;
52150Sstevel@tonic-gate 			break;
52160Sstevel@tonic-gate #if defined(O_XATTR)
52170Sstevel@tonic-gate 		case '@':
52180Sstevel@tonic-gate 			Atflag++;
52190Sstevel@tonic-gate 			break;
52200Sstevel@tonic-gate #endif
52210Sstevel@tonic-gate 		default:
52220Sstevel@tonic-gate 			Error_cnt++;
52230Sstevel@tonic-gate 		} /* option */
52240Sstevel@tonic-gate 	} /* (option = getopt(largc, largv, opts_p)) != EOF */
52250Sstevel@tonic-gate 
52260Sstevel@tonic-gate #ifdef WAITAROUND
52270Sstevel@tonic-gate 	if (waitaround) {
52280Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Rendezvous with cpio on pid"
52290Sstevel@tonic-gate 		    " %d\n"), getpid());
52300Sstevel@tonic-gate 
52310Sstevel@tonic-gate 		while (waitaround) {
52320Sstevel@tonic-gate 			(void) sleep(10);
52330Sstevel@tonic-gate 		}
52340Sstevel@tonic-gate 	}
52350Sstevel@tonic-gate #endif
52360Sstevel@tonic-gate 
52370Sstevel@tonic-gate 	largc -= optind;
52380Sstevel@tonic-gate 	largv += optind;
52390Sstevel@tonic-gate 	ckopts(Args);
52400Sstevel@tonic-gate 	if (!Error_cnt) {
52410Sstevel@tonic-gate 		Empty = e_valloc(E_EXIT, TARSZ);
52420Sstevel@tonic-gate 		if (Args & OCr) {
52430Sstevel@tonic-gate 			Renam_p = e_zalloc(E_EXIT, APATH + 1);
52440Sstevel@tonic-gate 			Renametmp_p = e_zalloc(E_EXIT, APATH + 1);
52450Sstevel@tonic-gate 		}
52460Sstevel@tonic-gate 		Symlnk_p = e_zalloc(E_EXIT, APATH);
52470Sstevel@tonic-gate 		Over_p = e_zalloc(E_EXIT, APATH);
52480Sstevel@tonic-gate 		Nam_p = e_zalloc(E_EXIT, APATH + 1);
52490Sstevel@tonic-gate 		if (Args & OCp) {
52500Sstevel@tonic-gate 			Savenam_p = e_zalloc(E_EXIT, APATH + 1);
52510Sstevel@tonic-gate 		}
52520Sstevel@tonic-gate 		Fullnam_p = e_zalloc(E_EXIT, APATH);
52530Sstevel@tonic-gate 		Lnknam_p = e_zalloc(E_EXIT, APATH);
52540Sstevel@tonic-gate 		Gen.g_nam_p = Nam_p;
52550Sstevel@tonic-gate 		if ((Fullnam_p = getcwd((char *)NULL, APATH)) == (char *)NULL)
52560Sstevel@tonic-gate 			msg(EXT, "Unable to determine current directory.");
52570Sstevel@tonic-gate 		if (Args & OCi) {
52580Sstevel@tonic-gate 			if (largc > 0) /* save patterns for -i option, if any */
52590Sstevel@tonic-gate 				Pat_pp = largv;
52600Sstevel@tonic-gate 			if (Args & OCE)
52610Sstevel@tonic-gate 				getpats(largc, largv);
52620Sstevel@tonic-gate 		} else if (Args & OCo) {
52630Sstevel@tonic-gate 			if (largc != 0) /* error if arguments left with -o */
52640Sstevel@tonic-gate 				Error_cnt++;
52650Sstevel@tonic-gate 			else if (fstat(Archive, &ArchSt) < 0)
52660Sstevel@tonic-gate 				msg(ERRN, "Error during stat() of archive");
52670Sstevel@tonic-gate 			switch (Hdr_type) {
52680Sstevel@tonic-gate 			case BIN:
52690Sstevel@tonic-gate 				Hdrsz = HDRSZ;
52700Sstevel@tonic-gate 				Pad_val = HALFWD;
52710Sstevel@tonic-gate 				break;
52720Sstevel@tonic-gate 			case CHR:
52730Sstevel@tonic-gate 				Hdrsz = CHRSZ;
52740Sstevel@tonic-gate 				Pad_val = 0;
52750Sstevel@tonic-gate 				Max_offset = (off_t)(CHAR_OFFSET_MAX);
52760Sstevel@tonic-gate 				break;
52770Sstevel@tonic-gate 			case ASC:
52780Sstevel@tonic-gate 			case CRC:
52790Sstevel@tonic-gate 				Hdrsz = ASCSZ;
52800Sstevel@tonic-gate 				Pad_val = FULLWD;
528136Schin 				Max_offset = (off_t)(ASC_OFFSET_MAX);
52820Sstevel@tonic-gate 				break;
52830Sstevel@tonic-gate 			case TAR:
52840Sstevel@tonic-gate 			/* FALLTHROUGH */
52850Sstevel@tonic-gate 			case USTAR: /* TAR and USTAR */
52860Sstevel@tonic-gate 				Hdrsz = TARSZ;
52870Sstevel@tonic-gate 				Pad_val = FULLBK;
52880Sstevel@tonic-gate 				Max_offset = (off_t)(CHAR_OFFSET_MAX);
52890Sstevel@tonic-gate 				break;
52900Sstevel@tonic-gate 			default:
52910Sstevel@tonic-gate 				msg(EXT, "Impossible header type.");
52920Sstevel@tonic-gate 			}
52930Sstevel@tonic-gate 		} else { /* directory must be specified */
52940Sstevel@tonic-gate 			if (largc != 1)
52950Sstevel@tonic-gate 				Error_cnt++;
52960Sstevel@tonic-gate 			else if (access(*largv, 2) < 0)
52970Sstevel@tonic-gate 				msg(ERRN,
52980Sstevel@tonic-gate 				    "Error during access() of \"%s\"", *largv);
52990Sstevel@tonic-gate 		}
53000Sstevel@tonic-gate 	}
53010Sstevel@tonic-gate 	if (Error_cnt)
53020Sstevel@tonic-gate 		usage(); /* exits! */
53030Sstevel@tonic-gate 	if (Args & (OCi | OCo)) {
53040Sstevel@tonic-gate 		if (!Dflag) {
53050Sstevel@tonic-gate 			if (Args & (OCB | OCC)) {
53060Sstevel@tonic-gate 				if (g_init(&Device, &Archive) < 0)
53070Sstevel@tonic-gate 					msg(EXTN,
53080Sstevel@tonic-gate 					    "Error during initialization");
53090Sstevel@tonic-gate 			} else {
53100Sstevel@tonic-gate 				if ((Bufsize = g_init(&Device, &Archive)) < 0)
53110Sstevel@tonic-gate 					msg(EXTN,
53120Sstevel@tonic-gate 					    "Error during initialization");
53130Sstevel@tonic-gate 			}
53140Sstevel@tonic-gate 		}
53150Sstevel@tonic-gate 
53160Sstevel@tonic-gate 		blk_cnt_max = _20K / Bufsize;
53170Sstevel@tonic-gate 		if (blk_cnt_max < MX_BUFS) {
53180Sstevel@tonic-gate 			blk_cnt_max = MX_BUFS;
53190Sstevel@tonic-gate 		}
53200Sstevel@tonic-gate 
53210Sstevel@tonic-gate 		Buffr.b_base_p = NULL;
53220Sstevel@tonic-gate 
53230Sstevel@tonic-gate 		for (blk_cnt = blk_cnt_max; blk_cnt > 1; blk_cnt--) {
53240Sstevel@tonic-gate 			Buffr.b_size = (size_t)(Bufsize * blk_cnt);
53250Sstevel@tonic-gate 			Buffr.b_base_p = e_valloc(E_NORMAL, Buffr.b_size);
53260Sstevel@tonic-gate 			if (Buffr.b_base_p != NULL) {
53270Sstevel@tonic-gate 				break;
53280Sstevel@tonic-gate 			}
53290Sstevel@tonic-gate 		}
53300Sstevel@tonic-gate 		if (Buffr.b_base_p == NULL || Buffr.b_size < (2 * CPIOBSZ)) {
53310Sstevel@tonic-gate 			msg(EXT, "Out of memory");
53320Sstevel@tonic-gate 		}
53330Sstevel@tonic-gate 
53340Sstevel@tonic-gate 		Buffr.b_out_p = Buffr.b_in_p = Buffr.b_base_p;
53350Sstevel@tonic-gate 		Buffr.b_cnt = 0L;
53360Sstevel@tonic-gate 		Buffr.b_end_p = Buffr.b_base_p + Buffr.b_size;
53370Sstevel@tonic-gate 	}
53380Sstevel@tonic-gate 
53390Sstevel@tonic-gate 	/*
53400Sstevel@tonic-gate 	 * Now that Bufsize has stabilized, we can allocate our i/o buffer
53410Sstevel@tonic-gate 	 */
53420Sstevel@tonic-gate 	Buf_p = e_valloc(E_EXIT, Bufsize);
53430Sstevel@tonic-gate 
53440Sstevel@tonic-gate 	if (Args & OCp) { /* get destination directory */
53450Sstevel@tonic-gate 		(void) strcpy(Fullnam_p, *largv);
53460Sstevel@tonic-gate 		if (stat(Fullnam_p, &DesSt) < 0)
53470Sstevel@tonic-gate 			msg(EXTN, "Error during stat() of \"%s\"", Fullnam_p);
53480Sstevel@tonic-gate 		if ((DesSt.st_mode & Ftype) != S_IFDIR)
53490Sstevel@tonic-gate 			msg(EXT, "\"%s\" is not a directory", Fullnam_p);
53500Sstevel@tonic-gate 	}
53510Sstevel@tonic-gate 	Full_p = Fullnam_p + strlen(Fullnam_p) - 1;
53520Sstevel@tonic-gate 	if (*Full_p != '/') {
53530Sstevel@tonic-gate 		Full_p++;
53540Sstevel@tonic-gate 		*Full_p = '/';
53550Sstevel@tonic-gate 	}
53560Sstevel@tonic-gate 	Full_p++;
53570Sstevel@tonic-gate 	*Full_p = '\0';
53580Sstevel@tonic-gate 	(void) strcpy(Lnknam_p, Fullnam_p);
53590Sstevel@tonic-gate 	Lnkend_p = Lnknam_p + strlen(Lnknam_p);
53600Sstevel@tonic-gate 	(void) getrlimit(RLIMIT_FSIZE, &rlim);
53610Sstevel@tonic-gate 	Max_filesz = (off_t)rlim.rlim_cur;
53620Sstevel@tonic-gate 	Lnk_hd.L_nxt_p = Lnk_hd.L_bck_p = &Lnk_hd;
53630Sstevel@tonic-gate 	Lnk_hd.L_lnk_p = (struct Lnk *)NULL;
53640Sstevel@tonic-gate }
53650Sstevel@tonic-gate 
53660Sstevel@tonic-gate /*
53670Sstevel@tonic-gate  * set_tym: Set the access and/or modification times for a file.
53680Sstevel@tonic-gate  */
53690Sstevel@tonic-gate 
53700Sstevel@tonic-gate static void
53710Sstevel@tonic-gate set_tym(int dirfd, char *nam_p, time_t atime, time_t mtime)
53720Sstevel@tonic-gate {
53730Sstevel@tonic-gate 	struct timeval times[2];
53740Sstevel@tonic-gate 
53750Sstevel@tonic-gate 	times[0].tv_sec = atime;
53760Sstevel@tonic-gate 	times[0].tv_usec = 0;
53770Sstevel@tonic-gate 	times[1].tv_sec = mtime;
53780Sstevel@tonic-gate 	times[1].tv_usec = 0;
53790Sstevel@tonic-gate 
53800Sstevel@tonic-gate 	if (futimesat(dirfd, nam_p, times) < 0) {
53810Sstevel@tonic-gate 		if (Args & OCa) {
53820Sstevel@tonic-gate 			msg(ERRN,
53830Sstevel@tonic-gate 			    "Unable to reset access time for \"%s%s%s\"",
53840Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
53850Sstevel@tonic-gate 			    nam_p : Fullnam_p,
53860Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
53870Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
53880Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
53890Sstevel@tonic-gate 			    "" : nam_p);
53900Sstevel@tonic-gate 		} else {
53910Sstevel@tonic-gate 			msg(ERRN,
53920Sstevel@tonic-gate 			    "Unable to reset modification time for \"%s%s%s\"",
53930Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
53940Sstevel@tonic-gate 			    nam_p : Fullnam_p,
53950Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
53960Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
53970Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
53980Sstevel@tonic-gate 			    "" : nam_p);
53990Sstevel@tonic-gate 		}
54000Sstevel@tonic-gate 	}
54010Sstevel@tonic-gate }
54020Sstevel@tonic-gate 
54030Sstevel@tonic-gate /*
54040Sstevel@tonic-gate  * sigint:  Catch interrupts.  If an interrupt occurs during the extraction
54050Sstevel@tonic-gate  * of a file from the archive with the -u option set, and the filename did
54060Sstevel@tonic-gate  * exist, remove the current file and restore the original file.  Then exit.
54070Sstevel@tonic-gate  */
54080Sstevel@tonic-gate 
54090Sstevel@tonic-gate /*ARGSUSED*/
54100Sstevel@tonic-gate static void
54110Sstevel@tonic-gate sigint(int sig)
54120Sstevel@tonic-gate {
54130Sstevel@tonic-gate 	char *nam_p;
54140Sstevel@tonic-gate 
54150Sstevel@tonic-gate 	(void) signal(SIGINT, SIG_IGN); /* block further signals */
54160Sstevel@tonic-gate 	if (!Finished) {
54170Sstevel@tonic-gate 		if (Args & OCi)
54180Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
54190Sstevel@tonic-gate 		else /* OCp */
54200Sstevel@tonic-gate 			nam_p = Fullnam_p;
54210Sstevel@tonic-gate 		if (*Over_p != '\0') { /* There is a temp file */
54220Sstevel@tonic-gate 			if (unlink(nam_p) < 0) {
54230Sstevel@tonic-gate 				msg(ERRN,
54240Sstevel@tonic-gate 				    "Cannot remove incomplete \"%s\"", nam_p);
54250Sstevel@tonic-gate 			}
54260Sstevel@tonic-gate 			if (rename(Over_p, nam_p) < 0) {
54270Sstevel@tonic-gate 				if (link(Over_p, nam_p) < 0) {
54280Sstevel@tonic-gate 					msg(ERRN,
54290Sstevel@tonic-gate 					    "Cannot recover original \"%s\"",
54300Sstevel@tonic-gate 					    nam_p);
54310Sstevel@tonic-gate 				}
54320Sstevel@tonic-gate 				if (unlink(Over_p)) {
54330Sstevel@tonic-gate 					msg(ERRN,
54340Sstevel@tonic-gate 					    "Cannot remove temp file \"%s\"",
54350Sstevel@tonic-gate 					    Over_p);
54360Sstevel@tonic-gate 				}
54370Sstevel@tonic-gate 			}
54380Sstevel@tonic-gate 		} else if (unlink(nam_p))
54390Sstevel@tonic-gate 			msg(ERRN,
54400Sstevel@tonic-gate 			    "Cannot remove incomplete \"%s\"", nam_p);
54410Sstevel@tonic-gate 			*Over_p = '\0';
54420Sstevel@tonic-gate 	}
54430Sstevel@tonic-gate 	exit(EXIT_CODE);
54440Sstevel@tonic-gate }
54450Sstevel@tonic-gate 
54460Sstevel@tonic-gate /*
54470Sstevel@tonic-gate  * swap: Swap bytes (-s), halfwords (-S) or or both halfwords and bytes (-b).
54480Sstevel@tonic-gate  */
54490Sstevel@tonic-gate 
54500Sstevel@tonic-gate static void
54510Sstevel@tonic-gate swap(char *buf_p, int cnt)
54520Sstevel@tonic-gate {
54530Sstevel@tonic-gate 	unsigned char tbyte;
54540Sstevel@tonic-gate 	int tcnt;
54550Sstevel@tonic-gate 	int rcnt;
54560Sstevel@tonic-gate 	ushort_t thalf;
54570Sstevel@tonic-gate 
54580Sstevel@tonic-gate 	rcnt = cnt % 4;
54590Sstevel@tonic-gate 	cnt /= 4;
54600Sstevel@tonic-gate 	if (Args & (OCb | OCs | BSM)) {
54610Sstevel@tonic-gate 		tcnt = cnt;
54620Sstevel@tonic-gate 		/* LINTED alignment */
54630Sstevel@tonic-gate 		Swp_p = (union swpbuf *)buf_p;
54640Sstevel@tonic-gate 		while (tcnt-- > 0) {
54650Sstevel@tonic-gate 			tbyte = Swp_p->s_byte[0];
54660Sstevel@tonic-gate 			Swp_p->s_byte[0] = Swp_p->s_byte[1];
54670Sstevel@tonic-gate 			Swp_p->s_byte[1] = tbyte;
54680Sstevel@tonic-gate 			tbyte = Swp_p->s_byte[2];
54690Sstevel@tonic-gate 			Swp_p->s_byte[2] = Swp_p->s_byte[3];
54700Sstevel@tonic-gate 			Swp_p->s_byte[3] = tbyte;
54710Sstevel@tonic-gate 			Swp_p++;
54720Sstevel@tonic-gate 		}
54730Sstevel@tonic-gate 		if (rcnt >= 2) {
54740Sstevel@tonic-gate 		tbyte = Swp_p->s_byte[0];
54750Sstevel@tonic-gate 		Swp_p->s_byte[0] = Swp_p->s_byte[1];
54760Sstevel@tonic-gate 		Swp_p->s_byte[1] = tbyte;
54770Sstevel@tonic-gate 		tbyte = Swp_p->s_byte[2];
54780Sstevel@tonic-gate 		}
54790Sstevel@tonic-gate 	}
54800Sstevel@tonic-gate 	if (Args & (OCb | OCS)) {
54810Sstevel@tonic-gate 		tcnt = cnt;
54820Sstevel@tonic-gate 		/* LINTED alignment */
54830Sstevel@tonic-gate 		Swp_p = (union swpbuf *)buf_p;
54840Sstevel@tonic-gate 		while (tcnt-- > 0) {
54850Sstevel@tonic-gate 			thalf = Swp_p->s_half[0];
54860Sstevel@tonic-gate 			Swp_p->s_half[0] = Swp_p->s_half[1];
54870Sstevel@tonic-gate 			Swp_p->s_half[1] = thalf;
54880Sstevel@tonic-gate 			Swp_p++;
54890Sstevel@tonic-gate 		}
54900Sstevel@tonic-gate 	}
54910Sstevel@tonic-gate }
54920Sstevel@tonic-gate 
54930Sstevel@tonic-gate /*
54940Sstevel@tonic-gate  * usage: Print the usage message on stderr and exit.
54950Sstevel@tonic-gate  */
54960Sstevel@tonic-gate 
54970Sstevel@tonic-gate static void
54980Sstevel@tonic-gate usage(void)
54990Sstevel@tonic-gate {
55000Sstevel@tonic-gate 
55010Sstevel@tonic-gate 	(void) fflush(stdout);
55020Sstevel@tonic-gate #if defined(O_XATTR)
55030Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("USAGE:\n"
55041134Sceastha 	    "\tcpio -i[bcdfkmrstuv@BSV6] [-C size] "
55051134Sceastha 	    "[-E file] [-H hdr] [-I file [-M msg]] "
55061134Sceastha 	    "[-R id] [patterns]\n"
55071134Sceastha 	    "\tcpio -o[acv@ABLV] [-C size] "
55081134Sceastha 	    "[-H hdr] [-O file [-M msg]]\n"
55091134Sceastha 	    "\tcpio -p[adlmuv@LV] [-R id] directory\n"));
55100Sstevel@tonic-gate #else
55110Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("USAGE:\n"
55121134Sceastha 	    "\tcpio -i[bcdfkmrstuvBSV6] [-C size] "
55131134Sceastha 	    "[-E file] [-H hdr] [-I file [-M msg]] "
55141134Sceastha 	    "[-R id] [patterns]\n"
55151134Sceastha 	    "\tcpio -o[acvABLV] [-C size] "
55161134Sceastha 	    "[-H hdr] [-O file [-M msg]]\n"
55171134Sceastha 	    "\tcpio -p[adlmuvLV] [-R id] directory\n"));
55180Sstevel@tonic-gate #endif
55190Sstevel@tonic-gate 	(void) fflush(stderr);
55200Sstevel@tonic-gate 	exit(EXIT_CODE);
55210Sstevel@tonic-gate }
55220Sstevel@tonic-gate 
55230Sstevel@tonic-gate /*
55240Sstevel@tonic-gate  * verbose: For each file, print either the filename (-v) or a dot (-V).
55250Sstevel@tonic-gate  * If the -t option (table of contents) is set, print either the filename,
55260Sstevel@tonic-gate  * or if the -v option is also set, print an "ls -l"-like listing.
55270Sstevel@tonic-gate  */
55280Sstevel@tonic-gate 
55290Sstevel@tonic-gate static void
55300Sstevel@tonic-gate verbose(char *nam_p)
55310Sstevel@tonic-gate {
55320Sstevel@tonic-gate 	int i, j, temp;
55330Sstevel@tonic-gate 	mode_t mode;
55340Sstevel@tonic-gate 	char modestr[12];
55350Sstevel@tonic-gate 
55360Sstevel@tonic-gate 	/*
55370Sstevel@tonic-gate 	 * The printf format and associated arguments to print the current
55380Sstevel@tonic-gate 	 * filename.  Normally, just nam_p.  If we're processing an extended
55390Sstevel@tonic-gate 	 * attribute, these are overridden.
55400Sstevel@tonic-gate 	 */
55410Sstevel@tonic-gate 	char *name_fmt = "%s";
55420Sstevel@tonic-gate 	const char *name = nam_p;
55430Sstevel@tonic-gate 	const char *attribute = NULL;
55440Sstevel@tonic-gate 
55450Sstevel@tonic-gate 	if (Gen.g_attrnam_p != (char *)NULL) {
55460Sstevel@tonic-gate 		/*
55470Sstevel@tonic-gate 		 * Translation note:
55480Sstevel@tonic-gate 		 * 'attribute' is a noun.
55490Sstevel@tonic-gate 		 */
55500Sstevel@tonic-gate 		name_fmt = gettext("%s attribute %s");
55510Sstevel@tonic-gate 
55520Sstevel@tonic-gate 		name = (Args & OCp) ? nam_p : Gen.g_attrfnam_p;
55530Sstevel@tonic-gate 		attribute = Gen.g_attrnam_p;
55540Sstevel@tonic-gate 	}
55550Sstevel@tonic-gate 
55560Sstevel@tonic-gate 	if ((Gen.g_mode == SECMODE) || ((Hdr_type == USTAR ||
55570Sstevel@tonic-gate 	    Hdr_type == TAR) && Thdr_p->tbuf.t_typeflag == 'A')) {
55580Sstevel@tonic-gate 		/* dont print ancillary file */
55590Sstevel@tonic-gate 		aclchar = '+';
55600Sstevel@tonic-gate 		return;
55610Sstevel@tonic-gate 	}
55620Sstevel@tonic-gate 	for (i = 0; i < 11; i++)
55630Sstevel@tonic-gate 		modestr[i] = '-';
55640Sstevel@tonic-gate 	modestr[i] = '\0';
55650Sstevel@tonic-gate 	modestr[i-1] = aclchar;
55660Sstevel@tonic-gate 	aclchar = ' ';
55670Sstevel@tonic-gate 
55680Sstevel@tonic-gate 	if ((Args & OCt) && (Args & OCv)) {
55690Sstevel@tonic-gate 		mode = Gen.g_mode;
55700Sstevel@tonic-gate 		for (i = 0; i < 3; i++) {
55710Sstevel@tonic-gate 			temp = (mode >> (6 - (i * 3)));
55720Sstevel@tonic-gate 			j = (i * 3) + 1;
55730Sstevel@tonic-gate 			if (S_IROTH & temp)
55740Sstevel@tonic-gate 				modestr[j] = 'r';
55750Sstevel@tonic-gate 			if (S_IWOTH & temp)
55760Sstevel@tonic-gate 				modestr[j + 1] = 'w';
55770Sstevel@tonic-gate 			if (S_IXOTH & temp)
55780Sstevel@tonic-gate 				modestr[j + 2] = 'x';
55790Sstevel@tonic-gate 		}
55800Sstevel@tonic-gate 
55810Sstevel@tonic-gate 		if (Hdr_type != BAR) {
55820Sstevel@tonic-gate 			temp = Gen.g_mode & Ftype;
55830Sstevel@tonic-gate 			switch (temp) {
55840Sstevel@tonic-gate 			case (S_IFIFO):
55850Sstevel@tonic-gate 				modestr[0] = 'p';
55860Sstevel@tonic-gate 				break;
55870Sstevel@tonic-gate 			case (S_IFCHR):
55880Sstevel@tonic-gate 				modestr[0] = 'c';
55890Sstevel@tonic-gate 				break;
55900Sstevel@tonic-gate 			case (S_IFDIR):
55910Sstevel@tonic-gate 				modestr[0] = 'd';
55920Sstevel@tonic-gate 				break;
55930Sstevel@tonic-gate 			case (S_IFBLK):
55940Sstevel@tonic-gate 				modestr[0] = 'b';
55950Sstevel@tonic-gate 				break;
55960Sstevel@tonic-gate 			case (S_IFREG): /* was initialized to '-' */
55970Sstevel@tonic-gate 				break;
55980Sstevel@tonic-gate 			case (S_IFLNK):
55990Sstevel@tonic-gate 				modestr[0] = 'l';
56000Sstevel@tonic-gate 				break;
56010Sstevel@tonic-gate 			default:
56020Sstevel@tonic-gate 				msg(ERR, "Impossible file type");
56030Sstevel@tonic-gate 			}
56040Sstevel@tonic-gate 		} else {		/* bar */
56050Sstevel@tonic-gate 			temp = Gen.g_mode & Ftype;
56060Sstevel@tonic-gate 			switch (temp) {
56070Sstevel@tonic-gate 			case (S_IFIFO):
56080Sstevel@tonic-gate 				modestr[0] = 'p';
56090Sstevel@tonic-gate 				break;
56100Sstevel@tonic-gate 			case (S_IFCHR):
56110Sstevel@tonic-gate 				modestr[0] = 'c';
56120Sstevel@tonic-gate 				break;
56130Sstevel@tonic-gate 			case (S_IFDIR):
56140Sstevel@tonic-gate 				modestr[0] = 'd';
56150Sstevel@tonic-gate 				break;
56160Sstevel@tonic-gate 			case (S_IFBLK):
56170Sstevel@tonic-gate 				modestr[0] = 'b';
56180Sstevel@tonic-gate 				break;
56190Sstevel@tonic-gate 			}
56200Sstevel@tonic-gate 			if (bar_linkflag == SYMTYPE)
56210Sstevel@tonic-gate 				modestr[0] = 'l';
56220Sstevel@tonic-gate 		}
56230Sstevel@tonic-gate 		if ((S_ISUID & Gen.g_mode) == S_ISUID)
56240Sstevel@tonic-gate 			modestr[3] = 's';
56250Sstevel@tonic-gate 		if ((S_ISVTX & Gen.g_mode) == S_ISVTX)
56260Sstevel@tonic-gate 			modestr[9] = 't';
56270Sstevel@tonic-gate 		if ((S_ISGID & G_p->g_mode) == S_ISGID && modestr[6] == 'x')
56280Sstevel@tonic-gate 			modestr[6] = 's';
56290Sstevel@tonic-gate 		else if ((S_ENFMT & Gen.g_mode) == S_ENFMT && modestr[6] != 'x')
56300Sstevel@tonic-gate 			modestr[6] = 'l';
56310Sstevel@tonic-gate 		if ((Hdr_type == TAR || Hdr_type == USTAR) && Gen.g_nlink == 0)
56320Sstevel@tonic-gate 			(void) printf("%s%4d ", modestr, (int)Gen.g_nlink+1);
56330Sstevel@tonic-gate 		else
56340Sstevel@tonic-gate 			(void) printf("%s%4d ", modestr, (int)Gen.g_nlink);
56350Sstevel@tonic-gate 		if (Lastuid == (int)Gen.g_uid) {
56360Sstevel@tonic-gate 			if (Lastuid == -1)
56370Sstevel@tonic-gate 				(void) printf("-1       ");
56380Sstevel@tonic-gate 			else
56390Sstevel@tonic-gate 				(void) printf("%-9s", Curpw_p->pw_name);
56400Sstevel@tonic-gate 		} else {
56410Sstevel@tonic-gate 			if (Curpw_p = getpwuid((int)Gen.g_uid)) {
56420Sstevel@tonic-gate 				(void) printf("%-9s", Curpw_p->pw_name);
56430Sstevel@tonic-gate 				Lastuid = (int)Gen.g_uid;
56440Sstevel@tonic-gate 			} else {
56450Sstevel@tonic-gate 				(void) printf("%-9d", (int)Gen.g_uid);
56460Sstevel@tonic-gate 				Lastuid = -1;
56470Sstevel@tonic-gate 			}
56480Sstevel@tonic-gate 		}
56490Sstevel@tonic-gate 		if (Lastgid == (int)Gen.g_gid) {
56500Sstevel@tonic-gate 			if (Lastgid == -1)
56510Sstevel@tonic-gate 				(void) printf("-1       ");
56520Sstevel@tonic-gate 			else
56530Sstevel@tonic-gate 				(void) printf("%-9s", Curgr_p->gr_name);
56540Sstevel@tonic-gate 		} else {
56550Sstevel@tonic-gate 			if (Curgr_p = getgrgid((int)Gen.g_gid)) {
56560Sstevel@tonic-gate 				(void) printf("%-9s", Curgr_p->gr_name);
56570Sstevel@tonic-gate 				Lastgid = (int)Gen.g_gid;
56580Sstevel@tonic-gate 			} else {
56590Sstevel@tonic-gate 				(void) printf("%-9d", (int)Gen.g_gid);
56600Sstevel@tonic-gate 				Lastgid = -1;
56610Sstevel@tonic-gate 			}
56620Sstevel@tonic-gate 		}
56630Sstevel@tonic-gate 
56640Sstevel@tonic-gate 		/* print file size */
56650Sstevel@tonic-gate 		if (!Aspec || ((Gen.g_mode & Ftype) == S_IFIFO) ||
56660Sstevel@tonic-gate 		    (Hdr_type == BAR && bar_linkflag == SYMTYPE)) {
56670Sstevel@tonic-gate 			if (Gen.g_filesz < (1LL << 31))
56680Sstevel@tonic-gate 				(void) printf("%7lld ",
56690Sstevel@tonic-gate 				    (offset_t)Gen.g_filesz);
56700Sstevel@tonic-gate 			else
56710Sstevel@tonic-gate 				(void) printf("%11lld ",
56720Sstevel@tonic-gate 				    (offset_t)Gen.g_filesz);
56730Sstevel@tonic-gate 		} else
56740Sstevel@tonic-gate 			(void) printf("%3d,%3d ", (int)major(Gen.g_rdev),
56750Sstevel@tonic-gate 			    (int)minor(Gen.g_rdev));
56760Sstevel@tonic-gate 		(void) cftime(Time, dcgettext(NULL, FORMAT, LC_TIME),
56770Sstevel@tonic-gate 		    (time_t *)&Gen.g_mtime);
56780Sstevel@tonic-gate 		(void) printf("%s, ", Time);
56790Sstevel@tonic-gate 		(void) printf(name_fmt, name, attribute);
56800Sstevel@tonic-gate 		if ((Gen.g_mode & Ftype) == S_IFLNK) {
56810Sstevel@tonic-gate 			if (Hdr_type == USTAR || Hdr_type == TAR)
56820Sstevel@tonic-gate 				(void) strcpy(Symlnk_p,
56830Sstevel@tonic-gate 				    Thdr_p->tbuf.t_linkname);
56840Sstevel@tonic-gate 			else {
56850Sstevel@tonic-gate 				(void) strncpy(Symlnk_p, Buffr.b_out_p,
56860Sstevel@tonic-gate 				    Gen.g_filesz);
56870Sstevel@tonic-gate 				*(Symlnk_p + Gen.g_filesz) = '\0';
56880Sstevel@tonic-gate 			}
56890Sstevel@tonic-gate 			(void) printf(" -> %s", Symlnk_p);
56900Sstevel@tonic-gate 		}
56910Sstevel@tonic-gate 		if (Hdr_type == BAR) {
56920Sstevel@tonic-gate 			if (bar_linkflag == SYMTYPE)
56930Sstevel@tonic-gate 				(void) printf(gettext(" symbolic link to %s"),
56940Sstevel@tonic-gate 				    bar_linkname);
56950Sstevel@tonic-gate 			else if (bar_linkflag == '1')
56960Sstevel@tonic-gate 				(void) printf(gettext(" linked to %s"),
56970Sstevel@tonic-gate 				    bar_linkname);
56980Sstevel@tonic-gate 		}
56990Sstevel@tonic-gate 		if ((Hdr_type == USTAR || Hdr_type == TAR) &&
57000Sstevel@tonic-gate 		    Thdr_p->tbuf.t_typeflag == '1') {
57010Sstevel@tonic-gate 			(void) printf(gettext(" linked to %s%s%s"),
57020Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
57030Sstevel@tonic-gate 			    Thdr_p->tbuf.t_linkname : Gen.g_attrfnam_p,
57040Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
57050Sstevel@tonic-gate 			    "" : gettext(" attribute "),
57060Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
57070Sstevel@tonic-gate 			    "" : Gen.g_linktoattrnam_p);
57080Sstevel@tonic-gate 		}
57090Sstevel@tonic-gate 		(void) printf("\n");
57100Sstevel@tonic-gate 	} else if ((Args & OCt) || (Args & OCv)) {
57110Sstevel@tonic-gate 		(void) fprintf(Out_p, name_fmt, name, attribute);
57120Sstevel@tonic-gate 		(void) fputc('\n', Out_p);
57130Sstevel@tonic-gate 	} else { /* OCV */
57140Sstevel@tonic-gate 		(void) fputc('.', Out_p);
57150Sstevel@tonic-gate 		if (Verbcnt++ >= 49) { /* start a new line of dots */
57160Sstevel@tonic-gate 			Verbcnt = 0;
57170Sstevel@tonic-gate 			(void) fputc('\n', Out_p);
57180Sstevel@tonic-gate 		}
57190Sstevel@tonic-gate 	}
57200Sstevel@tonic-gate 	(void) fflush(Out_p);
57210Sstevel@tonic-gate }
57220Sstevel@tonic-gate 
57230Sstevel@tonic-gate #define	MK_USHORT(a)	(a & 00000177777)
57240Sstevel@tonic-gate 
57250Sstevel@tonic-gate /*
57260Sstevel@tonic-gate  * write_hdr: Transfer header information for the generic structure
57270Sstevel@tonic-gate  * into the format for the selected header and bwrite() the header.
57280Sstevel@tonic-gate  * ACL support: add two new argumnets. secflag indicates that it's an
57290Sstevel@tonic-gate  *	ancillary file. len is the size of the file (incl. all security
57300Sstevel@tonic-gate  *	attributes). We only have acls now.
57310Sstevel@tonic-gate  */
57320Sstevel@tonic-gate 
57330Sstevel@tonic-gate static void
57340Sstevel@tonic-gate write_hdr(int secflag, off_t len)
57350Sstevel@tonic-gate {
57360Sstevel@tonic-gate 	int cnt, pad;
57370Sstevel@tonic-gate 	mode_t mode;
57380Sstevel@tonic-gate 	uid_t uid;
57390Sstevel@tonic-gate 	gid_t gid;
57400Sstevel@tonic-gate 	const char warnfmt[] = "%s%s%s : %s";
57410Sstevel@tonic-gate 
57420Sstevel@tonic-gate 	if (secflag == ARCHIVE_ACL) {
57430Sstevel@tonic-gate 		mode = SECMODE;
57440Sstevel@tonic-gate 	} else {
57450Sstevel@tonic-gate 		/*
57460Sstevel@tonic-gate 		 * If attribute is being archived in cpio format then
57470Sstevel@tonic-gate 		 * zap off the file type bits since those are truly a
57480Sstevel@tonic-gate 		 * mask and reset them with _XATTR_CPIO_MODE
57490Sstevel@tonic-gate 		 */
57500Sstevel@tonic-gate 
57510Sstevel@tonic-gate 		/*
57520Sstevel@tonic-gate 		 * len is the value of g_filesz for normal files
57530Sstevel@tonic-gate 		 * and the length of the special header buffer in
57540Sstevel@tonic-gate 		 * the case of acl and xattr headers.
57550Sstevel@tonic-gate 		 */
57560Sstevel@tonic-gate 		if (G_p->g_attrnam_p != (char *)NULL && Hdr_type != USTAR &&
57570Sstevel@tonic-gate 		    Hdr_type != TAR) {
5758*1231Smarks 			mode = (G_p->g_mode & POSIXMODES) | _XATTR_CPIO_MODE;
57590Sstevel@tonic-gate 		} else {
57600Sstevel@tonic-gate 			mode = G_p->g_mode;
57610Sstevel@tonic-gate 		}
57620Sstevel@tonic-gate 		if (secflag != ARCHIVE_XATTR) {
57630Sstevel@tonic-gate 			len = G_p->g_filesz;
57640Sstevel@tonic-gate 		}
57650Sstevel@tonic-gate 	}
57660Sstevel@tonic-gate 
57670Sstevel@tonic-gate 	uid = G_p->g_uid;
57680Sstevel@tonic-gate 	gid = G_p->g_gid;
57690Sstevel@tonic-gate 	/*
57700Sstevel@tonic-gate 	 * Handle EFT uids and gids.  If they get too big
57710Sstevel@tonic-gate 	 * to be represented in a particular format, force 'em to 'nobody'.
57720Sstevel@tonic-gate 	 */
57730Sstevel@tonic-gate 	switch (Hdr_type) {
57740Sstevel@tonic-gate 	case BIN:			/* 16-bits of u_short */
57750Sstevel@tonic-gate 		if ((ulong_t)uid > (ulong_t)USHRT_MAX)
57760Sstevel@tonic-gate 			uid = UID_NOBODY;
57770Sstevel@tonic-gate 		if ((ulong_t)gid > (ulong_t)USHRT_MAX)
57780Sstevel@tonic-gate 			gid = GID_NOBODY;
57790Sstevel@tonic-gate 		break;
57800Sstevel@tonic-gate 	case CHR:			/* %.6lo => 262143 base 10 */
57810Sstevel@tonic-gate 		if ((ulong_t)uid > (ulong_t)0777777)
57820Sstevel@tonic-gate 			uid = UID_NOBODY;
57830Sstevel@tonic-gate 		if ((ulong_t)gid > (ulong_t)0777777)
57840Sstevel@tonic-gate 			gid = GID_NOBODY;
57850Sstevel@tonic-gate 		break;
57860Sstevel@tonic-gate 	case ASC:			/* %.8lx => full 32 bits */
57870Sstevel@tonic-gate 	case CRC:
57880Sstevel@tonic-gate 		break;
57890Sstevel@tonic-gate 	case USTAR:
57900Sstevel@tonic-gate 	case TAR:			/* %.7lo => 2097151 base 10 */
57910Sstevel@tonic-gate 		if ((ulong_t)uid > (ulong_t)07777777)
57920Sstevel@tonic-gate 			uid = UID_NOBODY;
57930Sstevel@tonic-gate 		if ((ulong_t)gid > (ulong_t)07777777)
57940Sstevel@tonic-gate 			gid = GID_NOBODY;
57950Sstevel@tonic-gate 		break;
57960Sstevel@tonic-gate 	default:
57970Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
57980Sstevel@tonic-gate 	}
57990Sstevel@tonic-gate 
58000Sstevel@tonic-gate 	/*
58010Sstevel@tonic-gate 	 * Since cpio formats -don't- encode the symbolic names, print
58020Sstevel@tonic-gate 	 * a warning message when we map the uid or gid this way.
58030Sstevel@tonic-gate 	 * Also, if the ownership just changed, clear set[ug]id bits
58040Sstevel@tonic-gate 	 *
58050Sstevel@tonic-gate 	 * (Except for USTAR format of course, where we have a string
58060Sstevel@tonic-gate 	 * representation of the username embedded in the header)
58070Sstevel@tonic-gate 	 */
58080Sstevel@tonic-gate 	if (uid != G_p->g_uid && Hdr_type != USTAR) {
58090Sstevel@tonic-gate 		msg(ERR, warnfmt,
58100Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
58110Sstevel@tonic-gate 		    G_p->g_nam_p : G_p->g_attrfnam_p,
58120Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
58130Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
58140Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
58150Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p,
58160Sstevel@tonic-gate 		    gettext("uid too large for archive format"));
58170Sstevel@tonic-gate 		if (S_ISREG(mode))
58180Sstevel@tonic-gate 			mode &= ~S_ISUID;
58190Sstevel@tonic-gate 	}
58200Sstevel@tonic-gate 	if (gid != G_p->g_gid && Hdr_type != USTAR) {
58210Sstevel@tonic-gate 		msg(ERR, warnfmt,
58220Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
58230Sstevel@tonic-gate 		    G_p->g_nam_p : G_p->g_attrfnam_p,
58240Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
58250Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
58260Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
58270Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p,
58280Sstevel@tonic-gate 		    gettext("gid too large for archive format"));
58290Sstevel@tonic-gate 		if (S_ISREG(mode))
58300Sstevel@tonic-gate 			mode &= ~S_ISGID;
58310Sstevel@tonic-gate 	}
58320Sstevel@tonic-gate 
58330Sstevel@tonic-gate 	switch (Hdr_type) {
58340Sstevel@tonic-gate 	case BIN:
58350Sstevel@tonic-gate 	case CHR:
58360Sstevel@tonic-gate 	case ASC:
58370Sstevel@tonic-gate 	case CRC:
58380Sstevel@tonic-gate 		cnt = Hdrsz + G_p->g_namesz;
58390Sstevel@tonic-gate 		break;
58400Sstevel@tonic-gate 	case TAR:
58410Sstevel@tonic-gate 		/*FALLTHROUGH*/
58420Sstevel@tonic-gate 	case USTAR: /* TAR and USTAR */
58430Sstevel@tonic-gate 		cnt = TARSZ;
58440Sstevel@tonic-gate 		break;
58450Sstevel@tonic-gate 	default:
58460Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
58470Sstevel@tonic-gate 	}
58480Sstevel@tonic-gate 	FLUSH(cnt);
58490Sstevel@tonic-gate 
58500Sstevel@tonic-gate 	switch (Hdr_type) {
58510Sstevel@tonic-gate 	case BIN:
58520Sstevel@tonic-gate 		Hdr.h_magic = (short)G_p->g_magic;
58530Sstevel@tonic-gate 		Hdr.h_dev = G_p->g_dev;
58540Sstevel@tonic-gate 		Hdr.h_ino = G_p->g_ino;
58550Sstevel@tonic-gate 		Hdr.h_uid = uid;
58560Sstevel@tonic-gate 		Hdr.h_gid = gid;
58570Sstevel@tonic-gate 		Hdr.h_mode = mode;
58580Sstevel@tonic-gate 		Hdr.h_nlink = G_p->g_nlink;
58590Sstevel@tonic-gate 		Hdr.h_rdev = G_p->g_rdev;
58600Sstevel@tonic-gate 		mkshort(Hdr.h_mtime, (long)G_p->g_mtime);
58610Sstevel@tonic-gate 		Hdr.h_namesize = (short)G_p->g_namesz;
58620Sstevel@tonic-gate 		mkshort(Hdr.h_filesize, (long)len);
58630Sstevel@tonic-gate 		(void) strcpy(Hdr.h_name, G_p->g_nam_p);
58640Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, &Hdr, cnt);
58650Sstevel@tonic-gate 		break;
58660Sstevel@tonic-gate 	case CHR:
58670Sstevel@tonic-gate 		(void) sprintf(Buffr.b_in_p,
58680Sstevel@tonic-gate 		    "%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.11lo%.6lo%."
58690Sstevel@tonic-gate 		    "11llo%s", G_p->g_magic, G_p->g_dev, G_p->g_ino, mode,
58700Sstevel@tonic-gate 		    uid, gid, G_p->g_nlink, MK_USHORT(G_p->g_rdev),
58710Sstevel@tonic-gate 		    G_p->g_mtime, (long)G_p->g_namesz, (offset_t)len,
58720Sstevel@tonic-gate 		    G_p->g_nam_p);
58730Sstevel@tonic-gate 		break;
58740Sstevel@tonic-gate 	case ASC:
58750Sstevel@tonic-gate 	case CRC:
58760Sstevel@tonic-gate 		(void) sprintf(Buffr.b_in_p,
58770Sstevel@tonic-gate 		    "%.6lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%."
58780Sstevel@tonic-gate 		    "8lx%.8lx%.8lx%.8lx%s",
58791134Sceastha 		    G_p->g_magic, G_p->g_ino, mode, G_p->g_uid,
58801134Sceastha 		    G_p->g_gid, G_p->g_nlink, G_p->g_mtime, (ulong_t)len,
58811134Sceastha 		    major(G_p->g_dev), minor(G_p->g_dev),
58821134Sceastha 		    major(G_p->g_rdev), minor(G_p->g_rdev),
58831134Sceastha 		    G_p->g_namesz, G_p->g_cksum, G_p->g_nam_p);
58840Sstevel@tonic-gate 		break;
58850Sstevel@tonic-gate 	case USTAR:
58860Sstevel@tonic-gate 		Thdr_p = (union tblock *)Buffr.b_in_p;
58870Sstevel@tonic-gate 		(void) memcpy(Thdr_p, Empty, TARSZ);
58880Sstevel@tonic-gate 		(void) strncpy(Thdr_p->tbuf.t_name, G_p->g_tname,
58890Sstevel@tonic-gate 		    (int)strlen(G_p->g_tname));
58900Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mode, "%07o", (int)mode);
58910Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_uid, "%07o", (int)uid);
58920Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_gid, "%07o", (int)gid);
58930Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_size, "%011llo",
58940Sstevel@tonic-gate 		    (offset_t)len);
58950Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mtime, "%011lo", G_p->g_mtime);
58960Sstevel@tonic-gate 		if (secflag == ARCHIVE_ACL) {
58970Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = 'A';	/* ACL file type */
58980Sstevel@tonic-gate 		} else if (secflag == ARCHIVE_XATTR ||
58990Sstevel@tonic-gate 		    (G_p->g_attrnam_p != (char *)NULL)) {
59000Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = _XATTR_HDRTYPE;
59010Sstevel@tonic-gate 		} else {
59020Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = G_p->g_typeflag;
59030Sstevel@tonic-gate 		}
59040Sstevel@tonic-gate 		if (T_lname[0] != '\0') {
59050Sstevel@tonic-gate 			/*
59060Sstevel@tonic-gate 			 * if not a symbolic link
59070Sstevel@tonic-gate 			 */
59080Sstevel@tonic-gate 			if (((G_p->g_mode & Ftype) != S_IFLNK) &&
59090Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL)) {
59100Sstevel@tonic-gate 				Thdr_p->tbuf.t_typeflag = LNKTYPE;
59110Sstevel@tonic-gate 				(void) sprintf(Thdr_p->tbuf.t_size,
59120Sstevel@tonic-gate 				    "%011lo", 0L);
59130Sstevel@tonic-gate 			}
59140Sstevel@tonic-gate 			(void) strncpy(Thdr_p->tbuf.t_linkname, T_lname,
59150Sstevel@tonic-gate 			    strlen(T_lname));
59160Sstevel@tonic-gate 		}
59170Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_magic, "%s", TMAGIC);
59180Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_version, "%2s", TVERSION);
59190Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_uname, "%s",  G_p->g_uname);
59200Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_gname, "%s", G_p->g_gname);
59210Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_devmajor, "%07o",
59220Sstevel@tonic-gate 		    (int)major(G_p->g_rdev));
59230Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_devminor, "%07o",
59240Sstevel@tonic-gate 		    (int)minor(G_p->g_rdev));
59250Sstevel@tonic-gate 		if (Gen.g_prefix) {
59260Sstevel@tonic-gate 			(void) sprintf(Thdr_p->tbuf.t_prefix, "%s",
59270Sstevel@tonic-gate 			    Gen.g_prefix);
59280Sstevel@tonic-gate 			free(Gen.g_prefix);
59290Sstevel@tonic-gate 			Gen.g_prefix = NULL;
59300Sstevel@tonic-gate 		} else {
59310Sstevel@tonic-gate 			(void) sprintf(Thdr_p->tbuf.t_prefix, "%s", "");
59320Sstevel@tonic-gate 		}
59330Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_cksum, "%07o",
59340Sstevel@tonic-gate 		    (int)cksum(TARTYP, 0, NULL));
59350Sstevel@tonic-gate 		break;
59360Sstevel@tonic-gate 	case TAR:
59370Sstevel@tonic-gate 		Thdr_p = (union tblock *)Buffr.b_in_p;
59380Sstevel@tonic-gate 		(void) memcpy(Thdr_p, Empty, TARSZ);
59390Sstevel@tonic-gate 		(void) strncpy(Thdr_p->tbuf.t_name, G_p->g_nam_p,
59400Sstevel@tonic-gate 		    G_p->g_namesz);
59410Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mode, "%07o ", (int)mode);
59420Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_uid, "%07o ", (int)uid);
59430Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_gid, "%07o ", (int)gid);
59440Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_size, "%011llo ",
59450Sstevel@tonic-gate 		    (offset_t)len);
59460Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mtime, "%011o ",
59470Sstevel@tonic-gate 		    (int)G_p->g_mtime);
59480Sstevel@tonic-gate 		if (T_lname[0] != '\0') {
59490Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = '1';
59500Sstevel@tonic-gate 		} else {
59510Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = '\0';
59520Sstevel@tonic-gate 		}
59530Sstevel@tonic-gate 		(void) strncpy(Thdr_p->tbuf.t_linkname, T_lname,
59540Sstevel@tonic-gate 		    strlen(T_lname));
59550Sstevel@tonic-gate 		break;
59560Sstevel@tonic-gate 	default:
59570Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
59580Sstevel@tonic-gate 	} /* Hdr_type */
59590Sstevel@tonic-gate 
59600Sstevel@tonic-gate 	Buffr.b_in_p += cnt;
59610Sstevel@tonic-gate 	Buffr.b_cnt += cnt;
59620Sstevel@tonic-gate 	pad = ((cnt + Pad_val) & ~Pad_val) - cnt;
59630Sstevel@tonic-gate 	if (pad != 0) {
59640Sstevel@tonic-gate 		FLUSH(pad);
59650Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, Empty, pad);
59660Sstevel@tonic-gate 		Buffr.b_in_p += pad;
59670Sstevel@tonic-gate 		Buffr.b_cnt += pad;
59680Sstevel@tonic-gate 	}
59690Sstevel@tonic-gate }
59700Sstevel@tonic-gate 
59710Sstevel@tonic-gate /*
59720Sstevel@tonic-gate  * write_trail: Create the appropriate trailer for the selected header type
59730Sstevel@tonic-gate  * and bwrite the trailer.  Pad the buffer with nulls out to the next Bufsize
59740Sstevel@tonic-gate  * boundary, and force a write.  If the write completes, or if the trailer is
59750Sstevel@tonic-gate  * completely written (but not all of the padding nulls (as can happen on end
59760Sstevel@tonic-gate  * of medium)) return.  Otherwise, the trailer was not completely written out,
59770Sstevel@tonic-gate  * so re-pad the buffer with nulls and try again.
59780Sstevel@tonic-gate  */
59790Sstevel@tonic-gate 
59800Sstevel@tonic-gate static void
59810Sstevel@tonic-gate write_trail(void)
59820Sstevel@tonic-gate {
59830Sstevel@tonic-gate 	int cnt, need;
59840Sstevel@tonic-gate 
59850Sstevel@tonic-gate 	switch (Hdr_type) {
59860Sstevel@tonic-gate 	case BIN:
59870Sstevel@tonic-gate 		Gen.g_magic = CMN_BIN;
59880Sstevel@tonic-gate 		break;
59890Sstevel@tonic-gate 	case CHR:
59900Sstevel@tonic-gate 		Gen.g_magic = CMN_BIN;
59910Sstevel@tonic-gate 		break;
59920Sstevel@tonic-gate 	case ASC:
59930Sstevel@tonic-gate 		Gen.g_magic = CMN_ASC;
59940Sstevel@tonic-gate 		break;
59950Sstevel@tonic-gate 	case CRC:
59960Sstevel@tonic-gate 		Gen.g_magic = CMN_CRC;
59970Sstevel@tonic-gate 		break;
59980Sstevel@tonic-gate 	}
59990Sstevel@tonic-gate 
60000Sstevel@tonic-gate 	switch (Hdr_type) {
60010Sstevel@tonic-gate 	case BIN:
60020Sstevel@tonic-gate 	case CHR:
60030Sstevel@tonic-gate 	case ASC:
60040Sstevel@tonic-gate 	case CRC:
60050Sstevel@tonic-gate 		Gen.g_mode = Gen.g_uid = Gen.g_gid = 0;
60060Sstevel@tonic-gate 		Gen.g_nlink = 1;
60070Sstevel@tonic-gate 		Gen.g_mtime = Gen.g_ino = Gen.g_dev = 0;
60080Sstevel@tonic-gate 		Gen.g_rdev = Gen.g_cksum = 0;
60090Sstevel@tonic-gate 		Gen.g_filesz = (off_t)0;
60100Sstevel@tonic-gate 		Gen.g_namesz = strlen("TRAILER!!!") + 1;
60110Sstevel@tonic-gate 		(void) strcpy(Gen.g_nam_p, "TRAILER!!!");
60120Sstevel@tonic-gate 		G_p = &Gen;
60130Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
60140Sstevel@tonic-gate 		break;
60150Sstevel@tonic-gate 	case TAR:
60160Sstevel@tonic-gate 	/*FALLTHROUGH*/
60170Sstevel@tonic-gate 	case USTAR: /* TAR and USTAR */
60180Sstevel@tonic-gate 		for (cnt = 0; cnt < 3; cnt++) {
60190Sstevel@tonic-gate 			FLUSH(TARSZ);
60200Sstevel@tonic-gate 			(void) memcpy(Buffr.b_in_p, Empty, TARSZ);
60210Sstevel@tonic-gate 			Buffr.b_in_p += TARSZ;
60220Sstevel@tonic-gate 			Buffr.b_cnt += TARSZ;
60230Sstevel@tonic-gate 		}
60240Sstevel@tonic-gate 		break;
60250Sstevel@tonic-gate 	default:
60260Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
60270Sstevel@tonic-gate 	}
60280Sstevel@tonic-gate 	need = Bufsize - (Buffr.b_cnt % Bufsize);
60290Sstevel@tonic-gate 	if (need == Bufsize)
60300Sstevel@tonic-gate 		need = 0;
60310Sstevel@tonic-gate 
60320Sstevel@tonic-gate 	while (Buffr.b_cnt > 0) {
60330Sstevel@tonic-gate 		while (need > 0) {
60340Sstevel@tonic-gate 			cnt = (need < TARSZ) ? need : TARSZ;
60350Sstevel@tonic-gate 			need -= cnt;
60360Sstevel@tonic-gate 			FLUSH(cnt);
60370Sstevel@tonic-gate 			(void) memcpy(Buffr.b_in_p, Empty, cnt);
60380Sstevel@tonic-gate 			Buffr.b_in_p += cnt;
60390Sstevel@tonic-gate 			Buffr.b_cnt += cnt;
60400Sstevel@tonic-gate 		}
60410Sstevel@tonic-gate 		bflush();
60420Sstevel@tonic-gate 	}
60430Sstevel@tonic-gate }
60440Sstevel@tonic-gate 
60450Sstevel@tonic-gate /*
60460Sstevel@tonic-gate  * if archives in USTAR format, check if typeflag == '5' for directories
60470Sstevel@tonic-gate  */
60480Sstevel@tonic-gate static int
60490Sstevel@tonic-gate ustar_dir(void)
60500Sstevel@tonic-gate {
60510Sstevel@tonic-gate 	if (Hdr_type == USTAR || Hdr_type == TAR) {
60520Sstevel@tonic-gate 		if (Thdr_p->tbuf.t_typeflag == '5')
60530Sstevel@tonic-gate 			return (1);
60540Sstevel@tonic-gate 	}
60550Sstevel@tonic-gate 	return (0);
60560Sstevel@tonic-gate }
60570Sstevel@tonic-gate 
60580Sstevel@tonic-gate /*
60590Sstevel@tonic-gate  * if archives in USTAR format, check if typeflag == '3' || '4' || '6'
60600Sstevel@tonic-gate  * for character, block, fifo special files
60610Sstevel@tonic-gate  */
60620Sstevel@tonic-gate static int
60630Sstevel@tonic-gate ustar_spec(void)
60640Sstevel@tonic-gate {
60650Sstevel@tonic-gate 	int typeflag;
60660Sstevel@tonic-gate 
60670Sstevel@tonic-gate 	if (Hdr_type == USTAR || Hdr_type == TAR) {
60680Sstevel@tonic-gate 		typeflag = Thdr_p->tbuf.t_typeflag;
60690Sstevel@tonic-gate 		if (typeflag == '3' || typeflag == '4' || typeflag == '6')
60700Sstevel@tonic-gate 			return (1);
60710Sstevel@tonic-gate 	}
60720Sstevel@tonic-gate 	return (0);
60730Sstevel@tonic-gate }
60740Sstevel@tonic-gate 
60750Sstevel@tonic-gate /*
60760Sstevel@tonic-gate  * The return value is a pointer to a converted copy of the information in
60770Sstevel@tonic-gate  * FromStat if the file is representable in -Hodc format, and NULL otherwise.
60780Sstevel@tonic-gate  */
60790Sstevel@tonic-gate 
60800Sstevel@tonic-gate static struct stat *
60810Sstevel@tonic-gate convert_to_old_stat(struct stat *FromStat, char *namep, char *attrp)
60820Sstevel@tonic-gate {
60830Sstevel@tonic-gate 	static struct stat ToSt;
60840Sstevel@tonic-gate 	cpioinfo_t TmpSt;
60850Sstevel@tonic-gate 
60860Sstevel@tonic-gate 	(void) memset(&TmpSt, 0, sizeof (cpioinfo_t));
60870Sstevel@tonic-gate 	stat_to_svr32_stat(&TmpSt, FromStat);
60880Sstevel@tonic-gate 	(void) memset(&ToSt, 0, sizeof (ToSt));
60890Sstevel@tonic-gate 
60900Sstevel@tonic-gate 	if (TmpSt.st_rdev == (o_dev_t)NODEV &&
60910Sstevel@tonic-gate 	    (((TmpSt.st_mode & Ftype) == S_IFCHR) ||
60920Sstevel@tonic-gate 	    ((TmpSt.st_mode & Ftype) == S_IFBLK))) {
60930Sstevel@tonic-gate 		/*
60940Sstevel@tonic-gate 		 * Encountered a problem representing the rdev information.
60950Sstevel@tonic-gate 		 * Don't archive it.
60960Sstevel@tonic-gate 		 */
60970Sstevel@tonic-gate 
60980Sstevel@tonic-gate 		msg(ERR, "Error -Hodc format can't support expanded"
60990Sstevel@tonic-gate 		    "types on %s%s%s",
61000Sstevel@tonic-gate 		    namep,
61010Sstevel@tonic-gate 		    (attrp == NULL) ? "" : gettext(" Attribute"),
61020Sstevel@tonic-gate 		    (attrp == NULL) ? "" : attrp);
61030Sstevel@tonic-gate 		return (NULL);
61040Sstevel@tonic-gate 	}
61050Sstevel@tonic-gate 
61060Sstevel@tonic-gate 	if (TmpSt.st_dev == (o_dev_t)NODEV) {
61070Sstevel@tonic-gate 		/*
61080Sstevel@tonic-gate 		 * Having trouble representing the device/inode pair.  We can't
61090Sstevel@tonic-gate 		 * track links in this case; break them all into separate
61100Sstevel@tonic-gate 		 * files.
61110Sstevel@tonic-gate 		 */
61120Sstevel@tonic-gate 
61130Sstevel@tonic-gate 		TmpSt.st_ino = 0;
61140Sstevel@tonic-gate 
61150Sstevel@tonic-gate 		if (((TmpSt.st_mode & Ftype) != S_IFDIR) &&
61160Sstevel@tonic-gate 		    TmpSt.st_nlink > 1)
61170Sstevel@tonic-gate 			msg(POST,
61180Sstevel@tonic-gate 			    "Warning: file %s%s%s has large "
61190Sstevel@tonic-gate 			    "device number - linked "
61200Sstevel@tonic-gate 			    "files will be restored as "
61210Sstevel@tonic-gate 			    "separate files",
61220Sstevel@tonic-gate 			    namep,
61230Sstevel@tonic-gate 			    (attrp == NULL) ? "" : gettext(" Attribute"),
61240Sstevel@tonic-gate 			    (attrp == NULL) ? "" : attrp);
61250Sstevel@tonic-gate 
61260Sstevel@tonic-gate 		/* ensure no links */
61270Sstevel@tonic-gate 
61280Sstevel@tonic-gate 		TmpSt.st_nlink = 1;
61290Sstevel@tonic-gate 	}
61300Sstevel@tonic-gate 
61310Sstevel@tonic-gate 	/* Start converting values */
61320Sstevel@tonic-gate 
61330Sstevel@tonic-gate 	if (TmpSt.st_dev < 0) {
61340Sstevel@tonic-gate 		ToSt.st_dev = 0;
61350Sstevel@tonic-gate 	} else {
61360Sstevel@tonic-gate 		ToSt.st_dev = (dev_t)TmpSt.st_dev;
61370Sstevel@tonic-gate 	}
61380Sstevel@tonic-gate 
61390Sstevel@tonic-gate 	/* -actual- not truncated uid */
61400Sstevel@tonic-gate 
61410Sstevel@tonic-gate 	ToSt.st_uid = TmpSt.st_uid;
61420Sstevel@tonic-gate 
61430Sstevel@tonic-gate 	/* -actual- not truncated gid */
61440Sstevel@tonic-gate 
61450Sstevel@tonic-gate 	ToSt.st_gid = TmpSt.st_gid;
61460Sstevel@tonic-gate 	ToSt.st_ino = (ino_t)TmpSt.st_ino;
61470Sstevel@tonic-gate 	ToSt.st_mode = (mode_t)TmpSt.st_mode;
61480Sstevel@tonic-gate 	ToSt.st_mtime = (ulong_t)TmpSt.st_modtime;
61490Sstevel@tonic-gate 	ToSt.st_nlink = (nlink_t)TmpSt.st_nlink;
61500Sstevel@tonic-gate 	ToSt.st_size = (off_t)TmpSt.st_size;
61510Sstevel@tonic-gate 	ToSt.st_rdev = (dev_t)TmpSt.st_rdev;
61520Sstevel@tonic-gate 
61530Sstevel@tonic-gate 	return (&ToSt);
61540Sstevel@tonic-gate }
61550Sstevel@tonic-gate 
61560Sstevel@tonic-gate /*
61570Sstevel@tonic-gate  * In the beginning of each bar archive, there is a header which describes the
61580Sstevel@tonic-gate  * current volume being created, followed by a header which describes the
61590Sstevel@tonic-gate  * current file being created, followed by the file itself.  If there is
61600Sstevel@tonic-gate  * more than one file to be created, a separate header will be created for
61610Sstevel@tonic-gate  * each additional file.  This structure may be repeated if the bar archive
61620Sstevel@tonic-gate  * contains multiple volumes.  If a file spans across volumes, its header
61630Sstevel@tonic-gate  * will not be repeated in the next volume.
61640Sstevel@tonic-gate  *               +------------------+
61650Sstevel@tonic-gate  *               |    vol header    |
61660Sstevel@tonic-gate  *               |------------------|
61670Sstevel@tonic-gate  *               |   file header i  |     i = 0
61680Sstevel@tonic-gate  *               |------------------|
61690Sstevel@tonic-gate  *               |     <file i>     |
61700Sstevel@tonic-gate  *               |------------------|
61710Sstevel@tonic-gate  *               |  file header i+1 |
61720Sstevel@tonic-gate  *               |------------------|
61730Sstevel@tonic-gate  *               |    <file i+1>    |
61740Sstevel@tonic-gate  *               |------------------|
61750Sstevel@tonic-gate  *               |        .         |
61760Sstevel@tonic-gate  *               |        .         |
61770Sstevel@tonic-gate  *               |        .         |
61780Sstevel@tonic-gate  *               +------------------+
61790Sstevel@tonic-gate  */
61800Sstevel@tonic-gate 
61810Sstevel@tonic-gate /*
61820Sstevel@tonic-gate  * read in the header that describes the current volume of the bar archive
61830Sstevel@tonic-gate  * to be extracted.
61840Sstevel@tonic-gate  */
61850Sstevel@tonic-gate static void
61860Sstevel@tonic-gate read_bar_vol_hdr(void)
61870Sstevel@tonic-gate {
61880Sstevel@tonic-gate 	union b_block *tmp_hdr;
61890Sstevel@tonic-gate 
61900Sstevel@tonic-gate 	tmp_hdr = (union b_block *)Buffr.b_out_p;
61910Sstevel@tonic-gate 	if (tmp_hdr->dbuf.bar_magic[0] == BAR_VOLUME_MAGIC) {
61920Sstevel@tonic-gate 
61930Sstevel@tonic-gate 		if (bar_Vhdr == NULL) {
61940Sstevel@tonic-gate 			bar_Vhdr = e_zalloc(E_EXIT, TBLOCK);
61950Sstevel@tonic-gate 		}
61960Sstevel@tonic-gate 		(void) memcpy(&(bar_Vhdr->dbuf), &(tmp_hdr->dbuf), TBLOCK);
61970Sstevel@tonic-gate 	} else {
61980Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
61990Sstevel@tonic-gate 		    "bar error: cannot read volume header\n"));
62000Sstevel@tonic-gate 		exit(1);
62010Sstevel@tonic-gate 	}
62020Sstevel@tonic-gate 
62030Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.mode, "%8lo", &Gen_bar_vol.g_mode);
62040Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.uid, "%8d", (int *)&Gen_bar_vol.g_uid);
62050Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.gid, "%8d", (int *)&Gen_bar_vol.g_gid);
62060Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.size, "%12llo",
62070Sstevel@tonic-gate 	    (u_off_t *)&Gen_bar_vol.g_filesz);
62080Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.mtime, "%12lo", &Gen_bar_vol.g_mtime);
62090Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.chksum, "%8lo", &Gen_bar_vol.g_cksum);
62100Sstevel@tonic-gate 
62110Sstevel@tonic-gate 	/* set the compress flag */
62120Sstevel@tonic-gate 	if (bar_Vhdr->dbuf.compressed == '1')
62130Sstevel@tonic-gate 		Compressed = 1;
62140Sstevel@tonic-gate 	else
62150Sstevel@tonic-gate 		Compressed = 0;
62160Sstevel@tonic-gate 
62170Sstevel@tonic-gate 	Buffr.b_out_p += 512;
62180Sstevel@tonic-gate 	Buffr.b_cnt -= 512;
62190Sstevel@tonic-gate 
62200Sstevel@tonic-gate 	/*
62210Sstevel@tonic-gate 	 * not the first volume; exit
62220Sstevel@tonic-gate 	 */
62230Sstevel@tonic-gate 	if (strcmp(bar_Vhdr->dbuf.volume_num, "1") != 0) {
62240Sstevel@tonic-gate 		(void) fprintf(stderr,
62250Sstevel@tonic-gate 		    gettext("error: This is not volume 1.  "));
62260Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("This is volume %s.  "),
62270Sstevel@tonic-gate 		    bar_Vhdr->dbuf.volume_num);
62280Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Please insert volume 1.\n"));
62290Sstevel@tonic-gate 		exit(1);
62300Sstevel@tonic-gate 	}
62310Sstevel@tonic-gate 
62320Sstevel@tonic-gate 	read_bar_file_hdr();
62330Sstevel@tonic-gate }
62340Sstevel@tonic-gate 
62350Sstevel@tonic-gate /*
62360Sstevel@tonic-gate  * read in the header that describes the current file to be extracted
62370Sstevel@tonic-gate  */
62380Sstevel@tonic-gate static void
62390Sstevel@tonic-gate read_bar_file_hdr(void)
62400Sstevel@tonic-gate {
62410Sstevel@tonic-gate 	union b_block *tmp_hdr;
62420Sstevel@tonic-gate 	char *start_of_name, *name_p;
62430Sstevel@tonic-gate 	char *tmp;
62440Sstevel@tonic-gate 
62450Sstevel@tonic-gate 	if (*Buffr.b_out_p == '\0') {
62460Sstevel@tonic-gate 		*Gen.g_nam_p = '\0';
62470Sstevel@tonic-gate 		exit(0);
62480Sstevel@tonic-gate 	}
62490Sstevel@tonic-gate 
62500Sstevel@tonic-gate 	tmp_hdr = (union b_block *)Buffr.b_out_p;
62510Sstevel@tonic-gate 
62520Sstevel@tonic-gate 	tmp = &tmp_hdr->dbuf.mode[1];
62530Sstevel@tonic-gate 	(void) sscanf(tmp, "%8lo", &Gen.g_mode);
62540Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.uid, "%8lo", &Gen.g_uid);
62550Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.gid, "%8lo", &Gen.g_gid);
62560Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.size, "%12llo",
62570Sstevel@tonic-gate 	    (u_off_t *)&Gen.g_filesz);
62580Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.mtime, "%12lo", &Gen.g_mtime);
62590Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.chksum, "%8lo", &Gen.g_cksum);
62600Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.rdev, "%8lo", &Gen.g_rdev);
62610Sstevel@tonic-gate 
62620Sstevel@tonic-gate #define	to_new_major(x)	(int)((unsigned)((x) & OMAXMAJ) << NBITSMINOR)
62630Sstevel@tonic-gate #define	to_new_minor(x)	(int)((x) & OMAXMIN)
62640Sstevel@tonic-gate 	Gen.g_rdev = to_new_major(Gen.g_rdev) | to_new_minor(Gen.g_rdev);
62650Sstevel@tonic-gate 	bar_linkflag = tmp_hdr->dbuf.linkflag;
62660Sstevel@tonic-gate 	start_of_name = &tmp_hdr->dbuf.start_of_name;
62670Sstevel@tonic-gate 
62680Sstevel@tonic-gate 
62690Sstevel@tonic-gate 	name_p = Gen.g_nam_p;
62700Sstevel@tonic-gate 	while (*name_p++ = *start_of_name++)
62710Sstevel@tonic-gate 		;
62720Sstevel@tonic-gate 	*name_p = '\0';
62730Sstevel@tonic-gate 	if (bar_linkflag == LNKTYPE || bar_linkflag == SYMTYPE)
62740Sstevel@tonic-gate 		(void) strcpy(bar_linkname, start_of_name);
62750Sstevel@tonic-gate 
62760Sstevel@tonic-gate 	Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
62770Sstevel@tonic-gate 	(void) strcpy(nambuf, Gen.g_nam_p);
62780Sstevel@tonic-gate }
62790Sstevel@tonic-gate 
62800Sstevel@tonic-gate /*
62810Sstevel@tonic-gate  * if the bar archive is compressed, set up a pipe and do the de-compression
62820Sstevel@tonic-gate  * as the compressed file is read in.
62830Sstevel@tonic-gate  */
62840Sstevel@tonic-gate static void
62850Sstevel@tonic-gate setup_uncompress(FILE **pipef)
62860Sstevel@tonic-gate {
62870Sstevel@tonic-gate 	char *cmd_buf;
62880Sstevel@tonic-gate 	size_t cmdlen;
62890Sstevel@tonic-gate 
62900Sstevel@tonic-gate 	cmd_buf = e_zalloc(E_EXIT, MAXPATHLEN * 2);
62910Sstevel@tonic-gate 
62920Sstevel@tonic-gate 	if (access(Gen.g_nam_p, W_OK) != 0) {
62930Sstevel@tonic-gate 		cmdlen = snprintf(cmd_buf, MAXPATHLEN * 2,
62940Sstevel@tonic-gate 		    "chmod +w '%s'; uncompress -c > '%s'; "
62950Sstevel@tonic-gate 		    "chmod 0%o '%s'",
62960Sstevel@tonic-gate 		    Gen.g_nam_p, Gen.g_nam_p, (int)G_p->g_mode, Gen.g_nam_p);
62970Sstevel@tonic-gate 	} else {
62980Sstevel@tonic-gate 		cmdlen = snprintf(cmd_buf, MAXPATHLEN * 2,
62990Sstevel@tonic-gate 		    "uncompress -c > '%s'", Gen.g_nam_p);
63000Sstevel@tonic-gate 	}
63010Sstevel@tonic-gate 
63020Sstevel@tonic-gate 	if (cmdlen >= MAXPATHLEN * 2 ||
63030Sstevel@tonic-gate 	    (*pipef = popen(cmd_buf, "w")) == NULL) {
63040Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("error\n"));
63050Sstevel@tonic-gate 		exit(1);
63060Sstevel@tonic-gate 	}
63070Sstevel@tonic-gate 
63080Sstevel@tonic-gate 	if (close(Ofile) != 0)
63090Sstevel@tonic-gate 		msg(EXTN, "close error");
63100Sstevel@tonic-gate 	Ofile = fileno(*pipef);
63110Sstevel@tonic-gate 
63120Sstevel@tonic-gate 	free(cmd_buf);
63130Sstevel@tonic-gate }
63140Sstevel@tonic-gate 
63150Sstevel@tonic-gate /*
63160Sstevel@tonic-gate  * if the bar archive spans multiple volumes, read in the header that
63170Sstevel@tonic-gate  * describes the next volume.
63180Sstevel@tonic-gate  */
63190Sstevel@tonic-gate static void
63200Sstevel@tonic-gate skip_bar_volhdr(void)
63210Sstevel@tonic-gate {
63220Sstevel@tonic-gate 	char *buff;
63230Sstevel@tonic-gate 	union b_block *tmp_hdr;
63240Sstevel@tonic-gate 
63250Sstevel@tonic-gate 	buff = e_zalloc(E_EXIT, (uint_t)Bufsize);
63260Sstevel@tonic-gate 
63270Sstevel@tonic-gate 	if (g_read(Device, Archive, buff, Bufsize) < 0) {
63280Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
63290Sstevel@tonic-gate 		    "error in skip_bar_volhdr\n"));
63300Sstevel@tonic-gate 	} else {
63310Sstevel@tonic-gate 
63320Sstevel@tonic-gate 		tmp_hdr = (union b_block *)buff;
63330Sstevel@tonic-gate 		if (tmp_hdr->dbuf.bar_magic[0] == BAR_VOLUME_MAGIC) {
63340Sstevel@tonic-gate 
63350Sstevel@tonic-gate 			if (bar_Vhdr == NULL) {
63360Sstevel@tonic-gate 				bar_Vhdr = e_zalloc(E_EXIT, TBLOCK);
63370Sstevel@tonic-gate 			}
63380Sstevel@tonic-gate 			(void) memcpy(&(bar_Vhdr->dbuf),
63390Sstevel@tonic-gate 			    &(tmp_hdr->dbuf), TBLOCK);
63400Sstevel@tonic-gate 		} else {
63410Sstevel@tonic-gate 			(void) fprintf(stderr,
63420Sstevel@tonic-gate 			    gettext("cpio error: cannot read bar volume "
63430Sstevel@tonic-gate 			    "header\n"));
63440Sstevel@tonic-gate 			exit(1);
63450Sstevel@tonic-gate 		}
63460Sstevel@tonic-gate 
63470Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.mode, "%8lo",
63480Sstevel@tonic-gate 		    &Gen_bar_vol.g_mode);
63490Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.uid, "%8lo",
63500Sstevel@tonic-gate 		    &Gen_bar_vol.g_uid);
63510Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.gid, "%8lo",
63520Sstevel@tonic-gate 		    &Gen_bar_vol.g_gid);
63530Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.size, "%12llo",
63540Sstevel@tonic-gate 		    (u_off_t *)&Gen_bar_vol.g_filesz);
63550Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.mtime, "%12lo",
63560Sstevel@tonic-gate 		    &Gen_bar_vol.g_mtime);
63570Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.chksum, "%8lo",
63580Sstevel@tonic-gate 		    &Gen_bar_vol.g_cksum);
63590Sstevel@tonic-gate 		if (bar_Vhdr->dbuf.compressed == '1')
63600Sstevel@tonic-gate 			Compressed = 1;
63610Sstevel@tonic-gate 		else
63620Sstevel@tonic-gate 			Compressed = 0;
63630Sstevel@tonic-gate 	}
63640Sstevel@tonic-gate 
63650Sstevel@tonic-gate 	/*
63660Sstevel@tonic-gate 	 * Now put the rest of the bytes read in into the data buffer.
63670Sstevel@tonic-gate 	 */
63680Sstevel@tonic-gate 	(void) memcpy(Buffr.b_in_p, &buff[512], (Bufsize - 512));
63690Sstevel@tonic-gate 	Buffr.b_in_p += (Bufsize - 512);
63700Sstevel@tonic-gate 	Buffr.b_cnt += (long)(Bufsize - 512);
63710Sstevel@tonic-gate 
63720Sstevel@tonic-gate 	free(buff);
63730Sstevel@tonic-gate }
63740Sstevel@tonic-gate 
63750Sstevel@tonic-gate /*
63760Sstevel@tonic-gate  * check the linkflag which indicates the type of the file to be extracted,
63770Sstevel@tonic-gate  * invoke the corresponding routine to extract the file.
63780Sstevel@tonic-gate  */
63790Sstevel@tonic-gate static void
63800Sstevel@tonic-gate bar_file_in(void)
63810Sstevel@tonic-gate {
63820Sstevel@tonic-gate 	/*
63830Sstevel@tonic-gate 	 * the file is a directory
63840Sstevel@tonic-gate 	 */
63850Sstevel@tonic-gate 	if (Adir) {
63860Sstevel@tonic-gate 		if (ckname(1) != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
63870Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
63880Sstevel@tonic-gate 		}
63890Sstevel@tonic-gate 		return;
63900Sstevel@tonic-gate 	}
63910Sstevel@tonic-gate 
63920Sstevel@tonic-gate 	switch (bar_linkflag) {
63930Sstevel@tonic-gate 	case REGTYPE:
63940Sstevel@tonic-gate 		/* regular file */
63950Sstevel@tonic-gate 		if ((ckname(1) == F_SKIP) ||
63960Sstevel@tonic-gate 		    (Ofile = openout(G_p->g_dirfd)) < 0) {
63970Sstevel@tonic-gate 			data_in(P_SKIP);
63980Sstevel@tonic-gate 		} else {
63990Sstevel@tonic-gate 			data_in(P_PROC);
64000Sstevel@tonic-gate 		}
64010Sstevel@tonic-gate 		break;
64020Sstevel@tonic-gate 	case LNKTYPE:
64030Sstevel@tonic-gate 		/* hard link */
64040Sstevel@tonic-gate 		if (ckname(1) == F_SKIP) {
64050Sstevel@tonic-gate 			break;
64060Sstevel@tonic-gate 		}
64070Sstevel@tonic-gate 		(void) creat_lnk(G_p->g_dirfd, bar_linkname, G_p->g_nam_p);
64080Sstevel@tonic-gate 		break;
64090Sstevel@tonic-gate 	case SYMTYPE:
64100Sstevel@tonic-gate 		/* symbolic link */
64110Sstevel@tonic-gate 		if ((ckname(1) == F_SKIP) ||
64120Sstevel@tonic-gate 		    (Ofile = openout(G_p->g_dirfd)) < 0) {
64130Sstevel@tonic-gate 			data_in(P_SKIP);
64140Sstevel@tonic-gate 		} else {
64150Sstevel@tonic-gate 			data_in(P_PROC);
64160Sstevel@tonic-gate 		}
64170Sstevel@tonic-gate 		break;
64180Sstevel@tonic-gate 	case CHRTYPE:
64190Sstevel@tonic-gate 		/* character device or FIFO */
64200Sstevel@tonic-gate 		if (ckname(1) != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
64210Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
64220Sstevel@tonic-gate 		}
64230Sstevel@tonic-gate 		break;
64240Sstevel@tonic-gate 	default:
64250Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("error: unknown file type\n"));
64260Sstevel@tonic-gate 		break;
64270Sstevel@tonic-gate 	}
64280Sstevel@tonic-gate }
64290Sstevel@tonic-gate 
64300Sstevel@tonic-gate 
64310Sstevel@tonic-gate /*
64320Sstevel@tonic-gate  * This originally came from libgenIO/g_init.c
64330Sstevel@tonic-gate  * XXX	And it is very broken.
64340Sstevel@tonic-gate  */
64350Sstevel@tonic-gate 
64360Sstevel@tonic-gate /* #include <sys/statvfs.h> */
64370Sstevel@tonic-gate #include <ftw.h>
64380Sstevel@tonic-gate /* #include <libgenIO.h> */
64390Sstevel@tonic-gate #define	G_TM_TAPE	1	/* Tapemaster controller    */
64400Sstevel@tonic-gate #define	G_XY_DISK	3	/* xy disks		*/
64410Sstevel@tonic-gate #define	G_SD_DISK	7	/* scsi sd disk		*/
64420Sstevel@tonic-gate #define	G_XT_TAPE	8	/* xt tapes		*/
64430Sstevel@tonic-gate #define	G_SF_FLOPPY	9	/* sf floppy		*/
64440Sstevel@tonic-gate #define	G_XD_DISK	10	/* xd disks		*/
64450Sstevel@tonic-gate #define	G_ST_TAPE	11	/* scsi tape		*/
64460Sstevel@tonic-gate #define	G_NS		12	/* noswap pseudo-dev	*/
64470Sstevel@tonic-gate #define	G_RAM		13	/* ram pseudo-dev	*/
64480Sstevel@tonic-gate #define	G_FT		14	/* tftp			*/
64490Sstevel@tonic-gate #define	G_HD		15	/* 386 network disk	*/
64500Sstevel@tonic-gate #define	G_FD		16	/* 386 AT disk		*/
64510Sstevel@tonic-gate #define	G_FILE		28	/* file, not a device	*/
64520Sstevel@tonic-gate #define	G_NO_DEV	29	/* device does not require special treatment */
64530Sstevel@tonic-gate #define	G_DEV_MAX	30	/* last valid device type */
64540Sstevel@tonic-gate 
64550Sstevel@tonic-gate /*
64560Sstevel@tonic-gate  * g_init: Determine the device being accessed, set the buffer size,
64570Sstevel@tonic-gate  * and perform any device specific initialization. Since at this point
64580Sstevel@tonic-gate  * Sun has no system call to read the configuration, the major numbers
64590Sstevel@tonic-gate  * are assumed to be static and types are figured out as such. However,
64600Sstevel@tonic-gate  * as a rough estimate, the buffer size for all types is set to 512
64610Sstevel@tonic-gate  * as a default.
64620Sstevel@tonic-gate  */
64630Sstevel@tonic-gate 
64640Sstevel@tonic-gate static int
64650Sstevel@tonic-gate g_init(int *devtype, int *fdes)
64660Sstevel@tonic-gate {
64670Sstevel@tonic-gate 	int bufsize;
64680Sstevel@tonic-gate 	struct stat st_buf;
64690Sstevel@tonic-gate 	struct statvfs stfs_buf;
64700Sstevel@tonic-gate 
64710Sstevel@tonic-gate 	*devtype = G_NO_DEV;
64720Sstevel@tonic-gate 	bufsize = -1;
64730Sstevel@tonic-gate 	if (fstat(*fdes, &st_buf) == -1)
64740Sstevel@tonic-gate 		return (-1);
6475871Scasper 	if (!S_ISCHR(st_buf.st_mode) && !S_ISBLK(st_buf.st_mode)) {
6476871Scasper 		if (S_ISFIFO(st_buf.st_mode)) {
64770Sstevel@tonic-gate 			bufsize = 512;
64780Sstevel@tonic-gate 		} else {
64790Sstevel@tonic-gate 			/* find block size for this file system */
64800Sstevel@tonic-gate 			*devtype = G_FILE;
64810Sstevel@tonic-gate 			if (fstatvfs(*fdes, &stfs_buf) < 0) {
64820Sstevel@tonic-gate 					bufsize = -1;
64830Sstevel@tonic-gate 					errno = ENODEV;
64840Sstevel@tonic-gate 			} else
64850Sstevel@tonic-gate 				bufsize = stfs_buf.f_bsize;
64860Sstevel@tonic-gate 		}
64870Sstevel@tonic-gate 
64880Sstevel@tonic-gate 		return (bufsize);
64890Sstevel@tonic-gate 
64900Sstevel@tonic-gate 	/*
64910Sstevel@tonic-gate 	 * We'll have to add a remote attribute to stat but this
64920Sstevel@tonic-gate 	 * should work for now.
64930Sstevel@tonic-gate 	 */
64940Sstevel@tonic-gate 	} else if (st_buf.st_dev & 0x8000)	/* if remote  rdev */
64950Sstevel@tonic-gate 		return (512);
64960Sstevel@tonic-gate 
64970Sstevel@tonic-gate 	bufsize = 512;
64980Sstevel@tonic-gate 
64990Sstevel@tonic-gate 	if (Hdr_type == BAR) {
65000Sstevel@tonic-gate 		if (is_tape(*fdes)) {
65010Sstevel@tonic-gate 			bufsize = BAR_TAPE_SIZE;
65020Sstevel@tonic-gate 			(void) fprintf(stderr, "Archiving to tape");
65030Sstevel@tonic-gate 			(void) fprintf(stderr, " blocking factor 126\n");
65040Sstevel@tonic-gate 		} else if (is_floppy(*fdes)) {
65050Sstevel@tonic-gate 			bufsize = BAR_FLOPPY_SIZE;
65060Sstevel@tonic-gate 			(void) fprintf(stderr, "Archiving to floppy");
65070Sstevel@tonic-gate 			(void) fprintf(stderr, " blocking factor 18\n");
65080Sstevel@tonic-gate 		}
65090Sstevel@tonic-gate 	}
65100Sstevel@tonic-gate 
65110Sstevel@tonic-gate 	return (bufsize);
65120Sstevel@tonic-gate }
65130Sstevel@tonic-gate 
65140Sstevel@tonic-gate /*
65150Sstevel@tonic-gate  * This originally came from libgenIO/g_read.c
65160Sstevel@tonic-gate  */
65170Sstevel@tonic-gate 
65180Sstevel@tonic-gate /*
65190Sstevel@tonic-gate  * g_read: Read nbytes of data from fdes (of type devtype) and place
65200Sstevel@tonic-gate  * data in location pointed to by buf.  In case of end of medium,
65210Sstevel@tonic-gate  * translate (where necessary) device specific EOM indications into
65220Sstevel@tonic-gate  * the generic EOM indication of rv = -1, errno = ENOSPC.
65230Sstevel@tonic-gate  */
65240Sstevel@tonic-gate 
65250Sstevel@tonic-gate static int
65260Sstevel@tonic-gate g_read(int devtype, int fdes, char *buf, unsigned nbytes)
65270Sstevel@tonic-gate {
65280Sstevel@tonic-gate 	int rv;
65290Sstevel@tonic-gate 
65300Sstevel@tonic-gate 	if (devtype < 0 || devtype >= G_DEV_MAX) {
65310Sstevel@tonic-gate 		errno = ENODEV;
65320Sstevel@tonic-gate 		return (-1);
65330Sstevel@tonic-gate 	}
65340Sstevel@tonic-gate 
65350Sstevel@tonic-gate 	rv = read(fdes, buf, nbytes);
65360Sstevel@tonic-gate 
65370Sstevel@tonic-gate 	/* st devices return 0 when no space left */
65380Sstevel@tonic-gate 	if ((rv == 0 && errno == 0 && Hdr_type != BAR) ||
65390Sstevel@tonic-gate 	    (rv == -1 && errno == EIO)) {
65400Sstevel@tonic-gate 		errno = 0;
65410Sstevel@tonic-gate 		rv = 0;
65420Sstevel@tonic-gate 	}
65430Sstevel@tonic-gate 
65440Sstevel@tonic-gate 	return (rv);
65450Sstevel@tonic-gate }
65460Sstevel@tonic-gate 
65470Sstevel@tonic-gate /*
65480Sstevel@tonic-gate  * This originally came from libgenIO/g_write.c
65490Sstevel@tonic-gate  */
65500Sstevel@tonic-gate 
65510Sstevel@tonic-gate /*
65520Sstevel@tonic-gate  * g_write: Write nbytes of data to fdes (of type devtype) from
65530Sstevel@tonic-gate  * the location pointed to by buf.  In case of end of medium,
65540Sstevel@tonic-gate  * translate (where necessary) device specific EOM indications into
65550Sstevel@tonic-gate  * the generic EOM indication of rv = -1, errno = ENOSPC.
65560Sstevel@tonic-gate  */
65570Sstevel@tonic-gate 
65580Sstevel@tonic-gate static int
65590Sstevel@tonic-gate g_write(int devtype, int fdes, char *buf, unsigned nbytes)
65600Sstevel@tonic-gate {
65610Sstevel@tonic-gate 	int rv;
65620Sstevel@tonic-gate 
65630Sstevel@tonic-gate 	if (devtype < 0 || devtype >= G_DEV_MAX) {
65640Sstevel@tonic-gate 		errno = ENODEV;
65650Sstevel@tonic-gate 		return (-1);
65660Sstevel@tonic-gate 	}
65670Sstevel@tonic-gate 
65680Sstevel@tonic-gate 	rv = write(fdes, buf, nbytes);
65690Sstevel@tonic-gate 
65700Sstevel@tonic-gate 	/* st devices return 0 when no more space left */
65710Sstevel@tonic-gate 	if ((rv == 0 && errno == 0) || (rv == -1 && errno == EIO)) {
65720Sstevel@tonic-gate 		errno = ENOSPC;
65730Sstevel@tonic-gate 		rv = -1;
65740Sstevel@tonic-gate 	}
65750Sstevel@tonic-gate 
65760Sstevel@tonic-gate 	return (rv);
65770Sstevel@tonic-gate }
65780Sstevel@tonic-gate 
65790Sstevel@tonic-gate /*
65800Sstevel@tonic-gate  * Test for tape
65810Sstevel@tonic-gate  */
65820Sstevel@tonic-gate 
65830Sstevel@tonic-gate static int
65840Sstevel@tonic-gate is_tape(int fd)
65850Sstevel@tonic-gate {
65860Sstevel@tonic-gate 	struct mtget stuff;
65870Sstevel@tonic-gate 
65880Sstevel@tonic-gate 	/*
65890Sstevel@tonic-gate 	 * try to do a generic tape ioctl, just to see if
65900Sstevel@tonic-gate 	 * the thing is in fact a tape drive(er).
65910Sstevel@tonic-gate 	 */
65920Sstevel@tonic-gate 	if (ioctl(fd, MTIOCGET, &stuff) != -1) {
65930Sstevel@tonic-gate 		/* the ioctl succeeded, must have been a tape */
65940Sstevel@tonic-gate 		return (1);
65950Sstevel@tonic-gate 	}
65960Sstevel@tonic-gate 	return (0);
65970Sstevel@tonic-gate }
65980Sstevel@tonic-gate 
65990Sstevel@tonic-gate /*
66000Sstevel@tonic-gate  * Test for floppy
66010Sstevel@tonic-gate  */
66020Sstevel@tonic-gate 
66030Sstevel@tonic-gate static int
66040Sstevel@tonic-gate is_floppy(int fd)
66050Sstevel@tonic-gate {
66060Sstevel@tonic-gate 	struct fd_char stuff;
66070Sstevel@tonic-gate 
66080Sstevel@tonic-gate 	/*
66090Sstevel@tonic-gate 	 * try to get the floppy drive characteristics, just to see if
66100Sstevel@tonic-gate 	 * the thing is in fact a floppy drive(er).
66110Sstevel@tonic-gate 	 */
66120Sstevel@tonic-gate 	if (ioctl(fd, FDIOGCHAR, &stuff) != -1) {
66130Sstevel@tonic-gate 		/* the ioctl succeeded, must have been a floppy */
66140Sstevel@tonic-gate 		return (1);
66150Sstevel@tonic-gate 	}
66160Sstevel@tonic-gate 
66170Sstevel@tonic-gate 	return (0);
66180Sstevel@tonic-gate }
66190Sstevel@tonic-gate 
66200Sstevel@tonic-gate /*
66210Sstevel@tonic-gate  * New functions for ACLs and other security attributes
66220Sstevel@tonic-gate  */
66230Sstevel@tonic-gate 
66240Sstevel@tonic-gate /*
66250Sstevel@tonic-gate  * The function appends the new security attribute info to the end of
66260Sstevel@tonic-gate  * existing secinfo.
66270Sstevel@tonic-gate  */
66280Sstevel@tonic-gate static int
66290Sstevel@tonic-gate append_secattr(
6630789Sahrens 	char		**secinfo,	/* existing security info */
6631789Sahrens 	int		*secinfo_len,	/* length of existing security info */
6632789Sahrens 	acl_t		*aclp) 	/* new attribute data pointer */
66330Sstevel@tonic-gate {
66340Sstevel@tonic-gate 	char	*new_secinfo;
66350Sstevel@tonic-gate 	char	*attrtext;
66360Sstevel@tonic-gate 	size_t	newattrsize;
66370Sstevel@tonic-gate 	int	oldsize;
66380Sstevel@tonic-gate 
66390Sstevel@tonic-gate 	/* no need to add */
6640789Sahrens 	if (aclp == NULL) {
66410Sstevel@tonic-gate 		return (0);
66420Sstevel@tonic-gate 	}
66430Sstevel@tonic-gate 
6644789Sahrens 	switch (acl_type(aclp)) {
6645789Sahrens 	case ACLENT_T:
6646789Sahrens 	case ACE_T:
66470Sstevel@tonic-gate 		/* LINTED alignment */
6648789Sahrens 		attrtext = acl_totext(aclp);
66490Sstevel@tonic-gate 		if (attrtext == NULL) {
66500Sstevel@tonic-gate 			(void) fprintf(stderr, "acltotext failed\n");
66510Sstevel@tonic-gate 			return (-1);
66520Sstevel@tonic-gate 		}
66530Sstevel@tonic-gate 		/* header: type + size = 8 */
66540Sstevel@tonic-gate 		newattrsize = 8 + strlen(attrtext) + 1;
66550Sstevel@tonic-gate 		attr = e_zalloc(E_NORMAL, newattrsize);
66560Sstevel@tonic-gate 		if (attr == NULL) {
66570Sstevel@tonic-gate 			(void) fprintf(stderr, "can't allocate memory\n");
66580Sstevel@tonic-gate 			return (-1);
66590Sstevel@tonic-gate 		}
6660789Sahrens 		attr->attr_type = (acl_type(aclp) == ACLENT_T) ?
6661789Sahrens 		    UFSD_ACL : ACE_ACL;
66620Sstevel@tonic-gate 		/* acl entry count */
6663789Sahrens 		(void) sprintf(attr->attr_len, "%06o", acl_cnt(aclp));
66640Sstevel@tonic-gate 		(void) strcpy((char *)&attr->attr_info[0], attrtext);
66650Sstevel@tonic-gate 		free(attrtext);
66660Sstevel@tonic-gate 		break;
66670Sstevel@tonic-gate 
66680Sstevel@tonic-gate 		/* SunFed's case goes here */
66690Sstevel@tonic-gate 
66700Sstevel@tonic-gate 	default:
66710Sstevel@tonic-gate 		(void) fprintf(stderr, "unrecognized attribute type\n");
66720Sstevel@tonic-gate 		return (-1);
66730Sstevel@tonic-gate 	}
66740Sstevel@tonic-gate 
66750Sstevel@tonic-gate 	/* old security info + new attr header(8) + new attr */
66760Sstevel@tonic-gate 	oldsize = *secinfo_len;
66770Sstevel@tonic-gate 	*secinfo_len += newattrsize;
66780Sstevel@tonic-gate 	new_secinfo = e_zalloc(E_NORMAL, (uint_t)*secinfo_len);
66790Sstevel@tonic-gate 	if (new_secinfo == NULL) {
66800Sstevel@tonic-gate 		(void) fprintf(stderr, "can't allocate memory\n");
66810Sstevel@tonic-gate 		*secinfo_len -= newattrsize;
66820Sstevel@tonic-gate 		return (-1);
66830Sstevel@tonic-gate 	}
66840Sstevel@tonic-gate 
66850Sstevel@tonic-gate 	(void) memcpy(new_secinfo, *secinfo, oldsize);
66860Sstevel@tonic-gate 	(void) memcpy(new_secinfo + oldsize, attr, newattrsize);
66870Sstevel@tonic-gate 
66880Sstevel@tonic-gate 	free(*secinfo);
66890Sstevel@tonic-gate 	*secinfo = new_secinfo;
66900Sstevel@tonic-gate 	return (0);
66910Sstevel@tonic-gate }
66920Sstevel@tonic-gate 
66930Sstevel@tonic-gate static void
66940Sstevel@tonic-gate write_ancillary(char *secinfo, int len)
66950Sstevel@tonic-gate {
66960Sstevel@tonic-gate 	long    pad;
66970Sstevel@tonic-gate 	long    cnt;
66980Sstevel@tonic-gate 
66990Sstevel@tonic-gate 	/* Just tranditional permissions or no security attribute info */
67000Sstevel@tonic-gate 	if (len == 0) {
67010Sstevel@tonic-gate 		return;
67020Sstevel@tonic-gate 	}
67030Sstevel@tonic-gate 
67040Sstevel@tonic-gate 	/* write out security info */
67050Sstevel@tonic-gate 	while (len > 0) {
67060Sstevel@tonic-gate 		cnt = (unsigned)(len > CPIOBSZ) ? CPIOBSZ : len;
67070Sstevel@tonic-gate 		FLUSH(cnt);
67080Sstevel@tonic-gate 		errno = 0;
67090Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, secinfo, (unsigned)cnt);
67100Sstevel@tonic-gate 		Buffr.b_in_p += cnt;
67110Sstevel@tonic-gate 		Buffr.b_cnt += (long)cnt;
67120Sstevel@tonic-gate 		len -= (long)cnt;
67130Sstevel@tonic-gate 	}
67140Sstevel@tonic-gate 	pad = (Pad_val + 1 - (cnt & Pad_val)) & Pad_val;
67150Sstevel@tonic-gate 	if (pad != 0) {
67160Sstevel@tonic-gate 		FLUSH(pad);
67170Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, Empty, pad);
67180Sstevel@tonic-gate 		Buffr.b_in_p += pad;
67190Sstevel@tonic-gate 		Buffr.b_cnt += pad;
67200Sstevel@tonic-gate 	}
67210Sstevel@tonic-gate }
67220Sstevel@tonic-gate 
67230Sstevel@tonic-gate static int
67240Sstevel@tonic-gate remove_dir(char *path)
67250Sstevel@tonic-gate {
67260Sstevel@tonic-gate 	DIR		*name;
67270Sstevel@tonic-gate 	struct dirent	*direct;
67280Sstevel@tonic-gate 	struct stat	sbuf;
67290Sstevel@tonic-gate 	char		*path_copy;
67300Sstevel@tonic-gate 
67310Sstevel@tonic-gate #define	MSG1	"remove_dir() failed to stat(\"%s\") "
67320Sstevel@tonic-gate #define	MSG2	"remove_dir() failed to remove_dir(\"%s\") "
67330Sstevel@tonic-gate #define	MSG3	"remove_dir() failed to unlink(\"%s\") "
67340Sstevel@tonic-gate 
67350Sstevel@tonic-gate 	/*
67360Sstevel@tonic-gate 	 * Open the directory for reading.
67370Sstevel@tonic-gate 	 */
67380Sstevel@tonic-gate 	if ((name = opendir(path)) == NULL) {
67390Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to opendir(\"%s\") ", path);
67400Sstevel@tonic-gate 		return (-1);
67410Sstevel@tonic-gate 	}
67420Sstevel@tonic-gate 
67430Sstevel@tonic-gate 	if (chdir(path) == -1) {
67440Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to chdir(\"%s\") ", path);
67450Sstevel@tonic-gate 		return (-1);
67460Sstevel@tonic-gate 	}
67470Sstevel@tonic-gate 
67480Sstevel@tonic-gate 	/*
67490Sstevel@tonic-gate 	 * Read every directory entry.
67500Sstevel@tonic-gate 	 */
67510Sstevel@tonic-gate 	while ((direct = readdir(name)) != NULL) {
67520Sstevel@tonic-gate 		/*
67530Sstevel@tonic-gate 		 * Ignore "." and ".." entries.
67540Sstevel@tonic-gate 		 */
67550Sstevel@tonic-gate 		if (strcmp(direct->d_name, ".") == 0 ||
67560Sstevel@tonic-gate 		    strcmp(direct->d_name, "..") == 0)
67570Sstevel@tonic-gate 			continue;
67580Sstevel@tonic-gate 
67590Sstevel@tonic-gate 			if (lstat(direct->d_name, &sbuf) == -1) {
67600Sstevel@tonic-gate 				msg(ERRN, MSG1, direct->d_name);
67610Sstevel@tonic-gate 				(void) closedir(name);
67620Sstevel@tonic-gate 			return (-1);
67630Sstevel@tonic-gate 		}
67640Sstevel@tonic-gate 
67650Sstevel@tonic-gate 		if (S_ISDIR(sbuf.st_mode)) {
67660Sstevel@tonic-gate 			if (remove_dir(direct->d_name) == -1) {
67670Sstevel@tonic-gate 				msg(ERRN, MSG2, direct->d_name);
67680Sstevel@tonic-gate 				(void) closedir(name);
67690Sstevel@tonic-gate 				return (-1);
67700Sstevel@tonic-gate 			}
67710Sstevel@tonic-gate 		} else {
67720Sstevel@tonic-gate 			if (unlink(direct->d_name) == -1) {
67730Sstevel@tonic-gate 				msg(ERRN, MSG3, direct->d_name);
67740Sstevel@tonic-gate 				(void) closedir(name);
67750Sstevel@tonic-gate 				return (-1);
67760Sstevel@tonic-gate 			}
67770Sstevel@tonic-gate 		}
67780Sstevel@tonic-gate 
67790Sstevel@tonic-gate 	}
67800Sstevel@tonic-gate 
67810Sstevel@tonic-gate 	/*
67820Sstevel@tonic-gate 	 * Close the directory we just finished reading.
67830Sstevel@tonic-gate 	 */
67840Sstevel@tonic-gate 	(void) closedir(name);
67850Sstevel@tonic-gate 
67860Sstevel@tonic-gate 	/*
67870Sstevel@tonic-gate 	 * Change directory to the parent directory...
67880Sstevel@tonic-gate 	 */
67890Sstevel@tonic-gate 	if (chdir("..") == -1) {
67900Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to chdir(\"..\") ");
67910Sstevel@tonic-gate 		return (-1);
67920Sstevel@tonic-gate 	}
67930Sstevel@tonic-gate 
67940Sstevel@tonic-gate 	/*
67950Sstevel@tonic-gate 	 * ...and finally remove the directory; note we have to
67960Sstevel@tonic-gate 	 * make a copy since basename is free to modify its input.
67970Sstevel@tonic-gate 	 */
67980Sstevel@tonic-gate 	path_copy = e_strdup(E_NORMAL, path);
67990Sstevel@tonic-gate 	if (path_copy == NULL) {
68000Sstevel@tonic-gate 		msg(ERRN, "cannot strdup() the directory pathname ");
68010Sstevel@tonic-gate 		return (-1);
68020Sstevel@tonic-gate 	}
68030Sstevel@tonic-gate 
68040Sstevel@tonic-gate 	if (rmdir(basename(path_copy)) == -1) {
68050Sstevel@tonic-gate 		free(path_copy);
68060Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to rmdir(\"%s\") ", path);
68070Sstevel@tonic-gate 		return (-1);
68080Sstevel@tonic-gate 	}
68090Sstevel@tonic-gate 
68100Sstevel@tonic-gate 	free(path_copy);
68110Sstevel@tonic-gate 	return (0);
68120Sstevel@tonic-gate 
68130Sstevel@tonic-gate }
68140Sstevel@tonic-gate 
68150Sstevel@tonic-gate static int
68160Sstevel@tonic-gate save_cwd(void)
68170Sstevel@tonic-gate {
68180Sstevel@tonic-gate 	return (open(".", O_RDONLY));
68190Sstevel@tonic-gate }
68200Sstevel@tonic-gate 
68210Sstevel@tonic-gate static void
68220Sstevel@tonic-gate rest_cwd(int cwd)
68230Sstevel@tonic-gate {
68240Sstevel@tonic-gate 	(void) fchdir(cwd);
68250Sstevel@tonic-gate 	(void) close(cwd);
68260Sstevel@tonic-gate }
68270Sstevel@tonic-gate 
68280Sstevel@tonic-gate #if defined(O_XATTR)
68290Sstevel@tonic-gate static void
68300Sstevel@tonic-gate xattrs_out(int (*func)())
68310Sstevel@tonic-gate {
68320Sstevel@tonic-gate 	int dirpfd;
68330Sstevel@tonic-gate 	int filefd;
68340Sstevel@tonic-gate 	DIR *dirp;
68350Sstevel@tonic-gate 	struct dirent *dp;
68360Sstevel@tonic-gate 	int slen;
68370Sstevel@tonic-gate 	char *namep, *savenamep;
68380Sstevel@tonic-gate 
68390Sstevel@tonic-gate 	if (pathconf(G_p->g_nam_p, _PC_XATTR_EXISTS) != 1) {
68400Sstevel@tonic-gate 		return;
68410Sstevel@tonic-gate 	}
68420Sstevel@tonic-gate 
68430Sstevel@tonic-gate 	/*
68440Sstevel@tonic-gate 	 * If aclp still exists then free it since it is was set when base
68450Sstevel@tonic-gate 	 * file was extracted.
68460Sstevel@tonic-gate 	 */
6847789Sahrens 	if (aclp != NULL) {
6848789Sahrens 		acl_free(aclp);
68490Sstevel@tonic-gate 		aclp = NULL;
6850789Sahrens 		acl_is_set = 0;
68510Sstevel@tonic-gate 	}
68520Sstevel@tonic-gate 
68530Sstevel@tonic-gate 	Gen.g_dirfd = attropen(G_p->g_nam_p, ".", O_RDONLY);
68540Sstevel@tonic-gate 	if (Gen.g_dirfd == -1) {
68550Sstevel@tonic-gate 		msg(ERRN, "Cannot open attribute directory of file \"%s\"",
68560Sstevel@tonic-gate 		    G_p->g_nam_p);
68570Sstevel@tonic-gate 		return;
68580Sstevel@tonic-gate 
68590Sstevel@tonic-gate 	}
68600Sstevel@tonic-gate 	savenamep = G_p->g_nam_p;
68610Sstevel@tonic-gate 
68620Sstevel@tonic-gate 	if ((dirpfd = dup(Gen.g_dirfd)) == -1)  {
68630Sstevel@tonic-gate 		msg(ERRN, "Cannot dup(2) attribute directory descriptor");
68640Sstevel@tonic-gate 		return;
68650Sstevel@tonic-gate 	}
68660Sstevel@tonic-gate 
68670Sstevel@tonic-gate 	if ((dirp = fdopendir(dirpfd)) == (DIR *)NULL) {
68680Sstevel@tonic-gate 		msg(ERRN, "Cannot fdopendir(2) directory file descriptor");
68690Sstevel@tonic-gate 		return;
68700Sstevel@tonic-gate 	}
68710Sstevel@tonic-gate 
68720Sstevel@tonic-gate 	while ((dp = readdir(dirp)) != (struct dirent *)NULL) {
68730Sstevel@tonic-gate 		if (strcmp(dp->d_name, "..") == 0) {
68740Sstevel@tonic-gate 			continue;
68750Sstevel@tonic-gate 		}
68760Sstevel@tonic-gate 
68770Sstevel@tonic-gate 		if (strcmp(dp->d_name, ".") == 0) {
68780Sstevel@tonic-gate 			Hiddendir = 1;
68790Sstevel@tonic-gate 		} else {
68800Sstevel@tonic-gate 			Hiddendir = 0;
68810Sstevel@tonic-gate 		}
68820Sstevel@tonic-gate 
68830Sstevel@tonic-gate 		Gen.g_attrnam_p = dp->d_name;
68840Sstevel@tonic-gate 
68850Sstevel@tonic-gate 		if (STAT(Gen.g_dirfd, Gen.g_nam_p, &SrcSt) == -1) {
68860Sstevel@tonic-gate 			msg(ERRN,
68870Sstevel@tonic-gate 			    "Could not fstatat(2) attribute \"%s\" of"
68880Sstevel@tonic-gate 			    " file \"%s\"", dp->d_name, savenamep);
68890Sstevel@tonic-gate 			continue;
68900Sstevel@tonic-gate 		}
68910Sstevel@tonic-gate 
68920Sstevel@tonic-gate 		if (Use_old_stat) {
68930Sstevel@tonic-gate 			OldSt = convert_to_old_stat(&SrcSt,
68940Sstevel@tonic-gate 			    Gen.g_nam_p, Gen.g_attrnam_p);
68950Sstevel@tonic-gate 
68960Sstevel@tonic-gate 			if (OldSt == NULL) {
68970Sstevel@tonic-gate 				msg(ERRN,
68980Sstevel@tonic-gate 				    "Could not convert to old stat format");
68990Sstevel@tonic-gate 				continue;
69000Sstevel@tonic-gate 			}
69010Sstevel@tonic-gate 		}
69020Sstevel@tonic-gate 
69030Sstevel@tonic-gate 		Gen.g_attrfnam_p = savenamep;
69040Sstevel@tonic-gate 
69050Sstevel@tonic-gate 		/*
69060Sstevel@tonic-gate 		 * Set up dummy header name
69070Sstevel@tonic-gate 		 *
69080Sstevel@tonic-gate 		 * One piece is written with .hdr, which
69090Sstevel@tonic-gate 		 * contains the actual xattr hdr or pathing information
69100Sstevel@tonic-gate 		 * then the name is updated to drop the .hdr off
69110Sstevel@tonic-gate 		 * and the actual file itself is archived.
69120Sstevel@tonic-gate 		 */
69130Sstevel@tonic-gate 		slen = strlen(Gen.g_attrnam_p) + strlen(DEVNULL) +
69140Sstevel@tonic-gate 		    strlen(XATTRHDR) + 1;
69150Sstevel@tonic-gate 		if ((namep = e_zalloc(E_NORMAL, slen)) == (char *)NULL) {
69160Sstevel@tonic-gate 			msg(ERRN, "Could not calloc memory for attribute name");
69170Sstevel@tonic-gate 			continue;
69180Sstevel@tonic-gate 		}
69190Sstevel@tonic-gate 		(void) sprintf(namep, "%s/%s%s",
69200Sstevel@tonic-gate 		    DEVNULL, Gen.g_attrnam_p, XATTRHDR);
69210Sstevel@tonic-gate 		Gen.g_nam_p = namep;
69220Sstevel@tonic-gate 
69230Sstevel@tonic-gate 		/*
69240Sstevel@tonic-gate 		 * Get attribute's ACL info: don't bother allocating space
69250Sstevel@tonic-gate 		 * if there are only standard permissions, i.e. ACL count < 4
69260Sstevel@tonic-gate 		 */
69270Sstevel@tonic-gate 		if (Pflag) {
69280Sstevel@tonic-gate 			filefd = openat(Gen.g_dirfd, dp->d_name, O_RDONLY);
69290Sstevel@tonic-gate 			if (filefd == -1) {
69300Sstevel@tonic-gate 				msg(ERRN,
69310Sstevel@tonic-gate 				    "Could not open attribute \"%s\" of"
69320Sstevel@tonic-gate 				    " file \"%s\"", dp->d_name, savenamep);
69330Sstevel@tonic-gate 				free(namep);
69340Sstevel@tonic-gate 				continue;
69350Sstevel@tonic-gate 			}
6936789Sahrens 			if (facl_get(filefd, ACL_NO_TRIVIAL, &aclp) != 0) {
69370Sstevel@tonic-gate 				msg(ERRN,
69380Sstevel@tonic-gate 				    "Error with acl() on %s",
69390Sstevel@tonic-gate 				    Gen.g_nam_p);
69400Sstevel@tonic-gate 			}
69410Sstevel@tonic-gate 			(void) close(filefd);
69420Sstevel@tonic-gate 		}
69430Sstevel@tonic-gate 		(void) creat_hdr();
69440Sstevel@tonic-gate 		(void) (*func)();
69450Sstevel@tonic-gate 		if (Gen.g_passdirfd != -1) {
69460Sstevel@tonic-gate 			(void) close(Gen.g_passdirfd);
69470Sstevel@tonic-gate 			Gen.g_passdirfd = -1;
69480Sstevel@tonic-gate 		}
69490Sstevel@tonic-gate 		Gen.g_attrnam_p = (char *)NULL;
69500Sstevel@tonic-gate 		Gen.g_attrfnam_p = (char *)NULL;
69510Sstevel@tonic-gate 		Gen.g_linktoattrfnam_p = (char *)NULL;
69520Sstevel@tonic-gate 		Gen.g_linktoattrnam_p = (char *)NULL;
6953789Sahrens 		if (aclp != NULL) {
6954789Sahrens 			acl_free(aclp);
69550Sstevel@tonic-gate 			aclp = NULL;
6956789Sahrens 			acl_is_set = 0;
69570Sstevel@tonic-gate 		}
69580Sstevel@tonic-gate 		free(namep);
69590Sstevel@tonic-gate 	}
69600Sstevel@tonic-gate 
69610Sstevel@tonic-gate 	(void) closedir(dirp);
69620Sstevel@tonic-gate 	(void) close(Gen.g_dirfd);
69630Sstevel@tonic-gate 	Gen.g_dirfd = -1;
69640Sstevel@tonic-gate }
69650Sstevel@tonic-gate #else
69660Sstevel@tonic-gate static void
69670Sstevel@tonic-gate xattrs_out(int (*func)())
69680Sstevel@tonic-gate {
69690Sstevel@tonic-gate }
69700Sstevel@tonic-gate #endif
69710Sstevel@tonic-gate 
69720Sstevel@tonic-gate /*
69730Sstevel@tonic-gate  * Return the parent directory of a given path.
69740Sstevel@tonic-gate  *
69750Sstevel@tonic-gate  * Examples:
69760Sstevel@tonic-gate  * /usr/tmp return /usr
69770Sstevel@tonic-gate  * /usr/tmp/file return /usr/tmp
69780Sstevel@tonic-gate  * /  returns .
69790Sstevel@tonic-gate  * /usr returns /
69800Sstevel@tonic-gate  * file returns .
69810Sstevel@tonic-gate  *
69820Sstevel@tonic-gate  * dir is assumed to be at least as big as path.
69830Sstevel@tonic-gate  */
69840Sstevel@tonic-gate static void
69850Sstevel@tonic-gate get_parent(char *path, char *dir)
69860Sstevel@tonic-gate {
69870Sstevel@tonic-gate 	char *s;
69880Sstevel@tonic-gate 	char tmpdir[PATH_MAX + 1];
69890Sstevel@tonic-gate 
69900Sstevel@tonic-gate 	if (strlen(path) > PATH_MAX) {
69910Sstevel@tonic-gate 		msg(EXT, "pathname is too long");
69920Sstevel@tonic-gate 	}
69930Sstevel@tonic-gate 	(void) strcpy(tmpdir, path);
69940Sstevel@tonic-gate 	chop_endslashes(tmpdir);
69950Sstevel@tonic-gate 
69960Sstevel@tonic-gate 	if ((s = strrchr(tmpdir, '/')) == NULL) {
69970Sstevel@tonic-gate 		(void) strcpy(dir, ".");
69980Sstevel@tonic-gate 	} else {
69990Sstevel@tonic-gate 		s = skipslashes(s, tmpdir);
70000Sstevel@tonic-gate 		*s = '\0';
70010Sstevel@tonic-gate 		if (s == tmpdir)
70020Sstevel@tonic-gate 			(void) strcpy(dir, "/");
70030Sstevel@tonic-gate 		else
70040Sstevel@tonic-gate 			(void) strcpy(dir, tmpdir);
70050Sstevel@tonic-gate 	}
70060Sstevel@tonic-gate }
70070Sstevel@tonic-gate 
70080Sstevel@tonic-gate #if defined(O_XATTR)
70090Sstevel@tonic-gate #define	ROUNDTOTBLOCK(a)		((a + (TBLOCK -1)) & ~(TBLOCK -1))
70100Sstevel@tonic-gate 
70110Sstevel@tonic-gate static void
70120Sstevel@tonic-gate prepare_xattr_hdr(
70130Sstevel@tonic-gate 	char		**attrbuf,
70140Sstevel@tonic-gate 	char		*filename,
70150Sstevel@tonic-gate 	char		*attrname,
70160Sstevel@tonic-gate 	char		typeflag,
70170Sstevel@tonic-gate 	struct Lnk	*linkinfo,
70180Sstevel@tonic-gate 	int		*rlen)
70190Sstevel@tonic-gate {
70200Sstevel@tonic-gate 	char			*bufhead;	/* ptr to full buffer */
70210Sstevel@tonic-gate 	struct xattr_hdr 	*hptr;		/* ptr to header in bufhead */
70220Sstevel@tonic-gate 	struct xattr_buf	*tptr;		/* ptr to pathing pieces */
70230Sstevel@tonic-gate 	int			totalen;	/* total buffer length */
70240Sstevel@tonic-gate 	int			len;		/* length returned to user */
70250Sstevel@tonic-gate 	int			stringlen;	/* length of filename + attr */
70260Sstevel@tonic-gate 	int			linkstringlen;	/* ditto in link section */
70270Sstevel@tonic-gate 	int			complen;	/* length of pathing section */
70280Sstevel@tonic-gate 	int			linklen;	/* length of link section */
70290Sstevel@tonic-gate 
70300Sstevel@tonic-gate 
70310Sstevel@tonic-gate 	/*
70320Sstevel@tonic-gate 	 * Release previous buffer if any.
70330Sstevel@tonic-gate 	 */
70340Sstevel@tonic-gate 
70350Sstevel@tonic-gate 	if (*attrbuf != (char *)NULL) {
70360Sstevel@tonic-gate 		free(*attrbuf);
70370Sstevel@tonic-gate 		*attrbuf = NULL;
70380Sstevel@tonic-gate 	}
70390Sstevel@tonic-gate 
70400Sstevel@tonic-gate 	/*
70410Sstevel@tonic-gate 	 * First add in fixed size stuff
70420Sstevel@tonic-gate 	 */
70430Sstevel@tonic-gate 	len = sizeof (struct xattr_hdr) + sizeof (struct xattr_buf);
70440Sstevel@tonic-gate 
70450Sstevel@tonic-gate 	/*
70460Sstevel@tonic-gate 	 * Add space for two nulls
70470Sstevel@tonic-gate 	 */
70480Sstevel@tonic-gate 	stringlen = strlen(attrname) + strlen(filename) + 2;
70490Sstevel@tonic-gate 	complen = stringlen + sizeof (struct xattr_buf);
70500Sstevel@tonic-gate 
70510Sstevel@tonic-gate 	len += stringlen;
70520Sstevel@tonic-gate 
70530Sstevel@tonic-gate 	/*
70540Sstevel@tonic-gate 	 * Now add on space for link info if any
70550Sstevel@tonic-gate 	 */
70560Sstevel@tonic-gate 
70570Sstevel@tonic-gate 	if (linkinfo != NULL) {
70580Sstevel@tonic-gate 		/*
70590Sstevel@tonic-gate 		 * Again add space for two nulls
70600Sstevel@tonic-gate 		 */
70610Sstevel@tonic-gate 		linkstringlen = strlen(linkinfo->L_gen.g_attrfnam_p) +
70620Sstevel@tonic-gate 		    strlen(linkinfo->L_gen.g_attrnam_p) + 2;
70630Sstevel@tonic-gate 		len += linkstringlen;
70640Sstevel@tonic-gate 	}
70650Sstevel@tonic-gate 
70660Sstevel@tonic-gate 	/*
70670Sstevel@tonic-gate 	 * Now add padding to end to fill out TBLOCK
70680Sstevel@tonic-gate 	 *
70690Sstevel@tonic-gate 	 * Function returns size of real data and not size + padding.
70700Sstevel@tonic-gate 	 */
70710Sstevel@tonic-gate 
70720Sstevel@tonic-gate 	totalen = ROUNDTOTBLOCK(len);
70730Sstevel@tonic-gate 	bufhead = e_zalloc(E_EXIT, totalen);
70740Sstevel@tonic-gate 
70750Sstevel@tonic-gate 	/*
70760Sstevel@tonic-gate 	 * Now we can fill in the necessary pieces
70770Sstevel@tonic-gate 	 */
70780Sstevel@tonic-gate 
70790Sstevel@tonic-gate 	if (linkinfo != (struct Lnk *)NULL) {
70800Sstevel@tonic-gate 		linklen = linkstringlen + (sizeof (struct xattr_buf));
70810Sstevel@tonic-gate 	} else {
70820Sstevel@tonic-gate 		linklen = 0;
70830Sstevel@tonic-gate 	}
70840Sstevel@tonic-gate 
70850Sstevel@tonic-gate 	/*
70860Sstevel@tonic-gate 	 * first fill in the fixed header
70870Sstevel@tonic-gate 	 */
70880Sstevel@tonic-gate 	hptr = (struct xattr_hdr *)bufhead;
70890Sstevel@tonic-gate 	(void) sprintf(hptr->h_version, "%s", XATTR_ARCH_VERS);
70900Sstevel@tonic-gate 	(void) sprintf(hptr->h_component_len, "%0*d",
70910Sstevel@tonic-gate 	    sizeof (hptr->h_component_len) - 1, complen);
70920Sstevel@tonic-gate 	(void) sprintf(hptr->h_link_component_len, "%0*d",
70930Sstevel@tonic-gate 	    sizeof (hptr->h_link_component_len) - 1, linklen);
70940Sstevel@tonic-gate 	(void) sprintf(hptr->h_size, "%0*d", sizeof (hptr->h_size) - 1, len);
70950Sstevel@tonic-gate 
70960Sstevel@tonic-gate 	/*
70970Sstevel@tonic-gate 	 * Now fill in the filename + attrnames section
70980Sstevel@tonic-gate 	 */
70990Sstevel@tonic-gate 
71000Sstevel@tonic-gate 	tptr = (struct xattr_buf *)(bufhead + sizeof (struct xattr_hdr));
71010Sstevel@tonic-gate 	(void) sprintf(tptr->h_namesz, "%0*d", sizeof (tptr->h_namesz) - 1,
71020Sstevel@tonic-gate 	    stringlen);
71030Sstevel@tonic-gate 	(void) strcpy(tptr->h_names, filename);
71040Sstevel@tonic-gate 	(void) strcpy(&tptr->h_names[strlen(filename) + 1], attrname);
71050Sstevel@tonic-gate 	tptr->h_typeflag = typeflag;
71060Sstevel@tonic-gate 
71070Sstevel@tonic-gate 	/*
71080Sstevel@tonic-gate 	 * Now fill in the optional link section if we have one
71090Sstevel@tonic-gate 	 */
71100Sstevel@tonic-gate 
71110Sstevel@tonic-gate 	if (linkinfo != (struct Lnk *)NULL) {
71120Sstevel@tonic-gate 		tptr = (struct xattr_buf *)(bufhead +
71130Sstevel@tonic-gate 		    sizeof (struct xattr_hdr) + complen);
71140Sstevel@tonic-gate 
71150Sstevel@tonic-gate 		(void) sprintf(tptr->h_namesz, "%0*d",
71160Sstevel@tonic-gate 		    sizeof (tptr->h_namesz) - 1, linkstringlen);
71170Sstevel@tonic-gate 		(void) strcpy(tptr->h_names, linkinfo->L_gen.g_attrfnam_p);
71180Sstevel@tonic-gate 		(void) strcpy(
71190Sstevel@tonic-gate 		    &tptr->h_names[strlen(linkinfo->L_gen.g_attrfnam_p) + 1],
71200Sstevel@tonic-gate 		    linkinfo->L_gen.g_attrnam_p);
71210Sstevel@tonic-gate 		tptr->h_typeflag = typeflag;
71220Sstevel@tonic-gate 	}
71230Sstevel@tonic-gate 	*attrbuf = (char *)bufhead;
71240Sstevel@tonic-gate 	*rlen = len;
71250Sstevel@tonic-gate }
71260Sstevel@tonic-gate #endif /* O_XATTR */
71270Sstevel@tonic-gate 
71280Sstevel@tonic-gate static char
71290Sstevel@tonic-gate tartype(int type)
71300Sstevel@tonic-gate {
71310Sstevel@tonic-gate 	switch (type) {
71320Sstevel@tonic-gate 
71330Sstevel@tonic-gate 	case S_IFDIR:
71340Sstevel@tonic-gate 		return (DIRTYPE);
71350Sstevel@tonic-gate 
71360Sstevel@tonic-gate 	case S_IFLNK:
71370Sstevel@tonic-gate 		return (SYMTYPE);
71380Sstevel@tonic-gate 
71390Sstevel@tonic-gate 	case S_IFIFO:
71400Sstevel@tonic-gate 		return (FIFOTYPE);
71410Sstevel@tonic-gate 
71420Sstevel@tonic-gate 	case S_IFCHR:
71430Sstevel@tonic-gate 		return (CHRTYPE);
71440Sstevel@tonic-gate 
71450Sstevel@tonic-gate 	case S_IFBLK:
71460Sstevel@tonic-gate 		return (BLKTYPE);
71470Sstevel@tonic-gate 
71480Sstevel@tonic-gate 	case S_IFREG:
71490Sstevel@tonic-gate 		return (REGTYPE);
71500Sstevel@tonic-gate 
71510Sstevel@tonic-gate 	default:
71520Sstevel@tonic-gate 		return ('\0');
71530Sstevel@tonic-gate 	}
71540Sstevel@tonic-gate }
71550Sstevel@tonic-gate 
71560Sstevel@tonic-gate #if defined(O_XATTR)
71570Sstevel@tonic-gate static int
71580Sstevel@tonic-gate openfile(int omode)
71590Sstevel@tonic-gate {
71600Sstevel@tonic-gate 
71610Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
71620Sstevel@tonic-gate 		return (attropen(G_p->g_attrfnam_p, G_p->g_attrnam_p, omode));
71630Sstevel@tonic-gate 	} else {
71640Sstevel@tonic-gate 		return (openat(G_p->g_dirfd,
71650Sstevel@tonic-gate 		    get_component(G_p->g_nam_p), omode));
71660Sstevel@tonic-gate 	}
71670Sstevel@tonic-gate }
71680Sstevel@tonic-gate #else
71690Sstevel@tonic-gate static int
71700Sstevel@tonic-gate openfile(int omode)
71710Sstevel@tonic-gate {
71720Sstevel@tonic-gate 	return (openat(G_p->g_dirfd, get_component(G_p->g_nam_p), omode));
71730Sstevel@tonic-gate }
71740Sstevel@tonic-gate #endif
71750Sstevel@tonic-gate 
71760Sstevel@tonic-gate #if defined(O_XATTR)
71770Sstevel@tonic-gate static int
71780Sstevel@tonic-gate read_xattr_hdr()
71790Sstevel@tonic-gate {
71800Sstevel@tonic-gate 	off_t		bytes;
71810Sstevel@tonic-gate 	int		comp_len, link_len;
71820Sstevel@tonic-gate 	int		namelen;
71830Sstevel@tonic-gate 	int		cnt;
71840Sstevel@tonic-gate 	char		*tp;
71850Sstevel@tonic-gate 	int		pad;
71860Sstevel@tonic-gate 
71870Sstevel@tonic-gate 	/*
71880Sstevel@tonic-gate 	 * Include any padding in the read.  We need to be positioned
71890Sstevel@tonic-gate 	 * at beginning of next header.
71900Sstevel@tonic-gate 	 */
71910Sstevel@tonic-gate 
71920Sstevel@tonic-gate 	bytes = Gen.g_filesz;
71930Sstevel@tonic-gate 
71940Sstevel@tonic-gate 	if ((xattrhead = e_zalloc(E_NORMAL, (size_t)bytes)) == NULL) {
71950Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
71960Sstevel@tonic-gate 		    "Insufficient memory for extended attribute\n"));
71970Sstevel@tonic-gate 		return (1);
71980Sstevel@tonic-gate 	}
71990Sstevel@tonic-gate 
72000Sstevel@tonic-gate 	tp = (char *)xattrhead;
72010Sstevel@tonic-gate 	while (bytes > 0) {
72020Sstevel@tonic-gate 		cnt = (int)(bytes > CPIOBSZ) ? CPIOBSZ : bytes;
72030Sstevel@tonic-gate 		FILL(cnt);
72040Sstevel@tonic-gate 		(void) memcpy(tp, Buffr.b_out_p, cnt);
72050Sstevel@tonic-gate 		tp += cnt;
72060Sstevel@tonic-gate 		Buffr.b_out_p += cnt;
72070Sstevel@tonic-gate 		Buffr.b_cnt -= (off_t)cnt;
72080Sstevel@tonic-gate 		bytes -= (off_t)cnt;
72090Sstevel@tonic-gate 	}
72100Sstevel@tonic-gate 
72110Sstevel@tonic-gate 	pad = (Pad_val + 1 - (Gen.g_filesz & Pad_val)) &
72120Sstevel@tonic-gate 	    Pad_val;
72130Sstevel@tonic-gate 	if (pad != 0) {
72140Sstevel@tonic-gate 		FILL(pad);
72150Sstevel@tonic-gate 		Buffr.b_out_p += pad;
72160Sstevel@tonic-gate 		Buffr.b_cnt -= (off_t)pad;
72170Sstevel@tonic-gate 	}
72180Sstevel@tonic-gate 
72190Sstevel@tonic-gate 	/*
72200Sstevel@tonic-gate 	 * Validate that we can handle header format
72210Sstevel@tonic-gate 	 */
72220Sstevel@tonic-gate 
72230Sstevel@tonic-gate 	if (strcmp(xattrhead->h_version, XATTR_ARCH_VERS) != 0) {
72240Sstevel@tonic-gate 		(void) fprintf(stderr,
72250Sstevel@tonic-gate 		    gettext("Unknown extended attribute format encountered\n"));
72260Sstevel@tonic-gate 		(void) fprintf(stderr,
72270Sstevel@tonic-gate 		    gettext("Disabling extended attribute header parsing\n"));
72280Sstevel@tonic-gate 		xattrbadhead = 1;
72290Sstevel@tonic-gate 		return (1);
72300Sstevel@tonic-gate 	}
72310Sstevel@tonic-gate 	(void) sscanf(xattrhead->h_component_len, "%10d", &comp_len);
72320Sstevel@tonic-gate 	(void) sscanf(xattrhead->h_link_component_len, "%10d", &link_len);
72330Sstevel@tonic-gate 	xattrp = (struct xattr_buf *)(((char *)xattrhead) +
72340Sstevel@tonic-gate 	    sizeof (struct xattr_hdr));
72350Sstevel@tonic-gate 	(void) sscanf(xattrp->h_namesz, "%7d", &namelen);
72360Sstevel@tonic-gate 	if (link_len > 0) {
72370Sstevel@tonic-gate 		xattr_linkp = (struct xattr_buf *)((int)xattrp + (int)comp_len);
72380Sstevel@tonic-gate 	} else {
72390Sstevel@tonic-gate 		xattr_linkp = NULL;
72400Sstevel@tonic-gate 	}
72410Sstevel@tonic-gate 
72420Sstevel@tonic-gate 	return (0);
72430Sstevel@tonic-gate }
72440Sstevel@tonic-gate #endif
72450Sstevel@tonic-gate 
72460Sstevel@tonic-gate static mode_t
72470Sstevel@tonic-gate attrmode(char type)
72480Sstevel@tonic-gate {
72490Sstevel@tonic-gate 	mode_t mode;
72500Sstevel@tonic-gate 
72510Sstevel@tonic-gate 	switch (type) {
72520Sstevel@tonic-gate 	case '\0':
72530Sstevel@tonic-gate 	case REGTYPE:
72540Sstevel@tonic-gate 	case LNKTYPE:
72550Sstevel@tonic-gate 		mode = S_IFREG;
72560Sstevel@tonic-gate 		break;
72570Sstevel@tonic-gate 
72580Sstevel@tonic-gate 	case SYMTYPE:
72590Sstevel@tonic-gate 		mode = S_IFLNK;
72600Sstevel@tonic-gate 		break;
72610Sstevel@tonic-gate 
72620Sstevel@tonic-gate 	case CHRTYPE:
72630Sstevel@tonic-gate 		mode = S_IFCHR;
72640Sstevel@tonic-gate 		break;
72650Sstevel@tonic-gate 	case BLKTYPE:
72660Sstevel@tonic-gate 		mode = S_IFBLK;
72670Sstevel@tonic-gate 		break;
72680Sstevel@tonic-gate 	case DIRTYPE:
72690Sstevel@tonic-gate 		mode = S_IFDIR;
72700Sstevel@tonic-gate 		break;
72710Sstevel@tonic-gate 	case FIFOTYPE:
72720Sstevel@tonic-gate 		mode = S_IFIFO;
72730Sstevel@tonic-gate 		break;
72740Sstevel@tonic-gate 	case CONTTYPE:
72750Sstevel@tonic-gate 	default:
72760Sstevel@tonic-gate 		mode = 0;
72770Sstevel@tonic-gate 	}
72780Sstevel@tonic-gate 
72790Sstevel@tonic-gate 	return (mode);
72800Sstevel@tonic-gate }
72810Sstevel@tonic-gate 
72820Sstevel@tonic-gate #if defined(O_XATTR)
72830Sstevel@tonic-gate static char *
72840Sstevel@tonic-gate get_component(char *path)
72850Sstevel@tonic-gate {
72860Sstevel@tonic-gate 	char *ptr;
72870Sstevel@tonic-gate 
72880Sstevel@tonic-gate 	ptr = strrchr(path, '/');
72890Sstevel@tonic-gate 	if (ptr == NULL) {
72900Sstevel@tonic-gate 		return (path);
72910Sstevel@tonic-gate 	} else {
72920Sstevel@tonic-gate 		/*
72930Sstevel@tonic-gate 		 * Handle trailing slash
72940Sstevel@tonic-gate 		 */
72950Sstevel@tonic-gate 		if (*(ptr + 1) == '\0')
72960Sstevel@tonic-gate 			return (ptr);
72970Sstevel@tonic-gate 		else
72980Sstevel@tonic-gate 			return (ptr + 1);
72990Sstevel@tonic-gate 	}
73000Sstevel@tonic-gate }
73010Sstevel@tonic-gate #else
73020Sstevel@tonic-gate static char *
73030Sstevel@tonic-gate get_component(char *path)
73040Sstevel@tonic-gate {
73050Sstevel@tonic-gate 	return (path);
73060Sstevel@tonic-gate }
73070Sstevel@tonic-gate #endif
73080Sstevel@tonic-gate 
73090Sstevel@tonic-gate static int
73100Sstevel@tonic-gate open_dir(char *name)
73110Sstevel@tonic-gate {
73120Sstevel@tonic-gate 	int fd = -1;
73130Sstevel@tonic-gate 	int cnt = 0;
73140Sstevel@tonic-gate 	char *dir;
73150Sstevel@tonic-gate 
73160Sstevel@tonic-gate 	dir = e_zalloc(E_EXIT, strlen(name) + 1);
73170Sstevel@tonic-gate 
73180Sstevel@tonic-gate 	/*
73190Sstevel@tonic-gate 	 * open directory; creating missing directories along the way.
73200Sstevel@tonic-gate 	 */
73210Sstevel@tonic-gate 	get_parent(name, dir);
73220Sstevel@tonic-gate 	do {
73230Sstevel@tonic-gate 		fd = open(dir, O_RDONLY);
73240Sstevel@tonic-gate 		if (fd != -1) {
73250Sstevel@tonic-gate 			free(dir);
73260Sstevel@tonic-gate 			return (fd);
73270Sstevel@tonic-gate 		}
73280Sstevel@tonic-gate 		cnt++;
73290Sstevel@tonic-gate 	} while (cnt <= 1 && missdir(name) == 0);
73300Sstevel@tonic-gate 
73310Sstevel@tonic-gate 	free(dir);
73320Sstevel@tonic-gate 	return (-1);
73330Sstevel@tonic-gate }
73340Sstevel@tonic-gate 
73350Sstevel@tonic-gate static int
73360Sstevel@tonic-gate open_dirfd()
73370Sstevel@tonic-gate {
73380Sstevel@tonic-gate #ifdef O_XATTR
73390Sstevel@tonic-gate 	if ((Args & OCt) == 0) {
73400Sstevel@tonic-gate 		close_dirfd();
73410Sstevel@tonic-gate 		if (G_p->g_attrnam_p != (char *)NULL) {
73420Sstevel@tonic-gate 			G_p->g_dirfd = attropen(G_p->g_attrfnam_p,
73430Sstevel@tonic-gate 			    ".", O_RDONLY);
73440Sstevel@tonic-gate 			if (G_p->g_dirfd == -1 && (Args & (OCi | OCp))) {
73450Sstevel@tonic-gate 				G_p->g_dirfd =
73460Sstevel@tonic-gate 				    retry_attrdir_open(G_p->g_attrfnam_p);
73470Sstevel@tonic-gate 				if (G_p->g_dirfd == -1) {
73480Sstevel@tonic-gate 					msg(ERRN,
73490Sstevel@tonic-gate 					    "Cannot open attribute"
73500Sstevel@tonic-gate 					    " directory of file %s",
73510Sstevel@tonic-gate 					    G_p->g_attrfnam_p);
73520Sstevel@tonic-gate 					return (1);
73530Sstevel@tonic-gate 				}
73540Sstevel@tonic-gate 			}
73550Sstevel@tonic-gate 		} else {
73560Sstevel@tonic-gate 			G_p->g_dirfd = open_dir(G_p->g_nam_p);
73570Sstevel@tonic-gate 			if (G_p->g_dirfd == -1) {
73580Sstevel@tonic-gate 				msg(ERRN,
73590Sstevel@tonic-gate 				    "Cannot open/create %s", G_p->g_nam_p);
73600Sstevel@tonic-gate 				return (1);
73610Sstevel@tonic-gate 			}
73620Sstevel@tonic-gate 		}
73630Sstevel@tonic-gate 	} else {
73640Sstevel@tonic-gate 		G_p->g_dirfd = -1;
73650Sstevel@tonic-gate 	}
73660Sstevel@tonic-gate #else
73670Sstevel@tonic-gate 	G_p->g_dirfd = -1;
73680Sstevel@tonic-gate #endif
73690Sstevel@tonic-gate 	return (0);
73700Sstevel@tonic-gate }
73710Sstevel@tonic-gate 
73720Sstevel@tonic-gate static void
73730Sstevel@tonic-gate close_dirfd()
73740Sstevel@tonic-gate {
73750Sstevel@tonic-gate 	if (G_p->g_dirfd != -1) {
73760Sstevel@tonic-gate 		(void) close(G_p->g_dirfd);
73770Sstevel@tonic-gate 		G_p->g_dirfd = -1;
73780Sstevel@tonic-gate 	}
73790Sstevel@tonic-gate }
73800Sstevel@tonic-gate 
73810Sstevel@tonic-gate static void
73820Sstevel@tonic-gate write_xattr_hdr()
73830Sstevel@tonic-gate {
73840Sstevel@tonic-gate 	char *attrbuf = NULL;
73850Sstevel@tonic-gate 	int  attrlen = 0;
73860Sstevel@tonic-gate 	char *namep;
73870Sstevel@tonic-gate 	struct Lnk *tl_p, *linkinfo;
73880Sstevel@tonic-gate 
73890Sstevel@tonic-gate 
73900Sstevel@tonic-gate 	/*
73910Sstevel@tonic-gate 	 * namep was allocated in xattrs_out.  It is big enough to hold
73920Sstevel@tonic-gate 	 * either the name + .hdr on the end or just the attr name
73930Sstevel@tonic-gate 	 */
73940Sstevel@tonic-gate 
73950Sstevel@tonic-gate #if defined(O_XATTR)
73960Sstevel@tonic-gate 	namep = Gen.g_nam_p;
73970Sstevel@tonic-gate 	(void) creat_hdr();
73980Sstevel@tonic-gate 
73990Sstevel@tonic-gate 
74000Sstevel@tonic-gate 	if (Args & OCo) {
74010Sstevel@tonic-gate 		linkinfo = NULL;
74020Sstevel@tonic-gate 		tl_p = Lnk_hd.L_nxt_p;
74030Sstevel@tonic-gate 		while (tl_p != &Lnk_hd) {
74040Sstevel@tonic-gate 			if (tl_p->L_gen.g_ino == G_p->g_ino &&
74051134Sceastha 			    tl_p->L_gen.g_dev == G_p->g_dev) {
74060Sstevel@tonic-gate 					linkinfo = tl_p;
74070Sstevel@tonic-gate 					break; /* found */
74080Sstevel@tonic-gate 			}
74090Sstevel@tonic-gate 			tl_p = tl_p->L_nxt_p;
74100Sstevel@tonic-gate 		}
74110Sstevel@tonic-gate 		prepare_xattr_hdr(&attrbuf, Gen.g_attrfnam_p,
74120Sstevel@tonic-gate 		    Gen.g_attrnam_p,
74130Sstevel@tonic-gate 		    (linkinfo == (struct Lnk *)NULL) ?
74140Sstevel@tonic-gate 		    tartype(Gen.g_mode & Ftype) : LNKTYPE,
74150Sstevel@tonic-gate 		    linkinfo, &attrlen);
74160Sstevel@tonic-gate 		Gen.g_filesz = attrlen;
74170Sstevel@tonic-gate 		write_hdr(ARCHIVE_XATTR, (off_t)attrlen);
74180Sstevel@tonic-gate 		(void) sprintf(namep, "%s/%s", DEVNULL, Gen.g_attrnam_p);
74190Sstevel@tonic-gate 		(void) write_ancillary(attrbuf, attrlen);
74200Sstevel@tonic-gate 	}
74210Sstevel@tonic-gate 
74220Sstevel@tonic-gate 	(void) creat_hdr();
74230Sstevel@tonic-gate #endif
74240Sstevel@tonic-gate }
74250Sstevel@tonic-gate 
74260Sstevel@tonic-gate static int
74270Sstevel@tonic-gate retry_attrdir_open(char *name)
74280Sstevel@tonic-gate {
74290Sstevel@tonic-gate 	int dirfd = -1;
74300Sstevel@tonic-gate 	struct timeval times[2];
74310Sstevel@tonic-gate 	mode_t newmode;
74320Sstevel@tonic-gate 	struct stat parentstat;
7433*1231Smarks 	acl_t *aclp = NULL;
7434*1231Smarks 	int error;
74350Sstevel@tonic-gate 
74360Sstevel@tonic-gate 	/*
74370Sstevel@tonic-gate 	 * We couldn't get to attrdir. See if its
74380Sstevel@tonic-gate 	 * just a mode problem on the parent file.
74390Sstevel@tonic-gate 	 * for example: a mode such as r-xr--r--
74400Sstevel@tonic-gate 	 * won't let us create an attribute dir
74410Sstevel@tonic-gate 	 * if it doesn't already exist.
7442*1231Smarks 	 *
7443*1231Smarks 	 * If file has a non-trivial ACL, then save it
7444*1231Smarks 	 * off so that we can place it back on after doing
7445*1231Smarks 	 * chmod's.
74460Sstevel@tonic-gate 	 */
74470Sstevel@tonic-gate 
74480Sstevel@tonic-gate 	if (stat(name, &parentstat) == -1) {
74490Sstevel@tonic-gate 		msg(ERRN, "Cannot stat file %s", name);
74500Sstevel@tonic-gate 		return (-1);
74510Sstevel@tonic-gate 	}
7452*1231Smarks 
7453*1231Smarks 	if ((error = acl_get(name, ACL_NO_TRIVIAL, &aclp)) != 0) {
7454*1231Smarks 		msg(ERRN,
7455*1231Smarks 		    "Failed to retrieve ACL on %s %s", name, strerror(errno));
7456*1231Smarks 		return (-1);
7457*1231Smarks 	}
7458*1231Smarks 
74590Sstevel@tonic-gate 	newmode = S_IWUSR | parentstat.st_mode;
74600Sstevel@tonic-gate 	if (chmod(name, newmode) == -1) {
74610Sstevel@tonic-gate 		msg(ERRN, "Cannot change mode of file %s to %o", name, newmode);
7462*1231Smarks 		if (aclp)
7463*1231Smarks 			acl_free(aclp);
74640Sstevel@tonic-gate 		return (-1);
74650Sstevel@tonic-gate 	}
74660Sstevel@tonic-gate 
74670Sstevel@tonic-gate 	dirfd = attropen(name, ".", O_RDONLY);
7468*1231Smarks 
7469*1231Smarks 	/*
7470*1231Smarks 	 * Don't print error here, caller will handle printing out
7471*1231Smarks 	 * can't open message.
7472*1231Smarks 	 */
7473*1231Smarks 
7474*1231Smarks 	/*
7475*1231Smarks 	 * Put mode back to original
7476*1231Smarks 	 */
7477*1231Smarks 	if (chmod(name, parentstat.st_mode) != 0) {
7478*1231Smarks 		msg(ERRN, "Cannot restore permissions of file %s to %o",
7479*1231Smarks 		    name, parentstat.st_mode);
7480*1231Smarks 	}
7481*1231Smarks 
7482*1231Smarks 	if (aclp) {
7483*1231Smarks 		error = acl_set(name, aclp);
7484*1231Smarks 		if (error) {
7485*1231Smarks 			msg(ERRN, "failed to set ACL on %s", name);
7486*1231Smarks 		}
7487*1231Smarks 		acl_free(aclp);
7488*1231Smarks 	}
7489*1231Smarks 	/*
7490*1231Smarks 	 * Put back time stamps
7491*1231Smarks 	 */
7492*1231Smarks 
7493*1231Smarks 	times[0].tv_sec = parentstat.st_atime;
7494*1231Smarks 	times[0].tv_usec = 0;
7495*1231Smarks 	times[1].tv_sec = parentstat.st_mtime;
7496*1231Smarks 	times[1].tv_usec = 0;
7497*1231Smarks 	if (utimes(name, times) != 0) {
7498*1231Smarks 		msg(ERRN, "Cannot reset timestamps on file %s");
74990Sstevel@tonic-gate 	}
75000Sstevel@tonic-gate 
75010Sstevel@tonic-gate 	return (dirfd);
75020Sstevel@tonic-gate }
75030Sstevel@tonic-gate 
75040Sstevel@tonic-gate /*
75050Sstevel@tonic-gate  * skip over extra slashes in string.
75060Sstevel@tonic-gate  *
75070Sstevel@tonic-gate  * For example:
75080Sstevel@tonic-gate  * /usr/tmp/////
75090Sstevel@tonic-gate  *
75100Sstevel@tonic-gate  * would return pointer at
75110Sstevel@tonic-gate  * /usr/tmp/////
75120Sstevel@tonic-gate  *         ^
75130Sstevel@tonic-gate  */
75140Sstevel@tonic-gate static char *
75150Sstevel@tonic-gate skipslashes(char *string, char *start)
75160Sstevel@tonic-gate {
75170Sstevel@tonic-gate 	while ((string > start) && *(string - 1) == '/') {
75180Sstevel@tonic-gate 		string--;
75190Sstevel@tonic-gate 	}
75200Sstevel@tonic-gate 
75210Sstevel@tonic-gate 	return (string);
75220Sstevel@tonic-gate }
75230Sstevel@tonic-gate 
75240Sstevel@tonic-gate static sl_info_t *
75250Sstevel@tonic-gate sl_info_alloc(void)
75260Sstevel@tonic-gate {
75270Sstevel@tonic-gate 	static int num_left;
75280Sstevel@tonic-gate 	static sl_info_t *slipool;
75290Sstevel@tonic-gate 
75300Sstevel@tonic-gate 	if (num_left > 0) {
75310Sstevel@tonic-gate 		return (&slipool[--num_left]);
75320Sstevel@tonic-gate 	}
75330Sstevel@tonic-gate 	num_left = SL_INFO_ALLOC_CHUNK;
75340Sstevel@tonic-gate 	slipool = e_zalloc(E_EXIT, sizeof (sl_info_t) * num_left);
75350Sstevel@tonic-gate 	return (&slipool[--num_left]);
75360Sstevel@tonic-gate }
75370Sstevel@tonic-gate 
75380Sstevel@tonic-gate /*
75390Sstevel@tonic-gate  * If a match for the key values was found in the tree, return a pointer to it.
75400Sstevel@tonic-gate  * If a match was not found, insert it and return a pointer to it.  This is
75410Sstevel@tonic-gate  * based on Knuth's Algorithm A in Vol 3, section 6.2.3.
75420Sstevel@tonic-gate  */
75430Sstevel@tonic-gate 
75440Sstevel@tonic-gate sl_info_t *
75450Sstevel@tonic-gate sl_insert(dev_t device, ino_t inode)
75460Sstevel@tonic-gate {
75470Sstevel@tonic-gate 	sl_info_t *p;		/* moves down the tree */
75480Sstevel@tonic-gate 	sl_info_t *q;		/* scratch */
75490Sstevel@tonic-gate 	sl_info_t *r;		/* scratch */
75500Sstevel@tonic-gate 	sl_info_t *s;		/* pt where rebalancing may be needed */
75510Sstevel@tonic-gate 	sl_info_t *t;		/* father of s */
75520Sstevel@tonic-gate 	sl_info_t *head;
75530Sstevel@tonic-gate 
75540Sstevel@tonic-gate 	int a;			/* used to hold balance factors */
75550Sstevel@tonic-gate 	int done;		/* loop control */
75560Sstevel@tonic-gate 	int cmpflg;		/* used to hold the result of a comparison */
75570Sstevel@tonic-gate 
75580Sstevel@tonic-gate 	/* initialize */
75590Sstevel@tonic-gate 
75600Sstevel@tonic-gate 	head = sl_devhash_lookup(device);
75610Sstevel@tonic-gate 
75620Sstevel@tonic-gate 	if (head == NULL) {
75630Sstevel@tonic-gate 		head = sl_info_alloc();
75640Sstevel@tonic-gate 		head->llink = NULL;
75650Sstevel@tonic-gate 		head->bal = 0;
75660Sstevel@tonic-gate 
75670Sstevel@tonic-gate 		p = head->rlink = sl_info_alloc();
75680Sstevel@tonic-gate 		p->sl_ino = inode;
75690Sstevel@tonic-gate 		p->sl_count = 0;
75700Sstevel@tonic-gate 		p->bal = 0;
75710Sstevel@tonic-gate 		p->llink = NULL;
75720Sstevel@tonic-gate 		p->rlink = NULL;
75730Sstevel@tonic-gate 		sl_devhash_insert(device, head);
75740Sstevel@tonic-gate 		return (p);
75750Sstevel@tonic-gate 	}
75760Sstevel@tonic-gate 
75770Sstevel@tonic-gate 	t = head;
75780Sstevel@tonic-gate 	s = p = head->rlink;
75790Sstevel@tonic-gate 
75800Sstevel@tonic-gate 	/* compare */
75810Sstevel@tonic-gate 
75820Sstevel@tonic-gate 	for (done = 0; ! done; ) {
75830Sstevel@tonic-gate 		switch (sl_compare(inode, p->sl_ino)) {
75840Sstevel@tonic-gate 			case -1:
75850Sstevel@tonic-gate 				/* move left */
75860Sstevel@tonic-gate 
75870Sstevel@tonic-gate 				q = p->llink;
75880Sstevel@tonic-gate 
75890Sstevel@tonic-gate 				if (q == NULL) {
75900Sstevel@tonic-gate 					q = sl_info_alloc();
75910Sstevel@tonic-gate 					p->llink = q;
75920Sstevel@tonic-gate 					done = 1;
75930Sstevel@tonic-gate 					continue;
75940Sstevel@tonic-gate 				}
75950Sstevel@tonic-gate 
75960Sstevel@tonic-gate 				break;
75970Sstevel@tonic-gate 
75980Sstevel@tonic-gate 			case 0:
75990Sstevel@tonic-gate 				/* found it */
76000Sstevel@tonic-gate 				return (p);
76010Sstevel@tonic-gate 				break;
76020Sstevel@tonic-gate 
76030Sstevel@tonic-gate 			case 1:
76040Sstevel@tonic-gate 				/* move right */
76050Sstevel@tonic-gate 
76060Sstevel@tonic-gate 				q = p->rlink;
76070Sstevel@tonic-gate 
76080Sstevel@tonic-gate 				if (q == NULL) {
76090Sstevel@tonic-gate 					q = sl_info_alloc();
76100Sstevel@tonic-gate 					p->rlink = q;
76110Sstevel@tonic-gate 					done = 1;
76120Sstevel@tonic-gate 					continue;
76130Sstevel@tonic-gate 				}
76140Sstevel@tonic-gate 
76150Sstevel@tonic-gate 				break;
76160Sstevel@tonic-gate 		}
76170Sstevel@tonic-gate 
76180Sstevel@tonic-gate 		if (q->bal != 0) {
76190Sstevel@tonic-gate 			t = p;
76200Sstevel@tonic-gate 			s = q;
76210Sstevel@tonic-gate 		}
76220Sstevel@tonic-gate 
76230Sstevel@tonic-gate 		p = q;
76240Sstevel@tonic-gate 	}
76250Sstevel@tonic-gate 
76260Sstevel@tonic-gate 	/* insert */
76270Sstevel@tonic-gate 
76280Sstevel@tonic-gate 	q->sl_ino = inode;
76290Sstevel@tonic-gate 	q->sl_count = 0;
76300Sstevel@tonic-gate 	q->llink = q->rlink = NULL;
76310Sstevel@tonic-gate 	q->bal = 0;
76320Sstevel@tonic-gate 
76330Sstevel@tonic-gate 	/* adjust balance factors */
76340Sstevel@tonic-gate 
76350Sstevel@tonic-gate 	if ((cmpflg = sl_compare(inode, s->sl_ino)) < 0) {
76360Sstevel@tonic-gate 		r = p = s->llink;
76370Sstevel@tonic-gate 	} else {
76380Sstevel@tonic-gate 		r = p = s->rlink;
76390Sstevel@tonic-gate 	}
76400Sstevel@tonic-gate 
76410Sstevel@tonic-gate 	while (p != q) {
76420Sstevel@tonic-gate 		switch (sl_compare(inode, p->sl_ino)) {
76430Sstevel@tonic-gate 			case -1:
76440Sstevel@tonic-gate 				p->bal = -1;
76450Sstevel@tonic-gate 				p = p->llink;
76460Sstevel@tonic-gate 				break;
76470Sstevel@tonic-gate 
76480Sstevel@tonic-gate 			case 0:
76490Sstevel@tonic-gate 				break;
76500Sstevel@tonic-gate 
76510Sstevel@tonic-gate 			case 1:
76520Sstevel@tonic-gate 				p->bal = 1;
76530Sstevel@tonic-gate 				p = p->rlink;
76540Sstevel@tonic-gate 				break;
76550Sstevel@tonic-gate 		}
76560Sstevel@tonic-gate 	}
76570Sstevel@tonic-gate 
76580Sstevel@tonic-gate 	/* balancing act */
76590Sstevel@tonic-gate 
76600Sstevel@tonic-gate 	if (cmpflg < 0) {
76610Sstevel@tonic-gate 		a = -1;
76620Sstevel@tonic-gate 	} else {
76630Sstevel@tonic-gate 		a = 1;
76640Sstevel@tonic-gate 	}
76650Sstevel@tonic-gate 
76660Sstevel@tonic-gate 	if (s->bal == 0) {
76670Sstevel@tonic-gate 		s->bal = a;
76680Sstevel@tonic-gate 		head->llink = (sl_info_t *)((int)head->llink + 1);
76690Sstevel@tonic-gate 		return (q);
76700Sstevel@tonic-gate 	} else if (s->bal == -a) {
76710Sstevel@tonic-gate 		s->bal = 0;
76720Sstevel@tonic-gate 		return (q);
76730Sstevel@tonic-gate 	}
76740Sstevel@tonic-gate 
76750Sstevel@tonic-gate 	/*
76760Sstevel@tonic-gate 	 * (s->bal == a)
76770Sstevel@tonic-gate 	 */
76780Sstevel@tonic-gate 
76790Sstevel@tonic-gate 	if (r->bal == a) {
76800Sstevel@tonic-gate 		/* single rotation */
76810Sstevel@tonic-gate 
76820Sstevel@tonic-gate 		p = r;
76830Sstevel@tonic-gate 
76840Sstevel@tonic-gate 		if (a == -1) {
76850Sstevel@tonic-gate 			s->llink = r->rlink;
76860Sstevel@tonic-gate 			r->rlink = s;
76870Sstevel@tonic-gate 		} else if (a == 1) {
76880Sstevel@tonic-gate 			s->rlink = r->llink;
76890Sstevel@tonic-gate 			r->llink = s;
76900Sstevel@tonic-gate 		}
76910Sstevel@tonic-gate 
76920Sstevel@tonic-gate 		s->bal = r->bal = 0;
76930Sstevel@tonic-gate 
76940Sstevel@tonic-gate 	} else if (r->bal == -a) {
76950Sstevel@tonic-gate 		/* double rotation */
76960Sstevel@tonic-gate 
76970Sstevel@tonic-gate 		if (a == -1) {
76980Sstevel@tonic-gate 			p = r->rlink;
76990Sstevel@tonic-gate 			r->rlink = p->llink;
77000Sstevel@tonic-gate 			p->llink = r;
77010Sstevel@tonic-gate 			s->llink = p->rlink;
77020Sstevel@tonic-gate 			p->rlink = s;
77030Sstevel@tonic-gate 		} else if (a == 1) {
77040Sstevel@tonic-gate 			p = r->llink;
77050Sstevel@tonic-gate 			r->llink = p->rlink;
77060Sstevel@tonic-gate 			p->rlink = r;
77070Sstevel@tonic-gate 			s->rlink = p->llink;
77080Sstevel@tonic-gate 			p->llink = s;
77090Sstevel@tonic-gate 		}
77100Sstevel@tonic-gate 
77110Sstevel@tonic-gate 		if (p->bal == 0) {
77120Sstevel@tonic-gate 			s->bal = 0;
77130Sstevel@tonic-gate 			r->bal = 0;
77140Sstevel@tonic-gate 		} else if (p->bal == -a) {
77150Sstevel@tonic-gate 			s->bal = 0;
77160Sstevel@tonic-gate 			r->bal = a;
77170Sstevel@tonic-gate 		} else if (p->bal == a) {
77180Sstevel@tonic-gate 			s->bal = -a;
77190Sstevel@tonic-gate 			r->bal = 0;
77200Sstevel@tonic-gate 		}
77210Sstevel@tonic-gate 
77220Sstevel@tonic-gate 		p->bal = 0;
77230Sstevel@tonic-gate 	}
77240Sstevel@tonic-gate 
77250Sstevel@tonic-gate 	/* finishing touch */
77260Sstevel@tonic-gate 
77270Sstevel@tonic-gate 	if (s == t->rlink) {
77280Sstevel@tonic-gate 		t->rlink = p;
77290Sstevel@tonic-gate 	} else {
77300Sstevel@tonic-gate 		t->llink = p;
77310Sstevel@tonic-gate 	}
77320Sstevel@tonic-gate 
77330Sstevel@tonic-gate 	return (q);
77340Sstevel@tonic-gate }
77350Sstevel@tonic-gate 
77360Sstevel@tonic-gate /*
77370Sstevel@tonic-gate  * sl_numlinks: return the number of links that we saw during our preview.
77380Sstevel@tonic-gate  */
77390Sstevel@tonic-gate 
77400Sstevel@tonic-gate static ulong_t
77410Sstevel@tonic-gate sl_numlinks(dev_t device, ino_t inode)
77420Sstevel@tonic-gate {
77430Sstevel@tonic-gate 	sl_info_t *p = sl_search(device, inode);
77440Sstevel@tonic-gate 
77450Sstevel@tonic-gate 	if (p) {
77461134Sceastha 		return (p->sl_count);
77470Sstevel@tonic-gate 	} else {
77481134Sceastha 		return (1);
77490Sstevel@tonic-gate 	}
77500Sstevel@tonic-gate }
77510Sstevel@tonic-gate 
77520Sstevel@tonic-gate /*
77530Sstevel@tonic-gate  * sl_preview_synonyms:  Read the file list from the input stream, remembering
77540Sstevel@tonic-gate  * each reference to each file.
77550Sstevel@tonic-gate  */
77560Sstevel@tonic-gate 
77570Sstevel@tonic-gate static void
77580Sstevel@tonic-gate sl_preview_synonyms(void)
77590Sstevel@tonic-gate {
77600Sstevel@tonic-gate 	char buf [APATH+1];
77610Sstevel@tonic-gate 	char *s;
77620Sstevel@tonic-gate 
77630Sstevel@tonic-gate 	char *suffix = "/cpioXXXXXX";
77640Sstevel@tonic-gate 	char *tmpdir = getenv("TMPDIR");
77650Sstevel@tonic-gate 	int    tmpfd, islnk;
77660Sstevel@tonic-gate 	FILE *tmpfile;
77670Sstevel@tonic-gate 	char *tmpfname;
77680Sstevel@tonic-gate 
77690Sstevel@tonic-gate 	if (tmpdir == NULL || *tmpdir == '\0' ||
77700Sstevel@tonic-gate 	    (strlen(tmpdir) + strlen(suffix)) > APATH) {
77710Sstevel@tonic-gate 		struct statvfs tdsb;
77720Sstevel@tonic-gate 
77730Sstevel@tonic-gate 		tmpdir = "/var/tmp";
77740Sstevel@tonic-gate 
77750Sstevel@tonic-gate 		/* /var/tmp is read-only in the mini-root environment */
77760Sstevel@tonic-gate 
77770Sstevel@tonic-gate 		if (statvfs(tmpdir, &tdsb) == -1 || tdsb.f_flag & ST_RDONLY) {
77780Sstevel@tonic-gate 			tmpdir = "/tmp";
77790Sstevel@tonic-gate 		}
77800Sstevel@tonic-gate 	}
77810Sstevel@tonic-gate 
77820Sstevel@tonic-gate 	tmpfname = e_zalloc(E_EXIT, strlen(tmpdir) + strlen(suffix) + 1);
77830Sstevel@tonic-gate 
77840Sstevel@tonic-gate 	(void) strcpy(tmpfname, tmpdir);
77850Sstevel@tonic-gate 	(void) strcat(tmpfname, suffix);
77860Sstevel@tonic-gate 
77870Sstevel@tonic-gate 	if ((tmpfd = mkstemp(tmpfname)) == -1) {
77880Sstevel@tonic-gate 		msg(EXTN, "cannot open tmpfile %s", tmpfname);
77890Sstevel@tonic-gate 	}
77900Sstevel@tonic-gate 
77910Sstevel@tonic-gate 	if (unlink(tmpfname) == -1) {
77920Sstevel@tonic-gate 		msg(EXTN, "cannot unlink tmpfile %s", tmpfname);
77930Sstevel@tonic-gate 	}
77940Sstevel@tonic-gate 
77950Sstevel@tonic-gate 	if ((tmpfile = fdopen(tmpfd, "w+")) == NULL) {
77960Sstevel@tonic-gate 		msg(EXTN, "cannot fdopen tmpfile %s", tmpfname);
77970Sstevel@tonic-gate 	}
77980Sstevel@tonic-gate 
77990Sstevel@tonic-gate 	while ((s = fgets(buf, APATH+1, In_p)) != NULL) {
78000Sstevel@tonic-gate 		size_t lastchar;
78010Sstevel@tonic-gate 		struct stat sb;
78020Sstevel@tonic-gate 
78030Sstevel@tonic-gate 		if (fputs(buf, tmpfile) == EOF) {
78040Sstevel@tonic-gate 			msg(EXTN, "problem writing to tmpfile %s", tmpfname);
78050Sstevel@tonic-gate 		}
78060Sstevel@tonic-gate 
78070Sstevel@tonic-gate 		/* pre-process the name */
78080Sstevel@tonic-gate 
78090Sstevel@tonic-gate 		lastchar = strlen(s) - 1;
78100Sstevel@tonic-gate 
78110Sstevel@tonic-gate 		if (s[lastchar] != '\n' && lastchar == APATH - 1) {
78120Sstevel@tonic-gate 			continue;
78130Sstevel@tonic-gate 		} else {
78140Sstevel@tonic-gate 			s[lastchar] = '\0';
78150Sstevel@tonic-gate 		}
78160Sstevel@tonic-gate 
78170Sstevel@tonic-gate 		while (s[0] == '.' && s[1] == '/') {
78180Sstevel@tonic-gate 			s += 2;
78190Sstevel@tonic-gate 			while (s[0] == '/') {
78200Sstevel@tonic-gate 				s++;
78210Sstevel@tonic-gate 			}
78220Sstevel@tonic-gate 		}
78230Sstevel@tonic-gate 
78240Sstevel@tonic-gate 		if (lstat(s, &sb) < 0) {
78250Sstevel@tonic-gate 			continue;
78260Sstevel@tonic-gate 		}
78270Sstevel@tonic-gate 		islnk = 0;
78280Sstevel@tonic-gate 		if (S_ISLNK(sb.st_mode)) {
78290Sstevel@tonic-gate 			islnk = 1;
78300Sstevel@tonic-gate 			if (Args & OCL) {
78310Sstevel@tonic-gate 				if (stat(s, &sb) < 0) {
78320Sstevel@tonic-gate 					continue;
78330Sstevel@tonic-gate 				}
78340Sstevel@tonic-gate 			}
78350Sstevel@tonic-gate 		}
78360Sstevel@tonic-gate 		sl_remember_tgt(&sb, islnk);
78370Sstevel@tonic-gate 
78380Sstevel@tonic-gate #if defined(O_XATTR)
78390Sstevel@tonic-gate 		if (Atflag) {
78400Sstevel@tonic-gate 			int  dirfd;
78410Sstevel@tonic-gate 			DIR  *dirp;
78420Sstevel@tonic-gate 			struct dirent *dp;
78430Sstevel@tonic-gate 
78440Sstevel@tonic-gate 			if (pathconf(s, _PC_XATTR_EXISTS) != 1)
78450Sstevel@tonic-gate 				continue;
78460Sstevel@tonic-gate 
78470Sstevel@tonic-gate 			dirfd = attropen(s, ".", O_RDONLY);
78480Sstevel@tonic-gate 			if (dirfd == -1)
78490Sstevel@tonic-gate 				continue;
78500Sstevel@tonic-gate 
78510Sstevel@tonic-gate 			tmpfd = dup(dirfd);
78520Sstevel@tonic-gate 			if (tmpfd == -1) {
78530Sstevel@tonic-gate 				(void) close(dirfd);
78540Sstevel@tonic-gate 				continue;
78550Sstevel@tonic-gate 			}
78560Sstevel@tonic-gate 			dirp = fdopendir(tmpfd);
78570Sstevel@tonic-gate 			if (dirp == NULL) {
78580Sstevel@tonic-gate 				(void) close(dirfd);
78590Sstevel@tonic-gate 				(void) close(tmpfd);
78600Sstevel@tonic-gate 				continue;
78610Sstevel@tonic-gate 			}
78620Sstevel@tonic-gate 
78630Sstevel@tonic-gate 			while (dp = readdir(dirp)) {
78640Sstevel@tonic-gate 				if ((dp->d_name[0] == '.' &&
78650Sstevel@tonic-gate 				    dp->d_name[1] == '\0') ||
78660Sstevel@tonic-gate 				    (dp->d_name[0] == '.' &&
78670Sstevel@tonic-gate 				    dp->d_name[1] == '.' &&
78680Sstevel@tonic-gate 				    dp->d_name[2] == '\0'))
78690Sstevel@tonic-gate 					continue;
78700Sstevel@tonic-gate 
78710Sstevel@tonic-gate 				if (fstatat(dirfd, dp->d_name, &sb,
78720Sstevel@tonic-gate 				    AT_SYMLINK_NOFOLLOW) < 0) {
78730Sstevel@tonic-gate 					continue;
78740Sstevel@tonic-gate 				}
78750Sstevel@tonic-gate 				islnk = 0;
78760Sstevel@tonic-gate 				if (S_ISLNK(sb.st_mode)) {
78770Sstevel@tonic-gate 					islnk = 1;
78780Sstevel@tonic-gate 					if (Args & OCL) {
78790Sstevel@tonic-gate 						if (fstatat(dirfd, dp->d_name,
78800Sstevel@tonic-gate 						    &sb, 0) < 0) {
78810Sstevel@tonic-gate 							continue;
78820Sstevel@tonic-gate 						}
78830Sstevel@tonic-gate 					}
78840Sstevel@tonic-gate 				}
78850Sstevel@tonic-gate 				sl_remember_tgt(&sb, islnk);
78860Sstevel@tonic-gate 			}
78870Sstevel@tonic-gate 			(void) closedir(dirp);
78880Sstevel@tonic-gate 			(void) close(dirfd);
78890Sstevel@tonic-gate 		}
78900Sstevel@tonic-gate #endif
78910Sstevel@tonic-gate 	}
78920Sstevel@tonic-gate 
78930Sstevel@tonic-gate 	if (ferror(In_p)) {
78940Sstevel@tonic-gate 		msg(EXTN, "error reading stdin");
78950Sstevel@tonic-gate 	}
78960Sstevel@tonic-gate 
78970Sstevel@tonic-gate 	if (fseek(tmpfile, 0L, SEEK_SET) == -1) {
78980Sstevel@tonic-gate 		msg(EXTN, "cannot fseek on tmpfile %s", tmpfname);
78990Sstevel@tonic-gate 	}
79000Sstevel@tonic-gate 
79010Sstevel@tonic-gate 	In_p = tmpfile;
79020Sstevel@tonic-gate 	free(tmpfname);
79030Sstevel@tonic-gate }
79040Sstevel@tonic-gate 
79050Sstevel@tonic-gate /*
79060Sstevel@tonic-gate  * sl_remember_tgt: Add the device/inode for lstat or stat info to the list of
79070Sstevel@tonic-gate  * those we've seen before.
79080Sstevel@tonic-gate  *
79090Sstevel@tonic-gate  * This tree (rooted under head) is keyed by the device/inode of the file
79100Sstevel@tonic-gate  * being pointed to.  A count is kept of the number of references encountered
79110Sstevel@tonic-gate  * so far.
79120Sstevel@tonic-gate  */
79130Sstevel@tonic-gate 
79140Sstevel@tonic-gate static void
79150Sstevel@tonic-gate sl_remember_tgt(const struct stat *sbp, int isSymlink)
79160Sstevel@tonic-gate {
79170Sstevel@tonic-gate 	sl_info_t *p;
79180Sstevel@tonic-gate 	dev_t device;
79190Sstevel@tonic-gate 	ino_t inode;
79200Sstevel@tonic-gate 
79210Sstevel@tonic-gate 	device = sbp->st_dev;
79220Sstevel@tonic-gate 	inode  = sbp->st_ino;
79230Sstevel@tonic-gate 
79240Sstevel@tonic-gate 	/* Determine whether we've seen this one before */
79250Sstevel@tonic-gate 
79260Sstevel@tonic-gate 	p = sl_insert(device, inode);
79270Sstevel@tonic-gate 
79280Sstevel@tonic-gate 	if (p->sl_count > 0) {
79290Sstevel@tonic-gate 		/*
79300Sstevel@tonic-gate 		 * We have seen this file before.
79310Sstevel@tonic-gate 		 * Note that if we are not chasing symlinks, and this one is a
79320Sstevel@tonic-gate 		 * symlink, it is identically the one we saw before (you cannot
79330Sstevel@tonic-gate 		 * have hard links to symlinks); in this case, we leave the
79340Sstevel@tonic-gate 		 * count alone, so that we don't wind up archiving a symlink to
79350Sstevel@tonic-gate 		 * itself.
79360Sstevel@tonic-gate 		 */
79370Sstevel@tonic-gate 
79380Sstevel@tonic-gate 		if ((Args & OCL) || (! isSymlink)) {
79390Sstevel@tonic-gate 			p->sl_count++;
79400Sstevel@tonic-gate 		}
79410Sstevel@tonic-gate 	} else {
79420Sstevel@tonic-gate 		/* We have not seen this file before */
79430Sstevel@tonic-gate 
79440Sstevel@tonic-gate 		p->sl_count = 1;
79450Sstevel@tonic-gate 
79460Sstevel@tonic-gate 		if (Use_old_stat) {
79470Sstevel@tonic-gate 			/* -Hodc: remap inode (-1 on overflow) */
79480Sstevel@tonic-gate 
79490Sstevel@tonic-gate 			sl_remap_t  *q;
79500Sstevel@tonic-gate 
79510Sstevel@tonic-gate 			for (q = sl_remap_head; q && (q->dev != device);
79520Sstevel@tonic-gate 			    q = q->next) {
79530Sstevel@tonic-gate 				/* do nothing */
79540Sstevel@tonic-gate 			}
79550Sstevel@tonic-gate 
79560Sstevel@tonic-gate 			if (q == NULL) {
79570Sstevel@tonic-gate 				q = e_zalloc(E_EXIT, sizeof (sl_remap_t));
79580Sstevel@tonic-gate 				q->dev = device;
79590Sstevel@tonic-gate 				p->sl_ino2 = q->inode_count = 1;
79600Sstevel@tonic-gate 
79610Sstevel@tonic-gate 				q->next = (sl_remap_head) ?
79620Sstevel@tonic-gate 				    sl_remap_head->next : NULL;
79630Sstevel@tonic-gate 				sl_remap_head = q;
79640Sstevel@tonic-gate 			} else {
79650Sstevel@tonic-gate 				if ((size_t)q->inode_count <=
79660Sstevel@tonic-gate 				    ((1 << (sizeof (o_ino_t) * 8)) - 1)) {
79670Sstevel@tonic-gate 					/* fits in o_ino_t */
79680Sstevel@tonic-gate 					p->sl_ino2 = ++(q->inode_count);
79690Sstevel@tonic-gate 				} else {
79700Sstevel@tonic-gate 					p->sl_ino2 = (ino_t)-1;
79710Sstevel@tonic-gate 				}
79720Sstevel@tonic-gate 			}
79730Sstevel@tonic-gate 		}
79740Sstevel@tonic-gate 	}
79750Sstevel@tonic-gate }
79760Sstevel@tonic-gate 
79770Sstevel@tonic-gate /*
79780Sstevel@tonic-gate  * A faster search, which does not insert the key values into the tree.
79790Sstevel@tonic-gate  * If the a match was found in the tree, return a pointer to it.  If it was not
79800Sstevel@tonic-gate  * found, return NULL.
79810Sstevel@tonic-gate  */
79820Sstevel@tonic-gate 
79830Sstevel@tonic-gate sl_info_t *
79840Sstevel@tonic-gate sl_search(dev_t device, ino_t inode)
79850Sstevel@tonic-gate {
79860Sstevel@tonic-gate 	sl_info_t *p;		/* moves down the tree */
79870Sstevel@tonic-gate 	int c;			/* comparison value */
79880Sstevel@tonic-gate 	sl_info_t *retval = NULL; /* return value */
79890Sstevel@tonic-gate 	sl_info_t *head;
79900Sstevel@tonic-gate 
79910Sstevel@tonic-gate 	head = sl_devhash_lookup(device);
79920Sstevel@tonic-gate 	if (head != NULL) {
79930Sstevel@tonic-gate 		for (p = head->rlink; p; ) {
79940Sstevel@tonic-gate 			if ((c = sl_compare(inode, p->sl_ino)) == 0) {
79950Sstevel@tonic-gate 				retval = p;
79960Sstevel@tonic-gate 				break;
79970Sstevel@tonic-gate 			} else if (c < 0) {
79980Sstevel@tonic-gate 				p = p->llink;
79990Sstevel@tonic-gate 			} else {
80000Sstevel@tonic-gate 				p = p->rlink;
80010Sstevel@tonic-gate 			}
80020Sstevel@tonic-gate 		}
80030Sstevel@tonic-gate 	}
80040Sstevel@tonic-gate 
80050Sstevel@tonic-gate 	return (retval);
80060Sstevel@tonic-gate }
80070Sstevel@tonic-gate 
80080Sstevel@tonic-gate static sl_info_t *
80090Sstevel@tonic-gate sl_devhash_lookup(dev_t device)
80100Sstevel@tonic-gate {
80110Sstevel@tonic-gate 	int key;
80120Sstevel@tonic-gate 	sl_info_link_t *lp;
80130Sstevel@tonic-gate 	static sl_info_link_t *devcache;
80140Sstevel@tonic-gate 
80150Sstevel@tonic-gate 	if (devcache != NULL && devcache->dev == device) {
80160Sstevel@tonic-gate 		return (devcache->head);
80170Sstevel@tonic-gate 	}
80180Sstevel@tonic-gate 
80190Sstevel@tonic-gate 	key = DEV_HASHKEY(device);
80200Sstevel@tonic-gate 	for (lp = sl_devhash[key]; lp; lp = lp->next) {
80210Sstevel@tonic-gate 		if (lp->dev == device) {
80220Sstevel@tonic-gate 			devcache = lp;
80230Sstevel@tonic-gate 			return (lp->head);
80240Sstevel@tonic-gate 		}
80250Sstevel@tonic-gate 	}
80260Sstevel@tonic-gate 	return (NULL);
80270Sstevel@tonic-gate }
80280Sstevel@tonic-gate 
80290Sstevel@tonic-gate static void
80300Sstevel@tonic-gate sl_devhash_insert(dev_t device, sl_info_t *head)
80310Sstevel@tonic-gate {
80320Sstevel@tonic-gate 	int key = DEV_HASHKEY(device);
80330Sstevel@tonic-gate 	sl_info_link_t *lp;
80340Sstevel@tonic-gate 
80350Sstevel@tonic-gate 	lp = e_zalloc(E_EXIT, sizeof (sl_info_link_t));
80360Sstevel@tonic-gate 	lp->dev = device;
80370Sstevel@tonic-gate 	lp->head = head;
80380Sstevel@tonic-gate 	lp->next = sl_devhash[key];
80390Sstevel@tonic-gate 	sl_devhash[key] = lp;
80400Sstevel@tonic-gate }
80410Sstevel@tonic-gate 
80420Sstevel@tonic-gate static void
80430Sstevel@tonic-gate chop_endslashes(char *path)
80440Sstevel@tonic-gate {
80450Sstevel@tonic-gate 	char *end, *ptr;
80460Sstevel@tonic-gate 
80470Sstevel@tonic-gate 	end = &path[strlen(path) -1];
80480Sstevel@tonic-gate 	if (*end == '/' && end != path) {
80490Sstevel@tonic-gate 		ptr = skipslashes(end, path);
80500Sstevel@tonic-gate 		if (ptr != NULL && ptr != path) {
80510Sstevel@tonic-gate 			*ptr = '\0';
80520Sstevel@tonic-gate 		}
80530Sstevel@tonic-gate 	}
80540Sstevel@tonic-gate }
80550Sstevel@tonic-gate 
80560Sstevel@tonic-gate #if !defined(O_XATTR)
80570Sstevel@tonic-gate int
80580Sstevel@tonic-gate openat64(int fd, char *name, int oflag, mode_t cmode)
80590Sstevel@tonic-gate {
80600Sstevel@tonic-gate 	return (open64(name, oflag, cmode));
80610Sstevel@tonic-gate }
80620Sstevel@tonic-gate 
80630Sstevel@tonic-gate int
80640Sstevel@tonic-gate openat(int fd, char *name, int oflag, mode_t cmode)
80650Sstevel@tonic-gate {
80660Sstevel@tonic-gate 	return (open(name, oflag, cmode));
80670Sstevel@tonic-gate }
80680Sstevel@tonic-gate 
80690Sstevel@tonic-gate int
80700Sstevel@tonic-gate fchownat(int fd, char *name, uid_t owner, gid_t group, int flag)
80710Sstevel@tonic-gate {
80720Sstevel@tonic-gate 	if (flag == AT_SYMLINK_NOFOLLOW)
80730Sstevel@tonic-gate 		return (lchown(name, owner, group));
80740Sstevel@tonic-gate 	else
80750Sstevel@tonic-gate 		return (chown(name, owner, group));
80760Sstevel@tonic-gate }
80770Sstevel@tonic-gate 
80780Sstevel@tonic-gate int
80790Sstevel@tonic-gate renameat(int fromfd, char *old, int tofd, char *new)
80800Sstevel@tonic-gate {
80810Sstevel@tonic-gate 	return (rename(old, new));
80820Sstevel@tonic-gate }
80830Sstevel@tonic-gate 
80840Sstevel@tonic-gate int
80850Sstevel@tonic-gate futimesat(int fd, char *path, struct timeval times[2])
80860Sstevel@tonic-gate {
80870Sstevel@tonic-gate 	return (utimes(path, times));
80880Sstevel@tonic-gate }
80890Sstevel@tonic-gate 
80900Sstevel@tonic-gate int
80910Sstevel@tonic-gate unlinkat(int dirfd, char *path, int flag)
80920Sstevel@tonic-gate {
80930Sstevel@tonic-gate 	if (flag == AT_REMOVEDIR) {
80940Sstevel@tonic-gate 		return (rmdir(path));
80950Sstevel@tonic-gate 	} else {
80960Sstevel@tonic-gate 		return (unlink(path));
80970Sstevel@tonic-gate 	}
80980Sstevel@tonic-gate }
80990Sstevel@tonic-gate 
81000Sstevel@tonic-gate int
81010Sstevel@tonic-gate fstatat(int fd, char *path, struct stat *buf, int flag)
81020Sstevel@tonic-gate {
81030Sstevel@tonic-gate 	if (flag == AT_SYMLINK_NOFOLLOW)
81040Sstevel@tonic-gate 		return (lstat(path, buf));
81050Sstevel@tonic-gate 	else
81060Sstevel@tonic-gate 		return (stat(path, buf));
81070Sstevel@tonic-gate }
81080Sstevel@tonic-gate 
81090Sstevel@tonic-gate int
81100Sstevel@tonic-gate attropen(char *file, char *attr, int omode, mode_t cmode)
81110Sstevel@tonic-gate {
81120Sstevel@tonic-gate 	errno = ENOTSUP;
81130Sstevel@tonic-gate 	return (-1);
81140Sstevel@tonic-gate }
81150Sstevel@tonic-gate #endif
8116