xref: /onnv-gate/usr/src/cmd/pack/pack.c (revision 5331:3047ad28a67b)
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 /*
22*5331Samw  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
29212Scf46844 #pragma ident	"%Z%%M%	%I%	%E% SMI"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  *	Huffman encoding program
33*5331Samw  *	Usage:	pack [[ -f ] [ - ] [-/] filename ... ] filename ...
340Sstevel@tonic-gate  *		- option: enable/disable listing of statistics
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <locale.h>
380Sstevel@tonic-gate #include <stdarg.h>
390Sstevel@tonic-gate #include <sys/isa_defs.h>
400Sstevel@tonic-gate #include <sys/param.h>
410Sstevel@tonic-gate #include <utime.h>
42789Sahrens #include <sys/acl.h>
43789Sahrens #include <aclutils.h>
44*5331Samw #include <libcmdutils.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #undef lint
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #define	END	256
490Sstevel@tonic-gate #define	PACKED 017436 /* <US><RS> - Unlikely value */
500Sstevel@tonic-gate #define	SUF0	'.'
510Sstevel@tonic-gate #define	SUF1	'z'
520Sstevel@tonic-gate 
530Sstevel@tonic-gate struct stat status, ostatus;
540Sstevel@tonic-gate static struct utimbuf u_times;
550Sstevel@tonic-gate 
560Sstevel@tonic-gate /* union for overlaying a long int with a set of four characters */
570Sstevel@tonic-gate union FOUR {
580Sstevel@tonic-gate 	struct { long int lng; } lint;
590Sstevel@tonic-gate 	struct { char c0, c1, c2, c3; } chars;
600Sstevel@tonic-gate };
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /* character counters */
630Sstevel@tonic-gate long	count [END+1];
640Sstevel@tonic-gate union	FOUR insize;
650Sstevel@tonic-gate long	outsize;
660Sstevel@tonic-gate long	dictsize;
670Sstevel@tonic-gate int	diffbytes;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /* i/o stuff */
700Sstevel@tonic-gate char	vflag = 0;
710Sstevel@tonic-gate int	force = 0;	/* allow forced packing for consistency in directory */
720Sstevel@tonic-gate 
730Sstevel@tonic-gate static	char filename [MAXPATHLEN];
740Sstevel@tonic-gate static int max_name;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate int	infile;		/* unpacked file */
770Sstevel@tonic-gate int	outfile;	/* packed file */
780Sstevel@tonic-gate char	inbuff [BUFSIZ];
790Sstevel@tonic-gate char	outbuff [BUFSIZ+4];
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /* variables associated with the tree */
820Sstevel@tonic-gate int	maxlev;
830Sstevel@tonic-gate int	levcount [25];
840Sstevel@tonic-gate int	lastnode;
850Sstevel@tonic-gate int	parent [2*END+1];
860Sstevel@tonic-gate 
870Sstevel@tonic-gate /* variables associated with the encoding process */
880Sstevel@tonic-gate char	length [END+1];
890Sstevel@tonic-gate long	bits [END+1];
900Sstevel@tonic-gate union	FOUR mask;
910Sstevel@tonic-gate long	inc;
920Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN)
930Sstevel@tonic-gate char	*maskshuff[4]  = {&(mask.chars.c3),
940Sstevel@tonic-gate 			    &(mask.chars.c2),
950Sstevel@tonic-gate 			    &(mask.chars.c1),
960Sstevel@tonic-gate 			    &(mask.chars.c0)};
970Sstevel@tonic-gate #elif defined(_BIG_ENDIAN)
980Sstevel@tonic-gate char	*maskshuff[4]  = {&(mask.chars.c0),
990Sstevel@tonic-gate 			    &(mask.chars.c1),
1000Sstevel@tonic-gate 			    &(mask.chars.c2),
1010Sstevel@tonic-gate 			    &(mask.chars.c3)};
1020Sstevel@tonic-gate #else
1030Sstevel@tonic-gate #error Unknown byte ordering!
1040Sstevel@tonic-gate #endif
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate /* the heap */
1070Sstevel@tonic-gate int	n;
1080Sstevel@tonic-gate struct	heap {
1090Sstevel@tonic-gate 	long int count;
1100Sstevel@tonic-gate 	int node;
1110Sstevel@tonic-gate } heap [END+2];
1120Sstevel@tonic-gate #define	hmove(a, b) {(b).count = (a).count; (b).node = (a).node; }
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate static void heapify(int i);
115*5331Samw 
116*5331Samw /* Extended system attribute support */
117*5331Samw 
118*5331Samw static int saflg = 0;
119*5331Samw 
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate /* gather character frequency statistics */
1220Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */
123212Scf46844 int
input(char * source)1240Sstevel@tonic-gate input(char *source)
1250Sstevel@tonic-gate {
1260Sstevel@tonic-gate 	register int i;
1270Sstevel@tonic-gate 	for (i = 0; i < END; i++)
1280Sstevel@tonic-gate 		count[i] = 0;
1290Sstevel@tonic-gate 	while ((i = read(infile, inbuff, BUFSIZ)) > 0)
1300Sstevel@tonic-gate 		while (i > 0)
1310Sstevel@tonic-gate 			count[inbuff[--i]&0377] += 2;
1320Sstevel@tonic-gate 	if (i == 0)
1330Sstevel@tonic-gate 		return (1);
134*5331Samw 	(void) fprintf(stderr, gettext(
135*5331Samw 	    "pack: %s: read error - file unchanged: "), source);
1360Sstevel@tonic-gate 	perror("");
1370Sstevel@tonic-gate 	return (0);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate /* encode the current file */
1410Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */
142212Scf46844 int
output(char * source)1430Sstevel@tonic-gate output(char *source)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate 	int c, i, inleft;
1460Sstevel@tonic-gate 	char *inp;
1470Sstevel@tonic-gate 	register char **q, *outp;
1480Sstevel@tonic-gate 	register int bitsleft;
1490Sstevel@tonic-gate 	long temp;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	/* output ``PACKED'' header */
1520Sstevel@tonic-gate 	outbuff[0] = 037; 	/* ascii US */
1530Sstevel@tonic-gate 	outbuff[1] = 036; 	/* ascii RS */
1540Sstevel@tonic-gate 	/* output the length and the dictionary */
1550Sstevel@tonic-gate 	temp = insize.lint.lng;
1560Sstevel@tonic-gate 	for (i = 5; i >= 2; i--) {
1570Sstevel@tonic-gate 		outbuff[i] =  (char)(temp & 0377);
1580Sstevel@tonic-gate 		temp >>= 8;
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 	outp = &outbuff[6];
1610Sstevel@tonic-gate 	*outp++ = maxlev;
1620Sstevel@tonic-gate 	for (i = 1; i < maxlev; i++)
1630Sstevel@tonic-gate 		*outp++ = levcount[i];
1640Sstevel@tonic-gate 	*outp++ = levcount[maxlev]-2;
1650Sstevel@tonic-gate 	for (i = 1; i <= maxlev; i++)
1660Sstevel@tonic-gate 		for (c = 0; c < END; c++)
1670Sstevel@tonic-gate 			if (length[c] == i)
1680Sstevel@tonic-gate 				*outp++ = c;
1690Sstevel@tonic-gate 	dictsize = outp-&outbuff[0];
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	/* output the text */
172*5331Samw 	(void) lseek(infile, 0L, 0);
1730Sstevel@tonic-gate 	outsize = 0;
1740Sstevel@tonic-gate 	bitsleft = 8;
1750Sstevel@tonic-gate 	inleft = 0;
1760Sstevel@tonic-gate 	do {
1770Sstevel@tonic-gate 		if (inleft <= 0) {
1780Sstevel@tonic-gate 			inleft = read(infile, inp = &inbuff[0], BUFSIZ);
1790Sstevel@tonic-gate 			if (inleft < 0) {
180*5331Samw 				(void) fprintf(stderr, gettext(
1810Sstevel@tonic-gate 				    "pack: %s: read error - file unchanged: "),
182*5331Samw 				    source);
1830Sstevel@tonic-gate 				perror("");
1840Sstevel@tonic-gate 				return (0);
1850Sstevel@tonic-gate 			}
1860Sstevel@tonic-gate 		}
1870Sstevel@tonic-gate 		c = (--inleft < 0) ? END : (*inp++ & 0377);
1880Sstevel@tonic-gate 		mask.lint.lng = bits[c]<<bitsleft;
1890Sstevel@tonic-gate 		q = &maskshuff[0];
1900Sstevel@tonic-gate 		if (bitsleft == 8)
1910Sstevel@tonic-gate 			*outp = **q++;
1920Sstevel@tonic-gate 		else
1930Sstevel@tonic-gate 			*outp |= **q++;
1940Sstevel@tonic-gate 		bitsleft -= length[c];
1950Sstevel@tonic-gate 		while (bitsleft < 0) {
1960Sstevel@tonic-gate 			*++outp = **q++;
1970Sstevel@tonic-gate 			bitsleft += 8;
1980Sstevel@tonic-gate 		}
1990Sstevel@tonic-gate 		if (outp >= &outbuff[BUFSIZ]) {
2000Sstevel@tonic-gate 			if (write(outfile, outbuff, BUFSIZ) != BUFSIZ) {
201*5331Samw wrerr:				(void) fprintf(stderr, gettext(
202*5331Samw 				    "pack: %s.z: write error - "
203*5331Samw 				    "file unchanged: "), source);
2040Sstevel@tonic-gate 				perror("");
2050Sstevel@tonic-gate 				return (0);
2060Sstevel@tonic-gate 			}
2070Sstevel@tonic-gate 			((union FOUR *)outbuff)->lint.lng =
208*5331Samw 			    ((union FOUR *)&outbuff[BUFSIZ])->lint.lng;
2090Sstevel@tonic-gate 			outp -= BUFSIZ;
2100Sstevel@tonic-gate 			outsize += BUFSIZ;
2110Sstevel@tonic-gate 		}
2120Sstevel@tonic-gate 	} while (c != END);
2130Sstevel@tonic-gate 	if (bitsleft < 8)
2140Sstevel@tonic-gate 		outp++;
2150Sstevel@tonic-gate 	c = outp-outbuff;
2160Sstevel@tonic-gate 	if (write(outfile, outbuff, c) != c)
2170Sstevel@tonic-gate 		goto wrerr;
2180Sstevel@tonic-gate 	outsize += c;
2190Sstevel@tonic-gate 	return (1);
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate /* makes a heap out of heap[i],...,heap[n] */
2230Sstevel@tonic-gate void
heapify(int i)2240Sstevel@tonic-gate heapify(int i)
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate 	register int k;
2270Sstevel@tonic-gate 	int lastparent;
2280Sstevel@tonic-gate 	struct heap heapsubi;
2290Sstevel@tonic-gate 	hmove(heap[i], heapsubi);
2300Sstevel@tonic-gate 	lastparent = n/2;
2310Sstevel@tonic-gate 	while (i <= lastparent) {
2320Sstevel@tonic-gate 		k = 2*i;
2330Sstevel@tonic-gate 		if (heap[k].count > heap[k+1].count && k < n)
2340Sstevel@tonic-gate 			k++;
2350Sstevel@tonic-gate 		if (heapsubi.count < heap[k].count)
2360Sstevel@tonic-gate 			break;
2370Sstevel@tonic-gate 		hmove(heap[k], heap[i]);
2380Sstevel@tonic-gate 		i = k;
2390Sstevel@tonic-gate 	}
2400Sstevel@tonic-gate 	hmove(heapsubi, heap[i]);
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate /* return 1 after successful packing, 0 otherwise */
2440Sstevel@tonic-gate int
packfile(char * source)2450Sstevel@tonic-gate packfile(char *source)
2460Sstevel@tonic-gate {
2470Sstevel@tonic-gate 	register int c, i, p;
2480Sstevel@tonic-gate 	long bitsout;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	/* gather frequency statistics */
2510Sstevel@tonic-gate 	if (input(source) == 0)
2520Sstevel@tonic-gate 		return (0);
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	/* put occurring chars in heap with their counts */
2550Sstevel@tonic-gate 	diffbytes = -1;
2560Sstevel@tonic-gate 	count[END] = 1;
2570Sstevel@tonic-gate 	insize.lint.lng = n = 0;
2580Sstevel@tonic-gate 	for (i = END; i >= 0; i--) {
2590Sstevel@tonic-gate 		parent[i] = 0;
2600Sstevel@tonic-gate 		if (count[i] > 0) {
2610Sstevel@tonic-gate 			diffbytes++;
2620Sstevel@tonic-gate 			insize.lint.lng += count[i];
2630Sstevel@tonic-gate 			heap[++n].count = count[i];
2640Sstevel@tonic-gate 			heap[n].node = i;
2650Sstevel@tonic-gate 		}
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate 	if (diffbytes == 1) {
268*5331Samw 		(void) fprintf(stderr, gettext(
269*5331Samw 		    "pack: %s: trivial file - file unchanged\n"), source);
2700Sstevel@tonic-gate 		return (0);
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 	insize.lint.lng >>= 1;
2730Sstevel@tonic-gate 	for (i = n/2; i >= 1; i--)
2740Sstevel@tonic-gate 		heapify(i);
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	/* build Huffman tree */
2770Sstevel@tonic-gate 	lastnode = END;
2780Sstevel@tonic-gate 	while (n > 1) {
2790Sstevel@tonic-gate 		parent[heap[1].node] = ++lastnode;
2800Sstevel@tonic-gate 		inc = heap[1].count;
2810Sstevel@tonic-gate 		hmove(heap[n], heap[1]);
2820Sstevel@tonic-gate 		n--;
2830Sstevel@tonic-gate 		heapify(1);
2840Sstevel@tonic-gate 		parent[heap[1].node] = lastnode;
2850Sstevel@tonic-gate 		heap[1].node = lastnode;
2860Sstevel@tonic-gate 		heap[1].count += inc;
2870Sstevel@tonic-gate 		heapify(1);
2880Sstevel@tonic-gate 	}
2890Sstevel@tonic-gate 	parent[lastnode] = 0;
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	/* assign lengths to encoding for each character */
2920Sstevel@tonic-gate 	bitsout = maxlev = 0;
2930Sstevel@tonic-gate 	for (i = 1; i <= 24; i++)
2940Sstevel@tonic-gate 		levcount[i] = 0;
2950Sstevel@tonic-gate 	for (i = 0; i <= END; i++) {
2960Sstevel@tonic-gate 		c = 0;
2970Sstevel@tonic-gate 		for (p = parent[i]; p != 0; p = parent[p])
2980Sstevel@tonic-gate 			c++;
2990Sstevel@tonic-gate 		levcount[c]++;
3000Sstevel@tonic-gate 		length[i] = c;
3010Sstevel@tonic-gate 		if (c > maxlev)
3020Sstevel@tonic-gate 			maxlev = c;
3030Sstevel@tonic-gate 		bitsout += c*(count[i]>>1);
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate 	if (maxlev > 24) {
3060Sstevel@tonic-gate 		/* can't occur unless insize.lint.lng >= 2**24 */
307*5331Samw 		(void) fprintf(stderr, gettext(
308*5331Samw 		    "pack: %s: Huffman tree has too many levels - "
309*5331Samw 		    "file unchanged\n"), source);
3100Sstevel@tonic-gate 		return (0);
3110Sstevel@tonic-gate 	}
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	/* don't bother if no compression results */
3140Sstevel@tonic-gate 	outsize = ((bitsout+7)>>3)+6+maxlev+diffbytes;
3150Sstevel@tonic-gate 	if ((insize.lint.lng+BUFSIZ-1)/BUFSIZ <=
316*5331Samw 	    (outsize+BUFSIZ-1)/BUFSIZ && !force) {
317*5331Samw 		(void) printf(gettext(
318*5331Samw 		    "pack: %s: no saving - file unchanged\n"), source);
3190Sstevel@tonic-gate 		return (0);
3200Sstevel@tonic-gate 	}
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	/* compute bit patterns for each character */
3230Sstevel@tonic-gate 	inc = 1L << 24;
3240Sstevel@tonic-gate 	inc >>= maxlev;
3250Sstevel@tonic-gate 	mask.lint.lng = 0;
3260Sstevel@tonic-gate 	for (i = maxlev; i > 0; i--) {
3270Sstevel@tonic-gate 		for (c = 0; c <= END; c++)
3280Sstevel@tonic-gate 			if (length[c] == i) {
3290Sstevel@tonic-gate 				bits[c] = mask.lint.lng;
3300Sstevel@tonic-gate 				mask.lint.lng += inc;
3310Sstevel@tonic-gate 			}
3320Sstevel@tonic-gate 		mask.lint.lng &= ~inc;
3330Sstevel@tonic-gate 		inc <<= 1;
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	return (output(source));
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate 
339212Scf46844 int
main(int argc,char * argv[])3400Sstevel@tonic-gate main(int argc, char *argv[])
3410Sstevel@tonic-gate {
3420Sstevel@tonic-gate 	extern  int optind;
3430Sstevel@tonic-gate 	register int i;
3440Sstevel@tonic-gate 	register char *cp;
3450Sstevel@tonic-gate 	int k, sep, errflg = 0;
3460Sstevel@tonic-gate 	int c;
347789Sahrens 	int error;
3480Sstevel@tonic-gate 	int fcount = 0; /* count failures */
349789Sahrens 	acl_t *aclp = NULL;
350*5331Samw 	char *progname;
351*5331Samw 	int sattr_exist = 0;
352*5331Samw 	int xattr_exist = 0;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3550Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
3560Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
3570Sstevel@tonic-gate #endif
3580Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3590Sstevel@tonic-gate 
360*5331Samw 	if (progname = strrchr(argv[0], '/'))
361*5331Samw 		++progname;
362*5331Samw 	else
363*5331Samw 		progname = argv[0];
364*5331Samw 
365*5331Samw 	while ((c = getopt(argc, argv, "f-/")) != EOF) {
3660Sstevel@tonic-gate 		if (c == 'f')
3670Sstevel@tonic-gate 			force++;
368*5331Samw 		else if (c == '/')
369*5331Samw 			saflg++;
3700Sstevel@tonic-gate 		else
3710Sstevel@tonic-gate 			++errflg;
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate 	/*
3740Sstevel@tonic-gate 	 * Check for invalid option.  Also check for missing
3750Sstevel@tonic-gate 	 * file operand, ie: "pack" or "pack -".
3760Sstevel@tonic-gate 	 */
3770Sstevel@tonic-gate 	argc -= optind;
3780Sstevel@tonic-gate 	argv = &argv[optind];
3790Sstevel@tonic-gate 	if (errflg || argc < 1 ||
380*5331Samw 	    (argc == 1 && (argv[0][0] == '-' || argv[0][0] == '/' &&
381*5331Samw 	    argv[0][1] == '\0'))) {
382*5331Samw 		(void) fprintf(stderr, gettext(
383*5331Samw 		    "usage: pack [-f] [-] [-/] file...\n"));
3840Sstevel@tonic-gate 		if (argc < 1 ||
385*5331Samw 		    (argc == 1 && (argv[0][0] == '-' || argv[0][0] == '/') &&
386*5331Samw 		    argv[0][1] == '\0')) {
3870Sstevel@tonic-gate 			/*
3880Sstevel@tonic-gate 			 * return 1 for usage error when no file was specified
3890Sstevel@tonic-gate 			 */
3900Sstevel@tonic-gate 			return (1);
3910Sstevel@tonic-gate 		}
3920Sstevel@tonic-gate 	}
393*5331Samw 
3940Sstevel@tonic-gate 	/* loop through the file names */
3950Sstevel@tonic-gate 	for (k = 0; k < argc; k++) {
3960Sstevel@tonic-gate 		if (argv[k][0] == '-' && argv[k][1] == '\0') {
3970Sstevel@tonic-gate 			vflag = 1 - vflag;
3980Sstevel@tonic-gate 			continue;
3990Sstevel@tonic-gate 		}
4000Sstevel@tonic-gate 		fcount++; /* increase failure count - expect the worst */
4010Sstevel@tonic-gate 		if (errflg) {
4020Sstevel@tonic-gate 			/*
4030Sstevel@tonic-gate 			 * invalid option; just count the number of files not
4040Sstevel@tonic-gate 			 * packed
4050Sstevel@tonic-gate 			 */
4060Sstevel@tonic-gate 			continue;
4070Sstevel@tonic-gate 		}
4080Sstevel@tonic-gate 		/* remove any .z suffix the user may have added */
4090Sstevel@tonic-gate 		for (cp = argv[k]; *cp != '\0'; ++cp)
4100Sstevel@tonic-gate 			;
4110Sstevel@tonic-gate 		if (cp[-1] == SUF1 && cp[-2] == SUF0) {
4120Sstevel@tonic-gate 			*cp-- = '\0'; *cp-- = '\0'; *cp = '\0';
4130Sstevel@tonic-gate 		}
4140Sstevel@tonic-gate 		sep = -1;  cp = filename;
4150Sstevel@tonic-gate 		max_name = pathconf(argv[k], _PC_NAME_MAX);
4160Sstevel@tonic-gate 		if (max_name == -1) {
4170Sstevel@tonic-gate 			/* pathname invalid or no limit on length of filename */
4180Sstevel@tonic-gate 			max_name = _POSIX_NAME_MAX;
4190Sstevel@tonic-gate 		}
4200Sstevel@tonic-gate 		/* copy argv[k] to filename and count chars in base name */
4210Sstevel@tonic-gate 		for (i = 0; i < (MAXPATHLEN-3) && (*cp = argv[k][i]); i++)
4220Sstevel@tonic-gate 			if (*cp++ == '/') sep = i;
4230Sstevel@tonic-gate 		if ((infile = open(filename, 0)) < 0) {
424*5331Samw 			(void) fprintf(stderr, gettext(
425*5331Samw 			    "pack: %s: cannot open: "), filename);
4260Sstevel@tonic-gate 			perror("");
4270Sstevel@tonic-gate 			continue;
4280Sstevel@tonic-gate 		}
4290Sstevel@tonic-gate 		if (i >= (MAXPATHLEN-3) || (i-sep) > (max_name - 1)) {
430*5331Samw 			(void) fprintf(stderr, gettext(
431*5331Samw 			    "pack: %s: file name too long\n"), argv[k]);
4320Sstevel@tonic-gate 			continue;
4330Sstevel@tonic-gate 		}
434*5331Samw 		(void) fstat(infile, &status);
435871Scasper 		if (S_ISDIR(status.st_mode)) {
436*5331Samw 			(void) fprintf(stderr, gettext(
437*5331Samw 			    "pack: %s: cannot pack a directory\n"),
438*5331Samw 			    argv[k]);
4390Sstevel@tonic-gate 			goto closein;
4400Sstevel@tonic-gate 		}
4410Sstevel@tonic-gate 		if (status.st_size == 0) {
442*5331Samw 			(void) fprintf(stderr, gettext(
443*5331Samw 			    "pack: %s: cannot pack a zero length file\n"),
444*5331Samw 			    argv[k]);
4450Sstevel@tonic-gate 			goto closein;
4460Sstevel@tonic-gate 		}
4470Sstevel@tonic-gate 		if (status.st_nlink != 1) {
448*5331Samw 			(void) fprintf(stderr, gettext(
449*5331Samw 			    "pack: %s: has links\n"),
450*5331Samw 			    argv[k]);
4510Sstevel@tonic-gate 			goto closein;
4520Sstevel@tonic-gate 		}
4530Sstevel@tonic-gate 		*cp++ = SUF0;  *cp++ = SUF1;  *cp = '\0';
4540Sstevel@tonic-gate 		if (stat(filename, &ostatus) != -1) {
455*5331Samw 			(void) fprintf(stderr, gettext(
456*5331Samw 			    "pack: %s: already exists\n"), filename);
4570Sstevel@tonic-gate 			goto closein;
4580Sstevel@tonic-gate 		}
459*5331Samw 		if ((outfile = creat(filename, status.st_mode | O_RDONLY))
460*5331Samw 		    < 0) {
461*5331Samw 			(void) fprintf(stderr, gettext(
462*5331Samw 			    "pack: %s: cannot create: "), filename);
4630Sstevel@tonic-gate 			perror("");
4640Sstevel@tonic-gate 			goto closein;
4650Sstevel@tonic-gate 		}
4660Sstevel@tonic-gate 
467789Sahrens 		error = facl_get(infile, ACL_NO_TRIVIAL, &aclp);
468789Sahrens 
469789Sahrens 		if (error != 0) {
470*5331Samw 			(void) fprintf(stderr, gettext(
471789Sahrens 			    "pack: %s: cannot retrieve ACL: %s\n"), argv[k],
472789Sahrens 			    acl_strerror(error));
473789Sahrens 		}
474*5331Samw 
475*5331Samw 		if (packfile(argv[k])) {
476*5331Samw 			if (pathconf(argv[k], _PC_XATTR_EXISTS) == 1)
477*5331Samw 				xattr_exist = 1;
478*5331Samw 			if (saflg && sysattr_support(argv[k],
479*5331Samw 			    _PC_SATTR_EXISTS) == 1)
480*5331Samw 				sattr_exist = 1;
481*5331Samw 			if (sattr_exist || xattr_exist) {
482*5331Samw 				if (mv_xattrs(progname, argv[k], filename,
483*5331Samw 				    sattr_exist, 0) != 0) {
484*5331Samw 				/* Move attributes back ... */
485*5331Samw 					xattr_exist = 0;
486*5331Samw 					sattr_exist = 0;
487*5331Samw 					if (pathconf(filename,
488*5331Samw 					    _PC_XATTR_EXISTS) == 1)
489*5331Samw 						xattr_exist = 1;
490*5331Samw 					if (saflg && sysattr_support(filename,
491*5331Samw 					    _PC_SATTR_EXISTS) == 1)
492*5331Samw 						sattr_exist = 1;
493*5331Samw 					if (xattr_exist || sattr_exist) {
494*5331Samw 						(void) mv_xattrs(progname,
495*5331Samw 						    filename, argv[k],
496*5331Samw 						    sattr_exist, 1);
497*5331Samw 						(void) unlink(filename);
498*5331Samw 						goto out;
499*5331Samw 					}
500*5331Samw 				} else {
501*5331Samw 					errno = 0;
502*5331Samw 					if (unlink(argv[k]) != 0) {
503*5331Samw 						(void) fprintf(stderr, gettext(
504*5331Samw 						    "pack: %s :cannot unlink:"),
505*5331Samw 						    argv[k]);
506*5331Samw 						if (errno == EPERM)
507*5331Samw 							perror("No permission");
508*5331Samw 						else
509*5331Samw 							perror("");
510*5331Samw 					}
511*5331Samw 				}
512*5331Samw 			} else {
513*5331Samw 				errno = 0;
514*5331Samw 				if (unlink(argv[k]) != 0) {
515*5331Samw 					(void) fprintf(stderr, gettext(
516*5331Samw 					    "pack: %s :cannot unlink"),
517*5331Samw 					    argv[k]);
518*5331Samw 					if (errno == EPERM)
519*5331Samw 						perror("No permission");
520*5331Samw 					else
521*5331Samw 						perror("");
522*5331Samw 				}
5230Sstevel@tonic-gate 			}
524*5331Samw 			(void) printf(gettext(
525*5331Samw 			    "pack: %s: %.1f%% Compression\n"),
526*5331Samw 			    argv[k],
527*5331Samw 			    ((double)(-outsize+(insize.lint.lng))/
528*5331Samw 			    (double)insize.lint.lng)*100);
5290Sstevel@tonic-gate 			/* output statistics */
5300Sstevel@tonic-gate 			if (vflag) {
531*5331Samw 				(void) printf(gettext(
532*5331Samw 				    "\tfrom %ld to %ld bytes\n"),
533*5331Samw 				    insize.lint.lng, outsize);
534*5331Samw 				(void) printf(gettext(
535*5331Samw 				    "\tHuffman tree has %d levels below "
536*5331Samw 				    "root\n"), maxlev);
537*5331Samw 				(void) printf(gettext(
538*5331Samw 				    "\t%d distinct bytes in input\n"),
539*5331Samw 				    diffbytes);
540*5331Samw 				(void) printf(gettext(
541*5331Samw 				    "\tdictionary overhead = %ld bytes\n"),
542*5331Samw 				    dictsize);
543*5331Samw 				(void) printf(gettext(
5440Sstevel@tonic-gate 				    "\teffective  entropy  = %.2f bits/byte\n"),
545*5331Samw 				    ((double)outsize / (double)insize.lint.lng)
546*5331Samw 				    * 8);
547*5331Samw 				(void) printf(gettext(
5480Sstevel@tonic-gate 				    "\tasymptotic entropy  = %.2f bits/byte\n"),
549*5331Samw 				    ((double)(outsize-dictsize) /
550*5331Samw 				    (double)insize.lint.lng) * 8);
5510Sstevel@tonic-gate 			}
552*5331Samw 
5530Sstevel@tonic-gate 			u_times.actime = status.st_atime;
5540Sstevel@tonic-gate 			u_times.modtime = status.st_mtime;
5550Sstevel@tonic-gate 			if (utime(filename, &u_times) != 0) {
5560Sstevel@tonic-gate 				errflg++;
557*5331Samw 				(void) fprintf(stderr,
558*5331Samw 				    gettext(
559*5331Samw 				    "pack: cannot change times on %s: "),
560*5331Samw 				    filename);
5610Sstevel@tonic-gate 				perror("");
5620Sstevel@tonic-gate 			}
5630Sstevel@tonic-gate 			if (chmod(filename, status.st_mode) != 0) {
5640Sstevel@tonic-gate 				errflg++;
565*5331Samw 				(void) fprintf(stderr,
566*5331Samw 				    gettext(
567*5331Samw 				    "pack: can't change mode to %o on %s: "),
568*5331Samw 				    status.st_mode, filename);
5690Sstevel@tonic-gate 				perror("");
5700Sstevel@tonic-gate 			}
571*5331Samw 			(void) chown(filename, status.st_uid, status.st_gid);
572789Sahrens 			if (aclp && (facl_set(outfile, aclp) < 0)) {
573*5331Samw 				(void) fprintf(stderr, gettext(
574789Sahrens 				    "pack: %s: failed to set acl entries\n"),
575789Sahrens 				    filename);
576789Sahrens 				perror("");
577789Sahrens 			}
5780Sstevel@tonic-gate 			if (!errflg)
5790Sstevel@tonic-gate 				fcount--;  /* success after all */
580*5331Samw 
5810Sstevel@tonic-gate 		}
582*5331Samw out:
5831620Smarks 		if (aclp) {
584789Sahrens 			acl_free(aclp);
5851620Smarks 			aclp = NULL;
5861620Smarks 		}
587789Sahrens 
588*5331Samw closein:	(void) close(outfile);
589*5331Samw 		(void) close(infile);
5900Sstevel@tonic-gate 	}
5910Sstevel@tonic-gate 	return (fcount);
5920Sstevel@tonic-gate }
593