xref: /titanic_52/usr/src/cmd/cpio/cpio.c (revision 32fd284719e6d05c88b17f3b407c96e4aef0c1ee)
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 /*
22395fcd24Sceastha  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	All Rights Reserved					*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
317c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <errno.h>
397c478bd9Sstevel@tonic-gate #include <unistd.h>
407c478bd9Sstevel@tonic-gate #include <stdlib.h>
417c478bd9Sstevel@tonic-gate #include <fcntl.h>
427c478bd9Sstevel@tonic-gate #include <memory.h>
437c478bd9Sstevel@tonic-gate #include <string.h>
447c478bd9Sstevel@tonic-gate #include <stdarg.h>
457c478bd9Sstevel@tonic-gate #include <sys/stat.h>
467c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
477c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
487c478bd9Sstevel@tonic-gate #include <sys/param.h>
497c478bd9Sstevel@tonic-gate #include <utime.h>
507c478bd9Sstevel@tonic-gate #include <pwd.h>
517c478bd9Sstevel@tonic-gate #include <grp.h>
527c478bd9Sstevel@tonic-gate #include <signal.h>
537c478bd9Sstevel@tonic-gate #include <ctype.h>
547c478bd9Sstevel@tonic-gate #include <archives.h>
557c478bd9Sstevel@tonic-gate #include <locale.h>
567c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
577c478bd9Sstevel@tonic-gate #include <sys/mtio.h>
587c478bd9Sstevel@tonic-gate #include <sys/fdio.h>
597c478bd9Sstevel@tonic-gate #include "cpio.h"
607c478bd9Sstevel@tonic-gate #include <sys/acl.h>
617c478bd9Sstevel@tonic-gate #include <sys/time.h>
627c478bd9Sstevel@tonic-gate #include <sys/resource.h>
637c478bd9Sstevel@tonic-gate #include <fnmatch.h>
647c478bd9Sstevel@tonic-gate #include <libgen.h>
657c478bd9Sstevel@tonic-gate #include <libintl.h>
667c478bd9Sstevel@tonic-gate #include <dirent.h>
677c478bd9Sstevel@tonic-gate #include <limits.h>
68fa9e4066Sahrens #include <aclutils.h>
69ced83f9bSceastha #if defined(_PC_SATTR_ENABLED)
70ced83f9bSceastha #include <libnvpair.h>
71ced83f9bSceastha #include <attr.h>
72ced83f9bSceastha #include <libcmdutils.h>
73ced83f9bSceastha #endif	/* _PC_SATTR_ENABLED */
74647ab8f4Sceastha #ifdef SOLARIS_PRIVS
75647ab8f4Sceastha #include <priv.h>
76647ab8f4Sceastha #endif	/* SOLARIS_PRIVS */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * Special kludge for off_t being a signed quantity.
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate #if _FILE_OFFSET_BITS == 64
827c478bd9Sstevel@tonic-gate typedef	u_longlong_t	u_off_t;
837c478bd9Sstevel@tonic-gate #else
847c478bd9Sstevel@tonic-gate typedef	ulong_t		u_off_t;
857c478bd9Sstevel@tonic-gate #endif
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #define	SECMODE	0xe080
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate #define	DEVNULL		"/dev/null"
907c478bd9Sstevel@tonic-gate #define	XATTRHDR	".hdr"
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #define	NAMELEN		32
937c478bd9Sstevel@tonic-gate #define	TYPELEN 	16
947c478bd9Sstevel@tonic-gate #define	PERMLEN		4
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #define	FILE_COPIED	1
977c478bd9Sstevel@tonic-gate #define	FILE_LINKED	2
987c478bd9Sstevel@tonic-gate #define	FILE_PASS_ERR	-1
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate #define	ARCHIVE_NORMAL	0
1017c478bd9Sstevel@tonic-gate #define	ARCHIVE_ACL	1
1027c478bd9Sstevel@tonic-gate #define	ARCHIVE_XATTR	2
1037c478bd9Sstevel@tonic-gate 
104ced83f9bSceastha #ifndef	VIEW_READONLY
105ced83f9bSceastha #define	VIEW_READONLY	"SUNWattr_ro"
106ced83f9bSceastha #endif
107ced83f9bSceastha 
108ced83f9bSceastha #ifndef	VIEW_READWRITE
109ced83f9bSceastha #define	VIEW_READWRITE	"SUNWattr_rw"
110ced83f9bSceastha #endif
111ced83f9bSceastha 
112ced83f9bSceastha 
1137c478bd9Sstevel@tonic-gate #define	LSTAT(dir, path, statbuf) fstatat(dir, \
114ced83f9bSceastha     get_component((Gen.g_attrnam_p == NULL) ? \
1157c478bd9Sstevel@tonic-gate     path : Gen.g_attrnam_p), statbuf, AT_SYMLINK_NOFOLLOW)
1167c478bd9Sstevel@tonic-gate #define	STAT(dir, path, statbuf) fstatat(dir, \
117ced83f9bSceastha     get_component((Gen.g_attrnam_p == NULL) ? \
1187c478bd9Sstevel@tonic-gate     path : Gen.g_attrnam_p), statbuf, 0)
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate  *	These limits reflect the maximum size regular file that
1227c478bd9Sstevel@tonic-gate  *	can be archived, depending on the archive type. For archives
1237c478bd9Sstevel@tonic-gate  *	with character-format headers (odc, tar, ustar) we use
124944d83a0Schin  *	CHAR_OFFSET_MAX.  For archives with SVR4 ASCII headers (-c, -H crc)
125944d83a0Schin  *	we store filesize in an 8-char hexadecimal string and use
126944d83a0Schin  *	ASC_OFFSET_MAX.  Otherwise, we are limited to the size that will
127944d83a0Schin  *	fit in a signed long value.
1287c478bd9Sstevel@tonic-gate  */
129944d83a0Schin #define	CHAR_OFFSET_MAX	077777777777ULL	/* 11 octal digits */
130944d83a0Schin #define	ASC_OFFSET_MAX	0XFFFFFFFF	/* 8 hexadecimal digits */
1317c478bd9Sstevel@tonic-gate #define	BIN_OFFSET_MAX	LONG_MAX	/* signed long max value */
1327c478bd9Sstevel@tonic-gate 
133d2443e76Smarks #define	POSIXMODES	07777
134d2443e76Smarks 
1357c478bd9Sstevel@tonic-gate static char	aclchar = ' ';
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate static struct Lnk *add_lnk(struct Lnk **);
1387c478bd9Sstevel@tonic-gate static int bfill(void);
1397c478bd9Sstevel@tonic-gate static void bflush(void);
1407c478bd9Sstevel@tonic-gate static int chgreel(int dir);
1417c478bd9Sstevel@tonic-gate static int ckname(int);
1427c478bd9Sstevel@tonic-gate static void ckopts(long mask);
1437c478bd9Sstevel@tonic-gate static long cksum(char hdr, int byt_cnt, int *err);
1447c478bd9Sstevel@tonic-gate static int creat_hdr(void);
1457c478bd9Sstevel@tonic-gate static int creat_lnk(int dirfd, char *name1_p, char *name2_p);
1467c478bd9Sstevel@tonic-gate static int creat_spec(int dirfd);
1477c478bd9Sstevel@tonic-gate static int creat_tmp(char *nam_p);
1487c478bd9Sstevel@tonic-gate static void data_in(int proc_mode);
1497c478bd9Sstevel@tonic-gate static void data_out(void);
1507c478bd9Sstevel@tonic-gate static void data_pass(void);
1517c478bd9Sstevel@tonic-gate static void file_in(void);
1527c478bd9Sstevel@tonic-gate static int file_out(void);
1537c478bd9Sstevel@tonic-gate static int file_pass(void);
1547c478bd9Sstevel@tonic-gate static void flush_lnks(void);
1557c478bd9Sstevel@tonic-gate static int gethdr(void);
1567c478bd9Sstevel@tonic-gate static int getname(void);
1577c478bd9Sstevel@tonic-gate static void getpats(int largc, char **largv);
1587c478bd9Sstevel@tonic-gate static void ioerror(int dir);
1597c478bd9Sstevel@tonic-gate static int matched(void);
1607c478bd9Sstevel@tonic-gate static int missdir(char *nam_p);
1617c478bd9Sstevel@tonic-gate static long mklong(short v[]);
1627c478bd9Sstevel@tonic-gate static void mkshort(short sval[], long v);
1637c478bd9Sstevel@tonic-gate static void msg(int severity, const char *fmt, ...);
1647c478bd9Sstevel@tonic-gate static int openout(int dirfd);
1657c478bd9Sstevel@tonic-gate static int read_hdr(int hdr);
1667c478bd9Sstevel@tonic-gate static void reclaim(struct Lnk *l_p);
1677c478bd9Sstevel@tonic-gate static void rstbuf(void);
1687c478bd9Sstevel@tonic-gate static void setpasswd(char *nam);
1697c478bd9Sstevel@tonic-gate static void rstfiles(int over, int dirfd);
1707c478bd9Sstevel@tonic-gate static void scan4trail(void);
1717c478bd9Sstevel@tonic-gate static void setup(int largc, char **largv);
1727c478bd9Sstevel@tonic-gate static void set_tym(int dirfd, char *nam_p, time_t atime, time_t mtime);
1737c478bd9Sstevel@tonic-gate static void sigint(int sig);
1747c478bd9Sstevel@tonic-gate static void swap(char *buf_p, int cnt);
1757c478bd9Sstevel@tonic-gate static void usage(void);
1767c478bd9Sstevel@tonic-gate static void verbose(char *nam_p);
1777c478bd9Sstevel@tonic-gate static void write_hdr(int secflag, off_t len);
1787c478bd9Sstevel@tonic-gate static void write_trail(void);
1797c478bd9Sstevel@tonic-gate static int ustar_dir(void);
1807c478bd9Sstevel@tonic-gate static int ustar_spec(void);
1817c478bd9Sstevel@tonic-gate static struct stat *convert_to_old_stat(struct stat *, char *, char *);
1827c478bd9Sstevel@tonic-gate static void read_bar_vol_hdr(void);
1837c478bd9Sstevel@tonic-gate static void read_bar_file_hdr(void);
1847c478bd9Sstevel@tonic-gate static void setup_uncompress(FILE **);
1857c478bd9Sstevel@tonic-gate static void skip_bar_volhdr(void);
1867c478bd9Sstevel@tonic-gate static void bar_file_in(void);
1877c478bd9Sstevel@tonic-gate static int g_init(int *devtype, int *fdes);
1887c478bd9Sstevel@tonic-gate static int g_read(int, int, char *, unsigned);
1897c478bd9Sstevel@tonic-gate static int g_write(int, int, char *, unsigned);
1907c478bd9Sstevel@tonic-gate static int is_floppy(int);
1917c478bd9Sstevel@tonic-gate static int is_tape(int);
1927c478bd9Sstevel@tonic-gate static void write_ancillary(char *secinfo, int len);
1937c478bd9Sstevel@tonic-gate static int remove_dir(char *);
1947c478bd9Sstevel@tonic-gate static int save_cwd(void);
1957c478bd9Sstevel@tonic-gate static void rest_cwd(int cwd);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate static void xattrs_out(int (*func)());
1987c478bd9Sstevel@tonic-gate static void get_parent(char *path, char *dir);
1997c478bd9Sstevel@tonic-gate static void prepare_xattr_hdr(char **attrbuf, char *filename,
2007c478bd9Sstevel@tonic-gate     char *attrname, char typeflag, struct Lnk *linkinfo, int *rlen);
2017c478bd9Sstevel@tonic-gate static char tartype(int type);
2027c478bd9Sstevel@tonic-gate static int openfile(int omode);
2037c478bd9Sstevel@tonic-gate static mode_t attrmode(char type);
2047c478bd9Sstevel@tonic-gate static char *get_component(char *path);
2057c478bd9Sstevel@tonic-gate static int open_dir(char *name);
2067c478bd9Sstevel@tonic-gate static int open_dirfd();
2077c478bd9Sstevel@tonic-gate static void close_dirfd();
2087c478bd9Sstevel@tonic-gate static void write_xattr_hdr();
2097c478bd9Sstevel@tonic-gate static char *skipslashes(char *string, char *start);
2107c478bd9Sstevel@tonic-gate static int read_xattr_hdr();
2117c478bd9Sstevel@tonic-gate static void chop_endslashes(char *path);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate /* helpful types */
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate static
2177c478bd9Sstevel@tonic-gate struct passwd	*Curpw_p,	/* Current password entry for -t option */
2187c478bd9Sstevel@tonic-gate 		*Rpw_p,		/* Password entry for -R option */
2197c478bd9Sstevel@tonic-gate 		*dpasswd;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate static
2227c478bd9Sstevel@tonic-gate struct group	*Curgr_p,	/* Current group entry for -t option */
2237c478bd9Sstevel@tonic-gate 		*dgroup;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /* Data structure for buffered I/O. */
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate static
2287c478bd9Sstevel@tonic-gate struct buf_info {
2297c478bd9Sstevel@tonic-gate 	char	*b_base_p,	/* Pointer to base of buffer */
2307c478bd9Sstevel@tonic-gate 		*b_out_p,	/* Position to take bytes from buffer at */
2317c478bd9Sstevel@tonic-gate 		*b_in_p,	/* Position to put bytes into buffer at */
2327c478bd9Sstevel@tonic-gate 		*b_end_p;	/* Pointer to end of buffer */
2337c478bd9Sstevel@tonic-gate 	long	b_cnt,		/* Count of unprocessed bytes */
2347c478bd9Sstevel@tonic-gate 		b_size;		/* Size of buffer in bytes */
2357c478bd9Sstevel@tonic-gate } Buffr;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate /* Generic header format */
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate static
2407c478bd9Sstevel@tonic-gate struct gen_hdr {
2417c478bd9Sstevel@tonic-gate 	ulong_t	g_magic,	/* Magic number field */
2427c478bd9Sstevel@tonic-gate 		g_ino,		/* Inode number of file */
2437c478bd9Sstevel@tonic-gate 		g_mode,		/* Mode of file */
2447c478bd9Sstevel@tonic-gate 		g_uid,		/* Uid of file */
2457c478bd9Sstevel@tonic-gate 		g_gid,		/* Gid of file */
2467c478bd9Sstevel@tonic-gate 		g_nlink,	/* Number of links */
2477c478bd9Sstevel@tonic-gate 		g_mtime;	/* Modification time */
2487c478bd9Sstevel@tonic-gate 	off_t	g_filesz;	/* Length of file */
2497c478bd9Sstevel@tonic-gate 	ulong_t	g_dev,		/* File system of file */
2507c478bd9Sstevel@tonic-gate 		g_rdev,		/* Major/minor numbers of special files */
2517c478bd9Sstevel@tonic-gate 		g_namesz,	/* Length of filename */
2527c478bd9Sstevel@tonic-gate 		g_cksum;	/* Checksum of file */
2537c478bd9Sstevel@tonic-gate 	char	g_gname[32],
2547c478bd9Sstevel@tonic-gate 		g_uname[32],
2557c478bd9Sstevel@tonic-gate 		g_version[2],
2567c478bd9Sstevel@tonic-gate 		g_tmagic[6],
2577c478bd9Sstevel@tonic-gate 		g_typeflag;
2587c478bd9Sstevel@tonic-gate 	char	*g_tname,
2597c478bd9Sstevel@tonic-gate 		*g_prefix,
2607c478bd9Sstevel@tonic-gate 		*g_nam_p,	/* Filename */
261ced83f9bSceastha 		*g_attrparent_p, /* attribute parent */
262ced83f9bSceastha 		*g_attrpath_p, /* attribute path */
2637c478bd9Sstevel@tonic-gate 		*g_attrnam_p,	/* attribute */
2647c478bd9Sstevel@tonic-gate 		*g_attrfnam_p,  /* Real file name attr belongs to */
2657c478bd9Sstevel@tonic-gate 		*g_linktoattrfnam_p, /* file linked attribute belongs to */
2667c478bd9Sstevel@tonic-gate 		*g_linktoattrnam_p,  /* attribute g_attrnam_p is linked to */
2677c478bd9Sstevel@tonic-gate 		*g_dirpath;	/* dirname currently opened */
2687c478bd9Sstevel@tonic-gate 	int	g_dirfd;	/* directory file descriptor */
2697c478bd9Sstevel@tonic-gate 	int	g_passdirfd;	/* directory fd to pass to */
270ced83f9bSceastha 	int	g_rw_sysattr;	/* read-write system attribute */
271ced83f9bSceastha 	int	g_baseparent_fd;	/* base file's parent fd */
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate } Gen, *G_p;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate /* Data structure for handling multiply-linked files */
2767c478bd9Sstevel@tonic-gate static
2777c478bd9Sstevel@tonic-gate char	prebuf[PRESIZ+1],
2787c478bd9Sstevel@tonic-gate 	nambuf[NAMSIZ+1],
2797c478bd9Sstevel@tonic-gate 	fullnam[MAXNAM+1];
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate static
2837c478bd9Sstevel@tonic-gate struct Lnk {
2847c478bd9Sstevel@tonic-gate 	short	L_cnt,		/* Number of links encountered */
2857c478bd9Sstevel@tonic-gate 		L_data;		/* Data has been encountered if 1 */
2867c478bd9Sstevel@tonic-gate 	struct gen_hdr	L_gen;	/* gen_hdr information for this file */
2877c478bd9Sstevel@tonic-gate 	struct Lnk	*L_nxt_p,	/* Next file in list */
2887c478bd9Sstevel@tonic-gate 			*L_bck_p,	/* Previous file in list */
2897c478bd9Sstevel@tonic-gate 			*L_lnk_p;	/* Next link for this file */
2907c478bd9Sstevel@tonic-gate } Lnk_hd;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate static
2937c478bd9Sstevel@tonic-gate struct hdr_cpio	Hdr;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate /*
2967c478bd9Sstevel@tonic-gate  * -------------------------------------------------------------------------
2977c478bd9Sstevel@tonic-gate  *		   Stuff needed to pre-view the name stream
2987c478bd9Sstevel@tonic-gate  *
2997c478bd9Sstevel@tonic-gate  * issymlink is used to remember that the current file is a symlink between
3007c478bd9Sstevel@tonic-gate  * getname() and file_pass(); the former trashes this information immediately
3017c478bd9Sstevel@tonic-gate  * when -L is specified.
3027c478bd9Sstevel@tonic-gate  */
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate static
3057c478bd9Sstevel@tonic-gate int	issymlink = 0;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate static
3087c478bd9Sstevel@tonic-gate FILE	*In_p = stdin;		/* Where the input comes from */
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate typedef struct sl_info
3117c478bd9Sstevel@tonic-gate {
3127c478bd9Sstevel@tonic-gate 	struct sl_info *llink;	/* Left subtree ptr (tree depth in *sl_head) */
3137c478bd9Sstevel@tonic-gate 	struct sl_info *rlink;	/* Right subtree ptr */
3147c478bd9Sstevel@tonic-gate 	int bal;		/* Subtree balance factor */
3157c478bd9Sstevel@tonic-gate 	ulong_t	sl_count;	/* Number of symlinks */
316ced83f9bSceastha 	int	sl_ftype;	/* file type of inode */
3177c478bd9Sstevel@tonic-gate 	ino_t	sl_ino;		/* Inode of file */
3187c478bd9Sstevel@tonic-gate 	ino_t	sl_ino2;	/* alternate inode for -Hodc */
3197c478bd9Sstevel@tonic-gate } sl_info_t;
3207c478bd9Sstevel@tonic-gate 
321ced83f9bSceastha typedef struct data_in
322ced83f9bSceastha {
323ced83f9bSceastha 	int	data_in_swapfile;
324ced83f9bSceastha 	int	data_in_proc_mode;
325ced83f9bSceastha 	int	data_in_partialflg;
326ced83f9bSceastha 	int	data_in_compress_flag;
327ced83f9bSceastha 	long	data_in_cksumval;
328ced83f9bSceastha 	FILE	*data_in_pipef;
329ced83f9bSceastha } data_in_t;
330ced83f9bSceastha 
3317c478bd9Sstevel@tonic-gate /*
3327c478bd9Sstevel@tonic-gate  * The following structure maintains a hash entry for the
3337c478bd9Sstevel@tonic-gate  * balancing trees which are allocated for each device nodes.
3347c478bd9Sstevel@tonic-gate  */
3357c478bd9Sstevel@tonic-gate typedef struct sl_info_link
3367c478bd9Sstevel@tonic-gate {
3377c478bd9Sstevel@tonic-gate 	dev_t		dev;
3387c478bd9Sstevel@tonic-gate 	sl_info_t	*head;
3397c478bd9Sstevel@tonic-gate 	struct sl_info_link *next;
3407c478bd9Sstevel@tonic-gate } sl_info_link_t;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate #define	SL_INFO_ALLOC_CHUNK	1024
3437c478bd9Sstevel@tonic-gate #define	NDEVHENTRY		0x40
3447c478bd9Sstevel@tonic-gate #define	DEV_HASHKEY(x)		((x) & (NDEVHENTRY -1))
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate /*
3477c478bd9Sstevel@tonic-gate  * For remapping dev,inode for -Hodc archives.
3487c478bd9Sstevel@tonic-gate  */
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate typedef struct sl_remap
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate 	dev_t			dev;		/* device */
3537c478bd9Sstevel@tonic-gate 	int			inode_count;	/* # inodes seen on dev */
3547c478bd9Sstevel@tonic-gate 	struct sl_remap 	*next;		/* next in the chain */
3557c478bd9Sstevel@tonic-gate } sl_remap_t;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate /* forward declarations */
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate static sl_info_t 	*sl_info_alloc(void);
360ced83f9bSceastha static sl_info_t 	*sl_insert(dev_t, ino_t, int);
361ced83f9bSceastha static ulong_t		sl_numlinks(dev_t, ino_t, int);
3627c478bd9Sstevel@tonic-gate static void		sl_preview_synonyms(void);
363ced83f9bSceastha static void		sl_remember_tgt(const struct stat *, int, int);
364ced83f9bSceastha static sl_info_t 	*sl_search(dev_t, ino_t, int);
3657c478bd9Sstevel@tonic-gate static sl_info_t	*sl_devhash_lookup(dev_t);
3667c478bd9Sstevel@tonic-gate static void		sl_devhash_insert(dev_t, sl_info_t *);
3677c478bd9Sstevel@tonic-gate 
368ced83f9bSceastha extern int		sl_compare(ino_t, int, ino_t, int);
369ced83f9bSceastha #define	sl_compare(lino, lftype, rino, rftype)	(lino < rino ? -1 : \
370ced83f9bSceastha 	    (lino > rino ? 1 : (lftype < rftype ? -1 : \
371ced83f9bSceastha 	    (lftype > rftype ? 1 : 0))))
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate /* global storage */
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate static sl_remap_t  *sl_remap_head = NULL; /* head of the inode-remap list */
3767c478bd9Sstevel@tonic-gate static sl_info_link_t	*sl_devhash[NDEVHENTRY]; /* hash table */
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate /*
3797c478bd9Sstevel@tonic-gate  * -------------------------------------------------------------------------
3807c478bd9Sstevel@tonic-gate  */
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate static
3837c478bd9Sstevel@tonic-gate struct stat	ArchSt,	/* stat(2) information of the archive */
3847c478bd9Sstevel@tonic-gate 		SrcSt,	/* stat(2) information of source file */
3857c478bd9Sstevel@tonic-gate 		DesSt,	/* stat(2) of destination file */
3867c478bd9Sstevel@tonic-gate 		*OldSt = NULL;	/* stat info converted to svr32 format */
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate /*
3897c478bd9Sstevel@tonic-gate  * bin_mag: Used to validate a binary magic number,
3907c478bd9Sstevel@tonic-gate  * by combining to bytes into an unsigned short.
3917c478bd9Sstevel@tonic-gate  */
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate static
3947c478bd9Sstevel@tonic-gate union bin_mag {
3957c478bd9Sstevel@tonic-gate 	unsigned char b_byte[2];
3967c478bd9Sstevel@tonic-gate 	ushort_t b_half;
3977c478bd9Sstevel@tonic-gate } Binmag;
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate static
4007c478bd9Sstevel@tonic-gate union tblock *Thdr_p;	/* TAR header pointer */
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate static union b_block *bar_Vhdr;
4037c478bd9Sstevel@tonic-gate static struct gen_hdr Gen_bar_vol;
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate /*
4067c478bd9Sstevel@tonic-gate  * swpbuf: Used in swap() to swap bytes within a halfword,
4077c478bd9Sstevel@tonic-gate  * halfwords within a word, or to reverse the order of the
4087c478bd9Sstevel@tonic-gate  * bytes within a word.  Also used in mklong() and mkshort().
4097c478bd9Sstevel@tonic-gate  */
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate static
4127c478bd9Sstevel@tonic-gate union swpbuf {
4137c478bd9Sstevel@tonic-gate 	unsigned char	s_byte[4];
4147c478bd9Sstevel@tonic-gate 	ushort_t	s_half[2];
4157c478bd9Sstevel@tonic-gate 	ulong_t	s_word;
4167c478bd9Sstevel@tonic-gate } *Swp_p;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate static
419ced83f9bSceastha char	*myname,		/* program name */
420ced83f9bSceastha 	Adir,			/* Flags object as a directory */
4217c478bd9Sstevel@tonic-gate 	Hiddendir,		/* Processing hidden attribute directory */
4227c478bd9Sstevel@tonic-gate 	Aspec,			/* Flags object as a special file */
4237c478bd9Sstevel@tonic-gate 	Do_rename,		/* Indicates rename() is to be used */
4247c478bd9Sstevel@tonic-gate 	Time[50],		/* Array to hold date and time */
4257c478bd9Sstevel@tonic-gate 	Ttyname[] = "/dev/tty",	/* Controlling console */
4267c478bd9Sstevel@tonic-gate 	T_lname[MAXPATHLEN],	/* Array to hold links name for tar */
4277c478bd9Sstevel@tonic-gate 	*Buf_p,			/* Buffer for file system I/O */
4287c478bd9Sstevel@tonic-gate 	*Empty,			/* Empty block for TARTYP headers */
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 */
4547c478bd9Sstevel@tonic-gate 	Def_mode = 0777,	/* Default file/directory protection modes */
4557c478bd9Sstevel@tonic-gate 	Device,		/* Device type being accessed (used with libgenIO) */
4567c478bd9Sstevel@tonic-gate 	Error_cnt = 0,	/* Cumulative count of I/O errors */
4577c478bd9Sstevel@tonic-gate 	Finished = 1,	/* Indicates that a file transfer has completed */
4587c478bd9Sstevel@tonic-gate 	Hdrsz = ASCSZ,	/* Fixed length portion of the header */
4597c478bd9Sstevel@tonic-gate 	Hdr_type,		/* Flag to indicate type of header selected */
4607c478bd9Sstevel@tonic-gate 	Ifile,		/* File des. of file being archived */
4617c478bd9Sstevel@tonic-gate 	Ofile,		/* File des. of file being extracted from archive */
4627c478bd9Sstevel@tonic-gate 	Use_old_stat = 0,    /* Create an old style -Hodc hdr (small dev's) */
4637c478bd9Sstevel@tonic-gate 	Onecopy = 0,	/* Flags old vs. new link handling */
4647c478bd9Sstevel@tonic-gate 	Pad_val = 0,	/* Indicates the number of bytes to pad (if any) */
4657c478bd9Sstevel@tonic-gate 	PageSize = 0,	/* The native page size, used for figuring block size */
4667c478bd9Sstevel@tonic-gate 	Volcnt = 1,	/* Number of archive volumes processed */
4677c478bd9Sstevel@tonic-gate 	Verbcnt = 0,	/* Count of number of dots '.' output */
4687c478bd9Sstevel@tonic-gate 	Eomflag = 0,
4697c478bd9Sstevel@tonic-gate 	Dflag = 0,
4707c478bd9Sstevel@tonic-gate 	Atflag = 0,	/* Archive/restore extended attributes */
471ced83f9bSceastha 	SysAtflag = 0,	/* Archive/restore extended system attributes */
4727c478bd9Sstevel@tonic-gate 	Compressed,	/* Flag to indicate if the bar archive is compressed */
473647ab8f4Sceastha 	Bar_vol_num = 0, /* Volume number count for bar archive */
474ced83f9bSceastha 	privileged = 0,	/* Flag set if running with higher privileges */
475ced83f9bSceastha 	attr_baseparent_fd = -1;	/* attribute's base file descriptor */
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate static
479f48205beScasper gid_t	Lastgid = (gid_t)-1;	/* Used with -t & -v to record current gid */
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate static
482f48205beScasper uid_t	Lastuid = (uid_t)-1;	/* Used with -t & -v to record current uid */
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate static
4857c478bd9Sstevel@tonic-gate long	Args,		/* Mask of selected options */
4867c478bd9Sstevel@tonic-gate 	Max_namesz = CPATH;	/* Maximum size of pathnames/filenames */
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate static
4897c478bd9Sstevel@tonic-gate int	Bufsize = BUFSZ;	/* Default block size */
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate static u_longlong_t    Blocks;	/* full blocks transferred */
4937c478bd9Sstevel@tonic-gate static u_longlong_t    SBlocks;	/* cumulative char count from short reads */
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate static off_t	Max_offset = BIN_OFFSET_MAX;	/* largest file size */
4977c478bd9Sstevel@tonic-gate static off_t	Max_filesz;			/* from getrlimit */
4987c478bd9Sstevel@tonic-gate 
499ced83f9bSceastha static ulong_t	Savedev;
500ced83f9bSceastha 
5017c478bd9Sstevel@tonic-gate static
5027c478bd9Sstevel@tonic-gate FILE	*Ef_p,			/* File pointer of pattern input file */
5037c478bd9Sstevel@tonic-gate 	*Err_p = stderr,	/* File pointer for error reporting */
5047c478bd9Sstevel@tonic-gate 	*Out_p = stdout,	/* File pointer for non-archive output */
5057c478bd9Sstevel@tonic-gate 	*Rtty_p,		/* Input file pointer for interactive rename */
5067c478bd9Sstevel@tonic-gate 	*Wtty_p;		/* Output file ptr for interactive rename */
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate static
5097c478bd9Sstevel@tonic-gate ushort_t	Ftype = S_IFMT;	/* File type mask */
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate /* ACL support */
5127c478bd9Sstevel@tonic-gate static struct sec_attr {
5137c478bd9Sstevel@tonic-gate 	char	attr_type;
5147c478bd9Sstevel@tonic-gate 	char	attr_len[7];
5157c478bd9Sstevel@tonic-gate 	char	attr_info[1];
5167c478bd9Sstevel@tonic-gate } *attr;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate static int	Pflag = 0;	/* flag indicates that acl is preserved */
519fa9e4066Sahrens static int	acl_is_set = 0; /* True if an acl was set on the file */
520fa9e4066Sahrens 
521fa9e4066Sahrens acl_t *aclp;
5227c478bd9Sstevel@tonic-gate 
523ced83f9bSceastha #if defined(O_XATTR)
524ced83f9bSceastha typedef enum {
525ced83f9bSceastha 	ATTR_OK,
526ced83f9bSceastha 	ATTR_SKIP,
527ced83f9bSceastha 	ATTR_CHDIR_ERR,
528ced83f9bSceastha 	ATTR_OPEN_ERR,
529ced83f9bSceastha 	ATTR_XATTR_ERR,
530ced83f9bSceastha 	ATTR_SATTR_ERR
531ced83f9bSceastha } attr_status_t;
532ced83f9bSceastha #endif
533ced83f9bSceastha 
534ced83f9bSceastha #if defined(O_XATTR)
535ced83f9bSceastha typedef enum {
536ced83f9bSceastha 	ARC_CREATE,
537ced83f9bSceastha 	ARC_RESTORE
538ced83f9bSceastha } arc_action_t;
539ced83f9bSceastha #endif
540ced83f9bSceastha 
541ced83f9bSceastha 
5427c478bd9Sstevel@tonic-gate /*
5437c478bd9Sstevel@tonic-gate  *
5447c478bd9Sstevel@tonic-gate  * cpio has been changed to support extended attributes.
5457c478bd9Sstevel@tonic-gate  *
5467c478bd9Sstevel@tonic-gate  * As part of this change cpio has been changed to use the new *at() syscalls
5477c478bd9Sstevel@tonic-gate  * such as openat, fchownat(), unlinkat()...
5487c478bd9Sstevel@tonic-gate  *
5497c478bd9Sstevel@tonic-gate  * This was done so that attributes can be handled with as few code changes
5507c478bd9Sstevel@tonic-gate  * as possible.
5517c478bd9Sstevel@tonic-gate  *
5527c478bd9Sstevel@tonic-gate  * What this means is that cpio now opens the directory that a file or directory
5537c478bd9Sstevel@tonic-gate  * resides in and then performs *at() functions to manipulate the entry.
5547c478bd9Sstevel@tonic-gate  *
5557c478bd9Sstevel@tonic-gate  * For example a new file is now created like this:
5567c478bd9Sstevel@tonic-gate  *
5577c478bd9Sstevel@tonic-gate  * dfd = open(<some dir path>)
5587c478bd9Sstevel@tonic-gate  * fd = openat(dfd, <name>,....);
5597c478bd9Sstevel@tonic-gate  *
5607c478bd9Sstevel@tonic-gate  * or in the case of an extended attribute
5617c478bd9Sstevel@tonic-gate  *
5627c478bd9Sstevel@tonic-gate  * dfd = attropen(<pathname>, ".", ....)
5637c478bd9Sstevel@tonic-gate  *
5647c478bd9Sstevel@tonic-gate  * Once we have a directory file descriptor all of the *at() functions can
5657c478bd9Sstevel@tonic-gate  * be applied to it.
5667c478bd9Sstevel@tonic-gate  *
5677c478bd9Sstevel@tonic-gate  * unlinkat(dfd, <component name>,...)
5687c478bd9Sstevel@tonic-gate  * fchownat(dfd, <component name>,..)
5697c478bd9Sstevel@tonic-gate  *
5707c478bd9Sstevel@tonic-gate  * This works for both normal namespace files and extended attribute file
5717c478bd9Sstevel@tonic-gate  *
5727c478bd9Sstevel@tonic-gate  */
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate /*
5757c478bd9Sstevel@tonic-gate  * Extended attribute layout
5767c478bd9Sstevel@tonic-gate  *
5777c478bd9Sstevel@tonic-gate  * Extended attributes are stored in two pieces.
5787c478bd9Sstevel@tonic-gate  * 1. An attribute header which has information about
5797c478bd9Sstevel@tonic-gate  *    what file the attribute is for and what the attribute
5807c478bd9Sstevel@tonic-gate  *    is named.
5817c478bd9Sstevel@tonic-gate  * 2. The attribute record itself.  Stored as a normal file type
5827c478bd9Sstevel@tonic-gate  *    of entry.
5837c478bd9Sstevel@tonic-gate  * Both the header and attribute record have special modes/typeflags
5847c478bd9Sstevel@tonic-gate  * associated with them.
5857c478bd9Sstevel@tonic-gate  *
5867c478bd9Sstevel@tonic-gate  * The names of the header in the archive look like:
5877c478bd9Sstevel@tonic-gate  * /dev/null/attr.hdr
5887c478bd9Sstevel@tonic-gate  *
5897c478bd9Sstevel@tonic-gate  * The name of the attribute looks like:
5907c478bd9Sstevel@tonic-gate  * /dev/null/attr.
5917c478bd9Sstevel@tonic-gate  *
5927c478bd9Sstevel@tonic-gate  * This is done so that an archiver that doesn't understand these formats
5937c478bd9Sstevel@tonic-gate  * can just dispose of the attribute records unless the user chooses to
5947c478bd9Sstevel@tonic-gate  * rename them via cpio -r or pax -i
5957c478bd9Sstevel@tonic-gate  *
5967c478bd9Sstevel@tonic-gate  * The format is composed of a fixed size header followed
5977c478bd9Sstevel@tonic-gate  * by a variable sized xattr_buf. If the attribute is a hard link
5987c478bd9Sstevel@tonic-gate  * to another attribute, then another xattr_buf section is included
5997c478bd9Sstevel@tonic-gate  * for the link.
6007c478bd9Sstevel@tonic-gate  *
6017c478bd9Sstevel@tonic-gate  * The xattr_buf is used to define the necessary "pathing" steps
6027c478bd9Sstevel@tonic-gate  * to get to the extended attribute.  This is necessary to support
6037c478bd9Sstevel@tonic-gate  * a fully recursive attribute model where an attribute may itself
6047c478bd9Sstevel@tonic-gate  * have an attribute.
6057c478bd9Sstevel@tonic-gate  *
6067c478bd9Sstevel@tonic-gate  * The basic layout looks like this.
6077c478bd9Sstevel@tonic-gate  *
6087c478bd9Sstevel@tonic-gate  *     --------------------------------
6097c478bd9Sstevel@tonic-gate  *     |                              |
6107c478bd9Sstevel@tonic-gate  *     |         xattr_hdr            |
6117c478bd9Sstevel@tonic-gate  *     |                              |
6127c478bd9Sstevel@tonic-gate  *     --------------------------------
6137c478bd9Sstevel@tonic-gate  *     --------------------------------
6147c478bd9Sstevel@tonic-gate  *     |                              |
6157c478bd9Sstevel@tonic-gate  *     |        xattr_buf             |
6167c478bd9Sstevel@tonic-gate  *     |                              |
6177c478bd9Sstevel@tonic-gate  *     --------------------------------
6187c478bd9Sstevel@tonic-gate  *     --------------------------------
6197c478bd9Sstevel@tonic-gate  *     |                              |
6207c478bd9Sstevel@tonic-gate  *     |      (optional link info)    |
6217c478bd9Sstevel@tonic-gate  *     |                              |
6227c478bd9Sstevel@tonic-gate  *     --------------------------------
6237c478bd9Sstevel@tonic-gate  *     --------------------------------
6247c478bd9Sstevel@tonic-gate  *     |                              |
6257c478bd9Sstevel@tonic-gate  *     |      attribute itself        |
6267c478bd9Sstevel@tonic-gate  *     |      stored as normal tar    |
6277c478bd9Sstevel@tonic-gate  *     |      or cpio data with       |
6287c478bd9Sstevel@tonic-gate  *     |      special mode or         |
6297c478bd9Sstevel@tonic-gate  *     |      typeflag                |
6307c478bd9Sstevel@tonic-gate  *     |                              |
6317c478bd9Sstevel@tonic-gate  *     --------------------------------
6327c478bd9Sstevel@tonic-gate  *
6337c478bd9Sstevel@tonic-gate  */
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate /*
6367c478bd9Sstevel@tonic-gate  * Extended attributes structures
6377c478bd9Sstevel@tonic-gate  *
6387c478bd9Sstevel@tonic-gate  * xattrhead is the complete extended attribute header, as read of off
6397c478bd9Sstevel@tonic-gate  * disk/tape. It includes the variable xattr_buf portion.
6407c478bd9Sstevel@tonic-gate  *
6417c478bd9Sstevel@tonic-gate  * xattrp is basically an offset into xattrhead that points to the
6427c478bd9Sstevel@tonic-gate  * "pathing" section which defines how to get to the attribute.
6437c478bd9Sstevel@tonic-gate  *
6447c478bd9Sstevel@tonic-gate  * xattr_linkp is identical to xattrp except that it is used for linked
6457c478bd9Sstevel@tonic-gate  * attributes.  It provides the pathing steps to get to the linked
6467c478bd9Sstevel@tonic-gate  * attribute.
6477c478bd9Sstevel@tonic-gate  *
6487c478bd9Sstevel@tonic-gate  * These structures are updated when an extended attribute header is read off
6497c478bd9Sstevel@tonic-gate  * of disk/tape.
6507c478bd9Sstevel@tonic-gate  */
6517c478bd9Sstevel@tonic-gate static struct xattr_hdr	*xattrhead;
6527c478bd9Sstevel@tonic-gate static struct xattr_buf	*xattrp;
6537c478bd9Sstevel@tonic-gate static struct xattr_buf	*xattr_linkp;
6547c478bd9Sstevel@tonic-gate static int 		xattrbadhead;	/* is extended attribute header bad? */
6557c478bd9Sstevel@tonic-gate 
656fa9e4066Sahrens static int	append_secattr(char **, int *, acl_t *);
6577c478bd9Sstevel@tonic-gate static void	write_ancillary(char *, int);
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 /*
6927c478bd9Sstevel@tonic-gate  * Allocation wrappers and their flags
6937c478bd9Sstevel@tonic-gate  */
6947c478bd9Sstevel@tonic-gate #define	E_NORMAL	0x0	/* Return NULL if allocation fails */
6957c478bd9Sstevel@tonic-gate #define	E_EXIT		0x1	/* Exit if allocation fails */
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate static void *e_realloc(int flag, void *old, size_t newsize);
6987c478bd9Sstevel@tonic-gate static char *e_strdup(int flag, const char *arg);
6997c478bd9Sstevel@tonic-gate static void *e_valloc(int flag, size_t size);
7007c478bd9Sstevel@tonic-gate static void *e_zalloc(int flag, size_t size);
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate #define	EXIT_CODE	(Error_cnt > 255 ? 255 : Error_cnt)
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate /*
7057c478bd9Sstevel@tonic-gate  * main: Call setup() to process options and perform initializations,
7067c478bd9Sstevel@tonic-gate  * and then select either copy in (-i), copy out (-o), or pass (-p) action.
7077c478bd9Sstevel@tonic-gate  */
7087c478bd9Sstevel@tonic-gate 
709944d83a0Schin int
7107c478bd9Sstevel@tonic-gate main(int argc, char **argv)
7117c478bd9Sstevel@tonic-gate {
7127c478bd9Sstevel@tonic-gate 	int i;
7137c478bd9Sstevel@tonic-gate 	int passret;
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
7167c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
7177c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
7187c478bd9Sstevel@tonic-gate #endif
7197c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	(void) memset(&Gen, 0, sizeof (Gen));
722ced83f9bSceastha 	myname = e_strdup(E_EXIT, basename(argv[0]));
7237c478bd9Sstevel@tonic-gate 	setup(argc, argv);
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	if (signal(SIGINT, sigint) == SIG_IGN)
7267c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, SIG_IGN);
7277c478bd9Sstevel@tonic-gate 	switch (Args & (OCi | OCo | OCp)) {
7287c478bd9Sstevel@tonic-gate 	case OCi: /* COPY IN */
7297c478bd9Sstevel@tonic-gate 		Hdr_type = NONE;
730ced83f9bSceastha 		if (Atflag || SysAtflag) {
731ced83f9bSceastha 			/*
732ced83f9bSceastha 			 * Save the current working directory, so
733ced83f9bSceastha 			 * we can change back here after cd'ing into
734ced83f9bSceastha 			 * the attribute directory when processing
735ced83f9bSceastha 			 * attributes.
736ced83f9bSceastha 			 */
737ced83f9bSceastha 			if ((attr_baseparent_fd = save_cwd()) < 0) {
738ced83f9bSceastha 				msg(EXT, "Unable to open current directory.");
739ced83f9bSceastha 			}
740ced83f9bSceastha 		}
7417c478bd9Sstevel@tonic-gate 		while ((i = gethdr()) != 0) {
7427c478bd9Sstevel@tonic-gate 			Gen.g_dirfd = -1;
7437c478bd9Sstevel@tonic-gate 			if (i == 1) {
7447c478bd9Sstevel@tonic-gate 				file_in();
7457c478bd9Sstevel@tonic-gate 				/*
7467c478bd9Sstevel@tonic-gate 				 * Any ACL info for this file would or should
7477c478bd9Sstevel@tonic-gate 				 * have been used after file_in(); clear out
7487c478bd9Sstevel@tonic-gate 				 * aclp so it is is not erroneously used on
7497c478bd9Sstevel@tonic-gate 				 * the next file.
7507c478bd9Sstevel@tonic-gate 				 */
7517c478bd9Sstevel@tonic-gate 				if (aclp != NULL) {
752fa9e4066Sahrens 					acl_free(aclp);
7537c478bd9Sstevel@tonic-gate 					aclp = NULL;
7547c478bd9Sstevel@tonic-gate 				}
755fa9e4066Sahrens 				acl_is_set = 0;
7567c478bd9Sstevel@tonic-gate 			}
7577c478bd9Sstevel@tonic-gate 			(void) memset(&Gen, 0, sizeof (Gen));
7587c478bd9Sstevel@tonic-gate 		}
7597c478bd9Sstevel@tonic-gate 		/* Do not count "extra" "read-ahead" buffered data */
7607c478bd9Sstevel@tonic-gate 		if (Buffr.b_cnt > Bufsize)
7617c478bd9Sstevel@tonic-gate 			Blocks -=  (u_longlong_t)(Buffr.b_cnt / Bufsize);
7627c478bd9Sstevel@tonic-gate 		break;
7637c478bd9Sstevel@tonic-gate 	case OCo: /* COPY OUT */
7647c478bd9Sstevel@tonic-gate 		if (Args & OCA) {
7657c478bd9Sstevel@tonic-gate 			scan4trail();
7667c478bd9Sstevel@tonic-gate 		}
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 		Gen.g_dirfd = -1;
7697c478bd9Sstevel@tonic-gate 		Gen.g_dirpath = NULL;
7707c478bd9Sstevel@tonic-gate 		sl_preview_synonyms();
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 		while ((i = getname()) != 0) {
7737c478bd9Sstevel@tonic-gate 			if (i == 1) {
7747c478bd9Sstevel@tonic-gate 				(void) file_out();
775ced83f9bSceastha 				if (Atflag || SysAtflag) {
7767c478bd9Sstevel@tonic-gate 					if (Gen.g_dirfd != -1) {
7777c478bd9Sstevel@tonic-gate 						(void) close(Gen.g_dirfd);
7787c478bd9Sstevel@tonic-gate 					}
7797c478bd9Sstevel@tonic-gate 					Gen.g_dirfd = -1;
7807c478bd9Sstevel@tonic-gate 					xattrs_out(file_out);
7817c478bd9Sstevel@tonic-gate 				}
7827c478bd9Sstevel@tonic-gate 			}
7837c478bd9Sstevel@tonic-gate 			if (aclp != NULL) {
784fa9e4066Sahrens 				acl_free(aclp);
7857c478bd9Sstevel@tonic-gate 				aclp = NULL;
786fa9e4066Sahrens 				acl_is_set = 0;
7877c478bd9Sstevel@tonic-gate 			}
7887c478bd9Sstevel@tonic-gate 		}
7897c478bd9Sstevel@tonic-gate 		write_trail();
7907c478bd9Sstevel@tonic-gate 		break;
7917c478bd9Sstevel@tonic-gate 	case OCp: /* PASS */
7927c478bd9Sstevel@tonic-gate 		sl_preview_synonyms();
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 		Gen.g_dirfd = -1;
7957c478bd9Sstevel@tonic-gate 		Gen.g_passdirfd = -1;
7967c478bd9Sstevel@tonic-gate 		Gen.g_dirpath = NULL;
7977c478bd9Sstevel@tonic-gate 		while (getname()) {
7987c478bd9Sstevel@tonic-gate 			/*
7997c478bd9Sstevel@tonic-gate 			 * If file is a fully qualified path then
8007c478bd9Sstevel@tonic-gate 			 * file_pass will strip off the leading '/'
8017c478bd9Sstevel@tonic-gate 			 * and we need to save off the unstripped
8027c478bd9Sstevel@tonic-gate 			 * name for attribute traversal.
8037c478bd9Sstevel@tonic-gate 			 */
804ced83f9bSceastha 			if (Atflag || SysAtflag) {
8057c478bd9Sstevel@tonic-gate 				(void) strcpy(Savenam_p, Gen.g_nam_p);
8067c478bd9Sstevel@tonic-gate 			}
8077c478bd9Sstevel@tonic-gate 			passret = file_pass();
8087c478bd9Sstevel@tonic-gate 			if (aclp != NULL) {
809fa9e4066Sahrens 				acl_free(aclp);
8107c478bd9Sstevel@tonic-gate 				aclp = NULL;
811fa9e4066Sahrens 				acl_is_set = 0;
8127c478bd9Sstevel@tonic-gate 			}
8137c478bd9Sstevel@tonic-gate 			if (Gen.g_passdirfd != -1)
8147c478bd9Sstevel@tonic-gate 				(void) close(Gen.g_passdirfd);
8157c478bd9Sstevel@tonic-gate 			Gen.g_passdirfd = -1;
816ced83f9bSceastha 			if (Atflag || SysAtflag) {
8177c478bd9Sstevel@tonic-gate 				if (Gen.g_dirfd != -1) {
8187c478bd9Sstevel@tonic-gate 					(void) close(Gen.g_dirfd);
8197c478bd9Sstevel@tonic-gate 				}
8207c478bd9Sstevel@tonic-gate 				Gen.g_dirfd = -1;
8217c478bd9Sstevel@tonic-gate 				if (passret != FILE_LINKED) {
8227c478bd9Sstevel@tonic-gate 					Gen.g_nam_p = Savenam_p;
8237c478bd9Sstevel@tonic-gate 					xattrs_out(file_pass);
8247c478bd9Sstevel@tonic-gate 				}
8257c478bd9Sstevel@tonic-gate 			}
8267c478bd9Sstevel@tonic-gate 		}
8277c478bd9Sstevel@tonic-gate 		break;
8287c478bd9Sstevel@tonic-gate 	default:
8297c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible action.");
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate 	if (Ofile > 0) {
8327c478bd9Sstevel@tonic-gate 		if (close(Ofile) != 0)
8337c478bd9Sstevel@tonic-gate 			msg(EXTN, "close error");
8347c478bd9Sstevel@tonic-gate 	}
8357c478bd9Sstevel@tonic-gate 	if (Archive > 0) {
8367c478bd9Sstevel@tonic-gate 		if (close(Archive) != 0)
8377c478bd9Sstevel@tonic-gate 			msg(EXTN, "close error");
8387c478bd9Sstevel@tonic-gate 	}
8397c478bd9Sstevel@tonic-gate 	Blocks = (u_longlong_t)(Blocks * Bufsize + SBlocks + 0x1FF) >> 9;
8407c478bd9Sstevel@tonic-gate 	msg(EPOST, "%lld blocks", Blocks);
8417c478bd9Sstevel@tonic-gate 	if (Error_cnt)
8427c478bd9Sstevel@tonic-gate 		msg(EPOST, "%d error(s)", Error_cnt);
8437c478bd9Sstevel@tonic-gate 	return (EXIT_CODE);
8447c478bd9Sstevel@tonic-gate }
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate /*
8477c478bd9Sstevel@tonic-gate  * add_lnk: Add a linked file's header to the linked file data structure, by
8487c478bd9Sstevel@tonic-gate  * either adding it to the end of an existing sub-list or starting
8497c478bd9Sstevel@tonic-gate  * a new sub-list.  Each sub-list saves the links to a given file.
8507c478bd9Sstevel@tonic-gate  *
8517c478bd9Sstevel@tonic-gate  * Directly returns a pointer to the new entry; returns a pointer to the head
8527c478bd9Sstevel@tonic-gate  * of the sub-list in which that entry is located through the argument.
8537c478bd9Sstevel@tonic-gate  */
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate static struct Lnk *
8567c478bd9Sstevel@tonic-gate add_lnk(struct Lnk **sublist_return)
8577c478bd9Sstevel@tonic-gate {
8587c478bd9Sstevel@tonic-gate 	struct Lnk *new_entry, *sublist;
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	for (sublist = Lnk_hd.L_nxt_p;
8617c478bd9Sstevel@tonic-gate 	    sublist != &Lnk_hd;
8627c478bd9Sstevel@tonic-gate 	    sublist = sublist->L_nxt_p) {
8637c478bd9Sstevel@tonic-gate 		if (sublist->L_gen.g_ino == G_p->g_ino &&
8647c478bd9Sstevel@tonic-gate 		    sublist->L_gen.g_dev == G_p->g_dev) {
8657c478bd9Sstevel@tonic-gate 			/* found */
8667c478bd9Sstevel@tonic-gate 			break;
8677c478bd9Sstevel@tonic-gate 		}
8687c478bd9Sstevel@tonic-gate 	}
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	new_entry = e_zalloc(E_EXIT, sizeof (struct Lnk));
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	new_entry->L_lnk_p = NULL;
8737c478bd9Sstevel@tonic-gate 	new_entry->L_gen = *G_p; /* structure copy */
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	new_entry->L_gen.g_nam_p = e_zalloc(E_EXIT, (size_t)G_p->g_namesz);
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 	(void) strcpy(new_entry->L_gen.g_nam_p, G_p->g_nam_p);
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	if (sublist == &Lnk_hd) {
8807c478bd9Sstevel@tonic-gate 		/* start new sub-list */
8817c478bd9Sstevel@tonic-gate 		new_entry->L_nxt_p = &Lnk_hd;
8827c478bd9Sstevel@tonic-gate 		new_entry->L_bck_p = Lnk_hd.L_bck_p;
8837c478bd9Sstevel@tonic-gate 		Lnk_hd.L_bck_p = new_entry->L_bck_p->L_nxt_p = new_entry;
8847c478bd9Sstevel@tonic-gate 		new_entry->L_lnk_p = NULL;
8857c478bd9Sstevel@tonic-gate 		new_entry->L_cnt = 1;
8867c478bd9Sstevel@tonic-gate 		new_entry->L_data = Onecopy ? 0 : 1;
8877c478bd9Sstevel@tonic-gate 		sublist = new_entry;
8887c478bd9Sstevel@tonic-gate 	} else {
8897c478bd9Sstevel@tonic-gate 		/* add to existing sub-list */
8907c478bd9Sstevel@tonic-gate 		struct Lnk *ptr;
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 		sublist->L_cnt++;
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 		for (ptr = sublist;
8957c478bd9Sstevel@tonic-gate 		    ptr->L_lnk_p != NULL;
8967c478bd9Sstevel@tonic-gate 		    ptr = ptr->L_lnk_p) {
8977c478bd9Sstevel@tonic-gate 			ptr->L_gen.g_filesz = G_p->g_filesz;
8987c478bd9Sstevel@tonic-gate 		}
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 		ptr->L_gen.g_filesz = G_p->g_filesz;
9017c478bd9Sstevel@tonic-gate 		ptr->L_lnk_p = new_entry;
9027c478bd9Sstevel@tonic-gate 	}
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	*sublist_return = sublist;
9057c478bd9Sstevel@tonic-gate 	return (new_entry);
9067c478bd9Sstevel@tonic-gate }
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate /*
9097c478bd9Sstevel@tonic-gate  * bfill: Read req_cnt bytes (out of filelen bytes) from the I/O buffer,
9107c478bd9Sstevel@tonic-gate  * moving them to rd_buf_p.  When there are no bytes left in the I/O buffer,
9117c478bd9Sstevel@tonic-gate  * Fillbuf is set and the I/O buffer is filled.  The variable dist is the
9127c478bd9Sstevel@tonic-gate  * distance to lseek if an I/O error is encountered with the -k option set
9137c478bd9Sstevel@tonic-gate  * (converted to a multiple of Bufsize).
9147c478bd9Sstevel@tonic-gate  */
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate static int
9177c478bd9Sstevel@tonic-gate bfill(void)
9187c478bd9Sstevel@tonic-gate {
9197c478bd9Sstevel@tonic-gate 	int i = 0, rv;
9207c478bd9Sstevel@tonic-gate 	static int eof = 0;
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	if (!Dflag) {
9237c478bd9Sstevel@tonic-gate 	while ((Buffr.b_end_p - Buffr.b_in_p) >= Bufsize) {
9247c478bd9Sstevel@tonic-gate 		errno = 0;
9257c478bd9Sstevel@tonic-gate 		if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0) {
9267c478bd9Sstevel@tonic-gate 			if (((Buffr.b_end_p - Buffr.b_in_p) >= Bufsize) &&
9277c478bd9Sstevel@tonic-gate 			    (Eomflag == 0)) {
9287c478bd9Sstevel@tonic-gate 				Eomflag = 1;
9297c478bd9Sstevel@tonic-gate 				return (1);
9307c478bd9Sstevel@tonic-gate 			}
9317c478bd9Sstevel@tonic-gate 			if (errno == ENOSPC) {
9327c478bd9Sstevel@tonic-gate 				(void) chgreel(INPUT);
9337c478bd9Sstevel@tonic-gate 				if (Hdr_type == BAR) {
9347c478bd9Sstevel@tonic-gate 					skip_bar_volhdr();
9357c478bd9Sstevel@tonic-gate 				}
9367c478bd9Sstevel@tonic-gate 				continue;
9377c478bd9Sstevel@tonic-gate 			} else if (Args & OCk) {
9387c478bd9Sstevel@tonic-gate 				if (i++ > MX_SEEKS)
9397c478bd9Sstevel@tonic-gate 					msg(EXT, "Cannot recover.");
9407c478bd9Sstevel@tonic-gate 				if (lseek(Archive, Bufsize, SEEK_REL) < 0)
9417c478bd9Sstevel@tonic-gate 					msg(EXTN, "Cannot lseek()");
9427c478bd9Sstevel@tonic-gate 				Error_cnt++;
9437c478bd9Sstevel@tonic-gate 				Buf_error++;
9447c478bd9Sstevel@tonic-gate 				rv = 0;
9457c478bd9Sstevel@tonic-gate 				continue;
9467c478bd9Sstevel@tonic-gate 			} else
9477c478bd9Sstevel@tonic-gate 				ioerror(INPUT);
9487c478bd9Sstevel@tonic-gate 		} /* (rv = g_read(Device, Archive ... */
9497c478bd9Sstevel@tonic-gate 		if (Hdr_type != BAR || rv == Bufsize) {
9507c478bd9Sstevel@tonic-gate 			Buffr.b_in_p += rv;
9517c478bd9Sstevel@tonic-gate 			Buffr.b_cnt += (long)rv;
9527c478bd9Sstevel@tonic-gate 		}
9537c478bd9Sstevel@tonic-gate 		if (rv == Bufsize) {
9547c478bd9Sstevel@tonic-gate 			eof = 0;
9557c478bd9Sstevel@tonic-gate 			Blocks++;
9567c478bd9Sstevel@tonic-gate 		} else if (rv == 0) {
9577c478bd9Sstevel@tonic-gate 			if (!eof) {
9587c478bd9Sstevel@tonic-gate 				eof = 1;
9597c478bd9Sstevel@tonic-gate 				break;
9607c478bd9Sstevel@tonic-gate 			}
9617c478bd9Sstevel@tonic-gate 			(void) chgreel(INPUT);
9627c478bd9Sstevel@tonic-gate 			eof = 0;	/* reset the eof after chgreel	*/
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 			/*
9657c478bd9Sstevel@tonic-gate 			 * if spans multiple volume, skip the volume header of
9667c478bd9Sstevel@tonic-gate 			 * the next volume so that the file currently being
9677c478bd9Sstevel@tonic-gate 			 * extracted can continue to be extracted.
9687c478bd9Sstevel@tonic-gate 			 */
9697c478bd9Sstevel@tonic-gate 			if (Hdr_type == BAR) {
9707c478bd9Sstevel@tonic-gate 				skip_bar_volhdr();
9717c478bd9Sstevel@tonic-gate 			}
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 			continue;
9747c478bd9Sstevel@tonic-gate 		} else {
9757c478bd9Sstevel@tonic-gate 			eof = 0;
9767c478bd9Sstevel@tonic-gate 			SBlocks += (u_longlong_t)rv;
9777c478bd9Sstevel@tonic-gate 		}
9787c478bd9Sstevel@tonic-gate 	} /* (Buffr.b_end_p - Buffr.b_in_p) <= Bufsize */
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	} else {			/* Dflag */
9817c478bd9Sstevel@tonic-gate 		errno = 0;
9827c478bd9Sstevel@tonic-gate 		if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0) {
9837c478bd9Sstevel@tonic-gate 			return (-1);
9847c478bd9Sstevel@tonic-gate 		} /* (rv = g_read(Device, Archive ... */
9857c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += rv;
9867c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += (long)rv;
9877c478bd9Sstevel@tonic-gate 		if (rv == Bufsize) {
9887c478bd9Sstevel@tonic-gate 			eof = 0;
9897c478bd9Sstevel@tonic-gate 			Blocks++;
9907c478bd9Sstevel@tonic-gate 		} else if (!rv) {
9917c478bd9Sstevel@tonic-gate 			if (!eof) {
9927c478bd9Sstevel@tonic-gate 				eof = 1;
9937c478bd9Sstevel@tonic-gate 				return (rv);
9947c478bd9Sstevel@tonic-gate 			}
9957c478bd9Sstevel@tonic-gate 			return (-1);
9967c478bd9Sstevel@tonic-gate 		} else {
9977c478bd9Sstevel@tonic-gate 			eof = 0;
9987c478bd9Sstevel@tonic-gate 			SBlocks += (u_longlong_t)rv;
9997c478bd9Sstevel@tonic-gate 		}
10007c478bd9Sstevel@tonic-gate 	}
10017c478bd9Sstevel@tonic-gate 	return (rv);
10027c478bd9Sstevel@tonic-gate }
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate /*
10057c478bd9Sstevel@tonic-gate  * bflush: Move wr_cnt bytes from data_p into the I/O buffer.  When the
10067c478bd9Sstevel@tonic-gate  * I/O buffer is full, Flushbuf is set and the buffer is written out.
10077c478bd9Sstevel@tonic-gate  */
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate static void
10107c478bd9Sstevel@tonic-gate bflush(void)
10117c478bd9Sstevel@tonic-gate {
10127c478bd9Sstevel@tonic-gate 	int rv;
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	while (Buffr.b_cnt >= Bufsize) {
10157c478bd9Sstevel@tonic-gate 		errno = 0;
10167c478bd9Sstevel@tonic-gate 		if ((rv = g_write(Device, Archive, Buffr.b_out_p,
10177c478bd9Sstevel@tonic-gate 		    Bufsize)) < 0) {
10187c478bd9Sstevel@tonic-gate 			if (errno == ENOSPC && !Dflag)
10197c478bd9Sstevel@tonic-gate 				rv = chgreel(OUTPUT);
10207c478bd9Sstevel@tonic-gate 			else
10217c478bd9Sstevel@tonic-gate 				ioerror(OUTPUT);
10227c478bd9Sstevel@tonic-gate 		}
10237c478bd9Sstevel@tonic-gate 		Buffr.b_out_p += rv;
10247c478bd9Sstevel@tonic-gate 		Buffr.b_cnt -= (long)rv;
10257c478bd9Sstevel@tonic-gate 		if (rv == Bufsize)
10267c478bd9Sstevel@tonic-gate 			Blocks++;
10277c478bd9Sstevel@tonic-gate 		else if (rv > 0)
10287c478bd9Sstevel@tonic-gate 			SBlocks += (u_longlong_t)rv;
10297c478bd9Sstevel@tonic-gate 	}
10307c478bd9Sstevel@tonic-gate 	rstbuf();
10317c478bd9Sstevel@tonic-gate }
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate /*
10347c478bd9Sstevel@tonic-gate  * chgreel: Determine if end-of-medium has been reached.  If it has,
10357c478bd9Sstevel@tonic-gate  * close the current medium and prompt the user for the next medium.
10367c478bd9Sstevel@tonic-gate  */
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate static int
10397c478bd9Sstevel@tonic-gate chgreel(int dir)
10407c478bd9Sstevel@tonic-gate {
10417c478bd9Sstevel@tonic-gate 	int lastchar, tryagain, askagain, rv;
10427c478bd9Sstevel@tonic-gate 	int tmpdev;
10437c478bd9Sstevel@tonic-gate 	char str[APATH];
10447c478bd9Sstevel@tonic-gate 	struct stat statb;
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	rv = 0;
10477c478bd9Sstevel@tonic-gate 	if (fstat(Archive, &statb) < 0)
10487c478bd9Sstevel@tonic-gate 		msg(EXTN, "Error during stat() of archive");
10497c478bd9Sstevel@tonic-gate 	if ((statb.st_mode & S_IFMT) != S_IFCHR) {
10507c478bd9Sstevel@tonic-gate 		if (dir == INPUT) {
10517c478bd9Sstevel@tonic-gate 			msg(EXT, "%s%s\n",
10527c478bd9Sstevel@tonic-gate 			    "Can't read input:  end of file encountered ",
10537c478bd9Sstevel@tonic-gate 			    "prior to expected end of archive.");
10547c478bd9Sstevel@tonic-gate 		}
10557c478bd9Sstevel@tonic-gate 	}
10567c478bd9Sstevel@tonic-gate 	msg(EPOST, "\007End of medium on \"%s\".", dir ? "output" : "input");
10577c478bd9Sstevel@tonic-gate 	if (is_floppy(Archive))
10587c478bd9Sstevel@tonic-gate 		(void) ioctl(Archive, FDEJECT, NULL);
10597c478bd9Sstevel@tonic-gate 	if ((close(Archive) != 0) && (dir == OUTPUT))
10607c478bd9Sstevel@tonic-gate 		msg(EXTN, "close error");
10617c478bd9Sstevel@tonic-gate 	Archive = 0;
10627c478bd9Sstevel@tonic-gate 	Volcnt++;
10637c478bd9Sstevel@tonic-gate 	for (;;) {
1064ced83f9bSceastha 		if (Rtty_p == NULL)
10657c478bd9Sstevel@tonic-gate 			Rtty_p = fopen(Ttyname, "r");
10667c478bd9Sstevel@tonic-gate 		do { /* tryagain */
10677c478bd9Sstevel@tonic-gate 			if (IOfil_p) {
10687c478bd9Sstevel@tonic-gate 				do {
10697c478bd9Sstevel@tonic-gate 					msg(EPOST, gettext(Eom_p), Volcnt);
10707c478bd9Sstevel@tonic-gate 					if (!Rtty_p || fgets(str, sizeof (str),
1071ced83f9bSceastha 					    Rtty_p) == NULL)
10727c478bd9Sstevel@tonic-gate 						msg(EXT, "Cannot read tty.");
10737c478bd9Sstevel@tonic-gate 					askagain = 0;
10747c478bd9Sstevel@tonic-gate 					switch (*str) {
10757c478bd9Sstevel@tonic-gate 					case '\n':
10767c478bd9Sstevel@tonic-gate 						(void) strcpy(str, IOfil_p);
10777c478bd9Sstevel@tonic-gate 						break;
10787c478bd9Sstevel@tonic-gate 					case 'q':
10797c478bd9Sstevel@tonic-gate 						exit(EXIT_CODE);
10807c478bd9Sstevel@tonic-gate 					default:
10817c478bd9Sstevel@tonic-gate 						askagain = 1;
10827c478bd9Sstevel@tonic-gate 					}
10837c478bd9Sstevel@tonic-gate 				} while (askagain);
10847c478bd9Sstevel@tonic-gate 			} else {
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 				if (Hdr_type == BAR)
10877c478bd9Sstevel@tonic-gate 					Bar_vol_num++;
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 				msg(EPOST,
10907c478bd9Sstevel@tonic-gate 				    "To continue, type device/file name when "
10917c478bd9Sstevel@tonic-gate 				    "ready.");
10927c478bd9Sstevel@tonic-gate 				if (!Rtty_p || fgets(str, sizeof (str),
1093ced83f9bSceastha 				    Rtty_p) == NULL)
10947c478bd9Sstevel@tonic-gate 					msg(EXT, "Cannot read tty.");
10957c478bd9Sstevel@tonic-gate 				lastchar = strlen(str) - 1;
10967c478bd9Sstevel@tonic-gate 				if (*(str + lastchar) == '\n') /* remove '\n' */
10977c478bd9Sstevel@tonic-gate 					*(str + lastchar) = '\0';
10987c478bd9Sstevel@tonic-gate 				if (!*str)
10997c478bd9Sstevel@tonic-gate 					exit(EXIT_CODE);
11007c478bd9Sstevel@tonic-gate 			}
11017c478bd9Sstevel@tonic-gate 			tryagain = 0;
11027c478bd9Sstevel@tonic-gate 			if ((Archive = open(str, dir)) < 0) {
11037c478bd9Sstevel@tonic-gate 				msg(ERRN, "Cannot open \"%s\"", str);
11047c478bd9Sstevel@tonic-gate 				tryagain = 1;
11057c478bd9Sstevel@tonic-gate 			}
11067c478bd9Sstevel@tonic-gate 		} while (tryagain);
11077c478bd9Sstevel@tonic-gate 		(void) g_init(&tmpdev, &Archive);
11087c478bd9Sstevel@tonic-gate 		if (tmpdev != Device)
11097c478bd9Sstevel@tonic-gate 			msg(EXT, "Cannot change media types in mid-stream.");
11107c478bd9Sstevel@tonic-gate 		if (dir == INPUT)
11117c478bd9Sstevel@tonic-gate 			break;
11127c478bd9Sstevel@tonic-gate 		else { /* dir == OUTPUT */
11137c478bd9Sstevel@tonic-gate 			errno = 0;
11147c478bd9Sstevel@tonic-gate 			if ((rv = g_write(Device, Archive, Buffr.b_out_p,
11157c478bd9Sstevel@tonic-gate 			    Bufsize)) == Bufsize)
11167c478bd9Sstevel@tonic-gate 				break;
11177c478bd9Sstevel@tonic-gate 			else
11187c478bd9Sstevel@tonic-gate 				msg(ERR,
11197c478bd9Sstevel@tonic-gate 				    "Unable to write this medium, try "
11207c478bd9Sstevel@tonic-gate 				    "another.");
11217c478bd9Sstevel@tonic-gate 		}
11227c478bd9Sstevel@tonic-gate 	} /* ;; */
11237c478bd9Sstevel@tonic-gate 	Eomflag = 0;
11247c478bd9Sstevel@tonic-gate 	return (rv);
11257c478bd9Sstevel@tonic-gate }
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate /*
11287c478bd9Sstevel@tonic-gate  * ckname: Check filenames against user specified patterns,
11297c478bd9Sstevel@tonic-gate  * and/or ask the user for new name when -r is used.
11307c478bd9Sstevel@tonic-gate  */
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate static int
11337c478bd9Sstevel@tonic-gate ckname(int flag)
11347c478bd9Sstevel@tonic-gate {
1135ced83f9bSceastha 	int	lastchar;
1136ced83f9bSceastha 	size_t	rename_bufsz = Max_namesz + 1;
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	if (Hdr_type != TAR && Hdr_type != USTAR && Hdr_type != BAR) {
11397c478bd9Sstevel@tonic-gate 		/* Re-visit tar size issues later */
11407c478bd9Sstevel@tonic-gate 		if (G_p->g_namesz - 1 > Max_namesz) {
11417c478bd9Sstevel@tonic-gate 			msg(ERR, "Name exceeds maximum length - skipped.");
11427c478bd9Sstevel@tonic-gate 			return (F_SKIP);
11437c478bd9Sstevel@tonic-gate 		}
11447c478bd9Sstevel@tonic-gate 	}
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 	if (Pat_pp && !matched())
11477c478bd9Sstevel@tonic-gate 		return (F_SKIP);
1148ced83f9bSceastha 
1149ced83f9bSceastha 	/* rename interactively */
1150ced83f9bSceastha 	if ((Args & OCr) && !Adir && !G_p->g_rw_sysattr) {
11517c478bd9Sstevel@tonic-gate 		(void) fprintf(Wtty_p, gettext("Rename \"%s%s%s\"? "),
1152ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? G_p->g_nam_p : Renam_p,
1153ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? "" : gettext(" Attribute "),
1154ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? "" : G_p->g_attrnam_p);
11557c478bd9Sstevel@tonic-gate 		(void) fflush(Wtty_p);
1156ced83f9bSceastha 		if (fgets(Renametmp_p, rename_bufsz, Rtty_p) == NULL)
11577c478bd9Sstevel@tonic-gate 			msg(EXT, "Cannot read tty.");
11587c478bd9Sstevel@tonic-gate 		if (feof(Rtty_p))
11597c478bd9Sstevel@tonic-gate 			exit(EXIT_CODE);
11607c478bd9Sstevel@tonic-gate 		lastchar = strlen(Renametmp_p) - 1;
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 		/* remove trailing '\n' */
11637c478bd9Sstevel@tonic-gate 		if (*(Renametmp_p + lastchar) == '\n')
11647c478bd9Sstevel@tonic-gate 			*(Renametmp_p + lastchar) = '\0';
11657c478bd9Sstevel@tonic-gate 		if (*Renametmp_p == '\0') {
11667c478bd9Sstevel@tonic-gate 			msg(POST, "%s%s%s Skipped.",
1167ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? G_p->g_nam_p :
1168ced83f9bSceastha 			    G_p->g_attrfnam_p,
1169ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
1170ced83f9bSceastha 			    gettext(" Attribute "),
1171ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" : G_p->g_attrnam_p);
1172ced83f9bSceastha 			if (G_p->g_attrparent_p == NULL) {
11737c478bd9Sstevel@tonic-gate 				*G_p->g_nam_p = '\0';
1174ced83f9bSceastha 			}
1175ced83f9bSceastha 			if (Renam_attr_p) {
1176ced83f9bSceastha 				*Renam_attr_p = '\0';
1177ced83f9bSceastha 			}
11787c478bd9Sstevel@tonic-gate 			return (F_SKIP);
11797c478bd9Sstevel@tonic-gate 		} else if (strcmp(Renametmp_p, ".") != 0) {
1180ced83f9bSceastha 			if (G_p->g_attrnam_p == NULL) {
1181ced83f9bSceastha 				if (strlen(Renametmp_p) > strlen(
1182ced83f9bSceastha 				    G_p->g_nam_p)) {
11837c478bd9Sstevel@tonic-gate 					if ((G_p->g_nam_p != &nambuf[0]) &&
1184ced83f9bSceastha 					    (G_p->g_nam_p != &fullnam[0])) {
11857c478bd9Sstevel@tonic-gate 						free(G_p->g_nam_p);
1186ced83f9bSceastha 						G_p->g_nam_p = e_zalloc(E_EXIT,
1187ced83f9bSceastha 						    rename_bufsz);
11887c478bd9Sstevel@tonic-gate 					}
1189ced83f9bSceastha 				}
1190ced83f9bSceastha 				if (Renam_attr_p) {
1191ced83f9bSceastha 					*Renam_attr_p = '\0';
1192ced83f9bSceastha 				}
1193ced83f9bSceastha 				if ((strlcpy(Renam_p, Renametmp_p,
1194ced83f9bSceastha 				    rename_bufsz) > rename_bufsz) ||
1195ced83f9bSceastha 				    (strlcpy(G_p->g_nam_p, Renametmp_p,
1196ced83f9bSceastha 				    rename_bufsz) > rename_bufsz)) {
1197ced83f9bSceastha 					msg(EXTN, "buffer overflow");
1198ced83f9bSceastha 				}
1199ced83f9bSceastha 			} else {
1200ced83f9bSceastha 				if (G_p->g_attrnam_p != NULL) {
12017c478bd9Sstevel@tonic-gate 					free(G_p->g_attrnam_p);
12027c478bd9Sstevel@tonic-gate 					G_p->g_attrnam_p = e_strdup(E_EXIT,
12037c478bd9Sstevel@tonic-gate 					    Renametmp_p);
12047c478bd9Sstevel@tonic-gate 					(void) strcpy(G_p->g_nam_p, Renam_p);
1205ced83f9bSceastha 					if (Renam_attr_p) {
1206ced83f9bSceastha 						if (strlcpy(Renam_attr_p,
1207ced83f9bSceastha 						    Renametmp_p, rename_bufsz) >
1208ced83f9bSceastha 						    rename_bufsz) {
1209ced83f9bSceastha 							msg(EXTN,
1210ced83f9bSceastha 							    "buffer overflow");
12117c478bd9Sstevel@tonic-gate 						}
1212ced83f9bSceastha 					}
1213ced83f9bSceastha 				}
1214ced83f9bSceastha 			}
1215ced83f9bSceastha 		} else {
1216ced83f9bSceastha 			if (G_p->g_attrnam_p == NULL) {
1217ced83f9bSceastha 				*Renam_p = '\0';
1218ced83f9bSceastha 			}
1219ced83f9bSceastha 			if (Renam_attr_p) {
1220ced83f9bSceastha 				*Renam_attr_p = '\0';
1221ced83f9bSceastha 			}
12227c478bd9Sstevel@tonic-gate 		}
12237c478bd9Sstevel@tonic-gate 	}
12247c478bd9Sstevel@tonic-gate 	if (flag != 0 || Onecopy == 0) {
12257c478bd9Sstevel@tonic-gate 		VERBOSE((Args & OCt), G_p->g_nam_p);
12267c478bd9Sstevel@tonic-gate 	}
12277c478bd9Sstevel@tonic-gate 	if (Args & OCt)
12287c478bd9Sstevel@tonic-gate 		return (F_SKIP);
12297c478bd9Sstevel@tonic-gate 	return (F_EXTR);
12307c478bd9Sstevel@tonic-gate }
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate /*
12337c478bd9Sstevel@tonic-gate  * ckopts: Check the validity of all command line options.
12347c478bd9Sstevel@tonic-gate  */
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate static void
12377c478bd9Sstevel@tonic-gate ckopts(long mask)
12387c478bd9Sstevel@tonic-gate {
12397c478bd9Sstevel@tonic-gate 	int oflag;
12407c478bd9Sstevel@tonic-gate 	char *t_p;
12417c478bd9Sstevel@tonic-gate 	long errmsk;
1242647ab8f4Sceastha 	uid_t	Euid = geteuid();	/* Effective uid of invoker */
1243647ab8f4Sceastha #ifdef SOLARIS_PRIVS
1244647ab8f4Sceastha 	priv_set_t *privset;
12453a3e214eSceastha 	priv_set_t *zones_privset;
1246647ab8f4Sceastha #endif	/* SOLARIS_PRIVS */
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 	if (mask & OCi) {
12497c478bd9Sstevel@tonic-gate 		errmsk = mask & INV_MSK4i;
12507c478bd9Sstevel@tonic-gate 	} else if (mask & OCo) {
12517c478bd9Sstevel@tonic-gate 		errmsk = mask & INV_MSK4o;
12527c478bd9Sstevel@tonic-gate 	} else if (mask & OCp) {
12537c478bd9Sstevel@tonic-gate 		errmsk = mask & INV_MSK4p;
12547c478bd9Sstevel@tonic-gate 	} else {
12557c478bd9Sstevel@tonic-gate 		msg(ERR, "One of -i, -o or -p must be specified.");
12567c478bd9Sstevel@tonic-gate 		errmsk = 0;
12577c478bd9Sstevel@tonic-gate 	}
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 	if (errmsk) {
12607c478bd9Sstevel@tonic-gate 		/* if non-zero, invalid options were specified */
12617c478bd9Sstevel@tonic-gate 		Error_cnt++;
12627c478bd9Sstevel@tonic-gate 	}
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 	if ((mask & OCa) && (mask & OCm) && ((mask & OCi) ||
12657c478bd9Sstevel@tonic-gate 	    (mask & OCo))) {
12667c478bd9Sstevel@tonic-gate 		msg(ERR, "-a and -m are mutually exclusive.");
12677c478bd9Sstevel@tonic-gate 	}
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 	if ((mask & OCc) && (mask & OCH) && (strcmp("odc", Hdr_p))) {
12707c478bd9Sstevel@tonic-gate 		msg(ERR, "-c and -H are mutually exclusive.");
12717c478bd9Sstevel@tonic-gate 	}
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate 	if ((mask & OCv) && (mask & OCV)) {
12747c478bd9Sstevel@tonic-gate 		msg(ERR, "-v and -V are mutually exclusive.");
12757c478bd9Sstevel@tonic-gate 	}
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 	if ((mask & OCt) && (mask & OCV)) {
12787c478bd9Sstevel@tonic-gate 		msg(ERR, "-t and -V are mutually exclusive.");
12797c478bd9Sstevel@tonic-gate 	}
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 	if ((mask & OCB) && (mask & OCC)) {
12827c478bd9Sstevel@tonic-gate 		msg(ERR, "-B and -C are mutually exclusive.");
12837c478bd9Sstevel@tonic-gate 	}
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 	if ((mask & OCH) && (mask & OC6)) {
12867c478bd9Sstevel@tonic-gate 		msg(ERR, "-H and -6 are mutually exclusive.");
12877c478bd9Sstevel@tonic-gate 	}
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 	if ((mask & OCM) && !((mask & OCI) || (mask & OCO))) {
12907c478bd9Sstevel@tonic-gate 		msg(ERR, "-M not meaningful without -O or -I.");
12917c478bd9Sstevel@tonic-gate 	}
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	if ((mask & OCA) && !(mask & OCO)) {
12947c478bd9Sstevel@tonic-gate 		msg(ERR, "-A requires the -O option.");
12957c478bd9Sstevel@tonic-gate 	}
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 	if (Bufsize <= 0) {
12987c478bd9Sstevel@tonic-gate 		msg(ERR, "Illegal size given for -C option.");
12997c478bd9Sstevel@tonic-gate 	}
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 	if (mask & OCH) {
13027c478bd9Sstevel@tonic-gate 		t_p = Hdr_p;
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 		while (*t_p != NULL) {
13057c478bd9Sstevel@tonic-gate 			if (isupper(*t_p)) {
13067c478bd9Sstevel@tonic-gate 				*t_p = 'a' + (*t_p - 'A');
13077c478bd9Sstevel@tonic-gate 			}
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate 			t_p++;
13107c478bd9Sstevel@tonic-gate 		}
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 		if (!(strcmp("odc", Hdr_p))) {
13137c478bd9Sstevel@tonic-gate 			Hdr_type = CHR;
13147c478bd9Sstevel@tonic-gate 			Max_namesz = CPATH;
13157c478bd9Sstevel@tonic-gate 			Onecopy = 0;
13167c478bd9Sstevel@tonic-gate 			Use_old_stat = 1;
13177c478bd9Sstevel@tonic-gate 		} else if (!(strcmp("crc", Hdr_p))) {
13187c478bd9Sstevel@tonic-gate 			Hdr_type = CRC;
13197c478bd9Sstevel@tonic-gate 			Max_namesz = APATH;
13207c478bd9Sstevel@tonic-gate 			Onecopy = 1;
13217c478bd9Sstevel@tonic-gate 		} else if (!(strcmp("tar", Hdr_p))) {
13227c478bd9Sstevel@tonic-gate 			if (Args & OCo) {
13237c478bd9Sstevel@tonic-gate 				Hdr_type = USTAR;
13247c478bd9Sstevel@tonic-gate 				Max_namesz = HNAMLEN - 1;
13257c478bd9Sstevel@tonic-gate 			} else {
13267c478bd9Sstevel@tonic-gate 				Hdr_type = TAR;
13277c478bd9Sstevel@tonic-gate 				Max_namesz = TNAMLEN - 1;
13287c478bd9Sstevel@tonic-gate 			}
13297c478bd9Sstevel@tonic-gate 			Onecopy = 0;
13307c478bd9Sstevel@tonic-gate 		} else if (!(strcmp("ustar", Hdr_p))) {
13317c478bd9Sstevel@tonic-gate 			Hdr_type = USTAR;
13327c478bd9Sstevel@tonic-gate 			Max_namesz = HNAMLEN - 1;
13337c478bd9Sstevel@tonic-gate 			Onecopy = 0;
13347c478bd9Sstevel@tonic-gate 		} else if (!(strcmp("bar", Hdr_p))) {
13357c478bd9Sstevel@tonic-gate 			if ((Args & OCo) || (Args & OCp)) {
13367c478bd9Sstevel@tonic-gate 				msg(ERR,
13377c478bd9Sstevel@tonic-gate 				    "Header type bar can only be used with -i");
13387c478bd9Sstevel@tonic-gate 			}
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 			if (Args & OCP) {
13417c478bd9Sstevel@tonic-gate 				msg(ERR,
13427c478bd9Sstevel@tonic-gate 				    "Can't preserve using bar header");
13437c478bd9Sstevel@tonic-gate 			}
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 			Hdr_type = BAR;
13467c478bd9Sstevel@tonic-gate 			Max_namesz = TNAMLEN - 1;
13477c478bd9Sstevel@tonic-gate 			Onecopy = 0;
13487c478bd9Sstevel@tonic-gate 		} else {
13497c478bd9Sstevel@tonic-gate 			msg(ERR, "Invalid header \"%s\" specified", Hdr_p);
13507c478bd9Sstevel@tonic-gate 		}
13517c478bd9Sstevel@tonic-gate 	}
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 	if (mask & OCr) {
13547c478bd9Sstevel@tonic-gate 		Rtty_p = fopen(Ttyname, "r");
13557c478bd9Sstevel@tonic-gate 		Wtty_p = fopen(Ttyname, "w");
13567c478bd9Sstevel@tonic-gate 
1357ced83f9bSceastha 		if (Rtty_p == NULL || Wtty_p == NULL) {
13587c478bd9Sstevel@tonic-gate 			msg(ERR, "Cannot rename, \"%s\" missing", Ttyname);
13597c478bd9Sstevel@tonic-gate 		}
13607c478bd9Sstevel@tonic-gate 	}
13617c478bd9Sstevel@tonic-gate 
1362ced83f9bSceastha 	if ((mask & OCE) && (Ef_p = fopen(Efil_p, "r")) == NULL) {
13637c478bd9Sstevel@tonic-gate 		msg(ERR, "Cannot open \"%s\" to read patterns", Efil_p);
13647c478bd9Sstevel@tonic-gate 	}
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate 	if ((mask & OCI) && (Archive = open(IOfil_p, O_RDONLY)) < 0) {
13677c478bd9Sstevel@tonic-gate 		msg(ERR, "Cannot open \"%s\" for input", IOfil_p);
13687c478bd9Sstevel@tonic-gate 	}
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate 	if (mask & OCO) {
13717c478bd9Sstevel@tonic-gate 		if (mask & OCA) {
13727c478bd9Sstevel@tonic-gate 			if ((Archive = open(IOfil_p, O_RDWR)) < 0) {
13737c478bd9Sstevel@tonic-gate 				msg(ERR,
13747c478bd9Sstevel@tonic-gate 				    "Cannot open \"%s\" for append",
13757c478bd9Sstevel@tonic-gate 				    IOfil_p);
13767c478bd9Sstevel@tonic-gate 			}
13777c478bd9Sstevel@tonic-gate 		} else {
13787c478bd9Sstevel@tonic-gate 			oflag = (O_WRONLY | O_CREAT | O_TRUNC);
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 			if ((Archive = open(IOfil_p, oflag, 0777)) < 0) {
13817c478bd9Sstevel@tonic-gate 				msg(ERR,
13827c478bd9Sstevel@tonic-gate 				    "Cannot open \"%s\" for output",
13837c478bd9Sstevel@tonic-gate 				    IOfil_p);
13847c478bd9Sstevel@tonic-gate 			}
13857c478bd9Sstevel@tonic-gate 		}
13867c478bd9Sstevel@tonic-gate 	}
13877c478bd9Sstevel@tonic-gate 
1388647ab8f4Sceastha #ifdef SOLARIS_PRIVS
1389647ab8f4Sceastha 	if ((privset = priv_allocset()) == NULL) {
1390647ab8f4Sceastha 		msg(ERR, "Unable to allocate privilege set");
1391647ab8f4Sceastha 	} else if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
1392647ab8f4Sceastha 		msg(ERR, "Unable to obtain privilege set");
1393647ab8f4Sceastha 	} else {
13943a3e214eSceastha 		zones_privset = priv_str_to_set("zone", "", NULL);
13953a3e214eSceastha 		if (zones_privset != NULL) {
13963a3e214eSceastha 			privileged = (priv_issubset(zones_privset,
13973a3e214eSceastha 			    privset) == B_TRUE);
13983a3e214eSceastha 			priv_freeset(zones_privset);
13993a3e214eSceastha 		} else {
14003a3e214eSceastha 			msg(ERR, "Unable to map privilege to privilege set");
14013a3e214eSceastha 		}
1402647ab8f4Sceastha 	}
1403647ab8f4Sceastha 	if (privset != NULL) {
1404647ab8f4Sceastha 		priv_freeset(privset);
1405647ab8f4Sceastha 	}
1406647ab8f4Sceastha #else
1407647ab8f4Sceastha 	privileged = (Euid == 0);
1408647ab8f4Sceastha #endif	/* SOLARIS_PRIVS */
1409647ab8f4Sceastha 
14107c478bd9Sstevel@tonic-gate 	if (mask & OCR) {
1411ced83f9bSceastha 		if ((Rpw_p = getpwnam(Own_p)) == NULL) {
14127c478bd9Sstevel@tonic-gate 			msg(ERR, "\"%s\" is not a valid user id", Own_p);
1413647ab8f4Sceastha 		} else if ((Euid != Rpw_p->pw_uid) && !privileged) {
1414647ab8f4Sceastha 			msg(ERR, "R option only valid for super-user or "
1415647ab8f4Sceastha 			    "id matches login id of user executing cpio");
14167c478bd9Sstevel@tonic-gate 		}
14177c478bd9Sstevel@tonic-gate 	}
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	if ((mask & OCo) && !(mask & OCO)) {
14207c478bd9Sstevel@tonic-gate 		Out_p = stderr;
14217c478bd9Sstevel@tonic-gate 	}
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	if ((mask & OCp) && ((mask & (OCB|OCC)) == 0)) {
14247c478bd9Sstevel@tonic-gate 		/*
14257c478bd9Sstevel@tonic-gate 		 * We are in pass mode with no block size specified.  Use the
14267c478bd9Sstevel@tonic-gate 		 * larger of the native page size and 8192.
14277c478bd9Sstevel@tonic-gate 		 */
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate 		Bufsize = (PageSize > 8192) ? PageSize : 8192;
14307c478bd9Sstevel@tonic-gate 	}
14317c478bd9Sstevel@tonic-gate }
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate /*
14347c478bd9Sstevel@tonic-gate  * cksum: Calculate the simple checksum of a file (CRC) or header
14357c478bd9Sstevel@tonic-gate  * (TARTYP (TAR and USTAR)).  For -o and the CRC header, the file is opened and
14367c478bd9Sstevel@tonic-gate  * the checksum is calculated.  For -i and the CRC header, the checksum
14377c478bd9Sstevel@tonic-gate  * is calculated as each block is transferred from the archive I/O buffer
14387c478bd9Sstevel@tonic-gate  * to the file system I/O buffer.  The TARTYP (TAR and USTAR) headers calculate
14397c478bd9Sstevel@tonic-gate  * the simple checksum of the header (with the checksum field of the
14407c478bd9Sstevel@tonic-gate  * header initialized to all spaces (\040).
14417c478bd9Sstevel@tonic-gate  */
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate static long
14447c478bd9Sstevel@tonic-gate cksum(char hdr, int byt_cnt, int *err)
14457c478bd9Sstevel@tonic-gate {
14467c478bd9Sstevel@tonic-gate 	char *crc_p, *end_p;
14477c478bd9Sstevel@tonic-gate 	int cnt;
14487c478bd9Sstevel@tonic-gate 	long checksum = 0L, have;
14497c478bd9Sstevel@tonic-gate 	off_t lcnt;
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 	if (err != NULL)
14527c478bd9Sstevel@tonic-gate 		*err = 0;
14537c478bd9Sstevel@tonic-gate 	switch (hdr) {
14547c478bd9Sstevel@tonic-gate 	case CRC:
14557c478bd9Sstevel@tonic-gate 		if (Args & OCi) { /* do running checksum */
14567c478bd9Sstevel@tonic-gate 			end_p = Buffr.b_out_p + byt_cnt;
14577c478bd9Sstevel@tonic-gate 			for (crc_p = Buffr.b_out_p; crc_p < end_p; crc_p++)
14587c478bd9Sstevel@tonic-gate 				checksum += (long)*crc_p;
14597c478bd9Sstevel@tonic-gate 			break;
14607c478bd9Sstevel@tonic-gate 		}
14617c478bd9Sstevel@tonic-gate 		/* OCo - do checksum of file */
14627c478bd9Sstevel@tonic-gate 		lcnt = G_p->g_filesz;
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate 		while (lcnt > 0) {
14657c478bd9Sstevel@tonic-gate 			have = (lcnt < Bufsize) ? lcnt : Bufsize;
14667c478bd9Sstevel@tonic-gate 			errno = 0;
14677c478bd9Sstevel@tonic-gate 			if (read(Ifile, Buf_p, have) != have) {
14687c478bd9Sstevel@tonic-gate 				msg(ERR, "Error computing checksum.");
14697c478bd9Sstevel@tonic-gate 				if (err != NULL)
14707c478bd9Sstevel@tonic-gate 					*err = 1;
14717c478bd9Sstevel@tonic-gate 				break;
14727c478bd9Sstevel@tonic-gate 			}
14737c478bd9Sstevel@tonic-gate 			end_p = Buf_p + have;
14747c478bd9Sstevel@tonic-gate 			for (crc_p = Buf_p; crc_p < end_p; crc_p++)
14757c478bd9Sstevel@tonic-gate 				checksum += (long)*crc_p;
14767c478bd9Sstevel@tonic-gate 			lcnt -= have;
14777c478bd9Sstevel@tonic-gate 		}
14787c478bd9Sstevel@tonic-gate 		if (lseek(Ifile, (off_t)0, SEEK_ABS) < 0)
14797c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot reset file after checksum");
14807c478bd9Sstevel@tonic-gate 		break;
14817c478bd9Sstevel@tonic-gate 	case TARTYP: /* TAR and USTAR */
14827c478bd9Sstevel@tonic-gate 		crc_p = Thdr_p->tbuf.t_cksum;
14837c478bd9Sstevel@tonic-gate 		for (cnt = 0; cnt < TCRCLEN; cnt++) {
14847c478bd9Sstevel@tonic-gate 			*crc_p = '\040';
14857c478bd9Sstevel@tonic-gate 			crc_p++;
14867c478bd9Sstevel@tonic-gate 		}
14877c478bd9Sstevel@tonic-gate 		crc_p = (char *)Thdr_p;
14887c478bd9Sstevel@tonic-gate 		for (cnt = 0; cnt < TARSZ; cnt++) {
14897c478bd9Sstevel@tonic-gate 			/*
14907c478bd9Sstevel@tonic-gate 			 * tar uses unsigned checksum, so we must use unsigned
14917c478bd9Sstevel@tonic-gate 			 * here in order to be able to read tar archives.
14927c478bd9Sstevel@tonic-gate 			 */
14937c478bd9Sstevel@tonic-gate 			checksum += (long)((unsigned char)(*crc_p));
14947c478bd9Sstevel@tonic-gate 			crc_p++;
14957c478bd9Sstevel@tonic-gate 		}
14967c478bd9Sstevel@tonic-gate 		break;
14977c478bd9Sstevel@tonic-gate 	default:
14987c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
14997c478bd9Sstevel@tonic-gate 	} /* hdr */
15007c478bd9Sstevel@tonic-gate 	return (checksum);
15017c478bd9Sstevel@tonic-gate }
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate /*
15047c478bd9Sstevel@tonic-gate  * creat_hdr: Fill in the generic header structure with the specific
15057c478bd9Sstevel@tonic-gate  *            header information based on the value of Hdr_type.
15067c478bd9Sstevel@tonic-gate  *
15077c478bd9Sstevel@tonic-gate  *            return (1) if this process was successful, and (0) otherwise.
15087c478bd9Sstevel@tonic-gate  */
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate static int
15117c478bd9Sstevel@tonic-gate creat_hdr(void)
15127c478bd9Sstevel@tonic-gate {
15137c478bd9Sstevel@tonic-gate 	ushort_t ftype;
15147c478bd9Sstevel@tonic-gate 	int fullnamesize;
15157c478bd9Sstevel@tonic-gate 	dev_t dev;
15167c478bd9Sstevel@tonic-gate 	ino_t ino;
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 	ftype = SrcSt.st_mode & Ftype;
15197c478bd9Sstevel@tonic-gate 	Adir = (ftype == S_IFDIR);
1520cdd68f5aSceastha 	Aspec = (ftype == S_IFBLK || ftype == S_IFCHR || ftype == S_IFIFO ||
1521cdd68f5aSceastha 	    ftype == S_IFSOCK);
15227c478bd9Sstevel@tonic-gate 	switch (Hdr_type) {
15237c478bd9Sstevel@tonic-gate 		case BIN:
15247c478bd9Sstevel@tonic-gate 			Gen.g_magic = CMN_BIN;
15257c478bd9Sstevel@tonic-gate 			break;
15267c478bd9Sstevel@tonic-gate 		case CHR:
15277c478bd9Sstevel@tonic-gate 			Gen.g_magic = CMN_BIN;
15287c478bd9Sstevel@tonic-gate 			break;
15297c478bd9Sstevel@tonic-gate 		case ASC:
15307c478bd9Sstevel@tonic-gate 			Gen.g_magic = CMN_ASC;
15317c478bd9Sstevel@tonic-gate 			break;
15327c478bd9Sstevel@tonic-gate 		case CRC:
15337c478bd9Sstevel@tonic-gate 			Gen.g_magic = CMN_CRC;
15347c478bd9Sstevel@tonic-gate 			break;
15357c478bd9Sstevel@tonic-gate 		case USTAR:
15367c478bd9Sstevel@tonic-gate 			/*
15377c478bd9Sstevel@tonic-gate 			 * If the length of the full name is greater than 256,
15387c478bd9Sstevel@tonic-gate 			 * print out a message and return.
15397c478bd9Sstevel@tonic-gate 			 */
15407c478bd9Sstevel@tonic-gate 			if ((fullnamesize = strlen(Gen.g_nam_p)) > MAXNAM) {
15417c478bd9Sstevel@tonic-gate 				msg(ERR,
15427c478bd9Sstevel@tonic-gate 				    "%s: file name too long", Gen.g_nam_p);
15437c478bd9Sstevel@tonic-gate 				return (0);
15447c478bd9Sstevel@tonic-gate 			} else if (fullnamesize > NAMSIZ) {
15457c478bd9Sstevel@tonic-gate 				/*
15467c478bd9Sstevel@tonic-gate 				 * The length of the full name is greater than
15477c478bd9Sstevel@tonic-gate 				 * 100, so we must split the filename from the
15487c478bd9Sstevel@tonic-gate 				 * path
15497c478bd9Sstevel@tonic-gate 				 */
15507c478bd9Sstevel@tonic-gate 				char namebuff[NAMSIZ+1];
15517c478bd9Sstevel@tonic-gate 				char prebuff[PRESIZ+1];
15527c478bd9Sstevel@tonic-gate 				char *lastslash;
15537c478bd9Sstevel@tonic-gate 				int presize, namesize;
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 				(void) memset(namebuff, '\0',
15567c478bd9Sstevel@tonic-gate 				    sizeof (namebuff));
15577c478bd9Sstevel@tonic-gate 				(void) memset(prebuff, '\0', sizeof (prebuff));
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 				lastslash = strrchr(Gen.g_nam_p, '/');
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 				if (lastslash != NULL) {
15627c478bd9Sstevel@tonic-gate 					namesize = strlen(++lastslash);
15637c478bd9Sstevel@tonic-gate 					presize = fullnamesize - namesize - 1;
15647c478bd9Sstevel@tonic-gate 				} else {
15657c478bd9Sstevel@tonic-gate 					namesize = fullnamesize;
15667c478bd9Sstevel@tonic-gate 					lastslash = Gen.g_nam_p;
15677c478bd9Sstevel@tonic-gate 					presize = 0;
15687c478bd9Sstevel@tonic-gate 				}
15697c478bd9Sstevel@tonic-gate 
15707c478bd9Sstevel@tonic-gate 				/*
15717c478bd9Sstevel@tonic-gate 				 * If the filename is greater than 100 we can't
15727c478bd9Sstevel@tonic-gate 				 * archive the file
15737c478bd9Sstevel@tonic-gate 				 */
15747c478bd9Sstevel@tonic-gate 				if (namesize > NAMSIZ) {
15757c478bd9Sstevel@tonic-gate 					msg(ERR,
15767c478bd9Sstevel@tonic-gate 					    "%s: filename is greater than %d",
15777c478bd9Sstevel@tonic-gate 					    lastslash, NAMSIZ);
15787c478bd9Sstevel@tonic-gate 					return (0);
15797c478bd9Sstevel@tonic-gate 				}
15807c478bd9Sstevel@tonic-gate 				(void) strncpy(&namebuff[0], lastslash,
15817c478bd9Sstevel@tonic-gate 				    namesize);
15827c478bd9Sstevel@tonic-gate 				/*
15837c478bd9Sstevel@tonic-gate 				 * If the prefix is greater than 155 we can't
15847c478bd9Sstevel@tonic-gate 				 * archive the file.
15857c478bd9Sstevel@tonic-gate 				 */
15867c478bd9Sstevel@tonic-gate 				if (presize > PRESIZ) {
15877c478bd9Sstevel@tonic-gate 					msg(ERR,
15887c478bd9Sstevel@tonic-gate 					    "%s: prefix is greater than %d",
15897c478bd9Sstevel@tonic-gate 					    Gen.g_nam_p, PRESIZ);
15907c478bd9Sstevel@tonic-gate 					return (0);
15917c478bd9Sstevel@tonic-gate 				}
15927c478bd9Sstevel@tonic-gate 				(void) strncpy(&prebuff[0], Gen.g_nam_p,
15937c478bd9Sstevel@tonic-gate 				    presize);
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate 				Gen.g_tname = e_zalloc(E_EXIT, namesize + 1);
15967c478bd9Sstevel@tonic-gate 				(void) strcpy(Gen.g_tname, namebuff);
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 				Gen.g_prefix = e_zalloc(E_EXIT, presize + 1);
15997c478bd9Sstevel@tonic-gate 				(void) strcpy(Gen.g_prefix, prebuff);
16007c478bd9Sstevel@tonic-gate 			} else {
16017c478bd9Sstevel@tonic-gate 				Gen.g_tname = Gen.g_nam_p;
16027c478bd9Sstevel@tonic-gate 			}
16037c478bd9Sstevel@tonic-gate 			(void) strcpy(Gen.g_tmagic, "ustar");
16047c478bd9Sstevel@tonic-gate 			(void) strcpy(Gen.g_version, "00");
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 			dpasswd = getpwuid(SrcSt.st_uid);
1607ced83f9bSceastha 			if (dpasswd == NULL) {
16087c478bd9Sstevel@tonic-gate 				msg(EPOST,
16097c478bd9Sstevel@tonic-gate 				    "cpio: could not get passwd information "
16107c478bd9Sstevel@tonic-gate 				    "for %s%s%s",
1611ced83f9bSceastha 				    (Gen.g_attrnam_p == NULL) ?
16127c478bd9Sstevel@tonic-gate 				    Gen.g_nam_p : Gen.g_attrfnam_p,
1613ced83f9bSceastha 				    (Gen.g_attrnam_p == NULL) ?
1614ced83f9bSceastha 				    "" : Gen.g_rw_sysattr ?
1615ced83f9bSceastha 				    gettext(" System Attribute ") :
1616ced83f9bSceastha 				    gettext(" Attribute "),
1617ced83f9bSceastha 				    (Gen.g_attrnam_p == NULL) ?
16187c478bd9Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
16197c478bd9Sstevel@tonic-gate 				/* make name null string */
16207c478bd9Sstevel@tonic-gate 				Gen.g_uname[0] = '\0';
16217c478bd9Sstevel@tonic-gate 			} else {
16227c478bd9Sstevel@tonic-gate 				(void) strncpy(&Gen.g_uname[0],
16237c478bd9Sstevel@tonic-gate 				    dpasswd->pw_name, 32);
16247c478bd9Sstevel@tonic-gate 			}
16257c478bd9Sstevel@tonic-gate 			dgroup = getgrgid(SrcSt.st_gid);
1626ced83f9bSceastha 			if (dgroup == NULL) {
16277c478bd9Sstevel@tonic-gate 				msg(EPOST,
16287c478bd9Sstevel@tonic-gate 				    "cpio: could not get group information "
16297c478bd9Sstevel@tonic-gate 				    "for %s%s%S",
1630ced83f9bSceastha 				    (Gen.g_attrnam_p == NULL) ?
16317c478bd9Sstevel@tonic-gate 				    Gen.g_nam_p : Gen.g_attrfnam_p,
1632ced83f9bSceastha 				    (Gen.g_attrnam_p == NULL) ?
1633ced83f9bSceastha 				    "" : Gen.g_rw_sysattr ?
1634ced83f9bSceastha 				    gettext(" System Attribute ") :
1635ced83f9bSceastha 				    gettext(" Attribute "),
1636ced83f9bSceastha 				    (Gen.g_attrnam_p == NULL) ?
16377c478bd9Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
16387c478bd9Sstevel@tonic-gate 				/* make name null string */
16397c478bd9Sstevel@tonic-gate 				Gen.g_gname[0] = '\0';
16407c478bd9Sstevel@tonic-gate 			} else {
16417c478bd9Sstevel@tonic-gate 				(void) strncpy(&Gen.g_gname[0],
16427c478bd9Sstevel@tonic-gate 				    dgroup->gr_name, 32);
16437c478bd9Sstevel@tonic-gate 			}
16447c478bd9Sstevel@tonic-gate 			Gen.g_typeflag = tartype(ftype);
16457c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
16467c478bd9Sstevel@tonic-gate 		case TAR:
16477c478bd9Sstevel@tonic-gate 			(void) memset(T_lname, '\0', sizeof (T_lname));
16487c478bd9Sstevel@tonic-gate 			break;
16497c478bd9Sstevel@tonic-gate 		default:
16507c478bd9Sstevel@tonic-gate 			msg(EXT, "Impossible header type.");
16517c478bd9Sstevel@tonic-gate 	}
16527c478bd9Sstevel@tonic-gate 
1653ced83f9bSceastha 	if (Use_old_stat && (Gen.g_attrnam_p != NULL)) {
1654ced83f9bSceastha 		/*
1655ced83f9bSceastha 		 * When processing extended attributes, creat_hdr()
1656ced83f9bSceastha 		 * can get called multiple times which means that
1657ced83f9bSceastha 		 * SrcSt.st.st_dev would have gotten converted to
1658ced83f9bSceastha 		 * -Hodc format.  We should always use the original
1659ced83f9bSceastha 		 * device here as we need to be able to match on
1660ced83f9bSceastha 		 * the original device id from the file that was
1661ced83f9bSceastha 		 * previewed in sl_preview_synonyms().
1662ced83f9bSceastha 		 */
1663ced83f9bSceastha 		dev = Savedev;
1664ced83f9bSceastha 	} else {
16657c478bd9Sstevel@tonic-gate 		dev = SrcSt.st_dev;
1666ced83f9bSceastha 	}
16677c478bd9Sstevel@tonic-gate 	ino = SrcSt.st_ino;
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 	if (Use_old_stat) {
16707c478bd9Sstevel@tonic-gate 		SrcSt = *OldSt;
16717c478bd9Sstevel@tonic-gate 	}
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 	Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
16747c478bd9Sstevel@tonic-gate 	Gen.g_uid = SrcSt.st_uid;
16757c478bd9Sstevel@tonic-gate 	Gen.g_gid = SrcSt.st_gid;
16767c478bd9Sstevel@tonic-gate 	Gen.g_dev = SrcSt.st_dev;
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 	if (Use_old_stat) {
16797c478bd9Sstevel@tonic-gate 		/* -Hodc */
16807c478bd9Sstevel@tonic-gate 
1681ced83f9bSceastha 		sl_info_t *p = sl_search(dev, ino, ftype);
16827c478bd9Sstevel@tonic-gate 		Gen.g_ino = p ? p->sl_ino2 : -1;
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 		if (Gen.g_ino == (ulong_t)-1) {
16857c478bd9Sstevel@tonic-gate 			msg(ERR, "%s%s%s: cannot be archived - inode too big "
16867c478bd9Sstevel@tonic-gate 			    "for -Hodc format",
1687ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ?
16887c478bd9Sstevel@tonic-gate 			    Gen.g_nam_p : Gen.g_attrfnam_p,
1689ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ? "" : Gen.g_rw_sysattr ?
1690ced83f9bSceastha 			    gettext(" System Attribute ") :
1691ced83f9bSceastha 			    gettext(" Attribute "),
1692ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ? "" : Gen.g_attrnam_p);
16937c478bd9Sstevel@tonic-gate 			return (0);
16947c478bd9Sstevel@tonic-gate 		}
16957c478bd9Sstevel@tonic-gate 	} else {
16967c478bd9Sstevel@tonic-gate 		Gen.g_ino = SrcSt.st_ino;
16977c478bd9Sstevel@tonic-gate 	}
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate 	Gen.g_mode = SrcSt.st_mode;
17007c478bd9Sstevel@tonic-gate 	Gen.g_mtime = SrcSt.st_mtime;
1701ced83f9bSceastha 	Gen.g_nlink = Adir ? SrcSt.st_nlink : sl_numlinks(dev, ino, ftype);
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate 	if (ftype == S_IFREG || ftype == S_IFLNK)
17047c478bd9Sstevel@tonic-gate 		Gen.g_filesz = (off_t)SrcSt.st_size;
17057c478bd9Sstevel@tonic-gate 	else
17067c478bd9Sstevel@tonic-gate 		Gen.g_filesz = (off_t)0;
17077c478bd9Sstevel@tonic-gate 	Gen.g_rdev = SrcSt.st_rdev;
17087c478bd9Sstevel@tonic-gate 	return (1);
17097c478bd9Sstevel@tonic-gate }
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate /*
17127c478bd9Sstevel@tonic-gate  * creat_lnk: Create a link from the existing name1_p to name2_p.
17137c478bd9Sstevel@tonic-gate  */
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate static
17167c478bd9Sstevel@tonic-gate int
17177c478bd9Sstevel@tonic-gate creat_lnk(int dirfd, char *name1_p, char *name2_p)
17187c478bd9Sstevel@tonic-gate {
17197c478bd9Sstevel@tonic-gate 	int cnt = 0;
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate 	do {
17227c478bd9Sstevel@tonic-gate 		errno = 0;
17237c478bd9Sstevel@tonic-gate 		if (!link(name1_p, name2_p)) {
17247c478bd9Sstevel@tonic-gate 			if (aclp != NULL) {
1725fa9e4066Sahrens 				acl_free(aclp);
17267c478bd9Sstevel@tonic-gate 				aclp = NULL;
1727fa9e4066Sahrens 				acl_is_set = 0;
17287c478bd9Sstevel@tonic-gate 			}
17297c478bd9Sstevel@tonic-gate 			cnt = 0;
17307c478bd9Sstevel@tonic-gate 			break;
1731647ab8f4Sceastha 		} else if ((errno == EEXIST) && (cnt == 0)) {
17327c478bd9Sstevel@tonic-gate 			struct stat lsb1;
17337c478bd9Sstevel@tonic-gate 			struct stat lsb2;
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 			/*
17367c478bd9Sstevel@tonic-gate 			 * Check to see if we are trying to link this
17377c478bd9Sstevel@tonic-gate 			 * file to itself.  If so, count the effort as
17387c478bd9Sstevel@tonic-gate 			 * successful.  If the two files are different,
17397c478bd9Sstevel@tonic-gate 			 * or if either lstat is unsuccessful, proceed
17407c478bd9Sstevel@tonic-gate 			 * as we would have otherwise; the appropriate
17417c478bd9Sstevel@tonic-gate 			 * error will be reported subsequently.
17427c478bd9Sstevel@tonic-gate 			 */
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 			if (lstat(name1_p, &lsb1) != 0) {
17457c478bd9Sstevel@tonic-gate 				msg(ERR, "Cannot lstat source file %s",
17467c478bd9Sstevel@tonic-gate 				    name1_p);
17477c478bd9Sstevel@tonic-gate 			} else {
17487c478bd9Sstevel@tonic-gate 				if (lstat(name2_p, &lsb2) != 0) {
17497c478bd9Sstevel@tonic-gate 					msg(ERR, "Cannot lstat "
1750647ab8f4Sceastha 					    "destination file %s", name2_p);
17517c478bd9Sstevel@tonic-gate 				} else {
1752647ab8f4Sceastha 					if (lsb1.st_dev == lsb2.st_dev &&
1753647ab8f4Sceastha 					    lsb1.st_ino == lsb2.st_ino) {
1754647ab8f4Sceastha 						VERBOSE((Args & (OCv | OCV)),
17557c478bd9Sstevel@tonic-gate 						    name2_p);
17567c478bd9Sstevel@tonic-gate 						return (0);
17577c478bd9Sstevel@tonic-gate 					}
17587c478bd9Sstevel@tonic-gate 				}
17597c478bd9Sstevel@tonic-gate 			}
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 			if (!(Args & OCu) && G_p->g_mtime <= DesSt.st_mtime)
1762647ab8f4Sceastha 				msg(ERR, "Existing \"%s\" same age or newer",
17637c478bd9Sstevel@tonic-gate 				    name2_p);
17647c478bd9Sstevel@tonic-gate 			else if (unlinkat(dirfd, get_component(name2_p), 0) < 0)
17657c478bd9Sstevel@tonic-gate 				msg(ERRN, "Error cannot unlink \"%s\"",
17667c478bd9Sstevel@tonic-gate 				    name2_p);
17677c478bd9Sstevel@tonic-gate 		}
17687c478bd9Sstevel@tonic-gate 		cnt++;
17697c478bd9Sstevel@tonic-gate 	} while ((cnt < 2) && missdir(name2_p) == 0);
17707c478bd9Sstevel@tonic-gate 	if (!cnt) {
17717c478bd9Sstevel@tonic-gate 		char *newname;
17727c478bd9Sstevel@tonic-gate 		char *fromname;
17737c478bd9Sstevel@tonic-gate 		char *attrname;
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 		newname = name2_p;
17767c478bd9Sstevel@tonic-gate 		fromname = name1_p;
17777c478bd9Sstevel@tonic-gate 		attrname = Gen.g_attrnam_p;
17787c478bd9Sstevel@tonic-gate 		if (attrname) {
17797c478bd9Sstevel@tonic-gate 			if (Args & OCp) {
17807c478bd9Sstevel@tonic-gate 				newname = fromname = Fullnam_p;
17817c478bd9Sstevel@tonic-gate 			} else {
17827c478bd9Sstevel@tonic-gate 				newname = Gen.g_attrfnam_p;
17837c478bd9Sstevel@tonic-gate 			}
17847c478bd9Sstevel@tonic-gate 		}
17857c478bd9Sstevel@tonic-gate 		if (Args & OCv) {
17867c478bd9Sstevel@tonic-gate 			(void) fprintf(Err_p,
17877c478bd9Sstevel@tonic-gate 			    gettext("%s%s%s linked to %s%s%s\n"), newname,
1788ced83f9bSceastha 			    (attrname == NULL) ? "" : gettext(" attribute "),
1789ced83f9bSceastha 			    (attrname == NULL) ? "" : attrname,
1790ced83f9bSceastha 			    (attrname == NULL) ? fromname : newname,
1791ced83f9bSceastha 			    (attrname == NULL) ? "" : gettext(" attribute "),
1792ced83f9bSceastha 			    (attrname == NULL) ? "" : name1_p);
1793ced83f9bSceastha 		} else {
17947c478bd9Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), newname);
1795ced83f9bSceastha 		}
17967c478bd9Sstevel@tonic-gate 	} else if (cnt == 1)
17977c478bd9Sstevel@tonic-gate 		msg(ERRN,
17987c478bd9Sstevel@tonic-gate 		    "Unable to create directory for \"%s\"", name2_p);
17997c478bd9Sstevel@tonic-gate 	else if (cnt == 2)
18007c478bd9Sstevel@tonic-gate 		msg(ERRN,
18017c478bd9Sstevel@tonic-gate 		    "Cannot link \"%s\" and \"%s\"", name1_p, name2_p);
18027c478bd9Sstevel@tonic-gate 	return (cnt);
18037c478bd9Sstevel@tonic-gate }
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate /*
18067c478bd9Sstevel@tonic-gate  * creat_spec:
18077c478bd9Sstevel@tonic-gate  *   Create one of the following:
18087c478bd9Sstevel@tonic-gate  *       directory
18097c478bd9Sstevel@tonic-gate  *       character special file
18107c478bd9Sstevel@tonic-gate  *       block special file
18117c478bd9Sstevel@tonic-gate  *       fifo
1812cdd68f5aSceastha  *	 socket
18137c478bd9Sstevel@tonic-gate  */
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate static int
18167c478bd9Sstevel@tonic-gate creat_spec(int dirfd)
18177c478bd9Sstevel@tonic-gate {
18187c478bd9Sstevel@tonic-gate 	char *nam_p;
18197c478bd9Sstevel@tonic-gate 	int cnt, result, rv = 0;
18207c478bd9Sstevel@tonic-gate 	char *curdir;
18217c478bd9Sstevel@tonic-gate 	char *lastslash;
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 	Do_rename = 0;	/* creat_tmp() may reset this */
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate 	if (Args & OCp) {
18267c478bd9Sstevel@tonic-gate 		nam_p = Fullnam_p;
18277c478bd9Sstevel@tonic-gate 	} else {
18287c478bd9Sstevel@tonic-gate 		nam_p = G_p->g_nam_p;
18297c478bd9Sstevel@tonic-gate 	}
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 	/*
18327c478bd9Sstevel@tonic-gate 	 * Is this the extraction of the hidden attribute directory?
1833ced83f9bSceastha 	 * If we are processing the hidden attribute directory of an
1834ced83f9bSceastha 	 * attribute, then just return as modes and times cannot be set.
1835ced83f9bSceastha 	 * Otherwise, if we are processing a hidden attribute, just set
1836ced83f9bSceastha 	 * the mode/times correctly and return.
18377c478bd9Sstevel@tonic-gate 	 */
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 	if (Hiddendir) {
1840ced83f9bSceastha 		if (G_p->g_attrparent_p == NULL) {
1841647ab8f4Sceastha 			if (Args & OCR) {
1842647ab8f4Sceastha 				if (fchownat(dirfd, ".", Rpw_p->pw_uid,
1843647ab8f4Sceastha 				    Rpw_p->pw_gid, 0) != 0) {
1844647ab8f4Sceastha 					msg(ERRN,
1845ced83f9bSceastha 					    "Cannot chown() \"attribute "
1846ced83f9bSceastha 					    "directory of file %s\"",
1847ced83f9bSceastha 					    G_p->g_attrfnam_p);
1848647ab8f4Sceastha 				}
1849647ab8f4Sceastha 			} else if ((fchownat(dirfd, ".", G_p->g_uid,
1850647ab8f4Sceastha 			    G_p->g_gid, 0) != 0) && privileged) {
18517c478bd9Sstevel@tonic-gate 				msg(ERRN,
18527c478bd9Sstevel@tonic-gate 				    "Cannot chown() \"attribute directory of "
18537c478bd9Sstevel@tonic-gate 				    "file %s\"", G_p->g_attrfnam_p);
18547c478bd9Sstevel@tonic-gate 			}
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate 			if (fchmod(dirfd, G_p->g_mode) != 0) {
18577c478bd9Sstevel@tonic-gate 				msg(ERRN,
18587c478bd9Sstevel@tonic-gate 				    "Cannot chmod() \"attribute directory of "
18597c478bd9Sstevel@tonic-gate 				    "file %s\"", G_p->g_attrfnam_p);
18607c478bd9Sstevel@tonic-gate 			}
18617c478bd9Sstevel@tonic-gate 
1862fa9e4066Sahrens 			acl_is_set = 0;
18637c478bd9Sstevel@tonic-gate 			if (Pflag && aclp != NULL) {
1864fa9e4066Sahrens 				if (facl_set(dirfd, aclp) < 0) {
18657c478bd9Sstevel@tonic-gate 					msg(ERRN,
18667c478bd9Sstevel@tonic-gate 					    "failed to set acl on attribute"
1867ced83f9bSceastha 					    " directory of %s ",
1868ced83f9bSceastha 					    G_p->g_attrfnam_p);
18697c478bd9Sstevel@tonic-gate 				} else {
1870fa9e4066Sahrens 					acl_is_set = 1;
18717c478bd9Sstevel@tonic-gate 				}
1872fa9e4066Sahrens 				acl_free(aclp);
18737c478bd9Sstevel@tonic-gate 				aclp = NULL;
18747c478bd9Sstevel@tonic-gate 			}
1875ced83f9bSceastha 		}
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate 		return (1);
18787c478bd9Sstevel@tonic-gate 	}
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate 	result = stat(nam_p, &DesSt);
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate 	if (ustar_dir() || Adir) {
18837c478bd9Sstevel@tonic-gate 		/*
18847c478bd9Sstevel@tonic-gate 		 *  The archive file is a directory.
18857c478bd9Sstevel@tonic-gate 		 *  Skip "." and ".."
18867c478bd9Sstevel@tonic-gate 		 */
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 		curdir = strrchr(nam_p, '.');
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate 		if (curdir != NULL && curdir[1] == NULL) {
18917c478bd9Sstevel@tonic-gate 			lastslash = strrchr(nam_p, '/');
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate 			if (lastslash != NULL) {
18947c478bd9Sstevel@tonic-gate 				lastslash++;
18957c478bd9Sstevel@tonic-gate 			} else {
18967c478bd9Sstevel@tonic-gate 				lastslash = nam_p;
18977c478bd9Sstevel@tonic-gate 			}
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate 			if (!(strcmp(lastslash, ".")) ||
19007c478bd9Sstevel@tonic-gate 			    !(strcmp(lastslash, ".."))) {
19017c478bd9Sstevel@tonic-gate 				return (1);
19027c478bd9Sstevel@tonic-gate 			}
19037c478bd9Sstevel@tonic-gate 		}
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate 		if (result == 0) {
19067c478bd9Sstevel@tonic-gate 			/* A file by the same name exists. */
19077c478bd9Sstevel@tonic-gate 
19087c478bd9Sstevel@tonic-gate 			/* Take care of ACLs */
1909fa9e4066Sahrens 			acl_is_set = 0;
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate 			if (Pflag && aclp != NULL) {
1912fa9e4066Sahrens 				if (acl_set(nam_p, aclp) < 0) {
19137c478bd9Sstevel@tonic-gate 					msg(ERRN,
19147c478bd9Sstevel@tonic-gate 					    "\"%s\": failed to set acl",
19157c478bd9Sstevel@tonic-gate 					    nam_p);
19167c478bd9Sstevel@tonic-gate 				} else {
1917fa9e4066Sahrens 					acl_is_set = 1;
19187c478bd9Sstevel@tonic-gate 				}
19197c478bd9Sstevel@tonic-gate 
1920fa9e4066Sahrens 				acl_free(aclp);
19217c478bd9Sstevel@tonic-gate 				aclp = NULL;
19227c478bd9Sstevel@tonic-gate 			}
19237c478bd9Sstevel@tonic-gate 			if (Args & OCd) {
19247c478bd9Sstevel@tonic-gate 				/*
19257c478bd9Sstevel@tonic-gate 				 * We are creating directories.  Keep the
19267c478bd9Sstevel@tonic-gate 				 * existing file.
19277c478bd9Sstevel@tonic-gate 				 */
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate 				rstfiles(U_KEEP, dirfd);
19307c478bd9Sstevel@tonic-gate 			}
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate 			/* Report success. */
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 			return (1);
19357c478bd9Sstevel@tonic-gate 		}
19367c478bd9Sstevel@tonic-gate 	} else {
19377c478bd9Sstevel@tonic-gate 		/* The archive file is not a directory. */
19387c478bd9Sstevel@tonic-gate 
19397c478bd9Sstevel@tonic-gate 		if (result == 0) {
19407c478bd9Sstevel@tonic-gate 			/*
19417c478bd9Sstevel@tonic-gate 			 * A file by the same name exists.  Move it to a
19427c478bd9Sstevel@tonic-gate 			 * temporary file.
19437c478bd9Sstevel@tonic-gate 			 */
19447c478bd9Sstevel@tonic-gate 
19457c478bd9Sstevel@tonic-gate 			if (creat_tmp(nam_p) < 0) {
19467c478bd9Sstevel@tonic-gate 				/*
19477c478bd9Sstevel@tonic-gate 				 * We weren't able to create the temp file.
19487c478bd9Sstevel@tonic-gate 				 * Report failure.
19497c478bd9Sstevel@tonic-gate 				 */
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 				return (0);
19527c478bd9Sstevel@tonic-gate 			}
19537c478bd9Sstevel@tonic-gate 		}
19547c478bd9Sstevel@tonic-gate 	}
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 	/*
19577c478bd9Sstevel@tonic-gate 	 * This pile tries to create the file directly, and, if there is a
19587c478bd9Sstevel@tonic-gate 	 * problem, creates missing directories, and then tries to create the
19597c478bd9Sstevel@tonic-gate 	 * file again.  Two strikes and you're out.
19607c478bd9Sstevel@tonic-gate 	 */
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate 	cnt = 0;
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate 	do {
19657c478bd9Sstevel@tonic-gate 		if (ustar_dir() || Adir) {
19667c478bd9Sstevel@tonic-gate 			/* The archive file is a directory. */
19677c478bd9Sstevel@tonic-gate 
19687c478bd9Sstevel@tonic-gate 			result = mkdir(nam_p, G_p->g_mode);
19697c478bd9Sstevel@tonic-gate 		} else if (ustar_spec() || Aspec) {
19707c478bd9Sstevel@tonic-gate 			/*
19717c478bd9Sstevel@tonic-gate 			 * The archive file is block special,
1972cdd68f5aSceastha 			 * char special, socket, or a fifo.
1973cdd68f5aSceastha 			 * Note that, for a socket, the third
1974cdd68f5aSceastha 			 * parameter to mknod() is ignored.
19757c478bd9Sstevel@tonic-gate 			 */
19767c478bd9Sstevel@tonic-gate 
19777c478bd9Sstevel@tonic-gate 			result = mknod(nam_p, (int)G_p->g_mode,
19787c478bd9Sstevel@tonic-gate 			    (int)G_p->g_rdev);
19797c478bd9Sstevel@tonic-gate 		}
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate 		if (result >= 0) {
19827c478bd9Sstevel@tonic-gate 			/*
19837c478bd9Sstevel@tonic-gate 			 * The file creation succeeded.  Take care of the ACLs.
19847c478bd9Sstevel@tonic-gate 			 */
19857c478bd9Sstevel@tonic-gate 
1986fa9e4066Sahrens 			acl_is_set = 0;
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate 			if (Pflag && aclp != NULL) {
1989fa9e4066Sahrens 				if (acl_set(nam_p, aclp) < 0) {
19907c478bd9Sstevel@tonic-gate 					msg(ERRN,
19917c478bd9Sstevel@tonic-gate 					    "\"%s\": failed to set acl", nam_p);
19927c478bd9Sstevel@tonic-gate 				} else {
1993fa9e4066Sahrens 					acl_is_set = 1;
19947c478bd9Sstevel@tonic-gate 				}
19957c478bd9Sstevel@tonic-gate 
1996fa9e4066Sahrens 				acl_free(aclp);
19977c478bd9Sstevel@tonic-gate 				aclp = NULL;
19987c478bd9Sstevel@tonic-gate 			}
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate 			cnt = 0;
20017c478bd9Sstevel@tonic-gate 			break;
20027c478bd9Sstevel@tonic-gate 		}
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate 		cnt++;
20057c478bd9Sstevel@tonic-gate 	} while (cnt < 2 && missdir(nam_p) == 0);
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate 	switch (cnt) {
20087c478bd9Sstevel@tonic-gate 	case 0:
20097c478bd9Sstevel@tonic-gate 		rv = 1;
20107c478bd9Sstevel@tonic-gate 		rstfiles(U_OVER, dirfd);
20117c478bd9Sstevel@tonic-gate 		break;
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate 	case 1:
20147c478bd9Sstevel@tonic-gate 		msg(ERRN,
20157c478bd9Sstevel@tonic-gate 		    "Cannot create directory for \"%s\"", nam_p);
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate 		if (*Over_p == '\0') {
20187c478bd9Sstevel@tonic-gate 			rstfiles(U_KEEP, dirfd);
20197c478bd9Sstevel@tonic-gate 		}
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 		break;
20227c478bd9Sstevel@tonic-gate 
20237c478bd9Sstevel@tonic-gate 	case 2:
20247c478bd9Sstevel@tonic-gate 		if (ustar_dir() || Adir) {
20257c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot create directory \"%s\"", nam_p);
20267c478bd9Sstevel@tonic-gate 		} else if (ustar_spec() || Aspec) {
20277c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot mknod() \"%s\"", nam_p);
20287c478bd9Sstevel@tonic-gate 		}
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate 		if (*Over_p == '\0') {
20317c478bd9Sstevel@tonic-gate 			rstfiles(U_KEEP, dirfd);
20327c478bd9Sstevel@tonic-gate 		}
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate 		break;
20357c478bd9Sstevel@tonic-gate 
20367c478bd9Sstevel@tonic-gate 	default:
20377c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible case.");
20387c478bd9Sstevel@tonic-gate 	}
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 	return (rv);
20417c478bd9Sstevel@tonic-gate }
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate /*
20447c478bd9Sstevel@tonic-gate  * creat_tmp:
20457c478bd9Sstevel@tonic-gate  */
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate static int
20487c478bd9Sstevel@tonic-gate creat_tmp(char *nam_p)
20497c478bd9Sstevel@tonic-gate {
20507c478bd9Sstevel@tonic-gate 	char *t_p;
20517c478bd9Sstevel@tonic-gate 	int	cwd;
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate 	if ((Args & OCp) && G_p->g_ino == DesSt.st_ino &&
20547c478bd9Sstevel@tonic-gate 	    G_p->g_dev == DesSt.st_dev) {
20557c478bd9Sstevel@tonic-gate 		msg(ERR, "Attempt to pass a file to itself.");
20567c478bd9Sstevel@tonic-gate 		return (-1);
20577c478bd9Sstevel@tonic-gate 	}
20587c478bd9Sstevel@tonic-gate 
20597c478bd9Sstevel@tonic-gate 	if (G_p->g_mtime <= DesSt.st_mtime && !(Args & OCu)) {
20607c478bd9Sstevel@tonic-gate 		msg(ERR, "Existing \"%s\" same age or newer", nam_p);
20617c478bd9Sstevel@tonic-gate 		return (-1);
20627c478bd9Sstevel@tonic-gate 	}
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate 	/* Make the temporary file name. */
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate 	(void) strcpy(Over_p, nam_p);
20677c478bd9Sstevel@tonic-gate 	t_p = Over_p + strlen(Over_p);
20687c478bd9Sstevel@tonic-gate 
20697c478bd9Sstevel@tonic-gate 	while (t_p != Over_p) {
20707c478bd9Sstevel@tonic-gate 		if (*(t_p - 1) == '/')
20717c478bd9Sstevel@tonic-gate 			break;
20727c478bd9Sstevel@tonic-gate 		t_p--;
20737c478bd9Sstevel@tonic-gate 	}
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 	(void) strcpy(t_p, "XXXXXX");
20767c478bd9Sstevel@tonic-gate 
2077ced83f9bSceastha 	if (G_p->g_attrnam_p != NULL) {
20787c478bd9Sstevel@tonic-gate 		/*
20797c478bd9Sstevel@tonic-gate 		 * Save our current directory, so we can go into
20807c478bd9Sstevel@tonic-gate 		 * the attribute directory to make the temp file
20817c478bd9Sstevel@tonic-gate 		 * and then return.
20827c478bd9Sstevel@tonic-gate 		 */
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 		cwd = save_cwd();
20857c478bd9Sstevel@tonic-gate 		(void) fchdir(G_p->g_dirfd);
20867c478bd9Sstevel@tonic-gate 	}
20877c478bd9Sstevel@tonic-gate 
20887c478bd9Sstevel@tonic-gate 	(void) mktemp(Over_p);
20897c478bd9Sstevel@tonic-gate 
2090ced83f9bSceastha 	if (G_p->g_attrnam_p != NULL) {
20917c478bd9Sstevel@tonic-gate 		/* Return to the current directory. */
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 		rest_cwd(cwd);
20947c478bd9Sstevel@tonic-gate 	}
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate 	if (*Over_p == '\0') {
20977c478bd9Sstevel@tonic-gate 		/* mktemp reports a failure. */
20987c478bd9Sstevel@tonic-gate 
20997c478bd9Sstevel@tonic-gate 		msg(ERR, "Cannot get temporary file name.");
21007c478bd9Sstevel@tonic-gate 		return (-1);
21017c478bd9Sstevel@tonic-gate 	}
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate 	/*
21047c478bd9Sstevel@tonic-gate 	 * If it's a regular file, write to the temporary file, and then rename
2105da6c28aaSamw 	 * in order to accommodate potential executables.
21067c478bd9Sstevel@tonic-gate 	 *
21077c478bd9Sstevel@tonic-gate 	 * Note: g_typeflag is only defined (set) for USTAR archive types.  It
21087c478bd9Sstevel@tonic-gate 	 * defaults to 0 in the cpio-format-regular file case, so this test
21097c478bd9Sstevel@tonic-gate 	 * succeeds.
21107c478bd9Sstevel@tonic-gate 	 */
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate 	if (G_p->g_typeflag == 0 &&
21137c478bd9Sstevel@tonic-gate 	    (DesSt.st_mode & (ulong_t)Ftype) == S_IFREG &&
21147c478bd9Sstevel@tonic-gate 	    (G_p->g_mode & (ulong_t)Ftype) == S_IFREG) {
21157c478bd9Sstevel@tonic-gate 		/*
21167c478bd9Sstevel@tonic-gate 		 * The archive file and the filesystem file are both regular
21177c478bd9Sstevel@tonic-gate 		 * files.  We write to the temporary file in this case.
21187c478bd9Sstevel@tonic-gate 		 */
21197c478bd9Sstevel@tonic-gate 
21207c478bd9Sstevel@tonic-gate 		if (Args & OCp) {
2121ced83f9bSceastha 			if (G_p->g_attrnam_p == NULL) {
21227c478bd9Sstevel@tonic-gate 				Fullnam_p = Over_p;
21237c478bd9Sstevel@tonic-gate 			} else {
21247c478bd9Sstevel@tonic-gate 				Attrfile_p = Over_p;
21257c478bd9Sstevel@tonic-gate 			}
21267c478bd9Sstevel@tonic-gate 		} else {
21277c478bd9Sstevel@tonic-gate 			G_p->g_nam_p = Over_p;
2128ced83f9bSceastha 			if (G_p->g_attrnam_p != NULL) {
21297c478bd9Sstevel@tonic-gate 				Attrfile_p = Over_p;
21307c478bd9Sstevel@tonic-gate 			}
21317c478bd9Sstevel@tonic-gate 		}
21327c478bd9Sstevel@tonic-gate 
2133ced83f9bSceastha 		if (G_p->g_attrnam_p == NULL) {
21347c478bd9Sstevel@tonic-gate 			Over_p = nam_p;
21357c478bd9Sstevel@tonic-gate 		} else {
21367c478bd9Sstevel@tonic-gate 			Over_p = G_p->g_attrnam_p;
21377c478bd9Sstevel@tonic-gate 		}
21387c478bd9Sstevel@tonic-gate 
21397c478bd9Sstevel@tonic-gate 		Do_rename = 1;
21407c478bd9Sstevel@tonic-gate 	} else {
21417c478bd9Sstevel@tonic-gate 		/*
21427c478bd9Sstevel@tonic-gate 		 * Either the archive file or the filesystem file is not a
21437c478bd9Sstevel@tonic-gate 		 * regular file.
21447c478bd9Sstevel@tonic-gate 		 */
21457c478bd9Sstevel@tonic-gate 
21467c478bd9Sstevel@tonic-gate 		Do_rename = 0;
21477c478bd9Sstevel@tonic-gate 
21487c478bd9Sstevel@tonic-gate 		if (S_ISDIR(DesSt.st_mode)) {
21497c478bd9Sstevel@tonic-gate 			/*
21507c478bd9Sstevel@tonic-gate 			 * The filesystem file is a directory.
21517c478bd9Sstevel@tonic-gate 			 *
21527c478bd9Sstevel@tonic-gate 			 * Save the current working directory because we will
21537c478bd9Sstevel@tonic-gate 			 * want to restore it back just in case remove_dir()
21547c478bd9Sstevel@tonic-gate 			 * fails or get confused about where we should be.
21557c478bd9Sstevel@tonic-gate 			 */
21567c478bd9Sstevel@tonic-gate 
21577c478bd9Sstevel@tonic-gate 			*Over_p = '\0';
21587c478bd9Sstevel@tonic-gate 			cwd = save_cwd();
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate 			if (remove_dir(nam_p) < 0) {
21617c478bd9Sstevel@tonic-gate 				msg(ERRN,
21627c478bd9Sstevel@tonic-gate 				    "Cannot remove the directory \"%s\"",
21637c478bd9Sstevel@tonic-gate 				    nam_p);
21647c478bd9Sstevel@tonic-gate 				/*
21657c478bd9Sstevel@tonic-gate 				 * Restore working directory back to the one
21667c478bd9Sstevel@tonic-gate 				 * saved earlier.
21677c478bd9Sstevel@tonic-gate 				 */
21687c478bd9Sstevel@tonic-gate 
21697c478bd9Sstevel@tonic-gate 				rest_cwd(cwd);
21707c478bd9Sstevel@tonic-gate 				return (-1);
21717c478bd9Sstevel@tonic-gate 			}
21727c478bd9Sstevel@tonic-gate 
21737c478bd9Sstevel@tonic-gate 			/*
21747c478bd9Sstevel@tonic-gate 			 * Restore working directory back to the one
21757c478bd9Sstevel@tonic-gate 			 * saved earlier
21767c478bd9Sstevel@tonic-gate 			 */
21777c478bd9Sstevel@tonic-gate 
21787c478bd9Sstevel@tonic-gate 			rest_cwd(cwd);
21797c478bd9Sstevel@tonic-gate 		} else {
21807c478bd9Sstevel@tonic-gate 			/*
21817c478bd9Sstevel@tonic-gate 			 * The file is not a directory. Will use the original
21827c478bd9Sstevel@tonic-gate 			 * link/unlink construct, however, if the file is
21837c478bd9Sstevel@tonic-gate 			 * namefs, link would fail with EXDEV. Therefore, we
21847c478bd9Sstevel@tonic-gate 			 * use rename() first to back up the file.
21857c478bd9Sstevel@tonic-gate 			 */
21867c478bd9Sstevel@tonic-gate 			if (rename(nam_p, Over_p) < 0) {
21877c478bd9Sstevel@tonic-gate 				/*
21887c478bd9Sstevel@tonic-gate 				 * If rename failed, try old construction
21897c478bd9Sstevel@tonic-gate 				 * method.
21907c478bd9Sstevel@tonic-gate 				 */
21917c478bd9Sstevel@tonic-gate 				if (link(nam_p, Over_p) < 0) {
21927c478bd9Sstevel@tonic-gate 					msg(ERRN,
2193ced83f9bSceastha 					    "Cannot rename temporary file "
2194ced83f9bSceastha 					    "\"%s\" to \"%s\"", Over_p, nam_p);
21957c478bd9Sstevel@tonic-gate 					*Over_p = '\0';
21967c478bd9Sstevel@tonic-gate 					return (-1);
21977c478bd9Sstevel@tonic-gate 				}
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate 				if (unlink(nam_p) < 0) {
22007c478bd9Sstevel@tonic-gate 					msg(ERRN,
22017c478bd9Sstevel@tonic-gate 					    "Cannot unlink() current \"%s\"",
22027c478bd9Sstevel@tonic-gate 					    nam_p);
22037c478bd9Sstevel@tonic-gate 					(void) unlink(Over_p);
22047c478bd9Sstevel@tonic-gate 					*Over_p = '\0';
22057c478bd9Sstevel@tonic-gate 					return (-1);
22067c478bd9Sstevel@tonic-gate 				}
22077c478bd9Sstevel@tonic-gate 			}
22087c478bd9Sstevel@tonic-gate 		}
22097c478bd9Sstevel@tonic-gate 	}
22107c478bd9Sstevel@tonic-gate 
22117c478bd9Sstevel@tonic-gate 	return (1);
22127c478bd9Sstevel@tonic-gate }
22137c478bd9Sstevel@tonic-gate 
22147c478bd9Sstevel@tonic-gate /*
2215ced83f9bSceastha  * Copy the datasize amount of data from the input file to buffer.
2216ced83f9bSceastha  *
2217ced83f9bSceastha  * ifd		- Input file descriptor.
2218ced83f9bSceastha  * buffer	- Buffer (allocated by caller) to copy data to.
2219ced83f9bSceastha  * datasize	- The amount of data to read from the input file
2220ced83f9bSceastha  *		and copy to the buffer.
2221ced83f9bSceastha  * error	- When reading from an Archive file, indicates unreadable
2222ced83f9bSceastha  *		data was encountered, otherwise indicates errno.
2223ced83f9bSceastha  * data_in_info	- Information needed when called from data_in().
2224ced83f9bSceastha  */
2225ced83f9bSceastha static ssize_t
2226ced83f9bSceastha read_chunk(int ifd, char *buffer, size_t datasize, data_in_t *data_in_info)
2227ced83f9bSceastha {
2228ced83f9bSceastha 	if (Args & OCp) {
2229ced83f9bSceastha 		return (read(ifd, buffer, datasize));
2230ced83f9bSceastha 	} else {
2231ced83f9bSceastha 		FILL(datasize);
2232ced83f9bSceastha 		if (data_in_info->data_in_proc_mode != P_SKIP) {
2233ced83f9bSceastha 			if (Hdr_type == CRC)
2234ced83f9bSceastha 				data_in_info->data_in_cksumval += cksum(CRC,
2235ced83f9bSceastha 				    datasize, NULL);
2236ced83f9bSceastha 			if (data_in_info->data_in_swapfile)
2237ced83f9bSceastha 				swap(Buffr.b_out_p, datasize);
2238ced83f9bSceastha 
2239ced83f9bSceastha 
2240ced83f9bSceastha 			/*
2241ced83f9bSceastha 			 * if the bar archive is compressed, set up a pipe and
2242ced83f9bSceastha 			 * do the de-compression while reading in the file
2243ced83f9bSceastha 			 */
2244ced83f9bSceastha 			if (Hdr_type == BAR) {
2245ced83f9bSceastha 				if (data_in_info->data_in_compress_flag == 0 &&
2246ced83f9bSceastha 				    Compressed) {
2247ced83f9bSceastha 					setup_uncompress(
2248ced83f9bSceastha 					    &(data_in_info->data_in_pipef));
2249ced83f9bSceastha 					data_in_info->data_in_compress_flag++;
2250ced83f9bSceastha 				}
2251ced83f9bSceastha 			}
2252ced83f9bSceastha 		}
2253ced83f9bSceastha 		(void) memcpy(buffer, Buffr.b_out_p, datasize);
2254ced83f9bSceastha 		Buffr.b_out_p += datasize;
2255ced83f9bSceastha 		Buffr.b_cnt -= datasize;
2256ced83f9bSceastha 		return (datasize);
2257ced83f9bSceastha 	}
2258ced83f9bSceastha }
2259ced83f9bSceastha 
2260ced83f9bSceastha 
2261ced83f9bSceastha /*
2262ced83f9bSceastha  * Copy data from the input file to output file descriptor.
2263ced83f9bSceastha  * If ifd is -1, then the input file is the archive file.
2264ced83f9bSceastha  *
2265ced83f9bSceastha  * Parameters
2266ced83f9bSceastha  *	ifd		- Input file descriptor to read from.
2267ced83f9bSceastha  *	ofd		- Output file descriptor of extracted file.
2268ced83f9bSceastha  *	rw_sysattr	- Flag indicating if a file is an extended
2269ced83f9bSceastha  *			system attribute file.
2270ced83f9bSceastha  *	bytes		- Amount of data of copy/write.
2271ced83f9bSceastha  *	blocksize	- Amount of data to read at a time from either
2272ced83f9bSceastha  *			the input file descriptor or from the archive.
2273ced83f9bSceastha  *	data_in_info	- information needed while reading data when
2274ced83f9bSceastha  *			called by data_in().
2275ced83f9bSceastha  *
2276ced83f9bSceastha  * Return code
2277ced83f9bSceastha  *	0		Success
2278ced83f9bSceastha  *	< 0		An error occurred during the read of the input
2279ced83f9bSceastha  *			file
2280ced83f9bSceastha  *	> 0		An error occurred during the write of the output
2281ced83f9bSceastha  *			file descriptor.
2282ced83f9bSceastha  */
2283ced83f9bSceastha static int
2284ced83f9bSceastha data_copy(int ifd, int ofd, int rw_sysattr, uint_t bytes,
2285ced83f9bSceastha     size_t blocksize, data_in_t *data_in_info)
2286ced83f9bSceastha {
2287ced83f9bSceastha 	char *buf;
2288ced83f9bSceastha 	int write_it = ((data_in_info == NULL) ||
2289ced83f9bSceastha 	    data_in_info->data_in_proc_mode != P_SKIP);
2290ced83f9bSceastha 	size_t bytesread;
2291ced83f9bSceastha 	size_t maxwrite;
2292ced83f9bSceastha 	size_t piosize;		/* preferred I/O size */
2293ced83f9bSceastha 	ssize_t got;
2294ced83f9bSceastha 	ssize_t cnt;
2295ced83f9bSceastha 	struct stat tsbuf;
2296ced83f9bSceastha 
2297ced83f9bSceastha 	/* No data to copy. */
2298ced83f9bSceastha 	if (bytes == 0) {
2299ced83f9bSceastha 		return (0);
2300ced83f9bSceastha 	}
2301ced83f9bSceastha 
2302ced83f9bSceastha 	/*
2303ced83f9bSceastha 	 * To figure out the size of the buffer used to accumulate data
2304ced83f9bSceastha 	 * from readtape() and to write to the file, we need to determine
2305ced83f9bSceastha 	 * the largest chunk of data to be written to the file at one time.
2306ced83f9bSceastha 	 * This is determined based on the following three things:
2307ced83f9bSceastha 	 *	1) The size of the archived file.
2308ced83f9bSceastha 	 *	2) The preferred I/O size of the file.
2309ced83f9bSceastha 	 *	3) If the file is a read-write system attribute file.
2310ced83f9bSceastha 	 *
2311ced83f9bSceastha 	 * If the size of the file is less than the preferred I/O
2312ced83f9bSceastha 	 * size or it's a read-write system attribute file, which must be
2313ced83f9bSceastha 	 * written in one operation, then set the maximum write size to the
2314ced83f9bSceastha 	 * size of the archived file.  Otherwise, the maximum write size is
2315ced83f9bSceastha 	 * preferred I/O size.
2316ced83f9bSceastha 	 */
2317ced83f9bSceastha 	if (rw_sysattr || (bytes < blocksize)) {
2318ced83f9bSceastha 		maxwrite = bytes;
2319ced83f9bSceastha 	} else {
2320ced83f9bSceastha 		if (fstat(ofd, &tsbuf) == 0) {
2321ced83f9bSceastha 			piosize = tsbuf.st_blksize;
2322ced83f9bSceastha 		} else {
2323ced83f9bSceastha 			piosize = blocksize;
2324ced83f9bSceastha 		}
2325ced83f9bSceastha 		maxwrite = min(bytes, piosize);
2326ced83f9bSceastha 	}
2327ced83f9bSceastha 	buf = e_zalloc(E_EXIT, maxwrite);
2328ced83f9bSceastha 
2329ced83f9bSceastha 	while (bytes > 0) {
2330ced83f9bSceastha 		/*
2331ced83f9bSceastha 		 * Read as much data as we can.  We're limited by
2332ced83f9bSceastha 		 * the smallest of:
2333ced83f9bSceastha 		 *	- the number of bytes left to be read,
2334ced83f9bSceastha 		 *	- the size of the chunk of data to read (blocksize).
2335ced83f9bSceastha 		 */
2336ced83f9bSceastha 		for (bytesread = 0; bytesread < maxwrite; bytesread += got) {
2337ced83f9bSceastha 
2338ced83f9bSceastha 			/*
2339ced83f9bSceastha 			 * Read the data from either the input file descriptor
2340ced83f9bSceastha 			 * or the archive file.  read_chunk() will only return
2341ced83f9bSceastha 			 * <= 0 if called from data_pass().
2342ced83f9bSceastha 			 */
2343ced83f9bSceastha 			errno = 0;
2344ced83f9bSceastha 			if ((got = read_chunk(ifd, buf + bytesread,
2345ced83f9bSceastha 			    min(maxwrite - bytesread, blocksize),
2346ced83f9bSceastha 			    data_in_info)) <= 0) {
2347ced83f9bSceastha 
2348ced83f9bSceastha 				/*
2349ced83f9bSceastha 				 * If data couldn't be read from the input file
2350ced83f9bSceastha 				 * descriptor, set corrupt to indicate the error
2351ced83f9bSceastha 				 * and return.
2352ced83f9bSceastha 				 */
2353ced83f9bSceastha 				free(buf);
2354ced83f9bSceastha 				return (-1);
2355ced83f9bSceastha 			}
2356ced83f9bSceastha 		}
2357ced83f9bSceastha 
2358ced83f9bSceastha 		/*
2359ced83f9bSceastha 		 * write_it will always be true when called from data_pass(),
2360ced83f9bSceastha 		 * however, it will not be true when called from data_in()
2361ced83f9bSceastha 		 * if we are just doing a table of contents or if there
2362ced83f9bSceastha 		 * there was a problem writing to the file earlier and now
2363ced83f9bSceastha 		 * we're just reading the data without writing it order to
2364ced83f9bSceastha 		 * process the next file.
2365ced83f9bSceastha 		 */
2366ced83f9bSceastha 		if (write_it) {
2367ced83f9bSceastha 			errno = 0;
2368ced83f9bSceastha 			if ((cnt = write(ofd, buf, maxwrite)) < maxwrite) {
2369ced83f9bSceastha 				/*
2370ced83f9bSceastha 				 * data_in() needs to know if it was an
2371ced83f9bSceastha 				 * actual write(2) failure, or if we just
2372ced83f9bSceastha 				 * couldn't write all of the data requested
2373ced83f9bSceastha 				 * so that we know that the rest of the file's
2374ced83f9bSceastha 				 * data can be read but not written.
2375ced83f9bSceastha 				 */
2376ced83f9bSceastha 				if ((cnt != -1) && (data_in_info != NULL)) {
2377ced83f9bSceastha 					data_in_info->data_in_partialflg = 1;
2378ced83f9bSceastha 				}
2379ced83f9bSceastha 				free(buf);
2380ced83f9bSceastha 				return (1);
2381395fcd24Sceastha 			} else if (Args & OCp) {
2382395fcd24Sceastha 				Blocks += (u_longlong_t)((cnt +
2383395fcd24Sceastha 				    (Bufsize - 1)) / Bufsize);
2384ced83f9bSceastha 			}
2385ced83f9bSceastha 		}
2386ced83f9bSceastha 		bytes -= maxwrite;
2387ced83f9bSceastha 
2388ced83f9bSceastha 		/*
2389ced83f9bSceastha 		 * If we've reached this point and there is still data
2390ced83f9bSceastha 		 * to be written, maxwrite had to have been determined
2391ced83f9bSceastha 		 * by the preferred I/O size.  If the number of bytes
2392ced83f9bSceastha 		 * left to write is smaller than the preferred I/O size,
2393ced83f9bSceastha 		 * then we're about to do our final write to the file, so
2394ced83f9bSceastha 		 * just set maxwrite to the number of bytes left to write.
2395ced83f9bSceastha 		 */
2396ced83f9bSceastha 		if ((bytes > 0) && (bytes < maxwrite)) {
2397ced83f9bSceastha 			maxwrite = bytes;
2398ced83f9bSceastha 		}
2399ced83f9bSceastha 	}
2400ced83f9bSceastha 	free(buf);
2401ced83f9bSceastha 
2402ced83f9bSceastha 	return (0);
2403ced83f9bSceastha }
2404ced83f9bSceastha /*
24057c478bd9Sstevel@tonic-gate  * data_in:  If proc_mode == P_PROC, bread() the file's data from the archive
24067c478bd9Sstevel@tonic-gate  * and write(2) it to the open fdes gotten from openout().  If proc_mode ==
24077c478bd9Sstevel@tonic-gate  * P_SKIP, or becomes P_SKIP (due to errors etc), bread(2) the file's data
24087c478bd9Sstevel@tonic-gate  * and ignore it.  If the user specified any of the "swap" options (b, s or S),
24097c478bd9Sstevel@tonic-gate  * and the length of the file is not appropriate for that action, do not
24107c478bd9Sstevel@tonic-gate  * perform the "swap", otherwise perform the action on a buffer by buffer basis.
24117c478bd9Sstevel@tonic-gate  * If the CRC header was selected, calculate a running checksum as each buffer
24127c478bd9Sstevel@tonic-gate  * is processed.
24137c478bd9Sstevel@tonic-gate  */
24147c478bd9Sstevel@tonic-gate 
24157c478bd9Sstevel@tonic-gate static void
24167c478bd9Sstevel@tonic-gate data_in(int proc_mode)
24177c478bd9Sstevel@tonic-gate {
24187c478bd9Sstevel@tonic-gate 	char *nam_p;
2419ced83f9bSceastha 	int pad;
2420ced83f9bSceastha 	int rv;
2421ced83f9bSceastha 	int swapfile = 0;
24227c478bd9Sstevel@tonic-gate 	int cstatus = 0;
2423ced83f9bSceastha 	data_in_t *data_in_info;
24247c478bd9Sstevel@tonic-gate 
2425ced83f9bSceastha 	if (G_p->g_attrnam_p != NULL) {
24267c478bd9Sstevel@tonic-gate 		nam_p = G_p->g_attrnam_p;
24277c478bd9Sstevel@tonic-gate 	} else {
24287c478bd9Sstevel@tonic-gate 		nam_p = G_p->g_nam_p;
24297c478bd9Sstevel@tonic-gate 	}
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate 	if (((G_p->g_mode & Ftype) == S_IFLNK && proc_mode != P_SKIP) ||
24327c478bd9Sstevel@tonic-gate 	    (Hdr_type == BAR && bar_linkflag == '2' && proc_mode != P_SKIP)) {
24337c478bd9Sstevel@tonic-gate 		proc_mode = P_SKIP;
24347c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
24357c478bd9Sstevel@tonic-gate 	}
24367c478bd9Sstevel@tonic-gate 	if (Args & (OCb | OCs | OCS)) { /* verfify that swapping is possible */
24377c478bd9Sstevel@tonic-gate 		swapfile = 1;
24387c478bd9Sstevel@tonic-gate 		if (Args & (OCs | OCb) && G_p->g_filesz % 2) {
24397c478bd9Sstevel@tonic-gate 			msg(ERR,
24407c478bd9Sstevel@tonic-gate 			    "Cannot swap bytes of \"%s\", odd number of bytes",
24417c478bd9Sstevel@tonic-gate 			    nam_p);
24427c478bd9Sstevel@tonic-gate 			swapfile = 0;
24437c478bd9Sstevel@tonic-gate 		}
24447c478bd9Sstevel@tonic-gate 		if (Args & (OCS | OCb) && G_p->g_filesz % 4) {
24457c478bd9Sstevel@tonic-gate 			msg(ERR,
24467c478bd9Sstevel@tonic-gate 			    "Cannot swap halfwords of \"%s\", odd number "
24477c478bd9Sstevel@tonic-gate 			    "of halfwords", nam_p);
24487c478bd9Sstevel@tonic-gate 			swapfile = 0;
24497c478bd9Sstevel@tonic-gate 		}
24507c478bd9Sstevel@tonic-gate 	}
2451ced83f9bSceastha 
2452ced83f9bSceastha 	data_in_info = e_zalloc(E_EXIT, sizeof (data_in_t));
2453ced83f9bSceastha 	data_in_info->data_in_swapfile = swapfile;
2454ced83f9bSceastha 	data_in_info->data_in_proc_mode = proc_mode;
2455ced83f9bSceastha 	data_in_info->data_in_partialflg = 0;
2456ced83f9bSceastha 	data_in_info->data_in_cksumval = 0L;
2457ced83f9bSceastha 	data_in_info->data_in_compress_flag = 0;
24587c478bd9Sstevel@tonic-gate 
24597c478bd9Sstevel@tonic-gate 	/* This writes out the file from the archive */
2460ced83f9bSceastha 	if ((rv = data_copy(-1, Ofile, (G_p->g_attrnam_p == NULL) ? 0 :
2461ced83f9bSceastha 	    G_p->g_rw_sysattr, G_p->g_filesz, CPIOBSZ, data_in_info)) != 0) {
2462ced83f9bSceastha 		if (data_in_info->data_in_partialflg) {
2463ced83f9bSceastha 			msg(EXTN, "Cannot write \"%s%s%s\"",
2464ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
2465ced83f9bSceastha 			    G_p->g_attrfnam_p,
2466ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
2467ced83f9bSceastha 			    G_p->g_rw_sysattr ?
2468ced83f9bSceastha 			    gettext(" System Attribute ") :
2469ced83f9bSceastha 			    gettext(" Attribute "), nam_p);
2470ced83f9bSceastha 		} else {
2471ced83f9bSceastha 			msg(ERRN, "Cannot write \"%s%s%s\"",
2472ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
2473ced83f9bSceastha 			    G_p->g_attrfnam_p,
2474ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
2475ced83f9bSceastha 			    G_p->g_rw_sysattr ?
2476ced83f9bSceastha 			    gettext(" System Attribute ") :
2477ced83f9bSceastha 			    gettext(" Attribute "), nam_p);
2478ced83f9bSceastha 		}
24797c478bd9Sstevel@tonic-gate 
24807c478bd9Sstevel@tonic-gate 		/*
2481ced83f9bSceastha 		 * Even though we couldn't write to the file,
2482ced83f9bSceastha 		 * we need to continue reading the data for this
2483ced83f9bSceastha 		 * file so that we can process the next file in
2484ced83f9bSceastha 		 * the archive.
24857c478bd9Sstevel@tonic-gate 		 */
24867c478bd9Sstevel@tonic-gate 		proc_mode = P_SKIP;
2487ced83f9bSceastha 		data_in_info->data_in_proc_mode = proc_mode;
24887c478bd9Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_dirfd);
24897c478bd9Sstevel@tonic-gate 		cstatus = close(Ofile);
24907c478bd9Sstevel@tonic-gate 		Ofile = 0;
24917c478bd9Sstevel@tonic-gate 		if (cstatus != 0) {
24927c478bd9Sstevel@tonic-gate 			msg(EXTN, "close error");
24937c478bd9Sstevel@tonic-gate 		}
24947c478bd9Sstevel@tonic-gate 	}
24957c478bd9Sstevel@tonic-gate 
24967c478bd9Sstevel@tonic-gate 	pad = (Pad_val + 1 - (G_p->g_filesz & Pad_val)) & Pad_val;
24977c478bd9Sstevel@tonic-gate 	if (pad != 0) {
24987c478bd9Sstevel@tonic-gate 		FILL(pad);
24997c478bd9Sstevel@tonic-gate 		Buffr.b_out_p += pad;
25007c478bd9Sstevel@tonic-gate 		Buffr.b_cnt -= pad;
25017c478bd9Sstevel@tonic-gate 	}
25027c478bd9Sstevel@tonic-gate 	if (proc_mode != P_SKIP) {
2503ced83f9bSceastha 		if (Hdr_type == CRC &&
2504ced83f9bSceastha 		    Gen.g_cksum != data_in_info->data_in_cksumval) {
25057c478bd9Sstevel@tonic-gate 			msg(ERR, "\"%s\" - checksum error", nam_p);
25067c478bd9Sstevel@tonic-gate 			rstfiles(U_KEEP, G_p->g_dirfd);
25077c478bd9Sstevel@tonic-gate 		} else
25087c478bd9Sstevel@tonic-gate 			rstfiles(U_OVER, G_p->g_dirfd);
2509ced83f9bSceastha 		if (Hdr_type == BAR && data_in_info->data_in_compress_flag) {
2510ced83f9bSceastha 			(void) pclose(data_in_info->data_in_pipef);
25117c478bd9Sstevel@tonic-gate 		} else {
25127c478bd9Sstevel@tonic-gate 			cstatus = close(Ofile);
25137c478bd9Sstevel@tonic-gate 		}
25147c478bd9Sstevel@tonic-gate 		Ofile = 0;
25157c478bd9Sstevel@tonic-gate 		if (cstatus != 0) {
25167c478bd9Sstevel@tonic-gate 			msg(EXTN, "close error");
25177c478bd9Sstevel@tonic-gate 		}
25187c478bd9Sstevel@tonic-gate 	}
2519ced83f9bSceastha 	(void) free(data_in_info);
2520ced83f9bSceastha 
2521ced83f9bSceastha 	VERBOSE((proc_mode != P_SKIP && (Args & (OCv | OCV))),
2522ced83f9bSceastha 	    (G_p->g_attrparent_p == NULL) ? G_p->g_nam_p : G_p->g_attrpath_p);
25237c478bd9Sstevel@tonic-gate 	Finished = 1;
25247c478bd9Sstevel@tonic-gate }
25257c478bd9Sstevel@tonic-gate 
25267c478bd9Sstevel@tonic-gate /*
25277c478bd9Sstevel@tonic-gate  * data_out:  open(2) the file to be archived, compute the checksum
25287c478bd9Sstevel@tonic-gate  * of it's data if the CRC header was specified and write the header.
25297c478bd9Sstevel@tonic-gate  * read(2) each block of data and bwrite() it to the archive.  For TARTYP (TAR
25307c478bd9Sstevel@tonic-gate  * and USTAR) archives, pad the data with NULLs to the next 512 byte boundary.
25317c478bd9Sstevel@tonic-gate  */
25327c478bd9Sstevel@tonic-gate 
25337c478bd9Sstevel@tonic-gate static void
25347c478bd9Sstevel@tonic-gate data_out(void)
25357c478bd9Sstevel@tonic-gate {
25367c478bd9Sstevel@tonic-gate 	char *nam_p;
25377c478bd9Sstevel@tonic-gate 	int cnt, amount_read, pad;
25387c478bd9Sstevel@tonic-gate 	off_t amt_to_read, real_filesz;
25397c478bd9Sstevel@tonic-gate 	int	errret = 0;
25407c478bd9Sstevel@tonic-gate 
25417c478bd9Sstevel@tonic-gate 	nam_p = G_p->g_nam_p;
25427c478bd9Sstevel@tonic-gate 	if (Aspec) {
25437c478bd9Sstevel@tonic-gate 		if (Pflag && aclp != NULL) {
25447c478bd9Sstevel@tonic-gate 			char    *secinfo = NULL;
25457c478bd9Sstevel@tonic-gate 			int	len = 0;
25467c478bd9Sstevel@tonic-gate 
25477c478bd9Sstevel@tonic-gate 			/* append security attributes */
2548fa9e4066Sahrens 			if (append_secattr(&secinfo, &len, aclp) == -1) {
25497c478bd9Sstevel@tonic-gate 				msg(ERR,
25507c478bd9Sstevel@tonic-gate 				    "can create security information");
25517c478bd9Sstevel@tonic-gate 			}
25527c478bd9Sstevel@tonic-gate 			/* call append_secattr() if more than one */
25537c478bd9Sstevel@tonic-gate 
25547c478bd9Sstevel@tonic-gate 			if (len > 0) {
25557c478bd9Sstevel@tonic-gate 			/* write ancillary only if there is sec info */
25567c478bd9Sstevel@tonic-gate 				(void) write_hdr(ARCHIVE_ACL, (off_t)len);
25577c478bd9Sstevel@tonic-gate 				(void) write_ancillary(secinfo, len);
25587c478bd9Sstevel@tonic-gate 			}
25597c478bd9Sstevel@tonic-gate 		}
25607c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
25617c478bd9Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_dirfd);
25627c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
25637c478bd9Sstevel@tonic-gate 		return;
25647c478bd9Sstevel@tonic-gate 	}
25657c478bd9Sstevel@tonic-gate 	if ((G_p->g_mode & Ftype) == S_IFLNK && (Hdr_type !=
25667c478bd9Sstevel@tonic-gate 	    USTAR && Hdr_type != TAR)) { /* symbolic link */
25677c478bd9Sstevel@tonic-gate 		int size;
25687c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
25697c478bd9Sstevel@tonic-gate 
25707c478bd9Sstevel@tonic-gate 		FLUSH(G_p->g_filesz);
25717c478bd9Sstevel@tonic-gate 		errno = 0;
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate 		/* Note that "size" and G_p->g_filesz are the same number */
25747c478bd9Sstevel@tonic-gate 
25757c478bd9Sstevel@tonic-gate 		if ((size = readlink(nam_p, Buffr.b_in_p, G_p->g_filesz)) <
25767c478bd9Sstevel@tonic-gate 		    0) {
25777c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot read symbolic link \"%s\"", nam_p);
25787c478bd9Sstevel@tonic-gate 			return;
25797c478bd9Sstevel@tonic-gate 		}
25807c478bd9Sstevel@tonic-gate 
25817c478bd9Sstevel@tonic-gate 		/*
25827c478bd9Sstevel@tonic-gate 		 * Note that it is OK not to add the NUL after the name read by
25837c478bd9Sstevel@tonic-gate 		 * readlink, because it is not being used subsequently.
25847c478bd9Sstevel@tonic-gate 		 */
25857c478bd9Sstevel@tonic-gate 
25867c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += size;
25877c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += size;
25887c478bd9Sstevel@tonic-gate 		pad = (Pad_val + 1 - (size & Pad_val)) & Pad_val;
25897c478bd9Sstevel@tonic-gate 		if (pad != 0) {
25907c478bd9Sstevel@tonic-gate 			FLUSH(pad);
25917c478bd9Sstevel@tonic-gate 			(void) memcpy(Buffr.b_in_p, Empty, pad);
25927c478bd9Sstevel@tonic-gate 			Buffr.b_in_p += pad;
25937c478bd9Sstevel@tonic-gate 			Buffr.b_cnt += pad;
25947c478bd9Sstevel@tonic-gate 		}
25957c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
25967c478bd9Sstevel@tonic-gate 		return;
25977c478bd9Sstevel@tonic-gate 	} else if ((G_p->g_mode & Ftype) == S_IFLNK &&
25987c478bd9Sstevel@tonic-gate 	    (Hdr_type == USTAR || Hdr_type == TAR)) {
25997c478bd9Sstevel@tonic-gate 		int size;
26007c478bd9Sstevel@tonic-gate 
26017c478bd9Sstevel@tonic-gate 		/*
26027c478bd9Sstevel@tonic-gate 		 * G_p->g_filesz is the length of the right-hand side of
26037c478bd9Sstevel@tonic-gate 		 * the symlink "x -> y".
26047c478bd9Sstevel@tonic-gate 		 * The tar link field is only NAMSIZ long.
26057c478bd9Sstevel@tonic-gate 		 */
26067c478bd9Sstevel@tonic-gate 
26077c478bd9Sstevel@tonic-gate 		if (G_p->g_filesz > NAMSIZ) {
26087c478bd9Sstevel@tonic-gate 			msg(ERRN,
26097c478bd9Sstevel@tonic-gate 			    "Symbolic link too long \"%s\"", nam_p);
26107c478bd9Sstevel@tonic-gate 			return;
26117c478bd9Sstevel@tonic-gate 		}
26127c478bd9Sstevel@tonic-gate 		if ((size = readlink(nam_p, T_lname, G_p->g_filesz)) < 0) {
26137c478bd9Sstevel@tonic-gate 			msg(ERRN,
26147c478bd9Sstevel@tonic-gate 			    "Cannot read symbolic link \"%s\"", nam_p);
26157c478bd9Sstevel@tonic-gate 			return;
26167c478bd9Sstevel@tonic-gate 		}
26177c478bd9Sstevel@tonic-gate 		T_lname[size] = '\0';
26187c478bd9Sstevel@tonic-gate 		G_p->g_filesz = (off_t)0;
26197c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
26207c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
26217c478bd9Sstevel@tonic-gate 		return;
26227c478bd9Sstevel@tonic-gate 	}
26237c478bd9Sstevel@tonic-gate 	if ((Ifile = openfile(O_RDONLY)) < 0) {
26247c478bd9Sstevel@tonic-gate 		msg(ERR, "\"%s%s%s\" ?",
2625ced83f9bSceastha 		    (Gen.g_attrnam_p == NULL) ? nam_p : Gen.g_attrfnam_p,
2626ced83f9bSceastha 		    (Gen.g_attrnam_p == NULL) ? "" : Gen.g_rw_sysattr ?
2627ced83f9bSceastha 		    gettext(" System Attribute ") : gettext(" Attribute "),
2628ced83f9bSceastha 		    (Gen.g_attrnam_p == NULL) ? "" :
2629ced83f9bSceastha 		    (Gen.g_attrparent_p == NULL) ? Gen.g_attrnam_p :
2630ced83f9bSceastha 		    Gen.g_attrparent_p);
26317c478bd9Sstevel@tonic-gate 		return;
26327c478bd9Sstevel@tonic-gate 	}
26337c478bd9Sstevel@tonic-gate 
26347c478bd9Sstevel@tonic-gate 	/*
26357c478bd9Sstevel@tonic-gate 	 * Dump extended attribute header.
26367c478bd9Sstevel@tonic-gate 	 */
26377c478bd9Sstevel@tonic-gate 
2638ced83f9bSceastha 	if (Gen.g_attrnam_p != NULL) {
26397c478bd9Sstevel@tonic-gate 		write_xattr_hdr();
26407c478bd9Sstevel@tonic-gate 	}
26417c478bd9Sstevel@tonic-gate 
26427c478bd9Sstevel@tonic-gate 	if (Hdr_type == CRC) {
26437c478bd9Sstevel@tonic-gate 		long csum = cksum(CRC, 0, &errret);
26447c478bd9Sstevel@tonic-gate 		if (errret != 0) {
26457c478bd9Sstevel@tonic-gate 			G_p->g_cksum = (ulong_t)-1;
26467c478bd9Sstevel@tonic-gate 			msg(POST, "\"%s%s%s\" skipped",
2647ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ?
26487c478bd9Sstevel@tonic-gate 			    nam_p : Gen.g_attrfnam_p,
2649ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ? "" : Gen.g_rw_sysattr ?
2650ced83f9bSceastha 			    gettext(" System Attribute ") :
2651ced83f9bSceastha 			    gettext(" Attribute "),
2652ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ? "" : nam_p);
26537c478bd9Sstevel@tonic-gate 			(void) close(Ifile);
26547c478bd9Sstevel@tonic-gate 			return;
26557c478bd9Sstevel@tonic-gate 		}
26567c478bd9Sstevel@tonic-gate 		G_p->g_cksum = csum;
26577c478bd9Sstevel@tonic-gate 	} else {
26587c478bd9Sstevel@tonic-gate 		G_p->g_cksum = 0;
26597c478bd9Sstevel@tonic-gate 	}
26607c478bd9Sstevel@tonic-gate 
26617c478bd9Sstevel@tonic-gate 	/*
26627c478bd9Sstevel@tonic-gate 	 * ACL has been retrieved in getname().
26637c478bd9Sstevel@tonic-gate 	 */
26647c478bd9Sstevel@tonic-gate 	if (Pflag) {
26657c478bd9Sstevel@tonic-gate 		char    *secinfo = NULL;
26667c478bd9Sstevel@tonic-gate 		int	len = 0;
26677c478bd9Sstevel@tonic-gate 
26687c478bd9Sstevel@tonic-gate 		/* append security attributes */
2669fa9e4066Sahrens 		if ((append_secattr(&secinfo, &len, aclp)) == -1)
26707c478bd9Sstevel@tonic-gate 			msg(ERR, "can create security information");
26717c478bd9Sstevel@tonic-gate 
26727c478bd9Sstevel@tonic-gate 		/* call append_secattr() if more than one */
26737c478bd9Sstevel@tonic-gate 
26747c478bd9Sstevel@tonic-gate 		if (len > 0) {
26757c478bd9Sstevel@tonic-gate 		/* write ancillary only if there is sec info */
26767c478bd9Sstevel@tonic-gate 			(void) write_hdr(ARCHIVE_ACL, (off_t)len);
26777c478bd9Sstevel@tonic-gate 			(void) write_ancillary(secinfo, len);
26787c478bd9Sstevel@tonic-gate 		}
26797c478bd9Sstevel@tonic-gate 	}
26807c478bd9Sstevel@tonic-gate 
26817c478bd9Sstevel@tonic-gate 	write_hdr(ARCHIVE_NORMAL, (off_t)0);
26827c478bd9Sstevel@tonic-gate 
26837c478bd9Sstevel@tonic-gate 	real_filesz = 0;
26847c478bd9Sstevel@tonic-gate 
26857c478bd9Sstevel@tonic-gate 	for (amt_to_read = G_p->g_filesz;
26867c478bd9Sstevel@tonic-gate 	    amt_to_read > 0;
26877c478bd9Sstevel@tonic-gate 	    amt_to_read -= (off_t)amount_read) {
26887c478bd9Sstevel@tonic-gate 		FLUSH(CPIOBSZ);
26897c478bd9Sstevel@tonic-gate 		errno = 0;
26907c478bd9Sstevel@tonic-gate 
26917c478bd9Sstevel@tonic-gate 		if ((amount_read = read(Ifile, Buffr.b_in_p, CPIOBSZ)) < 0) {
26927c478bd9Sstevel@tonic-gate 			msg(EXTN, "Cannot read \"%s%s%s\"",
2693ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ?
26947c478bd9Sstevel@tonic-gate 			    nam_p : Gen.g_attrfnam_p,
2695ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ? "" : Gen.g_rw_sysattr ?
2696ced83f9bSceastha 			    gettext(" System Attribute ") :
2697ced83f9bSceastha 			    gettext(" Attribute "),
2698ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ? "" : nam_p);
26997c478bd9Sstevel@tonic-gate 			break;
27007c478bd9Sstevel@tonic-gate 		}
27017c478bd9Sstevel@tonic-gate 
27027c478bd9Sstevel@tonic-gate 		if (amount_read == 0) {
27037c478bd9Sstevel@tonic-gate 			/* the file has shrunk */
27047c478bd9Sstevel@tonic-gate 			real_filesz = G_p->g_filesz - amt_to_read;
27057c478bd9Sstevel@tonic-gate 			break;
27067c478bd9Sstevel@tonic-gate 		} else if (amount_read > amt_to_read) {
27077c478bd9Sstevel@tonic-gate 			/* the file has grown */
27087c478bd9Sstevel@tonic-gate 			real_filesz = G_p->g_filesz +
27097c478bd9Sstevel@tonic-gate 			    (amount_read - amt_to_read);
27107c478bd9Sstevel@tonic-gate 			amount_read = amt_to_read;
27117c478bd9Sstevel@tonic-gate 		} else if (amount_read == amt_to_read) {
27127c478bd9Sstevel@tonic-gate 			/* the file is the same size */
27137c478bd9Sstevel@tonic-gate 			real_filesz = G_p->g_filesz;
27147c478bd9Sstevel@tonic-gate 		}
27157c478bd9Sstevel@tonic-gate 
27167c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += amount_read;
27177c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += (long)amount_read;
27187c478bd9Sstevel@tonic-gate 	}
27197c478bd9Sstevel@tonic-gate 
27207c478bd9Sstevel@tonic-gate 	while (amt_to_read > 0) {
27217c478bd9Sstevel@tonic-gate 		cnt = (amt_to_read > CPIOBSZ) ? CPIOBSZ : (int)amt_to_read;
27227c478bd9Sstevel@tonic-gate 		FLUSH(cnt);
27237c478bd9Sstevel@tonic-gate 		(void) memset(Buffr.b_in_p, NULL, cnt);
27247c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += cnt;
27257c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += cnt;
27267c478bd9Sstevel@tonic-gate 		amt_to_read -= cnt;
27277c478bd9Sstevel@tonic-gate 	}
27287c478bd9Sstevel@tonic-gate 
27297c478bd9Sstevel@tonic-gate 	pad = (Pad_val + 1 - (G_p->g_filesz & Pad_val)) & Pad_val;
27307c478bd9Sstevel@tonic-gate 	if (pad != 0) {
27317c478bd9Sstevel@tonic-gate 		FLUSH(pad);
27327c478bd9Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, Empty, pad);
27337c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += pad;
27347c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += pad;
27357c478bd9Sstevel@tonic-gate 	}
27367c478bd9Sstevel@tonic-gate 
27377c478bd9Sstevel@tonic-gate 	if (real_filesz > G_p->g_filesz) {
27387c478bd9Sstevel@tonic-gate 		msg(ERR, "File size of \"%s%s%s\" has increased by %lld",
2739ced83f9bSceastha 		    (Gen.g_attrnam_p == NULL) ?
27407c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p : Gen.g_attrfnam_p,
2741ced83f9bSceastha 		    (Gen.g_attrnam_p == NULL) ? "" : Gen.g_rw_sysattr ?
2742ced83f9bSceastha 		    gettext(" System Attribute ") : gettext(" Attribute "),
2743ced83f9bSceastha 		    (Gen.g_attrnam_p == NULL) ? "" : G_p->g_nam_p,
27447c478bd9Sstevel@tonic-gate 		    (real_filesz - G_p->g_filesz));
27457c478bd9Sstevel@tonic-gate 	}
27467c478bd9Sstevel@tonic-gate 	if (real_filesz < G_p->g_filesz) {
27477c478bd9Sstevel@tonic-gate 		msg(ERR, "File size of \"%s%s%s\" has decreased by %lld",
2748ced83f9bSceastha 		    (Gen.g_attrnam_p == NULL) ?
27497c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p : Gen.g_attrfnam_p,
2750ced83f9bSceastha 		    (Gen.g_attrnam_p == NULL) ? "" : Gen.g_rw_sysattr ?
2751ced83f9bSceastha 		    gettext(" System Attribute ") : gettext(" Attribute "),
2752ced83f9bSceastha 		    (Gen.g_attrnam_p == NULL) ? "" : G_p->g_nam_p,
27537c478bd9Sstevel@tonic-gate 		    (G_p->g_filesz - real_filesz));
27547c478bd9Sstevel@tonic-gate 	}
27557c478bd9Sstevel@tonic-gate 
27567c478bd9Sstevel@tonic-gate 	(void) close(Ifile);
27577c478bd9Sstevel@tonic-gate 	rstfiles(U_KEEP, G_p->g_dirfd);
27587c478bd9Sstevel@tonic-gate 	VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
27597c478bd9Sstevel@tonic-gate }
27607c478bd9Sstevel@tonic-gate 
27617c478bd9Sstevel@tonic-gate /*
27627c478bd9Sstevel@tonic-gate  * data_pass:  If not a special file (Aspec), open(2) the file to be
27637c478bd9Sstevel@tonic-gate  * transferred, read(2) each block of data and write(2) it to the output file
27647c478bd9Sstevel@tonic-gate  * Ofile, which was opened in file_pass().
27657c478bd9Sstevel@tonic-gate  */
27667c478bd9Sstevel@tonic-gate 
27677c478bd9Sstevel@tonic-gate static void
27687c478bd9Sstevel@tonic-gate data_pass(void)
27697c478bd9Sstevel@tonic-gate {
2770ced83f9bSceastha 	int rv;
2771ced83f9bSceastha 	int done = 1;
27727c478bd9Sstevel@tonic-gate 	int cstatus;
27737c478bd9Sstevel@tonic-gate 	char *namep = Nam_p;
27747c478bd9Sstevel@tonic-gate 
2775ced83f9bSceastha 	if (G_p->g_attrnam_p != NULL) {
27767c478bd9Sstevel@tonic-gate 		namep = G_p->g_attrnam_p;
27777c478bd9Sstevel@tonic-gate 	}
27787c478bd9Sstevel@tonic-gate 	if (Aspec) {
27797c478bd9Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_passdirfd);
27807c478bd9Sstevel@tonic-gate 		cstatus = close(Ofile);
27817c478bd9Sstevel@tonic-gate 		Ofile = 0;
27827c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), Nam_p);
27837c478bd9Sstevel@tonic-gate 		if (cstatus != 0) {
27847c478bd9Sstevel@tonic-gate 			msg(EXTN, "close error");
27857c478bd9Sstevel@tonic-gate 		}
27867c478bd9Sstevel@tonic-gate 		return;
27877c478bd9Sstevel@tonic-gate 	}
27887c478bd9Sstevel@tonic-gate 	if ((Ifile = openat(G_p->g_dirfd, get_component(namep), 0)) < 0) {
27897c478bd9Sstevel@tonic-gate 		msg(ERRN, "Cannot open \"%s%s%s\", skipped",
2790ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? Nam_p : G_p->g_attrfnam_p,
2791ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? "" : G_p->g_rw_sysattr ?
2792ced83f9bSceastha 		    gettext(" System Attribute ") : gettext(" Attribute "),
2793ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? "" : G_p->g_attrnam_p);
27947c478bd9Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_passdirfd);
27957c478bd9Sstevel@tonic-gate 		cstatus = close(Ofile);
27967c478bd9Sstevel@tonic-gate 		Ofile = 0;
27977c478bd9Sstevel@tonic-gate 		if (cstatus != 0) {
27987c478bd9Sstevel@tonic-gate 			msg(EXTN, "close error");
27997c478bd9Sstevel@tonic-gate 		}
28007c478bd9Sstevel@tonic-gate 		return;
28017c478bd9Sstevel@tonic-gate 	}
28027c478bd9Sstevel@tonic-gate 
2803ced83f9bSceastha 	if ((rv = data_copy(Ifile, Ofile, (G_p->g_attrnam_p == NULL) ? 0 :
2804ced83f9bSceastha 	    G_p->g_rw_sysattr, G_p->g_filesz, Bufsize, NULL)) != 0) {
2805ced83f9bSceastha 		/* read error */
2806ced83f9bSceastha 		if (rv < 0) {
28077c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot read \"%s%s%s\"",
2808ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ?
28097c478bd9Sstevel@tonic-gate 			    Nam_p : G_p->g_attrfnam_p,
2810ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
2811ced83f9bSceastha 			    G_p->g_rw_sysattr ? gettext(" System Attribute ") :
2812ced83f9bSceastha 			    gettext(" Attribute "),
2813ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" : G_p->g_attrnam_p);
2814ced83f9bSceastha 		/* write error */
2815ced83f9bSceastha 		} else if (rv > 0) {
28167c478bd9Sstevel@tonic-gate 			if (Do_rename) {
28177c478bd9Sstevel@tonic-gate 				msg(ERRN, "Cannot write \"%s%s%s\"", Over_p,
2818ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ? "" :
2819ced83f9bSceastha 				    G_p->g_rw_sysattr ?
2820ced83f9bSceastha 				    gettext(" System Attribute ") :
2821ced83f9bSceastha 				    gettext(" Attribute "),
2822ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ? "" : Over_p);
28237c478bd9Sstevel@tonic-gate 			} else {
28247c478bd9Sstevel@tonic-gate 				msg(ERRN, "Cannot write \"%s%s%s\"",
28257c478bd9Sstevel@tonic-gate 				    Fullnam_p,
2826ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ? "" :
2827ced83f9bSceastha 				    G_p->g_rw_sysattr ?
2828ced83f9bSceastha 				    gettext(" System Attribute ") :
2829ced83f9bSceastha 				    gettext(" Attribute "),
2830ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ?
28317c478bd9Sstevel@tonic-gate 				    "" : G_p->g_attrnam_p);
28327c478bd9Sstevel@tonic-gate 			}
2833ced83f9bSceastha 		}
28347c478bd9Sstevel@tonic-gate 		done = 0;
28357c478bd9Sstevel@tonic-gate 	}
2836ced83f9bSceastha 
28377c478bd9Sstevel@tonic-gate 	if (done) {
28387c478bd9Sstevel@tonic-gate 		rstfiles(U_OVER, G_p->g_passdirfd);
28397c478bd9Sstevel@tonic-gate 	} else {
28407c478bd9Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_passdirfd);
28417c478bd9Sstevel@tonic-gate 	}
2842ced83f9bSceastha 
28437c478bd9Sstevel@tonic-gate 	(void) close(Ifile);
28447c478bd9Sstevel@tonic-gate 	cstatus = close(Ofile);
28457c478bd9Sstevel@tonic-gate 	Ofile = 0;
28467c478bd9Sstevel@tonic-gate 	if (cstatus != 0) {
28477c478bd9Sstevel@tonic-gate 		msg(EXTN, "close error");
28487c478bd9Sstevel@tonic-gate 	}
28497c478bd9Sstevel@tonic-gate 	VERBOSE((Args & (OCv | OCV)), Fullnam_p);
28507c478bd9Sstevel@tonic-gate 	Finished = 1;
28517c478bd9Sstevel@tonic-gate }
28527c478bd9Sstevel@tonic-gate 
28537c478bd9Sstevel@tonic-gate /*
28547c478bd9Sstevel@tonic-gate  * Allocation wrappers.  Used to centralize error handling for
28557c478bd9Sstevel@tonic-gate  * failed allocations.
28567c478bd9Sstevel@tonic-gate  */
28577c478bd9Sstevel@tonic-gate static void *
28587c478bd9Sstevel@tonic-gate e_alloc_fail(int flag)
28597c478bd9Sstevel@tonic-gate {
28607c478bd9Sstevel@tonic-gate 	if (flag == E_EXIT) {
28617c478bd9Sstevel@tonic-gate 		msg(EXTN, "Out of memory");
28627c478bd9Sstevel@tonic-gate 	}
28637c478bd9Sstevel@tonic-gate 
28647c478bd9Sstevel@tonic-gate 	return (NULL);
28657c478bd9Sstevel@tonic-gate }
28667c478bd9Sstevel@tonic-gate 
28677c478bd9Sstevel@tonic-gate /*
28687c478bd9Sstevel@tonic-gate  *  Note: unlike the other e_*lloc functions, e_realloc does not zero out the
28697c478bd9Sstevel@tonic-gate  *  additional memory it returns.  Ensure that you do not trust its contents
28707c478bd9Sstevel@tonic-gate  *  when you call it.
28717c478bd9Sstevel@tonic-gate  */
28727c478bd9Sstevel@tonic-gate 
28737c478bd9Sstevel@tonic-gate static void *
28747c478bd9Sstevel@tonic-gate e_realloc(int flag, void *old, size_t newsize)
28757c478bd9Sstevel@tonic-gate {
28767c478bd9Sstevel@tonic-gate 	void *ret = realloc(old, newsize);
28777c478bd9Sstevel@tonic-gate 
28787c478bd9Sstevel@tonic-gate 	if (ret == NULL) {
28797c478bd9Sstevel@tonic-gate 		return (e_alloc_fail(flag));
28807c478bd9Sstevel@tonic-gate 	}
28817c478bd9Sstevel@tonic-gate 
28827c478bd9Sstevel@tonic-gate 	return (ret);
28837c478bd9Sstevel@tonic-gate }
28847c478bd9Sstevel@tonic-gate 
28857c478bd9Sstevel@tonic-gate static char *
28867c478bd9Sstevel@tonic-gate e_strdup(int flag, const char *arg)
28877c478bd9Sstevel@tonic-gate {
28887c478bd9Sstevel@tonic-gate 	char *ret = strdup(arg);
28897c478bd9Sstevel@tonic-gate 
28907c478bd9Sstevel@tonic-gate 	if (ret == NULL) {
28917c478bd9Sstevel@tonic-gate 		return (e_alloc_fail(flag));
28927c478bd9Sstevel@tonic-gate 	}
28937c478bd9Sstevel@tonic-gate 
28947c478bd9Sstevel@tonic-gate 	return (ret);
28957c478bd9Sstevel@tonic-gate }
28967c478bd9Sstevel@tonic-gate 
28977c478bd9Sstevel@tonic-gate static void *
28987c478bd9Sstevel@tonic-gate e_valloc(int flag, size_t size)
28997c478bd9Sstevel@tonic-gate {
29007c478bd9Sstevel@tonic-gate 	void *ret = valloc(size);
29017c478bd9Sstevel@tonic-gate 
29027c478bd9Sstevel@tonic-gate 	if (ret == NULL) {
29037c478bd9Sstevel@tonic-gate 		return (e_alloc_fail(flag));
29047c478bd9Sstevel@tonic-gate 	}
29057c478bd9Sstevel@tonic-gate 
29067c478bd9Sstevel@tonic-gate 	return (ret);
29077c478bd9Sstevel@tonic-gate }
29087c478bd9Sstevel@tonic-gate 
29097c478bd9Sstevel@tonic-gate static void *
29107c478bd9Sstevel@tonic-gate e_zalloc(int flag, size_t size)
29117c478bd9Sstevel@tonic-gate {
29127c478bd9Sstevel@tonic-gate 	void *ret = malloc(size);
29137c478bd9Sstevel@tonic-gate 
29147c478bd9Sstevel@tonic-gate 	if (ret == NULL) {
29157c478bd9Sstevel@tonic-gate 		return (e_alloc_fail(flag));
29167c478bd9Sstevel@tonic-gate 	}
29177c478bd9Sstevel@tonic-gate 
29187c478bd9Sstevel@tonic-gate 	(void) memset(ret, 0, size);
29197c478bd9Sstevel@tonic-gate 	return (ret);
29207c478bd9Sstevel@tonic-gate }
29217c478bd9Sstevel@tonic-gate 
29227c478bd9Sstevel@tonic-gate /*
29237c478bd9Sstevel@tonic-gate  * file_in:  Process an object from the archive.  If a TARTYP (TAR or USTAR)
29247c478bd9Sstevel@tonic-gate  * archive and g_nlink == 1, link this file to the file name in t_linkname
29257c478bd9Sstevel@tonic-gate  * and return.  Handle linked files in one of two ways.  If Onecopy == 0, this
29267c478bd9Sstevel@tonic-gate  * is an old style (binary or -c) archive, create and extract the data for the
29277c478bd9Sstevel@tonic-gate  * first link found, link all subsequent links to this file and skip their data.
29287c478bd9Sstevel@tonic-gate  * If Oncecopy == 1, save links until all have been processed, and then
29297c478bd9Sstevel@tonic-gate  * process the links first to last checking their names against the patterns
29307c478bd9Sstevel@tonic-gate  * and/or asking the user to rename them.  The first link that is accepted
29317c478bd9Sstevel@tonic-gate  * for xtraction is created and the data is read from the archive.
29327c478bd9Sstevel@tonic-gate  * All subsequent links that are accepted are linked to this file.
29337c478bd9Sstevel@tonic-gate  */
29347c478bd9Sstevel@tonic-gate 
29357c478bd9Sstevel@tonic-gate static void
29367c478bd9Sstevel@tonic-gate file_in(void)
29377c478bd9Sstevel@tonic-gate {
29387c478bd9Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
29397c478bd9Sstevel@tonic-gate 	int lnkem = 0, cleanup = 0;
29407c478bd9Sstevel@tonic-gate 	int proc_file;
29417c478bd9Sstevel@tonic-gate 	struct Lnk *ttl_p;
29427c478bd9Sstevel@tonic-gate 	int typeflag;
29437c478bd9Sstevel@tonic-gate 	char savacl;
29447c478bd9Sstevel@tonic-gate 	int cwd;
29457c478bd9Sstevel@tonic-gate 
29467c478bd9Sstevel@tonic-gate 	G_p = &Gen;
29477c478bd9Sstevel@tonic-gate 
29487c478bd9Sstevel@tonic-gate 	/*
2949ced83f9bSceastha 	 * Now that we've read the extended header,
2950ced83f9bSceastha 	 * determine if we should restore attributes.
2951ced83f9bSceastha 	 * Don't restore the attribute if we are extracting
2952ced83f9bSceastha 	 * a file from an archive (as opposed to doing a table of
2953ced83f9bSceastha 	 * contents) and any of the following are true:
2954ced83f9bSceastha 	 * 1. neither -@ or -/ was specified.
2955ced83f9bSceastha 	 * 2. -@ was specified, -/ wasn't specified, and we're
2956ced83f9bSceastha 	 * processing a hidden attribute directory of an attribute
2957ced83f9bSceastha 	 * or we're processing a read-write system attribute file.
2958ced83f9bSceastha 	 * 3.  -@ wasn't specified, -/ was specified, and the file
2959ced83f9bSceastha 	 * we're processing it not a read-write system attribute file,
2960ced83f9bSceastha 	 * or we're processing the hidden attribute directory of an
2961ced83f9bSceastha 	 * attribute.
2962ced83f9bSceastha 	 *
2963ced83f9bSceastha 	 * We always process the attributes if we're just generating
2964ced83f9bSceastha 	 * generating a table of contents, or if both -@ and -/ were
2965ced83f9bSceastha 	 * specified.
2966ced83f9bSceastha 	 */
2967ced83f9bSceastha 	if (G_p->g_attrnam_p != NULL) {
2968ced83f9bSceastha 		if (((Args & OCt) == 0) &&
2969ced83f9bSceastha 		    ((!Atflag && !SysAtflag) ||
2970ced83f9bSceastha 		    (Atflag && !SysAtflag && ((G_p->g_attrparent_p != NULL) ||
2971ced83f9bSceastha 		    G_p->g_rw_sysattr)) ||
2972ced83f9bSceastha 		    (!Atflag && SysAtflag && ((G_p->g_attrparent_p != NULL) ||
2973ced83f9bSceastha 		    !G_p->g_rw_sysattr)))) {
2974ced83f9bSceastha 			proc_file = F_SKIP;
2975ced83f9bSceastha 			data_in(P_SKIP);
2976ced83f9bSceastha 			return;
2977ced83f9bSceastha 		}
2978ced83f9bSceastha 	}
2979ced83f9bSceastha 
2980ced83f9bSceastha 	/*
29817c478bd9Sstevel@tonic-gate 	 * Open target directory if this isn't a skipped file
29827c478bd9Sstevel@tonic-gate 	 * and g_nlink == 1
29837c478bd9Sstevel@tonic-gate 	 *
29847c478bd9Sstevel@tonic-gate 	 * Links are handled further down in this function.
29857c478bd9Sstevel@tonic-gate 	 */
29867c478bd9Sstevel@tonic-gate 
29877c478bd9Sstevel@tonic-gate 	proc_file = ckname(0);
29887c478bd9Sstevel@tonic-gate 
29897c478bd9Sstevel@tonic-gate 	if (proc_file == F_SKIP && G_p->g_nlink == 1) {
29907c478bd9Sstevel@tonic-gate 		/*
29917c478bd9Sstevel@tonic-gate 		 * Normally ckname() prints out the file as a side
29927c478bd9Sstevel@tonic-gate 		 * effect except for table of contents listing
29937c478bd9Sstevel@tonic-gate 		 * when its parameter is zero and Onecopy isn't
29947c478bd9Sstevel@tonic-gate 		 * Zero.  Due to this we need to force the name
29957c478bd9Sstevel@tonic-gate 		 * to be printed here.
29967c478bd9Sstevel@tonic-gate 		 */
29977c478bd9Sstevel@tonic-gate 		if (Onecopy == 1) {
29987c478bd9Sstevel@tonic-gate 			VERBOSE((Args & OCt), G_p->g_nam_p);
29997c478bd9Sstevel@tonic-gate 		}
30007c478bd9Sstevel@tonic-gate 		data_in(P_SKIP);
30017c478bd9Sstevel@tonic-gate 		return;
30027c478bd9Sstevel@tonic-gate 	}
30037c478bd9Sstevel@tonic-gate 
30047c478bd9Sstevel@tonic-gate 	if (proc_file != F_SKIP && open_dirfd() != 0) {
30057c478bd9Sstevel@tonic-gate 		data_in(P_SKIP);
30067c478bd9Sstevel@tonic-gate 		return;
30077c478bd9Sstevel@tonic-gate 	}
30087c478bd9Sstevel@tonic-gate 
30097c478bd9Sstevel@tonic-gate 	if (Hdr_type == BAR) {
30107c478bd9Sstevel@tonic-gate 		bar_file_in();
30117c478bd9Sstevel@tonic-gate 		close_dirfd();
30127c478bd9Sstevel@tonic-gate 		return;
30137c478bd9Sstevel@tonic-gate 	}
30147c478bd9Sstevel@tonic-gate 
30157c478bd9Sstevel@tonic-gate 	/*
30167c478bd9Sstevel@tonic-gate 	 * For archives in USTAR format, the files are extracted according
30177c478bd9Sstevel@tonic-gate 	 * to the typeflag.
30187c478bd9Sstevel@tonic-gate 	 */
30197c478bd9Sstevel@tonic-gate 	if (Hdr_type == USTAR || Hdr_type == TAR) {
30207c478bd9Sstevel@tonic-gate 		typeflag = Thdr_p->tbuf.t_typeflag;
30217c478bd9Sstevel@tonic-gate 		if (G_p->g_nlink == 1) {		/* hard link */
30227c478bd9Sstevel@tonic-gate 			if (proc_file != F_SKIP) {
30237c478bd9Sstevel@tonic-gate 				int i;
30247c478bd9Sstevel@tonic-gate 				char lname[NAMSIZ+1];
30257c478bd9Sstevel@tonic-gate 				(void) memset(lname, '\0', sizeof (lname));
30267c478bd9Sstevel@tonic-gate 
30277c478bd9Sstevel@tonic-gate 				(void) strncpy(lname, Thdr_p->tbuf.t_linkname,
30287c478bd9Sstevel@tonic-gate 				    NAMSIZ);
30297c478bd9Sstevel@tonic-gate 				for (i = 0; i <= NAMSIZ && lname[i] != 0; i++)
30307c478bd9Sstevel@tonic-gate 					;
30317c478bd9Sstevel@tonic-gate 
30327c478bd9Sstevel@tonic-gate 				lname[i] = 0;
30337c478bd9Sstevel@tonic-gate 				(void) creat_lnk(G_p->g_dirfd,
30347c478bd9Sstevel@tonic-gate 				    &lname[0], G_p->g_nam_p);
30357c478bd9Sstevel@tonic-gate 			}
30367c478bd9Sstevel@tonic-gate 			close_dirfd();
30377c478bd9Sstevel@tonic-gate 			return;
30387c478bd9Sstevel@tonic-gate 		}
30397c478bd9Sstevel@tonic-gate 		if (typeflag == '3' || typeflag == '4' || typeflag == '5' ||
30407c478bd9Sstevel@tonic-gate 		    typeflag == '6') {
30417c478bd9Sstevel@tonic-gate 			if (proc_file != F_SKIP &&
30427c478bd9Sstevel@tonic-gate 			    creat_spec(G_p->g_dirfd) > 0) {
3043ced83f9bSceastha 				VERBOSE((Args & (OCv | OCV)),
3044ced83f9bSceastha 				    (G_p->g_attrparent_p == NULL) ?
3045ced83f9bSceastha 				    G_p->g_nam_p : G_p->g_attrpath_p);
30467c478bd9Sstevel@tonic-gate 			}
30477c478bd9Sstevel@tonic-gate 			close_dirfd();
30487c478bd9Sstevel@tonic-gate 			return;
30497c478bd9Sstevel@tonic-gate 		} else if (Adir || Aspec) {
30507c478bd9Sstevel@tonic-gate 			if ((proc_file == F_SKIP) ||
30517c478bd9Sstevel@tonic-gate 			    (Ofile = openout(G_p->g_dirfd)) < 0) {
30527c478bd9Sstevel@tonic-gate 				data_in(P_SKIP);
30537c478bd9Sstevel@tonic-gate 			} else {
30547c478bd9Sstevel@tonic-gate 				data_in(P_PROC);
30557c478bd9Sstevel@tonic-gate 			}
30567c478bd9Sstevel@tonic-gate 			close_dirfd();
30577c478bd9Sstevel@tonic-gate 			return;
30587c478bd9Sstevel@tonic-gate 		}
30597c478bd9Sstevel@tonic-gate 	}
30607c478bd9Sstevel@tonic-gate 
30617c478bd9Sstevel@tonic-gate 	if (Adir) {
30627c478bd9Sstevel@tonic-gate 		if (proc_file != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
30637c478bd9Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
30647c478bd9Sstevel@tonic-gate 		}
30657c478bd9Sstevel@tonic-gate 		close_dirfd();
30667c478bd9Sstevel@tonic-gate 		if (Onecopy == 1) {
30677c478bd9Sstevel@tonic-gate 			VERBOSE((Args & OCt), G_p->g_nam_p);
30687c478bd9Sstevel@tonic-gate 		}
30697c478bd9Sstevel@tonic-gate 		return;
30707c478bd9Sstevel@tonic-gate 	}
30717c478bd9Sstevel@tonic-gate 	if (G_p->g_nlink == 1 || (Hdr_type == TAR ||
30727c478bd9Sstevel@tonic-gate 	    Hdr_type == USTAR)) {
30737c478bd9Sstevel@tonic-gate 		if (Aspec) {
30747c478bd9Sstevel@tonic-gate 			if (proc_file != F_SKIP && creat_spec(G_p->g_dirfd) > 0)
30757c478bd9Sstevel@tonic-gate 				VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
30767c478bd9Sstevel@tonic-gate 		} else {
30777c478bd9Sstevel@tonic-gate 			if ((proc_file == F_SKIP) ||
30787c478bd9Sstevel@tonic-gate 			    (Ofile = openout(G_p->g_dirfd)) < 0) {
30797c478bd9Sstevel@tonic-gate 				data_in(P_SKIP);
30807c478bd9Sstevel@tonic-gate 			} else {
30817c478bd9Sstevel@tonic-gate 				data_in(P_PROC);
30827c478bd9Sstevel@tonic-gate 			}
30837c478bd9Sstevel@tonic-gate 		}
30847c478bd9Sstevel@tonic-gate 		close_dirfd();
30857c478bd9Sstevel@tonic-gate 		return;
30867c478bd9Sstevel@tonic-gate 	}
30877c478bd9Sstevel@tonic-gate 	close_dirfd();
30887c478bd9Sstevel@tonic-gate 
30897c478bd9Sstevel@tonic-gate 	tl_p = add_lnk(&ttl_p);
30907c478bd9Sstevel@tonic-gate 	l_p = ttl_p;
30917c478bd9Sstevel@tonic-gate 	if (l_p->L_cnt == l_p->L_gen.g_nlink)
30927c478bd9Sstevel@tonic-gate 		cleanup = 1;
3093ced83f9bSceastha 	if (!Onecopy || G_p->g_attrnam_p != NULL) {
30947c478bd9Sstevel@tonic-gate 		lnkem = (tl_p != l_p) ? 1 : 0;
30957c478bd9Sstevel@tonic-gate 		G_p = &tl_p->L_gen;
30967c478bd9Sstevel@tonic-gate 		if (proc_file == F_SKIP) {
30977c478bd9Sstevel@tonic-gate 			data_in(P_SKIP);
30987c478bd9Sstevel@tonic-gate 		} else {
30997c478bd9Sstevel@tonic-gate 			if (open_dirfd() != 0)
31007c478bd9Sstevel@tonic-gate 				return;
31017c478bd9Sstevel@tonic-gate 			if (!lnkem) {
31027c478bd9Sstevel@tonic-gate 				if (Aspec) {
31037c478bd9Sstevel@tonic-gate 					if (creat_spec(G_p->g_dirfd) > 0)
31047c478bd9Sstevel@tonic-gate 						VERBOSE((Args & (OCv | OCV)),
31057c478bd9Sstevel@tonic-gate 						    G_p->g_nam_p);
31067c478bd9Sstevel@tonic-gate 				} else if ((Ofile =
31077c478bd9Sstevel@tonic-gate 				    openout(G_p->g_dirfd)) < 0) {
31087c478bd9Sstevel@tonic-gate 					data_in(P_SKIP);
31097c478bd9Sstevel@tonic-gate 					close_dirfd();
31107c478bd9Sstevel@tonic-gate 					reclaim(l_p);
31117c478bd9Sstevel@tonic-gate 				} else {
31127c478bd9Sstevel@tonic-gate 					data_in(P_PROC);
31137c478bd9Sstevel@tonic-gate 					close_dirfd();
31147c478bd9Sstevel@tonic-gate 				}
31157c478bd9Sstevel@tonic-gate 			} else {
31167c478bd9Sstevel@tonic-gate 				/*
31177c478bd9Sstevel@tonic-gate 				 * Are we linking an attribute?
31187c478bd9Sstevel@tonic-gate 				 */
31197c478bd9Sstevel@tonic-gate 				cwd = -1;
3120ced83f9bSceastha 				if (l_p->L_gen.g_attrnam_p != NULL) {
31217c478bd9Sstevel@tonic-gate 					(void) strcpy(Lnkend_p,
31227c478bd9Sstevel@tonic-gate 					    l_p->L_gen.g_attrnam_p);
31237c478bd9Sstevel@tonic-gate 					(void) strcpy(Full_p,
31247c478bd9Sstevel@tonic-gate 					    tl_p->L_gen.g_attrnam_p);
31257c478bd9Sstevel@tonic-gate 					cwd = save_cwd();
31267c478bd9Sstevel@tonic-gate 					(void) fchdir(G_p->g_dirfd);
31277c478bd9Sstevel@tonic-gate 				} else {
31287c478bd9Sstevel@tonic-gate 					(void) strcpy(Lnkend_p,
31297c478bd9Sstevel@tonic-gate 					    l_p->L_gen.g_nam_p);
31307c478bd9Sstevel@tonic-gate 					(void) strcpy(Full_p,
31317c478bd9Sstevel@tonic-gate 					    tl_p->L_gen.g_nam_p);
31327c478bd9Sstevel@tonic-gate 				}
31337c478bd9Sstevel@tonic-gate 				(void) creat_lnk(G_p->g_dirfd,
31347c478bd9Sstevel@tonic-gate 				    Lnkend_p, Full_p);
31357c478bd9Sstevel@tonic-gate 				data_in(P_SKIP);
31367c478bd9Sstevel@tonic-gate 				close_dirfd();
3137ced83f9bSceastha 				l_p->L_lnk_p = NULL;
31387c478bd9Sstevel@tonic-gate 				free(tl_p->L_gen.g_nam_p);
31397c478bd9Sstevel@tonic-gate 				free(tl_p);
31407c478bd9Sstevel@tonic-gate 				if (cwd != -1)
31417c478bd9Sstevel@tonic-gate 					rest_cwd(cwd);
31427c478bd9Sstevel@tonic-gate 			}
31437c478bd9Sstevel@tonic-gate 		}
31447c478bd9Sstevel@tonic-gate 	} else { /* Onecopy */
31457c478bd9Sstevel@tonic-gate 		if (tl_p->L_gen.g_filesz)
31467c478bd9Sstevel@tonic-gate 			cleanup = 1;
31477c478bd9Sstevel@tonic-gate 		if (!cleanup) {
31487c478bd9Sstevel@tonic-gate 			close_dirfd();
31497c478bd9Sstevel@tonic-gate 			return; /* don't do anything yet */
31507c478bd9Sstevel@tonic-gate 		}
31517c478bd9Sstevel@tonic-gate 		tl_p = l_p;
31527c478bd9Sstevel@tonic-gate 		/*
31537c478bd9Sstevel@tonic-gate 		 * ckname will clear aclchar. We need to keep aclchar for
31547c478bd9Sstevel@tonic-gate 		 * all links.
31557c478bd9Sstevel@tonic-gate 		 */
31567c478bd9Sstevel@tonic-gate 		savacl = aclchar;
3157ced83f9bSceastha 		while (tl_p != NULL) {
31587c478bd9Sstevel@tonic-gate 			G_p = &tl_p->L_gen;
31597c478bd9Sstevel@tonic-gate 			aclchar = savacl;
31607c478bd9Sstevel@tonic-gate 			if ((proc_file = ckname(1)) != F_SKIP) {
31617c478bd9Sstevel@tonic-gate 				if (open_dirfd() != 0) {
31627c478bd9Sstevel@tonic-gate 					return;
31637c478bd9Sstevel@tonic-gate 				}
31647c478bd9Sstevel@tonic-gate 				if (l_p->L_data) {
31657c478bd9Sstevel@tonic-gate 					(void) creat_lnk(G_p->g_dirfd,
31667c478bd9Sstevel@tonic-gate 					    l_p->L_gen.g_nam_p,
31677c478bd9Sstevel@tonic-gate 					    G_p->g_nam_p);
31687c478bd9Sstevel@tonic-gate 				} else if (Aspec) {
31697c478bd9Sstevel@tonic-gate 					(void) creat_spec(G_p->g_dirfd);
31707c478bd9Sstevel@tonic-gate 					l_p->L_data = 1;
31717c478bd9Sstevel@tonic-gate 					VERBOSE((Args & (OCv | OCV)),
31727c478bd9Sstevel@tonic-gate 					    G_p->g_nam_p);
31737c478bd9Sstevel@tonic-gate 				} else if ((Ofile =
31747c478bd9Sstevel@tonic-gate 				    openout(G_p->g_dirfd)) < 0) {
31757c478bd9Sstevel@tonic-gate 					proc_file = F_SKIP;
31767c478bd9Sstevel@tonic-gate 				} else {
31777c478bd9Sstevel@tonic-gate 					data_in(P_PROC);
31787c478bd9Sstevel@tonic-gate 					l_p->L_data = 1;
31797c478bd9Sstevel@tonic-gate 				}
31807c478bd9Sstevel@tonic-gate 			} /* (proc_file = ckname(1)) != F_SKIP */
31817c478bd9Sstevel@tonic-gate 
31827c478bd9Sstevel@tonic-gate 			tl_p = tl_p->L_lnk_p;
31837c478bd9Sstevel@tonic-gate 
31847c478bd9Sstevel@tonic-gate 			close_dirfd();
31857c478bd9Sstevel@tonic-gate 
31867c478bd9Sstevel@tonic-gate 			if (proc_file == F_SKIP && !cleanup) {
31877c478bd9Sstevel@tonic-gate 				tl_p->L_nxt_p = l_p->L_nxt_p;
31887c478bd9Sstevel@tonic-gate 				tl_p->L_bck_p = l_p->L_bck_p;
31897c478bd9Sstevel@tonic-gate 				l_p->L_bck_p->L_nxt_p = tl_p;
31907c478bd9Sstevel@tonic-gate 				l_p->L_nxt_p->L_bck_p = tl_p;
31917c478bd9Sstevel@tonic-gate 				free(l_p->L_gen.g_nam_p);
31927c478bd9Sstevel@tonic-gate 				free(l_p);
31937c478bd9Sstevel@tonic-gate 			}
3194ced83f9bSceastha 		} /* tl_p->L_lnk_p != NULL */
31957c478bd9Sstevel@tonic-gate 		if (l_p->L_data == 0) {
31967c478bd9Sstevel@tonic-gate 			data_in(P_SKIP);
31977c478bd9Sstevel@tonic-gate 		}
31987c478bd9Sstevel@tonic-gate 	}
31997c478bd9Sstevel@tonic-gate 	if (cleanup) {
32007c478bd9Sstevel@tonic-gate 		reclaim(l_p);
32017c478bd9Sstevel@tonic-gate 	}
32027c478bd9Sstevel@tonic-gate }
32037c478bd9Sstevel@tonic-gate 
32047c478bd9Sstevel@tonic-gate /*
32057c478bd9Sstevel@tonic-gate  * file_out:  If the current file is not a special file (!Aspec) and it
32067c478bd9Sstevel@tonic-gate  * is identical to the archive, skip it (do not archive the archive if it
32077c478bd9Sstevel@tonic-gate  * is a regular file).  If creating a TARTYP (TAR or USTAR) archive, the first
32087c478bd9Sstevel@tonic-gate  * time a link to a file is encountered, write the header and file out normally.
32097c478bd9Sstevel@tonic-gate  * Subsequent links to this file put this file name in their t_linkname field.
32107c478bd9Sstevel@tonic-gate  * Otherwise, links are handled in one of two ways, for the old headers
32117c478bd9Sstevel@tonic-gate  * (i.e. binary and -c), linked files are written out as they are encountered.
32127c478bd9Sstevel@tonic-gate  * For the new headers (ASC and CRC), links are saved up until all the links
32137c478bd9Sstevel@tonic-gate  * to each file are found.  For a file with n links, write n - 1 headers with
32147c478bd9Sstevel@tonic-gate  * g_filesz set to 0, write the final (nth) header with the correct g_filesz
32157c478bd9Sstevel@tonic-gate  * value and write the data for the file to the archive.
32167c478bd9Sstevel@tonic-gate  */
32177c478bd9Sstevel@tonic-gate 
32187c478bd9Sstevel@tonic-gate static
32197c478bd9Sstevel@tonic-gate int
32207c478bd9Sstevel@tonic-gate file_out(void)
32217c478bd9Sstevel@tonic-gate {
32227c478bd9Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
32237c478bd9Sstevel@tonic-gate 	int cleanup = 0;
32247c478bd9Sstevel@tonic-gate 	struct Lnk *ttl_p;
32257c478bd9Sstevel@tonic-gate 
32267c478bd9Sstevel@tonic-gate 	G_p = &Gen;
32277c478bd9Sstevel@tonic-gate 	if (!Aspec && IDENT(SrcSt, ArchSt))
32287c478bd9Sstevel@tonic-gate 		return (1); /* do not archive the archive if it's a reg file */
32297c478bd9Sstevel@tonic-gate 	if (G_p->g_filesz > Max_offset) {
32307c478bd9Sstevel@tonic-gate 		msg(ERR, "cpio: %s%s%s: too large to archive in current mode",
32317c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p,
3232ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? "" : G_p->g_rw_sysattr ?
3233ced83f9bSceastha 		    gettext(" System Attribute ") : gettext(" Attribute "),
3234ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? "" :
3235ced83f9bSceastha 		    ((G_p->g_attrparent_p == NULL) ? G_p->g_attrnam_p:
3236ced83f9bSceastha 		    G_p->g_attrpath_p));
32377c478bd9Sstevel@tonic-gate 		return (1); /* do not archive if it's too big */
32387c478bd9Sstevel@tonic-gate 	}
32397c478bd9Sstevel@tonic-gate 	if (Hdr_type == TAR || Hdr_type == USTAR) { /* TAR and USTAR */
32407c478bd9Sstevel@tonic-gate 		if (Adir) {
3241ced83f9bSceastha 			if (Gen.g_attrnam_p != NULL) {
32427c478bd9Sstevel@tonic-gate 				write_xattr_hdr();
32437c478bd9Sstevel@tonic-gate 			}
32447c478bd9Sstevel@tonic-gate 			write_hdr(ARCHIVE_NORMAL, 0);
32457c478bd9Sstevel@tonic-gate 			return (0);
32467c478bd9Sstevel@tonic-gate 		}
32477c478bd9Sstevel@tonic-gate 		if (G_p->g_nlink == 1) {
32487c478bd9Sstevel@tonic-gate 			data_out();
32497c478bd9Sstevel@tonic-gate 			return (0);
32507c478bd9Sstevel@tonic-gate 		}
32517c478bd9Sstevel@tonic-gate 		tl_p = add_lnk(&ttl_p);
32527c478bd9Sstevel@tonic-gate 		l_p = ttl_p;
32537c478bd9Sstevel@tonic-gate 		if (tl_p == l_p) { /* first link to this file encountered */
32547c478bd9Sstevel@tonic-gate 			data_out();
32557c478bd9Sstevel@tonic-gate 			return (0);
32567c478bd9Sstevel@tonic-gate 		}
32577c478bd9Sstevel@tonic-gate 		(void) strncpy(T_lname, l_p->L_gen.g_nam_p,
32587c478bd9Sstevel@tonic-gate 		    l_p->L_gen.g_namesz);
32597c478bd9Sstevel@tonic-gate 
32607c478bd9Sstevel@tonic-gate 		/*
32617c478bd9Sstevel@tonic-gate 		 * check if linkname is greater than 100 characters
32627c478bd9Sstevel@tonic-gate 		 */
32637c478bd9Sstevel@tonic-gate 		if (strlen(T_lname) > NAMSIZ) {
32647c478bd9Sstevel@tonic-gate 			msg(EPOST, "cpio: %s: linkname %s is greater than %d",
32657c478bd9Sstevel@tonic-gate 			    G_p->g_nam_p, T_lname, NAMSIZ);
32667c478bd9Sstevel@tonic-gate 			return (1);
32677c478bd9Sstevel@tonic-gate 		}
32687c478bd9Sstevel@tonic-gate 
32697c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
32707c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), tl_p->L_gen.g_nam_p);
32717c478bd9Sstevel@tonic-gate 
32727c478bd9Sstevel@tonic-gate 		/* find the lnk entry in sublist, unlink it, and free it */
32737c478bd9Sstevel@tonic-gate 		for (; ttl_p->L_lnk_p != NULL;
32747c478bd9Sstevel@tonic-gate 		    ttl_p = ttl_p->L_lnk_p) {
32757c478bd9Sstevel@tonic-gate 			if (ttl_p->L_lnk_p == tl_p) {
32767c478bd9Sstevel@tonic-gate 				ttl_p->L_lnk_p = tl_p->L_lnk_p;
32777c478bd9Sstevel@tonic-gate 				free(tl_p->L_gen.g_nam_p);
32787c478bd9Sstevel@tonic-gate 				free(tl_p);
32797c478bd9Sstevel@tonic-gate 				break;
32807c478bd9Sstevel@tonic-gate 			}
32817c478bd9Sstevel@tonic-gate 		}
32827c478bd9Sstevel@tonic-gate 
32837c478bd9Sstevel@tonic-gate 		return (0);
32847c478bd9Sstevel@tonic-gate 	}
32857c478bd9Sstevel@tonic-gate 	if (Adir) {
32867c478bd9Sstevel@tonic-gate 		/*
32877c478bd9Sstevel@tonic-gate 		 * ACL has been retrieved in getname().
32887c478bd9Sstevel@tonic-gate 		 */
32897c478bd9Sstevel@tonic-gate 		if (Pflag) {
32907c478bd9Sstevel@tonic-gate 			char    *secinfo = NULL;
32917c478bd9Sstevel@tonic-gate 			int	len = 0;
32927c478bd9Sstevel@tonic-gate 
32937c478bd9Sstevel@tonic-gate 			/* append security attributes */
3294fa9e4066Sahrens 			if ((append_secattr(&secinfo, &len, aclp)) == -1)
32957c478bd9Sstevel@tonic-gate 				msg(ERR, "can create security information");
32967c478bd9Sstevel@tonic-gate 
32977c478bd9Sstevel@tonic-gate 			/* call append_secattr() if more than one */
32987c478bd9Sstevel@tonic-gate 
32997c478bd9Sstevel@tonic-gate 			if (len > 0) {
33007c478bd9Sstevel@tonic-gate 			/* write ancillary */
33017c478bd9Sstevel@tonic-gate 				(void) write_hdr(ARCHIVE_ACL, (off_t)len);
33027c478bd9Sstevel@tonic-gate 				(void) write_ancillary(secinfo, len);
33037c478bd9Sstevel@tonic-gate 			}
33047c478bd9Sstevel@tonic-gate 		}
33057c478bd9Sstevel@tonic-gate 
3306ced83f9bSceastha 		if (Gen.g_attrnam_p != NULL) {
33077c478bd9Sstevel@tonic-gate 			write_xattr_hdr();
33087c478bd9Sstevel@tonic-gate 		}
33097c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
33107c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
33117c478bd9Sstevel@tonic-gate 		return (0);
33127c478bd9Sstevel@tonic-gate 	}
33137c478bd9Sstevel@tonic-gate 	if (G_p->g_nlink == 1) {
33147c478bd9Sstevel@tonic-gate 		data_out();
33157c478bd9Sstevel@tonic-gate 		return (0);
33167c478bd9Sstevel@tonic-gate 	} else {
33177c478bd9Sstevel@tonic-gate 		tl_p = add_lnk(&ttl_p);
33187c478bd9Sstevel@tonic-gate 		l_p = ttl_p;
33197c478bd9Sstevel@tonic-gate 
33207c478bd9Sstevel@tonic-gate 		if (l_p->L_cnt == l_p->L_gen.g_nlink)
33217c478bd9Sstevel@tonic-gate 			cleanup = 1;
3322ced83f9bSceastha 		else if (Onecopy && G_p->g_attrnam_p == NULL) {
33237c478bd9Sstevel@tonic-gate 			return (0); /* don't process data yet */
33247c478bd9Sstevel@tonic-gate 		}
33257c478bd9Sstevel@tonic-gate 	}
3326ced83f9bSceastha 	if (Onecopy && G_p->g_attrnam_p == NULL) {
33277c478bd9Sstevel@tonic-gate 		tl_p = l_p;
3328ced83f9bSceastha 		while (tl_p->L_lnk_p != NULL) {
33297c478bd9Sstevel@tonic-gate 			G_p = &tl_p->L_gen;
33307c478bd9Sstevel@tonic-gate 			G_p->g_filesz = (off_t)0;
33317c478bd9Sstevel@tonic-gate 			/* one link with the acl is sufficient */
33327c478bd9Sstevel@tonic-gate 			write_hdr(ARCHIVE_NORMAL, (off_t)0);
33337c478bd9Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
33347c478bd9Sstevel@tonic-gate 			tl_p = tl_p->L_lnk_p;
33357c478bd9Sstevel@tonic-gate 		}
33367c478bd9Sstevel@tonic-gate 		G_p = &tl_p->L_gen;
33377c478bd9Sstevel@tonic-gate 		if (open_dirfd() != 0)
33387c478bd9Sstevel@tonic-gate 			return (1);
33397c478bd9Sstevel@tonic-gate 	}
33407c478bd9Sstevel@tonic-gate 	/* old style: has acl and data for every link */
33417c478bd9Sstevel@tonic-gate 	data_out();
33427c478bd9Sstevel@tonic-gate 	if (cleanup)
33437c478bd9Sstevel@tonic-gate 		reclaim(l_p);
33447c478bd9Sstevel@tonic-gate 	return (0);
33457c478bd9Sstevel@tonic-gate }
33467c478bd9Sstevel@tonic-gate 
33477c478bd9Sstevel@tonic-gate /*
3348ced83f9bSceastha  * Verify the underlying file system supports the attribute type.
3349ced83f9bSceastha  * Only archive extended attribute files when '-@' was specified.
3350ced83f9bSceastha  * Only archive system extended attribute files if '-/' was specified.
3351ced83f9bSceastha  */
3352ced83f9bSceastha #if defined(O_XATTR)
3353ced83f9bSceastha static attr_status_t
3354ced83f9bSceastha verify_attr_support(char *filename, int attrflg, arc_action_t actflag,
3355ced83f9bSceastha     int *ext_attrflg)
3356ced83f9bSceastha {
3357ced83f9bSceastha 	/*
3358ced83f9bSceastha 	 * Verify extended attributes are supported/exist.  We only
3359ced83f9bSceastha 	 * need to check if we are processing a base file, not an
3360ced83f9bSceastha 	 * extended attribute.
3361ced83f9bSceastha 	 */
3362ced83f9bSceastha 	if (attrflg) {
3363ced83f9bSceastha 		*ext_attrflg = (pathconf(filename, (actflag == ARC_CREATE) ?
3364ced83f9bSceastha 		    _PC_XATTR_EXISTS : _PC_XATTR_ENABLED) == 1);
3365ced83f9bSceastha 	}
3366ced83f9bSceastha 	if (Atflag) {
3367ced83f9bSceastha #if defined(_PC_SATTR_ENABLED)
3368ced83f9bSceastha 		if (!*ext_attrflg) {
3369ced83f9bSceastha 			if (SysAtflag) {
3370ced83f9bSceastha 				/* Verify system attributes are supported */
3371ced83f9bSceastha 				if (sysattr_support(filename,
3372ced83f9bSceastha 				    (actflag == ARC_CREATE) ?_PC_SATTR_EXISTS :
3373ced83f9bSceastha 				    _PC_SATTR_ENABLED) != 1) {
3374ced83f9bSceastha 					return (ATTR_SATTR_ERR);
3375ced83f9bSceastha 				}
3376ced83f9bSceastha 			} else
3377ced83f9bSceastha 				return (ATTR_XATTR_ERR);
3378ced83f9bSceastha #else
3379ced83f9bSceastha 				return (ATTR_XATTR_ERR);
3380ced83f9bSceastha #endif  /* _PC_SATTR_ENABLED */
3381ced83f9bSceastha 		}
3382ced83f9bSceastha 
3383ced83f9bSceastha #if defined(_PC_SATTR_ENABLED)
3384ced83f9bSceastha 	} else if (SysAtflag) {
3385ced83f9bSceastha 		/* Verify system attributes are supported */
3386ced83f9bSceastha 		if (sysattr_support(filename, (actflag == ARC_CREATE) ?
3387ced83f9bSceastha 		    _PC_SATTR_EXISTS : _PC_SATTR_ENABLED) != 1) {
3388ced83f9bSceastha 			return (ATTR_SATTR_ERR);
3389ced83f9bSceastha 	}
3390ced83f9bSceastha #endif  /* _PC_SATTR_ENABLED */
3391ced83f9bSceastha 	} else {
3392ced83f9bSceastha 		return (ATTR_SKIP);
3393ced83f9bSceastha 	}
3394ced83f9bSceastha 
3395ced83f9bSceastha return (ATTR_OK);
3396ced83f9bSceastha }
3397ced83f9bSceastha #endif
3398ced83f9bSceastha 
3399ced83f9bSceastha #if defined(O_XATTR)
3400ced83f9bSceastha /*
3401ced83f9bSceastha  * Verify the attribute, attrname, is an attribute we want to restore.
3402ced83f9bSceastha  * Never restore read-only system attribute files.  Only restore read-write
3403ced83f9bSceastha  * system attributes files when -/ was specified, and only traverse into
3404ced83f9bSceastha  * the 2nd level attribute directory containing only system attributes if
3405ced83f9bSceastha  * -@ was specified.  This keeps us from archiving
3406ced83f9bSceastha  *	<attribute name>/<read-write system attribute file>
3407ced83f9bSceastha  * when -/ was specified without -@.
3408ced83f9bSceastha  *
3409ced83f9bSceastha  * attrname		- attribute file name
3410ced83f9bSceastha  * attrparent		- attribute's parent name within the base file's
3411ced83f9bSceastha  *			attribute digrectory hierarchy
3412ced83f9bSceastha  * arc_rwsysattr	- flag that indicates that read-write system attribute
3413ced83f9bSceastha  *			file should be archived as it contains other than
3414ced83f9bSceastha  *			the default system attributes.
3415ced83f9bSceastha  * rw_sysattr		- on return, flag will indicate if attrname is a
3416ced83f9bSceastha  *			read-write system attribute file.
3417ced83f9bSceastha  */
3418ced83f9bSceastha static attr_status_t
3419ced83f9bSceastha verify_attr(char *attrname, char *attrparent, int arc_rwsysattr,
3420ced83f9bSceastha     int *rw_sysattr)
3421ced83f9bSceastha {
3422ced83f9bSceastha #if defined(_PC_SATTR_ENABLED)
3423ced83f9bSceastha 	int	attr_supported;
3424ced83f9bSceastha 
3425ced83f9bSceastha 	/* Never restore read-only system attribute files */
3426ced83f9bSceastha 	if ((attr_supported = sysattr_type(attrname)) == _RO_SATTR) {
3427ced83f9bSceastha 		*rw_sysattr = 0;
3428ced83f9bSceastha 		return (ATTR_SKIP);
3429ced83f9bSceastha 	} else {
3430ced83f9bSceastha 		*rw_sysattr = (attr_supported == _RW_SATTR);
3431ced83f9bSceastha 	}
3432ced83f9bSceastha 
3433ced83f9bSceastha 	/*
3434ced83f9bSceastha 	 * Don't archive a read-write system attribute file if
3435ced83f9bSceastha 	 * it contains only the default system attributes.
3436ced83f9bSceastha 	 */
3437ced83f9bSceastha 	if (*rw_sysattr && !arc_rwsysattr) {
3438ced83f9bSceastha 		return (ATTR_SKIP);
3439ced83f9bSceastha 	}
3440ced83f9bSceastha 
3441ced83f9bSceastha #else
3442ced83f9bSceastha 	/* Never restore read-only system attribute files */
3443ced83f9bSceastha 	if ((*rw_sysattr = is_sysattr(attrname)) == 1) {
3444ced83f9bSceastha 		return (ATTR_SKIP);
3445ced83f9bSceastha 	}
3446ced83f9bSceastha #endif	/* _PC_SATTR_ENABLED */
3447ced83f9bSceastha 
3448ced83f9bSceastha 	/*
3449ced83f9bSceastha 	 * Only restore read-write system attribute files
3450ced83f9bSceastha 	 * when -/ was specified.  Only restore extended
3451ced83f9bSceastha 	 * attributes when -@ was specified.
3452ced83f9bSceastha 	 */
3453ced83f9bSceastha 	if (Atflag) {
3454ced83f9bSceastha 		if (!SysAtflag) {
3455ced83f9bSceastha 			/*
3456ced83f9bSceastha 			 * Only archive/restore the hidden directory "." if
3457ced83f9bSceastha 			 * we're processing the top level hidden attribute
3458ced83f9bSceastha 			 * directory.  We don't want to process the
3459ced83f9bSceastha 			 * hidden attribute directory of the attribute
3460ced83f9bSceastha 			 * directory that contains only extended system
3461ced83f9bSceastha 			 * attributes.
3462ced83f9bSceastha 			 */
3463ced83f9bSceastha 			if (*rw_sysattr || (Hiddendir &&
3464ced83f9bSceastha 			    (attrparent != NULL))) {
3465ced83f9bSceastha 				return (ATTR_SKIP);
3466ced83f9bSceastha 			}
3467ced83f9bSceastha 		}
3468ced83f9bSceastha 	} else if (SysAtflag) {
3469ced83f9bSceastha 		/*
3470ced83f9bSceastha 		 * Only archive/restore read-write extended system attribute
3471ced83f9bSceastha 		 * files of the base file.
3472ced83f9bSceastha 		 */
3473ced83f9bSceastha 		if (!*rw_sysattr || (attrparent != NULL)) {
3474ced83f9bSceastha 			return (ATTR_SKIP);
3475ced83f9bSceastha 		}
3476ced83f9bSceastha 	} else {
3477ced83f9bSceastha 		return (ATTR_SKIP);
3478ced83f9bSceastha 	}
3479ced83f9bSceastha 
3480ced83f9bSceastha 	return (ATTR_OK);
3481ced83f9bSceastha }
3482ced83f9bSceastha #endif
3483ced83f9bSceastha 
3484ced83f9bSceastha #if defined(O_XATTR)
3485ced83f9bSceastha static int
3486ced83f9bSceastha retry_open_attr(int pdirfd, int cwd, char *fullname, char *pattr, char *name,
3487ced83f9bSceastha     int oflag, mode_t mode)
3488ced83f9bSceastha {
3489ced83f9bSceastha 	int dirfd;
3490ced83f9bSceastha 	int ofilefd = -1;
3491ced83f9bSceastha 	struct timeval times[2];
3492ced83f9bSceastha 	mode_t newmode;
3493ced83f9bSceastha 	struct stat parentstat;
3494ced83f9bSceastha 	acl_t *aclp = NULL;
3495ced83f9bSceastha 	int error;
3496ced83f9bSceastha 
3497ced83f9bSceastha 	/*
3498ced83f9bSceastha 	 * We couldn't get to attrdir. See if its
3499ced83f9bSceastha 	 * just a mode problem on the parent file.
3500ced83f9bSceastha 	 * for example: a mode such as r-xr--r--
3501ced83f9bSceastha 	 * on a ufs file system without extended
3502ced83f9bSceastha 	 * system attribute support won't let us
3503ced83f9bSceastha 	 * create an attribute dir if it doesn't
3504ced83f9bSceastha 	 * already exist, and on a ufs file system
3505ced83f9bSceastha 	 * with extended system attribute support
3506ced83f9bSceastha 	 * won't let us open the attribute for
3507ced83f9bSceastha 	 * write.
3508ced83f9bSceastha 	 *
3509ced83f9bSceastha 	 * If file has a non-trivial ACL, then save it
3510ced83f9bSceastha 	 * off so that we can place it back on after doing
3511ced83f9bSceastha 	 * chmod's.
3512ced83f9bSceastha 	 */
3513ced83f9bSceastha 	if ((dirfd = openat(cwd, (pattr == NULL) ? fullname : pattr,
3514ced83f9bSceastha 	    O_RDONLY)) == -1) {
3515ced83f9bSceastha 		return (-1);
3516ced83f9bSceastha 	}
3517ced83f9bSceastha 	if (fstat(dirfd, &parentstat) == -1) {
3518ced83f9bSceastha 		msg(ERRN, "Cannot stat %sfile %s",
3519ced83f9bSceastha 		    (pdirfd == -1) ? "" : gettext("parent of "),
3520ced83f9bSceastha 		    (pdirfd == -1) ? fullname : name);
3521ced83f9bSceastha 		(void) close(dirfd);
3522ced83f9bSceastha 		return (-1);
3523ced83f9bSceastha 	}
3524ced83f9bSceastha 	if ((error = facl_get(dirfd, ACL_NO_TRIVIAL, &aclp)) != 0) {
3525ced83f9bSceastha 		msg(ERRN, "Failed to retrieve ACL on %sfile %s",
3526ced83f9bSceastha 		    (pdirfd == -1) ? "" : gettext("parent of "),
3527ced83f9bSceastha 		    (pdirfd == -1) ? fullname : name);
3528ced83f9bSceastha 		(void) close(dirfd);
3529ced83f9bSceastha 		return (-1);
3530ced83f9bSceastha 	}
3531ced83f9bSceastha 
3532ced83f9bSceastha 	newmode = S_IWUSR | parentstat.st_mode;
3533ced83f9bSceastha 	if (fchmod(dirfd, newmode) == -1) {
3534ced83f9bSceastha 		msg(ERRN, "Cannot change mode of %sfile %s to %o",
3535ced83f9bSceastha 		    (pdirfd == -1) ? "" : gettext("parent of "),
3536ced83f9bSceastha 		    (pdirfd == -1) ? fullname : name, newmode);
3537ced83f9bSceastha 		if (aclp)
3538ced83f9bSceastha 			acl_free(aclp);
3539ced83f9bSceastha 		(void) close(dirfd);
3540ced83f9bSceastha 		return (-1);
3541ced83f9bSceastha 	}
3542ced83f9bSceastha 
3543ced83f9bSceastha 
3544ced83f9bSceastha 	if (pdirfd == -1) {
3545ced83f9bSceastha 		/*
3546ced83f9bSceastha 		 * We weren't able to create the attribute directory before.
3547ced83f9bSceastha 		 * Now try again.
3548ced83f9bSceastha 		 */
3549ced83f9bSceastha 		ofilefd = attropen(fullname, ".", oflag);
3550ced83f9bSceastha 	} else {
3551ced83f9bSceastha 		/*
3552ced83f9bSceastha 		 * We weren't able to create open the attribute before.
3553ced83f9bSceastha 		 * Now try again.
3554ced83f9bSceastha 		 */
3555ced83f9bSceastha 		ofilefd = openat(pdirfd, name, oflag, mode);
3556ced83f9bSceastha 	}
3557ced83f9bSceastha 
3558ced83f9bSceastha 	/*
3559ced83f9bSceastha 	 * Put mode back to original
3560ced83f9bSceastha 	 */
3561ced83f9bSceastha 	if (fchmod(dirfd, parentstat.st_mode) == -1) {
3562ced83f9bSceastha 		msg(ERRN, "Cannot restore permissions of %sfile %s to %o",
3563ced83f9bSceastha 		    (pdirfd == -1) ? "" : gettext("parent of "),
3564ced83f9bSceastha 		    (pdirfd == -1) ? fullname : name, newmode);
3565ced83f9bSceastha 	}
3566ced83f9bSceastha 
3567ced83f9bSceastha 	if (aclp) {
3568ced83f9bSceastha 		error = facl_set(dirfd, aclp);
3569ced83f9bSceastha 		if (error) {
3570ced83f9bSceastha 			msg(ERRN, "failed to set acl entries on %sfile %s\n",
3571ced83f9bSceastha 			    (pdirfd == -1) ? "" : gettext("parent of "),
3572ced83f9bSceastha 			    (pdirfd == -1) ? fullname : name);
3573ced83f9bSceastha 		}
3574ced83f9bSceastha 		acl_free(aclp);
3575ced83f9bSceastha 	}
3576ced83f9bSceastha 
3577ced83f9bSceastha 	/*
3578ced83f9bSceastha 	 * Put back time stamps
3579ced83f9bSceastha 	 */
3580ced83f9bSceastha 
3581ced83f9bSceastha 	times[0].tv_sec = parentstat.st_atime;
3582ced83f9bSceastha 	times[0].tv_usec = 0;
3583ced83f9bSceastha 	times[1].tv_sec = parentstat.st_mtime;
3584ced83f9bSceastha 	times[1].tv_usec = 0;
3585ced83f9bSceastha 
3586ced83f9bSceastha 	(void) futimesat(cwd, (pattr == NULL) ? fullname : pattr, times);
3587ced83f9bSceastha 
3588ced83f9bSceastha 	(void) close(dirfd);
3589ced83f9bSceastha 
3590ced83f9bSceastha 	return (ofilefd);
3591ced83f9bSceastha }
3592ced83f9bSceastha #endif
3593ced83f9bSceastha 
3594ced83f9bSceastha #if defined(O_XATTR)
3595ced83f9bSceastha /*
3596ced83f9bSceastha  * Recursively open attribute directories until the attribute directory
3597ced83f9bSceastha  * containing the specified attribute, attrname, is opened.
3598ced83f9bSceastha  *
3599ced83f9bSceastha  * Currently, only 2 directory levels of attributes are supported, (i.e.,
3600ced83f9bSceastha  * extended system attributes on extended attributes).  The following are
3601ced83f9bSceastha  * the possible input combinations:
3602ced83f9bSceastha  *	1.  Open the attribute directory of the base file (don't change
3603ced83f9bSceastha  *	    into it).
3604ced83f9bSceastha  *		attr_parent = NULL
3605ced83f9bSceastha  *		attrname = '.'
3606ced83f9bSceastha  *	2.  Open the attribute directory of the base file and change into it.
3607ced83f9bSceastha  *		attr_parent = NULL
3608ced83f9bSceastha  *		attrname = <attr> | <sys_attr>
3609ced83f9bSceastha  *	3.  Open the attribute directory of the base file, change into it,
3610ced83f9bSceastha  *	    then recursively call open_attr_dir() to open the attribute's
3611ced83f9bSceastha  *	    parent directory (don't change into it).
3612ced83f9bSceastha  *		attr_parent = <attr>
3613ced83f9bSceastha  *		attrname = '.'
3614ced83f9bSceastha  *	4.  Open the attribute directory of the base file, change into it,
3615ced83f9bSceastha  *	    then recursively call open_attr_dir() to open the attribute's
3616ced83f9bSceastha  *	    parent directory and change into it.
3617ced83f9bSceastha  *		attr_parent = <attr>
3618ced83f9bSceastha  *		attrname = <attr> | <sys_attr>
3619ced83f9bSceastha  *
3620ced83f9bSceastha  * An attribute directory will be opened only if the underlying file system
3621ced83f9bSceastha  * supports the attribute type, and if the command line specifications
3622ced83f9bSceastha  * (f_extended_attr and f_sys_attr) enable the processing of the attribute
3623ced83f9bSceastha  * type.
3624ced83f9bSceastha  *
3625ced83f9bSceastha  * On succesful return, attr_parentfd will be the file descriptor of the
3626ced83f9bSceastha  * opened attribute directory.  In addition, if the attribute is a read-write
3627ced83f9bSceastha  * extended system attribute, rw_sysattr will be set to 1, otherwise
3628ced83f9bSceastha  * it will be set to 0.
3629ced83f9bSceastha  *
3630ced83f9bSceastha  * Possible return values:
3631ced83f9bSceastha  * 	ATTR_OK		Successfully opened and, if needed, changed into the
3632ced83f9bSceastha  *			attribute directory containing attrname.
3633ced83f9bSceastha  *	ATTR_SKIP	The command line specifications don't enable the
3634ced83f9bSceastha  *			processing of the attribute type.
3635ced83f9bSceastha  * 	ATTR_CHDIR_ERR	An error occurred while trying to change into an
3636ced83f9bSceastha  *			attribute directory.
3637ced83f9bSceastha  * 	ATTR_OPEN_ERR	An error occurred while trying to open an
3638ced83f9bSceastha  *			attribute directory.
3639ced83f9bSceastha  *	ATTR_XATTR_ERR	The underlying file system doesn't support extended
3640ced83f9bSceastha  *			attributes.
3641ced83f9bSceastha  *	ATTR_SATTR_ERR	The underlying file system doesn't support extended
3642ced83f9bSceastha  *			system attributes.
3643ced83f9bSceastha  */
3644ced83f9bSceastha static int
3645ced83f9bSceastha open_attr_dir(char *attrname, char *dirp, int cwd, char *attr_parent,
3646ced83f9bSceastha     int *attr_parentfd, int *rw_sysattr)
3647ced83f9bSceastha {
3648ced83f9bSceastha 	attr_status_t	rc;
3649ced83f9bSceastha 	int		firsttime = (*attr_parentfd == -1);
3650ced83f9bSceastha 	int		saveerrno;
3651ced83f9bSceastha 	int		ext_attr;
3652ced83f9bSceastha 
3653ced83f9bSceastha 	/*
3654ced83f9bSceastha 	 * open_attr_dir() was recursively called (input combination number 4),
3655ced83f9bSceastha 	 * close the previously opened file descriptor as we've already changed
3656ced83f9bSceastha 	 * into it.
3657ced83f9bSceastha 	 */
3658ced83f9bSceastha 	if (!firsttime) {
3659ced83f9bSceastha 		(void) close(*attr_parentfd);
3660ced83f9bSceastha 		*attr_parentfd = -1;
3661ced83f9bSceastha 	}
3662ced83f9bSceastha 
3663ced83f9bSceastha 	/*
3664ced83f9bSceastha 	 * Verify that the underlying file system supports the restoration
3665ced83f9bSceastha 	 * of the attribute.
3666ced83f9bSceastha 	 */
3667ced83f9bSceastha 	if ((rc = verify_attr_support(dirp, firsttime, ARC_RESTORE,
3668ced83f9bSceastha 	    &ext_attr)) != ATTR_OK) {
3669ced83f9bSceastha 		return (rc);
3670ced83f9bSceastha 	}
3671ced83f9bSceastha 
3672ced83f9bSceastha 	/* Open the base file's attribute directory */
3673ced83f9bSceastha 	if ((*attr_parentfd = attropen(dirp, ".", O_RDONLY)) == -1) {
3674ced83f9bSceastha 		/*
3675ced83f9bSceastha 		 * Save the errno from the attropen so it can be reported
3676ced83f9bSceastha 		 * if the retry of the attropen fails.
3677ced83f9bSceastha 		 */
3678ced83f9bSceastha 		saveerrno = errno;
3679ced83f9bSceastha 		if ((*attr_parentfd = retry_open_attr(-1, cwd, dirp,
3680ced83f9bSceastha 		    NULL, ".", O_RDONLY, 0)) == -1) {
3681ced83f9bSceastha 			(void) close(*attr_parentfd);
3682ced83f9bSceastha 			*attr_parentfd = -1;
3683ced83f9bSceastha 			errno = saveerrno;
3684ced83f9bSceastha 			return (ATTR_OPEN_ERR);
3685ced83f9bSceastha 		}
3686ced83f9bSceastha 	}
3687ced83f9bSceastha 
3688ced83f9bSceastha 	/*
3689ced83f9bSceastha 	 * Change into the parent attribute's directory unless we are
3690ced83f9bSceastha 	 * processing the hidden attribute directory of the base file itself.
3691ced83f9bSceastha 	 */
3692ced83f9bSceastha 	if ((Hiddendir == 0) || (firsttime && (attr_parent != NULL))) {
3693ced83f9bSceastha 		if (fchdir(*attr_parentfd) != 0) {
3694ced83f9bSceastha 			saveerrno = errno;
3695ced83f9bSceastha 			(void) close(*attr_parentfd);
3696ced83f9bSceastha 			*attr_parentfd = -1;
3697ced83f9bSceastha 			errno = saveerrno;
3698ced83f9bSceastha 			return (ATTR_CHDIR_ERR);
3699ced83f9bSceastha 		}
3700ced83f9bSceastha 	}
3701ced83f9bSceastha 
3702ced83f9bSceastha 	/* Determine if the attribute should be processed */
3703ced83f9bSceastha 	if ((rc = verify_attr(attrname, attr_parent, 1,
3704ced83f9bSceastha 	    rw_sysattr)) != ATTR_OK) {
3705ced83f9bSceastha 		saveerrno = errno;
3706ced83f9bSceastha 		(void) close(*attr_parentfd);
3707ced83f9bSceastha 		*attr_parentfd = -1;
3708ced83f9bSceastha 		errno = saveerrno;
3709ced83f9bSceastha 		return (rc);
3710ced83f9bSceastha 	}
3711ced83f9bSceastha 
3712ced83f9bSceastha 	/*
3713ced83f9bSceastha 	 * If the attribute is an extended system attribute of an attribute
3714ced83f9bSceastha 	 * (i.e., <attr>/<sys_attr>), then recursively call open_attr_dir() to
3715ced83f9bSceastha 	 * open the attribute directory of the parent attribute.
3716ced83f9bSceastha 	 */
3717ced83f9bSceastha 	if (firsttime && (attr_parent != NULL)) {
3718ced83f9bSceastha 		return (open_attr_dir(attrname, attr_parent, *attr_parentfd,
3719ced83f9bSceastha 		    attr_parent, attr_parentfd, rw_sysattr));
3720ced83f9bSceastha 	}
3721ced83f9bSceastha 
3722ced83f9bSceastha 	return (ATTR_OK);
3723ced83f9bSceastha }
3724ced83f9bSceastha #endif
3725ced83f9bSceastha 
3726ced83f9bSceastha /*
37277c478bd9Sstevel@tonic-gate  * file_pass:  If the -l option is set (link files when possible), and the
37287c478bd9Sstevel@tonic-gate  * source and destination file systems are the same, link the source file
37297c478bd9Sstevel@tonic-gate  * (G_p->g_nam_p) to the destination file (Fullnam) and return.  If not a
37307c478bd9Sstevel@tonic-gate  * linked file, transfer the data.  Otherwise, the first link to a file
37317c478bd9Sstevel@tonic-gate  * encountered is transferred normally and subsequent links are linked to it.
37327c478bd9Sstevel@tonic-gate  */
37337c478bd9Sstevel@tonic-gate 
37347c478bd9Sstevel@tonic-gate static int
37357c478bd9Sstevel@tonic-gate file_pass(void)
37367c478bd9Sstevel@tonic-gate {
37377c478bd9Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
37387c478bd9Sstevel@tonic-gate 	struct Lnk *ttl_p;
37397c478bd9Sstevel@tonic-gate 	char *save_name;
37407c478bd9Sstevel@tonic-gate 	int size;
37417c478bd9Sstevel@tonic-gate 	int cwd;
37427c478bd9Sstevel@tonic-gate 	char *lfrom, *lto;
37437c478bd9Sstevel@tonic-gate 
37447c478bd9Sstevel@tonic-gate 	G_p = &Gen;
37457c478bd9Sstevel@tonic-gate 
37467c478bd9Sstevel@tonic-gate 	if (Adir && !(Args & OCd)) {
37477c478bd9Sstevel@tonic-gate 		msg(ERR, "Use -d option to copy \"%s\"", G_p->g_nam_p);
37487c478bd9Sstevel@tonic-gate 		return (FILE_PASS_ERR);
37497c478bd9Sstevel@tonic-gate 	}
37507c478bd9Sstevel@tonic-gate 
37517c478bd9Sstevel@tonic-gate 	save_name = G_p->g_nam_p;
37527c478bd9Sstevel@tonic-gate 
37537c478bd9Sstevel@tonic-gate 	while (*(G_p->g_nam_p) == '/') {
37547c478bd9Sstevel@tonic-gate 		G_p->g_nam_p++;
37557c478bd9Sstevel@tonic-gate 	}
37567c478bd9Sstevel@tonic-gate 
3757ced83f9bSceastha 	(void) strcpy(Full_p, (G_p->g_attrfnam_p == NULL) ?
37587c478bd9Sstevel@tonic-gate 	    G_p->g_nam_p : G_p->g_attrfnam_p);
37597c478bd9Sstevel@tonic-gate 
3760ced83f9bSceastha 	if (G_p->g_attrnam_p == NULL) {
37617c478bd9Sstevel@tonic-gate 		G_p->g_passdirfd = open_dir(Fullnam_p);
37627c478bd9Sstevel@tonic-gate 
37637c478bd9Sstevel@tonic-gate 		if (G_p->g_passdirfd == -1) {
37647c478bd9Sstevel@tonic-gate 			msg(ERRN,
37657c478bd9Sstevel@tonic-gate 			    "Cannot open/create \"%s\"", Fullnam_p);
37667c478bd9Sstevel@tonic-gate 			return (FILE_PASS_ERR);
37677c478bd9Sstevel@tonic-gate 		}
37687c478bd9Sstevel@tonic-gate 	} else {
3769ced83f9bSceastha 		int	rw_sysattr;
37707c478bd9Sstevel@tonic-gate 
3771ced83f9bSceastha 		/*
3772ced83f9bSceastha 		 * Open the file's attribute directory.
3773ced83f9bSceastha 		 * Change into the base file's starting directory then call
3774ced83f9bSceastha 		 * open_attr_dir() to open the attribute directory of either
3775ced83f9bSceastha 		 * the base file (if G_p->g_attrparent_p is NULL) or the
3776ced83f9bSceastha 		 * attribute (if G_p->g_attrparent_p is set) of the base file.
3777ced83f9bSceastha 		 */
37787c478bd9Sstevel@tonic-gate 
3779ced83f9bSceastha 		G_p->g_passdirfd = -1;
3780ced83f9bSceastha 		(void) fchdir(G_p->g_baseparent_fd);
3781ced83f9bSceastha 		(void) open_attr_dir(G_p->g_attrnam_p, Fullnam_p,
3782ced83f9bSceastha 		    G_p->g_baseparent_fd, (G_p->g_attrparent_p == NULL) ? NULL :
3783ced83f9bSceastha 		    G_p->g_attrparent_p, &G_p->g_passdirfd, &rw_sysattr);
37847c478bd9Sstevel@tonic-gate 		if (G_p->g_passdirfd == -1) {
37857c478bd9Sstevel@tonic-gate 			msg(ERRN,
37867c478bd9Sstevel@tonic-gate 			    "Cannot open attribute directory of "
3787ced83f9bSceastha 			    "%s%s%sfile \"%s\"",
3788ced83f9bSceastha 			    (G_p->g_attrparent_p == NULL) ? "" :
3789ced83f9bSceastha 			    gettext("attribute \""),
3790ced83f9bSceastha 			    (G_p->g_attrparent_p == NULL) ? "" :
3791ced83f9bSceastha 			    G_p->g_attrparent_p,
3792ced83f9bSceastha 			    (G_p->g_attrparent_p == NULL) ? "" :
3793ced83f9bSceastha 			    gettext("\" of "), Fullnam_p);
37947c478bd9Sstevel@tonic-gate 			return (FILE_PASS_ERR);
37957c478bd9Sstevel@tonic-gate 		}
37967c478bd9Sstevel@tonic-gate 	}
37977c478bd9Sstevel@tonic-gate 
37987c478bd9Sstevel@tonic-gate 	if (Args & OCl) {
37997c478bd9Sstevel@tonic-gate 		/* We are linking back to the source directory. */
38007c478bd9Sstevel@tonic-gate 
38017c478bd9Sstevel@tonic-gate 		if (!Adir) {
38027c478bd9Sstevel@tonic-gate 			char *existingfile = save_name;
38037c478bd9Sstevel@tonic-gate 
38047c478bd9Sstevel@tonic-gate 			if ((Args & OCL) && issymlink) {
38057c478bd9Sstevel@tonic-gate 				/* We are chasing symlinks. */
38067c478bd9Sstevel@tonic-gate 
38077c478bd9Sstevel@tonic-gate 				if ((size = readlink(save_name, Symlnk_p,
38087c478bd9Sstevel@tonic-gate 				    MAXPATHLEN)) < 0) {
38097c478bd9Sstevel@tonic-gate 					msg(ERRN,
38107c478bd9Sstevel@tonic-gate 					    "Cannot read symbolic link \"%s\"",
38117c478bd9Sstevel@tonic-gate 					    save_name);
38127c478bd9Sstevel@tonic-gate 					return (FILE_PASS_ERR);
38137c478bd9Sstevel@tonic-gate 				}
38147c478bd9Sstevel@tonic-gate 
38157c478bd9Sstevel@tonic-gate 				Symlnk_p[size] = '\0';
38167c478bd9Sstevel@tonic-gate 				existingfile = Symlnk_p;
38177c478bd9Sstevel@tonic-gate 			}
38187c478bd9Sstevel@tonic-gate 
3819ced83f9bSceastha 			if (G_p->g_attrnam_p == NULL) {
38207c478bd9Sstevel@tonic-gate 				if (creat_lnk(G_p->g_passdirfd,
38217c478bd9Sstevel@tonic-gate 				    existingfile, Fullnam_p) == 0) {
38227c478bd9Sstevel@tonic-gate 					return (FILE_LINKED);
38237c478bd9Sstevel@tonic-gate 				}
38247c478bd9Sstevel@tonic-gate 			}
38257c478bd9Sstevel@tonic-gate 		}
38267c478bd9Sstevel@tonic-gate 	}
38277c478bd9Sstevel@tonic-gate 
38287c478bd9Sstevel@tonic-gate 	if ((G_p->g_mode & Ftype) == S_IFLNK && !(Args & OCL)) {
38297c478bd9Sstevel@tonic-gate 		/* The archive file is a symlink. */
38307c478bd9Sstevel@tonic-gate 
38317c478bd9Sstevel@tonic-gate 		errno = 0;
38327c478bd9Sstevel@tonic-gate 
38337c478bd9Sstevel@tonic-gate 		if ((size = readlink(save_name, Symlnk_p, MAXPATHLEN)) < 0) {
38347c478bd9Sstevel@tonic-gate 			msg(ERRN,
38357c478bd9Sstevel@tonic-gate 			    "Cannot read symbolic link \"%s\"", save_name);
38367c478bd9Sstevel@tonic-gate 			return (FILE_PASS_ERR);
38377c478bd9Sstevel@tonic-gate 		}
38387c478bd9Sstevel@tonic-gate 
38397c478bd9Sstevel@tonic-gate 		errno = 0;
38407c478bd9Sstevel@tonic-gate 		(void) missdir(Fullnam_p);
38417c478bd9Sstevel@tonic-gate 		*(Symlnk_p + size) = '\0';
38427c478bd9Sstevel@tonic-gate 
38437c478bd9Sstevel@tonic-gate 		if (symlink(Symlnk_p, Fullnam_p) < 0) {
38447c478bd9Sstevel@tonic-gate 			if (errno == EEXIST) {
38457c478bd9Sstevel@tonic-gate 				if (openout(G_p->g_passdirfd) < 0) {
3846647ab8f4Sceastha 					if (errno != EEXIST) {
3847647ab8f4Sceastha 						msg(ERRN,
3848647ab8f4Sceastha 						    "Cannot create \"%s\"",
3849647ab8f4Sceastha 						    Fullnam_p);
3850647ab8f4Sceastha 					}
38517c478bd9Sstevel@tonic-gate 					return (FILE_PASS_ERR);
38527c478bd9Sstevel@tonic-gate 				}
38537c478bd9Sstevel@tonic-gate 			} else {
38547c478bd9Sstevel@tonic-gate 				msg(ERRN, "Cannot create \"%s\"", Fullnam_p);
38557c478bd9Sstevel@tonic-gate 				return (FILE_PASS_ERR);
38567c478bd9Sstevel@tonic-gate 			}
3857647ab8f4Sceastha 		} else {
3858647ab8f4Sceastha 			if (Args & OCR) {
3859647ab8f4Sceastha 				if (lchown(Fullnam_p, (int)Rpw_p->pw_uid,
3860647ab8f4Sceastha 				    (int)Rpw_p->pw_gid) < 0) {
3861647ab8f4Sceastha 					msg(ERRN,
3862647ab8f4Sceastha 					    "Error during chown() of \"%s\"",
3863647ab8f4Sceastha 					    Fullnam_p);
38647c478bd9Sstevel@tonic-gate 				}
3865647ab8f4Sceastha 			} else if ((lchown(Fullnam_p, (int)G_p->g_uid,
3866647ab8f4Sceastha 			    (int)G_p->g_gid) < 0) && privileged) {
38677c478bd9Sstevel@tonic-gate 				msg(ERRN,
38687c478bd9Sstevel@tonic-gate 				    "Error during chown() of \"%s\"",
38697c478bd9Sstevel@tonic-gate 				    Fullnam_p);
38707c478bd9Sstevel@tonic-gate 			}
38717c478bd9Sstevel@tonic-gate 		}
38727c478bd9Sstevel@tonic-gate 
38737c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), Fullnam_p);
38747c478bd9Sstevel@tonic-gate 		return (FILE_PASS_ERR);
38757c478bd9Sstevel@tonic-gate 	}
38767c478bd9Sstevel@tonic-gate 
38777c478bd9Sstevel@tonic-gate 	if (!Adir && G_p->g_nlink > 1) {
38787c478bd9Sstevel@tonic-gate 		/* The archive file has hard links. */
38797c478bd9Sstevel@tonic-gate 
38807c478bd9Sstevel@tonic-gate 		tl_p = add_lnk(&ttl_p);
38817c478bd9Sstevel@tonic-gate 		l_p = ttl_p;
38827c478bd9Sstevel@tonic-gate 
38837c478bd9Sstevel@tonic-gate 		if (tl_p == l_p) {
38847c478bd9Sstevel@tonic-gate 			/* The archive file was not found. */
38857c478bd9Sstevel@tonic-gate 
38867c478bd9Sstevel@tonic-gate 			G_p = &tl_p->L_gen;
38877c478bd9Sstevel@tonic-gate 		} else {
38887c478bd9Sstevel@tonic-gate 			/* The archive file was found. */
38897c478bd9Sstevel@tonic-gate 
38907c478bd9Sstevel@tonic-gate 			cwd = -1;
38917c478bd9Sstevel@tonic-gate 
3892ced83f9bSceastha 			if (l_p->L_gen.g_attrnam_p != NULL) {
38937c478bd9Sstevel@tonic-gate 				/* We are linking an attribute */
38947c478bd9Sstevel@tonic-gate 
38957c478bd9Sstevel@tonic-gate 				(void) strcpy(Lnkend_p, l_p->L_gen.g_attrnam_p);
38967c478bd9Sstevel@tonic-gate 				cwd = save_cwd();
38977c478bd9Sstevel@tonic-gate 				(void) fchdir(G_p->g_passdirfd);
38987c478bd9Sstevel@tonic-gate 				lfrom = get_component(Lnknam_p);
38997c478bd9Sstevel@tonic-gate 				lto = tl_p->L_gen.g_attrnam_p;
39007c478bd9Sstevel@tonic-gate 			} else {
39017c478bd9Sstevel@tonic-gate 				/* We are not linking an attribute */
39027c478bd9Sstevel@tonic-gate 
39037c478bd9Sstevel@tonic-gate 				(void) strcpy(Lnkend_p, l_p->L_gen.g_nam_p);
39047c478bd9Sstevel@tonic-gate 				(void) strcpy(Full_p, tl_p->L_gen.g_nam_p);
39057c478bd9Sstevel@tonic-gate 				lfrom = Lnknam_p;
39067c478bd9Sstevel@tonic-gate 				lto = Fullnam_p;
39077c478bd9Sstevel@tonic-gate 			}
39087c478bd9Sstevel@tonic-gate 
39097c478bd9Sstevel@tonic-gate 			(void) creat_lnk(G_p->g_passdirfd, lfrom, lto);
39107c478bd9Sstevel@tonic-gate 
39117c478bd9Sstevel@tonic-gate 			if (cwd) {
39127c478bd9Sstevel@tonic-gate 				rest_cwd(cwd);
39137c478bd9Sstevel@tonic-gate 			}
39147c478bd9Sstevel@tonic-gate 
3915ced83f9bSceastha 			l_p->L_lnk_p = NULL;
39167c478bd9Sstevel@tonic-gate 			free(tl_p->L_gen.g_nam_p);
39177c478bd9Sstevel@tonic-gate 			free(tl_p);
39187c478bd9Sstevel@tonic-gate 
39197c478bd9Sstevel@tonic-gate 			if (l_p->L_cnt == G_p->g_nlink) {
39207c478bd9Sstevel@tonic-gate 				reclaim(l_p);
39217c478bd9Sstevel@tonic-gate 			}
39227c478bd9Sstevel@tonic-gate 
39237c478bd9Sstevel@tonic-gate 			return (FILE_LINKED);
39247c478bd9Sstevel@tonic-gate 		}
39257c478bd9Sstevel@tonic-gate 	}
39267c478bd9Sstevel@tonic-gate 
39277c478bd9Sstevel@tonic-gate 	if (Adir || Aspec) {
39287c478bd9Sstevel@tonic-gate 		/*
39297c478bd9Sstevel@tonic-gate 		 * The archive file is a directory,  block special, char
39307c478bd9Sstevel@tonic-gate 		 * special or a fifo.
39317c478bd9Sstevel@tonic-gate 		 */
39327c478bd9Sstevel@tonic-gate 
39337c478bd9Sstevel@tonic-gate 		if (creat_spec(G_p->g_passdirfd) > 0) {
39347c478bd9Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), Fullnam_p);
39357c478bd9Sstevel@tonic-gate 		}
39367c478bd9Sstevel@tonic-gate 	} else if ((Ofile = openout(G_p->g_passdirfd)) > 0) {
39377c478bd9Sstevel@tonic-gate 		data_pass();
39387c478bd9Sstevel@tonic-gate 	}
39397c478bd9Sstevel@tonic-gate 
39407c478bd9Sstevel@tonic-gate 	return (FILE_COPIED);
39417c478bd9Sstevel@tonic-gate }
39427c478bd9Sstevel@tonic-gate 
39437c478bd9Sstevel@tonic-gate /*
39447c478bd9Sstevel@tonic-gate  * flush_lnks: With new linked file handling, linked files are not archived
39457c478bd9Sstevel@tonic-gate  * until all links have been collected.  When the end of the list of filenames
39467c478bd9Sstevel@tonic-gate  * to archive has been reached, all files that did not encounter all their links
39477c478bd9Sstevel@tonic-gate  * are written out with actual (encountered) link counts.  A file with n links
39487c478bd9Sstevel@tonic-gate  * (that are archived) will be represented by n headers (one for each link (the
39497c478bd9Sstevel@tonic-gate  * first n - 1 have g_filesz set to 0)) followed by the data for the file.
39507c478bd9Sstevel@tonic-gate  */
39517c478bd9Sstevel@tonic-gate 
39527c478bd9Sstevel@tonic-gate static void
39537c478bd9Sstevel@tonic-gate flush_lnks(void)
39547c478bd9Sstevel@tonic-gate {
39557c478bd9Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
39567c478bd9Sstevel@tonic-gate 	off_t tfsize;
39577c478bd9Sstevel@tonic-gate 
39587c478bd9Sstevel@tonic-gate 	l_p = Lnk_hd.L_nxt_p;
39597c478bd9Sstevel@tonic-gate 	while (l_p != &Lnk_hd) {
39607c478bd9Sstevel@tonic-gate 		(void) strcpy(Gen.g_nam_p, l_p->L_gen.g_nam_p);
39617c478bd9Sstevel@tonic-gate 		if (stat(Gen.g_nam_p, &SrcSt) == 0) { /* check if file exists */
39627c478bd9Sstevel@tonic-gate 			tl_p = l_p;
39637c478bd9Sstevel@tonic-gate 			(void) creat_hdr();
39647c478bd9Sstevel@tonic-gate 			Gen.g_nlink = l_p->L_cnt; /* "actual" link count */
39657c478bd9Sstevel@tonic-gate 			tfsize = Gen.g_filesz;
39667c478bd9Sstevel@tonic-gate 			Gen.g_filesz = (off_t)0;
39677c478bd9Sstevel@tonic-gate 			G_p = &Gen;
3968ced83f9bSceastha 			while (tl_p != NULL) {
39697c478bd9Sstevel@tonic-gate 				Gen.g_nam_p = tl_p->L_gen.g_nam_p;
39707c478bd9Sstevel@tonic-gate 				Gen.g_namesz = tl_p->L_gen.g_namesz;
3971ced83f9bSceastha 				if (tl_p->L_lnk_p == NULL) {
39727c478bd9Sstevel@tonic-gate 					Gen.g_filesz = tfsize;
39737c478bd9Sstevel@tonic-gate 					if (open_dirfd() != 0) {
39747c478bd9Sstevel@tonic-gate 						break;
39757c478bd9Sstevel@tonic-gate 					}
39767c478bd9Sstevel@tonic-gate 					data_out();
39777c478bd9Sstevel@tonic-gate 					break;
39787c478bd9Sstevel@tonic-gate 				}
39797c478bd9Sstevel@tonic-gate 				write_hdr(ARCHIVE_NORMAL,
39807c478bd9Sstevel@tonic-gate 				    (off_t)0); /* header only */
39817c478bd9Sstevel@tonic-gate 				VERBOSE((Args & (OCv | OCV)), Gen.g_nam_p);
39827c478bd9Sstevel@tonic-gate 				tl_p = tl_p->L_lnk_p;
39837c478bd9Sstevel@tonic-gate 			}
39847c478bd9Sstevel@tonic-gate 			Gen.g_nam_p = Nam_p;
39857c478bd9Sstevel@tonic-gate 		} else /* stat(Gen.g_nam_p, &SrcSt) == 0 */
39867c478bd9Sstevel@tonic-gate 			msg(ERR, "\"%s%s%s\" has disappeared",
3987ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ?
39887c478bd9Sstevel@tonic-gate 			    Gen.g_nam_p : Gen.g_attrfnam_p,
3989ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ?
3990ced83f9bSceastha 			    "" : Gen.g_rw_sysattr ?
3991ced83f9bSceastha 			    gettext(" System Attribute ") :
3992ced83f9bSceastha 			    gettext(" Attribute "),
3993ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ?
39947c478bd9Sstevel@tonic-gate 			    "" : Gen.g_attrnam_p);
39957c478bd9Sstevel@tonic-gate 		tl_p = l_p;
39967c478bd9Sstevel@tonic-gate 		l_p = l_p->L_nxt_p;
39977c478bd9Sstevel@tonic-gate 		reclaim(tl_p);
39987c478bd9Sstevel@tonic-gate 	} /* l_p != &Lnk_hd */
39997c478bd9Sstevel@tonic-gate }
40007c478bd9Sstevel@tonic-gate 
4001da6c28aaSamw #if defined(O_XATTR)
4002da6c28aaSamw static int
4003da6c28aaSamw is_sysattr(char *name)
4004da6c28aaSamw {
4005da6c28aaSamw 	return ((strcmp(name, VIEW_READONLY) == 0) ||
4006da6c28aaSamw 	    (strcmp(name, VIEW_READWRITE) == 0));
4007da6c28aaSamw }
4008da6c28aaSamw #endif
4009da6c28aaSamw 
40107c478bd9Sstevel@tonic-gate /*
40117c478bd9Sstevel@tonic-gate  * gethdr: Get a header from the archive, validate it and check for the trailer.
40127c478bd9Sstevel@tonic-gate  * Any user specified Hdr_type is ignored (set to NONE in main).  Hdr_type is
40137c478bd9Sstevel@tonic-gate  * set appropriately after a valid header is found.  Unless the -k option is
40147c478bd9Sstevel@tonic-gate  * set a corrupted header causes an exit with an error.  I/O errors during
40157c478bd9Sstevel@tonic-gate  * examination of any part of the header cause gethdr to throw away any current
40167c478bd9Sstevel@tonic-gate  * data and start over.  Other errors during examination of any part of the
40177c478bd9Sstevel@tonic-gate  * header cause gethdr to advance one byte and continue the examination.
40187c478bd9Sstevel@tonic-gate  */
40197c478bd9Sstevel@tonic-gate 
40207c478bd9Sstevel@tonic-gate static int
40217c478bd9Sstevel@tonic-gate gethdr(void)
40227c478bd9Sstevel@tonic-gate {
40237c478bd9Sstevel@tonic-gate 	ushort_t ftype;
40247c478bd9Sstevel@tonic-gate 	int hit = NONE, cnt = 0;
40257c478bd9Sstevel@tonic-gate 	int goodhdr, hsize, offset;
40267c478bd9Sstevel@tonic-gate 	int bswap = 0;
40277c478bd9Sstevel@tonic-gate 	char *preptr;
40287c478bd9Sstevel@tonic-gate 	int k = 0;
40297c478bd9Sstevel@tonic-gate 	int j;
4030fa9e4066Sahrens 	int error;
4031fa9e4066Sahrens 	int aclcnt;
40327c478bd9Sstevel@tonic-gate 
40337c478bd9Sstevel@tonic-gate 	Gen.g_nam_p = Nam_p;
40347c478bd9Sstevel@tonic-gate 	do { /* hit == NONE && (Args & OCk) && Buffr.b_cnt > 0 */
40357c478bd9Sstevel@tonic-gate 		FILL(Hdrsz);
40367c478bd9Sstevel@tonic-gate 		switch (Hdr_type) {
40377c478bd9Sstevel@tonic-gate 		case NONE:
40387c478bd9Sstevel@tonic-gate 		case BIN:
40397c478bd9Sstevel@tonic-gate 			Binmag.b_byte[0] = Buffr.b_out_p[0];
40407c478bd9Sstevel@tonic-gate 			Binmag.b_byte[1] = Buffr.b_out_p[1];
40417c478bd9Sstevel@tonic-gate 			if ((Binmag.b_half == CMN_BIN) ||
40427c478bd9Sstevel@tonic-gate 			    (Binmag.b_half == CMN_BBS)) {
40437c478bd9Sstevel@tonic-gate 				hit = read_hdr(BIN);
40447c478bd9Sstevel@tonic-gate 				if (Hdr_type == NONE)
40457c478bd9Sstevel@tonic-gate 					bswap = 1;
40467c478bd9Sstevel@tonic-gate 				hsize = HDRSZ + Gen.g_namesz;
40477c478bd9Sstevel@tonic-gate 				break;
40487c478bd9Sstevel@tonic-gate 			}
40497c478bd9Sstevel@tonic-gate 			if (Hdr_type != NONE)
40507c478bd9Sstevel@tonic-gate 				break;
40517c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
40527c478bd9Sstevel@tonic-gate 		case CHR:
40537c478bd9Sstevel@tonic-gate 			if (!(strncmp(Buffr.b_out_p, CMS_CHR, CMS_LEN))) {
40547c478bd9Sstevel@tonic-gate 				hit = read_hdr(CHR);
40557c478bd9Sstevel@tonic-gate 				hsize = CHRSZ + Gen.g_namesz;
40567c478bd9Sstevel@tonic-gate 				break;
40577c478bd9Sstevel@tonic-gate 			}
40587c478bd9Sstevel@tonic-gate 			if (Hdr_type != NONE)
40597c478bd9Sstevel@tonic-gate 				break;
40607c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
40617c478bd9Sstevel@tonic-gate 		case ASC:
40627c478bd9Sstevel@tonic-gate 			if (!(strncmp(Buffr.b_out_p, CMS_ASC, CMS_LEN))) {
40637c478bd9Sstevel@tonic-gate 				hit = read_hdr(ASC);
40647c478bd9Sstevel@tonic-gate 				hsize = ASCSZ + Gen.g_namesz;
40657c478bd9Sstevel@tonic-gate 				Max_namesz = APATH;
40667c478bd9Sstevel@tonic-gate 				break;
40677c478bd9Sstevel@tonic-gate 			}
40687c478bd9Sstevel@tonic-gate 			if (Hdr_type != NONE)
40697c478bd9Sstevel@tonic-gate 				break;
40707c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
40717c478bd9Sstevel@tonic-gate 		case CRC:
40727c478bd9Sstevel@tonic-gate 			if (!(strncmp(Buffr.b_out_p, CMS_CRC, CMS_LEN))) {
40737c478bd9Sstevel@tonic-gate 				hit = read_hdr(CRC);
40747c478bd9Sstevel@tonic-gate 				hsize = ASCSZ + Gen.g_namesz;
40757c478bd9Sstevel@tonic-gate 				Max_namesz = APATH;
40767c478bd9Sstevel@tonic-gate 				break;
40777c478bd9Sstevel@tonic-gate 			}
40787c478bd9Sstevel@tonic-gate 			if (Hdr_type != NONE)
40797c478bd9Sstevel@tonic-gate 				break;
40807c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
40817c478bd9Sstevel@tonic-gate 
40827c478bd9Sstevel@tonic-gate 		case BAR:
40837c478bd9Sstevel@tonic-gate 			if (Hdr_p != NULL && strcmp(Hdr_p, "bar") == 0) {
40847c478bd9Sstevel@tonic-gate 				Hdrsz = BARSZ;
40857c478bd9Sstevel@tonic-gate 				FILL(Hdrsz);
40867c478bd9Sstevel@tonic-gate 				if ((hit = read_hdr(BAR)) == NONE) {
40877c478bd9Sstevel@tonic-gate 					Hdrsz = ASCSZ;
40887c478bd9Sstevel@tonic-gate 					break;
40897c478bd9Sstevel@tonic-gate 				}
40907c478bd9Sstevel@tonic-gate 				hit = BAR;
40917c478bd9Sstevel@tonic-gate 				hsize = BARSZ;
40927c478bd9Sstevel@tonic-gate 				break;
40937c478bd9Sstevel@tonic-gate 			}
40947c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
40957c478bd9Sstevel@tonic-gate 
40967c478bd9Sstevel@tonic-gate 		case USTAR:
40977c478bd9Sstevel@tonic-gate 			if (Hdr_p != NULL && strcmp(Hdr_p, "ustar") == 0) {
40987c478bd9Sstevel@tonic-gate 				Hdrsz = TARSZ;
40997c478bd9Sstevel@tonic-gate 				FILL(Hdrsz);
41007c478bd9Sstevel@tonic-gate 				if ((hit = read_hdr(USTAR)) == NONE) {
41017c478bd9Sstevel@tonic-gate 					Hdrsz = ASCSZ;
41027c478bd9Sstevel@tonic-gate 					break;
41037c478bd9Sstevel@tonic-gate 				}
41047c478bd9Sstevel@tonic-gate 				hit = USTAR;
41057c478bd9Sstevel@tonic-gate 				hsize = TARSZ;
41067c478bd9Sstevel@tonic-gate 				break;
41077c478bd9Sstevel@tonic-gate 			}
41087c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
41097c478bd9Sstevel@tonic-gate 		case TAR:
41107c478bd9Sstevel@tonic-gate 			if (Hdr_p != NULL && strcmp(Hdr_p, "tar") == 0) {
41117c478bd9Sstevel@tonic-gate 				Hdrsz = TARSZ;
41127c478bd9Sstevel@tonic-gate 				FILL(Hdrsz);
41137c478bd9Sstevel@tonic-gate 				if ((hit = read_hdr(TAR)) == NONE) {
41147c478bd9Sstevel@tonic-gate 					Hdrsz = ASCSZ;
41157c478bd9Sstevel@tonic-gate 					break;
41167c478bd9Sstevel@tonic-gate 				}
41177c478bd9Sstevel@tonic-gate 				hit = TAR;
41187c478bd9Sstevel@tonic-gate 				hsize = TARSZ;
41197c478bd9Sstevel@tonic-gate 				break;
41207c478bd9Sstevel@tonic-gate 			}
41217c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
41227c478bd9Sstevel@tonic-gate 		default:
41237c478bd9Sstevel@tonic-gate 			msg(EXT, "Impossible header type.");
41247c478bd9Sstevel@tonic-gate 		} /* Hdr_type */
41257c478bd9Sstevel@tonic-gate 
41267c478bd9Sstevel@tonic-gate 		if (hit == TAR || hit == USTAR) {
41277c478bd9Sstevel@tonic-gate 			Gen.g_nam_p = &nambuf[0];
41287c478bd9Sstevel@tonic-gate 		}
41297c478bd9Sstevel@tonic-gate 
41307c478bd9Sstevel@tonic-gate 		if (hit != NONE) {
41317c478bd9Sstevel@tonic-gate 			FILL(hsize);
41327c478bd9Sstevel@tonic-gate 			goodhdr = 1;
41337c478bd9Sstevel@tonic-gate 			if (Gen.g_filesz < (off_t)0 || Gen.g_namesz < 1)
41347c478bd9Sstevel@tonic-gate 				goodhdr = 0;
41357c478bd9Sstevel@tonic-gate 			if ((hit != USTAR) && (hit != TAR))
41367c478bd9Sstevel@tonic-gate 				if (Gen.g_namesz - 1 > Max_namesz)
41377c478bd9Sstevel@tonic-gate 					goodhdr = 0;
41387c478bd9Sstevel@tonic-gate 			/* TAR and USTAR */
41397c478bd9Sstevel@tonic-gate 			if ((hit == USTAR) || (hit == TAR)) {
41407c478bd9Sstevel@tonic-gate 				if (*Gen.g_nam_p == '\0') { /* tar trailer */
41417c478bd9Sstevel@tonic-gate 					goodhdr = 1;
41427c478bd9Sstevel@tonic-gate 				} else {
41437c478bd9Sstevel@tonic-gate 
41447c478bd9Sstevel@tonic-gate 					G_p = &Gen;
41457c478bd9Sstevel@tonic-gate 					if (G_p->g_cksum !=
41467c478bd9Sstevel@tonic-gate 					    cksum(TARTYP, 0, NULL)) {
41477c478bd9Sstevel@tonic-gate 						goodhdr = 0;
41487c478bd9Sstevel@tonic-gate 						msg(ERR,
41497c478bd9Sstevel@tonic-gate 						    "Bad header - checksum "
41507c478bd9Sstevel@tonic-gate 						    "error.");
41517c478bd9Sstevel@tonic-gate 					}
41527c478bd9Sstevel@tonic-gate 				}
41537c478bd9Sstevel@tonic-gate 			} else if (hit != BAR) { /* binary, -c, ASC and CRC */
41547c478bd9Sstevel@tonic-gate 				if (Gen.g_nlink <= (ulong_t)0)
41557c478bd9Sstevel@tonic-gate 					goodhdr = 0;
41567c478bd9Sstevel@tonic-gate 				if (*(Buffr.b_out_p + hsize - 1) != '\0')
41577c478bd9Sstevel@tonic-gate 					goodhdr = 0;
41587c478bd9Sstevel@tonic-gate 			}
41597c478bd9Sstevel@tonic-gate 			if (!goodhdr) {
41607c478bd9Sstevel@tonic-gate 				hit = NONE;
41617c478bd9Sstevel@tonic-gate 				if (!(Args & OCk))
41627c478bd9Sstevel@tonic-gate 					break;
41637c478bd9Sstevel@tonic-gate 				msg(ERR,
41647c478bd9Sstevel@tonic-gate 				    "Corrupt header, file(s) may be lost.");
41657c478bd9Sstevel@tonic-gate 			} else {
41667c478bd9Sstevel@tonic-gate 				FILL(hsize);
41677c478bd9Sstevel@tonic-gate 			}
41687c478bd9Sstevel@tonic-gate 		} /* hit != NONE */
41697c478bd9Sstevel@tonic-gate 		if (hit == NONE) {
41707c478bd9Sstevel@tonic-gate 			Buffr.b_out_p++;
41717c478bd9Sstevel@tonic-gate 			Buffr.b_cnt--;
41727c478bd9Sstevel@tonic-gate 			if (!(Args & OCk))
41737c478bd9Sstevel@tonic-gate 				break;
41747c478bd9Sstevel@tonic-gate 			if (!cnt++)
41757c478bd9Sstevel@tonic-gate 				msg(ERR, "Searching for magic number/header.");
41767c478bd9Sstevel@tonic-gate 		}
41777c478bd9Sstevel@tonic-gate 	} while (hit == NONE);
41787c478bd9Sstevel@tonic-gate 	if (hit == NONE) {
41797c478bd9Sstevel@tonic-gate 		if (Hdr_type == NONE)
41807c478bd9Sstevel@tonic-gate 			msg(EXT, "Not a cpio file, bad header.");
41817c478bd9Sstevel@tonic-gate 		else
41827c478bd9Sstevel@tonic-gate 			msg(EXT, "Bad magic number/header.");
41837c478bd9Sstevel@tonic-gate 	} else if (cnt > 0) {
41847c478bd9Sstevel@tonic-gate 		msg(EPOST, "Re-synchronized on magic number/header.");
41857c478bd9Sstevel@tonic-gate 	}
41867c478bd9Sstevel@tonic-gate 	if (Hdr_type == NONE) {
41877c478bd9Sstevel@tonic-gate 		Hdr_type = hit;
41887c478bd9Sstevel@tonic-gate 		switch (Hdr_type) {
41897c478bd9Sstevel@tonic-gate 		case BIN:
41907c478bd9Sstevel@tonic-gate 			if (bswap)
41917c478bd9Sstevel@tonic-gate 				Args |= BSM;
41927c478bd9Sstevel@tonic-gate 			Hdrsz = HDRSZ;
41937c478bd9Sstevel@tonic-gate 			Max_namesz = CPATH;
41947c478bd9Sstevel@tonic-gate 			Pad_val = HALFWD;
41957c478bd9Sstevel@tonic-gate 			Onecopy = 0;
41967c478bd9Sstevel@tonic-gate 			break;
41977c478bd9Sstevel@tonic-gate 		case CHR:
41987c478bd9Sstevel@tonic-gate 			Hdrsz = CHRSZ;
41997c478bd9Sstevel@tonic-gate 			Max_namesz = CPATH;
42007c478bd9Sstevel@tonic-gate 			Pad_val = 0;
42017c478bd9Sstevel@tonic-gate 			Onecopy = 0;
42027c478bd9Sstevel@tonic-gate 			break;
42037c478bd9Sstevel@tonic-gate 		case ASC:
42047c478bd9Sstevel@tonic-gate 		case CRC:
42057c478bd9Sstevel@tonic-gate 			Hdrsz = ASCSZ;
42067c478bd9Sstevel@tonic-gate 			Max_namesz = APATH;
42077c478bd9Sstevel@tonic-gate 			Pad_val = FULLWD;
42087c478bd9Sstevel@tonic-gate 			Onecopy = 1;
42097c478bd9Sstevel@tonic-gate 			break;
42107c478bd9Sstevel@tonic-gate 		case USTAR:
42117c478bd9Sstevel@tonic-gate 			Hdrsz = TARSZ;
42127c478bd9Sstevel@tonic-gate 			Max_namesz = HNAMLEN - 1;
42137c478bd9Sstevel@tonic-gate 			Pad_val = FULLBK;
42147c478bd9Sstevel@tonic-gate 			Onecopy = 0;
42157c478bd9Sstevel@tonic-gate 			break;
42167c478bd9Sstevel@tonic-gate 		case BAR:
42177c478bd9Sstevel@tonic-gate 		case TAR:
42187c478bd9Sstevel@tonic-gate 			Hdrsz = TARSZ;
42197c478bd9Sstevel@tonic-gate 			Max_namesz = TNAMLEN - 1;
42207c478bd9Sstevel@tonic-gate 			Pad_val = FULLBK;
42217c478bd9Sstevel@tonic-gate 			Onecopy = 0;
42227c478bd9Sstevel@tonic-gate 			break;
42237c478bd9Sstevel@tonic-gate 		default:
42247c478bd9Sstevel@tonic-gate 			msg(EXT, "Impossible header type.");
42257c478bd9Sstevel@tonic-gate 		} /* Hdr_type */
42267c478bd9Sstevel@tonic-gate 	} /* Hdr_type == NONE */
42277c478bd9Sstevel@tonic-gate 	if ((Hdr_type == USTAR) || (Hdr_type == TAR) ||
42287c478bd9Sstevel@tonic-gate 	    (Hdr_type == BAR)) {			/* TAR, USTAR, BAR */
42297c478bd9Sstevel@tonic-gate 		Gen.g_namesz = 0;
42307c478bd9Sstevel@tonic-gate 		if (Gen.g_nam_p[0] == '\0')
42317c478bd9Sstevel@tonic-gate 			return (0);
42327c478bd9Sstevel@tonic-gate 		else {
42337c478bd9Sstevel@tonic-gate 			preptr = &prebuf[0];
4234ced83f9bSceastha 			if (*preptr != NULL) {
42357c478bd9Sstevel@tonic-gate 				k = strlen(&prebuf[0]);
42367c478bd9Sstevel@tonic-gate 				if (k < PRESIZ) {
42377c478bd9Sstevel@tonic-gate 					(void) strcpy(&fullnam[0], &prebuf[0]);
42387c478bd9Sstevel@tonic-gate 					j = 0;
42397c478bd9Sstevel@tonic-gate 					fullnam[k++] = '/';
42407c478bd9Sstevel@tonic-gate 					while ((j < NAMSIZ) && (&nambuf[j] !=
4241ced83f9bSceastha 					    '\0')) {
42427c478bd9Sstevel@tonic-gate 						fullnam[k] = nambuf[j];
42437c478bd9Sstevel@tonic-gate 						k++; j++;
42447c478bd9Sstevel@tonic-gate 					}
42457c478bd9Sstevel@tonic-gate 					fullnam[k] = '\0';
42467c478bd9Sstevel@tonic-gate 				} else if (k >= PRESIZ) {
42477c478bd9Sstevel@tonic-gate 					k = 0;
42487c478bd9Sstevel@tonic-gate 					while ((k < PRESIZ) && (prebuf[k] !=
42497c478bd9Sstevel@tonic-gate 					    '\0')) {
42507c478bd9Sstevel@tonic-gate 						fullnam[k] = prebuf[k];
42517c478bd9Sstevel@tonic-gate 						k++;
42527c478bd9Sstevel@tonic-gate 					}
42537c478bd9Sstevel@tonic-gate 					fullnam[k++] = '/';
42547c478bd9Sstevel@tonic-gate 					j = 0;
42557c478bd9Sstevel@tonic-gate 					while ((j < NAMSIZ) && (nambuf[j] !=
42567c478bd9Sstevel@tonic-gate 					    '\0')) {
42577c478bd9Sstevel@tonic-gate 						fullnam[k] = nambuf[j];
42587c478bd9Sstevel@tonic-gate 						k++; j++;
42597c478bd9Sstevel@tonic-gate 					}
42607c478bd9Sstevel@tonic-gate 					fullnam[k] = '\0';
42617c478bd9Sstevel@tonic-gate 				}
42627c478bd9Sstevel@tonic-gate 				Gen.g_nam_p = &fullnam[0];
42637c478bd9Sstevel@tonic-gate 			} else
42647c478bd9Sstevel@tonic-gate 				Gen.g_nam_p = &nambuf[0];
42657c478bd9Sstevel@tonic-gate 
42667c478bd9Sstevel@tonic-gate 			/*
42677c478bd9Sstevel@tonic-gate 			 * initialize the buffer so that the prefix will not
42687c478bd9Sstevel@tonic-gate 			 * applied to the next entry in the archive
42697c478bd9Sstevel@tonic-gate 			 */
42707c478bd9Sstevel@tonic-gate 			(void) memset(prebuf, 0, sizeof (prebuf));
42717c478bd9Sstevel@tonic-gate 		}
42727c478bd9Sstevel@tonic-gate 	} else if (Hdr_type != BAR) {
42737c478bd9Sstevel@tonic-gate 		(void) memcpy(Gen.g_nam_p, Buffr.b_out_p + Hdrsz, Gen.g_namesz);
42747c478bd9Sstevel@tonic-gate 		if (!(strcmp(Gen.g_nam_p, "TRAILER!!!")))
42757c478bd9Sstevel@tonic-gate 			return (0);
42767c478bd9Sstevel@tonic-gate 	}
42777c478bd9Sstevel@tonic-gate 	offset = ((hsize + Pad_val) & ~Pad_val);
42787c478bd9Sstevel@tonic-gate 	FILL(offset + Hdrsz);
42797c478bd9Sstevel@tonic-gate 	Thdr_p = (union tblock *)Buffr.b_out_p;
42807c478bd9Sstevel@tonic-gate 	Buffr.b_out_p += offset;
42817c478bd9Sstevel@tonic-gate 	Buffr.b_cnt -= (off_t)offset;
42827c478bd9Sstevel@tonic-gate 	ftype = Gen.g_mode & Ftype;
42837c478bd9Sstevel@tonic-gate 
42847c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
42857c478bd9Sstevel@tonic-gate 	/* extended attribute support */
42867c478bd9Sstevel@tonic-gate 	if (((Gen.g_mode & S_IFMT) == _XATTR_CPIO_MODE) ||
42877c478bd9Sstevel@tonic-gate 	    ((Hdr_type == USTAR || Hdr_type == TAR) &&
42887c478bd9Sstevel@tonic-gate 	    Thdr_p->tbuf.t_typeflag == _XATTR_HDRTYPE)) {
4289ced83f9bSceastha 		char	*aname;
4290ced83f9bSceastha 		char	*attrparent = NULL;
4291ced83f9bSceastha 		char	*attrpath = NULL;
4292da6c28aaSamw 		char	*tapath;
4293ced83f9bSceastha 		char	*taname;
4294da6c28aaSamw 
4295ced83f9bSceastha 		if (xattrp != NULL) {
42967c478bd9Sstevel@tonic-gate 			if (xattrbadhead) {
42977c478bd9Sstevel@tonic-gate 				free(xattrhead);
42987c478bd9Sstevel@tonic-gate 				xattrp = NULL;
42997c478bd9Sstevel@tonic-gate 				xattr_linkp = NULL;
43007c478bd9Sstevel@tonic-gate 				xattrhead = NULL;
43017c478bd9Sstevel@tonic-gate 				return (1);
43027c478bd9Sstevel@tonic-gate 			}
4303da6c28aaSamw 
4304da6c28aaSamw 			/*
4305ced83f9bSceastha 			 * At this point, the attribute path contains
4306ced83f9bSceastha 			 * the path to the attribute rooted at the hidden
4307ced83f9bSceastha 			 * attribute directory of the base file.  This can
4308ced83f9bSceastha 			 * be a simple attribute or extended attribute name,
4309ced83f9bSceastha 			 * or it can be something like <attr>/<sys attr> if
4310ced83f9bSceastha 			 * we are processing a system attribute of an attribute.
4311ced83f9bSceastha 			 * Determine the attribute name and attribute parent
4312ced83f9bSceastha 			 * (if there is one).  When we are processing a simple
4313ced83f9bSceastha 			 * attribute or extended attribute name, the attribute
4314ced83f9bSceastha 			 * parent will be set to NULL.  When we are processing
4315ced83f9bSceastha 			 * something like <attr>/<sys attr>, the attribute
4316ced83f9bSceastha 			 * parent will be contain <attr>, and the attribute
4317ced83f9bSceastha 			 * name will contain <sys attr>.
4318da6c28aaSamw 			 */
4319ced83f9bSceastha 			tapath = xattrp->h_names +
4320ced83f9bSceastha 			    strlen(xattrp->h_names) + 1;
4321ced83f9bSceastha 			attrpath = e_strdup(E_EXIT, tapath);
4322ced83f9bSceastha 			if ((taname = strpbrk(tapath, "/")) != NULL) {
4323ced83f9bSceastha 				aname = taname + 1;
4324ced83f9bSceastha 				*taname = '\0';
4325ced83f9bSceastha 				attrparent = tapath;
4326ced83f9bSceastha 			} else {
4327ced83f9bSceastha 				aname = tapath;
43287c478bd9Sstevel@tonic-gate 			}
43297c478bd9Sstevel@tonic-gate 
4330ced83f9bSceastha 			Gen.g_rw_sysattr = is_sysattr(aname);
4331ced83f9bSceastha 			Gen.g_baseparent_fd = attr_baseparent_fd;
4332ced83f9bSceastha 
4333ced83f9bSceastha 			if (Gen.g_attrfnam_p != NULL) {
43347c478bd9Sstevel@tonic-gate 				free(Gen.g_attrfnam_p);
4335ced83f9bSceastha 				Gen.g_attrfnam_p = NULL;
43367c478bd9Sstevel@tonic-gate 			}
4337ced83f9bSceastha 			if (Gen.g_attrnam_p != NULL) {
43387c478bd9Sstevel@tonic-gate 				free(Gen.g_attrnam_p);
4339ced83f9bSceastha 				Gen.g_attrnam_p = NULL;
4340ced83f9bSceastha 			}
4341ced83f9bSceastha 			if (Gen.g_attrparent_p != NULL) {
4342ced83f9bSceastha 				free(Gen.g_attrparent_p);
4343ced83f9bSceastha 				Gen.g_attrparent_p = NULL;
4344ced83f9bSceastha 			}
4345ced83f9bSceastha 			if (Gen.g_attrpath_p != NULL) {
4346ced83f9bSceastha 				free(Gen.g_attrpath_p);
4347ced83f9bSceastha 				Gen.g_attrpath_p = NULL;
43487c478bd9Sstevel@tonic-gate 			}
43497c478bd9Sstevel@tonic-gate 			if (Renam_p && Renam_p[0] != '\0') {
43507c478bd9Sstevel@tonic-gate 				Gen.g_attrfnam_p = e_strdup(E_EXIT, Renam_p);
43517c478bd9Sstevel@tonic-gate 			} else {
43527c478bd9Sstevel@tonic-gate 				Gen.g_attrfnam_p = e_strdup(E_EXIT,
43537c478bd9Sstevel@tonic-gate 				    xattrp->h_names);
43547c478bd9Sstevel@tonic-gate 			}
4355ced83f9bSceastha 			Gen.g_attrnam_p = e_strdup(E_EXIT, aname);
43567c478bd9Sstevel@tonic-gate 
4357ced83f9bSceastha 			if (attrparent != NULL) {
4358ced83f9bSceastha 				if (Renam_attr_p && Renam_attr_p[0] != '\0') {
4359ced83f9bSceastha 					size_t	apathlen = strlen(attrparent) +
4360ced83f9bSceastha 					    strlen(aname) + 2;
4361ced83f9bSceastha 					Gen.g_attrparent_p = e_strdup(E_EXIT,
4362ced83f9bSceastha 					    Renam_attr_p);
4363ced83f9bSceastha 					Gen.g_attrpath_p = e_zalloc(E_EXIT,
4364ced83f9bSceastha 					    apathlen);
4365ced83f9bSceastha 					(void) snprintf(Gen.g_attrpath_p,
4366ced83f9bSceastha 					    apathlen, "%s/%s", Renam_attr_p,
4367ced83f9bSceastha 					    aname);
4368ced83f9bSceastha 					(void) free(attrparent);
4369ced83f9bSceastha 					(void) free(attrpath);
4370ced83f9bSceastha 				} else {
4371ced83f9bSceastha 					Gen.g_attrparent_p = attrparent;
4372ced83f9bSceastha 					Gen.g_attrpath_p = attrpath;
4373ced83f9bSceastha 				}
4374ced83f9bSceastha 			} else {
4375ced83f9bSceastha 				Gen.g_attrpath_p = attrpath;
4376ced83f9bSceastha 			}
43777c478bd9Sstevel@tonic-gate 
4378ced83f9bSceastha 			if (xattr_linkp != NULL) {
4379ced83f9bSceastha 				if (Gen.g_linktoattrfnam_p != NULL) {
4380ced83f9bSceastha 					free(Gen.g_linktoattrfnam_p);
4381ced83f9bSceastha 					Gen.g_linktoattrfnam_p = NULL;
4382ced83f9bSceastha 				}
4383ced83f9bSceastha 				if (Gen.g_linktoattrnam_p != NULL) {
4384ced83f9bSceastha 					free(Gen.g_linktoattrnam_p);
4385ced83f9bSceastha 					Gen.g_linktoattrnam_p = NULL;
4386ced83f9bSceastha 				}
4387ced83f9bSceastha 				if (Renam_attr_p && Renam_attr_p[0] != '\0') {
4388ced83f9bSceastha 					Gen.g_linktoattrfnam_p = e_strdup(
4389ced83f9bSceastha 					    E_EXIT, Renam_attr_p);
4390ced83f9bSceastha 				} else {
4391ced83f9bSceastha 					Gen.g_linktoattrfnam_p = e_strdup(
4392ced83f9bSceastha 					    E_EXIT, xattr_linkp->h_names);
4393ced83f9bSceastha 				}
4394ced83f9bSceastha 				Gen.g_linktoattrnam_p = e_strdup(E_EXIT,
4395ced83f9bSceastha 				    aname);
4396ced83f9bSceastha 				xattr_linkp = NULL;
4397ced83f9bSceastha 			}
43987c478bd9Sstevel@tonic-gate 			if (Hdr_type != USTAR && Hdr_type != TAR) {
43997c478bd9Sstevel@tonic-gate 				Gen.g_mode = Gen.g_mode & (~_XATTR_CPIO_MODE);
44007c478bd9Sstevel@tonic-gate 				Gen.g_mode |= attrmode(xattrp->h_typeflag);
44017c478bd9Sstevel@tonic-gate 			} else if (Hdr_type == USTAR || Hdr_type == TAR) {
44027c478bd9Sstevel@tonic-gate 				Thdr_p->tbuf.t_typeflag = xattrp->h_typeflag;
44037c478bd9Sstevel@tonic-gate 			}
4404ced83f9bSceastha 
44057c478bd9Sstevel@tonic-gate 			ftype = Gen.g_mode & Ftype;
44067c478bd9Sstevel@tonic-gate 			Adir = ftype == S_IFDIR;
4407cdd68f5aSceastha 			Aspec = (ftype == S_IFBLK || ftype == S_IFCHR ||
4408cdd68f5aSceastha 			    ftype == S_IFIFO || ftype == S_IFSOCK);
44097c478bd9Sstevel@tonic-gate 
44107c478bd9Sstevel@tonic-gate 			if (Gen.g_attrnam_p[0] == '.' &&
44117c478bd9Sstevel@tonic-gate 			    Gen.g_attrnam_p[1] == '\0' &&
44127c478bd9Sstevel@tonic-gate 			    xattrp->h_typeflag == DIRTYPE) {
44137c478bd9Sstevel@tonic-gate 				Hiddendir = 1;
44147c478bd9Sstevel@tonic-gate 			} else {
44157c478bd9Sstevel@tonic-gate 				Hiddendir = 0;
44167c478bd9Sstevel@tonic-gate 			}
44177c478bd9Sstevel@tonic-gate 
44187c478bd9Sstevel@tonic-gate 			free(xattrhead);
44197c478bd9Sstevel@tonic-gate 			xattrhead = NULL;
44207c478bd9Sstevel@tonic-gate 			xattrp = NULL;
44217c478bd9Sstevel@tonic-gate 		} else {
44227c478bd9Sstevel@tonic-gate 			if (xattrbadhead == 0) {
44237c478bd9Sstevel@tonic-gate 				(void) read_xattr_hdr();
44247c478bd9Sstevel@tonic-gate 				return (2);
44257c478bd9Sstevel@tonic-gate 			}
44267c478bd9Sstevel@tonic-gate 		}
4427ced83f9bSceastha 	} else {
4428ced83f9bSceastha 		Hiddendir = 0;
44297c478bd9Sstevel@tonic-gate 	}
44307c478bd9Sstevel@tonic-gate #endif /* O_XATTR */
44317c478bd9Sstevel@tonic-gate 
44327c478bd9Sstevel@tonic-gate 	/* acl support: grab acl info */
44337c478bd9Sstevel@tonic-gate 	if ((Gen.g_mode == SECMODE) || ((Hdr_type == USTAR ||
44347c478bd9Sstevel@tonic-gate 	    Hdr_type == TAR) && Thdr_p->tbuf.t_typeflag == 'A')) {
44357c478bd9Sstevel@tonic-gate 		/* this is an ancillary file */
44367c478bd9Sstevel@tonic-gate 		off_t	bytes;
44377c478bd9Sstevel@tonic-gate 		char	*secp;
44387c478bd9Sstevel@tonic-gate 		int	pad;
44397c478bd9Sstevel@tonic-gate 		int	cnt;
44407c478bd9Sstevel@tonic-gate 		char	*tp;
44417c478bd9Sstevel@tonic-gate 		int	attrsize;
44427c478bd9Sstevel@tonic-gate 
44437c478bd9Sstevel@tonic-gate 		if (Pflag) {
44447c478bd9Sstevel@tonic-gate 			bytes = Gen.g_filesz;
44457c478bd9Sstevel@tonic-gate 			secp = e_zalloc(E_EXIT, (uint_t)bytes);
44467c478bd9Sstevel@tonic-gate 			tp = secp;
44477c478bd9Sstevel@tonic-gate 
44487c478bd9Sstevel@tonic-gate 			while (bytes > 0) {
44497c478bd9Sstevel@tonic-gate 				cnt = (int)(bytes > CPIOBSZ) ? CPIOBSZ : bytes;
44507c478bd9Sstevel@tonic-gate 				FILL(cnt);
44517c478bd9Sstevel@tonic-gate 				(void) memcpy(tp, Buffr.b_out_p, cnt);
44527c478bd9Sstevel@tonic-gate 				tp += cnt;
44537c478bd9Sstevel@tonic-gate 				Buffr.b_out_p += cnt;
44547c478bd9Sstevel@tonic-gate 				Buffr.b_cnt -= (off_t)cnt;
44557c478bd9Sstevel@tonic-gate 				bytes -= (off_t)cnt;
44567c478bd9Sstevel@tonic-gate 			}
44577c478bd9Sstevel@tonic-gate 
44587c478bd9Sstevel@tonic-gate 			pad = (Pad_val + 1 - (Gen.g_filesz & Pad_val)) &
44597c478bd9Sstevel@tonic-gate 			    Pad_val;
44607c478bd9Sstevel@tonic-gate 			if (pad != 0) {
44617c478bd9Sstevel@tonic-gate 				FILL(pad);
44627c478bd9Sstevel@tonic-gate 				Buffr.b_out_p += pad;
44637c478bd9Sstevel@tonic-gate 				Buffr.b_cnt -= (off_t)pad;
44647c478bd9Sstevel@tonic-gate 			}
44657c478bd9Sstevel@tonic-gate 
44667c478bd9Sstevel@tonic-gate 			/* got all attributes in secp */
44677c478bd9Sstevel@tonic-gate 			tp = secp;
44687c478bd9Sstevel@tonic-gate 			do {
44697c478bd9Sstevel@tonic-gate 				attr = (struct sec_attr *)tp;
44707c478bd9Sstevel@tonic-gate 				switch (attr->attr_type) {
44717c478bd9Sstevel@tonic-gate 				case UFSD_ACL:
4472fa9e4066Sahrens 				case ACE_ACL:
44737c478bd9Sstevel@tonic-gate 					(void) sscanf(attr->attr_len, "%7lo",
44747c478bd9Sstevel@tonic-gate 					    (ulong_t *)&aclcnt);
44757c478bd9Sstevel@tonic-gate 					/* header is 8 */
44767c478bd9Sstevel@tonic-gate 					attrsize = 8 +
44777c478bd9Sstevel@tonic-gate 					    strlen(&attr->attr_info[0])
44787c478bd9Sstevel@tonic-gate 					    + 1;
4479fa9e4066Sahrens 
4480fa9e4066Sahrens 					error =
4481fa9e4066Sahrens 					    acl_fromtext(&attr->attr_info[0],
4482fa9e4066Sahrens 					    &aclp);
4483fa9e4066Sahrens 
4484fa9e4066Sahrens 					if (error != 0) {
4485fa9e4066Sahrens 						msg(ERR,
4486fa9e4066Sahrens 						    "aclfromtext failed: %s",
4487fa9e4066Sahrens 						    acl_strerror(error));
4488fa9e4066Sahrens 						bytes -= attrsize;
44897c478bd9Sstevel@tonic-gate 						break;
44907c478bd9Sstevel@tonic-gate 					}
4491fa9e4066Sahrens 
4492fa9e4066Sahrens 					if (aclcnt != acl_cnt(aclp)) {
44937c478bd9Sstevel@tonic-gate 						msg(ERR, "acl count error");
4494fa9e4066Sahrens 						bytes -= attrsize;
44957c478bd9Sstevel@tonic-gate 						break;
44967c478bd9Sstevel@tonic-gate 					}
44977c478bd9Sstevel@tonic-gate 					bytes -= attrsize;
44987c478bd9Sstevel@tonic-gate 					break;
44997c478bd9Sstevel@tonic-gate 
45007c478bd9Sstevel@tonic-gate 				/* SunFed case goes here */
45017c478bd9Sstevel@tonic-gate 
45027c478bd9Sstevel@tonic-gate 				default:
45037c478bd9Sstevel@tonic-gate 					msg(EXT, "unrecognized attr type");
45047c478bd9Sstevel@tonic-gate 					break;
45057c478bd9Sstevel@tonic-gate 			}
45067c478bd9Sstevel@tonic-gate 			/* next attributes */
45077c478bd9Sstevel@tonic-gate 			tp += attrsize;
45087c478bd9Sstevel@tonic-gate 			} while (bytes > 0);
45097c478bd9Sstevel@tonic-gate 			free(secp);
45107c478bd9Sstevel@tonic-gate 		} else {
45117c478bd9Sstevel@tonic-gate 			/* skip security info */
45127c478bd9Sstevel@tonic-gate 			G_p = &Gen;
45137c478bd9Sstevel@tonic-gate 			data_in(P_SKIP);
45147c478bd9Sstevel@tonic-gate 		}
45157c478bd9Sstevel@tonic-gate 		/*
45167c478bd9Sstevel@tonic-gate 		 * We already got the file content, dont call file_in()
45177c478bd9Sstevel@tonic-gate 		 * when return. The new return code(2) is used to
45187c478bd9Sstevel@tonic-gate 		 *  indicate that.
45197c478bd9Sstevel@tonic-gate 		 */
45207c478bd9Sstevel@tonic-gate 		VERBOSE((Args & OCt), Gen.g_nam_p);
45217c478bd9Sstevel@tonic-gate 		return (2);
45227c478bd9Sstevel@tonic-gate 	} /* acl */
45237c478bd9Sstevel@tonic-gate 
45247c478bd9Sstevel@tonic-gate 	Adir = (ftype == S_IFDIR);
4525cdd68f5aSceastha 	Aspec = (ftype == S_IFBLK || ftype == S_IFCHR || ftype == S_IFIFO ||
4526cdd68f5aSceastha 	    ftype == S_IFSOCK);
45277c478bd9Sstevel@tonic-gate 
45287c478bd9Sstevel@tonic-gate 	/*
45297c478bd9Sstevel@tonic-gate 	 * Skip any trailing slashes
45307c478bd9Sstevel@tonic-gate 	 */
45317c478bd9Sstevel@tonic-gate 	chop_endslashes(Gen.g_nam_p);
45327c478bd9Sstevel@tonic-gate 	return (1);
45337c478bd9Sstevel@tonic-gate }
45347c478bd9Sstevel@tonic-gate 
45357c478bd9Sstevel@tonic-gate /*
45367c478bd9Sstevel@tonic-gate  * getname: Get file names for inclusion in the archive.  When end of file
45377c478bd9Sstevel@tonic-gate  * on the input stream of file names is reached, flush the link buffer out.
45387c478bd9Sstevel@tonic-gate  * For each filename, remove leading "./"s and multiple "/"s, and remove
4539da6c28aaSamw  * any trailing newline "\n".  Finally, verify the existence of the file,
45407c478bd9Sstevel@tonic-gate  * and call creat_hdr() to fill in the gen_hdr structure.
45417c478bd9Sstevel@tonic-gate  */
45427c478bd9Sstevel@tonic-gate 
45437c478bd9Sstevel@tonic-gate static int
45447c478bd9Sstevel@tonic-gate getname(void)
45457c478bd9Sstevel@tonic-gate {
45467c478bd9Sstevel@tonic-gate 	int goodfile = 0, lastchar, err;
45477c478bd9Sstevel@tonic-gate 	char *s;
45487c478bd9Sstevel@tonic-gate 	char *dir;
45497c478bd9Sstevel@tonic-gate 
45507c478bd9Sstevel@tonic-gate 	Gen.g_nam_p = Nam_p;
4551ced83f9bSceastha 	Hiddendir = 0;
45527c478bd9Sstevel@tonic-gate 
45537c478bd9Sstevel@tonic-gate 	while (!goodfile) {
45547c478bd9Sstevel@tonic-gate 		err = 0;
45557c478bd9Sstevel@tonic-gate 
4556ced83f9bSceastha 		while ((s = fgets(Gen.g_nam_p, APATH+1, In_p)) != NULL) {
45577c478bd9Sstevel@tonic-gate 			lastchar = strlen(s) - 1;
45587c478bd9Sstevel@tonic-gate 			issymlink = 0;
45597c478bd9Sstevel@tonic-gate 
45607c478bd9Sstevel@tonic-gate 			if (s[lastchar] != '\n') {
45617c478bd9Sstevel@tonic-gate 				if (lastchar == APATH - 1) {
45627c478bd9Sstevel@tonic-gate 					if (!err) {
45637c478bd9Sstevel@tonic-gate 						msg(ERR,
45647c478bd9Sstevel@tonic-gate 						    "%s name too long.",
45657c478bd9Sstevel@tonic-gate 						    Nam_p);
45667c478bd9Sstevel@tonic-gate 					}
45677c478bd9Sstevel@tonic-gate 					goodfile = 0;
45687c478bd9Sstevel@tonic-gate 					err = 1;
45697c478bd9Sstevel@tonic-gate 				} else {
45707c478bd9Sstevel@tonic-gate 					break;
45717c478bd9Sstevel@tonic-gate 				}
45727c478bd9Sstevel@tonic-gate 			} else {
45737c478bd9Sstevel@tonic-gate 				s[lastchar] = '\0';
45747c478bd9Sstevel@tonic-gate 				break;
45757c478bd9Sstevel@tonic-gate 			}
45767c478bd9Sstevel@tonic-gate 		}
45777c478bd9Sstevel@tonic-gate 
4578ced83f9bSceastha 		if (s == NULL) {
45797c478bd9Sstevel@tonic-gate 			if (Gen.g_dirfd != -1) {
45807c478bd9Sstevel@tonic-gate 				(void) close(Gen.g_dirfd);
45817c478bd9Sstevel@tonic-gate 				Gen.g_dirfd = -1;
45827c478bd9Sstevel@tonic-gate 			}
45837c478bd9Sstevel@tonic-gate 			if (Onecopy && (Args & OCo)) {
45847c478bd9Sstevel@tonic-gate 				flush_lnks();
45857c478bd9Sstevel@tonic-gate 			}
45867c478bd9Sstevel@tonic-gate 			return (0);
45877c478bd9Sstevel@tonic-gate 		}
45887c478bd9Sstevel@tonic-gate 
45897c478bd9Sstevel@tonic-gate 		while (*Gen.g_nam_p == '.' && Gen.g_nam_p[1] == '/') {
45907c478bd9Sstevel@tonic-gate 			Gen.g_nam_p += 2;
45917c478bd9Sstevel@tonic-gate 			while (*Gen.g_nam_p == '/')
45927c478bd9Sstevel@tonic-gate 				Gen.g_nam_p++;
45937c478bd9Sstevel@tonic-gate 		}
45947c478bd9Sstevel@tonic-gate 
45957c478bd9Sstevel@tonic-gate 		/*
45967c478bd9Sstevel@tonic-gate 		 * Skip any trailing slashes
45977c478bd9Sstevel@tonic-gate 		 */
45987c478bd9Sstevel@tonic-gate 		chop_endslashes(Gen.g_nam_p);
45997c478bd9Sstevel@tonic-gate 
46007c478bd9Sstevel@tonic-gate 		/*
46017c478bd9Sstevel@tonic-gate 		 * Figure out parent directory
46027c478bd9Sstevel@tonic-gate 		 */
46037c478bd9Sstevel@tonic-gate 
4604ced83f9bSceastha 		if (Gen.g_attrnam_p != NULL) {
46057c478bd9Sstevel@tonic-gate 			if (Gen.g_dirfd != -1) {
46067c478bd9Sstevel@tonic-gate 				(void) close(Gen.g_dirfd);
46077c478bd9Sstevel@tonic-gate 			}
46087c478bd9Sstevel@tonic-gate 			Gen.g_dirfd = attropen(Gen.g_attrfnam_p, ".", O_RDONLY);
46097c478bd9Sstevel@tonic-gate 			if (Gen.g_dirfd == -1) {
46107c478bd9Sstevel@tonic-gate 				msg(ERRN,
46117c478bd9Sstevel@tonic-gate 				    "Cannot open attribute directory"
46127c478bd9Sstevel@tonic-gate 				    " of file %s", Gen.g_attrfnam_p);
46137c478bd9Sstevel@tonic-gate 				continue;
46147c478bd9Sstevel@tonic-gate 			}
46157c478bd9Sstevel@tonic-gate 		} else {
46167c478bd9Sstevel@tonic-gate #ifdef O_XATTR
46177c478bd9Sstevel@tonic-gate 			char dirpath[PATH_MAX];
46187c478bd9Sstevel@tonic-gate 
46197c478bd9Sstevel@tonic-gate 			get_parent(Gen.g_nam_p, dirpath);
4620ced83f9bSceastha 			if (Atflag || SysAtflag) {
46217c478bd9Sstevel@tonic-gate 				dir = dirpath;
46227c478bd9Sstevel@tonic-gate 				if (Gen.g_dirfd != -1) {
46237c478bd9Sstevel@tonic-gate 					(void) close(Gen.g_dirfd);
46247c478bd9Sstevel@tonic-gate 				}
46257c478bd9Sstevel@tonic-gate 				Gen.g_dirfd = open(dir, O_RDONLY);
46267c478bd9Sstevel@tonic-gate 				if (Gen.g_dirfd == -1) {
46277c478bd9Sstevel@tonic-gate 					msg(ERRN,
46287c478bd9Sstevel@tonic-gate 					    "Cannot open directory %s", dir);
46297c478bd9Sstevel@tonic-gate 					continue;
46307c478bd9Sstevel@tonic-gate 				}
46317c478bd9Sstevel@tonic-gate 			} else {
46327c478bd9Sstevel@tonic-gate 				/*
46337c478bd9Sstevel@tonic-gate 				 * g_dirpath is the pathname cache maintaining
46347c478bd9Sstevel@tonic-gate 				 * the dirname which is currently opened.
46357c478bd9Sstevel@tonic-gate 				 * We first check the g_dirpath to see if the
46367c478bd9Sstevel@tonic-gate 				 * given dirname matches. If so, we don't need
46377c478bd9Sstevel@tonic-gate 				 * to open the dir, but we can use the g_dirfd
46387c478bd9Sstevel@tonic-gate 				 * as is if it is still available.
46397c478bd9Sstevel@tonic-gate 				 */
46407c478bd9Sstevel@tonic-gate 				dir = NULL;
46417c478bd9Sstevel@tonic-gate 				if (Gen.g_dirpath == NULL ||
46427c478bd9Sstevel@tonic-gate 				    Gen.g_dirfd == -1) {
46437c478bd9Sstevel@tonic-gate 					/*
46447c478bd9Sstevel@tonic-gate 					 * It's the first time or it has
46457c478bd9Sstevel@tonic-gate 					 * all gone.
46467c478bd9Sstevel@tonic-gate 					 */
46477c478bd9Sstevel@tonic-gate 					dir = e_strdup(E_EXIT, dirpath);
46487c478bd9Sstevel@tonic-gate 				} else {
46497c478bd9Sstevel@tonic-gate 					if (strcmp(Gen.g_dirpath,
46507c478bd9Sstevel@tonic-gate 					    dirpath) != 0) {
46517c478bd9Sstevel@tonic-gate 						/* different directory */
46527c478bd9Sstevel@tonic-gate 						dir = e_strdup(E_EXIT, dirpath);
46537c478bd9Sstevel@tonic-gate 					}
46547c478bd9Sstevel@tonic-gate 				}
46557c478bd9Sstevel@tonic-gate 				if (dir != NULL) {
46567c478bd9Sstevel@tonic-gate 					/*
46577c478bd9Sstevel@tonic-gate 					 * We need to open the new directory.
46587c478bd9Sstevel@tonic-gate 					 * discard the pathname and dirfd
46597c478bd9Sstevel@tonic-gate 					 * for the previous directory.
46607c478bd9Sstevel@tonic-gate 					 */
46617c478bd9Sstevel@tonic-gate 					if (Gen.g_dirpath != NULL) {
46627c478bd9Sstevel@tonic-gate 						free(Gen.g_dirpath);
46637c478bd9Sstevel@tonic-gate 						Gen.g_dirpath = NULL;
46647c478bd9Sstevel@tonic-gate 					}
46657c478bd9Sstevel@tonic-gate 					if (Gen.g_dirfd != -1) {
46667c478bd9Sstevel@tonic-gate 						(void) close(Gen.g_dirfd);
46677c478bd9Sstevel@tonic-gate 					}
46687c478bd9Sstevel@tonic-gate 					/* open the new dir */
46697c478bd9Sstevel@tonic-gate 					Gen.g_dirfd = open(dir, O_RDONLY);
46707c478bd9Sstevel@tonic-gate 					if (Gen.g_dirfd == -1) {
46717c478bd9Sstevel@tonic-gate 						msg(ERRN, "Cannot open "
46727c478bd9Sstevel@tonic-gate 						    "directory %s", dir);
46737c478bd9Sstevel@tonic-gate 						continue;
46747c478bd9Sstevel@tonic-gate 					}
46757c478bd9Sstevel@tonic-gate 					Gen.g_dirpath = dir;
46767c478bd9Sstevel@tonic-gate 				}
46777c478bd9Sstevel@tonic-gate 			}
46787c478bd9Sstevel@tonic-gate #else
46797c478bd9Sstevel@tonic-gate 			Gen.g_dirfd = -1;
46807c478bd9Sstevel@tonic-gate #endif
46817c478bd9Sstevel@tonic-gate 		}
46827c478bd9Sstevel@tonic-gate 
46837c478bd9Sstevel@tonic-gate 		/* creat_hdr checks for USTAR filename length */
46847c478bd9Sstevel@tonic-gate 
46857c478bd9Sstevel@tonic-gate 		if (Hdr_type != USTAR && strlen(Gen.g_nam_p) >
46867c478bd9Sstevel@tonic-gate 		    Max_namesz) {
46877c478bd9Sstevel@tonic-gate 			if (!err) {
46887c478bd9Sstevel@tonic-gate 				msg(ERR, "%s%s%s name too long.",
4689ced83f9bSceastha 				    (Gen.g_attrnam_p == NULL) ?
46907c478bd9Sstevel@tonic-gate 				    Nam_p : Gen.g_attrfnam_p,
4691ced83f9bSceastha 				    (Gen.g_attrnam_p == NULL) ?
4692ced83f9bSceastha 				    "" : Gen.g_rw_sysattr ?
4693ced83f9bSceastha 				    gettext(" System Attribute ") :
4694ced83f9bSceastha 				    gettext(" Attribute "),
4695ced83f9bSceastha 				    (Gen.g_attrnam_p == NULL) ?
46967c478bd9Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
46977c478bd9Sstevel@tonic-gate 			}
46987c478bd9Sstevel@tonic-gate 			goodfile = 0;
46997c478bd9Sstevel@tonic-gate 			err = 1;
47007c478bd9Sstevel@tonic-gate 		}
47017c478bd9Sstevel@tonic-gate 
47027c478bd9Sstevel@tonic-gate 		if (err) {
47037c478bd9Sstevel@tonic-gate 			continue;
47047c478bd9Sstevel@tonic-gate 		} else {
47057c478bd9Sstevel@tonic-gate 			G_p = &Gen;
47067c478bd9Sstevel@tonic-gate 			if (!LSTAT(Gen.g_dirfd, Gen.g_nam_p, &SrcSt)) {
47077c478bd9Sstevel@tonic-gate 				goodfile = 1;
47087c478bd9Sstevel@tonic-gate 
47097c478bd9Sstevel@tonic-gate 				if ((SrcSt.st_mode & Ftype) == S_IFLNK) {
47107c478bd9Sstevel@tonic-gate 					issymlink = 1;
47117c478bd9Sstevel@tonic-gate 
47127c478bd9Sstevel@tonic-gate 					if ((Args & OCL)) {
47137c478bd9Sstevel@tonic-gate 						errno = 0;
47147c478bd9Sstevel@tonic-gate 						if (STAT(Gen.g_dirfd,
47157c478bd9Sstevel@tonic-gate 						    G_p->g_nam_p,
47167c478bd9Sstevel@tonic-gate 						    &SrcSt) < 0) {
47177c478bd9Sstevel@tonic-gate 							msg(ERRN,
47187c478bd9Sstevel@tonic-gate 							    "Cannot follow"
47197c478bd9Sstevel@tonic-gate 							    " \"%s%s%s\"",
47207c478bd9Sstevel@tonic-gate 							    (Gen.g_attrnam_p ==
4721ced83f9bSceastha 							    NULL) ?
47227c478bd9Sstevel@tonic-gate 							    Gen.g_nam_p :
47237c478bd9Sstevel@tonic-gate 							    Gen.g_attrfnam_p,
47247c478bd9Sstevel@tonic-gate 							    (Gen.g_attrnam_p ==
4725ced83f9bSceastha 							    NULL) ? "" :
4726ced83f9bSceastha 							    Gen.g_rw_sysattr ?
4727ced83f9bSceastha 							    gettext(
4728ced83f9bSceastha 							    " System "
4729ced83f9bSceastha 							    "Attribute ") :
47307c478bd9Sstevel@tonic-gate 							    gettext(
47317c478bd9Sstevel@tonic-gate 							    " Attribute "),
47327c478bd9Sstevel@tonic-gate 							    (Gen.g_attrnam_p ==
4733ced83f9bSceastha 							    NULL) ? "" :
47347c478bd9Sstevel@tonic-gate 							    Gen.g_attrnam_p);
47357c478bd9Sstevel@tonic-gate 							goodfile = 0;
47367c478bd9Sstevel@tonic-gate 						}
47377c478bd9Sstevel@tonic-gate 					}
47387c478bd9Sstevel@tonic-gate 				}
47397c478bd9Sstevel@tonic-gate 
47407c478bd9Sstevel@tonic-gate 				if (Use_old_stat) {
47417c478bd9Sstevel@tonic-gate 					OldSt = convert_to_old_stat(&SrcSt,
47427c478bd9Sstevel@tonic-gate 					    Gen.g_nam_p, Gen.g_attrnam_p);
47437c478bd9Sstevel@tonic-gate 
47447c478bd9Sstevel@tonic-gate 					if (OldSt == NULL) {
47457c478bd9Sstevel@tonic-gate 						goodfile = 0;
47467c478bd9Sstevel@tonic-gate 					}
47477c478bd9Sstevel@tonic-gate 				}
47487c478bd9Sstevel@tonic-gate 			} else {
47497c478bd9Sstevel@tonic-gate 				msg(ERRN,
47507c478bd9Sstevel@tonic-gate 				    "Error with fstatat() of \"%s%s%s\"",
4751ced83f9bSceastha 				    (Gen.g_attrnam_p == NULL) ?
47527c478bd9Sstevel@tonic-gate 				    Gen.g_nam_p : Gen.g_attrfnam_p,
4753ced83f9bSceastha 				    (Gen.g_attrnam_p == NULL) ? "" :
4754ced83f9bSceastha 				    Gen.g_rw_sysattr ?
4755ced83f9bSceastha 				    gettext(" System Attribute ") :
4756ced83f9bSceastha 				    gettext(" Attribute "),
4757ced83f9bSceastha 				    (Gen.g_attrnam_p == NULL) ?
47587c478bd9Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
47597c478bd9Sstevel@tonic-gate 			}
47607c478bd9Sstevel@tonic-gate 		}
47617c478bd9Sstevel@tonic-gate 	}
47627c478bd9Sstevel@tonic-gate 
47637c478bd9Sstevel@tonic-gate 	/*
47647c478bd9Sstevel@tonic-gate 	 * Get ACL info: dont bother allocating space if there are only
47657c478bd9Sstevel@tonic-gate 	 * standard permissions, i.e. ACL count < 4
47667c478bd9Sstevel@tonic-gate 	 */
47677c478bd9Sstevel@tonic-gate 	if ((SrcSt.st_mode & Ftype) != S_IFLNK && Pflag) {
4768fa9e4066Sahrens 		if (acl_get(Gen.g_nam_p, ACL_NO_TRIVIAL, &aclp) != 0)
47697c478bd9Sstevel@tonic-gate 			msg(ERRN, "Error with acl() of \"%s\"", Gen.g_nam_p);
47707c478bd9Sstevel@tonic-gate 	}
47717c478bd9Sstevel@tonic-gate 	/* else: only traditional permissions, so proceed as usual */
47727c478bd9Sstevel@tonic-gate 	if (creat_hdr())
47737c478bd9Sstevel@tonic-gate 		return (1);
47747c478bd9Sstevel@tonic-gate 	else return (2);
47757c478bd9Sstevel@tonic-gate }
47767c478bd9Sstevel@tonic-gate 
47777c478bd9Sstevel@tonic-gate /*
47787c478bd9Sstevel@tonic-gate  * getpats: Save any filenames/patterns specified as arguments.
47797c478bd9Sstevel@tonic-gate  * Read additional filenames/patterns from the file specified by the
47807c478bd9Sstevel@tonic-gate  * user.  The filenames/patterns must occur one per line.
47817c478bd9Sstevel@tonic-gate  */
47827c478bd9Sstevel@tonic-gate 
47837c478bd9Sstevel@tonic-gate static void
47847c478bd9Sstevel@tonic-gate getpats(int largc, char **largv)
47857c478bd9Sstevel@tonic-gate {
47867c478bd9Sstevel@tonic-gate 	char **t_pp;
47877c478bd9Sstevel@tonic-gate 	size_t len;
47887c478bd9Sstevel@tonic-gate 	unsigned numpat = largc, maxpat = largc + 2;
47897c478bd9Sstevel@tonic-gate 
47907c478bd9Sstevel@tonic-gate 	Pat_pp = e_zalloc(E_EXIT, maxpat * sizeof (char *));
47917c478bd9Sstevel@tonic-gate 	t_pp = Pat_pp;
47927c478bd9Sstevel@tonic-gate 	while (*largv) {
47937c478bd9Sstevel@tonic-gate 		*t_pp = e_zalloc(E_EXIT, strlen(*largv) + 1);
47947c478bd9Sstevel@tonic-gate 		(void) strcpy(*t_pp, *largv);
47957c478bd9Sstevel@tonic-gate 		t_pp++;
47967c478bd9Sstevel@tonic-gate 		largv++;
47977c478bd9Sstevel@tonic-gate 	}
4798ced83f9bSceastha 	while (fgets(Nam_p, Max_namesz + 1, Ef_p) != NULL) {
47997c478bd9Sstevel@tonic-gate 		if (numpat == maxpat - 1) {
48007c478bd9Sstevel@tonic-gate 			maxpat += 10;
48017c478bd9Sstevel@tonic-gate 			Pat_pp = e_realloc(E_EXIT, Pat_pp,
48027c478bd9Sstevel@tonic-gate 			    maxpat * sizeof (char *));
48037c478bd9Sstevel@tonic-gate 			t_pp = Pat_pp + numpat;
48047c478bd9Sstevel@tonic-gate 		}
48057c478bd9Sstevel@tonic-gate 		len = strlen(Nam_p); /* includes the \n */
48067c478bd9Sstevel@tonic-gate 		*(Nam_p + len - 1) = '\0'; /* remove the \n */
48077c478bd9Sstevel@tonic-gate 		*t_pp = e_zalloc(E_EXIT, len);
48087c478bd9Sstevel@tonic-gate 		(void) strcpy(*t_pp, Nam_p);
48097c478bd9Sstevel@tonic-gate 		t_pp++;
48107c478bd9Sstevel@tonic-gate 		numpat++;
48117c478bd9Sstevel@tonic-gate 	}
4812ced83f9bSceastha 	*t_pp = NULL;
48137c478bd9Sstevel@tonic-gate }
48147c478bd9Sstevel@tonic-gate 
48157c478bd9Sstevel@tonic-gate static void
48167c478bd9Sstevel@tonic-gate ioerror(int dir)
48177c478bd9Sstevel@tonic-gate {
48187c478bd9Sstevel@tonic-gate 	int t_errno;
48197c478bd9Sstevel@tonic-gate 
48207c478bd9Sstevel@tonic-gate 	t_errno = errno;
48217c478bd9Sstevel@tonic-gate 	errno = 0;
48227c478bd9Sstevel@tonic-gate 	if (fstat(Archive, &ArchSt) < 0)
48237c478bd9Sstevel@tonic-gate 		msg(EXTN, "Error during stat() of archive");
48247c478bd9Sstevel@tonic-gate 	errno = t_errno;
48257c478bd9Sstevel@tonic-gate 	if ((ArchSt.st_mode & Ftype) != S_IFCHR) {
48267c478bd9Sstevel@tonic-gate 		if (dir) {
48277c478bd9Sstevel@tonic-gate 			if (errno == EFBIG)
48287c478bd9Sstevel@tonic-gate 				msg(EXT, "ulimit reached for output file.");
48297c478bd9Sstevel@tonic-gate 			else if (errno == ENOSPC)
48307c478bd9Sstevel@tonic-gate 				msg(EXT, "No space left for output file.");
48317c478bd9Sstevel@tonic-gate 			else
48327c478bd9Sstevel@tonic-gate 				msg(EXTN, "I/O error - cannot continue");
48337c478bd9Sstevel@tonic-gate 		} else
48347c478bd9Sstevel@tonic-gate 			msg(EXT, "Unexpected end-of-file encountered.");
48357c478bd9Sstevel@tonic-gate 	} else
48367c478bd9Sstevel@tonic-gate 		msg(EXTN, "\007I/O error on \"%s\"", dir ? "output" : "input");
48377c478bd9Sstevel@tonic-gate }
48387c478bd9Sstevel@tonic-gate 
48397c478bd9Sstevel@tonic-gate /*
48407c478bd9Sstevel@tonic-gate  * matched: Determine if a filename matches the specified pattern(s).  If the
48417c478bd9Sstevel@tonic-gate  * pattern is matched (the second return), return 0 if -f was specified, else
48427c478bd9Sstevel@tonic-gate  * return != 0.  If the pattern is not matched (the first and third
48437c478bd9Sstevel@tonic-gate  * returns), return 0 if -f was not specified, else return != 0.
48447c478bd9Sstevel@tonic-gate  */
48457c478bd9Sstevel@tonic-gate 
48467c478bd9Sstevel@tonic-gate static int
48477c478bd9Sstevel@tonic-gate matched(void)
48487c478bd9Sstevel@tonic-gate {
48497c478bd9Sstevel@tonic-gate 	char *str_p = G_p->g_nam_p;
48507c478bd9Sstevel@tonic-gate 	char **pat_pp = Pat_pp;
48517c478bd9Sstevel@tonic-gate 	int negatep, result;
48527c478bd9Sstevel@tonic-gate 
48537c478bd9Sstevel@tonic-gate 	/*
48547c478bd9Sstevel@tonic-gate 	 * Check for attribute
48557c478bd9Sstevel@tonic-gate 	 */
4856ced83f9bSceastha 	if (G_p->g_attrfnam_p != NULL)
48577c478bd9Sstevel@tonic-gate 		str_p = G_p->g_attrfnam_p;
48587c478bd9Sstevel@tonic-gate 
48597c478bd9Sstevel@tonic-gate 	for (pat_pp = Pat_pp; *pat_pp; pat_pp++) {
48607c478bd9Sstevel@tonic-gate 		negatep = (**pat_pp == '!');
48617c478bd9Sstevel@tonic-gate 
48627c478bd9Sstevel@tonic-gate 		result = fnmatch(negatep ? (*pat_pp+1) : *pat_pp, str_p, 0);
48637c478bd9Sstevel@tonic-gate 
48647c478bd9Sstevel@tonic-gate 		if (result != 0 && result != FNM_NOMATCH) {
48657c478bd9Sstevel@tonic-gate 			msg(POST, "error matching file %s with pattern"
48667c478bd9Sstevel@tonic-gate 			    " %s\n", str_p, *pat_pp);
48677c478bd9Sstevel@tonic-gate 			return (Args & OCf);
48687c478bd9Sstevel@tonic-gate 		}
48697c478bd9Sstevel@tonic-gate 
48707c478bd9Sstevel@tonic-gate 		if ((result == 0 && ! negatep) ||
48717c478bd9Sstevel@tonic-gate 		    (result == FNM_NOMATCH && negatep)) {
4872da6c28aaSamw 			/* match occurred */
48737c478bd9Sstevel@tonic-gate 			return (!(Args & OCf));
48747c478bd9Sstevel@tonic-gate 		}
48757c478bd9Sstevel@tonic-gate 	}
48767c478bd9Sstevel@tonic-gate 	return (Args & OCf); /* not matched */
48777c478bd9Sstevel@tonic-gate }
48787c478bd9Sstevel@tonic-gate 
48797c478bd9Sstevel@tonic-gate /*
48807c478bd9Sstevel@tonic-gate  * missdir: Create missing directories for files.
48817c478bd9Sstevel@tonic-gate  * (Possible future performance enhancement, if missdir is called, we know
48827c478bd9Sstevel@tonic-gate  * that at least the very last directory of the path does not exist, therefore,
48837c478bd9Sstevel@tonic-gate  * scan the path from the end
48847c478bd9Sstevel@tonic-gate  */
48857c478bd9Sstevel@tonic-gate 
48867c478bd9Sstevel@tonic-gate static int
48877c478bd9Sstevel@tonic-gate missdir(char *nam_p)
48887c478bd9Sstevel@tonic-gate {
48897c478bd9Sstevel@tonic-gate 	char *c_p;
48907c478bd9Sstevel@tonic-gate 	int cnt = 2;
48917c478bd9Sstevel@tonic-gate 	char *lastp;
48927c478bd9Sstevel@tonic-gate 
48937c478bd9Sstevel@tonic-gate 	if (*(c_p = nam_p) == '/') /* skip over 'root slash' */
48947c478bd9Sstevel@tonic-gate 		c_p++;
48957c478bd9Sstevel@tonic-gate 
48967c478bd9Sstevel@tonic-gate 	lastp = c_p + strlen(nam_p) - 1;
48977c478bd9Sstevel@tonic-gate 	if (*lastp == '/')
48987c478bd9Sstevel@tonic-gate 		*lastp = '\0';
48997c478bd9Sstevel@tonic-gate 
49007c478bd9Sstevel@tonic-gate 	for (; *c_p; ++c_p) {
49017c478bd9Sstevel@tonic-gate 		if (*c_p == '/') {
49027c478bd9Sstevel@tonic-gate 			*c_p = '\0';
49037c478bd9Sstevel@tonic-gate 			if (stat(nam_p, &DesSt) < 0) {
49047c478bd9Sstevel@tonic-gate 				if (Args & OCd) {
49057c478bd9Sstevel@tonic-gate 					cnt = mkdir(nam_p, Def_mode);
49067c478bd9Sstevel@tonic-gate 					if (cnt != 0) {
49077c478bd9Sstevel@tonic-gate 						*c_p = '/';
49087c478bd9Sstevel@tonic-gate 						return (cnt);
49097c478bd9Sstevel@tonic-gate 					}
49107c478bd9Sstevel@tonic-gate 				} else {
49117c478bd9Sstevel@tonic-gate 					msg(ERR, "Missing -d option.");
49127c478bd9Sstevel@tonic-gate 					*c_p = '/';
49137c478bd9Sstevel@tonic-gate 					return (-1);
49147c478bd9Sstevel@tonic-gate 				}
49157c478bd9Sstevel@tonic-gate 			}
49167c478bd9Sstevel@tonic-gate 			*c_p = '/';
49177c478bd9Sstevel@tonic-gate 		}
49187c478bd9Sstevel@tonic-gate 	}
49197c478bd9Sstevel@tonic-gate 	if (cnt == 2) /* the file already exists */
49207c478bd9Sstevel@tonic-gate 		cnt = 0;
49217c478bd9Sstevel@tonic-gate 	return (cnt);
49227c478bd9Sstevel@tonic-gate }
49237c478bd9Sstevel@tonic-gate 
49247c478bd9Sstevel@tonic-gate /*
49257c478bd9Sstevel@tonic-gate  * mklong: Convert two shorts into one long.  For VAX, Interdata ...
49267c478bd9Sstevel@tonic-gate  */
49277c478bd9Sstevel@tonic-gate 
49287c478bd9Sstevel@tonic-gate static long
49297c478bd9Sstevel@tonic-gate mklong(short v[])
49307c478bd9Sstevel@tonic-gate {
49317c478bd9Sstevel@tonic-gate 
49327c478bd9Sstevel@tonic-gate 	union swpbuf swp_b;
49337c478bd9Sstevel@tonic-gate 
49347c478bd9Sstevel@tonic-gate 	swp_b.s_word = 1;
49357c478bd9Sstevel@tonic-gate 	if (swp_b.s_byte[0]) {
49367c478bd9Sstevel@tonic-gate 		swp_b.s_half[0] = v[1];
49377c478bd9Sstevel@tonic-gate 		swp_b.s_half[1] = v[0];
49387c478bd9Sstevel@tonic-gate 	} else {
49397c478bd9Sstevel@tonic-gate 		swp_b.s_half[0] = v[0];
49407c478bd9Sstevel@tonic-gate 		swp_b.s_half[1] = v[1];
49417c478bd9Sstevel@tonic-gate 	}
49427c478bd9Sstevel@tonic-gate 	return (swp_b.s_word);
49437c478bd9Sstevel@tonic-gate }
49447c478bd9Sstevel@tonic-gate 
49457c478bd9Sstevel@tonic-gate /*
49467c478bd9Sstevel@tonic-gate  * mkshort: Convert a long into 2 shorts, for VAX, Interdata ...
49477c478bd9Sstevel@tonic-gate  */
49487c478bd9Sstevel@tonic-gate 
49497c478bd9Sstevel@tonic-gate static void
49507c478bd9Sstevel@tonic-gate mkshort(short sval[], long v)
49517c478bd9Sstevel@tonic-gate {
49527c478bd9Sstevel@tonic-gate 	union swpbuf *swp_p, swp_b;
49537c478bd9Sstevel@tonic-gate 
49547c478bd9Sstevel@tonic-gate 	/* LINTED alignment */
49557c478bd9Sstevel@tonic-gate 	swp_p = (union swpbuf *)sval;
49567c478bd9Sstevel@tonic-gate 	swp_b.s_word = 1;
49577c478bd9Sstevel@tonic-gate 	if (swp_b.s_byte[0]) {
49587c478bd9Sstevel@tonic-gate 		swp_b.s_word = v;
49597c478bd9Sstevel@tonic-gate 		swp_p->s_half[0] = swp_b.s_half[1];
49607c478bd9Sstevel@tonic-gate 		swp_p->s_half[1] = swp_b.s_half[0];
49617c478bd9Sstevel@tonic-gate 	} else {
49627c478bd9Sstevel@tonic-gate 		swp_b.s_word = v;
49637c478bd9Sstevel@tonic-gate 		swp_p->s_half[0] = swp_b.s_half[0];
49647c478bd9Sstevel@tonic-gate 		swp_p->s_half[1] = swp_b.s_half[1];
49657c478bd9Sstevel@tonic-gate 	}
49667c478bd9Sstevel@tonic-gate }
49677c478bd9Sstevel@tonic-gate 
49687c478bd9Sstevel@tonic-gate /*
49697c478bd9Sstevel@tonic-gate  * msg: Print either a message (no error) (POST), an error message with or
49707c478bd9Sstevel@tonic-gate  * without the errno (ERRN or ERR), or print an error message with or without
49717c478bd9Sstevel@tonic-gate  * the errno and exit (EXTN or EXT).
49727c478bd9Sstevel@tonic-gate  */
49737c478bd9Sstevel@tonic-gate 
49747c478bd9Sstevel@tonic-gate static void
49757c478bd9Sstevel@tonic-gate msg(int severity, const char *fmt, ...)
49767c478bd9Sstevel@tonic-gate {
49777c478bd9Sstevel@tonic-gate 	FILE *file_p;
49787c478bd9Sstevel@tonic-gate 	va_list ap;
49797c478bd9Sstevel@tonic-gate 
49807c478bd9Sstevel@tonic-gate 	if ((Args & OCV) && Verbcnt) { /* clear current line of dots */
49817c478bd9Sstevel@tonic-gate 		(void) fputc('\n', Out_p);
49827c478bd9Sstevel@tonic-gate 		Verbcnt = 0;
49837c478bd9Sstevel@tonic-gate 	}
49847c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
49857c478bd9Sstevel@tonic-gate 	if (severity == POST)
49867c478bd9Sstevel@tonic-gate 		file_p = Out_p;
49877c478bd9Sstevel@tonic-gate 	else
49887c478bd9Sstevel@tonic-gate 		if (severity == EPOST)
49897c478bd9Sstevel@tonic-gate 			file_p = Err_p;
49907c478bd9Sstevel@tonic-gate 		else {
49917c478bd9Sstevel@tonic-gate 			file_p = Err_p;
49927c478bd9Sstevel@tonic-gate 			Error_cnt++;
49937c478bd9Sstevel@tonic-gate 		}
49947c478bd9Sstevel@tonic-gate 	(void) fflush(Out_p);
49957c478bd9Sstevel@tonic-gate 	(void) fflush(Err_p);
49967c478bd9Sstevel@tonic-gate 	if ((severity != POST) && (severity != EPOST))
49977c478bd9Sstevel@tonic-gate 		(void) fprintf(file_p, "cpio: ");
49987c478bd9Sstevel@tonic-gate 
49997c478bd9Sstevel@tonic-gate 	/* gettext replaces version of string */
50007c478bd9Sstevel@tonic-gate 
50017c478bd9Sstevel@tonic-gate 	(void) vfprintf(file_p, gettext(fmt), ap);
50027c478bd9Sstevel@tonic-gate 	if (severity == ERRN || severity == EXTN) {
5003ced83f9bSceastha 		if ((G_p->g_attrnam_p != NULL) && G_p->g_rw_sysattr) {
5004ced83f9bSceastha 			if (errno == EPERM) {
5005ced83f9bSceastha 				(void) fprintf(file_p, ", errno %d, %s", errno,
5006ced83f9bSceastha 				    gettext("insufficient privileges\n"));
5007ced83f9bSceastha 			} else if (errno == EINVAL) {
5008ced83f9bSceastha 				(void) fprintf(file_p, ", errno %d, %s",
5009ced83f9bSceastha 				    errno, gettext(
5010ced83f9bSceastha 				    "unsupported on underlying file system\n"));
5011ced83f9bSceastha 			} else {
50127c478bd9Sstevel@tonic-gate 				(void) fprintf(file_p, ", errno %d, ", errno);
50137c478bd9Sstevel@tonic-gate 				perror("");
5014ced83f9bSceastha 			}
5015ced83f9bSceastha 		} else {
5016ced83f9bSceastha 			(void) fprintf(file_p, ", errno %d, ", errno);
5017ced83f9bSceastha 			perror("");
5018ced83f9bSceastha 		}
50197c478bd9Sstevel@tonic-gate 	} else
50207c478bd9Sstevel@tonic-gate 		(void) fprintf(file_p, "\n");
50217c478bd9Sstevel@tonic-gate 	(void) fflush(file_p);
50227c478bd9Sstevel@tonic-gate 	va_end(ap);
50237c478bd9Sstevel@tonic-gate 	if (severity == EXT || severity == EXTN) {
50247c478bd9Sstevel@tonic-gate 		(void) fprintf(file_p, gettext("%d errors\n"), Error_cnt);
50257c478bd9Sstevel@tonic-gate 		exit(EXIT_CODE);
50267c478bd9Sstevel@tonic-gate 	}
50277c478bd9Sstevel@tonic-gate }
50287c478bd9Sstevel@tonic-gate 
50297c478bd9Sstevel@tonic-gate /*
50307c478bd9Sstevel@tonic-gate  * openout: Open files for output and set all necessary information.
50317c478bd9Sstevel@tonic-gate  * If the u option is set (unconditionally overwrite existing files),
50327c478bd9Sstevel@tonic-gate  * and the current file exists, get a temporary file name from mktemp(3C),
50337c478bd9Sstevel@tonic-gate  * link the temporary file to the existing file, and remove the existing file.
50347c478bd9Sstevel@tonic-gate  * Finally either creat(2), mkdir(2) or mknod(2) as appropriate.
50357c478bd9Sstevel@tonic-gate  *
50367c478bd9Sstevel@tonic-gate  */
50377c478bd9Sstevel@tonic-gate 
50387c478bd9Sstevel@tonic-gate static int
50397c478bd9Sstevel@tonic-gate openout(int dirfd)
50407c478bd9Sstevel@tonic-gate {
50417c478bd9Sstevel@tonic-gate 	char *nam_p;
50427c478bd9Sstevel@tonic-gate 	int cnt, result;
50437c478bd9Sstevel@tonic-gate 
50447c478bd9Sstevel@tonic-gate 	Do_rename = 0;	/* creat_tmp() may reset this */
50457c478bd9Sstevel@tonic-gate 
5046ced83f9bSceastha 	if (G_p->g_attrnam_p != NULL) {
50477c478bd9Sstevel@tonic-gate 		nam_p = G_p->g_attrnam_p;
50487c478bd9Sstevel@tonic-gate 	} else {
50497c478bd9Sstevel@tonic-gate 		if (Args & OCp) {
50507c478bd9Sstevel@tonic-gate 			nam_p = Fullnam_p;
50517c478bd9Sstevel@tonic-gate 		} else {
50527c478bd9Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
50537c478bd9Sstevel@tonic-gate 		}
50547c478bd9Sstevel@tonic-gate 	}
50557c478bd9Sstevel@tonic-gate 
50567c478bd9Sstevel@tonic-gate 
50577c478bd9Sstevel@tonic-gate 	if ((Max_filesz != RLIM_INFINITY) &&
50587c478bd9Sstevel@tonic-gate 	    (Max_filesz < (G_p->g_filesz >> 9))) {
50597c478bd9Sstevel@tonic-gate 		/* ... divided by 512 ... */
50607c478bd9Sstevel@tonic-gate 		msg(ERR, "Skipping \"%s%s%s\": exceeds ulimit by %lld bytes",
5061ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? nam_p : G_p->g_attrfnam_p,
5062ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? "" : G_p->g_rw_sysattr ?
5063ced83f9bSceastha 		    gettext(" System Attribute ") : gettext(" Attribute "),
5064ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? "" : nam_p,
50657c478bd9Sstevel@tonic-gate 		    (off_t)(G_p->g_filesz - (Max_filesz << 9)));
50667c478bd9Sstevel@tonic-gate 		return (-1);
50677c478bd9Sstevel@tonic-gate 	}
50687c478bd9Sstevel@tonic-gate 
50697c478bd9Sstevel@tonic-gate 	if (LSTAT(dirfd, nam_p, &DesSt) == 0) {
50707c478bd9Sstevel@tonic-gate 		/*
50717c478bd9Sstevel@tonic-gate 		 * A file by the same name exists.  Move it to a temporary
5072ced83f9bSceastha 		 * file unless it's a system attribute file.  If we are
5073ced83f9bSceastha 		 * restoring a system attribute file on a file system that
5074ced83f9bSceastha 		 * supports system attributes, then the system attribute file
5075ced83f9bSceastha 		 * will already exist (a default system attribute file will
5076ced83f9bSceastha 		 * get created when the file it is associated with is created).
5077ced83f9bSceastha 		 * If we create a temporary system attribute file, we can't
5078ced83f9bSceastha 		 * overwrite the existing system attribute file using
5079ced83f9bSceastha 		 * renameat().  In addition, only system attributes can exist
5080ced83f9bSceastha 		 * for an attribute of a file, therefore, a temporary file
5081ced83f9bSceastha 		 * cannot be created for a system attribute of an attribute.
5082ced83f9bSceastha 		 * Thus, when restoring a system attribute, we won't move it
5083ced83f9bSceastha 		 * to a temporary file, but will attempt to process it as if
5084ced83f9bSceastha 		 * it didn't already exist.
50857c478bd9Sstevel@tonic-gate 		 */
50867c478bd9Sstevel@tonic-gate 
5087ced83f9bSceastha #if defined(_PC_SATTR_ENABLED)
5088ced83f9bSceastha 		if (G_p->g_rw_sysattr == 0)
5089ced83f9bSceastha #endif	/* _PC_SATTR_ENABLED */
50907c478bd9Sstevel@tonic-gate 			if (creat_tmp(nam_p) < 0) {
50917c478bd9Sstevel@tonic-gate 				/*
5092ced83f9bSceastha 				 * We weren't able to create the temp file.
5093ced83f9bSceastha 				 * Report failure.
50947c478bd9Sstevel@tonic-gate 				 */
50957c478bd9Sstevel@tonic-gate 
50967c478bd9Sstevel@tonic-gate 				return (-1);
50977c478bd9Sstevel@tonic-gate 			}
50987c478bd9Sstevel@tonic-gate 	}
50997c478bd9Sstevel@tonic-gate 
51007c478bd9Sstevel@tonic-gate 	if (Do_rename) {
51017c478bd9Sstevel@tonic-gate 		/* nam_p was changed by creat_tmp() above. */
51027c478bd9Sstevel@tonic-gate 
51037c478bd9Sstevel@tonic-gate 		if (Args & OCp) {
5104ced83f9bSceastha 			if (G_p->g_attrnam_p != NULL) {
51057c478bd9Sstevel@tonic-gate 				nam_p = Attrfile_p;
51067c478bd9Sstevel@tonic-gate 			} else {
51077c478bd9Sstevel@tonic-gate 				nam_p = Fullnam_p;
51087c478bd9Sstevel@tonic-gate 			}
51097c478bd9Sstevel@tonic-gate 		} else {
51107c478bd9Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
51117c478bd9Sstevel@tonic-gate 		}
51127c478bd9Sstevel@tonic-gate 	}
51137c478bd9Sstevel@tonic-gate 
51147c478bd9Sstevel@tonic-gate 	/*
51157c478bd9Sstevel@tonic-gate 	 * This pile tries to create the file directly, and, if there is a
51167c478bd9Sstevel@tonic-gate 	 * problem, creates missing directories, and then tries to create the
51177c478bd9Sstevel@tonic-gate 	 * file again.  Two strikes and you're out.
51187c478bd9Sstevel@tonic-gate 	 *
51197c478bd9Sstevel@tonic-gate 	 * On XATTR system, the directory has already been created by
51207c478bd9Sstevel@tonic-gate 	 * open_dirfd(), so error shouldn't happen in the loop. However,
51217c478bd9Sstevel@tonic-gate 	 * on non-XATTR system, symlink/open may fail with ENOENT. In such
51227c478bd9Sstevel@tonic-gate 	 * case, we go to create missing directories.
51237c478bd9Sstevel@tonic-gate 	 */
51247c478bd9Sstevel@tonic-gate 
51257c478bd9Sstevel@tonic-gate 	cnt = 0;
51267c478bd9Sstevel@tonic-gate 
51277c478bd9Sstevel@tonic-gate 	do {
51287c478bd9Sstevel@tonic-gate 		errno = 0;
51297c478bd9Sstevel@tonic-gate 
51307c478bd9Sstevel@tonic-gate 		if (Hdr_type == TAR && Thdr_p->tbuf.t_typeflag == SYMTYPE) {
51317c478bd9Sstevel@tonic-gate 			/* The archive file is a TAR symlink. */
51327c478bd9Sstevel@tonic-gate 			if ((result =
51337c478bd9Sstevel@tonic-gate 			    symlink(Thdr_p->tbuf.t_linkname, nam_p)) >= 0) {
51347c478bd9Sstevel@tonic-gate 				cnt = 0;
51357c478bd9Sstevel@tonic-gate 				if (Over_p != NULL) {
51367c478bd9Sstevel@tonic-gate 					(void) unlinkat(dirfd,
51377c478bd9Sstevel@tonic-gate 					    get_component(Over_p), 0);
51387c478bd9Sstevel@tonic-gate 					*Over_p = '\0';
51397c478bd9Sstevel@tonic-gate 				}
51407c478bd9Sstevel@tonic-gate 				break;
51417c478bd9Sstevel@tonic-gate 			} else if (errno != ENOENT) {
51427c478bd9Sstevel@tonic-gate 				/* The attempt to symlink failed. */
51437c478bd9Sstevel@tonic-gate 				msg(ERRN,
51447c478bd9Sstevel@tonic-gate 				    "Cannot create symbolic link \"%s\" -> "
51457c478bd9Sstevel@tonic-gate 				    "\"%s\"",
51467c478bd9Sstevel@tonic-gate 				    Thdr_p->tbuf.t_linkname, nam_p);
51477c478bd9Sstevel@tonic-gate 
51487c478bd9Sstevel@tonic-gate 				if (*Over_p != '\0') {
51497c478bd9Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
51507c478bd9Sstevel@tonic-gate 				}
51517c478bd9Sstevel@tonic-gate 				return (-1);
51527c478bd9Sstevel@tonic-gate 			}
51537c478bd9Sstevel@tonic-gate 		} else if (Hdr_type == BAR && bar_linkflag == SYMTYPE) {
51547c478bd9Sstevel@tonic-gate 			if ((result = symlink(bar_linkname, nam_p)) >= 0) {
51557c478bd9Sstevel@tonic-gate 				cnt = 0;
51567c478bd9Sstevel@tonic-gate 				if (Over_p != NULL) {
51577c478bd9Sstevel@tonic-gate 					(void) unlinkat(dirfd,
51587c478bd9Sstevel@tonic-gate 					    get_component(Over_p), 0);
51597c478bd9Sstevel@tonic-gate 					*Over_p = '\0';
51607c478bd9Sstevel@tonic-gate 				}
51617c478bd9Sstevel@tonic-gate 				break;
51627c478bd9Sstevel@tonic-gate 			} else if (errno != ENOENT) {
51637c478bd9Sstevel@tonic-gate 				/* The attempt to symlink failed. */
51647c478bd9Sstevel@tonic-gate 				msg(ERRN,
51657c478bd9Sstevel@tonic-gate 				    "Cannot create symbolic link \"%s\" -> "
51667c478bd9Sstevel@tonic-gate 				    "\"%s\"",
51677c478bd9Sstevel@tonic-gate 				    bar_linkname, nam_p);
51687c478bd9Sstevel@tonic-gate 				if (*Over_p != '\0') {
51697c478bd9Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
51707c478bd9Sstevel@tonic-gate 				}
51717c478bd9Sstevel@tonic-gate 				return (-1);
51727c478bd9Sstevel@tonic-gate 			}
51737c478bd9Sstevel@tonic-gate 		} else if ((G_p->g_mode & Ftype) == S_IFLNK) {
51747c478bd9Sstevel@tonic-gate 			if ((!(Args & OCp)) && !(Hdr_type == USTAR)) {
5175*32fd2847Snakanon 				FILL(G_p->g_filesz);
51767c478bd9Sstevel@tonic-gate 				(void) strncpy(Symlnk_p,
51777c478bd9Sstevel@tonic-gate 				    Buffr.b_out_p, G_p->g_filesz);
51787c478bd9Sstevel@tonic-gate 				*(Symlnk_p + G_p->g_filesz) = '\0';
51797c478bd9Sstevel@tonic-gate 			} else if ((!(Args & OCp)) && (Hdr_type == USTAR)) {
51807c478bd9Sstevel@tonic-gate 				Symlnk_p[NAMSIZ] = '\0';
51817c478bd9Sstevel@tonic-gate 				(void) strncpy(Symlnk_p,
51827c478bd9Sstevel@tonic-gate 				    &Thdr_p->tbuf.t_linkname[0], NAMSIZ);
51837c478bd9Sstevel@tonic-gate 			}
51847c478bd9Sstevel@tonic-gate 			if ((result = symlink(Symlnk_p, nam_p)) >= 0) {
51857c478bd9Sstevel@tonic-gate 				cnt = 0;
51867c478bd9Sstevel@tonic-gate 				if (Over_p != NULL) {
51877c478bd9Sstevel@tonic-gate 					(void) unlinkat(dirfd,
51887c478bd9Sstevel@tonic-gate 					    get_component(Over_p), 0);
51897c478bd9Sstevel@tonic-gate 					*Over_p = '\0';
51907c478bd9Sstevel@tonic-gate 				}
51917c478bd9Sstevel@tonic-gate 				break;
51927c478bd9Sstevel@tonic-gate 			} else if (errno != ENOENT) {
51937c478bd9Sstevel@tonic-gate 				/* The attempt to symlink failed. */
51947c478bd9Sstevel@tonic-gate 				msg(ERRN,
51957c478bd9Sstevel@tonic-gate 				    "Cannot create symbolic link \"%s\" -> "
51967c478bd9Sstevel@tonic-gate 				    "\"%s\"",
51977c478bd9Sstevel@tonic-gate 				    Symlnk_p, nam_p);
51987c478bd9Sstevel@tonic-gate 
51997c478bd9Sstevel@tonic-gate 				if (*Over_p != '\0') {
52007c478bd9Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
52017c478bd9Sstevel@tonic-gate 				}
52027c478bd9Sstevel@tonic-gate 				return (-1);
52037c478bd9Sstevel@tonic-gate 			}
52047c478bd9Sstevel@tonic-gate 		} else {
5205ced83f9bSceastha 			int	saveerrno;
5206ced83f9bSceastha 
52077c478bd9Sstevel@tonic-gate 			if ((result = openat(dirfd, get_component(nam_p),
5208ced83f9bSceastha 			    O_CREAT|O_RDWR|O_TRUNC, (int)G_p->g_mode)) < 0) {
5209ced83f9bSceastha 				saveerrno = errno;
5210ced83f9bSceastha 				if (G_p->g_attrnam_p != NULL)  {
5211ced83f9bSceastha 					result = retry_open_attr(dirfd,
5212ced83f9bSceastha 					    Gen.g_baseparent_fd, Fullnam_p,
5213ced83f9bSceastha 					    (G_p->g_attrparent_p == NULL) ?
5214ced83f9bSceastha 					    NULL : G_p->g_attrparent_p, nam_p,
5215ced83f9bSceastha 					    O_CREAT|O_RDWR|O_TRUNC,
5216ced83f9bSceastha 					    (int)G_p->g_mode);
5217ced83f9bSceastha 				}
5218ced83f9bSceastha 			}
5219ced83f9bSceastha 			if (result < 0) {
5220ced83f9bSceastha 				errno = saveerrno;
5221ced83f9bSceastha 				if (errno != ENOENT) {
5222ced83f9bSceastha 					/* The attempt to open failed. */
5223ced83f9bSceastha 					msg(ERRN, "Cannot open file \"%s\"",
5224ced83f9bSceastha 					    nam_p);
5225ced83f9bSceastha 					if (*Over_p != '\0') {
5226ced83f9bSceastha 						rstfiles(U_KEEP, dirfd);
5227ced83f9bSceastha 					}
5228ced83f9bSceastha 					return (-1);
5229ced83f9bSceastha 				}
5230ced83f9bSceastha 			} else {
52317c478bd9Sstevel@tonic-gate 				/* acl support */
5232fa9e4066Sahrens 				acl_is_set = 0;
52337c478bd9Sstevel@tonic-gate 				if (Pflag && aclp != NULL) {
5234fa9e4066Sahrens 					if (facl_set(result, aclp) < 0) {
52357c478bd9Sstevel@tonic-gate 						msg(ERRN,
52367c478bd9Sstevel@tonic-gate 						    "\"%s\": failed to set acl",
52377c478bd9Sstevel@tonic-gate 						    nam_p);
52387c478bd9Sstevel@tonic-gate 					} else {
5239fa9e4066Sahrens 						acl_is_set = 1;
52407c478bd9Sstevel@tonic-gate 					}
5241fa9e4066Sahrens 					acl_free(aclp);
52427c478bd9Sstevel@tonic-gate 					aclp = NULL;
52437c478bd9Sstevel@tonic-gate 				}
52447c478bd9Sstevel@tonic-gate 				cnt = 0;
52457c478bd9Sstevel@tonic-gate 				break;
52467c478bd9Sstevel@tonic-gate 			}
52477c478bd9Sstevel@tonic-gate 		}
52487c478bd9Sstevel@tonic-gate 		cnt++;
52497c478bd9Sstevel@tonic-gate 	} while (cnt < 2 && missdir(nam_p) == 0);
52507c478bd9Sstevel@tonic-gate 
52517c478bd9Sstevel@tonic-gate 	switch (cnt) {
52527c478bd9Sstevel@tonic-gate 	case 0:
52537c478bd9Sstevel@tonic-gate 		if ((Args & OCi) && (Hdr_type == USTAR)) {
52547c478bd9Sstevel@tonic-gate 			setpasswd(nam_p);
52557c478bd9Sstevel@tonic-gate 		}
52567c478bd9Sstevel@tonic-gate 		if ((G_p->g_mode & Ftype) == S_IFLNK ||
52577c478bd9Sstevel@tonic-gate 		    (Hdr_type == BAR && bar_linkflag == SYMTYPE)) {
5258647ab8f4Sceastha 			if (Args & OCR) {
5259647ab8f4Sceastha 				if (fchownat(dirfd,
5260647ab8f4Sceastha 				    get_component(nam_p),
5261647ab8f4Sceastha 				    (int)Rpw_p->pw_uid,
5262647ab8f4Sceastha 				    (int)Rpw_p->pw_gid,
52637c478bd9Sstevel@tonic-gate 				    AT_SYMLINK_NOFOLLOW) < 0) {
52647c478bd9Sstevel@tonic-gate 					msg(ERRN,
52657c478bd9Sstevel@tonic-gate 					    "Error during chown() of "
52667c478bd9Sstevel@tonic-gate 					    "\"%s%s%s\"",
5267ced83f9bSceastha 					    (G_p->g_attrnam_p == NULL) ?
52687c478bd9Sstevel@tonic-gate 					    nam_p : G_p->g_attrfnam_p,
5269ced83f9bSceastha 					    (G_p->g_attrnam_p == NULL) ?
5270ced83f9bSceastha 					    "" : G_p->g_rw_sysattr ?
5271ced83f9bSceastha 					    gettext(" System Attribute ") :
5272ced83f9bSceastha 					    gettext(" Attribute "),
5273ced83f9bSceastha 					    (G_p->g_attrnam_p == NULL) ?
52747c478bd9Sstevel@tonic-gate 					    "" : nam_p);
52757c478bd9Sstevel@tonic-gate 				}
5276647ab8f4Sceastha 			} else if ((fchownat(dirfd, get_component(nam_p),
5277647ab8f4Sceastha 			    (int)G_p->g_uid, (int)G_p->g_gid,
5278647ab8f4Sceastha 			    AT_SYMLINK_NOFOLLOW) < 0) && privileged) {
52797c478bd9Sstevel@tonic-gate 				msg(ERRN,
52807c478bd9Sstevel@tonic-gate 				    "Error during chown() of \"%s%s%s\"",
5281ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ?
52827c478bd9Sstevel@tonic-gate 				    nam_p : G_p->g_attrfnam_p,
5283ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ? "" :
5284ced83f9bSceastha 				    G_p->g_rw_sysattr ?
5285ced83f9bSceastha 				    gettext(" System Attribute ") :
5286ced83f9bSceastha 				    gettext(" Attribute "),
5287ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ? "" : nam_p);
52887c478bd9Sstevel@tonic-gate 			}
52897c478bd9Sstevel@tonic-gate 		}
52907c478bd9Sstevel@tonic-gate 		break;
52917c478bd9Sstevel@tonic-gate 
52927c478bd9Sstevel@tonic-gate 	case 1:
52937c478bd9Sstevel@tonic-gate 		if (Do_rename) {
52947c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot create directory for \"%s%s%s\"",
5295ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? Over_p :
52967c478bd9Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
5297ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
5298ced83f9bSceastha 			    G_p->g_rw_sysattr ?
5299ced83f9bSceastha 			    gettext(" System Attribute ") :
53007c478bd9Sstevel@tonic-gate 			    gettext(" Attribute "),
5301ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" : Over_p);
53027c478bd9Sstevel@tonic-gate 		} else {
53037c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot create directory for \"%s%s%s\"",
5304ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? nam_p :
53057c478bd9Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
5306ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
5307ced83f9bSceastha 			    G_p->g_rw_sysattr ?
5308ced83f9bSceastha 			    gettext(" System Attribute ") :
53097c478bd9Sstevel@tonic-gate 			    gettext(" Attribute "),
5310ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" : nam_p);
53117c478bd9Sstevel@tonic-gate 		}
53127c478bd9Sstevel@tonic-gate 		break;
53137c478bd9Sstevel@tonic-gate 
53147c478bd9Sstevel@tonic-gate 	case 2:
53157c478bd9Sstevel@tonic-gate 		if (Do_rename) {
53167c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot create \"%s%s%s\"",
5317ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? Over_p :
53187c478bd9Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
5319ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
5320ced83f9bSceastha 			    G_p->g_rw_sysattr ?
5321ced83f9bSceastha 			    gettext(" System Attribute ") :
53227c478bd9Sstevel@tonic-gate 			    gettext(" Attribute "),
5323ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
53247c478bd9Sstevel@tonic-gate 			    Over_p);
53257c478bd9Sstevel@tonic-gate 		} else {
53267c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot create \"%s%s%s\"",
5327ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? nam_p :
53287c478bd9Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
5329ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
5330ced83f9bSceastha 			    G_p->g_rw_sysattr ?
5331ced83f9bSceastha 			    gettext(" System Attribute ") :
53327c478bd9Sstevel@tonic-gate 			    gettext(" Attribute "),
5333ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" : nam_p);
53347c478bd9Sstevel@tonic-gate 		}
53357c478bd9Sstevel@tonic-gate 		break;
53367c478bd9Sstevel@tonic-gate 
53377c478bd9Sstevel@tonic-gate 	default:
53387c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible case.");
53397c478bd9Sstevel@tonic-gate 	}
53407c478bd9Sstevel@tonic-gate 
53417c478bd9Sstevel@tonic-gate 	Finished = 0;
53427c478bd9Sstevel@tonic-gate 	return (result);
53437c478bd9Sstevel@tonic-gate }
53447c478bd9Sstevel@tonic-gate 
53457c478bd9Sstevel@tonic-gate /*
53467c478bd9Sstevel@tonic-gate  * read_hdr: Transfer headers from the selected format
53477c478bd9Sstevel@tonic-gate  * in the archive I/O buffer to the generic structure.
53487c478bd9Sstevel@tonic-gate  */
53497c478bd9Sstevel@tonic-gate 
53507c478bd9Sstevel@tonic-gate static
53517c478bd9Sstevel@tonic-gate int
53527c478bd9Sstevel@tonic-gate read_hdr(int hdr)
53537c478bd9Sstevel@tonic-gate {
53547c478bd9Sstevel@tonic-gate 	int rv = NONE;
53557c478bd9Sstevel@tonic-gate 	major_t maj, rmaj;
53567c478bd9Sstevel@tonic-gate 	minor_t min, rmin;
53577c478bd9Sstevel@tonic-gate 	char tmpnull;
53587c478bd9Sstevel@tonic-gate 	static int bar_read_cnt = 0;
53597c478bd9Sstevel@tonic-gate 
53607c478bd9Sstevel@tonic-gate 	if (hdr != BAR) {
53617c478bd9Sstevel@tonic-gate 		if (Buffr.b_end_p != (Buffr.b_out_p + Hdrsz)) {
53627c478bd9Sstevel@tonic-gate 			tmpnull = *(Buffr.b_out_p + Hdrsz);
53637c478bd9Sstevel@tonic-gate 			*(Buffr.b_out_p + Hdrsz) = '\0';
53647c478bd9Sstevel@tonic-gate 		}
53657c478bd9Sstevel@tonic-gate 	}
53667c478bd9Sstevel@tonic-gate 
53677c478bd9Sstevel@tonic-gate 	switch (hdr) {
53687c478bd9Sstevel@tonic-gate 	case BIN:
53697c478bd9Sstevel@tonic-gate 		(void) memcpy(&Hdr, Buffr.b_out_p, HDRSZ);
53707c478bd9Sstevel@tonic-gate 		if (Hdr.h_magic == (short)CMN_BBS) {
53717c478bd9Sstevel@tonic-gate 			swap((char *)&Hdr, HDRSZ);
53727c478bd9Sstevel@tonic-gate 		}
53737c478bd9Sstevel@tonic-gate 		Gen.g_magic = Hdr.h_magic;
53747c478bd9Sstevel@tonic-gate 		Gen.g_mode = Hdr.h_mode;
53757c478bd9Sstevel@tonic-gate 		Gen.g_uid = Hdr.h_uid;
53767c478bd9Sstevel@tonic-gate 		Gen.g_gid = Hdr.h_gid;
53777c478bd9Sstevel@tonic-gate 		Gen.g_nlink = Hdr.h_nlink;
53787c478bd9Sstevel@tonic-gate 		Gen.g_mtime = mklong(Hdr.h_mtime);
53797c478bd9Sstevel@tonic-gate 		Gen.g_ino = Hdr.h_ino;
53807c478bd9Sstevel@tonic-gate 		Gen.g_dev = Hdr.h_dev;
53817c478bd9Sstevel@tonic-gate 		Gen.g_rdev = Hdr.h_rdev;
53827c478bd9Sstevel@tonic-gate 		Gen.g_cksum = 0L;
53837c478bd9Sstevel@tonic-gate 		Gen.g_filesz = (off_t)mklong(Hdr.h_filesize);
53847c478bd9Sstevel@tonic-gate 		Gen.g_namesz = Hdr.h_namesize;
53857c478bd9Sstevel@tonic-gate 		rv = BIN;
53867c478bd9Sstevel@tonic-gate 		break;
53877c478bd9Sstevel@tonic-gate 	case CHR:
53887c478bd9Sstevel@tonic-gate 		if (sscanf(Buffr.b_out_p,
53897c478bd9Sstevel@tonic-gate 		    "%6lo%6lo%6lo%6lo%6lo%6lo%6lo%6lo%11lo%6o%11llo",
53907c478bd9Sstevel@tonic-gate 		    &Gen.g_magic, &Gen.g_dev, &Gen.g_ino, &Gen.g_mode,
53917c478bd9Sstevel@tonic-gate 		    &Gen.g_uid, &Gen.g_gid, &Gen.g_nlink, &Gen.g_rdev,
53927c478bd9Sstevel@tonic-gate 		    (ulong_t *)&Gen.g_mtime, (uint_t *)&Gen.g_namesz,
53937c478bd9Sstevel@tonic-gate 		    (u_off_t *)&Gen.g_filesz) == CHR_CNT) {
53947c478bd9Sstevel@tonic-gate 			rv = CHR;
53957c478bd9Sstevel@tonic-gate #define	cpioMAJOR(x)	(int)(((unsigned)x >> 8) & 0x7F)
53967c478bd9Sstevel@tonic-gate #define	cpioMINOR(x)	(int)(x & 0xFF)
53977c478bd9Sstevel@tonic-gate 			maj = cpioMAJOR(Gen.g_dev);
53987c478bd9Sstevel@tonic-gate 			rmaj = cpioMAJOR(Gen.g_rdev);
53997c478bd9Sstevel@tonic-gate 			min = cpioMINOR(Gen.g_dev);
54007c478bd9Sstevel@tonic-gate 			rmin = cpioMINOR(Gen.g_rdev);
54017c478bd9Sstevel@tonic-gate 			if (Use_old_stat) {
54027c478bd9Sstevel@tonic-gate 				/* needs error checking */
54037c478bd9Sstevel@tonic-gate 				Gen.g_dev = (maj << 8) | min;
54047c478bd9Sstevel@tonic-gate 				Gen.g_rdev = (rmaj << 8) | rmin;
54057c478bd9Sstevel@tonic-gate 			} else {
54067c478bd9Sstevel@tonic-gate 				Gen.g_dev = makedev(maj, min);
54077c478bd9Sstevel@tonic-gate 				Gen.g_rdev = makedev(rmaj, rmin);
54087c478bd9Sstevel@tonic-gate 			}
54097c478bd9Sstevel@tonic-gate 		}
54107c478bd9Sstevel@tonic-gate 		break;
54117c478bd9Sstevel@tonic-gate 	case ASC:
54127c478bd9Sstevel@tonic-gate 	case CRC:
54137c478bd9Sstevel@tonic-gate 		if (sscanf(Buffr.b_out_p,
54147c478bd9Sstevel@tonic-gate 		    "%6lx%8lx%8lx%8lx%8lx%8lx%8lx%8llx%8x%8x%8x%8x%8x%8lx",
54157c478bd9Sstevel@tonic-gate 		    &Gen.g_magic, &Gen.g_ino, &Gen.g_mode, &Gen.g_uid,
54167c478bd9Sstevel@tonic-gate 		    &Gen.g_gid, &Gen.g_nlink, &Gen.g_mtime,
54177c478bd9Sstevel@tonic-gate 		    (u_off_t *)&Gen.g_filesz, (uint_t *)&maj, (uint_t *)&min,
54187c478bd9Sstevel@tonic-gate 		    (uint_t *)&rmaj, (uint_t *)&rmin, (uint_t *)&Gen.g_namesz,
54197c478bd9Sstevel@tonic-gate 		    &Gen.g_cksum) == ASC_CNT) {
54207c478bd9Sstevel@tonic-gate 			Gen.g_dev = makedev(maj, min);
54217c478bd9Sstevel@tonic-gate 			Gen.g_rdev = makedev(rmaj, rmin);
54227c478bd9Sstevel@tonic-gate 			rv = hdr;
54237c478bd9Sstevel@tonic-gate 		}
54247c478bd9Sstevel@tonic-gate 		break;
54257c478bd9Sstevel@tonic-gate 	case USTAR: /* TAR and USTAR */
54267c478bd9Sstevel@tonic-gate 		if (*Buffr.b_out_p == '\0') {
54277c478bd9Sstevel@tonic-gate 			*Gen.g_nam_p = '\0';
54287c478bd9Sstevel@tonic-gate 			nambuf[0] = '\0';
54297c478bd9Sstevel@tonic-gate 		} else {
54307c478bd9Sstevel@tonic-gate 			Thdr_p = (union tblock *)Buffr.b_out_p;
54317c478bd9Sstevel@tonic-gate 			Gen.g_nam_p[0] = '\0';
54327c478bd9Sstevel@tonic-gate 			(void) strncpy((char *)&nambuf,
54337c478bd9Sstevel@tonic-gate 			    Thdr_p->tbuf.t_name, NAMSIZ);
54347c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mode, "%8lo",
54357c478bd9Sstevel@tonic-gate 			    &Gen.g_mode);
54367c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_uid, "%8lo", &Gen.g_uid);
54377c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_gid, "%8lo", &Gen.g_gid);
54387c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_size, "%11llo",
54397c478bd9Sstevel@tonic-gate 			    (u_off_t *)&Gen.g_filesz);
54407c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mtime, "%12lo",
54417c478bd9Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_mtime);
54427c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_cksum, "%8lo",
54437c478bd9Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_cksum);
5444ced83f9bSceastha 			if (Thdr_p->tbuf.t_linkname[0] != '\0')
54457c478bd9Sstevel@tonic-gate 				Gen.g_nlink = 1;
54467c478bd9Sstevel@tonic-gate 			else
54477c478bd9Sstevel@tonic-gate 				Gen.g_nlink = 0;
54487c478bd9Sstevel@tonic-gate 
54497c478bd9Sstevel@tonic-gate 			switch (Thdr_p->tbuf.t_typeflag) {
54507c478bd9Sstevel@tonic-gate 			case SYMTYPE:
54517c478bd9Sstevel@tonic-gate 				/* Symbolic Link */
54527c478bd9Sstevel@tonic-gate 				Gen.g_nlink = 2;
54537c478bd9Sstevel@tonic-gate 				break;
54547c478bd9Sstevel@tonic-gate 			case CHRTYPE:
54557c478bd9Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFCHR);
54567c478bd9Sstevel@tonic-gate 				break;
54577c478bd9Sstevel@tonic-gate 			case BLKTYPE:
54587c478bd9Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFBLK);
54597c478bd9Sstevel@tonic-gate 				break;
54607c478bd9Sstevel@tonic-gate 			case DIRTYPE:
54617c478bd9Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFDIR);
54627c478bd9Sstevel@tonic-gate 				break;
54637c478bd9Sstevel@tonic-gate 			case FIFOTYPE:
54647c478bd9Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFIFO);
54657c478bd9Sstevel@tonic-gate 				break;
54667c478bd9Sstevel@tonic-gate 			}
54677c478bd9Sstevel@tonic-gate 
54687c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_magic, "%8lo",
54697c478bd9Sstevel@tonic-gate 			    /* LINTED alignment */
54707c478bd9Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_tmagic);
54717c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_version, "%8lo",
54727c478bd9Sstevel@tonic-gate 			    /* LINTED alignment */
54737c478bd9Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_version);
54747c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_uname, "%32s",
54757c478bd9Sstevel@tonic-gate 			    (char *)&Gen.g_uname);
54767c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_gname, "%32s",
54777c478bd9Sstevel@tonic-gate 			    (char *)&Gen.g_gname);
54787c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_devmajor, "%8lo",
54797c478bd9Sstevel@tonic-gate 			    &Gen.g_dev);
54807c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_devminor, "%8lo",
54817c478bd9Sstevel@tonic-gate 			    &Gen.g_rdev);
54827c478bd9Sstevel@tonic-gate 			(void) strncpy((char *)&prebuf,
54837c478bd9Sstevel@tonic-gate 			    Thdr_p->tbuf.t_prefix, PRESIZ);
54847c478bd9Sstevel@tonic-gate 			Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
54857c478bd9Sstevel@tonic-gate 			Gen.g_dev = makedev(maj, min);
54867c478bd9Sstevel@tonic-gate 		}
54877c478bd9Sstevel@tonic-gate 		rv = USTAR;
54887c478bd9Sstevel@tonic-gate 		break;
54897c478bd9Sstevel@tonic-gate 	case TAR:
54907c478bd9Sstevel@tonic-gate 		if (*Buffr.b_out_p == '\0') {
54917c478bd9Sstevel@tonic-gate 			*Gen.g_nam_p = '\0';
54927c478bd9Sstevel@tonic-gate 			nambuf[0] = '\0';
54937c478bd9Sstevel@tonic-gate 		} else {
54947c478bd9Sstevel@tonic-gate 			Thdr_p = (union tblock *)Buffr.b_out_p;
54957c478bd9Sstevel@tonic-gate 			Gen.g_nam_p[0] = '\0';
54967c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mode, "%lo", &Gen.g_mode);
54977c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_uid, "%lo", &Gen.g_uid);
54987c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_gid, "%lo", &Gen.g_gid);
54997c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_size, "%llo",
55007c478bd9Sstevel@tonic-gate 			    (u_off_t *)&Gen.g_filesz);
55017c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mtime, "%lo",
55027c478bd9Sstevel@tonic-gate 			    &Gen.g_mtime);
55037c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_cksum, "%lo",
55047c478bd9Sstevel@tonic-gate 			    &Gen.g_cksum);
55057c478bd9Sstevel@tonic-gate 			if (Thdr_p->tbuf.t_typeflag == '1')	/* hardlink */
55067c478bd9Sstevel@tonic-gate 				Gen.g_nlink = 1;
55077c478bd9Sstevel@tonic-gate 			else
55087c478bd9Sstevel@tonic-gate 				Gen.g_nlink = 0;
55097c478bd9Sstevel@tonic-gate 			(void) strncpy(Gen.g_nam_p,
55107c478bd9Sstevel@tonic-gate 			    Thdr_p->tbuf.t_name, NAMSIZ);
55117c478bd9Sstevel@tonic-gate 			Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
55127c478bd9Sstevel@tonic-gate 			(void) strcpy(nambuf, Gen.g_nam_p);
55137c478bd9Sstevel@tonic-gate 		}
55147c478bd9Sstevel@tonic-gate 		rv = TAR;
55157c478bd9Sstevel@tonic-gate 		break;
55167c478bd9Sstevel@tonic-gate 	case BAR:
55177c478bd9Sstevel@tonic-gate 		if (Bar_vol_num == 0 && bar_read_cnt == 0) {
55187c478bd9Sstevel@tonic-gate 			read_bar_vol_hdr();
55197c478bd9Sstevel@tonic-gate 			bar_read_cnt++;
55207c478bd9Sstevel@tonic-gate 		}
55217c478bd9Sstevel@tonic-gate 		else
55227c478bd9Sstevel@tonic-gate 			read_bar_file_hdr();
55237c478bd9Sstevel@tonic-gate 		rv = BAR;
55247c478bd9Sstevel@tonic-gate 		break;
55257c478bd9Sstevel@tonic-gate 	default:
55267c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
55277c478bd9Sstevel@tonic-gate 	}
55287c478bd9Sstevel@tonic-gate 
55297c478bd9Sstevel@tonic-gate 	if (hdr != BAR) {
55307c478bd9Sstevel@tonic-gate 		if (Buffr.b_end_p != (Buffr.b_out_p + Hdrsz))
55317c478bd9Sstevel@tonic-gate 			*(Buffr.b_out_p + Hdrsz) = tmpnull;
55327c478bd9Sstevel@tonic-gate 	}
55337c478bd9Sstevel@tonic-gate 
55347c478bd9Sstevel@tonic-gate 	return (rv);
55357c478bd9Sstevel@tonic-gate }
55367c478bd9Sstevel@tonic-gate 
55377c478bd9Sstevel@tonic-gate /*
55387c478bd9Sstevel@tonic-gate  * reclaim: Reclaim linked file structure storage.
55397c478bd9Sstevel@tonic-gate  */
55407c478bd9Sstevel@tonic-gate 
55417c478bd9Sstevel@tonic-gate static void
55427c478bd9Sstevel@tonic-gate reclaim(struct Lnk *p)
55437c478bd9Sstevel@tonic-gate {
55447c478bd9Sstevel@tonic-gate 	p->L_bck_p->L_nxt_p = p->L_nxt_p;
55457c478bd9Sstevel@tonic-gate 	p->L_nxt_p->L_bck_p = p->L_bck_p;
55467c478bd9Sstevel@tonic-gate 
55477c478bd9Sstevel@tonic-gate 	while (p != NULL) {
55487c478bd9Sstevel@tonic-gate 		struct Lnk *new_p = p->L_lnk_p;
55497c478bd9Sstevel@tonic-gate 
55507c478bd9Sstevel@tonic-gate 		free(p->L_gen.g_nam_p);
55517c478bd9Sstevel@tonic-gate 		free(p);
55527c478bd9Sstevel@tonic-gate 		p = new_p;
55537c478bd9Sstevel@tonic-gate 	}
55547c478bd9Sstevel@tonic-gate }
55557c478bd9Sstevel@tonic-gate 
55567c478bd9Sstevel@tonic-gate /*
55577c478bd9Sstevel@tonic-gate  * rstbuf: Reset the I/O buffer, move incomplete potential headers to
55587c478bd9Sstevel@tonic-gate  * the front of the buffer and force bread() to refill the buffer.  The
55597c478bd9Sstevel@tonic-gate  * return value from bread() is returned (to identify I/O errors).  On the
55607c478bd9Sstevel@tonic-gate  * 3B2, reads must begin on a word boundary, therefore, with the -i option,
55617c478bd9Sstevel@tonic-gate  * any remaining bytes in the buffer must be moved to the base of the buffer
55627c478bd9Sstevel@tonic-gate  * in such a way that the destination locations of subsequent reads are
55637c478bd9Sstevel@tonic-gate  * word aligned.
55647c478bd9Sstevel@tonic-gate  */
55657c478bd9Sstevel@tonic-gate 
55667c478bd9Sstevel@tonic-gate static void
55677c478bd9Sstevel@tonic-gate rstbuf(void)
55687c478bd9Sstevel@tonic-gate {
55697c478bd9Sstevel@tonic-gate 	int pad;
55707c478bd9Sstevel@tonic-gate 
55717c478bd9Sstevel@tonic-gate 	if ((Args & OCi) || Append) {
55727c478bd9Sstevel@tonic-gate 		if (Buffr.b_out_p != Buffr.b_base_p) {
55737c478bd9Sstevel@tonic-gate 			pad = ((Buffr.b_cnt + FULLWD) & ~FULLWD);
55747c478bd9Sstevel@tonic-gate 			Buffr.b_in_p = Buffr.b_base_p + pad;
55757c478bd9Sstevel@tonic-gate 			pad -= Buffr.b_cnt;
55767c478bd9Sstevel@tonic-gate 			(void) memcpy(Buffr.b_base_p + pad, Buffr.b_out_p,
55777c478bd9Sstevel@tonic-gate 			    (int)Buffr.b_cnt);
55787c478bd9Sstevel@tonic-gate 			Buffr.b_out_p = Buffr.b_base_p + pad;
55797c478bd9Sstevel@tonic-gate 		}
55807c478bd9Sstevel@tonic-gate 		if (bfill() < 0)
55817c478bd9Sstevel@tonic-gate 			msg(EXT, "Unexpected end-of-archive encountered.");
55827c478bd9Sstevel@tonic-gate 	} else { /* OCo */
55837c478bd9Sstevel@tonic-gate 		(void) memcpy(Buffr.b_base_p, Buffr.b_out_p, (int)Buffr.b_cnt);
55847c478bd9Sstevel@tonic-gate 		Buffr.b_out_p = Buffr.b_base_p;
55857c478bd9Sstevel@tonic-gate 		Buffr.b_in_p = Buffr.b_base_p + Buffr.b_cnt;
55867c478bd9Sstevel@tonic-gate 	}
55877c478bd9Sstevel@tonic-gate }
55887c478bd9Sstevel@tonic-gate 
55897c478bd9Sstevel@tonic-gate static void
55907c478bd9Sstevel@tonic-gate setpasswd(char *nam)
55917c478bd9Sstevel@tonic-gate {
5592ced83f9bSceastha 	if ((dpasswd = getpwnam(&Gen.g_uname[0])) == NULL) {
55937c478bd9Sstevel@tonic-gate 		msg(EPOST, "cpio: problem reading passwd entry");
55947c478bd9Sstevel@tonic-gate 		msg(EPOST, "cpio: %s: owner not changed", nam);
55957c478bd9Sstevel@tonic-gate 		if (Gen.g_uid == UID_NOBODY && S_ISREG(Gen.g_mode))
55967c478bd9Sstevel@tonic-gate 			Gen.g_mode &= ~S_ISUID;
55977c478bd9Sstevel@tonic-gate 	} else
55987c478bd9Sstevel@tonic-gate 		Gen.g_uid = dpasswd->pw_uid;
55997c478bd9Sstevel@tonic-gate 
5600ced83f9bSceastha 	if ((dgroup = getgrnam(&Gen.g_gname[0])) == NULL) {
56017c478bd9Sstevel@tonic-gate 		msg(EPOST, "cpio: problem reading group entry");
56027c478bd9Sstevel@tonic-gate 		msg(EPOST, "cpio: %s: group not changed", nam);
56037c478bd9Sstevel@tonic-gate 		if (Gen.g_gid == GID_NOBODY && S_ISREG(Gen.g_mode))
56047c478bd9Sstevel@tonic-gate 			Gen.g_mode &= ~S_ISGID;
56057c478bd9Sstevel@tonic-gate 	} else
56067c478bd9Sstevel@tonic-gate 		Gen.g_gid = dgroup->gr_gid;
56077c478bd9Sstevel@tonic-gate 	G_p = &Gen;
56087c478bd9Sstevel@tonic-gate }
56097c478bd9Sstevel@tonic-gate 
56107c478bd9Sstevel@tonic-gate /*
56117c478bd9Sstevel@tonic-gate  * rstfiles:  Perform final changes to the file.  If the -u option is set,
56127c478bd9Sstevel@tonic-gate  * and overwrite == U_OVER, remove the temporary file, else if overwrite
56137c478bd9Sstevel@tonic-gate  * == U_KEEP, unlink the current file, and restore the existing version
56147c478bd9Sstevel@tonic-gate  * of the file.  In addition, where appropriate, set the access or modification
56157c478bd9Sstevel@tonic-gate  * times, change the owner and change the modes of the file.
56167c478bd9Sstevel@tonic-gate  *
56177c478bd9Sstevel@tonic-gate  * Note that if Do_rename is set, then the roles of original and temporary
56187c478bd9Sstevel@tonic-gate  * file are reversed. If all went well, we will rename() the temporary file
5619da6c28aaSamw  * over the original in order to accommodate potentially executing files.
56207c478bd9Sstevel@tonic-gate  */
56217c478bd9Sstevel@tonic-gate static void
56227c478bd9Sstevel@tonic-gate rstfiles(int over, int dirfd)
56237c478bd9Sstevel@tonic-gate {
56247c478bd9Sstevel@tonic-gate 	char *inam_p, *onam_p, *nam_p;
56257c478bd9Sstevel@tonic-gate 	int error;
56267c478bd9Sstevel@tonic-gate 
5627ced83f9bSceastha #if defined(_PC_SATTR_ENABLED)
5628ced83f9bSceastha 	/* Time or permissions cannot be set on system attribute files */
5629ced83f9bSceastha 	if ((Gen.g_attrnam_p != NULL) && (Gen.g_rw_sysattr == 1)) {
5630ced83f9bSceastha 		return;
5631ced83f9bSceastha 	}
5632ced83f9bSceastha #endif	/* _PC_SATTR_ENABLED */
5633ced83f9bSceastha 
56347c478bd9Sstevel@tonic-gate 	if (Args & OCp) {
5635ced83f9bSceastha 		if (G_p->g_attrnam_p == NULL) {
56367c478bd9Sstevel@tonic-gate 			nam_p = Fullnam_p;
56377c478bd9Sstevel@tonic-gate 		} else {
56387c478bd9Sstevel@tonic-gate 			nam_p = G_p->g_attrnam_p;
56397c478bd9Sstevel@tonic-gate 		}
56407c478bd9Sstevel@tonic-gate 	} else {
56417c478bd9Sstevel@tonic-gate 		if (Gen.g_nlink > (ulong_t)0) {
56427c478bd9Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
56437c478bd9Sstevel@tonic-gate 		} else {
56447c478bd9Sstevel@tonic-gate 			nam_p = Gen.g_nam_p;
56457c478bd9Sstevel@tonic-gate 		}
56467c478bd9Sstevel@tonic-gate 	}
5647ced83f9bSceastha 	if (Gen.g_attrnam_p != NULL) {
56487c478bd9Sstevel@tonic-gate 		nam_p = Gen.g_attrnam_p;
56497c478bd9Sstevel@tonic-gate 	}
56507c478bd9Sstevel@tonic-gate 
56517c478bd9Sstevel@tonic-gate 	if ((Args & OCi) && (Hdr_type == USTAR)) {
56527c478bd9Sstevel@tonic-gate 		setpasswd(nam_p);
56537c478bd9Sstevel@tonic-gate 	}
56547c478bd9Sstevel@tonic-gate 	if (over == U_KEEP && *Over_p != '\0') {
56557c478bd9Sstevel@tonic-gate 		if (Do_rename) {
56567c478bd9Sstevel@tonic-gate 			msg(POST, "Restoring existing \"%s%s%s\"",
5657ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? Over_p : Fullnam_p,
5658ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
5659ced83f9bSceastha 			    G_p->g_rw_sysattr ? gettext(" System Attribute ") :
5660ced83f9bSceastha 			    gettext(" Attribute "),
5661ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" : Over_p);
56627c478bd9Sstevel@tonic-gate 		} else {
56637c478bd9Sstevel@tonic-gate 			msg(POST, "Restoring existing \"%s%s%s\"",
5664ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? nam_p : Fullnam_p,
5665ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
5666ced83f9bSceastha 			    G_p->g_rw_sysattr ? gettext(" System Attribute ") :
5667ced83f9bSceastha 			    gettext(" Attribute "),
5668ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" : nam_p);
56697c478bd9Sstevel@tonic-gate 		}
56707c478bd9Sstevel@tonic-gate 
56717c478bd9Sstevel@tonic-gate 		/* delete what we just built */
56727c478bd9Sstevel@tonic-gate 		(void) unlinkat(dirfd, get_component(nam_p), 0);
56737c478bd9Sstevel@tonic-gate 
56747c478bd9Sstevel@tonic-gate 		/* If the old file needs restoring, do the necessary links */
56757c478bd9Sstevel@tonic-gate 		if (Do_rename) {
56767c478bd9Sstevel@tonic-gate 			char *tmp_ptr;
56777c478bd9Sstevel@tonic-gate 
56787c478bd9Sstevel@tonic-gate 			if (Args & OCp) {
56797c478bd9Sstevel@tonic-gate 				tmp_ptr = Fullnam_p;
56807c478bd9Sstevel@tonic-gate 				Fullnam_p = Over_p;
56817c478bd9Sstevel@tonic-gate 			} else {
56827c478bd9Sstevel@tonic-gate 				tmp_ptr = G_p->g_nam_p;
56837c478bd9Sstevel@tonic-gate 				G_p->g_nam_p = Over_p;
56847c478bd9Sstevel@tonic-gate 			}
56857c478bd9Sstevel@tonic-gate 			Over_p = tmp_ptr;
56867c478bd9Sstevel@tonic-gate 
56877c478bd9Sstevel@tonic-gate 			Do_rename = 0;	/* names now have original values */
56887c478bd9Sstevel@tonic-gate 		} else {
56897c478bd9Sstevel@tonic-gate 			if (rename(Over_p, nam_p) < 0) {
56907c478bd9Sstevel@tonic-gate 				if (link(Over_p, nam_p) < 0) {
56917c478bd9Sstevel@tonic-gate 					msg(EXTN,
56927c478bd9Sstevel@tonic-gate 					    "Cannot recover original version"
56937c478bd9Sstevel@tonic-gate 					    " of \"%s%s%s\"",
5694ced83f9bSceastha 					    (G_p->g_attrnam_p == NULL) ?
56957c478bd9Sstevel@tonic-gate 					    nam_p : Fullnam_p,
5696ced83f9bSceastha 					    (G_p->g_attrnam_p == NULL) ? "" :
5697ced83f9bSceastha 					    G_p->g_rw_sysattr ?
5698ced83f9bSceastha 					    gettext(" System Attribute ") :
5699ced83f9bSceastha 					    gettext(" Attribute "),
5700ced83f9bSceastha 					    (G_p->g_attrnam_p == NULL) ?
57017c478bd9Sstevel@tonic-gate 					    "" : nam_p);
57027c478bd9Sstevel@tonic-gate 				}
57037c478bd9Sstevel@tonic-gate 				if (unlinkat(dirfd, get_component(Over_p), 0)) {
57047c478bd9Sstevel@tonic-gate 					msg(ERRN,
57057c478bd9Sstevel@tonic-gate 					    "Cannot remove temp file "
57067c478bd9Sstevel@tonic-gate 					    "\"%s%s%s\"",
5707ced83f9bSceastha 					    (G_p->g_attrnam_p == NULL) ?
57087c478bd9Sstevel@tonic-gate 					    Over_p : Fullnam_p,
5709ced83f9bSceastha 					    (G_p->g_attrnam_p == NULL) ? "" :
5710ced83f9bSceastha 					    G_p->g_rw_sysattr ?
5711ced83f9bSceastha 					    gettext(" System Attribute ") :
5712ced83f9bSceastha 					    gettext(" Attribute "),
5713ced83f9bSceastha 					    (G_p->g_attrnam_p == NULL) ?
57147c478bd9Sstevel@tonic-gate 					    "" : Over_p);
57157c478bd9Sstevel@tonic-gate 				}
57167c478bd9Sstevel@tonic-gate 			}
57177c478bd9Sstevel@tonic-gate 		}
57187c478bd9Sstevel@tonic-gate 		*Over_p = '\0';
57197c478bd9Sstevel@tonic-gate 		return;
57207c478bd9Sstevel@tonic-gate 	} else if (over == U_OVER && *Over_p != '\0') {
57217c478bd9Sstevel@tonic-gate 		if (Do_rename) {
57227c478bd9Sstevel@tonic-gate 			char *tmp_ptr;
57237c478bd9Sstevel@tonic-gate 
57247c478bd9Sstevel@tonic-gate 			(void) renameat(dirfd, get_component(nam_p),
57257c478bd9Sstevel@tonic-gate 			    dirfd, get_component(Over_p));
57267c478bd9Sstevel@tonic-gate 			if (Args & OCp) {
5727ced83f9bSceastha 				if (G_p->g_attrnam_p == NULL) {
57287c478bd9Sstevel@tonic-gate 					tmp_ptr = Fullnam_p;
57297c478bd9Sstevel@tonic-gate 					Fullnam_p = Over_p;
57307c478bd9Sstevel@tonic-gate 					Over_p = tmp_ptr;
57317c478bd9Sstevel@tonic-gate 				} else {
57327c478bd9Sstevel@tonic-gate 					/*
57337c478bd9Sstevel@tonic-gate 					 * Over_p is pointing at g_attrnam_p
57347c478bd9Sstevel@tonic-gate 					 * which must be preserved.
57357c478bd9Sstevel@tonic-gate 					 *
57367c478bd9Sstevel@tonic-gate 					 * We don't want the tmp_ptr and so
57377c478bd9Sstevel@tonic-gate 					 * on to throw away our only copy of
57387c478bd9Sstevel@tonic-gate 					 * the name.
57397c478bd9Sstevel@tonic-gate 					 */
57407c478bd9Sstevel@tonic-gate 					Over_p = Attrfile_p;
57417c478bd9Sstevel@tonic-gate 				}
57427c478bd9Sstevel@tonic-gate 			} else {
57437c478bd9Sstevel@tonic-gate 				tmp_ptr = G_p->g_nam_p;
57447c478bd9Sstevel@tonic-gate 				G_p->g_nam_p = Over_p;
57457c478bd9Sstevel@tonic-gate 				Over_p = tmp_ptr;
57467c478bd9Sstevel@tonic-gate 			}
57477c478bd9Sstevel@tonic-gate 			Do_rename = 0;	/* names now have original values */
57487c478bd9Sstevel@tonic-gate 		} else {
57497c478bd9Sstevel@tonic-gate 			if (unlinkat(dirfd, get_component(Over_p), 0) < 0) {
57507c478bd9Sstevel@tonic-gate 				msg(ERRN,
57517c478bd9Sstevel@tonic-gate 				    "Cannot unlink() temp file \"%s%s%s\"",
5752ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ?
57537c478bd9Sstevel@tonic-gate 				    Over_p : Fullnam_p,
5754ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ? "" :
5755ced83f9bSceastha 				    G_p->g_rw_sysattr ?
5756ced83f9bSceastha 				    gettext(" System Attribute ") :
5757ced83f9bSceastha 				    gettext(" Attribute "),
5758ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ? "" : Over_p);
57597c478bd9Sstevel@tonic-gate 			}
57607c478bd9Sstevel@tonic-gate 		}
57617c478bd9Sstevel@tonic-gate 		*Over_p = '\0';
57627c478bd9Sstevel@tonic-gate 	}
57637c478bd9Sstevel@tonic-gate 	if (Args & OCp) {
5764ced83f9bSceastha 		if (G_p->g_attrnam_p != NULL) {
57657c478bd9Sstevel@tonic-gate 			inam_p = G_p->g_attrfnam_p;
57667c478bd9Sstevel@tonic-gate 			onam_p = G_p->g_attrnam_p;
57677c478bd9Sstevel@tonic-gate 		} else {
57687c478bd9Sstevel@tonic-gate 			inam_p = Nam_p;
57697c478bd9Sstevel@tonic-gate 			onam_p = Fullnam_p;
57707c478bd9Sstevel@tonic-gate 		}
57717c478bd9Sstevel@tonic-gate 	} else /* OCi only uses onam_p, OCo only uses inam_p */
5772ced83f9bSceastha 		if (G_p->g_attrnam_p != NULL) {
57737c478bd9Sstevel@tonic-gate 			inam_p = onam_p = G_p->g_attrnam_p;
57747c478bd9Sstevel@tonic-gate 		} else {
57757c478bd9Sstevel@tonic-gate 			inam_p = onam_p = G_p->g_nam_p;
57767c478bd9Sstevel@tonic-gate 		}
57777c478bd9Sstevel@tonic-gate 
57787c478bd9Sstevel@tonic-gate 	/*
5779647ab8f4Sceastha 	 * Change the owner, time, and mode to those of the file
5780647ab8f4Sceastha 	 * originally created in the archive.  Note: time and
5781647ab8f4Sceastha 	 * mode do not need to be restored for a symbolic link
5782647ab8f4Sceastha 	 * since rstfiles() is not called when the archived file
5783647ab8f4Sceastha 	 * is a symlink.
57847c478bd9Sstevel@tonic-gate 	 */
5785647ab8f4Sceastha 	if (!(Args & OCo)) {
5786647ab8f4Sceastha 		if (Args & OCR) {
5787647ab8f4Sceastha 			if (fchownat(dirfd, get_component(onam_p),
5788647ab8f4Sceastha 			    Rpw_p->pw_uid, Rpw_p->pw_gid,
5789647ab8f4Sceastha 			    AT_SYMLINK_NOFOLLOW) < 0) {
5790647ab8f4Sceastha 				msg(ERRN, "Cannot chown() \"%s%s%s\"",
5791647ab8f4Sceastha 				    onam_p,
5792ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ? "" :
5793ced83f9bSceastha 				    G_p->g_rw_sysattr ?
5794ced83f9bSceastha 				    gettext(" System Attribute ") :
5795ced83f9bSceastha 				    gettext(" Attribute "),
5796ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ? "" : onam_p);
5797647ab8f4Sceastha 			}
5798647ab8f4Sceastha 		} else {
5799647ab8f4Sceastha 			if ((fchownat(dirfd, get_component(onam_p),
5800647ab8f4Sceastha 			    G_p->g_uid, G_p->g_gid,
5801647ab8f4Sceastha 			    AT_SYMLINK_NOFOLLOW) < 0) && privileged) {
5802647ab8f4Sceastha 				msg(ERRN, "Cannot chown() \"%s%s%s\"",
5803647ab8f4Sceastha 				    onam_p,
5804ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ? "" :
5805ced83f9bSceastha 				    G_p->g_rw_sysattr ?
5806ced83f9bSceastha 				    gettext(" System Attribute ") :
5807ced83f9bSceastha 				    gettext(" Attribute "),
5808ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ? "" : onam_p);
5809647ab8f4Sceastha 			}
5810647ab8f4Sceastha 		}
58117c478bd9Sstevel@tonic-gate 
5812647ab8f4Sceastha 		if (Args & OCm) {
5813647ab8f4Sceastha 			set_tym(dirfd, get_component(onam_p),
5814647ab8f4Sceastha 			    G_p->g_mtime, G_p->g_mtime);
5815647ab8f4Sceastha 		}
5816647ab8f4Sceastha 
58177c478bd9Sstevel@tonic-gate 		/* Acl was not set, so we must chmod */
5818647ab8f4Sceastha 		if (!acl_is_set) {
5819647ab8f4Sceastha 			mode_t orig_mask, new_mask;
58207c478bd9Sstevel@tonic-gate 
58217c478bd9Sstevel@tonic-gate 			/*
58227c478bd9Sstevel@tonic-gate 			 * use fchmod for attributes, since
58237c478bd9Sstevel@tonic-gate 			 * we known they are always regular
58247c478bd9Sstevel@tonic-gate 			 * files, whereas when it isn't an
58257c478bd9Sstevel@tonic-gate 			 * attribute it could be for a fifo
58267c478bd9Sstevel@tonic-gate 			 * or something other that we don't
58277c478bd9Sstevel@tonic-gate 			 * open and don't have a valid Ofile
58287c478bd9Sstevel@tonic-gate 			 * for.
58297c478bd9Sstevel@tonic-gate 			 */
5830647ab8f4Sceastha 			if (privileged) {
5831647ab8f4Sceastha 				new_mask = G_p->g_mode;
5832647ab8f4Sceastha 			} else {
5833647ab8f4Sceastha 				orig_mask = umask(0);
5834647ab8f4Sceastha 				new_mask = G_p->g_mode & ~orig_mask;
5835647ab8f4Sceastha 			}
5836647ab8f4Sceastha 
5837ced83f9bSceastha 			if (G_p->g_attrnam_p != NULL) {
58387c478bd9Sstevel@tonic-gate 				error = fchmod(Ofile, new_mask);
58397c478bd9Sstevel@tonic-gate 			} else {
5840647ab8f4Sceastha 				error = chmod(onam_p, new_mask);
58417c478bd9Sstevel@tonic-gate 			}
58427c478bd9Sstevel@tonic-gate 			if (error < 0) {
58437c478bd9Sstevel@tonic-gate 				msg(ERRN,
58447c478bd9Sstevel@tonic-gate 				    "Cannot chmod() \"%s%s%s\"",
5845ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ?
5846647ab8f4Sceastha 				    onam_p : G_p->g_attrfnam_p,
5847ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ? "" :
5848ced83f9bSceastha 				    G_p->g_rw_sysattr ?
5849ced83f9bSceastha 				    gettext(" System Attribute ") :
5850ced83f9bSceastha 				    gettext(" Attribute "),
5851ced83f9bSceastha 				    (G_p->g_attrnam_p == NULL) ? "" : onam_p);
58527c478bd9Sstevel@tonic-gate 			}
5853647ab8f4Sceastha 			if (!privileged) {
58547c478bd9Sstevel@tonic-gate 				(void) umask(orig_mask);
58557c478bd9Sstevel@tonic-gate 			}
58567c478bd9Sstevel@tonic-gate 		}
58577c478bd9Sstevel@tonic-gate 	}
58587c478bd9Sstevel@tonic-gate 
58597c478bd9Sstevel@tonic-gate 	if (!(Args & OCi) && (Args & OCa)) {
58607c478bd9Sstevel@tonic-gate 		/*
58617c478bd9Sstevel@tonic-gate 		 * Use dirfd since we are updating original file
58627c478bd9Sstevel@tonic-gate 		 * and not just created file
58637c478bd9Sstevel@tonic-gate 		 */
58647c478bd9Sstevel@tonic-gate 		set_tym(G_p->g_dirfd, get_component(inam_p),
58657c478bd9Sstevel@tonic-gate 		    (ulong_t)SrcSt.st_atime, (ulong_t)SrcSt.st_mtime);
58667c478bd9Sstevel@tonic-gate 	}
58677c478bd9Sstevel@tonic-gate }
58687c478bd9Sstevel@tonic-gate 
58697c478bd9Sstevel@tonic-gate /*
58707c478bd9Sstevel@tonic-gate  * scan4trail: Scan the archive looking for the trailer.
58717c478bd9Sstevel@tonic-gate  * When found, back the archive up over the trailer and overwrite
58727c478bd9Sstevel@tonic-gate  * the trailer with the files to be added to the archive.
58737c478bd9Sstevel@tonic-gate  */
58747c478bd9Sstevel@tonic-gate 
58757c478bd9Sstevel@tonic-gate static void
58767c478bd9Sstevel@tonic-gate scan4trail(void)
58777c478bd9Sstevel@tonic-gate {
58787c478bd9Sstevel@tonic-gate 	int rv;
58797c478bd9Sstevel@tonic-gate 	off_t off1, off2;
58807c478bd9Sstevel@tonic-gate 
58817c478bd9Sstevel@tonic-gate 	Append = 1;
58827c478bd9Sstevel@tonic-gate 	Hdr_type = NONE;
5883ced83f9bSceastha 	G_p = NULL;
58847c478bd9Sstevel@tonic-gate 	while (gethdr()) {
58857c478bd9Sstevel@tonic-gate 		G_p = &Gen;
58867c478bd9Sstevel@tonic-gate 		data_in(P_SKIP);
58877c478bd9Sstevel@tonic-gate 	}
58887c478bd9Sstevel@tonic-gate 	off1 = Buffr.b_cnt;
58897c478bd9Sstevel@tonic-gate 	off2 = Bufsize - (Buffr.b_cnt % Bufsize);
58907c478bd9Sstevel@tonic-gate 	Buffr.b_out_p = Buffr.b_in_p = Buffr.b_base_p;
58917c478bd9Sstevel@tonic-gate 	Buffr.b_cnt = (off_t)0;
58927c478bd9Sstevel@tonic-gate 	if (lseek(Archive, -(off1 + off2), SEEK_REL) < 0)
58937c478bd9Sstevel@tonic-gate 		msg(EXTN, "Unable to append to this archive");
58947c478bd9Sstevel@tonic-gate 	if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0)
58957c478bd9Sstevel@tonic-gate 		msg(EXTN, "Cannot append to this archive");
58967c478bd9Sstevel@tonic-gate 	if (lseek(Archive, (off_t)-rv, SEEK_REL) < 0)
58977c478bd9Sstevel@tonic-gate 		msg(EXTN, "Unable to append to this archive");
58987c478bd9Sstevel@tonic-gate 	Buffr.b_cnt = off2;
58997c478bd9Sstevel@tonic-gate 	Buffr.b_in_p = Buffr.b_base_p + Buffr.b_cnt;
59007c478bd9Sstevel@tonic-gate 	Append = 0;
59017c478bd9Sstevel@tonic-gate }
59027c478bd9Sstevel@tonic-gate 
59037c478bd9Sstevel@tonic-gate /*
59047c478bd9Sstevel@tonic-gate  * setup:  Perform setup and initialization functions.  Parse the options
59057c478bd9Sstevel@tonic-gate  * using getopt(3C), call ckopts to check the options and initialize various
59067c478bd9Sstevel@tonic-gate  * structures and pointers.  Specifically, for the -i option, save any
59077c478bd9Sstevel@tonic-gate  * patterns, for the -o option, check (via stat(2)) the archive, and for
59087c478bd9Sstevel@tonic-gate  * the -p option, validate the destination directory.
59097c478bd9Sstevel@tonic-gate  */
59107c478bd9Sstevel@tonic-gate 
59117c478bd9Sstevel@tonic-gate static void
59127c478bd9Sstevel@tonic-gate setup(int largc, char **largv)
59137c478bd9Sstevel@tonic-gate {
59147c478bd9Sstevel@tonic-gate 	extern int optind;
59157c478bd9Sstevel@tonic-gate 	extern char *optarg;
59167c478bd9Sstevel@tonic-gate 
59177c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
5918ced83f9bSceastha #if defined(_PC_SATTR_ENABLED)
5919ced83f9bSceastha #ifdef WAITAROUND
5920ced83f9bSceastha 	char	*opts_p = "zabcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6@/";
5921ced83f9bSceastha #else
5922ced83f9bSceastha 	char	*opts_p = "abcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6@/";
5923ced83f9bSceastha #endif	/* WAITAROUND */
5924ced83f9bSceastha 
5925ced83f9bSceastha #else	/* _PC_SATTR_ENABLED */
59267c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
59277c478bd9Sstevel@tonic-gate 	char	*opts_p = "zabcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6@";
59287c478bd9Sstevel@tonic-gate #else
59297c478bd9Sstevel@tonic-gate 	char	*opts_p = "abcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6@";
5930ced83f9bSceastha #endif	/* WAITAROUND */
5931ced83f9bSceastha #endif	/* _PC_SATTR_ENABLED */
5932ced83f9bSceastha 
5933ced83f9bSceastha #else	/* O_XATTR */
59347c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
59357c478bd9Sstevel@tonic-gate 	char	*opts_p = "zabcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6";
59367c478bd9Sstevel@tonic-gate #else
59377c478bd9Sstevel@tonic-gate 	char	*opts_p = "abcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6";
5938ced83f9bSceastha #endif	/* WAITAROUND */
5939ced83f9bSceastha #endif	/* O_XATTR */
59407c478bd9Sstevel@tonic-gate 
59417c478bd9Sstevel@tonic-gate 	char   *dupl_p = "Only one occurrence of -%c allowed";
59427c478bd9Sstevel@tonic-gate 	int option;
59437c478bd9Sstevel@tonic-gate 	int blk_cnt, blk_cnt_max;
59447c478bd9Sstevel@tonic-gate 	struct rlimit rlim;
59457c478bd9Sstevel@tonic-gate 
59467c478bd9Sstevel@tonic-gate 	/* Remember the native page size. */
59477c478bd9Sstevel@tonic-gate 
59487c478bd9Sstevel@tonic-gate 	PageSize = sysconf(_SC_PAGESIZE);
59497c478bd9Sstevel@tonic-gate 
59507c478bd9Sstevel@tonic-gate 	if (PageSize == -1) {
59517c478bd9Sstevel@tonic-gate 		/*
59527c478bd9Sstevel@tonic-gate 		 * This sysconf call will almost certainly never fail.  The
59537c478bd9Sstevel@tonic-gate 		 * symbol PAGESIZE itself resolves to the above sysconf call,
59547c478bd9Sstevel@tonic-gate 		 * so we should go ahead and define our own constant.
59557c478bd9Sstevel@tonic-gate 		 */
59567c478bd9Sstevel@tonic-gate 		PageSize = 8192;
59577c478bd9Sstevel@tonic-gate 	}
59587c478bd9Sstevel@tonic-gate 
59597c478bd9Sstevel@tonic-gate 	Hdr_type = BIN;
59607c478bd9Sstevel@tonic-gate 	Max_offset = (off_t)(BIN_OFFSET_MAX);
59617c478bd9Sstevel@tonic-gate 	Efil_p = Hdr_p = Own_p = IOfil_p = NULL;
59627c478bd9Sstevel@tonic-gate 	while ((option = getopt(largc, largv, opts_p)) != EOF) {
59637c478bd9Sstevel@tonic-gate 		switch (option) {
59647c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
59657c478bd9Sstevel@tonic-gate 		case 'z':
59667c478bd9Sstevel@tonic-gate 			/* rendezvous with the debugger */
59677c478bd9Sstevel@tonic-gate 			waitaround = 1;
59687c478bd9Sstevel@tonic-gate 			break;
59697c478bd9Sstevel@tonic-gate #endif
59707c478bd9Sstevel@tonic-gate 		case 'a':	/* reset access time */
59717c478bd9Sstevel@tonic-gate 			Args |= OCa;
59727c478bd9Sstevel@tonic-gate 			break;
59737c478bd9Sstevel@tonic-gate 		case 'b':	/* swap bytes and halfwords */
59747c478bd9Sstevel@tonic-gate 			Args |= OCb;
59757c478bd9Sstevel@tonic-gate 			break;
59767c478bd9Sstevel@tonic-gate 		case 'c':	/* select character header */
59777c478bd9Sstevel@tonic-gate 			Args |= OCc;
59787c478bd9Sstevel@tonic-gate 			Hdr_type = ASC;
59797c478bd9Sstevel@tonic-gate 			Max_namesz = APATH;
59807c478bd9Sstevel@tonic-gate 			Onecopy = 1;
59817c478bd9Sstevel@tonic-gate 			break;
59827c478bd9Sstevel@tonic-gate 		case 'd':	/* create directories as needed */
59837c478bd9Sstevel@tonic-gate 			Args |= OCd;
59847c478bd9Sstevel@tonic-gate 			break;
59857c478bd9Sstevel@tonic-gate 		case 'f':	/* select files not in patterns */
59867c478bd9Sstevel@tonic-gate 			Args |= OCf;
59877c478bd9Sstevel@tonic-gate 			break;
59887c478bd9Sstevel@tonic-gate 		case 'i':	/* "copy in" */
59897c478bd9Sstevel@tonic-gate 			Args |= OCi;
59907c478bd9Sstevel@tonic-gate 			Archive = 0;
59917c478bd9Sstevel@tonic-gate 			break;
59927c478bd9Sstevel@tonic-gate 		case 'k':	/* retry after I/O errors */
59937c478bd9Sstevel@tonic-gate 			Args |= OCk;
59947c478bd9Sstevel@tonic-gate 			break;
59957c478bd9Sstevel@tonic-gate 		case 'l':	/* link files when possible */
59967c478bd9Sstevel@tonic-gate 			Args |= OCl;
59977c478bd9Sstevel@tonic-gate 			break;
59987c478bd9Sstevel@tonic-gate 		case 'm':	/* retain modification time */
59997c478bd9Sstevel@tonic-gate 			Args |= OCm;
60007c478bd9Sstevel@tonic-gate 			break;
60017c478bd9Sstevel@tonic-gate 		case 'o':	/* "copy out" */
60027c478bd9Sstevel@tonic-gate 			Args |= OCo;
60037c478bd9Sstevel@tonic-gate 			Archive = 1;
60047c478bd9Sstevel@tonic-gate 			break;
60057c478bd9Sstevel@tonic-gate 		case 'p':	/* "pass" */
60067c478bd9Sstevel@tonic-gate 			Max_namesz = APATH;
60077c478bd9Sstevel@tonic-gate 			Args |= OCp;
60087c478bd9Sstevel@tonic-gate 			break;
60097c478bd9Sstevel@tonic-gate 		case 'r':	/* rename files interactively */
60107c478bd9Sstevel@tonic-gate 			Args |= OCr;
60117c478bd9Sstevel@tonic-gate 			break;
60127c478bd9Sstevel@tonic-gate 		case 's':	/* swap bytes */
60137c478bd9Sstevel@tonic-gate 			Args |= OCs;
60147c478bd9Sstevel@tonic-gate 			break;
60157c478bd9Sstevel@tonic-gate 		case 't':	/* table of contents */
60167c478bd9Sstevel@tonic-gate 			Args |= OCt;
60177c478bd9Sstevel@tonic-gate 			break;
60187c478bd9Sstevel@tonic-gate 		case 'u':	/* copy unconditionally */
60197c478bd9Sstevel@tonic-gate 			Args |= OCu;
60207c478bd9Sstevel@tonic-gate 			break;
60217c478bd9Sstevel@tonic-gate 		case 'v':	/* verbose - print file names */
60227c478bd9Sstevel@tonic-gate 			Args |= OCv;
60237c478bd9Sstevel@tonic-gate 			break;
60247c478bd9Sstevel@tonic-gate 		case 'A':	/* append to existing archive */
60257c478bd9Sstevel@tonic-gate 			Args |= OCA;
60267c478bd9Sstevel@tonic-gate 			break;
60277c478bd9Sstevel@tonic-gate 		case 'B':	/* set block size to 5120 bytes */
60287c478bd9Sstevel@tonic-gate 			Args |= OCB;
60297c478bd9Sstevel@tonic-gate 			Bufsize = 5120;
60307c478bd9Sstevel@tonic-gate 			break;
60317c478bd9Sstevel@tonic-gate 		case 'C':	/* set arbitrary block size */
60327c478bd9Sstevel@tonic-gate 			if (Args & OCC)
60337c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'C');
60347c478bd9Sstevel@tonic-gate 			else {
60357c478bd9Sstevel@tonic-gate 				Args |= OCC;
60367c478bd9Sstevel@tonic-gate 				Bufsize = atoi(optarg);
60377c478bd9Sstevel@tonic-gate 			}
60387c478bd9Sstevel@tonic-gate 			break;
60397c478bd9Sstevel@tonic-gate 		case 'D':
60407c478bd9Sstevel@tonic-gate 			Dflag = 1;
60417c478bd9Sstevel@tonic-gate 			break;
60427c478bd9Sstevel@tonic-gate 		case 'E':	/* alternate file for pattern input */
60437c478bd9Sstevel@tonic-gate 			if (Args & OCE)
60447c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'E');
60457c478bd9Sstevel@tonic-gate 			else {
60467c478bd9Sstevel@tonic-gate 				Args |= OCE;
60477c478bd9Sstevel@tonic-gate 				Efil_p = optarg;
60487c478bd9Sstevel@tonic-gate 			}
60497c478bd9Sstevel@tonic-gate 			break;
60507c478bd9Sstevel@tonic-gate 		case 'H':	/* select header type */
60517c478bd9Sstevel@tonic-gate 			if (Args & OCH)
60527c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'H');
60537c478bd9Sstevel@tonic-gate 			else {
60547c478bd9Sstevel@tonic-gate 				Args |= OCH;
60557c478bd9Sstevel@tonic-gate 				Hdr_p = optarg;
60567c478bd9Sstevel@tonic-gate 			}
60577c478bd9Sstevel@tonic-gate 			break;
60587c478bd9Sstevel@tonic-gate 		case 'I':	/* alternate file for archive input */
60597c478bd9Sstevel@tonic-gate 			if (Args & OCI)
60607c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'I');
60617c478bd9Sstevel@tonic-gate 			else {
60627c478bd9Sstevel@tonic-gate 				Args |= OCI;
60637c478bd9Sstevel@tonic-gate 				IOfil_p = optarg;
60647c478bd9Sstevel@tonic-gate 			}
60657c478bd9Sstevel@tonic-gate 			break;
60667c478bd9Sstevel@tonic-gate 		case 'L':	/* follow symbolic links */
60677c478bd9Sstevel@tonic-gate 			Args |= OCL;
60687c478bd9Sstevel@tonic-gate 			break;
60697c478bd9Sstevel@tonic-gate 		case 'M':	/* specify new end-of-media message */
60707c478bd9Sstevel@tonic-gate 			if (Args & OCM)
60717c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'M');
60727c478bd9Sstevel@tonic-gate 			else {
60737c478bd9Sstevel@tonic-gate 				Args |= OCM;
60747c478bd9Sstevel@tonic-gate 				Eom_p = optarg;
60757c478bd9Sstevel@tonic-gate 			}
60767c478bd9Sstevel@tonic-gate 			break;
60777c478bd9Sstevel@tonic-gate 		case 'O':	/* alternate file for archive output */
60787c478bd9Sstevel@tonic-gate 			if (Args & OCO)
60797c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'O');
60807c478bd9Sstevel@tonic-gate 			else {
60817c478bd9Sstevel@tonic-gate 				Args |= OCO;
60827c478bd9Sstevel@tonic-gate 				IOfil_p = optarg;
60837c478bd9Sstevel@tonic-gate 			}
60847c478bd9Sstevel@tonic-gate 			break;
60857c478bd9Sstevel@tonic-gate 		case 'P':	/* preserve acls */
60867c478bd9Sstevel@tonic-gate 			Args |= OCP;
60877c478bd9Sstevel@tonic-gate 			Pflag++;
60887c478bd9Sstevel@tonic-gate 			break;
60897c478bd9Sstevel@tonic-gate 		case 'R':	/* change owner/group of files */
60907c478bd9Sstevel@tonic-gate 			if (Args & OCR)
60917c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'R');
60927c478bd9Sstevel@tonic-gate 			else {
60937c478bd9Sstevel@tonic-gate 				Args |= OCR;
60947c478bd9Sstevel@tonic-gate 				Own_p = optarg;
60957c478bd9Sstevel@tonic-gate 			}
60967c478bd9Sstevel@tonic-gate 			break;
60977c478bd9Sstevel@tonic-gate 		case 'S':	/* swap halfwords */
60987c478bd9Sstevel@tonic-gate 			Args |= OCS;
60997c478bd9Sstevel@tonic-gate 			break;
61007c478bd9Sstevel@tonic-gate 		case 'V':	/* print a dot '.' for each file */
61017c478bd9Sstevel@tonic-gate 			Args |= OCV;
61027c478bd9Sstevel@tonic-gate 			break;
61037c478bd9Sstevel@tonic-gate 		case '6':	/* for old, sixth-edition files */
61047c478bd9Sstevel@tonic-gate 			Args |= OC6;
61057c478bd9Sstevel@tonic-gate 			Ftype = SIXTH;
61067c478bd9Sstevel@tonic-gate 			break;
61077c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
61087c478bd9Sstevel@tonic-gate 		case '@':
61097c478bd9Sstevel@tonic-gate 			Atflag++;
61107c478bd9Sstevel@tonic-gate 			break;
6111ced83f9bSceastha #if defined(_PC_SATTR_ENABLED)
6112ced83f9bSceastha 		case '/':
6113ced83f9bSceastha 			SysAtflag++;
6114ced83f9bSceastha 			break;
6115ced83f9bSceastha #endif	/* _PC_SATTR_ENABLED */
6116ced83f9bSceastha #endif	/* O_XATTR */
61177c478bd9Sstevel@tonic-gate 		default:
61187c478bd9Sstevel@tonic-gate 			Error_cnt++;
61197c478bd9Sstevel@tonic-gate 		} /* option */
61207c478bd9Sstevel@tonic-gate 	} /* (option = getopt(largc, largv, opts_p)) != EOF */
61217c478bd9Sstevel@tonic-gate 
61227c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
61237c478bd9Sstevel@tonic-gate 	if (waitaround) {
61247c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Rendezvous with cpio on pid"
61257c478bd9Sstevel@tonic-gate 		    " %d\n"), getpid());
61267c478bd9Sstevel@tonic-gate 
61277c478bd9Sstevel@tonic-gate 		while (waitaround) {
61287c478bd9Sstevel@tonic-gate 			(void) sleep(10);
61297c478bd9Sstevel@tonic-gate 		}
61307c478bd9Sstevel@tonic-gate 	}
61317c478bd9Sstevel@tonic-gate #endif
61327c478bd9Sstevel@tonic-gate 
61337c478bd9Sstevel@tonic-gate 	largc -= optind;
61347c478bd9Sstevel@tonic-gate 	largv += optind;
61357c478bd9Sstevel@tonic-gate 	ckopts(Args);
61367c478bd9Sstevel@tonic-gate 	if (!Error_cnt) {
61377c478bd9Sstevel@tonic-gate 		Empty = e_valloc(E_EXIT, TARSZ);
61387c478bd9Sstevel@tonic-gate 		if (Args & OCr) {
61397c478bd9Sstevel@tonic-gate 			Renam_p = e_zalloc(E_EXIT, APATH + 1);
61407c478bd9Sstevel@tonic-gate 			Renametmp_p = e_zalloc(E_EXIT, APATH + 1);
6141ced83f9bSceastha #if defined(_PC_SATTR_ENABLED)
6142ced83f9bSceastha 			Renam_attr_p = e_zalloc(E_EXIT, APATH + 1);
6143ced83f9bSceastha #endif
61447c478bd9Sstevel@tonic-gate 		}
61457c478bd9Sstevel@tonic-gate 		Symlnk_p = e_zalloc(E_EXIT, APATH);
61467c478bd9Sstevel@tonic-gate 		Over_p = e_zalloc(E_EXIT, APATH);
61477c478bd9Sstevel@tonic-gate 		Nam_p = e_zalloc(E_EXIT, APATH + 1);
61487c478bd9Sstevel@tonic-gate 		if (Args & OCp) {
61497c478bd9Sstevel@tonic-gate 			Savenam_p = e_zalloc(E_EXIT, APATH + 1);
61507c478bd9Sstevel@tonic-gate 		}
61517c478bd9Sstevel@tonic-gate 		Fullnam_p = e_zalloc(E_EXIT, APATH);
61527c478bd9Sstevel@tonic-gate 		Lnknam_p = e_zalloc(E_EXIT, APATH);
61537c478bd9Sstevel@tonic-gate 		Gen.g_nam_p = Nam_p;
6154ced83f9bSceastha 		if ((Fullnam_p = getcwd(NULL, APATH)) == NULL)
61557c478bd9Sstevel@tonic-gate 			msg(EXT, "Unable to determine current directory.");
61567c478bd9Sstevel@tonic-gate 		if (Args & OCi) {
61577c478bd9Sstevel@tonic-gate 			if (largc > 0) /* save patterns for -i option, if any */
61587c478bd9Sstevel@tonic-gate 				Pat_pp = largv;
61597c478bd9Sstevel@tonic-gate 			if (Args & OCE)
61607c478bd9Sstevel@tonic-gate 				getpats(largc, largv);
61617c478bd9Sstevel@tonic-gate 		} else if (Args & OCo) {
61627c478bd9Sstevel@tonic-gate 			if (largc != 0) /* error if arguments left with -o */
61637c478bd9Sstevel@tonic-gate 				Error_cnt++;
61647c478bd9Sstevel@tonic-gate 			else if (fstat(Archive, &ArchSt) < 0)
61657c478bd9Sstevel@tonic-gate 				msg(ERRN, "Error during stat() of archive");
61667c478bd9Sstevel@tonic-gate 			switch (Hdr_type) {
61677c478bd9Sstevel@tonic-gate 			case BIN:
61687c478bd9Sstevel@tonic-gate 				Hdrsz = HDRSZ;
61697c478bd9Sstevel@tonic-gate 				Pad_val = HALFWD;
61707c478bd9Sstevel@tonic-gate 				break;
61717c478bd9Sstevel@tonic-gate 			case CHR:
61727c478bd9Sstevel@tonic-gate 				Hdrsz = CHRSZ;
61737c478bd9Sstevel@tonic-gate 				Pad_val = 0;
61747c478bd9Sstevel@tonic-gate 				Max_offset = (off_t)(CHAR_OFFSET_MAX);
61757c478bd9Sstevel@tonic-gate 				break;
61767c478bd9Sstevel@tonic-gate 			case ASC:
61777c478bd9Sstevel@tonic-gate 			case CRC:
61787c478bd9Sstevel@tonic-gate 				Hdrsz = ASCSZ;
61797c478bd9Sstevel@tonic-gate 				Pad_val = FULLWD;
6180944d83a0Schin 				Max_offset = (off_t)(ASC_OFFSET_MAX);
61817c478bd9Sstevel@tonic-gate 				break;
61827c478bd9Sstevel@tonic-gate 			case TAR:
61837c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
61847c478bd9Sstevel@tonic-gate 			case USTAR: /* TAR and USTAR */
61857c478bd9Sstevel@tonic-gate 				Hdrsz = TARSZ;
61867c478bd9Sstevel@tonic-gate 				Pad_val = FULLBK;
61877c478bd9Sstevel@tonic-gate 				Max_offset = (off_t)(CHAR_OFFSET_MAX);
61887c478bd9Sstevel@tonic-gate 				break;
61897c478bd9Sstevel@tonic-gate 			default:
61907c478bd9Sstevel@tonic-gate 				msg(EXT, "Impossible header type.");
61917c478bd9Sstevel@tonic-gate 			}
61927c478bd9Sstevel@tonic-gate 		} else { /* directory must be specified */
61937c478bd9Sstevel@tonic-gate 			if (largc != 1)
61947c478bd9Sstevel@tonic-gate 				Error_cnt++;
6195ed69b652Sjs198686 			else if (access(*largv, 2) < 0 && (errno != EACCES))
6196ed69b652Sjs198686 				/*
6197ed69b652Sjs198686 				 * EACCES is ignored here as it may occur
6198ed69b652Sjs198686 				 * when any directory component of the path
6199ed69b652Sjs198686 				 * does not have write permission, even though
6200ed69b652Sjs198686 				 * the destination subdirectory has write
6201ed69b652Sjs198686 				 * access. Writing to a read only directory
6202ed69b652Sjs198686 				 * is handled later, as in "copy in" mode.
6203ed69b652Sjs198686 				 */
62047c478bd9Sstevel@tonic-gate 				msg(ERRN,
62057c478bd9Sstevel@tonic-gate 				    "Error during access() of \"%s\"", *largv);
62067c478bd9Sstevel@tonic-gate 		}
62077c478bd9Sstevel@tonic-gate 	}
62087c478bd9Sstevel@tonic-gate 	if (Error_cnt)
62097c478bd9Sstevel@tonic-gate 		usage(); /* exits! */
62107c478bd9Sstevel@tonic-gate 	if (Args & (OCi | OCo)) {
62117c478bd9Sstevel@tonic-gate 		if (!Dflag) {
62127c478bd9Sstevel@tonic-gate 			if (Args & (OCB | OCC)) {
62137c478bd9Sstevel@tonic-gate 				if (g_init(&Device, &Archive) < 0)
62147c478bd9Sstevel@tonic-gate 					msg(EXTN,
62157c478bd9Sstevel@tonic-gate 					    "Error during initialization");
62167c478bd9Sstevel@tonic-gate 			} else {
62177c478bd9Sstevel@tonic-gate 				if ((Bufsize = g_init(&Device, &Archive)) < 0)
62187c478bd9Sstevel@tonic-gate 					msg(EXTN,
62197c478bd9Sstevel@tonic-gate 					    "Error during initialization");
62207c478bd9Sstevel@tonic-gate 			}
62217c478bd9Sstevel@tonic-gate 		}
62227c478bd9Sstevel@tonic-gate 
62237c478bd9Sstevel@tonic-gate 		blk_cnt_max = _20K / Bufsize;
62247c478bd9Sstevel@tonic-gate 		if (blk_cnt_max < MX_BUFS) {
62257c478bd9Sstevel@tonic-gate 			blk_cnt_max = MX_BUFS;
62267c478bd9Sstevel@tonic-gate 		}
62277c478bd9Sstevel@tonic-gate 
62287c478bd9Sstevel@tonic-gate 		Buffr.b_base_p = NULL;
62297c478bd9Sstevel@tonic-gate 
62307c478bd9Sstevel@tonic-gate 		for (blk_cnt = blk_cnt_max; blk_cnt > 1; blk_cnt--) {
62317c478bd9Sstevel@tonic-gate 			Buffr.b_size = (size_t)(Bufsize * blk_cnt);
62327c478bd9Sstevel@tonic-gate 			Buffr.b_base_p = e_valloc(E_NORMAL, Buffr.b_size);
62337c478bd9Sstevel@tonic-gate 			if (Buffr.b_base_p != NULL) {
62347c478bd9Sstevel@tonic-gate 				break;
62357c478bd9Sstevel@tonic-gate 			}
62367c478bd9Sstevel@tonic-gate 		}
62377c478bd9Sstevel@tonic-gate 		if (Buffr.b_base_p == NULL || Buffr.b_size < (2 * CPIOBSZ)) {
62387c478bd9Sstevel@tonic-gate 			msg(EXT, "Out of memory");
62397c478bd9Sstevel@tonic-gate 		}
62407c478bd9Sstevel@tonic-gate 
62417c478bd9Sstevel@tonic-gate 		Buffr.b_out_p = Buffr.b_in_p = Buffr.b_base_p;
62427c478bd9Sstevel@tonic-gate 		Buffr.b_cnt = 0L;
62437c478bd9Sstevel@tonic-gate 		Buffr.b_end_p = Buffr.b_base_p + Buffr.b_size;
62447c478bd9Sstevel@tonic-gate 	}
62457c478bd9Sstevel@tonic-gate 
62467c478bd9Sstevel@tonic-gate 	/*
62477c478bd9Sstevel@tonic-gate 	 * Now that Bufsize has stabilized, we can allocate our i/o buffer
62487c478bd9Sstevel@tonic-gate 	 */
62497c478bd9Sstevel@tonic-gate 	Buf_p = e_valloc(E_EXIT, Bufsize);
62507c478bd9Sstevel@tonic-gate 
62517c478bd9Sstevel@tonic-gate 	if (Args & OCp) { /* get destination directory */
62527c478bd9Sstevel@tonic-gate 		(void) strcpy(Fullnam_p, *largv);
62537c478bd9Sstevel@tonic-gate 		if (stat(Fullnam_p, &DesSt) < 0)
62547c478bd9Sstevel@tonic-gate 			msg(EXTN, "Error during stat() of \"%s\"", Fullnam_p);
62557c478bd9Sstevel@tonic-gate 		if ((DesSt.st_mode & Ftype) != S_IFDIR)
62567c478bd9Sstevel@tonic-gate 			msg(EXT, "\"%s\" is not a directory", Fullnam_p);
62577c478bd9Sstevel@tonic-gate 	}
62587c478bd9Sstevel@tonic-gate 	Full_p = Fullnam_p + strlen(Fullnam_p) - 1;
62597c478bd9Sstevel@tonic-gate 	if (*Full_p != '/') {
62607c478bd9Sstevel@tonic-gate 		Full_p++;
62617c478bd9Sstevel@tonic-gate 		*Full_p = '/';
62627c478bd9Sstevel@tonic-gate 	}
62637c478bd9Sstevel@tonic-gate 	Full_p++;
62647c478bd9Sstevel@tonic-gate 	*Full_p = '\0';
62657c478bd9Sstevel@tonic-gate 	(void) strcpy(Lnknam_p, Fullnam_p);
62667c478bd9Sstevel@tonic-gate 	Lnkend_p = Lnknam_p + strlen(Lnknam_p);
62677c478bd9Sstevel@tonic-gate 	(void) getrlimit(RLIMIT_FSIZE, &rlim);
62687c478bd9Sstevel@tonic-gate 	Max_filesz = (off_t)rlim.rlim_cur;
62697c478bd9Sstevel@tonic-gate 	Lnk_hd.L_nxt_p = Lnk_hd.L_bck_p = &Lnk_hd;
6270ced83f9bSceastha 	Lnk_hd.L_lnk_p = NULL;
62717c478bd9Sstevel@tonic-gate }
62727c478bd9Sstevel@tonic-gate 
62737c478bd9Sstevel@tonic-gate /*
62747c478bd9Sstevel@tonic-gate  * set_tym: Set the access and/or modification times for a file.
62757c478bd9Sstevel@tonic-gate  */
62767c478bd9Sstevel@tonic-gate 
62777c478bd9Sstevel@tonic-gate static void
62787c478bd9Sstevel@tonic-gate set_tym(int dirfd, char *nam_p, time_t atime, time_t mtime)
62797c478bd9Sstevel@tonic-gate {
62807c478bd9Sstevel@tonic-gate 	struct timeval times[2];
62817c478bd9Sstevel@tonic-gate 
62827c478bd9Sstevel@tonic-gate 	times[0].tv_sec = atime;
62837c478bd9Sstevel@tonic-gate 	times[0].tv_usec = 0;
62847c478bd9Sstevel@tonic-gate 	times[1].tv_sec = mtime;
62857c478bd9Sstevel@tonic-gate 	times[1].tv_usec = 0;
62867c478bd9Sstevel@tonic-gate 
62877c478bd9Sstevel@tonic-gate 	if (futimesat(dirfd, nam_p, times) < 0) {
62887c478bd9Sstevel@tonic-gate 		if (Args & OCa) {
62897c478bd9Sstevel@tonic-gate 			msg(ERRN,
62907c478bd9Sstevel@tonic-gate 			    "Unable to reset access time for \"%s%s%s\"",
6291ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? nam_p : Fullnam_p,
6292ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
6293ced83f9bSceastha 			    G_p->g_rw_sysattr ? gettext(" System Attribute ") :
6294ced83f9bSceastha 			    gettext(" Attribute "),
6295ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" : nam_p);
62967c478bd9Sstevel@tonic-gate 		} else {
62977c478bd9Sstevel@tonic-gate 			msg(ERRN,
62987c478bd9Sstevel@tonic-gate 			    "Unable to reset modification time for \"%s%s%s\"",
6299ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? nam_p : Fullnam_p,
6300ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" :
6301ced83f9bSceastha 			    G_p->g_rw_sysattr ? gettext(" System Attribute ") :
6302ced83f9bSceastha 			    gettext(" Attribute "),
6303ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL) ? "" : nam_p);
63047c478bd9Sstevel@tonic-gate 		}
63057c478bd9Sstevel@tonic-gate 	}
63067c478bd9Sstevel@tonic-gate }
63077c478bd9Sstevel@tonic-gate 
63087c478bd9Sstevel@tonic-gate /*
63097c478bd9Sstevel@tonic-gate  * sigint:  Catch interrupts.  If an interrupt occurs during the extraction
63107c478bd9Sstevel@tonic-gate  * of a file from the archive with the -u option set, and the filename did
63117c478bd9Sstevel@tonic-gate  * exist, remove the current file and restore the original file.  Then exit.
63127c478bd9Sstevel@tonic-gate  */
63137c478bd9Sstevel@tonic-gate 
63147c478bd9Sstevel@tonic-gate /*ARGSUSED*/
63157c478bd9Sstevel@tonic-gate static void
63167c478bd9Sstevel@tonic-gate sigint(int sig)
63177c478bd9Sstevel@tonic-gate {
63187c478bd9Sstevel@tonic-gate 	char *nam_p;
63197c478bd9Sstevel@tonic-gate 
63207c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, SIG_IGN); /* block further signals */
63217c478bd9Sstevel@tonic-gate 	if (!Finished) {
63227c478bd9Sstevel@tonic-gate 		if (Args & OCi)
63237c478bd9Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
63247c478bd9Sstevel@tonic-gate 		else /* OCp */
63257c478bd9Sstevel@tonic-gate 			nam_p = Fullnam_p;
63267c478bd9Sstevel@tonic-gate 		if (*Over_p != '\0') { /* There is a temp file */
63277c478bd9Sstevel@tonic-gate 			if (unlink(nam_p) < 0) {
63287c478bd9Sstevel@tonic-gate 				msg(ERRN,
63297c478bd9Sstevel@tonic-gate 				    "Cannot remove incomplete \"%s\"", nam_p);
63307c478bd9Sstevel@tonic-gate 			}
63317c478bd9Sstevel@tonic-gate 			if (rename(Over_p, nam_p) < 0) {
63327c478bd9Sstevel@tonic-gate 				if (link(Over_p, nam_p) < 0) {
63337c478bd9Sstevel@tonic-gate 					msg(ERRN,
63347c478bd9Sstevel@tonic-gate 					    "Cannot recover original \"%s\"",
63357c478bd9Sstevel@tonic-gate 					    nam_p);
63367c478bd9Sstevel@tonic-gate 				}
63377c478bd9Sstevel@tonic-gate 				if (unlink(Over_p)) {
63387c478bd9Sstevel@tonic-gate 					msg(ERRN,
63397c478bd9Sstevel@tonic-gate 					    "Cannot remove temp file \"%s\"",
63407c478bd9Sstevel@tonic-gate 					    Over_p);
63417c478bd9Sstevel@tonic-gate 				}
63427c478bd9Sstevel@tonic-gate 			}
63437c478bd9Sstevel@tonic-gate 		} else if (unlink(nam_p))
63447c478bd9Sstevel@tonic-gate 			msg(ERRN,
63457c478bd9Sstevel@tonic-gate 			    "Cannot remove incomplete \"%s\"", nam_p);
63467c478bd9Sstevel@tonic-gate 			*Over_p = '\0';
63477c478bd9Sstevel@tonic-gate 	}
63487c478bd9Sstevel@tonic-gate 	exit(EXIT_CODE);
63497c478bd9Sstevel@tonic-gate }
63507c478bd9Sstevel@tonic-gate 
63517c478bd9Sstevel@tonic-gate /*
63527c478bd9Sstevel@tonic-gate  * swap: Swap bytes (-s), halfwords (-S) or or both halfwords and bytes (-b).
63537c478bd9Sstevel@tonic-gate  */
63547c478bd9Sstevel@tonic-gate 
63557c478bd9Sstevel@tonic-gate static void
63567c478bd9Sstevel@tonic-gate swap(char *buf_p, int cnt)
63577c478bd9Sstevel@tonic-gate {
63587c478bd9Sstevel@tonic-gate 	unsigned char tbyte;
63597c478bd9Sstevel@tonic-gate 	int tcnt;
63607c478bd9Sstevel@tonic-gate 	int rcnt;
63617c478bd9Sstevel@tonic-gate 	ushort_t thalf;
63627c478bd9Sstevel@tonic-gate 
63637c478bd9Sstevel@tonic-gate 	rcnt = cnt % 4;
63647c478bd9Sstevel@tonic-gate 	cnt /= 4;
63657c478bd9Sstevel@tonic-gate 	if (Args & (OCb | OCs | BSM)) {
63667c478bd9Sstevel@tonic-gate 		tcnt = cnt;
63677c478bd9Sstevel@tonic-gate 		/* LINTED alignment */
63687c478bd9Sstevel@tonic-gate 		Swp_p = (union swpbuf *)buf_p;
63697c478bd9Sstevel@tonic-gate 		while (tcnt-- > 0) {
63707c478bd9Sstevel@tonic-gate 			tbyte = Swp_p->s_byte[0];
63717c478bd9Sstevel@tonic-gate 			Swp_p->s_byte[0] = Swp_p->s_byte[1];
63727c478bd9Sstevel@tonic-gate 			Swp_p->s_byte[1] = tbyte;
63737c478bd9Sstevel@tonic-gate 			tbyte = Swp_p->s_byte[2];
63747c478bd9Sstevel@tonic-gate 			Swp_p->s_byte[2] = Swp_p->s_byte[3];
63757c478bd9Sstevel@tonic-gate 			Swp_p->s_byte[3] = tbyte;
63767c478bd9Sstevel@tonic-gate 			Swp_p++;
63777c478bd9Sstevel@tonic-gate 		}
63787c478bd9Sstevel@tonic-gate 		if (rcnt >= 2) {
63797c478bd9Sstevel@tonic-gate 		tbyte = Swp_p->s_byte[0];
63807c478bd9Sstevel@tonic-gate 		Swp_p->s_byte[0] = Swp_p->s_byte[1];
63817c478bd9Sstevel@tonic-gate 		Swp_p->s_byte[1] = tbyte;
63827c478bd9Sstevel@tonic-gate 		tbyte = Swp_p->s_byte[2];
63837c478bd9Sstevel@tonic-gate 		}
63847c478bd9Sstevel@tonic-gate 	}
63857c478bd9Sstevel@tonic-gate 	if (Args & (OCb | OCS)) {
63867c478bd9Sstevel@tonic-gate 		tcnt = cnt;
63877c478bd9Sstevel@tonic-gate 		/* LINTED alignment */
63887c478bd9Sstevel@tonic-gate 		Swp_p = (union swpbuf *)buf_p;
63897c478bd9Sstevel@tonic-gate 		while (tcnt-- > 0) {
63907c478bd9Sstevel@tonic-gate 			thalf = Swp_p->s_half[0];
63917c478bd9Sstevel@tonic-gate 			Swp_p->s_half[0] = Swp_p->s_half[1];
63927c478bd9Sstevel@tonic-gate 			Swp_p->s_half[1] = thalf;
63937c478bd9Sstevel@tonic-gate 			Swp_p++;
63947c478bd9Sstevel@tonic-gate 		}
63957c478bd9Sstevel@tonic-gate 	}
63967c478bd9Sstevel@tonic-gate }
63977c478bd9Sstevel@tonic-gate 
63987c478bd9Sstevel@tonic-gate /*
63997c478bd9Sstevel@tonic-gate  * usage: Print the usage message on stderr and exit.
64007c478bd9Sstevel@tonic-gate  */
64017c478bd9Sstevel@tonic-gate 
64027c478bd9Sstevel@tonic-gate static void
64037c478bd9Sstevel@tonic-gate usage(void)
64047c478bd9Sstevel@tonic-gate {
64057c478bd9Sstevel@tonic-gate 
64067c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
64077c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
64087c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("USAGE:\n"
64097c478bd9Sstevel@tonic-gate 	    "\tcpio -i[bcdfkmrstuv@BSV6] [-C size] "
64107c478bd9Sstevel@tonic-gate 	    "[-E file] [-H hdr] [-I file [-M msg]] "
64117c478bd9Sstevel@tonic-gate 	    "[-R id] [patterns]\n"
64127c478bd9Sstevel@tonic-gate 	    "\tcpio -o[acv@ABLV] [-C size] "
64137c478bd9Sstevel@tonic-gate 	    "[-H hdr] [-O file [-M msg]]\n"
64147c478bd9Sstevel@tonic-gate 	    "\tcpio -p[adlmuv@LV] [-R id] directory\n"));
64157c478bd9Sstevel@tonic-gate #else
64167c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("USAGE:\n"
64177c478bd9Sstevel@tonic-gate 	    "\tcpio -i[bcdfkmrstuvBSV6] [-C size] "
64187c478bd9Sstevel@tonic-gate 	    "[-E file] [-H hdr] [-I file [-M msg]] "
64197c478bd9Sstevel@tonic-gate 	    "[-R id] [patterns]\n"
64207c478bd9Sstevel@tonic-gate 	    "\tcpio -o[acvABLV] [-C size] "
64217c478bd9Sstevel@tonic-gate 	    "[-H hdr] [-O file [-M msg]]\n"
64227c478bd9Sstevel@tonic-gate 	    "\tcpio -p[adlmuvLV] [-R id] directory\n"));
64237c478bd9Sstevel@tonic-gate #endif
64247c478bd9Sstevel@tonic-gate 	(void) fflush(stderr);
64257c478bd9Sstevel@tonic-gate 	exit(EXIT_CODE);
64267c478bd9Sstevel@tonic-gate }
64277c478bd9Sstevel@tonic-gate 
64287c478bd9Sstevel@tonic-gate /*
64297c478bd9Sstevel@tonic-gate  * verbose: For each file, print either the filename (-v) or a dot (-V).
64307c478bd9Sstevel@tonic-gate  * If the -t option (table of contents) is set, print either the filename,
64317c478bd9Sstevel@tonic-gate  * or if the -v option is also set, print an "ls -l"-like listing.
64327c478bd9Sstevel@tonic-gate  */
64337c478bd9Sstevel@tonic-gate 
64347c478bd9Sstevel@tonic-gate static void
64357c478bd9Sstevel@tonic-gate verbose(char *nam_p)
64367c478bd9Sstevel@tonic-gate {
64377c478bd9Sstevel@tonic-gate 	int i, j, temp;
64387c478bd9Sstevel@tonic-gate 	mode_t mode;
64397c478bd9Sstevel@tonic-gate 	char modestr[12];
64407c478bd9Sstevel@tonic-gate 
64417c478bd9Sstevel@tonic-gate 	/*
64427c478bd9Sstevel@tonic-gate 	 * The printf format and associated arguments to print the current
64437c478bd9Sstevel@tonic-gate 	 * filename.  Normally, just nam_p.  If we're processing an extended
64447c478bd9Sstevel@tonic-gate 	 * attribute, these are overridden.
64457c478bd9Sstevel@tonic-gate 	 */
64467c478bd9Sstevel@tonic-gate 	char *name_fmt = "%s";
64477c478bd9Sstevel@tonic-gate 	const char *name = nam_p;
64487c478bd9Sstevel@tonic-gate 	const char *attribute = NULL;
64497c478bd9Sstevel@tonic-gate 
6450ced83f9bSceastha 	if (Gen.g_attrnam_p != NULL) {
64517c478bd9Sstevel@tonic-gate 		/*
64527c478bd9Sstevel@tonic-gate 		 * Translation note:
64537c478bd9Sstevel@tonic-gate 		 * 'attribute' is a noun.
64547c478bd9Sstevel@tonic-gate 		 */
6455da6c28aaSamw 
6456ced83f9bSceastha 		if (Gen.g_rw_sysattr) {
6457ced83f9bSceastha 			name_fmt = gettext("%s system attribute %s");
6458ced83f9bSceastha 		} else if ((Args & OCt) &&
6459ced83f9bSceastha 		    (is_sysattr(basename(Gen.g_attrnam_p)))) {
6460da6c28aaSamw 			name_fmt = gettext("%s system attribute %s");
6461da6c28aaSamw 		} else {
64627c478bd9Sstevel@tonic-gate 			name_fmt = gettext("%s attribute %s");
6463da6c28aaSamw 		}
64647c478bd9Sstevel@tonic-gate 
64657c478bd9Sstevel@tonic-gate 		name = (Args & OCp) ? nam_p : Gen.g_attrfnam_p;
6466ced83f9bSceastha 		if (Gen.g_attrparent_p == NULL) {
64677c478bd9Sstevel@tonic-gate 			attribute = Gen.g_attrnam_p;
6468ced83f9bSceastha 		} else {
6469ced83f9bSceastha 			attribute = Gen.g_attrpath_p;
6470ced83f9bSceastha 		}
64717c478bd9Sstevel@tonic-gate 	}
64727c478bd9Sstevel@tonic-gate 
64737c478bd9Sstevel@tonic-gate 	if ((Gen.g_mode == SECMODE) || ((Hdr_type == USTAR ||
64747c478bd9Sstevel@tonic-gate 	    Hdr_type == TAR) && Thdr_p->tbuf.t_typeflag == 'A')) {
64757c478bd9Sstevel@tonic-gate 		/* dont print ancillary file */
64767c478bd9Sstevel@tonic-gate 		aclchar = '+';
64777c478bd9Sstevel@tonic-gate 		return;
64787c478bd9Sstevel@tonic-gate 	}
64797c478bd9Sstevel@tonic-gate 	for (i = 0; i < 11; i++)
64807c478bd9Sstevel@tonic-gate 		modestr[i] = '-';
64817c478bd9Sstevel@tonic-gate 	modestr[i] = '\0';
64827c478bd9Sstevel@tonic-gate 	modestr[i-1] = aclchar;
64837c478bd9Sstevel@tonic-gate 	aclchar = ' ';
64847c478bd9Sstevel@tonic-gate 
64857c478bd9Sstevel@tonic-gate 	if ((Args & OCt) && (Args & OCv)) {
64867c478bd9Sstevel@tonic-gate 		mode = Gen.g_mode;
64877c478bd9Sstevel@tonic-gate 		for (i = 0; i < 3; i++) {
64887c478bd9Sstevel@tonic-gate 			temp = (mode >> (6 - (i * 3)));
64897c478bd9Sstevel@tonic-gate 			j = (i * 3) + 1;
64907c478bd9Sstevel@tonic-gate 			if (S_IROTH & temp)
64917c478bd9Sstevel@tonic-gate 				modestr[j] = 'r';
64927c478bd9Sstevel@tonic-gate 			if (S_IWOTH & temp)
64937c478bd9Sstevel@tonic-gate 				modestr[j + 1] = 'w';
64947c478bd9Sstevel@tonic-gate 			if (S_IXOTH & temp)
64957c478bd9Sstevel@tonic-gate 				modestr[j + 2] = 'x';
64967c478bd9Sstevel@tonic-gate 		}
64977c478bd9Sstevel@tonic-gate 
64987c478bd9Sstevel@tonic-gate 		if (Hdr_type != BAR) {
64997c478bd9Sstevel@tonic-gate 			temp = Gen.g_mode & Ftype;
65007c478bd9Sstevel@tonic-gate 			switch (temp) {
65017c478bd9Sstevel@tonic-gate 			case (S_IFIFO):
65027c478bd9Sstevel@tonic-gate 				modestr[0] = 'p';
65037c478bd9Sstevel@tonic-gate 				break;
6504ced83f9bSceastha 			case (S_IFSOCK):
6505ced83f9bSceastha 				modestr[0] = 's';
6506ced83f9bSceastha 				break;
65077c478bd9Sstevel@tonic-gate 			case (S_IFCHR):
65087c478bd9Sstevel@tonic-gate 				modestr[0] = 'c';
65097c478bd9Sstevel@tonic-gate 				break;
65107c478bd9Sstevel@tonic-gate 			case (S_IFDIR):
65117c478bd9Sstevel@tonic-gate 				modestr[0] = 'd';
65127c478bd9Sstevel@tonic-gate 				break;
65137c478bd9Sstevel@tonic-gate 			case (S_IFBLK):
65147c478bd9Sstevel@tonic-gate 				modestr[0] = 'b';
65157c478bd9Sstevel@tonic-gate 				break;
65167c478bd9Sstevel@tonic-gate 			case (S_IFREG): /* was initialized to '-' */
65177c478bd9Sstevel@tonic-gate 				break;
65187c478bd9Sstevel@tonic-gate 			case (S_IFLNK):
65197c478bd9Sstevel@tonic-gate 				modestr[0] = 'l';
65207c478bd9Sstevel@tonic-gate 				break;
65217c478bd9Sstevel@tonic-gate 			default:
65227c478bd9Sstevel@tonic-gate 				msg(ERR, "Impossible file type");
65237c478bd9Sstevel@tonic-gate 			}
65247c478bd9Sstevel@tonic-gate 		} else {		/* bar */
65257c478bd9Sstevel@tonic-gate 			temp = Gen.g_mode & Ftype;
65267c478bd9Sstevel@tonic-gate 			switch (temp) {
65277c478bd9Sstevel@tonic-gate 			case (S_IFIFO):
65287c478bd9Sstevel@tonic-gate 				modestr[0] = 'p';
65297c478bd9Sstevel@tonic-gate 				break;
6530ced83f9bSceastha 			case (S_IFSOCK):
6531ced83f9bSceastha 				modestr[0] = 's';
6532ced83f9bSceastha 				break;
65337c478bd9Sstevel@tonic-gate 			case (S_IFCHR):
65347c478bd9Sstevel@tonic-gate 				modestr[0] = 'c';
65357c478bd9Sstevel@tonic-gate 				break;
65367c478bd9Sstevel@tonic-gate 			case (S_IFDIR):
65377c478bd9Sstevel@tonic-gate 				modestr[0] = 'd';
65387c478bd9Sstevel@tonic-gate 				break;
65397c478bd9Sstevel@tonic-gate 			case (S_IFBLK):
65407c478bd9Sstevel@tonic-gate 				modestr[0] = 'b';
65417c478bd9Sstevel@tonic-gate 				break;
65427c478bd9Sstevel@tonic-gate 			}
65437c478bd9Sstevel@tonic-gate 			if (bar_linkflag == SYMTYPE)
65447c478bd9Sstevel@tonic-gate 				modestr[0] = 'l';
65457c478bd9Sstevel@tonic-gate 		}
65467c478bd9Sstevel@tonic-gate 		if ((S_ISUID & Gen.g_mode) == S_ISUID)
65477c478bd9Sstevel@tonic-gate 			modestr[3] = 's';
65487c478bd9Sstevel@tonic-gate 		if ((S_ISVTX & Gen.g_mode) == S_ISVTX)
65497c478bd9Sstevel@tonic-gate 			modestr[9] = 't';
65507c478bd9Sstevel@tonic-gate 		if ((S_ISGID & G_p->g_mode) == S_ISGID && modestr[6] == 'x')
65517c478bd9Sstevel@tonic-gate 			modestr[6] = 's';
65527c478bd9Sstevel@tonic-gate 		else if ((S_ENFMT & Gen.g_mode) == S_ENFMT && modestr[6] != 'x')
65537c478bd9Sstevel@tonic-gate 			modestr[6] = 'l';
65547c478bd9Sstevel@tonic-gate 		if ((Hdr_type == TAR || Hdr_type == USTAR) && Gen.g_nlink == 0)
65557c478bd9Sstevel@tonic-gate 			(void) printf("%s%4d ", modestr, (int)Gen.g_nlink+1);
65567c478bd9Sstevel@tonic-gate 		else
65577c478bd9Sstevel@tonic-gate 			(void) printf("%s%4d ", modestr, (int)Gen.g_nlink);
65587c478bd9Sstevel@tonic-gate 		if (Lastuid == (int)Gen.g_uid) {
65597c478bd9Sstevel@tonic-gate 			if (Lastuid == -1)
65607c478bd9Sstevel@tonic-gate 				(void) printf("-1       ");
65617c478bd9Sstevel@tonic-gate 			else
65627c478bd9Sstevel@tonic-gate 				(void) printf("%-9s", Curpw_p->pw_name);
65637c478bd9Sstevel@tonic-gate 		} else {
65647c478bd9Sstevel@tonic-gate 			if (Curpw_p = getpwuid((int)Gen.g_uid)) {
65657c478bd9Sstevel@tonic-gate 				(void) printf("%-9s", Curpw_p->pw_name);
65667c478bd9Sstevel@tonic-gate 				Lastuid = (int)Gen.g_uid;
65677c478bd9Sstevel@tonic-gate 			} else {
65687c478bd9Sstevel@tonic-gate 				(void) printf("%-9d", (int)Gen.g_uid);
65697c478bd9Sstevel@tonic-gate 				Lastuid = -1;
65707c478bd9Sstevel@tonic-gate 			}
65717c478bd9Sstevel@tonic-gate 		}
65727c478bd9Sstevel@tonic-gate 		if (Lastgid == (int)Gen.g_gid) {
65737c478bd9Sstevel@tonic-gate 			if (Lastgid == -1)
65747c478bd9Sstevel@tonic-gate 				(void) printf("-1       ");
65757c478bd9Sstevel@tonic-gate 			else
65767c478bd9Sstevel@tonic-gate 				(void) printf("%-9s", Curgr_p->gr_name);
65777c478bd9Sstevel@tonic-gate 		} else {
65787c478bd9Sstevel@tonic-gate 			if (Curgr_p = getgrgid((int)Gen.g_gid)) {
65797c478bd9Sstevel@tonic-gate 				(void) printf("%-9s", Curgr_p->gr_name);
65807c478bd9Sstevel@tonic-gate 				Lastgid = (int)Gen.g_gid;
65817c478bd9Sstevel@tonic-gate 			} else {
65827c478bd9Sstevel@tonic-gate 				(void) printf("%-9d", (int)Gen.g_gid);
65837c478bd9Sstevel@tonic-gate 				Lastgid = -1;
65847c478bd9Sstevel@tonic-gate 			}
65857c478bd9Sstevel@tonic-gate 		}
65867c478bd9Sstevel@tonic-gate 
65877c478bd9Sstevel@tonic-gate 		/* print file size */
65887c478bd9Sstevel@tonic-gate 		if (!Aspec || ((Gen.g_mode & Ftype) == S_IFIFO) ||
6589ced83f9bSceastha 		    ((Gen.g_mode & Ftype) == S_IFSOCK) ||
65907c478bd9Sstevel@tonic-gate 		    (Hdr_type == BAR && bar_linkflag == SYMTYPE)) {
65917c478bd9Sstevel@tonic-gate 			if (Gen.g_filesz < (1LL << 31))
65927c478bd9Sstevel@tonic-gate 				(void) printf("%7lld ",
65937c478bd9Sstevel@tonic-gate 				    (offset_t)Gen.g_filesz);
65947c478bd9Sstevel@tonic-gate 			else
65957c478bd9Sstevel@tonic-gate 				(void) printf("%11lld ",
65967c478bd9Sstevel@tonic-gate 				    (offset_t)Gen.g_filesz);
65977c478bd9Sstevel@tonic-gate 		} else
65987c478bd9Sstevel@tonic-gate 			(void) printf("%3d,%3d ", (int)major(Gen.g_rdev),
65997c478bd9Sstevel@tonic-gate 			    (int)minor(Gen.g_rdev));
66007c478bd9Sstevel@tonic-gate 		(void) cftime(Time, dcgettext(NULL, FORMAT, LC_TIME),
66017c478bd9Sstevel@tonic-gate 		    (time_t *)&Gen.g_mtime);
66027c478bd9Sstevel@tonic-gate 		(void) printf("%s, ", Time);
66037c478bd9Sstevel@tonic-gate 		(void) printf(name_fmt, name, attribute);
66047c478bd9Sstevel@tonic-gate 		if ((Gen.g_mode & Ftype) == S_IFLNK) {
66057c478bd9Sstevel@tonic-gate 			if (Hdr_type == USTAR || Hdr_type == TAR)
66067c478bd9Sstevel@tonic-gate 				(void) strcpy(Symlnk_p,
66077c478bd9Sstevel@tonic-gate 				    Thdr_p->tbuf.t_linkname);
66087c478bd9Sstevel@tonic-gate 			else {
6609*32fd2847Snakanon 				FILL(Gen.g_filesz);
66107c478bd9Sstevel@tonic-gate 				(void) strncpy(Symlnk_p, Buffr.b_out_p,
66117c478bd9Sstevel@tonic-gate 				    Gen.g_filesz);
66127c478bd9Sstevel@tonic-gate 				*(Symlnk_p + Gen.g_filesz) = '\0';
66137c478bd9Sstevel@tonic-gate 			}
66147c478bd9Sstevel@tonic-gate 			(void) printf(" -> %s", Symlnk_p);
66157c478bd9Sstevel@tonic-gate 		}
66167c478bd9Sstevel@tonic-gate 		if (Hdr_type == BAR) {
66177c478bd9Sstevel@tonic-gate 			if (bar_linkflag == SYMTYPE)
66187c478bd9Sstevel@tonic-gate 				(void) printf(gettext(" symbolic link to %s"),
66197c478bd9Sstevel@tonic-gate 				    bar_linkname);
66207c478bd9Sstevel@tonic-gate 			else if (bar_linkflag == '1')
66217c478bd9Sstevel@tonic-gate 				(void) printf(gettext(" linked to %s"),
66227c478bd9Sstevel@tonic-gate 				    bar_linkname);
66237c478bd9Sstevel@tonic-gate 		}
66247c478bd9Sstevel@tonic-gate 		if ((Hdr_type == USTAR || Hdr_type == TAR) &&
66257c478bd9Sstevel@tonic-gate 		    Thdr_p->tbuf.t_typeflag == '1') {
66267c478bd9Sstevel@tonic-gate 			(void) printf(gettext(" linked to %s%s%s"),
6627ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ?
66287c478bd9Sstevel@tonic-gate 			    Thdr_p->tbuf.t_linkname : Gen.g_attrfnam_p,
6629ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ? "" :
6630ced83f9bSceastha 			    gettext(" attribute "),
6631ced83f9bSceastha 			    (Gen.g_attrnam_p == NULL) ?
66327c478bd9Sstevel@tonic-gate 			    "" : Gen.g_linktoattrnam_p);
66337c478bd9Sstevel@tonic-gate 		}
66347c478bd9Sstevel@tonic-gate 		(void) printf("\n");
66357c478bd9Sstevel@tonic-gate 	} else if ((Args & OCt) || (Args & OCv)) {
66367c478bd9Sstevel@tonic-gate 		(void) fprintf(Out_p, name_fmt, name, attribute);
66377c478bd9Sstevel@tonic-gate 		(void) fputc('\n', Out_p);
66387c478bd9Sstevel@tonic-gate 	} else { /* OCV */
66397c478bd9Sstevel@tonic-gate 		(void) fputc('.', Out_p);
66407c478bd9Sstevel@tonic-gate 		if (Verbcnt++ >= 49) { /* start a new line of dots */
66417c478bd9Sstevel@tonic-gate 			Verbcnt = 0;
66427c478bd9Sstevel@tonic-gate 			(void) fputc('\n', Out_p);
66437c478bd9Sstevel@tonic-gate 		}
66447c478bd9Sstevel@tonic-gate 	}
66457c478bd9Sstevel@tonic-gate 	(void) fflush(Out_p);
66467c478bd9Sstevel@tonic-gate }
66477c478bd9Sstevel@tonic-gate 
66487c478bd9Sstevel@tonic-gate #define	MK_USHORT(a)	(a & 00000177777)
66497c478bd9Sstevel@tonic-gate 
66507c478bd9Sstevel@tonic-gate /*
66517c478bd9Sstevel@tonic-gate  * write_hdr: Transfer header information for the generic structure
66527c478bd9Sstevel@tonic-gate  * into the format for the selected header and bwrite() the header.
66537c478bd9Sstevel@tonic-gate  * ACL support: add two new argumnets. secflag indicates that it's an
66547c478bd9Sstevel@tonic-gate  *	ancillary file. len is the size of the file (incl. all security
66557c478bd9Sstevel@tonic-gate  *	attributes). We only have acls now.
66567c478bd9Sstevel@tonic-gate  */
66577c478bd9Sstevel@tonic-gate 
66587c478bd9Sstevel@tonic-gate static void
66597c478bd9Sstevel@tonic-gate write_hdr(int secflag, off_t len)
66607c478bd9Sstevel@tonic-gate {
66617c478bd9Sstevel@tonic-gate 	int cnt, pad;
66627c478bd9Sstevel@tonic-gate 	mode_t mode;
66637c478bd9Sstevel@tonic-gate 	uid_t uid;
66647c478bd9Sstevel@tonic-gate 	gid_t gid;
66657c478bd9Sstevel@tonic-gate 	const char warnfmt[] = "%s%s%s : %s";
66667c478bd9Sstevel@tonic-gate 
66677c478bd9Sstevel@tonic-gate 	if (secflag == ARCHIVE_ACL) {
66687c478bd9Sstevel@tonic-gate 		mode = SECMODE;
66697c478bd9Sstevel@tonic-gate 	} else {
66707c478bd9Sstevel@tonic-gate 		/*
66717c478bd9Sstevel@tonic-gate 		 * If attribute is being archived in cpio format then
66727c478bd9Sstevel@tonic-gate 		 * zap off the file type bits since those are truly a
66737c478bd9Sstevel@tonic-gate 		 * mask and reset them with _XATTR_CPIO_MODE
66747c478bd9Sstevel@tonic-gate 		 */
66757c478bd9Sstevel@tonic-gate 
66767c478bd9Sstevel@tonic-gate 		/*
66777c478bd9Sstevel@tonic-gate 		 * len is the value of g_filesz for normal files
66787c478bd9Sstevel@tonic-gate 		 * and the length of the special header buffer in
66797c478bd9Sstevel@tonic-gate 		 * the case of acl and xattr headers.
66807c478bd9Sstevel@tonic-gate 		 */
6681ced83f9bSceastha 		if (G_p->g_attrnam_p != NULL && Hdr_type != USTAR &&
66827c478bd9Sstevel@tonic-gate 		    Hdr_type != TAR) {
6683d2443e76Smarks 			mode = (G_p->g_mode & POSIXMODES) | _XATTR_CPIO_MODE;
66847c478bd9Sstevel@tonic-gate 		} else {
66857c478bd9Sstevel@tonic-gate 			mode = G_p->g_mode;
66867c478bd9Sstevel@tonic-gate 		}
66877c478bd9Sstevel@tonic-gate 		if (secflag != ARCHIVE_XATTR) {
66887c478bd9Sstevel@tonic-gate 			len = G_p->g_filesz;
66897c478bd9Sstevel@tonic-gate 		}
66907c478bd9Sstevel@tonic-gate 	}
66917c478bd9Sstevel@tonic-gate 
66927c478bd9Sstevel@tonic-gate 	uid = G_p->g_uid;
66937c478bd9Sstevel@tonic-gate 	gid = G_p->g_gid;
66947c478bd9Sstevel@tonic-gate 	/*
66957c478bd9Sstevel@tonic-gate 	 * Handle EFT uids and gids.  If they get too big
66967c478bd9Sstevel@tonic-gate 	 * to be represented in a particular format, force 'em to 'nobody'.
66977c478bd9Sstevel@tonic-gate 	 */
66987c478bd9Sstevel@tonic-gate 	switch (Hdr_type) {
66997c478bd9Sstevel@tonic-gate 	case BIN:			/* 16-bits of u_short */
67007c478bd9Sstevel@tonic-gate 		if ((ulong_t)uid > (ulong_t)USHRT_MAX)
67017c478bd9Sstevel@tonic-gate 			uid = UID_NOBODY;
67027c478bd9Sstevel@tonic-gate 		if ((ulong_t)gid > (ulong_t)USHRT_MAX)
67037c478bd9Sstevel@tonic-gate 			gid = GID_NOBODY;
67047c478bd9Sstevel@tonic-gate 		break;
67057c478bd9Sstevel@tonic-gate 	case CHR:			/* %.6lo => 262143 base 10 */
67067c478bd9Sstevel@tonic-gate 		if ((ulong_t)uid > (ulong_t)0777777)
67077c478bd9Sstevel@tonic-gate 			uid = UID_NOBODY;
67087c478bd9Sstevel@tonic-gate 		if ((ulong_t)gid > (ulong_t)0777777)
67097c478bd9Sstevel@tonic-gate 			gid = GID_NOBODY;
67107c478bd9Sstevel@tonic-gate 		break;
67117c478bd9Sstevel@tonic-gate 	case ASC:			/* %.8lx => full 32 bits */
67127c478bd9Sstevel@tonic-gate 	case CRC:
67137c478bd9Sstevel@tonic-gate 		break;
67147c478bd9Sstevel@tonic-gate 	case USTAR:
67157c478bd9Sstevel@tonic-gate 	case TAR:			/* %.7lo => 2097151 base 10 */
67167c478bd9Sstevel@tonic-gate 		if ((ulong_t)uid > (ulong_t)07777777)
67177c478bd9Sstevel@tonic-gate 			uid = UID_NOBODY;
67187c478bd9Sstevel@tonic-gate 		if ((ulong_t)gid > (ulong_t)07777777)
67197c478bd9Sstevel@tonic-gate 			gid = GID_NOBODY;
67207c478bd9Sstevel@tonic-gate 		break;
67217c478bd9Sstevel@tonic-gate 	default:
67227c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
67237c478bd9Sstevel@tonic-gate 	}
67247c478bd9Sstevel@tonic-gate 
67257c478bd9Sstevel@tonic-gate 	/*
67267c478bd9Sstevel@tonic-gate 	 * Since cpio formats -don't- encode the symbolic names, print
67277c478bd9Sstevel@tonic-gate 	 * a warning message when we map the uid or gid this way.
67287c478bd9Sstevel@tonic-gate 	 * Also, if the ownership just changed, clear set[ug]id bits
67297c478bd9Sstevel@tonic-gate 	 *
67307c478bd9Sstevel@tonic-gate 	 * (Except for USTAR format of course, where we have a string
67317c478bd9Sstevel@tonic-gate 	 * representation of the username embedded in the header)
67327c478bd9Sstevel@tonic-gate 	 */
67337c478bd9Sstevel@tonic-gate 	if (uid != G_p->g_uid && Hdr_type != USTAR) {
67347c478bd9Sstevel@tonic-gate 		msg(ERR, warnfmt,
6735ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ?
67367c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p : G_p->g_attrfnam_p,
6737ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? "" : G_p->g_rw_sysattr ?
6738ced83f9bSceastha 		    gettext(" System Attribute ") : gettext(" Attribute "),
6739ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? "" : G_p->g_attrnam_p,
67407c478bd9Sstevel@tonic-gate 		    gettext("uid too large for archive format"));
67417c478bd9Sstevel@tonic-gate 		if (S_ISREG(mode))
67427c478bd9Sstevel@tonic-gate 			mode &= ~S_ISUID;
67437c478bd9Sstevel@tonic-gate 	}
67447c478bd9Sstevel@tonic-gate 	if (gid != G_p->g_gid && Hdr_type != USTAR) {
67457c478bd9Sstevel@tonic-gate 		msg(ERR, warnfmt,
6746ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ?
67477c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p : G_p->g_attrfnam_p,
6748ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? "" : G_p->g_rw_sysattr ?
6749ced83f9bSceastha 		    gettext(" System Attribute ") : gettext(" Attribute "),
6750ced83f9bSceastha 		    (G_p->g_attrnam_p == NULL) ? "" : G_p->g_attrnam_p,
67517c478bd9Sstevel@tonic-gate 		    gettext("gid too large for archive format"));
67527c478bd9Sstevel@tonic-gate 		if (S_ISREG(mode))
67537c478bd9Sstevel@tonic-gate 			mode &= ~S_ISGID;
67547c478bd9Sstevel@tonic-gate 	}
67557c478bd9Sstevel@tonic-gate 
67567c478bd9Sstevel@tonic-gate 	switch (Hdr_type) {
67577c478bd9Sstevel@tonic-gate 	case BIN:
67587c478bd9Sstevel@tonic-gate 	case CHR:
67597c478bd9Sstevel@tonic-gate 	case ASC:
67607c478bd9Sstevel@tonic-gate 	case CRC:
67617c478bd9Sstevel@tonic-gate 		cnt = Hdrsz + G_p->g_namesz;
67627c478bd9Sstevel@tonic-gate 		break;
67637c478bd9Sstevel@tonic-gate 	case TAR:
67647c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
67657c478bd9Sstevel@tonic-gate 	case USTAR: /* TAR and USTAR */
67667c478bd9Sstevel@tonic-gate 		cnt = TARSZ;
67677c478bd9Sstevel@tonic-gate 		break;
67687c478bd9Sstevel@tonic-gate 	default:
67697c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
67707c478bd9Sstevel@tonic-gate 	}
67717c478bd9Sstevel@tonic-gate 	FLUSH(cnt);
67727c478bd9Sstevel@tonic-gate 
67737c478bd9Sstevel@tonic-gate 	switch (Hdr_type) {
67747c478bd9Sstevel@tonic-gate 	case BIN:
67757c478bd9Sstevel@tonic-gate 		Hdr.h_magic = (short)G_p->g_magic;
67767c478bd9Sstevel@tonic-gate 		Hdr.h_dev = G_p->g_dev;
67777c478bd9Sstevel@tonic-gate 		Hdr.h_ino = G_p->g_ino;
67787c478bd9Sstevel@tonic-gate 		Hdr.h_uid = uid;
67797c478bd9Sstevel@tonic-gate 		Hdr.h_gid = gid;
67807c478bd9Sstevel@tonic-gate 		Hdr.h_mode = mode;
67817c478bd9Sstevel@tonic-gate 		Hdr.h_nlink = G_p->g_nlink;
67827c478bd9Sstevel@tonic-gate 		Hdr.h_rdev = G_p->g_rdev;
67837c478bd9Sstevel@tonic-gate 		mkshort(Hdr.h_mtime, (long)G_p->g_mtime);
67847c478bd9Sstevel@tonic-gate 		Hdr.h_namesize = (short)G_p->g_namesz;
67857c478bd9Sstevel@tonic-gate 		mkshort(Hdr.h_filesize, (long)len);
67867c478bd9Sstevel@tonic-gate 		(void) strcpy(Hdr.h_name, G_p->g_nam_p);
67877c478bd9Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, &Hdr, cnt);
67887c478bd9Sstevel@tonic-gate 		break;
67897c478bd9Sstevel@tonic-gate 	case CHR:
67907c478bd9Sstevel@tonic-gate 		(void) sprintf(Buffr.b_in_p,
67917c478bd9Sstevel@tonic-gate 		    "%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.11lo%.6lo%."
67927c478bd9Sstevel@tonic-gate 		    "11llo%s", G_p->g_magic, G_p->g_dev, G_p->g_ino, mode,
67937c478bd9Sstevel@tonic-gate 		    uid, gid, G_p->g_nlink, MK_USHORT(G_p->g_rdev),
67947c478bd9Sstevel@tonic-gate 		    G_p->g_mtime, (long)G_p->g_namesz, (offset_t)len,
67957c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p);
67967c478bd9Sstevel@tonic-gate 		break;
67977c478bd9Sstevel@tonic-gate 	case ASC:
67987c478bd9Sstevel@tonic-gate 	case CRC:
67997c478bd9Sstevel@tonic-gate 		(void) sprintf(Buffr.b_in_p,
68007c478bd9Sstevel@tonic-gate 		    "%.6lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%."
68017c478bd9Sstevel@tonic-gate 		    "8lx%.8lx%.8lx%.8lx%s",
68027c478bd9Sstevel@tonic-gate 		    G_p->g_magic, G_p->g_ino, mode, G_p->g_uid,
6803944d83a0Schin 		    G_p->g_gid, G_p->g_nlink, G_p->g_mtime, (ulong_t)len,
68047c478bd9Sstevel@tonic-gate 		    major(G_p->g_dev), minor(G_p->g_dev),
68057c478bd9Sstevel@tonic-gate 		    major(G_p->g_rdev), minor(G_p->g_rdev),
68067c478bd9Sstevel@tonic-gate 		    G_p->g_namesz, G_p->g_cksum, G_p->g_nam_p);
68077c478bd9Sstevel@tonic-gate 		break;
68087c478bd9Sstevel@tonic-gate 	case USTAR:
68097c478bd9Sstevel@tonic-gate 		Thdr_p = (union tblock *)Buffr.b_in_p;
68107c478bd9Sstevel@tonic-gate 		(void) memcpy(Thdr_p, Empty, TARSZ);
68117c478bd9Sstevel@tonic-gate 		(void) strncpy(Thdr_p->tbuf.t_name, G_p->g_tname,
68127c478bd9Sstevel@tonic-gate 		    (int)strlen(G_p->g_tname));
68137c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mode, "%07o", (int)mode);
68147c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_uid, "%07o", (int)uid);
68157c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_gid, "%07o", (int)gid);
68167c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_size, "%011llo",
68177c478bd9Sstevel@tonic-gate 		    (offset_t)len);
68187c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mtime, "%011lo", G_p->g_mtime);
68197c478bd9Sstevel@tonic-gate 		if (secflag == ARCHIVE_ACL) {
68207c478bd9Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = 'A';	/* ACL file type */
68217c478bd9Sstevel@tonic-gate 		} else if (secflag == ARCHIVE_XATTR ||
6822ced83f9bSceastha 		    (G_p->g_attrnam_p != NULL)) {
68237c478bd9Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = _XATTR_HDRTYPE;
68247c478bd9Sstevel@tonic-gate 		} else {
68257c478bd9Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = G_p->g_typeflag;
68267c478bd9Sstevel@tonic-gate 		}
68277c478bd9Sstevel@tonic-gate 		if (T_lname[0] != '\0') {
68287c478bd9Sstevel@tonic-gate 			/*
68297c478bd9Sstevel@tonic-gate 			 * if not a symbolic link
68307c478bd9Sstevel@tonic-gate 			 */
68317c478bd9Sstevel@tonic-gate 			if (((G_p->g_mode & Ftype) != S_IFLNK) &&
6832ced83f9bSceastha 			    (G_p->g_attrnam_p == NULL)) {
68337c478bd9Sstevel@tonic-gate 				Thdr_p->tbuf.t_typeflag = LNKTYPE;
68347c478bd9Sstevel@tonic-gate 				(void) sprintf(Thdr_p->tbuf.t_size,
68357c478bd9Sstevel@tonic-gate 				    "%011lo", 0L);
68367c478bd9Sstevel@tonic-gate 			}
68377c478bd9Sstevel@tonic-gate 			(void) strncpy(Thdr_p->tbuf.t_linkname, T_lname,
68387c478bd9Sstevel@tonic-gate 			    strlen(T_lname));
68397c478bd9Sstevel@tonic-gate 		}
68407c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_magic, "%s", TMAGIC);
68417c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_version, "%2s", TVERSION);
68427c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_uname, "%s",  G_p->g_uname);
68437c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_gname, "%s", G_p->g_gname);
68447c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_devmajor, "%07o",
68457c478bd9Sstevel@tonic-gate 		    (int)major(G_p->g_rdev));
68467c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_devminor, "%07o",
68477c478bd9Sstevel@tonic-gate 		    (int)minor(G_p->g_rdev));
68487c478bd9Sstevel@tonic-gate 		if (Gen.g_prefix) {
68497c478bd9Sstevel@tonic-gate 			(void) sprintf(Thdr_p->tbuf.t_prefix, "%s",
68507c478bd9Sstevel@tonic-gate 			    Gen.g_prefix);
68517c478bd9Sstevel@tonic-gate 			free(Gen.g_prefix);
68527c478bd9Sstevel@tonic-gate 			Gen.g_prefix = NULL;
68537c478bd9Sstevel@tonic-gate 		} else {
68547c478bd9Sstevel@tonic-gate 			(void) sprintf(Thdr_p->tbuf.t_prefix, "%s", "");
68557c478bd9Sstevel@tonic-gate 		}
68567c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_cksum, "%07o",
68577c478bd9Sstevel@tonic-gate 		    (int)cksum(TARTYP, 0, NULL));
68587c478bd9Sstevel@tonic-gate 		break;
68597c478bd9Sstevel@tonic-gate 	case TAR:
68607c478bd9Sstevel@tonic-gate 		Thdr_p = (union tblock *)Buffr.b_in_p;
68617c478bd9Sstevel@tonic-gate 		(void) memcpy(Thdr_p, Empty, TARSZ);
68627c478bd9Sstevel@tonic-gate 		(void) strncpy(Thdr_p->tbuf.t_name, G_p->g_nam_p,
68637c478bd9Sstevel@tonic-gate 		    G_p->g_namesz);
68647c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mode, "%07o ", (int)mode);
68657c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_uid, "%07o ", (int)uid);
68667c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_gid, "%07o ", (int)gid);
68677c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_size, "%011llo ",
68687c478bd9Sstevel@tonic-gate 		    (offset_t)len);
68697c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mtime, "%011o ",
68707c478bd9Sstevel@tonic-gate 		    (int)G_p->g_mtime);
68717c478bd9Sstevel@tonic-gate 		if (T_lname[0] != '\0') {
68727c478bd9Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = '1';
68737c478bd9Sstevel@tonic-gate 		} else {
68747c478bd9Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = '\0';
68757c478bd9Sstevel@tonic-gate 		}
68767c478bd9Sstevel@tonic-gate 		(void) strncpy(Thdr_p->tbuf.t_linkname, T_lname,
68777c478bd9Sstevel@tonic-gate 		    strlen(T_lname));
68787c478bd9Sstevel@tonic-gate 		break;
68797c478bd9Sstevel@tonic-gate 	default:
68807c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
68817c478bd9Sstevel@tonic-gate 	} /* Hdr_type */
68827c478bd9Sstevel@tonic-gate 
68837c478bd9Sstevel@tonic-gate 	Buffr.b_in_p += cnt;
68847c478bd9Sstevel@tonic-gate 	Buffr.b_cnt += cnt;
68857c478bd9Sstevel@tonic-gate 	pad = ((cnt + Pad_val) & ~Pad_val) - cnt;
68867c478bd9Sstevel@tonic-gate 	if (pad != 0) {
68877c478bd9Sstevel@tonic-gate 		FLUSH(pad);
68887c478bd9Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, Empty, pad);
68897c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += pad;
68907c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += pad;
68917c478bd9Sstevel@tonic-gate 	}
68927c478bd9Sstevel@tonic-gate }
68937c478bd9Sstevel@tonic-gate 
68947c478bd9Sstevel@tonic-gate /*
68957c478bd9Sstevel@tonic-gate  * write_trail: Create the appropriate trailer for the selected header type
68967c478bd9Sstevel@tonic-gate  * and bwrite the trailer.  Pad the buffer with nulls out to the next Bufsize
68977c478bd9Sstevel@tonic-gate  * boundary, and force a write.  If the write completes, or if the trailer is
68987c478bd9Sstevel@tonic-gate  * completely written (but not all of the padding nulls (as can happen on end
68997c478bd9Sstevel@tonic-gate  * of medium)) return.  Otherwise, the trailer was not completely written out,
69007c478bd9Sstevel@tonic-gate  * so re-pad the buffer with nulls and try again.
69017c478bd9Sstevel@tonic-gate  */
69027c478bd9Sstevel@tonic-gate 
69037c478bd9Sstevel@tonic-gate static void
69047c478bd9Sstevel@tonic-gate write_trail(void)
69057c478bd9Sstevel@tonic-gate {
69067c478bd9Sstevel@tonic-gate 	int cnt, need;
69077c478bd9Sstevel@tonic-gate 
69087c478bd9Sstevel@tonic-gate 	switch (Hdr_type) {
69097c478bd9Sstevel@tonic-gate 	case BIN:
69107c478bd9Sstevel@tonic-gate 		Gen.g_magic = CMN_BIN;
69117c478bd9Sstevel@tonic-gate 		break;
69127c478bd9Sstevel@tonic-gate 	case CHR:
69137c478bd9Sstevel@tonic-gate 		Gen.g_magic = CMN_BIN;
69147c478bd9Sstevel@tonic-gate 		break;
69157c478bd9Sstevel@tonic-gate 	case ASC:
69167c478bd9Sstevel@tonic-gate 		Gen.g_magic = CMN_ASC;
69177c478bd9Sstevel@tonic-gate 		break;
69187c478bd9Sstevel@tonic-gate 	case CRC:
69197c478bd9Sstevel@tonic-gate 		Gen.g_magic = CMN_CRC;
69207c478bd9Sstevel@tonic-gate 		break;
69217c478bd9Sstevel@tonic-gate 	}
69227c478bd9Sstevel@tonic-gate 
69237c478bd9Sstevel@tonic-gate 	switch (Hdr_type) {
69247c478bd9Sstevel@tonic-gate 	case BIN:
69257c478bd9Sstevel@tonic-gate 	case CHR:
69267c478bd9Sstevel@tonic-gate 	case ASC:
69277c478bd9Sstevel@tonic-gate 	case CRC:
69287c478bd9Sstevel@tonic-gate 		Gen.g_mode = Gen.g_uid = Gen.g_gid = 0;
69297c478bd9Sstevel@tonic-gate 		Gen.g_nlink = 1;
69307c478bd9Sstevel@tonic-gate 		Gen.g_mtime = Gen.g_ino = Gen.g_dev = 0;
69317c478bd9Sstevel@tonic-gate 		Gen.g_rdev = Gen.g_cksum = 0;
69327c478bd9Sstevel@tonic-gate 		Gen.g_filesz = (off_t)0;
69337c478bd9Sstevel@tonic-gate 		Gen.g_namesz = strlen("TRAILER!!!") + 1;
69347c478bd9Sstevel@tonic-gate 		(void) strcpy(Gen.g_nam_p, "TRAILER!!!");
69357c478bd9Sstevel@tonic-gate 		G_p = &Gen;
69367c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
69377c478bd9Sstevel@tonic-gate 		break;
69387c478bd9Sstevel@tonic-gate 	case TAR:
69397c478bd9Sstevel@tonic-gate 	/*FALLTHROUGH*/
69407c478bd9Sstevel@tonic-gate 	case USTAR: /* TAR and USTAR */
69417c478bd9Sstevel@tonic-gate 		for (cnt = 0; cnt < 3; cnt++) {
69427c478bd9Sstevel@tonic-gate 			FLUSH(TARSZ);
69437c478bd9Sstevel@tonic-gate 			(void) memcpy(Buffr.b_in_p, Empty, TARSZ);
69447c478bd9Sstevel@tonic-gate 			Buffr.b_in_p += TARSZ;
69457c478bd9Sstevel@tonic-gate 			Buffr.b_cnt += TARSZ;
69467c478bd9Sstevel@tonic-gate 		}
69477c478bd9Sstevel@tonic-gate 		break;
69487c478bd9Sstevel@tonic-gate 	default:
69497c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
69507c478bd9Sstevel@tonic-gate 	}
69517c478bd9Sstevel@tonic-gate 	need = Bufsize - (Buffr.b_cnt % Bufsize);
69527c478bd9Sstevel@tonic-gate 	if (need == Bufsize)
69537c478bd9Sstevel@tonic-gate 		need = 0;
69547c478bd9Sstevel@tonic-gate 
69557c478bd9Sstevel@tonic-gate 	while (Buffr.b_cnt > 0) {
69567c478bd9Sstevel@tonic-gate 		while (need > 0) {
69577c478bd9Sstevel@tonic-gate 			cnt = (need < TARSZ) ? need : TARSZ;
69587c478bd9Sstevel@tonic-gate 			need -= cnt;
69597c478bd9Sstevel@tonic-gate 			FLUSH(cnt);
69607c478bd9Sstevel@tonic-gate 			(void) memcpy(Buffr.b_in_p, Empty, cnt);
69617c478bd9Sstevel@tonic-gate 			Buffr.b_in_p += cnt;
69627c478bd9Sstevel@tonic-gate 			Buffr.b_cnt += cnt;
69637c478bd9Sstevel@tonic-gate 		}
69647c478bd9Sstevel@tonic-gate 		bflush();
69657c478bd9Sstevel@tonic-gate 	}
69667c478bd9Sstevel@tonic-gate }
69677c478bd9Sstevel@tonic-gate 
69687c478bd9Sstevel@tonic-gate /*
69697c478bd9Sstevel@tonic-gate  * if archives in USTAR format, check if typeflag == '5' for directories
69707c478bd9Sstevel@tonic-gate  */
69717c478bd9Sstevel@tonic-gate static int
69727c478bd9Sstevel@tonic-gate ustar_dir(void)
69737c478bd9Sstevel@tonic-gate {
69747c478bd9Sstevel@tonic-gate 	if (Hdr_type == USTAR || Hdr_type == TAR) {
69757c478bd9Sstevel@tonic-gate 		if (Thdr_p->tbuf.t_typeflag == '5')
69767c478bd9Sstevel@tonic-gate 			return (1);
69777c478bd9Sstevel@tonic-gate 	}
69787c478bd9Sstevel@tonic-gate 	return (0);
69797c478bd9Sstevel@tonic-gate }
69807c478bd9Sstevel@tonic-gate 
69817c478bd9Sstevel@tonic-gate /*
69827c478bd9Sstevel@tonic-gate  * if archives in USTAR format, check if typeflag == '3' || '4' || '6'
69837c478bd9Sstevel@tonic-gate  * for character, block, fifo special files
69847c478bd9Sstevel@tonic-gate  */
69857c478bd9Sstevel@tonic-gate static int
69867c478bd9Sstevel@tonic-gate ustar_spec(void)
69877c478bd9Sstevel@tonic-gate {
69887c478bd9Sstevel@tonic-gate 	int typeflag;
69897c478bd9Sstevel@tonic-gate 
69907c478bd9Sstevel@tonic-gate 	if (Hdr_type == USTAR || Hdr_type == TAR) {
69917c478bd9Sstevel@tonic-gate 		typeflag = Thdr_p->tbuf.t_typeflag;
69927c478bd9Sstevel@tonic-gate 		if (typeflag == '3' || typeflag == '4' || typeflag == '6')
69937c478bd9Sstevel@tonic-gate 			return (1);
69947c478bd9Sstevel@tonic-gate 	}
69957c478bd9Sstevel@tonic-gate 	return (0);
69967c478bd9Sstevel@tonic-gate }
69977c478bd9Sstevel@tonic-gate 
69987c478bd9Sstevel@tonic-gate /*
69997c478bd9Sstevel@tonic-gate  * The return value is a pointer to a converted copy of the information in
70007c478bd9Sstevel@tonic-gate  * FromStat if the file is representable in -Hodc format, and NULL otherwise.
70017c478bd9Sstevel@tonic-gate  */
70027c478bd9Sstevel@tonic-gate 
70037c478bd9Sstevel@tonic-gate static struct stat *
70047c478bd9Sstevel@tonic-gate convert_to_old_stat(struct stat *FromStat, char *namep, char *attrp)
70057c478bd9Sstevel@tonic-gate {
70067c478bd9Sstevel@tonic-gate 	static struct stat ToSt;
70077c478bd9Sstevel@tonic-gate 	cpioinfo_t TmpSt;
70087c478bd9Sstevel@tonic-gate 
70097c478bd9Sstevel@tonic-gate 	(void) memset(&TmpSt, 0, sizeof (cpioinfo_t));
70107c478bd9Sstevel@tonic-gate 	stat_to_svr32_stat(&TmpSt, FromStat);
70117c478bd9Sstevel@tonic-gate 	(void) memset(&ToSt, 0, sizeof (ToSt));
70127c478bd9Sstevel@tonic-gate 
70137c478bd9Sstevel@tonic-gate 	if (TmpSt.st_rdev == (o_dev_t)NODEV &&
70147c478bd9Sstevel@tonic-gate 	    (((TmpSt.st_mode & Ftype) == S_IFCHR) ||
70157c478bd9Sstevel@tonic-gate 	    ((TmpSt.st_mode & Ftype) == S_IFBLK))) {
70167c478bd9Sstevel@tonic-gate 		/*
70177c478bd9Sstevel@tonic-gate 		 * Encountered a problem representing the rdev information.
70187c478bd9Sstevel@tonic-gate 		 * Don't archive it.
70197c478bd9Sstevel@tonic-gate 		 */
70207c478bd9Sstevel@tonic-gate 
70217c478bd9Sstevel@tonic-gate 		msg(ERR, "Error -Hodc format can't support expanded"
70227c478bd9Sstevel@tonic-gate 		    "types on %s%s%s",
70237c478bd9Sstevel@tonic-gate 		    namep,
70247c478bd9Sstevel@tonic-gate 		    (attrp == NULL) ? "" : gettext(" Attribute"),
70257c478bd9Sstevel@tonic-gate 		    (attrp == NULL) ? "" : attrp);
70267c478bd9Sstevel@tonic-gate 		return (NULL);
70277c478bd9Sstevel@tonic-gate 	}
70287c478bd9Sstevel@tonic-gate 
70297c478bd9Sstevel@tonic-gate 	if (TmpSt.st_dev == (o_dev_t)NODEV) {
70307c478bd9Sstevel@tonic-gate 		/*
70317c478bd9Sstevel@tonic-gate 		 * Having trouble representing the device/inode pair.  We can't
70327c478bd9Sstevel@tonic-gate 		 * track links in this case; break them all into separate
70337c478bd9Sstevel@tonic-gate 		 * files.
70347c478bd9Sstevel@tonic-gate 		 */
70357c478bd9Sstevel@tonic-gate 
70367c478bd9Sstevel@tonic-gate 		TmpSt.st_ino = 0;
70377c478bd9Sstevel@tonic-gate 
70387c478bd9Sstevel@tonic-gate 		if (((TmpSt.st_mode & Ftype) != S_IFDIR) &&
70397c478bd9Sstevel@tonic-gate 		    TmpSt.st_nlink > 1)
70407c478bd9Sstevel@tonic-gate 			msg(POST,
70417c478bd9Sstevel@tonic-gate 			    "Warning: file %s%s%s has large "
70427c478bd9Sstevel@tonic-gate 			    "device number - linked "
70437c478bd9Sstevel@tonic-gate 			    "files will be restored as "
70447c478bd9Sstevel@tonic-gate 			    "separate files",
70457c478bd9Sstevel@tonic-gate 			    namep,
70467c478bd9Sstevel@tonic-gate 			    (attrp == NULL) ? "" : gettext(" Attribute"),
70477c478bd9Sstevel@tonic-gate 			    (attrp == NULL) ? "" : attrp);
70487c478bd9Sstevel@tonic-gate 
70497c478bd9Sstevel@tonic-gate 		/* ensure no links */
70507c478bd9Sstevel@tonic-gate 
70517c478bd9Sstevel@tonic-gate 		TmpSt.st_nlink = 1;
70527c478bd9Sstevel@tonic-gate 	}
70537c478bd9Sstevel@tonic-gate 
70547c478bd9Sstevel@tonic-gate 	/* Start converting values */
70557c478bd9Sstevel@tonic-gate 
70567c478bd9Sstevel@tonic-gate 	if (TmpSt.st_dev < 0) {
70577c478bd9Sstevel@tonic-gate 		ToSt.st_dev = 0;
70587c478bd9Sstevel@tonic-gate 	} else {
70597c478bd9Sstevel@tonic-gate 		ToSt.st_dev = (dev_t)TmpSt.st_dev;
70607c478bd9Sstevel@tonic-gate 	}
70617c478bd9Sstevel@tonic-gate 
70627c478bd9Sstevel@tonic-gate 	/* -actual- not truncated uid */
70637c478bd9Sstevel@tonic-gate 
70647c478bd9Sstevel@tonic-gate 	ToSt.st_uid = TmpSt.st_uid;
70657c478bd9Sstevel@tonic-gate 
70667c478bd9Sstevel@tonic-gate 	/* -actual- not truncated gid */
70677c478bd9Sstevel@tonic-gate 
70687c478bd9Sstevel@tonic-gate 	ToSt.st_gid = TmpSt.st_gid;
70697c478bd9Sstevel@tonic-gate 	ToSt.st_ino = (ino_t)TmpSt.st_ino;
70707c478bd9Sstevel@tonic-gate 	ToSt.st_mode = (mode_t)TmpSt.st_mode;
70717c478bd9Sstevel@tonic-gate 	ToSt.st_mtime = (ulong_t)TmpSt.st_modtime;
70727c478bd9Sstevel@tonic-gate 	ToSt.st_nlink = (nlink_t)TmpSt.st_nlink;
70737c478bd9Sstevel@tonic-gate 	ToSt.st_size = (off_t)TmpSt.st_size;
70747c478bd9Sstevel@tonic-gate 	ToSt.st_rdev = (dev_t)TmpSt.st_rdev;
70757c478bd9Sstevel@tonic-gate 
70767c478bd9Sstevel@tonic-gate 	return (&ToSt);
70777c478bd9Sstevel@tonic-gate }
70787c478bd9Sstevel@tonic-gate 
70797c478bd9Sstevel@tonic-gate /*
70807c478bd9Sstevel@tonic-gate  * In the beginning of each bar archive, there is a header which describes the
70817c478bd9Sstevel@tonic-gate  * current volume being created, followed by a header which describes the
70827c478bd9Sstevel@tonic-gate  * current file being created, followed by the file itself.  If there is
70837c478bd9Sstevel@tonic-gate  * more than one file to be created, a separate header will be created for
70847c478bd9Sstevel@tonic-gate  * each additional file.  This structure may be repeated if the bar archive
70857c478bd9Sstevel@tonic-gate  * contains multiple volumes.  If a file spans across volumes, its header
70867c478bd9Sstevel@tonic-gate  * will not be repeated in the next volume.
70877c478bd9Sstevel@tonic-gate  *               +------------------+
70887c478bd9Sstevel@tonic-gate  *               |    vol header    |
70897c478bd9Sstevel@tonic-gate  *               |------------------|
70907c478bd9Sstevel@tonic-gate  *               |   file header i  |     i = 0
70917c478bd9Sstevel@tonic-gate  *               |------------------|
70927c478bd9Sstevel@tonic-gate  *               |     <file i>     |
70937c478bd9Sstevel@tonic-gate  *               |------------------|
70947c478bd9Sstevel@tonic-gate  *               |  file header i+1 |
70957c478bd9Sstevel@tonic-gate  *               |------------------|
70967c478bd9Sstevel@tonic-gate  *               |    <file i+1>    |
70977c478bd9Sstevel@tonic-gate  *               |------------------|
70987c478bd9Sstevel@tonic-gate  *               |        .         |
70997c478bd9Sstevel@tonic-gate  *               |        .         |
71007c478bd9Sstevel@tonic-gate  *               |        .         |
71017c478bd9Sstevel@tonic-gate  *               +------------------+
71027c478bd9Sstevel@tonic-gate  */
71037c478bd9Sstevel@tonic-gate 
71047c478bd9Sstevel@tonic-gate /*
71057c478bd9Sstevel@tonic-gate  * read in the header that describes the current volume of the bar archive
71067c478bd9Sstevel@tonic-gate  * to be extracted.
71077c478bd9Sstevel@tonic-gate  */
71087c478bd9Sstevel@tonic-gate static void
71097c478bd9Sstevel@tonic-gate read_bar_vol_hdr(void)
71107c478bd9Sstevel@tonic-gate {
71117c478bd9Sstevel@tonic-gate 	union b_block *tmp_hdr;
71127c478bd9Sstevel@tonic-gate 
71137c478bd9Sstevel@tonic-gate 	tmp_hdr = (union b_block *)Buffr.b_out_p;
71147c478bd9Sstevel@tonic-gate 	if (tmp_hdr->dbuf.bar_magic[0] == BAR_VOLUME_MAGIC) {
71157c478bd9Sstevel@tonic-gate 
71167c478bd9Sstevel@tonic-gate 		if (bar_Vhdr == NULL) {
71177c478bd9Sstevel@tonic-gate 			bar_Vhdr = e_zalloc(E_EXIT, TBLOCK);
71187c478bd9Sstevel@tonic-gate 		}
71197c478bd9Sstevel@tonic-gate 		(void) memcpy(&(bar_Vhdr->dbuf), &(tmp_hdr->dbuf), TBLOCK);
71207c478bd9Sstevel@tonic-gate 	} else {
71217c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
71227c478bd9Sstevel@tonic-gate 		    "bar error: cannot read volume header\n"));
71237c478bd9Sstevel@tonic-gate 		exit(1);
71247c478bd9Sstevel@tonic-gate 	}
71257c478bd9Sstevel@tonic-gate 
71267c478bd9Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.mode, "%8lo", &Gen_bar_vol.g_mode);
71277c478bd9Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.uid, "%8d", (int *)&Gen_bar_vol.g_uid);
71287c478bd9Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.gid, "%8d", (int *)&Gen_bar_vol.g_gid);
71297c478bd9Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.size, "%12llo",
71307c478bd9Sstevel@tonic-gate 	    (u_off_t *)&Gen_bar_vol.g_filesz);
71317c478bd9Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.mtime, "%12lo", &Gen_bar_vol.g_mtime);
71327c478bd9Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.chksum, "%8lo", &Gen_bar_vol.g_cksum);
71337c478bd9Sstevel@tonic-gate 
71347c478bd9Sstevel@tonic-gate 	/* set the compress flag */
71357c478bd9Sstevel@tonic-gate 	if (bar_Vhdr->dbuf.compressed == '1')
71367c478bd9Sstevel@tonic-gate 		Compressed = 1;
71377c478bd9Sstevel@tonic-gate 	else
71387c478bd9Sstevel@tonic-gate 		Compressed = 0;
71397c478bd9Sstevel@tonic-gate 
71407c478bd9Sstevel@tonic-gate 	Buffr.b_out_p += 512;
71417c478bd9Sstevel@tonic-gate 	Buffr.b_cnt -= 512;
71427c478bd9Sstevel@tonic-gate 
71437c478bd9Sstevel@tonic-gate 	/*
71447c478bd9Sstevel@tonic-gate 	 * not the first volume; exit
71457c478bd9Sstevel@tonic-gate 	 */
71467c478bd9Sstevel@tonic-gate 	if (strcmp(bar_Vhdr->dbuf.volume_num, "1") != 0) {
71477c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
71487c478bd9Sstevel@tonic-gate 		    gettext("error: This is not volume 1.  "));
71497c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("This is volume %s.  "),
71507c478bd9Sstevel@tonic-gate 		    bar_Vhdr->dbuf.volume_num);
71517c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Please insert volume 1.\n"));
71527c478bd9Sstevel@tonic-gate 		exit(1);
71537c478bd9Sstevel@tonic-gate 	}
71547c478bd9Sstevel@tonic-gate 
71557c478bd9Sstevel@tonic-gate 	read_bar_file_hdr();
71567c478bd9Sstevel@tonic-gate }
71577c478bd9Sstevel@tonic-gate 
71587c478bd9Sstevel@tonic-gate /*
71597c478bd9Sstevel@tonic-gate  * read in the header that describes the current file to be extracted
71607c478bd9Sstevel@tonic-gate  */
71617c478bd9Sstevel@tonic-gate static void
71627c478bd9Sstevel@tonic-gate read_bar_file_hdr(void)
71637c478bd9Sstevel@tonic-gate {
71647c478bd9Sstevel@tonic-gate 	union b_block *tmp_hdr;
71657c478bd9Sstevel@tonic-gate 	char *start_of_name, *name_p;
71667c478bd9Sstevel@tonic-gate 	char *tmp;
71677c478bd9Sstevel@tonic-gate 
71687c478bd9Sstevel@tonic-gate 	if (*Buffr.b_out_p == '\0') {
71697c478bd9Sstevel@tonic-gate 		*Gen.g_nam_p = '\0';
71707c478bd9Sstevel@tonic-gate 		exit(0);
71717c478bd9Sstevel@tonic-gate 	}
71727c478bd9Sstevel@tonic-gate 
71737c478bd9Sstevel@tonic-gate 	tmp_hdr = (union b_block *)Buffr.b_out_p;
71747c478bd9Sstevel@tonic-gate 
71757c478bd9Sstevel@tonic-gate 	tmp = &tmp_hdr->dbuf.mode[1];
71767c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp, "%8lo", &Gen.g_mode);
71777c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.uid, "%8lo", &Gen.g_uid);
71787c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.gid, "%8lo", &Gen.g_gid);
71797c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.size, "%12llo",
71807c478bd9Sstevel@tonic-gate 	    (u_off_t *)&Gen.g_filesz);
71817c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.mtime, "%12lo", &Gen.g_mtime);
71827c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.chksum, "%8lo", &Gen.g_cksum);
71837c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.rdev, "%8lo", &Gen.g_rdev);
71847c478bd9Sstevel@tonic-gate 
71857c478bd9Sstevel@tonic-gate #define	to_new_major(x)	(int)((unsigned)((x) & OMAXMAJ) << NBITSMINOR)
71867c478bd9Sstevel@tonic-gate #define	to_new_minor(x)	(int)((x) & OMAXMIN)
71877c478bd9Sstevel@tonic-gate 	Gen.g_rdev = to_new_major(Gen.g_rdev) | to_new_minor(Gen.g_rdev);
71887c478bd9Sstevel@tonic-gate 	bar_linkflag = tmp_hdr->dbuf.linkflag;
71897c478bd9Sstevel@tonic-gate 	start_of_name = &tmp_hdr->dbuf.start_of_name;
71907c478bd9Sstevel@tonic-gate 
71917c478bd9Sstevel@tonic-gate 
71927c478bd9Sstevel@tonic-gate 	name_p = Gen.g_nam_p;
71937c478bd9Sstevel@tonic-gate 	while (*name_p++ = *start_of_name++)
71947c478bd9Sstevel@tonic-gate 		;
71957c478bd9Sstevel@tonic-gate 	*name_p = '\0';
71967c478bd9Sstevel@tonic-gate 	if (bar_linkflag == LNKTYPE || bar_linkflag == SYMTYPE)
71977c478bd9Sstevel@tonic-gate 		(void) strcpy(bar_linkname, start_of_name);
71987c478bd9Sstevel@tonic-gate 
71997c478bd9Sstevel@tonic-gate 	Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
72007c478bd9Sstevel@tonic-gate 	(void) strcpy(nambuf, Gen.g_nam_p);
72017c478bd9Sstevel@tonic-gate }
72027c478bd9Sstevel@tonic-gate 
72037c478bd9Sstevel@tonic-gate /*
72047c478bd9Sstevel@tonic-gate  * if the bar archive is compressed, set up a pipe and do the de-compression
72057c478bd9Sstevel@tonic-gate  * as the compressed file is read in.
72067c478bd9Sstevel@tonic-gate  */
72077c478bd9Sstevel@tonic-gate static void
72087c478bd9Sstevel@tonic-gate setup_uncompress(FILE **pipef)
72097c478bd9Sstevel@tonic-gate {
72107c478bd9Sstevel@tonic-gate 	char *cmd_buf;
72117c478bd9Sstevel@tonic-gate 	size_t cmdlen;
72127c478bd9Sstevel@tonic-gate 
72137c478bd9Sstevel@tonic-gate 	cmd_buf = e_zalloc(E_EXIT, MAXPATHLEN * 2);
72147c478bd9Sstevel@tonic-gate 
72157c478bd9Sstevel@tonic-gate 	if (access(Gen.g_nam_p, W_OK) != 0) {
72167c478bd9Sstevel@tonic-gate 		cmdlen = snprintf(cmd_buf, MAXPATHLEN * 2,
72177c478bd9Sstevel@tonic-gate 		    "chmod +w '%s'; uncompress -c > '%s'; "
72187c478bd9Sstevel@tonic-gate 		    "chmod 0%o '%s'",
72197c478bd9Sstevel@tonic-gate 		    Gen.g_nam_p, Gen.g_nam_p, (int)G_p->g_mode, Gen.g_nam_p);
72207c478bd9Sstevel@tonic-gate 	} else {
72217c478bd9Sstevel@tonic-gate 		cmdlen = snprintf(cmd_buf, MAXPATHLEN * 2,
72227c478bd9Sstevel@tonic-gate 		    "uncompress -c > '%s'", Gen.g_nam_p);
72237c478bd9Sstevel@tonic-gate 	}
72247c478bd9Sstevel@tonic-gate 
72257c478bd9Sstevel@tonic-gate 	if (cmdlen >= MAXPATHLEN * 2 ||
72267c478bd9Sstevel@tonic-gate 	    (*pipef = popen(cmd_buf, "w")) == NULL) {
72277c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("error\n"));
72287c478bd9Sstevel@tonic-gate 		exit(1);
72297c478bd9Sstevel@tonic-gate 	}
72307c478bd9Sstevel@tonic-gate 
72317c478bd9Sstevel@tonic-gate 	if (close(Ofile) != 0)
72327c478bd9Sstevel@tonic-gate 		msg(EXTN, "close error");
72337c478bd9Sstevel@tonic-gate 	Ofile = fileno(*pipef);
72347c478bd9Sstevel@tonic-gate 
72357c478bd9Sstevel@tonic-gate 	free(cmd_buf);
72367c478bd9Sstevel@tonic-gate }
72377c478bd9Sstevel@tonic-gate 
72387c478bd9Sstevel@tonic-gate /*
72397c478bd9Sstevel@tonic-gate  * if the bar archive spans multiple volumes, read in the header that
72407c478bd9Sstevel@tonic-gate  * describes the next volume.
72417c478bd9Sstevel@tonic-gate  */
72427c478bd9Sstevel@tonic-gate static void
72437c478bd9Sstevel@tonic-gate skip_bar_volhdr(void)
72447c478bd9Sstevel@tonic-gate {
72457c478bd9Sstevel@tonic-gate 	char *buff;
72467c478bd9Sstevel@tonic-gate 	union b_block *tmp_hdr;
72477c478bd9Sstevel@tonic-gate 
72487c478bd9Sstevel@tonic-gate 	buff = e_zalloc(E_EXIT, (uint_t)Bufsize);
72497c478bd9Sstevel@tonic-gate 
72507c478bd9Sstevel@tonic-gate 	if (g_read(Device, Archive, buff, Bufsize) < 0) {
72517c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
72527c478bd9Sstevel@tonic-gate 		    "error in skip_bar_volhdr\n"));
72537c478bd9Sstevel@tonic-gate 	} else {
72547c478bd9Sstevel@tonic-gate 
72557c478bd9Sstevel@tonic-gate 		tmp_hdr = (union b_block *)buff;
72567c478bd9Sstevel@tonic-gate 		if (tmp_hdr->dbuf.bar_magic[0] == BAR_VOLUME_MAGIC) {
72577c478bd9Sstevel@tonic-gate 
72587c478bd9Sstevel@tonic-gate 			if (bar_Vhdr == NULL) {
72597c478bd9Sstevel@tonic-gate 				bar_Vhdr = e_zalloc(E_EXIT, TBLOCK);
72607c478bd9Sstevel@tonic-gate 			}
72617c478bd9Sstevel@tonic-gate 			(void) memcpy(&(bar_Vhdr->dbuf),
72627c478bd9Sstevel@tonic-gate 			    &(tmp_hdr->dbuf), TBLOCK);
72637c478bd9Sstevel@tonic-gate 		} else {
72647c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
72657c478bd9Sstevel@tonic-gate 			    gettext("cpio error: cannot read bar volume "
72667c478bd9Sstevel@tonic-gate 			    "header\n"));
72677c478bd9Sstevel@tonic-gate 			exit(1);
72687c478bd9Sstevel@tonic-gate 		}
72697c478bd9Sstevel@tonic-gate 
72707c478bd9Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.mode, "%8lo",
72717c478bd9Sstevel@tonic-gate 		    &Gen_bar_vol.g_mode);
72727c478bd9Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.uid, "%8lo",
72737c478bd9Sstevel@tonic-gate 		    &Gen_bar_vol.g_uid);
72747c478bd9Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.gid, "%8lo",
72757c478bd9Sstevel@tonic-gate 		    &Gen_bar_vol.g_gid);
72767c478bd9Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.size, "%12llo",
72777c478bd9Sstevel@tonic-gate 		    (u_off_t *)&Gen_bar_vol.g_filesz);
72787c478bd9Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.mtime, "%12lo",
72797c478bd9Sstevel@tonic-gate 		    &Gen_bar_vol.g_mtime);
72807c478bd9Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.chksum, "%8lo",
72817c478bd9Sstevel@tonic-gate 		    &Gen_bar_vol.g_cksum);
72827c478bd9Sstevel@tonic-gate 		if (bar_Vhdr->dbuf.compressed == '1')
72837c478bd9Sstevel@tonic-gate 			Compressed = 1;
72847c478bd9Sstevel@tonic-gate 		else
72857c478bd9Sstevel@tonic-gate 			Compressed = 0;
72867c478bd9Sstevel@tonic-gate 	}
72877c478bd9Sstevel@tonic-gate 
72887c478bd9Sstevel@tonic-gate 	/*
72897c478bd9Sstevel@tonic-gate 	 * Now put the rest of the bytes read in into the data buffer.
72907c478bd9Sstevel@tonic-gate 	 */
72917c478bd9Sstevel@tonic-gate 	(void) memcpy(Buffr.b_in_p, &buff[512], (Bufsize - 512));
72927c478bd9Sstevel@tonic-gate 	Buffr.b_in_p += (Bufsize - 512);
72937c478bd9Sstevel@tonic-gate 	Buffr.b_cnt += (long)(Bufsize - 512);
72947c478bd9Sstevel@tonic-gate 
72957c478bd9Sstevel@tonic-gate 	free(buff);
72967c478bd9Sstevel@tonic-gate }
72977c478bd9Sstevel@tonic-gate 
72987c478bd9Sstevel@tonic-gate /*
72997c478bd9Sstevel@tonic-gate  * check the linkflag which indicates the type of the file to be extracted,
73007c478bd9Sstevel@tonic-gate  * invoke the corresponding routine to extract the file.
73017c478bd9Sstevel@tonic-gate  */
73027c478bd9Sstevel@tonic-gate static void
73037c478bd9Sstevel@tonic-gate bar_file_in(void)
73047c478bd9Sstevel@tonic-gate {
73057c478bd9Sstevel@tonic-gate 	/*
73067c478bd9Sstevel@tonic-gate 	 * the file is a directory
73077c478bd9Sstevel@tonic-gate 	 */
73087c478bd9Sstevel@tonic-gate 	if (Adir) {
73097c478bd9Sstevel@tonic-gate 		if (ckname(1) != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
73107c478bd9Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
73117c478bd9Sstevel@tonic-gate 		}
73127c478bd9Sstevel@tonic-gate 		return;
73137c478bd9Sstevel@tonic-gate 	}
73147c478bd9Sstevel@tonic-gate 
73157c478bd9Sstevel@tonic-gate 	switch (bar_linkflag) {
73167c478bd9Sstevel@tonic-gate 	case REGTYPE:
73177c478bd9Sstevel@tonic-gate 		/* regular file */
73187c478bd9Sstevel@tonic-gate 		if ((ckname(1) == F_SKIP) ||
73197c478bd9Sstevel@tonic-gate 		    (Ofile = openout(G_p->g_dirfd)) < 0) {
73207c478bd9Sstevel@tonic-gate 			data_in(P_SKIP);
73217c478bd9Sstevel@tonic-gate 		} else {
73227c478bd9Sstevel@tonic-gate 			data_in(P_PROC);
73237c478bd9Sstevel@tonic-gate 		}
73247c478bd9Sstevel@tonic-gate 		break;
73257c478bd9Sstevel@tonic-gate 	case LNKTYPE:
73267c478bd9Sstevel@tonic-gate 		/* hard link */
73277c478bd9Sstevel@tonic-gate 		if (ckname(1) == F_SKIP) {
73287c478bd9Sstevel@tonic-gate 			break;
73297c478bd9Sstevel@tonic-gate 		}
73307c478bd9Sstevel@tonic-gate 		(void) creat_lnk(G_p->g_dirfd, bar_linkname, G_p->g_nam_p);
73317c478bd9Sstevel@tonic-gate 		break;
73327c478bd9Sstevel@tonic-gate 	case SYMTYPE:
73337c478bd9Sstevel@tonic-gate 		/* symbolic link */
73347c478bd9Sstevel@tonic-gate 		if ((ckname(1) == F_SKIP) ||
73357c478bd9Sstevel@tonic-gate 		    (Ofile = openout(G_p->g_dirfd)) < 0) {
73367c478bd9Sstevel@tonic-gate 			data_in(P_SKIP);
73377c478bd9Sstevel@tonic-gate 		} else {
73387c478bd9Sstevel@tonic-gate 			data_in(P_PROC);
73397c478bd9Sstevel@tonic-gate 		}
73407c478bd9Sstevel@tonic-gate 		break;
73417c478bd9Sstevel@tonic-gate 	case CHRTYPE:
73427c478bd9Sstevel@tonic-gate 		/* character device or FIFO */
73437c478bd9Sstevel@tonic-gate 		if (ckname(1) != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
73447c478bd9Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
73457c478bd9Sstevel@tonic-gate 		}
73467c478bd9Sstevel@tonic-gate 		break;
73477c478bd9Sstevel@tonic-gate 	default:
73487c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("error: unknown file type\n"));
73497c478bd9Sstevel@tonic-gate 		break;
73507c478bd9Sstevel@tonic-gate 	}
73517c478bd9Sstevel@tonic-gate }
73527c478bd9Sstevel@tonic-gate 
73537c478bd9Sstevel@tonic-gate 
73547c478bd9Sstevel@tonic-gate /*
73557c478bd9Sstevel@tonic-gate  * This originally came from libgenIO/g_init.c
73567c478bd9Sstevel@tonic-gate  * XXX	And it is very broken.
73577c478bd9Sstevel@tonic-gate  */
73587c478bd9Sstevel@tonic-gate 
73597c478bd9Sstevel@tonic-gate /* #include <sys/statvfs.h> */
73607c478bd9Sstevel@tonic-gate #include <ftw.h>
73617c478bd9Sstevel@tonic-gate /* #include <libgenIO.h> */
73627c478bd9Sstevel@tonic-gate #define	G_TM_TAPE	1	/* Tapemaster controller    */
73637c478bd9Sstevel@tonic-gate #define	G_XY_DISK	3	/* xy disks		*/
73647c478bd9Sstevel@tonic-gate #define	G_SD_DISK	7	/* scsi sd disk		*/
73657c478bd9Sstevel@tonic-gate #define	G_XT_TAPE	8	/* xt tapes		*/
73667c478bd9Sstevel@tonic-gate #define	G_SF_FLOPPY	9	/* sf floppy		*/
73677c478bd9Sstevel@tonic-gate #define	G_XD_DISK	10	/* xd disks		*/
73687c478bd9Sstevel@tonic-gate #define	G_ST_TAPE	11	/* scsi tape		*/
73697c478bd9Sstevel@tonic-gate #define	G_NS		12	/* noswap pseudo-dev	*/
73707c478bd9Sstevel@tonic-gate #define	G_RAM		13	/* ram pseudo-dev	*/
73717c478bd9Sstevel@tonic-gate #define	G_FT		14	/* tftp			*/
73727c478bd9Sstevel@tonic-gate #define	G_HD		15	/* 386 network disk	*/
73737c478bd9Sstevel@tonic-gate #define	G_FD		16	/* 386 AT disk		*/
73747c478bd9Sstevel@tonic-gate #define	G_FILE		28	/* file, not a device	*/
73757c478bd9Sstevel@tonic-gate #define	G_NO_DEV	29	/* device does not require special treatment */
73767c478bd9Sstevel@tonic-gate #define	G_DEV_MAX	30	/* last valid device type */
73777c478bd9Sstevel@tonic-gate 
73787c478bd9Sstevel@tonic-gate /*
73797c478bd9Sstevel@tonic-gate  * g_init: Determine the device being accessed, set the buffer size,
73807c478bd9Sstevel@tonic-gate  * and perform any device specific initialization. Since at this point
73817c478bd9Sstevel@tonic-gate  * Sun has no system call to read the configuration, the major numbers
73827c478bd9Sstevel@tonic-gate  * are assumed to be static and types are figured out as such. However,
73837c478bd9Sstevel@tonic-gate  * as a rough estimate, the buffer size for all types is set to 512
73847c478bd9Sstevel@tonic-gate  * as a default.
73857c478bd9Sstevel@tonic-gate  */
73867c478bd9Sstevel@tonic-gate 
73877c478bd9Sstevel@tonic-gate static int
73887c478bd9Sstevel@tonic-gate g_init(int *devtype, int *fdes)
73897c478bd9Sstevel@tonic-gate {
73907c478bd9Sstevel@tonic-gate 	int bufsize;
73917c478bd9Sstevel@tonic-gate 	struct stat st_buf;
73927c478bd9Sstevel@tonic-gate 	struct statvfs stfs_buf;
73937c478bd9Sstevel@tonic-gate 
73947c478bd9Sstevel@tonic-gate 	*devtype = G_NO_DEV;
73957c478bd9Sstevel@tonic-gate 	bufsize = -1;
73967c478bd9Sstevel@tonic-gate 	if (fstat(*fdes, &st_buf) == -1)
73977c478bd9Sstevel@tonic-gate 		return (-1);
73984bc0a2efScasper 	if (!S_ISCHR(st_buf.st_mode) && !S_ISBLK(st_buf.st_mode)) {
73994bc0a2efScasper 		if (S_ISFIFO(st_buf.st_mode)) {
74007c478bd9Sstevel@tonic-gate 			bufsize = 512;
74017c478bd9Sstevel@tonic-gate 		} else {
74027c478bd9Sstevel@tonic-gate 			/* find block size for this file system */
74037c478bd9Sstevel@tonic-gate 			*devtype = G_FILE;
74047c478bd9Sstevel@tonic-gate 			if (fstatvfs(*fdes, &stfs_buf) < 0) {
74057c478bd9Sstevel@tonic-gate 					bufsize = -1;
74067c478bd9Sstevel@tonic-gate 					errno = ENODEV;
74077c478bd9Sstevel@tonic-gate 			} else
74087c478bd9Sstevel@tonic-gate 				bufsize = stfs_buf.f_bsize;
74097c478bd9Sstevel@tonic-gate 		}
74107c478bd9Sstevel@tonic-gate 
74117c478bd9Sstevel@tonic-gate 		return (bufsize);
74127c478bd9Sstevel@tonic-gate 
74137c478bd9Sstevel@tonic-gate 	/*
74147c478bd9Sstevel@tonic-gate 	 * We'll have to add a remote attribute to stat but this
74157c478bd9Sstevel@tonic-gate 	 * should work for now.
74167c478bd9Sstevel@tonic-gate 	 */
74177c478bd9Sstevel@tonic-gate 	} else if (st_buf.st_dev & 0x8000)	/* if remote  rdev */
74187c478bd9Sstevel@tonic-gate 		return (512);
74197c478bd9Sstevel@tonic-gate 
74207c478bd9Sstevel@tonic-gate 	bufsize = 512;
74217c478bd9Sstevel@tonic-gate 
74227c478bd9Sstevel@tonic-gate 	if (Hdr_type == BAR) {
74237c478bd9Sstevel@tonic-gate 		if (is_tape(*fdes)) {
74247c478bd9Sstevel@tonic-gate 			bufsize = BAR_TAPE_SIZE;
74257c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "Archiving to tape");
74267c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, " blocking factor 126\n");
74277c478bd9Sstevel@tonic-gate 		} else if (is_floppy(*fdes)) {
74287c478bd9Sstevel@tonic-gate 			bufsize = BAR_FLOPPY_SIZE;
74297c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "Archiving to floppy");
74307c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, " blocking factor 18\n");
74317c478bd9Sstevel@tonic-gate 		}
74327c478bd9Sstevel@tonic-gate 	}
74337c478bd9Sstevel@tonic-gate 
74347c478bd9Sstevel@tonic-gate 	return (bufsize);
74357c478bd9Sstevel@tonic-gate }
74367c478bd9Sstevel@tonic-gate 
74377c478bd9Sstevel@tonic-gate /*
74387c478bd9Sstevel@tonic-gate  * This originally came from libgenIO/g_read.c
74397c478bd9Sstevel@tonic-gate  */
74407c478bd9Sstevel@tonic-gate 
74417c478bd9Sstevel@tonic-gate /*
74427c478bd9Sstevel@tonic-gate  * g_read: Read nbytes of data from fdes (of type devtype) and place
74437c478bd9Sstevel@tonic-gate  * data in location pointed to by buf.  In case of end of medium,
74447c478bd9Sstevel@tonic-gate  * translate (where necessary) device specific EOM indications into
74457c478bd9Sstevel@tonic-gate  * the generic EOM indication of rv = -1, errno = ENOSPC.
74467c478bd9Sstevel@tonic-gate  */
74477c478bd9Sstevel@tonic-gate 
74487c478bd9Sstevel@tonic-gate static int
74497c478bd9Sstevel@tonic-gate g_read(int devtype, int fdes, char *buf, unsigned nbytes)
74507c478bd9Sstevel@tonic-gate {
74517c478bd9Sstevel@tonic-gate 	int rv;
74527c478bd9Sstevel@tonic-gate 
74537c478bd9Sstevel@tonic-gate 	if (devtype < 0 || devtype >= G_DEV_MAX) {
74547c478bd9Sstevel@tonic-gate 		errno = ENODEV;
74557c478bd9Sstevel@tonic-gate 		return (-1);
74567c478bd9Sstevel@tonic-gate 	}
74577c478bd9Sstevel@tonic-gate 
74587c478bd9Sstevel@tonic-gate 	rv = read(fdes, buf, nbytes);
74597c478bd9Sstevel@tonic-gate 
74607c478bd9Sstevel@tonic-gate 	/* st devices return 0 when no space left */
74617c478bd9Sstevel@tonic-gate 	if ((rv == 0 && errno == 0 && Hdr_type != BAR) ||
74627c478bd9Sstevel@tonic-gate 	    (rv == -1 && errno == EIO)) {
74637c478bd9Sstevel@tonic-gate 		errno = 0;
74647c478bd9Sstevel@tonic-gate 		rv = 0;
74657c478bd9Sstevel@tonic-gate 	}
74667c478bd9Sstevel@tonic-gate 
74677c478bd9Sstevel@tonic-gate 	return (rv);
74687c478bd9Sstevel@tonic-gate }
74697c478bd9Sstevel@tonic-gate 
74707c478bd9Sstevel@tonic-gate /*
74717c478bd9Sstevel@tonic-gate  * This originally came from libgenIO/g_write.c
74727c478bd9Sstevel@tonic-gate  */
74737c478bd9Sstevel@tonic-gate 
74747c478bd9Sstevel@tonic-gate /*
74757c478bd9Sstevel@tonic-gate  * g_write: Write nbytes of data to fdes (of type devtype) from
74767c478bd9Sstevel@tonic-gate  * the location pointed to by buf.  In case of end of medium,
74777c478bd9Sstevel@tonic-gate  * translate (where necessary) device specific EOM indications into
74787c478bd9Sstevel@tonic-gate  * the generic EOM indication of rv = -1, errno = ENOSPC.
74797c478bd9Sstevel@tonic-gate  */
74807c478bd9Sstevel@tonic-gate 
74817c478bd9Sstevel@tonic-gate static int
74827c478bd9Sstevel@tonic-gate g_write(int devtype, int fdes, char *buf, unsigned nbytes)
74837c478bd9Sstevel@tonic-gate {
74847c478bd9Sstevel@tonic-gate 	int rv;
74857c478bd9Sstevel@tonic-gate 
74867c478bd9Sstevel@tonic-gate 	if (devtype < 0 || devtype >= G_DEV_MAX) {
74877c478bd9Sstevel@tonic-gate 		errno = ENODEV;
74887c478bd9Sstevel@tonic-gate 		return (-1);
74897c478bd9Sstevel@tonic-gate 	}
74907c478bd9Sstevel@tonic-gate 
74917c478bd9Sstevel@tonic-gate 	rv = write(fdes, buf, nbytes);
74927c478bd9Sstevel@tonic-gate 
74937c478bd9Sstevel@tonic-gate 	/* st devices return 0 when no more space left */
74947c478bd9Sstevel@tonic-gate 	if ((rv == 0 && errno == 0) || (rv == -1 && errno == EIO)) {
74957c478bd9Sstevel@tonic-gate 		errno = ENOSPC;
74967c478bd9Sstevel@tonic-gate 		rv = -1;
74977c478bd9Sstevel@tonic-gate 	}
74987c478bd9Sstevel@tonic-gate 
74997c478bd9Sstevel@tonic-gate 	return (rv);
75007c478bd9Sstevel@tonic-gate }
75017c478bd9Sstevel@tonic-gate 
75027c478bd9Sstevel@tonic-gate /*
75037c478bd9Sstevel@tonic-gate  * Test for tape
75047c478bd9Sstevel@tonic-gate  */
75057c478bd9Sstevel@tonic-gate 
75067c478bd9Sstevel@tonic-gate static int
75077c478bd9Sstevel@tonic-gate is_tape(int fd)
75087c478bd9Sstevel@tonic-gate {
75097c478bd9Sstevel@tonic-gate 	struct mtget stuff;
75107c478bd9Sstevel@tonic-gate 
75117c478bd9Sstevel@tonic-gate 	/*
75127c478bd9Sstevel@tonic-gate 	 * try to do a generic tape ioctl, just to see if
75137c478bd9Sstevel@tonic-gate 	 * the thing is in fact a tape drive(er).
75147c478bd9Sstevel@tonic-gate 	 */
75157c478bd9Sstevel@tonic-gate 	if (ioctl(fd, MTIOCGET, &stuff) != -1) {
75167c478bd9Sstevel@tonic-gate 		/* the ioctl succeeded, must have been a tape */
75177c478bd9Sstevel@tonic-gate 		return (1);
75187c478bd9Sstevel@tonic-gate 	}
75197c478bd9Sstevel@tonic-gate 	return (0);
75207c478bd9Sstevel@tonic-gate }
75217c478bd9Sstevel@tonic-gate 
75227c478bd9Sstevel@tonic-gate /*
75237c478bd9Sstevel@tonic-gate  * Test for floppy
75247c478bd9Sstevel@tonic-gate  */
75257c478bd9Sstevel@tonic-gate 
75267c478bd9Sstevel@tonic-gate static int
75277c478bd9Sstevel@tonic-gate is_floppy(int fd)
75287c478bd9Sstevel@tonic-gate {
75297c478bd9Sstevel@tonic-gate 	struct fd_char stuff;
75307c478bd9Sstevel@tonic-gate 
75317c478bd9Sstevel@tonic-gate 	/*
75327c478bd9Sstevel@tonic-gate 	 * try to get the floppy drive characteristics, just to see if
75337c478bd9Sstevel@tonic-gate 	 * the thing is in fact a floppy drive(er).
75347c478bd9Sstevel@tonic-gate 	 */
75357c478bd9Sstevel@tonic-gate 	if (ioctl(fd, FDIOGCHAR, &stuff) != -1) {
75367c478bd9Sstevel@tonic-gate 		/* the ioctl succeeded, must have been a floppy */
75377c478bd9Sstevel@tonic-gate 		return (1);
75387c478bd9Sstevel@tonic-gate 	}
75397c478bd9Sstevel@tonic-gate 
75407c478bd9Sstevel@tonic-gate 	return (0);
75417c478bd9Sstevel@tonic-gate }
75427c478bd9Sstevel@tonic-gate 
75437c478bd9Sstevel@tonic-gate /*
75447c478bd9Sstevel@tonic-gate  * New functions for ACLs and other security attributes
75457c478bd9Sstevel@tonic-gate  */
75467c478bd9Sstevel@tonic-gate 
75477c478bd9Sstevel@tonic-gate /*
75487c478bd9Sstevel@tonic-gate  * The function appends the new security attribute info to the end of
75497c478bd9Sstevel@tonic-gate  * existing secinfo.
75507c478bd9Sstevel@tonic-gate  */
75517c478bd9Sstevel@tonic-gate static int
75527c478bd9Sstevel@tonic-gate append_secattr(
75537c478bd9Sstevel@tonic-gate 	char		**secinfo,	/* existing security info */
75547c478bd9Sstevel@tonic-gate 	int		*secinfo_len,	/* length of existing security info */
7555fa9e4066Sahrens 	acl_t		*aclp) 	/* new attribute data pointer */
75567c478bd9Sstevel@tonic-gate {
75577c478bd9Sstevel@tonic-gate 	char	*new_secinfo;
75587c478bd9Sstevel@tonic-gate 	char	*attrtext;
75597c478bd9Sstevel@tonic-gate 	size_t	newattrsize;
75607c478bd9Sstevel@tonic-gate 	int	oldsize;
75617c478bd9Sstevel@tonic-gate 
75627c478bd9Sstevel@tonic-gate 	/* no need to add */
7563fa9e4066Sahrens 	if (aclp == NULL) {
75647c478bd9Sstevel@tonic-gate 		return (0);
75657c478bd9Sstevel@tonic-gate 	}
75667c478bd9Sstevel@tonic-gate 
7567fa9e4066Sahrens 	switch (acl_type(aclp)) {
7568fa9e4066Sahrens 	case ACLENT_T:
7569fa9e4066Sahrens 	case ACE_T:
75707c478bd9Sstevel@tonic-gate 		/* LINTED alignment */
75715a5eeccaSmarks 		attrtext = acl_totext(aclp, ACL_APPEND_ID | ACL_COMPACT_FMT);
75727c478bd9Sstevel@tonic-gate 		if (attrtext == NULL) {
75737c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "acltotext failed\n");
75747c478bd9Sstevel@tonic-gate 			return (-1);
75757c478bd9Sstevel@tonic-gate 		}
75767c478bd9Sstevel@tonic-gate 		/* header: type + size = 8 */
75777c478bd9Sstevel@tonic-gate 		newattrsize = 8 + strlen(attrtext) + 1;
75787c478bd9Sstevel@tonic-gate 		attr = e_zalloc(E_NORMAL, newattrsize);
75797c478bd9Sstevel@tonic-gate 		if (attr == NULL) {
75807c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "can't allocate memory\n");
75817c478bd9Sstevel@tonic-gate 			return (-1);
75827c478bd9Sstevel@tonic-gate 		}
7583fa9e4066Sahrens 		attr->attr_type = (acl_type(aclp) == ACLENT_T) ?
7584fa9e4066Sahrens 		    UFSD_ACL : ACE_ACL;
75857c478bd9Sstevel@tonic-gate 		/* acl entry count */
7586fa9e4066Sahrens 		(void) sprintf(attr->attr_len, "%06o", acl_cnt(aclp));
75877c478bd9Sstevel@tonic-gate 		(void) strcpy((char *)&attr->attr_info[0], attrtext);
75887c478bd9Sstevel@tonic-gate 		free(attrtext);
75897c478bd9Sstevel@tonic-gate 		break;
75907c478bd9Sstevel@tonic-gate 
75917c478bd9Sstevel@tonic-gate 		/* SunFed's case goes here */
75927c478bd9Sstevel@tonic-gate 
75937c478bd9Sstevel@tonic-gate 	default:
75947c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "unrecognized attribute type\n");
75957c478bd9Sstevel@tonic-gate 		return (-1);
75967c478bd9Sstevel@tonic-gate 	}
75977c478bd9Sstevel@tonic-gate 
75987c478bd9Sstevel@tonic-gate 	/* old security info + new attr header(8) + new attr */
75997c478bd9Sstevel@tonic-gate 	oldsize = *secinfo_len;
76007c478bd9Sstevel@tonic-gate 	*secinfo_len += newattrsize;
76017c478bd9Sstevel@tonic-gate 	new_secinfo = e_zalloc(E_NORMAL, (uint_t)*secinfo_len);
76027c478bd9Sstevel@tonic-gate 	if (new_secinfo == NULL) {
76037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "can't allocate memory\n");
76047c478bd9Sstevel@tonic-gate 		*secinfo_len -= newattrsize;
76057c478bd9Sstevel@tonic-gate 		return (-1);
76067c478bd9Sstevel@tonic-gate 	}
76077c478bd9Sstevel@tonic-gate 
76087c478bd9Sstevel@tonic-gate 	(void) memcpy(new_secinfo, *secinfo, oldsize);
76097c478bd9Sstevel@tonic-gate 	(void) memcpy(new_secinfo + oldsize, attr, newattrsize);
76107c478bd9Sstevel@tonic-gate 
76117c478bd9Sstevel@tonic-gate 	free(*secinfo);
76127c478bd9Sstevel@tonic-gate 	*secinfo = new_secinfo;
76137c478bd9Sstevel@tonic-gate 	return (0);
76147c478bd9Sstevel@tonic-gate }
76157c478bd9Sstevel@tonic-gate 
76167c478bd9Sstevel@tonic-gate static void
76177c478bd9Sstevel@tonic-gate write_ancillary(char *secinfo, int len)
76187c478bd9Sstevel@tonic-gate {
76197c478bd9Sstevel@tonic-gate 	long    pad;
76207c478bd9Sstevel@tonic-gate 	long    cnt;
76217c478bd9Sstevel@tonic-gate 
76227c478bd9Sstevel@tonic-gate 	/* Just tranditional permissions or no security attribute info */
76237c478bd9Sstevel@tonic-gate 	if (len == 0) {
76247c478bd9Sstevel@tonic-gate 		return;
76257c478bd9Sstevel@tonic-gate 	}
76267c478bd9Sstevel@tonic-gate 
76277c478bd9Sstevel@tonic-gate 	/* write out security info */
76287c478bd9Sstevel@tonic-gate 	while (len > 0) {
76297c478bd9Sstevel@tonic-gate 		cnt = (unsigned)(len > CPIOBSZ) ? CPIOBSZ : len;
76307c478bd9Sstevel@tonic-gate 		FLUSH(cnt);
76317c478bd9Sstevel@tonic-gate 		errno = 0;
76327c478bd9Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, secinfo, (unsigned)cnt);
76337c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += cnt;
76347c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += (long)cnt;
76357c478bd9Sstevel@tonic-gate 		len -= (long)cnt;
76367c478bd9Sstevel@tonic-gate 	}
76377c478bd9Sstevel@tonic-gate 	pad = (Pad_val + 1 - (cnt & Pad_val)) & Pad_val;
76387c478bd9Sstevel@tonic-gate 	if (pad != 0) {
76397c478bd9Sstevel@tonic-gate 		FLUSH(pad);
76407c478bd9Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, Empty, pad);
76417c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += pad;
76427c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += pad;
76437c478bd9Sstevel@tonic-gate 	}
76447c478bd9Sstevel@tonic-gate }
76457c478bd9Sstevel@tonic-gate 
76467c478bd9Sstevel@tonic-gate static int
76477c478bd9Sstevel@tonic-gate remove_dir(char *path)
76487c478bd9Sstevel@tonic-gate {
76497c478bd9Sstevel@tonic-gate 	DIR		*name;
76507c478bd9Sstevel@tonic-gate 	struct dirent	*direct;
76517c478bd9Sstevel@tonic-gate 	struct stat	sbuf;
76527c478bd9Sstevel@tonic-gate 	char		*path_copy;
76537c478bd9Sstevel@tonic-gate 
76547c478bd9Sstevel@tonic-gate #define	MSG1	"remove_dir() failed to stat(\"%s\") "
76557c478bd9Sstevel@tonic-gate #define	MSG2	"remove_dir() failed to remove_dir(\"%s\") "
76567c478bd9Sstevel@tonic-gate #define	MSG3	"remove_dir() failed to unlink(\"%s\") "
76577c478bd9Sstevel@tonic-gate 
76587c478bd9Sstevel@tonic-gate 	/*
76597c478bd9Sstevel@tonic-gate 	 * Open the directory for reading.
76607c478bd9Sstevel@tonic-gate 	 */
76617c478bd9Sstevel@tonic-gate 	if ((name = opendir(path)) == NULL) {
76627c478bd9Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to opendir(\"%s\") ", path);
76637c478bd9Sstevel@tonic-gate 		return (-1);
76647c478bd9Sstevel@tonic-gate 	}
76657c478bd9Sstevel@tonic-gate 
76667c478bd9Sstevel@tonic-gate 	if (chdir(path) == -1) {
76677c478bd9Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to chdir(\"%s\") ", path);
76687c478bd9Sstevel@tonic-gate 		return (-1);
76697c478bd9Sstevel@tonic-gate 	}
76707c478bd9Sstevel@tonic-gate 
76717c478bd9Sstevel@tonic-gate 	/*
76727c478bd9Sstevel@tonic-gate 	 * Read every directory entry.
76737c478bd9Sstevel@tonic-gate 	 */
76747c478bd9Sstevel@tonic-gate 	while ((direct = readdir(name)) != NULL) {
76757c478bd9Sstevel@tonic-gate 		/*
76767c478bd9Sstevel@tonic-gate 		 * Ignore "." and ".." entries.
76777c478bd9Sstevel@tonic-gate 		 */
76787c478bd9Sstevel@tonic-gate 		if (strcmp(direct->d_name, ".") == 0 ||
76797c478bd9Sstevel@tonic-gate 		    strcmp(direct->d_name, "..") == 0)
76807c478bd9Sstevel@tonic-gate 			continue;
76817c478bd9Sstevel@tonic-gate 
76827c478bd9Sstevel@tonic-gate 			if (lstat(direct->d_name, &sbuf) == -1) {
76837c478bd9Sstevel@tonic-gate 				msg(ERRN, MSG1, direct->d_name);
76847c478bd9Sstevel@tonic-gate 				(void) closedir(name);
76857c478bd9Sstevel@tonic-gate 			return (-1);
76867c478bd9Sstevel@tonic-gate 		}
76877c478bd9Sstevel@tonic-gate 
76887c478bd9Sstevel@tonic-gate 		if (S_ISDIR(sbuf.st_mode)) {
76897c478bd9Sstevel@tonic-gate 			if (remove_dir(direct->d_name) == -1) {
76907c478bd9Sstevel@tonic-gate 				msg(ERRN, MSG2, direct->d_name);
76917c478bd9Sstevel@tonic-gate 				(void) closedir(name);
76927c478bd9Sstevel@tonic-gate 				return (-1);
76937c478bd9Sstevel@tonic-gate 			}
76947c478bd9Sstevel@tonic-gate 		} else {
76957c478bd9Sstevel@tonic-gate 			if (unlink(direct->d_name) == -1) {
76967c478bd9Sstevel@tonic-gate 				msg(ERRN, MSG3, direct->d_name);
76977c478bd9Sstevel@tonic-gate 				(void) closedir(name);
76987c478bd9Sstevel@tonic-gate 				return (-1);
76997c478bd9Sstevel@tonic-gate 			}
77007c478bd9Sstevel@tonic-gate 		}
77017c478bd9Sstevel@tonic-gate 
77027c478bd9Sstevel@tonic-gate 	}
77037c478bd9Sstevel@tonic-gate 
77047c478bd9Sstevel@tonic-gate 	/*
77057c478bd9Sstevel@tonic-gate 	 * Close the directory we just finished reading.
77067c478bd9Sstevel@tonic-gate 	 */
77077c478bd9Sstevel@tonic-gate 	(void) closedir(name);
77087c478bd9Sstevel@tonic-gate 
77097c478bd9Sstevel@tonic-gate 	/*
77107c478bd9Sstevel@tonic-gate 	 * Change directory to the parent directory...
77117c478bd9Sstevel@tonic-gate 	 */
77127c478bd9Sstevel@tonic-gate 	if (chdir("..") == -1) {
77137c478bd9Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to chdir(\"..\") ");
77147c478bd9Sstevel@tonic-gate 		return (-1);
77157c478bd9Sstevel@tonic-gate 	}
77167c478bd9Sstevel@tonic-gate 
77177c478bd9Sstevel@tonic-gate 	/*
77187c478bd9Sstevel@tonic-gate 	 * ...and finally remove the directory; note we have to
77197c478bd9Sstevel@tonic-gate 	 * make a copy since basename is free to modify its input.
77207c478bd9Sstevel@tonic-gate 	 */
77217c478bd9Sstevel@tonic-gate 	path_copy = e_strdup(E_NORMAL, path);
77227c478bd9Sstevel@tonic-gate 	if (path_copy == NULL) {
77237c478bd9Sstevel@tonic-gate 		msg(ERRN, "cannot strdup() the directory pathname ");
77247c478bd9Sstevel@tonic-gate 		return (-1);
77257c478bd9Sstevel@tonic-gate 	}
77267c478bd9Sstevel@tonic-gate 
77277c478bd9Sstevel@tonic-gate 	if (rmdir(basename(path_copy)) == -1) {
77287c478bd9Sstevel@tonic-gate 		free(path_copy);
77297c478bd9Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to rmdir(\"%s\") ", path);
77307c478bd9Sstevel@tonic-gate 		return (-1);
77317c478bd9Sstevel@tonic-gate 	}
77327c478bd9Sstevel@tonic-gate 
77337c478bd9Sstevel@tonic-gate 	free(path_copy);
77347c478bd9Sstevel@tonic-gate 	return (0);
77357c478bd9Sstevel@tonic-gate 
77367c478bd9Sstevel@tonic-gate }
77377c478bd9Sstevel@tonic-gate 
77387c478bd9Sstevel@tonic-gate static int
77397c478bd9Sstevel@tonic-gate save_cwd(void)
77407c478bd9Sstevel@tonic-gate {
77417c478bd9Sstevel@tonic-gate 	return (open(".", O_RDONLY));
77427c478bd9Sstevel@tonic-gate }
77437c478bd9Sstevel@tonic-gate 
77447c478bd9Sstevel@tonic-gate static void
77457c478bd9Sstevel@tonic-gate rest_cwd(int cwd)
77467c478bd9Sstevel@tonic-gate {
77477c478bd9Sstevel@tonic-gate 	(void) fchdir(cwd);
77487c478bd9Sstevel@tonic-gate 	(void) close(cwd);
77497c478bd9Sstevel@tonic-gate }
77507c478bd9Sstevel@tonic-gate 
77517c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
77527c478bd9Sstevel@tonic-gate static void
77537c478bd9Sstevel@tonic-gate xattrs_out(int (*func)())
77547c478bd9Sstevel@tonic-gate {
77557c478bd9Sstevel@tonic-gate 	int dirpfd;
77567c478bd9Sstevel@tonic-gate 	int filefd;
7757ced83f9bSceastha 	int arc_rwsysattr = 0;
7758ced83f9bSceastha 	int rw_sysattr = 0;
7759ced83f9bSceastha 	int ext_attr = 0;
77607c478bd9Sstevel@tonic-gate 	DIR *dirp;
77617c478bd9Sstevel@tonic-gate 	struct dirent *dp;
77627c478bd9Sstevel@tonic-gate 	int slen;
7763ced83f9bSceastha 	int plen;
77647c478bd9Sstevel@tonic-gate 	char *namep, *savenamep;
7765ced83f9bSceastha 	char *apathp;
7766ced83f9bSceastha 	char *attrparent = Gen.g_attrparent_p;
7767ced83f9bSceastha 	char *filename;
77687c478bd9Sstevel@tonic-gate 
7769ced83f9bSceastha 	if (attrparent == NULL) {
7770ced83f9bSceastha 		filename = Gen.g_nam_p;
7771ced83f9bSceastha 	} else {
7772ced83f9bSceastha 		filename = Gen.g_attrnam_p;
7773ced83f9bSceastha 	}
7774ced83f9bSceastha 
7775ced83f9bSceastha 	/*
7776ced83f9bSceastha 	 * If the underlying file system supports it, then
7777ced83f9bSceastha 	 * archive the extended attributes if -@ was specified,
7778ced83f9bSceastha 	 * and the extended system attributes if -/ was
7779ced83f9bSceastha 	 * specified.
7780ced83f9bSceastha 	 */
7781ced83f9bSceastha 	if (verify_attr_support(filename, (attrparent == NULL), ARC_CREATE,
7782ced83f9bSceastha 	    &ext_attr) != ATTR_OK) {
77837c478bd9Sstevel@tonic-gate 		return;
77847c478bd9Sstevel@tonic-gate 	}
77857c478bd9Sstevel@tonic-gate 
7786ced83f9bSceastha #if defined(_PC_SATTR_ENABLED)
7787ced83f9bSceastha 	if (SysAtflag) {
7788ced83f9bSceastha 		int		filefd;
7789ced83f9bSceastha 		nvlist_t 	*slist = NULL;
7790ced83f9bSceastha 
7791ced83f9bSceastha 		/*
7792ced83f9bSceastha 		 * Determine if there are non-transient system
7793ced83f9bSceastha 		 * attributes.
7794ced83f9bSceastha 		 */
7795ced83f9bSceastha 		errno = 0;
7796ced83f9bSceastha 		if ((filefd = open(filename, O_RDONLY)) == -1) {
7797ced83f9bSceastha 			if (attrparent == NULL) {
7798ced83f9bSceastha 				msg(EXTN,
7799ced83f9bSceastha 				    "unable to open %s%s%sfile %s",
7800ced83f9bSceastha 				    (attrparent == NULL) ? "" :
7801ced83f9bSceastha 				    gettext("attribute "),
7802ced83f9bSceastha 				    (attrparent == NULL) ? "" : attrparent,
7803ced83f9bSceastha 				    (attrparent == NULL) ? "" : gettext(" of "),
7804ced83f9bSceastha 				    (attrparent == NULL) ? G_p->g_nam_p :
7805ced83f9bSceastha 				    G_p->g_attrfnam_p);
7806ced83f9bSceastha 			}
7807ced83f9bSceastha 		}
7808ced83f9bSceastha 		if (((slist = sysattr_list(myname, filefd,
7809ced83f9bSceastha 		    filename)) != NULL) || (errno != 0)) {
7810ced83f9bSceastha 			arc_rwsysattr = 1;
7811ced83f9bSceastha 		}
7812ced83f9bSceastha 		if (slist != NULL) {
7813ced83f9bSceastha 			(void) nvlist_free(slist);
7814ced83f9bSceastha 			slist = NULL;
7815ced83f9bSceastha 		}
7816ced83f9bSceastha 		(void) close(filefd);
7817ced83f9bSceastha 	}
7818ced83f9bSceastha 
7819ced83f9bSceastha 	/*
7820ced83f9bSceastha 	 * If we aren't archiving extended system attributes, and we are
7821ced83f9bSceastha 	 * processing an attribute, or if we are archiving extended system
7822ced83f9bSceastha 	 * attributes, and there are are no extended attributes, then there's
7823ced83f9bSceastha 	 * no need to open up the attribute directory of the file unless the
7824ced83f9bSceastha 	 * extended system attributes are not transient (i.e, the system
7825ced83f9bSceastha 	 * attributes are not the default values).
7826ced83f9bSceastha 	 */
7827ced83f9bSceastha 	if ((arc_rwsysattr == 0) && ((attrparent != NULL) ||
7828ced83f9bSceastha 	    (SysAtflag && !ext_attr))) {
7829ced83f9bSceastha 		return;
7830ced83f9bSceastha 	}
7831ced83f9bSceastha 
7832ced83f9bSceastha #endif	/* _PC_SATTR_ENABLED */
7833ced83f9bSceastha 
78347c478bd9Sstevel@tonic-gate 	/*
78357c478bd9Sstevel@tonic-gate 	 * If aclp still exists then free it since it is was set when base
78367c478bd9Sstevel@tonic-gate 	 * file was extracted.
78377c478bd9Sstevel@tonic-gate 	 */
7838fa9e4066Sahrens 	if (aclp != NULL) {
7839fa9e4066Sahrens 		acl_free(aclp);
78407c478bd9Sstevel@tonic-gate 		aclp = NULL;
7841fa9e4066Sahrens 		acl_is_set = 0;
78427c478bd9Sstevel@tonic-gate 	}
78437c478bd9Sstevel@tonic-gate 
7844ced83f9bSceastha 	Gen.g_dirfd = attropen(filename, ".", O_RDONLY);
78457c478bd9Sstevel@tonic-gate 	if (Gen.g_dirfd == -1) {
7846ced83f9bSceastha 		msg(ERRN, "Cannot open attribute directory of file \"%s%s%s\"",
7847ced83f9bSceastha 		    (attrparent == NULL) ? "" : gettext("attribute "),
7848ced83f9bSceastha 		    (attrparent == NULL) ? "" : attrparent,
7849ced83f9bSceastha 		    (attrparent == NULL) ? "" : gettext(" of "), filename);
78507c478bd9Sstevel@tonic-gate 		return;
78517c478bd9Sstevel@tonic-gate 
78527c478bd9Sstevel@tonic-gate 	}
7853ced83f9bSceastha 
7854ced83f9bSceastha 	if (attrparent == NULL) {
78557c478bd9Sstevel@tonic-gate 		savenamep = G_p->g_nam_p;
7856ced83f9bSceastha 	} else {
7857ced83f9bSceastha 		savenamep = G_p->g_attrfnam_p;
7858ced83f9bSceastha 	}
78597c478bd9Sstevel@tonic-gate 
78607c478bd9Sstevel@tonic-gate 	if ((dirpfd = dup(Gen.g_dirfd)) == -1)  {
78617c478bd9Sstevel@tonic-gate 		msg(ERRN, "Cannot dup(2) attribute directory descriptor");
78627c478bd9Sstevel@tonic-gate 		return;
78637c478bd9Sstevel@tonic-gate 	}
78647c478bd9Sstevel@tonic-gate 
7865ced83f9bSceastha 	if ((dirp = fdopendir(dirpfd)) == NULL) {
78667c478bd9Sstevel@tonic-gate 		msg(ERRN, "Cannot fdopendir(2) directory file descriptor");
78677c478bd9Sstevel@tonic-gate 		return;
78687c478bd9Sstevel@tonic-gate 	}
78697c478bd9Sstevel@tonic-gate 
7870ced83f9bSceastha 	if (attrparent == NULL) {
7871ced83f9bSceastha 		Gen.g_baseparent_fd = save_cwd();
7872ced83f9bSceastha 	}
7873ced83f9bSceastha 
7874ced83f9bSceastha 	while ((dp = readdir(dirp)) != NULL) {
7875ced83f9bSceastha 		if (strcmp(dp->d_name, "..") == 0) {
7876ced83f9bSceastha 			continue;
7877ced83f9bSceastha 		}
7878ced83f9bSceastha 		if (verify_attr(dp->d_name, attrparent,
7879ced83f9bSceastha 		    arc_rwsysattr, &rw_sysattr) != ATTR_OK) {
78807c478bd9Sstevel@tonic-gate 			continue;
78817c478bd9Sstevel@tonic-gate 		}
78827c478bd9Sstevel@tonic-gate 
78837c478bd9Sstevel@tonic-gate 		if (strcmp(dp->d_name, ".") == 0) {
78847c478bd9Sstevel@tonic-gate 			Hiddendir = 1;
78857c478bd9Sstevel@tonic-gate 		} else {
78867c478bd9Sstevel@tonic-gate 			Hiddendir = 0;
78877c478bd9Sstevel@tonic-gate 		}
78887c478bd9Sstevel@tonic-gate 
7889ced83f9bSceastha 		Gen.g_rw_sysattr = rw_sysattr;
78907c478bd9Sstevel@tonic-gate 		Gen.g_attrnam_p = dp->d_name;
78917c478bd9Sstevel@tonic-gate 
78927c478bd9Sstevel@tonic-gate 		if (STAT(Gen.g_dirfd, Gen.g_nam_p, &SrcSt) == -1) {
78937c478bd9Sstevel@tonic-gate 			msg(ERRN,
78947c478bd9Sstevel@tonic-gate 			    "Could not fstatat(2) attribute \"%s\" of"
7895ced83f9bSceastha 			    " file \"%s\"", dp->d_name, (attrparent == NULL) ?
7896ced83f9bSceastha 			    savenamep : Gen.g_attrfnam_p);
78977c478bd9Sstevel@tonic-gate 			continue;
78987c478bd9Sstevel@tonic-gate 		}
78997c478bd9Sstevel@tonic-gate 
79007c478bd9Sstevel@tonic-gate 		if (Use_old_stat) {
7901ced83f9bSceastha 			Savedev = SrcSt.st_dev;
79027c478bd9Sstevel@tonic-gate 			OldSt = convert_to_old_stat(&SrcSt,
79037c478bd9Sstevel@tonic-gate 			    Gen.g_nam_p, Gen.g_attrnam_p);
79047c478bd9Sstevel@tonic-gate 
79057c478bd9Sstevel@tonic-gate 			if (OldSt == NULL) {
79067c478bd9Sstevel@tonic-gate 				msg(ERRN,
79077c478bd9Sstevel@tonic-gate 				    "Could not convert to old stat format");
79087c478bd9Sstevel@tonic-gate 				continue;
79097c478bd9Sstevel@tonic-gate 			}
79107c478bd9Sstevel@tonic-gate 		}
79117c478bd9Sstevel@tonic-gate 
79127c478bd9Sstevel@tonic-gate 		Gen.g_attrfnam_p = savenamep;
79137c478bd9Sstevel@tonic-gate 
79147c478bd9Sstevel@tonic-gate 		/*
79157c478bd9Sstevel@tonic-gate 		 * Set up dummy header name
79167c478bd9Sstevel@tonic-gate 		 *
79177c478bd9Sstevel@tonic-gate 		 * One piece is written with .hdr, which
79187c478bd9Sstevel@tonic-gate 		 * contains the actual xattr hdr or pathing information
79197c478bd9Sstevel@tonic-gate 		 * then the name is updated to drop the .hdr off
79207c478bd9Sstevel@tonic-gate 		 * and the actual file itself is archived.
79217c478bd9Sstevel@tonic-gate 		 */
79227c478bd9Sstevel@tonic-gate 		slen = strlen(Gen.g_attrnam_p) + strlen(DEVNULL) +
7923ced83f9bSceastha 		    strlen(XATTRHDR) + 2;	/* add one for '/' */
7924ced83f9bSceastha 		if ((namep = e_zalloc(E_NORMAL, slen)) == NULL) {
79257c478bd9Sstevel@tonic-gate 			msg(ERRN, "Could not calloc memory for attribute name");
79267c478bd9Sstevel@tonic-gate 			continue;
79277c478bd9Sstevel@tonic-gate 		}
7928ced83f9bSceastha 		(void) snprintf(namep, slen, "%s/%s%s",
79297c478bd9Sstevel@tonic-gate 		    DEVNULL, Gen.g_attrnam_p, XATTRHDR);
79307c478bd9Sstevel@tonic-gate 		Gen.g_nam_p = namep;
79317c478bd9Sstevel@tonic-gate 
7932ced83f9bSceastha 		plen = strlen(Gen.g_attrnam_p) + 1;
7933ced83f9bSceastha 		if (Gen.g_attrparent_p != NULL) {
7934ced83f9bSceastha 			plen += strlen(Gen.g_attrparent_p) + 1;
7935ced83f9bSceastha 		}
7936ced83f9bSceastha 		if ((apathp = e_zalloc(E_NORMAL, plen)) == NULL) {
7937ced83f9bSceastha 			msg(ERRN, "Could not calloc memory for attribute name");
7938ced83f9bSceastha 			continue;
7939ced83f9bSceastha 		}
7940ced83f9bSceastha 		(void) snprintf(apathp, plen, "%s%s%s",
7941ced83f9bSceastha 		    (Gen.g_attrparent_p == NULL) ? "" : Gen.g_attrparent_p,
7942ced83f9bSceastha 		    (Gen.g_attrparent_p == NULL) ? "" : "/", Gen.g_attrnam_p);
7943ced83f9bSceastha 
7944ced83f9bSceastha 		if (Gen.g_attrpath_p != NULL) {
7945ced83f9bSceastha 			free(Gen.g_attrpath_p);
7946ced83f9bSceastha 		}
7947ced83f9bSceastha 		Gen.g_attrpath_p = apathp;
7948ced83f9bSceastha 
79497c478bd9Sstevel@tonic-gate 		/*
79507c478bd9Sstevel@tonic-gate 		 * Get attribute's ACL info: don't bother allocating space
79517c478bd9Sstevel@tonic-gate 		 * if there are only standard permissions, i.e. ACL count < 4
79527c478bd9Sstevel@tonic-gate 		 */
79537c478bd9Sstevel@tonic-gate 		if (Pflag) {
79547c478bd9Sstevel@tonic-gate 			filefd = openat(Gen.g_dirfd, dp->d_name, O_RDONLY);
79557c478bd9Sstevel@tonic-gate 			if (filefd == -1) {
79567c478bd9Sstevel@tonic-gate 				msg(ERRN,
79577c478bd9Sstevel@tonic-gate 				    "Could not open attribute \"%s\" of"
79587c478bd9Sstevel@tonic-gate 				    " file \"%s\"", dp->d_name, savenamep);
79597c478bd9Sstevel@tonic-gate 				free(namep);
79607c478bd9Sstevel@tonic-gate 				continue;
79617c478bd9Sstevel@tonic-gate 			}
7962fa9e4066Sahrens 			if (facl_get(filefd, ACL_NO_TRIVIAL, &aclp) != 0) {
79637c478bd9Sstevel@tonic-gate 				msg(ERRN,
79647c478bd9Sstevel@tonic-gate 				    "Error with acl() on %s",
79657c478bd9Sstevel@tonic-gate 				    Gen.g_nam_p);
79667c478bd9Sstevel@tonic-gate 			}
79677c478bd9Sstevel@tonic-gate 			(void) close(filefd);
79687c478bd9Sstevel@tonic-gate 		}
7969ced83f9bSceastha 
79707c478bd9Sstevel@tonic-gate 		(void) creat_hdr();
79717c478bd9Sstevel@tonic-gate 		(void) (*func)();
7972ced83f9bSceastha 
7973ced83f9bSceastha #if defined(_PC_SATTR_ENABLED)
7974ced83f9bSceastha 		/*
7975ced83f9bSceastha 		 * Recursively call xattrs_out() to process the attribute's
7976ced83f9bSceastha 		 * hidden attribute directory and read-write system attributes.
7977ced83f9bSceastha 		 */
7978ced83f9bSceastha 		if (SysAtflag && !Hiddendir && !rw_sysattr) {
7979ced83f9bSceastha 			int	savedirfd = Gen.g_dirfd;
7980ced83f9bSceastha 
7981ced83f9bSceastha 			(void) fchdir(Gen.g_dirfd);
7982ced83f9bSceastha 			Gen.g_attrparent_p = dp->d_name;
7983ced83f9bSceastha 			xattrs_out(func);
7984ced83f9bSceastha 			Gen.g_dirfd = savedirfd;
7985ced83f9bSceastha 			Gen.g_attrparent_p = NULL;
7986ced83f9bSceastha 		}
7987ced83f9bSceastha #endif	/* _PC_SATTR_ENABLED */
7988ced83f9bSceastha 
79897c478bd9Sstevel@tonic-gate 		if (Gen.g_passdirfd != -1) {
79907c478bd9Sstevel@tonic-gate 			(void) close(Gen.g_passdirfd);
79917c478bd9Sstevel@tonic-gate 			Gen.g_passdirfd = -1;
79927c478bd9Sstevel@tonic-gate 		}
7993ced83f9bSceastha 		Gen.g_attrnam_p = NULL;
7994ced83f9bSceastha 		Gen.g_attrfnam_p = NULL;
7995ced83f9bSceastha 		Gen.g_linktoattrfnam_p = NULL;
7996ced83f9bSceastha 		Gen.g_linktoattrnam_p = NULL;
7997ced83f9bSceastha 		Gen.g_rw_sysattr = 0;
7998ced83f9bSceastha 		if (Gen.g_attrpath_p != NULL) {
7999ced83f9bSceastha 			free(Gen.g_attrpath_p);
8000ced83f9bSceastha 			Gen.g_attrpath_p = NULL;
8001ced83f9bSceastha 		}
8002ced83f9bSceastha 
8003fa9e4066Sahrens 		if (aclp != NULL) {
8004fa9e4066Sahrens 			acl_free(aclp);
80057c478bd9Sstevel@tonic-gate 			aclp = NULL;
8006fa9e4066Sahrens 			acl_is_set = 0;
80077c478bd9Sstevel@tonic-gate 		}
80087c478bd9Sstevel@tonic-gate 		free(namep);
80097c478bd9Sstevel@tonic-gate 	}
80107c478bd9Sstevel@tonic-gate 
80117c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
80127c478bd9Sstevel@tonic-gate 	(void) close(Gen.g_dirfd);
8013ced83f9bSceastha 	if (attrparent == NULL) {
8014ced83f9bSceastha 		rest_cwd(Gen.g_baseparent_fd);
80157c478bd9Sstevel@tonic-gate 		Gen.g_dirfd = -1;
80167c478bd9Sstevel@tonic-gate 	}
8017ced83f9bSceastha 	Hiddendir = 0;
8018ced83f9bSceastha }
80197c478bd9Sstevel@tonic-gate #else
80207c478bd9Sstevel@tonic-gate static void
80217c478bd9Sstevel@tonic-gate xattrs_out(int (*func)())
80227c478bd9Sstevel@tonic-gate {
80237c478bd9Sstevel@tonic-gate }
80247c478bd9Sstevel@tonic-gate #endif
80257c478bd9Sstevel@tonic-gate 
80267c478bd9Sstevel@tonic-gate /*
80277c478bd9Sstevel@tonic-gate  * Return the parent directory of a given path.
80287c478bd9Sstevel@tonic-gate  *
80297c478bd9Sstevel@tonic-gate  * Examples:
80307c478bd9Sstevel@tonic-gate  * /usr/tmp return /usr
80317c478bd9Sstevel@tonic-gate  * /usr/tmp/file return /usr/tmp
80327c478bd9Sstevel@tonic-gate  * /  returns .
80337c478bd9Sstevel@tonic-gate  * /usr returns /
80347c478bd9Sstevel@tonic-gate  * file returns .
80357c478bd9Sstevel@tonic-gate  *
80367c478bd9Sstevel@tonic-gate  * dir is assumed to be at least as big as path.
80377c478bd9Sstevel@tonic-gate  */
80387c478bd9Sstevel@tonic-gate static void
80397c478bd9Sstevel@tonic-gate get_parent(char *path, char *dir)
80407c478bd9Sstevel@tonic-gate {
80417c478bd9Sstevel@tonic-gate 	char *s;
80427c478bd9Sstevel@tonic-gate 	char tmpdir[PATH_MAX + 1];
80437c478bd9Sstevel@tonic-gate 
80447c478bd9Sstevel@tonic-gate 	if (strlen(path) > PATH_MAX) {
80457c478bd9Sstevel@tonic-gate 		msg(EXT, "pathname is too long");
80467c478bd9Sstevel@tonic-gate 	}
80477c478bd9Sstevel@tonic-gate 	(void) strcpy(tmpdir, path);
80487c478bd9Sstevel@tonic-gate 	chop_endslashes(tmpdir);
80497c478bd9Sstevel@tonic-gate 
80507c478bd9Sstevel@tonic-gate 	if ((s = strrchr(tmpdir, '/')) == NULL) {
80517c478bd9Sstevel@tonic-gate 		(void) strcpy(dir, ".");
80527c478bd9Sstevel@tonic-gate 	} else {
80537c478bd9Sstevel@tonic-gate 		s = skipslashes(s, tmpdir);
80547c478bd9Sstevel@tonic-gate 		*s = '\0';
80557c478bd9Sstevel@tonic-gate 		if (s == tmpdir)
80567c478bd9Sstevel@tonic-gate 			(void) strcpy(dir, "/");
80577c478bd9Sstevel@tonic-gate 		else
80587c478bd9Sstevel@tonic-gate 			(void) strcpy(dir, tmpdir);
80597c478bd9Sstevel@tonic-gate 	}
80607c478bd9Sstevel@tonic-gate }
80617c478bd9Sstevel@tonic-gate 
80627c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
80637c478bd9Sstevel@tonic-gate #define	ROUNDTOTBLOCK(a)		((a + (TBLOCK -1)) & ~(TBLOCK -1))
80647c478bd9Sstevel@tonic-gate 
80657c478bd9Sstevel@tonic-gate static void
80667c478bd9Sstevel@tonic-gate prepare_xattr_hdr(
80677c478bd9Sstevel@tonic-gate 	char		**attrbuf,
80687c478bd9Sstevel@tonic-gate 	char		*filename,
8069ced83f9bSceastha 	char		*attrpath,
80707c478bd9Sstevel@tonic-gate 	char		typeflag,
80717c478bd9Sstevel@tonic-gate 	struct Lnk	*linkinfo,
80727c478bd9Sstevel@tonic-gate 	int		*rlen)
80737c478bd9Sstevel@tonic-gate {
80747c478bd9Sstevel@tonic-gate 	char			*bufhead;	/* ptr to full buffer */
8075ced83f9bSceastha 	char			*aptr;
80767c478bd9Sstevel@tonic-gate 	struct xattr_hdr 	*hptr;		/* ptr to header in bufhead */
80777c478bd9Sstevel@tonic-gate 	struct xattr_buf	*tptr;		/* ptr to pathing pieces */
80787c478bd9Sstevel@tonic-gate 	int			totalen;	/* total buffer length */
80797c478bd9Sstevel@tonic-gate 	int			len;		/* length returned to user */
80807c478bd9Sstevel@tonic-gate 	int			stringlen;	/* length of filename + attr */
8081ced83f9bSceastha 						/*
8082ced83f9bSceastha 						 * length of filename + attr
8083ced83f9bSceastha 						 * in link section
8084ced83f9bSceastha 						 */
8085ced83f9bSceastha 	int			linkstringlen;
80867c478bd9Sstevel@tonic-gate 	int			complen;	/* length of pathing section */
80877c478bd9Sstevel@tonic-gate 	int			linklen;	/* length of link section */
8088ced83f9bSceastha 	int			attrnames_index; /* attrnames starting index */
80897c478bd9Sstevel@tonic-gate 
80907c478bd9Sstevel@tonic-gate 	/*
80917c478bd9Sstevel@tonic-gate 	 * Release previous buffer if any.
80927c478bd9Sstevel@tonic-gate 	 */
80937c478bd9Sstevel@tonic-gate 
8094ced83f9bSceastha 	if (*attrbuf != NULL) {
80957c478bd9Sstevel@tonic-gate 		free(*attrbuf);
80967c478bd9Sstevel@tonic-gate 		*attrbuf = NULL;
80977c478bd9Sstevel@tonic-gate 	}
80987c478bd9Sstevel@tonic-gate 
80997c478bd9Sstevel@tonic-gate 	/*
81007c478bd9Sstevel@tonic-gate 	 * First add in fixed size stuff
81017c478bd9Sstevel@tonic-gate 	 */
81027c478bd9Sstevel@tonic-gate 	len = sizeof (struct xattr_hdr) + sizeof (struct xattr_buf);
81037c478bd9Sstevel@tonic-gate 
81047c478bd9Sstevel@tonic-gate 	/*
81057c478bd9Sstevel@tonic-gate 	 * Add space for two nulls
81067c478bd9Sstevel@tonic-gate 	 */
8107ced83f9bSceastha 	stringlen = strlen(attrpath) + strlen(filename) + 2;
81087c478bd9Sstevel@tonic-gate 	complen = stringlen + sizeof (struct xattr_buf);
81097c478bd9Sstevel@tonic-gate 
81107c478bd9Sstevel@tonic-gate 	len += stringlen;
81117c478bd9Sstevel@tonic-gate 
81127c478bd9Sstevel@tonic-gate 	/*
81137c478bd9Sstevel@tonic-gate 	 * Now add on space for link info if any
81147c478bd9Sstevel@tonic-gate 	 */
81157c478bd9Sstevel@tonic-gate 
81167c478bd9Sstevel@tonic-gate 	if (linkinfo != NULL) {
81177c478bd9Sstevel@tonic-gate 		/*
81187c478bd9Sstevel@tonic-gate 		 * Again add space for two nulls
81197c478bd9Sstevel@tonic-gate 		 */
81207c478bd9Sstevel@tonic-gate 		linkstringlen = strlen(linkinfo->L_gen.g_attrfnam_p) +
81217c478bd9Sstevel@tonic-gate 		    strlen(linkinfo->L_gen.g_attrnam_p) + 2;
8122ced83f9bSceastha 		linklen = linkstringlen + sizeof (struct xattr_buf);
8123ced83f9bSceastha 		len += linklen;
8124ced83f9bSceastha 	} else {
8125ced83f9bSceastha 		linklen = 0;
81267c478bd9Sstevel@tonic-gate 	}
81277c478bd9Sstevel@tonic-gate 
81287c478bd9Sstevel@tonic-gate 	/*
81297c478bd9Sstevel@tonic-gate 	 * Now add padding to end to fill out TBLOCK
81307c478bd9Sstevel@tonic-gate 	 *
81317c478bd9Sstevel@tonic-gate 	 * Function returns size of real data and not size + padding.
81327c478bd9Sstevel@tonic-gate 	 */
81337c478bd9Sstevel@tonic-gate 
81347c478bd9Sstevel@tonic-gate 	totalen = ROUNDTOTBLOCK(len);
81357c478bd9Sstevel@tonic-gate 	bufhead = e_zalloc(E_EXIT, totalen);
81367c478bd9Sstevel@tonic-gate 
81377c478bd9Sstevel@tonic-gate 	/*
81387c478bd9Sstevel@tonic-gate 	 * Now we can fill in the necessary pieces
81397c478bd9Sstevel@tonic-gate 	 */
81407c478bd9Sstevel@tonic-gate 
81417c478bd9Sstevel@tonic-gate 	/*
81427c478bd9Sstevel@tonic-gate 	 * first fill in the fixed header
81437c478bd9Sstevel@tonic-gate 	 */
81447c478bd9Sstevel@tonic-gate 	hptr = (struct xattr_hdr *)bufhead;
81457c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_version, "%s", XATTR_ARCH_VERS);
81467c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_component_len, "%0*d",
81477c478bd9Sstevel@tonic-gate 	    sizeof (hptr->h_component_len) - 1, complen);
81487c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_link_component_len, "%0*d",
81497c478bd9Sstevel@tonic-gate 	    sizeof (hptr->h_link_component_len) - 1, linklen);
81507c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_size, "%0*d", sizeof (hptr->h_size) - 1, len);
81517c478bd9Sstevel@tonic-gate 
81527c478bd9Sstevel@tonic-gate 	/*
81537c478bd9Sstevel@tonic-gate 	 * Now fill in the filename + attrnames section
8154ced83f9bSceastha 	 * The filename and attrnames section can be composed of two or more
8155ced83f9bSceastha 	 * path segments separated by a null character.  The first segment
8156ced83f9bSceastha 	 * is the path to the parent file that roots the entire sequence in
8157ced83f9bSceastha 	 * the normal name space. The remaining segments describes a path
8158ced83f9bSceastha 	 * rooted at the hidden extended attribute directory of the leaf file of
8159ced83f9bSceastha 	 * the previous segment, making it possible to name attributes on
8160ced83f9bSceastha 	 * attributes.  Thus, if we are just archiving an extended attribute,
8161ced83f9bSceastha 	 * the second segment will contain the attribute name.  If we are
8162ced83f9bSceastha 	 * archiving a system attribute of an extended attribute, then the
8163ced83f9bSceastha 	 * second segment will contain the attribute name, and a third segment
8164ced83f9bSceastha 	 * will contain the system attribute name.  The attribute pathing
8165ced83f9bSceastha 	 * information is obtained from 'attrpath'.
81667c478bd9Sstevel@tonic-gate 	 */
81677c478bd9Sstevel@tonic-gate 
81687c478bd9Sstevel@tonic-gate 	tptr = (struct xattr_buf *)(bufhead + sizeof (struct xattr_hdr));
81697c478bd9Sstevel@tonic-gate 	(void) sprintf(tptr->h_namesz, "%0*d", sizeof (tptr->h_namesz) - 1,
81707c478bd9Sstevel@tonic-gate 	    stringlen);
81717c478bd9Sstevel@tonic-gate 	(void) strcpy(tptr->h_names, filename);
8172ced83f9bSceastha 	attrnames_index = strlen(filename) + 1;
8173ced83f9bSceastha 	(void) strcpy(&tptr->h_names[attrnames_index], attrpath);
81747c478bd9Sstevel@tonic-gate 	tptr->h_typeflag = typeflag;
81757c478bd9Sstevel@tonic-gate 
81767c478bd9Sstevel@tonic-gate 	/*
8177ced83f9bSceastha 	 * Split the attrnames section into two segments if 'attrpath'
8178ced83f9bSceastha 	 * contains pathing information for a system attribute of an
8179ced83f9bSceastha 	 * extended attribute.  We split them by replacing the '/' with
8180ced83f9bSceastha 	 * a '\0'.
8181ced83f9bSceastha 	 */
8182ced83f9bSceastha 	if ((aptr = strpbrk(&tptr->h_names[attrnames_index], "/")) != NULL) {
8183ced83f9bSceastha 		*aptr = '\0';
8184ced83f9bSceastha 	}
8185ced83f9bSceastha 
8186ced83f9bSceastha 	/*
81877c478bd9Sstevel@tonic-gate 	 * Now fill in the optional link section if we have one
81887c478bd9Sstevel@tonic-gate 	 */
81897c478bd9Sstevel@tonic-gate 
8190ced83f9bSceastha 	if (linkinfo != NULL) {
81917c478bd9Sstevel@tonic-gate 		tptr = (struct xattr_buf *)(bufhead +
81927c478bd9Sstevel@tonic-gate 		    sizeof (struct xattr_hdr) + complen);
81937c478bd9Sstevel@tonic-gate 
81947c478bd9Sstevel@tonic-gate 		(void) sprintf(tptr->h_namesz, "%0*d",
81957c478bd9Sstevel@tonic-gate 		    sizeof (tptr->h_namesz) - 1, linkstringlen);
81967c478bd9Sstevel@tonic-gate 		(void) strcpy(tptr->h_names, linkinfo->L_gen.g_attrfnam_p);
81977c478bd9Sstevel@tonic-gate 		(void) strcpy(
81987c478bd9Sstevel@tonic-gate 		    &tptr->h_names[strlen(linkinfo->L_gen.g_attrfnam_p) + 1],
81997c478bd9Sstevel@tonic-gate 		    linkinfo->L_gen.g_attrnam_p);
82007c478bd9Sstevel@tonic-gate 		tptr->h_typeflag = typeflag;
82017c478bd9Sstevel@tonic-gate 	}
82027c478bd9Sstevel@tonic-gate 	*attrbuf = (char *)bufhead;
82037c478bd9Sstevel@tonic-gate 	*rlen = len;
82047c478bd9Sstevel@tonic-gate }
82057c478bd9Sstevel@tonic-gate #endif	/* O_XATTR */
82067c478bd9Sstevel@tonic-gate 
82077c478bd9Sstevel@tonic-gate static char
82087c478bd9Sstevel@tonic-gate tartype(int type)
82097c478bd9Sstevel@tonic-gate {
82107c478bd9Sstevel@tonic-gate 	switch (type) {
82117c478bd9Sstevel@tonic-gate 
82127c478bd9Sstevel@tonic-gate 	case S_IFDIR:
82137c478bd9Sstevel@tonic-gate 		return (DIRTYPE);
82147c478bd9Sstevel@tonic-gate 
82157c478bd9Sstevel@tonic-gate 	case S_IFLNK:
82167c478bd9Sstevel@tonic-gate 		return (SYMTYPE);
82177c478bd9Sstevel@tonic-gate 
82187c478bd9Sstevel@tonic-gate 	case S_IFIFO:
82197c478bd9Sstevel@tonic-gate 		return (FIFOTYPE);
82207c478bd9Sstevel@tonic-gate 
82217c478bd9Sstevel@tonic-gate 	case S_IFCHR:
82227c478bd9Sstevel@tonic-gate 		return (CHRTYPE);
82237c478bd9Sstevel@tonic-gate 
82247c478bd9Sstevel@tonic-gate 	case S_IFBLK:
82257c478bd9Sstevel@tonic-gate 		return (BLKTYPE);
82267c478bd9Sstevel@tonic-gate 
82277c478bd9Sstevel@tonic-gate 	case S_IFREG:
82287c478bd9Sstevel@tonic-gate 		return (REGTYPE);
82297c478bd9Sstevel@tonic-gate 
82307c478bd9Sstevel@tonic-gate 	default:
82317c478bd9Sstevel@tonic-gate 		return ('\0');
82327c478bd9Sstevel@tonic-gate 	}
82337c478bd9Sstevel@tonic-gate }
82347c478bd9Sstevel@tonic-gate 
82357c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
82367c478bd9Sstevel@tonic-gate static int
82377c478bd9Sstevel@tonic-gate openfile(int omode)
82387c478bd9Sstevel@tonic-gate {
8239ced83f9bSceastha 	if (G_p->g_attrnam_p != NULL) {
8240ced83f9bSceastha 		return (openat(G_p->g_dirfd, G_p->g_attrnam_p, omode));
82417c478bd9Sstevel@tonic-gate 	} else {
82427c478bd9Sstevel@tonic-gate 		return (openat(G_p->g_dirfd,
82437c478bd9Sstevel@tonic-gate 		    get_component(G_p->g_nam_p), omode));
82447c478bd9Sstevel@tonic-gate 	}
82457c478bd9Sstevel@tonic-gate }
82467c478bd9Sstevel@tonic-gate #else
82477c478bd9Sstevel@tonic-gate static int
82487c478bd9Sstevel@tonic-gate openfile(int omode)
82497c478bd9Sstevel@tonic-gate {
82507c478bd9Sstevel@tonic-gate 	return (openat(G_p->g_dirfd, get_component(G_p->g_nam_p), omode));
82517c478bd9Sstevel@tonic-gate }
82527c478bd9Sstevel@tonic-gate #endif
82537c478bd9Sstevel@tonic-gate 
82547c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
82557c478bd9Sstevel@tonic-gate static int
82567c478bd9Sstevel@tonic-gate read_xattr_hdr()
82577c478bd9Sstevel@tonic-gate {
82587c478bd9Sstevel@tonic-gate 	off_t		bytes;
82597c478bd9Sstevel@tonic-gate 	int		comp_len, link_len;
82607c478bd9Sstevel@tonic-gate 	int		namelen;
8261da6c28aaSamw 	int		asz;
82627c478bd9Sstevel@tonic-gate 	int		cnt;
82637c478bd9Sstevel@tonic-gate 	char		*tp;
8264da6c28aaSamw 	char		*xattrapath;
82657c478bd9Sstevel@tonic-gate 	int		pad;
8266da6c28aaSamw 	int		parentfilelen;
82677c478bd9Sstevel@tonic-gate 
82687c478bd9Sstevel@tonic-gate 	/*
82697c478bd9Sstevel@tonic-gate 	 * Include any padding in the read.  We need to be positioned
82707c478bd9Sstevel@tonic-gate 	 * at beginning of next header.
82717c478bd9Sstevel@tonic-gate 	 */
82727c478bd9Sstevel@tonic-gate 
82737c478bd9Sstevel@tonic-gate 	bytes = Gen.g_filesz;
82747c478bd9Sstevel@tonic-gate 
82757c478bd9Sstevel@tonic-gate 	if ((xattrhead = e_zalloc(E_NORMAL, (size_t)bytes)) == NULL) {
82767c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
82777c478bd9Sstevel@tonic-gate 		    "Insufficient memory for extended attribute\n"));
82787c478bd9Sstevel@tonic-gate 		return (1);
82797c478bd9Sstevel@tonic-gate 	}
82807c478bd9Sstevel@tonic-gate 
82817c478bd9Sstevel@tonic-gate 	tp = (char *)xattrhead;
82827c478bd9Sstevel@tonic-gate 	while (bytes > 0) {
82837c478bd9Sstevel@tonic-gate 		cnt = (int)(bytes > CPIOBSZ) ? CPIOBSZ : bytes;
82847c478bd9Sstevel@tonic-gate 		FILL(cnt);
82857c478bd9Sstevel@tonic-gate 		(void) memcpy(tp, Buffr.b_out_p, cnt);
82867c478bd9Sstevel@tonic-gate 		tp += cnt;
82877c478bd9Sstevel@tonic-gate 		Buffr.b_out_p += cnt;
82887c478bd9Sstevel@tonic-gate 		Buffr.b_cnt -= (off_t)cnt;
82897c478bd9Sstevel@tonic-gate 		bytes -= (off_t)cnt;
82907c478bd9Sstevel@tonic-gate 	}
82917c478bd9Sstevel@tonic-gate 
82927c478bd9Sstevel@tonic-gate 	pad = (Pad_val + 1 - (Gen.g_filesz & Pad_val)) &
82937c478bd9Sstevel@tonic-gate 	    Pad_val;
82947c478bd9Sstevel@tonic-gate 	if (pad != 0) {
82957c478bd9Sstevel@tonic-gate 		FILL(pad);
82967c478bd9Sstevel@tonic-gate 		Buffr.b_out_p += pad;
82977c478bd9Sstevel@tonic-gate 		Buffr.b_cnt -= (off_t)pad;
82987c478bd9Sstevel@tonic-gate 	}
82997c478bd9Sstevel@tonic-gate 
83007c478bd9Sstevel@tonic-gate 	/*
83017c478bd9Sstevel@tonic-gate 	 * Validate that we can handle header format
83027c478bd9Sstevel@tonic-gate 	 */
83037c478bd9Sstevel@tonic-gate 
83047c478bd9Sstevel@tonic-gate 	if (strcmp(xattrhead->h_version, XATTR_ARCH_VERS) != 0) {
83057c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
83067c478bd9Sstevel@tonic-gate 		    gettext("Unknown extended attribute format encountered\n"));
83077c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
83087c478bd9Sstevel@tonic-gate 		    gettext("Disabling extended attribute header parsing\n"));
83097c478bd9Sstevel@tonic-gate 		xattrbadhead = 1;
83107c478bd9Sstevel@tonic-gate 		return (1);
83117c478bd9Sstevel@tonic-gate 	}
83127c478bd9Sstevel@tonic-gate 	(void) sscanf(xattrhead->h_component_len, "%10d", &comp_len);
83137c478bd9Sstevel@tonic-gate 	(void) sscanf(xattrhead->h_link_component_len, "%10d", &link_len);
83147c478bd9Sstevel@tonic-gate 	xattrp = (struct xattr_buf *)(((char *)xattrhead) +
83157c478bd9Sstevel@tonic-gate 	    sizeof (struct xattr_hdr));
83167c478bd9Sstevel@tonic-gate 	(void) sscanf(xattrp->h_namesz, "%7d", &namelen);
83177c478bd9Sstevel@tonic-gate 	if (link_len > 0) {
83187c478bd9Sstevel@tonic-gate 		xattr_linkp = (struct xattr_buf *)((int)xattrp + (int)comp_len);
83197c478bd9Sstevel@tonic-gate 	} else {
83207c478bd9Sstevel@tonic-gate 		xattr_linkp = NULL;
83217c478bd9Sstevel@tonic-gate 	}
83227c478bd9Sstevel@tonic-gate 
8323da6c28aaSamw 	/*
8324da6c28aaSamw 	 * Gather the attribute path from the filename and attrnames section.
8325da6c28aaSamw 	 * The filename and attrnames section can be composed of two or more
8326da6c28aaSamw 	 * path segments separated by a null character.  The first segment
8327da6c28aaSamw 	 * is the path to the parent file that roots the entire sequence in
8328da6c28aaSamw 	 * the normal name space. The remaining segments describes a path
8329da6c28aaSamw 	 * rooted at the hidden extended attribute directory of the leaf file of
8330da6c28aaSamw 	 * the previous segment, making it possible to name attributes on
8331da6c28aaSamw 	 * attributes.
8332da6c28aaSamw 	 */
8333da6c28aaSamw 	parentfilelen = strlen(xattrp->h_names);
8334da6c28aaSamw 	xattrapath = xattrp->h_names + parentfilelen + 1;
8335da6c28aaSamw 	asz = strlen(xattrapath);
8336da6c28aaSamw 	if ((asz + parentfilelen + 2) < namelen) {
8337da6c28aaSamw 		/*
8338da6c28aaSamw 		 * The attrnames section contains a system attribute on an
8339da6c28aaSamw 		 * attribute.  Save the name of the attribute for use later,
8340da6c28aaSamw 		 * and replace the null separating the attribute name from
8341da6c28aaSamw 		 * the system attribute name with a '/' so that xattrapath can
8342da6c28aaSamw 		 * be used to display messages with the full attribute path name
8343da6c28aaSamw 		 * rooted at the hidden attribute directory of the base file
8344da6c28aaSamw 		 * in normal name space.
8345da6c28aaSamw 		 */
8346da6c28aaSamw 		xattrapath[asz] = '/';
8347da6c28aaSamw 	}
8348da6c28aaSamw 
83497c478bd9Sstevel@tonic-gate 	return (0);
83507c478bd9Sstevel@tonic-gate }
83517c478bd9Sstevel@tonic-gate #endif
83527c478bd9Sstevel@tonic-gate 
83537c478bd9Sstevel@tonic-gate static mode_t
83547c478bd9Sstevel@tonic-gate attrmode(char type)
83557c478bd9Sstevel@tonic-gate {
83567c478bd9Sstevel@tonic-gate 	mode_t mode;
83577c478bd9Sstevel@tonic-gate 
83587c478bd9Sstevel@tonic-gate 	switch (type) {
83597c478bd9Sstevel@tonic-gate 	case '\0':
83607c478bd9Sstevel@tonic-gate 	case REGTYPE:
83617c478bd9Sstevel@tonic-gate 	case LNKTYPE:
83627c478bd9Sstevel@tonic-gate 		mode = S_IFREG;
83637c478bd9Sstevel@tonic-gate 		break;
83647c478bd9Sstevel@tonic-gate 
83657c478bd9Sstevel@tonic-gate 	case SYMTYPE:
83667c478bd9Sstevel@tonic-gate 		mode = S_IFLNK;
83677c478bd9Sstevel@tonic-gate 		break;
83687c478bd9Sstevel@tonic-gate 
83697c478bd9Sstevel@tonic-gate 	case CHRTYPE:
83707c478bd9Sstevel@tonic-gate 		mode = S_IFCHR;
83717c478bd9Sstevel@tonic-gate 		break;
83727c478bd9Sstevel@tonic-gate 	case BLKTYPE:
83737c478bd9Sstevel@tonic-gate 		mode = S_IFBLK;
83747c478bd9Sstevel@tonic-gate 		break;
83757c478bd9Sstevel@tonic-gate 	case DIRTYPE:
83767c478bd9Sstevel@tonic-gate 		mode = S_IFDIR;
83777c478bd9Sstevel@tonic-gate 		break;
83787c478bd9Sstevel@tonic-gate 	case FIFOTYPE:
83797c478bd9Sstevel@tonic-gate 		mode = S_IFIFO;
83807c478bd9Sstevel@tonic-gate 		break;
83817c478bd9Sstevel@tonic-gate 	case CONTTYPE:
83827c478bd9Sstevel@tonic-gate 	default:
83837c478bd9Sstevel@tonic-gate 		mode = 0;
83847c478bd9Sstevel@tonic-gate 	}
83857c478bd9Sstevel@tonic-gate 
83867c478bd9Sstevel@tonic-gate 	return (mode);
83877c478bd9Sstevel@tonic-gate }
83887c478bd9Sstevel@tonic-gate 
83897c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
83907c478bd9Sstevel@tonic-gate static char *
83917c478bd9Sstevel@tonic-gate get_component(char *path)
83927c478bd9Sstevel@tonic-gate {
83937c478bd9Sstevel@tonic-gate 	char *ptr;
83947c478bd9Sstevel@tonic-gate 
83957c478bd9Sstevel@tonic-gate 	ptr = strrchr(path, '/');
83967c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
83977c478bd9Sstevel@tonic-gate 		return (path);
83987c478bd9Sstevel@tonic-gate 	} else {
83997c478bd9Sstevel@tonic-gate 		/*
84007c478bd9Sstevel@tonic-gate 		 * Handle trailing slash
84017c478bd9Sstevel@tonic-gate 		 */
84027c478bd9Sstevel@tonic-gate 		if (*(ptr + 1) == '\0')
84037c478bd9Sstevel@tonic-gate 			return (ptr);
84047c478bd9Sstevel@tonic-gate 		else
84057c478bd9Sstevel@tonic-gate 			return (ptr + 1);
84067c478bd9Sstevel@tonic-gate 	}
84077c478bd9Sstevel@tonic-gate }
84087c478bd9Sstevel@tonic-gate #else
84097c478bd9Sstevel@tonic-gate static char *
84107c478bd9Sstevel@tonic-gate get_component(char *path)
84117c478bd9Sstevel@tonic-gate {
84127c478bd9Sstevel@tonic-gate 	return (path);
84137c478bd9Sstevel@tonic-gate }
84147c478bd9Sstevel@tonic-gate #endif
84157c478bd9Sstevel@tonic-gate 
84167c478bd9Sstevel@tonic-gate static int
84177c478bd9Sstevel@tonic-gate open_dir(char *name)
84187c478bd9Sstevel@tonic-gate {
84197c478bd9Sstevel@tonic-gate 	int fd = -1;
84207c478bd9Sstevel@tonic-gate 	int cnt = 0;
84217c478bd9Sstevel@tonic-gate 	char *dir;
84227c478bd9Sstevel@tonic-gate 
84237c478bd9Sstevel@tonic-gate 	dir = e_zalloc(E_EXIT, strlen(name) + 1);
84247c478bd9Sstevel@tonic-gate 
84257c478bd9Sstevel@tonic-gate 	/*
84267c478bd9Sstevel@tonic-gate 	 * open directory; creating missing directories along the way.
84277c478bd9Sstevel@tonic-gate 	 */
84287c478bd9Sstevel@tonic-gate 	get_parent(name, dir);
84297c478bd9Sstevel@tonic-gate 	do {
84307c478bd9Sstevel@tonic-gate 		fd = open(dir, O_RDONLY);
84317c478bd9Sstevel@tonic-gate 		if (fd != -1) {
84327c478bd9Sstevel@tonic-gate 			free(dir);
84337c478bd9Sstevel@tonic-gate 			return (fd);
84347c478bd9Sstevel@tonic-gate 		}
84357c478bd9Sstevel@tonic-gate 		cnt++;
84367c478bd9Sstevel@tonic-gate 	} while (cnt <= 1 && missdir(name) == 0);
84377c478bd9Sstevel@tonic-gate 
84387c478bd9Sstevel@tonic-gate 	free(dir);
84397c478bd9Sstevel@tonic-gate 	return (-1);
84407c478bd9Sstevel@tonic-gate }
84417c478bd9Sstevel@tonic-gate 
84427c478bd9Sstevel@tonic-gate static int
84437c478bd9Sstevel@tonic-gate open_dirfd()
84447c478bd9Sstevel@tonic-gate {
84457c478bd9Sstevel@tonic-gate #ifdef O_XATTR
84467c478bd9Sstevel@tonic-gate 	if ((Args & OCt) == 0) {
84477c478bd9Sstevel@tonic-gate 		close_dirfd();
8448ced83f9bSceastha 		if (G_p->g_attrnam_p != NULL) {
8449ced83f9bSceastha 			int	rw_sysattr;
8450ced83f9bSceastha 
8451ced83f9bSceastha 			/*
8452ced83f9bSceastha 			 * Open the file's attribute directory.
8453ced83f9bSceastha 			 * Change into the base file's starting directory then
8454ced83f9bSceastha 			 * call open_attr_dir() to open the attribute directory
8455ced83f9bSceastha 			 * of either the base file (if G_p->g_attrparent_p is
8456ced83f9bSceastha 			 * NULL) or the attribute (if G_p->g_attrparent_p is
8457ced83f9bSceastha 			 * set) of the base file.
8458ced83f9bSceastha 			 */
8459ced83f9bSceastha 			(void) fchdir(G_p->g_baseparent_fd);
8460ced83f9bSceastha 			(void) open_attr_dir(G_p->g_attrnam_p,
8461ced83f9bSceastha 			    G_p->g_attrfnam_p, G_p->g_baseparent_fd,
8462ced83f9bSceastha 			    (G_p->g_attrparent_p == NULL) ? NULL :
8463ced83f9bSceastha 			    G_p->g_attrparent_p, &G_p->g_dirfd, &rw_sysattr);
8464ced83f9bSceastha 			if (Args & OCi) {
8465ced83f9bSceastha 				int	saveerrno = errno;
8466ced83f9bSceastha 
8467ced83f9bSceastha 				(void) fchdir(G_p->g_baseparent_fd);
8468ced83f9bSceastha 				errno = saveerrno;
84697c478bd9Sstevel@tonic-gate 			}
8470ced83f9bSceastha 			if ((G_p->g_dirfd == -1) && (Args & (OCi | OCp))) {
8471ced83f9bSceastha 				msg(ERRN,
8472ced83f9bSceastha 				    "Cannot open attribute directory "
8473ced83f9bSceastha 				    "of %s%s%sfile \"%s\"",
8474ced83f9bSceastha 				    (G_p->g_attrparent_p == NULL) ? "" :
8475ced83f9bSceastha 				    gettext("attribute \""),
8476ced83f9bSceastha 				    (G_p->g_attrparent_p == NULL) ? "" :
8477ced83f9bSceastha 				    G_p->g_attrparent_p,
8478ced83f9bSceastha 				    (G_p->g_attrparent_p == NULL) ? "" :
8479ced83f9bSceastha 				    gettext("\" of "),
8480ced83f9bSceastha 				    G_p->g_attrfnam_p);
8481ced83f9bSceastha 				return (FILE_PASS_ERR);
84827c478bd9Sstevel@tonic-gate 			}
84837c478bd9Sstevel@tonic-gate 		} else {
84847c478bd9Sstevel@tonic-gate 			G_p->g_dirfd = open_dir(G_p->g_nam_p);
84857c478bd9Sstevel@tonic-gate 			if (G_p->g_dirfd == -1) {
84867c478bd9Sstevel@tonic-gate 				msg(ERRN,
84877c478bd9Sstevel@tonic-gate 				    "Cannot open/create %s", G_p->g_nam_p);
84887c478bd9Sstevel@tonic-gate 				return (1);
84897c478bd9Sstevel@tonic-gate 			}
84907c478bd9Sstevel@tonic-gate 		}
84917c478bd9Sstevel@tonic-gate 	} else {
84927c478bd9Sstevel@tonic-gate 		G_p->g_dirfd = -1;
84937c478bd9Sstevel@tonic-gate 	}
84947c478bd9Sstevel@tonic-gate #else
84957c478bd9Sstevel@tonic-gate 	G_p->g_dirfd = -1;
84967c478bd9Sstevel@tonic-gate #endif
84977c478bd9Sstevel@tonic-gate 	return (0);
84987c478bd9Sstevel@tonic-gate }
84997c478bd9Sstevel@tonic-gate 
85007c478bd9Sstevel@tonic-gate static void
85017c478bd9Sstevel@tonic-gate close_dirfd()
85027c478bd9Sstevel@tonic-gate {
85037c478bd9Sstevel@tonic-gate 	if (G_p->g_dirfd != -1) {
85047c478bd9Sstevel@tonic-gate 		(void) close(G_p->g_dirfd);
85057c478bd9Sstevel@tonic-gate 		G_p->g_dirfd = -1;
85067c478bd9Sstevel@tonic-gate 	}
85077c478bd9Sstevel@tonic-gate }
85087c478bd9Sstevel@tonic-gate 
85097c478bd9Sstevel@tonic-gate static void
85107c478bd9Sstevel@tonic-gate write_xattr_hdr()
85117c478bd9Sstevel@tonic-gate {
85127c478bd9Sstevel@tonic-gate 	char *attrbuf = NULL;
85137c478bd9Sstevel@tonic-gate 	int  attrlen = 0;
85147c478bd9Sstevel@tonic-gate 	char *namep;
85157c478bd9Sstevel@tonic-gate 	struct Lnk *tl_p, *linkinfo;
85167c478bd9Sstevel@tonic-gate 
85177c478bd9Sstevel@tonic-gate 
85187c478bd9Sstevel@tonic-gate 	/*
85197c478bd9Sstevel@tonic-gate 	 * namep was allocated in xattrs_out.  It is big enough to hold
85207c478bd9Sstevel@tonic-gate 	 * either the name + .hdr on the end or just the attr name
85217c478bd9Sstevel@tonic-gate 	 */
85227c478bd9Sstevel@tonic-gate 
85237c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
85247c478bd9Sstevel@tonic-gate 	namep = Gen.g_nam_p;
85257c478bd9Sstevel@tonic-gate 	(void) creat_hdr();
85267c478bd9Sstevel@tonic-gate 
85277c478bd9Sstevel@tonic-gate 	if (Args & OCo) {
85287c478bd9Sstevel@tonic-gate 		linkinfo = NULL;
85297c478bd9Sstevel@tonic-gate 		tl_p = Lnk_hd.L_nxt_p;
85307c478bd9Sstevel@tonic-gate 		while (tl_p != &Lnk_hd) {
85317c478bd9Sstevel@tonic-gate 			if (tl_p->L_gen.g_ino == G_p->g_ino &&
85327c478bd9Sstevel@tonic-gate 			    tl_p->L_gen.g_dev == G_p->g_dev) {
85337c478bd9Sstevel@tonic-gate 					linkinfo = tl_p;
85347c478bd9Sstevel@tonic-gate 					break; /* found */
85357c478bd9Sstevel@tonic-gate 			}
85367c478bd9Sstevel@tonic-gate 			tl_p = tl_p->L_nxt_p;
85377c478bd9Sstevel@tonic-gate 		}
85387c478bd9Sstevel@tonic-gate 		prepare_xattr_hdr(&attrbuf, Gen.g_attrfnam_p,
8539ced83f9bSceastha 		    Gen.g_attrpath_p,
8540ced83f9bSceastha 		    (linkinfo == NULL) ?
85417c478bd9Sstevel@tonic-gate 		    tartype(Gen.g_mode & Ftype) : LNKTYPE,
85427c478bd9Sstevel@tonic-gate 		    linkinfo, &attrlen);
85437c478bd9Sstevel@tonic-gate 		Gen.g_filesz = attrlen;
85447c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_XATTR, (off_t)attrlen);
85457c478bd9Sstevel@tonic-gate 		(void) sprintf(namep, "%s/%s", DEVNULL, Gen.g_attrnam_p);
85467c478bd9Sstevel@tonic-gate 		(void) write_ancillary(attrbuf, attrlen);
85477c478bd9Sstevel@tonic-gate 	}
85487c478bd9Sstevel@tonic-gate 
85497c478bd9Sstevel@tonic-gate 	(void) creat_hdr();
85507c478bd9Sstevel@tonic-gate #endif
85517c478bd9Sstevel@tonic-gate }
85527c478bd9Sstevel@tonic-gate 
85537c478bd9Sstevel@tonic-gate /*
85547c478bd9Sstevel@tonic-gate  * skip over extra slashes in string.
85557c478bd9Sstevel@tonic-gate  *
85567c478bd9Sstevel@tonic-gate  * For example:
85577c478bd9Sstevel@tonic-gate  * /usr/tmp/////
85587c478bd9Sstevel@tonic-gate  *
85597c478bd9Sstevel@tonic-gate  * would return pointer at
85607c478bd9Sstevel@tonic-gate  * /usr/tmp/////
85617c478bd9Sstevel@tonic-gate  *         ^
85627c478bd9Sstevel@tonic-gate  */
85637c478bd9Sstevel@tonic-gate static char *
85647c478bd9Sstevel@tonic-gate skipslashes(char *string, char *start)
85657c478bd9Sstevel@tonic-gate {
85667c478bd9Sstevel@tonic-gate 	while ((string > start) && *(string - 1) == '/') {
85677c478bd9Sstevel@tonic-gate 		string--;
85687c478bd9Sstevel@tonic-gate 	}
85697c478bd9Sstevel@tonic-gate 
85707c478bd9Sstevel@tonic-gate 	return (string);
85717c478bd9Sstevel@tonic-gate }
85727c478bd9Sstevel@tonic-gate 
85737c478bd9Sstevel@tonic-gate static sl_info_t *
85747c478bd9Sstevel@tonic-gate sl_info_alloc(void)
85757c478bd9Sstevel@tonic-gate {
85767c478bd9Sstevel@tonic-gate 	static int num_left;
85777c478bd9Sstevel@tonic-gate 	static sl_info_t *slipool;
85787c478bd9Sstevel@tonic-gate 
85797c478bd9Sstevel@tonic-gate 	if (num_left > 0) {
85807c478bd9Sstevel@tonic-gate 		return (&slipool[--num_left]);
85817c478bd9Sstevel@tonic-gate 	}
85827c478bd9Sstevel@tonic-gate 	num_left = SL_INFO_ALLOC_CHUNK;
85837c478bd9Sstevel@tonic-gate 	slipool = e_zalloc(E_EXIT, sizeof (sl_info_t) * num_left);
85847c478bd9Sstevel@tonic-gate 	return (&slipool[--num_left]);
85857c478bd9Sstevel@tonic-gate }
85867c478bd9Sstevel@tonic-gate 
85877c478bd9Sstevel@tonic-gate /*
85887c478bd9Sstevel@tonic-gate  * If a match for the key values was found in the tree, return a pointer to it.
85897c478bd9Sstevel@tonic-gate  * If a match was not found, insert it and return a pointer to it.  This is
85907c478bd9Sstevel@tonic-gate  * based on Knuth's Algorithm A in Vol 3, section 6.2.3.
85917c478bd9Sstevel@tonic-gate  */
85927c478bd9Sstevel@tonic-gate 
85937c478bd9Sstevel@tonic-gate sl_info_t *
8594ced83f9bSceastha sl_insert(dev_t device, ino_t inode, int ftype)
85957c478bd9Sstevel@tonic-gate {
85967c478bd9Sstevel@tonic-gate 	sl_info_t *p;		/* moves down the tree */
85977c478bd9Sstevel@tonic-gate 	sl_info_t *q;		/* scratch */
85987c478bd9Sstevel@tonic-gate 	sl_info_t *r;		/* scratch */
85997c478bd9Sstevel@tonic-gate 	sl_info_t *s;		/* pt where rebalancing may be needed */
86007c478bd9Sstevel@tonic-gate 	sl_info_t *t;		/* father of s */
86017c478bd9Sstevel@tonic-gate 	sl_info_t *head;
86027c478bd9Sstevel@tonic-gate 
86037c478bd9Sstevel@tonic-gate 	int a;			/* used to hold balance factors */
86047c478bd9Sstevel@tonic-gate 	int done;		/* loop control */
86057c478bd9Sstevel@tonic-gate 	int cmpflg;		/* used to hold the result of a comparison */
86067c478bd9Sstevel@tonic-gate 
86077c478bd9Sstevel@tonic-gate 	/* initialize */
86087c478bd9Sstevel@tonic-gate 
86097c478bd9Sstevel@tonic-gate 	head = sl_devhash_lookup(device);
86107c478bd9Sstevel@tonic-gate 
86117c478bd9Sstevel@tonic-gate 	if (head == NULL) {
86127c478bd9Sstevel@tonic-gate 		head = sl_info_alloc();
86137c478bd9Sstevel@tonic-gate 		head->llink = NULL;
86147c478bd9Sstevel@tonic-gate 		head->bal = 0;
86157c478bd9Sstevel@tonic-gate 
86167c478bd9Sstevel@tonic-gate 		p = head->rlink = sl_info_alloc();
86177c478bd9Sstevel@tonic-gate 		p->sl_ino = inode;
8618ced83f9bSceastha 		p->sl_ftype = ftype;
86197c478bd9Sstevel@tonic-gate 		p->sl_count = 0;
86207c478bd9Sstevel@tonic-gate 		p->bal = 0;
86217c478bd9Sstevel@tonic-gate 		p->llink = NULL;
86227c478bd9Sstevel@tonic-gate 		p->rlink = NULL;
86237c478bd9Sstevel@tonic-gate 		sl_devhash_insert(device, head);
86247c478bd9Sstevel@tonic-gate 		return (p);
86257c478bd9Sstevel@tonic-gate 	}
86267c478bd9Sstevel@tonic-gate 
86277c478bd9Sstevel@tonic-gate 	t = head;
86287c478bd9Sstevel@tonic-gate 	s = p = head->rlink;
86297c478bd9Sstevel@tonic-gate 
86307c478bd9Sstevel@tonic-gate 	/* compare */
86317c478bd9Sstevel@tonic-gate 
86327c478bd9Sstevel@tonic-gate 	for (done = 0; ! done; ) {
8633ced83f9bSceastha 		switch (sl_compare(inode, ftype, p->sl_ino, p->sl_ftype)) {
86347c478bd9Sstevel@tonic-gate 			case -1:
86357c478bd9Sstevel@tonic-gate 				/* move left */
86367c478bd9Sstevel@tonic-gate 
86377c478bd9Sstevel@tonic-gate 				q = p->llink;
86387c478bd9Sstevel@tonic-gate 
86397c478bd9Sstevel@tonic-gate 				if (q == NULL) {
86407c478bd9Sstevel@tonic-gate 					q = sl_info_alloc();
86417c478bd9Sstevel@tonic-gate 					p->llink = q;
86427c478bd9Sstevel@tonic-gate 					done = 1;
86437c478bd9Sstevel@tonic-gate 					continue;
86447c478bd9Sstevel@tonic-gate 				}
86457c478bd9Sstevel@tonic-gate 
86467c478bd9Sstevel@tonic-gate 				break;
86477c478bd9Sstevel@tonic-gate 
86487c478bd9Sstevel@tonic-gate 			case 0:
86497c478bd9Sstevel@tonic-gate 				/* found it */
86507c478bd9Sstevel@tonic-gate 				return (p);
86517c478bd9Sstevel@tonic-gate 				break;
86527c478bd9Sstevel@tonic-gate 
86537c478bd9Sstevel@tonic-gate 			case 1:
86547c478bd9Sstevel@tonic-gate 				/* move right */
86557c478bd9Sstevel@tonic-gate 
86567c478bd9Sstevel@tonic-gate 				q = p->rlink;
86577c478bd9Sstevel@tonic-gate 
86587c478bd9Sstevel@tonic-gate 				if (q == NULL) {
86597c478bd9Sstevel@tonic-gate 					q = sl_info_alloc();
86607c478bd9Sstevel@tonic-gate 					p->rlink = q;
86617c478bd9Sstevel@tonic-gate 					done = 1;
86627c478bd9Sstevel@tonic-gate 					continue;
86637c478bd9Sstevel@tonic-gate 				}
86647c478bd9Sstevel@tonic-gate 
86657c478bd9Sstevel@tonic-gate 				break;
86667c478bd9Sstevel@tonic-gate 		}
86677c478bd9Sstevel@tonic-gate 
86687c478bd9Sstevel@tonic-gate 		if (q->bal != 0) {
86697c478bd9Sstevel@tonic-gate 			t = p;
86707c478bd9Sstevel@tonic-gate 			s = q;
86717c478bd9Sstevel@tonic-gate 		}
86727c478bd9Sstevel@tonic-gate 
86737c478bd9Sstevel@tonic-gate 		p = q;
86747c478bd9Sstevel@tonic-gate 	}
86757c478bd9Sstevel@tonic-gate 
86767c478bd9Sstevel@tonic-gate 	/* insert */
86777c478bd9Sstevel@tonic-gate 
86787c478bd9Sstevel@tonic-gate 	q->sl_ino = inode;
8679ced83f9bSceastha 	q->sl_ftype = ftype;
86807c478bd9Sstevel@tonic-gate 	q->sl_count = 0;
86817c478bd9Sstevel@tonic-gate 	q->llink = q->rlink = NULL;
86827c478bd9Sstevel@tonic-gate 	q->bal = 0;
86837c478bd9Sstevel@tonic-gate 
86847c478bd9Sstevel@tonic-gate 	/* adjust balance factors */
86857c478bd9Sstevel@tonic-gate 
8686ced83f9bSceastha 	if ((cmpflg = sl_compare(inode, ftype, s->sl_ino, s->sl_ftype)) < 0) {
86877c478bd9Sstevel@tonic-gate 		r = p = s->llink;
86887c478bd9Sstevel@tonic-gate 	} else {
86897c478bd9Sstevel@tonic-gate 		r = p = s->rlink;
86907c478bd9Sstevel@tonic-gate 	}
86917c478bd9Sstevel@tonic-gate 
86927c478bd9Sstevel@tonic-gate 	while (p != q) {
8693ced83f9bSceastha 		switch (sl_compare(inode, ftype, p->sl_ino, p->sl_ftype)) {
86947c478bd9Sstevel@tonic-gate 			case -1:
86957c478bd9Sstevel@tonic-gate 				p->bal = -1;
86967c478bd9Sstevel@tonic-gate 				p = p->llink;
86977c478bd9Sstevel@tonic-gate 				break;
86987c478bd9Sstevel@tonic-gate 
86997c478bd9Sstevel@tonic-gate 			case 0:
87007c478bd9Sstevel@tonic-gate 				break;
87017c478bd9Sstevel@tonic-gate 
87027c478bd9Sstevel@tonic-gate 			case 1:
87037c478bd9Sstevel@tonic-gate 				p->bal = 1;
87047c478bd9Sstevel@tonic-gate 				p = p->rlink;
87057c478bd9Sstevel@tonic-gate 				break;
87067c478bd9Sstevel@tonic-gate 		}
87077c478bd9Sstevel@tonic-gate 	}
87087c478bd9Sstevel@tonic-gate 
87097c478bd9Sstevel@tonic-gate 	/* balancing act */
87107c478bd9Sstevel@tonic-gate 
87117c478bd9Sstevel@tonic-gate 	if (cmpflg < 0) {
87127c478bd9Sstevel@tonic-gate 		a = -1;
87137c478bd9Sstevel@tonic-gate 	} else {
87147c478bd9Sstevel@tonic-gate 		a = 1;
87157c478bd9Sstevel@tonic-gate 	}
87167c478bd9Sstevel@tonic-gate 
87177c478bd9Sstevel@tonic-gate 	if (s->bal == 0) {
87187c478bd9Sstevel@tonic-gate 		s->bal = a;
87197c478bd9Sstevel@tonic-gate 		head->llink = (sl_info_t *)((int)head->llink + 1);
87207c478bd9Sstevel@tonic-gate 		return (q);
87217c478bd9Sstevel@tonic-gate 	} else if (s->bal == -a) {
87227c478bd9Sstevel@tonic-gate 		s->bal = 0;
87237c478bd9Sstevel@tonic-gate 		return (q);
87247c478bd9Sstevel@tonic-gate 	}
87257c478bd9Sstevel@tonic-gate 
87267c478bd9Sstevel@tonic-gate 	/*
87277c478bd9Sstevel@tonic-gate 	 * (s->bal == a)
87287c478bd9Sstevel@tonic-gate 	 */
87297c478bd9Sstevel@tonic-gate 
87307c478bd9Sstevel@tonic-gate 	if (r->bal == a) {
87317c478bd9Sstevel@tonic-gate 		/* single rotation */
87327c478bd9Sstevel@tonic-gate 
87337c478bd9Sstevel@tonic-gate 		p = r;
87347c478bd9Sstevel@tonic-gate 
87357c478bd9Sstevel@tonic-gate 		if (a == -1) {
87367c478bd9Sstevel@tonic-gate 			s->llink = r->rlink;
87377c478bd9Sstevel@tonic-gate 			r->rlink = s;
87387c478bd9Sstevel@tonic-gate 		} else if (a == 1) {
87397c478bd9Sstevel@tonic-gate 			s->rlink = r->llink;
87407c478bd9Sstevel@tonic-gate 			r->llink = s;
87417c478bd9Sstevel@tonic-gate 		}
87427c478bd9Sstevel@tonic-gate 
87437c478bd9Sstevel@tonic-gate 		s->bal = r->bal = 0;
87447c478bd9Sstevel@tonic-gate 
87457c478bd9Sstevel@tonic-gate 	} else if (r->bal == -a) {
87467c478bd9Sstevel@tonic-gate 		/* double rotation */
87477c478bd9Sstevel@tonic-gate 
87487c478bd9Sstevel@tonic-gate 		if (a == -1) {
87497c478bd9Sstevel@tonic-gate 			p = r->rlink;
87507c478bd9Sstevel@tonic-gate 			r->rlink = p->llink;
87517c478bd9Sstevel@tonic-gate 			p->llink = r;
87527c478bd9Sstevel@tonic-gate 			s->llink = p->rlink;
87537c478bd9Sstevel@tonic-gate 			p->rlink = s;
87547c478bd9Sstevel@tonic-gate 		} else if (a == 1) {
87557c478bd9Sstevel@tonic-gate 			p = r->llink;
87567c478bd9Sstevel@tonic-gate 			r->llink = p->rlink;
87577c478bd9Sstevel@tonic-gate 			p->rlink = r;
87587c478bd9Sstevel@tonic-gate 			s->rlink = p->llink;
87597c478bd9Sstevel@tonic-gate 			p->llink = s;
87607c478bd9Sstevel@tonic-gate 		}
87617c478bd9Sstevel@tonic-gate 
87627c478bd9Sstevel@tonic-gate 		if (p->bal == 0) {
87637c478bd9Sstevel@tonic-gate 			s->bal = 0;
87647c478bd9Sstevel@tonic-gate 			r->bal = 0;
87657c478bd9Sstevel@tonic-gate 		} else if (p->bal == -a) {
87667c478bd9Sstevel@tonic-gate 			s->bal = 0;
87677c478bd9Sstevel@tonic-gate 			r->bal = a;
87687c478bd9Sstevel@tonic-gate 		} else if (p->bal == a) {
87697c478bd9Sstevel@tonic-gate 			s->bal = -a;
87707c478bd9Sstevel@tonic-gate 			r->bal = 0;
87717c478bd9Sstevel@tonic-gate 		}
87727c478bd9Sstevel@tonic-gate 
87737c478bd9Sstevel@tonic-gate 		p->bal = 0;
87747c478bd9Sstevel@tonic-gate 	}
87757c478bd9Sstevel@tonic-gate 
87767c478bd9Sstevel@tonic-gate 	/* finishing touch */
87777c478bd9Sstevel@tonic-gate 
87787c478bd9Sstevel@tonic-gate 	if (s == t->rlink) {
87797c478bd9Sstevel@tonic-gate 		t->rlink = p;
87807c478bd9Sstevel@tonic-gate 	} else {
87817c478bd9Sstevel@tonic-gate 		t->llink = p;
87827c478bd9Sstevel@tonic-gate 	}
87837c478bd9Sstevel@tonic-gate 
87847c478bd9Sstevel@tonic-gate 	return (q);
87857c478bd9Sstevel@tonic-gate }
87867c478bd9Sstevel@tonic-gate 
87877c478bd9Sstevel@tonic-gate /*
87887c478bd9Sstevel@tonic-gate  * sl_numlinks: return the number of links that we saw during our preview.
87897c478bd9Sstevel@tonic-gate  */
87907c478bd9Sstevel@tonic-gate 
87917c478bd9Sstevel@tonic-gate static ulong_t
8792ced83f9bSceastha sl_numlinks(dev_t device, ino_t inode, int ftype)
87937c478bd9Sstevel@tonic-gate {
8794ced83f9bSceastha 	sl_info_t *p = sl_search(device, inode, ftype);
87957c478bd9Sstevel@tonic-gate 
87967c478bd9Sstevel@tonic-gate 	if (p) {
87977c478bd9Sstevel@tonic-gate 		return (p->sl_count);
87987c478bd9Sstevel@tonic-gate 	} else {
87997c478bd9Sstevel@tonic-gate 		return (1);
88007c478bd9Sstevel@tonic-gate 	}
88017c478bd9Sstevel@tonic-gate }
88027c478bd9Sstevel@tonic-gate 
88037c478bd9Sstevel@tonic-gate /*
8804ced83f9bSceastha  * Preview extended and extended system attributes.
8805ced83f9bSceastha  *
8806ced83f9bSceastha  * Return 0 if successful, otherwise return 1.
8807ced83f9bSceastha  */
8808ced83f9bSceastha #if defined(O_XATTR)
8809ced83f9bSceastha static int
8810ced83f9bSceastha preview_attrs(char *s, char *attrparent)
8811ced83f9bSceastha {
8812ced83f9bSceastha 	char		*filename = (attrparent == NULL) ? s : attrparent;
8813ced83f9bSceastha 	int		dirfd;
8814ced83f9bSceastha 	int		tmpfd;
8815ced83f9bSceastha 	int		islnk;
8816ced83f9bSceastha 	int		rc = 0;
8817ced83f9bSceastha 	int		arc_rwsysattr = 0;
8818ced83f9bSceastha 	int		rw_sysattr = 0;
8819ced83f9bSceastha 	int		ext_attr = 0;
8820ced83f9bSceastha 	DIR		*dirp;
8821ced83f9bSceastha 	struct dirent	*dp;
8822ced83f9bSceastha 	struct stat	sb;
8823ced83f9bSceastha 
8824ced83f9bSceastha 	/*
8825ced83f9bSceastha 	 * If the underlying file system supports it, then
8826ced83f9bSceastha 	 * archive the extended attributes if -@ was specified,
8827ced83f9bSceastha 	 * and the extended system attributes if -/ was
8828ced83f9bSceastha 	 * specified.
8829ced83f9bSceastha 	 */
8830ced83f9bSceastha 	if (verify_attr_support(filename, (attrparent == NULL), ARC_CREATE,
8831ced83f9bSceastha 	    &ext_attr) != ATTR_OK) {
8832ced83f9bSceastha 		return (1);
8833ced83f9bSceastha 	}
8834ced83f9bSceastha 
8835ced83f9bSceastha #if defined(_PC_SATTR_ENABLED)
8836ced83f9bSceastha 	if (SysAtflag) {
8837ced83f9bSceastha 		int		filefd;
8838ced83f9bSceastha 		nvlist_t 	*slist = NULL;
8839ced83f9bSceastha 
8840ced83f9bSceastha 		/* Determine if there are non-transient system attributes. */
8841ced83f9bSceastha 		errno = 0;
8842ced83f9bSceastha 		if ((filefd = open(filename, O_RDONLY)) < 0) {
8843ced83f9bSceastha 			return (1);
8844ced83f9bSceastha 		}
8845ced83f9bSceastha 		if (((slist = sysattr_list(myname, filefd,
8846ced83f9bSceastha 		    filename)) != NULL) || (errno != 0)) {
8847ced83f9bSceastha 			arc_rwsysattr = 1;
8848ced83f9bSceastha 		}
8849ced83f9bSceastha 		if (slist != NULL) {
8850ced83f9bSceastha 			(void) nvlist_free(slist);
8851ced83f9bSceastha 			slist = NULL;
8852ced83f9bSceastha 		}
8853ced83f9bSceastha 		(void) close(filefd);
8854ced83f9bSceastha 	}
8855ced83f9bSceastha 
8856ced83f9bSceastha 	if ((arc_rwsysattr == 0) && ((attrparent != NULL) ||
8857ced83f9bSceastha 	    (SysAtflag && !ext_attr))) {
8858ced83f9bSceastha 		return (1);
8859ced83f9bSceastha 	}
8860ced83f9bSceastha #endif	/* _PC_SATTR_ENABLED */
8861ced83f9bSceastha 	/*
8862ced83f9bSceastha 	 * We need to open the attribute directory of the
8863ced83f9bSceastha 	 * file, and preview all of the file's attributes as
8864ced83f9bSceastha 	 * attributes of the file can be hard links to other
8865ced83f9bSceastha 	 * attributes of the file.
8866ced83f9bSceastha 	 */
8867ced83f9bSceastha 	dirfd = attropen(filename, ".", O_RDONLY);
8868ced83f9bSceastha 	if (dirfd == -1)
8869ced83f9bSceastha 		return (1);
8870ced83f9bSceastha 
8871ced83f9bSceastha 	tmpfd = dup(dirfd);
8872ced83f9bSceastha 	if (tmpfd == -1) {
8873ced83f9bSceastha 		(void) close(dirfd);
8874ced83f9bSceastha 		return (1);
8875ced83f9bSceastha 	}
8876ced83f9bSceastha 	dirp = fdopendir(tmpfd);
8877ced83f9bSceastha 	if (dirp == NULL) {
8878ced83f9bSceastha 		(void) close(dirfd);
8879ced83f9bSceastha 		(void) close(tmpfd);
8880ced83f9bSceastha 		return (1);
8881ced83f9bSceastha 	}
8882ced83f9bSceastha 
8883ced83f9bSceastha 	while (dp = readdir(dirp)) {
8884ced83f9bSceastha 		if (dp->d_name[0] == '.') {
8885ced83f9bSceastha 			if (dp->d_name[1] == '\0') {
8886ced83f9bSceastha 				Hiddendir = 1;
8887ced83f9bSceastha 			} else if ((dp->d_name[1] == '.') &&
8888ced83f9bSceastha 			    (dp->d_name[2] == '\0')) {
8889ced83f9bSceastha 				continue;
8890ced83f9bSceastha 			} else {
8891ced83f9bSceastha 				Hiddendir = 0;
8892ced83f9bSceastha 			}
8893ced83f9bSceastha 		} else {
8894ced83f9bSceastha 			Hiddendir = 0;
8895ced83f9bSceastha 		}
8896ced83f9bSceastha 
8897ced83f9bSceastha 		if (fstatat(dirfd, dp->d_name, &sb,
8898ced83f9bSceastha 		    AT_SYMLINK_NOFOLLOW) < 0) {
8899ced83f9bSceastha 			continue;
8900ced83f9bSceastha 		}
8901ced83f9bSceastha 
8902ced83f9bSceastha 		if (verify_attr(dp->d_name, attrparent,
8903ced83f9bSceastha 		    arc_rwsysattr, &rw_sysattr) != ATTR_OK) {
8904ced83f9bSceastha 			continue;
8905ced83f9bSceastha 		}
8906ced83f9bSceastha 
8907ced83f9bSceastha 		islnk = 0;
8908ced83f9bSceastha 		if (S_ISLNK(sb.st_mode)) {
8909ced83f9bSceastha 			islnk = 1;
8910ced83f9bSceastha 			if (Args & OCL) {
8911ced83f9bSceastha 				if (fstatat(dirfd, dp->d_name,
8912ced83f9bSceastha 				    &sb, 0) < 0) {
8913ced83f9bSceastha 					continue;
8914ced83f9bSceastha 				}
8915ced83f9bSceastha 			}
8916ced83f9bSceastha 		}
8917ced83f9bSceastha 		sl_remember_tgt(&sb, islnk, rw_sysattr);
8918ced83f9bSceastha 
8919ced83f9bSceastha 		/*
8920ced83f9bSceastha 		 * Recursively call preview_attrs() to preview extended
8921ced83f9bSceastha 		 * system attributes of attributes.
8922ced83f9bSceastha 		 */
8923ced83f9bSceastha 		if (SysAtflag && !Hiddendir && !rw_sysattr) {
8924ced83f9bSceastha 			int	my_cwd = save_cwd();
8925ced83f9bSceastha 
8926ced83f9bSceastha 			(void) fchdir(dirfd);
8927ced83f9bSceastha 			rc = preview_attrs(s, dp->d_name);
8928ced83f9bSceastha 			rest_cwd(my_cwd);
8929ced83f9bSceastha 		}
8930ced83f9bSceastha 	}
8931ced83f9bSceastha 	(void) closedir(dirp);
8932ced83f9bSceastha 	(void) close(dirfd);
8933ced83f9bSceastha 	return (rc);
8934ced83f9bSceastha }
8935ced83f9bSceastha #endif	/* O_XATTR */
8936ced83f9bSceastha 
8937ced83f9bSceastha /*
89387c478bd9Sstevel@tonic-gate  * sl_preview_synonyms:  Read the file list from the input stream, remembering
89397c478bd9Sstevel@tonic-gate  * each reference to each file.
89407c478bd9Sstevel@tonic-gate  */
89417c478bd9Sstevel@tonic-gate 
89427c478bd9Sstevel@tonic-gate static void
89437c478bd9Sstevel@tonic-gate sl_preview_synonyms(void)
89447c478bd9Sstevel@tonic-gate {
89457c478bd9Sstevel@tonic-gate 	char buf [APATH+1];
89467c478bd9Sstevel@tonic-gate 	char *s;
89477c478bd9Sstevel@tonic-gate 
89487c478bd9Sstevel@tonic-gate 	char *suffix = "/cpioXXXXXX";
89497c478bd9Sstevel@tonic-gate 	char *tmpdir = getenv("TMPDIR");
89507c478bd9Sstevel@tonic-gate 	int    tmpfd, islnk;
89517c478bd9Sstevel@tonic-gate 	FILE *tmpfile;
89527c478bd9Sstevel@tonic-gate 	char *tmpfname;
89537c478bd9Sstevel@tonic-gate 
89547c478bd9Sstevel@tonic-gate 	if (tmpdir == NULL || *tmpdir == '\0' ||
89557c478bd9Sstevel@tonic-gate 	    (strlen(tmpdir) + strlen(suffix)) > APATH) {
89567c478bd9Sstevel@tonic-gate 		struct statvfs tdsb;
89577c478bd9Sstevel@tonic-gate 
89587c478bd9Sstevel@tonic-gate 		tmpdir = "/var/tmp";
89597c478bd9Sstevel@tonic-gate 
89607c478bd9Sstevel@tonic-gate 		/* /var/tmp is read-only in the mini-root environment */
89617c478bd9Sstevel@tonic-gate 
89627c478bd9Sstevel@tonic-gate 		if (statvfs(tmpdir, &tdsb) == -1 || tdsb.f_flag & ST_RDONLY) {
89637c478bd9Sstevel@tonic-gate 			tmpdir = "/tmp";
89647c478bd9Sstevel@tonic-gate 		}
89657c478bd9Sstevel@tonic-gate 	}
89667c478bd9Sstevel@tonic-gate 
89677c478bd9Sstevel@tonic-gate 	tmpfname = e_zalloc(E_EXIT, strlen(tmpdir) + strlen(suffix) + 1);
89687c478bd9Sstevel@tonic-gate 
89697c478bd9Sstevel@tonic-gate 	(void) strcpy(tmpfname, tmpdir);
89707c478bd9Sstevel@tonic-gate 	(void) strcat(tmpfname, suffix);
89717c478bd9Sstevel@tonic-gate 
89727c478bd9Sstevel@tonic-gate 	if ((tmpfd = mkstemp(tmpfname)) == -1) {
8973ced83f9bSceastha 		msg(EXTN, "cannot open tmpfile %s%s", tmpdir, suffix);
89747c478bd9Sstevel@tonic-gate 	}
89757c478bd9Sstevel@tonic-gate 
89767c478bd9Sstevel@tonic-gate 	if (unlink(tmpfname) == -1) {
89777c478bd9Sstevel@tonic-gate 		msg(EXTN, "cannot unlink tmpfile %s", tmpfname);
89787c478bd9Sstevel@tonic-gate 	}
89797c478bd9Sstevel@tonic-gate 
89807c478bd9Sstevel@tonic-gate 	if ((tmpfile = fdopen(tmpfd, "w+")) == NULL) {
89817c478bd9Sstevel@tonic-gate 		msg(EXTN, "cannot fdopen tmpfile %s", tmpfname);
89827c478bd9Sstevel@tonic-gate 	}
89837c478bd9Sstevel@tonic-gate 
89847c478bd9Sstevel@tonic-gate 	while ((s = fgets(buf, APATH+1, In_p)) != NULL) {
89857c478bd9Sstevel@tonic-gate 		size_t lastchar;
89867c478bd9Sstevel@tonic-gate 		struct stat sb;
89877c478bd9Sstevel@tonic-gate 
89887c478bd9Sstevel@tonic-gate 		if (fputs(buf, tmpfile) == EOF) {
89897c478bd9Sstevel@tonic-gate 			msg(EXTN, "problem writing to tmpfile %s", tmpfname);
89907c478bd9Sstevel@tonic-gate 		}
89917c478bd9Sstevel@tonic-gate 
89927c478bd9Sstevel@tonic-gate 		/* pre-process the name */
89937c478bd9Sstevel@tonic-gate 
89947c478bd9Sstevel@tonic-gate 		lastchar = strlen(s) - 1;
89957c478bd9Sstevel@tonic-gate 
89967c478bd9Sstevel@tonic-gate 		if (s[lastchar] != '\n' && lastchar == APATH - 1) {
89977c478bd9Sstevel@tonic-gate 			continue;
89987c478bd9Sstevel@tonic-gate 		} else {
89997c478bd9Sstevel@tonic-gate 			s[lastchar] = '\0';
90007c478bd9Sstevel@tonic-gate 		}
90017c478bd9Sstevel@tonic-gate 
90027c478bd9Sstevel@tonic-gate 		while (s[0] == '.' && s[1] == '/') {
90037c478bd9Sstevel@tonic-gate 			s += 2;
90047c478bd9Sstevel@tonic-gate 			while (s[0] == '/') {
90057c478bd9Sstevel@tonic-gate 				s++;
90067c478bd9Sstevel@tonic-gate 			}
90077c478bd9Sstevel@tonic-gate 		}
90087c478bd9Sstevel@tonic-gate 
90097c478bd9Sstevel@tonic-gate 		if (lstat(s, &sb) < 0) {
90107c478bd9Sstevel@tonic-gate 			continue;
90117c478bd9Sstevel@tonic-gate 		}
90127c478bd9Sstevel@tonic-gate 		islnk = 0;
90137c478bd9Sstevel@tonic-gate 		if (S_ISLNK(sb.st_mode)) {
90147c478bd9Sstevel@tonic-gate 			islnk = 1;
90157c478bd9Sstevel@tonic-gate 			if (Args & OCL) {
90167c478bd9Sstevel@tonic-gate 				if (stat(s, &sb) < 0) {
90177c478bd9Sstevel@tonic-gate 					continue;
90187c478bd9Sstevel@tonic-gate 				}
90197c478bd9Sstevel@tonic-gate 			}
90207c478bd9Sstevel@tonic-gate 		}
9021ced83f9bSceastha 		sl_remember_tgt(&sb, islnk, 0);
90227c478bd9Sstevel@tonic-gate 
90237c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
9024ced83f9bSceastha 		if (Atflag || SysAtflag) {
9025ced83f9bSceastha 			(void) preview_attrs(s, NULL);
90267c478bd9Sstevel@tonic-gate 		}
9027ced83f9bSceastha #endif	/* O_XATTR */
90287c478bd9Sstevel@tonic-gate 	}
90297c478bd9Sstevel@tonic-gate 
90307c478bd9Sstevel@tonic-gate 	if (ferror(In_p)) {
90317c478bd9Sstevel@tonic-gate 		msg(EXTN, "error reading stdin");
90327c478bd9Sstevel@tonic-gate 	}
90337c478bd9Sstevel@tonic-gate 
90347c478bd9Sstevel@tonic-gate 	if (fseek(tmpfile, 0L, SEEK_SET) == -1) {
90357c478bd9Sstevel@tonic-gate 		msg(EXTN, "cannot fseek on tmpfile %s", tmpfname);
90367c478bd9Sstevel@tonic-gate 	}
90377c478bd9Sstevel@tonic-gate 
90387c478bd9Sstevel@tonic-gate 	In_p = tmpfile;
90397c478bd9Sstevel@tonic-gate 	free(tmpfname);
90407c478bd9Sstevel@tonic-gate }
90417c478bd9Sstevel@tonic-gate 
90427c478bd9Sstevel@tonic-gate /*
90437c478bd9Sstevel@tonic-gate  * sl_remember_tgt: Add the device/inode for lstat or stat info to the list of
90447c478bd9Sstevel@tonic-gate  * those we've seen before.
90457c478bd9Sstevel@tonic-gate  *
90467c478bd9Sstevel@tonic-gate  * This tree (rooted under head) is keyed by the device/inode of the file
90477c478bd9Sstevel@tonic-gate  * being pointed to.  A count is kept of the number of references encountered
90487c478bd9Sstevel@tonic-gate  * so far.
90497c478bd9Sstevel@tonic-gate  */
90507c478bd9Sstevel@tonic-gate 
90517c478bd9Sstevel@tonic-gate static void
9052ced83f9bSceastha sl_remember_tgt(const struct stat *sbp, int isSymlink, int is_sysattr)
90537c478bd9Sstevel@tonic-gate {
90547c478bd9Sstevel@tonic-gate 	sl_info_t *p;
90557c478bd9Sstevel@tonic-gate 	dev_t device;
90567c478bd9Sstevel@tonic-gate 	ino_t inode;
9057ced83f9bSceastha 	int ftype;
90587c478bd9Sstevel@tonic-gate 
90597c478bd9Sstevel@tonic-gate 	device = sbp->st_dev;
90607c478bd9Sstevel@tonic-gate 	inode  = sbp->st_ino;
9061ced83f9bSceastha 	ftype  = sbp->st_mode & Ftype;
90627c478bd9Sstevel@tonic-gate 
90637c478bd9Sstevel@tonic-gate 	/* Determine whether we've seen this one before */
90647c478bd9Sstevel@tonic-gate 
9065ced83f9bSceastha 	p = sl_insert(device, inode, ftype);
90667c478bd9Sstevel@tonic-gate 
90677c478bd9Sstevel@tonic-gate 	if (p->sl_count > 0) {
90687c478bd9Sstevel@tonic-gate 		/*
9069ced83f9bSceastha 		 * It appears as if have seen this file before as we found a
9070ced83f9bSceastha 		 * matching device, inode, and file type as a file already
9071ced83f9bSceastha 		 * processed.  Since there can possibly be files with the
9072ced83f9bSceastha 		 * same device, inode, and file type, but aren't hard links
9073ced83f9bSceastha 		 * (e.g., read-write system attribute files will always have
9074ced83f9bSceastha 		 * the same inode), we need to only attempt to add one to the
9075ced83f9bSceastha 		 * link count if the file we are processing is a hard link
9076ced83f9bSceastha 		 * (i.e., st_nlink > 1).
9077ced83f9bSceastha 		 *
90787c478bd9Sstevel@tonic-gate 		 * Note that if we are not chasing symlinks, and this one is a
90797c478bd9Sstevel@tonic-gate 		 * symlink, it is identically the one we saw before (you cannot
90807c478bd9Sstevel@tonic-gate 		 * have hard links to symlinks); in this case, we leave the
90817c478bd9Sstevel@tonic-gate 		 * count alone, so that we don't wind up archiving a symlink to
90827c478bd9Sstevel@tonic-gate 		 * itself.
90837c478bd9Sstevel@tonic-gate 		 */
90847c478bd9Sstevel@tonic-gate 
9085ced83f9bSceastha 		if (((Args & OCL) || (! isSymlink)) && !is_sysattr) {
90867c478bd9Sstevel@tonic-gate 			p->sl_count++;
90877c478bd9Sstevel@tonic-gate 		}
90887c478bd9Sstevel@tonic-gate 	} else {
90897c478bd9Sstevel@tonic-gate 		/* We have not seen this file before */
90907c478bd9Sstevel@tonic-gate 
90917c478bd9Sstevel@tonic-gate 		p->sl_count = 1;
90927c478bd9Sstevel@tonic-gate 
90937c478bd9Sstevel@tonic-gate 		if (Use_old_stat) {
90947c478bd9Sstevel@tonic-gate 			/* -Hodc: remap inode (-1 on overflow) */
90957c478bd9Sstevel@tonic-gate 
90967c478bd9Sstevel@tonic-gate 			sl_remap_t  *q;
90977c478bd9Sstevel@tonic-gate 
90987c478bd9Sstevel@tonic-gate 			for (q = sl_remap_head; q && (q->dev != device);
90997c478bd9Sstevel@tonic-gate 			    q = q->next) {
91007c478bd9Sstevel@tonic-gate 				/* do nothing */
91017c478bd9Sstevel@tonic-gate 			}
91027c478bd9Sstevel@tonic-gate 
91037c478bd9Sstevel@tonic-gate 			if (q == NULL) {
91047c478bd9Sstevel@tonic-gate 				q = e_zalloc(E_EXIT, sizeof (sl_remap_t));
91057c478bd9Sstevel@tonic-gate 				q->dev = device;
91067c478bd9Sstevel@tonic-gate 				p->sl_ino2 = q->inode_count = 1;
91077c478bd9Sstevel@tonic-gate 
91087c478bd9Sstevel@tonic-gate 				q->next = (sl_remap_head) ?
91097c478bd9Sstevel@tonic-gate 				    sl_remap_head->next : NULL;
91107c478bd9Sstevel@tonic-gate 				sl_remap_head = q;
91117c478bd9Sstevel@tonic-gate 			} else {
91127c478bd9Sstevel@tonic-gate 				if ((size_t)q->inode_count <=
91137c478bd9Sstevel@tonic-gate 				    ((1 << (sizeof (o_ino_t) * 8)) - 1)) {
91147c478bd9Sstevel@tonic-gate 					/* fits in o_ino_t */
91157c478bd9Sstevel@tonic-gate 					p->sl_ino2 = ++(q->inode_count);
91167c478bd9Sstevel@tonic-gate 				} else {
91177c478bd9Sstevel@tonic-gate 					p->sl_ino2 = (ino_t)-1;
91187c478bd9Sstevel@tonic-gate 				}
91197c478bd9Sstevel@tonic-gate 			}
91207c478bd9Sstevel@tonic-gate 		}
91217c478bd9Sstevel@tonic-gate 	}
91227c478bd9Sstevel@tonic-gate }
91237c478bd9Sstevel@tonic-gate 
91247c478bd9Sstevel@tonic-gate /*
91257c478bd9Sstevel@tonic-gate  * A faster search, which does not insert the key values into the tree.
91267c478bd9Sstevel@tonic-gate  * If the a match was found in the tree, return a pointer to it.  If it was not
91277c478bd9Sstevel@tonic-gate  * found, return NULL.
91287c478bd9Sstevel@tonic-gate  */
91297c478bd9Sstevel@tonic-gate 
91307c478bd9Sstevel@tonic-gate sl_info_t *
9131ced83f9bSceastha sl_search(dev_t device, ino_t inode, int ftype)
91327c478bd9Sstevel@tonic-gate {
91337c478bd9Sstevel@tonic-gate 	sl_info_t *p;		/* moves down the tree */
91347c478bd9Sstevel@tonic-gate 	int c;			/* comparison value */
91357c478bd9Sstevel@tonic-gate 	sl_info_t *retval = NULL; /* return value */
91367c478bd9Sstevel@tonic-gate 	sl_info_t *head;
91377c478bd9Sstevel@tonic-gate 
91387c478bd9Sstevel@tonic-gate 	head = sl_devhash_lookup(device);
91397c478bd9Sstevel@tonic-gate 	if (head != NULL) {
91407c478bd9Sstevel@tonic-gate 		for (p = head->rlink; p; ) {
9141ced83f9bSceastha 			if ((c = sl_compare(inode, ftype, p->sl_ino,
9142ced83f9bSceastha 			    p->sl_ftype)) == 0) {
91437c478bd9Sstevel@tonic-gate 				retval = p;
91447c478bd9Sstevel@tonic-gate 				break;
91457c478bd9Sstevel@tonic-gate 			} else if (c < 0) {
91467c478bd9Sstevel@tonic-gate 				p = p->llink;
91477c478bd9Sstevel@tonic-gate 			} else {
91487c478bd9Sstevel@tonic-gate 				p = p->rlink;
91497c478bd9Sstevel@tonic-gate 			}
91507c478bd9Sstevel@tonic-gate 		}
91517c478bd9Sstevel@tonic-gate 	}
91527c478bd9Sstevel@tonic-gate 
91537c478bd9Sstevel@tonic-gate 	return (retval);
91547c478bd9Sstevel@tonic-gate }
91557c478bd9Sstevel@tonic-gate 
91567c478bd9Sstevel@tonic-gate static sl_info_t *
91577c478bd9Sstevel@tonic-gate sl_devhash_lookup(dev_t device)
91587c478bd9Sstevel@tonic-gate {
91597c478bd9Sstevel@tonic-gate 	int key;
91607c478bd9Sstevel@tonic-gate 	sl_info_link_t *lp;
91617c478bd9Sstevel@tonic-gate 	static sl_info_link_t *devcache;
91627c478bd9Sstevel@tonic-gate 
91637c478bd9Sstevel@tonic-gate 	if (devcache != NULL && devcache->dev == device) {
91647c478bd9Sstevel@tonic-gate 		return (devcache->head);
91657c478bd9Sstevel@tonic-gate 	}
91667c478bd9Sstevel@tonic-gate 
91677c478bd9Sstevel@tonic-gate 	key = DEV_HASHKEY(device);
91687c478bd9Sstevel@tonic-gate 	for (lp = sl_devhash[key]; lp; lp = lp->next) {
91697c478bd9Sstevel@tonic-gate 		if (lp->dev == device) {
91707c478bd9Sstevel@tonic-gate 			devcache = lp;
91717c478bd9Sstevel@tonic-gate 			return (lp->head);
91727c478bd9Sstevel@tonic-gate 		}
91737c478bd9Sstevel@tonic-gate 	}
91747c478bd9Sstevel@tonic-gate 	return (NULL);
91757c478bd9Sstevel@tonic-gate }
91767c478bd9Sstevel@tonic-gate 
91777c478bd9Sstevel@tonic-gate static void
91787c478bd9Sstevel@tonic-gate sl_devhash_insert(dev_t device, sl_info_t *head)
91797c478bd9Sstevel@tonic-gate {
91807c478bd9Sstevel@tonic-gate 	int key = DEV_HASHKEY(device);
91817c478bd9Sstevel@tonic-gate 	sl_info_link_t *lp;
91827c478bd9Sstevel@tonic-gate 
91837c478bd9Sstevel@tonic-gate 	lp = e_zalloc(E_EXIT, sizeof (sl_info_link_t));
91847c478bd9Sstevel@tonic-gate 	lp->dev = device;
91857c478bd9Sstevel@tonic-gate 	lp->head = head;
91867c478bd9Sstevel@tonic-gate 	lp->next = sl_devhash[key];
91877c478bd9Sstevel@tonic-gate 	sl_devhash[key] = lp;
91887c478bd9Sstevel@tonic-gate }
91897c478bd9Sstevel@tonic-gate 
91907c478bd9Sstevel@tonic-gate static void
91917c478bd9Sstevel@tonic-gate chop_endslashes(char *path)
91927c478bd9Sstevel@tonic-gate {
91937c478bd9Sstevel@tonic-gate 	char *end, *ptr;
91947c478bd9Sstevel@tonic-gate 
91957c478bd9Sstevel@tonic-gate 	end = &path[strlen(path) -1];
91967c478bd9Sstevel@tonic-gate 	if (*end == '/' && end != path) {
91977c478bd9Sstevel@tonic-gate 		ptr = skipslashes(end, path);
91987c478bd9Sstevel@tonic-gate 		if (ptr != NULL && ptr != path) {
91997c478bd9Sstevel@tonic-gate 			*ptr = '\0';
92007c478bd9Sstevel@tonic-gate 		}
92017c478bd9Sstevel@tonic-gate 	}
92027c478bd9Sstevel@tonic-gate }
92037c478bd9Sstevel@tonic-gate 
92047c478bd9Sstevel@tonic-gate #if !defined(O_XATTR)
92057c478bd9Sstevel@tonic-gate int
92067c478bd9Sstevel@tonic-gate openat64(int fd, char *name, int oflag, mode_t cmode)
92077c478bd9Sstevel@tonic-gate {
92087c478bd9Sstevel@tonic-gate 	return (open64(name, oflag, cmode));
92097c478bd9Sstevel@tonic-gate }
92107c478bd9Sstevel@tonic-gate 
92117c478bd9Sstevel@tonic-gate int
92127c478bd9Sstevel@tonic-gate openat(int fd, char *name, int oflag, mode_t cmode)
92137c478bd9Sstevel@tonic-gate {
92147c478bd9Sstevel@tonic-gate 	return (open(name, oflag, cmode));
92157c478bd9Sstevel@tonic-gate }
92167c478bd9Sstevel@tonic-gate 
92177c478bd9Sstevel@tonic-gate int
92187c478bd9Sstevel@tonic-gate fchownat(int fd, char *name, uid_t owner, gid_t group, int flag)
92197c478bd9Sstevel@tonic-gate {
92207c478bd9Sstevel@tonic-gate 	if (flag == AT_SYMLINK_NOFOLLOW)
92217c478bd9Sstevel@tonic-gate 		return (lchown(name, owner, group));
92227c478bd9Sstevel@tonic-gate 	else
92237c478bd9Sstevel@tonic-gate 		return (chown(name, owner, group));
92247c478bd9Sstevel@tonic-gate }
92257c478bd9Sstevel@tonic-gate 
92267c478bd9Sstevel@tonic-gate int
92277c478bd9Sstevel@tonic-gate renameat(int fromfd, char *old, int tofd, char *new)
92287c478bd9Sstevel@tonic-gate {
92297c478bd9Sstevel@tonic-gate 	return (rename(old, new));
92307c478bd9Sstevel@tonic-gate }
92317c478bd9Sstevel@tonic-gate 
92327c478bd9Sstevel@tonic-gate int
92337c478bd9Sstevel@tonic-gate futimesat(int fd, char *path, struct timeval times[2])
92347c478bd9Sstevel@tonic-gate {
92357c478bd9Sstevel@tonic-gate 	return (utimes(path, times));
92367c478bd9Sstevel@tonic-gate }
92377c478bd9Sstevel@tonic-gate 
92387c478bd9Sstevel@tonic-gate int
92397c478bd9Sstevel@tonic-gate unlinkat(int dirfd, char *path, int flag)
92407c478bd9Sstevel@tonic-gate {
92417c478bd9Sstevel@tonic-gate 	if (flag == AT_REMOVEDIR) {
92427c478bd9Sstevel@tonic-gate 		return (rmdir(path));
92437c478bd9Sstevel@tonic-gate 	} else {
92447c478bd9Sstevel@tonic-gate 		return (unlink(path));
92457c478bd9Sstevel@tonic-gate 	}
92467c478bd9Sstevel@tonic-gate }
92477c478bd9Sstevel@tonic-gate 
92487c478bd9Sstevel@tonic-gate int
92497c478bd9Sstevel@tonic-gate fstatat(int fd, char *path, struct stat *buf, int flag)
92507c478bd9Sstevel@tonic-gate {
92517c478bd9Sstevel@tonic-gate 	if (flag == AT_SYMLINK_NOFOLLOW)
92527c478bd9Sstevel@tonic-gate 		return (lstat(path, buf));
92537c478bd9Sstevel@tonic-gate 	else
92547c478bd9Sstevel@tonic-gate 		return (stat(path, buf));
92557c478bd9Sstevel@tonic-gate }
92567c478bd9Sstevel@tonic-gate 
92577c478bd9Sstevel@tonic-gate int
92587c478bd9Sstevel@tonic-gate attropen(char *file, char *attr, int omode, mode_t cmode)
92597c478bd9Sstevel@tonic-gate {
92607c478bd9Sstevel@tonic-gate 	errno = ENOTSUP;
92617c478bd9Sstevel@tonic-gate 	return (-1);
92627c478bd9Sstevel@tonic-gate }
92637c478bd9Sstevel@tonic-gate #endif
9264