xref: /titanic_50/usr/src/cmd/unpack/unpack.c (revision f0a2d94fac0bda585fe02eb5f086dbbf15c986a7)
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
5*f0a2d94fSmarks  * Common Development and Distribution License (the "License").
6*f0a2d94fSmarks  * 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*f0a2d94fSmarks  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
277c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.21	*/
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  *	Huffman decompressor
347c478bd9Sstevel@tonic-gate  *	Usage:	pcat filename...
357c478bd9Sstevel@tonic-gate  *	or	unpack filename...
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <stdio.h>
397c478bd9Sstevel@tonic-gate #include <fcntl.h>
407c478bd9Sstevel@tonic-gate #include <setjmp.h>
417c478bd9Sstevel@tonic-gate #include <signal.h>
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <sys/stat.h>
447c478bd9Sstevel@tonic-gate #include <unistd.h>
457c478bd9Sstevel@tonic-gate #include <locale.h>
467c478bd9Sstevel@tonic-gate #include <utime.h>
477c478bd9Sstevel@tonic-gate #include <stdlib.h>
487c478bd9Sstevel@tonic-gate #include <limits.h>
497c478bd9Sstevel@tonic-gate #include <sys/param.h>
507c478bd9Sstevel@tonic-gate #include <dirent.h>
51fa9e4066Sahrens #include <sys/acl.h>
52fa9e4066Sahrens #include <aclutils.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate static struct utimbuf u_times;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static jmp_buf env;
577c478bd9Sstevel@tonic-gate static struct	stat status;
587c478bd9Sstevel@tonic-gate static char	*argv0, *argvk;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /* rmflg, when set it's ok to rm arvk file on caught signals */
617c478bd9Sstevel@tonic-gate static int	rmflg = 0;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #define	SUF0	'.'
647c478bd9Sstevel@tonic-gate #define	SUF1	'z'
657c478bd9Sstevel@tonic-gate #define	US	037
667c478bd9Sstevel@tonic-gate #define	RS	036
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /* variables associated with i/o */
697c478bd9Sstevel@tonic-gate static char	filename[MAXPATHLEN];
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static short	infile;
727c478bd9Sstevel@tonic-gate static short	outfile;
737c478bd9Sstevel@tonic-gate static short	inleft;
747c478bd9Sstevel@tonic-gate static short 	is_eof = 0;
757c478bd9Sstevel@tonic-gate static char	*inp;
767c478bd9Sstevel@tonic-gate static char	*outp;
777c478bd9Sstevel@tonic-gate static char	inbuff[BUFSIZ];
787c478bd9Sstevel@tonic-gate static char	outbuff[BUFSIZ];
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /* the dictionary */
817c478bd9Sstevel@tonic-gate static long	origsize;
827c478bd9Sstevel@tonic-gate static short	maxlev;
837c478bd9Sstevel@tonic-gate static short	intnodes[25];
847c478bd9Sstevel@tonic-gate static char	*tree[25];
857c478bd9Sstevel@tonic-gate static char	characters[256];
867c478bd9Sstevel@tonic-gate static char	*eof;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate static void putch(char c);
897c478bd9Sstevel@tonic-gate static int expand();
907c478bd9Sstevel@tonic-gate static int decode();
917c478bd9Sstevel@tonic-gate static int getwdsize();
927c478bd9Sstevel@tonic-gate static int getch();
937c478bd9Sstevel@tonic-gate static int getdict();
947c478bd9Sstevel@tonic-gate static int mv_xattrs();
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /* read in the dictionary portion and build decoding structures */
977c478bd9Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */
987c478bd9Sstevel@tonic-gate int
997c478bd9Sstevel@tonic-gate getdict()
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	register int c, i, nchildren;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	/*
1047c478bd9Sstevel@tonic-gate 	 * check two-byte header
1057c478bd9Sstevel@tonic-gate 	 * get size of original file,
1067c478bd9Sstevel@tonic-gate 	 * get number of levels in maxlev,
1077c478bd9Sstevel@tonic-gate 	 * get number of leaves on level i in intnodes[i],
1087c478bd9Sstevel@tonic-gate 	 * set tree[i] to point to leaves for level i
1097c478bd9Sstevel@tonic-gate 	 */
1107c478bd9Sstevel@tonic-gate 	eof = &characters[0];
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	inbuff[6] = 25;
1137c478bd9Sstevel@tonic-gate 	inleft = read(infile, &inbuff[0], BUFSIZ);
1147c478bd9Sstevel@tonic-gate 	if (inleft < 0) {
1157c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
1167c478bd9Sstevel@tonic-gate 			"%s: %s: read error: "), argv0, filename);
1177c478bd9Sstevel@tonic-gate 		perror("");
1187c478bd9Sstevel@tonic-gate 		return (0);
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 	if (inbuff[0] != US)
1217c478bd9Sstevel@tonic-gate 		goto goof;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	if (inbuff[1] == US) {		/* oldstyle packing */
1247c478bd9Sstevel@tonic-gate 		if (setjmp(env))
1257c478bd9Sstevel@tonic-gate 			return (0);
1267c478bd9Sstevel@tonic-gate 		return (expand());
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 	if (inbuff[1] != RS)
1297c478bd9Sstevel@tonic-gate 		goto goof;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	inp = &inbuff[2];
1327c478bd9Sstevel@tonic-gate 	origsize = 0;
1337c478bd9Sstevel@tonic-gate 	for (i = 0; i < 4; i++)
1347c478bd9Sstevel@tonic-gate 		origsize = origsize*256 + ((*inp++) & 0377);
1357c478bd9Sstevel@tonic-gate 	maxlev = *inp++ & 0377;
1367c478bd9Sstevel@tonic-gate 	if (maxlev > 24) {
1377c478bd9Sstevel@tonic-gate goof:		(void) fprintf(stderr, gettext(
1387c478bd9Sstevel@tonic-gate 			"%s: %s: not in packed format\n"), argv0, filename);
1397c478bd9Sstevel@tonic-gate 		return (0);
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 	for (i = 1; i <= maxlev; i++)
1427c478bd9Sstevel@tonic-gate 		intnodes[i] = *inp++ & 0377;
1437c478bd9Sstevel@tonic-gate 	for (i = 1; i <= maxlev; i++) {
1447c478bd9Sstevel@tonic-gate 		tree[i] = eof;
1457c478bd9Sstevel@tonic-gate 		for (c = intnodes[i]; c > 0; c--) {
1467c478bd9Sstevel@tonic-gate 			if (eof >= &characters[255])
1477c478bd9Sstevel@tonic-gate 				goto goof;
1487c478bd9Sstevel@tonic-gate 			*eof++ = *inp++;
1497c478bd9Sstevel@tonic-gate 		}
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 	*eof++ = *inp++;
1527c478bd9Sstevel@tonic-gate 	intnodes[maxlev] += 2;
1537c478bd9Sstevel@tonic-gate 	inleft -= inp - &inbuff[0];
1547c478bd9Sstevel@tonic-gate 	if (inleft < 0)
1557c478bd9Sstevel@tonic-gate 		goto goof;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	/*
1587c478bd9Sstevel@tonic-gate 	 * convert intnodes[i] to be number of
1597c478bd9Sstevel@tonic-gate 	 * internal nodes possessed by level i
1607c478bd9Sstevel@tonic-gate 	 */
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	nchildren = 0;
1637c478bd9Sstevel@tonic-gate 	for (i = maxlev; i >= 1; i--) {
1647c478bd9Sstevel@tonic-gate 		c = intnodes[i];
1657c478bd9Sstevel@tonic-gate 		intnodes[i] = nchildren /= 2;
1667c478bd9Sstevel@tonic-gate 		nchildren += c;
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 	return (decode());
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /* unpack the file */
1727c478bd9Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */
1737c478bd9Sstevel@tonic-gate int
1747c478bd9Sstevel@tonic-gate decode()
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 	register int bitsleft, c, i;
1777c478bd9Sstevel@tonic-gate 	int j, lev, cont = 1;
1787c478bd9Sstevel@tonic-gate 	char *p;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	outp = &outbuff[0];
1817c478bd9Sstevel@tonic-gate 	lev = 1;
1827c478bd9Sstevel@tonic-gate 	i = 0;
1837c478bd9Sstevel@tonic-gate 	while (cont) {
1847c478bd9Sstevel@tonic-gate 		if (inleft <= 0) {
1857c478bd9Sstevel@tonic-gate 			inleft = read(infile, inp = &inbuff[0], BUFSIZ);
1867c478bd9Sstevel@tonic-gate 			if (inleft < 0) {
1877c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
1887c478bd9Sstevel@tonic-gate 					"%s: %s: read error: "),
1897c478bd9Sstevel@tonic-gate 					argv0, filename);
1907c478bd9Sstevel@tonic-gate 				perror("");
1917c478bd9Sstevel@tonic-gate 				return (0);
1927c478bd9Sstevel@tonic-gate 			}
1937c478bd9Sstevel@tonic-gate 		}
1947c478bd9Sstevel@tonic-gate 		if (--inleft < 0) {
1957c478bd9Sstevel@tonic-gate uggh:			(void) fprintf(stderr, gettext(
1967c478bd9Sstevel@tonic-gate 				"%s: %s: unpacking error\n"),
1977c478bd9Sstevel@tonic-gate 				argv0, filename);
1987c478bd9Sstevel@tonic-gate 			return (0);
1997c478bd9Sstevel@tonic-gate 		}
2007c478bd9Sstevel@tonic-gate 		c = *inp++;
2017c478bd9Sstevel@tonic-gate 		bitsleft = 8;
2027c478bd9Sstevel@tonic-gate 		while (--bitsleft >= 0) {
2037c478bd9Sstevel@tonic-gate 			i *= 2;
2047c478bd9Sstevel@tonic-gate 			if (c & 0200)
2057c478bd9Sstevel@tonic-gate 				i++;
2067c478bd9Sstevel@tonic-gate 			c <<= 1;
2077c478bd9Sstevel@tonic-gate 			if ((j = i - intnodes[lev]) >= 0) {
2087c478bd9Sstevel@tonic-gate 				p = &tree[lev][j];
2097c478bd9Sstevel@tonic-gate 				if (p == eof) {
2107c478bd9Sstevel@tonic-gate 					c = outp - &outbuff[0];
2117c478bd9Sstevel@tonic-gate 				    if (write(outfile, &outbuff[0], c) != c) {
2127c478bd9Sstevel@tonic-gate wrerr:						(void) fprintf(stderr, gettext(
2137c478bd9Sstevel@tonic-gate 						"%s: %s: write error: "),
2147c478bd9Sstevel@tonic-gate 							argv0, argvk);
2157c478bd9Sstevel@tonic-gate 						perror("");
2167c478bd9Sstevel@tonic-gate 						return (0);
2177c478bd9Sstevel@tonic-gate 					}
2187c478bd9Sstevel@tonic-gate 					origsize -= c;
2197c478bd9Sstevel@tonic-gate 					if (origsize != 0)
2207c478bd9Sstevel@tonic-gate 						goto uggh;
2217c478bd9Sstevel@tonic-gate 					return (1);
2227c478bd9Sstevel@tonic-gate 				}
2237c478bd9Sstevel@tonic-gate 				*outp++ = *p;
2247c478bd9Sstevel@tonic-gate 				if (outp == &outbuff[BUFSIZ]) {
2257c478bd9Sstevel@tonic-gate 					if (write(outfile, outp = &outbuff[0],
2267c478bd9Sstevel@tonic-gate 						    BUFSIZ) != BUFSIZ)
2277c478bd9Sstevel@tonic-gate 						goto wrerr;
2287c478bd9Sstevel@tonic-gate 					origsize -= BUFSIZ;
2297c478bd9Sstevel@tonic-gate 				}
2307c478bd9Sstevel@tonic-gate 				lev = 1;
2317c478bd9Sstevel@tonic-gate 				i = 0;
2327c478bd9Sstevel@tonic-gate 			} else
2337c478bd9Sstevel@tonic-gate 				lev++;
2347c478bd9Sstevel@tonic-gate 		}
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 	return (1);	/* we won't get here , but lint is pleased */
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate int
2407c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	extern int optind;
2437c478bd9Sstevel@tonic-gate 	int i, k;
244fa9e4066Sahrens 	int error;
2457c478bd9Sstevel@tonic-gate 	int sep, errflg = 0, pcat = 0;
2467c478bd9Sstevel@tonic-gate 	register char *p1, *cp;
2477c478bd9Sstevel@tonic-gate 	int fcount = 0;		/* failure count */
2487c478bd9Sstevel@tonic-gate 	int max_name;
2497c478bd9Sstevel@tonic-gate 	void onsig(int);
250*f0a2d94fSmarks 	acl_t *aclp = NULL;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
2547c478bd9Sstevel@tonic-gate #ifdef __STDC__
2557c478bd9Sstevel@tonic-gate 		signal((int)SIGHUP, onsig);
2567c478bd9Sstevel@tonic-gate #else
2577c478bd9Sstevel@tonic-gate 		signal((int)SIGHUP, onsig);
2587c478bd9Sstevel@tonic-gate #endif
2597c478bd9Sstevel@tonic-gate 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
2607c478bd9Sstevel@tonic-gate #ifdef __STDC__
2617c478bd9Sstevel@tonic-gate 		signal((int)SIGINT, onsig);
2627c478bd9Sstevel@tonic-gate #else
2637c478bd9Sstevel@tonic-gate 		signal((int)SIGINT, onsig);
2647c478bd9Sstevel@tonic-gate #endif
2657c478bd9Sstevel@tonic-gate 	if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
2667c478bd9Sstevel@tonic-gate #ifdef __STDC__
2677c478bd9Sstevel@tonic-gate 		signal((int)SIGTERM, onsig);
2687c478bd9Sstevel@tonic-gate #else
2697c478bd9Sstevel@tonic-gate 		signal(SIGTERM, onsig);
2707c478bd9Sstevel@tonic-gate #endif
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2737c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
2747c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
2757c478bd9Sstevel@tonic-gate #endif
2767c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	p1 = *argv;
2797c478bd9Sstevel@tonic-gate 	while (*p1++);	/* Point p1 to end of argv[0] string */
2807c478bd9Sstevel@tonic-gate 	while (--p1 >= *argv)
2817c478bd9Sstevel@tonic-gate 		if (*p1 == '/')break;
2827c478bd9Sstevel@tonic-gate 	*argv = p1 + 1;
2837c478bd9Sstevel@tonic-gate 	argv0 = argv[0];
2847c478bd9Sstevel@tonic-gate 	if (**argv == 'p')pcat++;	/* User entered pcat (or /xx/xx/pcat) */
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	while (getopt(argc, argv, "") != EOF)
2877c478bd9Sstevel@tonic-gate 		++errflg;
2887c478bd9Sstevel@tonic-gate 	/*
2897c478bd9Sstevel@tonic-gate 	 * Check for invalid option.  Also check for missing
2907c478bd9Sstevel@tonic-gate 	 * file operand, ie: "unpack" or "pcat".
2917c478bd9Sstevel@tonic-gate 	 */
2927c478bd9Sstevel@tonic-gate 	argc -= optind;
2937c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
2947c478bd9Sstevel@tonic-gate 	if (errflg || argc < 1) {
2957c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("usage: %s file...\n"), argv0);
2967c478bd9Sstevel@tonic-gate 		if (argc < 1) {
2977c478bd9Sstevel@tonic-gate 			/*
2987c478bd9Sstevel@tonic-gate 			 * return 1 for usage error when no file was specified
2997c478bd9Sstevel@tonic-gate 			 */
3007c478bd9Sstevel@tonic-gate 			return (1);
3017c478bd9Sstevel@tonic-gate 		}
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate 	/* loop through the file names */
3047c478bd9Sstevel@tonic-gate 	for (k = 0; k < argc; k++) {
3057c478bd9Sstevel@tonic-gate 		fcount++;	/* expect the worst */
3067c478bd9Sstevel@tonic-gate 		if (errflg) {
3077c478bd9Sstevel@tonic-gate 			/*
3087c478bd9Sstevel@tonic-gate 			 * invalid option; just count the number of files not
3097c478bd9Sstevel@tonic-gate 			 * unpacked
3107c478bd9Sstevel@tonic-gate 			 */
3117c478bd9Sstevel@tonic-gate 			continue;
3127c478bd9Sstevel@tonic-gate 		}
3137c478bd9Sstevel@tonic-gate 		/* remove any .z suffix the user may have added */
3147c478bd9Sstevel@tonic-gate 		for (cp = argv[k]; *cp != '\0'; ++cp)
3157c478bd9Sstevel@tonic-gate 			;
3167c478bd9Sstevel@tonic-gate 		if (cp[-1] == SUF1 && cp[-2] == SUF0) {
3177c478bd9Sstevel@tonic-gate 			*cp-- = '\0'; *cp-- = '\0'; *cp = '\0';
3187c478bd9Sstevel@tonic-gate 		}
3197c478bd9Sstevel@tonic-gate 		sep = -1;
3207c478bd9Sstevel@tonic-gate 		cp = filename;
3217c478bd9Sstevel@tonic-gate 		argvk = argv[k];
3227c478bd9Sstevel@tonic-gate 		/* copy argv[k] to filename and count chars in base name */
3237c478bd9Sstevel@tonic-gate 		for (i = 0; i < (MAXPATHLEN-3) && (*cp = argvk[i]); i++)
3247c478bd9Sstevel@tonic-gate 			if (*cp++ == '/')
3257c478bd9Sstevel@tonic-gate 				sep = i;
3267c478bd9Sstevel@tonic-gate 		/* add .z suffix to filename */
3277c478bd9Sstevel@tonic-gate 		*cp++ = SUF0;
3287c478bd9Sstevel@tonic-gate 		*cp++ = SUF1;
3297c478bd9Sstevel@tonic-gate 		*cp = '\0';
3307c478bd9Sstevel@tonic-gate 		if ((infile = open(filename, O_RDONLY)) == -1) {
3317c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
3327c478bd9Sstevel@tonic-gate 				"%s: %s: cannot open: "),
3337c478bd9Sstevel@tonic-gate 				argv0, filename);
3347c478bd9Sstevel@tonic-gate 			perror("");
3357c478bd9Sstevel@tonic-gate 			goto done;
3367c478bd9Sstevel@tonic-gate 		}
3377c478bd9Sstevel@tonic-gate 		if (pcat)
3387c478bd9Sstevel@tonic-gate 			outfile = 1;	/* standard output */
3397c478bd9Sstevel@tonic-gate 		else {
340fa9e4066Sahrens 
341fa9e4066Sahrens 			error = facl_get(infile, ACL_NO_TRIVIAL, &aclp);
342fa9e4066Sahrens 			if (error != 0) {
343fa9e4066Sahrens 				(void) printf(gettext(
344fa9e4066Sahrens 				    "%s: %s: cannot retrieve ACL : %s\n"),
345fa9e4066Sahrens 				argv0, filename, acl_strerror(error));
346fa9e4066Sahrens 			}
347fa9e4066Sahrens 
3487c478bd9Sstevel@tonic-gate 			max_name = pathconf(filename, _PC_NAME_MAX);
3497c478bd9Sstevel@tonic-gate 			if (max_name == -1) {
3507c478bd9Sstevel@tonic-gate 				/* no limit on length of filename */
3517c478bd9Sstevel@tonic-gate 				max_name = _POSIX_NAME_MAX;
3527c478bd9Sstevel@tonic-gate 			}
3537c478bd9Sstevel@tonic-gate 			if (i >= (MAXPATHLEN-1) || (i - sep - 1) > max_name) {
3547c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
3557c478bd9Sstevel@tonic-gate 					"%s: %s: file name too long\n"),
3567c478bd9Sstevel@tonic-gate 					argv0, argvk);
3577c478bd9Sstevel@tonic-gate 				goto done;
3587c478bd9Sstevel@tonic-gate 			}
3597c478bd9Sstevel@tonic-gate 			if (stat(argvk, &status) != -1) {
3607c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
3617c478bd9Sstevel@tonic-gate 					"%s: %s: already exists\n"),
3627c478bd9Sstevel@tonic-gate 					argv0, argvk);
3637c478bd9Sstevel@tonic-gate 				goto done;
3647c478bd9Sstevel@tonic-gate 			}
3657c478bd9Sstevel@tonic-gate 			(void) fstat(infile, &status);
3667c478bd9Sstevel@tonic-gate 			if (status.st_nlink != 1) {
3677c478bd9Sstevel@tonic-gate 				(void) printf(gettext(
3687c478bd9Sstevel@tonic-gate 					"%s: %s: Warning: file has links\n"),
3697c478bd9Sstevel@tonic-gate 					argv0, filename);
3707c478bd9Sstevel@tonic-gate 			}
3717c478bd9Sstevel@tonic-gate 			if ((outfile = creat(argvk, status.st_mode)) == -1) {
3727c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
3737c478bd9Sstevel@tonic-gate 					"%s: %s: cannot create: "),
3747c478bd9Sstevel@tonic-gate 					argv0, argvk);
3757c478bd9Sstevel@tonic-gate 				perror("");
3767c478bd9Sstevel@tonic-gate 				goto done;
3777c478bd9Sstevel@tonic-gate 			}
3787c478bd9Sstevel@tonic-gate 			rmflg = 1;
3797c478bd9Sstevel@tonic-gate 		}
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 		if (getdict() &&	/* unpack */
3827c478bd9Sstevel@tonic-gate 		    (pcat ||
3837c478bd9Sstevel@tonic-gate 			(pathconf(filename, _PC_XATTR_EXISTS) != 1) ||
3847c478bd9Sstevel@tonic-gate 				(mv_xattrs(infile, outfile,
3857c478bd9Sstevel@tonic-gate 					filename, 0) == 0))) {
3867c478bd9Sstevel@tonic-gate 			if (!pcat) {
3877c478bd9Sstevel@tonic-gate 				/*
3887c478bd9Sstevel@tonic-gate 				 * preserve acc & mod dates
3897c478bd9Sstevel@tonic-gate 				 */
3907c478bd9Sstevel@tonic-gate 				u_times.actime = status.st_atime;
3917c478bd9Sstevel@tonic-gate 				u_times.modtime = status.st_mtime;
3927c478bd9Sstevel@tonic-gate 				if (utime(argvk, &u_times) != 0) {
3937c478bd9Sstevel@tonic-gate 					errflg++;
3947c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
3957c478bd9Sstevel@tonic-gate 					"%s: cannot change times on %s: "),
3967c478bd9Sstevel@tonic-gate 						argv0, argvk);
3977c478bd9Sstevel@tonic-gate 					perror("");
3987c478bd9Sstevel@tonic-gate 				}
3997c478bd9Sstevel@tonic-gate 				if (chmod(argvk, status.st_mode) != 0) {
4007c478bd9Sstevel@tonic-gate 					errflg++;
4017c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
4027c478bd9Sstevel@tonic-gate 					"%s: cannot change mode to %o on %s: "),
4037c478bd9Sstevel@tonic-gate 					    argv0, (uint_t)status.st_mode,
4047c478bd9Sstevel@tonic-gate 					    argvk);
4057c478bd9Sstevel@tonic-gate 					perror("");
4067c478bd9Sstevel@tonic-gate 				}
4077c478bd9Sstevel@tonic-gate 				(void) chown(argvk,
4087c478bd9Sstevel@tonic-gate 						status.st_uid, status.st_gid);
409fa9e4066Sahrens 				if (aclp && (facl_set(outfile, aclp) < 0)) {
410fa9e4066Sahrens 					(void) printf(gettext("%s: cannot "
411fa9e4066Sahrens 					    "set ACL on %s: "), argv0, argvk);
412fa9e4066Sahrens 					perror("");
413fa9e4066Sahrens 				}
414fa9e4066Sahrens 
4157c478bd9Sstevel@tonic-gate 				rmflg = 0;
4167c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%s: %s: unpacked\n"),
4177c478bd9Sstevel@tonic-gate 					argv0, argvk);
4187c478bd9Sstevel@tonic-gate 				(void) unlink(filename);
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 			}
4217c478bd9Sstevel@tonic-gate 			if (!errflg)
4227c478bd9Sstevel@tonic-gate 				fcount--; 	/* success after all */
4237c478bd9Sstevel@tonic-gate 		}
4247c478bd9Sstevel@tonic-gate 		else
4257c478bd9Sstevel@tonic-gate 			if (!pcat) {
4267c478bd9Sstevel@tonic-gate 				if (pathconf(argvk, _PC_XATTR_EXISTS) == 1) {
4277c478bd9Sstevel@tonic-gate 					(void) mv_xattrs(outfile, infile,
4287c478bd9Sstevel@tonic-gate 						argvk, 1);
4297c478bd9Sstevel@tonic-gate 				}
4307c478bd9Sstevel@tonic-gate 				(void) unlink(argvk);
4317c478bd9Sstevel@tonic-gate 			}
4327c478bd9Sstevel@tonic-gate done:		(void) close(infile);
4337c478bd9Sstevel@tonic-gate 		if (!pcat)
4347c478bd9Sstevel@tonic-gate 			(void) close(outfile);
435fa9e4066Sahrens 
436*f0a2d94fSmarks 		if (aclp) {
437fa9e4066Sahrens 			acl_free(aclp);
438*f0a2d94fSmarks 			aclp = NULL;
439*f0a2d94fSmarks 		}
4407c478bd9Sstevel@tonic-gate 	}
4417c478bd9Sstevel@tonic-gate 	return (fcount);
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate /*
4457c478bd9Sstevel@tonic-gate  * This code is for unpacking files that
4467c478bd9Sstevel@tonic-gate  * were packed using the previous algorithm.
4477c478bd9Sstevel@tonic-gate  */
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate static int	Tree[1024];
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate int
4547c478bd9Sstevel@tonic-gate expand()
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	int tp, bit;
4577c478bd9Sstevel@tonic-gate 	short word;
4587c478bd9Sstevel@tonic-gate 	int keysize, i, *t;
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	outp = outbuff;
4617c478bd9Sstevel@tonic-gate 	inp = &inbuff[2];
4627c478bd9Sstevel@tonic-gate 	inleft -= 2;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	origsize = ((long)(unsigned)getwdsize())*256*256;
4657c478bd9Sstevel@tonic-gate 	origsize += (unsigned)getwdsize();
4667c478bd9Sstevel@tonic-gate 	if (origsize == 0 || is_eof) {
4677c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
4687c478bd9Sstevel@tonic-gate 			"%s: %s: not in packed format\n"),
4697c478bd9Sstevel@tonic-gate 			argv0, filename);
4707c478bd9Sstevel@tonic-gate 		return (0);
4717c478bd9Sstevel@tonic-gate 	}
4727c478bd9Sstevel@tonic-gate 	t = Tree;
4737c478bd9Sstevel@tonic-gate 	for (keysize = getwdsize(); keysize--; ) {
4747c478bd9Sstevel@tonic-gate 		if ((i = getch()) == 0377)
4757c478bd9Sstevel@tonic-gate 			*t++ = getwdsize();
4767c478bd9Sstevel@tonic-gate 		else {
4777c478bd9Sstevel@tonic-gate 				/*
4787c478bd9Sstevel@tonic-gate 				 * reached EOF unexpectedly
4797c478bd9Sstevel@tonic-gate 				 */
4807c478bd9Sstevel@tonic-gate 			if (is_eof) {
4817c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
4827c478bd9Sstevel@tonic-gate 					"%s: %s: not in packed format\n"),
4837c478bd9Sstevel@tonic-gate 					argv0, filename);
4847c478bd9Sstevel@tonic-gate 				return (0);
4857c478bd9Sstevel@tonic-gate 			}
4867c478bd9Sstevel@tonic-gate 			*t++ = i & 0377;
4877c478bd9Sstevel@tonic-gate 		}
4887c478bd9Sstevel@tonic-gate 	}
4897c478bd9Sstevel@tonic-gate 		/*
4907c478bd9Sstevel@tonic-gate 		 * reached EOF unexpectedly
4917c478bd9Sstevel@tonic-gate 		 */
4927c478bd9Sstevel@tonic-gate 	if (is_eof) {
4937c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
4947c478bd9Sstevel@tonic-gate 			"%s: %s: not in packed format\n"),
4957c478bd9Sstevel@tonic-gate 			argv0, filename);
4967c478bd9Sstevel@tonic-gate 		return (0);
4977c478bd9Sstevel@tonic-gate 	}
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	bit = tp = 0;
5017c478bd9Sstevel@tonic-gate 	for (;;) {
5027c478bd9Sstevel@tonic-gate 		if (bit <= 0) {
5037c478bd9Sstevel@tonic-gate 			word = getwdsize();
5047c478bd9Sstevel@tonic-gate 			/*
5057c478bd9Sstevel@tonic-gate 			 * reached EOF unexpectedly
5067c478bd9Sstevel@tonic-gate 			 */
5077c478bd9Sstevel@tonic-gate 			if (word == 0 && is_eof && origsize > 0) {
5087c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
5097c478bd9Sstevel@tonic-gate 					"%s: %s: not in packed format\n"),
5107c478bd9Sstevel@tonic-gate 					argv0, filename);
5117c478bd9Sstevel@tonic-gate 				return (0);
5127c478bd9Sstevel@tonic-gate 			}
5137c478bd9Sstevel@tonic-gate 			bit = 16;
5147c478bd9Sstevel@tonic-gate 		}
5157c478bd9Sstevel@tonic-gate 		tp += Tree[tp + (word < 0)];
5167c478bd9Sstevel@tonic-gate 		word <<= 1;
5177c478bd9Sstevel@tonic-gate 		bit--;
5187c478bd9Sstevel@tonic-gate 		if (Tree[tp] == 0) {
5197c478bd9Sstevel@tonic-gate 			putch(Tree[tp+1]);
5207c478bd9Sstevel@tonic-gate 			tp = 0;
5217c478bd9Sstevel@tonic-gate 			if ((origsize -= 1) == 0) {
5227c478bd9Sstevel@tonic-gate 				(void) write(outfile, outbuff, outp - outbuff);
5237c478bd9Sstevel@tonic-gate 				return (1);
5247c478bd9Sstevel@tonic-gate 			}
5257c478bd9Sstevel@tonic-gate 		}
5267c478bd9Sstevel@tonic-gate 	}
5277c478bd9Sstevel@tonic-gate }
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate int
5307c478bd9Sstevel@tonic-gate getch()
5317c478bd9Sstevel@tonic-gate {
5327c478bd9Sstevel@tonic-gate 	if (inleft <= 0) {
5337c478bd9Sstevel@tonic-gate 		inleft = read(infile, inp = inbuff, BUFSIZ);
5347c478bd9Sstevel@tonic-gate 		if (inleft < 0) {
5357c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
5367c478bd9Sstevel@tonic-gate 				"%s: %s: read error: "),
5377c478bd9Sstevel@tonic-gate 				argv0, filename);
5387c478bd9Sstevel@tonic-gate 			perror("");
5397c478bd9Sstevel@tonic-gate 			longjmp(env, 1);
5407c478bd9Sstevel@tonic-gate 		} else {		/* reached EOF, report it */
5417c478bd9Sstevel@tonic-gate 			if (inleft == 0) {
5427c478bd9Sstevel@tonic-gate 				is_eof = 1;
5437c478bd9Sstevel@tonic-gate 				return (EOF);
5447c478bd9Sstevel@tonic-gate 			}
5457c478bd9Sstevel@tonic-gate 		}
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate 	inleft--;
5487c478bd9Sstevel@tonic-gate 	return (*inp++ & 0377);
5497c478bd9Sstevel@tonic-gate }
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate int
5527c478bd9Sstevel@tonic-gate getwdsize()
5537c478bd9Sstevel@tonic-gate {
5547c478bd9Sstevel@tonic-gate 	char c;
5557c478bd9Sstevel@tonic-gate 	int d;
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	c = getch();
5587c478bd9Sstevel@tonic-gate 	d = getch();
5597c478bd9Sstevel@tonic-gate 	if (is_eof)
5607c478bd9Sstevel@tonic-gate 		return (0);
5617c478bd9Sstevel@tonic-gate 	d <<= 8;
5627c478bd9Sstevel@tonic-gate 	d |= c & 0377;
5637c478bd9Sstevel@tonic-gate 	return (d);
5647c478bd9Sstevel@tonic-gate }
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate void
5677c478bd9Sstevel@tonic-gate onsig(int sig)
5687c478bd9Sstevel@tonic-gate {
5697c478bd9Sstevel@tonic-gate 				/* could be running as unpack or pcat	*/
5707c478bd9Sstevel@tonic-gate 				/* but rmflg is set only when running	*/
5717c478bd9Sstevel@tonic-gate 				/* as unpack and only when file is	*/
5727c478bd9Sstevel@tonic-gate 				/* created by unpack and not yet done	*/
5737c478bd9Sstevel@tonic-gate 	if (rmflg == 1)
5747c478bd9Sstevel@tonic-gate 		(void) unlink(argvk);
5757c478bd9Sstevel@tonic-gate 	exit(1);
5767c478bd9Sstevel@tonic-gate }
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate void
5797c478bd9Sstevel@tonic-gate putch(char c)
5807c478bd9Sstevel@tonic-gate {
5817c478bd9Sstevel@tonic-gate 	int n;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	*outp++ = c;
5847c478bd9Sstevel@tonic-gate 	if (outp == &outbuff[BUFSIZ]) {
5857c478bd9Sstevel@tonic-gate 		n = write(outfile, outp = outbuff, BUFSIZ);
5867c478bd9Sstevel@tonic-gate 		if (n < BUFSIZ) {
5877c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
5887c478bd9Sstevel@tonic-gate 				"%s: %s: write error: "),
5897c478bd9Sstevel@tonic-gate 				argv0, argvk);
5907c478bd9Sstevel@tonic-gate 			perror("");
5917c478bd9Sstevel@tonic-gate 			longjmp(env, 2);
5927c478bd9Sstevel@tonic-gate 		}
5937c478bd9Sstevel@tonic-gate 	}
5947c478bd9Sstevel@tonic-gate }
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate /*
5977c478bd9Sstevel@tonic-gate  * mv_xattrs - move (via renameat) all of the extended attributes
5987c478bd9Sstevel@tonic-gate  *	associated with the file referenced by infd to the file
5997c478bd9Sstevel@tonic-gate  *	referenced by outfd.  The infile and silent arguments are
6007c478bd9Sstevel@tonic-gate  *	provided for error message processing.  This function
6017c478bd9Sstevel@tonic-gate  *	returns 0 on success and -1 on error.
6027c478bd9Sstevel@tonic-gate  */
6037c478bd9Sstevel@tonic-gate static int
6047c478bd9Sstevel@tonic-gate mv_xattrs(int infd, int outfd, char *infile, int silent)
6057c478bd9Sstevel@tonic-gate {
6067c478bd9Sstevel@tonic-gate 	int indfd, outdfd, tmpfd;
6077c478bd9Sstevel@tonic-gate 	DIR *dirp = NULL;
6087c478bd9Sstevel@tonic-gate 	struct dirent *dp = NULL;
6097c478bd9Sstevel@tonic-gate 	int error = 0;
6107c478bd9Sstevel@tonic-gate 	char *etext;
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	indfd = outdfd = tmpfd = -1;
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	if ((indfd = openat(infd, ".", O_RDONLY|O_XATTR)) == -1) {
6157c478bd9Sstevel@tonic-gate 		etext = gettext("cannot open source");
6167c478bd9Sstevel@tonic-gate 		error = -1;
6177c478bd9Sstevel@tonic-gate 		goto out;
6187c478bd9Sstevel@tonic-gate 	}
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	if ((outdfd = openat(outfd, ".", O_RDONLY|O_XATTR)) == -1) {
6217c478bd9Sstevel@tonic-gate 		etext = gettext("cannot open target");
6227c478bd9Sstevel@tonic-gate 		error = -1;
6237c478bd9Sstevel@tonic-gate 		goto out;
6247c478bd9Sstevel@tonic-gate 	}
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	if ((tmpfd = dup(indfd)) == -1) {
6277c478bd9Sstevel@tonic-gate 		etext = gettext("cannot dup descriptor");
6287c478bd9Sstevel@tonic-gate 		error = -1;
6297c478bd9Sstevel@tonic-gate 		goto out;
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	}
6327c478bd9Sstevel@tonic-gate 	if ((dirp = fdopendir(tmpfd)) == NULL) {
6337c478bd9Sstevel@tonic-gate 		etext = gettext("cannot access source");
6347c478bd9Sstevel@tonic-gate 		error = -1;
6357c478bd9Sstevel@tonic-gate 		goto out;
6367c478bd9Sstevel@tonic-gate 	}
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	while (dp = readdir(dirp)) {
6397c478bd9Sstevel@tonic-gate 		if ((dp->d_name[0] == '.' && dp->d_name[1] == '\0') ||
6407c478bd9Sstevel@tonic-gate 		    (dp->d_name[0] == '.' && dp->d_name[1] == '.' &&
6417c478bd9Sstevel@tonic-gate 		    dp->d_name[2] == '\0'))
6427c478bd9Sstevel@tonic-gate 			continue;
6437c478bd9Sstevel@tonic-gate 		if ((renameat(indfd, dp->d_name, outdfd, dp->d_name)) == -1) {
6447c478bd9Sstevel@tonic-gate 			etext = dp->d_name;
6457c478bd9Sstevel@tonic-gate 			error = -1;
6467c478bd9Sstevel@tonic-gate 			goto out;
6477c478bd9Sstevel@tonic-gate 		}
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate out:
6507c478bd9Sstevel@tonic-gate 	if (error == -1 && silent == 0) {
6517c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(
6527c478bd9Sstevel@tonic-gate 			"unpack: %s: cannot move extended attributes, "),
6537c478bd9Sstevel@tonic-gate 			infile);
6547c478bd9Sstevel@tonic-gate 		perror(etext);
6557c478bd9Sstevel@tonic-gate 	}
6567c478bd9Sstevel@tonic-gate 	if (dirp)
6577c478bd9Sstevel@tonic-gate 		closedir(dirp);
6587c478bd9Sstevel@tonic-gate 	if (indfd != -1)
6597c478bd9Sstevel@tonic-gate 		close(indfd);
6607c478bd9Sstevel@tonic-gate 	if (outdfd != -1)
6617c478bd9Sstevel@tonic-gate 		close(outdfd);
6627c478bd9Sstevel@tonic-gate 	return (error);
6637c478bd9Sstevel@tonic-gate }
664