xref: /illumos-gate/usr/src/cmd/cpio/cpio.c (revision b0ee9efa)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53a3e214eSceastha  * Common Development and Distribution License (the "License").
63a3e214eSceastha  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2261694e45SRich Burridge  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
2333f5ff17SMilan Jurik  * Copyright 2012 Milan Jurik. All rights reserved.
24*b0ee9efaSGary Mills  * Copyright (c) 2012 Gary Mills
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	All Rights Reserved					*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
327c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include <stdlib.h>
407c478bd9Sstevel@tonic-gate #include <fcntl.h>
417c478bd9Sstevel@tonic-gate #include <memory.h>
427c478bd9Sstevel@tonic-gate #include <string.h>
437c478bd9Sstevel@tonic-gate #include <stdarg.h>
447c478bd9Sstevel@tonic-gate #include <sys/stat.h>
457c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
467c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
477c478bd9Sstevel@tonic-gate #include <sys/param.h>
487c478bd9Sstevel@tonic-gate #include <utime.h>
497c478bd9Sstevel@tonic-gate #include <pwd.h>
507c478bd9Sstevel@tonic-gate #include <grp.h>
517c478bd9Sstevel@tonic-gate #include <signal.h>
527c478bd9Sstevel@tonic-gate #include <ctype.h>
537c478bd9Sstevel@tonic-gate #include <locale.h>
547c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
557c478bd9Sstevel@tonic-gate #include <sys/mtio.h>
567c478bd9Sstevel@tonic-gate #include <sys/fdio.h>
577c478bd9Sstevel@tonic-gate #include "cpio.h"
587c478bd9Sstevel@tonic-gate #include <sys/acl.h>
597c478bd9Sstevel@tonic-gate #include <sys/time.h>
607c478bd9Sstevel@tonic-gate #include <sys/resource.h>
617c478bd9Sstevel@tonic-gate #include <fnmatch.h>
627c478bd9Sstevel@tonic-gate #include <libgen.h>
637c478bd9Sstevel@tonic-gate #include <libintl.h>
647c478bd9Sstevel@tonic-gate #include <dirent.h>
657c478bd9Sstevel@tonic-gate #include <limits.h>
66fa9e4066Sahrens #include <aclutils.h>
67ced83f9bSceastha #if defined(_PC_SATTR_ENABLED)
68ced83f9bSceastha #include <libnvpair.h>
69ced83f9bSceastha #include <attr.h>
70ced83f9bSceastha #include <libcmdutils.h>
71ced83f9bSceastha #endif	/* _PC_SATTR_ENABLED */
72647ab8f4Sceastha #ifdef SOLARIS_PRIVS
73647ab8f4Sceastha #include <priv.h>
74647ab8f4Sceastha #endif	/* SOLARIS_PRIVS */
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * Special kludge for off_t being a signed quantity.
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate #if _FILE_OFFSET_BITS == 64
807c478bd9Sstevel@tonic-gate typedef	u_longlong_t	u_off_t;
817c478bd9Sstevel@tonic-gate #else
827c478bd9Sstevel@tonic-gate typedef	ulong_t		u_off_t;
837c478bd9Sstevel@tonic-gate #endif
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #define	SECMODE	0xe080
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #define	DEVNULL		"/dev/null"
887c478bd9Sstevel@tonic-gate #define	XATTRHDR	".hdr"
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #define	NAMELEN		32
917c478bd9Sstevel@tonic-gate #define	TYPELEN 	16
927c478bd9Sstevel@tonic-gate #define	PERMLEN		4
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate #define	FILE_COPIED	1
957c478bd9Sstevel@tonic-gate #define	FILE_LINKED	2
967c478bd9Sstevel@tonic-gate #define	FILE_PASS_ERR	-1
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate #define	ARCHIVE_NORMAL	0
997c478bd9Sstevel@tonic-gate #define	ARCHIVE_ACL	1
1007c478bd9Sstevel@tonic-gate #define	ARCHIVE_XATTR	2
1015fbb8099SNobutomo Nakano #define	ARCHIVE_SPARSE	3
1027c478bd9Sstevel@tonic-gate 
103ced83f9bSceastha #ifndef	VIEW_READONLY
104ced83f9bSceastha #define	VIEW_READONLY	"SUNWattr_ro"
105ced83f9bSceastha #endif
106ced83f9bSceastha 
107ced83f9bSceastha #ifndef	VIEW_READWRITE
108ced83f9bSceastha #define	VIEW_READWRITE	"SUNWattr_rw"
109ced83f9bSceastha #endif
110ced83f9bSceastha 
111ced83f9bSceastha 
1127c478bd9Sstevel@tonic-gate #define	LSTAT(dir, path, statbuf) fstatat(dir, \
113ced83f9bSceastha     get_component((Gen.g_attrnam_p == NULL) ? \
1147c478bd9Sstevel@tonic-gate     path : Gen.g_attrnam_p), statbuf, AT_SYMLINK_NOFOLLOW)
1157c478bd9Sstevel@tonic-gate #define	STAT(dir, path, statbuf) fstatat(dir, \
116ced83f9bSceastha     get_component((Gen.g_attrnam_p == NULL) ? \
1177c478bd9Sstevel@tonic-gate     path : Gen.g_attrnam_p), statbuf, 0)
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate  *	These limits reflect the maximum size regular file that
1217c478bd9Sstevel@tonic-gate  *	can be archived, depending on the archive type. For archives
1227c478bd9Sstevel@tonic-gate  *	with character-format headers (odc, tar, ustar) we use
123944d83a0Schin  *	CHAR_OFFSET_MAX.  For archives with SVR4 ASCII headers (-c, -H crc)
124944d83a0Schin  *	we store filesize in an 8-char hexadecimal string and use
125944d83a0Schin  *	ASC_OFFSET_MAX.  Otherwise, we are limited to the size that will
126944d83a0Schin  *	fit in a signed long value.
1277c478bd9Sstevel@tonic-gate  */
128944d83a0Schin #define	CHAR_OFFSET_MAX	077777777777ULL	/* 11 octal digits */
129944d83a0Schin #define	ASC_OFFSET_MAX	0XFFFFFFFF	/* 8 hexadecimal digits */
1307c478bd9Sstevel@tonic-gate #define	BIN_OFFSET_MAX	LONG_MAX	/* signed long max value */
1317c478bd9Sstevel@tonic-gate 
132d2443e76Smarks #define	POSIXMODES	07777
133d2443e76Smarks 
1347c478bd9Sstevel@tonic-gate static char	aclchar = ' ';
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate static struct Lnk *add_lnk(struct Lnk **);
1377c478bd9Sstevel@tonic-gate static int bfill(void);
1387c478bd9Sstevel@tonic-gate static void bflush(void);
1397c478bd9Sstevel@tonic-gate static int chgreel(int dir);
1407c478bd9Sstevel@tonic-gate static int ckname(int);
1417c478bd9Sstevel@tonic-gate static void ckopts(long mask);
1427c478bd9Sstevel@tonic-gate static long cksum(char hdr, int byt_cnt, int *err);
1437c478bd9Sstevel@tonic-gate static int creat_hdr(void);
1447c478bd9Sstevel@tonic-gate static int creat_lnk(int dirfd, char *name1_p, char *name2_p);
1457c478bd9Sstevel@tonic-gate static int creat_spec(int dirfd);
1467c478bd9Sstevel@tonic-gate static int creat_tmp(char *nam_p);
1477c478bd9Sstevel@tonic-gate static void data_in(int proc_mode);
1487c478bd9Sstevel@tonic-gate static void data_out(void);
1497c478bd9Sstevel@tonic-gate static void data_pass(void);
1507c478bd9Sstevel@tonic-gate static void file_in(void);
1517c478bd9Sstevel@tonic-gate static int file_out(void);
1527c478bd9Sstevel@tonic-gate static int file_pass(void);
1537c478bd9Sstevel@tonic-gate static void flush_lnks(void);
1547c478bd9Sstevel@tonic-gate static int gethdr(void);
1557c478bd9Sstevel@tonic-gate static int getname(void);
1567c478bd9Sstevel@tonic-gate static void getpats(int largc, char **largv);
1577c478bd9Sstevel@tonic-gate static void ioerror(int dir);
1587c478bd9Sstevel@tonic-gate static int matched(void);
1597c478bd9Sstevel@tonic-gate static int missdir(char *nam_p);
1607c478bd9Sstevel@tonic-gate static long mklong(short v[]);
1617c478bd9Sstevel@tonic-gate static void mkshort(short sval[], long v);
1627c478bd9Sstevel@tonic-gate static int openout(int dirfd);
1637c478bd9Sstevel@tonic-gate static int read_hdr(int hdr);
1647c478bd9Sstevel@tonic-gate static void reclaim(struct Lnk *l_p);
1657c478bd9Sstevel@tonic-gate static void rstbuf(void);
1667c478bd9Sstevel@tonic-gate static void setpasswd(char *nam);
1677c478bd9Sstevel@tonic-gate static void rstfiles(int over, int dirfd);
1687c478bd9Sstevel@tonic-gate static void scan4trail(void);
1697c478bd9Sstevel@tonic-gate static void setup(int largc, char **largv);
1707c478bd9Sstevel@tonic-gate static void set_tym(int dirfd, char *nam_p, time_t atime, time_t mtime);
1717c478bd9Sstevel@tonic-gate static void sigint(int sig);
1727c478bd9Sstevel@tonic-gate static void swap(char *buf_p, int cnt);
1737c478bd9Sstevel@tonic-gate static void usage(void);
1747c478bd9Sstevel@tonic-gate static void verbose(char *nam_p);
1755fbb8099SNobutomo Nakano static void write_hdr(int arcflag, off_t len);
1767c478bd9Sstevel@tonic-gate static void write_trail(void);
1777c478bd9Sstevel@tonic-gate static int ustar_dir(void);
1787c478bd9Sstevel@tonic-gate static int ustar_spec(void);
1797c478bd9Sstevel@tonic-gate static struct stat *convert_to_old_stat(struct stat *, char *, char *);
1807c478bd9Sstevel@tonic-gate static void read_bar_vol_hdr(void);
1817c478bd9Sstevel@tonic-gate static void read_bar_file_hdr(void);
1827c478bd9Sstevel@tonic-gate static void setup_uncompress(FILE **);
1837c478bd9Sstevel@tonic-gate static void skip_bar_volhdr(void);
1847c478bd9Sstevel@tonic-gate static void bar_file_in(void);
1857c478bd9Sstevel@tonic-gate static int g_init(int *devtype, int *fdes);
1867c478bd9Sstevel@tonic-gate static int g_read(int, int, char *, unsigned);
1877c478bd9Sstevel@tonic-gate static int g_write(int, int, char *, unsigned);
1887c478bd9Sstevel@tonic-gate static int is_floppy(int);
1897c478bd9Sstevel@tonic-gate static int is_tape(int);
1905fbb8099SNobutomo Nakano static void write_ancillary(char *buf, size_t len, boolean_t padding);
1917c478bd9Sstevel@tonic-gate static int remove_dir(char *);
1927c478bd9Sstevel@tonic-gate static int save_cwd(void);
1937c478bd9Sstevel@tonic-gate static void rest_cwd(int cwd);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate static void xattrs_out(int (*func)());
1967c478bd9Sstevel@tonic-gate static void get_parent(char *path, char *dir);
1977c478bd9Sstevel@tonic-gate static void prepare_xattr_hdr(char **attrbuf, char *filename,
1987c478bd9Sstevel@tonic-gate     char *attrname, char typeflag, struct Lnk *linkinfo, int *rlen);
1997c478bd9Sstevel@tonic-gate static char tartype(int type);
2007c478bd9Sstevel@tonic-gate static int openfile(int omode);
2017c478bd9Sstevel@tonic-gate static mode_t attrmode(char type);
2027c478bd9Sstevel@tonic-gate static char *get_component(char *path);
2037c478bd9Sstevel@tonic-gate static int open_dir(char *name);
2047c478bd9Sstevel@tonic-gate static int open_dirfd();
2057c478bd9Sstevel@tonic-gate static void close_dirfd();
2067c478bd9Sstevel@tonic-gate static void write_xattr_hdr();
2077c478bd9Sstevel@tonic-gate static char *skipslashes(char *string, char *start);
2087c478bd9Sstevel@tonic-gate static int read_xattr_hdr();
2097c478bd9Sstevel@tonic-gate static void chop_endslashes(char *path);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /* helpful types */
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate static
2157c478bd9Sstevel@tonic-gate struct passwd	*Curpw_p,	/* Current password entry for -t option */
2167c478bd9Sstevel@tonic-gate 		*Rpw_p,		/* Password entry for -R option */
2177c478bd9Sstevel@tonic-gate 		*dpasswd;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate static
2207c478bd9Sstevel@tonic-gate struct group	*Curgr_p,	/* Current group entry for -t option */
2217c478bd9Sstevel@tonic-gate 		*dgroup;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate /* Data structure for buffered I/O. */
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate static
2267c478bd9Sstevel@tonic-gate struct buf_info {
2277c478bd9Sstevel@tonic-gate 	char	*b_base_p,	/* Pointer to base of buffer */
2287c478bd9Sstevel@tonic-gate 		*b_out_p,	/* Position to take bytes from buffer at */
2297c478bd9Sstevel@tonic-gate 		*b_in_p,	/* Position to put bytes into buffer at */
2307c478bd9Sstevel@tonic-gate 		*b_end_p;	/* Pointer to end of buffer */
2317c478bd9Sstevel@tonic-gate 	long	b_cnt,		/* Count of unprocessed bytes */
2327c478bd9Sstevel@tonic-gate 		b_size;		/* Size of buffer in bytes */
2337c478bd9Sstevel@tonic-gate } Buffr;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate /* Generic header format */
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate static
2387c478bd9Sstevel@tonic-gate struct gen_hdr {
2397c478bd9Sstevel@tonic-gate 	ulong_t	g_magic,	/* Magic number field */
2407c478bd9Sstevel@tonic-gate 		g_ino,		/* Inode number of file */
2417c478bd9Sstevel@tonic-gate 		g_mode,		/* Mode of file */
2427c478bd9Sstevel@tonic-gate 		g_uid,		/* Uid of file */
2437c478bd9Sstevel@tonic-gate 		g_gid,		/* Gid of file */
2447c478bd9Sstevel@tonic-gate 		g_nlink,	/* Number of links */
2457c478bd9Sstevel@tonic-gate 		g_mtime;	/* Modification time */
2467c478bd9Sstevel@tonic-gate 	off_t	g_filesz;	/* Length of file */
2477c478bd9Sstevel@tonic-gate 	ulong_t	g_dev,		/* File system of file */
2487c478bd9Sstevel@tonic-gate 		g_rdev,		/* Major/minor numbers of special files */
2497c478bd9Sstevel@tonic-gate 		g_namesz,	/* Length of filename */
2507c478bd9Sstevel@tonic-gate 		g_cksum;	/* Checksum of file */
2517c478bd9Sstevel@tonic-gate 	char	g_gname[32],
2527c478bd9Sstevel@tonic-gate 		g_uname[32],
2537c478bd9Sstevel@tonic-gate 		g_version[2],
2547c478bd9Sstevel@tonic-gate 		g_tmagic[6],
2557c478bd9Sstevel@tonic-gate 		g_typeflag;
2567c478bd9Sstevel@tonic-gate 	char	*g_tname,
2577c478bd9Sstevel@tonic-gate 		*g_prefix,
2587c478bd9Sstevel@tonic-gate 		*g_nam_p,	/* Filename */
259ced83f9bSceastha 		*g_attrparent_p, /* attribute parent */
260ced83f9bSceastha 		*g_attrpath_p, /* attribute path */
2617c478bd9Sstevel@tonic-gate 		*g_attrnam_p,	/* attribute */
2627c478bd9Sstevel@tonic-gate 		*g_attrfnam_p,  /* Real file name attr belongs to */
2637c478bd9Sstevel@tonic-gate 		*g_linktoattrfnam_p, /* file linked attribute belongs to */
2647c478bd9Sstevel@tonic-gate 		*g_linktoattrnam_p,  /* attribute g_attrnam_p is linked to */
2657c478bd9Sstevel@tonic-gate 		*g_dirpath;	/* dirname currently opened */
2667c478bd9Sstevel@tonic-gate 	int	g_dirfd;	/* directory file descriptor */
2677c478bd9Sstevel@tonic-gate 	int	g_passdirfd;	/* directory fd to pass to */
268ced83f9bSceastha 	int	g_rw_sysattr;	/* read-write system attribute */
269ced83f9bSceastha 	int	g_baseparent_fd;	/* base file's parent fd */
2705fbb8099SNobutomo Nakano 	holes_info_t *g_holes;	/* sparse file information */
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate } Gen, *G_p;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate /* Data structure for handling multiply-linked files */
2757c478bd9Sstevel@tonic-gate static
2767c478bd9Sstevel@tonic-gate char	prebuf[PRESIZ+1],
2777c478bd9Sstevel@tonic-gate 	nambuf[NAMSIZ+1],
2787c478bd9Sstevel@tonic-gate 	fullnam[MAXNAM+1];
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate static
2827c478bd9Sstevel@tonic-gate struct Lnk {
2837c478bd9Sstevel@tonic-gate 	short	L_cnt,		/* Number of links encountered */
2847c478bd9Sstevel@tonic-gate 		L_data;		/* Data has been encountered if 1 */
2857c478bd9Sstevel@tonic-gate 	struct gen_hdr	L_gen;	/* gen_hdr information for this file */
2867c478bd9Sstevel@tonic-gate 	struct Lnk	*L_nxt_p,	/* Next file in list */
2877c478bd9Sstevel@tonic-gate 			*L_bck_p,	/* Previous file in list */
2887c478bd9Sstevel@tonic-gate 			*L_lnk_p;	/* Next link for this file */
2897c478bd9Sstevel@tonic-gate } Lnk_hd;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate static
2927c478bd9Sstevel@tonic-gate struct hdr_cpio	Hdr;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate /*
2957c478bd9Sstevel@tonic-gate  * -------------------------------------------------------------------------
2967c478bd9Sstevel@tonic-gate  *		   Stuff needed to pre-view the name stream
2977c478bd9Sstevel@tonic-gate  *
2987c478bd9Sstevel@tonic-gate  * issymlink is used to remember that the current file is a symlink between
2997c478bd9Sstevel@tonic-gate  * getname() and file_pass(); the former trashes this information immediately
3007c478bd9Sstevel@tonic-gate  * when -L is specified.
3017c478bd9Sstevel@tonic-gate  */
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate static
3047c478bd9Sstevel@tonic-gate int	issymlink = 0;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate static
3077c478bd9Sstevel@tonic-gate FILE	*In_p = stdin;		/* Where the input comes from */
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate typedef struct sl_info
3107c478bd9Sstevel@tonic-gate {
3117c478bd9Sstevel@tonic-gate 	struct sl_info *llink;	/* Left subtree ptr (tree depth in *sl_head) */
3127c478bd9Sstevel@tonic-gate 	struct sl_info *rlink;	/* Right subtree ptr */
3137c478bd9Sstevel@tonic-gate 	int bal;		/* Subtree balance factor */
3147c478bd9Sstevel@tonic-gate 	ulong_t	sl_count;	/* Number of symlinks */
315ced83f9bSceastha 	int	sl_ftype;	/* file type of inode */
3167c478bd9Sstevel@tonic-gate 	ino_t	sl_ino;		/* Inode of file */
3177c478bd9Sstevel@tonic-gate 	ino_t	sl_ino2;	/* alternate inode for -Hodc */
3187c478bd9Sstevel@tonic-gate } sl_info_t;
3197c478bd9Sstevel@tonic-gate 
320ced83f9bSceastha typedef struct data_in
321ced83f9bSceastha {
3225fbb8099SNobutomo Nakano 	int		data_in_errno;
3234e621e6cSNobutomo Nakano 	char		data_in_swapfile;
3244e621e6cSNobutomo Nakano 	char		data_in_proc_mode;
3254e621e6cSNobutomo Nakano 	char		data_in_rd_eof;
3264e621e6cSNobutomo Nakano 	char		data_in_wr_part;
3274e621e6cSNobutomo Nakano 	char		data_in_compress_flag;
328ced83f9bSceastha 	long		data_in_cksumval;
329ced83f9bSceastha 	FILE		*data_in_pipef;
330ced83f9bSceastha } data_in_t;
331ced83f9bSceastha 
3327c478bd9Sstevel@tonic-gate /*
3337c478bd9Sstevel@tonic-gate  * The following structure maintains a hash entry for the
3347c478bd9Sstevel@tonic-gate  * balancing trees which are allocated for each device nodes.
3357c478bd9Sstevel@tonic-gate  */
3367c478bd9Sstevel@tonic-gate typedef struct sl_info_link
3377c478bd9Sstevel@tonic-gate {
3387c478bd9Sstevel@tonic-gate 	dev_t		dev;
3397c478bd9Sstevel@tonic-gate 	sl_info_t	*head;
3407c478bd9Sstevel@tonic-gate 	struct sl_info_link *next;
3417c478bd9Sstevel@tonic-gate } sl_info_link_t;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate #define	SL_INFO_ALLOC_CHUNK	1024
3447c478bd9Sstevel@tonic-gate #define	NDEVHENTRY		0x40
3457c478bd9Sstevel@tonic-gate #define	DEV_HASHKEY(x)		((x) & (NDEVHENTRY -1))
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate /*
3487c478bd9Sstevel@tonic-gate  * For remapping dev,inode for -Hodc archives.
3497c478bd9Sstevel@tonic-gate  */
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate typedef struct sl_remap
3527c478bd9Sstevel@tonic-gate {
3537c478bd9Sstevel@tonic-gate 	dev_t			dev;		/* device */
3547c478bd9Sstevel@tonic-gate 	int			inode_count;	/* # inodes seen on dev */
3557c478bd9Sstevel@tonic-gate 	struct sl_remap 	*next;		/* next in the chain */
3567c478bd9Sstevel@tonic-gate } sl_remap_t;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate /* forward declarations */
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate static sl_info_t 	*sl_info_alloc(void);
361ced83f9bSceastha static sl_info_t 	*sl_insert(dev_t, ino_t, int);
362ced83f9bSceastha static ulong_t		sl_numlinks(dev_t, ino_t, int);
3637c478bd9Sstevel@tonic-gate static void		sl_preview_synonyms(void);
364ced83f9bSceastha static void		sl_remember_tgt(const struct stat *, int, int);
365ced83f9bSceastha static sl_info_t 	*sl_search(dev_t, ino_t, int);
3667c478bd9Sstevel@tonic-gate static sl_info_t	*sl_devhash_lookup(dev_t);
3677c478bd9Sstevel@tonic-gate static void		sl_devhash_insert(dev_t, sl_info_t *);
3687c478bd9Sstevel@tonic-gate 
369ced83f9bSceastha extern int		sl_compare(ino_t, int, ino_t, int);
370ced83f9bSceastha #define	sl_compare(lino, lftype, rino, rftype)	(lino < rino ? -1 : \
371ced83f9bSceastha 	    (lino > rino ? 1 : (lftype < rftype ? -1 : \
372ced83f9bSceastha 	    (lftype > rftype ? 1 : 0))))
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate /* global storage */
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate static sl_remap_t  *sl_remap_head = NULL; /* head of the inode-remap list */
3777c478bd9Sstevel@tonic-gate static sl_info_link_t	*sl_devhash[NDEVHENTRY]; /* hash table */
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate /*
3807c478bd9Sstevel@tonic-gate  * -------------------------------------------------------------------------
3817c478bd9Sstevel@tonic-gate  */
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate static
3847c478bd9Sstevel@tonic-gate struct stat	ArchSt,	/* stat(2) information of the archive */
3857c478bd9Sstevel@tonic-gate 		SrcSt,	/* stat(2) information of source file */
3867c478bd9Sstevel@tonic-gate 		DesSt,	/* stat(2) of destination file */
3877c478bd9Sstevel@tonic-gate 		*OldSt = NULL;	/* stat info converted to svr32 format */
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate /*
3907c478bd9Sstevel@tonic-gate  * bin_mag: Used to validate a binary magic number,
3917c478bd9Sstevel@tonic-gate  * by combining to bytes into an unsigned short.
3927c478bd9Sstevel@tonic-gate  */
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate static
3957c478bd9Sstevel@tonic-gate union bin_mag {
3967c478bd9Sstevel@tonic-gate 	unsigned char b_byte[2];
3977c478bd9Sstevel@tonic-gate 	ushort_t b_half;
3987c478bd9Sstevel@tonic-gate } Binmag;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate static
4017c478bd9Sstevel@tonic-gate union tblock *Thdr_p;	/* TAR header pointer */
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate static union b_block *bar_Vhdr;
4047c478bd9Sstevel@tonic-gate static struct gen_hdr Gen_bar_vol;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate /*
4077c478bd9Sstevel@tonic-gate  * swpbuf: Used in swap() to swap bytes within a halfword,
4087c478bd9Sstevel@tonic-gate  * halfwords within a word, or to reverse the order of the
4097c478bd9Sstevel@tonic-gate  * bytes within a word.  Also used in mklong() and mkshort().
4107c478bd9Sstevel@tonic-gate  */
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate static
4137c478bd9Sstevel@tonic-gate union swpbuf {
4147c478bd9Sstevel@tonic-gate 	unsigned char	s_byte[4];
4157c478bd9Sstevel@tonic-gate 	ushort_t	s_half[2];
4167c478bd9Sstevel@tonic-gate 	ulong_t	s_word;
4177c478bd9Sstevel@tonic-gate } *Swp_p;
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate static
420ced83f9bSceastha char	*myname,		/* program name */
421ced83f9bSceastha 	Adir,			/* Flags object as a directory */
4227c478bd9Sstevel@tonic-gate 	Hiddendir,		/* Processing hidden attribute directory */
4237c478bd9Sstevel@tonic-gate 	Aspec,			/* Flags object as a special file */
4247c478bd9Sstevel@tonic-gate 	Do_rename,		/* Indicates rename() is to be used */
4257c478bd9Sstevel@tonic-gate 	Time[50],		/* Array to hold date and time */
4267c478bd9Sstevel@tonic-gate 	Ttyname[] = "/dev/tty",	/* Controlling console */
4277c478bd9Sstevel@tonic-gate 	T_lname[MAXPATHLEN],	/* Array to hold links name for tar */
4287c478bd9Sstevel@tonic-gate 	*Buf_p,			/* Buffer for file system I/O */
4297c478bd9Sstevel@tonic-gate 	*Full_p,		/* Pointer to full pathname */
4307c478bd9Sstevel@tonic-gate 	*Efil_p,		/* -E pattern file string */
4317c478bd9Sstevel@tonic-gate 	*Eom_p = "Change to part %d and press RETURN key. [q] ",
4327c478bd9Sstevel@tonic-gate 	*Fullnam_p,		/* Full pathname */
4337c478bd9Sstevel@tonic-gate 	*Attrfile_p,		/* attribute file */
4347c478bd9Sstevel@tonic-gate 	*Hdr_p,			/* -H header type string */
4357c478bd9Sstevel@tonic-gate 	*IOfil_p,		/* -I/-O input/output archive string */
4367c478bd9Sstevel@tonic-gate 	*Lnkend_p,		/* Pointer to end of Lnknam_p */
4377c478bd9Sstevel@tonic-gate 	*Lnknam_p,		/* Buffer for linking files with -p option */
4387c478bd9Sstevel@tonic-gate 	*Nam_p,			/* Array to hold filename */
4397c478bd9Sstevel@tonic-gate 	*Savenam_p,		/* copy of filename xattr belongs to */
4407c478bd9Sstevel@tonic-gate 	*Own_p,			/* New owner login id string */
4417c478bd9Sstevel@tonic-gate 	*Renam_p,		/* Buffer for renaming files */
442ced83f9bSceastha 	*Renam_attr_p,		/* Buffer for renaming attr with sys attrs */
4437c478bd9Sstevel@tonic-gate 	*Renametmp_p,		/* Tmp Buffer for renaming files */
4447c478bd9Sstevel@tonic-gate 	*Symlnk_p,		/* Buffer for holding symbolic link name */
4457c478bd9Sstevel@tonic-gate 	*Over_p,		/* Holds temporary filename when overwriting */
4467c478bd9Sstevel@tonic-gate 	**Pat_pp = 0,		/* Pattern strings */
4477c478bd9Sstevel@tonic-gate 	bar_linkflag,		/* flag to indicate if the file is a link */
4487c478bd9Sstevel@tonic-gate 	bar_linkname[MAXPATHLEN]; /* store the name of the link */
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate static
4517c478bd9Sstevel@tonic-gate int	Append = 0,	/* Flag set while searching to end of archive */
4527c478bd9Sstevel@tonic-gate 	Archive,	/* File descriptor of the archive */
453da6c28aaSamw 	Buf_error = 0,	/* I/O error occurred during buffer fill */
4545fbb8099SNobutomo Nakano 	Compress_sparse = 0,	/* Compress sparse files */
4557c478bd9Sstevel@tonic-gate 	Def_mode = 0777,	/* Default file/directory protection modes */
4567c478bd9Sstevel@tonic-gate 	Device,		/* Device type being accessed (used with libgenIO) */
4577c478bd9Sstevel@tonic-gate 	Error_cnt = 0,	/* Cumulative count of I/O errors */
4587c478bd9Sstevel@tonic-gate 	Finished = 1,	/* Indicates that a file transfer has completed */
4597c478bd9Sstevel@tonic-gate 	Hdrsz = ASCSZ,	/* Fixed length portion of the header */
4607c478bd9Sstevel@tonic-gate 	Hdr_type,		/* Flag to indicate type of header selected */
4617c478bd9Sstevel@tonic-gate 	Ifile,		/* File des. of file being archived */
4627c478bd9Sstevel@tonic-gate 	Ofile,		/* File des. of file being extracted from archive */
4637c478bd9Sstevel@tonic-gate 	Use_old_stat = 0,    /* Create an old style -Hodc hdr (small dev's) */
4647c478bd9Sstevel@tonic-gate 	Onecopy = 0,	/* Flags old vs. new link handling */
4657c478bd9Sstevel@tonic-gate 	Pad_val = 0,	/* Indicates the number of bytes to pad (if any) */
4667c478bd9Sstevel@tonic-gate 	PageSize = 0,	/* The native page size, used for figuring block size */
4677c478bd9Sstevel@tonic-gate 	Volcnt = 1,	/* Number of archive volumes processed */
4687c478bd9Sstevel@tonic-gate 	Verbcnt = 0,	/* Count of number of dots '.' output */
4697c478bd9Sstevel@tonic-gate 	Eomflag = 0,
4707c478bd9Sstevel@tonic-gate 	Dflag = 0,
4717c478bd9Sstevel@tonic-gate 	Atflag = 0,	/* Archive/restore extended attributes */
472ced83f9bSceastha 	SysAtflag = 0,	/* Archive/restore extended system attributes */
4737c478bd9Sstevel@tonic-gate 	Compressed,	/* Flag to indicate if the bar archive is compressed */
474647ab8f4Sceastha 	Bar_vol_num = 0, /* Volume number count for bar archive */
475ced83f9bSceastha 	privileged = 0,	/* Flag set if running with higher privileges */
476ced83f9bSceastha 	attr_baseparent_fd = -1;	/* attribute's base file descriptor */
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate static
480f48205beScasper gid_t	Lastgid = (gid_t)-1;	/* Used with -t & -v to record current gid */
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate static
483f48205beScasper uid_t	Lastuid = (uid_t)-1;	/* Used with -t & -v to record current uid */
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate static
4867c478bd9Sstevel@tonic-gate long	Args,		/* Mask of selected options */
4877c478bd9Sstevel@tonic-gate 	Max_namesz = CPATH;	/* Maximum size of pathnames/filenames */
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate static
4907c478bd9Sstevel@tonic-gate int	Bufsize = BUFSZ;	/* Default block size */
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate static u_longlong_t    Blocks;	/* full blocks transferred */
4947c478bd9Sstevel@tonic-gate static u_longlong_t    SBlocks;	/* cumulative char count from short reads */
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate static off_t	Max_offset = BIN_OFFSET_MAX;	/* largest file size */
4987c478bd9Sstevel@tonic-gate static off_t	Max_filesz;			/* from getrlimit */
4997c478bd9Sstevel@tonic-gate 
500ced83f9bSceastha static ulong_t	Savedev;
501ced83f9bSceastha 
5027c478bd9Sstevel@tonic-gate static
5037c478bd9Sstevel@tonic-gate FILE	*Ef_p,			/* File pointer of pattern input file */
5047c478bd9Sstevel@tonic-gate 	*Err_p = stderr,	/* File pointer for error reporting */
5057c478bd9Sstevel@tonic-gate 	*Out_p = stdout,	/* File pointer for non-archive output */
5067c478bd9Sstevel@tonic-gate 	*Rtty_p,		/* Input file pointer for interactive rename */
5077c478bd9Sstevel@tonic-gate 	*Wtty_p;		/* Output file ptr for interactive rename */
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate static
5107c478bd9Sstevel@tonic-gate ushort_t	Ftype = S_IFMT;	/* File type mask */
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate /* ACL support */
5137c478bd9Sstevel@tonic-gate static struct sec_attr {
5147c478bd9Sstevel@tonic-gate 	char	attr_type;
5157c478bd9Sstevel@tonic-gate 	char	attr_len[7];
5167c478bd9Sstevel@tonic-gate 	char	attr_info[1];
5177c478bd9Sstevel@tonic-gate } *attr;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate static int	Pflag = 0;	/* flag indicates that acl is preserved */
520fa9e4066Sahrens static int	acl_is_set = 0; /* True if an acl was set on the file */
521fa9e4066Sahrens 
522fa9e4066Sahrens acl_t *aclp;
5237c478bd9Sstevel@tonic-gate 
524ced83f9bSceastha #if defined(O_XATTR)
525ced83f9bSceastha typedef enum {
526ced83f9bSceastha 	ATTR_OK,
527ced83f9bSceastha 	ATTR_SKIP,
528ced83f9bSceastha 	ATTR_CHDIR_ERR,
529ced83f9bSceastha 	ATTR_OPEN_ERR,
530ced83f9bSceastha 	ATTR_XATTR_ERR,
531ced83f9bSceastha 	ATTR_SATTR_ERR
532ced83f9bSceastha } attr_status_t;
533ced83f9bSceastha #endif
534ced83f9bSceastha 
535ced83f9bSceastha #if defined(O_XATTR)
536ced83f9bSceastha typedef enum {
537ced83f9bSceastha 	ARC_CREATE,
538ced83f9bSceastha 	ARC_RESTORE
539ced83f9bSceastha } arc_action_t;
540ced83f9bSceastha #endif
541ced83f9bSceastha 
542ced83f9bSceastha 
5437c478bd9Sstevel@tonic-gate /*
5447c478bd9Sstevel@tonic-gate  *
5457c478bd9Sstevel@tonic-gate  * cpio has been changed to support extended attributes.
5467c478bd9Sstevel@tonic-gate  *
5477c478bd9Sstevel@tonic-gate  * As part of this change cpio has been changed to use the new *at() syscalls
5487c478bd9Sstevel@tonic-gate  * such as openat, fchownat(), unlinkat()...
5497c478bd9Sstevel@tonic-gate  *
5507c478bd9Sstevel@tonic-gate  * This was done so that attributes can be handled with as few code changes
5517c478bd9Sstevel@tonic-gate  * as possible.
5527c478bd9Sstevel@tonic-gate  *
5537c478bd9Sstevel@tonic-gate  * What this means is that cpio now opens the directory that a file or directory
5547c478bd9Sstevel@tonic-gate  * resides in and then performs *at() functions to manipulate the entry.
5557c478bd9Sstevel@tonic-gate  *
5567c478bd9Sstevel@tonic-gate  * For example a new file is now created like this:
5577c478bd9Sstevel@tonic-gate  *
5587c478bd9Sstevel@tonic-gate  * dfd = open(<some dir path>)
5597c478bd9Sstevel@tonic-gate  * fd = openat(dfd, <name>,....);
5607c478bd9Sstevel@tonic-gate  *
5617c478bd9Sstevel@tonic-gate  * or in the case of an extended attribute
5627c478bd9Sstevel@tonic-gate  *
5637c478bd9Sstevel@tonic-gate  * dfd = attropen(<pathname>, ".", ....)
5647c478bd9Sstevel@tonic-gate  *
5657c478bd9Sstevel@tonic-gate  * Once we have a directory file descriptor all of the *at() functions can
5667c478bd9Sstevel@tonic-gate  * be applied to it.
5677c478bd9Sstevel@tonic-gate  *
5687c478bd9Sstevel@tonic-gate  * unlinkat(dfd, <component name>,...)
5697c478bd9Sstevel@tonic-gate  * fchownat(dfd, <component name>,..)
5707c478bd9Sstevel@tonic-gate  *
5717c478bd9Sstevel@tonic-gate  * This works for both normal namespace files and extended attribute file
5727c478bd9Sstevel@tonic-gate  *
5737c478bd9Sstevel@tonic-gate  */
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate /*
5767c478bd9Sstevel@tonic-gate  * Extended attribute layout
5777c478bd9Sstevel@tonic-gate  *
5787c478bd9Sstevel@tonic-gate  * Extended attributes are stored in two pieces.
5797c478bd9Sstevel@tonic-gate  * 1. An attribute header which has information about
5807c478bd9Sstevel@tonic-gate  *    what file the attribute is for and what the attribute
5817c478bd9Sstevel@tonic-gate  *    is named.
5827c478bd9Sstevel@tonic-gate  * 2. The attribute record itself.  Stored as a normal file type
5837c478bd9Sstevel@tonic-gate  *    of entry.
5847c478bd9Sstevel@tonic-gate  * Both the header and attribute record have special modes/typeflags
5857c478bd9Sstevel@tonic-gate  * associated with them.
5867c478bd9Sstevel@tonic-gate  *
5877c478bd9Sstevel@tonic-gate  * The names of the header in the archive look like:
5887c478bd9Sstevel@tonic-gate  * /dev/null/attr.hdr
5897c478bd9Sstevel@tonic-gate  *
5907c478bd9Sstevel@tonic-gate  * The name of the attribute looks like:
5917c478bd9Sstevel@tonic-gate  * /dev/null/attr.
5927c478bd9Sstevel@tonic-gate  *
5937c478bd9Sstevel@tonic-gate  * This is done so that an archiver that doesn't understand these formats
5947c478bd9Sstevel@tonic-gate  * can just dispose of the attribute records unless the user chooses to
5957c478bd9Sstevel@tonic-gate  * rename them via cpio -r or pax -i
5967c478bd9Sstevel@tonic-gate  *
5977c478bd9Sstevel@tonic-gate  * The format is composed of a fixed size header followed
5987c478bd9Sstevel@tonic-gate  * by a variable sized xattr_buf. If the attribute is a hard link
5997c478bd9Sstevel@tonic-gate  * to another attribute, then another xattr_buf section is included
6007c478bd9Sstevel@tonic-gate  * for the link.
6017c478bd9Sstevel@tonic-gate  *
6027c478bd9Sstevel@tonic-gate  * The xattr_buf is used to define the necessary "pathing" steps
6037c478bd9Sstevel@tonic-gate  * to get to the extended attribute.  This is necessary to support
6047c478bd9Sstevel@tonic-gate  * a fully recursive attribute model where an attribute may itself
6057c478bd9Sstevel@tonic-gate  * have an attribute.
6067c478bd9Sstevel@tonic-gate  *
6077c478bd9Sstevel@tonic-gate  * The basic layout looks like this.
6087c478bd9Sstevel@tonic-gate  *
6097c478bd9Sstevel@tonic-gate  *     --------------------------------
6107c478bd9Sstevel@tonic-gate  *     |                              |
6117c478bd9Sstevel@tonic-gate  *     |         xattr_hdr            |
6127c478bd9Sstevel@tonic-gate  *     |                              |
6137c478bd9Sstevel@tonic-gate  *     --------------------------------
6147c478bd9Sstevel@tonic-gate  *     --------------------------------
6157c478bd9Sstevel@tonic-gate  *     |                              |
6167c478bd9Sstevel@tonic-gate  *     |        xattr_buf             |
6177c478bd9Sstevel@tonic-gate  *     |                              |
6187c478bd9Sstevel@tonic-gate  *     --------------------------------
6197c478bd9Sstevel@tonic-gate  *     --------------------------------
6207c478bd9Sstevel@tonic-gate  *     |                              |
6217c478bd9Sstevel@tonic-gate  *     |      (optional link info)    |
6227c478bd9Sstevel@tonic-gate  *     |                              |
6237c478bd9Sstevel@tonic-gate  *     --------------------------------
6247c478bd9Sstevel@tonic-gate  *     --------------------------------
6257c478bd9Sstevel@tonic-gate  *     |                              |
6267c478bd9Sstevel@tonic-gate  *     |      attribute itself        |
6277c478bd9Sstevel@tonic-gate  *     |      stored as normal tar    |
6287c478bd9Sstevel@tonic-gate  *     |      or cpio data with       |
6297c478bd9Sstevel@tonic-gate  *     |      special mode or         |
6307c478bd9Sstevel@tonic-gate  *     |      typeflag                |
6317c478bd9Sstevel@tonic-gate  *     |                              |
6327c478bd9Sstevel@tonic-gate  *     --------------------------------
6337c478bd9Sstevel@tonic-gate  *
6347c478bd9Sstevel@tonic-gate  */
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate /*
6377c478bd9Sstevel@tonic-gate  * Extended attributes structures
6387c478bd9Sstevel@tonic-gate  *
6397c478bd9Sstevel@tonic-gate  * xattrhead is the complete extended attribute header, as read of off
6407c478bd9Sstevel@tonic-gate  * disk/tape. It includes the variable xattr_buf portion.
6417c478bd9Sstevel@tonic-gate  *
6427c478bd9Sstevel@tonic-gate  * xattrp is basically an offset into xattrhead that points to the
6437c478bd9Sstevel@tonic-gate  * "pathing" section which defines how to get to the attribute.
6447c478bd9Sstevel@tonic-gate  *
6457c478bd9Sstevel@tonic-gate  * xattr_linkp is identical to xattrp except that it is used for linked
6467c478bd9Sstevel@tonic-gate  * attributes.  It provides the pathing steps to get to the linked
6477c478bd9Sstevel@tonic-gate  * attribute.
6487c478bd9Sstevel@tonic-gate  *
6497c478bd9Sstevel@tonic-gate  * These structures are updated when an extended attribute header is read off
6507c478bd9Sstevel@tonic-gate  * of disk/tape.
6517c478bd9Sstevel@tonic-gate  */
6527c478bd9Sstevel@tonic-gate static struct xattr_hdr	*xattrhead;
6537c478bd9Sstevel@tonic-gate static struct xattr_buf	*xattrp;
6547c478bd9Sstevel@tonic-gate static struct xattr_buf	*xattr_linkp;
6557c478bd9Sstevel@tonic-gate static int 		xattrbadhead;	/* is extended attribute header bad? */
6567c478bd9Sstevel@tonic-gate 
657fa9e4066Sahrens static int	append_secattr(char **, int *, acl_t *);
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate /*
6607c478bd9Sstevel@tonic-gate  * Note regarding cpio and changes to ensure cpio doesn't try to second
6617c478bd9Sstevel@tonic-gate  * guess whether it runs with sufficient privileges or not:
6627c478bd9Sstevel@tonic-gate  *
6637c478bd9Sstevel@tonic-gate  * cpio has been changed so that it doesn't carry a second implementation of
6647c478bd9Sstevel@tonic-gate  * the kernel's policy with respect to privileges.  Instead of attempting
6657c478bd9Sstevel@tonic-gate  * to restore uid and gid from an archive only if cpio is run as uid 0,
666647ab8f4Sceastha  * cpio now *always* tries to restore the uid and gid from the archive
667647ab8f4Sceastha  * except when the -R option is specified.  When the -R is specified,
668647ab8f4Sceastha  * the uid and gid of the restored file will be changed to those of the
669647ab8f4Sceastha  * login id specified.  In addition, chown(), set_tym(), and chmod() should
670647ab8f4Sceastha  * only be executed once during archive extraction, and to ensure
671647ab8f4Sceastha  * setuid/setgid bits are restored properly, chown() should always be
672647ab8f4Sceastha  * executed before chmod().
6737c478bd9Sstevel@tonic-gate  *
6747c478bd9Sstevel@tonic-gate  * Note regarding debugging mechanism for cpio:
6757c478bd9Sstevel@tonic-gate  *
6767c478bd9Sstevel@tonic-gate  * The following mechanism is provided to allow us to debug cpio in complicated
6777c478bd9Sstevel@tonic-gate  * situations, like when it is part of a pipe.  The idea is that you compile
6787c478bd9Sstevel@tonic-gate  * with -DWAITAROUND defined, and then add the "-z" command line option to the
6797c478bd9Sstevel@tonic-gate  * target cpio invocation.  If stderr is available, it will tell you to which
6807c478bd9Sstevel@tonic-gate  * pid to attach the debugger; otherwise, use ps to find it.  Attach to the
6817c478bd9Sstevel@tonic-gate  * process from the debugger, and, *PRESTO*, you are there!
6827c478bd9Sstevel@tonic-gate  *
6837c478bd9Sstevel@tonic-gate  * Simply assign "waitaround = 0" once you attach to the process, and then
6847c478bd9Sstevel@tonic-gate  * proceed from there as usual.
6857c478bd9Sstevel@tonic-gate  */
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
6887c478bd9Sstevel@tonic-gate int waitaround = 0;		/* wait for rendezvous with the debugger */
6897c478bd9Sstevel@tonic-gate #endif
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate #define	EXIT_CODE	(Error_cnt > 255 ? 255 : Error_cnt)
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate /*
6947c478bd9Sstevel@tonic-gate  * main: Call setup() to process options and perform initializations,
6957c478bd9Sstevel@tonic-gate  * and then select either copy in (-i), copy out (-o), or pass (-p) action.
6967c478bd9Sstevel@tonic-gate  */
6977c478bd9Sstevel@tonic-gate 
698944d83a0Schin int
6997c478bd9Sstevel@tonic-gate main(int argc, char **argv)
7007c478bd9Sstevel@tonic-gate {
7017c478bd9Sstevel@tonic-gate 	int i;
7027c478bd9Sstevel@tonic-gate 	int passret;
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
7057c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
7067c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
7077c478bd9Sstevel@tonic-gate #endif
7087c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	(void) memset(&Gen, 0, sizeof (Gen));
711ced83f9bSceastha 	myname = e_strdup(E_EXIT, basename(argv[0]));
7127c478bd9Sstevel@tonic-gate 	setup(argc, argv);
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	if (signal(SIGINT, sigint) == SIG_IGN)
7157c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, SIG_IGN);
7167c478bd9Sstevel@tonic-gate 	switch (Args & (OCi | OCo | OCp)) {
7177c478bd9Sstevel@tonic-gate 	case OCi: /* COPY IN */
7187c478bd9Sstevel@tonic-gate 		Hdr_type = NONE;
719ced83f9bSceastha 		if (Atflag || SysAtflag) {
720ced83f9bSceastha 			/*
721ced83f9bSceastha 			 * Save the current working directory, so
722ced83f9bSceastha 			 * we can change back here after cd'ing into
723ced83f9bSceastha 			 * the attribute directory when processing
724ced83f9bSceastha 			 * attributes.
725ced83f9bSceastha 			 */
726ced83f9bSceastha 			if ((attr_baseparent_fd = save_cwd()) < 0) {
727ced83f9bSceastha 				msg(EXT, "Unable to open current directory.");
728ced83f9bSceastha 			}
729ced83f9bSceastha 		}
7307c478bd9Sstevel@tonic-gate 		while ((i = gethdr()) != 0) {
7317c478bd9Sstevel@tonic-gate 			Gen.g_dirfd = -1;
7327c478bd9Sstevel@tonic-gate 			if (i == 1) {
7337c478bd9Sstevel@tonic-gate 				file_in();
7347c478bd9Sstevel@tonic-gate 				/*
7357c478bd9Sstevel@tonic-gate 				 * Any ACL info for this file would or should
7367c478bd9Sstevel@tonic-gate 				 * have been used after file_in(); clear out
7377c478bd9Sstevel@tonic-gate 				 * aclp so it is is not erroneously used on
7387c478bd9Sstevel@tonic-gate 				 * the next file.
7397c478bd9Sstevel@tonic-gate 				 */
7407c478bd9Sstevel@tonic-gate 				if (aclp != NULL) {
741fa9e4066Sahrens 					acl_free(aclp);
7427c478bd9Sstevel@tonic-gate 					aclp = NULL;
7437c478bd9Sstevel@tonic-gate 				}
744fa9e4066Sahrens 				acl_is_set = 0;
7457c478bd9Sstevel@tonic-gate 			}
7467c478bd9Sstevel@tonic-gate 			(void) memset(&Gen, 0, sizeof (Gen));
7477c478bd9Sstevel@tonic-gate 		}
7487c478bd9Sstevel@tonic-gate 		/* Do not count "extra" "read-ahead" buffered data */
7497c478bd9Sstevel@tonic-gate 		if (Buffr.b_cnt > Bufsize)
7507c478bd9Sstevel@tonic-gate 			Blocks -=  (u_longlong_t)(Buffr.b_cnt / Bufsize);
7517c478bd9Sstevel@tonic-gate 		break;
7527c478bd9Sstevel@tonic-gate 	case OCo: /* COPY OUT */
7537c478bd9Sstevel@tonic-gate 		if (Args & OCA) {
7547c478bd9Sstevel@tonic-gate 			scan4trail();
7557c478bd9Sstevel@tonic-gate 		}
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 		Gen.g_dirfd = -1;
7587c478bd9Sstevel@tonic-gate 		Gen.g_dirpath = NULL;
7597c478bd9Sstevel@tonic-gate 		sl_preview_synonyms();
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 		while ((i = getname()) != 0) {
7627c478bd9Sstevel@tonic-gate 			if (i == 1) {
7637c478bd9Sstevel@tonic-gate 				(void) file_out();
764ced83f9bSceastha 				if (Atflag || SysAtflag) {
7657c478bd9Sstevel@tonic-gate 					if (Gen.g_dirfd != -1) {
7667c478bd9Sstevel@tonic-gate 						(void) close(Gen.g_dirfd);
7677c478bd9Sstevel@tonic-gate 					}
7687c478bd9Sstevel@tonic-gate 					Gen.g_dirfd = -1;
7697c478bd9Sstevel@tonic-gate 					xattrs_out(file_out);
7707c478bd9Sstevel@tonic-gate 				}
7717c478bd9Sstevel@tonic-gate 			}
7727c478bd9Sstevel@tonic-gate 			if (aclp != NULL) {
773fa9e4066Sahrens 				acl_free(aclp);
7747c478bd9Sstevel@tonic-gate 				aclp = NULL;
775fa9e4066Sahrens 				acl_is_set = 0;
7767c478bd9Sstevel@tonic-gate 			}
7777c478bd9Sstevel@tonic-gate 		}
7787c478bd9Sstevel@tonic-gate 		write_trail();
7797c478bd9Sstevel@tonic-gate 		break;
7807c478bd9Sstevel@tonic-gate 	case OCp: /* PASS */
7817c478bd9Sstevel@tonic-gate 		sl_preview_synonyms();
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 		Gen.g_dirfd = -1;
7847c478bd9Sstevel@tonic-gate 		Gen.g_passdirfd = -1;
7857c478bd9Sstevel@tonic-gate 		Gen.g_dirpath = NULL;
7865fbb8099SNobutomo Nakano 		Compress_sparse = 1;
7877c478bd9Sstevel@tonic-gate 		while (getname()) {
7887c478bd9Sstevel@tonic-gate 			/*
7897c478bd9Sstevel@tonic-gate 			 * If file is a fully qualified path then
7907c478bd9Sstevel@tonic-gate 			 * file_pass will strip off the leading '/'
7917c478bd9Sstevel@tonic-gate 			 * and we need to save off the unstripped
7927c478bd9Sstevel@tonic-gate 			 * name for attribute traversal.
7937c478bd9Sstevel@tonic-gate 			 */
794ced83f9bSceastha 			if (Atflag || SysAtflag) {
7957c478bd9Sstevel@tonic-gate 				(void) strcpy(Savenam_p, Gen.g_nam_p);
7967c478bd9Sstevel@tonic-gate 			}
7977c478bd9Sstevel@tonic-gate 			passret = file_pass();
7987c478bd9Sstevel@tonic-gate 			if (aclp != NULL) {
799fa9e4066Sahrens 				acl_free(aclp);
8007c478bd9Sstevel@tonic-gate 				aclp = NULL;
801fa9e4066Sahrens 				acl_is_set = 0;
8027c478bd9Sstevel@tonic-gate 			}
8037c478bd9Sstevel@tonic-gate 			if (Gen.g_passdirfd != -1)
8047c478bd9Sstevel@tonic-gate 				(void) close(Gen.g_passdirfd);
8057c478bd9Sstevel@tonic-gate 			Gen.g_passdirfd = -1;
806ced83f9bSceastha 			if (Atflag || SysAtflag) {
8077c478bd9Sstevel@tonic-gate 				if (Gen.g_dirfd != -1) {
8087c478bd9Sstevel@tonic-gate 					(void) close(Gen.g_dirfd);
8097c478bd9Sstevel@tonic-gate 				}
8107c478bd9Sstevel@tonic-gate 				Gen.g_dirfd = -1;
8117c478bd9Sstevel@tonic-gate 				if (passret != FILE_LINKED) {
8127c478bd9Sstevel@tonic-gate 					Gen.g_nam_p = Savenam_p;
8137c478bd9Sstevel@tonic-gate 					xattrs_out(file_pass);
8147c478bd9Sstevel@tonic-gate 				}
8157c478bd9Sstevel@tonic-gate 			}
8167c478bd9Sstevel@tonic-gate 		}
8177c478bd9Sstevel@tonic-gate 		break;
8187c478bd9Sstevel@tonic-gate 	default:
8197c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible action.");
8207c478bd9Sstevel@tonic-gate 	}
8217c478bd9Sstevel@tonic-gate 	if (Ofile > 0) {
8227c478bd9Sstevel@tonic-gate 		if (close(Ofile) != 0)
8237c478bd9Sstevel@tonic-gate 			msg(EXTN, "close error");
8247c478bd9Sstevel@tonic-gate 	}
8257c478bd9Sstevel@tonic-gate 	if (Archive > 0) {
8267c478bd9Sstevel@tonic-gate 		if (close(Archive) != 0)
8277c478bd9Sstevel@tonic-gate 			msg(EXTN, "close error");
8287c478bd9Sstevel@tonic-gate 	}
829*b0ee9efaSGary Mills 	if ((Args & OCq) == 0) {
830*b0ee9efaSGary Mills 		Blocks = (u_longlong_t)(Blocks * Bufsize + SBlocks +
831*b0ee9efaSGary Mills 		    0x1FF) >> 9;
8327c478bd9Sstevel@tonic-gate 		msg(EPOST, "%lld blocks", Blocks);
833*b0ee9efaSGary Mills 	}
8347c478bd9Sstevel@tonic-gate 	if (Error_cnt)
8357c478bd9Sstevel@tonic-gate 		msg(EPOST, "%d error(s)", Error_cnt);
8367c478bd9Sstevel@tonic-gate 	return (EXIT_CODE);
8377c478bd9Sstevel@tonic-gate }
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate /*
8407c478bd9Sstevel@tonic-gate  * add_lnk: Add a linked file's header to the linked file data structure, by
8417c478bd9Sstevel@tonic-gate  * either adding it to the end of an existing sub-list or starting
8427c478bd9Sstevel@tonic-gate  * a new sub-list.  Each sub-list saves the links to a given file.
8437c478bd9Sstevel@tonic-gate  *
8447c478bd9Sstevel@tonic-gate  * Directly returns a pointer to the new entry; returns a pointer to the head
8457c478bd9Sstevel@tonic-gate  * of the sub-list in which that entry is located through the argument.
8467c478bd9Sstevel@tonic-gate  */
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate static struct Lnk *
8497c478bd9Sstevel@tonic-gate add_lnk(struct Lnk **sublist_return)
8507c478bd9Sstevel@tonic-gate {
8517c478bd9Sstevel@tonic-gate 	struct Lnk *new_entry, *sublist;
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	for (sublist = Lnk_hd.L_nxt_p;
8547c478bd9Sstevel@tonic-gate 	    sublist != &Lnk_hd;
8557c478bd9Sstevel@tonic-gate 	    sublist = sublist->L_nxt_p) {
8567c478bd9Sstevel@tonic-gate 		if (sublist->L_gen.g_ino == G_p->g_ino &&
8577c478bd9Sstevel@tonic-gate 		    sublist->L_gen.g_dev == G_p->g_dev) {
8587c478bd9Sstevel@tonic-gate 			/* found */
8597c478bd9Sstevel@tonic-gate 			break;
8607c478bd9Sstevel@tonic-gate 		}
8617c478bd9Sstevel@tonic-gate 	}
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 	new_entry = e_zalloc(E_EXIT, sizeof (struct Lnk));
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	new_entry->L_lnk_p = NULL;
8667c478bd9Sstevel@tonic-gate 	new_entry->L_gen = *G_p; /* structure copy */
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	new_entry->L_gen.g_nam_p = e_zalloc(E_EXIT, (size_t)G_p->g_namesz);
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	(void) strcpy(new_entry->L_gen.g_nam_p, G_p->g_nam_p);
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	if (sublist == &Lnk_hd) {
8737c478bd9Sstevel@tonic-gate 		/* start new sub-list */
8747c478bd9Sstevel@tonic-gate 		new_entry->L_nxt_p = &Lnk_hd;
8757c478bd9Sstevel@tonic-gate 		new_entry->L_bck_p = Lnk_hd.L_bck_p;
8767c478bd9Sstevel@tonic-gate 		Lnk_hd.L_bck_p = new_entry->L_bck_p->L_nxt_p = new_entry;
8777c478bd9Sstevel@tonic-gate 		new_entry->L_lnk_p = NULL;
8787c478bd9Sstevel@tonic-gate 		new_entry->L_cnt = 1;
8797c478bd9Sstevel@tonic-gate 		new_entry->L_data = Onecopy ? 0 : 1;
8807c478bd9Sstevel@tonic-gate 		sublist = new_entry;
8817c478bd9Sstevel@tonic-gate 	} else {
8827c478bd9Sstevel@tonic-gate 		/* add to existing sub-list */
8837c478bd9Sstevel@tonic-gate 		struct Lnk *ptr;
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 		sublist->L_cnt++;
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 		for (ptr = sublist;
8887c478bd9Sstevel@tonic-gate 		    ptr->L_lnk_p != NULL;
8897c478bd9Sstevel@tonic-gate 		    ptr = ptr->L_lnk_p) {
8907c478bd9Sstevel@tonic-gate 			ptr->L_gen.g_filesz = G_p->g_filesz;
8917c478bd9Sstevel@tonic-gate 		}
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 		ptr->L_gen.g_filesz = G_p->g_filesz;
8947c478bd9Sstevel@tonic-gate 		ptr->L_lnk_p = new_entry;
8957c478bd9Sstevel@tonic-gate 	}
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	*sublist_return = sublist;
8987c478bd9Sstevel@tonic-gate 	return (new_entry);
8997c478bd9Sstevel@tonic-gate }
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate /*
9027c478bd9Sstevel@tonic-gate  * bfill: Read req_cnt bytes (out of filelen bytes) from the I/O buffer,
9037c478bd9Sstevel@tonic-gate  * moving them to rd_buf_p.  When there are no bytes left in the I/O buffer,
9047c478bd9Sstevel@tonic-gate  * Fillbuf is set and the I/O buffer is filled.  The variable dist is the
9057c478bd9Sstevel@tonic-gate  * distance to lseek if an I/O error is encountered with the -k option set
9067c478bd9Sstevel@tonic-gate  * (converted to a multiple of Bufsize).
9077c478bd9Sstevel@tonic-gate  */
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate static int
9107c478bd9Sstevel@tonic-gate bfill(void)
9117c478bd9Sstevel@tonic-gate {
9127c478bd9Sstevel@tonic-gate 	int i = 0, rv;
9137c478bd9Sstevel@tonic-gate 	static int eof = 0;
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	if (!Dflag) {
9167c478bd9Sstevel@tonic-gate 	while ((Buffr.b_end_p - Buffr.b_in_p) >= Bufsize) {
9177c478bd9Sstevel@tonic-gate 		errno = 0;
9187c478bd9Sstevel@tonic-gate 		if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0) {
9197c478bd9Sstevel@tonic-gate 			if (((Buffr.b_end_p - Buffr.b_in_p) >= Bufsize) &&
9207c478bd9Sstevel@tonic-gate 			    (Eomflag == 0)) {
9217c478bd9Sstevel@tonic-gate 				Eomflag = 1;
9227c478bd9Sstevel@tonic-gate 				return (1);
9237c478bd9Sstevel@tonic-gate 			}
9247c478bd9Sstevel@tonic-gate 			if (errno == ENOSPC) {
9257c478bd9Sstevel@tonic-gate 				(void) chgreel(INPUT);
9267c478bd9Sstevel@tonic-gate 				if (Hdr_type == BAR) {
9277c478bd9Sstevel@tonic-gate 					skip_bar_volhdr();
9287c478bd9Sstevel@tonic-gate 				}
9297c478bd9Sstevel@tonic-gate 				continue;
9307c478bd9Sstevel@tonic-gate 			} else if (Args & OCk) {
9317c478bd9Sstevel@tonic-gate 				if (i++ > MX_SEEKS)
9327c478bd9Sstevel@tonic-gate 					msg(EXT, "Cannot recover.");
9337c478bd9Sstevel@tonic-gate 				if (lseek(Archive, Bufsize, SEEK_REL) < 0)
9347c478bd9Sstevel@tonic-gate 					msg(EXTN, "Cannot lseek()");
9357c478bd9Sstevel@tonic-gate 				Error_cnt++;
9367c478bd9Sstevel@tonic-gate 				Buf_error++;
9377c478bd9Sstevel@tonic-gate 				rv = 0;
9387c478bd9Sstevel@tonic-gate 				continue;
9397c478bd9Sstevel@tonic-gate 			} else
9407c478bd9Sstevel@tonic-gate 				ioerror(INPUT);
9417c478bd9Sstevel@tonic-gate 		} /* (rv = g_read(Device, Archive ... */
9427c478bd9Sstevel@tonic-gate 		if (Hdr_type != BAR || rv == Bufsize) {
9437c478bd9Sstevel@tonic-gate 			Buffr.b_in_p += rv;
9447c478bd9Sstevel@tonic-gate 			Buffr.b_cnt += (long)rv;
9457c478bd9Sstevel@tonic-gate 		}
9467c478bd9Sstevel@tonic-gate 		if (rv == Bufsize) {
9477c478bd9Sstevel@tonic-gate 			eof = 0;
9487c478bd9Sstevel@tonic-gate 			Blocks++;
9497c478bd9Sstevel@tonic-gate 		} else if (rv == 0) {
9507c478bd9Sstevel@tonic-gate 			if (!eof) {
9517c478bd9Sstevel@tonic-gate 				eof = 1;
9527c478bd9Sstevel@tonic-gate 				break;
9537c478bd9Sstevel@tonic-gate 			}
9547c478bd9Sstevel@tonic-gate 			(void) chgreel(INPUT);
9557c478bd9Sstevel@tonic-gate 			eof = 0;	/* reset the eof after chgreel	*/
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 			/*
9587c478bd9Sstevel@tonic-gate 			 * if spans multiple volume, skip the volume header of
9597c478bd9Sstevel@tonic-gate 			 * the next volume so that the file currently being
9607c478bd9Sstevel@tonic-gate 			 * extracted can continue to be extracted.
9617c478bd9Sstevel@tonic-gate 			 */
9627c478bd9Sstevel@tonic-gate 			if (Hdr_type == BAR) {
9637c478bd9Sstevel@tonic-gate 				skip_bar_volhdr();
9647c478bd9Sstevel@tonic-gate 			}
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 			continue;
9677c478bd9Sstevel@tonic-gate 		} else {
9687c478bd9Sstevel@tonic-gate 			eof = 0;
9697c478bd9Sstevel@tonic-gate 			SBlocks += (u_longlong_t)rv;
9707c478bd9Sstevel@tonic-gate 		}
9717c478bd9Sstevel@tonic-gate 	} /* (Buffr.b_end_p - Buffr.b_in_p) <= Bufsize */
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	} else {			/* Dflag */
9747c478bd9Sstevel@tonic-gate 		errno = 0;
9757c478bd9Sstevel@tonic-gate 		if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0) {
9767c478bd9Sstevel@tonic-gate 			return (-1);
9777c478bd9Sstevel@tonic-gate 		} /* (rv = g_read(Device, Archive ... */
9787c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += rv;
9797c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += (long)rv;
9807c478bd9Sstevel@tonic-gate 		if (rv == Bufsize) {
9817c478bd9Sstevel@tonic-gate 			eof = 0;
9827c478bd9Sstevel@tonic-gate 			Blocks++;
9837c478bd9Sstevel@tonic-gate 		} else if (!rv) {
9847c478bd9Sstevel@tonic-gate 			if (!eof) {
9857c478bd9Sstevel@tonic-gate 				eof = 1;
9867c478bd9Sstevel@tonic-gate 				return (rv);
9877c478bd9Sstevel@tonic-gate 			}
9887c478bd9Sstevel@tonic-gate 			return (-1);
9897c478bd9Sstevel@tonic-gate 		} else {
9907c478bd9Sstevel@tonic-gate 			eof = 0;
9917c478bd9Sstevel@tonic-gate 			SBlocks += (u_longlong_t)rv;
9927c478bd9Sstevel@tonic-gate 		}
9937c478bd9Sstevel@tonic-gate 	}
9947c478bd9Sstevel@tonic-gate 	return (rv);
9957c478bd9Sstevel@tonic-gate }
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate /*
9987c478bd9Sstevel@tonic-gate  * bflush: Move wr_cnt bytes from data_p into the I/O buffer.  When the
9997c478bd9Sstevel@tonic-gate  * I/O buffer is full, Flushbuf is set and the buffer is written out.
10007c478bd9Sstevel@tonic-gate  */
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate static void
10037c478bd9Sstevel@tonic-gate bflush(void)
10047c478bd9Sstevel@tonic-gate {
10057c478bd9Sstevel@tonic-gate 	int rv;
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 	while (Buffr.b_cnt >= Bufsize) {
10087c478bd9Sstevel@tonic-gate 		errno = 0;
10097c478bd9Sstevel@tonic-gate 		if ((rv = g_write(Device, Archive, Buffr.b_out_p,
10107c478bd9Sstevel@tonic-gate 		    Bufsize)) < 0) {
10117c478bd9Sstevel@tonic-gate 			if (errno == ENOSPC && !Dflag)
10127c478bd9Sstevel@tonic-gate 				rv = chgreel(OUTPUT);
10137c478bd9Sstevel@tonic-gate 			else
10147c478bd9Sstevel@tonic-gate 				ioerror(OUTPUT);
10157c478bd9Sstevel@tonic-gate 		}
10167c478bd9Sstevel@tonic-gate 		Buffr.b_out_p += rv;
10177c478bd9Sstevel@tonic-gate 		Buffr.b_cnt -= (long)rv;
10187c478bd9Sstevel@tonic-gate 		if (rv == Bufsize)
10197c478bd9Sstevel@tonic-gate 			Blocks++;
10207c478bd9Sstevel@tonic-gate 		else if (rv > 0)
10217c478bd9Sstevel@tonic-gate 			SBlocks += (u_longlong_t)rv;
10227c478bd9Sstevel@tonic-gate 	}
10237c478bd9Sstevel@tonic-gate 	rstbuf();
10247c478bd9Sstevel@tonic-gate }
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate /*
10277c478bd9Sstevel@tonic-gate  * chgreel: Determine if end-of-medium has been reached.  If it has,
10287c478bd9Sstevel@tonic-gate  * close the current medium and prompt the user for the next medium.
10297c478bd9Sstevel@tonic-gate  */
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate static int
10327c478bd9Sstevel@tonic-gate chgreel(int dir)
10337c478bd9Sstevel@tonic-gate {
10347c478bd9Sstevel@tonic-gate 	int lastchar, tryagain, askagain, rv;
10357c478bd9Sstevel@tonic-gate 	int tmpdev;
10367c478bd9Sstevel@tonic-gate 	char str[APATH];
10377c478bd9Sstevel@tonic-gate 	struct stat statb;
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	rv = 0;
10407c478bd9Sstevel@tonic-gate 	if (fstat(Archive, &statb) < 0)
10417c478bd9Sstevel@tonic-gate 		msg(EXTN, "Error during stat() of archive");
10427c478bd9Sstevel@tonic-gate 	if ((statb.st_mode & S_IFMT) != S_IFCHR) {
10437c478bd9Sstevel@tonic-gate 		if (dir == INPUT) {
10447c478bd9Sstevel@tonic-gate 			msg(EXT, "%s%s\n",
10457c478bd9Sstevel@tonic-gate 			    "Can't read input:  end of file encountered ",
10467c478bd9Sstevel@tonic-gate 			    "prior to expected end of archive.");
10477c478bd9Sstevel@tonic-gate 		}
10487c478bd9Sstevel@tonic-gate 	}
10497c478bd9Sstevel@tonic-gate 	msg(EPOST, "\007End of medium on \"%s\".", dir ? "output" : "input");
10507c478bd9Sstevel@tonic-gate 	if (is_floppy(Archive))
10517c478bd9Sstevel@tonic-gate 		(void) ioctl(Archive, FDEJECT, NULL);
10527c478bd9Sstevel@tonic-gate 	if ((close(Archive) != 0) && (dir == OUTPUT))
10537c478bd9Sstevel@tonic-gate 		msg(EXTN, "close error");
10547c478bd9Sstevel@tonic-gate 	Archive = 0;
10557c478bd9Sstevel@tonic-gate 	Volcnt++;
10567c478bd9Sstevel@tonic-gate 	for (;;) {
1057ced83f9bSceastha 		if (Rtty_p == NULL)
10587c478bd9Sstevel@tonic-gate 			Rtty_p = fopen(Ttyname, "r");
10597c478bd9Sstevel@tonic-gate 		do { /* tryagain */
10607c478bd9Sstevel@tonic-gate 			if (IOfil_p) {
10617c478bd9Sstevel@tonic-gate 				do {
10625fbb8099SNobutomo Nakano 					msg(EPOST, Eom_p, Volcnt);
10637c478bd9Sstevel@tonic-gate 					if (!Rtty_p || fgets(str, sizeof (str),
1064ced83f9bSceastha 					    Rtty_p) == NULL)
10657c478bd9Sstevel@tonic-gate 						msg(EXT, "Cannot read tty.");
10667c478bd9Sstevel@tonic-gate 					askagain = 0;
10677c478bd9Sstevel@tonic-gate 					switch (*str) {
10687c478bd9Sstevel@tonic-gate 					case '\n':
10697c478bd9Sstevel@tonic-gate 						(void) strcpy(str, IOfil_p);
10707c478bd9Sstevel@tonic-gate 						break;
10717c478bd9Sstevel@tonic-gate 					case 'q':
10727c478bd9Sstevel@tonic-gate 						exit(EXIT_CODE);
10737c478bd9Sstevel@tonic-gate 					default:
10747c478bd9Sstevel@tonic-gate 						askagain = 1;
10757c478bd9Sstevel@tonic-gate 					}
10767c478bd9Sstevel@tonic-gate 				} while (askagain);
10777c478bd9Sstevel@tonic-gate 			} else {
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 				if (Hdr_type == BAR)
10807c478bd9Sstevel@tonic-gate 					Bar_vol_num++;
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 				msg(EPOST,
10837c478bd9Sstevel@tonic-gate 				    "To continue, type device/file name when "
10847c478bd9Sstevel@tonic-gate 				    "ready.");
10857c478bd9Sstevel@tonic-gate 				if (!Rtty_p || fgets(str, sizeof (str),
1086ced83f9bSceastha 				    Rtty_p) == NULL)
10877c478bd9Sstevel@tonic-gate 					msg(EXT, "Cannot read tty.");
10887c478bd9Sstevel@tonic-gate 				lastchar = strlen(str) - 1;
10897c478bd9Sstevel@tonic-gate 				if (*(str + lastchar) == '\n') /* remove '\n' */
10907c478bd9Sstevel@tonic-gate 					*(str + lastchar) = '\0';
10917c478bd9Sstevel@tonic-gate 				if (!*str)
10927c478bd9Sstevel@tonic-gate 					exit(EXIT_CODE);
10937c478bd9Sstevel@tonic-gate 			}
10947c478bd9Sstevel@tonic-gate 			tryagain = 0;
10957c478bd9Sstevel@tonic-gate 			if ((Archive = open(str, dir)) < 0) {
10967c478bd9Sstevel@tonic-gate 				msg(ERRN, "Cannot open \"%s\"", str);
10977c478bd9Sstevel@tonic-gate 				tryagain = 1;
10987c478bd9Sstevel@tonic-gate 			}
10997c478bd9Sstevel@tonic-gate 		} while (tryagain);
11007c478bd9Sstevel@tonic-gate 		(void) g_init(&tmpdev, &Archive);
11017c478bd9Sstevel@tonic-gate 		if (tmpdev != Device)
11027c478bd9Sstevel@tonic-gate 			msg(EXT, "Cannot change media types in mid-stream.");
11037c478bd9Sstevel@tonic-gate 		if (dir == INPUT)
11047c478bd9Sstevel@tonic-gate 			break;
11057c478bd9Sstevel@tonic-gate 		else { /* dir == OUTPUT */
11067c478bd9Sstevel@tonic-gate 			errno = 0;
11077c478bd9Sstevel@tonic-gate 			if ((rv = g_write(Device, Archive, Buffr.b_out_p,
11087c478bd9Sstevel@tonic-gate 			    Bufsize)) == Bufsize)
11097c478bd9Sstevel@tonic-gate 				break;
11107c478bd9Sstevel@tonic-gate 			else
11117c478bd9Sstevel@tonic-gate 				msg(ERR,
11127c478bd9Sstevel@tonic-gate 				    "Unable to write this medium, try "
11137c478bd9Sstevel@tonic-gate 				    "another.");
11147c478bd9Sstevel@tonic-gate 		}
11157c478bd9Sstevel@tonic-gate 	} /* ;; */
11167c478bd9Sstevel@tonic-gate 	Eomflag = 0;
11177c478bd9Sstevel@tonic-gate 	return (rv);
11187c478bd9Sstevel@tonic-gate }
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate /*
11217c478bd9Sstevel@tonic-gate  * ckname: Check filenames against user specified patterns,
11227c478bd9Sstevel@tonic-gate  * and/or ask the user for new name when -r is used.
11237c478bd9Sstevel@tonic-gate  */
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate static int
11267c478bd9Sstevel@tonic-gate ckname(int flag)
11277c478bd9Sstevel@tonic-gate {
1128ced83f9bSceastha 	int	lastchar;
1129ced83f9bSceastha 	size_t	rename_bufsz = Max_namesz + 1;
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 	if (Hdr_type != TAR && Hdr_type != USTAR && Hdr_type != BAR) {
11327c478bd9Sstevel@tonic-gate 		/* Re-visit tar size issues later */
11337c478bd9Sstevel@tonic-gate 		if (G_p->g_namesz - 1 > Max_namesz) {
11347c478bd9Sstevel@tonic-gate 			msg(ERR, "Name exceeds maximum length - skipped.");
11357c478bd9Sstevel@tonic-gate 			return (F_SKIP);
11367c478bd9Sstevel@tonic-gate 		}
11377c478bd9Sstevel@tonic-gate 	}
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	if (Pat_pp && !matched())
11407c478bd9Sstevel@tonic-gate 		return (F_SKIP);
1141ced83f9bSceastha 
1142ced83f9bSceastha 	/* rename interactively */
1143ced83f9bSceastha 	if ((Args & OCr) && !Adir && !G_p->g_rw_sysattr) {
11447c478bd9Sstevel@tonic-gate 		(void) fprintf(Wtty_p, gettext("Rename \"%s%s%s\"? "),
1145ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? G_p->g_nam_p : Renam_p,
1146ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? "" : gettext(" Attribute "),
1147ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? "" : G_p->g_attrnam_p);
11487c478bd9Sstevel@tonic-gate 		(void) fflush(Wtty_p);
1149ced83f9bSceastha 		if (fgets(Renametmp_p, rename_bufsz, Rtty_p) == NULL)
11507c478bd9Sstevel@tonic-gate 			msg(EXT, "Cannot read tty.");
11517c478bd9Sstevel@tonic-gate 		if (feof(Rtty_p))
11527c478bd9Sstevel@tonic-gate 			exit(EXIT_CODE);
11537c478bd9Sstevel@tonic-gate 		lastchar = strlen(Renametmp_p) - 1;
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 		/* remove trailing '\n' */
11567c478bd9Sstevel@tonic-gate 		if (*(Renametmp_p + lastchar) == '\n')
11577c478bd9Sstevel@tonic-gate 			*(Renametmp_p + lastchar) = '\0';
11587c478bd9Sstevel@tonic-gate 		if (*Renametmp_p == '\0') {
11597c478bd9Sstevel@tonic-gate 			msg(POST, "%s%s%s Skipped.",
1160ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? G_p->g_nam_p :
1161ced83f9bSceastha 			    G_p->g_attrfnam_p,
1162ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
1163ced83f9bSceastha 			    gettext(" Attribute "),
1164ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" : G_p->g_attrnam_p);
1165ced83f9bSceastha 			if (G_p->g_attrparent_p == NULL) {
11667c478bd9Sstevel@tonic-gate 				*G_p->g_nam_p = '\0';
1167ced83f9bSceastha 			}
1168ced83f9bSceastha 			if (Renam_attr_p) {
1169ced83f9bSceastha 				*Renam_attr_p = '\0';
1170ced83f9bSceastha 			}
11717c478bd9Sstevel@tonic-gate 			return (F_SKIP);
11727c478bd9Sstevel@tonic-gate 		} else if (strcmp(Renametmp_p, ".") != 0) {
1173ced83f9bSceastha 			if (G_p->g_attrnam_p == NULL) {
1174ced83f9bSceastha 				if (strlen(Renametmp_p) > strlen(
1175ced83f9bSceastha 				    G_p->g_nam_p)) {
11767c478bd9Sstevel@tonic-gate 					if ((G_p->g_nam_p != &nambuf[0]) &&
1177ced83f9bSceastha 					    (G_p->g_nam_p != &fullnam[0])) {
11787c478bd9Sstevel@tonic-gate 						free(G_p->g_nam_p);
1179ced83f9bSceastha 						G_p->g_nam_p = e_zalloc(E_EXIT,
1180ced83f9bSceastha 						    rename_bufsz);
11817c478bd9Sstevel@tonic-gate 					}
1182ced83f9bSceastha 				}
1183ced83f9bSceastha 				if (Renam_attr_p) {
1184ced83f9bSceastha 					*Renam_attr_p = '\0';
1185ced83f9bSceastha 				}
1186ced83f9bSceastha 				if ((strlcpy(Renam_p, Renametmp_p,
1187ced83f9bSceastha 				    rename_bufsz) > rename_bufsz) ||
1188ced83f9bSceastha 				    (strlcpy(G_p->g_nam_p, Renametmp_p,
1189ced83f9bSceastha 				    rename_bufsz) > rename_bufsz)) {
1190ced83f9bSceastha 					msg(EXTN, "buffer overflow");
1191ced83f9bSceastha 				}
1192ced83f9bSceastha 			} else {
1193ced83f9bSceastha 				if (G_p->g_attrnam_p != NULL) {
11947c478bd9Sstevel@tonic-gate 					free(G_p->g_attrnam_p);
11957c478bd9Sstevel@tonic-gate 					G_p->g_attrnam_p = e_strdup(E_EXIT,
11967c478bd9Sstevel@tonic-gate 					    Renametmp_p);
11977c478bd9Sstevel@tonic-gate 					(void) strcpy(G_p->g_nam_p, Renam_p);
1198ced83f9bSceastha 					if (Renam_attr_p) {
1199ced83f9bSceastha 						if (strlcpy(Renam_attr_p,
1200ced83f9bSceastha 						    Renametmp_p, rename_bufsz) >
1201ced83f9bSceastha 						    rename_bufsz) {
1202ced83f9bSceastha 							msg(EXTN,
1203ced83f9bSceastha 							    "buffer overflow");
12047c478bd9Sstevel@tonic-gate 						}
1205ced83f9bSceastha 					}
1206ced83f9bSceastha 				}
1207ced83f9bSceastha 			}
1208ced83f9bSceastha 		} else {
1209ced83f9bSceastha 			if (G_p->g_attrnam_p == NULL) {
1210ced83f9bSceastha 				*Renam_p = '\0';
1211ced83f9bSceastha 			}
1212ced83f9bSceastha 			if (Renam_attr_p) {
1213ced83f9bSceastha 				*Renam_attr_p = '\0';
1214ced83f9bSceastha 			}
12157c478bd9Sstevel@tonic-gate 		}
12167c478bd9Sstevel@tonic-gate 	}
12177c478bd9Sstevel@tonic-gate 	if (flag != 0 || Onecopy == 0) {
12187c478bd9Sstevel@tonic-gate 		VERBOSE((Args & OCt), G_p->g_nam_p);
12197c478bd9Sstevel@tonic-gate 	}
12207c478bd9Sstevel@tonic-gate 	if (Args & OCt)
12217c478bd9Sstevel@tonic-gate 		return (F_SKIP);
12227c478bd9Sstevel@tonic-gate 	return (F_EXTR);
12237c478bd9Sstevel@tonic-gate }
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate /*
12267c478bd9Sstevel@tonic-gate  * ckopts: Check the validity of all command line options.
12277c478bd9Sstevel@tonic-gate  */
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate static void
12307c478bd9Sstevel@tonic-gate ckopts(long mask)
12317c478bd9Sstevel@tonic-gate {
12327c478bd9Sstevel@tonic-gate 	int oflag;
12337c478bd9Sstevel@tonic-gate 	char *t_p;
12347c478bd9Sstevel@tonic-gate 	long errmsk;
1235647ab8f4Sceastha 	uid_t	Euid = geteuid();	/* Effective uid of invoker */
1236647ab8f4Sceastha #ifdef SOLARIS_PRIVS
1237647ab8f4Sceastha 	priv_set_t *privset;
12383a3e214eSceastha 	priv_set_t *zones_privset;
1239647ab8f4Sceastha #endif	/* SOLARIS_PRIVS */
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 	if (mask & OCi) {
12427c478bd9Sstevel@tonic-gate 		errmsk = mask & INV_MSK4i;
12437c478bd9Sstevel@tonic-gate 	} else if (mask & OCo) {
12447c478bd9Sstevel@tonic-gate 		errmsk = mask & INV_MSK4o;
12457c478bd9Sstevel@tonic-gate 	} else if (mask & OCp) {
12467c478bd9Sstevel@tonic-gate 		errmsk = mask & INV_MSK4p;
12477c478bd9Sstevel@tonic-gate 	} else {
12487c478bd9Sstevel@tonic-gate 		msg(ERR, "One of -i, -o or -p must be specified.");
12497c478bd9Sstevel@tonic-gate 		errmsk = 0;
12507c478bd9Sstevel@tonic-gate 	}
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	if (errmsk) {
12537c478bd9Sstevel@tonic-gate 		/* if non-zero, invalid options were specified */
12547c478bd9Sstevel@tonic-gate 		Error_cnt++;
12557c478bd9Sstevel@tonic-gate 	}
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 	if ((mask & OCa) && (mask & OCm) && ((mask & OCi) ||
12587c478bd9Sstevel@tonic-gate 	    (mask & OCo))) {
12597c478bd9Sstevel@tonic-gate 		msg(ERR, "-a and -m are mutually exclusive.");
12607c478bd9Sstevel@tonic-gate 	}
12617c478bd9Sstevel@tonic-gate 
12625fbb8099SNobutomo Nakano 	if ((mask & OCc) && (mask & OCH) &&
12635fbb8099SNobutomo Nakano 	    (strcmp("odc", Hdr_p) != 0 && strcmp("odc_sparse", Hdr_p) != 0)) {
12647c478bd9Sstevel@tonic-gate 		msg(ERR, "-c and -H are mutually exclusive.");
12657c478bd9Sstevel@tonic-gate 	}
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 	if ((mask & OCv) && (mask & OCV)) {
12687c478bd9Sstevel@tonic-gate 		msg(ERR, "-v and -V are mutually exclusive.");
12697c478bd9Sstevel@tonic-gate 	}
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	if ((mask & OCt) && (mask & OCV)) {
12727c478bd9Sstevel@tonic-gate 		msg(ERR, "-t and -V are mutually exclusive.");
12737c478bd9Sstevel@tonic-gate 	}
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 	if ((mask & OCB) && (mask & OCC)) {
12767c478bd9Sstevel@tonic-gate 		msg(ERR, "-B and -C are mutually exclusive.");
12777c478bd9Sstevel@tonic-gate 	}
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 	if ((mask & OCH) && (mask & OC6)) {
12807c478bd9Sstevel@tonic-gate 		msg(ERR, "-H and -6 are mutually exclusive.");
12817c478bd9Sstevel@tonic-gate 	}
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate 	if ((mask & OCM) && !((mask & OCI) || (mask & OCO))) {
12847c478bd9Sstevel@tonic-gate 		msg(ERR, "-M not meaningful without -O or -I.");
12857c478bd9Sstevel@tonic-gate 	}
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 	if ((mask & OCA) && !(mask & OCO)) {
12887c478bd9Sstevel@tonic-gate 		msg(ERR, "-A requires the -O option.");
12897c478bd9Sstevel@tonic-gate 	}
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	if (Bufsize <= 0) {
12927c478bd9Sstevel@tonic-gate 		msg(ERR, "Illegal size given for -C option.");
12937c478bd9Sstevel@tonic-gate 	}
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 	if (mask & OCH) {
12967c478bd9Sstevel@tonic-gate 		t_p = Hdr_p;
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate 		while (*t_p != NULL) {
12997c478bd9Sstevel@tonic-gate 			if (isupper(*t_p)) {
13007c478bd9Sstevel@tonic-gate 				*t_p = 'a' + (*t_p - 'A');
13017c478bd9Sstevel@tonic-gate 			}
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 			t_p++;
13047c478bd9Sstevel@tonic-gate 		}
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 		if (!(strcmp("odc", Hdr_p))) {
13077c478bd9Sstevel@tonic-gate 			Hdr_type = CHR;
13087c478bd9Sstevel@tonic-gate 			Max_namesz = CPATH;
13097c478bd9Sstevel@tonic-gate 			Onecopy = 0;
13107c478bd9Sstevel@tonic-gate 			Use_old_stat = 1;
13115fbb8099SNobutomo Nakano 		} else if (!(strcmp("odc_sparse", Hdr_p))) {
13125fbb8099SNobutomo Nakano 			Hdr_type = CHR;
13135fbb8099SNobutomo Nakano 			Max_namesz = CPATH;
13145fbb8099SNobutomo Nakano 			Onecopy = 0;
13155fbb8099SNobutomo Nakano 			Use_old_stat = 1;
13165fbb8099SNobutomo Nakano 			Compress_sparse = 1;
13175fbb8099SNobutomo Nakano 		} else if (!(strcmp("ascii_sparse", Hdr_p))) {
13185fbb8099SNobutomo Nakano 			Hdr_type = ASC;
13195fbb8099SNobutomo Nakano 			Max_namesz = APATH;
13205fbb8099SNobutomo Nakano 			Onecopy = 1;
13215fbb8099SNobutomo Nakano 			Compress_sparse = 1;
13227c478bd9Sstevel@tonic-gate 		} else if (!(strcmp("crc", Hdr_p))) {
13237c478bd9Sstevel@tonic-gate 			Hdr_type = CRC;
13247c478bd9Sstevel@tonic-gate 			Max_namesz = APATH;
13257c478bd9Sstevel@tonic-gate 			Onecopy = 1;
13267c478bd9Sstevel@tonic-gate 		} else if (!(strcmp("tar", Hdr_p))) {
13277c478bd9Sstevel@tonic-gate 			if (Args & OCo) {
13287c478bd9Sstevel@tonic-gate 				Hdr_type = USTAR;
13297c478bd9Sstevel@tonic-gate 				Max_namesz = HNAMLEN - 1;
13307c478bd9Sstevel@tonic-gate 			} else {
13317c478bd9Sstevel@tonic-gate 				Hdr_type = TAR;
13327c478bd9Sstevel@tonic-gate 				Max_namesz = TNAMLEN - 1;
13337c478bd9Sstevel@tonic-gate 			}
13347c478bd9Sstevel@tonic-gate 			Onecopy = 0;
13357c478bd9Sstevel@tonic-gate 		} else if (!(strcmp("ustar", Hdr_p))) {
13367c478bd9Sstevel@tonic-gate 			Hdr_type = USTAR;
13377c478bd9Sstevel@tonic-gate 			Max_namesz = HNAMLEN - 1;
13387c478bd9Sstevel@tonic-gate 			Onecopy = 0;
13397c478bd9Sstevel@tonic-gate 		} else if (!(strcmp("bar", Hdr_p))) {
13407c478bd9Sstevel@tonic-gate 			if ((Args & OCo) || (Args & OCp)) {
13417c478bd9Sstevel@tonic-gate 				msg(ERR,
13427c478bd9Sstevel@tonic-gate 				    "Header type bar can only be used with -i");
13437c478bd9Sstevel@tonic-gate 			}
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 			if (Args & OCP) {
13467c478bd9Sstevel@tonic-gate 				msg(ERR,
13477c478bd9Sstevel@tonic-gate 				    "Can't preserve using bar header");
13487c478bd9Sstevel@tonic-gate 			}
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 			Hdr_type = BAR;
13517c478bd9Sstevel@tonic-gate 			Max_namesz = TNAMLEN - 1;
13527c478bd9Sstevel@tonic-gate 			Onecopy = 0;
13537c478bd9Sstevel@tonic-gate 		} else {
13547c478bd9Sstevel@tonic-gate 			msg(ERR, "Invalid header \"%s\" specified", Hdr_p);
13557c478bd9Sstevel@tonic-gate 		}
13567c478bd9Sstevel@tonic-gate 	}
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 	if (mask & OCr) {
13597c478bd9Sstevel@tonic-gate 		Rtty_p = fopen(Ttyname, "r");
13607c478bd9Sstevel@tonic-gate 		Wtty_p = fopen(Ttyname, "w");
13617c478bd9Sstevel@tonic-gate 
1362ced83f9bSceastha 		if (Rtty_p == NULL || Wtty_p == NULL) {
13637c478bd9Sstevel@tonic-gate 			msg(ERR, "Cannot rename, \"%s\" missing", Ttyname);
13647c478bd9Sstevel@tonic-gate 		}
13657c478bd9Sstevel@tonic-gate 	}
13667c478bd9Sstevel@tonic-gate 
1367ced83f9bSceastha 	if ((mask & OCE) && (Ef_p = fopen(Efil_p, "r")) == NULL) {
13687c478bd9Sstevel@tonic-gate 		msg(ERR, "Cannot open \"%s\" to read patterns", Efil_p);
13697c478bd9Sstevel@tonic-gate 	}
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 	if ((mask & OCI) && (Archive = open(IOfil_p, O_RDONLY)) < 0) {
13727c478bd9Sstevel@tonic-gate 		msg(ERR, "Cannot open \"%s\" for input", IOfil_p);
13737c478bd9Sstevel@tonic-gate 	}
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 	if (mask & OCO) {
13767c478bd9Sstevel@tonic-gate 		if (mask & OCA) {
13777c478bd9Sstevel@tonic-gate 			if ((Archive = open(IOfil_p, O_RDWR)) < 0) {
13787c478bd9Sstevel@tonic-gate 				msg(ERR,
13797c478bd9Sstevel@tonic-gate 				    "Cannot open \"%s\" for append",
13807c478bd9Sstevel@tonic-gate 				    IOfil_p);
13817c478bd9Sstevel@tonic-gate 			}
13827c478bd9Sstevel@tonic-gate 		} else {
13837c478bd9Sstevel@tonic-gate 			oflag = (O_WRONLY | O_CREAT | O_TRUNC);
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate 			if ((Archive = open(IOfil_p, oflag, 0777)) < 0) {
13867c478bd9Sstevel@tonic-gate 				msg(ERR,
13877c478bd9Sstevel@tonic-gate 				    "Cannot open \"%s\" for output",
13887c478bd9Sstevel@tonic-gate 				    IOfil_p);
13897c478bd9Sstevel@tonic-gate 			}
13907c478bd9Sstevel@tonic-gate 		}
13917c478bd9Sstevel@tonic-gate 	}
13927c478bd9Sstevel@tonic-gate 
1393647ab8f4Sceastha #ifdef SOLARIS_PRIVS
1394647ab8f4Sceastha 	if ((privset = priv_allocset()) == NULL) {
1395647ab8f4Sceastha 		msg(ERR, "Unable to allocate privilege set");
1396647ab8f4Sceastha 	} else if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
1397647ab8f4Sceastha 		msg(ERR, "Unable to obtain privilege set");
1398647ab8f4Sceastha 	} else {
13993a3e214eSceastha 		zones_privset = priv_str_to_set("zone", "", NULL);
14003a3e214eSceastha 		if (zones_privset != NULL) {
14013a3e214eSceastha 			privileged = (priv_issubset(zones_privset,
14023a3e214eSceastha 			    privset) == B_TRUE);
14033a3e214eSceastha 			priv_freeset(zones_privset);
14043a3e214eSceastha 		} else {
14053a3e214eSceastha 			msg(ERR, "Unable to map privilege to privilege set");
14063a3e214eSceastha 		}
1407647ab8f4Sceastha 	}
1408647ab8f4Sceastha 	if (privset != NULL) {
1409647ab8f4Sceastha 		priv_freeset(privset);
1410647ab8f4Sceastha 	}
1411647ab8f4Sceastha #else
1412647ab8f4Sceastha 	privileged = (Euid == 0);
1413647ab8f4Sceastha #endif	/* SOLARIS_PRIVS */
1414647ab8f4Sceastha 
14157c478bd9Sstevel@tonic-gate 	if (mask & OCR) {
1416ced83f9bSceastha 		if ((Rpw_p = getpwnam(Own_p)) == NULL) {
14177c478bd9Sstevel@tonic-gate 			msg(ERR, "\"%s\" is not a valid user id", Own_p);
1418647ab8f4Sceastha 		} else if ((Euid != Rpw_p->pw_uid) && !privileged) {
1419647ab8f4Sceastha 			msg(ERR, "R option only valid for super-user or "
1420647ab8f4Sceastha 			    "id matches login id of user executing cpio");
14217c478bd9Sstevel@tonic-gate 		}
14227c478bd9Sstevel@tonic-gate 	}
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate 	if ((mask & OCo) && !(mask & OCO)) {
14257c478bd9Sstevel@tonic-gate 		Out_p = stderr;
14267c478bd9Sstevel@tonic-gate 	}
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	if ((mask & OCp) && ((mask & (OCB|OCC)) == 0)) {
14297c478bd9Sstevel@tonic-gate 		/*
14307c478bd9Sstevel@tonic-gate 		 * We are in pass mode with no block size specified.  Use the
14317c478bd9Sstevel@tonic-gate 		 * larger of the native page size and 8192.
14327c478bd9Sstevel@tonic-gate 		 */
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 		Bufsize = (PageSize > 8192) ? PageSize : 8192;
14357c478bd9Sstevel@tonic-gate 	}
14367c478bd9Sstevel@tonic-gate }
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate /*
14397c478bd9Sstevel@tonic-gate  * cksum: Calculate the simple checksum of a file (CRC) or header
14407c478bd9Sstevel@tonic-gate  * (TARTYP (TAR and USTAR)).  For -o and the CRC header, the file is opened and
14417c478bd9Sstevel@tonic-gate  * the checksum is calculated.  For -i and the CRC header, the checksum
14427c478bd9Sstevel@tonic-gate  * is calculated as each block is transferred from the archive I/O buffer
14437c478bd9Sstevel@tonic-gate  * to the file system I/O buffer.  The TARTYP (TAR and USTAR) headers calculate
14447c478bd9Sstevel@tonic-gate  * the simple checksum of the header (with the checksum field of the
14457c478bd9Sstevel@tonic-gate  * header initialized to all spaces (\040).
14467c478bd9Sstevel@tonic-gate  */
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate static long
14497c478bd9Sstevel@tonic-gate cksum(char hdr, int byt_cnt, int *err)
14507c478bd9Sstevel@tonic-gate {
14517c478bd9Sstevel@tonic-gate 	char *crc_p, *end_p;
14527c478bd9Sstevel@tonic-gate 	int cnt;
14537c478bd9Sstevel@tonic-gate 	long checksum = 0L, have;
14547c478bd9Sstevel@tonic-gate 	off_t lcnt;
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 	if (err != NULL)
14577c478bd9Sstevel@tonic-gate 		*err = 0;
14587c478bd9Sstevel@tonic-gate 	switch (hdr) {
14597c478bd9Sstevel@tonic-gate 	case CRC:
14607c478bd9Sstevel@tonic-gate 		if (Args & OCi) { /* do running checksum */
14617c478bd9Sstevel@tonic-gate 			end_p = Buffr.b_out_p + byt_cnt;
14627c478bd9Sstevel@tonic-gate 			for (crc_p = Buffr.b_out_p; crc_p < end_p; crc_p++)
14637c478bd9Sstevel@tonic-gate 				checksum += (long)*crc_p;
14647c478bd9Sstevel@tonic-gate 			break;
14657c478bd9Sstevel@tonic-gate 		}
14667c478bd9Sstevel@tonic-gate 		/* OCo - do checksum of file */
14677c478bd9Sstevel@tonic-gate 		lcnt = G_p->g_filesz;
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate 		while (lcnt > 0) {
14707c478bd9Sstevel@tonic-gate 			have = (lcnt < Bufsize) ? lcnt : Bufsize;
14717c478bd9Sstevel@tonic-gate 			errno = 0;
14727c478bd9Sstevel@tonic-gate 			if (read(Ifile, Buf_p, have) != have) {
14737c478bd9Sstevel@tonic-gate 				msg(ERR, "Error computing checksum.");
14747c478bd9Sstevel@tonic-gate 				if (err != NULL)
14757c478bd9Sstevel@tonic-gate 					*err = 1;
14767c478bd9Sstevel@tonic-gate 				break;
14777c478bd9Sstevel@tonic-gate 			}
14787c478bd9Sstevel@tonic-gate 			end_p = Buf_p + have;
14797c478bd9Sstevel@tonic-gate 			for (crc_p = Buf_p; crc_p < end_p; crc_p++)
14807c478bd9Sstevel@tonic-gate 				checksum += (long)*crc_p;
14817c478bd9Sstevel@tonic-gate 			lcnt -= have;
14827c478bd9Sstevel@tonic-gate 		}
14837c478bd9Sstevel@tonic-gate 		if (lseek(Ifile, (off_t)0, SEEK_ABS) < 0)
14847c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot reset file after checksum");
14857c478bd9Sstevel@tonic-gate 		break;
14867c478bd9Sstevel@tonic-gate 	case TARTYP: /* TAR and USTAR */
14877c478bd9Sstevel@tonic-gate 		crc_p = Thdr_p->tbuf.t_cksum;
14887c478bd9Sstevel@tonic-gate 		for (cnt = 0; cnt < TCRCLEN; cnt++) {
14897c478bd9Sstevel@tonic-gate 			*crc_p = '\040';
14907c478bd9Sstevel@tonic-gate 			crc_p++;
14917c478bd9Sstevel@tonic-gate 		}
14927c478bd9Sstevel@tonic-gate 		crc_p = (char *)Thdr_p;
14937c478bd9Sstevel@tonic-gate 		for (cnt = 0; cnt < TARSZ; cnt++) {
14947c478bd9Sstevel@tonic-gate 			/*
14957c478bd9Sstevel@tonic-gate 			 * tar uses unsigned checksum, so we must use unsigned
14967c478bd9Sstevel@tonic-gate 			 * here in order to be able to read tar archives.
14977c478bd9Sstevel@tonic-gate 			 */
14987c478bd9Sstevel@tonic-gate 			checksum += (long)((unsigned char)(*crc_p));
14997c478bd9Sstevel@tonic-gate 			crc_p++;
15007c478bd9Sstevel@tonic-gate 		}
15017c478bd9Sstevel@tonic-gate 		break;
15027c478bd9Sstevel@tonic-gate 	default:
15037c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
15047c478bd9Sstevel@tonic-gate 	} /* hdr */
15057c478bd9Sstevel@tonic-gate 	return (checksum);
15067c478bd9Sstevel@tonic-gate }
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate /*
15097c478bd9Sstevel@tonic-gate  * creat_hdr: Fill in the generic header structure with the specific
15107c478bd9Sstevel@tonic-gate  *            header information based on the value of Hdr_type.
15117c478bd9Sstevel@tonic-gate  *
15127c478bd9Sstevel@tonic-gate  *            return (1) if this process was successful, and (0) otherwise.
15137c478bd9Sstevel@tonic-gate  */
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate static int
15167c478bd9Sstevel@tonic-gate creat_hdr(void)
15177c478bd9Sstevel@tonic-gate {
15187c478bd9Sstevel@tonic-gate 	ushort_t ftype;
15197c478bd9Sstevel@tonic-gate 	int fullnamesize;
15207c478bd9Sstevel@tonic-gate 	dev_t dev;
15217c478bd9Sstevel@tonic-gate 	ino_t ino;
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 	ftype = SrcSt.st_mode & Ftype;
15247c478bd9Sstevel@tonic-gate 	Adir = (ftype == S_IFDIR);
1525cdd68f5aSceastha 	Aspec = (ftype == S_IFBLK || ftype == S_IFCHR || ftype == S_IFIFO ||
1526cdd68f5aSceastha 	    ftype == S_IFSOCK);
15277c478bd9Sstevel@tonic-gate 	switch (Hdr_type) {
15287c478bd9Sstevel@tonic-gate 		case BIN:
15297c478bd9Sstevel@tonic-gate 			Gen.g_magic = CMN_BIN;
15307c478bd9Sstevel@tonic-gate 			break;
15317c478bd9Sstevel@tonic-gate 		case CHR:
15327c478bd9Sstevel@tonic-gate 			Gen.g_magic = CMN_BIN;
15337c478bd9Sstevel@tonic-gate 			break;
15347c478bd9Sstevel@tonic-gate 		case ASC:
15357c478bd9Sstevel@tonic-gate 			Gen.g_magic = CMN_ASC;
15367c478bd9Sstevel@tonic-gate 			break;
15377c478bd9Sstevel@tonic-gate 		case CRC:
15387c478bd9Sstevel@tonic-gate 			Gen.g_magic = CMN_CRC;
15397c478bd9Sstevel@tonic-gate 			break;
15407c478bd9Sstevel@tonic-gate 		case USTAR:
15417c478bd9Sstevel@tonic-gate 			/*
15427c478bd9Sstevel@tonic-gate 			 * If the length of the full name is greater than 256,
15437c478bd9Sstevel@tonic-gate 			 * print out a message and return.
15447c478bd9Sstevel@tonic-gate 			 */
15457c478bd9Sstevel@tonic-gate 			if ((fullnamesize = strlen(Gen.g_nam_p)) > MAXNAM) {
15467c478bd9Sstevel@tonic-gate 				msg(ERR,
15477c478bd9Sstevel@tonic-gate 				    "%s: file name too long", Gen.g_nam_p);
15487c478bd9Sstevel@tonic-gate 				return (0);
15497c478bd9Sstevel@tonic-gate 			} else if (fullnamesize > NAMSIZ) {
15507c478bd9Sstevel@tonic-gate 				/*
15517c478bd9Sstevel@tonic-gate 				 * The length of the full name is greater than
15527c478bd9Sstevel@tonic-gate 				 * 100, so we must split the filename from the
15537c478bd9Sstevel@tonic-gate 				 * path
15547c478bd9Sstevel@tonic-gate 				 */
15557c478bd9Sstevel@tonic-gate 				char namebuff[NAMSIZ+1];
15567c478bd9Sstevel@tonic-gate 				char prebuff[PRESIZ+1];
15577c478bd9Sstevel@tonic-gate 				char *lastslash;
15587c478bd9Sstevel@tonic-gate 				int presize, namesize;
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 				(void) memset(namebuff, '\0',
15617c478bd9Sstevel@tonic-gate 				    sizeof (namebuff));
15627c478bd9Sstevel@tonic-gate 				(void) memset(prebuff, '\0', sizeof (prebuff));
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 				lastslash = strrchr(Gen.g_nam_p, '/');
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 				if (lastslash != NULL) {
15677c478bd9Sstevel@tonic-gate 					namesize = strlen(++lastslash);
15687c478bd9Sstevel@tonic-gate 					presize = fullnamesize - namesize - 1;
15697c478bd9Sstevel@tonic-gate 				} else {
15707c478bd9Sstevel@tonic-gate 					namesize = fullnamesize;
15717c478bd9Sstevel@tonic-gate 					lastslash = Gen.g_nam_p;
15727c478bd9Sstevel@tonic-gate 					presize = 0;
15737c478bd9Sstevel@tonic-gate 				}
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 				/*
15767c478bd9Sstevel@tonic-gate 				 * If the filename is greater than 100 we can't
15777c478bd9Sstevel@tonic-gate 				 * archive the file
15787c478bd9Sstevel@tonic-gate 				 */
15797c478bd9Sstevel@tonic-gate 				if (namesize > NAMSIZ) {
15807c478bd9Sstevel@tonic-gate 					msg(ERR,
15817c478bd9Sstevel@tonic-gate 					    "%s: filename is greater than %d",
15827c478bd9Sstevel@tonic-gate 					    lastslash, NAMSIZ);
15837c478bd9Sstevel@tonic-gate 					return (0);
15847c478bd9Sstevel@tonic-gate 				}
15857c478bd9Sstevel@tonic-gate 				(void) strncpy(&namebuff[0], lastslash,
15867c478bd9Sstevel@tonic-gate 				    namesize);
15877c478bd9Sstevel@tonic-gate 				/*
15887c478bd9Sstevel@tonic-gate 				 * If the prefix is greater than 155 we can't
15897c478bd9Sstevel@tonic-gate 				 * archive the file.
15907c478bd9Sstevel@tonic-gate 				 */
15917c478bd9Sstevel@tonic-gate 				if (presize > PRESIZ) {
15927c478bd9Sstevel@tonic-gate 					msg(ERR,
15937c478bd9Sstevel@tonic-gate 					    "%s: prefix is greater than %d",
15947c478bd9Sstevel@tonic-gate 					    Gen.g_nam_p, PRESIZ);
15957c478bd9Sstevel@tonic-gate 					return (0);
15967c478bd9Sstevel@tonic-gate 				}
15977c478bd9Sstevel@tonic-gate 				(void) strncpy(&prebuff[0], Gen.g_nam_p,
15987c478bd9Sstevel@tonic-gate 				    presize);
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 				Gen.g_tname = e_zalloc(E_EXIT, namesize + 1);
16017c478bd9Sstevel@tonic-gate 				(void) strcpy(Gen.g_tname, namebuff);
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate 				Gen.g_prefix = e_zalloc(E_EXIT, presize + 1);
16047c478bd9Sstevel@tonic-gate 				(void) strcpy(Gen.g_prefix, prebuff);
16057c478bd9Sstevel@tonic-gate 			} else {
16067c478bd9Sstevel@tonic-gate 				Gen.g_tname = Gen.g_nam_p;
16077c478bd9Sstevel@tonic-gate 			}
16087c478bd9Sstevel@tonic-gate 			(void) strcpy(Gen.g_tmagic, "ustar");
16097c478bd9Sstevel@tonic-gate 			(void) strcpy(Gen.g_version, "00");
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 			dpasswd = getpwuid(SrcSt.st_uid);
1612ced83f9bSceastha 			if (dpasswd == NULL) {
16137c478bd9Sstevel@tonic-gate 				msg(EPOST,
16147c478bd9Sstevel@tonic-gate 				    "cpio: could not get passwd information "
16157c478bd9Sstevel@tonic-gate 				    "for %s%s%s",
1616ced83f9bSceastha 				    (Gen.g_attrnam_p == NULL) ?
16177c478bd9Sstevel@tonic-gate 				    Gen.g_nam_p : Gen.g_attrfnam_p,
1618ced83f9bSceastha 				    (Gen.g_attrnam_p == NULL) ?
1619ced83f9bSceastha 				    "" : Gen.g_rw_sysattr ?
1620ced83f9bSceastha 				    gettext(" System Attribute ") :
1621ced83f9bSceastha 				    gettext(" Attribute "),
1622ced83f9bSceastha 				    (Gen.g_attrnam_p == NULL) ?
16237c478bd9Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
16247c478bd9Sstevel@tonic-gate 				/* make name null string */
16257c478bd9Sstevel@tonic-gate 				Gen.g_uname[0] = '\0';
16267c478bd9Sstevel@tonic-gate 			} else {
16277c478bd9Sstevel@tonic-gate 				(void) strncpy(&Gen.g_uname[0],
16287c478bd9Sstevel@tonic-gate 				    dpasswd->pw_name, 32);
16297c478bd9Sstevel@tonic-gate 			}
16307c478bd9Sstevel@tonic-gate 			dgroup = getgrgid(SrcSt.st_gid);
1631ced83f9bSceastha 			if (dgroup == NULL) {
16327c478bd9Sstevel@tonic-gate 				msg(EPOST,
16337c478bd9Sstevel@tonic-gate 				    "cpio: could not get group information "
16345fbb8099SNobutomo Nakano 				    "for %s%s%s",
1635ced83f9bSceastha 				    (Gen.g_attrnam_p == NULL) ?
16367c478bd9Sstevel@tonic-gate 				    Gen.g_nam_p : Gen.g_attrfnam_p,
1637ced83f9bSceastha 				    (Gen.g_attrnam_p == NULL) ?
1638ced83f9bSceastha 				    "" : Gen.g_rw_sysattr ?
1639ced83f9bSceastha 				    gettext(" System Attribute ") :
1640ced83f9bSceastha 				    gettext(" Attribute "),
1641ced83f9bSceastha 				    (Gen.g_attrnam_p == NULL) ?
16427c478bd9Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
16437c478bd9Sstevel@tonic-gate 				/* make name null string */
16447c478bd9Sstevel@tonic-gate 				Gen.g_gname[0] = '\0';
16457c478bd9Sstevel@tonic-gate 			} else {
16467c478bd9Sstevel@tonic-gate 				(void) strncpy(&Gen.g_gname[0],
16477c478bd9Sstevel@tonic-gate 				    dgroup->gr_name, 32);
16487c478bd9Sstevel@tonic-gate 			}
16497c478bd9Sstevel@tonic-gate 			Gen.g_typeflag = tartype(ftype);
16507c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
16517c478bd9Sstevel@tonic-gate 		case TAR:
16527c478bd9Sstevel@tonic-gate 			(void) memset(T_lname, '\0', sizeof (T_lname));
16537c478bd9Sstevel@tonic-gate 			break;
16547c478bd9Sstevel@tonic-gate 		default:
16557c478bd9Sstevel@tonic-gate 			msg(EXT, "Impossible header type.");
16567c478bd9Sstevel@tonic-gate 	}
16577c478bd9Sstevel@tonic-gate 
1658ced83f9bSceastha 	if (Use_old_stat && (Gen.g_attrnam_p != NULL)) {
1659ced83f9bSceastha 		/*
1660ced83f9bSceastha 		 * When processing extended attributes, creat_hdr()
1661ced83f9bSceastha 		 * can get called multiple times which means that
1662ced83f9bSceastha 		 * SrcSt.st.st_dev would have gotten converted to
1663ced83f9bSceastha 		 * -Hodc format.  We should always use the original
1664ced83f9bSceastha 		 * device here as we need to be able to match on
1665ced83f9bSceastha 		 * the original device id from the file that was
1666ced83f9bSceastha 		 * previewed in sl_preview_synonyms().
1667ced83f9bSceastha 		 */
1668ced83f9bSceastha 		dev = Savedev;
1669ced83f9bSceastha 	} else {
16707c478bd9Sstevel@tonic-gate 		dev = SrcSt.st_dev;
1671ced83f9bSceastha 	}
16727c478bd9Sstevel@tonic-gate 	ino = SrcSt.st_ino;
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	if (Use_old_stat) {
16757c478bd9Sstevel@tonic-gate 		SrcSt = *OldSt;
16767c478bd9Sstevel@tonic-gate 	}
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 	Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
16797c478bd9Sstevel@tonic-gate 	Gen.g_uid = SrcSt.st_uid;
16807c478bd9Sstevel@tonic-gate 	Gen.g_gid = SrcSt.st_gid;
16817c478bd9Sstevel@tonic-gate 	Gen.g_dev = SrcSt.st_dev;
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate 	if (Use_old_stat) {
16847c478bd9Sstevel@tonic-gate 		/* -Hodc */
16857c478bd9Sstevel@tonic-gate 
1686ced83f9bSceastha 		sl_info_t *p = sl_search(dev, ino, ftype);
16877c478bd9Sstevel@tonic-gate 		Gen.g_ino = p ? p->sl_ino2 : -1;
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 		if (Gen.g_ino == (ulong_t)-1) {
16907c478bd9Sstevel@tonic-gate 			msg(ERR, "%s%s%s: cannot be archived - inode too big "
16917c478bd9Sstevel@tonic-gate 			    "for -Hodc format",
1692ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ?
16937c478bd9Sstevel@tonic-gate 			    Gen.g_nam_p : Gen.g_attrfnam_p,
1694ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ? "" : Gen.g_rw_sysattr ?
1695ced83f9bSceastha 			    gettext(" System Attribute ") :
1696ced83f9bSceastha 			    gettext(" Attribute "),
1697ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ? "" : Gen.g_attrnam_p);
16987c478bd9Sstevel@tonic-gate 			return (0);
16997c478bd9Sstevel@tonic-gate 		}
17007c478bd9Sstevel@tonic-gate 	} else {
17017c478bd9Sstevel@tonic-gate 		Gen.g_ino = SrcSt.st_ino;
17027c478bd9Sstevel@tonic-gate 	}
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate 	Gen.g_mode = SrcSt.st_mode;
17057c478bd9Sstevel@tonic-gate 	Gen.g_mtime = SrcSt.st_mtime;
1706ced83f9bSceastha 	Gen.g_nlink = Adir ? SrcSt.st_nlink : sl_numlinks(dev, ino, ftype);
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate 	if (ftype == S_IFREG || ftype == S_IFLNK)
17097c478bd9Sstevel@tonic-gate 		Gen.g_filesz = (off_t)SrcSt.st_size;
17107c478bd9Sstevel@tonic-gate 	else
17117c478bd9Sstevel@tonic-gate 		Gen.g_filesz = (off_t)0;
17127c478bd9Sstevel@tonic-gate 	Gen.g_rdev = SrcSt.st_rdev;
17137c478bd9Sstevel@tonic-gate 	return (1);
17147c478bd9Sstevel@tonic-gate }
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate /*
17177c478bd9Sstevel@tonic-gate  * creat_lnk: Create a link from the existing name1_p to name2_p.
17187c478bd9Sstevel@tonic-gate  */
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate static
17217c478bd9Sstevel@tonic-gate int
17227c478bd9Sstevel@tonic-gate creat_lnk(int dirfd, char *name1_p, char *name2_p)
17237c478bd9Sstevel@tonic-gate {
17247c478bd9Sstevel@tonic-gate 	int cnt = 0;
17257c478bd9Sstevel@tonic-gate 
17267c478bd9Sstevel@tonic-gate 	do {
17277c478bd9Sstevel@tonic-gate 		errno = 0;
17287c478bd9Sstevel@tonic-gate 		if (!link(name1_p, name2_p)) {
17297c478bd9Sstevel@tonic-gate 			if (aclp != NULL) {
1730fa9e4066Sahrens 				acl_free(aclp);
17317c478bd9Sstevel@tonic-gate 				aclp = NULL;
1732fa9e4066Sahrens 				acl_is_set = 0;
17337c478bd9Sstevel@tonic-gate 			}
17347c478bd9Sstevel@tonic-gate 			cnt = 0;
17357c478bd9Sstevel@tonic-gate 			break;
1736647ab8f4Sceastha 		} else if ((errno == EEXIST) && (cnt == 0)) {
17377c478bd9Sstevel@tonic-gate 			struct stat lsb1;
17387c478bd9Sstevel@tonic-gate 			struct stat lsb2;
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate 			/*
17417c478bd9Sstevel@tonic-gate 			 * Check to see if we are trying to link this
17427c478bd9Sstevel@tonic-gate 			 * file to itself.  If so, count the effort as
17437c478bd9Sstevel@tonic-gate 			 * successful.  If the two files are different,
17447c478bd9Sstevel@tonic-gate 			 * or if either lstat is unsuccessful, proceed
17457c478bd9Sstevel@tonic-gate 			 * as we would have otherwise; the appropriate
17467c478bd9Sstevel@tonic-gate 			 * error will be reported subsequently.
17477c478bd9Sstevel@tonic-gate 			 */
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate 			if (lstat(name1_p, &lsb1) != 0) {
17507c478bd9Sstevel@tonic-gate 				msg(ERR, "Cannot lstat source file %s",
17517c478bd9Sstevel@tonic-gate 				    name1_p);
17527c478bd9Sstevel@tonic-gate 			} else {
17537c478bd9Sstevel@tonic-gate 				if (lstat(name2_p, &lsb2) != 0) {
17547c478bd9Sstevel@tonic-gate 					msg(ERR, "Cannot lstat "
1755647ab8f4Sceastha 					    "destination file %s", name2_p);
17567c478bd9Sstevel@tonic-gate 				} else {
1757647ab8f4Sceastha 					if (lsb1.st_dev == lsb2.st_dev &&
1758647ab8f4Sceastha 					    lsb1.st_ino == lsb2.st_ino) {
1759647ab8f4Sceastha 						VERBOSE((Args & (OCv | OCV)),
17607c478bd9Sstevel@tonic-gate 						    name2_p);
17617c478bd9Sstevel@tonic-gate 						return (0);
17627c478bd9Sstevel@tonic-gate 					}
17637c478bd9Sstevel@tonic-gate 				}
17647c478bd9Sstevel@tonic-gate 			}
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 			if (!(Args & OCu) && G_p->g_mtime <= DesSt.st_mtime)
1767647ab8f4Sceastha 				msg(ERR, "Existing \"%s\" same age or newer",
17687c478bd9Sstevel@tonic-gate 				    name2_p);
17697c478bd9Sstevel@tonic-gate 			else if (unlinkat(dirfd, get_component(name2_p), 0) < 0)
17707c478bd9Sstevel@tonic-gate 				msg(ERRN, "Error cannot unlink \"%s\"",
17717c478bd9Sstevel@tonic-gate 				    name2_p);
17727c478bd9Sstevel@tonic-gate 		}
17737c478bd9Sstevel@tonic-gate 		cnt++;
17747c478bd9Sstevel@tonic-gate 	} while ((cnt < 2) && missdir(name2_p) == 0);
17757c478bd9Sstevel@tonic-gate 	if (!cnt) {
17767c478bd9Sstevel@tonic-gate 		char *newname;
17777c478bd9Sstevel@tonic-gate 		char *fromname;
17787c478bd9Sstevel@tonic-gate 		char *attrname;
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 		newname = name2_p;
17817c478bd9Sstevel@tonic-gate 		fromname = name1_p;
17827c478bd9Sstevel@tonic-gate 		attrname = Gen.g_attrnam_p;
17837c478bd9Sstevel@tonic-gate 		if (attrname) {
17847c478bd9Sstevel@tonic-gate 			if (Args & OCp) {
17857c478bd9Sstevel@tonic-gate 				newname = fromname = Fullnam_p;
17867c478bd9Sstevel@tonic-gate 			} else {
17877c478bd9Sstevel@tonic-gate 				newname = Gen.g_attrfnam_p;
17887c478bd9Sstevel@tonic-gate 			}
17897c478bd9Sstevel@tonic-gate 		}
17907c478bd9Sstevel@tonic-gate 		if (Args & OCv) {
17917c478bd9Sstevel@tonic-gate 			(void) fprintf(Err_p,
17927c478bd9Sstevel@tonic-gate 			    gettext("%s%s%s linked to %s%s%s\n"), newname,
1793ced83f9bSceastha 			    (attrname == NULL) ? "" : gettext(" attribute "),
1794ced83f9bSceastha 			    (attrname == NULL) ? "" : attrname,
1795ced83f9bSceastha 			    (attrname == NULL) ? fromname : newname,
1796ced83f9bSceastha 			    (attrname == NULL) ? "" : gettext(" attribute "),
1797ced83f9bSceastha 			    (attrname == NULL) ? "" : name1_p);
1798ced83f9bSceastha 		} else {
17997c478bd9Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), newname);
1800ced83f9bSceastha 		}
18017c478bd9Sstevel@tonic-gate 	} else if (cnt == 1)
18027c478bd9Sstevel@tonic-gate 		msg(ERRN,
18037c478bd9Sstevel@tonic-gate 		    "Unable to create directory for \"%s\"", name2_p);
18047c478bd9Sstevel@tonic-gate 	else if (cnt == 2)
18057c478bd9Sstevel@tonic-gate 		msg(ERRN,
18067c478bd9Sstevel@tonic-gate 		    "Cannot link \"%s\" and \"%s\"", name1_p, name2_p);
18077c478bd9Sstevel@tonic-gate 	return (cnt);
18087c478bd9Sstevel@tonic-gate }
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate /*
18117c478bd9Sstevel@tonic-gate  * creat_spec:
18127c478bd9Sstevel@tonic-gate  *   Create one of the following:
18137c478bd9Sstevel@tonic-gate  *       directory
18147c478bd9Sstevel@tonic-gate  *       character special file
18157c478bd9Sstevel@tonic-gate  *       block special file
18167c478bd9Sstevel@tonic-gate  *       fifo
1817cdd68f5aSceastha  *	 socket
18187c478bd9Sstevel@tonic-gate  */
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate static int
18217c478bd9Sstevel@tonic-gate creat_spec(int dirfd)
18227c478bd9Sstevel@tonic-gate {
18237c478bd9Sstevel@tonic-gate 	char *nam_p;
18247c478bd9Sstevel@tonic-gate 	int cnt, result, rv = 0;
18257c478bd9Sstevel@tonic-gate 	char *curdir;
18267c478bd9Sstevel@tonic-gate 	char *lastslash;
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate 	Do_rename = 0;	/* creat_tmp() may reset this */
18297c478bd9Sstevel@tonic-gate 
18307c478bd9Sstevel@tonic-gate 	if (Args & OCp) {
18317c478bd9Sstevel@tonic-gate 		nam_p = Fullnam_p;
18327c478bd9Sstevel@tonic-gate 	} else {
18337c478bd9Sstevel@tonic-gate 		nam_p = G_p->g_nam_p;
18347c478bd9Sstevel@tonic-gate 	}
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate 	/*
18377c478bd9Sstevel@tonic-gate 	 * Is this the extraction of the hidden attribute directory?
1838ced83f9bSceastha 	 * If we are processing the hidden attribute directory of an
1839ced83f9bSceastha 	 * attribute, then just return as modes and times cannot be set.
1840ced83f9bSceastha 	 * Otherwise, if we are processing a hidden attribute, just set
1841ced83f9bSceastha 	 * the mode/times correctly and return.
18427c478bd9Sstevel@tonic-gate 	 */
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 	if (Hiddendir) {
1845ced83f9bSceastha 		if (G_p->g_attrparent_p == NULL) {
1846647ab8f4Sceastha 			if (Args & OCR) {
1847647ab8f4Sceastha 				if (fchownat(dirfd, ".", Rpw_p->pw_uid,
1848647ab8f4Sceastha 				    Rpw_p->pw_gid, 0) != 0) {
1849647ab8f4Sceastha 					msg(ERRN,
1850ced83f9bSceastha 					    "Cannot chown() \"attribute "
1851ced83f9bSceastha 					    "directory of file %s\"",
1852ced83f9bSceastha 					    G_p->g_attrfnam_p);
1853647ab8f4Sceastha 				}
1854647ab8f4Sceastha 			} else if ((fchownat(dirfd, ".", G_p->g_uid,
1855647ab8f4Sceastha 			    G_p->g_gid, 0) != 0) && privileged) {
18567c478bd9Sstevel@tonic-gate 				msg(ERRN,
18577c478bd9Sstevel@tonic-gate 				    "Cannot chown() \"attribute directory of "
18587c478bd9Sstevel@tonic-gate 				    "file %s\"", G_p->g_attrfnam_p);
18597c478bd9Sstevel@tonic-gate 			}
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 			if (fchmod(dirfd, G_p->g_mode) != 0) {
18627c478bd9Sstevel@tonic-gate 				msg(ERRN,
18637c478bd9Sstevel@tonic-gate 				    "Cannot chmod() \"attribute directory of "
18647c478bd9Sstevel@tonic-gate 				    "file %s\"", G_p->g_attrfnam_p);
18657c478bd9Sstevel@tonic-gate 			}
18667c478bd9Sstevel@tonic-gate 
1867fa9e4066Sahrens 			acl_is_set = 0;
18687c478bd9Sstevel@tonic-gate 			if (Pflag && aclp != NULL) {
1869fa9e4066Sahrens 				if (facl_set(dirfd, aclp) < 0) {
18707c478bd9Sstevel@tonic-gate 					msg(ERRN,
18717c478bd9Sstevel@tonic-gate 					    "failed to set acl on attribute"
1872ced83f9bSceastha 					    " directory of %s ",
1873ced83f9bSceastha 					    G_p->g_attrfnam_p);
18747c478bd9Sstevel@tonic-gate 				} else {
1875fa9e4066Sahrens 					acl_is_set = 1;
18767c478bd9Sstevel@tonic-gate 				}
1877fa9e4066Sahrens 				acl_free(aclp);
18787c478bd9Sstevel@tonic-gate 				aclp = NULL;
18797c478bd9Sstevel@tonic-gate 			}
1880ced83f9bSceastha 		}
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate 		return (1);
18837c478bd9Sstevel@tonic-gate 	}
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 	result = stat(nam_p, &DesSt);
18867c478bd9Sstevel@tonic-gate 
18877c478bd9Sstevel@tonic-gate 	if (ustar_dir() || Adir) {
18887c478bd9Sstevel@tonic-gate 		/*
18897c478bd9Sstevel@tonic-gate 		 *  The archive file is a directory.
18907c478bd9Sstevel@tonic-gate 		 *  Skip "." and ".."
18917c478bd9Sstevel@tonic-gate 		 */
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate 		curdir = strrchr(nam_p, '.');
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate 		if (curdir != NULL && curdir[1] == NULL) {
18967c478bd9Sstevel@tonic-gate 			lastslash = strrchr(nam_p, '/');
18977c478bd9Sstevel@tonic-gate 
18987c478bd9Sstevel@tonic-gate 			if (lastslash != NULL) {
18997c478bd9Sstevel@tonic-gate 				lastslash++;
19007c478bd9Sstevel@tonic-gate 			} else {
19017c478bd9Sstevel@tonic-gate 				lastslash = nam_p;
19027c478bd9Sstevel@tonic-gate 			}
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate 			if (!(strcmp(lastslash, ".")) ||
19057c478bd9Sstevel@tonic-gate 			    !(strcmp(lastslash, ".."))) {
19067c478bd9Sstevel@tonic-gate 				return (1);
19077c478bd9Sstevel@tonic-gate 			}
19087c478bd9Sstevel@tonic-gate 		}
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate 		if (result == 0) {
19117c478bd9Sstevel@tonic-gate 			/* A file by the same name exists. */
19127c478bd9Sstevel@tonic-gate 
19137c478bd9Sstevel@tonic-gate 			/* Take care of ACLs */
1914fa9e4066Sahrens 			acl_is_set = 0;
19157c478bd9Sstevel@tonic-gate 
19167c478bd9Sstevel@tonic-gate 			if (Pflag && aclp != NULL) {
1917fa9e4066Sahrens 				if (acl_set(nam_p, aclp) < 0) {
19187c478bd9Sstevel@tonic-gate 					msg(ERRN,
19197c478bd9Sstevel@tonic-gate 					    "\"%s\": failed to set acl",
19207c478bd9Sstevel@tonic-gate 					    nam_p);
19217c478bd9Sstevel@tonic-gate 				} else {
1922fa9e4066Sahrens 					acl_is_set = 1;
19237c478bd9Sstevel@tonic-gate 				}
19247c478bd9Sstevel@tonic-gate 
1925fa9e4066Sahrens 				acl_free(aclp);
19267c478bd9Sstevel@tonic-gate 				aclp = NULL;
19277c478bd9Sstevel@tonic-gate 			}
19287c478bd9Sstevel@tonic-gate 			if (Args & OCd) {
19297c478bd9Sstevel@tonic-gate 				/*
19307c478bd9Sstevel@tonic-gate 				 * We are creating directories.  Keep the
19317c478bd9Sstevel@tonic-gate 				 * existing file.
19327c478bd9Sstevel@tonic-gate 				 */
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 				rstfiles(U_KEEP, dirfd);
19357c478bd9Sstevel@tonic-gate 			}
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate 			/* Report success. */
19387c478bd9Sstevel@tonic-gate 
19397c478bd9Sstevel@tonic-gate 			return (1);
19407c478bd9Sstevel@tonic-gate 		}
19417c478bd9Sstevel@tonic-gate 	} else {
19427c478bd9Sstevel@tonic-gate 		/* The archive file is not a directory. */
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate 		if (result == 0) {
19457c478bd9Sstevel@tonic-gate 			/*
19467c478bd9Sstevel@tonic-gate 			 * A file by the same name exists.  Move it to a
19477c478bd9Sstevel@tonic-gate 			 * temporary file.
19487c478bd9Sstevel@tonic-gate 			 */
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate 			if (creat_tmp(nam_p) < 0) {
19517c478bd9Sstevel@tonic-gate 				/*
19527c478bd9Sstevel@tonic-gate 				 * We weren't able to create the temp file.
19537c478bd9Sstevel@tonic-gate 				 * Report failure.
19547c478bd9Sstevel@tonic-gate 				 */
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 				return (0);
19577c478bd9Sstevel@tonic-gate 			}
19587c478bd9Sstevel@tonic-gate 		}
19597c478bd9Sstevel@tonic-gate 	}
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	/*
19627c478bd9Sstevel@tonic-gate 	 * This pile tries to create the file directly, and, if there is a
19637c478bd9Sstevel@tonic-gate 	 * problem, creates missing directories, and then tries to create the
19647c478bd9Sstevel@tonic-gate 	 * file again.  Two strikes and you're out.
19657c478bd9Sstevel@tonic-gate 	 */
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate 	cnt = 0;
19687c478bd9Sstevel@tonic-gate 
19697c478bd9Sstevel@tonic-gate 	do {
19707c478bd9Sstevel@tonic-gate 		if (ustar_dir() || Adir) {
19717c478bd9Sstevel@tonic-gate 			/* The archive file is a directory. */
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 			result = mkdir(nam_p, G_p->g_mode);
19747c478bd9Sstevel@tonic-gate 		} else if (ustar_spec() || Aspec) {
19757c478bd9Sstevel@tonic-gate 			/*
19767c478bd9Sstevel@tonic-gate 			 * The archive file is block special,
1977cdd68f5aSceastha 			 * char special, socket, or a fifo.
1978cdd68f5aSceastha 			 * Note that, for a socket, the third
1979cdd68f5aSceastha 			 * parameter to mknod() is ignored.
19807c478bd9Sstevel@tonic-gate 			 */
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 			result = mknod(nam_p, (int)G_p->g_mode,
19837c478bd9Sstevel@tonic-gate 			    (int)G_p->g_rdev);
19847c478bd9Sstevel@tonic-gate 		}
19857c478bd9Sstevel@tonic-gate 
19867c478bd9Sstevel@tonic-gate 		if (result >= 0) {
19877c478bd9Sstevel@tonic-gate 			/*
19887c478bd9Sstevel@tonic-gate 			 * The file creation succeeded.  Take care of the ACLs.
19897c478bd9Sstevel@tonic-gate 			 */
19907c478bd9Sstevel@tonic-gate 
1991fa9e4066Sahrens 			acl_is_set = 0;
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate 			if (Pflag && aclp != NULL) {
1994fa9e4066Sahrens 				if (acl_set(nam_p, aclp) < 0) {
19957c478bd9Sstevel@tonic-gate 					msg(ERRN,
19967c478bd9Sstevel@tonic-gate 					    "\"%s\": failed to set acl", nam_p);
19977c478bd9Sstevel@tonic-gate 				} else {
1998fa9e4066Sahrens 					acl_is_set = 1;
19997c478bd9Sstevel@tonic-gate 				}
20007c478bd9Sstevel@tonic-gate 
2001fa9e4066Sahrens 				acl_free(aclp);
20027c478bd9Sstevel@tonic-gate 				aclp = NULL;
20037c478bd9Sstevel@tonic-gate 			}
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate 			cnt = 0;
20067c478bd9Sstevel@tonic-gate 			break;
20077c478bd9Sstevel@tonic-gate 		}
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 		cnt++;
20107c478bd9Sstevel@tonic-gate 	} while (cnt < 2 && missdir(nam_p) == 0);
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 	switch (cnt) {
20137c478bd9Sstevel@tonic-gate 	case 0:
20147c478bd9Sstevel@tonic-gate 		rv = 1;
20157c478bd9Sstevel@tonic-gate 		rstfiles(U_OVER, dirfd);
20167c478bd9Sstevel@tonic-gate 		break;
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 	case 1:
20197c478bd9Sstevel@tonic-gate 		msg(ERRN,
20207c478bd9Sstevel@tonic-gate 		    "Cannot create directory for \"%s\"", nam_p);
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 		if (*Over_p == '\0') {
20237c478bd9Sstevel@tonic-gate 			rstfiles(U_KEEP, dirfd);
20247c478bd9Sstevel@tonic-gate 		}
20257c478bd9Sstevel@tonic-gate 
20267c478bd9Sstevel@tonic-gate 		break;
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 	case 2:
20297c478bd9Sstevel@tonic-gate 		if (ustar_dir() || Adir) {
20307c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot create directory \"%s\"", nam_p);
20317c478bd9Sstevel@tonic-gate 		} else if (ustar_spec() || Aspec) {
20327c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot mknod() \"%s\"", nam_p);
20337c478bd9Sstevel@tonic-gate 		}
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate 		if (*Over_p == '\0') {
20367c478bd9Sstevel@tonic-gate 			rstfiles(U_KEEP, dirfd);
20377c478bd9Sstevel@tonic-gate 		}
20387c478bd9Sstevel@tonic-gate 
20397c478bd9Sstevel@tonic-gate 		break;
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate 	default:
20427c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible case.");
20437c478bd9Sstevel@tonic-gate 	}
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate 	return (rv);
20467c478bd9Sstevel@tonic-gate }
20477c478bd9Sstevel@tonic-gate 
20487c478bd9Sstevel@tonic-gate /*
20497c478bd9Sstevel@tonic-gate  * creat_tmp:
20507c478bd9Sstevel@tonic-gate  */
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate static int
20537c478bd9Sstevel@tonic-gate creat_tmp(char *nam_p)
20547c478bd9Sstevel@tonic-gate {
20557c478bd9Sstevel@tonic-gate 	char *t_p;
20567c478bd9Sstevel@tonic-gate 	int	cwd;
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 	if ((Args & OCp) && G_p->g_ino == DesSt.st_ino &&
20597c478bd9Sstevel@tonic-gate 	    G_p->g_dev == DesSt.st_dev) {
20607c478bd9Sstevel@tonic-gate 		msg(ERR, "Attempt to pass a file to itself.");
20617c478bd9Sstevel@tonic-gate 		return (-1);
20627c478bd9Sstevel@tonic-gate 	}
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate 	if (G_p->g_mtime <= DesSt.st_mtime && !(Args & OCu)) {
20657c478bd9Sstevel@tonic-gate 		msg(ERR, "Existing \"%s\" same age or newer", nam_p);
20667c478bd9Sstevel@tonic-gate 		return (-1);
20677c478bd9Sstevel@tonic-gate 	}
20687c478bd9Sstevel@tonic-gate 
20697c478bd9Sstevel@tonic-gate 	/* Make the temporary file name. */
20707c478bd9Sstevel@tonic-gate 
20717c478bd9Sstevel@tonic-gate 	(void) strcpy(Over_p, nam_p);
20727c478bd9Sstevel@tonic-gate 	t_p = Over_p + strlen(Over_p);
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate 	while (t_p != Over_p) {
20757c478bd9Sstevel@tonic-gate 		if (*(t_p - 1) == '/')
20767c478bd9Sstevel@tonic-gate 			break;
20777c478bd9Sstevel@tonic-gate 		t_p--;
20787c478bd9Sstevel@tonic-gate 	}
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 	(void) strcpy(t_p, "XXXXXX");
20817c478bd9Sstevel@tonic-gate 
2082ced83f9bSceastha 	if (G_p->g_attrnam_p != NULL) {
20837c478bd9Sstevel@tonic-gate 		/*
20847c478bd9Sstevel@tonic-gate 		 * Save our current directory, so we can go into
20857c478bd9Sstevel@tonic-gate 		 * the attribute directory to make the temp file
20867c478bd9Sstevel@tonic-gate 		 * and then return.
20877c478bd9Sstevel@tonic-gate 		 */
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate 		cwd = save_cwd();
20907c478bd9Sstevel@tonic-gate 		(void) fchdir(G_p->g_dirfd);
20917c478bd9Sstevel@tonic-gate 	}
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	(void) mktemp(Over_p);
20947c478bd9Sstevel@tonic-gate 
2095ced83f9bSceastha 	if (G_p->g_attrnam_p != NULL) {
20967c478bd9Sstevel@tonic-gate 		/* Return to the current directory. */
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 		rest_cwd(cwd);
20997c478bd9Sstevel@tonic-gate 	}
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate 	if (*Over_p == '\0') {
21027c478bd9Sstevel@tonic-gate 		/* mktemp reports a failure. */
21037c478bd9Sstevel@tonic-gate 
21047c478bd9Sstevel@tonic-gate 		msg(ERR, "Cannot get temporary file name.");
21057c478bd9Sstevel@tonic-gate 		return (-1);
21067c478bd9Sstevel@tonic-gate 	}
21077c478bd9Sstevel@tonic-gate 
21087c478bd9Sstevel@tonic-gate 	/*
21097c478bd9Sstevel@tonic-gate 	 * If it's a regular file, write to the temporary file, and then rename
2110da6c28aaSamw 	 * in order to accommodate potential executables.
21117c478bd9Sstevel@tonic-gate 	 *
21127c478bd9Sstevel@tonic-gate 	 * Note: g_typeflag is only defined (set) for USTAR archive types.  It
21137c478bd9Sstevel@tonic-gate 	 * defaults to 0 in the cpio-format-regular file case, so this test
21147c478bd9Sstevel@tonic-gate 	 * succeeds.
21157c478bd9Sstevel@tonic-gate 	 */
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate 	if (G_p->g_typeflag == 0 &&
21187c478bd9Sstevel@tonic-gate 	    (DesSt.st_mode & (ulong_t)Ftype) == S_IFREG &&
21197c478bd9Sstevel@tonic-gate 	    (G_p->g_mode & (ulong_t)Ftype) == S_IFREG) {
21207c478bd9Sstevel@tonic-gate 		/*
21217c478bd9Sstevel@tonic-gate 		 * The archive file and the filesystem file are both regular
21227c478bd9Sstevel@tonic-gate 		 * files.  We write to the temporary file in this case.
21237c478bd9Sstevel@tonic-gate 		 */
21247c478bd9Sstevel@tonic-gate 
21257c478bd9Sstevel@tonic-gate 		if (Args & OCp) {
2126ced83f9bSceastha 			if (G_p->g_attrnam_p == NULL) {
21277c478bd9Sstevel@tonic-gate 				Fullnam_p = Over_p;
21287c478bd9Sstevel@tonic-gate 			} else {
21297c478bd9Sstevel@tonic-gate 				Attrfile_p = Over_p;
21307c478bd9Sstevel@tonic-gate 			}
21317c478bd9Sstevel@tonic-gate 		} else {
21327c478bd9Sstevel@tonic-gate 			G_p->g_nam_p = Over_p;
2133ced83f9bSceastha 			if (G_p->g_attrnam_p != NULL) {
21347c478bd9Sstevel@tonic-gate 				Attrfile_p = Over_p;
21357c478bd9Sstevel@tonic-gate 			}
21367c478bd9Sstevel@tonic-gate 		}
21377c478bd9Sstevel@tonic-gate 
2138ced83f9bSceastha 		if (G_p->g_attrnam_p == NULL) {
21397c478bd9Sstevel@tonic-gate 			Over_p = nam_p;
21407c478bd9Sstevel@tonic-gate 		} else {
21417c478bd9Sstevel@tonic-gate 			Over_p = G_p->g_attrnam_p;
21427c478bd9Sstevel@tonic-gate 		}
21437c478bd9Sstevel@tonic-gate 
21447c478bd9Sstevel@tonic-gate 		Do_rename = 1;
21457c478bd9Sstevel@tonic-gate 	} else {
21467c478bd9Sstevel@tonic-gate 		/*
21477c478bd9Sstevel@tonic-gate 		 * Either the archive file or the filesystem file is not a
21487c478bd9Sstevel@tonic-gate 		 * regular file.
21497c478bd9Sstevel@tonic-gate 		 */
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate 		Do_rename = 0;
21527c478bd9Sstevel@tonic-gate 
21537c478bd9Sstevel@tonic-gate 		if (S_ISDIR(DesSt.st_mode)) {
21547c478bd9Sstevel@tonic-gate 			/*
21557c478bd9Sstevel@tonic-gate 			 * The filesystem file is a directory.
21567c478bd9Sstevel@tonic-gate 			 *
21577c478bd9Sstevel@tonic-gate 			 * Save the current working directory because we will
21587c478bd9Sstevel@tonic-gate 			 * want to restore it back just in case remove_dir()
21597c478bd9Sstevel@tonic-gate 			 * fails or get confused about where we should be.
21607c478bd9Sstevel@tonic-gate 			 */
21617c478bd9Sstevel@tonic-gate 
21627c478bd9Sstevel@tonic-gate 			*Over_p = '\0';
21637c478bd9Sstevel@tonic-gate 			cwd = save_cwd();
21647c478bd9Sstevel@tonic-gate 
21657c478bd9Sstevel@tonic-gate 			if (remove_dir(nam_p) < 0) {
21667c478bd9Sstevel@tonic-gate 				msg(ERRN,
21677c478bd9Sstevel@tonic-gate 				    "Cannot remove the directory \"%s\"",
21687c478bd9Sstevel@tonic-gate 				    nam_p);
21697c478bd9Sstevel@tonic-gate 				/*
21707c478bd9Sstevel@tonic-gate 				 * Restore working directory back to the one
21717c478bd9Sstevel@tonic-gate 				 * saved earlier.
21727c478bd9Sstevel@tonic-gate 				 */
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 				rest_cwd(cwd);
21757c478bd9Sstevel@tonic-gate 				return (-1);
21767c478bd9Sstevel@tonic-gate 			}
21777c478bd9Sstevel@tonic-gate 
21787c478bd9Sstevel@tonic-gate 			/*
21797c478bd9Sstevel@tonic-gate 			 * Restore working directory back to the one
21807c478bd9Sstevel@tonic-gate 			 * saved earlier
21817c478bd9Sstevel@tonic-gate 			 */
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate 			rest_cwd(cwd);
21847c478bd9Sstevel@tonic-gate 		} else {
21857c478bd9Sstevel@tonic-gate 			/*
21867c478bd9Sstevel@tonic-gate 			 * The file is not a directory. Will use the original
21877c478bd9Sstevel@tonic-gate 			 * link/unlink construct, however, if the file is
21887c478bd9Sstevel@tonic-gate 			 * namefs, link would fail with EXDEV. Therefore, we
21897c478bd9Sstevel@tonic-gate 			 * use rename() first to back up the file.
21907c478bd9Sstevel@tonic-gate 			 */
21917c478bd9Sstevel@tonic-gate 			if (rename(nam_p, Over_p) < 0) {
21927c478bd9Sstevel@tonic-gate 				/*
21937c478bd9Sstevel@tonic-gate 				 * If rename failed, try old construction
21947c478bd9Sstevel@tonic-gate 				 * method.
21957c478bd9Sstevel@tonic-gate 				 */
21967c478bd9Sstevel@tonic-gate 				if (link(nam_p, Over_p) < 0) {
21977c478bd9Sstevel@tonic-gate 					msg(ERRN,
2198ced83f9bSceastha 					    "Cannot rename temporary file "
2199ced83f9bSceastha 					    "\"%s\" to \"%s\"", Over_p, nam_p);
22007c478bd9Sstevel@tonic-gate 					*Over_p = '\0';
22017c478bd9Sstevel@tonic-gate 					return (-1);
22027c478bd9Sstevel@tonic-gate 				}
22037c478bd9Sstevel@tonic-gate 
22047c478bd9Sstevel@tonic-gate 				if (unlink(nam_p) < 0) {
22057c478bd9Sstevel@tonic-gate 					msg(ERRN,
22067c478bd9Sstevel@tonic-gate 					    "Cannot unlink() current \"%s\"",
22077c478bd9Sstevel@tonic-gate 					    nam_p);
22087c478bd9Sstevel@tonic-gate 					(void) unlink(Over_p);
22097c478bd9Sstevel@tonic-gate 					*Over_p = '\0';
22107c478bd9Sstevel@tonic-gate 					return (-1);
22117c478bd9Sstevel@tonic-gate 				}
22127c478bd9Sstevel@tonic-gate 			}
22137c478bd9Sstevel@tonic-gate 		}
22147c478bd9Sstevel@tonic-gate 	}
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate 	return (1);
22177c478bd9Sstevel@tonic-gate }
22187c478bd9Sstevel@tonic-gate 
22197c478bd9Sstevel@tonic-gate /*
2220ced83f9bSceastha  * Copy the datasize amount of data from the input file to buffer.
2221ced83f9bSceastha  *
2222ced83f9bSceastha  * ifd		- Input file descriptor.
2223ced83f9bSceastha  * buffer	- Buffer (allocated by caller) to copy data to.
2224ced83f9bSceastha  * datasize	- The amount of data to read from the input file
2225ced83f9bSceastha  *		and copy to the buffer.
2226ced83f9bSceastha  * error	- When reading from an Archive file, indicates unreadable
2227ced83f9bSceastha  *		data was encountered, otherwise indicates errno.
2228ced83f9bSceastha  * data_in_info	- Information needed when called from data_in().
2229ced83f9bSceastha  */
2230ced83f9bSceastha static ssize_t
2231ced83f9bSceastha read_chunk(int ifd, char *buffer, size_t datasize, data_in_t *data_in_info)
2232ced83f9bSceastha {
2233ced83f9bSceastha 	if (Args & OCp) {
2234ced83f9bSceastha 		return (read(ifd, buffer, datasize));
2235ced83f9bSceastha 	} else {
2236ced83f9bSceastha 		FILL(datasize);
2237ced83f9bSceastha 		if (data_in_info->data_in_proc_mode != P_SKIP) {
2238ced83f9bSceastha 			if (Hdr_type == CRC)
2239ced83f9bSceastha 				data_in_info->data_in_cksumval += cksum(CRC,
2240ced83f9bSceastha 				    datasize, NULL);
2241ced83f9bSceastha 			if (data_in_info->data_in_swapfile)
2242ced83f9bSceastha 				swap(Buffr.b_out_p, datasize);
2243ced83f9bSceastha 
2244ced83f9bSceastha 
2245ced83f9bSceastha 			/*
2246ced83f9bSceastha 			 * if the bar archive is compressed, set up a pipe and
2247ced83f9bSceastha 			 * do the de-compression while reading in the file
2248ced83f9bSceastha 			 */
2249ced83f9bSceastha 			if (Hdr_type == BAR) {
2250ced83f9bSceastha 				if (data_in_info->data_in_compress_flag == 0 &&
2251ced83f9bSceastha 				    Compressed) {
2252ced83f9bSceastha 					setup_uncompress(
2253ced83f9bSceastha 					    &(data_in_info->data_in_pipef));
2254ced83f9bSceastha 					data_in_info->data_in_compress_flag++;
2255ced83f9bSceastha 				}
2256ced83f9bSceastha 			}
2257ced83f9bSceastha 		}
2258ced83f9bSceastha 		(void) memcpy(buffer, Buffr.b_out_p, datasize);
2259ced83f9bSceastha 		Buffr.b_out_p += datasize;
2260ced83f9bSceastha 		Buffr.b_cnt -= datasize;
2261ced83f9bSceastha 		return (datasize);
2262ced83f9bSceastha 	}
2263ced83f9bSceastha }
2264ced83f9bSceastha 
22655fbb8099SNobutomo Nakano /*
22665fbb8099SNobutomo Nakano  * Read as much data as we can.
22675fbb8099SNobutomo Nakano  *
22685fbb8099SNobutomo Nakano  * ifd		- input file descriptor.
22695fbb8099SNobutomo Nakano  * buf		- Buffer (allocated by caller) to copy data to.
22705fbb8099SNobutomo Nakano  * bytes	- The amount of data to read from the input file
22715fbb8099SNobutomo Nakano  *		and copy to the buffer.
22725fbb8099SNobutomo Nakano  * rdblocksz	- The size of the chunk of data to read.
22735fbb8099SNobutomo Nakano  *
22744e621e6cSNobutomo Nakano  * Return number of bytes failed to read.
22754e621e6cSNobutomo Nakano  * Return -1 when buffer is empty and read failed.
22765fbb8099SNobutomo Nakano  */
22775fbb8099SNobutomo Nakano static int
22785fbb8099SNobutomo Nakano read_bytes(int ifd, char *buf, size_t bytes, size_t rdblocksz,
22795fbb8099SNobutomo Nakano     data_in_t *data_in_info)
22805fbb8099SNobutomo Nakano {
22815fbb8099SNobutomo Nakano 	size_t	bytesread;
22825fbb8099SNobutomo Nakano 	ssize_t	got;
22835fbb8099SNobutomo Nakano 
22845fbb8099SNobutomo Nakano 	for (bytesread = 0; bytesread < bytes; bytesread += got) {
22855fbb8099SNobutomo Nakano 		/*
22865fbb8099SNobutomo Nakano 		 * Read the data from either the input file descriptor
22875fbb8099SNobutomo Nakano 		 * or the archive file.  read_chunk() will only return
22885fbb8099SNobutomo Nakano 		 * <= 0 if data_copy() was called from data_pass().
22895fbb8099SNobutomo Nakano 		 */
22905fbb8099SNobutomo Nakano 		if ((got = read_chunk(ifd, buf + bytesread,
22915fbb8099SNobutomo Nakano 		    min(bytes - bytesread, rdblocksz),
22925fbb8099SNobutomo Nakano 		    data_in_info)) <= 0) {
22935fbb8099SNobutomo Nakano 			/*
22944e621e6cSNobutomo Nakano 			 * We come here only in the pass mode.
22955fbb8099SNobutomo Nakano 			 * If data couldn't be read from the input file
22964e621e6cSNobutomo Nakano 			 * descriptor, return number of bytes in the buf.
22974e621e6cSNobutomo Nakano 			 * If buffer is empty, return -1.
22985fbb8099SNobutomo Nakano 			 */
22994e621e6cSNobutomo Nakano 			if (bytesread == 0) {
23004e621e6cSNobutomo Nakano 				if (got == 0) /* EOF */
23014e621e6cSNobutomo Nakano 					data_in_info->data_in_rd_eof = 1;
23025fbb8099SNobutomo Nakano 				return (-1);
23035fbb8099SNobutomo Nakano 			}
23044e621e6cSNobutomo Nakano 			return (bytes - bytesread);
23054e621e6cSNobutomo Nakano 		}
23065fbb8099SNobutomo Nakano 	}
23075fbb8099SNobutomo Nakano 	return (0);
23085fbb8099SNobutomo Nakano }
2309ced83f9bSceastha 
2310ced83f9bSceastha /*
23115fbb8099SNobutomo Nakano  * Write as much data as we can.
23125fbb8099SNobutomo Nakano  *
23135fbb8099SNobutomo Nakano  * ofd		- output file descriptor.
23145fbb8099SNobutomo Nakano  * buf		- Source buffer to output data from.
23155fbb8099SNobutomo Nakano  * maxwrite	- The amount of data to write to the output.
23165fbb8099SNobutomo Nakano  *
23175fbb8099SNobutomo Nakano  * return 0 upon success.
23185fbb8099SNobutomo Nakano  */
23195fbb8099SNobutomo Nakano static int
23205fbb8099SNobutomo Nakano write_bytes(int ofd, char *buf, size_t maxwrite, data_in_t *data_in_info)
23215fbb8099SNobutomo Nakano {
23225fbb8099SNobutomo Nakano 	ssize_t	cnt;
23235fbb8099SNobutomo Nakano 
23244e621e6cSNobutomo Nakano 	errno = 0;
23255fbb8099SNobutomo Nakano 	if ((cnt = write(ofd, buf, maxwrite)) < (ssize_t)maxwrite) {
23265fbb8099SNobutomo Nakano 		data_in_info->data_in_errno = errno;
23275fbb8099SNobutomo Nakano 		/*
23285fbb8099SNobutomo Nakano 		 * data_in() needs to know if it was an actual write(2)
23295fbb8099SNobutomo Nakano 		 * failure, or if we just couldn't write all of the data
23305fbb8099SNobutomo Nakano 		 * requested so that we know that the rest of the file's
23315fbb8099SNobutomo Nakano 		 * data can be read but not written.
23325fbb8099SNobutomo Nakano 		 */
23334e621e6cSNobutomo Nakano 		if (cnt != -1)
23344e621e6cSNobutomo Nakano 			data_in_info->data_in_wr_part = 1;
23355fbb8099SNobutomo Nakano 		return (1);
23365fbb8099SNobutomo Nakano 	} else if (Args & OCp) {
23375fbb8099SNobutomo Nakano 		Blocks += (u_longlong_t)((cnt + (Bufsize - 1)) / Bufsize);
23385fbb8099SNobutomo Nakano 	}
23395fbb8099SNobutomo Nakano 	return (0);
23405fbb8099SNobutomo Nakano }
23415fbb8099SNobutomo Nakano 
23425fbb8099SNobutomo Nakano /*
23435fbb8099SNobutomo Nakano  * Perform I/O for given byte size with using limited i/o block size
23445fbb8099SNobutomo Nakano  * and supplied buffer.
23455fbb8099SNobutomo Nakano  *
23465fbb8099SNobutomo Nakano  * ifd/ofd	- i/o file descriptor
23475fbb8099SNobutomo Nakano  * buf		- buffer to be used for i/o
23485fbb8099SNobutomo Nakano  * bytes	- Amount to read/write
23495fbb8099SNobutomo Nakano  * wrblocksz	- Output block size.
23505fbb8099SNobutomo Nakano  * rdblocksz	- Read block size.
23515fbb8099SNobutomo Nakano  *
23525fbb8099SNobutomo Nakano  * Return 0 upon success. Return negative if read failed.
23535fbb8099SNobutomo Nakano  * Return positive non-zero if write failed.
23545fbb8099SNobutomo Nakano  */
23555fbb8099SNobutomo Nakano static int
23565fbb8099SNobutomo Nakano rdwr_bytes(int ifd, int ofd, char *buf, off_t bytes,
23575fbb8099SNobutomo Nakano     size_t wrblocksz, size_t rdblocksz, data_in_t *data_in_info)
23585fbb8099SNobutomo Nakano {
23594e621e6cSNobutomo Nakano 	int rv, sz;
23605fbb8099SNobutomo Nakano 	int error = 0;
23614e621e6cSNobutomo Nakano 	int write_it = (data_in_info->data_in_proc_mode != P_SKIP);
23625fbb8099SNobutomo Nakano 
23635fbb8099SNobutomo Nakano 	while (bytes > 0) {
23645fbb8099SNobutomo Nakano 		/*
23655fbb8099SNobutomo Nakano 		 * If the number of bytes left to write is smaller than
23665fbb8099SNobutomo Nakano 		 * the preferred I/O size, then we're about to do our final
23675fbb8099SNobutomo Nakano 		 * write to the file, so just set wrblocksz to the number of
23685fbb8099SNobutomo Nakano 		 * bytes left to write.
23695fbb8099SNobutomo Nakano 		 */
23705fbb8099SNobutomo Nakano 		if (bytes < wrblocksz)
23715fbb8099SNobutomo Nakano 			wrblocksz = bytes;
23725fbb8099SNobutomo Nakano 
23735fbb8099SNobutomo Nakano 		/* Read input till satisfy output block size */
23744e621e6cSNobutomo Nakano 		sz = read_bytes(ifd, buf, wrblocksz, rdblocksz, data_in_info);
23754e621e6cSNobutomo Nakano 		if (sz < 0)
23764e621e6cSNobutomo Nakano 			return (sz);
23775fbb8099SNobutomo Nakano 
23785fbb8099SNobutomo Nakano 		if (write_it) {
23794e621e6cSNobutomo Nakano 			rv = write_bytes(ofd, buf,
23804e621e6cSNobutomo Nakano 			    wrblocksz - sz, data_in_info);
23815fbb8099SNobutomo Nakano 			if (rv != 0) {
23825fbb8099SNobutomo Nakano 				/*
23835fbb8099SNobutomo Nakano 				 * If we wrote partial, we return and quits.
23845fbb8099SNobutomo Nakano 				 * Otherwise, read through the rest of input
23855fbb8099SNobutomo Nakano 				 * to go to the next file.
23865fbb8099SNobutomo Nakano 				 */
23875fbb8099SNobutomo Nakano 				if ((Args & OCp) ||
23884e621e6cSNobutomo Nakano 				    data_in_info->data_in_wr_part) {
23895fbb8099SNobutomo Nakano 					return (rv);
23905fbb8099SNobutomo Nakano 				} else {
23915fbb8099SNobutomo Nakano 					write_it = 0;
23925fbb8099SNobutomo Nakano 				}
23935fbb8099SNobutomo Nakano 				error = 1;
23945fbb8099SNobutomo Nakano 			}
23955fbb8099SNobutomo Nakano 		}
23964e621e6cSNobutomo Nakano 		bytes -= (wrblocksz - sz);
23975fbb8099SNobutomo Nakano 	}
23985fbb8099SNobutomo Nakano 	return (error);
23995fbb8099SNobutomo Nakano }
24005fbb8099SNobutomo Nakano 
24015fbb8099SNobutomo Nakano /*
24025fbb8099SNobutomo Nakano  * Write zeros for give size.
24035fbb8099SNobutomo Nakano  *
24045fbb8099SNobutomo Nakano  * ofd		- output file descriptor
24055fbb8099SNobutomo Nakano  * buf		- buffer to fill with zeros
24065fbb8099SNobutomo Nakano  * bytes	- Amount to write
24075fbb8099SNobutomo Nakano  * wrblocksz	- Write block size
24085fbb8099SNobutomo Nakano  *
24095fbb8099SNobutomo Nakano  * return 0 upon success.
24105fbb8099SNobutomo Nakano  */
24115fbb8099SNobutomo Nakano static int
24125fbb8099SNobutomo Nakano write_zeros(int ofd, char *buf, off_t bytes, size_t wrblocksz,
24135fbb8099SNobutomo Nakano     data_in_t *data_in_info)
24145fbb8099SNobutomo Nakano {
24155fbb8099SNobutomo Nakano 	int	rv;
24165fbb8099SNobutomo Nakano 
24175fbb8099SNobutomo Nakano 	(void) memset(buf, 0, min(bytes, wrblocksz));
24185fbb8099SNobutomo Nakano 	while (bytes > 0) {
24195fbb8099SNobutomo Nakano 		if (bytes < wrblocksz)
24205fbb8099SNobutomo Nakano 			wrblocksz = bytes;
24215fbb8099SNobutomo Nakano 		rv = write_bytes(ofd, buf, wrblocksz, data_in_info);
24225fbb8099SNobutomo Nakano 		if (rv != 0)
24235fbb8099SNobutomo Nakano 			return (rv);
24245fbb8099SNobutomo Nakano 		bytes -= wrblocksz;
24255fbb8099SNobutomo Nakano 	}
24265fbb8099SNobutomo Nakano 	return (0);
24275fbb8099SNobutomo Nakano }
24285fbb8099SNobutomo Nakano 
24295fbb8099SNobutomo Nakano /*
24305fbb8099SNobutomo Nakano  * To figure out the size of the buffer used to accumulate data from
24315fbb8099SNobutomo Nakano  * readtape() and to write to the file, we need to determine the largest
24325fbb8099SNobutomo Nakano  * chunk of data to be written to the file at one time. This is determined
24335fbb8099SNobutomo Nakano  * based on the following three things:
24345fbb8099SNobutomo Nakano  *	1) The size of the archived file.
24355fbb8099SNobutomo Nakano  *	2) The preferred I/O size of the file.
24365fbb8099SNobutomo Nakano  *	3) If the file is a read-write system attribute file.
24375fbb8099SNobutomo Nakano  * If the size of the file is less than the preferred I/O size or it's a
24385fbb8099SNobutomo Nakano  * read-write system attribute file, which must be written in one operation,
24395fbb8099SNobutomo Nakano  * then set the maximum write size to the size of the archived file.
24405fbb8099SNobutomo Nakano  * Otherwise, the maximum write size is preferred I/O size.
24415fbb8099SNobutomo Nakano  */
24425fbb8099SNobutomo Nakano static int
24435fbb8099SNobutomo Nakano calc_maxwrite(int ofd, int rw_sysattr, off_t bytes, size_t blocksize)
24445fbb8099SNobutomo Nakano {
24455fbb8099SNobutomo Nakano 	struct stat tsbuf;
24465fbb8099SNobutomo Nakano 	size_t maxwrite;
24475fbb8099SNobutomo Nakano 	size_t piosize;		/* preferred I/O size */
24485fbb8099SNobutomo Nakano 
24495fbb8099SNobutomo Nakano 	if (rw_sysattr || bytes < blocksize) {
24505fbb8099SNobutomo Nakano 		maxwrite = bytes;
24515fbb8099SNobutomo Nakano 	} else {
24525fbb8099SNobutomo Nakano 		if (fstat(ofd, &tsbuf) == 0) {
24535fbb8099SNobutomo Nakano 			piosize = tsbuf.st_blksize;
24545fbb8099SNobutomo Nakano 		} else {
24555fbb8099SNobutomo Nakano 			piosize = blocksize;
24565fbb8099SNobutomo Nakano 		}
24575fbb8099SNobutomo Nakano 		maxwrite = min(bytes, piosize);
24585fbb8099SNobutomo Nakano 	}
24595fbb8099SNobutomo Nakano 	return (maxwrite);
24605fbb8099SNobutomo Nakano }
24615fbb8099SNobutomo Nakano /*
24625fbb8099SNobutomo Nakano  * data_copy() and data_copy_with_holes() copy data from the input
24635fbb8099SNobutomo Nakano  * file to output file descriptor. If ifd is -1, then the input file is
24645fbb8099SNobutomo Nakano  * the archive file.
2465ced83f9bSceastha  *
2466ced83f9bSceastha  * Parameters
2467ced83f9bSceastha  *	ifd		- Input file descriptor to read from.
2468ced83f9bSceastha  *	ofd		- Output file descriptor of extracted file.
2469ced83f9bSceastha  *	rw_sysattr	- Flag indicating if a file is an extended
2470ced83f9bSceastha  *			system attribute file.
24715fbb8099SNobutomo Nakano  *	bytes		- Amount of data (file size) of copy/write.
2472ced83f9bSceastha  *	blocksize	- Amount of data to read at a time from either
2473ced83f9bSceastha  *			the input file descriptor or from the archive.
2474ced83f9bSceastha  *	data_in_info	- information needed while reading data when
2475ced83f9bSceastha  *			called by data_in().
24765fbb8099SNobutomo Nakano  *	holes		- Information of holes in the input file.
2477ced83f9bSceastha  *
2478ced83f9bSceastha  * Return code
2479ced83f9bSceastha  *	0		Success
2480ced83f9bSceastha  *	< 0		An error occurred during the read of the input
2481ced83f9bSceastha  *			file
2482ced83f9bSceastha  *	> 0		An error occurred during the write of the output
2483ced83f9bSceastha  *			file descriptor.
2484ced83f9bSceastha  */
2485ced83f9bSceastha static int
24865d7d0335SNobutomo Nakano data_copy(int ifd, int ofd, int rw_sysattr, off_t bytes,
2487ced83f9bSceastha     size_t blocksize, data_in_t *data_in_info)
2488ced83f9bSceastha {
2489ced83f9bSceastha 	char *buf;
2490ced83f9bSceastha 	size_t maxwrite;
24915fbb8099SNobutomo Nakano 	int rv;
2492ced83f9bSceastha 
2493ced83f9bSceastha 	/* No data to copy. */
24945fbb8099SNobutomo Nakano 	if (bytes == 0)
2495ced83f9bSceastha 		return (0);
2496ced83f9bSceastha 
24975fbb8099SNobutomo Nakano 	maxwrite = calc_maxwrite(ofd, rw_sysattr, bytes, blocksize);
2498ced83f9bSceastha 	buf = e_zalloc(E_EXIT, maxwrite);
2499ced83f9bSceastha 
25005fbb8099SNobutomo Nakano 	rv = rdwr_bytes(ifd, ofd, buf, bytes, maxwrite,
25015fbb8099SNobutomo Nakano 	    blocksize, data_in_info);
2502ced83f9bSceastha 
2503ced83f9bSceastha 	free(buf);
25045fbb8099SNobutomo Nakano 	return (rv);
2505ced83f9bSceastha }
25065fbb8099SNobutomo Nakano 
25075fbb8099SNobutomo Nakano static int
25085fbb8099SNobutomo Nakano data_copy_with_holes(int ifd, int ofd, int rw_sysattr, off_t bytes,
25095fbb8099SNobutomo Nakano     size_t blocksize, data_in_t *data_in_info, holes_info_t *holes)
25105fbb8099SNobutomo Nakano {
25115fbb8099SNobutomo Nakano 	holes_list_t	*hl;
25125fbb8099SNobutomo Nakano 	off_t		curpos, noff, datasize;
25135fbb8099SNobutomo Nakano 	char		*buf;
25145fbb8099SNobutomo Nakano 	size_t		maxwrite;
25155fbb8099SNobutomo Nakano 	int		rv, error;
25165fbb8099SNobutomo Nakano 
25175fbb8099SNobutomo Nakano 	if (bytes == 0)
25185fbb8099SNobutomo Nakano 		return (0);
25195fbb8099SNobutomo Nakano 
25205fbb8099SNobutomo Nakano 	maxwrite = calc_maxwrite(ofd, rw_sysattr, bytes, blocksize);
25215fbb8099SNobutomo Nakano 	buf = e_zalloc(E_EXIT, maxwrite);
25225fbb8099SNobutomo Nakano 
25235fbb8099SNobutomo Nakano 	error = 0;
25245fbb8099SNobutomo Nakano 	curpos = 0;
25255fbb8099SNobutomo Nakano 	for (hl = holes->holes_list; hl != NULL; hl = hl->hl_next) {
25265fbb8099SNobutomo Nakano 		if (curpos != hl->hl_data) {
25275fbb8099SNobutomo Nakano 			/* adjust output position */
25285fbb8099SNobutomo Nakano 			noff = lseek(ofd, hl->hl_data, SEEK_SET);
25295fbb8099SNobutomo Nakano 			if (noff != hl->hl_data) {
25305fbb8099SNobutomo Nakano 				/*
25315fbb8099SNobutomo Nakano 				 * Can't seek to the target, try to adjust
25325fbb8099SNobutomo Nakano 				 * position by filling with zeros.
25335fbb8099SNobutomo Nakano 				 */
25345fbb8099SNobutomo Nakano 				datasize = hl->hl_data - curpos;
25355fbb8099SNobutomo Nakano 				rv = write_zeros(ofd, buf, datasize,
25365fbb8099SNobutomo Nakano 				    maxwrite, data_in_info);
25375fbb8099SNobutomo Nakano 				if (rv != 0)
25385fbb8099SNobutomo Nakano 					goto errout;
25395fbb8099SNobutomo Nakano 			}
25405fbb8099SNobutomo Nakano 			/*
25415fbb8099SNobutomo Nakano 			 * Data is contiguous in the archive, but fragmented
25425fbb8099SNobutomo Nakano 			 * in the regular file, so we also adjust the input
25435fbb8099SNobutomo Nakano 			 * file position in pass mode.
25445fbb8099SNobutomo Nakano 			 */
25455fbb8099SNobutomo Nakano 			if (Args & OCp) {
25465fbb8099SNobutomo Nakano 				/* adjust input position */
25475fbb8099SNobutomo Nakano 				(void) lseek(ifd, hl->hl_data, SEEK_SET);
25485fbb8099SNobutomo Nakano 			}
25495fbb8099SNobutomo Nakano 			curpos = hl->hl_data;
25505fbb8099SNobutomo Nakano 		}
25515fbb8099SNobutomo Nakano 		datasize = hl->hl_hole - hl->hl_data;
25525fbb8099SNobutomo Nakano 		if (datasize == 0) {
25535fbb8099SNobutomo Nakano 			/*
25545fbb8099SNobutomo Nakano 			 * There is a hole at the end of file. To create
25555fbb8099SNobutomo Nakano 			 * such hole, we append one byte, and truncate the
25565fbb8099SNobutomo Nakano 			 * last block. This is necessary because ftruncate(2)
25575fbb8099SNobutomo Nakano 			 * alone allocates one block on the end of file.
25585fbb8099SNobutomo Nakano 			 */
25595fbb8099SNobutomo Nakano 			rv = write_zeros(ofd, buf, 1, maxwrite, data_in_info);
25605fbb8099SNobutomo Nakano 			if (rv != 0)
25615fbb8099SNobutomo Nakano 				goto errout;
25625fbb8099SNobutomo Nakano 			(void) ftruncate(ofd, hl->hl_data);
25635fbb8099SNobutomo Nakano 			break;
25645fbb8099SNobutomo Nakano 		}
25655fbb8099SNobutomo Nakano 		rv = rdwr_bytes(ifd, ofd, buf, datasize, maxwrite,
25665fbb8099SNobutomo Nakano 		    blocksize, data_in_info);
25675fbb8099SNobutomo Nakano 		if (rv != 0) {
25685fbb8099SNobutomo Nakano errout:
25695fbb8099SNobutomo Nakano 			/*
25705fbb8099SNobutomo Nakano 			 * Return if we got a read error or in pass mode,
25715fbb8099SNobutomo Nakano 			 * or failed with partial write. Otherwise, we'll
25724e621e6cSNobutomo Nakano 			 * read through the input stream till next file.
25735fbb8099SNobutomo Nakano 			 */
25745fbb8099SNobutomo Nakano 			if (rv < 0 || (Args & OCp) ||
25754e621e6cSNobutomo Nakano 			    data_in_info->data_in_wr_part) {
25765fbb8099SNobutomo Nakano 				free(buf);
25775fbb8099SNobutomo Nakano 				return (rv);
25785fbb8099SNobutomo Nakano 			}
25795fbb8099SNobutomo Nakano 			error = 1;
25805fbb8099SNobutomo Nakano 			hl = hl->hl_next;
25815fbb8099SNobutomo Nakano 			break;
25825fbb8099SNobutomo Nakano 		}
25835fbb8099SNobutomo Nakano 		curpos += datasize;
2584ced83f9bSceastha 	}
2585ced83f9bSceastha 
2586ced83f9bSceastha 	/*
25875fbb8099SNobutomo Nakano 	 * We should read through the input data to go to the next
25885fbb8099SNobutomo Nakano 	 * header when non-fatal error occured.
2589ced83f9bSceastha 	 */
25904e621e6cSNobutomo Nakano 	if (error && !(Args & OCp)) {
25915fbb8099SNobutomo Nakano 		data_in_info->data_in_proc_mode = P_SKIP;
25925fbb8099SNobutomo Nakano 		while (hl != NULL) {
25935fbb8099SNobutomo Nakano 			datasize = hl->hl_hole - hl->hl_data;
25945fbb8099SNobutomo Nakano 			rv = rdwr_bytes(ifd, ofd, buf, datasize, maxwrite,
25955fbb8099SNobutomo Nakano 			    blocksize, data_in_info);
25965fbb8099SNobutomo Nakano 			if (rv != 0)
25975fbb8099SNobutomo Nakano 				break;
25985fbb8099SNobutomo Nakano 			hl = hl->hl_next;
2599ced83f9bSceastha 		}
26005fbb8099SNobutomo Nakano 	}
26015fbb8099SNobutomo Nakano 
2602ced83f9bSceastha 	free(buf);
26035fbb8099SNobutomo Nakano 	return (error);
26045fbb8099SNobutomo Nakano }
26055fbb8099SNobutomo Nakano 
26065fbb8099SNobutomo Nakano /*
26075fbb8099SNobutomo Nakano  * Strip off the sparse file information that is prepended to
26085fbb8099SNobutomo Nakano  * the compressed sparse file. The information is in the following
26095fbb8099SNobutomo Nakano  * format:
26105fbb8099SNobutomo Nakano  * 	<prepended info size><SP><orig file size><SP><holes info>
26115fbb8099SNobutomo Nakano  * where prepended info size is long right justified in 10 bytes.
26125fbb8099SNobutomo Nakano  * Holesdata consists of the series of offset pairs:
26135fbb8099SNobutomo Nakano  * 	<data offset><SP><hole offset><SP><data offset><SP><hole offset>...
26145fbb8099SNobutomo Nakano  * prepended info size and original file size have been read in gethdr().
26155fbb8099SNobutomo Nakano  * We read the rest of holes information here in this function.
26165fbb8099SNobutomo Nakano  */
26175fbb8099SNobutomo Nakano static int
26185fbb8099SNobutomo Nakano read_holesdata(holes_info_t *holes, off_t *fileszp,
26195fbb8099SNobutomo Nakano     char *nam_p, data_in_t *data_in_info)
26205fbb8099SNobutomo Nakano {
26215fbb8099SNobutomo Nakano 	char		*holesdata;
26225fbb8099SNobutomo Nakano 	size_t		holesdata_sz;
26235fbb8099SNobutomo Nakano 
26245fbb8099SNobutomo Nakano 	/* We've already read the header. */
26255fbb8099SNobutomo Nakano 	holesdata_sz = holes->holesdata_sz - MIN_HOLES_HDRSIZE;
26265fbb8099SNobutomo Nakano 
26275fbb8099SNobutomo Nakano 	if ((holesdata = e_zalloc(E_NORMAL, holesdata_sz)) == NULL) {
26285fbb8099SNobutomo Nakano 		msg(ERRN, "Could not allocate memory for "
26295fbb8099SNobutomo Nakano 		    "sparse file information", nam_p);
2630ced83f9bSceastha 		return (1);
2631ced83f9bSceastha 	}
2632ced83f9bSceastha 	/*
26335fbb8099SNobutomo Nakano 	 * This function is called only in OCi mode. Therefore,
26345fbb8099SNobutomo Nakano 	 * read_bytes() won't fail, and won't return if error occurs in
26355fbb8099SNobutomo Nakano 	 * input stream. See rstbuf().
2636ced83f9bSceastha 	 */
26375fbb8099SNobutomo Nakano 	(void) read_bytes(-1, holesdata, holesdata_sz, CPIOBSZ, data_in_info);
26385fbb8099SNobutomo Nakano 	*fileszp -= holesdata_sz;
2639ced83f9bSceastha 
26405fbb8099SNobutomo Nakano 	/* The string should be terminated. */
26415fbb8099SNobutomo Nakano 	if (holesdata[holesdata_sz - 1] != '\0') {
26425fbb8099SNobutomo Nakano invalid:
26435fbb8099SNobutomo Nakano 		free(holesdata);
26445fbb8099SNobutomo Nakano 		msg(ERR, "invalid sparse file information", nam_p);
26455fbb8099SNobutomo Nakano 		return (1);
26465fbb8099SNobutomo Nakano 	}
26475fbb8099SNobutomo Nakano 	if (parse_holesdata(holes, holesdata) != 0)
26485fbb8099SNobutomo Nakano 		goto invalid;
26495fbb8099SNobutomo Nakano 
26505fbb8099SNobutomo Nakano 	/* sanity check */
26515fbb8099SNobutomo Nakano 	if (*fileszp != holes->data_size)
26525fbb8099SNobutomo Nakano 		goto invalid;
26535fbb8099SNobutomo Nakano 
26545fbb8099SNobutomo Nakano 	free(holesdata);
2655ced83f9bSceastha 	return (0);
2656ced83f9bSceastha }
26575fbb8099SNobutomo Nakano 
2658ced83f9bSceastha /*
26597c478bd9Sstevel@tonic-gate  * data_in:  If proc_mode == P_PROC, bread() the file's data from the archive
26607c478bd9Sstevel@tonic-gate  * and write(2) it to the open fdes gotten from openout().  If proc_mode ==
26617c478bd9Sstevel@tonic-gate  * P_SKIP, or becomes P_SKIP (due to errors etc), bread(2) the file's data
26627c478bd9Sstevel@tonic-gate  * and ignore it.  If the user specified any of the "swap" options (b, s or S),
26637c478bd9Sstevel@tonic-gate  * and the length of the file is not appropriate for that action, do not
26647c478bd9Sstevel@tonic-gate  * perform the "swap", otherwise perform the action on a buffer by buffer basis.
26657c478bd9Sstevel@tonic-gate  * If the CRC header was selected, calculate a running checksum as each buffer
26667c478bd9Sstevel@tonic-gate  * is processed.
26677c478bd9Sstevel@tonic-gate  */
26687c478bd9Sstevel@tonic-gate static void
26697c478bd9Sstevel@tonic-gate data_in(int proc_mode)
26707c478bd9Sstevel@tonic-gate {
26717c478bd9Sstevel@tonic-gate 	char *nam_p;
26725fbb8099SNobutomo Nakano 	int pad, rv;
26735fbb8099SNobutomo Nakano 	int error = 0;
2674ced83f9bSceastha 	int swapfile = 0;
26757c478bd9Sstevel@tonic-gate 	int cstatus = 0;
26765fbb8099SNobutomo Nakano 	off_t	filesz;
2677ced83f9bSceastha 	data_in_t *data_in_info;
26787c478bd9Sstevel@tonic-gate 
2679ced83f9bSceastha 	if (G_p->g_attrnam_p != NULL) {
26807c478bd9Sstevel@tonic-gate 		nam_p = G_p->g_attrnam_p;
26817c478bd9Sstevel@tonic-gate 	} else {
26827c478bd9Sstevel@tonic-gate 		nam_p = G_p->g_nam_p;
26837c478bd9Sstevel@tonic-gate 	}
26847c478bd9Sstevel@tonic-gate 
26857c478bd9Sstevel@tonic-gate 	if (((G_p->g_mode & Ftype) == S_IFLNK && proc_mode != P_SKIP) ||
26867c478bd9Sstevel@tonic-gate 	    (Hdr_type == BAR && bar_linkflag == '2' && proc_mode != P_SKIP)) {
26877c478bd9Sstevel@tonic-gate 		proc_mode = P_SKIP;
26887c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
26897c478bd9Sstevel@tonic-gate 	}
26907c478bd9Sstevel@tonic-gate 	if (Args & (OCb | OCs | OCS)) { /* verfify that swapping is possible */
26917c478bd9Sstevel@tonic-gate 		swapfile = 1;
26927c478bd9Sstevel@tonic-gate 		if (Args & (OCs | OCb) && G_p->g_filesz % 2) {
26937c478bd9Sstevel@tonic-gate 			msg(ERR,
26947c478bd9Sstevel@tonic-gate 			    "Cannot swap bytes of \"%s\", odd number of bytes",
26957c478bd9Sstevel@tonic-gate 			    nam_p);
26967c478bd9Sstevel@tonic-gate 			swapfile = 0;
26977c478bd9Sstevel@tonic-gate 		}
26987c478bd9Sstevel@tonic-gate 		if (Args & (OCS | OCb) && G_p->g_filesz % 4) {
26997c478bd9Sstevel@tonic-gate 			msg(ERR,
27007c478bd9Sstevel@tonic-gate 			    "Cannot swap halfwords of \"%s\", odd number "
27017c478bd9Sstevel@tonic-gate 			    "of halfwords", nam_p);
27027c478bd9Sstevel@tonic-gate 			swapfile = 0;
27037c478bd9Sstevel@tonic-gate 		}
27047c478bd9Sstevel@tonic-gate 	}
2705ced83f9bSceastha 
2706ced83f9bSceastha 	data_in_info = e_zalloc(E_EXIT, sizeof (data_in_t));
2707ced83f9bSceastha 	data_in_info->data_in_swapfile = swapfile;
2708ced83f9bSceastha 	data_in_info->data_in_proc_mode = proc_mode;
27097c478bd9Sstevel@tonic-gate 
27105fbb8099SNobutomo Nakano 	filesz = G_p->g_filesz;
27115fbb8099SNobutomo Nakano 
27125fbb8099SNobutomo Nakano 	if (S_ISSPARSE(G_p->g_mode) && G_p->g_holes != NULL) {
27135fbb8099SNobutomo Nakano 		/* We've already read the header in gethdr() */
27145fbb8099SNobutomo Nakano 		filesz -= MIN_HOLES_HDRSIZE;
27155fbb8099SNobutomo Nakano 
27165fbb8099SNobutomo Nakano 		/*
27175fbb8099SNobutomo Nakano 		 * Strip rest of the sparse file information. This includes
27185fbb8099SNobutomo Nakano 		 * the data/hole offset pairs which will be used to restore
27195fbb8099SNobutomo Nakano 		 * the holes in the file.
27205fbb8099SNobutomo Nakano 		 */
27215fbb8099SNobutomo Nakano 		if (proc_mode == P_SKIP) {
27225fbb8099SNobutomo Nakano 			/* holes info isn't necessary to skip file */
27235fbb8099SNobutomo Nakano 			free_holes_info(G_p->g_holes);
27245fbb8099SNobutomo Nakano 			G_p->g_holes = NULL;
27255fbb8099SNobutomo Nakano 		} else {
27265fbb8099SNobutomo Nakano 			rv = read_holesdata(G_p->g_holes, &filesz,
27275fbb8099SNobutomo Nakano 			    nam_p, data_in_info);
27285fbb8099SNobutomo Nakano 			if (rv != 0) {
27295fbb8099SNobutomo Nakano 				/*
27305fbb8099SNobutomo Nakano 				 * We got an error. Skip this file. holes info
27315fbb8099SNobutomo Nakano 				 * is no longer necessary.
27325fbb8099SNobutomo Nakano 				 */
27335fbb8099SNobutomo Nakano 				free_holes_info(G_p->g_holes);
27345fbb8099SNobutomo Nakano 				G_p->g_holes = NULL;
27355fbb8099SNobutomo Nakano 
27365fbb8099SNobutomo Nakano 				data_in_info->data_in_proc_mode = P_SKIP;
27375fbb8099SNobutomo Nakano 				error = 1;
27385fbb8099SNobutomo Nakano 			}
27395fbb8099SNobutomo Nakano 		}
27405fbb8099SNobutomo Nakano 	}
27415fbb8099SNobutomo Nakano 
27425fbb8099SNobutomo Nakano 	if (G_p->g_holes != NULL) {
27435fbb8099SNobutomo Nakano 		rv = data_copy_with_holes(-1, Ofile,
27445fbb8099SNobutomo Nakano 		    (G_p->g_attrnam_p == NULL) ? 0 : G_p->g_rw_sysattr,
27455fbb8099SNobutomo Nakano 		    G_p->g_holes->orig_size,
27465fbb8099SNobutomo Nakano 		    CPIOBSZ, data_in_info, G_p->g_holes);
27475fbb8099SNobutomo Nakano 
27485fbb8099SNobutomo Nakano 		free_holes_info(G_p->g_holes);
27495fbb8099SNobutomo Nakano 		G_p->g_holes = NULL;
27505fbb8099SNobutomo Nakano 	} else {
27515fbb8099SNobutomo Nakano 		rv = data_copy(-1, Ofile,
27525fbb8099SNobutomo Nakano 		    (G_p->g_attrnam_p == NULL) ? 0 : G_p->g_rw_sysattr,
27535fbb8099SNobutomo Nakano 		    filesz, CPIOBSZ, data_in_info);
27545fbb8099SNobutomo Nakano 	}
27555fbb8099SNobutomo Nakano 
27567c478bd9Sstevel@tonic-gate 	/* This writes out the file from the archive */
27575fbb8099SNobutomo Nakano 	if (rv != 0 || error) {
27585fbb8099SNobutomo Nakano 		errno = data_in_info->data_in_errno;
27595fbb8099SNobutomo Nakano 
27604e621e6cSNobutomo Nakano 		if (!error) {
27614e621e6cSNobutomo Nakano 			msg(data_in_info->data_in_wr_part ? EXTN : ERRN,
27624e621e6cSNobutomo Nakano 			    "Cannot write \"%s%s%s\"",
2763ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
2764ced83f9bSceastha 			    G_p->g_attrfnam_p,
2765ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
2766ced83f9bSceastha 			    G_p->g_rw_sysattr ?
2767ced83f9bSceastha 			    gettext(" System Attribute ") :
2768ced83f9bSceastha 			    gettext(" Attribute "), nam_p);
2769ced83f9bSceastha 		}
27707c478bd9Sstevel@tonic-gate 		/*
27715fbb8099SNobutomo Nakano 		 * We've failed to write to the file, and input data
27725fbb8099SNobutomo Nakano 		 * has been skiped to the next file. We'll need to restore
27735fbb8099SNobutomo Nakano 		 * the original file, and skip the rest of work.
27747c478bd9Sstevel@tonic-gate 		 */
27757c478bd9Sstevel@tonic-gate 		proc_mode = P_SKIP;
27767c478bd9Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_dirfd);
27777c478bd9Sstevel@tonic-gate 		cstatus = close(Ofile);
27787c478bd9Sstevel@tonic-gate 		Ofile = 0;
27797c478bd9Sstevel@tonic-gate 		if (cstatus != 0) {
27807c478bd9Sstevel@tonic-gate 			msg(EXTN, "close error");
27817c478bd9Sstevel@tonic-gate 		}
27827c478bd9Sstevel@tonic-gate 	}
27837c478bd9Sstevel@tonic-gate 
27845fbb8099SNobutomo Nakano 	/* we must use g_filesz for the amount of padding */
27857c478bd9Sstevel@tonic-gate 	pad = (Pad_val + 1 - (G_p->g_filesz & Pad_val)) & Pad_val;
27867c478bd9Sstevel@tonic-gate 	if (pad != 0) {
27877c478bd9Sstevel@tonic-gate 		FILL(pad);
27887c478bd9Sstevel@tonic-gate 		Buffr.b_out_p += pad;
27897c478bd9Sstevel@tonic-gate 		Buffr.b_cnt -= pad;
27907c478bd9Sstevel@tonic-gate 	}
27917c478bd9Sstevel@tonic-gate 	if (proc_mode != P_SKIP) {
2792ced83f9bSceastha 		if (Hdr_type == CRC &&
2793ced83f9bSceastha 		    Gen.g_cksum != data_in_info->data_in_cksumval) {
27947c478bd9Sstevel@tonic-gate 			msg(ERR, "\"%s\" - checksum error", nam_p);
27957c478bd9Sstevel@tonic-gate 			rstfiles(U_KEEP, G_p->g_dirfd);
27967c478bd9Sstevel@tonic-gate 		} else
27977c478bd9Sstevel@tonic-gate 			rstfiles(U_OVER, G_p->g_dirfd);
2798ced83f9bSceastha 		if (Hdr_type == BAR && data_in_info->data_in_compress_flag) {
2799ced83f9bSceastha 			(void) pclose(data_in_info->data_in_pipef);
28007c478bd9Sstevel@tonic-gate 		} else {
28017c478bd9Sstevel@tonic-gate 			cstatus = close(Ofile);
28027c478bd9Sstevel@tonic-gate 		}
28037c478bd9Sstevel@tonic-gate 		Ofile = 0;
28047c478bd9Sstevel@tonic-gate 		if (cstatus != 0) {
28057c478bd9Sstevel@tonic-gate 			msg(EXTN, "close error");
28067c478bd9Sstevel@tonic-gate 		}
28077c478bd9Sstevel@tonic-gate 	}
2808ced83f9bSceastha 	(void) free(data_in_info);
2809ced83f9bSceastha 
2810ced83f9bSceastha 	VERBOSE((proc_mode != P_SKIP && (Args & (OCv | OCV))),
2811ced83f9bSceastha 	    (G_p->g_attrparent_p == NULL) ? G_p->g_nam_p : G_p->g_attrpath_p);
28127c478bd9Sstevel@tonic-gate 	Finished = 1;
28137c478bd9Sstevel@tonic-gate }
28147c478bd9Sstevel@tonic-gate 
28157c478bd9Sstevel@tonic-gate /*
28165fbb8099SNobutomo Nakano  * Read regular file. Return number of bytes which weren't read.
28175fbb8099SNobutomo Nakano  * Upon return, real_filesz will be real file size of input file.
28185fbb8099SNobutomo Nakano  * When read_exact is specified, read size is adjusted to the given
28195fbb8099SNobutomo Nakano  * file size.
28205fbb8099SNobutomo Nakano  */
28215fbb8099SNobutomo Nakano static off_t
28225fbb8099SNobutomo Nakano read_file(char *nam_p, off_t file_size, off_t *real_filesz,
28235fbb8099SNobutomo Nakano 	boolean_t read_exact)
28245fbb8099SNobutomo Nakano {
28255fbb8099SNobutomo Nakano 	int	amount_read;
28265fbb8099SNobutomo Nakano 	off_t	amt_to_read;
28275fbb8099SNobutomo Nakano 	off_t	readsz;
28285fbb8099SNobutomo Nakano 
28295fbb8099SNobutomo Nakano 	if (file_size == 0)
28305fbb8099SNobutomo Nakano 		return (0);
28315fbb8099SNobutomo Nakano 
28325fbb8099SNobutomo Nakano 	amt_to_read = file_size;
28335fbb8099SNobutomo Nakano 	do {
28345fbb8099SNobutomo Nakano 		if (read_exact && amt_to_read < CPIOBSZ)
28355fbb8099SNobutomo Nakano 			readsz = amt_to_read;
28365fbb8099SNobutomo Nakano 		else
28375fbb8099SNobutomo Nakano 			readsz = CPIOBSZ;
28385fbb8099SNobutomo Nakano 
28395fbb8099SNobutomo Nakano 		FLUSH(readsz);
28405fbb8099SNobutomo Nakano 		errno = 0;
28415fbb8099SNobutomo Nakano 
28425fbb8099SNobutomo Nakano 		if ((amount_read = read(Ifile, Buffr.b_in_p, readsz)) < 0) {
28435fbb8099SNobutomo Nakano 			msg(EXTN, "Cannot read \"%s%s%s\"",
28445fbb8099SNobutomo Nakano 			    (Gen.g_attrnam_p == NULL) ?
28455fbb8099SNobutomo Nakano 			    nam_p : Gen.g_attrfnam_p,
28465fbb8099SNobutomo Nakano 			    (Gen.g_attrnam_p == NULL) ? "" : Gen.g_rw_sysattr ?
28475fbb8099SNobutomo Nakano 			    gettext(" System Attribute ") :
28485fbb8099SNobutomo Nakano 			    gettext(" Attribute "),
28495fbb8099SNobutomo Nakano 			    (Gen.g_attrnam_p == NULL) ? "" : nam_p);
28505fbb8099SNobutomo Nakano 			break;
28515fbb8099SNobutomo Nakano 		}
28525fbb8099SNobutomo Nakano 
28535fbb8099SNobutomo Nakano 		if (amount_read == 0) {
28545fbb8099SNobutomo Nakano 			/* got EOF. the file has shrunk */
28555fbb8099SNobutomo Nakano 			*real_filesz = file_size - amt_to_read;
28565fbb8099SNobutomo Nakano 			break;
28575fbb8099SNobutomo Nakano 		} else if (amount_read > amt_to_read) {
28585fbb8099SNobutomo Nakano 			/* the file has grown */
28595fbb8099SNobutomo Nakano 			*real_filesz = file_size +
28605fbb8099SNobutomo Nakano 			    (amount_read - amt_to_read);
28615fbb8099SNobutomo Nakano 			amount_read = amt_to_read;
28625fbb8099SNobutomo Nakano 		} else if (amount_read == amt_to_read) {
28635fbb8099SNobutomo Nakano 			/* the file is the same size */
28645fbb8099SNobutomo Nakano 			*real_filesz = file_size;
28655fbb8099SNobutomo Nakano 		}
28665fbb8099SNobutomo Nakano 
28675fbb8099SNobutomo Nakano 		Buffr.b_in_p += amount_read;
28685fbb8099SNobutomo Nakano 		Buffr.b_cnt += (long)amount_read;
28695fbb8099SNobutomo Nakano 
28705fbb8099SNobutomo Nakano 		amt_to_read -= (off_t)amount_read;
28715fbb8099SNobutomo Nakano 		if (!read_exact &&
28725fbb8099SNobutomo Nakano 		    amt_to_read == 0 && amount_read == CPIOBSZ) {
28735fbb8099SNobutomo Nakano 			/*
28745fbb8099SNobutomo Nakano 			 * If the file size is multiple of CPIOBSZ, we may
28755fbb8099SNobutomo Nakano 			 * be able to read more from the file even though
28765fbb8099SNobutomo Nakano 			 * amt_to_read already gets 0.
28775fbb8099SNobutomo Nakano 			 */
28785fbb8099SNobutomo Nakano 			FLUSH(CPIOBSZ);
28795fbb8099SNobutomo Nakano 			amount_read = read(Ifile, Buffr.b_in_p, CPIOBSZ);
28805fbb8099SNobutomo Nakano 			if (amount_read != 0) {
28815fbb8099SNobutomo Nakano 				/* the file has grown */
28825fbb8099SNobutomo Nakano 				*real_filesz = file_size + amount_read;
28835fbb8099SNobutomo Nakano 			}
28845fbb8099SNobutomo Nakano 		}
28855fbb8099SNobutomo Nakano 	} while (amt_to_read != 0);
28865fbb8099SNobutomo Nakano 
28875fbb8099SNobutomo Nakano 	return (amt_to_read);
28885fbb8099SNobutomo Nakano }
28895fbb8099SNobutomo Nakano 
28905fbb8099SNobutomo Nakano /*
28915fbb8099SNobutomo Nakano  * Read through the data in files skipping holes.
28925fbb8099SNobutomo Nakano  */
28935fbb8099SNobutomo Nakano static off_t
28945fbb8099SNobutomo Nakano read_compress_holes(char *nam_p, off_t file_size, off_t *real_filesz,
28955fbb8099SNobutomo Nakano     holes_info_t *holes, int *hole_changed)
28965fbb8099SNobutomo Nakano {
28975fbb8099SNobutomo Nakano 	off_t		left;
28985fbb8099SNobutomo Nakano 	off_t		datasize, realsz;
28995fbb8099SNobutomo Nakano 	off_t		curpos, npos;
29005fbb8099SNobutomo Nakano 	holes_list_t	*hl = holes->holes_list;
29015fbb8099SNobutomo Nakano 
29025fbb8099SNobutomo Nakano 	curpos = 0;
29035fbb8099SNobutomo Nakano 	for (hl = holes->holes_list; hl != NULL; hl = hl->hl_next) {
29045fbb8099SNobutomo Nakano 		datasize = hl->hl_hole - hl->hl_data;
29055fbb8099SNobutomo Nakano 
29065fbb8099SNobutomo Nakano 		npos = lseek(Ifile, curpos, SEEK_DATA);
29075fbb8099SNobutomo Nakano 		if (npos == -1 && errno == ENXIO) {
29085fbb8099SNobutomo Nakano 			/*
29095fbb8099SNobutomo Nakano 			 * No more data. There are two cases.
29105fbb8099SNobutomo Nakano 			 * - we have a hole toward the end of file.
29115fbb8099SNobutomo Nakano 			 * - file has been shrunk, and we've reached EOF.
29125fbb8099SNobutomo Nakano 			 */
29135fbb8099SNobutomo Nakano 			*real_filesz = lseek(Ifile, 0, SEEK_END);
29145fbb8099SNobutomo Nakano 			if (hl->hl_data == file_size)
29155fbb8099SNobutomo Nakano 				return (0);
29165fbb8099SNobutomo Nakano 			/*
29175fbb8099SNobutomo Nakano 			 * File has been shrunk. Check the amount of data
29185fbb8099SNobutomo Nakano 			 * left.
29195fbb8099SNobutomo Nakano 			 */
29205fbb8099SNobutomo Nakano 			left = 0;
29215fbb8099SNobutomo Nakano 			while (hl != NULL) {
29225fbb8099SNobutomo Nakano 				left += (hl->hl_hole - hl->hl_data);
29235fbb8099SNobutomo Nakano 				hl = hl->hl_next;
29245fbb8099SNobutomo Nakano 			}
29255fbb8099SNobutomo Nakano 			return (left);
29265fbb8099SNobutomo Nakano 		}
29275fbb8099SNobutomo Nakano 
29285fbb8099SNobutomo Nakano 		/* found data */
29295fbb8099SNobutomo Nakano 		curpos = npos;
29305fbb8099SNobutomo Nakano 		if (curpos != hl->hl_data) {
29315fbb8099SNobutomo Nakano 			/*
29325fbb8099SNobutomo Nakano 			 * File has been changed. We shouldn't read data
29335fbb8099SNobutomo Nakano 			 * from different offset since we've already put
29345fbb8099SNobutomo Nakano 			 * the holes data.
29355fbb8099SNobutomo Nakano 			 */
29365fbb8099SNobutomo Nakano 			*hole_changed = 1;
29375fbb8099SNobutomo Nakano 			(void) lseek(Ifile, hl->hl_data, SEEK_SET);
29385fbb8099SNobutomo Nakano 			curpos = hl->hl_data;
29395fbb8099SNobutomo Nakano 		}
29405fbb8099SNobutomo Nakano 		left = read_file(nam_p, datasize, &realsz, B_TRUE);
29415fbb8099SNobutomo Nakano 		if (left != 0) {
29425fbb8099SNobutomo Nakano 			/* file has been shrunk */
29435fbb8099SNobutomo Nakano 			*real_filesz = curpos + datasize - left;
29445fbb8099SNobutomo Nakano 			left = file_size - *real_filesz;
29455fbb8099SNobutomo Nakano 			return (left);
29465fbb8099SNobutomo Nakano 		}
29475fbb8099SNobutomo Nakano 		curpos += datasize;
29485fbb8099SNobutomo Nakano 	}
29495fbb8099SNobutomo Nakano 	/*
29505fbb8099SNobutomo Nakano 	 * We've read exact size of holes. We need to make sure
29515fbb8099SNobutomo Nakano 	 * that file hasn't grown by reading from the EOF.
29525fbb8099SNobutomo Nakano 	 */
29535fbb8099SNobutomo Nakano 	realsz = 0;
29545fbb8099SNobutomo Nakano 	(void) read_file(nam_p, CPIOBSZ, &realsz, B_FALSE);
29555fbb8099SNobutomo Nakano 
29565fbb8099SNobutomo Nakano 	*real_filesz = curpos + realsz;
29575fbb8099SNobutomo Nakano 	return (0);
29585fbb8099SNobutomo Nakano }
29595fbb8099SNobutomo Nakano 
29605fbb8099SNobutomo Nakano /*
29617c478bd9Sstevel@tonic-gate  * data_out:  open(2) the file to be archived, compute the checksum
29627c478bd9Sstevel@tonic-gate  * of it's data if the CRC header was specified and write the header.
29637c478bd9Sstevel@tonic-gate  * read(2) each block of data and bwrite() it to the archive.  For TARTYP (TAR
29647c478bd9Sstevel@tonic-gate  * and USTAR) archives, pad the data with NULLs to the next 512 byte boundary.
29657c478bd9Sstevel@tonic-gate  */
29667c478bd9Sstevel@tonic-gate static void
29677c478bd9Sstevel@tonic-gate data_out(void)
29687c478bd9Sstevel@tonic-gate {
29697c478bd9Sstevel@tonic-gate 	char		*nam_p;
29705fbb8099SNobutomo Nakano 	int		cnt, pad;
29715fbb8099SNobutomo Nakano 	off_t		amt_to_read;
29725fbb8099SNobutomo Nakano 	off_t		real_filesz;
29737c478bd9Sstevel@tonic-gate 	int		errret = 0;
29745fbb8099SNobutomo Nakano 	int		hole_changed = 0;
29755fbb8099SNobutomo Nakano 	off_t		orig_filesz;
29765fbb8099SNobutomo Nakano 	holes_info_t	*holes = NULL;
29777c478bd9Sstevel@tonic-gate 
29787c478bd9Sstevel@tonic-gate 	nam_p = G_p->g_nam_p;
29797c478bd9Sstevel@tonic-gate 	if (Aspec) {
29807c478bd9Sstevel@tonic-gate 		if (Pflag && aclp != NULL) {
29817c478bd9Sstevel@tonic-gate 			char    *secinfo = NULL;
29827c478bd9Sstevel@tonic-gate 			int	len = 0;
29837c478bd9Sstevel@tonic-gate 
29847c478bd9Sstevel@tonic-gate 			/* append security attributes */
2985fa9e4066Sahrens 			if (append_secattr(&secinfo, &len, aclp) == -1) {
29867c478bd9Sstevel@tonic-gate 				msg(ERR,
29877c478bd9Sstevel@tonic-gate 				    "can create security information");
29887c478bd9Sstevel@tonic-gate 			}
29897c478bd9Sstevel@tonic-gate 			/* call append_secattr() if more than one */
29907c478bd9Sstevel@tonic-gate 
29917c478bd9Sstevel@tonic-gate 			if (len > 0) {
29927c478bd9Sstevel@tonic-gate 			/* write ancillary only if there is sec info */
29935fbb8099SNobutomo Nakano 				write_hdr(ARCHIVE_ACL, (off_t)len);
29945fbb8099SNobutomo Nakano 				write_ancillary(secinfo, len, B_TRUE);
29957c478bd9Sstevel@tonic-gate 			}
29967c478bd9Sstevel@tonic-gate 		}
29977c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
29987c478bd9Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_dirfd);
29997c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
30007c478bd9Sstevel@tonic-gate 		return;
30017c478bd9Sstevel@tonic-gate 	}
30027c478bd9Sstevel@tonic-gate 	if ((G_p->g_mode & Ftype) == S_IFLNK && (Hdr_type !=
30037c478bd9Sstevel@tonic-gate 	    USTAR && Hdr_type != TAR)) { /* symbolic link */
30047c478bd9Sstevel@tonic-gate 		int size;
30057c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
30067c478bd9Sstevel@tonic-gate 
30077c478bd9Sstevel@tonic-gate 		FLUSH(G_p->g_filesz);
30087c478bd9Sstevel@tonic-gate 		errno = 0;
30097c478bd9Sstevel@tonic-gate 
30107c478bd9Sstevel@tonic-gate 		/* Note that "size" and G_p->g_filesz are the same number */
30117c478bd9Sstevel@tonic-gate 
30127c478bd9Sstevel@tonic-gate 		if ((size = readlink(nam_p, Buffr.b_in_p, G_p->g_filesz)) <
30137c478bd9Sstevel@tonic-gate 		    0) {
30147c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot read symbolic link \"%s\"", nam_p);
30157c478bd9Sstevel@tonic-gate 			return;
30167c478bd9Sstevel@tonic-gate 		}
30177c478bd9Sstevel@tonic-gate 
30187c478bd9Sstevel@tonic-gate 		/*
30197c478bd9Sstevel@tonic-gate 		 * Note that it is OK not to add the NUL after the name read by
30207c478bd9Sstevel@tonic-gate 		 * readlink, because it is not being used subsequently.
30217c478bd9Sstevel@tonic-gate 		 */
30227c478bd9Sstevel@tonic-gate 
30237c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += size;
30247c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += size;
30257c478bd9Sstevel@tonic-gate 		pad = (Pad_val + 1 - (size & Pad_val)) & Pad_val;
30267c478bd9Sstevel@tonic-gate 		if (pad != 0) {
30277c478bd9Sstevel@tonic-gate 			FLUSH(pad);
30285fbb8099SNobutomo Nakano 			(void) memset(Buffr.b_in_p, 0, pad);
30297c478bd9Sstevel@tonic-gate 			Buffr.b_in_p += pad;
30307c478bd9Sstevel@tonic-gate 			Buffr.b_cnt += pad;
30317c478bd9Sstevel@tonic-gate 		}
30327c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
30337c478bd9Sstevel@tonic-gate 		return;
30347c478bd9Sstevel@tonic-gate 	} else if ((G_p->g_mode & Ftype) == S_IFLNK &&
30357c478bd9Sstevel@tonic-gate 	    (Hdr_type == USTAR || Hdr_type == TAR)) {
30367c478bd9Sstevel@tonic-gate 		int size;
30377c478bd9Sstevel@tonic-gate 
30387c478bd9Sstevel@tonic-gate 		/*
30397c478bd9Sstevel@tonic-gate 		 * G_p->g_filesz is the length of the right-hand side of
30407c478bd9Sstevel@tonic-gate 		 * the symlink "x -> y".
30417c478bd9Sstevel@tonic-gate 		 * The tar link field is only NAMSIZ long.
30427c478bd9Sstevel@tonic-gate 		 */
30437c478bd9Sstevel@tonic-gate 
30447c478bd9Sstevel@tonic-gate 		if (G_p->g_filesz > NAMSIZ) {
30457c478bd9Sstevel@tonic-gate 			msg(ERRN,
30467c478bd9Sstevel@tonic-gate 			    "Symbolic link too long \"%s\"", nam_p);
30477c478bd9Sstevel@tonic-gate 			return;
30487c478bd9Sstevel@tonic-gate 		}
30497c478bd9Sstevel@tonic-gate 		if ((size = readlink(nam_p, T_lname, G_p->g_filesz)) < 0) {
30507c478bd9Sstevel@tonic-gate 			msg(ERRN,
30517c478bd9Sstevel@tonic-gate 			    "Cannot read symbolic link \"%s\"", nam_p);
30527c478bd9Sstevel@tonic-gate 			return;
30537c478bd9Sstevel@tonic-gate 		}
30547c478bd9Sstevel@tonic-gate 		T_lname[size] = '\0';
30557c478bd9Sstevel@tonic-gate 		G_p->g_filesz = (off_t)0;
30567c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
30577c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
30587c478bd9Sstevel@tonic-gate 		return;
30597c478bd9Sstevel@tonic-gate 	}
30607c478bd9Sstevel@tonic-gate 	if ((Ifile = openfile(O_RDONLY)) < 0) {
30617c478bd9Sstevel@tonic-gate 		msg(ERR, "\"%s%s%s\" ?",
3062ced83f9bSceastha 		    (Gen.g_attrnam_p == NULL) ? nam_p : Gen.g_attrfnam_p,
3063ced83f9bSceastha 		    (Gen.g_attrnam_p == NULL) ? "" : Gen.g_rw_sysattr ?
3064ced83f9bSceastha 		    gettext(" System Attribute ") : gettext(" Attribute "),
3065ced83f9bSceastha 		    (Gen.g_attrnam_p == NULL) ? "" :
3066ced83f9bSceastha 		    (Gen.g_attrparent_p == NULL) ? Gen.g_attrnam_p :
3067ced83f9bSceastha 		    Gen.g_attrparent_p);
30687c478bd9Sstevel@tonic-gate 		return;
30697c478bd9Sstevel@tonic-gate 	}
30707c478bd9Sstevel@tonic-gate 
30715fbb8099SNobutomo Nakano 	/* save original file size */
30725fbb8099SNobutomo Nakano 	orig_filesz = G_p->g_filesz;
30735fbb8099SNobutomo Nakano 
30745fbb8099SNobutomo Nakano 	/*
30755fbb8099SNobutomo Nakano 	 * Calculate the new compressed file size of a sparse file
30765fbb8099SNobutomo Nakano 	 * before any of the header information is written
30775fbb8099SNobutomo Nakano 	 * to the archive.
30785fbb8099SNobutomo Nakano 	 */
30795fbb8099SNobutomo Nakano 	if (Compress_sparse && S_ISREG(G_p->g_mode)) {
30805fbb8099SNobutomo Nakano 		/*
30815fbb8099SNobutomo Nakano 		 * If the file being processed is a sparse file, gather the
30825fbb8099SNobutomo Nakano 		 * hole information and the compressed file size.
30835fbb8099SNobutomo Nakano 		 * G_p->g_filesz will need to be changed to be the size of
30845fbb8099SNobutomo Nakano 		 * the compressed sparse file plus the the size of the hole
30855fbb8099SNobutomo Nakano 		 * information that will be prepended to the compressed file
30865fbb8099SNobutomo Nakano 		 * in the archive.
30875fbb8099SNobutomo Nakano 		 */
30885fbb8099SNobutomo Nakano 		holes = get_holes_info(Ifile, G_p->g_filesz, B_FALSE);
30895fbb8099SNobutomo Nakano 		if (holes != NULL)
30905fbb8099SNobutomo Nakano 			G_p->g_filesz = holes->holesdata_sz + holes->data_size;
30915fbb8099SNobutomo Nakano 
30925fbb8099SNobutomo Nakano 		if (G_p->g_filesz > Max_offset) {
30935fbb8099SNobutomo Nakano 			msg(ERR, "%s%s%s: too large to archive "
30945fbb8099SNobutomo Nakano 			    "in current mode",
30955fbb8099SNobutomo Nakano 			    G_p->g_nam_p,
30965fbb8099SNobutomo Nakano 			    (G_p->g_attrnam_p == NULL) ? "" :
30975fbb8099SNobutomo Nakano 			    G_p->g_rw_sysattr ?
30985fbb8099SNobutomo Nakano 			    gettext(" System Attribute ") :
30995fbb8099SNobutomo Nakano 			    gettext(" Attribute "),
31005fbb8099SNobutomo Nakano 			    (G_p->g_attrnam_p == NULL) ? "" :
31015fbb8099SNobutomo Nakano 			    ((G_p->g_attrparent_p == NULL) ?
31025fbb8099SNobutomo Nakano 			    G_p->g_attrnam_p:
31035fbb8099SNobutomo Nakano 			    G_p->g_attrpath_p));
31045fbb8099SNobutomo Nakano 
31055fbb8099SNobutomo Nakano 			(void) close(Ifile);
31065fbb8099SNobutomo Nakano 			if (holes != NULL)
31075fbb8099SNobutomo Nakano 				free_holes_info(holes);
31085fbb8099SNobutomo Nakano 			return; /* do not archive if it's too big */
31095fbb8099SNobutomo Nakano 		}
31105fbb8099SNobutomo Nakano 	}
31115fbb8099SNobutomo Nakano 
31127c478bd9Sstevel@tonic-gate 	/*
31137c478bd9Sstevel@tonic-gate 	 * Dump extended attribute header.
31147c478bd9Sstevel@tonic-gate 	 */
31157c478bd9Sstevel@tonic-gate 
3116ced83f9bSceastha 	if (Gen.g_attrnam_p != NULL) {
31177c478bd9Sstevel@tonic-gate 		write_xattr_hdr();
31187c478bd9Sstevel@tonic-gate 	}
31197c478bd9Sstevel@tonic-gate 
31207c478bd9Sstevel@tonic-gate 	if (Hdr_type == CRC) {
31217c478bd9Sstevel@tonic-gate 		long csum = cksum(CRC, 0, &errret);
31227c478bd9Sstevel@tonic-gate 		if (errret != 0) {
31237c478bd9Sstevel@tonic-gate 			G_p->g_cksum = (ulong_t)-1;
31247c478bd9Sstevel@tonic-gate 			msg(POST, "\"%s%s%s\" skipped",
3125ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ?
31267c478bd9Sstevel@tonic-gate 			    nam_p : Gen.g_attrfnam_p,
3127ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ? "" : Gen.g_rw_sysattr ?
3128ced83f9bSceastha 			    gettext(" System Attribute ") :
3129ced83f9bSceastha 			    gettext(" Attribute "),
3130ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ? "" : nam_p);
31315fbb8099SNobutomo Nakano 			if (holes != NULL)
31325fbb8099SNobutomo Nakano 				free_holes_info(holes);
31337c478bd9Sstevel@tonic-gate 			(void) close(Ifile);
31347c478bd9Sstevel@tonic-gate 			return;
31357c478bd9Sstevel@tonic-gate 		}
31367c478bd9Sstevel@tonic-gate 		G_p->g_cksum = csum;
31377c478bd9Sstevel@tonic-gate 	} else {
31387c478bd9Sstevel@tonic-gate 		G_p->g_cksum = 0;
31397c478bd9Sstevel@tonic-gate 	}
31407c478bd9Sstevel@tonic-gate 
31417c478bd9Sstevel@tonic-gate 	/*
31427c478bd9Sstevel@tonic-gate 	 * ACL has been retrieved in getname().
31437c478bd9Sstevel@tonic-gate 	 */
31447c478bd9Sstevel@tonic-gate 	if (Pflag) {
31457c478bd9Sstevel@tonic-gate 		char    *secinfo = NULL;
31467c478bd9Sstevel@tonic-gate 		int	len = 0;
31477c478bd9Sstevel@tonic-gate 
31487c478bd9Sstevel@tonic-gate 		/* append security attributes */
3149fa9e4066Sahrens 		if ((append_secattr(&secinfo, &len, aclp)) == -1)
31507c478bd9Sstevel@tonic-gate 			msg(ERR, "can create security information");
31517c478bd9Sstevel@tonic-gate 
31527c478bd9Sstevel@tonic-gate 		/* call append_secattr() if more than one */
31537c478bd9Sstevel@tonic-gate 
31547c478bd9Sstevel@tonic-gate 		if (len > 0) {
31557c478bd9Sstevel@tonic-gate 		/* write ancillary only if there is sec info */
31565fbb8099SNobutomo Nakano 			write_hdr(ARCHIVE_ACL, (off_t)len);
31575fbb8099SNobutomo Nakano 			write_ancillary(secinfo, len, B_TRUE);
31587c478bd9Sstevel@tonic-gate 		}
31597c478bd9Sstevel@tonic-gate 	}
31607c478bd9Sstevel@tonic-gate 
31615fbb8099SNobutomo Nakano 	if (holes != NULL) {
31625fbb8099SNobutomo Nakano 		/*
31635fbb8099SNobutomo Nakano 		 * Write the header info with a modified c_mode field to
31645fbb8099SNobutomo Nakano 		 * indicate a compressed sparse file is being archived,
31655fbb8099SNobutomo Nakano 		 * as well as the new file size, including the size of the
31665fbb8099SNobutomo Nakano 		 * compressed file as well as all the prepended data.
31675fbb8099SNobutomo Nakano 		 */
31685fbb8099SNobutomo Nakano 		write_hdr(ARCHIVE_SPARSE, (off_t)0);
31695fbb8099SNobutomo Nakano 		/* Prepend sparse file info */
31705fbb8099SNobutomo Nakano 		write_ancillary(holes->holesdata,
31715fbb8099SNobutomo Nakano 		    holes->holesdata_sz, B_FALSE);
31725fbb8099SNobutomo Nakano 	} else {
31737c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
31745fbb8099SNobutomo Nakano 	}
31757c478bd9Sstevel@tonic-gate 
31767c478bd9Sstevel@tonic-gate 	real_filesz = 0;
31777c478bd9Sstevel@tonic-gate 
31785fbb8099SNobutomo Nakano 	if (holes != NULL) {
31795fbb8099SNobutomo Nakano 		amt_to_read = read_compress_holes(nam_p, G_p->g_filesz,
31805fbb8099SNobutomo Nakano 		    &real_filesz, holes, &hole_changed);
31815fbb8099SNobutomo Nakano 	} else {
31825fbb8099SNobutomo Nakano 		amt_to_read = read_file(nam_p, G_p->g_filesz,
31835fbb8099SNobutomo Nakano 		    &real_filesz, B_FALSE);
31847c478bd9Sstevel@tonic-gate 	}
31857c478bd9Sstevel@tonic-gate 
31867c478bd9Sstevel@tonic-gate 	while (amt_to_read > 0) {
31877c478bd9Sstevel@tonic-gate 		cnt = (amt_to_read > CPIOBSZ) ? CPIOBSZ : (int)amt_to_read;
31887c478bd9Sstevel@tonic-gate 		FLUSH(cnt);
31895fbb8099SNobutomo Nakano 		(void) memset(Buffr.b_in_p, 0, cnt);
31907c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += cnt;
31917c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += cnt;
31927c478bd9Sstevel@tonic-gate 		amt_to_read -= cnt;
31937c478bd9Sstevel@tonic-gate 	}
31947c478bd9Sstevel@tonic-gate 
31957c478bd9Sstevel@tonic-gate 	pad = (Pad_val + 1 - (G_p->g_filesz & Pad_val)) & Pad_val;
31967c478bd9Sstevel@tonic-gate 	if (pad != 0) {
31977c478bd9Sstevel@tonic-gate 		FLUSH(pad);
31985fbb8099SNobutomo Nakano 		(void) memset(Buffr.b_in_p, 0, pad);
31997c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += pad;
32007c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += pad;
32017c478bd9Sstevel@tonic-gate 	}
32027c478bd9Sstevel@tonic-gate 
32035fbb8099SNobutomo Nakano 	if (hole_changed == 1) {
32045fbb8099SNobutomo Nakano 		msg(ERR,
32055fbb8099SNobutomo Nakano 		    "File data and hole offsets of \"%s%s%s\" have changed",
32065fbb8099SNobutomo Nakano 		    (Gen.g_attrnam_p == NULL) ?
32075fbb8099SNobutomo Nakano 		    G_p->g_nam_p : Gen.g_attrfnam_p,
32085fbb8099SNobutomo Nakano 		    (Gen.g_attrnam_p == NULL) ? "" : Gen.g_rw_sysattr ?
32095fbb8099SNobutomo Nakano 		    gettext(" System Attribute ") : gettext(" Attribute "),
32105fbb8099SNobutomo Nakano 		    (Gen.g_attrnam_p == NULL) ? "" : G_p->g_nam_p);
32115fbb8099SNobutomo Nakano 	}
32125fbb8099SNobutomo Nakano 	if (real_filesz > orig_filesz) {
32137c478bd9Sstevel@tonic-gate 		msg(ERR, "File size of \"%s%s%s\" has increased by %lld",
3214ced83f9bSceastha 		    (Gen.g_attrnam_p == NULL) ?
32157c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p : Gen.g_attrfnam_p,
3216ced83f9bSceastha 		    (Gen.g_attrnam_p == NULL) ? "" : Gen.g_rw_sysattr ?
3217ced83f9bSceastha 		    gettext(" System Attribute ") : gettext(" Attribute "),
3218ced83f9bSceastha 		    (Gen.g_attrnam_p == NULL) ? "" : G_p->g_nam_p,
32195fbb8099SNobutomo Nakano 		    (real_filesz - orig_filesz));
32207c478bd9Sstevel@tonic-gate 	}
32215fbb8099SNobutomo Nakano 	if (real_filesz < orig_filesz) {
32227c478bd9Sstevel@tonic-gate 		msg(ERR, "File size of \"%s%s%s\" has decreased by %lld",
3223ced83f9bSceastha 		    (Gen.g_attrnam_p == NULL) ?
32247c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p : Gen.g_attrfnam_p,
3225ced83f9bSceastha 		    (Gen.g_attrnam_p == NULL) ? "" : Gen.g_rw_sysattr ?
3226ced83f9bSceastha 		    gettext(" System Attribute ") : gettext(" Attribute "),
3227ced83f9bSceastha 		    (Gen.g_attrnam_p == NULL) ? "" : G_p->g_nam_p,
32285fbb8099SNobutomo Nakano 		    (orig_filesz - real_filesz));
32297c478bd9Sstevel@tonic-gate 	}
32307c478bd9Sstevel@tonic-gate 
32315fbb8099SNobutomo Nakano 	if (holes != NULL)
32325fbb8099SNobutomo Nakano 		free_holes_info(holes);
32335fbb8099SNobutomo Nakano 
32347c478bd9Sstevel@tonic-gate 	(void) close(Ifile);
32357c478bd9Sstevel@tonic-gate 	rstfiles(U_KEEP, G_p->g_dirfd);
32367c478bd9Sstevel@tonic-gate 	VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
32377c478bd9Sstevel@tonic-gate }
32387c478bd9Sstevel@tonic-gate 
32397c478bd9Sstevel@tonic-gate /*
32407c478bd9Sstevel@tonic-gate  * data_pass:  If not a special file (Aspec), open(2) the file to be
32417c478bd9Sstevel@tonic-gate  * transferred, read(2) each block of data and write(2) it to the output file
32427c478bd9Sstevel@tonic-gate  * Ofile, which was opened in file_pass().
32437c478bd9Sstevel@tonic-gate  */
32447c478bd9Sstevel@tonic-gate static void
32457c478bd9Sstevel@tonic-gate data_pass(void)
32467c478bd9Sstevel@tonic-gate {
3247ced83f9bSceastha 	int rv;
32487c478bd9Sstevel@tonic-gate 	int cstatus;
32497c478bd9Sstevel@tonic-gate 	char *namep = Nam_p;
32505fbb8099SNobutomo Nakano 	holes_info_t *holes = NULL;
32514e621e6cSNobutomo Nakano 	data_in_t *data_in_info;
32527c478bd9Sstevel@tonic-gate 
3253ced83f9bSceastha 	if (G_p->g_attrnam_p != NULL) {
32547c478bd9Sstevel@tonic-gate 		namep = G_p->g_attrnam_p;
32557c478bd9Sstevel@tonic-gate 	}
32567c478bd9Sstevel@tonic-gate 	if (Aspec) {
32577c478bd9Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_passdirfd);
32587c478bd9Sstevel@tonic-gate 		cstatus = close(Ofile);
32597c478bd9Sstevel@tonic-gate 		Ofile = 0;
32607c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), Nam_p);
32617c478bd9Sstevel@tonic-gate 		if (cstatus != 0) {
32627c478bd9Sstevel@tonic-gate 			msg(EXTN, "close error");
32637c478bd9Sstevel@tonic-gate 		}
32647c478bd9Sstevel@tonic-gate 		return;
32657c478bd9Sstevel@tonic-gate 	}
32667c478bd9Sstevel@tonic-gate 	if ((Ifile = openat(G_p->g_dirfd, get_component(namep), 0)) < 0) {
32677c478bd9Sstevel@tonic-gate 		msg(ERRN, "Cannot open \"%s%s%s\", skipped",
3268ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? Nam_p : G_p->g_attrfnam_p,
3269ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? "" : G_p->g_rw_sysattr ?
3270ced83f9bSceastha 		    gettext(" System Attribute ") : gettext(" Attribute "),
3271ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? "" : G_p->g_attrnam_p);
32727c478bd9Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_passdirfd);
32737c478bd9Sstevel@tonic-gate 		cstatus = close(Ofile);
32747c478bd9Sstevel@tonic-gate 		Ofile = 0;
32757c478bd9Sstevel@tonic-gate 		if (cstatus != 0) {
32767c478bd9Sstevel@tonic-gate 			msg(EXTN, "close error");
32777c478bd9Sstevel@tonic-gate 		}
32787c478bd9Sstevel@tonic-gate 		return;
32797c478bd9Sstevel@tonic-gate 	}
32807c478bd9Sstevel@tonic-gate 
32814e621e6cSNobutomo Nakano 	data_in_info = e_zalloc(E_EXIT, sizeof (data_in_t));
32824e621e6cSNobutomo Nakano 	data_in_info->data_in_proc_mode = P_PROC;
32834e621e6cSNobutomo Nakano 
32845fbb8099SNobutomo Nakano 	if (S_ISREG(G_p->g_mode))
32855fbb8099SNobutomo Nakano 		holes = get_holes_info(Ifile, G_p->g_filesz, B_TRUE);
32865fbb8099SNobutomo Nakano 
32875fbb8099SNobutomo Nakano 	if (holes != NULL) {
32885fbb8099SNobutomo Nakano 		rv = data_copy_with_holes(Ifile, Ofile,
32895fbb8099SNobutomo Nakano 		    (G_p->g_attrnam_p == NULL) ? 0 : G_p->g_rw_sysattr,
32904e621e6cSNobutomo Nakano 		    G_p->g_filesz, Bufsize, data_in_info, holes);
32915fbb8099SNobutomo Nakano 
32925fbb8099SNobutomo Nakano 		free_holes_info(holes);
32935fbb8099SNobutomo Nakano 	} else {
32945fbb8099SNobutomo Nakano 		rv = data_copy(Ifile, Ofile,
32955fbb8099SNobutomo Nakano 		    (G_p->g_attrnam_p == NULL) ? 0 : G_p->g_rw_sysattr,
32964e621e6cSNobutomo Nakano 		    G_p->g_filesz, Bufsize, data_in_info);
32975fbb8099SNobutomo Nakano 	}
32984e621e6cSNobutomo Nakano 
3299ced83f9bSceastha 	if (rv < 0) {
33004e621e6cSNobutomo Nakano 		/* read error or unexpected EOF */
33014e621e6cSNobutomo Nakano 		if (data_in_info->data_in_rd_eof) {
33024e621e6cSNobutomo Nakano 			/*
33034e621e6cSNobutomo Nakano 			 * read has reached EOF unexpectedly, but this isn't
33044e621e6cSNobutomo Nakano 			 * an error since it's the latest shape of the file.
33054e621e6cSNobutomo Nakano 			 */
33064e621e6cSNobutomo Nakano 			msg(EPOST, "File size of \"%s%s%s\" has decreased",
33074e621e6cSNobutomo Nakano 			    (G_p->g_attrnam_p == NULL) ?
33084e621e6cSNobutomo Nakano 			    Nam_p : G_p->g_attrfnam_p,
33094e621e6cSNobutomo Nakano 			    (G_p->g_attrnam_p == NULL) ? "" :
33104e621e6cSNobutomo Nakano 			    G_p->g_rw_sysattr ? gettext(" System Attribute ") :
33114e621e6cSNobutomo Nakano 			    gettext(" Attribute "),
33124e621e6cSNobutomo Nakano 			    (G_p->g_attrnam_p == NULL) ? "" : G_p->g_attrnam_p);
33134e621e6cSNobutomo Nakano 
33144e621e6cSNobutomo Nakano 			/* It's not error. We'll use the new file */
33154e621e6cSNobutomo Nakano 			rv = 0;
33164e621e6cSNobutomo Nakano 		} else {
33174e621e6cSNobutomo Nakano 			/* read error */
33187c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot read \"%s%s%s\"",
3319ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ?
33207c478bd9Sstevel@tonic-gate 			    Nam_p : G_p->g_attrfnam_p,
3321ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
3322ced83f9bSceastha 			    G_p->g_rw_sysattr ? gettext(" System Attribute ") :
3323ced83f9bSceastha 			    gettext(" Attribute "),
3324ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" : G_p->g_attrnam_p);
33254e621e6cSNobutomo Nakano 		}
3326ced83f9bSceastha 	} else if (rv > 0) {
33274e621e6cSNobutomo Nakano 		/* write error */
33287c478bd9Sstevel@tonic-gate 		if (Do_rename) {
33297c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot write \"%s%s%s\"", Over_p,
3330ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
33314e621e6cSNobutomo Nakano 			    G_p->g_rw_sysattr ? gettext(" System Attribute ") :
3332ced83f9bSceastha 			    gettext(" Attribute "),
3333ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" : Over_p);
33347c478bd9Sstevel@tonic-gate 		} else {
33357c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot write \"%s%s%s\"",
33367c478bd9Sstevel@tonic-gate 			    Fullnam_p,
3337ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
33384e621e6cSNobutomo Nakano 			    G_p->g_rw_sysattr ? gettext(" System Attribute ") :
3339ced83f9bSceastha 			    gettext(" Attribute "),
33404e621e6cSNobutomo Nakano 			    (G_p->g_attrnam_p == NULL) ? "" : G_p->g_attrnam_p);
33417c478bd9Sstevel@tonic-gate 		}
3342ced83f9bSceastha 	}
33434e621e6cSNobutomo Nakano 
33444e621e6cSNobutomo Nakano 	free(data_in_info);
3345ced83f9bSceastha 
33465fbb8099SNobutomo Nakano 	if (rv == 0) {
33477c478bd9Sstevel@tonic-gate 		rstfiles(U_OVER, G_p->g_passdirfd);
33487c478bd9Sstevel@tonic-gate 	} else {
33497c478bd9Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_passdirfd);
33507c478bd9Sstevel@tonic-gate 	}
3351ced83f9bSceastha 
33527c478bd9Sstevel@tonic-gate 	(void) close(Ifile);
33537c478bd9Sstevel@tonic-gate 	cstatus = close(Ofile);
33547c478bd9Sstevel@tonic-gate 	Ofile = 0;
33557c478bd9Sstevel@tonic-gate 	if (cstatus != 0) {
33567c478bd9Sstevel@tonic-gate 		msg(EXTN, "close error");
33577c478bd9Sstevel@tonic-gate 	}
33587c478bd9Sstevel@tonic-gate 	VERBOSE((Args & (OCv | OCV)), Fullnam_p);
33597c478bd9Sstevel@tonic-gate 	Finished = 1;
33607c478bd9Sstevel@tonic-gate }
33617c478bd9Sstevel@tonic-gate 
33627c478bd9Sstevel@tonic-gate /*
33637c478bd9Sstevel@tonic-gate  * file_in:  Process an object from the archive.  If a TARTYP (TAR or USTAR)
33647c478bd9Sstevel@tonic-gate  * archive and g_nlink == 1, link this file to the file name in t_linkname
33657c478bd9Sstevel@tonic-gate  * and return.  Handle linked files in one of two ways.  If Onecopy == 0, this
33667c478bd9Sstevel@tonic-gate  * is an old style (binary or -c) archive, create and extract the data for the
33677c478bd9Sstevel@tonic-gate  * first link found, link all subsequent links to this file and skip their data.
33687c478bd9Sstevel@tonic-gate  * If Oncecopy == 1, save links until all have been processed, and then
33697c478bd9Sstevel@tonic-gate  * process the links first to last checking their names against the patterns
33707c478bd9Sstevel@tonic-gate  * and/or asking the user to rename them.  The first link that is accepted
33717c478bd9Sstevel@tonic-gate  * for xtraction is created and the data is read from the archive.
33727c478bd9Sstevel@tonic-gate  * All subsequent links that are accepted are linked to this file.
33737c478bd9Sstevel@tonic-gate  */
33747c478bd9Sstevel@tonic-gate static void
33757c478bd9Sstevel@tonic-gate file_in(void)
33767c478bd9Sstevel@tonic-gate {
33777c478bd9Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
33787c478bd9Sstevel@tonic-gate 	int lnkem = 0, cleanup = 0;
33797c478bd9Sstevel@tonic-gate 	int proc_file;
33807c478bd9Sstevel@tonic-gate 	struct Lnk *ttl_p;
33817c478bd9Sstevel@tonic-gate 	int typeflag;
33827c478bd9Sstevel@tonic-gate 	char savacl;
33837c478bd9Sstevel@tonic-gate 	int cwd;
33847c478bd9Sstevel@tonic-gate 
33857c478bd9Sstevel@tonic-gate 	G_p = &Gen;
33867c478bd9Sstevel@tonic-gate 
33877c478bd9Sstevel@tonic-gate 	/*
3388ced83f9bSceastha 	 * Now that we've read the extended header,
3389ced83f9bSceastha 	 * determine if we should restore attributes.
3390ced83f9bSceastha 	 * Don't restore the attribute if we are extracting
3391ced83f9bSceastha 	 * a file from an archive (as opposed to doing a table of
3392ced83f9bSceastha 	 * contents) and any of the following are true:
3393ced83f9bSceastha 	 * 1. neither -@ or -/ was specified.
3394ced83f9bSceastha 	 * 2. -@ was specified, -/ wasn't specified, and we're
3395ced83f9bSceastha 	 * processing a hidden attribute directory of an attribute
3396ced83f9bSceastha 	 * or we're processing a read-write system attribute file.
3397ced83f9bSceastha 	 * 3.  -@ wasn't specified, -/ was specified, and the file
3398ced83f9bSceastha 	 * we're processing it not a read-write system attribute file,
3399ced83f9bSceastha 	 * or we're processing the hidden attribute directory of an
3400ced83f9bSceastha 	 * attribute.
3401ced83f9bSceastha 	 *
3402ced83f9bSceastha 	 * We always process the attributes if we're just generating
3403ced83f9bSceastha 	 * generating a table of contents, or if both -@ and -/ were
3404ced83f9bSceastha 	 * specified.
3405ced83f9bSceastha 	 */
3406ced83f9bSceastha 	if (G_p->g_attrnam_p != NULL) {
3407ced83f9bSceastha 		if (((Args & OCt) == 0) &&
3408ced83f9bSceastha 		    ((!Atflag && !SysAtflag) ||
3409ced83f9bSceastha 		    (Atflag && !SysAtflag && ((G_p->g_attrparent_p != NULL) ||
3410ced83f9bSceastha 		    G_p->g_rw_sysattr)) ||
3411ced83f9bSceastha 		    (!Atflag && SysAtflag && ((G_p->g_attrparent_p != NULL) ||
3412ced83f9bSceastha 		    !G_p->g_rw_sysattr)))) {
3413ced83f9bSceastha 			proc_file = F_SKIP;
3414ced83f9bSceastha 			data_in(P_SKIP);
3415ced83f9bSceastha 			return;
3416ced83f9bSceastha 		}
3417ced83f9bSceastha 	}
3418ced83f9bSceastha 
3419ced83f9bSceastha 	/*
34207c478bd9Sstevel@tonic-gate 	 * Open target directory if this isn't a skipped file
34217c478bd9Sstevel@tonic-gate 	 * and g_nlink == 1
34227c478bd9Sstevel@tonic-gate 	 *
34237c478bd9Sstevel@tonic-gate 	 * Links are handled further down in this function.
34247c478bd9Sstevel@tonic-gate 	 */
34257c478bd9Sstevel@tonic-gate 
34267c478bd9Sstevel@tonic-gate 	proc_file = ckname(0);
34277c478bd9Sstevel@tonic-gate 
34287c478bd9Sstevel@tonic-gate 	if (proc_file == F_SKIP && G_p->g_nlink == 1) {
34297c478bd9Sstevel@tonic-gate 		/*
34307c478bd9Sstevel@tonic-gate 		 * Normally ckname() prints out the file as a side
34317c478bd9Sstevel@tonic-gate 		 * effect except for table of contents listing
34327c478bd9Sstevel@tonic-gate 		 * when its parameter is zero and Onecopy isn't
34337c478bd9Sstevel@tonic-gate 		 * Zero.  Due to this we need to force the name
34347c478bd9Sstevel@tonic-gate 		 * to be printed here.
34357c478bd9Sstevel@tonic-gate 		 */
34367c478bd9Sstevel@tonic-gate 		if (Onecopy == 1) {
34377c478bd9Sstevel@tonic-gate 			VERBOSE((Args & OCt), G_p->g_nam_p);
34387c478bd9Sstevel@tonic-gate 		}
34397c478bd9Sstevel@tonic-gate 		data_in(P_SKIP);
34407c478bd9Sstevel@tonic-gate 		return;
34417c478bd9Sstevel@tonic-gate 	}
34427c478bd9Sstevel@tonic-gate 
34437c478bd9Sstevel@tonic-gate 	if (proc_file != F_SKIP && open_dirfd() != 0) {
34447c478bd9Sstevel@tonic-gate 		data_in(P_SKIP);
34457c478bd9Sstevel@tonic-gate 		return;
34467c478bd9Sstevel@tonic-gate 	}
34477c478bd9Sstevel@tonic-gate 
34487c478bd9Sstevel@tonic-gate 	if (Hdr_type == BAR) {
34497c478bd9Sstevel@tonic-gate 		bar_file_in();
34507c478bd9Sstevel@tonic-gate 		close_dirfd();
34517c478bd9Sstevel@tonic-gate 		return;
34527c478bd9Sstevel@tonic-gate 	}
34537c478bd9Sstevel@tonic-gate 
34547c478bd9Sstevel@tonic-gate 	/*
34557c478bd9Sstevel@tonic-gate 	 * For archives in USTAR format, the files are extracted according
34567c478bd9Sstevel@tonic-gate 	 * to the typeflag.
34577c478bd9Sstevel@tonic-gate 	 */
34587c478bd9Sstevel@tonic-gate 	if (Hdr_type == USTAR || Hdr_type == TAR) {
34597c478bd9Sstevel@tonic-gate 		typeflag = Thdr_p->tbuf.t_typeflag;
34607c478bd9Sstevel@tonic-gate 		if (G_p->g_nlink == 1) {		/* hard link */
34617c478bd9Sstevel@tonic-gate 			if (proc_file != F_SKIP) {
34627c478bd9Sstevel@tonic-gate 				int i;
34637c478bd9Sstevel@tonic-gate 				char lname[NAMSIZ+1];
34647c478bd9Sstevel@tonic-gate 				(void) memset(lname, '\0', sizeof (lname));
34657c478bd9Sstevel@tonic-gate 
34667c478bd9Sstevel@tonic-gate 				(void) strncpy(lname, Thdr_p->tbuf.t_linkname,
34677c478bd9Sstevel@tonic-gate 				    NAMSIZ);
34687c478bd9Sstevel@tonic-gate 				for (i = 0; i <= NAMSIZ && lname[i] != 0; i++)
34697c478bd9Sstevel@tonic-gate 					;
34707c478bd9Sstevel@tonic-gate 
34717c478bd9Sstevel@tonic-gate 				lname[i] = 0;
34727c478bd9Sstevel@tonic-gate 				(void) creat_lnk(G_p->g_dirfd,
34737c478bd9Sstevel@tonic-gate 				    &lname[0], G_p->g_nam_p);
34747c478bd9Sstevel@tonic-gate 			}
34757c478bd9Sstevel@tonic-gate 			close_dirfd();
34767c478bd9Sstevel@tonic-gate 			return;
34777c478bd9Sstevel@tonic-gate 		}
34787c478bd9Sstevel@tonic-gate 		if (typeflag == '3' || typeflag == '4' || typeflag == '5' ||
34797c478bd9Sstevel@tonic-gate 		    typeflag == '6') {
34807c478bd9Sstevel@tonic-gate 			if (proc_file != F_SKIP &&
34817c478bd9Sstevel@tonic-gate 			    creat_spec(G_p->g_dirfd) > 0) {
3482ced83f9bSceastha 				VERBOSE((Args & (OCv | OCV)),
3483ced83f9bSceastha 				    (G_p->g_attrparent_p == NULL) ?
3484ced83f9bSceastha 				    G_p->g_nam_p : G_p->g_attrpath_p);
34857c478bd9Sstevel@tonic-gate 			}
34867c478bd9Sstevel@tonic-gate 			close_dirfd();
34877c478bd9Sstevel@tonic-gate 			return;
34887c478bd9Sstevel@tonic-gate 		} else if (Adir || Aspec) {
34897c478bd9Sstevel@tonic-gate 			if ((proc_file == F_SKIP) ||
34907c478bd9Sstevel@tonic-gate 			    (Ofile = openout(G_p->g_dirfd)) < 0) {
34917c478bd9Sstevel@tonic-gate 				data_in(P_SKIP);
34927c478bd9Sstevel@tonic-gate 			} else {
34937c478bd9Sstevel@tonic-gate 				data_in(P_PROC);
34947c478bd9Sstevel@tonic-gate 			}
34957c478bd9Sstevel@tonic-gate 			close_dirfd();
34967c478bd9Sstevel@tonic-gate 			return;
34977c478bd9Sstevel@tonic-gate 		}
34987c478bd9Sstevel@tonic-gate 	}
34997c478bd9Sstevel@tonic-gate 
35007c478bd9Sstevel@tonic-gate 	if (Adir) {
35017c478bd9Sstevel@tonic-gate 		if (proc_file != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
35027c478bd9Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
35037c478bd9Sstevel@tonic-gate 		}
35047c478bd9Sstevel@tonic-gate 		close_dirfd();
35057c478bd9Sstevel@tonic-gate 		if (Onecopy == 1) {
35067c478bd9Sstevel@tonic-gate 			VERBOSE((Args & OCt), G_p->g_nam_p);
35077c478bd9Sstevel@tonic-gate 		}
35087c478bd9Sstevel@tonic-gate 		return;
35097c478bd9Sstevel@tonic-gate 	}
35107c478bd9Sstevel@tonic-gate 	if (G_p->g_nlink == 1 || (Hdr_type == TAR ||
35117c478bd9Sstevel@tonic-gate 	    Hdr_type == USTAR)) {
35127c478bd9Sstevel@tonic-gate 		if (Aspec) {
35137c478bd9Sstevel@tonic-gate 			if (proc_file != F_SKIP && creat_spec(G_p->g_dirfd) > 0)
35147c478bd9Sstevel@tonic-gate 				VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
35157c478bd9Sstevel@tonic-gate 		} else {
35167c478bd9Sstevel@tonic-gate 			if ((proc_file == F_SKIP) ||
35177c478bd9Sstevel@tonic-gate 			    (Ofile = openout(G_p->g_dirfd)) < 0) {
35187c478bd9Sstevel@tonic-gate 				data_in(P_SKIP);
35197c478bd9Sstevel@tonic-gate 			} else {
35207c478bd9Sstevel@tonic-gate 				data_in(P_PROC);
35217c478bd9Sstevel@tonic-gate 			}
35227c478bd9Sstevel@tonic-gate 		}
35237c478bd9Sstevel@tonic-gate 		close_dirfd();
35247c478bd9Sstevel@tonic-gate 		return;
35257c478bd9Sstevel@tonic-gate 	}
35267c478bd9Sstevel@tonic-gate 	close_dirfd();
35277c478bd9Sstevel@tonic-gate 
35287c478bd9Sstevel@tonic-gate 	tl_p = add_lnk(&ttl_p);
35297c478bd9Sstevel@tonic-gate 	l_p = ttl_p;
35307c478bd9Sstevel@tonic-gate 	if (l_p->L_cnt == l_p->L_gen.g_nlink)
35317c478bd9Sstevel@tonic-gate 		cleanup = 1;
3532ced83f9bSceastha 	if (!Onecopy || G_p->g_attrnam_p != NULL) {
35337c478bd9Sstevel@tonic-gate 		lnkem = (tl_p != l_p) ? 1 : 0;
35347c478bd9Sstevel@tonic-gate 		G_p = &tl_p->L_gen;
35357c478bd9Sstevel@tonic-gate 		if (proc_file == F_SKIP) {
35367c478bd9Sstevel@tonic-gate 			data_in(P_SKIP);
35377c478bd9Sstevel@tonic-gate 		} else {
35387c478bd9Sstevel@tonic-gate 			if (open_dirfd() != 0)
35397c478bd9Sstevel@tonic-gate 				return;
35407c478bd9Sstevel@tonic-gate 			if (!lnkem) {
35417c478bd9Sstevel@tonic-gate 				if (Aspec) {
35427c478bd9Sstevel@tonic-gate 					if (creat_spec(G_p->g_dirfd) > 0)
35437c478bd9Sstevel@tonic-gate 						VERBOSE((Args & (OCv | OCV)),
35447c478bd9Sstevel@tonic-gate 						    G_p->g_nam_p);
35457c478bd9Sstevel@tonic-gate 				} else if ((Ofile =
35467c478bd9Sstevel@tonic-gate 				    openout(G_p->g_dirfd)) < 0) {
35477c478bd9Sstevel@tonic-gate 					data_in(P_SKIP);
35487c478bd9Sstevel@tonic-gate 					close_dirfd();
35497c478bd9Sstevel@tonic-gate 					reclaim(l_p);
35507c478bd9Sstevel@tonic-gate 				} else {
35517c478bd9Sstevel@tonic-gate 					data_in(P_PROC);
35527c478bd9Sstevel@tonic-gate 					close_dirfd();
35537c478bd9Sstevel@tonic-gate 				}
35547c478bd9Sstevel@tonic-gate 			} else {
35557c478bd9Sstevel@tonic-gate 				/*
35567c478bd9Sstevel@tonic-gate 				 * Are we linking an attribute?
35577c478bd9Sstevel@tonic-gate 				 */
35587c478bd9Sstevel@tonic-gate 				cwd = -1;
3559ced83f9bSceastha 				if (l_p->L_gen.g_attrnam_p != NULL) {
35607c478bd9Sstevel@tonic-gate 					(void) strcpy(Lnkend_p,
35617c478bd9Sstevel@tonic-gate 					    l_p->L_gen.g_attrnam_p);
35627c478bd9Sstevel@tonic-gate 					(void) strcpy(Full_p,
35637c478bd9Sstevel@tonic-gate 					    tl_p->L_gen.g_attrnam_p);
35647c478bd9Sstevel@tonic-gate 					cwd = save_cwd();
35657c478bd9Sstevel@tonic-gate 					(void) fchdir(G_p->g_dirfd);
35667c478bd9Sstevel@tonic-gate 				} else {
35677c478bd9Sstevel@tonic-gate 					(void) strcpy(Lnkend_p,
35687c478bd9Sstevel@tonic-gate 					    l_p->L_gen.g_nam_p);
35697c478bd9Sstevel@tonic-gate 					(void) strcpy(Full_p,
35707c478bd9Sstevel@tonic-gate 					    tl_p->L_gen.g_nam_p);
35717c478bd9Sstevel@tonic-gate 				}
35727c478bd9Sstevel@tonic-gate 				(void) creat_lnk(G_p->g_dirfd,
35737c478bd9Sstevel@tonic-gate 				    Lnkend_p, Full_p);
35747c478bd9Sstevel@tonic-gate 				data_in(P_SKIP);
35757c478bd9Sstevel@tonic-gate 				close_dirfd();
3576ced83f9bSceastha 				l_p->L_lnk_p = NULL;
35777c478bd9Sstevel@tonic-gate 				free(tl_p->L_gen.g_nam_p);
35787c478bd9Sstevel@tonic-gate 				free(tl_p);
35797c478bd9Sstevel@tonic-gate 				if (cwd != -1)
35807c478bd9Sstevel@tonic-gate 					rest_cwd(cwd);
35817c478bd9Sstevel@tonic-gate 			}
35827c478bd9Sstevel@tonic-gate 		}
35837c478bd9Sstevel@tonic-gate 	} else { /* Onecopy */
35847c478bd9Sstevel@tonic-gate 		if (tl_p->L_gen.g_filesz)
35857c478bd9Sstevel@tonic-gate 			cleanup = 1;
35867c478bd9Sstevel@tonic-gate 		if (!cleanup) {
35877c478bd9Sstevel@tonic-gate 			close_dirfd();
35887c478bd9Sstevel@tonic-gate 			return; /* don't do anything yet */
35897c478bd9Sstevel@tonic-gate 		}
35907c478bd9Sstevel@tonic-gate 		tl_p = l_p;
35917c478bd9Sstevel@tonic-gate 		/*
35927c478bd9Sstevel@tonic-gate 		 * ckname will clear aclchar. We need to keep aclchar for
35937c478bd9Sstevel@tonic-gate 		 * all links.
35947c478bd9Sstevel@tonic-gate 		 */
35957c478bd9Sstevel@tonic-gate 		savacl = aclchar;
3596ced83f9bSceastha 		while (tl_p != NULL) {
35977c478bd9Sstevel@tonic-gate 			G_p = &tl_p->L_gen;
35987c478bd9Sstevel@tonic-gate 			aclchar = savacl;
35997c478bd9Sstevel@tonic-gate 			if ((proc_file = ckname(1)) != F_SKIP) {
36007c478bd9Sstevel@tonic-gate 				if (open_dirfd() != 0) {
36017c478bd9Sstevel@tonic-gate 					return;
36027c478bd9Sstevel@tonic-gate 				}
36037c478bd9Sstevel@tonic-gate 				if (l_p->L_data) {
36047c478bd9Sstevel@tonic-gate 					(void) creat_lnk(G_p->g_dirfd,
36057c478bd9Sstevel@tonic-gate 					    l_p->L_gen.g_nam_p,
36067c478bd9Sstevel@tonic-gate 					    G_p->g_nam_p);
36077c478bd9Sstevel@tonic-gate 				} else if (Aspec) {
36087c478bd9Sstevel@tonic-gate 					(void) creat_spec(G_p->g_dirfd);
36097c478bd9Sstevel@tonic-gate 					l_p->L_data = 1;
36107c478bd9Sstevel@tonic-gate 					VERBOSE((Args & (OCv | OCV)),
36117c478bd9Sstevel@tonic-gate 					    G_p->g_nam_p);
36127c478bd9Sstevel@tonic-gate 				} else if ((Ofile =
36137c478bd9Sstevel@tonic-gate 				    openout(G_p->g_dirfd)) < 0) {
36147c478bd9Sstevel@tonic-gate 					proc_file = F_SKIP;
36157c478bd9Sstevel@tonic-gate 				} else {
36167c478bd9Sstevel@tonic-gate 					data_in(P_PROC);
36177c478bd9Sstevel@tonic-gate 					l_p->L_data = 1;
36187c478bd9Sstevel@tonic-gate 				}
36197c478bd9Sstevel@tonic-gate 			} /* (proc_file = ckname(1)) != F_SKIP */
36207c478bd9Sstevel@tonic-gate 
36217c478bd9Sstevel@tonic-gate 			tl_p = tl_p->L_lnk_p;
36227c478bd9Sstevel@tonic-gate 
36237c478bd9Sstevel@tonic-gate 			close_dirfd();
36247c478bd9Sstevel@tonic-gate 
36257c478bd9Sstevel@tonic-gate 			if (proc_file == F_SKIP && !cleanup) {
36267c478bd9Sstevel@tonic-gate 				tl_p->L_nxt_p = l_p->L_nxt_p;
36277c478bd9Sstevel@tonic-gate 				tl_p->L_bck_p = l_p->L_bck_p;
36287c478bd9Sstevel@tonic-gate 				l_p->L_bck_p->L_nxt_p = tl_p;
36297c478bd9Sstevel@tonic-gate 				l_p->L_nxt_p->L_bck_p = tl_p;
36307c478bd9Sstevel@tonic-gate 				free(l_p->L_gen.g_nam_p);
36317c478bd9Sstevel@tonic-gate 				free(l_p);
36327c478bd9Sstevel@tonic-gate 			}
3633ced83f9bSceastha 		} /* tl_p->L_lnk_p != NULL */
36347c478bd9Sstevel@tonic-gate 		if (l_p->L_data == 0) {
36357c478bd9Sstevel@tonic-gate 			data_in(P_SKIP);
36367c478bd9Sstevel@tonic-gate 		}
36377c478bd9Sstevel@tonic-gate 	}
36387c478bd9Sstevel@tonic-gate 	if (cleanup) {
36397c478bd9Sstevel@tonic-gate 		reclaim(l_p);
36407c478bd9Sstevel@tonic-gate 	}
36417c478bd9Sstevel@tonic-gate }
36427c478bd9Sstevel@tonic-gate 
36437c478bd9Sstevel@tonic-gate /*
36447c478bd9Sstevel@tonic-gate  * file_out:  If the current file is not a special file (!Aspec) and it
36457c478bd9Sstevel@tonic-gate  * is identical to the archive, skip it (do not archive the archive if it
36467c478bd9Sstevel@tonic-gate  * is a regular file).  If creating a TARTYP (TAR or USTAR) archive, the first
36477c478bd9Sstevel@tonic-gate  * time a link to a file is encountered, write the header and file out normally.
36487c478bd9Sstevel@tonic-gate  * Subsequent links to this file put this file name in their t_linkname field.
36497c478bd9Sstevel@tonic-gate  * Otherwise, links are handled in one of two ways, for the old headers
36507c478bd9Sstevel@tonic-gate  * (i.e. binary and -c), linked files are written out as they are encountered.
36517c478bd9Sstevel@tonic-gate  * For the new headers (ASC and CRC), links are saved up until all the links
36527c478bd9Sstevel@tonic-gate  * to each file are found.  For a file with n links, write n - 1 headers with
36537c478bd9Sstevel@tonic-gate  * g_filesz set to 0, write the final (nth) header with the correct g_filesz
36547c478bd9Sstevel@tonic-gate  * value and write the data for the file to the archive.
36557c478bd9Sstevel@tonic-gate  */
36567c478bd9Sstevel@tonic-gate static
36577c478bd9Sstevel@tonic-gate int
36587c478bd9Sstevel@tonic-gate file_out(void)
36597c478bd9Sstevel@tonic-gate {
36607c478bd9Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
36617c478bd9Sstevel@tonic-gate 	int cleanup = 0;
36627c478bd9Sstevel@tonic-gate 	struct Lnk *ttl_p;
36637c478bd9Sstevel@tonic-gate 
36647c478bd9Sstevel@tonic-gate 	G_p = &Gen;
36657c478bd9Sstevel@tonic-gate 	if (!Aspec && IDENT(SrcSt, ArchSt))
36667c478bd9Sstevel@tonic-gate 		return (1); /* do not archive the archive if it's a reg file */
36675fbb8099SNobutomo Nakano 	/*
36685fbb8099SNobutomo Nakano 	 * If compressing sparse files, wait until the compressed file size
36695fbb8099SNobutomo Nakano 	 * is known to check if file size is too big.
36705fbb8099SNobutomo Nakano 	 */
36715fbb8099SNobutomo Nakano 	if (Compress_sparse == 0 && G_p->g_filesz > Max_offset) {
36725fbb8099SNobutomo Nakano 		msg(ERR, "%s%s%s: too large to archive in current mode",
36737c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p,
3674ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? "" : G_p->g_rw_sysattr ?
3675ced83f9bSceastha 		    gettext(" System Attribute ") : gettext(" Attribute "),
3676ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? "" :
3677ced83f9bSceastha 		    ((G_p->g_attrparent_p == NULL) ? G_p->g_attrnam_p:
3678ced83f9bSceastha 		    G_p->g_attrpath_p));
36797c478bd9Sstevel@tonic-gate 		return (1); /* do not archive if it's too big */
36807c478bd9Sstevel@tonic-gate 	}
36817c478bd9Sstevel@tonic-gate 	if (Hdr_type == TAR || Hdr_type == USTAR) { /* TAR and USTAR */
36827c478bd9Sstevel@tonic-gate 		if (Adir) {
3683ced83f9bSceastha 			if (Gen.g_attrnam_p != NULL) {
36847c478bd9Sstevel@tonic-gate 				write_xattr_hdr();
36857c478bd9Sstevel@tonic-gate 			}
36867c478bd9Sstevel@tonic-gate 			write_hdr(ARCHIVE_NORMAL, 0);
36877c478bd9Sstevel@tonic-gate 			return (0);
36887c478bd9Sstevel@tonic-gate 		}
36897c478bd9Sstevel@tonic-gate 		if (G_p->g_nlink == 1) {
36907c478bd9Sstevel@tonic-gate 			data_out();
36917c478bd9Sstevel@tonic-gate 			return (0);
36927c478bd9Sstevel@tonic-gate 		}
36937c478bd9Sstevel@tonic-gate 		tl_p = add_lnk(&ttl_p);
36947c478bd9Sstevel@tonic-gate 		l_p = ttl_p;
36957c478bd9Sstevel@tonic-gate 		if (tl_p == l_p) { /* first link to this file encountered */
36967c478bd9Sstevel@tonic-gate 			data_out();
36977c478bd9Sstevel@tonic-gate 			return (0);
36987c478bd9Sstevel@tonic-gate 		}
36997c478bd9Sstevel@tonic-gate 		(void) strncpy(T_lname, l_p->L_gen.g_nam_p,
37007c478bd9Sstevel@tonic-gate 		    l_p->L_gen.g_namesz);
37017c478bd9Sstevel@tonic-gate 
37027c478bd9Sstevel@tonic-gate 		/*
37037c478bd9Sstevel@tonic-gate 		 * check if linkname is greater than 100 characters
37047c478bd9Sstevel@tonic-gate 		 */
37057c478bd9Sstevel@tonic-gate 		if (strlen(T_lname) > NAMSIZ) {
37067c478bd9Sstevel@tonic-gate 			msg(EPOST, "cpio: %s: linkname %s is greater than %d",
37077c478bd9Sstevel@tonic-gate 			    G_p->g_nam_p, T_lname, NAMSIZ);
37087c478bd9Sstevel@tonic-gate 			return (1);
37097c478bd9Sstevel@tonic-gate 		}
37107c478bd9Sstevel@tonic-gate 
37117c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
37127c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), tl_p->L_gen.g_nam_p);
37137c478bd9Sstevel@tonic-gate 
37147c478bd9Sstevel@tonic-gate 		/* find the lnk entry in sublist, unlink it, and free it */
37157c478bd9Sstevel@tonic-gate 		for (; ttl_p->L_lnk_p != NULL;
37167c478bd9Sstevel@tonic-gate 		    ttl_p = ttl_p->L_lnk_p) {
37177c478bd9Sstevel@tonic-gate 			if (ttl_p->L_lnk_p == tl_p) {
37187c478bd9Sstevel@tonic-gate 				ttl_p->L_lnk_p = tl_p->L_lnk_p;
37197c478bd9Sstevel@tonic-gate 				free(tl_p->L_gen.g_nam_p);
37207c478bd9Sstevel@tonic-gate 				free(tl_p);
37217c478bd9Sstevel@tonic-gate 				break;
37227c478bd9Sstevel@tonic-gate 			}
37237c478bd9Sstevel@tonic-gate 		}
37247c478bd9Sstevel@tonic-gate 
37257c478bd9Sstevel@tonic-gate 		return (0);
37267c478bd9Sstevel@tonic-gate 	}
37277c478bd9Sstevel@tonic-gate 	if (Adir) {
37287c478bd9Sstevel@tonic-gate 		/*
37297c478bd9Sstevel@tonic-gate 		 * ACL has been retrieved in getname().
37307c478bd9Sstevel@tonic-gate 		 */
37317c478bd9Sstevel@tonic-gate 		if (Pflag) {
37327c478bd9Sstevel@tonic-gate 			char    *secinfo = NULL;
37337c478bd9Sstevel@tonic-gate 			int	len = 0;
37347c478bd9Sstevel@tonic-gate 
37357c478bd9Sstevel@tonic-gate 			/* append security attributes */
3736fa9e4066Sahrens 			if ((append_secattr(&secinfo, &len, aclp)) == -1)
37377c478bd9Sstevel@tonic-gate 				msg(ERR, "can create security information");
37387c478bd9Sstevel@tonic-gate 
37397c478bd9Sstevel@tonic-gate 			/* call append_secattr() if more than one */
37407c478bd9Sstevel@tonic-gate 
37417c478bd9Sstevel@tonic-gate 			if (len > 0) {
37427c478bd9Sstevel@tonic-gate 			/* write ancillary */
37435fbb8099SNobutomo Nakano 				write_hdr(ARCHIVE_ACL, (off_t)len);
37445fbb8099SNobutomo Nakano 				write_ancillary(secinfo, len, B_TRUE);
37457c478bd9Sstevel@tonic-gate 			}
37467c478bd9Sstevel@tonic-gate 		}
37477c478bd9Sstevel@tonic-gate 
3748ced83f9bSceastha 		if (Gen.g_attrnam_p != NULL) {
37497c478bd9Sstevel@tonic-gate 			write_xattr_hdr();
37507c478bd9Sstevel@tonic-gate 		}
37517c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
37527c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
37537c478bd9Sstevel@tonic-gate 		return (0);
37547c478bd9Sstevel@tonic-gate 	}
37557c478bd9Sstevel@tonic-gate 	if (G_p->g_nlink == 1) {
37567c478bd9Sstevel@tonic-gate 		data_out();
37577c478bd9Sstevel@tonic-gate 		return (0);
37587c478bd9Sstevel@tonic-gate 	} else {
37597c478bd9Sstevel@tonic-gate 		tl_p = add_lnk(&ttl_p);
37607c478bd9Sstevel@tonic-gate 		l_p = ttl_p;
37617c478bd9Sstevel@tonic-gate 
37627c478bd9Sstevel@tonic-gate 		if (l_p->L_cnt == l_p->L_gen.g_nlink)
37637c478bd9Sstevel@tonic-gate 			cleanup = 1;
3764ced83f9bSceastha 		else if (Onecopy && G_p->g_attrnam_p == NULL) {
37657c478bd9Sstevel@tonic-gate 			return (0); /* don't process data yet */
37667c478bd9Sstevel@tonic-gate 		}
37677c478bd9Sstevel@tonic-gate 	}
3768ced83f9bSceastha 	if (Onecopy && G_p->g_attrnam_p == NULL) {
37697c478bd9Sstevel@tonic-gate 		tl_p = l_p;
3770ced83f9bSceastha 		while (tl_p->L_lnk_p != NULL) {
37717c478bd9Sstevel@tonic-gate 			G_p = &tl_p->L_gen;
37727c478bd9Sstevel@tonic-gate 			G_p->g_filesz = (off_t)0;
37737c478bd9Sstevel@tonic-gate 			/* one link with the acl is sufficient */
37747c478bd9Sstevel@tonic-gate 			write_hdr(ARCHIVE_NORMAL, (off_t)0);
37757c478bd9Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
37767c478bd9Sstevel@tonic-gate 			tl_p = tl_p->L_lnk_p;
37777c478bd9Sstevel@tonic-gate 		}
37787c478bd9Sstevel@tonic-gate 		G_p = &tl_p->L_gen;
37797c478bd9Sstevel@tonic-gate 		if (open_dirfd() != 0)
37807c478bd9Sstevel@tonic-gate 			return (1);
37817c478bd9Sstevel@tonic-gate 	}
37827c478bd9Sstevel@tonic-gate 	/* old style: has acl and data for every link */
37837c478bd9Sstevel@tonic-gate 	data_out();
37847c478bd9Sstevel@tonic-gate 	if (cleanup)
37857c478bd9Sstevel@tonic-gate 		reclaim(l_p);
37867c478bd9Sstevel@tonic-gate 	return (0);
37877c478bd9Sstevel@tonic-gate }
37887c478bd9Sstevel@tonic-gate 
37897c478bd9Sstevel@tonic-gate /*
3790ced83f9bSceastha  * Verify the underlying file system supports the attribute type.
3791ced83f9bSceastha  * Only archive extended attribute files when '-@' was specified.
3792ced83f9bSceastha  * Only archive system extended attribute files if '-/' was specified.
3793ced83f9bSceastha  */
3794ced83f9bSceastha #if defined(O_XATTR)
3795ced83f9bSceastha static attr_status_t
3796ced83f9bSceastha verify_attr_support(char *filename, int attrflg, arc_action_t actflag,
3797ced83f9bSceastha     int *ext_attrflg)
3798ced83f9bSceastha {
3799ced83f9bSceastha 	/*
3800ced83f9bSceastha 	 * Verify extended attributes are supported/exist.  We only
3801ced83f9bSceastha 	 * need to check if we are processing a base file, not an
3802ced83f9bSceastha 	 * extended attribute.
3803ced83f9bSceastha 	 */
3804ced83f9bSceastha 	if (attrflg) {
3805ced83f9bSceastha 		*ext_attrflg = (pathconf(filename, (actflag == ARC_CREATE) ?
3806ced83f9bSceastha 		    _PC_XATTR_EXISTS : _PC_XATTR_ENABLED) == 1);
3807ced83f9bSceastha 	}
3808ced83f9bSceastha 	if (Atflag) {
3809ced83f9bSceastha #if defined(_PC_SATTR_ENABLED)
3810ced83f9bSceastha 		if (!*ext_attrflg) {
3811ced83f9bSceastha 			if (SysAtflag) {
3812ced83f9bSceastha 				/* Verify system attributes are supported */
3813ced83f9bSceastha 				if (sysattr_support(filename,
3814ced83f9bSceastha 				    (actflag == ARC_CREATE) ?_PC_SATTR_EXISTS :
3815ced83f9bSceastha 				    _PC_SATTR_ENABLED) != 1) {
3816ced83f9bSceastha 					return (ATTR_SATTR_ERR);
3817ced83f9bSceastha 				}
3818ced83f9bSceastha 			} else
3819ced83f9bSceastha 				return (ATTR_XATTR_ERR);
3820ced83f9bSceastha #else
3821ced83f9bSceastha 				return (ATTR_XATTR_ERR);
3822ced83f9bSceastha #endif  /* _PC_SATTR_ENABLED */
3823ced83f9bSceastha 		}
3824ced83f9bSceastha 
3825ced83f9bSceastha #if defined(_PC_SATTR_ENABLED)
3826ced83f9bSceastha 	} else if (SysAtflag) {
3827ced83f9bSceastha 		/* Verify system attributes are supported */
3828ced83f9bSceastha 		if (sysattr_support(filename, (actflag == ARC_CREATE) ?
3829ced83f9bSceastha 		    _PC_SATTR_EXISTS : _PC_SATTR_ENABLED) != 1) {
3830ced83f9bSceastha 			return (ATTR_SATTR_ERR);
3831ced83f9bSceastha 	}
3832ced83f9bSceastha #endif  /* _PC_SATTR_ENABLED */
3833ced83f9bSceastha 	} else {
3834ced83f9bSceastha 		return (ATTR_SKIP);
3835ced83f9bSceastha 	}
3836ced83f9bSceastha 
3837ced83f9bSceastha return (ATTR_OK);
3838ced83f9bSceastha }
3839ced83f9bSceastha #endif
3840ced83f9bSceastha 
3841ced83f9bSceastha #if defined(O_XATTR)
3842ced83f9bSceastha /*
3843ced83f9bSceastha  * Verify the attribute, attrname, is an attribute we want to restore.
3844ced83f9bSceastha  * Never restore read-only system attribute files.  Only restore read-write
3845ced83f9bSceastha  * system attributes files when -/ was specified, and only traverse into
3846ced83f9bSceastha  * the 2nd level attribute directory containing only system attributes if
3847ced83f9bSceastha  * -@ was specified.  This keeps us from archiving
3848ced83f9bSceastha  *	<attribute name>/<read-write system attribute file>
3849ced83f9bSceastha  * when -/ was specified without -@.
3850ced83f9bSceastha  *
3851ced83f9bSceastha  * attrname		- attribute file name
3852ced83f9bSceastha  * attrparent		- attribute's parent name within the base file's
3853ced83f9bSceastha  *			attribute digrectory hierarchy
3854ced83f9bSceastha  * arc_rwsysattr	- flag that indicates that read-write system attribute
3855ced83f9bSceastha  *			file should be archived as it contains other than
3856ced83f9bSceastha  *			the default system attributes.
3857ced83f9bSceastha  * rw_sysattr		- on return, flag will indicate if attrname is a
3858ced83f9bSceastha  *			read-write system attribute file.
3859ced83f9bSceastha  */
3860ced83f9bSceastha static attr_status_t
3861ced83f9bSceastha verify_attr(char *attrname, char *attrparent, int arc_rwsysattr,
3862ced83f9bSceastha     int *rw_sysattr)
3863ced83f9bSceastha {
3864ced83f9bSceastha #if defined(_PC_SATTR_ENABLED)
3865ced83f9bSceastha 	int	attr_supported;
3866ced83f9bSceastha 
3867ced83f9bSceastha 	/* Never restore read-only system attribute files */
3868ced83f9bSceastha 	if ((attr_supported = sysattr_type(attrname)) == _RO_SATTR) {
3869ced83f9bSceastha 		*rw_sysattr = 0;
3870ced83f9bSceastha 		return (ATTR_SKIP);
3871ced83f9bSceastha 	} else {
3872ced83f9bSceastha 		*rw_sysattr = (attr_supported == _RW_SATTR);
3873ced83f9bSceastha 	}
3874ced83f9bSceastha 
3875ced83f9bSceastha 	/*
3876ced83f9bSceastha 	 * Don't archive a read-write system attribute file if
3877ced83f9bSceastha 	 * it contains only the default system attributes.
3878ced83f9bSceastha 	 */
3879ced83f9bSceastha 	if (*rw_sysattr && !arc_rwsysattr) {
3880ced83f9bSceastha 		return (ATTR_SKIP);
3881ced83f9bSceastha 	}
3882ced83f9bSceastha 
3883ced83f9bSceastha #else
3884ced83f9bSceastha 	/* Never restore read-only system attribute files */
3885ced83f9bSceastha 	if ((*rw_sysattr = is_sysattr(attrname)) == 1) {
3886ced83f9bSceastha 		return (ATTR_SKIP);
3887ced83f9bSceastha 	}
3888ced83f9bSceastha #endif	/* _PC_SATTR_ENABLED */
3889ced83f9bSceastha 
3890ced83f9bSceastha 	/*
3891ced83f9bSceastha 	 * Only restore read-write system attribute files
3892ced83f9bSceastha 	 * when -/ was specified.  Only restore extended
3893ced83f9bSceastha 	 * attributes when -@ was specified.
3894ced83f9bSceastha 	 */
3895ced83f9bSceastha 	if (Atflag) {
3896ced83f9bSceastha 		if (!SysAtflag) {
3897ced83f9bSceastha 			/*
3898ced83f9bSceastha 			 * Only archive/restore the hidden directory "." if
3899ced83f9bSceastha 			 * we're processing the top level hidden attribute
3900ced83f9bSceastha 			 * directory.  We don't want to process the
3901ced83f9bSceastha 			 * hidden attribute directory of the attribute
3902ced83f9bSceastha 			 * directory that contains only extended system
3903ced83f9bSceastha 			 * attributes.
3904ced83f9bSceastha 			 */
3905ced83f9bSceastha 			if (*rw_sysattr || (Hiddendir &&
3906ced83f9bSceastha 			    (attrparent != NULL))) {
3907ced83f9bSceastha 				return (ATTR_SKIP);
3908ced83f9bSceastha 			}
3909ced83f9bSceastha 		}
3910ced83f9bSceastha 	} else if (SysAtflag) {
3911ced83f9bSceastha 		/*
3912ced83f9bSceastha 		 * Only archive/restore read-write extended system attribute
3913ced83f9bSceastha 		 * files of the base file.
3914ced83f9bSceastha 		 */
3915ced83f9bSceastha 		if (!*rw_sysattr || (attrparent != NULL)) {
3916ced83f9bSceastha 			return (ATTR_SKIP);
3917ced83f9bSceastha 		}
3918ced83f9bSceastha 	} else {
3919ced83f9bSceastha 		return (ATTR_SKIP);
3920ced83f9bSceastha 	}
3921ced83f9bSceastha 
3922ced83f9bSceastha 	return (ATTR_OK);
3923ced83f9bSceastha }
3924ced83f9bSceastha #endif
3925ced83f9bSceastha 
3926ced83f9bSceastha #if defined(O_XATTR)
3927ced83f9bSceastha static int
3928ced83f9bSceastha retry_open_attr(int pdirfd, int cwd, char *fullname, char *pattr, char *name,
3929ced83f9bSceastha     int oflag, mode_t mode)
3930ced83f9bSceastha {
3931ced83f9bSceastha 	int dirfd;
3932ced83f9bSceastha 	int ofilefd = -1;
3933ced83f9bSceastha 	struct timeval times[2];
3934ced83f9bSceastha 	mode_t newmode;
3935ced83f9bSceastha 	struct stat parentstat;
3936ced83f9bSceastha 	acl_t *aclp = NULL;
3937ced83f9bSceastha 	int error;
3938ced83f9bSceastha 
3939ced83f9bSceastha 	/*
3940ced83f9bSceastha 	 * We couldn't get to attrdir. See if its
3941ced83f9bSceastha 	 * just a mode problem on the parent file.
3942ced83f9bSceastha 	 * for example: a mode such as r-xr--r--
3943ced83f9bSceastha 	 * on a ufs file system without extended
3944ced83f9bSceastha 	 * system attribute support won't let us
3945ced83f9bSceastha 	 * create an attribute dir if it doesn't
3946ced83f9bSceastha 	 * already exist, and on a ufs file system
3947ced83f9bSceastha 	 * with extended system attribute support
3948ced83f9bSceastha 	 * won't let us open the attribute for
3949ced83f9bSceastha 	 * write.
3950ced83f9bSceastha 	 *
3951ced83f9bSceastha 	 * If file has a non-trivial ACL, then save it
3952ced83f9bSceastha 	 * off so that we can place it back on after doing
3953ced83f9bSceastha 	 * chmod's.
3954ced83f9bSceastha 	 */
3955ced83f9bSceastha 	if ((dirfd = openat(cwd, (pattr == NULL) ? fullname : pattr,
3956ced83f9bSceastha 	    O_RDONLY)) == -1) {
3957ced83f9bSceastha 		return (-1);
3958ced83f9bSceastha 	}
3959ced83f9bSceastha 	if (fstat(dirfd, &parentstat) == -1) {
3960ced83f9bSceastha 		msg(ERRN, "Cannot stat %sfile %s",
3961ced83f9bSceastha 		    (pdirfd == -1) ? "" : gettext("parent of "),
3962ced83f9bSceastha 		    (pdirfd == -1) ? fullname : name);
3963ced83f9bSceastha 		(void) close(dirfd);
3964ced83f9bSceastha 		return (-1);
3965ced83f9bSceastha 	}
3966ced83f9bSceastha 	if ((error = facl_get(dirfd, ACL_NO_TRIVIAL, &aclp)) != 0) {
3967ced83f9bSceastha 		msg(ERRN, "Failed to retrieve ACL on %sfile %s",
3968ced83f9bSceastha 		    (pdirfd == -1) ? "" : gettext("parent of "),
3969ced83f9bSceastha 		    (pdirfd == -1) ? fullname : name);
3970ced83f9bSceastha 		(void) close(dirfd);
3971ced83f9bSceastha 		return (-1);
3972ced83f9bSceastha 	}
3973ced83f9bSceastha 
3974ced83f9bSceastha 	newmode = S_IWUSR | parentstat.st_mode;
3975ced83f9bSceastha 	if (fchmod(dirfd, newmode) == -1) {
3976ced83f9bSceastha 		msg(ERRN, "Cannot change mode of %sfile %s to %o",
3977ced83f9bSceastha 		    (pdirfd == -1) ? "" : gettext("parent of "),
3978ced83f9bSceastha 		    (pdirfd == -1) ? fullname : name, newmode);
3979ced83f9bSceastha 		if (aclp)
3980ced83f9bSceastha 			acl_free(aclp);
3981ced83f9bSceastha 		(void) close(dirfd);
3982ced83f9bSceastha 		return (-1);
3983ced83f9bSceastha 	}
3984ced83f9bSceastha 
3985ced83f9bSceastha 
3986ced83f9bSceastha 	if (pdirfd == -1) {
3987ced83f9bSceastha 		/*
3988ced83f9bSceastha 		 * We weren't able to create the attribute directory before.
3989ced83f9bSceastha 		 * Now try again.
3990ced83f9bSceastha 		 */
3991ced83f9bSceastha 		ofilefd = attropen(fullname, ".", oflag);
3992ced83f9bSceastha 	} else {
3993ced83f9bSceastha 		/*
3994ced83f9bSceastha 		 * We weren't able to create open the attribute before.
3995ced83f9bSceastha 		 * Now try again.
3996ced83f9bSceastha 		 */
3997ced83f9bSceastha 		ofilefd = openat(pdirfd, name, oflag, mode);
3998ced83f9bSceastha 	}
3999ced83f9bSceastha 
4000ced83f9bSceastha 	/*
4001ced83f9bSceastha 	 * Put mode back to original
4002ced83f9bSceastha 	 */
4003ced83f9bSceastha 	if (fchmod(dirfd, parentstat.st_mode) == -1) {
4004ced83f9bSceastha 		msg(ERRN, "Cannot restore permissions of %sfile %s to %o",
4005ced83f9bSceastha 		    (pdirfd == -1) ? "" : gettext("parent of "),
4006ced83f9bSceastha 		    (pdirfd == -1) ? fullname : name, newmode);
4007ced83f9bSceastha 	}
4008ced83f9bSceastha 
4009ced83f9bSceastha 	if (aclp) {
4010ced83f9bSceastha 		error = facl_set(dirfd, aclp);
4011ced83f9bSceastha 		if (error) {
4012ced83f9bSceastha 			msg(ERRN, "failed to set acl entries on %sfile %s\n",
4013ced83f9bSceastha 			    (pdirfd == -1) ? "" : gettext("parent of "),
4014ced83f9bSceastha 			    (pdirfd == -1) ? fullname : name);
4015ced83f9bSceastha 		}
4016ced83f9bSceastha 		acl_free(aclp);
4017ced83f9bSceastha 	}
4018ced83f9bSceastha 
4019ced83f9bSceastha 	/*
4020ced83f9bSceastha 	 * Put back time stamps
4021ced83f9bSceastha 	 */
4022ced83f9bSceastha 
4023ced83f9bSceastha 	times[0].tv_sec = parentstat.st_atime;
4024ced83f9bSceastha 	times[0].tv_usec = 0;
4025ced83f9bSceastha 	times[1].tv_sec = parentstat.st_mtime;
4026ced83f9bSceastha 	times[1].tv_usec = 0;
4027ced83f9bSceastha 
4028ced83f9bSceastha 	(void) futimesat(cwd, (pattr == NULL) ? fullname : pattr, times);
4029ced83f9bSceastha 
4030ced83f9bSceastha 	(void) close(dirfd);
4031ced83f9bSceastha 
4032ced83f9bSceastha 	return (ofilefd);
4033ced83f9bSceastha }
4034ced83f9bSceastha #endif
4035ced83f9bSceastha 
4036ced83f9bSceastha #if defined(O_XATTR)
4037ced83f9bSceastha /*
4038ced83f9bSceastha  * Recursively open attribute directories until the attribute directory
4039ced83f9bSceastha  * containing the specified attribute, attrname, is opened.
4040ced83f9bSceastha  *
4041ced83f9bSceastha  * Currently, only 2 directory levels of attributes are supported, (i.e.,
4042ced83f9bSceastha  * extended system attributes on extended attributes).  The following are
4043ced83f9bSceastha  * the possible input combinations:
4044ced83f9bSceastha  *	1.  Open the attribute directory of the base file (don't change
4045ced83f9bSceastha  *	    into it).
4046ced83f9bSceastha  *		attr_parent = NULL
4047ced83f9bSceastha  *		attrname = '.'
4048ced83f9bSceastha  *	2.  Open the attribute directory of the base file and change into it.
4049ced83f9bSceastha  *		attr_parent = NULL
4050ced83f9bSceastha  *		attrname = <attr> | <sys_attr>
4051ced83f9bSceastha  *	3.  Open the attribute directory of the base file, change into it,
4052ced83f9bSceastha  *	    then recursively call open_attr_dir() to open the attribute's
4053ced83f9bSceastha  *	    parent directory (don't change into it).
4054ced83f9bSceastha  *		attr_parent = <attr>
4055ced83f9bSceastha  *		attrname = '.'
4056ced83f9bSceastha  *	4.  Open the attribute directory of the base file, change into it,
4057ced83f9bSceastha  *	    then recursively call open_attr_dir() to open the attribute's
4058ced83f9bSceastha  *	    parent directory and change into it.
4059ced83f9bSceastha  *		attr_parent = <attr>
4060ced83f9bSceastha  *		attrname = <attr> | <sys_attr>
4061ced83f9bSceastha  *
4062ced83f9bSceastha  * An attribute directory will be opened only if the underlying file system
4063ced83f9bSceastha  * supports the attribute type, and if the command line specifications
4064ced83f9bSceastha  * (f_extended_attr and f_sys_attr) enable the processing of the attribute
4065ced83f9bSceastha  * type.
4066ced83f9bSceastha  *
4067ced83f9bSceastha  * On succesful return, attr_parentfd will be the file descriptor of the
4068ced83f9bSceastha  * opened attribute directory.  In addition, if the attribute is a read-write
4069ced83f9bSceastha  * extended system attribute, rw_sysattr will be set to 1, otherwise
4070ced83f9bSceastha  * it will be set to 0.
4071ced83f9bSceastha  *
4072ced83f9bSceastha  * Possible return values:
4073ced83f9bSceastha  * 	ATTR_OK		Successfully opened and, if needed, changed into the
4074ced83f9bSceastha  *			attribute directory containing attrname.
4075ced83f9bSceastha  *	ATTR_SKIP	The command line specifications don't enable the
4076ced83f9bSceastha  *			processing of the attribute type.
4077ced83f9bSceastha  * 	ATTR_CHDIR_ERR	An error occurred while trying to change into an
4078ced83f9bSceastha  *			attribute directory.
4079ced83f9bSceastha  * 	ATTR_OPEN_ERR	An error occurred while trying to open an
4080ced83f9bSceastha  *			attribute directory.
4081ced83f9bSceastha  *	ATTR_XATTR_ERR	The underlying file system doesn't support extended
4082ced83f9bSceastha  *			attributes.
4083ced83f9bSceastha  *	ATTR_SATTR_ERR	The underlying file system doesn't support extended
4084ced83f9bSceastha  *			system attributes.
4085ced83f9bSceastha  */
4086ced83f9bSceastha static int
4087ced83f9bSceastha open_attr_dir(char *attrname, char *dirp, int cwd, char *attr_parent,
4088ced83f9bSceastha     int *attr_parentfd, int *rw_sysattr)
4089ced83f9bSceastha {
4090ced83f9bSceastha 	attr_status_t	rc;
4091ced83f9bSceastha 	int		firsttime = (*attr_parentfd == -1);
4092ced83f9bSceastha 	int		saveerrno;
4093ced83f9bSceastha 	int		ext_attr;
4094ced83f9bSceastha 
4095ced83f9bSceastha 	/*
4096ced83f9bSceastha 	 * open_attr_dir() was recursively called (input combination number 4),
4097ced83f9bSceastha 	 * close the previously opened file descriptor as we've already changed
4098ced83f9bSceastha 	 * into it.
4099ced83f9bSceastha 	 */
4100ced83f9bSceastha 	if (!firsttime) {
4101ced83f9bSceastha 		(void) close(*attr_parentfd);
4102ced83f9bSceastha 		*attr_parentfd = -1;
4103ced83f9bSceastha 	}
4104ced83f9bSceastha 
4105ced83f9bSceastha 	/*
4106ced83f9bSceastha 	 * Verify that the underlying file system supports the restoration
4107ced83f9bSceastha 	 * of the attribute.
4108ced83f9bSceastha 	 */
4109ced83f9bSceastha 	if ((rc = verify_attr_support(dirp, firsttime, ARC_RESTORE,
4110ced83f9bSceastha 	    &ext_attr)) != ATTR_OK) {
4111ced83f9bSceastha 		return (rc);
4112ced83f9bSceastha 	}
4113ced83f9bSceastha 
4114ced83f9bSceastha 	/* Open the base file's attribute directory */
4115ced83f9bSceastha 	if ((*attr_parentfd = attropen(dirp, ".", O_RDONLY)) == -1) {
4116ced83f9bSceastha 		/*
4117ced83f9bSceastha 		 * Save the errno from the attropen so it can be reported
4118ced83f9bSceastha 		 * if the retry of the attropen fails.
4119ced83f9bSceastha 		 */
4120ced83f9bSceastha 		saveerrno = errno;
4121ced83f9bSceastha 		if ((*attr_parentfd = retry_open_attr(-1, cwd, dirp,
4122ced83f9bSceastha 		    NULL, ".", O_RDONLY, 0)) == -1) {
4123ced83f9bSceastha 			(void) close(*attr_parentfd);
4124ced83f9bSceastha 			*attr_parentfd = -1;
4125ced83f9bSceastha 			errno = saveerrno;
4126ced83f9bSceastha 			return (ATTR_OPEN_ERR);
4127ced83f9bSceastha 		}
4128ced83f9bSceastha 	}
4129ced83f9bSceastha 
4130ced83f9bSceastha 	/*
4131ced83f9bSceastha 	 * Change into the parent attribute's directory unless we are
4132ced83f9bSceastha 	 * processing the hidden attribute directory of the base file itself.
4133ced83f9bSceastha 	 */
4134ced83f9bSceastha 	if ((Hiddendir == 0) || (firsttime && (attr_parent != NULL))) {
4135ced83f9bSceastha 		if (fchdir(*attr_parentfd) != 0) {
4136ced83f9bSceastha 			saveerrno = errno;
4137ced83f9bSceastha 			(void) close(*attr_parentfd);
4138ced83f9bSceastha 			*attr_parentfd = -1;
4139ced83f9bSceastha 			errno = saveerrno;
4140ced83f9bSceastha 			return (ATTR_CHDIR_ERR);
4141ced83f9bSceastha 		}
4142ced83f9bSceastha 	}
4143ced83f9bSceastha 
4144ced83f9bSceastha 	/* Determine if the attribute should be processed */
4145ced83f9bSceastha 	if ((rc = verify_attr(attrname, attr_parent, 1,
4146ced83f9bSceastha 	    rw_sysattr)) != ATTR_OK) {
4147ced83f9bSceastha 		saveerrno = errno;
4148ced83f9bSceastha 		(void) close(*attr_parentfd);
4149ced83f9bSceastha 		*attr_parentfd = -1;
4150ced83f9bSceastha 		errno = saveerrno;
4151ced83f9bSceastha 		return (rc);
4152ced83f9bSceastha 	}
4153ced83f9bSceastha 
4154ced83f9bSceastha 	/*
4155ced83f9bSceastha 	 * If the attribute is an extended system attribute of an attribute
4156ced83f9bSceastha 	 * (i.e., <attr>/<sys_attr>), then recursively call open_attr_dir() to
4157ced83f9bSceastha 	 * open the attribute directory of the parent attribute.
4158ced83f9bSceastha 	 */
4159ced83f9bSceastha 	if (firsttime && (attr_parent != NULL)) {
4160ced83f9bSceastha 		return (open_attr_dir(attrname, attr_parent, *attr_parentfd,
4161ced83f9bSceastha 		    attr_parent, attr_parentfd, rw_sysattr));
4162ced83f9bSceastha 	}
4163ced83f9bSceastha 
4164ced83f9bSceastha 	return (ATTR_OK);
4165ced83f9bSceastha }
4166ced83f9bSceastha #endif
4167ced83f9bSceastha 
4168ced83f9bSceastha /*
41697c478bd9Sstevel@tonic-gate  * file_pass:  If the -l option is set (link files when possible), and the
41707c478bd9Sstevel@tonic-gate  * source and destination file systems are the same, link the source file
41717c478bd9Sstevel@tonic-gate  * (G_p->g_nam_p) to the destination file (Fullnam) and return.  If not a
41727c478bd9Sstevel@tonic-gate  * linked file, transfer the data.  Otherwise, the first link to a file
41737c478bd9Sstevel@tonic-gate  * encountered is transferred normally and subsequent links are linked to it.
41747c478bd9Sstevel@tonic-gate  */
41757c478bd9Sstevel@tonic-gate 
41767c478bd9Sstevel@tonic-gate static int
41777c478bd9Sstevel@tonic-gate file_pass(void)
41787c478bd9Sstevel@tonic-gate {
41797c478bd9Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
41807c478bd9Sstevel@tonic-gate 	struct Lnk *ttl_p;
41817c478bd9Sstevel@tonic-gate 	char *save_name;
41827c478bd9Sstevel@tonic-gate 	int size;
41837c478bd9Sstevel@tonic-gate 	int cwd;
41847c478bd9Sstevel@tonic-gate 	char *lfrom, *lto;
41857c478bd9Sstevel@tonic-gate 
41867c478bd9Sstevel@tonic-gate 	G_p = &Gen;
41877c478bd9Sstevel@tonic-gate 
41887c478bd9Sstevel@tonic-gate 	if (Adir && !(Args & OCd)) {
41897c478bd9Sstevel@tonic-gate 		msg(ERR, "Use -d option to copy \"%s\"", G_p->g_nam_p);
41907c478bd9Sstevel@tonic-gate 		return (FILE_PASS_ERR);
41917c478bd9Sstevel@tonic-gate 	}
41927c478bd9Sstevel@tonic-gate 
41937c478bd9Sstevel@tonic-gate 	save_name = G_p->g_nam_p;
41947c478bd9Sstevel@tonic-gate 
41957c478bd9Sstevel@tonic-gate 	while (*(G_p->g_nam_p) == '/') {
41967c478bd9Sstevel@tonic-gate 		G_p->g_nam_p++;
41977c478bd9Sstevel@tonic-gate 	}
41987c478bd9Sstevel@tonic-gate 
4199ced83f9bSceastha 	(void) strcpy(Full_p, (G_p->g_attrfnam_p == NULL) ?
42007c478bd9Sstevel@tonic-gate 	    G_p->g_nam_p : G_p->g_attrfnam_p);
42017c478bd9Sstevel@tonic-gate 
4202ced83f9bSceastha 	if (G_p->g_attrnam_p == NULL) {
42037c478bd9Sstevel@tonic-gate 		G_p->g_passdirfd = open_dir(Fullnam_p);
42047c478bd9Sstevel@tonic-gate 
42057c478bd9Sstevel@tonic-gate 		if (G_p->g_passdirfd == -1) {
42067c478bd9Sstevel@tonic-gate 			msg(ERRN,
42077c478bd9Sstevel@tonic-gate 			    "Cannot open/create \"%s\"", Fullnam_p);
42087c478bd9Sstevel@tonic-gate 			return (FILE_PASS_ERR);
42097c478bd9Sstevel@tonic-gate 		}
42107c478bd9Sstevel@tonic-gate 	} else {
4211ced83f9bSceastha 		int	rw_sysattr;
42127c478bd9Sstevel@tonic-gate 
4213ced83f9bSceastha 		/*
4214ced83f9bSceastha 		 * Open the file's attribute directory.
4215ced83f9bSceastha 		 * Change into the base file's starting directory then call
4216ced83f9bSceastha 		 * open_attr_dir() to open the attribute directory of either
4217ced83f9bSceastha 		 * the base file (if G_p->g_attrparent_p is NULL) or the
4218ced83f9bSceastha 		 * attribute (if G_p->g_attrparent_p is set) of the base file.
4219ced83f9bSceastha 		 */
42207c478bd9Sstevel@tonic-gate 
4221ced83f9bSceastha 		G_p->g_passdirfd = -1;
4222ced83f9bSceastha 		(void) fchdir(G_p->g_baseparent_fd);
4223ced83f9bSceastha 		(void) open_attr_dir(G_p->g_attrnam_p, Fullnam_p,
4224ced83f9bSceastha 		    G_p->g_baseparent_fd, (G_p->g_attrparent_p == NULL) ? NULL :
4225ced83f9bSceastha 		    G_p->g_attrparent_p, &G_p->g_passdirfd, &rw_sysattr);
42267c478bd9Sstevel@tonic-gate 		if (G_p->g_passdirfd == -1) {
42277c478bd9Sstevel@tonic-gate 			msg(ERRN,
42287c478bd9Sstevel@tonic-gate 			    "Cannot open attribute directory of "
4229ced83f9bSceastha 			    "%s%s%sfile \"%s\"",
4230ced83f9bSceastha 			    (G_p->g_attrparent_p == NULL) ? "" :
4231ced83f9bSceastha 			    gettext("attribute \""),
4232ced83f9bSceastha 			    (G_p->g_attrparent_p == NULL) ? "" :
4233ced83f9bSceastha 			    G_p->g_attrparent_p,
4234ced83f9bSceastha 			    (G_p->g_attrparent_p == NULL) ? "" :
4235ced83f9bSceastha 			    gettext("\" of "), Fullnam_p);
42367c478bd9Sstevel@tonic-gate 			return (FILE_PASS_ERR);
42377c478bd9Sstevel@tonic-gate 		}
42387c478bd9Sstevel@tonic-gate 	}
42397c478bd9Sstevel@tonic-gate 
42407c478bd9Sstevel@tonic-gate 	if (Args & OCl) {
42417c478bd9Sstevel@tonic-gate 		/* We are linking back to the source directory. */
42427c478bd9Sstevel@tonic-gate 
42437c478bd9Sstevel@tonic-gate 		if (!Adir) {
42447c478bd9Sstevel@tonic-gate 			char *existingfile = save_name;
42457c478bd9Sstevel@tonic-gate 
42467c478bd9Sstevel@tonic-gate 			if ((Args & OCL) && issymlink) {
42477c478bd9Sstevel@tonic-gate 				/* We are chasing symlinks. */
42487c478bd9Sstevel@tonic-gate 
42497c478bd9Sstevel@tonic-gate 				if ((size = readlink(save_name, Symlnk_p,
42507c478bd9Sstevel@tonic-gate 				    MAXPATHLEN)) < 0) {
42517c478bd9Sstevel@tonic-gate 					msg(ERRN,
42527c478bd9Sstevel@tonic-gate 					    "Cannot read symbolic link \"%s\"",
42537c478bd9Sstevel@tonic-gate 					    save_name);
42547c478bd9Sstevel@tonic-gate 					return (FILE_PASS_ERR);
42557c478bd9Sstevel@tonic-gate 				}
42567c478bd9Sstevel@tonic-gate 
42577c478bd9Sstevel@tonic-gate 				Symlnk_p[size] = '\0';
42587c478bd9Sstevel@tonic-gate 				existingfile = Symlnk_p;
42597c478bd9Sstevel@tonic-gate 			}
42607c478bd9Sstevel@tonic-gate 
4261ced83f9bSceastha 			if (G_p->g_attrnam_p == NULL) {
42627c478bd9Sstevel@tonic-gate 				if (creat_lnk(G_p->g_passdirfd,
42637c478bd9Sstevel@tonic-gate 				    existingfile, Fullnam_p) == 0) {
42647c478bd9Sstevel@tonic-gate 					return (FILE_LINKED);
42657c478bd9Sstevel@tonic-gate 				}
42667c478bd9Sstevel@tonic-gate 			}
42677c478bd9Sstevel@tonic-gate 		}
42687c478bd9Sstevel@tonic-gate 	}
42697c478bd9Sstevel@tonic-gate 
42707c478bd9Sstevel@tonic-gate 	if ((G_p->g_mode & Ftype) == S_IFLNK && !(Args & OCL)) {
42717c478bd9Sstevel@tonic-gate 		/* The archive file is a symlink. */
42727c478bd9Sstevel@tonic-gate 
42737c478bd9Sstevel@tonic-gate 		errno = 0;
42747c478bd9Sstevel@tonic-gate 
42757c478bd9Sstevel@tonic-gate 		if ((size = readlink(save_name, Symlnk_p, MAXPATHLEN)) < 0) {
42767c478bd9Sstevel@tonic-gate 			msg(ERRN,
42777c478bd9Sstevel@tonic-gate 			    "Cannot read symbolic link \"%s\"", save_name);
42787c478bd9Sstevel@tonic-gate 			return (FILE_PASS_ERR);
42797c478bd9Sstevel@tonic-gate 		}
42807c478bd9Sstevel@tonic-gate 
42817c478bd9Sstevel@tonic-gate 		errno = 0;
42827c478bd9Sstevel@tonic-gate 		(void) missdir(Fullnam_p);
42837c478bd9Sstevel@tonic-gate 		*(Symlnk_p + size) = '\0';
42847c478bd9Sstevel@tonic-gate 
42857c478bd9Sstevel@tonic-gate 		if (symlink(Symlnk_p, Fullnam_p) < 0) {
42867c478bd9Sstevel@tonic-gate 			if (errno == EEXIST) {
42877c478bd9Sstevel@tonic-gate 				if (openout(G_p->g_passdirfd) < 0) {
4288647ab8f4Sceastha 					if (errno != EEXIST) {
4289647ab8f4Sceastha 						msg(ERRN,
4290647ab8f4Sceastha 						    "Cannot create \"%s\"",
4291647ab8f4Sceastha 						    Fullnam_p);
4292647ab8f4Sceastha 					}
42937c478bd9Sstevel@tonic-gate 					return (FILE_PASS_ERR);
42947c478bd9Sstevel@tonic-gate 				}
42957c478bd9Sstevel@tonic-gate 			} else {
42967c478bd9Sstevel@tonic-gate 				msg(ERRN, "Cannot create \"%s\"", Fullnam_p);
42977c478bd9Sstevel@tonic-gate 				return (FILE_PASS_ERR);
42987c478bd9Sstevel@tonic-gate 			}
4299647ab8f4Sceastha 		} else {
4300647ab8f4Sceastha 			if (Args & OCR) {
4301647ab8f4Sceastha 				if (lchown(Fullnam_p, (int)Rpw_p->pw_uid,
4302647ab8f4Sceastha 				    (int)Rpw_p->pw_gid) < 0) {
4303647ab8f4Sceastha 					msg(ERRN,
4304647ab8f4Sceastha 					    "Error during chown() of \"%s\"",
4305647ab8f4Sceastha 					    Fullnam_p);
43067c478bd9Sstevel@tonic-gate 				}
4307647ab8f4Sceastha 			} else if ((lchown(Fullnam_p, (int)G_p->g_uid,
4308647ab8f4Sceastha 			    (int)G_p->g_gid) < 0) && privileged) {
43097c478bd9Sstevel@tonic-gate 				msg(ERRN,
43107c478bd9Sstevel@tonic-gate 				    "Error during chown() of \"%s\"",
43117c478bd9Sstevel@tonic-gate 				    Fullnam_p);
43127c478bd9Sstevel@tonic-gate 			}
43137c478bd9Sstevel@tonic-gate 		}
43147c478bd9Sstevel@tonic-gate 
43157c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), Fullnam_p);
43167c478bd9Sstevel@tonic-gate 		return (FILE_PASS_ERR);
43177c478bd9Sstevel@tonic-gate 	}
43187c478bd9Sstevel@tonic-gate 
43197c478bd9Sstevel@tonic-gate 	if (!Adir && G_p->g_nlink > 1) {
43207c478bd9Sstevel@tonic-gate 		/* The archive file has hard links. */
43217c478bd9Sstevel@tonic-gate 
43227c478bd9Sstevel@tonic-gate 		tl_p = add_lnk(&ttl_p);
43237c478bd9Sstevel@tonic-gate 		l_p = ttl_p;
43247c478bd9Sstevel@tonic-gate 
43257c478bd9Sstevel@tonic-gate 		if (tl_p == l_p) {
43267c478bd9Sstevel@tonic-gate 			/* The archive file was not found. */
43277c478bd9Sstevel@tonic-gate 
43287c478bd9Sstevel@tonic-gate 			G_p = &tl_p->L_gen;
43297c478bd9Sstevel@tonic-gate 		} else {
43307c478bd9Sstevel@tonic-gate 			/* The archive file was found. */
43317c478bd9Sstevel@tonic-gate 
43327c478bd9Sstevel@tonic-gate 			cwd = -1;
43337c478bd9Sstevel@tonic-gate 
4334ced83f9bSceastha 			if (l_p->L_gen.g_attrnam_p != NULL) {
43357c478bd9Sstevel@tonic-gate 				/* We are linking an attribute */
43367c478bd9Sstevel@tonic-gate 
43377c478bd9Sstevel@tonic-gate 				(void) strcpy(Lnkend_p, l_p->L_gen.g_attrnam_p);
43387c478bd9Sstevel@tonic-gate 				cwd = save_cwd();
43397c478bd9Sstevel@tonic-gate 				(void) fchdir(G_p->g_passdirfd);
43407c478bd9Sstevel@tonic-gate 				lfrom = get_component(Lnknam_p);
43417c478bd9Sstevel@tonic-gate 				lto = tl_p->L_gen.g_attrnam_p;
43427c478bd9Sstevel@tonic-gate 			} else {
43437c478bd9Sstevel@tonic-gate 				/* We are not linking an attribute */
43447c478bd9Sstevel@tonic-gate 
43457c478bd9Sstevel@tonic-gate 				(void) strcpy(Lnkend_p, l_p->L_gen.g_nam_p);
43467c478bd9Sstevel@tonic-gate 				(void) strcpy(Full_p, tl_p->L_gen.g_nam_p);
43477c478bd9Sstevel@tonic-gate 				lfrom = Lnknam_p;
43487c478bd9Sstevel@tonic-gate 				lto = Fullnam_p;
43497c478bd9Sstevel@tonic-gate 			}
43507c478bd9Sstevel@tonic-gate 
43517c478bd9Sstevel@tonic-gate 			(void) creat_lnk(G_p->g_passdirfd, lfrom, lto);
43527c478bd9Sstevel@tonic-gate 
43537c478bd9Sstevel@tonic-gate 			if (cwd) {
43547c478bd9Sstevel@tonic-gate 				rest_cwd(cwd);
43557c478bd9Sstevel@tonic-gate 			}
43567c478bd9Sstevel@tonic-gate 
4357ced83f9bSceastha 			l_p->L_lnk_p = NULL;
43587c478bd9Sstevel@tonic-gate 			free(tl_p->L_gen.g_nam_p);
43597c478bd9Sstevel@tonic-gate 			free(tl_p);
43607c478bd9Sstevel@tonic-gate 
43617c478bd9Sstevel@tonic-gate 			if (l_p->L_cnt == G_p->g_nlink) {
43627c478bd9Sstevel@tonic-gate 				reclaim(l_p);
43637c478bd9Sstevel@tonic-gate 			}
43647c478bd9Sstevel@tonic-gate 
43657c478bd9Sstevel@tonic-gate 			return (FILE_LINKED);
43667c478bd9Sstevel@tonic-gate 		}
43677c478bd9Sstevel@tonic-gate 	}
43687c478bd9Sstevel@tonic-gate 
43697c478bd9Sstevel@tonic-gate 	if (Adir || Aspec) {
43707c478bd9Sstevel@tonic-gate 		/*
43717c478bd9Sstevel@tonic-gate 		 * The archive file is a directory,  block special, char
43727c478bd9Sstevel@tonic-gate 		 * special or a fifo.
43737c478bd9Sstevel@tonic-gate 		 */
43747c478bd9Sstevel@tonic-gate 
43757c478bd9Sstevel@tonic-gate 		if (creat_spec(G_p->g_passdirfd) > 0) {
43767c478bd9Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), Fullnam_p);
43777c478bd9Sstevel@tonic-gate 		}
43787c478bd9Sstevel@tonic-gate 	} else if ((Ofile = openout(G_p->g_passdirfd)) > 0) {
43797c478bd9Sstevel@tonic-gate 		data_pass();
43807c478bd9Sstevel@tonic-gate 	}
43817c478bd9Sstevel@tonic-gate 
43827c478bd9Sstevel@tonic-gate 	return (FILE_COPIED);
43837c478bd9Sstevel@tonic-gate }
43847c478bd9Sstevel@tonic-gate 
43857c478bd9Sstevel@tonic-gate /*
43867c478bd9Sstevel@tonic-gate  * flush_lnks: With new linked file handling, linked files are not archived
43877c478bd9Sstevel@tonic-gate  * until all links have been collected.  When the end of the list of filenames
43887c478bd9Sstevel@tonic-gate  * to archive has been reached, all files that did not encounter all their links
43897c478bd9Sstevel@tonic-gate  * are written out with actual (encountered) link counts.  A file with n links
43907c478bd9Sstevel@tonic-gate  * (that are archived) will be represented by n headers (one for each link (the
43917c478bd9Sstevel@tonic-gate  * first n - 1 have g_filesz set to 0)) followed by the data for the file.
43927c478bd9Sstevel@tonic-gate  */
43937c478bd9Sstevel@tonic-gate 
43947c478bd9Sstevel@tonic-gate static void
43957c478bd9Sstevel@tonic-gate flush_lnks(void)
43967c478bd9Sstevel@tonic-gate {
43977c478bd9Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
43987c478bd9Sstevel@tonic-gate 	off_t tfsize;
43997c478bd9Sstevel@tonic-gate 
44007c478bd9Sstevel@tonic-gate 	l_p = Lnk_hd.L_nxt_p;
44017c478bd9Sstevel@tonic-gate 	while (l_p != &Lnk_hd) {
44027c478bd9Sstevel@tonic-gate 		(void) strcpy(Gen.g_nam_p, l_p->L_gen.g_nam_p);
44037c478bd9Sstevel@tonic-gate 		if (stat(Gen.g_nam_p, &SrcSt) == 0) { /* check if file exists */
44047c478bd9Sstevel@tonic-gate 			tl_p = l_p;
44057c478bd9Sstevel@tonic-gate 			(void) creat_hdr();
44067c478bd9Sstevel@tonic-gate 			Gen.g_nlink = l_p->L_cnt; /* "actual" link count */
44077c478bd9Sstevel@tonic-gate 			tfsize = Gen.g_filesz;
44087c478bd9Sstevel@tonic-gate 			Gen.g_filesz = (off_t)0;
44097c478bd9Sstevel@tonic-gate 			G_p = &Gen;
4410ced83f9bSceastha 			while (tl_p != NULL) {
44117c478bd9Sstevel@tonic-gate 				Gen.g_nam_p = tl_p->L_gen.g_nam_p;
44127c478bd9Sstevel@tonic-gate 				Gen.g_namesz = tl_p->L_gen.g_namesz;
4413ced83f9bSceastha 				if (tl_p->L_lnk_p == NULL) {
44147c478bd9Sstevel@tonic-gate 					Gen.g_filesz = tfsize;
44157c478bd9Sstevel@tonic-gate 					if (open_dirfd() != 0) {
44167c478bd9Sstevel@tonic-gate 						break;
44177c478bd9Sstevel@tonic-gate 					}
44187c478bd9Sstevel@tonic-gate 					data_out();
44197c478bd9Sstevel@tonic-gate 					break;
44207c478bd9Sstevel@tonic-gate 				}
44217c478bd9Sstevel@tonic-gate 				write_hdr(ARCHIVE_NORMAL,
44227c478bd9Sstevel@tonic-gate 				    (off_t)0); /* header only */
44237c478bd9Sstevel@tonic-gate 				VERBOSE((Args & (OCv | OCV)), Gen.g_nam_p);
44247c478bd9Sstevel@tonic-gate 				tl_p = tl_p->L_lnk_p;
44257c478bd9Sstevel@tonic-gate 			}
44267c478bd9Sstevel@tonic-gate 			Gen.g_nam_p = Nam_p;
44277c478bd9Sstevel@tonic-gate 		} else /* stat(Gen.g_nam_p, &SrcSt) == 0 */
44287c478bd9Sstevel@tonic-gate 			msg(ERR, "\"%s%s%s\" has disappeared",
4429ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ?
44307c478bd9Sstevel@tonic-gate 			    Gen.g_nam_p : Gen.g_attrfnam_p,
4431ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ?
4432ced83f9bSceastha 			    "" : Gen.g_rw_sysattr ?
4433ced83f9bSceastha 			    gettext(" System Attribute ") :
4434ced83f9bSceastha 			    gettext(" Attribute "),
4435ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ?
44367c478bd9Sstevel@tonic-gate 			    "" : Gen.g_attrnam_p);
44377c478bd9Sstevel@tonic-gate 		tl_p = l_p;
44387c478bd9Sstevel@tonic-gate 		l_p = l_p->L_nxt_p;
44397c478bd9Sstevel@tonic-gate 		reclaim(tl_p);
44407c478bd9Sstevel@tonic-gate 	} /* l_p != &Lnk_hd */
44417c478bd9Sstevel@tonic-gate }
44427c478bd9Sstevel@tonic-gate 
4443da6c28aaSamw #if defined(O_XATTR)
4444da6c28aaSamw static int
4445da6c28aaSamw is_sysattr(char *name)
4446da6c28aaSamw {
4447da6c28aaSamw 	return ((strcmp(name, VIEW_READONLY) == 0) ||
4448da6c28aaSamw 	    (strcmp(name, VIEW_READWRITE) == 0));
4449da6c28aaSamw }
4450da6c28aaSamw #endif
4451da6c28aaSamw 
44527c478bd9Sstevel@tonic-gate /*
44537c478bd9Sstevel@tonic-gate  * gethdr: Get a header from the archive, validate it and check for the trailer.
44547c478bd9Sstevel@tonic-gate  * Any user specified Hdr_type is ignored (set to NONE in main).  Hdr_type is
44557c478bd9Sstevel@tonic-gate  * set appropriately after a valid header is found.  Unless the -k option is
44567c478bd9Sstevel@tonic-gate  * set a corrupted header causes an exit with an error.  I/O errors during
44577c478bd9Sstevel@tonic-gate  * examination of any part of the header cause gethdr to throw away any current
44587c478bd9Sstevel@tonic-gate  * data and start over.  Other errors during examination of any part of the
44597c478bd9Sstevel@tonic-gate  * header cause gethdr to advance one byte and continue the examination.
44607c478bd9Sstevel@tonic-gate  */
44617c478bd9Sstevel@tonic-gate 
44627c478bd9Sstevel@tonic-gate static int
44637c478bd9Sstevel@tonic-gate gethdr(void)
44647c478bd9Sstevel@tonic-gate {
44657c478bd9Sstevel@tonic-gate 	ushort_t ftype;
44667c478bd9Sstevel@tonic-gate 	int hit = NONE, cnt = 0;
44677c478bd9Sstevel@tonic-gate 	int goodhdr, hsize, offset;
44687c478bd9Sstevel@tonic-gate 	int bswap = 0;
44697c478bd9Sstevel@tonic-gate 	char *preptr;
44707c478bd9Sstevel@tonic-gate 	int k = 0;
44717c478bd9Sstevel@tonic-gate 	int j;
4472fa9e4066Sahrens 	int error;
4473fa9e4066Sahrens 	int aclcnt;
44747c478bd9Sstevel@tonic-gate 
44757c478bd9Sstevel@tonic-gate 	Gen.g_nam_p = Nam_p;
44767c478bd9Sstevel@tonic-gate 	do { /* hit == NONE && (Args & OCk) && Buffr.b_cnt > 0 */
44777c478bd9Sstevel@tonic-gate 		FILL(Hdrsz);
44787c478bd9Sstevel@tonic-gate 		switch (Hdr_type) {
44797c478bd9Sstevel@tonic-gate 		case NONE:
44807c478bd9Sstevel@tonic-gate 		case BIN:
44817c478bd9Sstevel@tonic-gate 			Binmag.b_byte[0] = Buffr.b_out_p[0];
44827c478bd9Sstevel@tonic-gate 			Binmag.b_byte[1] = Buffr.b_out_p[1];
44837c478bd9Sstevel@tonic-gate 			if ((Binmag.b_half == CMN_BIN) ||
44847c478bd9Sstevel@tonic-gate 			    (Binmag.b_half == CMN_BBS)) {
44857c478bd9Sstevel@tonic-gate 				hit = read_hdr(BIN);
44867c478bd9Sstevel@tonic-gate 				if (Hdr_type == NONE)
44877c478bd9Sstevel@tonic-gate 					bswap = 1;
44887c478bd9Sstevel@tonic-gate 				hsize = HDRSZ + Gen.g_namesz;
44897c478bd9Sstevel@tonic-gate 				break;
44907c478bd9Sstevel@tonic-gate 			}
44917c478bd9Sstevel@tonic-gate 			if (Hdr_type != NONE)
44927c478bd9Sstevel@tonic-gate 				break;
44937c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
44947c478bd9Sstevel@tonic-gate 		case CHR:
44957c478bd9Sstevel@tonic-gate 			if (!(strncmp(Buffr.b_out_p, CMS_CHR, CMS_LEN))) {
44967c478bd9Sstevel@tonic-gate 				hit = read_hdr(CHR);
44977c478bd9Sstevel@tonic-gate 				hsize = CHRSZ + Gen.g_namesz;
44987c478bd9Sstevel@tonic-gate 				break;
44997c478bd9Sstevel@tonic-gate 			}
45007c478bd9Sstevel@tonic-gate 			if (Hdr_type != NONE)
45017c478bd9Sstevel@tonic-gate 				break;
45027c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
45037c478bd9Sstevel@tonic-gate 		case ASC:
45047c478bd9Sstevel@tonic-gate 			if (!(strncmp(Buffr.b_out_p, CMS_ASC, CMS_LEN))) {
45057c478bd9Sstevel@tonic-gate 				hit = read_hdr(ASC);
45067c478bd9Sstevel@tonic-gate 				hsize = ASCSZ + Gen.g_namesz;
45077c478bd9Sstevel@tonic-gate 				Max_namesz = APATH;
45087c478bd9Sstevel@tonic-gate 				break;
45097c478bd9Sstevel@tonic-gate 			}
45107c478bd9Sstevel@tonic-gate 			if (Hdr_type != NONE)
45117c478bd9Sstevel@tonic-gate 				break;
45127c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
45137c478bd9Sstevel@tonic-gate 		case CRC:
45147c478bd9Sstevel@tonic-gate 			if (!(strncmp(Buffr.b_out_p, CMS_CRC, CMS_LEN))) {
45157c478bd9Sstevel@tonic-gate 				hit = read_hdr(CRC);
45167c478bd9Sstevel@tonic-gate 				hsize = ASCSZ + Gen.g_namesz;
45177c478bd9Sstevel@tonic-gate 				Max_namesz = APATH;
45187c478bd9Sstevel@tonic-gate 				break;
45197c478bd9Sstevel@tonic-gate 			}
45207c478bd9Sstevel@tonic-gate 			if (Hdr_type != NONE)
45217c478bd9Sstevel@tonic-gate 				break;
45227c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
45237c478bd9Sstevel@tonic-gate 
45247c478bd9Sstevel@tonic-gate 		case BAR:
45257c478bd9Sstevel@tonic-gate 			if (Hdr_p != NULL && strcmp(Hdr_p, "bar") == 0) {
45267c478bd9Sstevel@tonic-gate 				Hdrsz = BARSZ;
45277c478bd9Sstevel@tonic-gate 				FILL(Hdrsz);
45287c478bd9Sstevel@tonic-gate 				if ((hit = read_hdr(BAR)) == NONE) {
45297c478bd9Sstevel@tonic-gate 					Hdrsz = ASCSZ;
45307c478bd9Sstevel@tonic-gate 					break;
45317c478bd9Sstevel@tonic-gate 				}
45327c478bd9Sstevel@tonic-gate 				hit = BAR;
45337c478bd9Sstevel@tonic-gate 				hsize = BARSZ;
45347c478bd9Sstevel@tonic-gate 				break;
45357c478bd9Sstevel@tonic-gate 			}
45367c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
45377c478bd9Sstevel@tonic-gate 
45387c478bd9Sstevel@tonic-gate 		case USTAR:
45397c478bd9Sstevel@tonic-gate 			if (Hdr_p != NULL && strcmp(Hdr_p, "ustar") == 0) {
45407c478bd9Sstevel@tonic-gate 				Hdrsz = TARSZ;
45417c478bd9Sstevel@tonic-gate 				FILL(Hdrsz);
45427c478bd9Sstevel@tonic-gate 				if ((hit = read_hdr(USTAR)) == NONE) {
45437c478bd9Sstevel@tonic-gate 					Hdrsz = ASCSZ;
45447c478bd9Sstevel@tonic-gate 					break;
45457c478bd9Sstevel@tonic-gate 				}
45467c478bd9Sstevel@tonic-gate 				hit = USTAR;
45477c478bd9Sstevel@tonic-gate 				hsize = TARSZ;
45487c478bd9Sstevel@tonic-gate 				break;
45497c478bd9Sstevel@tonic-gate 			}
45507c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
45517c478bd9Sstevel@tonic-gate 		case TAR:
45527c478bd9Sstevel@tonic-gate 			if (Hdr_p != NULL && strcmp(Hdr_p, "tar") == 0) {
45537c478bd9Sstevel@tonic-gate 				Hdrsz = TARSZ;
45547c478bd9Sstevel@tonic-gate 				FILL(Hdrsz);
45557c478bd9Sstevel@tonic-gate 				if ((hit = read_hdr(TAR)) == NONE) {
45567c478bd9Sstevel@tonic-gate 					Hdrsz = ASCSZ;
45577c478bd9Sstevel@tonic-gate 					break;
45587c478bd9Sstevel@tonic-gate 				}
45597c478bd9Sstevel@tonic-gate 				hit = TAR;
45607c478bd9Sstevel@tonic-gate 				hsize = TARSZ;
45617c478bd9Sstevel@tonic-gate 				break;
45627c478bd9Sstevel@tonic-gate 			}
45637c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
45647c478bd9Sstevel@tonic-gate 		default:
45657c478bd9Sstevel@tonic-gate 			msg(EXT, "Impossible header type.");
45667c478bd9Sstevel@tonic-gate 		} /* Hdr_type */
45677c478bd9Sstevel@tonic-gate 
45687c478bd9Sstevel@tonic-gate 		if (hit == TAR || hit == USTAR) {
45697c478bd9Sstevel@tonic-gate 			Gen.g_nam_p = &nambuf[0];
45707c478bd9Sstevel@tonic-gate 		}
45717c478bd9Sstevel@tonic-gate 
45727c478bd9Sstevel@tonic-gate 		if (hit != NONE) {
45737c478bd9Sstevel@tonic-gate 			FILL(hsize);
45747c478bd9Sstevel@tonic-gate 			goodhdr = 1;
45757c478bd9Sstevel@tonic-gate 			if (Gen.g_filesz < (off_t)0 || Gen.g_namesz < 1)
45767c478bd9Sstevel@tonic-gate 				goodhdr = 0;
45777c478bd9Sstevel@tonic-gate 			if ((hit != USTAR) && (hit != TAR))
45787c478bd9Sstevel@tonic-gate 				if (Gen.g_namesz - 1 > Max_namesz)
45797c478bd9Sstevel@tonic-gate 					goodhdr = 0;
45807c478bd9Sstevel@tonic-gate 			/* TAR and USTAR */
45817c478bd9Sstevel@tonic-gate 			if ((hit == USTAR) || (hit == TAR)) {
45827c478bd9Sstevel@tonic-gate 				if (*Gen.g_nam_p == '\0') { /* tar trailer */
45837c478bd9Sstevel@tonic-gate 					goodhdr = 1;
45847c478bd9Sstevel@tonic-gate 				} else {
45857c478bd9Sstevel@tonic-gate 
45867c478bd9Sstevel@tonic-gate 					G_p = &Gen;
45877c478bd9Sstevel@tonic-gate 					if (G_p->g_cksum !=
45887c478bd9Sstevel@tonic-gate 					    cksum(TARTYP, 0, NULL)) {
45897c478bd9Sstevel@tonic-gate 						goodhdr = 0;
45907c478bd9Sstevel@tonic-gate 						msg(ERR,
45917c478bd9Sstevel@tonic-gate 						    "Bad header - checksum "
45927c478bd9Sstevel@tonic-gate 						    "error.");
45937c478bd9Sstevel@tonic-gate 					}
45947c478bd9Sstevel@tonic-gate 				}
45957c478bd9Sstevel@tonic-gate 			} else if (hit != BAR) { /* binary, -c, ASC and CRC */
45967c478bd9Sstevel@tonic-gate 				if (Gen.g_nlink <= (ulong_t)0)
45977c478bd9Sstevel@tonic-gate 					goodhdr = 0;
45987c478bd9Sstevel@tonic-gate 				if (*(Buffr.b_out_p + hsize - 1) != '\0')
45997c478bd9Sstevel@tonic-gate 					goodhdr = 0;
46007c478bd9Sstevel@tonic-gate 			}
46017c478bd9Sstevel@tonic-gate 			if (!goodhdr) {
46027c478bd9Sstevel@tonic-gate 				hit = NONE;
46037c478bd9Sstevel@tonic-gate 				if (!(Args & OCk))
46047c478bd9Sstevel@tonic-gate 					break;
46057c478bd9Sstevel@tonic-gate 				msg(ERR,
46067c478bd9Sstevel@tonic-gate 				    "Corrupt header, file(s) may be lost.");
46077c478bd9Sstevel@tonic-gate 			} else {
46087c478bd9Sstevel@tonic-gate 				FILL(hsize);
46097c478bd9Sstevel@tonic-gate 			}
46107c478bd9Sstevel@tonic-gate 		} /* hit != NONE */
46117c478bd9Sstevel@tonic-gate 		if (hit == NONE) {
46127c478bd9Sstevel@tonic-gate 			Buffr.b_out_p++;
46137c478bd9Sstevel@tonic-gate 			Buffr.b_cnt--;
46147c478bd9Sstevel@tonic-gate 			if (!(Args & OCk))
46157c478bd9Sstevel@tonic-gate 				break;
46167c478bd9Sstevel@tonic-gate 			if (!cnt++)
46177c478bd9Sstevel@tonic-gate 				msg(ERR, "Searching for magic number/header.");
46187c478bd9Sstevel@tonic-gate 		}
46197c478bd9Sstevel@tonic-gate 	} while (hit == NONE);
46207c478bd9Sstevel@tonic-gate 	if (hit == NONE) {
46217c478bd9Sstevel@tonic-gate 		if (Hdr_type == NONE)
46227c478bd9Sstevel@tonic-gate 			msg(EXT, "Not a cpio file, bad header.");
46237c478bd9Sstevel@tonic-gate 		else
46247c478bd9Sstevel@tonic-gate 			msg(EXT, "Bad magic number/header.");
46257c478bd9Sstevel@tonic-gate 	} else if (cnt > 0) {
46267c478bd9Sstevel@tonic-gate 		msg(EPOST, "Re-synchronized on magic number/header.");
46277c478bd9Sstevel@tonic-gate 	}
46287c478bd9Sstevel@tonic-gate 	if (Hdr_type == NONE) {
46297c478bd9Sstevel@tonic-gate 		Hdr_type = hit;
46307c478bd9Sstevel@tonic-gate 		switch (Hdr_type) {
46317c478bd9Sstevel@tonic-gate 		case BIN:
46327c478bd9Sstevel@tonic-gate 			if (bswap)
46337c478bd9Sstevel@tonic-gate 				Args |= BSM;
46347c478bd9Sstevel@tonic-gate 			Hdrsz = HDRSZ;
46357c478bd9Sstevel@tonic-gate 			Max_namesz = CPATH;
46367c478bd9Sstevel@tonic-gate 			Pad_val = HALFWD;
46377c478bd9Sstevel@tonic-gate 			Onecopy = 0;
46387c478bd9Sstevel@tonic-gate 			break;
46397c478bd9Sstevel@tonic-gate 		case CHR:
46407c478bd9Sstevel@tonic-gate 			Hdrsz = CHRSZ;
46417c478bd9Sstevel@tonic-gate 			Max_namesz = CPATH;
46427c478bd9Sstevel@tonic-gate 			Pad_val = 0;
46437c478bd9Sstevel@tonic-gate 			Onecopy = 0;
46447c478bd9Sstevel@tonic-gate 			break;
46457c478bd9Sstevel@tonic-gate 		case ASC:
46467c478bd9Sstevel@tonic-gate 		case CRC:
46477c478bd9Sstevel@tonic-gate 			Hdrsz = ASCSZ;
46487c478bd9Sstevel@tonic-gate 			Max_namesz = APATH;
46497c478bd9Sstevel@tonic-gate 			Pad_val = FULLWD;
46507c478bd9Sstevel@tonic-gate 			Onecopy = 1;
46517c478bd9Sstevel@tonic-gate 			break;
46527c478bd9Sstevel@tonic-gate 		case USTAR:
46537c478bd9Sstevel@tonic-gate 			Hdrsz = TARSZ;
46547c478bd9Sstevel@tonic-gate 			Max_namesz = HNAMLEN - 1;
46557c478bd9Sstevel@tonic-gate 			Pad_val = FULLBK;
46567c478bd9Sstevel@tonic-gate 			Onecopy = 0;
46577c478bd9Sstevel@tonic-gate 			break;
46587c478bd9Sstevel@tonic-gate 		case BAR:
46597c478bd9Sstevel@tonic-gate 		case TAR:
46607c478bd9Sstevel@tonic-gate 			Hdrsz = TARSZ;
46617c478bd9Sstevel@tonic-gate 			Max_namesz = TNAMLEN - 1;
46627c478bd9Sstevel@tonic-gate 			Pad_val = FULLBK;
46637c478bd9Sstevel@tonic-gate 			Onecopy = 0;
46647c478bd9Sstevel@tonic-gate 			break;
46657c478bd9Sstevel@tonic-gate 		default:
46667c478bd9Sstevel@tonic-gate 			msg(EXT, "Impossible header type.");
46677c478bd9Sstevel@tonic-gate 		} /* Hdr_type */
46687c478bd9Sstevel@tonic-gate 	} /* Hdr_type == NONE */
46697c478bd9Sstevel@tonic-gate 	if ((Hdr_type == USTAR) || (Hdr_type == TAR) ||
46707c478bd9Sstevel@tonic-gate 	    (Hdr_type == BAR)) {			/* TAR, USTAR, BAR */
46717c478bd9Sstevel@tonic-gate 		Gen.g_namesz = 0;
46727c478bd9Sstevel@tonic-gate 		if (Gen.g_nam_p[0] == '\0')
46737c478bd9Sstevel@tonic-gate 			return (0);
46747c478bd9Sstevel@tonic-gate 		else {
46757c478bd9Sstevel@tonic-gate 			preptr = &prebuf[0];
4676ced83f9bSceastha 			if (*preptr != NULL) {
46777c478bd9Sstevel@tonic-gate 				k = strlen(&prebuf[0]);
46787c478bd9Sstevel@tonic-gate 				if (k < PRESIZ) {
46797c478bd9Sstevel@tonic-gate 					(void) strcpy(&fullnam[0], &prebuf[0]);
46807c478bd9Sstevel@tonic-gate 					j = 0;
46817c478bd9Sstevel@tonic-gate 					fullnam[k++] = '/';
46827c478bd9Sstevel@tonic-gate 					while ((j < NAMSIZ) && (&nambuf[j] !=
4683ced83f9bSceastha 					    '\0')) {
46847c478bd9Sstevel@tonic-gate 						fullnam[k] = nambuf[j];
46857c478bd9Sstevel@tonic-gate 						k++; j++;
46867c478bd9Sstevel@tonic-gate 					}
46877c478bd9Sstevel@tonic-gate 					fullnam[k] = '\0';
46887c478bd9Sstevel@tonic-gate 				} else if (k >= PRESIZ) {
46897c478bd9Sstevel@tonic-gate 					k = 0;
46907c478bd9Sstevel@tonic-gate 					while ((k < PRESIZ) && (prebuf[k] !=
46917c478bd9Sstevel@tonic-gate 					    '\0')) {
46927c478bd9Sstevel@tonic-gate 						fullnam[k] = prebuf[k];
46937c478bd9Sstevel@tonic-gate 						k++;
46947c478bd9Sstevel@tonic-gate 					}
46957c478bd9Sstevel@tonic-gate 					fullnam[k++] = '/';
46967c478bd9Sstevel@tonic-gate 					j = 0;
46977c478bd9Sstevel@tonic-gate 					while ((j < NAMSIZ) && (nambuf[j] !=
46987c478bd9Sstevel@tonic-gate 					    '\0')) {
46997c478bd9Sstevel@tonic-gate 						fullnam[k] = nambuf[j];
47007c478bd9Sstevel@tonic-gate 						k++; j++;
47017c478bd9Sstevel@tonic-gate 					}
47027c478bd9Sstevel@tonic-gate 					fullnam[k] = '\0';
47037c478bd9Sstevel@tonic-gate 				}
47047c478bd9Sstevel@tonic-gate 				Gen.g_nam_p = &fullnam[0];
47057c478bd9Sstevel@tonic-gate 			} else
47067c478bd9Sstevel@tonic-gate 				Gen.g_nam_p = &nambuf[0];
47077c478bd9Sstevel@tonic-gate 
47087c478bd9Sstevel@tonic-gate 			/*
47097c478bd9Sstevel@tonic-gate 			 * initialize the buffer so that the prefix will not
47107c478bd9Sstevel@tonic-gate 			 * applied to the next entry in the archive
47117c478bd9Sstevel@tonic-gate 			 */
47127c478bd9Sstevel@tonic-gate 			(void) memset(prebuf, 0, sizeof (prebuf));
47137c478bd9Sstevel@tonic-gate 		}
47147c478bd9Sstevel@tonic-gate 	} else if (Hdr_type != BAR) {
47157c478bd9Sstevel@tonic-gate 		(void) memcpy(Gen.g_nam_p, Buffr.b_out_p + Hdrsz, Gen.g_namesz);
47167c478bd9Sstevel@tonic-gate 		if (!(strcmp(Gen.g_nam_p, "TRAILER!!!")))
47177c478bd9Sstevel@tonic-gate 			return (0);
47187c478bd9Sstevel@tonic-gate 	}
47197c478bd9Sstevel@tonic-gate 	offset = ((hsize + Pad_val) & ~Pad_val);
47207c478bd9Sstevel@tonic-gate 	FILL(offset + Hdrsz);
47217c478bd9Sstevel@tonic-gate 	Thdr_p = (union tblock *)Buffr.b_out_p;
47227c478bd9Sstevel@tonic-gate 	Buffr.b_out_p += offset;
47237c478bd9Sstevel@tonic-gate 	Buffr.b_cnt -= (off_t)offset;
47247c478bd9Sstevel@tonic-gate 	ftype = Gen.g_mode & Ftype;
47257c478bd9Sstevel@tonic-gate 
47267c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
47277c478bd9Sstevel@tonic-gate 	/* extended attribute support */
47287c478bd9Sstevel@tonic-gate 	if (((Gen.g_mode & S_IFMT) == _XATTR_CPIO_MODE) ||
47297c478bd9Sstevel@tonic-gate 	    ((Hdr_type == USTAR || Hdr_type == TAR) &&
47307c478bd9Sstevel@tonic-gate 	    Thdr_p->tbuf.t_typeflag == _XATTR_HDRTYPE)) {
4731ced83f9bSceastha 		char	*aname;
4732ced83f9bSceastha 		char	*attrparent = NULL;
4733ced83f9bSceastha 		char	*attrpath = NULL;
4734da6c28aaSamw 		char	*tapath;
4735ced83f9bSceastha 		char	*taname;
4736da6c28aaSamw 
4737ced83f9bSceastha 		if (xattrp != NULL) {
47387c478bd9Sstevel@tonic-gate 			if (xattrbadhead) {
47397c478bd9Sstevel@tonic-gate 				free(xattrhead);
47407c478bd9Sstevel@tonic-gate 				xattrp = NULL;
47417c478bd9Sstevel@tonic-gate 				xattr_linkp = NULL;
47427c478bd9Sstevel@tonic-gate 				xattrhead = NULL;
47437c478bd9Sstevel@tonic-gate 				return (1);
47447c478bd9Sstevel@tonic-gate 			}
4745da6c28aaSamw 
4746da6c28aaSamw 			/*
4747ced83f9bSceastha 			 * At this point, the attribute path contains
4748ced83f9bSceastha 			 * the path to the attribute rooted at the hidden
4749ced83f9bSceastha 			 * attribute directory of the base file.  This can
4750ced83f9bSceastha 			 * be a simple attribute or extended attribute name,
4751ced83f9bSceastha 			 * or it can be something like <attr>/<sys attr> if
4752ced83f9bSceastha 			 * we are processing a system attribute of an attribute.
4753ced83f9bSceastha 			 * Determine the attribute name and attribute parent
4754ced83f9bSceastha 			 * (if there is one).  When we are processing a simple
4755ced83f9bSceastha 			 * attribute or extended attribute name, the attribute
4756ced83f9bSceastha 			 * parent will be set to NULL.  When we are processing
4757ced83f9bSceastha 			 * something like <attr>/<sys attr>, the attribute
4758ced83f9bSceastha 			 * parent will be contain <attr>, and the attribute
4759ced83f9bSceastha 			 * name will contain <sys attr>.
4760da6c28aaSamw 			 */
4761ced83f9bSceastha 			tapath = xattrp->h_names +
4762ced83f9bSceastha 			    strlen(xattrp->h_names) + 1;
4763ced83f9bSceastha 			attrpath = e_strdup(E_EXIT, tapath);
4764ced83f9bSceastha 			if ((taname = strpbrk(tapath, "/")) != NULL) {
4765ced83f9bSceastha 				aname = taname + 1;
4766ced83f9bSceastha 				*taname = '\0';
4767ced83f9bSceastha 				attrparent = tapath;
4768ced83f9bSceastha 			} else {
4769ced83f9bSceastha 				aname = tapath;
47707c478bd9Sstevel@tonic-gate 			}
47717c478bd9Sstevel@tonic-gate 
4772ced83f9bSceastha 			Gen.g_rw_sysattr = is_sysattr(aname);
4773ced83f9bSceastha 			Gen.g_baseparent_fd = attr_baseparent_fd;
4774ced83f9bSceastha 
4775ced83f9bSceastha 			if (Gen.g_attrfnam_p != NULL) {
47767c478bd9Sstevel@tonic-gate 				free(Gen.g_attrfnam_p);
4777ced83f9bSceastha 				Gen.g_attrfnam_p = NULL;
47787c478bd9Sstevel@tonic-gate 			}
4779ced83f9bSceastha 			if (Gen.g_attrnam_p != NULL) {
47807c478bd9Sstevel@tonic-gate 				free(Gen.g_attrnam_p);
4781ced83f9bSceastha 				Gen.g_attrnam_p = NULL;
4782ced83f9bSceastha 			}
4783ced83f9bSceastha 			if (Gen.g_attrparent_p != NULL) {
4784ced83f9bSceastha 				free(Gen.g_attrparent_p);
4785ced83f9bSceastha 				Gen.g_attrparent_p = NULL;
4786ced83f9bSceastha 			}
4787ced83f9bSceastha 			if (Gen.g_attrpath_p != NULL) {
4788ced83f9bSceastha 				free(Gen.g_attrpath_p);
4789ced83f9bSceastha 				Gen.g_attrpath_p = NULL;
47907c478bd9Sstevel@tonic-gate 			}
47917c478bd9Sstevel@tonic-gate 			if (Renam_p && Renam_p[0] != '\0') {
47927c478bd9Sstevel@tonic-gate 				Gen.g_attrfnam_p = e_strdup(E_EXIT, Renam_p);
47937c478bd9Sstevel@tonic-gate 			} else {
47947c478bd9Sstevel@tonic-gate 				Gen.g_attrfnam_p = e_strdup(E_EXIT,
47957c478bd9Sstevel@tonic-gate 				    xattrp->h_names);
47967c478bd9Sstevel@tonic-gate 			}
4797ced83f9bSceastha 			Gen.g_attrnam_p = e_strdup(E_EXIT, aname);
47987c478bd9Sstevel@tonic-gate 
4799ced83f9bSceastha 			if (attrparent != NULL) {
4800ced83f9bSceastha 				if (Renam_attr_p && Renam_attr_p[0] != '\0') {
4801ced83f9bSceastha 					size_t	apathlen = strlen(attrparent) +
4802ced83f9bSceastha 					    strlen(aname) + 2;
4803ced83f9bSceastha 					Gen.g_attrparent_p = e_strdup(E_EXIT,
4804ced83f9bSceastha 					    Renam_attr_p);
4805ced83f9bSceastha 					Gen.g_attrpath_p = e_zalloc(E_EXIT,
4806ced83f9bSceastha 					    apathlen);
4807ced83f9bSceastha 					(void) snprintf(Gen.g_attrpath_p,
4808ced83f9bSceastha 					    apathlen, "%s/%s", Renam_attr_p,
4809ced83f9bSceastha 					    aname);
4810ced83f9bSceastha 					(void) free(attrparent);
4811ced83f9bSceastha 					(void) free(attrpath);
4812ced83f9bSceastha 				} else {
4813ced83f9bSceastha 					Gen.g_attrparent_p = attrparent;
4814ced83f9bSceastha 					Gen.g_attrpath_p = attrpath;
4815ced83f9bSceastha 				}
4816ced83f9bSceastha 			} else {
4817ced83f9bSceastha 				Gen.g_attrpath_p = attrpath;
4818ced83f9bSceastha 			}
48197c478bd9Sstevel@tonic-gate 
4820ced83f9bSceastha 			if (xattr_linkp != NULL) {
4821ced83f9bSceastha 				if (Gen.g_linktoattrfnam_p != NULL) {
4822ced83f9bSceastha 					free(Gen.g_linktoattrfnam_p);
4823ced83f9bSceastha 					Gen.g_linktoattrfnam_p = NULL;
4824ced83f9bSceastha 				}
4825ced83f9bSceastha 				if (Gen.g_linktoattrnam_p != NULL) {
4826ced83f9bSceastha 					free(Gen.g_linktoattrnam_p);
4827ced83f9bSceastha 					Gen.g_linktoattrnam_p = NULL;
4828ced83f9bSceastha 				}
4829ced83f9bSceastha 				if (Renam_attr_p && Renam_attr_p[0] != '\0') {
4830ced83f9bSceastha 					Gen.g_linktoattrfnam_p = e_strdup(
4831ced83f9bSceastha 					    E_EXIT, Renam_attr_p);
4832ced83f9bSceastha 				} else {
4833ced83f9bSceastha 					Gen.g_linktoattrfnam_p = e_strdup(
4834ced83f9bSceastha 					    E_EXIT, xattr_linkp->h_names);
4835ced83f9bSceastha 				}
4836ced83f9bSceastha 				Gen.g_linktoattrnam_p = e_strdup(E_EXIT,
4837ced83f9bSceastha 				    aname);
4838ced83f9bSceastha 				xattr_linkp = NULL;
4839ced83f9bSceastha 			}
48407c478bd9Sstevel@tonic-gate 			if (Hdr_type != USTAR && Hdr_type != TAR) {
48417c478bd9Sstevel@tonic-gate 				Gen.g_mode = Gen.g_mode & (~_XATTR_CPIO_MODE);
48427c478bd9Sstevel@tonic-gate 				Gen.g_mode |= attrmode(xattrp->h_typeflag);
48437c478bd9Sstevel@tonic-gate 			} else if (Hdr_type == USTAR || Hdr_type == TAR) {
48447c478bd9Sstevel@tonic-gate 				Thdr_p->tbuf.t_typeflag = xattrp->h_typeflag;
48457c478bd9Sstevel@tonic-gate 			}
4846ced83f9bSceastha 
48477c478bd9Sstevel@tonic-gate 			ftype = Gen.g_mode & Ftype;
48487c478bd9Sstevel@tonic-gate 			Adir = ftype == S_IFDIR;
4849cdd68f5aSceastha 			Aspec = (ftype == S_IFBLK || ftype == S_IFCHR ||
4850cdd68f5aSceastha 			    ftype == S_IFIFO || ftype == S_IFSOCK);
48517c478bd9Sstevel@tonic-gate 
48527c478bd9Sstevel@tonic-gate 			if (Gen.g_attrnam_p[0] == '.' &&
48537c478bd9Sstevel@tonic-gate 			    Gen.g_attrnam_p[1] == '\0' &&
48547c478bd9Sstevel@tonic-gate 			    xattrp->h_typeflag == DIRTYPE) {
48557c478bd9Sstevel@tonic-gate 				Hiddendir = 1;
48567c478bd9Sstevel@tonic-gate 			} else {
48577c478bd9Sstevel@tonic-gate 				Hiddendir = 0;
48587c478bd9Sstevel@tonic-gate 			}
48597c478bd9Sstevel@tonic-gate 
48607c478bd9Sstevel@tonic-gate 			free(xattrhead);
48617c478bd9Sstevel@tonic-gate 			xattrhead = NULL;
48627c478bd9Sstevel@tonic-gate 			xattrp = NULL;
48637c478bd9Sstevel@tonic-gate 		} else {
48647c478bd9Sstevel@tonic-gate 			if (xattrbadhead == 0) {
48657c478bd9Sstevel@tonic-gate 				(void) read_xattr_hdr();
48667c478bd9Sstevel@tonic-gate 				return (2);
48677c478bd9Sstevel@tonic-gate 			}
48687c478bd9Sstevel@tonic-gate 		}
4869ced83f9bSceastha 	} else {
4870ced83f9bSceastha 		Hiddendir = 0;
48717c478bd9Sstevel@tonic-gate 	}
48727c478bd9Sstevel@tonic-gate #endif /* O_XATTR */
48737c478bd9Sstevel@tonic-gate 
48747c478bd9Sstevel@tonic-gate 	/* acl support: grab acl info */
48757c478bd9Sstevel@tonic-gate 	if ((Gen.g_mode == SECMODE) || ((Hdr_type == USTAR ||
48767c478bd9Sstevel@tonic-gate 	    Hdr_type == TAR) && Thdr_p->tbuf.t_typeflag == 'A')) {
48777c478bd9Sstevel@tonic-gate 		/* this is an ancillary file */
48787c478bd9Sstevel@tonic-gate 		off_t	bytes;
48797c478bd9Sstevel@tonic-gate 		char	*secp;
48807c478bd9Sstevel@tonic-gate 		int	pad;
48817c478bd9Sstevel@tonic-gate 		int	cnt;
48827c478bd9Sstevel@tonic-gate 		char	*tp;
48837c478bd9Sstevel@tonic-gate 		int	attrsize;
48847c478bd9Sstevel@tonic-gate 
48857c478bd9Sstevel@tonic-gate 		if (Pflag) {
48867c478bd9Sstevel@tonic-gate 			bytes = Gen.g_filesz;
48877c478bd9Sstevel@tonic-gate 			secp = e_zalloc(E_EXIT, (uint_t)bytes);
48887c478bd9Sstevel@tonic-gate 			tp = secp;
48897c478bd9Sstevel@tonic-gate 
48907c478bd9Sstevel@tonic-gate 			while (bytes > 0) {
48917c478bd9Sstevel@tonic-gate 				cnt = (int)(bytes > CPIOBSZ) ? CPIOBSZ : bytes;
48927c478bd9Sstevel@tonic-gate 				FILL(cnt);
48937c478bd9Sstevel@tonic-gate 				(void) memcpy(tp, Buffr.b_out_p, cnt);
48947c478bd9Sstevel@tonic-gate 				tp += cnt;
48957c478bd9Sstevel@tonic-gate 				Buffr.b_out_p += cnt;
48967c478bd9Sstevel@tonic-gate 				Buffr.b_cnt -= (off_t)cnt;
48977c478bd9Sstevel@tonic-gate 				bytes -= (off_t)cnt;
48987c478bd9Sstevel@tonic-gate 			}
48997c478bd9Sstevel@tonic-gate 
49007c478bd9Sstevel@tonic-gate 			pad = (Pad_val + 1 - (Gen.g_filesz & Pad_val)) &
49017c478bd9Sstevel@tonic-gate 			    Pad_val;
49027c478bd9Sstevel@tonic-gate 			if (pad != 0) {
49037c478bd9Sstevel@tonic-gate 				FILL(pad);
49047c478bd9Sstevel@tonic-gate 				Buffr.b_out_p += pad;
49057c478bd9Sstevel@tonic-gate 				Buffr.b_cnt -= (off_t)pad;
49067c478bd9Sstevel@tonic-gate 			}
49077c478bd9Sstevel@tonic-gate 
49087c478bd9Sstevel@tonic-gate 			/* got all attributes in secp */
49097c478bd9Sstevel@tonic-gate 			tp = secp;
49107c478bd9Sstevel@tonic-gate 			do {
49117c478bd9Sstevel@tonic-gate 				attr = (struct sec_attr *)tp;
49127c478bd9Sstevel@tonic-gate 				switch (attr->attr_type) {
49137c478bd9Sstevel@tonic-gate 				case UFSD_ACL:
4914fa9e4066Sahrens 				case ACE_ACL:
49157c478bd9Sstevel@tonic-gate 					(void) sscanf(attr->attr_len, "%7lo",
49167c478bd9Sstevel@tonic-gate 					    (ulong_t *)&aclcnt);
49177c478bd9Sstevel@tonic-gate 					/* header is 8 */
49187c478bd9Sstevel@tonic-gate 					attrsize = 8 +
49197c478bd9Sstevel@tonic-gate 					    strlen(&attr->attr_info[0])
49207c478bd9Sstevel@tonic-gate 					    + 1;
4921fa9e4066Sahrens 
4922fa9e4066Sahrens 					error =
4923fa9e4066Sahrens 					    acl_fromtext(&attr->attr_info[0],
4924fa9e4066Sahrens 					    &aclp);
4925fa9e4066Sahrens 
4926fa9e4066Sahrens 					if (error != 0) {
4927fa9e4066Sahrens 						msg(ERR,
4928fa9e4066Sahrens 						    "aclfromtext failed: %s",
4929fa9e4066Sahrens 						    acl_strerror(error));
4930fa9e4066Sahrens 						bytes -= attrsize;
49317c478bd9Sstevel@tonic-gate 						break;
49327c478bd9Sstevel@tonic-gate 					}
4933fa9e4066Sahrens 
4934fa9e4066Sahrens 					if (aclcnt != acl_cnt(aclp)) {
49357c478bd9Sstevel@tonic-gate 						msg(ERR, "acl count error");
4936fa9e4066Sahrens 						bytes -= attrsize;
49377c478bd9Sstevel@tonic-gate 						break;
49387c478bd9Sstevel@tonic-gate 					}
49397c478bd9Sstevel@tonic-gate 					bytes -= attrsize;
49407c478bd9Sstevel@tonic-gate 					break;
49417c478bd9Sstevel@tonic-gate 
49427c478bd9Sstevel@tonic-gate 				/* SunFed case goes here */
49437c478bd9Sstevel@tonic-gate 
49447c478bd9Sstevel@tonic-gate 				default:
49457c478bd9Sstevel@tonic-gate 					msg(EXT, "unrecognized attr type");
49467c478bd9Sstevel@tonic-gate 					break;
49477c478bd9Sstevel@tonic-gate 			}
49487c478bd9Sstevel@tonic-gate 			/* next attributes */
49497c478bd9Sstevel@tonic-gate 			tp += attrsize;
49507c478bd9Sstevel@tonic-gate 			} while (bytes > 0);
49517c478bd9Sstevel@tonic-gate 			free(secp);
49527c478bd9Sstevel@tonic-gate 		} else {
49537c478bd9Sstevel@tonic-gate 			/* skip security info */
49547c478bd9Sstevel@tonic-gate 			G_p = &Gen;
49557c478bd9Sstevel@tonic-gate 			data_in(P_SKIP);
49567c478bd9Sstevel@tonic-gate 		}
49577c478bd9Sstevel@tonic-gate 		/*
49587c478bd9Sstevel@tonic-gate 		 * We already got the file content, dont call file_in()
49597c478bd9Sstevel@tonic-gate 		 * when return. The new return code(2) is used to
49607c478bd9Sstevel@tonic-gate 		 *  indicate that.
49617c478bd9Sstevel@tonic-gate 		 */
49627c478bd9Sstevel@tonic-gate 		VERBOSE((Args & OCt), Gen.g_nam_p);
49637c478bd9Sstevel@tonic-gate 		return (2);
49647c478bd9Sstevel@tonic-gate 	} /* acl */
49657c478bd9Sstevel@tonic-gate 
49665fbb8099SNobutomo Nakano 	/*
49675fbb8099SNobutomo Nakano 	 * Sparse file support
49685fbb8099SNobutomo Nakano 	 * Read header of holesdata to get original file size.
49695fbb8099SNobutomo Nakano 	 * This is necessary because ckname() or file_in() shows file size
49705fbb8099SNobutomo Nakano 	 * with OCt before data_in() extracts the holesdata. data_in()
49715fbb8099SNobutomo Nakano 	 * actually doesn't extract the holesdata since proc_mode will be
49725fbb8099SNobutomo Nakano 	 * P_SKIP in the OCt mode.
49735fbb8099SNobutomo Nakano 	 */
49745fbb8099SNobutomo Nakano 	if ((Hdr_type == CHR || Hdr_type == ASC) &&
49755fbb8099SNobutomo Nakano 	    S_ISSPARSE(Gen.g_mode) && Gen.g_filesz > MIN_HOLES_HDRSIZE) {
49765fbb8099SNobutomo Nakano 		char	holesdata[MIN_HOLES_HDRSIZE + 1];
49775fbb8099SNobutomo Nakano 
49785fbb8099SNobutomo Nakano 		FILL(MIN_HOLES_HDRSIZE);
49795fbb8099SNobutomo Nakano 		(void) memcpy(holesdata, Buffr.b_out_p, MIN_HOLES_HDRSIZE);
49805fbb8099SNobutomo Nakano 		holesdata[MIN_HOLES_HDRSIZE] = '\0';
49815fbb8099SNobutomo Nakano 
49825fbb8099SNobutomo Nakano 		Gen.g_holes = read_holes_header(holesdata, Gen.g_filesz);
49835fbb8099SNobutomo Nakano 		if (Gen.g_holes == NULL) {
49845fbb8099SNobutomo Nakano 			msg(EXT, "invalid sparse file information");
49855fbb8099SNobutomo Nakano 		} else {
49865fbb8099SNobutomo Nakano 			Buffr.b_out_p += MIN_HOLES_HDRSIZE;
49875fbb8099SNobutomo Nakano 			Buffr.b_cnt -= MIN_HOLES_HDRSIZE;
49885fbb8099SNobutomo Nakano 		}
49895fbb8099SNobutomo Nakano 	}
49905fbb8099SNobutomo Nakano 
49917c478bd9Sstevel@tonic-gate 	Adir = (ftype == S_IFDIR);
4992cdd68f5aSceastha 	Aspec = (ftype == S_IFBLK || ftype == S_IFCHR || ftype == S_IFIFO ||
4993cdd68f5aSceastha 	    ftype == S_IFSOCK);
49947c478bd9Sstevel@tonic-gate 
49957c478bd9Sstevel@tonic-gate 	/*
49967c478bd9Sstevel@tonic-gate 	 * Skip any trailing slashes
49977c478bd9Sstevel@tonic-gate 	 */
49987c478bd9Sstevel@tonic-gate 	chop_endslashes(Gen.g_nam_p);
49997c478bd9Sstevel@tonic-gate 	return (1);
50007c478bd9Sstevel@tonic-gate }
50017c478bd9Sstevel@tonic-gate 
50027c478bd9Sstevel@tonic-gate /*
50037c478bd9Sstevel@tonic-gate  * getname: Get file names for inclusion in the archive.  When end of file
50047c478bd9Sstevel@tonic-gate  * on the input stream of file names is reached, flush the link buffer out.
50057c478bd9Sstevel@tonic-gate  * For each filename, remove leading "./"s and multiple "/"s, and remove
5006da6c28aaSamw  * any trailing newline "\n".  Finally, verify the existence of the file,
50077c478bd9Sstevel@tonic-gate  * and call creat_hdr() to fill in the gen_hdr structure.
50087c478bd9Sstevel@tonic-gate  */
50097c478bd9Sstevel@tonic-gate 
50107c478bd9Sstevel@tonic-gate static int
50117c478bd9Sstevel@tonic-gate getname(void)
50127c478bd9Sstevel@tonic-gate {
50137c478bd9Sstevel@tonic-gate 	int goodfile = 0, lastchar, err;
50147c478bd9Sstevel@tonic-gate 	char *s;
50157c478bd9Sstevel@tonic-gate 	char *dir;
50167c478bd9Sstevel@tonic-gate 
50177c478bd9Sstevel@tonic-gate 	Gen.g_nam_p = Nam_p;
5018ced83f9bSceastha 	Hiddendir = 0;
50197c478bd9Sstevel@tonic-gate 
50207c478bd9Sstevel@tonic-gate 	while (!goodfile) {
50217c478bd9Sstevel@tonic-gate 		err = 0;
50227c478bd9Sstevel@tonic-gate 
5023ced83f9bSceastha 		while ((s = fgets(Gen.g_nam_p, APATH+1, In_p)) != NULL) {
50247c478bd9Sstevel@tonic-gate 			lastchar = strlen(s) - 1;
50257c478bd9Sstevel@tonic-gate 			issymlink = 0;
50267c478bd9Sstevel@tonic-gate 
50277c478bd9Sstevel@tonic-gate 			if (s[lastchar] != '\n') {
50287c478bd9Sstevel@tonic-gate 				if (lastchar == APATH - 1) {
50297c478bd9Sstevel@tonic-gate 					if (!err) {
50307c478bd9Sstevel@tonic-gate 						msg(ERR,
50317c478bd9Sstevel@tonic-gate 						    "%s name too long.",
50327c478bd9Sstevel@tonic-gate 						    Nam_p);
50337c478bd9Sstevel@tonic-gate 					}
50347c478bd9Sstevel@tonic-gate 					goodfile = 0;
50357c478bd9Sstevel@tonic-gate 					err = 1;
50367c478bd9Sstevel@tonic-gate 				} else {
50377c478bd9Sstevel@tonic-gate 					break;
50387c478bd9Sstevel@tonic-gate 				}
50397c478bd9Sstevel@tonic-gate 			} else {
50407c478bd9Sstevel@tonic-gate 				s[lastchar] = '\0';
50417c478bd9Sstevel@tonic-gate 				break;
50427c478bd9Sstevel@tonic-gate 			}
50437c478bd9Sstevel@tonic-gate 		}
50447c478bd9Sstevel@tonic-gate 
5045ced83f9bSceastha 		if (s == NULL) {
50467c478bd9Sstevel@tonic-gate 			if (Gen.g_dirfd != -1) {
50477c478bd9Sstevel@tonic-gate 				(void) close(Gen.g_dirfd);
50487c478bd9Sstevel@tonic-gate 				Gen.g_dirfd = -1;
50497c478bd9Sstevel@tonic-gate 			}
50507c478bd9Sstevel@tonic-gate 			if (Onecopy && (Args & OCo)) {
50517c478bd9Sstevel@tonic-gate 				flush_lnks();
50527c478bd9Sstevel@tonic-gate 			}
50537c478bd9Sstevel@tonic-gate 			return (0);
50547c478bd9Sstevel@tonic-gate 		}
50557c478bd9Sstevel@tonic-gate 
50567c478bd9Sstevel@tonic-gate 		while (*Gen.g_nam_p == '.' && Gen.g_nam_p[1] == '/') {
50577c478bd9Sstevel@tonic-gate 			Gen.g_nam_p += 2;
50587c478bd9Sstevel@tonic-gate 			while (*Gen.g_nam_p == '/')
50597c478bd9Sstevel@tonic-gate 				Gen.g_nam_p++;
50607c478bd9Sstevel@tonic-gate 		}
50617c478bd9Sstevel@tonic-gate 
50627c478bd9Sstevel@tonic-gate 		/*
50637c478bd9Sstevel@tonic-gate 		 * Skip any trailing slashes
50647c478bd9Sstevel@tonic-gate 		 */
50657c478bd9Sstevel@tonic-gate 		chop_endslashes(Gen.g_nam_p);
50667c478bd9Sstevel@tonic-gate 
50677c478bd9Sstevel@tonic-gate 		/*
50687c478bd9Sstevel@tonic-gate 		 * Figure out parent directory
50697c478bd9Sstevel@tonic-gate 		 */
50707c478bd9Sstevel@tonic-gate 
5071ced83f9bSceastha 		if (Gen.g_attrnam_p != NULL) {
50727c478bd9Sstevel@tonic-gate 			if (Gen.g_dirfd != -1) {
50737c478bd9Sstevel@tonic-gate 				(void) close(Gen.g_dirfd);
50747c478bd9Sstevel@tonic-gate 			}
50757c478bd9Sstevel@tonic-gate 			Gen.g_dirfd = attropen(Gen.g_attrfnam_p, ".", O_RDONLY);
50767c478bd9Sstevel@tonic-gate 			if (Gen.g_dirfd == -1) {
50777c478bd9Sstevel@tonic-gate 				msg(ERRN,
50787c478bd9Sstevel@tonic-gate 				    "Cannot open attribute directory"
50797c478bd9Sstevel@tonic-gate 				    " of file %s", Gen.g_attrfnam_p);
50807c478bd9Sstevel@tonic-gate 				continue;
50817c478bd9Sstevel@tonic-gate 			}
50827c478bd9Sstevel@tonic-gate 		} else {
50837c478bd9Sstevel@tonic-gate #ifdef O_XATTR
50847c478bd9Sstevel@tonic-gate 			char dirpath[PATH_MAX];
50857c478bd9Sstevel@tonic-gate 
50867c478bd9Sstevel@tonic-gate 			get_parent(Gen.g_nam_p, dirpath);
5087ced83f9bSceastha 			if (Atflag || SysAtflag) {
50887c478bd9Sstevel@tonic-gate 				dir = dirpath;
50897c478bd9Sstevel@tonic-gate 				if (Gen.g_dirfd != -1) {
50907c478bd9Sstevel@tonic-gate 					(void) close(Gen.g_dirfd);
50917c478bd9Sstevel@tonic-gate 				}
50927c478bd9Sstevel@tonic-gate 				Gen.g_dirfd = open(dir, O_RDONLY);
50937c478bd9Sstevel@tonic-gate 				if (Gen.g_dirfd == -1) {
50947c478bd9Sstevel@tonic-gate 					msg(ERRN,
50957c478bd9Sstevel@tonic-gate 					    "Cannot open directory %s", dir);
50967c478bd9Sstevel@tonic-gate 					continue;
50977c478bd9Sstevel@tonic-gate 				}
50987c478bd9Sstevel@tonic-gate 			} else {
50997c478bd9Sstevel@tonic-gate 				/*
51007c478bd9Sstevel@tonic-gate 				 * g_dirpath is the pathname cache maintaining
51017c478bd9Sstevel@tonic-gate 				 * the dirname which is currently opened.
51027c478bd9Sstevel@tonic-gate 				 * We first check the g_dirpath to see if the
51037c478bd9Sstevel@tonic-gate 				 * given dirname matches. If so, we don't need
51047c478bd9Sstevel@tonic-gate 				 * to open the dir, but we can use the g_dirfd
51057c478bd9Sstevel@tonic-gate 				 * as is if it is still available.
51067c478bd9Sstevel@tonic-gate 				 */
51077c478bd9Sstevel@tonic-gate 				dir = NULL;
51087c478bd9Sstevel@tonic-gate 				if (Gen.g_dirpath == NULL ||
51097c478bd9Sstevel@tonic-gate 				    Gen.g_dirfd == -1) {
51107c478bd9Sstevel@tonic-gate 					/*
51117c478bd9Sstevel@tonic-gate 					 * It's the first time or it has
51127c478bd9Sstevel@tonic-gate 					 * all gone.
51137c478bd9Sstevel@tonic-gate 					 */
51147c478bd9Sstevel@tonic-gate 					dir = e_strdup(E_EXIT, dirpath);
51157c478bd9Sstevel@tonic-gate 				} else {
51167c478bd9Sstevel@tonic-gate 					if (strcmp(Gen.g_dirpath,
51177c478bd9Sstevel@tonic-gate 					    dirpath) != 0) {
51187c478bd9Sstevel@tonic-gate 						/* different directory */
51197c478bd9Sstevel@tonic-gate 						dir = e_strdup(E_EXIT, dirpath);
51207c478bd9Sstevel@tonic-gate 					}
51217c478bd9Sstevel@tonic-gate 				}
51227c478bd9Sstevel@tonic-gate 				if (dir != NULL) {
51237c478bd9Sstevel@tonic-gate 					/*
51247c478bd9Sstevel@tonic-gate 					 * We need to open the new directory.
51257c478bd9Sstevel@tonic-gate 					 * discard the pathname and dirfd
51267c478bd9Sstevel@tonic-gate 					 * for the previous directory.
51277c478bd9Sstevel@tonic-gate 					 */
51287c478bd9Sstevel@tonic-gate 					if (Gen.g_dirpath != NULL) {
51297c478bd9Sstevel@tonic-gate 						free(Gen.g_dirpath);
51307c478bd9Sstevel@tonic-gate 						Gen.g_dirpath = NULL;
51317c478bd9Sstevel@tonic-gate 					}
51327c478bd9Sstevel@tonic-gate 					if (Gen.g_dirfd != -1) {
51337c478bd9Sstevel@tonic-gate 						(void) close(Gen.g_dirfd);
51347c478bd9Sstevel@tonic-gate 					}
51357c478bd9Sstevel@tonic-gate 					/* open the new dir */
51367c478bd9Sstevel@tonic-gate 					Gen.g_dirfd = open(dir, O_RDONLY);
51377c478bd9Sstevel@tonic-gate 					if (Gen.g_dirfd == -1) {
51387c478bd9Sstevel@tonic-gate 						msg(ERRN, "Cannot open "
51397c478bd9Sstevel@tonic-gate 						    "directory %s", dir);
51407c478bd9Sstevel@tonic-gate 						continue;
51417c478bd9Sstevel@tonic-gate 					}
51427c478bd9Sstevel@tonic-gate 					Gen.g_dirpath = dir;
51437c478bd9Sstevel@tonic-gate 				}
51447c478bd9Sstevel@tonic-gate 			}
51457c478bd9Sstevel@tonic-gate #else
51467c478bd9Sstevel@tonic-gate 			Gen.g_dirfd = -1;
51477c478bd9Sstevel@tonic-gate #endif
51487c478bd9Sstevel@tonic-gate 		}
51497c478bd9Sstevel@tonic-gate 
51507c478bd9Sstevel@tonic-gate 		/* creat_hdr checks for USTAR filename length */
51517c478bd9Sstevel@tonic-gate 
51527c478bd9Sstevel@tonic-gate 		if (Hdr_type != USTAR && strlen(Gen.g_nam_p) >
51537c478bd9Sstevel@tonic-gate 		    Max_namesz) {
51547c478bd9Sstevel@tonic-gate 			if (!err) {
51557c478bd9Sstevel@tonic-gate 				msg(ERR, "%s%s%s name too long.",
5156ced83f9bSceastha 				    (Gen.g_attrnam_p == NULL) ?
51577c478bd9Sstevel@tonic-gate 				    Nam_p : Gen.g_attrfnam_p,
5158ced83f9bSceastha 				    (Gen.g_attrnam_p == NULL) ?
5159ced83f9bSceastha 				    "" : Gen.g_rw_sysattr ?
5160ced83f9bSceastha 				    gettext(" System Attribute ") :
5161ced83f9bSceastha 				    gettext(" Attribute "),
5162ced83f9bSceastha 				    (Gen.g_attrnam_p == NULL) ?
51637c478bd9Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
51647c478bd9Sstevel@tonic-gate 			}
51657c478bd9Sstevel@tonic-gate 			goodfile = 0;
51667c478bd9Sstevel@tonic-gate 			err = 1;
51677c478bd9Sstevel@tonic-gate 		}
51687c478bd9Sstevel@tonic-gate 
51697c478bd9Sstevel@tonic-gate 		if (err) {
51707c478bd9Sstevel@tonic-gate 			continue;
51717c478bd9Sstevel@tonic-gate 		} else {
51727c478bd9Sstevel@tonic-gate 			G_p = &Gen;
51737c478bd9Sstevel@tonic-gate 			if (!LSTAT(Gen.g_dirfd, Gen.g_nam_p, &SrcSt)) {
51747c478bd9Sstevel@tonic-gate 				goodfile = 1;
51757c478bd9Sstevel@tonic-gate 
51767c478bd9Sstevel@tonic-gate 				if ((SrcSt.st_mode & Ftype) == S_IFLNK) {
51777c478bd9Sstevel@tonic-gate 					issymlink = 1;
51787c478bd9Sstevel@tonic-gate 
51797c478bd9Sstevel@tonic-gate 					if ((Args & OCL)) {
51807c478bd9Sstevel@tonic-gate 						errno = 0;
51817c478bd9Sstevel@tonic-gate 						if (STAT(Gen.g_dirfd,
51827c478bd9Sstevel@tonic-gate 						    G_p->g_nam_p,
51837c478bd9Sstevel@tonic-gate 						    &SrcSt) < 0) {
51847c478bd9Sstevel@tonic-gate 							msg(ERRN,
51857c478bd9Sstevel@tonic-gate 							    "Cannot follow"
51867c478bd9Sstevel@tonic-gate 							    " \"%s%s%s\"",
51877c478bd9Sstevel@tonic-gate 							    (Gen.g_attrnam_p ==
5188ced83f9bSceastha 							    NULL) ?
51897c478bd9Sstevel@tonic-gate 							    Gen.g_nam_p :
51907c478bd9Sstevel@tonic-gate 							    Gen.g_attrfnam_p,
51917c478bd9Sstevel@tonic-gate 							    (Gen.g_attrnam_p ==
5192ced83f9bSceastha 							    NULL) ? "" :
5193ced83f9bSceastha 							    Gen.g_rw_sysattr ?
5194ced83f9bSceastha 							    gettext(
5195ced83f9bSceastha 							    " System "
5196ced83f9bSceastha 							    "Attribute ") :
51977c478bd9Sstevel@tonic-gate 							    gettext(
51987c478bd9Sstevel@tonic-gate 							    " Attribute "),
51997c478bd9Sstevel@tonic-gate 							    (Gen.g_attrnam_p ==
5200ced83f9bSceastha 							    NULL) ? "" :
52017c478bd9Sstevel@tonic-gate 							    Gen.g_attrnam_p);
52027c478bd9Sstevel@tonic-gate 							goodfile = 0;
52037c478bd9Sstevel@tonic-gate 						}
52047c478bd9Sstevel@tonic-gate 					}
52057c478bd9Sstevel@tonic-gate 				}
52067c478bd9Sstevel@tonic-gate 
52077c478bd9Sstevel@tonic-gate 				if (Use_old_stat) {
52087c478bd9Sstevel@tonic-gate 					OldSt = convert_to_old_stat(&SrcSt,
52097c478bd9Sstevel@tonic-gate 					    Gen.g_nam_p, Gen.g_attrnam_p);
52107c478bd9Sstevel@tonic-gate 
52117c478bd9Sstevel@tonic-gate 					if (OldSt == NULL) {
52127c478bd9Sstevel@tonic-gate 						goodfile = 0;
52137c478bd9Sstevel@tonic-gate 					}
52147c478bd9Sstevel@tonic-gate 				}
52157c478bd9Sstevel@tonic-gate 			} else {
52167c478bd9Sstevel@tonic-gate 				msg(ERRN,
52177c478bd9Sstevel@tonic-gate 				    "Error with fstatat() of \"%s%s%s\"",
5218ced83f9bSceastha 				    (Gen.g_attrnam_p == NULL) ?
52197c478bd9Sstevel@tonic-gate 				    Gen.g_nam_p : Gen.g_attrfnam_p,
5220ced83f9bSceastha 				    (Gen.g_attrnam_p == NULL) ? "" :
5221ced83f9bSceastha 				    Gen.g_rw_sysattr ?
5222ced83f9bSceastha 				    gettext(" System Attribute ") :
5223ced83f9bSceastha 				    gettext(" Attribute "),
5224ced83f9bSceastha 				    (Gen.g_attrnam_p == NULL) ?
52257c478bd9Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
52267c478bd9Sstevel@tonic-gate 			}
52277c478bd9Sstevel@tonic-gate 		}
52287c478bd9Sstevel@tonic-gate 	}
52297c478bd9Sstevel@tonic-gate 
52307c478bd9Sstevel@tonic-gate 	/*
52317c478bd9Sstevel@tonic-gate 	 * Get ACL info: dont bother allocating space if there are only
52327c478bd9Sstevel@tonic-gate 	 * standard permissions, i.e. ACL count < 4
52337c478bd9Sstevel@tonic-gate 	 */
52347c478bd9Sstevel@tonic-gate 	if ((SrcSt.st_mode & Ftype) != S_IFLNK && Pflag) {
5235fa9e4066Sahrens 		if (acl_get(Gen.g_nam_p, ACL_NO_TRIVIAL, &aclp) != 0)
52367c478bd9Sstevel@tonic-gate 			msg(ERRN, "Error with acl() of \"%s\"", Gen.g_nam_p);
52377c478bd9Sstevel@tonic-gate 	}
52387c478bd9Sstevel@tonic-gate 	/* else: only traditional permissions, so proceed as usual */
52397c478bd9Sstevel@tonic-gate 	if (creat_hdr())
52407c478bd9Sstevel@tonic-gate 		return (1);
52417c478bd9Sstevel@tonic-gate 	else return (2);
52427c478bd9Sstevel@tonic-gate }
52437c478bd9Sstevel@tonic-gate 
52447c478bd9Sstevel@tonic-gate /*
52457c478bd9Sstevel@tonic-gate  * getpats: Save any filenames/patterns specified as arguments.
52467c478bd9Sstevel@tonic-gate  * Read additional filenames/patterns from the file specified by the
52477c478bd9Sstevel@tonic-gate  * user.  The filenames/patterns must occur one per line.
52487c478bd9Sstevel@tonic-gate  */
52497c478bd9Sstevel@tonic-gate 
52507c478bd9Sstevel@tonic-gate static void
52517c478bd9Sstevel@tonic-gate getpats(int largc, char **largv)
52527c478bd9Sstevel@tonic-gate {
52537c478bd9Sstevel@tonic-gate 	char **t_pp;
52547c478bd9Sstevel@tonic-gate 	size_t len;
52557c478bd9Sstevel@tonic-gate 	unsigned numpat = largc, maxpat = largc + 2;
52567c478bd9Sstevel@tonic-gate 
52577c478bd9Sstevel@tonic-gate 	Pat_pp = e_zalloc(E_EXIT, maxpat * sizeof (char *));
52587c478bd9Sstevel@tonic-gate 	t_pp = Pat_pp;
52597c478bd9Sstevel@tonic-gate 	while (*largv) {
52607c478bd9Sstevel@tonic-gate 		*t_pp = e_zalloc(E_EXIT, strlen(*largv) + 1);
52617c478bd9Sstevel@tonic-gate 		(void) strcpy(*t_pp, *largv);
52627c478bd9Sstevel@tonic-gate 		t_pp++;
52637c478bd9Sstevel@tonic-gate 		largv++;
52647c478bd9Sstevel@tonic-gate 	}
5265ced83f9bSceastha 	while (fgets(Nam_p, Max_namesz + 1, Ef_p) != NULL) {
52667c478bd9Sstevel@tonic-gate 		if (numpat == maxpat - 1) {
52677c478bd9Sstevel@tonic-gate 			maxpat += 10;
52687c478bd9Sstevel@tonic-gate 			Pat_pp = e_realloc(E_EXIT, Pat_pp,
52697c478bd9Sstevel@tonic-gate 			    maxpat * sizeof (char *));
52707c478bd9Sstevel@tonic-gate 			t_pp = Pat_pp + numpat;
52717c478bd9Sstevel@tonic-gate 		}
52727c478bd9Sstevel@tonic-gate 		len = strlen(Nam_p); /* includes the \n */
52737c478bd9Sstevel@tonic-gate 		*(Nam_p + len - 1) = '\0'; /* remove the \n */
52747c478bd9Sstevel@tonic-gate 		*t_pp = e_zalloc(E_EXIT, len);
52757c478bd9Sstevel@tonic-gate 		(void) strcpy(*t_pp, Nam_p);
52767c478bd9Sstevel@tonic-gate 		t_pp++;
52777c478bd9Sstevel@tonic-gate 		numpat++;
52787c478bd9Sstevel@tonic-gate 	}
5279ced83f9bSceastha 	*t_pp = NULL;
52807c478bd9Sstevel@tonic-gate }
52817c478bd9Sstevel@tonic-gate 
52827c478bd9Sstevel@tonic-gate static void
52837c478bd9Sstevel@tonic-gate ioerror(int dir)
52847c478bd9Sstevel@tonic-gate {
52857c478bd9Sstevel@tonic-gate 	int t_errno;
52867c478bd9Sstevel@tonic-gate 
52877c478bd9Sstevel@tonic-gate 	t_errno = errno;
52887c478bd9Sstevel@tonic-gate 	errno = 0;
52897c478bd9Sstevel@tonic-gate 	if (fstat(Archive, &ArchSt) < 0)
52907c478bd9Sstevel@tonic-gate 		msg(EXTN, "Error during stat() of archive");
52917c478bd9Sstevel@tonic-gate 	errno = t_errno;
52927c478bd9Sstevel@tonic-gate 	if ((ArchSt.st_mode & Ftype) != S_IFCHR) {
52937c478bd9Sstevel@tonic-gate 		if (dir) {
52947c478bd9Sstevel@tonic-gate 			if (errno == EFBIG)
52957c478bd9Sstevel@tonic-gate 				msg(EXT, "ulimit reached for output file.");
52967c478bd9Sstevel@tonic-gate 			else if (errno == ENOSPC)
52977c478bd9Sstevel@tonic-gate 				msg(EXT, "No space left for output file.");
52987c478bd9Sstevel@tonic-gate 			else
52997c478bd9Sstevel@tonic-gate 				msg(EXTN, "I/O error - cannot continue");
53007c478bd9Sstevel@tonic-gate 		} else
53017c478bd9Sstevel@tonic-gate 			msg(EXT, "Unexpected end-of-file encountered.");
53027c478bd9Sstevel@tonic-gate 	} else
53037c478bd9Sstevel@tonic-gate 		msg(EXTN, "\007I/O error on \"%s\"", dir ? "output" : "input");
53047c478bd9Sstevel@tonic-gate }
53057c478bd9Sstevel@tonic-gate 
53067c478bd9Sstevel@tonic-gate /*
53077c478bd9Sstevel@tonic-gate  * matched: Determine if a filename matches the specified pattern(s).  If the
53087c478bd9Sstevel@tonic-gate  * pattern is matched (the second return), return 0 if -f was specified, else
53097c478bd9Sstevel@tonic-gate  * return != 0.  If the pattern is not matched (the first and third
53107c478bd9Sstevel@tonic-gate  * returns), return 0 if -f was not specified, else return != 0.
53117c478bd9Sstevel@tonic-gate  */
53127c478bd9Sstevel@tonic-gate 
53137c478bd9Sstevel@tonic-gate static int
53147c478bd9Sstevel@tonic-gate matched(void)
53157c478bd9Sstevel@tonic-gate {
53167c478bd9Sstevel@tonic-gate 	char *str_p = G_p->g_nam_p;
53177c478bd9Sstevel@tonic-gate 	char **pat_pp = Pat_pp;
53187c478bd9Sstevel@tonic-gate 	int negatep, result;
53197c478bd9Sstevel@tonic-gate 
53207c478bd9Sstevel@tonic-gate 	/*
53217c478bd9Sstevel@tonic-gate 	 * Check for attribute
53227c478bd9Sstevel@tonic-gate 	 */
5323ced83f9bSceastha 	if (G_p->g_attrfnam_p != NULL)
53247c478bd9Sstevel@tonic-gate 		str_p = G_p->g_attrfnam_p;
53257c478bd9Sstevel@tonic-gate 
53267c478bd9Sstevel@tonic-gate 	for (pat_pp = Pat_pp; *pat_pp; pat_pp++) {
53277c478bd9Sstevel@tonic-gate 		negatep = (**pat_pp == '!');
53287c478bd9Sstevel@tonic-gate 
53297c478bd9Sstevel@tonic-gate 		result = fnmatch(negatep ? (*pat_pp+1) : *pat_pp, str_p, 0);
53307c478bd9Sstevel@tonic-gate 
53317c478bd9Sstevel@tonic-gate 		if (result != 0 && result != FNM_NOMATCH) {
53327c478bd9Sstevel@tonic-gate 			msg(POST, "error matching file %s with pattern"
53337c478bd9Sstevel@tonic-gate 			    " %s\n", str_p, *pat_pp);
53347c478bd9Sstevel@tonic-gate 			return (Args & OCf);
53357c478bd9Sstevel@tonic-gate 		}
53367c478bd9Sstevel@tonic-gate 
53377c478bd9Sstevel@tonic-gate 		if ((result == 0 && ! negatep) ||
53387c478bd9Sstevel@tonic-gate 		    (result == FNM_NOMATCH && negatep)) {
5339da6c28aaSamw 			/* match occurred */
53407c478bd9Sstevel@tonic-gate 			return (!(Args & OCf));
53417c478bd9Sstevel@tonic-gate 		}
53427c478bd9Sstevel@tonic-gate 	}
53437c478bd9Sstevel@tonic-gate 	return (Args & OCf); /* not matched */
53447c478bd9Sstevel@tonic-gate }
53457c478bd9Sstevel@tonic-gate 
53467c478bd9Sstevel@tonic-gate /*
53477c478bd9Sstevel@tonic-gate  * missdir: Create missing directories for files.
53487c478bd9Sstevel@tonic-gate  * (Possible future performance enhancement, if missdir is called, we know
53497c478bd9Sstevel@tonic-gate  * that at least the very last directory of the path does not exist, therefore,
53507c478bd9Sstevel@tonic-gate  * scan the path from the end
53517c478bd9Sstevel@tonic-gate  */
53527c478bd9Sstevel@tonic-gate 
53537c478bd9Sstevel@tonic-gate static int
53547c478bd9Sstevel@tonic-gate missdir(char *nam_p)
53557c478bd9Sstevel@tonic-gate {
53567c478bd9Sstevel@tonic-gate 	char *c_p;
53577c478bd9Sstevel@tonic-gate 	int cnt = 2;
53587c478bd9Sstevel@tonic-gate 	char *lastp;
53597c478bd9Sstevel@tonic-gate 
53607c478bd9Sstevel@tonic-gate 	if (*(c_p = nam_p) == '/') /* skip over 'root slash' */
53617c478bd9Sstevel@tonic-gate 		c_p++;
53627c478bd9Sstevel@tonic-gate 
53637c478bd9Sstevel@tonic-gate 	lastp = c_p + strlen(nam_p) - 1;
53647c478bd9Sstevel@tonic-gate 	if (*lastp == '/')
53657c478bd9Sstevel@tonic-gate 		*lastp = '\0';
53667c478bd9Sstevel@tonic-gate 
53677c478bd9Sstevel@tonic-gate 	for (; *c_p; ++c_p) {
53687c478bd9Sstevel@tonic-gate 		if (*c_p == '/') {
53697c478bd9Sstevel@tonic-gate 			*c_p = '\0';
53707c478bd9Sstevel@tonic-gate 			if (stat(nam_p, &DesSt) < 0) {
53717c478bd9Sstevel@tonic-gate 				if (Args & OCd) {
53727c478bd9Sstevel@tonic-gate 					cnt = mkdir(nam_p, Def_mode);
53737c478bd9Sstevel@tonic-gate 					if (cnt != 0) {
53747c478bd9Sstevel@tonic-gate 						*c_p = '/';
53757c478bd9Sstevel@tonic-gate 						return (cnt);
53767c478bd9Sstevel@tonic-gate 					}
53777c478bd9Sstevel@tonic-gate 				} else {
53787c478bd9Sstevel@tonic-gate 					msg(ERR, "Missing -d option.");
53797c478bd9Sstevel@tonic-gate 					*c_p = '/';
53807c478bd9Sstevel@tonic-gate 					return (-1);
53817c478bd9Sstevel@tonic-gate 				}
53827c478bd9Sstevel@tonic-gate 			}
53837c478bd9Sstevel@tonic-gate 			*c_p = '/';
53847c478bd9Sstevel@tonic-gate 		}
53857c478bd9Sstevel@tonic-gate 	}
53867c478bd9Sstevel@tonic-gate 	if (cnt == 2) /* the file already exists */
53877c478bd9Sstevel@tonic-gate 		cnt = 0;
53887c478bd9Sstevel@tonic-gate 	return (cnt);
53897c478bd9Sstevel@tonic-gate }
53907c478bd9Sstevel@tonic-gate 
53917c478bd9Sstevel@tonic-gate /*
53927c478bd9Sstevel@tonic-gate  * mklong: Convert two shorts into one long.  For VAX, Interdata ...
53937c478bd9Sstevel@tonic-gate  */
53947c478bd9Sstevel@tonic-gate 
53957c478bd9Sstevel@tonic-gate static long
53967c478bd9Sstevel@tonic-gate mklong(short v[])
53977c478bd9Sstevel@tonic-gate {
53987c478bd9Sstevel@tonic-gate 
53997c478bd9Sstevel@tonic-gate 	union swpbuf swp_b;
54007c478bd9Sstevel@tonic-gate 
54017c478bd9Sstevel@tonic-gate 	swp_b.s_word = 1;
54027c478bd9Sstevel@tonic-gate 	if (swp_b.s_byte[0]) {
54037c478bd9Sstevel@tonic-gate 		swp_b.s_half[0] = v[1];
54047c478bd9Sstevel@tonic-gate 		swp_b.s_half[1] = v[0];
54057c478bd9Sstevel@tonic-gate 	} else {
54067c478bd9Sstevel@tonic-gate 		swp_b.s_half[0] = v[0];
54077c478bd9Sstevel@tonic-gate 		swp_b.s_half[1] = v[1];
54087c478bd9Sstevel@tonic-gate 	}
54097c478bd9Sstevel@tonic-gate 	return (swp_b.s_word);
54107c478bd9Sstevel@tonic-gate }
54117c478bd9Sstevel@tonic-gate 
54127c478bd9Sstevel@tonic-gate /*
54137c478bd9Sstevel@tonic-gate  * mkshort: Convert a long into 2 shorts, for VAX, Interdata ...
54147c478bd9Sstevel@tonic-gate  */
54157c478bd9Sstevel@tonic-gate 
54167c478bd9Sstevel@tonic-gate static void
54177c478bd9Sstevel@tonic-gate mkshort(short sval[], long v)
54187c478bd9Sstevel@tonic-gate {
54197c478bd9Sstevel@tonic-gate 	union swpbuf *swp_p, swp_b;
54207c478bd9Sstevel@tonic-gate 
54217c478bd9Sstevel@tonic-gate 	/* LINTED alignment */
54227c478bd9Sstevel@tonic-gate 	swp_p = (union swpbuf *)sval;
54237c478bd9Sstevel@tonic-gate 	swp_b.s_word = 1;
54247c478bd9Sstevel@tonic-gate 	if (swp_b.s_byte[0]) {
54257c478bd9Sstevel@tonic-gate 		swp_b.s_word = v;
54267c478bd9Sstevel@tonic-gate 		swp_p->s_half[0] = swp_b.s_half[1];
54277c478bd9Sstevel@tonic-gate 		swp_p->s_half[1] = swp_b.s_half[0];
54287c478bd9Sstevel@tonic-gate 	} else {
54297c478bd9Sstevel@tonic-gate 		swp_b.s_word = v;
54307c478bd9Sstevel@tonic-gate 		swp_p->s_half[0] = swp_b.s_half[0];
54317c478bd9Sstevel@tonic-gate 		swp_p->s_half[1] = swp_b.s_half[1];
54327c478bd9Sstevel@tonic-gate 	}
54337c478bd9Sstevel@tonic-gate }
54347c478bd9Sstevel@tonic-gate 
54357c478bd9Sstevel@tonic-gate /*
54367c478bd9Sstevel@tonic-gate  * msg: Print either a message (no error) (POST), an error message with or
54377c478bd9Sstevel@tonic-gate  * without the errno (ERRN or ERR), or print an error message with or without
54387c478bd9Sstevel@tonic-gate  * the errno and exit (EXTN or EXT).
54397c478bd9Sstevel@tonic-gate  */
54405fbb8099SNobutomo Nakano void
54417c478bd9Sstevel@tonic-gate msg(int severity, const char *fmt, ...)
54427c478bd9Sstevel@tonic-gate {
54437c478bd9Sstevel@tonic-gate 	FILE *file_p;
54447c478bd9Sstevel@tonic-gate 	va_list ap;
54457c478bd9Sstevel@tonic-gate 
54467c478bd9Sstevel@tonic-gate 	if ((Args & OCV) && Verbcnt) { /* clear current line of dots */
54477c478bd9Sstevel@tonic-gate 		(void) fputc('\n', Out_p);
54487c478bd9Sstevel@tonic-gate 		Verbcnt = 0;
54497c478bd9Sstevel@tonic-gate 	}
54507c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
54517c478bd9Sstevel@tonic-gate 	if (severity == POST)
54527c478bd9Sstevel@tonic-gate 		file_p = Out_p;
54537c478bd9Sstevel@tonic-gate 	else
54547c478bd9Sstevel@tonic-gate 		if (severity == EPOST)
54557c478bd9Sstevel@tonic-gate 			file_p = Err_p;
54567c478bd9Sstevel@tonic-gate 		else {
54577c478bd9Sstevel@tonic-gate 			file_p = Err_p;
54587c478bd9Sstevel@tonic-gate 			Error_cnt++;
54597c478bd9Sstevel@tonic-gate 		}
54607c478bd9Sstevel@tonic-gate 	(void) fflush(Out_p);
54617c478bd9Sstevel@tonic-gate 	(void) fflush(Err_p);
54627c478bd9Sstevel@tonic-gate 	if ((severity != POST) && (severity != EPOST))
54637c478bd9Sstevel@tonic-gate 		(void) fprintf(file_p, "cpio: ");
54647c478bd9Sstevel@tonic-gate 
54657c478bd9Sstevel@tonic-gate 	/* gettext replaces version of string */
54667c478bd9Sstevel@tonic-gate 
54677c478bd9Sstevel@tonic-gate 	(void) vfprintf(file_p, gettext(fmt), ap);
54687c478bd9Sstevel@tonic-gate 	if (severity == ERRN || severity == EXTN) {
54694582e5eaSchin 		if (G_p && (G_p->g_attrnam_p != NULL) && G_p->g_rw_sysattr) {
5470ced83f9bSceastha 			if (errno == EPERM) {
5471ced83f9bSceastha 				(void) fprintf(file_p, ", errno %d, %s", errno,
5472ced83f9bSceastha 				    gettext("insufficient privileges\n"));
5473ced83f9bSceastha 			} else if (errno == EINVAL) {
5474ced83f9bSceastha 				(void) fprintf(file_p, ", errno %d, %s",
5475ced83f9bSceastha 				    errno, gettext(
5476ced83f9bSceastha 				    "unsupported on underlying file system\n"));
5477ced83f9bSceastha 			} else {
54787c478bd9Sstevel@tonic-gate 				(void) fprintf(file_p, ", errno %d, ", errno);
54797c478bd9Sstevel@tonic-gate 				perror("");
5480ced83f9bSceastha 			}
5481ced83f9bSceastha 		} else {
5482ced83f9bSceastha 			(void) fprintf(file_p, ", errno %d, ", errno);
5483ced83f9bSceastha 			perror("");
5484ced83f9bSceastha 		}
54857c478bd9Sstevel@tonic-gate 	} else
54867c478bd9Sstevel@tonic-gate 		(void) fprintf(file_p, "\n");
54877c478bd9Sstevel@tonic-gate 	(void) fflush(file_p);
54887c478bd9Sstevel@tonic-gate 	va_end(ap);
54897c478bd9Sstevel@tonic-gate 	if (severity == EXT || severity == EXTN) {
54907c478bd9Sstevel@tonic-gate 		(void) fprintf(file_p, gettext("%d errors\n"), Error_cnt);
54917c478bd9Sstevel@tonic-gate 		exit(EXIT_CODE);
54927c478bd9Sstevel@tonic-gate 	}
54937c478bd9Sstevel@tonic-gate }
54947c478bd9Sstevel@tonic-gate 
54957c478bd9Sstevel@tonic-gate /*
54967c478bd9Sstevel@tonic-gate  * openout: Open files for output and set all necessary information.
54977c478bd9Sstevel@tonic-gate  * If the u option is set (unconditionally overwrite existing files),
54987c478bd9Sstevel@tonic-gate  * and the current file exists, get a temporary file name from mktemp(3C),
54997c478bd9Sstevel@tonic-gate  * link the temporary file to the existing file, and remove the existing file.
55007c478bd9Sstevel@tonic-gate  * Finally either creat(2), mkdir(2) or mknod(2) as appropriate.
55017c478bd9Sstevel@tonic-gate  *
55027c478bd9Sstevel@tonic-gate  */
55037c478bd9Sstevel@tonic-gate 
55047c478bd9Sstevel@tonic-gate static int
55057c478bd9Sstevel@tonic-gate openout(int dirfd)
55067c478bd9Sstevel@tonic-gate {
55077c478bd9Sstevel@tonic-gate 	char *nam_p;
55087c478bd9Sstevel@tonic-gate 	int cnt, result;
55097c478bd9Sstevel@tonic-gate 
55107c478bd9Sstevel@tonic-gate 	Do_rename = 0;	/* creat_tmp() may reset this */
55117c478bd9Sstevel@tonic-gate 
5512ced83f9bSceastha 	if (G_p->g_attrnam_p != NULL) {
55137c478bd9Sstevel@tonic-gate 		nam_p = G_p->g_attrnam_p;
55147c478bd9Sstevel@tonic-gate 	} else {
55157c478bd9Sstevel@tonic-gate 		if (Args & OCp) {
55167c478bd9Sstevel@tonic-gate 			nam_p = Fullnam_p;
55177c478bd9Sstevel@tonic-gate 		} else {
55187c478bd9Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
55197c478bd9Sstevel@tonic-gate 		}
55207c478bd9Sstevel@tonic-gate 	}
55217c478bd9Sstevel@tonic-gate 
55227c478bd9Sstevel@tonic-gate 
55237c478bd9Sstevel@tonic-gate 	if ((Max_filesz != RLIM_INFINITY) &&
55247c478bd9Sstevel@tonic-gate 	    (Max_filesz < (G_p->g_filesz >> 9))) {
55257c478bd9Sstevel@tonic-gate 		/* ... divided by 512 ... */
55267c478bd9Sstevel@tonic-gate 		msg(ERR, "Skipping \"%s%s%s\": exceeds ulimit by %lld bytes",
5527ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? nam_p : G_p->g_attrfnam_p,
5528ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? "" : G_p->g_rw_sysattr ?
5529ced83f9bSceastha 		    gettext(" System Attribute ") : gettext(" Attribute "),
5530ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? "" : nam_p,
55317c478bd9Sstevel@tonic-gate 		    (off_t)(G_p->g_filesz - (Max_filesz << 9)));
55327c478bd9Sstevel@tonic-gate 		return (-1);
55337c478bd9Sstevel@tonic-gate 	}
55347c478bd9Sstevel@tonic-gate 
55357c478bd9Sstevel@tonic-gate 	if (LSTAT(dirfd, nam_p, &DesSt) == 0) {
55367c478bd9Sstevel@tonic-gate 		/*
55377c478bd9Sstevel@tonic-gate 		 * A file by the same name exists.  Move it to a temporary
5538ced83f9bSceastha 		 * file unless it's a system attribute file.  If we are
5539ced83f9bSceastha 		 * restoring a system attribute file on a file system that
5540ced83f9bSceastha 		 * supports system attributes, then the system attribute file
5541ced83f9bSceastha 		 * will already exist (a default system attribute file will
5542ced83f9bSceastha 		 * get created when the file it is associated with is created).
5543ced83f9bSceastha 		 * If we create a temporary system attribute file, we can't
5544ced83f9bSceastha 		 * overwrite the existing system attribute file using
5545ced83f9bSceastha 		 * renameat().  In addition, only system attributes can exist
5546ced83f9bSceastha 		 * for an attribute of a file, therefore, a temporary file
5547ced83f9bSceastha 		 * cannot be created for a system attribute of an attribute.
5548ced83f9bSceastha 		 * Thus, when restoring a system attribute, we won't move it
5549ced83f9bSceastha 		 * to a temporary file, but will attempt to process it as if
5550ced83f9bSceastha 		 * it didn't already exist.
55517c478bd9Sstevel@tonic-gate 		 */
55527c478bd9Sstevel@tonic-gate 
5553ced83f9bSceastha #if defined(_PC_SATTR_ENABLED)
5554ced83f9bSceastha 		if (G_p->g_rw_sysattr == 0)
5555ced83f9bSceastha #endif	/* _PC_SATTR_ENABLED */
55567c478bd9Sstevel@tonic-gate 			if (creat_tmp(nam_p) < 0) {
55577c478bd9Sstevel@tonic-gate 				/*
5558ced83f9bSceastha 				 * We weren't able to create the temp file.
5559ced83f9bSceastha 				 * Report failure.
55607c478bd9Sstevel@tonic-gate 				 */
55617c478bd9Sstevel@tonic-gate 
55627c478bd9Sstevel@tonic-gate 				return (-1);
55637c478bd9Sstevel@tonic-gate 			}
55647c478bd9Sstevel@tonic-gate 	}
55657c478bd9Sstevel@tonic-gate 
55667c478bd9Sstevel@tonic-gate 	if (Do_rename) {
55677c478bd9Sstevel@tonic-gate 		/* nam_p was changed by creat_tmp() above. */
55687c478bd9Sstevel@tonic-gate 
55697c478bd9Sstevel@tonic-gate 		if (Args & OCp) {
5570ced83f9bSceastha 			if (G_p->g_attrnam_p != NULL) {
55717c478bd9Sstevel@tonic-gate 				nam_p = Attrfile_p;
55727c478bd9Sstevel@tonic-gate 			} else {
55737c478bd9Sstevel@tonic-gate 				nam_p = Fullnam_p;
55747c478bd9Sstevel@tonic-gate 			}
55757c478bd9Sstevel@tonic-gate 		} else {
55767c478bd9Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
55777c478bd9Sstevel@tonic-gate 		}
55787c478bd9Sstevel@tonic-gate 	}
55797c478bd9Sstevel@tonic-gate 
55807c478bd9Sstevel@tonic-gate 	/*
55817c478bd9Sstevel@tonic-gate 	 * This pile tries to create the file directly, and, if there is a
55827c478bd9Sstevel@tonic-gate 	 * problem, creates missing directories, and then tries to create the
55837c478bd9Sstevel@tonic-gate 	 * file again.  Two strikes and you're out.
55847c478bd9Sstevel@tonic-gate 	 *
55857c478bd9Sstevel@tonic-gate 	 * On XATTR system, the directory has already been created by
55867c478bd9Sstevel@tonic-gate 	 * open_dirfd(), so error shouldn't happen in the loop. However,
55877c478bd9Sstevel@tonic-gate 	 * on non-XATTR system, symlink/open may fail with ENOENT. In such
55887c478bd9Sstevel@tonic-gate 	 * case, we go to create missing directories.
55897c478bd9Sstevel@tonic-gate 	 */
55907c478bd9Sstevel@tonic-gate 
55917c478bd9Sstevel@tonic-gate 	cnt = 0;
55927c478bd9Sstevel@tonic-gate 
55937c478bd9Sstevel@tonic-gate 	do {
55947c478bd9Sstevel@tonic-gate 		errno = 0;
55957c478bd9Sstevel@tonic-gate 
55967c478bd9Sstevel@tonic-gate 		if (Hdr_type == TAR && Thdr_p->tbuf.t_typeflag == SYMTYPE) {
55977c478bd9Sstevel@tonic-gate 			/* The archive file is a TAR symlink. */
55987c478bd9Sstevel@tonic-gate 			if ((result =
55997c478bd9Sstevel@tonic-gate 			    symlink(Thdr_p->tbuf.t_linkname, nam_p)) >= 0) {
56007c478bd9Sstevel@tonic-gate 				cnt = 0;
56017c478bd9Sstevel@tonic-gate 				if (Over_p != NULL) {
56027c478bd9Sstevel@tonic-gate 					(void) unlinkat(dirfd,
56037c478bd9Sstevel@tonic-gate 					    get_component(Over_p), 0);
56047c478bd9Sstevel@tonic-gate 					*Over_p = '\0';
56057c478bd9Sstevel@tonic-gate 				}
56067c478bd9Sstevel@tonic-gate 				break;
56077c478bd9Sstevel@tonic-gate 			} else if (errno != ENOENT) {
56087c478bd9Sstevel@tonic-gate 				/* The attempt to symlink failed. */
56097c478bd9Sstevel@tonic-gate 				msg(ERRN,
56107c478bd9Sstevel@tonic-gate 				    "Cannot create symbolic link \"%s\" -> "
56117c478bd9Sstevel@tonic-gate 				    "\"%s\"",
56127c478bd9Sstevel@tonic-gate 				    Thdr_p->tbuf.t_linkname, nam_p);
56137c478bd9Sstevel@tonic-gate 
56147c478bd9Sstevel@tonic-gate 				if (*Over_p != '\0') {
56157c478bd9Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
56167c478bd9Sstevel@tonic-gate 				}
56177c478bd9Sstevel@tonic-gate 				return (-1);
56187c478bd9Sstevel@tonic-gate 			}
56197c478bd9Sstevel@tonic-gate 		} else if (Hdr_type == BAR && bar_linkflag == SYMTYPE) {
56207c478bd9Sstevel@tonic-gate 			if ((result = symlink(bar_linkname, nam_p)) >= 0) {
56217c478bd9Sstevel@tonic-gate 				cnt = 0;
56227c478bd9Sstevel@tonic-gate 				if (Over_p != NULL) {
56237c478bd9Sstevel@tonic-gate 					(void) unlinkat(dirfd,
56247c478bd9Sstevel@tonic-gate 					    get_component(Over_p), 0);
56257c478bd9Sstevel@tonic-gate 					*Over_p = '\0';
56267c478bd9Sstevel@tonic-gate 				}
56277c478bd9Sstevel@tonic-gate 				break;
56287c478bd9Sstevel@tonic-gate 			} else if (errno != ENOENT) {
56297c478bd9Sstevel@tonic-gate 				/* The attempt to symlink failed. */
56307c478bd9Sstevel@tonic-gate 				msg(ERRN,
56317c478bd9Sstevel@tonic-gate 				    "Cannot create symbolic link \"%s\" -> "
56327c478bd9Sstevel@tonic-gate 				    "\"%s\"",
56337c478bd9Sstevel@tonic-gate 				    bar_linkname, nam_p);
56347c478bd9Sstevel@tonic-gate 				if (*Over_p != '\0') {
56357c478bd9Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
56367c478bd9Sstevel@tonic-gate 				}
56377c478bd9Sstevel@tonic-gate 				return (-1);
56387c478bd9Sstevel@tonic-gate 			}
56397c478bd9Sstevel@tonic-gate 		} else if ((G_p->g_mode & Ftype) == S_IFLNK) {
56407c478bd9Sstevel@tonic-gate 			if ((!(Args & OCp)) && !(Hdr_type == USTAR)) {
564132fd2847Snakanon 				FILL(G_p->g_filesz);
56427c478bd9Sstevel@tonic-gate 				(void) strncpy(Symlnk_p,
56437c478bd9Sstevel@tonic-gate 				    Buffr.b_out_p, G_p->g_filesz);
56447c478bd9Sstevel@tonic-gate 				*(Symlnk_p + G_p->g_filesz) = '\0';
56457c478bd9Sstevel@tonic-gate 			} else if ((!(Args & OCp)) && (Hdr_type == USTAR)) {
56467c478bd9Sstevel@tonic-gate 				Symlnk_p[NAMSIZ] = '\0';
56477c478bd9Sstevel@tonic-gate 				(void) strncpy(Symlnk_p,
56487c478bd9Sstevel@tonic-gate 				    &Thdr_p->tbuf.t_linkname[0], NAMSIZ);
56497c478bd9Sstevel@tonic-gate 			}
56507c478bd9Sstevel@tonic-gate 			if ((result = symlink(Symlnk_p, nam_p)) >= 0) {
56517c478bd9Sstevel@tonic-gate 				cnt = 0;
56527c478bd9Sstevel@tonic-gate 				if (Over_p != NULL) {
56537c478bd9Sstevel@tonic-gate 					(void) unlinkat(dirfd,
56547c478bd9Sstevel@tonic-gate 					    get_component(Over_p), 0);
56557c478bd9Sstevel@tonic-gate 					*Over_p = '\0';
56567c478bd9Sstevel@tonic-gate 				}
56577c478bd9Sstevel@tonic-gate 				break;
56587c478bd9Sstevel@tonic-gate 			} else if (errno != ENOENT) {
56597c478bd9Sstevel@tonic-gate 				/* The attempt to symlink failed. */
56607c478bd9Sstevel@tonic-gate 				msg(ERRN,
56617c478bd9Sstevel@tonic-gate 				    "Cannot create symbolic link \"%s\" -> "
56627c478bd9Sstevel@tonic-gate 				    "\"%s\"",
56637c478bd9Sstevel@tonic-gate 				    Symlnk_p, nam_p);
56647c478bd9Sstevel@tonic-gate 
56657c478bd9Sstevel@tonic-gate 				if (*Over_p != '\0') {
56667c478bd9Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
56677c478bd9Sstevel@tonic-gate 				}
56687c478bd9Sstevel@tonic-gate 				return (-1);
56697c478bd9Sstevel@tonic-gate 			}
56707c478bd9Sstevel@tonic-gate 		} else {
5671ced83f9bSceastha 			int	saveerrno;
5672ced83f9bSceastha 
56737c478bd9Sstevel@tonic-gate 			if ((result = openat(dirfd, get_component(nam_p),
5674ced83f9bSceastha 			    O_CREAT|O_RDWR|O_TRUNC, (int)G_p->g_mode)) < 0) {
5675ced83f9bSceastha 				saveerrno = errno;
5676ced83f9bSceastha 				if (G_p->g_attrnam_p != NULL)  {
5677ced83f9bSceastha 					result = retry_open_attr(dirfd,
5678ced83f9bSceastha 					    Gen.g_baseparent_fd, Fullnam_p,
5679ced83f9bSceastha 					    (G_p->g_attrparent_p == NULL) ?
5680ced83f9bSceastha 					    NULL : G_p->g_attrparent_p, nam_p,
5681ced83f9bSceastha 					    O_CREAT|O_RDWR|O_TRUNC,
5682ced83f9bSceastha 					    (int)G_p->g_mode);
5683ced83f9bSceastha 				}
5684ced83f9bSceastha 			}
5685ced83f9bSceastha 			if (result < 0) {
5686ced83f9bSceastha 				errno = saveerrno;
5687ced83f9bSceastha 				if (errno != ENOENT) {
5688ced83f9bSceastha 					/* The attempt to open failed. */
5689ced83f9bSceastha 					msg(ERRN, "Cannot open file \"%s\"",
5690ced83f9bSceastha 					    nam_p);
5691ced83f9bSceastha 					if (*Over_p != '\0') {
5692ced83f9bSceastha 						rstfiles(U_KEEP, dirfd);
5693ced83f9bSceastha 					}
5694ced83f9bSceastha 					return (-1);
5695ced83f9bSceastha 				}
5696ced83f9bSceastha 			} else {
56977c478bd9Sstevel@tonic-gate 				/* acl support */
5698fa9e4066Sahrens 				acl_is_set = 0;
56997c478bd9Sstevel@tonic-gate 				if (Pflag && aclp != NULL) {
5700fa9e4066Sahrens 					if (facl_set(result, aclp) < 0) {
57017c478bd9Sstevel@tonic-gate 						msg(ERRN,
57027c478bd9Sstevel@tonic-gate 						    "\"%s\": failed to set acl",
57037c478bd9Sstevel@tonic-gate 						    nam_p);
57047c478bd9Sstevel@tonic-gate 					} else {
5705fa9e4066Sahrens 						acl_is_set = 1;
57067c478bd9Sstevel@tonic-gate 					}
5707fa9e4066Sahrens 					acl_free(aclp);
57087c478bd9Sstevel@tonic-gate 					aclp = NULL;
57097c478bd9Sstevel@tonic-gate 				}
57107c478bd9Sstevel@tonic-gate 				cnt = 0;
57117c478bd9Sstevel@tonic-gate 				break;
57127c478bd9Sstevel@tonic-gate 			}
57137c478bd9Sstevel@tonic-gate 		}
57147c478bd9Sstevel@tonic-gate 		cnt++;
57157c478bd9Sstevel@tonic-gate 	} while (cnt < 2 && missdir(nam_p) == 0);
57167c478bd9Sstevel@tonic-gate 
57177c478bd9Sstevel@tonic-gate 	switch (cnt) {
57187c478bd9Sstevel@tonic-gate 	case 0:
57197c478bd9Sstevel@tonic-gate 		if ((Args & OCi) && (Hdr_type == USTAR)) {
57207c478bd9Sstevel@tonic-gate 			setpasswd(nam_p);
57217c478bd9Sstevel@tonic-gate 		}
57227c478bd9Sstevel@tonic-gate 		if ((G_p->g_mode & Ftype) == S_IFLNK ||
57237c478bd9Sstevel@tonic-gate 		    (Hdr_type == BAR && bar_linkflag == SYMTYPE)) {
5724647ab8f4Sceastha 			if (Args & OCR) {
5725647ab8f4Sceastha 				if (fchownat(dirfd,
5726647ab8f4Sceastha 				    get_component(nam_p),
5727647ab8f4Sceastha 				    (int)Rpw_p->pw_uid,
5728647ab8f4Sceastha 				    (int)Rpw_p->pw_gid,
57297c478bd9Sstevel@tonic-gate 				    AT_SYMLINK_NOFOLLOW) < 0) {
57307c478bd9Sstevel@tonic-gate 					msg(ERRN,
57317c478bd9Sstevel@tonic-gate 					    "Error during chown() of "
57327c478bd9Sstevel@tonic-gate 					    "\"%s%s%s\"",
5733ced83f9bSceastha 					    (G_p->g_attrnam_p == NULL) ?
57347c478bd9Sstevel@tonic-gate 					    nam_p : G_p->g_attrfnam_p,
5735ced83f9bSceastha 					    (G_p->g_attrnam_p == NULL) ?
5736ced83f9bSceastha 					    "" : G_p->g_rw_sysattr ?
5737ced83f9bSceastha 					    gettext(" System Attribute ") :
5738ced83f9bSceastha 					    gettext(" Attribute "),
5739ced83f9bSceastha 					    (G_p->g_attrnam_p == NULL) ?
57407c478bd9Sstevel@tonic-gate 					    "" : nam_p);
57417c478bd9Sstevel@tonic-gate 				}
5742647ab8f4Sceastha 			} else if ((fchownat(dirfd, get_component(nam_p),
5743647ab8f4Sceastha 			    (int)G_p->g_uid, (int)G_p->g_gid,
5744647ab8f4Sceastha 			    AT_SYMLINK_NOFOLLOW) < 0) && privileged) {
57457c478bd9Sstevel@tonic-gate 				msg(ERRN,
57467c478bd9Sstevel@tonic-gate 				    "Error during chown() of \"%s%s%s\"",
5747ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ?
57487c478bd9Sstevel@tonic-gate 				    nam_p : G_p->g_attrfnam_p,
5749ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ? "" :
5750ced83f9bSceastha 				    G_p->g_rw_sysattr ?
5751ced83f9bSceastha 				    gettext(" System Attribute ") :
5752ced83f9bSceastha 				    gettext(" Attribute "),
5753ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ? "" : nam_p);
57547c478bd9Sstevel@tonic-gate 			}
57557c478bd9Sstevel@tonic-gate 		}
57567c478bd9Sstevel@tonic-gate 		break;
57577c478bd9Sstevel@tonic-gate 
57587c478bd9Sstevel@tonic-gate 	case 1:
57597c478bd9Sstevel@tonic-gate 		if (Do_rename) {
57607c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot create directory for \"%s%s%s\"",
5761ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? Over_p :
57627c478bd9Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
5763ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
5764ced83f9bSceastha 			    G_p->g_rw_sysattr ?
5765ced83f9bSceastha 			    gettext(" System Attribute ") :
57667c478bd9Sstevel@tonic-gate 			    gettext(" Attribute "),
5767ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" : Over_p);
57687c478bd9Sstevel@tonic-gate 		} else {
57697c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot create directory for \"%s%s%s\"",
5770ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? nam_p :
57717c478bd9Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
5772ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
5773ced83f9bSceastha 			    G_p->g_rw_sysattr ?
5774ced83f9bSceastha 			    gettext(" System Attribute ") :
57757c478bd9Sstevel@tonic-gate 			    gettext(" Attribute "),
5776ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" : nam_p);
57777c478bd9Sstevel@tonic-gate 		}
57787c478bd9Sstevel@tonic-gate 		break;
57797c478bd9Sstevel@tonic-gate 
57807c478bd9Sstevel@tonic-gate 	case 2:
57817c478bd9Sstevel@tonic-gate 		if (Do_rename) {
57827c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot create \"%s%s%s\"",
5783ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? Over_p :
57847c478bd9Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
5785ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
5786ced83f9bSceastha 			    G_p->g_rw_sysattr ?
5787ced83f9bSceastha 			    gettext(" System Attribute ") :
57887c478bd9Sstevel@tonic-gate 			    gettext(" Attribute "),
5789ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
57907c478bd9Sstevel@tonic-gate 			    Over_p);
57917c478bd9Sstevel@tonic-gate 		} else {
57927c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot create \"%s%s%s\"",
5793ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? nam_p :
57947c478bd9Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
5795ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
5796ced83f9bSceastha 			    G_p->g_rw_sysattr ?
5797ced83f9bSceastha 			    gettext(" System Attribute ") :
57987c478bd9Sstevel@tonic-gate 			    gettext(" Attribute "),
5799ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" : nam_p);
58007c478bd9Sstevel@tonic-gate 		}
58017c478bd9Sstevel@tonic-gate 		break;
58027c478bd9Sstevel@tonic-gate 
58037c478bd9Sstevel@tonic-gate 	default:
58047c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible case.");
58057c478bd9Sstevel@tonic-gate 	}
58067c478bd9Sstevel@tonic-gate 
58077c478bd9Sstevel@tonic-gate 	Finished = 0;
58087c478bd9Sstevel@tonic-gate 	return (result);
58097c478bd9Sstevel@tonic-gate }
58107c478bd9Sstevel@tonic-gate 
58117c478bd9Sstevel@tonic-gate /*
58127c478bd9Sstevel@tonic-gate  * read_hdr: Transfer headers from the selected format
58137c478bd9Sstevel@tonic-gate  * in the archive I/O buffer to the generic structure.
58147c478bd9Sstevel@tonic-gate  */
58157c478bd9Sstevel@tonic-gate 
58167c478bd9Sstevel@tonic-gate static
58177c478bd9Sstevel@tonic-gate int
58187c478bd9Sstevel@tonic-gate read_hdr(int hdr)
58197c478bd9Sstevel@tonic-gate {
58207c478bd9Sstevel@tonic-gate 	int rv = NONE;
58217c478bd9Sstevel@tonic-gate 	major_t maj, rmaj;
58227c478bd9Sstevel@tonic-gate 	minor_t min, rmin;
58237c478bd9Sstevel@tonic-gate 	char tmpnull;
58247c478bd9Sstevel@tonic-gate 	static int bar_read_cnt = 0;
58257c478bd9Sstevel@tonic-gate 
58267c478bd9Sstevel@tonic-gate 	if (hdr != BAR) {
58277c478bd9Sstevel@tonic-gate 		if (Buffr.b_end_p != (Buffr.b_out_p + Hdrsz)) {
58287c478bd9Sstevel@tonic-gate 			tmpnull = *(Buffr.b_out_p + Hdrsz);
58297c478bd9Sstevel@tonic-gate 			*(Buffr.b_out_p + Hdrsz) = '\0';
58307c478bd9Sstevel@tonic-gate 		}
58317c478bd9Sstevel@tonic-gate 	}
58327c478bd9Sstevel@tonic-gate 
58337c478bd9Sstevel@tonic-gate 	switch (hdr) {
58347c478bd9Sstevel@tonic-gate 	case BIN:
58357c478bd9Sstevel@tonic-gate 		(void) memcpy(&Hdr, Buffr.b_out_p, HDRSZ);
58367c478bd9Sstevel@tonic-gate 		if (Hdr.h_magic == (short)CMN_BBS) {
58377c478bd9Sstevel@tonic-gate 			swap((char *)&Hdr, HDRSZ);
58387c478bd9Sstevel@tonic-gate 		}
58397c478bd9Sstevel@tonic-gate 		Gen.g_magic = Hdr.h_magic;
58407c478bd9Sstevel@tonic-gate 		Gen.g_mode = Hdr.h_mode;
58417c478bd9Sstevel@tonic-gate 		Gen.g_uid = Hdr.h_uid;
58427c478bd9Sstevel@tonic-gate 		Gen.g_gid = Hdr.h_gid;
58437c478bd9Sstevel@tonic-gate 		Gen.g_nlink = Hdr.h_nlink;
58447c478bd9Sstevel@tonic-gate 		Gen.g_mtime = mklong(Hdr.h_mtime);
58457c478bd9Sstevel@tonic-gate 		Gen.g_ino = Hdr.h_ino;
58467c478bd9Sstevel@tonic-gate 		Gen.g_dev = Hdr.h_dev;
58477c478bd9Sstevel@tonic-gate 		Gen.g_rdev = Hdr.h_rdev;
58487c478bd9Sstevel@tonic-gate 		Gen.g_cksum = 0L;
58497c478bd9Sstevel@tonic-gate 		Gen.g_filesz = (off_t)mklong(Hdr.h_filesize);
58507c478bd9Sstevel@tonic-gate 		Gen.g_namesz = Hdr.h_namesize;
58517c478bd9Sstevel@tonic-gate 		rv = BIN;
58527c478bd9Sstevel@tonic-gate 		break;
58537c478bd9Sstevel@tonic-gate 	case CHR:
58547c478bd9Sstevel@tonic-gate 		if (sscanf(Buffr.b_out_p,
58557c478bd9Sstevel@tonic-gate 		    "%6lo%6lo%6lo%6lo%6lo%6lo%6lo%6lo%11lo%6o%11llo",
58567c478bd9Sstevel@tonic-gate 		    &Gen.g_magic, &Gen.g_dev, &Gen.g_ino, &Gen.g_mode,
58577c478bd9Sstevel@tonic-gate 		    &Gen.g_uid, &Gen.g_gid, &Gen.g_nlink, &Gen.g_rdev,
58587c478bd9Sstevel@tonic-gate 		    (ulong_t *)&Gen.g_mtime, (uint_t *)&Gen.g_namesz,
58597c478bd9Sstevel@tonic-gate 		    (u_off_t *)&Gen.g_filesz) == CHR_CNT) {
58607c478bd9Sstevel@tonic-gate 			rv = CHR;
58617c478bd9Sstevel@tonic-gate #define	cpioMAJOR(x)	(int)(((unsigned)x >> 8) & 0x7F)
58627c478bd9Sstevel@tonic-gate #define	cpioMINOR(x)	(int)(x & 0xFF)
58637c478bd9Sstevel@tonic-gate 			maj = cpioMAJOR(Gen.g_dev);
58647c478bd9Sstevel@tonic-gate 			rmaj = cpioMAJOR(Gen.g_rdev);
58657c478bd9Sstevel@tonic-gate 			min = cpioMINOR(Gen.g_dev);
58667c478bd9Sstevel@tonic-gate 			rmin = cpioMINOR(Gen.g_rdev);
58677c478bd9Sstevel@tonic-gate 			if (Use_old_stat) {
58687c478bd9Sstevel@tonic-gate 				/* needs error checking */
58697c478bd9Sstevel@tonic-gate 				Gen.g_dev = (maj << 8) | min;
58707c478bd9Sstevel@tonic-gate 				Gen.g_rdev = (rmaj << 8) | rmin;
58717c478bd9Sstevel@tonic-gate 			} else {
58727c478bd9Sstevel@tonic-gate 				Gen.g_dev = makedev(maj, min);
58737c478bd9Sstevel@tonic-gate 				Gen.g_rdev = makedev(rmaj, rmin);
58747c478bd9Sstevel@tonic-gate 			}
58757c478bd9Sstevel@tonic-gate 		}
58767c478bd9Sstevel@tonic-gate 		break;
58777c478bd9Sstevel@tonic-gate 	case ASC:
58787c478bd9Sstevel@tonic-gate 	case CRC:
58797c478bd9Sstevel@tonic-gate 		if (sscanf(Buffr.b_out_p,
58807c478bd9Sstevel@tonic-gate 		    "%6lx%8lx%8lx%8lx%8lx%8lx%8lx%8llx%8x%8x%8x%8x%8x%8lx",
58817c478bd9Sstevel@tonic-gate 		    &Gen.g_magic, &Gen.g_ino, &Gen.g_mode, &Gen.g_uid,
58827c478bd9Sstevel@tonic-gate 		    &Gen.g_gid, &Gen.g_nlink, &Gen.g_mtime,
58837c478bd9Sstevel@tonic-gate 		    (u_off_t *)&Gen.g_filesz, (uint_t *)&maj, (uint_t *)&min,
58847c478bd9Sstevel@tonic-gate 		    (uint_t *)&rmaj, (uint_t *)&rmin, (uint_t *)&Gen.g_namesz,
58857c478bd9Sstevel@tonic-gate 		    &Gen.g_cksum) == ASC_CNT) {
58867c478bd9Sstevel@tonic-gate 			Gen.g_dev = makedev(maj, min);
58877c478bd9Sstevel@tonic-gate 			Gen.g_rdev = makedev(rmaj, rmin);
58887c478bd9Sstevel@tonic-gate 			rv = hdr;
58897c478bd9Sstevel@tonic-gate 		}
58907c478bd9Sstevel@tonic-gate 		break;
58917c478bd9Sstevel@tonic-gate 	case USTAR: /* TAR and USTAR */
58927c478bd9Sstevel@tonic-gate 		if (*Buffr.b_out_p == '\0') {
58937c478bd9Sstevel@tonic-gate 			*Gen.g_nam_p = '\0';
58947c478bd9Sstevel@tonic-gate 			nambuf[0] = '\0';
58957c478bd9Sstevel@tonic-gate 		} else {
58967c478bd9Sstevel@tonic-gate 			Thdr_p = (union tblock *)Buffr.b_out_p;
58977c478bd9Sstevel@tonic-gate 			Gen.g_nam_p[0] = '\0';
58987c478bd9Sstevel@tonic-gate 			(void) strncpy((char *)&nambuf,
58997c478bd9Sstevel@tonic-gate 			    Thdr_p->tbuf.t_name, NAMSIZ);
59007c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mode, "%8lo",
59017c478bd9Sstevel@tonic-gate 			    &Gen.g_mode);
59027c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_uid, "%8lo", &Gen.g_uid);
59037c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_gid, "%8lo", &Gen.g_gid);
59047c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_size, "%11llo",
59057c478bd9Sstevel@tonic-gate 			    (u_off_t *)&Gen.g_filesz);
59067c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mtime, "%12lo",
59077c478bd9Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_mtime);
59087c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_cksum, "%8lo",
59097c478bd9Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_cksum);
5910ced83f9bSceastha 			if (Thdr_p->tbuf.t_linkname[0] != '\0')
59117c478bd9Sstevel@tonic-gate 				Gen.g_nlink = 1;
59127c478bd9Sstevel@tonic-gate 			else
59137c478bd9Sstevel@tonic-gate 				Gen.g_nlink = 0;
59147c478bd9Sstevel@tonic-gate 
59157c478bd9Sstevel@tonic-gate 			switch (Thdr_p->tbuf.t_typeflag) {
59167c478bd9Sstevel@tonic-gate 			case SYMTYPE:
59177c478bd9Sstevel@tonic-gate 				/* Symbolic Link */
59187c478bd9Sstevel@tonic-gate 				Gen.g_nlink = 2;
59197c478bd9Sstevel@tonic-gate 				break;
59207c478bd9Sstevel@tonic-gate 			case CHRTYPE:
59217c478bd9Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFCHR);
59227c478bd9Sstevel@tonic-gate 				break;
59237c478bd9Sstevel@tonic-gate 			case BLKTYPE:
59247c478bd9Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFBLK);
59257c478bd9Sstevel@tonic-gate 				break;
59267c478bd9Sstevel@tonic-gate 			case DIRTYPE:
59277c478bd9Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFDIR);
59287c478bd9Sstevel@tonic-gate 				break;
59297c478bd9Sstevel@tonic-gate 			case FIFOTYPE:
59307c478bd9Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFIFO);
59317c478bd9Sstevel@tonic-gate 				break;
59327c478bd9Sstevel@tonic-gate 			}
59337c478bd9Sstevel@tonic-gate 
59347c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_magic, "%8lo",
59357c478bd9Sstevel@tonic-gate 			    /* LINTED alignment */
59367c478bd9Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_tmagic);
59377c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_version, "%8lo",
59387c478bd9Sstevel@tonic-gate 			    /* LINTED alignment */
59397c478bd9Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_version);
59407c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_uname, "%32s",
59417c478bd9Sstevel@tonic-gate 			    (char *)&Gen.g_uname);
59427c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_gname, "%32s",
59437c478bd9Sstevel@tonic-gate 			    (char *)&Gen.g_gname);
59447c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_devmajor, "%8lo",
59457c478bd9Sstevel@tonic-gate 			    &Gen.g_dev);
59467c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_devminor, "%8lo",
59477c478bd9Sstevel@tonic-gate 			    &Gen.g_rdev);
59487c478bd9Sstevel@tonic-gate 			(void) strncpy((char *)&prebuf,
59497c478bd9Sstevel@tonic-gate 			    Thdr_p->tbuf.t_prefix, PRESIZ);
59507c478bd9Sstevel@tonic-gate 			Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
59517c478bd9Sstevel@tonic-gate 			Gen.g_dev = makedev(maj, min);
59527c478bd9Sstevel@tonic-gate 		}
59537c478bd9Sstevel@tonic-gate 		rv = USTAR;
59547c478bd9Sstevel@tonic-gate 		break;
59557c478bd9Sstevel@tonic-gate 	case TAR:
59567c478bd9Sstevel@tonic-gate 		if (*Buffr.b_out_p == '\0') {
59577c478bd9Sstevel@tonic-gate 			*Gen.g_nam_p = '\0';
59587c478bd9Sstevel@tonic-gate 			nambuf[0] = '\0';
59597c478bd9Sstevel@tonic-gate 		} else {
59607c478bd9Sstevel@tonic-gate 			Thdr_p = (union tblock *)Buffr.b_out_p;
59617c478bd9Sstevel@tonic-gate 			Gen.g_nam_p[0] = '\0';
59627c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mode, "%lo", &Gen.g_mode);
59637c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_uid, "%lo", &Gen.g_uid);
59647c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_gid, "%lo", &Gen.g_gid);
59657c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_size, "%llo",
59667c478bd9Sstevel@tonic-gate 			    (u_off_t *)&Gen.g_filesz);
59677c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mtime, "%lo",
59687c478bd9Sstevel@tonic-gate 			    &Gen.g_mtime);
59697c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_cksum, "%lo",
59707c478bd9Sstevel@tonic-gate 			    &Gen.g_cksum);
59717c478bd9Sstevel@tonic-gate 			if (Thdr_p->tbuf.t_typeflag == '1')	/* hardlink */
59727c478bd9Sstevel@tonic-gate 				Gen.g_nlink = 1;
59737c478bd9Sstevel@tonic-gate 			else
59747c478bd9Sstevel@tonic-gate 				Gen.g_nlink = 0;
59757c478bd9Sstevel@tonic-gate 			(void) strncpy(Gen.g_nam_p,
59767c478bd9Sstevel@tonic-gate 			    Thdr_p->tbuf.t_name, NAMSIZ);
59777c478bd9Sstevel@tonic-gate 			Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
59787c478bd9Sstevel@tonic-gate 			(void) strcpy(nambuf, Gen.g_nam_p);
59797c478bd9Sstevel@tonic-gate 		}
59807c478bd9Sstevel@tonic-gate 		rv = TAR;
59817c478bd9Sstevel@tonic-gate 		break;
59827c478bd9Sstevel@tonic-gate 	case BAR:
59837c478bd9Sstevel@tonic-gate 		if (Bar_vol_num == 0 && bar_read_cnt == 0) {
59847c478bd9Sstevel@tonic-gate 			read_bar_vol_hdr();
59857c478bd9Sstevel@tonic-gate 			bar_read_cnt++;
59867c478bd9Sstevel@tonic-gate 		}
59877c478bd9Sstevel@tonic-gate 		else
59887c478bd9Sstevel@tonic-gate 			read_bar_file_hdr();
59897c478bd9Sstevel@tonic-gate 		rv = BAR;
59907c478bd9Sstevel@tonic-gate 		break;
59917c478bd9Sstevel@tonic-gate 	default:
59927c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
59937c478bd9Sstevel@tonic-gate 	}
59947c478bd9Sstevel@tonic-gate 
59957c478bd9Sstevel@tonic-gate 	if (hdr != BAR) {
59967c478bd9Sstevel@tonic-gate 		if (Buffr.b_end_p != (Buffr.b_out_p + Hdrsz))
59977c478bd9Sstevel@tonic-gate 			*(Buffr.b_out_p + Hdrsz) = tmpnull;
59987c478bd9Sstevel@tonic-gate 	}
59997c478bd9Sstevel@tonic-gate 
60007c478bd9Sstevel@tonic-gate 	return (rv);
60017c478bd9Sstevel@tonic-gate }
60027c478bd9Sstevel@tonic-gate 
60037c478bd9Sstevel@tonic-gate /*
60047c478bd9Sstevel@tonic-gate  * reclaim: Reclaim linked file structure storage.
60057c478bd9Sstevel@tonic-gate  */
60067c478bd9Sstevel@tonic-gate 
60077c478bd9Sstevel@tonic-gate static void
60087c478bd9Sstevel@tonic-gate reclaim(struct Lnk *p)
60097c478bd9Sstevel@tonic-gate {
60107c478bd9Sstevel@tonic-gate 	p->L_bck_p->L_nxt_p = p->L_nxt_p;
60117c478bd9Sstevel@tonic-gate 	p->L_nxt_p->L_bck_p = p->L_bck_p;
60127c478bd9Sstevel@tonic-gate 
60137c478bd9Sstevel@tonic-gate 	while (p != NULL) {
60147c478bd9Sstevel@tonic-gate 		struct Lnk *new_p = p->L_lnk_p;
60157c478bd9Sstevel@tonic-gate 
60167c478bd9Sstevel@tonic-gate 		free(p->L_gen.g_nam_p);
60177c478bd9Sstevel@tonic-gate 		free(p);
60187c478bd9Sstevel@tonic-gate 		p = new_p;
60197c478bd9Sstevel@tonic-gate 	}
60207c478bd9Sstevel@tonic-gate }
60217c478bd9Sstevel@tonic-gate 
60227c478bd9Sstevel@tonic-gate /*
60237c478bd9Sstevel@tonic-gate  * rstbuf: Reset the I/O buffer, move incomplete potential headers to
60247c478bd9Sstevel@tonic-gate  * the front of the buffer and force bread() to refill the buffer.  The
60257c478bd9Sstevel@tonic-gate  * return value from bread() is returned (to identify I/O errors).  On the
60267c478bd9Sstevel@tonic-gate  * 3B2, reads must begin on a word boundary, therefore, with the -i option,
60277c478bd9Sstevel@tonic-gate  * any remaining bytes in the buffer must be moved to the base of the buffer
60287c478bd9Sstevel@tonic-gate  * in such a way that the destination locations of subsequent reads are
60297c478bd9Sstevel@tonic-gate  * word aligned.
60307c478bd9Sstevel@tonic-gate  */
60317c478bd9Sstevel@tonic-gate 
60327c478bd9Sstevel@tonic-gate static void
60337c478bd9Sstevel@tonic-gate rstbuf(void)
60347c478bd9Sstevel@tonic-gate {
60357c478bd9Sstevel@tonic-gate 	int pad;
60367c478bd9Sstevel@tonic-gate 
60377c478bd9Sstevel@tonic-gate 	if ((Args & OCi) || Append) {
60387c478bd9Sstevel@tonic-gate 		if (Buffr.b_out_p != Buffr.b_base_p) {
60397c478bd9Sstevel@tonic-gate 			pad = ((Buffr.b_cnt + FULLWD) & ~FULLWD);
60407c478bd9Sstevel@tonic-gate 			Buffr.b_in_p = Buffr.b_base_p + pad;
60417c478bd9Sstevel@tonic-gate 			pad -= Buffr.b_cnt;
60427c478bd9Sstevel@tonic-gate 			(void) memcpy(Buffr.b_base_p + pad, Buffr.b_out_p,
60437c478bd9Sstevel@tonic-gate 			    (int)Buffr.b_cnt);
60447c478bd9Sstevel@tonic-gate 			Buffr.b_out_p = Buffr.b_base_p + pad;
60457c478bd9Sstevel@tonic-gate 		}
60467c478bd9Sstevel@tonic-gate 		if (bfill() < 0)
60477c478bd9Sstevel@tonic-gate 			msg(EXT, "Unexpected end-of-archive encountered.");
60487c478bd9Sstevel@tonic-gate 	} else { /* OCo */
60497c478bd9Sstevel@tonic-gate 		(void) memcpy(Buffr.b_base_p, Buffr.b_out_p, (int)Buffr.b_cnt);
60507c478bd9Sstevel@tonic-gate 		Buffr.b_out_p = Buffr.b_base_p;
60517c478bd9Sstevel@tonic-gate 		Buffr.b_in_p = Buffr.b_base_p + Buffr.b_cnt;
60527c478bd9Sstevel@tonic-gate 	}
60537c478bd9Sstevel@tonic-gate }
60547c478bd9Sstevel@tonic-gate 
60557c478bd9Sstevel@tonic-gate static void
60567c478bd9Sstevel@tonic-gate setpasswd(char *nam)
60577c478bd9Sstevel@tonic-gate {
6058ced83f9bSceastha 	if ((dpasswd = getpwnam(&Gen.g_uname[0])) == NULL) {
60597c478bd9Sstevel@tonic-gate 		msg(EPOST, "cpio: problem reading passwd entry");
60607c478bd9Sstevel@tonic-gate 		msg(EPOST, "cpio: %s: owner not changed", nam);
60617c478bd9Sstevel@tonic-gate 		if (Gen.g_uid == UID_NOBODY && S_ISREG(Gen.g_mode))
60627c478bd9Sstevel@tonic-gate 			Gen.g_mode &= ~S_ISUID;
60637c478bd9Sstevel@tonic-gate 	} else
60647c478bd9Sstevel@tonic-gate 		Gen.g_uid = dpasswd->pw_uid;
60657c478bd9Sstevel@tonic-gate 
6066ced83f9bSceastha 	if ((dgroup = getgrnam(&Gen.g_gname[0])) == NULL) {
60677c478bd9Sstevel@tonic-gate 		msg(EPOST, "cpio: problem reading group entry");
60687c478bd9Sstevel@tonic-gate 		msg(EPOST, "cpio: %s: group not changed", nam);
60697c478bd9Sstevel@tonic-gate 		if (Gen.g_gid == GID_NOBODY && S_ISREG(Gen.g_mode))
60707c478bd9Sstevel@tonic-gate 			Gen.g_mode &= ~S_ISGID;
60717c478bd9Sstevel@tonic-gate 	} else
60727c478bd9Sstevel@tonic-gate 		Gen.g_gid = dgroup->gr_gid;
60737c478bd9Sstevel@tonic-gate 	G_p = &Gen;
60747c478bd9Sstevel@tonic-gate }
60757c478bd9Sstevel@tonic-gate 
60767c478bd9Sstevel@tonic-gate /*
60777c478bd9Sstevel@tonic-gate  * rstfiles:  Perform final changes to the file.  If the -u option is set,
60787c478bd9Sstevel@tonic-gate  * and overwrite == U_OVER, remove the temporary file, else if overwrite
60797c478bd9Sstevel@tonic-gate  * == U_KEEP, unlink the current file, and restore the existing version
60807c478bd9Sstevel@tonic-gate  * of the file.  In addition, where appropriate, set the access or modification
60817c478bd9Sstevel@tonic-gate  * times, change the owner and change the modes of the file.
60827c478bd9Sstevel@tonic-gate  *
60837c478bd9Sstevel@tonic-gate  * Note that if Do_rename is set, then the roles of original and temporary
60847c478bd9Sstevel@tonic-gate  * file are reversed. If all went well, we will rename() the temporary file
6085da6c28aaSamw  * over the original in order to accommodate potentially executing files.
60867c478bd9Sstevel@tonic-gate  */
60877c478bd9Sstevel@tonic-gate static void
60887c478bd9Sstevel@tonic-gate rstfiles(int over, int dirfd)
60897c478bd9Sstevel@tonic-gate {
60907c478bd9Sstevel@tonic-gate 	char *inam_p, *onam_p, *nam_p;
60917c478bd9Sstevel@tonic-gate 	int error;
60927c478bd9Sstevel@tonic-gate 
6093ced83f9bSceastha #if defined(_PC_SATTR_ENABLED)
6094ced83f9bSceastha 	/* Time or permissions cannot be set on system attribute files */
6095ced83f9bSceastha 	if ((Gen.g_attrnam_p != NULL) && (Gen.g_rw_sysattr == 1)) {
6096ced83f9bSceastha 		return;
6097ced83f9bSceastha 	}
6098ced83f9bSceastha #endif	/* _PC_SATTR_ENABLED */
6099ced83f9bSceastha 
61007c478bd9Sstevel@tonic-gate 	if (Args & OCp) {
6101ced83f9bSceastha 		if (G_p->g_attrnam_p == NULL) {
61027c478bd9Sstevel@tonic-gate 			nam_p = Fullnam_p;
61037c478bd9Sstevel@tonic-gate 		} else {
61047c478bd9Sstevel@tonic-gate 			nam_p = G_p->g_attrnam_p;
61057c478bd9Sstevel@tonic-gate 		}
61067c478bd9Sstevel@tonic-gate 	} else {
61077c478bd9Sstevel@tonic-gate 		if (Gen.g_nlink > (ulong_t)0) {
61087c478bd9Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
61097c478bd9Sstevel@tonic-gate 		} else {
61107c478bd9Sstevel@tonic-gate 			nam_p = Gen.g_nam_p;
61117c478bd9Sstevel@tonic-gate 		}
61127c478bd9Sstevel@tonic-gate 	}
6113ced83f9bSceastha 	if (Gen.g_attrnam_p != NULL) {
61147c478bd9Sstevel@tonic-gate 		nam_p = Gen.g_attrnam_p;
61157c478bd9Sstevel@tonic-gate 	}
61167c478bd9Sstevel@tonic-gate 
61177c478bd9Sstevel@tonic-gate 	if ((Args & OCi) && (Hdr_type == USTAR)) {
61187c478bd9Sstevel@tonic-gate 		setpasswd(nam_p);
61197c478bd9Sstevel@tonic-gate 	}
61207c478bd9Sstevel@tonic-gate 	if (over == U_KEEP && *Over_p != '\0') {
61217c478bd9Sstevel@tonic-gate 		if (Do_rename) {
61227c478bd9Sstevel@tonic-gate 			msg(POST, "Restoring existing \"%s%s%s\"",
6123ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? Over_p : Fullnam_p,
6124ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
6125ced83f9bSceastha 			    G_p->g_rw_sysattr ? gettext(" System Attribute ") :
6126ced83f9bSceastha 			    gettext(" Attribute "),
6127ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" : Over_p);
61287c478bd9Sstevel@tonic-gate 		} else {
61297c478bd9Sstevel@tonic-gate 			msg(POST, "Restoring existing \"%s%s%s\"",
6130ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? nam_p : Fullnam_p,
6131ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
6132ced83f9bSceastha 			    G_p->g_rw_sysattr ? gettext(" System Attribute ") :
6133ced83f9bSceastha 			    gettext(" Attribute "),
6134ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" : nam_p);
61357c478bd9Sstevel@tonic-gate 		}
61367c478bd9Sstevel@tonic-gate 
61377c478bd9Sstevel@tonic-gate 		/* delete what we just built */
61387c478bd9Sstevel@tonic-gate 		(void) unlinkat(dirfd, get_component(nam_p), 0);
61397c478bd9Sstevel@tonic-gate 
61407c478bd9Sstevel@tonic-gate 		/* If the old file needs restoring, do the necessary links */
61417c478bd9Sstevel@tonic-gate 		if (Do_rename) {
61427c478bd9Sstevel@tonic-gate 			char *tmp_ptr;
61437c478bd9Sstevel@tonic-gate 
61447c478bd9Sstevel@tonic-gate 			if (Args & OCp) {
61457c478bd9Sstevel@tonic-gate 				tmp_ptr = Fullnam_p;
61467c478bd9Sstevel@tonic-gate 				Fullnam_p = Over_p;
61477c478bd9Sstevel@tonic-gate 			} else {
61487c478bd9Sstevel@tonic-gate 				tmp_ptr = G_p->g_nam_p;
61497c478bd9Sstevel@tonic-gate 				G_p->g_nam_p = Over_p;
61507c478bd9Sstevel@tonic-gate 			}
61517c478bd9Sstevel@tonic-gate 			Over_p = tmp_ptr;
61527c478bd9Sstevel@tonic-gate 
61537c478bd9Sstevel@tonic-gate 			Do_rename = 0;	/* names now have original values */
61547c478bd9Sstevel@tonic-gate 		} else {
61557c478bd9Sstevel@tonic-gate 			if (rename(Over_p, nam_p) < 0) {
61567c478bd9Sstevel@tonic-gate 				if (link(Over_p, nam_p) < 0) {
61577c478bd9Sstevel@tonic-gate 					msg(EXTN,
61587c478bd9Sstevel@tonic-gate 					    "Cannot recover original version"
61597c478bd9Sstevel@tonic-gate 					    " of \"%s%s%s\"",
6160ced83f9bSceastha 					    (G_p->g_attrnam_p == NULL) ?
61617c478bd9Sstevel@tonic-gate 					    nam_p : Fullnam_p,
6162ced83f9bSceastha 					    (G_p->g_attrnam_p == NULL) ? "" :
6163ced83f9bSceastha 					    G_p->g_rw_sysattr ?
6164ced83f9bSceastha 					    gettext(" System Attribute ") :
6165ced83f9bSceastha 					    gettext(" Attribute "),
6166ced83f9bSceastha 					    (G_p->g_attrnam_p == NULL) ?
61677c478bd9Sstevel@tonic-gate 					    "" : nam_p);
61687c478bd9Sstevel@tonic-gate 				}
61697c478bd9Sstevel@tonic-gate 				if (unlinkat(dirfd, get_component(Over_p), 0)) {
61707c478bd9Sstevel@tonic-gate 					msg(ERRN,
61717c478bd9Sstevel@tonic-gate 					    "Cannot remove temp file "
61727c478bd9Sstevel@tonic-gate 					    "\"%s%s%s\"",
6173ced83f9bSceastha 					    (G_p->g_attrnam_p == NULL) ?
61747c478bd9Sstevel@tonic-gate 					    Over_p : Fullnam_p,
6175ced83f9bSceastha 					    (G_p->g_attrnam_p == NULL) ? "" :
6176ced83f9bSceastha 					    G_p->g_rw_sysattr ?
6177ced83f9bSceastha 					    gettext(" System Attribute ") :
6178ced83f9bSceastha 					    gettext(" Attribute "),
6179ced83f9bSceastha 					    (G_p->g_attrnam_p == NULL) ?
61807c478bd9Sstevel@tonic-gate 					    "" : Over_p);
61817c478bd9Sstevel@tonic-gate 				}
61827c478bd9Sstevel@tonic-gate 			}
61837c478bd9Sstevel@tonic-gate 		}
61847c478bd9Sstevel@tonic-gate 		*Over_p = '\0';
61857c478bd9Sstevel@tonic-gate 		return;
61867c478bd9Sstevel@tonic-gate 	} else if (over == U_OVER && *Over_p != '\0') {
61877c478bd9Sstevel@tonic-gate 		if (Do_rename) {
61887c478bd9Sstevel@tonic-gate 			char *tmp_ptr;
61897c478bd9Sstevel@tonic-gate 
61907c478bd9Sstevel@tonic-gate 			(void) renameat(dirfd, get_component(nam_p),
61917c478bd9Sstevel@tonic-gate 			    dirfd, get_component(Over_p));
61927c478bd9Sstevel@tonic-gate 			if (Args & OCp) {
6193ced83f9bSceastha 				if (G_p->g_attrnam_p == NULL) {
61947c478bd9Sstevel@tonic-gate 					tmp_ptr = Fullnam_p;
61957c478bd9Sstevel@tonic-gate 					Fullnam_p = Over_p;
61967c478bd9Sstevel@tonic-gate 					Over_p = tmp_ptr;
61977c478bd9Sstevel@tonic-gate 				} else {
61987c478bd9Sstevel@tonic-gate 					/*
61997c478bd9Sstevel@tonic-gate 					 * Over_p is pointing at g_attrnam_p
62007c478bd9Sstevel@tonic-gate 					 * which must be preserved.
62017c478bd9Sstevel@tonic-gate 					 *
62027c478bd9Sstevel@tonic-gate 					 * We don't want the tmp_ptr and so
62037c478bd9Sstevel@tonic-gate 					 * on to throw away our only copy of
62047c478bd9Sstevel@tonic-gate 					 * the name.
62057c478bd9Sstevel@tonic-gate 					 */
62067c478bd9Sstevel@tonic-gate 					Over_p = Attrfile_p;
62077c478bd9Sstevel@tonic-gate 				}
62087c478bd9Sstevel@tonic-gate 			} else {
62097c478bd9Sstevel@tonic-gate 				tmp_ptr = G_p->g_nam_p;
62107c478bd9Sstevel@tonic-gate 				G_p->g_nam_p = Over_p;
62117c478bd9Sstevel@tonic-gate 				Over_p = tmp_ptr;
62127c478bd9Sstevel@tonic-gate 			}
62137c478bd9Sstevel@tonic-gate 			Do_rename = 0;	/* names now have original values */
62147c478bd9Sstevel@tonic-gate 		} else {
62157c478bd9Sstevel@tonic-gate 			if (unlinkat(dirfd, get_component(Over_p), 0) < 0) {
62167c478bd9Sstevel@tonic-gate 				msg(ERRN,
62177c478bd9Sstevel@tonic-gate 				    "Cannot unlink() temp file \"%s%s%s\"",
6218ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ?
62197c478bd9Sstevel@tonic-gate 				    Over_p : Fullnam_p,
6220ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ? "" :
6221ced83f9bSceastha 				    G_p->g_rw_sysattr ?
6222ced83f9bSceastha 				    gettext(" System Attribute ") :
6223ced83f9bSceastha 				    gettext(" Attribute "),
6224ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ? "" : Over_p);
62257c478bd9Sstevel@tonic-gate 			}
62267c478bd9Sstevel@tonic-gate 		}
62277c478bd9Sstevel@tonic-gate 		*Over_p = '\0';
62287c478bd9Sstevel@tonic-gate 	}
62297c478bd9Sstevel@tonic-gate 	if (Args & OCp) {
6230ced83f9bSceastha 		if (G_p->g_attrnam_p != NULL) {
62317c478bd9Sstevel@tonic-gate 			inam_p = G_p->g_attrfnam_p;
62327c478bd9Sstevel@tonic-gate 			onam_p = G_p->g_attrnam_p;
62337c478bd9Sstevel@tonic-gate 		} else {
62347c478bd9Sstevel@tonic-gate 			inam_p = Nam_p;
62357c478bd9Sstevel@tonic-gate 			onam_p = Fullnam_p;
62367c478bd9Sstevel@tonic-gate 		}
62377c478bd9Sstevel@tonic-gate 	} else /* OCi only uses onam_p, OCo only uses inam_p */
6238ced83f9bSceastha 		if (G_p->g_attrnam_p != NULL) {
62397c478bd9Sstevel@tonic-gate 			inam_p = onam_p = G_p->g_attrnam_p;
62407c478bd9Sstevel@tonic-gate 		} else {
62417c478bd9Sstevel@tonic-gate 			inam_p = onam_p = G_p->g_nam_p;
62427c478bd9Sstevel@tonic-gate 		}
62437c478bd9Sstevel@tonic-gate 
62447c478bd9Sstevel@tonic-gate 	/*
6245647ab8f4Sceastha 	 * Change the owner, time, and mode to those of the file
6246647ab8f4Sceastha 	 * originally created in the archive.  Note: time and
6247647ab8f4Sceastha 	 * mode do not need to be restored for a symbolic link
6248647ab8f4Sceastha 	 * since rstfiles() is not called when the archived file
6249647ab8f4Sceastha 	 * is a symlink.
62507c478bd9Sstevel@tonic-gate 	 */
6251647ab8f4Sceastha 	if (!(Args & OCo)) {
6252647ab8f4Sceastha 		if (Args & OCR) {
6253647ab8f4Sceastha 			if (fchownat(dirfd, get_component(onam_p),
6254647ab8f4Sceastha 			    Rpw_p->pw_uid, Rpw_p->pw_gid,
6255647ab8f4Sceastha 			    AT_SYMLINK_NOFOLLOW) < 0) {
6256647ab8f4Sceastha 				msg(ERRN, "Cannot chown() \"%s%s%s\"",
6257647ab8f4Sceastha 				    onam_p,
6258ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ? "" :
6259ced83f9bSceastha 				    G_p->g_rw_sysattr ?
6260ced83f9bSceastha 				    gettext(" System Attribute ") :
6261ced83f9bSceastha 				    gettext(" Attribute "),
6262ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ? "" : onam_p);
6263647ab8f4Sceastha 			}
6264647ab8f4Sceastha 		} else {
6265647ab8f4Sceastha 			if ((fchownat(dirfd, get_component(onam_p),
6266647ab8f4Sceastha 			    G_p->g_uid, G_p->g_gid,
6267647ab8f4Sceastha 			    AT_SYMLINK_NOFOLLOW) < 0) && privileged) {
6268647ab8f4Sceastha 				msg(ERRN, "Cannot chown() \"%s%s%s\"",
6269647ab8f4Sceastha 				    onam_p,
6270ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ? "" :
6271ced83f9bSceastha 				    G_p->g_rw_sysattr ?
6272ced83f9bSceastha 				    gettext(" System Attribute ") :
6273ced83f9bSceastha 				    gettext(" Attribute "),
6274ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ? "" : onam_p);
6275647ab8f4Sceastha 			}
6276647ab8f4Sceastha 		}
62777c478bd9Sstevel@tonic-gate 
6278647ab8f4Sceastha 		if (Args & OCm) {
6279647ab8f4Sceastha 			set_tym(dirfd, get_component(onam_p),
6280647ab8f4Sceastha 			    G_p->g_mtime, G_p->g_mtime);
6281647ab8f4Sceastha 		}
6282647ab8f4Sceastha 
62837c478bd9Sstevel@tonic-gate 		/* Acl was not set, so we must chmod */
6284647ab8f4Sceastha 		if (!acl_is_set) {
6285647ab8f4Sceastha 			mode_t orig_mask, new_mask;
62867c478bd9Sstevel@tonic-gate 
62877c478bd9Sstevel@tonic-gate 			/*
62887c478bd9Sstevel@tonic-gate 			 * use fchmod for attributes, since
62897c478bd9Sstevel@tonic-gate 			 * we known they are always regular
62907c478bd9Sstevel@tonic-gate 			 * files, whereas when it isn't an
62917c478bd9Sstevel@tonic-gate 			 * attribute it could be for a fifo
62927c478bd9Sstevel@tonic-gate 			 * or something other that we don't
62937c478bd9Sstevel@tonic-gate 			 * open and don't have a valid Ofile
62947c478bd9Sstevel@tonic-gate 			 * for.
62957c478bd9Sstevel@tonic-gate 			 */
6296647ab8f4Sceastha 			if (privileged) {
6297647ab8f4Sceastha 				new_mask = G_p->g_mode;
6298647ab8f4Sceastha 			} else {
6299647ab8f4Sceastha 				orig_mask = umask(0);
6300647ab8f4Sceastha 				new_mask = G_p->g_mode & ~orig_mask;
6301647ab8f4Sceastha 			}
6302647ab8f4Sceastha 
6303ced83f9bSceastha 			if (G_p->g_attrnam_p != NULL) {
63047c478bd9Sstevel@tonic-gate 				error = fchmod(Ofile, new_mask);
63057c478bd9Sstevel@tonic-gate 			} else {
6306647ab8f4Sceastha 				error = chmod(onam_p, new_mask);
63077c478bd9Sstevel@tonic-gate 			}
63087c478bd9Sstevel@tonic-gate 			if (error < 0) {
63097c478bd9Sstevel@tonic-gate 				msg(ERRN,
63107c478bd9Sstevel@tonic-gate 				    "Cannot chmod() \"%s%s%s\"",
6311ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ?
6312647ab8f4Sceastha 				    onam_p : G_p->g_attrfnam_p,
6313ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ? "" :
6314ced83f9bSceastha 				    G_p->g_rw_sysattr ?
6315ced83f9bSceastha 				    gettext(" System Attribute ") :
6316ced83f9bSceastha 				    gettext(" Attribute "),
6317ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ? "" : onam_p);
63187c478bd9Sstevel@tonic-gate 			}
6319647ab8f4Sceastha 			if (!privileged) {
63207c478bd9Sstevel@tonic-gate 				(void) umask(orig_mask);
63217c478bd9Sstevel@tonic-gate 			}
63227c478bd9Sstevel@tonic-gate 		}
63237c478bd9Sstevel@tonic-gate 	}
63247c478bd9Sstevel@tonic-gate 
63257c478bd9Sstevel@tonic-gate 	if (!(Args & OCi) && (Args & OCa)) {
63267c478bd9Sstevel@tonic-gate 		/*
63277c478bd9Sstevel@tonic-gate 		 * Use dirfd since we are updating original file
63287c478bd9Sstevel@tonic-gate 		 * and not just created file
63297c478bd9Sstevel@tonic-gate 		 */
63307c478bd9Sstevel@tonic-gate 		set_tym(G_p->g_dirfd, get_component(inam_p),
63317c478bd9Sstevel@tonic-gate 		    (ulong_t)SrcSt.st_atime, (ulong_t)SrcSt.st_mtime);
63327c478bd9Sstevel@tonic-gate 	}
63337c478bd9Sstevel@tonic-gate }
63347c478bd9Sstevel@tonic-gate 
63357c478bd9Sstevel@tonic-gate /*
63367c478bd9Sstevel@tonic-gate  * scan4trail: Scan the archive looking for the trailer.
63377c478bd9Sstevel@tonic-gate  * When found, back the archive up over the trailer and overwrite
63387c478bd9Sstevel@tonic-gate  * the trailer with the files to be added to the archive.
63397c478bd9Sstevel@tonic-gate  */
63407c478bd9Sstevel@tonic-gate 
63417c478bd9Sstevel@tonic-gate static void
63427c478bd9Sstevel@tonic-gate scan4trail(void)
63437c478bd9Sstevel@tonic-gate {
63447c478bd9Sstevel@tonic-gate 	int rv;
63457c478bd9Sstevel@tonic-gate 	off_t off1, off2;
63467c478bd9Sstevel@tonic-gate 
63477c478bd9Sstevel@tonic-gate 	Append = 1;
63487c478bd9Sstevel@tonic-gate 	Hdr_type = NONE;
6349ced83f9bSceastha 	G_p = NULL;
63507c478bd9Sstevel@tonic-gate 	while (gethdr()) {
63517c478bd9Sstevel@tonic-gate 		G_p = &Gen;
63527c478bd9Sstevel@tonic-gate 		data_in(P_SKIP);
63537c478bd9Sstevel@tonic-gate 	}
63547c478bd9Sstevel@tonic-gate 	off1 = Buffr.b_cnt;
63557c478bd9Sstevel@tonic-gate 	off2 = Bufsize - (Buffr.b_cnt % Bufsize);
63567c478bd9Sstevel@tonic-gate 	Buffr.b_out_p = Buffr.b_in_p = Buffr.b_base_p;
63577c478bd9Sstevel@tonic-gate 	Buffr.b_cnt = (off_t)0;
63587c478bd9Sstevel@tonic-gate 	if (lseek(Archive, -(off1 + off2), SEEK_REL) < 0)
63597c478bd9Sstevel@tonic-gate 		msg(EXTN, "Unable to append to this archive");
63607c478bd9Sstevel@tonic-gate 	if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0)
63617c478bd9Sstevel@tonic-gate 		msg(EXTN, "Cannot append to this archive");
63627c478bd9Sstevel@tonic-gate 	if (lseek(Archive, (off_t)-rv, SEEK_REL) < 0)
63637c478bd9Sstevel@tonic-gate 		msg(EXTN, "Unable to append to this archive");
63647c478bd9Sstevel@tonic-gate 	Buffr.b_cnt = off2;
63657c478bd9Sstevel@tonic-gate 	Buffr.b_in_p = Buffr.b_base_p + Buffr.b_cnt;
63667c478bd9Sstevel@tonic-gate 	Append = 0;
63677c478bd9Sstevel@tonic-gate }
63687c478bd9Sstevel@tonic-gate 
63697c478bd9Sstevel@tonic-gate /*
63707c478bd9Sstevel@tonic-gate  * setup:  Perform setup and initialization functions.  Parse the options
63717c478bd9Sstevel@tonic-gate  * using getopt(3C), call ckopts to check the options and initialize various
63727c478bd9Sstevel@tonic-gate  * structures and pointers.  Specifically, for the -i option, save any
63737c478bd9Sstevel@tonic-gate  * patterns, for the -o option, check (via stat(2)) the archive, and for
63747c478bd9Sstevel@tonic-gate  * the -p option, validate the destination directory.
63757c478bd9Sstevel@tonic-gate  */
63767c478bd9Sstevel@tonic-gate 
63777c478bd9Sstevel@tonic-gate static void
63787c478bd9Sstevel@tonic-gate setup(int largc, char **largv)
63797c478bd9Sstevel@tonic-gate {
63807c478bd9Sstevel@tonic-gate 	extern int optind;
63817c478bd9Sstevel@tonic-gate 	extern char *optarg;
63827c478bd9Sstevel@tonic-gate 
63837c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
6384ced83f9bSceastha #if defined(_PC_SATTR_ENABLED)
6385ced83f9bSceastha #ifdef WAITAROUND
6386*b0ee9efaSGary Mills 	char	*opts_p = "zabcdfiklmopqrstuvABC:DE:H:I:LM:O:PR:SV6@/";
6387ced83f9bSceastha #else
6388*b0ee9efaSGary Mills 	char	*opts_p = "abcdfiklmopqrstuvABC:DE:H:I:LM:O:PR:SV6@/";
6389ced83f9bSceastha #endif	/* WAITAROUND */
6390ced83f9bSceastha 
6391ced83f9bSceastha #else	/* _PC_SATTR_ENABLED */
63927c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
6393*b0ee9efaSGary Mills 	char	*opts_p = "zabcdfiklmopqrstuvABC:DE:H:I:LM:O:PR:SV6@";
63947c478bd9Sstevel@tonic-gate #else
6395*b0ee9efaSGary Mills 	char	*opts_p = "abcdfiklmopqrstuvABC:DE:H:I:LM:O:PR:SV6@";
6396ced83f9bSceastha #endif	/* WAITAROUND */
6397ced83f9bSceastha #endif	/* _PC_SATTR_ENABLED */
6398ced83f9bSceastha 
6399ced83f9bSceastha #else	/* O_XATTR */
64007c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
6401*b0ee9efaSGary Mills 	char	*opts_p = "zabcdfiklmopqrstuvABC:DE:H:I:LM:O:PR:SV6";
64027c478bd9Sstevel@tonic-gate #else
6403*b0ee9efaSGary Mills 	char	*opts_p = "abcdfiklmopqrstuvABC:DE:H:I:LM:O:PR:SV6";
6404ced83f9bSceastha #endif	/* WAITAROUND */
6405ced83f9bSceastha #endif	/* O_XATTR */
64067c478bd9Sstevel@tonic-gate 
64077c478bd9Sstevel@tonic-gate 	char   *dupl_p = "Only one occurrence of -%c allowed";
64087c478bd9Sstevel@tonic-gate 	int option;
64097c478bd9Sstevel@tonic-gate 	int blk_cnt, blk_cnt_max;
64107c478bd9Sstevel@tonic-gate 	struct rlimit rlim;
64117c478bd9Sstevel@tonic-gate 
64127c478bd9Sstevel@tonic-gate 	/* Remember the native page size. */
64137c478bd9Sstevel@tonic-gate 
64147c478bd9Sstevel@tonic-gate 	PageSize = sysconf(_SC_PAGESIZE);
64157c478bd9Sstevel@tonic-gate 
64167c478bd9Sstevel@tonic-gate 	if (PageSize == -1) {
64177c478bd9Sstevel@tonic-gate 		/*
64187c478bd9Sstevel@tonic-gate 		 * This sysconf call will almost certainly never fail.  The
64197c478bd9Sstevel@tonic-gate 		 * symbol PAGESIZE itself resolves to the above sysconf call,
64207c478bd9Sstevel@tonic-gate 		 * so we should go ahead and define our own constant.
64217c478bd9Sstevel@tonic-gate 		 */
64227c478bd9Sstevel@tonic-gate 		PageSize = 8192;
64237c478bd9Sstevel@tonic-gate 	}
64247c478bd9Sstevel@tonic-gate 
64257c478bd9Sstevel@tonic-gate 	Hdr_type = BIN;
64267c478bd9Sstevel@tonic-gate 	Max_offset = (off_t)(BIN_OFFSET_MAX);
64277c478bd9Sstevel@tonic-gate 	Efil_p = Hdr_p = Own_p = IOfil_p = NULL;
64287c478bd9Sstevel@tonic-gate 	while ((option = getopt(largc, largv, opts_p)) != EOF) {
64297c478bd9Sstevel@tonic-gate 		switch (option) {
64307c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
64317c478bd9Sstevel@tonic-gate 		case 'z':
64327c478bd9Sstevel@tonic-gate 			/* rendezvous with the debugger */
64337c478bd9Sstevel@tonic-gate 			waitaround = 1;
64347c478bd9Sstevel@tonic-gate 			break;
64357c478bd9Sstevel@tonic-gate #endif
64367c478bd9Sstevel@tonic-gate 		case 'a':	/* reset access time */
64377c478bd9Sstevel@tonic-gate 			Args |= OCa;
64387c478bd9Sstevel@tonic-gate 			break;
64397c478bd9Sstevel@tonic-gate 		case 'b':	/* swap bytes and halfwords */
64407c478bd9Sstevel@tonic-gate 			Args |= OCb;
64417c478bd9Sstevel@tonic-gate 			break;
64427c478bd9Sstevel@tonic-gate 		case 'c':	/* select character header */
64437c478bd9Sstevel@tonic-gate 			Args |= OCc;
64447c478bd9Sstevel@tonic-gate 			Hdr_type = ASC;
64457c478bd9Sstevel@tonic-gate 			Max_namesz = APATH;
64467c478bd9Sstevel@tonic-gate 			Onecopy = 1;
64477c478bd9Sstevel@tonic-gate 			break;
64487c478bd9Sstevel@tonic-gate 		case 'd':	/* create directories as needed */
64497c478bd9Sstevel@tonic-gate 			Args |= OCd;
64507c478bd9Sstevel@tonic-gate 			break;
64517c478bd9Sstevel@tonic-gate 		case 'f':	/* select files not in patterns */
64527c478bd9Sstevel@tonic-gate 			Args |= OCf;
64537c478bd9Sstevel@tonic-gate 			break;
64547c478bd9Sstevel@tonic-gate 		case 'i':	/* "copy in" */
64557c478bd9Sstevel@tonic-gate 			Args |= OCi;
64567c478bd9Sstevel@tonic-gate 			Archive = 0;
64577c478bd9Sstevel@tonic-gate 			break;
64587c478bd9Sstevel@tonic-gate 		case 'k':	/* retry after I/O errors */
64597c478bd9Sstevel@tonic-gate 			Args |= OCk;
64607c478bd9Sstevel@tonic-gate 			break;
64617c478bd9Sstevel@tonic-gate 		case 'l':	/* link files when possible */
64627c478bd9Sstevel@tonic-gate 			Args |= OCl;
64637c478bd9Sstevel@tonic-gate 			break;
64647c478bd9Sstevel@tonic-gate 		case 'm':	/* retain modification time */
64657c478bd9Sstevel@tonic-gate 			Args |= OCm;
64667c478bd9Sstevel@tonic-gate 			break;
64677c478bd9Sstevel@tonic-gate 		case 'o':	/* "copy out" */
64687c478bd9Sstevel@tonic-gate 			Args |= OCo;
64697c478bd9Sstevel@tonic-gate 			Archive = 1;
64707c478bd9Sstevel@tonic-gate 			break;
64717c478bd9Sstevel@tonic-gate 		case 'p':	/* "pass" */
64727c478bd9Sstevel@tonic-gate 			Max_namesz = APATH;
64737c478bd9Sstevel@tonic-gate 			Args |= OCp;
64747c478bd9Sstevel@tonic-gate 			break;
6475*b0ee9efaSGary Mills 		case 'q':	/* "quiet" */
6476*b0ee9efaSGary Mills 			Args |= OCq;
6477*b0ee9efaSGary Mills 			break;
64787c478bd9Sstevel@tonic-gate 		case 'r':	/* rename files interactively */
64797c478bd9Sstevel@tonic-gate 			Args |= OCr;
64807c478bd9Sstevel@tonic-gate 			break;
64817c478bd9Sstevel@tonic-gate 		case 's':	/* swap bytes */
64827c478bd9Sstevel@tonic-gate 			Args |= OCs;
64837c478bd9Sstevel@tonic-gate 			break;
64847c478bd9Sstevel@tonic-gate 		case 't':	/* table of contents */
64857c478bd9Sstevel@tonic-gate 			Args |= OCt;
64867c478bd9Sstevel@tonic-gate 			break;
64877c478bd9Sstevel@tonic-gate 		case 'u':	/* copy unconditionally */
64887c478bd9Sstevel@tonic-gate 			Args |= OCu;
64897c478bd9Sstevel@tonic-gate 			break;
64907c478bd9Sstevel@tonic-gate 		case 'v':	/* verbose - print file names */
64917c478bd9Sstevel@tonic-gate 			Args |= OCv;
64927c478bd9Sstevel@tonic-gate 			break;
64937c478bd9Sstevel@tonic-gate 		case 'A':	/* append to existing archive */
64947c478bd9Sstevel@tonic-gate 			Args |= OCA;
64957c478bd9Sstevel@tonic-gate 			break;
64967c478bd9Sstevel@tonic-gate 		case 'B':	/* set block size to 5120 bytes */
64977c478bd9Sstevel@tonic-gate 			Args |= OCB;
64987c478bd9Sstevel@tonic-gate 			Bufsize = 5120;
64997c478bd9Sstevel@tonic-gate 			break;
65007c478bd9Sstevel@tonic-gate 		case 'C':	/* set arbitrary block size */
65017c478bd9Sstevel@tonic-gate 			if (Args & OCC)
65027c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'C');
65037c478bd9Sstevel@tonic-gate 			else {
65047c478bd9Sstevel@tonic-gate 				Args |= OCC;
65057c478bd9Sstevel@tonic-gate 				Bufsize = atoi(optarg);
65067c478bd9Sstevel@tonic-gate 			}
65077c478bd9Sstevel@tonic-gate 			break;
65087c478bd9Sstevel@tonic-gate 		case 'D':
65097c478bd9Sstevel@tonic-gate 			Dflag = 1;
65107c478bd9Sstevel@tonic-gate 			break;
65117c478bd9Sstevel@tonic-gate 		case 'E':	/* alternate file for pattern input */
65127c478bd9Sstevel@tonic-gate 			if (Args & OCE)
65137c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'E');
65147c478bd9Sstevel@tonic-gate 			else {
65157c478bd9Sstevel@tonic-gate 				Args |= OCE;
65167c478bd9Sstevel@tonic-gate 				Efil_p = optarg;
65177c478bd9Sstevel@tonic-gate 			}
65187c478bd9Sstevel@tonic-gate 			break;
65197c478bd9Sstevel@tonic-gate 		case 'H':	/* select header type */
65207c478bd9Sstevel@tonic-gate 			if (Args & OCH)
65217c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'H');
65227c478bd9Sstevel@tonic-gate 			else {
65237c478bd9Sstevel@tonic-gate 				Args |= OCH;
65247c478bd9Sstevel@tonic-gate 				Hdr_p = optarg;
65257c478bd9Sstevel@tonic-gate 			}
65267c478bd9Sstevel@tonic-gate 			break;
65277c478bd9Sstevel@tonic-gate 		case 'I':	/* alternate file for archive input */
65287c478bd9Sstevel@tonic-gate 			if (Args & OCI)
65297c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'I');
65307c478bd9Sstevel@tonic-gate 			else {
65317c478bd9Sstevel@tonic-gate 				Args |= OCI;
65327c478bd9Sstevel@tonic-gate 				IOfil_p = optarg;
65337c478bd9Sstevel@tonic-gate 			}
65347c478bd9Sstevel@tonic-gate 			break;
65357c478bd9Sstevel@tonic-gate 		case 'L':	/* follow symbolic links */
65367c478bd9Sstevel@tonic-gate 			Args |= OCL;
65377c478bd9Sstevel@tonic-gate 			break;
65387c478bd9Sstevel@tonic-gate 		case 'M':	/* specify new end-of-media message */
65397c478bd9Sstevel@tonic-gate 			if (Args & OCM)
65407c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'M');
65417c478bd9Sstevel@tonic-gate 			else {
65427c478bd9Sstevel@tonic-gate 				Args |= OCM;
65437c478bd9Sstevel@tonic-gate 				Eom_p = optarg;
65447c478bd9Sstevel@tonic-gate 			}
65457c478bd9Sstevel@tonic-gate 			break;
65467c478bd9Sstevel@tonic-gate 		case 'O':	/* alternate file for archive output */
65477c478bd9Sstevel@tonic-gate 			if (Args & OCO)
65487c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'O');
65497c478bd9Sstevel@tonic-gate 			else {
65507c478bd9Sstevel@tonic-gate 				Args |= OCO;
65517c478bd9Sstevel@tonic-gate 				IOfil_p = optarg;
65527c478bd9Sstevel@tonic-gate 			}
65537c478bd9Sstevel@tonic-gate 			break;
65547c478bd9Sstevel@tonic-gate 		case 'P':	/* preserve acls */
65557c478bd9Sstevel@tonic-gate 			Args |= OCP;
65567c478bd9Sstevel@tonic-gate 			Pflag++;
65577c478bd9Sstevel@tonic-gate 			break;
65587c478bd9Sstevel@tonic-gate 		case 'R':	/* change owner/group of files */
65597c478bd9Sstevel@tonic-gate 			if (Args & OCR)
65607c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'R');
65617c478bd9Sstevel@tonic-gate 			else {
65627c478bd9Sstevel@tonic-gate 				Args |= OCR;
65637c478bd9Sstevel@tonic-gate 				Own_p = optarg;
65647c478bd9Sstevel@tonic-gate 			}
65657c478bd9Sstevel@tonic-gate 			break;
65667c478bd9Sstevel@tonic-gate 		case 'S':	/* swap halfwords */
65677c478bd9Sstevel@tonic-gate 			Args |= OCS;
65687c478bd9Sstevel@tonic-gate 			break;
65697c478bd9Sstevel@tonic-gate 		case 'V':	/* print a dot '.' for each file */
65707c478bd9Sstevel@tonic-gate 			Args |= OCV;
65717c478bd9Sstevel@tonic-gate 			break;
65727c478bd9Sstevel@tonic-gate 		case '6':	/* for old, sixth-edition files */
65737c478bd9Sstevel@tonic-gate 			Args |= OC6;
65747c478bd9Sstevel@tonic-gate 			Ftype = SIXTH;
65757c478bd9Sstevel@tonic-gate 			break;
65767c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
65777c478bd9Sstevel@tonic-gate 		case '@':
65787c478bd9Sstevel@tonic-gate 			Atflag++;
65797c478bd9Sstevel@tonic-gate 			break;
6580ced83f9bSceastha #if defined(_PC_SATTR_ENABLED)
6581ced83f9bSceastha 		case '/':
6582ced83f9bSceastha 			SysAtflag++;
6583ced83f9bSceastha 			break;
6584ced83f9bSceastha #endif	/* _PC_SATTR_ENABLED */
6585ced83f9bSceastha #endif	/* O_XATTR */
65867c478bd9Sstevel@tonic-gate 		default:
65877c478bd9Sstevel@tonic-gate 			Error_cnt++;
65887c478bd9Sstevel@tonic-gate 		} /* option */
65897c478bd9Sstevel@tonic-gate 	} /* (option = getopt(largc, largv, opts_p)) != EOF */
65907c478bd9Sstevel@tonic-gate 
65917c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
65927c478bd9Sstevel@tonic-gate 	if (waitaround) {
65937c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Rendezvous with cpio on pid"
65947c478bd9Sstevel@tonic-gate 		    " %d\n"), getpid());
65957c478bd9Sstevel@tonic-gate 
65967c478bd9Sstevel@tonic-gate 		while (waitaround) {
65977c478bd9Sstevel@tonic-gate 			(void) sleep(10);
65987c478bd9Sstevel@tonic-gate 		}
65997c478bd9Sstevel@tonic-gate 	}
66007c478bd9Sstevel@tonic-gate #endif
66017c478bd9Sstevel@tonic-gate 
66027c478bd9Sstevel@tonic-gate 	largc -= optind;
66037c478bd9Sstevel@tonic-gate 	largv += optind;
66047c478bd9Sstevel@tonic-gate 	ckopts(Args);
66057c478bd9Sstevel@tonic-gate 	if (!Error_cnt) {
66067c478bd9Sstevel@tonic-gate 		if (Args & OCr) {
66077c478bd9Sstevel@tonic-gate 			Renam_p = e_zalloc(E_EXIT, APATH + 1);
66087c478bd9Sstevel@tonic-gate 			Renametmp_p = e_zalloc(E_EXIT, APATH + 1);
6609ced83f9bSceastha #if defined(_PC_SATTR_ENABLED)
6610ced83f9bSceastha 			Renam_attr_p = e_zalloc(E_EXIT, APATH + 1);
6611ced83f9bSceastha #endif
66127c478bd9Sstevel@tonic-gate 		}
66137c478bd9Sstevel@tonic-gate 		Symlnk_p = e_zalloc(E_EXIT, APATH);
66147c478bd9Sstevel@tonic-gate 		Over_p = e_zalloc(E_EXIT, APATH);
66157c478bd9Sstevel@tonic-gate 		Nam_p = e_zalloc(E_EXIT, APATH + 1);
66167c478bd9Sstevel@tonic-gate 		if (Args & OCp) {
66177c478bd9Sstevel@tonic-gate 			Savenam_p = e_zalloc(E_EXIT, APATH + 1);
66187c478bd9Sstevel@tonic-gate 		}
66197c478bd9Sstevel@tonic-gate 		Fullnam_p = e_zalloc(E_EXIT, APATH);
66207c478bd9Sstevel@tonic-gate 		Lnknam_p = e_zalloc(E_EXIT, APATH);
66217c478bd9Sstevel@tonic-gate 		Gen.g_nam_p = Nam_p;
6622ced83f9bSceastha 		if ((Fullnam_p = getcwd(NULL, APATH)) == NULL)
66237c478bd9Sstevel@tonic-gate 			msg(EXT, "Unable to determine current directory.");
66247c478bd9Sstevel@tonic-gate 		if (Args & OCi) {
66257c478bd9Sstevel@tonic-gate 			if (largc > 0) /* save patterns for -i option, if any */
66267c478bd9Sstevel@tonic-gate 				Pat_pp = largv;
66277c478bd9Sstevel@tonic-gate 			if (Args & OCE)
66287c478bd9Sstevel@tonic-gate 				getpats(largc, largv);
66297c478bd9Sstevel@tonic-gate 		} else if (Args & OCo) {
66307c478bd9Sstevel@tonic-gate 			if (largc != 0) /* error if arguments left with -o */
66317c478bd9Sstevel@tonic-gate 				Error_cnt++;
66327c478bd9Sstevel@tonic-gate 			else if (fstat(Archive, &ArchSt) < 0)
66337c478bd9Sstevel@tonic-gate 				msg(ERRN, "Error during stat() of archive");
66347c478bd9Sstevel@tonic-gate 			switch (Hdr_type) {
66357c478bd9Sstevel@tonic-gate 			case BIN:
66367c478bd9Sstevel@tonic-gate 				Hdrsz = HDRSZ;
66377c478bd9Sstevel@tonic-gate 				Pad_val = HALFWD;
66387c478bd9Sstevel@tonic-gate 				break;
66397c478bd9Sstevel@tonic-gate 			case CHR:
66407c478bd9Sstevel@tonic-gate 				Hdrsz = CHRSZ;
66417c478bd9Sstevel@tonic-gate 				Pad_val = 0;
66427c478bd9Sstevel@tonic-gate 				Max_offset = (off_t)(CHAR_OFFSET_MAX);
66437c478bd9Sstevel@tonic-gate 				break;
66447c478bd9Sstevel@tonic-gate 			case ASC:
66457c478bd9Sstevel@tonic-gate 			case CRC:
66467c478bd9Sstevel@tonic-gate 				Hdrsz = ASCSZ;
66477c478bd9Sstevel@tonic-gate 				Pad_val = FULLWD;
6648944d83a0Schin 				Max_offset = (off_t)(ASC_OFFSET_MAX);
66497c478bd9Sstevel@tonic-gate 				break;
66507c478bd9Sstevel@tonic-gate 			case TAR:
66517c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
66527c478bd9Sstevel@tonic-gate 			case USTAR: /* TAR and USTAR */
66537c478bd9Sstevel@tonic-gate 				Hdrsz = TARSZ;
66547c478bd9Sstevel@tonic-gate 				Pad_val = FULLBK;
66557c478bd9Sstevel@tonic-gate 				Max_offset = (off_t)(CHAR_OFFSET_MAX);
66567c478bd9Sstevel@tonic-gate 				break;
66577c478bd9Sstevel@tonic-gate 			default:
66587c478bd9Sstevel@tonic-gate 				msg(EXT, "Impossible header type.");
66597c478bd9Sstevel@tonic-gate 			}
66607c478bd9Sstevel@tonic-gate 		} else { /* directory must be specified */
66617c478bd9Sstevel@tonic-gate 			if (largc != 1)
66627c478bd9Sstevel@tonic-gate 				Error_cnt++;
6663ed69b652Sjs198686 			else if (access(*largv, 2) < 0 && (errno != EACCES))
6664ed69b652Sjs198686 				/*
6665ed69b652Sjs198686 				 * EACCES is ignored here as it may occur
6666ed69b652Sjs198686 				 * when any directory component of the path
6667ed69b652Sjs198686 				 * does not have write permission, even though
6668ed69b652Sjs198686 				 * the destination subdirectory has write
6669ed69b652Sjs198686 				 * access. Writing to a read only directory
6670ed69b652Sjs198686 				 * is handled later, as in "copy in" mode.
6671ed69b652Sjs198686 				 */
66727c478bd9Sstevel@tonic-gate 				msg(ERRN,
66737c478bd9Sstevel@tonic-gate 				    "Error during access() of \"%s\"", *largv);
66747c478bd9Sstevel@tonic-gate 		}
66757c478bd9Sstevel@tonic-gate 	}
66767c478bd9Sstevel@tonic-gate 	if (Error_cnt)
66777c478bd9Sstevel@tonic-gate 		usage(); /* exits! */
66787c478bd9Sstevel@tonic-gate 	if (Args & (OCi | OCo)) {
66797c478bd9Sstevel@tonic-gate 		if (!Dflag) {
66807c478bd9Sstevel@tonic-gate 			if (Args & (OCB | OCC)) {
66817c478bd9Sstevel@tonic-gate 				if (g_init(&Device, &Archive) < 0)
66827c478bd9Sstevel@tonic-gate 					msg(EXTN,
66837c478bd9Sstevel@tonic-gate 					    "Error during initialization");
66847c478bd9Sstevel@tonic-gate 			} else {
66857c478bd9Sstevel@tonic-gate 				if ((Bufsize = g_init(&Device, &Archive)) < 0)
66867c478bd9Sstevel@tonic-gate 					msg(EXTN,
66877c478bd9Sstevel@tonic-gate 					    "Error during initialization");
66887c478bd9Sstevel@tonic-gate 			}
66897c478bd9Sstevel@tonic-gate 		}
66907c478bd9Sstevel@tonic-gate 
66917c478bd9Sstevel@tonic-gate 		blk_cnt_max = _20K / Bufsize;
66927c478bd9Sstevel@tonic-gate 		if (blk_cnt_max < MX_BUFS) {
66937c478bd9Sstevel@tonic-gate 			blk_cnt_max = MX_BUFS;
66947c478bd9Sstevel@tonic-gate 		}
66957c478bd9Sstevel@tonic-gate 
66967c478bd9Sstevel@tonic-gate 		Buffr.b_base_p = NULL;
66977c478bd9Sstevel@tonic-gate 
66987c478bd9Sstevel@tonic-gate 		for (blk_cnt = blk_cnt_max; blk_cnt > 1; blk_cnt--) {
66997c478bd9Sstevel@tonic-gate 			Buffr.b_size = (size_t)(Bufsize * blk_cnt);
67007c478bd9Sstevel@tonic-gate 			Buffr.b_base_p = e_valloc(E_NORMAL, Buffr.b_size);
67017c478bd9Sstevel@tonic-gate 			if (Buffr.b_base_p != NULL) {
67027c478bd9Sstevel@tonic-gate 				break;
67037c478bd9Sstevel@tonic-gate 			}
67047c478bd9Sstevel@tonic-gate 		}
67057c478bd9Sstevel@tonic-gate 		if (Buffr.b_base_p == NULL || Buffr.b_size < (2 * CPIOBSZ)) {
67067c478bd9Sstevel@tonic-gate 			msg(EXT, "Out of memory");
67077c478bd9Sstevel@tonic-gate 		}
67087c478bd9Sstevel@tonic-gate 
67097c478bd9Sstevel@tonic-gate 		Buffr.b_out_p = Buffr.b_in_p = Buffr.b_base_p;
67107c478bd9Sstevel@tonic-gate 		Buffr.b_cnt = 0L;
67117c478bd9Sstevel@tonic-gate 		Buffr.b_end_p = Buffr.b_base_p + Buffr.b_size;
67127c478bd9Sstevel@tonic-gate 	}
67137c478bd9Sstevel@tonic-gate 
67147c478bd9Sstevel@tonic-gate 	/*
67157c478bd9Sstevel@tonic-gate 	 * Now that Bufsize has stabilized, we can allocate our i/o buffer
67167c478bd9Sstevel@tonic-gate 	 */
67177c478bd9Sstevel@tonic-gate 	Buf_p = e_valloc(E_EXIT, Bufsize);
67187c478bd9Sstevel@tonic-gate 
67197c478bd9Sstevel@tonic-gate 	if (Args & OCp) { /* get destination directory */
67207c478bd9Sstevel@tonic-gate 		(void) strcpy(Fullnam_p, *largv);
67217c478bd9Sstevel@tonic-gate 		if (stat(Fullnam_p, &DesSt) < 0)
67227c478bd9Sstevel@tonic-gate 			msg(EXTN, "Error during stat() of \"%s\"", Fullnam_p);
67237c478bd9Sstevel@tonic-gate 		if ((DesSt.st_mode & Ftype) != S_IFDIR)
67247c478bd9Sstevel@tonic-gate 			msg(EXT, "\"%s\" is not a directory", Fullnam_p);
67257c478bd9Sstevel@tonic-gate 	}
67267c478bd9Sstevel@tonic-gate 	Full_p = Fullnam_p + strlen(Fullnam_p) - 1;
67277c478bd9Sstevel@tonic-gate 	if (*Full_p != '/') {
67287c478bd9Sstevel@tonic-gate 		Full_p++;
67297c478bd9Sstevel@tonic-gate 		*Full_p = '/';
67307c478bd9Sstevel@tonic-gate 	}
67317c478bd9Sstevel@tonic-gate 	Full_p++;
67327c478bd9Sstevel@tonic-gate 	*Full_p = '\0';
67337c478bd9Sstevel@tonic-gate 	(void) strcpy(Lnknam_p, Fullnam_p);
67347c478bd9Sstevel@tonic-gate 	Lnkend_p = Lnknam_p + strlen(Lnknam_p);
67357c478bd9Sstevel@tonic-gate 	(void) getrlimit(RLIMIT_FSIZE, &rlim);
67367c478bd9Sstevel@tonic-gate 	Max_filesz = (off_t)rlim.rlim_cur;
67377c478bd9Sstevel@tonic-gate 	Lnk_hd.L_nxt_p = Lnk_hd.L_bck_p = &Lnk_hd;
6738ced83f9bSceastha 	Lnk_hd.L_lnk_p = NULL;
67397c478bd9Sstevel@tonic-gate }
67407c478bd9Sstevel@tonic-gate 
67417c478bd9Sstevel@tonic-gate /*
67427c478bd9Sstevel@tonic-gate  * set_tym: Set the access and/or modification times for a file.
67437c478bd9Sstevel@tonic-gate  */
67447c478bd9Sstevel@tonic-gate 
67457c478bd9Sstevel@tonic-gate static void
67467c478bd9Sstevel@tonic-gate set_tym(int dirfd, char *nam_p, time_t atime, time_t mtime)
67477c478bd9Sstevel@tonic-gate {
67487c478bd9Sstevel@tonic-gate 	struct timeval times[2];
67497c478bd9Sstevel@tonic-gate 
67507c478bd9Sstevel@tonic-gate 	times[0].tv_sec = atime;
67517c478bd9Sstevel@tonic-gate 	times[0].tv_usec = 0;
67527c478bd9Sstevel@tonic-gate 	times[1].tv_sec = mtime;
67537c478bd9Sstevel@tonic-gate 	times[1].tv_usec = 0;
67547c478bd9Sstevel@tonic-gate 
67557c478bd9Sstevel@tonic-gate 	if (futimesat(dirfd, nam_p, times) < 0) {
67567c478bd9Sstevel@tonic-gate 		if (Args & OCa) {
67577c478bd9Sstevel@tonic-gate 			msg(ERRN,
67587c478bd9Sstevel@tonic-gate 			    "Unable to reset access time for \"%s%s%s\"",
6759ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? nam_p : Fullnam_p,
6760ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
6761ced83f9bSceastha 			    G_p->g_rw_sysattr ? gettext(" System Attribute ") :
6762ced83f9bSceastha 			    gettext(" Attribute "),
6763ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" : nam_p);
67647c478bd9Sstevel@tonic-gate 		} else {
67657c478bd9Sstevel@tonic-gate 			msg(ERRN,
67667c478bd9Sstevel@tonic-gate 			    "Unable to reset modification time for \"%s%s%s\"",
6767ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? nam_p : Fullnam_p,
6768ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
6769ced83f9bSceastha 			    G_p->g_rw_sysattr ? gettext(" System Attribute ") :
6770ced83f9bSceastha 			    gettext(" Attribute "),
6771ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" : nam_p);
67727c478bd9Sstevel@tonic-gate 		}
67737c478bd9Sstevel@tonic-gate 	}
67747c478bd9Sstevel@tonic-gate }
67757c478bd9Sstevel@tonic-gate 
67767c478bd9Sstevel@tonic-gate /*
67777c478bd9Sstevel@tonic-gate  * sigint:  Catch interrupts.  If an interrupt occurs during the extraction
67787c478bd9Sstevel@tonic-gate  * of a file from the archive with the -u option set, and the filename did
67797c478bd9Sstevel@tonic-gate  * exist, remove the current file and restore the original file.  Then exit.
67807c478bd9Sstevel@tonic-gate  */
67817c478bd9Sstevel@tonic-gate 
67827c478bd9Sstevel@tonic-gate /*ARGSUSED*/
67837c478bd9Sstevel@tonic-gate static void
67847c478bd9Sstevel@tonic-gate sigint(int sig)
67857c478bd9Sstevel@tonic-gate {
67867c478bd9Sstevel@tonic-gate 	char *nam_p;
67877c478bd9Sstevel@tonic-gate 
67887c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, SIG_IGN); /* block further signals */
67897c478bd9Sstevel@tonic-gate 	if (!Finished) {
67907c478bd9Sstevel@tonic-gate 		if (Args & OCi)
67917c478bd9Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
67927c478bd9Sstevel@tonic-gate 		else /* OCp */
67937c478bd9Sstevel@tonic-gate 			nam_p = Fullnam_p;
67947c478bd9Sstevel@tonic-gate 		if (*Over_p != '\0') { /* There is a temp file */
67957c478bd9Sstevel@tonic-gate 			if (unlink(nam_p) < 0) {
67967c478bd9Sstevel@tonic-gate 				msg(ERRN,
67977c478bd9Sstevel@tonic-gate 				    "Cannot remove incomplete \"%s\"", nam_p);
67987c478bd9Sstevel@tonic-gate 			}
67997c478bd9Sstevel@tonic-gate 			if (rename(Over_p, nam_p) < 0) {
68007c478bd9Sstevel@tonic-gate 				if (link(Over_p, nam_p) < 0) {
68017c478bd9Sstevel@tonic-gate 					msg(ERRN,
68027c478bd9Sstevel@tonic-gate 					    "Cannot recover original \"%s\"",
68037c478bd9Sstevel@tonic-gate 					    nam_p);
68047c478bd9Sstevel@tonic-gate 				}
68057c478bd9Sstevel@tonic-gate 				if (unlink(Over_p)) {
68067c478bd9Sstevel@tonic-gate 					msg(ERRN,
68077c478bd9Sstevel@tonic-gate 					    "Cannot remove temp file \"%s\"",
68087c478bd9Sstevel@tonic-gate 					    Over_p);
68097c478bd9Sstevel@tonic-gate 				}
68107c478bd9Sstevel@tonic-gate 			}
68117c478bd9Sstevel@tonic-gate 		} else if (unlink(nam_p))
68127c478bd9Sstevel@tonic-gate 			msg(ERRN,
68137c478bd9Sstevel@tonic-gate 			    "Cannot remove incomplete \"%s\"", nam_p);
68147c478bd9Sstevel@tonic-gate 			*Over_p = '\0';
68157c478bd9Sstevel@tonic-gate 	}
68167c478bd9Sstevel@tonic-gate 	exit(EXIT_CODE);
68177c478bd9Sstevel@tonic-gate }
68187c478bd9Sstevel@tonic-gate 
68197c478bd9Sstevel@tonic-gate /*
68207c478bd9Sstevel@tonic-gate  * swap: Swap bytes (-s), halfwords (-S) or or both halfwords and bytes (-b).
68217c478bd9Sstevel@tonic-gate  */
68227c478bd9Sstevel@tonic-gate 
68237c478bd9Sstevel@tonic-gate static void
68247c478bd9Sstevel@tonic-gate swap(char *buf_p, int cnt)
68257c478bd9Sstevel@tonic-gate {
68267c478bd9Sstevel@tonic-gate 	unsigned char tbyte;
68277c478bd9Sstevel@tonic-gate 	int tcnt;
68287c478bd9Sstevel@tonic-gate 	int rcnt;
68297c478bd9Sstevel@tonic-gate 	ushort_t thalf;
68307c478bd9Sstevel@tonic-gate 
68317c478bd9Sstevel@tonic-gate 	rcnt = cnt % 4;
68327c478bd9Sstevel@tonic-gate 	cnt /= 4;
68337c478bd9Sstevel@tonic-gate 	if (Args & (OCb | OCs | BSM)) {
68347c478bd9Sstevel@tonic-gate 		tcnt = cnt;
68357c478bd9Sstevel@tonic-gate 		/* LINTED alignment */
68367c478bd9Sstevel@tonic-gate 		Swp_p = (union swpbuf *)buf_p;
68377c478bd9Sstevel@tonic-gate 		while (tcnt-- > 0) {
68387c478bd9Sstevel@tonic-gate 			tbyte = Swp_p->s_byte[0];
68397c478bd9Sstevel@tonic-gate 			Swp_p->s_byte[0] = Swp_p->s_byte[1];
68407c478bd9Sstevel@tonic-gate 			Swp_p->s_byte[1] = tbyte;
68417c478bd9Sstevel@tonic-gate 			tbyte = Swp_p->s_byte[2];
68427c478bd9Sstevel@tonic-gate 			Swp_p->s_byte[2] = Swp_p->s_byte[3];
68437c478bd9Sstevel@tonic-gate 			Swp_p->s_byte[3] = tbyte;
68447c478bd9Sstevel@tonic-gate 			Swp_p++;
68457c478bd9Sstevel@tonic-gate 		}
68467c478bd9Sstevel@tonic-gate 		if (rcnt >= 2) {
68477c478bd9Sstevel@tonic-gate 		tbyte = Swp_p->s_byte[0];
68487c478bd9Sstevel@tonic-gate 		Swp_p->s_byte[0] = Swp_p->s_byte[1];
68497c478bd9Sstevel@tonic-gate 		Swp_p->s_byte[1] = tbyte;
68507c478bd9Sstevel@tonic-gate 		tbyte = Swp_p->s_byte[2];
68517c478bd9Sstevel@tonic-gate 		}
68527c478bd9Sstevel@tonic-gate 	}
68537c478bd9Sstevel@tonic-gate 	if (Args & (OCb | OCS)) {
68547c478bd9Sstevel@tonic-gate 		tcnt = cnt;
68557c478bd9Sstevel@tonic-gate 		/* LINTED alignment */
68567c478bd9Sstevel@tonic-gate 		Swp_p = (union swpbuf *)buf_p;
68577c478bd9Sstevel@tonic-gate 		while (tcnt-- > 0) {
68587c478bd9Sstevel@tonic-gate 			thalf = Swp_p->s_half[0];
68597c478bd9Sstevel@tonic-gate 			Swp_p->s_half[0] = Swp_p->s_half[1];
68607c478bd9Sstevel@tonic-gate 			Swp_p->s_half[1] = thalf;
68617c478bd9Sstevel@tonic-gate 			Swp_p++;
68627c478bd9Sstevel@tonic-gate 		}
68637c478bd9Sstevel@tonic-gate 	}
68647c478bd9Sstevel@tonic-gate }
68657c478bd9Sstevel@tonic-gate 
68667c478bd9Sstevel@tonic-gate /*
68677c478bd9Sstevel@tonic-gate  * usage: Print the usage message on stderr and exit.
68687c478bd9Sstevel@tonic-gate  */
68697c478bd9Sstevel@tonic-gate 
68707c478bd9Sstevel@tonic-gate static void
68717c478bd9Sstevel@tonic-gate usage(void)
68727c478bd9Sstevel@tonic-gate {
68737c478bd9Sstevel@tonic-gate 
68747c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
68757c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
68767c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("USAGE:\n"
6877*b0ee9efaSGary Mills 	    "\tcpio -i[bcdfkmqrstuv@BSV6] [-C size] "
68787c478bd9Sstevel@tonic-gate 	    "[-E file] [-H hdr] [-I file [-M msg]] "
68797c478bd9Sstevel@tonic-gate 	    "[-R id] [patterns]\n"
68807c478bd9Sstevel@tonic-gate 	    "\tcpio -o[acv@ABLV] [-C size] "
68817c478bd9Sstevel@tonic-gate 	    "[-H hdr] [-O file [-M msg]]\n"
68827c478bd9Sstevel@tonic-gate 	    "\tcpio -p[adlmuv@LV] [-R id] directory\n"));
68837c478bd9Sstevel@tonic-gate #else
68847c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("USAGE:\n"
6885*b0ee9efaSGary Mills 	    "\tcpio -i[bcdfkmqrstuvBSV6] [-C size] "
68867c478bd9Sstevel@tonic-gate 	    "[-E file] [-H hdr] [-I file [-M msg]] "
68877c478bd9Sstevel@tonic-gate 	    "[-R id] [patterns]\n"
68887c478bd9Sstevel@tonic-gate 	    "\tcpio -o[acvABLV] [-C size] "
68897c478bd9Sstevel@tonic-gate 	    "[-H hdr] [-O file [-M msg]]\n"
68907c478bd9Sstevel@tonic-gate 	    "\tcpio -p[adlmuvLV] [-R id] directory\n"));
68917c478bd9Sstevel@tonic-gate #endif
68927c478bd9Sstevel@tonic-gate 	(void) fflush(stderr);
68937c478bd9Sstevel@tonic-gate 	exit(EXIT_CODE);
68947c478bd9Sstevel@tonic-gate }
68957c478bd9Sstevel@tonic-gate 
68967c478bd9Sstevel@tonic-gate /*
68977c478bd9Sstevel@tonic-gate  * verbose: For each file, print either the filename (-v) or a dot (-V).
68987c478bd9Sstevel@tonic-gate  * If the -t option (table of contents) is set, print either the filename,
68997c478bd9Sstevel@tonic-gate  * or if the -v option is also set, print an "ls -l"-like listing.
69007c478bd9Sstevel@tonic-gate  */
69017c478bd9Sstevel@tonic-gate 
69027c478bd9Sstevel@tonic-gate static void
69037c478bd9Sstevel@tonic-gate verbose(char *nam_p)
69047c478bd9Sstevel@tonic-gate {
69057c478bd9Sstevel@tonic-gate 	int i, j, temp;
69067c478bd9Sstevel@tonic-gate 	mode_t mode;
69077c478bd9Sstevel@tonic-gate 	char modestr[12];
69085fbb8099SNobutomo Nakano 	time_t	ttime;
69097c478bd9Sstevel@tonic-gate 
69107c478bd9Sstevel@tonic-gate 	/*
69117c478bd9Sstevel@tonic-gate 	 * The printf format and associated arguments to print the current
69127c478bd9Sstevel@tonic-gate 	 * filename.  Normally, just nam_p.  If we're processing an extended
69137c478bd9Sstevel@tonic-gate 	 * attribute, these are overridden.
69147c478bd9Sstevel@tonic-gate 	 */
69157c478bd9Sstevel@tonic-gate 	char *name_fmt = "%s";
69167c478bd9Sstevel@tonic-gate 	const char *name = nam_p;
69177c478bd9Sstevel@tonic-gate 	const char *attribute = NULL;
69187c478bd9Sstevel@tonic-gate 
6919ced83f9bSceastha 	if (Gen.g_attrnam_p != NULL) {
69207c478bd9Sstevel@tonic-gate 		/*
69217c478bd9Sstevel@tonic-gate 		 * Translation note:
69227c478bd9Sstevel@tonic-gate 		 * 'attribute' is a noun.
69237c478bd9Sstevel@tonic-gate 		 */
6924da6c28aaSamw 
6925ced83f9bSceastha 		if (Gen.g_rw_sysattr) {
6926ced83f9bSceastha 			name_fmt = gettext("%s system attribute %s");
6927ced83f9bSceastha 		} else if ((Args & OCt) &&
6928ced83f9bSceastha 		    (is_sysattr(basename(Gen.g_attrnam_p)))) {
6929da6c28aaSamw 			name_fmt = gettext("%s system attribute %s");
6930da6c28aaSamw 		} else {
69317c478bd9Sstevel@tonic-gate 			name_fmt = gettext("%s attribute %s");
6932da6c28aaSamw 		}
69337c478bd9Sstevel@tonic-gate 
69347c478bd9Sstevel@tonic-gate 		name = (Args & OCp) ? nam_p : Gen.g_attrfnam_p;
6935ced83f9bSceastha 		if (Gen.g_attrparent_p == NULL) {
69367c478bd9Sstevel@tonic-gate 			attribute = Gen.g_attrnam_p;
6937ced83f9bSceastha 		} else {
6938ced83f9bSceastha 			attribute = Gen.g_attrpath_p;
6939ced83f9bSceastha 		}
69407c478bd9Sstevel@tonic-gate 	}
69417c478bd9Sstevel@tonic-gate 
69427c478bd9Sstevel@tonic-gate 	if ((Gen.g_mode == SECMODE) || ((Hdr_type == USTAR ||
69437c478bd9Sstevel@tonic-gate 	    Hdr_type == TAR) && Thdr_p->tbuf.t_typeflag == 'A')) {
69447c478bd9Sstevel@tonic-gate 		/* dont print ancillary file */
69457c478bd9Sstevel@tonic-gate 		aclchar = '+';
69467c478bd9Sstevel@tonic-gate 		return;
69477c478bd9Sstevel@tonic-gate 	}
69487c478bd9Sstevel@tonic-gate 	for (i = 0; i < 11; i++)
69497c478bd9Sstevel@tonic-gate 		modestr[i] = '-';
69507c478bd9Sstevel@tonic-gate 	modestr[i] = '\0';
69517c478bd9Sstevel@tonic-gate 	modestr[i-1] = aclchar;
69527c478bd9Sstevel@tonic-gate 	aclchar = ' ';
69537c478bd9Sstevel@tonic-gate 
69547c478bd9Sstevel@tonic-gate 	if ((Args & OCt) && (Args & OCv)) {
69557c478bd9Sstevel@tonic-gate 		mode = Gen.g_mode;
69567c478bd9Sstevel@tonic-gate 		for (i = 0; i < 3; i++) {
69577c478bd9Sstevel@tonic-gate 			temp = (mode >> (6 - (i * 3)));
69587c478bd9Sstevel@tonic-gate 			j = (i * 3) + 1;
69597c478bd9Sstevel@tonic-gate 			if (S_IROTH & temp)
69607c478bd9Sstevel@tonic-gate 				modestr[j] = 'r';
69617c478bd9Sstevel@tonic-gate 			if (S_IWOTH & temp)
69627c478bd9Sstevel@tonic-gate 				modestr[j + 1] = 'w';
69637c478bd9Sstevel@tonic-gate 			if (S_IXOTH & temp)
69647c478bd9Sstevel@tonic-gate 				modestr[j + 2] = 'x';
69657c478bd9Sstevel@tonic-gate 		}
69667c478bd9Sstevel@tonic-gate 
69677c478bd9Sstevel@tonic-gate 		if (Hdr_type != BAR) {
69687c478bd9Sstevel@tonic-gate 			temp = Gen.g_mode & Ftype;
69697c478bd9Sstevel@tonic-gate 			switch (temp) {
69707c478bd9Sstevel@tonic-gate 			case (S_IFIFO):
69717c478bd9Sstevel@tonic-gate 				modestr[0] = 'p';
69727c478bd9Sstevel@tonic-gate 				break;
6973ced83f9bSceastha 			case (S_IFSOCK):
6974ced83f9bSceastha 				modestr[0] = 's';
6975ced83f9bSceastha 				break;
69767c478bd9Sstevel@tonic-gate 			case (S_IFCHR):
69777c478bd9Sstevel@tonic-gate 				modestr[0] = 'c';
69787c478bd9Sstevel@tonic-gate 				break;
69797c478bd9Sstevel@tonic-gate 			case (S_IFDIR):
69807c478bd9Sstevel@tonic-gate 				modestr[0] = 'd';
69817c478bd9Sstevel@tonic-gate 				break;
69827c478bd9Sstevel@tonic-gate 			case (S_IFBLK):
69837c478bd9Sstevel@tonic-gate 				modestr[0] = 'b';
69847c478bd9Sstevel@tonic-gate 				break;
69857c478bd9Sstevel@tonic-gate 			case (S_IFREG): /* was initialized to '-' */
69867c478bd9Sstevel@tonic-gate 				break;
69877c478bd9Sstevel@tonic-gate 			case (S_IFLNK):
69887c478bd9Sstevel@tonic-gate 				modestr[0] = 'l';
69897c478bd9Sstevel@tonic-gate 				break;
69907c478bd9Sstevel@tonic-gate 			default:
69917c478bd9Sstevel@tonic-gate 				msg(ERR, "Impossible file type");
69927c478bd9Sstevel@tonic-gate 			}
69937c478bd9Sstevel@tonic-gate 		} else {		/* bar */
69947c478bd9Sstevel@tonic-gate 			temp = Gen.g_mode & Ftype;
69957c478bd9Sstevel@tonic-gate 			switch (temp) {
69967c478bd9Sstevel@tonic-gate 			case (S_IFIFO):
69977c478bd9Sstevel@tonic-gate 				modestr[0] = 'p';
69987c478bd9Sstevel@tonic-gate 				break;
6999ced83f9bSceastha 			case (S_IFSOCK):
7000ced83f9bSceastha 				modestr[0] = 's';
7001ced83f9bSceastha 				break;
70027c478bd9Sstevel@tonic-gate 			case (S_IFCHR):
70037c478bd9Sstevel@tonic-gate 				modestr[0] = 'c';
70047c478bd9Sstevel@tonic-gate 				break;
70057c478bd9Sstevel@tonic-gate 			case (S_IFDIR):
70067c478bd9Sstevel@tonic-gate 				modestr[0] = 'd';
70077c478bd9Sstevel@tonic-gate 				break;
70087c478bd9Sstevel@tonic-gate 			case (S_IFBLK):
70097c478bd9Sstevel@tonic-gate 				modestr[0] = 'b';
70107c478bd9Sstevel@tonic-gate 				break;
70117c478bd9Sstevel@tonic-gate 			}
70127c478bd9Sstevel@tonic-gate 			if (bar_linkflag == SYMTYPE)
70137c478bd9Sstevel@tonic-gate 				modestr[0] = 'l';
70147c478bd9Sstevel@tonic-gate 		}
70157c478bd9Sstevel@tonic-gate 		if ((S_ISUID & Gen.g_mode) == S_ISUID)
70167c478bd9Sstevel@tonic-gate 			modestr[3] = 's';
70177c478bd9Sstevel@tonic-gate 		if ((S_ISVTX & Gen.g_mode) == S_ISVTX)
70187c478bd9Sstevel@tonic-gate 			modestr[9] = 't';
70197c478bd9Sstevel@tonic-gate 		if ((S_ISGID & G_p->g_mode) == S_ISGID && modestr[6] == 'x')
70207c478bd9Sstevel@tonic-gate 			modestr[6] = 's';
70217c478bd9Sstevel@tonic-gate 		else if ((S_ENFMT & Gen.g_mode) == S_ENFMT && modestr[6] != 'x')
70227c478bd9Sstevel@tonic-gate 			modestr[6] = 'l';
70237c478bd9Sstevel@tonic-gate 		if ((Hdr_type == TAR || Hdr_type == USTAR) && Gen.g_nlink == 0)
70247c478bd9Sstevel@tonic-gate 			(void) printf("%s%4d ", modestr, (int)Gen.g_nlink+1);
70257c478bd9Sstevel@tonic-gate 		else
70267c478bd9Sstevel@tonic-gate 			(void) printf("%s%4d ", modestr, (int)Gen.g_nlink);
70275fbb8099SNobutomo Nakano 		if (Lastuid == (uid_t)Gen.g_uid) {
70285fbb8099SNobutomo Nakano 			if (Lastuid == (uid_t)-1)
70297c478bd9Sstevel@tonic-gate 				(void) printf("-1       ");
70307c478bd9Sstevel@tonic-gate 			else
70317c478bd9Sstevel@tonic-gate 				(void) printf("%-9s", Curpw_p->pw_name);
70327c478bd9Sstevel@tonic-gate 		} else {
70337c478bd9Sstevel@tonic-gate 			if (Curpw_p = getpwuid((int)Gen.g_uid)) {
70347c478bd9Sstevel@tonic-gate 				(void) printf("%-9s", Curpw_p->pw_name);
70355fbb8099SNobutomo Nakano 				Lastuid = (uid_t)Gen.g_uid;
70367c478bd9Sstevel@tonic-gate 			} else {
70377c478bd9Sstevel@tonic-gate 				(void) printf("%-9d", (int)Gen.g_uid);
70385fbb8099SNobutomo Nakano 				Lastuid = (uid_t)-1;
70397c478bd9Sstevel@tonic-gate 			}
70407c478bd9Sstevel@tonic-gate 		}
70415fbb8099SNobutomo Nakano 		if (Lastgid == (gid_t)Gen.g_gid) {
70425fbb8099SNobutomo Nakano 			if (Lastgid == (gid_t)-1)
70437c478bd9Sstevel@tonic-gate 				(void) printf("-1       ");
70447c478bd9Sstevel@tonic-gate 			else
70457c478bd9Sstevel@tonic-gate 				(void) printf("%-9s", Curgr_p->gr_name);
70467c478bd9Sstevel@tonic-gate 		} else {
70477c478bd9Sstevel@tonic-gate 			if (Curgr_p = getgrgid((int)Gen.g_gid)) {
70487c478bd9Sstevel@tonic-gate 				(void) printf("%-9s", Curgr_p->gr_name);
70495fbb8099SNobutomo Nakano 				Lastgid = (gid_t)Gen.g_gid;
70507c478bd9Sstevel@tonic-gate 			} else {
70517c478bd9Sstevel@tonic-gate 				(void) printf("%-9d", (int)Gen.g_gid);
70525fbb8099SNobutomo Nakano 				Lastgid = (gid_t)-1;
70537c478bd9Sstevel@tonic-gate 			}
70547c478bd9Sstevel@tonic-gate 		}
70557c478bd9Sstevel@tonic-gate 
70567c478bd9Sstevel@tonic-gate 		/* print file size */
70577c478bd9Sstevel@tonic-gate 		if (!Aspec || ((Gen.g_mode & Ftype) == S_IFIFO) ||
7058ced83f9bSceastha 		    ((Gen.g_mode & Ftype) == S_IFSOCK) ||
70597c478bd9Sstevel@tonic-gate 		    (Hdr_type == BAR && bar_linkflag == SYMTYPE)) {
70605fbb8099SNobutomo Nakano 			off_t filesz = Gen.g_filesz;
70615fbb8099SNobutomo Nakano 
70625fbb8099SNobutomo Nakano 			if (S_ISSPARSE(Gen.g_mode) && Gen.g_holes != NULL)
70635fbb8099SNobutomo Nakano 				filesz = Gen.g_holes->orig_size;
70645fbb8099SNobutomo Nakano 
70655fbb8099SNobutomo Nakano 			if (filesz < (1LL << 31))
70665fbb8099SNobutomo Nakano 				(void) printf("%7lld ", (offset_t)filesz);
70677c478bd9Sstevel@tonic-gate 			else
70685fbb8099SNobutomo Nakano 				(void) printf("%11lld ", (offset_t)filesz);
70697c478bd9Sstevel@tonic-gate 		} else
70707c478bd9Sstevel@tonic-gate 			(void) printf("%3d,%3d ", (int)major(Gen.g_rdev),
70717c478bd9Sstevel@tonic-gate 			    (int)minor(Gen.g_rdev));
70725fbb8099SNobutomo Nakano 		ttime = Gen.g_mtime;
70735fbb8099SNobutomo Nakano 		(void) strftime(Time, sizeof (Time),
70745fbb8099SNobutomo Nakano 		    dcgettext(NULL, FORMAT, LC_TIME), localtime(&ttime));
70757c478bd9Sstevel@tonic-gate 		(void) printf("%s, ", Time);
70765fbb8099SNobutomo Nakano 		str_fprintf(stdout, name_fmt, name, attribute);
70777c478bd9Sstevel@tonic-gate 		if ((Gen.g_mode & Ftype) == S_IFLNK) {
70787c478bd9Sstevel@tonic-gate 			if (Hdr_type == USTAR || Hdr_type == TAR)
70797c478bd9Sstevel@tonic-gate 				(void) strcpy(Symlnk_p,
70807c478bd9Sstevel@tonic-gate 				    Thdr_p->tbuf.t_linkname);
70817c478bd9Sstevel@tonic-gate 			else {
708232fd2847Snakanon 				FILL(Gen.g_filesz);
70837c478bd9Sstevel@tonic-gate 				(void) strncpy(Symlnk_p, Buffr.b_out_p,
70847c478bd9Sstevel@tonic-gate 				    Gen.g_filesz);
70857c478bd9Sstevel@tonic-gate 				*(Symlnk_p + Gen.g_filesz) = '\0';
70867c478bd9Sstevel@tonic-gate 			}
70877c478bd9Sstevel@tonic-gate 			(void) printf(" -> %s", Symlnk_p);
70887c478bd9Sstevel@tonic-gate 		}
70897c478bd9Sstevel@tonic-gate 		if (Hdr_type == BAR) {
70907c478bd9Sstevel@tonic-gate 			if (bar_linkflag == SYMTYPE)
70917c478bd9Sstevel@tonic-gate 				(void) printf(gettext(" symbolic link to %s"),
70927c478bd9Sstevel@tonic-gate 				    bar_linkname);
70937c478bd9Sstevel@tonic-gate 			else if (bar_linkflag == '1')
70947c478bd9Sstevel@tonic-gate 				(void) printf(gettext(" linked to %s"),
70957c478bd9Sstevel@tonic-gate 				    bar_linkname);
70967c478bd9Sstevel@tonic-gate 		}
70977c478bd9Sstevel@tonic-gate 		if ((Hdr_type == USTAR || Hdr_type == TAR) &&
70987c478bd9Sstevel@tonic-gate 		    Thdr_p->tbuf.t_typeflag == '1') {
70997c478bd9Sstevel@tonic-gate 			(void) printf(gettext(" linked to %s%s%s"),
7100ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ?
71017c478bd9Sstevel@tonic-gate 			    Thdr_p->tbuf.t_linkname : Gen.g_attrfnam_p,
7102ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ? "" :
7103ced83f9bSceastha 			    gettext(" attribute "),
7104ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ?
71057c478bd9Sstevel@tonic-gate 			    "" : Gen.g_linktoattrnam_p);
71067c478bd9Sstevel@tonic-gate 		}
71077c478bd9Sstevel@tonic-gate 		(void) printf("\n");
71087c478bd9Sstevel@tonic-gate 	} else if ((Args & OCt) || (Args & OCv)) {
71095fbb8099SNobutomo Nakano 		str_fprintf(Out_p, name_fmt, name, attribute);
71107c478bd9Sstevel@tonic-gate 		(void) fputc('\n', Out_p);
71117c478bd9Sstevel@tonic-gate 	} else { /* OCV */
71127c478bd9Sstevel@tonic-gate 		(void) fputc('.', Out_p);
71137c478bd9Sstevel@tonic-gate 		if (Verbcnt++ >= 49) { /* start a new line of dots */
71147c478bd9Sstevel@tonic-gate 			Verbcnt = 0;
71157c478bd9Sstevel@tonic-gate 			(void) fputc('\n', Out_p);
71167c478bd9Sstevel@tonic-gate 		}
71177c478bd9Sstevel@tonic-gate 	}
71187c478bd9Sstevel@tonic-gate 	(void) fflush(Out_p);
71197c478bd9Sstevel@tonic-gate }
71207c478bd9Sstevel@tonic-gate 
71217c478bd9Sstevel@tonic-gate #define	MK_USHORT(a)	(a & 00000177777)
71227c478bd9Sstevel@tonic-gate 
71237c478bd9Sstevel@tonic-gate /*
71247c478bd9Sstevel@tonic-gate  * write_hdr: Transfer header information for the generic structure
71257c478bd9Sstevel@tonic-gate  * into the format for the selected header and bwrite() the header.
71267c478bd9Sstevel@tonic-gate  */
71277c478bd9Sstevel@tonic-gate 
71287c478bd9Sstevel@tonic-gate static void
71295fbb8099SNobutomo Nakano write_hdr(int arcflag, off_t len)
71307c478bd9Sstevel@tonic-gate {
71317c478bd9Sstevel@tonic-gate 	int cnt, pad;
71327c478bd9Sstevel@tonic-gate 	mode_t mode;
71337c478bd9Sstevel@tonic-gate 	uid_t uid;
71347c478bd9Sstevel@tonic-gate 	gid_t gid;
71357c478bd9Sstevel@tonic-gate 	const char warnfmt[] = "%s%s%s : %s";
71367c478bd9Sstevel@tonic-gate 
71375fbb8099SNobutomo Nakano 	switch (arcflag) {
71385fbb8099SNobutomo Nakano 	case ARCHIVE_ACL:
71397c478bd9Sstevel@tonic-gate 		mode = SECMODE;
71405fbb8099SNobutomo Nakano 		break;
71415fbb8099SNobutomo Nakano 
71425fbb8099SNobutomo Nakano 	case ARCHIVE_XATTR:
71435fbb8099SNobutomo Nakano 	case ARCHIVE_NORMAL:
71447c478bd9Sstevel@tonic-gate 		/*
71457c478bd9Sstevel@tonic-gate 		 * If attribute is being archived in cpio format then
71467c478bd9Sstevel@tonic-gate 		 * zap off the file type bits since those are truly a
71477c478bd9Sstevel@tonic-gate 		 * mask and reset them with _XATTR_CPIO_MODE
71487c478bd9Sstevel@tonic-gate 		 */
71497c478bd9Sstevel@tonic-gate 		/*
71507c478bd9Sstevel@tonic-gate 		 * len is the value of g_filesz for normal files
71517c478bd9Sstevel@tonic-gate 		 * and the length of the special header buffer in
71527c478bd9Sstevel@tonic-gate 		 * the case of acl and xattr headers.
71537c478bd9Sstevel@tonic-gate 		 */
7154ced83f9bSceastha 		if (G_p->g_attrnam_p != NULL && Hdr_type != USTAR &&
71557c478bd9Sstevel@tonic-gate 		    Hdr_type != TAR) {
7156d2443e76Smarks 			mode = (G_p->g_mode & POSIXMODES) | _XATTR_CPIO_MODE;
71577c478bd9Sstevel@tonic-gate 		} else {
71587c478bd9Sstevel@tonic-gate 			mode = G_p->g_mode;
71597c478bd9Sstevel@tonic-gate 		}
71605fbb8099SNobutomo Nakano 		if (arcflag != ARCHIVE_XATTR) {
71617c478bd9Sstevel@tonic-gate 			len = G_p->g_filesz;
71627c478bd9Sstevel@tonic-gate 		}
71635fbb8099SNobutomo Nakano 		break;
71645fbb8099SNobutomo Nakano 
71655fbb8099SNobutomo Nakano 	case ARCHIVE_SPARSE:
71665fbb8099SNobutomo Nakano 		mode = G_p->g_mode | C_ISSPARSE;
71675fbb8099SNobutomo Nakano 		len = G_p->g_filesz;
71685fbb8099SNobutomo Nakano 		break;
71697c478bd9Sstevel@tonic-gate 	}
71707c478bd9Sstevel@tonic-gate 
71717c478bd9Sstevel@tonic-gate 	uid = G_p->g_uid;
71727c478bd9Sstevel@tonic-gate 	gid = G_p->g_gid;
71737c478bd9Sstevel@tonic-gate 	/*
71747c478bd9Sstevel@tonic-gate 	 * Handle EFT uids and gids.  If they get too big
71757c478bd9Sstevel@tonic-gate 	 * to be represented in a particular format, force 'em to 'nobody'.
71767c478bd9Sstevel@tonic-gate 	 */
71777c478bd9Sstevel@tonic-gate 	switch (Hdr_type) {
71787c478bd9Sstevel@tonic-gate 	case BIN:			/* 16-bits of u_short */
71797c478bd9Sstevel@tonic-gate 		if ((ulong_t)uid > (ulong_t)USHRT_MAX)
71807c478bd9Sstevel@tonic-gate 			uid = UID_NOBODY;
71817c478bd9Sstevel@tonic-gate 		if ((ulong_t)gid > (ulong_t)USHRT_MAX)
71827c478bd9Sstevel@tonic-gate 			gid = GID_NOBODY;
71837c478bd9Sstevel@tonic-gate 		break;
71847c478bd9Sstevel@tonic-gate 	case CHR:			/* %.6lo => 262143 base 10 */
71857c478bd9Sstevel@tonic-gate 		if ((ulong_t)uid > (ulong_t)0777777)
71867c478bd9Sstevel@tonic-gate 			uid = UID_NOBODY;
71877c478bd9Sstevel@tonic-gate 		if ((ulong_t)gid > (ulong_t)0777777)
71887c478bd9Sstevel@tonic-gate 			gid = GID_NOBODY;
71897c478bd9Sstevel@tonic-gate 		break;
71907c478bd9Sstevel@tonic-gate 	case ASC:			/* %.8lx => full 32 bits */
71917c478bd9Sstevel@tonic-gate 	case CRC:
71927c478bd9Sstevel@tonic-gate 		break;
71937c478bd9Sstevel@tonic-gate 	case USTAR:
71947c478bd9Sstevel@tonic-gate 	case TAR:			/* %.7lo => 2097151 base 10 */
71957c478bd9Sstevel@tonic-gate 		if ((ulong_t)uid > (ulong_t)07777777)
71967c478bd9Sstevel@tonic-gate 			uid = UID_NOBODY;
71977c478bd9Sstevel@tonic-gate 		if ((ulong_t)gid > (ulong_t)07777777)
71987c478bd9Sstevel@tonic-gate 			gid = GID_NOBODY;
71997c478bd9Sstevel@tonic-gate 		break;
72007c478bd9Sstevel@tonic-gate 	default:
72017c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
72027c478bd9Sstevel@tonic-gate 	}
72037c478bd9Sstevel@tonic-gate 
72047c478bd9Sstevel@tonic-gate 	/*
72057c478bd9Sstevel@tonic-gate 	 * Since cpio formats -don't- encode the symbolic names, print
72067c478bd9Sstevel@tonic-gate 	 * a warning message when we map the uid or gid this way.
72077c478bd9Sstevel@tonic-gate 	 * Also, if the ownership just changed, clear set[ug]id bits
72087c478bd9Sstevel@tonic-gate 	 *
72097c478bd9Sstevel@tonic-gate 	 * (Except for USTAR format of course, where we have a string
72107c478bd9Sstevel@tonic-gate 	 * representation of the username embedded in the header)
72117c478bd9Sstevel@tonic-gate 	 */
72127c478bd9Sstevel@tonic-gate 	if (uid != G_p->g_uid && Hdr_type != USTAR) {
72137c478bd9Sstevel@tonic-gate 		msg(ERR, warnfmt,
7214ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ?
72157c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p : G_p->g_attrfnam_p,
7216ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? "" : G_p->g_rw_sysattr ?
7217ced83f9bSceastha 		    gettext(" System Attribute ") : gettext(" Attribute "),
7218ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? "" : G_p->g_attrnam_p,
72197c478bd9Sstevel@tonic-gate 		    gettext("uid too large for archive format"));
72207c478bd9Sstevel@tonic-gate 		if (S_ISREG(mode))
72217c478bd9Sstevel@tonic-gate 			mode &= ~S_ISUID;
72227c478bd9Sstevel@tonic-gate 	}
72237c478bd9Sstevel@tonic-gate 	if (gid != G_p->g_gid && Hdr_type != USTAR) {
72247c478bd9Sstevel@tonic-gate 		msg(ERR, warnfmt,
7225ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ?
72267c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p : G_p->g_attrfnam_p,
7227ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? "" : G_p->g_rw_sysattr ?
7228ced83f9bSceastha 		    gettext(" System Attribute ") : gettext(" Attribute "),
7229ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? "" : G_p->g_attrnam_p,
72307c478bd9Sstevel@tonic-gate 		    gettext("gid too large for archive format"));
72317c478bd9Sstevel@tonic-gate 		if (S_ISREG(mode))
72327c478bd9Sstevel@tonic-gate 			mode &= ~S_ISGID;
72337c478bd9Sstevel@tonic-gate 	}
72347c478bd9Sstevel@tonic-gate 
72357c478bd9Sstevel@tonic-gate 	switch (Hdr_type) {
72367c478bd9Sstevel@tonic-gate 	case BIN:
72377c478bd9Sstevel@tonic-gate 	case CHR:
72387c478bd9Sstevel@tonic-gate 	case ASC:
72397c478bd9Sstevel@tonic-gate 	case CRC:
72407c478bd9Sstevel@tonic-gate 		cnt = Hdrsz + G_p->g_namesz;
72417c478bd9Sstevel@tonic-gate 		break;
72427c478bd9Sstevel@tonic-gate 	case TAR:
72437c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
72447c478bd9Sstevel@tonic-gate 	case USTAR: /* TAR and USTAR */
72457c478bd9Sstevel@tonic-gate 		cnt = TARSZ;
72467c478bd9Sstevel@tonic-gate 		break;
72477c478bd9Sstevel@tonic-gate 	default:
72487c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
72497c478bd9Sstevel@tonic-gate 	}
72507c478bd9Sstevel@tonic-gate 	FLUSH(cnt);
72517c478bd9Sstevel@tonic-gate 
72527c478bd9Sstevel@tonic-gate 	switch (Hdr_type) {
72537c478bd9Sstevel@tonic-gate 	case BIN:
72547c478bd9Sstevel@tonic-gate 		Hdr.h_magic = (short)G_p->g_magic;
72557c478bd9Sstevel@tonic-gate 		Hdr.h_dev = G_p->g_dev;
72567c478bd9Sstevel@tonic-gate 		Hdr.h_ino = G_p->g_ino;
72577c478bd9Sstevel@tonic-gate 		Hdr.h_uid = uid;
72587c478bd9Sstevel@tonic-gate 		Hdr.h_gid = gid;
72597c478bd9Sstevel@tonic-gate 		Hdr.h_mode = mode;
72607c478bd9Sstevel@tonic-gate 		Hdr.h_nlink = G_p->g_nlink;
72617c478bd9Sstevel@tonic-gate 		Hdr.h_rdev = G_p->g_rdev;
72627c478bd9Sstevel@tonic-gate 		mkshort(Hdr.h_mtime, (long)G_p->g_mtime);
72637c478bd9Sstevel@tonic-gate 		Hdr.h_namesize = (short)G_p->g_namesz;
72647c478bd9Sstevel@tonic-gate 		mkshort(Hdr.h_filesize, (long)len);
72657c478bd9Sstevel@tonic-gate 		(void) strcpy(Hdr.h_name, G_p->g_nam_p);
72667c478bd9Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, &Hdr, cnt);
72677c478bd9Sstevel@tonic-gate 		break;
72687c478bd9Sstevel@tonic-gate 	case CHR:
72695fbb8099SNobutomo Nakano 		/*LINTED*/
72707c478bd9Sstevel@tonic-gate 		(void) sprintf(Buffr.b_in_p,
72717c478bd9Sstevel@tonic-gate 		    "%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.11lo%.6lo%."
72727c478bd9Sstevel@tonic-gate 		    "11llo%s", G_p->g_magic, G_p->g_dev, G_p->g_ino, mode,
72735fbb8099SNobutomo Nakano 		    (long)uid, (long)gid, G_p->g_nlink, MK_USHORT(G_p->g_rdev),
72747c478bd9Sstevel@tonic-gate 		    G_p->g_mtime, (long)G_p->g_namesz, (offset_t)len,
72757c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p);
72767c478bd9Sstevel@tonic-gate 		break;
72777c478bd9Sstevel@tonic-gate 	case ASC:
72787c478bd9Sstevel@tonic-gate 	case CRC:
72795fbb8099SNobutomo Nakano 		/*LINTED*/
72807c478bd9Sstevel@tonic-gate 		(void) sprintf(Buffr.b_in_p,
72817c478bd9Sstevel@tonic-gate 		    "%.6lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%."
72827c478bd9Sstevel@tonic-gate 		    "8lx%.8lx%.8lx%.8lx%s",
72837c478bd9Sstevel@tonic-gate 		    G_p->g_magic, G_p->g_ino, mode, G_p->g_uid,
7284944d83a0Schin 		    G_p->g_gid, G_p->g_nlink, G_p->g_mtime, (ulong_t)len,
72857c478bd9Sstevel@tonic-gate 		    major(G_p->g_dev), minor(G_p->g_dev),
72867c478bd9Sstevel@tonic-gate 		    major(G_p->g_rdev), minor(G_p->g_rdev),
72877c478bd9Sstevel@tonic-gate 		    G_p->g_namesz, G_p->g_cksum, G_p->g_nam_p);
72887c478bd9Sstevel@tonic-gate 		break;
72897c478bd9Sstevel@tonic-gate 	case USTAR:
72907c478bd9Sstevel@tonic-gate 		Thdr_p = (union tblock *)Buffr.b_in_p;
72915fbb8099SNobutomo Nakano 		(void) memset(Thdr_p, 0, TARSZ);
72927c478bd9Sstevel@tonic-gate 		(void) strncpy(Thdr_p->tbuf.t_name, G_p->g_tname,
72937c478bd9Sstevel@tonic-gate 		    (int)strlen(G_p->g_tname));
72947c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mode, "%07o", (int)mode);
72957c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_uid, "%07o", (int)uid);
72967c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_gid, "%07o", (int)gid);
72977c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_size, "%011llo",
72987c478bd9Sstevel@tonic-gate 		    (offset_t)len);
72997c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mtime, "%011lo", G_p->g_mtime);
73005fbb8099SNobutomo Nakano 		if (arcflag == ARCHIVE_ACL) {
73017c478bd9Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = 'A';	/* ACL file type */
73025fbb8099SNobutomo Nakano 		} else if (arcflag == ARCHIVE_XATTR ||
7303ced83f9bSceastha 		    (G_p->g_attrnam_p != NULL)) {
73047c478bd9Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = _XATTR_HDRTYPE;
73057c478bd9Sstevel@tonic-gate 		} else {
73067c478bd9Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = G_p->g_typeflag;
73077c478bd9Sstevel@tonic-gate 		}
73087c478bd9Sstevel@tonic-gate 		if (T_lname[0] != '\0') {
73097c478bd9Sstevel@tonic-gate 			/*
73107c478bd9Sstevel@tonic-gate 			 * if not a symbolic link
73117c478bd9Sstevel@tonic-gate 			 */
73127c478bd9Sstevel@tonic-gate 			if (((G_p->g_mode & Ftype) != S_IFLNK) &&
7313ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL)) {
73147c478bd9Sstevel@tonic-gate 				Thdr_p->tbuf.t_typeflag = LNKTYPE;
73157c478bd9Sstevel@tonic-gate 				(void) sprintf(Thdr_p->tbuf.t_size,
73167c478bd9Sstevel@tonic-gate 				    "%011lo", 0L);
73177c478bd9Sstevel@tonic-gate 			}
73187c478bd9Sstevel@tonic-gate 			(void) strncpy(Thdr_p->tbuf.t_linkname, T_lname,
73197c478bd9Sstevel@tonic-gate 			    strlen(T_lname));
73207c478bd9Sstevel@tonic-gate 		}
73215fbb8099SNobutomo Nakano 		(void) strcpy(Thdr_p->tbuf.t_magic, TMAGIC);
73225fbb8099SNobutomo Nakano 		(void) strcpy(Thdr_p->tbuf.t_version, TVERSION);
73235fbb8099SNobutomo Nakano 		(void) strcpy(Thdr_p->tbuf.t_uname, G_p->g_uname);
73245fbb8099SNobutomo Nakano 		(void) strcpy(Thdr_p->tbuf.t_gname, G_p->g_gname);
73257c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_devmajor, "%07o",
73267c478bd9Sstevel@tonic-gate 		    (int)major(G_p->g_rdev));
73277c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_devminor, "%07o",
73287c478bd9Sstevel@tonic-gate 		    (int)minor(G_p->g_rdev));
73297c478bd9Sstevel@tonic-gate 		if (Gen.g_prefix) {
73305fbb8099SNobutomo Nakano 			(void) strcpy(Thdr_p->tbuf.t_prefix, Gen.g_prefix);
73317c478bd9Sstevel@tonic-gate 			free(Gen.g_prefix);
73327c478bd9Sstevel@tonic-gate 			Gen.g_prefix = NULL;
73337c478bd9Sstevel@tonic-gate 		} else {
73345fbb8099SNobutomo Nakano 			Thdr_p->tbuf.t_prefix[0] = '\0';
73357c478bd9Sstevel@tonic-gate 		}
73367c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_cksum, "%07o",
73377c478bd9Sstevel@tonic-gate 		    (int)cksum(TARTYP, 0, NULL));
73387c478bd9Sstevel@tonic-gate 		break;
73397c478bd9Sstevel@tonic-gate 	case TAR:
73407c478bd9Sstevel@tonic-gate 		Thdr_p = (union tblock *)Buffr.b_in_p;
73415fbb8099SNobutomo Nakano 		(void) memset(Thdr_p, 0, TARSZ);
73427c478bd9Sstevel@tonic-gate 		(void) strncpy(Thdr_p->tbuf.t_name, G_p->g_nam_p,
73437c478bd9Sstevel@tonic-gate 		    G_p->g_namesz);
73447c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mode, "%07o ", (int)mode);
73457c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_uid, "%07o ", (int)uid);
73467c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_gid, "%07o ", (int)gid);
73477c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_size, "%011llo ",
73487c478bd9Sstevel@tonic-gate 		    (offset_t)len);
73497c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mtime, "%011o ",
73507c478bd9Sstevel@tonic-gate 		    (int)G_p->g_mtime);
73517c478bd9Sstevel@tonic-gate 		if (T_lname[0] != '\0') {
73527c478bd9Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = '1';
73537c478bd9Sstevel@tonic-gate 		} else {
73547c478bd9Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = '\0';
73557c478bd9Sstevel@tonic-gate 		}
73567c478bd9Sstevel@tonic-gate 		(void) strncpy(Thdr_p->tbuf.t_linkname, T_lname,
73577c478bd9Sstevel@tonic-gate 		    strlen(T_lname));
73587c478bd9Sstevel@tonic-gate 		break;
73597c478bd9Sstevel@tonic-gate 	default:
73607c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
73617c478bd9Sstevel@tonic-gate 	} /* Hdr_type */
73627c478bd9Sstevel@tonic-gate 
73637c478bd9Sstevel@tonic-gate 	Buffr.b_in_p += cnt;
73647c478bd9Sstevel@tonic-gate 	Buffr.b_cnt += cnt;
73657c478bd9Sstevel@tonic-gate 	pad = ((cnt + Pad_val) & ~Pad_val) - cnt;
73667c478bd9Sstevel@tonic-gate 	if (pad != 0) {
73677c478bd9Sstevel@tonic-gate 		FLUSH(pad);
73685fbb8099SNobutomo Nakano 		(void) memset(Buffr.b_in_p, 0, pad);
73697c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += pad;
73707c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += pad;
73717c478bd9Sstevel@tonic-gate 	}
73727c478bd9Sstevel@tonic-gate }
73737c478bd9Sstevel@tonic-gate 
73747c478bd9Sstevel@tonic-gate /*
73757c478bd9Sstevel@tonic-gate  * write_trail: Create the appropriate trailer for the selected header type
73767c478bd9Sstevel@tonic-gate  * and bwrite the trailer.  Pad the buffer with nulls out to the next Bufsize
73777c478bd9Sstevel@tonic-gate  * boundary, and force a write.  If the write completes, or if the trailer is
73787c478bd9Sstevel@tonic-gate  * completely written (but not all of the padding nulls (as can happen on end
73797c478bd9Sstevel@tonic-gate  * of medium)) return.  Otherwise, the trailer was not completely written out,
73807c478bd9Sstevel@tonic-gate  * so re-pad the buffer with nulls and try again.
73817c478bd9Sstevel@tonic-gate  */
73827c478bd9Sstevel@tonic-gate 
73837c478bd9Sstevel@tonic-gate static void
73847c478bd9Sstevel@tonic-gate write_trail(void)
73857c478bd9Sstevel@tonic-gate {
73867c478bd9Sstevel@tonic-gate 	int cnt, need;
73877c478bd9Sstevel@tonic-gate 
73887c478bd9Sstevel@tonic-gate 	switch (Hdr_type) {
73897c478bd9Sstevel@tonic-gate 	case BIN:
73907c478bd9Sstevel@tonic-gate 		Gen.g_magic = CMN_BIN;
73917c478bd9Sstevel@tonic-gate 		break;
73927c478bd9Sstevel@tonic-gate 	case CHR:
73937c478bd9Sstevel@tonic-gate 		Gen.g_magic = CMN_BIN;
73947c478bd9Sstevel@tonic-gate 		break;
73957c478bd9Sstevel@tonic-gate 	case ASC:
73967c478bd9Sstevel@tonic-gate 		Gen.g_magic = CMN_ASC;
73977c478bd9Sstevel@tonic-gate 		break;
73987c478bd9Sstevel@tonic-gate 	case CRC:
73997c478bd9Sstevel@tonic-gate 		Gen.g_magic = CMN_CRC;
74007c478bd9Sstevel@tonic-gate 		break;
74017c478bd9Sstevel@tonic-gate 	}
74027c478bd9Sstevel@tonic-gate 
74037c478bd9Sstevel@tonic-gate 	switch (Hdr_type) {
74047c478bd9Sstevel@tonic-gate 	case BIN:
74057c478bd9Sstevel@tonic-gate 	case CHR:
74067c478bd9Sstevel@tonic-gate 	case ASC:
74077c478bd9Sstevel@tonic-gate 	case CRC:
74087c478bd9Sstevel@tonic-gate 		Gen.g_mode = Gen.g_uid = Gen.g_gid = 0;
74097c478bd9Sstevel@tonic-gate 		Gen.g_nlink = 1;
74107c478bd9Sstevel@tonic-gate 		Gen.g_mtime = Gen.g_ino = Gen.g_dev = 0;
74117c478bd9Sstevel@tonic-gate 		Gen.g_rdev = Gen.g_cksum = 0;
74127c478bd9Sstevel@tonic-gate 		Gen.g_filesz = (off_t)0;
74137c478bd9Sstevel@tonic-gate 		Gen.g_namesz = strlen("TRAILER!!!") + 1;
74147c478bd9Sstevel@tonic-gate 		(void) strcpy(Gen.g_nam_p, "TRAILER!!!");
74157c478bd9Sstevel@tonic-gate 		G_p = &Gen;
74167c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
74177c478bd9Sstevel@tonic-gate 		break;
74187c478bd9Sstevel@tonic-gate 	case TAR:
74197c478bd9Sstevel@tonic-gate 	/*FALLTHROUGH*/
74207c478bd9Sstevel@tonic-gate 	case USTAR: /* TAR and USTAR */
74217c478bd9Sstevel@tonic-gate 		for (cnt = 0; cnt < 3; cnt++) {
74227c478bd9Sstevel@tonic-gate 			FLUSH(TARSZ);
74235fbb8099SNobutomo Nakano 			(void) memset(Buffr.b_in_p, 0, TARSZ);
74247c478bd9Sstevel@tonic-gate 			Buffr.b_in_p += TARSZ;
74257c478bd9Sstevel@tonic-gate 			Buffr.b_cnt += TARSZ;
74267c478bd9Sstevel@tonic-gate 		}
74277c478bd9Sstevel@tonic-gate 		break;
74287c478bd9Sstevel@tonic-gate 	default:
74297c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
74307c478bd9Sstevel@tonic-gate 	}
74317c478bd9Sstevel@tonic-gate 	need = Bufsize - (Buffr.b_cnt % Bufsize);
74327c478bd9Sstevel@tonic-gate 	if (need == Bufsize)
74337c478bd9Sstevel@tonic-gate 		need = 0;
74347c478bd9Sstevel@tonic-gate 
74357c478bd9Sstevel@tonic-gate 	while (Buffr.b_cnt > 0) {
74367c478bd9Sstevel@tonic-gate 		while (need > 0) {
74377c478bd9Sstevel@tonic-gate 			cnt = (need < TARSZ) ? need : TARSZ;
74387c478bd9Sstevel@tonic-gate 			need -= cnt;
74397c478bd9Sstevel@tonic-gate 			FLUSH(cnt);
74405fbb8099SNobutomo Nakano 			(void) memset(Buffr.b_in_p, 0, cnt);
74417c478bd9Sstevel@tonic-gate 			Buffr.b_in_p += cnt;
74427c478bd9Sstevel@tonic-gate 			Buffr.b_cnt += cnt;
74437c478bd9Sstevel@tonic-gate 		}
74447c478bd9Sstevel@tonic-gate 		bflush();
74457c478bd9Sstevel@tonic-gate 	}
74467c478bd9Sstevel@tonic-gate }
74477c478bd9Sstevel@tonic-gate 
74487c478bd9Sstevel@tonic-gate /*
74497c478bd9Sstevel@tonic-gate  * if archives in USTAR format, check if typeflag == '5' for directories
74507c478bd9Sstevel@tonic-gate  */
74517c478bd9Sstevel@tonic-gate static int
74527c478bd9Sstevel@tonic-gate ustar_dir(void)
74537c478bd9Sstevel@tonic-gate {
74547c478bd9Sstevel@tonic-gate 	if (Hdr_type == USTAR || Hdr_type == TAR) {
74557c478bd9Sstevel@tonic-gate 		if (Thdr_p->tbuf.t_typeflag == '5')
74567c478bd9Sstevel@tonic-gate 			return (1);
74577c478bd9Sstevel@tonic-gate 	}
74587c478bd9Sstevel@tonic-gate 	return (0);
74597c478bd9Sstevel@tonic-gate }
74607c478bd9Sstevel@tonic-gate 
74617c478bd9Sstevel@tonic-gate /*
74627c478bd9Sstevel@tonic-gate  * if archives in USTAR format, check if typeflag == '3' || '4' || '6'
74637c478bd9Sstevel@tonic-gate  * for character, block, fifo special files
74647c478bd9Sstevel@tonic-gate  */
74657c478bd9Sstevel@tonic-gate static int
74667c478bd9Sstevel@tonic-gate ustar_spec(void)
74677c478bd9Sstevel@tonic-gate {
74687c478bd9Sstevel@tonic-gate 	int typeflag;
74697c478bd9Sstevel@tonic-gate 
74707c478bd9Sstevel@tonic-gate 	if (Hdr_type == USTAR || Hdr_type == TAR) {
74717c478bd9Sstevel@tonic-gate 		typeflag = Thdr_p->tbuf.t_typeflag;
74727c478bd9Sstevel@tonic-gate 		if (typeflag == '3' || typeflag == '4' || typeflag == '6')
74737c478bd9Sstevel@tonic-gate 			return (1);
74747c478bd9Sstevel@tonic-gate 	}
74757c478bd9Sstevel@tonic-gate 	return (0);
74767c478bd9Sstevel@tonic-gate }
74777c478bd9Sstevel@tonic-gate 
74787c478bd9Sstevel@tonic-gate /*
74797c478bd9Sstevel@tonic-gate  * The return value is a pointer to a converted copy of the information in
74807c478bd9Sstevel@tonic-gate  * FromStat if the file is representable in -Hodc format, and NULL otherwise.
74817c478bd9Sstevel@tonic-gate  */
74827c478bd9Sstevel@tonic-gate 
74837c478bd9Sstevel@tonic-gate static struct stat *
74847c478bd9Sstevel@tonic-gate convert_to_old_stat(struct stat *FromStat, char *namep, char *attrp)
74857c478bd9Sstevel@tonic-gate {
74867c478bd9Sstevel@tonic-gate 	static struct stat ToSt;
74877c478bd9Sstevel@tonic-gate 	cpioinfo_t TmpSt;
74887c478bd9Sstevel@tonic-gate 
74897c478bd9Sstevel@tonic-gate 	(void) memset(&TmpSt, 0, sizeof (cpioinfo_t));
74907c478bd9Sstevel@tonic-gate 	stat_to_svr32_stat(&TmpSt, FromStat);
74917c478bd9Sstevel@tonic-gate 	(void) memset(&ToSt, 0, sizeof (ToSt));
74927c478bd9Sstevel@tonic-gate 
74937c478bd9Sstevel@tonic-gate 	if (TmpSt.st_rdev == (o_dev_t)NODEV &&
74947c478bd9Sstevel@tonic-gate 	    (((TmpSt.st_mode & Ftype) == S_IFCHR) ||
74957c478bd9Sstevel@tonic-gate 	    ((TmpSt.st_mode & Ftype) == S_IFBLK))) {
74967c478bd9Sstevel@tonic-gate 		/*
74977c478bd9Sstevel@tonic-gate 		 * Encountered a problem representing the rdev information.
74987c478bd9Sstevel@tonic-gate 		 * Don't archive it.
74997c478bd9Sstevel@tonic-gate 		 */
75007c478bd9Sstevel@tonic-gate 
75017c478bd9Sstevel@tonic-gate 		msg(ERR, "Error -Hodc format can't support expanded"
75027c478bd9Sstevel@tonic-gate 		    "types on %s%s%s",
75037c478bd9Sstevel@tonic-gate 		    namep,
75047c478bd9Sstevel@tonic-gate 		    (attrp == NULL) ? "" : gettext(" Attribute"),
75057c478bd9Sstevel@tonic-gate 		    (attrp == NULL) ? "" : attrp);
75067c478bd9Sstevel@tonic-gate 		return (NULL);
75077c478bd9Sstevel@tonic-gate 	}
75087c478bd9Sstevel@tonic-gate 
75097c478bd9Sstevel@tonic-gate 	if (TmpSt.st_dev == (o_dev_t)NODEV) {
75107c478bd9Sstevel@tonic-gate 		/*
75117c478bd9Sstevel@tonic-gate 		 * Having trouble representing the device/inode pair.  We can't
75127c478bd9Sstevel@tonic-gate 		 * track links in this case; break them all into separate
75137c478bd9Sstevel@tonic-gate 		 * files.
75147c478bd9Sstevel@tonic-gate 		 */
75157c478bd9Sstevel@tonic-gate 
75167c478bd9Sstevel@tonic-gate 		TmpSt.st_ino = 0;
75177c478bd9Sstevel@tonic-gate 
75187c478bd9Sstevel@tonic-gate 		if (((TmpSt.st_mode & Ftype) != S_IFDIR) &&
75197c478bd9Sstevel@tonic-gate 		    TmpSt.st_nlink > 1)
75207c478bd9Sstevel@tonic-gate 			msg(POST,
75217c478bd9Sstevel@tonic-gate 			    "Warning: file %s%s%s has large "
75227c478bd9Sstevel@tonic-gate 			    "device number - linked "
75237c478bd9Sstevel@tonic-gate 			    "files will be restored as "
75247c478bd9Sstevel@tonic-gate 			    "separate files",
75257c478bd9Sstevel@tonic-gate 			    namep,
75267c478bd9Sstevel@tonic-gate 			    (attrp == NULL) ? "" : gettext(" Attribute"),
75277c478bd9Sstevel@tonic-gate 			    (attrp == NULL) ? "" : attrp);
75287c478bd9Sstevel@tonic-gate 
75297c478bd9Sstevel@tonic-gate 		/* ensure no links */
75307c478bd9Sstevel@tonic-gate 
75317c478bd9Sstevel@tonic-gate 		TmpSt.st_nlink = 1;
75327c478bd9Sstevel@tonic-gate 	}
75337c478bd9Sstevel@tonic-gate 
75347c478bd9Sstevel@tonic-gate 	/* Start converting values */
75357c478bd9Sstevel@tonic-gate 
75367c478bd9Sstevel@tonic-gate 	if (TmpSt.st_dev < 0) {
75377c478bd9Sstevel@tonic-gate 		ToSt.st_dev = 0;
75387c478bd9Sstevel@tonic-gate 	} else {
75397c478bd9Sstevel@tonic-gate 		ToSt.st_dev = (dev_t)TmpSt.st_dev;
75407c478bd9Sstevel@tonic-gate 	}
75417c478bd9Sstevel@tonic-gate 
75427c478bd9Sstevel@tonic-gate 	/* -actual- not truncated uid */
75437c478bd9Sstevel@tonic-gate 
75447c478bd9Sstevel@tonic-gate 	ToSt.st_uid = TmpSt.st_uid;
75457c478bd9Sstevel@tonic-gate 
75467c478bd9Sstevel@tonic-gate 	/* -actual- not truncated gid */
75477c478bd9Sstevel@tonic-gate 
75487c478bd9Sstevel@tonic-gate 	ToSt.st_gid = TmpSt.st_gid;
75497c478bd9Sstevel@tonic-gate 	ToSt.st_ino = (ino_t)TmpSt.st_ino;
75507c478bd9Sstevel@tonic-gate 	ToSt.st_mode = (mode_t)TmpSt.st_mode;
75517c478bd9Sstevel@tonic-gate 	ToSt.st_mtime = (ulong_t)TmpSt.st_modtime;
75527c478bd9Sstevel@tonic-gate 	ToSt.st_nlink = (nlink_t)TmpSt.st_nlink;
75537c478bd9Sstevel@tonic-gate 	ToSt.st_size = (off_t)TmpSt.st_size;
75547c478bd9Sstevel@tonic-gate 	ToSt.st_rdev = (dev_t)TmpSt.st_rdev;
75557c478bd9Sstevel@tonic-gate 
75567c478bd9Sstevel@tonic-gate 	return (&ToSt);
75577c478bd9Sstevel@tonic-gate }
75587c478bd9Sstevel@tonic-gate 
75597c478bd9Sstevel@tonic-gate /*
75607c478bd9Sstevel@tonic-gate  * In the beginning of each bar archive, there is a header which describes the
75617c478bd9Sstevel@tonic-gate  * current volume being created, followed by a header which describes the
75627c478bd9Sstevel@tonic-gate  * current file being created, followed by the file itself.  If there is
75637c478bd9Sstevel@tonic-gate  * more than one file to be created, a separate header will be created for
75647c478bd9Sstevel@tonic-gate  * each additional file.  This structure may be repeated if the bar archive
75657c478bd9Sstevel@tonic-gate  * contains multiple volumes.  If a file spans across volumes, its header
75667c478bd9Sstevel@tonic-gate  * will not be repeated in the next volume.
75677c478bd9Sstevel@tonic-gate  *               +------------------+
75687c478bd9Sstevel@tonic-gate  *               |    vol header    |
75697c478bd9Sstevel@tonic-gate  *               |------------------|
75707c478bd9Sstevel@tonic-gate  *               |   file header i  |     i = 0
75717c478bd9Sstevel@tonic-gate  *               |------------------|
75727c478bd9Sstevel@tonic-gate  *               |     <file i>     |
75737c478bd9Sstevel@tonic-gate  *               |------------------|
75747c478bd9Sstevel@tonic-gate  *               |  file header i+1 |
75757c478bd9Sstevel@tonic-gate  *               |------------------|
75767c478bd9Sstevel@tonic-gate  *               |    <file i+1>    |
75777c478bd9Sstevel@tonic-gate  *               |------------------|
75787c478bd9Sstevel@tonic-gate  *               |        .         |
75797c478bd9Sstevel@tonic-gate  *               |        .         |
75807c478bd9Sstevel@tonic-gate  *               |        .         |
75817c478bd9Sstevel@tonic-gate  *               +------------------+
75827c478bd9Sstevel@tonic-gate  */
75837c478bd9Sstevel@tonic-gate 
75847c478bd9Sstevel@tonic-gate /*
75857c478bd9Sstevel@tonic-gate  * read in the header that describes the current volume of the bar archive
75867c478bd9Sstevel@tonic-gate  * to be extracted.
75877c478bd9Sstevel@tonic-gate  */
75887c478bd9Sstevel@tonic-gate static void
75897c478bd9Sstevel@tonic-gate read_bar_vol_hdr(void)
75907c478bd9Sstevel@tonic-gate {
75917c478bd9Sstevel@tonic-gate 	union b_block *tmp_hdr;
75927c478bd9Sstevel@tonic-gate 
75937c478bd9Sstevel@tonic-gate 	tmp_hdr = (union b_block *)Buffr.b_out_p;
75947c478bd9Sstevel@tonic-gate 	if (tmp_hdr->dbuf.bar_magic[0] == BAR_VOLUME_MAGIC) {
75957c478bd9Sstevel@tonic-gate 
75967c478bd9Sstevel@tonic-gate 		if (bar_Vhdr == NULL) {
75977c478bd9Sstevel@tonic-gate 			bar_Vhdr = e_zalloc(E_EXIT, TBLOCK);
75987c478bd9Sstevel@tonic-gate 		}
75997c478bd9Sstevel@tonic-gate 		(void) memcpy(&(bar_Vhdr->dbuf), &(tmp_hdr->dbuf), TBLOCK);
76007c478bd9Sstevel@tonic-gate 	} else {
76017c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
76027c478bd9Sstevel@tonic-gate 		    "bar error: cannot read volume header\n"));
76037c478bd9Sstevel@tonic-gate 		exit(1);
76047c478bd9Sstevel@tonic-gate 	}
76057c478bd9Sstevel@tonic-gate 
76067c478bd9Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.mode, "%8lo", &Gen_bar_vol.g_mode);
76077c478bd9Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.uid, "%8d", (int *)&Gen_bar_vol.g_uid);
76087c478bd9Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.gid, "%8d", (int *)&Gen_bar_vol.g_gid);
76097c478bd9Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.size, "%12llo",
76107c478bd9Sstevel@tonic-gate 	    (u_off_t *)&Gen_bar_vol.g_filesz);
76117c478bd9Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.mtime, "%12lo", &Gen_bar_vol.g_mtime);
76127c478bd9Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.chksum, "%8lo", &Gen_bar_vol.g_cksum);
76137c478bd9Sstevel@tonic-gate 
76147c478bd9Sstevel@tonic-gate 	/* set the compress flag */
76157c478bd9Sstevel@tonic-gate 	if (bar_Vhdr->dbuf.compressed == '1')
76167c478bd9Sstevel@tonic-gate 		Compressed = 1;
76177c478bd9Sstevel@tonic-gate 	else
76187c478bd9Sstevel@tonic-gate 		Compressed = 0;
76197c478bd9Sstevel@tonic-gate 
76207c478bd9Sstevel@tonic-gate 	Buffr.b_out_p += 512;
76217c478bd9Sstevel@tonic-gate 	Buffr.b_cnt -= 512;
76227c478bd9Sstevel@tonic-gate 
76237c478bd9Sstevel@tonic-gate 	/*
76247c478bd9Sstevel@tonic-gate 	 * not the first volume; exit
76257c478bd9Sstevel@tonic-gate 	 */
76267c478bd9Sstevel@tonic-gate 	if (strcmp(bar_Vhdr->dbuf.volume_num, "1") != 0) {
76277c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
76287c478bd9Sstevel@tonic-gate 		    gettext("error: This is not volume 1.  "));
76297c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("This is volume %s.  "),
76307c478bd9Sstevel@tonic-gate 		    bar_Vhdr->dbuf.volume_num);
76317c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Please insert volume 1.\n"));
76327c478bd9Sstevel@tonic-gate 		exit(1);
76337c478bd9Sstevel@tonic-gate 	}
76347c478bd9Sstevel@tonic-gate 
76357c478bd9Sstevel@tonic-gate 	read_bar_file_hdr();
76367c478bd9Sstevel@tonic-gate }
76377c478bd9Sstevel@tonic-gate 
76387c478bd9Sstevel@tonic-gate /*
76397c478bd9Sstevel@tonic-gate  * read in the header that describes the current file to be extracted
76407c478bd9Sstevel@tonic-gate  */
76417c478bd9Sstevel@tonic-gate static void
76427c478bd9Sstevel@tonic-gate read_bar_file_hdr(void)
76437c478bd9Sstevel@tonic-gate {
76447c478bd9Sstevel@tonic-gate 	union b_block *tmp_hdr;
76457c478bd9Sstevel@tonic-gate 	char *start_of_name, *name_p;
76467c478bd9Sstevel@tonic-gate 	char *tmp;
76477c478bd9Sstevel@tonic-gate 
76487c478bd9Sstevel@tonic-gate 	if (*Buffr.b_out_p == '\0') {
76497c478bd9Sstevel@tonic-gate 		*Gen.g_nam_p = '\0';
76507c478bd9Sstevel@tonic-gate 		exit(0);
76517c478bd9Sstevel@tonic-gate 	}
76527c478bd9Sstevel@tonic-gate 
76537c478bd9Sstevel@tonic-gate 	tmp_hdr = (union b_block *)Buffr.b_out_p;
76547c478bd9Sstevel@tonic-gate 
76557c478bd9Sstevel@tonic-gate 	tmp = &tmp_hdr->dbuf.mode[1];
76567c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp, "%8lo", &Gen.g_mode);
76577c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.uid, "%8lo", &Gen.g_uid);
76587c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.gid, "%8lo", &Gen.g_gid);
76597c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.size, "%12llo",
76607c478bd9Sstevel@tonic-gate 	    (u_off_t *)&Gen.g_filesz);
76617c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.mtime, "%12lo", &Gen.g_mtime);
76627c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.chksum, "%8lo", &Gen.g_cksum);
76637c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.rdev, "%8lo", &Gen.g_rdev);
76647c478bd9Sstevel@tonic-gate 
76657c478bd9Sstevel@tonic-gate #define	to_new_major(x)	(int)((unsigned)((x) & OMAXMAJ) << NBITSMINOR)
76667c478bd9Sstevel@tonic-gate #define	to_new_minor(x)	(int)((x) & OMAXMIN)
76677c478bd9Sstevel@tonic-gate 	Gen.g_rdev = to_new_major(Gen.g_rdev) | to_new_minor(Gen.g_rdev);
76687c478bd9Sstevel@tonic-gate 	bar_linkflag = tmp_hdr->dbuf.linkflag;
76697c478bd9Sstevel@tonic-gate 	start_of_name = &tmp_hdr->dbuf.start_of_name;
76707c478bd9Sstevel@tonic-gate 
76717c478bd9Sstevel@tonic-gate 
76727c478bd9Sstevel@tonic-gate 	name_p = Gen.g_nam_p;
76737c478bd9Sstevel@tonic-gate 	while (*name_p++ = *start_of_name++)
76747c478bd9Sstevel@tonic-gate 		;
76757c478bd9Sstevel@tonic-gate 	*name_p = '\0';
76767c478bd9Sstevel@tonic-gate 	if (bar_linkflag == LNKTYPE || bar_linkflag == SYMTYPE)
76777c478bd9Sstevel@tonic-gate 		(void) strcpy(bar_linkname, start_of_name);
76787c478bd9Sstevel@tonic-gate 
76797c478bd9Sstevel@tonic-gate 	Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
76807c478bd9Sstevel@tonic-gate 	(void) strcpy(nambuf, Gen.g_nam_p);
76817c478bd9Sstevel@tonic-gate }
76827c478bd9Sstevel@tonic-gate 
76837c478bd9Sstevel@tonic-gate /*
76847c478bd9Sstevel@tonic-gate  * if the bar archive is compressed, set up a pipe and do the de-compression
76857c478bd9Sstevel@tonic-gate  * as the compressed file is read in.
76867c478bd9Sstevel@tonic-gate  */
76877c478bd9Sstevel@tonic-gate static void
76887c478bd9Sstevel@tonic-gate setup_uncompress(FILE **pipef)
76897c478bd9Sstevel@tonic-gate {
76907c478bd9Sstevel@tonic-gate 	char *cmd_buf;
76917c478bd9Sstevel@tonic-gate 	size_t cmdlen;
76927c478bd9Sstevel@tonic-gate 
76937c478bd9Sstevel@tonic-gate 	cmd_buf = e_zalloc(E_EXIT, MAXPATHLEN * 2);
76947c478bd9Sstevel@tonic-gate 
76957c478bd9Sstevel@tonic-gate 	if (access(Gen.g_nam_p, W_OK) != 0) {
76967c478bd9Sstevel@tonic-gate 		cmdlen = snprintf(cmd_buf, MAXPATHLEN * 2,
76977c478bd9Sstevel@tonic-gate 		    "chmod +w '%s'; uncompress -c > '%s'; "
76987c478bd9Sstevel@tonic-gate 		    "chmod 0%o '%s'",
76997c478bd9Sstevel@tonic-gate 		    Gen.g_nam_p, Gen.g_nam_p, (int)G_p->g_mode, Gen.g_nam_p);
77007c478bd9Sstevel@tonic-gate 	} else {
77017c478bd9Sstevel@tonic-gate 		cmdlen = snprintf(cmd_buf, MAXPATHLEN * 2,
77027c478bd9Sstevel@tonic-gate 		    "uncompress -c > '%s'", Gen.g_nam_p);
77037c478bd9Sstevel@tonic-gate 	}
77047c478bd9Sstevel@tonic-gate 
77057c478bd9Sstevel@tonic-gate 	if (cmdlen >= MAXPATHLEN * 2 ||
77067c478bd9Sstevel@tonic-gate 	    (*pipef = popen(cmd_buf, "w")) == NULL) {
77077c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("error\n"));
77087c478bd9Sstevel@tonic-gate 		exit(1);
77097c478bd9Sstevel@tonic-gate 	}
77107c478bd9Sstevel@tonic-gate 
77117c478bd9Sstevel@tonic-gate 	if (close(Ofile) != 0)
77127c478bd9Sstevel@tonic-gate 		msg(EXTN, "close error");
77137c478bd9Sstevel@tonic-gate 	Ofile = fileno(*pipef);
77147c478bd9Sstevel@tonic-gate 
77157c478bd9Sstevel@tonic-gate 	free(cmd_buf);
77167c478bd9Sstevel@tonic-gate }
77177c478bd9Sstevel@tonic-gate 
77187c478bd9Sstevel@tonic-gate /*
77197c478bd9Sstevel@tonic-gate  * if the bar archive spans multiple volumes, read in the header that
77207c478bd9Sstevel@tonic-gate  * describes the next volume.
77217c478bd9Sstevel@tonic-gate  */
77227c478bd9Sstevel@tonic-gate static void
77237c478bd9Sstevel@tonic-gate skip_bar_volhdr(void)
77247c478bd9Sstevel@tonic-gate {
77257c478bd9Sstevel@tonic-gate 	char *buff;
77267c478bd9Sstevel@tonic-gate 	union b_block *tmp_hdr;
77277c478bd9Sstevel@tonic-gate 
77287c478bd9Sstevel@tonic-gate 	buff = e_zalloc(E_EXIT, (uint_t)Bufsize);
77297c478bd9Sstevel@tonic-gate 
77307c478bd9Sstevel@tonic-gate 	if (g_read(Device, Archive, buff, Bufsize) < 0) {
77317c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
77327c478bd9Sstevel@tonic-gate 		    "error in skip_bar_volhdr\n"));
77337c478bd9Sstevel@tonic-gate 	} else {
77347c478bd9Sstevel@tonic-gate 
77357c478bd9Sstevel@tonic-gate 		tmp_hdr = (union b_block *)buff;
77367c478bd9Sstevel@tonic-gate 		if (tmp_hdr->dbuf.bar_magic[0] == BAR_VOLUME_MAGIC) {
77377c478bd9Sstevel@tonic-gate 
77387c478bd9Sstevel@tonic-gate 			if (bar_Vhdr == NULL) {
77397c478bd9Sstevel@tonic-gate 				bar_Vhdr = e_zalloc(E_EXIT, TBLOCK);
77407c478bd9Sstevel@tonic-gate 			}
77417c478bd9Sstevel@tonic-gate 			(void) memcpy(&(bar_Vhdr->dbuf),
77427c478bd9Sstevel@tonic-gate 			    &(tmp_hdr->dbuf), TBLOCK);
77437c478bd9Sstevel@tonic-gate 		} else {
77447c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
77457c478bd9Sstevel@tonic-gate 			    gettext("cpio error: cannot read bar volume "
77467c478bd9Sstevel@tonic-gate 			    "header\n"));
77477c478bd9Sstevel@tonic-gate 			exit(1);
77487c478bd9Sstevel@tonic-gate 		}
77497c478bd9Sstevel@tonic-gate 
77507c478bd9Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.mode, "%8lo",
77517c478bd9Sstevel@tonic-gate 		    &Gen_bar_vol.g_mode);
77527c478bd9Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.uid, "%8lo",
77537c478bd9Sstevel@tonic-gate 		    &Gen_bar_vol.g_uid);
77547c478bd9Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.gid, "%8lo",
77557c478bd9Sstevel@tonic-gate 		    &Gen_bar_vol.g_gid);
77567c478bd9Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.size, "%12llo",
77577c478bd9Sstevel@tonic-gate 		    (u_off_t *)&Gen_bar_vol.g_filesz);
77587c478bd9Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.mtime, "%12lo",
77597c478bd9Sstevel@tonic-gate 		    &Gen_bar_vol.g_mtime);
77607c478bd9Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.chksum, "%8lo",
77617c478bd9Sstevel@tonic-gate 		    &Gen_bar_vol.g_cksum);
77627c478bd9Sstevel@tonic-gate 		if (bar_Vhdr->dbuf.compressed == '1')
77637c478bd9Sstevel@tonic-gate 			Compressed = 1;
77647c478bd9Sstevel@tonic-gate 		else
77657c478bd9Sstevel@tonic-gate 			Compressed = 0;
77667c478bd9Sstevel@tonic-gate 	}
77677c478bd9Sstevel@tonic-gate 
77687c478bd9Sstevel@tonic-gate 	/*
77697c478bd9Sstevel@tonic-gate 	 * Now put the rest of the bytes read in into the data buffer.
77707c478bd9Sstevel@tonic-gate 	 */
77717c478bd9Sstevel@tonic-gate 	(void) memcpy(Buffr.b_in_p, &buff[512], (Bufsize - 512));
77727c478bd9Sstevel@tonic-gate 	Buffr.b_in_p += (Bufsize - 512);
77737c478bd9Sstevel@tonic-gate 	Buffr.b_cnt += (long)(Bufsize - 512);
77747c478bd9Sstevel@tonic-gate 
77757c478bd9Sstevel@tonic-gate 	free(buff);
77767c478bd9Sstevel@tonic-gate }
77777c478bd9Sstevel@tonic-gate 
77787c478bd9Sstevel@tonic-gate /*
77797c478bd9Sstevel@tonic-gate  * check the linkflag which indicates the type of the file to be extracted,
77807c478bd9Sstevel@tonic-gate  * invoke the corresponding routine to extract the file.
77817c478bd9Sstevel@tonic-gate  */
77827c478bd9Sstevel@tonic-gate static void
77837c478bd9Sstevel@tonic-gate bar_file_in(void)
77847c478bd9Sstevel@tonic-gate {
77857c478bd9Sstevel@tonic-gate 	/*
77867c478bd9Sstevel@tonic-gate 	 * the file is a directory
77877c478bd9Sstevel@tonic-gate 	 */
77887c478bd9Sstevel@tonic-gate 	if (Adir) {
77897c478bd9Sstevel@tonic-gate 		if (ckname(1) != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
77907c478bd9Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
77917c478bd9Sstevel@tonic-gate 		}
77927c478bd9Sstevel@tonic-gate 		return;
77937c478bd9Sstevel@tonic-gate 	}
77947c478bd9Sstevel@tonic-gate 
77957c478bd9Sstevel@tonic-gate 	switch (bar_linkflag) {
77967c478bd9Sstevel@tonic-gate 	case REGTYPE:
77977c478bd9Sstevel@tonic-gate 		/* regular file */
77987c478bd9Sstevel@tonic-gate 		if ((ckname(1) == F_SKIP) ||
77997c478bd9Sstevel@tonic-gate 		    (Ofile = openout(G_p->g_dirfd)) < 0) {
78007c478bd9Sstevel@tonic-gate 			data_in(P_SKIP);
78017c478bd9Sstevel@tonic-gate 		} else {
78027c478bd9Sstevel@tonic-gate 			data_in(P_PROC);
78037c478bd9Sstevel@tonic-gate 		}
78047c478bd9Sstevel@tonic-gate 		break;
78057c478bd9Sstevel@tonic-gate 	case LNKTYPE:
78067c478bd9Sstevel@tonic-gate 		/* hard link */
78077c478bd9Sstevel@tonic-gate 		if (ckname(1) == F_SKIP) {
78087c478bd9Sstevel@tonic-gate 			break;
78097c478bd9Sstevel@tonic-gate 		}
78107c478bd9Sstevel@tonic-gate 		(void) creat_lnk(G_p->g_dirfd, bar_linkname, G_p->g_nam_p);
78117c478bd9Sstevel@tonic-gate 		break;
78127c478bd9Sstevel@tonic-gate 	case SYMTYPE:
78137c478bd9Sstevel@tonic-gate 		/* symbolic link */
78147c478bd9Sstevel@tonic-gate 		if ((ckname(1) == F_SKIP) ||
78157c478bd9Sstevel@tonic-gate 		    (Ofile = openout(G_p->g_dirfd)) < 0) {
78167c478bd9Sstevel@tonic-gate 			data_in(P_SKIP);
78177c478bd9Sstevel@tonic-gate 		} else {
78187c478bd9Sstevel@tonic-gate 			data_in(P_PROC);
78197c478bd9Sstevel@tonic-gate 		}
78207c478bd9Sstevel@tonic-gate 		break;
78217c478bd9Sstevel@tonic-gate 	case CHRTYPE:
78227c478bd9Sstevel@tonic-gate 		/* character device or FIFO */
78237c478bd9Sstevel@tonic-gate 		if (ckname(1) != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
78247c478bd9Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
78257c478bd9Sstevel@tonic-gate 		}
78267c478bd9Sstevel@tonic-gate 		break;
78277c478bd9Sstevel@tonic-gate 	default:
78287c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("error: unknown file type\n"));
78297c478bd9Sstevel@tonic-gate 		break;
78307c478bd9Sstevel@tonic-gate 	}
78317c478bd9Sstevel@tonic-gate }
78327c478bd9Sstevel@tonic-gate 
78337c478bd9Sstevel@tonic-gate 
78347c478bd9Sstevel@tonic-gate /*
78357c478bd9Sstevel@tonic-gate  * This originally came from libgenIO/g_init.c
78367c478bd9Sstevel@tonic-gate  * XXX	And it is very broken.
78377c478bd9Sstevel@tonic-gate  */
78387c478bd9Sstevel@tonic-gate 
78397c478bd9Sstevel@tonic-gate /* #include <sys/statvfs.h> */
78407c478bd9Sstevel@tonic-gate #include <ftw.h>
78417c478bd9Sstevel@tonic-gate /* #include <libgenIO.h> */
78427c478bd9Sstevel@tonic-gate #define	G_TM_TAPE	1	/* Tapemaster controller    */
78437c478bd9Sstevel@tonic-gate #define	G_XY_DISK	3	/* xy disks		*/
78447c478bd9Sstevel@tonic-gate #define	G_SD_DISK	7	/* scsi sd disk		*/
78457c478bd9Sstevel@tonic-gate #define	G_XT_TAPE	8	/* xt tapes		*/
78467c478bd9Sstevel@tonic-gate #define	G_SF_FLOPPY	9	/* sf floppy		*/
78477c478bd9Sstevel@tonic-gate #define	G_XD_DISK	10	/* xd disks		*/
78487c478bd9Sstevel@tonic-gate #define	G_ST_TAPE	11	/* scsi tape		*/
78497c478bd9Sstevel@tonic-gate #define	G_NS		12	/* noswap pseudo-dev	*/
78507c478bd9Sstevel@tonic-gate #define	G_RAM		13	/* ram pseudo-dev	*/
78517c478bd9Sstevel@tonic-gate #define	G_FT		14	/* tftp			*/
78527c478bd9Sstevel@tonic-gate #define	G_HD		15	/* 386 network disk	*/
78537c478bd9Sstevel@tonic-gate #define	G_FD		16	/* 386 AT disk		*/
78547c478bd9Sstevel@tonic-gate #define	G_FILE		28	/* file, not a device	*/
78557c478bd9Sstevel@tonic-gate #define	G_NO_DEV	29	/* device does not require special treatment */
78567c478bd9Sstevel@tonic-gate #define	G_DEV_MAX	30	/* last valid device type */
78577c478bd9Sstevel@tonic-gate 
78587c478bd9Sstevel@tonic-gate /*
78597c478bd9Sstevel@tonic-gate  * g_init: Determine the device being accessed, set the buffer size,
78607c478bd9Sstevel@tonic-gate  * and perform any device specific initialization. Since at this point
78617c478bd9Sstevel@tonic-gate  * Sun has no system call to read the configuration, the major numbers
78627c478bd9Sstevel@tonic-gate  * are assumed to be static and types are figured out as such. However,
78637c478bd9Sstevel@tonic-gate  * as a rough estimate, the buffer size for all types is set to 512
78647c478bd9Sstevel@tonic-gate  * as a default.
78657c478bd9Sstevel@tonic-gate  */
78667c478bd9Sstevel@tonic-gate 
78677c478bd9Sstevel@tonic-gate static int
78687c478bd9Sstevel@tonic-gate g_init(int *devtype, int *fdes)
78697c478bd9Sstevel@tonic-gate {
78707c478bd9Sstevel@tonic-gate 	int bufsize;
78717c478bd9Sstevel@tonic-gate 	struct stat st_buf;
78727c478bd9Sstevel@tonic-gate 	struct statvfs stfs_buf;
78737c478bd9Sstevel@tonic-gate 
78747c478bd9Sstevel@tonic-gate 	*devtype = G_NO_DEV;
78757c478bd9Sstevel@tonic-gate 	bufsize = -1;
78767c478bd9Sstevel@tonic-gate 	if (fstat(*fdes, &st_buf) == -1)
78777c478bd9Sstevel@tonic-gate 		return (-1);
78784bc0a2efScasper 	if (!S_ISCHR(st_buf.st_mode) && !S_ISBLK(st_buf.st_mode)) {
78794bc0a2efScasper 		if (S_ISFIFO(st_buf.st_mode)) {
78807c478bd9Sstevel@tonic-gate 			bufsize = 512;
78817c478bd9Sstevel@tonic-gate 		} else {
78827c478bd9Sstevel@tonic-gate 			/* find block size for this file system */
78837c478bd9Sstevel@tonic-gate 			*devtype = G_FILE;
78847c478bd9Sstevel@tonic-gate 			if (fstatvfs(*fdes, &stfs_buf) < 0) {
78857c478bd9Sstevel@tonic-gate 					bufsize = -1;
78867c478bd9Sstevel@tonic-gate 					errno = ENODEV;
78877c478bd9Sstevel@tonic-gate 			} else
78887c478bd9Sstevel@tonic-gate 				bufsize = stfs_buf.f_bsize;
78897c478bd9Sstevel@tonic-gate 		}
78907c478bd9Sstevel@tonic-gate 
78917c478bd9Sstevel@tonic-gate 		return (bufsize);
78927c478bd9Sstevel@tonic-gate 
78937c478bd9Sstevel@tonic-gate 	/*
78947c478bd9Sstevel@tonic-gate 	 * We'll have to add a remote attribute to stat but this
78957c478bd9Sstevel@tonic-gate 	 * should work for now.
78967c478bd9Sstevel@tonic-gate 	 */
78977c478bd9Sstevel@tonic-gate 	} else if (st_buf.st_dev & 0x8000)	/* if remote  rdev */
78987c478bd9Sstevel@tonic-gate 		return (512);
78997c478bd9Sstevel@tonic-gate 
79007c478bd9Sstevel@tonic-gate 	bufsize = 512;
79017c478bd9Sstevel@tonic-gate 
79027c478bd9Sstevel@tonic-gate 	if (Hdr_type == BAR) {
79037c478bd9Sstevel@tonic-gate 		if (is_tape(*fdes)) {
79047c478bd9Sstevel@tonic-gate 			bufsize = BAR_TAPE_SIZE;
790561694e45SRich Burridge 			msg(EPOST, "Archiving to tape blocking factor 126");
79067c478bd9Sstevel@tonic-gate 		} else if (is_floppy(*fdes)) {
79077c478bd9Sstevel@tonic-gate 			bufsize = BAR_FLOPPY_SIZE;
790861694e45SRich Burridge 			msg(EPOST, "Archiving to floppy blocking factor 18");
79097c478bd9Sstevel@tonic-gate 		}
79107c478bd9Sstevel@tonic-gate 	}
79117c478bd9Sstevel@tonic-gate 
79127c478bd9Sstevel@tonic-gate 	return (bufsize);
79137c478bd9Sstevel@tonic-gate }
79147c478bd9Sstevel@tonic-gate 
79157c478bd9Sstevel@tonic-gate /*
79167c478bd9Sstevel@tonic-gate  * This originally came from libgenIO/g_read.c
79177c478bd9Sstevel@tonic-gate  */
79187c478bd9Sstevel@tonic-gate 
79197c478bd9Sstevel@tonic-gate /*
79207c478bd9Sstevel@tonic-gate  * g_read: Read nbytes of data from fdes (of type devtype) and place
79217c478bd9Sstevel@tonic-gate  * data in location pointed to by buf.  In case of end of medium,
79227c478bd9Sstevel@tonic-gate  * translate (where necessary) device specific EOM indications into
79237c478bd9Sstevel@tonic-gate  * the generic EOM indication of rv = -1, errno = ENOSPC.
79247c478bd9Sstevel@tonic-gate  */
79257c478bd9Sstevel@tonic-gate 
79267c478bd9Sstevel@tonic-gate static int
79277c478bd9Sstevel@tonic-gate g_read(int devtype, int fdes, char *buf, unsigned nbytes)
79287c478bd9Sstevel@tonic-gate {
79297c478bd9Sstevel@tonic-gate 	int rv;
79307c478bd9Sstevel@tonic-gate 
79317c478bd9Sstevel@tonic-gate 	if (devtype < 0 || devtype >= G_DEV_MAX) {
79327c478bd9Sstevel@tonic-gate 		errno = ENODEV;
79337c478bd9Sstevel@tonic-gate 		return (-1);
79347c478bd9Sstevel@tonic-gate 	}
79357c478bd9Sstevel@tonic-gate 
79367c478bd9Sstevel@tonic-gate 	rv = read(fdes, buf, nbytes);
79377c478bd9Sstevel@tonic-gate 
79387c478bd9Sstevel@tonic-gate 	/* st devices return 0 when no space left */
79397c478bd9Sstevel@tonic-gate 	if ((rv == 0 && errno == 0 && Hdr_type != BAR) ||
79407c478bd9Sstevel@tonic-gate 	    (rv == -1 && errno == EIO)) {
79417c478bd9Sstevel@tonic-gate 		errno = 0;
79427c478bd9Sstevel@tonic-gate 		rv = 0;
79437c478bd9Sstevel@tonic-gate 	}
79447c478bd9Sstevel@tonic-gate 
79457c478bd9Sstevel@tonic-gate 	return (rv);
79467c478bd9Sstevel@tonic-gate }
79477c478bd9Sstevel@tonic-gate 
79487c478bd9Sstevel@tonic-gate /*
79497c478bd9Sstevel@tonic-gate  * This originally came from libgenIO/g_write.c
79507c478bd9Sstevel@tonic-gate  */
79517c478bd9Sstevel@tonic-gate 
79527c478bd9Sstevel@tonic-gate /*
79537c478bd9Sstevel@tonic-gate  * g_write: Write nbytes of data to fdes (of type devtype) from
79547c478bd9Sstevel@tonic-gate  * the location pointed to by buf.  In case of end of medium,
79557c478bd9Sstevel@tonic-gate  * translate (where necessary) device specific EOM indications into
79567c478bd9Sstevel@tonic-gate  * the generic EOM indication of rv = -1, errno = ENOSPC.
79577c478bd9Sstevel@tonic-gate  */
79587c478bd9Sstevel@tonic-gate 
79597c478bd9Sstevel@tonic-gate static int
79607c478bd9Sstevel@tonic-gate g_write(int devtype, int fdes, char *buf, unsigned nbytes)
79617c478bd9Sstevel@tonic-gate {
79627c478bd9Sstevel@tonic-gate 	int rv;
79637c478bd9Sstevel@tonic-gate 
79647c478bd9Sstevel@tonic-gate 	if (devtype < 0 || devtype >= G_DEV_MAX) {
79657c478bd9Sstevel@tonic-gate 		errno = ENODEV;
79667c478bd9Sstevel@tonic-gate 		return (-1);
79677c478bd9Sstevel@tonic-gate 	}
79687c478bd9Sstevel@tonic-gate 
79697c478bd9Sstevel@tonic-gate 	rv = write(fdes, buf, nbytes);
79707c478bd9Sstevel@tonic-gate 
79717c478bd9Sstevel@tonic-gate 	/* st devices return 0 when no more space left */
79727c478bd9Sstevel@tonic-gate 	if ((rv == 0 && errno == 0) || (rv == -1 && errno == EIO)) {
79737c478bd9Sstevel@tonic-gate 		errno = ENOSPC;
79747c478bd9Sstevel@tonic-gate 		rv = -1;
79757c478bd9Sstevel@tonic-gate 	}
79767c478bd9Sstevel@tonic-gate 
79777c478bd9Sstevel@tonic-gate 	return (rv);
79787c478bd9Sstevel@tonic-gate }
79797c478bd9Sstevel@tonic-gate 
79807c478bd9Sstevel@tonic-gate /*
79817c478bd9Sstevel@tonic-gate  * Test for tape
79827c478bd9Sstevel@tonic-gate  */
79837c478bd9Sstevel@tonic-gate 
79847c478bd9Sstevel@tonic-gate static int
79857c478bd9Sstevel@tonic-gate is_tape(int fd)
79867c478bd9Sstevel@tonic-gate {
79877c478bd9Sstevel@tonic-gate 	struct mtget stuff;
79887c478bd9Sstevel@tonic-gate 
79897c478bd9Sstevel@tonic-gate 	/*
79907c478bd9Sstevel@tonic-gate 	 * try to do a generic tape ioctl, just to see if
79917c478bd9Sstevel@tonic-gate 	 * the thing is in fact a tape drive(er).
79927c478bd9Sstevel@tonic-gate 	 */
79937c478bd9Sstevel@tonic-gate 	if (ioctl(fd, MTIOCGET, &stuff) != -1) {
79947c478bd9Sstevel@tonic-gate 		/* the ioctl succeeded, must have been a tape */
79957c478bd9Sstevel@tonic-gate 		return (1);
79967c478bd9Sstevel@tonic-gate 	}
79977c478bd9Sstevel@tonic-gate 	return (0);
79987c478bd9Sstevel@tonic-gate }
79997c478bd9Sstevel@tonic-gate 
80007c478bd9Sstevel@tonic-gate /*
80017c478bd9Sstevel@tonic-gate  * Test for floppy
80027c478bd9Sstevel@tonic-gate  */
80037c478bd9Sstevel@tonic-gate 
80047c478bd9Sstevel@tonic-gate static int
80057c478bd9Sstevel@tonic-gate is_floppy(int fd)
80067c478bd9Sstevel@tonic-gate {
80077c478bd9Sstevel@tonic-gate 	struct fd_char stuff;
80087c478bd9Sstevel@tonic-gate 
80097c478bd9Sstevel@tonic-gate 	/*
80107c478bd9Sstevel@tonic-gate 	 * try to get the floppy drive characteristics, just to see if
80117c478bd9Sstevel@tonic-gate 	 * the thing is in fact a floppy drive(er).
80127c478bd9Sstevel@tonic-gate 	 */
80137c478bd9Sstevel@tonic-gate 	if (ioctl(fd, FDIOGCHAR, &stuff) != -1) {
80147c478bd9Sstevel@tonic-gate 		/* the ioctl succeeded, must have been a floppy */
80157c478bd9Sstevel@tonic-gate 		return (1);
80167c478bd9Sstevel@tonic-gate 	}
80177c478bd9Sstevel@tonic-gate 
80187c478bd9Sstevel@tonic-gate 	return (0);
80197c478bd9Sstevel@tonic-gate }
80207c478bd9Sstevel@tonic-gate 
80217c478bd9Sstevel@tonic-gate /*
80227c478bd9Sstevel@tonic-gate  * New functions for ACLs and other security attributes
80237c478bd9Sstevel@tonic-gate  */
80247c478bd9Sstevel@tonic-gate 
80257c478bd9Sstevel@tonic-gate /*
80267c478bd9Sstevel@tonic-gate  * The function appends the new security attribute info to the end of
80277c478bd9Sstevel@tonic-gate  * existing secinfo.
80287c478bd9Sstevel@tonic-gate  */
80297c478bd9Sstevel@tonic-gate static int
80307c478bd9Sstevel@tonic-gate append_secattr(
80317c478bd9Sstevel@tonic-gate 	char		**secinfo,	/* existing security info */
80327c478bd9Sstevel@tonic-gate 	int		*secinfo_len,	/* length of existing security info */
8033fa9e4066Sahrens 	acl_t		*aclp) 	/* new attribute data pointer */
80347c478bd9Sstevel@tonic-gate {
80357c478bd9Sstevel@tonic-gate 	char	*new_secinfo;
80367c478bd9Sstevel@tonic-gate 	char	*attrtext;
80377c478bd9Sstevel@tonic-gate 	size_t	newattrsize;
80387c478bd9Sstevel@tonic-gate 	int	oldsize;
80397c478bd9Sstevel@tonic-gate 
80407c478bd9Sstevel@tonic-gate 	/* no need to add */
8041fa9e4066Sahrens 	if (aclp == NULL) {
80427c478bd9Sstevel@tonic-gate 		return (0);
80437c478bd9Sstevel@tonic-gate 	}
80447c478bd9Sstevel@tonic-gate 
8045fa9e4066Sahrens 	switch (acl_type(aclp)) {
8046fa9e4066Sahrens 	case ACLENT_T:
8047fa9e4066Sahrens 	case ACE_T:
8048b249c65cSmarks 		attrtext = acl_totext(aclp, ACL_APPEND_ID | ACL_COMPACT_FMT |
8049b249c65cSmarks 		    ACL_SID_FMT);
80507c478bd9Sstevel@tonic-gate 		if (attrtext == NULL) {
805161694e45SRich Burridge 			msg(EPOST, "acltotext failed");
80527c478bd9Sstevel@tonic-gate 			return (-1);
80537c478bd9Sstevel@tonic-gate 		}
80547c478bd9Sstevel@tonic-gate 		/* header: type + size = 8 */
80557c478bd9Sstevel@tonic-gate 		newattrsize = 8 + strlen(attrtext) + 1;
80567c478bd9Sstevel@tonic-gate 		attr = e_zalloc(E_NORMAL, newattrsize);
80577c478bd9Sstevel@tonic-gate 		if (attr == NULL) {
805861694e45SRich Burridge 			msg(EPOST, "can't allocate memory");
80597c478bd9Sstevel@tonic-gate 			return (-1);
80607c478bd9Sstevel@tonic-gate 		}
8061fa9e4066Sahrens 		attr->attr_type = (acl_type(aclp) == ACLENT_T) ?
8062fa9e4066Sahrens 		    UFSD_ACL : ACE_ACL;
80637c478bd9Sstevel@tonic-gate 		/* acl entry count */
8064fa9e4066Sahrens 		(void) sprintf(attr->attr_len, "%06o", acl_cnt(aclp));
80657c478bd9Sstevel@tonic-gate 		(void) strcpy((char *)&attr->attr_info[0], attrtext);
80667c478bd9Sstevel@tonic-gate 		free(attrtext);
80677c478bd9Sstevel@tonic-gate 		break;
80687c478bd9Sstevel@tonic-gate 
80697c478bd9Sstevel@tonic-gate 		/* SunFed's case goes here */
80707c478bd9Sstevel@tonic-gate 
80717c478bd9Sstevel@tonic-gate 	default:
807261694e45SRich Burridge 		msg(EPOST, "unrecognized attribute type");
80737c478bd9Sstevel@tonic-gate 		return (-1);
80747c478bd9Sstevel@tonic-gate 	}
80757c478bd9Sstevel@tonic-gate 
80767c478bd9Sstevel@tonic-gate 	/* old security info + new attr header(8) + new attr */
80777c478bd9Sstevel@tonic-gate 	oldsize = *secinfo_len;
80787c478bd9Sstevel@tonic-gate 	*secinfo_len += newattrsize;
80797c478bd9Sstevel@tonic-gate 	new_secinfo = e_zalloc(E_NORMAL, (uint_t)*secinfo_len);
80807c478bd9Sstevel@tonic-gate 	if (new_secinfo == NULL) {
808161694e45SRich Burridge 		msg(EPOST, "can't allocate memory");
80827c478bd9Sstevel@tonic-gate 		*secinfo_len -= newattrsize;
80837c478bd9Sstevel@tonic-gate 		return (-1);
80847c478bd9Sstevel@tonic-gate 	}
80857c478bd9Sstevel@tonic-gate 
80867c478bd9Sstevel@tonic-gate 	(void) memcpy(new_secinfo, *secinfo, oldsize);
80877c478bd9Sstevel@tonic-gate 	(void) memcpy(new_secinfo + oldsize, attr, newattrsize);
80887c478bd9Sstevel@tonic-gate 
80897c478bd9Sstevel@tonic-gate 	free(*secinfo);
80907c478bd9Sstevel@tonic-gate 	*secinfo = new_secinfo;
80917c478bd9Sstevel@tonic-gate 	return (0);
80927c478bd9Sstevel@tonic-gate }
80937c478bd9Sstevel@tonic-gate 
80945fbb8099SNobutomo Nakano /*
80955fbb8099SNobutomo Nakano  * Append size amount of data from buf to the archive.
80965fbb8099SNobutomo Nakano  */
80977c478bd9Sstevel@tonic-gate static void
80985fbb8099SNobutomo Nakano write_ancillary(char *buf, size_t len, boolean_t padding)
80997c478bd9Sstevel@tonic-gate {
81005fbb8099SNobutomo Nakano 	int	pad, cnt;
81017c478bd9Sstevel@tonic-gate 
81025fbb8099SNobutomo Nakano 	if (len == 0)
81037c478bd9Sstevel@tonic-gate 		return;
81047c478bd9Sstevel@tonic-gate 
81057c478bd9Sstevel@tonic-gate 	while (len > 0) {
81067c478bd9Sstevel@tonic-gate 		cnt = (unsigned)(len > CPIOBSZ) ? CPIOBSZ : len;
81077c478bd9Sstevel@tonic-gate 		FLUSH(cnt);
81087c478bd9Sstevel@tonic-gate 		errno = 0;
81095fbb8099SNobutomo Nakano 		(void) memcpy(Buffr.b_in_p, buf, (unsigned)cnt);
81107c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += cnt;
81115fbb8099SNobutomo Nakano 		Buffr.b_cnt += cnt;
81125fbb8099SNobutomo Nakano 		len -= cnt;
81135fbb8099SNobutomo Nakano 		buf += cnt;
81147c478bd9Sstevel@tonic-gate 	}
81155fbb8099SNobutomo Nakano 	if (padding) {
81167c478bd9Sstevel@tonic-gate 		pad = (Pad_val + 1 - (cnt & Pad_val)) & Pad_val;
81177c478bd9Sstevel@tonic-gate 		if (pad != 0) {
81187c478bd9Sstevel@tonic-gate 			FLUSH(pad);
81195fbb8099SNobutomo Nakano 			(void) memset(Buffr.b_in_p, 0, pad);
81207c478bd9Sstevel@tonic-gate 			Buffr.b_in_p += pad;
81217c478bd9Sstevel@tonic-gate 			Buffr.b_cnt += pad;
81227c478bd9Sstevel@tonic-gate 		}
81237c478bd9Sstevel@tonic-gate 	}
81245fbb8099SNobutomo Nakano }
81257c478bd9Sstevel@tonic-gate 
81267c478bd9Sstevel@tonic-gate static int
81277c478bd9Sstevel@tonic-gate remove_dir(char *path)
81287c478bd9Sstevel@tonic-gate {
81297c478bd9Sstevel@tonic-gate 	DIR		*name;
81307c478bd9Sstevel@tonic-gate 	struct dirent	*direct;
81317c478bd9Sstevel@tonic-gate 	struct stat	sbuf;
81327c478bd9Sstevel@tonic-gate 	char		*path_copy;
81337c478bd9Sstevel@tonic-gate 
81347c478bd9Sstevel@tonic-gate #define	MSG1	"remove_dir() failed to stat(\"%s\") "
81357c478bd9Sstevel@tonic-gate #define	MSG2	"remove_dir() failed to remove_dir(\"%s\") "
81367c478bd9Sstevel@tonic-gate #define	MSG3	"remove_dir() failed to unlink(\"%s\") "
81377c478bd9Sstevel@tonic-gate 
81387c478bd9Sstevel@tonic-gate 	/*
81397c478bd9Sstevel@tonic-gate 	 * Open the directory for reading.
81407c478bd9Sstevel@tonic-gate 	 */
81417c478bd9Sstevel@tonic-gate 	if ((name = opendir(path)) == NULL) {
81427c478bd9Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to opendir(\"%s\") ", path);
81437c478bd9Sstevel@tonic-gate 		return (-1);
81447c478bd9Sstevel@tonic-gate 	}
81457c478bd9Sstevel@tonic-gate 
81467c478bd9Sstevel@tonic-gate 	if (chdir(path) == -1) {
81477c478bd9Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to chdir(\"%s\") ", path);
81487c478bd9Sstevel@tonic-gate 		return (-1);
81497c478bd9Sstevel@tonic-gate 	}
81507c478bd9Sstevel@tonic-gate 
81517c478bd9Sstevel@tonic-gate 	/*
81527c478bd9Sstevel@tonic-gate 	 * Read every directory entry.
81537c478bd9Sstevel@tonic-gate 	 */
81547c478bd9Sstevel@tonic-gate 	while ((direct = readdir(name)) != NULL) {
81557c478bd9Sstevel@tonic-gate 		/*
81567c478bd9Sstevel@tonic-gate 		 * Ignore "." and ".." entries.
81577c478bd9Sstevel@tonic-gate 		 */
81587c478bd9Sstevel@tonic-gate 		if (strcmp(direct->d_name, ".") == 0 ||
81597c478bd9Sstevel@tonic-gate 		    strcmp(direct->d_name, "..") == 0)
81607c478bd9Sstevel@tonic-gate 			continue;
81617c478bd9Sstevel@tonic-gate 
81627c478bd9Sstevel@tonic-gate 			if (lstat(direct->d_name, &sbuf) == -1) {
81637c478bd9Sstevel@tonic-gate 				msg(ERRN, MSG1, direct->d_name);
81647c478bd9Sstevel@tonic-gate 				(void) closedir(name);
81657c478bd9Sstevel@tonic-gate 			return (-1);
81667c478bd9Sstevel@tonic-gate 		}
81677c478bd9Sstevel@tonic-gate 
81687c478bd9Sstevel@tonic-gate 		if (S_ISDIR(sbuf.st_mode)) {
81697c478bd9Sstevel@tonic-gate 			if (remove_dir(direct->d_name) == -1) {
81707c478bd9Sstevel@tonic-gate 				msg(ERRN, MSG2, direct->d_name);
81717c478bd9Sstevel@tonic-gate 				(void) closedir(name);
81727c478bd9Sstevel@tonic-gate 				return (-1);
81737c478bd9Sstevel@tonic-gate 			}
81747c478bd9Sstevel@tonic-gate 		} else {
81757c478bd9Sstevel@tonic-gate 			if (unlink(direct->d_name) == -1) {
81767c478bd9Sstevel@tonic-gate 				msg(ERRN, MSG3, direct->d_name);
81777c478bd9Sstevel@tonic-gate 				(void) closedir(name);
81787c478bd9Sstevel@tonic-gate 				return (-1);
81797c478bd9Sstevel@tonic-gate 			}
81807c478bd9Sstevel@tonic-gate 		}
81817c478bd9Sstevel@tonic-gate 
81827c478bd9Sstevel@tonic-gate 	}
81837c478bd9Sstevel@tonic-gate 
81847c478bd9Sstevel@tonic-gate 	/*
81857c478bd9Sstevel@tonic-gate 	 * Close the directory we just finished reading.
81867c478bd9Sstevel@tonic-gate 	 */
81877c478bd9Sstevel@tonic-gate 	(void) closedir(name);
81887c478bd9Sstevel@tonic-gate 
81897c478bd9Sstevel@tonic-gate 	/*
81907c478bd9Sstevel@tonic-gate 	 * Change directory to the parent directory...
81917c478bd9Sstevel@tonic-gate 	 */
81927c478bd9Sstevel@tonic-gate 	if (chdir("..") == -1) {
81937c478bd9Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to chdir(\"..\") ");
81947c478bd9Sstevel@tonic-gate 		return (-1);
81957c478bd9Sstevel@tonic-gate 	}
81967c478bd9Sstevel@tonic-gate 
81977c478bd9Sstevel@tonic-gate 	/*
81987c478bd9Sstevel@tonic-gate 	 * ...and finally remove the directory; note we have to
81997c478bd9Sstevel@tonic-gate 	 * make a copy since basename is free to modify its input.
82007c478bd9Sstevel@tonic-gate 	 */
82017c478bd9Sstevel@tonic-gate 	path_copy = e_strdup(E_NORMAL, path);
82027c478bd9Sstevel@tonic-gate 	if (path_copy == NULL) {
82037c478bd9Sstevel@tonic-gate 		msg(ERRN, "cannot strdup() the directory pathname ");
82047c478bd9Sstevel@tonic-gate 		return (-1);
82057c478bd9Sstevel@tonic-gate 	}
82067c478bd9Sstevel@tonic-gate 
82077c478bd9Sstevel@tonic-gate 	if (rmdir(basename(path_copy)) == -1) {
82087c478bd9Sstevel@tonic-gate 		free(path_copy);
82097c478bd9Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to rmdir(\"%s\") ", path);
82107c478bd9Sstevel@tonic-gate 		return (-1);
82117c478bd9Sstevel@tonic-gate 	}
82127c478bd9Sstevel@tonic-gate 
82137c478bd9Sstevel@tonic-gate 	free(path_copy);
82147c478bd9Sstevel@tonic-gate 	return (0);
82157c478bd9Sstevel@tonic-gate 
82167c478bd9Sstevel@tonic-gate }
82177c478bd9Sstevel@tonic-gate 
82187c478bd9Sstevel@tonic-gate static int
82197c478bd9Sstevel@tonic-gate save_cwd(void)
82207c478bd9Sstevel@tonic-gate {
82217c478bd9Sstevel@tonic-gate 	return (open(".", O_RDONLY));
82227c478bd9Sstevel@tonic-gate }
82237c478bd9Sstevel@tonic-gate 
82247c478bd9Sstevel@tonic-gate static void
82257c478bd9Sstevel@tonic-gate rest_cwd(int cwd)
82267c478bd9Sstevel@tonic-gate {
82277c478bd9Sstevel@tonic-gate 	(void) fchdir(cwd);
82287c478bd9Sstevel@tonic-gate 	(void) close(cwd);
82297c478bd9Sstevel@tonic-gate }
82307c478bd9Sstevel@tonic-gate 
82317c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
82327c478bd9Sstevel@tonic-gate static void
82337c478bd9Sstevel@tonic-gate xattrs_out(int (*func)())
82347c478bd9Sstevel@tonic-gate {
82357c478bd9Sstevel@tonic-gate 	int dirpfd;
82367c478bd9Sstevel@tonic-gate 	int filefd;
8237ced83f9bSceastha 	int arc_rwsysattr = 0;
8238ced83f9bSceastha 	int rw_sysattr = 0;
8239ced83f9bSceastha 	int ext_attr = 0;
82407c478bd9Sstevel@tonic-gate 	DIR *dirp;
82417c478bd9Sstevel@tonic-gate 	struct dirent *dp;
82427c478bd9Sstevel@tonic-gate 	int slen;
8243ced83f9bSceastha 	int plen;
82447c478bd9Sstevel@tonic-gate 	char *namep, *savenamep;
8245ced83f9bSceastha 	char *apathp;
8246ced83f9bSceastha 	char *attrparent = Gen.g_attrparent_p;
8247ced83f9bSceastha 	char *filename;
82487c478bd9Sstevel@tonic-gate 
8249ced83f9bSceastha 	if (attrparent == NULL) {
8250ced83f9bSceastha 		filename = Gen.g_nam_p;
8251ced83f9bSceastha 	} else {
8252ced83f9bSceastha 		filename = Gen.g_attrnam_p;
8253ced83f9bSceastha 	}
8254ced83f9bSceastha 
8255ced83f9bSceastha 	/*
8256ced83f9bSceastha 	 * If the underlying file system supports it, then
8257ced83f9bSceastha 	 * archive the extended attributes if -@ was specified,
8258ced83f9bSceastha 	 * and the extended system attributes if -/ was
8259ced83f9bSceastha 	 * specified.
8260ced83f9bSceastha 	 */
8261ced83f9bSceastha 	if (verify_attr_support(filename, (attrparent == NULL), ARC_CREATE,
8262ced83f9bSceastha 	    &ext_attr) != ATTR_OK) {
82637c478bd9Sstevel@tonic-gate 		return;
82647c478bd9Sstevel@tonic-gate 	}
82657c478bd9Sstevel@tonic-gate 
8266ced83f9bSceastha #if defined(_PC_SATTR_ENABLED)
8267ced83f9bSceastha 	if (SysAtflag) {
8268ced83f9bSceastha 		int		filefd;
8269ced83f9bSceastha 		nvlist_t 	*slist = NULL;
8270ced83f9bSceastha 
8271ced83f9bSceastha 		/*
8272ced83f9bSceastha 		 * Determine if there are non-transient system
8273ced83f9bSceastha 		 * attributes.
8274ced83f9bSceastha 		 */
8275ced83f9bSceastha 		errno = 0;
8276ced83f9bSceastha 		if ((filefd = open(filename, O_RDONLY)) == -1) {
8277ced83f9bSceastha 			if (attrparent == NULL) {
8278ced83f9bSceastha 				msg(EXTN,
8279ced83f9bSceastha 				    "unable to open %s%s%sfile %s",
8280ced83f9bSceastha 				    (attrparent == NULL) ? "" :
8281ced83f9bSceastha 				    gettext("attribute "),
8282ced83f9bSceastha 				    (attrparent == NULL) ? "" : attrparent,
8283ced83f9bSceastha 				    (attrparent == NULL) ? "" : gettext(" of "),
8284ced83f9bSceastha 				    (attrparent == NULL) ? G_p->g_nam_p :
8285ced83f9bSceastha 				    G_p->g_attrfnam_p);
8286ced83f9bSceastha 			}
8287ced83f9bSceastha 		}
8288ced83f9bSceastha 		if (((slist = sysattr_list(myname, filefd,
8289ced83f9bSceastha 		    filename)) != NULL) || (errno != 0)) {
8290ced83f9bSceastha 			arc_rwsysattr = 1;
8291ced83f9bSceastha 		}
8292ced83f9bSceastha 		if (slist != NULL) {
8293ced83f9bSceastha 			(void) nvlist_free(slist);
8294ced83f9bSceastha 			slist = NULL;
8295ced83f9bSceastha 		}
8296ced83f9bSceastha 		(void) close(filefd);
8297ced83f9bSceastha 	}
8298ced83f9bSceastha 
8299ced83f9bSceastha 	/*
8300ced83f9bSceastha 	 * If we aren't archiving extended system attributes, and we are
8301ced83f9bSceastha 	 * processing an attribute, or if we are archiving extended system
8302ced83f9bSceastha 	 * attributes, and there are are no extended attributes, then there's
8303ced83f9bSceastha 	 * no need to open up the attribute directory of the file unless the
8304ced83f9bSceastha 	 * extended system attributes are not transient (i.e, the system
8305ced83f9bSceastha 	 * attributes are not the default values).
8306ced83f9bSceastha 	 */
8307ced83f9bSceastha 	if ((arc_rwsysattr == 0) && ((attrparent != NULL) ||
8308ced83f9bSceastha 	    (SysAtflag && !ext_attr))) {
8309ced83f9bSceastha 		return;
8310ced83f9bSceastha 	}
8311ced83f9bSceastha 
8312ced83f9bSceastha #endif	/* _PC_SATTR_ENABLED */
8313ced83f9bSceastha 
83147c478bd9Sstevel@tonic-gate 	/*
83157c478bd9Sstevel@tonic-gate 	 * If aclp still exists then free it since it is was set when base
83167c478bd9Sstevel@tonic-gate 	 * file was extracted.
83177c478bd9Sstevel@tonic-gate 	 */
8318fa9e4066Sahrens 	if (aclp != NULL) {
8319fa9e4066Sahrens 		acl_free(aclp);
83207c478bd9Sstevel@tonic-gate 		aclp = NULL;
8321fa9e4066Sahrens 		acl_is_set = 0;
83227c478bd9Sstevel@tonic-gate 	}
83237c478bd9Sstevel@tonic-gate 
8324ced83f9bSceastha 	Gen.g_dirfd = attropen(filename, ".", O_RDONLY);
83257c478bd9Sstevel@tonic-gate 	if (Gen.g_dirfd == -1) {
8326ced83f9bSceastha 		msg(ERRN, "Cannot open attribute directory of file \"%s%s%s\"",
8327ced83f9bSceastha 		    (attrparent == NULL) ? "" : gettext("attribute "),
8328ced83f9bSceastha 		    (attrparent == NULL) ? "" : attrparent,
8329ced83f9bSceastha 		    (attrparent == NULL) ? "" : gettext(" of "), filename);
83307c478bd9Sstevel@tonic-gate 		return;
83317c478bd9Sstevel@tonic-gate 
83327c478bd9Sstevel@tonic-gate 	}
8333ced83f9bSceastha 
8334ced83f9bSceastha 	if (attrparent == NULL) {
83357c478bd9Sstevel@tonic-gate 		savenamep = G_p->g_nam_p;
8336ced83f9bSceastha 	} else {
8337ced83f9bSceastha 		savenamep = G_p->g_attrfnam_p;
8338ced83f9bSceastha 	}
83397c478bd9Sstevel@tonic-gate 
83407c478bd9Sstevel@tonic-gate 	if ((dirpfd = dup(Gen.g_dirfd)) == -1)  {
83417c478bd9Sstevel@tonic-gate 		msg(ERRN, "Cannot dup(2) attribute directory descriptor");
83427c478bd9Sstevel@tonic-gate 		return;
83437c478bd9Sstevel@tonic-gate 	}
83447c478bd9Sstevel@tonic-gate 
8345ced83f9bSceastha 	if ((dirp = fdopendir(dirpfd)) == NULL) {
83467c478bd9Sstevel@tonic-gate 		msg(ERRN, "Cannot fdopendir(2) directory file descriptor");
83477c478bd9Sstevel@tonic-gate 		return;
83487c478bd9Sstevel@tonic-gate 	}
83497c478bd9Sstevel@tonic-gate 
8350ced83f9bSceastha 	if (attrparent == NULL) {
8351ced83f9bSceastha 		Gen.g_baseparent_fd = save_cwd();
8352ced83f9bSceastha 	}
8353ced83f9bSceastha 
8354ced83f9bSceastha 	while ((dp = readdir(dirp)) != NULL) {
8355ced83f9bSceastha 		if (strcmp(dp->d_name, "..") == 0) {
8356ced83f9bSceastha 			continue;
8357ced83f9bSceastha 		}
8358ced83f9bSceastha 		if (verify_attr(dp->d_name, attrparent,
8359ced83f9bSceastha 		    arc_rwsysattr, &rw_sysattr) != ATTR_OK) {
83607c478bd9Sstevel@tonic-gate 			continue;
83617c478bd9Sstevel@tonic-gate 		}
83627c478bd9Sstevel@tonic-gate 
83637c478bd9Sstevel@tonic-gate 		if (strcmp(dp->d_name, ".") == 0) {
83647c478bd9Sstevel@tonic-gate 			Hiddendir = 1;
83657c478bd9Sstevel@tonic-gate 		} else {
83667c478bd9Sstevel@tonic-gate 			Hiddendir = 0;
83677c478bd9Sstevel@tonic-gate 		}
83687c478bd9Sstevel@tonic-gate 
8369ced83f9bSceastha 		Gen.g_rw_sysattr = rw_sysattr;
83707c478bd9Sstevel@tonic-gate 		Gen.g_attrnam_p = dp->d_name;
83717c478bd9Sstevel@tonic-gate 
83727c478bd9Sstevel@tonic-gate 		if (STAT(Gen.g_dirfd, Gen.g_nam_p, &SrcSt) == -1) {
83737c478bd9Sstevel@tonic-gate 			msg(ERRN,
83747c478bd9Sstevel@tonic-gate 			    "Could not fstatat(2) attribute \"%s\" of"
8375ced83f9bSceastha 			    " file \"%s\"", dp->d_name, (attrparent == NULL) ?
8376ced83f9bSceastha 			    savenamep : Gen.g_attrfnam_p);
83777c478bd9Sstevel@tonic-gate 			continue;
83787c478bd9Sstevel@tonic-gate 		}
83797c478bd9Sstevel@tonic-gate 
83807c478bd9Sstevel@tonic-gate 		if (Use_old_stat) {
8381ced83f9bSceastha 			Savedev = SrcSt.st_dev;
83827c478bd9Sstevel@tonic-gate 			OldSt = convert_to_old_stat(&SrcSt,
83837c478bd9Sstevel@tonic-gate 			    Gen.g_nam_p, Gen.g_attrnam_p);
83847c478bd9Sstevel@tonic-gate 
83857c478bd9Sstevel@tonic-gate 			if (OldSt == NULL) {
83867c478bd9Sstevel@tonic-gate 				msg(ERRN,
83877c478bd9Sstevel@tonic-gate 				    "Could not convert to old stat format");
83887c478bd9Sstevel@tonic-gate 				continue;
83897c478bd9Sstevel@tonic-gate 			}
83907c478bd9Sstevel@tonic-gate 		}
83917c478bd9Sstevel@tonic-gate 
83927c478bd9Sstevel@tonic-gate 		Gen.g_attrfnam_p = savenamep;
83937c478bd9Sstevel@tonic-gate 
83947c478bd9Sstevel@tonic-gate 		/*
83957c478bd9Sstevel@tonic-gate 		 * Set up dummy header name
83967c478bd9Sstevel@tonic-gate 		 *
83977c478bd9Sstevel@tonic-gate 		 * One piece is written with .hdr, which
83987c478bd9Sstevel@tonic-gate 		 * contains the actual xattr hdr or pathing information
83997c478bd9Sstevel@tonic-gate 		 * then the name is updated to drop the .hdr off
84007c478bd9Sstevel@tonic-gate 		 * and the actual file itself is archived.
84017c478bd9Sstevel@tonic-gate 		 */
84027c478bd9Sstevel@tonic-gate 		slen = strlen(Gen.g_attrnam_p) + strlen(DEVNULL) +
8403ced83f9bSceastha 		    strlen(XATTRHDR) + 2;	/* add one for '/' */
8404ced83f9bSceastha 		if ((namep = e_zalloc(E_NORMAL, slen)) == NULL) {
84057c478bd9Sstevel@tonic-gate 			msg(ERRN, "Could not calloc memory for attribute name");
84067c478bd9Sstevel@tonic-gate 			continue;
84077c478bd9Sstevel@tonic-gate 		}
8408ced83f9bSceastha 		(void) snprintf(namep, slen, "%s/%s%s",
84097c478bd9Sstevel@tonic-gate 		    DEVNULL, Gen.g_attrnam_p, XATTRHDR);
84107c478bd9Sstevel@tonic-gate 		Gen.g_nam_p = namep;
84117c478bd9Sstevel@tonic-gate 
8412ced83f9bSceastha 		plen = strlen(Gen.g_attrnam_p) + 1;
8413ced83f9bSceastha 		if (Gen.g_attrparent_p != NULL) {
8414ced83f9bSceastha 			plen += strlen(Gen.g_attrparent_p) + 1;
8415ced83f9bSceastha 		}
8416ced83f9bSceastha 		if ((apathp = e_zalloc(E_NORMAL, plen)) == NULL) {
8417ced83f9bSceastha 			msg(ERRN, "Could not calloc memory for attribute name");
8418ced83f9bSceastha 			continue;
8419ced83f9bSceastha 		}
8420ced83f9bSceastha 		(void) snprintf(apathp, plen, "%s%s%s",
8421ced83f9bSceastha 		    (Gen.g_attrparent_p == NULL) ? "" : Gen.g_attrparent_p,
8422ced83f9bSceastha 		    (Gen.g_attrparent_p == NULL) ? "" : "/", Gen.g_attrnam_p);
8423ced83f9bSceastha 
8424ced83f9bSceastha 		if (Gen.g_attrpath_p != NULL) {
8425ced83f9bSceastha 			free(Gen.g_attrpath_p);
8426ced83f9bSceastha 		}
8427ced83f9bSceastha 		Gen.g_attrpath_p = apathp;
8428ced83f9bSceastha 
84297c478bd9Sstevel@tonic-gate 		/*
84307c478bd9Sstevel@tonic-gate 		 * Get attribute's ACL info: don't bother allocating space
84317c478bd9Sstevel@tonic-gate 		 * if there are only standard permissions, i.e. ACL count < 4
84327c478bd9Sstevel@tonic-gate 		 */
84337c478bd9Sstevel@tonic-gate 		if (Pflag) {
84347c478bd9Sstevel@tonic-gate 			filefd = openat(Gen.g_dirfd, dp->d_name, O_RDONLY);
84357c478bd9Sstevel@tonic-gate 			if (filefd == -1) {
84367c478bd9Sstevel@tonic-gate 				msg(ERRN,
84377c478bd9Sstevel@tonic-gate 				    "Could not open attribute \"%s\" of"
84387c478bd9Sstevel@tonic-gate 				    " file \"%s\"", dp->d_name, savenamep);
84397c478bd9Sstevel@tonic-gate 				free(namep);
84407c478bd9Sstevel@tonic-gate 				continue;
84417c478bd9Sstevel@tonic-gate 			}
8442fa9e4066Sahrens 			if (facl_get(filefd, ACL_NO_TRIVIAL, &aclp) != 0) {
84437c478bd9Sstevel@tonic-gate 				msg(ERRN,
84447c478bd9Sstevel@tonic-gate 				    "Error with acl() on %s",
84457c478bd9Sstevel@tonic-gate 				    Gen.g_nam_p);
84467c478bd9Sstevel@tonic-gate 			}
84477c478bd9Sstevel@tonic-gate 			(void) close(filefd);
84487c478bd9Sstevel@tonic-gate 		}
8449ced83f9bSceastha 
84507c478bd9Sstevel@tonic-gate 		(void) creat_hdr();
84517c478bd9Sstevel@tonic-gate 		(void) (*func)();
8452ced83f9bSceastha 
8453ced83f9bSceastha #if defined(_PC_SATTR_ENABLED)
8454ced83f9bSceastha 		/*
8455ced83f9bSceastha 		 * Recursively call xattrs_out() to process the attribute's
8456ced83f9bSceastha 		 * hidden attribute directory and read-write system attributes.
8457ced83f9bSceastha 		 */
8458ced83f9bSceastha 		if (SysAtflag && !Hiddendir && !rw_sysattr) {
8459ced83f9bSceastha 			int	savedirfd = Gen.g_dirfd;
8460ced83f9bSceastha 
8461ced83f9bSceastha 			(void) fchdir(Gen.g_dirfd);
8462ced83f9bSceastha 			Gen.g_attrparent_p = dp->d_name;
8463ced83f9bSceastha 			xattrs_out(func);
8464ced83f9bSceastha 			Gen.g_dirfd = savedirfd;
8465ced83f9bSceastha 			Gen.g_attrparent_p = NULL;
8466ced83f9bSceastha 		}
8467ced83f9bSceastha #endif	/* _PC_SATTR_ENABLED */
8468ced83f9bSceastha 
84697c478bd9Sstevel@tonic-gate 		if (Gen.g_passdirfd != -1) {
84707c478bd9Sstevel@tonic-gate 			(void) close(Gen.g_passdirfd);
84717c478bd9Sstevel@tonic-gate 			Gen.g_passdirfd = -1;
84727c478bd9Sstevel@tonic-gate 		}
8473ced83f9bSceastha 		Gen.g_attrnam_p = NULL;
8474ced83f9bSceastha 		Gen.g_attrfnam_p = NULL;
8475ced83f9bSceastha 		Gen.g_linktoattrfnam_p = NULL;
8476ced83f9bSceastha 		Gen.g_linktoattrnam_p = NULL;
8477ced83f9bSceastha 		Gen.g_rw_sysattr = 0;
8478ced83f9bSceastha 		if (Gen.g_attrpath_p != NULL) {
8479ced83f9bSceastha 			free(Gen.g_attrpath_p);
8480ced83f9bSceastha 			Gen.g_attrpath_p = NULL;
8481ced83f9bSceastha 		}
8482ced83f9bSceastha 
8483fa9e4066Sahrens 		if (aclp != NULL) {
8484fa9e4066Sahrens 			acl_free(aclp);
84857c478bd9Sstevel@tonic-gate 			aclp = NULL;
8486fa9e4066Sahrens 			acl_is_set = 0;
84877c478bd9Sstevel@tonic-gate 		}
84887c478bd9Sstevel@tonic-gate 		free(namep);
84897c478bd9Sstevel@tonic-gate 	}
84907c478bd9Sstevel@tonic-gate 
84917c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
84927c478bd9Sstevel@tonic-gate 	(void) close(Gen.g_dirfd);
8493ced83f9bSceastha 	if (attrparent == NULL) {
8494ced83f9bSceastha 		rest_cwd(Gen.g_baseparent_fd);
84957c478bd9Sstevel@tonic-gate 		Gen.g_dirfd = -1;
84967c478bd9Sstevel@tonic-gate 	}
8497ced83f9bSceastha 	Hiddendir = 0;
8498ced83f9bSceastha }
84997c478bd9Sstevel@tonic-gate #else
85007c478bd9Sstevel@tonic-gate static void
85017c478bd9Sstevel@tonic-gate xattrs_out(int (*func)())
85027c478bd9Sstevel@tonic-gate {
85037c478bd9Sstevel@tonic-gate }
85047c478bd9Sstevel@tonic-gate #endif
85057c478bd9Sstevel@tonic-gate 
85067c478bd9Sstevel@tonic-gate /*
85077c478bd9Sstevel@tonic-gate  * Return the parent directory of a given path.
85087c478bd9Sstevel@tonic-gate  *
85097c478bd9Sstevel@tonic-gate  * Examples:
85107c478bd9Sstevel@tonic-gate  * /usr/tmp return /usr
85117c478bd9Sstevel@tonic-gate  * /usr/tmp/file return /usr/tmp
85127c478bd9Sstevel@tonic-gate  * /  returns .
85137c478bd9Sstevel@tonic-gate  * /usr returns /
85147c478bd9Sstevel@tonic-gate  * file returns .
85157c478bd9Sstevel@tonic-gate  *
85167c478bd9Sstevel@tonic-gate  * dir is assumed to be at least as big as path.
85177c478bd9Sstevel@tonic-gate  */
85187c478bd9Sstevel@tonic-gate static void
85197c478bd9Sstevel@tonic-gate get_parent(char *path, char *dir)
85207c478bd9Sstevel@tonic-gate {
85217c478bd9Sstevel@tonic-gate 	char *s;
85227c478bd9Sstevel@tonic-gate 	char tmpdir[PATH_MAX + 1];
85237c478bd9Sstevel@tonic-gate 
85247c478bd9Sstevel@tonic-gate 	if (strlen(path) > PATH_MAX) {
85257c478bd9Sstevel@tonic-gate 		msg(EXT, "pathname is too long");
85267c478bd9Sstevel@tonic-gate 	}
85277c478bd9Sstevel@tonic-gate 	(void) strcpy(tmpdir, path);
85287c478bd9Sstevel@tonic-gate 	chop_endslashes(tmpdir);
85297c478bd9Sstevel@tonic-gate 
85307c478bd9Sstevel@tonic-gate 	if ((s = strrchr(tmpdir, '/')) == NULL) {
85317c478bd9Sstevel@tonic-gate 		(void) strcpy(dir, ".");
85327c478bd9Sstevel@tonic-gate 	} else {
85337c478bd9Sstevel@tonic-gate 		s = skipslashes(s, tmpdir);
85347c478bd9Sstevel@tonic-gate 		*s = '\0';
85357c478bd9Sstevel@tonic-gate 		if (s == tmpdir)
85367c478bd9Sstevel@tonic-gate 			(void) strcpy(dir, "/");
85377c478bd9Sstevel@tonic-gate 		else
85387c478bd9Sstevel@tonic-gate 			(void) strcpy(dir, tmpdir);
85397c478bd9Sstevel@tonic-gate 	}
85407c478bd9Sstevel@tonic-gate }
85417c478bd9Sstevel@tonic-gate 
85427c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
85437c478bd9Sstevel@tonic-gate #define	ROUNDTOTBLOCK(a)		((a + (TBLOCK -1)) & ~(TBLOCK -1))
85447c478bd9Sstevel@tonic-gate 
85457c478bd9Sstevel@tonic-gate static void
85467c478bd9Sstevel@tonic-gate prepare_xattr_hdr(
85477c478bd9Sstevel@tonic-gate 	char		**attrbuf,
85487c478bd9Sstevel@tonic-gate 	char		*filename,
8549ced83f9bSceastha 	char		*attrpath,
85507c478bd9Sstevel@tonic-gate 	char		typeflag,
85517c478bd9Sstevel@tonic-gate 	struct Lnk	*linkinfo,
85527c478bd9Sstevel@tonic-gate 	int		*rlen)
85537c478bd9Sstevel@tonic-gate {
85547c478bd9Sstevel@tonic-gate 	char			*bufhead;	/* ptr to full buffer */
8555ced83f9bSceastha 	char			*aptr;
85567c478bd9Sstevel@tonic-gate 	struct xattr_hdr 	*hptr;		/* ptr to header in bufhead */
85577c478bd9Sstevel@tonic-gate 	struct xattr_buf	*tptr;		/* ptr to pathing pieces */
85587c478bd9Sstevel@tonic-gate 	int			totalen;	/* total buffer length */
85597c478bd9Sstevel@tonic-gate 	int			len;		/* length returned to user */
85607c478bd9Sstevel@tonic-gate 	int			stringlen;	/* length of filename + attr */
8561ced83f9bSceastha 						/*
8562ced83f9bSceastha 						 * length of filename + attr
8563ced83f9bSceastha 						 * in link section
8564ced83f9bSceastha 						 */
8565ced83f9bSceastha 	int			linkstringlen;
85667c478bd9Sstevel@tonic-gate 	int			complen;	/* length of pathing section */
85677c478bd9Sstevel@tonic-gate 	int			linklen;	/* length of link section */
8568ced83f9bSceastha 	int			attrnames_index; /* attrnames starting index */
85697c478bd9Sstevel@tonic-gate 
85707c478bd9Sstevel@tonic-gate 	/*
85717c478bd9Sstevel@tonic-gate 	 * Release previous buffer if any.
85727c478bd9Sstevel@tonic-gate 	 */
85737c478bd9Sstevel@tonic-gate 
8574ced83f9bSceastha 	if (*attrbuf != NULL) {
85757c478bd9Sstevel@tonic-gate 		free(*attrbuf);
85767c478bd9Sstevel@tonic-gate 		*attrbuf = NULL;
85777c478bd9Sstevel@tonic-gate 	}
85787c478bd9Sstevel@tonic-gate 
85797c478bd9Sstevel@tonic-gate 	/*
85807c478bd9Sstevel@tonic-gate 	 * First add in fixed size stuff
85817c478bd9Sstevel@tonic-gate 	 */
85827c478bd9Sstevel@tonic-gate 	len = sizeof (struct xattr_hdr) + sizeof (struct xattr_buf);
85837c478bd9Sstevel@tonic-gate 
85847c478bd9Sstevel@tonic-gate 	/*
85857c478bd9Sstevel@tonic-gate 	 * Add space for two nulls
85867c478bd9Sstevel@tonic-gate 	 */
8587ced83f9bSceastha 	stringlen = strlen(attrpath) + strlen(filename) + 2;
85887c478bd9Sstevel@tonic-gate 	complen = stringlen + sizeof (struct xattr_buf);
85897c478bd9Sstevel@tonic-gate 
85907c478bd9Sstevel@tonic-gate 	len += stringlen;
85917c478bd9Sstevel@tonic-gate 
85927c478bd9Sstevel@tonic-gate 	/*
85937c478bd9Sstevel@tonic-gate 	 * Now add on space for link info if any
85947c478bd9Sstevel@tonic-gate 	 */
85957c478bd9Sstevel@tonic-gate 
85967c478bd9Sstevel@tonic-gate 	if (linkinfo != NULL) {
85977c478bd9Sstevel@tonic-gate 		/*
85987c478bd9Sstevel@tonic-gate 		 * Again add space for two nulls
85997c478bd9Sstevel@tonic-gate 		 */
86007c478bd9Sstevel@tonic-gate 		linkstringlen = strlen(linkinfo->L_gen.g_attrfnam_p) +
86017c478bd9Sstevel@tonic-gate 		    strlen(linkinfo->L_gen.g_attrnam_p) + 2;
8602ced83f9bSceastha 		linklen = linkstringlen + sizeof (struct xattr_buf);
8603ced83f9bSceastha 		len += linklen;
8604ced83f9bSceastha 	} else {
8605ced83f9bSceastha 		linklen = 0;
86067c478bd9Sstevel@tonic-gate 	}
86077c478bd9Sstevel@tonic-gate 
86087c478bd9Sstevel@tonic-gate 	/*
86097c478bd9Sstevel@tonic-gate 	 * Now add padding to end to fill out TBLOCK
86107c478bd9Sstevel@tonic-gate 	 *
86117c478bd9Sstevel@tonic-gate 	 * Function returns size of real data and not size + padding.
86127c478bd9Sstevel@tonic-gate 	 */
86137c478bd9Sstevel@tonic-gate 
86147c478bd9Sstevel@tonic-gate 	totalen = ROUNDTOTBLOCK(len);
86157c478bd9Sstevel@tonic-gate 	bufhead = e_zalloc(E_EXIT, totalen);
86167c478bd9Sstevel@tonic-gate 
86177c478bd9Sstevel@tonic-gate 	/*
86187c478bd9Sstevel@tonic-gate 	 * Now we can fill in the necessary pieces
86197c478bd9Sstevel@tonic-gate 	 */
86207c478bd9Sstevel@tonic-gate 
86217c478bd9Sstevel@tonic-gate 	/*
86227c478bd9Sstevel@tonic-gate 	 * first fill in the fixed header
86237c478bd9Sstevel@tonic-gate 	 */
86247c478bd9Sstevel@tonic-gate 	hptr = (struct xattr_hdr *)bufhead;
86255fbb8099SNobutomo Nakano 	(void) strcpy(hptr->h_version, XATTR_ARCH_VERS);
86267c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_component_len, "%0*d",
86277c478bd9Sstevel@tonic-gate 	    sizeof (hptr->h_component_len) - 1, complen);
86287c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_link_component_len, "%0*d",
86297c478bd9Sstevel@tonic-gate 	    sizeof (hptr->h_link_component_len) - 1, linklen);
86307c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_size, "%0*d", sizeof (hptr->h_size) - 1, len);
86317c478bd9Sstevel@tonic-gate 
86327c478bd9Sstevel@tonic-gate 	/*
86337c478bd9Sstevel@tonic-gate 	 * Now fill in the filename + attrnames section
8634ced83f9bSceastha 	 * The filename and attrnames section can be composed of two or more
8635ced83f9bSceastha 	 * path segments separated by a null character.  The first segment
8636ced83f9bSceastha 	 * is the path to the parent file that roots the entire sequence in
8637ced83f9bSceastha 	 * the normal name space. The remaining segments describes a path
8638ced83f9bSceastha 	 * rooted at the hidden extended attribute directory of the leaf file of
8639ced83f9bSceastha 	 * the previous segment, making it possible to name attributes on
8640ced83f9bSceastha 	 * attributes.  Thus, if we are just archiving an extended attribute,
8641ced83f9bSceastha 	 * the second segment will contain the attribute name.  If we are
8642ced83f9bSceastha 	 * archiving a system attribute of an extended attribute, then the
8643ced83f9bSceastha 	 * second segment will contain the attribute name, and a third segment
8644ced83f9bSceastha 	 * will contain the system attribute name.  The attribute pathing
8645ced83f9bSceastha 	 * information is obtained from 'attrpath'.
86467c478bd9Sstevel@tonic-gate 	 */
86477c478bd9Sstevel@tonic-gate 
86487c478bd9Sstevel@tonic-gate 	tptr = (struct xattr_buf *)(bufhead + sizeof (struct xattr_hdr));
86497c478bd9Sstevel@tonic-gate 	(void) sprintf(tptr->h_namesz, "%0*d", sizeof (tptr->h_namesz) - 1,
86507c478bd9Sstevel@tonic-gate 	    stringlen);
86517c478bd9Sstevel@tonic-gate 	(void) strcpy(tptr->h_names, filename);
8652ced83f9bSceastha 	attrnames_index = strlen(filename) + 1;
8653ced83f9bSceastha 	(void) strcpy(&tptr->h_names[attrnames_index], attrpath);
86547c478bd9Sstevel@tonic-gate 	tptr->h_typeflag = typeflag;
86557c478bd9Sstevel@tonic-gate 
86567c478bd9Sstevel@tonic-gate 	/*
8657ced83f9bSceastha 	 * Split the attrnames section into two segments if 'attrpath'
8658ced83f9bSceastha 	 * contains pathing information for a system attribute of an
8659ced83f9bSceastha 	 * extended attribute.  We split them by replacing the '/' with
8660ced83f9bSceastha 	 * a '\0'.
8661ced83f9bSceastha 	 */
8662ced83f9bSceastha 	if ((aptr = strpbrk(&tptr->h_names[attrnames_index], "/")) != NULL) {
8663ced83f9bSceastha 		*aptr = '\0';
8664ced83f9bSceastha 	}
8665ced83f9bSceastha 
8666ced83f9bSceastha 	/*
86677c478bd9Sstevel@tonic-gate 	 * Now fill in the optional link section if we have one
86687c478bd9Sstevel@tonic-gate 	 */
86697c478bd9Sstevel@tonic-gate 
8670ced83f9bSceastha 	if (linkinfo != NULL) {
86717c478bd9Sstevel@tonic-gate 		tptr = (struct xattr_buf *)(bufhead +
86727c478bd9Sstevel@tonic-gate 		    sizeof (struct xattr_hdr) + complen);
86737c478bd9Sstevel@tonic-gate 
86747c478bd9Sstevel@tonic-gate 		(void) sprintf(tptr->h_namesz, "%0*d",
86757c478bd9Sstevel@tonic-gate 		    sizeof (tptr->h_namesz) - 1, linkstringlen);
86767c478bd9Sstevel@tonic-gate 		(void) strcpy(tptr->h_names, linkinfo->L_gen.g_attrfnam_p);
86777c478bd9Sstevel@tonic-gate 		(void) strcpy(
86787c478bd9Sstevel@tonic-gate 		    &tptr->h_names[strlen(linkinfo->L_gen.g_attrfnam_p) + 1],
86797c478bd9Sstevel@tonic-gate 		    linkinfo->L_gen.g_attrnam_p);
86807c478bd9Sstevel@tonic-gate 		tptr->h_typeflag = typeflag;
86817c478bd9Sstevel@tonic-gate 	}
86827c478bd9Sstevel@tonic-gate 	*attrbuf = (char *)bufhead;
86837c478bd9Sstevel@tonic-gate 	*rlen = len;
86847c478bd9Sstevel@tonic-gate }
86857c478bd9Sstevel@tonic-gate #endif	/* O_XATTR */
86867c478bd9Sstevel@tonic-gate 
86877c478bd9Sstevel@tonic-gate static char
86887c478bd9Sstevel@tonic-gate tartype(int type)
86897c478bd9Sstevel@tonic-gate {
86907c478bd9Sstevel@tonic-gate 	switch (type) {
86917c478bd9Sstevel@tonic-gate 
86927c478bd9Sstevel@tonic-gate 	case S_IFDIR:
86937c478bd9Sstevel@tonic-gate 		return (DIRTYPE);
86947c478bd9Sstevel@tonic-gate 
86957c478bd9Sstevel@tonic-gate 	case S_IFLNK:
86967c478bd9Sstevel@tonic-gate 		return (SYMTYPE);
86977c478bd9Sstevel@tonic-gate 
86987c478bd9Sstevel@tonic-gate 	case S_IFIFO:
86997c478bd9Sstevel@tonic-gate 		return (FIFOTYPE);
87007c478bd9Sstevel@tonic-gate 
87017c478bd9Sstevel@tonic-gate 	case S_IFCHR:
87027c478bd9Sstevel@tonic-gate 		return (CHRTYPE);
87037c478bd9Sstevel@tonic-gate 
87047c478bd9Sstevel@tonic-gate 	case S_IFBLK:
87057c478bd9Sstevel@tonic-gate 		return (BLKTYPE);
87067c478bd9Sstevel@tonic-gate 
87077c478bd9Sstevel@tonic-gate 	case S_IFREG:
87087c478bd9Sstevel@tonic-gate 		return (REGTYPE);
87097c478bd9Sstevel@tonic-gate 
87107c478bd9Sstevel@tonic-gate 	default:
87117c478bd9Sstevel@tonic-gate 		return ('\0');
87127c478bd9Sstevel@tonic-gate 	}
87137c478bd9Sstevel@tonic-gate }
87147c478bd9Sstevel@tonic-gate 
87157c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
87167c478bd9Sstevel@tonic-gate static int
87177c478bd9Sstevel@tonic-gate openfile(int omode)
87187c478bd9Sstevel@tonic-gate {
8719ced83f9bSceastha 	if (G_p->g_attrnam_p != NULL) {
8720ced83f9bSceastha 		return (openat(G_p->g_dirfd, G_p->g_attrnam_p, omode));
87217c478bd9Sstevel@tonic-gate 	} else {
87227c478bd9Sstevel@tonic-gate 		return (openat(G_p->g_dirfd,
87237c478bd9Sstevel@tonic-gate 		    get_component(G_p->g_nam_p), omode));
87247c478bd9Sstevel@tonic-gate 	}
87257c478bd9Sstevel@tonic-gate }
87267c478bd9Sstevel@tonic-gate #else
87277c478bd9Sstevel@tonic-gate static int
87287c478bd9Sstevel@tonic-gate openfile(int omode)
87297c478bd9Sstevel@tonic-gate {
87307c478bd9Sstevel@tonic-gate 	return (openat(G_p->g_dirfd, get_component(G_p->g_nam_p), omode));
87317c478bd9Sstevel@tonic-gate }
87327c478bd9Sstevel@tonic-gate #endif
87337c478bd9Sstevel@tonic-gate 
87347c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
87357c478bd9Sstevel@tonic-gate static int
87367c478bd9Sstevel@tonic-gate read_xattr_hdr()
87377c478bd9Sstevel@tonic-gate {
87387c478bd9Sstevel@tonic-gate 	off_t		bytes;
87397c478bd9Sstevel@tonic-gate 	int		comp_len, link_len;
87407c478bd9Sstevel@tonic-gate 	int		namelen;
8741da6c28aaSamw 	int		asz;
87427c478bd9Sstevel@tonic-gate 	int		cnt;
87437c478bd9Sstevel@tonic-gate 	char		*tp;
8744da6c28aaSamw 	char		*xattrapath;
87457c478bd9Sstevel@tonic-gate 	int		pad;
8746da6c28aaSamw 	int		parentfilelen;
87477c478bd9Sstevel@tonic-gate 
87487c478bd9Sstevel@tonic-gate 	/*
87497c478bd9Sstevel@tonic-gate 	 * Include any padding in the read.  We need to be positioned
87507c478bd9Sstevel@tonic-gate 	 * at beginning of next header.
87517c478bd9Sstevel@tonic-gate 	 */
87527c478bd9Sstevel@tonic-gate 
87537c478bd9Sstevel@tonic-gate 	bytes = Gen.g_filesz;
87547c478bd9Sstevel@tonic-gate 
87557c478bd9Sstevel@tonic-gate 	if ((xattrhead = e_zalloc(E_NORMAL, (size_t)bytes)) == NULL) {
87567c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
87577c478bd9Sstevel@tonic-gate 		    "Insufficient memory for extended attribute\n"));
87587c478bd9Sstevel@tonic-gate 		return (1);
87597c478bd9Sstevel@tonic-gate 	}
87607c478bd9Sstevel@tonic-gate 
87617c478bd9Sstevel@tonic-gate 	tp = (char *)xattrhead;
87627c478bd9Sstevel@tonic-gate 	while (bytes > 0) {
87637c478bd9Sstevel@tonic-gate 		cnt = (int)(bytes > CPIOBSZ) ? CPIOBSZ : bytes;
87647c478bd9Sstevel@tonic-gate 		FILL(cnt);
87657c478bd9Sstevel@tonic-gate 		(void) memcpy(tp, Buffr.b_out_p, cnt);
87667c478bd9Sstevel@tonic-gate 		tp += cnt;
87677c478bd9Sstevel@tonic-gate 		Buffr.b_out_p += cnt;
87687c478bd9Sstevel@tonic-gate 		Buffr.b_cnt -= (off_t)cnt;
87697c478bd9Sstevel@tonic-gate 		bytes -= (off_t)cnt;
87707c478bd9Sstevel@tonic-gate 	}
87717c478bd9Sstevel@tonic-gate 
87727c478bd9Sstevel@tonic-gate 	pad = (Pad_val + 1 - (Gen.g_filesz & Pad_val)) &
87737c478bd9Sstevel@tonic-gate 	    Pad_val;
87747c478bd9Sstevel@tonic-gate 	if (pad != 0) {
87757c478bd9Sstevel@tonic-gate 		FILL(pad);
87767c478bd9Sstevel@tonic-gate 		Buffr.b_out_p += pad;
87777c478bd9Sstevel@tonic-gate 		Buffr.b_cnt -= (off_t)pad;
87787c478bd9Sstevel@tonic-gate 	}
87797c478bd9Sstevel@tonic-gate 
87807c478bd9Sstevel@tonic-gate 	/*
87817c478bd9Sstevel@tonic-gate 	 * Validate that we can handle header format
87827c478bd9Sstevel@tonic-gate 	 */
87837c478bd9Sstevel@tonic-gate 
87847c478bd9Sstevel@tonic-gate 	if (strcmp(xattrhead->h_version, XATTR_ARCH_VERS) != 0) {
87857c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
87867c478bd9Sstevel@tonic-gate 		    gettext("Unknown extended attribute format encountered\n"));
87877c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
87887c478bd9Sstevel@tonic-gate 		    gettext("Disabling extended attribute header parsing\n"));
87897c478bd9Sstevel@tonic-gate 		xattrbadhead = 1;
87907c478bd9Sstevel@tonic-gate 		return (1);
87917c478bd9Sstevel@tonic-gate 	}
87927c478bd9Sstevel@tonic-gate 	(void) sscanf(xattrhead->h_component_len, "%10d", &comp_len);
87937c478bd9Sstevel@tonic-gate 	(void) sscanf(xattrhead->h_link_component_len, "%10d", &link_len);
87947c478bd9Sstevel@tonic-gate 	xattrp = (struct xattr_buf *)(((char *)xattrhead) +
87957c478bd9Sstevel@tonic-gate 	    sizeof (struct xattr_hdr));
87967c478bd9Sstevel@tonic-gate 	(void) sscanf(xattrp->h_namesz, "%7d", &namelen);
87977c478bd9Sstevel@tonic-gate 	if (link_len > 0) {
87987c478bd9Sstevel@tonic-gate 		xattr_linkp = (struct xattr_buf *)((int)xattrp + (int)comp_len);
87997c478bd9Sstevel@tonic-gate 	} else {
88007c478bd9Sstevel@tonic-gate 		xattr_linkp = NULL;
88017c478bd9Sstevel@tonic-gate 	}
88027c478bd9Sstevel@tonic-gate 
8803da6c28aaSamw 	/*
8804da6c28aaSamw 	 * Gather the attribute path from the filename and attrnames section.
8805da6c28aaSamw 	 * The filename and attrnames section can be composed of two or more
8806da6c28aaSamw 	 * path segments separated by a null character.  The first segment
8807da6c28aaSamw 	 * is the path to the parent file that roots the entire sequence in
8808da6c28aaSamw 	 * the normal name space. The remaining segments describes a path
8809da6c28aaSamw 	 * rooted at the hidden extended attribute directory of the leaf file of
8810da6c28aaSamw 	 * the previous segment, making it possible to name attributes on
8811da6c28aaSamw 	 * attributes.
8812da6c28aaSamw 	 */
8813da6c28aaSamw 	parentfilelen = strlen(xattrp->h_names);
8814da6c28aaSamw 	xattrapath = xattrp->h_names + parentfilelen + 1;
8815da6c28aaSamw 	asz = strlen(xattrapath);
8816da6c28aaSamw 	if ((asz + parentfilelen + 2) < namelen) {
8817da6c28aaSamw 		/*
8818da6c28aaSamw 		 * The attrnames section contains a system attribute on an
8819da6c28aaSamw 		 * attribute.  Save the name of the attribute for use later,
8820da6c28aaSamw 		 * and replace the null separating the attribute name from
8821da6c28aaSamw 		 * the system attribute name with a '/' so that xattrapath can
8822da6c28aaSamw 		 * be used to display messages with the full attribute path name
8823da6c28aaSamw 		 * rooted at the hidden attribute directory of the base file
8824da6c28aaSamw 		 * in normal name space.
8825da6c28aaSamw 		 */
8826da6c28aaSamw 		xattrapath[asz] = '/';
8827da6c28aaSamw 	}
8828da6c28aaSamw 
88297c478bd9Sstevel@tonic-gate 	return (0);
88307c478bd9Sstevel@tonic-gate }
88317c478bd9Sstevel@tonic-gate #endif
88327c478bd9Sstevel@tonic-gate 
88337c478bd9Sstevel@tonic-gate static mode_t
88347c478bd9Sstevel@tonic-gate attrmode(char type)
88357c478bd9Sstevel@tonic-gate {
88367c478bd9Sstevel@tonic-gate 	mode_t mode;
88377c478bd9Sstevel@tonic-gate 
88387c478bd9Sstevel@tonic-gate 	switch (type) {
88397c478bd9Sstevel@tonic-gate 	case '\0':
88407c478bd9Sstevel@tonic-gate 	case REGTYPE:
88417c478bd9Sstevel@tonic-gate 	case LNKTYPE:
88427c478bd9Sstevel@tonic-gate 		mode = S_IFREG;
88437c478bd9Sstevel@tonic-gate 		break;
88447c478bd9Sstevel@tonic-gate 
88457c478bd9Sstevel@tonic-gate 	case SYMTYPE:
88467c478bd9Sstevel@tonic-gate 		mode = S_IFLNK;
88477c478bd9Sstevel@tonic-gate 		break;
88487c478bd9Sstevel@tonic-gate 
88497c478bd9Sstevel@tonic-gate 	case CHRTYPE:
88507c478bd9Sstevel@tonic-gate 		mode = S_IFCHR;
88517c478bd9Sstevel@tonic-gate 		break;
88527c478bd9Sstevel@tonic-gate 	case BLKTYPE:
88537c478bd9Sstevel@tonic-gate 		mode = S_IFBLK;
88547c478bd9Sstevel@tonic-gate 		break;
88557c478bd9Sstevel@tonic-gate 	case DIRTYPE:
88567c478bd9Sstevel@tonic-gate 		mode = S_IFDIR;
88577c478bd9Sstevel@tonic-gate 		break;
88587c478bd9Sstevel@tonic-gate 	case FIFOTYPE:
88597c478bd9Sstevel@tonic-gate 		mode = S_IFIFO;
88607c478bd9Sstevel@tonic-gate 		break;
88617c478bd9Sstevel@tonic-gate 	case CONTTYPE:
88627c478bd9Sstevel@tonic-gate 	default:
88637c478bd9Sstevel@tonic-gate 		mode = 0;
88647c478bd9Sstevel@tonic-gate 	}
88657c478bd9Sstevel@tonic-gate 
88667c478bd9Sstevel@tonic-gate 	return (mode);
88677c478bd9Sstevel@tonic-gate }
88687c478bd9Sstevel@tonic-gate 
88697c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
88707c478bd9Sstevel@tonic-gate static char *
88717c478bd9Sstevel@tonic-gate get_component(char *path)
88727c478bd9Sstevel@tonic-gate {
88737c478bd9Sstevel@tonic-gate 	char *ptr;
88747c478bd9Sstevel@tonic-gate 
88757c478bd9Sstevel@tonic-gate 	ptr = strrchr(path, '/');
88767c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
88777c478bd9Sstevel@tonic-gate 		return (path);
88787c478bd9Sstevel@tonic-gate 	} else {
88797c478bd9Sstevel@tonic-gate 		/*
88807c478bd9Sstevel@tonic-gate 		 * Handle trailing slash
88817c478bd9Sstevel@tonic-gate 		 */
88827c478bd9Sstevel@tonic-gate 		if (*(ptr + 1) == '\0')
88837c478bd9Sstevel@tonic-gate 			return (ptr);
88847c478bd9Sstevel@tonic-gate 		else
88857c478bd9Sstevel@tonic-gate 			return (ptr + 1);
88867c478bd9Sstevel@tonic-gate 	}
88877c478bd9Sstevel@tonic-gate }
88887c478bd9Sstevel@tonic-gate #else
88897c478bd9Sstevel@tonic-gate static char *
88907c478bd9Sstevel@tonic-gate get_component(char *path)
88917c478bd9Sstevel@tonic-gate {
88927c478bd9Sstevel@tonic-gate 	return (path);
88937c478bd9Sstevel@tonic-gate }
88947c478bd9Sstevel@tonic-gate #endif
88957c478bd9Sstevel@tonic-gate 
88967c478bd9Sstevel@tonic-gate static int
88977c478bd9Sstevel@tonic-gate open_dir(char *name)
88987c478bd9Sstevel@tonic-gate {
88997c478bd9Sstevel@tonic-gate 	int fd = -1;
89007c478bd9Sstevel@tonic-gate 	int cnt = 0;
89017c478bd9Sstevel@tonic-gate 	char *dir;
89027c478bd9Sstevel@tonic-gate 
89037c478bd9Sstevel@tonic-gate 	dir = e_zalloc(E_EXIT, strlen(name) + 1);
89047c478bd9Sstevel@tonic-gate 
89057c478bd9Sstevel@tonic-gate 	/*
89067c478bd9Sstevel@tonic-gate 	 * open directory; creating missing directories along the way.
89077c478bd9Sstevel@tonic-gate 	 */
89087c478bd9Sstevel@tonic-gate 	get_parent(name, dir);
89097c478bd9Sstevel@tonic-gate 	do {
89107c478bd9Sstevel@tonic-gate 		fd = open(dir, O_RDONLY);
89117c478bd9Sstevel@tonic-gate 		if (fd != -1) {
89127c478bd9Sstevel@tonic-gate 			free(dir);
89137c478bd9Sstevel@tonic-gate 			return (fd);
89147c478bd9Sstevel@tonic-gate 		}
89157c478bd9Sstevel@tonic-gate 		cnt++;
89167c478bd9Sstevel@tonic-gate 	} while (cnt <= 1 && missdir(name) == 0);
89177c478bd9Sstevel@tonic-gate 
89187c478bd9Sstevel@tonic-gate 	free(dir);
89197c478bd9Sstevel@tonic-gate 	return (-1);
89207c478bd9Sstevel@tonic-gate }
89217c478bd9Sstevel@tonic-gate 
89227c478bd9Sstevel@tonic-gate static int
89237c478bd9Sstevel@tonic-gate open_dirfd()
89247c478bd9Sstevel@tonic-gate {
89257c478bd9Sstevel@tonic-gate #ifdef O_XATTR
89267c478bd9Sstevel@tonic-gate 	if ((Args & OCt) == 0) {
89277c478bd9Sstevel@tonic-gate 		close_dirfd();
8928ced83f9bSceastha 		if (G_p->g_attrnam_p != NULL) {
8929ced83f9bSceastha 			int	rw_sysattr;
8930ced83f9bSceastha 
8931ced83f9bSceastha 			/*
8932ced83f9bSceastha 			 * Open the file's attribute directory.
8933ced83f9bSceastha 			 * Change into the base file's starting directory then
8934ced83f9bSceastha 			 * call open_attr_dir() to open the attribute directory
8935ced83f9bSceastha 			 * of either the base file (if G_p->g_attrparent_p is
8936ced83f9bSceastha 			 * NULL) or the attribute (if G_p->g_attrparent_p is
8937ced83f9bSceastha 			 * set) of the base file.
8938ced83f9bSceastha 			 */
8939ced83f9bSceastha 			(void) fchdir(G_p->g_baseparent_fd);
8940ced83f9bSceastha 			(void) open_attr_dir(G_p->g_attrnam_p,
8941ced83f9bSceastha 			    G_p->g_attrfnam_p, G_p->g_baseparent_fd,
8942ced83f9bSceastha 			    (G_p->g_attrparent_p == NULL) ? NULL :
8943ced83f9bSceastha 			    G_p->g_attrparent_p, &G_p->g_dirfd, &rw_sysattr);
8944ced83f9bSceastha 			if (Args & OCi) {
8945ced83f9bSceastha 				int	saveerrno = errno;
8946ced83f9bSceastha 
8947ced83f9bSceastha 				(void) fchdir(G_p->g_baseparent_fd);
8948ced83f9bSceastha 				errno = saveerrno;
89497c478bd9Sstevel@tonic-gate 			}
8950ced83f9bSceastha 			if ((G_p->g_dirfd == -1) && (Args & (OCi | OCp))) {
8951ced83f9bSceastha 				msg(ERRN,
8952ced83f9bSceastha 				    "Cannot open attribute directory "
8953ced83f9bSceastha 				    "of %s%s%sfile \"%s\"",
8954ced83f9bSceastha 				    (G_p->g_attrparent_p == NULL) ? "" :
8955ced83f9bSceastha 				    gettext("attribute \""),
8956ced83f9bSceastha 				    (G_p->g_attrparent_p == NULL) ? "" :
8957ced83f9bSceastha 				    G_p->g_attrparent_p,
8958ced83f9bSceastha 				    (G_p->g_attrparent_p == NULL) ? "" :
8959ced83f9bSceastha 				    gettext("\" of "),
8960ced83f9bSceastha 				    G_p->g_attrfnam_p);
8961ced83f9bSceastha 				return (FILE_PASS_ERR);
89627c478bd9Sstevel@tonic-gate 			}
89637c478bd9Sstevel@tonic-gate 		} else {
89647c478bd9Sstevel@tonic-gate 			G_p->g_dirfd = open_dir(G_p->g_nam_p);
89657c478bd9Sstevel@tonic-gate 			if (G_p->g_dirfd == -1) {
89667c478bd9Sstevel@tonic-gate 				msg(ERRN,
89677c478bd9Sstevel@tonic-gate 				    "Cannot open/create %s", G_p->g_nam_p);
89687c478bd9Sstevel@tonic-gate 				return (1);
89697c478bd9Sstevel@tonic-gate 			}
89707c478bd9Sstevel@tonic-gate 		}
89717c478bd9Sstevel@tonic-gate 	} else {
89727c478bd9Sstevel@tonic-gate 		G_p->g_dirfd = -1;
89737c478bd9Sstevel@tonic-gate 	}
89747c478bd9Sstevel@tonic-gate #else
89757c478bd9Sstevel@tonic-gate 	G_p->g_dirfd = -1;
89767c478bd9Sstevel@tonic-gate #endif
89777c478bd9Sstevel@tonic-gate 	return (0);
89787c478bd9Sstevel@tonic-gate }
89797c478bd9Sstevel@tonic-gate 
89807c478bd9Sstevel@tonic-gate static void
89817c478bd9Sstevel@tonic-gate close_dirfd()
89827c478bd9Sstevel@tonic-gate {
89837c478bd9Sstevel@tonic-gate 	if (G_p->g_dirfd != -1) {
89847c478bd9Sstevel@tonic-gate 		(void) close(G_p->g_dirfd);
89857c478bd9Sstevel@tonic-gate 		G_p->g_dirfd = -1;
89867c478bd9Sstevel@tonic-gate 	}
89877c478bd9Sstevel@tonic-gate }
89887c478bd9Sstevel@tonic-gate 
89897c478bd9Sstevel@tonic-gate static void
89907c478bd9Sstevel@tonic-gate write_xattr_hdr()
89917c478bd9Sstevel@tonic-gate {
89927c478bd9Sstevel@tonic-gate 	char *attrbuf = NULL;
89937c478bd9Sstevel@tonic-gate 	int  attrlen = 0;
89947c478bd9Sstevel@tonic-gate 	char *namep;
89957c478bd9Sstevel@tonic-gate 	struct Lnk *tl_p, *linkinfo;
89967c478bd9Sstevel@tonic-gate 
89977c478bd9Sstevel@tonic-gate 	/*
89987c478bd9Sstevel@tonic-gate 	 * namep was allocated in xattrs_out.  It is big enough to hold
89997c478bd9Sstevel@tonic-gate 	 * either the name + .hdr on the end or just the attr name
90007c478bd9Sstevel@tonic-gate 	 */
90017c478bd9Sstevel@tonic-gate 
90027c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
90037c478bd9Sstevel@tonic-gate 	namep = Gen.g_nam_p;
90047c478bd9Sstevel@tonic-gate 	(void) creat_hdr();
90057c478bd9Sstevel@tonic-gate 
90067c478bd9Sstevel@tonic-gate 	if (Args & OCo) {
90077c478bd9Sstevel@tonic-gate 		linkinfo = NULL;
90087c478bd9Sstevel@tonic-gate 		tl_p = Lnk_hd.L_nxt_p;
90097c478bd9Sstevel@tonic-gate 		while (tl_p != &Lnk_hd) {
90107c478bd9Sstevel@tonic-gate 			if (tl_p->L_gen.g_ino == G_p->g_ino &&
90117c478bd9Sstevel@tonic-gate 			    tl_p->L_gen.g_dev == G_p->g_dev) {
90127c478bd9Sstevel@tonic-gate 					linkinfo = tl_p;
90137c478bd9Sstevel@tonic-gate 					break; /* found */
90147c478bd9Sstevel@tonic-gate 			}
90157c478bd9Sstevel@tonic-gate 			tl_p = tl_p->L_nxt_p;
90167c478bd9Sstevel@tonic-gate 		}
90177c478bd9Sstevel@tonic-gate 		prepare_xattr_hdr(&attrbuf, Gen.g_attrfnam_p,
9018ced83f9bSceastha 		    Gen.g_attrpath_p,
9019ced83f9bSceastha 		    (linkinfo == NULL) ?
90207c478bd9Sstevel@tonic-gate 		    tartype(Gen.g_mode & Ftype) : LNKTYPE,
90217c478bd9Sstevel@tonic-gate 		    linkinfo, &attrlen);
90227c478bd9Sstevel@tonic-gate 		Gen.g_filesz = attrlen;
90237c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_XATTR, (off_t)attrlen);
90245fbb8099SNobutomo Nakano 		/*LINTED*/
90257c478bd9Sstevel@tonic-gate 		(void) sprintf(namep, "%s/%s", DEVNULL, Gen.g_attrnam_p);
90265fbb8099SNobutomo Nakano 		write_ancillary(attrbuf, attrlen, B_TRUE);
90277c478bd9Sstevel@tonic-gate 	}
90287c478bd9Sstevel@tonic-gate 
90297c478bd9Sstevel@tonic-gate 	(void) creat_hdr();
90307c478bd9Sstevel@tonic-gate #endif
90317c478bd9Sstevel@tonic-gate }
90327c478bd9Sstevel@tonic-gate 
90337c478bd9Sstevel@tonic-gate /*
90347c478bd9Sstevel@tonic-gate  * skip over extra slashes in string.
90357c478bd9Sstevel@tonic-gate  *
90367c478bd9Sstevel@tonic-gate  * For example:
90377c478bd9Sstevel@tonic-gate  * /usr/tmp/////
90387c478bd9Sstevel@tonic-gate  *
90397c478bd9Sstevel@tonic-gate  * would return pointer at
90407c478bd9Sstevel@tonic-gate  * /usr/tmp/////
90417c478bd9Sstevel@tonic-gate  *         ^
90427c478bd9Sstevel@tonic-gate  */
90437c478bd9Sstevel@tonic-gate static char *
90447c478bd9Sstevel@tonic-gate skipslashes(char *string, char *start)
90457c478bd9Sstevel@tonic-gate {
90467c478bd9Sstevel@tonic-gate 	while ((string > start) && *(string - 1) == '/') {
90477c478bd9Sstevel@tonic-gate 		string--;
90487c478bd9Sstevel@tonic-gate 	}
90497c478bd9Sstevel@tonic-gate 
90507c478bd9Sstevel@tonic-gate 	return (string);
90517c478bd9Sstevel@tonic-gate }
90527c478bd9Sstevel@tonic-gate 
90537c478bd9Sstevel@tonic-gate static sl_info_t *
90547c478bd9Sstevel@tonic-gate sl_info_alloc(void)
90557c478bd9Sstevel@tonic-gate {
90567c478bd9Sstevel@tonic-gate 	static int num_left;
90577c478bd9Sstevel@tonic-gate 	static sl_info_t *slipool;
90587c478bd9Sstevel@tonic-gate 
90597c478bd9Sstevel@tonic-gate 	if (num_left > 0) {
90607c478bd9Sstevel@tonic-gate 		return (&slipool[--num_left]);
90617c478bd9Sstevel@tonic-gate 	}
90627c478bd9Sstevel@tonic-gate 	num_left = SL_INFO_ALLOC_CHUNK;
90637c478bd9Sstevel@tonic-gate 	slipool = e_zalloc(E_EXIT, sizeof (sl_info_t) * num_left);
90647c478bd9Sstevel@tonic-gate 	return (&slipool[--num_left]);
90657c478bd9Sstevel@tonic-gate }
90667c478bd9Sstevel@tonic-gate 
90677c478bd9Sstevel@tonic-gate /*
90687c478bd9Sstevel@tonic-gate  * If a match for the key values was found in the tree, return a pointer to it.
90697c478bd9Sstevel@tonic-gate  * If a match was not found, insert it and return a pointer to it.  This is
90707c478bd9Sstevel@tonic-gate  * based on Knuth's Algorithm A in Vol 3, section 6.2.3.
90717c478bd9Sstevel@tonic-gate  */
90727c478bd9Sstevel@tonic-gate 
90737c478bd9Sstevel@tonic-gate sl_info_t *
9074ced83f9bSceastha sl_insert(dev_t device, ino_t inode, int ftype)
90757c478bd9Sstevel@tonic-gate {
90767c478bd9Sstevel@tonic-gate 	sl_info_t *p;		/* moves down the tree */
90777c478bd9Sstevel@tonic-gate 	sl_info_t *q;		/* scratch */
90787c478bd9Sstevel@tonic-gate 	sl_info_t *r;		/* scratch */
90797c478bd9Sstevel@tonic-gate 	sl_info_t *s;		/* pt where rebalancing may be needed */
90807c478bd9Sstevel@tonic-gate 	sl_info_t *t;		/* father of s */
90817c478bd9Sstevel@tonic-gate 	sl_info_t *head;
90827c478bd9Sstevel@tonic-gate 
90837c478bd9Sstevel@tonic-gate 	int a;			/* used to hold balance factors */
90847c478bd9Sstevel@tonic-gate 	int done;		/* loop control */
90857c478bd9Sstevel@tonic-gate 	int cmpflg;		/* used to hold the result of a comparison */
90867c478bd9Sstevel@tonic-gate 
90877c478bd9Sstevel@tonic-gate 	/* initialize */
90887c478bd9Sstevel@tonic-gate 
90897c478bd9Sstevel@tonic-gate 	head = sl_devhash_lookup(device);
90907c478bd9Sstevel@tonic-gate 
90917c478bd9Sstevel@tonic-gate 	if (head == NULL) {
90927c478bd9Sstevel@tonic-gate 		head = sl_info_alloc();
90937c478bd9Sstevel@tonic-gate 		head->llink = NULL;
90947c478bd9Sstevel@tonic-gate 		head->bal = 0;
90957c478bd9Sstevel@tonic-gate 
90967c478bd9Sstevel@tonic-gate 		p = head->rlink = sl_info_alloc();
90977c478bd9Sstevel@tonic-gate 		p->sl_ino = inode;
9098ced83f9bSceastha 		p->sl_ftype = ftype;
90997c478bd9Sstevel@tonic-gate 		p->sl_count = 0;
91007c478bd9Sstevel@tonic-gate 		p->bal = 0;
91017c478bd9Sstevel@tonic-gate 		p->llink = NULL;
91027c478bd9Sstevel@tonic-gate 		p->rlink = NULL;
91037c478bd9Sstevel@tonic-gate 		sl_devhash_insert(device, head);
91047c478bd9Sstevel@tonic-gate 		return (p);
91057c478bd9Sstevel@tonic-gate 	}
91067c478bd9Sstevel@tonic-gate 
91077c478bd9Sstevel@tonic-gate 	t = head;
91087c478bd9Sstevel@tonic-gate 	s = p = head->rlink;
91097c478bd9Sstevel@tonic-gate 
91107c478bd9Sstevel@tonic-gate 	/* compare */
91117c478bd9Sstevel@tonic-gate 
91127c478bd9Sstevel@tonic-gate 	for (done = 0; ! done; ) {
9113ced83f9bSceastha 		switch (sl_compare(inode, ftype, p->sl_ino, p->sl_ftype)) {
91147c478bd9Sstevel@tonic-gate 			case -1:
91157c478bd9Sstevel@tonic-gate 				/* move left */
91167c478bd9Sstevel@tonic-gate 
91177c478bd9Sstevel@tonic-gate 				q = p->llink;
91187c478bd9Sstevel@tonic-gate 
91197c478bd9Sstevel@tonic-gate 				if (q == NULL) {
91207c478bd9Sstevel@tonic-gate 					q = sl_info_alloc();
91217c478bd9Sstevel@tonic-gate 					p->llink = q;
91227c478bd9Sstevel@tonic-gate 					done = 1;
91237c478bd9Sstevel@tonic-gate 					continue;
91247c478bd9Sstevel@tonic-gate 				}
91257c478bd9Sstevel@tonic-gate 
91267c478bd9Sstevel@tonic-gate 				break;
91277c478bd9Sstevel@tonic-gate 
91287c478bd9Sstevel@tonic-gate 			case 0:
91297c478bd9Sstevel@tonic-gate 				/* found it */
91307c478bd9Sstevel@tonic-gate 				return (p);
91317c478bd9Sstevel@tonic-gate 
91327c478bd9Sstevel@tonic-gate 			case 1:
91337c478bd9Sstevel@tonic-gate 				/* move right */
91347c478bd9Sstevel@tonic-gate 
91357c478bd9Sstevel@tonic-gate 				q = p->rlink;
91367c478bd9Sstevel@tonic-gate 
91377c478bd9Sstevel@tonic-gate 				if (q == NULL) {
91387c478bd9Sstevel@tonic-gate 					q = sl_info_alloc();
91397c478bd9Sstevel@tonic-gate 					p->rlink = q;
91407c478bd9Sstevel@tonic-gate 					done = 1;
91417c478bd9Sstevel@tonic-gate 					continue;
91427c478bd9Sstevel@tonic-gate 				}
91437c478bd9Sstevel@tonic-gate 
91447c478bd9Sstevel@tonic-gate 				break;
91457c478bd9Sstevel@tonic-gate 		}
91467c478bd9Sstevel@tonic-gate 
91477c478bd9Sstevel@tonic-gate 		if (q->bal != 0) {
91487c478bd9Sstevel@tonic-gate 			t = p;
91497c478bd9Sstevel@tonic-gate 			s = q;
91507c478bd9Sstevel@tonic-gate 		}
91517c478bd9Sstevel@tonic-gate 
91527c478bd9Sstevel@tonic-gate 		p = q;
91537c478bd9Sstevel@tonic-gate 	}
91547c478bd9Sstevel@tonic-gate 
91557c478bd9Sstevel@tonic-gate 	/* insert */
91567c478bd9Sstevel@tonic-gate 
91577c478bd9Sstevel@tonic-gate 	q->sl_ino = inode;
9158ced83f9bSceastha 	q->sl_ftype = ftype;
91597c478bd9Sstevel@tonic-gate 	q->sl_count = 0;
91607c478bd9Sstevel@tonic-gate 	q->llink = q->rlink = NULL;
91617c478bd9Sstevel@tonic-gate 	q->bal = 0;
91627c478bd9Sstevel@tonic-gate 
91637c478bd9Sstevel@tonic-gate 	/* adjust balance factors */
91647c478bd9Sstevel@tonic-gate 
9165ced83f9bSceastha 	if ((cmpflg = sl_compare(inode, ftype, s->sl_ino, s->sl_ftype)) < 0) {
91667c478bd9Sstevel@tonic-gate 		r = p = s->llink;
91677c478bd9Sstevel@tonic-gate 	} else {
91687c478bd9Sstevel@tonic-gate 		r = p = s->rlink;
91697c478bd9Sstevel@tonic-gate 	}
91707c478bd9Sstevel@tonic-gate 
91717c478bd9Sstevel@tonic-gate 	while (p != q) {
9172ced83f9bSceastha 		switch (sl_compare(inode, ftype, p->sl_ino, p->sl_ftype)) {
91737c478bd9Sstevel@tonic-gate 			case -1:
91747c478bd9Sstevel@tonic-gate 				p->bal = -1;
91757c478bd9Sstevel@tonic-gate 				p = p->llink;
91767c478bd9Sstevel@tonic-gate 				break;
91777c478bd9Sstevel@tonic-gate 
91787c478bd9Sstevel@tonic-gate 			case 0:
91797c478bd9Sstevel@tonic-gate 				break;
91807c478bd9Sstevel@tonic-gate 
91817c478bd9Sstevel@tonic-gate 			case 1:
91827c478bd9Sstevel@tonic-gate 				p->bal = 1;
91837c478bd9Sstevel@tonic-gate 				p = p->rlink;
91847c478bd9Sstevel@tonic-gate 				break;
91857c478bd9Sstevel@tonic-gate 		}
91867c478bd9Sstevel@tonic-gate 	}
91877c478bd9Sstevel@tonic-gate 
91887c478bd9Sstevel@tonic-gate 	/* balancing act */
91897c478bd9Sstevel@tonic-gate 
91907c478bd9Sstevel@tonic-gate 	if (cmpflg < 0) {
91917c478bd9Sstevel@tonic-gate 		a = -1;
91927c478bd9Sstevel@tonic-gate 	} else {
91937c478bd9Sstevel@tonic-gate 		a = 1;
91947c478bd9Sstevel@tonic-gate 	}
91957c478bd9Sstevel@tonic-gate 
91967c478bd9Sstevel@tonic-gate 	if (s->bal == 0) {
91977c478bd9Sstevel@tonic-gate 		s->bal = a;
91987c478bd9Sstevel@tonic-gate 		head->llink = (sl_info_t *)((int)head->llink + 1);
91997c478bd9Sstevel@tonic-gate 		return (q);
92007c478bd9Sstevel@tonic-gate 	} else if (s->bal == -a) {
92017c478bd9Sstevel@tonic-gate 		s->bal = 0;
92027c478bd9Sstevel@tonic-gate 		return (q);
92037c478bd9Sstevel@tonic-gate 	}
92047c478bd9Sstevel@tonic-gate 
92057c478bd9Sstevel@tonic-gate 	/*
92067c478bd9Sstevel@tonic-gate 	 * (s->bal == a)
92077c478bd9Sstevel@tonic-gate 	 */
92087c478bd9Sstevel@tonic-gate 
92097c478bd9Sstevel@tonic-gate 	if (r->bal == a) {
92107c478bd9Sstevel@tonic-gate 		/* single rotation */
92117c478bd9Sstevel@tonic-gate 
92127c478bd9Sstevel@tonic-gate 		p = r;
92137c478bd9Sstevel@tonic-gate 
92147c478bd9Sstevel@tonic-gate 		if (a == -1) {
92157c478bd9Sstevel@tonic-gate 			s->llink = r->rlink;
92167c478bd9Sstevel@tonic-gate 			r->rlink = s;
92177c478bd9Sstevel@tonic-gate 		} else if (a == 1) {
92187c478bd9Sstevel@tonic-gate 			s->rlink = r->llink;
92197c478bd9Sstevel@tonic-gate 			r->llink = s;
92207c478bd9Sstevel@tonic-gate 		}
92217c478bd9Sstevel@tonic-gate 
92227c478bd9Sstevel@tonic-gate 		s->bal = r->bal = 0;
92237c478bd9Sstevel@tonic-gate 
92247c478bd9Sstevel@tonic-gate 	} else if (r->bal == -a) {
92257c478bd9Sstevel@tonic-gate 		/* double rotation */
92267c478bd9Sstevel@tonic-gate 
92277c478bd9Sstevel@tonic-gate 		if (a == -1) {
92287c478bd9Sstevel@tonic-gate 			p = r->rlink;
92297c478bd9Sstevel@tonic-gate 			r->rlink = p->llink;
92307c478bd9Sstevel@tonic-gate 			p->llink = r;
92317c478bd9Sstevel@tonic-gate 			s->llink = p->rlink;
92327c478bd9Sstevel@tonic-gate 			p->rlink = s;
92337c478bd9Sstevel@tonic-gate 		} else if (a == 1) {
92347c478bd9Sstevel@tonic-gate 			p = r->llink;
92357c478bd9Sstevel@tonic-gate 			r->llink = p->rlink;
92367c478bd9Sstevel@tonic-gate 			p->rlink = r;
92377c478bd9Sstevel@tonic-gate 			s->rlink = p->llink;
92387c478bd9Sstevel@tonic-gate 			p->llink = s;
92397c478bd9Sstevel@tonic-gate 		}
92407c478bd9Sstevel@tonic-gate 
92417c478bd9Sstevel@tonic-gate 		if (p->bal == 0) {
92427c478bd9Sstevel@tonic-gate 			s->bal = 0;
92437c478bd9Sstevel@tonic-gate 			r->bal = 0;
92447c478bd9Sstevel@tonic-gate 		} else if (p->bal == -a) {
92457c478bd9Sstevel@tonic-gate 			s->bal = 0;
92467c478bd9Sstevel@tonic-gate 			r->bal = a;
92477c478bd9Sstevel@tonic-gate 		} else if (p->bal == a) {
92487c478bd9Sstevel@tonic-gate 			s->bal = -a;
92497c478bd9Sstevel@tonic-gate 			r->bal = 0;
92507c478bd9Sstevel@tonic-gate 		}
92517c478bd9Sstevel@tonic-gate 
92527c478bd9Sstevel@tonic-gate 		p->bal = 0;
92537c478bd9Sstevel@tonic-gate 	}
92547c478bd9Sstevel@tonic-gate 
92557c478bd9Sstevel@tonic-gate 	/* finishing touch */
92567c478bd9Sstevel@tonic-gate 
92577c478bd9Sstevel@tonic-gate 	if (s == t->rlink) {
92587c478bd9Sstevel@tonic-gate 		t->rlink = p;
92597c478bd9Sstevel@tonic-gate 	} else {
92607c478bd9Sstevel@tonic-gate 		t->llink = p;
92617c478bd9Sstevel@tonic-gate 	}
92627c478bd9Sstevel@tonic-gate 
92637c478bd9Sstevel@tonic-gate 	return (q);
92647c478bd9Sstevel@tonic-gate }
92657c478bd9Sstevel@tonic-gate 
92667c478bd9Sstevel@tonic-gate /*
92677c478bd9Sstevel@tonic-gate  * sl_numlinks: return the number of links that we saw during our preview.
92687c478bd9Sstevel@tonic-gate  */
92697c478bd9Sstevel@tonic-gate 
92707c478bd9Sstevel@tonic-gate static ulong_t
9271ced83f9bSceastha sl_numlinks(dev_t device, ino_t inode, int ftype)
92727c478bd9Sstevel@tonic-gate {
9273ced83f9bSceastha 	sl_info_t *p = sl_search(device, inode, ftype);
92747c478bd9Sstevel@tonic-gate 
92757c478bd9Sstevel@tonic-gate 	if (p) {
92767c478bd9Sstevel@tonic-gate 		return (p->sl_count);
92777c478bd9Sstevel@tonic-gate 	} else {
92787c478bd9Sstevel@tonic-gate 		return (1);
92797c478bd9Sstevel@tonic-gate 	}
92807c478bd9Sstevel@tonic-gate }
92817c478bd9Sstevel@tonic-gate 
92827c478bd9Sstevel@tonic-gate /*
9283ced83f9bSceastha  * Preview extended and extended system attributes.
9284ced83f9bSceastha  *
9285ced83f9bSceastha  * Return 0 if successful, otherwise return 1.
9286ced83f9bSceastha  */
9287ced83f9bSceastha #if defined(O_XATTR)
9288ced83f9bSceastha static int
9289ced83f9bSceastha preview_attrs(char *s, char *attrparent)
9290ced83f9bSceastha {
9291ced83f9bSceastha 	char		*filename = (attrparent == NULL) ? s : attrparent;
9292ced83f9bSceastha 	int		dirfd;
9293ced83f9bSceastha 	int		tmpfd;
9294ced83f9bSceastha 	int		islnk;
9295ced83f9bSceastha 	int		rc = 0;
9296ced83f9bSceastha 	int		arc_rwsysattr = 0;
9297ced83f9bSceastha 	int		rw_sysattr = 0;
9298ced83f9bSceastha 	int		ext_attr = 0;
9299ced83f9bSceastha 	DIR		*dirp;
9300ced83f9bSceastha 	struct dirent	*dp;
9301ced83f9bSceastha 	struct stat	sb;
9302ced83f9bSceastha 
9303ced83f9bSceastha 	/*
9304ced83f9bSceastha 	 * If the underlying file system supports it, then
9305ced83f9bSceastha 	 * archive the extended attributes if -@ was specified,
9306ced83f9bSceastha 	 * and the extended system attributes if -/ was
9307ced83f9bSceastha 	 * specified.
9308ced83f9bSceastha 	 */
9309ced83f9bSceastha 	if (verify_attr_support(filename, (attrparent == NULL), ARC_CREATE,
9310ced83f9bSceastha 	    &ext_attr) != ATTR_OK) {
9311ced83f9bSceastha 		return (1);
9312ced83f9bSceastha 	}
9313ced83f9bSceastha 
9314ced83f9bSceastha #if defined(_PC_SATTR_ENABLED)
9315ced83f9bSceastha 	if (SysAtflag) {
9316ced83f9bSceastha 		int		filefd;
9317ced83f9bSceastha 		nvlist_t 	*slist = NULL;
9318ced83f9bSceastha 
9319ced83f9bSceastha 		/* Determine if there are non-transient system attributes. */
9320ced83f9bSceastha 		errno = 0;
9321ced83f9bSceastha 		if ((filefd = open(filename, O_RDONLY)) < 0) {
9322ced83f9bSceastha 			return (1);
9323ced83f9bSceastha 		}
9324ced83f9bSceastha 		if (((slist = sysattr_list(myname, filefd,
9325ced83f9bSceastha 		    filename)) != NULL) || (errno != 0)) {
9326ced83f9bSceastha 			arc_rwsysattr = 1;
9327ced83f9bSceastha 		}
9328ced83f9bSceastha 		if (slist != NULL) {
9329ced83f9bSceastha 			(void) nvlist_free(slist);
9330ced83f9bSceastha 			slist = NULL;
9331ced83f9bSceastha 		}
9332ced83f9bSceastha 		(void) close(filefd);
9333ced83f9bSceastha 	}
9334ced83f9bSceastha 
9335ced83f9bSceastha 	if ((arc_rwsysattr == 0) && ((attrparent != NULL) ||
9336ced83f9bSceastha 	    (SysAtflag && !ext_attr))) {
9337ced83f9bSceastha 		return (1);
9338ced83f9bSceastha 	}
9339ced83f9bSceastha #endif	/* _PC_SATTR_ENABLED */
9340ced83f9bSceastha 	/*
9341ced83f9bSceastha 	 * We need to open the attribute directory of the
9342ced83f9bSceastha 	 * file, and preview all of the file's attributes as
9343ced83f9bSceastha 	 * attributes of the file can be hard links to other
9344ced83f9bSceastha 	 * attributes of the file.
9345ced83f9bSceastha 	 */
9346ced83f9bSceastha 	dirfd = attropen(filename, ".", O_RDONLY);
9347ced83f9bSceastha 	if (dirfd == -1)
9348ced83f9bSceastha 		return (1);
9349ced83f9bSceastha 
9350ced83f9bSceastha 	tmpfd = dup(dirfd);
9351ced83f9bSceastha 	if (tmpfd == -1) {
9352ced83f9bSceastha 		(void) close(dirfd);
9353ced83f9bSceastha 		return (1);
9354ced83f9bSceastha 	}
9355ced83f9bSceastha 	dirp = fdopendir(tmpfd);
9356ced83f9bSceastha 	if (dirp == NULL) {
9357ced83f9bSceastha 		(void) close(dirfd);
9358ced83f9bSceastha 		(void) close(tmpfd);
9359ced83f9bSceastha 		return (1);
9360ced83f9bSceastha 	}
9361ced83f9bSceastha 
9362ced83f9bSceastha 	while (dp = readdir(dirp)) {
9363ced83f9bSceastha 		if (dp->d_name[0] == '.') {
9364ced83f9bSceastha 			if (dp->d_name[1] == '\0') {
9365ced83f9bSceastha 				Hiddendir = 1;
9366ced83f9bSceastha 			} else if ((dp->d_name[1] == '.') &&
9367ced83f9bSceastha 			    (dp->d_name[2] == '\0')) {
9368ced83f9bSceastha 				continue;
9369ced83f9bSceastha 			} else {
9370ced83f9bSceastha 				Hiddendir = 0;
9371ced83f9bSceastha 			}
9372ced83f9bSceastha 		} else {
9373ced83f9bSceastha 			Hiddendir = 0;
9374ced83f9bSceastha 		}
9375ced83f9bSceastha 
9376ced83f9bSceastha 		if (fstatat(dirfd, dp->d_name, &sb,
9377ced83f9bSceastha 		    AT_SYMLINK_NOFOLLOW) < 0) {
9378ced83f9bSceastha 			continue;
9379ced83f9bSceastha 		}
9380ced83f9bSceastha 
9381ced83f9bSceastha 		if (verify_attr(dp->d_name, attrparent,
9382ced83f9bSceastha 		    arc_rwsysattr, &rw_sysattr) != ATTR_OK) {
9383ced83f9bSceastha 			continue;
9384ced83f9bSceastha 		}
9385ced83f9bSceastha 
9386ced83f9bSceastha 		islnk = 0;
9387ced83f9bSceastha 		if (S_ISLNK(sb.st_mode)) {
9388ced83f9bSceastha 			islnk = 1;
9389ced83f9bSceastha 			if (Args & OCL) {
9390ced83f9bSceastha 				if (fstatat(dirfd, dp->d_name,
9391ced83f9bSceastha 				    &sb, 0) < 0) {
9392ced83f9bSceastha 					continue;
9393ced83f9bSceastha 				}
9394ced83f9bSceastha 			}
9395ced83f9bSceastha 		}
9396ced83f9bSceastha 		sl_remember_tgt(&sb, islnk, rw_sysattr);
9397ced83f9bSceastha 
9398ced83f9bSceastha 		/*
9399ced83f9bSceastha 		 * Recursively call preview_attrs() to preview extended
9400ced83f9bSceastha 		 * system attributes of attributes.
9401ced83f9bSceastha 		 */
9402ced83f9bSceastha 		if (SysAtflag && !Hiddendir && !rw_sysattr) {
9403ced83f9bSceastha 			int	my_cwd = save_cwd();
9404ced83f9bSceastha 
9405ced83f9bSceastha 			(void) fchdir(dirfd);
9406ced83f9bSceastha 			rc = preview_attrs(s, dp->d_name);
9407ced83f9bSceastha 			rest_cwd(my_cwd);
9408ced83f9bSceastha 		}
9409ced83f9bSceastha 	}
9410ced83f9bSceastha 	(void) closedir(dirp);
9411ced83f9bSceastha 	(void) close(dirfd);
9412ced83f9bSceastha 	return (rc);
9413ced83f9bSceastha }
9414ced83f9bSceastha #endif	/* O_XATTR */
9415ced83f9bSceastha 
9416ced83f9bSceastha /*
94177c478bd9Sstevel@tonic-gate  * sl_preview_synonyms:  Read the file list from the input stream, remembering
94187c478bd9Sstevel@tonic-gate  * each reference to each file.
94197c478bd9Sstevel@tonic-gate  */
94207c478bd9Sstevel@tonic-gate 
94217c478bd9Sstevel@tonic-gate static void
94227c478bd9Sstevel@tonic-gate sl_preview_synonyms(void)
94237c478bd9Sstevel@tonic-gate {
94247c478bd9Sstevel@tonic-gate 	char buf [APATH+1];
94257c478bd9Sstevel@tonic-gate 	char *s;
94267c478bd9Sstevel@tonic-gate 
94277c478bd9Sstevel@tonic-gate 	char *suffix = "/cpioXXXXXX";
94287c478bd9Sstevel@tonic-gate 	char *tmpdir = getenv("TMPDIR");
94297c478bd9Sstevel@tonic-gate 	int    tmpfd, islnk;
94307c478bd9Sstevel@tonic-gate 	FILE *tmpfile;
94317c478bd9Sstevel@tonic-gate 	char *tmpfname;
94327c478bd9Sstevel@tonic-gate 
94337c478bd9Sstevel@tonic-gate 	if (tmpdir == NULL || *tmpdir == '\0' ||
94347c478bd9Sstevel@tonic-gate 	    (strlen(tmpdir) + strlen(suffix)) > APATH) {
94357c478bd9Sstevel@tonic-gate 		struct statvfs tdsb;
94367c478bd9Sstevel@tonic-gate 
94377c478bd9Sstevel@tonic-gate 		tmpdir = "/var/tmp";
94387c478bd9Sstevel@tonic-gate 
94397c478bd9Sstevel@tonic-gate 		/* /var/tmp is read-only in the mini-root environment */
94407c478bd9Sstevel@tonic-gate 
94417c478bd9Sstevel@tonic-gate 		if (statvfs(tmpdir, &tdsb) == -1 || tdsb.f_flag & ST_RDONLY) {
94427c478bd9Sstevel@tonic-gate 			tmpdir = "/tmp";
94437c478bd9Sstevel@tonic-gate 		}
94447c478bd9Sstevel@tonic-gate 	}
94457c478bd9Sstevel@tonic-gate 
94467c478bd9Sstevel@tonic-gate 	tmpfname = e_zalloc(E_EXIT, strlen(tmpdir) + strlen(suffix) + 1);
94477c478bd9Sstevel@tonic-gate 
94487c478bd9Sstevel@tonic-gate 	(void) strcpy(tmpfname, tmpdir);
94497c478bd9Sstevel@tonic-gate 	(void) strcat(tmpfname, suffix);
94507c478bd9Sstevel@tonic-gate 
94517c478bd9Sstevel@tonic-gate 	if ((tmpfd = mkstemp(tmpfname)) == -1) {
9452ced83f9bSceastha 		msg(EXTN, "cannot open tmpfile %s%s", tmpdir, suffix);
94537c478bd9Sstevel@tonic-gate 	}
94547c478bd9Sstevel@tonic-gate 
94557c478bd9Sstevel@tonic-gate 	if (unlink(tmpfname) == -1) {
94567c478bd9Sstevel@tonic-gate 		msg(EXTN, "cannot unlink tmpfile %s", tmpfname);
94577c478bd9Sstevel@tonic-gate 	}
94587c478bd9Sstevel@tonic-gate 
94597c478bd9Sstevel@tonic-gate 	if ((tmpfile = fdopen(tmpfd, "w+")) == NULL) {
94607c478bd9Sstevel@tonic-gate 		msg(EXTN, "cannot fdopen tmpfile %s", tmpfname);
94617c478bd9Sstevel@tonic-gate 	}
94627c478bd9Sstevel@tonic-gate 
94637c478bd9Sstevel@tonic-gate 	while ((s = fgets(buf, APATH+1, In_p)) != NULL) {
94647c478bd9Sstevel@tonic-gate 		size_t lastchar;
94657c478bd9Sstevel@tonic-gate 		struct stat sb;
94667c478bd9Sstevel@tonic-gate 
94677c478bd9Sstevel@tonic-gate 		if (fputs(buf, tmpfile) == EOF) {
94687c478bd9Sstevel@tonic-gate 			msg(EXTN, "problem writing to tmpfile %s", tmpfname);
94697c478bd9Sstevel@tonic-gate 		}
94707c478bd9Sstevel@tonic-gate 
94717c478bd9Sstevel@tonic-gate 		/* pre-process the name */
94727c478bd9Sstevel@tonic-gate 
94737c478bd9Sstevel@tonic-gate 		lastchar = strlen(s) - 1;
94747c478bd9Sstevel@tonic-gate 
94757c478bd9Sstevel@tonic-gate 		if (s[lastchar] != '\n' && lastchar == APATH - 1) {
94767c478bd9Sstevel@tonic-gate 			continue;
94777c478bd9Sstevel@tonic-gate 		} else {
94787c478bd9Sstevel@tonic-gate 			s[lastchar] = '\0';
94797c478bd9Sstevel@tonic-gate 		}
94807c478bd9Sstevel@tonic-gate 
94817c478bd9Sstevel@tonic-gate 		while (s[0] == '.' && s[1] == '/') {
94827c478bd9Sstevel@tonic-gate 			s += 2;
94837c478bd9Sstevel@tonic-gate 			while (s[0] == '/') {
94847c478bd9Sstevel@tonic-gate 				s++;
94857c478bd9Sstevel@tonic-gate 			}
94867c478bd9Sstevel@tonic-gate 		}
94877c478bd9Sstevel@tonic-gate 
94887c478bd9Sstevel@tonic-gate 		if (lstat(s, &sb) < 0) {
94897c478bd9Sstevel@tonic-gate 			continue;
94907c478bd9Sstevel@tonic-gate 		}
94917c478bd9Sstevel@tonic-gate 		islnk = 0;
94927c478bd9Sstevel@tonic-gate 		if (S_ISLNK(sb.st_mode)) {
94937c478bd9Sstevel@tonic-gate 			islnk = 1;
94947c478bd9Sstevel@tonic-gate 			if (Args & OCL) {
94957c478bd9Sstevel@tonic-gate 				if (stat(s, &sb) < 0) {
94967c478bd9Sstevel@tonic-gate 					continue;
94977c478bd9Sstevel@tonic-gate 				}
94987c478bd9Sstevel@tonic-gate 			}
94997c478bd9Sstevel@tonic-gate 		}
9500ced83f9bSceastha 		sl_remember_tgt(&sb, islnk, 0);
95017c478bd9Sstevel@tonic-gate 
95027c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
9503ced83f9bSceastha 		if (Atflag || SysAtflag) {
9504ced83f9bSceastha 			(void) preview_attrs(s, NULL);
95057c478bd9Sstevel@tonic-gate 		}
9506ced83f9bSceastha #endif	/* O_XATTR */
95077c478bd9Sstevel@tonic-gate 	}
95087c478bd9Sstevel@tonic-gate 
95097c478bd9Sstevel@tonic-gate 	if (ferror(In_p)) {
95107c478bd9Sstevel@tonic-gate 		msg(EXTN, "error reading stdin");
95117c478bd9Sstevel@tonic-gate 	}
95127c478bd9Sstevel@tonic-gate 
95137c478bd9Sstevel@tonic-gate 	if (fseek(tmpfile, 0L, SEEK_SET) == -1) {
95147c478bd9Sstevel@tonic-gate 		msg(EXTN, "cannot fseek on tmpfile %s", tmpfname);
95157c478bd9Sstevel@tonic-gate 	}
95167c478bd9Sstevel@tonic-gate 
95177c478bd9Sstevel@tonic-gate 	In_p = tmpfile;
95187c478bd9Sstevel@tonic-gate 	free(tmpfname);
95197c478bd9Sstevel@tonic-gate }
95207c478bd9Sstevel@tonic-gate 
95217c478bd9Sstevel@tonic-gate /*
95227c478bd9Sstevel@tonic-gate  * sl_remember_tgt: Add the device/inode for lstat or stat info to the list of
95237c478bd9Sstevel@tonic-gate  * those we've seen before.
95247c478bd9Sstevel@tonic-gate  *
95257c478bd9Sstevel@tonic-gate  * This tree (rooted under head) is keyed by the device/inode of the file
95267c478bd9Sstevel@tonic-gate  * being pointed to.  A count is kept of the number of references encountered
95277c478bd9Sstevel@tonic-gate  * so far.
95287c478bd9Sstevel@tonic-gate  */
95297c478bd9Sstevel@tonic-gate 
95307c478bd9Sstevel@tonic-gate static void
9531ced83f9bSceastha sl_remember_tgt(const struct stat *sbp, int isSymlink, int is_sysattr)
95327c478bd9Sstevel@tonic-gate {
95337c478bd9Sstevel@tonic-gate 	sl_info_t *p;
95347c478bd9Sstevel@tonic-gate 	dev_t device;
95357c478bd9Sstevel@tonic-gate 	ino_t inode;
9536ced83f9bSceastha 	int ftype;
95377c478bd9Sstevel@tonic-gate 
95387c478bd9Sstevel@tonic-gate 	device = sbp->st_dev;
95397c478bd9Sstevel@tonic-gate 	inode  = sbp->st_ino;
9540ced83f9bSceastha 	ftype  = sbp->st_mode & Ftype;
95417c478bd9Sstevel@tonic-gate 
95427c478bd9Sstevel@tonic-gate 	/* Determine whether we've seen this one before */
95437c478bd9Sstevel@tonic-gate 
9544ced83f9bSceastha 	p = sl_insert(device, inode, ftype);
95457c478bd9Sstevel@tonic-gate 
95467c478bd9Sstevel@tonic-gate 	if (p->sl_count > 0) {
95477c478bd9Sstevel@tonic-gate 		/*
9548ced83f9bSceastha 		 * It appears as if have seen this file before as we found a
9549ced83f9bSceastha 		 * matching device, inode, and file type as a file already
9550ced83f9bSceastha 		 * processed.  Since there can possibly be files with the
9551ced83f9bSceastha 		 * same device, inode, and file type, but aren't hard links
9552ced83f9bSceastha 		 * (e.g., read-write system attribute files will always have
9553ced83f9bSceastha 		 * the same inode), we need to only attempt to add one to the
9554ced83f9bSceastha 		 * link count if the file we are processing is a hard link
9555ced83f9bSceastha 		 * (i.e., st_nlink > 1).
9556ced83f9bSceastha 		 *
95577c478bd9Sstevel@tonic-gate 		 * Note that if we are not chasing symlinks, and this one is a
95587c478bd9Sstevel@tonic-gate 		 * symlink, it is identically the one we saw before (you cannot
95597c478bd9Sstevel@tonic-gate 		 * have hard links to symlinks); in this case, we leave the
95607c478bd9Sstevel@tonic-gate 		 * count alone, so that we don't wind up archiving a symlink to
95617c478bd9Sstevel@tonic-gate 		 * itself.
95627c478bd9Sstevel@tonic-gate 		 */
95637c478bd9Sstevel@tonic-gate 
9564ced83f9bSceastha 		if (((Args & OCL) || (! isSymlink)) && !is_sysattr) {
95657c478bd9Sstevel@tonic-gate 			p->sl_count++;
95667c478bd9Sstevel@tonic-gate 		}
95677c478bd9Sstevel@tonic-gate 	} else {
95687c478bd9Sstevel@tonic-gate 		/* We have not seen this file before */
95697c478bd9Sstevel@tonic-gate 
95707c478bd9Sstevel@tonic-gate 		p->sl_count = 1;
95717c478bd9Sstevel@tonic-gate 
95727c478bd9Sstevel@tonic-gate 		if (Use_old_stat) {
95737c478bd9Sstevel@tonic-gate 			/* -Hodc: remap inode (-1 on overflow) */
95747c478bd9Sstevel@tonic-gate 
95757c478bd9Sstevel@tonic-gate 			sl_remap_t  *q;
95767c478bd9Sstevel@tonic-gate 
95777c478bd9Sstevel@tonic-gate 			for (q = sl_remap_head; q && (q->dev != device);
95787c478bd9Sstevel@tonic-gate 			    q = q->next) {
95797c478bd9Sstevel@tonic-gate 				/* do nothing */
95807c478bd9Sstevel@tonic-gate 			}
95817c478bd9Sstevel@tonic-gate 
95827c478bd9Sstevel@tonic-gate 			if (q == NULL) {
95837c478bd9Sstevel@tonic-gate 				q = e_zalloc(E_EXIT, sizeof (sl_remap_t));
95847c478bd9Sstevel@tonic-gate 				q->dev = device;
95857c478bd9Sstevel@tonic-gate 				p->sl_ino2 = q->inode_count = 1;
95867c478bd9Sstevel@tonic-gate 
95877c478bd9Sstevel@tonic-gate 				q->next = (sl_remap_head) ?
95887c478bd9Sstevel@tonic-gate 				    sl_remap_head->next : NULL;
95897c478bd9Sstevel@tonic-gate 				sl_remap_head = q;
95907c478bd9Sstevel@tonic-gate 			} else {
95917c478bd9Sstevel@tonic-gate 				if ((size_t)q->inode_count <=
95927c478bd9Sstevel@tonic-gate 				    ((1 << (sizeof (o_ino_t) * 8)) - 1)) {
95937c478bd9Sstevel@tonic-gate 					/* fits in o_ino_t */
95947c478bd9Sstevel@tonic-gate 					p->sl_ino2 = ++(q->inode_count);
95957c478bd9Sstevel@tonic-gate 				} else {
95967c478bd9Sstevel@tonic-gate 					p->sl_ino2 = (ino_t)-1;
95977c478bd9Sstevel@tonic-gate 				}
95987c478bd9Sstevel@tonic-gate 			}
95997c478bd9Sstevel@tonic-gate 		}
96007c478bd9Sstevel@tonic-gate 	}
96017c478bd9Sstevel@tonic-gate }
96027c478bd9Sstevel@tonic-gate 
96037c478bd9Sstevel@tonic-gate /*
96047c478bd9Sstevel@tonic-gate  * A faster search, which does not insert the key values into the tree.
96057c478bd9Sstevel@tonic-gate  * If the a match was found in the tree, return a pointer to it.  If it was not
96067c478bd9Sstevel@tonic-gate  * found, return NULL.
96077c478bd9Sstevel@tonic-gate  */
96087c478bd9Sstevel@tonic-gate 
96097c478bd9Sstevel@tonic-gate sl_info_t *
9610ced83f9bSceastha sl_search(dev_t device, ino_t inode, int ftype)
96117c478bd9Sstevel@tonic-gate {
96127c478bd9Sstevel@tonic-gate 	sl_info_t *p;		/* moves down the tree */
96137c478bd9Sstevel@tonic-gate 	int c;			/* comparison value */
96147c478bd9Sstevel@tonic-gate 	sl_info_t *retval = NULL; /* return value */
96157c478bd9Sstevel@tonic-gate 	sl_info_t *head;
96167c478bd9Sstevel@tonic-gate 
96177c478bd9Sstevel@tonic-gate 	head = sl_devhash_lookup(device);
96187c478bd9Sstevel@tonic-gate 	if (head != NULL) {
96197c478bd9Sstevel@tonic-gate 		for (p = head->rlink; p; ) {
9620ced83f9bSceastha 			if ((c = sl_compare(inode, ftype, p->sl_ino,
9621ced83f9bSceastha 			    p->sl_ftype)) == 0) {
96227c478bd9Sstevel@tonic-gate 				retval = p;
96237c478bd9Sstevel@tonic-gate 				break;
96247c478bd9Sstevel@tonic-gate 			} else if (c < 0) {
96257c478bd9Sstevel@tonic-gate 				p = p->llink;
96267c478bd9Sstevel@tonic-gate 			} else {
96277c478bd9Sstevel@tonic-gate 				p = p->rlink;
96287c478bd9Sstevel@tonic-gate 			}
96297c478bd9Sstevel@tonic-gate 		}
96307c478bd9Sstevel@tonic-gate 	}
96317c478bd9Sstevel@tonic-gate 
96327c478bd9Sstevel@tonic-gate 	return (retval);
96337c478bd9Sstevel@tonic-gate }
96347c478bd9Sstevel@tonic-gate 
96357c478bd9Sstevel@tonic-gate static sl_info_t *
96367c478bd9Sstevel@tonic-gate sl_devhash_lookup(dev_t device)
96377c478bd9Sstevel@tonic-gate {
96387c478bd9Sstevel@tonic-gate 	int key;
96397c478bd9Sstevel@tonic-gate 	sl_info_link_t *lp;
96407c478bd9Sstevel@tonic-gate 	static sl_info_link_t *devcache;
96417c478bd9Sstevel@tonic-gate 
96427c478bd9Sstevel@tonic-gate 	if (devcache != NULL && devcache->dev == device) {
96437c478bd9Sstevel@tonic-gate 		return (devcache->head);
96447c478bd9Sstevel@tonic-gate 	}
96457c478bd9Sstevel@tonic-gate 
96467c478bd9Sstevel@tonic-gate 	key = DEV_HASHKEY(device);
96477c478bd9Sstevel@tonic-gate 	for (lp = sl_devhash[key]; lp; lp = lp->next) {
96487c478bd9Sstevel@tonic-gate 		if (lp->dev == device) {
96497c478bd9Sstevel@tonic-gate 			devcache = lp;
96507c478bd9Sstevel@tonic-gate 			return (lp->head);
96517c478bd9Sstevel@tonic-gate 		}
96527c478bd9Sstevel@tonic-gate 	}
96537c478bd9Sstevel@tonic-gate 	return (NULL);
96547c478bd9Sstevel@tonic-gate }
96557c478bd9Sstevel@tonic-gate 
96567c478bd9Sstevel@tonic-gate static void
96577c478bd9Sstevel@tonic-gate sl_devhash_insert(dev_t device, sl_info_t *head)
96587c478bd9Sstevel@tonic-gate {
96597c478bd9Sstevel@tonic-gate 	int key = DEV_HASHKEY(device);
96607c478bd9Sstevel@tonic-gate 	sl_info_link_t *lp;
96617c478bd9Sstevel@tonic-gate 
96627c478bd9Sstevel@tonic-gate 	lp = e_zalloc(E_EXIT, sizeof (sl_info_link_t));
96637c478bd9Sstevel@tonic-gate 	lp->dev = device;
96647c478bd9Sstevel@tonic-gate 	lp->head = head;
96657c478bd9Sstevel@tonic-gate 	lp->next = sl_devhash[key];
96667c478bd9Sstevel@tonic-gate 	sl_devhash[key] = lp;
96677c478bd9Sstevel@tonic-gate }
96687c478bd9Sstevel@tonic-gate 
96697c478bd9Sstevel@tonic-gate static void
96707c478bd9Sstevel@tonic-gate chop_endslashes(char *path)
96717c478bd9Sstevel@tonic-gate {
96727c478bd9Sstevel@tonic-gate 	char *end, *ptr;
96737c478bd9Sstevel@tonic-gate 
96747c478bd9Sstevel@tonic-gate 	end = &path[strlen(path) -1];
96757c478bd9Sstevel@tonic-gate 	if (*end == '/' && end != path) {
96767c478bd9Sstevel@tonic-gate 		ptr = skipslashes(end, path);
96777c478bd9Sstevel@tonic-gate 		if (ptr != NULL && ptr != path) {
96787c478bd9Sstevel@tonic-gate 			*ptr = '\0';
96797c478bd9Sstevel@tonic-gate 		}
96807c478bd9Sstevel@tonic-gate 	}
96817c478bd9Sstevel@tonic-gate }
96827c478bd9Sstevel@tonic-gate 
96837c478bd9Sstevel@tonic-gate #if !defined(O_XATTR)
96847c478bd9Sstevel@tonic-gate int
96857c478bd9Sstevel@tonic-gate openat64(int fd, char *name, int oflag, mode_t cmode)
96867c478bd9Sstevel@tonic-gate {
96877c478bd9Sstevel@tonic-gate 	return (open64(name, oflag, cmode));
96887c478bd9Sstevel@tonic-gate }
96897c478bd9Sstevel@tonic-gate 
96907c478bd9Sstevel@tonic-gate int
96917c478bd9Sstevel@tonic-gate openat(int fd, char *name, int oflag, mode_t cmode)
96927c478bd9Sstevel@tonic-gate {
96937c478bd9Sstevel@tonic-gate 	return (open(name, oflag, cmode));
96947c478bd9Sstevel@tonic-gate }
96957c478bd9Sstevel@tonic-gate 
96967c478bd9Sstevel@tonic-gate int
96977c478bd9Sstevel@tonic-gate fchownat(int fd, char *name, uid_t owner, gid_t group, int flag)
96987c478bd9Sstevel@tonic-gate {
96997c478bd9Sstevel@tonic-gate 	if (flag == AT_SYMLINK_NOFOLLOW)
97007c478bd9Sstevel@tonic-gate 		return (lchown(name, owner, group));
97017c478bd9Sstevel@tonic-gate 	else
97027c478bd9Sstevel@tonic-gate 		return (chown(name, owner, group));
97037c478bd9Sstevel@tonic-gate }
97047c478bd9Sstevel@tonic-gate 
97057c478bd9Sstevel@tonic-gate int
97067c478bd9Sstevel@tonic-gate renameat(int fromfd, char *old, int tofd, char *new)
97077c478bd9Sstevel@tonic-gate {
97087c478bd9Sstevel@tonic-gate 	return (rename(old, new));
97097c478bd9Sstevel@tonic-gate }
97107c478bd9Sstevel@tonic-gate 
97117c478bd9Sstevel@tonic-gate int
97127c478bd9Sstevel@tonic-gate futimesat(int fd, char *path, struct timeval times[2])
97137c478bd9Sstevel@tonic-gate {
97147c478bd9Sstevel@tonic-gate 	return (utimes(path, times));
97157c478bd9Sstevel@tonic-gate }
97167c478bd9Sstevel@tonic-gate 
97177c478bd9Sstevel@tonic-gate int
97187c478bd9Sstevel@tonic-gate unlinkat(int dirfd, char *path, int flag)
97197c478bd9Sstevel@tonic-gate {
97207c478bd9Sstevel@tonic-gate 	if (flag == AT_REMOVEDIR) {
97217c478bd9Sstevel@tonic-gate 		return (rmdir(path));
97227c478bd9Sstevel@tonic-gate 	} else {
97237c478bd9Sstevel@tonic-gate 		return (unlink(path));
97247c478bd9Sstevel@tonic-gate 	}
97257c478bd9Sstevel@tonic-gate }
97267c478bd9Sstevel@tonic-gate 
97277c478bd9Sstevel@tonic-gate int
97287c478bd9Sstevel@tonic-gate fstatat(int fd, char *path, struct stat *buf, int flag)
97297c478bd9Sstevel@tonic-gate {
97307c478bd9Sstevel@tonic-gate 	if (flag == AT_SYMLINK_NOFOLLOW)
97317c478bd9Sstevel@tonic-gate 		return (lstat(path, buf));
97327c478bd9Sstevel@tonic-gate 	else
97337c478bd9Sstevel@tonic-gate 		return (stat(path, buf));
97347c478bd9Sstevel@tonic-gate }
97357c478bd9Sstevel@tonic-gate 
97367c478bd9Sstevel@tonic-gate int
97377c478bd9Sstevel@tonic-gate attropen(char *file, char *attr, int omode, mode_t cmode)
97387c478bd9Sstevel@tonic-gate {
97397c478bd9Sstevel@tonic-gate 	errno = ENOTSUP;
97407c478bd9Sstevel@tonic-gate 	return (-1);
97417c478bd9Sstevel@tonic-gate }
97427c478bd9Sstevel@tonic-gate #endif
9743