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