xref: /onnv-gate/usr/src/cmd/cpio/cpio.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	All Rights Reserved					*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
32*0Sstevel@tonic-gate  * under license from the Regents of the University of California.
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include <stdio.h>
38*0Sstevel@tonic-gate #include <sys/types.h>
39*0Sstevel@tonic-gate #include <errno.h>
40*0Sstevel@tonic-gate #include <unistd.h>
41*0Sstevel@tonic-gate #include <stdlib.h>
42*0Sstevel@tonic-gate #include <fcntl.h>
43*0Sstevel@tonic-gate #include <memory.h>
44*0Sstevel@tonic-gate #include <string.h>
45*0Sstevel@tonic-gate #include <stdarg.h>
46*0Sstevel@tonic-gate #include <sys/stat.h>
47*0Sstevel@tonic-gate #include <sys/statvfs.h>
48*0Sstevel@tonic-gate #include <sys/mkdev.h>
49*0Sstevel@tonic-gate #include <sys/param.h>
50*0Sstevel@tonic-gate #include <utime.h>
51*0Sstevel@tonic-gate #include <pwd.h>
52*0Sstevel@tonic-gate #include <grp.h>
53*0Sstevel@tonic-gate #include <signal.h>
54*0Sstevel@tonic-gate #include <ctype.h>
55*0Sstevel@tonic-gate #include <archives.h>
56*0Sstevel@tonic-gate #include <locale.h>
57*0Sstevel@tonic-gate #include <sys/ioctl.h>
58*0Sstevel@tonic-gate #include <sys/mtio.h>
59*0Sstevel@tonic-gate #include <sys/fdio.h>
60*0Sstevel@tonic-gate #include "cpio.h"
61*0Sstevel@tonic-gate #include <sys/acl.h>
62*0Sstevel@tonic-gate #include <sys/time.h>
63*0Sstevel@tonic-gate #include <sys/resource.h>
64*0Sstevel@tonic-gate #include <fnmatch.h>
65*0Sstevel@tonic-gate #include <libgen.h>
66*0Sstevel@tonic-gate #include <libintl.h>
67*0Sstevel@tonic-gate #include <dirent.h>
68*0Sstevel@tonic-gate #include <limits.h>
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate /*
71*0Sstevel@tonic-gate  * Special kludge for off_t being a signed quantity.
72*0Sstevel@tonic-gate  */
73*0Sstevel@tonic-gate #if _FILE_OFFSET_BITS == 64
74*0Sstevel@tonic-gate typedef	u_longlong_t	u_off_t;
75*0Sstevel@tonic-gate #else
76*0Sstevel@tonic-gate typedef	ulong_t		u_off_t;
77*0Sstevel@tonic-gate #endif
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate #define	SECMODE	0xe080
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate #define	DEVNULL		"/dev/null"
82*0Sstevel@tonic-gate #define	XATTRHDR	".hdr"
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate #define	NAMELEN		32
85*0Sstevel@tonic-gate #define	TYPELEN 	16
86*0Sstevel@tonic-gate #define	PERMLEN		4
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate #define	FILE_COPIED	1
89*0Sstevel@tonic-gate #define	FILE_LINKED	2
90*0Sstevel@tonic-gate #define	FILE_PASS_ERR	-1
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate #define	ARCHIVE_NORMAL	0
93*0Sstevel@tonic-gate #define	ARCHIVE_ACL	1
94*0Sstevel@tonic-gate #define	ARCHIVE_XATTR	2
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate #define	LSTAT(dir, path, statbuf) fstatat(dir, \
97*0Sstevel@tonic-gate     get_component((Gen.g_attrnam_p == (char *)NULL) ? \
98*0Sstevel@tonic-gate     path : Gen.g_attrnam_p), statbuf, AT_SYMLINK_NOFOLLOW)
99*0Sstevel@tonic-gate #define	STAT(dir, path, statbuf) fstatat(dir, \
100*0Sstevel@tonic-gate     get_component((Gen.g_attrnam_p == (char *)NULL) ? \
101*0Sstevel@tonic-gate     path : Gen.g_attrnam_p), statbuf, 0)
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate /*
104*0Sstevel@tonic-gate  *	These limits reflect the maximum size regular file that
105*0Sstevel@tonic-gate  *	can be archived, depending on the archive type. For archives
106*0Sstevel@tonic-gate  *	with character-format headers (odc, tar, ustar) we use
107*0Sstevel@tonic-gate  *	CHAR_OFFSET_MAX. Otherwise, we are limited to the size
108*0Sstevel@tonic-gate  *	that will fit in a signed long value.
109*0Sstevel@tonic-gate  */
110*0Sstevel@tonic-gate #define	CHAR_OFFSET_MAX	077777777777	/* 11 octal digits */
111*0Sstevel@tonic-gate #define	BIN_OFFSET_MAX	LONG_MAX	/* signed long max value */
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate static char	aclchar = ' ';
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate static struct Lnk *add_lnk(struct Lnk **);
116*0Sstevel@tonic-gate static int bfill(void);
117*0Sstevel@tonic-gate static void bflush(void);
118*0Sstevel@tonic-gate static int chgreel(int dir);
119*0Sstevel@tonic-gate static int ckname(int);
120*0Sstevel@tonic-gate static void ckopts(long mask);
121*0Sstevel@tonic-gate static long cksum(char hdr, int byt_cnt, int *err);
122*0Sstevel@tonic-gate static int creat_hdr(void);
123*0Sstevel@tonic-gate static int creat_lnk(int dirfd, char *name1_p, char *name2_p);
124*0Sstevel@tonic-gate static int creat_spec(int dirfd);
125*0Sstevel@tonic-gate static int creat_tmp(char *nam_p);
126*0Sstevel@tonic-gate static void data_in(int proc_mode);
127*0Sstevel@tonic-gate static void data_out(void);
128*0Sstevel@tonic-gate static void data_pass(void);
129*0Sstevel@tonic-gate static void file_in(void);
130*0Sstevel@tonic-gate static int file_out(void);
131*0Sstevel@tonic-gate static int file_pass(void);
132*0Sstevel@tonic-gate static void flush_lnks(void);
133*0Sstevel@tonic-gate static int gethdr(void);
134*0Sstevel@tonic-gate static int getname(void);
135*0Sstevel@tonic-gate static void getpats(int largc, char **largv);
136*0Sstevel@tonic-gate static void ioerror(int dir);
137*0Sstevel@tonic-gate static int matched(void);
138*0Sstevel@tonic-gate static int missdir(char *nam_p);
139*0Sstevel@tonic-gate static long mklong(short v[]);
140*0Sstevel@tonic-gate static void mkshort(short sval[], long v);
141*0Sstevel@tonic-gate static void msg(int severity, const char *fmt, ...);
142*0Sstevel@tonic-gate static int openout(int dirfd);
143*0Sstevel@tonic-gate static int read_hdr(int hdr);
144*0Sstevel@tonic-gate static void reclaim(struct Lnk *l_p);
145*0Sstevel@tonic-gate static void rstbuf(void);
146*0Sstevel@tonic-gate static void setpasswd(char *nam);
147*0Sstevel@tonic-gate static void rstfiles(int over, int dirfd);
148*0Sstevel@tonic-gate static void scan4trail(void);
149*0Sstevel@tonic-gate static void setup(int largc, char **largv);
150*0Sstevel@tonic-gate static void set_tym(int dirfd, char *nam_p, time_t atime, time_t mtime);
151*0Sstevel@tonic-gate static void sigint(int sig);
152*0Sstevel@tonic-gate static void swap(char *buf_p, int cnt);
153*0Sstevel@tonic-gate static void usage(void);
154*0Sstevel@tonic-gate static void verbose(char *nam_p);
155*0Sstevel@tonic-gate static void write_hdr(int secflag, off_t len);
156*0Sstevel@tonic-gate static void write_trail(void);
157*0Sstevel@tonic-gate static int ustar_dir(void);
158*0Sstevel@tonic-gate static int ustar_spec(void);
159*0Sstevel@tonic-gate static struct stat *convert_to_old_stat(struct stat *, char *, char *);
160*0Sstevel@tonic-gate static void read_bar_vol_hdr(void);
161*0Sstevel@tonic-gate static void read_bar_file_hdr(void);
162*0Sstevel@tonic-gate static void setup_uncompress(FILE **);
163*0Sstevel@tonic-gate static void skip_bar_volhdr(void);
164*0Sstevel@tonic-gate static void bar_file_in(void);
165*0Sstevel@tonic-gate static int g_init(int *devtype, int *fdes);
166*0Sstevel@tonic-gate static int g_read(int, int, char *, unsigned);
167*0Sstevel@tonic-gate static int g_write(int, int, char *, unsigned);
168*0Sstevel@tonic-gate static int is_floppy(int);
169*0Sstevel@tonic-gate static int is_tape(int);
170*0Sstevel@tonic-gate static int append_secattr(char **, int *, int, char *, char);
171*0Sstevel@tonic-gate static void write_ancillary(char *secinfo, int len);
172*0Sstevel@tonic-gate static int remove_dir(char *);
173*0Sstevel@tonic-gate static int save_cwd(void);
174*0Sstevel@tonic-gate static void rest_cwd(int cwd);
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate static void xattrs_out(int (*func)());
177*0Sstevel@tonic-gate static void get_parent(char *path, char *dir);
178*0Sstevel@tonic-gate static void prepare_xattr_hdr(char **attrbuf, char *filename,
179*0Sstevel@tonic-gate     char *attrname, char typeflag, struct Lnk *linkinfo, int *rlen);
180*0Sstevel@tonic-gate static char tartype(int type);
181*0Sstevel@tonic-gate static int openfile(int omode);
182*0Sstevel@tonic-gate static mode_t attrmode(char type);
183*0Sstevel@tonic-gate static char *get_component(char *path);
184*0Sstevel@tonic-gate static int open_dir(char *name);
185*0Sstevel@tonic-gate static int open_dirfd();
186*0Sstevel@tonic-gate static void close_dirfd();
187*0Sstevel@tonic-gate static void write_xattr_hdr();
188*0Sstevel@tonic-gate static int retry_attrdir_open(char *name);
189*0Sstevel@tonic-gate static char *skipslashes(char *string, char *start);
190*0Sstevel@tonic-gate static int read_xattr_hdr();
191*0Sstevel@tonic-gate static void chop_endslashes(char *path);
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate /* helpful types */
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate static
197*0Sstevel@tonic-gate struct passwd	*Curpw_p,	/* Current password entry for -t option */
198*0Sstevel@tonic-gate 		*Rpw_p,		/* Password entry for -R option */
199*0Sstevel@tonic-gate 		*dpasswd;
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate static
202*0Sstevel@tonic-gate struct group	*Curgr_p,	/* Current group entry for -t option */
203*0Sstevel@tonic-gate 		*dgroup;
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate /* Data structure for buffered I/O. */
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate static
208*0Sstevel@tonic-gate struct buf_info {
209*0Sstevel@tonic-gate 	char	*b_base_p,	/* Pointer to base of buffer */
210*0Sstevel@tonic-gate 		*b_out_p,	/* Position to take bytes from buffer at */
211*0Sstevel@tonic-gate 		*b_in_p,	/* Position to put bytes into buffer at */
212*0Sstevel@tonic-gate 		*b_end_p;	/* Pointer to end of buffer */
213*0Sstevel@tonic-gate 	long	b_cnt,		/* Count of unprocessed bytes */
214*0Sstevel@tonic-gate 		b_size;		/* Size of buffer in bytes */
215*0Sstevel@tonic-gate } Buffr;
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate /* Generic header format */
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate static
220*0Sstevel@tonic-gate struct gen_hdr {
221*0Sstevel@tonic-gate 	ulong_t	g_magic,	/* Magic number field */
222*0Sstevel@tonic-gate 		g_ino,		/* Inode number of file */
223*0Sstevel@tonic-gate 		g_mode,		/* Mode of file */
224*0Sstevel@tonic-gate 		g_uid,		/* Uid of file */
225*0Sstevel@tonic-gate 		g_gid,		/* Gid of file */
226*0Sstevel@tonic-gate 		g_nlink,	/* Number of links */
227*0Sstevel@tonic-gate 		g_mtime;	/* Modification time */
228*0Sstevel@tonic-gate 	off_t	g_filesz;	/* Length of file */
229*0Sstevel@tonic-gate 	ulong_t	g_dev,		/* File system of file */
230*0Sstevel@tonic-gate 		g_rdev,		/* Major/minor numbers of special files */
231*0Sstevel@tonic-gate 		g_namesz,	/* Length of filename */
232*0Sstevel@tonic-gate 		g_cksum;	/* Checksum of file */
233*0Sstevel@tonic-gate 	char	g_gname[32],
234*0Sstevel@tonic-gate 		g_uname[32],
235*0Sstevel@tonic-gate 		g_version[2],
236*0Sstevel@tonic-gate 		g_tmagic[6],
237*0Sstevel@tonic-gate 		g_typeflag;
238*0Sstevel@tonic-gate 	char	*g_tname,
239*0Sstevel@tonic-gate 		*g_prefix,
240*0Sstevel@tonic-gate 		*g_nam_p,	/* Filename */
241*0Sstevel@tonic-gate 		*g_attrnam_p,	/* attribute */
242*0Sstevel@tonic-gate 		*g_attrfnam_p,  /* Real file name attr belongs to */
243*0Sstevel@tonic-gate 		*g_linktoattrfnam_p, /* file linked attribute belongs to */
244*0Sstevel@tonic-gate 		*g_linktoattrnam_p,  /* attribute g_attrnam_p is linked to */
245*0Sstevel@tonic-gate 		*g_dirpath;	/* dirname currently opened */
246*0Sstevel@tonic-gate 	int	g_dirfd;	/* directory file descriptor */
247*0Sstevel@tonic-gate 	int	g_passdirfd;	/* directory fd to pass to */
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate } Gen, *G_p;
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate /* Data structure for handling multiply-linked files */
252*0Sstevel@tonic-gate static
253*0Sstevel@tonic-gate char	prebuf[PRESIZ+1],
254*0Sstevel@tonic-gate 	nambuf[NAMSIZ+1],
255*0Sstevel@tonic-gate 	fullnam[MAXNAM+1];
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate static
259*0Sstevel@tonic-gate struct Lnk {
260*0Sstevel@tonic-gate 	short	L_cnt,		/* Number of links encountered */
261*0Sstevel@tonic-gate 		L_data;		/* Data has been encountered if 1 */
262*0Sstevel@tonic-gate 	struct gen_hdr	L_gen;	/* gen_hdr information for this file */
263*0Sstevel@tonic-gate 	struct Lnk	*L_nxt_p,	/* Next file in list */
264*0Sstevel@tonic-gate 			*L_bck_p,	/* Previous file in list */
265*0Sstevel@tonic-gate 			*L_lnk_p;	/* Next link for this file */
266*0Sstevel@tonic-gate } Lnk_hd;
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate static
269*0Sstevel@tonic-gate struct hdr_cpio	Hdr;
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate /*
272*0Sstevel@tonic-gate  * -------------------------------------------------------------------------
273*0Sstevel@tonic-gate  *		   Stuff needed to pre-view the name stream
274*0Sstevel@tonic-gate  *
275*0Sstevel@tonic-gate  * issymlink is used to remember that the current file is a symlink between
276*0Sstevel@tonic-gate  * getname() and file_pass(); the former trashes this information immediately
277*0Sstevel@tonic-gate  * when -L is specified.
278*0Sstevel@tonic-gate  */
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate static
281*0Sstevel@tonic-gate int	issymlink = 0;
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate static
284*0Sstevel@tonic-gate FILE	*In_p = stdin;		/* Where the input comes from */
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate typedef struct sl_info
287*0Sstevel@tonic-gate {
288*0Sstevel@tonic-gate 	struct sl_info *llink;	/* Left subtree ptr (tree depth in *sl_head) */
289*0Sstevel@tonic-gate 	struct sl_info *rlink;	/* Right subtree ptr */
290*0Sstevel@tonic-gate 	int bal;		/* Subtree balance factor */
291*0Sstevel@tonic-gate 	ulong_t	sl_count;	/* Number of symlinks */
292*0Sstevel@tonic-gate 	ino_t	sl_ino;		/* Inode of file */
293*0Sstevel@tonic-gate 	ino_t	sl_ino2;	/* alternate inode for -Hodc */
294*0Sstevel@tonic-gate } sl_info_t;
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate /*
297*0Sstevel@tonic-gate  * The following structure maintains a hash entry for the
298*0Sstevel@tonic-gate  * balancing trees which are allocated for each device nodes.
299*0Sstevel@tonic-gate  */
300*0Sstevel@tonic-gate typedef struct sl_info_link
301*0Sstevel@tonic-gate {
302*0Sstevel@tonic-gate 	dev_t		dev;
303*0Sstevel@tonic-gate 	sl_info_t	*head;
304*0Sstevel@tonic-gate 	struct sl_info_link *next;
305*0Sstevel@tonic-gate } sl_info_link_t;
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate #define	SL_INFO_ALLOC_CHUNK	1024
308*0Sstevel@tonic-gate #define	NDEVHENTRY		0x40
309*0Sstevel@tonic-gate #define	DEV_HASHKEY(x)		((x) & (NDEVHENTRY -1))
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate /*
312*0Sstevel@tonic-gate  * For remapping dev,inode for -Hodc archives.
313*0Sstevel@tonic-gate  */
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate typedef struct sl_remap
316*0Sstevel@tonic-gate {
317*0Sstevel@tonic-gate 	dev_t			dev;		/* device */
318*0Sstevel@tonic-gate 	int			inode_count;	/* # inodes seen on dev */
319*0Sstevel@tonic-gate 	struct sl_remap 	*next;		/* next in the chain */
320*0Sstevel@tonic-gate } sl_remap_t;
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate /* forward declarations */
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate static sl_info_t 	*sl_info_alloc(void);
325*0Sstevel@tonic-gate static sl_info_t 	*sl_insert(dev_t, ino_t);
326*0Sstevel@tonic-gate static ulong_t		sl_numlinks(dev_t, ino_t);
327*0Sstevel@tonic-gate static void		sl_preview_synonyms(void);
328*0Sstevel@tonic-gate static void		sl_remember_tgt(const struct stat *, int);
329*0Sstevel@tonic-gate static sl_info_t 	*sl_search(dev_t, ino_t);
330*0Sstevel@tonic-gate static sl_info_t	*sl_devhash_lookup(dev_t);
331*0Sstevel@tonic-gate static void		sl_devhash_insert(dev_t, sl_info_t *);
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate extern int		sl_compare(ino_t, ino_t);
334*0Sstevel@tonic-gate #define	sl_compare(lino, rino)	(lino < rino ? -1 : (lino > rino ? 1 : 0))
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate /* global storage */
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate static sl_remap_t  *sl_remap_head = NULL; /* head of the inode-remap list */
339*0Sstevel@tonic-gate static sl_info_link_t	*sl_devhash[NDEVHENTRY]; /* hash table */
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate /*
342*0Sstevel@tonic-gate  * -------------------------------------------------------------------------
343*0Sstevel@tonic-gate  */
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate static
346*0Sstevel@tonic-gate struct stat	ArchSt,	/* stat(2) information of the archive */
347*0Sstevel@tonic-gate 		SrcSt,	/* stat(2) information of source file */
348*0Sstevel@tonic-gate 		DesSt,	/* stat(2) of destination file */
349*0Sstevel@tonic-gate 		*OldSt = NULL;	/* stat info converted to svr32 format */
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate /*
352*0Sstevel@tonic-gate  * bin_mag: Used to validate a binary magic number,
353*0Sstevel@tonic-gate  * by combining to bytes into an unsigned short.
354*0Sstevel@tonic-gate  */
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate static
357*0Sstevel@tonic-gate union bin_mag {
358*0Sstevel@tonic-gate 	unsigned char b_byte[2];
359*0Sstevel@tonic-gate 	ushort_t b_half;
360*0Sstevel@tonic-gate } Binmag;
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate static
363*0Sstevel@tonic-gate union tblock *Thdr_p;	/* TAR header pointer */
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate static union b_block *bar_Vhdr;
366*0Sstevel@tonic-gate static struct gen_hdr Gen_bar_vol;
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate /*
369*0Sstevel@tonic-gate  * swpbuf: Used in swap() to swap bytes within a halfword,
370*0Sstevel@tonic-gate  * halfwords within a word, or to reverse the order of the
371*0Sstevel@tonic-gate  * bytes within a word.  Also used in mklong() and mkshort().
372*0Sstevel@tonic-gate  */
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate static
375*0Sstevel@tonic-gate union swpbuf {
376*0Sstevel@tonic-gate 	unsigned char	s_byte[4];
377*0Sstevel@tonic-gate 	ushort_t	s_half[2];
378*0Sstevel@tonic-gate 	ulong_t	s_word;
379*0Sstevel@tonic-gate } *Swp_p;
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate static
382*0Sstevel@tonic-gate char	Adir,			/* Flags object as a directory */
383*0Sstevel@tonic-gate 	Hiddendir,		/* Processing hidden attribute directory */
384*0Sstevel@tonic-gate 	Aspec,			/* Flags object as a special file */
385*0Sstevel@tonic-gate 	Do_rename,		/* Indicates rename() is to be used */
386*0Sstevel@tonic-gate 	Time[50],		/* Array to hold date and time */
387*0Sstevel@tonic-gate 	Ttyname[] = "/dev/tty",	/* Controlling console */
388*0Sstevel@tonic-gate 	T_lname[MAXPATHLEN],	/* Array to hold links name for tar */
389*0Sstevel@tonic-gate 	*Buf_p,			/* Buffer for file system I/O */
390*0Sstevel@tonic-gate 	*Empty,			/* Empty block for TARTYP headers */
391*0Sstevel@tonic-gate 	*Full_p,		/* Pointer to full pathname */
392*0Sstevel@tonic-gate 	*Efil_p,		/* -E pattern file string */
393*0Sstevel@tonic-gate 	*Eom_p = "Change to part %d and press RETURN key. [q] ",
394*0Sstevel@tonic-gate 	*Fullnam_p,		/* Full pathname */
395*0Sstevel@tonic-gate 	*Attrfile_p,		/* attribute file */
396*0Sstevel@tonic-gate 	*Hdr_p,			/* -H header type string */
397*0Sstevel@tonic-gate 	*IOfil_p,		/* -I/-O input/output archive string */
398*0Sstevel@tonic-gate 	*Lnkend_p,		/* Pointer to end of Lnknam_p */
399*0Sstevel@tonic-gate 	*Lnknam_p,		/* Buffer for linking files with -p option */
400*0Sstevel@tonic-gate 	*Nam_p,			/* Array to hold filename */
401*0Sstevel@tonic-gate 	*Savenam_p,		/* copy of filename xattr belongs to */
402*0Sstevel@tonic-gate 	*Own_p,			/* New owner login id string */
403*0Sstevel@tonic-gate 	*Renam_p,		/* Buffer for renaming files */
404*0Sstevel@tonic-gate 	*Renametmp_p,		/* Tmp Buffer for renaming files */
405*0Sstevel@tonic-gate 	*Symlnk_p,		/* Buffer for holding symbolic link name */
406*0Sstevel@tonic-gate 	*Over_p,		/* Holds temporary filename when overwriting */
407*0Sstevel@tonic-gate 	**Pat_pp = 0,		/* Pattern strings */
408*0Sstevel@tonic-gate 	bar_linkflag,		/* flag to indicate if the file is a link */
409*0Sstevel@tonic-gate 	bar_linkname[MAXPATHLEN]; /* store the name of the link */
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate static
412*0Sstevel@tonic-gate int	Append = 0,	/* Flag set while searching to end of archive */
413*0Sstevel@tonic-gate 	Archive,	/* File descriptor of the archive */
414*0Sstevel@tonic-gate 	Buf_error = 0,	/* I/O error occured during buffer fill */
415*0Sstevel@tonic-gate 	Def_mode = 0777,	/* Default file/directory protection modes */
416*0Sstevel@tonic-gate 	Device,		/* Device type being accessed (used with libgenIO) */
417*0Sstevel@tonic-gate 	Error_cnt = 0,	/* Cumulative count of I/O errors */
418*0Sstevel@tonic-gate 	Finished = 1,	/* Indicates that a file transfer has completed */
419*0Sstevel@tonic-gate 	Hdrsz = ASCSZ,	/* Fixed length portion of the header */
420*0Sstevel@tonic-gate 	Hdr_type,		/* Flag to indicate type of header selected */
421*0Sstevel@tonic-gate 	Ifile,		/* File des. of file being archived */
422*0Sstevel@tonic-gate 	Ofile,		/* File des. of file being extracted from archive */
423*0Sstevel@tonic-gate 	Use_old_stat = 0,    /* Create an old style -Hodc hdr (small dev's) */
424*0Sstevel@tonic-gate 	Onecopy = 0,	/* Flags old vs. new link handling */
425*0Sstevel@tonic-gate 	Pad_val = 0,	/* Indicates the number of bytes to pad (if any) */
426*0Sstevel@tonic-gate 	PageSize = 0,	/* The native page size, used for figuring block size */
427*0Sstevel@tonic-gate 	Volcnt = 1,	/* Number of archive volumes processed */
428*0Sstevel@tonic-gate 	Verbcnt = 0,	/* Count of number of dots '.' output */
429*0Sstevel@tonic-gate 	Eomflag = 0,
430*0Sstevel@tonic-gate 	Dflag = 0,
431*0Sstevel@tonic-gate 	Atflag = 0,	/* Archive/restore extended attributes */
432*0Sstevel@tonic-gate 	Compressed,	/* Flag to indicate if the bar archive is compressed */
433*0Sstevel@tonic-gate 	Bar_vol_num = 0; /* Volume number count for bar archive */
434*0Sstevel@tonic-gate 
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate static
437*0Sstevel@tonic-gate gid_t	Lastgid = -1;	/* Used with -t & -v to record current gid */
438*0Sstevel@tonic-gate 
439*0Sstevel@tonic-gate static
440*0Sstevel@tonic-gate uid_t	Lastuid = -1;	/* Used with -t & -v to record current uid */
441*0Sstevel@tonic-gate 
442*0Sstevel@tonic-gate static
443*0Sstevel@tonic-gate long	Args,		/* Mask of selected options */
444*0Sstevel@tonic-gate 	Max_namesz = CPATH;	/* Maximum size of pathnames/filenames */
445*0Sstevel@tonic-gate 
446*0Sstevel@tonic-gate static
447*0Sstevel@tonic-gate int	Bufsize = BUFSZ;	/* Default block size */
448*0Sstevel@tonic-gate 
449*0Sstevel@tonic-gate 
450*0Sstevel@tonic-gate static u_longlong_t    Blocks;	/* full blocks transferred */
451*0Sstevel@tonic-gate static u_longlong_t    SBlocks;	/* cumulative char count from short reads */
452*0Sstevel@tonic-gate 
453*0Sstevel@tonic-gate 
454*0Sstevel@tonic-gate static off_t	Max_offset = BIN_OFFSET_MAX;	/* largest file size */
455*0Sstevel@tonic-gate static off_t	Max_filesz;			/* from getrlimit */
456*0Sstevel@tonic-gate 
457*0Sstevel@tonic-gate static
458*0Sstevel@tonic-gate FILE	*Ef_p,			/* File pointer of pattern input file */
459*0Sstevel@tonic-gate 	*Err_p = stderr,	/* File pointer for error reporting */
460*0Sstevel@tonic-gate 	*Out_p = stdout,	/* File pointer for non-archive output */
461*0Sstevel@tonic-gate 	*Rtty_p,		/* Input file pointer for interactive rename */
462*0Sstevel@tonic-gate 	*Wtty_p;		/* Output file ptr for interactive rename */
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate static
465*0Sstevel@tonic-gate ushort_t	Ftype = S_IFMT;	/* File type mask */
466*0Sstevel@tonic-gate static
467*0Sstevel@tonic-gate uid_t	Euid;		/* Effective uid of invoker */
468*0Sstevel@tonic-gate 
469*0Sstevel@tonic-gate /* ACL support */
470*0Sstevel@tonic-gate static struct sec_attr {
471*0Sstevel@tonic-gate 	char	attr_type;
472*0Sstevel@tonic-gate 	char	attr_len[7];
473*0Sstevel@tonic-gate 	char	attr_info[1];
474*0Sstevel@tonic-gate } *attr;
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate static int	Pflag = 0;	/* flag indicates that acl is preserved */
477*0Sstevel@tonic-gate static int	aclcnt = 0;	/* acl entry count */
478*0Sstevel@tonic-gate static aclent_t *aclp = NULL;	/* pointer to ACL */
479*0Sstevel@tonic-gate static int	acl_set = 0;	/* True if an acl was set on the file */
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate /*
482*0Sstevel@tonic-gate  *
483*0Sstevel@tonic-gate  * cpio has been changed to support extended attributes.
484*0Sstevel@tonic-gate  *
485*0Sstevel@tonic-gate  * As part of this change cpio has been changed to use the new *at() syscalls
486*0Sstevel@tonic-gate  * such as openat, fchownat(), unlinkat()...
487*0Sstevel@tonic-gate  *
488*0Sstevel@tonic-gate  * This was done so that attributes can be handled with as few code changes
489*0Sstevel@tonic-gate  * as possible.
490*0Sstevel@tonic-gate  *
491*0Sstevel@tonic-gate  * What this means is that cpio now opens the directory that a file or directory
492*0Sstevel@tonic-gate  * resides in and then performs *at() functions to manipulate the entry.
493*0Sstevel@tonic-gate  *
494*0Sstevel@tonic-gate  * For example a new file is now created like this:
495*0Sstevel@tonic-gate  *
496*0Sstevel@tonic-gate  * dfd = open(<some dir path>)
497*0Sstevel@tonic-gate  * fd = openat(dfd, <name>,....);
498*0Sstevel@tonic-gate  *
499*0Sstevel@tonic-gate  * or in the case of an extended attribute
500*0Sstevel@tonic-gate  *
501*0Sstevel@tonic-gate  * dfd = attropen(<pathname>, ".", ....)
502*0Sstevel@tonic-gate  *
503*0Sstevel@tonic-gate  * Once we have a directory file descriptor all of the *at() functions can
504*0Sstevel@tonic-gate  * be applied to it.
505*0Sstevel@tonic-gate  *
506*0Sstevel@tonic-gate  * unlinkat(dfd, <component name>,...)
507*0Sstevel@tonic-gate  * fchownat(dfd, <component name>,..)
508*0Sstevel@tonic-gate  *
509*0Sstevel@tonic-gate  * This works for both normal namespace files and extended attribute file
510*0Sstevel@tonic-gate  *
511*0Sstevel@tonic-gate  */
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate /*
514*0Sstevel@tonic-gate  * Extended attribute layout
515*0Sstevel@tonic-gate  *
516*0Sstevel@tonic-gate  * Extended attributes are stored in two pieces.
517*0Sstevel@tonic-gate  * 1. An attribute header which has information about
518*0Sstevel@tonic-gate  *    what file the attribute is for and what the attribute
519*0Sstevel@tonic-gate  *    is named.
520*0Sstevel@tonic-gate  * 2. The attribute record itself.  Stored as a normal file type
521*0Sstevel@tonic-gate  *    of entry.
522*0Sstevel@tonic-gate  * Both the header and attribute record have special modes/typeflags
523*0Sstevel@tonic-gate  * associated with them.
524*0Sstevel@tonic-gate  *
525*0Sstevel@tonic-gate  * The names of the header in the archive look like:
526*0Sstevel@tonic-gate  * /dev/null/attr.hdr
527*0Sstevel@tonic-gate  *
528*0Sstevel@tonic-gate  * The name of the attribute looks like:
529*0Sstevel@tonic-gate  * /dev/null/attr.
530*0Sstevel@tonic-gate  *
531*0Sstevel@tonic-gate  * This is done so that an archiver that doesn't understand these formats
532*0Sstevel@tonic-gate  * can just dispose of the attribute records unless the user chooses to
533*0Sstevel@tonic-gate  * rename them via cpio -r or pax -i
534*0Sstevel@tonic-gate  *
535*0Sstevel@tonic-gate  * The format is composed of a fixed size header followed
536*0Sstevel@tonic-gate  * by a variable sized xattr_buf. If the attribute is a hard link
537*0Sstevel@tonic-gate  * to another attribute, then another xattr_buf section is included
538*0Sstevel@tonic-gate  * for the link.
539*0Sstevel@tonic-gate  *
540*0Sstevel@tonic-gate  * The xattr_buf is used to define the necessary "pathing" steps
541*0Sstevel@tonic-gate  * to get to the extended attribute.  This is necessary to support
542*0Sstevel@tonic-gate  * a fully recursive attribute model where an attribute may itself
543*0Sstevel@tonic-gate  * have an attribute.
544*0Sstevel@tonic-gate  *
545*0Sstevel@tonic-gate  * The basic layout looks like this.
546*0Sstevel@tonic-gate  *
547*0Sstevel@tonic-gate  *     --------------------------------
548*0Sstevel@tonic-gate  *     |                              |
549*0Sstevel@tonic-gate  *     |         xattr_hdr            |
550*0Sstevel@tonic-gate  *     |                              |
551*0Sstevel@tonic-gate  *     --------------------------------
552*0Sstevel@tonic-gate  *     --------------------------------
553*0Sstevel@tonic-gate  *     |                              |
554*0Sstevel@tonic-gate  *     |        xattr_buf             |
555*0Sstevel@tonic-gate  *     |                              |
556*0Sstevel@tonic-gate  *     --------------------------------
557*0Sstevel@tonic-gate  *     --------------------------------
558*0Sstevel@tonic-gate  *     |                              |
559*0Sstevel@tonic-gate  *     |      (optional link info)    |
560*0Sstevel@tonic-gate  *     |                              |
561*0Sstevel@tonic-gate  *     --------------------------------
562*0Sstevel@tonic-gate  *     --------------------------------
563*0Sstevel@tonic-gate  *     |                              |
564*0Sstevel@tonic-gate  *     |      attribute itself        |
565*0Sstevel@tonic-gate  *     |      stored as normal tar    |
566*0Sstevel@tonic-gate  *     |      or cpio data with       |
567*0Sstevel@tonic-gate  *     |      special mode or         |
568*0Sstevel@tonic-gate  *     |      typeflag                |
569*0Sstevel@tonic-gate  *     |                              |
570*0Sstevel@tonic-gate  *     --------------------------------
571*0Sstevel@tonic-gate  *
572*0Sstevel@tonic-gate  */
573*0Sstevel@tonic-gate 
574*0Sstevel@tonic-gate /*
575*0Sstevel@tonic-gate  * Extended attributes structures
576*0Sstevel@tonic-gate  *
577*0Sstevel@tonic-gate  * xattrhead is the complete extended attribute header, as read of off
578*0Sstevel@tonic-gate  * disk/tape. It includes the variable xattr_buf portion.
579*0Sstevel@tonic-gate  *
580*0Sstevel@tonic-gate  * xattrp is basically an offset into xattrhead that points to the
581*0Sstevel@tonic-gate  * "pathing" section which defines how to get to the attribute.
582*0Sstevel@tonic-gate  *
583*0Sstevel@tonic-gate  * xattr_linkp is identical to xattrp except that it is used for linked
584*0Sstevel@tonic-gate  * attributes.  It provides the pathing steps to get to the linked
585*0Sstevel@tonic-gate  * attribute.
586*0Sstevel@tonic-gate  *
587*0Sstevel@tonic-gate  * These structures are updated when an extended attribute header is read off
588*0Sstevel@tonic-gate  * of disk/tape.
589*0Sstevel@tonic-gate  */
590*0Sstevel@tonic-gate static struct xattr_hdr	*xattrhead;
591*0Sstevel@tonic-gate static struct xattr_buf	*xattrp;
592*0Sstevel@tonic-gate static struct xattr_buf	*xattr_linkp;
593*0Sstevel@tonic-gate static int 		xattrbadhead;	/* is extended attribute header bad? */
594*0Sstevel@tonic-gate 
595*0Sstevel@tonic-gate static int	append_secattr(char **, int *, int, char *, char);
596*0Sstevel@tonic-gate static void	write_ancillary(char *, int);
597*0Sstevel@tonic-gate 
598*0Sstevel@tonic-gate /*
599*0Sstevel@tonic-gate  * Note regarding cpio and changes to ensure cpio doesn't try to second
600*0Sstevel@tonic-gate  * guess whether it runs with sufficient privileges or not:
601*0Sstevel@tonic-gate  *
602*0Sstevel@tonic-gate  * cpio has been changed so that it doesn't carry a second implementation of
603*0Sstevel@tonic-gate  * the kernel's policy with respect to privileges.  Instead of attempting
604*0Sstevel@tonic-gate  * to restore uid and gid from an archive only if cpio is run as uid 0,
605*0Sstevel@tonic-gate  * cpio now *always* tries to restore the uid and gid from the archive.
606*0Sstevel@tonic-gate  * If the restoration of the uid and gid are unsuccessful, then an error
607*0Sstevel@tonic-gate  * message will only be received if cpio is being run with root privileges,
608*0Sstevel@tonic-gate  * i.e., effective uid (euid) is 0, or if -R was specified.
609*0Sstevel@tonic-gate  *
610*0Sstevel@tonic-gate  * Note regarding debugging mechanism for cpio:
611*0Sstevel@tonic-gate  *
612*0Sstevel@tonic-gate  * The following mechanism is provided to allow us to debug cpio in complicated
613*0Sstevel@tonic-gate  * situations, like when it is part of a pipe.  The idea is that you compile
614*0Sstevel@tonic-gate  * with -DWAITAROUND defined, and then add the "-z" command line option to the
615*0Sstevel@tonic-gate  * target cpio invocation.  If stderr is available, it will tell you to which
616*0Sstevel@tonic-gate  * pid to attach the debugger; otherwise, use ps to find it.  Attach to the
617*0Sstevel@tonic-gate  * process from the debugger, and, *PRESTO*, you are there!
618*0Sstevel@tonic-gate  *
619*0Sstevel@tonic-gate  * Simply assign "waitaround = 0" once you attach to the process, and then
620*0Sstevel@tonic-gate  * proceed from there as usual.
621*0Sstevel@tonic-gate  */
622*0Sstevel@tonic-gate 
623*0Sstevel@tonic-gate #ifdef WAITAROUND
624*0Sstevel@tonic-gate int waitaround = 0;		/* wait for rendezvous with the debugger */
625*0Sstevel@tonic-gate #endif
626*0Sstevel@tonic-gate 
627*0Sstevel@tonic-gate /*
628*0Sstevel@tonic-gate  * Allocation wrappers and their flags
629*0Sstevel@tonic-gate  */
630*0Sstevel@tonic-gate #define	E_NORMAL	0x0	/* Return NULL if allocation fails */
631*0Sstevel@tonic-gate #define	E_EXIT		0x1	/* Exit if allocation fails */
632*0Sstevel@tonic-gate 
633*0Sstevel@tonic-gate static void *e_realloc(int flag, void *old, size_t newsize);
634*0Sstevel@tonic-gate static char *e_strdup(int flag, const char *arg);
635*0Sstevel@tonic-gate static void *e_valloc(int flag, size_t size);
636*0Sstevel@tonic-gate static void *e_zalloc(int flag, size_t size);
637*0Sstevel@tonic-gate 
638*0Sstevel@tonic-gate #define	EXIT_CODE	(Error_cnt > 255 ? 255 : Error_cnt)
639*0Sstevel@tonic-gate 
640*0Sstevel@tonic-gate /*
641*0Sstevel@tonic-gate  * main: Call setup() to process options and perform initializations,
642*0Sstevel@tonic-gate  * and then select either copy in (-i), copy out (-o), or pass (-p) action.
643*0Sstevel@tonic-gate  */
644*0Sstevel@tonic-gate 
645*0Sstevel@tonic-gate main(int argc, char **argv)
646*0Sstevel@tonic-gate {
647*0Sstevel@tonic-gate 	int i;
648*0Sstevel@tonic-gate 	int passret;
649*0Sstevel@tonic-gate 
650*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
651*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
652*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
653*0Sstevel@tonic-gate #endif
654*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
655*0Sstevel@tonic-gate 
656*0Sstevel@tonic-gate 	(void) memset(&Gen, 0, sizeof (Gen));
657*0Sstevel@tonic-gate 	setup(argc, argv);
658*0Sstevel@tonic-gate 
659*0Sstevel@tonic-gate 	if (signal(SIGINT, sigint) == SIG_IGN)
660*0Sstevel@tonic-gate 		(void) signal(SIGINT, SIG_IGN);
661*0Sstevel@tonic-gate 	switch (Args & (OCi | OCo | OCp)) {
662*0Sstevel@tonic-gate 	case OCi: /* COPY IN */
663*0Sstevel@tonic-gate 		Hdr_type = NONE;
664*0Sstevel@tonic-gate 		while ((i = gethdr()) != 0) {
665*0Sstevel@tonic-gate 			Gen.g_dirfd = -1;
666*0Sstevel@tonic-gate 			if (i == 1) {
667*0Sstevel@tonic-gate 				file_in();
668*0Sstevel@tonic-gate 				/*
669*0Sstevel@tonic-gate 				 * Any ACL info for this file would or should
670*0Sstevel@tonic-gate 				 * have been used after file_in(); clear out
671*0Sstevel@tonic-gate 				 * aclp so it is is not erroneously used on
672*0Sstevel@tonic-gate 				 * the next file.
673*0Sstevel@tonic-gate 				 */
674*0Sstevel@tonic-gate 				if (aclp != NULL) {
675*0Sstevel@tonic-gate 					free(aclp);
676*0Sstevel@tonic-gate 					aclcnt = 0;
677*0Sstevel@tonic-gate 					aclp = NULL;
678*0Sstevel@tonic-gate 				}
679*0Sstevel@tonic-gate 				acl_set = 0;
680*0Sstevel@tonic-gate 			}
681*0Sstevel@tonic-gate 			(void) memset(&Gen, 0, sizeof (Gen));
682*0Sstevel@tonic-gate 		}
683*0Sstevel@tonic-gate 		/* Do not count "extra" "read-ahead" buffered data */
684*0Sstevel@tonic-gate 		if (Buffr.b_cnt > Bufsize)
685*0Sstevel@tonic-gate 			Blocks -=  (u_longlong_t)(Buffr.b_cnt / Bufsize);
686*0Sstevel@tonic-gate 		break;
687*0Sstevel@tonic-gate 	case OCo: /* COPY OUT */
688*0Sstevel@tonic-gate 		if (Args & OCA) {
689*0Sstevel@tonic-gate 			scan4trail();
690*0Sstevel@tonic-gate 		}
691*0Sstevel@tonic-gate 
692*0Sstevel@tonic-gate 		Gen.g_dirfd = -1;
693*0Sstevel@tonic-gate 		Gen.g_dirpath = NULL;
694*0Sstevel@tonic-gate 		sl_preview_synonyms();
695*0Sstevel@tonic-gate 
696*0Sstevel@tonic-gate 		while ((i = getname()) != 0) {
697*0Sstevel@tonic-gate 			if (i == 1) {
698*0Sstevel@tonic-gate 				(void) file_out();
699*0Sstevel@tonic-gate 				if (Atflag) {
700*0Sstevel@tonic-gate 					if (Gen.g_dirfd != -1) {
701*0Sstevel@tonic-gate 						(void) close(Gen.g_dirfd);
702*0Sstevel@tonic-gate 					}
703*0Sstevel@tonic-gate 					Gen.g_dirfd = -1;
704*0Sstevel@tonic-gate 					xattrs_out(file_out);
705*0Sstevel@tonic-gate 				}
706*0Sstevel@tonic-gate 				Hiddendir = 0;
707*0Sstevel@tonic-gate 			}
708*0Sstevel@tonic-gate 			if (aclp != NULL) {
709*0Sstevel@tonic-gate 				free(aclp);
710*0Sstevel@tonic-gate 				aclcnt = 0;
711*0Sstevel@tonic-gate 				aclp = NULL;
712*0Sstevel@tonic-gate 				acl_set = 0;
713*0Sstevel@tonic-gate 			}
714*0Sstevel@tonic-gate 		}
715*0Sstevel@tonic-gate 		write_trail();
716*0Sstevel@tonic-gate 		break;
717*0Sstevel@tonic-gate 	case OCp: /* PASS */
718*0Sstevel@tonic-gate 		sl_preview_synonyms();
719*0Sstevel@tonic-gate 
720*0Sstevel@tonic-gate 		Gen.g_dirfd = -1;
721*0Sstevel@tonic-gate 		Gen.g_passdirfd = -1;
722*0Sstevel@tonic-gate 		Gen.g_dirpath = NULL;
723*0Sstevel@tonic-gate 		while (getname()) {
724*0Sstevel@tonic-gate 			/*
725*0Sstevel@tonic-gate 			 * If file is a fully qualified path then
726*0Sstevel@tonic-gate 			 * file_pass will strip off the leading '/'
727*0Sstevel@tonic-gate 			 * and we need to save off the unstripped
728*0Sstevel@tonic-gate 			 * name for attribute traversal.
729*0Sstevel@tonic-gate 			 */
730*0Sstevel@tonic-gate 			if (Atflag) {
731*0Sstevel@tonic-gate 				(void) strcpy(Savenam_p, Gen.g_nam_p);
732*0Sstevel@tonic-gate 			}
733*0Sstevel@tonic-gate 			passret = file_pass();
734*0Sstevel@tonic-gate 			if (aclp != NULL) {
735*0Sstevel@tonic-gate 				free(aclp);
736*0Sstevel@tonic-gate 				aclcnt = 0;
737*0Sstevel@tonic-gate 				aclp = NULL;
738*0Sstevel@tonic-gate 				acl_set = 0;
739*0Sstevel@tonic-gate 			}
740*0Sstevel@tonic-gate 			if (Gen.g_passdirfd != -1)
741*0Sstevel@tonic-gate 				(void) close(Gen.g_passdirfd);
742*0Sstevel@tonic-gate 			Gen.g_passdirfd = -1;
743*0Sstevel@tonic-gate 			if (Atflag) {
744*0Sstevel@tonic-gate 				if (Gen.g_dirfd != -1) {
745*0Sstevel@tonic-gate 					(void) close(Gen.g_dirfd);
746*0Sstevel@tonic-gate 				}
747*0Sstevel@tonic-gate 				Gen.g_dirfd = -1;
748*0Sstevel@tonic-gate 				if (passret != FILE_LINKED) {
749*0Sstevel@tonic-gate 					Gen.g_nam_p = Savenam_p;
750*0Sstevel@tonic-gate 					xattrs_out(file_pass);
751*0Sstevel@tonic-gate 				}
752*0Sstevel@tonic-gate 			}
753*0Sstevel@tonic-gate 		}
754*0Sstevel@tonic-gate 		break;
755*0Sstevel@tonic-gate 	default:
756*0Sstevel@tonic-gate 		msg(EXT, "Impossible action.");
757*0Sstevel@tonic-gate 	}
758*0Sstevel@tonic-gate 	if (Ofile > 0) {
759*0Sstevel@tonic-gate 		if (close(Ofile) != 0)
760*0Sstevel@tonic-gate 			msg(EXTN, "close error");
761*0Sstevel@tonic-gate 	}
762*0Sstevel@tonic-gate 	if (Archive > 0) {
763*0Sstevel@tonic-gate 		if (close(Archive) != 0)
764*0Sstevel@tonic-gate 			msg(EXTN, "close error");
765*0Sstevel@tonic-gate 	}
766*0Sstevel@tonic-gate 	Blocks = (u_longlong_t)(Blocks * Bufsize + SBlocks + 0x1FF) >> 9;
767*0Sstevel@tonic-gate 	msg(EPOST, "%lld blocks", Blocks);
768*0Sstevel@tonic-gate 	if (Error_cnt)
769*0Sstevel@tonic-gate 		msg(EPOST, "%d error(s)", Error_cnt);
770*0Sstevel@tonic-gate 	return (EXIT_CODE);
771*0Sstevel@tonic-gate }
772*0Sstevel@tonic-gate 
773*0Sstevel@tonic-gate /*
774*0Sstevel@tonic-gate  * add_lnk: Add a linked file's header to the linked file data structure, by
775*0Sstevel@tonic-gate  * either adding it to the end of an existing sub-list or starting
776*0Sstevel@tonic-gate  * a new sub-list.  Each sub-list saves the links to a given file.
777*0Sstevel@tonic-gate  *
778*0Sstevel@tonic-gate  * Directly returns a pointer to the new entry; returns a pointer to the head
779*0Sstevel@tonic-gate  * of the sub-list in which that entry is located through the argument.
780*0Sstevel@tonic-gate  */
781*0Sstevel@tonic-gate 
782*0Sstevel@tonic-gate static struct Lnk *
783*0Sstevel@tonic-gate add_lnk(struct Lnk **sublist_return)
784*0Sstevel@tonic-gate {
785*0Sstevel@tonic-gate 	struct Lnk *new_entry, *sublist;
786*0Sstevel@tonic-gate 
787*0Sstevel@tonic-gate 	for (sublist = Lnk_hd.L_nxt_p;
788*0Sstevel@tonic-gate 	    sublist != &Lnk_hd;
789*0Sstevel@tonic-gate 	    sublist = sublist->L_nxt_p) {
790*0Sstevel@tonic-gate 		if (sublist->L_gen.g_ino == G_p->g_ino &&
791*0Sstevel@tonic-gate 		    sublist->L_gen.g_dev == G_p->g_dev) {
792*0Sstevel@tonic-gate 			/* found */
793*0Sstevel@tonic-gate 			break;
794*0Sstevel@tonic-gate 		}
795*0Sstevel@tonic-gate 	}
796*0Sstevel@tonic-gate 
797*0Sstevel@tonic-gate 	new_entry = e_zalloc(E_EXIT, sizeof (struct Lnk));
798*0Sstevel@tonic-gate 
799*0Sstevel@tonic-gate 	new_entry->L_lnk_p = NULL;
800*0Sstevel@tonic-gate 	new_entry->L_gen = *G_p; /* structure copy */
801*0Sstevel@tonic-gate 
802*0Sstevel@tonic-gate 	new_entry->L_gen.g_nam_p = e_zalloc(E_EXIT, (size_t)G_p->g_namesz);
803*0Sstevel@tonic-gate 
804*0Sstevel@tonic-gate 	(void) strcpy(new_entry->L_gen.g_nam_p, G_p->g_nam_p);
805*0Sstevel@tonic-gate 
806*0Sstevel@tonic-gate 	if (sublist == &Lnk_hd) {
807*0Sstevel@tonic-gate 		/* start new sub-list */
808*0Sstevel@tonic-gate 		new_entry->L_nxt_p = &Lnk_hd;
809*0Sstevel@tonic-gate 		new_entry->L_bck_p = Lnk_hd.L_bck_p;
810*0Sstevel@tonic-gate 		Lnk_hd.L_bck_p = new_entry->L_bck_p->L_nxt_p = new_entry;
811*0Sstevel@tonic-gate 		new_entry->L_lnk_p = NULL;
812*0Sstevel@tonic-gate 		new_entry->L_cnt = 1;
813*0Sstevel@tonic-gate 		new_entry->L_data = Onecopy ? 0 : 1;
814*0Sstevel@tonic-gate 		sublist = new_entry;
815*0Sstevel@tonic-gate 	} else {
816*0Sstevel@tonic-gate 		/* add to existing sub-list */
817*0Sstevel@tonic-gate 		struct Lnk *ptr;
818*0Sstevel@tonic-gate 
819*0Sstevel@tonic-gate 		sublist->L_cnt++;
820*0Sstevel@tonic-gate 
821*0Sstevel@tonic-gate 		for (ptr = sublist;
822*0Sstevel@tonic-gate 		    ptr->L_lnk_p != NULL;
823*0Sstevel@tonic-gate 		    ptr = ptr->L_lnk_p) {
824*0Sstevel@tonic-gate 			ptr->L_gen.g_filesz = G_p->g_filesz;
825*0Sstevel@tonic-gate 		}
826*0Sstevel@tonic-gate 
827*0Sstevel@tonic-gate 		ptr->L_gen.g_filesz = G_p->g_filesz;
828*0Sstevel@tonic-gate 		ptr->L_lnk_p = new_entry;
829*0Sstevel@tonic-gate 	}
830*0Sstevel@tonic-gate 
831*0Sstevel@tonic-gate 	*sublist_return = sublist;
832*0Sstevel@tonic-gate 	return (new_entry);
833*0Sstevel@tonic-gate }
834*0Sstevel@tonic-gate 
835*0Sstevel@tonic-gate /*
836*0Sstevel@tonic-gate  * bfill: Read req_cnt bytes (out of filelen bytes) from the I/O buffer,
837*0Sstevel@tonic-gate  * moving them to rd_buf_p.  When there are no bytes left in the I/O buffer,
838*0Sstevel@tonic-gate  * Fillbuf is set and the I/O buffer is filled.  The variable dist is the
839*0Sstevel@tonic-gate  * distance to lseek if an I/O error is encountered with the -k option set
840*0Sstevel@tonic-gate  * (converted to a multiple of Bufsize).
841*0Sstevel@tonic-gate  */
842*0Sstevel@tonic-gate 
843*0Sstevel@tonic-gate static int
844*0Sstevel@tonic-gate bfill(void)
845*0Sstevel@tonic-gate {
846*0Sstevel@tonic-gate 	int i = 0, rv;
847*0Sstevel@tonic-gate 	static int eof = 0;
848*0Sstevel@tonic-gate 
849*0Sstevel@tonic-gate 	if (!Dflag) {
850*0Sstevel@tonic-gate 	while ((Buffr.b_end_p - Buffr.b_in_p) >= Bufsize) {
851*0Sstevel@tonic-gate 		errno = 0;
852*0Sstevel@tonic-gate 		if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0) {
853*0Sstevel@tonic-gate 			if (((Buffr.b_end_p - Buffr.b_in_p) >= Bufsize) &&
854*0Sstevel@tonic-gate 			    (Eomflag == 0)) {
855*0Sstevel@tonic-gate 				Eomflag = 1;
856*0Sstevel@tonic-gate 				return (1);
857*0Sstevel@tonic-gate 			}
858*0Sstevel@tonic-gate 			if (errno == ENOSPC) {
859*0Sstevel@tonic-gate 				(void) chgreel(INPUT);
860*0Sstevel@tonic-gate 				if (Hdr_type == BAR) {
861*0Sstevel@tonic-gate 					skip_bar_volhdr();
862*0Sstevel@tonic-gate 				}
863*0Sstevel@tonic-gate 				continue;
864*0Sstevel@tonic-gate 			} else if (Args & OCk) {
865*0Sstevel@tonic-gate 				if (i++ > MX_SEEKS)
866*0Sstevel@tonic-gate 					msg(EXT, "Cannot recover.");
867*0Sstevel@tonic-gate 				if (lseek(Archive, Bufsize, SEEK_REL) < 0)
868*0Sstevel@tonic-gate 					msg(EXTN, "Cannot lseek()");
869*0Sstevel@tonic-gate 				Error_cnt++;
870*0Sstevel@tonic-gate 				Buf_error++;
871*0Sstevel@tonic-gate 				rv = 0;
872*0Sstevel@tonic-gate 				continue;
873*0Sstevel@tonic-gate 			} else
874*0Sstevel@tonic-gate 				ioerror(INPUT);
875*0Sstevel@tonic-gate 		} /* (rv = g_read(Device, Archive ... */
876*0Sstevel@tonic-gate 		if (Hdr_type != BAR || rv == Bufsize) {
877*0Sstevel@tonic-gate 			Buffr.b_in_p += rv;
878*0Sstevel@tonic-gate 			Buffr.b_cnt += (long)rv;
879*0Sstevel@tonic-gate 		}
880*0Sstevel@tonic-gate 		if (rv == Bufsize) {
881*0Sstevel@tonic-gate 			eof = 0;
882*0Sstevel@tonic-gate 			Blocks++;
883*0Sstevel@tonic-gate 		} else if (rv == 0) {
884*0Sstevel@tonic-gate 			if (!eof) {
885*0Sstevel@tonic-gate 				eof = 1;
886*0Sstevel@tonic-gate 				break;
887*0Sstevel@tonic-gate 			}
888*0Sstevel@tonic-gate 			(void) chgreel(INPUT);
889*0Sstevel@tonic-gate 			eof = 0;	/* reset the eof after chgreel	*/
890*0Sstevel@tonic-gate 
891*0Sstevel@tonic-gate 			/*
892*0Sstevel@tonic-gate 			 * if spans multiple volume, skip the volume header of
893*0Sstevel@tonic-gate 			 * the next volume so that the file currently being
894*0Sstevel@tonic-gate 			 * extracted can continue to be extracted.
895*0Sstevel@tonic-gate 			 */
896*0Sstevel@tonic-gate 			if (Hdr_type == BAR) {
897*0Sstevel@tonic-gate 				skip_bar_volhdr();
898*0Sstevel@tonic-gate 			}
899*0Sstevel@tonic-gate 
900*0Sstevel@tonic-gate 			continue;
901*0Sstevel@tonic-gate 		} else {
902*0Sstevel@tonic-gate 			eof = 0;
903*0Sstevel@tonic-gate 			SBlocks += (u_longlong_t)rv;
904*0Sstevel@tonic-gate 		}
905*0Sstevel@tonic-gate 	} /* (Buffr.b_end_p - Buffr.b_in_p) <= Bufsize */
906*0Sstevel@tonic-gate 
907*0Sstevel@tonic-gate 	} else {			/* Dflag */
908*0Sstevel@tonic-gate 		errno = 0;
909*0Sstevel@tonic-gate 		if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0) {
910*0Sstevel@tonic-gate 			return (-1);
911*0Sstevel@tonic-gate 		} /* (rv = g_read(Device, Archive ... */
912*0Sstevel@tonic-gate 		Buffr.b_in_p += rv;
913*0Sstevel@tonic-gate 		Buffr.b_cnt += (long)rv;
914*0Sstevel@tonic-gate 		if (rv == Bufsize) {
915*0Sstevel@tonic-gate 			eof = 0;
916*0Sstevel@tonic-gate 			Blocks++;
917*0Sstevel@tonic-gate 		} else if (!rv) {
918*0Sstevel@tonic-gate 			if (!eof) {
919*0Sstevel@tonic-gate 				eof = 1;
920*0Sstevel@tonic-gate 				return (rv);
921*0Sstevel@tonic-gate 			}
922*0Sstevel@tonic-gate 			return (-1);
923*0Sstevel@tonic-gate 		} else {
924*0Sstevel@tonic-gate 			eof = 0;
925*0Sstevel@tonic-gate 			SBlocks += (u_longlong_t)rv;
926*0Sstevel@tonic-gate 		}
927*0Sstevel@tonic-gate 	}
928*0Sstevel@tonic-gate 	return (rv);
929*0Sstevel@tonic-gate }
930*0Sstevel@tonic-gate 
931*0Sstevel@tonic-gate /*
932*0Sstevel@tonic-gate  * bflush: Move wr_cnt bytes from data_p into the I/O buffer.  When the
933*0Sstevel@tonic-gate  * I/O buffer is full, Flushbuf is set and the buffer is written out.
934*0Sstevel@tonic-gate  */
935*0Sstevel@tonic-gate 
936*0Sstevel@tonic-gate static void
937*0Sstevel@tonic-gate bflush(void)
938*0Sstevel@tonic-gate {
939*0Sstevel@tonic-gate 	int rv;
940*0Sstevel@tonic-gate 
941*0Sstevel@tonic-gate 	while (Buffr.b_cnt >= Bufsize) {
942*0Sstevel@tonic-gate 		errno = 0;
943*0Sstevel@tonic-gate 		if ((rv = g_write(Device, Archive, Buffr.b_out_p,
944*0Sstevel@tonic-gate 		    Bufsize)) < 0) {
945*0Sstevel@tonic-gate 			if (errno == ENOSPC && !Dflag)
946*0Sstevel@tonic-gate 				rv = chgreel(OUTPUT);
947*0Sstevel@tonic-gate 			else
948*0Sstevel@tonic-gate 				ioerror(OUTPUT);
949*0Sstevel@tonic-gate 		}
950*0Sstevel@tonic-gate 		Buffr.b_out_p += rv;
951*0Sstevel@tonic-gate 		Buffr.b_cnt -= (long)rv;
952*0Sstevel@tonic-gate 		if (rv == Bufsize)
953*0Sstevel@tonic-gate 			Blocks++;
954*0Sstevel@tonic-gate 		else if (rv > 0)
955*0Sstevel@tonic-gate 			SBlocks += (u_longlong_t)rv;
956*0Sstevel@tonic-gate 	}
957*0Sstevel@tonic-gate 	rstbuf();
958*0Sstevel@tonic-gate }
959*0Sstevel@tonic-gate 
960*0Sstevel@tonic-gate /*
961*0Sstevel@tonic-gate  * chgreel: Determine if end-of-medium has been reached.  If it has,
962*0Sstevel@tonic-gate  * close the current medium and prompt the user for the next medium.
963*0Sstevel@tonic-gate  */
964*0Sstevel@tonic-gate 
965*0Sstevel@tonic-gate static int
966*0Sstevel@tonic-gate chgreel(int dir)
967*0Sstevel@tonic-gate {
968*0Sstevel@tonic-gate 	int lastchar, tryagain, askagain, rv;
969*0Sstevel@tonic-gate 	int tmpdev;
970*0Sstevel@tonic-gate 	char str[APATH];
971*0Sstevel@tonic-gate 	struct stat statb;
972*0Sstevel@tonic-gate 
973*0Sstevel@tonic-gate 	rv = 0;
974*0Sstevel@tonic-gate 	if (fstat(Archive, &statb) < 0)
975*0Sstevel@tonic-gate 		msg(EXTN, "Error during stat() of archive");
976*0Sstevel@tonic-gate 	if ((statb.st_mode & S_IFMT) != S_IFCHR) {
977*0Sstevel@tonic-gate 		if (dir == INPUT) {
978*0Sstevel@tonic-gate 			msg(EXT, "%s%s\n",
979*0Sstevel@tonic-gate 				"Can't read input:  end of file encountered ",
980*0Sstevel@tonic-gate 				"prior to expected end of archive.");
981*0Sstevel@tonic-gate 		}
982*0Sstevel@tonic-gate 	}
983*0Sstevel@tonic-gate 	msg(EPOST, "\007End of medium on \"%s\".", dir ? "output" : "input");
984*0Sstevel@tonic-gate 	if (is_floppy(Archive))
985*0Sstevel@tonic-gate 		(void) ioctl(Archive, FDEJECT, NULL);
986*0Sstevel@tonic-gate 	if ((close(Archive) != 0) && (dir == OUTPUT))
987*0Sstevel@tonic-gate 		msg(EXTN, "close error");
988*0Sstevel@tonic-gate 	Archive = 0;
989*0Sstevel@tonic-gate 	Volcnt++;
990*0Sstevel@tonic-gate 	for (;;) {
991*0Sstevel@tonic-gate 		if (Rtty_p == (FILE *)NULL)
992*0Sstevel@tonic-gate 			Rtty_p = fopen(Ttyname, "r");
993*0Sstevel@tonic-gate 		do { /* tryagain */
994*0Sstevel@tonic-gate 			if (IOfil_p) {
995*0Sstevel@tonic-gate 				do {
996*0Sstevel@tonic-gate 					msg(EPOST, gettext(Eom_p), Volcnt);
997*0Sstevel@tonic-gate 					if (!Rtty_p || fgets(str, sizeof (str),
998*0Sstevel@tonic-gate 					    Rtty_p) == (char *)NULL)
999*0Sstevel@tonic-gate 						msg(EXT, "Cannot read tty.");
1000*0Sstevel@tonic-gate 					askagain = 0;
1001*0Sstevel@tonic-gate 					switch (*str) {
1002*0Sstevel@tonic-gate 					case '\n':
1003*0Sstevel@tonic-gate 						(void) strcpy(str, IOfil_p);
1004*0Sstevel@tonic-gate 						break;
1005*0Sstevel@tonic-gate 					case 'q':
1006*0Sstevel@tonic-gate 						exit(EXIT_CODE);
1007*0Sstevel@tonic-gate 					default:
1008*0Sstevel@tonic-gate 						askagain = 1;
1009*0Sstevel@tonic-gate 					}
1010*0Sstevel@tonic-gate 				} while (askagain);
1011*0Sstevel@tonic-gate 			} else {
1012*0Sstevel@tonic-gate 
1013*0Sstevel@tonic-gate 				if (Hdr_type == BAR)
1014*0Sstevel@tonic-gate 					Bar_vol_num++;
1015*0Sstevel@tonic-gate 
1016*0Sstevel@tonic-gate 				msg(EPOST,
1017*0Sstevel@tonic-gate 				    "To continue, type device/file name when "
1018*0Sstevel@tonic-gate 				    "ready.");
1019*0Sstevel@tonic-gate 				if (!Rtty_p || fgets(str, sizeof (str),
1020*0Sstevel@tonic-gate 				    Rtty_p) == (char *)NULL)
1021*0Sstevel@tonic-gate 					msg(EXT, "Cannot read tty.");
1022*0Sstevel@tonic-gate 				lastchar = strlen(str) - 1;
1023*0Sstevel@tonic-gate 				if (*(str + lastchar) == '\n') /* remove '\n' */
1024*0Sstevel@tonic-gate 					*(str + lastchar) = '\0';
1025*0Sstevel@tonic-gate 				if (!*str)
1026*0Sstevel@tonic-gate 					exit(EXIT_CODE);
1027*0Sstevel@tonic-gate 			}
1028*0Sstevel@tonic-gate 			tryagain = 0;
1029*0Sstevel@tonic-gate 			if ((Archive = open(str, dir)) < 0) {
1030*0Sstevel@tonic-gate 				msg(ERRN, "Cannot open \"%s\"", str);
1031*0Sstevel@tonic-gate 				tryagain = 1;
1032*0Sstevel@tonic-gate 			}
1033*0Sstevel@tonic-gate 		} while (tryagain);
1034*0Sstevel@tonic-gate 		(void) g_init(&tmpdev, &Archive);
1035*0Sstevel@tonic-gate 		if (tmpdev != Device)
1036*0Sstevel@tonic-gate 			msg(EXT, "Cannot change media types in mid-stream.");
1037*0Sstevel@tonic-gate 		if (dir == INPUT)
1038*0Sstevel@tonic-gate 			break;
1039*0Sstevel@tonic-gate 		else { /* dir == OUTPUT */
1040*0Sstevel@tonic-gate 			errno = 0;
1041*0Sstevel@tonic-gate 			if ((rv = g_write(Device, Archive, Buffr.b_out_p,
1042*0Sstevel@tonic-gate 			    Bufsize)) == Bufsize)
1043*0Sstevel@tonic-gate 				break;
1044*0Sstevel@tonic-gate 			else
1045*0Sstevel@tonic-gate 				msg(ERR,
1046*0Sstevel@tonic-gate 				    "Unable to write this medium, try "
1047*0Sstevel@tonic-gate 				    "another.");
1048*0Sstevel@tonic-gate 		}
1049*0Sstevel@tonic-gate 	} /* ;; */
1050*0Sstevel@tonic-gate 	Eomflag = 0;
1051*0Sstevel@tonic-gate 	return (rv);
1052*0Sstevel@tonic-gate }
1053*0Sstevel@tonic-gate 
1054*0Sstevel@tonic-gate /*
1055*0Sstevel@tonic-gate  * ckname: Check filenames against user specified patterns,
1056*0Sstevel@tonic-gate  * and/or ask the user for new name when -r is used.
1057*0Sstevel@tonic-gate  */
1058*0Sstevel@tonic-gate 
1059*0Sstevel@tonic-gate static int
1060*0Sstevel@tonic-gate ckname(int flag)
1061*0Sstevel@tonic-gate {
1062*0Sstevel@tonic-gate 	int lastchar, len;
1063*0Sstevel@tonic-gate 
1064*0Sstevel@tonic-gate 	if (Hdr_type != TAR && Hdr_type != USTAR && Hdr_type != BAR) {
1065*0Sstevel@tonic-gate 		/* Re-visit tar size issues later */
1066*0Sstevel@tonic-gate 		if (G_p->g_namesz - 1 > Max_namesz) {
1067*0Sstevel@tonic-gate 			msg(ERR, "Name exceeds maximum length - skipped.");
1068*0Sstevel@tonic-gate 			return (F_SKIP);
1069*0Sstevel@tonic-gate 		}
1070*0Sstevel@tonic-gate 	}
1071*0Sstevel@tonic-gate 
1072*0Sstevel@tonic-gate 	if (Pat_pp && !matched())
1073*0Sstevel@tonic-gate 		return (F_SKIP);
1074*0Sstevel@tonic-gate 	if ((Args & OCr) && !Adir) { /* rename interactively */
1075*0Sstevel@tonic-gate 		(void) fprintf(Wtty_p, gettext("Rename \"%s%s%s\"? "),
1076*0Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
1077*0Sstevel@tonic-gate 		    G_p->g_nam_p : Renam_p,
1078*0Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
1079*0Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
1080*0Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
1081*0Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p);
1082*0Sstevel@tonic-gate 		(void) fflush(Wtty_p);
1083*0Sstevel@tonic-gate 		if (fgets(Renametmp_p, Max_namesz + 1, Rtty_p) == (char *)NULL)
1084*0Sstevel@tonic-gate 			msg(EXT, "Cannot read tty.");
1085*0Sstevel@tonic-gate 		if (feof(Rtty_p))
1086*0Sstevel@tonic-gate 			exit(EXIT_CODE);
1087*0Sstevel@tonic-gate 		lastchar = strlen(Renametmp_p) - 1;
1088*0Sstevel@tonic-gate 
1089*0Sstevel@tonic-gate 		/* remove trailing '\n' */
1090*0Sstevel@tonic-gate 		if (*(Renametmp_p + lastchar) == '\n')
1091*0Sstevel@tonic-gate 			*(Renametmp_p + lastchar) = '\0';
1092*0Sstevel@tonic-gate 		if (*Renametmp_p == '\0') {
1093*0Sstevel@tonic-gate 			msg(POST, "%s%s%s Skipped.",
1094*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
1095*0Sstevel@tonic-gate 			    G_p->g_nam_p : G_p->g_attrfnam_p,
1096*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
1097*0Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
1098*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
1099*0Sstevel@tonic-gate 			    "" : G_p->g_attrnam_p);
1100*0Sstevel@tonic-gate 			*G_p->g_nam_p = '\0';
1101*0Sstevel@tonic-gate 			return (F_SKIP);
1102*0Sstevel@tonic-gate 		} else if (strcmp(Renametmp_p, ".") != 0) {
1103*0Sstevel@tonic-gate 			len = strlen(Renametmp_p);
1104*0Sstevel@tonic-gate 			if (len > (int)strlen(G_p->g_nam_p)) {
1105*0Sstevel@tonic-gate 				if ((G_p->g_nam_p != &nambuf[0]) &&
1106*0Sstevel@tonic-gate 				    (G_p->g_nam_p != &fullnam[0]))
1107*0Sstevel@tonic-gate 					free(G_p->g_nam_p);
1108*0Sstevel@tonic-gate 				G_p->g_nam_p = e_zalloc(E_EXIT, len + 1);
1109*0Sstevel@tonic-gate 			}
1110*0Sstevel@tonic-gate 			if (G_p->g_attrnam_p != (char *)NULL) {
1111*0Sstevel@tonic-gate 				free(G_p->g_attrnam_p);
1112*0Sstevel@tonic-gate 				G_p->g_attrnam_p = e_strdup(E_EXIT,
1113*0Sstevel@tonic-gate 				    Renametmp_p);
1114*0Sstevel@tonic-gate 				(void) strcpy(G_p->g_nam_p, Renam_p);
1115*0Sstevel@tonic-gate 			} else {
1116*0Sstevel@tonic-gate 				(void) strcpy(Renam_p, Renametmp_p);
1117*0Sstevel@tonic-gate 				(void) strcpy(G_p->g_nam_p, Renametmp_p);
1118*0Sstevel@tonic-gate 			}
1119*0Sstevel@tonic-gate 
1120*0Sstevel@tonic-gate 		}
1121*0Sstevel@tonic-gate 	}
1122*0Sstevel@tonic-gate 	if (flag != 0 || Onecopy == 0) {
1123*0Sstevel@tonic-gate 		VERBOSE((Args & OCt), G_p->g_nam_p);
1124*0Sstevel@tonic-gate 	}
1125*0Sstevel@tonic-gate 	if (Args & OCt)
1126*0Sstevel@tonic-gate 		return (F_SKIP);
1127*0Sstevel@tonic-gate 	return (F_EXTR);
1128*0Sstevel@tonic-gate }
1129*0Sstevel@tonic-gate 
1130*0Sstevel@tonic-gate /*
1131*0Sstevel@tonic-gate  * ckopts: Check the validity of all command line options.
1132*0Sstevel@tonic-gate  */
1133*0Sstevel@tonic-gate 
1134*0Sstevel@tonic-gate static void
1135*0Sstevel@tonic-gate ckopts(long mask)
1136*0Sstevel@tonic-gate {
1137*0Sstevel@tonic-gate 	int oflag;
1138*0Sstevel@tonic-gate 	char *t_p;
1139*0Sstevel@tonic-gate 	long errmsk;
1140*0Sstevel@tonic-gate 
1141*0Sstevel@tonic-gate 	if (mask & OCi) {
1142*0Sstevel@tonic-gate 		errmsk = mask & INV_MSK4i;
1143*0Sstevel@tonic-gate 	} else if (mask & OCo) {
1144*0Sstevel@tonic-gate 		errmsk = mask & INV_MSK4o;
1145*0Sstevel@tonic-gate 	} else if (mask & OCp) {
1146*0Sstevel@tonic-gate 		errmsk = mask & INV_MSK4p;
1147*0Sstevel@tonic-gate 	} else {
1148*0Sstevel@tonic-gate 		msg(ERR, "One of -i, -o or -p must be specified.");
1149*0Sstevel@tonic-gate 		errmsk = 0;
1150*0Sstevel@tonic-gate 	}
1151*0Sstevel@tonic-gate 
1152*0Sstevel@tonic-gate 	if (errmsk) {
1153*0Sstevel@tonic-gate 		/* if non-zero, invalid options were specified */
1154*0Sstevel@tonic-gate 		Error_cnt++;
1155*0Sstevel@tonic-gate 	}
1156*0Sstevel@tonic-gate 
1157*0Sstevel@tonic-gate 	if ((mask & OCa) && (mask & OCm) && ((mask & OCi) ||
1158*0Sstevel@tonic-gate 		(mask & OCo))) {
1159*0Sstevel@tonic-gate 		msg(ERR, "-a and -m are mutually exclusive.");
1160*0Sstevel@tonic-gate 	}
1161*0Sstevel@tonic-gate 
1162*0Sstevel@tonic-gate 	if ((mask & OCc) && (mask & OCH) && (strcmp("odc", Hdr_p))) {
1163*0Sstevel@tonic-gate 		msg(ERR, "-c and -H are mutually exclusive.");
1164*0Sstevel@tonic-gate 	}
1165*0Sstevel@tonic-gate 
1166*0Sstevel@tonic-gate 	if ((mask & OCv) && (mask & OCV)) {
1167*0Sstevel@tonic-gate 		msg(ERR, "-v and -V are mutually exclusive.");
1168*0Sstevel@tonic-gate 	}
1169*0Sstevel@tonic-gate 
1170*0Sstevel@tonic-gate 	if ((mask & OCt) && (mask & OCV)) {
1171*0Sstevel@tonic-gate 		msg(ERR, "-t and -V are mutually exclusive.");
1172*0Sstevel@tonic-gate 	}
1173*0Sstevel@tonic-gate 
1174*0Sstevel@tonic-gate 	if ((mask & OCB) && (mask & OCC)) {
1175*0Sstevel@tonic-gate 		msg(ERR, "-B and -C are mutually exclusive.");
1176*0Sstevel@tonic-gate 	}
1177*0Sstevel@tonic-gate 
1178*0Sstevel@tonic-gate 	if ((mask & OCH) && (mask & OC6)) {
1179*0Sstevel@tonic-gate 		msg(ERR, "-H and -6 are mutually exclusive.");
1180*0Sstevel@tonic-gate 	}
1181*0Sstevel@tonic-gate 
1182*0Sstevel@tonic-gate 	if ((mask & OCM) && !((mask & OCI) || (mask & OCO))) {
1183*0Sstevel@tonic-gate 		msg(ERR, "-M not meaningful without -O or -I.");
1184*0Sstevel@tonic-gate 	}
1185*0Sstevel@tonic-gate 
1186*0Sstevel@tonic-gate 	if ((mask & OCA) && !(mask & OCO)) {
1187*0Sstevel@tonic-gate 		msg(ERR, "-A requires the -O option.");
1188*0Sstevel@tonic-gate 	}
1189*0Sstevel@tonic-gate 
1190*0Sstevel@tonic-gate 	if (Bufsize <= 0) {
1191*0Sstevel@tonic-gate 		msg(ERR, "Illegal size given for -C option.");
1192*0Sstevel@tonic-gate 	}
1193*0Sstevel@tonic-gate 
1194*0Sstevel@tonic-gate 	if (mask & OCH) {
1195*0Sstevel@tonic-gate 		t_p = Hdr_p;
1196*0Sstevel@tonic-gate 
1197*0Sstevel@tonic-gate 		while (*t_p != NULL) {
1198*0Sstevel@tonic-gate 			if (isupper(*t_p)) {
1199*0Sstevel@tonic-gate 				*t_p = 'a' + (*t_p - 'A');
1200*0Sstevel@tonic-gate 			}
1201*0Sstevel@tonic-gate 
1202*0Sstevel@tonic-gate 			t_p++;
1203*0Sstevel@tonic-gate 		}
1204*0Sstevel@tonic-gate 
1205*0Sstevel@tonic-gate 		if (!(strcmp("odc", Hdr_p))) {
1206*0Sstevel@tonic-gate 			Hdr_type = CHR;
1207*0Sstevel@tonic-gate 			Max_namesz = CPATH;
1208*0Sstevel@tonic-gate 			Onecopy = 0;
1209*0Sstevel@tonic-gate 			Use_old_stat = 1;
1210*0Sstevel@tonic-gate 		} else if (!(strcmp("crc", Hdr_p))) {
1211*0Sstevel@tonic-gate 			Hdr_type = CRC;
1212*0Sstevel@tonic-gate 			Max_namesz = APATH;
1213*0Sstevel@tonic-gate 			Onecopy = 1;
1214*0Sstevel@tonic-gate 		} else if (!(strcmp("tar", Hdr_p))) {
1215*0Sstevel@tonic-gate 			if (Args & OCo) {
1216*0Sstevel@tonic-gate 				Hdr_type = USTAR;
1217*0Sstevel@tonic-gate 				Max_namesz = HNAMLEN - 1;
1218*0Sstevel@tonic-gate 			} else {
1219*0Sstevel@tonic-gate 				Hdr_type = TAR;
1220*0Sstevel@tonic-gate 				Max_namesz = TNAMLEN - 1;
1221*0Sstevel@tonic-gate 			}
1222*0Sstevel@tonic-gate 			Onecopy = 0;
1223*0Sstevel@tonic-gate 		} else if (!(strcmp("ustar", Hdr_p))) {
1224*0Sstevel@tonic-gate 			Hdr_type = USTAR;
1225*0Sstevel@tonic-gate 			Max_namesz = HNAMLEN - 1;
1226*0Sstevel@tonic-gate 			Onecopy = 0;
1227*0Sstevel@tonic-gate 		} else if (!(strcmp("bar", Hdr_p))) {
1228*0Sstevel@tonic-gate 			if ((Args & OCo) || (Args & OCp)) {
1229*0Sstevel@tonic-gate 				msg(ERR,
1230*0Sstevel@tonic-gate 				    "Header type bar can only be used with -i");
1231*0Sstevel@tonic-gate 			}
1232*0Sstevel@tonic-gate 
1233*0Sstevel@tonic-gate 			if (Args & OCP) {
1234*0Sstevel@tonic-gate 				msg(ERR,
1235*0Sstevel@tonic-gate 				    "Can't preserve using bar header");
1236*0Sstevel@tonic-gate 			}
1237*0Sstevel@tonic-gate 
1238*0Sstevel@tonic-gate 			Hdr_type = BAR;
1239*0Sstevel@tonic-gate 			Max_namesz = TNAMLEN - 1;
1240*0Sstevel@tonic-gate 			Onecopy = 0;
1241*0Sstevel@tonic-gate 		} else {
1242*0Sstevel@tonic-gate 			msg(ERR, "Invalid header \"%s\" specified", Hdr_p);
1243*0Sstevel@tonic-gate 		}
1244*0Sstevel@tonic-gate 	}
1245*0Sstevel@tonic-gate 
1246*0Sstevel@tonic-gate 	if (mask & OCr) {
1247*0Sstevel@tonic-gate 		Rtty_p = fopen(Ttyname, "r");
1248*0Sstevel@tonic-gate 		Wtty_p = fopen(Ttyname, "w");
1249*0Sstevel@tonic-gate 
1250*0Sstevel@tonic-gate 		if (Rtty_p == (FILE *)NULL || Wtty_p == (FILE *)NULL) {
1251*0Sstevel@tonic-gate 			msg(ERR, "Cannot rename, \"%s\" missing", Ttyname);
1252*0Sstevel@tonic-gate 		}
1253*0Sstevel@tonic-gate 	}
1254*0Sstevel@tonic-gate 
1255*0Sstevel@tonic-gate 	if ((mask & OCE) && (Ef_p = fopen(Efil_p, "r")) == (FILE *)NULL) {
1256*0Sstevel@tonic-gate 		msg(ERR, "Cannot open \"%s\" to read patterns", Efil_p);
1257*0Sstevel@tonic-gate 	}
1258*0Sstevel@tonic-gate 
1259*0Sstevel@tonic-gate 	if ((mask & OCI) && (Archive = open(IOfil_p, O_RDONLY)) < 0) {
1260*0Sstevel@tonic-gate 		msg(ERR, "Cannot open \"%s\" for input", IOfil_p);
1261*0Sstevel@tonic-gate 	}
1262*0Sstevel@tonic-gate 
1263*0Sstevel@tonic-gate 	if (mask & OCO) {
1264*0Sstevel@tonic-gate 		if (mask & OCA) {
1265*0Sstevel@tonic-gate 			if ((Archive = open(IOfil_p, O_RDWR)) < 0) {
1266*0Sstevel@tonic-gate 				msg(ERR,
1267*0Sstevel@tonic-gate 				    "Cannot open \"%s\" for append",
1268*0Sstevel@tonic-gate 				    IOfil_p);
1269*0Sstevel@tonic-gate 			}
1270*0Sstevel@tonic-gate 		} else {
1271*0Sstevel@tonic-gate 			oflag = (O_WRONLY | O_CREAT | O_TRUNC);
1272*0Sstevel@tonic-gate 
1273*0Sstevel@tonic-gate 			if ((Archive = open(IOfil_p, oflag, 0777)) < 0) {
1274*0Sstevel@tonic-gate 				msg(ERR,
1275*0Sstevel@tonic-gate 				    "Cannot open \"%s\" for output",
1276*0Sstevel@tonic-gate 				    IOfil_p);
1277*0Sstevel@tonic-gate 			}
1278*0Sstevel@tonic-gate 		}
1279*0Sstevel@tonic-gate 	}
1280*0Sstevel@tonic-gate 
1281*0Sstevel@tonic-gate 	if (mask & OCR) {
1282*0Sstevel@tonic-gate 		if ((Rpw_p = getpwnam(Own_p)) == (struct passwd *)NULL) {
1283*0Sstevel@tonic-gate 			msg(ERR, "\"%s\" is not a valid user id", Own_p);
1284*0Sstevel@tonic-gate 		}
1285*0Sstevel@tonic-gate 	}
1286*0Sstevel@tonic-gate 
1287*0Sstevel@tonic-gate 	if ((mask & OCo) && !(mask & OCO)) {
1288*0Sstevel@tonic-gate 		Out_p = stderr;
1289*0Sstevel@tonic-gate 	}
1290*0Sstevel@tonic-gate 
1291*0Sstevel@tonic-gate 	if ((mask & OCp) && ((mask & (OCB|OCC)) == 0)) {
1292*0Sstevel@tonic-gate 		/*
1293*0Sstevel@tonic-gate 		 * We are in pass mode with no block size specified.  Use the
1294*0Sstevel@tonic-gate 		 * larger of the native page size and 8192.
1295*0Sstevel@tonic-gate 		 */
1296*0Sstevel@tonic-gate 
1297*0Sstevel@tonic-gate 		Bufsize = (PageSize > 8192) ? PageSize : 8192;
1298*0Sstevel@tonic-gate 	}
1299*0Sstevel@tonic-gate }
1300*0Sstevel@tonic-gate 
1301*0Sstevel@tonic-gate /*
1302*0Sstevel@tonic-gate  * cksum: Calculate the simple checksum of a file (CRC) or header
1303*0Sstevel@tonic-gate  * (TARTYP (TAR and USTAR)).  For -o and the CRC header, the file is opened and
1304*0Sstevel@tonic-gate  * the checksum is calculated.  For -i and the CRC header, the checksum
1305*0Sstevel@tonic-gate  * is calculated as each block is transferred from the archive I/O buffer
1306*0Sstevel@tonic-gate  * to the file system I/O buffer.  The TARTYP (TAR and USTAR) headers calculate
1307*0Sstevel@tonic-gate  * the simple checksum of the header (with the checksum field of the
1308*0Sstevel@tonic-gate  * header initialized to all spaces (\040).
1309*0Sstevel@tonic-gate  */
1310*0Sstevel@tonic-gate 
1311*0Sstevel@tonic-gate static long
1312*0Sstevel@tonic-gate cksum(char hdr, int byt_cnt, int *err)
1313*0Sstevel@tonic-gate {
1314*0Sstevel@tonic-gate 	char *crc_p, *end_p;
1315*0Sstevel@tonic-gate 	int cnt;
1316*0Sstevel@tonic-gate 	long checksum = 0L, have;
1317*0Sstevel@tonic-gate 	off_t lcnt;
1318*0Sstevel@tonic-gate 
1319*0Sstevel@tonic-gate 	if (err != NULL)
1320*0Sstevel@tonic-gate 		*err = 0;
1321*0Sstevel@tonic-gate 	switch (hdr) {
1322*0Sstevel@tonic-gate 	case CRC:
1323*0Sstevel@tonic-gate 		if (Args & OCi) { /* do running checksum */
1324*0Sstevel@tonic-gate 			end_p = Buffr.b_out_p + byt_cnt;
1325*0Sstevel@tonic-gate 			for (crc_p = Buffr.b_out_p; crc_p < end_p; crc_p++)
1326*0Sstevel@tonic-gate 				checksum += (long)*crc_p;
1327*0Sstevel@tonic-gate 			break;
1328*0Sstevel@tonic-gate 		}
1329*0Sstevel@tonic-gate 		/* OCo - do checksum of file */
1330*0Sstevel@tonic-gate 		lcnt = G_p->g_filesz;
1331*0Sstevel@tonic-gate 
1332*0Sstevel@tonic-gate 		while (lcnt > 0) {
1333*0Sstevel@tonic-gate 			have = (lcnt < Bufsize) ? lcnt : Bufsize;
1334*0Sstevel@tonic-gate 			errno = 0;
1335*0Sstevel@tonic-gate 			if (read(Ifile, Buf_p, have) != have) {
1336*0Sstevel@tonic-gate 				msg(ERR, "Error computing checksum.");
1337*0Sstevel@tonic-gate 				if (err != NULL)
1338*0Sstevel@tonic-gate 					*err = 1;
1339*0Sstevel@tonic-gate 				break;
1340*0Sstevel@tonic-gate 			}
1341*0Sstevel@tonic-gate 			end_p = Buf_p + have;
1342*0Sstevel@tonic-gate 			for (crc_p = Buf_p; crc_p < end_p; crc_p++)
1343*0Sstevel@tonic-gate 				checksum += (long)*crc_p;
1344*0Sstevel@tonic-gate 			lcnt -= have;
1345*0Sstevel@tonic-gate 		}
1346*0Sstevel@tonic-gate 		if (lseek(Ifile, (off_t)0, SEEK_ABS) < 0)
1347*0Sstevel@tonic-gate 			msg(ERRN, "Cannot reset file after checksum");
1348*0Sstevel@tonic-gate 		break;
1349*0Sstevel@tonic-gate 	case TARTYP: /* TAR and USTAR */
1350*0Sstevel@tonic-gate 		crc_p = Thdr_p->tbuf.t_cksum;
1351*0Sstevel@tonic-gate 		for (cnt = 0; cnt < TCRCLEN; cnt++) {
1352*0Sstevel@tonic-gate 			*crc_p = '\040';
1353*0Sstevel@tonic-gate 			crc_p++;
1354*0Sstevel@tonic-gate 		}
1355*0Sstevel@tonic-gate 		crc_p = (char *)Thdr_p;
1356*0Sstevel@tonic-gate 		for (cnt = 0; cnt < TARSZ; cnt++) {
1357*0Sstevel@tonic-gate 			/*
1358*0Sstevel@tonic-gate 			 * tar uses unsigned checksum, so we must use unsigned
1359*0Sstevel@tonic-gate 			 * here in order to be able to read tar archives.
1360*0Sstevel@tonic-gate 			 */
1361*0Sstevel@tonic-gate 			checksum += (long)((unsigned char)(*crc_p));
1362*0Sstevel@tonic-gate 			crc_p++;
1363*0Sstevel@tonic-gate 		}
1364*0Sstevel@tonic-gate 		break;
1365*0Sstevel@tonic-gate 	default:
1366*0Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
1367*0Sstevel@tonic-gate 	} /* hdr */
1368*0Sstevel@tonic-gate 	return (checksum);
1369*0Sstevel@tonic-gate }
1370*0Sstevel@tonic-gate 
1371*0Sstevel@tonic-gate /*
1372*0Sstevel@tonic-gate  * creat_hdr: Fill in the generic header structure with the specific
1373*0Sstevel@tonic-gate  *            header information based on the value of Hdr_type.
1374*0Sstevel@tonic-gate  *
1375*0Sstevel@tonic-gate  *            return (1) if this process was successful, and (0) otherwise.
1376*0Sstevel@tonic-gate  */
1377*0Sstevel@tonic-gate 
1378*0Sstevel@tonic-gate static int
1379*0Sstevel@tonic-gate creat_hdr(void)
1380*0Sstevel@tonic-gate {
1381*0Sstevel@tonic-gate 	ushort_t ftype;
1382*0Sstevel@tonic-gate 	int fullnamesize;
1383*0Sstevel@tonic-gate 	dev_t dev;
1384*0Sstevel@tonic-gate 	ino_t ino;
1385*0Sstevel@tonic-gate 
1386*0Sstevel@tonic-gate 	ftype = SrcSt.st_mode & Ftype;
1387*0Sstevel@tonic-gate 	Adir = (ftype == S_IFDIR);
1388*0Sstevel@tonic-gate 	Aspec = (ftype == S_IFBLK || ftype == S_IFCHR || ftype == S_IFIFO);
1389*0Sstevel@tonic-gate 	switch (Hdr_type) {
1390*0Sstevel@tonic-gate 		case BIN:
1391*0Sstevel@tonic-gate 			Gen.g_magic = CMN_BIN;
1392*0Sstevel@tonic-gate 			break;
1393*0Sstevel@tonic-gate 		case CHR:
1394*0Sstevel@tonic-gate 			Gen.g_magic = CMN_BIN;
1395*0Sstevel@tonic-gate 			break;
1396*0Sstevel@tonic-gate 		case ASC:
1397*0Sstevel@tonic-gate 			Gen.g_magic = CMN_ASC;
1398*0Sstevel@tonic-gate 			break;
1399*0Sstevel@tonic-gate 		case CRC:
1400*0Sstevel@tonic-gate 			Gen.g_magic = CMN_CRC;
1401*0Sstevel@tonic-gate 			break;
1402*0Sstevel@tonic-gate 		case USTAR:
1403*0Sstevel@tonic-gate 			/*
1404*0Sstevel@tonic-gate 			 * If the length of the full name is greater than 256,
1405*0Sstevel@tonic-gate 			 * print out a message and return.
1406*0Sstevel@tonic-gate 			 */
1407*0Sstevel@tonic-gate 			if ((fullnamesize = strlen(Gen.g_nam_p)) > MAXNAM) {
1408*0Sstevel@tonic-gate 				msg(ERR,
1409*0Sstevel@tonic-gate 				    "%s: file name too long", Gen.g_nam_p);
1410*0Sstevel@tonic-gate 				return (0);
1411*0Sstevel@tonic-gate 			} else if (fullnamesize > NAMSIZ) {
1412*0Sstevel@tonic-gate 				/*
1413*0Sstevel@tonic-gate 				 * The length of the full name is greater than
1414*0Sstevel@tonic-gate 				 * 100, so we must split the filename from the
1415*0Sstevel@tonic-gate 				 * path
1416*0Sstevel@tonic-gate 				 */
1417*0Sstevel@tonic-gate 				char namebuff[NAMSIZ+1];
1418*0Sstevel@tonic-gate 				char prebuff[PRESIZ+1];
1419*0Sstevel@tonic-gate 				char *lastslash;
1420*0Sstevel@tonic-gate 				int presize, namesize;
1421*0Sstevel@tonic-gate 
1422*0Sstevel@tonic-gate 				(void) memset(namebuff, '\0',
1423*0Sstevel@tonic-gate 				    sizeof (namebuff));
1424*0Sstevel@tonic-gate 				(void) memset(prebuff, '\0', sizeof (prebuff));
1425*0Sstevel@tonic-gate 
1426*0Sstevel@tonic-gate 				lastslash = strrchr(Gen.g_nam_p, '/');
1427*0Sstevel@tonic-gate 
1428*0Sstevel@tonic-gate 				if (lastslash != NULL) {
1429*0Sstevel@tonic-gate 					namesize = strlen(++lastslash);
1430*0Sstevel@tonic-gate 					presize = fullnamesize - namesize - 1;
1431*0Sstevel@tonic-gate 				} else {
1432*0Sstevel@tonic-gate 					namesize = fullnamesize;
1433*0Sstevel@tonic-gate 					lastslash = Gen.g_nam_p;
1434*0Sstevel@tonic-gate 					presize = 0;
1435*0Sstevel@tonic-gate 				}
1436*0Sstevel@tonic-gate 
1437*0Sstevel@tonic-gate 				/*
1438*0Sstevel@tonic-gate 				 * If the filename is greater than 100 we can't
1439*0Sstevel@tonic-gate 				 * archive the file
1440*0Sstevel@tonic-gate 				 */
1441*0Sstevel@tonic-gate 				if (namesize > NAMSIZ) {
1442*0Sstevel@tonic-gate 					msg(ERR,
1443*0Sstevel@tonic-gate 					    "%s: filename is greater than %d",
1444*0Sstevel@tonic-gate 					    lastslash, NAMSIZ);
1445*0Sstevel@tonic-gate 					return (0);
1446*0Sstevel@tonic-gate 				}
1447*0Sstevel@tonic-gate 				(void) strncpy(&namebuff[0], lastslash,
1448*0Sstevel@tonic-gate 				    namesize);
1449*0Sstevel@tonic-gate 				/*
1450*0Sstevel@tonic-gate 				 * If the prefix is greater than 155 we can't
1451*0Sstevel@tonic-gate 				 * archive the file.
1452*0Sstevel@tonic-gate 				 */
1453*0Sstevel@tonic-gate 				if (presize > PRESIZ) {
1454*0Sstevel@tonic-gate 					msg(ERR,
1455*0Sstevel@tonic-gate 					    "%s: prefix is greater than %d",
1456*0Sstevel@tonic-gate 					    Gen.g_nam_p, PRESIZ);
1457*0Sstevel@tonic-gate 					return (0);
1458*0Sstevel@tonic-gate 				}
1459*0Sstevel@tonic-gate 				(void) strncpy(&prebuff[0], Gen.g_nam_p,
1460*0Sstevel@tonic-gate 				    presize);
1461*0Sstevel@tonic-gate 
1462*0Sstevel@tonic-gate 				Gen.g_tname = e_zalloc(E_EXIT, namesize + 1);
1463*0Sstevel@tonic-gate 				(void) strcpy(Gen.g_tname, namebuff);
1464*0Sstevel@tonic-gate 
1465*0Sstevel@tonic-gate 				Gen.g_prefix = e_zalloc(E_EXIT, presize + 1);
1466*0Sstevel@tonic-gate 				(void) strcpy(Gen.g_prefix, prebuff);
1467*0Sstevel@tonic-gate 			} else {
1468*0Sstevel@tonic-gate 				Gen.g_tname = Gen.g_nam_p;
1469*0Sstevel@tonic-gate 			}
1470*0Sstevel@tonic-gate 			(void) strcpy(Gen.g_tmagic, "ustar");
1471*0Sstevel@tonic-gate 			(void) strcpy(Gen.g_version, "00");
1472*0Sstevel@tonic-gate 
1473*0Sstevel@tonic-gate 			dpasswd = getpwuid(SrcSt.st_uid);
1474*0Sstevel@tonic-gate 			if (dpasswd == (struct passwd *)NULL) {
1475*0Sstevel@tonic-gate 				msg(EPOST,
1476*0Sstevel@tonic-gate 				    "cpio: could not get passwd information "
1477*0Sstevel@tonic-gate 				    "for %s%s%s",
1478*0Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
1479*0Sstevel@tonic-gate 				    Gen.g_nam_p : Gen.g_attrfnam_p,
1480*0Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
1481*0Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
1482*0Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
1483*0Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
1484*0Sstevel@tonic-gate 				/* make name null string */
1485*0Sstevel@tonic-gate 				Gen.g_uname[0] = '\0';
1486*0Sstevel@tonic-gate 			} else {
1487*0Sstevel@tonic-gate 				(void) strncpy(&Gen.g_uname[0],
1488*0Sstevel@tonic-gate 				    dpasswd->pw_name, 32);
1489*0Sstevel@tonic-gate 			}
1490*0Sstevel@tonic-gate 			dgroup = getgrgid(SrcSt.st_gid);
1491*0Sstevel@tonic-gate 			if (dgroup == (struct group *)NULL) {
1492*0Sstevel@tonic-gate 				msg(EPOST,
1493*0Sstevel@tonic-gate 				    "cpio: could not get group information "
1494*0Sstevel@tonic-gate 				    "for %s%s%S",
1495*0Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
1496*0Sstevel@tonic-gate 				    Gen.g_nam_p : Gen.g_attrfnam_p,
1497*0Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
1498*0Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
1499*0Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
1500*0Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
1501*0Sstevel@tonic-gate 				/* make name null string */
1502*0Sstevel@tonic-gate 				Gen.g_gname[0] = '\0';
1503*0Sstevel@tonic-gate 			} else {
1504*0Sstevel@tonic-gate 				(void) strncpy(&Gen.g_gname[0],
1505*0Sstevel@tonic-gate 				    dgroup->gr_name, 32);
1506*0Sstevel@tonic-gate 			}
1507*0Sstevel@tonic-gate 			Gen.g_typeflag = tartype(ftype);
1508*0Sstevel@tonic-gate 			/* FALLTHROUGH */
1509*0Sstevel@tonic-gate 		case TAR:
1510*0Sstevel@tonic-gate 			(void) memset(T_lname, '\0', sizeof (T_lname));
1511*0Sstevel@tonic-gate 			break;
1512*0Sstevel@tonic-gate 		default:
1513*0Sstevel@tonic-gate 			msg(EXT, "Impossible header type.");
1514*0Sstevel@tonic-gate 	}
1515*0Sstevel@tonic-gate 
1516*0Sstevel@tonic-gate 	dev = SrcSt.st_dev;
1517*0Sstevel@tonic-gate 	ino = SrcSt.st_ino;
1518*0Sstevel@tonic-gate 
1519*0Sstevel@tonic-gate 	if (Use_old_stat) {
1520*0Sstevel@tonic-gate 		SrcSt = *OldSt;
1521*0Sstevel@tonic-gate 	}
1522*0Sstevel@tonic-gate 
1523*0Sstevel@tonic-gate 	Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
1524*0Sstevel@tonic-gate 	Gen.g_uid = SrcSt.st_uid;
1525*0Sstevel@tonic-gate 	Gen.g_gid = SrcSt.st_gid;
1526*0Sstevel@tonic-gate 	Gen.g_dev = SrcSt.st_dev;
1527*0Sstevel@tonic-gate 
1528*0Sstevel@tonic-gate 	if (Use_old_stat) {
1529*0Sstevel@tonic-gate 		/* -Hodc */
1530*0Sstevel@tonic-gate 
1531*0Sstevel@tonic-gate 		sl_info_t *p = sl_search(dev, ino);
1532*0Sstevel@tonic-gate 		Gen.g_ino = p ? p->sl_ino2 : -1;
1533*0Sstevel@tonic-gate 
1534*0Sstevel@tonic-gate 		if (Gen.g_ino == (ulong_t)-1) {
1535*0Sstevel@tonic-gate 			msg(ERR, "%s%s%s: cannot be archived - inode too big "
1536*0Sstevel@tonic-gate 			    "for -Hodc format",
1537*0Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
1538*0Sstevel@tonic-gate 			    Gen.g_nam_p : Gen.g_attrfnam_p,
1539*0Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
1540*0Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
1541*0Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
1542*0Sstevel@tonic-gate 			    "" : Gen.g_attrnam_p);
1543*0Sstevel@tonic-gate 			return (0);
1544*0Sstevel@tonic-gate 		}
1545*0Sstevel@tonic-gate 	} else {
1546*0Sstevel@tonic-gate 		Gen.g_ino = SrcSt.st_ino;
1547*0Sstevel@tonic-gate 	}
1548*0Sstevel@tonic-gate 
1549*0Sstevel@tonic-gate 	Gen.g_mode = SrcSt.st_mode;
1550*0Sstevel@tonic-gate 	Gen.g_mtime = SrcSt.st_mtime;
1551*0Sstevel@tonic-gate 	Gen.g_nlink = (Adir) ? SrcSt.st_nlink
1552*0Sstevel@tonic-gate 	    : sl_numlinks(dev, ino);
1553*0Sstevel@tonic-gate 
1554*0Sstevel@tonic-gate 	if (ftype == S_IFREG || ftype == S_IFLNK)
1555*0Sstevel@tonic-gate 		Gen.g_filesz = (off_t)SrcSt.st_size;
1556*0Sstevel@tonic-gate 	else
1557*0Sstevel@tonic-gate 		Gen.g_filesz = (off_t)0;
1558*0Sstevel@tonic-gate 	Gen.g_rdev = SrcSt.st_rdev;
1559*0Sstevel@tonic-gate 	return (1);
1560*0Sstevel@tonic-gate }
1561*0Sstevel@tonic-gate 
1562*0Sstevel@tonic-gate /*
1563*0Sstevel@tonic-gate  * creat_lnk: Create a link from the existing name1_p to name2_p.
1564*0Sstevel@tonic-gate  */
1565*0Sstevel@tonic-gate 
1566*0Sstevel@tonic-gate static
1567*0Sstevel@tonic-gate int
1568*0Sstevel@tonic-gate creat_lnk(int dirfd, char *name1_p, char *name2_p)
1569*0Sstevel@tonic-gate {
1570*0Sstevel@tonic-gate 	int cnt = 0;
1571*0Sstevel@tonic-gate 
1572*0Sstevel@tonic-gate 	do {
1573*0Sstevel@tonic-gate 		errno = 0;
1574*0Sstevel@tonic-gate 		if (!link(name1_p, name2_p)) {
1575*0Sstevel@tonic-gate 			if (aclp != NULL) {
1576*0Sstevel@tonic-gate 				free(aclp);
1577*0Sstevel@tonic-gate 				aclp = NULL;
1578*0Sstevel@tonic-gate 				acl_set = 0;
1579*0Sstevel@tonic-gate 			}
1580*0Sstevel@tonic-gate 			cnt = 0;
1581*0Sstevel@tonic-gate 			break;
1582*0Sstevel@tonic-gate 		} else if (errno == EEXIST) {
1583*0Sstevel@tonic-gate 			if (cnt == 0) {
1584*0Sstevel@tonic-gate 				struct stat lsb1;
1585*0Sstevel@tonic-gate 				struct stat lsb2;
1586*0Sstevel@tonic-gate 
1587*0Sstevel@tonic-gate 				/*
1588*0Sstevel@tonic-gate 				 * Check to see if we are trying to link this
1589*0Sstevel@tonic-gate 				 * file to itself.  If so, count the effort as
1590*0Sstevel@tonic-gate 				 * successful.  If the two files are different,
1591*0Sstevel@tonic-gate 				 * or if either lstat is unsuccessful, proceed
1592*0Sstevel@tonic-gate 				 * as we would have otherwise; the appropriate
1593*0Sstevel@tonic-gate 				 * error will be reported subsequently.
1594*0Sstevel@tonic-gate 				 */
1595*0Sstevel@tonic-gate 
1596*0Sstevel@tonic-gate 				if (lstat(name1_p, &lsb1) != 0) {
1597*0Sstevel@tonic-gate 					msg(ERR, "Cannot lstat source file %s",
1598*0Sstevel@tonic-gate 					    name1_p);
1599*0Sstevel@tonic-gate 				} else {
1600*0Sstevel@tonic-gate 					if (lstat(name2_p, &lsb2) != 0) {
1601*0Sstevel@tonic-gate 						msg(ERR, "Cannot lstat "
1602*0Sstevel@tonic-gate 						    "destination file %s",
1603*0Sstevel@tonic-gate 						    name2_p);
1604*0Sstevel@tonic-gate 					} else {
1605*0Sstevel@tonic-gate 						if (lsb1.st_dev ==
1606*0Sstevel@tonic-gate 						    lsb2.st_dev &&
1607*0Sstevel@tonic-gate 						    lsb1.st_ino ==
1608*0Sstevel@tonic-gate 						    lsb2.st_ino) {
1609*0Sstevel@tonic-gate 							VERBOSE((Args &
1610*0Sstevel@tonic-gate 							    (OCv | OCV)),
1611*0Sstevel@tonic-gate 							    name2_p);
1612*0Sstevel@tonic-gate 							return (0);
1613*0Sstevel@tonic-gate 						}
1614*0Sstevel@tonic-gate 					}
1615*0Sstevel@tonic-gate 				}
1616*0Sstevel@tonic-gate 			}
1617*0Sstevel@tonic-gate 
1618*0Sstevel@tonic-gate 			if (!(Args & OCu) && G_p->g_mtime <= DesSt.st_mtime)
1619*0Sstevel@tonic-gate 				msg(ERR,
1620*0Sstevel@tonic-gate 				    "Existing \"%s\" same age or newer",
1621*0Sstevel@tonic-gate 				    name2_p);
1622*0Sstevel@tonic-gate 			else if (unlinkat(dirfd, get_component(name2_p), 0) < 0)
1623*0Sstevel@tonic-gate 				msg(ERRN, "Error cannot unlink \"%s\"",
1624*0Sstevel@tonic-gate 				    name2_p);
1625*0Sstevel@tonic-gate 		}
1626*0Sstevel@tonic-gate 		cnt++;
1627*0Sstevel@tonic-gate 	} while ((cnt < 2) && missdir(name2_p) == 0);
1628*0Sstevel@tonic-gate 	if (!cnt) {
1629*0Sstevel@tonic-gate 		char *newname;
1630*0Sstevel@tonic-gate 		char *fromname;
1631*0Sstevel@tonic-gate 		char *attrname;
1632*0Sstevel@tonic-gate 
1633*0Sstevel@tonic-gate 		newname = name2_p;
1634*0Sstevel@tonic-gate 		fromname = name1_p;
1635*0Sstevel@tonic-gate 		attrname = Gen.g_attrnam_p;
1636*0Sstevel@tonic-gate 		if (attrname) {
1637*0Sstevel@tonic-gate 			if (Args & OCp) {
1638*0Sstevel@tonic-gate 				newname = fromname = Fullnam_p;
1639*0Sstevel@tonic-gate 			} else {
1640*0Sstevel@tonic-gate 				newname = Gen.g_attrfnam_p;
1641*0Sstevel@tonic-gate 			}
1642*0Sstevel@tonic-gate 		}
1643*0Sstevel@tonic-gate 		if (Args & OCv) {
1644*0Sstevel@tonic-gate 			(void) fprintf(Err_p,
1645*0Sstevel@tonic-gate 			    gettext("%s%s%s linked to %s%s%s\n"), newname,
1646*0Sstevel@tonic-gate 			    (attrname == (char *)NULL) ?
1647*0Sstevel@tonic-gate 			    "" : gettext(" attribute "),
1648*0Sstevel@tonic-gate 			    (attrname == (char *)NULL) ?
1649*0Sstevel@tonic-gate 			    "" : attrname, fromname,
1650*0Sstevel@tonic-gate 			    (attrname == (char *)NULL) ?
1651*0Sstevel@tonic-gate 			    "" : gettext(" attribute "),
1652*0Sstevel@tonic-gate 			    (attrname == (char *)NULL) ?
1653*0Sstevel@tonic-gate 			    "" : name1_p);
1654*0Sstevel@tonic-gate 		}
1655*0Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), newname);
1656*0Sstevel@tonic-gate 	} else if (cnt == 1)
1657*0Sstevel@tonic-gate 		msg(ERRN,
1658*0Sstevel@tonic-gate 		    "Unable to create directory for \"%s\"", name2_p);
1659*0Sstevel@tonic-gate 	else if (cnt == 2)
1660*0Sstevel@tonic-gate 		msg(ERRN,
1661*0Sstevel@tonic-gate 		    "Cannot link \"%s\" and \"%s\"", name1_p, name2_p);
1662*0Sstevel@tonic-gate 	return (cnt);
1663*0Sstevel@tonic-gate }
1664*0Sstevel@tonic-gate 
1665*0Sstevel@tonic-gate /*
1666*0Sstevel@tonic-gate  * creat_spec:
1667*0Sstevel@tonic-gate  *   Create one of the following:
1668*0Sstevel@tonic-gate  *       directory
1669*0Sstevel@tonic-gate  *       character special file
1670*0Sstevel@tonic-gate  *       block special file
1671*0Sstevel@tonic-gate  *       fifo
1672*0Sstevel@tonic-gate  */
1673*0Sstevel@tonic-gate 
1674*0Sstevel@tonic-gate static int
1675*0Sstevel@tonic-gate creat_spec(int dirfd)
1676*0Sstevel@tonic-gate {
1677*0Sstevel@tonic-gate 	char *nam_p;
1678*0Sstevel@tonic-gate 	int cnt, result, rv = 0;
1679*0Sstevel@tonic-gate 	char *curdir;
1680*0Sstevel@tonic-gate 	char *lastslash;
1681*0Sstevel@tonic-gate 
1682*0Sstevel@tonic-gate 	Do_rename = 0;	/* creat_tmp() may reset this */
1683*0Sstevel@tonic-gate 
1684*0Sstevel@tonic-gate 	if (Args & OCp) {
1685*0Sstevel@tonic-gate 		nam_p = Fullnam_p;
1686*0Sstevel@tonic-gate 	} else {
1687*0Sstevel@tonic-gate 		nam_p = G_p->g_nam_p;
1688*0Sstevel@tonic-gate 	}
1689*0Sstevel@tonic-gate 
1690*0Sstevel@tonic-gate 	/*
1691*0Sstevel@tonic-gate 	 * Is this the extraction of the hidden attribute directory?
1692*0Sstevel@tonic-gate 	 * If so then just set the mode/times correctly, and return.
1693*0Sstevel@tonic-gate 	 */
1694*0Sstevel@tonic-gate 
1695*0Sstevel@tonic-gate 	if (Hiddendir) {
1696*0Sstevel@tonic-gate 		if (fchownat(dirfd, ".", G_p->g_uid, G_p->g_gid, 0) != 0) {
1697*0Sstevel@tonic-gate 			msg(ERRN,
1698*0Sstevel@tonic-gate 			    "Cannot chown() \"attribute directory of "
1699*0Sstevel@tonic-gate 			    "file %s\"", G_p->g_attrfnam_p);
1700*0Sstevel@tonic-gate 		}
1701*0Sstevel@tonic-gate 
1702*0Sstevel@tonic-gate 		if (fchmod(dirfd, G_p->g_mode) != 0) {
1703*0Sstevel@tonic-gate 			msg(ERRN,
1704*0Sstevel@tonic-gate 			    "Cannot chmod() \"attribute directory of "
1705*0Sstevel@tonic-gate 			    "file %s\"", G_p->g_attrfnam_p);
1706*0Sstevel@tonic-gate 		}
1707*0Sstevel@tonic-gate 
1708*0Sstevel@tonic-gate 		acl_set = 0;
1709*0Sstevel@tonic-gate 		if (Pflag && aclp != NULL) {
1710*0Sstevel@tonic-gate 			if (facl(dirfd, SETACL, aclcnt, aclp) < 0) {
1711*0Sstevel@tonic-gate 				msg(ERRN,
1712*0Sstevel@tonic-gate 				    "failed to set acl on attribute"
1713*0Sstevel@tonic-gate 				    " directory of %s ", G_p->g_attrfnam_p);
1714*0Sstevel@tonic-gate 			} else {
1715*0Sstevel@tonic-gate 				acl_set = 1;
1716*0Sstevel@tonic-gate 			}
1717*0Sstevel@tonic-gate 			free(aclp);
1718*0Sstevel@tonic-gate 			aclp = NULL;
1719*0Sstevel@tonic-gate 		}
1720*0Sstevel@tonic-gate 
1721*0Sstevel@tonic-gate 		return (1);
1722*0Sstevel@tonic-gate 	}
1723*0Sstevel@tonic-gate 
1724*0Sstevel@tonic-gate 	result = stat(nam_p, &DesSt);
1725*0Sstevel@tonic-gate 
1726*0Sstevel@tonic-gate 	if (ustar_dir() || Adir) {
1727*0Sstevel@tonic-gate 		/*
1728*0Sstevel@tonic-gate 		 *  The archive file is a directory.
1729*0Sstevel@tonic-gate 		 *  Skip "." and ".."
1730*0Sstevel@tonic-gate 		 */
1731*0Sstevel@tonic-gate 
1732*0Sstevel@tonic-gate 		curdir = strrchr(nam_p, '.');
1733*0Sstevel@tonic-gate 
1734*0Sstevel@tonic-gate 		if (curdir != NULL && curdir[1] == NULL) {
1735*0Sstevel@tonic-gate 			lastslash = strrchr(nam_p, '/');
1736*0Sstevel@tonic-gate 
1737*0Sstevel@tonic-gate 			if (lastslash != NULL) {
1738*0Sstevel@tonic-gate 				lastslash++;
1739*0Sstevel@tonic-gate 			} else {
1740*0Sstevel@tonic-gate 				lastslash = nam_p;
1741*0Sstevel@tonic-gate 			}
1742*0Sstevel@tonic-gate 
1743*0Sstevel@tonic-gate 			if (!(strcmp(lastslash, ".")) ||
1744*0Sstevel@tonic-gate 			    !(strcmp(lastslash, ".."))) {
1745*0Sstevel@tonic-gate 				return (1);
1746*0Sstevel@tonic-gate 			}
1747*0Sstevel@tonic-gate 		}
1748*0Sstevel@tonic-gate 
1749*0Sstevel@tonic-gate 		if (result == 0) {
1750*0Sstevel@tonic-gate 			/* A file by the same name exists. */
1751*0Sstevel@tonic-gate 
1752*0Sstevel@tonic-gate 			/* Take care of ACLs */
1753*0Sstevel@tonic-gate 			acl_set = 0;
1754*0Sstevel@tonic-gate 
1755*0Sstevel@tonic-gate 			if (Pflag && aclp != NULL) {
1756*0Sstevel@tonic-gate 				if (acl(nam_p, SETACL, aclcnt, aclp) < 0) {
1757*0Sstevel@tonic-gate 					msg(ERRN,
1758*0Sstevel@tonic-gate 					    "\"%s\": failed to set acl",
1759*0Sstevel@tonic-gate 					    nam_p);
1760*0Sstevel@tonic-gate 				} else {
1761*0Sstevel@tonic-gate 					acl_set = 1;
1762*0Sstevel@tonic-gate 				}
1763*0Sstevel@tonic-gate 
1764*0Sstevel@tonic-gate 				free(aclp);
1765*0Sstevel@tonic-gate 				aclp = NULL;
1766*0Sstevel@tonic-gate 			}
1767*0Sstevel@tonic-gate 			if (Args & OCd) {
1768*0Sstevel@tonic-gate 				/*
1769*0Sstevel@tonic-gate 				 * We are creating directories.  Keep the
1770*0Sstevel@tonic-gate 				 * existing file.
1771*0Sstevel@tonic-gate 				 */
1772*0Sstevel@tonic-gate 
1773*0Sstevel@tonic-gate 				rstfiles(U_KEEP, dirfd);
1774*0Sstevel@tonic-gate 			}
1775*0Sstevel@tonic-gate 
1776*0Sstevel@tonic-gate 			/* Report success. */
1777*0Sstevel@tonic-gate 
1778*0Sstevel@tonic-gate 			return (1);
1779*0Sstevel@tonic-gate 		}
1780*0Sstevel@tonic-gate 	} else {
1781*0Sstevel@tonic-gate 		/* The archive file is not a directory. */
1782*0Sstevel@tonic-gate 
1783*0Sstevel@tonic-gate 		if (result == 0) {
1784*0Sstevel@tonic-gate 			/*
1785*0Sstevel@tonic-gate 			 * A file by the same name exists.  Move it to a
1786*0Sstevel@tonic-gate 			 * temporary file.
1787*0Sstevel@tonic-gate 			 */
1788*0Sstevel@tonic-gate 
1789*0Sstevel@tonic-gate 			if (creat_tmp(nam_p) < 0) {
1790*0Sstevel@tonic-gate 				/*
1791*0Sstevel@tonic-gate 				 * We weren't able to create the temp file.
1792*0Sstevel@tonic-gate 				 * Report failure.
1793*0Sstevel@tonic-gate 				 */
1794*0Sstevel@tonic-gate 
1795*0Sstevel@tonic-gate 				return (0);
1796*0Sstevel@tonic-gate 			}
1797*0Sstevel@tonic-gate 		}
1798*0Sstevel@tonic-gate 	}
1799*0Sstevel@tonic-gate 
1800*0Sstevel@tonic-gate 	/*
1801*0Sstevel@tonic-gate 	 * This pile tries to create the file directly, and, if there is a
1802*0Sstevel@tonic-gate 	 * problem, creates missing directories, and then tries to create the
1803*0Sstevel@tonic-gate 	 * file again.  Two strikes and you're out.
1804*0Sstevel@tonic-gate 	 */
1805*0Sstevel@tonic-gate 
1806*0Sstevel@tonic-gate 	cnt = 0;
1807*0Sstevel@tonic-gate 
1808*0Sstevel@tonic-gate 	do {
1809*0Sstevel@tonic-gate 		if (ustar_dir() || Adir) {
1810*0Sstevel@tonic-gate 			/* The archive file is a directory. */
1811*0Sstevel@tonic-gate 
1812*0Sstevel@tonic-gate 			result = mkdir(nam_p, G_p->g_mode);
1813*0Sstevel@tonic-gate 		} else if (ustar_spec() || Aspec) {
1814*0Sstevel@tonic-gate 			/*
1815*0Sstevel@tonic-gate 			 * The archive file is block special,
1816*0Sstevel@tonic-gate 			 * char special or a fifo.
1817*0Sstevel@tonic-gate 			 */
1818*0Sstevel@tonic-gate 
1819*0Sstevel@tonic-gate 			result = mknod(nam_p, (int)G_p->g_mode,
1820*0Sstevel@tonic-gate 			    (int)G_p->g_rdev);
1821*0Sstevel@tonic-gate 		}
1822*0Sstevel@tonic-gate 
1823*0Sstevel@tonic-gate 		if (result >= 0) {
1824*0Sstevel@tonic-gate 			/*
1825*0Sstevel@tonic-gate 			 * The file creation succeeded.  Take care of the ACLs.
1826*0Sstevel@tonic-gate 			 */
1827*0Sstevel@tonic-gate 
1828*0Sstevel@tonic-gate 			acl_set = 0;
1829*0Sstevel@tonic-gate 
1830*0Sstevel@tonic-gate 			if (Pflag && aclp != NULL) {
1831*0Sstevel@tonic-gate 				if (acl(nam_p, SETACL, aclcnt, aclp) < 0) {
1832*0Sstevel@tonic-gate 					msg(ERRN,
1833*0Sstevel@tonic-gate 					    "\"%s\": failed to set acl", nam_p);
1834*0Sstevel@tonic-gate 				} else {
1835*0Sstevel@tonic-gate 					acl_set = 1;
1836*0Sstevel@tonic-gate 				}
1837*0Sstevel@tonic-gate 
1838*0Sstevel@tonic-gate 				free(aclp);
1839*0Sstevel@tonic-gate 				aclp = NULL;
1840*0Sstevel@tonic-gate 			}
1841*0Sstevel@tonic-gate 
1842*0Sstevel@tonic-gate 			cnt = 0;
1843*0Sstevel@tonic-gate 			break;
1844*0Sstevel@tonic-gate 		}
1845*0Sstevel@tonic-gate 
1846*0Sstevel@tonic-gate 		cnt++;
1847*0Sstevel@tonic-gate 	} while (cnt < 2 && missdir(nam_p) == 0);
1848*0Sstevel@tonic-gate 
1849*0Sstevel@tonic-gate 	switch (cnt) {
1850*0Sstevel@tonic-gate 	case 0:
1851*0Sstevel@tonic-gate 		rv = 1;
1852*0Sstevel@tonic-gate 		rstfiles(U_OVER, dirfd);
1853*0Sstevel@tonic-gate 		break;
1854*0Sstevel@tonic-gate 
1855*0Sstevel@tonic-gate 	case 1:
1856*0Sstevel@tonic-gate 		msg(ERRN,
1857*0Sstevel@tonic-gate 		    "Cannot create directory for \"%s\"", nam_p);
1858*0Sstevel@tonic-gate 
1859*0Sstevel@tonic-gate 		if (*Over_p == '\0') {
1860*0Sstevel@tonic-gate 			rstfiles(U_KEEP, dirfd);
1861*0Sstevel@tonic-gate 		}
1862*0Sstevel@tonic-gate 
1863*0Sstevel@tonic-gate 		break;
1864*0Sstevel@tonic-gate 
1865*0Sstevel@tonic-gate 	case 2:
1866*0Sstevel@tonic-gate 		if (ustar_dir() || Adir) {
1867*0Sstevel@tonic-gate 			msg(ERRN, "Cannot create directory \"%s\"", nam_p);
1868*0Sstevel@tonic-gate 		} else if (ustar_spec() || Aspec) {
1869*0Sstevel@tonic-gate 			msg(ERRN, "Cannot mknod() \"%s\"", nam_p);
1870*0Sstevel@tonic-gate 		}
1871*0Sstevel@tonic-gate 
1872*0Sstevel@tonic-gate 		if (*Over_p == '\0') {
1873*0Sstevel@tonic-gate 			rstfiles(U_KEEP, dirfd);
1874*0Sstevel@tonic-gate 		}
1875*0Sstevel@tonic-gate 
1876*0Sstevel@tonic-gate 		break;
1877*0Sstevel@tonic-gate 
1878*0Sstevel@tonic-gate 	default:
1879*0Sstevel@tonic-gate 		msg(EXT, "Impossible case.");
1880*0Sstevel@tonic-gate 	}
1881*0Sstevel@tonic-gate 
1882*0Sstevel@tonic-gate 	return (rv);
1883*0Sstevel@tonic-gate }
1884*0Sstevel@tonic-gate 
1885*0Sstevel@tonic-gate /*
1886*0Sstevel@tonic-gate  * creat_tmp:
1887*0Sstevel@tonic-gate  */
1888*0Sstevel@tonic-gate 
1889*0Sstevel@tonic-gate static int
1890*0Sstevel@tonic-gate creat_tmp(char *nam_p)
1891*0Sstevel@tonic-gate {
1892*0Sstevel@tonic-gate 	char *t_p;
1893*0Sstevel@tonic-gate 	int	cwd;
1894*0Sstevel@tonic-gate 
1895*0Sstevel@tonic-gate 	if ((Args & OCp) && G_p->g_ino == DesSt.st_ino &&
1896*0Sstevel@tonic-gate 	    G_p->g_dev == DesSt.st_dev) {
1897*0Sstevel@tonic-gate 		msg(ERR, "Attempt to pass a file to itself.");
1898*0Sstevel@tonic-gate 		return (-1);
1899*0Sstevel@tonic-gate 	}
1900*0Sstevel@tonic-gate 
1901*0Sstevel@tonic-gate 	if (G_p->g_mtime <= DesSt.st_mtime && !(Args & OCu)) {
1902*0Sstevel@tonic-gate 		msg(ERR, "Existing \"%s\" same age or newer", nam_p);
1903*0Sstevel@tonic-gate 		return (-1);
1904*0Sstevel@tonic-gate 	}
1905*0Sstevel@tonic-gate 
1906*0Sstevel@tonic-gate 	if (Euid && Aspec) {
1907*0Sstevel@tonic-gate 		msg(ERR, "Cannot overwrite \"%s\"", nam_p);
1908*0Sstevel@tonic-gate 		return (-1);
1909*0Sstevel@tonic-gate 	}
1910*0Sstevel@tonic-gate 
1911*0Sstevel@tonic-gate 	/* Make the temporary file name. */
1912*0Sstevel@tonic-gate 
1913*0Sstevel@tonic-gate 	(void) strcpy(Over_p, nam_p);
1914*0Sstevel@tonic-gate 	t_p = Over_p + strlen(Over_p);
1915*0Sstevel@tonic-gate 
1916*0Sstevel@tonic-gate 	while (t_p != Over_p) {
1917*0Sstevel@tonic-gate 		if (*(t_p - 1) == '/')
1918*0Sstevel@tonic-gate 			break;
1919*0Sstevel@tonic-gate 		t_p--;
1920*0Sstevel@tonic-gate 	}
1921*0Sstevel@tonic-gate 
1922*0Sstevel@tonic-gate 	(void) strcpy(t_p, "XXXXXX");
1923*0Sstevel@tonic-gate 
1924*0Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
1925*0Sstevel@tonic-gate 		/*
1926*0Sstevel@tonic-gate 		 * Save our current directory, so we can go into
1927*0Sstevel@tonic-gate 		 * the attribute directory to make the temp file
1928*0Sstevel@tonic-gate 		 * and then return.
1929*0Sstevel@tonic-gate 		 */
1930*0Sstevel@tonic-gate 
1931*0Sstevel@tonic-gate 		cwd = save_cwd();
1932*0Sstevel@tonic-gate 		(void) fchdir(G_p->g_dirfd);
1933*0Sstevel@tonic-gate 	}
1934*0Sstevel@tonic-gate 
1935*0Sstevel@tonic-gate 	(void) mktemp(Over_p);
1936*0Sstevel@tonic-gate 
1937*0Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
1938*0Sstevel@tonic-gate 		/* Return to the current directory. */
1939*0Sstevel@tonic-gate 
1940*0Sstevel@tonic-gate 		rest_cwd(cwd);
1941*0Sstevel@tonic-gate 	}
1942*0Sstevel@tonic-gate 
1943*0Sstevel@tonic-gate 	if (*Over_p == '\0') {
1944*0Sstevel@tonic-gate 		/* mktemp reports a failure. */
1945*0Sstevel@tonic-gate 
1946*0Sstevel@tonic-gate 		msg(ERR, "Cannot get temporary file name.");
1947*0Sstevel@tonic-gate 		return (-1);
1948*0Sstevel@tonic-gate 	}
1949*0Sstevel@tonic-gate 
1950*0Sstevel@tonic-gate 	/*
1951*0Sstevel@tonic-gate 	 * If it's a regular file, write to the temporary file, and then rename
1952*0Sstevel@tonic-gate 	 * in order to accomodate potential executables.
1953*0Sstevel@tonic-gate 	 *
1954*0Sstevel@tonic-gate 	 * Note: g_typeflag is only defined (set) for USTAR archive types.  It
1955*0Sstevel@tonic-gate 	 * defaults to 0 in the cpio-format-regular file case, so this test
1956*0Sstevel@tonic-gate 	 * succeeds.
1957*0Sstevel@tonic-gate 	 */
1958*0Sstevel@tonic-gate 
1959*0Sstevel@tonic-gate 	if (G_p->g_typeflag == 0 &&
1960*0Sstevel@tonic-gate 	    (DesSt.st_mode & (ulong_t)Ftype) == S_IFREG &&
1961*0Sstevel@tonic-gate 	    (G_p->g_mode & (ulong_t)Ftype) == S_IFREG) {
1962*0Sstevel@tonic-gate 		/*
1963*0Sstevel@tonic-gate 		 * The archive file and the filesystem file are both regular
1964*0Sstevel@tonic-gate 		 * files.  We write to the temporary file in this case.
1965*0Sstevel@tonic-gate 		 */
1966*0Sstevel@tonic-gate 
1967*0Sstevel@tonic-gate 		if (Args & OCp) {
1968*0Sstevel@tonic-gate 			if (G_p->g_attrnam_p == (char *)NULL) {
1969*0Sstevel@tonic-gate 				Fullnam_p = Over_p;
1970*0Sstevel@tonic-gate 			} else {
1971*0Sstevel@tonic-gate 				Attrfile_p = Over_p;
1972*0Sstevel@tonic-gate 			}
1973*0Sstevel@tonic-gate 		} else {
1974*0Sstevel@tonic-gate 			G_p->g_nam_p = Over_p;
1975*0Sstevel@tonic-gate 			if (G_p->g_attrnam_p != (char *)NULL) {
1976*0Sstevel@tonic-gate 				Attrfile_p = Over_p;
1977*0Sstevel@tonic-gate 			}
1978*0Sstevel@tonic-gate 		}
1979*0Sstevel@tonic-gate 
1980*0Sstevel@tonic-gate 		if (G_p->g_attrnam_p == (char *)NULL) {
1981*0Sstevel@tonic-gate 			Over_p = nam_p;
1982*0Sstevel@tonic-gate 		} else {
1983*0Sstevel@tonic-gate 			Over_p = G_p->g_attrnam_p;
1984*0Sstevel@tonic-gate 		}
1985*0Sstevel@tonic-gate 
1986*0Sstevel@tonic-gate 		Do_rename = 1;
1987*0Sstevel@tonic-gate 	} else {
1988*0Sstevel@tonic-gate 		/*
1989*0Sstevel@tonic-gate 		 * Either the archive file or the filesystem file is not a
1990*0Sstevel@tonic-gate 		 * regular file.
1991*0Sstevel@tonic-gate 		 */
1992*0Sstevel@tonic-gate 
1993*0Sstevel@tonic-gate 		Do_rename = 0;
1994*0Sstevel@tonic-gate 
1995*0Sstevel@tonic-gate 		if (S_ISDIR(DesSt.st_mode)) {
1996*0Sstevel@tonic-gate 			/*
1997*0Sstevel@tonic-gate 			 * The filesystem file is a directory.
1998*0Sstevel@tonic-gate 			 *
1999*0Sstevel@tonic-gate 			 * Save the current working directory because we will
2000*0Sstevel@tonic-gate 			 * want to restore it back just in case remove_dir()
2001*0Sstevel@tonic-gate 			 * fails or get confused about where we should be.
2002*0Sstevel@tonic-gate 			 */
2003*0Sstevel@tonic-gate 
2004*0Sstevel@tonic-gate 			*Over_p = '\0';
2005*0Sstevel@tonic-gate 			cwd = save_cwd();
2006*0Sstevel@tonic-gate 
2007*0Sstevel@tonic-gate 			if (remove_dir(nam_p) < 0) {
2008*0Sstevel@tonic-gate 				msg(ERRN,
2009*0Sstevel@tonic-gate 				    "Cannot remove the directory \"%s\"",
2010*0Sstevel@tonic-gate 				    nam_p);
2011*0Sstevel@tonic-gate 				/*
2012*0Sstevel@tonic-gate 				 * Restore working directory back to the one
2013*0Sstevel@tonic-gate 				 * saved earlier.
2014*0Sstevel@tonic-gate 				 */
2015*0Sstevel@tonic-gate 
2016*0Sstevel@tonic-gate 				rest_cwd(cwd);
2017*0Sstevel@tonic-gate 				return (-1);
2018*0Sstevel@tonic-gate 			}
2019*0Sstevel@tonic-gate 
2020*0Sstevel@tonic-gate 			/*
2021*0Sstevel@tonic-gate 			 * Restore working directory back to the one
2022*0Sstevel@tonic-gate 			 * saved earlier
2023*0Sstevel@tonic-gate 			 */
2024*0Sstevel@tonic-gate 
2025*0Sstevel@tonic-gate 			rest_cwd(cwd);
2026*0Sstevel@tonic-gate 		} else {
2027*0Sstevel@tonic-gate 			/*
2028*0Sstevel@tonic-gate 			 * The file is not a directory. Will use the original
2029*0Sstevel@tonic-gate 			 * link/unlink construct, however, if the file is
2030*0Sstevel@tonic-gate 			 * namefs, link would fail with EXDEV. Therefore, we
2031*0Sstevel@tonic-gate 			 * use rename() first to back up the file.
2032*0Sstevel@tonic-gate 			 */
2033*0Sstevel@tonic-gate 			if (rename(nam_p, Over_p) < 0) {
2034*0Sstevel@tonic-gate 				/*
2035*0Sstevel@tonic-gate 				 * If rename failed, try old construction
2036*0Sstevel@tonic-gate 				 * method.
2037*0Sstevel@tonic-gate 				 */
2038*0Sstevel@tonic-gate 				if (link(nam_p, Over_p) < 0) {
2039*0Sstevel@tonic-gate 					msg(ERRN,
2040*0Sstevel@tonic-gate 					    "Cannot create temporary file");
2041*0Sstevel@tonic-gate 					*Over_p = '\0';
2042*0Sstevel@tonic-gate 					return (-1);
2043*0Sstevel@tonic-gate 				}
2044*0Sstevel@tonic-gate 
2045*0Sstevel@tonic-gate 				if (unlink(nam_p) < 0) {
2046*0Sstevel@tonic-gate 					msg(ERRN,
2047*0Sstevel@tonic-gate 					    "Cannot unlink() current \"%s\"",
2048*0Sstevel@tonic-gate 					    nam_p);
2049*0Sstevel@tonic-gate 					(void) unlink(Over_p);
2050*0Sstevel@tonic-gate 					*Over_p = '\0';
2051*0Sstevel@tonic-gate 					return (-1);
2052*0Sstevel@tonic-gate 				}
2053*0Sstevel@tonic-gate 			}
2054*0Sstevel@tonic-gate 		}
2055*0Sstevel@tonic-gate 	}
2056*0Sstevel@tonic-gate 
2057*0Sstevel@tonic-gate 	return (1);
2058*0Sstevel@tonic-gate }
2059*0Sstevel@tonic-gate 
2060*0Sstevel@tonic-gate /*
2061*0Sstevel@tonic-gate  * data_in:  If proc_mode == P_PROC, bread() the file's data from the archive
2062*0Sstevel@tonic-gate  * and write(2) it to the open fdes gotten from openout().  If proc_mode ==
2063*0Sstevel@tonic-gate  * P_SKIP, or becomes P_SKIP (due to errors etc), bread(2) the file's data
2064*0Sstevel@tonic-gate  * and ignore it.  If the user specified any of the "swap" options (b, s or S),
2065*0Sstevel@tonic-gate  * and the length of the file is not appropriate for that action, do not
2066*0Sstevel@tonic-gate  * perform the "swap", otherwise perform the action on a buffer by buffer basis.
2067*0Sstevel@tonic-gate  * If the CRC header was selected, calculate a running checksum as each buffer
2068*0Sstevel@tonic-gate  * is processed.
2069*0Sstevel@tonic-gate  */
2070*0Sstevel@tonic-gate 
2071*0Sstevel@tonic-gate static void
2072*0Sstevel@tonic-gate data_in(int proc_mode)
2073*0Sstevel@tonic-gate {
2074*0Sstevel@tonic-gate 	char *nam_p;
2075*0Sstevel@tonic-gate 	int cnt, pad;
2076*0Sstevel@tonic-gate 	long cksumval = 0L;
2077*0Sstevel@tonic-gate 	off_t filesz;
2078*0Sstevel@tonic-gate 	int rv, swapfile = 0;
2079*0Sstevel@tonic-gate 	int compress_flag = 0;	/* if the file is compressed */
2080*0Sstevel@tonic-gate 	int cstatus = 0;
2081*0Sstevel@tonic-gate 	FILE *pipef;		/* pipe for bar to do de-compression */
2082*0Sstevel@tonic-gate 
2083*0Sstevel@tonic-gate 
2084*0Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
2085*0Sstevel@tonic-gate 		nam_p = G_p->g_attrnam_p;
2086*0Sstevel@tonic-gate 	} else {
2087*0Sstevel@tonic-gate 		nam_p = G_p->g_nam_p;
2088*0Sstevel@tonic-gate 	}
2089*0Sstevel@tonic-gate 
2090*0Sstevel@tonic-gate 
2091*0Sstevel@tonic-gate 	if (((G_p->g_mode & Ftype) == S_IFLNK && proc_mode != P_SKIP) ||
2092*0Sstevel@tonic-gate 	    (Hdr_type == BAR && bar_linkflag == '2' && proc_mode != P_SKIP)) {
2093*0Sstevel@tonic-gate 		proc_mode = P_SKIP;
2094*0Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
2095*0Sstevel@tonic-gate 	}
2096*0Sstevel@tonic-gate 	if (Args & (OCb | OCs | OCS)) { /* verfify that swapping is possible */
2097*0Sstevel@tonic-gate 		swapfile = 1;
2098*0Sstevel@tonic-gate 		if (Args & (OCs | OCb) && G_p->g_filesz % 2) {
2099*0Sstevel@tonic-gate 			msg(ERR,
2100*0Sstevel@tonic-gate 			    "Cannot swap bytes of \"%s\", odd number of bytes",
2101*0Sstevel@tonic-gate 			    nam_p);
2102*0Sstevel@tonic-gate 			swapfile = 0;
2103*0Sstevel@tonic-gate 		}
2104*0Sstevel@tonic-gate 		if (Args & (OCS | OCb) && G_p->g_filesz % 4) {
2105*0Sstevel@tonic-gate 			msg(ERR,
2106*0Sstevel@tonic-gate 			    "Cannot swap halfwords of \"%s\", odd number "
2107*0Sstevel@tonic-gate 			    "of halfwords", nam_p);
2108*0Sstevel@tonic-gate 			swapfile = 0;
2109*0Sstevel@tonic-gate 		}
2110*0Sstevel@tonic-gate 	}
2111*0Sstevel@tonic-gate 	filesz = G_p->g_filesz;
2112*0Sstevel@tonic-gate 
2113*0Sstevel@tonic-gate 	/* This writes out the file from the archive */
2114*0Sstevel@tonic-gate 
2115*0Sstevel@tonic-gate 	while (filesz > 0) {
2116*0Sstevel@tonic-gate 		cnt = (int)(filesz > CPIOBSZ) ? CPIOBSZ : filesz;
2117*0Sstevel@tonic-gate 		FILL(cnt);
2118*0Sstevel@tonic-gate 		if (proc_mode != P_SKIP) {
2119*0Sstevel@tonic-gate 			if (Hdr_type == CRC)
2120*0Sstevel@tonic-gate 				cksumval += cksum(CRC, cnt, NULL);
2121*0Sstevel@tonic-gate 			if (swapfile)
2122*0Sstevel@tonic-gate 				swap(Buffr.b_out_p, cnt);
2123*0Sstevel@tonic-gate 			errno = 0;
2124*0Sstevel@tonic-gate 
2125*0Sstevel@tonic-gate 			/*
2126*0Sstevel@tonic-gate 			 * if the bar archive is compressed, set up a pipe and
2127*0Sstevel@tonic-gate 			 * do the de-compression while reading in the file
2128*0Sstevel@tonic-gate 			 */
2129*0Sstevel@tonic-gate 			if (Hdr_type == BAR) {
2130*0Sstevel@tonic-gate 				if (compress_flag == 0 && Compressed) {
2131*0Sstevel@tonic-gate 					setup_uncompress(&pipef);
2132*0Sstevel@tonic-gate 					compress_flag++;
2133*0Sstevel@tonic-gate 				}
2134*0Sstevel@tonic-gate 
2135*0Sstevel@tonic-gate 			}
2136*0Sstevel@tonic-gate 
2137*0Sstevel@tonic-gate 			rv = write(Ofile, Buffr.b_out_p, cnt);
2138*0Sstevel@tonic-gate 			if (rv < cnt) {
2139*0Sstevel@tonic-gate 				if (rv < 0)
2140*0Sstevel@tonic-gate 					msg(ERRN, "Cannot write \"%s\"", nam_p);
2141*0Sstevel@tonic-gate 				else
2142*0Sstevel@tonic-gate 					msg(EXTN, "Cannot write \"%s\"", nam_p);
2143*0Sstevel@tonic-gate 				proc_mode = P_SKIP;
2144*0Sstevel@tonic-gate 				rstfiles(U_KEEP, G_p->g_dirfd);
2145*0Sstevel@tonic-gate 				cstatus = close(Ofile);
2146*0Sstevel@tonic-gate 				Ofile = 0;
2147*0Sstevel@tonic-gate 				if (cstatus != 0) {
2148*0Sstevel@tonic-gate 					msg(EXTN, "close error");
2149*0Sstevel@tonic-gate 				}
2150*0Sstevel@tonic-gate 			}
2151*0Sstevel@tonic-gate 		}
2152*0Sstevel@tonic-gate 		Buffr.b_out_p += cnt;
2153*0Sstevel@tonic-gate 		Buffr.b_cnt -= (long)cnt;
2154*0Sstevel@tonic-gate 		filesz -= (off_t)cnt;
2155*0Sstevel@tonic-gate 	} /* filesz */
2156*0Sstevel@tonic-gate 
2157*0Sstevel@tonic-gate 	pad = (Pad_val + 1 - (G_p->g_filesz & Pad_val)) & Pad_val;
2158*0Sstevel@tonic-gate 	if (pad != 0) {
2159*0Sstevel@tonic-gate 		FILL(pad);
2160*0Sstevel@tonic-gate 		Buffr.b_out_p += pad;
2161*0Sstevel@tonic-gate 		Buffr.b_cnt -= pad;
2162*0Sstevel@tonic-gate 	}
2163*0Sstevel@tonic-gate 	if (proc_mode != P_SKIP) {
2164*0Sstevel@tonic-gate 		if (Hdr_type == CRC && Gen.g_cksum != cksumval) {
2165*0Sstevel@tonic-gate 			msg(ERR, "\"%s\" - checksum error", nam_p);
2166*0Sstevel@tonic-gate 			rstfiles(U_KEEP, G_p->g_dirfd);
2167*0Sstevel@tonic-gate 		} else
2168*0Sstevel@tonic-gate 			rstfiles(U_OVER, G_p->g_dirfd);
2169*0Sstevel@tonic-gate 		if (Hdr_type == BAR && compress_flag) {
2170*0Sstevel@tonic-gate 			(void) pclose(pipef);
2171*0Sstevel@tonic-gate 		} else {
2172*0Sstevel@tonic-gate 			cstatus = close(Ofile);
2173*0Sstevel@tonic-gate 		}
2174*0Sstevel@tonic-gate 		Ofile = 0;
2175*0Sstevel@tonic-gate 		if (cstatus != 0) {
2176*0Sstevel@tonic-gate 			msg(EXTN, "close error");
2177*0Sstevel@tonic-gate 		}
2178*0Sstevel@tonic-gate 	}
2179*0Sstevel@tonic-gate 	VERBOSE((proc_mode != P_SKIP && (Args & (OCv | OCV))), G_p->g_nam_p);
2180*0Sstevel@tonic-gate 	Finished = 1;
2181*0Sstevel@tonic-gate }
2182*0Sstevel@tonic-gate 
2183*0Sstevel@tonic-gate /*
2184*0Sstevel@tonic-gate  * data_out:  open(2) the file to be archived, compute the checksum
2185*0Sstevel@tonic-gate  * of it's data if the CRC header was specified and write the header.
2186*0Sstevel@tonic-gate  * read(2) each block of data and bwrite() it to the archive.  For TARTYP (TAR
2187*0Sstevel@tonic-gate  * and USTAR) archives, pad the data with NULLs to the next 512 byte boundary.
2188*0Sstevel@tonic-gate  */
2189*0Sstevel@tonic-gate 
2190*0Sstevel@tonic-gate static void
2191*0Sstevel@tonic-gate data_out(void)
2192*0Sstevel@tonic-gate {
2193*0Sstevel@tonic-gate 	char *nam_p;
2194*0Sstevel@tonic-gate 	int cnt, amount_read, pad;
2195*0Sstevel@tonic-gate 	off_t amt_to_read, real_filesz;
2196*0Sstevel@tonic-gate 	int	errret = 0;
2197*0Sstevel@tonic-gate 
2198*0Sstevel@tonic-gate 	nam_p = G_p->g_nam_p;
2199*0Sstevel@tonic-gate 	if (Aspec) {
2200*0Sstevel@tonic-gate 		if (Pflag && aclp != NULL) {
2201*0Sstevel@tonic-gate 			char    *secinfo = NULL;
2202*0Sstevel@tonic-gate 			int	len = 0;
2203*0Sstevel@tonic-gate 
2204*0Sstevel@tonic-gate 			/* append security attributes */
2205*0Sstevel@tonic-gate 			if (append_secattr(&secinfo, &len, aclcnt,
2206*0Sstevel@tonic-gate 				(char *)aclp, UFSD_ACL) == -1) {
2207*0Sstevel@tonic-gate 				msg(ERR,
2208*0Sstevel@tonic-gate 				    "can create security information");
2209*0Sstevel@tonic-gate 			}
2210*0Sstevel@tonic-gate 			/* call append_secattr() if more than one */
2211*0Sstevel@tonic-gate 
2212*0Sstevel@tonic-gate 			if (len > 0) {
2213*0Sstevel@tonic-gate 			/* write ancillary only if there is sec info */
2214*0Sstevel@tonic-gate 				(void) write_hdr(ARCHIVE_ACL, (off_t)len);
2215*0Sstevel@tonic-gate 				(void) write_ancillary(secinfo, len);
2216*0Sstevel@tonic-gate 			}
2217*0Sstevel@tonic-gate 		}
2218*0Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
2219*0Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_dirfd);
2220*0Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
2221*0Sstevel@tonic-gate 		return;
2222*0Sstevel@tonic-gate 	}
2223*0Sstevel@tonic-gate 	if ((G_p->g_mode & Ftype) == S_IFLNK && (Hdr_type !=
2224*0Sstevel@tonic-gate 	    USTAR && Hdr_type != TAR)) { /* symbolic link */
2225*0Sstevel@tonic-gate 		int size;
2226*0Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
2227*0Sstevel@tonic-gate 
2228*0Sstevel@tonic-gate 		FLUSH(G_p->g_filesz);
2229*0Sstevel@tonic-gate 		errno = 0;
2230*0Sstevel@tonic-gate 
2231*0Sstevel@tonic-gate 		/* Note that "size" and G_p->g_filesz are the same number */
2232*0Sstevel@tonic-gate 
2233*0Sstevel@tonic-gate 		if ((size = readlink(nam_p, Buffr.b_in_p, G_p->g_filesz)) <
2234*0Sstevel@tonic-gate 		    0) {
2235*0Sstevel@tonic-gate 			msg(ERRN, "Cannot read symbolic link \"%s\"", nam_p);
2236*0Sstevel@tonic-gate 			return;
2237*0Sstevel@tonic-gate 		}
2238*0Sstevel@tonic-gate 
2239*0Sstevel@tonic-gate 		/*
2240*0Sstevel@tonic-gate 		 * Note that it is OK not to add the NUL after the name read by
2241*0Sstevel@tonic-gate 		 * readlink, because it is not being used subsequently.
2242*0Sstevel@tonic-gate 		 */
2243*0Sstevel@tonic-gate 
2244*0Sstevel@tonic-gate 		Buffr.b_in_p += size;
2245*0Sstevel@tonic-gate 		Buffr.b_cnt += size;
2246*0Sstevel@tonic-gate 		pad = (Pad_val + 1 - (size & Pad_val)) & Pad_val;
2247*0Sstevel@tonic-gate 		if (pad != 0) {
2248*0Sstevel@tonic-gate 			FLUSH(pad);
2249*0Sstevel@tonic-gate 			(void) memcpy(Buffr.b_in_p, Empty, pad);
2250*0Sstevel@tonic-gate 			Buffr.b_in_p += pad;
2251*0Sstevel@tonic-gate 			Buffr.b_cnt += pad;
2252*0Sstevel@tonic-gate 		}
2253*0Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
2254*0Sstevel@tonic-gate 		return;
2255*0Sstevel@tonic-gate 	} else if ((G_p->g_mode & Ftype) == S_IFLNK &&
2256*0Sstevel@tonic-gate 		    (Hdr_type == USTAR || Hdr_type == TAR)) {
2257*0Sstevel@tonic-gate 		int size;
2258*0Sstevel@tonic-gate 
2259*0Sstevel@tonic-gate 		/*
2260*0Sstevel@tonic-gate 		 * G_p->g_filesz is the length of the right-hand side of
2261*0Sstevel@tonic-gate 		 * the symlink "x -> y".
2262*0Sstevel@tonic-gate 		 * The tar link field is only NAMSIZ long.
2263*0Sstevel@tonic-gate 		 */
2264*0Sstevel@tonic-gate 
2265*0Sstevel@tonic-gate 		if (G_p->g_filesz > NAMSIZ) {
2266*0Sstevel@tonic-gate 			msg(ERRN,
2267*0Sstevel@tonic-gate 			    "Symbolic link too long \"%s\"", nam_p);
2268*0Sstevel@tonic-gate 			return;
2269*0Sstevel@tonic-gate 		}
2270*0Sstevel@tonic-gate 		if ((size = readlink(nam_p, T_lname, G_p->g_filesz)) < 0) {
2271*0Sstevel@tonic-gate 			msg(ERRN,
2272*0Sstevel@tonic-gate 			    "Cannot read symbolic link \"%s\"", nam_p);
2273*0Sstevel@tonic-gate 			return;
2274*0Sstevel@tonic-gate 		}
2275*0Sstevel@tonic-gate 		T_lname[size] = '\0';
2276*0Sstevel@tonic-gate 		G_p->g_filesz = (off_t)0;
2277*0Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
2278*0Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), nam_p);
2279*0Sstevel@tonic-gate 		return;
2280*0Sstevel@tonic-gate 	}
2281*0Sstevel@tonic-gate 	if ((Ifile = openfile(O_RDONLY)) < 0) {
2282*0Sstevel@tonic-gate 		msg(ERR, "\"%s%s%s\" ?",
2283*0Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
2284*0Sstevel@tonic-gate 		    nam_p : Gen.g_attrfnam_p,
2285*0Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
2286*0Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
2287*0Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
2288*0Sstevel@tonic-gate 		    "" : Gen.g_attrnam_p);
2289*0Sstevel@tonic-gate 		return;
2290*0Sstevel@tonic-gate 	}
2291*0Sstevel@tonic-gate 
2292*0Sstevel@tonic-gate 	/*
2293*0Sstevel@tonic-gate 	 * Dump extended attribute header.
2294*0Sstevel@tonic-gate 	 */
2295*0Sstevel@tonic-gate 
2296*0Sstevel@tonic-gate 	if (Gen.g_attrnam_p != (char *)NULL) {
2297*0Sstevel@tonic-gate 		write_xattr_hdr();
2298*0Sstevel@tonic-gate 	}
2299*0Sstevel@tonic-gate 
2300*0Sstevel@tonic-gate 	if (Hdr_type == CRC) {
2301*0Sstevel@tonic-gate 		long csum = cksum(CRC, 0, &errret);
2302*0Sstevel@tonic-gate 		if (errret != 0) {
2303*0Sstevel@tonic-gate 			G_p->g_cksum = (ulong_t)-1;
2304*0Sstevel@tonic-gate 			msg(POST, "\"%s%s%s\" skipped",
2305*0Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
2306*0Sstevel@tonic-gate 			    nam_p : Gen.g_attrfnam_p,
2307*0Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
2308*0Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
2309*0Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
2310*0Sstevel@tonic-gate 			    "" : nam_p);
2311*0Sstevel@tonic-gate 			(void) close(Ifile);
2312*0Sstevel@tonic-gate 			return;
2313*0Sstevel@tonic-gate 		}
2314*0Sstevel@tonic-gate 		G_p->g_cksum = csum;
2315*0Sstevel@tonic-gate 	} else {
2316*0Sstevel@tonic-gate 		G_p->g_cksum = 0;
2317*0Sstevel@tonic-gate 	}
2318*0Sstevel@tonic-gate 
2319*0Sstevel@tonic-gate 	/*
2320*0Sstevel@tonic-gate 	 * ACL has been retrieved in getname().
2321*0Sstevel@tonic-gate 	 */
2322*0Sstevel@tonic-gate 	if (Pflag) {
2323*0Sstevel@tonic-gate 		char    *secinfo = NULL;
2324*0Sstevel@tonic-gate 		int	len = 0;
2325*0Sstevel@tonic-gate 
2326*0Sstevel@tonic-gate 		/* append security attributes */
2327*0Sstevel@tonic-gate 		if ((append_secattr(&secinfo, &len, aclcnt, (char *)aclp,
2328*0Sstevel@tonic-gate 		    UFSD_ACL)) == -1)
2329*0Sstevel@tonic-gate 			msg(ERR, "can create security information");
2330*0Sstevel@tonic-gate 
2331*0Sstevel@tonic-gate 		/* call append_secattr() if more than one */
2332*0Sstevel@tonic-gate 
2333*0Sstevel@tonic-gate 		if (len > 0) {
2334*0Sstevel@tonic-gate 		/* write ancillary only if there is sec info */
2335*0Sstevel@tonic-gate 			(void) write_hdr(ARCHIVE_ACL, (off_t)len);
2336*0Sstevel@tonic-gate 			(void) write_ancillary(secinfo, len);
2337*0Sstevel@tonic-gate 		}
2338*0Sstevel@tonic-gate 	}
2339*0Sstevel@tonic-gate 
2340*0Sstevel@tonic-gate 	write_hdr(ARCHIVE_NORMAL, (off_t)0);
2341*0Sstevel@tonic-gate 
2342*0Sstevel@tonic-gate 	real_filesz = 0;
2343*0Sstevel@tonic-gate 
2344*0Sstevel@tonic-gate 	for (amt_to_read = G_p->g_filesz;
2345*0Sstevel@tonic-gate 	    amt_to_read > 0;
2346*0Sstevel@tonic-gate 	    amt_to_read -= (off_t)amount_read) {
2347*0Sstevel@tonic-gate 		FLUSH(CPIOBSZ);
2348*0Sstevel@tonic-gate 		errno = 0;
2349*0Sstevel@tonic-gate 
2350*0Sstevel@tonic-gate 		if ((amount_read = read(Ifile, Buffr.b_in_p, CPIOBSZ)) < 0) {
2351*0Sstevel@tonic-gate 			msg(EXTN, "Cannot read \"%s%s%s\"",
2352*0Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
2353*0Sstevel@tonic-gate 			    nam_p : Gen.g_attrfnam_p,
2354*0Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
2355*0Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
2356*0Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
2357*0Sstevel@tonic-gate 			    "" : nam_p);
2358*0Sstevel@tonic-gate 			break;
2359*0Sstevel@tonic-gate 		}
2360*0Sstevel@tonic-gate 
2361*0Sstevel@tonic-gate 		if (amount_read == 0) {
2362*0Sstevel@tonic-gate 			/* the file has shrunk */
2363*0Sstevel@tonic-gate 			real_filesz = G_p->g_filesz - amt_to_read;
2364*0Sstevel@tonic-gate 			break;
2365*0Sstevel@tonic-gate 		} else if (amount_read > amt_to_read) {
2366*0Sstevel@tonic-gate 			/* the file has grown */
2367*0Sstevel@tonic-gate 			real_filesz = G_p->g_filesz +
2368*0Sstevel@tonic-gate 				(amount_read - amt_to_read);
2369*0Sstevel@tonic-gate 			amount_read = amt_to_read;
2370*0Sstevel@tonic-gate 		} else if (amount_read == amt_to_read) {
2371*0Sstevel@tonic-gate 			/* the file is the same size */
2372*0Sstevel@tonic-gate 			real_filesz = G_p->g_filesz;
2373*0Sstevel@tonic-gate 		}
2374*0Sstevel@tonic-gate 
2375*0Sstevel@tonic-gate 		Buffr.b_in_p += amount_read;
2376*0Sstevel@tonic-gate 		Buffr.b_cnt += (long)amount_read;
2377*0Sstevel@tonic-gate 	}
2378*0Sstevel@tonic-gate 
2379*0Sstevel@tonic-gate 	while (amt_to_read > 0) {
2380*0Sstevel@tonic-gate 		cnt = (amt_to_read > CPIOBSZ) ? CPIOBSZ : (int)amt_to_read;
2381*0Sstevel@tonic-gate 		FLUSH(cnt);
2382*0Sstevel@tonic-gate 		(void) memset(Buffr.b_in_p, NULL, cnt);
2383*0Sstevel@tonic-gate 		Buffr.b_in_p += cnt;
2384*0Sstevel@tonic-gate 		Buffr.b_cnt += cnt;
2385*0Sstevel@tonic-gate 		amt_to_read -= cnt;
2386*0Sstevel@tonic-gate 	}
2387*0Sstevel@tonic-gate 
2388*0Sstevel@tonic-gate 	pad = (Pad_val + 1 - (G_p->g_filesz & Pad_val)) & Pad_val;
2389*0Sstevel@tonic-gate 	if (pad != 0) {
2390*0Sstevel@tonic-gate 		FLUSH(pad);
2391*0Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, Empty, pad);
2392*0Sstevel@tonic-gate 		Buffr.b_in_p += pad;
2393*0Sstevel@tonic-gate 		Buffr.b_cnt += pad;
2394*0Sstevel@tonic-gate 	}
2395*0Sstevel@tonic-gate 
2396*0Sstevel@tonic-gate 	if (real_filesz > G_p->g_filesz) {
2397*0Sstevel@tonic-gate 		msg(ERR, "File size of \"%s%s%s\" has increased by %lld",
2398*0Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
2399*0Sstevel@tonic-gate 		    G_p->g_nam_p : Gen.g_attrfnam_p,
2400*0Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
2401*0Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
2402*0Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
2403*0Sstevel@tonic-gate 		    "" : G_p->g_nam_p,
2404*0Sstevel@tonic-gate 		    (real_filesz - G_p->g_filesz));
2405*0Sstevel@tonic-gate 	}
2406*0Sstevel@tonic-gate 	if (real_filesz < G_p->g_filesz) {
2407*0Sstevel@tonic-gate 		msg(ERR, "File size of \"%s%s%s\" has decreased by %lld",
2408*0Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
2409*0Sstevel@tonic-gate 		    G_p->g_nam_p : Gen.g_attrfnam_p,
2410*0Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
2411*0Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
2412*0Sstevel@tonic-gate 		    (Gen.g_attrnam_p == (char *)NULL) ?
2413*0Sstevel@tonic-gate 		    "" : G_p->g_nam_p,
2414*0Sstevel@tonic-gate 		    (G_p->g_filesz - real_filesz));
2415*0Sstevel@tonic-gate 	}
2416*0Sstevel@tonic-gate 
2417*0Sstevel@tonic-gate 	(void) close(Ifile);
2418*0Sstevel@tonic-gate 	rstfiles(U_KEEP, G_p->g_dirfd);
2419*0Sstevel@tonic-gate 	VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
2420*0Sstevel@tonic-gate }
2421*0Sstevel@tonic-gate 
2422*0Sstevel@tonic-gate /*
2423*0Sstevel@tonic-gate  * data_pass:  If not a special file (Aspec), open(2) the file to be
2424*0Sstevel@tonic-gate  * transferred, read(2) each block of data and write(2) it to the output file
2425*0Sstevel@tonic-gate  * Ofile, which was opened in file_pass().
2426*0Sstevel@tonic-gate  */
2427*0Sstevel@tonic-gate 
2428*0Sstevel@tonic-gate static void
2429*0Sstevel@tonic-gate data_pass(void)
2430*0Sstevel@tonic-gate {
2431*0Sstevel@tonic-gate 	int cnt, done = 1;
2432*0Sstevel@tonic-gate 	int cstatus;
2433*0Sstevel@tonic-gate 	off_t filesz;
2434*0Sstevel@tonic-gate 	char *namep = Nam_p;
2435*0Sstevel@tonic-gate 
2436*0Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
2437*0Sstevel@tonic-gate 		namep = G_p->g_attrnam_p;
2438*0Sstevel@tonic-gate 	}
2439*0Sstevel@tonic-gate 	if (Aspec) {
2440*0Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_passdirfd);
2441*0Sstevel@tonic-gate 		cstatus = close(Ofile);
2442*0Sstevel@tonic-gate 		Ofile = 0;
2443*0Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), Nam_p);
2444*0Sstevel@tonic-gate 		if (cstatus != 0) {
2445*0Sstevel@tonic-gate 			msg(EXTN, "close error");
2446*0Sstevel@tonic-gate 		}
2447*0Sstevel@tonic-gate 		return;
2448*0Sstevel@tonic-gate 	}
2449*0Sstevel@tonic-gate 	if ((Ifile = openat(G_p->g_dirfd, get_component(namep), 0)) < 0) {
2450*0Sstevel@tonic-gate 		msg(ERRN, "Cannot open \"%s%s%s\", skipped",
2451*0Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
2452*0Sstevel@tonic-gate 		    Nam_p : G_p->g_attrfnam_p,
2453*0Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
2454*0Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
2455*0Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
2456*0Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p);
2457*0Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_passdirfd);
2458*0Sstevel@tonic-gate 		cstatus = close(Ofile);
2459*0Sstevel@tonic-gate 		Ofile = 0;
2460*0Sstevel@tonic-gate 		if (cstatus != 0) {
2461*0Sstevel@tonic-gate 			msg(EXTN, "close error");
2462*0Sstevel@tonic-gate 		}
2463*0Sstevel@tonic-gate 		return;
2464*0Sstevel@tonic-gate 	}
2465*0Sstevel@tonic-gate 	filesz = G_p->g_filesz;
2466*0Sstevel@tonic-gate 
2467*0Sstevel@tonic-gate 	while (filesz > 0) {
2468*0Sstevel@tonic-gate 		cnt = (unsigned)(filesz > Bufsize) ? Bufsize : filesz;
2469*0Sstevel@tonic-gate 		errno = 0;
2470*0Sstevel@tonic-gate 		if (read(Ifile, Buf_p, (unsigned)cnt) < 0) {
2471*0Sstevel@tonic-gate 			msg(ERRN, "Cannot read \"%s%s%s\"",
2472*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
2473*0Sstevel@tonic-gate 			    Nam_p : G_p->g_attrfnam_p,
2474*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
2475*0Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
2476*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
2477*0Sstevel@tonic-gate 			    "" : G_p->g_attrnam_p);
2478*0Sstevel@tonic-gate 			done = 0;
2479*0Sstevel@tonic-gate 			break;
2480*0Sstevel@tonic-gate 		}
2481*0Sstevel@tonic-gate 		errno = 0;
2482*0Sstevel@tonic-gate 		if (write(Ofile, Buf_p, (unsigned)cnt) < 0) {
2483*0Sstevel@tonic-gate 			if (Do_rename) {
2484*0Sstevel@tonic-gate 				msg(ERRN, "Cannot write \"%s%s%s\"", Over_p,
2485*0Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
2486*0Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
2487*0Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
2488*0Sstevel@tonic-gate 				    "" : Over_p);
2489*0Sstevel@tonic-gate 			} else {
2490*0Sstevel@tonic-gate 				msg(ERRN, "Cannot write \"%s%s%s\"",
2491*0Sstevel@tonic-gate 				    Fullnam_p,
2492*0Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
2493*0Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
2494*0Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
2495*0Sstevel@tonic-gate 				    "" : G_p->g_attrnam_p);
2496*0Sstevel@tonic-gate 			}
2497*0Sstevel@tonic-gate 
2498*0Sstevel@tonic-gate 			done = 0;
2499*0Sstevel@tonic-gate 			break;
2500*0Sstevel@tonic-gate 		}
2501*0Sstevel@tonic-gate 		Blocks += (u_longlong_t)((cnt + (Bufsize - 1)) / Bufsize);
2502*0Sstevel@tonic-gate 		filesz -= (off_t)cnt;
2503*0Sstevel@tonic-gate 	}
2504*0Sstevel@tonic-gate 	if (done) {
2505*0Sstevel@tonic-gate 		rstfiles(U_OVER, G_p->g_passdirfd);
2506*0Sstevel@tonic-gate 	} else {
2507*0Sstevel@tonic-gate 		rstfiles(U_KEEP, G_p->g_passdirfd);
2508*0Sstevel@tonic-gate 	}
2509*0Sstevel@tonic-gate 	(void) close(Ifile);
2510*0Sstevel@tonic-gate 	cstatus = close(Ofile);
2511*0Sstevel@tonic-gate 	Ofile = 0;
2512*0Sstevel@tonic-gate 	if (cstatus != 0) {
2513*0Sstevel@tonic-gate 		msg(EXTN, "close error");
2514*0Sstevel@tonic-gate 	}
2515*0Sstevel@tonic-gate 	VERBOSE((Args & (OCv | OCV)), Fullnam_p);
2516*0Sstevel@tonic-gate 	Finished = 1;
2517*0Sstevel@tonic-gate }
2518*0Sstevel@tonic-gate 
2519*0Sstevel@tonic-gate /*
2520*0Sstevel@tonic-gate  * Allocation wrappers.  Used to centralize error handling for
2521*0Sstevel@tonic-gate  * failed allocations.
2522*0Sstevel@tonic-gate  */
2523*0Sstevel@tonic-gate static void *
2524*0Sstevel@tonic-gate e_alloc_fail(int flag)
2525*0Sstevel@tonic-gate {
2526*0Sstevel@tonic-gate 	if (flag == E_EXIT) {
2527*0Sstevel@tonic-gate 		msg(EXTN, "Out of memory");
2528*0Sstevel@tonic-gate 	}
2529*0Sstevel@tonic-gate 
2530*0Sstevel@tonic-gate 	return (NULL);
2531*0Sstevel@tonic-gate }
2532*0Sstevel@tonic-gate 
2533*0Sstevel@tonic-gate /*
2534*0Sstevel@tonic-gate  *  Note: unlike the other e_*lloc functions, e_realloc does not zero out the
2535*0Sstevel@tonic-gate  *  additional memory it returns.  Ensure that you do not trust its contents
2536*0Sstevel@tonic-gate  *  when you call it.
2537*0Sstevel@tonic-gate  */
2538*0Sstevel@tonic-gate 
2539*0Sstevel@tonic-gate static void *
2540*0Sstevel@tonic-gate e_realloc(int flag, void *old, size_t newsize)
2541*0Sstevel@tonic-gate {
2542*0Sstevel@tonic-gate 	void *ret = realloc(old, newsize);
2543*0Sstevel@tonic-gate 
2544*0Sstevel@tonic-gate 	if (ret == NULL) {
2545*0Sstevel@tonic-gate 		return (e_alloc_fail(flag));
2546*0Sstevel@tonic-gate 	}
2547*0Sstevel@tonic-gate 
2548*0Sstevel@tonic-gate 	return (ret);
2549*0Sstevel@tonic-gate }
2550*0Sstevel@tonic-gate 
2551*0Sstevel@tonic-gate static char *
2552*0Sstevel@tonic-gate e_strdup(int flag, const char *arg)
2553*0Sstevel@tonic-gate {
2554*0Sstevel@tonic-gate 	char *ret = strdup(arg);
2555*0Sstevel@tonic-gate 
2556*0Sstevel@tonic-gate 	if (ret == NULL) {
2557*0Sstevel@tonic-gate 		return (e_alloc_fail(flag));
2558*0Sstevel@tonic-gate 	}
2559*0Sstevel@tonic-gate 
2560*0Sstevel@tonic-gate 	return (ret);
2561*0Sstevel@tonic-gate }
2562*0Sstevel@tonic-gate 
2563*0Sstevel@tonic-gate static void *
2564*0Sstevel@tonic-gate e_valloc(int flag, size_t size)
2565*0Sstevel@tonic-gate {
2566*0Sstevel@tonic-gate 	void *ret = valloc(size);
2567*0Sstevel@tonic-gate 
2568*0Sstevel@tonic-gate 	if (ret == NULL) {
2569*0Sstevel@tonic-gate 		return (e_alloc_fail(flag));
2570*0Sstevel@tonic-gate 	}
2571*0Sstevel@tonic-gate 
2572*0Sstevel@tonic-gate 	return (ret);
2573*0Sstevel@tonic-gate }
2574*0Sstevel@tonic-gate 
2575*0Sstevel@tonic-gate static void *
2576*0Sstevel@tonic-gate e_zalloc(int flag, size_t size)
2577*0Sstevel@tonic-gate {
2578*0Sstevel@tonic-gate 	void *ret = malloc(size);
2579*0Sstevel@tonic-gate 
2580*0Sstevel@tonic-gate 	if (ret == NULL) {
2581*0Sstevel@tonic-gate 		return (e_alloc_fail(flag));
2582*0Sstevel@tonic-gate 	}
2583*0Sstevel@tonic-gate 
2584*0Sstevel@tonic-gate 	(void) memset(ret, 0, size);
2585*0Sstevel@tonic-gate 	return (ret);
2586*0Sstevel@tonic-gate }
2587*0Sstevel@tonic-gate 
2588*0Sstevel@tonic-gate /*
2589*0Sstevel@tonic-gate  * file_in:  Process an object from the archive.  If a TARTYP (TAR or USTAR)
2590*0Sstevel@tonic-gate  * archive and g_nlink == 1, link this file to the file name in t_linkname
2591*0Sstevel@tonic-gate  * and return.  Handle linked files in one of two ways.  If Onecopy == 0, this
2592*0Sstevel@tonic-gate  * is an old style (binary or -c) archive, create and extract the data for the
2593*0Sstevel@tonic-gate  * first link found, link all subsequent links to this file and skip their data.
2594*0Sstevel@tonic-gate  * If Oncecopy == 1, save links until all have been processed, and then
2595*0Sstevel@tonic-gate  * process the links first to last checking their names against the patterns
2596*0Sstevel@tonic-gate  * and/or asking the user to rename them.  The first link that is accepted
2597*0Sstevel@tonic-gate  * for xtraction is created and the data is read from the archive.
2598*0Sstevel@tonic-gate  * All subsequent links that are accepted are linked to this file.
2599*0Sstevel@tonic-gate  */
2600*0Sstevel@tonic-gate 
2601*0Sstevel@tonic-gate static void
2602*0Sstevel@tonic-gate file_in(void)
2603*0Sstevel@tonic-gate {
2604*0Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
2605*0Sstevel@tonic-gate 	int lnkem = 0, cleanup = 0;
2606*0Sstevel@tonic-gate 	int proc_file;
2607*0Sstevel@tonic-gate 	struct Lnk *ttl_p;
2608*0Sstevel@tonic-gate 	int typeflag;
2609*0Sstevel@tonic-gate 	char savacl;
2610*0Sstevel@tonic-gate 	int cwd;
2611*0Sstevel@tonic-gate 
2612*0Sstevel@tonic-gate 	G_p = &Gen;
2613*0Sstevel@tonic-gate 
2614*0Sstevel@tonic-gate 	/*
2615*0Sstevel@tonic-gate 	 * Open target directory if this isn't a skipped file
2616*0Sstevel@tonic-gate 	 * and g_nlink == 1
2617*0Sstevel@tonic-gate 	 *
2618*0Sstevel@tonic-gate 	 * Links are handled further down in this function.
2619*0Sstevel@tonic-gate 	 */
2620*0Sstevel@tonic-gate 
2621*0Sstevel@tonic-gate 	proc_file = ckname(0);
2622*0Sstevel@tonic-gate 
2623*0Sstevel@tonic-gate 	if (proc_file == F_SKIP && G_p->g_nlink == 1) {
2624*0Sstevel@tonic-gate 		/*
2625*0Sstevel@tonic-gate 		 * Normally ckname() prints out the file as a side
2626*0Sstevel@tonic-gate 		 * effect except for table of contents listing
2627*0Sstevel@tonic-gate 		 * when its parameter is zero and Onecopy isn't
2628*0Sstevel@tonic-gate 		 * Zero.  Due to this we need to force the name
2629*0Sstevel@tonic-gate 		 * to be printed here.
2630*0Sstevel@tonic-gate 		 */
2631*0Sstevel@tonic-gate 		if (Onecopy == 1) {
2632*0Sstevel@tonic-gate 			VERBOSE((Args & OCt), G_p->g_nam_p);
2633*0Sstevel@tonic-gate 		}
2634*0Sstevel@tonic-gate 		data_in(P_SKIP);
2635*0Sstevel@tonic-gate 		return;
2636*0Sstevel@tonic-gate 	}
2637*0Sstevel@tonic-gate 
2638*0Sstevel@tonic-gate 	if (proc_file != F_SKIP && open_dirfd() != 0) {
2639*0Sstevel@tonic-gate 		data_in(P_SKIP);
2640*0Sstevel@tonic-gate 		return;
2641*0Sstevel@tonic-gate 	}
2642*0Sstevel@tonic-gate 
2643*0Sstevel@tonic-gate 	if (Hdr_type == BAR) {
2644*0Sstevel@tonic-gate 		bar_file_in();
2645*0Sstevel@tonic-gate 		close_dirfd();
2646*0Sstevel@tonic-gate 		return;
2647*0Sstevel@tonic-gate 	}
2648*0Sstevel@tonic-gate 
2649*0Sstevel@tonic-gate 	/*
2650*0Sstevel@tonic-gate 	 * For archives in USTAR format, the files are extracted according
2651*0Sstevel@tonic-gate 	 * to the typeflag.
2652*0Sstevel@tonic-gate 	 */
2653*0Sstevel@tonic-gate 	if (Hdr_type == USTAR || Hdr_type == TAR) {
2654*0Sstevel@tonic-gate 		typeflag = Thdr_p->tbuf.t_typeflag;
2655*0Sstevel@tonic-gate 		if (G_p->g_nlink == 1) {		/* hard link */
2656*0Sstevel@tonic-gate 			if (proc_file != F_SKIP) {
2657*0Sstevel@tonic-gate 			    int i;
2658*0Sstevel@tonic-gate 			    char lname[NAMSIZ+1];
2659*0Sstevel@tonic-gate 			    (void) memset(lname, '\0', sizeof (lname));
2660*0Sstevel@tonic-gate 
2661*0Sstevel@tonic-gate 			    (void) strncpy(lname, Thdr_p->tbuf.t_linkname,
2662*0Sstevel@tonic-gate 				NAMSIZ);
2663*0Sstevel@tonic-gate 			    for (i = 0; i <= NAMSIZ && lname[i] != 0; i++)
2664*0Sstevel@tonic-gate 				;
2665*0Sstevel@tonic-gate 
2666*0Sstevel@tonic-gate 			    lname[i] = 0;
2667*0Sstevel@tonic-gate 			    (void) creat_lnk(G_p->g_dirfd,
2668*0Sstevel@tonic-gate 				&lname[0], G_p->g_nam_p);
2669*0Sstevel@tonic-gate 			}
2670*0Sstevel@tonic-gate 			close_dirfd();
2671*0Sstevel@tonic-gate 			return;
2672*0Sstevel@tonic-gate 		}
2673*0Sstevel@tonic-gate 		if (typeflag == '3' || typeflag == '4' || typeflag == '5' ||
2674*0Sstevel@tonic-gate 		    typeflag == '6') {
2675*0Sstevel@tonic-gate 			if (proc_file != F_SKIP &&
2676*0Sstevel@tonic-gate 			    creat_spec(G_p->g_dirfd) > 0) {
2677*0Sstevel@tonic-gate 				VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
2678*0Sstevel@tonic-gate 			}
2679*0Sstevel@tonic-gate 			close_dirfd();
2680*0Sstevel@tonic-gate 			return;
2681*0Sstevel@tonic-gate 		} else if (Adir || Aspec) {
2682*0Sstevel@tonic-gate 			if ((proc_file == F_SKIP) ||
2683*0Sstevel@tonic-gate 					(Ofile = openout(G_p->g_dirfd)) < 0) {
2684*0Sstevel@tonic-gate 				data_in(P_SKIP);
2685*0Sstevel@tonic-gate 			} else {
2686*0Sstevel@tonic-gate 				data_in(P_PROC);
2687*0Sstevel@tonic-gate 			}
2688*0Sstevel@tonic-gate 			close_dirfd();
2689*0Sstevel@tonic-gate 			return;
2690*0Sstevel@tonic-gate 		}
2691*0Sstevel@tonic-gate 	}
2692*0Sstevel@tonic-gate 
2693*0Sstevel@tonic-gate 	if (Adir) {
2694*0Sstevel@tonic-gate 		if (proc_file != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
2695*0Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
2696*0Sstevel@tonic-gate 		}
2697*0Sstevel@tonic-gate 		close_dirfd();
2698*0Sstevel@tonic-gate 		if (Onecopy == 1) {
2699*0Sstevel@tonic-gate 			VERBOSE((Args & OCt), G_p->g_nam_p);
2700*0Sstevel@tonic-gate 		}
2701*0Sstevel@tonic-gate 		return;
2702*0Sstevel@tonic-gate 	}
2703*0Sstevel@tonic-gate 	if (G_p->g_nlink == 1 || (Hdr_type == TAR ||
2704*0Sstevel@tonic-gate 	    Hdr_type == USTAR)) {
2705*0Sstevel@tonic-gate 		if (Aspec) {
2706*0Sstevel@tonic-gate 			if (proc_file != F_SKIP && creat_spec(G_p->g_dirfd) > 0)
2707*0Sstevel@tonic-gate 				VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
2708*0Sstevel@tonic-gate 		} else {
2709*0Sstevel@tonic-gate 			if ((proc_file == F_SKIP) ||
2710*0Sstevel@tonic-gate 					(Ofile = openout(G_p->g_dirfd)) < 0) {
2711*0Sstevel@tonic-gate 				data_in(P_SKIP);
2712*0Sstevel@tonic-gate 			} else {
2713*0Sstevel@tonic-gate 				data_in(P_PROC);
2714*0Sstevel@tonic-gate 			}
2715*0Sstevel@tonic-gate 		}
2716*0Sstevel@tonic-gate 		close_dirfd();
2717*0Sstevel@tonic-gate 		return;
2718*0Sstevel@tonic-gate 	}
2719*0Sstevel@tonic-gate 	close_dirfd();
2720*0Sstevel@tonic-gate 
2721*0Sstevel@tonic-gate 	tl_p = add_lnk(&ttl_p);
2722*0Sstevel@tonic-gate 	l_p = ttl_p;
2723*0Sstevel@tonic-gate 	if (l_p->L_cnt == l_p->L_gen.g_nlink)
2724*0Sstevel@tonic-gate 		cleanup = 1;
2725*0Sstevel@tonic-gate 	if (!Onecopy || G_p->g_attrnam_p != (char *)NULL) {
2726*0Sstevel@tonic-gate 		lnkem = (tl_p != l_p) ? 1 : 0;
2727*0Sstevel@tonic-gate 		G_p = &tl_p->L_gen;
2728*0Sstevel@tonic-gate 		if (proc_file == F_SKIP) {
2729*0Sstevel@tonic-gate 			data_in(P_SKIP);
2730*0Sstevel@tonic-gate 		} else {
2731*0Sstevel@tonic-gate 			if (open_dirfd() != 0)
2732*0Sstevel@tonic-gate 				return;
2733*0Sstevel@tonic-gate 			if (!lnkem) {
2734*0Sstevel@tonic-gate 				if (Aspec) {
2735*0Sstevel@tonic-gate 					if (creat_spec(G_p->g_dirfd) > 0)
2736*0Sstevel@tonic-gate 						VERBOSE((Args & (OCv | OCV)),
2737*0Sstevel@tonic-gate 						    G_p->g_nam_p);
2738*0Sstevel@tonic-gate 				} else if ((Ofile =
2739*0Sstevel@tonic-gate 						openout(G_p->g_dirfd)) < 0) {
2740*0Sstevel@tonic-gate 					data_in(P_SKIP);
2741*0Sstevel@tonic-gate 					close_dirfd();
2742*0Sstevel@tonic-gate 					reclaim(l_p);
2743*0Sstevel@tonic-gate 				} else {
2744*0Sstevel@tonic-gate 					data_in(P_PROC);
2745*0Sstevel@tonic-gate 					close_dirfd();
2746*0Sstevel@tonic-gate 				}
2747*0Sstevel@tonic-gate 			} else {
2748*0Sstevel@tonic-gate 				/*
2749*0Sstevel@tonic-gate 				 * Are we linking an attribute?
2750*0Sstevel@tonic-gate 				 */
2751*0Sstevel@tonic-gate 				cwd = -1;
2752*0Sstevel@tonic-gate 				if (l_p->L_gen.g_attrnam_p != (char *)NULL) {
2753*0Sstevel@tonic-gate 					(void) strcpy(Lnkend_p,
2754*0Sstevel@tonic-gate 					    l_p->L_gen.g_attrnam_p);
2755*0Sstevel@tonic-gate 					(void) strcpy(Full_p,
2756*0Sstevel@tonic-gate 					    tl_p->L_gen.g_attrnam_p);
2757*0Sstevel@tonic-gate 					cwd = save_cwd();
2758*0Sstevel@tonic-gate 					(void) fchdir(G_p->g_dirfd);
2759*0Sstevel@tonic-gate 				} else {
2760*0Sstevel@tonic-gate 					(void) strcpy(Lnkend_p,
2761*0Sstevel@tonic-gate 					    l_p->L_gen.g_nam_p);
2762*0Sstevel@tonic-gate 					(void) strcpy(Full_p,
2763*0Sstevel@tonic-gate 					    tl_p->L_gen.g_nam_p);
2764*0Sstevel@tonic-gate 				}
2765*0Sstevel@tonic-gate 				(void) creat_lnk(G_p->g_dirfd,
2766*0Sstevel@tonic-gate 				    Lnkend_p, Full_p);
2767*0Sstevel@tonic-gate 				data_in(P_SKIP);
2768*0Sstevel@tonic-gate 				close_dirfd();
2769*0Sstevel@tonic-gate 				l_p->L_lnk_p = (struct Lnk *)NULL;
2770*0Sstevel@tonic-gate 				free(tl_p->L_gen.g_nam_p);
2771*0Sstevel@tonic-gate 				free(tl_p);
2772*0Sstevel@tonic-gate 				if (cwd != -1)
2773*0Sstevel@tonic-gate 					rest_cwd(cwd);
2774*0Sstevel@tonic-gate 			}
2775*0Sstevel@tonic-gate 		}
2776*0Sstevel@tonic-gate 	} else { /* Onecopy */
2777*0Sstevel@tonic-gate 		if (tl_p->L_gen.g_filesz)
2778*0Sstevel@tonic-gate 			cleanup = 1;
2779*0Sstevel@tonic-gate 		if (!cleanup) {
2780*0Sstevel@tonic-gate 			close_dirfd();
2781*0Sstevel@tonic-gate 			return; /* don't do anything yet */
2782*0Sstevel@tonic-gate 		}
2783*0Sstevel@tonic-gate 		tl_p = l_p;
2784*0Sstevel@tonic-gate 		/*
2785*0Sstevel@tonic-gate 		 * ckname will clear aclchar. We need to keep aclchar for
2786*0Sstevel@tonic-gate 		 * all links.
2787*0Sstevel@tonic-gate 		 */
2788*0Sstevel@tonic-gate 		savacl = aclchar;
2789*0Sstevel@tonic-gate 		while (tl_p != (struct Lnk *)NULL) {
2790*0Sstevel@tonic-gate 			G_p = &tl_p->L_gen;
2791*0Sstevel@tonic-gate 			aclchar = savacl;
2792*0Sstevel@tonic-gate 			if ((proc_file = ckname(1)) != F_SKIP) {
2793*0Sstevel@tonic-gate 				if (open_dirfd() != 0) {
2794*0Sstevel@tonic-gate 					return;
2795*0Sstevel@tonic-gate 				}
2796*0Sstevel@tonic-gate 				if (l_p->L_data) {
2797*0Sstevel@tonic-gate 					(void) creat_lnk(G_p->g_dirfd,
2798*0Sstevel@tonic-gate 					    l_p->L_gen.g_nam_p,
2799*0Sstevel@tonic-gate 					    G_p->g_nam_p);
2800*0Sstevel@tonic-gate 				} else if (Aspec) {
2801*0Sstevel@tonic-gate 					(void) creat_spec(G_p->g_dirfd);
2802*0Sstevel@tonic-gate 					l_p->L_data = 1;
2803*0Sstevel@tonic-gate 					VERBOSE((Args & (OCv | OCV)),
2804*0Sstevel@tonic-gate 					    G_p->g_nam_p);
2805*0Sstevel@tonic-gate 				} else if ((Ofile =
2806*0Sstevel@tonic-gate 				    openout(G_p->g_dirfd)) < 0) {
2807*0Sstevel@tonic-gate 					proc_file = F_SKIP;
2808*0Sstevel@tonic-gate 				} else {
2809*0Sstevel@tonic-gate 					data_in(P_PROC);
2810*0Sstevel@tonic-gate 					l_p->L_data = 1;
2811*0Sstevel@tonic-gate 				}
2812*0Sstevel@tonic-gate 			} /* (proc_file = ckname(1)) != F_SKIP */
2813*0Sstevel@tonic-gate 
2814*0Sstevel@tonic-gate 			tl_p = tl_p->L_lnk_p;
2815*0Sstevel@tonic-gate 
2816*0Sstevel@tonic-gate 			close_dirfd();
2817*0Sstevel@tonic-gate 
2818*0Sstevel@tonic-gate 			if (proc_file == F_SKIP && !cleanup) {
2819*0Sstevel@tonic-gate 				tl_p->L_nxt_p = l_p->L_nxt_p;
2820*0Sstevel@tonic-gate 				tl_p->L_bck_p = l_p->L_bck_p;
2821*0Sstevel@tonic-gate 				l_p->L_bck_p->L_nxt_p = tl_p;
2822*0Sstevel@tonic-gate 				l_p->L_nxt_p->L_bck_p = tl_p;
2823*0Sstevel@tonic-gate 				free(l_p->L_gen.g_nam_p);
2824*0Sstevel@tonic-gate 				free(l_p);
2825*0Sstevel@tonic-gate 			}
2826*0Sstevel@tonic-gate 		} /* tl_p->L_lnk_p != (struct Lnk *)NULL */
2827*0Sstevel@tonic-gate 		if (l_p->L_data == 0) {
2828*0Sstevel@tonic-gate 			data_in(P_SKIP);
2829*0Sstevel@tonic-gate 		}
2830*0Sstevel@tonic-gate 	}
2831*0Sstevel@tonic-gate 	if (cleanup) {
2832*0Sstevel@tonic-gate 		reclaim(l_p);
2833*0Sstevel@tonic-gate 	}
2834*0Sstevel@tonic-gate }
2835*0Sstevel@tonic-gate 
2836*0Sstevel@tonic-gate /*
2837*0Sstevel@tonic-gate  * file_out:  If the current file is not a special file (!Aspec) and it
2838*0Sstevel@tonic-gate  * is identical to the archive, skip it (do not archive the archive if it
2839*0Sstevel@tonic-gate  * is a regular file).  If creating a TARTYP (TAR or USTAR) archive, the first
2840*0Sstevel@tonic-gate  * time a link to a file is encountered, write the header and file out normally.
2841*0Sstevel@tonic-gate  * Subsequent links to this file put this file name in their t_linkname field.
2842*0Sstevel@tonic-gate  * Otherwise, links are handled in one of two ways, for the old headers
2843*0Sstevel@tonic-gate  * (i.e. binary and -c), linked files are written out as they are encountered.
2844*0Sstevel@tonic-gate  * For the new headers (ASC and CRC), links are saved up until all the links
2845*0Sstevel@tonic-gate  * to each file are found.  For a file with n links, write n - 1 headers with
2846*0Sstevel@tonic-gate  * g_filesz set to 0, write the final (nth) header with the correct g_filesz
2847*0Sstevel@tonic-gate  * value and write the data for the file to the archive.
2848*0Sstevel@tonic-gate  */
2849*0Sstevel@tonic-gate 
2850*0Sstevel@tonic-gate static
2851*0Sstevel@tonic-gate int
2852*0Sstevel@tonic-gate file_out(void)
2853*0Sstevel@tonic-gate {
2854*0Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
2855*0Sstevel@tonic-gate 	int cleanup = 0;
2856*0Sstevel@tonic-gate 	struct Lnk *ttl_p;
2857*0Sstevel@tonic-gate 
2858*0Sstevel@tonic-gate 	G_p = &Gen;
2859*0Sstevel@tonic-gate 	if (!Aspec && IDENT(SrcSt, ArchSt))
2860*0Sstevel@tonic-gate 		return (1); /* do not archive the archive if it's a reg file */
2861*0Sstevel@tonic-gate 	if (G_p->g_filesz > Max_offset) {
2862*0Sstevel@tonic-gate 		msg(ERR, "cpio: %s%s%s: too large to archive in current mode",
2863*0Sstevel@tonic-gate 		    G_p->g_nam_p,
2864*0Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
2865*0Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
2866*0Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
2867*0Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p);
2868*0Sstevel@tonic-gate 		return (1); /* do not archive if it's too big */
2869*0Sstevel@tonic-gate 	}
2870*0Sstevel@tonic-gate 	if (Hdr_type == TAR || Hdr_type == USTAR) { /* TAR and USTAR */
2871*0Sstevel@tonic-gate 		if (Adir) {
2872*0Sstevel@tonic-gate 			if (Gen.g_attrnam_p != (char *)NULL) {
2873*0Sstevel@tonic-gate 				write_xattr_hdr();
2874*0Sstevel@tonic-gate 			}
2875*0Sstevel@tonic-gate 			write_hdr(ARCHIVE_NORMAL, 0);
2876*0Sstevel@tonic-gate 			return (0);
2877*0Sstevel@tonic-gate 		}
2878*0Sstevel@tonic-gate 		if (G_p->g_nlink == 1) {
2879*0Sstevel@tonic-gate 			data_out();
2880*0Sstevel@tonic-gate 			return (0);
2881*0Sstevel@tonic-gate 		}
2882*0Sstevel@tonic-gate 		tl_p = add_lnk(&ttl_p);
2883*0Sstevel@tonic-gate 		l_p = ttl_p;
2884*0Sstevel@tonic-gate 		if (tl_p == l_p) { /* first link to this file encountered */
2885*0Sstevel@tonic-gate 			data_out();
2886*0Sstevel@tonic-gate 			return (0);
2887*0Sstevel@tonic-gate 		}
2888*0Sstevel@tonic-gate 		(void) strncpy(T_lname, l_p->L_gen.g_nam_p,
2889*0Sstevel@tonic-gate 		    l_p->L_gen.g_namesz);
2890*0Sstevel@tonic-gate 
2891*0Sstevel@tonic-gate 		/*
2892*0Sstevel@tonic-gate 		 * check if linkname is greater than 100 characters
2893*0Sstevel@tonic-gate 		 */
2894*0Sstevel@tonic-gate 		if (strlen(T_lname) > NAMSIZ) {
2895*0Sstevel@tonic-gate 			msg(EPOST, "cpio: %s: linkname %s is greater than %d",
2896*0Sstevel@tonic-gate 			    G_p->g_nam_p, T_lname, NAMSIZ);
2897*0Sstevel@tonic-gate 			return (1);
2898*0Sstevel@tonic-gate 		}
2899*0Sstevel@tonic-gate 
2900*0Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
2901*0Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), tl_p->L_gen.g_nam_p);
2902*0Sstevel@tonic-gate 
2903*0Sstevel@tonic-gate 		/* find the lnk entry in sublist, unlink it, and free it */
2904*0Sstevel@tonic-gate 		for (; ttl_p->L_lnk_p != NULL;
2905*0Sstevel@tonic-gate 		    ttl_p = ttl_p->L_lnk_p) {
2906*0Sstevel@tonic-gate 			if (ttl_p->L_lnk_p == tl_p) {
2907*0Sstevel@tonic-gate 				ttl_p->L_lnk_p = tl_p->L_lnk_p;
2908*0Sstevel@tonic-gate 				free(tl_p->L_gen.g_nam_p);
2909*0Sstevel@tonic-gate 				free(tl_p);
2910*0Sstevel@tonic-gate 				break;
2911*0Sstevel@tonic-gate 			}
2912*0Sstevel@tonic-gate 		}
2913*0Sstevel@tonic-gate 
2914*0Sstevel@tonic-gate 		return (0);
2915*0Sstevel@tonic-gate 	}
2916*0Sstevel@tonic-gate 	if (Adir) {
2917*0Sstevel@tonic-gate 		/*
2918*0Sstevel@tonic-gate 		 * ACL has been retrieved in getname().
2919*0Sstevel@tonic-gate 		 */
2920*0Sstevel@tonic-gate 		if (Pflag) {
2921*0Sstevel@tonic-gate 			char    *secinfo = NULL;
2922*0Sstevel@tonic-gate 			int	len = 0;
2923*0Sstevel@tonic-gate 
2924*0Sstevel@tonic-gate 			/* append security attributes */
2925*0Sstevel@tonic-gate 			if ((append_secattr(&secinfo, &len, aclcnt,
2926*0Sstevel@tonic-gate 			    (char *)aclp, UFSD_ACL)) == -1)
2927*0Sstevel@tonic-gate 				msg(ERR, "can create security information");
2928*0Sstevel@tonic-gate 
2929*0Sstevel@tonic-gate 			/* call append_secattr() if more than one */
2930*0Sstevel@tonic-gate 
2931*0Sstevel@tonic-gate 			if (len > 0) {
2932*0Sstevel@tonic-gate 			/* write ancillary */
2933*0Sstevel@tonic-gate 				(void) write_hdr(ARCHIVE_ACL, (off_t)len);
2934*0Sstevel@tonic-gate 				(void) write_ancillary(secinfo, len);
2935*0Sstevel@tonic-gate 			}
2936*0Sstevel@tonic-gate 		}
2937*0Sstevel@tonic-gate 
2938*0Sstevel@tonic-gate 		if (Gen.g_attrnam_p != (char *)NULL) {
2939*0Sstevel@tonic-gate 			write_xattr_hdr();
2940*0Sstevel@tonic-gate 		}
2941*0Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
2942*0Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
2943*0Sstevel@tonic-gate 		return (0);
2944*0Sstevel@tonic-gate 	}
2945*0Sstevel@tonic-gate 	if (G_p->g_nlink == 1) {
2946*0Sstevel@tonic-gate 		data_out();
2947*0Sstevel@tonic-gate 		return (0);
2948*0Sstevel@tonic-gate 	} else {
2949*0Sstevel@tonic-gate 		tl_p = add_lnk(&ttl_p);
2950*0Sstevel@tonic-gate 		l_p = ttl_p;
2951*0Sstevel@tonic-gate 
2952*0Sstevel@tonic-gate 		if (l_p->L_cnt == l_p->L_gen.g_nlink)
2953*0Sstevel@tonic-gate 			cleanup = 1;
2954*0Sstevel@tonic-gate 		else if (Onecopy && G_p->g_attrnam_p == (char *)NULL) {
2955*0Sstevel@tonic-gate 			return (0); /* don't process data yet */
2956*0Sstevel@tonic-gate 		}
2957*0Sstevel@tonic-gate 	}
2958*0Sstevel@tonic-gate 	if (Onecopy && G_p->g_attrnam_p == (char *)NULL) {
2959*0Sstevel@tonic-gate 		tl_p = l_p;
2960*0Sstevel@tonic-gate 		while (tl_p->L_lnk_p != (struct Lnk *)NULL) {
2961*0Sstevel@tonic-gate 			G_p = &tl_p->L_gen;
2962*0Sstevel@tonic-gate 			G_p->g_filesz = (off_t)0;
2963*0Sstevel@tonic-gate 			/* one link with the acl is sufficient */
2964*0Sstevel@tonic-gate 			write_hdr(ARCHIVE_NORMAL, (off_t)0);
2965*0Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
2966*0Sstevel@tonic-gate 			tl_p = tl_p->L_lnk_p;
2967*0Sstevel@tonic-gate 		}
2968*0Sstevel@tonic-gate 		G_p = &tl_p->L_gen;
2969*0Sstevel@tonic-gate 		if (open_dirfd() != 0)
2970*0Sstevel@tonic-gate 			return (1);
2971*0Sstevel@tonic-gate 	}
2972*0Sstevel@tonic-gate 	/* old style: has acl and data for every link */
2973*0Sstevel@tonic-gate 	data_out();
2974*0Sstevel@tonic-gate 	if (cleanup)
2975*0Sstevel@tonic-gate 		reclaim(l_p);
2976*0Sstevel@tonic-gate 	return (0);
2977*0Sstevel@tonic-gate }
2978*0Sstevel@tonic-gate 
2979*0Sstevel@tonic-gate /*
2980*0Sstevel@tonic-gate  * file_pass:  If the -l option is set (link files when possible), and the
2981*0Sstevel@tonic-gate  * source and destination file systems are the same, link the source file
2982*0Sstevel@tonic-gate  * (G_p->g_nam_p) to the destination file (Fullnam) and return.  If not a
2983*0Sstevel@tonic-gate  * linked file, transfer the data.  Otherwise, the first link to a file
2984*0Sstevel@tonic-gate  * encountered is transferred normally and subsequent links are linked to it.
2985*0Sstevel@tonic-gate  */
2986*0Sstevel@tonic-gate 
2987*0Sstevel@tonic-gate static int
2988*0Sstevel@tonic-gate file_pass(void)
2989*0Sstevel@tonic-gate {
2990*0Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
2991*0Sstevel@tonic-gate 	struct Lnk *ttl_p;
2992*0Sstevel@tonic-gate 	char *save_name;
2993*0Sstevel@tonic-gate 	int size;
2994*0Sstevel@tonic-gate 	int cwd;
2995*0Sstevel@tonic-gate 	char *lfrom, *lto;
2996*0Sstevel@tonic-gate 
2997*0Sstevel@tonic-gate 	G_p = &Gen;
2998*0Sstevel@tonic-gate 
2999*0Sstevel@tonic-gate 	if (Adir && !(Args & OCd)) {
3000*0Sstevel@tonic-gate 		msg(ERR, "Use -d option to copy \"%s\"", G_p->g_nam_p);
3001*0Sstevel@tonic-gate 		return (FILE_PASS_ERR);
3002*0Sstevel@tonic-gate 	}
3003*0Sstevel@tonic-gate 
3004*0Sstevel@tonic-gate 	save_name = G_p->g_nam_p;
3005*0Sstevel@tonic-gate 
3006*0Sstevel@tonic-gate 	while (*(G_p->g_nam_p) == '/') {
3007*0Sstevel@tonic-gate 		G_p->g_nam_p++;
3008*0Sstevel@tonic-gate 	}
3009*0Sstevel@tonic-gate 
3010*0Sstevel@tonic-gate 	(void) strcpy(Full_p,
3011*0Sstevel@tonic-gate 	    (G_p->g_attrfnam_p == (char *)NULL) ?
3012*0Sstevel@tonic-gate 	    G_p->g_nam_p : G_p->g_attrfnam_p);
3013*0Sstevel@tonic-gate 
3014*0Sstevel@tonic-gate 	if (G_p->g_attrnam_p == (char *)NULL) {
3015*0Sstevel@tonic-gate 		G_p->g_passdirfd = open_dir(Fullnam_p);
3016*0Sstevel@tonic-gate 
3017*0Sstevel@tonic-gate 		if (G_p->g_passdirfd == -1) {
3018*0Sstevel@tonic-gate 			msg(ERRN,
3019*0Sstevel@tonic-gate 			    "Cannot open/create \"%s\"", Fullnam_p);
3020*0Sstevel@tonic-gate 			return (FILE_PASS_ERR);
3021*0Sstevel@tonic-gate 		}
3022*0Sstevel@tonic-gate 	} else {
3023*0Sstevel@tonic-gate 		G_p->g_passdirfd = attropen(Fullnam_p, ".", O_RDONLY);
3024*0Sstevel@tonic-gate 
3025*0Sstevel@tonic-gate 		if (G_p->g_passdirfd == -1) {
3026*0Sstevel@tonic-gate 			G_p->g_passdirfd = retry_attrdir_open(Fullnam_p);
3027*0Sstevel@tonic-gate 
3028*0Sstevel@tonic-gate 			if (G_p->g_passdirfd == -1) {
3029*0Sstevel@tonic-gate 				msg(ERRN,
3030*0Sstevel@tonic-gate 				    "Cannot open attribute directory of"
3031*0Sstevel@tonic-gate 				    " file \"%s\"", Fullnam_p);
3032*0Sstevel@tonic-gate 				return (FILE_PASS_ERR);
3033*0Sstevel@tonic-gate 			}
3034*0Sstevel@tonic-gate 		}
3035*0Sstevel@tonic-gate 	}
3036*0Sstevel@tonic-gate 
3037*0Sstevel@tonic-gate 	if (Args & OCl) {
3038*0Sstevel@tonic-gate 		/* We are linking back to the source directory. */
3039*0Sstevel@tonic-gate 
3040*0Sstevel@tonic-gate 		if (!Adir) {
3041*0Sstevel@tonic-gate 			char *existingfile = save_name;
3042*0Sstevel@tonic-gate 
3043*0Sstevel@tonic-gate 			if ((Args & OCL) && issymlink) {
3044*0Sstevel@tonic-gate 				/* We are chasing symlinks. */
3045*0Sstevel@tonic-gate 
3046*0Sstevel@tonic-gate 				if ((size = readlink(save_name, Symlnk_p,
3047*0Sstevel@tonic-gate 				    MAXPATHLEN)) < 0) {
3048*0Sstevel@tonic-gate 					msg(ERRN,
3049*0Sstevel@tonic-gate 					    "Cannot read symbolic link \"%s\"",
3050*0Sstevel@tonic-gate 					    save_name);
3051*0Sstevel@tonic-gate 					return (FILE_PASS_ERR);
3052*0Sstevel@tonic-gate 				}
3053*0Sstevel@tonic-gate 
3054*0Sstevel@tonic-gate 				Symlnk_p[size] = '\0';
3055*0Sstevel@tonic-gate 				existingfile = Symlnk_p;
3056*0Sstevel@tonic-gate 			}
3057*0Sstevel@tonic-gate 
3058*0Sstevel@tonic-gate 			if (G_p->g_attrnam_p == (char *)NULL) {
3059*0Sstevel@tonic-gate 				if (creat_lnk(G_p->g_passdirfd,
3060*0Sstevel@tonic-gate 				    existingfile, Fullnam_p) == 0) {
3061*0Sstevel@tonic-gate 					return (FILE_LINKED);
3062*0Sstevel@tonic-gate 				}
3063*0Sstevel@tonic-gate 			}
3064*0Sstevel@tonic-gate 		}
3065*0Sstevel@tonic-gate 	}
3066*0Sstevel@tonic-gate 
3067*0Sstevel@tonic-gate 	if ((G_p->g_mode & Ftype) == S_IFLNK && !(Args & OCL)) {
3068*0Sstevel@tonic-gate 		/* The archive file is a symlink. */
3069*0Sstevel@tonic-gate 
3070*0Sstevel@tonic-gate 		errno = 0;
3071*0Sstevel@tonic-gate 
3072*0Sstevel@tonic-gate 		if ((size = readlink(save_name, Symlnk_p, MAXPATHLEN)) < 0) {
3073*0Sstevel@tonic-gate 			msg(ERRN,
3074*0Sstevel@tonic-gate 			    "Cannot read symbolic link \"%s\"", save_name);
3075*0Sstevel@tonic-gate 			return (FILE_PASS_ERR);
3076*0Sstevel@tonic-gate 		}
3077*0Sstevel@tonic-gate 
3078*0Sstevel@tonic-gate 		errno = 0;
3079*0Sstevel@tonic-gate 		(void) missdir(Fullnam_p);
3080*0Sstevel@tonic-gate 		*(Symlnk_p + size) = '\0';
3081*0Sstevel@tonic-gate 
3082*0Sstevel@tonic-gate 		if (symlink(Symlnk_p, Fullnam_p) < 0) {
3083*0Sstevel@tonic-gate 			if (errno == EEXIST) {
3084*0Sstevel@tonic-gate 				if (openout(G_p->g_passdirfd) < 0) {
3085*0Sstevel@tonic-gate 					return (FILE_PASS_ERR);
3086*0Sstevel@tonic-gate 				}
3087*0Sstevel@tonic-gate 			} else {
3088*0Sstevel@tonic-gate 				msg(ERRN, "Cannot create \"%s\"", Fullnam_p);
3089*0Sstevel@tonic-gate 				return (FILE_PASS_ERR);
3090*0Sstevel@tonic-gate 			}
3091*0Sstevel@tonic-gate 		}
3092*0Sstevel@tonic-gate 
3093*0Sstevel@tonic-gate 		if (lchown(Fullnam_p, (int)G_p->g_uid, (int)G_p->g_gid) < 0) {
3094*0Sstevel@tonic-gate 			if ((Euid == 0) || (Args & OCR)) {
3095*0Sstevel@tonic-gate 				msg(ERRN,
3096*0Sstevel@tonic-gate 				    "Error during chown() of \"%s\"",
3097*0Sstevel@tonic-gate 				    Fullnam_p);
3098*0Sstevel@tonic-gate 			}
3099*0Sstevel@tonic-gate 		}
3100*0Sstevel@tonic-gate 
3101*0Sstevel@tonic-gate 		VERBOSE((Args & (OCv | OCV)), Fullnam_p);
3102*0Sstevel@tonic-gate 		return (FILE_PASS_ERR);
3103*0Sstevel@tonic-gate 	}
3104*0Sstevel@tonic-gate 
3105*0Sstevel@tonic-gate 	if (!Adir && G_p->g_nlink > 1) {
3106*0Sstevel@tonic-gate 		/* The archive file has hard links. */
3107*0Sstevel@tonic-gate 
3108*0Sstevel@tonic-gate 		tl_p = add_lnk(&ttl_p);
3109*0Sstevel@tonic-gate 		l_p = ttl_p;
3110*0Sstevel@tonic-gate 
3111*0Sstevel@tonic-gate 		if (tl_p == l_p) {
3112*0Sstevel@tonic-gate 			/* The archive file was not found. */
3113*0Sstevel@tonic-gate 
3114*0Sstevel@tonic-gate 			G_p = &tl_p->L_gen;
3115*0Sstevel@tonic-gate 		} else {
3116*0Sstevel@tonic-gate 			/* The archive file was found. */
3117*0Sstevel@tonic-gate 
3118*0Sstevel@tonic-gate 			cwd = -1;
3119*0Sstevel@tonic-gate 
3120*0Sstevel@tonic-gate 			if (l_p->L_gen.g_attrnam_p != (char *)NULL) {
3121*0Sstevel@tonic-gate 				/* We are linking an attribute */
3122*0Sstevel@tonic-gate 
3123*0Sstevel@tonic-gate 				(void) strcpy(Lnkend_p, l_p->L_gen.g_attrnam_p);
3124*0Sstevel@tonic-gate 				cwd = save_cwd();
3125*0Sstevel@tonic-gate 				(void) fchdir(G_p->g_passdirfd);
3126*0Sstevel@tonic-gate 				lfrom = get_component(Lnknam_p);
3127*0Sstevel@tonic-gate 				lto = tl_p->L_gen.g_attrnam_p;
3128*0Sstevel@tonic-gate 			} else {
3129*0Sstevel@tonic-gate 				/* We are not linking an attribute */
3130*0Sstevel@tonic-gate 
3131*0Sstevel@tonic-gate 				(void) strcpy(Lnkend_p, l_p->L_gen.g_nam_p);
3132*0Sstevel@tonic-gate 				(void) strcpy(Full_p, tl_p->L_gen.g_nam_p);
3133*0Sstevel@tonic-gate 				lfrom = Lnknam_p;
3134*0Sstevel@tonic-gate 				lto = Fullnam_p;
3135*0Sstevel@tonic-gate 			}
3136*0Sstevel@tonic-gate 
3137*0Sstevel@tonic-gate 			(void) creat_lnk(G_p->g_passdirfd, lfrom, lto);
3138*0Sstevel@tonic-gate 
3139*0Sstevel@tonic-gate 			if (cwd) {
3140*0Sstevel@tonic-gate 				rest_cwd(cwd);
3141*0Sstevel@tonic-gate 			}
3142*0Sstevel@tonic-gate 
3143*0Sstevel@tonic-gate 			l_p->L_lnk_p = (struct Lnk *)NULL;
3144*0Sstevel@tonic-gate 			free(tl_p->L_gen.g_nam_p);
3145*0Sstevel@tonic-gate 			free(tl_p);
3146*0Sstevel@tonic-gate 
3147*0Sstevel@tonic-gate 			if (l_p->L_cnt == G_p->g_nlink) {
3148*0Sstevel@tonic-gate 				reclaim(l_p);
3149*0Sstevel@tonic-gate 			}
3150*0Sstevel@tonic-gate 
3151*0Sstevel@tonic-gate 			return (FILE_LINKED);
3152*0Sstevel@tonic-gate 		}
3153*0Sstevel@tonic-gate 	}
3154*0Sstevel@tonic-gate 
3155*0Sstevel@tonic-gate 	if (Adir || Aspec) {
3156*0Sstevel@tonic-gate 		/*
3157*0Sstevel@tonic-gate 		 * The archive file is a directory,  block special, char
3158*0Sstevel@tonic-gate 		 * special or a fifo.
3159*0Sstevel@tonic-gate 		 */
3160*0Sstevel@tonic-gate 
3161*0Sstevel@tonic-gate 		if (creat_spec(G_p->g_passdirfd) > 0) {
3162*0Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), Fullnam_p);
3163*0Sstevel@tonic-gate 		}
3164*0Sstevel@tonic-gate 	} else if ((Ofile = openout(G_p->g_passdirfd)) > 0) {
3165*0Sstevel@tonic-gate 		data_pass();
3166*0Sstevel@tonic-gate 	}
3167*0Sstevel@tonic-gate 
3168*0Sstevel@tonic-gate 	return (FILE_COPIED);
3169*0Sstevel@tonic-gate }
3170*0Sstevel@tonic-gate 
3171*0Sstevel@tonic-gate /*
3172*0Sstevel@tonic-gate  * flush_lnks: With new linked file handling, linked files are not archived
3173*0Sstevel@tonic-gate  * until all links have been collected.  When the end of the list of filenames
3174*0Sstevel@tonic-gate  * to archive has been reached, all files that did not encounter all their links
3175*0Sstevel@tonic-gate  * are written out with actual (encountered) link counts.  A file with n links
3176*0Sstevel@tonic-gate  * (that are archived) will be represented by n headers (one for each link (the
3177*0Sstevel@tonic-gate  * first n - 1 have g_filesz set to 0)) followed by the data for the file.
3178*0Sstevel@tonic-gate  */
3179*0Sstevel@tonic-gate 
3180*0Sstevel@tonic-gate static void
3181*0Sstevel@tonic-gate flush_lnks(void)
3182*0Sstevel@tonic-gate {
3183*0Sstevel@tonic-gate 	struct Lnk *l_p, *tl_p;
3184*0Sstevel@tonic-gate 	off_t tfsize;
3185*0Sstevel@tonic-gate 
3186*0Sstevel@tonic-gate 	l_p = Lnk_hd.L_nxt_p;
3187*0Sstevel@tonic-gate 	while (l_p != &Lnk_hd) {
3188*0Sstevel@tonic-gate 		(void) strcpy(Gen.g_nam_p, l_p->L_gen.g_nam_p);
3189*0Sstevel@tonic-gate 		if (stat(Gen.g_nam_p, &SrcSt) == 0) { /* check if file exists */
3190*0Sstevel@tonic-gate 			tl_p = l_p;
3191*0Sstevel@tonic-gate 			(void) creat_hdr();
3192*0Sstevel@tonic-gate 			Gen.g_nlink = l_p->L_cnt; /* "actual" link count */
3193*0Sstevel@tonic-gate 			tfsize = Gen.g_filesz;
3194*0Sstevel@tonic-gate 			Gen.g_filesz = (off_t)0;
3195*0Sstevel@tonic-gate 			G_p = &Gen;
3196*0Sstevel@tonic-gate 			while (tl_p != (struct Lnk *)NULL) {
3197*0Sstevel@tonic-gate 				Gen.g_nam_p = tl_p->L_gen.g_nam_p;
3198*0Sstevel@tonic-gate 				Gen.g_namesz = tl_p->L_gen.g_namesz;
3199*0Sstevel@tonic-gate 				if (tl_p->L_lnk_p == (struct Lnk *)NULL) {
3200*0Sstevel@tonic-gate 					Gen.g_filesz = tfsize;
3201*0Sstevel@tonic-gate 					if (open_dirfd() != 0) {
3202*0Sstevel@tonic-gate 						break;
3203*0Sstevel@tonic-gate 					}
3204*0Sstevel@tonic-gate 					data_out();
3205*0Sstevel@tonic-gate 					break;
3206*0Sstevel@tonic-gate 				}
3207*0Sstevel@tonic-gate 				write_hdr(ARCHIVE_NORMAL,
3208*0Sstevel@tonic-gate 				    (off_t)0); /* header only */
3209*0Sstevel@tonic-gate 				VERBOSE((Args & (OCv | OCV)), Gen.g_nam_p);
3210*0Sstevel@tonic-gate 				tl_p = tl_p->L_lnk_p;
3211*0Sstevel@tonic-gate 			}
3212*0Sstevel@tonic-gate 			Gen.g_nam_p = Nam_p;
3213*0Sstevel@tonic-gate 		} else /* stat(Gen.g_nam_p, &SrcSt) == 0 */
3214*0Sstevel@tonic-gate 			msg(ERR, "\"%s%s%s\" has disappeared",
3215*0Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
3216*0Sstevel@tonic-gate 			    Gen.g_nam_p : Gen.g_attrfnam_p,
3217*0Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
3218*0Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
3219*0Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
3220*0Sstevel@tonic-gate 			    "" : Gen.g_attrnam_p);
3221*0Sstevel@tonic-gate 		tl_p = l_p;
3222*0Sstevel@tonic-gate 		l_p = l_p->L_nxt_p;
3223*0Sstevel@tonic-gate 		reclaim(tl_p);
3224*0Sstevel@tonic-gate 	} /* l_p != &Lnk_hd */
3225*0Sstevel@tonic-gate }
3226*0Sstevel@tonic-gate 
3227*0Sstevel@tonic-gate /*
3228*0Sstevel@tonic-gate  * gethdr: Get a header from the archive, validate it and check for the trailer.
3229*0Sstevel@tonic-gate  * Any user specified Hdr_type is ignored (set to NONE in main).  Hdr_type is
3230*0Sstevel@tonic-gate  * set appropriately after a valid header is found.  Unless the -k option is
3231*0Sstevel@tonic-gate  * set a corrupted header causes an exit with an error.  I/O errors during
3232*0Sstevel@tonic-gate  * examination of any part of the header cause gethdr to throw away any current
3233*0Sstevel@tonic-gate  * data and start over.  Other errors during examination of any part of the
3234*0Sstevel@tonic-gate  * header cause gethdr to advance one byte and continue the examination.
3235*0Sstevel@tonic-gate  */
3236*0Sstevel@tonic-gate 
3237*0Sstevel@tonic-gate static int
3238*0Sstevel@tonic-gate gethdr(void)
3239*0Sstevel@tonic-gate {
3240*0Sstevel@tonic-gate 	ushort_t ftype;
3241*0Sstevel@tonic-gate 	int hit = NONE, cnt = 0;
3242*0Sstevel@tonic-gate 	int goodhdr, hsize, offset;
3243*0Sstevel@tonic-gate 	int bswap = 0;
3244*0Sstevel@tonic-gate 	char *preptr;
3245*0Sstevel@tonic-gate 	int k = 0;
3246*0Sstevel@tonic-gate 	int j;
3247*0Sstevel@tonic-gate 
3248*0Sstevel@tonic-gate 	Gen.g_nam_p = Nam_p;
3249*0Sstevel@tonic-gate 	do { /* hit == NONE && (Args & OCk) && Buffr.b_cnt > 0 */
3250*0Sstevel@tonic-gate 		FILL(Hdrsz);
3251*0Sstevel@tonic-gate 		switch (Hdr_type) {
3252*0Sstevel@tonic-gate 		case NONE:
3253*0Sstevel@tonic-gate 		case BIN:
3254*0Sstevel@tonic-gate 			Binmag.b_byte[0] = Buffr.b_out_p[0];
3255*0Sstevel@tonic-gate 			Binmag.b_byte[1] = Buffr.b_out_p[1];
3256*0Sstevel@tonic-gate 			if ((Binmag.b_half == CMN_BIN) ||
3257*0Sstevel@tonic-gate 			    (Binmag.b_half == CMN_BBS)) {
3258*0Sstevel@tonic-gate 				hit = read_hdr(BIN);
3259*0Sstevel@tonic-gate 				if (Hdr_type == NONE)
3260*0Sstevel@tonic-gate 					bswap = 1;
3261*0Sstevel@tonic-gate 				hsize = HDRSZ + Gen.g_namesz;
3262*0Sstevel@tonic-gate 				break;
3263*0Sstevel@tonic-gate 			}
3264*0Sstevel@tonic-gate 			if (Hdr_type != NONE)
3265*0Sstevel@tonic-gate 				break;
3266*0Sstevel@tonic-gate 			/*FALLTHROUGH*/
3267*0Sstevel@tonic-gate 		case CHR:
3268*0Sstevel@tonic-gate 			if (!(strncmp(Buffr.b_out_p, CMS_CHR, CMS_LEN))) {
3269*0Sstevel@tonic-gate 				hit = read_hdr(CHR);
3270*0Sstevel@tonic-gate 				hsize = CHRSZ + Gen.g_namesz;
3271*0Sstevel@tonic-gate 				break;
3272*0Sstevel@tonic-gate 			}
3273*0Sstevel@tonic-gate 			if (Hdr_type != NONE)
3274*0Sstevel@tonic-gate 				break;
3275*0Sstevel@tonic-gate 			/*FALLTHROUGH*/
3276*0Sstevel@tonic-gate 		case ASC:
3277*0Sstevel@tonic-gate 			if (!(strncmp(Buffr.b_out_p, CMS_ASC, CMS_LEN))) {
3278*0Sstevel@tonic-gate 				hit = read_hdr(ASC);
3279*0Sstevel@tonic-gate 				hsize = ASCSZ + Gen.g_namesz;
3280*0Sstevel@tonic-gate 				Max_namesz = APATH;
3281*0Sstevel@tonic-gate 				break;
3282*0Sstevel@tonic-gate 			}
3283*0Sstevel@tonic-gate 			if (Hdr_type != NONE)
3284*0Sstevel@tonic-gate 				break;
3285*0Sstevel@tonic-gate 			/*FALLTHROUGH*/
3286*0Sstevel@tonic-gate 		case CRC:
3287*0Sstevel@tonic-gate 			if (!(strncmp(Buffr.b_out_p, CMS_CRC, CMS_LEN))) {
3288*0Sstevel@tonic-gate 				hit = read_hdr(CRC);
3289*0Sstevel@tonic-gate 				hsize = ASCSZ + Gen.g_namesz;
3290*0Sstevel@tonic-gate 				Max_namesz = APATH;
3291*0Sstevel@tonic-gate 				break;
3292*0Sstevel@tonic-gate 			}
3293*0Sstevel@tonic-gate 			if (Hdr_type != NONE)
3294*0Sstevel@tonic-gate 				break;
3295*0Sstevel@tonic-gate 			/*FALLTHROUGH*/
3296*0Sstevel@tonic-gate 
3297*0Sstevel@tonic-gate 		case BAR:
3298*0Sstevel@tonic-gate 			if (Hdr_p != NULL && strcmp(Hdr_p, "bar") == 0) {
3299*0Sstevel@tonic-gate 				Hdrsz = BARSZ;
3300*0Sstevel@tonic-gate 				FILL(Hdrsz);
3301*0Sstevel@tonic-gate 				if ((hit = read_hdr(BAR)) == NONE) {
3302*0Sstevel@tonic-gate 					Hdrsz = ASCSZ;
3303*0Sstevel@tonic-gate 					break;
3304*0Sstevel@tonic-gate 				}
3305*0Sstevel@tonic-gate 				hit = BAR;
3306*0Sstevel@tonic-gate 				hsize = BARSZ;
3307*0Sstevel@tonic-gate 				break;
3308*0Sstevel@tonic-gate 			}
3309*0Sstevel@tonic-gate 			/*FALLTHROUGH*/
3310*0Sstevel@tonic-gate 
3311*0Sstevel@tonic-gate 		case USTAR:
3312*0Sstevel@tonic-gate 			if (Hdr_p != NULL && strcmp(Hdr_p, "ustar") == 0) {
3313*0Sstevel@tonic-gate 				Hdrsz = TARSZ;
3314*0Sstevel@tonic-gate 				FILL(Hdrsz);
3315*0Sstevel@tonic-gate 				if ((hit = read_hdr(USTAR)) == NONE) {
3316*0Sstevel@tonic-gate 					Hdrsz = ASCSZ;
3317*0Sstevel@tonic-gate 					break;
3318*0Sstevel@tonic-gate 				}
3319*0Sstevel@tonic-gate 				hit = USTAR;
3320*0Sstevel@tonic-gate 				hsize = TARSZ;
3321*0Sstevel@tonic-gate 				break;
3322*0Sstevel@tonic-gate 			}
3323*0Sstevel@tonic-gate 			/*FALLTHROUGH*/
3324*0Sstevel@tonic-gate 		case TAR:
3325*0Sstevel@tonic-gate 			if (Hdr_p != NULL && strcmp(Hdr_p, "tar") == 0) {
3326*0Sstevel@tonic-gate 				Hdrsz = TARSZ;
3327*0Sstevel@tonic-gate 				FILL(Hdrsz);
3328*0Sstevel@tonic-gate 				if ((hit = read_hdr(TAR)) == NONE) {
3329*0Sstevel@tonic-gate 					Hdrsz = ASCSZ;
3330*0Sstevel@tonic-gate 					break;
3331*0Sstevel@tonic-gate 				}
3332*0Sstevel@tonic-gate 				hit = TAR;
3333*0Sstevel@tonic-gate 				hsize = TARSZ;
3334*0Sstevel@tonic-gate 				break;
3335*0Sstevel@tonic-gate 			}
3336*0Sstevel@tonic-gate 			/*FALLTHROUGH*/
3337*0Sstevel@tonic-gate 		default:
3338*0Sstevel@tonic-gate 			msg(EXT, "Impossible header type.");
3339*0Sstevel@tonic-gate 		} /* Hdr_type */
3340*0Sstevel@tonic-gate 
3341*0Sstevel@tonic-gate 		if (hit == TAR || hit == USTAR) {
3342*0Sstevel@tonic-gate 			Gen.g_nam_p = &nambuf[0];
3343*0Sstevel@tonic-gate 		}
3344*0Sstevel@tonic-gate 
3345*0Sstevel@tonic-gate 		if (hit != NONE) {
3346*0Sstevel@tonic-gate 			FILL(hsize);
3347*0Sstevel@tonic-gate 			goodhdr = 1;
3348*0Sstevel@tonic-gate 			if (Gen.g_filesz < (off_t)0 || Gen.g_namesz < 1)
3349*0Sstevel@tonic-gate 				goodhdr = 0;
3350*0Sstevel@tonic-gate 			if ((hit != USTAR) && (hit != TAR))
3351*0Sstevel@tonic-gate 				if (Gen.g_namesz - 1 > Max_namesz)
3352*0Sstevel@tonic-gate 					goodhdr = 0;
3353*0Sstevel@tonic-gate 			/* TAR and USTAR */
3354*0Sstevel@tonic-gate 			if ((hit == USTAR) || (hit == TAR)) {
3355*0Sstevel@tonic-gate 				if (*Gen.g_nam_p == '\0') { /* tar trailer */
3356*0Sstevel@tonic-gate 					goodhdr = 1;
3357*0Sstevel@tonic-gate 				} else {
3358*0Sstevel@tonic-gate 
3359*0Sstevel@tonic-gate 					G_p = &Gen;
3360*0Sstevel@tonic-gate 					if (G_p->g_cksum !=
3361*0Sstevel@tonic-gate 					    cksum(TARTYP, 0, NULL)) {
3362*0Sstevel@tonic-gate 						goodhdr = 0;
3363*0Sstevel@tonic-gate 						msg(ERR,
3364*0Sstevel@tonic-gate 						    "Bad header - checksum "
3365*0Sstevel@tonic-gate 						    "error.");
3366*0Sstevel@tonic-gate 					}
3367*0Sstevel@tonic-gate 				}
3368*0Sstevel@tonic-gate 			} else if (hit != BAR) { /* binary, -c, ASC and CRC */
3369*0Sstevel@tonic-gate 				if (Gen.g_nlink <= (ulong_t)0)
3370*0Sstevel@tonic-gate 					goodhdr = 0;
3371*0Sstevel@tonic-gate 				if (*(Buffr.b_out_p + hsize - 1) != '\0')
3372*0Sstevel@tonic-gate 					goodhdr = 0;
3373*0Sstevel@tonic-gate 			}
3374*0Sstevel@tonic-gate 			if (!goodhdr) {
3375*0Sstevel@tonic-gate 				hit = NONE;
3376*0Sstevel@tonic-gate 				if (!(Args & OCk))
3377*0Sstevel@tonic-gate 					break;
3378*0Sstevel@tonic-gate 				msg(ERR,
3379*0Sstevel@tonic-gate 				    "Corrupt header, file(s) may be lost.");
3380*0Sstevel@tonic-gate 			} else {
3381*0Sstevel@tonic-gate 				FILL(hsize);
3382*0Sstevel@tonic-gate 			}
3383*0Sstevel@tonic-gate 		} /* hit != NONE */
3384*0Sstevel@tonic-gate 		if (hit == NONE) {
3385*0Sstevel@tonic-gate 			Buffr.b_out_p++;
3386*0Sstevel@tonic-gate 			Buffr.b_cnt--;
3387*0Sstevel@tonic-gate 			if (!(Args & OCk))
3388*0Sstevel@tonic-gate 				break;
3389*0Sstevel@tonic-gate 			if (!cnt++)
3390*0Sstevel@tonic-gate 				msg(ERR, "Searching for magic number/header.");
3391*0Sstevel@tonic-gate 		}
3392*0Sstevel@tonic-gate 	} while (hit == NONE);
3393*0Sstevel@tonic-gate 	if (hit == NONE) {
3394*0Sstevel@tonic-gate 		if (Hdr_type == NONE)
3395*0Sstevel@tonic-gate 			msg(EXT, "Not a cpio file, bad header.");
3396*0Sstevel@tonic-gate 		else
3397*0Sstevel@tonic-gate 			msg(EXT, "Bad magic number/header.");
3398*0Sstevel@tonic-gate 	} else if (cnt > 0) {
3399*0Sstevel@tonic-gate 		msg(EPOST, "Re-synchronized on magic number/header.");
3400*0Sstevel@tonic-gate 	}
3401*0Sstevel@tonic-gate 	if (Hdr_type == NONE) {
3402*0Sstevel@tonic-gate 		Hdr_type = hit;
3403*0Sstevel@tonic-gate 		switch (Hdr_type) {
3404*0Sstevel@tonic-gate 		case BIN:
3405*0Sstevel@tonic-gate 			if (bswap)
3406*0Sstevel@tonic-gate 				Args |= BSM;
3407*0Sstevel@tonic-gate 			Hdrsz = HDRSZ;
3408*0Sstevel@tonic-gate 			Max_namesz = CPATH;
3409*0Sstevel@tonic-gate 			Pad_val = HALFWD;
3410*0Sstevel@tonic-gate 			Onecopy = 0;
3411*0Sstevel@tonic-gate 			break;
3412*0Sstevel@tonic-gate 		case CHR:
3413*0Sstevel@tonic-gate 			Hdrsz = CHRSZ;
3414*0Sstevel@tonic-gate 			Max_namesz = CPATH;
3415*0Sstevel@tonic-gate 			Pad_val = 0;
3416*0Sstevel@tonic-gate 			Onecopy = 0;
3417*0Sstevel@tonic-gate 			break;
3418*0Sstevel@tonic-gate 		case ASC:
3419*0Sstevel@tonic-gate 		case CRC:
3420*0Sstevel@tonic-gate 			Hdrsz = ASCSZ;
3421*0Sstevel@tonic-gate 			Max_namesz = APATH;
3422*0Sstevel@tonic-gate 			Pad_val = FULLWD;
3423*0Sstevel@tonic-gate 			Onecopy = 1;
3424*0Sstevel@tonic-gate 			break;
3425*0Sstevel@tonic-gate 		case USTAR:
3426*0Sstevel@tonic-gate 			Hdrsz = TARSZ;
3427*0Sstevel@tonic-gate 			Max_namesz = HNAMLEN - 1;
3428*0Sstevel@tonic-gate 			Pad_val = FULLBK;
3429*0Sstevel@tonic-gate 			Onecopy = 0;
3430*0Sstevel@tonic-gate 			break;
3431*0Sstevel@tonic-gate 		case BAR:
3432*0Sstevel@tonic-gate 		case TAR:
3433*0Sstevel@tonic-gate 			Hdrsz = TARSZ;
3434*0Sstevel@tonic-gate 			Max_namesz = TNAMLEN - 1;
3435*0Sstevel@tonic-gate 			Pad_val = FULLBK;
3436*0Sstevel@tonic-gate 			Onecopy = 0;
3437*0Sstevel@tonic-gate 			break;
3438*0Sstevel@tonic-gate 		default:
3439*0Sstevel@tonic-gate 			msg(EXT, "Impossible header type.");
3440*0Sstevel@tonic-gate 		} /* Hdr_type */
3441*0Sstevel@tonic-gate 	} /* Hdr_type == NONE */
3442*0Sstevel@tonic-gate 	if ((Hdr_type == USTAR) || (Hdr_type == TAR) ||
3443*0Sstevel@tonic-gate 	    (Hdr_type == BAR)) {			/* TAR, USTAR, BAR */
3444*0Sstevel@tonic-gate 		Gen.g_namesz = 0;
3445*0Sstevel@tonic-gate 		if (Gen.g_nam_p[0] == '\0')
3446*0Sstevel@tonic-gate 			return (0);
3447*0Sstevel@tonic-gate 		else {
3448*0Sstevel@tonic-gate 			preptr = &prebuf[0];
3449*0Sstevel@tonic-gate 			if (*preptr != (char)NULL) {
3450*0Sstevel@tonic-gate 				k = strlen(&prebuf[0]);
3451*0Sstevel@tonic-gate 				if (k < PRESIZ) {
3452*0Sstevel@tonic-gate 					(void) strcpy(&fullnam[0], &prebuf[0]);
3453*0Sstevel@tonic-gate 					j = 0;
3454*0Sstevel@tonic-gate 					fullnam[k++] = '/';
3455*0Sstevel@tonic-gate 					while ((j < NAMSIZ) && (&nambuf[j] !=
3456*0Sstevel@tonic-gate 					    (char)NULL)) {
3457*0Sstevel@tonic-gate 						fullnam[k] = nambuf[j];
3458*0Sstevel@tonic-gate 						k++; j++;
3459*0Sstevel@tonic-gate 					}
3460*0Sstevel@tonic-gate 					fullnam[k] = '\0';
3461*0Sstevel@tonic-gate 				} else if (k >= PRESIZ) {
3462*0Sstevel@tonic-gate 					k = 0;
3463*0Sstevel@tonic-gate 					while ((k < PRESIZ) && (prebuf[k] !=
3464*0Sstevel@tonic-gate 					    '\0')) {
3465*0Sstevel@tonic-gate 						fullnam[k] = prebuf[k];
3466*0Sstevel@tonic-gate 						k++;
3467*0Sstevel@tonic-gate 					}
3468*0Sstevel@tonic-gate 					fullnam[k++] = '/';
3469*0Sstevel@tonic-gate 					j = 0;
3470*0Sstevel@tonic-gate 					while ((j < NAMSIZ) && (nambuf[j] !=
3471*0Sstevel@tonic-gate 					    '\0')) {
3472*0Sstevel@tonic-gate 						fullnam[k] = nambuf[j];
3473*0Sstevel@tonic-gate 						k++; j++;
3474*0Sstevel@tonic-gate 					}
3475*0Sstevel@tonic-gate 					fullnam[k] = '\0';
3476*0Sstevel@tonic-gate 				}
3477*0Sstevel@tonic-gate 				Gen.g_nam_p = &fullnam[0];
3478*0Sstevel@tonic-gate 			} else
3479*0Sstevel@tonic-gate 				Gen.g_nam_p = &nambuf[0];
3480*0Sstevel@tonic-gate 
3481*0Sstevel@tonic-gate 			/*
3482*0Sstevel@tonic-gate 			 * initialize the buffer so that the prefix will not
3483*0Sstevel@tonic-gate 			 * applied to the next entry in the archive
3484*0Sstevel@tonic-gate 			 */
3485*0Sstevel@tonic-gate 			(void) memset(prebuf, 0, sizeof (prebuf));
3486*0Sstevel@tonic-gate 		}
3487*0Sstevel@tonic-gate 	} else if (Hdr_type != BAR) {
3488*0Sstevel@tonic-gate 		(void) memcpy(Gen.g_nam_p, Buffr.b_out_p + Hdrsz, Gen.g_namesz);
3489*0Sstevel@tonic-gate 		if (!(strcmp(Gen.g_nam_p, "TRAILER!!!")))
3490*0Sstevel@tonic-gate 			return (0);
3491*0Sstevel@tonic-gate 	}
3492*0Sstevel@tonic-gate 	offset = ((hsize + Pad_val) & ~Pad_val);
3493*0Sstevel@tonic-gate 	FILL(offset + Hdrsz);
3494*0Sstevel@tonic-gate 	Thdr_p = (union tblock *)Buffr.b_out_p;
3495*0Sstevel@tonic-gate 	Buffr.b_out_p += offset;
3496*0Sstevel@tonic-gate 	Buffr.b_cnt -= (off_t)offset;
3497*0Sstevel@tonic-gate 	ftype = Gen.g_mode & Ftype;
3498*0Sstevel@tonic-gate 
3499*0Sstevel@tonic-gate #if defined(O_XATTR)
3500*0Sstevel@tonic-gate 	/* extended attribute support */
3501*0Sstevel@tonic-gate 	if (((Gen.g_mode & S_IFMT) == _XATTR_CPIO_MODE) ||
3502*0Sstevel@tonic-gate 	    ((Hdr_type == USTAR || Hdr_type == TAR) &&
3503*0Sstevel@tonic-gate 	    Thdr_p->tbuf.t_typeflag == _XATTR_HDRTYPE)) {
3504*0Sstevel@tonic-gate 		if (xattrp != (struct xattr_buf *)NULL) {
3505*0Sstevel@tonic-gate 			if (xattrbadhead) {
3506*0Sstevel@tonic-gate 				free(xattrhead);
3507*0Sstevel@tonic-gate 				xattrp = NULL;
3508*0Sstevel@tonic-gate 				xattr_linkp = NULL;
3509*0Sstevel@tonic-gate 				xattrhead = NULL;
3510*0Sstevel@tonic-gate 				return (1);
3511*0Sstevel@tonic-gate 			}
3512*0Sstevel@tonic-gate 			if (Atflag == 0 && ((Args & OCt) == 0)) {
3513*0Sstevel@tonic-gate 				data_in(P_SKIP);
3514*0Sstevel@tonic-gate 				free(xattrhead);
3515*0Sstevel@tonic-gate 				xattrhead = NULL;
3516*0Sstevel@tonic-gate 				xattrp = NULL;
3517*0Sstevel@tonic-gate 				Gen.g_attrfnam_p = (char *)NULL;
3518*0Sstevel@tonic-gate 				Gen.g_attrnam_p = (char *)NULL;
3519*0Sstevel@tonic-gate 				return (2);
3520*0Sstevel@tonic-gate 			}
3521*0Sstevel@tonic-gate 
3522*0Sstevel@tonic-gate 			if (Gen.g_attrfnam_p != (char *)NULL) {
3523*0Sstevel@tonic-gate 				free(Gen.g_attrfnam_p);
3524*0Sstevel@tonic-gate 				Gen.g_attrfnam_p = (char *)NULL;
3525*0Sstevel@tonic-gate 			}
3526*0Sstevel@tonic-gate 			if (Gen.g_attrnam_p != (char *)NULL) {
3527*0Sstevel@tonic-gate 				free(Gen.g_attrnam_p);
3528*0Sstevel@tonic-gate 				Gen.g_attrnam_p = (char *)NULL;
3529*0Sstevel@tonic-gate 			}
3530*0Sstevel@tonic-gate 			if (Renam_p && Renam_p[0] != '\0') {
3531*0Sstevel@tonic-gate 				Gen.g_attrfnam_p = e_strdup(E_EXIT, Renam_p);
3532*0Sstevel@tonic-gate 			} else {
3533*0Sstevel@tonic-gate 				Gen.g_attrfnam_p = e_strdup(E_EXIT,
3534*0Sstevel@tonic-gate 				    xattrp->h_names);
3535*0Sstevel@tonic-gate 			}
3536*0Sstevel@tonic-gate 
3537*0Sstevel@tonic-gate 			Gen.g_attrnam_p = e_strdup(E_EXIT,
3538*0Sstevel@tonic-gate 			    xattrp->h_names + strlen(xattrp->h_names) + 1);
3539*0Sstevel@tonic-gate 
3540*0Sstevel@tonic-gate 			if (Hdr_type != USTAR && Hdr_type != TAR) {
3541*0Sstevel@tonic-gate 				Gen.g_mode = Gen.g_mode & (~_XATTR_CPIO_MODE);
3542*0Sstevel@tonic-gate 				Gen.g_mode |= attrmode(xattrp->h_typeflag);
3543*0Sstevel@tonic-gate 
3544*0Sstevel@tonic-gate 			} else if (Hdr_type == USTAR || Hdr_type == TAR) {
3545*0Sstevel@tonic-gate 				Thdr_p->tbuf.t_typeflag = xattrp->h_typeflag;
3546*0Sstevel@tonic-gate 			}
3547*0Sstevel@tonic-gate 			if (xattr_linkp != (struct xattr_buf *)NULL) {
3548*0Sstevel@tonic-gate 				if (Gen.g_linktoattrfnam_p != (char *)NULL) {
3549*0Sstevel@tonic-gate 					free(Gen.g_linktoattrfnam_p);
3550*0Sstevel@tonic-gate 					Gen.g_linktoattrfnam_p = NULL;
3551*0Sstevel@tonic-gate 				}
3552*0Sstevel@tonic-gate 				if (Gen.g_linktoattrnam_p != (char *)NULL) {
3553*0Sstevel@tonic-gate 					free(Gen.g_linktoattrnam_p);
3554*0Sstevel@tonic-gate 					Gen.g_linktoattrnam_p = NULL;
3555*0Sstevel@tonic-gate 				}
3556*0Sstevel@tonic-gate 				Gen.g_linktoattrfnam_p = e_strdup(E_EXIT,
3557*0Sstevel@tonic-gate 				    xattr_linkp->h_names);
3558*0Sstevel@tonic-gate 				Gen.g_linktoattrnam_p = e_strdup(E_EXIT,
3559*0Sstevel@tonic-gate 				    xattr_linkp->h_names +
3560*0Sstevel@tonic-gate 				    strlen(xattr_linkp->h_names) + 1);
3561*0Sstevel@tonic-gate 				xattr_linkp = NULL;
3562*0Sstevel@tonic-gate 			}
3563*0Sstevel@tonic-gate 			ftype = Gen.g_mode & Ftype;
3564*0Sstevel@tonic-gate 			Adir = ftype == S_IFDIR;
3565*0Sstevel@tonic-gate 			Aspec = (ftype == S_IFBLK ||
3566*0Sstevel@tonic-gate 			    ftype == S_IFCHR || ftype == S_IFIFO);
3567*0Sstevel@tonic-gate 
3568*0Sstevel@tonic-gate 			if (Gen.g_attrnam_p[0] == '.' &&
3569*0Sstevel@tonic-gate 			    Gen.g_attrnam_p[1] == '\0' &&
3570*0Sstevel@tonic-gate 			    xattrp->h_typeflag == DIRTYPE) {
3571*0Sstevel@tonic-gate 				Hiddendir = 1;
3572*0Sstevel@tonic-gate 			} else {
3573*0Sstevel@tonic-gate 				Hiddendir = 0;
3574*0Sstevel@tonic-gate 			}
3575*0Sstevel@tonic-gate 
3576*0Sstevel@tonic-gate 			free(xattrhead);
3577*0Sstevel@tonic-gate 			xattrhead = NULL;
3578*0Sstevel@tonic-gate 			xattrp = NULL;
3579*0Sstevel@tonic-gate 		} else {
3580*0Sstevel@tonic-gate 			if (xattrbadhead == 0) {
3581*0Sstevel@tonic-gate 				(void) read_xattr_hdr();
3582*0Sstevel@tonic-gate 				return (2);
3583*0Sstevel@tonic-gate 			}
3584*0Sstevel@tonic-gate 		}
3585*0Sstevel@tonic-gate 	}
3586*0Sstevel@tonic-gate #endif /* O_XATTR */
3587*0Sstevel@tonic-gate 
3588*0Sstevel@tonic-gate 	/* acl support: grab acl info */
3589*0Sstevel@tonic-gate 	if ((Gen.g_mode == SECMODE) || ((Hdr_type == USTAR ||
3590*0Sstevel@tonic-gate 	    Hdr_type == TAR) && Thdr_p->tbuf.t_typeflag == 'A')) {
3591*0Sstevel@tonic-gate 		/* this is an ancillary file */
3592*0Sstevel@tonic-gate 		off_t	bytes;
3593*0Sstevel@tonic-gate 		char	*secp;
3594*0Sstevel@tonic-gate 		int	pad;
3595*0Sstevel@tonic-gate 		int	cnt;
3596*0Sstevel@tonic-gate 		char	*tp;
3597*0Sstevel@tonic-gate 		int	attrsize;
3598*0Sstevel@tonic-gate 
3599*0Sstevel@tonic-gate 		if (Pflag) {
3600*0Sstevel@tonic-gate 			bytes = Gen.g_filesz;
3601*0Sstevel@tonic-gate 			secp = e_zalloc(E_EXIT, (uint_t)bytes);
3602*0Sstevel@tonic-gate 			tp = secp;
3603*0Sstevel@tonic-gate 
3604*0Sstevel@tonic-gate 			while (bytes > 0) {
3605*0Sstevel@tonic-gate 				cnt = (int)(bytes > CPIOBSZ) ? CPIOBSZ : bytes;
3606*0Sstevel@tonic-gate 				FILL(cnt);
3607*0Sstevel@tonic-gate 				(void) memcpy(tp, Buffr.b_out_p, cnt);
3608*0Sstevel@tonic-gate 				tp += cnt;
3609*0Sstevel@tonic-gate 				Buffr.b_out_p += cnt;
3610*0Sstevel@tonic-gate 				Buffr.b_cnt -= (off_t)cnt;
3611*0Sstevel@tonic-gate 				bytes -= (off_t)cnt;
3612*0Sstevel@tonic-gate 			}
3613*0Sstevel@tonic-gate 
3614*0Sstevel@tonic-gate 			pad = (Pad_val + 1 - (Gen.g_filesz & Pad_val)) &
3615*0Sstevel@tonic-gate 			    Pad_val;
3616*0Sstevel@tonic-gate 			if (pad != 0) {
3617*0Sstevel@tonic-gate 				FILL(pad);
3618*0Sstevel@tonic-gate 				Buffr.b_out_p += pad;
3619*0Sstevel@tonic-gate 				Buffr.b_cnt -= (off_t)pad;
3620*0Sstevel@tonic-gate 			}
3621*0Sstevel@tonic-gate 
3622*0Sstevel@tonic-gate 			/* got all attributes in secp */
3623*0Sstevel@tonic-gate 			tp = secp;
3624*0Sstevel@tonic-gate 			do {
3625*0Sstevel@tonic-gate 				attr = (struct sec_attr *)tp;
3626*0Sstevel@tonic-gate 				switch (attr->attr_type) {
3627*0Sstevel@tonic-gate 				case UFSD_ACL:
3628*0Sstevel@tonic-gate 					(void) sscanf(attr->attr_len, "%7lo",
3629*0Sstevel@tonic-gate 					    (ulong_t *)&aclcnt);
3630*0Sstevel@tonic-gate 				/* header is 8 */
3631*0Sstevel@tonic-gate 					attrsize = 8 +
3632*0Sstevel@tonic-gate 					    strlen(&attr->attr_info[0])
3633*0Sstevel@tonic-gate 					    + 1;
3634*0Sstevel@tonic-gate 					aclp = aclfromtext(&attr->attr_info[0],
3635*0Sstevel@tonic-gate 					    &cnt);
3636*0Sstevel@tonic-gate 					if (aclp == NULL) {
3637*0Sstevel@tonic-gate 						msg(ERR, "aclfromtext failed");
3638*0Sstevel@tonic-gate 						break;
3639*0Sstevel@tonic-gate 					}
3640*0Sstevel@tonic-gate 					if (aclcnt != cnt) {
3641*0Sstevel@tonic-gate 						msg(ERR, "acl count error");
3642*0Sstevel@tonic-gate 						break;
3643*0Sstevel@tonic-gate 					}
3644*0Sstevel@tonic-gate 					bytes -= attrsize;
3645*0Sstevel@tonic-gate 					break;
3646*0Sstevel@tonic-gate 
3647*0Sstevel@tonic-gate 				/* SunFed case goes here */
3648*0Sstevel@tonic-gate 
3649*0Sstevel@tonic-gate 				default:
3650*0Sstevel@tonic-gate 					msg(EXT, "unrecognized attr type");
3651*0Sstevel@tonic-gate 					break;
3652*0Sstevel@tonic-gate 			}
3653*0Sstevel@tonic-gate 			/* next attributes */
3654*0Sstevel@tonic-gate 			tp += attrsize;
3655*0Sstevel@tonic-gate 			} while (bytes > 0);
3656*0Sstevel@tonic-gate 			free(secp);
3657*0Sstevel@tonic-gate 		} else {
3658*0Sstevel@tonic-gate 			/* skip security info */
3659*0Sstevel@tonic-gate 			G_p = &Gen;
3660*0Sstevel@tonic-gate 			data_in(P_SKIP);
3661*0Sstevel@tonic-gate 		}
3662*0Sstevel@tonic-gate 		/*
3663*0Sstevel@tonic-gate 		 * We already got the file content, dont call file_in()
3664*0Sstevel@tonic-gate 		 * when return. The new return code(2) is used to
3665*0Sstevel@tonic-gate 		 *  indicate that.
3666*0Sstevel@tonic-gate 		 */
3667*0Sstevel@tonic-gate 		VERBOSE((Args & OCt), Gen.g_nam_p);
3668*0Sstevel@tonic-gate 		return (2);
3669*0Sstevel@tonic-gate 	} /* acl */
3670*0Sstevel@tonic-gate 
3671*0Sstevel@tonic-gate 	Adir = (ftype == S_IFDIR);
3672*0Sstevel@tonic-gate 	Aspec = (ftype == S_IFBLK || ftype == S_IFCHR || ftype == S_IFIFO);
3673*0Sstevel@tonic-gate 
3674*0Sstevel@tonic-gate 	/*
3675*0Sstevel@tonic-gate 	 * Skip any trailing slashes
3676*0Sstevel@tonic-gate 	 */
3677*0Sstevel@tonic-gate 	chop_endslashes(Gen.g_nam_p);
3678*0Sstevel@tonic-gate 	return (1);
3679*0Sstevel@tonic-gate }
3680*0Sstevel@tonic-gate 
3681*0Sstevel@tonic-gate /*
3682*0Sstevel@tonic-gate  * getname: Get file names for inclusion in the archive.  When end of file
3683*0Sstevel@tonic-gate  * on the input stream of file names is reached, flush the link buffer out.
3684*0Sstevel@tonic-gate  * For each filename, remove leading "./"s and multiple "/"s, and remove
3685*0Sstevel@tonic-gate  * any trailing newline "\n".  Finally, verify the existance of the file,
3686*0Sstevel@tonic-gate  * and call creat_hdr() to fill in the gen_hdr structure.
3687*0Sstevel@tonic-gate  */
3688*0Sstevel@tonic-gate 
3689*0Sstevel@tonic-gate static int
3690*0Sstevel@tonic-gate getname(void)
3691*0Sstevel@tonic-gate {
3692*0Sstevel@tonic-gate 	int goodfile = 0, lastchar, err;
3693*0Sstevel@tonic-gate 	char *s;
3694*0Sstevel@tonic-gate 	char *dir;
3695*0Sstevel@tonic-gate 
3696*0Sstevel@tonic-gate 	Gen.g_nam_p = Nam_p;
3697*0Sstevel@tonic-gate 
3698*0Sstevel@tonic-gate 	while (!goodfile) {
3699*0Sstevel@tonic-gate 		err = 0;
3700*0Sstevel@tonic-gate 
3701*0Sstevel@tonic-gate 		while ((s = fgets(Gen.g_nam_p, APATH+1, In_p))
3702*0Sstevel@tonic-gate 		    != NULL) {
3703*0Sstevel@tonic-gate 			lastchar = strlen(s) - 1;
3704*0Sstevel@tonic-gate 			issymlink = 0;
3705*0Sstevel@tonic-gate 
3706*0Sstevel@tonic-gate 			if (s[lastchar] != '\n') {
3707*0Sstevel@tonic-gate 				if (lastchar == APATH - 1) {
3708*0Sstevel@tonic-gate 					if (!err) {
3709*0Sstevel@tonic-gate 						msg(ERR,
3710*0Sstevel@tonic-gate 						    "%s name too long.",
3711*0Sstevel@tonic-gate 						    Nam_p);
3712*0Sstevel@tonic-gate 					}
3713*0Sstevel@tonic-gate 					goodfile = 0;
3714*0Sstevel@tonic-gate 					err = 1;
3715*0Sstevel@tonic-gate 				} else {
3716*0Sstevel@tonic-gate 					break;
3717*0Sstevel@tonic-gate 				}
3718*0Sstevel@tonic-gate 			} else {
3719*0Sstevel@tonic-gate 				s[lastchar] = '\0';
3720*0Sstevel@tonic-gate 				break;
3721*0Sstevel@tonic-gate 			}
3722*0Sstevel@tonic-gate 		}
3723*0Sstevel@tonic-gate 
3724*0Sstevel@tonic-gate 		if (s == (char *)NULL) {
3725*0Sstevel@tonic-gate 			if (Gen.g_dirfd != -1) {
3726*0Sstevel@tonic-gate 				(void) close(Gen.g_dirfd);
3727*0Sstevel@tonic-gate 				Gen.g_dirfd = -1;
3728*0Sstevel@tonic-gate 			}
3729*0Sstevel@tonic-gate 			if (Onecopy && (Args & OCo)) {
3730*0Sstevel@tonic-gate 				flush_lnks();
3731*0Sstevel@tonic-gate 			}
3732*0Sstevel@tonic-gate 			return (0);
3733*0Sstevel@tonic-gate 		}
3734*0Sstevel@tonic-gate 
3735*0Sstevel@tonic-gate 		while (*Gen.g_nam_p == '.' && Gen.g_nam_p[1] == '/') {
3736*0Sstevel@tonic-gate 			Gen.g_nam_p += 2;
3737*0Sstevel@tonic-gate 			while (*Gen.g_nam_p == '/')
3738*0Sstevel@tonic-gate 				Gen.g_nam_p++;
3739*0Sstevel@tonic-gate 		}
3740*0Sstevel@tonic-gate 
3741*0Sstevel@tonic-gate 		/*
3742*0Sstevel@tonic-gate 		 * Skip any trailing slashes
3743*0Sstevel@tonic-gate 		 */
3744*0Sstevel@tonic-gate 		chop_endslashes(Gen.g_nam_p);
3745*0Sstevel@tonic-gate 
3746*0Sstevel@tonic-gate 		/*
3747*0Sstevel@tonic-gate 		 * Figure out parent directory
3748*0Sstevel@tonic-gate 		 */
3749*0Sstevel@tonic-gate 
3750*0Sstevel@tonic-gate 		if (Gen.g_attrnam_p != (char *)NULL) {
3751*0Sstevel@tonic-gate 			if (Gen.g_dirfd != -1) {
3752*0Sstevel@tonic-gate 				(void) close(Gen.g_dirfd);
3753*0Sstevel@tonic-gate 			}
3754*0Sstevel@tonic-gate 			Gen.g_dirfd = attropen(Gen.g_attrfnam_p, ".", O_RDONLY);
3755*0Sstevel@tonic-gate 			if (Gen.g_dirfd == -1) {
3756*0Sstevel@tonic-gate 				msg(ERRN,
3757*0Sstevel@tonic-gate 				    "Cannot open attribute directory"
3758*0Sstevel@tonic-gate 				    " of file %s", Gen.g_attrfnam_p);
3759*0Sstevel@tonic-gate 				continue;
3760*0Sstevel@tonic-gate 			}
3761*0Sstevel@tonic-gate 		} else {
3762*0Sstevel@tonic-gate #ifdef O_XATTR
3763*0Sstevel@tonic-gate 			char dirpath[PATH_MAX];
3764*0Sstevel@tonic-gate 
3765*0Sstevel@tonic-gate 			get_parent(Gen.g_nam_p, dirpath);
3766*0Sstevel@tonic-gate 			if (Atflag) {
3767*0Sstevel@tonic-gate 				dir = dirpath;
3768*0Sstevel@tonic-gate 				if (Gen.g_dirfd != -1) {
3769*0Sstevel@tonic-gate 					(void) close(Gen.g_dirfd);
3770*0Sstevel@tonic-gate 				}
3771*0Sstevel@tonic-gate 				Gen.g_dirfd = open(dir, O_RDONLY);
3772*0Sstevel@tonic-gate 				if (Gen.g_dirfd == -1) {
3773*0Sstevel@tonic-gate 					msg(ERRN,
3774*0Sstevel@tonic-gate 					    "Cannot open directory %s", dir);
3775*0Sstevel@tonic-gate 					continue;
3776*0Sstevel@tonic-gate 				}
3777*0Sstevel@tonic-gate 			} else {
3778*0Sstevel@tonic-gate 				/*
3779*0Sstevel@tonic-gate 				 * g_dirpath is the pathname cache maintaining
3780*0Sstevel@tonic-gate 				 * the dirname which is currently opened.
3781*0Sstevel@tonic-gate 				 * We first check the g_dirpath to see if the
3782*0Sstevel@tonic-gate 				 * given dirname matches. If so, we don't need
3783*0Sstevel@tonic-gate 				 * to open the dir, but we can use the g_dirfd
3784*0Sstevel@tonic-gate 				 * as is if it is still available.
3785*0Sstevel@tonic-gate 				 */
3786*0Sstevel@tonic-gate 				dir = NULL;
3787*0Sstevel@tonic-gate 				if (Gen.g_dirpath == NULL ||
3788*0Sstevel@tonic-gate 				    Gen.g_dirfd == -1) {
3789*0Sstevel@tonic-gate 					/*
3790*0Sstevel@tonic-gate 					 * It's the first time or it has
3791*0Sstevel@tonic-gate 					 * all gone.
3792*0Sstevel@tonic-gate 					 */
3793*0Sstevel@tonic-gate 					dir = e_strdup(E_EXIT, dirpath);
3794*0Sstevel@tonic-gate 				} else {
3795*0Sstevel@tonic-gate 					if (strcmp(Gen.g_dirpath,
3796*0Sstevel@tonic-gate 					    dirpath) != 0) {
3797*0Sstevel@tonic-gate 						/* different directory */
3798*0Sstevel@tonic-gate 						dir = e_strdup(E_EXIT, dirpath);
3799*0Sstevel@tonic-gate 					}
3800*0Sstevel@tonic-gate 				}
3801*0Sstevel@tonic-gate 				if (dir != NULL) {
3802*0Sstevel@tonic-gate 					/*
3803*0Sstevel@tonic-gate 					 * We need to open the new directory.
3804*0Sstevel@tonic-gate 					 * discard the pathname and dirfd
3805*0Sstevel@tonic-gate 					 * for the previous directory.
3806*0Sstevel@tonic-gate 					 */
3807*0Sstevel@tonic-gate 					if (Gen.g_dirpath != NULL) {
3808*0Sstevel@tonic-gate 						free(Gen.g_dirpath);
3809*0Sstevel@tonic-gate 						Gen.g_dirpath = NULL;
3810*0Sstevel@tonic-gate 					}
3811*0Sstevel@tonic-gate 					if (Gen.g_dirfd != -1) {
3812*0Sstevel@tonic-gate 						(void) close(Gen.g_dirfd);
3813*0Sstevel@tonic-gate 					}
3814*0Sstevel@tonic-gate 					/* open the new dir */
3815*0Sstevel@tonic-gate 					Gen.g_dirfd = open(dir, O_RDONLY);
3816*0Sstevel@tonic-gate 					if (Gen.g_dirfd == -1) {
3817*0Sstevel@tonic-gate 						msg(ERRN, "Cannot open "
3818*0Sstevel@tonic-gate 							"directory %s", dir);
3819*0Sstevel@tonic-gate 						continue;
3820*0Sstevel@tonic-gate 					}
3821*0Sstevel@tonic-gate 					Gen.g_dirpath = dir;
3822*0Sstevel@tonic-gate 				}
3823*0Sstevel@tonic-gate 			}
3824*0Sstevel@tonic-gate #else
3825*0Sstevel@tonic-gate 			Gen.g_dirfd = -1;
3826*0Sstevel@tonic-gate #endif
3827*0Sstevel@tonic-gate 		}
3828*0Sstevel@tonic-gate 
3829*0Sstevel@tonic-gate 		/* creat_hdr checks for USTAR filename length */
3830*0Sstevel@tonic-gate 
3831*0Sstevel@tonic-gate 		if (Hdr_type != USTAR && strlen(Gen.g_nam_p) >
3832*0Sstevel@tonic-gate 		    Max_namesz) {
3833*0Sstevel@tonic-gate 			if (!err) {
3834*0Sstevel@tonic-gate 				msg(ERR, "%s%s%s name too long.",
3835*0Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
3836*0Sstevel@tonic-gate 				    Nam_p : Gen.g_attrfnam_p,
3837*0Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
3838*0Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
3839*0Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
3840*0Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
3841*0Sstevel@tonic-gate 			}
3842*0Sstevel@tonic-gate 			goodfile = 0;
3843*0Sstevel@tonic-gate 			err = 1;
3844*0Sstevel@tonic-gate 		}
3845*0Sstevel@tonic-gate 
3846*0Sstevel@tonic-gate 		if (err) {
3847*0Sstevel@tonic-gate 			continue;
3848*0Sstevel@tonic-gate 		} else {
3849*0Sstevel@tonic-gate 			G_p = &Gen;
3850*0Sstevel@tonic-gate 			if (!LSTAT(Gen.g_dirfd, Gen.g_nam_p, &SrcSt)) {
3851*0Sstevel@tonic-gate 				goodfile = 1;
3852*0Sstevel@tonic-gate 
3853*0Sstevel@tonic-gate 				if ((SrcSt.st_mode & Ftype) == S_IFLNK) {
3854*0Sstevel@tonic-gate 					issymlink = 1;
3855*0Sstevel@tonic-gate 
3856*0Sstevel@tonic-gate 					if ((Args & OCL)) {
3857*0Sstevel@tonic-gate 						errno = 0;
3858*0Sstevel@tonic-gate 						if (STAT(Gen.g_dirfd,
3859*0Sstevel@tonic-gate 						    G_p->g_nam_p,
3860*0Sstevel@tonic-gate 						    &SrcSt) < 0) {
3861*0Sstevel@tonic-gate 							msg(ERRN,
3862*0Sstevel@tonic-gate 							    "Cannot follow"
3863*0Sstevel@tonic-gate 							    " \"%s%s%s\"",
3864*0Sstevel@tonic-gate 							    (Gen.g_attrnam_p ==
3865*0Sstevel@tonic-gate 							    (char *)NULL) ?
3866*0Sstevel@tonic-gate 							    Gen.g_nam_p :
3867*0Sstevel@tonic-gate 							    Gen.g_attrfnam_p,
3868*0Sstevel@tonic-gate 							    (Gen.g_attrnam_p ==
3869*0Sstevel@tonic-gate 							    (char *)NULL) ?
3870*0Sstevel@tonic-gate 							    "" :
3871*0Sstevel@tonic-gate 							    gettext(
3872*0Sstevel@tonic-gate 							    " Attribute "),
3873*0Sstevel@tonic-gate 							    (Gen.g_attrnam_p ==
3874*0Sstevel@tonic-gate 							    (char *)NULL) ?
3875*0Sstevel@tonic-gate 							    "" :
3876*0Sstevel@tonic-gate 							    Gen.g_attrnam_p);
3877*0Sstevel@tonic-gate 							goodfile = 0;
3878*0Sstevel@tonic-gate 						}
3879*0Sstevel@tonic-gate 					}
3880*0Sstevel@tonic-gate 				}
3881*0Sstevel@tonic-gate 
3882*0Sstevel@tonic-gate 				if (Use_old_stat) {
3883*0Sstevel@tonic-gate 					OldSt = convert_to_old_stat(&SrcSt,
3884*0Sstevel@tonic-gate 					    Gen.g_nam_p, Gen.g_attrnam_p);
3885*0Sstevel@tonic-gate 
3886*0Sstevel@tonic-gate 					if (OldSt == NULL) {
3887*0Sstevel@tonic-gate 						goodfile = 0;
3888*0Sstevel@tonic-gate 					}
3889*0Sstevel@tonic-gate 				}
3890*0Sstevel@tonic-gate 			} else {
3891*0Sstevel@tonic-gate 				msg(ERRN,
3892*0Sstevel@tonic-gate 				    "Error with fstatat() of \"%s%s%s\"",
3893*0Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
3894*0Sstevel@tonic-gate 				    Gen.g_nam_p : Gen.g_attrfnam_p,
3895*0Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
3896*0Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
3897*0Sstevel@tonic-gate 				    (Gen.g_attrnam_p == (char *)NULL) ?
3898*0Sstevel@tonic-gate 				    "" : Gen.g_attrnam_p);
3899*0Sstevel@tonic-gate 			}
3900*0Sstevel@tonic-gate 		}
3901*0Sstevel@tonic-gate 	}
3902*0Sstevel@tonic-gate 
3903*0Sstevel@tonic-gate 	/*
3904*0Sstevel@tonic-gate 	 * Get ACL info: dont bother allocating space if there are only
3905*0Sstevel@tonic-gate 	 * standard permissions, i.e. ACL count < 4
3906*0Sstevel@tonic-gate 	 */
3907*0Sstevel@tonic-gate 	if ((SrcSt.st_mode & Ftype) != S_IFLNK && Pflag) {
3908*0Sstevel@tonic-gate 		if ((aclcnt = acl(Gen.g_nam_p, GETACLCNT, 0, NULL)) < 0)
3909*0Sstevel@tonic-gate 			msg(ERRN, "Error with acl() of \"%s\"", Gen.g_nam_p);
3910*0Sstevel@tonic-gate 		if (aclcnt > MIN_ACL_ENTRIES) {
3911*0Sstevel@tonic-gate 			aclp = e_zalloc(E_EXIT, sizeof (aclent_t) * aclcnt);
3912*0Sstevel@tonic-gate 
3913*0Sstevel@tonic-gate 			if (acl(Gen.g_nam_p, GETACL, aclcnt, aclp) < 0) {
3914*0Sstevel@tonic-gate 				msg(ERRN,
3915*0Sstevel@tonic-gate 				    "Error with getacl() of \"%s\"",
3916*0Sstevel@tonic-gate 				    Gen.g_nam_p);
3917*0Sstevel@tonic-gate 				free(aclp);
3918*0Sstevel@tonic-gate 				aclp = NULL;
3919*0Sstevel@tonic-gate 			}
3920*0Sstevel@tonic-gate 		}
3921*0Sstevel@tonic-gate 	/* else: only traditional permissions, so proceed as usual */
3922*0Sstevel@tonic-gate 	}
3923*0Sstevel@tonic-gate 	if (creat_hdr())
3924*0Sstevel@tonic-gate 		return (1);
3925*0Sstevel@tonic-gate 	else return (2);
3926*0Sstevel@tonic-gate }
3927*0Sstevel@tonic-gate 
3928*0Sstevel@tonic-gate /*
3929*0Sstevel@tonic-gate  * getpats: Save any filenames/patterns specified as arguments.
3930*0Sstevel@tonic-gate  * Read additional filenames/patterns from the file specified by the
3931*0Sstevel@tonic-gate  * user.  The filenames/patterns must occur one per line.
3932*0Sstevel@tonic-gate  */
3933*0Sstevel@tonic-gate 
3934*0Sstevel@tonic-gate static void
3935*0Sstevel@tonic-gate getpats(int largc, char **largv)
3936*0Sstevel@tonic-gate {
3937*0Sstevel@tonic-gate 	char **t_pp;
3938*0Sstevel@tonic-gate 	size_t len;
3939*0Sstevel@tonic-gate 	unsigned numpat = largc, maxpat = largc + 2;
3940*0Sstevel@tonic-gate 
3941*0Sstevel@tonic-gate 	Pat_pp = e_zalloc(E_EXIT, maxpat * sizeof (char *));
3942*0Sstevel@tonic-gate 	t_pp = Pat_pp;
3943*0Sstevel@tonic-gate 	while (*largv) {
3944*0Sstevel@tonic-gate 		*t_pp = e_zalloc(E_EXIT, strlen(*largv) + 1);
3945*0Sstevel@tonic-gate 		(void) strcpy(*t_pp, *largv);
3946*0Sstevel@tonic-gate 		t_pp++;
3947*0Sstevel@tonic-gate 		largv++;
3948*0Sstevel@tonic-gate 	}
3949*0Sstevel@tonic-gate 	while (fgets(Nam_p, Max_namesz + 1, Ef_p) != (char *)NULL) {
3950*0Sstevel@tonic-gate 		if (numpat == maxpat - 1) {
3951*0Sstevel@tonic-gate 			maxpat += 10;
3952*0Sstevel@tonic-gate 			Pat_pp = e_realloc(E_EXIT, Pat_pp,
3953*0Sstevel@tonic-gate 			    maxpat * sizeof (char *));
3954*0Sstevel@tonic-gate 			t_pp = Pat_pp + numpat;
3955*0Sstevel@tonic-gate 		}
3956*0Sstevel@tonic-gate 		len = strlen(Nam_p); /* includes the \n */
3957*0Sstevel@tonic-gate 		*(Nam_p + len - 1) = '\0'; /* remove the \n */
3958*0Sstevel@tonic-gate 		*t_pp = e_zalloc(E_EXIT, len);
3959*0Sstevel@tonic-gate 		(void) strcpy(*t_pp, Nam_p);
3960*0Sstevel@tonic-gate 		t_pp++;
3961*0Sstevel@tonic-gate 		numpat++;
3962*0Sstevel@tonic-gate 	}
3963*0Sstevel@tonic-gate 	*t_pp = (char *)NULL;
3964*0Sstevel@tonic-gate }
3965*0Sstevel@tonic-gate 
3966*0Sstevel@tonic-gate static void
3967*0Sstevel@tonic-gate ioerror(int dir)
3968*0Sstevel@tonic-gate {
3969*0Sstevel@tonic-gate 	int t_errno;
3970*0Sstevel@tonic-gate 
3971*0Sstevel@tonic-gate 	t_errno = errno;
3972*0Sstevel@tonic-gate 	errno = 0;
3973*0Sstevel@tonic-gate 	if (fstat(Archive, &ArchSt) < 0)
3974*0Sstevel@tonic-gate 		msg(EXTN, "Error during stat() of archive");
3975*0Sstevel@tonic-gate 	errno = t_errno;
3976*0Sstevel@tonic-gate 	if ((ArchSt.st_mode & Ftype) != S_IFCHR) {
3977*0Sstevel@tonic-gate 		if (dir) {
3978*0Sstevel@tonic-gate 			if (errno == EFBIG)
3979*0Sstevel@tonic-gate 				msg(EXT, "ulimit reached for output file.");
3980*0Sstevel@tonic-gate 			else if (errno == ENOSPC)
3981*0Sstevel@tonic-gate 				msg(EXT, "No space left for output file.");
3982*0Sstevel@tonic-gate 			else
3983*0Sstevel@tonic-gate 				msg(EXTN, "I/O error - cannot continue");
3984*0Sstevel@tonic-gate 		} else
3985*0Sstevel@tonic-gate 			msg(EXT, "Unexpected end-of-file encountered.");
3986*0Sstevel@tonic-gate 	} else
3987*0Sstevel@tonic-gate 		msg(EXTN, "\007I/O error on \"%s\"", dir ? "output" : "input");
3988*0Sstevel@tonic-gate }
3989*0Sstevel@tonic-gate 
3990*0Sstevel@tonic-gate /*
3991*0Sstevel@tonic-gate  * matched: Determine if a filename matches the specified pattern(s).  If the
3992*0Sstevel@tonic-gate  * pattern is matched (the second return), return 0 if -f was specified, else
3993*0Sstevel@tonic-gate  * return != 0.  If the pattern is not matched (the first and third
3994*0Sstevel@tonic-gate  * returns), return 0 if -f was not specified, else return != 0.
3995*0Sstevel@tonic-gate  */
3996*0Sstevel@tonic-gate 
3997*0Sstevel@tonic-gate static int
3998*0Sstevel@tonic-gate matched(void)
3999*0Sstevel@tonic-gate {
4000*0Sstevel@tonic-gate 	char *str_p = G_p->g_nam_p;
4001*0Sstevel@tonic-gate 	char **pat_pp = Pat_pp;
4002*0Sstevel@tonic-gate 	int negatep, result;
4003*0Sstevel@tonic-gate 
4004*0Sstevel@tonic-gate 	/*
4005*0Sstevel@tonic-gate 	 * Check for attribute
4006*0Sstevel@tonic-gate 	 */
4007*0Sstevel@tonic-gate 	if (G_p->g_attrfnam_p != (char *)NULL)
4008*0Sstevel@tonic-gate 		str_p = G_p->g_attrfnam_p;
4009*0Sstevel@tonic-gate 
4010*0Sstevel@tonic-gate 	for (pat_pp = Pat_pp; *pat_pp; pat_pp++) {
4011*0Sstevel@tonic-gate 		negatep = (**pat_pp == '!');
4012*0Sstevel@tonic-gate 
4013*0Sstevel@tonic-gate 		result = fnmatch(negatep ? (*pat_pp+1) : *pat_pp, str_p, 0);
4014*0Sstevel@tonic-gate 
4015*0Sstevel@tonic-gate 		if (result != 0 && result != FNM_NOMATCH) {
4016*0Sstevel@tonic-gate 			msg(POST, "error matching file %s with pattern"
4017*0Sstevel@tonic-gate 			    " %s\n", str_p, *pat_pp);
4018*0Sstevel@tonic-gate 			return (Args & OCf);
4019*0Sstevel@tonic-gate 		}
4020*0Sstevel@tonic-gate 
4021*0Sstevel@tonic-gate 		if ((result == 0 && ! negatep) ||
4022*0Sstevel@tonic-gate 		    (result == FNM_NOMATCH && negatep)) {
4023*0Sstevel@tonic-gate 			/* match occured */
4024*0Sstevel@tonic-gate 			return (!(Args & OCf));
4025*0Sstevel@tonic-gate 		}
4026*0Sstevel@tonic-gate 	}
4027*0Sstevel@tonic-gate 	return (Args & OCf); /* not matched */
4028*0Sstevel@tonic-gate }
4029*0Sstevel@tonic-gate 
4030*0Sstevel@tonic-gate /*
4031*0Sstevel@tonic-gate  * missdir: Create missing directories for files.
4032*0Sstevel@tonic-gate  * (Possible future performance enhancement, if missdir is called, we know
4033*0Sstevel@tonic-gate  * that at least the very last directory of the path does not exist, therefore,
4034*0Sstevel@tonic-gate  * scan the path from the end
4035*0Sstevel@tonic-gate  */
4036*0Sstevel@tonic-gate 
4037*0Sstevel@tonic-gate static int
4038*0Sstevel@tonic-gate missdir(char *nam_p)
4039*0Sstevel@tonic-gate {
4040*0Sstevel@tonic-gate 	char *c_p;
4041*0Sstevel@tonic-gate 	int cnt = 2;
4042*0Sstevel@tonic-gate 	char *lastp;
4043*0Sstevel@tonic-gate 
4044*0Sstevel@tonic-gate 	if (*(c_p = nam_p) == '/') /* skip over 'root slash' */
4045*0Sstevel@tonic-gate 		c_p++;
4046*0Sstevel@tonic-gate 
4047*0Sstevel@tonic-gate 	lastp = c_p + strlen(nam_p) - 1;
4048*0Sstevel@tonic-gate 	if (*lastp == '/')
4049*0Sstevel@tonic-gate 		*lastp = '\0';
4050*0Sstevel@tonic-gate 
4051*0Sstevel@tonic-gate 	for (; *c_p; ++c_p) {
4052*0Sstevel@tonic-gate 		if (*c_p == '/') {
4053*0Sstevel@tonic-gate 			*c_p = '\0';
4054*0Sstevel@tonic-gate 			if (stat(nam_p, &DesSt) < 0) {
4055*0Sstevel@tonic-gate 				if (Args & OCd) {
4056*0Sstevel@tonic-gate 					cnt = mkdir(nam_p, Def_mode);
4057*0Sstevel@tonic-gate 					if (cnt != 0) {
4058*0Sstevel@tonic-gate 						*c_p = '/';
4059*0Sstevel@tonic-gate 						return (cnt);
4060*0Sstevel@tonic-gate 					}
4061*0Sstevel@tonic-gate 				} else {
4062*0Sstevel@tonic-gate 					msg(ERR, "Missing -d option.");
4063*0Sstevel@tonic-gate 					*c_p = '/';
4064*0Sstevel@tonic-gate 					return (-1);
4065*0Sstevel@tonic-gate 				}
4066*0Sstevel@tonic-gate 			}
4067*0Sstevel@tonic-gate 			*c_p = '/';
4068*0Sstevel@tonic-gate 		}
4069*0Sstevel@tonic-gate 	}
4070*0Sstevel@tonic-gate 	if (cnt == 2) /* the file already exists */
4071*0Sstevel@tonic-gate 		cnt = 0;
4072*0Sstevel@tonic-gate 	return (cnt);
4073*0Sstevel@tonic-gate }
4074*0Sstevel@tonic-gate 
4075*0Sstevel@tonic-gate /*
4076*0Sstevel@tonic-gate  * mklong: Convert two shorts into one long.  For VAX, Interdata ...
4077*0Sstevel@tonic-gate  */
4078*0Sstevel@tonic-gate 
4079*0Sstevel@tonic-gate static long
4080*0Sstevel@tonic-gate mklong(short v[])
4081*0Sstevel@tonic-gate {
4082*0Sstevel@tonic-gate 
4083*0Sstevel@tonic-gate 	union swpbuf swp_b;
4084*0Sstevel@tonic-gate 
4085*0Sstevel@tonic-gate 	swp_b.s_word = 1;
4086*0Sstevel@tonic-gate 	if (swp_b.s_byte[0]) {
4087*0Sstevel@tonic-gate 		swp_b.s_half[0] = v[1];
4088*0Sstevel@tonic-gate 		swp_b.s_half[1] = v[0];
4089*0Sstevel@tonic-gate 	} else {
4090*0Sstevel@tonic-gate 		swp_b.s_half[0] = v[0];
4091*0Sstevel@tonic-gate 		swp_b.s_half[1] = v[1];
4092*0Sstevel@tonic-gate 	}
4093*0Sstevel@tonic-gate 	return (swp_b.s_word);
4094*0Sstevel@tonic-gate }
4095*0Sstevel@tonic-gate 
4096*0Sstevel@tonic-gate /*
4097*0Sstevel@tonic-gate  * mkshort: Convert a long into 2 shorts, for VAX, Interdata ...
4098*0Sstevel@tonic-gate  */
4099*0Sstevel@tonic-gate 
4100*0Sstevel@tonic-gate static void
4101*0Sstevel@tonic-gate mkshort(short sval[], long v)
4102*0Sstevel@tonic-gate {
4103*0Sstevel@tonic-gate 	union swpbuf *swp_p, swp_b;
4104*0Sstevel@tonic-gate 
4105*0Sstevel@tonic-gate 	/* LINTED alignment */
4106*0Sstevel@tonic-gate 	swp_p = (union swpbuf *)sval;
4107*0Sstevel@tonic-gate 	swp_b.s_word = 1;
4108*0Sstevel@tonic-gate 	if (swp_b.s_byte[0]) {
4109*0Sstevel@tonic-gate 		swp_b.s_word = v;
4110*0Sstevel@tonic-gate 		swp_p->s_half[0] = swp_b.s_half[1];
4111*0Sstevel@tonic-gate 		swp_p->s_half[1] = swp_b.s_half[0];
4112*0Sstevel@tonic-gate 	} else {
4113*0Sstevel@tonic-gate 		swp_b.s_word = v;
4114*0Sstevel@tonic-gate 		swp_p->s_half[0] = swp_b.s_half[0];
4115*0Sstevel@tonic-gate 		swp_p->s_half[1] = swp_b.s_half[1];
4116*0Sstevel@tonic-gate 	}
4117*0Sstevel@tonic-gate }
4118*0Sstevel@tonic-gate 
4119*0Sstevel@tonic-gate /*
4120*0Sstevel@tonic-gate  * msg: Print either a message (no error) (POST), an error message with or
4121*0Sstevel@tonic-gate  * without the errno (ERRN or ERR), or print an error message with or without
4122*0Sstevel@tonic-gate  * the errno and exit (EXTN or EXT).
4123*0Sstevel@tonic-gate  */
4124*0Sstevel@tonic-gate 
4125*0Sstevel@tonic-gate static void
4126*0Sstevel@tonic-gate msg(int severity, const char *fmt, ...)
4127*0Sstevel@tonic-gate {
4128*0Sstevel@tonic-gate 	FILE *file_p;
4129*0Sstevel@tonic-gate 	va_list ap;
4130*0Sstevel@tonic-gate 
4131*0Sstevel@tonic-gate 	if ((Args & OCV) && Verbcnt) { /* clear current line of dots */
4132*0Sstevel@tonic-gate 		(void) fputc('\n', Out_p);
4133*0Sstevel@tonic-gate 		Verbcnt = 0;
4134*0Sstevel@tonic-gate 	}
4135*0Sstevel@tonic-gate 	va_start(ap, fmt);
4136*0Sstevel@tonic-gate 	if (severity == POST)
4137*0Sstevel@tonic-gate 		file_p = Out_p;
4138*0Sstevel@tonic-gate 	else
4139*0Sstevel@tonic-gate 		if (severity == EPOST)
4140*0Sstevel@tonic-gate 			file_p = Err_p;
4141*0Sstevel@tonic-gate 		else {
4142*0Sstevel@tonic-gate 			file_p = Err_p;
4143*0Sstevel@tonic-gate 			Error_cnt++;
4144*0Sstevel@tonic-gate 		}
4145*0Sstevel@tonic-gate 	(void) fflush(Out_p);
4146*0Sstevel@tonic-gate 	(void) fflush(Err_p);
4147*0Sstevel@tonic-gate 	if ((severity != POST) && (severity != EPOST))
4148*0Sstevel@tonic-gate 		(void) fprintf(file_p, "cpio: ");
4149*0Sstevel@tonic-gate 
4150*0Sstevel@tonic-gate 	/* gettext replaces version of string */
4151*0Sstevel@tonic-gate 
4152*0Sstevel@tonic-gate 	(void) vfprintf(file_p, gettext(fmt), ap);
4153*0Sstevel@tonic-gate 	if (severity == ERRN || severity == EXTN) {
4154*0Sstevel@tonic-gate 		(void) fprintf(file_p, ", errno %d, ", errno);
4155*0Sstevel@tonic-gate 		perror("");
4156*0Sstevel@tonic-gate 	} else
4157*0Sstevel@tonic-gate 		(void) fprintf(file_p, "\n");
4158*0Sstevel@tonic-gate 	(void) fflush(file_p);
4159*0Sstevel@tonic-gate 	va_end(ap);
4160*0Sstevel@tonic-gate 	if (severity == EXT || severity == EXTN) {
4161*0Sstevel@tonic-gate 		(void) fprintf(file_p, gettext("%d errors\n"), Error_cnt);
4162*0Sstevel@tonic-gate 		exit(EXIT_CODE);
4163*0Sstevel@tonic-gate 	}
4164*0Sstevel@tonic-gate }
4165*0Sstevel@tonic-gate 
4166*0Sstevel@tonic-gate /*
4167*0Sstevel@tonic-gate  * openout: Open files for output and set all necessary information.
4168*0Sstevel@tonic-gate  * If the u option is set (unconditionally overwrite existing files),
4169*0Sstevel@tonic-gate  * and the current file exists, get a temporary file name from mktemp(3C),
4170*0Sstevel@tonic-gate  * link the temporary file to the existing file, and remove the existing file.
4171*0Sstevel@tonic-gate  * Finally either creat(2), mkdir(2) or mknod(2) as appropriate.
4172*0Sstevel@tonic-gate  *
4173*0Sstevel@tonic-gate  */
4174*0Sstevel@tonic-gate 
4175*0Sstevel@tonic-gate static int
4176*0Sstevel@tonic-gate openout(int dirfd)
4177*0Sstevel@tonic-gate {
4178*0Sstevel@tonic-gate 	char *nam_p;
4179*0Sstevel@tonic-gate 	int cnt, result;
4180*0Sstevel@tonic-gate 
4181*0Sstevel@tonic-gate 	Do_rename = 0;	/* creat_tmp() may reset this */
4182*0Sstevel@tonic-gate 
4183*0Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
4184*0Sstevel@tonic-gate 		nam_p = G_p->g_attrnam_p;
4185*0Sstevel@tonic-gate 	} else {
4186*0Sstevel@tonic-gate 		if (Args & OCp) {
4187*0Sstevel@tonic-gate 			nam_p = Fullnam_p;
4188*0Sstevel@tonic-gate 		} else {
4189*0Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
4190*0Sstevel@tonic-gate 		}
4191*0Sstevel@tonic-gate 	}
4192*0Sstevel@tonic-gate 
4193*0Sstevel@tonic-gate 
4194*0Sstevel@tonic-gate 	if ((Max_filesz != RLIM_INFINITY) &&
4195*0Sstevel@tonic-gate 	    (Max_filesz < (G_p->g_filesz >> 9))) {
4196*0Sstevel@tonic-gate 		/* ... divided by 512 ... */
4197*0Sstevel@tonic-gate 		msg(ERR, "Skipping \"%s%s%s\": exceeds ulimit by %lld bytes",
4198*0Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
4199*0Sstevel@tonic-gate 		    nam_p : G_p->g_attrfnam_p,
4200*0Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
4201*0Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
4202*0Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
4203*0Sstevel@tonic-gate 		    "" : nam_p,
4204*0Sstevel@tonic-gate 		    (off_t)(G_p->g_filesz - (Max_filesz << 9)));
4205*0Sstevel@tonic-gate 		return (-1);
4206*0Sstevel@tonic-gate 	}
4207*0Sstevel@tonic-gate 
4208*0Sstevel@tonic-gate 	if (LSTAT(dirfd, nam_p, &DesSt) == 0) {
4209*0Sstevel@tonic-gate 		/*
4210*0Sstevel@tonic-gate 		 * A file by the same name exists.  Move it to a temporary
4211*0Sstevel@tonic-gate 		 * file.
4212*0Sstevel@tonic-gate 		 */
4213*0Sstevel@tonic-gate 
4214*0Sstevel@tonic-gate 		if (creat_tmp(nam_p) < 0) {
4215*0Sstevel@tonic-gate 			/*
4216*0Sstevel@tonic-gate 			 * We weren't able to create the temp file.  Report
4217*0Sstevel@tonic-gate 			 * failure.
4218*0Sstevel@tonic-gate 			 */
4219*0Sstevel@tonic-gate 
4220*0Sstevel@tonic-gate 			return (-1);
4221*0Sstevel@tonic-gate 		}
4222*0Sstevel@tonic-gate 	}
4223*0Sstevel@tonic-gate 
4224*0Sstevel@tonic-gate 	if (Do_rename) {
4225*0Sstevel@tonic-gate 		/* nam_p was changed by creat_tmp() above. */
4226*0Sstevel@tonic-gate 
4227*0Sstevel@tonic-gate 		if (Args & OCp) {
4228*0Sstevel@tonic-gate 			if (G_p->g_attrnam_p != (char *)NULL) {
4229*0Sstevel@tonic-gate 				nam_p = Attrfile_p;
4230*0Sstevel@tonic-gate 			} else {
4231*0Sstevel@tonic-gate 				nam_p = Fullnam_p;
4232*0Sstevel@tonic-gate 			}
4233*0Sstevel@tonic-gate 		} else {
4234*0Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
4235*0Sstevel@tonic-gate 		}
4236*0Sstevel@tonic-gate 	}
4237*0Sstevel@tonic-gate 
4238*0Sstevel@tonic-gate 	/*
4239*0Sstevel@tonic-gate 	 * This pile tries to create the file directly, and, if there is a
4240*0Sstevel@tonic-gate 	 * problem, creates missing directories, and then tries to create the
4241*0Sstevel@tonic-gate 	 * file again.  Two strikes and you're out.
4242*0Sstevel@tonic-gate 	 *
4243*0Sstevel@tonic-gate 	 * On XATTR system, the directory has already been created by
4244*0Sstevel@tonic-gate 	 * open_dirfd(), so error shouldn't happen in the loop. However,
4245*0Sstevel@tonic-gate 	 * on non-XATTR system, symlink/open may fail with ENOENT. In such
4246*0Sstevel@tonic-gate 	 * case, we go to create missing directories.
4247*0Sstevel@tonic-gate 	 */
4248*0Sstevel@tonic-gate 
4249*0Sstevel@tonic-gate 	cnt = 0;
4250*0Sstevel@tonic-gate 
4251*0Sstevel@tonic-gate 	do {
4252*0Sstevel@tonic-gate 		errno = 0;
4253*0Sstevel@tonic-gate 
4254*0Sstevel@tonic-gate 		if (Hdr_type == TAR && Thdr_p->tbuf.t_typeflag == SYMTYPE) {
4255*0Sstevel@tonic-gate 			/* The archive file is a TAR symlink. */
4256*0Sstevel@tonic-gate 			if ((result =
4257*0Sstevel@tonic-gate 			    symlink(Thdr_p->tbuf.t_linkname, nam_p)) >= 0) {
4258*0Sstevel@tonic-gate 				cnt = 0;
4259*0Sstevel@tonic-gate 				if (Over_p != NULL) {
4260*0Sstevel@tonic-gate 					(void) unlinkat(dirfd,
4261*0Sstevel@tonic-gate 					    get_component(Over_p), 0);
4262*0Sstevel@tonic-gate 					*Over_p = '\0';
4263*0Sstevel@tonic-gate 				}
4264*0Sstevel@tonic-gate 				break;
4265*0Sstevel@tonic-gate 			} else if (errno != ENOENT) {
4266*0Sstevel@tonic-gate 				/* The attempt to symlink failed. */
4267*0Sstevel@tonic-gate 				msg(ERRN,
4268*0Sstevel@tonic-gate 				    "Cannot create symbolic link \"%s\" -> "
4269*0Sstevel@tonic-gate 				    "\"%s\"",
4270*0Sstevel@tonic-gate 				    Thdr_p->tbuf.t_linkname, nam_p);
4271*0Sstevel@tonic-gate 
4272*0Sstevel@tonic-gate 				if (*Over_p != '\0') {
4273*0Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
4274*0Sstevel@tonic-gate 				}
4275*0Sstevel@tonic-gate 				return (-1);
4276*0Sstevel@tonic-gate 			}
4277*0Sstevel@tonic-gate 		} else if (Hdr_type == BAR && bar_linkflag == SYMTYPE) {
4278*0Sstevel@tonic-gate 			if ((result = symlink(bar_linkname, nam_p)) >= 0) {
4279*0Sstevel@tonic-gate 				cnt = 0;
4280*0Sstevel@tonic-gate 				if (Over_p != NULL) {
4281*0Sstevel@tonic-gate 					(void) unlinkat(dirfd,
4282*0Sstevel@tonic-gate 					    get_component(Over_p), 0);
4283*0Sstevel@tonic-gate 					*Over_p = '\0';
4284*0Sstevel@tonic-gate 				}
4285*0Sstevel@tonic-gate 				break;
4286*0Sstevel@tonic-gate 			} else if (errno != ENOENT) {
4287*0Sstevel@tonic-gate 				/* The attempt to symlink failed. */
4288*0Sstevel@tonic-gate 				msg(ERRN,
4289*0Sstevel@tonic-gate 				    "Cannot create symbolic link \"%s\" -> "
4290*0Sstevel@tonic-gate 				    "\"%s\"",
4291*0Sstevel@tonic-gate 				    bar_linkname, nam_p);
4292*0Sstevel@tonic-gate 				if (*Over_p != '\0') {
4293*0Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
4294*0Sstevel@tonic-gate 				}
4295*0Sstevel@tonic-gate 				return (-1);
4296*0Sstevel@tonic-gate 			}
4297*0Sstevel@tonic-gate 		} else if ((G_p->g_mode & Ftype) == S_IFLNK) {
4298*0Sstevel@tonic-gate 			if ((!(Args & OCp)) && !(Hdr_type == USTAR)) {
4299*0Sstevel@tonic-gate 				(void) strncpy(Symlnk_p,
4300*0Sstevel@tonic-gate 				    Buffr.b_out_p, G_p->g_filesz);
4301*0Sstevel@tonic-gate 				*(Symlnk_p + G_p->g_filesz) = '\0';
4302*0Sstevel@tonic-gate 			} else if ((!(Args & OCp)) && (Hdr_type == USTAR)) {
4303*0Sstevel@tonic-gate 				Symlnk_p[NAMSIZ] = '\0';
4304*0Sstevel@tonic-gate 				(void) strncpy(Symlnk_p,
4305*0Sstevel@tonic-gate 				    &Thdr_p->tbuf.t_linkname[0], NAMSIZ);
4306*0Sstevel@tonic-gate 			}
4307*0Sstevel@tonic-gate 			if ((result = symlink(Symlnk_p, nam_p)) >= 0) {
4308*0Sstevel@tonic-gate 				cnt = 0;
4309*0Sstevel@tonic-gate 				if (Over_p != NULL) {
4310*0Sstevel@tonic-gate 					(void) unlinkat(dirfd,
4311*0Sstevel@tonic-gate 					    get_component(Over_p), 0);
4312*0Sstevel@tonic-gate 					*Over_p = '\0';
4313*0Sstevel@tonic-gate 				}
4314*0Sstevel@tonic-gate 				break;
4315*0Sstevel@tonic-gate 			} else if (errno != ENOENT) {
4316*0Sstevel@tonic-gate 				/* The attempt to symlink failed. */
4317*0Sstevel@tonic-gate 				msg(ERRN,
4318*0Sstevel@tonic-gate 				    "Cannot create symbolic link \"%s\" -> "
4319*0Sstevel@tonic-gate 				    "\"%s\"",
4320*0Sstevel@tonic-gate 				    Symlnk_p, nam_p);
4321*0Sstevel@tonic-gate 
4322*0Sstevel@tonic-gate 				if (*Over_p != '\0') {
4323*0Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
4324*0Sstevel@tonic-gate 				}
4325*0Sstevel@tonic-gate 				return (-1);
4326*0Sstevel@tonic-gate 			}
4327*0Sstevel@tonic-gate 		} else {
4328*0Sstevel@tonic-gate 			if ((result = openat(dirfd, get_component(nam_p),
4329*0Sstevel@tonic-gate 			    O_CREAT|O_RDWR|O_TRUNC, (int)G_p->g_mode)) >= 0) {
4330*0Sstevel@tonic-gate 				/* acl support */
4331*0Sstevel@tonic-gate 				acl_set = 0;
4332*0Sstevel@tonic-gate 				if (Pflag && aclp != NULL) {
4333*0Sstevel@tonic-gate 					if (facl(result, SETACL, aclcnt, aclp)
4334*0Sstevel@tonic-gate 					    < 0) {
4335*0Sstevel@tonic-gate 						msg(ERRN,
4336*0Sstevel@tonic-gate 						    "\"%s\": failed to set acl",
4337*0Sstevel@tonic-gate 						    nam_p);
4338*0Sstevel@tonic-gate 					} else {
4339*0Sstevel@tonic-gate 						acl_set = 1;
4340*0Sstevel@tonic-gate 					}
4341*0Sstevel@tonic-gate 					free(aclp);
4342*0Sstevel@tonic-gate 					aclp = NULL;
4343*0Sstevel@tonic-gate 				}
4344*0Sstevel@tonic-gate 				cnt = 0;
4345*0Sstevel@tonic-gate 				break;
4346*0Sstevel@tonic-gate 			} else if (errno != ENOENT) {
4347*0Sstevel@tonic-gate 				/* The attempt to open failed. */
4348*0Sstevel@tonic-gate 				msg(ERRN, "Cannot open file \"%s\"", nam_p);
4349*0Sstevel@tonic-gate 				if (*Over_p != '\0') {
4350*0Sstevel@tonic-gate 					rstfiles(U_KEEP, dirfd);
4351*0Sstevel@tonic-gate 				}
4352*0Sstevel@tonic-gate 				return (-1);
4353*0Sstevel@tonic-gate 			}
4354*0Sstevel@tonic-gate 		}
4355*0Sstevel@tonic-gate 		cnt++;
4356*0Sstevel@tonic-gate 	} while (cnt < 2 && missdir(nam_p) == 0);
4357*0Sstevel@tonic-gate 
4358*0Sstevel@tonic-gate 	switch (cnt) {
4359*0Sstevel@tonic-gate 	case 0:
4360*0Sstevel@tonic-gate 		if ((Args & OCi) && (Hdr_type == USTAR)) {
4361*0Sstevel@tonic-gate 			setpasswd(nam_p);
4362*0Sstevel@tonic-gate 		}
4363*0Sstevel@tonic-gate 
4364*0Sstevel@tonic-gate 		if ((G_p->g_mode & Ftype) == S_IFLNK ||
4365*0Sstevel@tonic-gate 		    (Hdr_type == BAR && bar_linkflag == SYMTYPE)) {
4366*0Sstevel@tonic-gate 			if (fchownat(dirfd, get_component(nam_p),
4367*0Sstevel@tonic-gate 			    (int)G_p->g_uid, (int)G_p->g_gid,
4368*0Sstevel@tonic-gate 			    AT_SYMLINK_NOFOLLOW) < 0) {
4369*0Sstevel@tonic-gate 				if ((Euid == 0) || (Args & OCR)) {
4370*0Sstevel@tonic-gate 					msg(ERRN,
4371*0Sstevel@tonic-gate 					    "Error during chown() of "
4372*0Sstevel@tonic-gate 					    "\"%s%s%s\"",
4373*0Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
4374*0Sstevel@tonic-gate 					    nam_p : G_p->g_attrfnam_p,
4375*0Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
4376*0Sstevel@tonic-gate 					    "" : gettext(" Attribute "),
4377*0Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
4378*0Sstevel@tonic-gate 					    "" : nam_p);
4379*0Sstevel@tonic-gate 				}
4380*0Sstevel@tonic-gate 			}
4381*0Sstevel@tonic-gate 
4382*0Sstevel@tonic-gate 			break;
4383*0Sstevel@tonic-gate 		}
4384*0Sstevel@tonic-gate 
4385*0Sstevel@tonic-gate 		if (fchownat(dirfd, get_component(nam_p),
4386*0Sstevel@tonic-gate 		    (int)G_p->g_uid, (int)G_p->g_gid, 0) < 0) {
4387*0Sstevel@tonic-gate 			if ((Euid == 0) || (Args & OCR)) {
4388*0Sstevel@tonic-gate 				msg(ERRN,
4389*0Sstevel@tonic-gate 				    "Error during chown() of \"%s%s%s\"",
4390*0Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
4391*0Sstevel@tonic-gate 				    nam_p : G_p->g_attrfnam_p,
4392*0Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
4393*0Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
4394*0Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
4395*0Sstevel@tonic-gate 				    "" : nam_p);
4396*0Sstevel@tonic-gate 			}
4397*0Sstevel@tonic-gate 		}
4398*0Sstevel@tonic-gate 
4399*0Sstevel@tonic-gate 		break;
4400*0Sstevel@tonic-gate 
4401*0Sstevel@tonic-gate 	case 1:
4402*0Sstevel@tonic-gate 		if (Do_rename) {
4403*0Sstevel@tonic-gate 			msg(ERRN, "Cannot create directory for \"%s%s%s\"",
4404*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? Over_p :
4405*0Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
4406*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
4407*0Sstevel@tonic-gate 			    gettext(" Attribute "),
4408*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" : Over_p);
4409*0Sstevel@tonic-gate 		} else {
4410*0Sstevel@tonic-gate 			msg(ERRN, "Cannot create directory for \"%s%s%s\"",
4411*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? nam_p :
4412*0Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
4413*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
4414*0Sstevel@tonic-gate 			    gettext(" Attribute "),
4415*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" : nam_p);
4416*0Sstevel@tonic-gate 		}
4417*0Sstevel@tonic-gate 		break;
4418*0Sstevel@tonic-gate 
4419*0Sstevel@tonic-gate 	case 2:
4420*0Sstevel@tonic-gate 		if (Do_rename) {
4421*0Sstevel@tonic-gate 			msg(ERRN, "Cannot create \"%s%s%s\"",
4422*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? Over_p :
4423*0Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
4424*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
4425*0Sstevel@tonic-gate 			    gettext(" Attribute "),
4426*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
4427*0Sstevel@tonic-gate 			    Over_p);
4428*0Sstevel@tonic-gate 		} else {
4429*0Sstevel@tonic-gate 			msg(ERRN, "Cannot create \"%s%s%s\"",
4430*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? nam_p :
4431*0Sstevel@tonic-gate 			    G_p->g_attrfnam_p,
4432*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
4433*0Sstevel@tonic-gate 			    gettext(" Attribute "),
4434*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ? "" :
4435*0Sstevel@tonic-gate 			    nam_p);
4436*0Sstevel@tonic-gate 		}
4437*0Sstevel@tonic-gate 		break;
4438*0Sstevel@tonic-gate 
4439*0Sstevel@tonic-gate 	default:
4440*0Sstevel@tonic-gate 		msg(EXT, "Impossible case.");
4441*0Sstevel@tonic-gate 	}
4442*0Sstevel@tonic-gate 
4443*0Sstevel@tonic-gate 	Finished = 0;
4444*0Sstevel@tonic-gate 	return (result);
4445*0Sstevel@tonic-gate }
4446*0Sstevel@tonic-gate 
4447*0Sstevel@tonic-gate /*
4448*0Sstevel@tonic-gate  * read_hdr: Transfer headers from the selected format
4449*0Sstevel@tonic-gate  * in the archive I/O buffer to the generic structure.
4450*0Sstevel@tonic-gate  */
4451*0Sstevel@tonic-gate 
4452*0Sstevel@tonic-gate static
4453*0Sstevel@tonic-gate int
4454*0Sstevel@tonic-gate read_hdr(int hdr)
4455*0Sstevel@tonic-gate {
4456*0Sstevel@tonic-gate 	int rv = NONE;
4457*0Sstevel@tonic-gate 	major_t maj, rmaj;
4458*0Sstevel@tonic-gate 	minor_t min, rmin;
4459*0Sstevel@tonic-gate 	char tmpnull;
4460*0Sstevel@tonic-gate 	static int bar_read_cnt = 0;
4461*0Sstevel@tonic-gate 
4462*0Sstevel@tonic-gate 	if (hdr != BAR) {
4463*0Sstevel@tonic-gate 		if (Buffr.b_end_p != (Buffr.b_out_p + Hdrsz)) {
4464*0Sstevel@tonic-gate 			tmpnull = *(Buffr.b_out_p + Hdrsz);
4465*0Sstevel@tonic-gate 			*(Buffr.b_out_p + Hdrsz) = '\0';
4466*0Sstevel@tonic-gate 		}
4467*0Sstevel@tonic-gate 	}
4468*0Sstevel@tonic-gate 
4469*0Sstevel@tonic-gate 	switch (hdr) {
4470*0Sstevel@tonic-gate 	case BIN:
4471*0Sstevel@tonic-gate 		(void) memcpy(&Hdr, Buffr.b_out_p, HDRSZ);
4472*0Sstevel@tonic-gate 		if (Hdr.h_magic == (short)CMN_BBS) {
4473*0Sstevel@tonic-gate 			swap((char *)&Hdr, HDRSZ);
4474*0Sstevel@tonic-gate 		}
4475*0Sstevel@tonic-gate 		Gen.g_magic = Hdr.h_magic;
4476*0Sstevel@tonic-gate 		Gen.g_mode = Hdr.h_mode;
4477*0Sstevel@tonic-gate 		Gen.g_uid = Hdr.h_uid;
4478*0Sstevel@tonic-gate 		Gen.g_gid = Hdr.h_gid;
4479*0Sstevel@tonic-gate 		Gen.g_nlink = Hdr.h_nlink;
4480*0Sstevel@tonic-gate 		Gen.g_mtime = mklong(Hdr.h_mtime);
4481*0Sstevel@tonic-gate 		Gen.g_ino = Hdr.h_ino;
4482*0Sstevel@tonic-gate 		Gen.g_dev = Hdr.h_dev;
4483*0Sstevel@tonic-gate 		Gen.g_rdev = Hdr.h_rdev;
4484*0Sstevel@tonic-gate 		Gen.g_cksum = 0L;
4485*0Sstevel@tonic-gate 		Gen.g_filesz = (off_t)mklong(Hdr.h_filesize);
4486*0Sstevel@tonic-gate 		Gen.g_namesz = Hdr.h_namesize;
4487*0Sstevel@tonic-gate 		rv = BIN;
4488*0Sstevel@tonic-gate 		break;
4489*0Sstevel@tonic-gate 	case CHR:
4490*0Sstevel@tonic-gate 		if (sscanf(Buffr.b_out_p,
4491*0Sstevel@tonic-gate 		    "%6lo%6lo%6lo%6lo%6lo%6lo%6lo%6lo%11lo%6o%11llo",
4492*0Sstevel@tonic-gate 		    &Gen.g_magic, &Gen.g_dev, &Gen.g_ino, &Gen.g_mode,
4493*0Sstevel@tonic-gate 		    &Gen.g_uid, &Gen.g_gid, &Gen.g_nlink, &Gen.g_rdev,
4494*0Sstevel@tonic-gate 		    (ulong_t *)&Gen.g_mtime, (uint_t *)&Gen.g_namesz,
4495*0Sstevel@tonic-gate 		    (u_off_t *)&Gen.g_filesz) == CHR_CNT) {
4496*0Sstevel@tonic-gate 			rv = CHR;
4497*0Sstevel@tonic-gate #define	cpioMAJOR(x)	(int)(((unsigned)x >> 8) & 0x7F)
4498*0Sstevel@tonic-gate #define	cpioMINOR(x)	(int)(x & 0xFF)
4499*0Sstevel@tonic-gate 			maj = cpioMAJOR(Gen.g_dev);
4500*0Sstevel@tonic-gate 			rmaj = cpioMAJOR(Gen.g_rdev);
4501*0Sstevel@tonic-gate 			min = cpioMINOR(Gen.g_dev);
4502*0Sstevel@tonic-gate 			rmin = cpioMINOR(Gen.g_rdev);
4503*0Sstevel@tonic-gate 			if (Use_old_stat) {
4504*0Sstevel@tonic-gate 				/* needs error checking */
4505*0Sstevel@tonic-gate 				Gen.g_dev = (maj << 8) | min;
4506*0Sstevel@tonic-gate 				Gen.g_rdev = (rmaj << 8) | rmin;
4507*0Sstevel@tonic-gate 			} else {
4508*0Sstevel@tonic-gate 				Gen.g_dev = makedev(maj, min);
4509*0Sstevel@tonic-gate 				Gen.g_rdev = makedev(rmaj, rmin);
4510*0Sstevel@tonic-gate 			}
4511*0Sstevel@tonic-gate 		}
4512*0Sstevel@tonic-gate 		break;
4513*0Sstevel@tonic-gate 	case ASC:
4514*0Sstevel@tonic-gate 	case CRC:
4515*0Sstevel@tonic-gate 		if (sscanf(Buffr.b_out_p,
4516*0Sstevel@tonic-gate 		    "%6lx%8lx%8lx%8lx%8lx%8lx%8lx%8llx%8x%8x%8x%8x%8x%8lx",
4517*0Sstevel@tonic-gate 		    &Gen.g_magic, &Gen.g_ino, &Gen.g_mode, &Gen.g_uid,
4518*0Sstevel@tonic-gate 		    &Gen.g_gid, &Gen.g_nlink, &Gen.g_mtime,
4519*0Sstevel@tonic-gate 		    (u_off_t *)&Gen.g_filesz, (uint_t *)&maj, (uint_t *)&min,
4520*0Sstevel@tonic-gate 		    (uint_t *)&rmaj, (uint_t *)&rmin, (uint_t *)&Gen.g_namesz,
4521*0Sstevel@tonic-gate 		    &Gen.g_cksum) == ASC_CNT) {
4522*0Sstevel@tonic-gate 			Gen.g_dev = makedev(maj, min);
4523*0Sstevel@tonic-gate 			Gen.g_rdev = makedev(rmaj, rmin);
4524*0Sstevel@tonic-gate 			rv = hdr;
4525*0Sstevel@tonic-gate 		}
4526*0Sstevel@tonic-gate 		break;
4527*0Sstevel@tonic-gate 	case USTAR: /* TAR and USTAR */
4528*0Sstevel@tonic-gate 		if (*Buffr.b_out_p == '\0') {
4529*0Sstevel@tonic-gate 			*Gen.g_nam_p = '\0';
4530*0Sstevel@tonic-gate 			nambuf[0] = '\0';
4531*0Sstevel@tonic-gate 		} else {
4532*0Sstevel@tonic-gate 			Thdr_p = (union tblock *)Buffr.b_out_p;
4533*0Sstevel@tonic-gate 			Gen.g_nam_p[0] = '\0';
4534*0Sstevel@tonic-gate 			(void) strncpy((char *)&nambuf,
4535*0Sstevel@tonic-gate 			    Thdr_p->tbuf.t_name, NAMSIZ);
4536*0Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mode, "%8lo",
4537*0Sstevel@tonic-gate 			    &Gen.g_mode);
4538*0Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_uid, "%8lo", &Gen.g_uid);
4539*0Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_gid, "%8lo", &Gen.g_gid);
4540*0Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_size, "%11llo",
4541*0Sstevel@tonic-gate 			    (u_off_t *)&Gen.g_filesz);
4542*0Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mtime, "%12lo",
4543*0Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_mtime);
4544*0Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_cksum, "%8lo",
4545*0Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_cksum);
4546*0Sstevel@tonic-gate 			if (Thdr_p->tbuf.t_linkname[0] != (char)NULL)
4547*0Sstevel@tonic-gate 				Gen.g_nlink = 1;
4548*0Sstevel@tonic-gate 			else
4549*0Sstevel@tonic-gate 				Gen.g_nlink = 0;
4550*0Sstevel@tonic-gate 
4551*0Sstevel@tonic-gate 			switch (Thdr_p->tbuf.t_typeflag) {
4552*0Sstevel@tonic-gate 			case SYMTYPE:
4553*0Sstevel@tonic-gate 				/* Symbolic Link */
4554*0Sstevel@tonic-gate 				Gen.g_nlink = 2;
4555*0Sstevel@tonic-gate 				break;
4556*0Sstevel@tonic-gate 			case CHRTYPE:
4557*0Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFCHR);
4558*0Sstevel@tonic-gate 				break;
4559*0Sstevel@tonic-gate 			case BLKTYPE:
4560*0Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFBLK);
4561*0Sstevel@tonic-gate 				break;
4562*0Sstevel@tonic-gate 			case DIRTYPE:
4563*0Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFDIR);
4564*0Sstevel@tonic-gate 				break;
4565*0Sstevel@tonic-gate 			case FIFOTYPE:
4566*0Sstevel@tonic-gate 				Gen.g_mode |= (S_IFMT & S_IFIFO);
4567*0Sstevel@tonic-gate 				break;
4568*0Sstevel@tonic-gate 			}
4569*0Sstevel@tonic-gate 
4570*0Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_magic, "%8lo",
4571*0Sstevel@tonic-gate 			    /* LINTED alignment */
4572*0Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_tmagic);
4573*0Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_version, "%8lo",
4574*0Sstevel@tonic-gate 			    /* LINTED alignment */
4575*0Sstevel@tonic-gate 			    (ulong_t *)&Gen.g_version);
4576*0Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_uname, "%32s",
4577*0Sstevel@tonic-gate 			    (char *)&Gen.g_uname);
4578*0Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_gname, "%32s",
4579*0Sstevel@tonic-gate 			    (char *)&Gen.g_gname);
4580*0Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_devmajor, "%8lo",
4581*0Sstevel@tonic-gate 			    &Gen.g_dev);
4582*0Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_devminor, "%8lo",
4583*0Sstevel@tonic-gate 			    &Gen.g_rdev);
4584*0Sstevel@tonic-gate 			(void) strncpy((char *)&prebuf,
4585*0Sstevel@tonic-gate 			    Thdr_p->tbuf.t_prefix, PRESIZ);
4586*0Sstevel@tonic-gate 			Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
4587*0Sstevel@tonic-gate 			Gen.g_dev = makedev(maj, min);
4588*0Sstevel@tonic-gate 		}
4589*0Sstevel@tonic-gate 		rv = USTAR;
4590*0Sstevel@tonic-gate 		break;
4591*0Sstevel@tonic-gate 	case TAR:
4592*0Sstevel@tonic-gate 		if (*Buffr.b_out_p == '\0') {
4593*0Sstevel@tonic-gate 			*Gen.g_nam_p = '\0';
4594*0Sstevel@tonic-gate 			nambuf[0] = '\0';
4595*0Sstevel@tonic-gate 		} else {
4596*0Sstevel@tonic-gate 			Thdr_p = (union tblock *)Buffr.b_out_p;
4597*0Sstevel@tonic-gate 			Gen.g_nam_p[0] = '\0';
4598*0Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mode, "%lo", &Gen.g_mode);
4599*0Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_uid, "%lo", &Gen.g_uid);
4600*0Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_gid, "%lo", &Gen.g_gid);
4601*0Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_size, "%llo",
4602*0Sstevel@tonic-gate 			    (u_off_t *)&Gen.g_filesz);
4603*0Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_mtime, "%lo",
4604*0Sstevel@tonic-gate 			    &Gen.g_mtime);
4605*0Sstevel@tonic-gate 			(void) sscanf(Thdr_p->tbuf.t_cksum, "%lo",
4606*0Sstevel@tonic-gate 			    &Gen.g_cksum);
4607*0Sstevel@tonic-gate 			if (Thdr_p->tbuf.t_typeflag == '1')	/* hardlink */
4608*0Sstevel@tonic-gate 				Gen.g_nlink = 1;
4609*0Sstevel@tonic-gate 			else
4610*0Sstevel@tonic-gate 				Gen.g_nlink = 0;
4611*0Sstevel@tonic-gate 			(void) strncpy(Gen.g_nam_p,
4612*0Sstevel@tonic-gate 			    Thdr_p->tbuf.t_name, NAMSIZ);
4613*0Sstevel@tonic-gate 			Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
4614*0Sstevel@tonic-gate 			(void) strcpy(nambuf, Gen.g_nam_p);
4615*0Sstevel@tonic-gate 		}
4616*0Sstevel@tonic-gate 		rv = TAR;
4617*0Sstevel@tonic-gate 		break;
4618*0Sstevel@tonic-gate 	case BAR:
4619*0Sstevel@tonic-gate 		if (Bar_vol_num == 0 && bar_read_cnt == 0) {
4620*0Sstevel@tonic-gate 			read_bar_vol_hdr();
4621*0Sstevel@tonic-gate 			bar_read_cnt++;
4622*0Sstevel@tonic-gate 		}
4623*0Sstevel@tonic-gate 		else
4624*0Sstevel@tonic-gate 			read_bar_file_hdr();
4625*0Sstevel@tonic-gate 		rv = BAR;
4626*0Sstevel@tonic-gate 		break;
4627*0Sstevel@tonic-gate 	default:
4628*0Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
4629*0Sstevel@tonic-gate 	}
4630*0Sstevel@tonic-gate 
4631*0Sstevel@tonic-gate 	if (hdr != BAR) {
4632*0Sstevel@tonic-gate 		if (Buffr.b_end_p != (Buffr.b_out_p + Hdrsz))
4633*0Sstevel@tonic-gate 			*(Buffr.b_out_p + Hdrsz) = tmpnull;
4634*0Sstevel@tonic-gate 	}
4635*0Sstevel@tonic-gate 
4636*0Sstevel@tonic-gate 	return (rv);
4637*0Sstevel@tonic-gate }
4638*0Sstevel@tonic-gate 
4639*0Sstevel@tonic-gate /*
4640*0Sstevel@tonic-gate  * reclaim: Reclaim linked file structure storage.
4641*0Sstevel@tonic-gate  */
4642*0Sstevel@tonic-gate 
4643*0Sstevel@tonic-gate static void
4644*0Sstevel@tonic-gate reclaim(struct Lnk *p)
4645*0Sstevel@tonic-gate {
4646*0Sstevel@tonic-gate 	p->L_bck_p->L_nxt_p = p->L_nxt_p;
4647*0Sstevel@tonic-gate 	p->L_nxt_p->L_bck_p = p->L_bck_p;
4648*0Sstevel@tonic-gate 
4649*0Sstevel@tonic-gate 	while (p != NULL) {
4650*0Sstevel@tonic-gate 		struct Lnk *new_p = p->L_lnk_p;
4651*0Sstevel@tonic-gate 
4652*0Sstevel@tonic-gate 		free(p->L_gen.g_nam_p);
4653*0Sstevel@tonic-gate 		free(p);
4654*0Sstevel@tonic-gate 		p = new_p;
4655*0Sstevel@tonic-gate 	}
4656*0Sstevel@tonic-gate }
4657*0Sstevel@tonic-gate 
4658*0Sstevel@tonic-gate /*
4659*0Sstevel@tonic-gate  * rstbuf: Reset the I/O buffer, move incomplete potential headers to
4660*0Sstevel@tonic-gate  * the front of the buffer and force bread() to refill the buffer.  The
4661*0Sstevel@tonic-gate  * return value from bread() is returned (to identify I/O errors).  On the
4662*0Sstevel@tonic-gate  * 3B2, reads must begin on a word boundary, therefore, with the -i option,
4663*0Sstevel@tonic-gate  * any remaining bytes in the buffer must be moved to the base of the buffer
4664*0Sstevel@tonic-gate  * in such a way that the destination locations of subsequent reads are
4665*0Sstevel@tonic-gate  * word aligned.
4666*0Sstevel@tonic-gate  */
4667*0Sstevel@tonic-gate 
4668*0Sstevel@tonic-gate static void
4669*0Sstevel@tonic-gate rstbuf(void)
4670*0Sstevel@tonic-gate {
4671*0Sstevel@tonic-gate 	int pad;
4672*0Sstevel@tonic-gate 
4673*0Sstevel@tonic-gate 	if ((Args & OCi) || Append) {
4674*0Sstevel@tonic-gate 		if (Buffr.b_out_p != Buffr.b_base_p) {
4675*0Sstevel@tonic-gate 			pad = ((Buffr.b_cnt + FULLWD) & ~FULLWD);
4676*0Sstevel@tonic-gate 			Buffr.b_in_p = Buffr.b_base_p + pad;
4677*0Sstevel@tonic-gate 			pad -= Buffr.b_cnt;
4678*0Sstevel@tonic-gate 			(void) memcpy(Buffr.b_base_p + pad, Buffr.b_out_p,
4679*0Sstevel@tonic-gate 			    (int)Buffr.b_cnt);
4680*0Sstevel@tonic-gate 			Buffr.b_out_p = Buffr.b_base_p + pad;
4681*0Sstevel@tonic-gate 		}
4682*0Sstevel@tonic-gate 		if (bfill() < 0)
4683*0Sstevel@tonic-gate 			msg(EXT, "Unexpected end-of-archive encountered.");
4684*0Sstevel@tonic-gate 	} else { /* OCo */
4685*0Sstevel@tonic-gate 		(void) memcpy(Buffr.b_base_p, Buffr.b_out_p, (int)Buffr.b_cnt);
4686*0Sstevel@tonic-gate 		Buffr.b_out_p = Buffr.b_base_p;
4687*0Sstevel@tonic-gate 		Buffr.b_in_p = Buffr.b_base_p + Buffr.b_cnt;
4688*0Sstevel@tonic-gate 	}
4689*0Sstevel@tonic-gate }
4690*0Sstevel@tonic-gate 
4691*0Sstevel@tonic-gate static void
4692*0Sstevel@tonic-gate setpasswd(char *nam)
4693*0Sstevel@tonic-gate {
4694*0Sstevel@tonic-gate 	if ((dpasswd = getpwnam(&Gen.g_uname[0])) == (struct passwd *)NULL) {
4695*0Sstevel@tonic-gate 		msg(EPOST, "cpio: problem reading passwd entry");
4696*0Sstevel@tonic-gate 		msg(EPOST, "cpio: %s: owner not changed", nam);
4697*0Sstevel@tonic-gate 		if (Gen.g_uid == UID_NOBODY && S_ISREG(Gen.g_mode))
4698*0Sstevel@tonic-gate 			Gen.g_mode &= ~S_ISUID;
4699*0Sstevel@tonic-gate 	} else
4700*0Sstevel@tonic-gate 		Gen.g_uid = dpasswd->pw_uid;
4701*0Sstevel@tonic-gate 
4702*0Sstevel@tonic-gate 	if ((dgroup = getgrnam(&Gen.g_gname[0])) == (struct group *)NULL) {
4703*0Sstevel@tonic-gate 		msg(EPOST, "cpio: problem reading group entry");
4704*0Sstevel@tonic-gate 		msg(EPOST, "cpio: %s: group not changed", nam);
4705*0Sstevel@tonic-gate 		if (Gen.g_gid == GID_NOBODY && S_ISREG(Gen.g_mode))
4706*0Sstevel@tonic-gate 			Gen.g_mode &= ~S_ISGID;
4707*0Sstevel@tonic-gate 	} else
4708*0Sstevel@tonic-gate 		Gen.g_gid = dgroup->gr_gid;
4709*0Sstevel@tonic-gate 	G_p = &Gen;
4710*0Sstevel@tonic-gate }
4711*0Sstevel@tonic-gate 
4712*0Sstevel@tonic-gate /*
4713*0Sstevel@tonic-gate  * rstfiles:  Perform final changes to the file.  If the -u option is set,
4714*0Sstevel@tonic-gate  * and overwrite == U_OVER, remove the temporary file, else if overwrite
4715*0Sstevel@tonic-gate  * == U_KEEP, unlink the current file, and restore the existing version
4716*0Sstevel@tonic-gate  * of the file.  In addition, where appropriate, set the access or modification
4717*0Sstevel@tonic-gate  * times, change the owner and change the modes of the file.
4718*0Sstevel@tonic-gate  *
4719*0Sstevel@tonic-gate  * Note that if Do_rename is set, then the roles of original and temporary
4720*0Sstevel@tonic-gate  * file are reversed. If all went well, we will rename() the temporary file
4721*0Sstevel@tonic-gate  * over the original in order to accomodate potentially executing files.
4722*0Sstevel@tonic-gate  */
4723*0Sstevel@tonic-gate static void
4724*0Sstevel@tonic-gate rstfiles(int over, int dirfd)
4725*0Sstevel@tonic-gate {
4726*0Sstevel@tonic-gate 	char *inam_p, *onam_p, *nam_p;
4727*0Sstevel@tonic-gate 	int error;
4728*0Sstevel@tonic-gate 
4729*0Sstevel@tonic-gate 	if (Args & OCp) {
4730*0Sstevel@tonic-gate 		if (G_p->g_attrnam_p == (char *)NULL) {
4731*0Sstevel@tonic-gate 			nam_p = Fullnam_p;
4732*0Sstevel@tonic-gate 		} else {
4733*0Sstevel@tonic-gate 			nam_p = G_p->g_attrnam_p;
4734*0Sstevel@tonic-gate 		}
4735*0Sstevel@tonic-gate 	} else {
4736*0Sstevel@tonic-gate 		if (Gen.g_nlink > (ulong_t)0) {
4737*0Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
4738*0Sstevel@tonic-gate 		} else {
4739*0Sstevel@tonic-gate 			nam_p = Gen.g_nam_p;
4740*0Sstevel@tonic-gate 		}
4741*0Sstevel@tonic-gate 	}
4742*0Sstevel@tonic-gate 	if (Gen.g_attrnam_p != (char *)NULL) {
4743*0Sstevel@tonic-gate 		nam_p = Gen.g_attrnam_p;
4744*0Sstevel@tonic-gate 	}
4745*0Sstevel@tonic-gate 
4746*0Sstevel@tonic-gate 	if ((Args & OCi) && (Hdr_type == USTAR)) {
4747*0Sstevel@tonic-gate 		setpasswd(nam_p);
4748*0Sstevel@tonic-gate 	}
4749*0Sstevel@tonic-gate 	if (over == U_KEEP && *Over_p != '\0') {
4750*0Sstevel@tonic-gate 		if (Do_rename) {
4751*0Sstevel@tonic-gate 			msg(POST, "Restoring existing \"%s%s%s\"",
4752*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
4753*0Sstevel@tonic-gate 			    Over_p : Fullnam_p,
4754*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
4755*0Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
4756*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
4757*0Sstevel@tonic-gate 			    "" : Over_p);
4758*0Sstevel@tonic-gate 		} else {
4759*0Sstevel@tonic-gate 			msg(POST, "Restoring existing \"%s%s%s\"",
4760*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
4761*0Sstevel@tonic-gate 			    nam_p : Fullnam_p,
4762*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
4763*0Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
4764*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
4765*0Sstevel@tonic-gate 			    "" : nam_p);
4766*0Sstevel@tonic-gate 		}
4767*0Sstevel@tonic-gate 
4768*0Sstevel@tonic-gate 		/* delete what we just built */
4769*0Sstevel@tonic-gate 		(void) unlinkat(dirfd, get_component(nam_p), 0);
4770*0Sstevel@tonic-gate 
4771*0Sstevel@tonic-gate 		/* If the old file needs restoring, do the necessary links */
4772*0Sstevel@tonic-gate 		if (Do_rename) {
4773*0Sstevel@tonic-gate 			char *tmp_ptr;
4774*0Sstevel@tonic-gate 
4775*0Sstevel@tonic-gate 			if (Args & OCp) {
4776*0Sstevel@tonic-gate 				tmp_ptr = Fullnam_p;
4777*0Sstevel@tonic-gate 				Fullnam_p = Over_p;
4778*0Sstevel@tonic-gate 			} else {
4779*0Sstevel@tonic-gate 				tmp_ptr = G_p->g_nam_p;
4780*0Sstevel@tonic-gate 				G_p->g_nam_p = Over_p;
4781*0Sstevel@tonic-gate 			}
4782*0Sstevel@tonic-gate 			Over_p = tmp_ptr;
4783*0Sstevel@tonic-gate 
4784*0Sstevel@tonic-gate 			Do_rename = 0;	/* names now have original values */
4785*0Sstevel@tonic-gate 		} else {
4786*0Sstevel@tonic-gate 			if (rename(Over_p, nam_p) < 0) {
4787*0Sstevel@tonic-gate 				if (link(Over_p, nam_p) < 0) {
4788*0Sstevel@tonic-gate 					msg(EXTN,
4789*0Sstevel@tonic-gate 					    "Cannot recover original version"
4790*0Sstevel@tonic-gate 					    " of \"%s%s%s\"",
4791*0Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
4792*0Sstevel@tonic-gate 					    nam_p : Fullnam_p,
4793*0Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
4794*0Sstevel@tonic-gate 					    "" : gettext(" Attribute "),
4795*0Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
4796*0Sstevel@tonic-gate 					    "" : nam_p);
4797*0Sstevel@tonic-gate 				}
4798*0Sstevel@tonic-gate 				if (unlinkat(dirfd, get_component(Over_p), 0)) {
4799*0Sstevel@tonic-gate 					msg(ERRN,
4800*0Sstevel@tonic-gate 					    "Cannot remove temp file "
4801*0Sstevel@tonic-gate 					    "\"%s%s%s\"",
4802*0Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
4803*0Sstevel@tonic-gate 					    Over_p : Fullnam_p,
4804*0Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
4805*0Sstevel@tonic-gate 					    "" : gettext(" Attribute "),
4806*0Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
4807*0Sstevel@tonic-gate 					    "" : Over_p);
4808*0Sstevel@tonic-gate 				}
4809*0Sstevel@tonic-gate 			}
4810*0Sstevel@tonic-gate 		}
4811*0Sstevel@tonic-gate 		*Over_p = '\0';
4812*0Sstevel@tonic-gate 		return;
4813*0Sstevel@tonic-gate 	} else if (over == U_OVER && *Over_p != '\0') {
4814*0Sstevel@tonic-gate 		if (Do_rename) {
4815*0Sstevel@tonic-gate 			char *tmp_ptr;
4816*0Sstevel@tonic-gate 
4817*0Sstevel@tonic-gate 			(void) renameat(dirfd, get_component(nam_p),
4818*0Sstevel@tonic-gate 			    dirfd, get_component(Over_p));
4819*0Sstevel@tonic-gate 			if (Args & OCp) {
4820*0Sstevel@tonic-gate 				if (G_p->g_attrnam_p == (char *)NULL) {
4821*0Sstevel@tonic-gate 					tmp_ptr = Fullnam_p;
4822*0Sstevel@tonic-gate 					Fullnam_p = Over_p;
4823*0Sstevel@tonic-gate 					Over_p = tmp_ptr;
4824*0Sstevel@tonic-gate 				} else {
4825*0Sstevel@tonic-gate 					/*
4826*0Sstevel@tonic-gate 					 * Over_p is pointing at g_attrnam_p
4827*0Sstevel@tonic-gate 					 * which must be preserved.
4828*0Sstevel@tonic-gate 					 *
4829*0Sstevel@tonic-gate 					 * We don't want the tmp_ptr and so
4830*0Sstevel@tonic-gate 					 * on to throw away our only copy of
4831*0Sstevel@tonic-gate 					 * the name.
4832*0Sstevel@tonic-gate 					 */
4833*0Sstevel@tonic-gate 					Over_p = Attrfile_p;
4834*0Sstevel@tonic-gate 				}
4835*0Sstevel@tonic-gate 			} else {
4836*0Sstevel@tonic-gate 				tmp_ptr = G_p->g_nam_p;
4837*0Sstevel@tonic-gate 				G_p->g_nam_p = Over_p;
4838*0Sstevel@tonic-gate 				Over_p = tmp_ptr;
4839*0Sstevel@tonic-gate 			}
4840*0Sstevel@tonic-gate 			Do_rename = 0;	/* names now have original values */
4841*0Sstevel@tonic-gate 		} else {
4842*0Sstevel@tonic-gate 			if (unlinkat(dirfd, get_component(Over_p), 0) < 0) {
4843*0Sstevel@tonic-gate 				msg(ERRN,
4844*0Sstevel@tonic-gate 				    "Cannot unlink() temp file \"%s%s%s\"",
4845*0Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
4846*0Sstevel@tonic-gate 				    Over_p : Fullnam_p,
4847*0Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
4848*0Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
4849*0Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
4850*0Sstevel@tonic-gate 				    "" : Over_p);
4851*0Sstevel@tonic-gate 			}
4852*0Sstevel@tonic-gate 		}
4853*0Sstevel@tonic-gate 		*Over_p = '\0';
4854*0Sstevel@tonic-gate 	}
4855*0Sstevel@tonic-gate 	if (Args & OCp) {
4856*0Sstevel@tonic-gate 		if (G_p->g_attrnam_p != (char *)NULL) {
4857*0Sstevel@tonic-gate 			inam_p = G_p->g_attrfnam_p;
4858*0Sstevel@tonic-gate 			onam_p = G_p->g_attrnam_p;
4859*0Sstevel@tonic-gate 		} else {
4860*0Sstevel@tonic-gate 			inam_p = Nam_p;
4861*0Sstevel@tonic-gate 			onam_p = Fullnam_p;
4862*0Sstevel@tonic-gate 		}
4863*0Sstevel@tonic-gate 	} else /* OCi only uses onam_p, OCo only uses inam_p */
4864*0Sstevel@tonic-gate 		if (G_p->g_attrnam_p != (char *)NULL) {
4865*0Sstevel@tonic-gate 			inam_p = onam_p = G_p->g_attrnam_p;
4866*0Sstevel@tonic-gate 		} else {
4867*0Sstevel@tonic-gate 			inam_p = onam_p = G_p->g_nam_p;
4868*0Sstevel@tonic-gate 		}
4869*0Sstevel@tonic-gate 
4870*0Sstevel@tonic-gate 	/*
4871*0Sstevel@tonic-gate 	 * change the modes back to the ones originally created in the
4872*0Sstevel@tonic-gate 	 * archive
4873*0Sstevel@tonic-gate 	 */
4874*0Sstevel@tonic-gate 	if (Args & OCi) {
4875*0Sstevel@tonic-gate 		mode_t orig_mask, new_mask;
4876*0Sstevel@tonic-gate 		struct stat sbuf;
4877*0Sstevel@tonic-gate 
4878*0Sstevel@tonic-gate 		if (!(Pflag && acl_set)) {
4879*0Sstevel@tonic-gate 			/* Acl was not set, so we must chmod */
4880*0Sstevel@tonic-gate 			if (LSTAT(dirfd, G_p->g_nam_p, &sbuf) == 0) {
4881*0Sstevel@tonic-gate 				if ((sbuf.st_mode & Ftype) != S_IFLNK) {
4882*0Sstevel@tonic-gate 					orig_mask = umask(0);
4883*0Sstevel@tonic-gate 					new_mask = G_p->g_mode & ~orig_mask;
4884*0Sstevel@tonic-gate 
4885*0Sstevel@tonic-gate 					/*
4886*0Sstevel@tonic-gate 					 * use fchmod for attributes, since
4887*0Sstevel@tonic-gate 					 * we known they are always regular
4888*0Sstevel@tonic-gate 					 * files, whereas when it isn't an
4889*0Sstevel@tonic-gate 					 * attribute it could be for a fifo
4890*0Sstevel@tonic-gate 					 * or something other that we don't
4891*0Sstevel@tonic-gate 					 * open and don't have a valid Ofile
4892*0Sstevel@tonic-gate 					 * for.
4893*0Sstevel@tonic-gate 					 */
4894*0Sstevel@tonic-gate 					if (G_p->g_attrnam_p != (char *)NULL) {
4895*0Sstevel@tonic-gate 						error = fchmod(Ofile, new_mask);
4896*0Sstevel@tonic-gate 					} else {
4897*0Sstevel@tonic-gate 						error = chmod(G_p->g_nam_p,
4898*0Sstevel@tonic-gate 							new_mask);
4899*0Sstevel@tonic-gate 					}
4900*0Sstevel@tonic-gate 					if (error < 0) {
4901*0Sstevel@tonic-gate 						msg(ERRN,
4902*0Sstevel@tonic-gate 						    "Cannot chmod() \"%s%s%s\"",
4903*0Sstevel@tonic-gate 						    (G_p->g_attrnam_p ==
4904*0Sstevel@tonic-gate 						    (char *)NULL) ?
4905*0Sstevel@tonic-gate 						    G_p->g_nam_p :
4906*0Sstevel@tonic-gate 						    G_p->g_attrfnam_p,
4907*0Sstevel@tonic-gate 						    (G_p->g_attrnam_p ==
4908*0Sstevel@tonic-gate 						    (char *)NULL) ?
4909*0Sstevel@tonic-gate 						    "" : gettext(
4910*0Sstevel@tonic-gate 						    " Attribute "),
4911*0Sstevel@tonic-gate 						    (G_p->g_attrnam_p ==
4912*0Sstevel@tonic-gate 						    (char *)NULL) ?
4913*0Sstevel@tonic-gate 						    "" : G_p->g_attrnam_p);
4914*0Sstevel@tonic-gate 					}
4915*0Sstevel@tonic-gate 					(void) umask(orig_mask);
4916*0Sstevel@tonic-gate 				}
4917*0Sstevel@tonic-gate 			}
4918*0Sstevel@tonic-gate 		}
4919*0Sstevel@tonic-gate 	}
4920*0Sstevel@tonic-gate 
4921*0Sstevel@tonic-gate 	if (!(Args & OCo)) {
4922*0Sstevel@tonic-gate 		if (Args & OCm) {
4923*0Sstevel@tonic-gate 			set_tym(dirfd, get_component(onam_p),
4924*0Sstevel@tonic-gate 			    G_p->g_mtime, G_p->g_mtime);
4925*0Sstevel@tonic-gate 		}
4926*0Sstevel@tonic-gate 		if (!acl_set) {
4927*0Sstevel@tonic-gate 			if (G_p->g_attrnam_p != (char *)NULL) {
4928*0Sstevel@tonic-gate 				error = fchmod(Ofile, (int)G_p->g_mode);
4929*0Sstevel@tonic-gate 			} else {
4930*0Sstevel@tonic-gate 				error = chmod(onam_p, (int)G_p->g_mode);
4931*0Sstevel@tonic-gate 			}
4932*0Sstevel@tonic-gate 			if ((error < 0) && ((Euid == 0) || (Args & OCR))) {
4933*0Sstevel@tonic-gate 				msg(ERRN, "Cannot chmod() \"%s%s%s\"", onam_p,
4934*0Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
4935*0Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
4936*0Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
4937*0Sstevel@tonic-gate 				    "" : onam_p);
4938*0Sstevel@tonic-gate 			}
4939*0Sstevel@tonic-gate 		}
4940*0Sstevel@tonic-gate 		if ((Args & OCR) &&
4941*0Sstevel@tonic-gate 		    (fchownat(dirfd, get_component(onam_p), Rpw_p->pw_uid,
4942*0Sstevel@tonic-gate 			Rpw_p->pw_gid, 0) < 0)) {
4943*0Sstevel@tonic-gate 			msg(ERRN, "Cannot chown() \"%s%s%s\"",
4944*0Sstevel@tonic-gate 			    onam_p,
4945*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
4946*0Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
4947*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
4948*0Sstevel@tonic-gate 			    "" : onam_p);
4949*0Sstevel@tonic-gate 		}
4950*0Sstevel@tonic-gate 	}
4951*0Sstevel@tonic-gate 
4952*0Sstevel@tonic-gate 	if (!(Args & OCi) && (Args & OCa)) {
4953*0Sstevel@tonic-gate 		/*
4954*0Sstevel@tonic-gate 		 * Use dirfd since we are updating original file
4955*0Sstevel@tonic-gate 		 * and not just created file
4956*0Sstevel@tonic-gate 		 */
4957*0Sstevel@tonic-gate 		set_tym(G_p->g_dirfd, get_component(inam_p),
4958*0Sstevel@tonic-gate 		    (ulong_t)SrcSt.st_atime, (ulong_t)SrcSt.st_mtime);
4959*0Sstevel@tonic-gate 	}
4960*0Sstevel@tonic-gate 
4961*0Sstevel@tonic-gate 	if ((Args & OCp) && !(Args & OCR)) {
4962*0Sstevel@tonic-gate 		if (fchownat(dirfd, get_component(onam_p),
4963*0Sstevel@tonic-gate 		    G_p->g_uid, G_p->g_gid, 0) < 0) {
4964*0Sstevel@tonic-gate 			if (Euid == 0) {
4965*0Sstevel@tonic-gate 				msg(ERRN, "Cannot chown() \"%s%s%s\"",
4966*0Sstevel@tonic-gate 				    onam_p,
4967*0Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
4968*0Sstevel@tonic-gate 				    "" : gettext(" Attribute "),
4969*0Sstevel@tonic-gate 				    (G_p->g_attrnam_p == (char *)NULL) ?
4970*0Sstevel@tonic-gate 				    "" : onam_p);
4971*0Sstevel@tonic-gate 			}
4972*0Sstevel@tonic-gate 		}
4973*0Sstevel@tonic-gate 	} else {
4974*0Sstevel@tonic-gate 		/* OCi only uses onam_p, OCo only uses inam_p */
4975*0Sstevel@tonic-gate 		if (!(Args & OCR)) {
4976*0Sstevel@tonic-gate 			if ((Args & OCi) && (fchownat(dirfd,
4977*0Sstevel@tonic-gate 			    get_component(inam_p), G_p->g_uid,
4978*0Sstevel@tonic-gate 			    G_p->g_gid, 0) < 0)) {
4979*0Sstevel@tonic-gate 				if (Euid == 0) {
4980*0Sstevel@tonic-gate 					msg(ERRN, "Cannot chown() \"%s%s%s\"",
4981*0Sstevel@tonic-gate 					    onam_p,
4982*0Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
4983*0Sstevel@tonic-gate 					    "" : gettext(" Attribute "),
4984*0Sstevel@tonic-gate 					    (G_p->g_attrnam_p == (char *)NULL) ?
4985*0Sstevel@tonic-gate 					    "" : onam_p);
4986*0Sstevel@tonic-gate 				}
4987*0Sstevel@tonic-gate 			}
4988*0Sstevel@tonic-gate 		}
4989*0Sstevel@tonic-gate 	}
4990*0Sstevel@tonic-gate }
4991*0Sstevel@tonic-gate 
4992*0Sstevel@tonic-gate /*
4993*0Sstevel@tonic-gate  * scan4trail: Scan the archive looking for the trailer.
4994*0Sstevel@tonic-gate  * When found, back the archive up over the trailer and overwrite
4995*0Sstevel@tonic-gate  * the trailer with the files to be added to the archive.
4996*0Sstevel@tonic-gate  */
4997*0Sstevel@tonic-gate 
4998*0Sstevel@tonic-gate static void
4999*0Sstevel@tonic-gate scan4trail(void)
5000*0Sstevel@tonic-gate {
5001*0Sstevel@tonic-gate 	int rv;
5002*0Sstevel@tonic-gate 	off_t off1, off2;
5003*0Sstevel@tonic-gate 
5004*0Sstevel@tonic-gate 	Append = 1;
5005*0Sstevel@tonic-gate 	Hdr_type = NONE;
5006*0Sstevel@tonic-gate 	G_p = (struct gen_hdr *)NULL;
5007*0Sstevel@tonic-gate 	while (gethdr()) {
5008*0Sstevel@tonic-gate 		G_p = &Gen;
5009*0Sstevel@tonic-gate 		data_in(P_SKIP);
5010*0Sstevel@tonic-gate 	}
5011*0Sstevel@tonic-gate 	off1 = Buffr.b_cnt;
5012*0Sstevel@tonic-gate 	off2 = Bufsize - (Buffr.b_cnt % Bufsize);
5013*0Sstevel@tonic-gate 	Buffr.b_out_p = Buffr.b_in_p = Buffr.b_base_p;
5014*0Sstevel@tonic-gate 	Buffr.b_cnt = (off_t)0;
5015*0Sstevel@tonic-gate 	if (lseek(Archive, -(off1 + off2), SEEK_REL) < 0)
5016*0Sstevel@tonic-gate 		msg(EXTN, "Unable to append to this archive");
5017*0Sstevel@tonic-gate 	if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0)
5018*0Sstevel@tonic-gate 		msg(EXTN, "Cannot append to this archive");
5019*0Sstevel@tonic-gate 	if (lseek(Archive, (off_t)-rv, SEEK_REL) < 0)
5020*0Sstevel@tonic-gate 		msg(EXTN, "Unable to append to this archive");
5021*0Sstevel@tonic-gate 	Buffr.b_cnt = off2;
5022*0Sstevel@tonic-gate 	Buffr.b_in_p = Buffr.b_base_p + Buffr.b_cnt;
5023*0Sstevel@tonic-gate 	Append = 0;
5024*0Sstevel@tonic-gate }
5025*0Sstevel@tonic-gate 
5026*0Sstevel@tonic-gate /*
5027*0Sstevel@tonic-gate  * setup:  Perform setup and initialization functions.  Parse the options
5028*0Sstevel@tonic-gate  * using getopt(3C), call ckopts to check the options and initialize various
5029*0Sstevel@tonic-gate  * structures and pointers.  Specifically, for the -i option, save any
5030*0Sstevel@tonic-gate  * patterns, for the -o option, check (via stat(2)) the archive, and for
5031*0Sstevel@tonic-gate  * the -p option, validate the destination directory.
5032*0Sstevel@tonic-gate  */
5033*0Sstevel@tonic-gate 
5034*0Sstevel@tonic-gate static void
5035*0Sstevel@tonic-gate setup(int largc, char **largv)
5036*0Sstevel@tonic-gate {
5037*0Sstevel@tonic-gate 	extern int optind;
5038*0Sstevel@tonic-gate 	extern char *optarg;
5039*0Sstevel@tonic-gate 
5040*0Sstevel@tonic-gate #if defined(O_XATTR)
5041*0Sstevel@tonic-gate #ifdef WAITAROUND
5042*0Sstevel@tonic-gate 	char	*opts_p = "zabcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6@";
5043*0Sstevel@tonic-gate #else
5044*0Sstevel@tonic-gate 	char	*opts_p = "abcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6@";
5045*0Sstevel@tonic-gate #endif
5046*0Sstevel@tonic-gate #else
5047*0Sstevel@tonic-gate #ifdef WAITAROUND
5048*0Sstevel@tonic-gate 	char	*opts_p = "zabcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6";
5049*0Sstevel@tonic-gate #else
5050*0Sstevel@tonic-gate 	char	*opts_p = "abcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6";
5051*0Sstevel@tonic-gate #endif
5052*0Sstevel@tonic-gate #endif
5053*0Sstevel@tonic-gate 
5054*0Sstevel@tonic-gate 	char   *dupl_p = "Only one occurrence of -%c allowed";
5055*0Sstevel@tonic-gate 	int option;
5056*0Sstevel@tonic-gate 	int blk_cnt, blk_cnt_max;
5057*0Sstevel@tonic-gate 	struct rlimit rlim;
5058*0Sstevel@tonic-gate 
5059*0Sstevel@tonic-gate 	/* Remember the native page size. */
5060*0Sstevel@tonic-gate 
5061*0Sstevel@tonic-gate 	PageSize = sysconf(_SC_PAGESIZE);
5062*0Sstevel@tonic-gate 
5063*0Sstevel@tonic-gate 	if (PageSize == -1) {
5064*0Sstevel@tonic-gate 		/*
5065*0Sstevel@tonic-gate 		 * This sysconf call will almost certainly never fail.  The
5066*0Sstevel@tonic-gate 		 * symbol PAGESIZE itself resolves to the above sysconf call,
5067*0Sstevel@tonic-gate 		 * so we should go ahead and define our own constant.
5068*0Sstevel@tonic-gate 		 */
5069*0Sstevel@tonic-gate 		PageSize = 8192;
5070*0Sstevel@tonic-gate 	}
5071*0Sstevel@tonic-gate 
5072*0Sstevel@tonic-gate 	Euid = geteuid();
5073*0Sstevel@tonic-gate 	Hdr_type = BIN;
5074*0Sstevel@tonic-gate 	Max_offset = (off_t)(BIN_OFFSET_MAX);
5075*0Sstevel@tonic-gate 	Efil_p = Hdr_p = Own_p = IOfil_p = NULL;
5076*0Sstevel@tonic-gate 	while ((option = getopt(largc, largv, opts_p)) != EOF) {
5077*0Sstevel@tonic-gate 		switch (option) {
5078*0Sstevel@tonic-gate #ifdef WAITAROUND
5079*0Sstevel@tonic-gate 		case 'z':
5080*0Sstevel@tonic-gate 			/* rendezvous with the debugger */
5081*0Sstevel@tonic-gate 			waitaround = 1;
5082*0Sstevel@tonic-gate 			break;
5083*0Sstevel@tonic-gate #endif
5084*0Sstevel@tonic-gate 		case 'a':	/* reset access time */
5085*0Sstevel@tonic-gate 			Args |= OCa;
5086*0Sstevel@tonic-gate 			break;
5087*0Sstevel@tonic-gate 		case 'b':	/* swap bytes and halfwords */
5088*0Sstevel@tonic-gate 			Args |= OCb;
5089*0Sstevel@tonic-gate 			break;
5090*0Sstevel@tonic-gate 		case 'c':	/* select character header */
5091*0Sstevel@tonic-gate 			Args |= OCc;
5092*0Sstevel@tonic-gate 			Hdr_type = ASC;
5093*0Sstevel@tonic-gate 			Max_namesz = APATH;
5094*0Sstevel@tonic-gate 			Onecopy = 1;
5095*0Sstevel@tonic-gate 			break;
5096*0Sstevel@tonic-gate 		case 'd':	/* create directories as needed */
5097*0Sstevel@tonic-gate 			Args |= OCd;
5098*0Sstevel@tonic-gate 			break;
5099*0Sstevel@tonic-gate 		case 'f':	/* select files not in patterns */
5100*0Sstevel@tonic-gate 			Args |= OCf;
5101*0Sstevel@tonic-gate 			break;
5102*0Sstevel@tonic-gate 		case 'i':	/* "copy in" */
5103*0Sstevel@tonic-gate 			Args |= OCi;
5104*0Sstevel@tonic-gate 			Archive = 0;
5105*0Sstevel@tonic-gate 			break;
5106*0Sstevel@tonic-gate 		case 'k':	/* retry after I/O errors */
5107*0Sstevel@tonic-gate 			Args |= OCk;
5108*0Sstevel@tonic-gate 			break;
5109*0Sstevel@tonic-gate 		case 'l':	/* link files when possible */
5110*0Sstevel@tonic-gate 			Args |= OCl;
5111*0Sstevel@tonic-gate 			break;
5112*0Sstevel@tonic-gate 		case 'm':	/* retain modification time */
5113*0Sstevel@tonic-gate 			Args |= OCm;
5114*0Sstevel@tonic-gate 			break;
5115*0Sstevel@tonic-gate 		case 'o':	/* "copy out" */
5116*0Sstevel@tonic-gate 			Args |= OCo;
5117*0Sstevel@tonic-gate 			Archive = 1;
5118*0Sstevel@tonic-gate 			break;
5119*0Sstevel@tonic-gate 		case 'p':	/* "pass" */
5120*0Sstevel@tonic-gate 			Max_namesz = APATH;
5121*0Sstevel@tonic-gate 			Args |= OCp;
5122*0Sstevel@tonic-gate 			break;
5123*0Sstevel@tonic-gate 		case 'r':	/* rename files interactively */
5124*0Sstevel@tonic-gate 			Args |= OCr;
5125*0Sstevel@tonic-gate 			break;
5126*0Sstevel@tonic-gate 		case 's':	/* swap bytes */
5127*0Sstevel@tonic-gate 			Args |= OCs;
5128*0Sstevel@tonic-gate 			break;
5129*0Sstevel@tonic-gate 		case 't':	/* table of contents */
5130*0Sstevel@tonic-gate 			Args |= OCt;
5131*0Sstevel@tonic-gate 			break;
5132*0Sstevel@tonic-gate 		case 'u':	/* copy unconditionally */
5133*0Sstevel@tonic-gate 			Args |= OCu;
5134*0Sstevel@tonic-gate 			break;
5135*0Sstevel@tonic-gate 		case 'v':	/* verbose - print file names */
5136*0Sstevel@tonic-gate 			Args |= OCv;
5137*0Sstevel@tonic-gate 			break;
5138*0Sstevel@tonic-gate 		case 'A':	/* append to existing archive */
5139*0Sstevel@tonic-gate 			Args |= OCA;
5140*0Sstevel@tonic-gate 			break;
5141*0Sstevel@tonic-gate 		case 'B':	/* set block size to 5120 bytes */
5142*0Sstevel@tonic-gate 			Args |= OCB;
5143*0Sstevel@tonic-gate 			Bufsize = 5120;
5144*0Sstevel@tonic-gate 			break;
5145*0Sstevel@tonic-gate 		case 'C':	/* set arbitrary block size */
5146*0Sstevel@tonic-gate 			if (Args & OCC)
5147*0Sstevel@tonic-gate 				msg(ERR, dupl_p, 'C');
5148*0Sstevel@tonic-gate 			else {
5149*0Sstevel@tonic-gate 				Args |= OCC;
5150*0Sstevel@tonic-gate 				Bufsize = atoi(optarg);
5151*0Sstevel@tonic-gate 			}
5152*0Sstevel@tonic-gate 			break;
5153*0Sstevel@tonic-gate 		case 'D':
5154*0Sstevel@tonic-gate 			Dflag = 1;
5155*0Sstevel@tonic-gate 			break;
5156*0Sstevel@tonic-gate 		case 'E':	/* alternate file for pattern input */
5157*0Sstevel@tonic-gate 			if (Args & OCE)
5158*0Sstevel@tonic-gate 				msg(ERR, dupl_p, 'E');
5159*0Sstevel@tonic-gate 			else {
5160*0Sstevel@tonic-gate 				Args |= OCE;
5161*0Sstevel@tonic-gate 				Efil_p = optarg;
5162*0Sstevel@tonic-gate 			}
5163*0Sstevel@tonic-gate 			break;
5164*0Sstevel@tonic-gate 		case 'H':	/* select header type */
5165*0Sstevel@tonic-gate 			if (Args & OCH)
5166*0Sstevel@tonic-gate 				msg(ERR, dupl_p, 'H');
5167*0Sstevel@tonic-gate 			else {
5168*0Sstevel@tonic-gate 				Args |= OCH;
5169*0Sstevel@tonic-gate 				Hdr_p = optarg;
5170*0Sstevel@tonic-gate 			}
5171*0Sstevel@tonic-gate 			break;
5172*0Sstevel@tonic-gate 		case 'I':	/* alternate file for archive input */
5173*0Sstevel@tonic-gate 			if (Args & OCI)
5174*0Sstevel@tonic-gate 				msg(ERR, dupl_p, 'I');
5175*0Sstevel@tonic-gate 			else {
5176*0Sstevel@tonic-gate 				Args |= OCI;
5177*0Sstevel@tonic-gate 				IOfil_p = optarg;
5178*0Sstevel@tonic-gate 			}
5179*0Sstevel@tonic-gate 			break;
5180*0Sstevel@tonic-gate 		case 'L':	/* follow symbolic links */
5181*0Sstevel@tonic-gate 			Args |= OCL;
5182*0Sstevel@tonic-gate 			break;
5183*0Sstevel@tonic-gate 		case 'M':	/* specify new end-of-media message */
5184*0Sstevel@tonic-gate 			if (Args & OCM)
5185*0Sstevel@tonic-gate 				msg(ERR, dupl_p, 'M');
5186*0Sstevel@tonic-gate 			else {
5187*0Sstevel@tonic-gate 				Args |= OCM;
5188*0Sstevel@tonic-gate 				Eom_p = optarg;
5189*0Sstevel@tonic-gate 			}
5190*0Sstevel@tonic-gate 			break;
5191*0Sstevel@tonic-gate 		case 'O':	/* alternate file for archive output */
5192*0Sstevel@tonic-gate 			if (Args & OCO)
5193*0Sstevel@tonic-gate 				msg(ERR, dupl_p, 'O');
5194*0Sstevel@tonic-gate 			else {
5195*0Sstevel@tonic-gate 				Args |= OCO;
5196*0Sstevel@tonic-gate 				IOfil_p = optarg;
5197*0Sstevel@tonic-gate 			}
5198*0Sstevel@tonic-gate 			break;
5199*0Sstevel@tonic-gate 		case 'P':	/* preserve acls */
5200*0Sstevel@tonic-gate 			Args |= OCP;
5201*0Sstevel@tonic-gate 			Pflag++;
5202*0Sstevel@tonic-gate 			break;
5203*0Sstevel@tonic-gate 		case 'R':	/* change owner/group of files */
5204*0Sstevel@tonic-gate 			if (Args & OCR)
5205*0Sstevel@tonic-gate 				msg(ERR, dupl_p, 'R');
5206*0Sstevel@tonic-gate 			else {
5207*0Sstevel@tonic-gate 				Args |= OCR;
5208*0Sstevel@tonic-gate 				Own_p = optarg;
5209*0Sstevel@tonic-gate 			}
5210*0Sstevel@tonic-gate 			break;
5211*0Sstevel@tonic-gate 		case 'S':	/* swap halfwords */
5212*0Sstevel@tonic-gate 			Args |= OCS;
5213*0Sstevel@tonic-gate 			break;
5214*0Sstevel@tonic-gate 		case 'V':	/* print a dot '.' for each file */
5215*0Sstevel@tonic-gate 			Args |= OCV;
5216*0Sstevel@tonic-gate 			break;
5217*0Sstevel@tonic-gate 		case '6':	/* for old, sixth-edition files */
5218*0Sstevel@tonic-gate 			Args |= OC6;
5219*0Sstevel@tonic-gate 			Ftype = SIXTH;
5220*0Sstevel@tonic-gate 			break;
5221*0Sstevel@tonic-gate #if defined(O_XATTR)
5222*0Sstevel@tonic-gate 		case '@':
5223*0Sstevel@tonic-gate 			Atflag++;
5224*0Sstevel@tonic-gate 			break;
5225*0Sstevel@tonic-gate #endif
5226*0Sstevel@tonic-gate 		default:
5227*0Sstevel@tonic-gate 			Error_cnt++;
5228*0Sstevel@tonic-gate 		} /* option */
5229*0Sstevel@tonic-gate 	} /* (option = getopt(largc, largv, opts_p)) != EOF */
5230*0Sstevel@tonic-gate 
5231*0Sstevel@tonic-gate #ifdef WAITAROUND
5232*0Sstevel@tonic-gate 	if (waitaround) {
5233*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Rendezvous with cpio on pid"
5234*0Sstevel@tonic-gate 		    " %d\n"), getpid());
5235*0Sstevel@tonic-gate 
5236*0Sstevel@tonic-gate 		while (waitaround) {
5237*0Sstevel@tonic-gate 			(void) sleep(10);
5238*0Sstevel@tonic-gate 		}
5239*0Sstevel@tonic-gate 	}
5240*0Sstevel@tonic-gate #endif
5241*0Sstevel@tonic-gate 
5242*0Sstevel@tonic-gate 	largc -= optind;
5243*0Sstevel@tonic-gate 	largv += optind;
5244*0Sstevel@tonic-gate 	ckopts(Args);
5245*0Sstevel@tonic-gate 	if (!Error_cnt) {
5246*0Sstevel@tonic-gate 		Empty = e_valloc(E_EXIT, TARSZ);
5247*0Sstevel@tonic-gate 		if (Args & OCr) {
5248*0Sstevel@tonic-gate 			Renam_p = e_zalloc(E_EXIT, APATH + 1);
5249*0Sstevel@tonic-gate 			Renametmp_p = e_zalloc(E_EXIT, APATH + 1);
5250*0Sstevel@tonic-gate 		}
5251*0Sstevel@tonic-gate 		Symlnk_p = e_zalloc(E_EXIT, APATH);
5252*0Sstevel@tonic-gate 		Over_p = e_zalloc(E_EXIT, APATH);
5253*0Sstevel@tonic-gate 		Nam_p = e_zalloc(E_EXIT, APATH + 1);
5254*0Sstevel@tonic-gate 		if (Args & OCp) {
5255*0Sstevel@tonic-gate 			Savenam_p = e_zalloc(E_EXIT, APATH + 1);
5256*0Sstevel@tonic-gate 		}
5257*0Sstevel@tonic-gate 		Fullnam_p = e_zalloc(E_EXIT, APATH);
5258*0Sstevel@tonic-gate 		Lnknam_p = e_zalloc(E_EXIT, APATH);
5259*0Sstevel@tonic-gate 		Gen.g_nam_p = Nam_p;
5260*0Sstevel@tonic-gate 		if ((Fullnam_p = getcwd((char *)NULL, APATH)) == (char *)NULL)
5261*0Sstevel@tonic-gate 			msg(EXT, "Unable to determine current directory.");
5262*0Sstevel@tonic-gate 		if (Args & OCi) {
5263*0Sstevel@tonic-gate 			if (largc > 0) /* save patterns for -i option, if any */
5264*0Sstevel@tonic-gate 				Pat_pp = largv;
5265*0Sstevel@tonic-gate 			if (Args & OCE)
5266*0Sstevel@tonic-gate 				getpats(largc, largv);
5267*0Sstevel@tonic-gate 		} else if (Args & OCo) {
5268*0Sstevel@tonic-gate 			if (largc != 0) /* error if arguments left with -o */
5269*0Sstevel@tonic-gate 				Error_cnt++;
5270*0Sstevel@tonic-gate 			else if (fstat(Archive, &ArchSt) < 0)
5271*0Sstevel@tonic-gate 				msg(ERRN, "Error during stat() of archive");
5272*0Sstevel@tonic-gate 			switch (Hdr_type) {
5273*0Sstevel@tonic-gate 			case BIN:
5274*0Sstevel@tonic-gate 				Hdrsz = HDRSZ;
5275*0Sstevel@tonic-gate 				Pad_val = HALFWD;
5276*0Sstevel@tonic-gate 				break;
5277*0Sstevel@tonic-gate 			case CHR:
5278*0Sstevel@tonic-gate 				Hdrsz = CHRSZ;
5279*0Sstevel@tonic-gate 				Pad_val = 0;
5280*0Sstevel@tonic-gate 				Max_offset = (off_t)(CHAR_OFFSET_MAX);
5281*0Sstevel@tonic-gate 				break;
5282*0Sstevel@tonic-gate 			case ASC:
5283*0Sstevel@tonic-gate 			case CRC:
5284*0Sstevel@tonic-gate 				Hdrsz = ASCSZ;
5285*0Sstevel@tonic-gate 				Pad_val = FULLWD;
5286*0Sstevel@tonic-gate 				break;
5287*0Sstevel@tonic-gate 			case TAR:
5288*0Sstevel@tonic-gate 			/* FALLTHROUGH */
5289*0Sstevel@tonic-gate 			case USTAR: /* TAR and USTAR */
5290*0Sstevel@tonic-gate 				Hdrsz = TARSZ;
5291*0Sstevel@tonic-gate 				Pad_val = FULLBK;
5292*0Sstevel@tonic-gate 				Max_offset = (off_t)(CHAR_OFFSET_MAX);
5293*0Sstevel@tonic-gate 				break;
5294*0Sstevel@tonic-gate 			default:
5295*0Sstevel@tonic-gate 				msg(EXT, "Impossible header type.");
5296*0Sstevel@tonic-gate 			}
5297*0Sstevel@tonic-gate 		} else { /* directory must be specified */
5298*0Sstevel@tonic-gate 			if (largc != 1)
5299*0Sstevel@tonic-gate 				Error_cnt++;
5300*0Sstevel@tonic-gate 			else if (access(*largv, 2) < 0)
5301*0Sstevel@tonic-gate 				msg(ERRN,
5302*0Sstevel@tonic-gate 				    "Error during access() of \"%s\"", *largv);
5303*0Sstevel@tonic-gate 		}
5304*0Sstevel@tonic-gate 	}
5305*0Sstevel@tonic-gate 	if (Error_cnt)
5306*0Sstevel@tonic-gate 		usage(); /* exits! */
5307*0Sstevel@tonic-gate 	if (Args & (OCi | OCo)) {
5308*0Sstevel@tonic-gate 		if (!Dflag) {
5309*0Sstevel@tonic-gate 			if (Args & (OCB | OCC)) {
5310*0Sstevel@tonic-gate 				if (g_init(&Device, &Archive) < 0)
5311*0Sstevel@tonic-gate 					msg(EXTN,
5312*0Sstevel@tonic-gate 					    "Error during initialization");
5313*0Sstevel@tonic-gate 			} else {
5314*0Sstevel@tonic-gate 				if ((Bufsize = g_init(&Device, &Archive)) < 0)
5315*0Sstevel@tonic-gate 					msg(EXTN,
5316*0Sstevel@tonic-gate 					    "Error during initialization");
5317*0Sstevel@tonic-gate 			}
5318*0Sstevel@tonic-gate 		}
5319*0Sstevel@tonic-gate 
5320*0Sstevel@tonic-gate 		blk_cnt_max = _20K / Bufsize;
5321*0Sstevel@tonic-gate 		if (blk_cnt_max < MX_BUFS) {
5322*0Sstevel@tonic-gate 			blk_cnt_max = MX_BUFS;
5323*0Sstevel@tonic-gate 		}
5324*0Sstevel@tonic-gate 
5325*0Sstevel@tonic-gate 		Buffr.b_base_p = NULL;
5326*0Sstevel@tonic-gate 
5327*0Sstevel@tonic-gate 		for (blk_cnt = blk_cnt_max; blk_cnt > 1; blk_cnt--) {
5328*0Sstevel@tonic-gate 			Buffr.b_size = (size_t)(Bufsize * blk_cnt);
5329*0Sstevel@tonic-gate 			Buffr.b_base_p = e_valloc(E_NORMAL, Buffr.b_size);
5330*0Sstevel@tonic-gate 			if (Buffr.b_base_p != NULL) {
5331*0Sstevel@tonic-gate 				break;
5332*0Sstevel@tonic-gate 			}
5333*0Sstevel@tonic-gate 		}
5334*0Sstevel@tonic-gate 		if (Buffr.b_base_p == NULL || Buffr.b_size < (2 * CPIOBSZ)) {
5335*0Sstevel@tonic-gate 			msg(EXT, "Out of memory");
5336*0Sstevel@tonic-gate 		}
5337*0Sstevel@tonic-gate 
5338*0Sstevel@tonic-gate 		Buffr.b_out_p = Buffr.b_in_p = Buffr.b_base_p;
5339*0Sstevel@tonic-gate 		Buffr.b_cnt = 0L;
5340*0Sstevel@tonic-gate 		Buffr.b_end_p = Buffr.b_base_p + Buffr.b_size;
5341*0Sstevel@tonic-gate 	}
5342*0Sstevel@tonic-gate 
5343*0Sstevel@tonic-gate 	/*
5344*0Sstevel@tonic-gate 	 * Now that Bufsize has stabilized, we can allocate our i/o buffer
5345*0Sstevel@tonic-gate 	 */
5346*0Sstevel@tonic-gate 	Buf_p = e_valloc(E_EXIT, Bufsize);
5347*0Sstevel@tonic-gate 
5348*0Sstevel@tonic-gate 	if (Args & OCp) { /* get destination directory */
5349*0Sstevel@tonic-gate 		(void) strcpy(Fullnam_p, *largv);
5350*0Sstevel@tonic-gate 		if (stat(Fullnam_p, &DesSt) < 0)
5351*0Sstevel@tonic-gate 			msg(EXTN, "Error during stat() of \"%s\"", Fullnam_p);
5352*0Sstevel@tonic-gate 		if ((DesSt.st_mode & Ftype) != S_IFDIR)
5353*0Sstevel@tonic-gate 			msg(EXT, "\"%s\" is not a directory", Fullnam_p);
5354*0Sstevel@tonic-gate 	}
5355*0Sstevel@tonic-gate 	Full_p = Fullnam_p + strlen(Fullnam_p) - 1;
5356*0Sstevel@tonic-gate 	if (*Full_p != '/') {
5357*0Sstevel@tonic-gate 		Full_p++;
5358*0Sstevel@tonic-gate 		*Full_p = '/';
5359*0Sstevel@tonic-gate 	}
5360*0Sstevel@tonic-gate 	Full_p++;
5361*0Sstevel@tonic-gate 	*Full_p = '\0';
5362*0Sstevel@tonic-gate 	(void) strcpy(Lnknam_p, Fullnam_p);
5363*0Sstevel@tonic-gate 	Lnkend_p = Lnknam_p + strlen(Lnknam_p);
5364*0Sstevel@tonic-gate 	(void) getrlimit(RLIMIT_FSIZE, &rlim);
5365*0Sstevel@tonic-gate 	Max_filesz = (off_t)rlim.rlim_cur;
5366*0Sstevel@tonic-gate 	Lnk_hd.L_nxt_p = Lnk_hd.L_bck_p = &Lnk_hd;
5367*0Sstevel@tonic-gate 	Lnk_hd.L_lnk_p = (struct Lnk *)NULL;
5368*0Sstevel@tonic-gate }
5369*0Sstevel@tonic-gate 
5370*0Sstevel@tonic-gate /*
5371*0Sstevel@tonic-gate  * set_tym: Set the access and/or modification times for a file.
5372*0Sstevel@tonic-gate  */
5373*0Sstevel@tonic-gate 
5374*0Sstevel@tonic-gate static void
5375*0Sstevel@tonic-gate set_tym(int dirfd, char *nam_p, time_t atime, time_t mtime)
5376*0Sstevel@tonic-gate {
5377*0Sstevel@tonic-gate 	struct timeval times[2];
5378*0Sstevel@tonic-gate 
5379*0Sstevel@tonic-gate 	times[0].tv_sec = atime;
5380*0Sstevel@tonic-gate 	times[0].tv_usec = 0;
5381*0Sstevel@tonic-gate 	times[1].tv_sec = mtime;
5382*0Sstevel@tonic-gate 	times[1].tv_usec = 0;
5383*0Sstevel@tonic-gate 
5384*0Sstevel@tonic-gate 	if (futimesat(dirfd, nam_p, times) < 0) {
5385*0Sstevel@tonic-gate 		if (Args & OCa) {
5386*0Sstevel@tonic-gate 			msg(ERRN,
5387*0Sstevel@tonic-gate 			    "Unable to reset access time for \"%s%s%s\"",
5388*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
5389*0Sstevel@tonic-gate 			    nam_p : Fullnam_p,
5390*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
5391*0Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
5392*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
5393*0Sstevel@tonic-gate 			    "" : nam_p);
5394*0Sstevel@tonic-gate 		} else {
5395*0Sstevel@tonic-gate 			msg(ERRN,
5396*0Sstevel@tonic-gate 			    "Unable to reset modification time for \"%s%s%s\"",
5397*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
5398*0Sstevel@tonic-gate 			    nam_p : Fullnam_p,
5399*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
5400*0Sstevel@tonic-gate 			    "" : gettext(" Attribute "),
5401*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL) ?
5402*0Sstevel@tonic-gate 			    "" : nam_p);
5403*0Sstevel@tonic-gate 		}
5404*0Sstevel@tonic-gate 	}
5405*0Sstevel@tonic-gate }
5406*0Sstevel@tonic-gate 
5407*0Sstevel@tonic-gate /*
5408*0Sstevel@tonic-gate  * sigint:  Catch interrupts.  If an interrupt occurs during the extraction
5409*0Sstevel@tonic-gate  * of a file from the archive with the -u option set, and the filename did
5410*0Sstevel@tonic-gate  * exist, remove the current file and restore the original file.  Then exit.
5411*0Sstevel@tonic-gate  */
5412*0Sstevel@tonic-gate 
5413*0Sstevel@tonic-gate /*ARGSUSED*/
5414*0Sstevel@tonic-gate static void
5415*0Sstevel@tonic-gate sigint(int sig)
5416*0Sstevel@tonic-gate {
5417*0Sstevel@tonic-gate 	char *nam_p;
5418*0Sstevel@tonic-gate 
5419*0Sstevel@tonic-gate 	(void) signal(SIGINT, SIG_IGN); /* block further signals */
5420*0Sstevel@tonic-gate 	if (!Finished) {
5421*0Sstevel@tonic-gate 		if (Args & OCi)
5422*0Sstevel@tonic-gate 			nam_p = G_p->g_nam_p;
5423*0Sstevel@tonic-gate 		else /* OCp */
5424*0Sstevel@tonic-gate 			nam_p = Fullnam_p;
5425*0Sstevel@tonic-gate 		if (*Over_p != '\0') { /* There is a temp file */
5426*0Sstevel@tonic-gate 			if (unlink(nam_p) < 0) {
5427*0Sstevel@tonic-gate 				msg(ERRN,
5428*0Sstevel@tonic-gate 				    "Cannot remove incomplete \"%s\"", nam_p);
5429*0Sstevel@tonic-gate 			}
5430*0Sstevel@tonic-gate 			if (rename(Over_p, nam_p) < 0) {
5431*0Sstevel@tonic-gate 				if (link(Over_p, nam_p) < 0) {
5432*0Sstevel@tonic-gate 					msg(ERRN,
5433*0Sstevel@tonic-gate 					    "Cannot recover original \"%s\"",
5434*0Sstevel@tonic-gate 					    nam_p);
5435*0Sstevel@tonic-gate 				}
5436*0Sstevel@tonic-gate 				if (unlink(Over_p)) {
5437*0Sstevel@tonic-gate 					msg(ERRN,
5438*0Sstevel@tonic-gate 					    "Cannot remove temp file \"%s\"",
5439*0Sstevel@tonic-gate 					    Over_p);
5440*0Sstevel@tonic-gate 				}
5441*0Sstevel@tonic-gate 			}
5442*0Sstevel@tonic-gate 		} else if (unlink(nam_p))
5443*0Sstevel@tonic-gate 			msg(ERRN,
5444*0Sstevel@tonic-gate 			    "Cannot remove incomplete \"%s\"", nam_p);
5445*0Sstevel@tonic-gate 			*Over_p = '\0';
5446*0Sstevel@tonic-gate 	}
5447*0Sstevel@tonic-gate 	exit(EXIT_CODE);
5448*0Sstevel@tonic-gate }
5449*0Sstevel@tonic-gate 
5450*0Sstevel@tonic-gate /*
5451*0Sstevel@tonic-gate  * swap: Swap bytes (-s), halfwords (-S) or or both halfwords and bytes (-b).
5452*0Sstevel@tonic-gate  */
5453*0Sstevel@tonic-gate 
5454*0Sstevel@tonic-gate static void
5455*0Sstevel@tonic-gate swap(char *buf_p, int cnt)
5456*0Sstevel@tonic-gate {
5457*0Sstevel@tonic-gate 	unsigned char tbyte;
5458*0Sstevel@tonic-gate 	int tcnt;
5459*0Sstevel@tonic-gate 	int rcnt;
5460*0Sstevel@tonic-gate 	ushort_t thalf;
5461*0Sstevel@tonic-gate 
5462*0Sstevel@tonic-gate 	rcnt = cnt % 4;
5463*0Sstevel@tonic-gate 	cnt /= 4;
5464*0Sstevel@tonic-gate 	if (Args & (OCb | OCs | BSM)) {
5465*0Sstevel@tonic-gate 		tcnt = cnt;
5466*0Sstevel@tonic-gate 		/* LINTED alignment */
5467*0Sstevel@tonic-gate 		Swp_p = (union swpbuf *)buf_p;
5468*0Sstevel@tonic-gate 		while (tcnt-- > 0) {
5469*0Sstevel@tonic-gate 			tbyte = Swp_p->s_byte[0];
5470*0Sstevel@tonic-gate 			Swp_p->s_byte[0] = Swp_p->s_byte[1];
5471*0Sstevel@tonic-gate 			Swp_p->s_byte[1] = tbyte;
5472*0Sstevel@tonic-gate 			tbyte = Swp_p->s_byte[2];
5473*0Sstevel@tonic-gate 			Swp_p->s_byte[2] = Swp_p->s_byte[3];
5474*0Sstevel@tonic-gate 			Swp_p->s_byte[3] = tbyte;
5475*0Sstevel@tonic-gate 			Swp_p++;
5476*0Sstevel@tonic-gate 		}
5477*0Sstevel@tonic-gate 		if (rcnt >= 2) {
5478*0Sstevel@tonic-gate 		tbyte = Swp_p->s_byte[0];
5479*0Sstevel@tonic-gate 		Swp_p->s_byte[0] = Swp_p->s_byte[1];
5480*0Sstevel@tonic-gate 		Swp_p->s_byte[1] = tbyte;
5481*0Sstevel@tonic-gate 		tbyte = Swp_p->s_byte[2];
5482*0Sstevel@tonic-gate 		}
5483*0Sstevel@tonic-gate 	}
5484*0Sstevel@tonic-gate 	if (Args & (OCb | OCS)) {
5485*0Sstevel@tonic-gate 		tcnt = cnt;
5486*0Sstevel@tonic-gate 		/* LINTED alignment */
5487*0Sstevel@tonic-gate 		Swp_p = (union swpbuf *)buf_p;
5488*0Sstevel@tonic-gate 		while (tcnt-- > 0) {
5489*0Sstevel@tonic-gate 			thalf = Swp_p->s_half[0];
5490*0Sstevel@tonic-gate 			Swp_p->s_half[0] = Swp_p->s_half[1];
5491*0Sstevel@tonic-gate 			Swp_p->s_half[1] = thalf;
5492*0Sstevel@tonic-gate 			Swp_p++;
5493*0Sstevel@tonic-gate 		}
5494*0Sstevel@tonic-gate 	}
5495*0Sstevel@tonic-gate }
5496*0Sstevel@tonic-gate 
5497*0Sstevel@tonic-gate /*
5498*0Sstevel@tonic-gate  * usage: Print the usage message on stderr and exit.
5499*0Sstevel@tonic-gate  */
5500*0Sstevel@tonic-gate 
5501*0Sstevel@tonic-gate static void
5502*0Sstevel@tonic-gate usage(void)
5503*0Sstevel@tonic-gate {
5504*0Sstevel@tonic-gate 
5505*0Sstevel@tonic-gate 	(void) fflush(stdout);
5506*0Sstevel@tonic-gate #if defined(O_XATTR)
5507*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("USAGE:\n"
5508*0Sstevel@tonic-gate 				"\tcpio -i[bcdfkmrstuv@BSV6] [-C size] "
5509*0Sstevel@tonic-gate 				"[-E file] [-H hdr] [-I file [-M msg]] "
5510*0Sstevel@tonic-gate 				"[-R id] [patterns]\n"
5511*0Sstevel@tonic-gate 				"\tcpio -o[acv@ABLV] [-C size] "
5512*0Sstevel@tonic-gate 				"[-H hdr] [-O file [-M msg]]\n"
5513*0Sstevel@tonic-gate 				"\tcpio -p[adlmuv@LV] [-R id] directory\n"));
5514*0Sstevel@tonic-gate #else
5515*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("USAGE:\n"
5516*0Sstevel@tonic-gate 				"\tcpio -i[bcdfkmrstuvBSV6] [-C size] "
5517*0Sstevel@tonic-gate 				"[-E file] [-H hdr] [-I file [-M msg]] "
5518*0Sstevel@tonic-gate 				"[-R id] [patterns]\n"
5519*0Sstevel@tonic-gate 				"\tcpio -o[acvABLV] [-C size] "
5520*0Sstevel@tonic-gate 				"[-H hdr] [-O file [-M msg]]\n"
5521*0Sstevel@tonic-gate 				"\tcpio -p[adlmuvLV] [-R id] directory\n"));
5522*0Sstevel@tonic-gate #endif
5523*0Sstevel@tonic-gate 	(void) fflush(stderr);
5524*0Sstevel@tonic-gate 	exit(EXIT_CODE);
5525*0Sstevel@tonic-gate }
5526*0Sstevel@tonic-gate 
5527*0Sstevel@tonic-gate /*
5528*0Sstevel@tonic-gate  * verbose: For each file, print either the filename (-v) or a dot (-V).
5529*0Sstevel@tonic-gate  * If the -t option (table of contents) is set, print either the filename,
5530*0Sstevel@tonic-gate  * or if the -v option is also set, print an "ls -l"-like listing.
5531*0Sstevel@tonic-gate  */
5532*0Sstevel@tonic-gate 
5533*0Sstevel@tonic-gate static void
5534*0Sstevel@tonic-gate verbose(char *nam_p)
5535*0Sstevel@tonic-gate {
5536*0Sstevel@tonic-gate 	int i, j, temp;
5537*0Sstevel@tonic-gate 	mode_t mode;
5538*0Sstevel@tonic-gate 	char modestr[12];
5539*0Sstevel@tonic-gate 
5540*0Sstevel@tonic-gate 	/*
5541*0Sstevel@tonic-gate 	 * The printf format and associated arguments to print the current
5542*0Sstevel@tonic-gate 	 * filename.  Normally, just nam_p.  If we're processing an extended
5543*0Sstevel@tonic-gate 	 * attribute, these are overridden.
5544*0Sstevel@tonic-gate 	 */
5545*0Sstevel@tonic-gate 	char *name_fmt = "%s";
5546*0Sstevel@tonic-gate 	const char *name = nam_p;
5547*0Sstevel@tonic-gate 	const char *attribute = NULL;
5548*0Sstevel@tonic-gate 
5549*0Sstevel@tonic-gate 	if (Gen.g_attrnam_p != (char *)NULL) {
5550*0Sstevel@tonic-gate 		/*
5551*0Sstevel@tonic-gate 		 * Translation note:
5552*0Sstevel@tonic-gate 		 * 'attribute' is a noun.
5553*0Sstevel@tonic-gate 		 */
5554*0Sstevel@tonic-gate 		name_fmt = gettext("%s attribute %s");
5555*0Sstevel@tonic-gate 
5556*0Sstevel@tonic-gate 		name = (Args & OCp) ? nam_p : Gen.g_attrfnam_p;
5557*0Sstevel@tonic-gate 		attribute = Gen.g_attrnam_p;
5558*0Sstevel@tonic-gate 	}
5559*0Sstevel@tonic-gate 
5560*0Sstevel@tonic-gate 	if ((Gen.g_mode == SECMODE) || ((Hdr_type == USTAR ||
5561*0Sstevel@tonic-gate 	    Hdr_type == TAR) && Thdr_p->tbuf.t_typeflag == 'A')) {
5562*0Sstevel@tonic-gate 		/* dont print ancillary file */
5563*0Sstevel@tonic-gate 		aclchar = '+';
5564*0Sstevel@tonic-gate 		return;
5565*0Sstevel@tonic-gate 	}
5566*0Sstevel@tonic-gate 	for (i = 0; i < 11; i++)
5567*0Sstevel@tonic-gate 		modestr[i] = '-';
5568*0Sstevel@tonic-gate 	modestr[i] = '\0';
5569*0Sstevel@tonic-gate 	modestr[i-1] = aclchar;
5570*0Sstevel@tonic-gate 	aclchar = ' ';
5571*0Sstevel@tonic-gate 
5572*0Sstevel@tonic-gate 	if ((Args & OCt) && (Args & OCv)) {
5573*0Sstevel@tonic-gate 		mode = Gen.g_mode;
5574*0Sstevel@tonic-gate 		for (i = 0; i < 3; i++) {
5575*0Sstevel@tonic-gate 			temp = (mode >> (6 - (i * 3)));
5576*0Sstevel@tonic-gate 			j = (i * 3) + 1;
5577*0Sstevel@tonic-gate 			if (S_IROTH & temp)
5578*0Sstevel@tonic-gate 				modestr[j] = 'r';
5579*0Sstevel@tonic-gate 			if (S_IWOTH & temp)
5580*0Sstevel@tonic-gate 				modestr[j + 1] = 'w';
5581*0Sstevel@tonic-gate 			if (S_IXOTH & temp)
5582*0Sstevel@tonic-gate 				modestr[j + 2] = 'x';
5583*0Sstevel@tonic-gate 		}
5584*0Sstevel@tonic-gate 
5585*0Sstevel@tonic-gate 		if (Hdr_type != BAR) {
5586*0Sstevel@tonic-gate 			temp = Gen.g_mode & Ftype;
5587*0Sstevel@tonic-gate 			switch (temp) {
5588*0Sstevel@tonic-gate 			case (S_IFIFO):
5589*0Sstevel@tonic-gate 				modestr[0] = 'p';
5590*0Sstevel@tonic-gate 				break;
5591*0Sstevel@tonic-gate 			case (S_IFCHR):
5592*0Sstevel@tonic-gate 				modestr[0] = 'c';
5593*0Sstevel@tonic-gate 				break;
5594*0Sstevel@tonic-gate 			case (S_IFDIR):
5595*0Sstevel@tonic-gate 				modestr[0] = 'd';
5596*0Sstevel@tonic-gate 				break;
5597*0Sstevel@tonic-gate 			case (S_IFBLK):
5598*0Sstevel@tonic-gate 				modestr[0] = 'b';
5599*0Sstevel@tonic-gate 				break;
5600*0Sstevel@tonic-gate 			case (S_IFREG): /* was initialized to '-' */
5601*0Sstevel@tonic-gate 				break;
5602*0Sstevel@tonic-gate 			case (S_IFLNK):
5603*0Sstevel@tonic-gate 				modestr[0] = 'l';
5604*0Sstevel@tonic-gate 				break;
5605*0Sstevel@tonic-gate 			default:
5606*0Sstevel@tonic-gate 				msg(ERR, "Impossible file type");
5607*0Sstevel@tonic-gate 			}
5608*0Sstevel@tonic-gate 		} else {		/* bar */
5609*0Sstevel@tonic-gate 			temp = Gen.g_mode & Ftype;
5610*0Sstevel@tonic-gate 			switch (temp) {
5611*0Sstevel@tonic-gate 			case (S_IFIFO):
5612*0Sstevel@tonic-gate 				modestr[0] = 'p';
5613*0Sstevel@tonic-gate 				break;
5614*0Sstevel@tonic-gate 			case (S_IFCHR):
5615*0Sstevel@tonic-gate 				modestr[0] = 'c';
5616*0Sstevel@tonic-gate 				break;
5617*0Sstevel@tonic-gate 			case (S_IFDIR):
5618*0Sstevel@tonic-gate 				modestr[0] = 'd';
5619*0Sstevel@tonic-gate 				break;
5620*0Sstevel@tonic-gate 			case (S_IFBLK):
5621*0Sstevel@tonic-gate 				modestr[0] = 'b';
5622*0Sstevel@tonic-gate 				break;
5623*0Sstevel@tonic-gate 			}
5624*0Sstevel@tonic-gate 			if (bar_linkflag == SYMTYPE)
5625*0Sstevel@tonic-gate 				modestr[0] = 'l';
5626*0Sstevel@tonic-gate 		}
5627*0Sstevel@tonic-gate 		if ((S_ISUID & Gen.g_mode) == S_ISUID)
5628*0Sstevel@tonic-gate 			modestr[3] = 's';
5629*0Sstevel@tonic-gate 		if ((S_ISVTX & Gen.g_mode) == S_ISVTX)
5630*0Sstevel@tonic-gate 			modestr[9] = 't';
5631*0Sstevel@tonic-gate 		if ((S_ISGID & G_p->g_mode) == S_ISGID && modestr[6] == 'x')
5632*0Sstevel@tonic-gate 			modestr[6] = 's';
5633*0Sstevel@tonic-gate 		else if ((S_ENFMT & Gen.g_mode) == S_ENFMT && modestr[6] != 'x')
5634*0Sstevel@tonic-gate 			modestr[6] = 'l';
5635*0Sstevel@tonic-gate 		if ((Hdr_type == TAR || Hdr_type == USTAR) && Gen.g_nlink == 0)
5636*0Sstevel@tonic-gate 			(void) printf("%s%4d ", modestr, (int)Gen.g_nlink+1);
5637*0Sstevel@tonic-gate 		else
5638*0Sstevel@tonic-gate 			(void) printf("%s%4d ", modestr, (int)Gen.g_nlink);
5639*0Sstevel@tonic-gate 		if (Lastuid == (int)Gen.g_uid) {
5640*0Sstevel@tonic-gate 			if (Lastuid == -1)
5641*0Sstevel@tonic-gate 				(void) printf("-1       ");
5642*0Sstevel@tonic-gate 			else
5643*0Sstevel@tonic-gate 				(void) printf("%-9s", Curpw_p->pw_name);
5644*0Sstevel@tonic-gate 		} else {
5645*0Sstevel@tonic-gate 			if (Curpw_p = getpwuid((int)Gen.g_uid)) {
5646*0Sstevel@tonic-gate 				(void) printf("%-9s", Curpw_p->pw_name);
5647*0Sstevel@tonic-gate 				Lastuid = (int)Gen.g_uid;
5648*0Sstevel@tonic-gate 			} else {
5649*0Sstevel@tonic-gate 				(void) printf("%-9d", (int)Gen.g_uid);
5650*0Sstevel@tonic-gate 				Lastuid = -1;
5651*0Sstevel@tonic-gate 			}
5652*0Sstevel@tonic-gate 		}
5653*0Sstevel@tonic-gate 		if (Lastgid == (int)Gen.g_gid) {
5654*0Sstevel@tonic-gate 			if (Lastgid == -1)
5655*0Sstevel@tonic-gate 				(void) printf("-1       ");
5656*0Sstevel@tonic-gate 			else
5657*0Sstevel@tonic-gate 				(void) printf("%-9s", Curgr_p->gr_name);
5658*0Sstevel@tonic-gate 		} else {
5659*0Sstevel@tonic-gate 			if (Curgr_p = getgrgid((int)Gen.g_gid)) {
5660*0Sstevel@tonic-gate 				(void) printf("%-9s", Curgr_p->gr_name);
5661*0Sstevel@tonic-gate 				Lastgid = (int)Gen.g_gid;
5662*0Sstevel@tonic-gate 			} else {
5663*0Sstevel@tonic-gate 				(void) printf("%-9d", (int)Gen.g_gid);
5664*0Sstevel@tonic-gate 				Lastgid = -1;
5665*0Sstevel@tonic-gate 			}
5666*0Sstevel@tonic-gate 		}
5667*0Sstevel@tonic-gate 
5668*0Sstevel@tonic-gate 		/* print file size */
5669*0Sstevel@tonic-gate 		if (!Aspec || ((Gen.g_mode & Ftype) == S_IFIFO) ||
5670*0Sstevel@tonic-gate 		    (Hdr_type == BAR && bar_linkflag == SYMTYPE)) {
5671*0Sstevel@tonic-gate 			if (Gen.g_filesz < (1LL << 31))
5672*0Sstevel@tonic-gate 				(void) printf("%7lld ",
5673*0Sstevel@tonic-gate 				    (offset_t)Gen.g_filesz);
5674*0Sstevel@tonic-gate 			else
5675*0Sstevel@tonic-gate 				(void) printf("%11lld ",
5676*0Sstevel@tonic-gate 				    (offset_t)Gen.g_filesz);
5677*0Sstevel@tonic-gate 		} else
5678*0Sstevel@tonic-gate 			(void) printf("%3d,%3d ", (int)major(Gen.g_rdev),
5679*0Sstevel@tonic-gate 			    (int)minor(Gen.g_rdev));
5680*0Sstevel@tonic-gate 		(void) cftime(Time, dcgettext(NULL, FORMAT, LC_TIME),
5681*0Sstevel@tonic-gate 		    (time_t *)&Gen.g_mtime);
5682*0Sstevel@tonic-gate 		(void) printf("%s, ", Time);
5683*0Sstevel@tonic-gate 		(void) printf(name_fmt, name, attribute);
5684*0Sstevel@tonic-gate 		if ((Gen.g_mode & Ftype) == S_IFLNK) {
5685*0Sstevel@tonic-gate 			if (Hdr_type == USTAR || Hdr_type == TAR)
5686*0Sstevel@tonic-gate 				(void) strcpy(Symlnk_p,
5687*0Sstevel@tonic-gate 				    Thdr_p->tbuf.t_linkname);
5688*0Sstevel@tonic-gate 			else {
5689*0Sstevel@tonic-gate 				(void) strncpy(Symlnk_p, Buffr.b_out_p,
5690*0Sstevel@tonic-gate 				    Gen.g_filesz);
5691*0Sstevel@tonic-gate 				*(Symlnk_p + Gen.g_filesz) = '\0';
5692*0Sstevel@tonic-gate 			}
5693*0Sstevel@tonic-gate 			(void) printf(" -> %s", Symlnk_p);
5694*0Sstevel@tonic-gate 		}
5695*0Sstevel@tonic-gate 		if (Hdr_type == BAR) {
5696*0Sstevel@tonic-gate 			if (bar_linkflag == SYMTYPE)
5697*0Sstevel@tonic-gate 				(void) printf(gettext(" symbolic link to %s"),
5698*0Sstevel@tonic-gate 				    bar_linkname);
5699*0Sstevel@tonic-gate 			else if (bar_linkflag == '1')
5700*0Sstevel@tonic-gate 				(void) printf(gettext(" linked to %s"),
5701*0Sstevel@tonic-gate 				    bar_linkname);
5702*0Sstevel@tonic-gate 		}
5703*0Sstevel@tonic-gate 		if ((Hdr_type == USTAR || Hdr_type == TAR) &&
5704*0Sstevel@tonic-gate 		    Thdr_p->tbuf.t_typeflag == '1') {
5705*0Sstevel@tonic-gate 			(void) printf(gettext(" linked to %s%s%s"),
5706*0Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
5707*0Sstevel@tonic-gate 			    Thdr_p->tbuf.t_linkname : Gen.g_attrfnam_p,
5708*0Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
5709*0Sstevel@tonic-gate 			    "" : gettext(" attribute "),
5710*0Sstevel@tonic-gate 			    (Gen.g_attrnam_p == (char *)NULL) ?
5711*0Sstevel@tonic-gate 			    "" : Gen.g_linktoattrnam_p);
5712*0Sstevel@tonic-gate 		}
5713*0Sstevel@tonic-gate 		(void) printf("\n");
5714*0Sstevel@tonic-gate 	} else if ((Args & OCt) || (Args & OCv)) {
5715*0Sstevel@tonic-gate 		(void) fprintf(Out_p, name_fmt, name, attribute);
5716*0Sstevel@tonic-gate 		(void) fputc('\n', Out_p);
5717*0Sstevel@tonic-gate 	} else { /* OCV */
5718*0Sstevel@tonic-gate 		(void) fputc('.', Out_p);
5719*0Sstevel@tonic-gate 		if (Verbcnt++ >= 49) { /* start a new line of dots */
5720*0Sstevel@tonic-gate 			Verbcnt = 0;
5721*0Sstevel@tonic-gate 			(void) fputc('\n', Out_p);
5722*0Sstevel@tonic-gate 		}
5723*0Sstevel@tonic-gate 	}
5724*0Sstevel@tonic-gate 	(void) fflush(Out_p);
5725*0Sstevel@tonic-gate }
5726*0Sstevel@tonic-gate 
5727*0Sstevel@tonic-gate #define	MK_USHORT(a)	(a & 00000177777)
5728*0Sstevel@tonic-gate 
5729*0Sstevel@tonic-gate /*
5730*0Sstevel@tonic-gate  * write_hdr: Transfer header information for the generic structure
5731*0Sstevel@tonic-gate  * into the format for the selected header and bwrite() the header.
5732*0Sstevel@tonic-gate  * ACL support: add two new argumnets. secflag indicates that it's an
5733*0Sstevel@tonic-gate  *	ancillary file. len is the size of the file (incl. all security
5734*0Sstevel@tonic-gate  *	attributes). We only have acls now.
5735*0Sstevel@tonic-gate  */
5736*0Sstevel@tonic-gate 
5737*0Sstevel@tonic-gate static void
5738*0Sstevel@tonic-gate write_hdr(int secflag, off_t len)
5739*0Sstevel@tonic-gate {
5740*0Sstevel@tonic-gate 	int cnt, pad;
5741*0Sstevel@tonic-gate 	mode_t mode;
5742*0Sstevel@tonic-gate 	uid_t uid;
5743*0Sstevel@tonic-gate 	gid_t gid;
5744*0Sstevel@tonic-gate 	const char warnfmt[] = "%s%s%s : %s";
5745*0Sstevel@tonic-gate 
5746*0Sstevel@tonic-gate 	if (secflag == ARCHIVE_ACL) {
5747*0Sstevel@tonic-gate 		mode = SECMODE;
5748*0Sstevel@tonic-gate 	} else {
5749*0Sstevel@tonic-gate 		/*
5750*0Sstevel@tonic-gate 		 * If attribute is being archived in cpio format then
5751*0Sstevel@tonic-gate 		 * zap off the file type bits since those are truly a
5752*0Sstevel@tonic-gate 		 * mask and reset them with _XATTR_CPIO_MODE
5753*0Sstevel@tonic-gate 		 */
5754*0Sstevel@tonic-gate 
5755*0Sstevel@tonic-gate 		/*
5756*0Sstevel@tonic-gate 		 * len is the value of g_filesz for normal files
5757*0Sstevel@tonic-gate 		 * and the length of the special header buffer in
5758*0Sstevel@tonic-gate 		 * the case of acl and xattr headers.
5759*0Sstevel@tonic-gate 		 */
5760*0Sstevel@tonic-gate 		if (G_p->g_attrnam_p != (char *)NULL && Hdr_type != USTAR &&
5761*0Sstevel@tonic-gate 		    Hdr_type != TAR) {
5762*0Sstevel@tonic-gate 			mode = (G_p->g_mode & S_IAMB) | _XATTR_CPIO_MODE;
5763*0Sstevel@tonic-gate 		} else {
5764*0Sstevel@tonic-gate 			mode = G_p->g_mode;
5765*0Sstevel@tonic-gate 		}
5766*0Sstevel@tonic-gate 		if (secflag != ARCHIVE_XATTR) {
5767*0Sstevel@tonic-gate 			len = G_p->g_filesz;
5768*0Sstevel@tonic-gate 		}
5769*0Sstevel@tonic-gate 	}
5770*0Sstevel@tonic-gate 
5771*0Sstevel@tonic-gate 	uid = G_p->g_uid;
5772*0Sstevel@tonic-gate 	gid = G_p->g_gid;
5773*0Sstevel@tonic-gate 	/*
5774*0Sstevel@tonic-gate 	 * Handle EFT uids and gids.  If they get too big
5775*0Sstevel@tonic-gate 	 * to be represented in a particular format, force 'em to 'nobody'.
5776*0Sstevel@tonic-gate 	 */
5777*0Sstevel@tonic-gate 	switch (Hdr_type) {
5778*0Sstevel@tonic-gate 	case BIN:			/* 16-bits of u_short */
5779*0Sstevel@tonic-gate 		if ((ulong_t)uid > (ulong_t)USHRT_MAX)
5780*0Sstevel@tonic-gate 			uid = UID_NOBODY;
5781*0Sstevel@tonic-gate 		if ((ulong_t)gid > (ulong_t)USHRT_MAX)
5782*0Sstevel@tonic-gate 			gid = GID_NOBODY;
5783*0Sstevel@tonic-gate 		break;
5784*0Sstevel@tonic-gate 	case CHR:			/* %.6lo => 262143 base 10 */
5785*0Sstevel@tonic-gate 		if ((ulong_t)uid > (ulong_t)0777777)
5786*0Sstevel@tonic-gate 			uid = UID_NOBODY;
5787*0Sstevel@tonic-gate 		if ((ulong_t)gid > (ulong_t)0777777)
5788*0Sstevel@tonic-gate 			gid = GID_NOBODY;
5789*0Sstevel@tonic-gate 		break;
5790*0Sstevel@tonic-gate 	case ASC:			/* %.8lx => full 32 bits */
5791*0Sstevel@tonic-gate 	case CRC:
5792*0Sstevel@tonic-gate 		break;
5793*0Sstevel@tonic-gate 	case USTAR:
5794*0Sstevel@tonic-gate 	case TAR:			/* %.7lo => 2097151 base 10 */
5795*0Sstevel@tonic-gate 		if ((ulong_t)uid > (ulong_t)07777777)
5796*0Sstevel@tonic-gate 			uid = UID_NOBODY;
5797*0Sstevel@tonic-gate 		if ((ulong_t)gid > (ulong_t)07777777)
5798*0Sstevel@tonic-gate 			gid = GID_NOBODY;
5799*0Sstevel@tonic-gate 		break;
5800*0Sstevel@tonic-gate 	default:
5801*0Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
5802*0Sstevel@tonic-gate 	}
5803*0Sstevel@tonic-gate 
5804*0Sstevel@tonic-gate 	/*
5805*0Sstevel@tonic-gate 	 * Since cpio formats -don't- encode the symbolic names, print
5806*0Sstevel@tonic-gate 	 * a warning message when we map the uid or gid this way.
5807*0Sstevel@tonic-gate 	 * Also, if the ownership just changed, clear set[ug]id bits
5808*0Sstevel@tonic-gate 	 *
5809*0Sstevel@tonic-gate 	 * (Except for USTAR format of course, where we have a string
5810*0Sstevel@tonic-gate 	 * representation of the username embedded in the header)
5811*0Sstevel@tonic-gate 	 */
5812*0Sstevel@tonic-gate 	if (uid != G_p->g_uid && Hdr_type != USTAR) {
5813*0Sstevel@tonic-gate 		msg(ERR, warnfmt,
5814*0Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
5815*0Sstevel@tonic-gate 		    G_p->g_nam_p : G_p->g_attrfnam_p,
5816*0Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
5817*0Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
5818*0Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
5819*0Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p,
5820*0Sstevel@tonic-gate 		    gettext("uid too large for archive format"));
5821*0Sstevel@tonic-gate 		if (S_ISREG(mode))
5822*0Sstevel@tonic-gate 			mode &= ~S_ISUID;
5823*0Sstevel@tonic-gate 	}
5824*0Sstevel@tonic-gate 	if (gid != G_p->g_gid && Hdr_type != USTAR) {
5825*0Sstevel@tonic-gate 		msg(ERR, warnfmt,
5826*0Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
5827*0Sstevel@tonic-gate 		    G_p->g_nam_p : G_p->g_attrfnam_p,
5828*0Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
5829*0Sstevel@tonic-gate 		    "" : gettext(" Attribute "),
5830*0Sstevel@tonic-gate 		    (G_p->g_attrnam_p == (char *)NULL) ?
5831*0Sstevel@tonic-gate 		    "" : G_p->g_attrnam_p,
5832*0Sstevel@tonic-gate 		    gettext("gid too large for archive format"));
5833*0Sstevel@tonic-gate 		if (S_ISREG(mode))
5834*0Sstevel@tonic-gate 			mode &= ~S_ISGID;
5835*0Sstevel@tonic-gate 	}
5836*0Sstevel@tonic-gate 
5837*0Sstevel@tonic-gate 	switch (Hdr_type) {
5838*0Sstevel@tonic-gate 	case BIN:
5839*0Sstevel@tonic-gate 	case CHR:
5840*0Sstevel@tonic-gate 	case ASC:
5841*0Sstevel@tonic-gate 	case CRC:
5842*0Sstevel@tonic-gate 		cnt = Hdrsz + G_p->g_namesz;
5843*0Sstevel@tonic-gate 		break;
5844*0Sstevel@tonic-gate 	case TAR:
5845*0Sstevel@tonic-gate 		/*FALLTHROUGH*/
5846*0Sstevel@tonic-gate 	case USTAR: /* TAR and USTAR */
5847*0Sstevel@tonic-gate 		cnt = TARSZ;
5848*0Sstevel@tonic-gate 		break;
5849*0Sstevel@tonic-gate 	default:
5850*0Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
5851*0Sstevel@tonic-gate 	}
5852*0Sstevel@tonic-gate 	FLUSH(cnt);
5853*0Sstevel@tonic-gate 
5854*0Sstevel@tonic-gate 	switch (Hdr_type) {
5855*0Sstevel@tonic-gate 	case BIN:
5856*0Sstevel@tonic-gate 		Hdr.h_magic = (short)G_p->g_magic;
5857*0Sstevel@tonic-gate 		Hdr.h_dev = G_p->g_dev;
5858*0Sstevel@tonic-gate 		Hdr.h_ino = G_p->g_ino;
5859*0Sstevel@tonic-gate 		Hdr.h_uid = uid;
5860*0Sstevel@tonic-gate 		Hdr.h_gid = gid;
5861*0Sstevel@tonic-gate 		Hdr.h_mode = mode;
5862*0Sstevel@tonic-gate 		Hdr.h_nlink = G_p->g_nlink;
5863*0Sstevel@tonic-gate 		Hdr.h_rdev = G_p->g_rdev;
5864*0Sstevel@tonic-gate 		mkshort(Hdr.h_mtime, (long)G_p->g_mtime);
5865*0Sstevel@tonic-gate 		Hdr.h_namesize = (short)G_p->g_namesz;
5866*0Sstevel@tonic-gate 		mkshort(Hdr.h_filesize, (long)len);
5867*0Sstevel@tonic-gate 		(void) strcpy(Hdr.h_name, G_p->g_nam_p);
5868*0Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, &Hdr, cnt);
5869*0Sstevel@tonic-gate 		break;
5870*0Sstevel@tonic-gate 	case CHR:
5871*0Sstevel@tonic-gate 		(void) sprintf(Buffr.b_in_p,
5872*0Sstevel@tonic-gate 		    "%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.11lo%.6lo%."
5873*0Sstevel@tonic-gate 		    "11llo%s", G_p->g_magic, G_p->g_dev, G_p->g_ino, mode,
5874*0Sstevel@tonic-gate 		    uid, gid, G_p->g_nlink, MK_USHORT(G_p->g_rdev),
5875*0Sstevel@tonic-gate 		    G_p->g_mtime, (long)G_p->g_namesz, (offset_t)len,
5876*0Sstevel@tonic-gate 		    G_p->g_nam_p);
5877*0Sstevel@tonic-gate 		break;
5878*0Sstevel@tonic-gate 	case ASC:
5879*0Sstevel@tonic-gate 	case CRC:
5880*0Sstevel@tonic-gate 		(void) sprintf(Buffr.b_in_p,
5881*0Sstevel@tonic-gate 		    "%.6lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%."
5882*0Sstevel@tonic-gate 		    "8lx%.8lx%.8lx%.8lx%s",
5883*0Sstevel@tonic-gate 			G_p->g_magic, G_p->g_ino, mode, G_p->g_uid,
5884*0Sstevel@tonic-gate 			G_p->g_gid, G_p->g_nlink, G_p->g_mtime, (long)len,
5885*0Sstevel@tonic-gate 			major(G_p->g_dev), minor(G_p->g_dev),
5886*0Sstevel@tonic-gate 			major(G_p->g_rdev), minor(G_p->g_rdev),
5887*0Sstevel@tonic-gate 			G_p->g_namesz, G_p->g_cksum, G_p->g_nam_p);
5888*0Sstevel@tonic-gate 		break;
5889*0Sstevel@tonic-gate 	case USTAR:
5890*0Sstevel@tonic-gate 		Thdr_p = (union tblock *)Buffr.b_in_p;
5891*0Sstevel@tonic-gate 		(void) memcpy(Thdr_p, Empty, TARSZ);
5892*0Sstevel@tonic-gate 		(void) strncpy(Thdr_p->tbuf.t_name, G_p->g_tname,
5893*0Sstevel@tonic-gate 		    (int)strlen(G_p->g_tname));
5894*0Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mode, "%07o", (int)mode);
5895*0Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_uid, "%07o", (int)uid);
5896*0Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_gid, "%07o", (int)gid);
5897*0Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_size, "%011llo",
5898*0Sstevel@tonic-gate 		    (offset_t)len);
5899*0Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mtime, "%011lo", G_p->g_mtime);
5900*0Sstevel@tonic-gate 		if (secflag == ARCHIVE_ACL) {
5901*0Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = 'A';	/* ACL file type */
5902*0Sstevel@tonic-gate 		} else if (secflag == ARCHIVE_XATTR ||
5903*0Sstevel@tonic-gate 		    (G_p->g_attrnam_p != (char *)NULL)) {
5904*0Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = _XATTR_HDRTYPE;
5905*0Sstevel@tonic-gate 		} else {
5906*0Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = G_p->g_typeflag;
5907*0Sstevel@tonic-gate 		}
5908*0Sstevel@tonic-gate 		if (T_lname[0] != '\0') {
5909*0Sstevel@tonic-gate 			/*
5910*0Sstevel@tonic-gate 			 * if not a symbolic link
5911*0Sstevel@tonic-gate 			 */
5912*0Sstevel@tonic-gate 			if (((G_p->g_mode & Ftype) != S_IFLNK) &&
5913*0Sstevel@tonic-gate 			    (G_p->g_attrnam_p == (char *)NULL)) {
5914*0Sstevel@tonic-gate 				Thdr_p->tbuf.t_typeflag = LNKTYPE;
5915*0Sstevel@tonic-gate 				(void) sprintf(Thdr_p->tbuf.t_size,
5916*0Sstevel@tonic-gate 				    "%011lo", 0L);
5917*0Sstevel@tonic-gate 			}
5918*0Sstevel@tonic-gate 			(void) strncpy(Thdr_p->tbuf.t_linkname, T_lname,
5919*0Sstevel@tonic-gate 			    strlen(T_lname));
5920*0Sstevel@tonic-gate 		}
5921*0Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_magic, "%s", TMAGIC);
5922*0Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_version, "%2s", TVERSION);
5923*0Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_uname, "%s",  G_p->g_uname);
5924*0Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_gname, "%s", G_p->g_gname);
5925*0Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_devmajor, "%07o",
5926*0Sstevel@tonic-gate 		    (int)major(G_p->g_rdev));
5927*0Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_devminor, "%07o",
5928*0Sstevel@tonic-gate 		    (int)minor(G_p->g_rdev));
5929*0Sstevel@tonic-gate 		if (Gen.g_prefix) {
5930*0Sstevel@tonic-gate 			(void) sprintf(Thdr_p->tbuf.t_prefix, "%s",
5931*0Sstevel@tonic-gate 			    Gen.g_prefix);
5932*0Sstevel@tonic-gate 			free(Gen.g_prefix);
5933*0Sstevel@tonic-gate 			Gen.g_prefix = NULL;
5934*0Sstevel@tonic-gate 		} else {
5935*0Sstevel@tonic-gate 			(void) sprintf(Thdr_p->tbuf.t_prefix, "%s", "");
5936*0Sstevel@tonic-gate 		}
5937*0Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_cksum, "%07o",
5938*0Sstevel@tonic-gate 		    (int)cksum(TARTYP, 0, NULL));
5939*0Sstevel@tonic-gate 		break;
5940*0Sstevel@tonic-gate 	case TAR:
5941*0Sstevel@tonic-gate 		Thdr_p = (union tblock *)Buffr.b_in_p;
5942*0Sstevel@tonic-gate 		(void) memcpy(Thdr_p, Empty, TARSZ);
5943*0Sstevel@tonic-gate 		(void) strncpy(Thdr_p->tbuf.t_name, G_p->g_nam_p,
5944*0Sstevel@tonic-gate 		    G_p->g_namesz);
5945*0Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mode, "%07o ", (int)mode);
5946*0Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_uid, "%07o ", (int)uid);
5947*0Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_gid, "%07o ", (int)gid);
5948*0Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_size, "%011llo ",
5949*0Sstevel@tonic-gate 		    (offset_t)len);
5950*0Sstevel@tonic-gate 		(void) sprintf(Thdr_p->tbuf.t_mtime, "%011o ",
5951*0Sstevel@tonic-gate 		    (int)G_p->g_mtime);
5952*0Sstevel@tonic-gate 		if (T_lname[0] != '\0') {
5953*0Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = '1';
5954*0Sstevel@tonic-gate 		} else {
5955*0Sstevel@tonic-gate 			Thdr_p->tbuf.t_typeflag = '\0';
5956*0Sstevel@tonic-gate 		}
5957*0Sstevel@tonic-gate 		(void) strncpy(Thdr_p->tbuf.t_linkname, T_lname,
5958*0Sstevel@tonic-gate 		    strlen(T_lname));
5959*0Sstevel@tonic-gate 		break;
5960*0Sstevel@tonic-gate 	default:
5961*0Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
5962*0Sstevel@tonic-gate 	} /* Hdr_type */
5963*0Sstevel@tonic-gate 
5964*0Sstevel@tonic-gate 	Buffr.b_in_p += cnt;
5965*0Sstevel@tonic-gate 	Buffr.b_cnt += cnt;
5966*0Sstevel@tonic-gate 	pad = ((cnt + Pad_val) & ~Pad_val) - cnt;
5967*0Sstevel@tonic-gate 	if (pad != 0) {
5968*0Sstevel@tonic-gate 		FLUSH(pad);
5969*0Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, Empty, pad);
5970*0Sstevel@tonic-gate 		Buffr.b_in_p += pad;
5971*0Sstevel@tonic-gate 		Buffr.b_cnt += pad;
5972*0Sstevel@tonic-gate 	}
5973*0Sstevel@tonic-gate }
5974*0Sstevel@tonic-gate 
5975*0Sstevel@tonic-gate /*
5976*0Sstevel@tonic-gate  * write_trail: Create the appropriate trailer for the selected header type
5977*0Sstevel@tonic-gate  * and bwrite the trailer.  Pad the buffer with nulls out to the next Bufsize
5978*0Sstevel@tonic-gate  * boundary, and force a write.  If the write completes, or if the trailer is
5979*0Sstevel@tonic-gate  * completely written (but not all of the padding nulls (as can happen on end
5980*0Sstevel@tonic-gate  * of medium)) return.  Otherwise, the trailer was not completely written out,
5981*0Sstevel@tonic-gate  * so re-pad the buffer with nulls and try again.
5982*0Sstevel@tonic-gate  */
5983*0Sstevel@tonic-gate 
5984*0Sstevel@tonic-gate static void
5985*0Sstevel@tonic-gate write_trail(void)
5986*0Sstevel@tonic-gate {
5987*0Sstevel@tonic-gate 	int cnt, need;
5988*0Sstevel@tonic-gate 
5989*0Sstevel@tonic-gate 	switch (Hdr_type) {
5990*0Sstevel@tonic-gate 	case BIN:
5991*0Sstevel@tonic-gate 		Gen.g_magic = CMN_BIN;
5992*0Sstevel@tonic-gate 		break;
5993*0Sstevel@tonic-gate 	case CHR:
5994*0Sstevel@tonic-gate 		Gen.g_magic = CMN_BIN;
5995*0Sstevel@tonic-gate 		break;
5996*0Sstevel@tonic-gate 	case ASC:
5997*0Sstevel@tonic-gate 		Gen.g_magic = CMN_ASC;
5998*0Sstevel@tonic-gate 		break;
5999*0Sstevel@tonic-gate 	case CRC:
6000*0Sstevel@tonic-gate 		Gen.g_magic = CMN_CRC;
6001*0Sstevel@tonic-gate 		break;
6002*0Sstevel@tonic-gate 	}
6003*0Sstevel@tonic-gate 
6004*0Sstevel@tonic-gate 	switch (Hdr_type) {
6005*0Sstevel@tonic-gate 	case BIN:
6006*0Sstevel@tonic-gate 	case CHR:
6007*0Sstevel@tonic-gate 	case ASC:
6008*0Sstevel@tonic-gate 	case CRC:
6009*0Sstevel@tonic-gate 		Gen.g_mode = Gen.g_uid = Gen.g_gid = 0;
6010*0Sstevel@tonic-gate 		Gen.g_nlink = 1;
6011*0Sstevel@tonic-gate 		Gen.g_mtime = Gen.g_ino = Gen.g_dev = 0;
6012*0Sstevel@tonic-gate 		Gen.g_rdev = Gen.g_cksum = 0;
6013*0Sstevel@tonic-gate 		Gen.g_filesz = (off_t)0;
6014*0Sstevel@tonic-gate 		Gen.g_namesz = strlen("TRAILER!!!") + 1;
6015*0Sstevel@tonic-gate 		(void) strcpy(Gen.g_nam_p, "TRAILER!!!");
6016*0Sstevel@tonic-gate 		G_p = &Gen;
6017*0Sstevel@tonic-gate 		write_hdr(ARCHIVE_NORMAL, (off_t)0);
6018*0Sstevel@tonic-gate 		break;
6019*0Sstevel@tonic-gate 	case TAR:
6020*0Sstevel@tonic-gate 	/*FALLTHROUGH*/
6021*0Sstevel@tonic-gate 	case USTAR: /* TAR and USTAR */
6022*0Sstevel@tonic-gate 		for (cnt = 0; cnt < 3; cnt++) {
6023*0Sstevel@tonic-gate 			FLUSH(TARSZ);
6024*0Sstevel@tonic-gate 			(void) memcpy(Buffr.b_in_p, Empty, TARSZ);
6025*0Sstevel@tonic-gate 			Buffr.b_in_p += TARSZ;
6026*0Sstevel@tonic-gate 			Buffr.b_cnt += TARSZ;
6027*0Sstevel@tonic-gate 		}
6028*0Sstevel@tonic-gate 		break;
6029*0Sstevel@tonic-gate 	default:
6030*0Sstevel@tonic-gate 		msg(EXT, "Impossible header type.");
6031*0Sstevel@tonic-gate 	}
6032*0Sstevel@tonic-gate 	need = Bufsize - (Buffr.b_cnt % Bufsize);
6033*0Sstevel@tonic-gate 	if (need == Bufsize)
6034*0Sstevel@tonic-gate 		need = 0;
6035*0Sstevel@tonic-gate 
6036*0Sstevel@tonic-gate 	while (Buffr.b_cnt > 0) {
6037*0Sstevel@tonic-gate 		while (need > 0) {
6038*0Sstevel@tonic-gate 			cnt = (need < TARSZ) ? need : TARSZ;
6039*0Sstevel@tonic-gate 			need -= cnt;
6040*0Sstevel@tonic-gate 			FLUSH(cnt);
6041*0Sstevel@tonic-gate 			(void) memcpy(Buffr.b_in_p, Empty, cnt);
6042*0Sstevel@tonic-gate 			Buffr.b_in_p += cnt;
6043*0Sstevel@tonic-gate 			Buffr.b_cnt += cnt;
6044*0Sstevel@tonic-gate 		}
6045*0Sstevel@tonic-gate 		bflush();
6046*0Sstevel@tonic-gate 	}
6047*0Sstevel@tonic-gate }
6048*0Sstevel@tonic-gate 
6049*0Sstevel@tonic-gate /*
6050*0Sstevel@tonic-gate  * if archives in USTAR format, check if typeflag == '5' for directories
6051*0Sstevel@tonic-gate  */
6052*0Sstevel@tonic-gate static int
6053*0Sstevel@tonic-gate ustar_dir(void)
6054*0Sstevel@tonic-gate {
6055*0Sstevel@tonic-gate 	if (Hdr_type == USTAR || Hdr_type == TAR) {
6056*0Sstevel@tonic-gate 		if (Thdr_p->tbuf.t_typeflag == '5')
6057*0Sstevel@tonic-gate 			return (1);
6058*0Sstevel@tonic-gate 	}
6059*0Sstevel@tonic-gate 	return (0);
6060*0Sstevel@tonic-gate }
6061*0Sstevel@tonic-gate 
6062*0Sstevel@tonic-gate /*
6063*0Sstevel@tonic-gate  * if archives in USTAR format, check if typeflag == '3' || '4' || '6'
6064*0Sstevel@tonic-gate  * for character, block, fifo special files
6065*0Sstevel@tonic-gate  */
6066*0Sstevel@tonic-gate static int
6067*0Sstevel@tonic-gate ustar_spec(void)
6068*0Sstevel@tonic-gate {
6069*0Sstevel@tonic-gate 	int typeflag;
6070*0Sstevel@tonic-gate 
6071*0Sstevel@tonic-gate 	if (Hdr_type == USTAR || Hdr_type == TAR) {
6072*0Sstevel@tonic-gate 		typeflag = Thdr_p->tbuf.t_typeflag;
6073*0Sstevel@tonic-gate 		if (typeflag == '3' || typeflag == '4' || typeflag == '6')
6074*0Sstevel@tonic-gate 			return (1);
6075*0Sstevel@tonic-gate 	}
6076*0Sstevel@tonic-gate 	return (0);
6077*0Sstevel@tonic-gate }
6078*0Sstevel@tonic-gate 
6079*0Sstevel@tonic-gate /*
6080*0Sstevel@tonic-gate  * The return value is a pointer to a converted copy of the information in
6081*0Sstevel@tonic-gate  * FromStat if the file is representable in -Hodc format, and NULL otherwise.
6082*0Sstevel@tonic-gate  */
6083*0Sstevel@tonic-gate 
6084*0Sstevel@tonic-gate static struct stat *
6085*0Sstevel@tonic-gate convert_to_old_stat(struct stat *FromStat, char *namep, char *attrp)
6086*0Sstevel@tonic-gate {
6087*0Sstevel@tonic-gate 	static struct stat ToSt;
6088*0Sstevel@tonic-gate 	cpioinfo_t TmpSt;
6089*0Sstevel@tonic-gate 
6090*0Sstevel@tonic-gate 	(void) memset(&TmpSt, 0, sizeof (cpioinfo_t));
6091*0Sstevel@tonic-gate 	stat_to_svr32_stat(&TmpSt, FromStat);
6092*0Sstevel@tonic-gate 	(void) memset(&ToSt, 0, sizeof (ToSt));
6093*0Sstevel@tonic-gate 
6094*0Sstevel@tonic-gate 	if (TmpSt.st_rdev == (o_dev_t)NODEV &&
6095*0Sstevel@tonic-gate 	    (((TmpSt.st_mode & Ftype) == S_IFCHR) ||
6096*0Sstevel@tonic-gate 	    ((TmpSt.st_mode & Ftype) == S_IFBLK))) {
6097*0Sstevel@tonic-gate 		/*
6098*0Sstevel@tonic-gate 		 * Encountered a problem representing the rdev information.
6099*0Sstevel@tonic-gate 		 * Don't archive it.
6100*0Sstevel@tonic-gate 		 */
6101*0Sstevel@tonic-gate 
6102*0Sstevel@tonic-gate 		msg(ERR, "Error -Hodc format can't support expanded"
6103*0Sstevel@tonic-gate 		    "types on %s%s%s",
6104*0Sstevel@tonic-gate 		    namep,
6105*0Sstevel@tonic-gate 		    (attrp == NULL) ? "" : gettext(" Attribute"),
6106*0Sstevel@tonic-gate 		    (attrp == NULL) ? "" : attrp);
6107*0Sstevel@tonic-gate 		return (NULL);
6108*0Sstevel@tonic-gate 	}
6109*0Sstevel@tonic-gate 
6110*0Sstevel@tonic-gate 	if (TmpSt.st_dev == (o_dev_t)NODEV) {
6111*0Sstevel@tonic-gate 		/*
6112*0Sstevel@tonic-gate 		 * Having trouble representing the device/inode pair.  We can't
6113*0Sstevel@tonic-gate 		 * track links in this case; break them all into separate
6114*0Sstevel@tonic-gate 		 * files.
6115*0Sstevel@tonic-gate 		 */
6116*0Sstevel@tonic-gate 
6117*0Sstevel@tonic-gate 		TmpSt.st_ino = 0;
6118*0Sstevel@tonic-gate 
6119*0Sstevel@tonic-gate 		if (((TmpSt.st_mode & Ftype) != S_IFDIR) &&
6120*0Sstevel@tonic-gate 		    TmpSt.st_nlink > 1)
6121*0Sstevel@tonic-gate 			msg(POST,
6122*0Sstevel@tonic-gate 			    "Warning: file %s%s%s has large "
6123*0Sstevel@tonic-gate 			    "device number - linked "
6124*0Sstevel@tonic-gate 			    "files will be restored as "
6125*0Sstevel@tonic-gate 			    "separate files",
6126*0Sstevel@tonic-gate 			    namep,
6127*0Sstevel@tonic-gate 			    (attrp == NULL) ? "" : gettext(" Attribute"),
6128*0Sstevel@tonic-gate 			    (attrp == NULL) ? "" : attrp);
6129*0Sstevel@tonic-gate 
6130*0Sstevel@tonic-gate 		/* ensure no links */
6131*0Sstevel@tonic-gate 
6132*0Sstevel@tonic-gate 		TmpSt.st_nlink = 1;
6133*0Sstevel@tonic-gate 	}
6134*0Sstevel@tonic-gate 
6135*0Sstevel@tonic-gate 	/* Start converting values */
6136*0Sstevel@tonic-gate 
6137*0Sstevel@tonic-gate 	if (TmpSt.st_dev < 0) {
6138*0Sstevel@tonic-gate 		ToSt.st_dev = 0;
6139*0Sstevel@tonic-gate 	} else {
6140*0Sstevel@tonic-gate 		ToSt.st_dev = (dev_t)TmpSt.st_dev;
6141*0Sstevel@tonic-gate 	}
6142*0Sstevel@tonic-gate 
6143*0Sstevel@tonic-gate 	/* -actual- not truncated uid */
6144*0Sstevel@tonic-gate 
6145*0Sstevel@tonic-gate 	ToSt.st_uid = TmpSt.st_uid;
6146*0Sstevel@tonic-gate 
6147*0Sstevel@tonic-gate 	/* -actual- not truncated gid */
6148*0Sstevel@tonic-gate 
6149*0Sstevel@tonic-gate 	ToSt.st_gid = TmpSt.st_gid;
6150*0Sstevel@tonic-gate 	ToSt.st_ino = (ino_t)TmpSt.st_ino;
6151*0Sstevel@tonic-gate 	ToSt.st_mode = (mode_t)TmpSt.st_mode;
6152*0Sstevel@tonic-gate 	ToSt.st_mtime = (ulong_t)TmpSt.st_modtime;
6153*0Sstevel@tonic-gate 	ToSt.st_nlink = (nlink_t)TmpSt.st_nlink;
6154*0Sstevel@tonic-gate 	ToSt.st_size = (off_t)TmpSt.st_size;
6155*0Sstevel@tonic-gate 	ToSt.st_rdev = (dev_t)TmpSt.st_rdev;
6156*0Sstevel@tonic-gate 
6157*0Sstevel@tonic-gate 	return (&ToSt);
6158*0Sstevel@tonic-gate }
6159*0Sstevel@tonic-gate 
6160*0Sstevel@tonic-gate /*
6161*0Sstevel@tonic-gate  * In the beginning of each bar archive, there is a header which describes the
6162*0Sstevel@tonic-gate  * current volume being created, followed by a header which describes the
6163*0Sstevel@tonic-gate  * current file being created, followed by the file itself.  If there is
6164*0Sstevel@tonic-gate  * more than one file to be created, a separate header will be created for
6165*0Sstevel@tonic-gate  * each additional file.  This structure may be repeated if the bar archive
6166*0Sstevel@tonic-gate  * contains multiple volumes.  If a file spans across volumes, its header
6167*0Sstevel@tonic-gate  * will not be repeated in the next volume.
6168*0Sstevel@tonic-gate  *               +------------------+
6169*0Sstevel@tonic-gate  *               |    vol header    |
6170*0Sstevel@tonic-gate  *               |------------------|
6171*0Sstevel@tonic-gate  *               |   file header i  |     i = 0
6172*0Sstevel@tonic-gate  *               |------------------|
6173*0Sstevel@tonic-gate  *               |     <file i>     |
6174*0Sstevel@tonic-gate  *               |------------------|
6175*0Sstevel@tonic-gate  *               |  file header i+1 |
6176*0Sstevel@tonic-gate  *               |------------------|
6177*0Sstevel@tonic-gate  *               |    <file i+1>    |
6178*0Sstevel@tonic-gate  *               |------------------|
6179*0Sstevel@tonic-gate  *               |        .         |
6180*0Sstevel@tonic-gate  *               |        .         |
6181*0Sstevel@tonic-gate  *               |        .         |
6182*0Sstevel@tonic-gate  *               +------------------+
6183*0Sstevel@tonic-gate  */
6184*0Sstevel@tonic-gate 
6185*0Sstevel@tonic-gate /*
6186*0Sstevel@tonic-gate  * read in the header that describes the current volume of the bar archive
6187*0Sstevel@tonic-gate  * to be extracted.
6188*0Sstevel@tonic-gate  */
6189*0Sstevel@tonic-gate static void
6190*0Sstevel@tonic-gate read_bar_vol_hdr(void)
6191*0Sstevel@tonic-gate {
6192*0Sstevel@tonic-gate 	union b_block *tmp_hdr;
6193*0Sstevel@tonic-gate 
6194*0Sstevel@tonic-gate 	tmp_hdr = (union b_block *)Buffr.b_out_p;
6195*0Sstevel@tonic-gate 	if (tmp_hdr->dbuf.bar_magic[0] == BAR_VOLUME_MAGIC) {
6196*0Sstevel@tonic-gate 
6197*0Sstevel@tonic-gate 		if (bar_Vhdr == NULL) {
6198*0Sstevel@tonic-gate 			bar_Vhdr = e_zalloc(E_EXIT, TBLOCK);
6199*0Sstevel@tonic-gate 		}
6200*0Sstevel@tonic-gate 		(void) memcpy(&(bar_Vhdr->dbuf), &(tmp_hdr->dbuf), TBLOCK);
6201*0Sstevel@tonic-gate 	} else {
6202*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
6203*0Sstevel@tonic-gate 		    "bar error: cannot read volume header\n"));
6204*0Sstevel@tonic-gate 		exit(1);
6205*0Sstevel@tonic-gate 	}
6206*0Sstevel@tonic-gate 
6207*0Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.mode, "%8lo", &Gen_bar_vol.g_mode);
6208*0Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.uid, "%8d", (int *)&Gen_bar_vol.g_uid);
6209*0Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.gid, "%8d", (int *)&Gen_bar_vol.g_gid);
6210*0Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.size, "%12llo",
6211*0Sstevel@tonic-gate 	    (u_off_t *)&Gen_bar_vol.g_filesz);
6212*0Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.mtime, "%12lo", &Gen_bar_vol.g_mtime);
6213*0Sstevel@tonic-gate 	(void) sscanf(bar_Vhdr->dbuf.chksum, "%8lo", &Gen_bar_vol.g_cksum);
6214*0Sstevel@tonic-gate 
6215*0Sstevel@tonic-gate 	/* set the compress flag */
6216*0Sstevel@tonic-gate 	if (bar_Vhdr->dbuf.compressed == '1')
6217*0Sstevel@tonic-gate 		Compressed = 1;
6218*0Sstevel@tonic-gate 	else
6219*0Sstevel@tonic-gate 		Compressed = 0;
6220*0Sstevel@tonic-gate 
6221*0Sstevel@tonic-gate 	Buffr.b_out_p += 512;
6222*0Sstevel@tonic-gate 	Buffr.b_cnt -= 512;
6223*0Sstevel@tonic-gate 
6224*0Sstevel@tonic-gate 	/*
6225*0Sstevel@tonic-gate 	 * not the first volume; exit
6226*0Sstevel@tonic-gate 	 */
6227*0Sstevel@tonic-gate 	if (strcmp(bar_Vhdr->dbuf.volume_num, "1") != 0) {
6228*0Sstevel@tonic-gate 		(void) fprintf(stderr,
6229*0Sstevel@tonic-gate 		    gettext("error: This is not volume 1.  "));
6230*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("This is volume %s.  "),
6231*0Sstevel@tonic-gate 		    bar_Vhdr->dbuf.volume_num);
6232*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Please insert volume 1.\n"));
6233*0Sstevel@tonic-gate 		exit(1);
6234*0Sstevel@tonic-gate 	}
6235*0Sstevel@tonic-gate 
6236*0Sstevel@tonic-gate 	read_bar_file_hdr();
6237*0Sstevel@tonic-gate }
6238*0Sstevel@tonic-gate 
6239*0Sstevel@tonic-gate /*
6240*0Sstevel@tonic-gate  * read in the header that describes the current file to be extracted
6241*0Sstevel@tonic-gate  */
6242*0Sstevel@tonic-gate static void
6243*0Sstevel@tonic-gate read_bar_file_hdr(void)
6244*0Sstevel@tonic-gate {
6245*0Sstevel@tonic-gate 	union b_block *tmp_hdr;
6246*0Sstevel@tonic-gate 	char *start_of_name, *name_p;
6247*0Sstevel@tonic-gate 	char *tmp;
6248*0Sstevel@tonic-gate 
6249*0Sstevel@tonic-gate 	if (*Buffr.b_out_p == '\0') {
6250*0Sstevel@tonic-gate 		*Gen.g_nam_p = '\0';
6251*0Sstevel@tonic-gate 		exit(0);
6252*0Sstevel@tonic-gate 	}
6253*0Sstevel@tonic-gate 
6254*0Sstevel@tonic-gate 	tmp_hdr = (union b_block *)Buffr.b_out_p;
6255*0Sstevel@tonic-gate 
6256*0Sstevel@tonic-gate 	tmp = &tmp_hdr->dbuf.mode[1];
6257*0Sstevel@tonic-gate 	(void) sscanf(tmp, "%8lo", &Gen.g_mode);
6258*0Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.uid, "%8lo", &Gen.g_uid);
6259*0Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.gid, "%8lo", &Gen.g_gid);
6260*0Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.size, "%12llo",
6261*0Sstevel@tonic-gate 	    (u_off_t *)&Gen.g_filesz);
6262*0Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.mtime, "%12lo", &Gen.g_mtime);
6263*0Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.chksum, "%8lo", &Gen.g_cksum);
6264*0Sstevel@tonic-gate 	(void) sscanf(tmp_hdr->dbuf.rdev, "%8lo", &Gen.g_rdev);
6265*0Sstevel@tonic-gate 
6266*0Sstevel@tonic-gate #define	to_new_major(x)	(int)((unsigned)((x) & OMAXMAJ) << NBITSMINOR)
6267*0Sstevel@tonic-gate #define	to_new_minor(x)	(int)((x) & OMAXMIN)
6268*0Sstevel@tonic-gate 	Gen.g_rdev = to_new_major(Gen.g_rdev) | to_new_minor(Gen.g_rdev);
6269*0Sstevel@tonic-gate 	bar_linkflag = tmp_hdr->dbuf.linkflag;
6270*0Sstevel@tonic-gate 	start_of_name = &tmp_hdr->dbuf.start_of_name;
6271*0Sstevel@tonic-gate 
6272*0Sstevel@tonic-gate 
6273*0Sstevel@tonic-gate 	name_p = Gen.g_nam_p;
6274*0Sstevel@tonic-gate 	while (*name_p++ = *start_of_name++)
6275*0Sstevel@tonic-gate 		;
6276*0Sstevel@tonic-gate 	*name_p = '\0';
6277*0Sstevel@tonic-gate 	if (bar_linkflag == LNKTYPE || bar_linkflag == SYMTYPE)
6278*0Sstevel@tonic-gate 		(void) strcpy(bar_linkname, start_of_name);
6279*0Sstevel@tonic-gate 
6280*0Sstevel@tonic-gate 	Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
6281*0Sstevel@tonic-gate 	(void) strcpy(nambuf, Gen.g_nam_p);
6282*0Sstevel@tonic-gate }
6283*0Sstevel@tonic-gate 
6284*0Sstevel@tonic-gate /*
6285*0Sstevel@tonic-gate  * if the bar archive is compressed, set up a pipe and do the de-compression
6286*0Sstevel@tonic-gate  * as the compressed file is read in.
6287*0Sstevel@tonic-gate  */
6288*0Sstevel@tonic-gate static void
6289*0Sstevel@tonic-gate setup_uncompress(FILE **pipef)
6290*0Sstevel@tonic-gate {
6291*0Sstevel@tonic-gate 	char *cmd_buf;
6292*0Sstevel@tonic-gate 	size_t cmdlen;
6293*0Sstevel@tonic-gate 
6294*0Sstevel@tonic-gate 	cmd_buf = e_zalloc(E_EXIT, MAXPATHLEN * 2);
6295*0Sstevel@tonic-gate 
6296*0Sstevel@tonic-gate 	if (access(Gen.g_nam_p, W_OK) != 0) {
6297*0Sstevel@tonic-gate 		cmdlen = snprintf(cmd_buf, MAXPATHLEN * 2,
6298*0Sstevel@tonic-gate 		    "chmod +w '%s'; uncompress -c > '%s'; "
6299*0Sstevel@tonic-gate 		    "chmod 0%o '%s'",
6300*0Sstevel@tonic-gate 		    Gen.g_nam_p, Gen.g_nam_p, (int)G_p->g_mode, Gen.g_nam_p);
6301*0Sstevel@tonic-gate 	} else {
6302*0Sstevel@tonic-gate 		cmdlen = snprintf(cmd_buf, MAXPATHLEN * 2,
6303*0Sstevel@tonic-gate 		    "uncompress -c > '%s'", Gen.g_nam_p);
6304*0Sstevel@tonic-gate 	}
6305*0Sstevel@tonic-gate 
6306*0Sstevel@tonic-gate 	if (cmdlen >= MAXPATHLEN * 2 ||
6307*0Sstevel@tonic-gate 	    (*pipef = popen(cmd_buf, "w")) == NULL) {
6308*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("error\n"));
6309*0Sstevel@tonic-gate 		exit(1);
6310*0Sstevel@tonic-gate 	}
6311*0Sstevel@tonic-gate 
6312*0Sstevel@tonic-gate 	if (close(Ofile) != 0)
6313*0Sstevel@tonic-gate 		msg(EXTN, "close error");
6314*0Sstevel@tonic-gate 	Ofile = fileno(*pipef);
6315*0Sstevel@tonic-gate 
6316*0Sstevel@tonic-gate 	free(cmd_buf);
6317*0Sstevel@tonic-gate }
6318*0Sstevel@tonic-gate 
6319*0Sstevel@tonic-gate /*
6320*0Sstevel@tonic-gate  * if the bar archive spans multiple volumes, read in the header that
6321*0Sstevel@tonic-gate  * describes the next volume.
6322*0Sstevel@tonic-gate  */
6323*0Sstevel@tonic-gate static void
6324*0Sstevel@tonic-gate skip_bar_volhdr(void)
6325*0Sstevel@tonic-gate {
6326*0Sstevel@tonic-gate 	char *buff;
6327*0Sstevel@tonic-gate 	union b_block *tmp_hdr;
6328*0Sstevel@tonic-gate 
6329*0Sstevel@tonic-gate 	buff = e_zalloc(E_EXIT, (uint_t)Bufsize);
6330*0Sstevel@tonic-gate 
6331*0Sstevel@tonic-gate 	if (g_read(Device, Archive, buff, Bufsize) < 0) {
6332*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
6333*0Sstevel@tonic-gate 		    "error in skip_bar_volhdr\n"));
6334*0Sstevel@tonic-gate 	} else {
6335*0Sstevel@tonic-gate 
6336*0Sstevel@tonic-gate 		tmp_hdr = (union b_block *)buff;
6337*0Sstevel@tonic-gate 		if (tmp_hdr->dbuf.bar_magic[0] == BAR_VOLUME_MAGIC) {
6338*0Sstevel@tonic-gate 
6339*0Sstevel@tonic-gate 			if (bar_Vhdr == NULL) {
6340*0Sstevel@tonic-gate 				bar_Vhdr = e_zalloc(E_EXIT, TBLOCK);
6341*0Sstevel@tonic-gate 			}
6342*0Sstevel@tonic-gate 			(void) memcpy(&(bar_Vhdr->dbuf),
6343*0Sstevel@tonic-gate 			    &(tmp_hdr->dbuf), TBLOCK);
6344*0Sstevel@tonic-gate 		} else {
6345*0Sstevel@tonic-gate 			(void) fprintf(stderr,
6346*0Sstevel@tonic-gate 			    gettext("cpio error: cannot read bar volume "
6347*0Sstevel@tonic-gate 			    "header\n"));
6348*0Sstevel@tonic-gate 			exit(1);
6349*0Sstevel@tonic-gate 		}
6350*0Sstevel@tonic-gate 
6351*0Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.mode, "%8lo",
6352*0Sstevel@tonic-gate 		    &Gen_bar_vol.g_mode);
6353*0Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.uid, "%8lo",
6354*0Sstevel@tonic-gate 		    &Gen_bar_vol.g_uid);
6355*0Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.gid, "%8lo",
6356*0Sstevel@tonic-gate 		    &Gen_bar_vol.g_gid);
6357*0Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.size, "%12llo",
6358*0Sstevel@tonic-gate 		    (u_off_t *)&Gen_bar_vol.g_filesz);
6359*0Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.mtime, "%12lo",
6360*0Sstevel@tonic-gate 		    &Gen_bar_vol.g_mtime);
6361*0Sstevel@tonic-gate 		(void) sscanf(bar_Vhdr->dbuf.chksum, "%8lo",
6362*0Sstevel@tonic-gate 		    &Gen_bar_vol.g_cksum);
6363*0Sstevel@tonic-gate 		if (bar_Vhdr->dbuf.compressed == '1')
6364*0Sstevel@tonic-gate 			Compressed = 1;
6365*0Sstevel@tonic-gate 		else
6366*0Sstevel@tonic-gate 			Compressed = 0;
6367*0Sstevel@tonic-gate 	}
6368*0Sstevel@tonic-gate 
6369*0Sstevel@tonic-gate 	/*
6370*0Sstevel@tonic-gate 	 * Now put the rest of the bytes read in into the data buffer.
6371*0Sstevel@tonic-gate 	 */
6372*0Sstevel@tonic-gate 	(void) memcpy(Buffr.b_in_p, &buff[512], (Bufsize - 512));
6373*0Sstevel@tonic-gate 	Buffr.b_in_p += (Bufsize - 512);
6374*0Sstevel@tonic-gate 	Buffr.b_cnt += (long)(Bufsize - 512);
6375*0Sstevel@tonic-gate 
6376*0Sstevel@tonic-gate 	free(buff);
6377*0Sstevel@tonic-gate }
6378*0Sstevel@tonic-gate 
6379*0Sstevel@tonic-gate /*
6380*0Sstevel@tonic-gate  * check the linkflag which indicates the type of the file to be extracted,
6381*0Sstevel@tonic-gate  * invoke the corresponding routine to extract the file.
6382*0Sstevel@tonic-gate  */
6383*0Sstevel@tonic-gate static void
6384*0Sstevel@tonic-gate bar_file_in(void)
6385*0Sstevel@tonic-gate {
6386*0Sstevel@tonic-gate 	/*
6387*0Sstevel@tonic-gate 	 * the file is a directory
6388*0Sstevel@tonic-gate 	 */
6389*0Sstevel@tonic-gate 	if (Adir) {
6390*0Sstevel@tonic-gate 		if (ckname(1) != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
6391*0Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
6392*0Sstevel@tonic-gate 		}
6393*0Sstevel@tonic-gate 		return;
6394*0Sstevel@tonic-gate 	}
6395*0Sstevel@tonic-gate 
6396*0Sstevel@tonic-gate 	switch (bar_linkflag) {
6397*0Sstevel@tonic-gate 	case REGTYPE:
6398*0Sstevel@tonic-gate 		/* regular file */
6399*0Sstevel@tonic-gate 		if ((ckname(1) == F_SKIP) ||
6400*0Sstevel@tonic-gate 		    (Ofile = openout(G_p->g_dirfd)) < 0) {
6401*0Sstevel@tonic-gate 			data_in(P_SKIP);
6402*0Sstevel@tonic-gate 		} else {
6403*0Sstevel@tonic-gate 			data_in(P_PROC);
6404*0Sstevel@tonic-gate 		}
6405*0Sstevel@tonic-gate 		break;
6406*0Sstevel@tonic-gate 	case LNKTYPE:
6407*0Sstevel@tonic-gate 		/* hard link */
6408*0Sstevel@tonic-gate 		if (ckname(1) == F_SKIP) {
6409*0Sstevel@tonic-gate 			break;
6410*0Sstevel@tonic-gate 		}
6411*0Sstevel@tonic-gate 		(void) creat_lnk(G_p->g_dirfd, bar_linkname, G_p->g_nam_p);
6412*0Sstevel@tonic-gate 		break;
6413*0Sstevel@tonic-gate 	case SYMTYPE:
6414*0Sstevel@tonic-gate 		/* symbolic link */
6415*0Sstevel@tonic-gate 		if ((ckname(1) == F_SKIP) ||
6416*0Sstevel@tonic-gate 		    (Ofile = openout(G_p->g_dirfd)) < 0) {
6417*0Sstevel@tonic-gate 			data_in(P_SKIP);
6418*0Sstevel@tonic-gate 		} else {
6419*0Sstevel@tonic-gate 			data_in(P_PROC);
6420*0Sstevel@tonic-gate 		}
6421*0Sstevel@tonic-gate 		break;
6422*0Sstevel@tonic-gate 	case CHRTYPE:
6423*0Sstevel@tonic-gate 		/* character device or FIFO */
6424*0Sstevel@tonic-gate 		if (ckname(1) != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
6425*0Sstevel@tonic-gate 			VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
6426*0Sstevel@tonic-gate 		}
6427*0Sstevel@tonic-gate 		break;
6428*0Sstevel@tonic-gate 	default:
6429*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("error: unknown file type\n"));
6430*0Sstevel@tonic-gate 		break;
6431*0Sstevel@tonic-gate 	}
6432*0Sstevel@tonic-gate }
6433*0Sstevel@tonic-gate 
6434*0Sstevel@tonic-gate 
6435*0Sstevel@tonic-gate /*
6436*0Sstevel@tonic-gate  * This originally came from libgenIO/g_init.c
6437*0Sstevel@tonic-gate  * XXX	And it is very broken.
6438*0Sstevel@tonic-gate  */
6439*0Sstevel@tonic-gate 
6440*0Sstevel@tonic-gate /* #include <sys/statvfs.h> */
6441*0Sstevel@tonic-gate #include <ftw.h>
6442*0Sstevel@tonic-gate /* #include <libgenIO.h> */
6443*0Sstevel@tonic-gate #define	G_TM_TAPE	1	/* Tapemaster controller    */
6444*0Sstevel@tonic-gate #define	G_XY_DISK	3	/* xy disks		*/
6445*0Sstevel@tonic-gate #define	G_SD_DISK	7	/* scsi sd disk		*/
6446*0Sstevel@tonic-gate #define	G_XT_TAPE	8	/* xt tapes		*/
6447*0Sstevel@tonic-gate #define	G_SF_FLOPPY	9	/* sf floppy		*/
6448*0Sstevel@tonic-gate #define	G_XD_DISK	10	/* xd disks		*/
6449*0Sstevel@tonic-gate #define	G_ST_TAPE	11	/* scsi tape		*/
6450*0Sstevel@tonic-gate #define	G_NS		12	/* noswap pseudo-dev	*/
6451*0Sstevel@tonic-gate #define	G_RAM		13	/* ram pseudo-dev	*/
6452*0Sstevel@tonic-gate #define	G_FT		14	/* tftp			*/
6453*0Sstevel@tonic-gate #define	G_HD		15	/* 386 network disk	*/
6454*0Sstevel@tonic-gate #define	G_FD		16	/* 386 AT disk		*/
6455*0Sstevel@tonic-gate #define	G_FILE		28	/* file, not a device	*/
6456*0Sstevel@tonic-gate #define	G_NO_DEV	29	/* device does not require special treatment */
6457*0Sstevel@tonic-gate #define	G_DEV_MAX	30	/* last valid device type */
6458*0Sstevel@tonic-gate 
6459*0Sstevel@tonic-gate /*
6460*0Sstevel@tonic-gate  * g_init: Determine the device being accessed, set the buffer size,
6461*0Sstevel@tonic-gate  * and perform any device specific initialization. Since at this point
6462*0Sstevel@tonic-gate  * Sun has no system call to read the configuration, the major numbers
6463*0Sstevel@tonic-gate  * are assumed to be static and types are figured out as such. However,
6464*0Sstevel@tonic-gate  * as a rough estimate, the buffer size for all types is set to 512
6465*0Sstevel@tonic-gate  * as a default.
6466*0Sstevel@tonic-gate  */
6467*0Sstevel@tonic-gate 
6468*0Sstevel@tonic-gate static int
6469*0Sstevel@tonic-gate g_init(int *devtype, int *fdes)
6470*0Sstevel@tonic-gate {
6471*0Sstevel@tonic-gate 	int bufsize;
6472*0Sstevel@tonic-gate 	struct stat st_buf;
6473*0Sstevel@tonic-gate 	struct statvfs stfs_buf;
6474*0Sstevel@tonic-gate 
6475*0Sstevel@tonic-gate 	*devtype = G_NO_DEV;
6476*0Sstevel@tonic-gate 	bufsize = -1;
6477*0Sstevel@tonic-gate 	if (fstat(*fdes, &st_buf) == -1)
6478*0Sstevel@tonic-gate 		return (-1);
6479*0Sstevel@tonic-gate 	if (!(st_buf.st_mode & S_IFCHR) || !(st_buf.st_mode & S_IFBLK)) {
6480*0Sstevel@tonic-gate 		if (st_buf.st_mode & S_IFIFO) {
6481*0Sstevel@tonic-gate 			bufsize = 512;
6482*0Sstevel@tonic-gate 		} else {
6483*0Sstevel@tonic-gate 			/* find block size for this file system */
6484*0Sstevel@tonic-gate 			*devtype = G_FILE;
6485*0Sstevel@tonic-gate 			if (fstatvfs(*fdes, &stfs_buf) < 0) {
6486*0Sstevel@tonic-gate 					bufsize = -1;
6487*0Sstevel@tonic-gate 					errno = ENODEV;
6488*0Sstevel@tonic-gate 			} else
6489*0Sstevel@tonic-gate 				bufsize = stfs_buf.f_bsize;
6490*0Sstevel@tonic-gate 		}
6491*0Sstevel@tonic-gate 
6492*0Sstevel@tonic-gate 		return (bufsize);
6493*0Sstevel@tonic-gate 
6494*0Sstevel@tonic-gate 	/*
6495*0Sstevel@tonic-gate 	 * We'll have to add a remote attribute to stat but this
6496*0Sstevel@tonic-gate 	 * should work for now.
6497*0Sstevel@tonic-gate 	 */
6498*0Sstevel@tonic-gate 	} else if (st_buf.st_dev & 0x8000)	/* if remote  rdev */
6499*0Sstevel@tonic-gate 		return (512);
6500*0Sstevel@tonic-gate 
6501*0Sstevel@tonic-gate 	bufsize = 512;
6502*0Sstevel@tonic-gate 
6503*0Sstevel@tonic-gate 	if (Hdr_type == BAR) {
6504*0Sstevel@tonic-gate 		if (is_tape(*fdes)) {
6505*0Sstevel@tonic-gate 			bufsize = BAR_TAPE_SIZE;
6506*0Sstevel@tonic-gate 			(void) fprintf(stderr, "Archiving to tape");
6507*0Sstevel@tonic-gate 			(void) fprintf(stderr, " blocking factor 126\n");
6508*0Sstevel@tonic-gate 		} else if (is_floppy(*fdes)) {
6509*0Sstevel@tonic-gate 			bufsize = BAR_FLOPPY_SIZE;
6510*0Sstevel@tonic-gate 			(void) fprintf(stderr, "Archiving to floppy");
6511*0Sstevel@tonic-gate 			(void) fprintf(stderr, " blocking factor 18\n");
6512*0Sstevel@tonic-gate 		}
6513*0Sstevel@tonic-gate 	}
6514*0Sstevel@tonic-gate 
6515*0Sstevel@tonic-gate 	return (bufsize);
6516*0Sstevel@tonic-gate }
6517*0Sstevel@tonic-gate 
6518*0Sstevel@tonic-gate /*
6519*0Sstevel@tonic-gate  * This originally came from libgenIO/g_read.c
6520*0Sstevel@tonic-gate  */
6521*0Sstevel@tonic-gate 
6522*0Sstevel@tonic-gate /*
6523*0Sstevel@tonic-gate  * g_read: Read nbytes of data from fdes (of type devtype) and place
6524*0Sstevel@tonic-gate  * data in location pointed to by buf.  In case of end of medium,
6525*0Sstevel@tonic-gate  * translate (where necessary) device specific EOM indications into
6526*0Sstevel@tonic-gate  * the generic EOM indication of rv = -1, errno = ENOSPC.
6527*0Sstevel@tonic-gate  */
6528*0Sstevel@tonic-gate 
6529*0Sstevel@tonic-gate static int
6530*0Sstevel@tonic-gate g_read(int devtype, int fdes, char *buf, unsigned nbytes)
6531*0Sstevel@tonic-gate {
6532*0Sstevel@tonic-gate 	int rv;
6533*0Sstevel@tonic-gate 
6534*0Sstevel@tonic-gate 	if (devtype < 0 || devtype >= G_DEV_MAX) {
6535*0Sstevel@tonic-gate 		errno = ENODEV;
6536*0Sstevel@tonic-gate 		return (-1);
6537*0Sstevel@tonic-gate 	}
6538*0Sstevel@tonic-gate 
6539*0Sstevel@tonic-gate 	rv = read(fdes, buf, nbytes);
6540*0Sstevel@tonic-gate 
6541*0Sstevel@tonic-gate 	/* st devices return 0 when no space left */
6542*0Sstevel@tonic-gate 	if ((rv == 0 && errno == 0 && Hdr_type != BAR) ||
6543*0Sstevel@tonic-gate 	    (rv == -1 && errno == EIO)) {
6544*0Sstevel@tonic-gate 		errno = 0;
6545*0Sstevel@tonic-gate 		rv = 0;
6546*0Sstevel@tonic-gate 	}
6547*0Sstevel@tonic-gate 
6548*0Sstevel@tonic-gate 	return (rv);
6549*0Sstevel@tonic-gate }
6550*0Sstevel@tonic-gate 
6551*0Sstevel@tonic-gate /*
6552*0Sstevel@tonic-gate  * This originally came from libgenIO/g_write.c
6553*0Sstevel@tonic-gate  */
6554*0Sstevel@tonic-gate 
6555*0Sstevel@tonic-gate /*
6556*0Sstevel@tonic-gate  * g_write: Write nbytes of data to fdes (of type devtype) from
6557*0Sstevel@tonic-gate  * the location pointed to by buf.  In case of end of medium,
6558*0Sstevel@tonic-gate  * translate (where necessary) device specific EOM indications into
6559*0Sstevel@tonic-gate  * the generic EOM indication of rv = -1, errno = ENOSPC.
6560*0Sstevel@tonic-gate  */
6561*0Sstevel@tonic-gate 
6562*0Sstevel@tonic-gate static int
6563*0Sstevel@tonic-gate g_write(int devtype, int fdes, char *buf, unsigned nbytes)
6564*0Sstevel@tonic-gate {
6565*0Sstevel@tonic-gate 	int rv;
6566*0Sstevel@tonic-gate 
6567*0Sstevel@tonic-gate 	if (devtype < 0 || devtype >= G_DEV_MAX) {
6568*0Sstevel@tonic-gate 		errno = ENODEV;
6569*0Sstevel@tonic-gate 		return (-1);
6570*0Sstevel@tonic-gate 	}
6571*0Sstevel@tonic-gate 
6572*0Sstevel@tonic-gate 	rv = write(fdes, buf, nbytes);
6573*0Sstevel@tonic-gate 
6574*0Sstevel@tonic-gate 	/* st devices return 0 when no more space left */
6575*0Sstevel@tonic-gate 	if ((rv == 0 && errno == 0) || (rv == -1 && errno == EIO)) {
6576*0Sstevel@tonic-gate 		errno = ENOSPC;
6577*0Sstevel@tonic-gate 		rv = -1;
6578*0Sstevel@tonic-gate 	}
6579*0Sstevel@tonic-gate 
6580*0Sstevel@tonic-gate 	return (rv);
6581*0Sstevel@tonic-gate }
6582*0Sstevel@tonic-gate 
6583*0Sstevel@tonic-gate /*
6584*0Sstevel@tonic-gate  * Test for tape
6585*0Sstevel@tonic-gate  */
6586*0Sstevel@tonic-gate 
6587*0Sstevel@tonic-gate static int
6588*0Sstevel@tonic-gate is_tape(int fd)
6589*0Sstevel@tonic-gate {
6590*0Sstevel@tonic-gate 	struct mtget stuff;
6591*0Sstevel@tonic-gate 
6592*0Sstevel@tonic-gate 	/*
6593*0Sstevel@tonic-gate 	 * try to do a generic tape ioctl, just to see if
6594*0Sstevel@tonic-gate 	 * the thing is in fact a tape drive(er).
6595*0Sstevel@tonic-gate 	 */
6596*0Sstevel@tonic-gate 	if (ioctl(fd, MTIOCGET, &stuff) != -1) {
6597*0Sstevel@tonic-gate 		/* the ioctl succeeded, must have been a tape */
6598*0Sstevel@tonic-gate 		return (1);
6599*0Sstevel@tonic-gate 	}
6600*0Sstevel@tonic-gate 	return (0);
6601*0Sstevel@tonic-gate }
6602*0Sstevel@tonic-gate 
6603*0Sstevel@tonic-gate /*
6604*0Sstevel@tonic-gate  * Test for floppy
6605*0Sstevel@tonic-gate  */
6606*0Sstevel@tonic-gate 
6607*0Sstevel@tonic-gate static int
6608*0Sstevel@tonic-gate is_floppy(int fd)
6609*0Sstevel@tonic-gate {
6610*0Sstevel@tonic-gate 	struct fd_char stuff;
6611*0Sstevel@tonic-gate 
6612*0Sstevel@tonic-gate 	/*
6613*0Sstevel@tonic-gate 	 * try to get the floppy drive characteristics, just to see if
6614*0Sstevel@tonic-gate 	 * the thing is in fact a floppy drive(er).
6615*0Sstevel@tonic-gate 	 */
6616*0Sstevel@tonic-gate 	if (ioctl(fd, FDIOGCHAR, &stuff) != -1) {
6617*0Sstevel@tonic-gate 		/* the ioctl succeeded, must have been a floppy */
6618*0Sstevel@tonic-gate 		return (1);
6619*0Sstevel@tonic-gate 	}
6620*0Sstevel@tonic-gate 
6621*0Sstevel@tonic-gate 	return (0);
6622*0Sstevel@tonic-gate }
6623*0Sstevel@tonic-gate 
6624*0Sstevel@tonic-gate /*
6625*0Sstevel@tonic-gate  * New functions for ACLs and other security attributes
6626*0Sstevel@tonic-gate  */
6627*0Sstevel@tonic-gate 
6628*0Sstevel@tonic-gate /*
6629*0Sstevel@tonic-gate  * The function appends the new security attribute info to the end of
6630*0Sstevel@tonic-gate  * existing secinfo.
6631*0Sstevel@tonic-gate  */
6632*0Sstevel@tonic-gate static int
6633*0Sstevel@tonic-gate append_secattr(
6634*0Sstevel@tonic-gate 	char	**secinfo,	/* existing security info */
6635*0Sstevel@tonic-gate 	int	*secinfo_len,	/* length of existing security info */
6636*0Sstevel@tonic-gate 	int	size,		/* new attribute size: unit depends on type */
6637*0Sstevel@tonic-gate 	char	*attrp,		/* new attribute data pointer */
6638*0Sstevel@tonic-gate 	char	attr_type)	/* new attribute type */
6639*0Sstevel@tonic-gate {
6640*0Sstevel@tonic-gate 	char	*new_secinfo;
6641*0Sstevel@tonic-gate 	char	*attrtext;
6642*0Sstevel@tonic-gate 	size_t	newattrsize;
6643*0Sstevel@tonic-gate 	int	oldsize;
6644*0Sstevel@tonic-gate 
6645*0Sstevel@tonic-gate 	/* no need to add */
6646*0Sstevel@tonic-gate 	if (attrp == (char *)NULL) {
6647*0Sstevel@tonic-gate 		return (0);
6648*0Sstevel@tonic-gate 	}
6649*0Sstevel@tonic-gate 
6650*0Sstevel@tonic-gate 	switch (attr_type) {
6651*0Sstevel@tonic-gate 	case UFSD_ACL:
6652*0Sstevel@tonic-gate 		/* LINTED alignment */
6653*0Sstevel@tonic-gate 		attrtext = acltotext((aclent_t *)attrp, size);
6654*0Sstevel@tonic-gate 		if (attrtext == NULL) {
6655*0Sstevel@tonic-gate 			(void) fprintf(stderr, "acltotext failed\n");
6656*0Sstevel@tonic-gate 			return (-1);
6657*0Sstevel@tonic-gate 		}
6658*0Sstevel@tonic-gate 		/* header: type + size = 8 */
6659*0Sstevel@tonic-gate 		newattrsize = 8 + strlen(attrtext) + 1;
6660*0Sstevel@tonic-gate 		attr = e_zalloc(E_NORMAL, newattrsize);
6661*0Sstevel@tonic-gate 		if (attr == NULL) {
6662*0Sstevel@tonic-gate 			(void) fprintf(stderr, "can't allocate memory\n");
6663*0Sstevel@tonic-gate 			return (-1);
6664*0Sstevel@tonic-gate 		}
6665*0Sstevel@tonic-gate 		attr->attr_type = '1';		/* UFSD_ACL */
6666*0Sstevel@tonic-gate 		/* acl entry count */
6667*0Sstevel@tonic-gate 		(void) sprintf(attr->attr_len, "%06o", size);
6668*0Sstevel@tonic-gate 		(void) strcpy((char *)&attr->attr_info[0], attrtext);
6669*0Sstevel@tonic-gate 		free(attrtext);
6670*0Sstevel@tonic-gate 		break;
6671*0Sstevel@tonic-gate 
6672*0Sstevel@tonic-gate 		/* SunFed's case goes here */
6673*0Sstevel@tonic-gate 
6674*0Sstevel@tonic-gate 	default:
6675*0Sstevel@tonic-gate 		(void) fprintf(stderr, "unrecognized attribute type\n");
6676*0Sstevel@tonic-gate 		return (-1);
6677*0Sstevel@tonic-gate 	}
6678*0Sstevel@tonic-gate 
6679*0Sstevel@tonic-gate 	/* old security info + new attr header(8) + new attr */
6680*0Sstevel@tonic-gate 	oldsize = *secinfo_len;
6681*0Sstevel@tonic-gate 	*secinfo_len += newattrsize;
6682*0Sstevel@tonic-gate 	new_secinfo = e_zalloc(E_NORMAL, (uint_t)*secinfo_len);
6683*0Sstevel@tonic-gate 	if (new_secinfo == NULL) {
6684*0Sstevel@tonic-gate 		(void) fprintf(stderr, "can't allocate memory\n");
6685*0Sstevel@tonic-gate 		*secinfo_len -= newattrsize;
6686*0Sstevel@tonic-gate 		return (-1);
6687*0Sstevel@tonic-gate 	}
6688*0Sstevel@tonic-gate 
6689*0Sstevel@tonic-gate 	(void) memcpy(new_secinfo, *secinfo, oldsize);
6690*0Sstevel@tonic-gate 	(void) memcpy(new_secinfo + oldsize, attr, newattrsize);
6691*0Sstevel@tonic-gate 
6692*0Sstevel@tonic-gate 	free(*secinfo);
6693*0Sstevel@tonic-gate 	*secinfo = new_secinfo;
6694*0Sstevel@tonic-gate 	return (0);
6695*0Sstevel@tonic-gate }
6696*0Sstevel@tonic-gate 
6697*0Sstevel@tonic-gate static void
6698*0Sstevel@tonic-gate write_ancillary(char *secinfo, int len)
6699*0Sstevel@tonic-gate {
6700*0Sstevel@tonic-gate 	long    pad;
6701*0Sstevel@tonic-gate 	long    cnt;
6702*0Sstevel@tonic-gate 
6703*0Sstevel@tonic-gate 	/* Just tranditional permissions or no security attribute info */
6704*0Sstevel@tonic-gate 	if (len == 0) {
6705*0Sstevel@tonic-gate 		return;
6706*0Sstevel@tonic-gate 	}
6707*0Sstevel@tonic-gate 
6708*0Sstevel@tonic-gate 	/* write out security info */
6709*0Sstevel@tonic-gate 	while (len > 0) {
6710*0Sstevel@tonic-gate 		cnt = (unsigned)(len > CPIOBSZ) ? CPIOBSZ : len;
6711*0Sstevel@tonic-gate 		FLUSH(cnt);
6712*0Sstevel@tonic-gate 		errno = 0;
6713*0Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, secinfo, (unsigned)cnt);
6714*0Sstevel@tonic-gate 		Buffr.b_in_p += cnt;
6715*0Sstevel@tonic-gate 		Buffr.b_cnt += (long)cnt;
6716*0Sstevel@tonic-gate 		len -= (long)cnt;
6717*0Sstevel@tonic-gate 	}
6718*0Sstevel@tonic-gate 	pad = (Pad_val + 1 - (cnt & Pad_val)) & Pad_val;
6719*0Sstevel@tonic-gate 	if (pad != 0) {
6720*0Sstevel@tonic-gate 		FLUSH(pad);
6721*0Sstevel@tonic-gate 		(void) memcpy(Buffr.b_in_p, Empty, pad);
6722*0Sstevel@tonic-gate 		Buffr.b_in_p += pad;
6723*0Sstevel@tonic-gate 		Buffr.b_cnt += pad;
6724*0Sstevel@tonic-gate 	}
6725*0Sstevel@tonic-gate }
6726*0Sstevel@tonic-gate 
6727*0Sstevel@tonic-gate static int
6728*0Sstevel@tonic-gate remove_dir(char *path)
6729*0Sstevel@tonic-gate {
6730*0Sstevel@tonic-gate 	DIR		*name;
6731*0Sstevel@tonic-gate 	struct dirent	*direct;
6732*0Sstevel@tonic-gate 	struct stat	sbuf;
6733*0Sstevel@tonic-gate 	char		*path_copy;
6734*0Sstevel@tonic-gate 
6735*0Sstevel@tonic-gate #define	MSG1	"remove_dir() failed to stat(\"%s\") "
6736*0Sstevel@tonic-gate #define	MSG2	"remove_dir() failed to remove_dir(\"%s\") "
6737*0Sstevel@tonic-gate #define	MSG3	"remove_dir() failed to unlink(\"%s\") "
6738*0Sstevel@tonic-gate 
6739*0Sstevel@tonic-gate 	/*
6740*0Sstevel@tonic-gate 	 * Open the directory for reading.
6741*0Sstevel@tonic-gate 	 */
6742*0Sstevel@tonic-gate 	if ((name = opendir(path)) == NULL) {
6743*0Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to opendir(\"%s\") ", path);
6744*0Sstevel@tonic-gate 		return (-1);
6745*0Sstevel@tonic-gate 	}
6746*0Sstevel@tonic-gate 
6747*0Sstevel@tonic-gate 	if (chdir(path) == -1) {
6748*0Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to chdir(\"%s\") ", path);
6749*0Sstevel@tonic-gate 		return (-1);
6750*0Sstevel@tonic-gate 	}
6751*0Sstevel@tonic-gate 
6752*0Sstevel@tonic-gate 	/*
6753*0Sstevel@tonic-gate 	 * Read every directory entry.
6754*0Sstevel@tonic-gate 	 */
6755*0Sstevel@tonic-gate 	while ((direct = readdir(name)) != NULL) {
6756*0Sstevel@tonic-gate 		/*
6757*0Sstevel@tonic-gate 		 * Ignore "." and ".." entries.
6758*0Sstevel@tonic-gate 		 */
6759*0Sstevel@tonic-gate 		if (strcmp(direct->d_name, ".") == 0 ||
6760*0Sstevel@tonic-gate 		    strcmp(direct->d_name, "..") == 0)
6761*0Sstevel@tonic-gate 			continue;
6762*0Sstevel@tonic-gate 
6763*0Sstevel@tonic-gate 			if (lstat(direct->d_name, &sbuf) == -1) {
6764*0Sstevel@tonic-gate 				msg(ERRN, MSG1, direct->d_name);
6765*0Sstevel@tonic-gate 				(void) closedir(name);
6766*0Sstevel@tonic-gate 			return (-1);
6767*0Sstevel@tonic-gate 		}
6768*0Sstevel@tonic-gate 
6769*0Sstevel@tonic-gate 		if (S_ISDIR(sbuf.st_mode)) {
6770*0Sstevel@tonic-gate 			if (remove_dir(direct->d_name) == -1) {
6771*0Sstevel@tonic-gate 				msg(ERRN, MSG2, direct->d_name);
6772*0Sstevel@tonic-gate 				(void) closedir(name);
6773*0Sstevel@tonic-gate 				return (-1);
6774*0Sstevel@tonic-gate 			}
6775*0Sstevel@tonic-gate 		} else {
6776*0Sstevel@tonic-gate 			if (unlink(direct->d_name) == -1) {
6777*0Sstevel@tonic-gate 				msg(ERRN, MSG3, direct->d_name);
6778*0Sstevel@tonic-gate 				(void) closedir(name);
6779*0Sstevel@tonic-gate 				return (-1);
6780*0Sstevel@tonic-gate 			}
6781*0Sstevel@tonic-gate 		}
6782*0Sstevel@tonic-gate 
6783*0Sstevel@tonic-gate 	}
6784*0Sstevel@tonic-gate 
6785*0Sstevel@tonic-gate 	/*
6786*0Sstevel@tonic-gate 	 * Close the directory we just finished reading.
6787*0Sstevel@tonic-gate 	 */
6788*0Sstevel@tonic-gate 	(void) closedir(name);
6789*0Sstevel@tonic-gate 
6790*0Sstevel@tonic-gate 	/*
6791*0Sstevel@tonic-gate 	 * Change directory to the parent directory...
6792*0Sstevel@tonic-gate 	 */
6793*0Sstevel@tonic-gate 	if (chdir("..") == -1) {
6794*0Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to chdir(\"..\") ");
6795*0Sstevel@tonic-gate 		return (-1);
6796*0Sstevel@tonic-gate 	}
6797*0Sstevel@tonic-gate 
6798*0Sstevel@tonic-gate 	/*
6799*0Sstevel@tonic-gate 	 * ...and finally remove the directory; note we have to
6800*0Sstevel@tonic-gate 	 * make a copy since basename is free to modify its input.
6801*0Sstevel@tonic-gate 	 */
6802*0Sstevel@tonic-gate 	path_copy = e_strdup(E_NORMAL, path);
6803*0Sstevel@tonic-gate 	if (path_copy == NULL) {
6804*0Sstevel@tonic-gate 		msg(ERRN, "cannot strdup() the directory pathname ");
6805*0Sstevel@tonic-gate 		return (-1);
6806*0Sstevel@tonic-gate 	}
6807*0Sstevel@tonic-gate 
6808*0Sstevel@tonic-gate 	if (rmdir(basename(path_copy)) == -1) {
6809*0Sstevel@tonic-gate 		free(path_copy);
6810*0Sstevel@tonic-gate 		msg(ERRN, "remove_dir() failed to rmdir(\"%s\") ", path);
6811*0Sstevel@tonic-gate 		return (-1);
6812*0Sstevel@tonic-gate 	}
6813*0Sstevel@tonic-gate 
6814*0Sstevel@tonic-gate 	free(path_copy);
6815*0Sstevel@tonic-gate 	return (0);
6816*0Sstevel@tonic-gate 
6817*0Sstevel@tonic-gate }
6818*0Sstevel@tonic-gate 
6819*0Sstevel@tonic-gate static int
6820*0Sstevel@tonic-gate save_cwd(void)
6821*0Sstevel@tonic-gate {
6822*0Sstevel@tonic-gate 	return (open(".", O_RDONLY));
6823*0Sstevel@tonic-gate }
6824*0Sstevel@tonic-gate 
6825*0Sstevel@tonic-gate static void
6826*0Sstevel@tonic-gate rest_cwd(int cwd)
6827*0Sstevel@tonic-gate {
6828*0Sstevel@tonic-gate 	(void) fchdir(cwd);
6829*0Sstevel@tonic-gate 	(void) close(cwd);
6830*0Sstevel@tonic-gate }
6831*0Sstevel@tonic-gate 
6832*0Sstevel@tonic-gate #if defined(O_XATTR)
6833*0Sstevel@tonic-gate static void
6834*0Sstevel@tonic-gate xattrs_out(int (*func)())
6835*0Sstevel@tonic-gate {
6836*0Sstevel@tonic-gate 	int dirpfd;
6837*0Sstevel@tonic-gate 	int filefd;
6838*0Sstevel@tonic-gate 	DIR *dirp;
6839*0Sstevel@tonic-gate 	struct dirent *dp;
6840*0Sstevel@tonic-gate 	int slen;
6841*0Sstevel@tonic-gate 	char *namep, *savenamep;
6842*0Sstevel@tonic-gate 
6843*0Sstevel@tonic-gate 	if (pathconf(G_p->g_nam_p, _PC_XATTR_EXISTS) != 1) {
6844*0Sstevel@tonic-gate 		return;
6845*0Sstevel@tonic-gate 	}
6846*0Sstevel@tonic-gate 
6847*0Sstevel@tonic-gate 	/*
6848*0Sstevel@tonic-gate 	 * If aclp still exists then free it since it is was set when base
6849*0Sstevel@tonic-gate 	 * file was extracted.
6850*0Sstevel@tonic-gate 	 */
6851*0Sstevel@tonic-gate 	if (aclp != (aclent_t *)NULL) {
6852*0Sstevel@tonic-gate 		free(aclp);
6853*0Sstevel@tonic-gate 		aclcnt = 0;
6854*0Sstevel@tonic-gate 		aclp = NULL;
6855*0Sstevel@tonic-gate 		acl_set = 0;
6856*0Sstevel@tonic-gate 	}
6857*0Sstevel@tonic-gate 
6858*0Sstevel@tonic-gate 	Gen.g_dirfd = attropen(G_p->g_nam_p, ".", O_RDONLY);
6859*0Sstevel@tonic-gate 	if (Gen.g_dirfd == -1) {
6860*0Sstevel@tonic-gate 		msg(ERRN, "Cannot open attribute directory of file \"%s\"",
6861*0Sstevel@tonic-gate 		    G_p->g_nam_p);
6862*0Sstevel@tonic-gate 		return;
6863*0Sstevel@tonic-gate 
6864*0Sstevel@tonic-gate 	}
6865*0Sstevel@tonic-gate 	savenamep = G_p->g_nam_p;
6866*0Sstevel@tonic-gate 
6867*0Sstevel@tonic-gate 	if ((dirpfd = dup(Gen.g_dirfd)) == -1)  {
6868*0Sstevel@tonic-gate 		msg(ERRN, "Cannot dup(2) attribute directory descriptor");
6869*0Sstevel@tonic-gate 		return;
6870*0Sstevel@tonic-gate 	}
6871*0Sstevel@tonic-gate 
6872*0Sstevel@tonic-gate 	if ((dirp = fdopendir(dirpfd)) == (DIR *)NULL) {
6873*0Sstevel@tonic-gate 		msg(ERRN, "Cannot fdopendir(2) directory file descriptor");
6874*0Sstevel@tonic-gate 		return;
6875*0Sstevel@tonic-gate 	}
6876*0Sstevel@tonic-gate 
6877*0Sstevel@tonic-gate 	while ((dp = readdir(dirp)) != (struct dirent *)NULL) {
6878*0Sstevel@tonic-gate 		if (strcmp(dp->d_name, "..") == 0) {
6879*0Sstevel@tonic-gate 			continue;
6880*0Sstevel@tonic-gate 		}
6881*0Sstevel@tonic-gate 
6882*0Sstevel@tonic-gate 		if (strcmp(dp->d_name, ".") == 0) {
6883*0Sstevel@tonic-gate 			Hiddendir = 1;
6884*0Sstevel@tonic-gate 		} else {
6885*0Sstevel@tonic-gate 			Hiddendir = 0;
6886*0Sstevel@tonic-gate 		}
6887*0Sstevel@tonic-gate 
6888*0Sstevel@tonic-gate 		Gen.g_attrnam_p = dp->d_name;
6889*0Sstevel@tonic-gate 
6890*0Sstevel@tonic-gate 		if (STAT(Gen.g_dirfd, Gen.g_nam_p, &SrcSt) == -1) {
6891*0Sstevel@tonic-gate 			msg(ERRN,
6892*0Sstevel@tonic-gate 			    "Could not fstatat(2) attribute \"%s\" of"
6893*0Sstevel@tonic-gate 			    " file \"%s\"", dp->d_name, savenamep);
6894*0Sstevel@tonic-gate 			continue;
6895*0Sstevel@tonic-gate 		}
6896*0Sstevel@tonic-gate 
6897*0Sstevel@tonic-gate 		if (Use_old_stat) {
6898*0Sstevel@tonic-gate 			OldSt = convert_to_old_stat(&SrcSt,
6899*0Sstevel@tonic-gate 			    Gen.g_nam_p, Gen.g_attrnam_p);
6900*0Sstevel@tonic-gate 
6901*0Sstevel@tonic-gate 			if (OldSt == NULL) {
6902*0Sstevel@tonic-gate 				msg(ERRN,
6903*0Sstevel@tonic-gate 				    "Could not convert to old stat format");
6904*0Sstevel@tonic-gate 				continue;
6905*0Sstevel@tonic-gate 			}
6906*0Sstevel@tonic-gate 		}
6907*0Sstevel@tonic-gate 
6908*0Sstevel@tonic-gate 		Gen.g_attrfnam_p = savenamep;
6909*0Sstevel@tonic-gate 
6910*0Sstevel@tonic-gate 		/*
6911*0Sstevel@tonic-gate 		 * Set up dummy header name
6912*0Sstevel@tonic-gate 		 *
6913*0Sstevel@tonic-gate 		 * One piece is written with .hdr, which
6914*0Sstevel@tonic-gate 		 * contains the actual xattr hdr or pathing information
6915*0Sstevel@tonic-gate 		 * then the name is updated to drop the .hdr off
6916*0Sstevel@tonic-gate 		 * and the actual file itself is archived.
6917*0Sstevel@tonic-gate 		 */
6918*0Sstevel@tonic-gate 		slen = strlen(Gen.g_attrnam_p) + strlen(DEVNULL) +
6919*0Sstevel@tonic-gate 		    strlen(XATTRHDR) + 1;
6920*0Sstevel@tonic-gate 		if ((namep = e_zalloc(E_NORMAL, slen)) == (char *)NULL) {
6921*0Sstevel@tonic-gate 			msg(ERRN, "Could not calloc memory for attribute name");
6922*0Sstevel@tonic-gate 			continue;
6923*0Sstevel@tonic-gate 		}
6924*0Sstevel@tonic-gate 		(void) sprintf(namep, "%s/%s%s",
6925*0Sstevel@tonic-gate 		    DEVNULL, Gen.g_attrnam_p, XATTRHDR);
6926*0Sstevel@tonic-gate 		Gen.g_nam_p = namep;
6927*0Sstevel@tonic-gate 
6928*0Sstevel@tonic-gate 		/*
6929*0Sstevel@tonic-gate 		 * Get attribute's ACL info: don't bother allocating space
6930*0Sstevel@tonic-gate 		 * if there are only standard permissions, i.e. ACL count < 4
6931*0Sstevel@tonic-gate 		 */
6932*0Sstevel@tonic-gate 		if (Pflag) {
6933*0Sstevel@tonic-gate 			filefd = openat(Gen.g_dirfd, dp->d_name, O_RDONLY);
6934*0Sstevel@tonic-gate 			if (filefd == -1) {
6935*0Sstevel@tonic-gate 				msg(ERRN,
6936*0Sstevel@tonic-gate 				    "Could not open attribute \"%s\" of"
6937*0Sstevel@tonic-gate 				    " file \"%s\"", dp->d_name, savenamep);
6938*0Sstevel@tonic-gate 				free(namep);
6939*0Sstevel@tonic-gate 				continue;
6940*0Sstevel@tonic-gate 			}
6941*0Sstevel@tonic-gate 			if ((aclcnt = facl(filefd, GETACLCNT,
6942*0Sstevel@tonic-gate 			    0, NULL)) < 0) {
6943*0Sstevel@tonic-gate 				msg(ERRN,
6944*0Sstevel@tonic-gate 				    "Error with acl() on %s",
6945*0Sstevel@tonic-gate 				    Gen.g_nam_p);
6946*0Sstevel@tonic-gate 			}
6947*0Sstevel@tonic-gate 			if (aclcnt > MIN_ACL_ENTRIES) {
6948*0Sstevel@tonic-gate 				aclp = e_zalloc(E_EXIT,
6949*0Sstevel@tonic-gate 					sizeof (aclent_t) * aclcnt);
6950*0Sstevel@tonic-gate 
6951*0Sstevel@tonic-gate 				if (facl(filefd, GETACL, aclcnt, aclp) < 0) {
6952*0Sstevel@tonic-gate 					msg(ERRN,
6953*0Sstevel@tonic-gate 					    "Error with getacl() on %s",
6954*0Sstevel@tonic-gate 					    Gen.g_nam_p);
6955*0Sstevel@tonic-gate 					free(aclp);
6956*0Sstevel@tonic-gate 					aclp = NULL;
6957*0Sstevel@tonic-gate 				}
6958*0Sstevel@tonic-gate 			}
6959*0Sstevel@tonic-gate 			(void) close(filefd);
6960*0Sstevel@tonic-gate 		}
6961*0Sstevel@tonic-gate 		(void) creat_hdr();
6962*0Sstevel@tonic-gate 		(void) (*func)();
6963*0Sstevel@tonic-gate 		if (Gen.g_passdirfd != -1) {
6964*0Sstevel@tonic-gate 			(void) close(Gen.g_passdirfd);
6965*0Sstevel@tonic-gate 			Gen.g_passdirfd = -1;
6966*0Sstevel@tonic-gate 		}
6967*0Sstevel@tonic-gate 		Gen.g_attrnam_p = (char *)NULL;
6968*0Sstevel@tonic-gate 		Gen.g_attrfnam_p = (char *)NULL;
6969*0Sstevel@tonic-gate 		Gen.g_linktoattrfnam_p = (char *)NULL;
6970*0Sstevel@tonic-gate 		Gen.g_linktoattrnam_p = (char *)NULL;
6971*0Sstevel@tonic-gate 		if (aclp != (aclent_t *)NULL) {
6972*0Sstevel@tonic-gate 			free(aclp);
6973*0Sstevel@tonic-gate 			aclcnt = 0;
6974*0Sstevel@tonic-gate 			aclp = NULL;
6975*0Sstevel@tonic-gate 			acl_set = 0;
6976*0Sstevel@tonic-gate 		}
6977*0Sstevel@tonic-gate 		free(namep);
6978*0Sstevel@tonic-gate 	}
6979*0Sstevel@tonic-gate 
6980*0Sstevel@tonic-gate 	(void) closedir(dirp);
6981*0Sstevel@tonic-gate 	(void) close(Gen.g_dirfd);
6982*0Sstevel@tonic-gate 	Gen.g_dirfd = -1;
6983*0Sstevel@tonic-gate }
6984*0Sstevel@tonic-gate #else
6985*0Sstevel@tonic-gate static void
6986*0Sstevel@tonic-gate xattrs_out(int (*func)())
6987*0Sstevel@tonic-gate {
6988*0Sstevel@tonic-gate }
6989*0Sstevel@tonic-gate #endif
6990*0Sstevel@tonic-gate 
6991*0Sstevel@tonic-gate /*
6992*0Sstevel@tonic-gate  * Return the parent directory of a given path.
6993*0Sstevel@tonic-gate  *
6994*0Sstevel@tonic-gate  * Examples:
6995*0Sstevel@tonic-gate  * /usr/tmp return /usr
6996*0Sstevel@tonic-gate  * /usr/tmp/file return /usr/tmp
6997*0Sstevel@tonic-gate  * /  returns .
6998*0Sstevel@tonic-gate  * /usr returns /
6999*0Sstevel@tonic-gate  * file returns .
7000*0Sstevel@tonic-gate  *
7001*0Sstevel@tonic-gate  * dir is assumed to be at least as big as path.
7002*0Sstevel@tonic-gate  */
7003*0Sstevel@tonic-gate static void
7004*0Sstevel@tonic-gate get_parent(char *path, char *dir)
7005*0Sstevel@tonic-gate {
7006*0Sstevel@tonic-gate 	char *s;
7007*0Sstevel@tonic-gate 	char tmpdir[PATH_MAX + 1];
7008*0Sstevel@tonic-gate 
7009*0Sstevel@tonic-gate 	if (strlen(path) > PATH_MAX) {
7010*0Sstevel@tonic-gate 		msg(EXT, "pathname is too long");
7011*0Sstevel@tonic-gate 	}
7012*0Sstevel@tonic-gate 	(void) strcpy(tmpdir, path);
7013*0Sstevel@tonic-gate 	chop_endslashes(tmpdir);
7014*0Sstevel@tonic-gate 
7015*0Sstevel@tonic-gate 	if ((s = strrchr(tmpdir, '/')) == NULL) {
7016*0Sstevel@tonic-gate 		(void) strcpy(dir, ".");
7017*0Sstevel@tonic-gate 	} else {
7018*0Sstevel@tonic-gate 		s = skipslashes(s, tmpdir);
7019*0Sstevel@tonic-gate 		*s = '\0';
7020*0Sstevel@tonic-gate 		if (s == tmpdir)
7021*0Sstevel@tonic-gate 			(void) strcpy(dir, "/");
7022*0Sstevel@tonic-gate 		else
7023*0Sstevel@tonic-gate 			(void) strcpy(dir, tmpdir);
7024*0Sstevel@tonic-gate 	}
7025*0Sstevel@tonic-gate }
7026*0Sstevel@tonic-gate 
7027*0Sstevel@tonic-gate #if defined(O_XATTR)
7028*0Sstevel@tonic-gate #define	ROUNDTOTBLOCK(a)		((a + (TBLOCK -1)) & ~(TBLOCK -1))
7029*0Sstevel@tonic-gate 
7030*0Sstevel@tonic-gate static void
7031*0Sstevel@tonic-gate prepare_xattr_hdr(
7032*0Sstevel@tonic-gate 	char		**attrbuf,
7033*0Sstevel@tonic-gate 	char		*filename,
7034*0Sstevel@tonic-gate 	char		*attrname,
7035*0Sstevel@tonic-gate 	char		typeflag,
7036*0Sstevel@tonic-gate 	struct Lnk	*linkinfo,
7037*0Sstevel@tonic-gate 	int		*rlen)
7038*0Sstevel@tonic-gate {
7039*0Sstevel@tonic-gate 	char			*bufhead;	/* ptr to full buffer */
7040*0Sstevel@tonic-gate 	struct xattr_hdr 	*hptr;		/* ptr to header in bufhead */
7041*0Sstevel@tonic-gate 	struct xattr_buf	*tptr;		/* ptr to pathing pieces */
7042*0Sstevel@tonic-gate 	int			totalen;	/* total buffer length */
7043*0Sstevel@tonic-gate 	int			len;		/* length returned to user */
7044*0Sstevel@tonic-gate 	int			stringlen;	/* length of filename + attr */
7045*0Sstevel@tonic-gate 	int			linkstringlen;	/* ditto in link section */
7046*0Sstevel@tonic-gate 	int			complen;	/* length of pathing section */
7047*0Sstevel@tonic-gate 	int			linklen;	/* length of link section */
7048*0Sstevel@tonic-gate 
7049*0Sstevel@tonic-gate 
7050*0Sstevel@tonic-gate 	/*
7051*0Sstevel@tonic-gate 	 * Release previous buffer if any.
7052*0Sstevel@tonic-gate 	 */
7053*0Sstevel@tonic-gate 
7054*0Sstevel@tonic-gate 	if (*attrbuf != (char *)NULL) {
7055*0Sstevel@tonic-gate 		free(*attrbuf);
7056*0Sstevel@tonic-gate 		*attrbuf = NULL;
7057*0Sstevel@tonic-gate 	}
7058*0Sstevel@tonic-gate 
7059*0Sstevel@tonic-gate 	/*
7060*0Sstevel@tonic-gate 	 * First add in fixed size stuff
7061*0Sstevel@tonic-gate 	 */
7062*0Sstevel@tonic-gate 	len = sizeof (struct xattr_hdr) + sizeof (struct xattr_buf);
7063*0Sstevel@tonic-gate 
7064*0Sstevel@tonic-gate 	/*
7065*0Sstevel@tonic-gate 	 * Add space for two nulls
7066*0Sstevel@tonic-gate 	 */
7067*0Sstevel@tonic-gate 	stringlen = strlen(attrname) + strlen(filename) + 2;
7068*0Sstevel@tonic-gate 	complen = stringlen + sizeof (struct xattr_buf);
7069*0Sstevel@tonic-gate 
7070*0Sstevel@tonic-gate 	len += stringlen;
7071*0Sstevel@tonic-gate 
7072*0Sstevel@tonic-gate 	/*
7073*0Sstevel@tonic-gate 	 * Now add on space for link info if any
7074*0Sstevel@tonic-gate 	 */
7075*0Sstevel@tonic-gate 
7076*0Sstevel@tonic-gate 	if (linkinfo != NULL) {
7077*0Sstevel@tonic-gate 		/*
7078*0Sstevel@tonic-gate 		 * Again add space for two nulls
7079*0Sstevel@tonic-gate 		 */
7080*0Sstevel@tonic-gate 		linkstringlen = strlen(linkinfo->L_gen.g_attrfnam_p) +
7081*0Sstevel@tonic-gate 		    strlen(linkinfo->L_gen.g_attrnam_p) + 2;
7082*0Sstevel@tonic-gate 		len += linkstringlen;
7083*0Sstevel@tonic-gate 	}
7084*0Sstevel@tonic-gate 
7085*0Sstevel@tonic-gate 	/*
7086*0Sstevel@tonic-gate 	 * Now add padding to end to fill out TBLOCK
7087*0Sstevel@tonic-gate 	 *
7088*0Sstevel@tonic-gate 	 * Function returns size of real data and not size + padding.
7089*0Sstevel@tonic-gate 	 */
7090*0Sstevel@tonic-gate 
7091*0Sstevel@tonic-gate 	totalen = ROUNDTOTBLOCK(len);
7092*0Sstevel@tonic-gate 	bufhead = e_zalloc(E_EXIT, totalen);
7093*0Sstevel@tonic-gate 
7094*0Sstevel@tonic-gate 	/*
7095*0Sstevel@tonic-gate 	 * Now we can fill in the necessary pieces
7096*0Sstevel@tonic-gate 	 */
7097*0Sstevel@tonic-gate 
7098*0Sstevel@tonic-gate 	if (linkinfo != (struct Lnk *)NULL) {
7099*0Sstevel@tonic-gate 		linklen = linkstringlen + (sizeof (struct xattr_buf));
7100*0Sstevel@tonic-gate 	} else {
7101*0Sstevel@tonic-gate 		linklen = 0;
7102*0Sstevel@tonic-gate 	}
7103*0Sstevel@tonic-gate 
7104*0Sstevel@tonic-gate 	/*
7105*0Sstevel@tonic-gate 	 * first fill in the fixed header
7106*0Sstevel@tonic-gate 	 */
7107*0Sstevel@tonic-gate 	hptr = (struct xattr_hdr *)bufhead;
7108*0Sstevel@tonic-gate 	(void) sprintf(hptr->h_version, "%s", XATTR_ARCH_VERS);
7109*0Sstevel@tonic-gate 	(void) sprintf(hptr->h_component_len, "%0*d",
7110*0Sstevel@tonic-gate 	    sizeof (hptr->h_component_len) - 1, complen);
7111*0Sstevel@tonic-gate 	(void) sprintf(hptr->h_link_component_len, "%0*d",
7112*0Sstevel@tonic-gate 	    sizeof (hptr->h_link_component_len) - 1, linklen);
7113*0Sstevel@tonic-gate 	(void) sprintf(hptr->h_size, "%0*d", sizeof (hptr->h_size) - 1, len);
7114*0Sstevel@tonic-gate 
7115*0Sstevel@tonic-gate 	/*
7116*0Sstevel@tonic-gate 	 * Now fill in the filename + attrnames section
7117*0Sstevel@tonic-gate 	 */
7118*0Sstevel@tonic-gate 
7119*0Sstevel@tonic-gate 	tptr = (struct xattr_buf *)(bufhead + sizeof (struct xattr_hdr));
7120*0Sstevel@tonic-gate 	(void) sprintf(tptr->h_namesz, "%0*d", sizeof (tptr->h_namesz) - 1,
7121*0Sstevel@tonic-gate 	    stringlen);
7122*0Sstevel@tonic-gate 	(void) strcpy(tptr->h_names, filename);
7123*0Sstevel@tonic-gate 	(void) strcpy(&tptr->h_names[strlen(filename) + 1], attrname);
7124*0Sstevel@tonic-gate 	tptr->h_typeflag = typeflag;
7125*0Sstevel@tonic-gate 
7126*0Sstevel@tonic-gate 	/*
7127*0Sstevel@tonic-gate 	 * Now fill in the optional link section if we have one
7128*0Sstevel@tonic-gate 	 */
7129*0Sstevel@tonic-gate 
7130*0Sstevel@tonic-gate 	if (linkinfo != (struct Lnk *)NULL) {
7131*0Sstevel@tonic-gate 		tptr = (struct xattr_buf *)(bufhead +
7132*0Sstevel@tonic-gate 		    sizeof (struct xattr_hdr) + complen);
7133*0Sstevel@tonic-gate 
7134*0Sstevel@tonic-gate 		(void) sprintf(tptr->h_namesz, "%0*d",
7135*0Sstevel@tonic-gate 		    sizeof (tptr->h_namesz) - 1, linkstringlen);
7136*0Sstevel@tonic-gate 		(void) strcpy(tptr->h_names, linkinfo->L_gen.g_attrfnam_p);
7137*0Sstevel@tonic-gate 		(void) strcpy(
7138*0Sstevel@tonic-gate 		    &tptr->h_names[strlen(linkinfo->L_gen.g_attrfnam_p) + 1],
7139*0Sstevel@tonic-gate 		    linkinfo->L_gen.g_attrnam_p);
7140*0Sstevel@tonic-gate 		tptr->h_typeflag = typeflag;
7141*0Sstevel@tonic-gate 	}
7142*0Sstevel@tonic-gate 	*attrbuf = (char *)bufhead;
7143*0Sstevel@tonic-gate 	*rlen = len;
7144*0Sstevel@tonic-gate }
7145*0Sstevel@tonic-gate #endif /* O_XATTR */
7146*0Sstevel@tonic-gate 
7147*0Sstevel@tonic-gate static char
7148*0Sstevel@tonic-gate tartype(int type)
7149*0Sstevel@tonic-gate {
7150*0Sstevel@tonic-gate 	switch (type) {
7151*0Sstevel@tonic-gate 
7152*0Sstevel@tonic-gate 	case S_IFDIR:
7153*0Sstevel@tonic-gate 		return (DIRTYPE);
7154*0Sstevel@tonic-gate 
7155*0Sstevel@tonic-gate 	case S_IFLNK:
7156*0Sstevel@tonic-gate 		return (SYMTYPE);
7157*0Sstevel@tonic-gate 
7158*0Sstevel@tonic-gate 	case S_IFIFO:
7159*0Sstevel@tonic-gate 		return (FIFOTYPE);
7160*0Sstevel@tonic-gate 
7161*0Sstevel@tonic-gate 	case S_IFCHR:
7162*0Sstevel@tonic-gate 		return (CHRTYPE);
7163*0Sstevel@tonic-gate 
7164*0Sstevel@tonic-gate 	case S_IFBLK:
7165*0Sstevel@tonic-gate 		return (BLKTYPE);
7166*0Sstevel@tonic-gate 
7167*0Sstevel@tonic-gate 	case S_IFREG:
7168*0Sstevel@tonic-gate 		return (REGTYPE);
7169*0Sstevel@tonic-gate 
7170*0Sstevel@tonic-gate 	default:
7171*0Sstevel@tonic-gate 		return ('\0');
7172*0Sstevel@tonic-gate 	}
7173*0Sstevel@tonic-gate }
7174*0Sstevel@tonic-gate 
7175*0Sstevel@tonic-gate #if defined(O_XATTR)
7176*0Sstevel@tonic-gate static int
7177*0Sstevel@tonic-gate openfile(int omode)
7178*0Sstevel@tonic-gate {
7179*0Sstevel@tonic-gate 
7180*0Sstevel@tonic-gate 	if (G_p->g_attrnam_p != (char *)NULL) {
7181*0Sstevel@tonic-gate 		return (attropen(G_p->g_attrfnam_p, G_p->g_attrnam_p, omode));
7182*0Sstevel@tonic-gate 	} else {
7183*0Sstevel@tonic-gate 		return (openat(G_p->g_dirfd,
7184*0Sstevel@tonic-gate 		    get_component(G_p->g_nam_p), omode));
7185*0Sstevel@tonic-gate 	}
7186*0Sstevel@tonic-gate }
7187*0Sstevel@tonic-gate #else
7188*0Sstevel@tonic-gate static int
7189*0Sstevel@tonic-gate openfile(int omode)
7190*0Sstevel@tonic-gate {
7191*0Sstevel@tonic-gate 	return (openat(G_p->g_dirfd, get_component(G_p->g_nam_p), omode));
7192*0Sstevel@tonic-gate }
7193*0Sstevel@tonic-gate #endif
7194*0Sstevel@tonic-gate 
7195*0Sstevel@tonic-gate #if defined(O_XATTR)
7196*0Sstevel@tonic-gate static int
7197*0Sstevel@tonic-gate read_xattr_hdr()
7198*0Sstevel@tonic-gate {
7199*0Sstevel@tonic-gate 	off_t		bytes;
7200*0Sstevel@tonic-gate 	int		comp_len, link_len;
7201*0Sstevel@tonic-gate 	int		namelen;
7202*0Sstevel@tonic-gate 	int		cnt;
7203*0Sstevel@tonic-gate 	char		*tp;
7204*0Sstevel@tonic-gate 	int		pad;
7205*0Sstevel@tonic-gate 
7206*0Sstevel@tonic-gate 	/*
7207*0Sstevel@tonic-gate 	 * Include any padding in the read.  We need to be positioned
7208*0Sstevel@tonic-gate 	 * at beginning of next header.
7209*0Sstevel@tonic-gate 	 */
7210*0Sstevel@tonic-gate 
7211*0Sstevel@tonic-gate 	bytes = Gen.g_filesz;
7212*0Sstevel@tonic-gate 
7213*0Sstevel@tonic-gate 	if ((xattrhead = e_zalloc(E_NORMAL, (size_t)bytes)) == NULL) {
7214*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
7215*0Sstevel@tonic-gate 		    "Insufficient memory for extended attribute\n"));
7216*0Sstevel@tonic-gate 		return (1);
7217*0Sstevel@tonic-gate 	}
7218*0Sstevel@tonic-gate 
7219*0Sstevel@tonic-gate 	tp = (char *)xattrhead;
7220*0Sstevel@tonic-gate 	while (bytes > 0) {
7221*0Sstevel@tonic-gate 		cnt = (int)(bytes > CPIOBSZ) ? CPIOBSZ : bytes;
7222*0Sstevel@tonic-gate 		FILL(cnt);
7223*0Sstevel@tonic-gate 		(void) memcpy(tp, Buffr.b_out_p, cnt);
7224*0Sstevel@tonic-gate 		tp += cnt;
7225*0Sstevel@tonic-gate 		Buffr.b_out_p += cnt;
7226*0Sstevel@tonic-gate 		Buffr.b_cnt -= (off_t)cnt;
7227*0Sstevel@tonic-gate 		bytes -= (off_t)cnt;
7228*0Sstevel@tonic-gate 	}
7229*0Sstevel@tonic-gate 
7230*0Sstevel@tonic-gate 	pad = (Pad_val + 1 - (Gen.g_filesz & Pad_val)) &
7231*0Sstevel@tonic-gate 	    Pad_val;
7232*0Sstevel@tonic-gate 	if (pad != 0) {
7233*0Sstevel@tonic-gate 		FILL(pad);
7234*0Sstevel@tonic-gate 		Buffr.b_out_p += pad;
7235*0Sstevel@tonic-gate 		Buffr.b_cnt -= (off_t)pad;
7236*0Sstevel@tonic-gate 	}
7237*0Sstevel@tonic-gate 
7238*0Sstevel@tonic-gate 	/*
7239*0Sstevel@tonic-gate 	 * Validate that we can handle header format
7240*0Sstevel@tonic-gate 	 */
7241*0Sstevel@tonic-gate 
7242*0Sstevel@tonic-gate 	if (strcmp(xattrhead->h_version, XATTR_ARCH_VERS) != 0) {
7243*0Sstevel@tonic-gate 		(void) fprintf(stderr,
7244*0Sstevel@tonic-gate 		    gettext("Unknown extended attribute format encountered\n"));
7245*0Sstevel@tonic-gate 		(void) fprintf(stderr,
7246*0Sstevel@tonic-gate 		    gettext("Disabling extended attribute header parsing\n"));
7247*0Sstevel@tonic-gate 		xattrbadhead = 1;
7248*0Sstevel@tonic-gate 		return (1);
7249*0Sstevel@tonic-gate 	}
7250*0Sstevel@tonic-gate 	(void) sscanf(xattrhead->h_component_len, "%10d", &comp_len);
7251*0Sstevel@tonic-gate 	(void) sscanf(xattrhead->h_link_component_len, "%10d", &link_len);
7252*0Sstevel@tonic-gate 	xattrp = (struct xattr_buf *)(((char *)xattrhead) +
7253*0Sstevel@tonic-gate 	    sizeof (struct xattr_hdr));
7254*0Sstevel@tonic-gate 	(void) sscanf(xattrp->h_namesz, "%7d", &namelen);
7255*0Sstevel@tonic-gate 	if (link_len > 0) {
7256*0Sstevel@tonic-gate 		xattr_linkp = (struct xattr_buf *)((int)xattrp + (int)comp_len);
7257*0Sstevel@tonic-gate 	} else {
7258*0Sstevel@tonic-gate 		xattr_linkp = NULL;
7259*0Sstevel@tonic-gate 	}
7260*0Sstevel@tonic-gate 
7261*0Sstevel@tonic-gate 	return (0);
7262*0Sstevel@tonic-gate }
7263*0Sstevel@tonic-gate #endif
7264*0Sstevel@tonic-gate 
7265*0Sstevel@tonic-gate static mode_t
7266*0Sstevel@tonic-gate attrmode(char type)
7267*0Sstevel@tonic-gate {
7268*0Sstevel@tonic-gate 	mode_t mode;
7269*0Sstevel@tonic-gate 
7270*0Sstevel@tonic-gate 	switch (type) {
7271*0Sstevel@tonic-gate 	case '\0':
7272*0Sstevel@tonic-gate 	case REGTYPE:
7273*0Sstevel@tonic-gate 	case LNKTYPE:
7274*0Sstevel@tonic-gate 		mode = S_IFREG;
7275*0Sstevel@tonic-gate 		break;
7276*0Sstevel@tonic-gate 
7277*0Sstevel@tonic-gate 	case SYMTYPE:
7278*0Sstevel@tonic-gate 		mode = S_IFLNK;
7279*0Sstevel@tonic-gate 		break;
7280*0Sstevel@tonic-gate 
7281*0Sstevel@tonic-gate 	case CHRTYPE:
7282*0Sstevel@tonic-gate 		mode = S_IFCHR;
7283*0Sstevel@tonic-gate 		break;
7284*0Sstevel@tonic-gate 	case BLKTYPE:
7285*0Sstevel@tonic-gate 		mode = S_IFBLK;
7286*0Sstevel@tonic-gate 		break;
7287*0Sstevel@tonic-gate 	case DIRTYPE:
7288*0Sstevel@tonic-gate 		mode = S_IFDIR;
7289*0Sstevel@tonic-gate 		break;
7290*0Sstevel@tonic-gate 	case FIFOTYPE:
7291*0Sstevel@tonic-gate 		mode = S_IFIFO;
7292*0Sstevel@tonic-gate 		break;
7293*0Sstevel@tonic-gate 	case CONTTYPE:
7294*0Sstevel@tonic-gate 	default:
7295*0Sstevel@tonic-gate 		mode = 0;
7296*0Sstevel@tonic-gate 	}
7297*0Sstevel@tonic-gate 
7298*0Sstevel@tonic-gate 	return (mode);
7299*0Sstevel@tonic-gate }
7300*0Sstevel@tonic-gate 
7301*0Sstevel@tonic-gate #if defined(O_XATTR)
7302*0Sstevel@tonic-gate static char *
7303*0Sstevel@tonic-gate get_component(char *path)
7304*0Sstevel@tonic-gate {
7305*0Sstevel@tonic-gate 	char *ptr;
7306*0Sstevel@tonic-gate 
7307*0Sstevel@tonic-gate 	ptr = strrchr(path, '/');
7308*0Sstevel@tonic-gate 	if (ptr == NULL) {
7309*0Sstevel@tonic-gate 		return (path);
7310*0Sstevel@tonic-gate 	} else {
7311*0Sstevel@tonic-gate 		/*
7312*0Sstevel@tonic-gate 		 * Handle trailing slash
7313*0Sstevel@tonic-gate 		 */
7314*0Sstevel@tonic-gate 		if (*(ptr + 1) == '\0')
7315*0Sstevel@tonic-gate 			return (ptr);
7316*0Sstevel@tonic-gate 		else
7317*0Sstevel@tonic-gate 			return (ptr + 1);
7318*0Sstevel@tonic-gate 	}
7319*0Sstevel@tonic-gate }
7320*0Sstevel@tonic-gate #else
7321*0Sstevel@tonic-gate static char *
7322*0Sstevel@tonic-gate get_component(char *path)
7323*0Sstevel@tonic-gate {
7324*0Sstevel@tonic-gate 	return (path);
7325*0Sstevel@tonic-gate }
7326*0Sstevel@tonic-gate #endif
7327*0Sstevel@tonic-gate 
7328*0Sstevel@tonic-gate static int
7329*0Sstevel@tonic-gate open_dir(char *name)
7330*0Sstevel@tonic-gate {
7331*0Sstevel@tonic-gate 	int fd = -1;
7332*0Sstevel@tonic-gate 	int cnt = 0;
7333*0Sstevel@tonic-gate 	char *dir;
7334*0Sstevel@tonic-gate 
7335*0Sstevel@tonic-gate 	dir = e_zalloc(E_EXIT, strlen(name) + 1);
7336*0Sstevel@tonic-gate 
7337*0Sstevel@tonic-gate 	/*
7338*0Sstevel@tonic-gate 	 * open directory; creating missing directories along the way.
7339*0Sstevel@tonic-gate 	 */
7340*0Sstevel@tonic-gate 	get_parent(name, dir);
7341*0Sstevel@tonic-gate 	do {
7342*0Sstevel@tonic-gate 		fd = open(dir, O_RDONLY);
7343*0Sstevel@tonic-gate 		if (fd != -1) {
7344*0Sstevel@tonic-gate 			free(dir);
7345*0Sstevel@tonic-gate 			return (fd);
7346*0Sstevel@tonic-gate 		}
7347*0Sstevel@tonic-gate 		cnt++;
7348*0Sstevel@tonic-gate 	} while (cnt <= 1 && missdir(name) == 0);
7349*0Sstevel@tonic-gate 
7350*0Sstevel@tonic-gate 	free(dir);
7351*0Sstevel@tonic-gate 	return (-1);
7352*0Sstevel@tonic-gate }
7353*0Sstevel@tonic-gate 
7354*0Sstevel@tonic-gate static int
7355*0Sstevel@tonic-gate open_dirfd()
7356*0Sstevel@tonic-gate {
7357*0Sstevel@tonic-gate #ifdef O_XATTR
7358*0Sstevel@tonic-gate 	if ((Args & OCt) == 0) {
7359*0Sstevel@tonic-gate 		close_dirfd();
7360*0Sstevel@tonic-gate 		if (G_p->g_attrnam_p != (char *)NULL) {
7361*0Sstevel@tonic-gate 			G_p->g_dirfd = attropen(G_p->g_attrfnam_p,
7362*0Sstevel@tonic-gate 			    ".", O_RDONLY);
7363*0Sstevel@tonic-gate 			if (G_p->g_dirfd == -1 && (Args & (OCi | OCp))) {
7364*0Sstevel@tonic-gate 				G_p->g_dirfd =
7365*0Sstevel@tonic-gate 				    retry_attrdir_open(G_p->g_attrfnam_p);
7366*0Sstevel@tonic-gate 				if (G_p->g_dirfd == -1) {
7367*0Sstevel@tonic-gate 					msg(ERRN,
7368*0Sstevel@tonic-gate 					    "Cannot open attribute"
7369*0Sstevel@tonic-gate 					    " directory of file %s",
7370*0Sstevel@tonic-gate 					    G_p->g_attrfnam_p);
7371*0Sstevel@tonic-gate 					return (1);
7372*0Sstevel@tonic-gate 				}
7373*0Sstevel@tonic-gate 			}
7374*0Sstevel@tonic-gate 		} else {
7375*0Sstevel@tonic-gate 			G_p->g_dirfd = open_dir(G_p->g_nam_p);
7376*0Sstevel@tonic-gate 			if (G_p->g_dirfd == -1) {
7377*0Sstevel@tonic-gate 				msg(ERRN,
7378*0Sstevel@tonic-gate 				    "Cannot open/create %s", G_p->g_nam_p);
7379*0Sstevel@tonic-gate 				return (1);
7380*0Sstevel@tonic-gate 			}
7381*0Sstevel@tonic-gate 		}
7382*0Sstevel@tonic-gate 	} else {
7383*0Sstevel@tonic-gate 		G_p->g_dirfd = -1;
7384*0Sstevel@tonic-gate 	}
7385*0Sstevel@tonic-gate #else
7386*0Sstevel@tonic-gate 	G_p->g_dirfd = -1;
7387*0Sstevel@tonic-gate #endif
7388*0Sstevel@tonic-gate 	return (0);
7389*0Sstevel@tonic-gate }
7390*0Sstevel@tonic-gate 
7391*0Sstevel@tonic-gate static void
7392*0Sstevel@tonic-gate close_dirfd()
7393*0Sstevel@tonic-gate {
7394*0Sstevel@tonic-gate 	if (G_p->g_dirfd != -1) {
7395*0Sstevel@tonic-gate 		(void) close(G_p->g_dirfd);
7396*0Sstevel@tonic-gate 		G_p->g_dirfd = -1;
7397*0Sstevel@tonic-gate 	}
7398*0Sstevel@tonic-gate }
7399*0Sstevel@tonic-gate 
7400*0Sstevel@tonic-gate static void
7401*0Sstevel@tonic-gate write_xattr_hdr()
7402*0Sstevel@tonic-gate {
7403*0Sstevel@tonic-gate 	char *attrbuf = NULL;
7404*0Sstevel@tonic-gate 	int  attrlen = 0;
7405*0Sstevel@tonic-gate 	char *namep;
7406*0Sstevel@tonic-gate 	struct Lnk *tl_p, *linkinfo;
7407*0Sstevel@tonic-gate 
7408*0Sstevel@tonic-gate 
7409*0Sstevel@tonic-gate 	/*
7410*0Sstevel@tonic-gate 	 * namep was allocated in xattrs_out.  It is big enough to hold
7411*0Sstevel@tonic-gate 	 * either the name + .hdr on the end or just the attr name
7412*0Sstevel@tonic-gate 	 */
7413*0Sstevel@tonic-gate 
7414*0Sstevel@tonic-gate #if defined(O_XATTR)
7415*0Sstevel@tonic-gate 	namep = Gen.g_nam_p;
7416*0Sstevel@tonic-gate 	(void) creat_hdr();
7417*0Sstevel@tonic-gate 
7418*0Sstevel@tonic-gate 
7419*0Sstevel@tonic-gate 	if (Args & OCo) {
7420*0Sstevel@tonic-gate 		linkinfo = NULL;
7421*0Sstevel@tonic-gate 		tl_p = Lnk_hd.L_nxt_p;
7422*0Sstevel@tonic-gate 		while (tl_p != &Lnk_hd) {
7423*0Sstevel@tonic-gate 			if (tl_p->L_gen.g_ino == G_p->g_ino &&
7424*0Sstevel@tonic-gate 				tl_p->L_gen.g_dev == G_p->g_dev) {
7425*0Sstevel@tonic-gate 					linkinfo = tl_p;
7426*0Sstevel@tonic-gate 					break; /* found */
7427*0Sstevel@tonic-gate 			}
7428*0Sstevel@tonic-gate 			tl_p = tl_p->L_nxt_p;
7429*0Sstevel@tonic-gate 		}
7430*0Sstevel@tonic-gate 		prepare_xattr_hdr(&attrbuf, Gen.g_attrfnam_p,
7431*0Sstevel@tonic-gate 		    Gen.g_attrnam_p,
7432*0Sstevel@tonic-gate 		    (linkinfo == (struct Lnk *)NULL) ?
7433*0Sstevel@tonic-gate 		    tartype(Gen.g_mode & Ftype) : LNKTYPE,
7434*0Sstevel@tonic-gate 		    linkinfo, &attrlen);
7435*0Sstevel@tonic-gate 		Gen.g_filesz = attrlen;
7436*0Sstevel@tonic-gate 		write_hdr(ARCHIVE_XATTR, (off_t)attrlen);
7437*0Sstevel@tonic-gate 		(void) sprintf(namep, "%s/%s", DEVNULL, Gen.g_attrnam_p);
7438*0Sstevel@tonic-gate 		(void) write_ancillary(attrbuf, attrlen);
7439*0Sstevel@tonic-gate 	}
7440*0Sstevel@tonic-gate 
7441*0Sstevel@tonic-gate 	(void) creat_hdr();
7442*0Sstevel@tonic-gate #endif
7443*0Sstevel@tonic-gate }
7444*0Sstevel@tonic-gate 
7445*0Sstevel@tonic-gate static int
7446*0Sstevel@tonic-gate retry_attrdir_open(char *name)
7447*0Sstevel@tonic-gate {
7448*0Sstevel@tonic-gate 	int dirfd = -1;
7449*0Sstevel@tonic-gate 	struct timeval times[2];
7450*0Sstevel@tonic-gate 	mode_t newmode;
7451*0Sstevel@tonic-gate 	struct stat parentstat;
7452*0Sstevel@tonic-gate 
7453*0Sstevel@tonic-gate 	/*
7454*0Sstevel@tonic-gate 	 * We couldn't get to attrdir. See if its
7455*0Sstevel@tonic-gate 	 * just a mode problem on the parent file.
7456*0Sstevel@tonic-gate 	 * for example: a mode such as r-xr--r--
7457*0Sstevel@tonic-gate 	 * won't let us create an attribute dir
7458*0Sstevel@tonic-gate 	 * if it doesn't already exist.
7459*0Sstevel@tonic-gate 	 */
7460*0Sstevel@tonic-gate 
7461*0Sstevel@tonic-gate 	if (stat(name, &parentstat) == -1) {
7462*0Sstevel@tonic-gate 		msg(ERRN, "Cannot stat file %s", name);
7463*0Sstevel@tonic-gate 		return (-1);
7464*0Sstevel@tonic-gate 	}
7465*0Sstevel@tonic-gate 	newmode = S_IWUSR | parentstat.st_mode;
7466*0Sstevel@tonic-gate 	if (chmod(name, newmode) == -1) {
7467*0Sstevel@tonic-gate 		msg(ERRN, "Cannot change mode of file %s to %o", name, newmode);
7468*0Sstevel@tonic-gate 		return (-1);
7469*0Sstevel@tonic-gate 	}
7470*0Sstevel@tonic-gate 
7471*0Sstevel@tonic-gate 	dirfd = attropen(name, ".", O_RDONLY);
7472*0Sstevel@tonic-gate 	if (dirfd == -1) {
7473*0Sstevel@tonic-gate 		msg(ERRN, "Cannot open attribute directory of file %s", name);
7474*0Sstevel@tonic-gate 		return (-1);
7475*0Sstevel@tonic-gate 	} else {
7476*0Sstevel@tonic-gate 
7477*0Sstevel@tonic-gate 		/*
7478*0Sstevel@tonic-gate 		 * Put mode back to original
7479*0Sstevel@tonic-gate 		 */
7480*0Sstevel@tonic-gate 		if (chmod(name, parentstat.st_mode) != 0) {
7481*0Sstevel@tonic-gate 			msg(ERRN, "Cannot restore permissions of file %s to %o",
7482*0Sstevel@tonic-gate 			    name, parentstat.st_mode);
7483*0Sstevel@tonic-gate 		}
7484*0Sstevel@tonic-gate 
7485*0Sstevel@tonic-gate 		/*
7486*0Sstevel@tonic-gate 		 * Put back time stamps
7487*0Sstevel@tonic-gate 		 */
7488*0Sstevel@tonic-gate 
7489*0Sstevel@tonic-gate 		times[0].tv_sec = parentstat.st_atime;
7490*0Sstevel@tonic-gate 		times[0].tv_usec = 0;
7491*0Sstevel@tonic-gate 		times[1].tv_sec = parentstat.st_mtime;
7492*0Sstevel@tonic-gate 		times[1].tv_usec = 0;
7493*0Sstevel@tonic-gate 		if (utimes(name, times) != 0) {
7494*0Sstevel@tonic-gate 			msg(ERRN, "Cannot reset timestamps on file %s");
7495*0Sstevel@tonic-gate 		}
7496*0Sstevel@tonic-gate 	}
7497*0Sstevel@tonic-gate 
7498*0Sstevel@tonic-gate 	return (dirfd);
7499*0Sstevel@tonic-gate }
7500*0Sstevel@tonic-gate 
7501*0Sstevel@tonic-gate /*
7502*0Sstevel@tonic-gate  * skip over extra slashes in string.
7503*0Sstevel@tonic-gate  *
7504*0Sstevel@tonic-gate  * For example:
7505*0Sstevel@tonic-gate  * /usr/tmp/////
7506*0Sstevel@tonic-gate  *
7507*0Sstevel@tonic-gate  * would return pointer at
7508*0Sstevel@tonic-gate  * /usr/tmp/////
7509*0Sstevel@tonic-gate  *         ^
7510*0Sstevel@tonic-gate  */
7511*0Sstevel@tonic-gate static char *
7512*0Sstevel@tonic-gate skipslashes(char *string, char *start)
7513*0Sstevel@tonic-gate {
7514*0Sstevel@tonic-gate 	while ((string > start) && *(string - 1) == '/') {
7515*0Sstevel@tonic-gate 		string--;
7516*0Sstevel@tonic-gate 	}
7517*0Sstevel@tonic-gate 
7518*0Sstevel@tonic-gate 	return (string);
7519*0Sstevel@tonic-gate }
7520*0Sstevel@tonic-gate 
7521*0Sstevel@tonic-gate static sl_info_t *
7522*0Sstevel@tonic-gate sl_info_alloc(void)
7523*0Sstevel@tonic-gate {
7524*0Sstevel@tonic-gate 	static int num_left;
7525*0Sstevel@tonic-gate 	static sl_info_t *slipool;
7526*0Sstevel@tonic-gate 
7527*0Sstevel@tonic-gate 	if (num_left > 0) {
7528*0Sstevel@tonic-gate 		return (&slipool[--num_left]);
7529*0Sstevel@tonic-gate 	}
7530*0Sstevel@tonic-gate 	num_left = SL_INFO_ALLOC_CHUNK;
7531*0Sstevel@tonic-gate 	slipool = e_zalloc(E_EXIT, sizeof (sl_info_t) * num_left);
7532*0Sstevel@tonic-gate 	return (&slipool[--num_left]);
7533*0Sstevel@tonic-gate }
7534*0Sstevel@tonic-gate 
7535*0Sstevel@tonic-gate /*
7536*0Sstevel@tonic-gate  * If a match for the key values was found in the tree, return a pointer to it.
7537*0Sstevel@tonic-gate  * If a match was not found, insert it and return a pointer to it.  This is
7538*0Sstevel@tonic-gate  * based on Knuth's Algorithm A in Vol 3, section 6.2.3.
7539*0Sstevel@tonic-gate  */
7540*0Sstevel@tonic-gate 
7541*0Sstevel@tonic-gate sl_info_t *
7542*0Sstevel@tonic-gate sl_insert(dev_t device, ino_t inode)
7543*0Sstevel@tonic-gate {
7544*0Sstevel@tonic-gate 	sl_info_t *p;		/* moves down the tree */
7545*0Sstevel@tonic-gate 	sl_info_t *q;		/* scratch */
7546*0Sstevel@tonic-gate 	sl_info_t *r;		/* scratch */
7547*0Sstevel@tonic-gate 	sl_info_t *s;		/* pt where rebalancing may be needed */
7548*0Sstevel@tonic-gate 	sl_info_t *t;		/* father of s */
7549*0Sstevel@tonic-gate 	sl_info_t *head;
7550*0Sstevel@tonic-gate 
7551*0Sstevel@tonic-gate 	int a;			/* used to hold balance factors */
7552*0Sstevel@tonic-gate 	int done;		/* loop control */
7553*0Sstevel@tonic-gate 	int cmpflg;		/* used to hold the result of a comparison */
7554*0Sstevel@tonic-gate 
7555*0Sstevel@tonic-gate 	/* initialize */
7556*0Sstevel@tonic-gate 
7557*0Sstevel@tonic-gate 	head = sl_devhash_lookup(device);
7558*0Sstevel@tonic-gate 
7559*0Sstevel@tonic-gate 	if (head == NULL) {
7560*0Sstevel@tonic-gate 		head = sl_info_alloc();
7561*0Sstevel@tonic-gate 		head->llink = NULL;
7562*0Sstevel@tonic-gate 		head->bal = 0;
7563*0Sstevel@tonic-gate 
7564*0Sstevel@tonic-gate 		p = head->rlink = sl_info_alloc();
7565*0Sstevel@tonic-gate 		p->sl_ino = inode;
7566*0Sstevel@tonic-gate 		p->sl_count = 0;
7567*0Sstevel@tonic-gate 		p->bal = 0;
7568*0Sstevel@tonic-gate 		p->llink = NULL;
7569*0Sstevel@tonic-gate 		p->rlink = NULL;
7570*0Sstevel@tonic-gate 		sl_devhash_insert(device, head);
7571*0Sstevel@tonic-gate 		return (p);
7572*0Sstevel@tonic-gate 	}
7573*0Sstevel@tonic-gate 
7574*0Sstevel@tonic-gate 	t = head;
7575*0Sstevel@tonic-gate 	s = p = head->rlink;
7576*0Sstevel@tonic-gate 
7577*0Sstevel@tonic-gate 	/* compare */
7578*0Sstevel@tonic-gate 
7579*0Sstevel@tonic-gate 	for (done = 0; ! done; ) {
7580*0Sstevel@tonic-gate 		switch (sl_compare(inode, p->sl_ino)) {
7581*0Sstevel@tonic-gate 			case -1:
7582*0Sstevel@tonic-gate 				/* move left */
7583*0Sstevel@tonic-gate 
7584*0Sstevel@tonic-gate 				q = p->llink;
7585*0Sstevel@tonic-gate 
7586*0Sstevel@tonic-gate 				if (q == NULL) {
7587*0Sstevel@tonic-gate 					q = sl_info_alloc();
7588*0Sstevel@tonic-gate 					p->llink = q;
7589*0Sstevel@tonic-gate 					done = 1;
7590*0Sstevel@tonic-gate 					continue;
7591*0Sstevel@tonic-gate 				}
7592*0Sstevel@tonic-gate 
7593*0Sstevel@tonic-gate 				break;
7594*0Sstevel@tonic-gate 
7595*0Sstevel@tonic-gate 			case 0:
7596*0Sstevel@tonic-gate 				/* found it */
7597*0Sstevel@tonic-gate 				return (p);
7598*0Sstevel@tonic-gate 				break;
7599*0Sstevel@tonic-gate 
7600*0Sstevel@tonic-gate 			case 1:
7601*0Sstevel@tonic-gate 				/* move right */
7602*0Sstevel@tonic-gate 
7603*0Sstevel@tonic-gate 				q = p->rlink;
7604*0Sstevel@tonic-gate 
7605*0Sstevel@tonic-gate 				if (q == NULL) {
7606*0Sstevel@tonic-gate 					q = sl_info_alloc();
7607*0Sstevel@tonic-gate 					p->rlink = q;
7608*0Sstevel@tonic-gate 					done = 1;
7609*0Sstevel@tonic-gate 					continue;
7610*0Sstevel@tonic-gate 				}
7611*0Sstevel@tonic-gate 
7612*0Sstevel@tonic-gate 				break;
7613*0Sstevel@tonic-gate 		}
7614*0Sstevel@tonic-gate 
7615*0Sstevel@tonic-gate 		if (q->bal != 0) {
7616*0Sstevel@tonic-gate 			t = p;
7617*0Sstevel@tonic-gate 			s = q;
7618*0Sstevel@tonic-gate 		}
7619*0Sstevel@tonic-gate 
7620*0Sstevel@tonic-gate 		p = q;
7621*0Sstevel@tonic-gate 	}
7622*0Sstevel@tonic-gate 
7623*0Sstevel@tonic-gate 	/* insert */
7624*0Sstevel@tonic-gate 
7625*0Sstevel@tonic-gate 	q->sl_ino = inode;
7626*0Sstevel@tonic-gate 	q->sl_count = 0;
7627*0Sstevel@tonic-gate 	q->llink = q->rlink = NULL;
7628*0Sstevel@tonic-gate 	q->bal = 0;
7629*0Sstevel@tonic-gate 
7630*0Sstevel@tonic-gate 	/* adjust balance factors */
7631*0Sstevel@tonic-gate 
7632*0Sstevel@tonic-gate 	if ((cmpflg = sl_compare(inode, s->sl_ino)) < 0) {
7633*0Sstevel@tonic-gate 		r = p = s->llink;
7634*0Sstevel@tonic-gate 	} else {
7635*0Sstevel@tonic-gate 		r = p = s->rlink;
7636*0Sstevel@tonic-gate 	}
7637*0Sstevel@tonic-gate 
7638*0Sstevel@tonic-gate 	while (p != q) {
7639*0Sstevel@tonic-gate 		switch (sl_compare(inode, p->sl_ino)) {
7640*0Sstevel@tonic-gate 			case -1:
7641*0Sstevel@tonic-gate 				p->bal = -1;
7642*0Sstevel@tonic-gate 				p = p->llink;
7643*0Sstevel@tonic-gate 				break;
7644*0Sstevel@tonic-gate 
7645*0Sstevel@tonic-gate 			case 0:
7646*0Sstevel@tonic-gate 				break;
7647*0Sstevel@tonic-gate 
7648*0Sstevel@tonic-gate 			case 1:
7649*0Sstevel@tonic-gate 				p->bal = 1;
7650*0Sstevel@tonic-gate 				p = p->rlink;
7651*0Sstevel@tonic-gate 				break;
7652*0Sstevel@tonic-gate 		}
7653*0Sstevel@tonic-gate 	}
7654*0Sstevel@tonic-gate 
7655*0Sstevel@tonic-gate 	/* balancing act */
7656*0Sstevel@tonic-gate 
7657*0Sstevel@tonic-gate 	if (cmpflg < 0) {
7658*0Sstevel@tonic-gate 		a = -1;
7659*0Sstevel@tonic-gate 	} else {
7660*0Sstevel@tonic-gate 		a = 1;
7661*0Sstevel@tonic-gate 	}
7662*0Sstevel@tonic-gate 
7663*0Sstevel@tonic-gate 	if (s->bal == 0) {
7664*0Sstevel@tonic-gate 		s->bal = a;
7665*0Sstevel@tonic-gate 		head->llink = (sl_info_t *)((int)head->llink + 1);
7666*0Sstevel@tonic-gate 		return (q);
7667*0Sstevel@tonic-gate 	} else if (s->bal == -a) {
7668*0Sstevel@tonic-gate 		s->bal = 0;
7669*0Sstevel@tonic-gate 		return (q);
7670*0Sstevel@tonic-gate 	}
7671*0Sstevel@tonic-gate 
7672*0Sstevel@tonic-gate 	/*
7673*0Sstevel@tonic-gate 	 * (s->bal == a)
7674*0Sstevel@tonic-gate 	 */
7675*0Sstevel@tonic-gate 
7676*0Sstevel@tonic-gate 	if (r->bal == a) {
7677*0Sstevel@tonic-gate 		/* single rotation */
7678*0Sstevel@tonic-gate 
7679*0Sstevel@tonic-gate 		p = r;
7680*0Sstevel@tonic-gate 
7681*0Sstevel@tonic-gate 		if (a == -1) {
7682*0Sstevel@tonic-gate 			s->llink = r->rlink;
7683*0Sstevel@tonic-gate 			r->rlink = s;
7684*0Sstevel@tonic-gate 		} else if (a == 1) {
7685*0Sstevel@tonic-gate 			s->rlink = r->llink;
7686*0Sstevel@tonic-gate 			r->llink = s;
7687*0Sstevel@tonic-gate 		}
7688*0Sstevel@tonic-gate 
7689*0Sstevel@tonic-gate 		s->bal = r->bal = 0;
7690*0Sstevel@tonic-gate 
7691*0Sstevel@tonic-gate 	} else if (r->bal == -a) {
7692*0Sstevel@tonic-gate 		/* double rotation */
7693*0Sstevel@tonic-gate 
7694*0Sstevel@tonic-gate 		if (a == -1) {
7695*0Sstevel@tonic-gate 			p = r->rlink;
7696*0Sstevel@tonic-gate 			r->rlink = p->llink;
7697*0Sstevel@tonic-gate 			p->llink = r;
7698*0Sstevel@tonic-gate 			s->llink = p->rlink;
7699*0Sstevel@tonic-gate 			p->rlink = s;
7700*0Sstevel@tonic-gate 		} else if (a == 1) {
7701*0Sstevel@tonic-gate 			p = r->llink;
7702*0Sstevel@tonic-gate 			r->llink = p->rlink;
7703*0Sstevel@tonic-gate 			p->rlink = r;
7704*0Sstevel@tonic-gate 			s->rlink = p->llink;
7705*0Sstevel@tonic-gate 			p->llink = s;
7706*0Sstevel@tonic-gate 		}
7707*0Sstevel@tonic-gate 
7708*0Sstevel@tonic-gate 		if (p->bal == 0) {
7709*0Sstevel@tonic-gate 			s->bal = 0;
7710*0Sstevel@tonic-gate 			r->bal = 0;
7711*0Sstevel@tonic-gate 		} else if (p->bal == -a) {
7712*0Sstevel@tonic-gate 			s->bal = 0;
7713*0Sstevel@tonic-gate 			r->bal = a;
7714*0Sstevel@tonic-gate 		} else if (p->bal == a) {
7715*0Sstevel@tonic-gate 			s->bal = -a;
7716*0Sstevel@tonic-gate 			r->bal = 0;
7717*0Sstevel@tonic-gate 		}
7718*0Sstevel@tonic-gate 
7719*0Sstevel@tonic-gate 		p->bal = 0;
7720*0Sstevel@tonic-gate 	}
7721*0Sstevel@tonic-gate 
7722*0Sstevel@tonic-gate 	/* finishing touch */
7723*0Sstevel@tonic-gate 
7724*0Sstevel@tonic-gate 	if (s == t->rlink) {
7725*0Sstevel@tonic-gate 		t->rlink = p;
7726*0Sstevel@tonic-gate 	} else {
7727*0Sstevel@tonic-gate 		t->llink = p;
7728*0Sstevel@tonic-gate 	}
7729*0Sstevel@tonic-gate 
7730*0Sstevel@tonic-gate 	return (q);
7731*0Sstevel@tonic-gate }
7732*0Sstevel@tonic-gate 
7733*0Sstevel@tonic-gate /*
7734*0Sstevel@tonic-gate  * sl_numlinks: return the number of links that we saw during our preview.
7735*0Sstevel@tonic-gate  */
7736*0Sstevel@tonic-gate 
7737*0Sstevel@tonic-gate static ulong_t
7738*0Sstevel@tonic-gate sl_numlinks(dev_t device, ino_t inode)
7739*0Sstevel@tonic-gate {
7740*0Sstevel@tonic-gate 	sl_info_t *p = sl_search(device, inode);
7741*0Sstevel@tonic-gate 
7742*0Sstevel@tonic-gate 	if (p) {
7743*0Sstevel@tonic-gate 	    return (p->sl_count);
7744*0Sstevel@tonic-gate 	} else {
7745*0Sstevel@tonic-gate 	    return (1);
7746*0Sstevel@tonic-gate 	}
7747*0Sstevel@tonic-gate }
7748*0Sstevel@tonic-gate 
7749*0Sstevel@tonic-gate /*
7750*0Sstevel@tonic-gate  * sl_preview_synonyms:  Read the file list from the input stream, remembering
7751*0Sstevel@tonic-gate  * each reference to each file.
7752*0Sstevel@tonic-gate  */
7753*0Sstevel@tonic-gate 
7754*0Sstevel@tonic-gate static void
7755*0Sstevel@tonic-gate sl_preview_synonyms(void)
7756*0Sstevel@tonic-gate {
7757*0Sstevel@tonic-gate 	char buf [APATH+1];
7758*0Sstevel@tonic-gate 	char *s;
7759*0Sstevel@tonic-gate 
7760*0Sstevel@tonic-gate 	char *suffix = "/cpioXXXXXX";
7761*0Sstevel@tonic-gate 	char *tmpdir = getenv("TMPDIR");
7762*0Sstevel@tonic-gate 	int    tmpfd, islnk;
7763*0Sstevel@tonic-gate 	FILE *tmpfile;
7764*0Sstevel@tonic-gate 	char *tmpfname;
7765*0Sstevel@tonic-gate 
7766*0Sstevel@tonic-gate 	if (tmpdir == NULL || *tmpdir == '\0' ||
7767*0Sstevel@tonic-gate 	    (strlen(tmpdir) + strlen(suffix)) > APATH) {
7768*0Sstevel@tonic-gate 		struct statvfs tdsb;
7769*0Sstevel@tonic-gate 
7770*0Sstevel@tonic-gate 		tmpdir = "/var/tmp";
7771*0Sstevel@tonic-gate 
7772*0Sstevel@tonic-gate 		/* /var/tmp is read-only in the mini-root environment */
7773*0Sstevel@tonic-gate 
7774*0Sstevel@tonic-gate 		if (statvfs(tmpdir, &tdsb) == -1 || tdsb.f_flag & ST_RDONLY) {
7775*0Sstevel@tonic-gate 			tmpdir = "/tmp";
7776*0Sstevel@tonic-gate 		}
7777*0Sstevel@tonic-gate 	}
7778*0Sstevel@tonic-gate 
7779*0Sstevel@tonic-gate 	tmpfname = e_zalloc(E_EXIT, strlen(tmpdir) + strlen(suffix) + 1);
7780*0Sstevel@tonic-gate 
7781*0Sstevel@tonic-gate 	(void) strcpy(tmpfname, tmpdir);
7782*0Sstevel@tonic-gate 	(void) strcat(tmpfname, suffix);
7783*0Sstevel@tonic-gate 
7784*0Sstevel@tonic-gate 	if ((tmpfd = mkstemp(tmpfname)) == -1) {
7785*0Sstevel@tonic-gate 		msg(EXTN, "cannot open tmpfile %s", tmpfname);
7786*0Sstevel@tonic-gate 	}
7787*0Sstevel@tonic-gate 
7788*0Sstevel@tonic-gate 	if (unlink(tmpfname) == -1) {
7789*0Sstevel@tonic-gate 		msg(EXTN, "cannot unlink tmpfile %s", tmpfname);
7790*0Sstevel@tonic-gate 	}
7791*0Sstevel@tonic-gate 
7792*0Sstevel@tonic-gate 	if ((tmpfile = fdopen(tmpfd, "w+")) == NULL) {
7793*0Sstevel@tonic-gate 		msg(EXTN, "cannot fdopen tmpfile %s", tmpfname);
7794*0Sstevel@tonic-gate 	}
7795*0Sstevel@tonic-gate 
7796*0Sstevel@tonic-gate 	while ((s = fgets(buf, APATH+1, In_p)) != NULL) {
7797*0Sstevel@tonic-gate 		size_t lastchar;
7798*0Sstevel@tonic-gate 		struct stat sb;
7799*0Sstevel@tonic-gate 
7800*0Sstevel@tonic-gate 		if (fputs(buf, tmpfile) == EOF) {
7801*0Sstevel@tonic-gate 			msg(EXTN, "problem writing to tmpfile %s", tmpfname);
7802*0Sstevel@tonic-gate 		}
7803*0Sstevel@tonic-gate 
7804*0Sstevel@tonic-gate 		/* pre-process the name */
7805*0Sstevel@tonic-gate 
7806*0Sstevel@tonic-gate 		lastchar = strlen(s) - 1;
7807*0Sstevel@tonic-gate 
7808*0Sstevel@tonic-gate 		if (s[lastchar] != '\n' && lastchar == APATH - 1) {
7809*0Sstevel@tonic-gate 			continue;
7810*0Sstevel@tonic-gate 		} else {
7811*0Sstevel@tonic-gate 			s[lastchar] = '\0';
7812*0Sstevel@tonic-gate 		}
7813*0Sstevel@tonic-gate 
7814*0Sstevel@tonic-gate 		while (s[0] == '.' && s[1] == '/') {
7815*0Sstevel@tonic-gate 			s += 2;
7816*0Sstevel@tonic-gate 			while (s[0] == '/') {
7817*0Sstevel@tonic-gate 				s++;
7818*0Sstevel@tonic-gate 			}
7819*0Sstevel@tonic-gate 		}
7820*0Sstevel@tonic-gate 
7821*0Sstevel@tonic-gate 		if (lstat(s, &sb) < 0) {
7822*0Sstevel@tonic-gate 			continue;
7823*0Sstevel@tonic-gate 		}
7824*0Sstevel@tonic-gate 		islnk = 0;
7825*0Sstevel@tonic-gate 		if (S_ISLNK(sb.st_mode)) {
7826*0Sstevel@tonic-gate 			islnk = 1;
7827*0Sstevel@tonic-gate 			if (Args & OCL) {
7828*0Sstevel@tonic-gate 				if (stat(s, &sb) < 0) {
7829*0Sstevel@tonic-gate 					continue;
7830*0Sstevel@tonic-gate 				}
7831*0Sstevel@tonic-gate 			}
7832*0Sstevel@tonic-gate 		}
7833*0Sstevel@tonic-gate 		sl_remember_tgt(&sb, islnk);
7834*0Sstevel@tonic-gate 
7835*0Sstevel@tonic-gate #if defined(O_XATTR)
7836*0Sstevel@tonic-gate 		if (Atflag) {
7837*0Sstevel@tonic-gate 			int  dirfd;
7838*0Sstevel@tonic-gate 			DIR  *dirp;
7839*0Sstevel@tonic-gate 			struct dirent *dp;
7840*0Sstevel@tonic-gate 
7841*0Sstevel@tonic-gate 			if (pathconf(s, _PC_XATTR_EXISTS) != 1)
7842*0Sstevel@tonic-gate 				continue;
7843*0Sstevel@tonic-gate 
7844*0Sstevel@tonic-gate 			dirfd = attropen(s, ".", O_RDONLY);
7845*0Sstevel@tonic-gate 			if (dirfd == -1)
7846*0Sstevel@tonic-gate 				continue;
7847*0Sstevel@tonic-gate 
7848*0Sstevel@tonic-gate 			tmpfd = dup(dirfd);
7849*0Sstevel@tonic-gate 			if (tmpfd == -1) {
7850*0Sstevel@tonic-gate 				(void) close(dirfd);
7851*0Sstevel@tonic-gate 				continue;
7852*0Sstevel@tonic-gate 			}
7853*0Sstevel@tonic-gate 			dirp = fdopendir(tmpfd);
7854*0Sstevel@tonic-gate 			if (dirp == NULL) {
7855*0Sstevel@tonic-gate 				(void) close(dirfd);
7856*0Sstevel@tonic-gate 				(void) close(tmpfd);
7857*0Sstevel@tonic-gate 				continue;
7858*0Sstevel@tonic-gate 			}
7859*0Sstevel@tonic-gate 
7860*0Sstevel@tonic-gate 			while (dp = readdir(dirp)) {
7861*0Sstevel@tonic-gate 				if ((dp->d_name[0] == '.' &&
7862*0Sstevel@tonic-gate 				    dp->d_name[1] == '\0') ||
7863*0Sstevel@tonic-gate 				    (dp->d_name[0] == '.' &&
7864*0Sstevel@tonic-gate 				    dp->d_name[1] == '.' &&
7865*0Sstevel@tonic-gate 				    dp->d_name[2] == '\0'))
7866*0Sstevel@tonic-gate 					continue;
7867*0Sstevel@tonic-gate 
7868*0Sstevel@tonic-gate 				if (fstatat(dirfd, dp->d_name, &sb,
7869*0Sstevel@tonic-gate 				    AT_SYMLINK_NOFOLLOW) < 0) {
7870*0Sstevel@tonic-gate 					continue;
7871*0Sstevel@tonic-gate 				}
7872*0Sstevel@tonic-gate 				islnk = 0;
7873*0Sstevel@tonic-gate 				if (S_ISLNK(sb.st_mode)) {
7874*0Sstevel@tonic-gate 					islnk = 1;
7875*0Sstevel@tonic-gate 					if (Args & OCL) {
7876*0Sstevel@tonic-gate 						if (fstatat(dirfd, dp->d_name,
7877*0Sstevel@tonic-gate 						    &sb, 0) < 0) {
7878*0Sstevel@tonic-gate 							continue;
7879*0Sstevel@tonic-gate 						}
7880*0Sstevel@tonic-gate 					}
7881*0Sstevel@tonic-gate 				}
7882*0Sstevel@tonic-gate 				sl_remember_tgt(&sb, islnk);
7883*0Sstevel@tonic-gate 			}
7884*0Sstevel@tonic-gate 			(void) closedir(dirp);
7885*0Sstevel@tonic-gate 			(void) close(dirfd);
7886*0Sstevel@tonic-gate 		}
7887*0Sstevel@tonic-gate #endif
7888*0Sstevel@tonic-gate 	}
7889*0Sstevel@tonic-gate 
7890*0Sstevel@tonic-gate 	if (ferror(In_p)) {
7891*0Sstevel@tonic-gate 		msg(EXTN, "error reading stdin");
7892*0Sstevel@tonic-gate 	}
7893*0Sstevel@tonic-gate 
7894*0Sstevel@tonic-gate 	if (fseek(tmpfile, 0L, SEEK_SET) == -1) {
7895*0Sstevel@tonic-gate 		msg(EXTN, "cannot fseek on tmpfile %s", tmpfname);
7896*0Sstevel@tonic-gate 	}
7897*0Sstevel@tonic-gate 
7898*0Sstevel@tonic-gate 	In_p = tmpfile;
7899*0Sstevel@tonic-gate 	free(tmpfname);
7900*0Sstevel@tonic-gate }
7901*0Sstevel@tonic-gate 
7902*0Sstevel@tonic-gate /*
7903*0Sstevel@tonic-gate  * sl_remember_tgt: Add the device/inode for lstat or stat info to the list of
7904*0Sstevel@tonic-gate  * those we've seen before.
7905*0Sstevel@tonic-gate  *
7906*0Sstevel@tonic-gate  * This tree (rooted under head) is keyed by the device/inode of the file
7907*0Sstevel@tonic-gate  * being pointed to.  A count is kept of the number of references encountered
7908*0Sstevel@tonic-gate  * so far.
7909*0Sstevel@tonic-gate  */
7910*0Sstevel@tonic-gate 
7911*0Sstevel@tonic-gate static void
7912*0Sstevel@tonic-gate sl_remember_tgt(const struct stat *sbp, int isSymlink)
7913*0Sstevel@tonic-gate {
7914*0Sstevel@tonic-gate 	sl_info_t *p;
7915*0Sstevel@tonic-gate 	dev_t device;
7916*0Sstevel@tonic-gate 	ino_t inode;
7917*0Sstevel@tonic-gate 
7918*0Sstevel@tonic-gate 	device = sbp->st_dev;
7919*0Sstevel@tonic-gate 	inode  = sbp->st_ino;
7920*0Sstevel@tonic-gate 
7921*0Sstevel@tonic-gate 	/* Determine whether we've seen this one before */
7922*0Sstevel@tonic-gate 
7923*0Sstevel@tonic-gate 	p = sl_insert(device, inode);
7924*0Sstevel@tonic-gate 
7925*0Sstevel@tonic-gate 	if (p->sl_count > 0) {
7926*0Sstevel@tonic-gate 		/*
7927*0Sstevel@tonic-gate 		 * We have seen this file before.
7928*0Sstevel@tonic-gate 		 * Note that if we are not chasing symlinks, and this one is a
7929*0Sstevel@tonic-gate 		 * symlink, it is identically the one we saw before (you cannot
7930*0Sstevel@tonic-gate 		 * have hard links to symlinks); in this case, we leave the
7931*0Sstevel@tonic-gate 		 * count alone, so that we don't wind up archiving a symlink to
7932*0Sstevel@tonic-gate 		 * itself.
7933*0Sstevel@tonic-gate 		 */
7934*0Sstevel@tonic-gate 
7935*0Sstevel@tonic-gate 		if ((Args & OCL) || (! isSymlink)) {
7936*0Sstevel@tonic-gate 			p->sl_count++;
7937*0Sstevel@tonic-gate 		}
7938*0Sstevel@tonic-gate 	} else {
7939*0Sstevel@tonic-gate 		/* We have not seen this file before */
7940*0Sstevel@tonic-gate 
7941*0Sstevel@tonic-gate 		p->sl_count = 1;
7942*0Sstevel@tonic-gate 
7943*0Sstevel@tonic-gate 		if (Use_old_stat) {
7944*0Sstevel@tonic-gate 			/* -Hodc: remap inode (-1 on overflow) */
7945*0Sstevel@tonic-gate 
7946*0Sstevel@tonic-gate 			sl_remap_t  *q;
7947*0Sstevel@tonic-gate 
7948*0Sstevel@tonic-gate 			for (q = sl_remap_head; q && (q->dev != device);
7949*0Sstevel@tonic-gate 			    q = q->next) {
7950*0Sstevel@tonic-gate 				/* do nothing */
7951*0Sstevel@tonic-gate 			}
7952*0Sstevel@tonic-gate 
7953*0Sstevel@tonic-gate 			if (q == NULL) {
7954*0Sstevel@tonic-gate 				q = e_zalloc(E_EXIT, sizeof (sl_remap_t));
7955*0Sstevel@tonic-gate 				q->dev = device;
7956*0Sstevel@tonic-gate 				p->sl_ino2 = q->inode_count = 1;
7957*0Sstevel@tonic-gate 
7958*0Sstevel@tonic-gate 				q->next = (sl_remap_head) ?
7959*0Sstevel@tonic-gate 				    sl_remap_head->next : NULL;
7960*0Sstevel@tonic-gate 				sl_remap_head = q;
7961*0Sstevel@tonic-gate 			} else {
7962*0Sstevel@tonic-gate 				if ((size_t)q->inode_count <=
7963*0Sstevel@tonic-gate 				    ((1 << (sizeof (o_ino_t) * 8)) - 1)) {
7964*0Sstevel@tonic-gate 					/* fits in o_ino_t */
7965*0Sstevel@tonic-gate 					p->sl_ino2 = ++(q->inode_count);
7966*0Sstevel@tonic-gate 				} else {
7967*0Sstevel@tonic-gate 					p->sl_ino2 = (ino_t)-1;
7968*0Sstevel@tonic-gate 				}
7969*0Sstevel@tonic-gate 			}
7970*0Sstevel@tonic-gate 		}
7971*0Sstevel@tonic-gate 	}
7972*0Sstevel@tonic-gate }
7973*0Sstevel@tonic-gate 
7974*0Sstevel@tonic-gate /*
7975*0Sstevel@tonic-gate  * A faster search, which does not insert the key values into the tree.
7976*0Sstevel@tonic-gate  * If the a match was found in the tree, return a pointer to it.  If it was not
7977*0Sstevel@tonic-gate  * found, return NULL.
7978*0Sstevel@tonic-gate  */
7979*0Sstevel@tonic-gate 
7980*0Sstevel@tonic-gate sl_info_t *
7981*0Sstevel@tonic-gate sl_search(dev_t device, ino_t inode)
7982*0Sstevel@tonic-gate {
7983*0Sstevel@tonic-gate 	sl_info_t *p;		/* moves down the tree */
7984*0Sstevel@tonic-gate 	int c;			/* comparison value */
7985*0Sstevel@tonic-gate 	sl_info_t *retval = NULL; /* return value */
7986*0Sstevel@tonic-gate 	sl_info_t *head;
7987*0Sstevel@tonic-gate 
7988*0Sstevel@tonic-gate 	head = sl_devhash_lookup(device);
7989*0Sstevel@tonic-gate 	if (head != NULL) {
7990*0Sstevel@tonic-gate 		for (p = head->rlink; p; ) {
7991*0Sstevel@tonic-gate 			if ((c = sl_compare(inode, p->sl_ino)) == 0) {
7992*0Sstevel@tonic-gate 				retval = p;
7993*0Sstevel@tonic-gate 				break;
7994*0Sstevel@tonic-gate 			} else if (c < 0) {
7995*0Sstevel@tonic-gate 				p = p->llink;
7996*0Sstevel@tonic-gate 			} else {
7997*0Sstevel@tonic-gate 				p = p->rlink;
7998*0Sstevel@tonic-gate 			}
7999*0Sstevel@tonic-gate 		}
8000*0Sstevel@tonic-gate 	}
8001*0Sstevel@tonic-gate 
8002*0Sstevel@tonic-gate 	return (retval);
8003*0Sstevel@tonic-gate }
8004*0Sstevel@tonic-gate 
8005*0Sstevel@tonic-gate static sl_info_t *
8006*0Sstevel@tonic-gate sl_devhash_lookup(dev_t device)
8007*0Sstevel@tonic-gate {
8008*0Sstevel@tonic-gate 	int key;
8009*0Sstevel@tonic-gate 	sl_info_link_t *lp;
8010*0Sstevel@tonic-gate 	static sl_info_link_t *devcache;
8011*0Sstevel@tonic-gate 
8012*0Sstevel@tonic-gate 	if (devcache != NULL && devcache->dev == device) {
8013*0Sstevel@tonic-gate 		return (devcache->head);
8014*0Sstevel@tonic-gate 	}
8015*0Sstevel@tonic-gate 
8016*0Sstevel@tonic-gate 	key = DEV_HASHKEY(device);
8017*0Sstevel@tonic-gate 	for (lp = sl_devhash[key]; lp; lp = lp->next) {
8018*0Sstevel@tonic-gate 		if (lp->dev == device) {
8019*0Sstevel@tonic-gate 			devcache = lp;
8020*0Sstevel@tonic-gate 			return (lp->head);
8021*0Sstevel@tonic-gate 		}
8022*0Sstevel@tonic-gate 	}
8023*0Sstevel@tonic-gate 	return (NULL);
8024*0Sstevel@tonic-gate }
8025*0Sstevel@tonic-gate 
8026*0Sstevel@tonic-gate static void
8027*0Sstevel@tonic-gate sl_devhash_insert(dev_t device, sl_info_t *head)
8028*0Sstevel@tonic-gate {
8029*0Sstevel@tonic-gate 	int key = DEV_HASHKEY(device);
8030*0Sstevel@tonic-gate 	sl_info_link_t *lp;
8031*0Sstevel@tonic-gate 
8032*0Sstevel@tonic-gate 	lp = e_zalloc(E_EXIT, sizeof (sl_info_link_t));
8033*0Sstevel@tonic-gate 	lp->dev = device;
8034*0Sstevel@tonic-gate 	lp->head = head;
8035*0Sstevel@tonic-gate 	lp->next = sl_devhash[key];
8036*0Sstevel@tonic-gate 	sl_devhash[key] = lp;
8037*0Sstevel@tonic-gate }
8038*0Sstevel@tonic-gate 
8039*0Sstevel@tonic-gate static void
8040*0Sstevel@tonic-gate chop_endslashes(char *path)
8041*0Sstevel@tonic-gate {
8042*0Sstevel@tonic-gate 	char *end, *ptr;
8043*0Sstevel@tonic-gate 
8044*0Sstevel@tonic-gate 	end = &path[strlen(path) -1];
8045*0Sstevel@tonic-gate 	if (*end == '/' && end != path) {
8046*0Sstevel@tonic-gate 		ptr = skipslashes(end, path);
8047*0Sstevel@tonic-gate 		if (ptr != NULL && ptr != path) {
8048*0Sstevel@tonic-gate 			*ptr = '\0';
8049*0Sstevel@tonic-gate 		}
8050*0Sstevel@tonic-gate 	}
8051*0Sstevel@tonic-gate }
8052*0Sstevel@tonic-gate 
8053*0Sstevel@tonic-gate #if !defined(O_XATTR)
8054*0Sstevel@tonic-gate int
8055*0Sstevel@tonic-gate openat64(int fd, char *name, int oflag, mode_t cmode)
8056*0Sstevel@tonic-gate {
8057*0Sstevel@tonic-gate 	return (open64(name, oflag, cmode));
8058*0Sstevel@tonic-gate }
8059*0Sstevel@tonic-gate 
8060*0Sstevel@tonic-gate int
8061*0Sstevel@tonic-gate openat(int fd, char *name, int oflag, mode_t cmode)
8062*0Sstevel@tonic-gate {
8063*0Sstevel@tonic-gate 	return (open(name, oflag, cmode));
8064*0Sstevel@tonic-gate }
8065*0Sstevel@tonic-gate 
8066*0Sstevel@tonic-gate int
8067*0Sstevel@tonic-gate fchownat(int fd, char *name, uid_t owner, gid_t group, int flag)
8068*0Sstevel@tonic-gate {
8069*0Sstevel@tonic-gate 	if (flag == AT_SYMLINK_NOFOLLOW)
8070*0Sstevel@tonic-gate 		return (lchown(name, owner, group));
8071*0Sstevel@tonic-gate 	else
8072*0Sstevel@tonic-gate 		return (chown(name, owner, group));
8073*0Sstevel@tonic-gate }
8074*0Sstevel@tonic-gate 
8075*0Sstevel@tonic-gate int
8076*0Sstevel@tonic-gate renameat(int fromfd, char *old, int tofd, char *new)
8077*0Sstevel@tonic-gate {
8078*0Sstevel@tonic-gate 	return (rename(old, new));
8079*0Sstevel@tonic-gate }
8080*0Sstevel@tonic-gate 
8081*0Sstevel@tonic-gate int
8082*0Sstevel@tonic-gate futimesat(int fd, char *path, struct timeval times[2])
8083*0Sstevel@tonic-gate {
8084*0Sstevel@tonic-gate 	return (utimes(path, times));
8085*0Sstevel@tonic-gate }
8086*0Sstevel@tonic-gate 
8087*0Sstevel@tonic-gate int
8088*0Sstevel@tonic-gate unlinkat(int dirfd, char *path, int flag)
8089*0Sstevel@tonic-gate {
8090*0Sstevel@tonic-gate 	if (flag == AT_REMOVEDIR) {
8091*0Sstevel@tonic-gate 		return (rmdir(path));
8092*0Sstevel@tonic-gate 	} else {
8093*0Sstevel@tonic-gate 		return (unlink(path));
8094*0Sstevel@tonic-gate 	}
8095*0Sstevel@tonic-gate }
8096*0Sstevel@tonic-gate 
8097*0Sstevel@tonic-gate int
8098*0Sstevel@tonic-gate fstatat(int fd, char *path, struct stat *buf, int flag)
8099*0Sstevel@tonic-gate {
8100*0Sstevel@tonic-gate 	if (flag == AT_SYMLINK_NOFOLLOW)
8101*0Sstevel@tonic-gate 		return (lstat(path, buf));
8102*0Sstevel@tonic-gate 	else
8103*0Sstevel@tonic-gate 		return (stat(path, buf));
8104*0Sstevel@tonic-gate }
8105*0Sstevel@tonic-gate 
8106*0Sstevel@tonic-gate int
8107*0Sstevel@tonic-gate attropen(char *file, char *attr, int omode, mode_t cmode)
8108*0Sstevel@tonic-gate {
8109*0Sstevel@tonic-gate 	errno = ENOTSUP;
8110*0Sstevel@tonic-gate 	return (-1);
8111*0Sstevel@tonic-gate }
8112*0Sstevel@tonic-gate #endif
8113