xref: /titanic_52/usr/src/cmd/cpio/cpio.c (revision da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0)
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 /*
223a3e214eSceastha  * Copyright 2007 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>
69647ab8f4Sceastha #ifdef SOLARIS_PRIVS
70647ab8f4Sceastha #include <priv.h>
71647ab8f4Sceastha #endif	/* SOLARIS_PRIVS */
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * Special kludge for off_t being a signed quantity.
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate #if _FILE_OFFSET_BITS == 64
777c478bd9Sstevel@tonic-gate typedef	u_longlong_t	u_off_t;
787c478bd9Sstevel@tonic-gate #else
797c478bd9Sstevel@tonic-gate typedef	ulong_t		u_off_t;
807c478bd9Sstevel@tonic-gate #endif
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #define	SECMODE	0xe080
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #define	DEVNULL		"/dev/null"
857c478bd9Sstevel@tonic-gate #define	XATTRHDR	".hdr"
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #define	NAMELEN		32
887c478bd9Sstevel@tonic-gate #define	TYPELEN 	16
897c478bd9Sstevel@tonic-gate #define	PERMLEN		4
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate #define	FILE_COPIED	1
927c478bd9Sstevel@tonic-gate #define	FILE_LINKED	2
937c478bd9Sstevel@tonic-gate #define	FILE_PASS_ERR	-1
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate #define	ARCHIVE_NORMAL	0
967c478bd9Sstevel@tonic-gate #define	ARCHIVE_ACL	1
977c478bd9Sstevel@tonic-gate #define	ARCHIVE_XATTR	2
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate #define	LSTAT(dir, path, statbuf) fstatat(dir, \
1007c478bd9Sstevel@tonic-gate     get_component((Gen.g_attrnam_p == (char *)NULL) ? \
1017c478bd9Sstevel@tonic-gate     path : Gen.g_attrnam_p), statbuf, AT_SYMLINK_NOFOLLOW)
1027c478bd9Sstevel@tonic-gate #define	STAT(dir, path, statbuf) fstatat(dir, \
1037c478bd9Sstevel@tonic-gate     get_component((Gen.g_attrnam_p == (char *)NULL) ? \
1047c478bd9Sstevel@tonic-gate     path : Gen.g_attrnam_p), statbuf, 0)
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  *	These limits reflect the maximum size regular file that
1087c478bd9Sstevel@tonic-gate  *	can be archived, depending on the archive type. For archives
1097c478bd9Sstevel@tonic-gate  *	with character-format headers (odc, tar, ustar) we use
110944d83a0Schin  *	CHAR_OFFSET_MAX.  For archives with SVR4 ASCII headers (-c, -H crc)
111944d83a0Schin  *	we store filesize in an 8-char hexadecimal string and use
112944d83a0Schin  *	ASC_OFFSET_MAX.  Otherwise, we are limited to the size that will
113944d83a0Schin  *	fit in a signed long value.
1147c478bd9Sstevel@tonic-gate  */
115944d83a0Schin #define	CHAR_OFFSET_MAX	077777777777ULL	/* 11 octal digits */
116944d83a0Schin #define	ASC_OFFSET_MAX	0XFFFFFFFF	/* 8 hexadecimal digits */
1177c478bd9Sstevel@tonic-gate #define	BIN_OFFSET_MAX	LONG_MAX	/* signed long max value */
1187c478bd9Sstevel@tonic-gate 
119d2443e76Smarks #define	POSIXMODES	07777
120d2443e76Smarks 
1217c478bd9Sstevel@tonic-gate static char	aclchar = ' ';
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate static struct Lnk *add_lnk(struct Lnk **);
1247c478bd9Sstevel@tonic-gate static int bfill(void);
1257c478bd9Sstevel@tonic-gate static void bflush(void);
1267c478bd9Sstevel@tonic-gate static int chgreel(int dir);
1277c478bd9Sstevel@tonic-gate static int ckname(int);
1287c478bd9Sstevel@tonic-gate static void ckopts(long mask);
1297c478bd9Sstevel@tonic-gate static long cksum(char hdr, int byt_cnt, int *err);
1307c478bd9Sstevel@tonic-gate static int creat_hdr(void);
1317c478bd9Sstevel@tonic-gate static int creat_lnk(int dirfd, char *name1_p, char *name2_p);
1327c478bd9Sstevel@tonic-gate static int creat_spec(int dirfd);
1337c478bd9Sstevel@tonic-gate static int creat_tmp(char *nam_p);
1347c478bd9Sstevel@tonic-gate static void data_in(int proc_mode);
1357c478bd9Sstevel@tonic-gate static void data_out(void);
1367c478bd9Sstevel@tonic-gate static void data_pass(void);
1377c478bd9Sstevel@tonic-gate static void file_in(void);
1387c478bd9Sstevel@tonic-gate static int file_out(void);
1397c478bd9Sstevel@tonic-gate static int file_pass(void);
1407c478bd9Sstevel@tonic-gate static void flush_lnks(void);
1417c478bd9Sstevel@tonic-gate static int gethdr(void);
1427c478bd9Sstevel@tonic-gate static int getname(void);
1437c478bd9Sstevel@tonic-gate static void getpats(int largc, char **largv);
1447c478bd9Sstevel@tonic-gate static void ioerror(int dir);
1457c478bd9Sstevel@tonic-gate static int matched(void);
1467c478bd9Sstevel@tonic-gate static int missdir(char *nam_p);
1477c478bd9Sstevel@tonic-gate static long mklong(short v[]);
1487c478bd9Sstevel@tonic-gate static void mkshort(short sval[], long v);
1497c478bd9Sstevel@tonic-gate static void msg(int severity, const char *fmt, ...);
1507c478bd9Sstevel@tonic-gate static int openout(int dirfd);
1517c478bd9Sstevel@tonic-gate static int read_hdr(int hdr);
1527c478bd9Sstevel@tonic-gate static void reclaim(struct Lnk *l_p);
1537c478bd9Sstevel@tonic-gate static void rstbuf(void);
1547c478bd9Sstevel@tonic-gate static void setpasswd(char *nam);
1557c478bd9Sstevel@tonic-gate static void rstfiles(int over, int dirfd);
1567c478bd9Sstevel@tonic-gate static void scan4trail(void);
1577c478bd9Sstevel@tonic-gate static void setup(int largc, char **largv);
1587c478bd9Sstevel@tonic-gate static void set_tym(int dirfd, char *nam_p, time_t atime, time_t mtime);
1597c478bd9Sstevel@tonic-gate static void sigint(int sig);
1607c478bd9Sstevel@tonic-gate static void swap(char *buf_p, int cnt);
1617c478bd9Sstevel@tonic-gate static void usage(void);
1627c478bd9Sstevel@tonic-gate static void verbose(char *nam_p);
1637c478bd9Sstevel@tonic-gate static void write_hdr(int secflag, off_t len);
1647c478bd9Sstevel@tonic-gate static void write_trail(void);
1657c478bd9Sstevel@tonic-gate static int ustar_dir(void);
1667c478bd9Sstevel@tonic-gate static int ustar_spec(void);
1677c478bd9Sstevel@tonic-gate static struct stat *convert_to_old_stat(struct stat *, char *, char *);
1687c478bd9Sstevel@tonic-gate static void read_bar_vol_hdr(void);
1697c478bd9Sstevel@tonic-gate static void read_bar_file_hdr(void);
1707c478bd9Sstevel@tonic-gate static void setup_uncompress(FILE **);
1717c478bd9Sstevel@tonic-gate static void skip_bar_volhdr(void);
1727c478bd9Sstevel@tonic-gate static void bar_file_in(void);
1737c478bd9Sstevel@tonic-gate static int g_init(int *devtype, int *fdes);
1747c478bd9Sstevel@tonic-gate static int g_read(int, int, char *, unsigned);
1757c478bd9Sstevel@tonic-gate static int g_write(int, int, char *, unsigned);
1767c478bd9Sstevel@tonic-gate static int is_floppy(int);
1777c478bd9Sstevel@tonic-gate static int is_tape(int);
1787c478bd9Sstevel@tonic-gate static void write_ancillary(char *secinfo, int len);
1797c478bd9Sstevel@tonic-gate static int remove_dir(char *);
1807c478bd9Sstevel@tonic-gate static int save_cwd(void);
1817c478bd9Sstevel@tonic-gate static void rest_cwd(int cwd);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate static void xattrs_out(int (*func)());
1847c478bd9Sstevel@tonic-gate static void get_parent(char *path, char *dir);
1857c478bd9Sstevel@tonic-gate static void prepare_xattr_hdr(char **attrbuf, char *filename,
1867c478bd9Sstevel@tonic-gate     char *attrname, char typeflag, struct Lnk *linkinfo, int *rlen);
1877c478bd9Sstevel@tonic-gate static char tartype(int type);
1887c478bd9Sstevel@tonic-gate static int openfile(int omode);
1897c478bd9Sstevel@tonic-gate static mode_t attrmode(char type);
1907c478bd9Sstevel@tonic-gate static char *get_component(char *path);
1917c478bd9Sstevel@tonic-gate static int open_dir(char *name);
1927c478bd9Sstevel@tonic-gate static int open_dirfd();
1937c478bd9Sstevel@tonic-gate static void close_dirfd();
1947c478bd9Sstevel@tonic-gate static void write_xattr_hdr();
1957c478bd9Sstevel@tonic-gate static int retry_attrdir_open(char *name);
1967c478bd9Sstevel@tonic-gate static char *skipslashes(char *string, char *start);
1977c478bd9Sstevel@tonic-gate static int read_xattr_hdr();
1987c478bd9Sstevel@tonic-gate static void chop_endslashes(char *path);
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate /* helpful types */
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate static
2047c478bd9Sstevel@tonic-gate struct passwd	*Curpw_p,	/* Current password entry for -t option */
2057c478bd9Sstevel@tonic-gate 		*Rpw_p,		/* Password entry for -R option */
2067c478bd9Sstevel@tonic-gate 		*dpasswd;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate static
2097c478bd9Sstevel@tonic-gate struct group	*Curgr_p,	/* Current group entry for -t option */
2107c478bd9Sstevel@tonic-gate 		*dgroup;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /* Data structure for buffered I/O. */
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate static
2157c478bd9Sstevel@tonic-gate struct buf_info {
2167c478bd9Sstevel@tonic-gate 	char	*b_base_p,	/* Pointer to base of buffer */
2177c478bd9Sstevel@tonic-gate 		*b_out_p,	/* Position to take bytes from buffer at */
2187c478bd9Sstevel@tonic-gate 		*b_in_p,	/* Position to put bytes into buffer at */
2197c478bd9Sstevel@tonic-gate 		*b_end_p;	/* Pointer to end of buffer */
2207c478bd9Sstevel@tonic-gate 	long	b_cnt,		/* Count of unprocessed bytes */
2217c478bd9Sstevel@tonic-gate 		b_size;		/* Size of buffer in bytes */
2227c478bd9Sstevel@tonic-gate } Buffr;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /* Generic header format */
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate static
2277c478bd9Sstevel@tonic-gate struct gen_hdr {
2287c478bd9Sstevel@tonic-gate 	ulong_t	g_magic,	/* Magic number field */
2297c478bd9Sstevel@tonic-gate 		g_ino,		/* Inode number of file */
2307c478bd9Sstevel@tonic-gate 		g_mode,		/* Mode of file */
2317c478bd9Sstevel@tonic-gate 		g_uid,		/* Uid of file */
2327c478bd9Sstevel@tonic-gate 		g_gid,		/* Gid of file */
2337c478bd9Sstevel@tonic-gate 		g_nlink,	/* Number of links */
2347c478bd9Sstevel@tonic-gate 		g_mtime;	/* Modification time */
2357c478bd9Sstevel@tonic-gate 	off_t	g_filesz;	/* Length of file */
2367c478bd9Sstevel@tonic-gate 	ulong_t	g_dev,		/* File system of file */
2377c478bd9Sstevel@tonic-gate 		g_rdev,		/* Major/minor numbers of special files */
2387c478bd9Sstevel@tonic-gate 		g_namesz,	/* Length of filename */
2397c478bd9Sstevel@tonic-gate 		g_cksum;	/* Checksum of file */
2407c478bd9Sstevel@tonic-gate 	char	g_gname[32],
2417c478bd9Sstevel@tonic-gate 		g_uname[32],
2427c478bd9Sstevel@tonic-gate 		g_version[2],
2437c478bd9Sstevel@tonic-gate 		g_tmagic[6],
2447c478bd9Sstevel@tonic-gate 		g_typeflag;
2457c478bd9Sstevel@tonic-gate 	char	*g_tname,
2467c478bd9Sstevel@tonic-gate 		*g_prefix,
2477c478bd9Sstevel@tonic-gate 		*g_nam_p,	/* Filename */
2487c478bd9Sstevel@tonic-gate 		*g_attrnam_p,	/* attribute */
2497c478bd9Sstevel@tonic-gate 		*g_attrfnam_p,  /* Real file name attr belongs to */
2507c478bd9Sstevel@tonic-gate 		*g_linktoattrfnam_p, /* file linked attribute belongs to */
2517c478bd9Sstevel@tonic-gate 		*g_linktoattrnam_p,  /* attribute g_attrnam_p is linked to */
2527c478bd9Sstevel@tonic-gate 		*g_dirpath;	/* dirname currently opened */
2537c478bd9Sstevel@tonic-gate 	int	g_dirfd;	/* directory file descriptor */
2547c478bd9Sstevel@tonic-gate 	int	g_passdirfd;	/* directory fd to pass to */
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate } Gen, *G_p;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate /* Data structure for handling multiply-linked files */
2597c478bd9Sstevel@tonic-gate static
2607c478bd9Sstevel@tonic-gate char	prebuf[PRESIZ+1],
2617c478bd9Sstevel@tonic-gate 	nambuf[NAMSIZ+1],
2627c478bd9Sstevel@tonic-gate 	fullnam[MAXNAM+1];
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate static
2667c478bd9Sstevel@tonic-gate struct Lnk {
2677c478bd9Sstevel@tonic-gate 	short	L_cnt,		/* Number of links encountered */
2687c478bd9Sstevel@tonic-gate 		L_data;		/* Data has been encountered if 1 */
2697c478bd9Sstevel@tonic-gate 	struct gen_hdr	L_gen;	/* gen_hdr information for this file */
2707c478bd9Sstevel@tonic-gate 	struct Lnk	*L_nxt_p,	/* Next file in list */
2717c478bd9Sstevel@tonic-gate 			*L_bck_p,	/* Previous file in list */
2727c478bd9Sstevel@tonic-gate 			*L_lnk_p;	/* Next link for this file */
2737c478bd9Sstevel@tonic-gate } Lnk_hd;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate static
2767c478bd9Sstevel@tonic-gate struct hdr_cpio	Hdr;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate /*
2797c478bd9Sstevel@tonic-gate  * -------------------------------------------------------------------------
2807c478bd9Sstevel@tonic-gate  *		   Stuff needed to pre-view the name stream
2817c478bd9Sstevel@tonic-gate  *
2827c478bd9Sstevel@tonic-gate  * issymlink is used to remember that the current file is a symlink between
2837c478bd9Sstevel@tonic-gate  * getname() and file_pass(); the former trashes this information immediately
2847c478bd9Sstevel@tonic-gate  * when -L is specified.
2857c478bd9Sstevel@tonic-gate  */
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate static
2887c478bd9Sstevel@tonic-gate int	issymlink = 0;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate static
2917c478bd9Sstevel@tonic-gate FILE	*In_p = stdin;		/* Where the input comes from */
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate typedef struct sl_info
2947c478bd9Sstevel@tonic-gate {
2957c478bd9Sstevel@tonic-gate 	struct sl_info *llink;	/* Left subtree ptr (tree depth in *sl_head) */
2967c478bd9Sstevel@tonic-gate 	struct sl_info *rlink;	/* Right subtree ptr */
2977c478bd9Sstevel@tonic-gate 	int bal;		/* Subtree balance factor */
2987c478bd9Sstevel@tonic-gate 	ulong_t	sl_count;	/* Number of symlinks */
2997c478bd9Sstevel@tonic-gate 	ino_t	sl_ino;		/* Inode of file */
3007c478bd9Sstevel@tonic-gate 	ino_t	sl_ino2;	/* alternate inode for -Hodc */
3017c478bd9Sstevel@tonic-gate } sl_info_t;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate /*
3047c478bd9Sstevel@tonic-gate  * The following structure maintains a hash entry for the
3057c478bd9Sstevel@tonic-gate  * balancing trees which are allocated for each device nodes.
3067c478bd9Sstevel@tonic-gate  */
3077c478bd9Sstevel@tonic-gate typedef struct sl_info_link
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate 	dev_t		dev;
3107c478bd9Sstevel@tonic-gate 	sl_info_t	*head;
3117c478bd9Sstevel@tonic-gate 	struct sl_info_link *next;
3127c478bd9Sstevel@tonic-gate } sl_info_link_t;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate #define	SL_INFO_ALLOC_CHUNK	1024
3157c478bd9Sstevel@tonic-gate #define	NDEVHENTRY		0x40
3167c478bd9Sstevel@tonic-gate #define	DEV_HASHKEY(x)		((x) & (NDEVHENTRY -1))
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate /*
3197c478bd9Sstevel@tonic-gate  * For remapping dev,inode for -Hodc archives.
3207c478bd9Sstevel@tonic-gate  */
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate typedef struct sl_remap
3237c478bd9Sstevel@tonic-gate {
3247c478bd9Sstevel@tonic-gate 	dev_t			dev;		/* device */
3257c478bd9Sstevel@tonic-gate 	int			inode_count;	/* # inodes seen on dev */
3267c478bd9Sstevel@tonic-gate 	struct sl_remap 	*next;		/* next in the chain */
3277c478bd9Sstevel@tonic-gate } sl_remap_t;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate /* forward declarations */
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate static sl_info_t 	*sl_info_alloc(void);
3327c478bd9Sstevel@tonic-gate static sl_info_t 	*sl_insert(dev_t, ino_t);
3337c478bd9Sstevel@tonic-gate static ulong_t		sl_numlinks(dev_t, ino_t);
3347c478bd9Sstevel@tonic-gate static void		sl_preview_synonyms(void);
3357c478bd9Sstevel@tonic-gate static void		sl_remember_tgt(const struct stat *, int);
3367c478bd9Sstevel@tonic-gate static sl_info_t 	*sl_search(dev_t, ino_t);
3377c478bd9Sstevel@tonic-gate static sl_info_t	*sl_devhash_lookup(dev_t);
3387c478bd9Sstevel@tonic-gate static void		sl_devhash_insert(dev_t, sl_info_t *);
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate extern int		sl_compare(ino_t, ino_t);
3417c478bd9Sstevel@tonic-gate #define	sl_compare(lino, rino)	(lino < rino ? -1 : (lino > rino ? 1 : 0))
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate /* global storage */
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate static sl_remap_t  *sl_remap_head = NULL; /* head of the inode-remap list */
3467c478bd9Sstevel@tonic-gate static sl_info_link_t	*sl_devhash[NDEVHENTRY]; /* hash table */
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate /*
3497c478bd9Sstevel@tonic-gate  * -------------------------------------------------------------------------
3507c478bd9Sstevel@tonic-gate  */
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate static
3537c478bd9Sstevel@tonic-gate struct stat	ArchSt,	/* stat(2) information of the archive */
3547c478bd9Sstevel@tonic-gate 		SrcSt,	/* stat(2) information of source file */
3557c478bd9Sstevel@tonic-gate 		DesSt,	/* stat(2) of destination file */
3567c478bd9Sstevel@tonic-gate 		*OldSt = NULL;	/* stat info converted to svr32 format */
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate /*
3597c478bd9Sstevel@tonic-gate  * bin_mag: Used to validate a binary magic number,
3607c478bd9Sstevel@tonic-gate  * by combining to bytes into an unsigned short.
3617c478bd9Sstevel@tonic-gate  */
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate static
3647c478bd9Sstevel@tonic-gate union bin_mag {
3657c478bd9Sstevel@tonic-gate 	unsigned char b_byte[2];
3667c478bd9Sstevel@tonic-gate 	ushort_t b_half;
3677c478bd9Sstevel@tonic-gate } Binmag;
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate static
3707c478bd9Sstevel@tonic-gate union tblock *Thdr_p;	/* TAR header pointer */
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate static union b_block *bar_Vhdr;
3737c478bd9Sstevel@tonic-gate static struct gen_hdr Gen_bar_vol;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate /*
3767c478bd9Sstevel@tonic-gate  * swpbuf: Used in swap() to swap bytes within a halfword,
3777c478bd9Sstevel@tonic-gate  * halfwords within a word, or to reverse the order of the
3787c478bd9Sstevel@tonic-gate  * bytes within a word.  Also used in mklong() and mkshort().
3797c478bd9Sstevel@tonic-gate  */
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate static
3827c478bd9Sstevel@tonic-gate union swpbuf {
3837c478bd9Sstevel@tonic-gate 	unsigned char	s_byte[4];
3847c478bd9Sstevel@tonic-gate 	ushort_t	s_half[2];
3857c478bd9Sstevel@tonic-gate 	ulong_t	s_word;
3867c478bd9Sstevel@tonic-gate } *Swp_p;
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate static
3897c478bd9Sstevel@tonic-gate char	Adir,			/* Flags object as a directory */
3907c478bd9Sstevel@tonic-gate 	Hiddendir,		/* Processing hidden attribute directory */
3917c478bd9Sstevel@tonic-gate 	Aspec,			/* Flags object as a special file */
3927c478bd9Sstevel@tonic-gate 	Do_rename,		/* Indicates rename() is to be used */
3937c478bd9Sstevel@tonic-gate 	Time[50],		/* Array to hold date and time */
3947c478bd9Sstevel@tonic-gate 	Ttyname[] = "/dev/tty",	/* Controlling console */
3957c478bd9Sstevel@tonic-gate 	T_lname[MAXPATHLEN],	/* Array to hold links name for tar */
3967c478bd9Sstevel@tonic-gate 	*Buf_p,			/* Buffer for file system I/O */
3977c478bd9Sstevel@tonic-gate 	*Empty,			/* Empty block for TARTYP headers */
3987c478bd9Sstevel@tonic-gate 	*Full_p,		/* Pointer to full pathname */
3997c478bd9Sstevel@tonic-gate 	*Efil_p,		/* -E pattern file string */
4007c478bd9Sstevel@tonic-gate 	*Eom_p = "Change to part %d and press RETURN key. [q] ",
4017c478bd9Sstevel@tonic-gate 	*Fullnam_p,		/* Full pathname */
4027c478bd9Sstevel@tonic-gate 	*Attrfile_p,		/* attribute file */
4037c478bd9Sstevel@tonic-gate 	*Hdr_p,			/* -H header type string */
4047c478bd9Sstevel@tonic-gate 	*IOfil_p,		/* -I/-O input/output archive string */
4057c478bd9Sstevel@tonic-gate 	*Lnkend_p,		/* Pointer to end of Lnknam_p */
4067c478bd9Sstevel@tonic-gate 	*Lnknam_p,		/* Buffer for linking files with -p option */
4077c478bd9Sstevel@tonic-gate 	*Nam_p,			/* Array to hold filename */
4087c478bd9Sstevel@tonic-gate 	*Savenam_p,		/* copy of filename xattr belongs to */
4097c478bd9Sstevel@tonic-gate 	*Own_p,			/* New owner login id string */
4107c478bd9Sstevel@tonic-gate 	*Renam_p,		/* Buffer for renaming files */
4117c478bd9Sstevel@tonic-gate 	*Renametmp_p,		/* Tmp Buffer for renaming files */
4127c478bd9Sstevel@tonic-gate 	*Symlnk_p,		/* Buffer for holding symbolic link name */
4137c478bd9Sstevel@tonic-gate 	*Over_p,		/* Holds temporary filename when overwriting */
4147c478bd9Sstevel@tonic-gate 	**Pat_pp = 0,		/* Pattern strings */
4157c478bd9Sstevel@tonic-gate 	bar_linkflag,		/* flag to indicate if the file is a link */
4167c478bd9Sstevel@tonic-gate 	bar_linkname[MAXPATHLEN]; /* store the name of the link */
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate static
4197c478bd9Sstevel@tonic-gate int	Append = 0,	/* Flag set while searching to end of archive */
4207c478bd9Sstevel@tonic-gate 	Archive,	/* File descriptor of the archive */
421*da6c28aaSamw 	Buf_error = 0,	/* I/O error occurred during buffer fill */
4227c478bd9Sstevel@tonic-gate 	Def_mode = 0777,	/* Default file/directory protection modes */
4237c478bd9Sstevel@tonic-gate 	Device,		/* Device type being accessed (used with libgenIO) */
4247c478bd9Sstevel@tonic-gate 	Error_cnt = 0,	/* Cumulative count of I/O errors */
4257c478bd9Sstevel@tonic-gate 	Finished = 1,	/* Indicates that a file transfer has completed */
4267c478bd9Sstevel@tonic-gate 	Hdrsz = ASCSZ,	/* Fixed length portion of the header */
4277c478bd9Sstevel@tonic-gate 	Hdr_type,		/* Flag to indicate type of header selected */
4287c478bd9Sstevel@tonic-gate 	Ifile,		/* File des. of file being archived */
4297c478bd9Sstevel@tonic-gate 	Ofile,		/* File des. of file being extracted from archive */
4307c478bd9Sstevel@tonic-gate 	Use_old_stat = 0,    /* Create an old style -Hodc hdr (small dev's) */
4317c478bd9Sstevel@tonic-gate 	Onecopy = 0,	/* Flags old vs. new link handling */
4327c478bd9Sstevel@tonic-gate 	Pad_val = 0,	/* Indicates the number of bytes to pad (if any) */
4337c478bd9Sstevel@tonic-gate 	PageSize = 0,	/* The native page size, used for figuring block size */
4347c478bd9Sstevel@tonic-gate 	Volcnt = 1,	/* Number of archive volumes processed */
4357c478bd9Sstevel@tonic-gate 	Verbcnt = 0,	/* Count of number of dots '.' output */
4367c478bd9Sstevel@tonic-gate 	Eomflag = 0,
4377c478bd9Sstevel@tonic-gate 	Dflag = 0,
4387c478bd9Sstevel@tonic-gate 	Atflag = 0,	/* Archive/restore extended attributes */
4397c478bd9Sstevel@tonic-gate 	Compressed,	/* Flag to indicate if the bar archive is compressed */
440647ab8f4Sceastha 	Bar_vol_num = 0, /* Volume number count for bar archive */
441647ab8f4Sceastha 	privileged = 0;	/* Flag set if running with higher privileges */
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate static
445f48205beScasper gid_t	Lastgid = (gid_t)-1;	/* Used with -t & -v to record current gid */
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate static
448f48205beScasper uid_t	Lastuid = (uid_t)-1;	/* Used with -t & -v to record current uid */
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate static
4517c478bd9Sstevel@tonic-gate long	Args,		/* Mask of selected options */
4527c478bd9Sstevel@tonic-gate 	Max_namesz = CPATH;	/* Maximum size of pathnames/filenames */
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate static
4557c478bd9Sstevel@tonic-gate int	Bufsize = BUFSZ;	/* Default block size */
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate static u_longlong_t    Blocks;	/* full blocks transferred */
4597c478bd9Sstevel@tonic-gate static u_longlong_t    SBlocks;	/* cumulative char count from short reads */
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate static off_t	Max_offset = BIN_OFFSET_MAX;	/* largest file size */
4637c478bd9Sstevel@tonic-gate static off_t	Max_filesz;			/* from getrlimit */
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate static
4667c478bd9Sstevel@tonic-gate FILE	*Ef_p,			/* File pointer of pattern input file */
4677c478bd9Sstevel@tonic-gate 	*Err_p = stderr,	/* File pointer for error reporting */
4687c478bd9Sstevel@tonic-gate 	*Out_p = stdout,	/* File pointer for non-archive output */
4697c478bd9Sstevel@tonic-gate 	*Rtty_p,		/* Input file pointer for interactive rename */
4707c478bd9Sstevel@tonic-gate 	*Wtty_p;		/* Output file ptr for interactive rename */
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate static
4737c478bd9Sstevel@tonic-gate ushort_t	Ftype = S_IFMT;	/* File type mask */
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate /* ACL support */
4767c478bd9Sstevel@tonic-gate static struct sec_attr {
4777c478bd9Sstevel@tonic-gate 	char	attr_type;
4787c478bd9Sstevel@tonic-gate 	char	attr_len[7];
4797c478bd9Sstevel@tonic-gate 	char	attr_info[1];
4807c478bd9Sstevel@tonic-gate } *attr;
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate static int	Pflag = 0;	/* flag indicates that acl is preserved */
483fa9e4066Sahrens static int	acl_is_set = 0; /* True if an acl was set on the file */
484fa9e4066Sahrens 
485fa9e4066Sahrens acl_t *aclp;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate /*
4887c478bd9Sstevel@tonic-gate  *
4897c478bd9Sstevel@tonic-gate  * cpio has been changed to support extended attributes.
4907c478bd9Sstevel@tonic-gate  *
4917c478bd9Sstevel@tonic-gate  * As part of this change cpio has been changed to use the new *at() syscalls
4927c478bd9Sstevel@tonic-gate  * such as openat, fchownat(), unlinkat()...
4937c478bd9Sstevel@tonic-gate  *
4947c478bd9Sstevel@tonic-gate  * This was done so that attributes can be handled with as few code changes
4957c478bd9Sstevel@tonic-gate  * as possible.
4967c478bd9Sstevel@tonic-gate  *
4977c478bd9Sstevel@tonic-gate  * What this means is that cpio now opens the directory that a file or directory
4987c478bd9Sstevel@tonic-gate  * resides in and then performs *at() functions to manipulate the entry.
4997c478bd9Sstevel@tonic-gate  *
5007c478bd9Sstevel@tonic-gate  * For example a new file is now created like this:
5017c478bd9Sstevel@tonic-gate  *
5027c478bd9Sstevel@tonic-gate  * dfd = open(<some dir path>)
5037c478bd9Sstevel@tonic-gate  * fd = openat(dfd, <name>,....);
5047c478bd9Sstevel@tonic-gate  *
5057c478bd9Sstevel@tonic-gate  * or in the case of an extended attribute
5067c478bd9Sstevel@tonic-gate  *
5077c478bd9Sstevel@tonic-gate  * dfd = attropen(<pathname>, ".", ....)
5087c478bd9Sstevel@tonic-gate  *
5097c478bd9Sstevel@tonic-gate  * Once we have a directory file descriptor all of the *at() functions can
5107c478bd9Sstevel@tonic-gate  * be applied to it.
5117c478bd9Sstevel@tonic-gate  *
5127c478bd9Sstevel@tonic-gate  * unlinkat(dfd, <component name>,...)
5137c478bd9Sstevel@tonic-gate  * fchownat(dfd, <component name>,..)
5147c478bd9Sstevel@tonic-gate  *
5157c478bd9Sstevel@tonic-gate  * This works for both normal namespace files and extended attribute file
5167c478bd9Sstevel@tonic-gate  *
5177c478bd9Sstevel@tonic-gate  */
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate /*
5207c478bd9Sstevel@tonic-gate  * Extended attribute layout
5217c478bd9Sstevel@tonic-gate  *
5227c478bd9Sstevel@tonic-gate  * Extended attributes are stored in two pieces.
5237c478bd9Sstevel@tonic-gate  * 1. An attribute header which has information about
5247c478bd9Sstevel@tonic-gate  *    what file the attribute is for and what the attribute
5257c478bd9Sstevel@tonic-gate  *    is named.
5267c478bd9Sstevel@tonic-gate  * 2. The attribute record itself.  Stored as a normal file type
5277c478bd9Sstevel@tonic-gate  *    of entry.
5287c478bd9Sstevel@tonic-gate  * Both the header and attribute record have special modes/typeflags
5297c478bd9Sstevel@tonic-gate  * associated with them.
5307c478bd9Sstevel@tonic-gate  *
5317c478bd9Sstevel@tonic-gate  * The names of the header in the archive look like:
5327c478bd9Sstevel@tonic-gate  * /dev/null/attr.hdr
5337c478bd9Sstevel@tonic-gate  *
5347c478bd9Sstevel@tonic-gate  * The name of the attribute looks like:
5357c478bd9Sstevel@tonic-gate  * /dev/null/attr.
5367c478bd9Sstevel@tonic-gate  *
5377c478bd9Sstevel@tonic-gate  * This is done so that an archiver that doesn't understand these formats
5387c478bd9Sstevel@tonic-gate  * can just dispose of the attribute records unless the user chooses to
5397c478bd9Sstevel@tonic-gate  * rename them via cpio -r or pax -i
5407c478bd9Sstevel@tonic-gate  *
5417c478bd9Sstevel@tonic-gate  * The format is composed of a fixed size header followed
5427c478bd9Sstevel@tonic-gate  * by a variable sized xattr_buf. If the attribute is a hard link
5437c478bd9Sstevel@tonic-gate  * to another attribute, then another xattr_buf section is included
5447c478bd9Sstevel@tonic-gate  * for the link.
5457c478bd9Sstevel@tonic-gate  *
5467c478bd9Sstevel@tonic-gate  * The xattr_buf is used to define the necessary "pathing" steps
5477c478bd9Sstevel@tonic-gate  * to get to the extended attribute.  This is necessary to support
5487c478bd9Sstevel@tonic-gate  * a fully recursive attribute model where an attribute may itself
5497c478bd9Sstevel@tonic-gate  * have an attribute.
5507c478bd9Sstevel@tonic-gate  *
5517c478bd9Sstevel@tonic-gate  * The basic layout looks like this.
5527c478bd9Sstevel@tonic-gate  *
5537c478bd9Sstevel@tonic-gate  *     --------------------------------
5547c478bd9Sstevel@tonic-gate  *     |                              |
5557c478bd9Sstevel@tonic-gate  *     |         xattr_hdr            |
5567c478bd9Sstevel@tonic-gate  *     |                              |
5577c478bd9Sstevel@tonic-gate  *     --------------------------------
5587c478bd9Sstevel@tonic-gate  *     --------------------------------
5597c478bd9Sstevel@tonic-gate  *     |                              |
5607c478bd9Sstevel@tonic-gate  *     |        xattr_buf             |
5617c478bd9Sstevel@tonic-gate  *     |                              |
5627c478bd9Sstevel@tonic-gate  *     --------------------------------
5637c478bd9Sstevel@tonic-gate  *     --------------------------------
5647c478bd9Sstevel@tonic-gate  *     |                              |
5657c478bd9Sstevel@tonic-gate  *     |      (optional link info)    |
5667c478bd9Sstevel@tonic-gate  *     |                              |
5677c478bd9Sstevel@tonic-gate  *     --------------------------------
5687c478bd9Sstevel@tonic-gate  *     --------------------------------
5697c478bd9Sstevel@tonic-gate  *     |                              |
5707c478bd9Sstevel@tonic-gate  *     |      attribute itself        |
5717c478bd9Sstevel@tonic-gate  *     |      stored as normal tar    |
5727c478bd9Sstevel@tonic-gate  *     |      or cpio data with       |
5737c478bd9Sstevel@tonic-gate  *     |      special mode or         |
5747c478bd9Sstevel@tonic-gate  *     |      typeflag                |
5757c478bd9Sstevel@tonic-gate  *     |                              |
5767c478bd9Sstevel@tonic-gate  *     --------------------------------
5777c478bd9Sstevel@tonic-gate  *
5787c478bd9Sstevel@tonic-gate  */
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate /*
5817c478bd9Sstevel@tonic-gate  * Extended attributes structures
5827c478bd9Sstevel@tonic-gate  *
5837c478bd9Sstevel@tonic-gate  * xattrhead is the complete extended attribute header, as read of off
5847c478bd9Sstevel@tonic-gate  * disk/tape. It includes the variable xattr_buf portion.
5857c478bd9Sstevel@tonic-gate  *
5867c478bd9Sstevel@tonic-gate  * xattrp is basically an offset into xattrhead that points to the
5877c478bd9Sstevel@tonic-gate  * "pathing" section which defines how to get to the attribute.
5887c478bd9Sstevel@tonic-gate  *
5897c478bd9Sstevel@tonic-gate  * xattr_linkp is identical to xattrp except that it is used for linked
5907c478bd9Sstevel@tonic-gate  * attributes.  It provides the pathing steps to get to the linked
5917c478bd9Sstevel@tonic-gate  * attribute.
5927c478bd9Sstevel@tonic-gate  *
5937c478bd9Sstevel@tonic-gate  * These structures are updated when an extended attribute header is read off
5947c478bd9Sstevel@tonic-gate  * of disk/tape.
5957c478bd9Sstevel@tonic-gate  */
5967c478bd9Sstevel@tonic-gate static struct xattr_hdr	*xattrhead;
5977c478bd9Sstevel@tonic-gate static struct xattr_buf	*xattrp;
5987c478bd9Sstevel@tonic-gate static struct xattr_buf	*xattr_linkp;
5997c478bd9Sstevel@tonic-gate static int 		xattrbadhead;	/* is extended attribute header bad? */
6007c478bd9Sstevel@tonic-gate 
601fa9e4066Sahrens static int	append_secattr(char **, int *, acl_t *);
6027c478bd9Sstevel@tonic-gate static void	write_ancillary(char *, int);
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate /*
6057c478bd9Sstevel@tonic-gate  * Note regarding cpio and changes to ensure cpio doesn't try to second
6067c478bd9Sstevel@tonic-gate  * guess whether it runs with sufficient privileges or not:
6077c478bd9Sstevel@tonic-gate  *
6087c478bd9Sstevel@tonic-gate  * cpio has been changed so that it doesn't carry a second implementation of
6097c478bd9Sstevel@tonic-gate  * the kernel's policy with respect to privileges.  Instead of attempting
6107c478bd9Sstevel@tonic-gate  * to restore uid and gid from an archive only if cpio is run as uid 0,
611647ab8f4Sceastha  * cpio now *always* tries to restore the uid and gid from the archive
612647ab8f4Sceastha  * except when the -R option is specified.  When the -R is specified,
613647ab8f4Sceastha  * the uid and gid of the restored file will be changed to those of the
614647ab8f4Sceastha  * login id specified.  In addition, chown(), set_tym(), and chmod() should
615647ab8f4Sceastha  * only be executed once during archive extraction, and to ensure
616647ab8f4Sceastha  * setuid/setgid bits are restored properly, chown() should always be
617647ab8f4Sceastha  * executed before chmod().
6187c478bd9Sstevel@tonic-gate  *
6197c478bd9Sstevel@tonic-gate  * Note regarding debugging mechanism for cpio:
6207c478bd9Sstevel@tonic-gate  *
6217c478bd9Sstevel@tonic-gate  * The following mechanism is provided to allow us to debug cpio in complicated
6227c478bd9Sstevel@tonic-gate  * situations, like when it is part of a pipe.  The idea is that you compile
6237c478bd9Sstevel@tonic-gate  * with -DWAITAROUND defined, and then add the "-z" command line option to the
6247c478bd9Sstevel@tonic-gate  * target cpio invocation.  If stderr is available, it will tell you to which
6257c478bd9Sstevel@tonic-gate  * pid to attach the debugger; otherwise, use ps to find it.  Attach to the
6267c478bd9Sstevel@tonic-gate  * process from the debugger, and, *PRESTO*, you are there!
6277c478bd9Sstevel@tonic-gate  *
6287c478bd9Sstevel@tonic-gate  * Simply assign "waitaround = 0" once you attach to the process, and then
6297c478bd9Sstevel@tonic-gate  * proceed from there as usual.
6307c478bd9Sstevel@tonic-gate  */
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
6337c478bd9Sstevel@tonic-gate int waitaround = 0;		/* wait for rendezvous with the debugger */
6347c478bd9Sstevel@tonic-gate #endif
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate /*
6377c478bd9Sstevel@tonic-gate  * Allocation wrappers and their flags
6387c478bd9Sstevel@tonic-gate  */
6397c478bd9Sstevel@tonic-gate #define	E_NORMAL	0x0	/* Return NULL if allocation fails */
6407c478bd9Sstevel@tonic-gate #define	E_EXIT		0x1	/* Exit if allocation fails */
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate static void *e_realloc(int flag, void *old, size_t newsize);
6437c478bd9Sstevel@tonic-gate static char *e_strdup(int flag, const char *arg);
6447c478bd9Sstevel@tonic-gate static void *e_valloc(int flag, size_t size);
6457c478bd9Sstevel@tonic-gate static void *e_zalloc(int flag, size_t size);
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate #define	EXIT_CODE	(Error_cnt > 255 ? 255 : Error_cnt)
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate /*
6507c478bd9Sstevel@tonic-gate  * main: Call setup() to process options and perform initializations,
6517c478bd9Sstevel@tonic-gate  * and then select either copy in (-i), copy out (-o), or pass (-p) action.
6527c478bd9Sstevel@tonic-gate  */
6537c478bd9Sstevel@tonic-gate 
654944d83a0Schin int
6557c478bd9Sstevel@tonic-gate main(int argc, char **argv)
6567c478bd9Sstevel@tonic-gate {
6577c478bd9Sstevel@tonic-gate 	int i;
6587c478bd9Sstevel@tonic-gate 	int passret;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
6617c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
6627c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
6637c478bd9Sstevel@tonic-gate #endif
6647c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	(void) memset(&Gen, 0, sizeof (Gen));
6677c478bd9Sstevel@tonic-gate 	setup(argc, argv);
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	if (signal(SIGINT, sigint) == SIG_IGN)
6707c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, SIG_IGN);
6717c478bd9Sstevel@tonic-gate 	switch (Args & (OCi | OCo | OCp)) {
6727c478bd9Sstevel@tonic-gate 	case OCi: /* COPY IN */
6737c478bd9Sstevel@tonic-gate 		Hdr_type = NONE;
6747c478bd9Sstevel@tonic-gate 		while ((i = gethdr()) != 0) {
6757c478bd9Sstevel@tonic-gate 			Gen.g_dirfd = -1;
6767c478bd9Sstevel@tonic-gate 			if (i == 1) {
6777c478bd9Sstevel@tonic-gate 				file_in();
6787c478bd9Sstevel@tonic-gate 				/*
6797c478bd9Sstevel@tonic-gate 				 * Any ACL info for this file would or should
6807c478bd9Sstevel@tonic-gate 				 * have been used after file_in(); clear out
6817c478bd9Sstevel@tonic-gate 				 * aclp so it is is not erroneously used on
6827c478bd9Sstevel@tonic-gate 				 * the next file.
6837c478bd9Sstevel@tonic-gate 				 */
6847c478bd9Sstevel@tonic-gate 				if (aclp != NULL) {
685fa9e4066Sahrens 					acl_free(aclp);
6867c478bd9Sstevel@tonic-gate 					aclp = NULL;
6877c478bd9Sstevel@tonic-gate 				}
688fa9e4066Sahrens 				acl_is_set = 0;
6897c478bd9Sstevel@tonic-gate 			}
6907c478bd9Sstevel@tonic-gate 			(void) memset(&Gen, 0, sizeof (Gen));
6917c478bd9Sstevel@tonic-gate 		}
6927c478bd9Sstevel@tonic-gate 		/* Do not count "extra" "read-ahead" buffered data */
6937c478bd9Sstevel@tonic-gate 		if (Buffr.b_cnt > Bufsize)
6947c478bd9Sstevel@tonic-gate 			Blocks -=  (u_longlong_t)(Buffr.b_cnt / Bufsize);
6957c478bd9Sstevel@tonic-gate 		break;
6967c478bd9Sstevel@tonic-gate 	case OCo: /* COPY OUT */
6977c478bd9Sstevel@tonic-gate 		if (Args & OCA) {
6987c478bd9Sstevel@tonic-gate 			scan4trail();
6997c478bd9Sstevel@tonic-gate 		}
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 		Gen.g_dirfd = -1;
7027c478bd9Sstevel@tonic-gate 		Gen.g_dirpath = NULL;
7037c478bd9Sstevel@tonic-gate 		sl_preview_synonyms();
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 		while ((i = getname()) != 0) {
7067c478bd9Sstevel@tonic-gate 			if (i == 1) {
7077c478bd9Sstevel@tonic-gate 				(void) file_out();
7087c478bd9Sstevel@tonic-gate 				if (Atflag) {
7097c478bd9Sstevel@tonic-gate 					if (Gen.g_dirfd != -1) {
7107c478bd9Sstevel@tonic-gate 						(void) close(Gen.g_dirfd);
7117c478bd9Sstevel@tonic-gate 					}
7127c478bd9Sstevel@tonic-gate 					Gen.g_dirfd = -1;
7137c478bd9Sstevel@tonic-gate 					xattrs_out(file_out);
7147c478bd9Sstevel@tonic-gate 				}
7157c478bd9Sstevel@tonic-gate 				Hiddendir = 0;
7167c478bd9Sstevel@tonic-gate 			}
7177c478bd9Sstevel@tonic-gate 			if (aclp != NULL) {
718fa9e4066Sahrens 				acl_free(aclp);
7197c478bd9Sstevel@tonic-gate 				aclp = NULL;
720fa9e4066Sahrens 				acl_is_set = 0;
7217c478bd9Sstevel@tonic-gate 			}
7227c478bd9Sstevel@tonic-gate 		}
7237c478bd9Sstevel@tonic-gate 		write_trail();
7247c478bd9Sstevel@tonic-gate 		break;
7257c478bd9Sstevel@tonic-gate 	case OCp: /* PASS */
7267c478bd9Sstevel@tonic-gate 		sl_preview_synonyms();
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 		Gen.g_dirfd = -1;
7297c478bd9Sstevel@tonic-gate 		Gen.g_passdirfd = -1;
7307c478bd9Sstevel@tonic-gate 		Gen.g_dirpath = NULL;
7317c478bd9Sstevel@tonic-gate 		while (getname()) {
7327c478bd9Sstevel@tonic-gate 			/*
7337c478bd9Sstevel@tonic-gate 			 * If file is a fully qualified path then
7347c478bd9Sstevel@tonic-gate 			 * file_pass will strip off the leading '/'
7357c478bd9Sstevel@tonic-gate 			 * and we need to save off the unstripped
7367c478bd9Sstevel@tonic-gate 			 * name for attribute traversal.
7377c478bd9Sstevel@tonic-gate 			 */
7387c478bd9Sstevel@tonic-gate 			if (Atflag) {
7397c478bd9Sstevel@tonic-gate 				(void) strcpy(Savenam_p, Gen.g_nam_p);
7407c478bd9Sstevel@tonic-gate 			}
7417c478bd9Sstevel@tonic-gate 			passret = file_pass();
7427c478bd9Sstevel@tonic-gate 			if (aclp != NULL) {
743fa9e4066Sahrens 				acl_free(aclp);
7447c478bd9Sstevel@tonic-gate 				aclp = NULL;
745fa9e4066Sahrens 				acl_is_set = 0;
7467c478bd9Sstevel@tonic-gate 			}
7477c478bd9Sstevel@tonic-gate 			if (Gen.g_passdirfd != -1)
7487c478bd9Sstevel@tonic-gate 				(void) close(Gen.g_passdirfd);
7497c478bd9Sstevel@tonic-gate 			Gen.g_passdirfd = -1;
7507c478bd9Sstevel@tonic-gate 			if (Atflag) {
7517c478bd9Sstevel@tonic-gate 				if (Gen.g_dirfd != -1) {
7527c478bd9Sstevel@tonic-gate 					(void) close(Gen.g_dirfd);
7537c478bd9Sstevel@tonic-gate 				}
7547c478bd9Sstevel@tonic-gate 				Gen.g_dirfd = -1;
7557c478bd9Sstevel@tonic-gate 				if (passret != FILE_LINKED) {
7567c478bd9Sstevel@tonic-gate 					Gen.g_nam_p = Savenam_p;
7577c478bd9Sstevel@tonic-gate 					xattrs_out(file_pass);
7587c478bd9Sstevel@tonic-gate 				}
7597c478bd9Sstevel@tonic-gate 			}
7607c478bd9Sstevel@tonic-gate 		}
7617c478bd9Sstevel@tonic-gate 		break;
7627c478bd9Sstevel@tonic-gate 	default:
7637c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible action.");
7647c478bd9Sstevel@tonic-gate 	}
7657c478bd9Sstevel@tonic-gate 	if (Ofile > 0) {
7667c478bd9Sstevel@tonic-gate 		if (close(Ofile) != 0)
7677c478bd9Sstevel@tonic-gate 			msg(EXTN, "close error");
7687c478bd9Sstevel@tonic-gate 	}
7697c478bd9Sstevel@tonic-gate 	if (Archive > 0) {
7707c478bd9Sstevel@tonic-gate 		if (close(Archive) != 0)
7717c478bd9Sstevel@tonic-gate 			msg(EXTN, "close error");
7727c478bd9Sstevel@tonic-gate 	}
7737c478bd9Sstevel@tonic-gate 	Blocks = (u_longlong_t)(Blocks * Bufsize + SBlocks + 0x1FF) >> 9;
7747c478bd9Sstevel@tonic-gate 	msg(EPOST, "%lld blocks", Blocks);
7757c478bd9Sstevel@tonic-gate 	if (Error_cnt)
7767c478bd9Sstevel@tonic-gate 		msg(EPOST, "%d error(s)", Error_cnt);
7777c478bd9Sstevel@tonic-gate 	return (EXIT_CODE);
7787c478bd9Sstevel@tonic-gate }
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate /*
7817c478bd9Sstevel@tonic-gate  * add_lnk: Add a linked file's header to the linked file data structure, by
7827c478bd9Sstevel@tonic-gate  * either adding it to the end of an existing sub-list or starting
7837c478bd9Sstevel@tonic-gate  * a new sub-list.  Each sub-list saves the links to a given file.
7847c478bd9Sstevel@tonic-gate  *
7857c478bd9Sstevel@tonic-gate  * Directly returns a pointer to the new entry; returns a pointer to the head
7867c478bd9Sstevel@tonic-gate  * of the sub-list in which that entry is located through the argument.
7877c478bd9Sstevel@tonic-gate  */
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate static struct Lnk *
7907c478bd9Sstevel@tonic-gate add_lnk(struct Lnk **sublist_return)
7917c478bd9Sstevel@tonic-gate {
7927c478bd9Sstevel@tonic-gate 	struct Lnk *new_entry, *sublist;
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	for (sublist = Lnk_hd.L_nxt_p;
7957c478bd9Sstevel@tonic-gate 	    sublist != &Lnk_hd;
7967c478bd9Sstevel@tonic-gate 	    sublist = sublist->L_nxt_p) {
7977c478bd9Sstevel@tonic-gate 		if (sublist->L_gen.g_ino == G_p->g_ino &&
7987c478bd9Sstevel@tonic-gate 		    sublist->L_gen.g_dev == G_p->g_dev) {
7997c478bd9Sstevel@tonic-gate 			/* found */
8007c478bd9Sstevel@tonic-gate 			break;
8017c478bd9Sstevel@tonic-gate 		}
8027c478bd9Sstevel@tonic-gate 	}
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	new_entry = e_zalloc(E_EXIT, sizeof (struct Lnk));
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	new_entry->L_lnk_p = NULL;
8077c478bd9Sstevel@tonic-gate 	new_entry->L_gen = *G_p; /* structure copy */
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	new_entry->L_gen.g_nam_p = e_zalloc(E_EXIT, (size_t)G_p->g_namesz);
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	(void) strcpy(new_entry->L_gen.g_nam_p, G_p->g_nam_p);
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	if (sublist == &Lnk_hd) {
8147c478bd9Sstevel@tonic-gate 		/* start new sub-list */
8157c478bd9Sstevel@tonic-gate 		new_entry->L_nxt_p = &Lnk_hd;
8167c478bd9Sstevel@tonic-gate 		new_entry->L_bck_p = Lnk_hd.L_bck_p;
8177c478bd9Sstevel@tonic-gate 		Lnk_hd.L_bck_p = new_entry->L_bck_p->L_nxt_p = new_entry;
8187c478bd9Sstevel@tonic-gate 		new_entry->L_lnk_p = NULL;
8197c478bd9Sstevel@tonic-gate 		new_entry->L_cnt = 1;
8207c478bd9Sstevel@tonic-gate 		new_entry->L_data = Onecopy ? 0 : 1;
8217c478bd9Sstevel@tonic-gate 		sublist = new_entry;
8227c478bd9Sstevel@tonic-gate 	} else {
8237c478bd9Sstevel@tonic-gate 		/* add to existing sub-list */
8247c478bd9Sstevel@tonic-gate 		struct Lnk *ptr;
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 		sublist->L_cnt++;
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 		for (ptr = sublist;
8297c478bd9Sstevel@tonic-gate 		    ptr->L_lnk_p != NULL;
8307c478bd9Sstevel@tonic-gate 		    ptr = ptr->L_lnk_p) {
8317c478bd9Sstevel@tonic-gate 			ptr->L_gen.g_filesz = G_p->g_filesz;
8327c478bd9Sstevel@tonic-gate 		}
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 		ptr->L_gen.g_filesz = G_p->g_filesz;
8357c478bd9Sstevel@tonic-gate 		ptr->L_lnk_p = new_entry;
8367c478bd9Sstevel@tonic-gate 	}
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	*sublist_return = sublist;
8397c478bd9Sstevel@tonic-gate 	return (new_entry);
8407c478bd9Sstevel@tonic-gate }
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate /*
8437c478bd9Sstevel@tonic-gate  * bfill: Read req_cnt bytes (out of filelen bytes) from the I/O buffer,
8447c478bd9Sstevel@tonic-gate  * moving them to rd_buf_p.  When there are no bytes left in the I/O buffer,
8457c478bd9Sstevel@tonic-gate  * Fillbuf is set and the I/O buffer is filled.  The variable dist is the
8467c478bd9Sstevel@tonic-gate  * distance to lseek if an I/O error is encountered with the -k option set
8477c478bd9Sstevel@tonic-gate  * (converted to a multiple of Bufsize).
8487c478bd9Sstevel@tonic-gate  */
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate static int
8517c478bd9Sstevel@tonic-gate bfill(void)
8527c478bd9Sstevel@tonic-gate {
8537c478bd9Sstevel@tonic-gate 	int i = 0, rv;
8547c478bd9Sstevel@tonic-gate 	static int eof = 0;
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	if (!Dflag) {
8577c478bd9Sstevel@tonic-gate 	while ((Buffr.b_end_p - Buffr.b_in_p) >= Bufsize) {
8587c478bd9Sstevel@tonic-gate 		errno = 0;
8597c478bd9Sstevel@tonic-gate 		if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0) {
8607c478bd9Sstevel@tonic-gate 			if (((Buffr.b_end_p - Buffr.b_in_p) >= Bufsize) &&
8617c478bd9Sstevel@tonic-gate 			    (Eomflag == 0)) {
8627c478bd9Sstevel@tonic-gate 				Eomflag = 1;
8637c478bd9Sstevel@tonic-gate 				return (1);
8647c478bd9Sstevel@tonic-gate 			}
8657c478bd9Sstevel@tonic-gate 			if (errno == ENOSPC) {
8667c478bd9Sstevel@tonic-gate 				(void) chgreel(INPUT);
8677c478bd9Sstevel@tonic-gate 				if (Hdr_type == BAR) {
8687c478bd9Sstevel@tonic-gate 					skip_bar_volhdr();
8697c478bd9Sstevel@tonic-gate 				}
8707c478bd9Sstevel@tonic-gate 				continue;
8717c478bd9Sstevel@tonic-gate 			} else if (Args & OCk) {
8727c478bd9Sstevel@tonic-gate 				if (i++ > MX_SEEKS)
8737c478bd9Sstevel@tonic-gate 					msg(EXT, "Cannot recover.");
8747c478bd9Sstevel@tonic-gate 				if (lseek(Archive, Bufsize, SEEK_REL) < 0)
8757c478bd9Sstevel@tonic-gate 					msg(EXTN, "Cannot lseek()");
8767c478bd9Sstevel@tonic-gate 				Error_cnt++;
8777c478bd9Sstevel@tonic-gate 				Buf_error++;
8787c478bd9Sstevel@tonic-gate 				rv = 0;
8797c478bd9Sstevel@tonic-gate 				continue;
8807c478bd9Sstevel@tonic-gate 			} else
8817c478bd9Sstevel@tonic-gate 				ioerror(INPUT);
8827c478bd9Sstevel@tonic-gate 		} /* (rv = g_read(Device, Archive ... */
8837c478bd9Sstevel@tonic-gate 		if (Hdr_type != BAR || rv == Bufsize) {
8847c478bd9Sstevel@tonic-gate 			Buffr.b_in_p += rv;
8857c478bd9Sstevel@tonic-gate 			Buffr.b_cnt += (long)rv;
8867c478bd9Sstevel@tonic-gate 		}
8877c478bd9Sstevel@tonic-gate 		if (rv == Bufsize) {
8887c478bd9Sstevel@tonic-gate 			eof = 0;
8897c478bd9Sstevel@tonic-gate 			Blocks++;
8907c478bd9Sstevel@tonic-gate 		} else if (rv == 0) {
8917c478bd9Sstevel@tonic-gate 			if (!eof) {
8927c478bd9Sstevel@tonic-gate 				eof = 1;
8937c478bd9Sstevel@tonic-gate 				break;
8947c478bd9Sstevel@tonic-gate 			}
8957c478bd9Sstevel@tonic-gate 			(void) chgreel(INPUT);
8967c478bd9Sstevel@tonic-gate 			eof = 0;	/* reset the eof after chgreel	*/
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 			/*
8997c478bd9Sstevel@tonic-gate 			 * if spans multiple volume, skip the volume header of
9007c478bd9Sstevel@tonic-gate 			 * the next volume so that the file currently being
9017c478bd9Sstevel@tonic-gate 			 * extracted can continue to be extracted.
9027c478bd9Sstevel@tonic-gate 			 */
9037c478bd9Sstevel@tonic-gate 			if (Hdr_type == BAR) {
9047c478bd9Sstevel@tonic-gate 				skip_bar_volhdr();
9057c478bd9Sstevel@tonic-gate 			}
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 			continue;
9087c478bd9Sstevel@tonic-gate 		} else {
9097c478bd9Sstevel@tonic-gate 			eof = 0;
9107c478bd9Sstevel@tonic-gate 			SBlocks += (u_longlong_t)rv;
9117c478bd9Sstevel@tonic-gate 		}
9127c478bd9Sstevel@tonic-gate 	} /* (Buffr.b_end_p - Buffr.b_in_p) <= Bufsize */
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	} else {			/* Dflag */
9157c478bd9Sstevel@tonic-gate 		errno = 0;
9167c478bd9Sstevel@tonic-gate 		if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0) {
9177c478bd9Sstevel@tonic-gate 			return (-1);
9187c478bd9Sstevel@tonic-gate 		} /* (rv = g_read(Device, Archive ... */
9197c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += rv;
9207c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += (long)rv;
9217c478bd9Sstevel@tonic-gate 		if (rv == Bufsize) {
9227c478bd9Sstevel@tonic-gate 			eof = 0;
9237c478bd9Sstevel@tonic-gate 			Blocks++;
9247c478bd9Sstevel@tonic-gate 		} else if (!rv) {
9257c478bd9Sstevel@tonic-gate 			if (!eof) {
9267c478bd9Sstevel@tonic-gate 				eof = 1;
9277c478bd9Sstevel@tonic-gate 				return (rv);
9287c478bd9Sstevel@tonic-gate 			}
9297c478bd9Sstevel@tonic-gate 			return (-1);
9307c478bd9Sstevel@tonic-gate 		} else {
9317c478bd9Sstevel@tonic-gate 			eof = 0;
9327c478bd9Sstevel@tonic-gate 			SBlocks += (u_longlong_t)rv;
9337c478bd9Sstevel@tonic-gate 		}
9347c478bd9Sstevel@tonic-gate 	}
9357c478bd9Sstevel@tonic-gate 	return (rv);
9367c478bd9Sstevel@tonic-gate }
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate /*
9397c478bd9Sstevel@tonic-gate  * bflush: Move wr_cnt bytes from data_p into the I/O buffer.  When the
9407c478bd9Sstevel@tonic-gate  * I/O buffer is full, Flushbuf is set and the buffer is written out.
9417c478bd9Sstevel@tonic-gate  */
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate static void
9447c478bd9Sstevel@tonic-gate bflush(void)
9457c478bd9Sstevel@tonic-gate {
9467c478bd9Sstevel@tonic-gate 	int rv;
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	while (Buffr.b_cnt >= Bufsize) {
9497c478bd9Sstevel@tonic-gate 		errno = 0;
9507c478bd9Sstevel@tonic-gate 		if ((rv = g_write(Device, Archive, Buffr.b_out_p,
9517c478bd9Sstevel@tonic-gate 		    Bufsize)) < 0) {
9527c478bd9Sstevel@tonic-gate 			if (errno == ENOSPC && !Dflag)
9537c478bd9Sstevel@tonic-gate 				rv = chgreel(OUTPUT);
9547c478bd9Sstevel@tonic-gate 			else
9557c478bd9Sstevel@tonic-gate 				ioerror(OUTPUT);
9567c478bd9Sstevel@tonic-gate 		}
9577c478bd9Sstevel@tonic-gate 		Buffr.b_out_p += rv;
9587c478bd9Sstevel@tonic-gate 		Buffr.b_cnt -= (long)rv;
9597c478bd9Sstevel@tonic-gate 		if (rv == Bufsize)
9607c478bd9Sstevel@tonic-gate 			Blocks++;
9617c478bd9Sstevel@tonic-gate 		else if (rv > 0)
9627c478bd9Sstevel@tonic-gate 			SBlocks += (u_longlong_t)rv;
9637c478bd9Sstevel@tonic-gate 	}
9647c478bd9Sstevel@tonic-gate 	rstbuf();
9657c478bd9Sstevel@tonic-gate }
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate /*
9687c478bd9Sstevel@tonic-gate  * chgreel: Determine if end-of-medium has been reached.  If it has,
9697c478bd9Sstevel@tonic-gate  * close the current medium and prompt the user for the next medium.
9707c478bd9Sstevel@tonic-gate  */
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate static int
9737c478bd9Sstevel@tonic-gate chgreel(int dir)
9747c478bd9Sstevel@tonic-gate {
9757c478bd9Sstevel@tonic-gate 	int lastchar, tryagain, askagain, rv;
9767c478bd9Sstevel@tonic-gate 	int tmpdev;
9777c478bd9Sstevel@tonic-gate 	char str[APATH];
9787c478bd9Sstevel@tonic-gate 	struct stat statb;
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	rv = 0;
9817c478bd9Sstevel@tonic-gate 	if (fstat(Archive, &statb) < 0)
9827c478bd9Sstevel@tonic-gate 		msg(EXTN, "Error during stat() of archive");
9837c478bd9Sstevel@tonic-gate 	if ((statb.st_mode & S_IFMT) != S_IFCHR) {
9847c478bd9Sstevel@tonic-gate 		if (dir == INPUT) {
9857c478bd9Sstevel@tonic-gate 			msg(EXT, "%s%s\n",
9867c478bd9Sstevel@tonic-gate 			    "Can't read input:  end of file encountered ",
9877c478bd9Sstevel@tonic-gate 			    "prior to expected end of archive.");
9887c478bd9Sstevel@tonic-gate 		}
9897c478bd9Sstevel@tonic-gate 	}
9907c478bd9Sstevel@tonic-gate 	msg(EPOST, "\007End of medium on \"%s\".", dir ? "output" : "input");
9917c478bd9Sstevel@tonic-gate 	if (is_floppy(Archive))
9927c478bd9Sstevel@tonic-gate 		(void) ioctl(Archive, FDEJECT, NULL);
9937c478bd9Sstevel@tonic-gate 	if ((close(Archive) != 0) && (dir == OUTPUT))
9947c478bd9Sstevel@tonic-gate 		msg(EXTN, "close error");
9957c478bd9Sstevel@tonic-gate 	Archive = 0;
9967c478bd9Sstevel@tonic-gate 	Volcnt++;
9977c478bd9Sstevel@tonic-gate 	for (;;) {
9987c478bd9Sstevel@tonic-gate 		if (Rtty_p == (FILE *)NULL)
9997c478bd9Sstevel@tonic-gate 			Rtty_p = fopen(Ttyname, "r");
10007c478bd9Sstevel@tonic-gate 		do { /* tryagain */
10017c478bd9Sstevel@tonic-gate 			if (IOfil_p) {
10027c478bd9Sstevel@tonic-gate 				do {
10037c478bd9Sstevel@tonic-gate 					msg(EPOST, gettext(Eom_p), Volcnt);
10047c478bd9Sstevel@tonic-gate 					if (!Rtty_p || fgets(str, sizeof (str),
10057c478bd9Sstevel@tonic-gate 					    Rtty_p) == (char *)NULL)
10067c478bd9Sstevel@tonic-gate 						msg(EXT, "Cannot read tty.");
10077c478bd9Sstevel@tonic-gate 					askagain = 0;
10087c478bd9Sstevel@tonic-gate 					switch (*str) {
10097c478bd9Sstevel@tonic-gate 					case '\n':
10107c478bd9Sstevel@tonic-gate 						(void) strcpy(str, IOfil_p);
10117c478bd9Sstevel@tonic-gate 						break;
10127c478bd9Sstevel@tonic-gate 					case 'q':
10137c478bd9Sstevel@tonic-gate 						exit(EXIT_CODE);
10147c478bd9Sstevel@tonic-gate 					default:
10157c478bd9Sstevel@tonic-gate 						askagain = 1;
10167c478bd9Sstevel@tonic-gate 					}
10177c478bd9Sstevel@tonic-gate 				} while (askagain);
10187c478bd9Sstevel@tonic-gate 			} else {
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 				if (Hdr_type == BAR)
10217c478bd9Sstevel@tonic-gate 					Bar_vol_num++;
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 				msg(EPOST,
10247c478bd9Sstevel@tonic-gate 				    "To continue, type device/file name when "
10257c478bd9Sstevel@tonic-gate 				    "ready.");
10267c478bd9Sstevel@tonic-gate 				if (!Rtty_p || fgets(str, sizeof (str),
10277c478bd9Sstevel@tonic-gate 				    Rtty_p) == (char *)NULL)
10287c478bd9Sstevel@tonic-gate 					msg(EXT, "Cannot read tty.");
10297c478bd9Sstevel@tonic-gate 				lastchar = strlen(str) - 1;
10307c478bd9Sstevel@tonic-gate 				if (*(str + lastchar) == '\n') /* remove '\n' */
10317c478bd9Sstevel@tonic-gate 					*(str + lastchar) = '\0';
10327c478bd9Sstevel@tonic-gate 				if (!*str)
10337c478bd9Sstevel@tonic-gate 					exit(EXIT_CODE);
10347c478bd9Sstevel@tonic-gate 			}
10357c478bd9Sstevel@tonic-gate 			tryagain = 0;
10367c478bd9Sstevel@tonic-gate 			if ((Archive = open(str, dir)) < 0) {
10377c478bd9Sstevel@tonic-gate 				msg(ERRN, "Cannot open \"%s\"", str);
10387c478bd9Sstevel@tonic-gate 				tryagain = 1;
10397c478bd9Sstevel@tonic-gate 			}
10407c478bd9Sstevel@tonic-gate 		} while (tryagain);
10417c478bd9Sstevel@tonic-gate 		(void) g_init(&tmpdev, &Archive);
10427c478bd9Sstevel@tonic-gate 		if (tmpdev != Device)
10437c478bd9Sstevel@tonic-gate 			msg(EXT, "Cannot change media types in mid-stream.");
10447c478bd9Sstevel@tonic-gate 		if (dir == INPUT)
10457c478bd9Sstevel@tonic-gate 			break;
10467c478bd9Sstevel@tonic-gate 		else { /* dir == OUTPUT */
10477c478bd9Sstevel@tonic-gate 			errno = 0;
10487c478bd9Sstevel@tonic-gate 			if ((rv = g_write(Device, Archive, Buffr.b_out_p,
10497c478bd9Sstevel@tonic-gate 			    Bufsize)) == Bufsize)
10507c478bd9Sstevel@tonic-gate 				break;
10517c478bd9Sstevel@tonic-gate 			else
10527c478bd9Sstevel@tonic-gate 				msg(ERR,
10537c478bd9Sstevel@tonic-gate 				    "Unable to write this medium, try "
10547c478bd9Sstevel@tonic-gate 				    "another.");
10557c478bd9Sstevel@tonic-gate 		}
10567c478bd9Sstevel@tonic-gate 	} /* ;; */
10577c478bd9Sstevel@tonic-gate 	Eomflag = 0;
10587c478bd9Sstevel@tonic-gate 	return (rv);
10597c478bd9Sstevel@tonic-gate }
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate /*
10627c478bd9Sstevel@tonic-gate  * ckname: Check filenames against user specified patterns,
10637c478bd9Sstevel@tonic-gate  * and/or ask the user for new name when -r is used.
10647c478bd9Sstevel@tonic-gate  */
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate static int
10677c478bd9Sstevel@tonic-gate ckname(int flag)
10687c478bd9Sstevel@tonic-gate {
10697c478bd9Sstevel@tonic-gate 	int lastchar, len;
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 	if (Hdr_type != TAR && Hdr_type != USTAR && Hdr_type != BAR) {
10727c478bd9Sstevel@tonic-gate 		/* Re-visit tar size issues later */
10737c478bd9Sstevel@tonic-gate 		if (G_p->g_namesz - 1 > Max_namesz) {
10747c478bd9Sstevel@tonic-gate 			msg(ERR, "Name exceeds maximum length - skipped.");
10757c478bd9Sstevel@tonic-gate 			return (F_SKIP);
10767c478bd9Sstevel@tonic-gate 		}
10777c478bd9Sstevel@tonic-gate 	}
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	if (Pat_pp && !matched())
10807c478bd9Sstevel@tonic-gate 		return (F_SKIP);
10817c478bd9Sstevel@tonic-gate 	if ((Args & OCr) && !Adir) { /* rename interactively */
10827c478bd9Sstevel@tonic-gate 		(void) fprintf(Wtty_p, gettext("Rename \"%s%s%s\"? "),
10837c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
10847c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p : Renam_p,
10857c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
10867c478bd9Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
10877c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
10887c478bd9Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p);
10897c478bd9Sstevel@tonic-gate 		(void) fflush(Wtty_p);
10907c478bd9Sstevel@tonic-gate 		if (fgets(Renametmp_p, Max_namesz + 1, Rtty_p) == (char *)NULL)
10917c478bd9Sstevel@tonic-gate 			msg(EXT, "Cannot read tty.");
10927c478bd9Sstevel@tonic-gate 		if (feof(Rtty_p))
10937c478bd9Sstevel@tonic-gate 			exit(EXIT_CODE);
10947c478bd9Sstevel@tonic-gate 		lastchar = strlen(Renametmp_p) - 1;
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 		/* remove trailing '\n' */
10977c478bd9Sstevel@tonic-gate 		if (*(Renametmp_p + lastchar) == '\n')
10987c478bd9Sstevel@tonic-gate 			*(Renametmp_p + lastchar) = '\0';
10997c478bd9Sstevel@tonic-gate 		if (*Renametmp_p == '\0') {
11007c478bd9Sstevel@tonic-gate 			msg(POST, "%s%s%s Skipped.",
11017c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
11027c478bd9Sstevel@tonic-gate 			    G_p->g_nam_p : G_p->g_attrfnam_p,
11037c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
11047c478bd9Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
11057c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
11067c478bd9Sstevel@tonic-gate 			    "" : G_p->g_attrnam_p);
11077c478bd9Sstevel@tonic-gate 			*G_p->g_nam_p = '\0';
11087c478bd9Sstevel@tonic-gate 			return (F_SKIP);
11097c478bd9Sstevel@tonic-gate 		} else if (strcmp(Renametmp_p, ".") != 0) {
11107c478bd9Sstevel@tonic-gate 			len = strlen(Renametmp_p);
11117c478bd9Sstevel@tonic-gate 			if (len > (int)strlen(G_p->g_nam_p)) {
11127c478bd9Sstevel@tonic-gate 				if ((G_p->g_nam_p != &nambuf[0]) &&
11137c478bd9Sstevel@tonic-gate 				    (G_p->g_nam_p != &fullnam[0]))
11147c478bd9Sstevel@tonic-gate 					free(G_p->g_nam_p);
11157c478bd9Sstevel@tonic-gate 				G_p->g_nam_p = e_zalloc(E_EXIT, len + 1);
11167c478bd9Sstevel@tonic-gate 			}
11177c478bd9Sstevel@tonic-gate 			if (G_p->g_attrnam_p != (char *)NULL) {
11187c478bd9Sstevel@tonic-gate 				free(G_p->g_attrnam_p);
11197c478bd9Sstevel@tonic-gate 				G_p->g_attrnam_p = e_strdup(E_EXIT,
11207c478bd9Sstevel@tonic-gate 				    Renametmp_p);
11217c478bd9Sstevel@tonic-gate 				(void) strcpy(G_p->g_nam_p, Renam_p);
11227c478bd9Sstevel@tonic-gate 			} else {
11237c478bd9Sstevel@tonic-gate 				(void) strcpy(Renam_p, Renametmp_p);
11247c478bd9Sstevel@tonic-gate 				(void) strcpy(G_p->g_nam_p, Renametmp_p);
11257c478bd9Sstevel@tonic-gate 			}
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 		}
11287c478bd9Sstevel@tonic-gate 	}
11297c478bd9Sstevel@tonic-gate 	if (flag != 0 || Onecopy == 0) {
11307c478bd9Sstevel@tonic-gate 		VERBOSE((Args & OCt), G_p->g_nam_p);
11317c478bd9Sstevel@tonic-gate 	}
11327c478bd9Sstevel@tonic-gate 	if (Args & OCt)
11337c478bd9Sstevel@tonic-gate 		return (F_SKIP);
11347c478bd9Sstevel@tonic-gate 	return (F_EXTR);
11357c478bd9Sstevel@tonic-gate }
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate /*
11387c478bd9Sstevel@tonic-gate  * ckopts: Check the validity of all command line options.
11397c478bd9Sstevel@tonic-gate  */
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate static void
11427c478bd9Sstevel@tonic-gate ckopts(long mask)
11437c478bd9Sstevel@tonic-gate {
11447c478bd9Sstevel@tonic-gate 	int oflag;
11457c478bd9Sstevel@tonic-gate 	char *t_p;
11467c478bd9Sstevel@tonic-gate 	long errmsk;
1147647ab8f4Sceastha 	uid_t	Euid = geteuid();	/* Effective uid of invoker */
1148647ab8f4Sceastha #ifdef SOLARIS_PRIVS
1149647ab8f4Sceastha 	priv_set_t *privset;
11503a3e214eSceastha 	priv_set_t *zones_privset;
1151647ab8f4Sceastha #endif	/* SOLARIS_PRIVS */
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	if (mask & OCi) {
11547c478bd9Sstevel@tonic-gate 		errmsk = mask & INV_MSK4i;
11557c478bd9Sstevel@tonic-gate 	} else if (mask & OCo) {
11567c478bd9Sstevel@tonic-gate 		errmsk = mask & INV_MSK4o;
11577c478bd9Sstevel@tonic-gate 	} else if (mask & OCp) {
11587c478bd9Sstevel@tonic-gate 		errmsk = mask & INV_MSK4p;
11597c478bd9Sstevel@tonic-gate 	} else {
11607c478bd9Sstevel@tonic-gate 		msg(ERR, "One of -i, -o or -p must be specified.");
11617c478bd9Sstevel@tonic-gate 		errmsk = 0;
11627c478bd9Sstevel@tonic-gate 	}
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	if (errmsk) {
11657c478bd9Sstevel@tonic-gate 		/* if non-zero, invalid options were specified */
11667c478bd9Sstevel@tonic-gate 		Error_cnt++;
11677c478bd9Sstevel@tonic-gate 	}
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 	if ((mask & OCa) && (mask & OCm) && ((mask & OCi) ||
11707c478bd9Sstevel@tonic-gate 	    (mask & OCo))) {
11717c478bd9Sstevel@tonic-gate 		msg(ERR, "-a and -m are mutually exclusive.");
11727c478bd9Sstevel@tonic-gate 	}
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	if ((mask & OCc) && (mask & OCH) && (strcmp("odc", Hdr_p))) {
11757c478bd9Sstevel@tonic-gate 		msg(ERR, "-c and -H are mutually exclusive.");
11767c478bd9Sstevel@tonic-gate 	}
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 	if ((mask & OCv) && (mask & OCV)) {
11797c478bd9Sstevel@tonic-gate 		msg(ERR, "-v and -V are mutually exclusive.");
11807c478bd9Sstevel@tonic-gate 	}
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 	if ((mask & OCt) && (mask & OCV)) {
11837c478bd9Sstevel@tonic-gate 		msg(ERR, "-t and -V are mutually exclusive.");
11847c478bd9Sstevel@tonic-gate 	}
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	if ((mask & OCB) && (mask & OCC)) {
11877c478bd9Sstevel@tonic-gate 		msg(ERR, "-B and -C are mutually exclusive.");
11887c478bd9Sstevel@tonic-gate 	}
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 	if ((mask & OCH) && (mask & OC6)) {
11917c478bd9Sstevel@tonic-gate 		msg(ERR, "-H and -6 are mutually exclusive.");
11927c478bd9Sstevel@tonic-gate 	}
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 	if ((mask & OCM) && !((mask & OCI) || (mask & OCO))) {
11957c478bd9Sstevel@tonic-gate 		msg(ERR, "-M not meaningful without -O or -I.");
11967c478bd9Sstevel@tonic-gate 	}
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 	if ((mask & OCA) && !(mask & OCO)) {
11997c478bd9Sstevel@tonic-gate 		msg(ERR, "-A requires the -O option.");
12007c478bd9Sstevel@tonic-gate 	}
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	if (Bufsize <= 0) {
12037c478bd9Sstevel@tonic-gate 		msg(ERR, "Illegal size given for -C option.");
12047c478bd9Sstevel@tonic-gate 	}
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 	if (mask & OCH) {
12077c478bd9Sstevel@tonic-gate 		t_p = Hdr_p;
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 		while (*t_p != NULL) {
12107c478bd9Sstevel@tonic-gate 			if (isupper(*t_p)) {
12117c478bd9Sstevel@tonic-gate 				*t_p = 'a' + (*t_p - 'A');
12127c478bd9Sstevel@tonic-gate 			}
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 			t_p++;
12157c478bd9Sstevel@tonic-gate 		}
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 		if (!(strcmp("odc", Hdr_p))) {
12187c478bd9Sstevel@tonic-gate 			Hdr_type = CHR;
12197c478bd9Sstevel@tonic-gate 			Max_namesz = CPATH;
12207c478bd9Sstevel@tonic-gate 			Onecopy = 0;
12217c478bd9Sstevel@tonic-gate 			Use_old_stat = 1;
12227c478bd9Sstevel@tonic-gate 		} else if (!(strcmp("crc", Hdr_p))) {
12237c478bd9Sstevel@tonic-gate 			Hdr_type = CRC;
12247c478bd9Sstevel@tonic-gate 			Max_namesz = APATH;
12257c478bd9Sstevel@tonic-gate 			Onecopy = 1;
12267c478bd9Sstevel@tonic-gate 		} else if (!(strcmp("tar", Hdr_p))) {
12277c478bd9Sstevel@tonic-gate 			if (Args & OCo) {
12287c478bd9Sstevel@tonic-gate 				Hdr_type = USTAR;
12297c478bd9Sstevel@tonic-gate 				Max_namesz = HNAMLEN - 1;
12307c478bd9Sstevel@tonic-gate 			} else {
12317c478bd9Sstevel@tonic-gate 				Hdr_type = TAR;
12327c478bd9Sstevel@tonic-gate 				Max_namesz = TNAMLEN - 1;
12337c478bd9Sstevel@tonic-gate 			}
12347c478bd9Sstevel@tonic-gate 			Onecopy = 0;
12357c478bd9Sstevel@tonic-gate 		} else if (!(strcmp("ustar", Hdr_p))) {
12367c478bd9Sstevel@tonic-gate 			Hdr_type = USTAR;
12377c478bd9Sstevel@tonic-gate 			Max_namesz = HNAMLEN - 1;
12387c478bd9Sstevel@tonic-gate 			Onecopy = 0;
12397c478bd9Sstevel@tonic-gate 		} else if (!(strcmp("bar", Hdr_p))) {
12407c478bd9Sstevel@tonic-gate 			if ((Args & OCo) || (Args & OCp)) {
12417c478bd9Sstevel@tonic-gate 				msg(ERR,
12427c478bd9Sstevel@tonic-gate 				    "Header type bar can only be used with -i");
12437c478bd9Sstevel@tonic-gate 			}
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 			if (Args & OCP) {
12467c478bd9Sstevel@tonic-gate 				msg(ERR,
12477c478bd9Sstevel@tonic-gate 				    "Can't preserve using bar header");
12487c478bd9Sstevel@tonic-gate 			}
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 			Hdr_type = BAR;
12517c478bd9Sstevel@tonic-gate 			Max_namesz = TNAMLEN - 1;
12527c478bd9Sstevel@tonic-gate 			Onecopy = 0;
12537c478bd9Sstevel@tonic-gate 		} else {
12547c478bd9Sstevel@tonic-gate 			msg(ERR, "Invalid header \"%s\" specified", Hdr_p);
12557c478bd9Sstevel@tonic-gate 		}
12567c478bd9Sstevel@tonic-gate 	}
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 	if (mask & OCr) {
12597c478bd9Sstevel@tonic-gate 		Rtty_p = fopen(Ttyname, "r");
12607c478bd9Sstevel@tonic-gate 		Wtty_p = fopen(Ttyname, "w");
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 		if (Rtty_p == (FILE *)NULL || Wtty_p == (FILE *)NULL) {
12637c478bd9Sstevel@tonic-gate 			msg(ERR, "Cannot rename, \"%s\" missing", Ttyname);
12647c478bd9Sstevel@tonic-gate 		}
12657c478bd9Sstevel@tonic-gate 	}
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 	if ((mask & OCE) && (Ef_p = fopen(Efil_p, "r")) == (FILE *)NULL) {
12687c478bd9Sstevel@tonic-gate 		msg(ERR, "Cannot open \"%s\" to read patterns", Efil_p);
12697c478bd9Sstevel@tonic-gate 	}
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	if ((mask & OCI) && (Archive = open(IOfil_p, O_RDONLY)) < 0) {
12727c478bd9Sstevel@tonic-gate 		msg(ERR, "Cannot open \"%s\" for input", IOfil_p);
12737c478bd9Sstevel@tonic-gate 	}
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 	if (mask & OCO) {
12767c478bd9Sstevel@tonic-gate 		if (mask & OCA) {
12777c478bd9Sstevel@tonic-gate 			if ((Archive = open(IOfil_p, O_RDWR)) < 0) {
12787c478bd9Sstevel@tonic-gate 				msg(ERR,
12797c478bd9Sstevel@tonic-gate 				    "Cannot open \"%s\" for append",
12807c478bd9Sstevel@tonic-gate 				    IOfil_p);
12817c478bd9Sstevel@tonic-gate 			}
12827c478bd9Sstevel@tonic-gate 		} else {
12837c478bd9Sstevel@tonic-gate 			oflag = (O_WRONLY | O_CREAT | O_TRUNC);
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 			if ((Archive = open(IOfil_p, oflag, 0777)) < 0) {
12867c478bd9Sstevel@tonic-gate 				msg(ERR,
12877c478bd9Sstevel@tonic-gate 				    "Cannot open \"%s\" for output",
12887c478bd9Sstevel@tonic-gate 				    IOfil_p);
12897c478bd9Sstevel@tonic-gate 			}
12907c478bd9Sstevel@tonic-gate 		}
12917c478bd9Sstevel@tonic-gate 	}
12927c478bd9Sstevel@tonic-gate 
1293647ab8f4Sceastha #ifdef SOLARIS_PRIVS
1294647ab8f4Sceastha 	if ((privset = priv_allocset()) == NULL) {
1295647ab8f4Sceastha 		msg(ERR, "Unable to allocate privilege set");
1296647ab8f4Sceastha 	} else if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
1297647ab8f4Sceastha 		msg(ERR, "Unable to obtain privilege set");
1298647ab8f4Sceastha 	} else {
12993a3e214eSceastha 		zones_privset = priv_str_to_set("zone", "", NULL);
13003a3e214eSceastha 		if (zones_privset != NULL) {
13013a3e214eSceastha 			privileged = (priv_issubset(zones_privset,
13023a3e214eSceastha 			    privset) == B_TRUE);
13033a3e214eSceastha 			priv_freeset(zones_privset);
13043a3e214eSceastha 		} else {
13053a3e214eSceastha 			msg(ERR, "Unable to map privilege to privilege set");
13063a3e214eSceastha 		}
1307647ab8f4Sceastha 	}
1308647ab8f4Sceastha 	if (privset != NULL) {
1309647ab8f4Sceastha 		priv_freeset(privset);
1310647ab8f4Sceastha 	}
1311647ab8f4Sceastha #else
1312647ab8f4Sceastha 	privileged = (Euid == 0);
1313647ab8f4Sceastha #endif	/* SOLARIS_PRIVS */
1314647ab8f4Sceastha 
13157c478bd9Sstevel@tonic-gate 	if (mask & OCR) {
13167c478bd9Sstevel@tonic-gate 		if ((Rpw_p = getpwnam(Own_p)) == (struct passwd *)NULL) {
13177c478bd9Sstevel@tonic-gate 			msg(ERR, "\"%s\" is not a valid user id", Own_p);
1318647ab8f4Sceastha 		} else if ((Euid != Rpw_p->pw_uid) && !privileged) {
1319647ab8f4Sceastha 			msg(ERR, "R option only valid for super-user or "
1320647ab8f4Sceastha 			    "id matches login id of user executing cpio");
13217c478bd9Sstevel@tonic-gate 		}
13227c478bd9Sstevel@tonic-gate 	}
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate 	if ((mask & OCo) && !(mask & OCO)) {
13257c478bd9Sstevel@tonic-gate 		Out_p = stderr;
13267c478bd9Sstevel@tonic-gate 	}
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 	if ((mask & OCp) && ((mask & (OCB|OCC)) == 0)) {
13297c478bd9Sstevel@tonic-gate 		/*
13307c478bd9Sstevel@tonic-gate 		 * We are in pass mode with no block size specified.  Use the
13317c478bd9Sstevel@tonic-gate 		 * larger of the native page size and 8192.
13327c478bd9Sstevel@tonic-gate 		 */
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 		Bufsize = (PageSize > 8192) ? PageSize : 8192;
13357c478bd9Sstevel@tonic-gate 	}
13367c478bd9Sstevel@tonic-gate }
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate /*
13397c478bd9Sstevel@tonic-gate  * cksum: Calculate the simple checksum of a file (CRC) or header
13407c478bd9Sstevel@tonic-gate  * (TARTYP (TAR and USTAR)).  For -o and the CRC header, the file is opened and
13417c478bd9Sstevel@tonic-gate  * the checksum is calculated.  For -i and the CRC header, the checksum
13427c478bd9Sstevel@tonic-gate  * is calculated as each block is transferred from the archive I/O buffer
13437c478bd9Sstevel@tonic-gate  * to the file system I/O buffer.  The TARTYP (TAR and USTAR) headers calculate
13447c478bd9Sstevel@tonic-gate  * the simple checksum of the header (with the checksum field of the
13457c478bd9Sstevel@tonic-gate  * header initialized to all spaces (\040).
13467c478bd9Sstevel@tonic-gate  */
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate static long
13497c478bd9Sstevel@tonic-gate cksum(char hdr, int byt_cnt, int *err)
13507c478bd9Sstevel@tonic-gate {
13517c478bd9Sstevel@tonic-gate 	char *crc_p, *end_p;
13527c478bd9Sstevel@tonic-gate 	int cnt;
13537c478bd9Sstevel@tonic-gate 	long checksum = 0L, have;
13547c478bd9Sstevel@tonic-gate 	off_t lcnt;
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 	if (err != NULL)
13577c478bd9Sstevel@tonic-gate 		*err = 0;
13587c478bd9Sstevel@tonic-gate 	switch (hdr) {
13597c478bd9Sstevel@tonic-gate 	case CRC:
13607c478bd9Sstevel@tonic-gate 		if (Args & OCi) { /* do running checksum */
13617c478bd9Sstevel@tonic-gate 			end_p = Buffr.b_out_p + byt_cnt;
13627c478bd9Sstevel@tonic-gate 			for (crc_p = Buffr.b_out_p; crc_p < end_p; crc_p++)
13637c478bd9Sstevel@tonic-gate 				checksum += (long)*crc_p;
13647c478bd9Sstevel@tonic-gate 			break;
13657c478bd9Sstevel@tonic-gate 		}
13667c478bd9Sstevel@tonic-gate 		/* OCo - do checksum of file */
13677c478bd9Sstevel@tonic-gate 		lcnt = G_p->g_filesz;
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate 		while (lcnt > 0) {
13707c478bd9Sstevel@tonic-gate 			have = (lcnt < Bufsize) ? lcnt : Bufsize;
13717c478bd9Sstevel@tonic-gate 			errno = 0;
13727c478bd9Sstevel@tonic-gate 			if (read(Ifile, Buf_p, have) != have) {
13737c478bd9Sstevel@tonic-gate 				msg(ERR, "Error computing checksum.");
13747c478bd9Sstevel@tonic-gate 				if (err != NULL)
13757c478bd9Sstevel@tonic-gate 					*err = 1;
13767c478bd9Sstevel@tonic-gate 				break;
13777c478bd9Sstevel@tonic-gate 			}
13787c478bd9Sstevel@tonic-gate 			end_p = Buf_p + have;
13797c478bd9Sstevel@tonic-gate 			for (crc_p = Buf_p; crc_p < end_p; crc_p++)
13807c478bd9Sstevel@tonic-gate 				checksum += (long)*crc_p;
13817c478bd9Sstevel@tonic-gate 			lcnt -= have;
13827c478bd9Sstevel@tonic-gate 		}
13837c478bd9Sstevel@tonic-gate 		if (lseek(Ifile, (off_t)0, SEEK_ABS) < 0)
13847c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot reset file after checksum");
13857c478bd9Sstevel@tonic-gate 		break;
13867c478bd9Sstevel@tonic-gate 	case TARTYP: /* TAR and USTAR */
13877c478bd9Sstevel@tonic-gate 		crc_p = Thdr_p->tbuf.t_cksum;
13887c478bd9Sstevel@tonic-gate 		for (cnt = 0; cnt < TCRCLEN; cnt++) {
13897c478bd9Sstevel@tonic-gate 			*crc_p = '\040';
13907c478bd9Sstevel@tonic-gate 			crc_p++;
13917c478bd9Sstevel@tonic-gate 		}
13927c478bd9Sstevel@tonic-gate 		crc_p = (char *)Thdr_p;
13937c478bd9Sstevel@tonic-gate 		for (cnt = 0; cnt < TARSZ; cnt++) {
13947c478bd9Sstevel@tonic-gate 			/*
13957c478bd9Sstevel@tonic-gate 			 * tar uses unsigned checksum, so we must use unsigned
13967c478bd9Sstevel@tonic-gate 			 * here in order to be able to read tar archives.
13977c478bd9Sstevel@tonic-gate 			 */
13987c478bd9Sstevel@tonic-gate 			checksum += (long)((unsigned char)(*crc_p));
13997c478bd9Sstevel@tonic-gate 			crc_p++;
14007c478bd9Sstevel@tonic-gate 		}
14017c478bd9Sstevel@tonic-gate 		break;
14027c478bd9Sstevel@tonic-gate 	default:
14037c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
14047c478bd9Sstevel@tonic-gate 	} /* hdr */
14057c478bd9Sstevel@tonic-gate 	return (checksum);
14067c478bd9Sstevel@tonic-gate }
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate /*
14097c478bd9Sstevel@tonic-gate  * creat_hdr: Fill in the generic header structure with the specific
14107c478bd9Sstevel@tonic-gate  *            header information based on the value of Hdr_type.
14117c478bd9Sstevel@tonic-gate  *
14127c478bd9Sstevel@tonic-gate  *            return (1) if this process was successful, and (0) otherwise.
14137c478bd9Sstevel@tonic-gate  */
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate static int
14167c478bd9Sstevel@tonic-gate creat_hdr(void)
14177c478bd9Sstevel@tonic-gate {
14187c478bd9Sstevel@tonic-gate 	ushort_t ftype;
14197c478bd9Sstevel@tonic-gate 	int fullnamesize;
14207c478bd9Sstevel@tonic-gate 	dev_t dev;
14217c478bd9Sstevel@tonic-gate 	ino_t ino;
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	ftype = SrcSt.st_mode & Ftype;
14247c478bd9Sstevel@tonic-gate 	Adir = (ftype == S_IFDIR);
1425cdd68f5aSceastha 	Aspec = (ftype == S_IFBLK || ftype == S_IFCHR || ftype == S_IFIFO ||
1426cdd68f5aSceastha 	    ftype == S_IFSOCK);
14277c478bd9Sstevel@tonic-gate 	switch (Hdr_type) {
14287c478bd9Sstevel@tonic-gate 		case BIN:
14297c478bd9Sstevel@tonic-gate 			Gen.g_magic = CMN_BIN;
14307c478bd9Sstevel@tonic-gate 			break;
14317c478bd9Sstevel@tonic-gate 		case CHR:
14327c478bd9Sstevel@tonic-gate 			Gen.g_magic = CMN_BIN;
14337c478bd9Sstevel@tonic-gate 			break;
14347c478bd9Sstevel@tonic-gate 		case ASC:
14357c478bd9Sstevel@tonic-gate 			Gen.g_magic = CMN_ASC;
14367c478bd9Sstevel@tonic-gate 			break;
14377c478bd9Sstevel@tonic-gate 		case CRC:
14387c478bd9Sstevel@tonic-gate 			Gen.g_magic = CMN_CRC;
14397c478bd9Sstevel@tonic-gate 			break;
14407c478bd9Sstevel@tonic-gate 		case USTAR:
14417c478bd9Sstevel@tonic-gate 			/*
14427c478bd9Sstevel@tonic-gate 			 * If the length of the full name is greater than 256,
14437c478bd9Sstevel@tonic-gate 			 * print out a message and return.
14447c478bd9Sstevel@tonic-gate 			 */
14457c478bd9Sstevel@tonic-gate 			if ((fullnamesize = strlen(Gen.g_nam_p)) > MAXNAM) {
14467c478bd9Sstevel@tonic-gate 				msg(ERR,
14477c478bd9Sstevel@tonic-gate 				    "%s: file name too long", Gen.g_nam_p);
14487c478bd9Sstevel@tonic-gate 				return (0);
14497c478bd9Sstevel@tonic-gate 			} else if (fullnamesize > NAMSIZ) {
14507c478bd9Sstevel@tonic-gate 				/*
14517c478bd9Sstevel@tonic-gate 				 * The length of the full name is greater than
14527c478bd9Sstevel@tonic-gate 				 * 100, so we must split the filename from the
14537c478bd9Sstevel@tonic-gate 				 * path
14547c478bd9Sstevel@tonic-gate 				 */
14557c478bd9Sstevel@tonic-gate 				char namebuff[NAMSIZ+1];
14567c478bd9Sstevel@tonic-gate 				char prebuff[PRESIZ+1];
14577c478bd9Sstevel@tonic-gate 				char *lastslash;
14587c478bd9Sstevel@tonic-gate 				int presize, namesize;
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 				(void) memset(namebuff, '\0',
14617c478bd9Sstevel@tonic-gate 				    sizeof (namebuff));
14627c478bd9Sstevel@tonic-gate 				(void) memset(prebuff, '\0', sizeof (prebuff));
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate 				lastslash = strrchr(Gen.g_nam_p, '/');
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 				if (lastslash != NULL) {
14677c478bd9Sstevel@tonic-gate 					namesize = strlen(++lastslash);
14687c478bd9Sstevel@tonic-gate 					presize = fullnamesize - namesize - 1;
14697c478bd9Sstevel@tonic-gate 				} else {
14707c478bd9Sstevel@tonic-gate 					namesize = fullnamesize;
14717c478bd9Sstevel@tonic-gate 					lastslash = Gen.g_nam_p;
14727c478bd9Sstevel@tonic-gate 					presize = 0;
14737c478bd9Sstevel@tonic-gate 				}
14747c478bd9Sstevel@tonic-gate 
14757c478bd9Sstevel@tonic-gate 				/*
14767c478bd9Sstevel@tonic-gate 				 * If the filename is greater than 100 we can't
14777c478bd9Sstevel@tonic-gate 				 * archive the file
14787c478bd9Sstevel@tonic-gate 				 */
14797c478bd9Sstevel@tonic-gate 				if (namesize > NAMSIZ) {
14807c478bd9Sstevel@tonic-gate 					msg(ERR,
14817c478bd9Sstevel@tonic-gate 					    "%s: filename is greater than %d",
14827c478bd9Sstevel@tonic-gate 					    lastslash, NAMSIZ);
14837c478bd9Sstevel@tonic-gate 					return (0);
14847c478bd9Sstevel@tonic-gate 				}
14857c478bd9Sstevel@tonic-gate 				(void) strncpy(&namebuff[0], lastslash,
14867c478bd9Sstevel@tonic-gate 				    namesize);
14877c478bd9Sstevel@tonic-gate 				/*
14887c478bd9Sstevel@tonic-gate 				 * If the prefix is greater than 155 we can't
14897c478bd9Sstevel@tonic-gate 				 * archive the file.
14907c478bd9Sstevel@tonic-gate 				 */
14917c478bd9Sstevel@tonic-gate 				if (presize > PRESIZ) {
14927c478bd9Sstevel@tonic-gate 					msg(ERR,
14937c478bd9Sstevel@tonic-gate 					    "%s: prefix is greater than %d",
14947c478bd9Sstevel@tonic-gate 					    Gen.g_nam_p, PRESIZ);
14957c478bd9Sstevel@tonic-gate 					return (0);
14967c478bd9Sstevel@tonic-gate 				}
14977c478bd9Sstevel@tonic-gate 				(void) strncpy(&prebuff[0], Gen.g_nam_p,
14987c478bd9Sstevel@tonic-gate 				    presize);
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate 				Gen.g_tname = e_zalloc(E_EXIT, namesize + 1);
15017c478bd9Sstevel@tonic-gate 				(void) strcpy(Gen.g_tname, namebuff);
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 				Gen.g_prefix = e_zalloc(E_EXIT, presize + 1);
15047c478bd9Sstevel@tonic-gate 				(void) strcpy(Gen.g_prefix, prebuff);
15057c478bd9Sstevel@tonic-gate 			} else {
15067c478bd9Sstevel@tonic-gate 				Gen.g_tname = Gen.g_nam_p;
15077c478bd9Sstevel@tonic-gate 			}
15087c478bd9Sstevel@tonic-gate 			(void) strcpy(Gen.g_tmagic, "ustar");
15097c478bd9Sstevel@tonic-gate 			(void) strcpy(Gen.g_version, "00");
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 			dpasswd = getpwuid(SrcSt.st_uid);
15127c478bd9Sstevel@tonic-gate 			if (dpasswd == (struct passwd *)NULL) {
15137c478bd9Sstevel@tonic-gate 				msg(EPOST,
15147c478bd9Sstevel@tonic-gate 				    "cpio: could not get passwd information "
15157c478bd9Sstevel@tonic-gate 				    "for %s%s%s",
15167c478bd9Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
15177c478bd9Sstevel@tonic-gate 				    Gen.g_nam_p : Gen.g_attrfnam_p,
15187c478bd9Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
15197c478bd9Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
15207c478bd9Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
15217c478bd9Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
15227c478bd9Sstevel@tonic-gate 				/* make name null string */
15237c478bd9Sstevel@tonic-gate 				Gen.g_uname[0] = '\0';
15247c478bd9Sstevel@tonic-gate 			} else {
15257c478bd9Sstevel@tonic-gate 				(void) strncpy(&Gen.g_uname[0],
15267c478bd9Sstevel@tonic-gate 				    dpasswd->pw_name, 32);
15277c478bd9Sstevel@tonic-gate 			}
15287c478bd9Sstevel@tonic-gate 			dgroup = getgrgid(SrcSt.st_gid);
15297c478bd9Sstevel@tonic-gate 			if (dgroup == (struct group *)NULL) {
15307c478bd9Sstevel@tonic-gate 				msg(EPOST,
15317c478bd9Sstevel@tonic-gate 				    "cpio: could not get group information "
15327c478bd9Sstevel@tonic-gate 				    "for %s%s%S",
15337c478bd9Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
15347c478bd9Sstevel@tonic-gate 				    Gen.g_nam_p : Gen.g_attrfnam_p,
15357c478bd9Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
15367c478bd9Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
15377c478bd9Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
15387c478bd9Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
15397c478bd9Sstevel@tonic-gate 				/* make name null string */
15407c478bd9Sstevel@tonic-gate 				Gen.g_gname[0] = '\0';
15417c478bd9Sstevel@tonic-gate 			} else {
15427c478bd9Sstevel@tonic-gate 				(void) strncpy(&Gen.g_gname[0],
15437c478bd9Sstevel@tonic-gate 				    dgroup->gr_name, 32);
15447c478bd9Sstevel@tonic-gate 			}
15457c478bd9Sstevel@tonic-gate 			Gen.g_typeflag = tartype(ftype);
15467c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
15477c478bd9Sstevel@tonic-gate 		case TAR:
15487c478bd9Sstevel@tonic-gate 			(void) memset(T_lname, '\0', sizeof (T_lname));
15497c478bd9Sstevel@tonic-gate 			break;
15507c478bd9Sstevel@tonic-gate 		default:
15517c478bd9Sstevel@tonic-gate 			msg(EXT, "Impossible header type.");
15527c478bd9Sstevel@tonic-gate 	}
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 	dev = SrcSt.st_dev;
15557c478bd9Sstevel@tonic-gate 	ino = SrcSt.st_ino;
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	if (Use_old_stat) {
15587c478bd9Sstevel@tonic-gate 		SrcSt = *OldSt;
15597c478bd9Sstevel@tonic-gate 	}
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 	Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
15627c478bd9Sstevel@tonic-gate 	Gen.g_uid = SrcSt.st_uid;
15637c478bd9Sstevel@tonic-gate 	Gen.g_gid = SrcSt.st_gid;
15647c478bd9Sstevel@tonic-gate 	Gen.g_dev = SrcSt.st_dev;
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 	if (Use_old_stat) {
15677c478bd9Sstevel@tonic-gate 		/* -Hodc */
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate 		sl_info_t *p = sl_search(dev, ino);
15707c478bd9Sstevel@tonic-gate 		Gen.g_ino = p ? p->sl_ino2 : -1;
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 		if (Gen.g_ino == (ulong_t)-1) {
15737c478bd9Sstevel@tonic-gate 			msg(ERR, "%s%s%s: cannot be archived - inode too big "
15747c478bd9Sstevel@tonic-gate 			    "for -Hodc format",
15757c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
15767c478bd9Sstevel@tonic-gate 			    Gen.g_nam_p : Gen.g_attrfnam_p,
15777c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
15787c478bd9Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
15797c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
15807c478bd9Sstevel@tonic-gate 			    "" : Gen.g_attrnam_p);
15817c478bd9Sstevel@tonic-gate 			return (0);
15827c478bd9Sstevel@tonic-gate 		}
15837c478bd9Sstevel@tonic-gate 	} else {
15847c478bd9Sstevel@tonic-gate 		Gen.g_ino = SrcSt.st_ino;
15857c478bd9Sstevel@tonic-gate 	}
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 	Gen.g_mode = SrcSt.st_mode;
15887c478bd9Sstevel@tonic-gate 	Gen.g_mtime = SrcSt.st_mtime;
15897c478bd9Sstevel@tonic-gate 	Gen.g_nlink = (Adir) ? SrcSt.st_nlink
15907c478bd9Sstevel@tonic-gate 	    : sl_numlinks(dev, ino);
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 	if (ftype == S_IFREG || ftype == S_IFLNK)
15937c478bd9Sstevel@tonic-gate 		Gen.g_filesz = (off_t)SrcSt.st_size;
15947c478bd9Sstevel@tonic-gate 	else
15957c478bd9Sstevel@tonic-gate 		Gen.g_filesz = (off_t)0;
15967c478bd9Sstevel@tonic-gate 	Gen.g_rdev = SrcSt.st_rdev;
15977c478bd9Sstevel@tonic-gate 	return (1);
15987c478bd9Sstevel@tonic-gate }
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate /*
16017c478bd9Sstevel@tonic-gate  * creat_lnk: Create a link from the existing name1_p to name2_p.
16027c478bd9Sstevel@tonic-gate  */
16037c478bd9Sstevel@tonic-gate 
16047c478bd9Sstevel@tonic-gate static
16057c478bd9Sstevel@tonic-gate int
16067c478bd9Sstevel@tonic-gate creat_lnk(int dirfd, char *name1_p, char *name2_p)
16077c478bd9Sstevel@tonic-gate {
16087c478bd9Sstevel@tonic-gate 	int cnt = 0;
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate 	do {
16117c478bd9Sstevel@tonic-gate 		errno = 0;
16127c478bd9Sstevel@tonic-gate 		if (!link(name1_p, name2_p)) {
16137c478bd9Sstevel@tonic-gate 			if (aclp != NULL) {
1614fa9e4066Sahrens 				acl_free(aclp);
16157c478bd9Sstevel@tonic-gate 				aclp = NULL;
1616fa9e4066Sahrens 				acl_is_set = 0;
16177c478bd9Sstevel@tonic-gate 			}
16187c478bd9Sstevel@tonic-gate 			cnt = 0;
16197c478bd9Sstevel@tonic-gate 			break;
1620647ab8f4Sceastha 		} else if ((errno == EEXIST) && (cnt == 0)) {
16217c478bd9Sstevel@tonic-gate 			struct stat lsb1;
16227c478bd9Sstevel@tonic-gate 			struct stat lsb2;
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate 			/*
16257c478bd9Sstevel@tonic-gate 			 * Check to see if we are trying to link this
16267c478bd9Sstevel@tonic-gate 			 * file to itself.  If so, count the effort as
16277c478bd9Sstevel@tonic-gate 			 * successful.  If the two files are different,
16287c478bd9Sstevel@tonic-gate 			 * or if either lstat is unsuccessful, proceed
16297c478bd9Sstevel@tonic-gate 			 * as we would have otherwise; the appropriate
16307c478bd9Sstevel@tonic-gate 			 * error will be reported subsequently.
16317c478bd9Sstevel@tonic-gate 			 */
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate 			if (lstat(name1_p, &lsb1) != 0) {
16347c478bd9Sstevel@tonic-gate 				msg(ERR, "Cannot lstat source file %s",
16357c478bd9Sstevel@tonic-gate 				    name1_p);
16367c478bd9Sstevel@tonic-gate 			} else {
16377c478bd9Sstevel@tonic-gate 				if (lstat(name2_p, &lsb2) != 0) {
16387c478bd9Sstevel@tonic-gate 					msg(ERR, "Cannot lstat "
1639647ab8f4Sceastha 					    "destination file %s", name2_p);
16407c478bd9Sstevel@tonic-gate 				} else {
1641647ab8f4Sceastha 					if (lsb1.st_dev == lsb2.st_dev &&
1642647ab8f4Sceastha 					    lsb1.st_ino == lsb2.st_ino) {
1643647ab8f4Sceastha 						VERBOSE((Args & (OCv | OCV)),
16447c478bd9Sstevel@tonic-gate 						    name2_p);
16457c478bd9Sstevel@tonic-gate 						return (0);
16467c478bd9Sstevel@tonic-gate 					}
16477c478bd9Sstevel@tonic-gate 				}
16487c478bd9Sstevel@tonic-gate 			}
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 			if (!(Args & OCu) && G_p->g_mtime <= DesSt.st_mtime)
1651647ab8f4Sceastha 				msg(ERR, "Existing \"%s\" same age or newer",
16527c478bd9Sstevel@tonic-gate 				    name2_p);
16537c478bd9Sstevel@tonic-gate 			else if (unlinkat(dirfd, get_component(name2_p), 0) < 0)
16547c478bd9Sstevel@tonic-gate 				msg(ERRN, "Error cannot unlink \"%s\"",
16557c478bd9Sstevel@tonic-gate 				    name2_p);
16567c478bd9Sstevel@tonic-gate 		}
16577c478bd9Sstevel@tonic-gate 		cnt++;
16587c478bd9Sstevel@tonic-gate 	} while ((cnt < 2) && missdir(name2_p) == 0);
16597c478bd9Sstevel@tonic-gate 	if (!cnt) {
16607c478bd9Sstevel@tonic-gate 		char *newname;
16617c478bd9Sstevel@tonic-gate 		char *fromname;
16627c478bd9Sstevel@tonic-gate 		char *attrname;
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate 		newname = name2_p;
16657c478bd9Sstevel@tonic-gate 		fromname = name1_p;
16667c478bd9Sstevel@tonic-gate 		attrname = Gen.g_attrnam_p;
16677c478bd9Sstevel@tonic-gate 		if (attrname) {
16687c478bd9Sstevel@tonic-gate 			if (Args & OCp) {
16697c478bd9Sstevel@tonic-gate 				newname = fromname = Fullnam_p;
16707c478bd9Sstevel@tonic-gate 			} else {
16717c478bd9Sstevel@tonic-gate 				newname = Gen.g_attrfnam_p;
16727c478bd9Sstevel@tonic-gate 			}
16737c478bd9Sstevel@tonic-gate 		}
16747c478bd9Sstevel@tonic-gate 		if (Args & OCv) {
16757c478bd9Sstevel@tonic-gate 			(void) fprintf(Err_p,
16767c478bd9Sstevel@tonic-gate 			    gettext("%s%s%s linked to %s%s%s\n"), newname,
16777c478bd9Sstevel@tonic-gate 			    (attrname == (char *)NULL) ?
16787c478bd9Sstevel@tonic-gate 			    "" : gettext(" attribute "),
16797c478bd9Sstevel@tonic-gate 			    (attrname == (char *)NULL) ?
16807c478bd9Sstevel@tonic-gate 			    "" : attrname, fromname,
16817c478bd9Sstevel@tonic-gate 			    (attrname == (char *)NULL) ?
16827c478bd9Sstevel@tonic-gate 			    "" : gettext(" attribute "),
16837c478bd9Sstevel@tonic-gate 			    (attrname == (char *)NULL) ?
16847c478bd9Sstevel@tonic-gate 			    "" : name1_p);
16857c478bd9Sstevel@tonic-gate 		}
16867c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), newname);
16877c478bd9Sstevel@tonic-gate 	} else if (cnt == 1)
16887c478bd9Sstevel@tonic-gate 		msg(ERRN,
16897c478bd9Sstevel@tonic-gate 		    "Unable to create directory for \"%s\"", name2_p);
16907c478bd9Sstevel@tonic-gate 	else if (cnt == 2)
16917c478bd9Sstevel@tonic-gate 		msg(ERRN,
16927c478bd9Sstevel@tonic-gate 		    "Cannot link \"%s\" and \"%s\"", name1_p, name2_p);
16937c478bd9Sstevel@tonic-gate 	return (cnt);
16947c478bd9Sstevel@tonic-gate }
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate /*
16977c478bd9Sstevel@tonic-gate  * creat_spec:
16987c478bd9Sstevel@tonic-gate  *   Create one of the following:
16997c478bd9Sstevel@tonic-gate  *       directory
17007c478bd9Sstevel@tonic-gate  *       character special file
17017c478bd9Sstevel@tonic-gate  *       block special file
17027c478bd9Sstevel@tonic-gate  *       fifo
1703cdd68f5aSceastha  *	 socket
17047c478bd9Sstevel@tonic-gate  */
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate static int
17077c478bd9Sstevel@tonic-gate creat_spec(int dirfd)
17087c478bd9Sstevel@tonic-gate {
17097c478bd9Sstevel@tonic-gate 	char *nam_p;
17107c478bd9Sstevel@tonic-gate 	int cnt, result, rv = 0;
17117c478bd9Sstevel@tonic-gate 	char *curdir;
17127c478bd9Sstevel@tonic-gate 	char *lastslash;
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	Do_rename = 0;	/* creat_tmp() may reset this */
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 	if (Args & OCp) {
17177c478bd9Sstevel@tonic-gate 		nam_p = Fullnam_p;
17187c478bd9Sstevel@tonic-gate 	} else {
17197c478bd9Sstevel@tonic-gate 		nam_p = G_p->g_nam_p;
17207c478bd9Sstevel@tonic-gate 	}
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 	/*
17237c478bd9Sstevel@tonic-gate 	 * Is this the extraction of the hidden attribute directory?
17247c478bd9Sstevel@tonic-gate 	 * If so then just set the mode/times correctly, and return.
17257c478bd9Sstevel@tonic-gate 	 */
17267c478bd9Sstevel@tonic-gate 
17277c478bd9Sstevel@tonic-gate 	if (Hiddendir) {
1728647ab8f4Sceastha 		if (Args & OCR) {
1729647ab8f4Sceastha 			if (fchownat(dirfd, ".", Rpw_p->pw_uid,
1730647ab8f4Sceastha 			    Rpw_p->pw_gid, 0) != 0) {
1731647ab8f4Sceastha 				msg(ERRN,
1732647ab8f4Sceastha 				    "Cannot chown() \"attribute directory of "
1733647ab8f4Sceastha 				    "file %s\"", G_p->g_attrfnam_p);
1734647ab8f4Sceastha 			}
1735647ab8f4Sceastha 		} else if ((fchownat(dirfd, ".", G_p->g_uid,
1736647ab8f4Sceastha 		    G_p->g_gid, 0) != 0) && privileged) {
17377c478bd9Sstevel@tonic-gate 			msg(ERRN,
17387c478bd9Sstevel@tonic-gate 			    "Cannot chown() \"attribute directory of "
17397c478bd9Sstevel@tonic-gate 			    "file %s\"", G_p->g_attrfnam_p);
17407c478bd9Sstevel@tonic-gate 		}
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate 		if (fchmod(dirfd, G_p->g_mode) != 0) {
17437c478bd9Sstevel@tonic-gate 			msg(ERRN,
17447c478bd9Sstevel@tonic-gate 			    "Cannot chmod() \"attribute directory of "
17457c478bd9Sstevel@tonic-gate 			    "file %s\"", G_p->g_attrfnam_p);
17467c478bd9Sstevel@tonic-gate 		}
17477c478bd9Sstevel@tonic-gate 
1748fa9e4066Sahrens 		acl_is_set = 0;
17497c478bd9Sstevel@tonic-gate 		if (Pflag && aclp != NULL) {
1750fa9e4066Sahrens 			if (facl_set(dirfd, aclp) < 0) {
17517c478bd9Sstevel@tonic-gate 				msg(ERRN,
17527c478bd9Sstevel@tonic-gate 				    "failed to set acl on attribute"
17537c478bd9Sstevel@tonic-gate 				    " directory of %s ", G_p->g_attrfnam_p);
17547c478bd9Sstevel@tonic-gate 			} else {
1755fa9e4066Sahrens 				acl_is_set = 1;
17567c478bd9Sstevel@tonic-gate 			}
1757fa9e4066Sahrens 			acl_free(aclp);
17587c478bd9Sstevel@tonic-gate 			aclp = NULL;
17597c478bd9Sstevel@tonic-gate 		}
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 		return (1);
17627c478bd9Sstevel@tonic-gate 	}
17637c478bd9Sstevel@tonic-gate 
17647c478bd9Sstevel@tonic-gate 	result = stat(nam_p, &DesSt);
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 	if (ustar_dir() || Adir) {
17677c478bd9Sstevel@tonic-gate 		/*
17687c478bd9Sstevel@tonic-gate 		 *  The archive file is a directory.
17697c478bd9Sstevel@tonic-gate 		 *  Skip "." and ".."
17707c478bd9Sstevel@tonic-gate 		 */
17717c478bd9Sstevel@tonic-gate 
17727c478bd9Sstevel@tonic-gate 		curdir = strrchr(nam_p, '.');
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate 		if (curdir != NULL && curdir[1] == NULL) {
17757c478bd9Sstevel@tonic-gate 			lastslash = strrchr(nam_p, '/');
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 			if (lastslash != NULL) {
17787c478bd9Sstevel@tonic-gate 				lastslash++;
17797c478bd9Sstevel@tonic-gate 			} else {
17807c478bd9Sstevel@tonic-gate 				lastslash = nam_p;
17817c478bd9Sstevel@tonic-gate 			}
17827c478bd9Sstevel@tonic-gate 
17837c478bd9Sstevel@tonic-gate 			if (!(strcmp(lastslash, ".")) ||
17847c478bd9Sstevel@tonic-gate 			    !(strcmp(lastslash, ".."))) {
17857c478bd9Sstevel@tonic-gate 				return (1);
17867c478bd9Sstevel@tonic-gate 			}
17877c478bd9Sstevel@tonic-gate 		}
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate 		if (result == 0) {
17907c478bd9Sstevel@tonic-gate 			/* A file by the same name exists. */
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate 			/* Take care of ACLs */
1793fa9e4066Sahrens 			acl_is_set = 0;
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 			if (Pflag && aclp != NULL) {
1796fa9e4066Sahrens 				if (acl_set(nam_p, aclp) < 0) {
17977c478bd9Sstevel@tonic-gate 					msg(ERRN,
17987c478bd9Sstevel@tonic-gate 					    "\"%s\": failed to set acl",
17997c478bd9Sstevel@tonic-gate 					    nam_p);
18007c478bd9Sstevel@tonic-gate 				} else {
1801fa9e4066Sahrens 					acl_is_set = 1;
18027c478bd9Sstevel@tonic-gate 				}
18037c478bd9Sstevel@tonic-gate 
1804fa9e4066Sahrens 				acl_free(aclp);
18057c478bd9Sstevel@tonic-gate 				aclp = NULL;
18067c478bd9Sstevel@tonic-gate 			}
18077c478bd9Sstevel@tonic-gate 			if (Args & OCd) {
18087c478bd9Sstevel@tonic-gate 				/*
18097c478bd9Sstevel@tonic-gate 				 * We are creating directories.  Keep the
18107c478bd9Sstevel@tonic-gate 				 * existing file.
18117c478bd9Sstevel@tonic-gate 				 */
18127c478bd9Sstevel@tonic-gate 
18137c478bd9Sstevel@tonic-gate 				rstfiles(U_KEEP, dirfd);
18147c478bd9Sstevel@tonic-gate 			}
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 			/* Report success. */
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 			return (1);
18197c478bd9Sstevel@tonic-gate 		}
18207c478bd9Sstevel@tonic-gate 	} else {
18217c478bd9Sstevel@tonic-gate 		/* The archive file is not a directory. */
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 		if (result == 0) {
18247c478bd9Sstevel@tonic-gate 			/*
18257c478bd9Sstevel@tonic-gate 			 * A file by the same name exists.  Move it to a
18267c478bd9Sstevel@tonic-gate 			 * temporary file.
18277c478bd9Sstevel@tonic-gate 			 */
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 			if (creat_tmp(nam_p) < 0) {
18307c478bd9Sstevel@tonic-gate 				/*
18317c478bd9Sstevel@tonic-gate 				 * We weren't able to create the temp file.
18327c478bd9Sstevel@tonic-gate 				 * Report failure.
18337c478bd9Sstevel@tonic-gate 				 */
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate 				return (0);
18367c478bd9Sstevel@tonic-gate 			}
18377c478bd9Sstevel@tonic-gate 		}
18387c478bd9Sstevel@tonic-gate 	}
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate 	/*
18417c478bd9Sstevel@tonic-gate 	 * This pile tries to create the file directly, and, if there is a
18427c478bd9Sstevel@tonic-gate 	 * problem, creates missing directories, and then tries to create the
18437c478bd9Sstevel@tonic-gate 	 * file again.  Two strikes and you're out.
18447c478bd9Sstevel@tonic-gate 	 */
18457c478bd9Sstevel@tonic-gate 
18467c478bd9Sstevel@tonic-gate 	cnt = 0;
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 	do {
18497c478bd9Sstevel@tonic-gate 		if (ustar_dir() || Adir) {
18507c478bd9Sstevel@tonic-gate 			/* The archive file is a directory. */
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate 			result = mkdir(nam_p, G_p->g_mode);
18537c478bd9Sstevel@tonic-gate 		} else if (ustar_spec() || Aspec) {
18547c478bd9Sstevel@tonic-gate 			/*
18557c478bd9Sstevel@tonic-gate 			 * The archive file is block special,
1856cdd68f5aSceastha 			 * char special, socket, or a fifo.
1857cdd68f5aSceastha 			 * Note that, for a socket, the third
1858cdd68f5aSceastha 			 * parameter to mknod() is ignored.
18597c478bd9Sstevel@tonic-gate 			 */
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 			result = mknod(nam_p, (int)G_p->g_mode,
18627c478bd9Sstevel@tonic-gate 			    (int)G_p->g_rdev);
18637c478bd9Sstevel@tonic-gate 		}
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 		if (result >= 0) {
18667c478bd9Sstevel@tonic-gate 			/*
18677c478bd9Sstevel@tonic-gate 			 * The file creation succeeded.  Take care of the ACLs.
18687c478bd9Sstevel@tonic-gate 			 */
18697c478bd9Sstevel@tonic-gate 
1870fa9e4066Sahrens 			acl_is_set = 0;
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 			if (Pflag && aclp != NULL) {
1873fa9e4066Sahrens 				if (acl_set(nam_p, aclp) < 0) {
18747c478bd9Sstevel@tonic-gate 					msg(ERRN,
18757c478bd9Sstevel@tonic-gate 					    "\"%s\": failed to set acl", nam_p);
18767c478bd9Sstevel@tonic-gate 				} else {
1877fa9e4066Sahrens 					acl_is_set = 1;
18787c478bd9Sstevel@tonic-gate 				}
18797c478bd9Sstevel@tonic-gate 
1880fa9e4066Sahrens 				acl_free(aclp);
18817c478bd9Sstevel@tonic-gate 				aclp = NULL;
18827c478bd9Sstevel@tonic-gate 			}
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 			cnt = 0;
18857c478bd9Sstevel@tonic-gate 			break;
18867c478bd9Sstevel@tonic-gate 		}
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 		cnt++;
18897c478bd9Sstevel@tonic-gate 	} while (cnt < 2 && missdir(nam_p) == 0);
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 	switch (cnt) {
18927c478bd9Sstevel@tonic-gate 	case 0:
18937c478bd9Sstevel@tonic-gate 		rv = 1;
18947c478bd9Sstevel@tonic-gate 		rstfiles(U_OVER, dirfd);
18957c478bd9Sstevel@tonic-gate 		break;
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 	case 1:
18987c478bd9Sstevel@tonic-gate 		msg(ERRN,
18997c478bd9Sstevel@tonic-gate 		    "Cannot create directory for \"%s\"", nam_p);
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate 		if (*Over_p == '\0') {
19027c478bd9Sstevel@tonic-gate 			rstfiles(U_KEEP, dirfd);
19037c478bd9Sstevel@tonic-gate 		}
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate 		break;
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate 	case 2:
19087c478bd9Sstevel@tonic-gate 		if (ustar_dir() || Adir) {
19097c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot create directory \"%s\"", nam_p);
19107c478bd9Sstevel@tonic-gate 		} else if (ustar_spec() || Aspec) {
19117c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot mknod() \"%s\"", nam_p);
19127c478bd9Sstevel@tonic-gate 		}
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate 		if (*Over_p == '\0') {
19157c478bd9Sstevel@tonic-gate 			rstfiles(U_KEEP, dirfd);
19167c478bd9Sstevel@tonic-gate 		}
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate 		break;
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate 	default:
19217c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible case.");
19227c478bd9Sstevel@tonic-gate 	}
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate 	return (rv);
19257c478bd9Sstevel@tonic-gate }
19267c478bd9Sstevel@tonic-gate 
19277c478bd9Sstevel@tonic-gate /*
19287c478bd9Sstevel@tonic-gate  * creat_tmp:
19297c478bd9Sstevel@tonic-gate  */
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate static int
19327c478bd9Sstevel@tonic-gate creat_tmp(char *nam_p)
19337c478bd9Sstevel@tonic-gate {
19347c478bd9Sstevel@tonic-gate 	char *t_p;
19357c478bd9Sstevel@tonic-gate 	int	cwd;
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate 	if ((Args & OCp) && G_p->g_ino == DesSt.st_ino &&
19387c478bd9Sstevel@tonic-gate 	    G_p->g_dev == DesSt.st_dev) {
19397c478bd9Sstevel@tonic-gate 		msg(ERR, "Attempt to pass a file to itself.");
19407c478bd9Sstevel@tonic-gate 		return (-1);
19417c478bd9Sstevel@tonic-gate 	}
19427c478bd9Sstevel@tonic-gate 
19437c478bd9Sstevel@tonic-gate 	if (G_p->g_mtime <= DesSt.st_mtime && !(Args & OCu)) {
19447c478bd9Sstevel@tonic-gate 		msg(ERR, "Existing \"%s\" same age or newer", nam_p);
19457c478bd9Sstevel@tonic-gate 		return (-1);
19467c478bd9Sstevel@tonic-gate 	}
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate 	/* Make the temporary file name. */
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate 	(void) strcpy(Over_p, nam_p);
19517c478bd9Sstevel@tonic-gate 	t_p = Over_p + strlen(Over_p);
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 	while (t_p != Over_p) {
19547c478bd9Sstevel@tonic-gate 		if (*(t_p - 1) == '/')
19557c478bd9Sstevel@tonic-gate 			break;
19567c478bd9Sstevel@tonic-gate 		t_p--;
19577c478bd9Sstevel@tonic-gate 	}
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 	(void) strcpy(t_p, "XXXXXX");
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
19627c478bd9Sstevel@tonic-gate 		/*
19637c478bd9Sstevel@tonic-gate 		 * Save our current directory, so we can go into
19647c478bd9Sstevel@tonic-gate 		 * the attribute directory to make the temp file
19657c478bd9Sstevel@tonic-gate 		 * and then return.
19667c478bd9Sstevel@tonic-gate 		 */
19677c478bd9Sstevel@tonic-gate 
19687c478bd9Sstevel@tonic-gate 		cwd = save_cwd();
19697c478bd9Sstevel@tonic-gate 		(void) fchdir(G_p->g_dirfd);
19707c478bd9Sstevel@tonic-gate 	}
19717c478bd9Sstevel@tonic-gate 
19727c478bd9Sstevel@tonic-gate 	(void) mktemp(Over_p);
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
19757c478bd9Sstevel@tonic-gate 		/* Return to the current directory. */
19767c478bd9Sstevel@tonic-gate 
19777c478bd9Sstevel@tonic-gate 		rest_cwd(cwd);
19787c478bd9Sstevel@tonic-gate 	}
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate 	if (*Over_p == '\0') {
19817c478bd9Sstevel@tonic-gate 		/* mktemp reports a failure. */
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate 		msg(ERR, "Cannot get temporary file name.");
19847c478bd9Sstevel@tonic-gate 		return (-1);
19857c478bd9Sstevel@tonic-gate 	}
19867c478bd9Sstevel@tonic-gate 
19877c478bd9Sstevel@tonic-gate 	/*
19887c478bd9Sstevel@tonic-gate 	 * If it's a regular file, write to the temporary file, and then rename
1989*da6c28aaSamw 	 * in order to accommodate potential executables.
19907c478bd9Sstevel@tonic-gate 	 *
19917c478bd9Sstevel@tonic-gate 	 * Note: g_typeflag is only defined (set) for USTAR archive types.  It
19927c478bd9Sstevel@tonic-gate 	 * defaults to 0 in the cpio-format-regular file case, so this test
19937c478bd9Sstevel@tonic-gate 	 * succeeds.
19947c478bd9Sstevel@tonic-gate 	 */
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate 	if (G_p->g_typeflag == 0 &&
19977c478bd9Sstevel@tonic-gate 	    (DesSt.st_mode & (ulong_t)Ftype) == S_IFREG &&
19987c478bd9Sstevel@tonic-gate 	    (G_p->g_mode & (ulong_t)Ftype) == S_IFREG) {
19997c478bd9Sstevel@tonic-gate 		/*
20007c478bd9Sstevel@tonic-gate 		 * The archive file and the filesystem file are both regular
20017c478bd9Sstevel@tonic-gate 		 * files.  We write to the temporary file in this case.
20027c478bd9Sstevel@tonic-gate 		 */
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate 		if (Args & OCp) {
20057c478bd9Sstevel@tonic-gate 			if (G_p->g_attrnam_p == (char *)NULL) {
20067c478bd9Sstevel@tonic-gate 				Fullnam_p = Over_p;
20077c478bd9Sstevel@tonic-gate 			} else {
20087c478bd9Sstevel@tonic-gate 				Attrfile_p = Over_p;
20097c478bd9Sstevel@tonic-gate 			}
20107c478bd9Sstevel@tonic-gate 		} else {
20117c478bd9Sstevel@tonic-gate 			G_p->g_nam_p = Over_p;
20127c478bd9Sstevel@tonic-gate 			if (G_p->g_attrnam_p != (char *)NULL) {
20137c478bd9Sstevel@tonic-gate 				Attrfile_p = Over_p;
20147c478bd9Sstevel@tonic-gate 			}
20157c478bd9Sstevel@tonic-gate 		}
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate 		if (G_p->g_attrnam_p == (char *)NULL) {
20187c478bd9Sstevel@tonic-gate 			Over_p = nam_p;
20197c478bd9Sstevel@tonic-gate 		} else {
20207c478bd9Sstevel@tonic-gate 			Over_p = G_p->g_attrnam_p;
20217c478bd9Sstevel@tonic-gate 		}
20227c478bd9Sstevel@tonic-gate 
20237c478bd9Sstevel@tonic-gate 		Do_rename = 1;
20247c478bd9Sstevel@tonic-gate 	} else {
20257c478bd9Sstevel@tonic-gate 		/*
20267c478bd9Sstevel@tonic-gate 		 * Either the archive file or the filesystem file is not a
20277c478bd9Sstevel@tonic-gate 		 * regular file.
20287c478bd9Sstevel@tonic-gate 		 */
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate 		Do_rename = 0;
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 		if (S_ISDIR(DesSt.st_mode)) {
20337c478bd9Sstevel@tonic-gate 			/*
20347c478bd9Sstevel@tonic-gate 			 * The filesystem file is a directory.
20357c478bd9Sstevel@tonic-gate 			 *
20367c478bd9Sstevel@tonic-gate 			 * Save the current working directory because we will
20377c478bd9Sstevel@tonic-gate 			 * want to restore it back just in case remove_dir()
20387c478bd9Sstevel@tonic-gate 			 * fails or get confused about where we should be.
20397c478bd9Sstevel@tonic-gate 			 */
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate 			*Over_p = '\0';
20427c478bd9Sstevel@tonic-gate 			cwd = save_cwd();
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate 			if (remove_dir(nam_p) < 0) {
20457c478bd9Sstevel@tonic-gate 				msg(ERRN,
20467c478bd9Sstevel@tonic-gate 				    "Cannot remove the directory \"%s\"",
20477c478bd9Sstevel@tonic-gate 				    nam_p);
20487c478bd9Sstevel@tonic-gate 				/*
20497c478bd9Sstevel@tonic-gate 				 * Restore working directory back to the one
20507c478bd9Sstevel@tonic-gate 				 * saved earlier.
20517c478bd9Sstevel@tonic-gate 				 */
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate 				rest_cwd(cwd);
20547c478bd9Sstevel@tonic-gate 				return (-1);
20557c478bd9Sstevel@tonic-gate 			}
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate 			/*
20587c478bd9Sstevel@tonic-gate 			 * Restore working directory back to the one
20597c478bd9Sstevel@tonic-gate 			 * saved earlier
20607c478bd9Sstevel@tonic-gate 			 */
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 			rest_cwd(cwd);
20637c478bd9Sstevel@tonic-gate 		} else {
20647c478bd9Sstevel@tonic-gate 			/*
20657c478bd9Sstevel@tonic-gate 			 * The file is not a directory. Will use the original
20667c478bd9Sstevel@tonic-gate 			 * link/unlink construct, however, if the file is
20677c478bd9Sstevel@tonic-gate 			 * namefs, link would fail with EXDEV. Therefore, we
20687c478bd9Sstevel@tonic-gate 			 * use rename() first to back up the file.
20697c478bd9Sstevel@tonic-gate 			 */
20707c478bd9Sstevel@tonic-gate 			if (rename(nam_p, Over_p) < 0) {
20717c478bd9Sstevel@tonic-gate 				/*
20727c478bd9Sstevel@tonic-gate 				 * If rename failed, try old construction
20737c478bd9Sstevel@tonic-gate 				 * method.
20747c478bd9Sstevel@tonic-gate 				 */
20757c478bd9Sstevel@tonic-gate 				if (link(nam_p, Over_p) < 0) {
20767c478bd9Sstevel@tonic-gate 					msg(ERRN,
20777c478bd9Sstevel@tonic-gate 					    "Cannot create temporary file");
20787c478bd9Sstevel@tonic-gate 					*Over_p = '\0';
20797c478bd9Sstevel@tonic-gate 					return (-1);
20807c478bd9Sstevel@tonic-gate 				}
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate 				if (unlink(nam_p) < 0) {
20837c478bd9Sstevel@tonic-gate 					msg(ERRN,
20847c478bd9Sstevel@tonic-gate 					    "Cannot unlink() current \"%s\"",
20857c478bd9Sstevel@tonic-gate 					    nam_p);
20867c478bd9Sstevel@tonic-gate 					(void) unlink(Over_p);
20877c478bd9Sstevel@tonic-gate 					*Over_p = '\0';
20887c478bd9Sstevel@tonic-gate 					return (-1);
20897c478bd9Sstevel@tonic-gate 				}
20907c478bd9Sstevel@tonic-gate 			}
20917c478bd9Sstevel@tonic-gate 		}
20927c478bd9Sstevel@tonic-gate 	}
20937c478bd9Sstevel@tonic-gate 
20947c478bd9Sstevel@tonic-gate 	return (1);
20957c478bd9Sstevel@tonic-gate }
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate /*
20987c478bd9Sstevel@tonic-gate  * data_in:  If proc_mode == P_PROC, bread() the file's data from the archive
20997c478bd9Sstevel@tonic-gate  * and write(2) it to the open fdes gotten from openout().  If proc_mode ==
21007c478bd9Sstevel@tonic-gate  * P_SKIP, or becomes P_SKIP (due to errors etc), bread(2) the file's data
21017c478bd9Sstevel@tonic-gate  * and ignore it.  If the user specified any of the "swap" options (b, s or S),
21027c478bd9Sstevel@tonic-gate  * and the length of the file is not appropriate for that action, do not
21037c478bd9Sstevel@tonic-gate  * perform the "swap", otherwise perform the action on a buffer by buffer basis.
21047c478bd9Sstevel@tonic-gate  * If the CRC header was selected, calculate a running checksum as each buffer
21057c478bd9Sstevel@tonic-gate  * is processed.
21067c478bd9Sstevel@tonic-gate  */
21077c478bd9Sstevel@tonic-gate 
21087c478bd9Sstevel@tonic-gate static void
21097c478bd9Sstevel@tonic-gate data_in(int proc_mode)
21107c478bd9Sstevel@tonic-gate {
21117c478bd9Sstevel@tonic-gate 	char *nam_p;
21127c478bd9Sstevel@tonic-gate 	int cnt, pad;
21137c478bd9Sstevel@tonic-gate 	long cksumval = 0L;
21147c478bd9Sstevel@tonic-gate 	off_t filesz;
21157c478bd9Sstevel@tonic-gate 	int rv, swapfile = 0;
21167c478bd9Sstevel@tonic-gate 	int compress_flag = 0;	/* if the file is compressed */
21177c478bd9Sstevel@tonic-gate 	int cstatus = 0;
21187c478bd9Sstevel@tonic-gate 	FILE *pipef;		/* pipe for bar to do de-compression */
21197c478bd9Sstevel@tonic-gate 
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
21227c478bd9Sstevel@tonic-gate 		nam_p = G_p->g_attrnam_p;
21237c478bd9Sstevel@tonic-gate 	} else {
21247c478bd9Sstevel@tonic-gate 		nam_p = G_p->g_nam_p;
21257c478bd9Sstevel@tonic-gate 	}
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate 
21287c478bd9Sstevel@tonic-gate 	if (((G_p->g_mode & Ftype) == S_IFLNK && proc_mode != P_SKIP) ||
21297c478bd9Sstevel@tonic-gate 	    (Hdr_type == BAR && bar_linkflag == '2' && proc_mode != P_SKIP)) {
21307c478bd9Sstevel@tonic-gate 		proc_mode = P_SKIP;
21317c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
21327c478bd9Sstevel@tonic-gate 	}
21337c478bd9Sstevel@tonic-gate 	if (Args & (OCb | OCs | OCS)) { /* verfify that swapping is possible */
21347c478bd9Sstevel@tonic-gate 		swapfile = 1;
21357c478bd9Sstevel@tonic-gate 		if (Args & (OCs | OCb) && G_p->g_filesz % 2) {
21367c478bd9Sstevel@tonic-gate 			msg(ERR,
21377c478bd9Sstevel@tonic-gate 			    "Cannot swap bytes of \"%s\", odd number of bytes",
21387c478bd9Sstevel@tonic-gate 			    nam_p);
21397c478bd9Sstevel@tonic-gate 			swapfile = 0;
21407c478bd9Sstevel@tonic-gate 		}
21417c478bd9Sstevel@tonic-gate 		if (Args & (OCS | OCb) && G_p->g_filesz % 4) {
21427c478bd9Sstevel@tonic-gate 			msg(ERR,
21437c478bd9Sstevel@tonic-gate 			    "Cannot swap halfwords of \"%s\", odd number "
21447c478bd9Sstevel@tonic-gate 			    "of halfwords", nam_p);
21457c478bd9Sstevel@tonic-gate 			swapfile = 0;
21467c478bd9Sstevel@tonic-gate 		}
21477c478bd9Sstevel@tonic-gate 	}
21487c478bd9Sstevel@tonic-gate 	filesz = G_p->g_filesz;
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 	/* This writes out the file from the archive */
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate 	while (filesz > 0) {
21537c478bd9Sstevel@tonic-gate 		cnt = (int)(filesz > CPIOBSZ) ? CPIOBSZ : filesz;
21547c478bd9Sstevel@tonic-gate 		FILL(cnt);
21557c478bd9Sstevel@tonic-gate 		if (proc_mode != P_SKIP) {
21567c478bd9Sstevel@tonic-gate 			if (Hdr_type == CRC)
21577c478bd9Sstevel@tonic-gate 				cksumval += cksum(CRC, cnt, NULL);
21587c478bd9Sstevel@tonic-gate 			if (swapfile)
21597c478bd9Sstevel@tonic-gate 				swap(Buffr.b_out_p, cnt);
21607c478bd9Sstevel@tonic-gate 			errno = 0;
21617c478bd9Sstevel@tonic-gate 
21627c478bd9Sstevel@tonic-gate 			/*
21637c478bd9Sstevel@tonic-gate 			 * if the bar archive is compressed, set up a pipe and
21647c478bd9Sstevel@tonic-gate 			 * do the de-compression while reading in the file
21657c478bd9Sstevel@tonic-gate 			 */
21667c478bd9Sstevel@tonic-gate 			if (Hdr_type == BAR) {
21677c478bd9Sstevel@tonic-gate 				if (compress_flag == 0 && Compressed) {
21687c478bd9Sstevel@tonic-gate 					setup_uncompress(&pipef);
21697c478bd9Sstevel@tonic-gate 					compress_flag++;
21707c478bd9Sstevel@tonic-gate 				}
21717c478bd9Sstevel@tonic-gate 
21727c478bd9Sstevel@tonic-gate 			}
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 			rv = write(Ofile, Buffr.b_out_p, cnt);
21757c478bd9Sstevel@tonic-gate 			if (rv < cnt) {
21767c478bd9Sstevel@tonic-gate 				if (rv < 0)
21777c478bd9Sstevel@tonic-gate 					msg(ERRN, "Cannot write \"%s\"", nam_p);
21787c478bd9Sstevel@tonic-gate 				else
21797c478bd9Sstevel@tonic-gate 					msg(EXTN, "Cannot write \"%s\"", nam_p);
21807c478bd9Sstevel@tonic-gate 				proc_mode = P_SKIP;
21817c478bd9Sstevel@tonic-gate 				rstfiles(U_KEEP, G_p->g_dirfd);
21827c478bd9Sstevel@tonic-gate 				cstatus = close(Ofile);
21837c478bd9Sstevel@tonic-gate 				Ofile = 0;
21847c478bd9Sstevel@tonic-gate 				if (cstatus != 0) {
21857c478bd9Sstevel@tonic-gate 					msg(EXTN, "close error");
21867c478bd9Sstevel@tonic-gate 				}
21877c478bd9Sstevel@tonic-gate 			}
21887c478bd9Sstevel@tonic-gate 		}
21897c478bd9Sstevel@tonic-gate 		Buffr.b_out_p += cnt;
21907c478bd9Sstevel@tonic-gate 		Buffr.b_cnt -= (long)cnt;
21917c478bd9Sstevel@tonic-gate 		filesz -= (off_t)cnt;
21927c478bd9Sstevel@tonic-gate 	} /* filesz */
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate 	pad = (Pad_val + 1 - (G_p->g_filesz & Pad_val)) & Pad_val;
21957c478bd9Sstevel@tonic-gate 	if (pad != 0) {
21967c478bd9Sstevel@tonic-gate 		FILL(pad);
21977c478bd9Sstevel@tonic-gate 		Buffr.b_out_p += pad;
21987c478bd9Sstevel@tonic-gate 		Buffr.b_cnt -= pad;
21997c478bd9Sstevel@tonic-gate 	}
22007c478bd9Sstevel@tonic-gate 	if (proc_mode != P_SKIP) {
22017c478bd9Sstevel@tonic-gate 		if (Hdr_type == CRC && Gen.g_cksum != cksumval) {
22027c478bd9Sstevel@tonic-gate 			msg(ERR, "\"%s\" - checksum error", nam_p);
22037c478bd9Sstevel@tonic-gate 			rstfiles(U_KEEP, G_p->g_dirfd);
22047c478bd9Sstevel@tonic-gate 		} else
22057c478bd9Sstevel@tonic-gate 			rstfiles(U_OVER, G_p->g_dirfd);
22067c478bd9Sstevel@tonic-gate 		if (Hdr_type == BAR && compress_flag) {
22077c478bd9Sstevel@tonic-gate 			(void) pclose(pipef);
22087c478bd9Sstevel@tonic-gate 		} else {
22097c478bd9Sstevel@tonic-gate 			cstatus = close(Ofile);
22107c478bd9Sstevel@tonic-gate 		}
22117c478bd9Sstevel@tonic-gate 		Ofile = 0;
22127c478bd9Sstevel@tonic-gate 		if (cstatus != 0) {
22137c478bd9Sstevel@tonic-gate 			msg(EXTN, "close error");
22147c478bd9Sstevel@tonic-gate 		}
22157c478bd9Sstevel@tonic-gate 	}
22167c478bd9Sstevel@tonic-gate 	VERBOSE((proc_mode != P_SKIP && (Args & (OCv | OCV))), G_p->g_nam_p);
22177c478bd9Sstevel@tonic-gate 	Finished = 1;
22187c478bd9Sstevel@tonic-gate }
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate /*
22217c478bd9Sstevel@tonic-gate  * data_out:  open(2) the file to be archived, compute the checksum
22227c478bd9Sstevel@tonic-gate  * of it's data if the CRC header was specified and write the header.
22237c478bd9Sstevel@tonic-gate  * read(2) each block of data and bwrite() it to the archive.  For TARTYP (TAR
22247c478bd9Sstevel@tonic-gate  * and USTAR) archives, pad the data with NULLs to the next 512 byte boundary.
22257c478bd9Sstevel@tonic-gate  */
22267c478bd9Sstevel@tonic-gate 
22277c478bd9Sstevel@tonic-gate static void
22287c478bd9Sstevel@tonic-gate data_out(void)
22297c478bd9Sstevel@tonic-gate {
22307c478bd9Sstevel@tonic-gate 	char *nam_p;
22317c478bd9Sstevel@tonic-gate 	int cnt, amount_read, pad;
22327c478bd9Sstevel@tonic-gate 	off_t amt_to_read, real_filesz;
22337c478bd9Sstevel@tonic-gate 	int	errret = 0;
22347c478bd9Sstevel@tonic-gate 
22357c478bd9Sstevel@tonic-gate 	nam_p = G_p->g_nam_p;
22367c478bd9Sstevel@tonic-gate 	if (Aspec) {
22377c478bd9Sstevel@tonic-gate 		if (Pflag && aclp != NULL) {
22387c478bd9Sstevel@tonic-gate 			char    *secinfo = NULL;
22397c478bd9Sstevel@tonic-gate 			int	len = 0;
22407c478bd9Sstevel@tonic-gate 
22417c478bd9Sstevel@tonic-gate 			/* append security attributes */
2242fa9e4066Sahrens 			if (append_secattr(&secinfo, &len, aclp) == -1) {
22437c478bd9Sstevel@tonic-gate 				msg(ERR,
22447c478bd9Sstevel@tonic-gate 				    "can create security information");
22457c478bd9Sstevel@tonic-gate 			}
22467c478bd9Sstevel@tonic-gate 			/* call append_secattr() if more than one */
22477c478bd9Sstevel@tonic-gate 
22487c478bd9Sstevel@tonic-gate 			if (len > 0) {
22497c478bd9Sstevel@tonic-gate 			/* write ancillary only if there is sec info */
22507c478bd9Sstevel@tonic-gate 				(void) write_hdr(ARCHIVE_ACL, (off_t)len);
22517c478bd9Sstevel@tonic-gate 				(void) write_ancillary(secinfo, len);
22527c478bd9Sstevel@tonic-gate 			}
22537c478bd9Sstevel@tonic-gate 		}
22547c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
22557c478bd9Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_dirfd);
22567c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
22577c478bd9Sstevel@tonic-gate 		return;
22587c478bd9Sstevel@tonic-gate 	}
22597c478bd9Sstevel@tonic-gate 	if ((G_p->g_mode & Ftype) == S_IFLNK && (Hdr_type !=
22607c478bd9Sstevel@tonic-gate 	    USTAR && Hdr_type != TAR)) { /* symbolic link */
22617c478bd9Sstevel@tonic-gate 		int size;
22627c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
22637c478bd9Sstevel@tonic-gate 
22647c478bd9Sstevel@tonic-gate 		FLUSH(G_p->g_filesz);
22657c478bd9Sstevel@tonic-gate 		errno = 0;
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate 		/* Note that "size" and G_p->g_filesz are the same number */
22687c478bd9Sstevel@tonic-gate 
22697c478bd9Sstevel@tonic-gate 		if ((size = readlink(nam_p, Buffr.b_in_p, G_p->g_filesz)) <
22707c478bd9Sstevel@tonic-gate 		    0) {
22717c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot read symbolic link \"%s\"", nam_p);
22727c478bd9Sstevel@tonic-gate 			return;
22737c478bd9Sstevel@tonic-gate 		}
22747c478bd9Sstevel@tonic-gate 
22757c478bd9Sstevel@tonic-gate 		/*
22767c478bd9Sstevel@tonic-gate 		 * Note that it is OK not to add the NUL after the name read by
22777c478bd9Sstevel@tonic-gate 		 * readlink, because it is not being used subsequently.
22787c478bd9Sstevel@tonic-gate 		 */
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += size;
22817c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += size;
22827c478bd9Sstevel@tonic-gate 		pad = (Pad_val + 1 - (size & Pad_val)) & Pad_val;
22837c478bd9Sstevel@tonic-gate 		if (pad != 0) {
22847c478bd9Sstevel@tonic-gate 			FLUSH(pad);
22857c478bd9Sstevel@tonic-gate 			(void) memcpy(Buffr.b_in_p, Empty, pad);
22867c478bd9Sstevel@tonic-gate 			Buffr.b_in_p += pad;
22877c478bd9Sstevel@tonic-gate 			Buffr.b_cnt += pad;
22887c478bd9Sstevel@tonic-gate 		}
22897c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
22907c478bd9Sstevel@tonic-gate 		return;
22917c478bd9Sstevel@tonic-gate 	} else if ((G_p->g_mode & Ftype) == S_IFLNK &&
22927c478bd9Sstevel@tonic-gate 	    (Hdr_type == USTAR || Hdr_type == TAR)) {
22937c478bd9Sstevel@tonic-gate 		int size;
22947c478bd9Sstevel@tonic-gate 
22957c478bd9Sstevel@tonic-gate 		/*
22967c478bd9Sstevel@tonic-gate 		 * G_p->g_filesz is the length of the right-hand side of
22977c478bd9Sstevel@tonic-gate 		 * the symlink "x -> y".
22987c478bd9Sstevel@tonic-gate 		 * The tar link field is only NAMSIZ long.
22997c478bd9Sstevel@tonic-gate 		 */
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 		if (G_p->g_filesz > NAMSIZ) {
23027c478bd9Sstevel@tonic-gate 			msg(ERRN,
23037c478bd9Sstevel@tonic-gate 			    "Symbolic link too long \"%s\"", nam_p);
23047c478bd9Sstevel@tonic-gate 			return;
23057c478bd9Sstevel@tonic-gate 		}
23067c478bd9Sstevel@tonic-gate 		if ((size = readlink(nam_p, T_lname, G_p->g_filesz)) < 0) {
23077c478bd9Sstevel@tonic-gate 			msg(ERRN,
23087c478bd9Sstevel@tonic-gate 			    "Cannot read symbolic link \"%s\"", nam_p);
23097c478bd9Sstevel@tonic-gate 			return;
23107c478bd9Sstevel@tonic-gate 		}
23117c478bd9Sstevel@tonic-gate 		T_lname[size] = '\0';
23127c478bd9Sstevel@tonic-gate 		G_p->g_filesz = (off_t)0;
23137c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
23147c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
23157c478bd9Sstevel@tonic-gate 		return;
23167c478bd9Sstevel@tonic-gate 	}
23177c478bd9Sstevel@tonic-gate 	if ((Ifile = openfile(O_RDONLY)) < 0) {
23187c478bd9Sstevel@tonic-gate 		msg(ERR, "\"%s%s%s\" ?",
23197c478bd9Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
23207c478bd9Sstevel@tonic-gate 		    nam_p : Gen.g_attrfnam_p,
23217c478bd9Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
23227c478bd9Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
23237c478bd9Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
23247c478bd9Sstevel@tonic-gate 		    "" : Gen.g_attrnam_p);
23257c478bd9Sstevel@tonic-gate 		return;
23267c478bd9Sstevel@tonic-gate 	}
23277c478bd9Sstevel@tonic-gate 
23287c478bd9Sstevel@tonic-gate 	/*
23297c478bd9Sstevel@tonic-gate 	 * Dump extended attribute header.
23307c478bd9Sstevel@tonic-gate 	 */
23317c478bd9Sstevel@tonic-gate 
23327c478bd9Sstevel@tonic-gate 	if (Gen.g_attrnam_p != (char *)NULL) {
23337c478bd9Sstevel@tonic-gate 		write_xattr_hdr();
23347c478bd9Sstevel@tonic-gate 	}
23357c478bd9Sstevel@tonic-gate 
23367c478bd9Sstevel@tonic-gate 	if (Hdr_type == CRC) {
23377c478bd9Sstevel@tonic-gate 		long csum = cksum(CRC, 0, &errret);
23387c478bd9Sstevel@tonic-gate 		if (errret != 0) {
23397c478bd9Sstevel@tonic-gate 			G_p->g_cksum = (ulong_t)-1;
23407c478bd9Sstevel@tonic-gate 			msg(POST, "\"%s%s%s\" skipped",
23417c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
23427c478bd9Sstevel@tonic-gate 			    nam_p : Gen.g_attrfnam_p,
23437c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
23447c478bd9Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
23457c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
23467c478bd9Sstevel@tonic-gate 			    "" : nam_p);
23477c478bd9Sstevel@tonic-gate 			(void) close(Ifile);
23487c478bd9Sstevel@tonic-gate 			return;
23497c478bd9Sstevel@tonic-gate 		}
23507c478bd9Sstevel@tonic-gate 		G_p->g_cksum = csum;
23517c478bd9Sstevel@tonic-gate 	} else {
23527c478bd9Sstevel@tonic-gate 		G_p->g_cksum = 0;
23537c478bd9Sstevel@tonic-gate 	}
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate 	/*
23567c478bd9Sstevel@tonic-gate 	 * ACL has been retrieved in getname().
23577c478bd9Sstevel@tonic-gate 	 */
23587c478bd9Sstevel@tonic-gate 	if (Pflag) {
23597c478bd9Sstevel@tonic-gate 		char    *secinfo = NULL;
23607c478bd9Sstevel@tonic-gate 		int	len = 0;
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate 		/* append security attributes */
2363fa9e4066Sahrens 		if ((append_secattr(&secinfo, &len, aclp)) == -1)
23647c478bd9Sstevel@tonic-gate 			msg(ERR, "can create security information");
23657c478bd9Sstevel@tonic-gate 
23667c478bd9Sstevel@tonic-gate 		/* call append_secattr() if more than one */
23677c478bd9Sstevel@tonic-gate 
23687c478bd9Sstevel@tonic-gate 		if (len > 0) {
23697c478bd9Sstevel@tonic-gate 		/* write ancillary only if there is sec info */
23707c478bd9Sstevel@tonic-gate 			(void) write_hdr(ARCHIVE_ACL, (off_t)len);
23717c478bd9Sstevel@tonic-gate 			(void) write_ancillary(secinfo, len);
23727c478bd9Sstevel@tonic-gate 		}
23737c478bd9Sstevel@tonic-gate 	}
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate 	write_hdr(ARCHIVE_NORMAL, (off_t)0);
23767c478bd9Sstevel@tonic-gate 
23777c478bd9Sstevel@tonic-gate 	real_filesz = 0;
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate 	for (amt_to_read = G_p->g_filesz;
23807c478bd9Sstevel@tonic-gate 	    amt_to_read > 0;
23817c478bd9Sstevel@tonic-gate 	    amt_to_read -= (off_t)amount_read) {
23827c478bd9Sstevel@tonic-gate 		FLUSH(CPIOBSZ);
23837c478bd9Sstevel@tonic-gate 		errno = 0;
23847c478bd9Sstevel@tonic-gate 
23857c478bd9Sstevel@tonic-gate 		if ((amount_read = read(Ifile, Buffr.b_in_p, CPIOBSZ)) < 0) {
23867c478bd9Sstevel@tonic-gate 			msg(EXTN, "Cannot read \"%s%s%s\"",
23877c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
23887c478bd9Sstevel@tonic-gate 			    nam_p : Gen.g_attrfnam_p,
23897c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
23907c478bd9Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
23917c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
23927c478bd9Sstevel@tonic-gate 			    "" : nam_p);
23937c478bd9Sstevel@tonic-gate 			break;
23947c478bd9Sstevel@tonic-gate 		}
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate 		if (amount_read == 0) {
23977c478bd9Sstevel@tonic-gate 			/* the file has shrunk */
23987c478bd9Sstevel@tonic-gate 			real_filesz = G_p->g_filesz - amt_to_read;
23997c478bd9Sstevel@tonic-gate 			break;
24007c478bd9Sstevel@tonic-gate 		} else if (amount_read > amt_to_read) {
24017c478bd9Sstevel@tonic-gate 			/* the file has grown */
24027c478bd9Sstevel@tonic-gate 			real_filesz = G_p->g_filesz +
24037c478bd9Sstevel@tonic-gate 			    (amount_read - amt_to_read);
24047c478bd9Sstevel@tonic-gate 			amount_read = amt_to_read;
24057c478bd9Sstevel@tonic-gate 		} else if (amount_read == amt_to_read) {
24067c478bd9Sstevel@tonic-gate 			/* the file is the same size */
24077c478bd9Sstevel@tonic-gate 			real_filesz = G_p->g_filesz;
24087c478bd9Sstevel@tonic-gate 		}
24097c478bd9Sstevel@tonic-gate 
24107c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += amount_read;
24117c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += (long)amount_read;
24127c478bd9Sstevel@tonic-gate 	}
24137c478bd9Sstevel@tonic-gate 
24147c478bd9Sstevel@tonic-gate 	while (amt_to_read > 0) {
24157c478bd9Sstevel@tonic-gate 		cnt = (amt_to_read > CPIOBSZ) ? CPIOBSZ : (int)amt_to_read;
24167c478bd9Sstevel@tonic-gate 		FLUSH(cnt);
24177c478bd9Sstevel@tonic-gate 		(void) memset(Buffr.b_in_p, NULL, cnt);
24187c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += cnt;
24197c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += cnt;
24207c478bd9Sstevel@tonic-gate 		amt_to_read -= cnt;
24217c478bd9Sstevel@tonic-gate 	}
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate 	pad = (Pad_val + 1 - (G_p->g_filesz & Pad_val)) & Pad_val;
24247c478bd9Sstevel@tonic-gate 	if (pad != 0) {
24257c478bd9Sstevel@tonic-gate 		FLUSH(pad);
24267c478bd9Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, Empty, pad);
24277c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += pad;
24287c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += pad;
24297c478bd9Sstevel@tonic-gate 	}
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate 	if (real_filesz > G_p->g_filesz) {
24327c478bd9Sstevel@tonic-gate 		msg(ERR, "File size of \"%s%s%s\" has increased by %lld",
24337c478bd9Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
24347c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p : Gen.g_attrfnam_p,
24357c478bd9Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
24367c478bd9Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
24377c478bd9Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
24387c478bd9Sstevel@tonic-gate 		    "" : G_p->g_nam_p,
24397c478bd9Sstevel@tonic-gate 		    (real_filesz - G_p->g_filesz));
24407c478bd9Sstevel@tonic-gate 	}
24417c478bd9Sstevel@tonic-gate 	if (real_filesz < G_p->g_filesz) {
24427c478bd9Sstevel@tonic-gate 		msg(ERR, "File size of \"%s%s%s\" has decreased by %lld",
24437c478bd9Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
24447c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p : Gen.g_attrfnam_p,
24457c478bd9Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
24467c478bd9Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
24477c478bd9Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
24487c478bd9Sstevel@tonic-gate 		    "" : G_p->g_nam_p,
24497c478bd9Sstevel@tonic-gate 		    (G_p->g_filesz - real_filesz));
24507c478bd9Sstevel@tonic-gate 	}
24517c478bd9Sstevel@tonic-gate 
24527c478bd9Sstevel@tonic-gate 	(void) close(Ifile);
24537c478bd9Sstevel@tonic-gate 	rstfiles(U_KEEP, G_p->g_dirfd);
24547c478bd9Sstevel@tonic-gate 	VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
24557c478bd9Sstevel@tonic-gate }
24567c478bd9Sstevel@tonic-gate 
24577c478bd9Sstevel@tonic-gate /*
24587c478bd9Sstevel@tonic-gate  * data_pass:  If not a special file (Aspec), open(2) the file to be
24597c478bd9Sstevel@tonic-gate  * transferred, read(2) each block of data and write(2) it to the output file
24607c478bd9Sstevel@tonic-gate  * Ofile, which was opened in file_pass().
24617c478bd9Sstevel@tonic-gate  */
24627c478bd9Sstevel@tonic-gate 
24637c478bd9Sstevel@tonic-gate static void
24647c478bd9Sstevel@tonic-gate data_pass(void)
24657c478bd9Sstevel@tonic-gate {
24667c478bd9Sstevel@tonic-gate 	int cnt, done = 1;
24677c478bd9Sstevel@tonic-gate 	int cstatus;
24687c478bd9Sstevel@tonic-gate 	off_t filesz;
24697c478bd9Sstevel@tonic-gate 	char *namep = Nam_p;
24707c478bd9Sstevel@tonic-gate 
24717c478bd9Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
24727c478bd9Sstevel@tonic-gate 		namep = G_p->g_attrnam_p;
24737c478bd9Sstevel@tonic-gate 	}
24747c478bd9Sstevel@tonic-gate 	if (Aspec) {
24757c478bd9Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_passdirfd);
24767c478bd9Sstevel@tonic-gate 		cstatus = close(Ofile);
24777c478bd9Sstevel@tonic-gate 		Ofile = 0;
24787c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), Nam_p);
24797c478bd9Sstevel@tonic-gate 		if (cstatus != 0) {
24807c478bd9Sstevel@tonic-gate 			msg(EXTN, "close error");
24817c478bd9Sstevel@tonic-gate 		}
24827c478bd9Sstevel@tonic-gate 		return;
24837c478bd9Sstevel@tonic-gate 	}
24847c478bd9Sstevel@tonic-gate 	if ((Ifile = openat(G_p->g_dirfd, get_component(namep), 0)) < 0) {
24857c478bd9Sstevel@tonic-gate 		msg(ERRN, "Cannot open \"%s%s%s\", skipped",
24867c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
24877c478bd9Sstevel@tonic-gate 		    Nam_p : G_p->g_attrfnam_p,
24887c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
24897c478bd9Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
24907c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
24917c478bd9Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p);
24927c478bd9Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_passdirfd);
24937c478bd9Sstevel@tonic-gate 		cstatus = close(Ofile);
24947c478bd9Sstevel@tonic-gate 		Ofile = 0;
24957c478bd9Sstevel@tonic-gate 		if (cstatus != 0) {
24967c478bd9Sstevel@tonic-gate 			msg(EXTN, "close error");
24977c478bd9Sstevel@tonic-gate 		}
24987c478bd9Sstevel@tonic-gate 		return;
24997c478bd9Sstevel@tonic-gate 	}
25007c478bd9Sstevel@tonic-gate 	filesz = G_p->g_filesz;
25017c478bd9Sstevel@tonic-gate 
25027c478bd9Sstevel@tonic-gate 	while (filesz > 0) {
25037c478bd9Sstevel@tonic-gate 		cnt = (unsigned)(filesz > Bufsize) ? Bufsize : filesz;
25047c478bd9Sstevel@tonic-gate 		errno = 0;
25057c478bd9Sstevel@tonic-gate 		if (read(Ifile, Buf_p, (unsigned)cnt) < 0) {
25067c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot read \"%s%s%s\"",
25077c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
25087c478bd9Sstevel@tonic-gate 			    Nam_p : G_p->g_attrfnam_p,
25097c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
25107c478bd9Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
25117c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
25127c478bd9Sstevel@tonic-gate 			    "" : G_p->g_attrnam_p);
25137c478bd9Sstevel@tonic-gate 			done = 0;
25147c478bd9Sstevel@tonic-gate 			break;
25157c478bd9Sstevel@tonic-gate 		}
25167c478bd9Sstevel@tonic-gate 		errno = 0;
25177c478bd9Sstevel@tonic-gate 		if (write(Ofile, Buf_p, (unsigned)cnt) < 0) {
25187c478bd9Sstevel@tonic-gate 			if (Do_rename) {
25197c478bd9Sstevel@tonic-gate 				msg(ERRN, "Cannot write \"%s%s%s\"", Over_p,
25207c478bd9Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
25217c478bd9Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
25227c478bd9Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
25237c478bd9Sstevel@tonic-gate 				    "" : Over_p);
25247c478bd9Sstevel@tonic-gate 			} else {
25257c478bd9Sstevel@tonic-gate 				msg(ERRN, "Cannot write \"%s%s%s\"",
25267c478bd9Sstevel@tonic-gate 				    Fullnam_p,
25277c478bd9Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
25287c478bd9Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
25297c478bd9Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
25307c478bd9Sstevel@tonic-gate 				    "" : G_p->g_attrnam_p);
25317c478bd9Sstevel@tonic-gate 			}
25327c478bd9Sstevel@tonic-gate 
25337c478bd9Sstevel@tonic-gate 			done = 0;
25347c478bd9Sstevel@tonic-gate 			break;
25357c478bd9Sstevel@tonic-gate 		}
25367c478bd9Sstevel@tonic-gate 		Blocks += (u_longlong_t)((cnt + (Bufsize - 1)) / Bufsize);
25377c478bd9Sstevel@tonic-gate 		filesz -= (off_t)cnt;
25387c478bd9Sstevel@tonic-gate 	}
25397c478bd9Sstevel@tonic-gate 	if (done) {
25407c478bd9Sstevel@tonic-gate 		rstfiles(U_OVER, G_p->g_passdirfd);
25417c478bd9Sstevel@tonic-gate 	} else {
25427c478bd9Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_passdirfd);
25437c478bd9Sstevel@tonic-gate 	}
25447c478bd9Sstevel@tonic-gate 	(void) close(Ifile);
25457c478bd9Sstevel@tonic-gate 	cstatus = close(Ofile);
25467c478bd9Sstevel@tonic-gate 	Ofile = 0;
25477c478bd9Sstevel@tonic-gate 	if (cstatus != 0) {
25487c478bd9Sstevel@tonic-gate 		msg(EXTN, "close error");
25497c478bd9Sstevel@tonic-gate 	}
25507c478bd9Sstevel@tonic-gate 	VERBOSE((Args & (OCv | OCV)), Fullnam_p);
25517c478bd9Sstevel@tonic-gate 	Finished = 1;
25527c478bd9Sstevel@tonic-gate }
25537c478bd9Sstevel@tonic-gate 
25547c478bd9Sstevel@tonic-gate /*
25557c478bd9Sstevel@tonic-gate  * Allocation wrappers.  Used to centralize error handling for
25567c478bd9Sstevel@tonic-gate  * failed allocations.
25577c478bd9Sstevel@tonic-gate  */
25587c478bd9Sstevel@tonic-gate static void *
25597c478bd9Sstevel@tonic-gate e_alloc_fail(int flag)
25607c478bd9Sstevel@tonic-gate {
25617c478bd9Sstevel@tonic-gate 	if (flag == E_EXIT) {
25627c478bd9Sstevel@tonic-gate 		msg(EXTN, "Out of memory");
25637c478bd9Sstevel@tonic-gate 	}
25647c478bd9Sstevel@tonic-gate 
25657c478bd9Sstevel@tonic-gate 	return (NULL);
25667c478bd9Sstevel@tonic-gate }
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate /*
25697c478bd9Sstevel@tonic-gate  *  Note: unlike the other e_*lloc functions, e_realloc does not zero out the
25707c478bd9Sstevel@tonic-gate  *  additional memory it returns.  Ensure that you do not trust its contents
25717c478bd9Sstevel@tonic-gate  *  when you call it.
25727c478bd9Sstevel@tonic-gate  */
25737c478bd9Sstevel@tonic-gate 
25747c478bd9Sstevel@tonic-gate static void *
25757c478bd9Sstevel@tonic-gate e_realloc(int flag, void *old, size_t newsize)
25767c478bd9Sstevel@tonic-gate {
25777c478bd9Sstevel@tonic-gate 	void *ret = realloc(old, newsize);
25787c478bd9Sstevel@tonic-gate 
25797c478bd9Sstevel@tonic-gate 	if (ret == NULL) {
25807c478bd9Sstevel@tonic-gate 		return (e_alloc_fail(flag));
25817c478bd9Sstevel@tonic-gate 	}
25827c478bd9Sstevel@tonic-gate 
25837c478bd9Sstevel@tonic-gate 	return (ret);
25847c478bd9Sstevel@tonic-gate }
25857c478bd9Sstevel@tonic-gate 
25867c478bd9Sstevel@tonic-gate static char *
25877c478bd9Sstevel@tonic-gate e_strdup(int flag, const char *arg)
25887c478bd9Sstevel@tonic-gate {
25897c478bd9Sstevel@tonic-gate 	char *ret = strdup(arg);
25907c478bd9Sstevel@tonic-gate 
25917c478bd9Sstevel@tonic-gate 	if (ret == NULL) {
25927c478bd9Sstevel@tonic-gate 		return (e_alloc_fail(flag));
25937c478bd9Sstevel@tonic-gate 	}
25947c478bd9Sstevel@tonic-gate 
25957c478bd9Sstevel@tonic-gate 	return (ret);
25967c478bd9Sstevel@tonic-gate }
25977c478bd9Sstevel@tonic-gate 
25987c478bd9Sstevel@tonic-gate static void *
25997c478bd9Sstevel@tonic-gate e_valloc(int flag, size_t size)
26007c478bd9Sstevel@tonic-gate {
26017c478bd9Sstevel@tonic-gate 	void *ret = valloc(size);
26027c478bd9Sstevel@tonic-gate 
26037c478bd9Sstevel@tonic-gate 	if (ret == NULL) {
26047c478bd9Sstevel@tonic-gate 		return (e_alloc_fail(flag));
26057c478bd9Sstevel@tonic-gate 	}
26067c478bd9Sstevel@tonic-gate 
26077c478bd9Sstevel@tonic-gate 	return (ret);
26087c478bd9Sstevel@tonic-gate }
26097c478bd9Sstevel@tonic-gate 
26107c478bd9Sstevel@tonic-gate static void *
26117c478bd9Sstevel@tonic-gate e_zalloc(int flag, size_t size)
26127c478bd9Sstevel@tonic-gate {
26137c478bd9Sstevel@tonic-gate 	void *ret = malloc(size);
26147c478bd9Sstevel@tonic-gate 
26157c478bd9Sstevel@tonic-gate 	if (ret == NULL) {
26167c478bd9Sstevel@tonic-gate 		return (e_alloc_fail(flag));
26177c478bd9Sstevel@tonic-gate 	}
26187c478bd9Sstevel@tonic-gate 
26197c478bd9Sstevel@tonic-gate 	(void) memset(ret, 0, size);
26207c478bd9Sstevel@tonic-gate 	return (ret);
26217c478bd9Sstevel@tonic-gate }
26227c478bd9Sstevel@tonic-gate 
26237c478bd9Sstevel@tonic-gate /*
26247c478bd9Sstevel@tonic-gate  * file_in:  Process an object from the archive.  If a TARTYP (TAR or USTAR)
26257c478bd9Sstevel@tonic-gate  * archive and g_nlink == 1, link this file to the file name in t_linkname
26267c478bd9Sstevel@tonic-gate  * and return.  Handle linked files in one of two ways.  If Onecopy == 0, this
26277c478bd9Sstevel@tonic-gate  * is an old style (binary or -c) archive, create and extract the data for the
26287c478bd9Sstevel@tonic-gate  * first link found, link all subsequent links to this file and skip their data.
26297c478bd9Sstevel@tonic-gate  * If Oncecopy == 1, save links until all have been processed, and then
26307c478bd9Sstevel@tonic-gate  * process the links first to last checking their names against the patterns
26317c478bd9Sstevel@tonic-gate  * and/or asking the user to rename them.  The first link that is accepted
26327c478bd9Sstevel@tonic-gate  * for xtraction is created and the data is read from the archive.
26337c478bd9Sstevel@tonic-gate  * All subsequent links that are accepted are linked to this file.
26347c478bd9Sstevel@tonic-gate  */
26357c478bd9Sstevel@tonic-gate 
26367c478bd9Sstevel@tonic-gate static void
26377c478bd9Sstevel@tonic-gate file_in(void)
26387c478bd9Sstevel@tonic-gate {
26397c478bd9Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
26407c478bd9Sstevel@tonic-gate 	int lnkem = 0, cleanup = 0;
26417c478bd9Sstevel@tonic-gate 	int proc_file;
26427c478bd9Sstevel@tonic-gate 	struct Lnk *ttl_p;
26437c478bd9Sstevel@tonic-gate 	int typeflag;
26447c478bd9Sstevel@tonic-gate 	char savacl;
26457c478bd9Sstevel@tonic-gate 	int cwd;
26467c478bd9Sstevel@tonic-gate 
26477c478bd9Sstevel@tonic-gate 	G_p = &Gen;
26487c478bd9Sstevel@tonic-gate 
26497c478bd9Sstevel@tonic-gate 	/*
26507c478bd9Sstevel@tonic-gate 	 * Open target directory if this isn't a skipped file
26517c478bd9Sstevel@tonic-gate 	 * and g_nlink == 1
26527c478bd9Sstevel@tonic-gate 	 *
26537c478bd9Sstevel@tonic-gate 	 * Links are handled further down in this function.
26547c478bd9Sstevel@tonic-gate 	 */
26557c478bd9Sstevel@tonic-gate 
26567c478bd9Sstevel@tonic-gate 	proc_file = ckname(0);
26577c478bd9Sstevel@tonic-gate 
26587c478bd9Sstevel@tonic-gate 	if (proc_file == F_SKIP && G_p->g_nlink == 1) {
26597c478bd9Sstevel@tonic-gate 		/*
26607c478bd9Sstevel@tonic-gate 		 * Normally ckname() prints out the file as a side
26617c478bd9Sstevel@tonic-gate 		 * effect except for table of contents listing
26627c478bd9Sstevel@tonic-gate 		 * when its parameter is zero and Onecopy isn't
26637c478bd9Sstevel@tonic-gate 		 * Zero.  Due to this we need to force the name
26647c478bd9Sstevel@tonic-gate 		 * to be printed here.
26657c478bd9Sstevel@tonic-gate 		 */
26667c478bd9Sstevel@tonic-gate 		if (Onecopy == 1) {
26677c478bd9Sstevel@tonic-gate 			VERBOSE((Args & OCt), G_p->g_nam_p);
26687c478bd9Sstevel@tonic-gate 		}
26697c478bd9Sstevel@tonic-gate 		data_in(P_SKIP);
26707c478bd9Sstevel@tonic-gate 		return;
26717c478bd9Sstevel@tonic-gate 	}
26727c478bd9Sstevel@tonic-gate 
26737c478bd9Sstevel@tonic-gate 	if (proc_file != F_SKIP && open_dirfd() != 0) {
26747c478bd9Sstevel@tonic-gate 		data_in(P_SKIP);
26757c478bd9Sstevel@tonic-gate 		return;
26767c478bd9Sstevel@tonic-gate 	}
26777c478bd9Sstevel@tonic-gate 
26787c478bd9Sstevel@tonic-gate 	if (Hdr_type == BAR) {
26797c478bd9Sstevel@tonic-gate 		bar_file_in();
26807c478bd9Sstevel@tonic-gate 		close_dirfd();
26817c478bd9Sstevel@tonic-gate 		return;
26827c478bd9Sstevel@tonic-gate 	}
26837c478bd9Sstevel@tonic-gate 
26847c478bd9Sstevel@tonic-gate 	/*
26857c478bd9Sstevel@tonic-gate 	 * For archives in USTAR format, the files are extracted according
26867c478bd9Sstevel@tonic-gate 	 * to the typeflag.
26877c478bd9Sstevel@tonic-gate 	 */
26887c478bd9Sstevel@tonic-gate 	if (Hdr_type == USTAR || Hdr_type == TAR) {
26897c478bd9Sstevel@tonic-gate 		typeflag = Thdr_p->tbuf.t_typeflag;
26907c478bd9Sstevel@tonic-gate 		if (G_p->g_nlink == 1) {		/* hard link */
26917c478bd9Sstevel@tonic-gate 			if (proc_file != F_SKIP) {
26927c478bd9Sstevel@tonic-gate 				int i;
26937c478bd9Sstevel@tonic-gate 				char lname[NAMSIZ+1];
26947c478bd9Sstevel@tonic-gate 				(void) memset(lname, '\0', sizeof (lname));
26957c478bd9Sstevel@tonic-gate 
26967c478bd9Sstevel@tonic-gate 				(void) strncpy(lname, Thdr_p->tbuf.t_linkname,
26977c478bd9Sstevel@tonic-gate 				    NAMSIZ);
26987c478bd9Sstevel@tonic-gate 				for (i = 0; i <= NAMSIZ && lname[i] != 0; i++)
26997c478bd9Sstevel@tonic-gate 					;
27007c478bd9Sstevel@tonic-gate 
27017c478bd9Sstevel@tonic-gate 				lname[i] = 0;
27027c478bd9Sstevel@tonic-gate 				(void) creat_lnk(G_p->g_dirfd,
27037c478bd9Sstevel@tonic-gate 				    &lname[0], G_p->g_nam_p);
27047c478bd9Sstevel@tonic-gate 			}
27057c478bd9Sstevel@tonic-gate 			close_dirfd();
27067c478bd9Sstevel@tonic-gate 			return;
27077c478bd9Sstevel@tonic-gate 		}
27087c478bd9Sstevel@tonic-gate 		if (typeflag == '3' || typeflag == '4' || typeflag == '5' ||
27097c478bd9Sstevel@tonic-gate 		    typeflag == '6') {
27107c478bd9Sstevel@tonic-gate 			if (proc_file != F_SKIP &&
27117c478bd9Sstevel@tonic-gate 			    creat_spec(G_p->g_dirfd) > 0) {
27127c478bd9Sstevel@tonic-gate 				VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
27137c478bd9Sstevel@tonic-gate 			}
27147c478bd9Sstevel@tonic-gate 			close_dirfd();
27157c478bd9Sstevel@tonic-gate 			return;
27167c478bd9Sstevel@tonic-gate 		} else if (Adir || Aspec) {
27177c478bd9Sstevel@tonic-gate 			if ((proc_file == F_SKIP) ||
27187c478bd9Sstevel@tonic-gate 			    (Ofile = openout(G_p->g_dirfd)) < 0) {
27197c478bd9Sstevel@tonic-gate 				data_in(P_SKIP);
27207c478bd9Sstevel@tonic-gate 			} else {
27217c478bd9Sstevel@tonic-gate 				data_in(P_PROC);
27227c478bd9Sstevel@tonic-gate 			}
27237c478bd9Sstevel@tonic-gate 			close_dirfd();
27247c478bd9Sstevel@tonic-gate 			return;
27257c478bd9Sstevel@tonic-gate 		}
27267c478bd9Sstevel@tonic-gate 	}
27277c478bd9Sstevel@tonic-gate 
27287c478bd9Sstevel@tonic-gate 	if (Adir) {
27297c478bd9Sstevel@tonic-gate 		if (proc_file != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
27307c478bd9Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
27317c478bd9Sstevel@tonic-gate 		}
27327c478bd9Sstevel@tonic-gate 		close_dirfd();
27337c478bd9Sstevel@tonic-gate 		if (Onecopy == 1) {
27347c478bd9Sstevel@tonic-gate 			VERBOSE((Args & OCt), G_p->g_nam_p);
27357c478bd9Sstevel@tonic-gate 		}
27367c478bd9Sstevel@tonic-gate 		return;
27377c478bd9Sstevel@tonic-gate 	}
27387c478bd9Sstevel@tonic-gate 	if (G_p->g_nlink == 1 || (Hdr_type == TAR ||
27397c478bd9Sstevel@tonic-gate 	    Hdr_type == USTAR)) {
27407c478bd9Sstevel@tonic-gate 		if (Aspec) {
27417c478bd9Sstevel@tonic-gate 			if (proc_file != F_SKIP && creat_spec(G_p->g_dirfd) > 0)
27427c478bd9Sstevel@tonic-gate 				VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
27437c478bd9Sstevel@tonic-gate 		} else {
27447c478bd9Sstevel@tonic-gate 			if ((proc_file == F_SKIP) ||
27457c478bd9Sstevel@tonic-gate 			    (Ofile = openout(G_p->g_dirfd)) < 0) {
27467c478bd9Sstevel@tonic-gate 				data_in(P_SKIP);
27477c478bd9Sstevel@tonic-gate 			} else {
27487c478bd9Sstevel@tonic-gate 				data_in(P_PROC);
27497c478bd9Sstevel@tonic-gate 			}
27507c478bd9Sstevel@tonic-gate 		}
27517c478bd9Sstevel@tonic-gate 		close_dirfd();
27527c478bd9Sstevel@tonic-gate 		return;
27537c478bd9Sstevel@tonic-gate 	}
27547c478bd9Sstevel@tonic-gate 	close_dirfd();
27557c478bd9Sstevel@tonic-gate 
27567c478bd9Sstevel@tonic-gate 	tl_p = add_lnk(&ttl_p);
27577c478bd9Sstevel@tonic-gate 	l_p = ttl_p;
27587c478bd9Sstevel@tonic-gate 	if (l_p->L_cnt == l_p->L_gen.g_nlink)
27597c478bd9Sstevel@tonic-gate 		cleanup = 1;
27607c478bd9Sstevel@tonic-gate 	if (!Onecopy || G_p->g_attrnam_p != (char *)NULL) {
27617c478bd9Sstevel@tonic-gate 		lnkem = (tl_p != l_p) ? 1 : 0;
27627c478bd9Sstevel@tonic-gate 		G_p = &tl_p->L_gen;
27637c478bd9Sstevel@tonic-gate 		if (proc_file == F_SKIP) {
27647c478bd9Sstevel@tonic-gate 			data_in(P_SKIP);
27657c478bd9Sstevel@tonic-gate 		} else {
27667c478bd9Sstevel@tonic-gate 			if (open_dirfd() != 0)
27677c478bd9Sstevel@tonic-gate 				return;
27687c478bd9Sstevel@tonic-gate 			if (!lnkem) {
27697c478bd9Sstevel@tonic-gate 				if (Aspec) {
27707c478bd9Sstevel@tonic-gate 					if (creat_spec(G_p->g_dirfd) > 0)
27717c478bd9Sstevel@tonic-gate 						VERBOSE((Args & (OCv | OCV)),
27727c478bd9Sstevel@tonic-gate 						    G_p->g_nam_p);
27737c478bd9Sstevel@tonic-gate 				} else if ((Ofile =
27747c478bd9Sstevel@tonic-gate 				    openout(G_p->g_dirfd)) < 0) {
27757c478bd9Sstevel@tonic-gate 					data_in(P_SKIP);
27767c478bd9Sstevel@tonic-gate 					close_dirfd();
27777c478bd9Sstevel@tonic-gate 					reclaim(l_p);
27787c478bd9Sstevel@tonic-gate 				} else {
27797c478bd9Sstevel@tonic-gate 					data_in(P_PROC);
27807c478bd9Sstevel@tonic-gate 					close_dirfd();
27817c478bd9Sstevel@tonic-gate 				}
27827c478bd9Sstevel@tonic-gate 			} else {
27837c478bd9Sstevel@tonic-gate 				/*
27847c478bd9Sstevel@tonic-gate 				 * Are we linking an attribute?
27857c478bd9Sstevel@tonic-gate 				 */
27867c478bd9Sstevel@tonic-gate 				cwd = -1;
27877c478bd9Sstevel@tonic-gate 				if (l_p->L_gen.g_attrnam_p != (char *)NULL) {
27887c478bd9Sstevel@tonic-gate 					(void) strcpy(Lnkend_p,
27897c478bd9Sstevel@tonic-gate 					    l_p->L_gen.g_attrnam_p);
27907c478bd9Sstevel@tonic-gate 					(void) strcpy(Full_p,
27917c478bd9Sstevel@tonic-gate 					    tl_p->L_gen.g_attrnam_p);
27927c478bd9Sstevel@tonic-gate 					cwd = save_cwd();
27937c478bd9Sstevel@tonic-gate 					(void) fchdir(G_p->g_dirfd);
27947c478bd9Sstevel@tonic-gate 				} else {
27957c478bd9Sstevel@tonic-gate 					(void) strcpy(Lnkend_p,
27967c478bd9Sstevel@tonic-gate 					    l_p->L_gen.g_nam_p);
27977c478bd9Sstevel@tonic-gate 					(void) strcpy(Full_p,
27987c478bd9Sstevel@tonic-gate 					    tl_p->L_gen.g_nam_p);
27997c478bd9Sstevel@tonic-gate 				}
28007c478bd9Sstevel@tonic-gate 				(void) creat_lnk(G_p->g_dirfd,
28017c478bd9Sstevel@tonic-gate 				    Lnkend_p, Full_p);
28027c478bd9Sstevel@tonic-gate 				data_in(P_SKIP);
28037c478bd9Sstevel@tonic-gate 				close_dirfd();
28047c478bd9Sstevel@tonic-gate 				l_p->L_lnk_p = (struct Lnk *)NULL;
28057c478bd9Sstevel@tonic-gate 				free(tl_p->L_gen.g_nam_p);
28067c478bd9Sstevel@tonic-gate 				free(tl_p);
28077c478bd9Sstevel@tonic-gate 				if (cwd != -1)
28087c478bd9Sstevel@tonic-gate 					rest_cwd(cwd);
28097c478bd9Sstevel@tonic-gate 			}
28107c478bd9Sstevel@tonic-gate 		}
28117c478bd9Sstevel@tonic-gate 	} else { /* Onecopy */
28127c478bd9Sstevel@tonic-gate 		if (tl_p->L_gen.g_filesz)
28137c478bd9Sstevel@tonic-gate 			cleanup = 1;
28147c478bd9Sstevel@tonic-gate 		if (!cleanup) {
28157c478bd9Sstevel@tonic-gate 			close_dirfd();
28167c478bd9Sstevel@tonic-gate 			return; /* don't do anything yet */
28177c478bd9Sstevel@tonic-gate 		}
28187c478bd9Sstevel@tonic-gate 		tl_p = l_p;
28197c478bd9Sstevel@tonic-gate 		/*
28207c478bd9Sstevel@tonic-gate 		 * ckname will clear aclchar. We need to keep aclchar for
28217c478bd9Sstevel@tonic-gate 		 * all links.
28227c478bd9Sstevel@tonic-gate 		 */
28237c478bd9Sstevel@tonic-gate 		savacl = aclchar;
28247c478bd9Sstevel@tonic-gate 		while (tl_p != (struct Lnk *)NULL) {
28257c478bd9Sstevel@tonic-gate 			G_p = &tl_p->L_gen;
28267c478bd9Sstevel@tonic-gate 			aclchar = savacl;
28277c478bd9Sstevel@tonic-gate 			if ((proc_file = ckname(1)) != F_SKIP) {
28287c478bd9Sstevel@tonic-gate 				if (open_dirfd() != 0) {
28297c478bd9Sstevel@tonic-gate 					return;
28307c478bd9Sstevel@tonic-gate 				}
28317c478bd9Sstevel@tonic-gate 				if (l_p->L_data) {
28327c478bd9Sstevel@tonic-gate 					(void) creat_lnk(G_p->g_dirfd,
28337c478bd9Sstevel@tonic-gate 					    l_p->L_gen.g_nam_p,
28347c478bd9Sstevel@tonic-gate 					    G_p->g_nam_p);
28357c478bd9Sstevel@tonic-gate 				} else if (Aspec) {
28367c478bd9Sstevel@tonic-gate 					(void) creat_spec(G_p->g_dirfd);
28377c478bd9Sstevel@tonic-gate 					l_p->L_data = 1;
28387c478bd9Sstevel@tonic-gate 					VERBOSE((Args & (OCv | OCV)),
28397c478bd9Sstevel@tonic-gate 					    G_p->g_nam_p);
28407c478bd9Sstevel@tonic-gate 				} else if ((Ofile =
28417c478bd9Sstevel@tonic-gate 				    openout(G_p->g_dirfd)) < 0) {
28427c478bd9Sstevel@tonic-gate 					proc_file = F_SKIP;
28437c478bd9Sstevel@tonic-gate 				} else {
28447c478bd9Sstevel@tonic-gate 					data_in(P_PROC);
28457c478bd9Sstevel@tonic-gate 					l_p->L_data = 1;
28467c478bd9Sstevel@tonic-gate 				}
28477c478bd9Sstevel@tonic-gate 			} /* (proc_file = ckname(1)) != F_SKIP */
28487c478bd9Sstevel@tonic-gate 
28497c478bd9Sstevel@tonic-gate 			tl_p = tl_p->L_lnk_p;
28507c478bd9Sstevel@tonic-gate 
28517c478bd9Sstevel@tonic-gate 			close_dirfd();
28527c478bd9Sstevel@tonic-gate 
28537c478bd9Sstevel@tonic-gate 			if (proc_file == F_SKIP && !cleanup) {
28547c478bd9Sstevel@tonic-gate 				tl_p->L_nxt_p = l_p->L_nxt_p;
28557c478bd9Sstevel@tonic-gate 				tl_p->L_bck_p = l_p->L_bck_p;
28567c478bd9Sstevel@tonic-gate 				l_p->L_bck_p->L_nxt_p = tl_p;
28577c478bd9Sstevel@tonic-gate 				l_p->L_nxt_p->L_bck_p = tl_p;
28587c478bd9Sstevel@tonic-gate 				free(l_p->L_gen.g_nam_p);
28597c478bd9Sstevel@tonic-gate 				free(l_p);
28607c478bd9Sstevel@tonic-gate 			}
28617c478bd9Sstevel@tonic-gate 		} /* tl_p->L_lnk_p != (struct Lnk *)NULL */
28627c478bd9Sstevel@tonic-gate 		if (l_p->L_data == 0) {
28637c478bd9Sstevel@tonic-gate 			data_in(P_SKIP);
28647c478bd9Sstevel@tonic-gate 		}
28657c478bd9Sstevel@tonic-gate 	}
28667c478bd9Sstevel@tonic-gate 	if (cleanup) {
28677c478bd9Sstevel@tonic-gate 		reclaim(l_p);
28687c478bd9Sstevel@tonic-gate 	}
28697c478bd9Sstevel@tonic-gate }
28707c478bd9Sstevel@tonic-gate 
28717c478bd9Sstevel@tonic-gate /*
28727c478bd9Sstevel@tonic-gate  * file_out:  If the current file is not a special file (!Aspec) and it
28737c478bd9Sstevel@tonic-gate  * is identical to the archive, skip it (do not archive the archive if it
28747c478bd9Sstevel@tonic-gate  * is a regular file).  If creating a TARTYP (TAR or USTAR) archive, the first
28757c478bd9Sstevel@tonic-gate  * time a link to a file is encountered, write the header and file out normally.
28767c478bd9Sstevel@tonic-gate  * Subsequent links to this file put this file name in their t_linkname field.
28777c478bd9Sstevel@tonic-gate  * Otherwise, links are handled in one of two ways, for the old headers
28787c478bd9Sstevel@tonic-gate  * (i.e. binary and -c), linked files are written out as they are encountered.
28797c478bd9Sstevel@tonic-gate  * For the new headers (ASC and CRC), links are saved up until all the links
28807c478bd9Sstevel@tonic-gate  * to each file are found.  For a file with n links, write n - 1 headers with
28817c478bd9Sstevel@tonic-gate  * g_filesz set to 0, write the final (nth) header with the correct g_filesz
28827c478bd9Sstevel@tonic-gate  * value and write the data for the file to the archive.
28837c478bd9Sstevel@tonic-gate  */
28847c478bd9Sstevel@tonic-gate 
28857c478bd9Sstevel@tonic-gate static
28867c478bd9Sstevel@tonic-gate int
28877c478bd9Sstevel@tonic-gate file_out(void)
28887c478bd9Sstevel@tonic-gate {
28897c478bd9Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
28907c478bd9Sstevel@tonic-gate 	int cleanup = 0;
28917c478bd9Sstevel@tonic-gate 	struct Lnk *ttl_p;
28927c478bd9Sstevel@tonic-gate 
28937c478bd9Sstevel@tonic-gate 	G_p = &Gen;
28947c478bd9Sstevel@tonic-gate 	if (!Aspec && IDENT(SrcSt, ArchSt))
28957c478bd9Sstevel@tonic-gate 		return (1); /* do not archive the archive if it's a reg file */
28967c478bd9Sstevel@tonic-gate 	if (G_p->g_filesz > Max_offset) {
28977c478bd9Sstevel@tonic-gate 		msg(ERR, "cpio: %s%s%s: too large to archive in current mode",
28987c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p,
28997c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
29007c478bd9Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
29017c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
29027c478bd9Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p);
29037c478bd9Sstevel@tonic-gate 		return (1); /* do not archive if it's too big */
29047c478bd9Sstevel@tonic-gate 	}
29057c478bd9Sstevel@tonic-gate 	if (Hdr_type == TAR || Hdr_type == USTAR) { /* TAR and USTAR */
29067c478bd9Sstevel@tonic-gate 		if (Adir) {
29077c478bd9Sstevel@tonic-gate 			if (Gen.g_attrnam_p != (char *)NULL) {
29087c478bd9Sstevel@tonic-gate 				write_xattr_hdr();
29097c478bd9Sstevel@tonic-gate 			}
29107c478bd9Sstevel@tonic-gate 			write_hdr(ARCHIVE_NORMAL, 0);
29117c478bd9Sstevel@tonic-gate 			return (0);
29127c478bd9Sstevel@tonic-gate 		}
29137c478bd9Sstevel@tonic-gate 		if (G_p->g_nlink == 1) {
29147c478bd9Sstevel@tonic-gate 			data_out();
29157c478bd9Sstevel@tonic-gate 			return (0);
29167c478bd9Sstevel@tonic-gate 		}
29177c478bd9Sstevel@tonic-gate 		tl_p = add_lnk(&ttl_p);
29187c478bd9Sstevel@tonic-gate 		l_p = ttl_p;
29197c478bd9Sstevel@tonic-gate 		if (tl_p == l_p) { /* first link to this file encountered */
29207c478bd9Sstevel@tonic-gate 			data_out();
29217c478bd9Sstevel@tonic-gate 			return (0);
29227c478bd9Sstevel@tonic-gate 		}
29237c478bd9Sstevel@tonic-gate 		(void) strncpy(T_lname, l_p->L_gen.g_nam_p,
29247c478bd9Sstevel@tonic-gate 		    l_p->L_gen.g_namesz);
29257c478bd9Sstevel@tonic-gate 
29267c478bd9Sstevel@tonic-gate 		/*
29277c478bd9Sstevel@tonic-gate 		 * check if linkname is greater than 100 characters
29287c478bd9Sstevel@tonic-gate 		 */
29297c478bd9Sstevel@tonic-gate 		if (strlen(T_lname) > NAMSIZ) {
29307c478bd9Sstevel@tonic-gate 			msg(EPOST, "cpio: %s: linkname %s is greater than %d",
29317c478bd9Sstevel@tonic-gate 			    G_p->g_nam_p, T_lname, NAMSIZ);
29327c478bd9Sstevel@tonic-gate 			return (1);
29337c478bd9Sstevel@tonic-gate 		}
29347c478bd9Sstevel@tonic-gate 
29357c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
29367c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), tl_p->L_gen.g_nam_p);
29377c478bd9Sstevel@tonic-gate 
29387c478bd9Sstevel@tonic-gate 		/* find the lnk entry in sublist, unlink it, and free it */
29397c478bd9Sstevel@tonic-gate 		for (; ttl_p->L_lnk_p != NULL;
29407c478bd9Sstevel@tonic-gate 		    ttl_p = ttl_p->L_lnk_p) {
29417c478bd9Sstevel@tonic-gate 			if (ttl_p->L_lnk_p == tl_p) {
29427c478bd9Sstevel@tonic-gate 				ttl_p->L_lnk_p = tl_p->L_lnk_p;
29437c478bd9Sstevel@tonic-gate 				free(tl_p->L_gen.g_nam_p);
29447c478bd9Sstevel@tonic-gate 				free(tl_p);
29457c478bd9Sstevel@tonic-gate 				break;
29467c478bd9Sstevel@tonic-gate 			}
29477c478bd9Sstevel@tonic-gate 		}
29487c478bd9Sstevel@tonic-gate 
29497c478bd9Sstevel@tonic-gate 		return (0);
29507c478bd9Sstevel@tonic-gate 	}
29517c478bd9Sstevel@tonic-gate 	if (Adir) {
29527c478bd9Sstevel@tonic-gate 		/*
29537c478bd9Sstevel@tonic-gate 		 * ACL has been retrieved in getname().
29547c478bd9Sstevel@tonic-gate 		 */
29557c478bd9Sstevel@tonic-gate 		if (Pflag) {
29567c478bd9Sstevel@tonic-gate 			char    *secinfo = NULL;
29577c478bd9Sstevel@tonic-gate 			int	len = 0;
29587c478bd9Sstevel@tonic-gate 
29597c478bd9Sstevel@tonic-gate 			/* append security attributes */
2960fa9e4066Sahrens 			if ((append_secattr(&secinfo, &len, aclp)) == -1)
29617c478bd9Sstevel@tonic-gate 				msg(ERR, "can create security information");
29627c478bd9Sstevel@tonic-gate 
29637c478bd9Sstevel@tonic-gate 			/* call append_secattr() if more than one */
29647c478bd9Sstevel@tonic-gate 
29657c478bd9Sstevel@tonic-gate 			if (len > 0) {
29667c478bd9Sstevel@tonic-gate 			/* write ancillary */
29677c478bd9Sstevel@tonic-gate 				(void) write_hdr(ARCHIVE_ACL, (off_t)len);
29687c478bd9Sstevel@tonic-gate 				(void) write_ancillary(secinfo, len);
29697c478bd9Sstevel@tonic-gate 			}
29707c478bd9Sstevel@tonic-gate 		}
29717c478bd9Sstevel@tonic-gate 
29727c478bd9Sstevel@tonic-gate 		if (Gen.g_attrnam_p != (char *)NULL) {
29737c478bd9Sstevel@tonic-gate 			write_xattr_hdr();
29747c478bd9Sstevel@tonic-gate 		}
29757c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
29767c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
29777c478bd9Sstevel@tonic-gate 		return (0);
29787c478bd9Sstevel@tonic-gate 	}
29797c478bd9Sstevel@tonic-gate 	if (G_p->g_nlink == 1) {
29807c478bd9Sstevel@tonic-gate 		data_out();
29817c478bd9Sstevel@tonic-gate 		return (0);
29827c478bd9Sstevel@tonic-gate 	} else {
29837c478bd9Sstevel@tonic-gate 		tl_p = add_lnk(&ttl_p);
29847c478bd9Sstevel@tonic-gate 		l_p = ttl_p;
29857c478bd9Sstevel@tonic-gate 
29867c478bd9Sstevel@tonic-gate 		if (l_p->L_cnt == l_p->L_gen.g_nlink)
29877c478bd9Sstevel@tonic-gate 			cleanup = 1;
29887c478bd9Sstevel@tonic-gate 		else if (Onecopy && G_p->g_attrnam_p == (char *)NULL) {
29897c478bd9Sstevel@tonic-gate 			return (0); /* don't process data yet */
29907c478bd9Sstevel@tonic-gate 		}
29917c478bd9Sstevel@tonic-gate 	}
29927c478bd9Sstevel@tonic-gate 	if (Onecopy && G_p->g_attrnam_p == (char *)NULL) {
29937c478bd9Sstevel@tonic-gate 		tl_p = l_p;
29947c478bd9Sstevel@tonic-gate 		while (tl_p->L_lnk_p != (struct Lnk *)NULL) {
29957c478bd9Sstevel@tonic-gate 			G_p = &tl_p->L_gen;
29967c478bd9Sstevel@tonic-gate 			G_p->g_filesz = (off_t)0;
29977c478bd9Sstevel@tonic-gate 			/* one link with the acl is sufficient */
29987c478bd9Sstevel@tonic-gate 			write_hdr(ARCHIVE_NORMAL, (off_t)0);
29997c478bd9Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
30007c478bd9Sstevel@tonic-gate 			tl_p = tl_p->L_lnk_p;
30017c478bd9Sstevel@tonic-gate 		}
30027c478bd9Sstevel@tonic-gate 		G_p = &tl_p->L_gen;
30037c478bd9Sstevel@tonic-gate 		if (open_dirfd() != 0)
30047c478bd9Sstevel@tonic-gate 			return (1);
30057c478bd9Sstevel@tonic-gate 	}
30067c478bd9Sstevel@tonic-gate 	/* old style: has acl and data for every link */
30077c478bd9Sstevel@tonic-gate 	data_out();
30087c478bd9Sstevel@tonic-gate 	if (cleanup)
30097c478bd9Sstevel@tonic-gate 		reclaim(l_p);
30107c478bd9Sstevel@tonic-gate 	return (0);
30117c478bd9Sstevel@tonic-gate }
30127c478bd9Sstevel@tonic-gate 
30137c478bd9Sstevel@tonic-gate /*
30147c478bd9Sstevel@tonic-gate  * file_pass:  If the -l option is set (link files when possible), and the
30157c478bd9Sstevel@tonic-gate  * source and destination file systems are the same, link the source file
30167c478bd9Sstevel@tonic-gate  * (G_p->g_nam_p) to the destination file (Fullnam) and return.  If not a
30177c478bd9Sstevel@tonic-gate  * linked file, transfer the data.  Otherwise, the first link to a file
30187c478bd9Sstevel@tonic-gate  * encountered is transferred normally and subsequent links are linked to it.
30197c478bd9Sstevel@tonic-gate  */
30207c478bd9Sstevel@tonic-gate 
30217c478bd9Sstevel@tonic-gate static int
30227c478bd9Sstevel@tonic-gate file_pass(void)
30237c478bd9Sstevel@tonic-gate {
30247c478bd9Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
30257c478bd9Sstevel@tonic-gate 	struct Lnk *ttl_p;
30267c478bd9Sstevel@tonic-gate 	char *save_name;
30277c478bd9Sstevel@tonic-gate 	int size;
30287c478bd9Sstevel@tonic-gate 	int cwd;
30297c478bd9Sstevel@tonic-gate 	char *lfrom, *lto;
30307c478bd9Sstevel@tonic-gate 
30317c478bd9Sstevel@tonic-gate 	G_p = &Gen;
30327c478bd9Sstevel@tonic-gate 
30337c478bd9Sstevel@tonic-gate 	if (Adir && !(Args & OCd)) {
30347c478bd9Sstevel@tonic-gate 		msg(ERR, "Use -d option to copy \"%s\"", G_p->g_nam_p);
30357c478bd9Sstevel@tonic-gate 		return (FILE_PASS_ERR);
30367c478bd9Sstevel@tonic-gate 	}
30377c478bd9Sstevel@tonic-gate 
30387c478bd9Sstevel@tonic-gate 	save_name = G_p->g_nam_p;
30397c478bd9Sstevel@tonic-gate 
30407c478bd9Sstevel@tonic-gate 	while (*(G_p->g_nam_p) == '/') {
30417c478bd9Sstevel@tonic-gate 		G_p->g_nam_p++;
30427c478bd9Sstevel@tonic-gate 	}
30437c478bd9Sstevel@tonic-gate 
30447c478bd9Sstevel@tonic-gate 	(void) strcpy(Full_p,
30457c478bd9Sstevel@tonic-gate 	    (G_p->g_attrfnam_p == (char *)NULL) ?
30467c478bd9Sstevel@tonic-gate 	    G_p->g_nam_p : G_p->g_attrfnam_p);
30477c478bd9Sstevel@tonic-gate 
30487c478bd9Sstevel@tonic-gate 	if (G_p->g_attrnam_p == (char *)NULL) {
30497c478bd9Sstevel@tonic-gate 		G_p->g_passdirfd = open_dir(Fullnam_p);
30507c478bd9Sstevel@tonic-gate 
30517c478bd9Sstevel@tonic-gate 		if (G_p->g_passdirfd == -1) {
30527c478bd9Sstevel@tonic-gate 			msg(ERRN,
30537c478bd9Sstevel@tonic-gate 			    "Cannot open/create \"%s\"", Fullnam_p);
30547c478bd9Sstevel@tonic-gate 			return (FILE_PASS_ERR);
30557c478bd9Sstevel@tonic-gate 		}
30567c478bd9Sstevel@tonic-gate 	} else {
30577c478bd9Sstevel@tonic-gate 		G_p->g_passdirfd = attropen(Fullnam_p, ".", O_RDONLY);
30587c478bd9Sstevel@tonic-gate 
30597c478bd9Sstevel@tonic-gate 		if (G_p->g_passdirfd == -1) {
30607c478bd9Sstevel@tonic-gate 			G_p->g_passdirfd = retry_attrdir_open(Fullnam_p);
30617c478bd9Sstevel@tonic-gate 
30627c478bd9Sstevel@tonic-gate 			if (G_p->g_passdirfd == -1) {
30637c478bd9Sstevel@tonic-gate 				msg(ERRN,
30647c478bd9Sstevel@tonic-gate 				    "Cannot open attribute directory of"
30657c478bd9Sstevel@tonic-gate 				    " file \"%s\"", Fullnam_p);
30667c478bd9Sstevel@tonic-gate 				return (FILE_PASS_ERR);
30677c478bd9Sstevel@tonic-gate 			}
30687c478bd9Sstevel@tonic-gate 		}
30697c478bd9Sstevel@tonic-gate 	}
30707c478bd9Sstevel@tonic-gate 
30717c478bd9Sstevel@tonic-gate 	if (Args & OCl) {
30727c478bd9Sstevel@tonic-gate 		/* We are linking back to the source directory. */
30737c478bd9Sstevel@tonic-gate 
30747c478bd9Sstevel@tonic-gate 		if (!Adir) {
30757c478bd9Sstevel@tonic-gate 			char *existingfile = save_name;
30767c478bd9Sstevel@tonic-gate 
30777c478bd9Sstevel@tonic-gate 			if ((Args & OCL) && issymlink) {
30787c478bd9Sstevel@tonic-gate 				/* We are chasing symlinks. */
30797c478bd9Sstevel@tonic-gate 
30807c478bd9Sstevel@tonic-gate 				if ((size = readlink(save_name, Symlnk_p,
30817c478bd9Sstevel@tonic-gate 				    MAXPATHLEN)) < 0) {
30827c478bd9Sstevel@tonic-gate 					msg(ERRN,
30837c478bd9Sstevel@tonic-gate 					    "Cannot read symbolic link \"%s\"",
30847c478bd9Sstevel@tonic-gate 					    save_name);
30857c478bd9Sstevel@tonic-gate 					return (FILE_PASS_ERR);
30867c478bd9Sstevel@tonic-gate 				}
30877c478bd9Sstevel@tonic-gate 
30887c478bd9Sstevel@tonic-gate 				Symlnk_p[size] = '\0';
30897c478bd9Sstevel@tonic-gate 				existingfile = Symlnk_p;
30907c478bd9Sstevel@tonic-gate 			}
30917c478bd9Sstevel@tonic-gate 
30927c478bd9Sstevel@tonic-gate 			if (G_p->g_attrnam_p == (char *)NULL) {
30937c478bd9Sstevel@tonic-gate 				if (creat_lnk(G_p->g_passdirfd,
30947c478bd9Sstevel@tonic-gate 				    existingfile, Fullnam_p) == 0) {
30957c478bd9Sstevel@tonic-gate 					return (FILE_LINKED);
30967c478bd9Sstevel@tonic-gate 				}
30977c478bd9Sstevel@tonic-gate 			}
30987c478bd9Sstevel@tonic-gate 		}
30997c478bd9Sstevel@tonic-gate 	}
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate 	if ((G_p->g_mode & Ftype) == S_IFLNK && !(Args & OCL)) {
31027c478bd9Sstevel@tonic-gate 		/* The archive file is a symlink. */
31037c478bd9Sstevel@tonic-gate 
31047c478bd9Sstevel@tonic-gate 		errno = 0;
31057c478bd9Sstevel@tonic-gate 
31067c478bd9Sstevel@tonic-gate 		if ((size = readlink(save_name, Symlnk_p, MAXPATHLEN)) < 0) {
31077c478bd9Sstevel@tonic-gate 			msg(ERRN,
31087c478bd9Sstevel@tonic-gate 			    "Cannot read symbolic link \"%s\"", save_name);
31097c478bd9Sstevel@tonic-gate 			return (FILE_PASS_ERR);
31107c478bd9Sstevel@tonic-gate 		}
31117c478bd9Sstevel@tonic-gate 
31127c478bd9Sstevel@tonic-gate 		errno = 0;
31137c478bd9Sstevel@tonic-gate 		(void) missdir(Fullnam_p);
31147c478bd9Sstevel@tonic-gate 		*(Symlnk_p + size) = '\0';
31157c478bd9Sstevel@tonic-gate 
31167c478bd9Sstevel@tonic-gate 		if (symlink(Symlnk_p, Fullnam_p) < 0) {
31177c478bd9Sstevel@tonic-gate 			if (errno == EEXIST) {
31187c478bd9Sstevel@tonic-gate 				if (openout(G_p->g_passdirfd) < 0) {
3119647ab8f4Sceastha 					if (errno != EEXIST) {
3120647ab8f4Sceastha 						msg(ERRN,
3121647ab8f4Sceastha 						    "Cannot create \"%s\"",
3122647ab8f4Sceastha 						    Fullnam_p);
3123647ab8f4Sceastha 					}
31247c478bd9Sstevel@tonic-gate 					return (FILE_PASS_ERR);
31257c478bd9Sstevel@tonic-gate 				}
31267c478bd9Sstevel@tonic-gate 			} else {
31277c478bd9Sstevel@tonic-gate 				msg(ERRN, "Cannot create \"%s\"", Fullnam_p);
31287c478bd9Sstevel@tonic-gate 				return (FILE_PASS_ERR);
31297c478bd9Sstevel@tonic-gate 			}
3130647ab8f4Sceastha 		} else {
3131647ab8f4Sceastha 			if (Args & OCR) {
3132647ab8f4Sceastha 				if (lchown(Fullnam_p, (int)Rpw_p->pw_uid,
3133647ab8f4Sceastha 				    (int)Rpw_p->pw_gid) < 0) {
3134647ab8f4Sceastha 					msg(ERRN,
3135647ab8f4Sceastha 					    "Error during chown() of \"%s\"",
3136647ab8f4Sceastha 					    Fullnam_p);
31377c478bd9Sstevel@tonic-gate 				}
3138647ab8f4Sceastha 			} else if ((lchown(Fullnam_p, (int)G_p->g_uid,
3139647ab8f4Sceastha 			    (int)G_p->g_gid) < 0) && privileged) {
31407c478bd9Sstevel@tonic-gate 				msg(ERRN,
31417c478bd9Sstevel@tonic-gate 				    "Error during chown() of \"%s\"",
31427c478bd9Sstevel@tonic-gate 				    Fullnam_p);
31437c478bd9Sstevel@tonic-gate 			}
31447c478bd9Sstevel@tonic-gate 		}
31457c478bd9Sstevel@tonic-gate 
31467c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), Fullnam_p);
31477c478bd9Sstevel@tonic-gate 		return (FILE_PASS_ERR);
31487c478bd9Sstevel@tonic-gate 	}
31497c478bd9Sstevel@tonic-gate 
31507c478bd9Sstevel@tonic-gate 	if (!Adir && G_p->g_nlink > 1) {
31517c478bd9Sstevel@tonic-gate 		/* The archive file has hard links. */
31527c478bd9Sstevel@tonic-gate 
31537c478bd9Sstevel@tonic-gate 		tl_p = add_lnk(&ttl_p);
31547c478bd9Sstevel@tonic-gate 		l_p = ttl_p;
31557c478bd9Sstevel@tonic-gate 
31567c478bd9Sstevel@tonic-gate 		if (tl_p == l_p) {
31577c478bd9Sstevel@tonic-gate 			/* The archive file was not found. */
31587c478bd9Sstevel@tonic-gate 
31597c478bd9Sstevel@tonic-gate 			G_p = &tl_p->L_gen;
31607c478bd9Sstevel@tonic-gate 		} else {
31617c478bd9Sstevel@tonic-gate 			/* The archive file was found. */
31627c478bd9Sstevel@tonic-gate 
31637c478bd9Sstevel@tonic-gate 			cwd = -1;
31647c478bd9Sstevel@tonic-gate 
31657c478bd9Sstevel@tonic-gate 			if (l_p->L_gen.g_attrnam_p != (char *)NULL) {
31667c478bd9Sstevel@tonic-gate 				/* We are linking an attribute */
31677c478bd9Sstevel@tonic-gate 
31687c478bd9Sstevel@tonic-gate 				(void) strcpy(Lnkend_p, l_p->L_gen.g_attrnam_p);
31697c478bd9Sstevel@tonic-gate 				cwd = save_cwd();
31707c478bd9Sstevel@tonic-gate 				(void) fchdir(G_p->g_passdirfd);
31717c478bd9Sstevel@tonic-gate 				lfrom = get_component(Lnknam_p);
31727c478bd9Sstevel@tonic-gate 				lto = tl_p->L_gen.g_attrnam_p;
31737c478bd9Sstevel@tonic-gate 			} else {
31747c478bd9Sstevel@tonic-gate 				/* We are not linking an attribute */
31757c478bd9Sstevel@tonic-gate 
31767c478bd9Sstevel@tonic-gate 				(void) strcpy(Lnkend_p, l_p->L_gen.g_nam_p);
31777c478bd9Sstevel@tonic-gate 				(void) strcpy(Full_p, tl_p->L_gen.g_nam_p);
31787c478bd9Sstevel@tonic-gate 				lfrom = Lnknam_p;
31797c478bd9Sstevel@tonic-gate 				lto = Fullnam_p;
31807c478bd9Sstevel@tonic-gate 			}
31817c478bd9Sstevel@tonic-gate 
31827c478bd9Sstevel@tonic-gate 			(void) creat_lnk(G_p->g_passdirfd, lfrom, lto);
31837c478bd9Sstevel@tonic-gate 
31847c478bd9Sstevel@tonic-gate 			if (cwd) {
31857c478bd9Sstevel@tonic-gate 				rest_cwd(cwd);
31867c478bd9Sstevel@tonic-gate 			}
31877c478bd9Sstevel@tonic-gate 
31887c478bd9Sstevel@tonic-gate 			l_p->L_lnk_p = (struct Lnk *)NULL;
31897c478bd9Sstevel@tonic-gate 			free(tl_p->L_gen.g_nam_p);
31907c478bd9Sstevel@tonic-gate 			free(tl_p);
31917c478bd9Sstevel@tonic-gate 
31927c478bd9Sstevel@tonic-gate 			if (l_p->L_cnt == G_p->g_nlink) {
31937c478bd9Sstevel@tonic-gate 				reclaim(l_p);
31947c478bd9Sstevel@tonic-gate 			}
31957c478bd9Sstevel@tonic-gate 
31967c478bd9Sstevel@tonic-gate 			return (FILE_LINKED);
31977c478bd9Sstevel@tonic-gate 		}
31987c478bd9Sstevel@tonic-gate 	}
31997c478bd9Sstevel@tonic-gate 
32007c478bd9Sstevel@tonic-gate 	if (Adir || Aspec) {
32017c478bd9Sstevel@tonic-gate 		/*
32027c478bd9Sstevel@tonic-gate 		 * The archive file is a directory,  block special, char
32037c478bd9Sstevel@tonic-gate 		 * special or a fifo.
32047c478bd9Sstevel@tonic-gate 		 */
32057c478bd9Sstevel@tonic-gate 
32067c478bd9Sstevel@tonic-gate 		if (creat_spec(G_p->g_passdirfd) > 0) {
32077c478bd9Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), Fullnam_p);
32087c478bd9Sstevel@tonic-gate 		}
32097c478bd9Sstevel@tonic-gate 	} else if ((Ofile = openout(G_p->g_passdirfd)) > 0) {
32107c478bd9Sstevel@tonic-gate 		data_pass();
32117c478bd9Sstevel@tonic-gate 	}
32127c478bd9Sstevel@tonic-gate 
32137c478bd9Sstevel@tonic-gate 	return (FILE_COPIED);
32147c478bd9Sstevel@tonic-gate }
32157c478bd9Sstevel@tonic-gate 
32167c478bd9Sstevel@tonic-gate /*
32177c478bd9Sstevel@tonic-gate  * flush_lnks: With new linked file handling, linked files are not archived
32187c478bd9Sstevel@tonic-gate  * until all links have been collected.  When the end of the list of filenames
32197c478bd9Sstevel@tonic-gate  * to archive has been reached, all files that did not encounter all their links
32207c478bd9Sstevel@tonic-gate  * are written out with actual (encountered) link counts.  A file with n links
32217c478bd9Sstevel@tonic-gate  * (that are archived) will be represented by n headers (one for each link (the
32227c478bd9Sstevel@tonic-gate  * first n - 1 have g_filesz set to 0)) followed by the data for the file.
32237c478bd9Sstevel@tonic-gate  */
32247c478bd9Sstevel@tonic-gate 
32257c478bd9Sstevel@tonic-gate static void
32267c478bd9Sstevel@tonic-gate flush_lnks(void)
32277c478bd9Sstevel@tonic-gate {
32287c478bd9Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
32297c478bd9Sstevel@tonic-gate 	off_t tfsize;
32307c478bd9Sstevel@tonic-gate 
32317c478bd9Sstevel@tonic-gate 	l_p = Lnk_hd.L_nxt_p;
32327c478bd9Sstevel@tonic-gate 	while (l_p != &Lnk_hd) {
32337c478bd9Sstevel@tonic-gate 		(void) strcpy(Gen.g_nam_p, l_p->L_gen.g_nam_p);
32347c478bd9Sstevel@tonic-gate 		if (stat(Gen.g_nam_p, &SrcSt) == 0) { /* check if file exists */
32357c478bd9Sstevel@tonic-gate 			tl_p = l_p;
32367c478bd9Sstevel@tonic-gate 			(void) creat_hdr();
32377c478bd9Sstevel@tonic-gate 			Gen.g_nlink = l_p->L_cnt; /* "actual" link count */
32387c478bd9Sstevel@tonic-gate 			tfsize = Gen.g_filesz;
32397c478bd9Sstevel@tonic-gate 			Gen.g_filesz = (off_t)0;
32407c478bd9Sstevel@tonic-gate 			G_p = &Gen;
32417c478bd9Sstevel@tonic-gate 			while (tl_p != (struct Lnk *)NULL) {
32427c478bd9Sstevel@tonic-gate 				Gen.g_nam_p = tl_p->L_gen.g_nam_p;
32437c478bd9Sstevel@tonic-gate 				Gen.g_namesz = tl_p->L_gen.g_namesz;
32447c478bd9Sstevel@tonic-gate 				if (tl_p->L_lnk_p == (struct Lnk *)NULL) {
32457c478bd9Sstevel@tonic-gate 					Gen.g_filesz = tfsize;
32467c478bd9Sstevel@tonic-gate 					if (open_dirfd() != 0) {
32477c478bd9Sstevel@tonic-gate 						break;
32487c478bd9Sstevel@tonic-gate 					}
32497c478bd9Sstevel@tonic-gate 					data_out();
32507c478bd9Sstevel@tonic-gate 					break;
32517c478bd9Sstevel@tonic-gate 				}
32527c478bd9Sstevel@tonic-gate 				write_hdr(ARCHIVE_NORMAL,
32537c478bd9Sstevel@tonic-gate 				    (off_t)0); /* header only */
32547c478bd9Sstevel@tonic-gate 				VERBOSE((Args & (OCv | OCV)), Gen.g_nam_p);
32557c478bd9Sstevel@tonic-gate 				tl_p = tl_p->L_lnk_p;
32567c478bd9Sstevel@tonic-gate 			}
32577c478bd9Sstevel@tonic-gate 			Gen.g_nam_p = Nam_p;
32587c478bd9Sstevel@tonic-gate 		} else /* stat(Gen.g_nam_p, &SrcSt) == 0 */
32597c478bd9Sstevel@tonic-gate 			msg(ERR, "\"%s%s%s\" has disappeared",
32607c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
32617c478bd9Sstevel@tonic-gate 			    Gen.g_nam_p : Gen.g_attrfnam_p,
32627c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
32637c478bd9Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
32647c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
32657c478bd9Sstevel@tonic-gate 			    "" : Gen.g_attrnam_p);
32667c478bd9Sstevel@tonic-gate 		tl_p = l_p;
32677c478bd9Sstevel@tonic-gate 		l_p = l_p->L_nxt_p;
32687c478bd9Sstevel@tonic-gate 		reclaim(tl_p);
32697c478bd9Sstevel@tonic-gate 	} /* l_p != &Lnk_hd */
32707c478bd9Sstevel@tonic-gate }
32717c478bd9Sstevel@tonic-gate 
3272*da6c28aaSamw #if defined(O_XATTR)
3273*da6c28aaSamw static int
3274*da6c28aaSamw is_sysattr(char *name)
3275*da6c28aaSamw {
3276*da6c28aaSamw 	return ((strcmp(name, VIEW_READONLY) == 0) ||
3277*da6c28aaSamw 	    (strcmp(name, VIEW_READWRITE) == 0));
3278*da6c28aaSamw }
3279*da6c28aaSamw #endif
3280*da6c28aaSamw 
32817c478bd9Sstevel@tonic-gate /*
32827c478bd9Sstevel@tonic-gate  * gethdr: Get a header from the archive, validate it and check for the trailer.
32837c478bd9Sstevel@tonic-gate  * Any user specified Hdr_type is ignored (set to NONE in main).  Hdr_type is
32847c478bd9Sstevel@tonic-gate  * set appropriately after a valid header is found.  Unless the -k option is
32857c478bd9Sstevel@tonic-gate  * set a corrupted header causes an exit with an error.  I/O errors during
32867c478bd9Sstevel@tonic-gate  * examination of any part of the header cause gethdr to throw away any current
32877c478bd9Sstevel@tonic-gate  * data and start over.  Other errors during examination of any part of the
32887c478bd9Sstevel@tonic-gate  * header cause gethdr to advance one byte and continue the examination.
32897c478bd9Sstevel@tonic-gate  */
32907c478bd9Sstevel@tonic-gate 
32917c478bd9Sstevel@tonic-gate static int
32927c478bd9Sstevel@tonic-gate gethdr(void)
32937c478bd9Sstevel@tonic-gate {
32947c478bd9Sstevel@tonic-gate 	ushort_t ftype;
32957c478bd9Sstevel@tonic-gate 	int hit = NONE, cnt = 0;
32967c478bd9Sstevel@tonic-gate 	int goodhdr, hsize, offset;
32977c478bd9Sstevel@tonic-gate 	int bswap = 0;
32987c478bd9Sstevel@tonic-gate 	char *preptr;
32997c478bd9Sstevel@tonic-gate 	int k = 0;
33007c478bd9Sstevel@tonic-gate 	int j;
3301fa9e4066Sahrens 	int error;
3302fa9e4066Sahrens 	int aclcnt;
33037c478bd9Sstevel@tonic-gate 
33047c478bd9Sstevel@tonic-gate 	Gen.g_nam_p = Nam_p;
33057c478bd9Sstevel@tonic-gate 	do { /* hit == NONE && (Args & OCk) && Buffr.b_cnt > 0 */
33067c478bd9Sstevel@tonic-gate 		FILL(Hdrsz);
33077c478bd9Sstevel@tonic-gate 		switch (Hdr_type) {
33087c478bd9Sstevel@tonic-gate 		case NONE:
33097c478bd9Sstevel@tonic-gate 		case BIN:
33107c478bd9Sstevel@tonic-gate 			Binmag.b_byte[0] = Buffr.b_out_p[0];
33117c478bd9Sstevel@tonic-gate 			Binmag.b_byte[1] = Buffr.b_out_p[1];
33127c478bd9Sstevel@tonic-gate 			if ((Binmag.b_half == CMN_BIN) ||
33137c478bd9Sstevel@tonic-gate 			    (Binmag.b_half == CMN_BBS)) {
33147c478bd9Sstevel@tonic-gate 				hit = read_hdr(BIN);
33157c478bd9Sstevel@tonic-gate 				if (Hdr_type == NONE)
33167c478bd9Sstevel@tonic-gate 					bswap = 1;
33177c478bd9Sstevel@tonic-gate 				hsize = HDRSZ + Gen.g_namesz;
33187c478bd9Sstevel@tonic-gate 				break;
33197c478bd9Sstevel@tonic-gate 			}
33207c478bd9Sstevel@tonic-gate 			if (Hdr_type != NONE)
33217c478bd9Sstevel@tonic-gate 				break;
33227c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
33237c478bd9Sstevel@tonic-gate 		case CHR:
33247c478bd9Sstevel@tonic-gate 			if (!(strncmp(Buffr.b_out_p, CMS_CHR, CMS_LEN))) {
33257c478bd9Sstevel@tonic-gate 				hit = read_hdr(CHR);
33267c478bd9Sstevel@tonic-gate 				hsize = CHRSZ + Gen.g_namesz;
33277c478bd9Sstevel@tonic-gate 				break;
33287c478bd9Sstevel@tonic-gate 			}
33297c478bd9Sstevel@tonic-gate 			if (Hdr_type != NONE)
33307c478bd9Sstevel@tonic-gate 				break;
33317c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
33327c478bd9Sstevel@tonic-gate 		case ASC:
33337c478bd9Sstevel@tonic-gate 			if (!(strncmp(Buffr.b_out_p, CMS_ASC, CMS_LEN))) {
33347c478bd9Sstevel@tonic-gate 				hit = read_hdr(ASC);
33357c478bd9Sstevel@tonic-gate 				hsize = ASCSZ + Gen.g_namesz;
33367c478bd9Sstevel@tonic-gate 				Max_namesz = APATH;
33377c478bd9Sstevel@tonic-gate 				break;
33387c478bd9Sstevel@tonic-gate 			}
33397c478bd9Sstevel@tonic-gate 			if (Hdr_type != NONE)
33407c478bd9Sstevel@tonic-gate 				break;
33417c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
33427c478bd9Sstevel@tonic-gate 		case CRC:
33437c478bd9Sstevel@tonic-gate 			if (!(strncmp(Buffr.b_out_p, CMS_CRC, CMS_LEN))) {
33447c478bd9Sstevel@tonic-gate 				hit = read_hdr(CRC);
33457c478bd9Sstevel@tonic-gate 				hsize = ASCSZ + Gen.g_namesz;
33467c478bd9Sstevel@tonic-gate 				Max_namesz = APATH;
33477c478bd9Sstevel@tonic-gate 				break;
33487c478bd9Sstevel@tonic-gate 			}
33497c478bd9Sstevel@tonic-gate 			if (Hdr_type != NONE)
33507c478bd9Sstevel@tonic-gate 				break;
33517c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
33527c478bd9Sstevel@tonic-gate 
33537c478bd9Sstevel@tonic-gate 		case BAR:
33547c478bd9Sstevel@tonic-gate 			if (Hdr_p != NULL && strcmp(Hdr_p, "bar") == 0) {
33557c478bd9Sstevel@tonic-gate 				Hdrsz = BARSZ;
33567c478bd9Sstevel@tonic-gate 				FILL(Hdrsz);
33577c478bd9Sstevel@tonic-gate 				if ((hit = read_hdr(BAR)) == NONE) {
33587c478bd9Sstevel@tonic-gate 					Hdrsz = ASCSZ;
33597c478bd9Sstevel@tonic-gate 					break;
33607c478bd9Sstevel@tonic-gate 				}
33617c478bd9Sstevel@tonic-gate 				hit = BAR;
33627c478bd9Sstevel@tonic-gate 				hsize = BARSZ;
33637c478bd9Sstevel@tonic-gate 				break;
33647c478bd9Sstevel@tonic-gate 			}
33657c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
33667c478bd9Sstevel@tonic-gate 
33677c478bd9Sstevel@tonic-gate 		case USTAR:
33687c478bd9Sstevel@tonic-gate 			if (Hdr_p != NULL && strcmp(Hdr_p, "ustar") == 0) {
33697c478bd9Sstevel@tonic-gate 				Hdrsz = TARSZ;
33707c478bd9Sstevel@tonic-gate 				FILL(Hdrsz);
33717c478bd9Sstevel@tonic-gate 				if ((hit = read_hdr(USTAR)) == NONE) {
33727c478bd9Sstevel@tonic-gate 					Hdrsz = ASCSZ;
33737c478bd9Sstevel@tonic-gate 					break;
33747c478bd9Sstevel@tonic-gate 				}
33757c478bd9Sstevel@tonic-gate 				hit = USTAR;
33767c478bd9Sstevel@tonic-gate 				hsize = TARSZ;
33777c478bd9Sstevel@tonic-gate 				break;
33787c478bd9Sstevel@tonic-gate 			}
33797c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
33807c478bd9Sstevel@tonic-gate 		case TAR:
33817c478bd9Sstevel@tonic-gate 			if (Hdr_p != NULL && strcmp(Hdr_p, "tar") == 0) {
33827c478bd9Sstevel@tonic-gate 				Hdrsz = TARSZ;
33837c478bd9Sstevel@tonic-gate 				FILL(Hdrsz);
33847c478bd9Sstevel@tonic-gate 				if ((hit = read_hdr(TAR)) == NONE) {
33857c478bd9Sstevel@tonic-gate 					Hdrsz = ASCSZ;
33867c478bd9Sstevel@tonic-gate 					break;
33877c478bd9Sstevel@tonic-gate 				}
33887c478bd9Sstevel@tonic-gate 				hit = TAR;
33897c478bd9Sstevel@tonic-gate 				hsize = TARSZ;
33907c478bd9Sstevel@tonic-gate 				break;
33917c478bd9Sstevel@tonic-gate 			}
33927c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
33937c478bd9Sstevel@tonic-gate 		default:
33947c478bd9Sstevel@tonic-gate 			msg(EXT, "Impossible header type.");
33957c478bd9Sstevel@tonic-gate 		} /* Hdr_type */
33967c478bd9Sstevel@tonic-gate 
33977c478bd9Sstevel@tonic-gate 		if (hit == TAR || hit == USTAR) {
33987c478bd9Sstevel@tonic-gate 			Gen.g_nam_p = &nambuf[0];
33997c478bd9Sstevel@tonic-gate 		}
34007c478bd9Sstevel@tonic-gate 
34017c478bd9Sstevel@tonic-gate 		if (hit != NONE) {
34027c478bd9Sstevel@tonic-gate 			FILL(hsize);
34037c478bd9Sstevel@tonic-gate 			goodhdr = 1;
34047c478bd9Sstevel@tonic-gate 			if (Gen.g_filesz < (off_t)0 || Gen.g_namesz < 1)
34057c478bd9Sstevel@tonic-gate 				goodhdr = 0;
34067c478bd9Sstevel@tonic-gate 			if ((hit != USTAR) && (hit != TAR))
34077c478bd9Sstevel@tonic-gate 				if (Gen.g_namesz - 1 > Max_namesz)
34087c478bd9Sstevel@tonic-gate 					goodhdr = 0;
34097c478bd9Sstevel@tonic-gate 			/* TAR and USTAR */
34107c478bd9Sstevel@tonic-gate 			if ((hit == USTAR) || (hit == TAR)) {
34117c478bd9Sstevel@tonic-gate 				if (*Gen.g_nam_p == '\0') { /* tar trailer */
34127c478bd9Sstevel@tonic-gate 					goodhdr = 1;
34137c478bd9Sstevel@tonic-gate 				} else {
34147c478bd9Sstevel@tonic-gate 
34157c478bd9Sstevel@tonic-gate 					G_p = &Gen;
34167c478bd9Sstevel@tonic-gate 					if (G_p->g_cksum !=
34177c478bd9Sstevel@tonic-gate 					    cksum(TARTYP, 0, NULL)) {
34187c478bd9Sstevel@tonic-gate 						goodhdr = 0;
34197c478bd9Sstevel@tonic-gate 						msg(ERR,
34207c478bd9Sstevel@tonic-gate 						    "Bad header - checksum "
34217c478bd9Sstevel@tonic-gate 						    "error.");
34227c478bd9Sstevel@tonic-gate 					}
34237c478bd9Sstevel@tonic-gate 				}
34247c478bd9Sstevel@tonic-gate 			} else if (hit != BAR) { /* binary, -c, ASC and CRC */
34257c478bd9Sstevel@tonic-gate 				if (Gen.g_nlink <= (ulong_t)0)
34267c478bd9Sstevel@tonic-gate 					goodhdr = 0;
34277c478bd9Sstevel@tonic-gate 				if (*(Buffr.b_out_p + hsize - 1) != '\0')
34287c478bd9Sstevel@tonic-gate 					goodhdr = 0;
34297c478bd9Sstevel@tonic-gate 			}
34307c478bd9Sstevel@tonic-gate 			if (!goodhdr) {
34317c478bd9Sstevel@tonic-gate 				hit = NONE;
34327c478bd9Sstevel@tonic-gate 				if (!(Args & OCk))
34337c478bd9Sstevel@tonic-gate 					break;
34347c478bd9Sstevel@tonic-gate 				msg(ERR,
34357c478bd9Sstevel@tonic-gate 				    "Corrupt header, file(s) may be lost.");
34367c478bd9Sstevel@tonic-gate 			} else {
34377c478bd9Sstevel@tonic-gate 				FILL(hsize);
34387c478bd9Sstevel@tonic-gate 			}
34397c478bd9Sstevel@tonic-gate 		} /* hit != NONE */
34407c478bd9Sstevel@tonic-gate 		if (hit == NONE) {
34417c478bd9Sstevel@tonic-gate 			Buffr.b_out_p++;
34427c478bd9Sstevel@tonic-gate 			Buffr.b_cnt--;
34437c478bd9Sstevel@tonic-gate 			if (!(Args & OCk))
34447c478bd9Sstevel@tonic-gate 				break;
34457c478bd9Sstevel@tonic-gate 			if (!cnt++)
34467c478bd9Sstevel@tonic-gate 				msg(ERR, "Searching for magic number/header.");
34477c478bd9Sstevel@tonic-gate 		}
34487c478bd9Sstevel@tonic-gate 	} while (hit == NONE);
34497c478bd9Sstevel@tonic-gate 	if (hit == NONE) {
34507c478bd9Sstevel@tonic-gate 		if (Hdr_type == NONE)
34517c478bd9Sstevel@tonic-gate 			msg(EXT, "Not a cpio file, bad header.");
34527c478bd9Sstevel@tonic-gate 		else
34537c478bd9Sstevel@tonic-gate 			msg(EXT, "Bad magic number/header.");
34547c478bd9Sstevel@tonic-gate 	} else if (cnt > 0) {
34557c478bd9Sstevel@tonic-gate 		msg(EPOST, "Re-synchronized on magic number/header.");
34567c478bd9Sstevel@tonic-gate 	}
34577c478bd9Sstevel@tonic-gate 	if (Hdr_type == NONE) {
34587c478bd9Sstevel@tonic-gate 		Hdr_type = hit;
34597c478bd9Sstevel@tonic-gate 		switch (Hdr_type) {
34607c478bd9Sstevel@tonic-gate 		case BIN:
34617c478bd9Sstevel@tonic-gate 			if (bswap)
34627c478bd9Sstevel@tonic-gate 				Args |= BSM;
34637c478bd9Sstevel@tonic-gate 			Hdrsz = HDRSZ;
34647c478bd9Sstevel@tonic-gate 			Max_namesz = CPATH;
34657c478bd9Sstevel@tonic-gate 			Pad_val = HALFWD;
34667c478bd9Sstevel@tonic-gate 			Onecopy = 0;
34677c478bd9Sstevel@tonic-gate 			break;
34687c478bd9Sstevel@tonic-gate 		case CHR:
34697c478bd9Sstevel@tonic-gate 			Hdrsz = CHRSZ;
34707c478bd9Sstevel@tonic-gate 			Max_namesz = CPATH;
34717c478bd9Sstevel@tonic-gate 			Pad_val = 0;
34727c478bd9Sstevel@tonic-gate 			Onecopy = 0;
34737c478bd9Sstevel@tonic-gate 			break;
34747c478bd9Sstevel@tonic-gate 		case ASC:
34757c478bd9Sstevel@tonic-gate 		case CRC:
34767c478bd9Sstevel@tonic-gate 			Hdrsz = ASCSZ;
34777c478bd9Sstevel@tonic-gate 			Max_namesz = APATH;
34787c478bd9Sstevel@tonic-gate 			Pad_val = FULLWD;
34797c478bd9Sstevel@tonic-gate 			Onecopy = 1;
34807c478bd9Sstevel@tonic-gate 			break;
34817c478bd9Sstevel@tonic-gate 		case USTAR:
34827c478bd9Sstevel@tonic-gate 			Hdrsz = TARSZ;
34837c478bd9Sstevel@tonic-gate 			Max_namesz = HNAMLEN - 1;
34847c478bd9Sstevel@tonic-gate 			Pad_val = FULLBK;
34857c478bd9Sstevel@tonic-gate 			Onecopy = 0;
34867c478bd9Sstevel@tonic-gate 			break;
34877c478bd9Sstevel@tonic-gate 		case BAR:
34887c478bd9Sstevel@tonic-gate 		case TAR:
34897c478bd9Sstevel@tonic-gate 			Hdrsz = TARSZ;
34907c478bd9Sstevel@tonic-gate 			Max_namesz = TNAMLEN - 1;
34917c478bd9Sstevel@tonic-gate 			Pad_val = FULLBK;
34927c478bd9Sstevel@tonic-gate 			Onecopy = 0;
34937c478bd9Sstevel@tonic-gate 			break;
34947c478bd9Sstevel@tonic-gate 		default:
34957c478bd9Sstevel@tonic-gate 			msg(EXT, "Impossible header type.");
34967c478bd9Sstevel@tonic-gate 		} /* Hdr_type */
34977c478bd9Sstevel@tonic-gate 	} /* Hdr_type == NONE */
34987c478bd9Sstevel@tonic-gate 	if ((Hdr_type == USTAR) || (Hdr_type == TAR) ||
34997c478bd9Sstevel@tonic-gate 	    (Hdr_type == BAR)) {			/* TAR, USTAR, BAR */
35007c478bd9Sstevel@tonic-gate 		Gen.g_namesz = 0;
35017c478bd9Sstevel@tonic-gate 		if (Gen.g_nam_p[0] == '\0')
35027c478bd9Sstevel@tonic-gate 			return (0);
35037c478bd9Sstevel@tonic-gate 		else {
35047c478bd9Sstevel@tonic-gate 			preptr = &prebuf[0];
35057c478bd9Sstevel@tonic-gate 			if (*preptr != (char)NULL) {
35067c478bd9Sstevel@tonic-gate 				k = strlen(&prebuf[0]);
35077c478bd9Sstevel@tonic-gate 				if (k < PRESIZ) {
35087c478bd9Sstevel@tonic-gate 					(void) strcpy(&fullnam[0], &prebuf[0]);
35097c478bd9Sstevel@tonic-gate 					j = 0;
35107c478bd9Sstevel@tonic-gate 					fullnam[k++] = '/';
35117c478bd9Sstevel@tonic-gate 					while ((j < NAMSIZ) && (&nambuf[j] !=
35127c478bd9Sstevel@tonic-gate 					    (char)NULL)) {
35137c478bd9Sstevel@tonic-gate 						fullnam[k] = nambuf[j];
35147c478bd9Sstevel@tonic-gate 						k++; j++;
35157c478bd9Sstevel@tonic-gate 					}
35167c478bd9Sstevel@tonic-gate 					fullnam[k] = '\0';
35177c478bd9Sstevel@tonic-gate 				} else if (k >= PRESIZ) {
35187c478bd9Sstevel@tonic-gate 					k = 0;
35197c478bd9Sstevel@tonic-gate 					while ((k < PRESIZ) && (prebuf[k] !=
35207c478bd9Sstevel@tonic-gate 					    '\0')) {
35217c478bd9Sstevel@tonic-gate 						fullnam[k] = prebuf[k];
35227c478bd9Sstevel@tonic-gate 						k++;
35237c478bd9Sstevel@tonic-gate 					}
35247c478bd9Sstevel@tonic-gate 					fullnam[k++] = '/';
35257c478bd9Sstevel@tonic-gate 					j = 0;
35267c478bd9Sstevel@tonic-gate 					while ((j < NAMSIZ) && (nambuf[j] !=
35277c478bd9Sstevel@tonic-gate 					    '\0')) {
35287c478bd9Sstevel@tonic-gate 						fullnam[k] = nambuf[j];
35297c478bd9Sstevel@tonic-gate 						k++; j++;
35307c478bd9Sstevel@tonic-gate 					}
35317c478bd9Sstevel@tonic-gate 					fullnam[k] = '\0';
35327c478bd9Sstevel@tonic-gate 				}
35337c478bd9Sstevel@tonic-gate 				Gen.g_nam_p = &fullnam[0];
35347c478bd9Sstevel@tonic-gate 			} else
35357c478bd9Sstevel@tonic-gate 				Gen.g_nam_p = &nambuf[0];
35367c478bd9Sstevel@tonic-gate 
35377c478bd9Sstevel@tonic-gate 			/*
35387c478bd9Sstevel@tonic-gate 			 * initialize the buffer so that the prefix will not
35397c478bd9Sstevel@tonic-gate 			 * applied to the next entry in the archive
35407c478bd9Sstevel@tonic-gate 			 */
35417c478bd9Sstevel@tonic-gate 			(void) memset(prebuf, 0, sizeof (prebuf));
35427c478bd9Sstevel@tonic-gate 		}
35437c478bd9Sstevel@tonic-gate 	} else if (Hdr_type != BAR) {
35447c478bd9Sstevel@tonic-gate 		(void) memcpy(Gen.g_nam_p, Buffr.b_out_p + Hdrsz, Gen.g_namesz);
35457c478bd9Sstevel@tonic-gate 		if (!(strcmp(Gen.g_nam_p, "TRAILER!!!")))
35467c478bd9Sstevel@tonic-gate 			return (0);
35477c478bd9Sstevel@tonic-gate 	}
35487c478bd9Sstevel@tonic-gate 	offset = ((hsize + Pad_val) & ~Pad_val);
35497c478bd9Sstevel@tonic-gate 	FILL(offset + Hdrsz);
35507c478bd9Sstevel@tonic-gate 	Thdr_p = (union tblock *)Buffr.b_out_p;
35517c478bd9Sstevel@tonic-gate 	Buffr.b_out_p += offset;
35527c478bd9Sstevel@tonic-gate 	Buffr.b_cnt -= (off_t)offset;
35537c478bd9Sstevel@tonic-gate 	ftype = Gen.g_mode & Ftype;
35547c478bd9Sstevel@tonic-gate 
35557c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
35567c478bd9Sstevel@tonic-gate 	/* extended attribute support */
35577c478bd9Sstevel@tonic-gate 	if (((Gen.g_mode & S_IFMT) == _XATTR_CPIO_MODE) ||
35587c478bd9Sstevel@tonic-gate 	    ((Hdr_type == USTAR || Hdr_type == TAR) &&
35597c478bd9Sstevel@tonic-gate 	    Thdr_p->tbuf.t_typeflag == _XATTR_HDRTYPE)) {
3560*da6c28aaSamw 		char	*tapath;
3561*da6c28aaSamw 
35627c478bd9Sstevel@tonic-gate 		if (xattrp != (struct xattr_buf *)NULL) {
35637c478bd9Sstevel@tonic-gate 			if (xattrbadhead) {
35647c478bd9Sstevel@tonic-gate 				free(xattrhead);
35657c478bd9Sstevel@tonic-gate 				xattrp = NULL;
35667c478bd9Sstevel@tonic-gate 				xattr_linkp = NULL;
35677c478bd9Sstevel@tonic-gate 				xattrhead = NULL;
35687c478bd9Sstevel@tonic-gate 				return (1);
35697c478bd9Sstevel@tonic-gate 			}
3570*da6c28aaSamw 
3571*da6c28aaSamw 			/*
3572*da6c28aaSamw 			 * Skip processing of an attribute if -@ wasn't
3573*da6c28aaSamw 			 * specified, or if -@ was specified and the attribute
3574*da6c28aaSamw 			 * is either an extended system attribute or if the
3575*da6c28aaSamw 			 * attribute name is not a file name, but a path name
3576*da6c28aaSamw 			 * (-@ should only process extended attributes).
3577*da6c28aaSamw 			 */
3578*da6c28aaSamw 			tapath = xattrp->h_names + strlen(xattrp->h_names) + 1;
3579*da6c28aaSamw 			if (((Args & OCt) == 0) && ((Atflag == 0) ||
3580*da6c28aaSamw 			    (is_sysattr(tapath) ||
3581*da6c28aaSamw 			    (strpbrk(tapath, "/") != NULL)))) {
35827c478bd9Sstevel@tonic-gate 				data_in(P_SKIP);
35837c478bd9Sstevel@tonic-gate 				free(xattrhead);
35847c478bd9Sstevel@tonic-gate 				xattrhead = NULL;
35857c478bd9Sstevel@tonic-gate 				xattrp = NULL;
35867c478bd9Sstevel@tonic-gate 				Gen.g_attrfnam_p = (char *)NULL;
35877c478bd9Sstevel@tonic-gate 				Gen.g_attrnam_p = (char *)NULL;
35887c478bd9Sstevel@tonic-gate 				return (2);
35897c478bd9Sstevel@tonic-gate 			}
35907c478bd9Sstevel@tonic-gate 
35917c478bd9Sstevel@tonic-gate 			if (Gen.g_attrfnam_p != (char *)NULL) {
35927c478bd9Sstevel@tonic-gate 				free(Gen.g_attrfnam_p);
35937c478bd9Sstevel@tonic-gate 				Gen.g_attrfnam_p = (char *)NULL;
35947c478bd9Sstevel@tonic-gate 			}
35957c478bd9Sstevel@tonic-gate 			if (Gen.g_attrnam_p != (char *)NULL) {
35967c478bd9Sstevel@tonic-gate 				free(Gen.g_attrnam_p);
35977c478bd9Sstevel@tonic-gate 				Gen.g_attrnam_p = (char *)NULL;
35987c478bd9Sstevel@tonic-gate 			}
35997c478bd9Sstevel@tonic-gate 			if (Renam_p && Renam_p[0] != '\0') {
36007c478bd9Sstevel@tonic-gate 				Gen.g_attrfnam_p = e_strdup(E_EXIT, Renam_p);
36017c478bd9Sstevel@tonic-gate 			} else {
36027c478bd9Sstevel@tonic-gate 				Gen.g_attrfnam_p = e_strdup(E_EXIT,
36037c478bd9Sstevel@tonic-gate 				    xattrp->h_names);
36047c478bd9Sstevel@tonic-gate 			}
36057c478bd9Sstevel@tonic-gate 
3606*da6c28aaSamw 			Gen.g_attrnam_p = e_strdup(E_EXIT, tapath);
36077c478bd9Sstevel@tonic-gate 
36087c478bd9Sstevel@tonic-gate 			if (Hdr_type != USTAR && Hdr_type != TAR) {
36097c478bd9Sstevel@tonic-gate 				Gen.g_mode = Gen.g_mode & (~_XATTR_CPIO_MODE);
36107c478bd9Sstevel@tonic-gate 				Gen.g_mode |= attrmode(xattrp->h_typeflag);
36117c478bd9Sstevel@tonic-gate 			} else if (Hdr_type == USTAR || Hdr_type == TAR) {
36127c478bd9Sstevel@tonic-gate 				Thdr_p->tbuf.t_typeflag = xattrp->h_typeflag;
36137c478bd9Sstevel@tonic-gate 			}
36147c478bd9Sstevel@tonic-gate 			if (xattr_linkp != (struct xattr_buf *)NULL) {
36157c478bd9Sstevel@tonic-gate 				if (Gen.g_linktoattrfnam_p != (char *)NULL) {
36167c478bd9Sstevel@tonic-gate 					free(Gen.g_linktoattrfnam_p);
36177c478bd9Sstevel@tonic-gate 					Gen.g_linktoattrfnam_p = NULL;
36187c478bd9Sstevel@tonic-gate 				}
36197c478bd9Sstevel@tonic-gate 				if (Gen.g_linktoattrnam_p != (char *)NULL) {
36207c478bd9Sstevel@tonic-gate 					free(Gen.g_linktoattrnam_p);
36217c478bd9Sstevel@tonic-gate 					Gen.g_linktoattrnam_p = NULL;
36227c478bd9Sstevel@tonic-gate 				}
36237c478bd9Sstevel@tonic-gate 				Gen.g_linktoattrfnam_p = e_strdup(E_EXIT,
36247c478bd9Sstevel@tonic-gate 				    xattr_linkp->h_names);
36257c478bd9Sstevel@tonic-gate 				Gen.g_linktoattrnam_p = e_strdup(E_EXIT,
3626*da6c28aaSamw 				    tapath);
36277c478bd9Sstevel@tonic-gate 				xattr_linkp = NULL;
36287c478bd9Sstevel@tonic-gate 			}
36297c478bd9Sstevel@tonic-gate 			ftype = Gen.g_mode & Ftype;
36307c478bd9Sstevel@tonic-gate 			Adir = ftype == S_IFDIR;
3631cdd68f5aSceastha 			Aspec = (ftype == S_IFBLK || ftype == S_IFCHR ||
3632cdd68f5aSceastha 			    ftype == S_IFIFO || ftype == S_IFSOCK);
36337c478bd9Sstevel@tonic-gate 
36347c478bd9Sstevel@tonic-gate 			if (Gen.g_attrnam_p[0] == '.' &&
36357c478bd9Sstevel@tonic-gate 			    Gen.g_attrnam_p[1] == '\0' &&
36367c478bd9Sstevel@tonic-gate 			    xattrp->h_typeflag == DIRTYPE) {
36377c478bd9Sstevel@tonic-gate 				Hiddendir = 1;
36387c478bd9Sstevel@tonic-gate 			} else {
36397c478bd9Sstevel@tonic-gate 				Hiddendir = 0;
36407c478bd9Sstevel@tonic-gate 			}
36417c478bd9Sstevel@tonic-gate 
36427c478bd9Sstevel@tonic-gate 			free(xattrhead);
36437c478bd9Sstevel@tonic-gate 			xattrhead = NULL;
36447c478bd9Sstevel@tonic-gate 			xattrp = NULL;
36457c478bd9Sstevel@tonic-gate 		} else {
36467c478bd9Sstevel@tonic-gate 			if (xattrbadhead == 0) {
36477c478bd9Sstevel@tonic-gate 				(void) read_xattr_hdr();
36487c478bd9Sstevel@tonic-gate 				return (2);
36497c478bd9Sstevel@tonic-gate 			}
36507c478bd9Sstevel@tonic-gate 		}
36517c478bd9Sstevel@tonic-gate 	}
36527c478bd9Sstevel@tonic-gate #endif /* O_XATTR */
36537c478bd9Sstevel@tonic-gate 
36547c478bd9Sstevel@tonic-gate 	/* acl support: grab acl info */
36557c478bd9Sstevel@tonic-gate 	if ((Gen.g_mode == SECMODE) || ((Hdr_type == USTAR ||
36567c478bd9Sstevel@tonic-gate 	    Hdr_type == TAR) && Thdr_p->tbuf.t_typeflag == 'A')) {
36577c478bd9Sstevel@tonic-gate 		/* this is an ancillary file */
36587c478bd9Sstevel@tonic-gate 		off_t	bytes;
36597c478bd9Sstevel@tonic-gate 		char	*secp;
36607c478bd9Sstevel@tonic-gate 		int	pad;
36617c478bd9Sstevel@tonic-gate 		int	cnt;
36627c478bd9Sstevel@tonic-gate 		char	*tp;
36637c478bd9Sstevel@tonic-gate 		int	attrsize;
36647c478bd9Sstevel@tonic-gate 
36657c478bd9Sstevel@tonic-gate 		if (Pflag) {
36667c478bd9Sstevel@tonic-gate 			bytes = Gen.g_filesz;
36677c478bd9Sstevel@tonic-gate 			secp = e_zalloc(E_EXIT, (uint_t)bytes);
36687c478bd9Sstevel@tonic-gate 			tp = secp;
36697c478bd9Sstevel@tonic-gate 
36707c478bd9Sstevel@tonic-gate 			while (bytes > 0) {
36717c478bd9Sstevel@tonic-gate 				cnt = (int)(bytes > CPIOBSZ) ? CPIOBSZ : bytes;
36727c478bd9Sstevel@tonic-gate 				FILL(cnt);
36737c478bd9Sstevel@tonic-gate 				(void) memcpy(tp, Buffr.b_out_p, cnt);
36747c478bd9Sstevel@tonic-gate 				tp += cnt;
36757c478bd9Sstevel@tonic-gate 				Buffr.b_out_p += cnt;
36767c478bd9Sstevel@tonic-gate 				Buffr.b_cnt -= (off_t)cnt;
36777c478bd9Sstevel@tonic-gate 				bytes -= (off_t)cnt;
36787c478bd9Sstevel@tonic-gate 			}
36797c478bd9Sstevel@tonic-gate 
36807c478bd9Sstevel@tonic-gate 			pad = (Pad_val + 1 - (Gen.g_filesz & Pad_val)) &
36817c478bd9Sstevel@tonic-gate 			    Pad_val;
36827c478bd9Sstevel@tonic-gate 			if (pad != 0) {
36837c478bd9Sstevel@tonic-gate 				FILL(pad);
36847c478bd9Sstevel@tonic-gate 				Buffr.b_out_p += pad;
36857c478bd9Sstevel@tonic-gate 				Buffr.b_cnt -= (off_t)pad;
36867c478bd9Sstevel@tonic-gate 			}
36877c478bd9Sstevel@tonic-gate 
36887c478bd9Sstevel@tonic-gate 			/* got all attributes in secp */
36897c478bd9Sstevel@tonic-gate 			tp = secp;
36907c478bd9Sstevel@tonic-gate 			do {
36917c478bd9Sstevel@tonic-gate 				attr = (struct sec_attr *)tp;
36927c478bd9Sstevel@tonic-gate 				switch (attr->attr_type) {
36937c478bd9Sstevel@tonic-gate 				case UFSD_ACL:
3694fa9e4066Sahrens 				case ACE_ACL:
36957c478bd9Sstevel@tonic-gate 					(void) sscanf(attr->attr_len, "%7lo",
36967c478bd9Sstevel@tonic-gate 					    (ulong_t *)&aclcnt);
36977c478bd9Sstevel@tonic-gate 					/* header is 8 */
36987c478bd9Sstevel@tonic-gate 					attrsize = 8 +
36997c478bd9Sstevel@tonic-gate 					    strlen(&attr->attr_info[0])
37007c478bd9Sstevel@tonic-gate 					    + 1;
3701fa9e4066Sahrens 
3702fa9e4066Sahrens 					error =
3703fa9e4066Sahrens 					    acl_fromtext(&attr->attr_info[0],
3704fa9e4066Sahrens 					    &aclp);
3705fa9e4066Sahrens 
3706fa9e4066Sahrens 					if (error != 0) {
3707fa9e4066Sahrens 						msg(ERR,
3708fa9e4066Sahrens 						    "aclfromtext failed: %s",
3709fa9e4066Sahrens 						    acl_strerror(error));
3710fa9e4066Sahrens 						bytes -= attrsize;
37117c478bd9Sstevel@tonic-gate 						break;
37127c478bd9Sstevel@tonic-gate 					}
3713fa9e4066Sahrens 
3714fa9e4066Sahrens 					if (aclcnt != acl_cnt(aclp)) {
37157c478bd9Sstevel@tonic-gate 						msg(ERR, "acl count error");
3716fa9e4066Sahrens 						bytes -= attrsize;
37177c478bd9Sstevel@tonic-gate 						break;
37187c478bd9Sstevel@tonic-gate 					}
37197c478bd9Sstevel@tonic-gate 					bytes -= attrsize;
37207c478bd9Sstevel@tonic-gate 					break;
37217c478bd9Sstevel@tonic-gate 
37227c478bd9Sstevel@tonic-gate 				/* SunFed case goes here */
37237c478bd9Sstevel@tonic-gate 
37247c478bd9Sstevel@tonic-gate 				default:
37257c478bd9Sstevel@tonic-gate 					msg(EXT, "unrecognized attr type");
37267c478bd9Sstevel@tonic-gate 					break;
37277c478bd9Sstevel@tonic-gate 			}
37287c478bd9Sstevel@tonic-gate 			/* next attributes */
37297c478bd9Sstevel@tonic-gate 			tp += attrsize;
37307c478bd9Sstevel@tonic-gate 			} while (bytes > 0);
37317c478bd9Sstevel@tonic-gate 			free(secp);
37327c478bd9Sstevel@tonic-gate 		} else {
37337c478bd9Sstevel@tonic-gate 			/* skip security info */
37347c478bd9Sstevel@tonic-gate 			G_p = &Gen;
37357c478bd9Sstevel@tonic-gate 			data_in(P_SKIP);
37367c478bd9Sstevel@tonic-gate 		}
37377c478bd9Sstevel@tonic-gate 		/*
37387c478bd9Sstevel@tonic-gate 		 * We already got the file content, dont call file_in()
37397c478bd9Sstevel@tonic-gate 		 * when return. The new return code(2) is used to
37407c478bd9Sstevel@tonic-gate 		 *  indicate that.
37417c478bd9Sstevel@tonic-gate 		 */
37427c478bd9Sstevel@tonic-gate 		VERBOSE((Args & OCt), Gen.g_nam_p);
37437c478bd9Sstevel@tonic-gate 		return (2);
37447c478bd9Sstevel@tonic-gate 	} /* acl */
37457c478bd9Sstevel@tonic-gate 
37467c478bd9Sstevel@tonic-gate 	Adir = (ftype == S_IFDIR);
3747cdd68f5aSceastha 	Aspec = (ftype == S_IFBLK || ftype == S_IFCHR || ftype == S_IFIFO ||
3748cdd68f5aSceastha 	    ftype == S_IFSOCK);
37497c478bd9Sstevel@tonic-gate 
37507c478bd9Sstevel@tonic-gate 	/*
37517c478bd9Sstevel@tonic-gate 	 * Skip any trailing slashes
37527c478bd9Sstevel@tonic-gate 	 */
37537c478bd9Sstevel@tonic-gate 	chop_endslashes(Gen.g_nam_p);
37547c478bd9Sstevel@tonic-gate 	return (1);
37557c478bd9Sstevel@tonic-gate }
37567c478bd9Sstevel@tonic-gate 
37577c478bd9Sstevel@tonic-gate /*
37587c478bd9Sstevel@tonic-gate  * getname: Get file names for inclusion in the archive.  When end of file
37597c478bd9Sstevel@tonic-gate  * on the input stream of file names is reached, flush the link buffer out.
37607c478bd9Sstevel@tonic-gate  * For each filename, remove leading "./"s and multiple "/"s, and remove
3761*da6c28aaSamw  * any trailing newline "\n".  Finally, verify the existence of the file,
37627c478bd9Sstevel@tonic-gate  * and call creat_hdr() to fill in the gen_hdr structure.
37637c478bd9Sstevel@tonic-gate  */
37647c478bd9Sstevel@tonic-gate 
37657c478bd9Sstevel@tonic-gate static int
37667c478bd9Sstevel@tonic-gate getname(void)
37677c478bd9Sstevel@tonic-gate {
37687c478bd9Sstevel@tonic-gate 	int goodfile = 0, lastchar, err;
37697c478bd9Sstevel@tonic-gate 	char *s;
37707c478bd9Sstevel@tonic-gate 	char *dir;
37717c478bd9Sstevel@tonic-gate 
37727c478bd9Sstevel@tonic-gate 	Gen.g_nam_p = Nam_p;
37737c478bd9Sstevel@tonic-gate 
37747c478bd9Sstevel@tonic-gate 	while (!goodfile) {
37757c478bd9Sstevel@tonic-gate 		err = 0;
37767c478bd9Sstevel@tonic-gate 
37777c478bd9Sstevel@tonic-gate 		while ((s = fgets(Gen.g_nam_p, APATH+1, In_p))
37787c478bd9Sstevel@tonic-gate 		    != NULL) {
37797c478bd9Sstevel@tonic-gate 			lastchar = strlen(s) - 1;
37807c478bd9Sstevel@tonic-gate 			issymlink = 0;
37817c478bd9Sstevel@tonic-gate 
37827c478bd9Sstevel@tonic-gate 			if (s[lastchar] != '\n') {
37837c478bd9Sstevel@tonic-gate 				if (lastchar == APATH - 1) {
37847c478bd9Sstevel@tonic-gate 					if (!err) {
37857c478bd9Sstevel@tonic-gate 						msg(ERR,
37867c478bd9Sstevel@tonic-gate 						    "%s name too long.",
37877c478bd9Sstevel@tonic-gate 						    Nam_p);
37887c478bd9Sstevel@tonic-gate 					}
37897c478bd9Sstevel@tonic-gate 					goodfile = 0;
37907c478bd9Sstevel@tonic-gate 					err = 1;
37917c478bd9Sstevel@tonic-gate 				} else {
37927c478bd9Sstevel@tonic-gate 					break;
37937c478bd9Sstevel@tonic-gate 				}
37947c478bd9Sstevel@tonic-gate 			} else {
37957c478bd9Sstevel@tonic-gate 				s[lastchar] = '\0';
37967c478bd9Sstevel@tonic-gate 				break;
37977c478bd9Sstevel@tonic-gate 			}
37987c478bd9Sstevel@tonic-gate 		}
37997c478bd9Sstevel@tonic-gate 
38007c478bd9Sstevel@tonic-gate 		if (s == (char *)NULL) {
38017c478bd9Sstevel@tonic-gate 			if (Gen.g_dirfd != -1) {
38027c478bd9Sstevel@tonic-gate 				(void) close(Gen.g_dirfd);
38037c478bd9Sstevel@tonic-gate 				Gen.g_dirfd = -1;
38047c478bd9Sstevel@tonic-gate 			}
38057c478bd9Sstevel@tonic-gate 			if (Onecopy && (Args & OCo)) {
38067c478bd9Sstevel@tonic-gate 				flush_lnks();
38077c478bd9Sstevel@tonic-gate 			}
38087c478bd9Sstevel@tonic-gate 			return (0);
38097c478bd9Sstevel@tonic-gate 		}
38107c478bd9Sstevel@tonic-gate 
38117c478bd9Sstevel@tonic-gate 		while (*Gen.g_nam_p == '.' && Gen.g_nam_p[1] == '/') {
38127c478bd9Sstevel@tonic-gate 			Gen.g_nam_p += 2;
38137c478bd9Sstevel@tonic-gate 			while (*Gen.g_nam_p == '/')
38147c478bd9Sstevel@tonic-gate 				Gen.g_nam_p++;
38157c478bd9Sstevel@tonic-gate 		}
38167c478bd9Sstevel@tonic-gate 
38177c478bd9Sstevel@tonic-gate 		/*
38187c478bd9Sstevel@tonic-gate 		 * Skip any trailing slashes
38197c478bd9Sstevel@tonic-gate 		 */
38207c478bd9Sstevel@tonic-gate 		chop_endslashes(Gen.g_nam_p);
38217c478bd9Sstevel@tonic-gate 
38227c478bd9Sstevel@tonic-gate 		/*
38237c478bd9Sstevel@tonic-gate 		 * Figure out parent directory
38247c478bd9Sstevel@tonic-gate 		 */
38257c478bd9Sstevel@tonic-gate 
38267c478bd9Sstevel@tonic-gate 		if (Gen.g_attrnam_p != (char *)NULL) {
38277c478bd9Sstevel@tonic-gate 			if (Gen.g_dirfd != -1) {
38287c478bd9Sstevel@tonic-gate 				(void) close(Gen.g_dirfd);
38297c478bd9Sstevel@tonic-gate 			}
38307c478bd9Sstevel@tonic-gate 			Gen.g_dirfd = attropen(Gen.g_attrfnam_p, ".", O_RDONLY);
38317c478bd9Sstevel@tonic-gate 			if (Gen.g_dirfd == -1) {
38327c478bd9Sstevel@tonic-gate 				msg(ERRN,
38337c478bd9Sstevel@tonic-gate 				    "Cannot open attribute directory"
38347c478bd9Sstevel@tonic-gate 				    " of file %s", Gen.g_attrfnam_p);
38357c478bd9Sstevel@tonic-gate 				continue;
38367c478bd9Sstevel@tonic-gate 			}
38377c478bd9Sstevel@tonic-gate 		} else {
38387c478bd9Sstevel@tonic-gate #ifdef O_XATTR
38397c478bd9Sstevel@tonic-gate 			char dirpath[PATH_MAX];
38407c478bd9Sstevel@tonic-gate 
38417c478bd9Sstevel@tonic-gate 			get_parent(Gen.g_nam_p, dirpath);
38427c478bd9Sstevel@tonic-gate 			if (Atflag) {
38437c478bd9Sstevel@tonic-gate 				dir = dirpath;
38447c478bd9Sstevel@tonic-gate 				if (Gen.g_dirfd != -1) {
38457c478bd9Sstevel@tonic-gate 					(void) close(Gen.g_dirfd);
38467c478bd9Sstevel@tonic-gate 				}
38477c478bd9Sstevel@tonic-gate 				Gen.g_dirfd = open(dir, O_RDONLY);
38487c478bd9Sstevel@tonic-gate 				if (Gen.g_dirfd == -1) {
38497c478bd9Sstevel@tonic-gate 					msg(ERRN,
38507c478bd9Sstevel@tonic-gate 					    "Cannot open directory %s", dir);
38517c478bd9Sstevel@tonic-gate 					continue;
38527c478bd9Sstevel@tonic-gate 				}
38537c478bd9Sstevel@tonic-gate 			} else {
38547c478bd9Sstevel@tonic-gate 				/*
38557c478bd9Sstevel@tonic-gate 				 * g_dirpath is the pathname cache maintaining
38567c478bd9Sstevel@tonic-gate 				 * the dirname which is currently opened.
38577c478bd9Sstevel@tonic-gate 				 * We first check the g_dirpath to see if the
38587c478bd9Sstevel@tonic-gate 				 * given dirname matches. If so, we don't need
38597c478bd9Sstevel@tonic-gate 				 * to open the dir, but we can use the g_dirfd
38607c478bd9Sstevel@tonic-gate 				 * as is if it is still available.
38617c478bd9Sstevel@tonic-gate 				 */
38627c478bd9Sstevel@tonic-gate 				dir = NULL;
38637c478bd9Sstevel@tonic-gate 				if (Gen.g_dirpath == NULL ||
38647c478bd9Sstevel@tonic-gate 				    Gen.g_dirfd == -1) {
38657c478bd9Sstevel@tonic-gate 					/*
38667c478bd9Sstevel@tonic-gate 					 * It's the first time or it has
38677c478bd9Sstevel@tonic-gate 					 * all gone.
38687c478bd9Sstevel@tonic-gate 					 */
38697c478bd9Sstevel@tonic-gate 					dir = e_strdup(E_EXIT, dirpath);
38707c478bd9Sstevel@tonic-gate 				} else {
38717c478bd9Sstevel@tonic-gate 					if (strcmp(Gen.g_dirpath,
38727c478bd9Sstevel@tonic-gate 					    dirpath) != 0) {
38737c478bd9Sstevel@tonic-gate 						/* different directory */
38747c478bd9Sstevel@tonic-gate 						dir = e_strdup(E_EXIT, dirpath);
38757c478bd9Sstevel@tonic-gate 					}
38767c478bd9Sstevel@tonic-gate 				}
38777c478bd9Sstevel@tonic-gate 				if (dir != NULL) {
38787c478bd9Sstevel@tonic-gate 					/*
38797c478bd9Sstevel@tonic-gate 					 * We need to open the new directory.
38807c478bd9Sstevel@tonic-gate 					 * discard the pathname and dirfd
38817c478bd9Sstevel@tonic-gate 					 * for the previous directory.
38827c478bd9Sstevel@tonic-gate 					 */
38837c478bd9Sstevel@tonic-gate 					if (Gen.g_dirpath != NULL) {
38847c478bd9Sstevel@tonic-gate 						free(Gen.g_dirpath);
38857c478bd9Sstevel@tonic-gate 						Gen.g_dirpath = NULL;
38867c478bd9Sstevel@tonic-gate 					}
38877c478bd9Sstevel@tonic-gate 					if (Gen.g_dirfd != -1) {
38887c478bd9Sstevel@tonic-gate 						(void) close(Gen.g_dirfd);
38897c478bd9Sstevel@tonic-gate 					}
38907c478bd9Sstevel@tonic-gate 					/* open the new dir */
38917c478bd9Sstevel@tonic-gate 					Gen.g_dirfd = open(dir, O_RDONLY);
38927c478bd9Sstevel@tonic-gate 					if (Gen.g_dirfd == -1) {
38937c478bd9Sstevel@tonic-gate 						msg(ERRN, "Cannot open "
38947c478bd9Sstevel@tonic-gate 						    "directory %s", dir);
38957c478bd9Sstevel@tonic-gate 						continue;
38967c478bd9Sstevel@tonic-gate 					}
38977c478bd9Sstevel@tonic-gate 					Gen.g_dirpath = dir;
38987c478bd9Sstevel@tonic-gate 				}
38997c478bd9Sstevel@tonic-gate 			}
39007c478bd9Sstevel@tonic-gate #else
39017c478bd9Sstevel@tonic-gate 			Gen.g_dirfd = -1;
39027c478bd9Sstevel@tonic-gate #endif
39037c478bd9Sstevel@tonic-gate 		}
39047c478bd9Sstevel@tonic-gate 
39057c478bd9Sstevel@tonic-gate 		/* creat_hdr checks for USTAR filename length */
39067c478bd9Sstevel@tonic-gate 
39077c478bd9Sstevel@tonic-gate 		if (Hdr_type != USTAR && strlen(Gen.g_nam_p) >
39087c478bd9Sstevel@tonic-gate 		    Max_namesz) {
39097c478bd9Sstevel@tonic-gate 			if (!err) {
39107c478bd9Sstevel@tonic-gate 				msg(ERR, "%s%s%s name too long.",
39117c478bd9Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
39127c478bd9Sstevel@tonic-gate 				    Nam_p : Gen.g_attrfnam_p,
39137c478bd9Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
39147c478bd9Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
39157c478bd9Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
39167c478bd9Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
39177c478bd9Sstevel@tonic-gate 			}
39187c478bd9Sstevel@tonic-gate 			goodfile = 0;
39197c478bd9Sstevel@tonic-gate 			err = 1;
39207c478bd9Sstevel@tonic-gate 		}
39217c478bd9Sstevel@tonic-gate 
39227c478bd9Sstevel@tonic-gate 		if (err) {
39237c478bd9Sstevel@tonic-gate 			continue;
39247c478bd9Sstevel@tonic-gate 		} else {
39257c478bd9Sstevel@tonic-gate 			G_p = &Gen;
39267c478bd9Sstevel@tonic-gate 			if (!LSTAT(Gen.g_dirfd, Gen.g_nam_p, &SrcSt)) {
39277c478bd9Sstevel@tonic-gate 				goodfile = 1;
39287c478bd9Sstevel@tonic-gate 
39297c478bd9Sstevel@tonic-gate 				if ((SrcSt.st_mode & Ftype) == S_IFLNK) {
39307c478bd9Sstevel@tonic-gate 					issymlink = 1;
39317c478bd9Sstevel@tonic-gate 
39327c478bd9Sstevel@tonic-gate 					if ((Args & OCL)) {
39337c478bd9Sstevel@tonic-gate 						errno = 0;
39347c478bd9Sstevel@tonic-gate 						if (STAT(Gen.g_dirfd,
39357c478bd9Sstevel@tonic-gate 						    G_p->g_nam_p,
39367c478bd9Sstevel@tonic-gate 						    &SrcSt) < 0) {
39377c478bd9Sstevel@tonic-gate 							msg(ERRN,
39387c478bd9Sstevel@tonic-gate 							    "Cannot follow"
39397c478bd9Sstevel@tonic-gate 							    " \"%s%s%s\"",
39407c478bd9Sstevel@tonic-gate 							    (Gen.g_attrnam_p ==
39417c478bd9Sstevel@tonic-gate 							    (char *)NULL) ?
39427c478bd9Sstevel@tonic-gate 							    Gen.g_nam_p :
39437c478bd9Sstevel@tonic-gate 							    Gen.g_attrfnam_p,
39447c478bd9Sstevel@tonic-gate 							    (Gen.g_attrnam_p ==
39457c478bd9Sstevel@tonic-gate 							    (char *)NULL) ?
39467c478bd9Sstevel@tonic-gate 							    "" :
39477c478bd9Sstevel@tonic-gate 							    gettext(
39487c478bd9Sstevel@tonic-gate 							    " Attribute "),
39497c478bd9Sstevel@tonic-gate 							    (Gen.g_attrnam_p ==
39507c478bd9Sstevel@tonic-gate 							    (char *)NULL) ?
39517c478bd9Sstevel@tonic-gate 							    "" :
39527c478bd9Sstevel@tonic-gate 							    Gen.g_attrnam_p);
39537c478bd9Sstevel@tonic-gate 							goodfile = 0;
39547c478bd9Sstevel@tonic-gate 						}
39557c478bd9Sstevel@tonic-gate 					}
39567c478bd9Sstevel@tonic-gate 				}
39577c478bd9Sstevel@tonic-gate 
39587c478bd9Sstevel@tonic-gate 				if (Use_old_stat) {
39597c478bd9Sstevel@tonic-gate 					OldSt = convert_to_old_stat(&SrcSt,
39607c478bd9Sstevel@tonic-gate 					    Gen.g_nam_p, Gen.g_attrnam_p);
39617c478bd9Sstevel@tonic-gate 
39627c478bd9Sstevel@tonic-gate 					if (OldSt == NULL) {
39637c478bd9Sstevel@tonic-gate 						goodfile = 0;
39647c478bd9Sstevel@tonic-gate 					}
39657c478bd9Sstevel@tonic-gate 				}
39667c478bd9Sstevel@tonic-gate 			} else {
39677c478bd9Sstevel@tonic-gate 				msg(ERRN,
39687c478bd9Sstevel@tonic-gate 				    "Error with fstatat() of \"%s%s%s\"",
39697c478bd9Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
39707c478bd9Sstevel@tonic-gate 				    Gen.g_nam_p : Gen.g_attrfnam_p,
39717c478bd9Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
39727c478bd9Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
39737c478bd9Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
39747c478bd9Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
39757c478bd9Sstevel@tonic-gate 			}
39767c478bd9Sstevel@tonic-gate 		}
39777c478bd9Sstevel@tonic-gate 	}
39787c478bd9Sstevel@tonic-gate 
39797c478bd9Sstevel@tonic-gate 	/*
39807c478bd9Sstevel@tonic-gate 	 * Get ACL info: dont bother allocating space if there are only
39817c478bd9Sstevel@tonic-gate 	 * standard permissions, i.e. ACL count < 4
39827c478bd9Sstevel@tonic-gate 	 */
39837c478bd9Sstevel@tonic-gate 	if ((SrcSt.st_mode & Ftype) != S_IFLNK && Pflag) {
3984fa9e4066Sahrens 		if (acl_get(Gen.g_nam_p, ACL_NO_TRIVIAL, &aclp) != 0)
39857c478bd9Sstevel@tonic-gate 			msg(ERRN, "Error with acl() of \"%s\"", Gen.g_nam_p);
39867c478bd9Sstevel@tonic-gate 	}
39877c478bd9Sstevel@tonic-gate 	/* else: only traditional permissions, so proceed as usual */
39887c478bd9Sstevel@tonic-gate 	if (creat_hdr())
39897c478bd9Sstevel@tonic-gate 		return (1);
39907c478bd9Sstevel@tonic-gate 	else return (2);
39917c478bd9Sstevel@tonic-gate }
39927c478bd9Sstevel@tonic-gate 
39937c478bd9Sstevel@tonic-gate /*
39947c478bd9Sstevel@tonic-gate  * getpats: Save any filenames/patterns specified as arguments.
39957c478bd9Sstevel@tonic-gate  * Read additional filenames/patterns from the file specified by the
39967c478bd9Sstevel@tonic-gate  * user.  The filenames/patterns must occur one per line.
39977c478bd9Sstevel@tonic-gate  */
39987c478bd9Sstevel@tonic-gate 
39997c478bd9Sstevel@tonic-gate static void
40007c478bd9Sstevel@tonic-gate getpats(int largc, char **largv)
40017c478bd9Sstevel@tonic-gate {
40027c478bd9Sstevel@tonic-gate 	char **t_pp;
40037c478bd9Sstevel@tonic-gate 	size_t len;
40047c478bd9Sstevel@tonic-gate 	unsigned numpat = largc, maxpat = largc + 2;
40057c478bd9Sstevel@tonic-gate 
40067c478bd9Sstevel@tonic-gate 	Pat_pp = e_zalloc(E_EXIT, maxpat * sizeof (char *));
40077c478bd9Sstevel@tonic-gate 	t_pp = Pat_pp;
40087c478bd9Sstevel@tonic-gate 	while (*largv) {
40097c478bd9Sstevel@tonic-gate 		*t_pp = e_zalloc(E_EXIT, strlen(*largv) + 1);
40107c478bd9Sstevel@tonic-gate 		(void) strcpy(*t_pp, *largv);
40117c478bd9Sstevel@tonic-gate 		t_pp++;
40127c478bd9Sstevel@tonic-gate 		largv++;
40137c478bd9Sstevel@tonic-gate 	}
40147c478bd9Sstevel@tonic-gate 	while (fgets(Nam_p, Max_namesz + 1, Ef_p) != (char *)NULL) {
40157c478bd9Sstevel@tonic-gate 		if (numpat == maxpat - 1) {
40167c478bd9Sstevel@tonic-gate 			maxpat += 10;
40177c478bd9Sstevel@tonic-gate 			Pat_pp = e_realloc(E_EXIT, Pat_pp,
40187c478bd9Sstevel@tonic-gate 			    maxpat * sizeof (char *));
40197c478bd9Sstevel@tonic-gate 			t_pp = Pat_pp + numpat;
40207c478bd9Sstevel@tonic-gate 		}
40217c478bd9Sstevel@tonic-gate 		len = strlen(Nam_p); /* includes the \n */
40227c478bd9Sstevel@tonic-gate 		*(Nam_p + len - 1) = '\0'; /* remove the \n */
40237c478bd9Sstevel@tonic-gate 		*t_pp = e_zalloc(E_EXIT, len);
40247c478bd9Sstevel@tonic-gate 		(void) strcpy(*t_pp, Nam_p);
40257c478bd9Sstevel@tonic-gate 		t_pp++;
40267c478bd9Sstevel@tonic-gate 		numpat++;
40277c478bd9Sstevel@tonic-gate 	}
40287c478bd9Sstevel@tonic-gate 	*t_pp = (char *)NULL;
40297c478bd9Sstevel@tonic-gate }
40307c478bd9Sstevel@tonic-gate 
40317c478bd9Sstevel@tonic-gate static void
40327c478bd9Sstevel@tonic-gate ioerror(int dir)
40337c478bd9Sstevel@tonic-gate {
40347c478bd9Sstevel@tonic-gate 	int t_errno;
40357c478bd9Sstevel@tonic-gate 
40367c478bd9Sstevel@tonic-gate 	t_errno = errno;
40377c478bd9Sstevel@tonic-gate 	errno = 0;
40387c478bd9Sstevel@tonic-gate 	if (fstat(Archive, &ArchSt) < 0)
40397c478bd9Sstevel@tonic-gate 		msg(EXTN, "Error during stat() of archive");
40407c478bd9Sstevel@tonic-gate 	errno = t_errno;
40417c478bd9Sstevel@tonic-gate 	if ((ArchSt.st_mode & Ftype) != S_IFCHR) {
40427c478bd9Sstevel@tonic-gate 		if (dir) {
40437c478bd9Sstevel@tonic-gate 			if (errno == EFBIG)
40447c478bd9Sstevel@tonic-gate 				msg(EXT, "ulimit reached for output file.");
40457c478bd9Sstevel@tonic-gate 			else if (errno == ENOSPC)
40467c478bd9Sstevel@tonic-gate 				msg(EXT, "No space left for output file.");
40477c478bd9Sstevel@tonic-gate 			else
40487c478bd9Sstevel@tonic-gate 				msg(EXTN, "I/O error - cannot continue");
40497c478bd9Sstevel@tonic-gate 		} else
40507c478bd9Sstevel@tonic-gate 			msg(EXT, "Unexpected end-of-file encountered.");
40517c478bd9Sstevel@tonic-gate 	} else
40527c478bd9Sstevel@tonic-gate 		msg(EXTN, "\007I/O error on \"%s\"", dir ? "output" : "input");
40537c478bd9Sstevel@tonic-gate }
40547c478bd9Sstevel@tonic-gate 
40557c478bd9Sstevel@tonic-gate /*
40567c478bd9Sstevel@tonic-gate  * matched: Determine if a filename matches the specified pattern(s).  If the
40577c478bd9Sstevel@tonic-gate  * pattern is matched (the second return), return 0 if -f was specified, else
40587c478bd9Sstevel@tonic-gate  * return != 0.  If the pattern is not matched (the first and third
40597c478bd9Sstevel@tonic-gate  * returns), return 0 if -f was not specified, else return != 0.
40607c478bd9Sstevel@tonic-gate  */
40617c478bd9Sstevel@tonic-gate 
40627c478bd9Sstevel@tonic-gate static int
40637c478bd9Sstevel@tonic-gate matched(void)
40647c478bd9Sstevel@tonic-gate {
40657c478bd9Sstevel@tonic-gate 	char *str_p = G_p->g_nam_p;
40667c478bd9Sstevel@tonic-gate 	char **pat_pp = Pat_pp;
40677c478bd9Sstevel@tonic-gate 	int negatep, result;
40687c478bd9Sstevel@tonic-gate 
40697c478bd9Sstevel@tonic-gate 	/*
40707c478bd9Sstevel@tonic-gate 	 * Check for attribute
40717c478bd9Sstevel@tonic-gate 	 */
40727c478bd9Sstevel@tonic-gate 	if (G_p->g_attrfnam_p != (char *)NULL)
40737c478bd9Sstevel@tonic-gate 		str_p = G_p->g_attrfnam_p;
40747c478bd9Sstevel@tonic-gate 
40757c478bd9Sstevel@tonic-gate 	for (pat_pp = Pat_pp; *pat_pp; pat_pp++) {
40767c478bd9Sstevel@tonic-gate 		negatep = (**pat_pp == '!');
40777c478bd9Sstevel@tonic-gate 
40787c478bd9Sstevel@tonic-gate 		result = fnmatch(negatep ? (*pat_pp+1) : *pat_pp, str_p, 0);
40797c478bd9Sstevel@tonic-gate 
40807c478bd9Sstevel@tonic-gate 		if (result != 0 && result != FNM_NOMATCH) {
40817c478bd9Sstevel@tonic-gate 			msg(POST, "error matching file %s with pattern"
40827c478bd9Sstevel@tonic-gate 			    " %s\n", str_p, *pat_pp);
40837c478bd9Sstevel@tonic-gate 			return (Args & OCf);
40847c478bd9Sstevel@tonic-gate 		}
40857c478bd9Sstevel@tonic-gate 
40867c478bd9Sstevel@tonic-gate 		if ((result == 0 && ! negatep) ||
40877c478bd9Sstevel@tonic-gate 		    (result == FNM_NOMATCH && negatep)) {
4088*da6c28aaSamw 			/* match occurred */
40897c478bd9Sstevel@tonic-gate 			return (!(Args & OCf));
40907c478bd9Sstevel@tonic-gate 		}
40917c478bd9Sstevel@tonic-gate 	}
40927c478bd9Sstevel@tonic-gate 	return (Args & OCf); /* not matched */
40937c478bd9Sstevel@tonic-gate }
40947c478bd9Sstevel@tonic-gate 
40957c478bd9Sstevel@tonic-gate /*
40967c478bd9Sstevel@tonic-gate  * missdir: Create missing directories for files.
40977c478bd9Sstevel@tonic-gate  * (Possible future performance enhancement, if missdir is called, we know
40987c478bd9Sstevel@tonic-gate  * that at least the very last directory of the path does not exist, therefore,
40997c478bd9Sstevel@tonic-gate  * scan the path from the end
41007c478bd9Sstevel@tonic-gate  */
41017c478bd9Sstevel@tonic-gate 
41027c478bd9Sstevel@tonic-gate static int
41037c478bd9Sstevel@tonic-gate missdir(char *nam_p)
41047c478bd9Sstevel@tonic-gate {
41057c478bd9Sstevel@tonic-gate 	char *c_p;
41067c478bd9Sstevel@tonic-gate 	int cnt = 2;
41077c478bd9Sstevel@tonic-gate 	char *lastp;
41087c478bd9Sstevel@tonic-gate 
41097c478bd9Sstevel@tonic-gate 	if (*(c_p = nam_p) == '/') /* skip over 'root slash' */
41107c478bd9Sstevel@tonic-gate 		c_p++;
41117c478bd9Sstevel@tonic-gate 
41127c478bd9Sstevel@tonic-gate 	lastp = c_p + strlen(nam_p) - 1;
41137c478bd9Sstevel@tonic-gate 	if (*lastp == '/')
41147c478bd9Sstevel@tonic-gate 		*lastp = '\0';
41157c478bd9Sstevel@tonic-gate 
41167c478bd9Sstevel@tonic-gate 	for (; *c_p; ++c_p) {
41177c478bd9Sstevel@tonic-gate 		if (*c_p == '/') {
41187c478bd9Sstevel@tonic-gate 			*c_p = '\0';
41197c478bd9Sstevel@tonic-gate 			if (stat(nam_p, &DesSt) < 0) {
41207c478bd9Sstevel@tonic-gate 				if (Args & OCd) {
41217c478bd9Sstevel@tonic-gate 					cnt = mkdir(nam_p, Def_mode);
41227c478bd9Sstevel@tonic-gate 					if (cnt != 0) {
41237c478bd9Sstevel@tonic-gate 						*c_p = '/';
41247c478bd9Sstevel@tonic-gate 						return (cnt);
41257c478bd9Sstevel@tonic-gate 					}
41267c478bd9Sstevel@tonic-gate 				} else {
41277c478bd9Sstevel@tonic-gate 					msg(ERR, "Missing -d option.");
41287c478bd9Sstevel@tonic-gate 					*c_p = '/';
41297c478bd9Sstevel@tonic-gate 					return (-1);
41307c478bd9Sstevel@tonic-gate 				}
41317c478bd9Sstevel@tonic-gate 			}
41327c478bd9Sstevel@tonic-gate 			*c_p = '/';
41337c478bd9Sstevel@tonic-gate 		}
41347c478bd9Sstevel@tonic-gate 	}
41357c478bd9Sstevel@tonic-gate 	if (cnt == 2) /* the file already exists */
41367c478bd9Sstevel@tonic-gate 		cnt = 0;
41377c478bd9Sstevel@tonic-gate 	return (cnt);
41387c478bd9Sstevel@tonic-gate }
41397c478bd9Sstevel@tonic-gate 
41407c478bd9Sstevel@tonic-gate /*
41417c478bd9Sstevel@tonic-gate  * mklong: Convert two shorts into one long.  For VAX, Interdata ...
41427c478bd9Sstevel@tonic-gate  */
41437c478bd9Sstevel@tonic-gate 
41447c478bd9Sstevel@tonic-gate static long
41457c478bd9Sstevel@tonic-gate mklong(short v[])
41467c478bd9Sstevel@tonic-gate {
41477c478bd9Sstevel@tonic-gate 
41487c478bd9Sstevel@tonic-gate 	union swpbuf swp_b;
41497c478bd9Sstevel@tonic-gate 
41507c478bd9Sstevel@tonic-gate 	swp_b.s_word = 1;
41517c478bd9Sstevel@tonic-gate 	if (swp_b.s_byte[0]) {
41527c478bd9Sstevel@tonic-gate 		swp_b.s_half[0] = v[1];
41537c478bd9Sstevel@tonic-gate 		swp_b.s_half[1] = v[0];
41547c478bd9Sstevel@tonic-gate 	} else {
41557c478bd9Sstevel@tonic-gate 		swp_b.s_half[0] = v[0];
41567c478bd9Sstevel@tonic-gate 		swp_b.s_half[1] = v[1];
41577c478bd9Sstevel@tonic-gate 	}
41587c478bd9Sstevel@tonic-gate 	return (swp_b.s_word);
41597c478bd9Sstevel@tonic-gate }
41607c478bd9Sstevel@tonic-gate 
41617c478bd9Sstevel@tonic-gate /*
41627c478bd9Sstevel@tonic-gate  * mkshort: Convert a long into 2 shorts, for VAX, Interdata ...
41637c478bd9Sstevel@tonic-gate  */
41647c478bd9Sstevel@tonic-gate 
41657c478bd9Sstevel@tonic-gate static void
41667c478bd9Sstevel@tonic-gate mkshort(short sval[], long v)
41677c478bd9Sstevel@tonic-gate {
41687c478bd9Sstevel@tonic-gate 	union swpbuf *swp_p, swp_b;
41697c478bd9Sstevel@tonic-gate 
41707c478bd9Sstevel@tonic-gate 	/* LINTED alignment */
41717c478bd9Sstevel@tonic-gate 	swp_p = (union swpbuf *)sval;
41727c478bd9Sstevel@tonic-gate 	swp_b.s_word = 1;
41737c478bd9Sstevel@tonic-gate 	if (swp_b.s_byte[0]) {
41747c478bd9Sstevel@tonic-gate 		swp_b.s_word = v;
41757c478bd9Sstevel@tonic-gate 		swp_p->s_half[0] = swp_b.s_half[1];
41767c478bd9Sstevel@tonic-gate 		swp_p->s_half[1] = swp_b.s_half[0];
41777c478bd9Sstevel@tonic-gate 	} else {
41787c478bd9Sstevel@tonic-gate 		swp_b.s_word = v;
41797c478bd9Sstevel@tonic-gate 		swp_p->s_half[0] = swp_b.s_half[0];
41807c478bd9Sstevel@tonic-gate 		swp_p->s_half[1] = swp_b.s_half[1];
41817c478bd9Sstevel@tonic-gate 	}
41827c478bd9Sstevel@tonic-gate }
41837c478bd9Sstevel@tonic-gate 
41847c478bd9Sstevel@tonic-gate /*
41857c478bd9Sstevel@tonic-gate  * msg: Print either a message (no error) (POST), an error message with or
41867c478bd9Sstevel@tonic-gate  * without the errno (ERRN or ERR), or print an error message with or without
41877c478bd9Sstevel@tonic-gate  * the errno and exit (EXTN or EXT).
41887c478bd9Sstevel@tonic-gate  */
41897c478bd9Sstevel@tonic-gate 
41907c478bd9Sstevel@tonic-gate static void
41917c478bd9Sstevel@tonic-gate msg(int severity, const char *fmt, ...)
41927c478bd9Sstevel@tonic-gate {
41937c478bd9Sstevel@tonic-gate 	FILE *file_p;
41947c478bd9Sstevel@tonic-gate 	va_list ap;
41957c478bd9Sstevel@tonic-gate 
41967c478bd9Sstevel@tonic-gate 	if ((Args & OCV) && Verbcnt) { /* clear current line of dots */
41977c478bd9Sstevel@tonic-gate 		(void) fputc('\n', Out_p);
41987c478bd9Sstevel@tonic-gate 		Verbcnt = 0;
41997c478bd9Sstevel@tonic-gate 	}
42007c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
42017c478bd9Sstevel@tonic-gate 	if (severity == POST)
42027c478bd9Sstevel@tonic-gate 		file_p = Out_p;
42037c478bd9Sstevel@tonic-gate 	else
42047c478bd9Sstevel@tonic-gate 		if (severity == EPOST)
42057c478bd9Sstevel@tonic-gate 			file_p = Err_p;
42067c478bd9Sstevel@tonic-gate 		else {
42077c478bd9Sstevel@tonic-gate 			file_p = Err_p;
42087c478bd9Sstevel@tonic-gate 			Error_cnt++;
42097c478bd9Sstevel@tonic-gate 		}
42107c478bd9Sstevel@tonic-gate 	(void) fflush(Out_p);
42117c478bd9Sstevel@tonic-gate 	(void) fflush(Err_p);
42127c478bd9Sstevel@tonic-gate 	if ((severity != POST) && (severity != EPOST))
42137c478bd9Sstevel@tonic-gate 		(void) fprintf(file_p, "cpio: ");
42147c478bd9Sstevel@tonic-gate 
42157c478bd9Sstevel@tonic-gate 	/* gettext replaces version of string */
42167c478bd9Sstevel@tonic-gate 
42177c478bd9Sstevel@tonic-gate 	(void) vfprintf(file_p, gettext(fmt), ap);
42187c478bd9Sstevel@tonic-gate 	if (severity == ERRN || severity == EXTN) {
42197c478bd9Sstevel@tonic-gate 		(void) fprintf(file_p, ", errno %d, ", errno);
42207c478bd9Sstevel@tonic-gate 		perror("");
42217c478bd9Sstevel@tonic-gate 	} else
42227c478bd9Sstevel@tonic-gate 		(void) fprintf(file_p, "\n");
42237c478bd9Sstevel@tonic-gate 	(void) fflush(file_p);
42247c478bd9Sstevel@tonic-gate 	va_end(ap);
42257c478bd9Sstevel@tonic-gate 	if (severity == EXT || severity == EXTN) {
42267c478bd9Sstevel@tonic-gate 		(void) fprintf(file_p, gettext("%d errors\n"), Error_cnt);
42277c478bd9Sstevel@tonic-gate 		exit(EXIT_CODE);
42287c478bd9Sstevel@tonic-gate 	}
42297c478bd9Sstevel@tonic-gate }
42307c478bd9Sstevel@tonic-gate 
42317c478bd9Sstevel@tonic-gate /*
42327c478bd9Sstevel@tonic-gate  * openout: Open files for output and set all necessary information.
42337c478bd9Sstevel@tonic-gate  * If the u option is set (unconditionally overwrite existing files),
42347c478bd9Sstevel@tonic-gate  * and the current file exists, get a temporary file name from mktemp(3C),
42357c478bd9Sstevel@tonic-gate  * link the temporary file to the existing file, and remove the existing file.
42367c478bd9Sstevel@tonic-gate  * Finally either creat(2), mkdir(2) or mknod(2) as appropriate.
42377c478bd9Sstevel@tonic-gate  *
42387c478bd9Sstevel@tonic-gate  */
42397c478bd9Sstevel@tonic-gate 
42407c478bd9Sstevel@tonic-gate static int
42417c478bd9Sstevel@tonic-gate openout(int dirfd)
42427c478bd9Sstevel@tonic-gate {
42437c478bd9Sstevel@tonic-gate 	char *nam_p;
42447c478bd9Sstevel@tonic-gate 	int cnt, result;
42457c478bd9Sstevel@tonic-gate 
42467c478bd9Sstevel@tonic-gate 	Do_rename = 0;	/* creat_tmp() may reset this */
42477c478bd9Sstevel@tonic-gate 
42487c478bd9Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
42497c478bd9Sstevel@tonic-gate 		nam_p = G_p->g_attrnam_p;
42507c478bd9Sstevel@tonic-gate 	} else {
42517c478bd9Sstevel@tonic-gate 		if (Args & OCp) {
42527c478bd9Sstevel@tonic-gate 			nam_p = Fullnam_p;
42537c478bd9Sstevel@tonic-gate 		} else {
42547c478bd9Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
42557c478bd9Sstevel@tonic-gate 		}
42567c478bd9Sstevel@tonic-gate 	}
42577c478bd9Sstevel@tonic-gate 
42587c478bd9Sstevel@tonic-gate 
42597c478bd9Sstevel@tonic-gate 	if ((Max_filesz != RLIM_INFINITY) &&
42607c478bd9Sstevel@tonic-gate 	    (Max_filesz < (G_p->g_filesz >> 9))) {
42617c478bd9Sstevel@tonic-gate 		/* ... divided by 512 ... */
42627c478bd9Sstevel@tonic-gate 		msg(ERR, "Skipping \"%s%s%s\": exceeds ulimit by %lld bytes",
42637c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
42647c478bd9Sstevel@tonic-gate 		    nam_p : G_p->g_attrfnam_p,
42657c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
42667c478bd9Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
42677c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
42687c478bd9Sstevel@tonic-gate 		    "" : nam_p,
42697c478bd9Sstevel@tonic-gate 		    (off_t)(G_p->g_filesz - (Max_filesz << 9)));
42707c478bd9Sstevel@tonic-gate 		return (-1);
42717c478bd9Sstevel@tonic-gate 	}
42727c478bd9Sstevel@tonic-gate 
42737c478bd9Sstevel@tonic-gate 	if (LSTAT(dirfd, nam_p, &DesSt) == 0) {
42747c478bd9Sstevel@tonic-gate 		/*
42757c478bd9Sstevel@tonic-gate 		 * A file by the same name exists.  Move it to a temporary
42767c478bd9Sstevel@tonic-gate 		 * file.
42777c478bd9Sstevel@tonic-gate 		 */
42787c478bd9Sstevel@tonic-gate 
42797c478bd9Sstevel@tonic-gate 		if (creat_tmp(nam_p) < 0) {
42807c478bd9Sstevel@tonic-gate 			/*
42817c478bd9Sstevel@tonic-gate 			 * We weren't able to create the temp file.  Report
42827c478bd9Sstevel@tonic-gate 			 * failure.
42837c478bd9Sstevel@tonic-gate 			 */
42847c478bd9Sstevel@tonic-gate 
42857c478bd9Sstevel@tonic-gate 			return (-1);
42867c478bd9Sstevel@tonic-gate 		}
42877c478bd9Sstevel@tonic-gate 	}
42887c478bd9Sstevel@tonic-gate 
42897c478bd9Sstevel@tonic-gate 	if (Do_rename) {
42907c478bd9Sstevel@tonic-gate 		/* nam_p was changed by creat_tmp() above. */
42917c478bd9Sstevel@tonic-gate 
42927c478bd9Sstevel@tonic-gate 		if (Args & OCp) {
42937c478bd9Sstevel@tonic-gate 			if (G_p->g_attrnam_p != (char *)NULL) {
42947c478bd9Sstevel@tonic-gate 				nam_p = Attrfile_p;
42957c478bd9Sstevel@tonic-gate 			} else {
42967c478bd9Sstevel@tonic-gate 				nam_p = Fullnam_p;
42977c478bd9Sstevel@tonic-gate 			}
42987c478bd9Sstevel@tonic-gate 		} else {
42997c478bd9Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
43007c478bd9Sstevel@tonic-gate 		}
43017c478bd9Sstevel@tonic-gate 	}
43027c478bd9Sstevel@tonic-gate 
43037c478bd9Sstevel@tonic-gate 	/*
43047c478bd9Sstevel@tonic-gate 	 * This pile tries to create the file directly, and, if there is a
43057c478bd9Sstevel@tonic-gate 	 * problem, creates missing directories, and then tries to create the
43067c478bd9Sstevel@tonic-gate 	 * file again.  Two strikes and you're out.
43077c478bd9Sstevel@tonic-gate 	 *
43087c478bd9Sstevel@tonic-gate 	 * On XATTR system, the directory has already been created by
43097c478bd9Sstevel@tonic-gate 	 * open_dirfd(), so error shouldn't happen in the loop. However,
43107c478bd9Sstevel@tonic-gate 	 * on non-XATTR system, symlink/open may fail with ENOENT. In such
43117c478bd9Sstevel@tonic-gate 	 * case, we go to create missing directories.
43127c478bd9Sstevel@tonic-gate 	 */
43137c478bd9Sstevel@tonic-gate 
43147c478bd9Sstevel@tonic-gate 	cnt = 0;
43157c478bd9Sstevel@tonic-gate 
43167c478bd9Sstevel@tonic-gate 	do {
43177c478bd9Sstevel@tonic-gate 		errno = 0;
43187c478bd9Sstevel@tonic-gate 
43197c478bd9Sstevel@tonic-gate 		if (Hdr_type == TAR && Thdr_p->tbuf.t_typeflag == SYMTYPE) {
43207c478bd9Sstevel@tonic-gate 			/* The archive file is a TAR symlink. */
43217c478bd9Sstevel@tonic-gate 			if ((result =
43227c478bd9Sstevel@tonic-gate 			    symlink(Thdr_p->tbuf.t_linkname, nam_p)) >= 0) {
43237c478bd9Sstevel@tonic-gate 				cnt = 0;
43247c478bd9Sstevel@tonic-gate 				if (Over_p != NULL) {
43257c478bd9Sstevel@tonic-gate 					(void) unlinkat(dirfd,
43267c478bd9Sstevel@tonic-gate 					    get_component(Over_p), 0);
43277c478bd9Sstevel@tonic-gate 					*Over_p = '\0';
43287c478bd9Sstevel@tonic-gate 				}
43297c478bd9Sstevel@tonic-gate 				break;
43307c478bd9Sstevel@tonic-gate 			} else if (errno != ENOENT) {
43317c478bd9Sstevel@tonic-gate 				/* The attempt to symlink failed. */
43327c478bd9Sstevel@tonic-gate 				msg(ERRN,
43337c478bd9Sstevel@tonic-gate 				    "Cannot create symbolic link \"%s\" -> "
43347c478bd9Sstevel@tonic-gate 				    "\"%s\"",
43357c478bd9Sstevel@tonic-gate 				    Thdr_p->tbuf.t_linkname, nam_p);
43367c478bd9Sstevel@tonic-gate 
43377c478bd9Sstevel@tonic-gate 				if (*Over_p != '\0') {
43387c478bd9Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
43397c478bd9Sstevel@tonic-gate 				}
43407c478bd9Sstevel@tonic-gate 				return (-1);
43417c478bd9Sstevel@tonic-gate 			}
43427c478bd9Sstevel@tonic-gate 		} else if (Hdr_type == BAR && bar_linkflag == SYMTYPE) {
43437c478bd9Sstevel@tonic-gate 			if ((result = symlink(bar_linkname, nam_p)) >= 0) {
43447c478bd9Sstevel@tonic-gate 				cnt = 0;
43457c478bd9Sstevel@tonic-gate 				if (Over_p != NULL) {
43467c478bd9Sstevel@tonic-gate 					(void) unlinkat(dirfd,
43477c478bd9Sstevel@tonic-gate 					    get_component(Over_p), 0);
43487c478bd9Sstevel@tonic-gate 					*Over_p = '\0';
43497c478bd9Sstevel@tonic-gate 				}
43507c478bd9Sstevel@tonic-gate 				break;
43517c478bd9Sstevel@tonic-gate 			} else if (errno != ENOENT) {
43527c478bd9Sstevel@tonic-gate 				/* The attempt to symlink failed. */
43537c478bd9Sstevel@tonic-gate 				msg(ERRN,
43547c478bd9Sstevel@tonic-gate 				    "Cannot create symbolic link \"%s\" -> "
43557c478bd9Sstevel@tonic-gate 				    "\"%s\"",
43567c478bd9Sstevel@tonic-gate 				    bar_linkname, nam_p);
43577c478bd9Sstevel@tonic-gate 				if (*Over_p != '\0') {
43587c478bd9Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
43597c478bd9Sstevel@tonic-gate 				}
43607c478bd9Sstevel@tonic-gate 				return (-1);
43617c478bd9Sstevel@tonic-gate 			}
43627c478bd9Sstevel@tonic-gate 		} else if ((G_p->g_mode & Ftype) == S_IFLNK) {
43637c478bd9Sstevel@tonic-gate 			if ((!(Args & OCp)) && !(Hdr_type == USTAR)) {
43647c478bd9Sstevel@tonic-gate 				(void) strncpy(Symlnk_p,
43657c478bd9Sstevel@tonic-gate 				    Buffr.b_out_p, G_p->g_filesz);
43667c478bd9Sstevel@tonic-gate 				*(Symlnk_p + G_p->g_filesz) = '\0';
43677c478bd9Sstevel@tonic-gate 			} else if ((!(Args & OCp)) && (Hdr_type == USTAR)) {
43687c478bd9Sstevel@tonic-gate 				Symlnk_p[NAMSIZ] = '\0';
43697c478bd9Sstevel@tonic-gate 				(void) strncpy(Symlnk_p,
43707c478bd9Sstevel@tonic-gate 				    &Thdr_p->tbuf.t_linkname[0], NAMSIZ);
43717c478bd9Sstevel@tonic-gate 			}
43727c478bd9Sstevel@tonic-gate 			if ((result = symlink(Symlnk_p, nam_p)) >= 0) {
43737c478bd9Sstevel@tonic-gate 				cnt = 0;
43747c478bd9Sstevel@tonic-gate 				if (Over_p != NULL) {
43757c478bd9Sstevel@tonic-gate 					(void) unlinkat(dirfd,
43767c478bd9Sstevel@tonic-gate 					    get_component(Over_p), 0);
43777c478bd9Sstevel@tonic-gate 					*Over_p = '\0';
43787c478bd9Sstevel@tonic-gate 				}
43797c478bd9Sstevel@tonic-gate 				break;
43807c478bd9Sstevel@tonic-gate 			} else if (errno != ENOENT) {
43817c478bd9Sstevel@tonic-gate 				/* The attempt to symlink failed. */
43827c478bd9Sstevel@tonic-gate 				msg(ERRN,
43837c478bd9Sstevel@tonic-gate 				    "Cannot create symbolic link \"%s\" -> "
43847c478bd9Sstevel@tonic-gate 				    "\"%s\"",
43857c478bd9Sstevel@tonic-gate 				    Symlnk_p, nam_p);
43867c478bd9Sstevel@tonic-gate 
43877c478bd9Sstevel@tonic-gate 				if (*Over_p != '\0') {
43887c478bd9Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
43897c478bd9Sstevel@tonic-gate 				}
43907c478bd9Sstevel@tonic-gate 				return (-1);
43917c478bd9Sstevel@tonic-gate 			}
43927c478bd9Sstevel@tonic-gate 		} else {
43937c478bd9Sstevel@tonic-gate 			if ((result = openat(dirfd, get_component(nam_p),
43947c478bd9Sstevel@tonic-gate 			    O_CREAT|O_RDWR|O_TRUNC, (int)G_p->g_mode)) >= 0) {
43957c478bd9Sstevel@tonic-gate 				/* acl support */
4396fa9e4066Sahrens 				acl_is_set = 0;
43977c478bd9Sstevel@tonic-gate 				if (Pflag && aclp != NULL) {
4398fa9e4066Sahrens 					if (facl_set(result, aclp) < 0) {
43997c478bd9Sstevel@tonic-gate 						msg(ERRN,
44007c478bd9Sstevel@tonic-gate 						    "\"%s\": failed to set acl",
44017c478bd9Sstevel@tonic-gate 						    nam_p);
44027c478bd9Sstevel@tonic-gate 					} else {
4403fa9e4066Sahrens 						acl_is_set = 1;
44047c478bd9Sstevel@tonic-gate 					}
4405fa9e4066Sahrens 					acl_free(aclp);
44067c478bd9Sstevel@tonic-gate 					aclp = NULL;
44077c478bd9Sstevel@tonic-gate 				}
44087c478bd9Sstevel@tonic-gate 				cnt = 0;
44097c478bd9Sstevel@tonic-gate 				break;
44107c478bd9Sstevel@tonic-gate 			} else if (errno != ENOENT) {
44117c478bd9Sstevel@tonic-gate 				/* The attempt to open failed. */
44127c478bd9Sstevel@tonic-gate 				msg(ERRN, "Cannot open file \"%s\"", nam_p);
44137c478bd9Sstevel@tonic-gate 				if (*Over_p != '\0') {
44147c478bd9Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
44157c478bd9Sstevel@tonic-gate 				}
44167c478bd9Sstevel@tonic-gate 				return (-1);
44177c478bd9Sstevel@tonic-gate 			}
44187c478bd9Sstevel@tonic-gate 		}
44197c478bd9Sstevel@tonic-gate 		cnt++;
44207c478bd9Sstevel@tonic-gate 	} while (cnt < 2 && missdir(nam_p) == 0);
44217c478bd9Sstevel@tonic-gate 
44227c478bd9Sstevel@tonic-gate 	switch (cnt) {
44237c478bd9Sstevel@tonic-gate 	case 0:
44247c478bd9Sstevel@tonic-gate 		if ((Args & OCi) && (Hdr_type == USTAR)) {
44257c478bd9Sstevel@tonic-gate 			setpasswd(nam_p);
44267c478bd9Sstevel@tonic-gate 		}
44277c478bd9Sstevel@tonic-gate 		if ((G_p->g_mode & Ftype) == S_IFLNK ||
44287c478bd9Sstevel@tonic-gate 		    (Hdr_type == BAR && bar_linkflag == SYMTYPE)) {
4429647ab8f4Sceastha 			if (Args & OCR) {
4430647ab8f4Sceastha 				if (fchownat(dirfd,
4431647ab8f4Sceastha 				    get_component(nam_p),
4432647ab8f4Sceastha 				    (int)Rpw_p->pw_uid,
4433647ab8f4Sceastha 				    (int)Rpw_p->pw_gid,
44347c478bd9Sstevel@tonic-gate 				    AT_SYMLINK_NOFOLLOW) < 0) {
44357c478bd9Sstevel@tonic-gate 					msg(ERRN,
44367c478bd9Sstevel@tonic-gate 					    "Error during chown() of "
44377c478bd9Sstevel@tonic-gate 					    "\"%s%s%s\"",
4438647ab8f4Sceastha 					    (G_p->g_attrnam_p ==
4439647ab8f4Sceastha 					    (char *)NULL) ?
44407c478bd9Sstevel@tonic-gate 					    nam_p : G_p->g_attrfnam_p,
4441647ab8f4Sceastha 					    (G_p->g_attrnam_p ==
4442647ab8f4Sceastha 					    (char *)NULL) ?
44437c478bd9Sstevel@tonic-gate 					    "" : gettext(" Attribute "),
4444647ab8f4Sceastha 					    (G_p->g_attrnam_p ==
4445647ab8f4Sceastha 					    (char *)NULL) ?
44467c478bd9Sstevel@tonic-gate 					    "" : nam_p);
44477c478bd9Sstevel@tonic-gate 				}
4448647ab8f4Sceastha 			} else if ((fchownat(dirfd, get_component(nam_p),
4449647ab8f4Sceastha 			    (int)G_p->g_uid, (int)G_p->g_gid,
4450647ab8f4Sceastha 			    AT_SYMLINK_NOFOLLOW) < 0) && privileged) {
44517c478bd9Sstevel@tonic-gate 				msg(ERRN,
44527c478bd9Sstevel@tonic-gate 				    "Error during chown() of \"%s%s%s\"",
44537c478bd9Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
44547c478bd9Sstevel@tonic-gate 				    nam_p : G_p->g_attrfnam_p,
44557c478bd9Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
44567c478bd9Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
44577c478bd9Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
44587c478bd9Sstevel@tonic-gate 				    "" : nam_p);
44597c478bd9Sstevel@tonic-gate 			}
44607c478bd9Sstevel@tonic-gate 		}
44617c478bd9Sstevel@tonic-gate 		break;
44627c478bd9Sstevel@tonic-gate 
44637c478bd9Sstevel@tonic-gate 	case 1:
44647c478bd9Sstevel@tonic-gate 		if (Do_rename) {
44657c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot create directory for \"%s%s%s\"",
44667c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? Over_p :
44677c478bd9Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
44687c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
44697c478bd9Sstevel@tonic-gate 			    gettext(" Attribute "),
44707c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" : Over_p);
44717c478bd9Sstevel@tonic-gate 		} else {
44727c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot create directory for \"%s%s%s\"",
44737c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? nam_p :
44747c478bd9Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
44757c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
44767c478bd9Sstevel@tonic-gate 			    gettext(" Attribute "),
44777c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" : nam_p);
44787c478bd9Sstevel@tonic-gate 		}
44797c478bd9Sstevel@tonic-gate 		break;
44807c478bd9Sstevel@tonic-gate 
44817c478bd9Sstevel@tonic-gate 	case 2:
44827c478bd9Sstevel@tonic-gate 		if (Do_rename) {
44837c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot create \"%s%s%s\"",
44847c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? Over_p :
44857c478bd9Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
44867c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
44877c478bd9Sstevel@tonic-gate 			    gettext(" Attribute "),
44887c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
44897c478bd9Sstevel@tonic-gate 			    Over_p);
44907c478bd9Sstevel@tonic-gate 		} else {
44917c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot create \"%s%s%s\"",
44927c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? nam_p :
44937c478bd9Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
44947c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
44957c478bd9Sstevel@tonic-gate 			    gettext(" Attribute "),
44967c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
44977c478bd9Sstevel@tonic-gate 			    nam_p);
44987c478bd9Sstevel@tonic-gate 		}
44997c478bd9Sstevel@tonic-gate 		break;
45007c478bd9Sstevel@tonic-gate 
45017c478bd9Sstevel@tonic-gate 	default:
45027c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible case.");
45037c478bd9Sstevel@tonic-gate 	}
45047c478bd9Sstevel@tonic-gate 
45057c478bd9Sstevel@tonic-gate 	Finished = 0;
45067c478bd9Sstevel@tonic-gate 	return (result);
45077c478bd9Sstevel@tonic-gate }
45087c478bd9Sstevel@tonic-gate 
45097c478bd9Sstevel@tonic-gate /*
45107c478bd9Sstevel@tonic-gate  * read_hdr: Transfer headers from the selected format
45117c478bd9Sstevel@tonic-gate  * in the archive I/O buffer to the generic structure.
45127c478bd9Sstevel@tonic-gate  */
45137c478bd9Sstevel@tonic-gate 
45147c478bd9Sstevel@tonic-gate static
45157c478bd9Sstevel@tonic-gate int
45167c478bd9Sstevel@tonic-gate read_hdr(int hdr)
45177c478bd9Sstevel@tonic-gate {
45187c478bd9Sstevel@tonic-gate 	int rv = NONE;
45197c478bd9Sstevel@tonic-gate 	major_t maj, rmaj;
45207c478bd9Sstevel@tonic-gate 	minor_t min, rmin;
45217c478bd9Sstevel@tonic-gate 	char tmpnull;
45227c478bd9Sstevel@tonic-gate 	static int bar_read_cnt = 0;
45237c478bd9Sstevel@tonic-gate 
45247c478bd9Sstevel@tonic-gate 	if (hdr != BAR) {
45257c478bd9Sstevel@tonic-gate 		if (Buffr.b_end_p != (Buffr.b_out_p + Hdrsz)) {
45267c478bd9Sstevel@tonic-gate 			tmpnull = *(Buffr.b_out_p + Hdrsz);
45277c478bd9Sstevel@tonic-gate 			*(Buffr.b_out_p + Hdrsz) = '\0';
45287c478bd9Sstevel@tonic-gate 		}
45297c478bd9Sstevel@tonic-gate 	}
45307c478bd9Sstevel@tonic-gate 
45317c478bd9Sstevel@tonic-gate 	switch (hdr) {
45327c478bd9Sstevel@tonic-gate 	case BIN:
45337c478bd9Sstevel@tonic-gate 		(void) memcpy(&Hdr, Buffr.b_out_p, HDRSZ);
45347c478bd9Sstevel@tonic-gate 		if (Hdr.h_magic == (short)CMN_BBS) {
45357c478bd9Sstevel@tonic-gate 			swap((char *)&Hdr, HDRSZ);
45367c478bd9Sstevel@tonic-gate 		}
45377c478bd9Sstevel@tonic-gate 		Gen.g_magic = Hdr.h_magic;
45387c478bd9Sstevel@tonic-gate 		Gen.g_mode = Hdr.h_mode;
45397c478bd9Sstevel@tonic-gate 		Gen.g_uid = Hdr.h_uid;
45407c478bd9Sstevel@tonic-gate 		Gen.g_gid = Hdr.h_gid;
45417c478bd9Sstevel@tonic-gate 		Gen.g_nlink = Hdr.h_nlink;
45427c478bd9Sstevel@tonic-gate 		Gen.g_mtime = mklong(Hdr.h_mtime);
45437c478bd9Sstevel@tonic-gate 		Gen.g_ino = Hdr.h_ino;
45447c478bd9Sstevel@tonic-gate 		Gen.g_dev = Hdr.h_dev;
45457c478bd9Sstevel@tonic-gate 		Gen.g_rdev = Hdr.h_rdev;
45467c478bd9Sstevel@tonic-gate 		Gen.g_cksum = 0L;
45477c478bd9Sstevel@tonic-gate 		Gen.g_filesz = (off_t)mklong(Hdr.h_filesize);
45487c478bd9Sstevel@tonic-gate 		Gen.g_namesz = Hdr.h_namesize;
45497c478bd9Sstevel@tonic-gate 		rv = BIN;
45507c478bd9Sstevel@tonic-gate 		break;
45517c478bd9Sstevel@tonic-gate 	case CHR:
45527c478bd9Sstevel@tonic-gate 		if (sscanf(Buffr.b_out_p,
45537c478bd9Sstevel@tonic-gate 		    "%6lo%6lo%6lo%6lo%6lo%6lo%6lo%6lo%11lo%6o%11llo",
45547c478bd9Sstevel@tonic-gate 		    &Gen.g_magic, &Gen.g_dev, &Gen.g_ino, &Gen.g_mode,
45557c478bd9Sstevel@tonic-gate 		    &Gen.g_uid, &Gen.g_gid, &Gen.g_nlink, &Gen.g_rdev,
45567c478bd9Sstevel@tonic-gate 		    (ulong_t *)&Gen.g_mtime, (uint_t *)&Gen.g_namesz,
45577c478bd9Sstevel@tonic-gate 		    (u_off_t *)&Gen.g_filesz) == CHR_CNT) {
45587c478bd9Sstevel@tonic-gate 			rv = CHR;
45597c478bd9Sstevel@tonic-gate #define	cpioMAJOR(x)	(int)(((unsigned)x >> 8) & 0x7F)
45607c478bd9Sstevel@tonic-gate #define	cpioMINOR(x)	(int)(x & 0xFF)
45617c478bd9Sstevel@tonic-gate 			maj = cpioMAJOR(Gen.g_dev);
45627c478bd9Sstevel@tonic-gate 			rmaj = cpioMAJOR(Gen.g_rdev);
45637c478bd9Sstevel@tonic-gate 			min = cpioMINOR(Gen.g_dev);
45647c478bd9Sstevel@tonic-gate 			rmin = cpioMINOR(Gen.g_rdev);
45657c478bd9Sstevel@tonic-gate 			if (Use_old_stat) {
45667c478bd9Sstevel@tonic-gate 				/* needs error checking */
45677c478bd9Sstevel@tonic-gate 				Gen.g_dev = (maj << 8) | min;
45687c478bd9Sstevel@tonic-gate 				Gen.g_rdev = (rmaj << 8) | rmin;
45697c478bd9Sstevel@tonic-gate 			} else {
45707c478bd9Sstevel@tonic-gate 				Gen.g_dev = makedev(maj, min);
45717c478bd9Sstevel@tonic-gate 				Gen.g_rdev = makedev(rmaj, rmin);
45727c478bd9Sstevel@tonic-gate 			}
45737c478bd9Sstevel@tonic-gate 		}
45747c478bd9Sstevel@tonic-gate 		break;
45757c478bd9Sstevel@tonic-gate 	case ASC:
45767c478bd9Sstevel@tonic-gate 	case CRC:
45777c478bd9Sstevel@tonic-gate 		if (sscanf(Buffr.b_out_p,
45787c478bd9Sstevel@tonic-gate 		    "%6lx%8lx%8lx%8lx%8lx%8lx%8lx%8llx%8x%8x%8x%8x%8x%8lx",
45797c478bd9Sstevel@tonic-gate 		    &Gen.g_magic, &Gen.g_ino, &Gen.g_mode, &Gen.g_uid,
45807c478bd9Sstevel@tonic-gate 		    &Gen.g_gid, &Gen.g_nlink, &Gen.g_mtime,
45817c478bd9Sstevel@tonic-gate 		    (u_off_t *)&Gen.g_filesz, (uint_t *)&maj, (uint_t *)&min,
45827c478bd9Sstevel@tonic-gate 		    (uint_t *)&rmaj, (uint_t *)&rmin, (uint_t *)&Gen.g_namesz,
45837c478bd9Sstevel@tonic-gate 		    &Gen.g_cksum) == ASC_CNT) {
45847c478bd9Sstevel@tonic-gate 			Gen.g_dev = makedev(maj, min);
45857c478bd9Sstevel@tonic-gate 			Gen.g_rdev = makedev(rmaj, rmin);
45867c478bd9Sstevel@tonic-gate 			rv = hdr;
45877c478bd9Sstevel@tonic-gate 		}
45887c478bd9Sstevel@tonic-gate 		break;
45897c478bd9Sstevel@tonic-gate 	case USTAR: /* TAR and USTAR */
45907c478bd9Sstevel@tonic-gate 		if (*Buffr.b_out_p == '\0') {
45917c478bd9Sstevel@tonic-gate 			*Gen.g_nam_p = '\0';
45927c478bd9Sstevel@tonic-gate 			nambuf[0] = '\0';
45937c478bd9Sstevel@tonic-gate 		} else {
45947c478bd9Sstevel@tonic-gate 			Thdr_p = (union tblock *)Buffr.b_out_p;
45957c478bd9Sstevel@tonic-gate 			Gen.g_nam_p[0] = '\0';
45967c478bd9Sstevel@tonic-gate 			(void) strncpy((char *)&nambuf,
45977c478bd9Sstevel@tonic-gate 			    Thdr_p->tbuf.t_name, NAMSIZ);
45987c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mode, "%8lo",
45997c478bd9Sstevel@tonic-gate 			    &Gen.g_mode);
46007c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_uid, "%8lo", &Gen.g_uid);
46017c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_gid, "%8lo", &Gen.g_gid);
46027c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_size, "%11llo",
46037c478bd9Sstevel@tonic-gate 			    (u_off_t *)&Gen.g_filesz);
46047c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mtime, "%12lo",
46057c478bd9Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_mtime);
46067c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_cksum, "%8lo",
46077c478bd9Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_cksum);
46087c478bd9Sstevel@tonic-gate 			if (Thdr_p->tbuf.t_linkname[0] != (char)NULL)
46097c478bd9Sstevel@tonic-gate 				Gen.g_nlink = 1;
46107c478bd9Sstevel@tonic-gate 			else
46117c478bd9Sstevel@tonic-gate 				Gen.g_nlink = 0;
46127c478bd9Sstevel@tonic-gate 
46137c478bd9Sstevel@tonic-gate 			switch (Thdr_p->tbuf.t_typeflag) {
46147c478bd9Sstevel@tonic-gate 			case SYMTYPE:
46157c478bd9Sstevel@tonic-gate 				/* Symbolic Link */
46167c478bd9Sstevel@tonic-gate 				Gen.g_nlink = 2;
46177c478bd9Sstevel@tonic-gate 				break;
46187c478bd9Sstevel@tonic-gate 			case CHRTYPE:
46197c478bd9Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFCHR);
46207c478bd9Sstevel@tonic-gate 				break;
46217c478bd9Sstevel@tonic-gate 			case BLKTYPE:
46227c478bd9Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFBLK);
46237c478bd9Sstevel@tonic-gate 				break;
46247c478bd9Sstevel@tonic-gate 			case DIRTYPE:
46257c478bd9Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFDIR);
46267c478bd9Sstevel@tonic-gate 				break;
46277c478bd9Sstevel@tonic-gate 			case FIFOTYPE:
46287c478bd9Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFIFO);
46297c478bd9Sstevel@tonic-gate 				break;
46307c478bd9Sstevel@tonic-gate 			}
46317c478bd9Sstevel@tonic-gate 
46327c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_magic, "%8lo",
46337c478bd9Sstevel@tonic-gate 			    /* LINTED alignment */
46347c478bd9Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_tmagic);
46357c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_version, "%8lo",
46367c478bd9Sstevel@tonic-gate 			    /* LINTED alignment */
46377c478bd9Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_version);
46387c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_uname, "%32s",
46397c478bd9Sstevel@tonic-gate 			    (char *)&Gen.g_uname);
46407c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_gname, "%32s",
46417c478bd9Sstevel@tonic-gate 			    (char *)&Gen.g_gname);
46427c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_devmajor, "%8lo",
46437c478bd9Sstevel@tonic-gate 			    &Gen.g_dev);
46447c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_devminor, "%8lo",
46457c478bd9Sstevel@tonic-gate 			    &Gen.g_rdev);
46467c478bd9Sstevel@tonic-gate 			(void) strncpy((char *)&prebuf,
46477c478bd9Sstevel@tonic-gate 			    Thdr_p->tbuf.t_prefix, PRESIZ);
46487c478bd9Sstevel@tonic-gate 			Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
46497c478bd9Sstevel@tonic-gate 			Gen.g_dev = makedev(maj, min);
46507c478bd9Sstevel@tonic-gate 		}
46517c478bd9Sstevel@tonic-gate 		rv = USTAR;
46527c478bd9Sstevel@tonic-gate 		break;
46537c478bd9Sstevel@tonic-gate 	case TAR:
46547c478bd9Sstevel@tonic-gate 		if (*Buffr.b_out_p == '\0') {
46557c478bd9Sstevel@tonic-gate 			*Gen.g_nam_p = '\0';
46567c478bd9Sstevel@tonic-gate 			nambuf[0] = '\0';
46577c478bd9Sstevel@tonic-gate 		} else {
46587c478bd9Sstevel@tonic-gate 			Thdr_p = (union tblock *)Buffr.b_out_p;
46597c478bd9Sstevel@tonic-gate 			Gen.g_nam_p[0] = '\0';
46607c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mode, "%lo", &Gen.g_mode);
46617c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_uid, "%lo", &Gen.g_uid);
46627c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_gid, "%lo", &Gen.g_gid);
46637c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_size, "%llo",
46647c478bd9Sstevel@tonic-gate 			    (u_off_t *)&Gen.g_filesz);
46657c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mtime, "%lo",
46667c478bd9Sstevel@tonic-gate 			    &Gen.g_mtime);
46677c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_cksum, "%lo",
46687c478bd9Sstevel@tonic-gate 			    &Gen.g_cksum);
46697c478bd9Sstevel@tonic-gate 			if (Thdr_p->tbuf.t_typeflag == '1')	/* hardlink */
46707c478bd9Sstevel@tonic-gate 				Gen.g_nlink = 1;
46717c478bd9Sstevel@tonic-gate 			else
46727c478bd9Sstevel@tonic-gate 				Gen.g_nlink = 0;
46737c478bd9Sstevel@tonic-gate 			(void) strncpy(Gen.g_nam_p,
46747c478bd9Sstevel@tonic-gate 			    Thdr_p->tbuf.t_name, NAMSIZ);
46757c478bd9Sstevel@tonic-gate 			Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
46767c478bd9Sstevel@tonic-gate 			(void) strcpy(nambuf, Gen.g_nam_p);
46777c478bd9Sstevel@tonic-gate 		}
46787c478bd9Sstevel@tonic-gate 		rv = TAR;
46797c478bd9Sstevel@tonic-gate 		break;
46807c478bd9Sstevel@tonic-gate 	case BAR:
46817c478bd9Sstevel@tonic-gate 		if (Bar_vol_num == 0 && bar_read_cnt == 0) {
46827c478bd9Sstevel@tonic-gate 			read_bar_vol_hdr();
46837c478bd9Sstevel@tonic-gate 			bar_read_cnt++;
46847c478bd9Sstevel@tonic-gate 		}
46857c478bd9Sstevel@tonic-gate 		else
46867c478bd9Sstevel@tonic-gate 			read_bar_file_hdr();
46877c478bd9Sstevel@tonic-gate 		rv = BAR;
46887c478bd9Sstevel@tonic-gate 		break;
46897c478bd9Sstevel@tonic-gate 	default:
46907c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
46917c478bd9Sstevel@tonic-gate 	}
46927c478bd9Sstevel@tonic-gate 
46937c478bd9Sstevel@tonic-gate 	if (hdr != BAR) {
46947c478bd9Sstevel@tonic-gate 		if (Buffr.b_end_p != (Buffr.b_out_p + Hdrsz))
46957c478bd9Sstevel@tonic-gate 			*(Buffr.b_out_p + Hdrsz) = tmpnull;
46967c478bd9Sstevel@tonic-gate 	}
46977c478bd9Sstevel@tonic-gate 
46987c478bd9Sstevel@tonic-gate 	return (rv);
46997c478bd9Sstevel@tonic-gate }
47007c478bd9Sstevel@tonic-gate 
47017c478bd9Sstevel@tonic-gate /*
47027c478bd9Sstevel@tonic-gate  * reclaim: Reclaim linked file structure storage.
47037c478bd9Sstevel@tonic-gate  */
47047c478bd9Sstevel@tonic-gate 
47057c478bd9Sstevel@tonic-gate static void
47067c478bd9Sstevel@tonic-gate reclaim(struct Lnk *p)
47077c478bd9Sstevel@tonic-gate {
47087c478bd9Sstevel@tonic-gate 	p->L_bck_p->L_nxt_p = p->L_nxt_p;
47097c478bd9Sstevel@tonic-gate 	p->L_nxt_p->L_bck_p = p->L_bck_p;
47107c478bd9Sstevel@tonic-gate 
47117c478bd9Sstevel@tonic-gate 	while (p != NULL) {
47127c478bd9Sstevel@tonic-gate 		struct Lnk *new_p = p->L_lnk_p;
47137c478bd9Sstevel@tonic-gate 
47147c478bd9Sstevel@tonic-gate 		free(p->L_gen.g_nam_p);
47157c478bd9Sstevel@tonic-gate 		free(p);
47167c478bd9Sstevel@tonic-gate 		p = new_p;
47177c478bd9Sstevel@tonic-gate 	}
47187c478bd9Sstevel@tonic-gate }
47197c478bd9Sstevel@tonic-gate 
47207c478bd9Sstevel@tonic-gate /*
47217c478bd9Sstevel@tonic-gate  * rstbuf: Reset the I/O buffer, move incomplete potential headers to
47227c478bd9Sstevel@tonic-gate  * the front of the buffer and force bread() to refill the buffer.  The
47237c478bd9Sstevel@tonic-gate  * return value from bread() is returned (to identify I/O errors).  On the
47247c478bd9Sstevel@tonic-gate  * 3B2, reads must begin on a word boundary, therefore, with the -i option,
47257c478bd9Sstevel@tonic-gate  * any remaining bytes in the buffer must be moved to the base of the buffer
47267c478bd9Sstevel@tonic-gate  * in such a way that the destination locations of subsequent reads are
47277c478bd9Sstevel@tonic-gate  * word aligned.
47287c478bd9Sstevel@tonic-gate  */
47297c478bd9Sstevel@tonic-gate 
47307c478bd9Sstevel@tonic-gate static void
47317c478bd9Sstevel@tonic-gate rstbuf(void)
47327c478bd9Sstevel@tonic-gate {
47337c478bd9Sstevel@tonic-gate 	int pad;
47347c478bd9Sstevel@tonic-gate 
47357c478bd9Sstevel@tonic-gate 	if ((Args & OCi) || Append) {
47367c478bd9Sstevel@tonic-gate 		if (Buffr.b_out_p != Buffr.b_base_p) {
47377c478bd9Sstevel@tonic-gate 			pad = ((Buffr.b_cnt + FULLWD) & ~FULLWD);
47387c478bd9Sstevel@tonic-gate 			Buffr.b_in_p = Buffr.b_base_p + pad;
47397c478bd9Sstevel@tonic-gate 			pad -= Buffr.b_cnt;
47407c478bd9Sstevel@tonic-gate 			(void) memcpy(Buffr.b_base_p + pad, Buffr.b_out_p,
47417c478bd9Sstevel@tonic-gate 			    (int)Buffr.b_cnt);
47427c478bd9Sstevel@tonic-gate 			Buffr.b_out_p = Buffr.b_base_p + pad;
47437c478bd9Sstevel@tonic-gate 		}
47447c478bd9Sstevel@tonic-gate 		if (bfill() < 0)
47457c478bd9Sstevel@tonic-gate 			msg(EXT, "Unexpected end-of-archive encountered.");
47467c478bd9Sstevel@tonic-gate 	} else { /* OCo */
47477c478bd9Sstevel@tonic-gate 		(void) memcpy(Buffr.b_base_p, Buffr.b_out_p, (int)Buffr.b_cnt);
47487c478bd9Sstevel@tonic-gate 		Buffr.b_out_p = Buffr.b_base_p;
47497c478bd9Sstevel@tonic-gate 		Buffr.b_in_p = Buffr.b_base_p + Buffr.b_cnt;
47507c478bd9Sstevel@tonic-gate 	}
47517c478bd9Sstevel@tonic-gate }
47527c478bd9Sstevel@tonic-gate 
47537c478bd9Sstevel@tonic-gate static void
47547c478bd9Sstevel@tonic-gate setpasswd(char *nam)
47557c478bd9Sstevel@tonic-gate {
47567c478bd9Sstevel@tonic-gate 	if ((dpasswd = getpwnam(&Gen.g_uname[0])) == (struct passwd *)NULL) {
47577c478bd9Sstevel@tonic-gate 		msg(EPOST, "cpio: problem reading passwd entry");
47587c478bd9Sstevel@tonic-gate 		msg(EPOST, "cpio: %s: owner not changed", nam);
47597c478bd9Sstevel@tonic-gate 		if (Gen.g_uid == UID_NOBODY && S_ISREG(Gen.g_mode))
47607c478bd9Sstevel@tonic-gate 			Gen.g_mode &= ~S_ISUID;
47617c478bd9Sstevel@tonic-gate 	} else
47627c478bd9Sstevel@tonic-gate 		Gen.g_uid = dpasswd->pw_uid;
47637c478bd9Sstevel@tonic-gate 
47647c478bd9Sstevel@tonic-gate 	if ((dgroup = getgrnam(&Gen.g_gname[0])) == (struct group *)NULL) {
47657c478bd9Sstevel@tonic-gate 		msg(EPOST, "cpio: problem reading group entry");
47667c478bd9Sstevel@tonic-gate 		msg(EPOST, "cpio: %s: group not changed", nam);
47677c478bd9Sstevel@tonic-gate 		if (Gen.g_gid == GID_NOBODY && S_ISREG(Gen.g_mode))
47687c478bd9Sstevel@tonic-gate 			Gen.g_mode &= ~S_ISGID;
47697c478bd9Sstevel@tonic-gate 	} else
47707c478bd9Sstevel@tonic-gate 		Gen.g_gid = dgroup->gr_gid;
47717c478bd9Sstevel@tonic-gate 	G_p = &Gen;
47727c478bd9Sstevel@tonic-gate }
47737c478bd9Sstevel@tonic-gate 
47747c478bd9Sstevel@tonic-gate /*
47757c478bd9Sstevel@tonic-gate  * rstfiles:  Perform final changes to the file.  If the -u option is set,
47767c478bd9Sstevel@tonic-gate  * and overwrite == U_OVER, remove the temporary file, else if overwrite
47777c478bd9Sstevel@tonic-gate  * == U_KEEP, unlink the current file, and restore the existing version
47787c478bd9Sstevel@tonic-gate  * of the file.  In addition, where appropriate, set the access or modification
47797c478bd9Sstevel@tonic-gate  * times, change the owner and change the modes of the file.
47807c478bd9Sstevel@tonic-gate  *
47817c478bd9Sstevel@tonic-gate  * Note that if Do_rename is set, then the roles of original and temporary
47827c478bd9Sstevel@tonic-gate  * file are reversed. If all went well, we will rename() the temporary file
4783*da6c28aaSamw  * over the original in order to accommodate potentially executing files.
47847c478bd9Sstevel@tonic-gate  */
47857c478bd9Sstevel@tonic-gate static void
47867c478bd9Sstevel@tonic-gate rstfiles(int over, int dirfd)
47877c478bd9Sstevel@tonic-gate {
47887c478bd9Sstevel@tonic-gate 	char *inam_p, *onam_p, *nam_p;
47897c478bd9Sstevel@tonic-gate 	int error;
47907c478bd9Sstevel@tonic-gate 
47917c478bd9Sstevel@tonic-gate 	if (Args & OCp) {
47927c478bd9Sstevel@tonic-gate 		if (G_p->g_attrnam_p == (char *)NULL) {
47937c478bd9Sstevel@tonic-gate 			nam_p = Fullnam_p;
47947c478bd9Sstevel@tonic-gate 		} else {
47957c478bd9Sstevel@tonic-gate 			nam_p = G_p->g_attrnam_p;
47967c478bd9Sstevel@tonic-gate 		}
47977c478bd9Sstevel@tonic-gate 	} else {
47987c478bd9Sstevel@tonic-gate 		if (Gen.g_nlink > (ulong_t)0) {
47997c478bd9Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
48007c478bd9Sstevel@tonic-gate 		} else {
48017c478bd9Sstevel@tonic-gate 			nam_p = Gen.g_nam_p;
48027c478bd9Sstevel@tonic-gate 		}
48037c478bd9Sstevel@tonic-gate 	}
48047c478bd9Sstevel@tonic-gate 	if (Gen.g_attrnam_p != (char *)NULL) {
48057c478bd9Sstevel@tonic-gate 		nam_p = Gen.g_attrnam_p;
48067c478bd9Sstevel@tonic-gate 	}
48077c478bd9Sstevel@tonic-gate 
48087c478bd9Sstevel@tonic-gate 	if ((Args & OCi) && (Hdr_type == USTAR)) {
48097c478bd9Sstevel@tonic-gate 		setpasswd(nam_p);
48107c478bd9Sstevel@tonic-gate 	}
48117c478bd9Sstevel@tonic-gate 	if (over == U_KEEP && *Over_p != '\0') {
48127c478bd9Sstevel@tonic-gate 		if (Do_rename) {
48137c478bd9Sstevel@tonic-gate 			msg(POST, "Restoring existing \"%s%s%s\"",
48147c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
48157c478bd9Sstevel@tonic-gate 			    Over_p : Fullnam_p,
48167c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
48177c478bd9Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
48187c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
48197c478bd9Sstevel@tonic-gate 			    "" : Over_p);
48207c478bd9Sstevel@tonic-gate 		} else {
48217c478bd9Sstevel@tonic-gate 			msg(POST, "Restoring existing \"%s%s%s\"",
48227c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
48237c478bd9Sstevel@tonic-gate 			    nam_p : Fullnam_p,
48247c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
48257c478bd9Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
48267c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
48277c478bd9Sstevel@tonic-gate 			    "" : nam_p);
48287c478bd9Sstevel@tonic-gate 		}
48297c478bd9Sstevel@tonic-gate 
48307c478bd9Sstevel@tonic-gate 		/* delete what we just built */
48317c478bd9Sstevel@tonic-gate 		(void) unlinkat(dirfd, get_component(nam_p), 0);
48327c478bd9Sstevel@tonic-gate 
48337c478bd9Sstevel@tonic-gate 		/* If the old file needs restoring, do the necessary links */
48347c478bd9Sstevel@tonic-gate 		if (Do_rename) {
48357c478bd9Sstevel@tonic-gate 			char *tmp_ptr;
48367c478bd9Sstevel@tonic-gate 
48377c478bd9Sstevel@tonic-gate 			if (Args & OCp) {
48387c478bd9Sstevel@tonic-gate 				tmp_ptr = Fullnam_p;
48397c478bd9Sstevel@tonic-gate 				Fullnam_p = Over_p;
48407c478bd9Sstevel@tonic-gate 			} else {
48417c478bd9Sstevel@tonic-gate 				tmp_ptr = G_p->g_nam_p;
48427c478bd9Sstevel@tonic-gate 				G_p->g_nam_p = Over_p;
48437c478bd9Sstevel@tonic-gate 			}
48447c478bd9Sstevel@tonic-gate 			Over_p = tmp_ptr;
48457c478bd9Sstevel@tonic-gate 
48467c478bd9Sstevel@tonic-gate 			Do_rename = 0;	/* names now have original values */
48477c478bd9Sstevel@tonic-gate 		} else {
48487c478bd9Sstevel@tonic-gate 			if (rename(Over_p, nam_p) < 0) {
48497c478bd9Sstevel@tonic-gate 				if (link(Over_p, nam_p) < 0) {
48507c478bd9Sstevel@tonic-gate 					msg(EXTN,
48517c478bd9Sstevel@tonic-gate 					    "Cannot recover original version"
48527c478bd9Sstevel@tonic-gate 					    " of \"%s%s%s\"",
48537c478bd9Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
48547c478bd9Sstevel@tonic-gate 					    nam_p : Fullnam_p,
48557c478bd9Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
48567c478bd9Sstevel@tonic-gate 					    "" : gettext(" Attribute "),
48577c478bd9Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
48587c478bd9Sstevel@tonic-gate 					    "" : nam_p);
48597c478bd9Sstevel@tonic-gate 				}
48607c478bd9Sstevel@tonic-gate 				if (unlinkat(dirfd, get_component(Over_p), 0)) {
48617c478bd9Sstevel@tonic-gate 					msg(ERRN,
48627c478bd9Sstevel@tonic-gate 					    "Cannot remove temp file "
48637c478bd9Sstevel@tonic-gate 					    "\"%s%s%s\"",
48647c478bd9Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
48657c478bd9Sstevel@tonic-gate 					    Over_p : Fullnam_p,
48667c478bd9Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
48677c478bd9Sstevel@tonic-gate 					    "" : gettext(" Attribute "),
48687c478bd9Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
48697c478bd9Sstevel@tonic-gate 					    "" : Over_p);
48707c478bd9Sstevel@tonic-gate 				}
48717c478bd9Sstevel@tonic-gate 			}
48727c478bd9Sstevel@tonic-gate 		}
48737c478bd9Sstevel@tonic-gate 		*Over_p = '\0';
48747c478bd9Sstevel@tonic-gate 		return;
48757c478bd9Sstevel@tonic-gate 	} else if (over == U_OVER && *Over_p != '\0') {
48767c478bd9Sstevel@tonic-gate 		if (Do_rename) {
48777c478bd9Sstevel@tonic-gate 			char *tmp_ptr;
48787c478bd9Sstevel@tonic-gate 
48797c478bd9Sstevel@tonic-gate 			(void) renameat(dirfd, get_component(nam_p),
48807c478bd9Sstevel@tonic-gate 			    dirfd, get_component(Over_p));
48817c478bd9Sstevel@tonic-gate 			if (Args & OCp) {
48827c478bd9Sstevel@tonic-gate 				if (G_p->g_attrnam_p == (char *)NULL) {
48837c478bd9Sstevel@tonic-gate 					tmp_ptr = Fullnam_p;
48847c478bd9Sstevel@tonic-gate 					Fullnam_p = Over_p;
48857c478bd9Sstevel@tonic-gate 					Over_p = tmp_ptr;
48867c478bd9Sstevel@tonic-gate 				} else {
48877c478bd9Sstevel@tonic-gate 					/*
48887c478bd9Sstevel@tonic-gate 					 * Over_p is pointing at g_attrnam_p
48897c478bd9Sstevel@tonic-gate 					 * which must be preserved.
48907c478bd9Sstevel@tonic-gate 					 *
48917c478bd9Sstevel@tonic-gate 					 * We don't want the tmp_ptr and so
48927c478bd9Sstevel@tonic-gate 					 * on to throw away our only copy of
48937c478bd9Sstevel@tonic-gate 					 * the name.
48947c478bd9Sstevel@tonic-gate 					 */
48957c478bd9Sstevel@tonic-gate 					Over_p = Attrfile_p;
48967c478bd9Sstevel@tonic-gate 				}
48977c478bd9Sstevel@tonic-gate 			} else {
48987c478bd9Sstevel@tonic-gate 				tmp_ptr = G_p->g_nam_p;
48997c478bd9Sstevel@tonic-gate 				G_p->g_nam_p = Over_p;
49007c478bd9Sstevel@tonic-gate 				Over_p = tmp_ptr;
49017c478bd9Sstevel@tonic-gate 			}
49027c478bd9Sstevel@tonic-gate 			Do_rename = 0;	/* names now have original values */
49037c478bd9Sstevel@tonic-gate 		} else {
49047c478bd9Sstevel@tonic-gate 			if (unlinkat(dirfd, get_component(Over_p), 0) < 0) {
49057c478bd9Sstevel@tonic-gate 				msg(ERRN,
49067c478bd9Sstevel@tonic-gate 				    "Cannot unlink() temp file \"%s%s%s\"",
49077c478bd9Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
49087c478bd9Sstevel@tonic-gate 				    Over_p : Fullnam_p,
49097c478bd9Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
49107c478bd9Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
49117c478bd9Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
49127c478bd9Sstevel@tonic-gate 				    "" : Over_p);
49137c478bd9Sstevel@tonic-gate 			}
49147c478bd9Sstevel@tonic-gate 		}
49157c478bd9Sstevel@tonic-gate 		*Over_p = '\0';
49167c478bd9Sstevel@tonic-gate 	}
49177c478bd9Sstevel@tonic-gate 	if (Args & OCp) {
49187c478bd9Sstevel@tonic-gate 		if (G_p->g_attrnam_p != (char *)NULL) {
49197c478bd9Sstevel@tonic-gate 			inam_p = G_p->g_attrfnam_p;
49207c478bd9Sstevel@tonic-gate 			onam_p = G_p->g_attrnam_p;
49217c478bd9Sstevel@tonic-gate 		} else {
49227c478bd9Sstevel@tonic-gate 			inam_p = Nam_p;
49237c478bd9Sstevel@tonic-gate 			onam_p = Fullnam_p;
49247c478bd9Sstevel@tonic-gate 		}
49257c478bd9Sstevel@tonic-gate 	} else /* OCi only uses onam_p, OCo only uses inam_p */
49267c478bd9Sstevel@tonic-gate 		if (G_p->g_attrnam_p != (char *)NULL) {
49277c478bd9Sstevel@tonic-gate 			inam_p = onam_p = G_p->g_attrnam_p;
49287c478bd9Sstevel@tonic-gate 		} else {
49297c478bd9Sstevel@tonic-gate 			inam_p = onam_p = G_p->g_nam_p;
49307c478bd9Sstevel@tonic-gate 		}
49317c478bd9Sstevel@tonic-gate 
49327c478bd9Sstevel@tonic-gate 	/*
4933647ab8f4Sceastha 	 * Change the owner, time, and mode to those of the file
4934647ab8f4Sceastha 	 * originally created in the archive.  Note: time and
4935647ab8f4Sceastha 	 * mode do not need to be restored for a symbolic link
4936647ab8f4Sceastha 	 * since rstfiles() is not called when the archived file
4937647ab8f4Sceastha 	 * is a symlink.
49387c478bd9Sstevel@tonic-gate 	 */
4939647ab8f4Sceastha 	if (!(Args & OCo)) {
4940647ab8f4Sceastha 		if (Args & OCR) {
4941647ab8f4Sceastha 			if (fchownat(dirfd, get_component(onam_p),
4942647ab8f4Sceastha 			    Rpw_p->pw_uid, Rpw_p->pw_gid,
4943647ab8f4Sceastha 			    AT_SYMLINK_NOFOLLOW) < 0) {
4944647ab8f4Sceastha 				msg(ERRN, "Cannot chown() \"%s%s%s\"",
4945647ab8f4Sceastha 				    onam_p,
4946647ab8f4Sceastha 				    (G_p->g_attrnam_p == (char *)NULL) ?
4947647ab8f4Sceastha 				    "" : gettext(" Attribute "),
4948647ab8f4Sceastha 				    (G_p->g_attrnam_p == (char *)NULL) ?
4949647ab8f4Sceastha 				    "" : onam_p);
4950647ab8f4Sceastha 			}
4951647ab8f4Sceastha 		} else {
4952647ab8f4Sceastha 			if ((fchownat(dirfd, get_component(onam_p),
4953647ab8f4Sceastha 			    G_p->g_uid, G_p->g_gid,
4954647ab8f4Sceastha 			    AT_SYMLINK_NOFOLLOW) < 0) && privileged) {
4955647ab8f4Sceastha 				msg(ERRN, "Cannot chown() \"%s%s%s\"",
4956647ab8f4Sceastha 				    onam_p,
4957647ab8f4Sceastha 				    (G_p->g_attrnam_p == (char *)NULL) ?
4958647ab8f4Sceastha 				    "" : gettext(" Attribute "),
4959647ab8f4Sceastha 				    (G_p->g_attrnam_p == (char *)NULL) ?
4960647ab8f4Sceastha 				    "" : onam_p);
4961647ab8f4Sceastha 			}
4962647ab8f4Sceastha 		}
49637c478bd9Sstevel@tonic-gate 
4964647ab8f4Sceastha 		if (Args & OCm) {
4965647ab8f4Sceastha 			set_tym(dirfd, get_component(onam_p),
4966647ab8f4Sceastha 			    G_p->g_mtime, G_p->g_mtime);
4967647ab8f4Sceastha 		}
4968647ab8f4Sceastha 
49697c478bd9Sstevel@tonic-gate 		/* Acl was not set, so we must chmod */
4970647ab8f4Sceastha 		if (!acl_is_set) {
4971647ab8f4Sceastha 			mode_t orig_mask, new_mask;
49727c478bd9Sstevel@tonic-gate 
49737c478bd9Sstevel@tonic-gate 			/*
49747c478bd9Sstevel@tonic-gate 			 * use fchmod for attributes, since
49757c478bd9Sstevel@tonic-gate 			 * we known they are always regular
49767c478bd9Sstevel@tonic-gate 			 * files, whereas when it isn't an
49777c478bd9Sstevel@tonic-gate 			 * attribute it could be for a fifo
49787c478bd9Sstevel@tonic-gate 			 * or something other that we don't
49797c478bd9Sstevel@tonic-gate 			 * open and don't have a valid Ofile
49807c478bd9Sstevel@tonic-gate 			 * for.
49817c478bd9Sstevel@tonic-gate 			 */
4982647ab8f4Sceastha 			if (privileged) {
4983647ab8f4Sceastha 				new_mask = G_p->g_mode;
4984647ab8f4Sceastha 			} else {
4985647ab8f4Sceastha 				orig_mask = umask(0);
4986647ab8f4Sceastha 				new_mask = G_p->g_mode & ~orig_mask;
4987647ab8f4Sceastha 			}
4988647ab8f4Sceastha 
49897c478bd9Sstevel@tonic-gate 			if (G_p->g_attrnam_p != (char *)NULL) {
49907c478bd9Sstevel@tonic-gate 				error = fchmod(Ofile, new_mask);
49917c478bd9Sstevel@tonic-gate 			} else {
4992647ab8f4Sceastha 				error = chmod(onam_p, new_mask);
49937c478bd9Sstevel@tonic-gate 			}
49947c478bd9Sstevel@tonic-gate 			if (error < 0) {
49957c478bd9Sstevel@tonic-gate 				msg(ERRN,
49967c478bd9Sstevel@tonic-gate 				    "Cannot chmod() \"%s%s%s\"",
4997647ab8f4Sceastha 				    (G_p->g_attrnam_p == (char *)NULL) ?
4998647ab8f4Sceastha 				    onam_p : G_p->g_attrfnam_p,
4999647ab8f4Sceastha 				    (G_p->g_attrnam_p == (char *)NULL) ?
5000647ab8f4Sceastha 				    "" : gettext(" Attribute "),
5001647ab8f4Sceastha 				    (G_p->g_attrnam_p == (char *)NULL) ?
5002647ab8f4Sceastha 				    "" : onam_p);
50037c478bd9Sstevel@tonic-gate 			}
5004647ab8f4Sceastha 			if (!privileged) {
50057c478bd9Sstevel@tonic-gate 				(void) umask(orig_mask);
50067c478bd9Sstevel@tonic-gate 			}
50077c478bd9Sstevel@tonic-gate 		}
50087c478bd9Sstevel@tonic-gate 	}
50097c478bd9Sstevel@tonic-gate 
50107c478bd9Sstevel@tonic-gate 	if (!(Args & OCi) && (Args & OCa)) {
50117c478bd9Sstevel@tonic-gate 		/*
50127c478bd9Sstevel@tonic-gate 		 * Use dirfd since we are updating original file
50137c478bd9Sstevel@tonic-gate 		 * and not just created file
50147c478bd9Sstevel@tonic-gate 		 */
50157c478bd9Sstevel@tonic-gate 		set_tym(G_p->g_dirfd, get_component(inam_p),
50167c478bd9Sstevel@tonic-gate 		    (ulong_t)SrcSt.st_atime, (ulong_t)SrcSt.st_mtime);
50177c478bd9Sstevel@tonic-gate 	}
50187c478bd9Sstevel@tonic-gate }
50197c478bd9Sstevel@tonic-gate 
50207c478bd9Sstevel@tonic-gate /*
50217c478bd9Sstevel@tonic-gate  * scan4trail: Scan the archive looking for the trailer.
50227c478bd9Sstevel@tonic-gate  * When found, back the archive up over the trailer and overwrite
50237c478bd9Sstevel@tonic-gate  * the trailer with the files to be added to the archive.
50247c478bd9Sstevel@tonic-gate  */
50257c478bd9Sstevel@tonic-gate 
50267c478bd9Sstevel@tonic-gate static void
50277c478bd9Sstevel@tonic-gate scan4trail(void)
50287c478bd9Sstevel@tonic-gate {
50297c478bd9Sstevel@tonic-gate 	int rv;
50307c478bd9Sstevel@tonic-gate 	off_t off1, off2;
50317c478bd9Sstevel@tonic-gate 
50327c478bd9Sstevel@tonic-gate 	Append = 1;
50337c478bd9Sstevel@tonic-gate 	Hdr_type = NONE;
50347c478bd9Sstevel@tonic-gate 	G_p = (struct gen_hdr *)NULL;
50357c478bd9Sstevel@tonic-gate 	while (gethdr()) {
50367c478bd9Sstevel@tonic-gate 		G_p = &Gen;
50377c478bd9Sstevel@tonic-gate 		data_in(P_SKIP);
50387c478bd9Sstevel@tonic-gate 	}
50397c478bd9Sstevel@tonic-gate 	off1 = Buffr.b_cnt;
50407c478bd9Sstevel@tonic-gate 	off2 = Bufsize - (Buffr.b_cnt % Bufsize);
50417c478bd9Sstevel@tonic-gate 	Buffr.b_out_p = Buffr.b_in_p = Buffr.b_base_p;
50427c478bd9Sstevel@tonic-gate 	Buffr.b_cnt = (off_t)0;
50437c478bd9Sstevel@tonic-gate 	if (lseek(Archive, -(off1 + off2), SEEK_REL) < 0)
50447c478bd9Sstevel@tonic-gate 		msg(EXTN, "Unable to append to this archive");
50457c478bd9Sstevel@tonic-gate 	if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0)
50467c478bd9Sstevel@tonic-gate 		msg(EXTN, "Cannot append to this archive");
50477c478bd9Sstevel@tonic-gate 	if (lseek(Archive, (off_t)-rv, SEEK_REL) < 0)
50487c478bd9Sstevel@tonic-gate 		msg(EXTN, "Unable to append to this archive");
50497c478bd9Sstevel@tonic-gate 	Buffr.b_cnt = off2;
50507c478bd9Sstevel@tonic-gate 	Buffr.b_in_p = Buffr.b_base_p + Buffr.b_cnt;
50517c478bd9Sstevel@tonic-gate 	Append = 0;
50527c478bd9Sstevel@tonic-gate }
50537c478bd9Sstevel@tonic-gate 
50547c478bd9Sstevel@tonic-gate /*
50557c478bd9Sstevel@tonic-gate  * setup:  Perform setup and initialization functions.  Parse the options
50567c478bd9Sstevel@tonic-gate  * using getopt(3C), call ckopts to check the options and initialize various
50577c478bd9Sstevel@tonic-gate  * structures and pointers.  Specifically, for the -i option, save any
50587c478bd9Sstevel@tonic-gate  * patterns, for the -o option, check (via stat(2)) the archive, and for
50597c478bd9Sstevel@tonic-gate  * the -p option, validate the destination directory.
50607c478bd9Sstevel@tonic-gate  */
50617c478bd9Sstevel@tonic-gate 
50627c478bd9Sstevel@tonic-gate static void
50637c478bd9Sstevel@tonic-gate setup(int largc, char **largv)
50647c478bd9Sstevel@tonic-gate {
50657c478bd9Sstevel@tonic-gate 	extern int optind;
50667c478bd9Sstevel@tonic-gate 	extern char *optarg;
50677c478bd9Sstevel@tonic-gate 
50687c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
50697c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
50707c478bd9Sstevel@tonic-gate 	char	*opts_p = "zabcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6@";
50717c478bd9Sstevel@tonic-gate #else
50727c478bd9Sstevel@tonic-gate 	char	*opts_p = "abcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6@";
50737c478bd9Sstevel@tonic-gate #endif
50747c478bd9Sstevel@tonic-gate #else
50757c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
50767c478bd9Sstevel@tonic-gate 	char	*opts_p = "zabcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6";
50777c478bd9Sstevel@tonic-gate #else
50787c478bd9Sstevel@tonic-gate 	char	*opts_p = "abcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6";
50797c478bd9Sstevel@tonic-gate #endif
50807c478bd9Sstevel@tonic-gate #endif
50817c478bd9Sstevel@tonic-gate 
50827c478bd9Sstevel@tonic-gate 	char   *dupl_p = "Only one occurrence of -%c allowed";
50837c478bd9Sstevel@tonic-gate 	int option;
50847c478bd9Sstevel@tonic-gate 	int blk_cnt, blk_cnt_max;
50857c478bd9Sstevel@tonic-gate 	struct rlimit rlim;
50867c478bd9Sstevel@tonic-gate 
50877c478bd9Sstevel@tonic-gate 	/* Remember the native page size. */
50887c478bd9Sstevel@tonic-gate 
50897c478bd9Sstevel@tonic-gate 	PageSize = sysconf(_SC_PAGESIZE);
50907c478bd9Sstevel@tonic-gate 
50917c478bd9Sstevel@tonic-gate 	if (PageSize == -1) {
50927c478bd9Sstevel@tonic-gate 		/*
50937c478bd9Sstevel@tonic-gate 		 * This sysconf call will almost certainly never fail.  The
50947c478bd9Sstevel@tonic-gate 		 * symbol PAGESIZE itself resolves to the above sysconf call,
50957c478bd9Sstevel@tonic-gate 		 * so we should go ahead and define our own constant.
50967c478bd9Sstevel@tonic-gate 		 */
50977c478bd9Sstevel@tonic-gate 		PageSize = 8192;
50987c478bd9Sstevel@tonic-gate 	}
50997c478bd9Sstevel@tonic-gate 
51007c478bd9Sstevel@tonic-gate 	Hdr_type = BIN;
51017c478bd9Sstevel@tonic-gate 	Max_offset = (off_t)(BIN_OFFSET_MAX);
51027c478bd9Sstevel@tonic-gate 	Efil_p = Hdr_p = Own_p = IOfil_p = NULL;
51037c478bd9Sstevel@tonic-gate 	while ((option = getopt(largc, largv, opts_p)) != EOF) {
51047c478bd9Sstevel@tonic-gate 		switch (option) {
51057c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
51067c478bd9Sstevel@tonic-gate 		case 'z':
51077c478bd9Sstevel@tonic-gate 			/* rendezvous with the debugger */
51087c478bd9Sstevel@tonic-gate 			waitaround = 1;
51097c478bd9Sstevel@tonic-gate 			break;
51107c478bd9Sstevel@tonic-gate #endif
51117c478bd9Sstevel@tonic-gate 		case 'a':	/* reset access time */
51127c478bd9Sstevel@tonic-gate 			Args |= OCa;
51137c478bd9Sstevel@tonic-gate 			break;
51147c478bd9Sstevel@tonic-gate 		case 'b':	/* swap bytes and halfwords */
51157c478bd9Sstevel@tonic-gate 			Args |= OCb;
51167c478bd9Sstevel@tonic-gate 			break;
51177c478bd9Sstevel@tonic-gate 		case 'c':	/* select character header */
51187c478bd9Sstevel@tonic-gate 			Args |= OCc;
51197c478bd9Sstevel@tonic-gate 			Hdr_type = ASC;
51207c478bd9Sstevel@tonic-gate 			Max_namesz = APATH;
51217c478bd9Sstevel@tonic-gate 			Onecopy = 1;
51227c478bd9Sstevel@tonic-gate 			break;
51237c478bd9Sstevel@tonic-gate 		case 'd':	/* create directories as needed */
51247c478bd9Sstevel@tonic-gate 			Args |= OCd;
51257c478bd9Sstevel@tonic-gate 			break;
51267c478bd9Sstevel@tonic-gate 		case 'f':	/* select files not in patterns */
51277c478bd9Sstevel@tonic-gate 			Args |= OCf;
51287c478bd9Sstevel@tonic-gate 			break;
51297c478bd9Sstevel@tonic-gate 		case 'i':	/* "copy in" */
51307c478bd9Sstevel@tonic-gate 			Args |= OCi;
51317c478bd9Sstevel@tonic-gate 			Archive = 0;
51327c478bd9Sstevel@tonic-gate 			break;
51337c478bd9Sstevel@tonic-gate 		case 'k':	/* retry after I/O errors */
51347c478bd9Sstevel@tonic-gate 			Args |= OCk;
51357c478bd9Sstevel@tonic-gate 			break;
51367c478bd9Sstevel@tonic-gate 		case 'l':	/* link files when possible */
51377c478bd9Sstevel@tonic-gate 			Args |= OCl;
51387c478bd9Sstevel@tonic-gate 			break;
51397c478bd9Sstevel@tonic-gate 		case 'm':	/* retain modification time */
51407c478bd9Sstevel@tonic-gate 			Args |= OCm;
51417c478bd9Sstevel@tonic-gate 			break;
51427c478bd9Sstevel@tonic-gate 		case 'o':	/* "copy out" */
51437c478bd9Sstevel@tonic-gate 			Args |= OCo;
51447c478bd9Sstevel@tonic-gate 			Archive = 1;
51457c478bd9Sstevel@tonic-gate 			break;
51467c478bd9Sstevel@tonic-gate 		case 'p':	/* "pass" */
51477c478bd9Sstevel@tonic-gate 			Max_namesz = APATH;
51487c478bd9Sstevel@tonic-gate 			Args |= OCp;
51497c478bd9Sstevel@tonic-gate 			break;
51507c478bd9Sstevel@tonic-gate 		case 'r':	/* rename files interactively */
51517c478bd9Sstevel@tonic-gate 			Args |= OCr;
51527c478bd9Sstevel@tonic-gate 			break;
51537c478bd9Sstevel@tonic-gate 		case 's':	/* swap bytes */
51547c478bd9Sstevel@tonic-gate 			Args |= OCs;
51557c478bd9Sstevel@tonic-gate 			break;
51567c478bd9Sstevel@tonic-gate 		case 't':	/* table of contents */
51577c478bd9Sstevel@tonic-gate 			Args |= OCt;
51587c478bd9Sstevel@tonic-gate 			break;
51597c478bd9Sstevel@tonic-gate 		case 'u':	/* copy unconditionally */
51607c478bd9Sstevel@tonic-gate 			Args |= OCu;
51617c478bd9Sstevel@tonic-gate 			break;
51627c478bd9Sstevel@tonic-gate 		case 'v':	/* verbose - print file names */
51637c478bd9Sstevel@tonic-gate 			Args |= OCv;
51647c478bd9Sstevel@tonic-gate 			break;
51657c478bd9Sstevel@tonic-gate 		case 'A':	/* append to existing archive */
51667c478bd9Sstevel@tonic-gate 			Args |= OCA;
51677c478bd9Sstevel@tonic-gate 			break;
51687c478bd9Sstevel@tonic-gate 		case 'B':	/* set block size to 5120 bytes */
51697c478bd9Sstevel@tonic-gate 			Args |= OCB;
51707c478bd9Sstevel@tonic-gate 			Bufsize = 5120;
51717c478bd9Sstevel@tonic-gate 			break;
51727c478bd9Sstevel@tonic-gate 		case 'C':	/* set arbitrary block size */
51737c478bd9Sstevel@tonic-gate 			if (Args & OCC)
51747c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'C');
51757c478bd9Sstevel@tonic-gate 			else {
51767c478bd9Sstevel@tonic-gate 				Args |= OCC;
51777c478bd9Sstevel@tonic-gate 				Bufsize = atoi(optarg);
51787c478bd9Sstevel@tonic-gate 			}
51797c478bd9Sstevel@tonic-gate 			break;
51807c478bd9Sstevel@tonic-gate 		case 'D':
51817c478bd9Sstevel@tonic-gate 			Dflag = 1;
51827c478bd9Sstevel@tonic-gate 			break;
51837c478bd9Sstevel@tonic-gate 		case 'E':	/* alternate file for pattern input */
51847c478bd9Sstevel@tonic-gate 			if (Args & OCE)
51857c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'E');
51867c478bd9Sstevel@tonic-gate 			else {
51877c478bd9Sstevel@tonic-gate 				Args |= OCE;
51887c478bd9Sstevel@tonic-gate 				Efil_p = optarg;
51897c478bd9Sstevel@tonic-gate 			}
51907c478bd9Sstevel@tonic-gate 			break;
51917c478bd9Sstevel@tonic-gate 		case 'H':	/* select header type */
51927c478bd9Sstevel@tonic-gate 			if (Args & OCH)
51937c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'H');
51947c478bd9Sstevel@tonic-gate 			else {
51957c478bd9Sstevel@tonic-gate 				Args |= OCH;
51967c478bd9Sstevel@tonic-gate 				Hdr_p = optarg;
51977c478bd9Sstevel@tonic-gate 			}
51987c478bd9Sstevel@tonic-gate 			break;
51997c478bd9Sstevel@tonic-gate 		case 'I':	/* alternate file for archive input */
52007c478bd9Sstevel@tonic-gate 			if (Args & OCI)
52017c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'I');
52027c478bd9Sstevel@tonic-gate 			else {
52037c478bd9Sstevel@tonic-gate 				Args |= OCI;
52047c478bd9Sstevel@tonic-gate 				IOfil_p = optarg;
52057c478bd9Sstevel@tonic-gate 			}
52067c478bd9Sstevel@tonic-gate 			break;
52077c478bd9Sstevel@tonic-gate 		case 'L':	/* follow symbolic links */
52087c478bd9Sstevel@tonic-gate 			Args |= OCL;
52097c478bd9Sstevel@tonic-gate 			break;
52107c478bd9Sstevel@tonic-gate 		case 'M':	/* specify new end-of-media message */
52117c478bd9Sstevel@tonic-gate 			if (Args & OCM)
52127c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'M');
52137c478bd9Sstevel@tonic-gate 			else {
52147c478bd9Sstevel@tonic-gate 				Args |= OCM;
52157c478bd9Sstevel@tonic-gate 				Eom_p = optarg;
52167c478bd9Sstevel@tonic-gate 			}
52177c478bd9Sstevel@tonic-gate 			break;
52187c478bd9Sstevel@tonic-gate 		case 'O':	/* alternate file for archive output */
52197c478bd9Sstevel@tonic-gate 			if (Args & OCO)
52207c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'O');
52217c478bd9Sstevel@tonic-gate 			else {
52227c478bd9Sstevel@tonic-gate 				Args |= OCO;
52237c478bd9Sstevel@tonic-gate 				IOfil_p = optarg;
52247c478bd9Sstevel@tonic-gate 			}
52257c478bd9Sstevel@tonic-gate 			break;
52267c478bd9Sstevel@tonic-gate 		case 'P':	/* preserve acls */
52277c478bd9Sstevel@tonic-gate 			Args |= OCP;
52287c478bd9Sstevel@tonic-gate 			Pflag++;
52297c478bd9Sstevel@tonic-gate 			break;
52307c478bd9Sstevel@tonic-gate 		case 'R':	/* change owner/group of files */
52317c478bd9Sstevel@tonic-gate 			if (Args & OCR)
52327c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'R');
52337c478bd9Sstevel@tonic-gate 			else {
52347c478bd9Sstevel@tonic-gate 				Args |= OCR;
52357c478bd9Sstevel@tonic-gate 				Own_p = optarg;
52367c478bd9Sstevel@tonic-gate 			}
52377c478bd9Sstevel@tonic-gate 			break;
52387c478bd9Sstevel@tonic-gate 		case 'S':	/* swap halfwords */
52397c478bd9Sstevel@tonic-gate 			Args |= OCS;
52407c478bd9Sstevel@tonic-gate 			break;
52417c478bd9Sstevel@tonic-gate 		case 'V':	/* print a dot '.' for each file */
52427c478bd9Sstevel@tonic-gate 			Args |= OCV;
52437c478bd9Sstevel@tonic-gate 			break;
52447c478bd9Sstevel@tonic-gate 		case '6':	/* for old, sixth-edition files */
52457c478bd9Sstevel@tonic-gate 			Args |= OC6;
52467c478bd9Sstevel@tonic-gate 			Ftype = SIXTH;
52477c478bd9Sstevel@tonic-gate 			break;
52487c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
52497c478bd9Sstevel@tonic-gate 		case '@':
52507c478bd9Sstevel@tonic-gate 			Atflag++;
52517c478bd9Sstevel@tonic-gate 			break;
52527c478bd9Sstevel@tonic-gate #endif
52537c478bd9Sstevel@tonic-gate 		default:
52547c478bd9Sstevel@tonic-gate 			Error_cnt++;
52557c478bd9Sstevel@tonic-gate 		} /* option */
52567c478bd9Sstevel@tonic-gate 	} /* (option = getopt(largc, largv, opts_p)) != EOF */
52577c478bd9Sstevel@tonic-gate 
52587c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
52597c478bd9Sstevel@tonic-gate 	if (waitaround) {
52607c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Rendezvous with cpio on pid"
52617c478bd9Sstevel@tonic-gate 		    " %d\n"), getpid());
52627c478bd9Sstevel@tonic-gate 
52637c478bd9Sstevel@tonic-gate 		while (waitaround) {
52647c478bd9Sstevel@tonic-gate 			(void) sleep(10);
52657c478bd9Sstevel@tonic-gate 		}
52667c478bd9Sstevel@tonic-gate 	}
52677c478bd9Sstevel@tonic-gate #endif
52687c478bd9Sstevel@tonic-gate 
52697c478bd9Sstevel@tonic-gate 	largc -= optind;
52707c478bd9Sstevel@tonic-gate 	largv += optind;
52717c478bd9Sstevel@tonic-gate 	ckopts(Args);
52727c478bd9Sstevel@tonic-gate 	if (!Error_cnt) {
52737c478bd9Sstevel@tonic-gate 		Empty = e_valloc(E_EXIT, TARSZ);
52747c478bd9Sstevel@tonic-gate 		if (Args & OCr) {
52757c478bd9Sstevel@tonic-gate 			Renam_p = e_zalloc(E_EXIT, APATH + 1);
52767c478bd9Sstevel@tonic-gate 			Renametmp_p = e_zalloc(E_EXIT, APATH + 1);
52777c478bd9Sstevel@tonic-gate 		}
52787c478bd9Sstevel@tonic-gate 		Symlnk_p = e_zalloc(E_EXIT, APATH);
52797c478bd9Sstevel@tonic-gate 		Over_p = e_zalloc(E_EXIT, APATH);
52807c478bd9Sstevel@tonic-gate 		Nam_p = e_zalloc(E_EXIT, APATH + 1);
52817c478bd9Sstevel@tonic-gate 		if (Args & OCp) {
52827c478bd9Sstevel@tonic-gate 			Savenam_p = e_zalloc(E_EXIT, APATH + 1);
52837c478bd9Sstevel@tonic-gate 		}
52847c478bd9Sstevel@tonic-gate 		Fullnam_p = e_zalloc(E_EXIT, APATH);
52857c478bd9Sstevel@tonic-gate 		Lnknam_p = e_zalloc(E_EXIT, APATH);
52867c478bd9Sstevel@tonic-gate 		Gen.g_nam_p = Nam_p;
52877c478bd9Sstevel@tonic-gate 		if ((Fullnam_p = getcwd((char *)NULL, APATH)) == (char *)NULL)
52887c478bd9Sstevel@tonic-gate 			msg(EXT, "Unable to determine current directory.");
52897c478bd9Sstevel@tonic-gate 		if (Args & OCi) {
52907c478bd9Sstevel@tonic-gate 			if (largc > 0) /* save patterns for -i option, if any */
52917c478bd9Sstevel@tonic-gate 				Pat_pp = largv;
52927c478bd9Sstevel@tonic-gate 			if (Args & OCE)
52937c478bd9Sstevel@tonic-gate 				getpats(largc, largv);
52947c478bd9Sstevel@tonic-gate 		} else if (Args & OCo) {
52957c478bd9Sstevel@tonic-gate 			if (largc != 0) /* error if arguments left with -o */
52967c478bd9Sstevel@tonic-gate 				Error_cnt++;
52977c478bd9Sstevel@tonic-gate 			else if (fstat(Archive, &ArchSt) < 0)
52987c478bd9Sstevel@tonic-gate 				msg(ERRN, "Error during stat() of archive");
52997c478bd9Sstevel@tonic-gate 			switch (Hdr_type) {
53007c478bd9Sstevel@tonic-gate 			case BIN:
53017c478bd9Sstevel@tonic-gate 				Hdrsz = HDRSZ;
53027c478bd9Sstevel@tonic-gate 				Pad_val = HALFWD;
53037c478bd9Sstevel@tonic-gate 				break;
53047c478bd9Sstevel@tonic-gate 			case CHR:
53057c478bd9Sstevel@tonic-gate 				Hdrsz = CHRSZ;
53067c478bd9Sstevel@tonic-gate 				Pad_val = 0;
53077c478bd9Sstevel@tonic-gate 				Max_offset = (off_t)(CHAR_OFFSET_MAX);
53087c478bd9Sstevel@tonic-gate 				break;
53097c478bd9Sstevel@tonic-gate 			case ASC:
53107c478bd9Sstevel@tonic-gate 			case CRC:
53117c478bd9Sstevel@tonic-gate 				Hdrsz = ASCSZ;
53127c478bd9Sstevel@tonic-gate 				Pad_val = FULLWD;
5313944d83a0Schin 				Max_offset = (off_t)(ASC_OFFSET_MAX);
53147c478bd9Sstevel@tonic-gate 				break;
53157c478bd9Sstevel@tonic-gate 			case TAR:
53167c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
53177c478bd9Sstevel@tonic-gate 			case USTAR: /* TAR and USTAR */
53187c478bd9Sstevel@tonic-gate 				Hdrsz = TARSZ;
53197c478bd9Sstevel@tonic-gate 				Pad_val = FULLBK;
53207c478bd9Sstevel@tonic-gate 				Max_offset = (off_t)(CHAR_OFFSET_MAX);
53217c478bd9Sstevel@tonic-gate 				break;
53227c478bd9Sstevel@tonic-gate 			default:
53237c478bd9Sstevel@tonic-gate 				msg(EXT, "Impossible header type.");
53247c478bd9Sstevel@tonic-gate 			}
53257c478bd9Sstevel@tonic-gate 		} else { /* directory must be specified */
53267c478bd9Sstevel@tonic-gate 			if (largc != 1)
53277c478bd9Sstevel@tonic-gate 				Error_cnt++;
5328ed69b652Sjs198686 			else if (access(*largv, 2) < 0 && (errno != EACCES))
5329ed69b652Sjs198686 				/*
5330ed69b652Sjs198686 				 * EACCES is ignored here as it may occur
5331ed69b652Sjs198686 				 * when any directory component of the path
5332ed69b652Sjs198686 				 * does not have write permission, even though
5333ed69b652Sjs198686 				 * the destination subdirectory has write
5334ed69b652Sjs198686 				 * access. Writing to a read only directory
5335ed69b652Sjs198686 				 * is handled later, as in "copy in" mode.
5336ed69b652Sjs198686 				 */
53377c478bd9Sstevel@tonic-gate 				msg(ERRN,
53387c478bd9Sstevel@tonic-gate 				    "Error during access() of \"%s\"", *largv);
53397c478bd9Sstevel@tonic-gate 		}
53407c478bd9Sstevel@tonic-gate 	}
53417c478bd9Sstevel@tonic-gate 	if (Error_cnt)
53427c478bd9Sstevel@tonic-gate 		usage(); /* exits! */
53437c478bd9Sstevel@tonic-gate 	if (Args & (OCi | OCo)) {
53447c478bd9Sstevel@tonic-gate 		if (!Dflag) {
53457c478bd9Sstevel@tonic-gate 			if (Args & (OCB | OCC)) {
53467c478bd9Sstevel@tonic-gate 				if (g_init(&Device, &Archive) < 0)
53477c478bd9Sstevel@tonic-gate 					msg(EXTN,
53487c478bd9Sstevel@tonic-gate 					    "Error during initialization");
53497c478bd9Sstevel@tonic-gate 			} else {
53507c478bd9Sstevel@tonic-gate 				if ((Bufsize = g_init(&Device, &Archive)) < 0)
53517c478bd9Sstevel@tonic-gate 					msg(EXTN,
53527c478bd9Sstevel@tonic-gate 					    "Error during initialization");
53537c478bd9Sstevel@tonic-gate 			}
53547c478bd9Sstevel@tonic-gate 		}
53557c478bd9Sstevel@tonic-gate 
53567c478bd9Sstevel@tonic-gate 		blk_cnt_max = _20K / Bufsize;
53577c478bd9Sstevel@tonic-gate 		if (blk_cnt_max < MX_BUFS) {
53587c478bd9Sstevel@tonic-gate 			blk_cnt_max = MX_BUFS;
53597c478bd9Sstevel@tonic-gate 		}
53607c478bd9Sstevel@tonic-gate 
53617c478bd9Sstevel@tonic-gate 		Buffr.b_base_p = NULL;
53627c478bd9Sstevel@tonic-gate 
53637c478bd9Sstevel@tonic-gate 		for (blk_cnt = blk_cnt_max; blk_cnt > 1; blk_cnt--) {
53647c478bd9Sstevel@tonic-gate 			Buffr.b_size = (size_t)(Bufsize * blk_cnt);
53657c478bd9Sstevel@tonic-gate 			Buffr.b_base_p = e_valloc(E_NORMAL, Buffr.b_size);
53667c478bd9Sstevel@tonic-gate 			if (Buffr.b_base_p != NULL) {
53677c478bd9Sstevel@tonic-gate 				break;
53687c478bd9Sstevel@tonic-gate 			}
53697c478bd9Sstevel@tonic-gate 		}
53707c478bd9Sstevel@tonic-gate 		if (Buffr.b_base_p == NULL || Buffr.b_size < (2 * CPIOBSZ)) {
53717c478bd9Sstevel@tonic-gate 			msg(EXT, "Out of memory");
53727c478bd9Sstevel@tonic-gate 		}
53737c478bd9Sstevel@tonic-gate 
53747c478bd9Sstevel@tonic-gate 		Buffr.b_out_p = Buffr.b_in_p = Buffr.b_base_p;
53757c478bd9Sstevel@tonic-gate 		Buffr.b_cnt = 0L;
53767c478bd9Sstevel@tonic-gate 		Buffr.b_end_p = Buffr.b_base_p + Buffr.b_size;
53777c478bd9Sstevel@tonic-gate 	}
53787c478bd9Sstevel@tonic-gate 
53797c478bd9Sstevel@tonic-gate 	/*
53807c478bd9Sstevel@tonic-gate 	 * Now that Bufsize has stabilized, we can allocate our i/o buffer
53817c478bd9Sstevel@tonic-gate 	 */
53827c478bd9Sstevel@tonic-gate 	Buf_p = e_valloc(E_EXIT, Bufsize);
53837c478bd9Sstevel@tonic-gate 
53847c478bd9Sstevel@tonic-gate 	if (Args & OCp) { /* get destination directory */
53857c478bd9Sstevel@tonic-gate 		(void) strcpy(Fullnam_p, *largv);
53867c478bd9Sstevel@tonic-gate 		if (stat(Fullnam_p, &DesSt) < 0)
53877c478bd9Sstevel@tonic-gate 			msg(EXTN, "Error during stat() of \"%s\"", Fullnam_p);
53887c478bd9Sstevel@tonic-gate 		if ((DesSt.st_mode & Ftype) != S_IFDIR)
53897c478bd9Sstevel@tonic-gate 			msg(EXT, "\"%s\" is not a directory", Fullnam_p);
53907c478bd9Sstevel@tonic-gate 	}
53917c478bd9Sstevel@tonic-gate 	Full_p = Fullnam_p + strlen(Fullnam_p) - 1;
53927c478bd9Sstevel@tonic-gate 	if (*Full_p != '/') {
53937c478bd9Sstevel@tonic-gate 		Full_p++;
53947c478bd9Sstevel@tonic-gate 		*Full_p = '/';
53957c478bd9Sstevel@tonic-gate 	}
53967c478bd9Sstevel@tonic-gate 	Full_p++;
53977c478bd9Sstevel@tonic-gate 	*Full_p = '\0';
53987c478bd9Sstevel@tonic-gate 	(void) strcpy(Lnknam_p, Fullnam_p);
53997c478bd9Sstevel@tonic-gate 	Lnkend_p = Lnknam_p + strlen(Lnknam_p);
54007c478bd9Sstevel@tonic-gate 	(void) getrlimit(RLIMIT_FSIZE, &rlim);
54017c478bd9Sstevel@tonic-gate 	Max_filesz = (off_t)rlim.rlim_cur;
54027c478bd9Sstevel@tonic-gate 	Lnk_hd.L_nxt_p = Lnk_hd.L_bck_p = &Lnk_hd;
54037c478bd9Sstevel@tonic-gate 	Lnk_hd.L_lnk_p = (struct Lnk *)NULL;
54047c478bd9Sstevel@tonic-gate }
54057c478bd9Sstevel@tonic-gate 
54067c478bd9Sstevel@tonic-gate /*
54077c478bd9Sstevel@tonic-gate  * set_tym: Set the access and/or modification times for a file.
54087c478bd9Sstevel@tonic-gate  */
54097c478bd9Sstevel@tonic-gate 
54107c478bd9Sstevel@tonic-gate static void
54117c478bd9Sstevel@tonic-gate set_tym(int dirfd, char *nam_p, time_t atime, time_t mtime)
54127c478bd9Sstevel@tonic-gate {
54137c478bd9Sstevel@tonic-gate 	struct timeval times[2];
54147c478bd9Sstevel@tonic-gate 
54157c478bd9Sstevel@tonic-gate 	times[0].tv_sec = atime;
54167c478bd9Sstevel@tonic-gate 	times[0].tv_usec = 0;
54177c478bd9Sstevel@tonic-gate 	times[1].tv_sec = mtime;
54187c478bd9Sstevel@tonic-gate 	times[1].tv_usec = 0;
54197c478bd9Sstevel@tonic-gate 
54207c478bd9Sstevel@tonic-gate 	if (futimesat(dirfd, nam_p, times) < 0) {
54217c478bd9Sstevel@tonic-gate 		if (Args & OCa) {
54227c478bd9Sstevel@tonic-gate 			msg(ERRN,
54237c478bd9Sstevel@tonic-gate 			    "Unable to reset access time for \"%s%s%s\"",
54247c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
54257c478bd9Sstevel@tonic-gate 			    nam_p : Fullnam_p,
54267c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
54277c478bd9Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
54287c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
54297c478bd9Sstevel@tonic-gate 			    "" : nam_p);
54307c478bd9Sstevel@tonic-gate 		} else {
54317c478bd9Sstevel@tonic-gate 			msg(ERRN,
54327c478bd9Sstevel@tonic-gate 			    "Unable to reset modification time for \"%s%s%s\"",
54337c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
54347c478bd9Sstevel@tonic-gate 			    nam_p : Fullnam_p,
54357c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
54367c478bd9Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
54377c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
54387c478bd9Sstevel@tonic-gate 			    "" : nam_p);
54397c478bd9Sstevel@tonic-gate 		}
54407c478bd9Sstevel@tonic-gate 	}
54417c478bd9Sstevel@tonic-gate }
54427c478bd9Sstevel@tonic-gate 
54437c478bd9Sstevel@tonic-gate /*
54447c478bd9Sstevel@tonic-gate  * sigint:  Catch interrupts.  If an interrupt occurs during the extraction
54457c478bd9Sstevel@tonic-gate  * of a file from the archive with the -u option set, and the filename did
54467c478bd9Sstevel@tonic-gate  * exist, remove the current file and restore the original file.  Then exit.
54477c478bd9Sstevel@tonic-gate  */
54487c478bd9Sstevel@tonic-gate 
54497c478bd9Sstevel@tonic-gate /*ARGSUSED*/
54507c478bd9Sstevel@tonic-gate static void
54517c478bd9Sstevel@tonic-gate sigint(int sig)
54527c478bd9Sstevel@tonic-gate {
54537c478bd9Sstevel@tonic-gate 	char *nam_p;
54547c478bd9Sstevel@tonic-gate 
54557c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, SIG_IGN); /* block further signals */
54567c478bd9Sstevel@tonic-gate 	if (!Finished) {
54577c478bd9Sstevel@tonic-gate 		if (Args & OCi)
54587c478bd9Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
54597c478bd9Sstevel@tonic-gate 		else /* OCp */
54607c478bd9Sstevel@tonic-gate 			nam_p = Fullnam_p;
54617c478bd9Sstevel@tonic-gate 		if (*Over_p != '\0') { /* There is a temp file */
54627c478bd9Sstevel@tonic-gate 			if (unlink(nam_p) < 0) {
54637c478bd9Sstevel@tonic-gate 				msg(ERRN,
54647c478bd9Sstevel@tonic-gate 				    "Cannot remove incomplete \"%s\"", nam_p);
54657c478bd9Sstevel@tonic-gate 			}
54667c478bd9Sstevel@tonic-gate 			if (rename(Over_p, nam_p) < 0) {
54677c478bd9Sstevel@tonic-gate 				if (link(Over_p, nam_p) < 0) {
54687c478bd9Sstevel@tonic-gate 					msg(ERRN,
54697c478bd9Sstevel@tonic-gate 					    "Cannot recover original \"%s\"",
54707c478bd9Sstevel@tonic-gate 					    nam_p);
54717c478bd9Sstevel@tonic-gate 				}
54727c478bd9Sstevel@tonic-gate 				if (unlink(Over_p)) {
54737c478bd9Sstevel@tonic-gate 					msg(ERRN,
54747c478bd9Sstevel@tonic-gate 					    "Cannot remove temp file \"%s\"",
54757c478bd9Sstevel@tonic-gate 					    Over_p);
54767c478bd9Sstevel@tonic-gate 				}
54777c478bd9Sstevel@tonic-gate 			}
54787c478bd9Sstevel@tonic-gate 		} else if (unlink(nam_p))
54797c478bd9Sstevel@tonic-gate 			msg(ERRN,
54807c478bd9Sstevel@tonic-gate 			    "Cannot remove incomplete \"%s\"", nam_p);
54817c478bd9Sstevel@tonic-gate 			*Over_p = '\0';
54827c478bd9Sstevel@tonic-gate 	}
54837c478bd9Sstevel@tonic-gate 	exit(EXIT_CODE);
54847c478bd9Sstevel@tonic-gate }
54857c478bd9Sstevel@tonic-gate 
54867c478bd9Sstevel@tonic-gate /*
54877c478bd9Sstevel@tonic-gate  * swap: Swap bytes (-s), halfwords (-S) or or both halfwords and bytes (-b).
54887c478bd9Sstevel@tonic-gate  */
54897c478bd9Sstevel@tonic-gate 
54907c478bd9Sstevel@tonic-gate static void
54917c478bd9Sstevel@tonic-gate swap(char *buf_p, int cnt)
54927c478bd9Sstevel@tonic-gate {
54937c478bd9Sstevel@tonic-gate 	unsigned char tbyte;
54947c478bd9Sstevel@tonic-gate 	int tcnt;
54957c478bd9Sstevel@tonic-gate 	int rcnt;
54967c478bd9Sstevel@tonic-gate 	ushort_t thalf;
54977c478bd9Sstevel@tonic-gate 
54987c478bd9Sstevel@tonic-gate 	rcnt = cnt % 4;
54997c478bd9Sstevel@tonic-gate 	cnt /= 4;
55007c478bd9Sstevel@tonic-gate 	if (Args & (OCb | OCs | BSM)) {
55017c478bd9Sstevel@tonic-gate 		tcnt = cnt;
55027c478bd9Sstevel@tonic-gate 		/* LINTED alignment */
55037c478bd9Sstevel@tonic-gate 		Swp_p = (union swpbuf *)buf_p;
55047c478bd9Sstevel@tonic-gate 		while (tcnt-- > 0) {
55057c478bd9Sstevel@tonic-gate 			tbyte = Swp_p->s_byte[0];
55067c478bd9Sstevel@tonic-gate 			Swp_p->s_byte[0] = Swp_p->s_byte[1];
55077c478bd9Sstevel@tonic-gate 			Swp_p->s_byte[1] = tbyte;
55087c478bd9Sstevel@tonic-gate 			tbyte = Swp_p->s_byte[2];
55097c478bd9Sstevel@tonic-gate 			Swp_p->s_byte[2] = Swp_p->s_byte[3];
55107c478bd9Sstevel@tonic-gate 			Swp_p->s_byte[3] = tbyte;
55117c478bd9Sstevel@tonic-gate 			Swp_p++;
55127c478bd9Sstevel@tonic-gate 		}
55137c478bd9Sstevel@tonic-gate 		if (rcnt >= 2) {
55147c478bd9Sstevel@tonic-gate 		tbyte = Swp_p->s_byte[0];
55157c478bd9Sstevel@tonic-gate 		Swp_p->s_byte[0] = Swp_p->s_byte[1];
55167c478bd9Sstevel@tonic-gate 		Swp_p->s_byte[1] = tbyte;
55177c478bd9Sstevel@tonic-gate 		tbyte = Swp_p->s_byte[2];
55187c478bd9Sstevel@tonic-gate 		}
55197c478bd9Sstevel@tonic-gate 	}
55207c478bd9Sstevel@tonic-gate 	if (Args & (OCb | OCS)) {
55217c478bd9Sstevel@tonic-gate 		tcnt = cnt;
55227c478bd9Sstevel@tonic-gate 		/* LINTED alignment */
55237c478bd9Sstevel@tonic-gate 		Swp_p = (union swpbuf *)buf_p;
55247c478bd9Sstevel@tonic-gate 		while (tcnt-- > 0) {
55257c478bd9Sstevel@tonic-gate 			thalf = Swp_p->s_half[0];
55267c478bd9Sstevel@tonic-gate 			Swp_p->s_half[0] = Swp_p->s_half[1];
55277c478bd9Sstevel@tonic-gate 			Swp_p->s_half[1] = thalf;
55287c478bd9Sstevel@tonic-gate 			Swp_p++;
55297c478bd9Sstevel@tonic-gate 		}
55307c478bd9Sstevel@tonic-gate 	}
55317c478bd9Sstevel@tonic-gate }
55327c478bd9Sstevel@tonic-gate 
55337c478bd9Sstevel@tonic-gate /*
55347c478bd9Sstevel@tonic-gate  * usage: Print the usage message on stderr and exit.
55357c478bd9Sstevel@tonic-gate  */
55367c478bd9Sstevel@tonic-gate 
55377c478bd9Sstevel@tonic-gate static void
55387c478bd9Sstevel@tonic-gate usage(void)
55397c478bd9Sstevel@tonic-gate {
55407c478bd9Sstevel@tonic-gate 
55417c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
55427c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
55437c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("USAGE:\n"
55447c478bd9Sstevel@tonic-gate 	    "\tcpio -i[bcdfkmrstuv@BSV6] [-C size] "
55457c478bd9Sstevel@tonic-gate 	    "[-E file] [-H hdr] [-I file [-M msg]] "
55467c478bd9Sstevel@tonic-gate 	    "[-R id] [patterns]\n"
55477c478bd9Sstevel@tonic-gate 	    "\tcpio -o[acv@ABLV] [-C size] "
55487c478bd9Sstevel@tonic-gate 	    "[-H hdr] [-O file [-M msg]]\n"
55497c478bd9Sstevel@tonic-gate 	    "\tcpio -p[adlmuv@LV] [-R id] directory\n"));
55507c478bd9Sstevel@tonic-gate #else
55517c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("USAGE:\n"
55527c478bd9Sstevel@tonic-gate 	    "\tcpio -i[bcdfkmrstuvBSV6] [-C size] "
55537c478bd9Sstevel@tonic-gate 	    "[-E file] [-H hdr] [-I file [-M msg]] "
55547c478bd9Sstevel@tonic-gate 	    "[-R id] [patterns]\n"
55557c478bd9Sstevel@tonic-gate 	    "\tcpio -o[acvABLV] [-C size] "
55567c478bd9Sstevel@tonic-gate 	    "[-H hdr] [-O file [-M msg]]\n"
55577c478bd9Sstevel@tonic-gate 	    "\tcpio -p[adlmuvLV] [-R id] directory\n"));
55587c478bd9Sstevel@tonic-gate #endif
55597c478bd9Sstevel@tonic-gate 	(void) fflush(stderr);
55607c478bd9Sstevel@tonic-gate 	exit(EXIT_CODE);
55617c478bd9Sstevel@tonic-gate }
55627c478bd9Sstevel@tonic-gate 
55637c478bd9Sstevel@tonic-gate /*
55647c478bd9Sstevel@tonic-gate  * verbose: For each file, print either the filename (-v) or a dot (-V).
55657c478bd9Sstevel@tonic-gate  * If the -t option (table of contents) is set, print either the filename,
55667c478bd9Sstevel@tonic-gate  * or if the -v option is also set, print an "ls -l"-like listing.
55677c478bd9Sstevel@tonic-gate  */
55687c478bd9Sstevel@tonic-gate 
55697c478bd9Sstevel@tonic-gate static void
55707c478bd9Sstevel@tonic-gate verbose(char *nam_p)
55717c478bd9Sstevel@tonic-gate {
55727c478bd9Sstevel@tonic-gate 	int i, j, temp;
55737c478bd9Sstevel@tonic-gate 	mode_t mode;
55747c478bd9Sstevel@tonic-gate 	char modestr[12];
55757c478bd9Sstevel@tonic-gate 
55767c478bd9Sstevel@tonic-gate 	/*
55777c478bd9Sstevel@tonic-gate 	 * The printf format and associated arguments to print the current
55787c478bd9Sstevel@tonic-gate 	 * filename.  Normally, just nam_p.  If we're processing an extended
55797c478bd9Sstevel@tonic-gate 	 * attribute, these are overridden.
55807c478bd9Sstevel@tonic-gate 	 */
55817c478bd9Sstevel@tonic-gate 	char *name_fmt = "%s";
55827c478bd9Sstevel@tonic-gate 	const char *name = nam_p;
55837c478bd9Sstevel@tonic-gate 	const char *attribute = NULL;
55847c478bd9Sstevel@tonic-gate 
55857c478bd9Sstevel@tonic-gate 	if (Gen.g_attrnam_p != (char *)NULL) {
55867c478bd9Sstevel@tonic-gate 		/*
55877c478bd9Sstevel@tonic-gate 		 * Translation note:
55887c478bd9Sstevel@tonic-gate 		 * 'attribute' is a noun.
55897c478bd9Sstevel@tonic-gate 		 */
5590*da6c28aaSamw 
5591*da6c28aaSamw 		if (is_sysattr(basename(Gen.g_attrnam_p))) {
5592*da6c28aaSamw 			name_fmt = gettext("%s system attribute %s");
5593*da6c28aaSamw 		} else {
55947c478bd9Sstevel@tonic-gate 			name_fmt = gettext("%s attribute %s");
5595*da6c28aaSamw 		}
55967c478bd9Sstevel@tonic-gate 
55977c478bd9Sstevel@tonic-gate 		name = (Args & OCp) ? nam_p : Gen.g_attrfnam_p;
55987c478bd9Sstevel@tonic-gate 		attribute = Gen.g_attrnam_p;
55997c478bd9Sstevel@tonic-gate 	}
56007c478bd9Sstevel@tonic-gate 
56017c478bd9Sstevel@tonic-gate 	if ((Gen.g_mode == SECMODE) || ((Hdr_type == USTAR ||
56027c478bd9Sstevel@tonic-gate 	    Hdr_type == TAR) && Thdr_p->tbuf.t_typeflag == 'A')) {
56037c478bd9Sstevel@tonic-gate 		/* dont print ancillary file */
56047c478bd9Sstevel@tonic-gate 		aclchar = '+';
56057c478bd9Sstevel@tonic-gate 		return;
56067c478bd9Sstevel@tonic-gate 	}
56077c478bd9Sstevel@tonic-gate 	for (i = 0; i < 11; i++)
56087c478bd9Sstevel@tonic-gate 		modestr[i] = '-';
56097c478bd9Sstevel@tonic-gate 	modestr[i] = '\0';
56107c478bd9Sstevel@tonic-gate 	modestr[i-1] = aclchar;
56117c478bd9Sstevel@tonic-gate 	aclchar = ' ';
56127c478bd9Sstevel@tonic-gate 
56137c478bd9Sstevel@tonic-gate 	if ((Args & OCt) && (Args & OCv)) {
56147c478bd9Sstevel@tonic-gate 		mode = Gen.g_mode;
56157c478bd9Sstevel@tonic-gate 		for (i = 0; i < 3; i++) {
56167c478bd9Sstevel@tonic-gate 			temp = (mode >> (6 - (i * 3)));
56177c478bd9Sstevel@tonic-gate 			j = (i * 3) + 1;
56187c478bd9Sstevel@tonic-gate 			if (S_IROTH & temp)
56197c478bd9Sstevel@tonic-gate 				modestr[j] = 'r';
56207c478bd9Sstevel@tonic-gate 			if (S_IWOTH & temp)
56217c478bd9Sstevel@tonic-gate 				modestr[j + 1] = 'w';
56227c478bd9Sstevel@tonic-gate 			if (S_IXOTH & temp)
56237c478bd9Sstevel@tonic-gate 				modestr[j + 2] = 'x';
56247c478bd9Sstevel@tonic-gate 		}
56257c478bd9Sstevel@tonic-gate 
56267c478bd9Sstevel@tonic-gate 		if (Hdr_type != BAR) {
56277c478bd9Sstevel@tonic-gate 			temp = Gen.g_mode & Ftype;
56287c478bd9Sstevel@tonic-gate 			switch (temp) {
56297c478bd9Sstevel@tonic-gate 			case (S_IFIFO):
56307c478bd9Sstevel@tonic-gate 				modestr[0] = 'p';
56317c478bd9Sstevel@tonic-gate 				break;
56327c478bd9Sstevel@tonic-gate 			case (S_IFCHR):
56337c478bd9Sstevel@tonic-gate 				modestr[0] = 'c';
56347c478bd9Sstevel@tonic-gate 				break;
56357c478bd9Sstevel@tonic-gate 			case (S_IFDIR):
56367c478bd9Sstevel@tonic-gate 				modestr[0] = 'd';
56377c478bd9Sstevel@tonic-gate 				break;
56387c478bd9Sstevel@tonic-gate 			case (S_IFBLK):
56397c478bd9Sstevel@tonic-gate 				modestr[0] = 'b';
56407c478bd9Sstevel@tonic-gate 				break;
56417c478bd9Sstevel@tonic-gate 			case (S_IFREG): /* was initialized to '-' */
56427c478bd9Sstevel@tonic-gate 				break;
56437c478bd9Sstevel@tonic-gate 			case (S_IFLNK):
56447c478bd9Sstevel@tonic-gate 				modestr[0] = 'l';
56457c478bd9Sstevel@tonic-gate 				break;
56467c478bd9Sstevel@tonic-gate 			default:
56477c478bd9Sstevel@tonic-gate 				msg(ERR, "Impossible file type");
56487c478bd9Sstevel@tonic-gate 			}
56497c478bd9Sstevel@tonic-gate 		} else {		/* bar */
56507c478bd9Sstevel@tonic-gate 			temp = Gen.g_mode & Ftype;
56517c478bd9Sstevel@tonic-gate 			switch (temp) {
56527c478bd9Sstevel@tonic-gate 			case (S_IFIFO):
56537c478bd9Sstevel@tonic-gate 				modestr[0] = 'p';
56547c478bd9Sstevel@tonic-gate 				break;
56557c478bd9Sstevel@tonic-gate 			case (S_IFCHR):
56567c478bd9Sstevel@tonic-gate 				modestr[0] = 'c';
56577c478bd9Sstevel@tonic-gate 				break;
56587c478bd9Sstevel@tonic-gate 			case (S_IFDIR):
56597c478bd9Sstevel@tonic-gate 				modestr[0] = 'd';
56607c478bd9Sstevel@tonic-gate 				break;
56617c478bd9Sstevel@tonic-gate 			case (S_IFBLK):
56627c478bd9Sstevel@tonic-gate 				modestr[0] = 'b';
56637c478bd9Sstevel@tonic-gate 				break;
56647c478bd9Sstevel@tonic-gate 			}
56657c478bd9Sstevel@tonic-gate 			if (bar_linkflag == SYMTYPE)
56667c478bd9Sstevel@tonic-gate 				modestr[0] = 'l';
56677c478bd9Sstevel@tonic-gate 		}
56687c478bd9Sstevel@tonic-gate 		if ((S_ISUID & Gen.g_mode) == S_ISUID)
56697c478bd9Sstevel@tonic-gate 			modestr[3] = 's';
56707c478bd9Sstevel@tonic-gate 		if ((S_ISVTX & Gen.g_mode) == S_ISVTX)
56717c478bd9Sstevel@tonic-gate 			modestr[9] = 't';
56727c478bd9Sstevel@tonic-gate 		if ((S_ISGID & G_p->g_mode) == S_ISGID && modestr[6] == 'x')
56737c478bd9Sstevel@tonic-gate 			modestr[6] = 's';
56747c478bd9Sstevel@tonic-gate 		else if ((S_ENFMT & Gen.g_mode) == S_ENFMT && modestr[6] != 'x')
56757c478bd9Sstevel@tonic-gate 			modestr[6] = 'l';
56767c478bd9Sstevel@tonic-gate 		if ((Hdr_type == TAR || Hdr_type == USTAR) && Gen.g_nlink == 0)
56777c478bd9Sstevel@tonic-gate 			(void) printf("%s%4d ", modestr, (int)Gen.g_nlink+1);
56787c478bd9Sstevel@tonic-gate 		else
56797c478bd9Sstevel@tonic-gate 			(void) printf("%s%4d ", modestr, (int)Gen.g_nlink);
56807c478bd9Sstevel@tonic-gate 		if (Lastuid == (int)Gen.g_uid) {
56817c478bd9Sstevel@tonic-gate 			if (Lastuid == -1)
56827c478bd9Sstevel@tonic-gate 				(void) printf("-1       ");
56837c478bd9Sstevel@tonic-gate 			else
56847c478bd9Sstevel@tonic-gate 				(void) printf("%-9s", Curpw_p->pw_name);
56857c478bd9Sstevel@tonic-gate 		} else {
56867c478bd9Sstevel@tonic-gate 			if (Curpw_p = getpwuid((int)Gen.g_uid)) {
56877c478bd9Sstevel@tonic-gate 				(void) printf("%-9s", Curpw_p->pw_name);
56887c478bd9Sstevel@tonic-gate 				Lastuid = (int)Gen.g_uid;
56897c478bd9Sstevel@tonic-gate 			} else {
56907c478bd9Sstevel@tonic-gate 				(void) printf("%-9d", (int)Gen.g_uid);
56917c478bd9Sstevel@tonic-gate 				Lastuid = -1;
56927c478bd9Sstevel@tonic-gate 			}
56937c478bd9Sstevel@tonic-gate 		}
56947c478bd9Sstevel@tonic-gate 		if (Lastgid == (int)Gen.g_gid) {
56957c478bd9Sstevel@tonic-gate 			if (Lastgid == -1)
56967c478bd9Sstevel@tonic-gate 				(void) printf("-1       ");
56977c478bd9Sstevel@tonic-gate 			else
56987c478bd9Sstevel@tonic-gate 				(void) printf("%-9s", Curgr_p->gr_name);
56997c478bd9Sstevel@tonic-gate 		} else {
57007c478bd9Sstevel@tonic-gate 			if (Curgr_p = getgrgid((int)Gen.g_gid)) {
57017c478bd9Sstevel@tonic-gate 				(void) printf("%-9s", Curgr_p->gr_name);
57027c478bd9Sstevel@tonic-gate 				Lastgid = (int)Gen.g_gid;
57037c478bd9Sstevel@tonic-gate 			} else {
57047c478bd9Sstevel@tonic-gate 				(void) printf("%-9d", (int)Gen.g_gid);
57057c478bd9Sstevel@tonic-gate 				Lastgid = -1;
57067c478bd9Sstevel@tonic-gate 			}
57077c478bd9Sstevel@tonic-gate 		}
57087c478bd9Sstevel@tonic-gate 
57097c478bd9Sstevel@tonic-gate 		/* print file size */
57107c478bd9Sstevel@tonic-gate 		if (!Aspec || ((Gen.g_mode & Ftype) == S_IFIFO) ||
57117c478bd9Sstevel@tonic-gate 		    (Hdr_type == BAR && bar_linkflag == SYMTYPE)) {
57127c478bd9Sstevel@tonic-gate 			if (Gen.g_filesz < (1LL << 31))
57137c478bd9Sstevel@tonic-gate 				(void) printf("%7lld ",
57147c478bd9Sstevel@tonic-gate 				    (offset_t)Gen.g_filesz);
57157c478bd9Sstevel@tonic-gate 			else
57167c478bd9Sstevel@tonic-gate 				(void) printf("%11lld ",
57177c478bd9Sstevel@tonic-gate 				    (offset_t)Gen.g_filesz);
57187c478bd9Sstevel@tonic-gate 		} else
57197c478bd9Sstevel@tonic-gate 			(void) printf("%3d,%3d ", (int)major(Gen.g_rdev),
57207c478bd9Sstevel@tonic-gate 			    (int)minor(Gen.g_rdev));
57217c478bd9Sstevel@tonic-gate 		(void) cftime(Time, dcgettext(NULL, FORMAT, LC_TIME),
57227c478bd9Sstevel@tonic-gate 		    (time_t *)&Gen.g_mtime);
57237c478bd9Sstevel@tonic-gate 		(void) printf("%s, ", Time);
57247c478bd9Sstevel@tonic-gate 		(void) printf(name_fmt, name, attribute);
57257c478bd9Sstevel@tonic-gate 		if ((Gen.g_mode & Ftype) == S_IFLNK) {
57267c478bd9Sstevel@tonic-gate 			if (Hdr_type == USTAR || Hdr_type == TAR)
57277c478bd9Sstevel@tonic-gate 				(void) strcpy(Symlnk_p,
57287c478bd9Sstevel@tonic-gate 				    Thdr_p->tbuf.t_linkname);
57297c478bd9Sstevel@tonic-gate 			else {
57307c478bd9Sstevel@tonic-gate 				(void) strncpy(Symlnk_p, Buffr.b_out_p,
57317c478bd9Sstevel@tonic-gate 				    Gen.g_filesz);
57327c478bd9Sstevel@tonic-gate 				*(Symlnk_p + Gen.g_filesz) = '\0';
57337c478bd9Sstevel@tonic-gate 			}
57347c478bd9Sstevel@tonic-gate 			(void) printf(" -> %s", Symlnk_p);
57357c478bd9Sstevel@tonic-gate 		}
57367c478bd9Sstevel@tonic-gate 		if (Hdr_type == BAR) {
57377c478bd9Sstevel@tonic-gate 			if (bar_linkflag == SYMTYPE)
57387c478bd9Sstevel@tonic-gate 				(void) printf(gettext(" symbolic link to %s"),
57397c478bd9Sstevel@tonic-gate 				    bar_linkname);
57407c478bd9Sstevel@tonic-gate 			else if (bar_linkflag == '1')
57417c478bd9Sstevel@tonic-gate 				(void) printf(gettext(" linked to %s"),
57427c478bd9Sstevel@tonic-gate 				    bar_linkname);
57437c478bd9Sstevel@tonic-gate 		}
57447c478bd9Sstevel@tonic-gate 		if ((Hdr_type == USTAR || Hdr_type == TAR) &&
57457c478bd9Sstevel@tonic-gate 		    Thdr_p->tbuf.t_typeflag == '1') {
57467c478bd9Sstevel@tonic-gate 			(void) printf(gettext(" linked to %s%s%s"),
57477c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
57487c478bd9Sstevel@tonic-gate 			    Thdr_p->tbuf.t_linkname : Gen.g_attrfnam_p,
57497c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
57507c478bd9Sstevel@tonic-gate 			    "" : gettext(" attribute "),
57517c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
57527c478bd9Sstevel@tonic-gate 			    "" : Gen.g_linktoattrnam_p);
57537c478bd9Sstevel@tonic-gate 		}
57547c478bd9Sstevel@tonic-gate 		(void) printf("\n");
57557c478bd9Sstevel@tonic-gate 	} else if ((Args & OCt) || (Args & OCv)) {
57567c478bd9Sstevel@tonic-gate 		(void) fprintf(Out_p, name_fmt, name, attribute);
57577c478bd9Sstevel@tonic-gate 		(void) fputc('\n', Out_p);
57587c478bd9Sstevel@tonic-gate 	} else { /* OCV */
57597c478bd9Sstevel@tonic-gate 		(void) fputc('.', Out_p);
57607c478bd9Sstevel@tonic-gate 		if (Verbcnt++ >= 49) { /* start a new line of dots */
57617c478bd9Sstevel@tonic-gate 			Verbcnt = 0;
57627c478bd9Sstevel@tonic-gate 			(void) fputc('\n', Out_p);
57637c478bd9Sstevel@tonic-gate 		}
57647c478bd9Sstevel@tonic-gate 	}
57657c478bd9Sstevel@tonic-gate 	(void) fflush(Out_p);
57667c478bd9Sstevel@tonic-gate }
57677c478bd9Sstevel@tonic-gate 
57687c478bd9Sstevel@tonic-gate #define	MK_USHORT(a)	(a & 00000177777)
57697c478bd9Sstevel@tonic-gate 
57707c478bd9Sstevel@tonic-gate /*
57717c478bd9Sstevel@tonic-gate  * write_hdr: Transfer header information for the generic structure
57727c478bd9Sstevel@tonic-gate  * into the format for the selected header and bwrite() the header.
57737c478bd9Sstevel@tonic-gate  * ACL support: add two new argumnets. secflag indicates that it's an
57747c478bd9Sstevel@tonic-gate  *	ancillary file. len is the size of the file (incl. all security
57757c478bd9Sstevel@tonic-gate  *	attributes). We only have acls now.
57767c478bd9Sstevel@tonic-gate  */
57777c478bd9Sstevel@tonic-gate 
57787c478bd9Sstevel@tonic-gate static void
57797c478bd9Sstevel@tonic-gate write_hdr(int secflag, off_t len)
57807c478bd9Sstevel@tonic-gate {
57817c478bd9Sstevel@tonic-gate 	int cnt, pad;
57827c478bd9Sstevel@tonic-gate 	mode_t mode;
57837c478bd9Sstevel@tonic-gate 	uid_t uid;
57847c478bd9Sstevel@tonic-gate 	gid_t gid;
57857c478bd9Sstevel@tonic-gate 	const char warnfmt[] = "%s%s%s : %s";
57867c478bd9Sstevel@tonic-gate 
57877c478bd9Sstevel@tonic-gate 	if (secflag == ARCHIVE_ACL) {
57887c478bd9Sstevel@tonic-gate 		mode = SECMODE;
57897c478bd9Sstevel@tonic-gate 	} else {
57907c478bd9Sstevel@tonic-gate 		/*
57917c478bd9Sstevel@tonic-gate 		 * If attribute is being archived in cpio format then
57927c478bd9Sstevel@tonic-gate 		 * zap off the file type bits since those are truly a
57937c478bd9Sstevel@tonic-gate 		 * mask and reset them with _XATTR_CPIO_MODE
57947c478bd9Sstevel@tonic-gate 		 */
57957c478bd9Sstevel@tonic-gate 
57967c478bd9Sstevel@tonic-gate 		/*
57977c478bd9Sstevel@tonic-gate 		 * len is the value of g_filesz for normal files
57987c478bd9Sstevel@tonic-gate 		 * and the length of the special header buffer in
57997c478bd9Sstevel@tonic-gate 		 * the case of acl and xattr headers.
58007c478bd9Sstevel@tonic-gate 		 */
58017c478bd9Sstevel@tonic-gate 		if (G_p->g_attrnam_p != (char *)NULL && Hdr_type != USTAR &&
58027c478bd9Sstevel@tonic-gate 		    Hdr_type != TAR) {
5803d2443e76Smarks 			mode = (G_p->g_mode & POSIXMODES) | _XATTR_CPIO_MODE;
58047c478bd9Sstevel@tonic-gate 		} else {
58057c478bd9Sstevel@tonic-gate 			mode = G_p->g_mode;
58067c478bd9Sstevel@tonic-gate 		}
58077c478bd9Sstevel@tonic-gate 		if (secflag != ARCHIVE_XATTR) {
58087c478bd9Sstevel@tonic-gate 			len = G_p->g_filesz;
58097c478bd9Sstevel@tonic-gate 		}
58107c478bd9Sstevel@tonic-gate 	}
58117c478bd9Sstevel@tonic-gate 
58127c478bd9Sstevel@tonic-gate 	uid = G_p->g_uid;
58137c478bd9Sstevel@tonic-gate 	gid = G_p->g_gid;
58147c478bd9Sstevel@tonic-gate 	/*
58157c478bd9Sstevel@tonic-gate 	 * Handle EFT uids and gids.  If they get too big
58167c478bd9Sstevel@tonic-gate 	 * to be represented in a particular format, force 'em to 'nobody'.
58177c478bd9Sstevel@tonic-gate 	 */
58187c478bd9Sstevel@tonic-gate 	switch (Hdr_type) {
58197c478bd9Sstevel@tonic-gate 	case BIN:			/* 16-bits of u_short */
58207c478bd9Sstevel@tonic-gate 		if ((ulong_t)uid > (ulong_t)USHRT_MAX)
58217c478bd9Sstevel@tonic-gate 			uid = UID_NOBODY;
58227c478bd9Sstevel@tonic-gate 		if ((ulong_t)gid > (ulong_t)USHRT_MAX)
58237c478bd9Sstevel@tonic-gate 			gid = GID_NOBODY;
58247c478bd9Sstevel@tonic-gate 		break;
58257c478bd9Sstevel@tonic-gate 	case CHR:			/* %.6lo => 262143 base 10 */
58267c478bd9Sstevel@tonic-gate 		if ((ulong_t)uid > (ulong_t)0777777)
58277c478bd9Sstevel@tonic-gate 			uid = UID_NOBODY;
58287c478bd9Sstevel@tonic-gate 		if ((ulong_t)gid > (ulong_t)0777777)
58297c478bd9Sstevel@tonic-gate 			gid = GID_NOBODY;
58307c478bd9Sstevel@tonic-gate 		break;
58317c478bd9Sstevel@tonic-gate 	case ASC:			/* %.8lx => full 32 bits */
58327c478bd9Sstevel@tonic-gate 	case CRC:
58337c478bd9Sstevel@tonic-gate 		break;
58347c478bd9Sstevel@tonic-gate 	case USTAR:
58357c478bd9Sstevel@tonic-gate 	case TAR:			/* %.7lo => 2097151 base 10 */
58367c478bd9Sstevel@tonic-gate 		if ((ulong_t)uid > (ulong_t)07777777)
58377c478bd9Sstevel@tonic-gate 			uid = UID_NOBODY;
58387c478bd9Sstevel@tonic-gate 		if ((ulong_t)gid > (ulong_t)07777777)
58397c478bd9Sstevel@tonic-gate 			gid = GID_NOBODY;
58407c478bd9Sstevel@tonic-gate 		break;
58417c478bd9Sstevel@tonic-gate 	default:
58427c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
58437c478bd9Sstevel@tonic-gate 	}
58447c478bd9Sstevel@tonic-gate 
58457c478bd9Sstevel@tonic-gate 	/*
58467c478bd9Sstevel@tonic-gate 	 * Since cpio formats -don't- encode the symbolic names, print
58477c478bd9Sstevel@tonic-gate 	 * a warning message when we map the uid or gid this way.
58487c478bd9Sstevel@tonic-gate 	 * Also, if the ownership just changed, clear set[ug]id bits
58497c478bd9Sstevel@tonic-gate 	 *
58507c478bd9Sstevel@tonic-gate 	 * (Except for USTAR format of course, where we have a string
58517c478bd9Sstevel@tonic-gate 	 * representation of the username embedded in the header)
58527c478bd9Sstevel@tonic-gate 	 */
58537c478bd9Sstevel@tonic-gate 	if (uid != G_p->g_uid && Hdr_type != USTAR) {
58547c478bd9Sstevel@tonic-gate 		msg(ERR, warnfmt,
58557c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
58567c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p : G_p->g_attrfnam_p,
58577c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
58587c478bd9Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
58597c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
58607c478bd9Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p,
58617c478bd9Sstevel@tonic-gate 		    gettext("uid too large for archive format"));
58627c478bd9Sstevel@tonic-gate 		if (S_ISREG(mode))
58637c478bd9Sstevel@tonic-gate 			mode &= ~S_ISUID;
58647c478bd9Sstevel@tonic-gate 	}
58657c478bd9Sstevel@tonic-gate 	if (gid != G_p->g_gid && Hdr_type != USTAR) {
58667c478bd9Sstevel@tonic-gate 		msg(ERR, warnfmt,
58677c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
58687c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p : G_p->g_attrfnam_p,
58697c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
58707c478bd9Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
58717c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
58727c478bd9Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p,
58737c478bd9Sstevel@tonic-gate 		    gettext("gid too large for archive format"));
58747c478bd9Sstevel@tonic-gate 		if (S_ISREG(mode))
58757c478bd9Sstevel@tonic-gate 			mode &= ~S_ISGID;
58767c478bd9Sstevel@tonic-gate 	}
58777c478bd9Sstevel@tonic-gate 
58787c478bd9Sstevel@tonic-gate 	switch (Hdr_type) {
58797c478bd9Sstevel@tonic-gate 	case BIN:
58807c478bd9Sstevel@tonic-gate 	case CHR:
58817c478bd9Sstevel@tonic-gate 	case ASC:
58827c478bd9Sstevel@tonic-gate 	case CRC:
58837c478bd9Sstevel@tonic-gate 		cnt = Hdrsz + G_p->g_namesz;
58847c478bd9Sstevel@tonic-gate 		break;
58857c478bd9Sstevel@tonic-gate 	case TAR:
58867c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
58877c478bd9Sstevel@tonic-gate 	case USTAR: /* TAR and USTAR */
58887c478bd9Sstevel@tonic-gate 		cnt = TARSZ;
58897c478bd9Sstevel@tonic-gate 		break;
58907c478bd9Sstevel@tonic-gate 	default:
58917c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
58927c478bd9Sstevel@tonic-gate 	}
58937c478bd9Sstevel@tonic-gate 	FLUSH(cnt);
58947c478bd9Sstevel@tonic-gate 
58957c478bd9Sstevel@tonic-gate 	switch (Hdr_type) {
58967c478bd9Sstevel@tonic-gate 	case BIN:
58977c478bd9Sstevel@tonic-gate 		Hdr.h_magic = (short)G_p->g_magic;
58987c478bd9Sstevel@tonic-gate 		Hdr.h_dev = G_p->g_dev;
58997c478bd9Sstevel@tonic-gate 		Hdr.h_ino = G_p->g_ino;
59007c478bd9Sstevel@tonic-gate 		Hdr.h_uid = uid;
59017c478bd9Sstevel@tonic-gate 		Hdr.h_gid = gid;
59027c478bd9Sstevel@tonic-gate 		Hdr.h_mode = mode;
59037c478bd9Sstevel@tonic-gate 		Hdr.h_nlink = G_p->g_nlink;
59047c478bd9Sstevel@tonic-gate 		Hdr.h_rdev = G_p->g_rdev;
59057c478bd9Sstevel@tonic-gate 		mkshort(Hdr.h_mtime, (long)G_p->g_mtime);
59067c478bd9Sstevel@tonic-gate 		Hdr.h_namesize = (short)G_p->g_namesz;
59077c478bd9Sstevel@tonic-gate 		mkshort(Hdr.h_filesize, (long)len);
59087c478bd9Sstevel@tonic-gate 		(void) strcpy(Hdr.h_name, G_p->g_nam_p);
59097c478bd9Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, &Hdr, cnt);
59107c478bd9Sstevel@tonic-gate 		break;
59117c478bd9Sstevel@tonic-gate 	case CHR:
59127c478bd9Sstevel@tonic-gate 		(void) sprintf(Buffr.b_in_p,
59137c478bd9Sstevel@tonic-gate 		    "%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.11lo%.6lo%."
59147c478bd9Sstevel@tonic-gate 		    "11llo%s", G_p->g_magic, G_p->g_dev, G_p->g_ino, mode,
59157c478bd9Sstevel@tonic-gate 		    uid, gid, G_p->g_nlink, MK_USHORT(G_p->g_rdev),
59167c478bd9Sstevel@tonic-gate 		    G_p->g_mtime, (long)G_p->g_namesz, (offset_t)len,
59177c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p);
59187c478bd9Sstevel@tonic-gate 		break;
59197c478bd9Sstevel@tonic-gate 	case ASC:
59207c478bd9Sstevel@tonic-gate 	case CRC:
59217c478bd9Sstevel@tonic-gate 		(void) sprintf(Buffr.b_in_p,
59227c478bd9Sstevel@tonic-gate 		    "%.6lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%."
59237c478bd9Sstevel@tonic-gate 		    "8lx%.8lx%.8lx%.8lx%s",
59247c478bd9Sstevel@tonic-gate 		    G_p->g_magic, G_p->g_ino, mode, G_p->g_uid,
5925944d83a0Schin 		    G_p->g_gid, G_p->g_nlink, G_p->g_mtime, (ulong_t)len,
59267c478bd9Sstevel@tonic-gate 		    major(G_p->g_dev), minor(G_p->g_dev),
59277c478bd9Sstevel@tonic-gate 		    major(G_p->g_rdev), minor(G_p->g_rdev),
59287c478bd9Sstevel@tonic-gate 		    G_p->g_namesz, G_p->g_cksum, G_p->g_nam_p);
59297c478bd9Sstevel@tonic-gate 		break;
59307c478bd9Sstevel@tonic-gate 	case USTAR:
59317c478bd9Sstevel@tonic-gate 		Thdr_p = (union tblock *)Buffr.b_in_p;
59327c478bd9Sstevel@tonic-gate 		(void) memcpy(Thdr_p, Empty, TARSZ);
59337c478bd9Sstevel@tonic-gate 		(void) strncpy(Thdr_p->tbuf.t_name, G_p->g_tname,
59347c478bd9Sstevel@tonic-gate 		    (int)strlen(G_p->g_tname));
59357c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mode, "%07o", (int)mode);
59367c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_uid, "%07o", (int)uid);
59377c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_gid, "%07o", (int)gid);
59387c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_size, "%011llo",
59397c478bd9Sstevel@tonic-gate 		    (offset_t)len);
59407c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mtime, "%011lo", G_p->g_mtime);
59417c478bd9Sstevel@tonic-gate 		if (secflag == ARCHIVE_ACL) {
59427c478bd9Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = 'A';	/* ACL file type */
59437c478bd9Sstevel@tonic-gate 		} else if (secflag == ARCHIVE_XATTR ||
59447c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p != (char *)NULL)) {
59457c478bd9Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = _XATTR_HDRTYPE;
59467c478bd9Sstevel@tonic-gate 		} else {
59477c478bd9Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = G_p->g_typeflag;
59487c478bd9Sstevel@tonic-gate 		}
59497c478bd9Sstevel@tonic-gate 		if (T_lname[0] != '\0') {
59507c478bd9Sstevel@tonic-gate 			/*
59517c478bd9Sstevel@tonic-gate 			 * if not a symbolic link
59527c478bd9Sstevel@tonic-gate 			 */
59537c478bd9Sstevel@tonic-gate 			if (((G_p->g_mode & Ftype) != S_IFLNK) &&
59547c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL)) {
59557c478bd9Sstevel@tonic-gate 				Thdr_p->tbuf.t_typeflag = LNKTYPE;
59567c478bd9Sstevel@tonic-gate 				(void) sprintf(Thdr_p->tbuf.t_size,
59577c478bd9Sstevel@tonic-gate 				    "%011lo", 0L);
59587c478bd9Sstevel@tonic-gate 			}
59597c478bd9Sstevel@tonic-gate 			(void) strncpy(Thdr_p->tbuf.t_linkname, T_lname,
59607c478bd9Sstevel@tonic-gate 			    strlen(T_lname));
59617c478bd9Sstevel@tonic-gate 		}
59627c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_magic, "%s", TMAGIC);
59637c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_version, "%2s", TVERSION);
59647c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_uname, "%s",  G_p->g_uname);
59657c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_gname, "%s", G_p->g_gname);
59667c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_devmajor, "%07o",
59677c478bd9Sstevel@tonic-gate 		    (int)major(G_p->g_rdev));
59687c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_devminor, "%07o",
59697c478bd9Sstevel@tonic-gate 		    (int)minor(G_p->g_rdev));
59707c478bd9Sstevel@tonic-gate 		if (Gen.g_prefix) {
59717c478bd9Sstevel@tonic-gate 			(void) sprintf(Thdr_p->tbuf.t_prefix, "%s",
59727c478bd9Sstevel@tonic-gate 			    Gen.g_prefix);
59737c478bd9Sstevel@tonic-gate 			free(Gen.g_prefix);
59747c478bd9Sstevel@tonic-gate 			Gen.g_prefix = NULL;
59757c478bd9Sstevel@tonic-gate 		} else {
59767c478bd9Sstevel@tonic-gate 			(void) sprintf(Thdr_p->tbuf.t_prefix, "%s", "");
59777c478bd9Sstevel@tonic-gate 		}
59787c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_cksum, "%07o",
59797c478bd9Sstevel@tonic-gate 		    (int)cksum(TARTYP, 0, NULL));
59807c478bd9Sstevel@tonic-gate 		break;
59817c478bd9Sstevel@tonic-gate 	case TAR:
59827c478bd9Sstevel@tonic-gate 		Thdr_p = (union tblock *)Buffr.b_in_p;
59837c478bd9Sstevel@tonic-gate 		(void) memcpy(Thdr_p, Empty, TARSZ);
59847c478bd9Sstevel@tonic-gate 		(void) strncpy(Thdr_p->tbuf.t_name, G_p->g_nam_p,
59857c478bd9Sstevel@tonic-gate 		    G_p->g_namesz);
59867c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mode, "%07o ", (int)mode);
59877c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_uid, "%07o ", (int)uid);
59887c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_gid, "%07o ", (int)gid);
59897c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_size, "%011llo ",
59907c478bd9Sstevel@tonic-gate 		    (offset_t)len);
59917c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mtime, "%011o ",
59927c478bd9Sstevel@tonic-gate 		    (int)G_p->g_mtime);
59937c478bd9Sstevel@tonic-gate 		if (T_lname[0] != '\0') {
59947c478bd9Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = '1';
59957c478bd9Sstevel@tonic-gate 		} else {
59967c478bd9Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = '\0';
59977c478bd9Sstevel@tonic-gate 		}
59987c478bd9Sstevel@tonic-gate 		(void) strncpy(Thdr_p->tbuf.t_linkname, T_lname,
59997c478bd9Sstevel@tonic-gate 		    strlen(T_lname));
60007c478bd9Sstevel@tonic-gate 		break;
60017c478bd9Sstevel@tonic-gate 	default:
60027c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
60037c478bd9Sstevel@tonic-gate 	} /* Hdr_type */
60047c478bd9Sstevel@tonic-gate 
60057c478bd9Sstevel@tonic-gate 	Buffr.b_in_p += cnt;
60067c478bd9Sstevel@tonic-gate 	Buffr.b_cnt += cnt;
60077c478bd9Sstevel@tonic-gate 	pad = ((cnt + Pad_val) & ~Pad_val) - cnt;
60087c478bd9Sstevel@tonic-gate 	if (pad != 0) {
60097c478bd9Sstevel@tonic-gate 		FLUSH(pad);
60107c478bd9Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, Empty, pad);
60117c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += pad;
60127c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += pad;
60137c478bd9Sstevel@tonic-gate 	}
60147c478bd9Sstevel@tonic-gate }
60157c478bd9Sstevel@tonic-gate 
60167c478bd9Sstevel@tonic-gate /*
60177c478bd9Sstevel@tonic-gate  * write_trail: Create the appropriate trailer for the selected header type
60187c478bd9Sstevel@tonic-gate  * and bwrite the trailer.  Pad the buffer with nulls out to the next Bufsize
60197c478bd9Sstevel@tonic-gate  * boundary, and force a write.  If the write completes, or if the trailer is
60207c478bd9Sstevel@tonic-gate  * completely written (but not all of the padding nulls (as can happen on end
60217c478bd9Sstevel@tonic-gate  * of medium)) return.  Otherwise, the trailer was not completely written out,
60227c478bd9Sstevel@tonic-gate  * so re-pad the buffer with nulls and try again.
60237c478bd9Sstevel@tonic-gate  */
60247c478bd9Sstevel@tonic-gate 
60257c478bd9Sstevel@tonic-gate static void
60267c478bd9Sstevel@tonic-gate write_trail(void)
60277c478bd9Sstevel@tonic-gate {
60287c478bd9Sstevel@tonic-gate 	int cnt, need;
60297c478bd9Sstevel@tonic-gate 
60307c478bd9Sstevel@tonic-gate 	switch (Hdr_type) {
60317c478bd9Sstevel@tonic-gate 	case BIN:
60327c478bd9Sstevel@tonic-gate 		Gen.g_magic = CMN_BIN;
60337c478bd9Sstevel@tonic-gate 		break;
60347c478bd9Sstevel@tonic-gate 	case CHR:
60357c478bd9Sstevel@tonic-gate 		Gen.g_magic = CMN_BIN;
60367c478bd9Sstevel@tonic-gate 		break;
60377c478bd9Sstevel@tonic-gate 	case ASC:
60387c478bd9Sstevel@tonic-gate 		Gen.g_magic = CMN_ASC;
60397c478bd9Sstevel@tonic-gate 		break;
60407c478bd9Sstevel@tonic-gate 	case CRC:
60417c478bd9Sstevel@tonic-gate 		Gen.g_magic = CMN_CRC;
60427c478bd9Sstevel@tonic-gate 		break;
60437c478bd9Sstevel@tonic-gate 	}
60447c478bd9Sstevel@tonic-gate 
60457c478bd9Sstevel@tonic-gate 	switch (Hdr_type) {
60467c478bd9Sstevel@tonic-gate 	case BIN:
60477c478bd9Sstevel@tonic-gate 	case CHR:
60487c478bd9Sstevel@tonic-gate 	case ASC:
60497c478bd9Sstevel@tonic-gate 	case CRC:
60507c478bd9Sstevel@tonic-gate 		Gen.g_mode = Gen.g_uid = Gen.g_gid = 0;
60517c478bd9Sstevel@tonic-gate 		Gen.g_nlink = 1;
60527c478bd9Sstevel@tonic-gate 		Gen.g_mtime = Gen.g_ino = Gen.g_dev = 0;
60537c478bd9Sstevel@tonic-gate 		Gen.g_rdev = Gen.g_cksum = 0;
60547c478bd9Sstevel@tonic-gate 		Gen.g_filesz = (off_t)0;
60557c478bd9Sstevel@tonic-gate 		Gen.g_namesz = strlen("TRAILER!!!") + 1;
60567c478bd9Sstevel@tonic-gate 		(void) strcpy(Gen.g_nam_p, "TRAILER!!!");
60577c478bd9Sstevel@tonic-gate 		G_p = &Gen;
60587c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
60597c478bd9Sstevel@tonic-gate 		break;
60607c478bd9Sstevel@tonic-gate 	case TAR:
60617c478bd9Sstevel@tonic-gate 	/*FALLTHROUGH*/
60627c478bd9Sstevel@tonic-gate 	case USTAR: /* TAR and USTAR */
60637c478bd9Sstevel@tonic-gate 		for (cnt = 0; cnt < 3; cnt++) {
60647c478bd9Sstevel@tonic-gate 			FLUSH(TARSZ);
60657c478bd9Sstevel@tonic-gate 			(void) memcpy(Buffr.b_in_p, Empty, TARSZ);
60667c478bd9Sstevel@tonic-gate 			Buffr.b_in_p += TARSZ;
60677c478bd9Sstevel@tonic-gate 			Buffr.b_cnt += TARSZ;
60687c478bd9Sstevel@tonic-gate 		}
60697c478bd9Sstevel@tonic-gate 		break;
60707c478bd9Sstevel@tonic-gate 	default:
60717c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
60727c478bd9Sstevel@tonic-gate 	}
60737c478bd9Sstevel@tonic-gate 	need = Bufsize - (Buffr.b_cnt % Bufsize);
60747c478bd9Sstevel@tonic-gate 	if (need == Bufsize)
60757c478bd9Sstevel@tonic-gate 		need = 0;
60767c478bd9Sstevel@tonic-gate 
60777c478bd9Sstevel@tonic-gate 	while (Buffr.b_cnt > 0) {
60787c478bd9Sstevel@tonic-gate 		while (need > 0) {
60797c478bd9Sstevel@tonic-gate 			cnt = (need < TARSZ) ? need : TARSZ;
60807c478bd9Sstevel@tonic-gate 			need -= cnt;
60817c478bd9Sstevel@tonic-gate 			FLUSH(cnt);
60827c478bd9Sstevel@tonic-gate 			(void) memcpy(Buffr.b_in_p, Empty, cnt);
60837c478bd9Sstevel@tonic-gate 			Buffr.b_in_p += cnt;
60847c478bd9Sstevel@tonic-gate 			Buffr.b_cnt += cnt;
60857c478bd9Sstevel@tonic-gate 		}
60867c478bd9Sstevel@tonic-gate 		bflush();
60877c478bd9Sstevel@tonic-gate 	}
60887c478bd9Sstevel@tonic-gate }
60897c478bd9Sstevel@tonic-gate 
60907c478bd9Sstevel@tonic-gate /*
60917c478bd9Sstevel@tonic-gate  * if archives in USTAR format, check if typeflag == '5' for directories
60927c478bd9Sstevel@tonic-gate  */
60937c478bd9Sstevel@tonic-gate static int
60947c478bd9Sstevel@tonic-gate ustar_dir(void)
60957c478bd9Sstevel@tonic-gate {
60967c478bd9Sstevel@tonic-gate 	if (Hdr_type == USTAR || Hdr_type == TAR) {
60977c478bd9Sstevel@tonic-gate 		if (Thdr_p->tbuf.t_typeflag == '5')
60987c478bd9Sstevel@tonic-gate 			return (1);
60997c478bd9Sstevel@tonic-gate 	}
61007c478bd9Sstevel@tonic-gate 	return (0);
61017c478bd9Sstevel@tonic-gate }
61027c478bd9Sstevel@tonic-gate 
61037c478bd9Sstevel@tonic-gate /*
61047c478bd9Sstevel@tonic-gate  * if archives in USTAR format, check if typeflag == '3' || '4' || '6'
61057c478bd9Sstevel@tonic-gate  * for character, block, fifo special files
61067c478bd9Sstevel@tonic-gate  */
61077c478bd9Sstevel@tonic-gate static int
61087c478bd9Sstevel@tonic-gate ustar_spec(void)
61097c478bd9Sstevel@tonic-gate {
61107c478bd9Sstevel@tonic-gate 	int typeflag;
61117c478bd9Sstevel@tonic-gate 
61127c478bd9Sstevel@tonic-gate 	if (Hdr_type == USTAR || Hdr_type == TAR) {
61137c478bd9Sstevel@tonic-gate 		typeflag = Thdr_p->tbuf.t_typeflag;
61147c478bd9Sstevel@tonic-gate 		if (typeflag == '3' || typeflag == '4' || typeflag == '6')
61157c478bd9Sstevel@tonic-gate 			return (1);
61167c478bd9Sstevel@tonic-gate 	}
61177c478bd9Sstevel@tonic-gate 	return (0);
61187c478bd9Sstevel@tonic-gate }
61197c478bd9Sstevel@tonic-gate 
61207c478bd9Sstevel@tonic-gate /*
61217c478bd9Sstevel@tonic-gate  * The return value is a pointer to a converted copy of the information in
61227c478bd9Sstevel@tonic-gate  * FromStat if the file is representable in -Hodc format, and NULL otherwise.
61237c478bd9Sstevel@tonic-gate  */
61247c478bd9Sstevel@tonic-gate 
61257c478bd9Sstevel@tonic-gate static struct stat *
61267c478bd9Sstevel@tonic-gate convert_to_old_stat(struct stat *FromStat, char *namep, char *attrp)
61277c478bd9Sstevel@tonic-gate {
61287c478bd9Sstevel@tonic-gate 	static struct stat ToSt;
61297c478bd9Sstevel@tonic-gate 	cpioinfo_t TmpSt;
61307c478bd9Sstevel@tonic-gate 
61317c478bd9Sstevel@tonic-gate 	(void) memset(&TmpSt, 0, sizeof (cpioinfo_t));
61327c478bd9Sstevel@tonic-gate 	stat_to_svr32_stat(&TmpSt, FromStat);
61337c478bd9Sstevel@tonic-gate 	(void) memset(&ToSt, 0, sizeof (ToSt));
61347c478bd9Sstevel@tonic-gate 
61357c478bd9Sstevel@tonic-gate 	if (TmpSt.st_rdev == (o_dev_t)NODEV &&
61367c478bd9Sstevel@tonic-gate 	    (((TmpSt.st_mode & Ftype) == S_IFCHR) ||
61377c478bd9Sstevel@tonic-gate 	    ((TmpSt.st_mode & Ftype) == S_IFBLK))) {
61387c478bd9Sstevel@tonic-gate 		/*
61397c478bd9Sstevel@tonic-gate 		 * Encountered a problem representing the rdev information.
61407c478bd9Sstevel@tonic-gate 		 * Don't archive it.
61417c478bd9Sstevel@tonic-gate 		 */
61427c478bd9Sstevel@tonic-gate 
61437c478bd9Sstevel@tonic-gate 		msg(ERR, "Error -Hodc format can't support expanded"
61447c478bd9Sstevel@tonic-gate 		    "types on %s%s%s",
61457c478bd9Sstevel@tonic-gate 		    namep,
61467c478bd9Sstevel@tonic-gate 		    (attrp == NULL) ? "" : gettext(" Attribute"),
61477c478bd9Sstevel@tonic-gate 		    (attrp == NULL) ? "" : attrp);
61487c478bd9Sstevel@tonic-gate 		return (NULL);
61497c478bd9Sstevel@tonic-gate 	}
61507c478bd9Sstevel@tonic-gate 
61517c478bd9Sstevel@tonic-gate 	if (TmpSt.st_dev == (o_dev_t)NODEV) {
61527c478bd9Sstevel@tonic-gate 		/*
61537c478bd9Sstevel@tonic-gate 		 * Having trouble representing the device/inode pair.  We can't
61547c478bd9Sstevel@tonic-gate 		 * track links in this case; break them all into separate
61557c478bd9Sstevel@tonic-gate 		 * files.
61567c478bd9Sstevel@tonic-gate 		 */
61577c478bd9Sstevel@tonic-gate 
61587c478bd9Sstevel@tonic-gate 		TmpSt.st_ino = 0;
61597c478bd9Sstevel@tonic-gate 
61607c478bd9Sstevel@tonic-gate 		if (((TmpSt.st_mode & Ftype) != S_IFDIR) &&
61617c478bd9Sstevel@tonic-gate 		    TmpSt.st_nlink > 1)
61627c478bd9Sstevel@tonic-gate 			msg(POST,
61637c478bd9Sstevel@tonic-gate 			    "Warning: file %s%s%s has large "
61647c478bd9Sstevel@tonic-gate 			    "device number - linked "
61657c478bd9Sstevel@tonic-gate 			    "files will be restored as "
61667c478bd9Sstevel@tonic-gate 			    "separate files",
61677c478bd9Sstevel@tonic-gate 			    namep,
61687c478bd9Sstevel@tonic-gate 			    (attrp == NULL) ? "" : gettext(" Attribute"),
61697c478bd9Sstevel@tonic-gate 			    (attrp == NULL) ? "" : attrp);
61707c478bd9Sstevel@tonic-gate 
61717c478bd9Sstevel@tonic-gate 		/* ensure no links */
61727c478bd9Sstevel@tonic-gate 
61737c478bd9Sstevel@tonic-gate 		TmpSt.st_nlink = 1;
61747c478bd9Sstevel@tonic-gate 	}
61757c478bd9Sstevel@tonic-gate 
61767c478bd9Sstevel@tonic-gate 	/* Start converting values */
61777c478bd9Sstevel@tonic-gate 
61787c478bd9Sstevel@tonic-gate 	if (TmpSt.st_dev < 0) {
61797c478bd9Sstevel@tonic-gate 		ToSt.st_dev = 0;
61807c478bd9Sstevel@tonic-gate 	} else {
61817c478bd9Sstevel@tonic-gate 		ToSt.st_dev = (dev_t)TmpSt.st_dev;
61827c478bd9Sstevel@tonic-gate 	}
61837c478bd9Sstevel@tonic-gate 
61847c478bd9Sstevel@tonic-gate 	/* -actual- not truncated uid */
61857c478bd9Sstevel@tonic-gate 
61867c478bd9Sstevel@tonic-gate 	ToSt.st_uid = TmpSt.st_uid;
61877c478bd9Sstevel@tonic-gate 
61887c478bd9Sstevel@tonic-gate 	/* -actual- not truncated gid */
61897c478bd9Sstevel@tonic-gate 
61907c478bd9Sstevel@tonic-gate 	ToSt.st_gid = TmpSt.st_gid;
61917c478bd9Sstevel@tonic-gate 	ToSt.st_ino = (ino_t)TmpSt.st_ino;
61927c478bd9Sstevel@tonic-gate 	ToSt.st_mode = (mode_t)TmpSt.st_mode;
61937c478bd9Sstevel@tonic-gate 	ToSt.st_mtime = (ulong_t)TmpSt.st_modtime;
61947c478bd9Sstevel@tonic-gate 	ToSt.st_nlink = (nlink_t)TmpSt.st_nlink;
61957c478bd9Sstevel@tonic-gate 	ToSt.st_size = (off_t)TmpSt.st_size;
61967c478bd9Sstevel@tonic-gate 	ToSt.st_rdev = (dev_t)TmpSt.st_rdev;
61977c478bd9Sstevel@tonic-gate 
61987c478bd9Sstevel@tonic-gate 	return (&ToSt);
61997c478bd9Sstevel@tonic-gate }
62007c478bd9Sstevel@tonic-gate 
62017c478bd9Sstevel@tonic-gate /*
62027c478bd9Sstevel@tonic-gate  * In the beginning of each bar archive, there is a header which describes the
62037c478bd9Sstevel@tonic-gate  * current volume being created, followed by a header which describes the
62047c478bd9Sstevel@tonic-gate  * current file being created, followed by the file itself.  If there is
62057c478bd9Sstevel@tonic-gate  * more than one file to be created, a separate header will be created for
62067c478bd9Sstevel@tonic-gate  * each additional file.  This structure may be repeated if the bar archive
62077c478bd9Sstevel@tonic-gate  * contains multiple volumes.  If a file spans across volumes, its header
62087c478bd9Sstevel@tonic-gate  * will not be repeated in the next volume.
62097c478bd9Sstevel@tonic-gate  *               +------------------+
62107c478bd9Sstevel@tonic-gate  *               |    vol header    |
62117c478bd9Sstevel@tonic-gate  *               |------------------|
62127c478bd9Sstevel@tonic-gate  *               |   file header i  |     i = 0
62137c478bd9Sstevel@tonic-gate  *               |------------------|
62147c478bd9Sstevel@tonic-gate  *               |     <file i>     |
62157c478bd9Sstevel@tonic-gate  *               |------------------|
62167c478bd9Sstevel@tonic-gate  *               |  file header i+1 |
62177c478bd9Sstevel@tonic-gate  *               |------------------|
62187c478bd9Sstevel@tonic-gate  *               |    <file i+1>    |
62197c478bd9Sstevel@tonic-gate  *               |------------------|
62207c478bd9Sstevel@tonic-gate  *               |        .         |
62217c478bd9Sstevel@tonic-gate  *               |        .         |
62227c478bd9Sstevel@tonic-gate  *               |        .         |
62237c478bd9Sstevel@tonic-gate  *               +------------------+
62247c478bd9Sstevel@tonic-gate  */
62257c478bd9Sstevel@tonic-gate 
62267c478bd9Sstevel@tonic-gate /*
62277c478bd9Sstevel@tonic-gate  * read in the header that describes the current volume of the bar archive
62287c478bd9Sstevel@tonic-gate  * to be extracted.
62297c478bd9Sstevel@tonic-gate  */
62307c478bd9Sstevel@tonic-gate static void
62317c478bd9Sstevel@tonic-gate read_bar_vol_hdr(void)
62327c478bd9Sstevel@tonic-gate {
62337c478bd9Sstevel@tonic-gate 	union b_block *tmp_hdr;
62347c478bd9Sstevel@tonic-gate 
62357c478bd9Sstevel@tonic-gate 	tmp_hdr = (union b_block *)Buffr.b_out_p;
62367c478bd9Sstevel@tonic-gate 	if (tmp_hdr->dbuf.bar_magic[0] == BAR_VOLUME_MAGIC) {
62377c478bd9Sstevel@tonic-gate 
62387c478bd9Sstevel@tonic-gate 		if (bar_Vhdr == NULL) {
62397c478bd9Sstevel@tonic-gate 			bar_Vhdr = e_zalloc(E_EXIT, TBLOCK);
62407c478bd9Sstevel@tonic-gate 		}
62417c478bd9Sstevel@tonic-gate 		(void) memcpy(&(bar_Vhdr->dbuf), &(tmp_hdr->dbuf), TBLOCK);
62427c478bd9Sstevel@tonic-gate 	} else {
62437c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
62447c478bd9Sstevel@tonic-gate 		    "bar error: cannot read volume header\n"));
62457c478bd9Sstevel@tonic-gate 		exit(1);
62467c478bd9Sstevel@tonic-gate 	}
62477c478bd9Sstevel@tonic-gate 
62487c478bd9Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.mode, "%8lo", &Gen_bar_vol.g_mode);
62497c478bd9Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.uid, "%8d", (int *)&Gen_bar_vol.g_uid);
62507c478bd9Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.gid, "%8d", (int *)&Gen_bar_vol.g_gid);
62517c478bd9Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.size, "%12llo",
62527c478bd9Sstevel@tonic-gate 	    (u_off_t *)&Gen_bar_vol.g_filesz);
62537c478bd9Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.mtime, "%12lo", &Gen_bar_vol.g_mtime);
62547c478bd9Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.chksum, "%8lo", &Gen_bar_vol.g_cksum);
62557c478bd9Sstevel@tonic-gate 
62567c478bd9Sstevel@tonic-gate 	/* set the compress flag */
62577c478bd9Sstevel@tonic-gate 	if (bar_Vhdr->dbuf.compressed == '1')
62587c478bd9Sstevel@tonic-gate 		Compressed = 1;
62597c478bd9Sstevel@tonic-gate 	else
62607c478bd9Sstevel@tonic-gate 		Compressed = 0;
62617c478bd9Sstevel@tonic-gate 
62627c478bd9Sstevel@tonic-gate 	Buffr.b_out_p += 512;
62637c478bd9Sstevel@tonic-gate 	Buffr.b_cnt -= 512;
62647c478bd9Sstevel@tonic-gate 
62657c478bd9Sstevel@tonic-gate 	/*
62667c478bd9Sstevel@tonic-gate 	 * not the first volume; exit
62677c478bd9Sstevel@tonic-gate 	 */
62687c478bd9Sstevel@tonic-gate 	if (strcmp(bar_Vhdr->dbuf.volume_num, "1") != 0) {
62697c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
62707c478bd9Sstevel@tonic-gate 		    gettext("error: This is not volume 1.  "));
62717c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("This is volume %s.  "),
62727c478bd9Sstevel@tonic-gate 		    bar_Vhdr->dbuf.volume_num);
62737c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Please insert volume 1.\n"));
62747c478bd9Sstevel@tonic-gate 		exit(1);
62757c478bd9Sstevel@tonic-gate 	}
62767c478bd9Sstevel@tonic-gate 
62777c478bd9Sstevel@tonic-gate 	read_bar_file_hdr();
62787c478bd9Sstevel@tonic-gate }
62797c478bd9Sstevel@tonic-gate 
62807c478bd9Sstevel@tonic-gate /*
62817c478bd9Sstevel@tonic-gate  * read in the header that describes the current file to be extracted
62827c478bd9Sstevel@tonic-gate  */
62837c478bd9Sstevel@tonic-gate static void
62847c478bd9Sstevel@tonic-gate read_bar_file_hdr(void)
62857c478bd9Sstevel@tonic-gate {
62867c478bd9Sstevel@tonic-gate 	union b_block *tmp_hdr;
62877c478bd9Sstevel@tonic-gate 	char *start_of_name, *name_p;
62887c478bd9Sstevel@tonic-gate 	char *tmp;
62897c478bd9Sstevel@tonic-gate 
62907c478bd9Sstevel@tonic-gate 	if (*Buffr.b_out_p == '\0') {
62917c478bd9Sstevel@tonic-gate 		*Gen.g_nam_p = '\0';
62927c478bd9Sstevel@tonic-gate 		exit(0);
62937c478bd9Sstevel@tonic-gate 	}
62947c478bd9Sstevel@tonic-gate 
62957c478bd9Sstevel@tonic-gate 	tmp_hdr = (union b_block *)Buffr.b_out_p;
62967c478bd9Sstevel@tonic-gate 
62977c478bd9Sstevel@tonic-gate 	tmp = &tmp_hdr->dbuf.mode[1];
62987c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp, "%8lo", &Gen.g_mode);
62997c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.uid, "%8lo", &Gen.g_uid);
63007c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.gid, "%8lo", &Gen.g_gid);
63017c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.size, "%12llo",
63027c478bd9Sstevel@tonic-gate 	    (u_off_t *)&Gen.g_filesz);
63037c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.mtime, "%12lo", &Gen.g_mtime);
63047c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.chksum, "%8lo", &Gen.g_cksum);
63057c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.rdev, "%8lo", &Gen.g_rdev);
63067c478bd9Sstevel@tonic-gate 
63077c478bd9Sstevel@tonic-gate #define	to_new_major(x)	(int)((unsigned)((x) & OMAXMAJ) << NBITSMINOR)
63087c478bd9Sstevel@tonic-gate #define	to_new_minor(x)	(int)((x) & OMAXMIN)
63097c478bd9Sstevel@tonic-gate 	Gen.g_rdev = to_new_major(Gen.g_rdev) | to_new_minor(Gen.g_rdev);
63107c478bd9Sstevel@tonic-gate 	bar_linkflag = tmp_hdr->dbuf.linkflag;
63117c478bd9Sstevel@tonic-gate 	start_of_name = &tmp_hdr->dbuf.start_of_name;
63127c478bd9Sstevel@tonic-gate 
63137c478bd9Sstevel@tonic-gate 
63147c478bd9Sstevel@tonic-gate 	name_p = Gen.g_nam_p;
63157c478bd9Sstevel@tonic-gate 	while (*name_p++ = *start_of_name++)
63167c478bd9Sstevel@tonic-gate 		;
63177c478bd9Sstevel@tonic-gate 	*name_p = '\0';
63187c478bd9Sstevel@tonic-gate 	if (bar_linkflag == LNKTYPE || bar_linkflag == SYMTYPE)
63197c478bd9Sstevel@tonic-gate 		(void) strcpy(bar_linkname, start_of_name);
63207c478bd9Sstevel@tonic-gate 
63217c478bd9Sstevel@tonic-gate 	Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
63227c478bd9Sstevel@tonic-gate 	(void) strcpy(nambuf, Gen.g_nam_p);
63237c478bd9Sstevel@tonic-gate }
63247c478bd9Sstevel@tonic-gate 
63257c478bd9Sstevel@tonic-gate /*
63267c478bd9Sstevel@tonic-gate  * if the bar archive is compressed, set up a pipe and do the de-compression
63277c478bd9Sstevel@tonic-gate  * as the compressed file is read in.
63287c478bd9Sstevel@tonic-gate  */
63297c478bd9Sstevel@tonic-gate static void
63307c478bd9Sstevel@tonic-gate setup_uncompress(FILE **pipef)
63317c478bd9Sstevel@tonic-gate {
63327c478bd9Sstevel@tonic-gate 	char *cmd_buf;
63337c478bd9Sstevel@tonic-gate 	size_t cmdlen;
63347c478bd9Sstevel@tonic-gate 
63357c478bd9Sstevel@tonic-gate 	cmd_buf = e_zalloc(E_EXIT, MAXPATHLEN * 2);
63367c478bd9Sstevel@tonic-gate 
63377c478bd9Sstevel@tonic-gate 	if (access(Gen.g_nam_p, W_OK) != 0) {
63387c478bd9Sstevel@tonic-gate 		cmdlen = snprintf(cmd_buf, MAXPATHLEN * 2,
63397c478bd9Sstevel@tonic-gate 		    "chmod +w '%s'; uncompress -c > '%s'; "
63407c478bd9Sstevel@tonic-gate 		    "chmod 0%o '%s'",
63417c478bd9Sstevel@tonic-gate 		    Gen.g_nam_p, Gen.g_nam_p, (int)G_p->g_mode, Gen.g_nam_p);
63427c478bd9Sstevel@tonic-gate 	} else {
63437c478bd9Sstevel@tonic-gate 		cmdlen = snprintf(cmd_buf, MAXPATHLEN * 2,
63447c478bd9Sstevel@tonic-gate 		    "uncompress -c > '%s'", Gen.g_nam_p);
63457c478bd9Sstevel@tonic-gate 	}
63467c478bd9Sstevel@tonic-gate 
63477c478bd9Sstevel@tonic-gate 	if (cmdlen >= MAXPATHLEN * 2 ||
63487c478bd9Sstevel@tonic-gate 	    (*pipef = popen(cmd_buf, "w")) == NULL) {
63497c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("error\n"));
63507c478bd9Sstevel@tonic-gate 		exit(1);
63517c478bd9Sstevel@tonic-gate 	}
63527c478bd9Sstevel@tonic-gate 
63537c478bd9Sstevel@tonic-gate 	if (close(Ofile) != 0)
63547c478bd9Sstevel@tonic-gate 		msg(EXTN, "close error");
63557c478bd9Sstevel@tonic-gate 	Ofile = fileno(*pipef);
63567c478bd9Sstevel@tonic-gate 
63577c478bd9Sstevel@tonic-gate 	free(cmd_buf);
63587c478bd9Sstevel@tonic-gate }
63597c478bd9Sstevel@tonic-gate 
63607c478bd9Sstevel@tonic-gate /*
63617c478bd9Sstevel@tonic-gate  * if the bar archive spans multiple volumes, read in the header that
63627c478bd9Sstevel@tonic-gate  * describes the next volume.
63637c478bd9Sstevel@tonic-gate  */
63647c478bd9Sstevel@tonic-gate static void
63657c478bd9Sstevel@tonic-gate skip_bar_volhdr(void)
63667c478bd9Sstevel@tonic-gate {
63677c478bd9Sstevel@tonic-gate 	char *buff;
63687c478bd9Sstevel@tonic-gate 	union b_block *tmp_hdr;
63697c478bd9Sstevel@tonic-gate 
63707c478bd9Sstevel@tonic-gate 	buff = e_zalloc(E_EXIT, (uint_t)Bufsize);
63717c478bd9Sstevel@tonic-gate 
63727c478bd9Sstevel@tonic-gate 	if (g_read(Device, Archive, buff, Bufsize) < 0) {
63737c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
63747c478bd9Sstevel@tonic-gate 		    "error in skip_bar_volhdr\n"));
63757c478bd9Sstevel@tonic-gate 	} else {
63767c478bd9Sstevel@tonic-gate 
63777c478bd9Sstevel@tonic-gate 		tmp_hdr = (union b_block *)buff;
63787c478bd9Sstevel@tonic-gate 		if (tmp_hdr->dbuf.bar_magic[0] == BAR_VOLUME_MAGIC) {
63797c478bd9Sstevel@tonic-gate 
63807c478bd9Sstevel@tonic-gate 			if (bar_Vhdr == NULL) {
63817c478bd9Sstevel@tonic-gate 				bar_Vhdr = e_zalloc(E_EXIT, TBLOCK);
63827c478bd9Sstevel@tonic-gate 			}
63837c478bd9Sstevel@tonic-gate 			(void) memcpy(&(bar_Vhdr->dbuf),
63847c478bd9Sstevel@tonic-gate 			    &(tmp_hdr->dbuf), TBLOCK);
63857c478bd9Sstevel@tonic-gate 		} else {
63867c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
63877c478bd9Sstevel@tonic-gate 			    gettext("cpio error: cannot read bar volume "
63887c478bd9Sstevel@tonic-gate 			    "header\n"));
63897c478bd9Sstevel@tonic-gate 			exit(1);
63907c478bd9Sstevel@tonic-gate 		}
63917c478bd9Sstevel@tonic-gate 
63927c478bd9Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.mode, "%8lo",
63937c478bd9Sstevel@tonic-gate 		    &Gen_bar_vol.g_mode);
63947c478bd9Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.uid, "%8lo",
63957c478bd9Sstevel@tonic-gate 		    &Gen_bar_vol.g_uid);
63967c478bd9Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.gid, "%8lo",
63977c478bd9Sstevel@tonic-gate 		    &Gen_bar_vol.g_gid);
63987c478bd9Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.size, "%12llo",
63997c478bd9Sstevel@tonic-gate 		    (u_off_t *)&Gen_bar_vol.g_filesz);
64007c478bd9Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.mtime, "%12lo",
64017c478bd9Sstevel@tonic-gate 		    &Gen_bar_vol.g_mtime);
64027c478bd9Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.chksum, "%8lo",
64037c478bd9Sstevel@tonic-gate 		    &Gen_bar_vol.g_cksum);
64047c478bd9Sstevel@tonic-gate 		if (bar_Vhdr->dbuf.compressed == '1')
64057c478bd9Sstevel@tonic-gate 			Compressed = 1;
64067c478bd9Sstevel@tonic-gate 		else
64077c478bd9Sstevel@tonic-gate 			Compressed = 0;
64087c478bd9Sstevel@tonic-gate 	}
64097c478bd9Sstevel@tonic-gate 
64107c478bd9Sstevel@tonic-gate 	/*
64117c478bd9Sstevel@tonic-gate 	 * Now put the rest of the bytes read in into the data buffer.
64127c478bd9Sstevel@tonic-gate 	 */
64137c478bd9Sstevel@tonic-gate 	(void) memcpy(Buffr.b_in_p, &buff[512], (Bufsize - 512));
64147c478bd9Sstevel@tonic-gate 	Buffr.b_in_p += (Bufsize - 512);
64157c478bd9Sstevel@tonic-gate 	Buffr.b_cnt += (long)(Bufsize - 512);
64167c478bd9Sstevel@tonic-gate 
64177c478bd9Sstevel@tonic-gate 	free(buff);
64187c478bd9Sstevel@tonic-gate }
64197c478bd9Sstevel@tonic-gate 
64207c478bd9Sstevel@tonic-gate /*
64217c478bd9Sstevel@tonic-gate  * check the linkflag which indicates the type of the file to be extracted,
64227c478bd9Sstevel@tonic-gate  * invoke the corresponding routine to extract the file.
64237c478bd9Sstevel@tonic-gate  */
64247c478bd9Sstevel@tonic-gate static void
64257c478bd9Sstevel@tonic-gate bar_file_in(void)
64267c478bd9Sstevel@tonic-gate {
64277c478bd9Sstevel@tonic-gate 	/*
64287c478bd9Sstevel@tonic-gate 	 * the file is a directory
64297c478bd9Sstevel@tonic-gate 	 */
64307c478bd9Sstevel@tonic-gate 	if (Adir) {
64317c478bd9Sstevel@tonic-gate 		if (ckname(1) != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
64327c478bd9Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
64337c478bd9Sstevel@tonic-gate 		}
64347c478bd9Sstevel@tonic-gate 		return;
64357c478bd9Sstevel@tonic-gate 	}
64367c478bd9Sstevel@tonic-gate 
64377c478bd9Sstevel@tonic-gate 	switch (bar_linkflag) {
64387c478bd9Sstevel@tonic-gate 	case REGTYPE:
64397c478bd9Sstevel@tonic-gate 		/* regular file */
64407c478bd9Sstevel@tonic-gate 		if ((ckname(1) == F_SKIP) ||
64417c478bd9Sstevel@tonic-gate 		    (Ofile = openout(G_p->g_dirfd)) < 0) {
64427c478bd9Sstevel@tonic-gate 			data_in(P_SKIP);
64437c478bd9Sstevel@tonic-gate 		} else {
64447c478bd9Sstevel@tonic-gate 			data_in(P_PROC);
64457c478bd9Sstevel@tonic-gate 		}
64467c478bd9Sstevel@tonic-gate 		break;
64477c478bd9Sstevel@tonic-gate 	case LNKTYPE:
64487c478bd9Sstevel@tonic-gate 		/* hard link */
64497c478bd9Sstevel@tonic-gate 		if (ckname(1) == F_SKIP) {
64507c478bd9Sstevel@tonic-gate 			break;
64517c478bd9Sstevel@tonic-gate 		}
64527c478bd9Sstevel@tonic-gate 		(void) creat_lnk(G_p->g_dirfd, bar_linkname, G_p->g_nam_p);
64537c478bd9Sstevel@tonic-gate 		break;
64547c478bd9Sstevel@tonic-gate 	case SYMTYPE:
64557c478bd9Sstevel@tonic-gate 		/* symbolic link */
64567c478bd9Sstevel@tonic-gate 		if ((ckname(1) == F_SKIP) ||
64577c478bd9Sstevel@tonic-gate 		    (Ofile = openout(G_p->g_dirfd)) < 0) {
64587c478bd9Sstevel@tonic-gate 			data_in(P_SKIP);
64597c478bd9Sstevel@tonic-gate 		} else {
64607c478bd9Sstevel@tonic-gate 			data_in(P_PROC);
64617c478bd9Sstevel@tonic-gate 		}
64627c478bd9Sstevel@tonic-gate 		break;
64637c478bd9Sstevel@tonic-gate 	case CHRTYPE:
64647c478bd9Sstevel@tonic-gate 		/* character device or FIFO */
64657c478bd9Sstevel@tonic-gate 		if (ckname(1) != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
64667c478bd9Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
64677c478bd9Sstevel@tonic-gate 		}
64687c478bd9Sstevel@tonic-gate 		break;
64697c478bd9Sstevel@tonic-gate 	default:
64707c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("error: unknown file type\n"));
64717c478bd9Sstevel@tonic-gate 		break;
64727c478bd9Sstevel@tonic-gate 	}
64737c478bd9Sstevel@tonic-gate }
64747c478bd9Sstevel@tonic-gate 
64757c478bd9Sstevel@tonic-gate 
64767c478bd9Sstevel@tonic-gate /*
64777c478bd9Sstevel@tonic-gate  * This originally came from libgenIO/g_init.c
64787c478bd9Sstevel@tonic-gate  * XXX	And it is very broken.
64797c478bd9Sstevel@tonic-gate  */
64807c478bd9Sstevel@tonic-gate 
64817c478bd9Sstevel@tonic-gate /* #include <sys/statvfs.h> */
64827c478bd9Sstevel@tonic-gate #include <ftw.h>
64837c478bd9Sstevel@tonic-gate /* #include <libgenIO.h> */
64847c478bd9Sstevel@tonic-gate #define	G_TM_TAPE	1	/* Tapemaster controller    */
64857c478bd9Sstevel@tonic-gate #define	G_XY_DISK	3	/* xy disks		*/
64867c478bd9Sstevel@tonic-gate #define	G_SD_DISK	7	/* scsi sd disk		*/
64877c478bd9Sstevel@tonic-gate #define	G_XT_TAPE	8	/* xt tapes		*/
64887c478bd9Sstevel@tonic-gate #define	G_SF_FLOPPY	9	/* sf floppy		*/
64897c478bd9Sstevel@tonic-gate #define	G_XD_DISK	10	/* xd disks		*/
64907c478bd9Sstevel@tonic-gate #define	G_ST_TAPE	11	/* scsi tape		*/
64917c478bd9Sstevel@tonic-gate #define	G_NS		12	/* noswap pseudo-dev	*/
64927c478bd9Sstevel@tonic-gate #define	G_RAM		13	/* ram pseudo-dev	*/
64937c478bd9Sstevel@tonic-gate #define	G_FT		14	/* tftp			*/
64947c478bd9Sstevel@tonic-gate #define	G_HD		15	/* 386 network disk	*/
64957c478bd9Sstevel@tonic-gate #define	G_FD		16	/* 386 AT disk		*/
64967c478bd9Sstevel@tonic-gate #define	G_FILE		28	/* file, not a device	*/
64977c478bd9Sstevel@tonic-gate #define	G_NO_DEV	29	/* device does not require special treatment */
64987c478bd9Sstevel@tonic-gate #define	G_DEV_MAX	30	/* last valid device type */
64997c478bd9Sstevel@tonic-gate 
65007c478bd9Sstevel@tonic-gate /*
65017c478bd9Sstevel@tonic-gate  * g_init: Determine the device being accessed, set the buffer size,
65027c478bd9Sstevel@tonic-gate  * and perform any device specific initialization. Since at this point
65037c478bd9Sstevel@tonic-gate  * Sun has no system call to read the configuration, the major numbers
65047c478bd9Sstevel@tonic-gate  * are assumed to be static and types are figured out as such. However,
65057c478bd9Sstevel@tonic-gate  * as a rough estimate, the buffer size for all types is set to 512
65067c478bd9Sstevel@tonic-gate  * as a default.
65077c478bd9Sstevel@tonic-gate  */
65087c478bd9Sstevel@tonic-gate 
65097c478bd9Sstevel@tonic-gate static int
65107c478bd9Sstevel@tonic-gate g_init(int *devtype, int *fdes)
65117c478bd9Sstevel@tonic-gate {
65127c478bd9Sstevel@tonic-gate 	int bufsize;
65137c478bd9Sstevel@tonic-gate 	struct stat st_buf;
65147c478bd9Sstevel@tonic-gate 	struct statvfs stfs_buf;
65157c478bd9Sstevel@tonic-gate 
65167c478bd9Sstevel@tonic-gate 	*devtype = G_NO_DEV;
65177c478bd9Sstevel@tonic-gate 	bufsize = -1;
65187c478bd9Sstevel@tonic-gate 	if (fstat(*fdes, &st_buf) == -1)
65197c478bd9Sstevel@tonic-gate 		return (-1);
65204bc0a2efScasper 	if (!S_ISCHR(st_buf.st_mode) && !S_ISBLK(st_buf.st_mode)) {
65214bc0a2efScasper 		if (S_ISFIFO(st_buf.st_mode)) {
65227c478bd9Sstevel@tonic-gate 			bufsize = 512;
65237c478bd9Sstevel@tonic-gate 		} else {
65247c478bd9Sstevel@tonic-gate 			/* find block size for this file system */
65257c478bd9Sstevel@tonic-gate 			*devtype = G_FILE;
65267c478bd9Sstevel@tonic-gate 			if (fstatvfs(*fdes, &stfs_buf) < 0) {
65277c478bd9Sstevel@tonic-gate 					bufsize = -1;
65287c478bd9Sstevel@tonic-gate 					errno = ENODEV;
65297c478bd9Sstevel@tonic-gate 			} else
65307c478bd9Sstevel@tonic-gate 				bufsize = stfs_buf.f_bsize;
65317c478bd9Sstevel@tonic-gate 		}
65327c478bd9Sstevel@tonic-gate 
65337c478bd9Sstevel@tonic-gate 		return (bufsize);
65347c478bd9Sstevel@tonic-gate 
65357c478bd9Sstevel@tonic-gate 	/*
65367c478bd9Sstevel@tonic-gate 	 * We'll have to add a remote attribute to stat but this
65377c478bd9Sstevel@tonic-gate 	 * should work for now.
65387c478bd9Sstevel@tonic-gate 	 */
65397c478bd9Sstevel@tonic-gate 	} else if (st_buf.st_dev & 0x8000)	/* if remote  rdev */
65407c478bd9Sstevel@tonic-gate 		return (512);
65417c478bd9Sstevel@tonic-gate 
65427c478bd9Sstevel@tonic-gate 	bufsize = 512;
65437c478bd9Sstevel@tonic-gate 
65447c478bd9Sstevel@tonic-gate 	if (Hdr_type == BAR) {
65457c478bd9Sstevel@tonic-gate 		if (is_tape(*fdes)) {
65467c478bd9Sstevel@tonic-gate 			bufsize = BAR_TAPE_SIZE;
65477c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "Archiving to tape");
65487c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, " blocking factor 126\n");
65497c478bd9Sstevel@tonic-gate 		} else if (is_floppy(*fdes)) {
65507c478bd9Sstevel@tonic-gate 			bufsize = BAR_FLOPPY_SIZE;
65517c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "Archiving to floppy");
65527c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, " blocking factor 18\n");
65537c478bd9Sstevel@tonic-gate 		}
65547c478bd9Sstevel@tonic-gate 	}
65557c478bd9Sstevel@tonic-gate 
65567c478bd9Sstevel@tonic-gate 	return (bufsize);
65577c478bd9Sstevel@tonic-gate }
65587c478bd9Sstevel@tonic-gate 
65597c478bd9Sstevel@tonic-gate /*
65607c478bd9Sstevel@tonic-gate  * This originally came from libgenIO/g_read.c
65617c478bd9Sstevel@tonic-gate  */
65627c478bd9Sstevel@tonic-gate 
65637c478bd9Sstevel@tonic-gate /*
65647c478bd9Sstevel@tonic-gate  * g_read: Read nbytes of data from fdes (of type devtype) and place
65657c478bd9Sstevel@tonic-gate  * data in location pointed to by buf.  In case of end of medium,
65667c478bd9Sstevel@tonic-gate  * translate (where necessary) device specific EOM indications into
65677c478bd9Sstevel@tonic-gate  * the generic EOM indication of rv = -1, errno = ENOSPC.
65687c478bd9Sstevel@tonic-gate  */
65697c478bd9Sstevel@tonic-gate 
65707c478bd9Sstevel@tonic-gate static int
65717c478bd9Sstevel@tonic-gate g_read(int devtype, int fdes, char *buf, unsigned nbytes)
65727c478bd9Sstevel@tonic-gate {
65737c478bd9Sstevel@tonic-gate 	int rv;
65747c478bd9Sstevel@tonic-gate 
65757c478bd9Sstevel@tonic-gate 	if (devtype < 0 || devtype >= G_DEV_MAX) {
65767c478bd9Sstevel@tonic-gate 		errno = ENODEV;
65777c478bd9Sstevel@tonic-gate 		return (-1);
65787c478bd9Sstevel@tonic-gate 	}
65797c478bd9Sstevel@tonic-gate 
65807c478bd9Sstevel@tonic-gate 	rv = read(fdes, buf, nbytes);
65817c478bd9Sstevel@tonic-gate 
65827c478bd9Sstevel@tonic-gate 	/* st devices return 0 when no space left */
65837c478bd9Sstevel@tonic-gate 	if ((rv == 0 && errno == 0 && Hdr_type != BAR) ||
65847c478bd9Sstevel@tonic-gate 	    (rv == -1 && errno == EIO)) {
65857c478bd9Sstevel@tonic-gate 		errno = 0;
65867c478bd9Sstevel@tonic-gate 		rv = 0;
65877c478bd9Sstevel@tonic-gate 	}
65887c478bd9Sstevel@tonic-gate 
65897c478bd9Sstevel@tonic-gate 	return (rv);
65907c478bd9Sstevel@tonic-gate }
65917c478bd9Sstevel@tonic-gate 
65927c478bd9Sstevel@tonic-gate /*
65937c478bd9Sstevel@tonic-gate  * This originally came from libgenIO/g_write.c
65947c478bd9Sstevel@tonic-gate  */
65957c478bd9Sstevel@tonic-gate 
65967c478bd9Sstevel@tonic-gate /*
65977c478bd9Sstevel@tonic-gate  * g_write: Write nbytes of data to fdes (of type devtype) from
65987c478bd9Sstevel@tonic-gate  * the location pointed to by buf.  In case of end of medium,
65997c478bd9Sstevel@tonic-gate  * translate (where necessary) device specific EOM indications into
66007c478bd9Sstevel@tonic-gate  * the generic EOM indication of rv = -1, errno = ENOSPC.
66017c478bd9Sstevel@tonic-gate  */
66027c478bd9Sstevel@tonic-gate 
66037c478bd9Sstevel@tonic-gate static int
66047c478bd9Sstevel@tonic-gate g_write(int devtype, int fdes, char *buf, unsigned nbytes)
66057c478bd9Sstevel@tonic-gate {
66067c478bd9Sstevel@tonic-gate 	int rv;
66077c478bd9Sstevel@tonic-gate 
66087c478bd9Sstevel@tonic-gate 	if (devtype < 0 || devtype >= G_DEV_MAX) {
66097c478bd9Sstevel@tonic-gate 		errno = ENODEV;
66107c478bd9Sstevel@tonic-gate 		return (-1);
66117c478bd9Sstevel@tonic-gate 	}
66127c478bd9Sstevel@tonic-gate 
66137c478bd9Sstevel@tonic-gate 	rv = write(fdes, buf, nbytes);
66147c478bd9Sstevel@tonic-gate 
66157c478bd9Sstevel@tonic-gate 	/* st devices return 0 when no more space left */
66167c478bd9Sstevel@tonic-gate 	if ((rv == 0 && errno == 0) || (rv == -1 && errno == EIO)) {
66177c478bd9Sstevel@tonic-gate 		errno = ENOSPC;
66187c478bd9Sstevel@tonic-gate 		rv = -1;
66197c478bd9Sstevel@tonic-gate 	}
66207c478bd9Sstevel@tonic-gate 
66217c478bd9Sstevel@tonic-gate 	return (rv);
66227c478bd9Sstevel@tonic-gate }
66237c478bd9Sstevel@tonic-gate 
66247c478bd9Sstevel@tonic-gate /*
66257c478bd9Sstevel@tonic-gate  * Test for tape
66267c478bd9Sstevel@tonic-gate  */
66277c478bd9Sstevel@tonic-gate 
66287c478bd9Sstevel@tonic-gate static int
66297c478bd9Sstevel@tonic-gate is_tape(int fd)
66307c478bd9Sstevel@tonic-gate {
66317c478bd9Sstevel@tonic-gate 	struct mtget stuff;
66327c478bd9Sstevel@tonic-gate 
66337c478bd9Sstevel@tonic-gate 	/*
66347c478bd9Sstevel@tonic-gate 	 * try to do a generic tape ioctl, just to see if
66357c478bd9Sstevel@tonic-gate 	 * the thing is in fact a tape drive(er).
66367c478bd9Sstevel@tonic-gate 	 */
66377c478bd9Sstevel@tonic-gate 	if (ioctl(fd, MTIOCGET, &stuff) != -1) {
66387c478bd9Sstevel@tonic-gate 		/* the ioctl succeeded, must have been a tape */
66397c478bd9Sstevel@tonic-gate 		return (1);
66407c478bd9Sstevel@tonic-gate 	}
66417c478bd9Sstevel@tonic-gate 	return (0);
66427c478bd9Sstevel@tonic-gate }
66437c478bd9Sstevel@tonic-gate 
66447c478bd9Sstevel@tonic-gate /*
66457c478bd9Sstevel@tonic-gate  * Test for floppy
66467c478bd9Sstevel@tonic-gate  */
66477c478bd9Sstevel@tonic-gate 
66487c478bd9Sstevel@tonic-gate static int
66497c478bd9Sstevel@tonic-gate is_floppy(int fd)
66507c478bd9Sstevel@tonic-gate {
66517c478bd9Sstevel@tonic-gate 	struct fd_char stuff;
66527c478bd9Sstevel@tonic-gate 
66537c478bd9Sstevel@tonic-gate 	/*
66547c478bd9Sstevel@tonic-gate 	 * try to get the floppy drive characteristics, just to see if
66557c478bd9Sstevel@tonic-gate 	 * the thing is in fact a floppy drive(er).
66567c478bd9Sstevel@tonic-gate 	 */
66577c478bd9Sstevel@tonic-gate 	if (ioctl(fd, FDIOGCHAR, &stuff) != -1) {
66587c478bd9Sstevel@tonic-gate 		/* the ioctl succeeded, must have been a floppy */
66597c478bd9Sstevel@tonic-gate 		return (1);
66607c478bd9Sstevel@tonic-gate 	}
66617c478bd9Sstevel@tonic-gate 
66627c478bd9Sstevel@tonic-gate 	return (0);
66637c478bd9Sstevel@tonic-gate }
66647c478bd9Sstevel@tonic-gate 
66657c478bd9Sstevel@tonic-gate /*
66667c478bd9Sstevel@tonic-gate  * New functions for ACLs and other security attributes
66677c478bd9Sstevel@tonic-gate  */
66687c478bd9Sstevel@tonic-gate 
66697c478bd9Sstevel@tonic-gate /*
66707c478bd9Sstevel@tonic-gate  * The function appends the new security attribute info to the end of
66717c478bd9Sstevel@tonic-gate  * existing secinfo.
66727c478bd9Sstevel@tonic-gate  */
66737c478bd9Sstevel@tonic-gate static int
66747c478bd9Sstevel@tonic-gate append_secattr(
66757c478bd9Sstevel@tonic-gate 	char		**secinfo,	/* existing security info */
66767c478bd9Sstevel@tonic-gate 	int		*secinfo_len,	/* length of existing security info */
6677fa9e4066Sahrens 	acl_t		*aclp) 	/* new attribute data pointer */
66787c478bd9Sstevel@tonic-gate {
66797c478bd9Sstevel@tonic-gate 	char	*new_secinfo;
66807c478bd9Sstevel@tonic-gate 	char	*attrtext;
66817c478bd9Sstevel@tonic-gate 	size_t	newattrsize;
66827c478bd9Sstevel@tonic-gate 	int	oldsize;
66837c478bd9Sstevel@tonic-gate 
66847c478bd9Sstevel@tonic-gate 	/* no need to add */
6685fa9e4066Sahrens 	if (aclp == NULL) {
66867c478bd9Sstevel@tonic-gate 		return (0);
66877c478bd9Sstevel@tonic-gate 	}
66887c478bd9Sstevel@tonic-gate 
6689fa9e4066Sahrens 	switch (acl_type(aclp)) {
6690fa9e4066Sahrens 	case ACLENT_T:
6691fa9e4066Sahrens 	case ACE_T:
66927c478bd9Sstevel@tonic-gate 		/* LINTED alignment */
66935a5eeccaSmarks 		attrtext = acl_totext(aclp, ACL_APPEND_ID | ACL_COMPACT_FMT);
66947c478bd9Sstevel@tonic-gate 		if (attrtext == NULL) {
66957c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "acltotext failed\n");
66967c478bd9Sstevel@tonic-gate 			return (-1);
66977c478bd9Sstevel@tonic-gate 		}
66987c478bd9Sstevel@tonic-gate 		/* header: type + size = 8 */
66997c478bd9Sstevel@tonic-gate 		newattrsize = 8 + strlen(attrtext) + 1;
67007c478bd9Sstevel@tonic-gate 		attr = e_zalloc(E_NORMAL, newattrsize);
67017c478bd9Sstevel@tonic-gate 		if (attr == NULL) {
67027c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "can't allocate memory\n");
67037c478bd9Sstevel@tonic-gate 			return (-1);
67047c478bd9Sstevel@tonic-gate 		}
6705fa9e4066Sahrens 		attr->attr_type = (acl_type(aclp) == ACLENT_T) ?
6706fa9e4066Sahrens 		    UFSD_ACL : ACE_ACL;
67077c478bd9Sstevel@tonic-gate 		/* acl entry count */
6708fa9e4066Sahrens 		(void) sprintf(attr->attr_len, "%06o", acl_cnt(aclp));
67097c478bd9Sstevel@tonic-gate 		(void) strcpy((char *)&attr->attr_info[0], attrtext);
67107c478bd9Sstevel@tonic-gate 		free(attrtext);
67117c478bd9Sstevel@tonic-gate 		break;
67127c478bd9Sstevel@tonic-gate 
67137c478bd9Sstevel@tonic-gate 		/* SunFed's case goes here */
67147c478bd9Sstevel@tonic-gate 
67157c478bd9Sstevel@tonic-gate 	default:
67167c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "unrecognized attribute type\n");
67177c478bd9Sstevel@tonic-gate 		return (-1);
67187c478bd9Sstevel@tonic-gate 	}
67197c478bd9Sstevel@tonic-gate 
67207c478bd9Sstevel@tonic-gate 	/* old security info + new attr header(8) + new attr */
67217c478bd9Sstevel@tonic-gate 	oldsize = *secinfo_len;
67227c478bd9Sstevel@tonic-gate 	*secinfo_len += newattrsize;
67237c478bd9Sstevel@tonic-gate 	new_secinfo = e_zalloc(E_NORMAL, (uint_t)*secinfo_len);
67247c478bd9Sstevel@tonic-gate 	if (new_secinfo == NULL) {
67257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "can't allocate memory\n");
67267c478bd9Sstevel@tonic-gate 		*secinfo_len -= newattrsize;
67277c478bd9Sstevel@tonic-gate 		return (-1);
67287c478bd9Sstevel@tonic-gate 	}
67297c478bd9Sstevel@tonic-gate 
67307c478bd9Sstevel@tonic-gate 	(void) memcpy(new_secinfo, *secinfo, oldsize);
67317c478bd9Sstevel@tonic-gate 	(void) memcpy(new_secinfo + oldsize, attr, newattrsize);
67327c478bd9Sstevel@tonic-gate 
67337c478bd9Sstevel@tonic-gate 	free(*secinfo);
67347c478bd9Sstevel@tonic-gate 	*secinfo = new_secinfo;
67357c478bd9Sstevel@tonic-gate 	return (0);
67367c478bd9Sstevel@tonic-gate }
67377c478bd9Sstevel@tonic-gate 
67387c478bd9Sstevel@tonic-gate static void
67397c478bd9Sstevel@tonic-gate write_ancillary(char *secinfo, int len)
67407c478bd9Sstevel@tonic-gate {
67417c478bd9Sstevel@tonic-gate 	long    pad;
67427c478bd9Sstevel@tonic-gate 	long    cnt;
67437c478bd9Sstevel@tonic-gate 
67447c478bd9Sstevel@tonic-gate 	/* Just tranditional permissions or no security attribute info */
67457c478bd9Sstevel@tonic-gate 	if (len == 0) {
67467c478bd9Sstevel@tonic-gate 		return;
67477c478bd9Sstevel@tonic-gate 	}
67487c478bd9Sstevel@tonic-gate 
67497c478bd9Sstevel@tonic-gate 	/* write out security info */
67507c478bd9Sstevel@tonic-gate 	while (len > 0) {
67517c478bd9Sstevel@tonic-gate 		cnt = (unsigned)(len > CPIOBSZ) ? CPIOBSZ : len;
67527c478bd9Sstevel@tonic-gate 		FLUSH(cnt);
67537c478bd9Sstevel@tonic-gate 		errno = 0;
67547c478bd9Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, secinfo, (unsigned)cnt);
67557c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += cnt;
67567c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += (long)cnt;
67577c478bd9Sstevel@tonic-gate 		len -= (long)cnt;
67587c478bd9Sstevel@tonic-gate 	}
67597c478bd9Sstevel@tonic-gate 	pad = (Pad_val + 1 - (cnt & Pad_val)) & Pad_val;
67607c478bd9Sstevel@tonic-gate 	if (pad != 0) {
67617c478bd9Sstevel@tonic-gate 		FLUSH(pad);
67627c478bd9Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, Empty, pad);
67637c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += pad;
67647c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += pad;
67657c478bd9Sstevel@tonic-gate 	}
67667c478bd9Sstevel@tonic-gate }
67677c478bd9Sstevel@tonic-gate 
67687c478bd9Sstevel@tonic-gate static int
67697c478bd9Sstevel@tonic-gate remove_dir(char *path)
67707c478bd9Sstevel@tonic-gate {
67717c478bd9Sstevel@tonic-gate 	DIR		*name;
67727c478bd9Sstevel@tonic-gate 	struct dirent	*direct;
67737c478bd9Sstevel@tonic-gate 	struct stat	sbuf;
67747c478bd9Sstevel@tonic-gate 	char		*path_copy;
67757c478bd9Sstevel@tonic-gate 
67767c478bd9Sstevel@tonic-gate #define	MSG1	"remove_dir() failed to stat(\"%s\") "
67777c478bd9Sstevel@tonic-gate #define	MSG2	"remove_dir() failed to remove_dir(\"%s\") "
67787c478bd9Sstevel@tonic-gate #define	MSG3	"remove_dir() failed to unlink(\"%s\") "
67797c478bd9Sstevel@tonic-gate 
67807c478bd9Sstevel@tonic-gate 	/*
67817c478bd9Sstevel@tonic-gate 	 * Open the directory for reading.
67827c478bd9Sstevel@tonic-gate 	 */
67837c478bd9Sstevel@tonic-gate 	if ((name = opendir(path)) == NULL) {
67847c478bd9Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to opendir(\"%s\") ", path);
67857c478bd9Sstevel@tonic-gate 		return (-1);
67867c478bd9Sstevel@tonic-gate 	}
67877c478bd9Sstevel@tonic-gate 
67887c478bd9Sstevel@tonic-gate 	if (chdir(path) == -1) {
67897c478bd9Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to chdir(\"%s\") ", path);
67907c478bd9Sstevel@tonic-gate 		return (-1);
67917c478bd9Sstevel@tonic-gate 	}
67927c478bd9Sstevel@tonic-gate 
67937c478bd9Sstevel@tonic-gate 	/*
67947c478bd9Sstevel@tonic-gate 	 * Read every directory entry.
67957c478bd9Sstevel@tonic-gate 	 */
67967c478bd9Sstevel@tonic-gate 	while ((direct = readdir(name)) != NULL) {
67977c478bd9Sstevel@tonic-gate 		/*
67987c478bd9Sstevel@tonic-gate 		 * Ignore "." and ".." entries.
67997c478bd9Sstevel@tonic-gate 		 */
68007c478bd9Sstevel@tonic-gate 		if (strcmp(direct->d_name, ".") == 0 ||
68017c478bd9Sstevel@tonic-gate 		    strcmp(direct->d_name, "..") == 0)
68027c478bd9Sstevel@tonic-gate 			continue;
68037c478bd9Sstevel@tonic-gate 
68047c478bd9Sstevel@tonic-gate 			if (lstat(direct->d_name, &sbuf) == -1) {
68057c478bd9Sstevel@tonic-gate 				msg(ERRN, MSG1, direct->d_name);
68067c478bd9Sstevel@tonic-gate 				(void) closedir(name);
68077c478bd9Sstevel@tonic-gate 			return (-1);
68087c478bd9Sstevel@tonic-gate 		}
68097c478bd9Sstevel@tonic-gate 
68107c478bd9Sstevel@tonic-gate 		if (S_ISDIR(sbuf.st_mode)) {
68117c478bd9Sstevel@tonic-gate 			if (remove_dir(direct->d_name) == -1) {
68127c478bd9Sstevel@tonic-gate 				msg(ERRN, MSG2, direct->d_name);
68137c478bd9Sstevel@tonic-gate 				(void) closedir(name);
68147c478bd9Sstevel@tonic-gate 				return (-1);
68157c478bd9Sstevel@tonic-gate 			}
68167c478bd9Sstevel@tonic-gate 		} else {
68177c478bd9Sstevel@tonic-gate 			if (unlink(direct->d_name) == -1) {
68187c478bd9Sstevel@tonic-gate 				msg(ERRN, MSG3, direct->d_name);
68197c478bd9Sstevel@tonic-gate 				(void) closedir(name);
68207c478bd9Sstevel@tonic-gate 				return (-1);
68217c478bd9Sstevel@tonic-gate 			}
68227c478bd9Sstevel@tonic-gate 		}
68237c478bd9Sstevel@tonic-gate 
68247c478bd9Sstevel@tonic-gate 	}
68257c478bd9Sstevel@tonic-gate 
68267c478bd9Sstevel@tonic-gate 	/*
68277c478bd9Sstevel@tonic-gate 	 * Close the directory we just finished reading.
68287c478bd9Sstevel@tonic-gate 	 */
68297c478bd9Sstevel@tonic-gate 	(void) closedir(name);
68307c478bd9Sstevel@tonic-gate 
68317c478bd9Sstevel@tonic-gate 	/*
68327c478bd9Sstevel@tonic-gate 	 * Change directory to the parent directory...
68337c478bd9Sstevel@tonic-gate 	 */
68347c478bd9Sstevel@tonic-gate 	if (chdir("..") == -1) {
68357c478bd9Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to chdir(\"..\") ");
68367c478bd9Sstevel@tonic-gate 		return (-1);
68377c478bd9Sstevel@tonic-gate 	}
68387c478bd9Sstevel@tonic-gate 
68397c478bd9Sstevel@tonic-gate 	/*
68407c478bd9Sstevel@tonic-gate 	 * ...and finally remove the directory; note we have to
68417c478bd9Sstevel@tonic-gate 	 * make a copy since basename is free to modify its input.
68427c478bd9Sstevel@tonic-gate 	 */
68437c478bd9Sstevel@tonic-gate 	path_copy = e_strdup(E_NORMAL, path);
68447c478bd9Sstevel@tonic-gate 	if (path_copy == NULL) {
68457c478bd9Sstevel@tonic-gate 		msg(ERRN, "cannot strdup() the directory pathname ");
68467c478bd9Sstevel@tonic-gate 		return (-1);
68477c478bd9Sstevel@tonic-gate 	}
68487c478bd9Sstevel@tonic-gate 
68497c478bd9Sstevel@tonic-gate 	if (rmdir(basename(path_copy)) == -1) {
68507c478bd9Sstevel@tonic-gate 		free(path_copy);
68517c478bd9Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to rmdir(\"%s\") ", path);
68527c478bd9Sstevel@tonic-gate 		return (-1);
68537c478bd9Sstevel@tonic-gate 	}
68547c478bd9Sstevel@tonic-gate 
68557c478bd9Sstevel@tonic-gate 	free(path_copy);
68567c478bd9Sstevel@tonic-gate 	return (0);
68577c478bd9Sstevel@tonic-gate 
68587c478bd9Sstevel@tonic-gate }
68597c478bd9Sstevel@tonic-gate 
68607c478bd9Sstevel@tonic-gate static int
68617c478bd9Sstevel@tonic-gate save_cwd(void)
68627c478bd9Sstevel@tonic-gate {
68637c478bd9Sstevel@tonic-gate 	return (open(".", O_RDONLY));
68647c478bd9Sstevel@tonic-gate }
68657c478bd9Sstevel@tonic-gate 
68667c478bd9Sstevel@tonic-gate static void
68677c478bd9Sstevel@tonic-gate rest_cwd(int cwd)
68687c478bd9Sstevel@tonic-gate {
68697c478bd9Sstevel@tonic-gate 	(void) fchdir(cwd);
68707c478bd9Sstevel@tonic-gate 	(void) close(cwd);
68717c478bd9Sstevel@tonic-gate }
68727c478bd9Sstevel@tonic-gate 
68737c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
68747c478bd9Sstevel@tonic-gate static void
68757c478bd9Sstevel@tonic-gate xattrs_out(int (*func)())
68767c478bd9Sstevel@tonic-gate {
68777c478bd9Sstevel@tonic-gate 	int dirpfd;
68787c478bd9Sstevel@tonic-gate 	int filefd;
68797c478bd9Sstevel@tonic-gate 	DIR *dirp;
68807c478bd9Sstevel@tonic-gate 	struct dirent *dp;
68817c478bd9Sstevel@tonic-gate 	int slen;
68827c478bd9Sstevel@tonic-gate 	char *namep, *savenamep;
68837c478bd9Sstevel@tonic-gate 
68847c478bd9Sstevel@tonic-gate 	if (pathconf(G_p->g_nam_p, _PC_XATTR_EXISTS) != 1) {
68857c478bd9Sstevel@tonic-gate 		return;
68867c478bd9Sstevel@tonic-gate 	}
68877c478bd9Sstevel@tonic-gate 
68887c478bd9Sstevel@tonic-gate 	/*
68897c478bd9Sstevel@tonic-gate 	 * If aclp still exists then free it since it is was set when base
68907c478bd9Sstevel@tonic-gate 	 * file was extracted.
68917c478bd9Sstevel@tonic-gate 	 */
6892fa9e4066Sahrens 	if (aclp != NULL) {
6893fa9e4066Sahrens 		acl_free(aclp);
68947c478bd9Sstevel@tonic-gate 		aclp = NULL;
6895fa9e4066Sahrens 		acl_is_set = 0;
68967c478bd9Sstevel@tonic-gate 	}
68977c478bd9Sstevel@tonic-gate 
68987c478bd9Sstevel@tonic-gate 	Gen.g_dirfd = attropen(G_p->g_nam_p, ".", O_RDONLY);
68997c478bd9Sstevel@tonic-gate 	if (Gen.g_dirfd == -1) {
69007c478bd9Sstevel@tonic-gate 		msg(ERRN, "Cannot open attribute directory of file \"%s\"",
69017c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p);
69027c478bd9Sstevel@tonic-gate 		return;
69037c478bd9Sstevel@tonic-gate 
69047c478bd9Sstevel@tonic-gate 	}
69057c478bd9Sstevel@tonic-gate 	savenamep = G_p->g_nam_p;
69067c478bd9Sstevel@tonic-gate 
69077c478bd9Sstevel@tonic-gate 	if ((dirpfd = dup(Gen.g_dirfd)) == -1)  {
69087c478bd9Sstevel@tonic-gate 		msg(ERRN, "Cannot dup(2) attribute directory descriptor");
69097c478bd9Sstevel@tonic-gate 		return;
69107c478bd9Sstevel@tonic-gate 	}
69117c478bd9Sstevel@tonic-gate 
69127c478bd9Sstevel@tonic-gate 	if ((dirp = fdopendir(dirpfd)) == (DIR *)NULL) {
69137c478bd9Sstevel@tonic-gate 		msg(ERRN, "Cannot fdopendir(2) directory file descriptor");
69147c478bd9Sstevel@tonic-gate 		return;
69157c478bd9Sstevel@tonic-gate 	}
69167c478bd9Sstevel@tonic-gate 
69177c478bd9Sstevel@tonic-gate 	while ((dp = readdir(dirp)) != (struct dirent *)NULL) {
6918*da6c28aaSamw 		if ((strcmp(dp->d_name, "..") == 0) ||
6919*da6c28aaSamw 		    is_sysattr(dp->d_name)) {
69207c478bd9Sstevel@tonic-gate 			continue;
69217c478bd9Sstevel@tonic-gate 		}
69227c478bd9Sstevel@tonic-gate 
69237c478bd9Sstevel@tonic-gate 		if (strcmp(dp->d_name, ".") == 0) {
69247c478bd9Sstevel@tonic-gate 			Hiddendir = 1;
69257c478bd9Sstevel@tonic-gate 		} else {
69267c478bd9Sstevel@tonic-gate 			Hiddendir = 0;
69277c478bd9Sstevel@tonic-gate 		}
69287c478bd9Sstevel@tonic-gate 
69297c478bd9Sstevel@tonic-gate 		Gen.g_attrnam_p = dp->d_name;
69307c478bd9Sstevel@tonic-gate 
69317c478bd9Sstevel@tonic-gate 		if (STAT(Gen.g_dirfd, Gen.g_nam_p, &SrcSt) == -1) {
69327c478bd9Sstevel@tonic-gate 			msg(ERRN,
69337c478bd9Sstevel@tonic-gate 			    "Could not fstatat(2) attribute \"%s\" of"
69347c478bd9Sstevel@tonic-gate 			    " file \"%s\"", dp->d_name, savenamep);
69357c478bd9Sstevel@tonic-gate 			continue;
69367c478bd9Sstevel@tonic-gate 		}
69377c478bd9Sstevel@tonic-gate 
69387c478bd9Sstevel@tonic-gate 		if (Use_old_stat) {
69397c478bd9Sstevel@tonic-gate 			OldSt = convert_to_old_stat(&SrcSt,
69407c478bd9Sstevel@tonic-gate 			    Gen.g_nam_p, Gen.g_attrnam_p);
69417c478bd9Sstevel@tonic-gate 
69427c478bd9Sstevel@tonic-gate 			if (OldSt == NULL) {
69437c478bd9Sstevel@tonic-gate 				msg(ERRN,
69447c478bd9Sstevel@tonic-gate 				    "Could not convert to old stat format");
69457c478bd9Sstevel@tonic-gate 				continue;
69467c478bd9Sstevel@tonic-gate 			}
69477c478bd9Sstevel@tonic-gate 		}
69487c478bd9Sstevel@tonic-gate 
69497c478bd9Sstevel@tonic-gate 		Gen.g_attrfnam_p = savenamep;
69507c478bd9Sstevel@tonic-gate 
69517c478bd9Sstevel@tonic-gate 		/*
69527c478bd9Sstevel@tonic-gate 		 * Set up dummy header name
69537c478bd9Sstevel@tonic-gate 		 *
69547c478bd9Sstevel@tonic-gate 		 * One piece is written with .hdr, which
69557c478bd9Sstevel@tonic-gate 		 * contains the actual xattr hdr or pathing information
69567c478bd9Sstevel@tonic-gate 		 * then the name is updated to drop the .hdr off
69577c478bd9Sstevel@tonic-gate 		 * and the actual file itself is archived.
69587c478bd9Sstevel@tonic-gate 		 */
69597c478bd9Sstevel@tonic-gate 		slen = strlen(Gen.g_attrnam_p) + strlen(DEVNULL) +
69607c478bd9Sstevel@tonic-gate 		    strlen(XATTRHDR) + 1;
69617c478bd9Sstevel@tonic-gate 		if ((namep = e_zalloc(E_NORMAL, slen)) == (char *)NULL) {
69627c478bd9Sstevel@tonic-gate 			msg(ERRN, "Could not calloc memory for attribute name");
69637c478bd9Sstevel@tonic-gate 			continue;
69647c478bd9Sstevel@tonic-gate 		}
69657c478bd9Sstevel@tonic-gate 		(void) sprintf(namep, "%s/%s%s",
69667c478bd9Sstevel@tonic-gate 		    DEVNULL, Gen.g_attrnam_p, XATTRHDR);
69677c478bd9Sstevel@tonic-gate 		Gen.g_nam_p = namep;
69687c478bd9Sstevel@tonic-gate 
69697c478bd9Sstevel@tonic-gate 		/*
69707c478bd9Sstevel@tonic-gate 		 * Get attribute's ACL info: don't bother allocating space
69717c478bd9Sstevel@tonic-gate 		 * if there are only standard permissions, i.e. ACL count < 4
69727c478bd9Sstevel@tonic-gate 		 */
69737c478bd9Sstevel@tonic-gate 		if (Pflag) {
69747c478bd9Sstevel@tonic-gate 			filefd = openat(Gen.g_dirfd, dp->d_name, O_RDONLY);
69757c478bd9Sstevel@tonic-gate 			if (filefd == -1) {
69767c478bd9Sstevel@tonic-gate 				msg(ERRN,
69777c478bd9Sstevel@tonic-gate 				    "Could not open attribute \"%s\" of"
69787c478bd9Sstevel@tonic-gate 				    " file \"%s\"", dp->d_name, savenamep);
69797c478bd9Sstevel@tonic-gate 				free(namep);
69807c478bd9Sstevel@tonic-gate 				continue;
69817c478bd9Sstevel@tonic-gate 			}
6982fa9e4066Sahrens 			if (facl_get(filefd, ACL_NO_TRIVIAL, &aclp) != 0) {
69837c478bd9Sstevel@tonic-gate 				msg(ERRN,
69847c478bd9Sstevel@tonic-gate 				    "Error with acl() on %s",
69857c478bd9Sstevel@tonic-gate 				    Gen.g_nam_p);
69867c478bd9Sstevel@tonic-gate 			}
69877c478bd9Sstevel@tonic-gate 			(void) close(filefd);
69887c478bd9Sstevel@tonic-gate 		}
69897c478bd9Sstevel@tonic-gate 		(void) creat_hdr();
69907c478bd9Sstevel@tonic-gate 		(void) (*func)();
69917c478bd9Sstevel@tonic-gate 		if (Gen.g_passdirfd != -1) {
69927c478bd9Sstevel@tonic-gate 			(void) close(Gen.g_passdirfd);
69937c478bd9Sstevel@tonic-gate 			Gen.g_passdirfd = -1;
69947c478bd9Sstevel@tonic-gate 		}
69957c478bd9Sstevel@tonic-gate 		Gen.g_attrnam_p = (char *)NULL;
69967c478bd9Sstevel@tonic-gate 		Gen.g_attrfnam_p = (char *)NULL;
69977c478bd9Sstevel@tonic-gate 		Gen.g_linktoattrfnam_p = (char *)NULL;
69987c478bd9Sstevel@tonic-gate 		Gen.g_linktoattrnam_p = (char *)NULL;
6999fa9e4066Sahrens 		if (aclp != NULL) {
7000fa9e4066Sahrens 			acl_free(aclp);
70017c478bd9Sstevel@tonic-gate 			aclp = NULL;
7002fa9e4066Sahrens 			acl_is_set = 0;
70037c478bd9Sstevel@tonic-gate 		}
70047c478bd9Sstevel@tonic-gate 		free(namep);
70057c478bd9Sstevel@tonic-gate 	}
70067c478bd9Sstevel@tonic-gate 
70077c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
70087c478bd9Sstevel@tonic-gate 	(void) close(Gen.g_dirfd);
70097c478bd9Sstevel@tonic-gate 	Gen.g_dirfd = -1;
70107c478bd9Sstevel@tonic-gate }
70117c478bd9Sstevel@tonic-gate #else
70127c478bd9Sstevel@tonic-gate static void
70137c478bd9Sstevel@tonic-gate xattrs_out(int (*func)())
70147c478bd9Sstevel@tonic-gate {
70157c478bd9Sstevel@tonic-gate }
70167c478bd9Sstevel@tonic-gate #endif
70177c478bd9Sstevel@tonic-gate 
70187c478bd9Sstevel@tonic-gate /*
70197c478bd9Sstevel@tonic-gate  * Return the parent directory of a given path.
70207c478bd9Sstevel@tonic-gate  *
70217c478bd9Sstevel@tonic-gate  * Examples:
70227c478bd9Sstevel@tonic-gate  * /usr/tmp return /usr
70237c478bd9Sstevel@tonic-gate  * /usr/tmp/file return /usr/tmp
70247c478bd9Sstevel@tonic-gate  * /  returns .
70257c478bd9Sstevel@tonic-gate  * /usr returns /
70267c478bd9Sstevel@tonic-gate  * file returns .
70277c478bd9Sstevel@tonic-gate  *
70287c478bd9Sstevel@tonic-gate  * dir is assumed to be at least as big as path.
70297c478bd9Sstevel@tonic-gate  */
70307c478bd9Sstevel@tonic-gate static void
70317c478bd9Sstevel@tonic-gate get_parent(char *path, char *dir)
70327c478bd9Sstevel@tonic-gate {
70337c478bd9Sstevel@tonic-gate 	char *s;
70347c478bd9Sstevel@tonic-gate 	char tmpdir[PATH_MAX + 1];
70357c478bd9Sstevel@tonic-gate 
70367c478bd9Sstevel@tonic-gate 	if (strlen(path) > PATH_MAX) {
70377c478bd9Sstevel@tonic-gate 		msg(EXT, "pathname is too long");
70387c478bd9Sstevel@tonic-gate 	}
70397c478bd9Sstevel@tonic-gate 	(void) strcpy(tmpdir, path);
70407c478bd9Sstevel@tonic-gate 	chop_endslashes(tmpdir);
70417c478bd9Sstevel@tonic-gate 
70427c478bd9Sstevel@tonic-gate 	if ((s = strrchr(tmpdir, '/')) == NULL) {
70437c478bd9Sstevel@tonic-gate 		(void) strcpy(dir, ".");
70447c478bd9Sstevel@tonic-gate 	} else {
70457c478bd9Sstevel@tonic-gate 		s = skipslashes(s, tmpdir);
70467c478bd9Sstevel@tonic-gate 		*s = '\0';
70477c478bd9Sstevel@tonic-gate 		if (s == tmpdir)
70487c478bd9Sstevel@tonic-gate 			(void) strcpy(dir, "/");
70497c478bd9Sstevel@tonic-gate 		else
70507c478bd9Sstevel@tonic-gate 			(void) strcpy(dir, tmpdir);
70517c478bd9Sstevel@tonic-gate 	}
70527c478bd9Sstevel@tonic-gate }
70537c478bd9Sstevel@tonic-gate 
70547c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
70557c478bd9Sstevel@tonic-gate #define	ROUNDTOTBLOCK(a)		((a + (TBLOCK -1)) & ~(TBLOCK -1))
70567c478bd9Sstevel@tonic-gate 
70577c478bd9Sstevel@tonic-gate static void
70587c478bd9Sstevel@tonic-gate prepare_xattr_hdr(
70597c478bd9Sstevel@tonic-gate 	char		**attrbuf,
70607c478bd9Sstevel@tonic-gate 	char		*filename,
70617c478bd9Sstevel@tonic-gate 	char		*attrname,
70627c478bd9Sstevel@tonic-gate 	char		typeflag,
70637c478bd9Sstevel@tonic-gate 	struct Lnk	*linkinfo,
70647c478bd9Sstevel@tonic-gate 	int		*rlen)
70657c478bd9Sstevel@tonic-gate {
70667c478bd9Sstevel@tonic-gate 	char			*bufhead;	/* ptr to full buffer */
70677c478bd9Sstevel@tonic-gate 	struct xattr_hdr 	*hptr;		/* ptr to header in bufhead */
70687c478bd9Sstevel@tonic-gate 	struct xattr_buf	*tptr;		/* ptr to pathing pieces */
70697c478bd9Sstevel@tonic-gate 	int			totalen;	/* total buffer length */
70707c478bd9Sstevel@tonic-gate 	int			len;		/* length returned to user */
70717c478bd9Sstevel@tonic-gate 	int			stringlen;	/* length of filename + attr */
70727c478bd9Sstevel@tonic-gate 	int			linkstringlen;	/* ditto in link section */
70737c478bd9Sstevel@tonic-gate 	int			complen;	/* length of pathing section */
70747c478bd9Sstevel@tonic-gate 	int			linklen;	/* length of link section */
70757c478bd9Sstevel@tonic-gate 
70767c478bd9Sstevel@tonic-gate 
70777c478bd9Sstevel@tonic-gate 	/*
70787c478bd9Sstevel@tonic-gate 	 * Release previous buffer if any.
70797c478bd9Sstevel@tonic-gate 	 */
70807c478bd9Sstevel@tonic-gate 
70817c478bd9Sstevel@tonic-gate 	if (*attrbuf != (char *)NULL) {
70827c478bd9Sstevel@tonic-gate 		free(*attrbuf);
70837c478bd9Sstevel@tonic-gate 		*attrbuf = NULL;
70847c478bd9Sstevel@tonic-gate 	}
70857c478bd9Sstevel@tonic-gate 
70867c478bd9Sstevel@tonic-gate 	/*
70877c478bd9Sstevel@tonic-gate 	 * First add in fixed size stuff
70887c478bd9Sstevel@tonic-gate 	 */
70897c478bd9Sstevel@tonic-gate 	len = sizeof (struct xattr_hdr) + sizeof (struct xattr_buf);
70907c478bd9Sstevel@tonic-gate 
70917c478bd9Sstevel@tonic-gate 	/*
70927c478bd9Sstevel@tonic-gate 	 * Add space for two nulls
70937c478bd9Sstevel@tonic-gate 	 */
70947c478bd9Sstevel@tonic-gate 	stringlen = strlen(attrname) + strlen(filename) + 2;
70957c478bd9Sstevel@tonic-gate 	complen = stringlen + sizeof (struct xattr_buf);
70967c478bd9Sstevel@tonic-gate 
70977c478bd9Sstevel@tonic-gate 	len += stringlen;
70987c478bd9Sstevel@tonic-gate 
70997c478bd9Sstevel@tonic-gate 	/*
71007c478bd9Sstevel@tonic-gate 	 * Now add on space for link info if any
71017c478bd9Sstevel@tonic-gate 	 */
71027c478bd9Sstevel@tonic-gate 
71037c478bd9Sstevel@tonic-gate 	if (linkinfo != NULL) {
71047c478bd9Sstevel@tonic-gate 		/*
71057c478bd9Sstevel@tonic-gate 		 * Again add space for two nulls
71067c478bd9Sstevel@tonic-gate 		 */
71077c478bd9Sstevel@tonic-gate 		linkstringlen = strlen(linkinfo->L_gen.g_attrfnam_p) +
71087c478bd9Sstevel@tonic-gate 		    strlen(linkinfo->L_gen.g_attrnam_p) + 2;
71097c478bd9Sstevel@tonic-gate 		len += linkstringlen;
71107c478bd9Sstevel@tonic-gate 	}
71117c478bd9Sstevel@tonic-gate 
71127c478bd9Sstevel@tonic-gate 	/*
71137c478bd9Sstevel@tonic-gate 	 * Now add padding to end to fill out TBLOCK
71147c478bd9Sstevel@tonic-gate 	 *
71157c478bd9Sstevel@tonic-gate 	 * Function returns size of real data and not size + padding.
71167c478bd9Sstevel@tonic-gate 	 */
71177c478bd9Sstevel@tonic-gate 
71187c478bd9Sstevel@tonic-gate 	totalen = ROUNDTOTBLOCK(len);
71197c478bd9Sstevel@tonic-gate 	bufhead = e_zalloc(E_EXIT, totalen);
71207c478bd9Sstevel@tonic-gate 
71217c478bd9Sstevel@tonic-gate 	/*
71227c478bd9Sstevel@tonic-gate 	 * Now we can fill in the necessary pieces
71237c478bd9Sstevel@tonic-gate 	 */
71247c478bd9Sstevel@tonic-gate 
71257c478bd9Sstevel@tonic-gate 	if (linkinfo != (struct Lnk *)NULL) {
71267c478bd9Sstevel@tonic-gate 		linklen = linkstringlen + (sizeof (struct xattr_buf));
71277c478bd9Sstevel@tonic-gate 	} else {
71287c478bd9Sstevel@tonic-gate 		linklen = 0;
71297c478bd9Sstevel@tonic-gate 	}
71307c478bd9Sstevel@tonic-gate 
71317c478bd9Sstevel@tonic-gate 	/*
71327c478bd9Sstevel@tonic-gate 	 * first fill in the fixed header
71337c478bd9Sstevel@tonic-gate 	 */
71347c478bd9Sstevel@tonic-gate 	hptr = (struct xattr_hdr *)bufhead;
71357c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_version, "%s", XATTR_ARCH_VERS);
71367c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_component_len, "%0*d",
71377c478bd9Sstevel@tonic-gate 	    sizeof (hptr->h_component_len) - 1, complen);
71387c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_link_component_len, "%0*d",
71397c478bd9Sstevel@tonic-gate 	    sizeof (hptr->h_link_component_len) - 1, linklen);
71407c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_size, "%0*d", sizeof (hptr->h_size) - 1, len);
71417c478bd9Sstevel@tonic-gate 
71427c478bd9Sstevel@tonic-gate 	/*
71437c478bd9Sstevel@tonic-gate 	 * Now fill in the filename + attrnames section
71447c478bd9Sstevel@tonic-gate 	 */
71457c478bd9Sstevel@tonic-gate 
71467c478bd9Sstevel@tonic-gate 	tptr = (struct xattr_buf *)(bufhead + sizeof (struct xattr_hdr));
71477c478bd9Sstevel@tonic-gate 	(void) sprintf(tptr->h_namesz, "%0*d", sizeof (tptr->h_namesz) - 1,
71487c478bd9Sstevel@tonic-gate 	    stringlen);
71497c478bd9Sstevel@tonic-gate 	(void) strcpy(tptr->h_names, filename);
71507c478bd9Sstevel@tonic-gate 	(void) strcpy(&tptr->h_names[strlen(filename) + 1], attrname);
71517c478bd9Sstevel@tonic-gate 	tptr->h_typeflag = typeflag;
71527c478bd9Sstevel@tonic-gate 
71537c478bd9Sstevel@tonic-gate 	/*
71547c478bd9Sstevel@tonic-gate 	 * Now fill in the optional link section if we have one
71557c478bd9Sstevel@tonic-gate 	 */
71567c478bd9Sstevel@tonic-gate 
71577c478bd9Sstevel@tonic-gate 	if (linkinfo != (struct Lnk *)NULL) {
71587c478bd9Sstevel@tonic-gate 		tptr = (struct xattr_buf *)(bufhead +
71597c478bd9Sstevel@tonic-gate 		    sizeof (struct xattr_hdr) + complen);
71607c478bd9Sstevel@tonic-gate 
71617c478bd9Sstevel@tonic-gate 		(void) sprintf(tptr->h_namesz, "%0*d",
71627c478bd9Sstevel@tonic-gate 		    sizeof (tptr->h_namesz) - 1, linkstringlen);
71637c478bd9Sstevel@tonic-gate 		(void) strcpy(tptr->h_names, linkinfo->L_gen.g_attrfnam_p);
71647c478bd9Sstevel@tonic-gate 		(void) strcpy(
71657c478bd9Sstevel@tonic-gate 		    &tptr->h_names[strlen(linkinfo->L_gen.g_attrfnam_p) + 1],
71667c478bd9Sstevel@tonic-gate 		    linkinfo->L_gen.g_attrnam_p);
71677c478bd9Sstevel@tonic-gate 		tptr->h_typeflag = typeflag;
71687c478bd9Sstevel@tonic-gate 	}
71697c478bd9Sstevel@tonic-gate 	*attrbuf = (char *)bufhead;
71707c478bd9Sstevel@tonic-gate 	*rlen = len;
71717c478bd9Sstevel@tonic-gate }
71727c478bd9Sstevel@tonic-gate #endif /* O_XATTR */
71737c478bd9Sstevel@tonic-gate 
71747c478bd9Sstevel@tonic-gate static char
71757c478bd9Sstevel@tonic-gate tartype(int type)
71767c478bd9Sstevel@tonic-gate {
71777c478bd9Sstevel@tonic-gate 	switch (type) {
71787c478bd9Sstevel@tonic-gate 
71797c478bd9Sstevel@tonic-gate 	case S_IFDIR:
71807c478bd9Sstevel@tonic-gate 		return (DIRTYPE);
71817c478bd9Sstevel@tonic-gate 
71827c478bd9Sstevel@tonic-gate 	case S_IFLNK:
71837c478bd9Sstevel@tonic-gate 		return (SYMTYPE);
71847c478bd9Sstevel@tonic-gate 
71857c478bd9Sstevel@tonic-gate 	case S_IFIFO:
71867c478bd9Sstevel@tonic-gate 		return (FIFOTYPE);
71877c478bd9Sstevel@tonic-gate 
71887c478bd9Sstevel@tonic-gate 	case S_IFCHR:
71897c478bd9Sstevel@tonic-gate 		return (CHRTYPE);
71907c478bd9Sstevel@tonic-gate 
71917c478bd9Sstevel@tonic-gate 	case S_IFBLK:
71927c478bd9Sstevel@tonic-gate 		return (BLKTYPE);
71937c478bd9Sstevel@tonic-gate 
71947c478bd9Sstevel@tonic-gate 	case S_IFREG:
71957c478bd9Sstevel@tonic-gate 		return (REGTYPE);
71967c478bd9Sstevel@tonic-gate 
71977c478bd9Sstevel@tonic-gate 	default:
71987c478bd9Sstevel@tonic-gate 		return ('\0');
71997c478bd9Sstevel@tonic-gate 	}
72007c478bd9Sstevel@tonic-gate }
72017c478bd9Sstevel@tonic-gate 
72027c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
72037c478bd9Sstevel@tonic-gate static int
72047c478bd9Sstevel@tonic-gate openfile(int omode)
72057c478bd9Sstevel@tonic-gate {
72067c478bd9Sstevel@tonic-gate 
72077c478bd9Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
72087c478bd9Sstevel@tonic-gate 		return (attropen(G_p->g_attrfnam_p, G_p->g_attrnam_p, omode));
72097c478bd9Sstevel@tonic-gate 	} else {
72107c478bd9Sstevel@tonic-gate 		return (openat(G_p->g_dirfd,
72117c478bd9Sstevel@tonic-gate 		    get_component(G_p->g_nam_p), omode));
72127c478bd9Sstevel@tonic-gate 	}
72137c478bd9Sstevel@tonic-gate }
72147c478bd9Sstevel@tonic-gate #else
72157c478bd9Sstevel@tonic-gate static int
72167c478bd9Sstevel@tonic-gate openfile(int omode)
72177c478bd9Sstevel@tonic-gate {
72187c478bd9Sstevel@tonic-gate 	return (openat(G_p->g_dirfd, get_component(G_p->g_nam_p), omode));
72197c478bd9Sstevel@tonic-gate }
72207c478bd9Sstevel@tonic-gate #endif
72217c478bd9Sstevel@tonic-gate 
72227c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
72237c478bd9Sstevel@tonic-gate static int
72247c478bd9Sstevel@tonic-gate read_xattr_hdr()
72257c478bd9Sstevel@tonic-gate {
72267c478bd9Sstevel@tonic-gate 	off_t		bytes;
72277c478bd9Sstevel@tonic-gate 	int		comp_len, link_len;
72287c478bd9Sstevel@tonic-gate 	int		namelen;
7229*da6c28aaSamw 	int		asz;
72307c478bd9Sstevel@tonic-gate 	int		cnt;
72317c478bd9Sstevel@tonic-gate 	char		*tp;
7232*da6c28aaSamw 	char		*xattrapath;
72337c478bd9Sstevel@tonic-gate 	int		pad;
7234*da6c28aaSamw 	int		parentfilelen;
72357c478bd9Sstevel@tonic-gate 
72367c478bd9Sstevel@tonic-gate 	/*
72377c478bd9Sstevel@tonic-gate 	 * Include any padding in the read.  We need to be positioned
72387c478bd9Sstevel@tonic-gate 	 * at beginning of next header.
72397c478bd9Sstevel@tonic-gate 	 */
72407c478bd9Sstevel@tonic-gate 
72417c478bd9Sstevel@tonic-gate 	bytes = Gen.g_filesz;
72427c478bd9Sstevel@tonic-gate 
72437c478bd9Sstevel@tonic-gate 	if ((xattrhead = e_zalloc(E_NORMAL, (size_t)bytes)) == NULL) {
72447c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
72457c478bd9Sstevel@tonic-gate 		    "Insufficient memory for extended attribute\n"));
72467c478bd9Sstevel@tonic-gate 		return (1);
72477c478bd9Sstevel@tonic-gate 	}
72487c478bd9Sstevel@tonic-gate 
72497c478bd9Sstevel@tonic-gate 	tp = (char *)xattrhead;
72507c478bd9Sstevel@tonic-gate 	while (bytes > 0) {
72517c478bd9Sstevel@tonic-gate 		cnt = (int)(bytes > CPIOBSZ) ? CPIOBSZ : bytes;
72527c478bd9Sstevel@tonic-gate 		FILL(cnt);
72537c478bd9Sstevel@tonic-gate 		(void) memcpy(tp, Buffr.b_out_p, cnt);
72547c478bd9Sstevel@tonic-gate 		tp += cnt;
72557c478bd9Sstevel@tonic-gate 		Buffr.b_out_p += cnt;
72567c478bd9Sstevel@tonic-gate 		Buffr.b_cnt -= (off_t)cnt;
72577c478bd9Sstevel@tonic-gate 		bytes -= (off_t)cnt;
72587c478bd9Sstevel@tonic-gate 	}
72597c478bd9Sstevel@tonic-gate 
72607c478bd9Sstevel@tonic-gate 	pad = (Pad_val + 1 - (Gen.g_filesz & Pad_val)) &
72617c478bd9Sstevel@tonic-gate 	    Pad_val;
72627c478bd9Sstevel@tonic-gate 	if (pad != 0) {
72637c478bd9Sstevel@tonic-gate 		FILL(pad);
72647c478bd9Sstevel@tonic-gate 		Buffr.b_out_p += pad;
72657c478bd9Sstevel@tonic-gate 		Buffr.b_cnt -= (off_t)pad;
72667c478bd9Sstevel@tonic-gate 	}
72677c478bd9Sstevel@tonic-gate 
72687c478bd9Sstevel@tonic-gate 	/*
72697c478bd9Sstevel@tonic-gate 	 * Validate that we can handle header format
72707c478bd9Sstevel@tonic-gate 	 */
72717c478bd9Sstevel@tonic-gate 
72727c478bd9Sstevel@tonic-gate 	if (strcmp(xattrhead->h_version, XATTR_ARCH_VERS) != 0) {
72737c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
72747c478bd9Sstevel@tonic-gate 		    gettext("Unknown extended attribute format encountered\n"));
72757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
72767c478bd9Sstevel@tonic-gate 		    gettext("Disabling extended attribute header parsing\n"));
72777c478bd9Sstevel@tonic-gate 		xattrbadhead = 1;
72787c478bd9Sstevel@tonic-gate 		return (1);
72797c478bd9Sstevel@tonic-gate 	}
72807c478bd9Sstevel@tonic-gate 	(void) sscanf(xattrhead->h_component_len, "%10d", &comp_len);
72817c478bd9Sstevel@tonic-gate 	(void) sscanf(xattrhead->h_link_component_len, "%10d", &link_len);
72827c478bd9Sstevel@tonic-gate 	xattrp = (struct xattr_buf *)(((char *)xattrhead) +
72837c478bd9Sstevel@tonic-gate 	    sizeof (struct xattr_hdr));
72847c478bd9Sstevel@tonic-gate 	(void) sscanf(xattrp->h_namesz, "%7d", &namelen);
72857c478bd9Sstevel@tonic-gate 	if (link_len > 0) {
72867c478bd9Sstevel@tonic-gate 		xattr_linkp = (struct xattr_buf *)((int)xattrp + (int)comp_len);
72877c478bd9Sstevel@tonic-gate 	} else {
72887c478bd9Sstevel@tonic-gate 		xattr_linkp = NULL;
72897c478bd9Sstevel@tonic-gate 	}
72907c478bd9Sstevel@tonic-gate 
7291*da6c28aaSamw 	/*
7292*da6c28aaSamw 	 * Gather the attribute path from the filename and attrnames section.
7293*da6c28aaSamw 	 * The filename and attrnames section can be composed of two or more
7294*da6c28aaSamw 	 * path segments separated by a null character.  The first segment
7295*da6c28aaSamw 	 * is the path to the parent file that roots the entire sequence in
7296*da6c28aaSamw 	 * the normal name space. The remaining segments describes a path
7297*da6c28aaSamw 	 * rooted at the hidden extended attribute directory of the leaf file of
7298*da6c28aaSamw 	 * the previous segment, making it possible to name attributes on
7299*da6c28aaSamw 	 * attributes.
7300*da6c28aaSamw 	 */
7301*da6c28aaSamw 	parentfilelen = strlen(xattrp->h_names);
7302*da6c28aaSamw 	xattrapath = xattrp->h_names + parentfilelen + 1;
7303*da6c28aaSamw 	asz = strlen(xattrapath);
7304*da6c28aaSamw 	if ((asz + parentfilelen + 2) < namelen) {
7305*da6c28aaSamw 		/*
7306*da6c28aaSamw 		 * The attrnames section contains a system attribute on an
7307*da6c28aaSamw 		 * attribute.  Save the name of the attribute for use later,
7308*da6c28aaSamw 		 * and replace the null separating the attribute name from
7309*da6c28aaSamw 		 * the system attribute name with a '/' so that xattrapath can
7310*da6c28aaSamw 		 * be used to display messages with the full attribute path name
7311*da6c28aaSamw 		 * rooted at the hidden attribute directory of the base file
7312*da6c28aaSamw 		 * in normal name space.
7313*da6c28aaSamw 		 */
7314*da6c28aaSamw 		xattrapath[asz] = '/';
7315*da6c28aaSamw 	}
7316*da6c28aaSamw 
73177c478bd9Sstevel@tonic-gate 	return (0);
73187c478bd9Sstevel@tonic-gate }
73197c478bd9Sstevel@tonic-gate #endif
73207c478bd9Sstevel@tonic-gate 
73217c478bd9Sstevel@tonic-gate static mode_t
73227c478bd9Sstevel@tonic-gate attrmode(char type)
73237c478bd9Sstevel@tonic-gate {
73247c478bd9Sstevel@tonic-gate 	mode_t mode;
73257c478bd9Sstevel@tonic-gate 
73267c478bd9Sstevel@tonic-gate 	switch (type) {
73277c478bd9Sstevel@tonic-gate 	case '\0':
73287c478bd9Sstevel@tonic-gate 	case REGTYPE:
73297c478bd9Sstevel@tonic-gate 	case LNKTYPE:
73307c478bd9Sstevel@tonic-gate 		mode = S_IFREG;
73317c478bd9Sstevel@tonic-gate 		break;
73327c478bd9Sstevel@tonic-gate 
73337c478bd9Sstevel@tonic-gate 	case SYMTYPE:
73347c478bd9Sstevel@tonic-gate 		mode = S_IFLNK;
73357c478bd9Sstevel@tonic-gate 		break;
73367c478bd9Sstevel@tonic-gate 
73377c478bd9Sstevel@tonic-gate 	case CHRTYPE:
73387c478bd9Sstevel@tonic-gate 		mode = S_IFCHR;
73397c478bd9Sstevel@tonic-gate 		break;
73407c478bd9Sstevel@tonic-gate 	case BLKTYPE:
73417c478bd9Sstevel@tonic-gate 		mode = S_IFBLK;
73427c478bd9Sstevel@tonic-gate 		break;
73437c478bd9Sstevel@tonic-gate 	case DIRTYPE:
73447c478bd9Sstevel@tonic-gate 		mode = S_IFDIR;
73457c478bd9Sstevel@tonic-gate 		break;
73467c478bd9Sstevel@tonic-gate 	case FIFOTYPE:
73477c478bd9Sstevel@tonic-gate 		mode = S_IFIFO;
73487c478bd9Sstevel@tonic-gate 		break;
73497c478bd9Sstevel@tonic-gate 	case CONTTYPE:
73507c478bd9Sstevel@tonic-gate 	default:
73517c478bd9Sstevel@tonic-gate 		mode = 0;
73527c478bd9Sstevel@tonic-gate 	}
73537c478bd9Sstevel@tonic-gate 
73547c478bd9Sstevel@tonic-gate 	return (mode);
73557c478bd9Sstevel@tonic-gate }
73567c478bd9Sstevel@tonic-gate 
73577c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
73587c478bd9Sstevel@tonic-gate static char *
73597c478bd9Sstevel@tonic-gate get_component(char *path)
73607c478bd9Sstevel@tonic-gate {
73617c478bd9Sstevel@tonic-gate 	char *ptr;
73627c478bd9Sstevel@tonic-gate 
73637c478bd9Sstevel@tonic-gate 	ptr = strrchr(path, '/');
73647c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
73657c478bd9Sstevel@tonic-gate 		return (path);
73667c478bd9Sstevel@tonic-gate 	} else {
73677c478bd9Sstevel@tonic-gate 		/*
73687c478bd9Sstevel@tonic-gate 		 * Handle trailing slash
73697c478bd9Sstevel@tonic-gate 		 */
73707c478bd9Sstevel@tonic-gate 		if (*(ptr + 1) == '\0')
73717c478bd9Sstevel@tonic-gate 			return (ptr);
73727c478bd9Sstevel@tonic-gate 		else
73737c478bd9Sstevel@tonic-gate 			return (ptr + 1);
73747c478bd9Sstevel@tonic-gate 	}
73757c478bd9Sstevel@tonic-gate }
73767c478bd9Sstevel@tonic-gate #else
73777c478bd9Sstevel@tonic-gate static char *
73787c478bd9Sstevel@tonic-gate get_component(char *path)
73797c478bd9Sstevel@tonic-gate {
73807c478bd9Sstevel@tonic-gate 	return (path);
73817c478bd9Sstevel@tonic-gate }
73827c478bd9Sstevel@tonic-gate #endif
73837c478bd9Sstevel@tonic-gate 
73847c478bd9Sstevel@tonic-gate static int
73857c478bd9Sstevel@tonic-gate open_dir(char *name)
73867c478bd9Sstevel@tonic-gate {
73877c478bd9Sstevel@tonic-gate 	int fd = -1;
73887c478bd9Sstevel@tonic-gate 	int cnt = 0;
73897c478bd9Sstevel@tonic-gate 	char *dir;
73907c478bd9Sstevel@tonic-gate 
73917c478bd9Sstevel@tonic-gate 	dir = e_zalloc(E_EXIT, strlen(name) + 1);
73927c478bd9Sstevel@tonic-gate 
73937c478bd9Sstevel@tonic-gate 	/*
73947c478bd9Sstevel@tonic-gate 	 * open directory; creating missing directories along the way.
73957c478bd9Sstevel@tonic-gate 	 */
73967c478bd9Sstevel@tonic-gate 	get_parent(name, dir);
73977c478bd9Sstevel@tonic-gate 	do {
73987c478bd9Sstevel@tonic-gate 		fd = open(dir, O_RDONLY);
73997c478bd9Sstevel@tonic-gate 		if (fd != -1) {
74007c478bd9Sstevel@tonic-gate 			free(dir);
74017c478bd9Sstevel@tonic-gate 			return (fd);
74027c478bd9Sstevel@tonic-gate 		}
74037c478bd9Sstevel@tonic-gate 		cnt++;
74047c478bd9Sstevel@tonic-gate 	} while (cnt <= 1 && missdir(name) == 0);
74057c478bd9Sstevel@tonic-gate 
74067c478bd9Sstevel@tonic-gate 	free(dir);
74077c478bd9Sstevel@tonic-gate 	return (-1);
74087c478bd9Sstevel@tonic-gate }
74097c478bd9Sstevel@tonic-gate 
74107c478bd9Sstevel@tonic-gate static int
74117c478bd9Sstevel@tonic-gate open_dirfd()
74127c478bd9Sstevel@tonic-gate {
74137c478bd9Sstevel@tonic-gate #ifdef O_XATTR
74147c478bd9Sstevel@tonic-gate 	if ((Args & OCt) == 0) {
74157c478bd9Sstevel@tonic-gate 		close_dirfd();
74167c478bd9Sstevel@tonic-gate 		if (G_p->g_attrnam_p != (char *)NULL) {
74177c478bd9Sstevel@tonic-gate 			G_p->g_dirfd = attropen(G_p->g_attrfnam_p,
74187c478bd9Sstevel@tonic-gate 			    ".", O_RDONLY);
74197c478bd9Sstevel@tonic-gate 			if (G_p->g_dirfd == -1 && (Args & (OCi | OCp))) {
74207c478bd9Sstevel@tonic-gate 				G_p->g_dirfd =
74217c478bd9Sstevel@tonic-gate 				    retry_attrdir_open(G_p->g_attrfnam_p);
74227c478bd9Sstevel@tonic-gate 				if (G_p->g_dirfd == -1) {
74237c478bd9Sstevel@tonic-gate 					msg(ERRN,
74247c478bd9Sstevel@tonic-gate 					    "Cannot open attribute"
74257c478bd9Sstevel@tonic-gate 					    " directory of file %s",
74267c478bd9Sstevel@tonic-gate 					    G_p->g_attrfnam_p);
74277c478bd9Sstevel@tonic-gate 					return (1);
74287c478bd9Sstevel@tonic-gate 				}
74297c478bd9Sstevel@tonic-gate 			}
74307c478bd9Sstevel@tonic-gate 		} else {
74317c478bd9Sstevel@tonic-gate 			G_p->g_dirfd = open_dir(G_p->g_nam_p);
74327c478bd9Sstevel@tonic-gate 			if (G_p->g_dirfd == -1) {
74337c478bd9Sstevel@tonic-gate 				msg(ERRN,
74347c478bd9Sstevel@tonic-gate 				    "Cannot open/create %s", G_p->g_nam_p);
74357c478bd9Sstevel@tonic-gate 				return (1);
74367c478bd9Sstevel@tonic-gate 			}
74377c478bd9Sstevel@tonic-gate 		}
74387c478bd9Sstevel@tonic-gate 	} else {
74397c478bd9Sstevel@tonic-gate 		G_p->g_dirfd = -1;
74407c478bd9Sstevel@tonic-gate 	}
74417c478bd9Sstevel@tonic-gate #else
74427c478bd9Sstevel@tonic-gate 	G_p->g_dirfd = -1;
74437c478bd9Sstevel@tonic-gate #endif
74447c478bd9Sstevel@tonic-gate 	return (0);
74457c478bd9Sstevel@tonic-gate }
74467c478bd9Sstevel@tonic-gate 
74477c478bd9Sstevel@tonic-gate static void
74487c478bd9Sstevel@tonic-gate close_dirfd()
74497c478bd9Sstevel@tonic-gate {
74507c478bd9Sstevel@tonic-gate 	if (G_p->g_dirfd != -1) {
74517c478bd9Sstevel@tonic-gate 		(void) close(G_p->g_dirfd);
74527c478bd9Sstevel@tonic-gate 		G_p->g_dirfd = -1;
74537c478bd9Sstevel@tonic-gate 	}
74547c478bd9Sstevel@tonic-gate }
74557c478bd9Sstevel@tonic-gate 
74567c478bd9Sstevel@tonic-gate static void
74577c478bd9Sstevel@tonic-gate write_xattr_hdr()
74587c478bd9Sstevel@tonic-gate {
74597c478bd9Sstevel@tonic-gate 	char *attrbuf = NULL;
74607c478bd9Sstevel@tonic-gate 	int  attrlen = 0;
74617c478bd9Sstevel@tonic-gate 	char *namep;
74627c478bd9Sstevel@tonic-gate 	struct Lnk *tl_p, *linkinfo;
74637c478bd9Sstevel@tonic-gate 
74647c478bd9Sstevel@tonic-gate 
74657c478bd9Sstevel@tonic-gate 	/*
74667c478bd9Sstevel@tonic-gate 	 * namep was allocated in xattrs_out.  It is big enough to hold
74677c478bd9Sstevel@tonic-gate 	 * either the name + .hdr on the end or just the attr name
74687c478bd9Sstevel@tonic-gate 	 */
74697c478bd9Sstevel@tonic-gate 
74707c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
74717c478bd9Sstevel@tonic-gate 	namep = Gen.g_nam_p;
74727c478bd9Sstevel@tonic-gate 	(void) creat_hdr();
74737c478bd9Sstevel@tonic-gate 
74747c478bd9Sstevel@tonic-gate 
74757c478bd9Sstevel@tonic-gate 	if (Args & OCo) {
74767c478bd9Sstevel@tonic-gate 		linkinfo = NULL;
74777c478bd9Sstevel@tonic-gate 		tl_p = Lnk_hd.L_nxt_p;
74787c478bd9Sstevel@tonic-gate 		while (tl_p != &Lnk_hd) {
74797c478bd9Sstevel@tonic-gate 			if (tl_p->L_gen.g_ino == G_p->g_ino &&
74807c478bd9Sstevel@tonic-gate 			    tl_p->L_gen.g_dev == G_p->g_dev) {
74817c478bd9Sstevel@tonic-gate 					linkinfo = tl_p;
74827c478bd9Sstevel@tonic-gate 					break; /* found */
74837c478bd9Sstevel@tonic-gate 			}
74847c478bd9Sstevel@tonic-gate 			tl_p = tl_p->L_nxt_p;
74857c478bd9Sstevel@tonic-gate 		}
74867c478bd9Sstevel@tonic-gate 		prepare_xattr_hdr(&attrbuf, Gen.g_attrfnam_p,
74877c478bd9Sstevel@tonic-gate 		    Gen.g_attrnam_p,
74887c478bd9Sstevel@tonic-gate 		    (linkinfo == (struct Lnk *)NULL) ?
74897c478bd9Sstevel@tonic-gate 		    tartype(Gen.g_mode & Ftype) : LNKTYPE,
74907c478bd9Sstevel@tonic-gate 		    linkinfo, &attrlen);
74917c478bd9Sstevel@tonic-gate 		Gen.g_filesz = attrlen;
74927c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_XATTR, (off_t)attrlen);
74937c478bd9Sstevel@tonic-gate 		(void) sprintf(namep, "%s/%s", DEVNULL, Gen.g_attrnam_p);
74947c478bd9Sstevel@tonic-gate 		(void) write_ancillary(attrbuf, attrlen);
74957c478bd9Sstevel@tonic-gate 	}
74967c478bd9Sstevel@tonic-gate 
74977c478bd9Sstevel@tonic-gate 	(void) creat_hdr();
74987c478bd9Sstevel@tonic-gate #endif
74997c478bd9Sstevel@tonic-gate }
75007c478bd9Sstevel@tonic-gate 
75017c478bd9Sstevel@tonic-gate static int
75027c478bd9Sstevel@tonic-gate retry_attrdir_open(char *name)
75037c478bd9Sstevel@tonic-gate {
75047c478bd9Sstevel@tonic-gate 	int dirfd = -1;
75057c478bd9Sstevel@tonic-gate 	struct timeval times[2];
75067c478bd9Sstevel@tonic-gate 	mode_t newmode;
75077c478bd9Sstevel@tonic-gate 	struct stat parentstat;
7508d2443e76Smarks 	acl_t *aclp = NULL;
7509d2443e76Smarks 	int error;
75107c478bd9Sstevel@tonic-gate 
75117c478bd9Sstevel@tonic-gate 	/*
75127c478bd9Sstevel@tonic-gate 	 * We couldn't get to attrdir. See if its
75137c478bd9Sstevel@tonic-gate 	 * just a mode problem on the parent file.
75147c478bd9Sstevel@tonic-gate 	 * for example: a mode such as r-xr--r--
75157c478bd9Sstevel@tonic-gate 	 * won't let us create an attribute dir
75167c478bd9Sstevel@tonic-gate 	 * if it doesn't already exist.
7517d2443e76Smarks 	 *
7518d2443e76Smarks 	 * If file has a non-trivial ACL, then save it
7519d2443e76Smarks 	 * off so that we can place it back on after doing
7520d2443e76Smarks 	 * chmod's.
75217c478bd9Sstevel@tonic-gate 	 */
75227c478bd9Sstevel@tonic-gate 
75237c478bd9Sstevel@tonic-gate 	if (stat(name, &parentstat) == -1) {
75247c478bd9Sstevel@tonic-gate 		msg(ERRN, "Cannot stat file %s", name);
75257c478bd9Sstevel@tonic-gate 		return (-1);
75267c478bd9Sstevel@tonic-gate 	}
7527d2443e76Smarks 
7528d2443e76Smarks 	if ((error = acl_get(name, ACL_NO_TRIVIAL, &aclp)) != 0) {
7529d2443e76Smarks 		msg(ERRN,
7530d2443e76Smarks 		    "Failed to retrieve ACL on %s %s", name, strerror(errno));
7531d2443e76Smarks 		return (-1);
7532d2443e76Smarks 	}
7533d2443e76Smarks 
75347c478bd9Sstevel@tonic-gate 	newmode = S_IWUSR | parentstat.st_mode;
75357c478bd9Sstevel@tonic-gate 	if (chmod(name, newmode) == -1) {
75367c478bd9Sstevel@tonic-gate 		msg(ERRN, "Cannot change mode of file %s to %o", name, newmode);
7537d2443e76Smarks 		if (aclp)
7538d2443e76Smarks 			acl_free(aclp);
75397c478bd9Sstevel@tonic-gate 		return (-1);
75407c478bd9Sstevel@tonic-gate 	}
75417c478bd9Sstevel@tonic-gate 
75427c478bd9Sstevel@tonic-gate 	dirfd = attropen(name, ".", O_RDONLY);
7543d2443e76Smarks 
7544d2443e76Smarks 	/*
7545d2443e76Smarks 	 * Don't print error here, caller will handle printing out
7546d2443e76Smarks 	 * can't open message.
7547d2443e76Smarks 	 */
75487c478bd9Sstevel@tonic-gate 
75497c478bd9Sstevel@tonic-gate 	/*
75507c478bd9Sstevel@tonic-gate 	 * Put mode back to original
75517c478bd9Sstevel@tonic-gate 	 */
75527c478bd9Sstevel@tonic-gate 	if (chmod(name, parentstat.st_mode) != 0) {
75537c478bd9Sstevel@tonic-gate 		msg(ERRN, "Cannot restore permissions of file %s to %o",
75547c478bd9Sstevel@tonic-gate 		    name, parentstat.st_mode);
75557c478bd9Sstevel@tonic-gate 	}
75567c478bd9Sstevel@tonic-gate 
7557d2443e76Smarks 	if (aclp) {
7558d2443e76Smarks 		error = acl_set(name, aclp);
7559d2443e76Smarks 		if (error) {
7560d2443e76Smarks 			msg(ERRN, "failed to set ACL on %s", name);
7561d2443e76Smarks 		}
7562d2443e76Smarks 		acl_free(aclp);
7563d2443e76Smarks 	}
75647c478bd9Sstevel@tonic-gate 	/*
75657c478bd9Sstevel@tonic-gate 	 * Put back time stamps
75667c478bd9Sstevel@tonic-gate 	 */
75677c478bd9Sstevel@tonic-gate 
75687c478bd9Sstevel@tonic-gate 	times[0].tv_sec = parentstat.st_atime;
75697c478bd9Sstevel@tonic-gate 	times[0].tv_usec = 0;
75707c478bd9Sstevel@tonic-gate 	times[1].tv_sec = parentstat.st_mtime;
75717c478bd9Sstevel@tonic-gate 	times[1].tv_usec = 0;
75727c478bd9Sstevel@tonic-gate 	if (utimes(name, times) != 0) {
75737c478bd9Sstevel@tonic-gate 		msg(ERRN, "Cannot reset timestamps on file %s");
75747c478bd9Sstevel@tonic-gate 	}
75757c478bd9Sstevel@tonic-gate 
75767c478bd9Sstevel@tonic-gate 	return (dirfd);
75777c478bd9Sstevel@tonic-gate }
75787c478bd9Sstevel@tonic-gate 
75797c478bd9Sstevel@tonic-gate /*
75807c478bd9Sstevel@tonic-gate  * skip over extra slashes in string.
75817c478bd9Sstevel@tonic-gate  *
75827c478bd9Sstevel@tonic-gate  * For example:
75837c478bd9Sstevel@tonic-gate  * /usr/tmp/////
75847c478bd9Sstevel@tonic-gate  *
75857c478bd9Sstevel@tonic-gate  * would return pointer at
75867c478bd9Sstevel@tonic-gate  * /usr/tmp/////
75877c478bd9Sstevel@tonic-gate  *         ^
75887c478bd9Sstevel@tonic-gate  */
75897c478bd9Sstevel@tonic-gate static char *
75907c478bd9Sstevel@tonic-gate skipslashes(char *string, char *start)
75917c478bd9Sstevel@tonic-gate {
75927c478bd9Sstevel@tonic-gate 	while ((string > start) && *(string - 1) == '/') {
75937c478bd9Sstevel@tonic-gate 		string--;
75947c478bd9Sstevel@tonic-gate 	}
75957c478bd9Sstevel@tonic-gate 
75967c478bd9Sstevel@tonic-gate 	return (string);
75977c478bd9Sstevel@tonic-gate }
75987c478bd9Sstevel@tonic-gate 
75997c478bd9Sstevel@tonic-gate static sl_info_t *
76007c478bd9Sstevel@tonic-gate sl_info_alloc(void)
76017c478bd9Sstevel@tonic-gate {
76027c478bd9Sstevel@tonic-gate 	static int num_left;
76037c478bd9Sstevel@tonic-gate 	static sl_info_t *slipool;
76047c478bd9Sstevel@tonic-gate 
76057c478bd9Sstevel@tonic-gate 	if (num_left > 0) {
76067c478bd9Sstevel@tonic-gate 		return (&slipool[--num_left]);
76077c478bd9Sstevel@tonic-gate 	}
76087c478bd9Sstevel@tonic-gate 	num_left = SL_INFO_ALLOC_CHUNK;
76097c478bd9Sstevel@tonic-gate 	slipool = e_zalloc(E_EXIT, sizeof (sl_info_t) * num_left);
76107c478bd9Sstevel@tonic-gate 	return (&slipool[--num_left]);
76117c478bd9Sstevel@tonic-gate }
76127c478bd9Sstevel@tonic-gate 
76137c478bd9Sstevel@tonic-gate /*
76147c478bd9Sstevel@tonic-gate  * If a match for the key values was found in the tree, return a pointer to it.
76157c478bd9Sstevel@tonic-gate  * If a match was not found, insert it and return a pointer to it.  This is
76167c478bd9Sstevel@tonic-gate  * based on Knuth's Algorithm A in Vol 3, section 6.2.3.
76177c478bd9Sstevel@tonic-gate  */
76187c478bd9Sstevel@tonic-gate 
76197c478bd9Sstevel@tonic-gate sl_info_t *
76207c478bd9Sstevel@tonic-gate sl_insert(dev_t device, ino_t inode)
76217c478bd9Sstevel@tonic-gate {
76227c478bd9Sstevel@tonic-gate 	sl_info_t *p;		/* moves down the tree */
76237c478bd9Sstevel@tonic-gate 	sl_info_t *q;		/* scratch */
76247c478bd9Sstevel@tonic-gate 	sl_info_t *r;		/* scratch */
76257c478bd9Sstevel@tonic-gate 	sl_info_t *s;		/* pt where rebalancing may be needed */
76267c478bd9Sstevel@tonic-gate 	sl_info_t *t;		/* father of s */
76277c478bd9Sstevel@tonic-gate 	sl_info_t *head;
76287c478bd9Sstevel@tonic-gate 
76297c478bd9Sstevel@tonic-gate 	int a;			/* used to hold balance factors */
76307c478bd9Sstevel@tonic-gate 	int done;		/* loop control */
76317c478bd9Sstevel@tonic-gate 	int cmpflg;		/* used to hold the result of a comparison */
76327c478bd9Sstevel@tonic-gate 
76337c478bd9Sstevel@tonic-gate 	/* initialize */
76347c478bd9Sstevel@tonic-gate 
76357c478bd9Sstevel@tonic-gate 	head = sl_devhash_lookup(device);
76367c478bd9Sstevel@tonic-gate 
76377c478bd9Sstevel@tonic-gate 	if (head == NULL) {
76387c478bd9Sstevel@tonic-gate 		head = sl_info_alloc();
76397c478bd9Sstevel@tonic-gate 		head->llink = NULL;
76407c478bd9Sstevel@tonic-gate 		head->bal = 0;
76417c478bd9Sstevel@tonic-gate 
76427c478bd9Sstevel@tonic-gate 		p = head->rlink = sl_info_alloc();
76437c478bd9Sstevel@tonic-gate 		p->sl_ino = inode;
76447c478bd9Sstevel@tonic-gate 		p->sl_count = 0;
76457c478bd9Sstevel@tonic-gate 		p->bal = 0;
76467c478bd9Sstevel@tonic-gate 		p->llink = NULL;
76477c478bd9Sstevel@tonic-gate 		p->rlink = NULL;
76487c478bd9Sstevel@tonic-gate 		sl_devhash_insert(device, head);
76497c478bd9Sstevel@tonic-gate 		return (p);
76507c478bd9Sstevel@tonic-gate 	}
76517c478bd9Sstevel@tonic-gate 
76527c478bd9Sstevel@tonic-gate 	t = head;
76537c478bd9Sstevel@tonic-gate 	s = p = head->rlink;
76547c478bd9Sstevel@tonic-gate 
76557c478bd9Sstevel@tonic-gate 	/* compare */
76567c478bd9Sstevel@tonic-gate 
76577c478bd9Sstevel@tonic-gate 	for (done = 0; ! done; ) {
76587c478bd9Sstevel@tonic-gate 		switch (sl_compare(inode, p->sl_ino)) {
76597c478bd9Sstevel@tonic-gate 			case -1:
76607c478bd9Sstevel@tonic-gate 				/* move left */
76617c478bd9Sstevel@tonic-gate 
76627c478bd9Sstevel@tonic-gate 				q = p->llink;
76637c478bd9Sstevel@tonic-gate 
76647c478bd9Sstevel@tonic-gate 				if (q == NULL) {
76657c478bd9Sstevel@tonic-gate 					q = sl_info_alloc();
76667c478bd9Sstevel@tonic-gate 					p->llink = q;
76677c478bd9Sstevel@tonic-gate 					done = 1;
76687c478bd9Sstevel@tonic-gate 					continue;
76697c478bd9Sstevel@tonic-gate 				}
76707c478bd9Sstevel@tonic-gate 
76717c478bd9Sstevel@tonic-gate 				break;
76727c478bd9Sstevel@tonic-gate 
76737c478bd9Sstevel@tonic-gate 			case 0:
76747c478bd9Sstevel@tonic-gate 				/* found it */
76757c478bd9Sstevel@tonic-gate 				return (p);
76767c478bd9Sstevel@tonic-gate 				break;
76777c478bd9Sstevel@tonic-gate 
76787c478bd9Sstevel@tonic-gate 			case 1:
76797c478bd9Sstevel@tonic-gate 				/* move right */
76807c478bd9Sstevel@tonic-gate 
76817c478bd9Sstevel@tonic-gate 				q = p->rlink;
76827c478bd9Sstevel@tonic-gate 
76837c478bd9Sstevel@tonic-gate 				if (q == NULL) {
76847c478bd9Sstevel@tonic-gate 					q = sl_info_alloc();
76857c478bd9Sstevel@tonic-gate 					p->rlink = q;
76867c478bd9Sstevel@tonic-gate 					done = 1;
76877c478bd9Sstevel@tonic-gate 					continue;
76887c478bd9Sstevel@tonic-gate 				}
76897c478bd9Sstevel@tonic-gate 
76907c478bd9Sstevel@tonic-gate 				break;
76917c478bd9Sstevel@tonic-gate 		}
76927c478bd9Sstevel@tonic-gate 
76937c478bd9Sstevel@tonic-gate 		if (q->bal != 0) {
76947c478bd9Sstevel@tonic-gate 			t = p;
76957c478bd9Sstevel@tonic-gate 			s = q;
76967c478bd9Sstevel@tonic-gate 		}
76977c478bd9Sstevel@tonic-gate 
76987c478bd9Sstevel@tonic-gate 		p = q;
76997c478bd9Sstevel@tonic-gate 	}
77007c478bd9Sstevel@tonic-gate 
77017c478bd9Sstevel@tonic-gate 	/* insert */
77027c478bd9Sstevel@tonic-gate 
77037c478bd9Sstevel@tonic-gate 	q->sl_ino = inode;
77047c478bd9Sstevel@tonic-gate 	q->sl_count = 0;
77057c478bd9Sstevel@tonic-gate 	q->llink = q->rlink = NULL;
77067c478bd9Sstevel@tonic-gate 	q->bal = 0;
77077c478bd9Sstevel@tonic-gate 
77087c478bd9Sstevel@tonic-gate 	/* adjust balance factors */
77097c478bd9Sstevel@tonic-gate 
77107c478bd9Sstevel@tonic-gate 	if ((cmpflg = sl_compare(inode, s->sl_ino)) < 0) {
77117c478bd9Sstevel@tonic-gate 		r = p = s->llink;
77127c478bd9Sstevel@tonic-gate 	} else {
77137c478bd9Sstevel@tonic-gate 		r = p = s->rlink;
77147c478bd9Sstevel@tonic-gate 	}
77157c478bd9Sstevel@tonic-gate 
77167c478bd9Sstevel@tonic-gate 	while (p != q) {
77177c478bd9Sstevel@tonic-gate 		switch (sl_compare(inode, p->sl_ino)) {
77187c478bd9Sstevel@tonic-gate 			case -1:
77197c478bd9Sstevel@tonic-gate 				p->bal = -1;
77207c478bd9Sstevel@tonic-gate 				p = p->llink;
77217c478bd9Sstevel@tonic-gate 				break;
77227c478bd9Sstevel@tonic-gate 
77237c478bd9Sstevel@tonic-gate 			case 0:
77247c478bd9Sstevel@tonic-gate 				break;
77257c478bd9Sstevel@tonic-gate 
77267c478bd9Sstevel@tonic-gate 			case 1:
77277c478bd9Sstevel@tonic-gate 				p->bal = 1;
77287c478bd9Sstevel@tonic-gate 				p = p->rlink;
77297c478bd9Sstevel@tonic-gate 				break;
77307c478bd9Sstevel@tonic-gate 		}
77317c478bd9Sstevel@tonic-gate 	}
77327c478bd9Sstevel@tonic-gate 
77337c478bd9Sstevel@tonic-gate 	/* balancing act */
77347c478bd9Sstevel@tonic-gate 
77357c478bd9Sstevel@tonic-gate 	if (cmpflg < 0) {
77367c478bd9Sstevel@tonic-gate 		a = -1;
77377c478bd9Sstevel@tonic-gate 	} else {
77387c478bd9Sstevel@tonic-gate 		a = 1;
77397c478bd9Sstevel@tonic-gate 	}
77407c478bd9Sstevel@tonic-gate 
77417c478bd9Sstevel@tonic-gate 	if (s->bal == 0) {
77427c478bd9Sstevel@tonic-gate 		s->bal = a;
77437c478bd9Sstevel@tonic-gate 		head->llink = (sl_info_t *)((int)head->llink + 1);
77447c478bd9Sstevel@tonic-gate 		return (q);
77457c478bd9Sstevel@tonic-gate 	} else if (s->bal == -a) {
77467c478bd9Sstevel@tonic-gate 		s->bal = 0;
77477c478bd9Sstevel@tonic-gate 		return (q);
77487c478bd9Sstevel@tonic-gate 	}
77497c478bd9Sstevel@tonic-gate 
77507c478bd9Sstevel@tonic-gate 	/*
77517c478bd9Sstevel@tonic-gate 	 * (s->bal == a)
77527c478bd9Sstevel@tonic-gate 	 */
77537c478bd9Sstevel@tonic-gate 
77547c478bd9Sstevel@tonic-gate 	if (r->bal == a) {
77557c478bd9Sstevel@tonic-gate 		/* single rotation */
77567c478bd9Sstevel@tonic-gate 
77577c478bd9Sstevel@tonic-gate 		p = r;
77587c478bd9Sstevel@tonic-gate 
77597c478bd9Sstevel@tonic-gate 		if (a == -1) {
77607c478bd9Sstevel@tonic-gate 			s->llink = r->rlink;
77617c478bd9Sstevel@tonic-gate 			r->rlink = s;
77627c478bd9Sstevel@tonic-gate 		} else if (a == 1) {
77637c478bd9Sstevel@tonic-gate 			s->rlink = r->llink;
77647c478bd9Sstevel@tonic-gate 			r->llink = s;
77657c478bd9Sstevel@tonic-gate 		}
77667c478bd9Sstevel@tonic-gate 
77677c478bd9Sstevel@tonic-gate 		s->bal = r->bal = 0;
77687c478bd9Sstevel@tonic-gate 
77697c478bd9Sstevel@tonic-gate 	} else if (r->bal == -a) {
77707c478bd9Sstevel@tonic-gate 		/* double rotation */
77717c478bd9Sstevel@tonic-gate 
77727c478bd9Sstevel@tonic-gate 		if (a == -1) {
77737c478bd9Sstevel@tonic-gate 			p = r->rlink;
77747c478bd9Sstevel@tonic-gate 			r->rlink = p->llink;
77757c478bd9Sstevel@tonic-gate 			p->llink = r;
77767c478bd9Sstevel@tonic-gate 			s->llink = p->rlink;
77777c478bd9Sstevel@tonic-gate 			p->rlink = s;
77787c478bd9Sstevel@tonic-gate 		} else if (a == 1) {
77797c478bd9Sstevel@tonic-gate 			p = r->llink;
77807c478bd9Sstevel@tonic-gate 			r->llink = p->rlink;
77817c478bd9Sstevel@tonic-gate 			p->rlink = r;
77827c478bd9Sstevel@tonic-gate 			s->rlink = p->llink;
77837c478bd9Sstevel@tonic-gate 			p->llink = s;
77847c478bd9Sstevel@tonic-gate 		}
77857c478bd9Sstevel@tonic-gate 
77867c478bd9Sstevel@tonic-gate 		if (p->bal == 0) {
77877c478bd9Sstevel@tonic-gate 			s->bal = 0;
77887c478bd9Sstevel@tonic-gate 			r->bal = 0;
77897c478bd9Sstevel@tonic-gate 		} else if (p->bal == -a) {
77907c478bd9Sstevel@tonic-gate 			s->bal = 0;
77917c478bd9Sstevel@tonic-gate 			r->bal = a;
77927c478bd9Sstevel@tonic-gate 		} else if (p->bal == a) {
77937c478bd9Sstevel@tonic-gate 			s->bal = -a;
77947c478bd9Sstevel@tonic-gate 			r->bal = 0;
77957c478bd9Sstevel@tonic-gate 		}
77967c478bd9Sstevel@tonic-gate 
77977c478bd9Sstevel@tonic-gate 		p->bal = 0;
77987c478bd9Sstevel@tonic-gate 	}
77997c478bd9Sstevel@tonic-gate 
78007c478bd9Sstevel@tonic-gate 	/* finishing touch */
78017c478bd9Sstevel@tonic-gate 
78027c478bd9Sstevel@tonic-gate 	if (s == t->rlink) {
78037c478bd9Sstevel@tonic-gate 		t->rlink = p;
78047c478bd9Sstevel@tonic-gate 	} else {
78057c478bd9Sstevel@tonic-gate 		t->llink = p;
78067c478bd9Sstevel@tonic-gate 	}
78077c478bd9Sstevel@tonic-gate 
78087c478bd9Sstevel@tonic-gate 	return (q);
78097c478bd9Sstevel@tonic-gate }
78107c478bd9Sstevel@tonic-gate 
78117c478bd9Sstevel@tonic-gate /*
78127c478bd9Sstevel@tonic-gate  * sl_numlinks: return the number of links that we saw during our preview.
78137c478bd9Sstevel@tonic-gate  */
78147c478bd9Sstevel@tonic-gate 
78157c478bd9Sstevel@tonic-gate static ulong_t
78167c478bd9Sstevel@tonic-gate sl_numlinks(dev_t device, ino_t inode)
78177c478bd9Sstevel@tonic-gate {
78187c478bd9Sstevel@tonic-gate 	sl_info_t *p = sl_search(device, inode);
78197c478bd9Sstevel@tonic-gate 
78207c478bd9Sstevel@tonic-gate 	if (p) {
78217c478bd9Sstevel@tonic-gate 		return (p->sl_count);
78227c478bd9Sstevel@tonic-gate 	} else {
78237c478bd9Sstevel@tonic-gate 		return (1);
78247c478bd9Sstevel@tonic-gate 	}
78257c478bd9Sstevel@tonic-gate }
78267c478bd9Sstevel@tonic-gate 
78277c478bd9Sstevel@tonic-gate /*
78287c478bd9Sstevel@tonic-gate  * sl_preview_synonyms:  Read the file list from the input stream, remembering
78297c478bd9Sstevel@tonic-gate  * each reference to each file.
78307c478bd9Sstevel@tonic-gate  */
78317c478bd9Sstevel@tonic-gate 
78327c478bd9Sstevel@tonic-gate static void
78337c478bd9Sstevel@tonic-gate sl_preview_synonyms(void)
78347c478bd9Sstevel@tonic-gate {
78357c478bd9Sstevel@tonic-gate 	char buf [APATH+1];
78367c478bd9Sstevel@tonic-gate 	char *s;
78377c478bd9Sstevel@tonic-gate 
78387c478bd9Sstevel@tonic-gate 	char *suffix = "/cpioXXXXXX";
78397c478bd9Sstevel@tonic-gate 	char *tmpdir = getenv("TMPDIR");
78407c478bd9Sstevel@tonic-gate 	int    tmpfd, islnk;
78417c478bd9Sstevel@tonic-gate 	FILE *tmpfile;
78427c478bd9Sstevel@tonic-gate 	char *tmpfname;
78437c478bd9Sstevel@tonic-gate 
78447c478bd9Sstevel@tonic-gate 	if (tmpdir == NULL || *tmpdir == '\0' ||
78457c478bd9Sstevel@tonic-gate 	    (strlen(tmpdir) + strlen(suffix)) > APATH) {
78467c478bd9Sstevel@tonic-gate 		struct statvfs tdsb;
78477c478bd9Sstevel@tonic-gate 
78487c478bd9Sstevel@tonic-gate 		tmpdir = "/var/tmp";
78497c478bd9Sstevel@tonic-gate 
78507c478bd9Sstevel@tonic-gate 		/* /var/tmp is read-only in the mini-root environment */
78517c478bd9Sstevel@tonic-gate 
78527c478bd9Sstevel@tonic-gate 		if (statvfs(tmpdir, &tdsb) == -1 || tdsb.f_flag & ST_RDONLY) {
78537c478bd9Sstevel@tonic-gate 			tmpdir = "/tmp";
78547c478bd9Sstevel@tonic-gate 		}
78557c478bd9Sstevel@tonic-gate 	}
78567c478bd9Sstevel@tonic-gate 
78577c478bd9Sstevel@tonic-gate 	tmpfname = e_zalloc(E_EXIT, strlen(tmpdir) + strlen(suffix) + 1);
78587c478bd9Sstevel@tonic-gate 
78597c478bd9Sstevel@tonic-gate 	(void) strcpy(tmpfname, tmpdir);
78607c478bd9Sstevel@tonic-gate 	(void) strcat(tmpfname, suffix);
78617c478bd9Sstevel@tonic-gate 
78627c478bd9Sstevel@tonic-gate 	if ((tmpfd = mkstemp(tmpfname)) == -1) {
78637c478bd9Sstevel@tonic-gate 		msg(EXTN, "cannot open tmpfile %s", tmpfname);
78647c478bd9Sstevel@tonic-gate 	}
78657c478bd9Sstevel@tonic-gate 
78667c478bd9Sstevel@tonic-gate 	if (unlink(tmpfname) == -1) {
78677c478bd9Sstevel@tonic-gate 		msg(EXTN, "cannot unlink tmpfile %s", tmpfname);
78687c478bd9Sstevel@tonic-gate 	}
78697c478bd9Sstevel@tonic-gate 
78707c478bd9Sstevel@tonic-gate 	if ((tmpfile = fdopen(tmpfd, "w+")) == NULL) {
78717c478bd9Sstevel@tonic-gate 		msg(EXTN, "cannot fdopen tmpfile %s", tmpfname);
78727c478bd9Sstevel@tonic-gate 	}
78737c478bd9Sstevel@tonic-gate 
78747c478bd9Sstevel@tonic-gate 	while ((s = fgets(buf, APATH+1, In_p)) != NULL) {
78757c478bd9Sstevel@tonic-gate 		size_t lastchar;
78767c478bd9Sstevel@tonic-gate 		struct stat sb;
78777c478bd9Sstevel@tonic-gate 
78787c478bd9Sstevel@tonic-gate 		if (fputs(buf, tmpfile) == EOF) {
78797c478bd9Sstevel@tonic-gate 			msg(EXTN, "problem writing to tmpfile %s", tmpfname);
78807c478bd9Sstevel@tonic-gate 		}
78817c478bd9Sstevel@tonic-gate 
78827c478bd9Sstevel@tonic-gate 		/* pre-process the name */
78837c478bd9Sstevel@tonic-gate 
78847c478bd9Sstevel@tonic-gate 		lastchar = strlen(s) - 1;
78857c478bd9Sstevel@tonic-gate 
78867c478bd9Sstevel@tonic-gate 		if (s[lastchar] != '\n' && lastchar == APATH - 1) {
78877c478bd9Sstevel@tonic-gate 			continue;
78887c478bd9Sstevel@tonic-gate 		} else {
78897c478bd9Sstevel@tonic-gate 			s[lastchar] = '\0';
78907c478bd9Sstevel@tonic-gate 		}
78917c478bd9Sstevel@tonic-gate 
78927c478bd9Sstevel@tonic-gate 		while (s[0] == '.' && s[1] == '/') {
78937c478bd9Sstevel@tonic-gate 			s += 2;
78947c478bd9Sstevel@tonic-gate 			while (s[0] == '/') {
78957c478bd9Sstevel@tonic-gate 				s++;
78967c478bd9Sstevel@tonic-gate 			}
78977c478bd9Sstevel@tonic-gate 		}
78987c478bd9Sstevel@tonic-gate 
78997c478bd9Sstevel@tonic-gate 		if (lstat(s, &sb) < 0) {
79007c478bd9Sstevel@tonic-gate 			continue;
79017c478bd9Sstevel@tonic-gate 		}
79027c478bd9Sstevel@tonic-gate 		islnk = 0;
79037c478bd9Sstevel@tonic-gate 		if (S_ISLNK(sb.st_mode)) {
79047c478bd9Sstevel@tonic-gate 			islnk = 1;
79057c478bd9Sstevel@tonic-gate 			if (Args & OCL) {
79067c478bd9Sstevel@tonic-gate 				if (stat(s, &sb) < 0) {
79077c478bd9Sstevel@tonic-gate 					continue;
79087c478bd9Sstevel@tonic-gate 				}
79097c478bd9Sstevel@tonic-gate 			}
79107c478bd9Sstevel@tonic-gate 		}
79117c478bd9Sstevel@tonic-gate 		sl_remember_tgt(&sb, islnk);
79127c478bd9Sstevel@tonic-gate 
79137c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
79147c478bd9Sstevel@tonic-gate 		if (Atflag) {
79157c478bd9Sstevel@tonic-gate 			int  dirfd;
79167c478bd9Sstevel@tonic-gate 			DIR  *dirp;
79177c478bd9Sstevel@tonic-gate 			struct dirent *dp;
79187c478bd9Sstevel@tonic-gate 
79197c478bd9Sstevel@tonic-gate 			if (pathconf(s, _PC_XATTR_EXISTS) != 1)
79207c478bd9Sstevel@tonic-gate 				continue;
79217c478bd9Sstevel@tonic-gate 
79227c478bd9Sstevel@tonic-gate 			dirfd = attropen(s, ".", O_RDONLY);
79237c478bd9Sstevel@tonic-gate 			if (dirfd == -1)
79247c478bd9Sstevel@tonic-gate 				continue;
79257c478bd9Sstevel@tonic-gate 
79267c478bd9Sstevel@tonic-gate 			tmpfd = dup(dirfd);
79277c478bd9Sstevel@tonic-gate 			if (tmpfd == -1) {
79287c478bd9Sstevel@tonic-gate 				(void) close(dirfd);
79297c478bd9Sstevel@tonic-gate 				continue;
79307c478bd9Sstevel@tonic-gate 			}
79317c478bd9Sstevel@tonic-gate 			dirp = fdopendir(tmpfd);
79327c478bd9Sstevel@tonic-gate 			if (dirp == NULL) {
79337c478bd9Sstevel@tonic-gate 				(void) close(dirfd);
79347c478bd9Sstevel@tonic-gate 				(void) close(tmpfd);
79357c478bd9Sstevel@tonic-gate 				continue;
79367c478bd9Sstevel@tonic-gate 			}
79377c478bd9Sstevel@tonic-gate 
79387c478bd9Sstevel@tonic-gate 			while (dp = readdir(dirp)) {
79397c478bd9Sstevel@tonic-gate 				if ((dp->d_name[0] == '.' &&
79407c478bd9Sstevel@tonic-gate 				    dp->d_name[1] == '\0') ||
79417c478bd9Sstevel@tonic-gate 				    (dp->d_name[0] == '.' &&
79427c478bd9Sstevel@tonic-gate 				    dp->d_name[1] == '.' &&
7943*da6c28aaSamw 				    dp->d_name[2] == '\0') ||
7944*da6c28aaSamw 				    is_sysattr(dp->d_name))
79457c478bd9Sstevel@tonic-gate 					continue;
79467c478bd9Sstevel@tonic-gate 
79477c478bd9Sstevel@tonic-gate 				if (fstatat(dirfd, dp->d_name, &sb,
79487c478bd9Sstevel@tonic-gate 				    AT_SYMLINK_NOFOLLOW) < 0) {
79497c478bd9Sstevel@tonic-gate 					continue;
79507c478bd9Sstevel@tonic-gate 				}
79517c478bd9Sstevel@tonic-gate 				islnk = 0;
79527c478bd9Sstevel@tonic-gate 				if (S_ISLNK(sb.st_mode)) {
79537c478bd9Sstevel@tonic-gate 					islnk = 1;
79547c478bd9Sstevel@tonic-gate 					if (Args & OCL) {
79557c478bd9Sstevel@tonic-gate 						if (fstatat(dirfd, dp->d_name,
79567c478bd9Sstevel@tonic-gate 						    &sb, 0) < 0) {
79577c478bd9Sstevel@tonic-gate 							continue;
79587c478bd9Sstevel@tonic-gate 						}
79597c478bd9Sstevel@tonic-gate 					}
79607c478bd9Sstevel@tonic-gate 				}
79617c478bd9Sstevel@tonic-gate 				sl_remember_tgt(&sb, islnk);
79627c478bd9Sstevel@tonic-gate 			}
79637c478bd9Sstevel@tonic-gate 			(void) closedir(dirp);
79647c478bd9Sstevel@tonic-gate 			(void) close(dirfd);
79657c478bd9Sstevel@tonic-gate 		}
79667c478bd9Sstevel@tonic-gate #endif
79677c478bd9Sstevel@tonic-gate 	}
79687c478bd9Sstevel@tonic-gate 
79697c478bd9Sstevel@tonic-gate 	if (ferror(In_p)) {
79707c478bd9Sstevel@tonic-gate 		msg(EXTN, "error reading stdin");
79717c478bd9Sstevel@tonic-gate 	}
79727c478bd9Sstevel@tonic-gate 
79737c478bd9Sstevel@tonic-gate 	if (fseek(tmpfile, 0L, SEEK_SET) == -1) {
79747c478bd9Sstevel@tonic-gate 		msg(EXTN, "cannot fseek on tmpfile %s", tmpfname);
79757c478bd9Sstevel@tonic-gate 	}
79767c478bd9Sstevel@tonic-gate 
79777c478bd9Sstevel@tonic-gate 	In_p = tmpfile;
79787c478bd9Sstevel@tonic-gate 	free(tmpfname);
79797c478bd9Sstevel@tonic-gate }
79807c478bd9Sstevel@tonic-gate 
79817c478bd9Sstevel@tonic-gate /*
79827c478bd9Sstevel@tonic-gate  * sl_remember_tgt: Add the device/inode for lstat or stat info to the list of
79837c478bd9Sstevel@tonic-gate  * those we've seen before.
79847c478bd9Sstevel@tonic-gate  *
79857c478bd9Sstevel@tonic-gate  * This tree (rooted under head) is keyed by the device/inode of the file
79867c478bd9Sstevel@tonic-gate  * being pointed to.  A count is kept of the number of references encountered
79877c478bd9Sstevel@tonic-gate  * so far.
79887c478bd9Sstevel@tonic-gate  */
79897c478bd9Sstevel@tonic-gate 
79907c478bd9Sstevel@tonic-gate static void
79917c478bd9Sstevel@tonic-gate sl_remember_tgt(const struct stat *sbp, int isSymlink)
79927c478bd9Sstevel@tonic-gate {
79937c478bd9Sstevel@tonic-gate 	sl_info_t *p;
79947c478bd9Sstevel@tonic-gate 	dev_t device;
79957c478bd9Sstevel@tonic-gate 	ino_t inode;
79967c478bd9Sstevel@tonic-gate 
79977c478bd9Sstevel@tonic-gate 	device = sbp->st_dev;
79987c478bd9Sstevel@tonic-gate 	inode  = sbp->st_ino;
79997c478bd9Sstevel@tonic-gate 
80007c478bd9Sstevel@tonic-gate 	/* Determine whether we've seen this one before */
80017c478bd9Sstevel@tonic-gate 
80027c478bd9Sstevel@tonic-gate 	p = sl_insert(device, inode);
80037c478bd9Sstevel@tonic-gate 
80047c478bd9Sstevel@tonic-gate 	if (p->sl_count > 0) {
80057c478bd9Sstevel@tonic-gate 		/*
80067c478bd9Sstevel@tonic-gate 		 * We have seen this file before.
80077c478bd9Sstevel@tonic-gate 		 * Note that if we are not chasing symlinks, and this one is a
80087c478bd9Sstevel@tonic-gate 		 * symlink, it is identically the one we saw before (you cannot
80097c478bd9Sstevel@tonic-gate 		 * have hard links to symlinks); in this case, we leave the
80107c478bd9Sstevel@tonic-gate 		 * count alone, so that we don't wind up archiving a symlink to
80117c478bd9Sstevel@tonic-gate 		 * itself.
80127c478bd9Sstevel@tonic-gate 		 */
80137c478bd9Sstevel@tonic-gate 
80147c478bd9Sstevel@tonic-gate 		if ((Args & OCL) || (! isSymlink)) {
80157c478bd9Sstevel@tonic-gate 			p->sl_count++;
80167c478bd9Sstevel@tonic-gate 		}
80177c478bd9Sstevel@tonic-gate 	} else {
80187c478bd9Sstevel@tonic-gate 		/* We have not seen this file before */
80197c478bd9Sstevel@tonic-gate 
80207c478bd9Sstevel@tonic-gate 		p->sl_count = 1;
80217c478bd9Sstevel@tonic-gate 
80227c478bd9Sstevel@tonic-gate 		if (Use_old_stat) {
80237c478bd9Sstevel@tonic-gate 			/* -Hodc: remap inode (-1 on overflow) */
80247c478bd9Sstevel@tonic-gate 
80257c478bd9Sstevel@tonic-gate 			sl_remap_t  *q;
80267c478bd9Sstevel@tonic-gate 
80277c478bd9Sstevel@tonic-gate 			for (q = sl_remap_head; q && (q->dev != device);
80287c478bd9Sstevel@tonic-gate 			    q = q->next) {
80297c478bd9Sstevel@tonic-gate 				/* do nothing */
80307c478bd9Sstevel@tonic-gate 			}
80317c478bd9Sstevel@tonic-gate 
80327c478bd9Sstevel@tonic-gate 			if (q == NULL) {
80337c478bd9Sstevel@tonic-gate 				q = e_zalloc(E_EXIT, sizeof (sl_remap_t));
80347c478bd9Sstevel@tonic-gate 				q->dev = device;
80357c478bd9Sstevel@tonic-gate 				p->sl_ino2 = q->inode_count = 1;
80367c478bd9Sstevel@tonic-gate 
80377c478bd9Sstevel@tonic-gate 				q->next = (sl_remap_head) ?
80387c478bd9Sstevel@tonic-gate 				    sl_remap_head->next : NULL;
80397c478bd9Sstevel@tonic-gate 				sl_remap_head = q;
80407c478bd9Sstevel@tonic-gate 			} else {
80417c478bd9Sstevel@tonic-gate 				if ((size_t)q->inode_count <=
80427c478bd9Sstevel@tonic-gate 				    ((1 << (sizeof (o_ino_t) * 8)) - 1)) {
80437c478bd9Sstevel@tonic-gate 					/* fits in o_ino_t */
80447c478bd9Sstevel@tonic-gate 					p->sl_ino2 = ++(q->inode_count);
80457c478bd9Sstevel@tonic-gate 				} else {
80467c478bd9Sstevel@tonic-gate 					p->sl_ino2 = (ino_t)-1;
80477c478bd9Sstevel@tonic-gate 				}
80487c478bd9Sstevel@tonic-gate 			}
80497c478bd9Sstevel@tonic-gate 		}
80507c478bd9Sstevel@tonic-gate 	}
80517c478bd9Sstevel@tonic-gate }
80527c478bd9Sstevel@tonic-gate 
80537c478bd9Sstevel@tonic-gate /*
80547c478bd9Sstevel@tonic-gate  * A faster search, which does not insert the key values into the tree.
80557c478bd9Sstevel@tonic-gate  * If the a match was found in the tree, return a pointer to it.  If it was not
80567c478bd9Sstevel@tonic-gate  * found, return NULL.
80577c478bd9Sstevel@tonic-gate  */
80587c478bd9Sstevel@tonic-gate 
80597c478bd9Sstevel@tonic-gate sl_info_t *
80607c478bd9Sstevel@tonic-gate sl_search(dev_t device, ino_t inode)
80617c478bd9Sstevel@tonic-gate {
80627c478bd9Sstevel@tonic-gate 	sl_info_t *p;		/* moves down the tree */
80637c478bd9Sstevel@tonic-gate 	int c;			/* comparison value */
80647c478bd9Sstevel@tonic-gate 	sl_info_t *retval = NULL; /* return value */
80657c478bd9Sstevel@tonic-gate 	sl_info_t *head;
80667c478bd9Sstevel@tonic-gate 
80677c478bd9Sstevel@tonic-gate 	head = sl_devhash_lookup(device);
80687c478bd9Sstevel@tonic-gate 	if (head != NULL) {
80697c478bd9Sstevel@tonic-gate 		for (p = head->rlink; p; ) {
80707c478bd9Sstevel@tonic-gate 			if ((c = sl_compare(inode, p->sl_ino)) == 0) {
80717c478bd9Sstevel@tonic-gate 				retval = p;
80727c478bd9Sstevel@tonic-gate 				break;
80737c478bd9Sstevel@tonic-gate 			} else if (c < 0) {
80747c478bd9Sstevel@tonic-gate 				p = p->llink;
80757c478bd9Sstevel@tonic-gate 			} else {
80767c478bd9Sstevel@tonic-gate 				p = p->rlink;
80777c478bd9Sstevel@tonic-gate 			}
80787c478bd9Sstevel@tonic-gate 		}
80797c478bd9Sstevel@tonic-gate 	}
80807c478bd9Sstevel@tonic-gate 
80817c478bd9Sstevel@tonic-gate 	return (retval);
80827c478bd9Sstevel@tonic-gate }
80837c478bd9Sstevel@tonic-gate 
80847c478bd9Sstevel@tonic-gate static sl_info_t *
80857c478bd9Sstevel@tonic-gate sl_devhash_lookup(dev_t device)
80867c478bd9Sstevel@tonic-gate {
80877c478bd9Sstevel@tonic-gate 	int key;
80887c478bd9Sstevel@tonic-gate 	sl_info_link_t *lp;
80897c478bd9Sstevel@tonic-gate 	static sl_info_link_t *devcache;
80907c478bd9Sstevel@tonic-gate 
80917c478bd9Sstevel@tonic-gate 	if (devcache != NULL && devcache->dev == device) {
80927c478bd9Sstevel@tonic-gate 		return (devcache->head);
80937c478bd9Sstevel@tonic-gate 	}
80947c478bd9Sstevel@tonic-gate 
80957c478bd9Sstevel@tonic-gate 	key = DEV_HASHKEY(device);
80967c478bd9Sstevel@tonic-gate 	for (lp = sl_devhash[key]; lp; lp = lp->next) {
80977c478bd9Sstevel@tonic-gate 		if (lp->dev == device) {
80987c478bd9Sstevel@tonic-gate 			devcache = lp;
80997c478bd9Sstevel@tonic-gate 			return (lp->head);
81007c478bd9Sstevel@tonic-gate 		}
81017c478bd9Sstevel@tonic-gate 	}
81027c478bd9Sstevel@tonic-gate 	return (NULL);
81037c478bd9Sstevel@tonic-gate }
81047c478bd9Sstevel@tonic-gate 
81057c478bd9Sstevel@tonic-gate static void
81067c478bd9Sstevel@tonic-gate sl_devhash_insert(dev_t device, sl_info_t *head)
81077c478bd9Sstevel@tonic-gate {
81087c478bd9Sstevel@tonic-gate 	int key = DEV_HASHKEY(device);
81097c478bd9Sstevel@tonic-gate 	sl_info_link_t *lp;
81107c478bd9Sstevel@tonic-gate 
81117c478bd9Sstevel@tonic-gate 	lp = e_zalloc(E_EXIT, sizeof (sl_info_link_t));
81127c478bd9Sstevel@tonic-gate 	lp->dev = device;
81137c478bd9Sstevel@tonic-gate 	lp->head = head;
81147c478bd9Sstevel@tonic-gate 	lp->next = sl_devhash[key];
81157c478bd9Sstevel@tonic-gate 	sl_devhash[key] = lp;
81167c478bd9Sstevel@tonic-gate }
81177c478bd9Sstevel@tonic-gate 
81187c478bd9Sstevel@tonic-gate static void
81197c478bd9Sstevel@tonic-gate chop_endslashes(char *path)
81207c478bd9Sstevel@tonic-gate {
81217c478bd9Sstevel@tonic-gate 	char *end, *ptr;
81227c478bd9Sstevel@tonic-gate 
81237c478bd9Sstevel@tonic-gate 	end = &path[strlen(path) -1];
81247c478bd9Sstevel@tonic-gate 	if (*end == '/' && end != path) {
81257c478bd9Sstevel@tonic-gate 		ptr = skipslashes(end, path);
81267c478bd9Sstevel@tonic-gate 		if (ptr != NULL && ptr != path) {
81277c478bd9Sstevel@tonic-gate 			*ptr = '\0';
81287c478bd9Sstevel@tonic-gate 		}
81297c478bd9Sstevel@tonic-gate 	}
81307c478bd9Sstevel@tonic-gate }
81317c478bd9Sstevel@tonic-gate 
81327c478bd9Sstevel@tonic-gate #if !defined(O_XATTR)
81337c478bd9Sstevel@tonic-gate int
81347c478bd9Sstevel@tonic-gate openat64(int fd, char *name, int oflag, mode_t cmode)
81357c478bd9Sstevel@tonic-gate {
81367c478bd9Sstevel@tonic-gate 	return (open64(name, oflag, cmode));
81377c478bd9Sstevel@tonic-gate }
81387c478bd9Sstevel@tonic-gate 
81397c478bd9Sstevel@tonic-gate int
81407c478bd9Sstevel@tonic-gate openat(int fd, char *name, int oflag, mode_t cmode)
81417c478bd9Sstevel@tonic-gate {
81427c478bd9Sstevel@tonic-gate 	return (open(name, oflag, cmode));
81437c478bd9Sstevel@tonic-gate }
81447c478bd9Sstevel@tonic-gate 
81457c478bd9Sstevel@tonic-gate int
81467c478bd9Sstevel@tonic-gate fchownat(int fd, char *name, uid_t owner, gid_t group, int flag)
81477c478bd9Sstevel@tonic-gate {
81487c478bd9Sstevel@tonic-gate 	if (flag == AT_SYMLINK_NOFOLLOW)
81497c478bd9Sstevel@tonic-gate 		return (lchown(name, owner, group));
81507c478bd9Sstevel@tonic-gate 	else
81517c478bd9Sstevel@tonic-gate 		return (chown(name, owner, group));
81527c478bd9Sstevel@tonic-gate }
81537c478bd9Sstevel@tonic-gate 
81547c478bd9Sstevel@tonic-gate int
81557c478bd9Sstevel@tonic-gate renameat(int fromfd, char *old, int tofd, char *new)
81567c478bd9Sstevel@tonic-gate {
81577c478bd9Sstevel@tonic-gate 	return (rename(old, new));
81587c478bd9Sstevel@tonic-gate }
81597c478bd9Sstevel@tonic-gate 
81607c478bd9Sstevel@tonic-gate int
81617c478bd9Sstevel@tonic-gate futimesat(int fd, char *path, struct timeval times[2])
81627c478bd9Sstevel@tonic-gate {
81637c478bd9Sstevel@tonic-gate 	return (utimes(path, times));
81647c478bd9Sstevel@tonic-gate }
81657c478bd9Sstevel@tonic-gate 
81667c478bd9Sstevel@tonic-gate int
81677c478bd9Sstevel@tonic-gate unlinkat(int dirfd, char *path, int flag)
81687c478bd9Sstevel@tonic-gate {
81697c478bd9Sstevel@tonic-gate 	if (flag == AT_REMOVEDIR) {
81707c478bd9Sstevel@tonic-gate 		return (rmdir(path));
81717c478bd9Sstevel@tonic-gate 	} else {
81727c478bd9Sstevel@tonic-gate 		return (unlink(path));
81737c478bd9Sstevel@tonic-gate 	}
81747c478bd9Sstevel@tonic-gate }
81757c478bd9Sstevel@tonic-gate 
81767c478bd9Sstevel@tonic-gate int
81777c478bd9Sstevel@tonic-gate fstatat(int fd, char *path, struct stat *buf, int flag)
81787c478bd9Sstevel@tonic-gate {
81797c478bd9Sstevel@tonic-gate 	if (flag == AT_SYMLINK_NOFOLLOW)
81807c478bd9Sstevel@tonic-gate 		return (lstat(path, buf));
81817c478bd9Sstevel@tonic-gate 	else
81827c478bd9Sstevel@tonic-gate 		return (stat(path, buf));
81837c478bd9Sstevel@tonic-gate }
81847c478bd9Sstevel@tonic-gate 
81857c478bd9Sstevel@tonic-gate int
81867c478bd9Sstevel@tonic-gate attropen(char *file, char *attr, int omode, mode_t cmode)
81877c478bd9Sstevel@tonic-gate {
81887c478bd9Sstevel@tonic-gate 	errno = ENOTSUP;
81897c478bd9Sstevel@tonic-gate 	return (-1);
81907c478bd9Sstevel@tonic-gate }
81917c478bd9Sstevel@tonic-gate #endif
8192