xref: /titanic_50/usr/src/cmd/pack/pack.c (revision da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5f0a2d94fSmarks  * Common Development and Distribution License (the "License").
6f0a2d94fSmarks  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*da6c28aaSamw  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
29a77d64afScf46844 #pragma ident	"%Z%%M%	%I%	%E% SMI"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  *	Huffman encoding program
33*da6c28aaSamw  *	Usage:	pack [[ -f ] [ - ] [-/] filename ... ] filename ...
347c478bd9Sstevel@tonic-gate  *		- option: enable/disable listing of statistics
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <locale.h>
387c478bd9Sstevel@tonic-gate #include <stdarg.h>
397c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
407c478bd9Sstevel@tonic-gate #include <sys/param.h>
417c478bd9Sstevel@tonic-gate #include <utime.h>
42fa9e4066Sahrens #include <sys/acl.h>
43fa9e4066Sahrens #include <aclutils.h>
44*da6c28aaSamw #include <libcmdutils.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #undef lint
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define	END	256
497c478bd9Sstevel@tonic-gate #define	PACKED 017436 /* <US><RS> - Unlikely value */
507c478bd9Sstevel@tonic-gate #define	SUF0	'.'
517c478bd9Sstevel@tonic-gate #define	SUF1	'z'
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate struct stat status, ostatus;
547c478bd9Sstevel@tonic-gate static struct utimbuf u_times;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /* union for overlaying a long int with a set of four characters */
577c478bd9Sstevel@tonic-gate union FOUR {
587c478bd9Sstevel@tonic-gate 	struct { long int lng; } lint;
597c478bd9Sstevel@tonic-gate 	struct { char c0, c1, c2, c3; } chars;
607c478bd9Sstevel@tonic-gate };
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /* character counters */
637c478bd9Sstevel@tonic-gate long	count [END+1];
647c478bd9Sstevel@tonic-gate union	FOUR insize;
657c478bd9Sstevel@tonic-gate long	outsize;
667c478bd9Sstevel@tonic-gate long	dictsize;
677c478bd9Sstevel@tonic-gate int	diffbytes;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /* i/o stuff */
707c478bd9Sstevel@tonic-gate char	vflag = 0;
717c478bd9Sstevel@tonic-gate int	force = 0;	/* allow forced packing for consistency in directory */
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate static	char filename [MAXPATHLEN];
747c478bd9Sstevel@tonic-gate static int max_name;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate int	infile;		/* unpacked file */
777c478bd9Sstevel@tonic-gate int	outfile;	/* packed file */
787c478bd9Sstevel@tonic-gate char	inbuff [BUFSIZ];
797c478bd9Sstevel@tonic-gate char	outbuff [BUFSIZ+4];
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /* variables associated with the tree */
827c478bd9Sstevel@tonic-gate int	maxlev;
837c478bd9Sstevel@tonic-gate int	levcount [25];
847c478bd9Sstevel@tonic-gate int	lastnode;
857c478bd9Sstevel@tonic-gate int	parent [2*END+1];
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /* variables associated with the encoding process */
887c478bd9Sstevel@tonic-gate char	length [END+1];
897c478bd9Sstevel@tonic-gate long	bits [END+1];
907c478bd9Sstevel@tonic-gate union	FOUR mask;
917c478bd9Sstevel@tonic-gate long	inc;
927c478bd9Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN)
937c478bd9Sstevel@tonic-gate char	*maskshuff[4]  = {&(mask.chars.c3),
947c478bd9Sstevel@tonic-gate 			    &(mask.chars.c2),
957c478bd9Sstevel@tonic-gate 			    &(mask.chars.c1),
967c478bd9Sstevel@tonic-gate 			    &(mask.chars.c0)};
977c478bd9Sstevel@tonic-gate #elif defined(_BIG_ENDIAN)
987c478bd9Sstevel@tonic-gate char	*maskshuff[4]  = {&(mask.chars.c0),
997c478bd9Sstevel@tonic-gate 			    &(mask.chars.c1),
1007c478bd9Sstevel@tonic-gate 			    &(mask.chars.c2),
1017c478bd9Sstevel@tonic-gate 			    &(mask.chars.c3)};
1027c478bd9Sstevel@tonic-gate #else
1037c478bd9Sstevel@tonic-gate #error Unknown byte ordering!
1047c478bd9Sstevel@tonic-gate #endif
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /* the heap */
1077c478bd9Sstevel@tonic-gate int	n;
1087c478bd9Sstevel@tonic-gate struct	heap {
1097c478bd9Sstevel@tonic-gate 	long int count;
1107c478bd9Sstevel@tonic-gate 	int node;
1117c478bd9Sstevel@tonic-gate } heap [END+2];
1127c478bd9Sstevel@tonic-gate #define	hmove(a, b) {(b).count = (a).count; (b).node = (a).node; }
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate static void heapify(int i);
115*da6c28aaSamw 
116*da6c28aaSamw /* Extended system attribute support */
117*da6c28aaSamw 
118*da6c28aaSamw static int saflg = 0;
119*da6c28aaSamw 
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /* gather character frequency statistics */
1227c478bd9Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */
123a77d64afScf46844 int
input(char * source)1247c478bd9Sstevel@tonic-gate input(char *source)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	register int i;
1277c478bd9Sstevel@tonic-gate 	for (i = 0; i < END; i++)
1287c478bd9Sstevel@tonic-gate 		count[i] = 0;
1297c478bd9Sstevel@tonic-gate 	while ((i = read(infile, inbuff, BUFSIZ)) > 0)
1307c478bd9Sstevel@tonic-gate 		while (i > 0)
1317c478bd9Sstevel@tonic-gate 			count[inbuff[--i]&0377] += 2;
1327c478bd9Sstevel@tonic-gate 	if (i == 0)
1337c478bd9Sstevel@tonic-gate 		return (1);
134*da6c28aaSamw 	(void) fprintf(stderr, gettext(
1357c478bd9Sstevel@tonic-gate 	    "pack: %s: read error - file unchanged: "), source);
1367c478bd9Sstevel@tonic-gate 	perror("");
1377c478bd9Sstevel@tonic-gate 	return (0);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /* encode the current file */
1417c478bd9Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */
142a77d64afScf46844 int
output(char * source)1437c478bd9Sstevel@tonic-gate output(char *source)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate 	int c, i, inleft;
1467c478bd9Sstevel@tonic-gate 	char *inp;
1477c478bd9Sstevel@tonic-gate 	register char **q, *outp;
1487c478bd9Sstevel@tonic-gate 	register int bitsleft;
1497c478bd9Sstevel@tonic-gate 	long temp;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	/* output ``PACKED'' header */
1527c478bd9Sstevel@tonic-gate 	outbuff[0] = 037; 	/* ascii US */
1537c478bd9Sstevel@tonic-gate 	outbuff[1] = 036; 	/* ascii RS */
1547c478bd9Sstevel@tonic-gate 	/* output the length and the dictionary */
1557c478bd9Sstevel@tonic-gate 	temp = insize.lint.lng;
1567c478bd9Sstevel@tonic-gate 	for (i = 5; i >= 2; i--) {
1577c478bd9Sstevel@tonic-gate 		outbuff[i] =  (char)(temp & 0377);
1587c478bd9Sstevel@tonic-gate 		temp >>= 8;
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 	outp = &outbuff[6];
1617c478bd9Sstevel@tonic-gate 	*outp++ = maxlev;
1627c478bd9Sstevel@tonic-gate 	for (i = 1; i < maxlev; i++)
1637c478bd9Sstevel@tonic-gate 		*outp++ = levcount[i];
1647c478bd9Sstevel@tonic-gate 	*outp++ = levcount[maxlev]-2;
1657c478bd9Sstevel@tonic-gate 	for (i = 1; i <= maxlev; i++)
1667c478bd9Sstevel@tonic-gate 		for (c = 0; c < END; c++)
1677c478bd9Sstevel@tonic-gate 			if (length[c] == i)
1687c478bd9Sstevel@tonic-gate 				*outp++ = c;
1697c478bd9Sstevel@tonic-gate 	dictsize = outp-&outbuff[0];
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	/* output the text */
172*da6c28aaSamw 	(void) lseek(infile, 0L, 0);
1737c478bd9Sstevel@tonic-gate 	outsize = 0;
1747c478bd9Sstevel@tonic-gate 	bitsleft = 8;
1757c478bd9Sstevel@tonic-gate 	inleft = 0;
1767c478bd9Sstevel@tonic-gate 	do {
1777c478bd9Sstevel@tonic-gate 		if (inleft <= 0) {
1787c478bd9Sstevel@tonic-gate 			inleft = read(infile, inp = &inbuff[0], BUFSIZ);
1797c478bd9Sstevel@tonic-gate 			if (inleft < 0) {
180*da6c28aaSamw 				(void) fprintf(stderr, gettext(
1817c478bd9Sstevel@tonic-gate 				    "pack: %s: read error - file unchanged: "),
1827c478bd9Sstevel@tonic-gate 				    source);
1837c478bd9Sstevel@tonic-gate 				perror("");
1847c478bd9Sstevel@tonic-gate 				return (0);
1857c478bd9Sstevel@tonic-gate 			}
1867c478bd9Sstevel@tonic-gate 		}
1877c478bd9Sstevel@tonic-gate 		c = (--inleft < 0) ? END : (*inp++ & 0377);
1887c478bd9Sstevel@tonic-gate 		mask.lint.lng = bits[c]<<bitsleft;
1897c478bd9Sstevel@tonic-gate 		q = &maskshuff[0];
1907c478bd9Sstevel@tonic-gate 		if (bitsleft == 8)
1917c478bd9Sstevel@tonic-gate 			*outp = **q++;
1927c478bd9Sstevel@tonic-gate 		else
1937c478bd9Sstevel@tonic-gate 			*outp |= **q++;
1947c478bd9Sstevel@tonic-gate 		bitsleft -= length[c];
1957c478bd9Sstevel@tonic-gate 		while (bitsleft < 0) {
1967c478bd9Sstevel@tonic-gate 			*++outp = **q++;
1977c478bd9Sstevel@tonic-gate 			bitsleft += 8;
1987c478bd9Sstevel@tonic-gate 		}
1997c478bd9Sstevel@tonic-gate 		if (outp >= &outbuff[BUFSIZ]) {
2007c478bd9Sstevel@tonic-gate 			if (write(outfile, outbuff, BUFSIZ) != BUFSIZ) {
201*da6c28aaSamw wrerr:				(void) fprintf(stderr, gettext(
202*da6c28aaSamw 				    "pack: %s.z: write error - "
203*da6c28aaSamw 				    "file unchanged: "), source);
2047c478bd9Sstevel@tonic-gate 				perror("");
2057c478bd9Sstevel@tonic-gate 				return (0);
2067c478bd9Sstevel@tonic-gate 			}
2077c478bd9Sstevel@tonic-gate 			((union FOUR *)outbuff)->lint.lng =
2087c478bd9Sstevel@tonic-gate 			    ((union FOUR *)&outbuff[BUFSIZ])->lint.lng;
2097c478bd9Sstevel@tonic-gate 			outp -= BUFSIZ;
2107c478bd9Sstevel@tonic-gate 			outsize += BUFSIZ;
2117c478bd9Sstevel@tonic-gate 		}
2127c478bd9Sstevel@tonic-gate 	} while (c != END);
2137c478bd9Sstevel@tonic-gate 	if (bitsleft < 8)
2147c478bd9Sstevel@tonic-gate 		outp++;
2157c478bd9Sstevel@tonic-gate 	c = outp-outbuff;
2167c478bd9Sstevel@tonic-gate 	if (write(outfile, outbuff, c) != c)
2177c478bd9Sstevel@tonic-gate 		goto wrerr;
2187c478bd9Sstevel@tonic-gate 	outsize += c;
2197c478bd9Sstevel@tonic-gate 	return (1);
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate /* makes a heap out of heap[i],...,heap[n] */
2237c478bd9Sstevel@tonic-gate void
heapify(int i)2247c478bd9Sstevel@tonic-gate heapify(int i)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	register int k;
2277c478bd9Sstevel@tonic-gate 	int lastparent;
2287c478bd9Sstevel@tonic-gate 	struct heap heapsubi;
2297c478bd9Sstevel@tonic-gate 	hmove(heap[i], heapsubi);
2307c478bd9Sstevel@tonic-gate 	lastparent = n/2;
2317c478bd9Sstevel@tonic-gate 	while (i <= lastparent) {
2327c478bd9Sstevel@tonic-gate 		k = 2*i;
2337c478bd9Sstevel@tonic-gate 		if (heap[k].count > heap[k+1].count && k < n)
2347c478bd9Sstevel@tonic-gate 			k++;
2357c478bd9Sstevel@tonic-gate 		if (heapsubi.count < heap[k].count)
2367c478bd9Sstevel@tonic-gate 			break;
2377c478bd9Sstevel@tonic-gate 		hmove(heap[k], heap[i]);
2387c478bd9Sstevel@tonic-gate 		i = k;
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 	hmove(heapsubi, heap[i]);
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate /* return 1 after successful packing, 0 otherwise */
2447c478bd9Sstevel@tonic-gate int
packfile(char * source)2457c478bd9Sstevel@tonic-gate packfile(char *source)
2467c478bd9Sstevel@tonic-gate {
2477c478bd9Sstevel@tonic-gate 	register int c, i, p;
2487c478bd9Sstevel@tonic-gate 	long bitsout;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	/* gather frequency statistics */
2517c478bd9Sstevel@tonic-gate 	if (input(source) == 0)
2527c478bd9Sstevel@tonic-gate 		return (0);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	/* put occurring chars in heap with their counts */
2557c478bd9Sstevel@tonic-gate 	diffbytes = -1;
2567c478bd9Sstevel@tonic-gate 	count[END] = 1;
2577c478bd9Sstevel@tonic-gate 	insize.lint.lng = n = 0;
2587c478bd9Sstevel@tonic-gate 	for (i = END; i >= 0; i--) {
2597c478bd9Sstevel@tonic-gate 		parent[i] = 0;
2607c478bd9Sstevel@tonic-gate 		if (count[i] > 0) {
2617c478bd9Sstevel@tonic-gate 			diffbytes++;
2627c478bd9Sstevel@tonic-gate 			insize.lint.lng += count[i];
2637c478bd9Sstevel@tonic-gate 			heap[++n].count = count[i];
2647c478bd9Sstevel@tonic-gate 			heap[n].node = i;
2657c478bd9Sstevel@tonic-gate 		}
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 	if (diffbytes == 1) {
268*da6c28aaSamw 		(void) fprintf(stderr, gettext(
2697c478bd9Sstevel@tonic-gate 		    "pack: %s: trivial file - file unchanged\n"), source);
2707c478bd9Sstevel@tonic-gate 		return (0);
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 	insize.lint.lng >>= 1;
2737c478bd9Sstevel@tonic-gate 	for (i = n/2; i >= 1; i--)
2747c478bd9Sstevel@tonic-gate 		heapify(i);
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	/* build Huffman tree */
2777c478bd9Sstevel@tonic-gate 	lastnode = END;
2787c478bd9Sstevel@tonic-gate 	while (n > 1) {
2797c478bd9Sstevel@tonic-gate 		parent[heap[1].node] = ++lastnode;
2807c478bd9Sstevel@tonic-gate 		inc = heap[1].count;
2817c478bd9Sstevel@tonic-gate 		hmove(heap[n], heap[1]);
2827c478bd9Sstevel@tonic-gate 		n--;
2837c478bd9Sstevel@tonic-gate 		heapify(1);
2847c478bd9Sstevel@tonic-gate 		parent[heap[1].node] = lastnode;
2857c478bd9Sstevel@tonic-gate 		heap[1].node = lastnode;
2867c478bd9Sstevel@tonic-gate 		heap[1].count += inc;
2877c478bd9Sstevel@tonic-gate 		heapify(1);
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 	parent[lastnode] = 0;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	/* assign lengths to encoding for each character */
2927c478bd9Sstevel@tonic-gate 	bitsout = maxlev = 0;
2937c478bd9Sstevel@tonic-gate 	for (i = 1; i <= 24; i++)
2947c478bd9Sstevel@tonic-gate 		levcount[i] = 0;
2957c478bd9Sstevel@tonic-gate 	for (i = 0; i <= END; i++) {
2967c478bd9Sstevel@tonic-gate 		c = 0;
2977c478bd9Sstevel@tonic-gate 		for (p = parent[i]; p != 0; p = parent[p])
2987c478bd9Sstevel@tonic-gate 			c++;
2997c478bd9Sstevel@tonic-gate 		levcount[c]++;
3007c478bd9Sstevel@tonic-gate 		length[i] = c;
3017c478bd9Sstevel@tonic-gate 		if (c > maxlev)
3027c478bd9Sstevel@tonic-gate 			maxlev = c;
3037c478bd9Sstevel@tonic-gate 		bitsout += c*(count[i]>>1);
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 	if (maxlev > 24) {
3067c478bd9Sstevel@tonic-gate 		/* can't occur unless insize.lint.lng >= 2**24 */
307*da6c28aaSamw 		(void) fprintf(stderr, gettext(
308*da6c28aaSamw 		    "pack: %s: Huffman tree has too many levels - "
309*da6c28aaSamw 		    "file unchanged\n"), source);
3107c478bd9Sstevel@tonic-gate 		return (0);
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	/* don't bother if no compression results */
3147c478bd9Sstevel@tonic-gate 	outsize = ((bitsout+7)>>3)+6+maxlev+diffbytes;
3157c478bd9Sstevel@tonic-gate 	if ((insize.lint.lng+BUFSIZ-1)/BUFSIZ <=
3167c478bd9Sstevel@tonic-gate 	    (outsize+BUFSIZ-1)/BUFSIZ && !force) {
317*da6c28aaSamw 		(void) printf(gettext(
3187c478bd9Sstevel@tonic-gate 		    "pack: %s: no saving - file unchanged\n"), source);
3197c478bd9Sstevel@tonic-gate 		return (0);
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	/* compute bit patterns for each character */
3237c478bd9Sstevel@tonic-gate 	inc = 1L << 24;
3247c478bd9Sstevel@tonic-gate 	inc >>= maxlev;
3257c478bd9Sstevel@tonic-gate 	mask.lint.lng = 0;
3267c478bd9Sstevel@tonic-gate 	for (i = maxlev; i > 0; i--) {
3277c478bd9Sstevel@tonic-gate 		for (c = 0; c <= END; c++)
3287c478bd9Sstevel@tonic-gate 			if (length[c] == i) {
3297c478bd9Sstevel@tonic-gate 				bits[c] = mask.lint.lng;
3307c478bd9Sstevel@tonic-gate 				mask.lint.lng += inc;
3317c478bd9Sstevel@tonic-gate 			}
3327c478bd9Sstevel@tonic-gate 		mask.lint.lng &= ~inc;
3337c478bd9Sstevel@tonic-gate 		inc <<= 1;
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	return (output(source));
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate 
339a77d64afScf46844 int
main(int argc,char * argv[])3407c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
3417c478bd9Sstevel@tonic-gate {
3427c478bd9Sstevel@tonic-gate 	extern  int optind;
3437c478bd9Sstevel@tonic-gate 	register int i;
3447c478bd9Sstevel@tonic-gate 	register char *cp;
3457c478bd9Sstevel@tonic-gate 	int k, sep, errflg = 0;
3467c478bd9Sstevel@tonic-gate 	int c;
347fa9e4066Sahrens 	int error;
3487c478bd9Sstevel@tonic-gate 	int fcount = 0; /* count failures */
349fa9e4066Sahrens 	acl_t *aclp = NULL;
350*da6c28aaSamw 	char *progname;
351*da6c28aaSamw 	int sattr_exist = 0;
352*da6c28aaSamw 	int xattr_exist = 0;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3557c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
3567c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
3577c478bd9Sstevel@tonic-gate #endif
3587c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3597c478bd9Sstevel@tonic-gate 
360*da6c28aaSamw 	if (progname = strrchr(argv[0], '/'))
361*da6c28aaSamw 		++progname;
362*da6c28aaSamw 	else
363*da6c28aaSamw 		progname = argv[0];
364*da6c28aaSamw 
365*da6c28aaSamw 	while ((c = getopt(argc, argv, "f-/")) != EOF) {
3667c478bd9Sstevel@tonic-gate 		if (c == 'f')
3677c478bd9Sstevel@tonic-gate 			force++;
368*da6c28aaSamw 		else if (c == '/')
369*da6c28aaSamw 			saflg++;
3707c478bd9Sstevel@tonic-gate 		else
3717c478bd9Sstevel@tonic-gate 			++errflg;
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate 	/*
3747c478bd9Sstevel@tonic-gate 	 * Check for invalid option.  Also check for missing
3757c478bd9Sstevel@tonic-gate 	 * file operand, ie: "pack" or "pack -".
3767c478bd9Sstevel@tonic-gate 	 */
3777c478bd9Sstevel@tonic-gate 	argc -= optind;
3787c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
3797c478bd9Sstevel@tonic-gate 	if (errflg || argc < 1 ||
380*da6c28aaSamw 	    (argc == 1 && (argv[0][0] == '-' || argv[0][0] == '/' &&
381*da6c28aaSamw 	    argv[0][1] == '\0'))) {
382*da6c28aaSamw 		(void) fprintf(stderr, gettext(
383*da6c28aaSamw 		    "usage: pack [-f] [-] [-/] file...\n"));
3847c478bd9Sstevel@tonic-gate 		if (argc < 1 ||
385*da6c28aaSamw 		    (argc == 1 && (argv[0][0] == '-' || argv[0][0] == '/') &&
3867c478bd9Sstevel@tonic-gate 		    argv[0][1] == '\0')) {
3877c478bd9Sstevel@tonic-gate 			/*
3887c478bd9Sstevel@tonic-gate 			 * return 1 for usage error when no file was specified
3897c478bd9Sstevel@tonic-gate 			 */
3907c478bd9Sstevel@tonic-gate 			return (1);
3917c478bd9Sstevel@tonic-gate 		}
3927c478bd9Sstevel@tonic-gate 	}
393*da6c28aaSamw 
3947c478bd9Sstevel@tonic-gate 	/* loop through the file names */
3957c478bd9Sstevel@tonic-gate 	for (k = 0; k < argc; k++) {
3967c478bd9Sstevel@tonic-gate 		if (argv[k][0] == '-' && argv[k][1] == '\0') {
3977c478bd9Sstevel@tonic-gate 			vflag = 1 - vflag;
3987c478bd9Sstevel@tonic-gate 			continue;
3997c478bd9Sstevel@tonic-gate 		}
4007c478bd9Sstevel@tonic-gate 		fcount++; /* increase failure count - expect the worst */
4017c478bd9Sstevel@tonic-gate 		if (errflg) {
4027c478bd9Sstevel@tonic-gate 			/*
4037c478bd9Sstevel@tonic-gate 			 * invalid option; just count the number of files not
4047c478bd9Sstevel@tonic-gate 			 * packed
4057c478bd9Sstevel@tonic-gate 			 */
4067c478bd9Sstevel@tonic-gate 			continue;
4077c478bd9Sstevel@tonic-gate 		}
4087c478bd9Sstevel@tonic-gate 		/* remove any .z suffix the user may have added */
4097c478bd9Sstevel@tonic-gate 		for (cp = argv[k]; *cp != '\0'; ++cp)
4107c478bd9Sstevel@tonic-gate 			;
4117c478bd9Sstevel@tonic-gate 		if (cp[-1] == SUF1 && cp[-2] == SUF0) {
4127c478bd9Sstevel@tonic-gate 			*cp-- = '\0'; *cp-- = '\0'; *cp = '\0';
4137c478bd9Sstevel@tonic-gate 		}
4147c478bd9Sstevel@tonic-gate 		sep = -1;  cp = filename;
4157c478bd9Sstevel@tonic-gate 		max_name = pathconf(argv[k], _PC_NAME_MAX);
4167c478bd9Sstevel@tonic-gate 		if (max_name == -1) {
4177c478bd9Sstevel@tonic-gate 			/* pathname invalid or no limit on length of filename */
4187c478bd9Sstevel@tonic-gate 			max_name = _POSIX_NAME_MAX;
4197c478bd9Sstevel@tonic-gate 		}
4207c478bd9Sstevel@tonic-gate 		/* copy argv[k] to filename and count chars in base name */
4217c478bd9Sstevel@tonic-gate 		for (i = 0; i < (MAXPATHLEN-3) && (*cp = argv[k][i]); i++)
4227c478bd9Sstevel@tonic-gate 			if (*cp++ == '/') sep = i;
4237c478bd9Sstevel@tonic-gate 		if ((infile = open(filename, 0)) < 0) {
424*da6c28aaSamw 			(void) fprintf(stderr, gettext(
4257c478bd9Sstevel@tonic-gate 			    "pack: %s: cannot open: "), filename);
4267c478bd9Sstevel@tonic-gate 			perror("");
4277c478bd9Sstevel@tonic-gate 			continue;
4287c478bd9Sstevel@tonic-gate 		}
4297c478bd9Sstevel@tonic-gate 		if (i >= (MAXPATHLEN-3) || (i-sep) > (max_name - 1)) {
430*da6c28aaSamw 			(void) fprintf(stderr, gettext(
4317c478bd9Sstevel@tonic-gate 			    "pack: %s: file name too long\n"), argv[k]);
4327c478bd9Sstevel@tonic-gate 			continue;
4337c478bd9Sstevel@tonic-gate 		}
434*da6c28aaSamw 		(void) fstat(infile, &status);
4354bc0a2efScasper 		if (S_ISDIR(status.st_mode)) {
436*da6c28aaSamw 			(void) fprintf(stderr, gettext(
4377c478bd9Sstevel@tonic-gate 			    "pack: %s: cannot pack a directory\n"),
4387c478bd9Sstevel@tonic-gate 			    argv[k]);
4397c478bd9Sstevel@tonic-gate 			goto closein;
4407c478bd9Sstevel@tonic-gate 		}
4417c478bd9Sstevel@tonic-gate 		if (status.st_size == 0) {
442*da6c28aaSamw 			(void) fprintf(stderr, gettext(
4437c478bd9Sstevel@tonic-gate 			    "pack: %s: cannot pack a zero length file\n"),
4447c478bd9Sstevel@tonic-gate 			    argv[k]);
4457c478bd9Sstevel@tonic-gate 			goto closein;
4467c478bd9Sstevel@tonic-gate 		}
4477c478bd9Sstevel@tonic-gate 		if (status.st_nlink != 1) {
448*da6c28aaSamw 			(void) fprintf(stderr, gettext(
4497c478bd9Sstevel@tonic-gate 			    "pack: %s: has links\n"),
4507c478bd9Sstevel@tonic-gate 			    argv[k]);
4517c478bd9Sstevel@tonic-gate 			goto closein;
4527c478bd9Sstevel@tonic-gate 		}
4537c478bd9Sstevel@tonic-gate 		*cp++ = SUF0;  *cp++ = SUF1;  *cp = '\0';
4547c478bd9Sstevel@tonic-gate 		if (stat(filename, &ostatus) != -1) {
455*da6c28aaSamw 			(void) fprintf(stderr, gettext(
4567c478bd9Sstevel@tonic-gate 			    "pack: %s: already exists\n"), filename);
4577c478bd9Sstevel@tonic-gate 			goto closein;
4587c478bd9Sstevel@tonic-gate 		}
459*da6c28aaSamw 		if ((outfile = creat(filename, status.st_mode | O_RDONLY))
460*da6c28aaSamw 		    < 0) {
461*da6c28aaSamw 			(void) fprintf(stderr, gettext(
4627c478bd9Sstevel@tonic-gate 			    "pack: %s: cannot create: "), filename);
4637c478bd9Sstevel@tonic-gate 			perror("");
4647c478bd9Sstevel@tonic-gate 			goto closein;
4657c478bd9Sstevel@tonic-gate 		}
4667c478bd9Sstevel@tonic-gate 
467fa9e4066Sahrens 		error = facl_get(infile, ACL_NO_TRIVIAL, &aclp);
468fa9e4066Sahrens 
469fa9e4066Sahrens 		if (error != 0) {
470*da6c28aaSamw 			(void) fprintf(stderr, gettext(
471fa9e4066Sahrens 			    "pack: %s: cannot retrieve ACL: %s\n"), argv[k],
472fa9e4066Sahrens 			    acl_strerror(error));
473fa9e4066Sahrens 		}
474*da6c28aaSamw 
475*da6c28aaSamw 		if (packfile(argv[k])) {
476*da6c28aaSamw 			if (pathconf(argv[k], _PC_XATTR_EXISTS) == 1)
477*da6c28aaSamw 				xattr_exist = 1;
478*da6c28aaSamw 			if (saflg && sysattr_support(argv[k],
479*da6c28aaSamw 			    _PC_SATTR_EXISTS) == 1)
480*da6c28aaSamw 				sattr_exist = 1;
481*da6c28aaSamw 			if (sattr_exist || xattr_exist) {
482*da6c28aaSamw 				if (mv_xattrs(progname, argv[k], filename,
483*da6c28aaSamw 				    sattr_exist, 0) != 0) {
484*da6c28aaSamw 				/* Move attributes back ... */
485*da6c28aaSamw 					xattr_exist = 0;
486*da6c28aaSamw 					sattr_exist = 0;
487*da6c28aaSamw 					if (pathconf(filename,
488*da6c28aaSamw 					    _PC_XATTR_EXISTS) == 1)
489*da6c28aaSamw 						xattr_exist = 1;
490*da6c28aaSamw 					if (saflg && sysattr_support(filename,
491*da6c28aaSamw 					    _PC_SATTR_EXISTS) == 1)
492*da6c28aaSamw 						sattr_exist = 1;
493*da6c28aaSamw 					if (xattr_exist || sattr_exist) {
494*da6c28aaSamw 						(void) mv_xattrs(progname,
495*da6c28aaSamw 						    filename, argv[k],
496*da6c28aaSamw 						    sattr_exist, 1);
497*da6c28aaSamw 						(void) unlink(filename);
498*da6c28aaSamw 						goto out;
499*da6c28aaSamw 					}
500*da6c28aaSamw 				} else {
501*da6c28aaSamw 					errno = 0;
5027c478bd9Sstevel@tonic-gate 					if (unlink(argv[k]) != 0) {
503*da6c28aaSamw 						(void) fprintf(stderr, gettext(
5047c478bd9Sstevel@tonic-gate 						    "pack: %s :cannot unlink:"),
5057c478bd9Sstevel@tonic-gate 						    argv[k]);
506*da6c28aaSamw 						if (errno == EPERM)
507*da6c28aaSamw 							perror("No permission");
508*da6c28aaSamw 						else
5097c478bd9Sstevel@tonic-gate 							perror("");
5107c478bd9Sstevel@tonic-gate 					}
511*da6c28aaSamw 				}
512*da6c28aaSamw 			} else {
513*da6c28aaSamw 				errno = 0;
514*da6c28aaSamw 				if (unlink(argv[k]) != 0) {
515*da6c28aaSamw 					(void) fprintf(stderr, gettext(
516*da6c28aaSamw 					    "pack: %s :cannot unlink"),
517*da6c28aaSamw 					    argv[k]);
518*da6c28aaSamw 					if (errno == EPERM)
519*da6c28aaSamw 						perror("No permission");
520*da6c28aaSamw 					else
521*da6c28aaSamw 						perror("");
522*da6c28aaSamw 				}
523*da6c28aaSamw 			}
524*da6c28aaSamw 			(void) printf(gettext(
5257c478bd9Sstevel@tonic-gate 			    "pack: %s: %.1f%% Compression\n"),
5267c478bd9Sstevel@tonic-gate 			    argv[k],
527*da6c28aaSamw 			    ((double)(-outsize+(insize.lint.lng))/
528*da6c28aaSamw 			    (double)insize.lint.lng)*100);
5297c478bd9Sstevel@tonic-gate 			/* output statistics */
5307c478bd9Sstevel@tonic-gate 			if (vflag) {
531*da6c28aaSamw 				(void) printf(gettext(
532*da6c28aaSamw 				    "\tfrom %ld to %ld bytes\n"),
5337c478bd9Sstevel@tonic-gate 				    insize.lint.lng, outsize);
534*da6c28aaSamw 				(void) printf(gettext(
535*da6c28aaSamw 				    "\tHuffman tree has %d levels below "
536*da6c28aaSamw 				    "root\n"), maxlev);
537*da6c28aaSamw 				(void) printf(gettext(
5387c478bd9Sstevel@tonic-gate 				    "\t%d distinct bytes in input\n"),
5397c478bd9Sstevel@tonic-gate 				    diffbytes);
540*da6c28aaSamw 				(void) printf(gettext(
5417c478bd9Sstevel@tonic-gate 				    "\tdictionary overhead = %ld bytes\n"),
5427c478bd9Sstevel@tonic-gate 				    dictsize);
543*da6c28aaSamw 				(void) printf(gettext(
5447c478bd9Sstevel@tonic-gate 				    "\teffective  entropy  = %.2f bits/byte\n"),
545*da6c28aaSamw 				    ((double)outsize / (double)insize.lint.lng)
546*da6c28aaSamw 				    * 8);
547*da6c28aaSamw 				(void) printf(gettext(
5487c478bd9Sstevel@tonic-gate 				    "\tasymptotic entropy  = %.2f bits/byte\n"),
5497c478bd9Sstevel@tonic-gate 				    ((double)(outsize-dictsize) /
5507c478bd9Sstevel@tonic-gate 				    (double)insize.lint.lng) * 8);
5517c478bd9Sstevel@tonic-gate 			}
552*da6c28aaSamw 
5537c478bd9Sstevel@tonic-gate 			u_times.actime = status.st_atime;
5547c478bd9Sstevel@tonic-gate 			u_times.modtime = status.st_mtime;
5557c478bd9Sstevel@tonic-gate 			if (utime(filename, &u_times) != 0) {
5567c478bd9Sstevel@tonic-gate 				errflg++;
557*da6c28aaSamw 				(void) fprintf(stderr,
5587c478bd9Sstevel@tonic-gate 				    gettext(
5597c478bd9Sstevel@tonic-gate 				    "pack: cannot change times on %s: "),
5607c478bd9Sstevel@tonic-gate 				    filename);
5617c478bd9Sstevel@tonic-gate 				perror("");
5627c478bd9Sstevel@tonic-gate 			}
5637c478bd9Sstevel@tonic-gate 			if (chmod(filename, status.st_mode) != 0) {
5647c478bd9Sstevel@tonic-gate 				errflg++;
565*da6c28aaSamw 				(void) fprintf(stderr,
5667c478bd9Sstevel@tonic-gate 				    gettext(
5677c478bd9Sstevel@tonic-gate 				    "pack: can't change mode to %o on %s: "),
5687c478bd9Sstevel@tonic-gate 				    status.st_mode, filename);
5697c478bd9Sstevel@tonic-gate 				perror("");
5707c478bd9Sstevel@tonic-gate 			}
571*da6c28aaSamw 			(void) chown(filename, status.st_uid, status.st_gid);
572fa9e4066Sahrens 			if (aclp && (facl_set(outfile, aclp) < 0)) {
573*da6c28aaSamw 				(void) fprintf(stderr, gettext(
574fa9e4066Sahrens 				    "pack: %s: failed to set acl entries\n"),
575fa9e4066Sahrens 				    filename);
576fa9e4066Sahrens 				perror("");
577fa9e4066Sahrens 			}
5787c478bd9Sstevel@tonic-gate 			if (!errflg)
5797c478bd9Sstevel@tonic-gate 				fcount--;  /* success after all */
580fa9e4066Sahrens 
581*da6c28aaSamw 		}
582*da6c28aaSamw out:
583f0a2d94fSmarks 		if (aclp) {
584fa9e4066Sahrens 			acl_free(aclp);
585f0a2d94fSmarks 			aclp = NULL;
586f0a2d94fSmarks 		}
587fa9e4066Sahrens 
588*da6c28aaSamw closein:	(void) close(outfile);
589*da6c28aaSamw 		(void) close(infile);
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 	return (fcount);
5927c478bd9Sstevel@tonic-gate }
593