xref: /titanic_54/usr/src/cmd/cpio/cpio.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	All Rights Reserved					*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
32*7c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include <stdio.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
39*7c478bd9Sstevel@tonic-gate #include <errno.h>
40*7c478bd9Sstevel@tonic-gate #include <unistd.h>
41*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
42*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
43*7c478bd9Sstevel@tonic-gate #include <memory.h>
44*7c478bd9Sstevel@tonic-gate #include <string.h>
45*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
50*7c478bd9Sstevel@tonic-gate #include <utime.h>
51*7c478bd9Sstevel@tonic-gate #include <pwd.h>
52*7c478bd9Sstevel@tonic-gate #include <grp.h>
53*7c478bd9Sstevel@tonic-gate #include <signal.h>
54*7c478bd9Sstevel@tonic-gate #include <ctype.h>
55*7c478bd9Sstevel@tonic-gate #include <archives.h>
56*7c478bd9Sstevel@tonic-gate #include <locale.h>
57*7c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
58*7c478bd9Sstevel@tonic-gate #include <sys/mtio.h>
59*7c478bd9Sstevel@tonic-gate #include <sys/fdio.h>
60*7c478bd9Sstevel@tonic-gate #include "cpio.h"
61*7c478bd9Sstevel@tonic-gate #include <sys/acl.h>
62*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
63*7c478bd9Sstevel@tonic-gate #include <sys/resource.h>
64*7c478bd9Sstevel@tonic-gate #include <fnmatch.h>
65*7c478bd9Sstevel@tonic-gate #include <libgen.h>
66*7c478bd9Sstevel@tonic-gate #include <libintl.h>
67*7c478bd9Sstevel@tonic-gate #include <dirent.h>
68*7c478bd9Sstevel@tonic-gate #include <limits.h>
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate /*
71*7c478bd9Sstevel@tonic-gate  * Special kludge for off_t being a signed quantity.
72*7c478bd9Sstevel@tonic-gate  */
73*7c478bd9Sstevel@tonic-gate #if _FILE_OFFSET_BITS == 64
74*7c478bd9Sstevel@tonic-gate typedef	u_longlong_t	u_off_t;
75*7c478bd9Sstevel@tonic-gate #else
76*7c478bd9Sstevel@tonic-gate typedef	ulong_t		u_off_t;
77*7c478bd9Sstevel@tonic-gate #endif
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate #define	SECMODE	0xe080
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate #define	DEVNULL		"/dev/null"
82*7c478bd9Sstevel@tonic-gate #define	XATTRHDR	".hdr"
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate #define	NAMELEN		32
85*7c478bd9Sstevel@tonic-gate #define	TYPELEN 	16
86*7c478bd9Sstevel@tonic-gate #define	PERMLEN		4
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate #define	FILE_COPIED	1
89*7c478bd9Sstevel@tonic-gate #define	FILE_LINKED	2
90*7c478bd9Sstevel@tonic-gate #define	FILE_PASS_ERR	-1
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate #define	ARCHIVE_NORMAL	0
93*7c478bd9Sstevel@tonic-gate #define	ARCHIVE_ACL	1
94*7c478bd9Sstevel@tonic-gate #define	ARCHIVE_XATTR	2
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate #define	LSTAT(dir, path, statbuf) fstatat(dir, \
97*7c478bd9Sstevel@tonic-gate     get_component((Gen.g_attrnam_p == (char *)NULL) ? \
98*7c478bd9Sstevel@tonic-gate     path : Gen.g_attrnam_p), statbuf, AT_SYMLINK_NOFOLLOW)
99*7c478bd9Sstevel@tonic-gate #define	STAT(dir, path, statbuf) fstatat(dir, \
100*7c478bd9Sstevel@tonic-gate     get_component((Gen.g_attrnam_p == (char *)NULL) ? \
101*7c478bd9Sstevel@tonic-gate     path : Gen.g_attrnam_p), statbuf, 0)
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate /*
104*7c478bd9Sstevel@tonic-gate  *	These limits reflect the maximum size regular file that
105*7c478bd9Sstevel@tonic-gate  *	can be archived, depending on the archive type. For archives
106*7c478bd9Sstevel@tonic-gate  *	with character-format headers (odc, tar, ustar) we use
107*7c478bd9Sstevel@tonic-gate  *	CHAR_OFFSET_MAX. Otherwise, we are limited to the size
108*7c478bd9Sstevel@tonic-gate  *	that will fit in a signed long value.
109*7c478bd9Sstevel@tonic-gate  */
110*7c478bd9Sstevel@tonic-gate #define	CHAR_OFFSET_MAX	077777777777	/* 11 octal digits */
111*7c478bd9Sstevel@tonic-gate #define	BIN_OFFSET_MAX	LONG_MAX	/* signed long max value */
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate static char	aclchar = ' ';
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate static struct Lnk *add_lnk(struct Lnk **);
116*7c478bd9Sstevel@tonic-gate static int bfill(void);
117*7c478bd9Sstevel@tonic-gate static void bflush(void);
118*7c478bd9Sstevel@tonic-gate static int chgreel(int dir);
119*7c478bd9Sstevel@tonic-gate static int ckname(int);
120*7c478bd9Sstevel@tonic-gate static void ckopts(long mask);
121*7c478bd9Sstevel@tonic-gate static long cksum(char hdr, int byt_cnt, int *err);
122*7c478bd9Sstevel@tonic-gate static int creat_hdr(void);
123*7c478bd9Sstevel@tonic-gate static int creat_lnk(int dirfd, char *name1_p, char *name2_p);
124*7c478bd9Sstevel@tonic-gate static int creat_spec(int dirfd);
125*7c478bd9Sstevel@tonic-gate static int creat_tmp(char *nam_p);
126*7c478bd9Sstevel@tonic-gate static void data_in(int proc_mode);
127*7c478bd9Sstevel@tonic-gate static void data_out(void);
128*7c478bd9Sstevel@tonic-gate static void data_pass(void);
129*7c478bd9Sstevel@tonic-gate static void file_in(void);
130*7c478bd9Sstevel@tonic-gate static int file_out(void);
131*7c478bd9Sstevel@tonic-gate static int file_pass(void);
132*7c478bd9Sstevel@tonic-gate static void flush_lnks(void);
133*7c478bd9Sstevel@tonic-gate static int gethdr(void);
134*7c478bd9Sstevel@tonic-gate static int getname(void);
135*7c478bd9Sstevel@tonic-gate static void getpats(int largc, char **largv);
136*7c478bd9Sstevel@tonic-gate static void ioerror(int dir);
137*7c478bd9Sstevel@tonic-gate static int matched(void);
138*7c478bd9Sstevel@tonic-gate static int missdir(char *nam_p);
139*7c478bd9Sstevel@tonic-gate static long mklong(short v[]);
140*7c478bd9Sstevel@tonic-gate static void mkshort(short sval[], long v);
141*7c478bd9Sstevel@tonic-gate static void msg(int severity, const char *fmt, ...);
142*7c478bd9Sstevel@tonic-gate static int openout(int dirfd);
143*7c478bd9Sstevel@tonic-gate static int read_hdr(int hdr);
144*7c478bd9Sstevel@tonic-gate static void reclaim(struct Lnk *l_p);
145*7c478bd9Sstevel@tonic-gate static void rstbuf(void);
146*7c478bd9Sstevel@tonic-gate static void setpasswd(char *nam);
147*7c478bd9Sstevel@tonic-gate static void rstfiles(int over, int dirfd);
148*7c478bd9Sstevel@tonic-gate static void scan4trail(void);
149*7c478bd9Sstevel@tonic-gate static void setup(int largc, char **largv);
150*7c478bd9Sstevel@tonic-gate static void set_tym(int dirfd, char *nam_p, time_t atime, time_t mtime);
151*7c478bd9Sstevel@tonic-gate static void sigint(int sig);
152*7c478bd9Sstevel@tonic-gate static void swap(char *buf_p, int cnt);
153*7c478bd9Sstevel@tonic-gate static void usage(void);
154*7c478bd9Sstevel@tonic-gate static void verbose(char *nam_p);
155*7c478bd9Sstevel@tonic-gate static void write_hdr(int secflag, off_t len);
156*7c478bd9Sstevel@tonic-gate static void write_trail(void);
157*7c478bd9Sstevel@tonic-gate static int ustar_dir(void);
158*7c478bd9Sstevel@tonic-gate static int ustar_spec(void);
159*7c478bd9Sstevel@tonic-gate static struct stat *convert_to_old_stat(struct stat *, char *, char *);
160*7c478bd9Sstevel@tonic-gate static void read_bar_vol_hdr(void);
161*7c478bd9Sstevel@tonic-gate static void read_bar_file_hdr(void);
162*7c478bd9Sstevel@tonic-gate static void setup_uncompress(FILE **);
163*7c478bd9Sstevel@tonic-gate static void skip_bar_volhdr(void);
164*7c478bd9Sstevel@tonic-gate static void bar_file_in(void);
165*7c478bd9Sstevel@tonic-gate static int g_init(int *devtype, int *fdes);
166*7c478bd9Sstevel@tonic-gate static int g_read(int, int, char *, unsigned);
167*7c478bd9Sstevel@tonic-gate static int g_write(int, int, char *, unsigned);
168*7c478bd9Sstevel@tonic-gate static int is_floppy(int);
169*7c478bd9Sstevel@tonic-gate static int is_tape(int);
170*7c478bd9Sstevel@tonic-gate static int append_secattr(char **, int *, int, char *, char);
171*7c478bd9Sstevel@tonic-gate static void write_ancillary(char *secinfo, int len);
172*7c478bd9Sstevel@tonic-gate static int remove_dir(char *);
173*7c478bd9Sstevel@tonic-gate static int save_cwd(void);
174*7c478bd9Sstevel@tonic-gate static void rest_cwd(int cwd);
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate static void xattrs_out(int (*func)());
177*7c478bd9Sstevel@tonic-gate static void get_parent(char *path, char *dir);
178*7c478bd9Sstevel@tonic-gate static void prepare_xattr_hdr(char **attrbuf, char *filename,
179*7c478bd9Sstevel@tonic-gate     char *attrname, char typeflag, struct Lnk *linkinfo, int *rlen);
180*7c478bd9Sstevel@tonic-gate static char tartype(int type);
181*7c478bd9Sstevel@tonic-gate static int openfile(int omode);
182*7c478bd9Sstevel@tonic-gate static mode_t attrmode(char type);
183*7c478bd9Sstevel@tonic-gate static char *get_component(char *path);
184*7c478bd9Sstevel@tonic-gate static int open_dir(char *name);
185*7c478bd9Sstevel@tonic-gate static int open_dirfd();
186*7c478bd9Sstevel@tonic-gate static void close_dirfd();
187*7c478bd9Sstevel@tonic-gate static void write_xattr_hdr();
188*7c478bd9Sstevel@tonic-gate static int retry_attrdir_open(char *name);
189*7c478bd9Sstevel@tonic-gate static char *skipslashes(char *string, char *start);
190*7c478bd9Sstevel@tonic-gate static int read_xattr_hdr();
191*7c478bd9Sstevel@tonic-gate static void chop_endslashes(char *path);
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate /* helpful types */
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate static
197*7c478bd9Sstevel@tonic-gate struct passwd	*Curpw_p,	/* Current password entry for -t option */
198*7c478bd9Sstevel@tonic-gate 		*Rpw_p,		/* Password entry for -R option */
199*7c478bd9Sstevel@tonic-gate 		*dpasswd;
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate static
202*7c478bd9Sstevel@tonic-gate struct group	*Curgr_p,	/* Current group entry for -t option */
203*7c478bd9Sstevel@tonic-gate 		*dgroup;
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate /* Data structure for buffered I/O. */
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate static
208*7c478bd9Sstevel@tonic-gate struct buf_info {
209*7c478bd9Sstevel@tonic-gate 	char	*b_base_p,	/* Pointer to base of buffer */
210*7c478bd9Sstevel@tonic-gate 		*b_out_p,	/* Position to take bytes from buffer at */
211*7c478bd9Sstevel@tonic-gate 		*b_in_p,	/* Position to put bytes into buffer at */
212*7c478bd9Sstevel@tonic-gate 		*b_end_p;	/* Pointer to end of buffer */
213*7c478bd9Sstevel@tonic-gate 	long	b_cnt,		/* Count of unprocessed bytes */
214*7c478bd9Sstevel@tonic-gate 		b_size;		/* Size of buffer in bytes */
215*7c478bd9Sstevel@tonic-gate } Buffr;
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate /* Generic header format */
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate static
220*7c478bd9Sstevel@tonic-gate struct gen_hdr {
221*7c478bd9Sstevel@tonic-gate 	ulong_t	g_magic,	/* Magic number field */
222*7c478bd9Sstevel@tonic-gate 		g_ino,		/* Inode number of file */
223*7c478bd9Sstevel@tonic-gate 		g_mode,		/* Mode of file */
224*7c478bd9Sstevel@tonic-gate 		g_uid,		/* Uid of file */
225*7c478bd9Sstevel@tonic-gate 		g_gid,		/* Gid of file */
226*7c478bd9Sstevel@tonic-gate 		g_nlink,	/* Number of links */
227*7c478bd9Sstevel@tonic-gate 		g_mtime;	/* Modification time */
228*7c478bd9Sstevel@tonic-gate 	off_t	g_filesz;	/* Length of file */
229*7c478bd9Sstevel@tonic-gate 	ulong_t	g_dev,		/* File system of file */
230*7c478bd9Sstevel@tonic-gate 		g_rdev,		/* Major/minor numbers of special files */
231*7c478bd9Sstevel@tonic-gate 		g_namesz,	/* Length of filename */
232*7c478bd9Sstevel@tonic-gate 		g_cksum;	/* Checksum of file */
233*7c478bd9Sstevel@tonic-gate 	char	g_gname[32],
234*7c478bd9Sstevel@tonic-gate 		g_uname[32],
235*7c478bd9Sstevel@tonic-gate 		g_version[2],
236*7c478bd9Sstevel@tonic-gate 		g_tmagic[6],
237*7c478bd9Sstevel@tonic-gate 		g_typeflag;
238*7c478bd9Sstevel@tonic-gate 	char	*g_tname,
239*7c478bd9Sstevel@tonic-gate 		*g_prefix,
240*7c478bd9Sstevel@tonic-gate 		*g_nam_p,	/* Filename */
241*7c478bd9Sstevel@tonic-gate 		*g_attrnam_p,	/* attribute */
242*7c478bd9Sstevel@tonic-gate 		*g_attrfnam_p,  /* Real file name attr belongs to */
243*7c478bd9Sstevel@tonic-gate 		*g_linktoattrfnam_p, /* file linked attribute belongs to */
244*7c478bd9Sstevel@tonic-gate 		*g_linktoattrnam_p,  /* attribute g_attrnam_p is linked to */
245*7c478bd9Sstevel@tonic-gate 		*g_dirpath;	/* dirname currently opened */
246*7c478bd9Sstevel@tonic-gate 	int	g_dirfd;	/* directory file descriptor */
247*7c478bd9Sstevel@tonic-gate 	int	g_passdirfd;	/* directory fd to pass to */
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate } Gen, *G_p;
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate /* Data structure for handling multiply-linked files */
252*7c478bd9Sstevel@tonic-gate static
253*7c478bd9Sstevel@tonic-gate char	prebuf[PRESIZ+1],
254*7c478bd9Sstevel@tonic-gate 	nambuf[NAMSIZ+1],
255*7c478bd9Sstevel@tonic-gate 	fullnam[MAXNAM+1];
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate static
259*7c478bd9Sstevel@tonic-gate struct Lnk {
260*7c478bd9Sstevel@tonic-gate 	short	L_cnt,		/* Number of links encountered */
261*7c478bd9Sstevel@tonic-gate 		L_data;		/* Data has been encountered if 1 */
262*7c478bd9Sstevel@tonic-gate 	struct gen_hdr	L_gen;	/* gen_hdr information for this file */
263*7c478bd9Sstevel@tonic-gate 	struct Lnk	*L_nxt_p,	/* Next file in list */
264*7c478bd9Sstevel@tonic-gate 			*L_bck_p,	/* Previous file in list */
265*7c478bd9Sstevel@tonic-gate 			*L_lnk_p;	/* Next link for this file */
266*7c478bd9Sstevel@tonic-gate } Lnk_hd;
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate static
269*7c478bd9Sstevel@tonic-gate struct hdr_cpio	Hdr;
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate /*
272*7c478bd9Sstevel@tonic-gate  * -------------------------------------------------------------------------
273*7c478bd9Sstevel@tonic-gate  *		   Stuff needed to pre-view the name stream
274*7c478bd9Sstevel@tonic-gate  *
275*7c478bd9Sstevel@tonic-gate  * issymlink is used to remember that the current file is a symlink between
276*7c478bd9Sstevel@tonic-gate  * getname() and file_pass(); the former trashes this information immediately
277*7c478bd9Sstevel@tonic-gate  * when -L is specified.
278*7c478bd9Sstevel@tonic-gate  */
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate static
281*7c478bd9Sstevel@tonic-gate int	issymlink = 0;
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate static
284*7c478bd9Sstevel@tonic-gate FILE	*In_p = stdin;		/* Where the input comes from */
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate typedef struct sl_info
287*7c478bd9Sstevel@tonic-gate {
288*7c478bd9Sstevel@tonic-gate 	struct sl_info *llink;	/* Left subtree ptr (tree depth in *sl_head) */
289*7c478bd9Sstevel@tonic-gate 	struct sl_info *rlink;	/* Right subtree ptr */
290*7c478bd9Sstevel@tonic-gate 	int bal;		/* Subtree balance factor */
291*7c478bd9Sstevel@tonic-gate 	ulong_t	sl_count;	/* Number of symlinks */
292*7c478bd9Sstevel@tonic-gate 	ino_t	sl_ino;		/* Inode of file */
293*7c478bd9Sstevel@tonic-gate 	ino_t	sl_ino2;	/* alternate inode for -Hodc */
294*7c478bd9Sstevel@tonic-gate } sl_info_t;
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate /*
297*7c478bd9Sstevel@tonic-gate  * The following structure maintains a hash entry for the
298*7c478bd9Sstevel@tonic-gate  * balancing trees which are allocated for each device nodes.
299*7c478bd9Sstevel@tonic-gate  */
300*7c478bd9Sstevel@tonic-gate typedef struct sl_info_link
301*7c478bd9Sstevel@tonic-gate {
302*7c478bd9Sstevel@tonic-gate 	dev_t		dev;
303*7c478bd9Sstevel@tonic-gate 	sl_info_t	*head;
304*7c478bd9Sstevel@tonic-gate 	struct sl_info_link *next;
305*7c478bd9Sstevel@tonic-gate } sl_info_link_t;
306*7c478bd9Sstevel@tonic-gate 
307*7c478bd9Sstevel@tonic-gate #define	SL_INFO_ALLOC_CHUNK	1024
308*7c478bd9Sstevel@tonic-gate #define	NDEVHENTRY		0x40
309*7c478bd9Sstevel@tonic-gate #define	DEV_HASHKEY(x)		((x) & (NDEVHENTRY -1))
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate /*
312*7c478bd9Sstevel@tonic-gate  * For remapping dev,inode for -Hodc archives.
313*7c478bd9Sstevel@tonic-gate  */
314*7c478bd9Sstevel@tonic-gate 
315*7c478bd9Sstevel@tonic-gate typedef struct sl_remap
316*7c478bd9Sstevel@tonic-gate {
317*7c478bd9Sstevel@tonic-gate 	dev_t			dev;		/* device */
318*7c478bd9Sstevel@tonic-gate 	int			inode_count;	/* # inodes seen on dev */
319*7c478bd9Sstevel@tonic-gate 	struct sl_remap 	*next;		/* next in the chain */
320*7c478bd9Sstevel@tonic-gate } sl_remap_t;
321*7c478bd9Sstevel@tonic-gate 
322*7c478bd9Sstevel@tonic-gate /* forward declarations */
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate static sl_info_t 	*sl_info_alloc(void);
325*7c478bd9Sstevel@tonic-gate static sl_info_t 	*sl_insert(dev_t, ino_t);
326*7c478bd9Sstevel@tonic-gate static ulong_t		sl_numlinks(dev_t, ino_t);
327*7c478bd9Sstevel@tonic-gate static void		sl_preview_synonyms(void);
328*7c478bd9Sstevel@tonic-gate static void		sl_remember_tgt(const struct stat *, int);
329*7c478bd9Sstevel@tonic-gate static sl_info_t 	*sl_search(dev_t, ino_t);
330*7c478bd9Sstevel@tonic-gate static sl_info_t	*sl_devhash_lookup(dev_t);
331*7c478bd9Sstevel@tonic-gate static void		sl_devhash_insert(dev_t, sl_info_t *);
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate extern int		sl_compare(ino_t, ino_t);
334*7c478bd9Sstevel@tonic-gate #define	sl_compare(lino, rino)	(lino < rino ? -1 : (lino > rino ? 1 : 0))
335*7c478bd9Sstevel@tonic-gate 
336*7c478bd9Sstevel@tonic-gate /* global storage */
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate static sl_remap_t  *sl_remap_head = NULL; /* head of the inode-remap list */
339*7c478bd9Sstevel@tonic-gate static sl_info_link_t	*sl_devhash[NDEVHENTRY]; /* hash table */
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate /*
342*7c478bd9Sstevel@tonic-gate  * -------------------------------------------------------------------------
343*7c478bd9Sstevel@tonic-gate  */
344*7c478bd9Sstevel@tonic-gate 
345*7c478bd9Sstevel@tonic-gate static
346*7c478bd9Sstevel@tonic-gate struct stat	ArchSt,	/* stat(2) information of the archive */
347*7c478bd9Sstevel@tonic-gate 		SrcSt,	/* stat(2) information of source file */
348*7c478bd9Sstevel@tonic-gate 		DesSt,	/* stat(2) of destination file */
349*7c478bd9Sstevel@tonic-gate 		*OldSt = NULL;	/* stat info converted to svr32 format */
350*7c478bd9Sstevel@tonic-gate 
351*7c478bd9Sstevel@tonic-gate /*
352*7c478bd9Sstevel@tonic-gate  * bin_mag: Used to validate a binary magic number,
353*7c478bd9Sstevel@tonic-gate  * by combining to bytes into an unsigned short.
354*7c478bd9Sstevel@tonic-gate  */
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate static
357*7c478bd9Sstevel@tonic-gate union bin_mag {
358*7c478bd9Sstevel@tonic-gate 	unsigned char b_byte[2];
359*7c478bd9Sstevel@tonic-gate 	ushort_t b_half;
360*7c478bd9Sstevel@tonic-gate } Binmag;
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate static
363*7c478bd9Sstevel@tonic-gate union tblock *Thdr_p;	/* TAR header pointer */
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate static union b_block *bar_Vhdr;
366*7c478bd9Sstevel@tonic-gate static struct gen_hdr Gen_bar_vol;
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate /*
369*7c478bd9Sstevel@tonic-gate  * swpbuf: Used in swap() to swap bytes within a halfword,
370*7c478bd9Sstevel@tonic-gate  * halfwords within a word, or to reverse the order of the
371*7c478bd9Sstevel@tonic-gate  * bytes within a word.  Also used in mklong() and mkshort().
372*7c478bd9Sstevel@tonic-gate  */
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate static
375*7c478bd9Sstevel@tonic-gate union swpbuf {
376*7c478bd9Sstevel@tonic-gate 	unsigned char	s_byte[4];
377*7c478bd9Sstevel@tonic-gate 	ushort_t	s_half[2];
378*7c478bd9Sstevel@tonic-gate 	ulong_t	s_word;
379*7c478bd9Sstevel@tonic-gate } *Swp_p;
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate static
382*7c478bd9Sstevel@tonic-gate char	Adir,			/* Flags object as a directory */
383*7c478bd9Sstevel@tonic-gate 	Hiddendir,		/* Processing hidden attribute directory */
384*7c478bd9Sstevel@tonic-gate 	Aspec,			/* Flags object as a special file */
385*7c478bd9Sstevel@tonic-gate 	Do_rename,		/* Indicates rename() is to be used */
386*7c478bd9Sstevel@tonic-gate 	Time[50],		/* Array to hold date and time */
387*7c478bd9Sstevel@tonic-gate 	Ttyname[] = "/dev/tty",	/* Controlling console */
388*7c478bd9Sstevel@tonic-gate 	T_lname[MAXPATHLEN],	/* Array to hold links name for tar */
389*7c478bd9Sstevel@tonic-gate 	*Buf_p,			/* Buffer for file system I/O */
390*7c478bd9Sstevel@tonic-gate 	*Empty,			/* Empty block for TARTYP headers */
391*7c478bd9Sstevel@tonic-gate 	*Full_p,		/* Pointer to full pathname */
392*7c478bd9Sstevel@tonic-gate 	*Efil_p,		/* -E pattern file string */
393*7c478bd9Sstevel@tonic-gate 	*Eom_p = "Change to part %d and press RETURN key. [q] ",
394*7c478bd9Sstevel@tonic-gate 	*Fullnam_p,		/* Full pathname */
395*7c478bd9Sstevel@tonic-gate 	*Attrfile_p,		/* attribute file */
396*7c478bd9Sstevel@tonic-gate 	*Hdr_p,			/* -H header type string */
397*7c478bd9Sstevel@tonic-gate 	*IOfil_p,		/* -I/-O input/output archive string */
398*7c478bd9Sstevel@tonic-gate 	*Lnkend_p,		/* Pointer to end of Lnknam_p */
399*7c478bd9Sstevel@tonic-gate 	*Lnknam_p,		/* Buffer for linking files with -p option */
400*7c478bd9Sstevel@tonic-gate 	*Nam_p,			/* Array to hold filename */
401*7c478bd9Sstevel@tonic-gate 	*Savenam_p,		/* copy of filename xattr belongs to */
402*7c478bd9Sstevel@tonic-gate 	*Own_p,			/* New owner login id string */
403*7c478bd9Sstevel@tonic-gate 	*Renam_p,		/* Buffer for renaming files */
404*7c478bd9Sstevel@tonic-gate 	*Renametmp_p,		/* Tmp Buffer for renaming files */
405*7c478bd9Sstevel@tonic-gate 	*Symlnk_p,		/* Buffer for holding symbolic link name */
406*7c478bd9Sstevel@tonic-gate 	*Over_p,		/* Holds temporary filename when overwriting */
407*7c478bd9Sstevel@tonic-gate 	**Pat_pp = 0,		/* Pattern strings */
408*7c478bd9Sstevel@tonic-gate 	bar_linkflag,		/* flag to indicate if the file is a link */
409*7c478bd9Sstevel@tonic-gate 	bar_linkname[MAXPATHLEN]; /* store the name of the link */
410*7c478bd9Sstevel@tonic-gate 
411*7c478bd9Sstevel@tonic-gate static
412*7c478bd9Sstevel@tonic-gate int	Append = 0,	/* Flag set while searching to end of archive */
413*7c478bd9Sstevel@tonic-gate 	Archive,	/* File descriptor of the archive */
414*7c478bd9Sstevel@tonic-gate 	Buf_error = 0,	/* I/O error occured during buffer fill */
415*7c478bd9Sstevel@tonic-gate 	Def_mode = 0777,	/* Default file/directory protection modes */
416*7c478bd9Sstevel@tonic-gate 	Device,		/* Device type being accessed (used with libgenIO) */
417*7c478bd9Sstevel@tonic-gate 	Error_cnt = 0,	/* Cumulative count of I/O errors */
418*7c478bd9Sstevel@tonic-gate 	Finished = 1,	/* Indicates that a file transfer has completed */
419*7c478bd9Sstevel@tonic-gate 	Hdrsz = ASCSZ,	/* Fixed length portion of the header */
420*7c478bd9Sstevel@tonic-gate 	Hdr_type,		/* Flag to indicate type of header selected */
421*7c478bd9Sstevel@tonic-gate 	Ifile,		/* File des. of file being archived */
422*7c478bd9Sstevel@tonic-gate 	Ofile,		/* File des. of file being extracted from archive */
423*7c478bd9Sstevel@tonic-gate 	Use_old_stat = 0,    /* Create an old style -Hodc hdr (small dev's) */
424*7c478bd9Sstevel@tonic-gate 	Onecopy = 0,	/* Flags old vs. new link handling */
425*7c478bd9Sstevel@tonic-gate 	Pad_val = 0,	/* Indicates the number of bytes to pad (if any) */
426*7c478bd9Sstevel@tonic-gate 	PageSize = 0,	/* The native page size, used for figuring block size */
427*7c478bd9Sstevel@tonic-gate 	Volcnt = 1,	/* Number of archive volumes processed */
428*7c478bd9Sstevel@tonic-gate 	Verbcnt = 0,	/* Count of number of dots '.' output */
429*7c478bd9Sstevel@tonic-gate 	Eomflag = 0,
430*7c478bd9Sstevel@tonic-gate 	Dflag = 0,
431*7c478bd9Sstevel@tonic-gate 	Atflag = 0,	/* Archive/restore extended attributes */
432*7c478bd9Sstevel@tonic-gate 	Compressed,	/* Flag to indicate if the bar archive is compressed */
433*7c478bd9Sstevel@tonic-gate 	Bar_vol_num = 0; /* Volume number count for bar archive */
434*7c478bd9Sstevel@tonic-gate 
435*7c478bd9Sstevel@tonic-gate 
436*7c478bd9Sstevel@tonic-gate static
437*7c478bd9Sstevel@tonic-gate gid_t	Lastgid = -1;	/* Used with -t & -v to record current gid */
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate static
440*7c478bd9Sstevel@tonic-gate uid_t	Lastuid = -1;	/* Used with -t & -v to record current uid */
441*7c478bd9Sstevel@tonic-gate 
442*7c478bd9Sstevel@tonic-gate static
443*7c478bd9Sstevel@tonic-gate long	Args,		/* Mask of selected options */
444*7c478bd9Sstevel@tonic-gate 	Max_namesz = CPATH;	/* Maximum size of pathnames/filenames */
445*7c478bd9Sstevel@tonic-gate 
446*7c478bd9Sstevel@tonic-gate static
447*7c478bd9Sstevel@tonic-gate int	Bufsize = BUFSZ;	/* Default block size */
448*7c478bd9Sstevel@tonic-gate 
449*7c478bd9Sstevel@tonic-gate 
450*7c478bd9Sstevel@tonic-gate static u_longlong_t    Blocks;	/* full blocks transferred */
451*7c478bd9Sstevel@tonic-gate static u_longlong_t    SBlocks;	/* cumulative char count from short reads */
452*7c478bd9Sstevel@tonic-gate 
453*7c478bd9Sstevel@tonic-gate 
454*7c478bd9Sstevel@tonic-gate static off_t	Max_offset = BIN_OFFSET_MAX;	/* largest file size */
455*7c478bd9Sstevel@tonic-gate static off_t	Max_filesz;			/* from getrlimit */
456*7c478bd9Sstevel@tonic-gate 
457*7c478bd9Sstevel@tonic-gate static
458*7c478bd9Sstevel@tonic-gate FILE	*Ef_p,			/* File pointer of pattern input file */
459*7c478bd9Sstevel@tonic-gate 	*Err_p = stderr,	/* File pointer for error reporting */
460*7c478bd9Sstevel@tonic-gate 	*Out_p = stdout,	/* File pointer for non-archive output */
461*7c478bd9Sstevel@tonic-gate 	*Rtty_p,		/* Input file pointer for interactive rename */
462*7c478bd9Sstevel@tonic-gate 	*Wtty_p;		/* Output file ptr for interactive rename */
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate static
465*7c478bd9Sstevel@tonic-gate ushort_t	Ftype = S_IFMT;	/* File type mask */
466*7c478bd9Sstevel@tonic-gate static
467*7c478bd9Sstevel@tonic-gate uid_t	Euid;		/* Effective uid of invoker */
468*7c478bd9Sstevel@tonic-gate 
469*7c478bd9Sstevel@tonic-gate /* ACL support */
470*7c478bd9Sstevel@tonic-gate static struct sec_attr {
471*7c478bd9Sstevel@tonic-gate 	char	attr_type;
472*7c478bd9Sstevel@tonic-gate 	char	attr_len[7];
473*7c478bd9Sstevel@tonic-gate 	char	attr_info[1];
474*7c478bd9Sstevel@tonic-gate } *attr;
475*7c478bd9Sstevel@tonic-gate 
476*7c478bd9Sstevel@tonic-gate static int	Pflag = 0;	/* flag indicates that acl is preserved */
477*7c478bd9Sstevel@tonic-gate static int	aclcnt = 0;	/* acl entry count */
478*7c478bd9Sstevel@tonic-gate static aclent_t *aclp = NULL;	/* pointer to ACL */
479*7c478bd9Sstevel@tonic-gate static int	acl_set = 0;	/* True if an acl was set on the file */
480*7c478bd9Sstevel@tonic-gate 
481*7c478bd9Sstevel@tonic-gate /*
482*7c478bd9Sstevel@tonic-gate  *
483*7c478bd9Sstevel@tonic-gate  * cpio has been changed to support extended attributes.
484*7c478bd9Sstevel@tonic-gate  *
485*7c478bd9Sstevel@tonic-gate  * As part of this change cpio has been changed to use the new *at() syscalls
486*7c478bd9Sstevel@tonic-gate  * such as openat, fchownat(), unlinkat()...
487*7c478bd9Sstevel@tonic-gate  *
488*7c478bd9Sstevel@tonic-gate  * This was done so that attributes can be handled with as few code changes
489*7c478bd9Sstevel@tonic-gate  * as possible.
490*7c478bd9Sstevel@tonic-gate  *
491*7c478bd9Sstevel@tonic-gate  * What this means is that cpio now opens the directory that a file or directory
492*7c478bd9Sstevel@tonic-gate  * resides in and then performs *at() functions to manipulate the entry.
493*7c478bd9Sstevel@tonic-gate  *
494*7c478bd9Sstevel@tonic-gate  * For example a new file is now created like this:
495*7c478bd9Sstevel@tonic-gate  *
496*7c478bd9Sstevel@tonic-gate  * dfd = open(<some dir path>)
497*7c478bd9Sstevel@tonic-gate  * fd = openat(dfd, <name>,....);
498*7c478bd9Sstevel@tonic-gate  *
499*7c478bd9Sstevel@tonic-gate  * or in the case of an extended attribute
500*7c478bd9Sstevel@tonic-gate  *
501*7c478bd9Sstevel@tonic-gate  * dfd = attropen(<pathname>, ".", ....)
502*7c478bd9Sstevel@tonic-gate  *
503*7c478bd9Sstevel@tonic-gate  * Once we have a directory file descriptor all of the *at() functions can
504*7c478bd9Sstevel@tonic-gate  * be applied to it.
505*7c478bd9Sstevel@tonic-gate  *
506*7c478bd9Sstevel@tonic-gate  * unlinkat(dfd, <component name>,...)
507*7c478bd9Sstevel@tonic-gate  * fchownat(dfd, <component name>,..)
508*7c478bd9Sstevel@tonic-gate  *
509*7c478bd9Sstevel@tonic-gate  * This works for both normal namespace files and extended attribute file
510*7c478bd9Sstevel@tonic-gate  *
511*7c478bd9Sstevel@tonic-gate  */
512*7c478bd9Sstevel@tonic-gate 
513*7c478bd9Sstevel@tonic-gate /*
514*7c478bd9Sstevel@tonic-gate  * Extended attribute layout
515*7c478bd9Sstevel@tonic-gate  *
516*7c478bd9Sstevel@tonic-gate  * Extended attributes are stored in two pieces.
517*7c478bd9Sstevel@tonic-gate  * 1. An attribute header which has information about
518*7c478bd9Sstevel@tonic-gate  *    what file the attribute is for and what the attribute
519*7c478bd9Sstevel@tonic-gate  *    is named.
520*7c478bd9Sstevel@tonic-gate  * 2. The attribute record itself.  Stored as a normal file type
521*7c478bd9Sstevel@tonic-gate  *    of entry.
522*7c478bd9Sstevel@tonic-gate  * Both the header and attribute record have special modes/typeflags
523*7c478bd9Sstevel@tonic-gate  * associated with them.
524*7c478bd9Sstevel@tonic-gate  *
525*7c478bd9Sstevel@tonic-gate  * The names of the header in the archive look like:
526*7c478bd9Sstevel@tonic-gate  * /dev/null/attr.hdr
527*7c478bd9Sstevel@tonic-gate  *
528*7c478bd9Sstevel@tonic-gate  * The name of the attribute looks like:
529*7c478bd9Sstevel@tonic-gate  * /dev/null/attr.
530*7c478bd9Sstevel@tonic-gate  *
531*7c478bd9Sstevel@tonic-gate  * This is done so that an archiver that doesn't understand these formats
532*7c478bd9Sstevel@tonic-gate  * can just dispose of the attribute records unless the user chooses to
533*7c478bd9Sstevel@tonic-gate  * rename them via cpio -r or pax -i
534*7c478bd9Sstevel@tonic-gate  *
535*7c478bd9Sstevel@tonic-gate  * The format is composed of a fixed size header followed
536*7c478bd9Sstevel@tonic-gate  * by a variable sized xattr_buf. If the attribute is a hard link
537*7c478bd9Sstevel@tonic-gate  * to another attribute, then another xattr_buf section is included
538*7c478bd9Sstevel@tonic-gate  * for the link.
539*7c478bd9Sstevel@tonic-gate  *
540*7c478bd9Sstevel@tonic-gate  * The xattr_buf is used to define the necessary "pathing" steps
541*7c478bd9Sstevel@tonic-gate  * to get to the extended attribute.  This is necessary to support
542*7c478bd9Sstevel@tonic-gate  * a fully recursive attribute model where an attribute may itself
543*7c478bd9Sstevel@tonic-gate  * have an attribute.
544*7c478bd9Sstevel@tonic-gate  *
545*7c478bd9Sstevel@tonic-gate  * The basic layout looks like this.
546*7c478bd9Sstevel@tonic-gate  *
547*7c478bd9Sstevel@tonic-gate  *     --------------------------------
548*7c478bd9Sstevel@tonic-gate  *     |                              |
549*7c478bd9Sstevel@tonic-gate  *     |         xattr_hdr            |
550*7c478bd9Sstevel@tonic-gate  *     |                              |
551*7c478bd9Sstevel@tonic-gate  *     --------------------------------
552*7c478bd9Sstevel@tonic-gate  *     --------------------------------
553*7c478bd9Sstevel@tonic-gate  *     |                              |
554*7c478bd9Sstevel@tonic-gate  *     |        xattr_buf             |
555*7c478bd9Sstevel@tonic-gate  *     |                              |
556*7c478bd9Sstevel@tonic-gate  *     --------------------------------
557*7c478bd9Sstevel@tonic-gate  *     --------------------------------
558*7c478bd9Sstevel@tonic-gate  *     |                              |
559*7c478bd9Sstevel@tonic-gate  *     |      (optional link info)    |
560*7c478bd9Sstevel@tonic-gate  *     |                              |
561*7c478bd9Sstevel@tonic-gate  *     --------------------------------
562*7c478bd9Sstevel@tonic-gate  *     --------------------------------
563*7c478bd9Sstevel@tonic-gate  *     |                              |
564*7c478bd9Sstevel@tonic-gate  *     |      attribute itself        |
565*7c478bd9Sstevel@tonic-gate  *     |      stored as normal tar    |
566*7c478bd9Sstevel@tonic-gate  *     |      or cpio data with       |
567*7c478bd9Sstevel@tonic-gate  *     |      special mode or         |
568*7c478bd9Sstevel@tonic-gate  *     |      typeflag                |
569*7c478bd9Sstevel@tonic-gate  *     |                              |
570*7c478bd9Sstevel@tonic-gate  *     --------------------------------
571*7c478bd9Sstevel@tonic-gate  *
572*7c478bd9Sstevel@tonic-gate  */
573*7c478bd9Sstevel@tonic-gate 
574*7c478bd9Sstevel@tonic-gate /*
575*7c478bd9Sstevel@tonic-gate  * Extended attributes structures
576*7c478bd9Sstevel@tonic-gate  *
577*7c478bd9Sstevel@tonic-gate  * xattrhead is the complete extended attribute header, as read of off
578*7c478bd9Sstevel@tonic-gate  * disk/tape. It includes the variable xattr_buf portion.
579*7c478bd9Sstevel@tonic-gate  *
580*7c478bd9Sstevel@tonic-gate  * xattrp is basically an offset into xattrhead that points to the
581*7c478bd9Sstevel@tonic-gate  * "pathing" section which defines how to get to the attribute.
582*7c478bd9Sstevel@tonic-gate  *
583*7c478bd9Sstevel@tonic-gate  * xattr_linkp is identical to xattrp except that it is used for linked
584*7c478bd9Sstevel@tonic-gate  * attributes.  It provides the pathing steps to get to the linked
585*7c478bd9Sstevel@tonic-gate  * attribute.
586*7c478bd9Sstevel@tonic-gate  *
587*7c478bd9Sstevel@tonic-gate  * These structures are updated when an extended attribute header is read off
588*7c478bd9Sstevel@tonic-gate  * of disk/tape.
589*7c478bd9Sstevel@tonic-gate  */
590*7c478bd9Sstevel@tonic-gate static struct xattr_hdr	*xattrhead;
591*7c478bd9Sstevel@tonic-gate static struct xattr_buf	*xattrp;
592*7c478bd9Sstevel@tonic-gate static struct xattr_buf	*xattr_linkp;
593*7c478bd9Sstevel@tonic-gate static int 		xattrbadhead;	/* is extended attribute header bad? */
594*7c478bd9Sstevel@tonic-gate 
595*7c478bd9Sstevel@tonic-gate static int	append_secattr(char **, int *, int, char *, char);
596*7c478bd9Sstevel@tonic-gate static void	write_ancillary(char *, int);
597*7c478bd9Sstevel@tonic-gate 
598*7c478bd9Sstevel@tonic-gate /*
599*7c478bd9Sstevel@tonic-gate  * Note regarding cpio and changes to ensure cpio doesn't try to second
600*7c478bd9Sstevel@tonic-gate  * guess whether it runs with sufficient privileges or not:
601*7c478bd9Sstevel@tonic-gate  *
602*7c478bd9Sstevel@tonic-gate  * cpio has been changed so that it doesn't carry a second implementation of
603*7c478bd9Sstevel@tonic-gate  * the kernel's policy with respect to privileges.  Instead of attempting
604*7c478bd9Sstevel@tonic-gate  * to restore uid and gid from an archive only if cpio is run as uid 0,
605*7c478bd9Sstevel@tonic-gate  * cpio now *always* tries to restore the uid and gid from the archive.
606*7c478bd9Sstevel@tonic-gate  * If the restoration of the uid and gid are unsuccessful, then an error
607*7c478bd9Sstevel@tonic-gate  * message will only be received if cpio is being run with root privileges,
608*7c478bd9Sstevel@tonic-gate  * i.e., effective uid (euid) is 0, or if -R was specified.
609*7c478bd9Sstevel@tonic-gate  *
610*7c478bd9Sstevel@tonic-gate  * Note regarding debugging mechanism for cpio:
611*7c478bd9Sstevel@tonic-gate  *
612*7c478bd9Sstevel@tonic-gate  * The following mechanism is provided to allow us to debug cpio in complicated
613*7c478bd9Sstevel@tonic-gate  * situations, like when it is part of a pipe.  The idea is that you compile
614*7c478bd9Sstevel@tonic-gate  * with -DWAITAROUND defined, and then add the "-z" command line option to the
615*7c478bd9Sstevel@tonic-gate  * target cpio invocation.  If stderr is available, it will tell you to which
616*7c478bd9Sstevel@tonic-gate  * pid to attach the debugger; otherwise, use ps to find it.  Attach to the
617*7c478bd9Sstevel@tonic-gate  * process from the debugger, and, *PRESTO*, you are there!
618*7c478bd9Sstevel@tonic-gate  *
619*7c478bd9Sstevel@tonic-gate  * Simply assign "waitaround = 0" once you attach to the process, and then
620*7c478bd9Sstevel@tonic-gate  * proceed from there as usual.
621*7c478bd9Sstevel@tonic-gate  */
622*7c478bd9Sstevel@tonic-gate 
623*7c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
624*7c478bd9Sstevel@tonic-gate int waitaround = 0;		/* wait for rendezvous with the debugger */
625*7c478bd9Sstevel@tonic-gate #endif
626*7c478bd9Sstevel@tonic-gate 
627*7c478bd9Sstevel@tonic-gate /*
628*7c478bd9Sstevel@tonic-gate  * Allocation wrappers and their flags
629*7c478bd9Sstevel@tonic-gate  */
630*7c478bd9Sstevel@tonic-gate #define	E_NORMAL	0x0	/* Return NULL if allocation fails */
631*7c478bd9Sstevel@tonic-gate #define	E_EXIT		0x1	/* Exit if allocation fails */
632*7c478bd9Sstevel@tonic-gate 
633*7c478bd9Sstevel@tonic-gate static void *e_realloc(int flag, void *old, size_t newsize);
634*7c478bd9Sstevel@tonic-gate static char *e_strdup(int flag, const char *arg);
635*7c478bd9Sstevel@tonic-gate static void *e_valloc(int flag, size_t size);
636*7c478bd9Sstevel@tonic-gate static void *e_zalloc(int flag, size_t size);
637*7c478bd9Sstevel@tonic-gate 
638*7c478bd9Sstevel@tonic-gate #define	EXIT_CODE	(Error_cnt > 255 ? 255 : Error_cnt)
639*7c478bd9Sstevel@tonic-gate 
640*7c478bd9Sstevel@tonic-gate /*
641*7c478bd9Sstevel@tonic-gate  * main: Call setup() to process options and perform initializations,
642*7c478bd9Sstevel@tonic-gate  * and then select either copy in (-i), copy out (-o), or pass (-p) action.
643*7c478bd9Sstevel@tonic-gate  */
644*7c478bd9Sstevel@tonic-gate 
645*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
646*7c478bd9Sstevel@tonic-gate {
647*7c478bd9Sstevel@tonic-gate 	int i;
648*7c478bd9Sstevel@tonic-gate 	int passret;
649*7c478bd9Sstevel@tonic-gate 
650*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
651*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
652*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
653*7c478bd9Sstevel@tonic-gate #endif
654*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
655*7c478bd9Sstevel@tonic-gate 
656*7c478bd9Sstevel@tonic-gate 	(void) memset(&Gen, 0, sizeof (Gen));
657*7c478bd9Sstevel@tonic-gate 	setup(argc, argv);
658*7c478bd9Sstevel@tonic-gate 
659*7c478bd9Sstevel@tonic-gate 	if (signal(SIGINT, sigint) == SIG_IGN)
660*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, SIG_IGN);
661*7c478bd9Sstevel@tonic-gate 	switch (Args & (OCi | OCo | OCp)) {
662*7c478bd9Sstevel@tonic-gate 	case OCi: /* COPY IN */
663*7c478bd9Sstevel@tonic-gate 		Hdr_type = NONE;
664*7c478bd9Sstevel@tonic-gate 		while ((i = gethdr()) != 0) {
665*7c478bd9Sstevel@tonic-gate 			Gen.g_dirfd = -1;
666*7c478bd9Sstevel@tonic-gate 			if (i == 1) {
667*7c478bd9Sstevel@tonic-gate 				file_in();
668*7c478bd9Sstevel@tonic-gate 				/*
669*7c478bd9Sstevel@tonic-gate 				 * Any ACL info for this file would or should
670*7c478bd9Sstevel@tonic-gate 				 * have been used after file_in(); clear out
671*7c478bd9Sstevel@tonic-gate 				 * aclp so it is is not erroneously used on
672*7c478bd9Sstevel@tonic-gate 				 * the next file.
673*7c478bd9Sstevel@tonic-gate 				 */
674*7c478bd9Sstevel@tonic-gate 				if (aclp != NULL) {
675*7c478bd9Sstevel@tonic-gate 					free(aclp);
676*7c478bd9Sstevel@tonic-gate 					aclcnt = 0;
677*7c478bd9Sstevel@tonic-gate 					aclp = NULL;
678*7c478bd9Sstevel@tonic-gate 				}
679*7c478bd9Sstevel@tonic-gate 				acl_set = 0;
680*7c478bd9Sstevel@tonic-gate 			}
681*7c478bd9Sstevel@tonic-gate 			(void) memset(&Gen, 0, sizeof (Gen));
682*7c478bd9Sstevel@tonic-gate 		}
683*7c478bd9Sstevel@tonic-gate 		/* Do not count "extra" "read-ahead" buffered data */
684*7c478bd9Sstevel@tonic-gate 		if (Buffr.b_cnt > Bufsize)
685*7c478bd9Sstevel@tonic-gate 			Blocks -=  (u_longlong_t)(Buffr.b_cnt / Bufsize);
686*7c478bd9Sstevel@tonic-gate 		break;
687*7c478bd9Sstevel@tonic-gate 	case OCo: /* COPY OUT */
688*7c478bd9Sstevel@tonic-gate 		if (Args & OCA) {
689*7c478bd9Sstevel@tonic-gate 			scan4trail();
690*7c478bd9Sstevel@tonic-gate 		}
691*7c478bd9Sstevel@tonic-gate 
692*7c478bd9Sstevel@tonic-gate 		Gen.g_dirfd = -1;
693*7c478bd9Sstevel@tonic-gate 		Gen.g_dirpath = NULL;
694*7c478bd9Sstevel@tonic-gate 		sl_preview_synonyms();
695*7c478bd9Sstevel@tonic-gate 
696*7c478bd9Sstevel@tonic-gate 		while ((i = getname()) != 0) {
697*7c478bd9Sstevel@tonic-gate 			if (i == 1) {
698*7c478bd9Sstevel@tonic-gate 				(void) file_out();
699*7c478bd9Sstevel@tonic-gate 				if (Atflag) {
700*7c478bd9Sstevel@tonic-gate 					if (Gen.g_dirfd != -1) {
701*7c478bd9Sstevel@tonic-gate 						(void) close(Gen.g_dirfd);
702*7c478bd9Sstevel@tonic-gate 					}
703*7c478bd9Sstevel@tonic-gate 					Gen.g_dirfd = -1;
704*7c478bd9Sstevel@tonic-gate 					xattrs_out(file_out);
705*7c478bd9Sstevel@tonic-gate 				}
706*7c478bd9Sstevel@tonic-gate 				Hiddendir = 0;
707*7c478bd9Sstevel@tonic-gate 			}
708*7c478bd9Sstevel@tonic-gate 			if (aclp != NULL) {
709*7c478bd9Sstevel@tonic-gate 				free(aclp);
710*7c478bd9Sstevel@tonic-gate 				aclcnt = 0;
711*7c478bd9Sstevel@tonic-gate 				aclp = NULL;
712*7c478bd9Sstevel@tonic-gate 				acl_set = 0;
713*7c478bd9Sstevel@tonic-gate 			}
714*7c478bd9Sstevel@tonic-gate 		}
715*7c478bd9Sstevel@tonic-gate 		write_trail();
716*7c478bd9Sstevel@tonic-gate 		break;
717*7c478bd9Sstevel@tonic-gate 	case OCp: /* PASS */
718*7c478bd9Sstevel@tonic-gate 		sl_preview_synonyms();
719*7c478bd9Sstevel@tonic-gate 
720*7c478bd9Sstevel@tonic-gate 		Gen.g_dirfd = -1;
721*7c478bd9Sstevel@tonic-gate 		Gen.g_passdirfd = -1;
722*7c478bd9Sstevel@tonic-gate 		Gen.g_dirpath = NULL;
723*7c478bd9Sstevel@tonic-gate 		while (getname()) {
724*7c478bd9Sstevel@tonic-gate 			/*
725*7c478bd9Sstevel@tonic-gate 			 * If file is a fully qualified path then
726*7c478bd9Sstevel@tonic-gate 			 * file_pass will strip off the leading '/'
727*7c478bd9Sstevel@tonic-gate 			 * and we need to save off the unstripped
728*7c478bd9Sstevel@tonic-gate 			 * name for attribute traversal.
729*7c478bd9Sstevel@tonic-gate 			 */
730*7c478bd9Sstevel@tonic-gate 			if (Atflag) {
731*7c478bd9Sstevel@tonic-gate 				(void) strcpy(Savenam_p, Gen.g_nam_p);
732*7c478bd9Sstevel@tonic-gate 			}
733*7c478bd9Sstevel@tonic-gate 			passret = file_pass();
734*7c478bd9Sstevel@tonic-gate 			if (aclp != NULL) {
735*7c478bd9Sstevel@tonic-gate 				free(aclp);
736*7c478bd9Sstevel@tonic-gate 				aclcnt = 0;
737*7c478bd9Sstevel@tonic-gate 				aclp = NULL;
738*7c478bd9Sstevel@tonic-gate 				acl_set = 0;
739*7c478bd9Sstevel@tonic-gate 			}
740*7c478bd9Sstevel@tonic-gate 			if (Gen.g_passdirfd != -1)
741*7c478bd9Sstevel@tonic-gate 				(void) close(Gen.g_passdirfd);
742*7c478bd9Sstevel@tonic-gate 			Gen.g_passdirfd = -1;
743*7c478bd9Sstevel@tonic-gate 			if (Atflag) {
744*7c478bd9Sstevel@tonic-gate 				if (Gen.g_dirfd != -1) {
745*7c478bd9Sstevel@tonic-gate 					(void) close(Gen.g_dirfd);
746*7c478bd9Sstevel@tonic-gate 				}
747*7c478bd9Sstevel@tonic-gate 				Gen.g_dirfd = -1;
748*7c478bd9Sstevel@tonic-gate 				if (passret != FILE_LINKED) {
749*7c478bd9Sstevel@tonic-gate 					Gen.g_nam_p = Savenam_p;
750*7c478bd9Sstevel@tonic-gate 					xattrs_out(file_pass);
751*7c478bd9Sstevel@tonic-gate 				}
752*7c478bd9Sstevel@tonic-gate 			}
753*7c478bd9Sstevel@tonic-gate 		}
754*7c478bd9Sstevel@tonic-gate 		break;
755*7c478bd9Sstevel@tonic-gate 	default:
756*7c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible action.");
757*7c478bd9Sstevel@tonic-gate 	}
758*7c478bd9Sstevel@tonic-gate 	if (Ofile > 0) {
759*7c478bd9Sstevel@tonic-gate 		if (close(Ofile) != 0)
760*7c478bd9Sstevel@tonic-gate 			msg(EXTN, "close error");
761*7c478bd9Sstevel@tonic-gate 	}
762*7c478bd9Sstevel@tonic-gate 	if (Archive > 0) {
763*7c478bd9Sstevel@tonic-gate 		if (close(Archive) != 0)
764*7c478bd9Sstevel@tonic-gate 			msg(EXTN, "close error");
765*7c478bd9Sstevel@tonic-gate 	}
766*7c478bd9Sstevel@tonic-gate 	Blocks = (u_longlong_t)(Blocks * Bufsize + SBlocks + 0x1FF) >> 9;
767*7c478bd9Sstevel@tonic-gate 	msg(EPOST, "%lld blocks", Blocks);
768*7c478bd9Sstevel@tonic-gate 	if (Error_cnt)
769*7c478bd9Sstevel@tonic-gate 		msg(EPOST, "%d error(s)", Error_cnt);
770*7c478bd9Sstevel@tonic-gate 	return (EXIT_CODE);
771*7c478bd9Sstevel@tonic-gate }
772*7c478bd9Sstevel@tonic-gate 
773*7c478bd9Sstevel@tonic-gate /*
774*7c478bd9Sstevel@tonic-gate  * add_lnk: Add a linked file's header to the linked file data structure, by
775*7c478bd9Sstevel@tonic-gate  * either adding it to the end of an existing sub-list or starting
776*7c478bd9Sstevel@tonic-gate  * a new sub-list.  Each sub-list saves the links to a given file.
777*7c478bd9Sstevel@tonic-gate  *
778*7c478bd9Sstevel@tonic-gate  * Directly returns a pointer to the new entry; returns a pointer to the head
779*7c478bd9Sstevel@tonic-gate  * of the sub-list in which that entry is located through the argument.
780*7c478bd9Sstevel@tonic-gate  */
781*7c478bd9Sstevel@tonic-gate 
782*7c478bd9Sstevel@tonic-gate static struct Lnk *
783*7c478bd9Sstevel@tonic-gate add_lnk(struct Lnk **sublist_return)
784*7c478bd9Sstevel@tonic-gate {
785*7c478bd9Sstevel@tonic-gate 	struct Lnk *new_entry, *sublist;
786*7c478bd9Sstevel@tonic-gate 
787*7c478bd9Sstevel@tonic-gate 	for (sublist = Lnk_hd.L_nxt_p;
788*7c478bd9Sstevel@tonic-gate 	    sublist != &Lnk_hd;
789*7c478bd9Sstevel@tonic-gate 	    sublist = sublist->L_nxt_p) {
790*7c478bd9Sstevel@tonic-gate 		if (sublist->L_gen.g_ino == G_p->g_ino &&
791*7c478bd9Sstevel@tonic-gate 		    sublist->L_gen.g_dev == G_p->g_dev) {
792*7c478bd9Sstevel@tonic-gate 			/* found */
793*7c478bd9Sstevel@tonic-gate 			break;
794*7c478bd9Sstevel@tonic-gate 		}
795*7c478bd9Sstevel@tonic-gate 	}
796*7c478bd9Sstevel@tonic-gate 
797*7c478bd9Sstevel@tonic-gate 	new_entry = e_zalloc(E_EXIT, sizeof (struct Lnk));
798*7c478bd9Sstevel@tonic-gate 
799*7c478bd9Sstevel@tonic-gate 	new_entry->L_lnk_p = NULL;
800*7c478bd9Sstevel@tonic-gate 	new_entry->L_gen = *G_p; /* structure copy */
801*7c478bd9Sstevel@tonic-gate 
802*7c478bd9Sstevel@tonic-gate 	new_entry->L_gen.g_nam_p = e_zalloc(E_EXIT, (size_t)G_p->g_namesz);
803*7c478bd9Sstevel@tonic-gate 
804*7c478bd9Sstevel@tonic-gate 	(void) strcpy(new_entry->L_gen.g_nam_p, G_p->g_nam_p);
805*7c478bd9Sstevel@tonic-gate 
806*7c478bd9Sstevel@tonic-gate 	if (sublist == &Lnk_hd) {
807*7c478bd9Sstevel@tonic-gate 		/* start new sub-list */
808*7c478bd9Sstevel@tonic-gate 		new_entry->L_nxt_p = &Lnk_hd;
809*7c478bd9Sstevel@tonic-gate 		new_entry->L_bck_p = Lnk_hd.L_bck_p;
810*7c478bd9Sstevel@tonic-gate 		Lnk_hd.L_bck_p = new_entry->L_bck_p->L_nxt_p = new_entry;
811*7c478bd9Sstevel@tonic-gate 		new_entry->L_lnk_p = NULL;
812*7c478bd9Sstevel@tonic-gate 		new_entry->L_cnt = 1;
813*7c478bd9Sstevel@tonic-gate 		new_entry->L_data = Onecopy ? 0 : 1;
814*7c478bd9Sstevel@tonic-gate 		sublist = new_entry;
815*7c478bd9Sstevel@tonic-gate 	} else {
816*7c478bd9Sstevel@tonic-gate 		/* add to existing sub-list */
817*7c478bd9Sstevel@tonic-gate 		struct Lnk *ptr;
818*7c478bd9Sstevel@tonic-gate 
819*7c478bd9Sstevel@tonic-gate 		sublist->L_cnt++;
820*7c478bd9Sstevel@tonic-gate 
821*7c478bd9Sstevel@tonic-gate 		for (ptr = sublist;
822*7c478bd9Sstevel@tonic-gate 		    ptr->L_lnk_p != NULL;
823*7c478bd9Sstevel@tonic-gate 		    ptr = ptr->L_lnk_p) {
824*7c478bd9Sstevel@tonic-gate 			ptr->L_gen.g_filesz = G_p->g_filesz;
825*7c478bd9Sstevel@tonic-gate 		}
826*7c478bd9Sstevel@tonic-gate 
827*7c478bd9Sstevel@tonic-gate 		ptr->L_gen.g_filesz = G_p->g_filesz;
828*7c478bd9Sstevel@tonic-gate 		ptr->L_lnk_p = new_entry;
829*7c478bd9Sstevel@tonic-gate 	}
830*7c478bd9Sstevel@tonic-gate 
831*7c478bd9Sstevel@tonic-gate 	*sublist_return = sublist;
832*7c478bd9Sstevel@tonic-gate 	return (new_entry);
833*7c478bd9Sstevel@tonic-gate }
834*7c478bd9Sstevel@tonic-gate 
835*7c478bd9Sstevel@tonic-gate /*
836*7c478bd9Sstevel@tonic-gate  * bfill: Read req_cnt bytes (out of filelen bytes) from the I/O buffer,
837*7c478bd9Sstevel@tonic-gate  * moving them to rd_buf_p.  When there are no bytes left in the I/O buffer,
838*7c478bd9Sstevel@tonic-gate  * Fillbuf is set and the I/O buffer is filled.  The variable dist is the
839*7c478bd9Sstevel@tonic-gate  * distance to lseek if an I/O error is encountered with the -k option set
840*7c478bd9Sstevel@tonic-gate  * (converted to a multiple of Bufsize).
841*7c478bd9Sstevel@tonic-gate  */
842*7c478bd9Sstevel@tonic-gate 
843*7c478bd9Sstevel@tonic-gate static int
844*7c478bd9Sstevel@tonic-gate bfill(void)
845*7c478bd9Sstevel@tonic-gate {
846*7c478bd9Sstevel@tonic-gate 	int i = 0, rv;
847*7c478bd9Sstevel@tonic-gate 	static int eof = 0;
848*7c478bd9Sstevel@tonic-gate 
849*7c478bd9Sstevel@tonic-gate 	if (!Dflag) {
850*7c478bd9Sstevel@tonic-gate 	while ((Buffr.b_end_p - Buffr.b_in_p) >= Bufsize) {
851*7c478bd9Sstevel@tonic-gate 		errno = 0;
852*7c478bd9Sstevel@tonic-gate 		if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0) {
853*7c478bd9Sstevel@tonic-gate 			if (((Buffr.b_end_p - Buffr.b_in_p) >= Bufsize) &&
854*7c478bd9Sstevel@tonic-gate 			    (Eomflag == 0)) {
855*7c478bd9Sstevel@tonic-gate 				Eomflag = 1;
856*7c478bd9Sstevel@tonic-gate 				return (1);
857*7c478bd9Sstevel@tonic-gate 			}
858*7c478bd9Sstevel@tonic-gate 			if (errno == ENOSPC) {
859*7c478bd9Sstevel@tonic-gate 				(void) chgreel(INPUT);
860*7c478bd9Sstevel@tonic-gate 				if (Hdr_type == BAR) {
861*7c478bd9Sstevel@tonic-gate 					skip_bar_volhdr();
862*7c478bd9Sstevel@tonic-gate 				}
863*7c478bd9Sstevel@tonic-gate 				continue;
864*7c478bd9Sstevel@tonic-gate 			} else if (Args & OCk) {
865*7c478bd9Sstevel@tonic-gate 				if (i++ > MX_SEEKS)
866*7c478bd9Sstevel@tonic-gate 					msg(EXT, "Cannot recover.");
867*7c478bd9Sstevel@tonic-gate 				if (lseek(Archive, Bufsize, SEEK_REL) < 0)
868*7c478bd9Sstevel@tonic-gate 					msg(EXTN, "Cannot lseek()");
869*7c478bd9Sstevel@tonic-gate 				Error_cnt++;
870*7c478bd9Sstevel@tonic-gate 				Buf_error++;
871*7c478bd9Sstevel@tonic-gate 				rv = 0;
872*7c478bd9Sstevel@tonic-gate 				continue;
873*7c478bd9Sstevel@tonic-gate 			} else
874*7c478bd9Sstevel@tonic-gate 				ioerror(INPUT);
875*7c478bd9Sstevel@tonic-gate 		} /* (rv = g_read(Device, Archive ... */
876*7c478bd9Sstevel@tonic-gate 		if (Hdr_type != BAR || rv == Bufsize) {
877*7c478bd9Sstevel@tonic-gate 			Buffr.b_in_p += rv;
878*7c478bd9Sstevel@tonic-gate 			Buffr.b_cnt += (long)rv;
879*7c478bd9Sstevel@tonic-gate 		}
880*7c478bd9Sstevel@tonic-gate 		if (rv == Bufsize) {
881*7c478bd9Sstevel@tonic-gate 			eof = 0;
882*7c478bd9Sstevel@tonic-gate 			Blocks++;
883*7c478bd9Sstevel@tonic-gate 		} else if (rv == 0) {
884*7c478bd9Sstevel@tonic-gate 			if (!eof) {
885*7c478bd9Sstevel@tonic-gate 				eof = 1;
886*7c478bd9Sstevel@tonic-gate 				break;
887*7c478bd9Sstevel@tonic-gate 			}
888*7c478bd9Sstevel@tonic-gate 			(void) chgreel(INPUT);
889*7c478bd9Sstevel@tonic-gate 			eof = 0;	/* reset the eof after chgreel	*/
890*7c478bd9Sstevel@tonic-gate 
891*7c478bd9Sstevel@tonic-gate 			/*
892*7c478bd9Sstevel@tonic-gate 			 * if spans multiple volume, skip the volume header of
893*7c478bd9Sstevel@tonic-gate 			 * the next volume so that the file currently being
894*7c478bd9Sstevel@tonic-gate 			 * extracted can continue to be extracted.
895*7c478bd9Sstevel@tonic-gate 			 */
896*7c478bd9Sstevel@tonic-gate 			if (Hdr_type == BAR) {
897*7c478bd9Sstevel@tonic-gate 				skip_bar_volhdr();
898*7c478bd9Sstevel@tonic-gate 			}
899*7c478bd9Sstevel@tonic-gate 
900*7c478bd9Sstevel@tonic-gate 			continue;
901*7c478bd9Sstevel@tonic-gate 		} else {
902*7c478bd9Sstevel@tonic-gate 			eof = 0;
903*7c478bd9Sstevel@tonic-gate 			SBlocks += (u_longlong_t)rv;
904*7c478bd9Sstevel@tonic-gate 		}
905*7c478bd9Sstevel@tonic-gate 	} /* (Buffr.b_end_p - Buffr.b_in_p) <= Bufsize */
906*7c478bd9Sstevel@tonic-gate 
907*7c478bd9Sstevel@tonic-gate 	} else {			/* Dflag */
908*7c478bd9Sstevel@tonic-gate 		errno = 0;
909*7c478bd9Sstevel@tonic-gate 		if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0) {
910*7c478bd9Sstevel@tonic-gate 			return (-1);
911*7c478bd9Sstevel@tonic-gate 		} /* (rv = g_read(Device, Archive ... */
912*7c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += rv;
913*7c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += (long)rv;
914*7c478bd9Sstevel@tonic-gate 		if (rv == Bufsize) {
915*7c478bd9Sstevel@tonic-gate 			eof = 0;
916*7c478bd9Sstevel@tonic-gate 			Blocks++;
917*7c478bd9Sstevel@tonic-gate 		} else if (!rv) {
918*7c478bd9Sstevel@tonic-gate 			if (!eof) {
919*7c478bd9Sstevel@tonic-gate 				eof = 1;
920*7c478bd9Sstevel@tonic-gate 				return (rv);
921*7c478bd9Sstevel@tonic-gate 			}
922*7c478bd9Sstevel@tonic-gate 			return (-1);
923*7c478bd9Sstevel@tonic-gate 		} else {
924*7c478bd9Sstevel@tonic-gate 			eof = 0;
925*7c478bd9Sstevel@tonic-gate 			SBlocks += (u_longlong_t)rv;
926*7c478bd9Sstevel@tonic-gate 		}
927*7c478bd9Sstevel@tonic-gate 	}
928*7c478bd9Sstevel@tonic-gate 	return (rv);
929*7c478bd9Sstevel@tonic-gate }
930*7c478bd9Sstevel@tonic-gate 
931*7c478bd9Sstevel@tonic-gate /*
932*7c478bd9Sstevel@tonic-gate  * bflush: Move wr_cnt bytes from data_p into the I/O buffer.  When the
933*7c478bd9Sstevel@tonic-gate  * I/O buffer is full, Flushbuf is set and the buffer is written out.
934*7c478bd9Sstevel@tonic-gate  */
935*7c478bd9Sstevel@tonic-gate 
936*7c478bd9Sstevel@tonic-gate static void
937*7c478bd9Sstevel@tonic-gate bflush(void)
938*7c478bd9Sstevel@tonic-gate {
939*7c478bd9Sstevel@tonic-gate 	int rv;
940*7c478bd9Sstevel@tonic-gate 
941*7c478bd9Sstevel@tonic-gate 	while (Buffr.b_cnt >= Bufsize) {
942*7c478bd9Sstevel@tonic-gate 		errno = 0;
943*7c478bd9Sstevel@tonic-gate 		if ((rv = g_write(Device, Archive, Buffr.b_out_p,
944*7c478bd9Sstevel@tonic-gate 		    Bufsize)) < 0) {
945*7c478bd9Sstevel@tonic-gate 			if (errno == ENOSPC && !Dflag)
946*7c478bd9Sstevel@tonic-gate 				rv = chgreel(OUTPUT);
947*7c478bd9Sstevel@tonic-gate 			else
948*7c478bd9Sstevel@tonic-gate 				ioerror(OUTPUT);
949*7c478bd9Sstevel@tonic-gate 		}
950*7c478bd9Sstevel@tonic-gate 		Buffr.b_out_p += rv;
951*7c478bd9Sstevel@tonic-gate 		Buffr.b_cnt -= (long)rv;
952*7c478bd9Sstevel@tonic-gate 		if (rv == Bufsize)
953*7c478bd9Sstevel@tonic-gate 			Blocks++;
954*7c478bd9Sstevel@tonic-gate 		else if (rv > 0)
955*7c478bd9Sstevel@tonic-gate 			SBlocks += (u_longlong_t)rv;
956*7c478bd9Sstevel@tonic-gate 	}
957*7c478bd9Sstevel@tonic-gate 	rstbuf();
958*7c478bd9Sstevel@tonic-gate }
959*7c478bd9Sstevel@tonic-gate 
960*7c478bd9Sstevel@tonic-gate /*
961*7c478bd9Sstevel@tonic-gate  * chgreel: Determine if end-of-medium has been reached.  If it has,
962*7c478bd9Sstevel@tonic-gate  * close the current medium and prompt the user for the next medium.
963*7c478bd9Sstevel@tonic-gate  */
964*7c478bd9Sstevel@tonic-gate 
965*7c478bd9Sstevel@tonic-gate static int
966*7c478bd9Sstevel@tonic-gate chgreel(int dir)
967*7c478bd9Sstevel@tonic-gate {
968*7c478bd9Sstevel@tonic-gate 	int lastchar, tryagain, askagain, rv;
969*7c478bd9Sstevel@tonic-gate 	int tmpdev;
970*7c478bd9Sstevel@tonic-gate 	char str[APATH];
971*7c478bd9Sstevel@tonic-gate 	struct stat statb;
972*7c478bd9Sstevel@tonic-gate 
973*7c478bd9Sstevel@tonic-gate 	rv = 0;
974*7c478bd9Sstevel@tonic-gate 	if (fstat(Archive, &statb) < 0)
975*7c478bd9Sstevel@tonic-gate 		msg(EXTN, "Error during stat() of archive");
976*7c478bd9Sstevel@tonic-gate 	if ((statb.st_mode & S_IFMT) != S_IFCHR) {
977*7c478bd9Sstevel@tonic-gate 		if (dir == INPUT) {
978*7c478bd9Sstevel@tonic-gate 			msg(EXT, "%s%s\n",
979*7c478bd9Sstevel@tonic-gate 				"Can't read input:  end of file encountered ",
980*7c478bd9Sstevel@tonic-gate 				"prior to expected end of archive.");
981*7c478bd9Sstevel@tonic-gate 		}
982*7c478bd9Sstevel@tonic-gate 	}
983*7c478bd9Sstevel@tonic-gate 	msg(EPOST, "\007End of medium on \"%s\".", dir ? "output" : "input");
984*7c478bd9Sstevel@tonic-gate 	if (is_floppy(Archive))
985*7c478bd9Sstevel@tonic-gate 		(void) ioctl(Archive, FDEJECT, NULL);
986*7c478bd9Sstevel@tonic-gate 	if ((close(Archive) != 0) && (dir == OUTPUT))
987*7c478bd9Sstevel@tonic-gate 		msg(EXTN, "close error");
988*7c478bd9Sstevel@tonic-gate 	Archive = 0;
989*7c478bd9Sstevel@tonic-gate 	Volcnt++;
990*7c478bd9Sstevel@tonic-gate 	for (;;) {
991*7c478bd9Sstevel@tonic-gate 		if (Rtty_p == (FILE *)NULL)
992*7c478bd9Sstevel@tonic-gate 			Rtty_p = fopen(Ttyname, "r");
993*7c478bd9Sstevel@tonic-gate 		do { /* tryagain */
994*7c478bd9Sstevel@tonic-gate 			if (IOfil_p) {
995*7c478bd9Sstevel@tonic-gate 				do {
996*7c478bd9Sstevel@tonic-gate 					msg(EPOST, gettext(Eom_p), Volcnt);
997*7c478bd9Sstevel@tonic-gate 					if (!Rtty_p || fgets(str, sizeof (str),
998*7c478bd9Sstevel@tonic-gate 					    Rtty_p) == (char *)NULL)
999*7c478bd9Sstevel@tonic-gate 						msg(EXT, "Cannot read tty.");
1000*7c478bd9Sstevel@tonic-gate 					askagain = 0;
1001*7c478bd9Sstevel@tonic-gate 					switch (*str) {
1002*7c478bd9Sstevel@tonic-gate 					case '\n':
1003*7c478bd9Sstevel@tonic-gate 						(void) strcpy(str, IOfil_p);
1004*7c478bd9Sstevel@tonic-gate 						break;
1005*7c478bd9Sstevel@tonic-gate 					case 'q':
1006*7c478bd9Sstevel@tonic-gate 						exit(EXIT_CODE);
1007*7c478bd9Sstevel@tonic-gate 					default:
1008*7c478bd9Sstevel@tonic-gate 						askagain = 1;
1009*7c478bd9Sstevel@tonic-gate 					}
1010*7c478bd9Sstevel@tonic-gate 				} while (askagain);
1011*7c478bd9Sstevel@tonic-gate 			} else {
1012*7c478bd9Sstevel@tonic-gate 
1013*7c478bd9Sstevel@tonic-gate 				if (Hdr_type == BAR)
1014*7c478bd9Sstevel@tonic-gate 					Bar_vol_num++;
1015*7c478bd9Sstevel@tonic-gate 
1016*7c478bd9Sstevel@tonic-gate 				msg(EPOST,
1017*7c478bd9Sstevel@tonic-gate 				    "To continue, type device/file name when "
1018*7c478bd9Sstevel@tonic-gate 				    "ready.");
1019*7c478bd9Sstevel@tonic-gate 				if (!Rtty_p || fgets(str, sizeof (str),
1020*7c478bd9Sstevel@tonic-gate 				    Rtty_p) == (char *)NULL)
1021*7c478bd9Sstevel@tonic-gate 					msg(EXT, "Cannot read tty.");
1022*7c478bd9Sstevel@tonic-gate 				lastchar = strlen(str) - 1;
1023*7c478bd9Sstevel@tonic-gate 				if (*(str + lastchar) == '\n') /* remove '\n' */
1024*7c478bd9Sstevel@tonic-gate 					*(str + lastchar) = '\0';
1025*7c478bd9Sstevel@tonic-gate 				if (!*str)
1026*7c478bd9Sstevel@tonic-gate 					exit(EXIT_CODE);
1027*7c478bd9Sstevel@tonic-gate 			}
1028*7c478bd9Sstevel@tonic-gate 			tryagain = 0;
1029*7c478bd9Sstevel@tonic-gate 			if ((Archive = open(str, dir)) < 0) {
1030*7c478bd9Sstevel@tonic-gate 				msg(ERRN, "Cannot open \"%s\"", str);
1031*7c478bd9Sstevel@tonic-gate 				tryagain = 1;
1032*7c478bd9Sstevel@tonic-gate 			}
1033*7c478bd9Sstevel@tonic-gate 		} while (tryagain);
1034*7c478bd9Sstevel@tonic-gate 		(void) g_init(&tmpdev, &Archive);
1035*7c478bd9Sstevel@tonic-gate 		if (tmpdev != Device)
1036*7c478bd9Sstevel@tonic-gate 			msg(EXT, "Cannot change media types in mid-stream.");
1037*7c478bd9Sstevel@tonic-gate 		if (dir == INPUT)
1038*7c478bd9Sstevel@tonic-gate 			break;
1039*7c478bd9Sstevel@tonic-gate 		else { /* dir == OUTPUT */
1040*7c478bd9Sstevel@tonic-gate 			errno = 0;
1041*7c478bd9Sstevel@tonic-gate 			if ((rv = g_write(Device, Archive, Buffr.b_out_p,
1042*7c478bd9Sstevel@tonic-gate 			    Bufsize)) == Bufsize)
1043*7c478bd9Sstevel@tonic-gate 				break;
1044*7c478bd9Sstevel@tonic-gate 			else
1045*7c478bd9Sstevel@tonic-gate 				msg(ERR,
1046*7c478bd9Sstevel@tonic-gate 				    "Unable to write this medium, try "
1047*7c478bd9Sstevel@tonic-gate 				    "another.");
1048*7c478bd9Sstevel@tonic-gate 		}
1049*7c478bd9Sstevel@tonic-gate 	} /* ;; */
1050*7c478bd9Sstevel@tonic-gate 	Eomflag = 0;
1051*7c478bd9Sstevel@tonic-gate 	return (rv);
1052*7c478bd9Sstevel@tonic-gate }
1053*7c478bd9Sstevel@tonic-gate 
1054*7c478bd9Sstevel@tonic-gate /*
1055*7c478bd9Sstevel@tonic-gate  * ckname: Check filenames against user specified patterns,
1056*7c478bd9Sstevel@tonic-gate  * and/or ask the user for new name when -r is used.
1057*7c478bd9Sstevel@tonic-gate  */
1058*7c478bd9Sstevel@tonic-gate 
1059*7c478bd9Sstevel@tonic-gate static int
1060*7c478bd9Sstevel@tonic-gate ckname(int flag)
1061*7c478bd9Sstevel@tonic-gate {
1062*7c478bd9Sstevel@tonic-gate 	int lastchar, len;
1063*7c478bd9Sstevel@tonic-gate 
1064*7c478bd9Sstevel@tonic-gate 	if (Hdr_type != TAR && Hdr_type != USTAR && Hdr_type != BAR) {
1065*7c478bd9Sstevel@tonic-gate 		/* Re-visit tar size issues later */
1066*7c478bd9Sstevel@tonic-gate 		if (G_p->g_namesz - 1 > Max_namesz) {
1067*7c478bd9Sstevel@tonic-gate 			msg(ERR, "Name exceeds maximum length - skipped.");
1068*7c478bd9Sstevel@tonic-gate 			return (F_SKIP);
1069*7c478bd9Sstevel@tonic-gate 		}
1070*7c478bd9Sstevel@tonic-gate 	}
1071*7c478bd9Sstevel@tonic-gate 
1072*7c478bd9Sstevel@tonic-gate 	if (Pat_pp && !matched())
1073*7c478bd9Sstevel@tonic-gate 		return (F_SKIP);
1074*7c478bd9Sstevel@tonic-gate 	if ((Args & OCr) && !Adir) { /* rename interactively */
1075*7c478bd9Sstevel@tonic-gate 		(void) fprintf(Wtty_p, gettext("Rename \"%s%s%s\"? "),
1076*7c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
1077*7c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p : Renam_p,
1078*7c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
1079*7c478bd9Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
1080*7c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
1081*7c478bd9Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p);
1082*7c478bd9Sstevel@tonic-gate 		(void) fflush(Wtty_p);
1083*7c478bd9Sstevel@tonic-gate 		if (fgets(Renametmp_p, Max_namesz + 1, Rtty_p) == (char *)NULL)
1084*7c478bd9Sstevel@tonic-gate 			msg(EXT, "Cannot read tty.");
1085*7c478bd9Sstevel@tonic-gate 		if (feof(Rtty_p))
1086*7c478bd9Sstevel@tonic-gate 			exit(EXIT_CODE);
1087*7c478bd9Sstevel@tonic-gate 		lastchar = strlen(Renametmp_p) - 1;
1088*7c478bd9Sstevel@tonic-gate 
1089*7c478bd9Sstevel@tonic-gate 		/* remove trailing '\n' */
1090*7c478bd9Sstevel@tonic-gate 		if (*(Renametmp_p + lastchar) == '\n')
1091*7c478bd9Sstevel@tonic-gate 			*(Renametmp_p + lastchar) = '\0';
1092*7c478bd9Sstevel@tonic-gate 		if (*Renametmp_p == '\0') {
1093*7c478bd9Sstevel@tonic-gate 			msg(POST, "%s%s%s Skipped.",
1094*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
1095*7c478bd9Sstevel@tonic-gate 			    G_p->g_nam_p : G_p->g_attrfnam_p,
1096*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
1097*7c478bd9Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
1098*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
1099*7c478bd9Sstevel@tonic-gate 			    "" : G_p->g_attrnam_p);
1100*7c478bd9Sstevel@tonic-gate 			*G_p->g_nam_p = '\0';
1101*7c478bd9Sstevel@tonic-gate 			return (F_SKIP);
1102*7c478bd9Sstevel@tonic-gate 		} else if (strcmp(Renametmp_p, ".") != 0) {
1103*7c478bd9Sstevel@tonic-gate 			len = strlen(Renametmp_p);
1104*7c478bd9Sstevel@tonic-gate 			if (len > (int)strlen(G_p->g_nam_p)) {
1105*7c478bd9Sstevel@tonic-gate 				if ((G_p->g_nam_p != &nambuf[0]) &&
1106*7c478bd9Sstevel@tonic-gate 				    (G_p->g_nam_p != &fullnam[0]))
1107*7c478bd9Sstevel@tonic-gate 					free(G_p->g_nam_p);
1108*7c478bd9Sstevel@tonic-gate 				G_p->g_nam_p = e_zalloc(E_EXIT, len + 1);
1109*7c478bd9Sstevel@tonic-gate 			}
1110*7c478bd9Sstevel@tonic-gate 			if (G_p->g_attrnam_p != (char *)NULL) {
1111*7c478bd9Sstevel@tonic-gate 				free(G_p->g_attrnam_p);
1112*7c478bd9Sstevel@tonic-gate 				G_p->g_attrnam_p = e_strdup(E_EXIT,
1113*7c478bd9Sstevel@tonic-gate 				    Renametmp_p);
1114*7c478bd9Sstevel@tonic-gate 				(void) strcpy(G_p->g_nam_p, Renam_p);
1115*7c478bd9Sstevel@tonic-gate 			} else {
1116*7c478bd9Sstevel@tonic-gate 				(void) strcpy(Renam_p, Renametmp_p);
1117*7c478bd9Sstevel@tonic-gate 				(void) strcpy(G_p->g_nam_p, Renametmp_p);
1118*7c478bd9Sstevel@tonic-gate 			}
1119*7c478bd9Sstevel@tonic-gate 
1120*7c478bd9Sstevel@tonic-gate 		}
1121*7c478bd9Sstevel@tonic-gate 	}
1122*7c478bd9Sstevel@tonic-gate 	if (flag != 0 || Onecopy == 0) {
1123*7c478bd9Sstevel@tonic-gate 		VERBOSE((Args & OCt), G_p->g_nam_p);
1124*7c478bd9Sstevel@tonic-gate 	}
1125*7c478bd9Sstevel@tonic-gate 	if (Args & OCt)
1126*7c478bd9Sstevel@tonic-gate 		return (F_SKIP);
1127*7c478bd9Sstevel@tonic-gate 	return (F_EXTR);
1128*7c478bd9Sstevel@tonic-gate }
1129*7c478bd9Sstevel@tonic-gate 
1130*7c478bd9Sstevel@tonic-gate /*
1131*7c478bd9Sstevel@tonic-gate  * ckopts: Check the validity of all command line options.
1132*7c478bd9Sstevel@tonic-gate  */
1133*7c478bd9Sstevel@tonic-gate 
1134*7c478bd9Sstevel@tonic-gate static void
1135*7c478bd9Sstevel@tonic-gate ckopts(long mask)
1136*7c478bd9Sstevel@tonic-gate {
1137*7c478bd9Sstevel@tonic-gate 	int oflag;
1138*7c478bd9Sstevel@tonic-gate 	char *t_p;
1139*7c478bd9Sstevel@tonic-gate 	long errmsk;
1140*7c478bd9Sstevel@tonic-gate 
1141*7c478bd9Sstevel@tonic-gate 	if (mask & OCi) {
1142*7c478bd9Sstevel@tonic-gate 		errmsk = mask & INV_MSK4i;
1143*7c478bd9Sstevel@tonic-gate 	} else if (mask & OCo) {
1144*7c478bd9Sstevel@tonic-gate 		errmsk = mask & INV_MSK4o;
1145*7c478bd9Sstevel@tonic-gate 	} else if (mask & OCp) {
1146*7c478bd9Sstevel@tonic-gate 		errmsk = mask & INV_MSK4p;
1147*7c478bd9Sstevel@tonic-gate 	} else {
1148*7c478bd9Sstevel@tonic-gate 		msg(ERR, "One of -i, -o or -p must be specified.");
1149*7c478bd9Sstevel@tonic-gate 		errmsk = 0;
1150*7c478bd9Sstevel@tonic-gate 	}
1151*7c478bd9Sstevel@tonic-gate 
1152*7c478bd9Sstevel@tonic-gate 	if (errmsk) {
1153*7c478bd9Sstevel@tonic-gate 		/* if non-zero, invalid options were specified */
1154*7c478bd9Sstevel@tonic-gate 		Error_cnt++;
1155*7c478bd9Sstevel@tonic-gate 	}
1156*7c478bd9Sstevel@tonic-gate 
1157*7c478bd9Sstevel@tonic-gate 	if ((mask & OCa) && (mask & OCm) && ((mask & OCi) ||
1158*7c478bd9Sstevel@tonic-gate 		(mask & OCo))) {
1159*7c478bd9Sstevel@tonic-gate 		msg(ERR, "-a and -m are mutually exclusive.");
1160*7c478bd9Sstevel@tonic-gate 	}
1161*7c478bd9Sstevel@tonic-gate 
1162*7c478bd9Sstevel@tonic-gate 	if ((mask & OCc) && (mask & OCH) && (strcmp("odc", Hdr_p))) {
1163*7c478bd9Sstevel@tonic-gate 		msg(ERR, "-c and -H are mutually exclusive.");
1164*7c478bd9Sstevel@tonic-gate 	}
1165*7c478bd9Sstevel@tonic-gate 
1166*7c478bd9Sstevel@tonic-gate 	if ((mask & OCv) && (mask & OCV)) {
1167*7c478bd9Sstevel@tonic-gate 		msg(ERR, "-v and -V are mutually exclusive.");
1168*7c478bd9Sstevel@tonic-gate 	}
1169*7c478bd9Sstevel@tonic-gate 
1170*7c478bd9Sstevel@tonic-gate 	if ((mask & OCt) && (mask & OCV)) {
1171*7c478bd9Sstevel@tonic-gate 		msg(ERR, "-t and -V are mutually exclusive.");
1172*7c478bd9Sstevel@tonic-gate 	}
1173*7c478bd9Sstevel@tonic-gate 
1174*7c478bd9Sstevel@tonic-gate 	if ((mask & OCB) && (mask & OCC)) {
1175*7c478bd9Sstevel@tonic-gate 		msg(ERR, "-B and -C are mutually exclusive.");
1176*7c478bd9Sstevel@tonic-gate 	}
1177*7c478bd9Sstevel@tonic-gate 
1178*7c478bd9Sstevel@tonic-gate 	if ((mask & OCH) && (mask & OC6)) {
1179*7c478bd9Sstevel@tonic-gate 		msg(ERR, "-H and -6 are mutually exclusive.");
1180*7c478bd9Sstevel@tonic-gate 	}
1181*7c478bd9Sstevel@tonic-gate 
1182*7c478bd9Sstevel@tonic-gate 	if ((mask & OCM) && !((mask & OCI) || (mask & OCO))) {
1183*7c478bd9Sstevel@tonic-gate 		msg(ERR, "-M not meaningful without -O or -I.");
1184*7c478bd9Sstevel@tonic-gate 	}
1185*7c478bd9Sstevel@tonic-gate 
1186*7c478bd9Sstevel@tonic-gate 	if ((mask & OCA) && !(mask & OCO)) {
1187*7c478bd9Sstevel@tonic-gate 		msg(ERR, "-A requires the -O option.");
1188*7c478bd9Sstevel@tonic-gate 	}
1189*7c478bd9Sstevel@tonic-gate 
1190*7c478bd9Sstevel@tonic-gate 	if (Bufsize <= 0) {
1191*7c478bd9Sstevel@tonic-gate 		msg(ERR, "Illegal size given for -C option.");
1192*7c478bd9Sstevel@tonic-gate 	}
1193*7c478bd9Sstevel@tonic-gate 
1194*7c478bd9Sstevel@tonic-gate 	if (mask & OCH) {
1195*7c478bd9Sstevel@tonic-gate 		t_p = Hdr_p;
1196*7c478bd9Sstevel@tonic-gate 
1197*7c478bd9Sstevel@tonic-gate 		while (*t_p != NULL) {
1198*7c478bd9Sstevel@tonic-gate 			if (isupper(*t_p)) {
1199*7c478bd9Sstevel@tonic-gate 				*t_p = 'a' + (*t_p - 'A');
1200*7c478bd9Sstevel@tonic-gate 			}
1201*7c478bd9Sstevel@tonic-gate 
1202*7c478bd9Sstevel@tonic-gate 			t_p++;
1203*7c478bd9Sstevel@tonic-gate 		}
1204*7c478bd9Sstevel@tonic-gate 
1205*7c478bd9Sstevel@tonic-gate 		if (!(strcmp("odc", Hdr_p))) {
1206*7c478bd9Sstevel@tonic-gate 			Hdr_type = CHR;
1207*7c478bd9Sstevel@tonic-gate 			Max_namesz = CPATH;
1208*7c478bd9Sstevel@tonic-gate 			Onecopy = 0;
1209*7c478bd9Sstevel@tonic-gate 			Use_old_stat = 1;
1210*7c478bd9Sstevel@tonic-gate 		} else if (!(strcmp("crc", Hdr_p))) {
1211*7c478bd9Sstevel@tonic-gate 			Hdr_type = CRC;
1212*7c478bd9Sstevel@tonic-gate 			Max_namesz = APATH;
1213*7c478bd9Sstevel@tonic-gate 			Onecopy = 1;
1214*7c478bd9Sstevel@tonic-gate 		} else if (!(strcmp("tar", Hdr_p))) {
1215*7c478bd9Sstevel@tonic-gate 			if (Args & OCo) {
1216*7c478bd9Sstevel@tonic-gate 				Hdr_type = USTAR;
1217*7c478bd9Sstevel@tonic-gate 				Max_namesz = HNAMLEN - 1;
1218*7c478bd9Sstevel@tonic-gate 			} else {
1219*7c478bd9Sstevel@tonic-gate 				Hdr_type = TAR;
1220*7c478bd9Sstevel@tonic-gate 				Max_namesz = TNAMLEN - 1;
1221*7c478bd9Sstevel@tonic-gate 			}
1222*7c478bd9Sstevel@tonic-gate 			Onecopy = 0;
1223*7c478bd9Sstevel@tonic-gate 		} else if (!(strcmp("ustar", Hdr_p))) {
1224*7c478bd9Sstevel@tonic-gate 			Hdr_type = USTAR;
1225*7c478bd9Sstevel@tonic-gate 			Max_namesz = HNAMLEN - 1;
1226*7c478bd9Sstevel@tonic-gate 			Onecopy = 0;
1227*7c478bd9Sstevel@tonic-gate 		} else if (!(strcmp("bar", Hdr_p))) {
1228*7c478bd9Sstevel@tonic-gate 			if ((Args & OCo) || (Args & OCp)) {
1229*7c478bd9Sstevel@tonic-gate 				msg(ERR,
1230*7c478bd9Sstevel@tonic-gate 				    "Header type bar can only be used with -i");
1231*7c478bd9Sstevel@tonic-gate 			}
1232*7c478bd9Sstevel@tonic-gate 
1233*7c478bd9Sstevel@tonic-gate 			if (Args & OCP) {
1234*7c478bd9Sstevel@tonic-gate 				msg(ERR,
1235*7c478bd9Sstevel@tonic-gate 				    "Can't preserve using bar header");
1236*7c478bd9Sstevel@tonic-gate 			}
1237*7c478bd9Sstevel@tonic-gate 
1238*7c478bd9Sstevel@tonic-gate 			Hdr_type = BAR;
1239*7c478bd9Sstevel@tonic-gate 			Max_namesz = TNAMLEN - 1;
1240*7c478bd9Sstevel@tonic-gate 			Onecopy = 0;
1241*7c478bd9Sstevel@tonic-gate 		} else {
1242*7c478bd9Sstevel@tonic-gate 			msg(ERR, "Invalid header \"%s\" specified", Hdr_p);
1243*7c478bd9Sstevel@tonic-gate 		}
1244*7c478bd9Sstevel@tonic-gate 	}
1245*7c478bd9Sstevel@tonic-gate 
1246*7c478bd9Sstevel@tonic-gate 	if (mask & OCr) {
1247*7c478bd9Sstevel@tonic-gate 		Rtty_p = fopen(Ttyname, "r");
1248*7c478bd9Sstevel@tonic-gate 		Wtty_p = fopen(Ttyname, "w");
1249*7c478bd9Sstevel@tonic-gate 
1250*7c478bd9Sstevel@tonic-gate 		if (Rtty_p == (FILE *)NULL || Wtty_p == (FILE *)NULL) {
1251*7c478bd9Sstevel@tonic-gate 			msg(ERR, "Cannot rename, \"%s\" missing", Ttyname);
1252*7c478bd9Sstevel@tonic-gate 		}
1253*7c478bd9Sstevel@tonic-gate 	}
1254*7c478bd9Sstevel@tonic-gate 
1255*7c478bd9Sstevel@tonic-gate 	if ((mask & OCE) && (Ef_p = fopen(Efil_p, "r")) == (FILE *)NULL) {
1256*7c478bd9Sstevel@tonic-gate 		msg(ERR, "Cannot open \"%s\" to read patterns", Efil_p);
1257*7c478bd9Sstevel@tonic-gate 	}
1258*7c478bd9Sstevel@tonic-gate 
1259*7c478bd9Sstevel@tonic-gate 	if ((mask & OCI) && (Archive = open(IOfil_p, O_RDONLY)) < 0) {
1260*7c478bd9Sstevel@tonic-gate 		msg(ERR, "Cannot open \"%s\" for input", IOfil_p);
1261*7c478bd9Sstevel@tonic-gate 	}
1262*7c478bd9Sstevel@tonic-gate 
1263*7c478bd9Sstevel@tonic-gate 	if (mask & OCO) {
1264*7c478bd9Sstevel@tonic-gate 		if (mask & OCA) {
1265*7c478bd9Sstevel@tonic-gate 			if ((Archive = open(IOfil_p, O_RDWR)) < 0) {
1266*7c478bd9Sstevel@tonic-gate 				msg(ERR,
1267*7c478bd9Sstevel@tonic-gate 				    "Cannot open \"%s\" for append",
1268*7c478bd9Sstevel@tonic-gate 				    IOfil_p);
1269*7c478bd9Sstevel@tonic-gate 			}
1270*7c478bd9Sstevel@tonic-gate 		} else {
1271*7c478bd9Sstevel@tonic-gate 			oflag = (O_WRONLY | O_CREAT | O_TRUNC);
1272*7c478bd9Sstevel@tonic-gate 
1273*7c478bd9Sstevel@tonic-gate 			if ((Archive = open(IOfil_p, oflag, 0777)) < 0) {
1274*7c478bd9Sstevel@tonic-gate 				msg(ERR,
1275*7c478bd9Sstevel@tonic-gate 				    "Cannot open \"%s\" for output",
1276*7c478bd9Sstevel@tonic-gate 				    IOfil_p);
1277*7c478bd9Sstevel@tonic-gate 			}
1278*7c478bd9Sstevel@tonic-gate 		}
1279*7c478bd9Sstevel@tonic-gate 	}
1280*7c478bd9Sstevel@tonic-gate 
1281*7c478bd9Sstevel@tonic-gate 	if (mask & OCR) {
1282*7c478bd9Sstevel@tonic-gate 		if ((Rpw_p = getpwnam(Own_p)) == (struct passwd *)NULL) {
1283*7c478bd9Sstevel@tonic-gate 			msg(ERR, "\"%s\" is not a valid user id", Own_p);
1284*7c478bd9Sstevel@tonic-gate 		}
1285*7c478bd9Sstevel@tonic-gate 	}
1286*7c478bd9Sstevel@tonic-gate 
1287*7c478bd9Sstevel@tonic-gate 	if ((mask & OCo) && !(mask & OCO)) {
1288*7c478bd9Sstevel@tonic-gate 		Out_p = stderr;
1289*7c478bd9Sstevel@tonic-gate 	}
1290*7c478bd9Sstevel@tonic-gate 
1291*7c478bd9Sstevel@tonic-gate 	if ((mask & OCp) && ((mask & (OCB|OCC)) == 0)) {
1292*7c478bd9Sstevel@tonic-gate 		/*
1293*7c478bd9Sstevel@tonic-gate 		 * We are in pass mode with no block size specified.  Use the
1294*7c478bd9Sstevel@tonic-gate 		 * larger of the native page size and 8192.
1295*7c478bd9Sstevel@tonic-gate 		 */
1296*7c478bd9Sstevel@tonic-gate 
1297*7c478bd9Sstevel@tonic-gate 		Bufsize = (PageSize > 8192) ? PageSize : 8192;
1298*7c478bd9Sstevel@tonic-gate 	}
1299*7c478bd9Sstevel@tonic-gate }
1300*7c478bd9Sstevel@tonic-gate 
1301*7c478bd9Sstevel@tonic-gate /*
1302*7c478bd9Sstevel@tonic-gate  * cksum: Calculate the simple checksum of a file (CRC) or header
1303*7c478bd9Sstevel@tonic-gate  * (TARTYP (TAR and USTAR)).  For -o and the CRC header, the file is opened and
1304*7c478bd9Sstevel@tonic-gate  * the checksum is calculated.  For -i and the CRC header, the checksum
1305*7c478bd9Sstevel@tonic-gate  * is calculated as each block is transferred from the archive I/O buffer
1306*7c478bd9Sstevel@tonic-gate  * to the file system I/O buffer.  The TARTYP (TAR and USTAR) headers calculate
1307*7c478bd9Sstevel@tonic-gate  * the simple checksum of the header (with the checksum field of the
1308*7c478bd9Sstevel@tonic-gate  * header initialized to all spaces (\040).
1309*7c478bd9Sstevel@tonic-gate  */
1310*7c478bd9Sstevel@tonic-gate 
1311*7c478bd9Sstevel@tonic-gate static long
1312*7c478bd9Sstevel@tonic-gate cksum(char hdr, int byt_cnt, int *err)
1313*7c478bd9Sstevel@tonic-gate {
1314*7c478bd9Sstevel@tonic-gate 	char *crc_p, *end_p;
1315*7c478bd9Sstevel@tonic-gate 	int cnt;
1316*7c478bd9Sstevel@tonic-gate 	long checksum = 0L, have;
1317*7c478bd9Sstevel@tonic-gate 	off_t lcnt;
1318*7c478bd9Sstevel@tonic-gate 
1319*7c478bd9Sstevel@tonic-gate 	if (err != NULL)
1320*7c478bd9Sstevel@tonic-gate 		*err = 0;
1321*7c478bd9Sstevel@tonic-gate 	switch (hdr) {
1322*7c478bd9Sstevel@tonic-gate 	case CRC:
1323*7c478bd9Sstevel@tonic-gate 		if (Args & OCi) { /* do running checksum */
1324*7c478bd9Sstevel@tonic-gate 			end_p = Buffr.b_out_p + byt_cnt;
1325*7c478bd9Sstevel@tonic-gate 			for (crc_p = Buffr.b_out_p; crc_p < end_p; crc_p++)
1326*7c478bd9Sstevel@tonic-gate 				checksum += (long)*crc_p;
1327*7c478bd9Sstevel@tonic-gate 			break;
1328*7c478bd9Sstevel@tonic-gate 		}
1329*7c478bd9Sstevel@tonic-gate 		/* OCo - do checksum of file */
1330*7c478bd9Sstevel@tonic-gate 		lcnt = G_p->g_filesz;
1331*7c478bd9Sstevel@tonic-gate 
1332*7c478bd9Sstevel@tonic-gate 		while (lcnt > 0) {
1333*7c478bd9Sstevel@tonic-gate 			have = (lcnt < Bufsize) ? lcnt : Bufsize;
1334*7c478bd9Sstevel@tonic-gate 			errno = 0;
1335*7c478bd9Sstevel@tonic-gate 			if (read(Ifile, Buf_p, have) != have) {
1336*7c478bd9Sstevel@tonic-gate 				msg(ERR, "Error computing checksum.");
1337*7c478bd9Sstevel@tonic-gate 				if (err != NULL)
1338*7c478bd9Sstevel@tonic-gate 					*err = 1;
1339*7c478bd9Sstevel@tonic-gate 				break;
1340*7c478bd9Sstevel@tonic-gate 			}
1341*7c478bd9Sstevel@tonic-gate 			end_p = Buf_p + have;
1342*7c478bd9Sstevel@tonic-gate 			for (crc_p = Buf_p; crc_p < end_p; crc_p++)
1343*7c478bd9Sstevel@tonic-gate 				checksum += (long)*crc_p;
1344*7c478bd9Sstevel@tonic-gate 			lcnt -= have;
1345*7c478bd9Sstevel@tonic-gate 		}
1346*7c478bd9Sstevel@tonic-gate 		if (lseek(Ifile, (off_t)0, SEEK_ABS) < 0)
1347*7c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot reset file after checksum");
1348*7c478bd9Sstevel@tonic-gate 		break;
1349*7c478bd9Sstevel@tonic-gate 	case TARTYP: /* TAR and USTAR */
1350*7c478bd9Sstevel@tonic-gate 		crc_p = Thdr_p->tbuf.t_cksum;
1351*7c478bd9Sstevel@tonic-gate 		for (cnt = 0; cnt < TCRCLEN; cnt++) {
1352*7c478bd9Sstevel@tonic-gate 			*crc_p = '\040';
1353*7c478bd9Sstevel@tonic-gate 			crc_p++;
1354*7c478bd9Sstevel@tonic-gate 		}
1355*7c478bd9Sstevel@tonic-gate 		crc_p = (char *)Thdr_p;
1356*7c478bd9Sstevel@tonic-gate 		for (cnt = 0; cnt < TARSZ; cnt++) {
1357*7c478bd9Sstevel@tonic-gate 			/*
1358*7c478bd9Sstevel@tonic-gate 			 * tar uses unsigned checksum, so we must use unsigned
1359*7c478bd9Sstevel@tonic-gate 			 * here in order to be able to read tar archives.
1360*7c478bd9Sstevel@tonic-gate 			 */
1361*7c478bd9Sstevel@tonic-gate 			checksum += (long)((unsigned char)(*crc_p));
1362*7c478bd9Sstevel@tonic-gate 			crc_p++;
1363*7c478bd9Sstevel@tonic-gate 		}
1364*7c478bd9Sstevel@tonic-gate 		break;
1365*7c478bd9Sstevel@tonic-gate 	default:
1366*7c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
1367*7c478bd9Sstevel@tonic-gate 	} /* hdr */
1368*7c478bd9Sstevel@tonic-gate 	return (checksum);
1369*7c478bd9Sstevel@tonic-gate }
1370*7c478bd9Sstevel@tonic-gate 
1371*7c478bd9Sstevel@tonic-gate /*
1372*7c478bd9Sstevel@tonic-gate  * creat_hdr: Fill in the generic header structure with the specific
1373*7c478bd9Sstevel@tonic-gate  *            header information based on the value of Hdr_type.
1374*7c478bd9Sstevel@tonic-gate  *
1375*7c478bd9Sstevel@tonic-gate  *            return (1) if this process was successful, and (0) otherwise.
1376*7c478bd9Sstevel@tonic-gate  */
1377*7c478bd9Sstevel@tonic-gate 
1378*7c478bd9Sstevel@tonic-gate static int
1379*7c478bd9Sstevel@tonic-gate creat_hdr(void)
1380*7c478bd9Sstevel@tonic-gate {
1381*7c478bd9Sstevel@tonic-gate 	ushort_t ftype;
1382*7c478bd9Sstevel@tonic-gate 	int fullnamesize;
1383*7c478bd9Sstevel@tonic-gate 	dev_t dev;
1384*7c478bd9Sstevel@tonic-gate 	ino_t ino;
1385*7c478bd9Sstevel@tonic-gate 
1386*7c478bd9Sstevel@tonic-gate 	ftype = SrcSt.st_mode & Ftype;
1387*7c478bd9Sstevel@tonic-gate 	Adir = (ftype == S_IFDIR);
1388*7c478bd9Sstevel@tonic-gate 	Aspec = (ftype == S_IFBLK || ftype == S_IFCHR || ftype == S_IFIFO);
1389*7c478bd9Sstevel@tonic-gate 	switch (Hdr_type) {
1390*7c478bd9Sstevel@tonic-gate 		case BIN:
1391*7c478bd9Sstevel@tonic-gate 			Gen.g_magic = CMN_BIN;
1392*7c478bd9Sstevel@tonic-gate 			break;
1393*7c478bd9Sstevel@tonic-gate 		case CHR:
1394*7c478bd9Sstevel@tonic-gate 			Gen.g_magic = CMN_BIN;
1395*7c478bd9Sstevel@tonic-gate 			break;
1396*7c478bd9Sstevel@tonic-gate 		case ASC:
1397*7c478bd9Sstevel@tonic-gate 			Gen.g_magic = CMN_ASC;
1398*7c478bd9Sstevel@tonic-gate 			break;
1399*7c478bd9Sstevel@tonic-gate 		case CRC:
1400*7c478bd9Sstevel@tonic-gate 			Gen.g_magic = CMN_CRC;
1401*7c478bd9Sstevel@tonic-gate 			break;
1402*7c478bd9Sstevel@tonic-gate 		case USTAR:
1403*7c478bd9Sstevel@tonic-gate 			/*
1404*7c478bd9Sstevel@tonic-gate 			 * If the length of the full name is greater than 256,
1405*7c478bd9Sstevel@tonic-gate 			 * print out a message and return.
1406*7c478bd9Sstevel@tonic-gate 			 */
1407*7c478bd9Sstevel@tonic-gate 			if ((fullnamesize = strlen(Gen.g_nam_p)) > MAXNAM) {
1408*7c478bd9Sstevel@tonic-gate 				msg(ERR,
1409*7c478bd9Sstevel@tonic-gate 				    "%s: file name too long", Gen.g_nam_p);
1410*7c478bd9Sstevel@tonic-gate 				return (0);
1411*7c478bd9Sstevel@tonic-gate 			} else if (fullnamesize > NAMSIZ) {
1412*7c478bd9Sstevel@tonic-gate 				/*
1413*7c478bd9Sstevel@tonic-gate 				 * The length of the full name is greater than
1414*7c478bd9Sstevel@tonic-gate 				 * 100, so we must split the filename from the
1415*7c478bd9Sstevel@tonic-gate 				 * path
1416*7c478bd9Sstevel@tonic-gate 				 */
1417*7c478bd9Sstevel@tonic-gate 				char namebuff[NAMSIZ+1];
1418*7c478bd9Sstevel@tonic-gate 				char prebuff[PRESIZ+1];
1419*7c478bd9Sstevel@tonic-gate 				char *lastslash;
1420*7c478bd9Sstevel@tonic-gate 				int presize, namesize;
1421*7c478bd9Sstevel@tonic-gate 
1422*7c478bd9Sstevel@tonic-gate 				(void) memset(namebuff, '\0',
1423*7c478bd9Sstevel@tonic-gate 				    sizeof (namebuff));
1424*7c478bd9Sstevel@tonic-gate 				(void) memset(prebuff, '\0', sizeof (prebuff));
1425*7c478bd9Sstevel@tonic-gate 
1426*7c478bd9Sstevel@tonic-gate 				lastslash = strrchr(Gen.g_nam_p, '/');
1427*7c478bd9Sstevel@tonic-gate 
1428*7c478bd9Sstevel@tonic-gate 				if (lastslash != NULL) {
1429*7c478bd9Sstevel@tonic-gate 					namesize = strlen(++lastslash);
1430*7c478bd9Sstevel@tonic-gate 					presize = fullnamesize - namesize - 1;
1431*7c478bd9Sstevel@tonic-gate 				} else {
1432*7c478bd9Sstevel@tonic-gate 					namesize = fullnamesize;
1433*7c478bd9Sstevel@tonic-gate 					lastslash = Gen.g_nam_p;
1434*7c478bd9Sstevel@tonic-gate 					presize = 0;
1435*7c478bd9Sstevel@tonic-gate 				}
1436*7c478bd9Sstevel@tonic-gate 
1437*7c478bd9Sstevel@tonic-gate 				/*
1438*7c478bd9Sstevel@tonic-gate 				 * If the filename is greater than 100 we can't
1439*7c478bd9Sstevel@tonic-gate 				 * archive the file
1440*7c478bd9Sstevel@tonic-gate 				 */
1441*7c478bd9Sstevel@tonic-gate 				if (namesize > NAMSIZ) {
1442*7c478bd9Sstevel@tonic-gate 					msg(ERR,
1443*7c478bd9Sstevel@tonic-gate 					    "%s: filename is greater than %d",
1444*7c478bd9Sstevel@tonic-gate 					    lastslash, NAMSIZ);
1445*7c478bd9Sstevel@tonic-gate 					return (0);
1446*7c478bd9Sstevel@tonic-gate 				}
1447*7c478bd9Sstevel@tonic-gate 				(void) strncpy(&namebuff[0], lastslash,
1448*7c478bd9Sstevel@tonic-gate 				    namesize);
1449*7c478bd9Sstevel@tonic-gate 				/*
1450*7c478bd9Sstevel@tonic-gate 				 * If the prefix is greater than 155 we can't
1451*7c478bd9Sstevel@tonic-gate 				 * archive the file.
1452*7c478bd9Sstevel@tonic-gate 				 */
1453*7c478bd9Sstevel@tonic-gate 				if (presize > PRESIZ) {
1454*7c478bd9Sstevel@tonic-gate 					msg(ERR,
1455*7c478bd9Sstevel@tonic-gate 					    "%s: prefix is greater than %d",
1456*7c478bd9Sstevel@tonic-gate 					    Gen.g_nam_p, PRESIZ);
1457*7c478bd9Sstevel@tonic-gate 					return (0);
1458*7c478bd9Sstevel@tonic-gate 				}
1459*7c478bd9Sstevel@tonic-gate 				(void) strncpy(&prebuff[0], Gen.g_nam_p,
1460*7c478bd9Sstevel@tonic-gate 				    presize);
1461*7c478bd9Sstevel@tonic-gate 
1462*7c478bd9Sstevel@tonic-gate 				Gen.g_tname = e_zalloc(E_EXIT, namesize + 1);
1463*7c478bd9Sstevel@tonic-gate 				(void) strcpy(Gen.g_tname, namebuff);
1464*7c478bd9Sstevel@tonic-gate 
1465*7c478bd9Sstevel@tonic-gate 				Gen.g_prefix = e_zalloc(E_EXIT, presize + 1);
1466*7c478bd9Sstevel@tonic-gate 				(void) strcpy(Gen.g_prefix, prebuff);
1467*7c478bd9Sstevel@tonic-gate 			} else {
1468*7c478bd9Sstevel@tonic-gate 				Gen.g_tname = Gen.g_nam_p;
1469*7c478bd9Sstevel@tonic-gate 			}
1470*7c478bd9Sstevel@tonic-gate 			(void) strcpy(Gen.g_tmagic, "ustar");
1471*7c478bd9Sstevel@tonic-gate 			(void) strcpy(Gen.g_version, "00");
1472*7c478bd9Sstevel@tonic-gate 
1473*7c478bd9Sstevel@tonic-gate 			dpasswd = getpwuid(SrcSt.st_uid);
1474*7c478bd9Sstevel@tonic-gate 			if (dpasswd == (struct passwd *)NULL) {
1475*7c478bd9Sstevel@tonic-gate 				msg(EPOST,
1476*7c478bd9Sstevel@tonic-gate 				    "cpio: could not get passwd information "
1477*7c478bd9Sstevel@tonic-gate 				    "for %s%s%s",
1478*7c478bd9Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
1479*7c478bd9Sstevel@tonic-gate 				    Gen.g_nam_p : Gen.g_attrfnam_p,
1480*7c478bd9Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
1481*7c478bd9Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
1482*7c478bd9Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
1483*7c478bd9Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
1484*7c478bd9Sstevel@tonic-gate 				/* make name null string */
1485*7c478bd9Sstevel@tonic-gate 				Gen.g_uname[0] = '\0';
1486*7c478bd9Sstevel@tonic-gate 			} else {
1487*7c478bd9Sstevel@tonic-gate 				(void) strncpy(&Gen.g_uname[0],
1488*7c478bd9Sstevel@tonic-gate 				    dpasswd->pw_name, 32);
1489*7c478bd9Sstevel@tonic-gate 			}
1490*7c478bd9Sstevel@tonic-gate 			dgroup = getgrgid(SrcSt.st_gid);
1491*7c478bd9Sstevel@tonic-gate 			if (dgroup == (struct group *)NULL) {
1492*7c478bd9Sstevel@tonic-gate 				msg(EPOST,
1493*7c478bd9Sstevel@tonic-gate 				    "cpio: could not get group information "
1494*7c478bd9Sstevel@tonic-gate 				    "for %s%s%S",
1495*7c478bd9Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
1496*7c478bd9Sstevel@tonic-gate 				    Gen.g_nam_p : Gen.g_attrfnam_p,
1497*7c478bd9Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
1498*7c478bd9Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
1499*7c478bd9Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
1500*7c478bd9Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
1501*7c478bd9Sstevel@tonic-gate 				/* make name null string */
1502*7c478bd9Sstevel@tonic-gate 				Gen.g_gname[0] = '\0';
1503*7c478bd9Sstevel@tonic-gate 			} else {
1504*7c478bd9Sstevel@tonic-gate 				(void) strncpy(&Gen.g_gname[0],
1505*7c478bd9Sstevel@tonic-gate 				    dgroup->gr_name, 32);
1506*7c478bd9Sstevel@tonic-gate 			}
1507*7c478bd9Sstevel@tonic-gate 			Gen.g_typeflag = tartype(ftype);
1508*7c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
1509*7c478bd9Sstevel@tonic-gate 		case TAR:
1510*7c478bd9Sstevel@tonic-gate 			(void) memset(T_lname, '\0', sizeof (T_lname));
1511*7c478bd9Sstevel@tonic-gate 			break;
1512*7c478bd9Sstevel@tonic-gate 		default:
1513*7c478bd9Sstevel@tonic-gate 			msg(EXT, "Impossible header type.");
1514*7c478bd9Sstevel@tonic-gate 	}
1515*7c478bd9Sstevel@tonic-gate 
1516*7c478bd9Sstevel@tonic-gate 	dev = SrcSt.st_dev;
1517*7c478bd9Sstevel@tonic-gate 	ino = SrcSt.st_ino;
1518*7c478bd9Sstevel@tonic-gate 
1519*7c478bd9Sstevel@tonic-gate 	if (Use_old_stat) {
1520*7c478bd9Sstevel@tonic-gate 		SrcSt = *OldSt;
1521*7c478bd9Sstevel@tonic-gate 	}
1522*7c478bd9Sstevel@tonic-gate 
1523*7c478bd9Sstevel@tonic-gate 	Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
1524*7c478bd9Sstevel@tonic-gate 	Gen.g_uid = SrcSt.st_uid;
1525*7c478bd9Sstevel@tonic-gate 	Gen.g_gid = SrcSt.st_gid;
1526*7c478bd9Sstevel@tonic-gate 	Gen.g_dev = SrcSt.st_dev;
1527*7c478bd9Sstevel@tonic-gate 
1528*7c478bd9Sstevel@tonic-gate 	if (Use_old_stat) {
1529*7c478bd9Sstevel@tonic-gate 		/* -Hodc */
1530*7c478bd9Sstevel@tonic-gate 
1531*7c478bd9Sstevel@tonic-gate 		sl_info_t *p = sl_search(dev, ino);
1532*7c478bd9Sstevel@tonic-gate 		Gen.g_ino = p ? p->sl_ino2 : -1;
1533*7c478bd9Sstevel@tonic-gate 
1534*7c478bd9Sstevel@tonic-gate 		if (Gen.g_ino == (ulong_t)-1) {
1535*7c478bd9Sstevel@tonic-gate 			msg(ERR, "%s%s%s: cannot be archived - inode too big "
1536*7c478bd9Sstevel@tonic-gate 			    "for -Hodc format",
1537*7c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
1538*7c478bd9Sstevel@tonic-gate 			    Gen.g_nam_p : Gen.g_attrfnam_p,
1539*7c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
1540*7c478bd9Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
1541*7c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
1542*7c478bd9Sstevel@tonic-gate 			    "" : Gen.g_attrnam_p);
1543*7c478bd9Sstevel@tonic-gate 			return (0);
1544*7c478bd9Sstevel@tonic-gate 		}
1545*7c478bd9Sstevel@tonic-gate 	} else {
1546*7c478bd9Sstevel@tonic-gate 		Gen.g_ino = SrcSt.st_ino;
1547*7c478bd9Sstevel@tonic-gate 	}
1548*7c478bd9Sstevel@tonic-gate 
1549*7c478bd9Sstevel@tonic-gate 	Gen.g_mode = SrcSt.st_mode;
1550*7c478bd9Sstevel@tonic-gate 	Gen.g_mtime = SrcSt.st_mtime;
1551*7c478bd9Sstevel@tonic-gate 	Gen.g_nlink = (Adir) ? SrcSt.st_nlink
1552*7c478bd9Sstevel@tonic-gate 	    : sl_numlinks(dev, ino);
1553*7c478bd9Sstevel@tonic-gate 
1554*7c478bd9Sstevel@tonic-gate 	if (ftype == S_IFREG || ftype == S_IFLNK)
1555*7c478bd9Sstevel@tonic-gate 		Gen.g_filesz = (off_t)SrcSt.st_size;
1556*7c478bd9Sstevel@tonic-gate 	else
1557*7c478bd9Sstevel@tonic-gate 		Gen.g_filesz = (off_t)0;
1558*7c478bd9Sstevel@tonic-gate 	Gen.g_rdev = SrcSt.st_rdev;
1559*7c478bd9Sstevel@tonic-gate 	return (1);
1560*7c478bd9Sstevel@tonic-gate }
1561*7c478bd9Sstevel@tonic-gate 
1562*7c478bd9Sstevel@tonic-gate /*
1563*7c478bd9Sstevel@tonic-gate  * creat_lnk: Create a link from the existing name1_p to name2_p.
1564*7c478bd9Sstevel@tonic-gate  */
1565*7c478bd9Sstevel@tonic-gate 
1566*7c478bd9Sstevel@tonic-gate static
1567*7c478bd9Sstevel@tonic-gate int
1568*7c478bd9Sstevel@tonic-gate creat_lnk(int dirfd, char *name1_p, char *name2_p)
1569*7c478bd9Sstevel@tonic-gate {
1570*7c478bd9Sstevel@tonic-gate 	int cnt = 0;
1571*7c478bd9Sstevel@tonic-gate 
1572*7c478bd9Sstevel@tonic-gate 	do {
1573*7c478bd9Sstevel@tonic-gate 		errno = 0;
1574*7c478bd9Sstevel@tonic-gate 		if (!link(name1_p, name2_p)) {
1575*7c478bd9Sstevel@tonic-gate 			if (aclp != NULL) {
1576*7c478bd9Sstevel@tonic-gate 				free(aclp);
1577*7c478bd9Sstevel@tonic-gate 				aclp = NULL;
1578*7c478bd9Sstevel@tonic-gate 				acl_set = 0;
1579*7c478bd9Sstevel@tonic-gate 			}
1580*7c478bd9Sstevel@tonic-gate 			cnt = 0;
1581*7c478bd9Sstevel@tonic-gate 			break;
1582*7c478bd9Sstevel@tonic-gate 		} else if (errno == EEXIST) {
1583*7c478bd9Sstevel@tonic-gate 			if (cnt == 0) {
1584*7c478bd9Sstevel@tonic-gate 				struct stat lsb1;
1585*7c478bd9Sstevel@tonic-gate 				struct stat lsb2;
1586*7c478bd9Sstevel@tonic-gate 
1587*7c478bd9Sstevel@tonic-gate 				/*
1588*7c478bd9Sstevel@tonic-gate 				 * Check to see if we are trying to link this
1589*7c478bd9Sstevel@tonic-gate 				 * file to itself.  If so, count the effort as
1590*7c478bd9Sstevel@tonic-gate 				 * successful.  If the two files are different,
1591*7c478bd9Sstevel@tonic-gate 				 * or if either lstat is unsuccessful, proceed
1592*7c478bd9Sstevel@tonic-gate 				 * as we would have otherwise; the appropriate
1593*7c478bd9Sstevel@tonic-gate 				 * error will be reported subsequently.
1594*7c478bd9Sstevel@tonic-gate 				 */
1595*7c478bd9Sstevel@tonic-gate 
1596*7c478bd9Sstevel@tonic-gate 				if (lstat(name1_p, &lsb1) != 0) {
1597*7c478bd9Sstevel@tonic-gate 					msg(ERR, "Cannot lstat source file %s",
1598*7c478bd9Sstevel@tonic-gate 					    name1_p);
1599*7c478bd9Sstevel@tonic-gate 				} else {
1600*7c478bd9Sstevel@tonic-gate 					if (lstat(name2_p, &lsb2) != 0) {
1601*7c478bd9Sstevel@tonic-gate 						msg(ERR, "Cannot lstat "
1602*7c478bd9Sstevel@tonic-gate 						    "destination file %s",
1603*7c478bd9Sstevel@tonic-gate 						    name2_p);
1604*7c478bd9Sstevel@tonic-gate 					} else {
1605*7c478bd9Sstevel@tonic-gate 						if (lsb1.st_dev ==
1606*7c478bd9Sstevel@tonic-gate 						    lsb2.st_dev &&
1607*7c478bd9Sstevel@tonic-gate 						    lsb1.st_ino ==
1608*7c478bd9Sstevel@tonic-gate 						    lsb2.st_ino) {
1609*7c478bd9Sstevel@tonic-gate 							VERBOSE((Args &
1610*7c478bd9Sstevel@tonic-gate 							    (OCv | OCV)),
1611*7c478bd9Sstevel@tonic-gate 							    name2_p);
1612*7c478bd9Sstevel@tonic-gate 							return (0);
1613*7c478bd9Sstevel@tonic-gate 						}
1614*7c478bd9Sstevel@tonic-gate 					}
1615*7c478bd9Sstevel@tonic-gate 				}
1616*7c478bd9Sstevel@tonic-gate 			}
1617*7c478bd9Sstevel@tonic-gate 
1618*7c478bd9Sstevel@tonic-gate 			if (!(Args & OCu) && G_p->g_mtime <= DesSt.st_mtime)
1619*7c478bd9Sstevel@tonic-gate 				msg(ERR,
1620*7c478bd9Sstevel@tonic-gate 				    "Existing \"%s\" same age or newer",
1621*7c478bd9Sstevel@tonic-gate 				    name2_p);
1622*7c478bd9Sstevel@tonic-gate 			else if (unlinkat(dirfd, get_component(name2_p), 0) < 0)
1623*7c478bd9Sstevel@tonic-gate 				msg(ERRN, "Error cannot unlink \"%s\"",
1624*7c478bd9Sstevel@tonic-gate 				    name2_p);
1625*7c478bd9Sstevel@tonic-gate 		}
1626*7c478bd9Sstevel@tonic-gate 		cnt++;
1627*7c478bd9Sstevel@tonic-gate 	} while ((cnt < 2) && missdir(name2_p) == 0);
1628*7c478bd9Sstevel@tonic-gate 	if (!cnt) {
1629*7c478bd9Sstevel@tonic-gate 		char *newname;
1630*7c478bd9Sstevel@tonic-gate 		char *fromname;
1631*7c478bd9Sstevel@tonic-gate 		char *attrname;
1632*7c478bd9Sstevel@tonic-gate 
1633*7c478bd9Sstevel@tonic-gate 		newname = name2_p;
1634*7c478bd9Sstevel@tonic-gate 		fromname = name1_p;
1635*7c478bd9Sstevel@tonic-gate 		attrname = Gen.g_attrnam_p;
1636*7c478bd9Sstevel@tonic-gate 		if (attrname) {
1637*7c478bd9Sstevel@tonic-gate 			if (Args & OCp) {
1638*7c478bd9Sstevel@tonic-gate 				newname = fromname = Fullnam_p;
1639*7c478bd9Sstevel@tonic-gate 			} else {
1640*7c478bd9Sstevel@tonic-gate 				newname = Gen.g_attrfnam_p;
1641*7c478bd9Sstevel@tonic-gate 			}
1642*7c478bd9Sstevel@tonic-gate 		}
1643*7c478bd9Sstevel@tonic-gate 		if (Args & OCv) {
1644*7c478bd9Sstevel@tonic-gate 			(void) fprintf(Err_p,
1645*7c478bd9Sstevel@tonic-gate 			    gettext("%s%s%s linked to %s%s%s\n"), newname,
1646*7c478bd9Sstevel@tonic-gate 			    (attrname == (char *)NULL) ?
1647*7c478bd9Sstevel@tonic-gate 			    "" : gettext(" attribute "),
1648*7c478bd9Sstevel@tonic-gate 			    (attrname == (char *)NULL) ?
1649*7c478bd9Sstevel@tonic-gate 			    "" : attrname, fromname,
1650*7c478bd9Sstevel@tonic-gate 			    (attrname == (char *)NULL) ?
1651*7c478bd9Sstevel@tonic-gate 			    "" : gettext(" attribute "),
1652*7c478bd9Sstevel@tonic-gate 			    (attrname == (char *)NULL) ?
1653*7c478bd9Sstevel@tonic-gate 			    "" : name1_p);
1654*7c478bd9Sstevel@tonic-gate 		}
1655*7c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), newname);
1656*7c478bd9Sstevel@tonic-gate 	} else if (cnt == 1)
1657*7c478bd9Sstevel@tonic-gate 		msg(ERRN,
1658*7c478bd9Sstevel@tonic-gate 		    "Unable to create directory for \"%s\"", name2_p);
1659*7c478bd9Sstevel@tonic-gate 	else if (cnt == 2)
1660*7c478bd9Sstevel@tonic-gate 		msg(ERRN,
1661*7c478bd9Sstevel@tonic-gate 		    "Cannot link \"%s\" and \"%s\"", name1_p, name2_p);
1662*7c478bd9Sstevel@tonic-gate 	return (cnt);
1663*7c478bd9Sstevel@tonic-gate }
1664*7c478bd9Sstevel@tonic-gate 
1665*7c478bd9Sstevel@tonic-gate /*
1666*7c478bd9Sstevel@tonic-gate  * creat_spec:
1667*7c478bd9Sstevel@tonic-gate  *   Create one of the following:
1668*7c478bd9Sstevel@tonic-gate  *       directory
1669*7c478bd9Sstevel@tonic-gate  *       character special file
1670*7c478bd9Sstevel@tonic-gate  *       block special file
1671*7c478bd9Sstevel@tonic-gate  *       fifo
1672*7c478bd9Sstevel@tonic-gate  */
1673*7c478bd9Sstevel@tonic-gate 
1674*7c478bd9Sstevel@tonic-gate static int
1675*7c478bd9Sstevel@tonic-gate creat_spec(int dirfd)
1676*7c478bd9Sstevel@tonic-gate {
1677*7c478bd9Sstevel@tonic-gate 	char *nam_p;
1678*7c478bd9Sstevel@tonic-gate 	int cnt, result, rv = 0;
1679*7c478bd9Sstevel@tonic-gate 	char *curdir;
1680*7c478bd9Sstevel@tonic-gate 	char *lastslash;
1681*7c478bd9Sstevel@tonic-gate 
1682*7c478bd9Sstevel@tonic-gate 	Do_rename = 0;	/* creat_tmp() may reset this */
1683*7c478bd9Sstevel@tonic-gate 
1684*7c478bd9Sstevel@tonic-gate 	if (Args & OCp) {
1685*7c478bd9Sstevel@tonic-gate 		nam_p = Fullnam_p;
1686*7c478bd9Sstevel@tonic-gate 	} else {
1687*7c478bd9Sstevel@tonic-gate 		nam_p = G_p->g_nam_p;
1688*7c478bd9Sstevel@tonic-gate 	}
1689*7c478bd9Sstevel@tonic-gate 
1690*7c478bd9Sstevel@tonic-gate 	/*
1691*7c478bd9Sstevel@tonic-gate 	 * Is this the extraction of the hidden attribute directory?
1692*7c478bd9Sstevel@tonic-gate 	 * If so then just set the mode/times correctly, and return.
1693*7c478bd9Sstevel@tonic-gate 	 */
1694*7c478bd9Sstevel@tonic-gate 
1695*7c478bd9Sstevel@tonic-gate 	if (Hiddendir) {
1696*7c478bd9Sstevel@tonic-gate 		if (fchownat(dirfd, ".", G_p->g_uid, G_p->g_gid, 0) != 0) {
1697*7c478bd9Sstevel@tonic-gate 			msg(ERRN,
1698*7c478bd9Sstevel@tonic-gate 			    "Cannot chown() \"attribute directory of "
1699*7c478bd9Sstevel@tonic-gate 			    "file %s\"", G_p->g_attrfnam_p);
1700*7c478bd9Sstevel@tonic-gate 		}
1701*7c478bd9Sstevel@tonic-gate 
1702*7c478bd9Sstevel@tonic-gate 		if (fchmod(dirfd, G_p->g_mode) != 0) {
1703*7c478bd9Sstevel@tonic-gate 			msg(ERRN,
1704*7c478bd9Sstevel@tonic-gate 			    "Cannot chmod() \"attribute directory of "
1705*7c478bd9Sstevel@tonic-gate 			    "file %s\"", G_p->g_attrfnam_p);
1706*7c478bd9Sstevel@tonic-gate 		}
1707*7c478bd9Sstevel@tonic-gate 
1708*7c478bd9Sstevel@tonic-gate 		acl_set = 0;
1709*7c478bd9Sstevel@tonic-gate 		if (Pflag && aclp != NULL) {
1710*7c478bd9Sstevel@tonic-gate 			if (facl(dirfd, SETACL, aclcnt, aclp) < 0) {
1711*7c478bd9Sstevel@tonic-gate 				msg(ERRN,
1712*7c478bd9Sstevel@tonic-gate 				    "failed to set acl on attribute"
1713*7c478bd9Sstevel@tonic-gate 				    " directory of %s ", G_p->g_attrfnam_p);
1714*7c478bd9Sstevel@tonic-gate 			} else {
1715*7c478bd9Sstevel@tonic-gate 				acl_set = 1;
1716*7c478bd9Sstevel@tonic-gate 			}
1717*7c478bd9Sstevel@tonic-gate 			free(aclp);
1718*7c478bd9Sstevel@tonic-gate 			aclp = NULL;
1719*7c478bd9Sstevel@tonic-gate 		}
1720*7c478bd9Sstevel@tonic-gate 
1721*7c478bd9Sstevel@tonic-gate 		return (1);
1722*7c478bd9Sstevel@tonic-gate 	}
1723*7c478bd9Sstevel@tonic-gate 
1724*7c478bd9Sstevel@tonic-gate 	result = stat(nam_p, &DesSt);
1725*7c478bd9Sstevel@tonic-gate 
1726*7c478bd9Sstevel@tonic-gate 	if (ustar_dir() || Adir) {
1727*7c478bd9Sstevel@tonic-gate 		/*
1728*7c478bd9Sstevel@tonic-gate 		 *  The archive file is a directory.
1729*7c478bd9Sstevel@tonic-gate 		 *  Skip "." and ".."
1730*7c478bd9Sstevel@tonic-gate 		 */
1731*7c478bd9Sstevel@tonic-gate 
1732*7c478bd9Sstevel@tonic-gate 		curdir = strrchr(nam_p, '.');
1733*7c478bd9Sstevel@tonic-gate 
1734*7c478bd9Sstevel@tonic-gate 		if (curdir != NULL && curdir[1] == NULL) {
1735*7c478bd9Sstevel@tonic-gate 			lastslash = strrchr(nam_p, '/');
1736*7c478bd9Sstevel@tonic-gate 
1737*7c478bd9Sstevel@tonic-gate 			if (lastslash != NULL) {
1738*7c478bd9Sstevel@tonic-gate 				lastslash++;
1739*7c478bd9Sstevel@tonic-gate 			} else {
1740*7c478bd9Sstevel@tonic-gate 				lastslash = nam_p;
1741*7c478bd9Sstevel@tonic-gate 			}
1742*7c478bd9Sstevel@tonic-gate 
1743*7c478bd9Sstevel@tonic-gate 			if (!(strcmp(lastslash, ".")) ||
1744*7c478bd9Sstevel@tonic-gate 			    !(strcmp(lastslash, ".."))) {
1745*7c478bd9Sstevel@tonic-gate 				return (1);
1746*7c478bd9Sstevel@tonic-gate 			}
1747*7c478bd9Sstevel@tonic-gate 		}
1748*7c478bd9Sstevel@tonic-gate 
1749*7c478bd9Sstevel@tonic-gate 		if (result == 0) {
1750*7c478bd9Sstevel@tonic-gate 			/* A file by the same name exists. */
1751*7c478bd9Sstevel@tonic-gate 
1752*7c478bd9Sstevel@tonic-gate 			/* Take care of ACLs */
1753*7c478bd9Sstevel@tonic-gate 			acl_set = 0;
1754*7c478bd9Sstevel@tonic-gate 
1755*7c478bd9Sstevel@tonic-gate 			if (Pflag && aclp != NULL) {
1756*7c478bd9Sstevel@tonic-gate 				if (acl(nam_p, SETACL, aclcnt, aclp) < 0) {
1757*7c478bd9Sstevel@tonic-gate 					msg(ERRN,
1758*7c478bd9Sstevel@tonic-gate 					    "\"%s\": failed to set acl",
1759*7c478bd9Sstevel@tonic-gate 					    nam_p);
1760*7c478bd9Sstevel@tonic-gate 				} else {
1761*7c478bd9Sstevel@tonic-gate 					acl_set = 1;
1762*7c478bd9Sstevel@tonic-gate 				}
1763*7c478bd9Sstevel@tonic-gate 
1764*7c478bd9Sstevel@tonic-gate 				free(aclp);
1765*7c478bd9Sstevel@tonic-gate 				aclp = NULL;
1766*7c478bd9Sstevel@tonic-gate 			}
1767*7c478bd9Sstevel@tonic-gate 			if (Args & OCd) {
1768*7c478bd9Sstevel@tonic-gate 				/*
1769*7c478bd9Sstevel@tonic-gate 				 * We are creating directories.  Keep the
1770*7c478bd9Sstevel@tonic-gate 				 * existing file.
1771*7c478bd9Sstevel@tonic-gate 				 */
1772*7c478bd9Sstevel@tonic-gate 
1773*7c478bd9Sstevel@tonic-gate 				rstfiles(U_KEEP, dirfd);
1774*7c478bd9Sstevel@tonic-gate 			}
1775*7c478bd9Sstevel@tonic-gate 
1776*7c478bd9Sstevel@tonic-gate 			/* Report success. */
1777*7c478bd9Sstevel@tonic-gate 
1778*7c478bd9Sstevel@tonic-gate 			return (1);
1779*7c478bd9Sstevel@tonic-gate 		}
1780*7c478bd9Sstevel@tonic-gate 	} else {
1781*7c478bd9Sstevel@tonic-gate 		/* The archive file is not a directory. */
1782*7c478bd9Sstevel@tonic-gate 
1783*7c478bd9Sstevel@tonic-gate 		if (result == 0) {
1784*7c478bd9Sstevel@tonic-gate 			/*
1785*7c478bd9Sstevel@tonic-gate 			 * A file by the same name exists.  Move it to a
1786*7c478bd9Sstevel@tonic-gate 			 * temporary file.
1787*7c478bd9Sstevel@tonic-gate 			 */
1788*7c478bd9Sstevel@tonic-gate 
1789*7c478bd9Sstevel@tonic-gate 			if (creat_tmp(nam_p) < 0) {
1790*7c478bd9Sstevel@tonic-gate 				/*
1791*7c478bd9Sstevel@tonic-gate 				 * We weren't able to create the temp file.
1792*7c478bd9Sstevel@tonic-gate 				 * Report failure.
1793*7c478bd9Sstevel@tonic-gate 				 */
1794*7c478bd9Sstevel@tonic-gate 
1795*7c478bd9Sstevel@tonic-gate 				return (0);
1796*7c478bd9Sstevel@tonic-gate 			}
1797*7c478bd9Sstevel@tonic-gate 		}
1798*7c478bd9Sstevel@tonic-gate 	}
1799*7c478bd9Sstevel@tonic-gate 
1800*7c478bd9Sstevel@tonic-gate 	/*
1801*7c478bd9Sstevel@tonic-gate 	 * This pile tries to create the file directly, and, if there is a
1802*7c478bd9Sstevel@tonic-gate 	 * problem, creates missing directories, and then tries to create the
1803*7c478bd9Sstevel@tonic-gate 	 * file again.  Two strikes and you're out.
1804*7c478bd9Sstevel@tonic-gate 	 */
1805*7c478bd9Sstevel@tonic-gate 
1806*7c478bd9Sstevel@tonic-gate 	cnt = 0;
1807*7c478bd9Sstevel@tonic-gate 
1808*7c478bd9Sstevel@tonic-gate 	do {
1809*7c478bd9Sstevel@tonic-gate 		if (ustar_dir() || Adir) {
1810*7c478bd9Sstevel@tonic-gate 			/* The archive file is a directory. */
1811*7c478bd9Sstevel@tonic-gate 
1812*7c478bd9Sstevel@tonic-gate 			result = mkdir(nam_p, G_p->g_mode);
1813*7c478bd9Sstevel@tonic-gate 		} else if (ustar_spec() || Aspec) {
1814*7c478bd9Sstevel@tonic-gate 			/*
1815*7c478bd9Sstevel@tonic-gate 			 * The archive file is block special,
1816*7c478bd9Sstevel@tonic-gate 			 * char special or a fifo.
1817*7c478bd9Sstevel@tonic-gate 			 */
1818*7c478bd9Sstevel@tonic-gate 
1819*7c478bd9Sstevel@tonic-gate 			result = mknod(nam_p, (int)G_p->g_mode,
1820*7c478bd9Sstevel@tonic-gate 			    (int)G_p->g_rdev);
1821*7c478bd9Sstevel@tonic-gate 		}
1822*7c478bd9Sstevel@tonic-gate 
1823*7c478bd9Sstevel@tonic-gate 		if (result >= 0) {
1824*7c478bd9Sstevel@tonic-gate 			/*
1825*7c478bd9Sstevel@tonic-gate 			 * The file creation succeeded.  Take care of the ACLs.
1826*7c478bd9Sstevel@tonic-gate 			 */
1827*7c478bd9Sstevel@tonic-gate 
1828*7c478bd9Sstevel@tonic-gate 			acl_set = 0;
1829*7c478bd9Sstevel@tonic-gate 
1830*7c478bd9Sstevel@tonic-gate 			if (Pflag && aclp != NULL) {
1831*7c478bd9Sstevel@tonic-gate 				if (acl(nam_p, SETACL, aclcnt, aclp) < 0) {
1832*7c478bd9Sstevel@tonic-gate 					msg(ERRN,
1833*7c478bd9Sstevel@tonic-gate 					    "\"%s\": failed to set acl", nam_p);
1834*7c478bd9Sstevel@tonic-gate 				} else {
1835*7c478bd9Sstevel@tonic-gate 					acl_set = 1;
1836*7c478bd9Sstevel@tonic-gate 				}
1837*7c478bd9Sstevel@tonic-gate 
1838*7c478bd9Sstevel@tonic-gate 				free(aclp);
1839*7c478bd9Sstevel@tonic-gate 				aclp = NULL;
1840*7c478bd9Sstevel@tonic-gate 			}
1841*7c478bd9Sstevel@tonic-gate 
1842*7c478bd9Sstevel@tonic-gate 			cnt = 0;
1843*7c478bd9Sstevel@tonic-gate 			break;
1844*7c478bd9Sstevel@tonic-gate 		}
1845*7c478bd9Sstevel@tonic-gate 
1846*7c478bd9Sstevel@tonic-gate 		cnt++;
1847*7c478bd9Sstevel@tonic-gate 	} while (cnt < 2 && missdir(nam_p) == 0);
1848*7c478bd9Sstevel@tonic-gate 
1849*7c478bd9Sstevel@tonic-gate 	switch (cnt) {
1850*7c478bd9Sstevel@tonic-gate 	case 0:
1851*7c478bd9Sstevel@tonic-gate 		rv = 1;
1852*7c478bd9Sstevel@tonic-gate 		rstfiles(U_OVER, dirfd);
1853*7c478bd9Sstevel@tonic-gate 		break;
1854*7c478bd9Sstevel@tonic-gate 
1855*7c478bd9Sstevel@tonic-gate 	case 1:
1856*7c478bd9Sstevel@tonic-gate 		msg(ERRN,
1857*7c478bd9Sstevel@tonic-gate 		    "Cannot create directory for \"%s\"", nam_p);
1858*7c478bd9Sstevel@tonic-gate 
1859*7c478bd9Sstevel@tonic-gate 		if (*Over_p == '\0') {
1860*7c478bd9Sstevel@tonic-gate 			rstfiles(U_KEEP, dirfd);
1861*7c478bd9Sstevel@tonic-gate 		}
1862*7c478bd9Sstevel@tonic-gate 
1863*7c478bd9Sstevel@tonic-gate 		break;
1864*7c478bd9Sstevel@tonic-gate 
1865*7c478bd9Sstevel@tonic-gate 	case 2:
1866*7c478bd9Sstevel@tonic-gate 		if (ustar_dir() || Adir) {
1867*7c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot create directory \"%s\"", nam_p);
1868*7c478bd9Sstevel@tonic-gate 		} else if (ustar_spec() || Aspec) {
1869*7c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot mknod() \"%s\"", nam_p);
1870*7c478bd9Sstevel@tonic-gate 		}
1871*7c478bd9Sstevel@tonic-gate 
1872*7c478bd9Sstevel@tonic-gate 		if (*Over_p == '\0') {
1873*7c478bd9Sstevel@tonic-gate 			rstfiles(U_KEEP, dirfd);
1874*7c478bd9Sstevel@tonic-gate 		}
1875*7c478bd9Sstevel@tonic-gate 
1876*7c478bd9Sstevel@tonic-gate 		break;
1877*7c478bd9Sstevel@tonic-gate 
1878*7c478bd9Sstevel@tonic-gate 	default:
1879*7c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible case.");
1880*7c478bd9Sstevel@tonic-gate 	}
1881*7c478bd9Sstevel@tonic-gate 
1882*7c478bd9Sstevel@tonic-gate 	return (rv);
1883*7c478bd9Sstevel@tonic-gate }
1884*7c478bd9Sstevel@tonic-gate 
1885*7c478bd9Sstevel@tonic-gate /*
1886*7c478bd9Sstevel@tonic-gate  * creat_tmp:
1887*7c478bd9Sstevel@tonic-gate  */
1888*7c478bd9Sstevel@tonic-gate 
1889*7c478bd9Sstevel@tonic-gate static int
1890*7c478bd9Sstevel@tonic-gate creat_tmp(char *nam_p)
1891*7c478bd9Sstevel@tonic-gate {
1892*7c478bd9Sstevel@tonic-gate 	char *t_p;
1893*7c478bd9Sstevel@tonic-gate 	int	cwd;
1894*7c478bd9Sstevel@tonic-gate 
1895*7c478bd9Sstevel@tonic-gate 	if ((Args & OCp) && G_p->g_ino == DesSt.st_ino &&
1896*7c478bd9Sstevel@tonic-gate 	    G_p->g_dev == DesSt.st_dev) {
1897*7c478bd9Sstevel@tonic-gate 		msg(ERR, "Attempt to pass a file to itself.");
1898*7c478bd9Sstevel@tonic-gate 		return (-1);
1899*7c478bd9Sstevel@tonic-gate 	}
1900*7c478bd9Sstevel@tonic-gate 
1901*7c478bd9Sstevel@tonic-gate 	if (G_p->g_mtime <= DesSt.st_mtime && !(Args & OCu)) {
1902*7c478bd9Sstevel@tonic-gate 		msg(ERR, "Existing \"%s\" same age or newer", nam_p);
1903*7c478bd9Sstevel@tonic-gate 		return (-1);
1904*7c478bd9Sstevel@tonic-gate 	}
1905*7c478bd9Sstevel@tonic-gate 
1906*7c478bd9Sstevel@tonic-gate 	if (Euid && Aspec) {
1907*7c478bd9Sstevel@tonic-gate 		msg(ERR, "Cannot overwrite \"%s\"", nam_p);
1908*7c478bd9Sstevel@tonic-gate 		return (-1);
1909*7c478bd9Sstevel@tonic-gate 	}
1910*7c478bd9Sstevel@tonic-gate 
1911*7c478bd9Sstevel@tonic-gate 	/* Make the temporary file name. */
1912*7c478bd9Sstevel@tonic-gate 
1913*7c478bd9Sstevel@tonic-gate 	(void) strcpy(Over_p, nam_p);
1914*7c478bd9Sstevel@tonic-gate 	t_p = Over_p + strlen(Over_p);
1915*7c478bd9Sstevel@tonic-gate 
1916*7c478bd9Sstevel@tonic-gate 	while (t_p != Over_p) {
1917*7c478bd9Sstevel@tonic-gate 		if (*(t_p - 1) == '/')
1918*7c478bd9Sstevel@tonic-gate 			break;
1919*7c478bd9Sstevel@tonic-gate 		t_p--;
1920*7c478bd9Sstevel@tonic-gate 	}
1921*7c478bd9Sstevel@tonic-gate 
1922*7c478bd9Sstevel@tonic-gate 	(void) strcpy(t_p, "XXXXXX");
1923*7c478bd9Sstevel@tonic-gate 
1924*7c478bd9Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
1925*7c478bd9Sstevel@tonic-gate 		/*
1926*7c478bd9Sstevel@tonic-gate 		 * Save our current directory, so we can go into
1927*7c478bd9Sstevel@tonic-gate 		 * the attribute directory to make the temp file
1928*7c478bd9Sstevel@tonic-gate 		 * and then return.
1929*7c478bd9Sstevel@tonic-gate 		 */
1930*7c478bd9Sstevel@tonic-gate 
1931*7c478bd9Sstevel@tonic-gate 		cwd = save_cwd();
1932*7c478bd9Sstevel@tonic-gate 		(void) fchdir(G_p->g_dirfd);
1933*7c478bd9Sstevel@tonic-gate 	}
1934*7c478bd9Sstevel@tonic-gate 
1935*7c478bd9Sstevel@tonic-gate 	(void) mktemp(Over_p);
1936*7c478bd9Sstevel@tonic-gate 
1937*7c478bd9Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
1938*7c478bd9Sstevel@tonic-gate 		/* Return to the current directory. */
1939*7c478bd9Sstevel@tonic-gate 
1940*7c478bd9Sstevel@tonic-gate 		rest_cwd(cwd);
1941*7c478bd9Sstevel@tonic-gate 	}
1942*7c478bd9Sstevel@tonic-gate 
1943*7c478bd9Sstevel@tonic-gate 	if (*Over_p == '\0') {
1944*7c478bd9Sstevel@tonic-gate 		/* mktemp reports a failure. */
1945*7c478bd9Sstevel@tonic-gate 
1946*7c478bd9Sstevel@tonic-gate 		msg(ERR, "Cannot get temporary file name.");
1947*7c478bd9Sstevel@tonic-gate 		return (-1);
1948*7c478bd9Sstevel@tonic-gate 	}
1949*7c478bd9Sstevel@tonic-gate 
1950*7c478bd9Sstevel@tonic-gate 	/*
1951*7c478bd9Sstevel@tonic-gate 	 * If it's a regular file, write to the temporary file, and then rename
1952*7c478bd9Sstevel@tonic-gate 	 * in order to accomodate potential executables.
1953*7c478bd9Sstevel@tonic-gate 	 *
1954*7c478bd9Sstevel@tonic-gate 	 * Note: g_typeflag is only defined (set) for USTAR archive types.  It
1955*7c478bd9Sstevel@tonic-gate 	 * defaults to 0 in the cpio-format-regular file case, so this test
1956*7c478bd9Sstevel@tonic-gate 	 * succeeds.
1957*7c478bd9Sstevel@tonic-gate 	 */
1958*7c478bd9Sstevel@tonic-gate 
1959*7c478bd9Sstevel@tonic-gate 	if (G_p->g_typeflag == 0 &&
1960*7c478bd9Sstevel@tonic-gate 	    (DesSt.st_mode & (ulong_t)Ftype) == S_IFREG &&
1961*7c478bd9Sstevel@tonic-gate 	    (G_p->g_mode & (ulong_t)Ftype) == S_IFREG) {
1962*7c478bd9Sstevel@tonic-gate 		/*
1963*7c478bd9Sstevel@tonic-gate 		 * The archive file and the filesystem file are both regular
1964*7c478bd9Sstevel@tonic-gate 		 * files.  We write to the temporary file in this case.
1965*7c478bd9Sstevel@tonic-gate 		 */
1966*7c478bd9Sstevel@tonic-gate 
1967*7c478bd9Sstevel@tonic-gate 		if (Args & OCp) {
1968*7c478bd9Sstevel@tonic-gate 			if (G_p->g_attrnam_p == (char *)NULL) {
1969*7c478bd9Sstevel@tonic-gate 				Fullnam_p = Over_p;
1970*7c478bd9Sstevel@tonic-gate 			} else {
1971*7c478bd9Sstevel@tonic-gate 				Attrfile_p = Over_p;
1972*7c478bd9Sstevel@tonic-gate 			}
1973*7c478bd9Sstevel@tonic-gate 		} else {
1974*7c478bd9Sstevel@tonic-gate 			G_p->g_nam_p = Over_p;
1975*7c478bd9Sstevel@tonic-gate 			if (G_p->g_attrnam_p != (char *)NULL) {
1976*7c478bd9Sstevel@tonic-gate 				Attrfile_p = Over_p;
1977*7c478bd9Sstevel@tonic-gate 			}
1978*7c478bd9Sstevel@tonic-gate 		}
1979*7c478bd9Sstevel@tonic-gate 
1980*7c478bd9Sstevel@tonic-gate 		if (G_p->g_attrnam_p == (char *)NULL) {
1981*7c478bd9Sstevel@tonic-gate 			Over_p = nam_p;
1982*7c478bd9Sstevel@tonic-gate 		} else {
1983*7c478bd9Sstevel@tonic-gate 			Over_p = G_p->g_attrnam_p;
1984*7c478bd9Sstevel@tonic-gate 		}
1985*7c478bd9Sstevel@tonic-gate 
1986*7c478bd9Sstevel@tonic-gate 		Do_rename = 1;
1987*7c478bd9Sstevel@tonic-gate 	} else {
1988*7c478bd9Sstevel@tonic-gate 		/*
1989*7c478bd9Sstevel@tonic-gate 		 * Either the archive file or the filesystem file is not a
1990*7c478bd9Sstevel@tonic-gate 		 * regular file.
1991*7c478bd9Sstevel@tonic-gate 		 */
1992*7c478bd9Sstevel@tonic-gate 
1993*7c478bd9Sstevel@tonic-gate 		Do_rename = 0;
1994*7c478bd9Sstevel@tonic-gate 
1995*7c478bd9Sstevel@tonic-gate 		if (S_ISDIR(DesSt.st_mode)) {
1996*7c478bd9Sstevel@tonic-gate 			/*
1997*7c478bd9Sstevel@tonic-gate 			 * The filesystem file is a directory.
1998*7c478bd9Sstevel@tonic-gate 			 *
1999*7c478bd9Sstevel@tonic-gate 			 * Save the current working directory because we will
2000*7c478bd9Sstevel@tonic-gate 			 * want to restore it back just in case remove_dir()
2001*7c478bd9Sstevel@tonic-gate 			 * fails or get confused about where we should be.
2002*7c478bd9Sstevel@tonic-gate 			 */
2003*7c478bd9Sstevel@tonic-gate 
2004*7c478bd9Sstevel@tonic-gate 			*Over_p = '\0';
2005*7c478bd9Sstevel@tonic-gate 			cwd = save_cwd();
2006*7c478bd9Sstevel@tonic-gate 
2007*7c478bd9Sstevel@tonic-gate 			if (remove_dir(nam_p) < 0) {
2008*7c478bd9Sstevel@tonic-gate 				msg(ERRN,
2009*7c478bd9Sstevel@tonic-gate 				    "Cannot remove the directory \"%s\"",
2010*7c478bd9Sstevel@tonic-gate 				    nam_p);
2011*7c478bd9Sstevel@tonic-gate 				/*
2012*7c478bd9Sstevel@tonic-gate 				 * Restore working directory back to the one
2013*7c478bd9Sstevel@tonic-gate 				 * saved earlier.
2014*7c478bd9Sstevel@tonic-gate 				 */
2015*7c478bd9Sstevel@tonic-gate 
2016*7c478bd9Sstevel@tonic-gate 				rest_cwd(cwd);
2017*7c478bd9Sstevel@tonic-gate 				return (-1);
2018*7c478bd9Sstevel@tonic-gate 			}
2019*7c478bd9Sstevel@tonic-gate 
2020*7c478bd9Sstevel@tonic-gate 			/*
2021*7c478bd9Sstevel@tonic-gate 			 * Restore working directory back to the one
2022*7c478bd9Sstevel@tonic-gate 			 * saved earlier
2023*7c478bd9Sstevel@tonic-gate 			 */
2024*7c478bd9Sstevel@tonic-gate 
2025*7c478bd9Sstevel@tonic-gate 			rest_cwd(cwd);
2026*7c478bd9Sstevel@tonic-gate 		} else {
2027*7c478bd9Sstevel@tonic-gate 			/*
2028*7c478bd9Sstevel@tonic-gate 			 * The file is not a directory. Will use the original
2029*7c478bd9Sstevel@tonic-gate 			 * link/unlink construct, however, if the file is
2030*7c478bd9Sstevel@tonic-gate 			 * namefs, link would fail with EXDEV. Therefore, we
2031*7c478bd9Sstevel@tonic-gate 			 * use rename() first to back up the file.
2032*7c478bd9Sstevel@tonic-gate 			 */
2033*7c478bd9Sstevel@tonic-gate 			if (rename(nam_p, Over_p) < 0) {
2034*7c478bd9Sstevel@tonic-gate 				/*
2035*7c478bd9Sstevel@tonic-gate 				 * If rename failed, try old construction
2036*7c478bd9Sstevel@tonic-gate 				 * method.
2037*7c478bd9Sstevel@tonic-gate 				 */
2038*7c478bd9Sstevel@tonic-gate 				if (link(nam_p, Over_p) < 0) {
2039*7c478bd9Sstevel@tonic-gate 					msg(ERRN,
2040*7c478bd9Sstevel@tonic-gate 					    "Cannot create temporary file");
2041*7c478bd9Sstevel@tonic-gate 					*Over_p = '\0';
2042*7c478bd9Sstevel@tonic-gate 					return (-1);
2043*7c478bd9Sstevel@tonic-gate 				}
2044*7c478bd9Sstevel@tonic-gate 
2045*7c478bd9Sstevel@tonic-gate 				if (unlink(nam_p) < 0) {
2046*7c478bd9Sstevel@tonic-gate 					msg(ERRN,
2047*7c478bd9Sstevel@tonic-gate 					    "Cannot unlink() current \"%s\"",
2048*7c478bd9Sstevel@tonic-gate 					    nam_p);
2049*7c478bd9Sstevel@tonic-gate 					(void) unlink(Over_p);
2050*7c478bd9Sstevel@tonic-gate 					*Over_p = '\0';
2051*7c478bd9Sstevel@tonic-gate 					return (-1);
2052*7c478bd9Sstevel@tonic-gate 				}
2053*7c478bd9Sstevel@tonic-gate 			}
2054*7c478bd9Sstevel@tonic-gate 		}
2055*7c478bd9Sstevel@tonic-gate 	}
2056*7c478bd9Sstevel@tonic-gate 
2057*7c478bd9Sstevel@tonic-gate 	return (1);
2058*7c478bd9Sstevel@tonic-gate }
2059*7c478bd9Sstevel@tonic-gate 
2060*7c478bd9Sstevel@tonic-gate /*
2061*7c478bd9Sstevel@tonic-gate  * data_in:  If proc_mode == P_PROC, bread() the file's data from the archive
2062*7c478bd9Sstevel@tonic-gate  * and write(2) it to the open fdes gotten from openout().  If proc_mode ==
2063*7c478bd9Sstevel@tonic-gate  * P_SKIP, or becomes P_SKIP (due to errors etc), bread(2) the file's data
2064*7c478bd9Sstevel@tonic-gate  * and ignore it.  If the user specified any of the "swap" options (b, s or S),
2065*7c478bd9Sstevel@tonic-gate  * and the length of the file is not appropriate for that action, do not
2066*7c478bd9Sstevel@tonic-gate  * perform the "swap", otherwise perform the action on a buffer by buffer basis.
2067*7c478bd9Sstevel@tonic-gate  * If the CRC header was selected, calculate a running checksum as each buffer
2068*7c478bd9Sstevel@tonic-gate  * is processed.
2069*7c478bd9Sstevel@tonic-gate  */
2070*7c478bd9Sstevel@tonic-gate 
2071*7c478bd9Sstevel@tonic-gate static void
2072*7c478bd9Sstevel@tonic-gate data_in(int proc_mode)
2073*7c478bd9Sstevel@tonic-gate {
2074*7c478bd9Sstevel@tonic-gate 	char *nam_p;
2075*7c478bd9Sstevel@tonic-gate 	int cnt, pad;
2076*7c478bd9Sstevel@tonic-gate 	long cksumval = 0L;
2077*7c478bd9Sstevel@tonic-gate 	off_t filesz;
2078*7c478bd9Sstevel@tonic-gate 	int rv, swapfile = 0;
2079*7c478bd9Sstevel@tonic-gate 	int compress_flag = 0;	/* if the file is compressed */
2080*7c478bd9Sstevel@tonic-gate 	int cstatus = 0;
2081*7c478bd9Sstevel@tonic-gate 	FILE *pipef;		/* pipe for bar to do de-compression */
2082*7c478bd9Sstevel@tonic-gate 
2083*7c478bd9Sstevel@tonic-gate 
2084*7c478bd9Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
2085*7c478bd9Sstevel@tonic-gate 		nam_p = G_p->g_attrnam_p;
2086*7c478bd9Sstevel@tonic-gate 	} else {
2087*7c478bd9Sstevel@tonic-gate 		nam_p = G_p->g_nam_p;
2088*7c478bd9Sstevel@tonic-gate 	}
2089*7c478bd9Sstevel@tonic-gate 
2090*7c478bd9Sstevel@tonic-gate 
2091*7c478bd9Sstevel@tonic-gate 	if (((G_p->g_mode & Ftype) == S_IFLNK && proc_mode != P_SKIP) ||
2092*7c478bd9Sstevel@tonic-gate 	    (Hdr_type == BAR && bar_linkflag == '2' && proc_mode != P_SKIP)) {
2093*7c478bd9Sstevel@tonic-gate 		proc_mode = P_SKIP;
2094*7c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
2095*7c478bd9Sstevel@tonic-gate 	}
2096*7c478bd9Sstevel@tonic-gate 	if (Args & (OCb | OCs | OCS)) { /* verfify that swapping is possible */
2097*7c478bd9Sstevel@tonic-gate 		swapfile = 1;
2098*7c478bd9Sstevel@tonic-gate 		if (Args & (OCs | OCb) && G_p->g_filesz % 2) {
2099*7c478bd9Sstevel@tonic-gate 			msg(ERR,
2100*7c478bd9Sstevel@tonic-gate 			    "Cannot swap bytes of \"%s\", odd number of bytes",
2101*7c478bd9Sstevel@tonic-gate 			    nam_p);
2102*7c478bd9Sstevel@tonic-gate 			swapfile = 0;
2103*7c478bd9Sstevel@tonic-gate 		}
2104*7c478bd9Sstevel@tonic-gate 		if (Args & (OCS | OCb) && G_p->g_filesz % 4) {
2105*7c478bd9Sstevel@tonic-gate 			msg(ERR,
2106*7c478bd9Sstevel@tonic-gate 			    "Cannot swap halfwords of \"%s\", odd number "
2107*7c478bd9Sstevel@tonic-gate 			    "of halfwords", nam_p);
2108*7c478bd9Sstevel@tonic-gate 			swapfile = 0;
2109*7c478bd9Sstevel@tonic-gate 		}
2110*7c478bd9Sstevel@tonic-gate 	}
2111*7c478bd9Sstevel@tonic-gate 	filesz = G_p->g_filesz;
2112*7c478bd9Sstevel@tonic-gate 
2113*7c478bd9Sstevel@tonic-gate 	/* This writes out the file from the archive */
2114*7c478bd9Sstevel@tonic-gate 
2115*7c478bd9Sstevel@tonic-gate 	while (filesz > 0) {
2116*7c478bd9Sstevel@tonic-gate 		cnt = (int)(filesz > CPIOBSZ) ? CPIOBSZ : filesz;
2117*7c478bd9Sstevel@tonic-gate 		FILL(cnt);
2118*7c478bd9Sstevel@tonic-gate 		if (proc_mode != P_SKIP) {
2119*7c478bd9Sstevel@tonic-gate 			if (Hdr_type == CRC)
2120*7c478bd9Sstevel@tonic-gate 				cksumval += cksum(CRC, cnt, NULL);
2121*7c478bd9Sstevel@tonic-gate 			if (swapfile)
2122*7c478bd9Sstevel@tonic-gate 				swap(Buffr.b_out_p, cnt);
2123*7c478bd9Sstevel@tonic-gate 			errno = 0;
2124*7c478bd9Sstevel@tonic-gate 
2125*7c478bd9Sstevel@tonic-gate 			/*
2126*7c478bd9Sstevel@tonic-gate 			 * if the bar archive is compressed, set up a pipe and
2127*7c478bd9Sstevel@tonic-gate 			 * do the de-compression while reading in the file
2128*7c478bd9Sstevel@tonic-gate 			 */
2129*7c478bd9Sstevel@tonic-gate 			if (Hdr_type == BAR) {
2130*7c478bd9Sstevel@tonic-gate 				if (compress_flag == 0 && Compressed) {
2131*7c478bd9Sstevel@tonic-gate 					setup_uncompress(&pipef);
2132*7c478bd9Sstevel@tonic-gate 					compress_flag++;
2133*7c478bd9Sstevel@tonic-gate 				}
2134*7c478bd9Sstevel@tonic-gate 
2135*7c478bd9Sstevel@tonic-gate 			}
2136*7c478bd9Sstevel@tonic-gate 
2137*7c478bd9Sstevel@tonic-gate 			rv = write(Ofile, Buffr.b_out_p, cnt);
2138*7c478bd9Sstevel@tonic-gate 			if (rv < cnt) {
2139*7c478bd9Sstevel@tonic-gate 				if (rv < 0)
2140*7c478bd9Sstevel@tonic-gate 					msg(ERRN, "Cannot write \"%s\"", nam_p);
2141*7c478bd9Sstevel@tonic-gate 				else
2142*7c478bd9Sstevel@tonic-gate 					msg(EXTN, "Cannot write \"%s\"", nam_p);
2143*7c478bd9Sstevel@tonic-gate 				proc_mode = P_SKIP;
2144*7c478bd9Sstevel@tonic-gate 				rstfiles(U_KEEP, G_p->g_dirfd);
2145*7c478bd9Sstevel@tonic-gate 				cstatus = close(Ofile);
2146*7c478bd9Sstevel@tonic-gate 				Ofile = 0;
2147*7c478bd9Sstevel@tonic-gate 				if (cstatus != 0) {
2148*7c478bd9Sstevel@tonic-gate 					msg(EXTN, "close error");
2149*7c478bd9Sstevel@tonic-gate 				}
2150*7c478bd9Sstevel@tonic-gate 			}
2151*7c478bd9Sstevel@tonic-gate 		}
2152*7c478bd9Sstevel@tonic-gate 		Buffr.b_out_p += cnt;
2153*7c478bd9Sstevel@tonic-gate 		Buffr.b_cnt -= (long)cnt;
2154*7c478bd9Sstevel@tonic-gate 		filesz -= (off_t)cnt;
2155*7c478bd9Sstevel@tonic-gate 	} /* filesz */
2156*7c478bd9Sstevel@tonic-gate 
2157*7c478bd9Sstevel@tonic-gate 	pad = (Pad_val + 1 - (G_p->g_filesz & Pad_val)) & Pad_val;
2158*7c478bd9Sstevel@tonic-gate 	if (pad != 0) {
2159*7c478bd9Sstevel@tonic-gate 		FILL(pad);
2160*7c478bd9Sstevel@tonic-gate 		Buffr.b_out_p += pad;
2161*7c478bd9Sstevel@tonic-gate 		Buffr.b_cnt -= pad;
2162*7c478bd9Sstevel@tonic-gate 	}
2163*7c478bd9Sstevel@tonic-gate 	if (proc_mode != P_SKIP) {
2164*7c478bd9Sstevel@tonic-gate 		if (Hdr_type == CRC && Gen.g_cksum != cksumval) {
2165*7c478bd9Sstevel@tonic-gate 			msg(ERR, "\"%s\" - checksum error", nam_p);
2166*7c478bd9Sstevel@tonic-gate 			rstfiles(U_KEEP, G_p->g_dirfd);
2167*7c478bd9Sstevel@tonic-gate 		} else
2168*7c478bd9Sstevel@tonic-gate 			rstfiles(U_OVER, G_p->g_dirfd);
2169*7c478bd9Sstevel@tonic-gate 		if (Hdr_type == BAR && compress_flag) {
2170*7c478bd9Sstevel@tonic-gate 			(void) pclose(pipef);
2171*7c478bd9Sstevel@tonic-gate 		} else {
2172*7c478bd9Sstevel@tonic-gate 			cstatus = close(Ofile);
2173*7c478bd9Sstevel@tonic-gate 		}
2174*7c478bd9Sstevel@tonic-gate 		Ofile = 0;
2175*7c478bd9Sstevel@tonic-gate 		if (cstatus != 0) {
2176*7c478bd9Sstevel@tonic-gate 			msg(EXTN, "close error");
2177*7c478bd9Sstevel@tonic-gate 		}
2178*7c478bd9Sstevel@tonic-gate 	}
2179*7c478bd9Sstevel@tonic-gate 	VERBOSE((proc_mode != P_SKIP && (Args & (OCv | OCV))), G_p->g_nam_p);
2180*7c478bd9Sstevel@tonic-gate 	Finished = 1;
2181*7c478bd9Sstevel@tonic-gate }
2182*7c478bd9Sstevel@tonic-gate 
2183*7c478bd9Sstevel@tonic-gate /*
2184*7c478bd9Sstevel@tonic-gate  * data_out:  open(2) the file to be archived, compute the checksum
2185*7c478bd9Sstevel@tonic-gate  * of it's data if the CRC header was specified and write the header.
2186*7c478bd9Sstevel@tonic-gate  * read(2) each block of data and bwrite() it to the archive.  For TARTYP (TAR
2187*7c478bd9Sstevel@tonic-gate  * and USTAR) archives, pad the data with NULLs to the next 512 byte boundary.
2188*7c478bd9Sstevel@tonic-gate  */
2189*7c478bd9Sstevel@tonic-gate 
2190*7c478bd9Sstevel@tonic-gate static void
2191*7c478bd9Sstevel@tonic-gate data_out(void)
2192*7c478bd9Sstevel@tonic-gate {
2193*7c478bd9Sstevel@tonic-gate 	char *nam_p;
2194*7c478bd9Sstevel@tonic-gate 	int cnt, amount_read, pad;
2195*7c478bd9Sstevel@tonic-gate 	off_t amt_to_read, real_filesz;
2196*7c478bd9Sstevel@tonic-gate 	int	errret = 0;
2197*7c478bd9Sstevel@tonic-gate 
2198*7c478bd9Sstevel@tonic-gate 	nam_p = G_p->g_nam_p;
2199*7c478bd9Sstevel@tonic-gate 	if (Aspec) {
2200*7c478bd9Sstevel@tonic-gate 		if (Pflag && aclp != NULL) {
2201*7c478bd9Sstevel@tonic-gate 			char    *secinfo = NULL;
2202*7c478bd9Sstevel@tonic-gate 			int	len = 0;
2203*7c478bd9Sstevel@tonic-gate 
2204*7c478bd9Sstevel@tonic-gate 			/* append security attributes */
2205*7c478bd9Sstevel@tonic-gate 			if (append_secattr(&secinfo, &len, aclcnt,
2206*7c478bd9Sstevel@tonic-gate 				(char *)aclp, UFSD_ACL) == -1) {
2207*7c478bd9Sstevel@tonic-gate 				msg(ERR,
2208*7c478bd9Sstevel@tonic-gate 				    "can create security information");
2209*7c478bd9Sstevel@tonic-gate 			}
2210*7c478bd9Sstevel@tonic-gate 			/* call append_secattr() if more than one */
2211*7c478bd9Sstevel@tonic-gate 
2212*7c478bd9Sstevel@tonic-gate 			if (len > 0) {
2213*7c478bd9Sstevel@tonic-gate 			/* write ancillary only if there is sec info */
2214*7c478bd9Sstevel@tonic-gate 				(void) write_hdr(ARCHIVE_ACL, (off_t)len);
2215*7c478bd9Sstevel@tonic-gate 				(void) write_ancillary(secinfo, len);
2216*7c478bd9Sstevel@tonic-gate 			}
2217*7c478bd9Sstevel@tonic-gate 		}
2218*7c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
2219*7c478bd9Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_dirfd);
2220*7c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
2221*7c478bd9Sstevel@tonic-gate 		return;
2222*7c478bd9Sstevel@tonic-gate 	}
2223*7c478bd9Sstevel@tonic-gate 	if ((G_p->g_mode & Ftype) == S_IFLNK && (Hdr_type !=
2224*7c478bd9Sstevel@tonic-gate 	    USTAR && Hdr_type != TAR)) { /* symbolic link */
2225*7c478bd9Sstevel@tonic-gate 		int size;
2226*7c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
2227*7c478bd9Sstevel@tonic-gate 
2228*7c478bd9Sstevel@tonic-gate 		FLUSH(G_p->g_filesz);
2229*7c478bd9Sstevel@tonic-gate 		errno = 0;
2230*7c478bd9Sstevel@tonic-gate 
2231*7c478bd9Sstevel@tonic-gate 		/* Note that "size" and G_p->g_filesz are the same number */
2232*7c478bd9Sstevel@tonic-gate 
2233*7c478bd9Sstevel@tonic-gate 		if ((size = readlink(nam_p, Buffr.b_in_p, G_p->g_filesz)) <
2234*7c478bd9Sstevel@tonic-gate 		    0) {
2235*7c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot read symbolic link \"%s\"", nam_p);
2236*7c478bd9Sstevel@tonic-gate 			return;
2237*7c478bd9Sstevel@tonic-gate 		}
2238*7c478bd9Sstevel@tonic-gate 
2239*7c478bd9Sstevel@tonic-gate 		/*
2240*7c478bd9Sstevel@tonic-gate 		 * Note that it is OK not to add the NUL after the name read by
2241*7c478bd9Sstevel@tonic-gate 		 * readlink, because it is not being used subsequently.
2242*7c478bd9Sstevel@tonic-gate 		 */
2243*7c478bd9Sstevel@tonic-gate 
2244*7c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += size;
2245*7c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += size;
2246*7c478bd9Sstevel@tonic-gate 		pad = (Pad_val + 1 - (size & Pad_val)) & Pad_val;
2247*7c478bd9Sstevel@tonic-gate 		if (pad != 0) {
2248*7c478bd9Sstevel@tonic-gate 			FLUSH(pad);
2249*7c478bd9Sstevel@tonic-gate 			(void) memcpy(Buffr.b_in_p, Empty, pad);
2250*7c478bd9Sstevel@tonic-gate 			Buffr.b_in_p += pad;
2251*7c478bd9Sstevel@tonic-gate 			Buffr.b_cnt += pad;
2252*7c478bd9Sstevel@tonic-gate 		}
2253*7c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
2254*7c478bd9Sstevel@tonic-gate 		return;
2255*7c478bd9Sstevel@tonic-gate 	} else if ((G_p->g_mode & Ftype) == S_IFLNK &&
2256*7c478bd9Sstevel@tonic-gate 		    (Hdr_type == USTAR || Hdr_type == TAR)) {
2257*7c478bd9Sstevel@tonic-gate 		int size;
2258*7c478bd9Sstevel@tonic-gate 
2259*7c478bd9Sstevel@tonic-gate 		/*
2260*7c478bd9Sstevel@tonic-gate 		 * G_p->g_filesz is the length of the right-hand side of
2261*7c478bd9Sstevel@tonic-gate 		 * the symlink "x -> y".
2262*7c478bd9Sstevel@tonic-gate 		 * The tar link field is only NAMSIZ long.
2263*7c478bd9Sstevel@tonic-gate 		 */
2264*7c478bd9Sstevel@tonic-gate 
2265*7c478bd9Sstevel@tonic-gate 		if (G_p->g_filesz > NAMSIZ) {
2266*7c478bd9Sstevel@tonic-gate 			msg(ERRN,
2267*7c478bd9Sstevel@tonic-gate 			    "Symbolic link too long \"%s\"", nam_p);
2268*7c478bd9Sstevel@tonic-gate 			return;
2269*7c478bd9Sstevel@tonic-gate 		}
2270*7c478bd9Sstevel@tonic-gate 		if ((size = readlink(nam_p, T_lname, G_p->g_filesz)) < 0) {
2271*7c478bd9Sstevel@tonic-gate 			msg(ERRN,
2272*7c478bd9Sstevel@tonic-gate 			    "Cannot read symbolic link \"%s\"", nam_p);
2273*7c478bd9Sstevel@tonic-gate 			return;
2274*7c478bd9Sstevel@tonic-gate 		}
2275*7c478bd9Sstevel@tonic-gate 		T_lname[size] = '\0';
2276*7c478bd9Sstevel@tonic-gate 		G_p->g_filesz = (off_t)0;
2277*7c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
2278*7c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
2279*7c478bd9Sstevel@tonic-gate 		return;
2280*7c478bd9Sstevel@tonic-gate 	}
2281*7c478bd9Sstevel@tonic-gate 	if ((Ifile = openfile(O_RDONLY)) < 0) {
2282*7c478bd9Sstevel@tonic-gate 		msg(ERR, "\"%s%s%s\" ?",
2283*7c478bd9Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
2284*7c478bd9Sstevel@tonic-gate 		    nam_p : Gen.g_attrfnam_p,
2285*7c478bd9Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
2286*7c478bd9Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
2287*7c478bd9Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
2288*7c478bd9Sstevel@tonic-gate 		    "" : Gen.g_attrnam_p);
2289*7c478bd9Sstevel@tonic-gate 		return;
2290*7c478bd9Sstevel@tonic-gate 	}
2291*7c478bd9Sstevel@tonic-gate 
2292*7c478bd9Sstevel@tonic-gate 	/*
2293*7c478bd9Sstevel@tonic-gate 	 * Dump extended attribute header.
2294*7c478bd9Sstevel@tonic-gate 	 */
2295*7c478bd9Sstevel@tonic-gate 
2296*7c478bd9Sstevel@tonic-gate 	if (Gen.g_attrnam_p != (char *)NULL) {
2297*7c478bd9Sstevel@tonic-gate 		write_xattr_hdr();
2298*7c478bd9Sstevel@tonic-gate 	}
2299*7c478bd9Sstevel@tonic-gate 
2300*7c478bd9Sstevel@tonic-gate 	if (Hdr_type == CRC) {
2301*7c478bd9Sstevel@tonic-gate 		long csum = cksum(CRC, 0, &errret);
2302*7c478bd9Sstevel@tonic-gate 		if (errret != 0) {
2303*7c478bd9Sstevel@tonic-gate 			G_p->g_cksum = (ulong_t)-1;
2304*7c478bd9Sstevel@tonic-gate 			msg(POST, "\"%s%s%s\" skipped",
2305*7c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
2306*7c478bd9Sstevel@tonic-gate 			    nam_p : Gen.g_attrfnam_p,
2307*7c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
2308*7c478bd9Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
2309*7c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
2310*7c478bd9Sstevel@tonic-gate 			    "" : nam_p);
2311*7c478bd9Sstevel@tonic-gate 			(void) close(Ifile);
2312*7c478bd9Sstevel@tonic-gate 			return;
2313*7c478bd9Sstevel@tonic-gate 		}
2314*7c478bd9Sstevel@tonic-gate 		G_p->g_cksum = csum;
2315*7c478bd9Sstevel@tonic-gate 	} else {
2316*7c478bd9Sstevel@tonic-gate 		G_p->g_cksum = 0;
2317*7c478bd9Sstevel@tonic-gate 	}
2318*7c478bd9Sstevel@tonic-gate 
2319*7c478bd9Sstevel@tonic-gate 	/*
2320*7c478bd9Sstevel@tonic-gate 	 * ACL has been retrieved in getname().
2321*7c478bd9Sstevel@tonic-gate 	 */
2322*7c478bd9Sstevel@tonic-gate 	if (Pflag) {
2323*7c478bd9Sstevel@tonic-gate 		char    *secinfo = NULL;
2324*7c478bd9Sstevel@tonic-gate 		int	len = 0;
2325*7c478bd9Sstevel@tonic-gate 
2326*7c478bd9Sstevel@tonic-gate 		/* append security attributes */
2327*7c478bd9Sstevel@tonic-gate 		if ((append_secattr(&secinfo, &len, aclcnt, (char *)aclp,
2328*7c478bd9Sstevel@tonic-gate 		    UFSD_ACL)) == -1)
2329*7c478bd9Sstevel@tonic-gate 			msg(ERR, "can create security information");
2330*7c478bd9Sstevel@tonic-gate 
2331*7c478bd9Sstevel@tonic-gate 		/* call append_secattr() if more than one */
2332*7c478bd9Sstevel@tonic-gate 
2333*7c478bd9Sstevel@tonic-gate 		if (len > 0) {
2334*7c478bd9Sstevel@tonic-gate 		/* write ancillary only if there is sec info */
2335*7c478bd9Sstevel@tonic-gate 			(void) write_hdr(ARCHIVE_ACL, (off_t)len);
2336*7c478bd9Sstevel@tonic-gate 			(void) write_ancillary(secinfo, len);
2337*7c478bd9Sstevel@tonic-gate 		}
2338*7c478bd9Sstevel@tonic-gate 	}
2339*7c478bd9Sstevel@tonic-gate 
2340*7c478bd9Sstevel@tonic-gate 	write_hdr(ARCHIVE_NORMAL, (off_t)0);
2341*7c478bd9Sstevel@tonic-gate 
2342*7c478bd9Sstevel@tonic-gate 	real_filesz = 0;
2343*7c478bd9Sstevel@tonic-gate 
2344*7c478bd9Sstevel@tonic-gate 	for (amt_to_read = G_p->g_filesz;
2345*7c478bd9Sstevel@tonic-gate 	    amt_to_read > 0;
2346*7c478bd9Sstevel@tonic-gate 	    amt_to_read -= (off_t)amount_read) {
2347*7c478bd9Sstevel@tonic-gate 		FLUSH(CPIOBSZ);
2348*7c478bd9Sstevel@tonic-gate 		errno = 0;
2349*7c478bd9Sstevel@tonic-gate 
2350*7c478bd9Sstevel@tonic-gate 		if ((amount_read = read(Ifile, Buffr.b_in_p, CPIOBSZ)) < 0) {
2351*7c478bd9Sstevel@tonic-gate 			msg(EXTN, "Cannot read \"%s%s%s\"",
2352*7c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
2353*7c478bd9Sstevel@tonic-gate 			    nam_p : Gen.g_attrfnam_p,
2354*7c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
2355*7c478bd9Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
2356*7c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
2357*7c478bd9Sstevel@tonic-gate 			    "" : nam_p);
2358*7c478bd9Sstevel@tonic-gate 			break;
2359*7c478bd9Sstevel@tonic-gate 		}
2360*7c478bd9Sstevel@tonic-gate 
2361*7c478bd9Sstevel@tonic-gate 		if (amount_read == 0) {
2362*7c478bd9Sstevel@tonic-gate 			/* the file has shrunk */
2363*7c478bd9Sstevel@tonic-gate 			real_filesz = G_p->g_filesz - amt_to_read;
2364*7c478bd9Sstevel@tonic-gate 			break;
2365*7c478bd9Sstevel@tonic-gate 		} else if (amount_read > amt_to_read) {
2366*7c478bd9Sstevel@tonic-gate 			/* the file has grown */
2367*7c478bd9Sstevel@tonic-gate 			real_filesz = G_p->g_filesz +
2368*7c478bd9Sstevel@tonic-gate 				(amount_read - amt_to_read);
2369*7c478bd9Sstevel@tonic-gate 			amount_read = amt_to_read;
2370*7c478bd9Sstevel@tonic-gate 		} else if (amount_read == amt_to_read) {
2371*7c478bd9Sstevel@tonic-gate 			/* the file is the same size */
2372*7c478bd9Sstevel@tonic-gate 			real_filesz = G_p->g_filesz;
2373*7c478bd9Sstevel@tonic-gate 		}
2374*7c478bd9Sstevel@tonic-gate 
2375*7c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += amount_read;
2376*7c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += (long)amount_read;
2377*7c478bd9Sstevel@tonic-gate 	}
2378*7c478bd9Sstevel@tonic-gate 
2379*7c478bd9Sstevel@tonic-gate 	while (amt_to_read > 0) {
2380*7c478bd9Sstevel@tonic-gate 		cnt = (amt_to_read > CPIOBSZ) ? CPIOBSZ : (int)amt_to_read;
2381*7c478bd9Sstevel@tonic-gate 		FLUSH(cnt);
2382*7c478bd9Sstevel@tonic-gate 		(void) memset(Buffr.b_in_p, NULL, cnt);
2383*7c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += cnt;
2384*7c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += cnt;
2385*7c478bd9Sstevel@tonic-gate 		amt_to_read -= cnt;
2386*7c478bd9Sstevel@tonic-gate 	}
2387*7c478bd9Sstevel@tonic-gate 
2388*7c478bd9Sstevel@tonic-gate 	pad = (Pad_val + 1 - (G_p->g_filesz & Pad_val)) & Pad_val;
2389*7c478bd9Sstevel@tonic-gate 	if (pad != 0) {
2390*7c478bd9Sstevel@tonic-gate 		FLUSH(pad);
2391*7c478bd9Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, Empty, pad);
2392*7c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += pad;
2393*7c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += pad;
2394*7c478bd9Sstevel@tonic-gate 	}
2395*7c478bd9Sstevel@tonic-gate 
2396*7c478bd9Sstevel@tonic-gate 	if (real_filesz > G_p->g_filesz) {
2397*7c478bd9Sstevel@tonic-gate 		msg(ERR, "File size of \"%s%s%s\" has increased by %lld",
2398*7c478bd9Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
2399*7c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p : Gen.g_attrfnam_p,
2400*7c478bd9Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
2401*7c478bd9Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
2402*7c478bd9Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
2403*7c478bd9Sstevel@tonic-gate 		    "" : G_p->g_nam_p,
2404*7c478bd9Sstevel@tonic-gate 		    (real_filesz - G_p->g_filesz));
2405*7c478bd9Sstevel@tonic-gate 	}
2406*7c478bd9Sstevel@tonic-gate 	if (real_filesz < G_p->g_filesz) {
2407*7c478bd9Sstevel@tonic-gate 		msg(ERR, "File size of \"%s%s%s\" has decreased by %lld",
2408*7c478bd9Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
2409*7c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p : Gen.g_attrfnam_p,
2410*7c478bd9Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
2411*7c478bd9Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
2412*7c478bd9Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
2413*7c478bd9Sstevel@tonic-gate 		    "" : G_p->g_nam_p,
2414*7c478bd9Sstevel@tonic-gate 		    (G_p->g_filesz - real_filesz));
2415*7c478bd9Sstevel@tonic-gate 	}
2416*7c478bd9Sstevel@tonic-gate 
2417*7c478bd9Sstevel@tonic-gate 	(void) close(Ifile);
2418*7c478bd9Sstevel@tonic-gate 	rstfiles(U_KEEP, G_p->g_dirfd);
2419*7c478bd9Sstevel@tonic-gate 	VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
2420*7c478bd9Sstevel@tonic-gate }
2421*7c478bd9Sstevel@tonic-gate 
2422*7c478bd9Sstevel@tonic-gate /*
2423*7c478bd9Sstevel@tonic-gate  * data_pass:  If not a special file (Aspec), open(2) the file to be
2424*7c478bd9Sstevel@tonic-gate  * transferred, read(2) each block of data and write(2) it to the output file
2425*7c478bd9Sstevel@tonic-gate  * Ofile, which was opened in file_pass().
2426*7c478bd9Sstevel@tonic-gate  */
2427*7c478bd9Sstevel@tonic-gate 
2428*7c478bd9Sstevel@tonic-gate static void
2429*7c478bd9Sstevel@tonic-gate data_pass(void)
2430*7c478bd9Sstevel@tonic-gate {
2431*7c478bd9Sstevel@tonic-gate 	int cnt, done = 1;
2432*7c478bd9Sstevel@tonic-gate 	int cstatus;
2433*7c478bd9Sstevel@tonic-gate 	off_t filesz;
2434*7c478bd9Sstevel@tonic-gate 	char *namep = Nam_p;
2435*7c478bd9Sstevel@tonic-gate 
2436*7c478bd9Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
2437*7c478bd9Sstevel@tonic-gate 		namep = G_p->g_attrnam_p;
2438*7c478bd9Sstevel@tonic-gate 	}
2439*7c478bd9Sstevel@tonic-gate 	if (Aspec) {
2440*7c478bd9Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_passdirfd);
2441*7c478bd9Sstevel@tonic-gate 		cstatus = close(Ofile);
2442*7c478bd9Sstevel@tonic-gate 		Ofile = 0;
2443*7c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), Nam_p);
2444*7c478bd9Sstevel@tonic-gate 		if (cstatus != 0) {
2445*7c478bd9Sstevel@tonic-gate 			msg(EXTN, "close error");
2446*7c478bd9Sstevel@tonic-gate 		}
2447*7c478bd9Sstevel@tonic-gate 		return;
2448*7c478bd9Sstevel@tonic-gate 	}
2449*7c478bd9Sstevel@tonic-gate 	if ((Ifile = openat(G_p->g_dirfd, get_component(namep), 0)) < 0) {
2450*7c478bd9Sstevel@tonic-gate 		msg(ERRN, "Cannot open \"%s%s%s\", skipped",
2451*7c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
2452*7c478bd9Sstevel@tonic-gate 		    Nam_p : G_p->g_attrfnam_p,
2453*7c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
2454*7c478bd9Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
2455*7c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
2456*7c478bd9Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p);
2457*7c478bd9Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_passdirfd);
2458*7c478bd9Sstevel@tonic-gate 		cstatus = close(Ofile);
2459*7c478bd9Sstevel@tonic-gate 		Ofile = 0;
2460*7c478bd9Sstevel@tonic-gate 		if (cstatus != 0) {
2461*7c478bd9Sstevel@tonic-gate 			msg(EXTN, "close error");
2462*7c478bd9Sstevel@tonic-gate 		}
2463*7c478bd9Sstevel@tonic-gate 		return;
2464*7c478bd9Sstevel@tonic-gate 	}
2465*7c478bd9Sstevel@tonic-gate 	filesz = G_p->g_filesz;
2466*7c478bd9Sstevel@tonic-gate 
2467*7c478bd9Sstevel@tonic-gate 	while (filesz > 0) {
2468*7c478bd9Sstevel@tonic-gate 		cnt = (unsigned)(filesz > Bufsize) ? Bufsize : filesz;
2469*7c478bd9Sstevel@tonic-gate 		errno = 0;
2470*7c478bd9Sstevel@tonic-gate 		if (read(Ifile, Buf_p, (unsigned)cnt) < 0) {
2471*7c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot read \"%s%s%s\"",
2472*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
2473*7c478bd9Sstevel@tonic-gate 			    Nam_p : G_p->g_attrfnam_p,
2474*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
2475*7c478bd9Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
2476*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
2477*7c478bd9Sstevel@tonic-gate 			    "" : G_p->g_attrnam_p);
2478*7c478bd9Sstevel@tonic-gate 			done = 0;
2479*7c478bd9Sstevel@tonic-gate 			break;
2480*7c478bd9Sstevel@tonic-gate 		}
2481*7c478bd9Sstevel@tonic-gate 		errno = 0;
2482*7c478bd9Sstevel@tonic-gate 		if (write(Ofile, Buf_p, (unsigned)cnt) < 0) {
2483*7c478bd9Sstevel@tonic-gate 			if (Do_rename) {
2484*7c478bd9Sstevel@tonic-gate 				msg(ERRN, "Cannot write \"%s%s%s\"", Over_p,
2485*7c478bd9Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
2486*7c478bd9Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
2487*7c478bd9Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
2488*7c478bd9Sstevel@tonic-gate 				    "" : Over_p);
2489*7c478bd9Sstevel@tonic-gate 			} else {
2490*7c478bd9Sstevel@tonic-gate 				msg(ERRN, "Cannot write \"%s%s%s\"",
2491*7c478bd9Sstevel@tonic-gate 				    Fullnam_p,
2492*7c478bd9Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
2493*7c478bd9Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
2494*7c478bd9Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
2495*7c478bd9Sstevel@tonic-gate 				    "" : G_p->g_attrnam_p);
2496*7c478bd9Sstevel@tonic-gate 			}
2497*7c478bd9Sstevel@tonic-gate 
2498*7c478bd9Sstevel@tonic-gate 			done = 0;
2499*7c478bd9Sstevel@tonic-gate 			break;
2500*7c478bd9Sstevel@tonic-gate 		}
2501*7c478bd9Sstevel@tonic-gate 		Blocks += (u_longlong_t)((cnt + (Bufsize - 1)) / Bufsize);
2502*7c478bd9Sstevel@tonic-gate 		filesz -= (off_t)cnt;
2503*7c478bd9Sstevel@tonic-gate 	}
2504*7c478bd9Sstevel@tonic-gate 	if (done) {
2505*7c478bd9Sstevel@tonic-gate 		rstfiles(U_OVER, G_p->g_passdirfd);
2506*7c478bd9Sstevel@tonic-gate 	} else {
2507*7c478bd9Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_passdirfd);
2508*7c478bd9Sstevel@tonic-gate 	}
2509*7c478bd9Sstevel@tonic-gate 	(void) close(Ifile);
2510*7c478bd9Sstevel@tonic-gate 	cstatus = close(Ofile);
2511*7c478bd9Sstevel@tonic-gate 	Ofile = 0;
2512*7c478bd9Sstevel@tonic-gate 	if (cstatus != 0) {
2513*7c478bd9Sstevel@tonic-gate 		msg(EXTN, "close error");
2514*7c478bd9Sstevel@tonic-gate 	}
2515*7c478bd9Sstevel@tonic-gate 	VERBOSE((Args & (OCv | OCV)), Fullnam_p);
2516*7c478bd9Sstevel@tonic-gate 	Finished = 1;
2517*7c478bd9Sstevel@tonic-gate }
2518*7c478bd9Sstevel@tonic-gate 
2519*7c478bd9Sstevel@tonic-gate /*
2520*7c478bd9Sstevel@tonic-gate  * Allocation wrappers.  Used to centralize error handling for
2521*7c478bd9Sstevel@tonic-gate  * failed allocations.
2522*7c478bd9Sstevel@tonic-gate  */
2523*7c478bd9Sstevel@tonic-gate static void *
2524*7c478bd9Sstevel@tonic-gate e_alloc_fail(int flag)
2525*7c478bd9Sstevel@tonic-gate {
2526*7c478bd9Sstevel@tonic-gate 	if (flag == E_EXIT) {
2527*7c478bd9Sstevel@tonic-gate 		msg(EXTN, "Out of memory");
2528*7c478bd9Sstevel@tonic-gate 	}
2529*7c478bd9Sstevel@tonic-gate 
2530*7c478bd9Sstevel@tonic-gate 	return (NULL);
2531*7c478bd9Sstevel@tonic-gate }
2532*7c478bd9Sstevel@tonic-gate 
2533*7c478bd9Sstevel@tonic-gate /*
2534*7c478bd9Sstevel@tonic-gate  *  Note: unlike the other e_*lloc functions, e_realloc does not zero out the
2535*7c478bd9Sstevel@tonic-gate  *  additional memory it returns.  Ensure that you do not trust its contents
2536*7c478bd9Sstevel@tonic-gate  *  when you call it.
2537*7c478bd9Sstevel@tonic-gate  */
2538*7c478bd9Sstevel@tonic-gate 
2539*7c478bd9Sstevel@tonic-gate static void *
2540*7c478bd9Sstevel@tonic-gate e_realloc(int flag, void *old, size_t newsize)
2541*7c478bd9Sstevel@tonic-gate {
2542*7c478bd9Sstevel@tonic-gate 	void *ret = realloc(old, newsize);
2543*7c478bd9Sstevel@tonic-gate 
2544*7c478bd9Sstevel@tonic-gate 	if (ret == NULL) {
2545*7c478bd9Sstevel@tonic-gate 		return (e_alloc_fail(flag));
2546*7c478bd9Sstevel@tonic-gate 	}
2547*7c478bd9Sstevel@tonic-gate 
2548*7c478bd9Sstevel@tonic-gate 	return (ret);
2549*7c478bd9Sstevel@tonic-gate }
2550*7c478bd9Sstevel@tonic-gate 
2551*7c478bd9Sstevel@tonic-gate static char *
2552*7c478bd9Sstevel@tonic-gate e_strdup(int flag, const char *arg)
2553*7c478bd9Sstevel@tonic-gate {
2554*7c478bd9Sstevel@tonic-gate 	char *ret = strdup(arg);
2555*7c478bd9Sstevel@tonic-gate 
2556*7c478bd9Sstevel@tonic-gate 	if (ret == NULL) {
2557*7c478bd9Sstevel@tonic-gate 		return (e_alloc_fail(flag));
2558*7c478bd9Sstevel@tonic-gate 	}
2559*7c478bd9Sstevel@tonic-gate 
2560*7c478bd9Sstevel@tonic-gate 	return (ret);
2561*7c478bd9Sstevel@tonic-gate }
2562*7c478bd9Sstevel@tonic-gate 
2563*7c478bd9Sstevel@tonic-gate static void *
2564*7c478bd9Sstevel@tonic-gate e_valloc(int flag, size_t size)
2565*7c478bd9Sstevel@tonic-gate {
2566*7c478bd9Sstevel@tonic-gate 	void *ret = valloc(size);
2567*7c478bd9Sstevel@tonic-gate 
2568*7c478bd9Sstevel@tonic-gate 	if (ret == NULL) {
2569*7c478bd9Sstevel@tonic-gate 		return (e_alloc_fail(flag));
2570*7c478bd9Sstevel@tonic-gate 	}
2571*7c478bd9Sstevel@tonic-gate 
2572*7c478bd9Sstevel@tonic-gate 	return (ret);
2573*7c478bd9Sstevel@tonic-gate }
2574*7c478bd9Sstevel@tonic-gate 
2575*7c478bd9Sstevel@tonic-gate static void *
2576*7c478bd9Sstevel@tonic-gate e_zalloc(int flag, size_t size)
2577*7c478bd9Sstevel@tonic-gate {
2578*7c478bd9Sstevel@tonic-gate 	void *ret = malloc(size);
2579*7c478bd9Sstevel@tonic-gate 
2580*7c478bd9Sstevel@tonic-gate 	if (ret == NULL) {
2581*7c478bd9Sstevel@tonic-gate 		return (e_alloc_fail(flag));
2582*7c478bd9Sstevel@tonic-gate 	}
2583*7c478bd9Sstevel@tonic-gate 
2584*7c478bd9Sstevel@tonic-gate 	(void) memset(ret, 0, size);
2585*7c478bd9Sstevel@tonic-gate 	return (ret);
2586*7c478bd9Sstevel@tonic-gate }
2587*7c478bd9Sstevel@tonic-gate 
2588*7c478bd9Sstevel@tonic-gate /*
2589*7c478bd9Sstevel@tonic-gate  * file_in:  Process an object from the archive.  If a TARTYP (TAR or USTAR)
2590*7c478bd9Sstevel@tonic-gate  * archive and g_nlink == 1, link this file to the file name in t_linkname
2591*7c478bd9Sstevel@tonic-gate  * and return.  Handle linked files in one of two ways.  If Onecopy == 0, this
2592*7c478bd9Sstevel@tonic-gate  * is an old style (binary or -c) archive, create and extract the data for the
2593*7c478bd9Sstevel@tonic-gate  * first link found, link all subsequent links to this file and skip their data.
2594*7c478bd9Sstevel@tonic-gate  * If Oncecopy == 1, save links until all have been processed, and then
2595*7c478bd9Sstevel@tonic-gate  * process the links first to last checking their names against the patterns
2596*7c478bd9Sstevel@tonic-gate  * and/or asking the user to rename them.  The first link that is accepted
2597*7c478bd9Sstevel@tonic-gate  * for xtraction is created and the data is read from the archive.
2598*7c478bd9Sstevel@tonic-gate  * All subsequent links that are accepted are linked to this file.
2599*7c478bd9Sstevel@tonic-gate  */
2600*7c478bd9Sstevel@tonic-gate 
2601*7c478bd9Sstevel@tonic-gate static void
2602*7c478bd9Sstevel@tonic-gate file_in(void)
2603*7c478bd9Sstevel@tonic-gate {
2604*7c478bd9Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
2605*7c478bd9Sstevel@tonic-gate 	int lnkem = 0, cleanup = 0;
2606*7c478bd9Sstevel@tonic-gate 	int proc_file;
2607*7c478bd9Sstevel@tonic-gate 	struct Lnk *ttl_p;
2608*7c478bd9Sstevel@tonic-gate 	int typeflag;
2609*7c478bd9Sstevel@tonic-gate 	char savacl;
2610*7c478bd9Sstevel@tonic-gate 	int cwd;
2611*7c478bd9Sstevel@tonic-gate 
2612*7c478bd9Sstevel@tonic-gate 	G_p = &Gen;
2613*7c478bd9Sstevel@tonic-gate 
2614*7c478bd9Sstevel@tonic-gate 	/*
2615*7c478bd9Sstevel@tonic-gate 	 * Open target directory if this isn't a skipped file
2616*7c478bd9Sstevel@tonic-gate 	 * and g_nlink == 1
2617*7c478bd9Sstevel@tonic-gate 	 *
2618*7c478bd9Sstevel@tonic-gate 	 * Links are handled further down in this function.
2619*7c478bd9Sstevel@tonic-gate 	 */
2620*7c478bd9Sstevel@tonic-gate 
2621*7c478bd9Sstevel@tonic-gate 	proc_file = ckname(0);
2622*7c478bd9Sstevel@tonic-gate 
2623*7c478bd9Sstevel@tonic-gate 	if (proc_file == F_SKIP && G_p->g_nlink == 1) {
2624*7c478bd9Sstevel@tonic-gate 		/*
2625*7c478bd9Sstevel@tonic-gate 		 * Normally ckname() prints out the file as a side
2626*7c478bd9Sstevel@tonic-gate 		 * effect except for table of contents listing
2627*7c478bd9Sstevel@tonic-gate 		 * when its parameter is zero and Onecopy isn't
2628*7c478bd9Sstevel@tonic-gate 		 * Zero.  Due to this we need to force the name
2629*7c478bd9Sstevel@tonic-gate 		 * to be printed here.
2630*7c478bd9Sstevel@tonic-gate 		 */
2631*7c478bd9Sstevel@tonic-gate 		if (Onecopy == 1) {
2632*7c478bd9Sstevel@tonic-gate 			VERBOSE((Args & OCt), G_p->g_nam_p);
2633*7c478bd9Sstevel@tonic-gate 		}
2634*7c478bd9Sstevel@tonic-gate 		data_in(P_SKIP);
2635*7c478bd9Sstevel@tonic-gate 		return;
2636*7c478bd9Sstevel@tonic-gate 	}
2637*7c478bd9Sstevel@tonic-gate 
2638*7c478bd9Sstevel@tonic-gate 	if (proc_file != F_SKIP && open_dirfd() != 0) {
2639*7c478bd9Sstevel@tonic-gate 		data_in(P_SKIP);
2640*7c478bd9Sstevel@tonic-gate 		return;
2641*7c478bd9Sstevel@tonic-gate 	}
2642*7c478bd9Sstevel@tonic-gate 
2643*7c478bd9Sstevel@tonic-gate 	if (Hdr_type == BAR) {
2644*7c478bd9Sstevel@tonic-gate 		bar_file_in();
2645*7c478bd9Sstevel@tonic-gate 		close_dirfd();
2646*7c478bd9Sstevel@tonic-gate 		return;
2647*7c478bd9Sstevel@tonic-gate 	}
2648*7c478bd9Sstevel@tonic-gate 
2649*7c478bd9Sstevel@tonic-gate 	/*
2650*7c478bd9Sstevel@tonic-gate 	 * For archives in USTAR format, the files are extracted according
2651*7c478bd9Sstevel@tonic-gate 	 * to the typeflag.
2652*7c478bd9Sstevel@tonic-gate 	 */
2653*7c478bd9Sstevel@tonic-gate 	if (Hdr_type == USTAR || Hdr_type == TAR) {
2654*7c478bd9Sstevel@tonic-gate 		typeflag = Thdr_p->tbuf.t_typeflag;
2655*7c478bd9Sstevel@tonic-gate 		if (G_p->g_nlink == 1) {		/* hard link */
2656*7c478bd9Sstevel@tonic-gate 			if (proc_file != F_SKIP) {
2657*7c478bd9Sstevel@tonic-gate 			    int i;
2658*7c478bd9Sstevel@tonic-gate 			    char lname[NAMSIZ+1];
2659*7c478bd9Sstevel@tonic-gate 			    (void) memset(lname, '\0', sizeof (lname));
2660*7c478bd9Sstevel@tonic-gate 
2661*7c478bd9Sstevel@tonic-gate 			    (void) strncpy(lname, Thdr_p->tbuf.t_linkname,
2662*7c478bd9Sstevel@tonic-gate 				NAMSIZ);
2663*7c478bd9Sstevel@tonic-gate 			    for (i = 0; i <= NAMSIZ && lname[i] != 0; i++)
2664*7c478bd9Sstevel@tonic-gate 				;
2665*7c478bd9Sstevel@tonic-gate 
2666*7c478bd9Sstevel@tonic-gate 			    lname[i] = 0;
2667*7c478bd9Sstevel@tonic-gate 			    (void) creat_lnk(G_p->g_dirfd,
2668*7c478bd9Sstevel@tonic-gate 				&lname[0], G_p->g_nam_p);
2669*7c478bd9Sstevel@tonic-gate 			}
2670*7c478bd9Sstevel@tonic-gate 			close_dirfd();
2671*7c478bd9Sstevel@tonic-gate 			return;
2672*7c478bd9Sstevel@tonic-gate 		}
2673*7c478bd9Sstevel@tonic-gate 		if (typeflag == '3' || typeflag == '4' || typeflag == '5' ||
2674*7c478bd9Sstevel@tonic-gate 		    typeflag == '6') {
2675*7c478bd9Sstevel@tonic-gate 			if (proc_file != F_SKIP &&
2676*7c478bd9Sstevel@tonic-gate 			    creat_spec(G_p->g_dirfd) > 0) {
2677*7c478bd9Sstevel@tonic-gate 				VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
2678*7c478bd9Sstevel@tonic-gate 			}
2679*7c478bd9Sstevel@tonic-gate 			close_dirfd();
2680*7c478bd9Sstevel@tonic-gate 			return;
2681*7c478bd9Sstevel@tonic-gate 		} else if (Adir || Aspec) {
2682*7c478bd9Sstevel@tonic-gate 			if ((proc_file == F_SKIP) ||
2683*7c478bd9Sstevel@tonic-gate 					(Ofile = openout(G_p->g_dirfd)) < 0) {
2684*7c478bd9Sstevel@tonic-gate 				data_in(P_SKIP);
2685*7c478bd9Sstevel@tonic-gate 			} else {
2686*7c478bd9Sstevel@tonic-gate 				data_in(P_PROC);
2687*7c478bd9Sstevel@tonic-gate 			}
2688*7c478bd9Sstevel@tonic-gate 			close_dirfd();
2689*7c478bd9Sstevel@tonic-gate 			return;
2690*7c478bd9Sstevel@tonic-gate 		}
2691*7c478bd9Sstevel@tonic-gate 	}
2692*7c478bd9Sstevel@tonic-gate 
2693*7c478bd9Sstevel@tonic-gate 	if (Adir) {
2694*7c478bd9Sstevel@tonic-gate 		if (proc_file != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
2695*7c478bd9Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
2696*7c478bd9Sstevel@tonic-gate 		}
2697*7c478bd9Sstevel@tonic-gate 		close_dirfd();
2698*7c478bd9Sstevel@tonic-gate 		if (Onecopy == 1) {
2699*7c478bd9Sstevel@tonic-gate 			VERBOSE((Args & OCt), G_p->g_nam_p);
2700*7c478bd9Sstevel@tonic-gate 		}
2701*7c478bd9Sstevel@tonic-gate 		return;
2702*7c478bd9Sstevel@tonic-gate 	}
2703*7c478bd9Sstevel@tonic-gate 	if (G_p->g_nlink == 1 || (Hdr_type == TAR ||
2704*7c478bd9Sstevel@tonic-gate 	    Hdr_type == USTAR)) {
2705*7c478bd9Sstevel@tonic-gate 		if (Aspec) {
2706*7c478bd9Sstevel@tonic-gate 			if (proc_file != F_SKIP && creat_spec(G_p->g_dirfd) > 0)
2707*7c478bd9Sstevel@tonic-gate 				VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
2708*7c478bd9Sstevel@tonic-gate 		} else {
2709*7c478bd9Sstevel@tonic-gate 			if ((proc_file == F_SKIP) ||
2710*7c478bd9Sstevel@tonic-gate 					(Ofile = openout(G_p->g_dirfd)) < 0) {
2711*7c478bd9Sstevel@tonic-gate 				data_in(P_SKIP);
2712*7c478bd9Sstevel@tonic-gate 			} else {
2713*7c478bd9Sstevel@tonic-gate 				data_in(P_PROC);
2714*7c478bd9Sstevel@tonic-gate 			}
2715*7c478bd9Sstevel@tonic-gate 		}
2716*7c478bd9Sstevel@tonic-gate 		close_dirfd();
2717*7c478bd9Sstevel@tonic-gate 		return;
2718*7c478bd9Sstevel@tonic-gate 	}
2719*7c478bd9Sstevel@tonic-gate 	close_dirfd();
2720*7c478bd9Sstevel@tonic-gate 
2721*7c478bd9Sstevel@tonic-gate 	tl_p = add_lnk(&ttl_p);
2722*7c478bd9Sstevel@tonic-gate 	l_p = ttl_p;
2723*7c478bd9Sstevel@tonic-gate 	if (l_p->L_cnt == l_p->L_gen.g_nlink)
2724*7c478bd9Sstevel@tonic-gate 		cleanup = 1;
2725*7c478bd9Sstevel@tonic-gate 	if (!Onecopy || G_p->g_attrnam_p != (char *)NULL) {
2726*7c478bd9Sstevel@tonic-gate 		lnkem = (tl_p != l_p) ? 1 : 0;
2727*7c478bd9Sstevel@tonic-gate 		G_p = &tl_p->L_gen;
2728*7c478bd9Sstevel@tonic-gate 		if (proc_file == F_SKIP) {
2729*7c478bd9Sstevel@tonic-gate 			data_in(P_SKIP);
2730*7c478bd9Sstevel@tonic-gate 		} else {
2731*7c478bd9Sstevel@tonic-gate 			if (open_dirfd() != 0)
2732*7c478bd9Sstevel@tonic-gate 				return;
2733*7c478bd9Sstevel@tonic-gate 			if (!lnkem) {
2734*7c478bd9Sstevel@tonic-gate 				if (Aspec) {
2735*7c478bd9Sstevel@tonic-gate 					if (creat_spec(G_p->g_dirfd) > 0)
2736*7c478bd9Sstevel@tonic-gate 						VERBOSE((Args & (OCv | OCV)),
2737*7c478bd9Sstevel@tonic-gate 						    G_p->g_nam_p);
2738*7c478bd9Sstevel@tonic-gate 				} else if ((Ofile =
2739*7c478bd9Sstevel@tonic-gate 						openout(G_p->g_dirfd)) < 0) {
2740*7c478bd9Sstevel@tonic-gate 					data_in(P_SKIP);
2741*7c478bd9Sstevel@tonic-gate 					close_dirfd();
2742*7c478bd9Sstevel@tonic-gate 					reclaim(l_p);
2743*7c478bd9Sstevel@tonic-gate 				} else {
2744*7c478bd9Sstevel@tonic-gate 					data_in(P_PROC);
2745*7c478bd9Sstevel@tonic-gate 					close_dirfd();
2746*7c478bd9Sstevel@tonic-gate 				}
2747*7c478bd9Sstevel@tonic-gate 			} else {
2748*7c478bd9Sstevel@tonic-gate 				/*
2749*7c478bd9Sstevel@tonic-gate 				 * Are we linking an attribute?
2750*7c478bd9Sstevel@tonic-gate 				 */
2751*7c478bd9Sstevel@tonic-gate 				cwd = -1;
2752*7c478bd9Sstevel@tonic-gate 				if (l_p->L_gen.g_attrnam_p != (char *)NULL) {
2753*7c478bd9Sstevel@tonic-gate 					(void) strcpy(Lnkend_p,
2754*7c478bd9Sstevel@tonic-gate 					    l_p->L_gen.g_attrnam_p);
2755*7c478bd9Sstevel@tonic-gate 					(void) strcpy(Full_p,
2756*7c478bd9Sstevel@tonic-gate 					    tl_p->L_gen.g_attrnam_p);
2757*7c478bd9Sstevel@tonic-gate 					cwd = save_cwd();
2758*7c478bd9Sstevel@tonic-gate 					(void) fchdir(G_p->g_dirfd);
2759*7c478bd9Sstevel@tonic-gate 				} else {
2760*7c478bd9Sstevel@tonic-gate 					(void) strcpy(Lnkend_p,
2761*7c478bd9Sstevel@tonic-gate 					    l_p->L_gen.g_nam_p);
2762*7c478bd9Sstevel@tonic-gate 					(void) strcpy(Full_p,
2763*7c478bd9Sstevel@tonic-gate 					    tl_p->L_gen.g_nam_p);
2764*7c478bd9Sstevel@tonic-gate 				}
2765*7c478bd9Sstevel@tonic-gate 				(void) creat_lnk(G_p->g_dirfd,
2766*7c478bd9Sstevel@tonic-gate 				    Lnkend_p, Full_p);
2767*7c478bd9Sstevel@tonic-gate 				data_in(P_SKIP);
2768*7c478bd9Sstevel@tonic-gate 				close_dirfd();
2769*7c478bd9Sstevel@tonic-gate 				l_p->L_lnk_p = (struct Lnk *)NULL;
2770*7c478bd9Sstevel@tonic-gate 				free(tl_p->L_gen.g_nam_p);
2771*7c478bd9Sstevel@tonic-gate 				free(tl_p);
2772*7c478bd9Sstevel@tonic-gate 				if (cwd != -1)
2773*7c478bd9Sstevel@tonic-gate 					rest_cwd(cwd);
2774*7c478bd9Sstevel@tonic-gate 			}
2775*7c478bd9Sstevel@tonic-gate 		}
2776*7c478bd9Sstevel@tonic-gate 	} else { /* Onecopy */
2777*7c478bd9Sstevel@tonic-gate 		if (tl_p->L_gen.g_filesz)
2778*7c478bd9Sstevel@tonic-gate 			cleanup = 1;
2779*7c478bd9Sstevel@tonic-gate 		if (!cleanup) {
2780*7c478bd9Sstevel@tonic-gate 			close_dirfd();
2781*7c478bd9Sstevel@tonic-gate 			return; /* don't do anything yet */
2782*7c478bd9Sstevel@tonic-gate 		}
2783*7c478bd9Sstevel@tonic-gate 		tl_p = l_p;
2784*7c478bd9Sstevel@tonic-gate 		/*
2785*7c478bd9Sstevel@tonic-gate 		 * ckname will clear aclchar. We need to keep aclchar for
2786*7c478bd9Sstevel@tonic-gate 		 * all links.
2787*7c478bd9Sstevel@tonic-gate 		 */
2788*7c478bd9Sstevel@tonic-gate 		savacl = aclchar;
2789*7c478bd9Sstevel@tonic-gate 		while (tl_p != (struct Lnk *)NULL) {
2790*7c478bd9Sstevel@tonic-gate 			G_p = &tl_p->L_gen;
2791*7c478bd9Sstevel@tonic-gate 			aclchar = savacl;
2792*7c478bd9Sstevel@tonic-gate 			if ((proc_file = ckname(1)) != F_SKIP) {
2793*7c478bd9Sstevel@tonic-gate 				if (open_dirfd() != 0) {
2794*7c478bd9Sstevel@tonic-gate 					return;
2795*7c478bd9Sstevel@tonic-gate 				}
2796*7c478bd9Sstevel@tonic-gate 				if (l_p->L_data) {
2797*7c478bd9Sstevel@tonic-gate 					(void) creat_lnk(G_p->g_dirfd,
2798*7c478bd9Sstevel@tonic-gate 					    l_p->L_gen.g_nam_p,
2799*7c478bd9Sstevel@tonic-gate 					    G_p->g_nam_p);
2800*7c478bd9Sstevel@tonic-gate 				} else if (Aspec) {
2801*7c478bd9Sstevel@tonic-gate 					(void) creat_spec(G_p->g_dirfd);
2802*7c478bd9Sstevel@tonic-gate 					l_p->L_data = 1;
2803*7c478bd9Sstevel@tonic-gate 					VERBOSE((Args & (OCv | OCV)),
2804*7c478bd9Sstevel@tonic-gate 					    G_p->g_nam_p);
2805*7c478bd9Sstevel@tonic-gate 				} else if ((Ofile =
2806*7c478bd9Sstevel@tonic-gate 				    openout(G_p->g_dirfd)) < 0) {
2807*7c478bd9Sstevel@tonic-gate 					proc_file = F_SKIP;
2808*7c478bd9Sstevel@tonic-gate 				} else {
2809*7c478bd9Sstevel@tonic-gate 					data_in(P_PROC);
2810*7c478bd9Sstevel@tonic-gate 					l_p->L_data = 1;
2811*7c478bd9Sstevel@tonic-gate 				}
2812*7c478bd9Sstevel@tonic-gate 			} /* (proc_file = ckname(1)) != F_SKIP */
2813*7c478bd9Sstevel@tonic-gate 
2814*7c478bd9Sstevel@tonic-gate 			tl_p = tl_p->L_lnk_p;
2815*7c478bd9Sstevel@tonic-gate 
2816*7c478bd9Sstevel@tonic-gate 			close_dirfd();
2817*7c478bd9Sstevel@tonic-gate 
2818*7c478bd9Sstevel@tonic-gate 			if (proc_file == F_SKIP && !cleanup) {
2819*7c478bd9Sstevel@tonic-gate 				tl_p->L_nxt_p = l_p->L_nxt_p;
2820*7c478bd9Sstevel@tonic-gate 				tl_p->L_bck_p = l_p->L_bck_p;
2821*7c478bd9Sstevel@tonic-gate 				l_p->L_bck_p->L_nxt_p = tl_p;
2822*7c478bd9Sstevel@tonic-gate 				l_p->L_nxt_p->L_bck_p = tl_p;
2823*7c478bd9Sstevel@tonic-gate 				free(l_p->L_gen.g_nam_p);
2824*7c478bd9Sstevel@tonic-gate 				free(l_p);
2825*7c478bd9Sstevel@tonic-gate 			}
2826*7c478bd9Sstevel@tonic-gate 		} /* tl_p->L_lnk_p != (struct Lnk *)NULL */
2827*7c478bd9Sstevel@tonic-gate 		if (l_p->L_data == 0) {
2828*7c478bd9Sstevel@tonic-gate 			data_in(P_SKIP);
2829*7c478bd9Sstevel@tonic-gate 		}
2830*7c478bd9Sstevel@tonic-gate 	}
2831*7c478bd9Sstevel@tonic-gate 	if (cleanup) {
2832*7c478bd9Sstevel@tonic-gate 		reclaim(l_p);
2833*7c478bd9Sstevel@tonic-gate 	}
2834*7c478bd9Sstevel@tonic-gate }
2835*7c478bd9Sstevel@tonic-gate 
2836*7c478bd9Sstevel@tonic-gate /*
2837*7c478bd9Sstevel@tonic-gate  * file_out:  If the current file is not a special file (!Aspec) and it
2838*7c478bd9Sstevel@tonic-gate  * is identical to the archive, skip it (do not archive the archive if it
2839*7c478bd9Sstevel@tonic-gate  * is a regular file).  If creating a TARTYP (TAR or USTAR) archive, the first
2840*7c478bd9Sstevel@tonic-gate  * time a link to a file is encountered, write the header and file out normally.
2841*7c478bd9Sstevel@tonic-gate  * Subsequent links to this file put this file name in their t_linkname field.
2842*7c478bd9Sstevel@tonic-gate  * Otherwise, links are handled in one of two ways, for the old headers
2843*7c478bd9Sstevel@tonic-gate  * (i.e. binary and -c), linked files are written out as they are encountered.
2844*7c478bd9Sstevel@tonic-gate  * For the new headers (ASC and CRC), links are saved up until all the links
2845*7c478bd9Sstevel@tonic-gate  * to each file are found.  For a file with n links, write n - 1 headers with
2846*7c478bd9Sstevel@tonic-gate  * g_filesz set to 0, write the final (nth) header with the correct g_filesz
2847*7c478bd9Sstevel@tonic-gate  * value and write the data for the file to the archive.
2848*7c478bd9Sstevel@tonic-gate  */
2849*7c478bd9Sstevel@tonic-gate 
2850*7c478bd9Sstevel@tonic-gate static
2851*7c478bd9Sstevel@tonic-gate int
2852*7c478bd9Sstevel@tonic-gate file_out(void)
2853*7c478bd9Sstevel@tonic-gate {
2854*7c478bd9Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
2855*7c478bd9Sstevel@tonic-gate 	int cleanup = 0;
2856*7c478bd9Sstevel@tonic-gate 	struct Lnk *ttl_p;
2857*7c478bd9Sstevel@tonic-gate 
2858*7c478bd9Sstevel@tonic-gate 	G_p = &Gen;
2859*7c478bd9Sstevel@tonic-gate 	if (!Aspec && IDENT(SrcSt, ArchSt))
2860*7c478bd9Sstevel@tonic-gate 		return (1); /* do not archive the archive if it's a reg file */
2861*7c478bd9Sstevel@tonic-gate 	if (G_p->g_filesz > Max_offset) {
2862*7c478bd9Sstevel@tonic-gate 		msg(ERR, "cpio: %s%s%s: too large to archive in current mode",
2863*7c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p,
2864*7c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
2865*7c478bd9Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
2866*7c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
2867*7c478bd9Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p);
2868*7c478bd9Sstevel@tonic-gate 		return (1); /* do not archive if it's too big */
2869*7c478bd9Sstevel@tonic-gate 	}
2870*7c478bd9Sstevel@tonic-gate 	if (Hdr_type == TAR || Hdr_type == USTAR) { /* TAR and USTAR */
2871*7c478bd9Sstevel@tonic-gate 		if (Adir) {
2872*7c478bd9Sstevel@tonic-gate 			if (Gen.g_attrnam_p != (char *)NULL) {
2873*7c478bd9Sstevel@tonic-gate 				write_xattr_hdr();
2874*7c478bd9Sstevel@tonic-gate 			}
2875*7c478bd9Sstevel@tonic-gate 			write_hdr(ARCHIVE_NORMAL, 0);
2876*7c478bd9Sstevel@tonic-gate 			return (0);
2877*7c478bd9Sstevel@tonic-gate 		}
2878*7c478bd9Sstevel@tonic-gate 		if (G_p->g_nlink == 1) {
2879*7c478bd9Sstevel@tonic-gate 			data_out();
2880*7c478bd9Sstevel@tonic-gate 			return (0);
2881*7c478bd9Sstevel@tonic-gate 		}
2882*7c478bd9Sstevel@tonic-gate 		tl_p = add_lnk(&ttl_p);
2883*7c478bd9Sstevel@tonic-gate 		l_p = ttl_p;
2884*7c478bd9Sstevel@tonic-gate 		if (tl_p == l_p) { /* first link to this file encountered */
2885*7c478bd9Sstevel@tonic-gate 			data_out();
2886*7c478bd9Sstevel@tonic-gate 			return (0);
2887*7c478bd9Sstevel@tonic-gate 		}
2888*7c478bd9Sstevel@tonic-gate 		(void) strncpy(T_lname, l_p->L_gen.g_nam_p,
2889*7c478bd9Sstevel@tonic-gate 		    l_p->L_gen.g_namesz);
2890*7c478bd9Sstevel@tonic-gate 
2891*7c478bd9Sstevel@tonic-gate 		/*
2892*7c478bd9Sstevel@tonic-gate 		 * check if linkname is greater than 100 characters
2893*7c478bd9Sstevel@tonic-gate 		 */
2894*7c478bd9Sstevel@tonic-gate 		if (strlen(T_lname) > NAMSIZ) {
2895*7c478bd9Sstevel@tonic-gate 			msg(EPOST, "cpio: %s: linkname %s is greater than %d",
2896*7c478bd9Sstevel@tonic-gate 			    G_p->g_nam_p, T_lname, NAMSIZ);
2897*7c478bd9Sstevel@tonic-gate 			return (1);
2898*7c478bd9Sstevel@tonic-gate 		}
2899*7c478bd9Sstevel@tonic-gate 
2900*7c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
2901*7c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), tl_p->L_gen.g_nam_p);
2902*7c478bd9Sstevel@tonic-gate 
2903*7c478bd9Sstevel@tonic-gate 		/* find the lnk entry in sublist, unlink it, and free it */
2904*7c478bd9Sstevel@tonic-gate 		for (; ttl_p->L_lnk_p != NULL;
2905*7c478bd9Sstevel@tonic-gate 		    ttl_p = ttl_p->L_lnk_p) {
2906*7c478bd9Sstevel@tonic-gate 			if (ttl_p->L_lnk_p == tl_p) {
2907*7c478bd9Sstevel@tonic-gate 				ttl_p->L_lnk_p = tl_p->L_lnk_p;
2908*7c478bd9Sstevel@tonic-gate 				free(tl_p->L_gen.g_nam_p);
2909*7c478bd9Sstevel@tonic-gate 				free(tl_p);
2910*7c478bd9Sstevel@tonic-gate 				break;
2911*7c478bd9Sstevel@tonic-gate 			}
2912*7c478bd9Sstevel@tonic-gate 		}
2913*7c478bd9Sstevel@tonic-gate 
2914*7c478bd9Sstevel@tonic-gate 		return (0);
2915*7c478bd9Sstevel@tonic-gate 	}
2916*7c478bd9Sstevel@tonic-gate 	if (Adir) {
2917*7c478bd9Sstevel@tonic-gate 		/*
2918*7c478bd9Sstevel@tonic-gate 		 * ACL has been retrieved in getname().
2919*7c478bd9Sstevel@tonic-gate 		 */
2920*7c478bd9Sstevel@tonic-gate 		if (Pflag) {
2921*7c478bd9Sstevel@tonic-gate 			char    *secinfo = NULL;
2922*7c478bd9Sstevel@tonic-gate 			int	len = 0;
2923*7c478bd9Sstevel@tonic-gate 
2924*7c478bd9Sstevel@tonic-gate 			/* append security attributes */
2925*7c478bd9Sstevel@tonic-gate 			if ((append_secattr(&secinfo, &len, aclcnt,
2926*7c478bd9Sstevel@tonic-gate 			    (char *)aclp, UFSD_ACL)) == -1)
2927*7c478bd9Sstevel@tonic-gate 				msg(ERR, "can create security information");
2928*7c478bd9Sstevel@tonic-gate 
2929*7c478bd9Sstevel@tonic-gate 			/* call append_secattr() if more than one */
2930*7c478bd9Sstevel@tonic-gate 
2931*7c478bd9Sstevel@tonic-gate 			if (len > 0) {
2932*7c478bd9Sstevel@tonic-gate 			/* write ancillary */
2933*7c478bd9Sstevel@tonic-gate 				(void) write_hdr(ARCHIVE_ACL, (off_t)len);
2934*7c478bd9Sstevel@tonic-gate 				(void) write_ancillary(secinfo, len);
2935*7c478bd9Sstevel@tonic-gate 			}
2936*7c478bd9Sstevel@tonic-gate 		}
2937*7c478bd9Sstevel@tonic-gate 
2938*7c478bd9Sstevel@tonic-gate 		if (Gen.g_attrnam_p != (char *)NULL) {
2939*7c478bd9Sstevel@tonic-gate 			write_xattr_hdr();
2940*7c478bd9Sstevel@tonic-gate 		}
2941*7c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
2942*7c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
2943*7c478bd9Sstevel@tonic-gate 		return (0);
2944*7c478bd9Sstevel@tonic-gate 	}
2945*7c478bd9Sstevel@tonic-gate 	if (G_p->g_nlink == 1) {
2946*7c478bd9Sstevel@tonic-gate 		data_out();
2947*7c478bd9Sstevel@tonic-gate 		return (0);
2948*7c478bd9Sstevel@tonic-gate 	} else {
2949*7c478bd9Sstevel@tonic-gate 		tl_p = add_lnk(&ttl_p);
2950*7c478bd9Sstevel@tonic-gate 		l_p = ttl_p;
2951*7c478bd9Sstevel@tonic-gate 
2952*7c478bd9Sstevel@tonic-gate 		if (l_p->L_cnt == l_p->L_gen.g_nlink)
2953*7c478bd9Sstevel@tonic-gate 			cleanup = 1;
2954*7c478bd9Sstevel@tonic-gate 		else if (Onecopy && G_p->g_attrnam_p == (char *)NULL) {
2955*7c478bd9Sstevel@tonic-gate 			return (0); /* don't process data yet */
2956*7c478bd9Sstevel@tonic-gate 		}
2957*7c478bd9Sstevel@tonic-gate 	}
2958*7c478bd9Sstevel@tonic-gate 	if (Onecopy && G_p->g_attrnam_p == (char *)NULL) {
2959*7c478bd9Sstevel@tonic-gate 		tl_p = l_p;
2960*7c478bd9Sstevel@tonic-gate 		while (tl_p->L_lnk_p != (struct Lnk *)NULL) {
2961*7c478bd9Sstevel@tonic-gate 			G_p = &tl_p->L_gen;
2962*7c478bd9Sstevel@tonic-gate 			G_p->g_filesz = (off_t)0;
2963*7c478bd9Sstevel@tonic-gate 			/* one link with the acl is sufficient */
2964*7c478bd9Sstevel@tonic-gate 			write_hdr(ARCHIVE_NORMAL, (off_t)0);
2965*7c478bd9Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
2966*7c478bd9Sstevel@tonic-gate 			tl_p = tl_p->L_lnk_p;
2967*7c478bd9Sstevel@tonic-gate 		}
2968*7c478bd9Sstevel@tonic-gate 		G_p = &tl_p->L_gen;
2969*7c478bd9Sstevel@tonic-gate 		if (open_dirfd() != 0)
2970*7c478bd9Sstevel@tonic-gate 			return (1);
2971*7c478bd9Sstevel@tonic-gate 	}
2972*7c478bd9Sstevel@tonic-gate 	/* old style: has acl and data for every link */
2973*7c478bd9Sstevel@tonic-gate 	data_out();
2974*7c478bd9Sstevel@tonic-gate 	if (cleanup)
2975*7c478bd9Sstevel@tonic-gate 		reclaim(l_p);
2976*7c478bd9Sstevel@tonic-gate 	return (0);
2977*7c478bd9Sstevel@tonic-gate }
2978*7c478bd9Sstevel@tonic-gate 
2979*7c478bd9Sstevel@tonic-gate /*
2980*7c478bd9Sstevel@tonic-gate  * file_pass:  If the -l option is set (link files when possible), and the
2981*7c478bd9Sstevel@tonic-gate  * source and destination file systems are the same, link the source file
2982*7c478bd9Sstevel@tonic-gate  * (G_p->g_nam_p) to the destination file (Fullnam) and return.  If not a
2983*7c478bd9Sstevel@tonic-gate  * linked file, transfer the data.  Otherwise, the first link to a file
2984*7c478bd9Sstevel@tonic-gate  * encountered is transferred normally and subsequent links are linked to it.
2985*7c478bd9Sstevel@tonic-gate  */
2986*7c478bd9Sstevel@tonic-gate 
2987*7c478bd9Sstevel@tonic-gate static int
2988*7c478bd9Sstevel@tonic-gate file_pass(void)
2989*7c478bd9Sstevel@tonic-gate {
2990*7c478bd9Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
2991*7c478bd9Sstevel@tonic-gate 	struct Lnk *ttl_p;
2992*7c478bd9Sstevel@tonic-gate 	char *save_name;
2993*7c478bd9Sstevel@tonic-gate 	int size;
2994*7c478bd9Sstevel@tonic-gate 	int cwd;
2995*7c478bd9Sstevel@tonic-gate 	char *lfrom, *lto;
2996*7c478bd9Sstevel@tonic-gate 
2997*7c478bd9Sstevel@tonic-gate 	G_p = &Gen;
2998*7c478bd9Sstevel@tonic-gate 
2999*7c478bd9Sstevel@tonic-gate 	if (Adir && !(Args & OCd)) {
3000*7c478bd9Sstevel@tonic-gate 		msg(ERR, "Use -d option to copy \"%s\"", G_p->g_nam_p);
3001*7c478bd9Sstevel@tonic-gate 		return (FILE_PASS_ERR);
3002*7c478bd9Sstevel@tonic-gate 	}
3003*7c478bd9Sstevel@tonic-gate 
3004*7c478bd9Sstevel@tonic-gate 	save_name = G_p->g_nam_p;
3005*7c478bd9Sstevel@tonic-gate 
3006*7c478bd9Sstevel@tonic-gate 	while (*(G_p->g_nam_p) == '/') {
3007*7c478bd9Sstevel@tonic-gate 		G_p->g_nam_p++;
3008*7c478bd9Sstevel@tonic-gate 	}
3009*7c478bd9Sstevel@tonic-gate 
3010*7c478bd9Sstevel@tonic-gate 	(void) strcpy(Full_p,
3011*7c478bd9Sstevel@tonic-gate 	    (G_p->g_attrfnam_p == (char *)NULL) ?
3012*7c478bd9Sstevel@tonic-gate 	    G_p->g_nam_p : G_p->g_attrfnam_p);
3013*7c478bd9Sstevel@tonic-gate 
3014*7c478bd9Sstevel@tonic-gate 	if (G_p->g_attrnam_p == (char *)NULL) {
3015*7c478bd9Sstevel@tonic-gate 		G_p->g_passdirfd = open_dir(Fullnam_p);
3016*7c478bd9Sstevel@tonic-gate 
3017*7c478bd9Sstevel@tonic-gate 		if (G_p->g_passdirfd == -1) {
3018*7c478bd9Sstevel@tonic-gate 			msg(ERRN,
3019*7c478bd9Sstevel@tonic-gate 			    "Cannot open/create \"%s\"", Fullnam_p);
3020*7c478bd9Sstevel@tonic-gate 			return (FILE_PASS_ERR);
3021*7c478bd9Sstevel@tonic-gate 		}
3022*7c478bd9Sstevel@tonic-gate 	} else {
3023*7c478bd9Sstevel@tonic-gate 		G_p->g_passdirfd = attropen(Fullnam_p, ".", O_RDONLY);
3024*7c478bd9Sstevel@tonic-gate 
3025*7c478bd9Sstevel@tonic-gate 		if (G_p->g_passdirfd == -1) {
3026*7c478bd9Sstevel@tonic-gate 			G_p->g_passdirfd = retry_attrdir_open(Fullnam_p);
3027*7c478bd9Sstevel@tonic-gate 
3028*7c478bd9Sstevel@tonic-gate 			if (G_p->g_passdirfd == -1) {
3029*7c478bd9Sstevel@tonic-gate 				msg(ERRN,
3030*7c478bd9Sstevel@tonic-gate 				    "Cannot open attribute directory of"
3031*7c478bd9Sstevel@tonic-gate 				    " file \"%s\"", Fullnam_p);
3032*7c478bd9Sstevel@tonic-gate 				return (FILE_PASS_ERR);
3033*7c478bd9Sstevel@tonic-gate 			}
3034*7c478bd9Sstevel@tonic-gate 		}
3035*7c478bd9Sstevel@tonic-gate 	}
3036*7c478bd9Sstevel@tonic-gate 
3037*7c478bd9Sstevel@tonic-gate 	if (Args & OCl) {
3038*7c478bd9Sstevel@tonic-gate 		/* We are linking back to the source directory. */
3039*7c478bd9Sstevel@tonic-gate 
3040*7c478bd9Sstevel@tonic-gate 		if (!Adir) {
3041*7c478bd9Sstevel@tonic-gate 			char *existingfile = save_name;
3042*7c478bd9Sstevel@tonic-gate 
3043*7c478bd9Sstevel@tonic-gate 			if ((Args & OCL) && issymlink) {
3044*7c478bd9Sstevel@tonic-gate 				/* We are chasing symlinks. */
3045*7c478bd9Sstevel@tonic-gate 
3046*7c478bd9Sstevel@tonic-gate 				if ((size = readlink(save_name, Symlnk_p,
3047*7c478bd9Sstevel@tonic-gate 				    MAXPATHLEN)) < 0) {
3048*7c478bd9Sstevel@tonic-gate 					msg(ERRN,
3049*7c478bd9Sstevel@tonic-gate 					    "Cannot read symbolic link \"%s\"",
3050*7c478bd9Sstevel@tonic-gate 					    save_name);
3051*7c478bd9Sstevel@tonic-gate 					return (FILE_PASS_ERR);
3052*7c478bd9Sstevel@tonic-gate 				}
3053*7c478bd9Sstevel@tonic-gate 
3054*7c478bd9Sstevel@tonic-gate 				Symlnk_p[size] = '\0';
3055*7c478bd9Sstevel@tonic-gate 				existingfile = Symlnk_p;
3056*7c478bd9Sstevel@tonic-gate 			}
3057*7c478bd9Sstevel@tonic-gate 
3058*7c478bd9Sstevel@tonic-gate 			if (G_p->g_attrnam_p == (char *)NULL) {
3059*7c478bd9Sstevel@tonic-gate 				if (creat_lnk(G_p->g_passdirfd,
3060*7c478bd9Sstevel@tonic-gate 				    existingfile, Fullnam_p) == 0) {
3061*7c478bd9Sstevel@tonic-gate 					return (FILE_LINKED);
3062*7c478bd9Sstevel@tonic-gate 				}
3063*7c478bd9Sstevel@tonic-gate 			}
3064*7c478bd9Sstevel@tonic-gate 		}
3065*7c478bd9Sstevel@tonic-gate 	}
3066*7c478bd9Sstevel@tonic-gate 
3067*7c478bd9Sstevel@tonic-gate 	if ((G_p->g_mode & Ftype) == S_IFLNK && !(Args & OCL)) {
3068*7c478bd9Sstevel@tonic-gate 		/* The archive file is a symlink. */
3069*7c478bd9Sstevel@tonic-gate 
3070*7c478bd9Sstevel@tonic-gate 		errno = 0;
3071*7c478bd9Sstevel@tonic-gate 
3072*7c478bd9Sstevel@tonic-gate 		if ((size = readlink(save_name, Symlnk_p, MAXPATHLEN)) < 0) {
3073*7c478bd9Sstevel@tonic-gate 			msg(ERRN,
3074*7c478bd9Sstevel@tonic-gate 			    "Cannot read symbolic link \"%s\"", save_name);
3075*7c478bd9Sstevel@tonic-gate 			return (FILE_PASS_ERR);
3076*7c478bd9Sstevel@tonic-gate 		}
3077*7c478bd9Sstevel@tonic-gate 
3078*7c478bd9Sstevel@tonic-gate 		errno = 0;
3079*7c478bd9Sstevel@tonic-gate 		(void) missdir(Fullnam_p);
3080*7c478bd9Sstevel@tonic-gate 		*(Symlnk_p + size) = '\0';
3081*7c478bd9Sstevel@tonic-gate 
3082*7c478bd9Sstevel@tonic-gate 		if (symlink(Symlnk_p, Fullnam_p) < 0) {
3083*7c478bd9Sstevel@tonic-gate 			if (errno == EEXIST) {
3084*7c478bd9Sstevel@tonic-gate 				if (openout(G_p->g_passdirfd) < 0) {
3085*7c478bd9Sstevel@tonic-gate 					return (FILE_PASS_ERR);
3086*7c478bd9Sstevel@tonic-gate 				}
3087*7c478bd9Sstevel@tonic-gate 			} else {
3088*7c478bd9Sstevel@tonic-gate 				msg(ERRN, "Cannot create \"%s\"", Fullnam_p);
3089*7c478bd9Sstevel@tonic-gate 				return (FILE_PASS_ERR);
3090*7c478bd9Sstevel@tonic-gate 			}
3091*7c478bd9Sstevel@tonic-gate 		}
3092*7c478bd9Sstevel@tonic-gate 
3093*7c478bd9Sstevel@tonic-gate 		if (lchown(Fullnam_p, (int)G_p->g_uid, (int)G_p->g_gid) < 0) {
3094*7c478bd9Sstevel@tonic-gate 			if ((Euid == 0) || (Args & OCR)) {
3095*7c478bd9Sstevel@tonic-gate 				msg(ERRN,
3096*7c478bd9Sstevel@tonic-gate 				    "Error during chown() of \"%s\"",
3097*7c478bd9Sstevel@tonic-gate 				    Fullnam_p);
3098*7c478bd9Sstevel@tonic-gate 			}
3099*7c478bd9Sstevel@tonic-gate 		}
3100*7c478bd9Sstevel@tonic-gate 
3101*7c478bd9Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), Fullnam_p);
3102*7c478bd9Sstevel@tonic-gate 		return (FILE_PASS_ERR);
3103*7c478bd9Sstevel@tonic-gate 	}
3104*7c478bd9Sstevel@tonic-gate 
3105*7c478bd9Sstevel@tonic-gate 	if (!Adir && G_p->g_nlink > 1) {
3106*7c478bd9Sstevel@tonic-gate 		/* The archive file has hard links. */
3107*7c478bd9Sstevel@tonic-gate 
3108*7c478bd9Sstevel@tonic-gate 		tl_p = add_lnk(&ttl_p);
3109*7c478bd9Sstevel@tonic-gate 		l_p = ttl_p;
3110*7c478bd9Sstevel@tonic-gate 
3111*7c478bd9Sstevel@tonic-gate 		if (tl_p == l_p) {
3112*7c478bd9Sstevel@tonic-gate 			/* The archive file was not found. */
3113*7c478bd9Sstevel@tonic-gate 
3114*7c478bd9Sstevel@tonic-gate 			G_p = &tl_p->L_gen;
3115*7c478bd9Sstevel@tonic-gate 		} else {
3116*7c478bd9Sstevel@tonic-gate 			/* The archive file was found. */
3117*7c478bd9Sstevel@tonic-gate 
3118*7c478bd9Sstevel@tonic-gate 			cwd = -1;
3119*7c478bd9Sstevel@tonic-gate 
3120*7c478bd9Sstevel@tonic-gate 			if (l_p->L_gen.g_attrnam_p != (char *)NULL) {
3121*7c478bd9Sstevel@tonic-gate 				/* We are linking an attribute */
3122*7c478bd9Sstevel@tonic-gate 
3123*7c478bd9Sstevel@tonic-gate 				(void) strcpy(Lnkend_p, l_p->L_gen.g_attrnam_p);
3124*7c478bd9Sstevel@tonic-gate 				cwd = save_cwd();
3125*7c478bd9Sstevel@tonic-gate 				(void) fchdir(G_p->g_passdirfd);
3126*7c478bd9Sstevel@tonic-gate 				lfrom = get_component(Lnknam_p);
3127*7c478bd9Sstevel@tonic-gate 				lto = tl_p->L_gen.g_attrnam_p;
3128*7c478bd9Sstevel@tonic-gate 			} else {
3129*7c478bd9Sstevel@tonic-gate 				/* We are not linking an attribute */
3130*7c478bd9Sstevel@tonic-gate 
3131*7c478bd9Sstevel@tonic-gate 				(void) strcpy(Lnkend_p, l_p->L_gen.g_nam_p);
3132*7c478bd9Sstevel@tonic-gate 				(void) strcpy(Full_p, tl_p->L_gen.g_nam_p);
3133*7c478bd9Sstevel@tonic-gate 				lfrom = Lnknam_p;
3134*7c478bd9Sstevel@tonic-gate 				lto = Fullnam_p;
3135*7c478bd9Sstevel@tonic-gate 			}
3136*7c478bd9Sstevel@tonic-gate 
3137*7c478bd9Sstevel@tonic-gate 			(void) creat_lnk(G_p->g_passdirfd, lfrom, lto);
3138*7c478bd9Sstevel@tonic-gate 
3139*7c478bd9Sstevel@tonic-gate 			if (cwd) {
3140*7c478bd9Sstevel@tonic-gate 				rest_cwd(cwd);
3141*7c478bd9Sstevel@tonic-gate 			}
3142*7c478bd9Sstevel@tonic-gate 
3143*7c478bd9Sstevel@tonic-gate 			l_p->L_lnk_p = (struct Lnk *)NULL;
3144*7c478bd9Sstevel@tonic-gate 			free(tl_p->L_gen.g_nam_p);
3145*7c478bd9Sstevel@tonic-gate 			free(tl_p);
3146*7c478bd9Sstevel@tonic-gate 
3147*7c478bd9Sstevel@tonic-gate 			if (l_p->L_cnt == G_p->g_nlink) {
3148*7c478bd9Sstevel@tonic-gate 				reclaim(l_p);
3149*7c478bd9Sstevel@tonic-gate 			}
3150*7c478bd9Sstevel@tonic-gate 
3151*7c478bd9Sstevel@tonic-gate 			return (FILE_LINKED);
3152*7c478bd9Sstevel@tonic-gate 		}
3153*7c478bd9Sstevel@tonic-gate 	}
3154*7c478bd9Sstevel@tonic-gate 
3155*7c478bd9Sstevel@tonic-gate 	if (Adir || Aspec) {
3156*7c478bd9Sstevel@tonic-gate 		/*
3157*7c478bd9Sstevel@tonic-gate 		 * The archive file is a directory,  block special, char
3158*7c478bd9Sstevel@tonic-gate 		 * special or a fifo.
3159*7c478bd9Sstevel@tonic-gate 		 */
3160*7c478bd9Sstevel@tonic-gate 
3161*7c478bd9Sstevel@tonic-gate 		if (creat_spec(G_p->g_passdirfd) > 0) {
3162*7c478bd9Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), Fullnam_p);
3163*7c478bd9Sstevel@tonic-gate 		}
3164*7c478bd9Sstevel@tonic-gate 	} else if ((Ofile = openout(G_p->g_passdirfd)) > 0) {
3165*7c478bd9Sstevel@tonic-gate 		data_pass();
3166*7c478bd9Sstevel@tonic-gate 	}
3167*7c478bd9Sstevel@tonic-gate 
3168*7c478bd9Sstevel@tonic-gate 	return (FILE_COPIED);
3169*7c478bd9Sstevel@tonic-gate }
3170*7c478bd9Sstevel@tonic-gate 
3171*7c478bd9Sstevel@tonic-gate /*
3172*7c478bd9Sstevel@tonic-gate  * flush_lnks: With new linked file handling, linked files are not archived
3173*7c478bd9Sstevel@tonic-gate  * until all links have been collected.  When the end of the list of filenames
3174*7c478bd9Sstevel@tonic-gate  * to archive has been reached, all files that did not encounter all their links
3175*7c478bd9Sstevel@tonic-gate  * are written out with actual (encountered) link counts.  A file with n links
3176*7c478bd9Sstevel@tonic-gate  * (that are archived) will be represented by n headers (one for each link (the
3177*7c478bd9Sstevel@tonic-gate  * first n - 1 have g_filesz set to 0)) followed by the data for the file.
3178*7c478bd9Sstevel@tonic-gate  */
3179*7c478bd9Sstevel@tonic-gate 
3180*7c478bd9Sstevel@tonic-gate static void
3181*7c478bd9Sstevel@tonic-gate flush_lnks(void)
3182*7c478bd9Sstevel@tonic-gate {
3183*7c478bd9Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
3184*7c478bd9Sstevel@tonic-gate 	off_t tfsize;
3185*7c478bd9Sstevel@tonic-gate 
3186*7c478bd9Sstevel@tonic-gate 	l_p = Lnk_hd.L_nxt_p;
3187*7c478bd9Sstevel@tonic-gate 	while (l_p != &Lnk_hd) {
3188*7c478bd9Sstevel@tonic-gate 		(void) strcpy(Gen.g_nam_p, l_p->L_gen.g_nam_p);
3189*7c478bd9Sstevel@tonic-gate 		if (stat(Gen.g_nam_p, &SrcSt) == 0) { /* check if file exists */
3190*7c478bd9Sstevel@tonic-gate 			tl_p = l_p;
3191*7c478bd9Sstevel@tonic-gate 			(void) creat_hdr();
3192*7c478bd9Sstevel@tonic-gate 			Gen.g_nlink = l_p->L_cnt; /* "actual" link count */
3193*7c478bd9Sstevel@tonic-gate 			tfsize = Gen.g_filesz;
3194*7c478bd9Sstevel@tonic-gate 			Gen.g_filesz = (off_t)0;
3195*7c478bd9Sstevel@tonic-gate 			G_p = &Gen;
3196*7c478bd9Sstevel@tonic-gate 			while (tl_p != (struct Lnk *)NULL) {
3197*7c478bd9Sstevel@tonic-gate 				Gen.g_nam_p = tl_p->L_gen.g_nam_p;
3198*7c478bd9Sstevel@tonic-gate 				Gen.g_namesz = tl_p->L_gen.g_namesz;
3199*7c478bd9Sstevel@tonic-gate 				if (tl_p->L_lnk_p == (struct Lnk *)NULL) {
3200*7c478bd9Sstevel@tonic-gate 					Gen.g_filesz = tfsize;
3201*7c478bd9Sstevel@tonic-gate 					if (open_dirfd() != 0) {
3202*7c478bd9Sstevel@tonic-gate 						break;
3203*7c478bd9Sstevel@tonic-gate 					}
3204*7c478bd9Sstevel@tonic-gate 					data_out();
3205*7c478bd9Sstevel@tonic-gate 					break;
3206*7c478bd9Sstevel@tonic-gate 				}
3207*7c478bd9Sstevel@tonic-gate 				write_hdr(ARCHIVE_NORMAL,
3208*7c478bd9Sstevel@tonic-gate 				    (off_t)0); /* header only */
3209*7c478bd9Sstevel@tonic-gate 				VERBOSE((Args & (OCv | OCV)), Gen.g_nam_p);
3210*7c478bd9Sstevel@tonic-gate 				tl_p = tl_p->L_lnk_p;
3211*7c478bd9Sstevel@tonic-gate 			}
3212*7c478bd9Sstevel@tonic-gate 			Gen.g_nam_p = Nam_p;
3213*7c478bd9Sstevel@tonic-gate 		} else /* stat(Gen.g_nam_p, &SrcSt) == 0 */
3214*7c478bd9Sstevel@tonic-gate 			msg(ERR, "\"%s%s%s\" has disappeared",
3215*7c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
3216*7c478bd9Sstevel@tonic-gate 			    Gen.g_nam_p : Gen.g_attrfnam_p,
3217*7c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
3218*7c478bd9Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
3219*7c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
3220*7c478bd9Sstevel@tonic-gate 			    "" : Gen.g_attrnam_p);
3221*7c478bd9Sstevel@tonic-gate 		tl_p = l_p;
3222*7c478bd9Sstevel@tonic-gate 		l_p = l_p->L_nxt_p;
3223*7c478bd9Sstevel@tonic-gate 		reclaim(tl_p);
3224*7c478bd9Sstevel@tonic-gate 	} /* l_p != &Lnk_hd */
3225*7c478bd9Sstevel@tonic-gate }
3226*7c478bd9Sstevel@tonic-gate 
3227*7c478bd9Sstevel@tonic-gate /*
3228*7c478bd9Sstevel@tonic-gate  * gethdr: Get a header from the archive, validate it and check for the trailer.
3229*7c478bd9Sstevel@tonic-gate  * Any user specified Hdr_type is ignored (set to NONE in main).  Hdr_type is
3230*7c478bd9Sstevel@tonic-gate  * set appropriately after a valid header is found.  Unless the -k option is
3231*7c478bd9Sstevel@tonic-gate  * set a corrupted header causes an exit with an error.  I/O errors during
3232*7c478bd9Sstevel@tonic-gate  * examination of any part of the header cause gethdr to throw away any current
3233*7c478bd9Sstevel@tonic-gate  * data and start over.  Other errors during examination of any part of the
3234*7c478bd9Sstevel@tonic-gate  * header cause gethdr to advance one byte and continue the examination.
3235*7c478bd9Sstevel@tonic-gate  */
3236*7c478bd9Sstevel@tonic-gate 
3237*7c478bd9Sstevel@tonic-gate static int
3238*7c478bd9Sstevel@tonic-gate gethdr(void)
3239*7c478bd9Sstevel@tonic-gate {
3240*7c478bd9Sstevel@tonic-gate 	ushort_t ftype;
3241*7c478bd9Sstevel@tonic-gate 	int hit = NONE, cnt = 0;
3242*7c478bd9Sstevel@tonic-gate 	int goodhdr, hsize, offset;
3243*7c478bd9Sstevel@tonic-gate 	int bswap = 0;
3244*7c478bd9Sstevel@tonic-gate 	char *preptr;
3245*7c478bd9Sstevel@tonic-gate 	int k = 0;
3246*7c478bd9Sstevel@tonic-gate 	int j;
3247*7c478bd9Sstevel@tonic-gate 
3248*7c478bd9Sstevel@tonic-gate 	Gen.g_nam_p = Nam_p;
3249*7c478bd9Sstevel@tonic-gate 	do { /* hit == NONE && (Args & OCk) && Buffr.b_cnt > 0 */
3250*7c478bd9Sstevel@tonic-gate 		FILL(Hdrsz);
3251*7c478bd9Sstevel@tonic-gate 		switch (Hdr_type) {
3252*7c478bd9Sstevel@tonic-gate 		case NONE:
3253*7c478bd9Sstevel@tonic-gate 		case BIN:
3254*7c478bd9Sstevel@tonic-gate 			Binmag.b_byte[0] = Buffr.b_out_p[0];
3255*7c478bd9Sstevel@tonic-gate 			Binmag.b_byte[1] = Buffr.b_out_p[1];
3256*7c478bd9Sstevel@tonic-gate 			if ((Binmag.b_half == CMN_BIN) ||
3257*7c478bd9Sstevel@tonic-gate 			    (Binmag.b_half == CMN_BBS)) {
3258*7c478bd9Sstevel@tonic-gate 				hit = read_hdr(BIN);
3259*7c478bd9Sstevel@tonic-gate 				if (Hdr_type == NONE)
3260*7c478bd9Sstevel@tonic-gate 					bswap = 1;
3261*7c478bd9Sstevel@tonic-gate 				hsize = HDRSZ + Gen.g_namesz;
3262*7c478bd9Sstevel@tonic-gate 				break;
3263*7c478bd9Sstevel@tonic-gate 			}
3264*7c478bd9Sstevel@tonic-gate 			if (Hdr_type != NONE)
3265*7c478bd9Sstevel@tonic-gate 				break;
3266*7c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
3267*7c478bd9Sstevel@tonic-gate 		case CHR:
3268*7c478bd9Sstevel@tonic-gate 			if (!(strncmp(Buffr.b_out_p, CMS_CHR, CMS_LEN))) {
3269*7c478bd9Sstevel@tonic-gate 				hit = read_hdr(CHR);
3270*7c478bd9Sstevel@tonic-gate 				hsize = CHRSZ + Gen.g_namesz;
3271*7c478bd9Sstevel@tonic-gate 				break;
3272*7c478bd9Sstevel@tonic-gate 			}
3273*7c478bd9Sstevel@tonic-gate 			if (Hdr_type != NONE)
3274*7c478bd9Sstevel@tonic-gate 				break;
3275*7c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
3276*7c478bd9Sstevel@tonic-gate 		case ASC:
3277*7c478bd9Sstevel@tonic-gate 			if (!(strncmp(Buffr.b_out_p, CMS_ASC, CMS_LEN))) {
3278*7c478bd9Sstevel@tonic-gate 				hit = read_hdr(ASC);
3279*7c478bd9Sstevel@tonic-gate 				hsize = ASCSZ + Gen.g_namesz;
3280*7c478bd9Sstevel@tonic-gate 				Max_namesz = APATH;
3281*7c478bd9Sstevel@tonic-gate 				break;
3282*7c478bd9Sstevel@tonic-gate 			}
3283*7c478bd9Sstevel@tonic-gate 			if (Hdr_type != NONE)
3284*7c478bd9Sstevel@tonic-gate 				break;
3285*7c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
3286*7c478bd9Sstevel@tonic-gate 		case CRC:
3287*7c478bd9Sstevel@tonic-gate 			if (!(strncmp(Buffr.b_out_p, CMS_CRC, CMS_LEN))) {
3288*7c478bd9Sstevel@tonic-gate 				hit = read_hdr(CRC);
3289*7c478bd9Sstevel@tonic-gate 				hsize = ASCSZ + Gen.g_namesz;
3290*7c478bd9Sstevel@tonic-gate 				Max_namesz = APATH;
3291*7c478bd9Sstevel@tonic-gate 				break;
3292*7c478bd9Sstevel@tonic-gate 			}
3293*7c478bd9Sstevel@tonic-gate 			if (Hdr_type != NONE)
3294*7c478bd9Sstevel@tonic-gate 				break;
3295*7c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
3296*7c478bd9Sstevel@tonic-gate 
3297*7c478bd9Sstevel@tonic-gate 		case BAR:
3298*7c478bd9Sstevel@tonic-gate 			if (Hdr_p != NULL && strcmp(Hdr_p, "bar") == 0) {
3299*7c478bd9Sstevel@tonic-gate 				Hdrsz = BARSZ;
3300*7c478bd9Sstevel@tonic-gate 				FILL(Hdrsz);
3301*7c478bd9Sstevel@tonic-gate 				if ((hit = read_hdr(BAR)) == NONE) {
3302*7c478bd9Sstevel@tonic-gate 					Hdrsz = ASCSZ;
3303*7c478bd9Sstevel@tonic-gate 					break;
3304*7c478bd9Sstevel@tonic-gate 				}
3305*7c478bd9Sstevel@tonic-gate 				hit = BAR;
3306*7c478bd9Sstevel@tonic-gate 				hsize = BARSZ;
3307*7c478bd9Sstevel@tonic-gate 				break;
3308*7c478bd9Sstevel@tonic-gate 			}
3309*7c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
3310*7c478bd9Sstevel@tonic-gate 
3311*7c478bd9Sstevel@tonic-gate 		case USTAR:
3312*7c478bd9Sstevel@tonic-gate 			if (Hdr_p != NULL && strcmp(Hdr_p, "ustar") == 0) {
3313*7c478bd9Sstevel@tonic-gate 				Hdrsz = TARSZ;
3314*7c478bd9Sstevel@tonic-gate 				FILL(Hdrsz);
3315*7c478bd9Sstevel@tonic-gate 				if ((hit = read_hdr(USTAR)) == NONE) {
3316*7c478bd9Sstevel@tonic-gate 					Hdrsz = ASCSZ;
3317*7c478bd9Sstevel@tonic-gate 					break;
3318*7c478bd9Sstevel@tonic-gate 				}
3319*7c478bd9Sstevel@tonic-gate 				hit = USTAR;
3320*7c478bd9Sstevel@tonic-gate 				hsize = TARSZ;
3321*7c478bd9Sstevel@tonic-gate 				break;
3322*7c478bd9Sstevel@tonic-gate 			}
3323*7c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
3324*7c478bd9Sstevel@tonic-gate 		case TAR:
3325*7c478bd9Sstevel@tonic-gate 			if (Hdr_p != NULL && strcmp(Hdr_p, "tar") == 0) {
3326*7c478bd9Sstevel@tonic-gate 				Hdrsz = TARSZ;
3327*7c478bd9Sstevel@tonic-gate 				FILL(Hdrsz);
3328*7c478bd9Sstevel@tonic-gate 				if ((hit = read_hdr(TAR)) == NONE) {
3329*7c478bd9Sstevel@tonic-gate 					Hdrsz = ASCSZ;
3330*7c478bd9Sstevel@tonic-gate 					break;
3331*7c478bd9Sstevel@tonic-gate 				}
3332*7c478bd9Sstevel@tonic-gate 				hit = TAR;
3333*7c478bd9Sstevel@tonic-gate 				hsize = TARSZ;
3334*7c478bd9Sstevel@tonic-gate 				break;
3335*7c478bd9Sstevel@tonic-gate 			}
3336*7c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
3337*7c478bd9Sstevel@tonic-gate 		default:
3338*7c478bd9Sstevel@tonic-gate 			msg(EXT, "Impossible header type.");
3339*7c478bd9Sstevel@tonic-gate 		} /* Hdr_type */
3340*7c478bd9Sstevel@tonic-gate 
3341*7c478bd9Sstevel@tonic-gate 		if (hit == TAR || hit == USTAR) {
3342*7c478bd9Sstevel@tonic-gate 			Gen.g_nam_p = &nambuf[0];
3343*7c478bd9Sstevel@tonic-gate 		}
3344*7c478bd9Sstevel@tonic-gate 
3345*7c478bd9Sstevel@tonic-gate 		if (hit != NONE) {
3346*7c478bd9Sstevel@tonic-gate 			FILL(hsize);
3347*7c478bd9Sstevel@tonic-gate 			goodhdr = 1;
3348*7c478bd9Sstevel@tonic-gate 			if (Gen.g_filesz < (off_t)0 || Gen.g_namesz < 1)
3349*7c478bd9Sstevel@tonic-gate 				goodhdr = 0;
3350*7c478bd9Sstevel@tonic-gate 			if ((hit != USTAR) && (hit != TAR))
3351*7c478bd9Sstevel@tonic-gate 				if (Gen.g_namesz - 1 > Max_namesz)
3352*7c478bd9Sstevel@tonic-gate 					goodhdr = 0;
3353*7c478bd9Sstevel@tonic-gate 			/* TAR and USTAR */
3354*7c478bd9Sstevel@tonic-gate 			if ((hit == USTAR) || (hit == TAR)) {
3355*7c478bd9Sstevel@tonic-gate 				if (*Gen.g_nam_p == '\0') { /* tar trailer */
3356*7c478bd9Sstevel@tonic-gate 					goodhdr = 1;
3357*7c478bd9Sstevel@tonic-gate 				} else {
3358*7c478bd9Sstevel@tonic-gate 
3359*7c478bd9Sstevel@tonic-gate 					G_p = &Gen;
3360*7c478bd9Sstevel@tonic-gate 					if (G_p->g_cksum !=
3361*7c478bd9Sstevel@tonic-gate 					    cksum(TARTYP, 0, NULL)) {
3362*7c478bd9Sstevel@tonic-gate 						goodhdr = 0;
3363*7c478bd9Sstevel@tonic-gate 						msg(ERR,
3364*7c478bd9Sstevel@tonic-gate 						    "Bad header - checksum "
3365*7c478bd9Sstevel@tonic-gate 						    "error.");
3366*7c478bd9Sstevel@tonic-gate 					}
3367*7c478bd9Sstevel@tonic-gate 				}
3368*7c478bd9Sstevel@tonic-gate 			} else if (hit != BAR) { /* binary, -c, ASC and CRC */
3369*7c478bd9Sstevel@tonic-gate 				if (Gen.g_nlink <= (ulong_t)0)
3370*7c478bd9Sstevel@tonic-gate 					goodhdr = 0;
3371*7c478bd9Sstevel@tonic-gate 				if (*(Buffr.b_out_p + hsize - 1) != '\0')
3372*7c478bd9Sstevel@tonic-gate 					goodhdr = 0;
3373*7c478bd9Sstevel@tonic-gate 			}
3374*7c478bd9Sstevel@tonic-gate 			if (!goodhdr) {
3375*7c478bd9Sstevel@tonic-gate 				hit = NONE;
3376*7c478bd9Sstevel@tonic-gate 				if (!(Args & OCk))
3377*7c478bd9Sstevel@tonic-gate 					break;
3378*7c478bd9Sstevel@tonic-gate 				msg(ERR,
3379*7c478bd9Sstevel@tonic-gate 				    "Corrupt header, file(s) may be lost.");
3380*7c478bd9Sstevel@tonic-gate 			} else {
3381*7c478bd9Sstevel@tonic-gate 				FILL(hsize);
3382*7c478bd9Sstevel@tonic-gate 			}
3383*7c478bd9Sstevel@tonic-gate 		} /* hit != NONE */
3384*7c478bd9Sstevel@tonic-gate 		if (hit == NONE) {
3385*7c478bd9Sstevel@tonic-gate 			Buffr.b_out_p++;
3386*7c478bd9Sstevel@tonic-gate 			Buffr.b_cnt--;
3387*7c478bd9Sstevel@tonic-gate 			if (!(Args & OCk))
3388*7c478bd9Sstevel@tonic-gate 				break;
3389*7c478bd9Sstevel@tonic-gate 			if (!cnt++)
3390*7c478bd9Sstevel@tonic-gate 				msg(ERR, "Searching for magic number/header.");
3391*7c478bd9Sstevel@tonic-gate 		}
3392*7c478bd9Sstevel@tonic-gate 	} while (hit == NONE);
3393*7c478bd9Sstevel@tonic-gate 	if (hit == NONE) {
3394*7c478bd9Sstevel@tonic-gate 		if (Hdr_type == NONE)
3395*7c478bd9Sstevel@tonic-gate 			msg(EXT, "Not a cpio file, bad header.");
3396*7c478bd9Sstevel@tonic-gate 		else
3397*7c478bd9Sstevel@tonic-gate 			msg(EXT, "Bad magic number/header.");
3398*7c478bd9Sstevel@tonic-gate 	} else if (cnt > 0) {
3399*7c478bd9Sstevel@tonic-gate 		msg(EPOST, "Re-synchronized on magic number/header.");
3400*7c478bd9Sstevel@tonic-gate 	}
3401*7c478bd9Sstevel@tonic-gate 	if (Hdr_type == NONE) {
3402*7c478bd9Sstevel@tonic-gate 		Hdr_type = hit;
3403*7c478bd9Sstevel@tonic-gate 		switch (Hdr_type) {
3404*7c478bd9Sstevel@tonic-gate 		case BIN:
3405*7c478bd9Sstevel@tonic-gate 			if (bswap)
3406*7c478bd9Sstevel@tonic-gate 				Args |= BSM;
3407*7c478bd9Sstevel@tonic-gate 			Hdrsz = HDRSZ;
3408*7c478bd9Sstevel@tonic-gate 			Max_namesz = CPATH;
3409*7c478bd9Sstevel@tonic-gate 			Pad_val = HALFWD;
3410*7c478bd9Sstevel@tonic-gate 			Onecopy = 0;
3411*7c478bd9Sstevel@tonic-gate 			break;
3412*7c478bd9Sstevel@tonic-gate 		case CHR:
3413*7c478bd9Sstevel@tonic-gate 			Hdrsz = CHRSZ;
3414*7c478bd9Sstevel@tonic-gate 			Max_namesz = CPATH;
3415*7c478bd9Sstevel@tonic-gate 			Pad_val = 0;
3416*7c478bd9Sstevel@tonic-gate 			Onecopy = 0;
3417*7c478bd9Sstevel@tonic-gate 			break;
3418*7c478bd9Sstevel@tonic-gate 		case ASC:
3419*7c478bd9Sstevel@tonic-gate 		case CRC:
3420*7c478bd9Sstevel@tonic-gate 			Hdrsz = ASCSZ;
3421*7c478bd9Sstevel@tonic-gate 			Max_namesz = APATH;
3422*7c478bd9Sstevel@tonic-gate 			Pad_val = FULLWD;
3423*7c478bd9Sstevel@tonic-gate 			Onecopy = 1;
3424*7c478bd9Sstevel@tonic-gate 			break;
3425*7c478bd9Sstevel@tonic-gate 		case USTAR:
3426*7c478bd9Sstevel@tonic-gate 			Hdrsz = TARSZ;
3427*7c478bd9Sstevel@tonic-gate 			Max_namesz = HNAMLEN - 1;
3428*7c478bd9Sstevel@tonic-gate 			Pad_val = FULLBK;
3429*7c478bd9Sstevel@tonic-gate 			Onecopy = 0;
3430*7c478bd9Sstevel@tonic-gate 			break;
3431*7c478bd9Sstevel@tonic-gate 		case BAR:
3432*7c478bd9Sstevel@tonic-gate 		case TAR:
3433*7c478bd9Sstevel@tonic-gate 			Hdrsz = TARSZ;
3434*7c478bd9Sstevel@tonic-gate 			Max_namesz = TNAMLEN - 1;
3435*7c478bd9Sstevel@tonic-gate 			Pad_val = FULLBK;
3436*7c478bd9Sstevel@tonic-gate 			Onecopy = 0;
3437*7c478bd9Sstevel@tonic-gate 			break;
3438*7c478bd9Sstevel@tonic-gate 		default:
3439*7c478bd9Sstevel@tonic-gate 			msg(EXT, "Impossible header type.");
3440*7c478bd9Sstevel@tonic-gate 		} /* Hdr_type */
3441*7c478bd9Sstevel@tonic-gate 	} /* Hdr_type == NONE */
3442*7c478bd9Sstevel@tonic-gate 	if ((Hdr_type == USTAR) || (Hdr_type == TAR) ||
3443*7c478bd9Sstevel@tonic-gate 	    (Hdr_type == BAR)) {			/* TAR, USTAR, BAR */
3444*7c478bd9Sstevel@tonic-gate 		Gen.g_namesz = 0;
3445*7c478bd9Sstevel@tonic-gate 		if (Gen.g_nam_p[0] == '\0')
3446*7c478bd9Sstevel@tonic-gate 			return (0);
3447*7c478bd9Sstevel@tonic-gate 		else {
3448*7c478bd9Sstevel@tonic-gate 			preptr = &prebuf[0];
3449*7c478bd9Sstevel@tonic-gate 			if (*preptr != (char)NULL) {
3450*7c478bd9Sstevel@tonic-gate 				k = strlen(&prebuf[0]);
3451*7c478bd9Sstevel@tonic-gate 				if (k < PRESIZ) {
3452*7c478bd9Sstevel@tonic-gate 					(void) strcpy(&fullnam[0], &prebuf[0]);
3453*7c478bd9Sstevel@tonic-gate 					j = 0;
3454*7c478bd9Sstevel@tonic-gate 					fullnam[k++] = '/';
3455*7c478bd9Sstevel@tonic-gate 					while ((j < NAMSIZ) && (&nambuf[j] !=
3456*7c478bd9Sstevel@tonic-gate 					    (char)NULL)) {
3457*7c478bd9Sstevel@tonic-gate 						fullnam[k] = nambuf[j];
3458*7c478bd9Sstevel@tonic-gate 						k++; j++;
3459*7c478bd9Sstevel@tonic-gate 					}
3460*7c478bd9Sstevel@tonic-gate 					fullnam[k] = '\0';
3461*7c478bd9Sstevel@tonic-gate 				} else if (k >= PRESIZ) {
3462*7c478bd9Sstevel@tonic-gate 					k = 0;
3463*7c478bd9Sstevel@tonic-gate 					while ((k < PRESIZ) && (prebuf[k] !=
3464*7c478bd9Sstevel@tonic-gate 					    '\0')) {
3465*7c478bd9Sstevel@tonic-gate 						fullnam[k] = prebuf[k];
3466*7c478bd9Sstevel@tonic-gate 						k++;
3467*7c478bd9Sstevel@tonic-gate 					}
3468*7c478bd9Sstevel@tonic-gate 					fullnam[k++] = '/';
3469*7c478bd9Sstevel@tonic-gate 					j = 0;
3470*7c478bd9Sstevel@tonic-gate 					while ((j < NAMSIZ) && (nambuf[j] !=
3471*7c478bd9Sstevel@tonic-gate 					    '\0')) {
3472*7c478bd9Sstevel@tonic-gate 						fullnam[k] = nambuf[j];
3473*7c478bd9Sstevel@tonic-gate 						k++; j++;
3474*7c478bd9Sstevel@tonic-gate 					}
3475*7c478bd9Sstevel@tonic-gate 					fullnam[k] = '\0';
3476*7c478bd9Sstevel@tonic-gate 				}
3477*7c478bd9Sstevel@tonic-gate 				Gen.g_nam_p = &fullnam[0];
3478*7c478bd9Sstevel@tonic-gate 			} else
3479*7c478bd9Sstevel@tonic-gate 				Gen.g_nam_p = &nambuf[0];
3480*7c478bd9Sstevel@tonic-gate 
3481*7c478bd9Sstevel@tonic-gate 			/*
3482*7c478bd9Sstevel@tonic-gate 			 * initialize the buffer so that the prefix will not
3483*7c478bd9Sstevel@tonic-gate 			 * applied to the next entry in the archive
3484*7c478bd9Sstevel@tonic-gate 			 */
3485*7c478bd9Sstevel@tonic-gate 			(void) memset(prebuf, 0, sizeof (prebuf));
3486*7c478bd9Sstevel@tonic-gate 		}
3487*7c478bd9Sstevel@tonic-gate 	} else if (Hdr_type != BAR) {
3488*7c478bd9Sstevel@tonic-gate 		(void) memcpy(Gen.g_nam_p, Buffr.b_out_p + Hdrsz, Gen.g_namesz);
3489*7c478bd9Sstevel@tonic-gate 		if (!(strcmp(Gen.g_nam_p, "TRAILER!!!")))
3490*7c478bd9Sstevel@tonic-gate 			return (0);
3491*7c478bd9Sstevel@tonic-gate 	}
3492*7c478bd9Sstevel@tonic-gate 	offset = ((hsize + Pad_val) & ~Pad_val);
3493*7c478bd9Sstevel@tonic-gate 	FILL(offset + Hdrsz);
3494*7c478bd9Sstevel@tonic-gate 	Thdr_p = (union tblock *)Buffr.b_out_p;
3495*7c478bd9Sstevel@tonic-gate 	Buffr.b_out_p += offset;
3496*7c478bd9Sstevel@tonic-gate 	Buffr.b_cnt -= (off_t)offset;
3497*7c478bd9Sstevel@tonic-gate 	ftype = Gen.g_mode & Ftype;
3498*7c478bd9Sstevel@tonic-gate 
3499*7c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
3500*7c478bd9Sstevel@tonic-gate 	/* extended attribute support */
3501*7c478bd9Sstevel@tonic-gate 	if (((Gen.g_mode & S_IFMT) == _XATTR_CPIO_MODE) ||
3502*7c478bd9Sstevel@tonic-gate 	    ((Hdr_type == USTAR || Hdr_type == TAR) &&
3503*7c478bd9Sstevel@tonic-gate 	    Thdr_p->tbuf.t_typeflag == _XATTR_HDRTYPE)) {
3504*7c478bd9Sstevel@tonic-gate 		if (xattrp != (struct xattr_buf *)NULL) {
3505*7c478bd9Sstevel@tonic-gate 			if (xattrbadhead) {
3506*7c478bd9Sstevel@tonic-gate 				free(xattrhead);
3507*7c478bd9Sstevel@tonic-gate 				xattrp = NULL;
3508*7c478bd9Sstevel@tonic-gate 				xattr_linkp = NULL;
3509*7c478bd9Sstevel@tonic-gate 				xattrhead = NULL;
3510*7c478bd9Sstevel@tonic-gate 				return (1);
3511*7c478bd9Sstevel@tonic-gate 			}
3512*7c478bd9Sstevel@tonic-gate 			if (Atflag == 0 && ((Args & OCt) == 0)) {
3513*7c478bd9Sstevel@tonic-gate 				data_in(P_SKIP);
3514*7c478bd9Sstevel@tonic-gate 				free(xattrhead);
3515*7c478bd9Sstevel@tonic-gate 				xattrhead = NULL;
3516*7c478bd9Sstevel@tonic-gate 				xattrp = NULL;
3517*7c478bd9Sstevel@tonic-gate 				Gen.g_attrfnam_p = (char *)NULL;
3518*7c478bd9Sstevel@tonic-gate 				Gen.g_attrnam_p = (char *)NULL;
3519*7c478bd9Sstevel@tonic-gate 				return (2);
3520*7c478bd9Sstevel@tonic-gate 			}
3521*7c478bd9Sstevel@tonic-gate 
3522*7c478bd9Sstevel@tonic-gate 			if (Gen.g_attrfnam_p != (char *)NULL) {
3523*7c478bd9Sstevel@tonic-gate 				free(Gen.g_attrfnam_p);
3524*7c478bd9Sstevel@tonic-gate 				Gen.g_attrfnam_p = (char *)NULL;
3525*7c478bd9Sstevel@tonic-gate 			}
3526*7c478bd9Sstevel@tonic-gate 			if (Gen.g_attrnam_p != (char *)NULL) {
3527*7c478bd9Sstevel@tonic-gate 				free(Gen.g_attrnam_p);
3528*7c478bd9Sstevel@tonic-gate 				Gen.g_attrnam_p = (char *)NULL;
3529*7c478bd9Sstevel@tonic-gate 			}
3530*7c478bd9Sstevel@tonic-gate 			if (Renam_p && Renam_p[0] != '\0') {
3531*7c478bd9Sstevel@tonic-gate 				Gen.g_attrfnam_p = e_strdup(E_EXIT, Renam_p);
3532*7c478bd9Sstevel@tonic-gate 			} else {
3533*7c478bd9Sstevel@tonic-gate 				Gen.g_attrfnam_p = e_strdup(E_EXIT,
3534*7c478bd9Sstevel@tonic-gate 				    xattrp->h_names);
3535*7c478bd9Sstevel@tonic-gate 			}
3536*7c478bd9Sstevel@tonic-gate 
3537*7c478bd9Sstevel@tonic-gate 			Gen.g_attrnam_p = e_strdup(E_EXIT,
3538*7c478bd9Sstevel@tonic-gate 			    xattrp->h_names + strlen(xattrp->h_names) + 1);
3539*7c478bd9Sstevel@tonic-gate 
3540*7c478bd9Sstevel@tonic-gate 			if (Hdr_type != USTAR && Hdr_type != TAR) {
3541*7c478bd9Sstevel@tonic-gate 				Gen.g_mode = Gen.g_mode & (~_XATTR_CPIO_MODE);
3542*7c478bd9Sstevel@tonic-gate 				Gen.g_mode |= attrmode(xattrp->h_typeflag);
3543*7c478bd9Sstevel@tonic-gate 
3544*7c478bd9Sstevel@tonic-gate 			} else if (Hdr_type == USTAR || Hdr_type == TAR) {
3545*7c478bd9Sstevel@tonic-gate 				Thdr_p->tbuf.t_typeflag = xattrp->h_typeflag;
3546*7c478bd9Sstevel@tonic-gate 			}
3547*7c478bd9Sstevel@tonic-gate 			if (xattr_linkp != (struct xattr_buf *)NULL) {
3548*7c478bd9Sstevel@tonic-gate 				if (Gen.g_linktoattrfnam_p != (char *)NULL) {
3549*7c478bd9Sstevel@tonic-gate 					free(Gen.g_linktoattrfnam_p);
3550*7c478bd9Sstevel@tonic-gate 					Gen.g_linktoattrfnam_p = NULL;
3551*7c478bd9Sstevel@tonic-gate 				}
3552*7c478bd9Sstevel@tonic-gate 				if (Gen.g_linktoattrnam_p != (char *)NULL) {
3553*7c478bd9Sstevel@tonic-gate 					free(Gen.g_linktoattrnam_p);
3554*7c478bd9Sstevel@tonic-gate 					Gen.g_linktoattrnam_p = NULL;
3555*7c478bd9Sstevel@tonic-gate 				}
3556*7c478bd9Sstevel@tonic-gate 				Gen.g_linktoattrfnam_p = e_strdup(E_EXIT,
3557*7c478bd9Sstevel@tonic-gate 				    xattr_linkp->h_names);
3558*7c478bd9Sstevel@tonic-gate 				Gen.g_linktoattrnam_p = e_strdup(E_EXIT,
3559*7c478bd9Sstevel@tonic-gate 				    xattr_linkp->h_names +
3560*7c478bd9Sstevel@tonic-gate 				    strlen(xattr_linkp->h_names) + 1);
3561*7c478bd9Sstevel@tonic-gate 				xattr_linkp = NULL;
3562*7c478bd9Sstevel@tonic-gate 			}
3563*7c478bd9Sstevel@tonic-gate 			ftype = Gen.g_mode & Ftype;
3564*7c478bd9Sstevel@tonic-gate 			Adir = ftype == S_IFDIR;
3565*7c478bd9Sstevel@tonic-gate 			Aspec = (ftype == S_IFBLK ||
3566*7c478bd9Sstevel@tonic-gate 			    ftype == S_IFCHR || ftype == S_IFIFO);
3567*7c478bd9Sstevel@tonic-gate 
3568*7c478bd9Sstevel@tonic-gate 			if (Gen.g_attrnam_p[0] == '.' &&
3569*7c478bd9Sstevel@tonic-gate 			    Gen.g_attrnam_p[1] == '\0' &&
3570*7c478bd9Sstevel@tonic-gate 			    xattrp->h_typeflag == DIRTYPE) {
3571*7c478bd9Sstevel@tonic-gate 				Hiddendir = 1;
3572*7c478bd9Sstevel@tonic-gate 			} else {
3573*7c478bd9Sstevel@tonic-gate 				Hiddendir = 0;
3574*7c478bd9Sstevel@tonic-gate 			}
3575*7c478bd9Sstevel@tonic-gate 
3576*7c478bd9Sstevel@tonic-gate 			free(xattrhead);
3577*7c478bd9Sstevel@tonic-gate 			xattrhead = NULL;
3578*7c478bd9Sstevel@tonic-gate 			xattrp = NULL;
3579*7c478bd9Sstevel@tonic-gate 		} else {
3580*7c478bd9Sstevel@tonic-gate 			if (xattrbadhead == 0) {
3581*7c478bd9Sstevel@tonic-gate 				(void) read_xattr_hdr();
3582*7c478bd9Sstevel@tonic-gate 				return (2);
3583*7c478bd9Sstevel@tonic-gate 			}
3584*7c478bd9Sstevel@tonic-gate 		}
3585*7c478bd9Sstevel@tonic-gate 	}
3586*7c478bd9Sstevel@tonic-gate #endif /* O_XATTR */
3587*7c478bd9Sstevel@tonic-gate 
3588*7c478bd9Sstevel@tonic-gate 	/* acl support: grab acl info */
3589*7c478bd9Sstevel@tonic-gate 	if ((Gen.g_mode == SECMODE) || ((Hdr_type == USTAR ||
3590*7c478bd9Sstevel@tonic-gate 	    Hdr_type == TAR) && Thdr_p->tbuf.t_typeflag == 'A')) {
3591*7c478bd9Sstevel@tonic-gate 		/* this is an ancillary file */
3592*7c478bd9Sstevel@tonic-gate 		off_t	bytes;
3593*7c478bd9Sstevel@tonic-gate 		char	*secp;
3594*7c478bd9Sstevel@tonic-gate 		int	pad;
3595*7c478bd9Sstevel@tonic-gate 		int	cnt;
3596*7c478bd9Sstevel@tonic-gate 		char	*tp;
3597*7c478bd9Sstevel@tonic-gate 		int	attrsize;
3598*7c478bd9Sstevel@tonic-gate 
3599*7c478bd9Sstevel@tonic-gate 		if (Pflag) {
3600*7c478bd9Sstevel@tonic-gate 			bytes = Gen.g_filesz;
3601*7c478bd9Sstevel@tonic-gate 			secp = e_zalloc(E_EXIT, (uint_t)bytes);
3602*7c478bd9Sstevel@tonic-gate 			tp = secp;
3603*7c478bd9Sstevel@tonic-gate 
3604*7c478bd9Sstevel@tonic-gate 			while (bytes > 0) {
3605*7c478bd9Sstevel@tonic-gate 				cnt = (int)(bytes > CPIOBSZ) ? CPIOBSZ : bytes;
3606*7c478bd9Sstevel@tonic-gate 				FILL(cnt);
3607*7c478bd9Sstevel@tonic-gate 				(void) memcpy(tp, Buffr.b_out_p, cnt);
3608*7c478bd9Sstevel@tonic-gate 				tp += cnt;
3609*7c478bd9Sstevel@tonic-gate 				Buffr.b_out_p += cnt;
3610*7c478bd9Sstevel@tonic-gate 				Buffr.b_cnt -= (off_t)cnt;
3611*7c478bd9Sstevel@tonic-gate 				bytes -= (off_t)cnt;
3612*7c478bd9Sstevel@tonic-gate 			}
3613*7c478bd9Sstevel@tonic-gate 
3614*7c478bd9Sstevel@tonic-gate 			pad = (Pad_val + 1 - (Gen.g_filesz & Pad_val)) &
3615*7c478bd9Sstevel@tonic-gate 			    Pad_val;
3616*7c478bd9Sstevel@tonic-gate 			if (pad != 0) {
3617*7c478bd9Sstevel@tonic-gate 				FILL(pad);
3618*7c478bd9Sstevel@tonic-gate 				Buffr.b_out_p += pad;
3619*7c478bd9Sstevel@tonic-gate 				Buffr.b_cnt -= (off_t)pad;
3620*7c478bd9Sstevel@tonic-gate 			}
3621*7c478bd9Sstevel@tonic-gate 
3622*7c478bd9Sstevel@tonic-gate 			/* got all attributes in secp */
3623*7c478bd9Sstevel@tonic-gate 			tp = secp;
3624*7c478bd9Sstevel@tonic-gate 			do {
3625*7c478bd9Sstevel@tonic-gate 				attr = (struct sec_attr *)tp;
3626*7c478bd9Sstevel@tonic-gate 				switch (attr->attr_type) {
3627*7c478bd9Sstevel@tonic-gate 				case UFSD_ACL:
3628*7c478bd9Sstevel@tonic-gate 					(void) sscanf(attr->attr_len, "%7lo",
3629*7c478bd9Sstevel@tonic-gate 					    (ulong_t *)&aclcnt);
3630*7c478bd9Sstevel@tonic-gate 				/* header is 8 */
3631*7c478bd9Sstevel@tonic-gate 					attrsize = 8 +
3632*7c478bd9Sstevel@tonic-gate 					    strlen(&attr->attr_info[0])
3633*7c478bd9Sstevel@tonic-gate 					    + 1;
3634*7c478bd9Sstevel@tonic-gate 					aclp = aclfromtext(&attr->attr_info[0],
3635*7c478bd9Sstevel@tonic-gate 					    &cnt);
3636*7c478bd9Sstevel@tonic-gate 					if (aclp == NULL) {
3637*7c478bd9Sstevel@tonic-gate 						msg(ERR, "aclfromtext failed");
3638*7c478bd9Sstevel@tonic-gate 						break;
3639*7c478bd9Sstevel@tonic-gate 					}
3640*7c478bd9Sstevel@tonic-gate 					if (aclcnt != cnt) {
3641*7c478bd9Sstevel@tonic-gate 						msg(ERR, "acl count error");
3642*7c478bd9Sstevel@tonic-gate 						break;
3643*7c478bd9Sstevel@tonic-gate 					}
3644*7c478bd9Sstevel@tonic-gate 					bytes -= attrsize;
3645*7c478bd9Sstevel@tonic-gate 					break;
3646*7c478bd9Sstevel@tonic-gate 
3647*7c478bd9Sstevel@tonic-gate 				/* SunFed case goes here */
3648*7c478bd9Sstevel@tonic-gate 
3649*7c478bd9Sstevel@tonic-gate 				default:
3650*7c478bd9Sstevel@tonic-gate 					msg(EXT, "unrecognized attr type");
3651*7c478bd9Sstevel@tonic-gate 					break;
3652*7c478bd9Sstevel@tonic-gate 			}
3653*7c478bd9Sstevel@tonic-gate 			/* next attributes */
3654*7c478bd9Sstevel@tonic-gate 			tp += attrsize;
3655*7c478bd9Sstevel@tonic-gate 			} while (bytes > 0);
3656*7c478bd9Sstevel@tonic-gate 			free(secp);
3657*7c478bd9Sstevel@tonic-gate 		} else {
3658*7c478bd9Sstevel@tonic-gate 			/* skip security info */
3659*7c478bd9Sstevel@tonic-gate 			G_p = &Gen;
3660*7c478bd9Sstevel@tonic-gate 			data_in(P_SKIP);
3661*7c478bd9Sstevel@tonic-gate 		}
3662*7c478bd9Sstevel@tonic-gate 		/*
3663*7c478bd9Sstevel@tonic-gate 		 * We already got the file content, dont call file_in()
3664*7c478bd9Sstevel@tonic-gate 		 * when return. The new return code(2) is used to
3665*7c478bd9Sstevel@tonic-gate 		 *  indicate that.
3666*7c478bd9Sstevel@tonic-gate 		 */
3667*7c478bd9Sstevel@tonic-gate 		VERBOSE((Args & OCt), Gen.g_nam_p);
3668*7c478bd9Sstevel@tonic-gate 		return (2);
3669*7c478bd9Sstevel@tonic-gate 	} /* acl */
3670*7c478bd9Sstevel@tonic-gate 
3671*7c478bd9Sstevel@tonic-gate 	Adir = (ftype == S_IFDIR);
3672*7c478bd9Sstevel@tonic-gate 	Aspec = (ftype == S_IFBLK || ftype == S_IFCHR || ftype == S_IFIFO);
3673*7c478bd9Sstevel@tonic-gate 
3674*7c478bd9Sstevel@tonic-gate 	/*
3675*7c478bd9Sstevel@tonic-gate 	 * Skip any trailing slashes
3676*7c478bd9Sstevel@tonic-gate 	 */
3677*7c478bd9Sstevel@tonic-gate 	chop_endslashes(Gen.g_nam_p);
3678*7c478bd9Sstevel@tonic-gate 	return (1);
3679*7c478bd9Sstevel@tonic-gate }
3680*7c478bd9Sstevel@tonic-gate 
3681*7c478bd9Sstevel@tonic-gate /*
3682*7c478bd9Sstevel@tonic-gate  * getname: Get file names for inclusion in the archive.  When end of file
3683*7c478bd9Sstevel@tonic-gate  * on the input stream of file names is reached, flush the link buffer out.
3684*7c478bd9Sstevel@tonic-gate  * For each filename, remove leading "./"s and multiple "/"s, and remove
3685*7c478bd9Sstevel@tonic-gate  * any trailing newline "\n".  Finally, verify the existance of the file,
3686*7c478bd9Sstevel@tonic-gate  * and call creat_hdr() to fill in the gen_hdr structure.
3687*7c478bd9Sstevel@tonic-gate  */
3688*7c478bd9Sstevel@tonic-gate 
3689*7c478bd9Sstevel@tonic-gate static int
3690*7c478bd9Sstevel@tonic-gate getname(void)
3691*7c478bd9Sstevel@tonic-gate {
3692*7c478bd9Sstevel@tonic-gate 	int goodfile = 0, lastchar, err;
3693*7c478bd9Sstevel@tonic-gate 	char *s;
3694*7c478bd9Sstevel@tonic-gate 	char *dir;
3695*7c478bd9Sstevel@tonic-gate 
3696*7c478bd9Sstevel@tonic-gate 	Gen.g_nam_p = Nam_p;
3697*7c478bd9Sstevel@tonic-gate 
3698*7c478bd9Sstevel@tonic-gate 	while (!goodfile) {
3699*7c478bd9Sstevel@tonic-gate 		err = 0;
3700*7c478bd9Sstevel@tonic-gate 
3701*7c478bd9Sstevel@tonic-gate 		while ((s = fgets(Gen.g_nam_p, APATH+1, In_p))
3702*7c478bd9Sstevel@tonic-gate 		    != NULL) {
3703*7c478bd9Sstevel@tonic-gate 			lastchar = strlen(s) - 1;
3704*7c478bd9Sstevel@tonic-gate 			issymlink = 0;
3705*7c478bd9Sstevel@tonic-gate 
3706*7c478bd9Sstevel@tonic-gate 			if (s[lastchar] != '\n') {
3707*7c478bd9Sstevel@tonic-gate 				if (lastchar == APATH - 1) {
3708*7c478bd9Sstevel@tonic-gate 					if (!err) {
3709*7c478bd9Sstevel@tonic-gate 						msg(ERR,
3710*7c478bd9Sstevel@tonic-gate 						    "%s name too long.",
3711*7c478bd9Sstevel@tonic-gate 						    Nam_p);
3712*7c478bd9Sstevel@tonic-gate 					}
3713*7c478bd9Sstevel@tonic-gate 					goodfile = 0;
3714*7c478bd9Sstevel@tonic-gate 					err = 1;
3715*7c478bd9Sstevel@tonic-gate 				} else {
3716*7c478bd9Sstevel@tonic-gate 					break;
3717*7c478bd9Sstevel@tonic-gate 				}
3718*7c478bd9Sstevel@tonic-gate 			} else {
3719*7c478bd9Sstevel@tonic-gate 				s[lastchar] = '\0';
3720*7c478bd9Sstevel@tonic-gate 				break;
3721*7c478bd9Sstevel@tonic-gate 			}
3722*7c478bd9Sstevel@tonic-gate 		}
3723*7c478bd9Sstevel@tonic-gate 
3724*7c478bd9Sstevel@tonic-gate 		if (s == (char *)NULL) {
3725*7c478bd9Sstevel@tonic-gate 			if (Gen.g_dirfd != -1) {
3726*7c478bd9Sstevel@tonic-gate 				(void) close(Gen.g_dirfd);
3727*7c478bd9Sstevel@tonic-gate 				Gen.g_dirfd = -1;
3728*7c478bd9Sstevel@tonic-gate 			}
3729*7c478bd9Sstevel@tonic-gate 			if (Onecopy && (Args & OCo)) {
3730*7c478bd9Sstevel@tonic-gate 				flush_lnks();
3731*7c478bd9Sstevel@tonic-gate 			}
3732*7c478bd9Sstevel@tonic-gate 			return (0);
3733*7c478bd9Sstevel@tonic-gate 		}
3734*7c478bd9Sstevel@tonic-gate 
3735*7c478bd9Sstevel@tonic-gate 		while (*Gen.g_nam_p == '.' && Gen.g_nam_p[1] == '/') {
3736*7c478bd9Sstevel@tonic-gate 			Gen.g_nam_p += 2;
3737*7c478bd9Sstevel@tonic-gate 			while (*Gen.g_nam_p == '/')
3738*7c478bd9Sstevel@tonic-gate 				Gen.g_nam_p++;
3739*7c478bd9Sstevel@tonic-gate 		}
3740*7c478bd9Sstevel@tonic-gate 
3741*7c478bd9Sstevel@tonic-gate 		/*
3742*7c478bd9Sstevel@tonic-gate 		 * Skip any trailing slashes
3743*7c478bd9Sstevel@tonic-gate 		 */
3744*7c478bd9Sstevel@tonic-gate 		chop_endslashes(Gen.g_nam_p);
3745*7c478bd9Sstevel@tonic-gate 
3746*7c478bd9Sstevel@tonic-gate 		/*
3747*7c478bd9Sstevel@tonic-gate 		 * Figure out parent directory
3748*7c478bd9Sstevel@tonic-gate 		 */
3749*7c478bd9Sstevel@tonic-gate 
3750*7c478bd9Sstevel@tonic-gate 		if (Gen.g_attrnam_p != (char *)NULL) {
3751*7c478bd9Sstevel@tonic-gate 			if (Gen.g_dirfd != -1) {
3752*7c478bd9Sstevel@tonic-gate 				(void) close(Gen.g_dirfd);
3753*7c478bd9Sstevel@tonic-gate 			}
3754*7c478bd9Sstevel@tonic-gate 			Gen.g_dirfd = attropen(Gen.g_attrfnam_p, ".", O_RDONLY);
3755*7c478bd9Sstevel@tonic-gate 			if (Gen.g_dirfd == -1) {
3756*7c478bd9Sstevel@tonic-gate 				msg(ERRN,
3757*7c478bd9Sstevel@tonic-gate 				    "Cannot open attribute directory"
3758*7c478bd9Sstevel@tonic-gate 				    " of file %s", Gen.g_attrfnam_p);
3759*7c478bd9Sstevel@tonic-gate 				continue;
3760*7c478bd9Sstevel@tonic-gate 			}
3761*7c478bd9Sstevel@tonic-gate 		} else {
3762*7c478bd9Sstevel@tonic-gate #ifdef O_XATTR
3763*7c478bd9Sstevel@tonic-gate 			char dirpath[PATH_MAX];
3764*7c478bd9Sstevel@tonic-gate 
3765*7c478bd9Sstevel@tonic-gate 			get_parent(Gen.g_nam_p, dirpath);
3766*7c478bd9Sstevel@tonic-gate 			if (Atflag) {
3767*7c478bd9Sstevel@tonic-gate 				dir = dirpath;
3768*7c478bd9Sstevel@tonic-gate 				if (Gen.g_dirfd != -1) {
3769*7c478bd9Sstevel@tonic-gate 					(void) close(Gen.g_dirfd);
3770*7c478bd9Sstevel@tonic-gate 				}
3771*7c478bd9Sstevel@tonic-gate 				Gen.g_dirfd = open(dir, O_RDONLY);
3772*7c478bd9Sstevel@tonic-gate 				if (Gen.g_dirfd == -1) {
3773*7c478bd9Sstevel@tonic-gate 					msg(ERRN,
3774*7c478bd9Sstevel@tonic-gate 					    "Cannot open directory %s", dir);
3775*7c478bd9Sstevel@tonic-gate 					continue;
3776*7c478bd9Sstevel@tonic-gate 				}
3777*7c478bd9Sstevel@tonic-gate 			} else {
3778*7c478bd9Sstevel@tonic-gate 				/*
3779*7c478bd9Sstevel@tonic-gate 				 * g_dirpath is the pathname cache maintaining
3780*7c478bd9Sstevel@tonic-gate 				 * the dirname which is currently opened.
3781*7c478bd9Sstevel@tonic-gate 				 * We first check the g_dirpath to see if the
3782*7c478bd9Sstevel@tonic-gate 				 * given dirname matches. If so, we don't need
3783*7c478bd9Sstevel@tonic-gate 				 * to open the dir, but we can use the g_dirfd
3784*7c478bd9Sstevel@tonic-gate 				 * as is if it is still available.
3785*7c478bd9Sstevel@tonic-gate 				 */
3786*7c478bd9Sstevel@tonic-gate 				dir = NULL;
3787*7c478bd9Sstevel@tonic-gate 				if (Gen.g_dirpath == NULL ||
3788*7c478bd9Sstevel@tonic-gate 				    Gen.g_dirfd == -1) {
3789*7c478bd9Sstevel@tonic-gate 					/*
3790*7c478bd9Sstevel@tonic-gate 					 * It's the first time or it has
3791*7c478bd9Sstevel@tonic-gate 					 * all gone.
3792*7c478bd9Sstevel@tonic-gate 					 */
3793*7c478bd9Sstevel@tonic-gate 					dir = e_strdup(E_EXIT, dirpath);
3794*7c478bd9Sstevel@tonic-gate 				} else {
3795*7c478bd9Sstevel@tonic-gate 					if (strcmp(Gen.g_dirpath,
3796*7c478bd9Sstevel@tonic-gate 					    dirpath) != 0) {
3797*7c478bd9Sstevel@tonic-gate 						/* different directory */
3798*7c478bd9Sstevel@tonic-gate 						dir = e_strdup(E_EXIT, dirpath);
3799*7c478bd9Sstevel@tonic-gate 					}
3800*7c478bd9Sstevel@tonic-gate 				}
3801*7c478bd9Sstevel@tonic-gate 				if (dir != NULL) {
3802*7c478bd9Sstevel@tonic-gate 					/*
3803*7c478bd9Sstevel@tonic-gate 					 * We need to open the new directory.
3804*7c478bd9Sstevel@tonic-gate 					 * discard the pathname and dirfd
3805*7c478bd9Sstevel@tonic-gate 					 * for the previous directory.
3806*7c478bd9Sstevel@tonic-gate 					 */
3807*7c478bd9Sstevel@tonic-gate 					if (Gen.g_dirpath != NULL) {
3808*7c478bd9Sstevel@tonic-gate 						free(Gen.g_dirpath);
3809*7c478bd9Sstevel@tonic-gate 						Gen.g_dirpath = NULL;
3810*7c478bd9Sstevel@tonic-gate 					}
3811*7c478bd9Sstevel@tonic-gate 					if (Gen.g_dirfd != -1) {
3812*7c478bd9Sstevel@tonic-gate 						(void) close(Gen.g_dirfd);
3813*7c478bd9Sstevel@tonic-gate 					}
3814*7c478bd9Sstevel@tonic-gate 					/* open the new dir */
3815*7c478bd9Sstevel@tonic-gate 					Gen.g_dirfd = open(dir, O_RDONLY);
3816*7c478bd9Sstevel@tonic-gate 					if (Gen.g_dirfd == -1) {
3817*7c478bd9Sstevel@tonic-gate 						msg(ERRN, "Cannot open "
3818*7c478bd9Sstevel@tonic-gate 							"directory %s", dir);
3819*7c478bd9Sstevel@tonic-gate 						continue;
3820*7c478bd9Sstevel@tonic-gate 					}
3821*7c478bd9Sstevel@tonic-gate 					Gen.g_dirpath = dir;
3822*7c478bd9Sstevel@tonic-gate 				}
3823*7c478bd9Sstevel@tonic-gate 			}
3824*7c478bd9Sstevel@tonic-gate #else
3825*7c478bd9Sstevel@tonic-gate 			Gen.g_dirfd = -1;
3826*7c478bd9Sstevel@tonic-gate #endif
3827*7c478bd9Sstevel@tonic-gate 		}
3828*7c478bd9Sstevel@tonic-gate 
3829*7c478bd9Sstevel@tonic-gate 		/* creat_hdr checks for USTAR filename length */
3830*7c478bd9Sstevel@tonic-gate 
3831*7c478bd9Sstevel@tonic-gate 		if (Hdr_type != USTAR && strlen(Gen.g_nam_p) >
3832*7c478bd9Sstevel@tonic-gate 		    Max_namesz) {
3833*7c478bd9Sstevel@tonic-gate 			if (!err) {
3834*7c478bd9Sstevel@tonic-gate 				msg(ERR, "%s%s%s name too long.",
3835*7c478bd9Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
3836*7c478bd9Sstevel@tonic-gate 				    Nam_p : Gen.g_attrfnam_p,
3837*7c478bd9Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
3838*7c478bd9Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
3839*7c478bd9Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
3840*7c478bd9Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
3841*7c478bd9Sstevel@tonic-gate 			}
3842*7c478bd9Sstevel@tonic-gate 			goodfile = 0;
3843*7c478bd9Sstevel@tonic-gate 			err = 1;
3844*7c478bd9Sstevel@tonic-gate 		}
3845*7c478bd9Sstevel@tonic-gate 
3846*7c478bd9Sstevel@tonic-gate 		if (err) {
3847*7c478bd9Sstevel@tonic-gate 			continue;
3848*7c478bd9Sstevel@tonic-gate 		} else {
3849*7c478bd9Sstevel@tonic-gate 			G_p = &Gen;
3850*7c478bd9Sstevel@tonic-gate 			if (!LSTAT(Gen.g_dirfd, Gen.g_nam_p, &SrcSt)) {
3851*7c478bd9Sstevel@tonic-gate 				goodfile = 1;
3852*7c478bd9Sstevel@tonic-gate 
3853*7c478bd9Sstevel@tonic-gate 				if ((SrcSt.st_mode & Ftype) == S_IFLNK) {
3854*7c478bd9Sstevel@tonic-gate 					issymlink = 1;
3855*7c478bd9Sstevel@tonic-gate 
3856*7c478bd9Sstevel@tonic-gate 					if ((Args & OCL)) {
3857*7c478bd9Sstevel@tonic-gate 						errno = 0;
3858*7c478bd9Sstevel@tonic-gate 						if (STAT(Gen.g_dirfd,
3859*7c478bd9Sstevel@tonic-gate 						    G_p->g_nam_p,
3860*7c478bd9Sstevel@tonic-gate 						    &SrcSt) < 0) {
3861*7c478bd9Sstevel@tonic-gate 							msg(ERRN,
3862*7c478bd9Sstevel@tonic-gate 							    "Cannot follow"
3863*7c478bd9Sstevel@tonic-gate 							    " \"%s%s%s\"",
3864*7c478bd9Sstevel@tonic-gate 							    (Gen.g_attrnam_p ==
3865*7c478bd9Sstevel@tonic-gate 							    (char *)NULL) ?
3866*7c478bd9Sstevel@tonic-gate 							    Gen.g_nam_p :
3867*7c478bd9Sstevel@tonic-gate 							    Gen.g_attrfnam_p,
3868*7c478bd9Sstevel@tonic-gate 							    (Gen.g_attrnam_p ==
3869*7c478bd9Sstevel@tonic-gate 							    (char *)NULL) ?
3870*7c478bd9Sstevel@tonic-gate 							    "" :
3871*7c478bd9Sstevel@tonic-gate 							    gettext(
3872*7c478bd9Sstevel@tonic-gate 							    " Attribute "),
3873*7c478bd9Sstevel@tonic-gate 							    (Gen.g_attrnam_p ==
3874*7c478bd9Sstevel@tonic-gate 							    (char *)NULL) ?
3875*7c478bd9Sstevel@tonic-gate 							    "" :
3876*7c478bd9Sstevel@tonic-gate 							    Gen.g_attrnam_p);
3877*7c478bd9Sstevel@tonic-gate 							goodfile = 0;
3878*7c478bd9Sstevel@tonic-gate 						}
3879*7c478bd9Sstevel@tonic-gate 					}
3880*7c478bd9Sstevel@tonic-gate 				}
3881*7c478bd9Sstevel@tonic-gate 
3882*7c478bd9Sstevel@tonic-gate 				if (Use_old_stat) {
3883*7c478bd9Sstevel@tonic-gate 					OldSt = convert_to_old_stat(&SrcSt,
3884*7c478bd9Sstevel@tonic-gate 					    Gen.g_nam_p, Gen.g_attrnam_p);
3885*7c478bd9Sstevel@tonic-gate 
3886*7c478bd9Sstevel@tonic-gate 					if (OldSt == NULL) {
3887*7c478bd9Sstevel@tonic-gate 						goodfile = 0;
3888*7c478bd9Sstevel@tonic-gate 					}
3889*7c478bd9Sstevel@tonic-gate 				}
3890*7c478bd9Sstevel@tonic-gate 			} else {
3891*7c478bd9Sstevel@tonic-gate 				msg(ERRN,
3892*7c478bd9Sstevel@tonic-gate 				    "Error with fstatat() of \"%s%s%s\"",
3893*7c478bd9Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
3894*7c478bd9Sstevel@tonic-gate 				    Gen.g_nam_p : Gen.g_attrfnam_p,
3895*7c478bd9Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
3896*7c478bd9Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
3897*7c478bd9Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
3898*7c478bd9Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
3899*7c478bd9Sstevel@tonic-gate 			}
3900*7c478bd9Sstevel@tonic-gate 		}
3901*7c478bd9Sstevel@tonic-gate 	}
3902*7c478bd9Sstevel@tonic-gate 
3903*7c478bd9Sstevel@tonic-gate 	/*
3904*7c478bd9Sstevel@tonic-gate 	 * Get ACL info: dont bother allocating space if there are only
3905*7c478bd9Sstevel@tonic-gate 	 * standard permissions, i.e. ACL count < 4
3906*7c478bd9Sstevel@tonic-gate 	 */
3907*7c478bd9Sstevel@tonic-gate 	if ((SrcSt.st_mode & Ftype) != S_IFLNK && Pflag) {
3908*7c478bd9Sstevel@tonic-gate 		if ((aclcnt = acl(Gen.g_nam_p, GETACLCNT, 0, NULL)) < 0)
3909*7c478bd9Sstevel@tonic-gate 			msg(ERRN, "Error with acl() of \"%s\"", Gen.g_nam_p);
3910*7c478bd9Sstevel@tonic-gate 		if (aclcnt > MIN_ACL_ENTRIES) {
3911*7c478bd9Sstevel@tonic-gate 			aclp = e_zalloc(E_EXIT, sizeof (aclent_t) * aclcnt);
3912*7c478bd9Sstevel@tonic-gate 
3913*7c478bd9Sstevel@tonic-gate 			if (acl(Gen.g_nam_p, GETACL, aclcnt, aclp) < 0) {
3914*7c478bd9Sstevel@tonic-gate 				msg(ERRN,
3915*7c478bd9Sstevel@tonic-gate 				    "Error with getacl() of \"%s\"",
3916*7c478bd9Sstevel@tonic-gate 				    Gen.g_nam_p);
3917*7c478bd9Sstevel@tonic-gate 				free(aclp);
3918*7c478bd9Sstevel@tonic-gate 				aclp = NULL;
3919*7c478bd9Sstevel@tonic-gate 			}
3920*7c478bd9Sstevel@tonic-gate 		}
3921*7c478bd9Sstevel@tonic-gate 	/* else: only traditional permissions, so proceed as usual */
3922*7c478bd9Sstevel@tonic-gate 	}
3923*7c478bd9Sstevel@tonic-gate 	if (creat_hdr())
3924*7c478bd9Sstevel@tonic-gate 		return (1);
3925*7c478bd9Sstevel@tonic-gate 	else return (2);
3926*7c478bd9Sstevel@tonic-gate }
3927*7c478bd9Sstevel@tonic-gate 
3928*7c478bd9Sstevel@tonic-gate /*
3929*7c478bd9Sstevel@tonic-gate  * getpats: Save any filenames/patterns specified as arguments.
3930*7c478bd9Sstevel@tonic-gate  * Read additional filenames/patterns from the file specified by the
3931*7c478bd9Sstevel@tonic-gate  * user.  The filenames/patterns must occur one per line.
3932*7c478bd9Sstevel@tonic-gate  */
3933*7c478bd9Sstevel@tonic-gate 
3934*7c478bd9Sstevel@tonic-gate static void
3935*7c478bd9Sstevel@tonic-gate getpats(int largc, char **largv)
3936*7c478bd9Sstevel@tonic-gate {
3937*7c478bd9Sstevel@tonic-gate 	char **t_pp;
3938*7c478bd9Sstevel@tonic-gate 	size_t len;
3939*7c478bd9Sstevel@tonic-gate 	unsigned numpat = largc, maxpat = largc + 2;
3940*7c478bd9Sstevel@tonic-gate 
3941*7c478bd9Sstevel@tonic-gate 	Pat_pp = e_zalloc(E_EXIT, maxpat * sizeof (char *));
3942*7c478bd9Sstevel@tonic-gate 	t_pp = Pat_pp;
3943*7c478bd9Sstevel@tonic-gate 	while (*largv) {
3944*7c478bd9Sstevel@tonic-gate 		*t_pp = e_zalloc(E_EXIT, strlen(*largv) + 1);
3945*7c478bd9Sstevel@tonic-gate 		(void) strcpy(*t_pp, *largv);
3946*7c478bd9Sstevel@tonic-gate 		t_pp++;
3947*7c478bd9Sstevel@tonic-gate 		largv++;
3948*7c478bd9Sstevel@tonic-gate 	}
3949*7c478bd9Sstevel@tonic-gate 	while (fgets(Nam_p, Max_namesz + 1, Ef_p) != (char *)NULL) {
3950*7c478bd9Sstevel@tonic-gate 		if (numpat == maxpat - 1) {
3951*7c478bd9Sstevel@tonic-gate 			maxpat += 10;
3952*7c478bd9Sstevel@tonic-gate 			Pat_pp = e_realloc(E_EXIT, Pat_pp,
3953*7c478bd9Sstevel@tonic-gate 			    maxpat * sizeof (char *));
3954*7c478bd9Sstevel@tonic-gate 			t_pp = Pat_pp + numpat;
3955*7c478bd9Sstevel@tonic-gate 		}
3956*7c478bd9Sstevel@tonic-gate 		len = strlen(Nam_p); /* includes the \n */
3957*7c478bd9Sstevel@tonic-gate 		*(Nam_p + len - 1) = '\0'; /* remove the \n */
3958*7c478bd9Sstevel@tonic-gate 		*t_pp = e_zalloc(E_EXIT, len);
3959*7c478bd9Sstevel@tonic-gate 		(void) strcpy(*t_pp, Nam_p);
3960*7c478bd9Sstevel@tonic-gate 		t_pp++;
3961*7c478bd9Sstevel@tonic-gate 		numpat++;
3962*7c478bd9Sstevel@tonic-gate 	}
3963*7c478bd9Sstevel@tonic-gate 	*t_pp = (char *)NULL;
3964*7c478bd9Sstevel@tonic-gate }
3965*7c478bd9Sstevel@tonic-gate 
3966*7c478bd9Sstevel@tonic-gate static void
3967*7c478bd9Sstevel@tonic-gate ioerror(int dir)
3968*7c478bd9Sstevel@tonic-gate {
3969*7c478bd9Sstevel@tonic-gate 	int t_errno;
3970*7c478bd9Sstevel@tonic-gate 
3971*7c478bd9Sstevel@tonic-gate 	t_errno = errno;
3972*7c478bd9Sstevel@tonic-gate 	errno = 0;
3973*7c478bd9Sstevel@tonic-gate 	if (fstat(Archive, &ArchSt) < 0)
3974*7c478bd9Sstevel@tonic-gate 		msg(EXTN, "Error during stat() of archive");
3975*7c478bd9Sstevel@tonic-gate 	errno = t_errno;
3976*7c478bd9Sstevel@tonic-gate 	if ((ArchSt.st_mode & Ftype) != S_IFCHR) {
3977*7c478bd9Sstevel@tonic-gate 		if (dir) {
3978*7c478bd9Sstevel@tonic-gate 			if (errno == EFBIG)
3979*7c478bd9Sstevel@tonic-gate 				msg(EXT, "ulimit reached for output file.");
3980*7c478bd9Sstevel@tonic-gate 			else if (errno == ENOSPC)
3981*7c478bd9Sstevel@tonic-gate 				msg(EXT, "No space left for output file.");
3982*7c478bd9Sstevel@tonic-gate 			else
3983*7c478bd9Sstevel@tonic-gate 				msg(EXTN, "I/O error - cannot continue");
3984*7c478bd9Sstevel@tonic-gate 		} else
3985*7c478bd9Sstevel@tonic-gate 			msg(EXT, "Unexpected end-of-file encountered.");
3986*7c478bd9Sstevel@tonic-gate 	} else
3987*7c478bd9Sstevel@tonic-gate 		msg(EXTN, "\007I/O error on \"%s\"", dir ? "output" : "input");
3988*7c478bd9Sstevel@tonic-gate }
3989*7c478bd9Sstevel@tonic-gate 
3990*7c478bd9Sstevel@tonic-gate /*
3991*7c478bd9Sstevel@tonic-gate  * matched: Determine if a filename matches the specified pattern(s).  If the
3992*7c478bd9Sstevel@tonic-gate  * pattern is matched (the second return), return 0 if -f was specified, else
3993*7c478bd9Sstevel@tonic-gate  * return != 0.  If the pattern is not matched (the first and third
3994*7c478bd9Sstevel@tonic-gate  * returns), return 0 if -f was not specified, else return != 0.
3995*7c478bd9Sstevel@tonic-gate  */
3996*7c478bd9Sstevel@tonic-gate 
3997*7c478bd9Sstevel@tonic-gate static int
3998*7c478bd9Sstevel@tonic-gate matched(void)
3999*7c478bd9Sstevel@tonic-gate {
4000*7c478bd9Sstevel@tonic-gate 	char *str_p = G_p->g_nam_p;
4001*7c478bd9Sstevel@tonic-gate 	char **pat_pp = Pat_pp;
4002*7c478bd9Sstevel@tonic-gate 	int negatep, result;
4003*7c478bd9Sstevel@tonic-gate 
4004*7c478bd9Sstevel@tonic-gate 	/*
4005*7c478bd9Sstevel@tonic-gate 	 * Check for attribute
4006*7c478bd9Sstevel@tonic-gate 	 */
4007*7c478bd9Sstevel@tonic-gate 	if (G_p->g_attrfnam_p != (char *)NULL)
4008*7c478bd9Sstevel@tonic-gate 		str_p = G_p->g_attrfnam_p;
4009*7c478bd9Sstevel@tonic-gate 
4010*7c478bd9Sstevel@tonic-gate 	for (pat_pp = Pat_pp; *pat_pp; pat_pp++) {
4011*7c478bd9Sstevel@tonic-gate 		negatep = (**pat_pp == '!');
4012*7c478bd9Sstevel@tonic-gate 
4013*7c478bd9Sstevel@tonic-gate 		result = fnmatch(negatep ? (*pat_pp+1) : *pat_pp, str_p, 0);
4014*7c478bd9Sstevel@tonic-gate 
4015*7c478bd9Sstevel@tonic-gate 		if (result != 0 && result != FNM_NOMATCH) {
4016*7c478bd9Sstevel@tonic-gate 			msg(POST, "error matching file %s with pattern"
4017*7c478bd9Sstevel@tonic-gate 			    " %s\n", str_p, *pat_pp);
4018*7c478bd9Sstevel@tonic-gate 			return (Args & OCf);
4019*7c478bd9Sstevel@tonic-gate 		}
4020*7c478bd9Sstevel@tonic-gate 
4021*7c478bd9Sstevel@tonic-gate 		if ((result == 0 && ! negatep) ||
4022*7c478bd9Sstevel@tonic-gate 		    (result == FNM_NOMATCH && negatep)) {
4023*7c478bd9Sstevel@tonic-gate 			/* match occured */
4024*7c478bd9Sstevel@tonic-gate 			return (!(Args & OCf));
4025*7c478bd9Sstevel@tonic-gate 		}
4026*7c478bd9Sstevel@tonic-gate 	}
4027*7c478bd9Sstevel@tonic-gate 	return (Args & OCf); /* not matched */
4028*7c478bd9Sstevel@tonic-gate }
4029*7c478bd9Sstevel@tonic-gate 
4030*7c478bd9Sstevel@tonic-gate /*
4031*7c478bd9Sstevel@tonic-gate  * missdir: Create missing directories for files.
4032*7c478bd9Sstevel@tonic-gate  * (Possible future performance enhancement, if missdir is called, we know
4033*7c478bd9Sstevel@tonic-gate  * that at least the very last directory of the path does not exist, therefore,
4034*7c478bd9Sstevel@tonic-gate  * scan the path from the end
4035*7c478bd9Sstevel@tonic-gate  */
4036*7c478bd9Sstevel@tonic-gate 
4037*7c478bd9Sstevel@tonic-gate static int
4038*7c478bd9Sstevel@tonic-gate missdir(char *nam_p)
4039*7c478bd9Sstevel@tonic-gate {
4040*7c478bd9Sstevel@tonic-gate 	char *c_p;
4041*7c478bd9Sstevel@tonic-gate 	int cnt = 2;
4042*7c478bd9Sstevel@tonic-gate 	char *lastp;
4043*7c478bd9Sstevel@tonic-gate 
4044*7c478bd9Sstevel@tonic-gate 	if (*(c_p = nam_p) == '/') /* skip over 'root slash' */
4045*7c478bd9Sstevel@tonic-gate 		c_p++;
4046*7c478bd9Sstevel@tonic-gate 
4047*7c478bd9Sstevel@tonic-gate 	lastp = c_p + strlen(nam_p) - 1;
4048*7c478bd9Sstevel@tonic-gate 	if (*lastp == '/')
4049*7c478bd9Sstevel@tonic-gate 		*lastp = '\0';
4050*7c478bd9Sstevel@tonic-gate 
4051*7c478bd9Sstevel@tonic-gate 	for (; *c_p; ++c_p) {
4052*7c478bd9Sstevel@tonic-gate 		if (*c_p == '/') {
4053*7c478bd9Sstevel@tonic-gate 			*c_p = '\0';
4054*7c478bd9Sstevel@tonic-gate 			if (stat(nam_p, &DesSt) < 0) {
4055*7c478bd9Sstevel@tonic-gate 				if (Args & OCd) {
4056*7c478bd9Sstevel@tonic-gate 					cnt = mkdir(nam_p, Def_mode);
4057*7c478bd9Sstevel@tonic-gate 					if (cnt != 0) {
4058*7c478bd9Sstevel@tonic-gate 						*c_p = '/';
4059*7c478bd9Sstevel@tonic-gate 						return (cnt);
4060*7c478bd9Sstevel@tonic-gate 					}
4061*7c478bd9Sstevel@tonic-gate 				} else {
4062*7c478bd9Sstevel@tonic-gate 					msg(ERR, "Missing -d option.");
4063*7c478bd9Sstevel@tonic-gate 					*c_p = '/';
4064*7c478bd9Sstevel@tonic-gate 					return (-1);
4065*7c478bd9Sstevel@tonic-gate 				}
4066*7c478bd9Sstevel@tonic-gate 			}
4067*7c478bd9Sstevel@tonic-gate 			*c_p = '/';
4068*7c478bd9Sstevel@tonic-gate 		}
4069*7c478bd9Sstevel@tonic-gate 	}
4070*7c478bd9Sstevel@tonic-gate 	if (cnt == 2) /* the file already exists */
4071*7c478bd9Sstevel@tonic-gate 		cnt = 0;
4072*7c478bd9Sstevel@tonic-gate 	return (cnt);
4073*7c478bd9Sstevel@tonic-gate }
4074*7c478bd9Sstevel@tonic-gate 
4075*7c478bd9Sstevel@tonic-gate /*
4076*7c478bd9Sstevel@tonic-gate  * mklong: Convert two shorts into one long.  For VAX, Interdata ...
4077*7c478bd9Sstevel@tonic-gate  */
4078*7c478bd9Sstevel@tonic-gate 
4079*7c478bd9Sstevel@tonic-gate static long
4080*7c478bd9Sstevel@tonic-gate mklong(short v[])
4081*7c478bd9Sstevel@tonic-gate {
4082*7c478bd9Sstevel@tonic-gate 
4083*7c478bd9Sstevel@tonic-gate 	union swpbuf swp_b;
4084*7c478bd9Sstevel@tonic-gate 
4085*7c478bd9Sstevel@tonic-gate 	swp_b.s_word = 1;
4086*7c478bd9Sstevel@tonic-gate 	if (swp_b.s_byte[0]) {
4087*7c478bd9Sstevel@tonic-gate 		swp_b.s_half[0] = v[1];
4088*7c478bd9Sstevel@tonic-gate 		swp_b.s_half[1] = v[0];
4089*7c478bd9Sstevel@tonic-gate 	} else {
4090*7c478bd9Sstevel@tonic-gate 		swp_b.s_half[0] = v[0];
4091*7c478bd9Sstevel@tonic-gate 		swp_b.s_half[1] = v[1];
4092*7c478bd9Sstevel@tonic-gate 	}
4093*7c478bd9Sstevel@tonic-gate 	return (swp_b.s_word);
4094*7c478bd9Sstevel@tonic-gate }
4095*7c478bd9Sstevel@tonic-gate 
4096*7c478bd9Sstevel@tonic-gate /*
4097*7c478bd9Sstevel@tonic-gate  * mkshort: Convert a long into 2 shorts, for VAX, Interdata ...
4098*7c478bd9Sstevel@tonic-gate  */
4099*7c478bd9Sstevel@tonic-gate 
4100*7c478bd9Sstevel@tonic-gate static void
4101*7c478bd9Sstevel@tonic-gate mkshort(short sval[], long v)
4102*7c478bd9Sstevel@tonic-gate {
4103*7c478bd9Sstevel@tonic-gate 	union swpbuf *swp_p, swp_b;
4104*7c478bd9Sstevel@tonic-gate 
4105*7c478bd9Sstevel@tonic-gate 	/* LINTED alignment */
4106*7c478bd9Sstevel@tonic-gate 	swp_p = (union swpbuf *)sval;
4107*7c478bd9Sstevel@tonic-gate 	swp_b.s_word = 1;
4108*7c478bd9Sstevel@tonic-gate 	if (swp_b.s_byte[0]) {
4109*7c478bd9Sstevel@tonic-gate 		swp_b.s_word = v;
4110*7c478bd9Sstevel@tonic-gate 		swp_p->s_half[0] = swp_b.s_half[1];
4111*7c478bd9Sstevel@tonic-gate 		swp_p->s_half[1] = swp_b.s_half[0];
4112*7c478bd9Sstevel@tonic-gate 	} else {
4113*7c478bd9Sstevel@tonic-gate 		swp_b.s_word = v;
4114*7c478bd9Sstevel@tonic-gate 		swp_p->s_half[0] = swp_b.s_half[0];
4115*7c478bd9Sstevel@tonic-gate 		swp_p->s_half[1] = swp_b.s_half[1];
4116*7c478bd9Sstevel@tonic-gate 	}
4117*7c478bd9Sstevel@tonic-gate }
4118*7c478bd9Sstevel@tonic-gate 
4119*7c478bd9Sstevel@tonic-gate /*
4120*7c478bd9Sstevel@tonic-gate  * msg: Print either a message (no error) (POST), an error message with or
4121*7c478bd9Sstevel@tonic-gate  * without the errno (ERRN or ERR), or print an error message with or without
4122*7c478bd9Sstevel@tonic-gate  * the errno and exit (EXTN or EXT).
4123*7c478bd9Sstevel@tonic-gate  */
4124*7c478bd9Sstevel@tonic-gate 
4125*7c478bd9Sstevel@tonic-gate static void
4126*7c478bd9Sstevel@tonic-gate msg(int severity, const char *fmt, ...)
4127*7c478bd9Sstevel@tonic-gate {
4128*7c478bd9Sstevel@tonic-gate 	FILE *file_p;
4129*7c478bd9Sstevel@tonic-gate 	va_list ap;
4130*7c478bd9Sstevel@tonic-gate 
4131*7c478bd9Sstevel@tonic-gate 	if ((Args & OCV) && Verbcnt) { /* clear current line of dots */
4132*7c478bd9Sstevel@tonic-gate 		(void) fputc('\n', Out_p);
4133*7c478bd9Sstevel@tonic-gate 		Verbcnt = 0;
4134*7c478bd9Sstevel@tonic-gate 	}
4135*7c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
4136*7c478bd9Sstevel@tonic-gate 	if (severity == POST)
4137*7c478bd9Sstevel@tonic-gate 		file_p = Out_p;
4138*7c478bd9Sstevel@tonic-gate 	else
4139*7c478bd9Sstevel@tonic-gate 		if (severity == EPOST)
4140*7c478bd9Sstevel@tonic-gate 			file_p = Err_p;
4141*7c478bd9Sstevel@tonic-gate 		else {
4142*7c478bd9Sstevel@tonic-gate 			file_p = Err_p;
4143*7c478bd9Sstevel@tonic-gate 			Error_cnt++;
4144*7c478bd9Sstevel@tonic-gate 		}
4145*7c478bd9Sstevel@tonic-gate 	(void) fflush(Out_p);
4146*7c478bd9Sstevel@tonic-gate 	(void) fflush(Err_p);
4147*7c478bd9Sstevel@tonic-gate 	if ((severity != POST) && (severity != EPOST))
4148*7c478bd9Sstevel@tonic-gate 		(void) fprintf(file_p, "cpio: ");
4149*7c478bd9Sstevel@tonic-gate 
4150*7c478bd9Sstevel@tonic-gate 	/* gettext replaces version of string */
4151*7c478bd9Sstevel@tonic-gate 
4152*7c478bd9Sstevel@tonic-gate 	(void) vfprintf(file_p, gettext(fmt), ap);
4153*7c478bd9Sstevel@tonic-gate 	if (severity == ERRN || severity == EXTN) {
4154*7c478bd9Sstevel@tonic-gate 		(void) fprintf(file_p, ", errno %d, ", errno);
4155*7c478bd9Sstevel@tonic-gate 		perror("");
4156*7c478bd9Sstevel@tonic-gate 	} else
4157*7c478bd9Sstevel@tonic-gate 		(void) fprintf(file_p, "\n");
4158*7c478bd9Sstevel@tonic-gate 	(void) fflush(file_p);
4159*7c478bd9Sstevel@tonic-gate 	va_end(ap);
4160*7c478bd9Sstevel@tonic-gate 	if (severity == EXT || severity == EXTN) {
4161*7c478bd9Sstevel@tonic-gate 		(void) fprintf(file_p, gettext("%d errors\n"), Error_cnt);
4162*7c478bd9Sstevel@tonic-gate 		exit(EXIT_CODE);
4163*7c478bd9Sstevel@tonic-gate 	}
4164*7c478bd9Sstevel@tonic-gate }
4165*7c478bd9Sstevel@tonic-gate 
4166*7c478bd9Sstevel@tonic-gate /*
4167*7c478bd9Sstevel@tonic-gate  * openout: Open files for output and set all necessary information.
4168*7c478bd9Sstevel@tonic-gate  * If the u option is set (unconditionally overwrite existing files),
4169*7c478bd9Sstevel@tonic-gate  * and the current file exists, get a temporary file name from mktemp(3C),
4170*7c478bd9Sstevel@tonic-gate  * link the temporary file to the existing file, and remove the existing file.
4171*7c478bd9Sstevel@tonic-gate  * Finally either creat(2), mkdir(2) or mknod(2) as appropriate.
4172*7c478bd9Sstevel@tonic-gate  *
4173*7c478bd9Sstevel@tonic-gate  */
4174*7c478bd9Sstevel@tonic-gate 
4175*7c478bd9Sstevel@tonic-gate static int
4176*7c478bd9Sstevel@tonic-gate openout(int dirfd)
4177*7c478bd9Sstevel@tonic-gate {
4178*7c478bd9Sstevel@tonic-gate 	char *nam_p;
4179*7c478bd9Sstevel@tonic-gate 	int cnt, result;
4180*7c478bd9Sstevel@tonic-gate 
4181*7c478bd9Sstevel@tonic-gate 	Do_rename = 0;	/* creat_tmp() may reset this */
4182*7c478bd9Sstevel@tonic-gate 
4183*7c478bd9Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
4184*7c478bd9Sstevel@tonic-gate 		nam_p = G_p->g_attrnam_p;
4185*7c478bd9Sstevel@tonic-gate 	} else {
4186*7c478bd9Sstevel@tonic-gate 		if (Args & OCp) {
4187*7c478bd9Sstevel@tonic-gate 			nam_p = Fullnam_p;
4188*7c478bd9Sstevel@tonic-gate 		} else {
4189*7c478bd9Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
4190*7c478bd9Sstevel@tonic-gate 		}
4191*7c478bd9Sstevel@tonic-gate 	}
4192*7c478bd9Sstevel@tonic-gate 
4193*7c478bd9Sstevel@tonic-gate 
4194*7c478bd9Sstevel@tonic-gate 	if ((Max_filesz != RLIM_INFINITY) &&
4195*7c478bd9Sstevel@tonic-gate 	    (Max_filesz < (G_p->g_filesz >> 9))) {
4196*7c478bd9Sstevel@tonic-gate 		/* ... divided by 512 ... */
4197*7c478bd9Sstevel@tonic-gate 		msg(ERR, "Skipping \"%s%s%s\": exceeds ulimit by %lld bytes",
4198*7c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
4199*7c478bd9Sstevel@tonic-gate 		    nam_p : G_p->g_attrfnam_p,
4200*7c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
4201*7c478bd9Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
4202*7c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
4203*7c478bd9Sstevel@tonic-gate 		    "" : nam_p,
4204*7c478bd9Sstevel@tonic-gate 		    (off_t)(G_p->g_filesz - (Max_filesz << 9)));
4205*7c478bd9Sstevel@tonic-gate 		return (-1);
4206*7c478bd9Sstevel@tonic-gate 	}
4207*7c478bd9Sstevel@tonic-gate 
4208*7c478bd9Sstevel@tonic-gate 	if (LSTAT(dirfd, nam_p, &DesSt) == 0) {
4209*7c478bd9Sstevel@tonic-gate 		/*
4210*7c478bd9Sstevel@tonic-gate 		 * A file by the same name exists.  Move it to a temporary
4211*7c478bd9Sstevel@tonic-gate 		 * file.
4212*7c478bd9Sstevel@tonic-gate 		 */
4213*7c478bd9Sstevel@tonic-gate 
4214*7c478bd9Sstevel@tonic-gate 		if (creat_tmp(nam_p) < 0) {
4215*7c478bd9Sstevel@tonic-gate 			/*
4216*7c478bd9Sstevel@tonic-gate 			 * We weren't able to create the temp file.  Report
4217*7c478bd9Sstevel@tonic-gate 			 * failure.
4218*7c478bd9Sstevel@tonic-gate 			 */
4219*7c478bd9Sstevel@tonic-gate 
4220*7c478bd9Sstevel@tonic-gate 			return (-1);
4221*7c478bd9Sstevel@tonic-gate 		}
4222*7c478bd9Sstevel@tonic-gate 	}
4223*7c478bd9Sstevel@tonic-gate 
4224*7c478bd9Sstevel@tonic-gate 	if (Do_rename) {
4225*7c478bd9Sstevel@tonic-gate 		/* nam_p was changed by creat_tmp() above. */
4226*7c478bd9Sstevel@tonic-gate 
4227*7c478bd9Sstevel@tonic-gate 		if (Args & OCp) {
4228*7c478bd9Sstevel@tonic-gate 			if (G_p->g_attrnam_p != (char *)NULL) {
4229*7c478bd9Sstevel@tonic-gate 				nam_p = Attrfile_p;
4230*7c478bd9Sstevel@tonic-gate 			} else {
4231*7c478bd9Sstevel@tonic-gate 				nam_p = Fullnam_p;
4232*7c478bd9Sstevel@tonic-gate 			}
4233*7c478bd9Sstevel@tonic-gate 		} else {
4234*7c478bd9Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
4235*7c478bd9Sstevel@tonic-gate 		}
4236*7c478bd9Sstevel@tonic-gate 	}
4237*7c478bd9Sstevel@tonic-gate 
4238*7c478bd9Sstevel@tonic-gate 	/*
4239*7c478bd9Sstevel@tonic-gate 	 * This pile tries to create the file directly, and, if there is a
4240*7c478bd9Sstevel@tonic-gate 	 * problem, creates missing directories, and then tries to create the
4241*7c478bd9Sstevel@tonic-gate 	 * file again.  Two strikes and you're out.
4242*7c478bd9Sstevel@tonic-gate 	 *
4243*7c478bd9Sstevel@tonic-gate 	 * On XATTR system, the directory has already been created by
4244*7c478bd9Sstevel@tonic-gate 	 * open_dirfd(), so error shouldn't happen in the loop. However,
4245*7c478bd9Sstevel@tonic-gate 	 * on non-XATTR system, symlink/open may fail with ENOENT. In such
4246*7c478bd9Sstevel@tonic-gate 	 * case, we go to create missing directories.
4247*7c478bd9Sstevel@tonic-gate 	 */
4248*7c478bd9Sstevel@tonic-gate 
4249*7c478bd9Sstevel@tonic-gate 	cnt = 0;
4250*7c478bd9Sstevel@tonic-gate 
4251*7c478bd9Sstevel@tonic-gate 	do {
4252*7c478bd9Sstevel@tonic-gate 		errno = 0;
4253*7c478bd9Sstevel@tonic-gate 
4254*7c478bd9Sstevel@tonic-gate 		if (Hdr_type == TAR && Thdr_p->tbuf.t_typeflag == SYMTYPE) {
4255*7c478bd9Sstevel@tonic-gate 			/* The archive file is a TAR symlink. */
4256*7c478bd9Sstevel@tonic-gate 			if ((result =
4257*7c478bd9Sstevel@tonic-gate 			    symlink(Thdr_p->tbuf.t_linkname, nam_p)) >= 0) {
4258*7c478bd9Sstevel@tonic-gate 				cnt = 0;
4259*7c478bd9Sstevel@tonic-gate 				if (Over_p != NULL) {
4260*7c478bd9Sstevel@tonic-gate 					(void) unlinkat(dirfd,
4261*7c478bd9Sstevel@tonic-gate 					    get_component(Over_p), 0);
4262*7c478bd9Sstevel@tonic-gate 					*Over_p = '\0';
4263*7c478bd9Sstevel@tonic-gate 				}
4264*7c478bd9Sstevel@tonic-gate 				break;
4265*7c478bd9Sstevel@tonic-gate 			} else if (errno != ENOENT) {
4266*7c478bd9Sstevel@tonic-gate 				/* The attempt to symlink failed. */
4267*7c478bd9Sstevel@tonic-gate 				msg(ERRN,
4268*7c478bd9Sstevel@tonic-gate 				    "Cannot create symbolic link \"%s\" -> "
4269*7c478bd9Sstevel@tonic-gate 				    "\"%s\"",
4270*7c478bd9Sstevel@tonic-gate 				    Thdr_p->tbuf.t_linkname, nam_p);
4271*7c478bd9Sstevel@tonic-gate 
4272*7c478bd9Sstevel@tonic-gate 				if (*Over_p != '\0') {
4273*7c478bd9Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
4274*7c478bd9Sstevel@tonic-gate 				}
4275*7c478bd9Sstevel@tonic-gate 				return (-1);
4276*7c478bd9Sstevel@tonic-gate 			}
4277*7c478bd9Sstevel@tonic-gate 		} else if (Hdr_type == BAR && bar_linkflag == SYMTYPE) {
4278*7c478bd9Sstevel@tonic-gate 			if ((result = symlink(bar_linkname, nam_p)) >= 0) {
4279*7c478bd9Sstevel@tonic-gate 				cnt = 0;
4280*7c478bd9Sstevel@tonic-gate 				if (Over_p != NULL) {
4281*7c478bd9Sstevel@tonic-gate 					(void) unlinkat(dirfd,
4282*7c478bd9Sstevel@tonic-gate 					    get_component(Over_p), 0);
4283*7c478bd9Sstevel@tonic-gate 					*Over_p = '\0';
4284*7c478bd9Sstevel@tonic-gate 				}
4285*7c478bd9Sstevel@tonic-gate 				break;
4286*7c478bd9Sstevel@tonic-gate 			} else if (errno != ENOENT) {
4287*7c478bd9Sstevel@tonic-gate 				/* The attempt to symlink failed. */
4288*7c478bd9Sstevel@tonic-gate 				msg(ERRN,
4289*7c478bd9Sstevel@tonic-gate 				    "Cannot create symbolic link \"%s\" -> "
4290*7c478bd9Sstevel@tonic-gate 				    "\"%s\"",
4291*7c478bd9Sstevel@tonic-gate 				    bar_linkname, nam_p);
4292*7c478bd9Sstevel@tonic-gate 				if (*Over_p != '\0') {
4293*7c478bd9Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
4294*7c478bd9Sstevel@tonic-gate 				}
4295*7c478bd9Sstevel@tonic-gate 				return (-1);
4296*7c478bd9Sstevel@tonic-gate 			}
4297*7c478bd9Sstevel@tonic-gate 		} else if ((G_p->g_mode & Ftype) == S_IFLNK) {
4298*7c478bd9Sstevel@tonic-gate 			if ((!(Args & OCp)) && !(Hdr_type == USTAR)) {
4299*7c478bd9Sstevel@tonic-gate 				(void) strncpy(Symlnk_p,
4300*7c478bd9Sstevel@tonic-gate 				    Buffr.b_out_p, G_p->g_filesz);
4301*7c478bd9Sstevel@tonic-gate 				*(Symlnk_p + G_p->g_filesz) = '\0';
4302*7c478bd9Sstevel@tonic-gate 			} else if ((!(Args & OCp)) && (Hdr_type == USTAR)) {
4303*7c478bd9Sstevel@tonic-gate 				Symlnk_p[NAMSIZ] = '\0';
4304*7c478bd9Sstevel@tonic-gate 				(void) strncpy(Symlnk_p,
4305*7c478bd9Sstevel@tonic-gate 				    &Thdr_p->tbuf.t_linkname[0], NAMSIZ);
4306*7c478bd9Sstevel@tonic-gate 			}
4307*7c478bd9Sstevel@tonic-gate 			if ((result = symlink(Symlnk_p, nam_p)) >= 0) {
4308*7c478bd9Sstevel@tonic-gate 				cnt = 0;
4309*7c478bd9Sstevel@tonic-gate 				if (Over_p != NULL) {
4310*7c478bd9Sstevel@tonic-gate 					(void) unlinkat(dirfd,
4311*7c478bd9Sstevel@tonic-gate 					    get_component(Over_p), 0);
4312*7c478bd9Sstevel@tonic-gate 					*Over_p = '\0';
4313*7c478bd9Sstevel@tonic-gate 				}
4314*7c478bd9Sstevel@tonic-gate 				break;
4315*7c478bd9Sstevel@tonic-gate 			} else if (errno != ENOENT) {
4316*7c478bd9Sstevel@tonic-gate 				/* The attempt to symlink failed. */
4317*7c478bd9Sstevel@tonic-gate 				msg(ERRN,
4318*7c478bd9Sstevel@tonic-gate 				    "Cannot create symbolic link \"%s\" -> "
4319*7c478bd9Sstevel@tonic-gate 				    "\"%s\"",
4320*7c478bd9Sstevel@tonic-gate 				    Symlnk_p, nam_p);
4321*7c478bd9Sstevel@tonic-gate 
4322*7c478bd9Sstevel@tonic-gate 				if (*Over_p != '\0') {
4323*7c478bd9Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
4324*7c478bd9Sstevel@tonic-gate 				}
4325*7c478bd9Sstevel@tonic-gate 				return (-1);
4326*7c478bd9Sstevel@tonic-gate 			}
4327*7c478bd9Sstevel@tonic-gate 		} else {
4328*7c478bd9Sstevel@tonic-gate 			if ((result = openat(dirfd, get_component(nam_p),
4329*7c478bd9Sstevel@tonic-gate 			    O_CREAT|O_RDWR|O_TRUNC, (int)G_p->g_mode)) >= 0) {
4330*7c478bd9Sstevel@tonic-gate 				/* acl support */
4331*7c478bd9Sstevel@tonic-gate 				acl_set = 0;
4332*7c478bd9Sstevel@tonic-gate 				if (Pflag && aclp != NULL) {
4333*7c478bd9Sstevel@tonic-gate 					if (facl(result, SETACL, aclcnt, aclp)
4334*7c478bd9Sstevel@tonic-gate 					    < 0) {
4335*7c478bd9Sstevel@tonic-gate 						msg(ERRN,
4336*7c478bd9Sstevel@tonic-gate 						    "\"%s\": failed to set acl",
4337*7c478bd9Sstevel@tonic-gate 						    nam_p);
4338*7c478bd9Sstevel@tonic-gate 					} else {
4339*7c478bd9Sstevel@tonic-gate 						acl_set = 1;
4340*7c478bd9Sstevel@tonic-gate 					}
4341*7c478bd9Sstevel@tonic-gate 					free(aclp);
4342*7c478bd9Sstevel@tonic-gate 					aclp = NULL;
4343*7c478bd9Sstevel@tonic-gate 				}
4344*7c478bd9Sstevel@tonic-gate 				cnt = 0;
4345*7c478bd9Sstevel@tonic-gate 				break;
4346*7c478bd9Sstevel@tonic-gate 			} else if (errno != ENOENT) {
4347*7c478bd9Sstevel@tonic-gate 				/* The attempt to open failed. */
4348*7c478bd9Sstevel@tonic-gate 				msg(ERRN, "Cannot open file \"%s\"", nam_p);
4349*7c478bd9Sstevel@tonic-gate 				if (*Over_p != '\0') {
4350*7c478bd9Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
4351*7c478bd9Sstevel@tonic-gate 				}
4352*7c478bd9Sstevel@tonic-gate 				return (-1);
4353*7c478bd9Sstevel@tonic-gate 			}
4354*7c478bd9Sstevel@tonic-gate 		}
4355*7c478bd9Sstevel@tonic-gate 		cnt++;
4356*7c478bd9Sstevel@tonic-gate 	} while (cnt < 2 && missdir(nam_p) == 0);
4357*7c478bd9Sstevel@tonic-gate 
4358*7c478bd9Sstevel@tonic-gate 	switch (cnt) {
4359*7c478bd9Sstevel@tonic-gate 	case 0:
4360*7c478bd9Sstevel@tonic-gate 		if ((Args & OCi) && (Hdr_type == USTAR)) {
4361*7c478bd9Sstevel@tonic-gate 			setpasswd(nam_p);
4362*7c478bd9Sstevel@tonic-gate 		}
4363*7c478bd9Sstevel@tonic-gate 
4364*7c478bd9Sstevel@tonic-gate 		if ((G_p->g_mode & Ftype) == S_IFLNK ||
4365*7c478bd9Sstevel@tonic-gate 		    (Hdr_type == BAR && bar_linkflag == SYMTYPE)) {
4366*7c478bd9Sstevel@tonic-gate 			if (fchownat(dirfd, get_component(nam_p),
4367*7c478bd9Sstevel@tonic-gate 			    (int)G_p->g_uid, (int)G_p->g_gid,
4368*7c478bd9Sstevel@tonic-gate 			    AT_SYMLINK_NOFOLLOW) < 0) {
4369*7c478bd9Sstevel@tonic-gate 				if ((Euid == 0) || (Args & OCR)) {
4370*7c478bd9Sstevel@tonic-gate 					msg(ERRN,
4371*7c478bd9Sstevel@tonic-gate 					    "Error during chown() of "
4372*7c478bd9Sstevel@tonic-gate 					    "\"%s%s%s\"",
4373*7c478bd9Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
4374*7c478bd9Sstevel@tonic-gate 					    nam_p : G_p->g_attrfnam_p,
4375*7c478bd9Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
4376*7c478bd9Sstevel@tonic-gate 					    "" : gettext(" Attribute "),
4377*7c478bd9Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
4378*7c478bd9Sstevel@tonic-gate 					    "" : nam_p);
4379*7c478bd9Sstevel@tonic-gate 				}
4380*7c478bd9Sstevel@tonic-gate 			}
4381*7c478bd9Sstevel@tonic-gate 
4382*7c478bd9Sstevel@tonic-gate 			break;
4383*7c478bd9Sstevel@tonic-gate 		}
4384*7c478bd9Sstevel@tonic-gate 
4385*7c478bd9Sstevel@tonic-gate 		if (fchownat(dirfd, get_component(nam_p),
4386*7c478bd9Sstevel@tonic-gate 		    (int)G_p->g_uid, (int)G_p->g_gid, 0) < 0) {
4387*7c478bd9Sstevel@tonic-gate 			if ((Euid == 0) || (Args & OCR)) {
4388*7c478bd9Sstevel@tonic-gate 				msg(ERRN,
4389*7c478bd9Sstevel@tonic-gate 				    "Error during chown() of \"%s%s%s\"",
4390*7c478bd9Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
4391*7c478bd9Sstevel@tonic-gate 				    nam_p : G_p->g_attrfnam_p,
4392*7c478bd9Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
4393*7c478bd9Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
4394*7c478bd9Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
4395*7c478bd9Sstevel@tonic-gate 				    "" : nam_p);
4396*7c478bd9Sstevel@tonic-gate 			}
4397*7c478bd9Sstevel@tonic-gate 		}
4398*7c478bd9Sstevel@tonic-gate 
4399*7c478bd9Sstevel@tonic-gate 		break;
4400*7c478bd9Sstevel@tonic-gate 
4401*7c478bd9Sstevel@tonic-gate 	case 1:
4402*7c478bd9Sstevel@tonic-gate 		if (Do_rename) {
4403*7c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot create directory for \"%s%s%s\"",
4404*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? Over_p :
4405*7c478bd9Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
4406*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
4407*7c478bd9Sstevel@tonic-gate 			    gettext(" Attribute "),
4408*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" : Over_p);
4409*7c478bd9Sstevel@tonic-gate 		} else {
4410*7c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot create directory for \"%s%s%s\"",
4411*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? nam_p :
4412*7c478bd9Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
4413*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
4414*7c478bd9Sstevel@tonic-gate 			    gettext(" Attribute "),
4415*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" : nam_p);
4416*7c478bd9Sstevel@tonic-gate 		}
4417*7c478bd9Sstevel@tonic-gate 		break;
4418*7c478bd9Sstevel@tonic-gate 
4419*7c478bd9Sstevel@tonic-gate 	case 2:
4420*7c478bd9Sstevel@tonic-gate 		if (Do_rename) {
4421*7c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot create \"%s%s%s\"",
4422*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? Over_p :
4423*7c478bd9Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
4424*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
4425*7c478bd9Sstevel@tonic-gate 			    gettext(" Attribute "),
4426*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
4427*7c478bd9Sstevel@tonic-gate 			    Over_p);
4428*7c478bd9Sstevel@tonic-gate 		} else {
4429*7c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot create \"%s%s%s\"",
4430*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? nam_p :
4431*7c478bd9Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
4432*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
4433*7c478bd9Sstevel@tonic-gate 			    gettext(" Attribute "),
4434*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
4435*7c478bd9Sstevel@tonic-gate 			    nam_p);
4436*7c478bd9Sstevel@tonic-gate 		}
4437*7c478bd9Sstevel@tonic-gate 		break;
4438*7c478bd9Sstevel@tonic-gate 
4439*7c478bd9Sstevel@tonic-gate 	default:
4440*7c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible case.");
4441*7c478bd9Sstevel@tonic-gate 	}
4442*7c478bd9Sstevel@tonic-gate 
4443*7c478bd9Sstevel@tonic-gate 	Finished = 0;
4444*7c478bd9Sstevel@tonic-gate 	return (result);
4445*7c478bd9Sstevel@tonic-gate }
4446*7c478bd9Sstevel@tonic-gate 
4447*7c478bd9Sstevel@tonic-gate /*
4448*7c478bd9Sstevel@tonic-gate  * read_hdr: Transfer headers from the selected format
4449*7c478bd9Sstevel@tonic-gate  * in the archive I/O buffer to the generic structure.
4450*7c478bd9Sstevel@tonic-gate  */
4451*7c478bd9Sstevel@tonic-gate 
4452*7c478bd9Sstevel@tonic-gate static
4453*7c478bd9Sstevel@tonic-gate int
4454*7c478bd9Sstevel@tonic-gate read_hdr(int hdr)
4455*7c478bd9Sstevel@tonic-gate {
4456*7c478bd9Sstevel@tonic-gate 	int rv = NONE;
4457*7c478bd9Sstevel@tonic-gate 	major_t maj, rmaj;
4458*7c478bd9Sstevel@tonic-gate 	minor_t min, rmin;
4459*7c478bd9Sstevel@tonic-gate 	char tmpnull;
4460*7c478bd9Sstevel@tonic-gate 	static int bar_read_cnt = 0;
4461*7c478bd9Sstevel@tonic-gate 
4462*7c478bd9Sstevel@tonic-gate 	if (hdr != BAR) {
4463*7c478bd9Sstevel@tonic-gate 		if (Buffr.b_end_p != (Buffr.b_out_p + Hdrsz)) {
4464*7c478bd9Sstevel@tonic-gate 			tmpnull = *(Buffr.b_out_p + Hdrsz);
4465*7c478bd9Sstevel@tonic-gate 			*(Buffr.b_out_p + Hdrsz) = '\0';
4466*7c478bd9Sstevel@tonic-gate 		}
4467*7c478bd9Sstevel@tonic-gate 	}
4468*7c478bd9Sstevel@tonic-gate 
4469*7c478bd9Sstevel@tonic-gate 	switch (hdr) {
4470*7c478bd9Sstevel@tonic-gate 	case BIN:
4471*7c478bd9Sstevel@tonic-gate 		(void) memcpy(&Hdr, Buffr.b_out_p, HDRSZ);
4472*7c478bd9Sstevel@tonic-gate 		if (Hdr.h_magic == (short)CMN_BBS) {
4473*7c478bd9Sstevel@tonic-gate 			swap((char *)&Hdr, HDRSZ);
4474*7c478bd9Sstevel@tonic-gate 		}
4475*7c478bd9Sstevel@tonic-gate 		Gen.g_magic = Hdr.h_magic;
4476*7c478bd9Sstevel@tonic-gate 		Gen.g_mode = Hdr.h_mode;
4477*7c478bd9Sstevel@tonic-gate 		Gen.g_uid = Hdr.h_uid;
4478*7c478bd9Sstevel@tonic-gate 		Gen.g_gid = Hdr.h_gid;
4479*7c478bd9Sstevel@tonic-gate 		Gen.g_nlink = Hdr.h_nlink;
4480*7c478bd9Sstevel@tonic-gate 		Gen.g_mtime = mklong(Hdr.h_mtime);
4481*7c478bd9Sstevel@tonic-gate 		Gen.g_ino = Hdr.h_ino;
4482*7c478bd9Sstevel@tonic-gate 		Gen.g_dev = Hdr.h_dev;
4483*7c478bd9Sstevel@tonic-gate 		Gen.g_rdev = Hdr.h_rdev;
4484*7c478bd9Sstevel@tonic-gate 		Gen.g_cksum = 0L;
4485*7c478bd9Sstevel@tonic-gate 		Gen.g_filesz = (off_t)mklong(Hdr.h_filesize);
4486*7c478bd9Sstevel@tonic-gate 		Gen.g_namesz = Hdr.h_namesize;
4487*7c478bd9Sstevel@tonic-gate 		rv = BIN;
4488*7c478bd9Sstevel@tonic-gate 		break;
4489*7c478bd9Sstevel@tonic-gate 	case CHR:
4490*7c478bd9Sstevel@tonic-gate 		if (sscanf(Buffr.b_out_p,
4491*7c478bd9Sstevel@tonic-gate 		    "%6lo%6lo%6lo%6lo%6lo%6lo%6lo%6lo%11lo%6o%11llo",
4492*7c478bd9Sstevel@tonic-gate 		    &Gen.g_magic, &Gen.g_dev, &Gen.g_ino, &Gen.g_mode,
4493*7c478bd9Sstevel@tonic-gate 		    &Gen.g_uid, &Gen.g_gid, &Gen.g_nlink, &Gen.g_rdev,
4494*7c478bd9Sstevel@tonic-gate 		    (ulong_t *)&Gen.g_mtime, (uint_t *)&Gen.g_namesz,
4495*7c478bd9Sstevel@tonic-gate 		    (u_off_t *)&Gen.g_filesz) == CHR_CNT) {
4496*7c478bd9Sstevel@tonic-gate 			rv = CHR;
4497*7c478bd9Sstevel@tonic-gate #define	cpioMAJOR(x)	(int)(((unsigned)x >> 8) & 0x7F)
4498*7c478bd9Sstevel@tonic-gate #define	cpioMINOR(x)	(int)(x & 0xFF)
4499*7c478bd9Sstevel@tonic-gate 			maj = cpioMAJOR(Gen.g_dev);
4500*7c478bd9Sstevel@tonic-gate 			rmaj = cpioMAJOR(Gen.g_rdev);
4501*7c478bd9Sstevel@tonic-gate 			min = cpioMINOR(Gen.g_dev);
4502*7c478bd9Sstevel@tonic-gate 			rmin = cpioMINOR(Gen.g_rdev);
4503*7c478bd9Sstevel@tonic-gate 			if (Use_old_stat) {
4504*7c478bd9Sstevel@tonic-gate 				/* needs error checking */
4505*7c478bd9Sstevel@tonic-gate 				Gen.g_dev = (maj << 8) | min;
4506*7c478bd9Sstevel@tonic-gate 				Gen.g_rdev = (rmaj << 8) | rmin;
4507*7c478bd9Sstevel@tonic-gate 			} else {
4508*7c478bd9Sstevel@tonic-gate 				Gen.g_dev = makedev(maj, min);
4509*7c478bd9Sstevel@tonic-gate 				Gen.g_rdev = makedev(rmaj, rmin);
4510*7c478bd9Sstevel@tonic-gate 			}
4511*7c478bd9Sstevel@tonic-gate 		}
4512*7c478bd9Sstevel@tonic-gate 		break;
4513*7c478bd9Sstevel@tonic-gate 	case ASC:
4514*7c478bd9Sstevel@tonic-gate 	case CRC:
4515*7c478bd9Sstevel@tonic-gate 		if (sscanf(Buffr.b_out_p,
4516*7c478bd9Sstevel@tonic-gate 		    "%6lx%8lx%8lx%8lx%8lx%8lx%8lx%8llx%8x%8x%8x%8x%8x%8lx",
4517*7c478bd9Sstevel@tonic-gate 		    &Gen.g_magic, &Gen.g_ino, &Gen.g_mode, &Gen.g_uid,
4518*7c478bd9Sstevel@tonic-gate 		    &Gen.g_gid, &Gen.g_nlink, &Gen.g_mtime,
4519*7c478bd9Sstevel@tonic-gate 		    (u_off_t *)&Gen.g_filesz, (uint_t *)&maj, (uint_t *)&min,
4520*7c478bd9Sstevel@tonic-gate 		    (uint_t *)&rmaj, (uint_t *)&rmin, (uint_t *)&Gen.g_namesz,
4521*7c478bd9Sstevel@tonic-gate 		    &Gen.g_cksum) == ASC_CNT) {
4522*7c478bd9Sstevel@tonic-gate 			Gen.g_dev = makedev(maj, min);
4523*7c478bd9Sstevel@tonic-gate 			Gen.g_rdev = makedev(rmaj, rmin);
4524*7c478bd9Sstevel@tonic-gate 			rv = hdr;
4525*7c478bd9Sstevel@tonic-gate 		}
4526*7c478bd9Sstevel@tonic-gate 		break;
4527*7c478bd9Sstevel@tonic-gate 	case USTAR: /* TAR and USTAR */
4528*7c478bd9Sstevel@tonic-gate 		if (*Buffr.b_out_p == '\0') {
4529*7c478bd9Sstevel@tonic-gate 			*Gen.g_nam_p = '\0';
4530*7c478bd9Sstevel@tonic-gate 			nambuf[0] = '\0';
4531*7c478bd9Sstevel@tonic-gate 		} else {
4532*7c478bd9Sstevel@tonic-gate 			Thdr_p = (union tblock *)Buffr.b_out_p;
4533*7c478bd9Sstevel@tonic-gate 			Gen.g_nam_p[0] = '\0';
4534*7c478bd9Sstevel@tonic-gate 			(void) strncpy((char *)&nambuf,
4535*7c478bd9Sstevel@tonic-gate 			    Thdr_p->tbuf.t_name, NAMSIZ);
4536*7c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mode, "%8lo",
4537*7c478bd9Sstevel@tonic-gate 			    &Gen.g_mode);
4538*7c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_uid, "%8lo", &Gen.g_uid);
4539*7c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_gid, "%8lo", &Gen.g_gid);
4540*7c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_size, "%11llo",
4541*7c478bd9Sstevel@tonic-gate 			    (u_off_t *)&Gen.g_filesz);
4542*7c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mtime, "%12lo",
4543*7c478bd9Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_mtime);
4544*7c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_cksum, "%8lo",
4545*7c478bd9Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_cksum);
4546*7c478bd9Sstevel@tonic-gate 			if (Thdr_p->tbuf.t_linkname[0] != (char)NULL)
4547*7c478bd9Sstevel@tonic-gate 				Gen.g_nlink = 1;
4548*7c478bd9Sstevel@tonic-gate 			else
4549*7c478bd9Sstevel@tonic-gate 				Gen.g_nlink = 0;
4550*7c478bd9Sstevel@tonic-gate 
4551*7c478bd9Sstevel@tonic-gate 			switch (Thdr_p->tbuf.t_typeflag) {
4552*7c478bd9Sstevel@tonic-gate 			case SYMTYPE:
4553*7c478bd9Sstevel@tonic-gate 				/* Symbolic Link */
4554*7c478bd9Sstevel@tonic-gate 				Gen.g_nlink = 2;
4555*7c478bd9Sstevel@tonic-gate 				break;
4556*7c478bd9Sstevel@tonic-gate 			case CHRTYPE:
4557*7c478bd9Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFCHR);
4558*7c478bd9Sstevel@tonic-gate 				break;
4559*7c478bd9Sstevel@tonic-gate 			case BLKTYPE:
4560*7c478bd9Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFBLK);
4561*7c478bd9Sstevel@tonic-gate 				break;
4562*7c478bd9Sstevel@tonic-gate 			case DIRTYPE:
4563*7c478bd9Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFDIR);
4564*7c478bd9Sstevel@tonic-gate 				break;
4565*7c478bd9Sstevel@tonic-gate 			case FIFOTYPE:
4566*7c478bd9Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFIFO);
4567*7c478bd9Sstevel@tonic-gate 				break;
4568*7c478bd9Sstevel@tonic-gate 			}
4569*7c478bd9Sstevel@tonic-gate 
4570*7c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_magic, "%8lo",
4571*7c478bd9Sstevel@tonic-gate 			    /* LINTED alignment */
4572*7c478bd9Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_tmagic);
4573*7c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_version, "%8lo",
4574*7c478bd9Sstevel@tonic-gate 			    /* LINTED alignment */
4575*7c478bd9Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_version);
4576*7c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_uname, "%32s",
4577*7c478bd9Sstevel@tonic-gate 			    (char *)&Gen.g_uname);
4578*7c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_gname, "%32s",
4579*7c478bd9Sstevel@tonic-gate 			    (char *)&Gen.g_gname);
4580*7c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_devmajor, "%8lo",
4581*7c478bd9Sstevel@tonic-gate 			    &Gen.g_dev);
4582*7c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_devminor, "%8lo",
4583*7c478bd9Sstevel@tonic-gate 			    &Gen.g_rdev);
4584*7c478bd9Sstevel@tonic-gate 			(void) strncpy((char *)&prebuf,
4585*7c478bd9Sstevel@tonic-gate 			    Thdr_p->tbuf.t_prefix, PRESIZ);
4586*7c478bd9Sstevel@tonic-gate 			Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
4587*7c478bd9Sstevel@tonic-gate 			Gen.g_dev = makedev(maj, min);
4588*7c478bd9Sstevel@tonic-gate 		}
4589*7c478bd9Sstevel@tonic-gate 		rv = USTAR;
4590*7c478bd9Sstevel@tonic-gate 		break;
4591*7c478bd9Sstevel@tonic-gate 	case TAR:
4592*7c478bd9Sstevel@tonic-gate 		if (*Buffr.b_out_p == '\0') {
4593*7c478bd9Sstevel@tonic-gate 			*Gen.g_nam_p = '\0';
4594*7c478bd9Sstevel@tonic-gate 			nambuf[0] = '\0';
4595*7c478bd9Sstevel@tonic-gate 		} else {
4596*7c478bd9Sstevel@tonic-gate 			Thdr_p = (union tblock *)Buffr.b_out_p;
4597*7c478bd9Sstevel@tonic-gate 			Gen.g_nam_p[0] = '\0';
4598*7c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mode, "%lo", &Gen.g_mode);
4599*7c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_uid, "%lo", &Gen.g_uid);
4600*7c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_gid, "%lo", &Gen.g_gid);
4601*7c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_size, "%llo",
4602*7c478bd9Sstevel@tonic-gate 			    (u_off_t *)&Gen.g_filesz);
4603*7c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mtime, "%lo",
4604*7c478bd9Sstevel@tonic-gate 			    &Gen.g_mtime);
4605*7c478bd9Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_cksum, "%lo",
4606*7c478bd9Sstevel@tonic-gate 			    &Gen.g_cksum);
4607*7c478bd9Sstevel@tonic-gate 			if (Thdr_p->tbuf.t_typeflag == '1')	/* hardlink */
4608*7c478bd9Sstevel@tonic-gate 				Gen.g_nlink = 1;
4609*7c478bd9Sstevel@tonic-gate 			else
4610*7c478bd9Sstevel@tonic-gate 				Gen.g_nlink = 0;
4611*7c478bd9Sstevel@tonic-gate 			(void) strncpy(Gen.g_nam_p,
4612*7c478bd9Sstevel@tonic-gate 			    Thdr_p->tbuf.t_name, NAMSIZ);
4613*7c478bd9Sstevel@tonic-gate 			Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
4614*7c478bd9Sstevel@tonic-gate 			(void) strcpy(nambuf, Gen.g_nam_p);
4615*7c478bd9Sstevel@tonic-gate 		}
4616*7c478bd9Sstevel@tonic-gate 		rv = TAR;
4617*7c478bd9Sstevel@tonic-gate 		break;
4618*7c478bd9Sstevel@tonic-gate 	case BAR:
4619*7c478bd9Sstevel@tonic-gate 		if (Bar_vol_num == 0 && bar_read_cnt == 0) {
4620*7c478bd9Sstevel@tonic-gate 			read_bar_vol_hdr();
4621*7c478bd9Sstevel@tonic-gate 			bar_read_cnt++;
4622*7c478bd9Sstevel@tonic-gate 		}
4623*7c478bd9Sstevel@tonic-gate 		else
4624*7c478bd9Sstevel@tonic-gate 			read_bar_file_hdr();
4625*7c478bd9Sstevel@tonic-gate 		rv = BAR;
4626*7c478bd9Sstevel@tonic-gate 		break;
4627*7c478bd9Sstevel@tonic-gate 	default:
4628*7c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
4629*7c478bd9Sstevel@tonic-gate 	}
4630*7c478bd9Sstevel@tonic-gate 
4631*7c478bd9Sstevel@tonic-gate 	if (hdr != BAR) {
4632*7c478bd9Sstevel@tonic-gate 		if (Buffr.b_end_p != (Buffr.b_out_p + Hdrsz))
4633*7c478bd9Sstevel@tonic-gate 			*(Buffr.b_out_p + Hdrsz) = tmpnull;
4634*7c478bd9Sstevel@tonic-gate 	}
4635*7c478bd9Sstevel@tonic-gate 
4636*7c478bd9Sstevel@tonic-gate 	return (rv);
4637*7c478bd9Sstevel@tonic-gate }
4638*7c478bd9Sstevel@tonic-gate 
4639*7c478bd9Sstevel@tonic-gate /*
4640*7c478bd9Sstevel@tonic-gate  * reclaim: Reclaim linked file structure storage.
4641*7c478bd9Sstevel@tonic-gate  */
4642*7c478bd9Sstevel@tonic-gate 
4643*7c478bd9Sstevel@tonic-gate static void
4644*7c478bd9Sstevel@tonic-gate reclaim(struct Lnk *p)
4645*7c478bd9Sstevel@tonic-gate {
4646*7c478bd9Sstevel@tonic-gate 	p->L_bck_p->L_nxt_p = p->L_nxt_p;
4647*7c478bd9Sstevel@tonic-gate 	p->L_nxt_p->L_bck_p = p->L_bck_p;
4648*7c478bd9Sstevel@tonic-gate 
4649*7c478bd9Sstevel@tonic-gate 	while (p != NULL) {
4650*7c478bd9Sstevel@tonic-gate 		struct Lnk *new_p = p->L_lnk_p;
4651*7c478bd9Sstevel@tonic-gate 
4652*7c478bd9Sstevel@tonic-gate 		free(p->L_gen.g_nam_p);
4653*7c478bd9Sstevel@tonic-gate 		free(p);
4654*7c478bd9Sstevel@tonic-gate 		p = new_p;
4655*7c478bd9Sstevel@tonic-gate 	}
4656*7c478bd9Sstevel@tonic-gate }
4657*7c478bd9Sstevel@tonic-gate 
4658*7c478bd9Sstevel@tonic-gate /*
4659*7c478bd9Sstevel@tonic-gate  * rstbuf: Reset the I/O buffer, move incomplete potential headers to
4660*7c478bd9Sstevel@tonic-gate  * the front of the buffer and force bread() to refill the buffer.  The
4661*7c478bd9Sstevel@tonic-gate  * return value from bread() is returned (to identify I/O errors).  On the
4662*7c478bd9Sstevel@tonic-gate  * 3B2, reads must begin on a word boundary, therefore, with the -i option,
4663*7c478bd9Sstevel@tonic-gate  * any remaining bytes in the buffer must be moved to the base of the buffer
4664*7c478bd9Sstevel@tonic-gate  * in such a way that the destination locations of subsequent reads are
4665*7c478bd9Sstevel@tonic-gate  * word aligned.
4666*7c478bd9Sstevel@tonic-gate  */
4667*7c478bd9Sstevel@tonic-gate 
4668*7c478bd9Sstevel@tonic-gate static void
4669*7c478bd9Sstevel@tonic-gate rstbuf(void)
4670*7c478bd9Sstevel@tonic-gate {
4671*7c478bd9Sstevel@tonic-gate 	int pad;
4672*7c478bd9Sstevel@tonic-gate 
4673*7c478bd9Sstevel@tonic-gate 	if ((Args & OCi) || Append) {
4674*7c478bd9Sstevel@tonic-gate 		if (Buffr.b_out_p != Buffr.b_base_p) {
4675*7c478bd9Sstevel@tonic-gate 			pad = ((Buffr.b_cnt + FULLWD) & ~FULLWD);
4676*7c478bd9Sstevel@tonic-gate 			Buffr.b_in_p = Buffr.b_base_p + pad;
4677*7c478bd9Sstevel@tonic-gate 			pad -= Buffr.b_cnt;
4678*7c478bd9Sstevel@tonic-gate 			(void) memcpy(Buffr.b_base_p + pad, Buffr.b_out_p,
4679*7c478bd9Sstevel@tonic-gate 			    (int)Buffr.b_cnt);
4680*7c478bd9Sstevel@tonic-gate 			Buffr.b_out_p = Buffr.b_base_p + pad;
4681*7c478bd9Sstevel@tonic-gate 		}
4682*7c478bd9Sstevel@tonic-gate 		if (bfill() < 0)
4683*7c478bd9Sstevel@tonic-gate 			msg(EXT, "Unexpected end-of-archive encountered.");
4684*7c478bd9Sstevel@tonic-gate 	} else { /* OCo */
4685*7c478bd9Sstevel@tonic-gate 		(void) memcpy(Buffr.b_base_p, Buffr.b_out_p, (int)Buffr.b_cnt);
4686*7c478bd9Sstevel@tonic-gate 		Buffr.b_out_p = Buffr.b_base_p;
4687*7c478bd9Sstevel@tonic-gate 		Buffr.b_in_p = Buffr.b_base_p + Buffr.b_cnt;
4688*7c478bd9Sstevel@tonic-gate 	}
4689*7c478bd9Sstevel@tonic-gate }
4690*7c478bd9Sstevel@tonic-gate 
4691*7c478bd9Sstevel@tonic-gate static void
4692*7c478bd9Sstevel@tonic-gate setpasswd(char *nam)
4693*7c478bd9Sstevel@tonic-gate {
4694*7c478bd9Sstevel@tonic-gate 	if ((dpasswd = getpwnam(&Gen.g_uname[0])) == (struct passwd *)NULL) {
4695*7c478bd9Sstevel@tonic-gate 		msg(EPOST, "cpio: problem reading passwd entry");
4696*7c478bd9Sstevel@tonic-gate 		msg(EPOST, "cpio: %s: owner not changed", nam);
4697*7c478bd9Sstevel@tonic-gate 		if (Gen.g_uid == UID_NOBODY && S_ISREG(Gen.g_mode))
4698*7c478bd9Sstevel@tonic-gate 			Gen.g_mode &= ~S_ISUID;
4699*7c478bd9Sstevel@tonic-gate 	} else
4700*7c478bd9Sstevel@tonic-gate 		Gen.g_uid = dpasswd->pw_uid;
4701*7c478bd9Sstevel@tonic-gate 
4702*7c478bd9Sstevel@tonic-gate 	if ((dgroup = getgrnam(&Gen.g_gname[0])) == (struct group *)NULL) {
4703*7c478bd9Sstevel@tonic-gate 		msg(EPOST, "cpio: problem reading group entry");
4704*7c478bd9Sstevel@tonic-gate 		msg(EPOST, "cpio: %s: group not changed", nam);
4705*7c478bd9Sstevel@tonic-gate 		if (Gen.g_gid == GID_NOBODY && S_ISREG(Gen.g_mode))
4706*7c478bd9Sstevel@tonic-gate 			Gen.g_mode &= ~S_ISGID;
4707*7c478bd9Sstevel@tonic-gate 	} else
4708*7c478bd9Sstevel@tonic-gate 		Gen.g_gid = dgroup->gr_gid;
4709*7c478bd9Sstevel@tonic-gate 	G_p = &Gen;
4710*7c478bd9Sstevel@tonic-gate }
4711*7c478bd9Sstevel@tonic-gate 
4712*7c478bd9Sstevel@tonic-gate /*
4713*7c478bd9Sstevel@tonic-gate  * rstfiles:  Perform final changes to the file.  If the -u option is set,
4714*7c478bd9Sstevel@tonic-gate  * and overwrite == U_OVER, remove the temporary file, else if overwrite
4715*7c478bd9Sstevel@tonic-gate  * == U_KEEP, unlink the current file, and restore the existing version
4716*7c478bd9Sstevel@tonic-gate  * of the file.  In addition, where appropriate, set the access or modification
4717*7c478bd9Sstevel@tonic-gate  * times, change the owner and change the modes of the file.
4718*7c478bd9Sstevel@tonic-gate  *
4719*7c478bd9Sstevel@tonic-gate  * Note that if Do_rename is set, then the roles of original and temporary
4720*7c478bd9Sstevel@tonic-gate  * file are reversed. If all went well, we will rename() the temporary file
4721*7c478bd9Sstevel@tonic-gate  * over the original in order to accomodate potentially executing files.
4722*7c478bd9Sstevel@tonic-gate  */
4723*7c478bd9Sstevel@tonic-gate static void
4724*7c478bd9Sstevel@tonic-gate rstfiles(int over, int dirfd)
4725*7c478bd9Sstevel@tonic-gate {
4726*7c478bd9Sstevel@tonic-gate 	char *inam_p, *onam_p, *nam_p;
4727*7c478bd9Sstevel@tonic-gate 	int error;
4728*7c478bd9Sstevel@tonic-gate 
4729*7c478bd9Sstevel@tonic-gate 	if (Args & OCp) {
4730*7c478bd9Sstevel@tonic-gate 		if (G_p->g_attrnam_p == (char *)NULL) {
4731*7c478bd9Sstevel@tonic-gate 			nam_p = Fullnam_p;
4732*7c478bd9Sstevel@tonic-gate 		} else {
4733*7c478bd9Sstevel@tonic-gate 			nam_p = G_p->g_attrnam_p;
4734*7c478bd9Sstevel@tonic-gate 		}
4735*7c478bd9Sstevel@tonic-gate 	} else {
4736*7c478bd9Sstevel@tonic-gate 		if (Gen.g_nlink > (ulong_t)0) {
4737*7c478bd9Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
4738*7c478bd9Sstevel@tonic-gate 		} else {
4739*7c478bd9Sstevel@tonic-gate 			nam_p = Gen.g_nam_p;
4740*7c478bd9Sstevel@tonic-gate 		}
4741*7c478bd9Sstevel@tonic-gate 	}
4742*7c478bd9Sstevel@tonic-gate 	if (Gen.g_attrnam_p != (char *)NULL) {
4743*7c478bd9Sstevel@tonic-gate 		nam_p = Gen.g_attrnam_p;
4744*7c478bd9Sstevel@tonic-gate 	}
4745*7c478bd9Sstevel@tonic-gate 
4746*7c478bd9Sstevel@tonic-gate 	if ((Args & OCi) && (Hdr_type == USTAR)) {
4747*7c478bd9Sstevel@tonic-gate 		setpasswd(nam_p);
4748*7c478bd9Sstevel@tonic-gate 	}
4749*7c478bd9Sstevel@tonic-gate 	if (over == U_KEEP && *Over_p != '\0') {
4750*7c478bd9Sstevel@tonic-gate 		if (Do_rename) {
4751*7c478bd9Sstevel@tonic-gate 			msg(POST, "Restoring existing \"%s%s%s\"",
4752*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
4753*7c478bd9Sstevel@tonic-gate 			    Over_p : Fullnam_p,
4754*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
4755*7c478bd9Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
4756*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
4757*7c478bd9Sstevel@tonic-gate 			    "" : Over_p);
4758*7c478bd9Sstevel@tonic-gate 		} else {
4759*7c478bd9Sstevel@tonic-gate 			msg(POST, "Restoring existing \"%s%s%s\"",
4760*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
4761*7c478bd9Sstevel@tonic-gate 			    nam_p : Fullnam_p,
4762*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
4763*7c478bd9Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
4764*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
4765*7c478bd9Sstevel@tonic-gate 			    "" : nam_p);
4766*7c478bd9Sstevel@tonic-gate 		}
4767*7c478bd9Sstevel@tonic-gate 
4768*7c478bd9Sstevel@tonic-gate 		/* delete what we just built */
4769*7c478bd9Sstevel@tonic-gate 		(void) unlinkat(dirfd, get_component(nam_p), 0);
4770*7c478bd9Sstevel@tonic-gate 
4771*7c478bd9Sstevel@tonic-gate 		/* If the old file needs restoring, do the necessary links */
4772*7c478bd9Sstevel@tonic-gate 		if (Do_rename) {
4773*7c478bd9Sstevel@tonic-gate 			char *tmp_ptr;
4774*7c478bd9Sstevel@tonic-gate 
4775*7c478bd9Sstevel@tonic-gate 			if (Args & OCp) {
4776*7c478bd9Sstevel@tonic-gate 				tmp_ptr = Fullnam_p;
4777*7c478bd9Sstevel@tonic-gate 				Fullnam_p = Over_p;
4778*7c478bd9Sstevel@tonic-gate 			} else {
4779*7c478bd9Sstevel@tonic-gate 				tmp_ptr = G_p->g_nam_p;
4780*7c478bd9Sstevel@tonic-gate 				G_p->g_nam_p = Over_p;
4781*7c478bd9Sstevel@tonic-gate 			}
4782*7c478bd9Sstevel@tonic-gate 			Over_p = tmp_ptr;
4783*7c478bd9Sstevel@tonic-gate 
4784*7c478bd9Sstevel@tonic-gate 			Do_rename = 0;	/* names now have original values */
4785*7c478bd9Sstevel@tonic-gate 		} else {
4786*7c478bd9Sstevel@tonic-gate 			if (rename(Over_p, nam_p) < 0) {
4787*7c478bd9Sstevel@tonic-gate 				if (link(Over_p, nam_p) < 0) {
4788*7c478bd9Sstevel@tonic-gate 					msg(EXTN,
4789*7c478bd9Sstevel@tonic-gate 					    "Cannot recover original version"
4790*7c478bd9Sstevel@tonic-gate 					    " of \"%s%s%s\"",
4791*7c478bd9Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
4792*7c478bd9Sstevel@tonic-gate 					    nam_p : Fullnam_p,
4793*7c478bd9Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
4794*7c478bd9Sstevel@tonic-gate 					    "" : gettext(" Attribute "),
4795*7c478bd9Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
4796*7c478bd9Sstevel@tonic-gate 					    "" : nam_p);
4797*7c478bd9Sstevel@tonic-gate 				}
4798*7c478bd9Sstevel@tonic-gate 				if (unlinkat(dirfd, get_component(Over_p), 0)) {
4799*7c478bd9Sstevel@tonic-gate 					msg(ERRN,
4800*7c478bd9Sstevel@tonic-gate 					    "Cannot remove temp file "
4801*7c478bd9Sstevel@tonic-gate 					    "\"%s%s%s\"",
4802*7c478bd9Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
4803*7c478bd9Sstevel@tonic-gate 					    Over_p : Fullnam_p,
4804*7c478bd9Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
4805*7c478bd9Sstevel@tonic-gate 					    "" : gettext(" Attribute "),
4806*7c478bd9Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
4807*7c478bd9Sstevel@tonic-gate 					    "" : Over_p);
4808*7c478bd9Sstevel@tonic-gate 				}
4809*7c478bd9Sstevel@tonic-gate 			}
4810*7c478bd9Sstevel@tonic-gate 		}
4811*7c478bd9Sstevel@tonic-gate 		*Over_p = '\0';
4812*7c478bd9Sstevel@tonic-gate 		return;
4813*7c478bd9Sstevel@tonic-gate 	} else if (over == U_OVER && *Over_p != '\0') {
4814*7c478bd9Sstevel@tonic-gate 		if (Do_rename) {
4815*7c478bd9Sstevel@tonic-gate 			char *tmp_ptr;
4816*7c478bd9Sstevel@tonic-gate 
4817*7c478bd9Sstevel@tonic-gate 			(void) renameat(dirfd, get_component(nam_p),
4818*7c478bd9Sstevel@tonic-gate 			    dirfd, get_component(Over_p));
4819*7c478bd9Sstevel@tonic-gate 			if (Args & OCp) {
4820*7c478bd9Sstevel@tonic-gate 				if (G_p->g_attrnam_p == (char *)NULL) {
4821*7c478bd9Sstevel@tonic-gate 					tmp_ptr = Fullnam_p;
4822*7c478bd9Sstevel@tonic-gate 					Fullnam_p = Over_p;
4823*7c478bd9Sstevel@tonic-gate 					Over_p = tmp_ptr;
4824*7c478bd9Sstevel@tonic-gate 				} else {
4825*7c478bd9Sstevel@tonic-gate 					/*
4826*7c478bd9Sstevel@tonic-gate 					 * Over_p is pointing at g_attrnam_p
4827*7c478bd9Sstevel@tonic-gate 					 * which must be preserved.
4828*7c478bd9Sstevel@tonic-gate 					 *
4829*7c478bd9Sstevel@tonic-gate 					 * We don't want the tmp_ptr and so
4830*7c478bd9Sstevel@tonic-gate 					 * on to throw away our only copy of
4831*7c478bd9Sstevel@tonic-gate 					 * the name.
4832*7c478bd9Sstevel@tonic-gate 					 */
4833*7c478bd9Sstevel@tonic-gate 					Over_p = Attrfile_p;
4834*7c478bd9Sstevel@tonic-gate 				}
4835*7c478bd9Sstevel@tonic-gate 			} else {
4836*7c478bd9Sstevel@tonic-gate 				tmp_ptr = G_p->g_nam_p;
4837*7c478bd9Sstevel@tonic-gate 				G_p->g_nam_p = Over_p;
4838*7c478bd9Sstevel@tonic-gate 				Over_p = tmp_ptr;
4839*7c478bd9Sstevel@tonic-gate 			}
4840*7c478bd9Sstevel@tonic-gate 			Do_rename = 0;	/* names now have original values */
4841*7c478bd9Sstevel@tonic-gate 		} else {
4842*7c478bd9Sstevel@tonic-gate 			if (unlinkat(dirfd, get_component(Over_p), 0) < 0) {
4843*7c478bd9Sstevel@tonic-gate 				msg(ERRN,
4844*7c478bd9Sstevel@tonic-gate 				    "Cannot unlink() temp file \"%s%s%s\"",
4845*7c478bd9Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
4846*7c478bd9Sstevel@tonic-gate 				    Over_p : Fullnam_p,
4847*7c478bd9Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
4848*7c478bd9Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
4849*7c478bd9Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
4850*7c478bd9Sstevel@tonic-gate 				    "" : Over_p);
4851*7c478bd9Sstevel@tonic-gate 			}
4852*7c478bd9Sstevel@tonic-gate 		}
4853*7c478bd9Sstevel@tonic-gate 		*Over_p = '\0';
4854*7c478bd9Sstevel@tonic-gate 	}
4855*7c478bd9Sstevel@tonic-gate 	if (Args & OCp) {
4856*7c478bd9Sstevel@tonic-gate 		if (G_p->g_attrnam_p != (char *)NULL) {
4857*7c478bd9Sstevel@tonic-gate 			inam_p = G_p->g_attrfnam_p;
4858*7c478bd9Sstevel@tonic-gate 			onam_p = G_p->g_attrnam_p;
4859*7c478bd9Sstevel@tonic-gate 		} else {
4860*7c478bd9Sstevel@tonic-gate 			inam_p = Nam_p;
4861*7c478bd9Sstevel@tonic-gate 			onam_p = Fullnam_p;
4862*7c478bd9Sstevel@tonic-gate 		}
4863*7c478bd9Sstevel@tonic-gate 	} else /* OCi only uses onam_p, OCo only uses inam_p */
4864*7c478bd9Sstevel@tonic-gate 		if (G_p->g_attrnam_p != (char *)NULL) {
4865*7c478bd9Sstevel@tonic-gate 			inam_p = onam_p = G_p->g_attrnam_p;
4866*7c478bd9Sstevel@tonic-gate 		} else {
4867*7c478bd9Sstevel@tonic-gate 			inam_p = onam_p = G_p->g_nam_p;
4868*7c478bd9Sstevel@tonic-gate 		}
4869*7c478bd9Sstevel@tonic-gate 
4870*7c478bd9Sstevel@tonic-gate 	/*
4871*7c478bd9Sstevel@tonic-gate 	 * change the modes back to the ones originally created in the
4872*7c478bd9Sstevel@tonic-gate 	 * archive
4873*7c478bd9Sstevel@tonic-gate 	 */
4874*7c478bd9Sstevel@tonic-gate 	if (Args & OCi) {
4875*7c478bd9Sstevel@tonic-gate 		mode_t orig_mask, new_mask;
4876*7c478bd9Sstevel@tonic-gate 		struct stat sbuf;
4877*7c478bd9Sstevel@tonic-gate 
4878*7c478bd9Sstevel@tonic-gate 		if (!(Pflag && acl_set)) {
4879*7c478bd9Sstevel@tonic-gate 			/* Acl was not set, so we must chmod */
4880*7c478bd9Sstevel@tonic-gate 			if (LSTAT(dirfd, G_p->g_nam_p, &sbuf) == 0) {
4881*7c478bd9Sstevel@tonic-gate 				if ((sbuf.st_mode & Ftype) != S_IFLNK) {
4882*7c478bd9Sstevel@tonic-gate 					orig_mask = umask(0);
4883*7c478bd9Sstevel@tonic-gate 					new_mask = G_p->g_mode & ~orig_mask;
4884*7c478bd9Sstevel@tonic-gate 
4885*7c478bd9Sstevel@tonic-gate 					/*
4886*7c478bd9Sstevel@tonic-gate 					 * use fchmod for attributes, since
4887*7c478bd9Sstevel@tonic-gate 					 * we known they are always regular
4888*7c478bd9Sstevel@tonic-gate 					 * files, whereas when it isn't an
4889*7c478bd9Sstevel@tonic-gate 					 * attribute it could be for a fifo
4890*7c478bd9Sstevel@tonic-gate 					 * or something other that we don't
4891*7c478bd9Sstevel@tonic-gate 					 * open and don't have a valid Ofile
4892*7c478bd9Sstevel@tonic-gate 					 * for.
4893*7c478bd9Sstevel@tonic-gate 					 */
4894*7c478bd9Sstevel@tonic-gate 					if (G_p->g_attrnam_p != (char *)NULL) {
4895*7c478bd9Sstevel@tonic-gate 						error = fchmod(Ofile, new_mask);
4896*7c478bd9Sstevel@tonic-gate 					} else {
4897*7c478bd9Sstevel@tonic-gate 						error = chmod(G_p->g_nam_p,
4898*7c478bd9Sstevel@tonic-gate 							new_mask);
4899*7c478bd9Sstevel@tonic-gate 					}
4900*7c478bd9Sstevel@tonic-gate 					if (error < 0) {
4901*7c478bd9Sstevel@tonic-gate 						msg(ERRN,
4902*7c478bd9Sstevel@tonic-gate 						    "Cannot chmod() \"%s%s%s\"",
4903*7c478bd9Sstevel@tonic-gate 						    (G_p->g_attrnam_p ==
4904*7c478bd9Sstevel@tonic-gate 						    (char *)NULL) ?
4905*7c478bd9Sstevel@tonic-gate 						    G_p->g_nam_p :
4906*7c478bd9Sstevel@tonic-gate 						    G_p->g_attrfnam_p,
4907*7c478bd9Sstevel@tonic-gate 						    (G_p->g_attrnam_p ==
4908*7c478bd9Sstevel@tonic-gate 						    (char *)NULL) ?
4909*7c478bd9Sstevel@tonic-gate 						    "" : gettext(
4910*7c478bd9Sstevel@tonic-gate 						    " Attribute "),
4911*7c478bd9Sstevel@tonic-gate 						    (G_p->g_attrnam_p ==
4912*7c478bd9Sstevel@tonic-gate 						    (char *)NULL) ?
4913*7c478bd9Sstevel@tonic-gate 						    "" : G_p->g_attrnam_p);
4914*7c478bd9Sstevel@tonic-gate 					}
4915*7c478bd9Sstevel@tonic-gate 					(void) umask(orig_mask);
4916*7c478bd9Sstevel@tonic-gate 				}
4917*7c478bd9Sstevel@tonic-gate 			}
4918*7c478bd9Sstevel@tonic-gate 		}
4919*7c478bd9Sstevel@tonic-gate 	}
4920*7c478bd9Sstevel@tonic-gate 
4921*7c478bd9Sstevel@tonic-gate 	if (!(Args & OCo)) {
4922*7c478bd9Sstevel@tonic-gate 		if (Args & OCm) {
4923*7c478bd9Sstevel@tonic-gate 			set_tym(dirfd, get_component(onam_p),
4924*7c478bd9Sstevel@tonic-gate 			    G_p->g_mtime, G_p->g_mtime);
4925*7c478bd9Sstevel@tonic-gate 		}
4926*7c478bd9Sstevel@tonic-gate 		if (!acl_set) {
4927*7c478bd9Sstevel@tonic-gate 			if (G_p->g_attrnam_p != (char *)NULL) {
4928*7c478bd9Sstevel@tonic-gate 				error = fchmod(Ofile, (int)G_p->g_mode);
4929*7c478bd9Sstevel@tonic-gate 			} else {
4930*7c478bd9Sstevel@tonic-gate 				error = chmod(onam_p, (int)G_p->g_mode);
4931*7c478bd9Sstevel@tonic-gate 			}
4932*7c478bd9Sstevel@tonic-gate 			if ((error < 0) && ((Euid == 0) || (Args & OCR))) {
4933*7c478bd9Sstevel@tonic-gate 				msg(ERRN, "Cannot chmod() \"%s%s%s\"", onam_p,
4934*7c478bd9Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
4935*7c478bd9Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
4936*7c478bd9Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
4937*7c478bd9Sstevel@tonic-gate 				    "" : onam_p);
4938*7c478bd9Sstevel@tonic-gate 			}
4939*7c478bd9Sstevel@tonic-gate 		}
4940*7c478bd9Sstevel@tonic-gate 		if ((Args & OCR) &&
4941*7c478bd9Sstevel@tonic-gate 		    (fchownat(dirfd, get_component(onam_p), Rpw_p->pw_uid,
4942*7c478bd9Sstevel@tonic-gate 			Rpw_p->pw_gid, 0) < 0)) {
4943*7c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot chown() \"%s%s%s\"",
4944*7c478bd9Sstevel@tonic-gate 			    onam_p,
4945*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
4946*7c478bd9Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
4947*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
4948*7c478bd9Sstevel@tonic-gate 			    "" : onam_p);
4949*7c478bd9Sstevel@tonic-gate 		}
4950*7c478bd9Sstevel@tonic-gate 	}
4951*7c478bd9Sstevel@tonic-gate 
4952*7c478bd9Sstevel@tonic-gate 	if (!(Args & OCi) && (Args & OCa)) {
4953*7c478bd9Sstevel@tonic-gate 		/*
4954*7c478bd9Sstevel@tonic-gate 		 * Use dirfd since we are updating original file
4955*7c478bd9Sstevel@tonic-gate 		 * and not just created file
4956*7c478bd9Sstevel@tonic-gate 		 */
4957*7c478bd9Sstevel@tonic-gate 		set_tym(G_p->g_dirfd, get_component(inam_p),
4958*7c478bd9Sstevel@tonic-gate 		    (ulong_t)SrcSt.st_atime, (ulong_t)SrcSt.st_mtime);
4959*7c478bd9Sstevel@tonic-gate 	}
4960*7c478bd9Sstevel@tonic-gate 
4961*7c478bd9Sstevel@tonic-gate 	if ((Args & OCp) && !(Args & OCR)) {
4962*7c478bd9Sstevel@tonic-gate 		if (fchownat(dirfd, get_component(onam_p),
4963*7c478bd9Sstevel@tonic-gate 		    G_p->g_uid, G_p->g_gid, 0) < 0) {
4964*7c478bd9Sstevel@tonic-gate 			if (Euid == 0) {
4965*7c478bd9Sstevel@tonic-gate 				msg(ERRN, "Cannot chown() \"%s%s%s\"",
4966*7c478bd9Sstevel@tonic-gate 				    onam_p,
4967*7c478bd9Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
4968*7c478bd9Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
4969*7c478bd9Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
4970*7c478bd9Sstevel@tonic-gate 				    "" : onam_p);
4971*7c478bd9Sstevel@tonic-gate 			}
4972*7c478bd9Sstevel@tonic-gate 		}
4973*7c478bd9Sstevel@tonic-gate 	} else {
4974*7c478bd9Sstevel@tonic-gate 		/* OCi only uses onam_p, OCo only uses inam_p */
4975*7c478bd9Sstevel@tonic-gate 		if (!(Args & OCR)) {
4976*7c478bd9Sstevel@tonic-gate 			if ((Args & OCi) && (fchownat(dirfd,
4977*7c478bd9Sstevel@tonic-gate 			    get_component(inam_p), G_p->g_uid,
4978*7c478bd9Sstevel@tonic-gate 			    G_p->g_gid, 0) < 0)) {
4979*7c478bd9Sstevel@tonic-gate 				if (Euid == 0) {
4980*7c478bd9Sstevel@tonic-gate 					msg(ERRN, "Cannot chown() \"%s%s%s\"",
4981*7c478bd9Sstevel@tonic-gate 					    onam_p,
4982*7c478bd9Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
4983*7c478bd9Sstevel@tonic-gate 					    "" : gettext(" Attribute "),
4984*7c478bd9Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
4985*7c478bd9Sstevel@tonic-gate 					    "" : onam_p);
4986*7c478bd9Sstevel@tonic-gate 				}
4987*7c478bd9Sstevel@tonic-gate 			}
4988*7c478bd9Sstevel@tonic-gate 		}
4989*7c478bd9Sstevel@tonic-gate 	}
4990*7c478bd9Sstevel@tonic-gate }
4991*7c478bd9Sstevel@tonic-gate 
4992*7c478bd9Sstevel@tonic-gate /*
4993*7c478bd9Sstevel@tonic-gate  * scan4trail: Scan the archive looking for the trailer.
4994*7c478bd9Sstevel@tonic-gate  * When found, back the archive up over the trailer and overwrite
4995*7c478bd9Sstevel@tonic-gate  * the trailer with the files to be added to the archive.
4996*7c478bd9Sstevel@tonic-gate  */
4997*7c478bd9Sstevel@tonic-gate 
4998*7c478bd9Sstevel@tonic-gate static void
4999*7c478bd9Sstevel@tonic-gate scan4trail(void)
5000*7c478bd9Sstevel@tonic-gate {
5001*7c478bd9Sstevel@tonic-gate 	int rv;
5002*7c478bd9Sstevel@tonic-gate 	off_t off1, off2;
5003*7c478bd9Sstevel@tonic-gate 
5004*7c478bd9Sstevel@tonic-gate 	Append = 1;
5005*7c478bd9Sstevel@tonic-gate 	Hdr_type = NONE;
5006*7c478bd9Sstevel@tonic-gate 	G_p = (struct gen_hdr *)NULL;
5007*7c478bd9Sstevel@tonic-gate 	while (gethdr()) {
5008*7c478bd9Sstevel@tonic-gate 		G_p = &Gen;
5009*7c478bd9Sstevel@tonic-gate 		data_in(P_SKIP);
5010*7c478bd9Sstevel@tonic-gate 	}
5011*7c478bd9Sstevel@tonic-gate 	off1 = Buffr.b_cnt;
5012*7c478bd9Sstevel@tonic-gate 	off2 = Bufsize - (Buffr.b_cnt % Bufsize);
5013*7c478bd9Sstevel@tonic-gate 	Buffr.b_out_p = Buffr.b_in_p = Buffr.b_base_p;
5014*7c478bd9Sstevel@tonic-gate 	Buffr.b_cnt = (off_t)0;
5015*7c478bd9Sstevel@tonic-gate 	if (lseek(Archive, -(off1 + off2), SEEK_REL) < 0)
5016*7c478bd9Sstevel@tonic-gate 		msg(EXTN, "Unable to append to this archive");
5017*7c478bd9Sstevel@tonic-gate 	if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0)
5018*7c478bd9Sstevel@tonic-gate 		msg(EXTN, "Cannot append to this archive");
5019*7c478bd9Sstevel@tonic-gate 	if (lseek(Archive, (off_t)-rv, SEEK_REL) < 0)
5020*7c478bd9Sstevel@tonic-gate 		msg(EXTN, "Unable to append to this archive");
5021*7c478bd9Sstevel@tonic-gate 	Buffr.b_cnt = off2;
5022*7c478bd9Sstevel@tonic-gate 	Buffr.b_in_p = Buffr.b_base_p + Buffr.b_cnt;
5023*7c478bd9Sstevel@tonic-gate 	Append = 0;
5024*7c478bd9Sstevel@tonic-gate }
5025*7c478bd9Sstevel@tonic-gate 
5026*7c478bd9Sstevel@tonic-gate /*
5027*7c478bd9Sstevel@tonic-gate  * setup:  Perform setup and initialization functions.  Parse the options
5028*7c478bd9Sstevel@tonic-gate  * using getopt(3C), call ckopts to check the options and initialize various
5029*7c478bd9Sstevel@tonic-gate  * structures and pointers.  Specifically, for the -i option, save any
5030*7c478bd9Sstevel@tonic-gate  * patterns, for the -o option, check (via stat(2)) the archive, and for
5031*7c478bd9Sstevel@tonic-gate  * the -p option, validate the destination directory.
5032*7c478bd9Sstevel@tonic-gate  */
5033*7c478bd9Sstevel@tonic-gate 
5034*7c478bd9Sstevel@tonic-gate static void
5035*7c478bd9Sstevel@tonic-gate setup(int largc, char **largv)
5036*7c478bd9Sstevel@tonic-gate {
5037*7c478bd9Sstevel@tonic-gate 	extern int optind;
5038*7c478bd9Sstevel@tonic-gate 	extern char *optarg;
5039*7c478bd9Sstevel@tonic-gate 
5040*7c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
5041*7c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
5042*7c478bd9Sstevel@tonic-gate 	char	*opts_p = "zabcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6@";
5043*7c478bd9Sstevel@tonic-gate #else
5044*7c478bd9Sstevel@tonic-gate 	char	*opts_p = "abcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6@";
5045*7c478bd9Sstevel@tonic-gate #endif
5046*7c478bd9Sstevel@tonic-gate #else
5047*7c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
5048*7c478bd9Sstevel@tonic-gate 	char	*opts_p = "zabcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6";
5049*7c478bd9Sstevel@tonic-gate #else
5050*7c478bd9Sstevel@tonic-gate 	char	*opts_p = "abcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6";
5051*7c478bd9Sstevel@tonic-gate #endif
5052*7c478bd9Sstevel@tonic-gate #endif
5053*7c478bd9Sstevel@tonic-gate 
5054*7c478bd9Sstevel@tonic-gate 	char   *dupl_p = "Only one occurrence of -%c allowed";
5055*7c478bd9Sstevel@tonic-gate 	int option;
5056*7c478bd9Sstevel@tonic-gate 	int blk_cnt, blk_cnt_max;
5057*7c478bd9Sstevel@tonic-gate 	struct rlimit rlim;
5058*7c478bd9Sstevel@tonic-gate 
5059*7c478bd9Sstevel@tonic-gate 	/* Remember the native page size. */
5060*7c478bd9Sstevel@tonic-gate 
5061*7c478bd9Sstevel@tonic-gate 	PageSize = sysconf(_SC_PAGESIZE);
5062*7c478bd9Sstevel@tonic-gate 
5063*7c478bd9Sstevel@tonic-gate 	if (PageSize == -1) {
5064*7c478bd9Sstevel@tonic-gate 		/*
5065*7c478bd9Sstevel@tonic-gate 		 * This sysconf call will almost certainly never fail.  The
5066*7c478bd9Sstevel@tonic-gate 		 * symbol PAGESIZE itself resolves to the above sysconf call,
5067*7c478bd9Sstevel@tonic-gate 		 * so we should go ahead and define our own constant.
5068*7c478bd9Sstevel@tonic-gate 		 */
5069*7c478bd9Sstevel@tonic-gate 		PageSize = 8192;
5070*7c478bd9Sstevel@tonic-gate 	}
5071*7c478bd9Sstevel@tonic-gate 
5072*7c478bd9Sstevel@tonic-gate 	Euid = geteuid();
5073*7c478bd9Sstevel@tonic-gate 	Hdr_type = BIN;
5074*7c478bd9Sstevel@tonic-gate 	Max_offset = (off_t)(BIN_OFFSET_MAX);
5075*7c478bd9Sstevel@tonic-gate 	Efil_p = Hdr_p = Own_p = IOfil_p = NULL;
5076*7c478bd9Sstevel@tonic-gate 	while ((option = getopt(largc, largv, opts_p)) != EOF) {
5077*7c478bd9Sstevel@tonic-gate 		switch (option) {
5078*7c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
5079*7c478bd9Sstevel@tonic-gate 		case 'z':
5080*7c478bd9Sstevel@tonic-gate 			/* rendezvous with the debugger */
5081*7c478bd9Sstevel@tonic-gate 			waitaround = 1;
5082*7c478bd9Sstevel@tonic-gate 			break;
5083*7c478bd9Sstevel@tonic-gate #endif
5084*7c478bd9Sstevel@tonic-gate 		case 'a':	/* reset access time */
5085*7c478bd9Sstevel@tonic-gate 			Args |= OCa;
5086*7c478bd9Sstevel@tonic-gate 			break;
5087*7c478bd9Sstevel@tonic-gate 		case 'b':	/* swap bytes and halfwords */
5088*7c478bd9Sstevel@tonic-gate 			Args |= OCb;
5089*7c478bd9Sstevel@tonic-gate 			break;
5090*7c478bd9Sstevel@tonic-gate 		case 'c':	/* select character header */
5091*7c478bd9Sstevel@tonic-gate 			Args |= OCc;
5092*7c478bd9Sstevel@tonic-gate 			Hdr_type = ASC;
5093*7c478bd9Sstevel@tonic-gate 			Max_namesz = APATH;
5094*7c478bd9Sstevel@tonic-gate 			Onecopy = 1;
5095*7c478bd9Sstevel@tonic-gate 			break;
5096*7c478bd9Sstevel@tonic-gate 		case 'd':	/* create directories as needed */
5097*7c478bd9Sstevel@tonic-gate 			Args |= OCd;
5098*7c478bd9Sstevel@tonic-gate 			break;
5099*7c478bd9Sstevel@tonic-gate 		case 'f':	/* select files not in patterns */
5100*7c478bd9Sstevel@tonic-gate 			Args |= OCf;
5101*7c478bd9Sstevel@tonic-gate 			break;
5102*7c478bd9Sstevel@tonic-gate 		case 'i':	/* "copy in" */
5103*7c478bd9Sstevel@tonic-gate 			Args |= OCi;
5104*7c478bd9Sstevel@tonic-gate 			Archive = 0;
5105*7c478bd9Sstevel@tonic-gate 			break;
5106*7c478bd9Sstevel@tonic-gate 		case 'k':	/* retry after I/O errors */
5107*7c478bd9Sstevel@tonic-gate 			Args |= OCk;
5108*7c478bd9Sstevel@tonic-gate 			break;
5109*7c478bd9Sstevel@tonic-gate 		case 'l':	/* link files when possible */
5110*7c478bd9Sstevel@tonic-gate 			Args |= OCl;
5111*7c478bd9Sstevel@tonic-gate 			break;
5112*7c478bd9Sstevel@tonic-gate 		case 'm':	/* retain modification time */
5113*7c478bd9Sstevel@tonic-gate 			Args |= OCm;
5114*7c478bd9Sstevel@tonic-gate 			break;
5115*7c478bd9Sstevel@tonic-gate 		case 'o':	/* "copy out" */
5116*7c478bd9Sstevel@tonic-gate 			Args |= OCo;
5117*7c478bd9Sstevel@tonic-gate 			Archive = 1;
5118*7c478bd9Sstevel@tonic-gate 			break;
5119*7c478bd9Sstevel@tonic-gate 		case 'p':	/* "pass" */
5120*7c478bd9Sstevel@tonic-gate 			Max_namesz = APATH;
5121*7c478bd9Sstevel@tonic-gate 			Args |= OCp;
5122*7c478bd9Sstevel@tonic-gate 			break;
5123*7c478bd9Sstevel@tonic-gate 		case 'r':	/* rename files interactively */
5124*7c478bd9Sstevel@tonic-gate 			Args |= OCr;
5125*7c478bd9Sstevel@tonic-gate 			break;
5126*7c478bd9Sstevel@tonic-gate 		case 's':	/* swap bytes */
5127*7c478bd9Sstevel@tonic-gate 			Args |= OCs;
5128*7c478bd9Sstevel@tonic-gate 			break;
5129*7c478bd9Sstevel@tonic-gate 		case 't':	/* table of contents */
5130*7c478bd9Sstevel@tonic-gate 			Args |= OCt;
5131*7c478bd9Sstevel@tonic-gate 			break;
5132*7c478bd9Sstevel@tonic-gate 		case 'u':	/* copy unconditionally */
5133*7c478bd9Sstevel@tonic-gate 			Args |= OCu;
5134*7c478bd9Sstevel@tonic-gate 			break;
5135*7c478bd9Sstevel@tonic-gate 		case 'v':	/* verbose - print file names */
5136*7c478bd9Sstevel@tonic-gate 			Args |= OCv;
5137*7c478bd9Sstevel@tonic-gate 			break;
5138*7c478bd9Sstevel@tonic-gate 		case 'A':	/* append to existing archive */
5139*7c478bd9Sstevel@tonic-gate 			Args |= OCA;
5140*7c478bd9Sstevel@tonic-gate 			break;
5141*7c478bd9Sstevel@tonic-gate 		case 'B':	/* set block size to 5120 bytes */
5142*7c478bd9Sstevel@tonic-gate 			Args |= OCB;
5143*7c478bd9Sstevel@tonic-gate 			Bufsize = 5120;
5144*7c478bd9Sstevel@tonic-gate 			break;
5145*7c478bd9Sstevel@tonic-gate 		case 'C':	/* set arbitrary block size */
5146*7c478bd9Sstevel@tonic-gate 			if (Args & OCC)
5147*7c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'C');
5148*7c478bd9Sstevel@tonic-gate 			else {
5149*7c478bd9Sstevel@tonic-gate 				Args |= OCC;
5150*7c478bd9Sstevel@tonic-gate 				Bufsize = atoi(optarg);
5151*7c478bd9Sstevel@tonic-gate 			}
5152*7c478bd9Sstevel@tonic-gate 			break;
5153*7c478bd9Sstevel@tonic-gate 		case 'D':
5154*7c478bd9Sstevel@tonic-gate 			Dflag = 1;
5155*7c478bd9Sstevel@tonic-gate 			break;
5156*7c478bd9Sstevel@tonic-gate 		case 'E':	/* alternate file for pattern input */
5157*7c478bd9Sstevel@tonic-gate 			if (Args & OCE)
5158*7c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'E');
5159*7c478bd9Sstevel@tonic-gate 			else {
5160*7c478bd9Sstevel@tonic-gate 				Args |= OCE;
5161*7c478bd9Sstevel@tonic-gate 				Efil_p = optarg;
5162*7c478bd9Sstevel@tonic-gate 			}
5163*7c478bd9Sstevel@tonic-gate 			break;
5164*7c478bd9Sstevel@tonic-gate 		case 'H':	/* select header type */
5165*7c478bd9Sstevel@tonic-gate 			if (Args & OCH)
5166*7c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'H');
5167*7c478bd9Sstevel@tonic-gate 			else {
5168*7c478bd9Sstevel@tonic-gate 				Args |= OCH;
5169*7c478bd9Sstevel@tonic-gate 				Hdr_p = optarg;
5170*7c478bd9Sstevel@tonic-gate 			}
5171*7c478bd9Sstevel@tonic-gate 			break;
5172*7c478bd9Sstevel@tonic-gate 		case 'I':	/* alternate file for archive input */
5173*7c478bd9Sstevel@tonic-gate 			if (Args & OCI)
5174*7c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'I');
5175*7c478bd9Sstevel@tonic-gate 			else {
5176*7c478bd9Sstevel@tonic-gate 				Args |= OCI;
5177*7c478bd9Sstevel@tonic-gate 				IOfil_p = optarg;
5178*7c478bd9Sstevel@tonic-gate 			}
5179*7c478bd9Sstevel@tonic-gate 			break;
5180*7c478bd9Sstevel@tonic-gate 		case 'L':	/* follow symbolic links */
5181*7c478bd9Sstevel@tonic-gate 			Args |= OCL;
5182*7c478bd9Sstevel@tonic-gate 			break;
5183*7c478bd9Sstevel@tonic-gate 		case 'M':	/* specify new end-of-media message */
5184*7c478bd9Sstevel@tonic-gate 			if (Args & OCM)
5185*7c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'M');
5186*7c478bd9Sstevel@tonic-gate 			else {
5187*7c478bd9Sstevel@tonic-gate 				Args |= OCM;
5188*7c478bd9Sstevel@tonic-gate 				Eom_p = optarg;
5189*7c478bd9Sstevel@tonic-gate 			}
5190*7c478bd9Sstevel@tonic-gate 			break;
5191*7c478bd9Sstevel@tonic-gate 		case 'O':	/* alternate file for archive output */
5192*7c478bd9Sstevel@tonic-gate 			if (Args & OCO)
5193*7c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'O');
5194*7c478bd9Sstevel@tonic-gate 			else {
5195*7c478bd9Sstevel@tonic-gate 				Args |= OCO;
5196*7c478bd9Sstevel@tonic-gate 				IOfil_p = optarg;
5197*7c478bd9Sstevel@tonic-gate 			}
5198*7c478bd9Sstevel@tonic-gate 			break;
5199*7c478bd9Sstevel@tonic-gate 		case 'P':	/* preserve acls */
5200*7c478bd9Sstevel@tonic-gate 			Args |= OCP;
5201*7c478bd9Sstevel@tonic-gate 			Pflag++;
5202*7c478bd9Sstevel@tonic-gate 			break;
5203*7c478bd9Sstevel@tonic-gate 		case 'R':	/* change owner/group of files */
5204*7c478bd9Sstevel@tonic-gate 			if (Args & OCR)
5205*7c478bd9Sstevel@tonic-gate 				msg(ERR, dupl_p, 'R');
5206*7c478bd9Sstevel@tonic-gate 			else {
5207*7c478bd9Sstevel@tonic-gate 				Args |= OCR;
5208*7c478bd9Sstevel@tonic-gate 				Own_p = optarg;
5209*7c478bd9Sstevel@tonic-gate 			}
5210*7c478bd9Sstevel@tonic-gate 			break;
5211*7c478bd9Sstevel@tonic-gate 		case 'S':	/* swap halfwords */
5212*7c478bd9Sstevel@tonic-gate 			Args |= OCS;
5213*7c478bd9Sstevel@tonic-gate 			break;
5214*7c478bd9Sstevel@tonic-gate 		case 'V':	/* print a dot '.' for each file */
5215*7c478bd9Sstevel@tonic-gate 			Args |= OCV;
5216*7c478bd9Sstevel@tonic-gate 			break;
5217*7c478bd9Sstevel@tonic-gate 		case '6':	/* for old, sixth-edition files */
5218*7c478bd9Sstevel@tonic-gate 			Args |= OC6;
5219*7c478bd9Sstevel@tonic-gate 			Ftype = SIXTH;
5220*7c478bd9Sstevel@tonic-gate 			break;
5221*7c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
5222*7c478bd9Sstevel@tonic-gate 		case '@':
5223*7c478bd9Sstevel@tonic-gate 			Atflag++;
5224*7c478bd9Sstevel@tonic-gate 			break;
5225*7c478bd9Sstevel@tonic-gate #endif
5226*7c478bd9Sstevel@tonic-gate 		default:
5227*7c478bd9Sstevel@tonic-gate 			Error_cnt++;
5228*7c478bd9Sstevel@tonic-gate 		} /* option */
5229*7c478bd9Sstevel@tonic-gate 	} /* (option = getopt(largc, largv, opts_p)) != EOF */
5230*7c478bd9Sstevel@tonic-gate 
5231*7c478bd9Sstevel@tonic-gate #ifdef WAITAROUND
5232*7c478bd9Sstevel@tonic-gate 	if (waitaround) {
5233*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Rendezvous with cpio on pid"
5234*7c478bd9Sstevel@tonic-gate 		    " %d\n"), getpid());
5235*7c478bd9Sstevel@tonic-gate 
5236*7c478bd9Sstevel@tonic-gate 		while (waitaround) {
5237*7c478bd9Sstevel@tonic-gate 			(void) sleep(10);
5238*7c478bd9Sstevel@tonic-gate 		}
5239*7c478bd9Sstevel@tonic-gate 	}
5240*7c478bd9Sstevel@tonic-gate #endif
5241*7c478bd9Sstevel@tonic-gate 
5242*7c478bd9Sstevel@tonic-gate 	largc -= optind;
5243*7c478bd9Sstevel@tonic-gate 	largv += optind;
5244*7c478bd9Sstevel@tonic-gate 	ckopts(Args);
5245*7c478bd9Sstevel@tonic-gate 	if (!Error_cnt) {
5246*7c478bd9Sstevel@tonic-gate 		Empty = e_valloc(E_EXIT, TARSZ);
5247*7c478bd9Sstevel@tonic-gate 		if (Args & OCr) {
5248*7c478bd9Sstevel@tonic-gate 			Renam_p = e_zalloc(E_EXIT, APATH + 1);
5249*7c478bd9Sstevel@tonic-gate 			Renametmp_p = e_zalloc(E_EXIT, APATH + 1);
5250*7c478bd9Sstevel@tonic-gate 		}
5251*7c478bd9Sstevel@tonic-gate 		Symlnk_p = e_zalloc(E_EXIT, APATH);
5252*7c478bd9Sstevel@tonic-gate 		Over_p = e_zalloc(E_EXIT, APATH);
5253*7c478bd9Sstevel@tonic-gate 		Nam_p = e_zalloc(E_EXIT, APATH + 1);
5254*7c478bd9Sstevel@tonic-gate 		if (Args & OCp) {
5255*7c478bd9Sstevel@tonic-gate 			Savenam_p = e_zalloc(E_EXIT, APATH + 1);
5256*7c478bd9Sstevel@tonic-gate 		}
5257*7c478bd9Sstevel@tonic-gate 		Fullnam_p = e_zalloc(E_EXIT, APATH);
5258*7c478bd9Sstevel@tonic-gate 		Lnknam_p = e_zalloc(E_EXIT, APATH);
5259*7c478bd9Sstevel@tonic-gate 		Gen.g_nam_p = Nam_p;
5260*7c478bd9Sstevel@tonic-gate 		if ((Fullnam_p = getcwd((char *)NULL, APATH)) == (char *)NULL)
5261*7c478bd9Sstevel@tonic-gate 			msg(EXT, "Unable to determine current directory.");
5262*7c478bd9Sstevel@tonic-gate 		if (Args & OCi) {
5263*7c478bd9Sstevel@tonic-gate 			if (largc > 0) /* save patterns for -i option, if any */
5264*7c478bd9Sstevel@tonic-gate 				Pat_pp = largv;
5265*7c478bd9Sstevel@tonic-gate 			if (Args & OCE)
5266*7c478bd9Sstevel@tonic-gate 				getpats(largc, largv);
5267*7c478bd9Sstevel@tonic-gate 		} else if (Args & OCo) {
5268*7c478bd9Sstevel@tonic-gate 			if (largc != 0) /* error if arguments left with -o */
5269*7c478bd9Sstevel@tonic-gate 				Error_cnt++;
5270*7c478bd9Sstevel@tonic-gate 			else if (fstat(Archive, &ArchSt) < 0)
5271*7c478bd9Sstevel@tonic-gate 				msg(ERRN, "Error during stat() of archive");
5272*7c478bd9Sstevel@tonic-gate 			switch (Hdr_type) {
5273*7c478bd9Sstevel@tonic-gate 			case BIN:
5274*7c478bd9Sstevel@tonic-gate 				Hdrsz = HDRSZ;
5275*7c478bd9Sstevel@tonic-gate 				Pad_val = HALFWD;
5276*7c478bd9Sstevel@tonic-gate 				break;
5277*7c478bd9Sstevel@tonic-gate 			case CHR:
5278*7c478bd9Sstevel@tonic-gate 				Hdrsz = CHRSZ;
5279*7c478bd9Sstevel@tonic-gate 				Pad_val = 0;
5280*7c478bd9Sstevel@tonic-gate 				Max_offset = (off_t)(CHAR_OFFSET_MAX);
5281*7c478bd9Sstevel@tonic-gate 				break;
5282*7c478bd9Sstevel@tonic-gate 			case ASC:
5283*7c478bd9Sstevel@tonic-gate 			case CRC:
5284*7c478bd9Sstevel@tonic-gate 				Hdrsz = ASCSZ;
5285*7c478bd9Sstevel@tonic-gate 				Pad_val = FULLWD;
5286*7c478bd9Sstevel@tonic-gate 				break;
5287*7c478bd9Sstevel@tonic-gate 			case TAR:
5288*7c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
5289*7c478bd9Sstevel@tonic-gate 			case USTAR: /* TAR and USTAR */
5290*7c478bd9Sstevel@tonic-gate 				Hdrsz = TARSZ;
5291*7c478bd9Sstevel@tonic-gate 				Pad_val = FULLBK;
5292*7c478bd9Sstevel@tonic-gate 				Max_offset = (off_t)(CHAR_OFFSET_MAX);
5293*7c478bd9Sstevel@tonic-gate 				break;
5294*7c478bd9Sstevel@tonic-gate 			default:
5295*7c478bd9Sstevel@tonic-gate 				msg(EXT, "Impossible header type.");
5296*7c478bd9Sstevel@tonic-gate 			}
5297*7c478bd9Sstevel@tonic-gate 		} else { /* directory must be specified */
5298*7c478bd9Sstevel@tonic-gate 			if (largc != 1)
5299*7c478bd9Sstevel@tonic-gate 				Error_cnt++;
5300*7c478bd9Sstevel@tonic-gate 			else if (access(*largv, 2) < 0)
5301*7c478bd9Sstevel@tonic-gate 				msg(ERRN,
5302*7c478bd9Sstevel@tonic-gate 				    "Error during access() of \"%s\"", *largv);
5303*7c478bd9Sstevel@tonic-gate 		}
5304*7c478bd9Sstevel@tonic-gate 	}
5305*7c478bd9Sstevel@tonic-gate 	if (Error_cnt)
5306*7c478bd9Sstevel@tonic-gate 		usage(); /* exits! */
5307*7c478bd9Sstevel@tonic-gate 	if (Args & (OCi | OCo)) {
5308*7c478bd9Sstevel@tonic-gate 		if (!Dflag) {
5309*7c478bd9Sstevel@tonic-gate 			if (Args & (OCB | OCC)) {
5310*7c478bd9Sstevel@tonic-gate 				if (g_init(&Device, &Archive) < 0)
5311*7c478bd9Sstevel@tonic-gate 					msg(EXTN,
5312*7c478bd9Sstevel@tonic-gate 					    "Error during initialization");
5313*7c478bd9Sstevel@tonic-gate 			} else {
5314*7c478bd9Sstevel@tonic-gate 				if ((Bufsize = g_init(&Device, &Archive)) < 0)
5315*7c478bd9Sstevel@tonic-gate 					msg(EXTN,
5316*7c478bd9Sstevel@tonic-gate 					    "Error during initialization");
5317*7c478bd9Sstevel@tonic-gate 			}
5318*7c478bd9Sstevel@tonic-gate 		}
5319*7c478bd9Sstevel@tonic-gate 
5320*7c478bd9Sstevel@tonic-gate 		blk_cnt_max = _20K / Bufsize;
5321*7c478bd9Sstevel@tonic-gate 		if (blk_cnt_max < MX_BUFS) {
5322*7c478bd9Sstevel@tonic-gate 			blk_cnt_max = MX_BUFS;
5323*7c478bd9Sstevel@tonic-gate 		}
5324*7c478bd9Sstevel@tonic-gate 
5325*7c478bd9Sstevel@tonic-gate 		Buffr.b_base_p = NULL;
5326*7c478bd9Sstevel@tonic-gate 
5327*7c478bd9Sstevel@tonic-gate 		for (blk_cnt = blk_cnt_max; blk_cnt > 1; blk_cnt--) {
5328*7c478bd9Sstevel@tonic-gate 			Buffr.b_size = (size_t)(Bufsize * blk_cnt);
5329*7c478bd9Sstevel@tonic-gate 			Buffr.b_base_p = e_valloc(E_NORMAL, Buffr.b_size);
5330*7c478bd9Sstevel@tonic-gate 			if (Buffr.b_base_p != NULL) {
5331*7c478bd9Sstevel@tonic-gate 				break;
5332*7c478bd9Sstevel@tonic-gate 			}
5333*7c478bd9Sstevel@tonic-gate 		}
5334*7c478bd9Sstevel@tonic-gate 		if (Buffr.b_base_p == NULL || Buffr.b_size < (2 * CPIOBSZ)) {
5335*7c478bd9Sstevel@tonic-gate 			msg(EXT, "Out of memory");
5336*7c478bd9Sstevel@tonic-gate 		}
5337*7c478bd9Sstevel@tonic-gate 
5338*7c478bd9Sstevel@tonic-gate 		Buffr.b_out_p = Buffr.b_in_p = Buffr.b_base_p;
5339*7c478bd9Sstevel@tonic-gate 		Buffr.b_cnt = 0L;
5340*7c478bd9Sstevel@tonic-gate 		Buffr.b_end_p = Buffr.b_base_p + Buffr.b_size;
5341*7c478bd9Sstevel@tonic-gate 	}
5342*7c478bd9Sstevel@tonic-gate 
5343*7c478bd9Sstevel@tonic-gate 	/*
5344*7c478bd9Sstevel@tonic-gate 	 * Now that Bufsize has stabilized, we can allocate our i/o buffer
5345*7c478bd9Sstevel@tonic-gate 	 */
5346*7c478bd9Sstevel@tonic-gate 	Buf_p = e_valloc(E_EXIT, Bufsize);
5347*7c478bd9Sstevel@tonic-gate 
5348*7c478bd9Sstevel@tonic-gate 	if (Args & OCp) { /* get destination directory */
5349*7c478bd9Sstevel@tonic-gate 		(void) strcpy(Fullnam_p, *largv);
5350*7c478bd9Sstevel@tonic-gate 		if (stat(Fullnam_p, &DesSt) < 0)
5351*7c478bd9Sstevel@tonic-gate 			msg(EXTN, "Error during stat() of \"%s\"", Fullnam_p);
5352*7c478bd9Sstevel@tonic-gate 		if ((DesSt.st_mode & Ftype) != S_IFDIR)
5353*7c478bd9Sstevel@tonic-gate 			msg(EXT, "\"%s\" is not a directory", Fullnam_p);
5354*7c478bd9Sstevel@tonic-gate 	}
5355*7c478bd9Sstevel@tonic-gate 	Full_p = Fullnam_p + strlen(Fullnam_p) - 1;
5356*7c478bd9Sstevel@tonic-gate 	if (*Full_p != '/') {
5357*7c478bd9Sstevel@tonic-gate 		Full_p++;
5358*7c478bd9Sstevel@tonic-gate 		*Full_p = '/';
5359*7c478bd9Sstevel@tonic-gate 	}
5360*7c478bd9Sstevel@tonic-gate 	Full_p++;
5361*7c478bd9Sstevel@tonic-gate 	*Full_p = '\0';
5362*7c478bd9Sstevel@tonic-gate 	(void) strcpy(Lnknam_p, Fullnam_p);
5363*7c478bd9Sstevel@tonic-gate 	Lnkend_p = Lnknam_p + strlen(Lnknam_p);
5364*7c478bd9Sstevel@tonic-gate 	(void) getrlimit(RLIMIT_FSIZE, &rlim);
5365*7c478bd9Sstevel@tonic-gate 	Max_filesz = (off_t)rlim.rlim_cur;
5366*7c478bd9Sstevel@tonic-gate 	Lnk_hd.L_nxt_p = Lnk_hd.L_bck_p = &Lnk_hd;
5367*7c478bd9Sstevel@tonic-gate 	Lnk_hd.L_lnk_p = (struct Lnk *)NULL;
5368*7c478bd9Sstevel@tonic-gate }
5369*7c478bd9Sstevel@tonic-gate 
5370*7c478bd9Sstevel@tonic-gate /*
5371*7c478bd9Sstevel@tonic-gate  * set_tym: Set the access and/or modification times for a file.
5372*7c478bd9Sstevel@tonic-gate  */
5373*7c478bd9Sstevel@tonic-gate 
5374*7c478bd9Sstevel@tonic-gate static void
5375*7c478bd9Sstevel@tonic-gate set_tym(int dirfd, char *nam_p, time_t atime, time_t mtime)
5376*7c478bd9Sstevel@tonic-gate {
5377*7c478bd9Sstevel@tonic-gate 	struct timeval times[2];
5378*7c478bd9Sstevel@tonic-gate 
5379*7c478bd9Sstevel@tonic-gate 	times[0].tv_sec = atime;
5380*7c478bd9Sstevel@tonic-gate 	times[0].tv_usec = 0;
5381*7c478bd9Sstevel@tonic-gate 	times[1].tv_sec = mtime;
5382*7c478bd9Sstevel@tonic-gate 	times[1].tv_usec = 0;
5383*7c478bd9Sstevel@tonic-gate 
5384*7c478bd9Sstevel@tonic-gate 	if (futimesat(dirfd, nam_p, times) < 0) {
5385*7c478bd9Sstevel@tonic-gate 		if (Args & OCa) {
5386*7c478bd9Sstevel@tonic-gate 			msg(ERRN,
5387*7c478bd9Sstevel@tonic-gate 			    "Unable to reset access time for \"%s%s%s\"",
5388*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
5389*7c478bd9Sstevel@tonic-gate 			    nam_p : Fullnam_p,
5390*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
5391*7c478bd9Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
5392*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
5393*7c478bd9Sstevel@tonic-gate 			    "" : nam_p);
5394*7c478bd9Sstevel@tonic-gate 		} else {
5395*7c478bd9Sstevel@tonic-gate 			msg(ERRN,
5396*7c478bd9Sstevel@tonic-gate 			    "Unable to reset modification time for \"%s%s%s\"",
5397*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
5398*7c478bd9Sstevel@tonic-gate 			    nam_p : Fullnam_p,
5399*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
5400*7c478bd9Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
5401*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
5402*7c478bd9Sstevel@tonic-gate 			    "" : nam_p);
5403*7c478bd9Sstevel@tonic-gate 		}
5404*7c478bd9Sstevel@tonic-gate 	}
5405*7c478bd9Sstevel@tonic-gate }
5406*7c478bd9Sstevel@tonic-gate 
5407*7c478bd9Sstevel@tonic-gate /*
5408*7c478bd9Sstevel@tonic-gate  * sigint:  Catch interrupts.  If an interrupt occurs during the extraction
5409*7c478bd9Sstevel@tonic-gate  * of a file from the archive with the -u option set, and the filename did
5410*7c478bd9Sstevel@tonic-gate  * exist, remove the current file and restore the original file.  Then exit.
5411*7c478bd9Sstevel@tonic-gate  */
5412*7c478bd9Sstevel@tonic-gate 
5413*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5414*7c478bd9Sstevel@tonic-gate static void
5415*7c478bd9Sstevel@tonic-gate sigint(int sig)
5416*7c478bd9Sstevel@tonic-gate {
5417*7c478bd9Sstevel@tonic-gate 	char *nam_p;
5418*7c478bd9Sstevel@tonic-gate 
5419*7c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, SIG_IGN); /* block further signals */
5420*7c478bd9Sstevel@tonic-gate 	if (!Finished) {
5421*7c478bd9Sstevel@tonic-gate 		if (Args & OCi)
5422*7c478bd9Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
5423*7c478bd9Sstevel@tonic-gate 		else /* OCp */
5424*7c478bd9Sstevel@tonic-gate 			nam_p = Fullnam_p;
5425*7c478bd9Sstevel@tonic-gate 		if (*Over_p != '\0') { /* There is a temp file */
5426*7c478bd9Sstevel@tonic-gate 			if (unlink(nam_p) < 0) {
5427*7c478bd9Sstevel@tonic-gate 				msg(ERRN,
5428*7c478bd9Sstevel@tonic-gate 				    "Cannot remove incomplete \"%s\"", nam_p);
5429*7c478bd9Sstevel@tonic-gate 			}
5430*7c478bd9Sstevel@tonic-gate 			if (rename(Over_p, nam_p) < 0) {
5431*7c478bd9Sstevel@tonic-gate 				if (link(Over_p, nam_p) < 0) {
5432*7c478bd9Sstevel@tonic-gate 					msg(ERRN,
5433*7c478bd9Sstevel@tonic-gate 					    "Cannot recover original \"%s\"",
5434*7c478bd9Sstevel@tonic-gate 					    nam_p);
5435*7c478bd9Sstevel@tonic-gate 				}
5436*7c478bd9Sstevel@tonic-gate 				if (unlink(Over_p)) {
5437*7c478bd9Sstevel@tonic-gate 					msg(ERRN,
5438*7c478bd9Sstevel@tonic-gate 					    "Cannot remove temp file \"%s\"",
5439*7c478bd9Sstevel@tonic-gate 					    Over_p);
5440*7c478bd9Sstevel@tonic-gate 				}
5441*7c478bd9Sstevel@tonic-gate 			}
5442*7c478bd9Sstevel@tonic-gate 		} else if (unlink(nam_p))
5443*7c478bd9Sstevel@tonic-gate 			msg(ERRN,
5444*7c478bd9Sstevel@tonic-gate 			    "Cannot remove incomplete \"%s\"", nam_p);
5445*7c478bd9Sstevel@tonic-gate 			*Over_p = '\0';
5446*7c478bd9Sstevel@tonic-gate 	}
5447*7c478bd9Sstevel@tonic-gate 	exit(EXIT_CODE);
5448*7c478bd9Sstevel@tonic-gate }
5449*7c478bd9Sstevel@tonic-gate 
5450*7c478bd9Sstevel@tonic-gate /*
5451*7c478bd9Sstevel@tonic-gate  * swap: Swap bytes (-s), halfwords (-S) or or both halfwords and bytes (-b).
5452*7c478bd9Sstevel@tonic-gate  */
5453*7c478bd9Sstevel@tonic-gate 
5454*7c478bd9Sstevel@tonic-gate static void
5455*7c478bd9Sstevel@tonic-gate swap(char *buf_p, int cnt)
5456*7c478bd9Sstevel@tonic-gate {
5457*7c478bd9Sstevel@tonic-gate 	unsigned char tbyte;
5458*7c478bd9Sstevel@tonic-gate 	int tcnt;
5459*7c478bd9Sstevel@tonic-gate 	int rcnt;
5460*7c478bd9Sstevel@tonic-gate 	ushort_t thalf;
5461*7c478bd9Sstevel@tonic-gate 
5462*7c478bd9Sstevel@tonic-gate 	rcnt = cnt % 4;
5463*7c478bd9Sstevel@tonic-gate 	cnt /= 4;
5464*7c478bd9Sstevel@tonic-gate 	if (Args & (OCb | OCs | BSM)) {
5465*7c478bd9Sstevel@tonic-gate 		tcnt = cnt;
5466*7c478bd9Sstevel@tonic-gate 		/* LINTED alignment */
5467*7c478bd9Sstevel@tonic-gate 		Swp_p = (union swpbuf *)buf_p;
5468*7c478bd9Sstevel@tonic-gate 		while (tcnt-- > 0) {
5469*7c478bd9Sstevel@tonic-gate 			tbyte = Swp_p->s_byte[0];
5470*7c478bd9Sstevel@tonic-gate 			Swp_p->s_byte[0] = Swp_p->s_byte[1];
5471*7c478bd9Sstevel@tonic-gate 			Swp_p->s_byte[1] = tbyte;
5472*7c478bd9Sstevel@tonic-gate 			tbyte = Swp_p->s_byte[2];
5473*7c478bd9Sstevel@tonic-gate 			Swp_p->s_byte[2] = Swp_p->s_byte[3];
5474*7c478bd9Sstevel@tonic-gate 			Swp_p->s_byte[3] = tbyte;
5475*7c478bd9Sstevel@tonic-gate 			Swp_p++;
5476*7c478bd9Sstevel@tonic-gate 		}
5477*7c478bd9Sstevel@tonic-gate 		if (rcnt >= 2) {
5478*7c478bd9Sstevel@tonic-gate 		tbyte = Swp_p->s_byte[0];
5479*7c478bd9Sstevel@tonic-gate 		Swp_p->s_byte[0] = Swp_p->s_byte[1];
5480*7c478bd9Sstevel@tonic-gate 		Swp_p->s_byte[1] = tbyte;
5481*7c478bd9Sstevel@tonic-gate 		tbyte = Swp_p->s_byte[2];
5482*7c478bd9Sstevel@tonic-gate 		}
5483*7c478bd9Sstevel@tonic-gate 	}
5484*7c478bd9Sstevel@tonic-gate 	if (Args & (OCb | OCS)) {
5485*7c478bd9Sstevel@tonic-gate 		tcnt = cnt;
5486*7c478bd9Sstevel@tonic-gate 		/* LINTED alignment */
5487*7c478bd9Sstevel@tonic-gate 		Swp_p = (union swpbuf *)buf_p;
5488*7c478bd9Sstevel@tonic-gate 		while (tcnt-- > 0) {
5489*7c478bd9Sstevel@tonic-gate 			thalf = Swp_p->s_half[0];
5490*7c478bd9Sstevel@tonic-gate 			Swp_p->s_half[0] = Swp_p->s_half[1];
5491*7c478bd9Sstevel@tonic-gate 			Swp_p->s_half[1] = thalf;
5492*7c478bd9Sstevel@tonic-gate 			Swp_p++;
5493*7c478bd9Sstevel@tonic-gate 		}
5494*7c478bd9Sstevel@tonic-gate 	}
5495*7c478bd9Sstevel@tonic-gate }
5496*7c478bd9Sstevel@tonic-gate 
5497*7c478bd9Sstevel@tonic-gate /*
5498*7c478bd9Sstevel@tonic-gate  * usage: Print the usage message on stderr and exit.
5499*7c478bd9Sstevel@tonic-gate  */
5500*7c478bd9Sstevel@tonic-gate 
5501*7c478bd9Sstevel@tonic-gate static void
5502*7c478bd9Sstevel@tonic-gate usage(void)
5503*7c478bd9Sstevel@tonic-gate {
5504*7c478bd9Sstevel@tonic-gate 
5505*7c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
5506*7c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
5507*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("USAGE:\n"
5508*7c478bd9Sstevel@tonic-gate 				"\tcpio -i[bcdfkmrstuv@BSV6] [-C size] "
5509*7c478bd9Sstevel@tonic-gate 				"[-E file] [-H hdr] [-I file [-M msg]] "
5510*7c478bd9Sstevel@tonic-gate 				"[-R id] [patterns]\n"
5511*7c478bd9Sstevel@tonic-gate 				"\tcpio -o[acv@ABLV] [-C size] "
5512*7c478bd9Sstevel@tonic-gate 				"[-H hdr] [-O file [-M msg]]\n"
5513*7c478bd9Sstevel@tonic-gate 				"\tcpio -p[adlmuv@LV] [-R id] directory\n"));
5514*7c478bd9Sstevel@tonic-gate #else
5515*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("USAGE:\n"
5516*7c478bd9Sstevel@tonic-gate 				"\tcpio -i[bcdfkmrstuvBSV6] [-C size] "
5517*7c478bd9Sstevel@tonic-gate 				"[-E file] [-H hdr] [-I file [-M msg]] "
5518*7c478bd9Sstevel@tonic-gate 				"[-R id] [patterns]\n"
5519*7c478bd9Sstevel@tonic-gate 				"\tcpio -o[acvABLV] [-C size] "
5520*7c478bd9Sstevel@tonic-gate 				"[-H hdr] [-O file [-M msg]]\n"
5521*7c478bd9Sstevel@tonic-gate 				"\tcpio -p[adlmuvLV] [-R id] directory\n"));
5522*7c478bd9Sstevel@tonic-gate #endif
5523*7c478bd9Sstevel@tonic-gate 	(void) fflush(stderr);
5524*7c478bd9Sstevel@tonic-gate 	exit(EXIT_CODE);
5525*7c478bd9Sstevel@tonic-gate }
5526*7c478bd9Sstevel@tonic-gate 
5527*7c478bd9Sstevel@tonic-gate /*
5528*7c478bd9Sstevel@tonic-gate  * verbose: For each file, print either the filename (-v) or a dot (-V).
5529*7c478bd9Sstevel@tonic-gate  * If the -t option (table of contents) is set, print either the filename,
5530*7c478bd9Sstevel@tonic-gate  * or if the -v option is also set, print an "ls -l"-like listing.
5531*7c478bd9Sstevel@tonic-gate  */
5532*7c478bd9Sstevel@tonic-gate 
5533*7c478bd9Sstevel@tonic-gate static void
5534*7c478bd9Sstevel@tonic-gate verbose(char *nam_p)
5535*7c478bd9Sstevel@tonic-gate {
5536*7c478bd9Sstevel@tonic-gate 	int i, j, temp;
5537*7c478bd9Sstevel@tonic-gate 	mode_t mode;
5538*7c478bd9Sstevel@tonic-gate 	char modestr[12];
5539*7c478bd9Sstevel@tonic-gate 
5540*7c478bd9Sstevel@tonic-gate 	/*
5541*7c478bd9Sstevel@tonic-gate 	 * The printf format and associated arguments to print the current
5542*7c478bd9Sstevel@tonic-gate 	 * filename.  Normally, just nam_p.  If we're processing an extended
5543*7c478bd9Sstevel@tonic-gate 	 * attribute, these are overridden.
5544*7c478bd9Sstevel@tonic-gate 	 */
5545*7c478bd9Sstevel@tonic-gate 	char *name_fmt = "%s";
5546*7c478bd9Sstevel@tonic-gate 	const char *name = nam_p;
5547*7c478bd9Sstevel@tonic-gate 	const char *attribute = NULL;
5548*7c478bd9Sstevel@tonic-gate 
5549*7c478bd9Sstevel@tonic-gate 	if (Gen.g_attrnam_p != (char *)NULL) {
5550*7c478bd9Sstevel@tonic-gate 		/*
5551*7c478bd9Sstevel@tonic-gate 		 * Translation note:
5552*7c478bd9Sstevel@tonic-gate 		 * 'attribute' is a noun.
5553*7c478bd9Sstevel@tonic-gate 		 */
5554*7c478bd9Sstevel@tonic-gate 		name_fmt = gettext("%s attribute %s");
5555*7c478bd9Sstevel@tonic-gate 
5556*7c478bd9Sstevel@tonic-gate 		name = (Args & OCp) ? nam_p : Gen.g_attrfnam_p;
5557*7c478bd9Sstevel@tonic-gate 		attribute = Gen.g_attrnam_p;
5558*7c478bd9Sstevel@tonic-gate 	}
5559*7c478bd9Sstevel@tonic-gate 
5560*7c478bd9Sstevel@tonic-gate 	if ((Gen.g_mode == SECMODE) || ((Hdr_type == USTAR ||
5561*7c478bd9Sstevel@tonic-gate 	    Hdr_type == TAR) && Thdr_p->tbuf.t_typeflag == 'A')) {
5562*7c478bd9Sstevel@tonic-gate 		/* dont print ancillary file */
5563*7c478bd9Sstevel@tonic-gate 		aclchar = '+';
5564*7c478bd9Sstevel@tonic-gate 		return;
5565*7c478bd9Sstevel@tonic-gate 	}
5566*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < 11; i++)
5567*7c478bd9Sstevel@tonic-gate 		modestr[i] = '-';
5568*7c478bd9Sstevel@tonic-gate 	modestr[i] = '\0';
5569*7c478bd9Sstevel@tonic-gate 	modestr[i-1] = aclchar;
5570*7c478bd9Sstevel@tonic-gate 	aclchar = ' ';
5571*7c478bd9Sstevel@tonic-gate 
5572*7c478bd9Sstevel@tonic-gate 	if ((Args & OCt) && (Args & OCv)) {
5573*7c478bd9Sstevel@tonic-gate 		mode = Gen.g_mode;
5574*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < 3; i++) {
5575*7c478bd9Sstevel@tonic-gate 			temp = (mode >> (6 - (i * 3)));
5576*7c478bd9Sstevel@tonic-gate 			j = (i * 3) + 1;
5577*7c478bd9Sstevel@tonic-gate 			if (S_IROTH & temp)
5578*7c478bd9Sstevel@tonic-gate 				modestr[j] = 'r';
5579*7c478bd9Sstevel@tonic-gate 			if (S_IWOTH & temp)
5580*7c478bd9Sstevel@tonic-gate 				modestr[j + 1] = 'w';
5581*7c478bd9Sstevel@tonic-gate 			if (S_IXOTH & temp)
5582*7c478bd9Sstevel@tonic-gate 				modestr[j + 2] = 'x';
5583*7c478bd9Sstevel@tonic-gate 		}
5584*7c478bd9Sstevel@tonic-gate 
5585*7c478bd9Sstevel@tonic-gate 		if (Hdr_type != BAR) {
5586*7c478bd9Sstevel@tonic-gate 			temp = Gen.g_mode & Ftype;
5587*7c478bd9Sstevel@tonic-gate 			switch (temp) {
5588*7c478bd9Sstevel@tonic-gate 			case (S_IFIFO):
5589*7c478bd9Sstevel@tonic-gate 				modestr[0] = 'p';
5590*7c478bd9Sstevel@tonic-gate 				break;
5591*7c478bd9Sstevel@tonic-gate 			case (S_IFCHR):
5592*7c478bd9Sstevel@tonic-gate 				modestr[0] = 'c';
5593*7c478bd9Sstevel@tonic-gate 				break;
5594*7c478bd9Sstevel@tonic-gate 			case (S_IFDIR):
5595*7c478bd9Sstevel@tonic-gate 				modestr[0] = 'd';
5596*7c478bd9Sstevel@tonic-gate 				break;
5597*7c478bd9Sstevel@tonic-gate 			case (S_IFBLK):
5598*7c478bd9Sstevel@tonic-gate 				modestr[0] = 'b';
5599*7c478bd9Sstevel@tonic-gate 				break;
5600*7c478bd9Sstevel@tonic-gate 			case (S_IFREG): /* was initialized to '-' */
5601*7c478bd9Sstevel@tonic-gate 				break;
5602*7c478bd9Sstevel@tonic-gate 			case (S_IFLNK):
5603*7c478bd9Sstevel@tonic-gate 				modestr[0] = 'l';
5604*7c478bd9Sstevel@tonic-gate 				break;
5605*7c478bd9Sstevel@tonic-gate 			default:
5606*7c478bd9Sstevel@tonic-gate 				msg(ERR, "Impossible file type");
5607*7c478bd9Sstevel@tonic-gate 			}
5608*7c478bd9Sstevel@tonic-gate 		} else {		/* bar */
5609*7c478bd9Sstevel@tonic-gate 			temp = Gen.g_mode & Ftype;
5610*7c478bd9Sstevel@tonic-gate 			switch (temp) {
5611*7c478bd9Sstevel@tonic-gate 			case (S_IFIFO):
5612*7c478bd9Sstevel@tonic-gate 				modestr[0] = 'p';
5613*7c478bd9Sstevel@tonic-gate 				break;
5614*7c478bd9Sstevel@tonic-gate 			case (S_IFCHR):
5615*7c478bd9Sstevel@tonic-gate 				modestr[0] = 'c';
5616*7c478bd9Sstevel@tonic-gate 				break;
5617*7c478bd9Sstevel@tonic-gate 			case (S_IFDIR):
5618*7c478bd9Sstevel@tonic-gate 				modestr[0] = 'd';
5619*7c478bd9Sstevel@tonic-gate 				break;
5620*7c478bd9Sstevel@tonic-gate 			case (S_IFBLK):
5621*7c478bd9Sstevel@tonic-gate 				modestr[0] = 'b';
5622*7c478bd9Sstevel@tonic-gate 				break;
5623*7c478bd9Sstevel@tonic-gate 			}
5624*7c478bd9Sstevel@tonic-gate 			if (bar_linkflag == SYMTYPE)
5625*7c478bd9Sstevel@tonic-gate 				modestr[0] = 'l';
5626*7c478bd9Sstevel@tonic-gate 		}
5627*7c478bd9Sstevel@tonic-gate 		if ((S_ISUID & Gen.g_mode) == S_ISUID)
5628*7c478bd9Sstevel@tonic-gate 			modestr[3] = 's';
5629*7c478bd9Sstevel@tonic-gate 		if ((S_ISVTX & Gen.g_mode) == S_ISVTX)
5630*7c478bd9Sstevel@tonic-gate 			modestr[9] = 't';
5631*7c478bd9Sstevel@tonic-gate 		if ((S_ISGID & G_p->g_mode) == S_ISGID && modestr[6] == 'x')
5632*7c478bd9Sstevel@tonic-gate 			modestr[6] = 's';
5633*7c478bd9Sstevel@tonic-gate 		else if ((S_ENFMT & Gen.g_mode) == S_ENFMT && modestr[6] != 'x')
5634*7c478bd9Sstevel@tonic-gate 			modestr[6] = 'l';
5635*7c478bd9Sstevel@tonic-gate 		if ((Hdr_type == TAR || Hdr_type == USTAR) && Gen.g_nlink == 0)
5636*7c478bd9Sstevel@tonic-gate 			(void) printf("%s%4d ", modestr, (int)Gen.g_nlink+1);
5637*7c478bd9Sstevel@tonic-gate 		else
5638*7c478bd9Sstevel@tonic-gate 			(void) printf("%s%4d ", modestr, (int)Gen.g_nlink);
5639*7c478bd9Sstevel@tonic-gate 		if (Lastuid == (int)Gen.g_uid) {
5640*7c478bd9Sstevel@tonic-gate 			if (Lastuid == -1)
5641*7c478bd9Sstevel@tonic-gate 				(void) printf("-1       ");
5642*7c478bd9Sstevel@tonic-gate 			else
5643*7c478bd9Sstevel@tonic-gate 				(void) printf("%-9s", Curpw_p->pw_name);
5644*7c478bd9Sstevel@tonic-gate 		} else {
5645*7c478bd9Sstevel@tonic-gate 			if (Curpw_p = getpwuid((int)Gen.g_uid)) {
5646*7c478bd9Sstevel@tonic-gate 				(void) printf("%-9s", Curpw_p->pw_name);
5647*7c478bd9Sstevel@tonic-gate 				Lastuid = (int)Gen.g_uid;
5648*7c478bd9Sstevel@tonic-gate 			} else {
5649*7c478bd9Sstevel@tonic-gate 				(void) printf("%-9d", (int)Gen.g_uid);
5650*7c478bd9Sstevel@tonic-gate 				Lastuid = -1;
5651*7c478bd9Sstevel@tonic-gate 			}
5652*7c478bd9Sstevel@tonic-gate 		}
5653*7c478bd9Sstevel@tonic-gate 		if (Lastgid == (int)Gen.g_gid) {
5654*7c478bd9Sstevel@tonic-gate 			if (Lastgid == -1)
5655*7c478bd9Sstevel@tonic-gate 				(void) printf("-1       ");
5656*7c478bd9Sstevel@tonic-gate 			else
5657*7c478bd9Sstevel@tonic-gate 				(void) printf("%-9s", Curgr_p->gr_name);
5658*7c478bd9Sstevel@tonic-gate 		} else {
5659*7c478bd9Sstevel@tonic-gate 			if (Curgr_p = getgrgid((int)Gen.g_gid)) {
5660*7c478bd9Sstevel@tonic-gate 				(void) printf("%-9s", Curgr_p->gr_name);
5661*7c478bd9Sstevel@tonic-gate 				Lastgid = (int)Gen.g_gid;
5662*7c478bd9Sstevel@tonic-gate 			} else {
5663*7c478bd9Sstevel@tonic-gate 				(void) printf("%-9d", (int)Gen.g_gid);
5664*7c478bd9Sstevel@tonic-gate 				Lastgid = -1;
5665*7c478bd9Sstevel@tonic-gate 			}
5666*7c478bd9Sstevel@tonic-gate 		}
5667*7c478bd9Sstevel@tonic-gate 
5668*7c478bd9Sstevel@tonic-gate 		/* print file size */
5669*7c478bd9Sstevel@tonic-gate 		if (!Aspec || ((Gen.g_mode & Ftype) == S_IFIFO) ||
5670*7c478bd9Sstevel@tonic-gate 		    (Hdr_type == BAR && bar_linkflag == SYMTYPE)) {
5671*7c478bd9Sstevel@tonic-gate 			if (Gen.g_filesz < (1LL << 31))
5672*7c478bd9Sstevel@tonic-gate 				(void) printf("%7lld ",
5673*7c478bd9Sstevel@tonic-gate 				    (offset_t)Gen.g_filesz);
5674*7c478bd9Sstevel@tonic-gate 			else
5675*7c478bd9Sstevel@tonic-gate 				(void) printf("%11lld ",
5676*7c478bd9Sstevel@tonic-gate 				    (offset_t)Gen.g_filesz);
5677*7c478bd9Sstevel@tonic-gate 		} else
5678*7c478bd9Sstevel@tonic-gate 			(void) printf("%3d,%3d ", (int)major(Gen.g_rdev),
5679*7c478bd9Sstevel@tonic-gate 			    (int)minor(Gen.g_rdev));
5680*7c478bd9Sstevel@tonic-gate 		(void) cftime(Time, dcgettext(NULL, FORMAT, LC_TIME),
5681*7c478bd9Sstevel@tonic-gate 		    (time_t *)&Gen.g_mtime);
5682*7c478bd9Sstevel@tonic-gate 		(void) printf("%s, ", Time);
5683*7c478bd9Sstevel@tonic-gate 		(void) printf(name_fmt, name, attribute);
5684*7c478bd9Sstevel@tonic-gate 		if ((Gen.g_mode & Ftype) == S_IFLNK) {
5685*7c478bd9Sstevel@tonic-gate 			if (Hdr_type == USTAR || Hdr_type == TAR)
5686*7c478bd9Sstevel@tonic-gate 				(void) strcpy(Symlnk_p,
5687*7c478bd9Sstevel@tonic-gate 				    Thdr_p->tbuf.t_linkname);
5688*7c478bd9Sstevel@tonic-gate 			else {
5689*7c478bd9Sstevel@tonic-gate 				(void) strncpy(Symlnk_p, Buffr.b_out_p,
5690*7c478bd9Sstevel@tonic-gate 				    Gen.g_filesz);
5691*7c478bd9Sstevel@tonic-gate 				*(Symlnk_p + Gen.g_filesz) = '\0';
5692*7c478bd9Sstevel@tonic-gate 			}
5693*7c478bd9Sstevel@tonic-gate 			(void) printf(" -> %s", Symlnk_p);
5694*7c478bd9Sstevel@tonic-gate 		}
5695*7c478bd9Sstevel@tonic-gate 		if (Hdr_type == BAR) {
5696*7c478bd9Sstevel@tonic-gate 			if (bar_linkflag == SYMTYPE)
5697*7c478bd9Sstevel@tonic-gate 				(void) printf(gettext(" symbolic link to %s"),
5698*7c478bd9Sstevel@tonic-gate 				    bar_linkname);
5699*7c478bd9Sstevel@tonic-gate 			else if (bar_linkflag == '1')
5700*7c478bd9Sstevel@tonic-gate 				(void) printf(gettext(" linked to %s"),
5701*7c478bd9Sstevel@tonic-gate 				    bar_linkname);
5702*7c478bd9Sstevel@tonic-gate 		}
5703*7c478bd9Sstevel@tonic-gate 		if ((Hdr_type == USTAR || Hdr_type == TAR) &&
5704*7c478bd9Sstevel@tonic-gate 		    Thdr_p->tbuf.t_typeflag == '1') {
5705*7c478bd9Sstevel@tonic-gate 			(void) printf(gettext(" linked to %s%s%s"),
5706*7c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
5707*7c478bd9Sstevel@tonic-gate 			    Thdr_p->tbuf.t_linkname : Gen.g_attrfnam_p,
5708*7c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
5709*7c478bd9Sstevel@tonic-gate 			    "" : gettext(" attribute "),
5710*7c478bd9Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
5711*7c478bd9Sstevel@tonic-gate 			    "" : Gen.g_linktoattrnam_p);
5712*7c478bd9Sstevel@tonic-gate 		}
5713*7c478bd9Sstevel@tonic-gate 		(void) printf("\n");
5714*7c478bd9Sstevel@tonic-gate 	} else if ((Args & OCt) || (Args & OCv)) {
5715*7c478bd9Sstevel@tonic-gate 		(void) fprintf(Out_p, name_fmt, name, attribute);
5716*7c478bd9Sstevel@tonic-gate 		(void) fputc('\n', Out_p);
5717*7c478bd9Sstevel@tonic-gate 	} else { /* OCV */
5718*7c478bd9Sstevel@tonic-gate 		(void) fputc('.', Out_p);
5719*7c478bd9Sstevel@tonic-gate 		if (Verbcnt++ >= 49) { /* start a new line of dots */
5720*7c478bd9Sstevel@tonic-gate 			Verbcnt = 0;
5721*7c478bd9Sstevel@tonic-gate 			(void) fputc('\n', Out_p);
5722*7c478bd9Sstevel@tonic-gate 		}
5723*7c478bd9Sstevel@tonic-gate 	}
5724*7c478bd9Sstevel@tonic-gate 	(void) fflush(Out_p);
5725*7c478bd9Sstevel@tonic-gate }
5726*7c478bd9Sstevel@tonic-gate 
5727*7c478bd9Sstevel@tonic-gate #define	MK_USHORT(a)	(a & 00000177777)
5728*7c478bd9Sstevel@tonic-gate 
5729*7c478bd9Sstevel@tonic-gate /*
5730*7c478bd9Sstevel@tonic-gate  * write_hdr: Transfer header information for the generic structure
5731*7c478bd9Sstevel@tonic-gate  * into the format for the selected header and bwrite() the header.
5732*7c478bd9Sstevel@tonic-gate  * ACL support: add two new argumnets. secflag indicates that it's an
5733*7c478bd9Sstevel@tonic-gate  *	ancillary file. len is the size of the file (incl. all security
5734*7c478bd9Sstevel@tonic-gate  *	attributes). We only have acls now.
5735*7c478bd9Sstevel@tonic-gate  */
5736*7c478bd9Sstevel@tonic-gate 
5737*7c478bd9Sstevel@tonic-gate static void
5738*7c478bd9Sstevel@tonic-gate write_hdr(int secflag, off_t len)
5739*7c478bd9Sstevel@tonic-gate {
5740*7c478bd9Sstevel@tonic-gate 	int cnt, pad;
5741*7c478bd9Sstevel@tonic-gate 	mode_t mode;
5742*7c478bd9Sstevel@tonic-gate 	uid_t uid;
5743*7c478bd9Sstevel@tonic-gate 	gid_t gid;
5744*7c478bd9Sstevel@tonic-gate 	const char warnfmt[] = "%s%s%s : %s";
5745*7c478bd9Sstevel@tonic-gate 
5746*7c478bd9Sstevel@tonic-gate 	if (secflag == ARCHIVE_ACL) {
5747*7c478bd9Sstevel@tonic-gate 		mode = SECMODE;
5748*7c478bd9Sstevel@tonic-gate 	} else {
5749*7c478bd9Sstevel@tonic-gate 		/*
5750*7c478bd9Sstevel@tonic-gate 		 * If attribute is being archived in cpio format then
5751*7c478bd9Sstevel@tonic-gate 		 * zap off the file type bits since those are truly a
5752*7c478bd9Sstevel@tonic-gate 		 * mask and reset them with _XATTR_CPIO_MODE
5753*7c478bd9Sstevel@tonic-gate 		 */
5754*7c478bd9Sstevel@tonic-gate 
5755*7c478bd9Sstevel@tonic-gate 		/*
5756*7c478bd9Sstevel@tonic-gate 		 * len is the value of g_filesz for normal files
5757*7c478bd9Sstevel@tonic-gate 		 * and the length of the special header buffer in
5758*7c478bd9Sstevel@tonic-gate 		 * the case of acl and xattr headers.
5759*7c478bd9Sstevel@tonic-gate 		 */
5760*7c478bd9Sstevel@tonic-gate 		if (G_p->g_attrnam_p != (char *)NULL && Hdr_type != USTAR &&
5761*7c478bd9Sstevel@tonic-gate 		    Hdr_type != TAR) {
5762*7c478bd9Sstevel@tonic-gate 			mode = (G_p->g_mode & S_IAMB) | _XATTR_CPIO_MODE;
5763*7c478bd9Sstevel@tonic-gate 		} else {
5764*7c478bd9Sstevel@tonic-gate 			mode = G_p->g_mode;
5765*7c478bd9Sstevel@tonic-gate 		}
5766*7c478bd9Sstevel@tonic-gate 		if (secflag != ARCHIVE_XATTR) {
5767*7c478bd9Sstevel@tonic-gate 			len = G_p->g_filesz;
5768*7c478bd9Sstevel@tonic-gate 		}
5769*7c478bd9Sstevel@tonic-gate 	}
5770*7c478bd9Sstevel@tonic-gate 
5771*7c478bd9Sstevel@tonic-gate 	uid = G_p->g_uid;
5772*7c478bd9Sstevel@tonic-gate 	gid = G_p->g_gid;
5773*7c478bd9Sstevel@tonic-gate 	/*
5774*7c478bd9Sstevel@tonic-gate 	 * Handle EFT uids and gids.  If they get too big
5775*7c478bd9Sstevel@tonic-gate 	 * to be represented in a particular format, force 'em to 'nobody'.
5776*7c478bd9Sstevel@tonic-gate 	 */
5777*7c478bd9Sstevel@tonic-gate 	switch (Hdr_type) {
5778*7c478bd9Sstevel@tonic-gate 	case BIN:			/* 16-bits of u_short */
5779*7c478bd9Sstevel@tonic-gate 		if ((ulong_t)uid > (ulong_t)USHRT_MAX)
5780*7c478bd9Sstevel@tonic-gate 			uid = UID_NOBODY;
5781*7c478bd9Sstevel@tonic-gate 		if ((ulong_t)gid > (ulong_t)USHRT_MAX)
5782*7c478bd9Sstevel@tonic-gate 			gid = GID_NOBODY;
5783*7c478bd9Sstevel@tonic-gate 		break;
5784*7c478bd9Sstevel@tonic-gate 	case CHR:			/* %.6lo => 262143 base 10 */
5785*7c478bd9Sstevel@tonic-gate 		if ((ulong_t)uid > (ulong_t)0777777)
5786*7c478bd9Sstevel@tonic-gate 			uid = UID_NOBODY;
5787*7c478bd9Sstevel@tonic-gate 		if ((ulong_t)gid > (ulong_t)0777777)
5788*7c478bd9Sstevel@tonic-gate 			gid = GID_NOBODY;
5789*7c478bd9Sstevel@tonic-gate 		break;
5790*7c478bd9Sstevel@tonic-gate 	case ASC:			/* %.8lx => full 32 bits */
5791*7c478bd9Sstevel@tonic-gate 	case CRC:
5792*7c478bd9Sstevel@tonic-gate 		break;
5793*7c478bd9Sstevel@tonic-gate 	case USTAR:
5794*7c478bd9Sstevel@tonic-gate 	case TAR:			/* %.7lo => 2097151 base 10 */
5795*7c478bd9Sstevel@tonic-gate 		if ((ulong_t)uid > (ulong_t)07777777)
5796*7c478bd9Sstevel@tonic-gate 			uid = UID_NOBODY;
5797*7c478bd9Sstevel@tonic-gate 		if ((ulong_t)gid > (ulong_t)07777777)
5798*7c478bd9Sstevel@tonic-gate 			gid = GID_NOBODY;
5799*7c478bd9Sstevel@tonic-gate 		break;
5800*7c478bd9Sstevel@tonic-gate 	default:
5801*7c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
5802*7c478bd9Sstevel@tonic-gate 	}
5803*7c478bd9Sstevel@tonic-gate 
5804*7c478bd9Sstevel@tonic-gate 	/*
5805*7c478bd9Sstevel@tonic-gate 	 * Since cpio formats -don't- encode the symbolic names, print
5806*7c478bd9Sstevel@tonic-gate 	 * a warning message when we map the uid or gid this way.
5807*7c478bd9Sstevel@tonic-gate 	 * Also, if the ownership just changed, clear set[ug]id bits
5808*7c478bd9Sstevel@tonic-gate 	 *
5809*7c478bd9Sstevel@tonic-gate 	 * (Except for USTAR format of course, where we have a string
5810*7c478bd9Sstevel@tonic-gate 	 * representation of the username embedded in the header)
5811*7c478bd9Sstevel@tonic-gate 	 */
5812*7c478bd9Sstevel@tonic-gate 	if (uid != G_p->g_uid && Hdr_type != USTAR) {
5813*7c478bd9Sstevel@tonic-gate 		msg(ERR, warnfmt,
5814*7c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
5815*7c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p : G_p->g_attrfnam_p,
5816*7c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
5817*7c478bd9Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
5818*7c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
5819*7c478bd9Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p,
5820*7c478bd9Sstevel@tonic-gate 		    gettext("uid too large for archive format"));
5821*7c478bd9Sstevel@tonic-gate 		if (S_ISREG(mode))
5822*7c478bd9Sstevel@tonic-gate 			mode &= ~S_ISUID;
5823*7c478bd9Sstevel@tonic-gate 	}
5824*7c478bd9Sstevel@tonic-gate 	if (gid != G_p->g_gid && Hdr_type != USTAR) {
5825*7c478bd9Sstevel@tonic-gate 		msg(ERR, warnfmt,
5826*7c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
5827*7c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p : G_p->g_attrfnam_p,
5828*7c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
5829*7c478bd9Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
5830*7c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
5831*7c478bd9Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p,
5832*7c478bd9Sstevel@tonic-gate 		    gettext("gid too large for archive format"));
5833*7c478bd9Sstevel@tonic-gate 		if (S_ISREG(mode))
5834*7c478bd9Sstevel@tonic-gate 			mode &= ~S_ISGID;
5835*7c478bd9Sstevel@tonic-gate 	}
5836*7c478bd9Sstevel@tonic-gate 
5837*7c478bd9Sstevel@tonic-gate 	switch (Hdr_type) {
5838*7c478bd9Sstevel@tonic-gate 	case BIN:
5839*7c478bd9Sstevel@tonic-gate 	case CHR:
5840*7c478bd9Sstevel@tonic-gate 	case ASC:
5841*7c478bd9Sstevel@tonic-gate 	case CRC:
5842*7c478bd9Sstevel@tonic-gate 		cnt = Hdrsz + G_p->g_namesz;
5843*7c478bd9Sstevel@tonic-gate 		break;
5844*7c478bd9Sstevel@tonic-gate 	case TAR:
5845*7c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
5846*7c478bd9Sstevel@tonic-gate 	case USTAR: /* TAR and USTAR */
5847*7c478bd9Sstevel@tonic-gate 		cnt = TARSZ;
5848*7c478bd9Sstevel@tonic-gate 		break;
5849*7c478bd9Sstevel@tonic-gate 	default:
5850*7c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
5851*7c478bd9Sstevel@tonic-gate 	}
5852*7c478bd9Sstevel@tonic-gate 	FLUSH(cnt);
5853*7c478bd9Sstevel@tonic-gate 
5854*7c478bd9Sstevel@tonic-gate 	switch (Hdr_type) {
5855*7c478bd9Sstevel@tonic-gate 	case BIN:
5856*7c478bd9Sstevel@tonic-gate 		Hdr.h_magic = (short)G_p->g_magic;
5857*7c478bd9Sstevel@tonic-gate 		Hdr.h_dev = G_p->g_dev;
5858*7c478bd9Sstevel@tonic-gate 		Hdr.h_ino = G_p->g_ino;
5859*7c478bd9Sstevel@tonic-gate 		Hdr.h_uid = uid;
5860*7c478bd9Sstevel@tonic-gate 		Hdr.h_gid = gid;
5861*7c478bd9Sstevel@tonic-gate 		Hdr.h_mode = mode;
5862*7c478bd9Sstevel@tonic-gate 		Hdr.h_nlink = G_p->g_nlink;
5863*7c478bd9Sstevel@tonic-gate 		Hdr.h_rdev = G_p->g_rdev;
5864*7c478bd9Sstevel@tonic-gate 		mkshort(Hdr.h_mtime, (long)G_p->g_mtime);
5865*7c478bd9Sstevel@tonic-gate 		Hdr.h_namesize = (short)G_p->g_namesz;
5866*7c478bd9Sstevel@tonic-gate 		mkshort(Hdr.h_filesize, (long)len);
5867*7c478bd9Sstevel@tonic-gate 		(void) strcpy(Hdr.h_name, G_p->g_nam_p);
5868*7c478bd9Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, &Hdr, cnt);
5869*7c478bd9Sstevel@tonic-gate 		break;
5870*7c478bd9Sstevel@tonic-gate 	case CHR:
5871*7c478bd9Sstevel@tonic-gate 		(void) sprintf(Buffr.b_in_p,
5872*7c478bd9Sstevel@tonic-gate 		    "%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.11lo%.6lo%."
5873*7c478bd9Sstevel@tonic-gate 		    "11llo%s", G_p->g_magic, G_p->g_dev, G_p->g_ino, mode,
5874*7c478bd9Sstevel@tonic-gate 		    uid, gid, G_p->g_nlink, MK_USHORT(G_p->g_rdev),
5875*7c478bd9Sstevel@tonic-gate 		    G_p->g_mtime, (long)G_p->g_namesz, (offset_t)len,
5876*7c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p);
5877*7c478bd9Sstevel@tonic-gate 		break;
5878*7c478bd9Sstevel@tonic-gate 	case ASC:
5879*7c478bd9Sstevel@tonic-gate 	case CRC:
5880*7c478bd9Sstevel@tonic-gate 		(void) sprintf(Buffr.b_in_p,
5881*7c478bd9Sstevel@tonic-gate 		    "%.6lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%."
5882*7c478bd9Sstevel@tonic-gate 		    "8lx%.8lx%.8lx%.8lx%s",
5883*7c478bd9Sstevel@tonic-gate 			G_p->g_magic, G_p->g_ino, mode, G_p->g_uid,
5884*7c478bd9Sstevel@tonic-gate 			G_p->g_gid, G_p->g_nlink, G_p->g_mtime, (long)len,
5885*7c478bd9Sstevel@tonic-gate 			major(G_p->g_dev), minor(G_p->g_dev),
5886*7c478bd9Sstevel@tonic-gate 			major(G_p->g_rdev), minor(G_p->g_rdev),
5887*7c478bd9Sstevel@tonic-gate 			G_p->g_namesz, G_p->g_cksum, G_p->g_nam_p);
5888*7c478bd9Sstevel@tonic-gate 		break;
5889*7c478bd9Sstevel@tonic-gate 	case USTAR:
5890*7c478bd9Sstevel@tonic-gate 		Thdr_p = (union tblock *)Buffr.b_in_p;
5891*7c478bd9Sstevel@tonic-gate 		(void) memcpy(Thdr_p, Empty, TARSZ);
5892*7c478bd9Sstevel@tonic-gate 		(void) strncpy(Thdr_p->tbuf.t_name, G_p->g_tname,
5893*7c478bd9Sstevel@tonic-gate 		    (int)strlen(G_p->g_tname));
5894*7c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mode, "%07o", (int)mode);
5895*7c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_uid, "%07o", (int)uid);
5896*7c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_gid, "%07o", (int)gid);
5897*7c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_size, "%011llo",
5898*7c478bd9Sstevel@tonic-gate 		    (offset_t)len);
5899*7c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mtime, "%011lo", G_p->g_mtime);
5900*7c478bd9Sstevel@tonic-gate 		if (secflag == ARCHIVE_ACL) {
5901*7c478bd9Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = 'A';	/* ACL file type */
5902*7c478bd9Sstevel@tonic-gate 		} else if (secflag == ARCHIVE_XATTR ||
5903*7c478bd9Sstevel@tonic-gate 		    (G_p->g_attrnam_p != (char *)NULL)) {
5904*7c478bd9Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = _XATTR_HDRTYPE;
5905*7c478bd9Sstevel@tonic-gate 		} else {
5906*7c478bd9Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = G_p->g_typeflag;
5907*7c478bd9Sstevel@tonic-gate 		}
5908*7c478bd9Sstevel@tonic-gate 		if (T_lname[0] != '\0') {
5909*7c478bd9Sstevel@tonic-gate 			/*
5910*7c478bd9Sstevel@tonic-gate 			 * if not a symbolic link
5911*7c478bd9Sstevel@tonic-gate 			 */
5912*7c478bd9Sstevel@tonic-gate 			if (((G_p->g_mode & Ftype) != S_IFLNK) &&
5913*7c478bd9Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL)) {
5914*7c478bd9Sstevel@tonic-gate 				Thdr_p->tbuf.t_typeflag = LNKTYPE;
5915*7c478bd9Sstevel@tonic-gate 				(void) sprintf(Thdr_p->tbuf.t_size,
5916*7c478bd9Sstevel@tonic-gate 				    "%011lo", 0L);
5917*7c478bd9Sstevel@tonic-gate 			}
5918*7c478bd9Sstevel@tonic-gate 			(void) strncpy(Thdr_p->tbuf.t_linkname, T_lname,
5919*7c478bd9Sstevel@tonic-gate 			    strlen(T_lname));
5920*7c478bd9Sstevel@tonic-gate 		}
5921*7c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_magic, "%s", TMAGIC);
5922*7c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_version, "%2s", TVERSION);
5923*7c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_uname, "%s",  G_p->g_uname);
5924*7c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_gname, "%s", G_p->g_gname);
5925*7c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_devmajor, "%07o",
5926*7c478bd9Sstevel@tonic-gate 		    (int)major(G_p->g_rdev));
5927*7c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_devminor, "%07o",
5928*7c478bd9Sstevel@tonic-gate 		    (int)minor(G_p->g_rdev));
5929*7c478bd9Sstevel@tonic-gate 		if (Gen.g_prefix) {
5930*7c478bd9Sstevel@tonic-gate 			(void) sprintf(Thdr_p->tbuf.t_prefix, "%s",
5931*7c478bd9Sstevel@tonic-gate 			    Gen.g_prefix);
5932*7c478bd9Sstevel@tonic-gate 			free(Gen.g_prefix);
5933*7c478bd9Sstevel@tonic-gate 			Gen.g_prefix = NULL;
5934*7c478bd9Sstevel@tonic-gate 		} else {
5935*7c478bd9Sstevel@tonic-gate 			(void) sprintf(Thdr_p->tbuf.t_prefix, "%s", "");
5936*7c478bd9Sstevel@tonic-gate 		}
5937*7c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_cksum, "%07o",
5938*7c478bd9Sstevel@tonic-gate 		    (int)cksum(TARTYP, 0, NULL));
5939*7c478bd9Sstevel@tonic-gate 		break;
5940*7c478bd9Sstevel@tonic-gate 	case TAR:
5941*7c478bd9Sstevel@tonic-gate 		Thdr_p = (union tblock *)Buffr.b_in_p;
5942*7c478bd9Sstevel@tonic-gate 		(void) memcpy(Thdr_p, Empty, TARSZ);
5943*7c478bd9Sstevel@tonic-gate 		(void) strncpy(Thdr_p->tbuf.t_name, G_p->g_nam_p,
5944*7c478bd9Sstevel@tonic-gate 		    G_p->g_namesz);
5945*7c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mode, "%07o ", (int)mode);
5946*7c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_uid, "%07o ", (int)uid);
5947*7c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_gid, "%07o ", (int)gid);
5948*7c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_size, "%011llo ",
5949*7c478bd9Sstevel@tonic-gate 		    (offset_t)len);
5950*7c478bd9Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mtime, "%011o ",
5951*7c478bd9Sstevel@tonic-gate 		    (int)G_p->g_mtime);
5952*7c478bd9Sstevel@tonic-gate 		if (T_lname[0] != '\0') {
5953*7c478bd9Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = '1';
5954*7c478bd9Sstevel@tonic-gate 		} else {
5955*7c478bd9Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = '\0';
5956*7c478bd9Sstevel@tonic-gate 		}
5957*7c478bd9Sstevel@tonic-gate 		(void) strncpy(Thdr_p->tbuf.t_linkname, T_lname,
5958*7c478bd9Sstevel@tonic-gate 		    strlen(T_lname));
5959*7c478bd9Sstevel@tonic-gate 		break;
5960*7c478bd9Sstevel@tonic-gate 	default:
5961*7c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
5962*7c478bd9Sstevel@tonic-gate 	} /* Hdr_type */
5963*7c478bd9Sstevel@tonic-gate 
5964*7c478bd9Sstevel@tonic-gate 	Buffr.b_in_p += cnt;
5965*7c478bd9Sstevel@tonic-gate 	Buffr.b_cnt += cnt;
5966*7c478bd9Sstevel@tonic-gate 	pad = ((cnt + Pad_val) & ~Pad_val) - cnt;
5967*7c478bd9Sstevel@tonic-gate 	if (pad != 0) {
5968*7c478bd9Sstevel@tonic-gate 		FLUSH(pad);
5969*7c478bd9Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, Empty, pad);
5970*7c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += pad;
5971*7c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += pad;
5972*7c478bd9Sstevel@tonic-gate 	}
5973*7c478bd9Sstevel@tonic-gate }
5974*7c478bd9Sstevel@tonic-gate 
5975*7c478bd9Sstevel@tonic-gate /*
5976*7c478bd9Sstevel@tonic-gate  * write_trail: Create the appropriate trailer for the selected header type
5977*7c478bd9Sstevel@tonic-gate  * and bwrite the trailer.  Pad the buffer with nulls out to the next Bufsize
5978*7c478bd9Sstevel@tonic-gate  * boundary, and force a write.  If the write completes, or if the trailer is
5979*7c478bd9Sstevel@tonic-gate  * completely written (but not all of the padding nulls (as can happen on end
5980*7c478bd9Sstevel@tonic-gate  * of medium)) return.  Otherwise, the trailer was not completely written out,
5981*7c478bd9Sstevel@tonic-gate  * so re-pad the buffer with nulls and try again.
5982*7c478bd9Sstevel@tonic-gate  */
5983*7c478bd9Sstevel@tonic-gate 
5984*7c478bd9Sstevel@tonic-gate static void
5985*7c478bd9Sstevel@tonic-gate write_trail(void)
5986*7c478bd9Sstevel@tonic-gate {
5987*7c478bd9Sstevel@tonic-gate 	int cnt, need;
5988*7c478bd9Sstevel@tonic-gate 
5989*7c478bd9Sstevel@tonic-gate 	switch (Hdr_type) {
5990*7c478bd9Sstevel@tonic-gate 	case BIN:
5991*7c478bd9Sstevel@tonic-gate 		Gen.g_magic = CMN_BIN;
5992*7c478bd9Sstevel@tonic-gate 		break;
5993*7c478bd9Sstevel@tonic-gate 	case CHR:
5994*7c478bd9Sstevel@tonic-gate 		Gen.g_magic = CMN_BIN;
5995*7c478bd9Sstevel@tonic-gate 		break;
5996*7c478bd9Sstevel@tonic-gate 	case ASC:
5997*7c478bd9Sstevel@tonic-gate 		Gen.g_magic = CMN_ASC;
5998*7c478bd9Sstevel@tonic-gate 		break;
5999*7c478bd9Sstevel@tonic-gate 	case CRC:
6000*7c478bd9Sstevel@tonic-gate 		Gen.g_magic = CMN_CRC;
6001*7c478bd9Sstevel@tonic-gate 		break;
6002*7c478bd9Sstevel@tonic-gate 	}
6003*7c478bd9Sstevel@tonic-gate 
6004*7c478bd9Sstevel@tonic-gate 	switch (Hdr_type) {
6005*7c478bd9Sstevel@tonic-gate 	case BIN:
6006*7c478bd9Sstevel@tonic-gate 	case CHR:
6007*7c478bd9Sstevel@tonic-gate 	case ASC:
6008*7c478bd9Sstevel@tonic-gate 	case CRC:
6009*7c478bd9Sstevel@tonic-gate 		Gen.g_mode = Gen.g_uid = Gen.g_gid = 0;
6010*7c478bd9Sstevel@tonic-gate 		Gen.g_nlink = 1;
6011*7c478bd9Sstevel@tonic-gate 		Gen.g_mtime = Gen.g_ino = Gen.g_dev = 0;
6012*7c478bd9Sstevel@tonic-gate 		Gen.g_rdev = Gen.g_cksum = 0;
6013*7c478bd9Sstevel@tonic-gate 		Gen.g_filesz = (off_t)0;
6014*7c478bd9Sstevel@tonic-gate 		Gen.g_namesz = strlen("TRAILER!!!") + 1;
6015*7c478bd9Sstevel@tonic-gate 		(void) strcpy(Gen.g_nam_p, "TRAILER!!!");
6016*7c478bd9Sstevel@tonic-gate 		G_p = &Gen;
6017*7c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
6018*7c478bd9Sstevel@tonic-gate 		break;
6019*7c478bd9Sstevel@tonic-gate 	case TAR:
6020*7c478bd9Sstevel@tonic-gate 	/*FALLTHROUGH*/
6021*7c478bd9Sstevel@tonic-gate 	case USTAR: /* TAR and USTAR */
6022*7c478bd9Sstevel@tonic-gate 		for (cnt = 0; cnt < 3; cnt++) {
6023*7c478bd9Sstevel@tonic-gate 			FLUSH(TARSZ);
6024*7c478bd9Sstevel@tonic-gate 			(void) memcpy(Buffr.b_in_p, Empty, TARSZ);
6025*7c478bd9Sstevel@tonic-gate 			Buffr.b_in_p += TARSZ;
6026*7c478bd9Sstevel@tonic-gate 			Buffr.b_cnt += TARSZ;
6027*7c478bd9Sstevel@tonic-gate 		}
6028*7c478bd9Sstevel@tonic-gate 		break;
6029*7c478bd9Sstevel@tonic-gate 	default:
6030*7c478bd9Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
6031*7c478bd9Sstevel@tonic-gate 	}
6032*7c478bd9Sstevel@tonic-gate 	need = Bufsize - (Buffr.b_cnt % Bufsize);
6033*7c478bd9Sstevel@tonic-gate 	if (need == Bufsize)
6034*7c478bd9Sstevel@tonic-gate 		need = 0;
6035*7c478bd9Sstevel@tonic-gate 
6036*7c478bd9Sstevel@tonic-gate 	while (Buffr.b_cnt > 0) {
6037*7c478bd9Sstevel@tonic-gate 		while (need > 0) {
6038*7c478bd9Sstevel@tonic-gate 			cnt = (need < TARSZ) ? need : TARSZ;
6039*7c478bd9Sstevel@tonic-gate 			need -= cnt;
6040*7c478bd9Sstevel@tonic-gate 			FLUSH(cnt);
6041*7c478bd9Sstevel@tonic-gate 			(void) memcpy(Buffr.b_in_p, Empty, cnt);
6042*7c478bd9Sstevel@tonic-gate 			Buffr.b_in_p += cnt;
6043*7c478bd9Sstevel@tonic-gate 			Buffr.b_cnt += cnt;
6044*7c478bd9Sstevel@tonic-gate 		}
6045*7c478bd9Sstevel@tonic-gate 		bflush();
6046*7c478bd9Sstevel@tonic-gate 	}
6047*7c478bd9Sstevel@tonic-gate }
6048*7c478bd9Sstevel@tonic-gate 
6049*7c478bd9Sstevel@tonic-gate /*
6050*7c478bd9Sstevel@tonic-gate  * if archives in USTAR format, check if typeflag == '5' for directories
6051*7c478bd9Sstevel@tonic-gate  */
6052*7c478bd9Sstevel@tonic-gate static int
6053*7c478bd9Sstevel@tonic-gate ustar_dir(void)
6054*7c478bd9Sstevel@tonic-gate {
6055*7c478bd9Sstevel@tonic-gate 	if (Hdr_type == USTAR || Hdr_type == TAR) {
6056*7c478bd9Sstevel@tonic-gate 		if (Thdr_p->tbuf.t_typeflag == '5')
6057*7c478bd9Sstevel@tonic-gate 			return (1);
6058*7c478bd9Sstevel@tonic-gate 	}
6059*7c478bd9Sstevel@tonic-gate 	return (0);
6060*7c478bd9Sstevel@tonic-gate }
6061*7c478bd9Sstevel@tonic-gate 
6062*7c478bd9Sstevel@tonic-gate /*
6063*7c478bd9Sstevel@tonic-gate  * if archives in USTAR format, check if typeflag == '3' || '4' || '6'
6064*7c478bd9Sstevel@tonic-gate  * for character, block, fifo special files
6065*7c478bd9Sstevel@tonic-gate  */
6066*7c478bd9Sstevel@tonic-gate static int
6067*7c478bd9Sstevel@tonic-gate ustar_spec(void)
6068*7c478bd9Sstevel@tonic-gate {
6069*7c478bd9Sstevel@tonic-gate 	int typeflag;
6070*7c478bd9Sstevel@tonic-gate 
6071*7c478bd9Sstevel@tonic-gate 	if (Hdr_type == USTAR || Hdr_type == TAR) {
6072*7c478bd9Sstevel@tonic-gate 		typeflag = Thdr_p->tbuf.t_typeflag;
6073*7c478bd9Sstevel@tonic-gate 		if (typeflag == '3' || typeflag == '4' || typeflag == '6')
6074*7c478bd9Sstevel@tonic-gate 			return (1);
6075*7c478bd9Sstevel@tonic-gate 	}
6076*7c478bd9Sstevel@tonic-gate 	return (0);
6077*7c478bd9Sstevel@tonic-gate }
6078*7c478bd9Sstevel@tonic-gate 
6079*7c478bd9Sstevel@tonic-gate /*
6080*7c478bd9Sstevel@tonic-gate  * The return value is a pointer to a converted copy of the information in
6081*7c478bd9Sstevel@tonic-gate  * FromStat if the file is representable in -Hodc format, and NULL otherwise.
6082*7c478bd9Sstevel@tonic-gate  */
6083*7c478bd9Sstevel@tonic-gate 
6084*7c478bd9Sstevel@tonic-gate static struct stat *
6085*7c478bd9Sstevel@tonic-gate convert_to_old_stat(struct stat *FromStat, char *namep, char *attrp)
6086*7c478bd9Sstevel@tonic-gate {
6087*7c478bd9Sstevel@tonic-gate 	static struct stat ToSt;
6088*7c478bd9Sstevel@tonic-gate 	cpioinfo_t TmpSt;
6089*7c478bd9Sstevel@tonic-gate 
6090*7c478bd9Sstevel@tonic-gate 	(void) memset(&TmpSt, 0, sizeof (cpioinfo_t));
6091*7c478bd9Sstevel@tonic-gate 	stat_to_svr32_stat(&TmpSt, FromStat);
6092*7c478bd9Sstevel@tonic-gate 	(void) memset(&ToSt, 0, sizeof (ToSt));
6093*7c478bd9Sstevel@tonic-gate 
6094*7c478bd9Sstevel@tonic-gate 	if (TmpSt.st_rdev == (o_dev_t)NODEV &&
6095*7c478bd9Sstevel@tonic-gate 	    (((TmpSt.st_mode & Ftype) == S_IFCHR) ||
6096*7c478bd9Sstevel@tonic-gate 	    ((TmpSt.st_mode & Ftype) == S_IFBLK))) {
6097*7c478bd9Sstevel@tonic-gate 		/*
6098*7c478bd9Sstevel@tonic-gate 		 * Encountered a problem representing the rdev information.
6099*7c478bd9Sstevel@tonic-gate 		 * Don't archive it.
6100*7c478bd9Sstevel@tonic-gate 		 */
6101*7c478bd9Sstevel@tonic-gate 
6102*7c478bd9Sstevel@tonic-gate 		msg(ERR, "Error -Hodc format can't support expanded"
6103*7c478bd9Sstevel@tonic-gate 		    "types on %s%s%s",
6104*7c478bd9Sstevel@tonic-gate 		    namep,
6105*7c478bd9Sstevel@tonic-gate 		    (attrp == NULL) ? "" : gettext(" Attribute"),
6106*7c478bd9Sstevel@tonic-gate 		    (attrp == NULL) ? "" : attrp);
6107*7c478bd9Sstevel@tonic-gate 		return (NULL);
6108*7c478bd9Sstevel@tonic-gate 	}
6109*7c478bd9Sstevel@tonic-gate 
6110*7c478bd9Sstevel@tonic-gate 	if (TmpSt.st_dev == (o_dev_t)NODEV) {
6111*7c478bd9Sstevel@tonic-gate 		/*
6112*7c478bd9Sstevel@tonic-gate 		 * Having trouble representing the device/inode pair.  We can't
6113*7c478bd9Sstevel@tonic-gate 		 * track links in this case; break them all into separate
6114*7c478bd9Sstevel@tonic-gate 		 * files.
6115*7c478bd9Sstevel@tonic-gate 		 */
6116*7c478bd9Sstevel@tonic-gate 
6117*7c478bd9Sstevel@tonic-gate 		TmpSt.st_ino = 0;
6118*7c478bd9Sstevel@tonic-gate 
6119*7c478bd9Sstevel@tonic-gate 		if (((TmpSt.st_mode & Ftype) != S_IFDIR) &&
6120*7c478bd9Sstevel@tonic-gate 		    TmpSt.st_nlink > 1)
6121*7c478bd9Sstevel@tonic-gate 			msg(POST,
6122*7c478bd9Sstevel@tonic-gate 			    "Warning: file %s%s%s has large "
6123*7c478bd9Sstevel@tonic-gate 			    "device number - linked "
6124*7c478bd9Sstevel@tonic-gate 			    "files will be restored as "
6125*7c478bd9Sstevel@tonic-gate 			    "separate files",
6126*7c478bd9Sstevel@tonic-gate 			    namep,
6127*7c478bd9Sstevel@tonic-gate 			    (attrp == NULL) ? "" : gettext(" Attribute"),
6128*7c478bd9Sstevel@tonic-gate 			    (attrp == NULL) ? "" : attrp);
6129*7c478bd9Sstevel@tonic-gate 
6130*7c478bd9Sstevel@tonic-gate 		/* ensure no links */
6131*7c478bd9Sstevel@tonic-gate 
6132*7c478bd9Sstevel@tonic-gate 		TmpSt.st_nlink = 1;
6133*7c478bd9Sstevel@tonic-gate 	}
6134*7c478bd9Sstevel@tonic-gate 
6135*7c478bd9Sstevel@tonic-gate 	/* Start converting values */
6136*7c478bd9Sstevel@tonic-gate 
6137*7c478bd9Sstevel@tonic-gate 	if (TmpSt.st_dev < 0) {
6138*7c478bd9Sstevel@tonic-gate 		ToSt.st_dev = 0;
6139*7c478bd9Sstevel@tonic-gate 	} else {
6140*7c478bd9Sstevel@tonic-gate 		ToSt.st_dev = (dev_t)TmpSt.st_dev;
6141*7c478bd9Sstevel@tonic-gate 	}
6142*7c478bd9Sstevel@tonic-gate 
6143*7c478bd9Sstevel@tonic-gate 	/* -actual- not truncated uid */
6144*7c478bd9Sstevel@tonic-gate 
6145*7c478bd9Sstevel@tonic-gate 	ToSt.st_uid = TmpSt.st_uid;
6146*7c478bd9Sstevel@tonic-gate 
6147*7c478bd9Sstevel@tonic-gate 	/* -actual- not truncated gid */
6148*7c478bd9Sstevel@tonic-gate 
6149*7c478bd9Sstevel@tonic-gate 	ToSt.st_gid = TmpSt.st_gid;
6150*7c478bd9Sstevel@tonic-gate 	ToSt.st_ino = (ino_t)TmpSt.st_ino;
6151*7c478bd9Sstevel@tonic-gate 	ToSt.st_mode = (mode_t)TmpSt.st_mode;
6152*7c478bd9Sstevel@tonic-gate 	ToSt.st_mtime = (ulong_t)TmpSt.st_modtime;
6153*7c478bd9Sstevel@tonic-gate 	ToSt.st_nlink = (nlink_t)TmpSt.st_nlink;
6154*7c478bd9Sstevel@tonic-gate 	ToSt.st_size = (off_t)TmpSt.st_size;
6155*7c478bd9Sstevel@tonic-gate 	ToSt.st_rdev = (dev_t)TmpSt.st_rdev;
6156*7c478bd9Sstevel@tonic-gate 
6157*7c478bd9Sstevel@tonic-gate 	return (&ToSt);
6158*7c478bd9Sstevel@tonic-gate }
6159*7c478bd9Sstevel@tonic-gate 
6160*7c478bd9Sstevel@tonic-gate /*
6161*7c478bd9Sstevel@tonic-gate  * In the beginning of each bar archive, there is a header which describes the
6162*7c478bd9Sstevel@tonic-gate  * current volume being created, followed by a header which describes the
6163*7c478bd9Sstevel@tonic-gate  * current file being created, followed by the file itself.  If there is
6164*7c478bd9Sstevel@tonic-gate  * more than one file to be created, a separate header will be created for
6165*7c478bd9Sstevel@tonic-gate  * each additional file.  This structure may be repeated if the bar archive
6166*7c478bd9Sstevel@tonic-gate  * contains multiple volumes.  If a file spans across volumes, its header
6167*7c478bd9Sstevel@tonic-gate  * will not be repeated in the next volume.
6168*7c478bd9Sstevel@tonic-gate  *               +------------------+
6169*7c478bd9Sstevel@tonic-gate  *               |    vol header    |
6170*7c478bd9Sstevel@tonic-gate  *               |------------------|
6171*7c478bd9Sstevel@tonic-gate  *               |   file header i  |     i = 0
6172*7c478bd9Sstevel@tonic-gate  *               |------------------|
6173*7c478bd9Sstevel@tonic-gate  *               |     <file i>     |
6174*7c478bd9Sstevel@tonic-gate  *               |------------------|
6175*7c478bd9Sstevel@tonic-gate  *               |  file header i+1 |
6176*7c478bd9Sstevel@tonic-gate  *               |------------------|
6177*7c478bd9Sstevel@tonic-gate  *               |    <file i+1>    |
6178*7c478bd9Sstevel@tonic-gate  *               |------------------|
6179*7c478bd9Sstevel@tonic-gate  *               |        .         |
6180*7c478bd9Sstevel@tonic-gate  *               |        .         |
6181*7c478bd9Sstevel@tonic-gate  *               |        .         |
6182*7c478bd9Sstevel@tonic-gate  *               +------------------+
6183*7c478bd9Sstevel@tonic-gate  */
6184*7c478bd9Sstevel@tonic-gate 
6185*7c478bd9Sstevel@tonic-gate /*
6186*7c478bd9Sstevel@tonic-gate  * read in the header that describes the current volume of the bar archive
6187*7c478bd9Sstevel@tonic-gate  * to be extracted.
6188*7c478bd9Sstevel@tonic-gate  */
6189*7c478bd9Sstevel@tonic-gate static void
6190*7c478bd9Sstevel@tonic-gate read_bar_vol_hdr(void)
6191*7c478bd9Sstevel@tonic-gate {
6192*7c478bd9Sstevel@tonic-gate 	union b_block *tmp_hdr;
6193*7c478bd9Sstevel@tonic-gate 
6194*7c478bd9Sstevel@tonic-gate 	tmp_hdr = (union b_block *)Buffr.b_out_p;
6195*7c478bd9Sstevel@tonic-gate 	if (tmp_hdr->dbuf.bar_magic[0] == BAR_VOLUME_MAGIC) {
6196*7c478bd9Sstevel@tonic-gate 
6197*7c478bd9Sstevel@tonic-gate 		if (bar_Vhdr == NULL) {
6198*7c478bd9Sstevel@tonic-gate 			bar_Vhdr = e_zalloc(E_EXIT, TBLOCK);
6199*7c478bd9Sstevel@tonic-gate 		}
6200*7c478bd9Sstevel@tonic-gate 		(void) memcpy(&(bar_Vhdr->dbuf), &(tmp_hdr->dbuf), TBLOCK);
6201*7c478bd9Sstevel@tonic-gate 	} else {
6202*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
6203*7c478bd9Sstevel@tonic-gate 		    "bar error: cannot read volume header\n"));
6204*7c478bd9Sstevel@tonic-gate 		exit(1);
6205*7c478bd9Sstevel@tonic-gate 	}
6206*7c478bd9Sstevel@tonic-gate 
6207*7c478bd9Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.mode, "%8lo", &Gen_bar_vol.g_mode);
6208*7c478bd9Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.uid, "%8d", (int *)&Gen_bar_vol.g_uid);
6209*7c478bd9Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.gid, "%8d", (int *)&Gen_bar_vol.g_gid);
6210*7c478bd9Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.size, "%12llo",
6211*7c478bd9Sstevel@tonic-gate 	    (u_off_t *)&Gen_bar_vol.g_filesz);
6212*7c478bd9Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.mtime, "%12lo", &Gen_bar_vol.g_mtime);
6213*7c478bd9Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.chksum, "%8lo", &Gen_bar_vol.g_cksum);
6214*7c478bd9Sstevel@tonic-gate 
6215*7c478bd9Sstevel@tonic-gate 	/* set the compress flag */
6216*7c478bd9Sstevel@tonic-gate 	if (bar_Vhdr->dbuf.compressed == '1')
6217*7c478bd9Sstevel@tonic-gate 		Compressed = 1;
6218*7c478bd9Sstevel@tonic-gate 	else
6219*7c478bd9Sstevel@tonic-gate 		Compressed = 0;
6220*7c478bd9Sstevel@tonic-gate 
6221*7c478bd9Sstevel@tonic-gate 	Buffr.b_out_p += 512;
6222*7c478bd9Sstevel@tonic-gate 	Buffr.b_cnt -= 512;
6223*7c478bd9Sstevel@tonic-gate 
6224*7c478bd9Sstevel@tonic-gate 	/*
6225*7c478bd9Sstevel@tonic-gate 	 * not the first volume; exit
6226*7c478bd9Sstevel@tonic-gate 	 */
6227*7c478bd9Sstevel@tonic-gate 	if (strcmp(bar_Vhdr->dbuf.volume_num, "1") != 0) {
6228*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6229*7c478bd9Sstevel@tonic-gate 		    gettext("error: This is not volume 1.  "));
6230*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("This is volume %s.  "),
6231*7c478bd9Sstevel@tonic-gate 		    bar_Vhdr->dbuf.volume_num);
6232*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Please insert volume 1.\n"));
6233*7c478bd9Sstevel@tonic-gate 		exit(1);
6234*7c478bd9Sstevel@tonic-gate 	}
6235*7c478bd9Sstevel@tonic-gate 
6236*7c478bd9Sstevel@tonic-gate 	read_bar_file_hdr();
6237*7c478bd9Sstevel@tonic-gate }
6238*7c478bd9Sstevel@tonic-gate 
6239*7c478bd9Sstevel@tonic-gate /*
6240*7c478bd9Sstevel@tonic-gate  * read in the header that describes the current file to be extracted
6241*7c478bd9Sstevel@tonic-gate  */
6242*7c478bd9Sstevel@tonic-gate static void
6243*7c478bd9Sstevel@tonic-gate read_bar_file_hdr(void)
6244*7c478bd9Sstevel@tonic-gate {
6245*7c478bd9Sstevel@tonic-gate 	union b_block *tmp_hdr;
6246*7c478bd9Sstevel@tonic-gate 	char *start_of_name, *name_p;
6247*7c478bd9Sstevel@tonic-gate 	char *tmp;
6248*7c478bd9Sstevel@tonic-gate 
6249*7c478bd9Sstevel@tonic-gate 	if (*Buffr.b_out_p == '\0') {
6250*7c478bd9Sstevel@tonic-gate 		*Gen.g_nam_p = '\0';
6251*7c478bd9Sstevel@tonic-gate 		exit(0);
6252*7c478bd9Sstevel@tonic-gate 	}
6253*7c478bd9Sstevel@tonic-gate 
6254*7c478bd9Sstevel@tonic-gate 	tmp_hdr = (union b_block *)Buffr.b_out_p;
6255*7c478bd9Sstevel@tonic-gate 
6256*7c478bd9Sstevel@tonic-gate 	tmp = &tmp_hdr->dbuf.mode[1];
6257*7c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp, "%8lo", &Gen.g_mode);
6258*7c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.uid, "%8lo", &Gen.g_uid);
6259*7c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.gid, "%8lo", &Gen.g_gid);
6260*7c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.size, "%12llo",
6261*7c478bd9Sstevel@tonic-gate 	    (u_off_t *)&Gen.g_filesz);
6262*7c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.mtime, "%12lo", &Gen.g_mtime);
6263*7c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.chksum, "%8lo", &Gen.g_cksum);
6264*7c478bd9Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.rdev, "%8lo", &Gen.g_rdev);
6265*7c478bd9Sstevel@tonic-gate 
6266*7c478bd9Sstevel@tonic-gate #define	to_new_major(x)	(int)((unsigned)((x) & OMAXMAJ) << NBITSMINOR)
6267*7c478bd9Sstevel@tonic-gate #define	to_new_minor(x)	(int)((x) & OMAXMIN)
6268*7c478bd9Sstevel@tonic-gate 	Gen.g_rdev = to_new_major(Gen.g_rdev) | to_new_minor(Gen.g_rdev);
6269*7c478bd9Sstevel@tonic-gate 	bar_linkflag = tmp_hdr->dbuf.linkflag;
6270*7c478bd9Sstevel@tonic-gate 	start_of_name = &tmp_hdr->dbuf.start_of_name;
6271*7c478bd9Sstevel@tonic-gate 
6272*7c478bd9Sstevel@tonic-gate 
6273*7c478bd9Sstevel@tonic-gate 	name_p = Gen.g_nam_p;
6274*7c478bd9Sstevel@tonic-gate 	while (*name_p++ = *start_of_name++)
6275*7c478bd9Sstevel@tonic-gate 		;
6276*7c478bd9Sstevel@tonic-gate 	*name_p = '\0';
6277*7c478bd9Sstevel@tonic-gate 	if (bar_linkflag == LNKTYPE || bar_linkflag == SYMTYPE)
6278*7c478bd9Sstevel@tonic-gate 		(void) strcpy(bar_linkname, start_of_name);
6279*7c478bd9Sstevel@tonic-gate 
6280*7c478bd9Sstevel@tonic-gate 	Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
6281*7c478bd9Sstevel@tonic-gate 	(void) strcpy(nambuf, Gen.g_nam_p);
6282*7c478bd9Sstevel@tonic-gate }
6283*7c478bd9Sstevel@tonic-gate 
6284*7c478bd9Sstevel@tonic-gate /*
6285*7c478bd9Sstevel@tonic-gate  * if the bar archive is compressed, set up a pipe and do the de-compression
6286*7c478bd9Sstevel@tonic-gate  * as the compressed file is read in.
6287*7c478bd9Sstevel@tonic-gate  */
6288*7c478bd9Sstevel@tonic-gate static void
6289*7c478bd9Sstevel@tonic-gate setup_uncompress(FILE **pipef)
6290*7c478bd9Sstevel@tonic-gate {
6291*7c478bd9Sstevel@tonic-gate 	char *cmd_buf;
6292*7c478bd9Sstevel@tonic-gate 	size_t cmdlen;
6293*7c478bd9Sstevel@tonic-gate 
6294*7c478bd9Sstevel@tonic-gate 	cmd_buf = e_zalloc(E_EXIT, MAXPATHLEN * 2);
6295*7c478bd9Sstevel@tonic-gate 
6296*7c478bd9Sstevel@tonic-gate 	if (access(Gen.g_nam_p, W_OK) != 0) {
6297*7c478bd9Sstevel@tonic-gate 		cmdlen = snprintf(cmd_buf, MAXPATHLEN * 2,
6298*7c478bd9Sstevel@tonic-gate 		    "chmod +w '%s'; uncompress -c > '%s'; "
6299*7c478bd9Sstevel@tonic-gate 		    "chmod 0%o '%s'",
6300*7c478bd9Sstevel@tonic-gate 		    Gen.g_nam_p, Gen.g_nam_p, (int)G_p->g_mode, Gen.g_nam_p);
6301*7c478bd9Sstevel@tonic-gate 	} else {
6302*7c478bd9Sstevel@tonic-gate 		cmdlen = snprintf(cmd_buf, MAXPATHLEN * 2,
6303*7c478bd9Sstevel@tonic-gate 		    "uncompress -c > '%s'", Gen.g_nam_p);
6304*7c478bd9Sstevel@tonic-gate 	}
6305*7c478bd9Sstevel@tonic-gate 
6306*7c478bd9Sstevel@tonic-gate 	if (cmdlen >= MAXPATHLEN * 2 ||
6307*7c478bd9Sstevel@tonic-gate 	    (*pipef = popen(cmd_buf, "w")) == NULL) {
6308*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("error\n"));
6309*7c478bd9Sstevel@tonic-gate 		exit(1);
6310*7c478bd9Sstevel@tonic-gate 	}
6311*7c478bd9Sstevel@tonic-gate 
6312*7c478bd9Sstevel@tonic-gate 	if (close(Ofile) != 0)
6313*7c478bd9Sstevel@tonic-gate 		msg(EXTN, "close error");
6314*7c478bd9Sstevel@tonic-gate 	Ofile = fileno(*pipef);
6315*7c478bd9Sstevel@tonic-gate 
6316*7c478bd9Sstevel@tonic-gate 	free(cmd_buf);
6317*7c478bd9Sstevel@tonic-gate }
6318*7c478bd9Sstevel@tonic-gate 
6319*7c478bd9Sstevel@tonic-gate /*
6320*7c478bd9Sstevel@tonic-gate  * if the bar archive spans multiple volumes, read in the header that
6321*7c478bd9Sstevel@tonic-gate  * describes the next volume.
6322*7c478bd9Sstevel@tonic-gate  */
6323*7c478bd9Sstevel@tonic-gate static void
6324*7c478bd9Sstevel@tonic-gate skip_bar_volhdr(void)
6325*7c478bd9Sstevel@tonic-gate {
6326*7c478bd9Sstevel@tonic-gate 	char *buff;
6327*7c478bd9Sstevel@tonic-gate 	union b_block *tmp_hdr;
6328*7c478bd9Sstevel@tonic-gate 
6329*7c478bd9Sstevel@tonic-gate 	buff = e_zalloc(E_EXIT, (uint_t)Bufsize);
6330*7c478bd9Sstevel@tonic-gate 
6331*7c478bd9Sstevel@tonic-gate 	if (g_read(Device, Archive, buff, Bufsize) < 0) {
6332*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
6333*7c478bd9Sstevel@tonic-gate 		    "error in skip_bar_volhdr\n"));
6334*7c478bd9Sstevel@tonic-gate 	} else {
6335*7c478bd9Sstevel@tonic-gate 
6336*7c478bd9Sstevel@tonic-gate 		tmp_hdr = (union b_block *)buff;
6337*7c478bd9Sstevel@tonic-gate 		if (tmp_hdr->dbuf.bar_magic[0] == BAR_VOLUME_MAGIC) {
6338*7c478bd9Sstevel@tonic-gate 
6339*7c478bd9Sstevel@tonic-gate 			if (bar_Vhdr == NULL) {
6340*7c478bd9Sstevel@tonic-gate 				bar_Vhdr = e_zalloc(E_EXIT, TBLOCK);
6341*7c478bd9Sstevel@tonic-gate 			}
6342*7c478bd9Sstevel@tonic-gate 			(void) memcpy(&(bar_Vhdr->dbuf),
6343*7c478bd9Sstevel@tonic-gate 			    &(tmp_hdr->dbuf), TBLOCK);
6344*7c478bd9Sstevel@tonic-gate 		} else {
6345*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
6346*7c478bd9Sstevel@tonic-gate 			    gettext("cpio error: cannot read bar volume "
6347*7c478bd9Sstevel@tonic-gate 			    "header\n"));
6348*7c478bd9Sstevel@tonic-gate 			exit(1);
6349*7c478bd9Sstevel@tonic-gate 		}
6350*7c478bd9Sstevel@tonic-gate 
6351*7c478bd9Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.mode, "%8lo",
6352*7c478bd9Sstevel@tonic-gate 		    &Gen_bar_vol.g_mode);
6353*7c478bd9Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.uid, "%8lo",
6354*7c478bd9Sstevel@tonic-gate 		    &Gen_bar_vol.g_uid);
6355*7c478bd9Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.gid, "%8lo",
6356*7c478bd9Sstevel@tonic-gate 		    &Gen_bar_vol.g_gid);
6357*7c478bd9Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.size, "%12llo",
6358*7c478bd9Sstevel@tonic-gate 		    (u_off_t *)&Gen_bar_vol.g_filesz);
6359*7c478bd9Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.mtime, "%12lo",
6360*7c478bd9Sstevel@tonic-gate 		    &Gen_bar_vol.g_mtime);
6361*7c478bd9Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.chksum, "%8lo",
6362*7c478bd9Sstevel@tonic-gate 		    &Gen_bar_vol.g_cksum);
6363*7c478bd9Sstevel@tonic-gate 		if (bar_Vhdr->dbuf.compressed == '1')
6364*7c478bd9Sstevel@tonic-gate 			Compressed = 1;
6365*7c478bd9Sstevel@tonic-gate 		else
6366*7c478bd9Sstevel@tonic-gate 			Compressed = 0;
6367*7c478bd9Sstevel@tonic-gate 	}
6368*7c478bd9Sstevel@tonic-gate 
6369*7c478bd9Sstevel@tonic-gate 	/*
6370*7c478bd9Sstevel@tonic-gate 	 * Now put the rest of the bytes read in into the data buffer.
6371*7c478bd9Sstevel@tonic-gate 	 */
6372*7c478bd9Sstevel@tonic-gate 	(void) memcpy(Buffr.b_in_p, &buff[512], (Bufsize - 512));
6373*7c478bd9Sstevel@tonic-gate 	Buffr.b_in_p += (Bufsize - 512);
6374*7c478bd9Sstevel@tonic-gate 	Buffr.b_cnt += (long)(Bufsize - 512);
6375*7c478bd9Sstevel@tonic-gate 
6376*7c478bd9Sstevel@tonic-gate 	free(buff);
6377*7c478bd9Sstevel@tonic-gate }
6378*7c478bd9Sstevel@tonic-gate 
6379*7c478bd9Sstevel@tonic-gate /*
6380*7c478bd9Sstevel@tonic-gate  * check the linkflag which indicates the type of the file to be extracted,
6381*7c478bd9Sstevel@tonic-gate  * invoke the corresponding routine to extract the file.
6382*7c478bd9Sstevel@tonic-gate  */
6383*7c478bd9Sstevel@tonic-gate static void
6384*7c478bd9Sstevel@tonic-gate bar_file_in(void)
6385*7c478bd9Sstevel@tonic-gate {
6386*7c478bd9Sstevel@tonic-gate 	/*
6387*7c478bd9Sstevel@tonic-gate 	 * the file is a directory
6388*7c478bd9Sstevel@tonic-gate 	 */
6389*7c478bd9Sstevel@tonic-gate 	if (Adir) {
6390*7c478bd9Sstevel@tonic-gate 		if (ckname(1) != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
6391*7c478bd9Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
6392*7c478bd9Sstevel@tonic-gate 		}
6393*7c478bd9Sstevel@tonic-gate 		return;
6394*7c478bd9Sstevel@tonic-gate 	}
6395*7c478bd9Sstevel@tonic-gate 
6396*7c478bd9Sstevel@tonic-gate 	switch (bar_linkflag) {
6397*7c478bd9Sstevel@tonic-gate 	case REGTYPE:
6398*7c478bd9Sstevel@tonic-gate 		/* regular file */
6399*7c478bd9Sstevel@tonic-gate 		if ((ckname(1) == F_SKIP) ||
6400*7c478bd9Sstevel@tonic-gate 		    (Ofile = openout(G_p->g_dirfd)) < 0) {
6401*7c478bd9Sstevel@tonic-gate 			data_in(P_SKIP);
6402*7c478bd9Sstevel@tonic-gate 		} else {
6403*7c478bd9Sstevel@tonic-gate 			data_in(P_PROC);
6404*7c478bd9Sstevel@tonic-gate 		}
6405*7c478bd9Sstevel@tonic-gate 		break;
6406*7c478bd9Sstevel@tonic-gate 	case LNKTYPE:
6407*7c478bd9Sstevel@tonic-gate 		/* hard link */
6408*7c478bd9Sstevel@tonic-gate 		if (ckname(1) == F_SKIP) {
6409*7c478bd9Sstevel@tonic-gate 			break;
6410*7c478bd9Sstevel@tonic-gate 		}
6411*7c478bd9Sstevel@tonic-gate 		(void) creat_lnk(G_p->g_dirfd, bar_linkname, G_p->g_nam_p);
6412*7c478bd9Sstevel@tonic-gate 		break;
6413*7c478bd9Sstevel@tonic-gate 	case SYMTYPE:
6414*7c478bd9Sstevel@tonic-gate 		/* symbolic link */
6415*7c478bd9Sstevel@tonic-gate 		if ((ckname(1) == F_SKIP) ||
6416*7c478bd9Sstevel@tonic-gate 		    (Ofile = openout(G_p->g_dirfd)) < 0) {
6417*7c478bd9Sstevel@tonic-gate 			data_in(P_SKIP);
6418*7c478bd9Sstevel@tonic-gate 		} else {
6419*7c478bd9Sstevel@tonic-gate 			data_in(P_PROC);
6420*7c478bd9Sstevel@tonic-gate 		}
6421*7c478bd9Sstevel@tonic-gate 		break;
6422*7c478bd9Sstevel@tonic-gate 	case CHRTYPE:
6423*7c478bd9Sstevel@tonic-gate 		/* character device or FIFO */
6424*7c478bd9Sstevel@tonic-gate 		if (ckname(1) != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
6425*7c478bd9Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
6426*7c478bd9Sstevel@tonic-gate 		}
6427*7c478bd9Sstevel@tonic-gate 		break;
6428*7c478bd9Sstevel@tonic-gate 	default:
6429*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("error: unknown file type\n"));
6430*7c478bd9Sstevel@tonic-gate 		break;
6431*7c478bd9Sstevel@tonic-gate 	}
6432*7c478bd9Sstevel@tonic-gate }
6433*7c478bd9Sstevel@tonic-gate 
6434*7c478bd9Sstevel@tonic-gate 
6435*7c478bd9Sstevel@tonic-gate /*
6436*7c478bd9Sstevel@tonic-gate  * This originally came from libgenIO/g_init.c
6437*7c478bd9Sstevel@tonic-gate  * XXX	And it is very broken.
6438*7c478bd9Sstevel@tonic-gate  */
6439*7c478bd9Sstevel@tonic-gate 
6440*7c478bd9Sstevel@tonic-gate /* #include <sys/statvfs.h> */
6441*7c478bd9Sstevel@tonic-gate #include <ftw.h>
6442*7c478bd9Sstevel@tonic-gate /* #include <libgenIO.h> */
6443*7c478bd9Sstevel@tonic-gate #define	G_TM_TAPE	1	/* Tapemaster controller    */
6444*7c478bd9Sstevel@tonic-gate #define	G_XY_DISK	3	/* xy disks		*/
6445*7c478bd9Sstevel@tonic-gate #define	G_SD_DISK	7	/* scsi sd disk		*/
6446*7c478bd9Sstevel@tonic-gate #define	G_XT_TAPE	8	/* xt tapes		*/
6447*7c478bd9Sstevel@tonic-gate #define	G_SF_FLOPPY	9	/* sf floppy		*/
6448*7c478bd9Sstevel@tonic-gate #define	G_XD_DISK	10	/* xd disks		*/
6449*7c478bd9Sstevel@tonic-gate #define	G_ST_TAPE	11	/* scsi tape		*/
6450*7c478bd9Sstevel@tonic-gate #define	G_NS		12	/* noswap pseudo-dev	*/
6451*7c478bd9Sstevel@tonic-gate #define	G_RAM		13	/* ram pseudo-dev	*/
6452*7c478bd9Sstevel@tonic-gate #define	G_FT		14	/* tftp			*/
6453*7c478bd9Sstevel@tonic-gate #define	G_HD		15	/* 386 network disk	*/
6454*7c478bd9Sstevel@tonic-gate #define	G_FD		16	/* 386 AT disk		*/
6455*7c478bd9Sstevel@tonic-gate #define	G_FILE		28	/* file, not a device	*/
6456*7c478bd9Sstevel@tonic-gate #define	G_NO_DEV	29	/* device does not require special treatment */
6457*7c478bd9Sstevel@tonic-gate #define	G_DEV_MAX	30	/* last valid device type */
6458*7c478bd9Sstevel@tonic-gate 
6459*7c478bd9Sstevel@tonic-gate /*
6460*7c478bd9Sstevel@tonic-gate  * g_init: Determine the device being accessed, set the buffer size,
6461*7c478bd9Sstevel@tonic-gate  * and perform any device specific initialization. Since at this point
6462*7c478bd9Sstevel@tonic-gate  * Sun has no system call to read the configuration, the major numbers
6463*7c478bd9Sstevel@tonic-gate  * are assumed to be static and types are figured out as such. However,
6464*7c478bd9Sstevel@tonic-gate  * as a rough estimate, the buffer size for all types is set to 512
6465*7c478bd9Sstevel@tonic-gate  * as a default.
6466*7c478bd9Sstevel@tonic-gate  */
6467*7c478bd9Sstevel@tonic-gate 
6468*7c478bd9Sstevel@tonic-gate static int
6469*7c478bd9Sstevel@tonic-gate g_init(int *devtype, int *fdes)
6470*7c478bd9Sstevel@tonic-gate {
6471*7c478bd9Sstevel@tonic-gate 	int bufsize;
6472*7c478bd9Sstevel@tonic-gate 	struct stat st_buf;
6473*7c478bd9Sstevel@tonic-gate 	struct statvfs stfs_buf;
6474*7c478bd9Sstevel@tonic-gate 
6475*7c478bd9Sstevel@tonic-gate 	*devtype = G_NO_DEV;
6476*7c478bd9Sstevel@tonic-gate 	bufsize = -1;
6477*7c478bd9Sstevel@tonic-gate 	if (fstat(*fdes, &st_buf) == -1)
6478*7c478bd9Sstevel@tonic-gate 		return (-1);
6479*7c478bd9Sstevel@tonic-gate 	if (!(st_buf.st_mode & S_IFCHR) || !(st_buf.st_mode & S_IFBLK)) {
6480*7c478bd9Sstevel@tonic-gate 		if (st_buf.st_mode & S_IFIFO) {
6481*7c478bd9Sstevel@tonic-gate 			bufsize = 512;
6482*7c478bd9Sstevel@tonic-gate 		} else {
6483*7c478bd9Sstevel@tonic-gate 			/* find block size for this file system */
6484*7c478bd9Sstevel@tonic-gate 			*devtype = G_FILE;
6485*7c478bd9Sstevel@tonic-gate 			if (fstatvfs(*fdes, &stfs_buf) < 0) {
6486*7c478bd9Sstevel@tonic-gate 					bufsize = -1;
6487*7c478bd9Sstevel@tonic-gate 					errno = ENODEV;
6488*7c478bd9Sstevel@tonic-gate 			} else
6489*7c478bd9Sstevel@tonic-gate 				bufsize = stfs_buf.f_bsize;
6490*7c478bd9Sstevel@tonic-gate 		}
6491*7c478bd9Sstevel@tonic-gate 
6492*7c478bd9Sstevel@tonic-gate 		return (bufsize);
6493*7c478bd9Sstevel@tonic-gate 
6494*7c478bd9Sstevel@tonic-gate 	/*
6495*7c478bd9Sstevel@tonic-gate 	 * We'll have to add a remote attribute to stat but this
6496*7c478bd9Sstevel@tonic-gate 	 * should work for now.
6497*7c478bd9Sstevel@tonic-gate 	 */
6498*7c478bd9Sstevel@tonic-gate 	} else if (st_buf.st_dev & 0x8000)	/* if remote  rdev */
6499*7c478bd9Sstevel@tonic-gate 		return (512);
6500*7c478bd9Sstevel@tonic-gate 
6501*7c478bd9Sstevel@tonic-gate 	bufsize = 512;
6502*7c478bd9Sstevel@tonic-gate 
6503*7c478bd9Sstevel@tonic-gate 	if (Hdr_type == BAR) {
6504*7c478bd9Sstevel@tonic-gate 		if (is_tape(*fdes)) {
6505*7c478bd9Sstevel@tonic-gate 			bufsize = BAR_TAPE_SIZE;
6506*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "Archiving to tape");
6507*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, " blocking factor 126\n");
6508*7c478bd9Sstevel@tonic-gate 		} else if (is_floppy(*fdes)) {
6509*7c478bd9Sstevel@tonic-gate 			bufsize = BAR_FLOPPY_SIZE;
6510*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "Archiving to floppy");
6511*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, " blocking factor 18\n");
6512*7c478bd9Sstevel@tonic-gate 		}
6513*7c478bd9Sstevel@tonic-gate 	}
6514*7c478bd9Sstevel@tonic-gate 
6515*7c478bd9Sstevel@tonic-gate 	return (bufsize);
6516*7c478bd9Sstevel@tonic-gate }
6517*7c478bd9Sstevel@tonic-gate 
6518*7c478bd9Sstevel@tonic-gate /*
6519*7c478bd9Sstevel@tonic-gate  * This originally came from libgenIO/g_read.c
6520*7c478bd9Sstevel@tonic-gate  */
6521*7c478bd9Sstevel@tonic-gate 
6522*7c478bd9Sstevel@tonic-gate /*
6523*7c478bd9Sstevel@tonic-gate  * g_read: Read nbytes of data from fdes (of type devtype) and place
6524*7c478bd9Sstevel@tonic-gate  * data in location pointed to by buf.  In case of end of medium,
6525*7c478bd9Sstevel@tonic-gate  * translate (where necessary) device specific EOM indications into
6526*7c478bd9Sstevel@tonic-gate  * the generic EOM indication of rv = -1, errno = ENOSPC.
6527*7c478bd9Sstevel@tonic-gate  */
6528*7c478bd9Sstevel@tonic-gate 
6529*7c478bd9Sstevel@tonic-gate static int
6530*7c478bd9Sstevel@tonic-gate g_read(int devtype, int fdes, char *buf, unsigned nbytes)
6531*7c478bd9Sstevel@tonic-gate {
6532*7c478bd9Sstevel@tonic-gate 	int rv;
6533*7c478bd9Sstevel@tonic-gate 
6534*7c478bd9Sstevel@tonic-gate 	if (devtype < 0 || devtype >= G_DEV_MAX) {
6535*7c478bd9Sstevel@tonic-gate 		errno = ENODEV;
6536*7c478bd9Sstevel@tonic-gate 		return (-1);
6537*7c478bd9Sstevel@tonic-gate 	}
6538*7c478bd9Sstevel@tonic-gate 
6539*7c478bd9Sstevel@tonic-gate 	rv = read(fdes, buf, nbytes);
6540*7c478bd9Sstevel@tonic-gate 
6541*7c478bd9Sstevel@tonic-gate 	/* st devices return 0 when no space left */
6542*7c478bd9Sstevel@tonic-gate 	if ((rv == 0 && errno == 0 && Hdr_type != BAR) ||
6543*7c478bd9Sstevel@tonic-gate 	    (rv == -1 && errno == EIO)) {
6544*7c478bd9Sstevel@tonic-gate 		errno = 0;
6545*7c478bd9Sstevel@tonic-gate 		rv = 0;
6546*7c478bd9Sstevel@tonic-gate 	}
6547*7c478bd9Sstevel@tonic-gate 
6548*7c478bd9Sstevel@tonic-gate 	return (rv);
6549*7c478bd9Sstevel@tonic-gate }
6550*7c478bd9Sstevel@tonic-gate 
6551*7c478bd9Sstevel@tonic-gate /*
6552*7c478bd9Sstevel@tonic-gate  * This originally came from libgenIO/g_write.c
6553*7c478bd9Sstevel@tonic-gate  */
6554*7c478bd9Sstevel@tonic-gate 
6555*7c478bd9Sstevel@tonic-gate /*
6556*7c478bd9Sstevel@tonic-gate  * g_write: Write nbytes of data to fdes (of type devtype) from
6557*7c478bd9Sstevel@tonic-gate  * the location pointed to by buf.  In case of end of medium,
6558*7c478bd9Sstevel@tonic-gate  * translate (where necessary) device specific EOM indications into
6559*7c478bd9Sstevel@tonic-gate  * the generic EOM indication of rv = -1, errno = ENOSPC.
6560*7c478bd9Sstevel@tonic-gate  */
6561*7c478bd9Sstevel@tonic-gate 
6562*7c478bd9Sstevel@tonic-gate static int
6563*7c478bd9Sstevel@tonic-gate g_write(int devtype, int fdes, char *buf, unsigned nbytes)
6564*7c478bd9Sstevel@tonic-gate {
6565*7c478bd9Sstevel@tonic-gate 	int rv;
6566*7c478bd9Sstevel@tonic-gate 
6567*7c478bd9Sstevel@tonic-gate 	if (devtype < 0 || devtype >= G_DEV_MAX) {
6568*7c478bd9Sstevel@tonic-gate 		errno = ENODEV;
6569*7c478bd9Sstevel@tonic-gate 		return (-1);
6570*7c478bd9Sstevel@tonic-gate 	}
6571*7c478bd9Sstevel@tonic-gate 
6572*7c478bd9Sstevel@tonic-gate 	rv = write(fdes, buf, nbytes);
6573*7c478bd9Sstevel@tonic-gate 
6574*7c478bd9Sstevel@tonic-gate 	/* st devices return 0 when no more space left */
6575*7c478bd9Sstevel@tonic-gate 	if ((rv == 0 && errno == 0) || (rv == -1 && errno == EIO)) {
6576*7c478bd9Sstevel@tonic-gate 		errno = ENOSPC;
6577*7c478bd9Sstevel@tonic-gate 		rv = -1;
6578*7c478bd9Sstevel@tonic-gate 	}
6579*7c478bd9Sstevel@tonic-gate 
6580*7c478bd9Sstevel@tonic-gate 	return (rv);
6581*7c478bd9Sstevel@tonic-gate }
6582*7c478bd9Sstevel@tonic-gate 
6583*7c478bd9Sstevel@tonic-gate /*
6584*7c478bd9Sstevel@tonic-gate  * Test for tape
6585*7c478bd9Sstevel@tonic-gate  */
6586*7c478bd9Sstevel@tonic-gate 
6587*7c478bd9Sstevel@tonic-gate static int
6588*7c478bd9Sstevel@tonic-gate is_tape(int fd)
6589*7c478bd9Sstevel@tonic-gate {
6590*7c478bd9Sstevel@tonic-gate 	struct mtget stuff;
6591*7c478bd9Sstevel@tonic-gate 
6592*7c478bd9Sstevel@tonic-gate 	/*
6593*7c478bd9Sstevel@tonic-gate 	 * try to do a generic tape ioctl, just to see if
6594*7c478bd9Sstevel@tonic-gate 	 * the thing is in fact a tape drive(er).
6595*7c478bd9Sstevel@tonic-gate 	 */
6596*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, MTIOCGET, &stuff) != -1) {
6597*7c478bd9Sstevel@tonic-gate 		/* the ioctl succeeded, must have been a tape */
6598*7c478bd9Sstevel@tonic-gate 		return (1);
6599*7c478bd9Sstevel@tonic-gate 	}
6600*7c478bd9Sstevel@tonic-gate 	return (0);
6601*7c478bd9Sstevel@tonic-gate }
6602*7c478bd9Sstevel@tonic-gate 
6603*7c478bd9Sstevel@tonic-gate /*
6604*7c478bd9Sstevel@tonic-gate  * Test for floppy
6605*7c478bd9Sstevel@tonic-gate  */
6606*7c478bd9Sstevel@tonic-gate 
6607*7c478bd9Sstevel@tonic-gate static int
6608*7c478bd9Sstevel@tonic-gate is_floppy(int fd)
6609*7c478bd9Sstevel@tonic-gate {
6610*7c478bd9Sstevel@tonic-gate 	struct fd_char stuff;
6611*7c478bd9Sstevel@tonic-gate 
6612*7c478bd9Sstevel@tonic-gate 	/*
6613*7c478bd9Sstevel@tonic-gate 	 * try to get the floppy drive characteristics, just to see if
6614*7c478bd9Sstevel@tonic-gate 	 * the thing is in fact a floppy drive(er).
6615*7c478bd9Sstevel@tonic-gate 	 */
6616*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, FDIOGCHAR, &stuff) != -1) {
6617*7c478bd9Sstevel@tonic-gate 		/* the ioctl succeeded, must have been a floppy */
6618*7c478bd9Sstevel@tonic-gate 		return (1);
6619*7c478bd9Sstevel@tonic-gate 	}
6620*7c478bd9Sstevel@tonic-gate 
6621*7c478bd9Sstevel@tonic-gate 	return (0);
6622*7c478bd9Sstevel@tonic-gate }
6623*7c478bd9Sstevel@tonic-gate 
6624*7c478bd9Sstevel@tonic-gate /*
6625*7c478bd9Sstevel@tonic-gate  * New functions for ACLs and other security attributes
6626*7c478bd9Sstevel@tonic-gate  */
6627*7c478bd9Sstevel@tonic-gate 
6628*7c478bd9Sstevel@tonic-gate /*
6629*7c478bd9Sstevel@tonic-gate  * The function appends the new security attribute info to the end of
6630*7c478bd9Sstevel@tonic-gate  * existing secinfo.
6631*7c478bd9Sstevel@tonic-gate  */
6632*7c478bd9Sstevel@tonic-gate static int
6633*7c478bd9Sstevel@tonic-gate append_secattr(
6634*7c478bd9Sstevel@tonic-gate 	char	**secinfo,	/* existing security info */
6635*7c478bd9Sstevel@tonic-gate 	int	*secinfo_len,	/* length of existing security info */
6636*7c478bd9Sstevel@tonic-gate 	int	size,		/* new attribute size: unit depends on type */
6637*7c478bd9Sstevel@tonic-gate 	char	*attrp,		/* new attribute data pointer */
6638*7c478bd9Sstevel@tonic-gate 	char	attr_type)	/* new attribute type */
6639*7c478bd9Sstevel@tonic-gate {
6640*7c478bd9Sstevel@tonic-gate 	char	*new_secinfo;
6641*7c478bd9Sstevel@tonic-gate 	char	*attrtext;
6642*7c478bd9Sstevel@tonic-gate 	size_t	newattrsize;
6643*7c478bd9Sstevel@tonic-gate 	int	oldsize;
6644*7c478bd9Sstevel@tonic-gate 
6645*7c478bd9Sstevel@tonic-gate 	/* no need to add */
6646*7c478bd9Sstevel@tonic-gate 	if (attrp == (char *)NULL) {
6647*7c478bd9Sstevel@tonic-gate 		return (0);
6648*7c478bd9Sstevel@tonic-gate 	}
6649*7c478bd9Sstevel@tonic-gate 
6650*7c478bd9Sstevel@tonic-gate 	switch (attr_type) {
6651*7c478bd9Sstevel@tonic-gate 	case UFSD_ACL:
6652*7c478bd9Sstevel@tonic-gate 		/* LINTED alignment */
6653*7c478bd9Sstevel@tonic-gate 		attrtext = acltotext((aclent_t *)attrp, size);
6654*7c478bd9Sstevel@tonic-gate 		if (attrtext == NULL) {
6655*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "acltotext failed\n");
6656*7c478bd9Sstevel@tonic-gate 			return (-1);
6657*7c478bd9Sstevel@tonic-gate 		}
6658*7c478bd9Sstevel@tonic-gate 		/* header: type + size = 8 */
6659*7c478bd9Sstevel@tonic-gate 		newattrsize = 8 + strlen(attrtext) + 1;
6660*7c478bd9Sstevel@tonic-gate 		attr = e_zalloc(E_NORMAL, newattrsize);
6661*7c478bd9Sstevel@tonic-gate 		if (attr == NULL) {
6662*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "can't allocate memory\n");
6663*7c478bd9Sstevel@tonic-gate 			return (-1);
6664*7c478bd9Sstevel@tonic-gate 		}
6665*7c478bd9Sstevel@tonic-gate 		attr->attr_type = '1';		/* UFSD_ACL */
6666*7c478bd9Sstevel@tonic-gate 		/* acl entry count */
6667*7c478bd9Sstevel@tonic-gate 		(void) sprintf(attr->attr_len, "%06o", size);
6668*7c478bd9Sstevel@tonic-gate 		(void) strcpy((char *)&attr->attr_info[0], attrtext);
6669*7c478bd9Sstevel@tonic-gate 		free(attrtext);
6670*7c478bd9Sstevel@tonic-gate 		break;
6671*7c478bd9Sstevel@tonic-gate 
6672*7c478bd9Sstevel@tonic-gate 		/* SunFed's case goes here */
6673*7c478bd9Sstevel@tonic-gate 
6674*7c478bd9Sstevel@tonic-gate 	default:
6675*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "unrecognized attribute type\n");
6676*7c478bd9Sstevel@tonic-gate 		return (-1);
6677*7c478bd9Sstevel@tonic-gate 	}
6678*7c478bd9Sstevel@tonic-gate 
6679*7c478bd9Sstevel@tonic-gate 	/* old security info + new attr header(8) + new attr */
6680*7c478bd9Sstevel@tonic-gate 	oldsize = *secinfo_len;
6681*7c478bd9Sstevel@tonic-gate 	*secinfo_len += newattrsize;
6682*7c478bd9Sstevel@tonic-gate 	new_secinfo = e_zalloc(E_NORMAL, (uint_t)*secinfo_len);
6683*7c478bd9Sstevel@tonic-gate 	if (new_secinfo == NULL) {
6684*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "can't allocate memory\n");
6685*7c478bd9Sstevel@tonic-gate 		*secinfo_len -= newattrsize;
6686*7c478bd9Sstevel@tonic-gate 		return (-1);
6687*7c478bd9Sstevel@tonic-gate 	}
6688*7c478bd9Sstevel@tonic-gate 
6689*7c478bd9Sstevel@tonic-gate 	(void) memcpy(new_secinfo, *secinfo, oldsize);
6690*7c478bd9Sstevel@tonic-gate 	(void) memcpy(new_secinfo + oldsize, attr, newattrsize);
6691*7c478bd9Sstevel@tonic-gate 
6692*7c478bd9Sstevel@tonic-gate 	free(*secinfo);
6693*7c478bd9Sstevel@tonic-gate 	*secinfo = new_secinfo;
6694*7c478bd9Sstevel@tonic-gate 	return (0);
6695*7c478bd9Sstevel@tonic-gate }
6696*7c478bd9Sstevel@tonic-gate 
6697*7c478bd9Sstevel@tonic-gate static void
6698*7c478bd9Sstevel@tonic-gate write_ancillary(char *secinfo, int len)
6699*7c478bd9Sstevel@tonic-gate {
6700*7c478bd9Sstevel@tonic-gate 	long    pad;
6701*7c478bd9Sstevel@tonic-gate 	long    cnt;
6702*7c478bd9Sstevel@tonic-gate 
6703*7c478bd9Sstevel@tonic-gate 	/* Just tranditional permissions or no security attribute info */
6704*7c478bd9Sstevel@tonic-gate 	if (len == 0) {
6705*7c478bd9Sstevel@tonic-gate 		return;
6706*7c478bd9Sstevel@tonic-gate 	}
6707*7c478bd9Sstevel@tonic-gate 
6708*7c478bd9Sstevel@tonic-gate 	/* write out security info */
6709*7c478bd9Sstevel@tonic-gate 	while (len > 0) {
6710*7c478bd9Sstevel@tonic-gate 		cnt = (unsigned)(len > CPIOBSZ) ? CPIOBSZ : len;
6711*7c478bd9Sstevel@tonic-gate 		FLUSH(cnt);
6712*7c478bd9Sstevel@tonic-gate 		errno = 0;
6713*7c478bd9Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, secinfo, (unsigned)cnt);
6714*7c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += cnt;
6715*7c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += (long)cnt;
6716*7c478bd9Sstevel@tonic-gate 		len -= (long)cnt;
6717*7c478bd9Sstevel@tonic-gate 	}
6718*7c478bd9Sstevel@tonic-gate 	pad = (Pad_val + 1 - (cnt & Pad_val)) & Pad_val;
6719*7c478bd9Sstevel@tonic-gate 	if (pad != 0) {
6720*7c478bd9Sstevel@tonic-gate 		FLUSH(pad);
6721*7c478bd9Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, Empty, pad);
6722*7c478bd9Sstevel@tonic-gate 		Buffr.b_in_p += pad;
6723*7c478bd9Sstevel@tonic-gate 		Buffr.b_cnt += pad;
6724*7c478bd9Sstevel@tonic-gate 	}
6725*7c478bd9Sstevel@tonic-gate }
6726*7c478bd9Sstevel@tonic-gate 
6727*7c478bd9Sstevel@tonic-gate static int
6728*7c478bd9Sstevel@tonic-gate remove_dir(char *path)
6729*7c478bd9Sstevel@tonic-gate {
6730*7c478bd9Sstevel@tonic-gate 	DIR		*name;
6731*7c478bd9Sstevel@tonic-gate 	struct dirent	*direct;
6732*7c478bd9Sstevel@tonic-gate 	struct stat	sbuf;
6733*7c478bd9Sstevel@tonic-gate 	char		*path_copy;
6734*7c478bd9Sstevel@tonic-gate 
6735*7c478bd9Sstevel@tonic-gate #define	MSG1	"remove_dir() failed to stat(\"%s\") "
6736*7c478bd9Sstevel@tonic-gate #define	MSG2	"remove_dir() failed to remove_dir(\"%s\") "
6737*7c478bd9Sstevel@tonic-gate #define	MSG3	"remove_dir() failed to unlink(\"%s\") "
6738*7c478bd9Sstevel@tonic-gate 
6739*7c478bd9Sstevel@tonic-gate 	/*
6740*7c478bd9Sstevel@tonic-gate 	 * Open the directory for reading.
6741*7c478bd9Sstevel@tonic-gate 	 */
6742*7c478bd9Sstevel@tonic-gate 	if ((name = opendir(path)) == NULL) {
6743*7c478bd9Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to opendir(\"%s\") ", path);
6744*7c478bd9Sstevel@tonic-gate 		return (-1);
6745*7c478bd9Sstevel@tonic-gate 	}
6746*7c478bd9Sstevel@tonic-gate 
6747*7c478bd9Sstevel@tonic-gate 	if (chdir(path) == -1) {
6748*7c478bd9Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to chdir(\"%s\") ", path);
6749*7c478bd9Sstevel@tonic-gate 		return (-1);
6750*7c478bd9Sstevel@tonic-gate 	}
6751*7c478bd9Sstevel@tonic-gate 
6752*7c478bd9Sstevel@tonic-gate 	/*
6753*7c478bd9Sstevel@tonic-gate 	 * Read every directory entry.
6754*7c478bd9Sstevel@tonic-gate 	 */
6755*7c478bd9Sstevel@tonic-gate 	while ((direct = readdir(name)) != NULL) {
6756*7c478bd9Sstevel@tonic-gate 		/*
6757*7c478bd9Sstevel@tonic-gate 		 * Ignore "." and ".." entries.
6758*7c478bd9Sstevel@tonic-gate 		 */
6759*7c478bd9Sstevel@tonic-gate 		if (strcmp(direct->d_name, ".") == 0 ||
6760*7c478bd9Sstevel@tonic-gate 		    strcmp(direct->d_name, "..") == 0)
6761*7c478bd9Sstevel@tonic-gate 			continue;
6762*7c478bd9Sstevel@tonic-gate 
6763*7c478bd9Sstevel@tonic-gate 			if (lstat(direct->d_name, &sbuf) == -1) {
6764*7c478bd9Sstevel@tonic-gate 				msg(ERRN, MSG1, direct->d_name);
6765*7c478bd9Sstevel@tonic-gate 				(void) closedir(name);
6766*7c478bd9Sstevel@tonic-gate 			return (-1);
6767*7c478bd9Sstevel@tonic-gate 		}
6768*7c478bd9Sstevel@tonic-gate 
6769*7c478bd9Sstevel@tonic-gate 		if (S_ISDIR(sbuf.st_mode)) {
6770*7c478bd9Sstevel@tonic-gate 			if (remove_dir(direct->d_name) == -1) {
6771*7c478bd9Sstevel@tonic-gate 				msg(ERRN, MSG2, direct->d_name);
6772*7c478bd9Sstevel@tonic-gate 				(void) closedir(name);
6773*7c478bd9Sstevel@tonic-gate 				return (-1);
6774*7c478bd9Sstevel@tonic-gate 			}
6775*7c478bd9Sstevel@tonic-gate 		} else {
6776*7c478bd9Sstevel@tonic-gate 			if (unlink(direct->d_name) == -1) {
6777*7c478bd9Sstevel@tonic-gate 				msg(ERRN, MSG3, direct->d_name);
6778*7c478bd9Sstevel@tonic-gate 				(void) closedir(name);
6779*7c478bd9Sstevel@tonic-gate 				return (-1);
6780*7c478bd9Sstevel@tonic-gate 			}
6781*7c478bd9Sstevel@tonic-gate 		}
6782*7c478bd9Sstevel@tonic-gate 
6783*7c478bd9Sstevel@tonic-gate 	}
6784*7c478bd9Sstevel@tonic-gate 
6785*7c478bd9Sstevel@tonic-gate 	/*
6786*7c478bd9Sstevel@tonic-gate 	 * Close the directory we just finished reading.
6787*7c478bd9Sstevel@tonic-gate 	 */
6788*7c478bd9Sstevel@tonic-gate 	(void) closedir(name);
6789*7c478bd9Sstevel@tonic-gate 
6790*7c478bd9Sstevel@tonic-gate 	/*
6791*7c478bd9Sstevel@tonic-gate 	 * Change directory to the parent directory...
6792*7c478bd9Sstevel@tonic-gate 	 */
6793*7c478bd9Sstevel@tonic-gate 	if (chdir("..") == -1) {
6794*7c478bd9Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to chdir(\"..\") ");
6795*7c478bd9Sstevel@tonic-gate 		return (-1);
6796*7c478bd9Sstevel@tonic-gate 	}
6797*7c478bd9Sstevel@tonic-gate 
6798*7c478bd9Sstevel@tonic-gate 	/*
6799*7c478bd9Sstevel@tonic-gate 	 * ...and finally remove the directory; note we have to
6800*7c478bd9Sstevel@tonic-gate 	 * make a copy since basename is free to modify its input.
6801*7c478bd9Sstevel@tonic-gate 	 */
6802*7c478bd9Sstevel@tonic-gate 	path_copy = e_strdup(E_NORMAL, path);
6803*7c478bd9Sstevel@tonic-gate 	if (path_copy == NULL) {
6804*7c478bd9Sstevel@tonic-gate 		msg(ERRN, "cannot strdup() the directory pathname ");
6805*7c478bd9Sstevel@tonic-gate 		return (-1);
6806*7c478bd9Sstevel@tonic-gate 	}
6807*7c478bd9Sstevel@tonic-gate 
6808*7c478bd9Sstevel@tonic-gate 	if (rmdir(basename(path_copy)) == -1) {
6809*7c478bd9Sstevel@tonic-gate 		free(path_copy);
6810*7c478bd9Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to rmdir(\"%s\") ", path);
6811*7c478bd9Sstevel@tonic-gate 		return (-1);
6812*7c478bd9Sstevel@tonic-gate 	}
6813*7c478bd9Sstevel@tonic-gate 
6814*7c478bd9Sstevel@tonic-gate 	free(path_copy);
6815*7c478bd9Sstevel@tonic-gate 	return (0);
6816*7c478bd9Sstevel@tonic-gate 
6817*7c478bd9Sstevel@tonic-gate }
6818*7c478bd9Sstevel@tonic-gate 
6819*7c478bd9Sstevel@tonic-gate static int
6820*7c478bd9Sstevel@tonic-gate save_cwd(void)
6821*7c478bd9Sstevel@tonic-gate {
6822*7c478bd9Sstevel@tonic-gate 	return (open(".", O_RDONLY));
6823*7c478bd9Sstevel@tonic-gate }
6824*7c478bd9Sstevel@tonic-gate 
6825*7c478bd9Sstevel@tonic-gate static void
6826*7c478bd9Sstevel@tonic-gate rest_cwd(int cwd)
6827*7c478bd9Sstevel@tonic-gate {
6828*7c478bd9Sstevel@tonic-gate 	(void) fchdir(cwd);
6829*7c478bd9Sstevel@tonic-gate 	(void) close(cwd);
6830*7c478bd9Sstevel@tonic-gate }
6831*7c478bd9Sstevel@tonic-gate 
6832*7c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
6833*7c478bd9Sstevel@tonic-gate static void
6834*7c478bd9Sstevel@tonic-gate xattrs_out(int (*func)())
6835*7c478bd9Sstevel@tonic-gate {
6836*7c478bd9Sstevel@tonic-gate 	int dirpfd;
6837*7c478bd9Sstevel@tonic-gate 	int filefd;
6838*7c478bd9Sstevel@tonic-gate 	DIR *dirp;
6839*7c478bd9Sstevel@tonic-gate 	struct dirent *dp;
6840*7c478bd9Sstevel@tonic-gate 	int slen;
6841*7c478bd9Sstevel@tonic-gate 	char *namep, *savenamep;
6842*7c478bd9Sstevel@tonic-gate 
6843*7c478bd9Sstevel@tonic-gate 	if (pathconf(G_p->g_nam_p, _PC_XATTR_EXISTS) != 1) {
6844*7c478bd9Sstevel@tonic-gate 		return;
6845*7c478bd9Sstevel@tonic-gate 	}
6846*7c478bd9Sstevel@tonic-gate 
6847*7c478bd9Sstevel@tonic-gate 	/*
6848*7c478bd9Sstevel@tonic-gate 	 * If aclp still exists then free it since it is was set when base
6849*7c478bd9Sstevel@tonic-gate 	 * file was extracted.
6850*7c478bd9Sstevel@tonic-gate 	 */
6851*7c478bd9Sstevel@tonic-gate 	if (aclp != (aclent_t *)NULL) {
6852*7c478bd9Sstevel@tonic-gate 		free(aclp);
6853*7c478bd9Sstevel@tonic-gate 		aclcnt = 0;
6854*7c478bd9Sstevel@tonic-gate 		aclp = NULL;
6855*7c478bd9Sstevel@tonic-gate 		acl_set = 0;
6856*7c478bd9Sstevel@tonic-gate 	}
6857*7c478bd9Sstevel@tonic-gate 
6858*7c478bd9Sstevel@tonic-gate 	Gen.g_dirfd = attropen(G_p->g_nam_p, ".", O_RDONLY);
6859*7c478bd9Sstevel@tonic-gate 	if (Gen.g_dirfd == -1) {
6860*7c478bd9Sstevel@tonic-gate 		msg(ERRN, "Cannot open attribute directory of file \"%s\"",
6861*7c478bd9Sstevel@tonic-gate 		    G_p->g_nam_p);
6862*7c478bd9Sstevel@tonic-gate 		return;
6863*7c478bd9Sstevel@tonic-gate 
6864*7c478bd9Sstevel@tonic-gate 	}
6865*7c478bd9Sstevel@tonic-gate 	savenamep = G_p->g_nam_p;
6866*7c478bd9Sstevel@tonic-gate 
6867*7c478bd9Sstevel@tonic-gate 	if ((dirpfd = dup(Gen.g_dirfd)) == -1)  {
6868*7c478bd9Sstevel@tonic-gate 		msg(ERRN, "Cannot dup(2) attribute directory descriptor");
6869*7c478bd9Sstevel@tonic-gate 		return;
6870*7c478bd9Sstevel@tonic-gate 	}
6871*7c478bd9Sstevel@tonic-gate 
6872*7c478bd9Sstevel@tonic-gate 	if ((dirp = fdopendir(dirpfd)) == (DIR *)NULL) {
6873*7c478bd9Sstevel@tonic-gate 		msg(ERRN, "Cannot fdopendir(2) directory file descriptor");
6874*7c478bd9Sstevel@tonic-gate 		return;
6875*7c478bd9Sstevel@tonic-gate 	}
6876*7c478bd9Sstevel@tonic-gate 
6877*7c478bd9Sstevel@tonic-gate 	while ((dp = readdir(dirp)) != (struct dirent *)NULL) {
6878*7c478bd9Sstevel@tonic-gate 		if (strcmp(dp->d_name, "..") == 0) {
6879*7c478bd9Sstevel@tonic-gate 			continue;
6880*7c478bd9Sstevel@tonic-gate 		}
6881*7c478bd9Sstevel@tonic-gate 
6882*7c478bd9Sstevel@tonic-gate 		if (strcmp(dp->d_name, ".") == 0) {
6883*7c478bd9Sstevel@tonic-gate 			Hiddendir = 1;
6884*7c478bd9Sstevel@tonic-gate 		} else {
6885*7c478bd9Sstevel@tonic-gate 			Hiddendir = 0;
6886*7c478bd9Sstevel@tonic-gate 		}
6887*7c478bd9Sstevel@tonic-gate 
6888*7c478bd9Sstevel@tonic-gate 		Gen.g_attrnam_p = dp->d_name;
6889*7c478bd9Sstevel@tonic-gate 
6890*7c478bd9Sstevel@tonic-gate 		if (STAT(Gen.g_dirfd, Gen.g_nam_p, &SrcSt) == -1) {
6891*7c478bd9Sstevel@tonic-gate 			msg(ERRN,
6892*7c478bd9Sstevel@tonic-gate 			    "Could not fstatat(2) attribute \"%s\" of"
6893*7c478bd9Sstevel@tonic-gate 			    " file \"%s\"", dp->d_name, savenamep);
6894*7c478bd9Sstevel@tonic-gate 			continue;
6895*7c478bd9Sstevel@tonic-gate 		}
6896*7c478bd9Sstevel@tonic-gate 
6897*7c478bd9Sstevel@tonic-gate 		if (Use_old_stat) {
6898*7c478bd9Sstevel@tonic-gate 			OldSt = convert_to_old_stat(&SrcSt,
6899*7c478bd9Sstevel@tonic-gate 			    Gen.g_nam_p, Gen.g_attrnam_p);
6900*7c478bd9Sstevel@tonic-gate 
6901*7c478bd9Sstevel@tonic-gate 			if (OldSt == NULL) {
6902*7c478bd9Sstevel@tonic-gate 				msg(ERRN,
6903*7c478bd9Sstevel@tonic-gate 				    "Could not convert to old stat format");
6904*7c478bd9Sstevel@tonic-gate 				continue;
6905*7c478bd9Sstevel@tonic-gate 			}
6906*7c478bd9Sstevel@tonic-gate 		}
6907*7c478bd9Sstevel@tonic-gate 
6908*7c478bd9Sstevel@tonic-gate 		Gen.g_attrfnam_p = savenamep;
6909*7c478bd9Sstevel@tonic-gate 
6910*7c478bd9Sstevel@tonic-gate 		/*
6911*7c478bd9Sstevel@tonic-gate 		 * Set up dummy header name
6912*7c478bd9Sstevel@tonic-gate 		 *
6913*7c478bd9Sstevel@tonic-gate 		 * One piece is written with .hdr, which
6914*7c478bd9Sstevel@tonic-gate 		 * contains the actual xattr hdr or pathing information
6915*7c478bd9Sstevel@tonic-gate 		 * then the name is updated to drop the .hdr off
6916*7c478bd9Sstevel@tonic-gate 		 * and the actual file itself is archived.
6917*7c478bd9Sstevel@tonic-gate 		 */
6918*7c478bd9Sstevel@tonic-gate 		slen = strlen(Gen.g_attrnam_p) + strlen(DEVNULL) +
6919*7c478bd9Sstevel@tonic-gate 		    strlen(XATTRHDR) + 1;
6920*7c478bd9Sstevel@tonic-gate 		if ((namep = e_zalloc(E_NORMAL, slen)) == (char *)NULL) {
6921*7c478bd9Sstevel@tonic-gate 			msg(ERRN, "Could not calloc memory for attribute name");
6922*7c478bd9Sstevel@tonic-gate 			continue;
6923*7c478bd9Sstevel@tonic-gate 		}
6924*7c478bd9Sstevel@tonic-gate 		(void) sprintf(namep, "%s/%s%s",
6925*7c478bd9Sstevel@tonic-gate 		    DEVNULL, Gen.g_attrnam_p, XATTRHDR);
6926*7c478bd9Sstevel@tonic-gate 		Gen.g_nam_p = namep;
6927*7c478bd9Sstevel@tonic-gate 
6928*7c478bd9Sstevel@tonic-gate 		/*
6929*7c478bd9Sstevel@tonic-gate 		 * Get attribute's ACL info: don't bother allocating space
6930*7c478bd9Sstevel@tonic-gate 		 * if there are only standard permissions, i.e. ACL count < 4
6931*7c478bd9Sstevel@tonic-gate 		 */
6932*7c478bd9Sstevel@tonic-gate 		if (Pflag) {
6933*7c478bd9Sstevel@tonic-gate 			filefd = openat(Gen.g_dirfd, dp->d_name, O_RDONLY);
6934*7c478bd9Sstevel@tonic-gate 			if (filefd == -1) {
6935*7c478bd9Sstevel@tonic-gate 				msg(ERRN,
6936*7c478bd9Sstevel@tonic-gate 				    "Could not open attribute \"%s\" of"
6937*7c478bd9Sstevel@tonic-gate 				    " file \"%s\"", dp->d_name, savenamep);
6938*7c478bd9Sstevel@tonic-gate 				free(namep);
6939*7c478bd9Sstevel@tonic-gate 				continue;
6940*7c478bd9Sstevel@tonic-gate 			}
6941*7c478bd9Sstevel@tonic-gate 			if ((aclcnt = facl(filefd, GETACLCNT,
6942*7c478bd9Sstevel@tonic-gate 			    0, NULL)) < 0) {
6943*7c478bd9Sstevel@tonic-gate 				msg(ERRN,
6944*7c478bd9Sstevel@tonic-gate 				    "Error with acl() on %s",
6945*7c478bd9Sstevel@tonic-gate 				    Gen.g_nam_p);
6946*7c478bd9Sstevel@tonic-gate 			}
6947*7c478bd9Sstevel@tonic-gate 			if (aclcnt > MIN_ACL_ENTRIES) {
6948*7c478bd9Sstevel@tonic-gate 				aclp = e_zalloc(E_EXIT,
6949*7c478bd9Sstevel@tonic-gate 					sizeof (aclent_t) * aclcnt);
6950*7c478bd9Sstevel@tonic-gate 
6951*7c478bd9Sstevel@tonic-gate 				if (facl(filefd, GETACL, aclcnt, aclp) < 0) {
6952*7c478bd9Sstevel@tonic-gate 					msg(ERRN,
6953*7c478bd9Sstevel@tonic-gate 					    "Error with getacl() on %s",
6954*7c478bd9Sstevel@tonic-gate 					    Gen.g_nam_p);
6955*7c478bd9Sstevel@tonic-gate 					free(aclp);
6956*7c478bd9Sstevel@tonic-gate 					aclp = NULL;
6957*7c478bd9Sstevel@tonic-gate 				}
6958*7c478bd9Sstevel@tonic-gate 			}
6959*7c478bd9Sstevel@tonic-gate 			(void) close(filefd);
6960*7c478bd9Sstevel@tonic-gate 		}
6961*7c478bd9Sstevel@tonic-gate 		(void) creat_hdr();
6962*7c478bd9Sstevel@tonic-gate 		(void) (*func)();
6963*7c478bd9Sstevel@tonic-gate 		if (Gen.g_passdirfd != -1) {
6964*7c478bd9Sstevel@tonic-gate 			(void) close(Gen.g_passdirfd);
6965*7c478bd9Sstevel@tonic-gate 			Gen.g_passdirfd = -1;
6966*7c478bd9Sstevel@tonic-gate 		}
6967*7c478bd9Sstevel@tonic-gate 		Gen.g_attrnam_p = (char *)NULL;
6968*7c478bd9Sstevel@tonic-gate 		Gen.g_attrfnam_p = (char *)NULL;
6969*7c478bd9Sstevel@tonic-gate 		Gen.g_linktoattrfnam_p = (char *)NULL;
6970*7c478bd9Sstevel@tonic-gate 		Gen.g_linktoattrnam_p = (char *)NULL;
6971*7c478bd9Sstevel@tonic-gate 		if (aclp != (aclent_t *)NULL) {
6972*7c478bd9Sstevel@tonic-gate 			free(aclp);
6973*7c478bd9Sstevel@tonic-gate 			aclcnt = 0;
6974*7c478bd9Sstevel@tonic-gate 			aclp = NULL;
6975*7c478bd9Sstevel@tonic-gate 			acl_set = 0;
6976*7c478bd9Sstevel@tonic-gate 		}
6977*7c478bd9Sstevel@tonic-gate 		free(namep);
6978*7c478bd9Sstevel@tonic-gate 	}
6979*7c478bd9Sstevel@tonic-gate 
6980*7c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
6981*7c478bd9Sstevel@tonic-gate 	(void) close(Gen.g_dirfd);
6982*7c478bd9Sstevel@tonic-gate 	Gen.g_dirfd = -1;
6983*7c478bd9Sstevel@tonic-gate }
6984*7c478bd9Sstevel@tonic-gate #else
6985*7c478bd9Sstevel@tonic-gate static void
6986*7c478bd9Sstevel@tonic-gate xattrs_out(int (*func)())
6987*7c478bd9Sstevel@tonic-gate {
6988*7c478bd9Sstevel@tonic-gate }
6989*7c478bd9Sstevel@tonic-gate #endif
6990*7c478bd9Sstevel@tonic-gate 
6991*7c478bd9Sstevel@tonic-gate /*
6992*7c478bd9Sstevel@tonic-gate  * Return the parent directory of a given path.
6993*7c478bd9Sstevel@tonic-gate  *
6994*7c478bd9Sstevel@tonic-gate  * Examples:
6995*7c478bd9Sstevel@tonic-gate  * /usr/tmp return /usr
6996*7c478bd9Sstevel@tonic-gate  * /usr/tmp/file return /usr/tmp
6997*7c478bd9Sstevel@tonic-gate  * /  returns .
6998*7c478bd9Sstevel@tonic-gate  * /usr returns /
6999*7c478bd9Sstevel@tonic-gate  * file returns .
7000*7c478bd9Sstevel@tonic-gate  *
7001*7c478bd9Sstevel@tonic-gate  * dir is assumed to be at least as big as path.
7002*7c478bd9Sstevel@tonic-gate  */
7003*7c478bd9Sstevel@tonic-gate static void
7004*7c478bd9Sstevel@tonic-gate get_parent(char *path, char *dir)
7005*7c478bd9Sstevel@tonic-gate {
7006*7c478bd9Sstevel@tonic-gate 	char *s;
7007*7c478bd9Sstevel@tonic-gate 	char tmpdir[PATH_MAX + 1];
7008*7c478bd9Sstevel@tonic-gate 
7009*7c478bd9Sstevel@tonic-gate 	if (strlen(path) > PATH_MAX) {
7010*7c478bd9Sstevel@tonic-gate 		msg(EXT, "pathname is too long");
7011*7c478bd9Sstevel@tonic-gate 	}
7012*7c478bd9Sstevel@tonic-gate 	(void) strcpy(tmpdir, path);
7013*7c478bd9Sstevel@tonic-gate 	chop_endslashes(tmpdir);
7014*7c478bd9Sstevel@tonic-gate 
7015*7c478bd9Sstevel@tonic-gate 	if ((s = strrchr(tmpdir, '/')) == NULL) {
7016*7c478bd9Sstevel@tonic-gate 		(void) strcpy(dir, ".");
7017*7c478bd9Sstevel@tonic-gate 	} else {
7018*7c478bd9Sstevel@tonic-gate 		s = skipslashes(s, tmpdir);
7019*7c478bd9Sstevel@tonic-gate 		*s = '\0';
7020*7c478bd9Sstevel@tonic-gate 		if (s == tmpdir)
7021*7c478bd9Sstevel@tonic-gate 			(void) strcpy(dir, "/");
7022*7c478bd9Sstevel@tonic-gate 		else
7023*7c478bd9Sstevel@tonic-gate 			(void) strcpy(dir, tmpdir);
7024*7c478bd9Sstevel@tonic-gate 	}
7025*7c478bd9Sstevel@tonic-gate }
7026*7c478bd9Sstevel@tonic-gate 
7027*7c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
7028*7c478bd9Sstevel@tonic-gate #define	ROUNDTOTBLOCK(a)		((a + (TBLOCK -1)) & ~(TBLOCK -1))
7029*7c478bd9Sstevel@tonic-gate 
7030*7c478bd9Sstevel@tonic-gate static void
7031*7c478bd9Sstevel@tonic-gate prepare_xattr_hdr(
7032*7c478bd9Sstevel@tonic-gate 	char		**attrbuf,
7033*7c478bd9Sstevel@tonic-gate 	char		*filename,
7034*7c478bd9Sstevel@tonic-gate 	char		*attrname,
7035*7c478bd9Sstevel@tonic-gate 	char		typeflag,
7036*7c478bd9Sstevel@tonic-gate 	struct Lnk	*linkinfo,
7037*7c478bd9Sstevel@tonic-gate 	int		*rlen)
7038*7c478bd9Sstevel@tonic-gate {
7039*7c478bd9Sstevel@tonic-gate 	char			*bufhead;	/* ptr to full buffer */
7040*7c478bd9Sstevel@tonic-gate 	struct xattr_hdr 	*hptr;		/* ptr to header in bufhead */
7041*7c478bd9Sstevel@tonic-gate 	struct xattr_buf	*tptr;		/* ptr to pathing pieces */
7042*7c478bd9Sstevel@tonic-gate 	int			totalen;	/* total buffer length */
7043*7c478bd9Sstevel@tonic-gate 	int			len;		/* length returned to user */
7044*7c478bd9Sstevel@tonic-gate 	int			stringlen;	/* length of filename + attr */
7045*7c478bd9Sstevel@tonic-gate 	int			linkstringlen;	/* ditto in link section */
7046*7c478bd9Sstevel@tonic-gate 	int			complen;	/* length of pathing section */
7047*7c478bd9Sstevel@tonic-gate 	int			linklen;	/* length of link section */
7048*7c478bd9Sstevel@tonic-gate 
7049*7c478bd9Sstevel@tonic-gate 
7050*7c478bd9Sstevel@tonic-gate 	/*
7051*7c478bd9Sstevel@tonic-gate 	 * Release previous buffer if any.
7052*7c478bd9Sstevel@tonic-gate 	 */
7053*7c478bd9Sstevel@tonic-gate 
7054*7c478bd9Sstevel@tonic-gate 	if (*attrbuf != (char *)NULL) {
7055*7c478bd9Sstevel@tonic-gate 		free(*attrbuf);
7056*7c478bd9Sstevel@tonic-gate 		*attrbuf = NULL;
7057*7c478bd9Sstevel@tonic-gate 	}
7058*7c478bd9Sstevel@tonic-gate 
7059*7c478bd9Sstevel@tonic-gate 	/*
7060*7c478bd9Sstevel@tonic-gate 	 * First add in fixed size stuff
7061*7c478bd9Sstevel@tonic-gate 	 */
7062*7c478bd9Sstevel@tonic-gate 	len = sizeof (struct xattr_hdr) + sizeof (struct xattr_buf);
7063*7c478bd9Sstevel@tonic-gate 
7064*7c478bd9Sstevel@tonic-gate 	/*
7065*7c478bd9Sstevel@tonic-gate 	 * Add space for two nulls
7066*7c478bd9Sstevel@tonic-gate 	 */
7067*7c478bd9Sstevel@tonic-gate 	stringlen = strlen(attrname) + strlen(filename) + 2;
7068*7c478bd9Sstevel@tonic-gate 	complen = stringlen + sizeof (struct xattr_buf);
7069*7c478bd9Sstevel@tonic-gate 
7070*7c478bd9Sstevel@tonic-gate 	len += stringlen;
7071*7c478bd9Sstevel@tonic-gate 
7072*7c478bd9Sstevel@tonic-gate 	/*
7073*7c478bd9Sstevel@tonic-gate 	 * Now add on space for link info if any
7074*7c478bd9Sstevel@tonic-gate 	 */
7075*7c478bd9Sstevel@tonic-gate 
7076*7c478bd9Sstevel@tonic-gate 	if (linkinfo != NULL) {
7077*7c478bd9Sstevel@tonic-gate 		/*
7078*7c478bd9Sstevel@tonic-gate 		 * Again add space for two nulls
7079*7c478bd9Sstevel@tonic-gate 		 */
7080*7c478bd9Sstevel@tonic-gate 		linkstringlen = strlen(linkinfo->L_gen.g_attrfnam_p) +
7081*7c478bd9Sstevel@tonic-gate 		    strlen(linkinfo->L_gen.g_attrnam_p) + 2;
7082*7c478bd9Sstevel@tonic-gate 		len += linkstringlen;
7083*7c478bd9Sstevel@tonic-gate 	}
7084*7c478bd9Sstevel@tonic-gate 
7085*7c478bd9Sstevel@tonic-gate 	/*
7086*7c478bd9Sstevel@tonic-gate 	 * Now add padding to end to fill out TBLOCK
7087*7c478bd9Sstevel@tonic-gate 	 *
7088*7c478bd9Sstevel@tonic-gate 	 * Function returns size of real data and not size + padding.
7089*7c478bd9Sstevel@tonic-gate 	 */
7090*7c478bd9Sstevel@tonic-gate 
7091*7c478bd9Sstevel@tonic-gate 	totalen = ROUNDTOTBLOCK(len);
7092*7c478bd9Sstevel@tonic-gate 	bufhead = e_zalloc(E_EXIT, totalen);
7093*7c478bd9Sstevel@tonic-gate 
7094*7c478bd9Sstevel@tonic-gate 	/*
7095*7c478bd9Sstevel@tonic-gate 	 * Now we can fill in the necessary pieces
7096*7c478bd9Sstevel@tonic-gate 	 */
7097*7c478bd9Sstevel@tonic-gate 
7098*7c478bd9Sstevel@tonic-gate 	if (linkinfo != (struct Lnk *)NULL) {
7099*7c478bd9Sstevel@tonic-gate 		linklen = linkstringlen + (sizeof (struct xattr_buf));
7100*7c478bd9Sstevel@tonic-gate 	} else {
7101*7c478bd9Sstevel@tonic-gate 		linklen = 0;
7102*7c478bd9Sstevel@tonic-gate 	}
7103*7c478bd9Sstevel@tonic-gate 
7104*7c478bd9Sstevel@tonic-gate 	/*
7105*7c478bd9Sstevel@tonic-gate 	 * first fill in the fixed header
7106*7c478bd9Sstevel@tonic-gate 	 */
7107*7c478bd9Sstevel@tonic-gate 	hptr = (struct xattr_hdr *)bufhead;
7108*7c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_version, "%s", XATTR_ARCH_VERS);
7109*7c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_component_len, "%0*d",
7110*7c478bd9Sstevel@tonic-gate 	    sizeof (hptr->h_component_len) - 1, complen);
7111*7c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_link_component_len, "%0*d",
7112*7c478bd9Sstevel@tonic-gate 	    sizeof (hptr->h_link_component_len) - 1, linklen);
7113*7c478bd9Sstevel@tonic-gate 	(void) sprintf(hptr->h_size, "%0*d", sizeof (hptr->h_size) - 1, len);
7114*7c478bd9Sstevel@tonic-gate 
7115*7c478bd9Sstevel@tonic-gate 	/*
7116*7c478bd9Sstevel@tonic-gate 	 * Now fill in the filename + attrnames section
7117*7c478bd9Sstevel@tonic-gate 	 */
7118*7c478bd9Sstevel@tonic-gate 
7119*7c478bd9Sstevel@tonic-gate 	tptr = (struct xattr_buf *)(bufhead + sizeof (struct xattr_hdr));
7120*7c478bd9Sstevel@tonic-gate 	(void) sprintf(tptr->h_namesz, "%0*d", sizeof (tptr->h_namesz) - 1,
7121*7c478bd9Sstevel@tonic-gate 	    stringlen);
7122*7c478bd9Sstevel@tonic-gate 	(void) strcpy(tptr->h_names, filename);
7123*7c478bd9Sstevel@tonic-gate 	(void) strcpy(&tptr->h_names[strlen(filename) + 1], attrname);
7124*7c478bd9Sstevel@tonic-gate 	tptr->h_typeflag = typeflag;
7125*7c478bd9Sstevel@tonic-gate 
7126*7c478bd9Sstevel@tonic-gate 	/*
7127*7c478bd9Sstevel@tonic-gate 	 * Now fill in the optional link section if we have one
7128*7c478bd9Sstevel@tonic-gate 	 */
7129*7c478bd9Sstevel@tonic-gate 
7130*7c478bd9Sstevel@tonic-gate 	if (linkinfo != (struct Lnk *)NULL) {
7131*7c478bd9Sstevel@tonic-gate 		tptr = (struct xattr_buf *)(bufhead +
7132*7c478bd9Sstevel@tonic-gate 		    sizeof (struct xattr_hdr) + complen);
7133*7c478bd9Sstevel@tonic-gate 
7134*7c478bd9Sstevel@tonic-gate 		(void) sprintf(tptr->h_namesz, "%0*d",
7135*7c478bd9Sstevel@tonic-gate 		    sizeof (tptr->h_namesz) - 1, linkstringlen);
7136*7c478bd9Sstevel@tonic-gate 		(void) strcpy(tptr->h_names, linkinfo->L_gen.g_attrfnam_p);
7137*7c478bd9Sstevel@tonic-gate 		(void) strcpy(
7138*7c478bd9Sstevel@tonic-gate 		    &tptr->h_names[strlen(linkinfo->L_gen.g_attrfnam_p) + 1],
7139*7c478bd9Sstevel@tonic-gate 		    linkinfo->L_gen.g_attrnam_p);
7140*7c478bd9Sstevel@tonic-gate 		tptr->h_typeflag = typeflag;
7141*7c478bd9Sstevel@tonic-gate 	}
7142*7c478bd9Sstevel@tonic-gate 	*attrbuf = (char *)bufhead;
7143*7c478bd9Sstevel@tonic-gate 	*rlen = len;
7144*7c478bd9Sstevel@tonic-gate }
7145*7c478bd9Sstevel@tonic-gate #endif /* O_XATTR */
7146*7c478bd9Sstevel@tonic-gate 
7147*7c478bd9Sstevel@tonic-gate static char
7148*7c478bd9Sstevel@tonic-gate tartype(int type)
7149*7c478bd9Sstevel@tonic-gate {
7150*7c478bd9Sstevel@tonic-gate 	switch (type) {
7151*7c478bd9Sstevel@tonic-gate 
7152*7c478bd9Sstevel@tonic-gate 	case S_IFDIR:
7153*7c478bd9Sstevel@tonic-gate 		return (DIRTYPE);
7154*7c478bd9Sstevel@tonic-gate 
7155*7c478bd9Sstevel@tonic-gate 	case S_IFLNK:
7156*7c478bd9Sstevel@tonic-gate 		return (SYMTYPE);
7157*7c478bd9Sstevel@tonic-gate 
7158*7c478bd9Sstevel@tonic-gate 	case S_IFIFO:
7159*7c478bd9Sstevel@tonic-gate 		return (FIFOTYPE);
7160*7c478bd9Sstevel@tonic-gate 
7161*7c478bd9Sstevel@tonic-gate 	case S_IFCHR:
7162*7c478bd9Sstevel@tonic-gate 		return (CHRTYPE);
7163*7c478bd9Sstevel@tonic-gate 
7164*7c478bd9Sstevel@tonic-gate 	case S_IFBLK:
7165*7c478bd9Sstevel@tonic-gate 		return (BLKTYPE);
7166*7c478bd9Sstevel@tonic-gate 
7167*7c478bd9Sstevel@tonic-gate 	case S_IFREG:
7168*7c478bd9Sstevel@tonic-gate 		return (REGTYPE);
7169*7c478bd9Sstevel@tonic-gate 
7170*7c478bd9Sstevel@tonic-gate 	default:
7171*7c478bd9Sstevel@tonic-gate 		return ('\0');
7172*7c478bd9Sstevel@tonic-gate 	}
7173*7c478bd9Sstevel@tonic-gate }
7174*7c478bd9Sstevel@tonic-gate 
7175*7c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
7176*7c478bd9Sstevel@tonic-gate static int
7177*7c478bd9Sstevel@tonic-gate openfile(int omode)
7178*7c478bd9Sstevel@tonic-gate {
7179*7c478bd9Sstevel@tonic-gate 
7180*7c478bd9Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
7181*7c478bd9Sstevel@tonic-gate 		return (attropen(G_p->g_attrfnam_p, G_p->g_attrnam_p, omode));
7182*7c478bd9Sstevel@tonic-gate 	} else {
7183*7c478bd9Sstevel@tonic-gate 		return (openat(G_p->g_dirfd,
7184*7c478bd9Sstevel@tonic-gate 		    get_component(G_p->g_nam_p), omode));
7185*7c478bd9Sstevel@tonic-gate 	}
7186*7c478bd9Sstevel@tonic-gate }
7187*7c478bd9Sstevel@tonic-gate #else
7188*7c478bd9Sstevel@tonic-gate static int
7189*7c478bd9Sstevel@tonic-gate openfile(int omode)
7190*7c478bd9Sstevel@tonic-gate {
7191*7c478bd9Sstevel@tonic-gate 	return (openat(G_p->g_dirfd, get_component(G_p->g_nam_p), omode));
7192*7c478bd9Sstevel@tonic-gate }
7193*7c478bd9Sstevel@tonic-gate #endif
7194*7c478bd9Sstevel@tonic-gate 
7195*7c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
7196*7c478bd9Sstevel@tonic-gate static int
7197*7c478bd9Sstevel@tonic-gate read_xattr_hdr()
7198*7c478bd9Sstevel@tonic-gate {
7199*7c478bd9Sstevel@tonic-gate 	off_t		bytes;
7200*7c478bd9Sstevel@tonic-gate 	int		comp_len, link_len;
7201*7c478bd9Sstevel@tonic-gate 	int		namelen;
7202*7c478bd9Sstevel@tonic-gate 	int		cnt;
7203*7c478bd9Sstevel@tonic-gate 	char		*tp;
7204*7c478bd9Sstevel@tonic-gate 	int		pad;
7205*7c478bd9Sstevel@tonic-gate 
7206*7c478bd9Sstevel@tonic-gate 	/*
7207*7c478bd9Sstevel@tonic-gate 	 * Include any padding in the read.  We need to be positioned
7208*7c478bd9Sstevel@tonic-gate 	 * at beginning of next header.
7209*7c478bd9Sstevel@tonic-gate 	 */
7210*7c478bd9Sstevel@tonic-gate 
7211*7c478bd9Sstevel@tonic-gate 	bytes = Gen.g_filesz;
7212*7c478bd9Sstevel@tonic-gate 
7213*7c478bd9Sstevel@tonic-gate 	if ((xattrhead = e_zalloc(E_NORMAL, (size_t)bytes)) == NULL) {
7214*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
7215*7c478bd9Sstevel@tonic-gate 		    "Insufficient memory for extended attribute\n"));
7216*7c478bd9Sstevel@tonic-gate 		return (1);
7217*7c478bd9Sstevel@tonic-gate 	}
7218*7c478bd9Sstevel@tonic-gate 
7219*7c478bd9Sstevel@tonic-gate 	tp = (char *)xattrhead;
7220*7c478bd9Sstevel@tonic-gate 	while (bytes > 0) {
7221*7c478bd9Sstevel@tonic-gate 		cnt = (int)(bytes > CPIOBSZ) ? CPIOBSZ : bytes;
7222*7c478bd9Sstevel@tonic-gate 		FILL(cnt);
7223*7c478bd9Sstevel@tonic-gate 		(void) memcpy(tp, Buffr.b_out_p, cnt);
7224*7c478bd9Sstevel@tonic-gate 		tp += cnt;
7225*7c478bd9Sstevel@tonic-gate 		Buffr.b_out_p += cnt;
7226*7c478bd9Sstevel@tonic-gate 		Buffr.b_cnt -= (off_t)cnt;
7227*7c478bd9Sstevel@tonic-gate 		bytes -= (off_t)cnt;
7228*7c478bd9Sstevel@tonic-gate 	}
7229*7c478bd9Sstevel@tonic-gate 
7230*7c478bd9Sstevel@tonic-gate 	pad = (Pad_val + 1 - (Gen.g_filesz & Pad_val)) &
7231*7c478bd9Sstevel@tonic-gate 	    Pad_val;
7232*7c478bd9Sstevel@tonic-gate 	if (pad != 0) {
7233*7c478bd9Sstevel@tonic-gate 		FILL(pad);
7234*7c478bd9Sstevel@tonic-gate 		Buffr.b_out_p += pad;
7235*7c478bd9Sstevel@tonic-gate 		Buffr.b_cnt -= (off_t)pad;
7236*7c478bd9Sstevel@tonic-gate 	}
7237*7c478bd9Sstevel@tonic-gate 
7238*7c478bd9Sstevel@tonic-gate 	/*
7239*7c478bd9Sstevel@tonic-gate 	 * Validate that we can handle header format
7240*7c478bd9Sstevel@tonic-gate 	 */
7241*7c478bd9Sstevel@tonic-gate 
7242*7c478bd9Sstevel@tonic-gate 	if (strcmp(xattrhead->h_version, XATTR_ARCH_VERS) != 0) {
7243*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
7244*7c478bd9Sstevel@tonic-gate 		    gettext("Unknown extended attribute format encountered\n"));
7245*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
7246*7c478bd9Sstevel@tonic-gate 		    gettext("Disabling extended attribute header parsing\n"));
7247*7c478bd9Sstevel@tonic-gate 		xattrbadhead = 1;
7248*7c478bd9Sstevel@tonic-gate 		return (1);
7249*7c478bd9Sstevel@tonic-gate 	}
7250*7c478bd9Sstevel@tonic-gate 	(void) sscanf(xattrhead->h_component_len, "%10d", &comp_len);
7251*7c478bd9Sstevel@tonic-gate 	(void) sscanf(xattrhead->h_link_component_len, "%10d", &link_len);
7252*7c478bd9Sstevel@tonic-gate 	xattrp = (struct xattr_buf *)(((char *)xattrhead) +
7253*7c478bd9Sstevel@tonic-gate 	    sizeof (struct xattr_hdr));
7254*7c478bd9Sstevel@tonic-gate 	(void) sscanf(xattrp->h_namesz, "%7d", &namelen);
7255*7c478bd9Sstevel@tonic-gate 	if (link_len > 0) {
7256*7c478bd9Sstevel@tonic-gate 		xattr_linkp = (struct xattr_buf *)((int)xattrp + (int)comp_len);
7257*7c478bd9Sstevel@tonic-gate 	} else {
7258*7c478bd9Sstevel@tonic-gate 		xattr_linkp = NULL;
7259*7c478bd9Sstevel@tonic-gate 	}
7260*7c478bd9Sstevel@tonic-gate 
7261*7c478bd9Sstevel@tonic-gate 	return (0);
7262*7c478bd9Sstevel@tonic-gate }
7263*7c478bd9Sstevel@tonic-gate #endif
7264*7c478bd9Sstevel@tonic-gate 
7265*7c478bd9Sstevel@tonic-gate static mode_t
7266*7c478bd9Sstevel@tonic-gate attrmode(char type)
7267*7c478bd9Sstevel@tonic-gate {
7268*7c478bd9Sstevel@tonic-gate 	mode_t mode;
7269*7c478bd9Sstevel@tonic-gate 
7270*7c478bd9Sstevel@tonic-gate 	switch (type) {
7271*7c478bd9Sstevel@tonic-gate 	case '\0':
7272*7c478bd9Sstevel@tonic-gate 	case REGTYPE:
7273*7c478bd9Sstevel@tonic-gate 	case LNKTYPE:
7274*7c478bd9Sstevel@tonic-gate 		mode = S_IFREG;
7275*7c478bd9Sstevel@tonic-gate 		break;
7276*7c478bd9Sstevel@tonic-gate 
7277*7c478bd9Sstevel@tonic-gate 	case SYMTYPE:
7278*7c478bd9Sstevel@tonic-gate 		mode = S_IFLNK;
7279*7c478bd9Sstevel@tonic-gate 		break;
7280*7c478bd9Sstevel@tonic-gate 
7281*7c478bd9Sstevel@tonic-gate 	case CHRTYPE:
7282*7c478bd9Sstevel@tonic-gate 		mode = S_IFCHR;
7283*7c478bd9Sstevel@tonic-gate 		break;
7284*7c478bd9Sstevel@tonic-gate 	case BLKTYPE:
7285*7c478bd9Sstevel@tonic-gate 		mode = S_IFBLK;
7286*7c478bd9Sstevel@tonic-gate 		break;
7287*7c478bd9Sstevel@tonic-gate 	case DIRTYPE:
7288*7c478bd9Sstevel@tonic-gate 		mode = S_IFDIR;
7289*7c478bd9Sstevel@tonic-gate 		break;
7290*7c478bd9Sstevel@tonic-gate 	case FIFOTYPE:
7291*7c478bd9Sstevel@tonic-gate 		mode = S_IFIFO;
7292*7c478bd9Sstevel@tonic-gate 		break;
7293*7c478bd9Sstevel@tonic-gate 	case CONTTYPE:
7294*7c478bd9Sstevel@tonic-gate 	default:
7295*7c478bd9Sstevel@tonic-gate 		mode = 0;
7296*7c478bd9Sstevel@tonic-gate 	}
7297*7c478bd9Sstevel@tonic-gate 
7298*7c478bd9Sstevel@tonic-gate 	return (mode);
7299*7c478bd9Sstevel@tonic-gate }
7300*7c478bd9Sstevel@tonic-gate 
7301*7c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
7302*7c478bd9Sstevel@tonic-gate static char *
7303*7c478bd9Sstevel@tonic-gate get_component(char *path)
7304*7c478bd9Sstevel@tonic-gate {
7305*7c478bd9Sstevel@tonic-gate 	char *ptr;
7306*7c478bd9Sstevel@tonic-gate 
7307*7c478bd9Sstevel@tonic-gate 	ptr = strrchr(path, '/');
7308*7c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
7309*7c478bd9Sstevel@tonic-gate 		return (path);
7310*7c478bd9Sstevel@tonic-gate 	} else {
7311*7c478bd9Sstevel@tonic-gate 		/*
7312*7c478bd9Sstevel@tonic-gate 		 * Handle trailing slash
7313*7c478bd9Sstevel@tonic-gate 		 */
7314*7c478bd9Sstevel@tonic-gate 		if (*(ptr + 1) == '\0')
7315*7c478bd9Sstevel@tonic-gate 			return (ptr);
7316*7c478bd9Sstevel@tonic-gate 		else
7317*7c478bd9Sstevel@tonic-gate 			return (ptr + 1);
7318*7c478bd9Sstevel@tonic-gate 	}
7319*7c478bd9Sstevel@tonic-gate }
7320*7c478bd9Sstevel@tonic-gate #else
7321*7c478bd9Sstevel@tonic-gate static char *
7322*7c478bd9Sstevel@tonic-gate get_component(char *path)
7323*7c478bd9Sstevel@tonic-gate {
7324*7c478bd9Sstevel@tonic-gate 	return (path);
7325*7c478bd9Sstevel@tonic-gate }
7326*7c478bd9Sstevel@tonic-gate #endif
7327*7c478bd9Sstevel@tonic-gate 
7328*7c478bd9Sstevel@tonic-gate static int
7329*7c478bd9Sstevel@tonic-gate open_dir(char *name)
7330*7c478bd9Sstevel@tonic-gate {
7331*7c478bd9Sstevel@tonic-gate 	int fd = -1;
7332*7c478bd9Sstevel@tonic-gate 	int cnt = 0;
7333*7c478bd9Sstevel@tonic-gate 	char *dir;
7334*7c478bd9Sstevel@tonic-gate 
7335*7c478bd9Sstevel@tonic-gate 	dir = e_zalloc(E_EXIT, strlen(name) + 1);
7336*7c478bd9Sstevel@tonic-gate 
7337*7c478bd9Sstevel@tonic-gate 	/*
7338*7c478bd9Sstevel@tonic-gate 	 * open directory; creating missing directories along the way.
7339*7c478bd9Sstevel@tonic-gate 	 */
7340*7c478bd9Sstevel@tonic-gate 	get_parent(name, dir);
7341*7c478bd9Sstevel@tonic-gate 	do {
7342*7c478bd9Sstevel@tonic-gate 		fd = open(dir, O_RDONLY);
7343*7c478bd9Sstevel@tonic-gate 		if (fd != -1) {
7344*7c478bd9Sstevel@tonic-gate 			free(dir);
7345*7c478bd9Sstevel@tonic-gate 			return (fd);
7346*7c478bd9Sstevel@tonic-gate 		}
7347*7c478bd9Sstevel@tonic-gate 		cnt++;
7348*7c478bd9Sstevel@tonic-gate 	} while (cnt <= 1 && missdir(name) == 0);
7349*7c478bd9Sstevel@tonic-gate 
7350*7c478bd9Sstevel@tonic-gate 	free(dir);
7351*7c478bd9Sstevel@tonic-gate 	return (-1);
7352*7c478bd9Sstevel@tonic-gate }
7353*7c478bd9Sstevel@tonic-gate 
7354*7c478bd9Sstevel@tonic-gate static int
7355*7c478bd9Sstevel@tonic-gate open_dirfd()
7356*7c478bd9Sstevel@tonic-gate {
7357*7c478bd9Sstevel@tonic-gate #ifdef O_XATTR
7358*7c478bd9Sstevel@tonic-gate 	if ((Args & OCt) == 0) {
7359*7c478bd9Sstevel@tonic-gate 		close_dirfd();
7360*7c478bd9Sstevel@tonic-gate 		if (G_p->g_attrnam_p != (char *)NULL) {
7361*7c478bd9Sstevel@tonic-gate 			G_p->g_dirfd = attropen(G_p->g_attrfnam_p,
7362*7c478bd9Sstevel@tonic-gate 			    ".", O_RDONLY);
7363*7c478bd9Sstevel@tonic-gate 			if (G_p->g_dirfd == -1 && (Args & (OCi | OCp))) {
7364*7c478bd9Sstevel@tonic-gate 				G_p->g_dirfd =
7365*7c478bd9Sstevel@tonic-gate 				    retry_attrdir_open(G_p->g_attrfnam_p);
7366*7c478bd9Sstevel@tonic-gate 				if (G_p->g_dirfd == -1) {
7367*7c478bd9Sstevel@tonic-gate 					msg(ERRN,
7368*7c478bd9Sstevel@tonic-gate 					    "Cannot open attribute"
7369*7c478bd9Sstevel@tonic-gate 					    " directory of file %s",
7370*7c478bd9Sstevel@tonic-gate 					    G_p->g_attrfnam_p);
7371*7c478bd9Sstevel@tonic-gate 					return (1);
7372*7c478bd9Sstevel@tonic-gate 				}
7373*7c478bd9Sstevel@tonic-gate 			}
7374*7c478bd9Sstevel@tonic-gate 		} else {
7375*7c478bd9Sstevel@tonic-gate 			G_p->g_dirfd = open_dir(G_p->g_nam_p);
7376*7c478bd9Sstevel@tonic-gate 			if (G_p->g_dirfd == -1) {
7377*7c478bd9Sstevel@tonic-gate 				msg(ERRN,
7378*7c478bd9Sstevel@tonic-gate 				    "Cannot open/create %s", G_p->g_nam_p);
7379*7c478bd9Sstevel@tonic-gate 				return (1);
7380*7c478bd9Sstevel@tonic-gate 			}
7381*7c478bd9Sstevel@tonic-gate 		}
7382*7c478bd9Sstevel@tonic-gate 	} else {
7383*7c478bd9Sstevel@tonic-gate 		G_p->g_dirfd = -1;
7384*7c478bd9Sstevel@tonic-gate 	}
7385*7c478bd9Sstevel@tonic-gate #else
7386*7c478bd9Sstevel@tonic-gate 	G_p->g_dirfd = -1;
7387*7c478bd9Sstevel@tonic-gate #endif
7388*7c478bd9Sstevel@tonic-gate 	return (0);
7389*7c478bd9Sstevel@tonic-gate }
7390*7c478bd9Sstevel@tonic-gate 
7391*7c478bd9Sstevel@tonic-gate static void
7392*7c478bd9Sstevel@tonic-gate close_dirfd()
7393*7c478bd9Sstevel@tonic-gate {
7394*7c478bd9Sstevel@tonic-gate 	if (G_p->g_dirfd != -1) {
7395*7c478bd9Sstevel@tonic-gate 		(void) close(G_p->g_dirfd);
7396*7c478bd9Sstevel@tonic-gate 		G_p->g_dirfd = -1;
7397*7c478bd9Sstevel@tonic-gate 	}
7398*7c478bd9Sstevel@tonic-gate }
7399*7c478bd9Sstevel@tonic-gate 
7400*7c478bd9Sstevel@tonic-gate static void
7401*7c478bd9Sstevel@tonic-gate write_xattr_hdr()
7402*7c478bd9Sstevel@tonic-gate {
7403*7c478bd9Sstevel@tonic-gate 	char *attrbuf = NULL;
7404*7c478bd9Sstevel@tonic-gate 	int  attrlen = 0;
7405*7c478bd9Sstevel@tonic-gate 	char *namep;
7406*7c478bd9Sstevel@tonic-gate 	struct Lnk *tl_p, *linkinfo;
7407*7c478bd9Sstevel@tonic-gate 
7408*7c478bd9Sstevel@tonic-gate 
7409*7c478bd9Sstevel@tonic-gate 	/*
7410*7c478bd9Sstevel@tonic-gate 	 * namep was allocated in xattrs_out.  It is big enough to hold
7411*7c478bd9Sstevel@tonic-gate 	 * either the name + .hdr on the end or just the attr name
7412*7c478bd9Sstevel@tonic-gate 	 */
7413*7c478bd9Sstevel@tonic-gate 
7414*7c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
7415*7c478bd9Sstevel@tonic-gate 	namep = Gen.g_nam_p;
7416*7c478bd9Sstevel@tonic-gate 	(void) creat_hdr();
7417*7c478bd9Sstevel@tonic-gate 
7418*7c478bd9Sstevel@tonic-gate 
7419*7c478bd9Sstevel@tonic-gate 	if (Args & OCo) {
7420*7c478bd9Sstevel@tonic-gate 		linkinfo = NULL;
7421*7c478bd9Sstevel@tonic-gate 		tl_p = Lnk_hd.L_nxt_p;
7422*7c478bd9Sstevel@tonic-gate 		while (tl_p != &Lnk_hd) {
7423*7c478bd9Sstevel@tonic-gate 			if (tl_p->L_gen.g_ino == G_p->g_ino &&
7424*7c478bd9Sstevel@tonic-gate 				tl_p->L_gen.g_dev == G_p->g_dev) {
7425*7c478bd9Sstevel@tonic-gate 					linkinfo = tl_p;
7426*7c478bd9Sstevel@tonic-gate 					break; /* found */
7427*7c478bd9Sstevel@tonic-gate 			}
7428*7c478bd9Sstevel@tonic-gate 			tl_p = tl_p->L_nxt_p;
7429*7c478bd9Sstevel@tonic-gate 		}
7430*7c478bd9Sstevel@tonic-gate 		prepare_xattr_hdr(&attrbuf, Gen.g_attrfnam_p,
7431*7c478bd9Sstevel@tonic-gate 		    Gen.g_attrnam_p,
7432*7c478bd9Sstevel@tonic-gate 		    (linkinfo == (struct Lnk *)NULL) ?
7433*7c478bd9Sstevel@tonic-gate 		    tartype(Gen.g_mode & Ftype) : LNKTYPE,
7434*7c478bd9Sstevel@tonic-gate 		    linkinfo, &attrlen);
7435*7c478bd9Sstevel@tonic-gate 		Gen.g_filesz = attrlen;
7436*7c478bd9Sstevel@tonic-gate 		write_hdr(ARCHIVE_XATTR, (off_t)attrlen);
7437*7c478bd9Sstevel@tonic-gate 		(void) sprintf(namep, "%s/%s", DEVNULL, Gen.g_attrnam_p);
7438*7c478bd9Sstevel@tonic-gate 		(void) write_ancillary(attrbuf, attrlen);
7439*7c478bd9Sstevel@tonic-gate 	}
7440*7c478bd9Sstevel@tonic-gate 
7441*7c478bd9Sstevel@tonic-gate 	(void) creat_hdr();
7442*7c478bd9Sstevel@tonic-gate #endif
7443*7c478bd9Sstevel@tonic-gate }
7444*7c478bd9Sstevel@tonic-gate 
7445*7c478bd9Sstevel@tonic-gate static int
7446*7c478bd9Sstevel@tonic-gate retry_attrdir_open(char *name)
7447*7c478bd9Sstevel@tonic-gate {
7448*7c478bd9Sstevel@tonic-gate 	int dirfd = -1;
7449*7c478bd9Sstevel@tonic-gate 	struct timeval times[2];
7450*7c478bd9Sstevel@tonic-gate 	mode_t newmode;
7451*7c478bd9Sstevel@tonic-gate 	struct stat parentstat;
7452*7c478bd9Sstevel@tonic-gate 
7453*7c478bd9Sstevel@tonic-gate 	/*
7454*7c478bd9Sstevel@tonic-gate 	 * We couldn't get to attrdir. See if its
7455*7c478bd9Sstevel@tonic-gate 	 * just a mode problem on the parent file.
7456*7c478bd9Sstevel@tonic-gate 	 * for example: a mode such as r-xr--r--
7457*7c478bd9Sstevel@tonic-gate 	 * won't let us create an attribute dir
7458*7c478bd9Sstevel@tonic-gate 	 * if it doesn't already exist.
7459*7c478bd9Sstevel@tonic-gate 	 */
7460*7c478bd9Sstevel@tonic-gate 
7461*7c478bd9Sstevel@tonic-gate 	if (stat(name, &parentstat) == -1) {
7462*7c478bd9Sstevel@tonic-gate 		msg(ERRN, "Cannot stat file %s", name);
7463*7c478bd9Sstevel@tonic-gate 		return (-1);
7464*7c478bd9Sstevel@tonic-gate 	}
7465*7c478bd9Sstevel@tonic-gate 	newmode = S_IWUSR | parentstat.st_mode;
7466*7c478bd9Sstevel@tonic-gate 	if (chmod(name, newmode) == -1) {
7467*7c478bd9Sstevel@tonic-gate 		msg(ERRN, "Cannot change mode of file %s to %o", name, newmode);
7468*7c478bd9Sstevel@tonic-gate 		return (-1);
7469*7c478bd9Sstevel@tonic-gate 	}
7470*7c478bd9Sstevel@tonic-gate 
7471*7c478bd9Sstevel@tonic-gate 	dirfd = attropen(name, ".", O_RDONLY);
7472*7c478bd9Sstevel@tonic-gate 	if (dirfd == -1) {
7473*7c478bd9Sstevel@tonic-gate 		msg(ERRN, "Cannot open attribute directory of file %s", name);
7474*7c478bd9Sstevel@tonic-gate 		return (-1);
7475*7c478bd9Sstevel@tonic-gate 	} else {
7476*7c478bd9Sstevel@tonic-gate 
7477*7c478bd9Sstevel@tonic-gate 		/*
7478*7c478bd9Sstevel@tonic-gate 		 * Put mode back to original
7479*7c478bd9Sstevel@tonic-gate 		 */
7480*7c478bd9Sstevel@tonic-gate 		if (chmod(name, parentstat.st_mode) != 0) {
7481*7c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot restore permissions of file %s to %o",
7482*7c478bd9Sstevel@tonic-gate 			    name, parentstat.st_mode);
7483*7c478bd9Sstevel@tonic-gate 		}
7484*7c478bd9Sstevel@tonic-gate 
7485*7c478bd9Sstevel@tonic-gate 		/*
7486*7c478bd9Sstevel@tonic-gate 		 * Put back time stamps
7487*7c478bd9Sstevel@tonic-gate 		 */
7488*7c478bd9Sstevel@tonic-gate 
7489*7c478bd9Sstevel@tonic-gate 		times[0].tv_sec = parentstat.st_atime;
7490*7c478bd9Sstevel@tonic-gate 		times[0].tv_usec = 0;
7491*7c478bd9Sstevel@tonic-gate 		times[1].tv_sec = parentstat.st_mtime;
7492*7c478bd9Sstevel@tonic-gate 		times[1].tv_usec = 0;
7493*7c478bd9Sstevel@tonic-gate 		if (utimes(name, times) != 0) {
7494*7c478bd9Sstevel@tonic-gate 			msg(ERRN, "Cannot reset timestamps on file %s");
7495*7c478bd9Sstevel@tonic-gate 		}
7496*7c478bd9Sstevel@tonic-gate 	}
7497*7c478bd9Sstevel@tonic-gate 
7498*7c478bd9Sstevel@tonic-gate 	return (dirfd);
7499*7c478bd9Sstevel@tonic-gate }
7500*7c478bd9Sstevel@tonic-gate 
7501*7c478bd9Sstevel@tonic-gate /*
7502*7c478bd9Sstevel@tonic-gate  * skip over extra slashes in string.
7503*7c478bd9Sstevel@tonic-gate  *
7504*7c478bd9Sstevel@tonic-gate  * For example:
7505*7c478bd9Sstevel@tonic-gate  * /usr/tmp/////
7506*7c478bd9Sstevel@tonic-gate  *
7507*7c478bd9Sstevel@tonic-gate  * would return pointer at
7508*7c478bd9Sstevel@tonic-gate  * /usr/tmp/////
7509*7c478bd9Sstevel@tonic-gate  *         ^
7510*7c478bd9Sstevel@tonic-gate  */
7511*7c478bd9Sstevel@tonic-gate static char *
7512*7c478bd9Sstevel@tonic-gate skipslashes(char *string, char *start)
7513*7c478bd9Sstevel@tonic-gate {
7514*7c478bd9Sstevel@tonic-gate 	while ((string > start) && *(string - 1) == '/') {
7515*7c478bd9Sstevel@tonic-gate 		string--;
7516*7c478bd9Sstevel@tonic-gate 	}
7517*7c478bd9Sstevel@tonic-gate 
7518*7c478bd9Sstevel@tonic-gate 	return (string);
7519*7c478bd9Sstevel@tonic-gate }
7520*7c478bd9Sstevel@tonic-gate 
7521*7c478bd9Sstevel@tonic-gate static sl_info_t *
7522*7c478bd9Sstevel@tonic-gate sl_info_alloc(void)
7523*7c478bd9Sstevel@tonic-gate {
7524*7c478bd9Sstevel@tonic-gate 	static int num_left;
7525*7c478bd9Sstevel@tonic-gate 	static sl_info_t *slipool;
7526*7c478bd9Sstevel@tonic-gate 
7527*7c478bd9Sstevel@tonic-gate 	if (num_left > 0) {
7528*7c478bd9Sstevel@tonic-gate 		return (&slipool[--num_left]);
7529*7c478bd9Sstevel@tonic-gate 	}
7530*7c478bd9Sstevel@tonic-gate 	num_left = SL_INFO_ALLOC_CHUNK;
7531*7c478bd9Sstevel@tonic-gate 	slipool = e_zalloc(E_EXIT, sizeof (sl_info_t) * num_left);
7532*7c478bd9Sstevel@tonic-gate 	return (&slipool[--num_left]);
7533*7c478bd9Sstevel@tonic-gate }
7534*7c478bd9Sstevel@tonic-gate 
7535*7c478bd9Sstevel@tonic-gate /*
7536*7c478bd9Sstevel@tonic-gate  * If a match for the key values was found in the tree, return a pointer to it.
7537*7c478bd9Sstevel@tonic-gate  * If a match was not found, insert it and return a pointer to it.  This is
7538*7c478bd9Sstevel@tonic-gate  * based on Knuth's Algorithm A in Vol 3, section 6.2.3.
7539*7c478bd9Sstevel@tonic-gate  */
7540*7c478bd9Sstevel@tonic-gate 
7541*7c478bd9Sstevel@tonic-gate sl_info_t *
7542*7c478bd9Sstevel@tonic-gate sl_insert(dev_t device, ino_t inode)
7543*7c478bd9Sstevel@tonic-gate {
7544*7c478bd9Sstevel@tonic-gate 	sl_info_t *p;		/* moves down the tree */
7545*7c478bd9Sstevel@tonic-gate 	sl_info_t *q;		/* scratch */
7546*7c478bd9Sstevel@tonic-gate 	sl_info_t *r;		/* scratch */
7547*7c478bd9Sstevel@tonic-gate 	sl_info_t *s;		/* pt where rebalancing may be needed */
7548*7c478bd9Sstevel@tonic-gate 	sl_info_t *t;		/* father of s */
7549*7c478bd9Sstevel@tonic-gate 	sl_info_t *head;
7550*7c478bd9Sstevel@tonic-gate 
7551*7c478bd9Sstevel@tonic-gate 	int a;			/* used to hold balance factors */
7552*7c478bd9Sstevel@tonic-gate 	int done;		/* loop control */
7553*7c478bd9Sstevel@tonic-gate 	int cmpflg;		/* used to hold the result of a comparison */
7554*7c478bd9Sstevel@tonic-gate 
7555*7c478bd9Sstevel@tonic-gate 	/* initialize */
7556*7c478bd9Sstevel@tonic-gate 
7557*7c478bd9Sstevel@tonic-gate 	head = sl_devhash_lookup(device);
7558*7c478bd9Sstevel@tonic-gate 
7559*7c478bd9Sstevel@tonic-gate 	if (head == NULL) {
7560*7c478bd9Sstevel@tonic-gate 		head = sl_info_alloc();
7561*7c478bd9Sstevel@tonic-gate 		head->llink = NULL;
7562*7c478bd9Sstevel@tonic-gate 		head->bal = 0;
7563*7c478bd9Sstevel@tonic-gate 
7564*7c478bd9Sstevel@tonic-gate 		p = head->rlink = sl_info_alloc();
7565*7c478bd9Sstevel@tonic-gate 		p->sl_ino = inode;
7566*7c478bd9Sstevel@tonic-gate 		p->sl_count = 0;
7567*7c478bd9Sstevel@tonic-gate 		p->bal = 0;
7568*7c478bd9Sstevel@tonic-gate 		p->llink = NULL;
7569*7c478bd9Sstevel@tonic-gate 		p->rlink = NULL;
7570*7c478bd9Sstevel@tonic-gate 		sl_devhash_insert(device, head);
7571*7c478bd9Sstevel@tonic-gate 		return (p);
7572*7c478bd9Sstevel@tonic-gate 	}
7573*7c478bd9Sstevel@tonic-gate 
7574*7c478bd9Sstevel@tonic-gate 	t = head;
7575*7c478bd9Sstevel@tonic-gate 	s = p = head->rlink;
7576*7c478bd9Sstevel@tonic-gate 
7577*7c478bd9Sstevel@tonic-gate 	/* compare */
7578*7c478bd9Sstevel@tonic-gate 
7579*7c478bd9Sstevel@tonic-gate 	for (done = 0; ! done; ) {
7580*7c478bd9Sstevel@tonic-gate 		switch (sl_compare(inode, p->sl_ino)) {
7581*7c478bd9Sstevel@tonic-gate 			case -1:
7582*7c478bd9Sstevel@tonic-gate 				/* move left */
7583*7c478bd9Sstevel@tonic-gate 
7584*7c478bd9Sstevel@tonic-gate 				q = p->llink;
7585*7c478bd9Sstevel@tonic-gate 
7586*7c478bd9Sstevel@tonic-gate 				if (q == NULL) {
7587*7c478bd9Sstevel@tonic-gate 					q = sl_info_alloc();
7588*7c478bd9Sstevel@tonic-gate 					p->llink = q;
7589*7c478bd9Sstevel@tonic-gate 					done = 1;
7590*7c478bd9Sstevel@tonic-gate 					continue;
7591*7c478bd9Sstevel@tonic-gate 				}
7592*7c478bd9Sstevel@tonic-gate 
7593*7c478bd9Sstevel@tonic-gate 				break;
7594*7c478bd9Sstevel@tonic-gate 
7595*7c478bd9Sstevel@tonic-gate 			case 0:
7596*7c478bd9Sstevel@tonic-gate 				/* found it */
7597*7c478bd9Sstevel@tonic-gate 				return (p);
7598*7c478bd9Sstevel@tonic-gate 				break;
7599*7c478bd9Sstevel@tonic-gate 
7600*7c478bd9Sstevel@tonic-gate 			case 1:
7601*7c478bd9Sstevel@tonic-gate 				/* move right */
7602*7c478bd9Sstevel@tonic-gate 
7603*7c478bd9Sstevel@tonic-gate 				q = p->rlink;
7604*7c478bd9Sstevel@tonic-gate 
7605*7c478bd9Sstevel@tonic-gate 				if (q == NULL) {
7606*7c478bd9Sstevel@tonic-gate 					q = sl_info_alloc();
7607*7c478bd9Sstevel@tonic-gate 					p->rlink = q;
7608*7c478bd9Sstevel@tonic-gate 					done = 1;
7609*7c478bd9Sstevel@tonic-gate 					continue;
7610*7c478bd9Sstevel@tonic-gate 				}
7611*7c478bd9Sstevel@tonic-gate 
7612*7c478bd9Sstevel@tonic-gate 				break;
7613*7c478bd9Sstevel@tonic-gate 		}
7614*7c478bd9Sstevel@tonic-gate 
7615*7c478bd9Sstevel@tonic-gate 		if (q->bal != 0) {
7616*7c478bd9Sstevel@tonic-gate 			t = p;
7617*7c478bd9Sstevel@tonic-gate 			s = q;
7618*7c478bd9Sstevel@tonic-gate 		}
7619*7c478bd9Sstevel@tonic-gate 
7620*7c478bd9Sstevel@tonic-gate 		p = q;
7621*7c478bd9Sstevel@tonic-gate 	}
7622*7c478bd9Sstevel@tonic-gate 
7623*7c478bd9Sstevel@tonic-gate 	/* insert */
7624*7c478bd9Sstevel@tonic-gate 
7625*7c478bd9Sstevel@tonic-gate 	q->sl_ino = inode;
7626*7c478bd9Sstevel@tonic-gate 	q->sl_count = 0;
7627*7c478bd9Sstevel@tonic-gate 	q->llink = q->rlink = NULL;
7628*7c478bd9Sstevel@tonic-gate 	q->bal = 0;
7629*7c478bd9Sstevel@tonic-gate 
7630*7c478bd9Sstevel@tonic-gate 	/* adjust balance factors */
7631*7c478bd9Sstevel@tonic-gate 
7632*7c478bd9Sstevel@tonic-gate 	if ((cmpflg = sl_compare(inode, s->sl_ino)) < 0) {
7633*7c478bd9Sstevel@tonic-gate 		r = p = s->llink;
7634*7c478bd9Sstevel@tonic-gate 	} else {
7635*7c478bd9Sstevel@tonic-gate 		r = p = s->rlink;
7636*7c478bd9Sstevel@tonic-gate 	}
7637*7c478bd9Sstevel@tonic-gate 
7638*7c478bd9Sstevel@tonic-gate 	while (p != q) {
7639*7c478bd9Sstevel@tonic-gate 		switch (sl_compare(inode, p->sl_ino)) {
7640*7c478bd9Sstevel@tonic-gate 			case -1:
7641*7c478bd9Sstevel@tonic-gate 				p->bal = -1;
7642*7c478bd9Sstevel@tonic-gate 				p = p->llink;
7643*7c478bd9Sstevel@tonic-gate 				break;
7644*7c478bd9Sstevel@tonic-gate 
7645*7c478bd9Sstevel@tonic-gate 			case 0:
7646*7c478bd9Sstevel@tonic-gate 				break;
7647*7c478bd9Sstevel@tonic-gate 
7648*7c478bd9Sstevel@tonic-gate 			case 1:
7649*7c478bd9Sstevel@tonic-gate 				p->bal = 1;
7650*7c478bd9Sstevel@tonic-gate 				p = p->rlink;
7651*7c478bd9Sstevel@tonic-gate 				break;
7652*7c478bd9Sstevel@tonic-gate 		}
7653*7c478bd9Sstevel@tonic-gate 	}
7654*7c478bd9Sstevel@tonic-gate 
7655*7c478bd9Sstevel@tonic-gate 	/* balancing act */
7656*7c478bd9Sstevel@tonic-gate 
7657*7c478bd9Sstevel@tonic-gate 	if (cmpflg < 0) {
7658*7c478bd9Sstevel@tonic-gate 		a = -1;
7659*7c478bd9Sstevel@tonic-gate 	} else {
7660*7c478bd9Sstevel@tonic-gate 		a = 1;
7661*7c478bd9Sstevel@tonic-gate 	}
7662*7c478bd9Sstevel@tonic-gate 
7663*7c478bd9Sstevel@tonic-gate 	if (s->bal == 0) {
7664*7c478bd9Sstevel@tonic-gate 		s->bal = a;
7665*7c478bd9Sstevel@tonic-gate 		head->llink = (sl_info_t *)((int)head->llink + 1);
7666*7c478bd9Sstevel@tonic-gate 		return (q);
7667*7c478bd9Sstevel@tonic-gate 	} else if (s->bal == -a) {
7668*7c478bd9Sstevel@tonic-gate 		s->bal = 0;
7669*7c478bd9Sstevel@tonic-gate 		return (q);
7670*7c478bd9Sstevel@tonic-gate 	}
7671*7c478bd9Sstevel@tonic-gate 
7672*7c478bd9Sstevel@tonic-gate 	/*
7673*7c478bd9Sstevel@tonic-gate 	 * (s->bal == a)
7674*7c478bd9Sstevel@tonic-gate 	 */
7675*7c478bd9Sstevel@tonic-gate 
7676*7c478bd9Sstevel@tonic-gate 	if (r->bal == a) {
7677*7c478bd9Sstevel@tonic-gate 		/* single rotation */
7678*7c478bd9Sstevel@tonic-gate 
7679*7c478bd9Sstevel@tonic-gate 		p = r;
7680*7c478bd9Sstevel@tonic-gate 
7681*7c478bd9Sstevel@tonic-gate 		if (a == -1) {
7682*7c478bd9Sstevel@tonic-gate 			s->llink = r->rlink;
7683*7c478bd9Sstevel@tonic-gate 			r->rlink = s;
7684*7c478bd9Sstevel@tonic-gate 		} else if (a == 1) {
7685*7c478bd9Sstevel@tonic-gate 			s->rlink = r->llink;
7686*7c478bd9Sstevel@tonic-gate 			r->llink = s;
7687*7c478bd9Sstevel@tonic-gate 		}
7688*7c478bd9Sstevel@tonic-gate 
7689*7c478bd9Sstevel@tonic-gate 		s->bal = r->bal = 0;
7690*7c478bd9Sstevel@tonic-gate 
7691*7c478bd9Sstevel@tonic-gate 	} else if (r->bal == -a) {
7692*7c478bd9Sstevel@tonic-gate 		/* double rotation */
7693*7c478bd9Sstevel@tonic-gate 
7694*7c478bd9Sstevel@tonic-gate 		if (a == -1) {
7695*7c478bd9Sstevel@tonic-gate 			p = r->rlink;
7696*7c478bd9Sstevel@tonic-gate 			r->rlink = p->llink;
7697*7c478bd9Sstevel@tonic-gate 			p->llink = r;
7698*7c478bd9Sstevel@tonic-gate 			s->llink = p->rlink;
7699*7c478bd9Sstevel@tonic-gate 			p->rlink = s;
7700*7c478bd9Sstevel@tonic-gate 		} else if (a == 1) {
7701*7c478bd9Sstevel@tonic-gate 			p = r->llink;
7702*7c478bd9Sstevel@tonic-gate 			r->llink = p->rlink;
7703*7c478bd9Sstevel@tonic-gate 			p->rlink = r;
7704*7c478bd9Sstevel@tonic-gate 			s->rlink = p->llink;
7705*7c478bd9Sstevel@tonic-gate 			p->llink = s;
7706*7c478bd9Sstevel@tonic-gate 		}
7707*7c478bd9Sstevel@tonic-gate 
7708*7c478bd9Sstevel@tonic-gate 		if (p->bal == 0) {
7709*7c478bd9Sstevel@tonic-gate 			s->bal = 0;
7710*7c478bd9Sstevel@tonic-gate 			r->bal = 0;
7711*7c478bd9Sstevel@tonic-gate 		} else if (p->bal == -a) {
7712*7c478bd9Sstevel@tonic-gate 			s->bal = 0;
7713*7c478bd9Sstevel@tonic-gate 			r->bal = a;
7714*7c478bd9Sstevel@tonic-gate 		} else if (p->bal == a) {
7715*7c478bd9Sstevel@tonic-gate 			s->bal = -a;
7716*7c478bd9Sstevel@tonic-gate 			r->bal = 0;
7717*7c478bd9Sstevel@tonic-gate 		}
7718*7c478bd9Sstevel@tonic-gate 
7719*7c478bd9Sstevel@tonic-gate 		p->bal = 0;
7720*7c478bd9Sstevel@tonic-gate 	}
7721*7c478bd9Sstevel@tonic-gate 
7722*7c478bd9Sstevel@tonic-gate 	/* finishing touch */
7723*7c478bd9Sstevel@tonic-gate 
7724*7c478bd9Sstevel@tonic-gate 	if (s == t->rlink) {
7725*7c478bd9Sstevel@tonic-gate 		t->rlink = p;
7726*7c478bd9Sstevel@tonic-gate 	} else {
7727*7c478bd9Sstevel@tonic-gate 		t->llink = p;
7728*7c478bd9Sstevel@tonic-gate 	}
7729*7c478bd9Sstevel@tonic-gate 
7730*7c478bd9Sstevel@tonic-gate 	return (q);
7731*7c478bd9Sstevel@tonic-gate }
7732*7c478bd9Sstevel@tonic-gate 
7733*7c478bd9Sstevel@tonic-gate /*
7734*7c478bd9Sstevel@tonic-gate  * sl_numlinks: return the number of links that we saw during our preview.
7735*7c478bd9Sstevel@tonic-gate  */
7736*7c478bd9Sstevel@tonic-gate 
7737*7c478bd9Sstevel@tonic-gate static ulong_t
7738*7c478bd9Sstevel@tonic-gate sl_numlinks(dev_t device, ino_t inode)
7739*7c478bd9Sstevel@tonic-gate {
7740*7c478bd9Sstevel@tonic-gate 	sl_info_t *p = sl_search(device, inode);
7741*7c478bd9Sstevel@tonic-gate 
7742*7c478bd9Sstevel@tonic-gate 	if (p) {
7743*7c478bd9Sstevel@tonic-gate 	    return (p->sl_count);
7744*7c478bd9Sstevel@tonic-gate 	} else {
7745*7c478bd9Sstevel@tonic-gate 	    return (1);
7746*7c478bd9Sstevel@tonic-gate 	}
7747*7c478bd9Sstevel@tonic-gate }
7748*7c478bd9Sstevel@tonic-gate 
7749*7c478bd9Sstevel@tonic-gate /*
7750*7c478bd9Sstevel@tonic-gate  * sl_preview_synonyms:  Read the file list from the input stream, remembering
7751*7c478bd9Sstevel@tonic-gate  * each reference to each file.
7752*7c478bd9Sstevel@tonic-gate  */
7753*7c478bd9Sstevel@tonic-gate 
7754*7c478bd9Sstevel@tonic-gate static void
7755*7c478bd9Sstevel@tonic-gate sl_preview_synonyms(void)
7756*7c478bd9Sstevel@tonic-gate {
7757*7c478bd9Sstevel@tonic-gate 	char buf [APATH+1];
7758*7c478bd9Sstevel@tonic-gate 	char *s;
7759*7c478bd9Sstevel@tonic-gate 
7760*7c478bd9Sstevel@tonic-gate 	char *suffix = "/cpioXXXXXX";
7761*7c478bd9Sstevel@tonic-gate 	char *tmpdir = getenv("TMPDIR");
7762*7c478bd9Sstevel@tonic-gate 	int    tmpfd, islnk;
7763*7c478bd9Sstevel@tonic-gate 	FILE *tmpfile;
7764*7c478bd9Sstevel@tonic-gate 	char *tmpfname;
7765*7c478bd9Sstevel@tonic-gate 
7766*7c478bd9Sstevel@tonic-gate 	if (tmpdir == NULL || *tmpdir == '\0' ||
7767*7c478bd9Sstevel@tonic-gate 	    (strlen(tmpdir) + strlen(suffix)) > APATH) {
7768*7c478bd9Sstevel@tonic-gate 		struct statvfs tdsb;
7769*7c478bd9Sstevel@tonic-gate 
7770*7c478bd9Sstevel@tonic-gate 		tmpdir = "/var/tmp";
7771*7c478bd9Sstevel@tonic-gate 
7772*7c478bd9Sstevel@tonic-gate 		/* /var/tmp is read-only in the mini-root environment */
7773*7c478bd9Sstevel@tonic-gate 
7774*7c478bd9Sstevel@tonic-gate 		if (statvfs(tmpdir, &tdsb) == -1 || tdsb.f_flag & ST_RDONLY) {
7775*7c478bd9Sstevel@tonic-gate 			tmpdir = "/tmp";
7776*7c478bd9Sstevel@tonic-gate 		}
7777*7c478bd9Sstevel@tonic-gate 	}
7778*7c478bd9Sstevel@tonic-gate 
7779*7c478bd9Sstevel@tonic-gate 	tmpfname = e_zalloc(E_EXIT, strlen(tmpdir) + strlen(suffix) + 1);
7780*7c478bd9Sstevel@tonic-gate 
7781*7c478bd9Sstevel@tonic-gate 	(void) strcpy(tmpfname, tmpdir);
7782*7c478bd9Sstevel@tonic-gate 	(void) strcat(tmpfname, suffix);
7783*7c478bd9Sstevel@tonic-gate 
7784*7c478bd9Sstevel@tonic-gate 	if ((tmpfd = mkstemp(tmpfname)) == -1) {
7785*7c478bd9Sstevel@tonic-gate 		msg(EXTN, "cannot open tmpfile %s", tmpfname);
7786*7c478bd9Sstevel@tonic-gate 	}
7787*7c478bd9Sstevel@tonic-gate 
7788*7c478bd9Sstevel@tonic-gate 	if (unlink(tmpfname) == -1) {
7789*7c478bd9Sstevel@tonic-gate 		msg(EXTN, "cannot unlink tmpfile %s", tmpfname);
7790*7c478bd9Sstevel@tonic-gate 	}
7791*7c478bd9Sstevel@tonic-gate 
7792*7c478bd9Sstevel@tonic-gate 	if ((tmpfile = fdopen(tmpfd, "w+")) == NULL) {
7793*7c478bd9Sstevel@tonic-gate 		msg(EXTN, "cannot fdopen tmpfile %s", tmpfname);
7794*7c478bd9Sstevel@tonic-gate 	}
7795*7c478bd9Sstevel@tonic-gate 
7796*7c478bd9Sstevel@tonic-gate 	while ((s = fgets(buf, APATH+1, In_p)) != NULL) {
7797*7c478bd9Sstevel@tonic-gate 		size_t lastchar;
7798*7c478bd9Sstevel@tonic-gate 		struct stat sb;
7799*7c478bd9Sstevel@tonic-gate 
7800*7c478bd9Sstevel@tonic-gate 		if (fputs(buf, tmpfile) == EOF) {
7801*7c478bd9Sstevel@tonic-gate 			msg(EXTN, "problem writing to tmpfile %s", tmpfname);
7802*7c478bd9Sstevel@tonic-gate 		}
7803*7c478bd9Sstevel@tonic-gate 
7804*7c478bd9Sstevel@tonic-gate 		/* pre-process the name */
7805*7c478bd9Sstevel@tonic-gate 
7806*7c478bd9Sstevel@tonic-gate 		lastchar = strlen(s) - 1;
7807*7c478bd9Sstevel@tonic-gate 
7808*7c478bd9Sstevel@tonic-gate 		if (s[lastchar] != '\n' && lastchar == APATH - 1) {
7809*7c478bd9Sstevel@tonic-gate 			continue;
7810*7c478bd9Sstevel@tonic-gate 		} else {
7811*7c478bd9Sstevel@tonic-gate 			s[lastchar] = '\0';
7812*7c478bd9Sstevel@tonic-gate 		}
7813*7c478bd9Sstevel@tonic-gate 
7814*7c478bd9Sstevel@tonic-gate 		while (s[0] == '.' && s[1] == '/') {
7815*7c478bd9Sstevel@tonic-gate 			s += 2;
7816*7c478bd9Sstevel@tonic-gate 			while (s[0] == '/') {
7817*7c478bd9Sstevel@tonic-gate 				s++;
7818*7c478bd9Sstevel@tonic-gate 			}
7819*7c478bd9Sstevel@tonic-gate 		}
7820*7c478bd9Sstevel@tonic-gate 
7821*7c478bd9Sstevel@tonic-gate 		if (lstat(s, &sb) < 0) {
7822*7c478bd9Sstevel@tonic-gate 			continue;
7823*7c478bd9Sstevel@tonic-gate 		}
7824*7c478bd9Sstevel@tonic-gate 		islnk = 0;
7825*7c478bd9Sstevel@tonic-gate 		if (S_ISLNK(sb.st_mode)) {
7826*7c478bd9Sstevel@tonic-gate 			islnk = 1;
7827*7c478bd9Sstevel@tonic-gate 			if (Args & OCL) {
7828*7c478bd9Sstevel@tonic-gate 				if (stat(s, &sb) < 0) {
7829*7c478bd9Sstevel@tonic-gate 					continue;
7830*7c478bd9Sstevel@tonic-gate 				}
7831*7c478bd9Sstevel@tonic-gate 			}
7832*7c478bd9Sstevel@tonic-gate 		}
7833*7c478bd9Sstevel@tonic-gate 		sl_remember_tgt(&sb, islnk);
7834*7c478bd9Sstevel@tonic-gate 
7835*7c478bd9Sstevel@tonic-gate #if defined(O_XATTR)
7836*7c478bd9Sstevel@tonic-gate 		if (Atflag) {
7837*7c478bd9Sstevel@tonic-gate 			int  dirfd;
7838*7c478bd9Sstevel@tonic-gate 			DIR  *dirp;
7839*7c478bd9Sstevel@tonic-gate 			struct dirent *dp;
7840*7c478bd9Sstevel@tonic-gate 
7841*7c478bd9Sstevel@tonic-gate 			if (pathconf(s, _PC_XATTR_EXISTS) != 1)
7842*7c478bd9Sstevel@tonic-gate 				continue;
7843*7c478bd9Sstevel@tonic-gate 
7844*7c478bd9Sstevel@tonic-gate 			dirfd = attropen(s, ".", O_RDONLY);
7845*7c478bd9Sstevel@tonic-gate 			if (dirfd == -1)
7846*7c478bd9Sstevel@tonic-gate 				continue;
7847*7c478bd9Sstevel@tonic-gate 
7848*7c478bd9Sstevel@tonic-gate 			tmpfd = dup(dirfd);
7849*7c478bd9Sstevel@tonic-gate 			if (tmpfd == -1) {
7850*7c478bd9Sstevel@tonic-gate 				(void) close(dirfd);
7851*7c478bd9Sstevel@tonic-gate 				continue;
7852*7c478bd9Sstevel@tonic-gate 			}
7853*7c478bd9Sstevel@tonic-gate 			dirp = fdopendir(tmpfd);
7854*7c478bd9Sstevel@tonic-gate 			if (dirp == NULL) {
7855*7c478bd9Sstevel@tonic-gate 				(void) close(dirfd);
7856*7c478bd9Sstevel@tonic-gate 				(void) close(tmpfd);
7857*7c478bd9Sstevel@tonic-gate 				continue;
7858*7c478bd9Sstevel@tonic-gate 			}
7859*7c478bd9Sstevel@tonic-gate 
7860*7c478bd9Sstevel@tonic-gate 			while (dp = readdir(dirp)) {
7861*7c478bd9Sstevel@tonic-gate 				if ((dp->d_name[0] == '.' &&
7862*7c478bd9Sstevel@tonic-gate 				    dp->d_name[1] == '\0') ||
7863*7c478bd9Sstevel@tonic-gate 				    (dp->d_name[0] == '.' &&
7864*7c478bd9Sstevel@tonic-gate 				    dp->d_name[1] == '.' &&
7865*7c478bd9Sstevel@tonic-gate 				    dp->d_name[2] == '\0'))
7866*7c478bd9Sstevel@tonic-gate 					continue;
7867*7c478bd9Sstevel@tonic-gate 
7868*7c478bd9Sstevel@tonic-gate 				if (fstatat(dirfd, dp->d_name, &sb,
7869*7c478bd9Sstevel@tonic-gate 				    AT_SYMLINK_NOFOLLOW) < 0) {
7870*7c478bd9Sstevel@tonic-gate 					continue;
7871*7c478bd9Sstevel@tonic-gate 				}
7872*7c478bd9Sstevel@tonic-gate 				islnk = 0;
7873*7c478bd9Sstevel@tonic-gate 				if (S_ISLNK(sb.st_mode)) {
7874*7c478bd9Sstevel@tonic-gate 					islnk = 1;
7875*7c478bd9Sstevel@tonic-gate 					if (Args & OCL) {
7876*7c478bd9Sstevel@tonic-gate 						if (fstatat(dirfd, dp->d_name,
7877*7c478bd9Sstevel@tonic-gate 						    &sb, 0) < 0) {
7878*7c478bd9Sstevel@tonic-gate 							continue;
7879*7c478bd9Sstevel@tonic-gate 						}
7880*7c478bd9Sstevel@tonic-gate 					}
7881*7c478bd9Sstevel@tonic-gate 				}
7882*7c478bd9Sstevel@tonic-gate 				sl_remember_tgt(&sb, islnk);
7883*7c478bd9Sstevel@tonic-gate 			}
7884*7c478bd9Sstevel@tonic-gate 			(void) closedir(dirp);
7885*7c478bd9Sstevel@tonic-gate 			(void) close(dirfd);
7886*7c478bd9Sstevel@tonic-gate 		}
7887*7c478bd9Sstevel@tonic-gate #endif
7888*7c478bd9Sstevel@tonic-gate 	}
7889*7c478bd9Sstevel@tonic-gate 
7890*7c478bd9Sstevel@tonic-gate 	if (ferror(In_p)) {
7891*7c478bd9Sstevel@tonic-gate 		msg(EXTN, "error reading stdin");
7892*7c478bd9Sstevel@tonic-gate 	}
7893*7c478bd9Sstevel@tonic-gate 
7894*7c478bd9Sstevel@tonic-gate 	if (fseek(tmpfile, 0L, SEEK_SET) == -1) {
7895*7c478bd9Sstevel@tonic-gate 		msg(EXTN, "cannot fseek on tmpfile %s", tmpfname);
7896*7c478bd9Sstevel@tonic-gate 	}
7897*7c478bd9Sstevel@tonic-gate 
7898*7c478bd9Sstevel@tonic-gate 	In_p = tmpfile;
7899*7c478bd9Sstevel@tonic-gate 	free(tmpfname);
7900*7c478bd9Sstevel@tonic-gate }
7901*7c478bd9Sstevel@tonic-gate 
7902*7c478bd9Sstevel@tonic-gate /*
7903*7c478bd9Sstevel@tonic-gate  * sl_remember_tgt: Add the device/inode for lstat or stat info to the list of
7904*7c478bd9Sstevel@tonic-gate  * those we've seen before.
7905*7c478bd9Sstevel@tonic-gate  *
7906*7c478bd9Sstevel@tonic-gate  * This tree (rooted under head) is keyed by the device/inode of the file
7907*7c478bd9Sstevel@tonic-gate  * being pointed to.  A count is kept of the number of references encountered
7908*7c478bd9Sstevel@tonic-gate  * so far.
7909*7c478bd9Sstevel@tonic-gate  */
7910*7c478bd9Sstevel@tonic-gate 
7911*7c478bd9Sstevel@tonic-gate static void
7912*7c478bd9Sstevel@tonic-gate sl_remember_tgt(const struct stat *sbp, int isSymlink)
7913*7c478bd9Sstevel@tonic-gate {
7914*7c478bd9Sstevel@tonic-gate 	sl_info_t *p;
7915*7c478bd9Sstevel@tonic-gate 	dev_t device;
7916*7c478bd9Sstevel@tonic-gate 	ino_t inode;
7917*7c478bd9Sstevel@tonic-gate 
7918*7c478bd9Sstevel@tonic-gate 	device = sbp->st_dev;
7919*7c478bd9Sstevel@tonic-gate 	inode  = sbp->st_ino;
7920*7c478bd9Sstevel@tonic-gate 
7921*7c478bd9Sstevel@tonic-gate 	/* Determine whether we've seen this one before */
7922*7c478bd9Sstevel@tonic-gate 
7923*7c478bd9Sstevel@tonic-gate 	p = sl_insert(device, inode);
7924*7c478bd9Sstevel@tonic-gate 
7925*7c478bd9Sstevel@tonic-gate 	if (p->sl_count > 0) {
7926*7c478bd9Sstevel@tonic-gate 		/*
7927*7c478bd9Sstevel@tonic-gate 		 * We have seen this file before.
7928*7c478bd9Sstevel@tonic-gate 		 * Note that if we are not chasing symlinks, and this one is a
7929*7c478bd9Sstevel@tonic-gate 		 * symlink, it is identically the one we saw before (you cannot
7930*7c478bd9Sstevel@tonic-gate 		 * have hard links to symlinks); in this case, we leave the
7931*7c478bd9Sstevel@tonic-gate 		 * count alone, so that we don't wind up archiving a symlink to
7932*7c478bd9Sstevel@tonic-gate 		 * itself.
7933*7c478bd9Sstevel@tonic-gate 		 */
7934*7c478bd9Sstevel@tonic-gate 
7935*7c478bd9Sstevel@tonic-gate 		if ((Args & OCL) || (! isSymlink)) {
7936*7c478bd9Sstevel@tonic-gate 			p->sl_count++;
7937*7c478bd9Sstevel@tonic-gate 		}
7938*7c478bd9Sstevel@tonic-gate 	} else {
7939*7c478bd9Sstevel@tonic-gate 		/* We have not seen this file before */
7940*7c478bd9Sstevel@tonic-gate 
7941*7c478bd9Sstevel@tonic-gate 		p->sl_count = 1;
7942*7c478bd9Sstevel@tonic-gate 
7943*7c478bd9Sstevel@tonic-gate 		if (Use_old_stat) {
7944*7c478bd9Sstevel@tonic-gate 			/* -Hodc: remap inode (-1 on overflow) */
7945*7c478bd9Sstevel@tonic-gate 
7946*7c478bd9Sstevel@tonic-gate 			sl_remap_t  *q;
7947*7c478bd9Sstevel@tonic-gate 
7948*7c478bd9Sstevel@tonic-gate 			for (q = sl_remap_head; q && (q->dev != device);
7949*7c478bd9Sstevel@tonic-gate 			    q = q->next) {
7950*7c478bd9Sstevel@tonic-gate 				/* do nothing */
7951*7c478bd9Sstevel@tonic-gate 			}
7952*7c478bd9Sstevel@tonic-gate 
7953*7c478bd9Sstevel@tonic-gate 			if (q == NULL) {
7954*7c478bd9Sstevel@tonic-gate 				q = e_zalloc(E_EXIT, sizeof (sl_remap_t));
7955*7c478bd9Sstevel@tonic-gate 				q->dev = device;
7956*7c478bd9Sstevel@tonic-gate 				p->sl_ino2 = q->inode_count = 1;
7957*7c478bd9Sstevel@tonic-gate 
7958*7c478bd9Sstevel@tonic-gate 				q->next = (sl_remap_head) ?
7959*7c478bd9Sstevel@tonic-gate 				    sl_remap_head->next : NULL;
7960*7c478bd9Sstevel@tonic-gate 				sl_remap_head = q;
7961*7c478bd9Sstevel@tonic-gate 			} else {
7962*7c478bd9Sstevel@tonic-gate 				if ((size_t)q->inode_count <=
7963*7c478bd9Sstevel@tonic-gate 				    ((1 << (sizeof (o_ino_t) * 8)) - 1)) {
7964*7c478bd9Sstevel@tonic-gate 					/* fits in o_ino_t */
7965*7c478bd9Sstevel@tonic-gate 					p->sl_ino2 = ++(q->inode_count);
7966*7c478bd9Sstevel@tonic-gate 				} else {
7967*7c478bd9Sstevel@tonic-gate 					p->sl_ino2 = (ino_t)-1;
7968*7c478bd9Sstevel@tonic-gate 				}
7969*7c478bd9Sstevel@tonic-gate 			}
7970*7c478bd9Sstevel@tonic-gate 		}
7971*7c478bd9Sstevel@tonic-gate 	}
7972*7c478bd9Sstevel@tonic-gate }
7973*7c478bd9Sstevel@tonic-gate 
7974*7c478bd9Sstevel@tonic-gate /*
7975*7c478bd9Sstevel@tonic-gate  * A faster search, which does not insert the key values into the tree.
7976*7c478bd9Sstevel@tonic-gate  * If the a match was found in the tree, return a pointer to it.  If it was not
7977*7c478bd9Sstevel@tonic-gate  * found, return NULL.
7978*7c478bd9Sstevel@tonic-gate  */
7979*7c478bd9Sstevel@tonic-gate 
7980*7c478bd9Sstevel@tonic-gate sl_info_t *
7981*7c478bd9Sstevel@tonic-gate sl_search(dev_t device, ino_t inode)
7982*7c478bd9Sstevel@tonic-gate {
7983*7c478bd9Sstevel@tonic-gate 	sl_info_t *p;		/* moves down the tree */
7984*7c478bd9Sstevel@tonic-gate 	int c;			/* comparison value */
7985*7c478bd9Sstevel@tonic-gate 	sl_info_t *retval = NULL; /* return value */
7986*7c478bd9Sstevel@tonic-gate 	sl_info_t *head;
7987*7c478bd9Sstevel@tonic-gate 
7988*7c478bd9Sstevel@tonic-gate 	head = sl_devhash_lookup(device);
7989*7c478bd9Sstevel@tonic-gate 	if (head != NULL) {
7990*7c478bd9Sstevel@tonic-gate 		for (p = head->rlink; p; ) {
7991*7c478bd9Sstevel@tonic-gate 			if ((c = sl_compare(inode, p->sl_ino)) == 0) {
7992*7c478bd9Sstevel@tonic-gate 				retval = p;
7993*7c478bd9Sstevel@tonic-gate 				break;
7994*7c478bd9Sstevel@tonic-gate 			} else if (c < 0) {
7995*7c478bd9Sstevel@tonic-gate 				p = p->llink;
7996*7c478bd9Sstevel@tonic-gate 			} else {
7997*7c478bd9Sstevel@tonic-gate 				p = p->rlink;
7998*7c478bd9Sstevel@tonic-gate 			}
7999*7c478bd9Sstevel@tonic-gate 		}
8000*7c478bd9Sstevel@tonic-gate 	}
8001*7c478bd9Sstevel@tonic-gate 
8002*7c478bd9Sstevel@tonic-gate 	return (retval);
8003*7c478bd9Sstevel@tonic-gate }
8004*7c478bd9Sstevel@tonic-gate 
8005*7c478bd9Sstevel@tonic-gate static sl_info_t *
8006*7c478bd9Sstevel@tonic-gate sl_devhash_lookup(dev_t device)
8007*7c478bd9Sstevel@tonic-gate {
8008*7c478bd9Sstevel@tonic-gate 	int key;
8009*7c478bd9Sstevel@tonic-gate 	sl_info_link_t *lp;
8010*7c478bd9Sstevel@tonic-gate 	static sl_info_link_t *devcache;
8011*7c478bd9Sstevel@tonic-gate 
8012*7c478bd9Sstevel@tonic-gate 	if (devcache != NULL && devcache->dev == device) {
8013*7c478bd9Sstevel@tonic-gate 		return (devcache->head);
8014*7c478bd9Sstevel@tonic-gate 	}
8015*7c478bd9Sstevel@tonic-gate 
8016*7c478bd9Sstevel@tonic-gate 	key = DEV_HASHKEY(device);
8017*7c478bd9Sstevel@tonic-gate 	for (lp = sl_devhash[key]; lp; lp = lp->next) {
8018*7c478bd9Sstevel@tonic-gate 		if (lp->dev == device) {
8019*7c478bd9Sstevel@tonic-gate 			devcache = lp;
8020*7c478bd9Sstevel@tonic-gate 			return (lp->head);
8021*7c478bd9Sstevel@tonic-gate 		}
8022*7c478bd9Sstevel@tonic-gate 	}
8023*7c478bd9Sstevel@tonic-gate 	return (NULL);
8024*7c478bd9Sstevel@tonic-gate }
8025*7c478bd9Sstevel@tonic-gate 
8026*7c478bd9Sstevel@tonic-gate static void
8027*7c478bd9Sstevel@tonic-gate sl_devhash_insert(dev_t device, sl_info_t *head)
8028*7c478bd9Sstevel@tonic-gate {
8029*7c478bd9Sstevel@tonic-gate 	int key = DEV_HASHKEY(device);
8030*7c478bd9Sstevel@tonic-gate 	sl_info_link_t *lp;
8031*7c478bd9Sstevel@tonic-gate 
8032*7c478bd9Sstevel@tonic-gate 	lp = e_zalloc(E_EXIT, sizeof (sl_info_link_t));
8033*7c478bd9Sstevel@tonic-gate 	lp->dev = device;
8034*7c478bd9Sstevel@tonic-gate 	lp->head = head;
8035*7c478bd9Sstevel@tonic-gate 	lp->next = sl_devhash[key];
8036*7c478bd9Sstevel@tonic-gate 	sl_devhash[key] = lp;
8037*7c478bd9Sstevel@tonic-gate }
8038*7c478bd9Sstevel@tonic-gate 
8039*7c478bd9Sstevel@tonic-gate static void
8040*7c478bd9Sstevel@tonic-gate chop_endslashes(char *path)
8041*7c478bd9Sstevel@tonic-gate {
8042*7c478bd9Sstevel@tonic-gate 	char *end, *ptr;
8043*7c478bd9Sstevel@tonic-gate 
8044*7c478bd9Sstevel@tonic-gate 	end = &path[strlen(path) -1];
8045*7c478bd9Sstevel@tonic-gate 	if (*end == '/' && end != path) {
8046*7c478bd9Sstevel@tonic-gate 		ptr = skipslashes(end, path);
8047*7c478bd9Sstevel@tonic-gate 		if (ptr != NULL && ptr != path) {
8048*7c478bd9Sstevel@tonic-gate 			*ptr = '\0';
8049*7c478bd9Sstevel@tonic-gate 		}
8050*7c478bd9Sstevel@tonic-gate 	}
8051*7c478bd9Sstevel@tonic-gate }
8052*7c478bd9Sstevel@tonic-gate 
8053*7c478bd9Sstevel@tonic-gate #if !defined(O_XATTR)
8054*7c478bd9Sstevel@tonic-gate int
8055*7c478bd9Sstevel@tonic-gate openat64(int fd, char *name, int oflag, mode_t cmode)
8056*7c478bd9Sstevel@tonic-gate {
8057*7c478bd9Sstevel@tonic-gate 	return (open64(name, oflag, cmode));
8058*7c478bd9Sstevel@tonic-gate }
8059*7c478bd9Sstevel@tonic-gate 
8060*7c478bd9Sstevel@tonic-gate int
8061*7c478bd9Sstevel@tonic-gate openat(int fd, char *name, int oflag, mode_t cmode)
8062*7c478bd9Sstevel@tonic-gate {
8063*7c478bd9Sstevel@tonic-gate 	return (open(name, oflag, cmode));
8064*7c478bd9Sstevel@tonic-gate }
8065*7c478bd9Sstevel@tonic-gate 
8066*7c478bd9Sstevel@tonic-gate int
8067*7c478bd9Sstevel@tonic-gate fchownat(int fd, char *name, uid_t owner, gid_t group, int flag)
8068*7c478bd9Sstevel@tonic-gate {
8069*7c478bd9Sstevel@tonic-gate 	if (flag == AT_SYMLINK_NOFOLLOW)
8070*7c478bd9Sstevel@tonic-gate 		return (lchown(name, owner, group));
8071*7c478bd9Sstevel@tonic-gate 	else
8072*7c478bd9Sstevel@tonic-gate 		return (chown(name, owner, group));
8073*7c478bd9Sstevel@tonic-gate }
8074*7c478bd9Sstevel@tonic-gate 
8075*7c478bd9Sstevel@tonic-gate int
8076*7c478bd9Sstevel@tonic-gate renameat(int fromfd, char *old, int tofd, char *new)
8077*7c478bd9Sstevel@tonic-gate {
8078*7c478bd9Sstevel@tonic-gate 	return (rename(old, new));
8079*7c478bd9Sstevel@tonic-gate }
8080*7c478bd9Sstevel@tonic-gate 
8081*7c478bd9Sstevel@tonic-gate int
8082*7c478bd9Sstevel@tonic-gate futimesat(int fd, char *path, struct timeval times[2])
8083*7c478bd9Sstevel@tonic-gate {
8084*7c478bd9Sstevel@tonic-gate 	return (utimes(path, times));
8085*7c478bd9Sstevel@tonic-gate }
8086*7c478bd9Sstevel@tonic-gate 
8087*7c478bd9Sstevel@tonic-gate int
8088*7c478bd9Sstevel@tonic-gate unlinkat(int dirfd, char *path, int flag)
8089*7c478bd9Sstevel@tonic-gate {
8090*7c478bd9Sstevel@tonic-gate 	if (flag == AT_REMOVEDIR) {
8091*7c478bd9Sstevel@tonic-gate 		return (rmdir(path));
8092*7c478bd9Sstevel@tonic-gate 	} else {
8093*7c478bd9Sstevel@tonic-gate 		return (unlink(path));
8094*7c478bd9Sstevel@tonic-gate 	}
8095*7c478bd9Sstevel@tonic-gate }
8096*7c478bd9Sstevel@tonic-gate 
8097*7c478bd9Sstevel@tonic-gate int
8098*7c478bd9Sstevel@tonic-gate fstatat(int fd, char *path, struct stat *buf, int flag)
8099*7c478bd9Sstevel@tonic-gate {
8100*7c478bd9Sstevel@tonic-gate 	if (flag == AT_SYMLINK_NOFOLLOW)
8101*7c478bd9Sstevel@tonic-gate 		return (lstat(path, buf));
8102*7c478bd9Sstevel@tonic-gate 	else
8103*7c478bd9Sstevel@tonic-gate 		return (stat(path, buf));
8104*7c478bd9Sstevel@tonic-gate }
8105*7c478bd9Sstevel@tonic-gate 
8106*7c478bd9Sstevel@tonic-gate int
8107*7c478bd9Sstevel@tonic-gate attropen(char *file, char *attr, int omode, mode_t cmode)
8108*7c478bd9Sstevel@tonic-gate {
8109*7c478bd9Sstevel@tonic-gate 	errno = ENOTSUP;
8110*7c478bd9Sstevel@tonic-gate 	return (-1);
8111*7c478bd9Sstevel@tonic-gate }
8112*7c478bd9Sstevel@tonic-gate #endif
8113