xref: /titanic_53/usr/src/cmd/unpack/unpack.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.21	*/
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate /*
34*7c478bd9Sstevel@tonic-gate  *	Huffman decompressor
35*7c478bd9Sstevel@tonic-gate  *	Usage:	pcat filename...
36*7c478bd9Sstevel@tonic-gate  *	or	unpack filename...
37*7c478bd9Sstevel@tonic-gate  */
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include <stdio.h>
40*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
41*7c478bd9Sstevel@tonic-gate #include <setjmp.h>
42*7c478bd9Sstevel@tonic-gate #include <signal.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
45*7c478bd9Sstevel@tonic-gate #include <unistd.h>
46*7c478bd9Sstevel@tonic-gate #include <locale.h>
47*7c478bd9Sstevel@tonic-gate #include <utime.h>
48*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
49*7c478bd9Sstevel@tonic-gate #include <limits.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
51*7c478bd9Sstevel@tonic-gate #include <dirent.h>
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate static struct utimbuf u_times;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate static jmp_buf env;
56*7c478bd9Sstevel@tonic-gate static struct	stat status;
57*7c478bd9Sstevel@tonic-gate static char	*argv0, *argvk;
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate /* rmflg, when set it's ok to rm arvk file on caught signals */
60*7c478bd9Sstevel@tonic-gate static int	rmflg = 0;
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate #define	SUF0	'.'
63*7c478bd9Sstevel@tonic-gate #define	SUF1	'z'
64*7c478bd9Sstevel@tonic-gate #define	US	037
65*7c478bd9Sstevel@tonic-gate #define	RS	036
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate /* variables associated with i/o */
68*7c478bd9Sstevel@tonic-gate static char	filename[MAXPATHLEN];
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate static short	infile;
71*7c478bd9Sstevel@tonic-gate static short	outfile;
72*7c478bd9Sstevel@tonic-gate static short	inleft;
73*7c478bd9Sstevel@tonic-gate static short 	is_eof = 0;
74*7c478bd9Sstevel@tonic-gate static char	*inp;
75*7c478bd9Sstevel@tonic-gate static char	*outp;
76*7c478bd9Sstevel@tonic-gate static char	inbuff[BUFSIZ];
77*7c478bd9Sstevel@tonic-gate static char	outbuff[BUFSIZ];
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate /* the dictionary */
80*7c478bd9Sstevel@tonic-gate static long	origsize;
81*7c478bd9Sstevel@tonic-gate static short	maxlev;
82*7c478bd9Sstevel@tonic-gate static short	intnodes[25];
83*7c478bd9Sstevel@tonic-gate static char	*tree[25];
84*7c478bd9Sstevel@tonic-gate static char	characters[256];
85*7c478bd9Sstevel@tonic-gate static char	*eof;
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate static void putch(char c);
88*7c478bd9Sstevel@tonic-gate static int expand();
89*7c478bd9Sstevel@tonic-gate static int decode();
90*7c478bd9Sstevel@tonic-gate static int getwdsize();
91*7c478bd9Sstevel@tonic-gate static int getch();
92*7c478bd9Sstevel@tonic-gate static int getdict();
93*7c478bd9Sstevel@tonic-gate static int mv_xattrs();
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate /* read in the dictionary portion and build decoding structures */
96*7c478bd9Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */
97*7c478bd9Sstevel@tonic-gate int
98*7c478bd9Sstevel@tonic-gate getdict()
99*7c478bd9Sstevel@tonic-gate {
100*7c478bd9Sstevel@tonic-gate 	register int c, i, nchildren;
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	/*
103*7c478bd9Sstevel@tonic-gate 	 * check two-byte header
104*7c478bd9Sstevel@tonic-gate 	 * get size of original file,
105*7c478bd9Sstevel@tonic-gate 	 * get number of levels in maxlev,
106*7c478bd9Sstevel@tonic-gate 	 * get number of leaves on level i in intnodes[i],
107*7c478bd9Sstevel@tonic-gate 	 * set tree[i] to point to leaves for level i
108*7c478bd9Sstevel@tonic-gate 	 */
109*7c478bd9Sstevel@tonic-gate 	eof = &characters[0];
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	inbuff[6] = 25;
112*7c478bd9Sstevel@tonic-gate 	inleft = read(infile, &inbuff[0], BUFSIZ);
113*7c478bd9Sstevel@tonic-gate 	if (inleft < 0) {
114*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
115*7c478bd9Sstevel@tonic-gate 			"%s: %s: read error: "), argv0, filename);
116*7c478bd9Sstevel@tonic-gate 		perror("");
117*7c478bd9Sstevel@tonic-gate 		return (0);
118*7c478bd9Sstevel@tonic-gate 	}
119*7c478bd9Sstevel@tonic-gate 	if (inbuff[0] != US)
120*7c478bd9Sstevel@tonic-gate 		goto goof;
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 	if (inbuff[1] == US) {		/* oldstyle packing */
123*7c478bd9Sstevel@tonic-gate 		if (setjmp(env))
124*7c478bd9Sstevel@tonic-gate 			return (0);
125*7c478bd9Sstevel@tonic-gate 		return (expand());
126*7c478bd9Sstevel@tonic-gate 	}
127*7c478bd9Sstevel@tonic-gate 	if (inbuff[1] != RS)
128*7c478bd9Sstevel@tonic-gate 		goto goof;
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	inp = &inbuff[2];
131*7c478bd9Sstevel@tonic-gate 	origsize = 0;
132*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < 4; i++)
133*7c478bd9Sstevel@tonic-gate 		origsize = origsize*256 + ((*inp++) & 0377);
134*7c478bd9Sstevel@tonic-gate 	maxlev = *inp++ & 0377;
135*7c478bd9Sstevel@tonic-gate 	if (maxlev > 24) {
136*7c478bd9Sstevel@tonic-gate goof:		(void) fprintf(stderr, gettext(
137*7c478bd9Sstevel@tonic-gate 			"%s: %s: not in packed format\n"), argv0, filename);
138*7c478bd9Sstevel@tonic-gate 		return (0);
139*7c478bd9Sstevel@tonic-gate 	}
140*7c478bd9Sstevel@tonic-gate 	for (i = 1; i <= maxlev; i++)
141*7c478bd9Sstevel@tonic-gate 		intnodes[i] = *inp++ & 0377;
142*7c478bd9Sstevel@tonic-gate 	for (i = 1; i <= maxlev; i++) {
143*7c478bd9Sstevel@tonic-gate 		tree[i] = eof;
144*7c478bd9Sstevel@tonic-gate 		for (c = intnodes[i]; c > 0; c--) {
145*7c478bd9Sstevel@tonic-gate 			if (eof >= &characters[255])
146*7c478bd9Sstevel@tonic-gate 				goto goof;
147*7c478bd9Sstevel@tonic-gate 			*eof++ = *inp++;
148*7c478bd9Sstevel@tonic-gate 		}
149*7c478bd9Sstevel@tonic-gate 	}
150*7c478bd9Sstevel@tonic-gate 	*eof++ = *inp++;
151*7c478bd9Sstevel@tonic-gate 	intnodes[maxlev] += 2;
152*7c478bd9Sstevel@tonic-gate 	inleft -= inp - &inbuff[0];
153*7c478bd9Sstevel@tonic-gate 	if (inleft < 0)
154*7c478bd9Sstevel@tonic-gate 		goto goof;
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	/*
157*7c478bd9Sstevel@tonic-gate 	 * convert intnodes[i] to be number of
158*7c478bd9Sstevel@tonic-gate 	 * internal nodes possessed by level i
159*7c478bd9Sstevel@tonic-gate 	 */
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	nchildren = 0;
162*7c478bd9Sstevel@tonic-gate 	for (i = maxlev; i >= 1; i--) {
163*7c478bd9Sstevel@tonic-gate 		c = intnodes[i];
164*7c478bd9Sstevel@tonic-gate 		intnodes[i] = nchildren /= 2;
165*7c478bd9Sstevel@tonic-gate 		nchildren += c;
166*7c478bd9Sstevel@tonic-gate 	}
167*7c478bd9Sstevel@tonic-gate 	return (decode());
168*7c478bd9Sstevel@tonic-gate }
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate /* unpack the file */
171*7c478bd9Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */
172*7c478bd9Sstevel@tonic-gate int
173*7c478bd9Sstevel@tonic-gate decode()
174*7c478bd9Sstevel@tonic-gate {
175*7c478bd9Sstevel@tonic-gate 	register int bitsleft, c, i;
176*7c478bd9Sstevel@tonic-gate 	int j, lev, cont = 1;
177*7c478bd9Sstevel@tonic-gate 	char *p;
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	outp = &outbuff[0];
180*7c478bd9Sstevel@tonic-gate 	lev = 1;
181*7c478bd9Sstevel@tonic-gate 	i = 0;
182*7c478bd9Sstevel@tonic-gate 	while (cont) {
183*7c478bd9Sstevel@tonic-gate 		if (inleft <= 0) {
184*7c478bd9Sstevel@tonic-gate 			inleft = read(infile, inp = &inbuff[0], BUFSIZ);
185*7c478bd9Sstevel@tonic-gate 			if (inleft < 0) {
186*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
187*7c478bd9Sstevel@tonic-gate 					"%s: %s: read error: "),
188*7c478bd9Sstevel@tonic-gate 					argv0, filename);
189*7c478bd9Sstevel@tonic-gate 				perror("");
190*7c478bd9Sstevel@tonic-gate 				return (0);
191*7c478bd9Sstevel@tonic-gate 			}
192*7c478bd9Sstevel@tonic-gate 		}
193*7c478bd9Sstevel@tonic-gate 		if (--inleft < 0) {
194*7c478bd9Sstevel@tonic-gate uggh:			(void) fprintf(stderr, gettext(
195*7c478bd9Sstevel@tonic-gate 				"%s: %s: unpacking error\n"),
196*7c478bd9Sstevel@tonic-gate 				argv0, filename);
197*7c478bd9Sstevel@tonic-gate 			return (0);
198*7c478bd9Sstevel@tonic-gate 		}
199*7c478bd9Sstevel@tonic-gate 		c = *inp++;
200*7c478bd9Sstevel@tonic-gate 		bitsleft = 8;
201*7c478bd9Sstevel@tonic-gate 		while (--bitsleft >= 0) {
202*7c478bd9Sstevel@tonic-gate 			i *= 2;
203*7c478bd9Sstevel@tonic-gate 			if (c & 0200)
204*7c478bd9Sstevel@tonic-gate 				i++;
205*7c478bd9Sstevel@tonic-gate 			c <<= 1;
206*7c478bd9Sstevel@tonic-gate 			if ((j = i - intnodes[lev]) >= 0) {
207*7c478bd9Sstevel@tonic-gate 				p = &tree[lev][j];
208*7c478bd9Sstevel@tonic-gate 				if (p == eof) {
209*7c478bd9Sstevel@tonic-gate 					c = outp - &outbuff[0];
210*7c478bd9Sstevel@tonic-gate 				    if (write(outfile, &outbuff[0], c) != c) {
211*7c478bd9Sstevel@tonic-gate wrerr:						(void) fprintf(stderr, gettext(
212*7c478bd9Sstevel@tonic-gate 						"%s: %s: write error: "),
213*7c478bd9Sstevel@tonic-gate 							argv0, argvk);
214*7c478bd9Sstevel@tonic-gate 						perror("");
215*7c478bd9Sstevel@tonic-gate 						return (0);
216*7c478bd9Sstevel@tonic-gate 					}
217*7c478bd9Sstevel@tonic-gate 					origsize -= c;
218*7c478bd9Sstevel@tonic-gate 					if (origsize != 0)
219*7c478bd9Sstevel@tonic-gate 						goto uggh;
220*7c478bd9Sstevel@tonic-gate 					return (1);
221*7c478bd9Sstevel@tonic-gate 				}
222*7c478bd9Sstevel@tonic-gate 				*outp++ = *p;
223*7c478bd9Sstevel@tonic-gate 				if (outp == &outbuff[BUFSIZ]) {
224*7c478bd9Sstevel@tonic-gate 					if (write(outfile, outp = &outbuff[0],
225*7c478bd9Sstevel@tonic-gate 						    BUFSIZ) != BUFSIZ)
226*7c478bd9Sstevel@tonic-gate 						goto wrerr;
227*7c478bd9Sstevel@tonic-gate 					origsize -= BUFSIZ;
228*7c478bd9Sstevel@tonic-gate 				}
229*7c478bd9Sstevel@tonic-gate 				lev = 1;
230*7c478bd9Sstevel@tonic-gate 				i = 0;
231*7c478bd9Sstevel@tonic-gate 			} else
232*7c478bd9Sstevel@tonic-gate 				lev++;
233*7c478bd9Sstevel@tonic-gate 		}
234*7c478bd9Sstevel@tonic-gate 	}
235*7c478bd9Sstevel@tonic-gate 	return (1);	/* we won't get here , but lint is pleased */
236*7c478bd9Sstevel@tonic-gate }
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate int
239*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
240*7c478bd9Sstevel@tonic-gate {
241*7c478bd9Sstevel@tonic-gate 	extern int optind;
242*7c478bd9Sstevel@tonic-gate 	int i, k;
243*7c478bd9Sstevel@tonic-gate 	int sep, errflg = 0, pcat = 0;
244*7c478bd9Sstevel@tonic-gate 	register char *p1, *cp;
245*7c478bd9Sstevel@tonic-gate 	int fcount = 0;		/* failure count */
246*7c478bd9Sstevel@tonic-gate 	int max_name;
247*7c478bd9Sstevel@tonic-gate 	void onsig(int);
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 	if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
251*7c478bd9Sstevel@tonic-gate #ifdef __STDC__
252*7c478bd9Sstevel@tonic-gate 		signal((int)SIGHUP, onsig);
253*7c478bd9Sstevel@tonic-gate #else
254*7c478bd9Sstevel@tonic-gate 		signal((int)SIGHUP, onsig);
255*7c478bd9Sstevel@tonic-gate #endif
256*7c478bd9Sstevel@tonic-gate 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
257*7c478bd9Sstevel@tonic-gate #ifdef __STDC__
258*7c478bd9Sstevel@tonic-gate 		signal((int)SIGINT, onsig);
259*7c478bd9Sstevel@tonic-gate #else
260*7c478bd9Sstevel@tonic-gate 		signal((int)SIGINT, onsig);
261*7c478bd9Sstevel@tonic-gate #endif
262*7c478bd9Sstevel@tonic-gate 	if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
263*7c478bd9Sstevel@tonic-gate #ifdef __STDC__
264*7c478bd9Sstevel@tonic-gate 		signal((int)SIGTERM, onsig);
265*7c478bd9Sstevel@tonic-gate #else
266*7c478bd9Sstevel@tonic-gate 		signal(SIGTERM, onsig);
267*7c478bd9Sstevel@tonic-gate #endif
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
270*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
271*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
272*7c478bd9Sstevel@tonic-gate #endif
273*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate 	p1 = *argv;
276*7c478bd9Sstevel@tonic-gate 	while (*p1++);	/* Point p1 to end of argv[0] string */
277*7c478bd9Sstevel@tonic-gate 	while (--p1 >= *argv)
278*7c478bd9Sstevel@tonic-gate 		if (*p1 == '/')break;
279*7c478bd9Sstevel@tonic-gate 	*argv = p1 + 1;
280*7c478bd9Sstevel@tonic-gate 	argv0 = argv[0];
281*7c478bd9Sstevel@tonic-gate 	if (**argv == 'p')pcat++;	/* User entered pcat (or /xx/xx/pcat) */
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate 	while (getopt(argc, argv, "") != EOF)
284*7c478bd9Sstevel@tonic-gate 		++errflg;
285*7c478bd9Sstevel@tonic-gate 	/*
286*7c478bd9Sstevel@tonic-gate 	 * Check for invalid option.  Also check for missing
287*7c478bd9Sstevel@tonic-gate 	 * file operand, ie: "unpack" or "pcat".
288*7c478bd9Sstevel@tonic-gate 	 */
289*7c478bd9Sstevel@tonic-gate 	argc -= optind;
290*7c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
291*7c478bd9Sstevel@tonic-gate 	if (errflg || argc < 1) {
292*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("usage: %s file...\n"), argv0);
293*7c478bd9Sstevel@tonic-gate 		if (argc < 1) {
294*7c478bd9Sstevel@tonic-gate 			/*
295*7c478bd9Sstevel@tonic-gate 			 * return 1 for usage error when no file was specified
296*7c478bd9Sstevel@tonic-gate 			 */
297*7c478bd9Sstevel@tonic-gate 			return (1);
298*7c478bd9Sstevel@tonic-gate 		}
299*7c478bd9Sstevel@tonic-gate 	}
300*7c478bd9Sstevel@tonic-gate 	/* loop through the file names */
301*7c478bd9Sstevel@tonic-gate 	for (k = 0; k < argc; k++) {
302*7c478bd9Sstevel@tonic-gate 		fcount++;	/* expect the worst */
303*7c478bd9Sstevel@tonic-gate 		if (errflg) {
304*7c478bd9Sstevel@tonic-gate 			/*
305*7c478bd9Sstevel@tonic-gate 			 * invalid option; just count the number of files not
306*7c478bd9Sstevel@tonic-gate 			 * unpacked
307*7c478bd9Sstevel@tonic-gate 			 */
308*7c478bd9Sstevel@tonic-gate 			continue;
309*7c478bd9Sstevel@tonic-gate 		}
310*7c478bd9Sstevel@tonic-gate 		/* remove any .z suffix the user may have added */
311*7c478bd9Sstevel@tonic-gate 		for (cp = argv[k]; *cp != '\0'; ++cp)
312*7c478bd9Sstevel@tonic-gate 			;
313*7c478bd9Sstevel@tonic-gate 		if (cp[-1] == SUF1 && cp[-2] == SUF0) {
314*7c478bd9Sstevel@tonic-gate 			*cp-- = '\0'; *cp-- = '\0'; *cp = '\0';
315*7c478bd9Sstevel@tonic-gate 		}
316*7c478bd9Sstevel@tonic-gate 		sep = -1;
317*7c478bd9Sstevel@tonic-gate 		cp = filename;
318*7c478bd9Sstevel@tonic-gate 		argvk = argv[k];
319*7c478bd9Sstevel@tonic-gate 		/* copy argv[k] to filename and count chars in base name */
320*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < (MAXPATHLEN-3) && (*cp = argvk[i]); i++)
321*7c478bd9Sstevel@tonic-gate 			if (*cp++ == '/')
322*7c478bd9Sstevel@tonic-gate 				sep = i;
323*7c478bd9Sstevel@tonic-gate 		/* add .z suffix to filename */
324*7c478bd9Sstevel@tonic-gate 		*cp++ = SUF0;
325*7c478bd9Sstevel@tonic-gate 		*cp++ = SUF1;
326*7c478bd9Sstevel@tonic-gate 		*cp = '\0';
327*7c478bd9Sstevel@tonic-gate 		if ((infile = open(filename, O_RDONLY)) == -1) {
328*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
329*7c478bd9Sstevel@tonic-gate 				"%s: %s: cannot open: "),
330*7c478bd9Sstevel@tonic-gate 				argv0, filename);
331*7c478bd9Sstevel@tonic-gate 			perror("");
332*7c478bd9Sstevel@tonic-gate 			goto done;
333*7c478bd9Sstevel@tonic-gate 		}
334*7c478bd9Sstevel@tonic-gate 		if (pcat)
335*7c478bd9Sstevel@tonic-gate 			outfile = 1;	/* standard output */
336*7c478bd9Sstevel@tonic-gate 		else {
337*7c478bd9Sstevel@tonic-gate 			max_name = pathconf(filename, _PC_NAME_MAX);
338*7c478bd9Sstevel@tonic-gate 			if (max_name == -1) {
339*7c478bd9Sstevel@tonic-gate 				/* no limit on length of filename */
340*7c478bd9Sstevel@tonic-gate 				max_name = _POSIX_NAME_MAX;
341*7c478bd9Sstevel@tonic-gate 			}
342*7c478bd9Sstevel@tonic-gate 			if (i >= (MAXPATHLEN-1) || (i - sep - 1) > max_name) {
343*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
344*7c478bd9Sstevel@tonic-gate 					"%s: %s: file name too long\n"),
345*7c478bd9Sstevel@tonic-gate 					argv0, argvk);
346*7c478bd9Sstevel@tonic-gate 				goto done;
347*7c478bd9Sstevel@tonic-gate 			}
348*7c478bd9Sstevel@tonic-gate 			if (stat(argvk, &status) != -1) {
349*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
350*7c478bd9Sstevel@tonic-gate 					"%s: %s: already exists\n"),
351*7c478bd9Sstevel@tonic-gate 					argv0, argvk);
352*7c478bd9Sstevel@tonic-gate 				goto done;
353*7c478bd9Sstevel@tonic-gate 			}
354*7c478bd9Sstevel@tonic-gate 			(void) fstat(infile, &status);
355*7c478bd9Sstevel@tonic-gate 			if (status.st_nlink != 1) {
356*7c478bd9Sstevel@tonic-gate 				(void) printf(gettext(
357*7c478bd9Sstevel@tonic-gate 					"%s: %s: Warning: file has links\n"),
358*7c478bd9Sstevel@tonic-gate 					argv0, filename);
359*7c478bd9Sstevel@tonic-gate 			}
360*7c478bd9Sstevel@tonic-gate 			if ((outfile = creat(argvk, status.st_mode)) == -1) {
361*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
362*7c478bd9Sstevel@tonic-gate 					"%s: %s: cannot create: "),
363*7c478bd9Sstevel@tonic-gate 					argv0, argvk);
364*7c478bd9Sstevel@tonic-gate 				perror("");
365*7c478bd9Sstevel@tonic-gate 				goto done;
366*7c478bd9Sstevel@tonic-gate 			}
367*7c478bd9Sstevel@tonic-gate 			rmflg = 1;
368*7c478bd9Sstevel@tonic-gate 		}
369*7c478bd9Sstevel@tonic-gate 
370*7c478bd9Sstevel@tonic-gate 		if (getdict() &&	/* unpack */
371*7c478bd9Sstevel@tonic-gate 		    (pcat ||
372*7c478bd9Sstevel@tonic-gate 			(pathconf(filename, _PC_XATTR_EXISTS) != 1) ||
373*7c478bd9Sstevel@tonic-gate 				(mv_xattrs(infile, outfile,
374*7c478bd9Sstevel@tonic-gate 					filename, 0) == 0))) {
375*7c478bd9Sstevel@tonic-gate 			if (!pcat) {
376*7c478bd9Sstevel@tonic-gate 				/*
377*7c478bd9Sstevel@tonic-gate 				 * preserve acc & mod dates
378*7c478bd9Sstevel@tonic-gate 				 */
379*7c478bd9Sstevel@tonic-gate 				u_times.actime = status.st_atime;
380*7c478bd9Sstevel@tonic-gate 				u_times.modtime = status.st_mtime;
381*7c478bd9Sstevel@tonic-gate 				if (utime(argvk, &u_times) != 0) {
382*7c478bd9Sstevel@tonic-gate 					errflg++;
383*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
384*7c478bd9Sstevel@tonic-gate 					"%s: cannot change times on %s: "),
385*7c478bd9Sstevel@tonic-gate 						argv0, argvk);
386*7c478bd9Sstevel@tonic-gate 					perror("");
387*7c478bd9Sstevel@tonic-gate 				}
388*7c478bd9Sstevel@tonic-gate 				if (chmod(argvk, status.st_mode) != 0) {
389*7c478bd9Sstevel@tonic-gate 					errflg++;
390*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
391*7c478bd9Sstevel@tonic-gate 					"%s: cannot change mode to %o on %s: "),
392*7c478bd9Sstevel@tonic-gate 					    argv0, (uint_t)status.st_mode,
393*7c478bd9Sstevel@tonic-gate 					    argvk);
394*7c478bd9Sstevel@tonic-gate 					perror("");
395*7c478bd9Sstevel@tonic-gate 				}
396*7c478bd9Sstevel@tonic-gate 				(void) chown(argvk,
397*7c478bd9Sstevel@tonic-gate 						status.st_uid, status.st_gid);
398*7c478bd9Sstevel@tonic-gate 				rmflg = 0;
399*7c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%s: %s: unpacked\n"),
400*7c478bd9Sstevel@tonic-gate 					argv0, argvk);
401*7c478bd9Sstevel@tonic-gate 				(void) unlink(filename);
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate 			}
404*7c478bd9Sstevel@tonic-gate 			if (!errflg)
405*7c478bd9Sstevel@tonic-gate 				fcount--; 	/* success after all */
406*7c478bd9Sstevel@tonic-gate 		}
407*7c478bd9Sstevel@tonic-gate 		else
408*7c478bd9Sstevel@tonic-gate 			if (!pcat) {
409*7c478bd9Sstevel@tonic-gate 				if (pathconf(argvk, _PC_XATTR_EXISTS) == 1) {
410*7c478bd9Sstevel@tonic-gate 					(void) mv_xattrs(outfile, infile,
411*7c478bd9Sstevel@tonic-gate 						argvk, 1);
412*7c478bd9Sstevel@tonic-gate 				}
413*7c478bd9Sstevel@tonic-gate 				(void) unlink(argvk);
414*7c478bd9Sstevel@tonic-gate 			}
415*7c478bd9Sstevel@tonic-gate done:		(void) close(infile);
416*7c478bd9Sstevel@tonic-gate 		if (!pcat)
417*7c478bd9Sstevel@tonic-gate 			(void) close(outfile);
418*7c478bd9Sstevel@tonic-gate 	}
419*7c478bd9Sstevel@tonic-gate 	return (fcount);
420*7c478bd9Sstevel@tonic-gate }
421*7c478bd9Sstevel@tonic-gate 
422*7c478bd9Sstevel@tonic-gate /*
423*7c478bd9Sstevel@tonic-gate  * This code is for unpacking files that
424*7c478bd9Sstevel@tonic-gate  * were packed using the previous algorithm.
425*7c478bd9Sstevel@tonic-gate  */
426*7c478bd9Sstevel@tonic-gate 
427*7c478bd9Sstevel@tonic-gate static int	Tree[1024];
428*7c478bd9Sstevel@tonic-gate 
429*7c478bd9Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */
430*7c478bd9Sstevel@tonic-gate 
431*7c478bd9Sstevel@tonic-gate int
432*7c478bd9Sstevel@tonic-gate expand()
433*7c478bd9Sstevel@tonic-gate {
434*7c478bd9Sstevel@tonic-gate 	int tp, bit;
435*7c478bd9Sstevel@tonic-gate 	short word;
436*7c478bd9Sstevel@tonic-gate 	int keysize, i, *t;
437*7c478bd9Sstevel@tonic-gate 
438*7c478bd9Sstevel@tonic-gate 	outp = outbuff;
439*7c478bd9Sstevel@tonic-gate 	inp = &inbuff[2];
440*7c478bd9Sstevel@tonic-gate 	inleft -= 2;
441*7c478bd9Sstevel@tonic-gate 
442*7c478bd9Sstevel@tonic-gate 	origsize = ((long)(unsigned)getwdsize())*256*256;
443*7c478bd9Sstevel@tonic-gate 	origsize += (unsigned)getwdsize();
444*7c478bd9Sstevel@tonic-gate 	if (origsize == 0 || is_eof) {
445*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
446*7c478bd9Sstevel@tonic-gate 			"%s: %s: not in packed format\n"),
447*7c478bd9Sstevel@tonic-gate 			argv0, filename);
448*7c478bd9Sstevel@tonic-gate 		return (0);
449*7c478bd9Sstevel@tonic-gate 	}
450*7c478bd9Sstevel@tonic-gate 	t = Tree;
451*7c478bd9Sstevel@tonic-gate 	for (keysize = getwdsize(); keysize--; ) {
452*7c478bd9Sstevel@tonic-gate 		if ((i = getch()) == 0377)
453*7c478bd9Sstevel@tonic-gate 			*t++ = getwdsize();
454*7c478bd9Sstevel@tonic-gate 		else {
455*7c478bd9Sstevel@tonic-gate 				/*
456*7c478bd9Sstevel@tonic-gate 				 * reached EOF unexpectedly
457*7c478bd9Sstevel@tonic-gate 				 */
458*7c478bd9Sstevel@tonic-gate 			if (is_eof) {
459*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
460*7c478bd9Sstevel@tonic-gate 					"%s: %s: not in packed format\n"),
461*7c478bd9Sstevel@tonic-gate 					argv0, filename);
462*7c478bd9Sstevel@tonic-gate 				return (0);
463*7c478bd9Sstevel@tonic-gate 			}
464*7c478bd9Sstevel@tonic-gate 			*t++ = i & 0377;
465*7c478bd9Sstevel@tonic-gate 		}
466*7c478bd9Sstevel@tonic-gate 	}
467*7c478bd9Sstevel@tonic-gate 		/*
468*7c478bd9Sstevel@tonic-gate 		 * reached EOF unexpectedly
469*7c478bd9Sstevel@tonic-gate 		 */
470*7c478bd9Sstevel@tonic-gate 	if (is_eof) {
471*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
472*7c478bd9Sstevel@tonic-gate 			"%s: %s: not in packed format\n"),
473*7c478bd9Sstevel@tonic-gate 			argv0, filename);
474*7c478bd9Sstevel@tonic-gate 		return (0);
475*7c478bd9Sstevel@tonic-gate 	}
476*7c478bd9Sstevel@tonic-gate 
477*7c478bd9Sstevel@tonic-gate 
478*7c478bd9Sstevel@tonic-gate 	bit = tp = 0;
479*7c478bd9Sstevel@tonic-gate 	for (;;) {
480*7c478bd9Sstevel@tonic-gate 		if (bit <= 0) {
481*7c478bd9Sstevel@tonic-gate 			word = getwdsize();
482*7c478bd9Sstevel@tonic-gate 			/*
483*7c478bd9Sstevel@tonic-gate 			 * reached EOF unexpectedly
484*7c478bd9Sstevel@tonic-gate 			 */
485*7c478bd9Sstevel@tonic-gate 			if (word == 0 && is_eof && origsize > 0) {
486*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
487*7c478bd9Sstevel@tonic-gate 					"%s: %s: not in packed format\n"),
488*7c478bd9Sstevel@tonic-gate 					argv0, filename);
489*7c478bd9Sstevel@tonic-gate 				return (0);
490*7c478bd9Sstevel@tonic-gate 			}
491*7c478bd9Sstevel@tonic-gate 			bit = 16;
492*7c478bd9Sstevel@tonic-gate 		}
493*7c478bd9Sstevel@tonic-gate 		tp += Tree[tp + (word < 0)];
494*7c478bd9Sstevel@tonic-gate 		word <<= 1;
495*7c478bd9Sstevel@tonic-gate 		bit--;
496*7c478bd9Sstevel@tonic-gate 		if (Tree[tp] == 0) {
497*7c478bd9Sstevel@tonic-gate 			putch(Tree[tp+1]);
498*7c478bd9Sstevel@tonic-gate 			tp = 0;
499*7c478bd9Sstevel@tonic-gate 			if ((origsize -= 1) == 0) {
500*7c478bd9Sstevel@tonic-gate 				(void) write(outfile, outbuff, outp - outbuff);
501*7c478bd9Sstevel@tonic-gate 				return (1);
502*7c478bd9Sstevel@tonic-gate 			}
503*7c478bd9Sstevel@tonic-gate 		}
504*7c478bd9Sstevel@tonic-gate 	}
505*7c478bd9Sstevel@tonic-gate }
506*7c478bd9Sstevel@tonic-gate 
507*7c478bd9Sstevel@tonic-gate int
508*7c478bd9Sstevel@tonic-gate getch()
509*7c478bd9Sstevel@tonic-gate {
510*7c478bd9Sstevel@tonic-gate 	if (inleft <= 0) {
511*7c478bd9Sstevel@tonic-gate 		inleft = read(infile, inp = inbuff, BUFSIZ);
512*7c478bd9Sstevel@tonic-gate 		if (inleft < 0) {
513*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
514*7c478bd9Sstevel@tonic-gate 				"%s: %s: read error: "),
515*7c478bd9Sstevel@tonic-gate 				argv0, filename);
516*7c478bd9Sstevel@tonic-gate 			perror("");
517*7c478bd9Sstevel@tonic-gate 			longjmp(env, 1);
518*7c478bd9Sstevel@tonic-gate 		} else {		/* reached EOF, report it */
519*7c478bd9Sstevel@tonic-gate 			if (inleft == 0) {
520*7c478bd9Sstevel@tonic-gate 				is_eof = 1;
521*7c478bd9Sstevel@tonic-gate 				return (EOF);
522*7c478bd9Sstevel@tonic-gate 			}
523*7c478bd9Sstevel@tonic-gate 		}
524*7c478bd9Sstevel@tonic-gate 	}
525*7c478bd9Sstevel@tonic-gate 	inleft--;
526*7c478bd9Sstevel@tonic-gate 	return (*inp++ & 0377);
527*7c478bd9Sstevel@tonic-gate }
528*7c478bd9Sstevel@tonic-gate 
529*7c478bd9Sstevel@tonic-gate int
530*7c478bd9Sstevel@tonic-gate getwdsize()
531*7c478bd9Sstevel@tonic-gate {
532*7c478bd9Sstevel@tonic-gate 	char c;
533*7c478bd9Sstevel@tonic-gate 	int d;
534*7c478bd9Sstevel@tonic-gate 
535*7c478bd9Sstevel@tonic-gate 	c = getch();
536*7c478bd9Sstevel@tonic-gate 	d = getch();
537*7c478bd9Sstevel@tonic-gate 	if (is_eof)
538*7c478bd9Sstevel@tonic-gate 		return (0);
539*7c478bd9Sstevel@tonic-gate 	d <<= 8;
540*7c478bd9Sstevel@tonic-gate 	d |= c & 0377;
541*7c478bd9Sstevel@tonic-gate 	return (d);
542*7c478bd9Sstevel@tonic-gate }
543*7c478bd9Sstevel@tonic-gate 
544*7c478bd9Sstevel@tonic-gate void
545*7c478bd9Sstevel@tonic-gate onsig(int sig)
546*7c478bd9Sstevel@tonic-gate {
547*7c478bd9Sstevel@tonic-gate 				/* could be running as unpack or pcat	*/
548*7c478bd9Sstevel@tonic-gate 				/* but rmflg is set only when running	*/
549*7c478bd9Sstevel@tonic-gate 				/* as unpack and only when file is	*/
550*7c478bd9Sstevel@tonic-gate 				/* created by unpack and not yet done	*/
551*7c478bd9Sstevel@tonic-gate 	if (rmflg == 1)
552*7c478bd9Sstevel@tonic-gate 		(void) unlink(argvk);
553*7c478bd9Sstevel@tonic-gate 	exit(1);
554*7c478bd9Sstevel@tonic-gate }
555*7c478bd9Sstevel@tonic-gate 
556*7c478bd9Sstevel@tonic-gate void
557*7c478bd9Sstevel@tonic-gate putch(char c)
558*7c478bd9Sstevel@tonic-gate {
559*7c478bd9Sstevel@tonic-gate 	int n;
560*7c478bd9Sstevel@tonic-gate 
561*7c478bd9Sstevel@tonic-gate 	*outp++ = c;
562*7c478bd9Sstevel@tonic-gate 	if (outp == &outbuff[BUFSIZ]) {
563*7c478bd9Sstevel@tonic-gate 		n = write(outfile, outp = outbuff, BUFSIZ);
564*7c478bd9Sstevel@tonic-gate 		if (n < BUFSIZ) {
565*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
566*7c478bd9Sstevel@tonic-gate 				"%s: %s: write error: "),
567*7c478bd9Sstevel@tonic-gate 				argv0, argvk);
568*7c478bd9Sstevel@tonic-gate 			perror("");
569*7c478bd9Sstevel@tonic-gate 			longjmp(env, 2);
570*7c478bd9Sstevel@tonic-gate 		}
571*7c478bd9Sstevel@tonic-gate 	}
572*7c478bd9Sstevel@tonic-gate }
573*7c478bd9Sstevel@tonic-gate 
574*7c478bd9Sstevel@tonic-gate /*
575*7c478bd9Sstevel@tonic-gate  * mv_xattrs - move (via renameat) all of the extended attributes
576*7c478bd9Sstevel@tonic-gate  *	associated with the file referenced by infd to the file
577*7c478bd9Sstevel@tonic-gate  *	referenced by outfd.  The infile and silent arguments are
578*7c478bd9Sstevel@tonic-gate  *	provided for error message processing.  This function
579*7c478bd9Sstevel@tonic-gate  *	returns 0 on success and -1 on error.
580*7c478bd9Sstevel@tonic-gate  */
581*7c478bd9Sstevel@tonic-gate static int
582*7c478bd9Sstevel@tonic-gate mv_xattrs(int infd, int outfd, char *infile, int silent)
583*7c478bd9Sstevel@tonic-gate {
584*7c478bd9Sstevel@tonic-gate 	int indfd, outdfd, tmpfd;
585*7c478bd9Sstevel@tonic-gate 	DIR *dirp = NULL;
586*7c478bd9Sstevel@tonic-gate 	struct dirent *dp = NULL;
587*7c478bd9Sstevel@tonic-gate 	int error = 0;
588*7c478bd9Sstevel@tonic-gate 	char *etext;
589*7c478bd9Sstevel@tonic-gate 
590*7c478bd9Sstevel@tonic-gate 	indfd = outdfd = tmpfd = -1;
591*7c478bd9Sstevel@tonic-gate 
592*7c478bd9Sstevel@tonic-gate 	if ((indfd = openat(infd, ".", O_RDONLY|O_XATTR)) == -1) {
593*7c478bd9Sstevel@tonic-gate 		etext = gettext("cannot open source");
594*7c478bd9Sstevel@tonic-gate 		error = -1;
595*7c478bd9Sstevel@tonic-gate 		goto out;
596*7c478bd9Sstevel@tonic-gate 	}
597*7c478bd9Sstevel@tonic-gate 
598*7c478bd9Sstevel@tonic-gate 	if ((outdfd = openat(outfd, ".", O_RDONLY|O_XATTR)) == -1) {
599*7c478bd9Sstevel@tonic-gate 		etext = gettext("cannot open target");
600*7c478bd9Sstevel@tonic-gate 		error = -1;
601*7c478bd9Sstevel@tonic-gate 		goto out;
602*7c478bd9Sstevel@tonic-gate 	}
603*7c478bd9Sstevel@tonic-gate 
604*7c478bd9Sstevel@tonic-gate 	if ((tmpfd = dup(indfd)) == -1) {
605*7c478bd9Sstevel@tonic-gate 		etext = gettext("cannot dup descriptor");
606*7c478bd9Sstevel@tonic-gate 		error = -1;
607*7c478bd9Sstevel@tonic-gate 		goto out;
608*7c478bd9Sstevel@tonic-gate 
609*7c478bd9Sstevel@tonic-gate 	}
610*7c478bd9Sstevel@tonic-gate 	if ((dirp = fdopendir(tmpfd)) == NULL) {
611*7c478bd9Sstevel@tonic-gate 		etext = gettext("cannot access source");
612*7c478bd9Sstevel@tonic-gate 		error = -1;
613*7c478bd9Sstevel@tonic-gate 		goto out;
614*7c478bd9Sstevel@tonic-gate 	}
615*7c478bd9Sstevel@tonic-gate 
616*7c478bd9Sstevel@tonic-gate 	while (dp = readdir(dirp)) {
617*7c478bd9Sstevel@tonic-gate 		if ((dp->d_name[0] == '.' && dp->d_name[1] == '\0') ||
618*7c478bd9Sstevel@tonic-gate 		    (dp->d_name[0] == '.' && dp->d_name[1] == '.' &&
619*7c478bd9Sstevel@tonic-gate 		    dp->d_name[2] == '\0'))
620*7c478bd9Sstevel@tonic-gate 			continue;
621*7c478bd9Sstevel@tonic-gate 		if ((renameat(indfd, dp->d_name, outdfd, dp->d_name)) == -1) {
622*7c478bd9Sstevel@tonic-gate 			etext = dp->d_name;
623*7c478bd9Sstevel@tonic-gate 			error = -1;
624*7c478bd9Sstevel@tonic-gate 			goto out;
625*7c478bd9Sstevel@tonic-gate 		}
626*7c478bd9Sstevel@tonic-gate 	}
627*7c478bd9Sstevel@tonic-gate out:
628*7c478bd9Sstevel@tonic-gate 	if (error == -1 && silent == 0) {
629*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(
630*7c478bd9Sstevel@tonic-gate 			"unpack: %s: cannot move extended attributes, "),
631*7c478bd9Sstevel@tonic-gate 			infile);
632*7c478bd9Sstevel@tonic-gate 		perror(etext);
633*7c478bd9Sstevel@tonic-gate 	}
634*7c478bd9Sstevel@tonic-gate 	if (dirp)
635*7c478bd9Sstevel@tonic-gate 		closedir(dirp);
636*7c478bd9Sstevel@tonic-gate 	if (indfd != -1)
637*7c478bd9Sstevel@tonic-gate 		close(indfd);
638*7c478bd9Sstevel@tonic-gate 	if (outdfd != -1)
639*7c478bd9Sstevel@tonic-gate 		close(outdfd);
640*7c478bd9Sstevel@tonic-gate 	return (error);
641*7c478bd9Sstevel@tonic-gate }
642