10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51620Smarks * Common Development and Distribution License (the "License").
61620Smarks * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
220Sstevel@tonic-gate /* All Rights Reserved */
230Sstevel@tonic-gate
240Sstevel@tonic-gate
250Sstevel@tonic-gate /*
26*5331Samw * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
270Sstevel@tonic-gate * Use is subject to license terms.
280Sstevel@tonic-gate */
290Sstevel@tonic-gate
30*5331Samw #pragma ident "%Z%%M% %I% %E% SMI"
310Sstevel@tonic-gate
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate * Huffman decompressor
340Sstevel@tonic-gate * Usage: pcat filename...
350Sstevel@tonic-gate * or unpack filename...
360Sstevel@tonic-gate */
370Sstevel@tonic-gate
380Sstevel@tonic-gate #include <setjmp.h>
390Sstevel@tonic-gate #include <signal.h>
400Sstevel@tonic-gate #include <locale.h>
410Sstevel@tonic-gate #include <utime.h>
420Sstevel@tonic-gate #include <sys/param.h>
43789Sahrens #include <sys/acl.h>
44789Sahrens #include <aclutils.h>
45*5331Samw #include <libcmdutils.h>
460Sstevel@tonic-gate
470Sstevel@tonic-gate static struct utimbuf u_times;
480Sstevel@tonic-gate static jmp_buf env;
490Sstevel@tonic-gate static struct stat status;
500Sstevel@tonic-gate static char *argv0, *argvk;
510Sstevel@tonic-gate
520Sstevel@tonic-gate /* rmflg, when set it's ok to rm arvk file on caught signals */
530Sstevel@tonic-gate static int rmflg = 0;
540Sstevel@tonic-gate
550Sstevel@tonic-gate #define SUF0 '.'
560Sstevel@tonic-gate #define SUF1 'z'
570Sstevel@tonic-gate #define US 037
580Sstevel@tonic-gate #define RS 036
590Sstevel@tonic-gate
600Sstevel@tonic-gate /* variables associated with i/o */
610Sstevel@tonic-gate static char filename[MAXPATHLEN];
620Sstevel@tonic-gate
630Sstevel@tonic-gate static short infile;
640Sstevel@tonic-gate static short outfile;
650Sstevel@tonic-gate static short inleft;
660Sstevel@tonic-gate static short is_eof = 0;
670Sstevel@tonic-gate static char *inp;
680Sstevel@tonic-gate static char *outp;
690Sstevel@tonic-gate static char inbuff[BUFSIZ];
700Sstevel@tonic-gate static char outbuff[BUFSIZ];
710Sstevel@tonic-gate
720Sstevel@tonic-gate /* the dictionary */
730Sstevel@tonic-gate static long origsize;
740Sstevel@tonic-gate static short maxlev;
750Sstevel@tonic-gate static short intnodes[25];
760Sstevel@tonic-gate static char *tree[25];
770Sstevel@tonic-gate static char characters[256];
780Sstevel@tonic-gate static char *eof;
790Sstevel@tonic-gate
800Sstevel@tonic-gate static void putch(char c);
810Sstevel@tonic-gate static int expand();
820Sstevel@tonic-gate static int decode();
830Sstevel@tonic-gate static int getwdsize();
840Sstevel@tonic-gate static int getch();
850Sstevel@tonic-gate static int getdict();
86*5331Samw
87*5331Samw /* Extended system attribute support */
88*5331Samw
89*5331Samw static int saflg = 0;
90*5331Samw
910Sstevel@tonic-gate
920Sstevel@tonic-gate /* read in the dictionary portion and build decoding structures */
930Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */
940Sstevel@tonic-gate int
getdict()950Sstevel@tonic-gate getdict()
960Sstevel@tonic-gate {
970Sstevel@tonic-gate register int c, i, nchildren;
980Sstevel@tonic-gate
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate * check two-byte header
1010Sstevel@tonic-gate * get size of original file,
1020Sstevel@tonic-gate * get number of levels in maxlev,
1030Sstevel@tonic-gate * get number of leaves on level i in intnodes[i],
1040Sstevel@tonic-gate * set tree[i] to point to leaves for level i
1050Sstevel@tonic-gate */
1060Sstevel@tonic-gate eof = &characters[0];
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate inbuff[6] = 25;
1090Sstevel@tonic-gate inleft = read(infile, &inbuff[0], BUFSIZ);
1100Sstevel@tonic-gate if (inleft < 0) {
1110Sstevel@tonic-gate (void) fprintf(stderr, gettext(
112*5331Samw "%s: %s: read error: "), argv0, filename);
1130Sstevel@tonic-gate perror("");
1140Sstevel@tonic-gate return (0);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate if (inbuff[0] != US)
1170Sstevel@tonic-gate goto goof;
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate if (inbuff[1] == US) { /* oldstyle packing */
1200Sstevel@tonic-gate if (setjmp(env))
1210Sstevel@tonic-gate return (0);
1220Sstevel@tonic-gate return (expand());
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate if (inbuff[1] != RS)
1250Sstevel@tonic-gate goto goof;
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate inp = &inbuff[2];
1280Sstevel@tonic-gate origsize = 0;
1290Sstevel@tonic-gate for (i = 0; i < 4; i++)
1300Sstevel@tonic-gate origsize = origsize*256 + ((*inp++) & 0377);
1310Sstevel@tonic-gate maxlev = *inp++ & 0377;
1320Sstevel@tonic-gate if (maxlev > 24) {
1330Sstevel@tonic-gate goof: (void) fprintf(stderr, gettext(
134*5331Samw "%s: %s: not in packed format\n"), argv0, filename);
1350Sstevel@tonic-gate return (0);
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate for (i = 1; i <= maxlev; i++)
1380Sstevel@tonic-gate intnodes[i] = *inp++ & 0377;
1390Sstevel@tonic-gate for (i = 1; i <= maxlev; i++) {
1400Sstevel@tonic-gate tree[i] = eof;
1410Sstevel@tonic-gate for (c = intnodes[i]; c > 0; c--) {
1420Sstevel@tonic-gate if (eof >= &characters[255])
1430Sstevel@tonic-gate goto goof;
1440Sstevel@tonic-gate *eof++ = *inp++;
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate *eof++ = *inp++;
1480Sstevel@tonic-gate intnodes[maxlev] += 2;
1490Sstevel@tonic-gate inleft -= inp - &inbuff[0];
1500Sstevel@tonic-gate if (inleft < 0)
1510Sstevel@tonic-gate goto goof;
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate * convert intnodes[i] to be number of
1550Sstevel@tonic-gate * internal nodes possessed by level i
1560Sstevel@tonic-gate */
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate nchildren = 0;
1590Sstevel@tonic-gate for (i = maxlev; i >= 1; i--) {
1600Sstevel@tonic-gate c = intnodes[i];
1610Sstevel@tonic-gate intnodes[i] = nchildren /= 2;
1620Sstevel@tonic-gate nchildren += c;
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate return (decode());
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate /* unpack the file */
1680Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */
1690Sstevel@tonic-gate int
decode()1700Sstevel@tonic-gate decode()
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate register int bitsleft, c, i;
1730Sstevel@tonic-gate int j, lev, cont = 1;
1740Sstevel@tonic-gate char *p;
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate outp = &outbuff[0];
1770Sstevel@tonic-gate lev = 1;
1780Sstevel@tonic-gate i = 0;
1790Sstevel@tonic-gate while (cont) {
1800Sstevel@tonic-gate if (inleft <= 0) {
1810Sstevel@tonic-gate inleft = read(infile, inp = &inbuff[0], BUFSIZ);
1820Sstevel@tonic-gate if (inleft < 0) {
1830Sstevel@tonic-gate (void) fprintf(stderr, gettext(
184*5331Samw "%s: %s: read error: "),
185*5331Samw argv0, filename);
1860Sstevel@tonic-gate perror("");
1870Sstevel@tonic-gate return (0);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate if (--inleft < 0) {
1910Sstevel@tonic-gate uggh: (void) fprintf(stderr, gettext(
192*5331Samw "%s: %s: unpacking error\n"),
193*5331Samw argv0, filename);
1940Sstevel@tonic-gate return (0);
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate c = *inp++;
1970Sstevel@tonic-gate bitsleft = 8;
1980Sstevel@tonic-gate while (--bitsleft >= 0) {
1990Sstevel@tonic-gate i *= 2;
2000Sstevel@tonic-gate if (c & 0200)
2010Sstevel@tonic-gate i++;
2020Sstevel@tonic-gate c <<= 1;
2030Sstevel@tonic-gate if ((j = i - intnodes[lev]) >= 0) {
2040Sstevel@tonic-gate p = &tree[lev][j];
2050Sstevel@tonic-gate if (p == eof) {
2060Sstevel@tonic-gate c = outp - &outbuff[0];
207*5331Samw if (write(outfile, &outbuff[0], c)
208*5331Samw != c) {
209*5331Samw wrerr: (void) fprintf(stderr,
210*5331Samw gettext(
211*5331Samw "%s: %s: write error: "),
212*5331Samw argv0, argvk);
2130Sstevel@tonic-gate perror("");
2140Sstevel@tonic-gate return (0);
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate origsize -= c;
2170Sstevel@tonic-gate if (origsize != 0)
2180Sstevel@tonic-gate goto uggh;
2190Sstevel@tonic-gate return (1);
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate *outp++ = *p;
2220Sstevel@tonic-gate if (outp == &outbuff[BUFSIZ]) {
2230Sstevel@tonic-gate if (write(outfile, outp = &outbuff[0],
224*5331Samw BUFSIZ) != BUFSIZ)
2250Sstevel@tonic-gate goto wrerr;
2260Sstevel@tonic-gate origsize -= BUFSIZ;
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate lev = 1;
2290Sstevel@tonic-gate i = 0;
2300Sstevel@tonic-gate } else
2310Sstevel@tonic-gate lev++;
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate return (1); /* we won't get here , but lint is pleased */
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate int
main(int argc,char * argv[])2380Sstevel@tonic-gate main(int argc, char *argv[])
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate extern int optind;
2410Sstevel@tonic-gate int i, k;
242789Sahrens int error;
2430Sstevel@tonic-gate int sep, errflg = 0, pcat = 0;
2440Sstevel@tonic-gate register char *p1, *cp;
2450Sstevel@tonic-gate int fcount = 0; /* failure count */
2460Sstevel@tonic-gate int max_name;
2470Sstevel@tonic-gate void onsig(int);
2481620Smarks acl_t *aclp = NULL;
249*5331Samw int c;
250*5331Samw char *progname;
251*5331Samw int sattr_exist = 0;
252*5331Samw int xattr_exist = 0;
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
2550Sstevel@tonic-gate #ifdef __STDC__
256*5331Samw (void) signal((int)SIGHUP, onsig);
2570Sstevel@tonic-gate #else
258*5331Samw (void) signal((int)SIGHUP, onsig);
2590Sstevel@tonic-gate #endif
2600Sstevel@tonic-gate if (signal(SIGINT, SIG_IGN) != SIG_IGN)
2610Sstevel@tonic-gate #ifdef __STDC__
262*5331Samw (void) signal((int)SIGINT, onsig);
2630Sstevel@tonic-gate #else
264*5331Samw (void) signal((int)SIGINT, onsig);
2650Sstevel@tonic-gate #endif
2660Sstevel@tonic-gate if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
2670Sstevel@tonic-gate #ifdef __STDC__
268*5331Samw (void) signal((int)SIGTERM, onsig);
2690Sstevel@tonic-gate #else
270*5331Samw (void) signal(SIGTERM, onsig);
2710Sstevel@tonic-gate #endif
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
2740Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
2750Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
2760Sstevel@tonic-gate #endif
2770Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
2780Sstevel@tonic-gate
279*5331Samw if (progname = strrchr(argv[0], '/'))
280*5331Samw ++progname;
281*5331Samw else
282*5331Samw progname = argv[0];
283*5331Samw
2840Sstevel@tonic-gate p1 = *argv;
285*5331Samw while (*p1++) { }; /* Point p1 to end of argv[0] string */
2860Sstevel@tonic-gate while (--p1 >= *argv)
2870Sstevel@tonic-gate if (*p1 == '/')break;
2880Sstevel@tonic-gate *argv = p1 + 1;
2890Sstevel@tonic-gate argv0 = argv[0];
2900Sstevel@tonic-gate if (**argv == 'p')pcat++; /* User entered pcat (or /xx/xx/pcat) */
2910Sstevel@tonic-gate
292*5331Samw while ((c = getopt(argc, argv, "/")) != EOF) {
293*5331Samw if (c == '/') {
294*5331Samw if (pcat)
295*5331Samw ++errflg;
296*5331Samw else
297*5331Samw saflg++;
298*5331Samw } else
299*5331Samw ++errflg;
300*5331Samw }
3010Sstevel@tonic-gate /*
3020Sstevel@tonic-gate * Check for invalid option. Also check for missing
3030Sstevel@tonic-gate * file operand, ie: "unpack" or "pcat".
3040Sstevel@tonic-gate */
3050Sstevel@tonic-gate argc -= optind;
3060Sstevel@tonic-gate argv = &argv[optind];
3070Sstevel@tonic-gate if (errflg || argc < 1) {
308*5331Samw if (!pcat)
309*5331Samw (void) fprintf(stderr,
310*5331Samw gettext("usage: %s [-/] file...\n"), argv0);
311*5331Samw else
312*5331Samw (void) fprintf(stderr, gettext("usage: %s file...\n"),
313*5331Samw argv0);
314*5331Samw
3150Sstevel@tonic-gate if (argc < 1) {
3160Sstevel@tonic-gate /*
3170Sstevel@tonic-gate * return 1 for usage error when no file was specified
3180Sstevel@tonic-gate */
3190Sstevel@tonic-gate return (1);
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate }
3220Sstevel@tonic-gate /* loop through the file names */
3230Sstevel@tonic-gate for (k = 0; k < argc; k++) {
3240Sstevel@tonic-gate fcount++; /* expect the worst */
3250Sstevel@tonic-gate if (errflg) {
3260Sstevel@tonic-gate /*
3270Sstevel@tonic-gate * invalid option; just count the number of files not
3280Sstevel@tonic-gate * unpacked
3290Sstevel@tonic-gate */
3300Sstevel@tonic-gate continue;
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate /* remove any .z suffix the user may have added */
3330Sstevel@tonic-gate for (cp = argv[k]; *cp != '\0'; ++cp)
3340Sstevel@tonic-gate ;
3350Sstevel@tonic-gate if (cp[-1] == SUF1 && cp[-2] == SUF0) {
3360Sstevel@tonic-gate *cp-- = '\0'; *cp-- = '\0'; *cp = '\0';
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate sep = -1;
3390Sstevel@tonic-gate cp = filename;
3400Sstevel@tonic-gate argvk = argv[k];
3410Sstevel@tonic-gate /* copy argv[k] to filename and count chars in base name */
3420Sstevel@tonic-gate for (i = 0; i < (MAXPATHLEN-3) && (*cp = argvk[i]); i++)
3430Sstevel@tonic-gate if (*cp++ == '/')
3440Sstevel@tonic-gate sep = i;
3450Sstevel@tonic-gate /* add .z suffix to filename */
3460Sstevel@tonic-gate *cp++ = SUF0;
3470Sstevel@tonic-gate *cp++ = SUF1;
3480Sstevel@tonic-gate *cp = '\0';
3490Sstevel@tonic-gate if ((infile = open(filename, O_RDONLY)) == -1) {
3500Sstevel@tonic-gate (void) fprintf(stderr, gettext(
351*5331Samw "%s: %s: cannot open: "),
352*5331Samw argv0, filename);
3530Sstevel@tonic-gate perror("");
3540Sstevel@tonic-gate goto done;
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate if (pcat)
3570Sstevel@tonic-gate outfile = 1; /* standard output */
3580Sstevel@tonic-gate else {
359789Sahrens
360789Sahrens error = facl_get(infile, ACL_NO_TRIVIAL, &aclp);
361789Sahrens if (error != 0) {
362789Sahrens (void) printf(gettext(
363789Sahrens "%s: %s: cannot retrieve ACL : %s\n"),
364*5331Samw argv0, filename, acl_strerror(error));
365789Sahrens }
366789Sahrens
3670Sstevel@tonic-gate max_name = pathconf(filename, _PC_NAME_MAX);
3680Sstevel@tonic-gate if (max_name == -1) {
3690Sstevel@tonic-gate /* no limit on length of filename */
3700Sstevel@tonic-gate max_name = _POSIX_NAME_MAX;
3710Sstevel@tonic-gate }
3720Sstevel@tonic-gate if (i >= (MAXPATHLEN-1) || (i - sep - 1) > max_name) {
3730Sstevel@tonic-gate (void) fprintf(stderr, gettext(
374*5331Samw "%s: %s: file name too long\n"),
375*5331Samw argv0, argvk);
3760Sstevel@tonic-gate goto done;
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate if (stat(argvk, &status) != -1) {
3790Sstevel@tonic-gate (void) fprintf(stderr, gettext(
380*5331Samw "%s: %s: already exists\n"),
381*5331Samw argv0, argvk);
3820Sstevel@tonic-gate goto done;
3830Sstevel@tonic-gate }
3840Sstevel@tonic-gate (void) fstat(infile, &status);
3850Sstevel@tonic-gate if (status.st_nlink != 1) {
3860Sstevel@tonic-gate (void) printf(gettext(
387*5331Samw "%s: %s: Warning: file has links\n"),
388*5331Samw argv0, filename);
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate if ((outfile = creat(argvk, status.st_mode)) == -1) {
3910Sstevel@tonic-gate (void) fprintf(stderr, gettext(
392*5331Samw "%s: %s: cannot create: "),
393*5331Samw argv0, argvk);
3940Sstevel@tonic-gate perror("");
3950Sstevel@tonic-gate goto done;
3960Sstevel@tonic-gate }
3970Sstevel@tonic-gate rmflg = 1;
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate
400*5331Samw if (getdict()) { /* unpack */
401*5331Samw if (pathconf(filename, _PC_XATTR_EXISTS) == 1)
402*5331Samw xattr_exist = 1;
403*5331Samw if (saflg && sysattr_support(filename,
404*5331Samw _PC_SATTR_EXISTS) == 1)
405*5331Samw sattr_exist = 1;
406*5331Samw if (pcat || xattr_exist || sattr_exist) {
407*5331Samw if (mv_xattrs(progname, filename, argv[k],
408*5331Samw sattr_exist, 0)
409*5331Samw != 0) {
410*5331Samw /* Move attributes back ... */
411*5331Samw xattr_exist = 0;
412*5331Samw sattr_exist = 0;
413*5331Samw if (pathconf(argvk, _PC_XATTR_EXISTS)
414*5331Samw == 1)
415*5331Samw xattr_exist = 1;
416*5331Samw if (saflg && sysattr_support(argvk,
417*5331Samw _PC_SATTR_EXISTS) == 1)
418*5331Samw sattr_exist = 1;
419*5331Samw if (!pcat && (xattr_exist ||
420*5331Samw sattr_exist)) {
421*5331Samw (void) mv_xattrs(progname,
422*5331Samw argv[k], filename,
423*5331Samw sattr_exist, 1);
424*5331Samw (void) unlink(argvk);
425*5331Samw goto done;
426*5331Samw }
427*5331Samw } else {
428*5331Samw if (!pcat)
429*5331Samw (void) unlink(filename);
430*5331Samw }
431*5331Samw } else if (!pcat)
432*5331Samw (void) unlink(filename);
433*5331Samw
4340Sstevel@tonic-gate if (!pcat) {
435*5331Samw (void) printf(gettext("%s: %s: unpacked\n"),
436*5331Samw argv0, argvk);
4370Sstevel@tonic-gate /*
4380Sstevel@tonic-gate * preserve acc & mod dates
4390Sstevel@tonic-gate */
4400Sstevel@tonic-gate u_times.actime = status.st_atime;
4410Sstevel@tonic-gate u_times.modtime = status.st_mtime;
4420Sstevel@tonic-gate if (utime(argvk, &u_times) != 0) {
4430Sstevel@tonic-gate errflg++;
4440Sstevel@tonic-gate (void) fprintf(stderr, gettext(
445*5331Samw "%s: cannot change times on %s: "),
446*5331Samw argv0, argvk);
4470Sstevel@tonic-gate perror("");
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate if (chmod(argvk, status.st_mode) != 0) {
4500Sstevel@tonic-gate errflg++;
4510Sstevel@tonic-gate (void) fprintf(stderr, gettext(
4520Sstevel@tonic-gate "%s: cannot change mode to %o on %s: "),
4530Sstevel@tonic-gate argv0, (uint_t)status.st_mode,
4540Sstevel@tonic-gate argvk);
4550Sstevel@tonic-gate perror("");
4560Sstevel@tonic-gate }
4570Sstevel@tonic-gate (void) chown(argvk,
458*5331Samw status.st_uid, status.st_gid);
459789Sahrens if (aclp && (facl_set(outfile, aclp) < 0)) {
460789Sahrens (void) printf(gettext("%s: cannot "
461789Sahrens "set ACL on %s: "), argv0, argvk);
462789Sahrens perror("");
463789Sahrens }
464789Sahrens
4650Sstevel@tonic-gate rmflg = 0;
4660Sstevel@tonic-gate }
4670Sstevel@tonic-gate if (!errflg)
4680Sstevel@tonic-gate fcount--; /* success after all */
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate done: (void) close(infile);
4710Sstevel@tonic-gate if (!pcat)
4720Sstevel@tonic-gate (void) close(outfile);
473789Sahrens
4741620Smarks if (aclp) {
475789Sahrens acl_free(aclp);
4761620Smarks aclp = NULL;
4771620Smarks }
4780Sstevel@tonic-gate }
4790Sstevel@tonic-gate return (fcount);
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate
4820Sstevel@tonic-gate /*
4830Sstevel@tonic-gate * This code is for unpacking files that
4840Sstevel@tonic-gate * were packed using the previous algorithm.
4850Sstevel@tonic-gate */
4860Sstevel@tonic-gate
4870Sstevel@tonic-gate static int Tree[1024];
4880Sstevel@tonic-gate
4890Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */
4900Sstevel@tonic-gate
4910Sstevel@tonic-gate int
expand()4920Sstevel@tonic-gate expand()
4930Sstevel@tonic-gate {
4940Sstevel@tonic-gate int tp, bit;
4950Sstevel@tonic-gate short word;
4960Sstevel@tonic-gate int keysize, i, *t;
4970Sstevel@tonic-gate
4980Sstevel@tonic-gate outp = outbuff;
4990Sstevel@tonic-gate inp = &inbuff[2];
5000Sstevel@tonic-gate inleft -= 2;
5010Sstevel@tonic-gate
5020Sstevel@tonic-gate origsize = ((long)(unsigned)getwdsize())*256*256;
5030Sstevel@tonic-gate origsize += (unsigned)getwdsize();
5040Sstevel@tonic-gate if (origsize == 0 || is_eof) {
5050Sstevel@tonic-gate (void) fprintf(stderr, gettext(
506*5331Samw "%s: %s: not in packed format\n"),
507*5331Samw argv0, filename);
5080Sstevel@tonic-gate return (0);
5090Sstevel@tonic-gate }
5100Sstevel@tonic-gate t = Tree;
5110Sstevel@tonic-gate for (keysize = getwdsize(); keysize--; ) {
5120Sstevel@tonic-gate if ((i = getch()) == 0377)
5130Sstevel@tonic-gate *t++ = getwdsize();
5140Sstevel@tonic-gate else {
5150Sstevel@tonic-gate /*
5160Sstevel@tonic-gate * reached EOF unexpectedly
5170Sstevel@tonic-gate */
5180Sstevel@tonic-gate if (is_eof) {
5190Sstevel@tonic-gate (void) fprintf(stderr, gettext(
520*5331Samw "%s: %s: not in packed format\n"),
521*5331Samw argv0, filename);
5220Sstevel@tonic-gate return (0);
5230Sstevel@tonic-gate }
5240Sstevel@tonic-gate *t++ = i & 0377;
5250Sstevel@tonic-gate }
5260Sstevel@tonic-gate }
5270Sstevel@tonic-gate /*
5280Sstevel@tonic-gate * reached EOF unexpectedly
5290Sstevel@tonic-gate */
5300Sstevel@tonic-gate if (is_eof) {
5310Sstevel@tonic-gate (void) fprintf(stderr, gettext(
532*5331Samw "%s: %s: not in packed format\n"),
533*5331Samw argv0, filename);
5340Sstevel@tonic-gate return (0);
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate
5370Sstevel@tonic-gate
5380Sstevel@tonic-gate bit = tp = 0;
5390Sstevel@tonic-gate for (;;) {
5400Sstevel@tonic-gate if (bit <= 0) {
5410Sstevel@tonic-gate word = getwdsize();
5420Sstevel@tonic-gate /*
5430Sstevel@tonic-gate * reached EOF unexpectedly
5440Sstevel@tonic-gate */
5450Sstevel@tonic-gate if (word == 0 && is_eof && origsize > 0) {
5460Sstevel@tonic-gate (void) fprintf(stderr, gettext(
547*5331Samw "%s: %s: not in packed format\n"),
548*5331Samw argv0, filename);
5490Sstevel@tonic-gate return (0);
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate bit = 16;
5520Sstevel@tonic-gate }
5530Sstevel@tonic-gate tp += Tree[tp + (word < 0)];
5540Sstevel@tonic-gate word <<= 1;
5550Sstevel@tonic-gate bit--;
5560Sstevel@tonic-gate if (Tree[tp] == 0) {
5570Sstevel@tonic-gate putch(Tree[tp+1]);
5580Sstevel@tonic-gate tp = 0;
5590Sstevel@tonic-gate if ((origsize -= 1) == 0) {
5600Sstevel@tonic-gate (void) write(outfile, outbuff, outp - outbuff);
5610Sstevel@tonic-gate return (1);
5620Sstevel@tonic-gate }
5630Sstevel@tonic-gate }
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate
5670Sstevel@tonic-gate int
getch()5680Sstevel@tonic-gate getch()
5690Sstevel@tonic-gate {
5700Sstevel@tonic-gate if (inleft <= 0) {
5710Sstevel@tonic-gate inleft = read(infile, inp = inbuff, BUFSIZ);
5720Sstevel@tonic-gate if (inleft < 0) {
5730Sstevel@tonic-gate (void) fprintf(stderr, gettext(
574*5331Samw "%s: %s: read error: "),
575*5331Samw argv0, filename);
5760Sstevel@tonic-gate perror("");
5770Sstevel@tonic-gate longjmp(env, 1);
5780Sstevel@tonic-gate } else { /* reached EOF, report it */
5790Sstevel@tonic-gate if (inleft == 0) {
5800Sstevel@tonic-gate is_eof = 1;
5810Sstevel@tonic-gate return (EOF);
5820Sstevel@tonic-gate }
5830Sstevel@tonic-gate }
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate inleft--;
5860Sstevel@tonic-gate return (*inp++ & 0377);
5870Sstevel@tonic-gate }
5880Sstevel@tonic-gate
5890Sstevel@tonic-gate int
getwdsize()5900Sstevel@tonic-gate getwdsize()
5910Sstevel@tonic-gate {
5920Sstevel@tonic-gate char c;
5930Sstevel@tonic-gate int d;
5940Sstevel@tonic-gate
5950Sstevel@tonic-gate c = getch();
5960Sstevel@tonic-gate d = getch();
5970Sstevel@tonic-gate if (is_eof)
5980Sstevel@tonic-gate return (0);
5990Sstevel@tonic-gate d <<= 8;
6000Sstevel@tonic-gate d |= c & 0377;
6010Sstevel@tonic-gate return (d);
6020Sstevel@tonic-gate }
6030Sstevel@tonic-gate
6040Sstevel@tonic-gate void
onsig(int sig)6050Sstevel@tonic-gate onsig(int sig)
6060Sstevel@tonic-gate {
6070Sstevel@tonic-gate /* could be running as unpack or pcat */
6080Sstevel@tonic-gate /* but rmflg is set only when running */
6090Sstevel@tonic-gate /* as unpack and only when file is */
6100Sstevel@tonic-gate /* created by unpack and not yet done */
6110Sstevel@tonic-gate if (rmflg == 1)
6120Sstevel@tonic-gate (void) unlink(argvk);
613*5331Samw /* To quiet lint noise */
614*5331Samw if (sig == SIGTERM || sig == SIGHUP || sig == SIGINT)
615*5331Samw exit(1);
6160Sstevel@tonic-gate }
6170Sstevel@tonic-gate
6180Sstevel@tonic-gate void
putch(char c)6190Sstevel@tonic-gate putch(char c)
6200Sstevel@tonic-gate {
6210Sstevel@tonic-gate int n;
6220Sstevel@tonic-gate
6230Sstevel@tonic-gate *outp++ = c;
6240Sstevel@tonic-gate if (outp == &outbuff[BUFSIZ]) {
6250Sstevel@tonic-gate n = write(outfile, outp = outbuff, BUFSIZ);
6260Sstevel@tonic-gate if (n < BUFSIZ) {
6270Sstevel@tonic-gate (void) fprintf(stderr, gettext(
628*5331Samw "%s: %s: write error: "),
629*5331Samw argv0, argvk);
6300Sstevel@tonic-gate perror("");
6310Sstevel@tonic-gate longjmp(env, 2);
6320Sstevel@tonic-gate }
6330Sstevel@tonic-gate }
6340Sstevel@tonic-gate }
635