xref: /illumos-gate/usr/src/cmd/unpack/unpack.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5f0a2d94fSmarks  * Common Development and Distribution License (the "License").
6f0a2d94fSmarks  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
227c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
26*da6c28aaSamw  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
277c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  *	Huffman decompressor
327c478bd9Sstevel@tonic-gate  *	Usage:	pcat filename...
337c478bd9Sstevel@tonic-gate  *	or	unpack filename...
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <setjmp.h>
377c478bd9Sstevel@tonic-gate #include <signal.h>
387c478bd9Sstevel@tonic-gate #include <locale.h>
397c478bd9Sstevel@tonic-gate #include <utime.h>
407c478bd9Sstevel@tonic-gate #include <sys/param.h>
41fa9e4066Sahrens #include <sys/acl.h>
42fa9e4066Sahrens #include <aclutils.h>
43*da6c28aaSamw #include <libcmdutils.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static struct utimbuf u_times;
467c478bd9Sstevel@tonic-gate static jmp_buf env;
477c478bd9Sstevel@tonic-gate static struct	stat status;
487c478bd9Sstevel@tonic-gate static char	*argv0, *argvk;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /* rmflg, when set it's ok to rm arvk file on caught signals */
517c478bd9Sstevel@tonic-gate static int	rmflg = 0;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define	SUF0	'.'
547c478bd9Sstevel@tonic-gate #define	SUF1	'z'
557c478bd9Sstevel@tonic-gate #define	US	037
567c478bd9Sstevel@tonic-gate #define	RS	036
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /* variables associated with i/o */
597c478bd9Sstevel@tonic-gate static char	filename[MAXPATHLEN];
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate static short	infile;
627c478bd9Sstevel@tonic-gate static short	outfile;
637c478bd9Sstevel@tonic-gate static short	inleft;
647c478bd9Sstevel@tonic-gate static short 	is_eof = 0;
657c478bd9Sstevel@tonic-gate static char	*inp;
667c478bd9Sstevel@tonic-gate static char	*outp;
677c478bd9Sstevel@tonic-gate static char	inbuff[BUFSIZ];
687c478bd9Sstevel@tonic-gate static char	outbuff[BUFSIZ];
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /* the dictionary */
717c478bd9Sstevel@tonic-gate static long	origsize;
727c478bd9Sstevel@tonic-gate static short	maxlev;
737c478bd9Sstevel@tonic-gate static short	intnodes[25];
747c478bd9Sstevel@tonic-gate static char	*tree[25];
757c478bd9Sstevel@tonic-gate static char	characters[256];
767c478bd9Sstevel@tonic-gate static char	*eof;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate static void putch(char c);
797c478bd9Sstevel@tonic-gate static int expand();
807c478bd9Sstevel@tonic-gate static int decode();
817c478bd9Sstevel@tonic-gate static int getwdsize();
827c478bd9Sstevel@tonic-gate static int getch();
837c478bd9Sstevel@tonic-gate static int getdict();
84*da6c28aaSamw 
85*da6c28aaSamw /* Extended system attribute support */
86*da6c28aaSamw 
87*da6c28aaSamw static int saflg = 0;
88*da6c28aaSamw 
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /* read in the dictionary portion and build decoding structures */
917c478bd9Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */
927c478bd9Sstevel@tonic-gate int
getdict()937c478bd9Sstevel@tonic-gate getdict()
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	register int c, i, nchildren;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	/*
987c478bd9Sstevel@tonic-gate 	 * check two-byte header
997c478bd9Sstevel@tonic-gate 	 * get size of original file,
1007c478bd9Sstevel@tonic-gate 	 * get number of levels in maxlev,
1017c478bd9Sstevel@tonic-gate 	 * get number of leaves on level i in intnodes[i],
1027c478bd9Sstevel@tonic-gate 	 * set tree[i] to point to leaves for level i
1037c478bd9Sstevel@tonic-gate 	 */
1047c478bd9Sstevel@tonic-gate 	eof = &characters[0];
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	inbuff[6] = 25;
1077c478bd9Sstevel@tonic-gate 	inleft = read(infile, &inbuff[0], BUFSIZ);
1087c478bd9Sstevel@tonic-gate 	if (inleft < 0) {
1097c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
110*da6c28aaSamw 		    "%s: %s: read error: "), argv0, filename);
1117c478bd9Sstevel@tonic-gate 		perror("");
1127c478bd9Sstevel@tonic-gate 		return (0);
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 	if (inbuff[0] != US)
1157c478bd9Sstevel@tonic-gate 		goto goof;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if (inbuff[1] == US) {		/* oldstyle packing */
1187c478bd9Sstevel@tonic-gate 		if (setjmp(env))
1197c478bd9Sstevel@tonic-gate 			return (0);
1207c478bd9Sstevel@tonic-gate 		return (expand());
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 	if (inbuff[1] != RS)
1237c478bd9Sstevel@tonic-gate 		goto goof;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	inp = &inbuff[2];
1267c478bd9Sstevel@tonic-gate 	origsize = 0;
1277c478bd9Sstevel@tonic-gate 	for (i = 0; i < 4; i++)
1287c478bd9Sstevel@tonic-gate 		origsize = origsize*256 + ((*inp++) & 0377);
1297c478bd9Sstevel@tonic-gate 	maxlev = *inp++ & 0377;
1307c478bd9Sstevel@tonic-gate 	if (maxlev > 24) {
1317c478bd9Sstevel@tonic-gate goof:		(void) fprintf(stderr, gettext(
132*da6c28aaSamw 		    "%s: %s: not in packed format\n"), argv0, filename);
1337c478bd9Sstevel@tonic-gate 		return (0);
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 	for (i = 1; i <= maxlev; i++)
1367c478bd9Sstevel@tonic-gate 		intnodes[i] = *inp++ & 0377;
1377c478bd9Sstevel@tonic-gate 	for (i = 1; i <= maxlev; i++) {
1387c478bd9Sstevel@tonic-gate 		tree[i] = eof;
1397c478bd9Sstevel@tonic-gate 		for (c = intnodes[i]; c > 0; c--) {
1407c478bd9Sstevel@tonic-gate 			if (eof >= &characters[255])
1417c478bd9Sstevel@tonic-gate 				goto goof;
1427c478bd9Sstevel@tonic-gate 			*eof++ = *inp++;
1437c478bd9Sstevel@tonic-gate 		}
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 	*eof++ = *inp++;
1467c478bd9Sstevel@tonic-gate 	intnodes[maxlev] += 2;
1477c478bd9Sstevel@tonic-gate 	inleft -= inp - &inbuff[0];
1487c478bd9Sstevel@tonic-gate 	if (inleft < 0)
1497c478bd9Sstevel@tonic-gate 		goto goof;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	/*
1527c478bd9Sstevel@tonic-gate 	 * convert intnodes[i] to be number of
1537c478bd9Sstevel@tonic-gate 	 * internal nodes possessed by level i
1547c478bd9Sstevel@tonic-gate 	 */
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	nchildren = 0;
1577c478bd9Sstevel@tonic-gate 	for (i = maxlev; i >= 1; i--) {
1587c478bd9Sstevel@tonic-gate 		c = intnodes[i];
1597c478bd9Sstevel@tonic-gate 		intnodes[i] = nchildren /= 2;
1607c478bd9Sstevel@tonic-gate 		nchildren += c;
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 	return (decode());
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /* unpack the file */
1667c478bd9Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */
1677c478bd9Sstevel@tonic-gate int
decode()1687c478bd9Sstevel@tonic-gate decode()
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	register int bitsleft, c, i;
1717c478bd9Sstevel@tonic-gate 	int j, lev, cont = 1;
1727c478bd9Sstevel@tonic-gate 	char *p;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	outp = &outbuff[0];
1757c478bd9Sstevel@tonic-gate 	lev = 1;
1767c478bd9Sstevel@tonic-gate 	i = 0;
1777c478bd9Sstevel@tonic-gate 	while (cont) {
1787c478bd9Sstevel@tonic-gate 		if (inleft <= 0) {
1797c478bd9Sstevel@tonic-gate 			inleft = read(infile, inp = &inbuff[0], BUFSIZ);
1807c478bd9Sstevel@tonic-gate 			if (inleft < 0) {
1817c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
182*da6c28aaSamw 				    "%s: %s: read error: "),
183*da6c28aaSamw 				    argv0, filename);
1847c478bd9Sstevel@tonic-gate 				perror("");
1857c478bd9Sstevel@tonic-gate 				return (0);
1867c478bd9Sstevel@tonic-gate 			}
1877c478bd9Sstevel@tonic-gate 		}
1887c478bd9Sstevel@tonic-gate 		if (--inleft < 0) {
1897c478bd9Sstevel@tonic-gate uggh:			(void) fprintf(stderr, gettext(
190*da6c28aaSamw 			    "%s: %s: unpacking error\n"),
191*da6c28aaSamw 			    argv0, filename);
1927c478bd9Sstevel@tonic-gate 			return (0);
1937c478bd9Sstevel@tonic-gate 		}
1947c478bd9Sstevel@tonic-gate 		c = *inp++;
1957c478bd9Sstevel@tonic-gate 		bitsleft = 8;
1967c478bd9Sstevel@tonic-gate 		while (--bitsleft >= 0) {
1977c478bd9Sstevel@tonic-gate 			i *= 2;
1987c478bd9Sstevel@tonic-gate 			if (c & 0200)
1997c478bd9Sstevel@tonic-gate 				i++;
2007c478bd9Sstevel@tonic-gate 			c <<= 1;
2017c478bd9Sstevel@tonic-gate 			if ((j = i - intnodes[lev]) >= 0) {
2027c478bd9Sstevel@tonic-gate 				p = &tree[lev][j];
2037c478bd9Sstevel@tonic-gate 				if (p == eof) {
2047c478bd9Sstevel@tonic-gate 					c = outp - &outbuff[0];
205*da6c28aaSamw 					if (write(outfile, &outbuff[0], c)
206*da6c28aaSamw 					    != c) {
207*da6c28aaSamw wrerr:						(void) fprintf(stderr,
208*da6c28aaSamw 						    gettext(
209*da6c28aaSamw 						    "%s: %s: write error: "),
210*da6c28aaSamw 						    argv0, argvk);
2117c478bd9Sstevel@tonic-gate 						perror("");
2127c478bd9Sstevel@tonic-gate 						return (0);
2137c478bd9Sstevel@tonic-gate 					}
2147c478bd9Sstevel@tonic-gate 					origsize -= c;
2157c478bd9Sstevel@tonic-gate 					if (origsize != 0)
2167c478bd9Sstevel@tonic-gate 						goto uggh;
2177c478bd9Sstevel@tonic-gate 					return (1);
2187c478bd9Sstevel@tonic-gate 				}
2197c478bd9Sstevel@tonic-gate 				*outp++ = *p;
2207c478bd9Sstevel@tonic-gate 				if (outp == &outbuff[BUFSIZ]) {
2217c478bd9Sstevel@tonic-gate 					if (write(outfile, outp = &outbuff[0],
222*da6c28aaSamw 					    BUFSIZ) != BUFSIZ)
2237c478bd9Sstevel@tonic-gate 						goto wrerr;
2247c478bd9Sstevel@tonic-gate 					origsize -= BUFSIZ;
2257c478bd9Sstevel@tonic-gate 				}
2267c478bd9Sstevel@tonic-gate 				lev = 1;
2277c478bd9Sstevel@tonic-gate 				i = 0;
2287c478bd9Sstevel@tonic-gate 			} else
2297c478bd9Sstevel@tonic-gate 				lev++;
2307c478bd9Sstevel@tonic-gate 		}
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate 	return (1);	/* we won't get here , but lint is pleased */
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])2367c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	extern int optind;
2397c478bd9Sstevel@tonic-gate 	int i, k;
240fa9e4066Sahrens 	int error;
2417c478bd9Sstevel@tonic-gate 	int sep, errflg = 0, pcat = 0;
2427c478bd9Sstevel@tonic-gate 	register char *p1, *cp;
2437c478bd9Sstevel@tonic-gate 	int fcount = 0;		/* failure count */
2447c478bd9Sstevel@tonic-gate 	int max_name;
2457c478bd9Sstevel@tonic-gate 	void onsig(int);
246f0a2d94fSmarks 	acl_t *aclp = NULL;
247*da6c28aaSamw 	int c;
248*da6c28aaSamw 	char *progname;
249*da6c28aaSamw 	int sattr_exist = 0;
250*da6c28aaSamw 	int xattr_exist = 0;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
2537c478bd9Sstevel@tonic-gate #ifdef __STDC__
254*da6c28aaSamw 		(void) signal((int)SIGHUP, onsig);
2557c478bd9Sstevel@tonic-gate #else
256*da6c28aaSamw 		(void) signal((int)SIGHUP, onsig);
2577c478bd9Sstevel@tonic-gate #endif
2587c478bd9Sstevel@tonic-gate 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
2597c478bd9Sstevel@tonic-gate #ifdef __STDC__
260*da6c28aaSamw 		(void) signal((int)SIGINT, onsig);
2617c478bd9Sstevel@tonic-gate #else
262*da6c28aaSamw 		(void) signal((int)SIGINT, onsig);
2637c478bd9Sstevel@tonic-gate #endif
2647c478bd9Sstevel@tonic-gate 	if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
2657c478bd9Sstevel@tonic-gate #ifdef __STDC__
266*da6c28aaSamw 		(void) signal((int)SIGTERM, onsig);
2677c478bd9Sstevel@tonic-gate #else
268*da6c28aaSamw 		(void) signal(SIGTERM, onsig);
2697c478bd9Sstevel@tonic-gate #endif
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2727c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
2737c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
2747c478bd9Sstevel@tonic-gate #endif
2757c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2767c478bd9Sstevel@tonic-gate 
277*da6c28aaSamw 	if (progname = strrchr(argv[0], '/'))
278*da6c28aaSamw 		++progname;
279*da6c28aaSamw 	else
280*da6c28aaSamw 		progname = argv[0];
281*da6c28aaSamw 
2827c478bd9Sstevel@tonic-gate 	p1 = *argv;
283*da6c28aaSamw 	while (*p1++) { };	/* Point p1 to end of argv[0] string */
2847c478bd9Sstevel@tonic-gate 	while (--p1 >= *argv)
2857c478bd9Sstevel@tonic-gate 		if (*p1 == '/')break;
2867c478bd9Sstevel@tonic-gate 	*argv = p1 + 1;
2877c478bd9Sstevel@tonic-gate 	argv0 = argv[0];
2887c478bd9Sstevel@tonic-gate 	if (**argv == 'p')pcat++;	/* User entered pcat (or /xx/xx/pcat) */
2897c478bd9Sstevel@tonic-gate 
290*da6c28aaSamw 	while ((c = getopt(argc, argv, "/")) != EOF) {
291*da6c28aaSamw 		if (c == '/') {
292*da6c28aaSamw 			if (pcat)
293*da6c28aaSamw 				++errflg;
294*da6c28aaSamw 			else
295*da6c28aaSamw 				saflg++;
296*da6c28aaSamw 		} else
297*da6c28aaSamw 			++errflg;
298*da6c28aaSamw 	}
2997c478bd9Sstevel@tonic-gate 	/*
3007c478bd9Sstevel@tonic-gate 	 * Check for invalid option.  Also check for missing
3017c478bd9Sstevel@tonic-gate 	 * file operand, ie: "unpack" or "pcat".
3027c478bd9Sstevel@tonic-gate 	 */
3037c478bd9Sstevel@tonic-gate 	argc -= optind;
3047c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
3057c478bd9Sstevel@tonic-gate 	if (errflg || argc < 1) {
306*da6c28aaSamw 		if (!pcat)
307*da6c28aaSamw 			(void) fprintf(stderr,
308*da6c28aaSamw 			    gettext("usage: %s [-/] file...\n"), argv0);
309*da6c28aaSamw 		else
310*da6c28aaSamw 			(void) fprintf(stderr, gettext("usage: %s file...\n"),
311*da6c28aaSamw 			    argv0);
312*da6c28aaSamw 
3137c478bd9Sstevel@tonic-gate 		if (argc < 1) {
3147c478bd9Sstevel@tonic-gate 			/*
3157c478bd9Sstevel@tonic-gate 			 * return 1 for usage error when no file was specified
3167c478bd9Sstevel@tonic-gate 			 */
3177c478bd9Sstevel@tonic-gate 			return (1);
3187c478bd9Sstevel@tonic-gate 		}
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate 	/* loop through the file names */
3217c478bd9Sstevel@tonic-gate 	for (k = 0; k < argc; k++) {
3227c478bd9Sstevel@tonic-gate 		fcount++;	/* expect the worst */
3237c478bd9Sstevel@tonic-gate 		if (errflg) {
3247c478bd9Sstevel@tonic-gate 			/*
3257c478bd9Sstevel@tonic-gate 			 * invalid option; just count the number of files not
3267c478bd9Sstevel@tonic-gate 			 * unpacked
3277c478bd9Sstevel@tonic-gate 			 */
3287c478bd9Sstevel@tonic-gate 			continue;
3297c478bd9Sstevel@tonic-gate 		}
3307c478bd9Sstevel@tonic-gate 		/* remove any .z suffix the user may have added */
3317c478bd9Sstevel@tonic-gate 		for (cp = argv[k]; *cp != '\0'; ++cp)
3327c478bd9Sstevel@tonic-gate 			;
3337c478bd9Sstevel@tonic-gate 		if (cp[-1] == SUF1 && cp[-2] == SUF0) {
3347c478bd9Sstevel@tonic-gate 			*cp-- = '\0'; *cp-- = '\0'; *cp = '\0';
3357c478bd9Sstevel@tonic-gate 		}
3367c478bd9Sstevel@tonic-gate 		sep = -1;
3377c478bd9Sstevel@tonic-gate 		cp = filename;
3387c478bd9Sstevel@tonic-gate 		argvk = argv[k];
3397c478bd9Sstevel@tonic-gate 		/* copy argv[k] to filename and count chars in base name */
3407c478bd9Sstevel@tonic-gate 		for (i = 0; i < (MAXPATHLEN-3) && (*cp = argvk[i]); i++)
3417c478bd9Sstevel@tonic-gate 			if (*cp++ == '/')
3427c478bd9Sstevel@tonic-gate 				sep = i;
3437c478bd9Sstevel@tonic-gate 		/* add .z suffix to filename */
3447c478bd9Sstevel@tonic-gate 		*cp++ = SUF0;
3457c478bd9Sstevel@tonic-gate 		*cp++ = SUF1;
3467c478bd9Sstevel@tonic-gate 		*cp = '\0';
3477c478bd9Sstevel@tonic-gate 		if ((infile = open(filename, O_RDONLY)) == -1) {
3487c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
349*da6c28aaSamw 			    "%s: %s: cannot open: "),
350*da6c28aaSamw 			    argv0, filename);
3517c478bd9Sstevel@tonic-gate 			perror("");
3527c478bd9Sstevel@tonic-gate 			goto done;
3537c478bd9Sstevel@tonic-gate 		}
3547c478bd9Sstevel@tonic-gate 		if (pcat)
3557c478bd9Sstevel@tonic-gate 			outfile = 1;	/* standard output */
3567c478bd9Sstevel@tonic-gate 		else {
357fa9e4066Sahrens 
358fa9e4066Sahrens 			error = facl_get(infile, ACL_NO_TRIVIAL, &aclp);
359fa9e4066Sahrens 			if (error != 0) {
360fa9e4066Sahrens 				(void) printf(gettext(
361fa9e4066Sahrens 				    "%s: %s: cannot retrieve ACL : %s\n"),
362*da6c28aaSamw 				    argv0, filename, acl_strerror(error));
363fa9e4066Sahrens 			}
364fa9e4066Sahrens 
3657c478bd9Sstevel@tonic-gate 			max_name = pathconf(filename, _PC_NAME_MAX);
3667c478bd9Sstevel@tonic-gate 			if (max_name == -1) {
3677c478bd9Sstevel@tonic-gate 				/* no limit on length of filename */
3687c478bd9Sstevel@tonic-gate 				max_name = _POSIX_NAME_MAX;
3697c478bd9Sstevel@tonic-gate 			}
3707c478bd9Sstevel@tonic-gate 			if (i >= (MAXPATHLEN-1) || (i - sep - 1) > max_name) {
3717c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
372*da6c28aaSamw 				    "%s: %s: file name too long\n"),
373*da6c28aaSamw 				    argv0, argvk);
3747c478bd9Sstevel@tonic-gate 				goto done;
3757c478bd9Sstevel@tonic-gate 			}
3767c478bd9Sstevel@tonic-gate 			if (stat(argvk, &status) != -1) {
3777c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
378*da6c28aaSamw 				    "%s: %s: already exists\n"),
379*da6c28aaSamw 				    argv0, argvk);
3807c478bd9Sstevel@tonic-gate 				goto done;
3817c478bd9Sstevel@tonic-gate 			}
3827c478bd9Sstevel@tonic-gate 			(void) fstat(infile, &status);
3837c478bd9Sstevel@tonic-gate 			if (status.st_nlink != 1) {
3847c478bd9Sstevel@tonic-gate 				(void) printf(gettext(
385*da6c28aaSamw 				    "%s: %s: Warning: file has links\n"),
386*da6c28aaSamw 				    argv0, filename);
3877c478bd9Sstevel@tonic-gate 			}
3887c478bd9Sstevel@tonic-gate 			if ((outfile = creat(argvk, status.st_mode)) == -1) {
3897c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
390*da6c28aaSamw 				    "%s: %s: cannot create: "),
391*da6c28aaSamw 				    argv0, argvk);
3927c478bd9Sstevel@tonic-gate 				perror("");
3937c478bd9Sstevel@tonic-gate 				goto done;
3947c478bd9Sstevel@tonic-gate 			}
3957c478bd9Sstevel@tonic-gate 			rmflg = 1;
3967c478bd9Sstevel@tonic-gate 		}
3977c478bd9Sstevel@tonic-gate 
398*da6c28aaSamw 		if (getdict()) { 	/* unpack */
399*da6c28aaSamw 			if (pathconf(filename, _PC_XATTR_EXISTS) == 1)
400*da6c28aaSamw 				xattr_exist = 1;
401*da6c28aaSamw 			if (saflg && sysattr_support(filename,
402*da6c28aaSamw 			    _PC_SATTR_EXISTS) == 1)
403*da6c28aaSamw 				sattr_exist = 1;
404*da6c28aaSamw 			if (pcat || xattr_exist || sattr_exist) {
405*da6c28aaSamw 				if (mv_xattrs(progname, filename, argv[k],
406*da6c28aaSamw 				    sattr_exist, 0)
407*da6c28aaSamw 				    != 0) {
408*da6c28aaSamw 					/* Move attributes back ... */
409*da6c28aaSamw 					xattr_exist = 0;
410*da6c28aaSamw 					sattr_exist = 0;
411*da6c28aaSamw 					if (pathconf(argvk, _PC_XATTR_EXISTS)
412*da6c28aaSamw 					    == 1)
413*da6c28aaSamw 						xattr_exist = 1;
414*da6c28aaSamw 					if (saflg && sysattr_support(argvk,
415*da6c28aaSamw 					    _PC_SATTR_EXISTS) == 1)
416*da6c28aaSamw 						sattr_exist = 1;
417*da6c28aaSamw 					if (!pcat && (xattr_exist ||
418*da6c28aaSamw 					    sattr_exist)) {
419*da6c28aaSamw 						(void) mv_xattrs(progname,
420*da6c28aaSamw 						    argv[k], filename,
421*da6c28aaSamw 						    sattr_exist, 1);
422*da6c28aaSamw 						(void) unlink(argvk);
423*da6c28aaSamw 						goto done;
424*da6c28aaSamw 					}
425*da6c28aaSamw 				} else {
426*da6c28aaSamw 					if (!pcat)
427*da6c28aaSamw 						(void) unlink(filename);
428*da6c28aaSamw 				}
429*da6c28aaSamw 			} else if (!pcat)
430*da6c28aaSamw 				(void) unlink(filename);
431*da6c28aaSamw 
4327c478bd9Sstevel@tonic-gate 			if (!pcat) {
433*da6c28aaSamw 				(void) printf(gettext("%s: %s: unpacked\n"),
434*da6c28aaSamw 				    argv0, argvk);
4357c478bd9Sstevel@tonic-gate 				/*
4367c478bd9Sstevel@tonic-gate 				 * preserve acc & mod dates
4377c478bd9Sstevel@tonic-gate 				 */
4387c478bd9Sstevel@tonic-gate 				u_times.actime = status.st_atime;
4397c478bd9Sstevel@tonic-gate 				u_times.modtime = status.st_mtime;
4407c478bd9Sstevel@tonic-gate 				if (utime(argvk, &u_times) != 0) {
4417c478bd9Sstevel@tonic-gate 					errflg++;
4427c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
443*da6c28aaSamw 					    "%s: cannot change times on %s: "),
444*da6c28aaSamw 					    argv0, argvk);
4457c478bd9Sstevel@tonic-gate 					perror("");
4467c478bd9Sstevel@tonic-gate 				}
4477c478bd9Sstevel@tonic-gate 				if (chmod(argvk, status.st_mode) != 0) {
4487c478bd9Sstevel@tonic-gate 					errflg++;
4497c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
4507c478bd9Sstevel@tonic-gate 					"%s: cannot change mode to %o on %s: "),
4517c478bd9Sstevel@tonic-gate 					    argv0, (uint_t)status.st_mode,
4527c478bd9Sstevel@tonic-gate 					    argvk);
4537c478bd9Sstevel@tonic-gate 					perror("");
4547c478bd9Sstevel@tonic-gate 				}
4557c478bd9Sstevel@tonic-gate 				(void) chown(argvk,
456*da6c28aaSamw 				    status.st_uid, status.st_gid);
457fa9e4066Sahrens 				if (aclp && (facl_set(outfile, aclp) < 0)) {
458fa9e4066Sahrens 					(void) printf(gettext("%s: cannot "
459fa9e4066Sahrens 					    "set ACL on %s: "), argv0, argvk);
460fa9e4066Sahrens 					perror("");
461fa9e4066Sahrens 				}
462fa9e4066Sahrens 
4637c478bd9Sstevel@tonic-gate 				rmflg = 0;
4647c478bd9Sstevel@tonic-gate 			}
4657c478bd9Sstevel@tonic-gate 			if (!errflg)
4667c478bd9Sstevel@tonic-gate 				fcount--; 	/* success after all */
4677c478bd9Sstevel@tonic-gate 		}
4687c478bd9Sstevel@tonic-gate done:		(void) close(infile);
4697c478bd9Sstevel@tonic-gate 		if (!pcat)
4707c478bd9Sstevel@tonic-gate 			(void) close(outfile);
471fa9e4066Sahrens 
472f0a2d94fSmarks 		if (aclp) {
473fa9e4066Sahrens 			acl_free(aclp);
474f0a2d94fSmarks 			aclp = NULL;
475f0a2d94fSmarks 		}
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate 	return (fcount);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate /*
4817c478bd9Sstevel@tonic-gate  * This code is for unpacking files that
4827c478bd9Sstevel@tonic-gate  * were packed using the previous algorithm.
4837c478bd9Sstevel@tonic-gate  */
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate static int	Tree[1024];
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate int
expand()4907c478bd9Sstevel@tonic-gate expand()
4917c478bd9Sstevel@tonic-gate {
4927c478bd9Sstevel@tonic-gate 	int tp, bit;
4937c478bd9Sstevel@tonic-gate 	short word;
4947c478bd9Sstevel@tonic-gate 	int keysize, i, *t;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	outp = outbuff;
4977c478bd9Sstevel@tonic-gate 	inp = &inbuff[2];
4987c478bd9Sstevel@tonic-gate 	inleft -= 2;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	origsize = ((long)(unsigned)getwdsize())*256*256;
5017c478bd9Sstevel@tonic-gate 	origsize += (unsigned)getwdsize();
5027c478bd9Sstevel@tonic-gate 	if (origsize == 0 || is_eof) {
5037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
504*da6c28aaSamw 		    "%s: %s: not in packed format\n"),
505*da6c28aaSamw 		    argv0, filename);
5067c478bd9Sstevel@tonic-gate 		return (0);
5077c478bd9Sstevel@tonic-gate 	}
5087c478bd9Sstevel@tonic-gate 	t = Tree;
5097c478bd9Sstevel@tonic-gate 	for (keysize = getwdsize(); keysize--; ) {
5107c478bd9Sstevel@tonic-gate 		if ((i = getch()) == 0377)
5117c478bd9Sstevel@tonic-gate 			*t++ = getwdsize();
5127c478bd9Sstevel@tonic-gate 		else {
5137c478bd9Sstevel@tonic-gate 				/*
5147c478bd9Sstevel@tonic-gate 				 * reached EOF unexpectedly
5157c478bd9Sstevel@tonic-gate 				 */
5167c478bd9Sstevel@tonic-gate 			if (is_eof) {
5177c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
518*da6c28aaSamw 				    "%s: %s: not in packed format\n"),
519*da6c28aaSamw 				    argv0, filename);
5207c478bd9Sstevel@tonic-gate 				return (0);
5217c478bd9Sstevel@tonic-gate 			}
5227c478bd9Sstevel@tonic-gate 			*t++ = i & 0377;
5237c478bd9Sstevel@tonic-gate 		}
5247c478bd9Sstevel@tonic-gate 	}
5257c478bd9Sstevel@tonic-gate 		/*
5267c478bd9Sstevel@tonic-gate 		 * reached EOF unexpectedly
5277c478bd9Sstevel@tonic-gate 		 */
5287c478bd9Sstevel@tonic-gate 	if (is_eof) {
5297c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
530*da6c28aaSamw 		    "%s: %s: not in packed format\n"),
531*da6c28aaSamw 		    argv0, filename);
5327c478bd9Sstevel@tonic-gate 		return (0);
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	bit = tp = 0;
5377c478bd9Sstevel@tonic-gate 	for (;;) {
5387c478bd9Sstevel@tonic-gate 		if (bit <= 0) {
5397c478bd9Sstevel@tonic-gate 			word = getwdsize();
5407c478bd9Sstevel@tonic-gate 			/*
5417c478bd9Sstevel@tonic-gate 			 * reached EOF unexpectedly
5427c478bd9Sstevel@tonic-gate 			 */
5437c478bd9Sstevel@tonic-gate 			if (word == 0 && is_eof && origsize > 0) {
5447c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
545*da6c28aaSamw 				    "%s: %s: not in packed format\n"),
546*da6c28aaSamw 				    argv0, filename);
5477c478bd9Sstevel@tonic-gate 				return (0);
5487c478bd9Sstevel@tonic-gate 			}
5497c478bd9Sstevel@tonic-gate 			bit = 16;
5507c478bd9Sstevel@tonic-gate 		}
5517c478bd9Sstevel@tonic-gate 		tp += Tree[tp + (word < 0)];
5527c478bd9Sstevel@tonic-gate 		word <<= 1;
5537c478bd9Sstevel@tonic-gate 		bit--;
5547c478bd9Sstevel@tonic-gate 		if (Tree[tp] == 0) {
5557c478bd9Sstevel@tonic-gate 			putch(Tree[tp+1]);
5567c478bd9Sstevel@tonic-gate 			tp = 0;
5577c478bd9Sstevel@tonic-gate 			if ((origsize -= 1) == 0) {
5587c478bd9Sstevel@tonic-gate 				(void) write(outfile, outbuff, outp - outbuff);
5597c478bd9Sstevel@tonic-gate 				return (1);
5607c478bd9Sstevel@tonic-gate 			}
5617c478bd9Sstevel@tonic-gate 		}
5627c478bd9Sstevel@tonic-gate 	}
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate int
getch()5667c478bd9Sstevel@tonic-gate getch()
5677c478bd9Sstevel@tonic-gate {
5687c478bd9Sstevel@tonic-gate 	if (inleft <= 0) {
5697c478bd9Sstevel@tonic-gate 		inleft = read(infile, inp = inbuff, BUFSIZ);
5707c478bd9Sstevel@tonic-gate 		if (inleft < 0) {
5717c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
572*da6c28aaSamw 			    "%s: %s: read error: "),
573*da6c28aaSamw 			    argv0, filename);
5747c478bd9Sstevel@tonic-gate 			perror("");
5757c478bd9Sstevel@tonic-gate 			longjmp(env, 1);
5767c478bd9Sstevel@tonic-gate 		} else {		/* reached EOF, report it */
5777c478bd9Sstevel@tonic-gate 			if (inleft == 0) {
5787c478bd9Sstevel@tonic-gate 				is_eof = 1;
5797c478bd9Sstevel@tonic-gate 				return (EOF);
5807c478bd9Sstevel@tonic-gate 			}
5817c478bd9Sstevel@tonic-gate 		}
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 	inleft--;
5847c478bd9Sstevel@tonic-gate 	return (*inp++ & 0377);
5857c478bd9Sstevel@tonic-gate }
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate int
getwdsize()5887c478bd9Sstevel@tonic-gate getwdsize()
5897c478bd9Sstevel@tonic-gate {
5907c478bd9Sstevel@tonic-gate 	char c;
5917c478bd9Sstevel@tonic-gate 	int d;
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	c = getch();
5947c478bd9Sstevel@tonic-gate 	d = getch();
5957c478bd9Sstevel@tonic-gate 	if (is_eof)
5967c478bd9Sstevel@tonic-gate 		return (0);
5977c478bd9Sstevel@tonic-gate 	d <<= 8;
5987c478bd9Sstevel@tonic-gate 	d |= c & 0377;
5997c478bd9Sstevel@tonic-gate 	return (d);
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate void
onsig(int sig)6037c478bd9Sstevel@tonic-gate onsig(int sig)
6047c478bd9Sstevel@tonic-gate {
6057c478bd9Sstevel@tonic-gate 				/* could be running as unpack or pcat	*/
6067c478bd9Sstevel@tonic-gate 				/* but rmflg is set only when running	*/
6077c478bd9Sstevel@tonic-gate 				/* as unpack and only when file is	*/
6087c478bd9Sstevel@tonic-gate 				/* created by unpack and not yet done	*/
6097c478bd9Sstevel@tonic-gate 	if (rmflg == 1)
6107c478bd9Sstevel@tonic-gate 		(void) unlink(argvk);
611*da6c28aaSamw 	/* To quiet lint noise */
612*da6c28aaSamw 	if (sig == SIGTERM || sig == SIGHUP || sig == SIGINT)
613*da6c28aaSamw 		exit(1);
6147c478bd9Sstevel@tonic-gate }
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate void
putch(char c)6177c478bd9Sstevel@tonic-gate putch(char c)
6187c478bd9Sstevel@tonic-gate {
6197c478bd9Sstevel@tonic-gate 	int n;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	*outp++ = c;
6227c478bd9Sstevel@tonic-gate 	if (outp == &outbuff[BUFSIZ]) {
6237c478bd9Sstevel@tonic-gate 		n = write(outfile, outp = outbuff, BUFSIZ);
6247c478bd9Sstevel@tonic-gate 		if (n < BUFSIZ) {
6257c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
626*da6c28aaSamw 			    "%s: %s: write error: "),
627*da6c28aaSamw 			    argv0, argvk);
6287c478bd9Sstevel@tonic-gate 			perror("");
6297c478bd9Sstevel@tonic-gate 			longjmp(env, 2);
6307c478bd9Sstevel@tonic-gate 		}
6317c478bd9Sstevel@tonic-gate 	}
6327c478bd9Sstevel@tonic-gate }
633