xref: /titanic_50/usr/src/cmd/pack/pack.c (revision f0a2d94fac0bda585fe02eb5f086dbbf15c986a7)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*f0a2d94fSmarks  * Common Development and Distribution License (the "License").
6*f0a2d94fSmarks  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*f0a2d94fSmarks  * Copyright 2006 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
337c478bd9Sstevel@tonic-gate  *	Usage:	pack [[ -f ] [ - ] filename ... ] filename ...
347c478bd9Sstevel@tonic-gate  *		- option: enable/disable listing of statistics
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/stat.h>
407c478bd9Sstevel@tonic-gate #include <unistd.h>
417c478bd9Sstevel@tonic-gate #include <locale.h>
427c478bd9Sstevel@tonic-gate #include <stdarg.h>
437c478bd9Sstevel@tonic-gate #include <errno.h>
447c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
457c478bd9Sstevel@tonic-gate #include <stdlib.h>
467c478bd9Sstevel@tonic-gate #include <limits.h>
477c478bd9Sstevel@tonic-gate #include <sys/param.h>
487c478bd9Sstevel@tonic-gate #include <fcntl.h>
497c478bd9Sstevel@tonic-gate #include <utime.h>
507c478bd9Sstevel@tonic-gate #include <string.h>
517c478bd9Sstevel@tonic-gate #include <dirent.h>
527c478bd9Sstevel@tonic-gate #include <unistd.h>
53fa9e4066Sahrens #include <sys/acl.h>
54fa9e4066Sahrens #include <aclutils.h>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #undef lint
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #define	END	256
597c478bd9Sstevel@tonic-gate #define	PACKED 017436 /* <US><RS> - Unlikely value */
607c478bd9Sstevel@tonic-gate #define	SUF0	'.'
617c478bd9Sstevel@tonic-gate #define	SUF1	'z'
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate struct stat status, ostatus;
647c478bd9Sstevel@tonic-gate static struct utimbuf u_times;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /* union for overlaying a long int with a set of four characters */
677c478bd9Sstevel@tonic-gate union FOUR {
687c478bd9Sstevel@tonic-gate 	struct { long int lng; } lint;
697c478bd9Sstevel@tonic-gate 	struct { char c0, c1, c2, c3; } chars;
707c478bd9Sstevel@tonic-gate };
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /* character counters */
737c478bd9Sstevel@tonic-gate long	count [END+1];
747c478bd9Sstevel@tonic-gate union	FOUR insize;
757c478bd9Sstevel@tonic-gate long	outsize;
767c478bd9Sstevel@tonic-gate long	dictsize;
777c478bd9Sstevel@tonic-gate int	diffbytes;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /* i/o stuff */
807c478bd9Sstevel@tonic-gate char	vflag = 0;
817c478bd9Sstevel@tonic-gate int	force = 0;	/* allow forced packing for consistency in directory */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate static	char filename [MAXPATHLEN];
847c478bd9Sstevel@tonic-gate static int max_name;
857c478bd9Sstevel@tonic-gate static int max_path = MAXPATHLEN;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate int	infile;		/* unpacked file */
887c478bd9Sstevel@tonic-gate int	outfile;	/* packed file */
897c478bd9Sstevel@tonic-gate char	inbuff [BUFSIZ];
907c478bd9Sstevel@tonic-gate char	outbuff [BUFSIZ+4];
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /* variables associated with the tree */
937c478bd9Sstevel@tonic-gate int	maxlev;
947c478bd9Sstevel@tonic-gate int	levcount [25];
957c478bd9Sstevel@tonic-gate int	lastnode;
967c478bd9Sstevel@tonic-gate int	parent [2*END+1];
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /* variables associated with the encoding process */
997c478bd9Sstevel@tonic-gate char	length [END+1];
1007c478bd9Sstevel@tonic-gate long	bits [END+1];
1017c478bd9Sstevel@tonic-gate union	FOUR mask;
1027c478bd9Sstevel@tonic-gate long	inc;
1037c478bd9Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN)
1047c478bd9Sstevel@tonic-gate char	*maskshuff[4]  = {&(mask.chars.c3),
1057c478bd9Sstevel@tonic-gate 			    &(mask.chars.c2),
1067c478bd9Sstevel@tonic-gate 			    &(mask.chars.c1),
1077c478bd9Sstevel@tonic-gate 			    &(mask.chars.c0)};
1087c478bd9Sstevel@tonic-gate #elif defined(_BIG_ENDIAN)
1097c478bd9Sstevel@tonic-gate char	*maskshuff[4]  = {&(mask.chars.c0),
1107c478bd9Sstevel@tonic-gate 			    &(mask.chars.c1),
1117c478bd9Sstevel@tonic-gate 			    &(mask.chars.c2),
1127c478bd9Sstevel@tonic-gate 			    &(mask.chars.c3)};
1137c478bd9Sstevel@tonic-gate #else
1147c478bd9Sstevel@tonic-gate #error Unknown byte ordering!
1157c478bd9Sstevel@tonic-gate #endif
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /* the heap */
1187c478bd9Sstevel@tonic-gate int	n;
1197c478bd9Sstevel@tonic-gate struct	heap {
1207c478bd9Sstevel@tonic-gate 	long int count;
1217c478bd9Sstevel@tonic-gate 	int node;
1227c478bd9Sstevel@tonic-gate } heap [END+2];
1237c478bd9Sstevel@tonic-gate #define	hmove(a, b) {(b).count = (a).count; (b).node = (a).node; }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate static void heapify(int i);
1267c478bd9Sstevel@tonic-gate static int mv_xattrs(int, int, char *, int);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /* gather character frequency statistics */
1297c478bd9Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */
130a77d64afScf46844 int
1317c478bd9Sstevel@tonic-gate input(char *source)
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	register int i;
1347c478bd9Sstevel@tonic-gate 	for (i = 0; i < END; i++)
1357c478bd9Sstevel@tonic-gate 		count[i] = 0;
1367c478bd9Sstevel@tonic-gate 	while ((i = read(infile, inbuff, BUFSIZ)) > 0)
1377c478bd9Sstevel@tonic-gate 		while (i > 0)
1387c478bd9Sstevel@tonic-gate 			count[inbuff[--i]&0377] += 2;
1397c478bd9Sstevel@tonic-gate 	if (i == 0)
1407c478bd9Sstevel@tonic-gate 		return (1);
1417c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext(
1427c478bd9Sstevel@tonic-gate 		"pack: %s: read error - file unchanged: "), source);
1437c478bd9Sstevel@tonic-gate 	perror("");
1447c478bd9Sstevel@tonic-gate 	return (0);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /* encode the current file */
1487c478bd9Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */
149a77d64afScf46844 int
1507c478bd9Sstevel@tonic-gate output(char *source)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	int c, i, inleft;
1537c478bd9Sstevel@tonic-gate 	char *inp;
1547c478bd9Sstevel@tonic-gate 	register char **q, *outp;
1557c478bd9Sstevel@tonic-gate 	register int bitsleft;
1567c478bd9Sstevel@tonic-gate 	long temp;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	/* output ``PACKED'' header */
1597c478bd9Sstevel@tonic-gate 	outbuff[0] = 037; 	/* ascii US */
1607c478bd9Sstevel@tonic-gate 	outbuff[1] = 036; 	/* ascii RS */
1617c478bd9Sstevel@tonic-gate 	/* output the length and the dictionary */
1627c478bd9Sstevel@tonic-gate 	temp = insize.lint.lng;
1637c478bd9Sstevel@tonic-gate 	for (i = 5; i >= 2; i--) {
1647c478bd9Sstevel@tonic-gate 		outbuff[i] =  (char)(temp & 0377);
1657c478bd9Sstevel@tonic-gate 		temp >>= 8;
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 	outp = &outbuff[6];
1687c478bd9Sstevel@tonic-gate 	*outp++ = maxlev;
1697c478bd9Sstevel@tonic-gate 	for (i = 1; i < maxlev; i++)
1707c478bd9Sstevel@tonic-gate 		*outp++ = levcount[i];
1717c478bd9Sstevel@tonic-gate 	*outp++ = levcount[maxlev]-2;
1727c478bd9Sstevel@tonic-gate 	for (i = 1; i <= maxlev; i++)
1737c478bd9Sstevel@tonic-gate 		for (c = 0; c < END; c++)
1747c478bd9Sstevel@tonic-gate 			if (length[c] == i)
1757c478bd9Sstevel@tonic-gate 				*outp++ = c;
1767c478bd9Sstevel@tonic-gate 	dictsize = outp-&outbuff[0];
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	/* output the text */
1797c478bd9Sstevel@tonic-gate 	lseek(infile, 0L, 0);
1807c478bd9Sstevel@tonic-gate 	outsize = 0;
1817c478bd9Sstevel@tonic-gate 	bitsleft = 8;
1827c478bd9Sstevel@tonic-gate 	inleft = 0;
1837c478bd9Sstevel@tonic-gate 	do {
1847c478bd9Sstevel@tonic-gate 		if (inleft <= 0) {
1857c478bd9Sstevel@tonic-gate 			inleft = read(infile, inp = &inbuff[0], BUFSIZ);
1867c478bd9Sstevel@tonic-gate 			if (inleft < 0) {
1877c478bd9Sstevel@tonic-gate 				fprintf(stderr, gettext(
1887c478bd9Sstevel@tonic-gate 				    "pack: %s: read error - file unchanged: "),
1897c478bd9Sstevel@tonic-gate 					    source);
1907c478bd9Sstevel@tonic-gate 				perror("");
1917c478bd9Sstevel@tonic-gate 				return (0);
1927c478bd9Sstevel@tonic-gate 			}
1937c478bd9Sstevel@tonic-gate 		}
1947c478bd9Sstevel@tonic-gate 		c = (--inleft < 0) ? END : (*inp++ & 0377);
1957c478bd9Sstevel@tonic-gate 		mask.lint.lng = bits[c]<<bitsleft;
1967c478bd9Sstevel@tonic-gate 		q = &maskshuff[0];
1977c478bd9Sstevel@tonic-gate 		if (bitsleft == 8)
1987c478bd9Sstevel@tonic-gate 			*outp = **q++;
1997c478bd9Sstevel@tonic-gate 		else
2007c478bd9Sstevel@tonic-gate 			*outp |= **q++;
2017c478bd9Sstevel@tonic-gate 		bitsleft -= length[c];
2027c478bd9Sstevel@tonic-gate 		while (bitsleft < 0) {
2037c478bd9Sstevel@tonic-gate 			*++outp = **q++;
2047c478bd9Sstevel@tonic-gate 			bitsleft += 8;
2057c478bd9Sstevel@tonic-gate 		}
2067c478bd9Sstevel@tonic-gate 		if (outp >= &outbuff[BUFSIZ]) {
2077c478bd9Sstevel@tonic-gate 			if (write(outfile, outbuff, BUFSIZ) != BUFSIZ) {
2087c478bd9Sstevel@tonic-gate wrerr:				fprintf(stderr, gettext(
2097c478bd9Sstevel@tonic-gate 				"pack: %s.z: write error - file unchanged: "),
2107c478bd9Sstevel@tonic-gate 					source);
2117c478bd9Sstevel@tonic-gate 				perror("");
2127c478bd9Sstevel@tonic-gate 				return (0);
2137c478bd9Sstevel@tonic-gate 			}
2147c478bd9Sstevel@tonic-gate 			((union FOUR *)outbuff)->lint.lng =
2157c478bd9Sstevel@tonic-gate 				    ((union FOUR *)&outbuff[BUFSIZ])->lint.lng;
2167c478bd9Sstevel@tonic-gate 			outp -= BUFSIZ;
2177c478bd9Sstevel@tonic-gate 			outsize += BUFSIZ;
2187c478bd9Sstevel@tonic-gate 		}
2197c478bd9Sstevel@tonic-gate 	} while (c != END);
2207c478bd9Sstevel@tonic-gate 	if (bitsleft < 8)
2217c478bd9Sstevel@tonic-gate 		outp++;
2227c478bd9Sstevel@tonic-gate 	c = outp-outbuff;
2237c478bd9Sstevel@tonic-gate 	if (write(outfile, outbuff, c) != c)
2247c478bd9Sstevel@tonic-gate 		goto wrerr;
2257c478bd9Sstevel@tonic-gate 	outsize += c;
2267c478bd9Sstevel@tonic-gate 	return (1);
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate /* makes a heap out of heap[i],...,heap[n] */
2307c478bd9Sstevel@tonic-gate void
2317c478bd9Sstevel@tonic-gate heapify(int i)
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate 	register int k;
2347c478bd9Sstevel@tonic-gate 	int lastparent;
2357c478bd9Sstevel@tonic-gate 	struct heap heapsubi;
2367c478bd9Sstevel@tonic-gate 	hmove(heap[i], heapsubi);
2377c478bd9Sstevel@tonic-gate 	lastparent = n/2;
2387c478bd9Sstevel@tonic-gate 	while (i <= lastparent) {
2397c478bd9Sstevel@tonic-gate 		k = 2*i;
2407c478bd9Sstevel@tonic-gate 		if (heap[k].count > heap[k+1].count && k < n)
2417c478bd9Sstevel@tonic-gate 			k++;
2427c478bd9Sstevel@tonic-gate 		if (heapsubi.count < heap[k].count)
2437c478bd9Sstevel@tonic-gate 			break;
2447c478bd9Sstevel@tonic-gate 		hmove(heap[k], heap[i]);
2457c478bd9Sstevel@tonic-gate 		i = k;
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 	hmove(heapsubi, heap[i]);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate /* return 1 after successful packing, 0 otherwise */
2517c478bd9Sstevel@tonic-gate int
2527c478bd9Sstevel@tonic-gate packfile(char *source)
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate 	register int c, i, p;
2557c478bd9Sstevel@tonic-gate 	long bitsout;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	/* gather frequency statistics */
2587c478bd9Sstevel@tonic-gate 	if (input(source) == 0)
2597c478bd9Sstevel@tonic-gate 		return (0);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	/* put occurring chars in heap with their counts */
2627c478bd9Sstevel@tonic-gate 	diffbytes = -1;
2637c478bd9Sstevel@tonic-gate 	count[END] = 1;
2647c478bd9Sstevel@tonic-gate 	insize.lint.lng = n = 0;
2657c478bd9Sstevel@tonic-gate 	for (i = END; i >= 0; i--) {
2667c478bd9Sstevel@tonic-gate 		parent[i] = 0;
2677c478bd9Sstevel@tonic-gate 		if (count[i] > 0) {
2687c478bd9Sstevel@tonic-gate 			diffbytes++;
2697c478bd9Sstevel@tonic-gate 			insize.lint.lng += count[i];
2707c478bd9Sstevel@tonic-gate 			heap[++n].count = count[i];
2717c478bd9Sstevel@tonic-gate 			heap[n].node = i;
2727c478bd9Sstevel@tonic-gate 		}
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 	if (diffbytes == 1) {
2757c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(
2767c478bd9Sstevel@tonic-gate 			"pack: %s: trivial file - file unchanged\n"), source);
2777c478bd9Sstevel@tonic-gate 		return (0);
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate 	insize.lint.lng >>= 1;
2807c478bd9Sstevel@tonic-gate 	for (i = n/2; i >= 1; i--)
2817c478bd9Sstevel@tonic-gate 		heapify(i);
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	/* build Huffman tree */
2847c478bd9Sstevel@tonic-gate 	lastnode = END;
2857c478bd9Sstevel@tonic-gate 	while (n > 1) {
2867c478bd9Sstevel@tonic-gate 		parent[heap[1].node] = ++lastnode;
2877c478bd9Sstevel@tonic-gate 		inc = heap[1].count;
2887c478bd9Sstevel@tonic-gate 		hmove(heap[n], heap[1]);
2897c478bd9Sstevel@tonic-gate 		n--;
2907c478bd9Sstevel@tonic-gate 		heapify(1);
2917c478bd9Sstevel@tonic-gate 		parent[heap[1].node] = lastnode;
2927c478bd9Sstevel@tonic-gate 		heap[1].node = lastnode;
2937c478bd9Sstevel@tonic-gate 		heap[1].count += inc;
2947c478bd9Sstevel@tonic-gate 		heapify(1);
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 	parent[lastnode] = 0;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	/* assign lengths to encoding for each character */
2997c478bd9Sstevel@tonic-gate 	bitsout = maxlev = 0;
3007c478bd9Sstevel@tonic-gate 	for (i = 1; i <= 24; i++)
3017c478bd9Sstevel@tonic-gate 		levcount[i] = 0;
3027c478bd9Sstevel@tonic-gate 	for (i = 0; i <= END; i++) {
3037c478bd9Sstevel@tonic-gate 		c = 0;
3047c478bd9Sstevel@tonic-gate 		for (p = parent[i]; p != 0; p = parent[p])
3057c478bd9Sstevel@tonic-gate 			c++;
3067c478bd9Sstevel@tonic-gate 		levcount[c]++;
3077c478bd9Sstevel@tonic-gate 		length[i] = c;
3087c478bd9Sstevel@tonic-gate 		if (c > maxlev)
3097c478bd9Sstevel@tonic-gate 			maxlev = c;
3107c478bd9Sstevel@tonic-gate 		bitsout += c*(count[i]>>1);
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 	if (maxlev > 24) {
3137c478bd9Sstevel@tonic-gate 		/* can't occur unless insize.lint.lng >= 2**24 */
3147c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(
3157c478bd9Sstevel@tonic-gate 	"pack: %s: Huffman tree has too many levels - file unchanged\n"),
3167c478bd9Sstevel@tonic-gate 			source);
3177c478bd9Sstevel@tonic-gate 		return (0);
3187c478bd9Sstevel@tonic-gate 	}
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	/* don't bother if no compression results */
3217c478bd9Sstevel@tonic-gate 	outsize = ((bitsout+7)>>3)+6+maxlev+diffbytes;
3227c478bd9Sstevel@tonic-gate 	if ((insize.lint.lng+BUFSIZ-1)/BUFSIZ <=
3237c478bd9Sstevel@tonic-gate 				    (outsize+BUFSIZ-1)/BUFSIZ && !force) {
3247c478bd9Sstevel@tonic-gate 		printf(gettext(
3257c478bd9Sstevel@tonic-gate 			"pack: %s: no saving - file unchanged\n"), source);
3267c478bd9Sstevel@tonic-gate 		return (0);
3277c478bd9Sstevel@tonic-gate 	}
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	/* compute bit patterns for each character */
3307c478bd9Sstevel@tonic-gate 	inc = 1L << 24;
3317c478bd9Sstevel@tonic-gate 	inc >>= maxlev;
3327c478bd9Sstevel@tonic-gate 	mask.lint.lng = 0;
3337c478bd9Sstevel@tonic-gate 	for (i = maxlev; i > 0; i--) {
3347c478bd9Sstevel@tonic-gate 		for (c = 0; c <= END; c++)
3357c478bd9Sstevel@tonic-gate 			if (length[c] == i) {
3367c478bd9Sstevel@tonic-gate 				bits[c] = mask.lint.lng;
3377c478bd9Sstevel@tonic-gate 				mask.lint.lng += inc;
3387c478bd9Sstevel@tonic-gate 			}
3397c478bd9Sstevel@tonic-gate 		mask.lint.lng &= ~inc;
3407c478bd9Sstevel@tonic-gate 		inc <<= 1;
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	return (output(source));
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate 
346a77d64afScf46844 int
3477c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
3487c478bd9Sstevel@tonic-gate {
3497c478bd9Sstevel@tonic-gate 	extern  int optind;
3507c478bd9Sstevel@tonic-gate 	register int i;
3517c478bd9Sstevel@tonic-gate 	register char *cp;
3527c478bd9Sstevel@tonic-gate 	int k, sep, errflg = 0;
3537c478bd9Sstevel@tonic-gate 	int c;
354fa9e4066Sahrens 	int error;
3557c478bd9Sstevel@tonic-gate 	int fcount = 0; /* count failures */
356fa9e4066Sahrens 	acl_t *aclp = NULL;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3597c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
3607c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
3617c478bd9Sstevel@tonic-gate #endif
3627c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "f-")) != EOF) {
3657c478bd9Sstevel@tonic-gate 		if (c == 'f')
3667c478bd9Sstevel@tonic-gate 			force++;
3677c478bd9Sstevel@tonic-gate 		else
3687c478bd9Sstevel@tonic-gate 			++errflg;
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate 	/*
3717c478bd9Sstevel@tonic-gate 	 * Check for invalid option.  Also check for missing
3727c478bd9Sstevel@tonic-gate 	 * file operand, ie: "pack" or "pack -".
3737c478bd9Sstevel@tonic-gate 	 */
3747c478bd9Sstevel@tonic-gate 	argc -= optind;
3757c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
3767c478bd9Sstevel@tonic-gate 	if (errflg || argc < 1 ||
3777c478bd9Sstevel@tonic-gate 		(argc == 1 && argv[0][0] == '-' && argv[0][1] == '\0')) {
3787c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(
3797c478bd9Sstevel@tonic-gate 			"usage: pack [-f] [-] file...\n"));
3807c478bd9Sstevel@tonic-gate 		if (argc < 1 ||
3817c478bd9Sstevel@tonic-gate 			(argc == 1 && argv[0][0] == '-' &&
3827c478bd9Sstevel@tonic-gate 				argv[0][1] == '\0')) {
3837c478bd9Sstevel@tonic-gate 			/*
3847c478bd9Sstevel@tonic-gate 			 * return 1 for usage error when no file was specified
3857c478bd9Sstevel@tonic-gate 			 */
3867c478bd9Sstevel@tonic-gate 			return (1);
3877c478bd9Sstevel@tonic-gate 		}
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 	/* loop through the file names */
3907c478bd9Sstevel@tonic-gate 	for (k = 0; k < argc; k++) {
3917c478bd9Sstevel@tonic-gate 		if (argv[k][0] == '-' && argv[k][1] == '\0') {
3927c478bd9Sstevel@tonic-gate 			vflag = 1 - vflag;
3937c478bd9Sstevel@tonic-gate 			continue;
3947c478bd9Sstevel@tonic-gate 		}
3957c478bd9Sstevel@tonic-gate 		fcount++; /* increase failure count - expect the worst */
3967c478bd9Sstevel@tonic-gate 		if (errflg) {
3977c478bd9Sstevel@tonic-gate 			/*
3987c478bd9Sstevel@tonic-gate 			 * invalid option; just count the number of files not
3997c478bd9Sstevel@tonic-gate 			 * packed
4007c478bd9Sstevel@tonic-gate 			 */
4017c478bd9Sstevel@tonic-gate 			continue;
4027c478bd9Sstevel@tonic-gate 		}
4037c478bd9Sstevel@tonic-gate 		/* remove any .z suffix the user may have added */
4047c478bd9Sstevel@tonic-gate 		for (cp = argv[k]; *cp != '\0'; ++cp)
4057c478bd9Sstevel@tonic-gate 			;
4067c478bd9Sstevel@tonic-gate 		if (cp[-1] == SUF1 && cp[-2] == SUF0) {
4077c478bd9Sstevel@tonic-gate 			*cp-- = '\0'; *cp-- = '\0'; *cp = '\0';
4087c478bd9Sstevel@tonic-gate 		}
4097c478bd9Sstevel@tonic-gate 		sep = -1;  cp = filename;
4107c478bd9Sstevel@tonic-gate 		max_name = pathconf(argv[k], _PC_NAME_MAX);
4117c478bd9Sstevel@tonic-gate 		if (max_name == -1) {
4127c478bd9Sstevel@tonic-gate 			/* pathname invalid or no limit on length of filename */
4137c478bd9Sstevel@tonic-gate 			max_name = _POSIX_NAME_MAX;
4147c478bd9Sstevel@tonic-gate 		}
4157c478bd9Sstevel@tonic-gate 		/* copy argv[k] to filename and count chars in base name */
4167c478bd9Sstevel@tonic-gate 		for (i = 0; i < (MAXPATHLEN-3) && (*cp = argv[k][i]); i++)
4177c478bd9Sstevel@tonic-gate 			if (*cp++ == '/') sep = i;
4187c478bd9Sstevel@tonic-gate 		if ((infile = open(filename, 0)) < 0) {
4197c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(
4207c478bd9Sstevel@tonic-gate 				"pack: %s: cannot open: "), filename);
4217c478bd9Sstevel@tonic-gate 			perror("");
4227c478bd9Sstevel@tonic-gate 			continue;
4237c478bd9Sstevel@tonic-gate 		}
4247c478bd9Sstevel@tonic-gate 		if (i >= (MAXPATHLEN-3) || (i-sep) > (max_name - 1)) {
4257c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(
4267c478bd9Sstevel@tonic-gate 				"pack: %s: file name too long\n"), argv[k]);
4277c478bd9Sstevel@tonic-gate 			continue;
4287c478bd9Sstevel@tonic-gate 		}
4297c478bd9Sstevel@tonic-gate 		fstat(infile, &status);
4304bc0a2efScasper 		if (S_ISDIR(status.st_mode)) {
4317c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(
4327c478bd9Sstevel@tonic-gate 				"pack: %s: cannot pack a directory\n"),
4337c478bd9Sstevel@tonic-gate 				    argv[k]);
4347c478bd9Sstevel@tonic-gate 			goto closein;
4357c478bd9Sstevel@tonic-gate 		}
4367c478bd9Sstevel@tonic-gate 		if (status.st_size == 0) {
4377c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(
4387c478bd9Sstevel@tonic-gate 				"pack: %s: cannot pack a zero length file\n"),
4397c478bd9Sstevel@tonic-gate 				argv[k]);
4407c478bd9Sstevel@tonic-gate 			goto closein;
4417c478bd9Sstevel@tonic-gate 		}
4427c478bd9Sstevel@tonic-gate 		if (status.st_nlink != 1) {
4437c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(
4447c478bd9Sstevel@tonic-gate 				"pack: %s: has links\n"),
4457c478bd9Sstevel@tonic-gate 				argv[k]);
4467c478bd9Sstevel@tonic-gate 			goto closein;
4477c478bd9Sstevel@tonic-gate 		}
4487c478bd9Sstevel@tonic-gate 		*cp++ = SUF0;  *cp++ = SUF1;  *cp = '\0';
4497c478bd9Sstevel@tonic-gate 		if (stat(filename, &ostatus) != -1) {
4507c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(
4517c478bd9Sstevel@tonic-gate 				"pack: %s: already exists\n"), filename);
4527c478bd9Sstevel@tonic-gate 			goto closein;
4537c478bd9Sstevel@tonic-gate 		}
454fa9e4066Sahrens 
4557c478bd9Sstevel@tonic-gate 		if ((outfile = creat(filename, status.st_mode)) < 0) {
4567c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(
4577c478bd9Sstevel@tonic-gate 				"pack: %s: cannot create: "), filename);
4587c478bd9Sstevel@tonic-gate 			perror("");
4597c478bd9Sstevel@tonic-gate 			goto closein;
4607c478bd9Sstevel@tonic-gate 		}
4617c478bd9Sstevel@tonic-gate 
462fa9e4066Sahrens 		error = facl_get(infile, ACL_NO_TRIVIAL, &aclp);
463fa9e4066Sahrens 
464fa9e4066Sahrens 		if (error != 0) {
465fa9e4066Sahrens 			fprintf(stderr, gettext(
466fa9e4066Sahrens 			    "pack: %s: cannot retrieve ACL: %s\n"), argv[k],
467fa9e4066Sahrens 			    acl_strerror(error));
468fa9e4066Sahrens 		}
4697c478bd9Sstevel@tonic-gate 		if (packfile(argv[k]) &&
4707c478bd9Sstevel@tonic-gate 		    ((pathconf(argv[k], _PC_XATTR_EXISTS) != 1) ||
4717c478bd9Sstevel@tonic-gate 				(mv_xattrs(infile, outfile,
4727c478bd9Sstevel@tonic-gate 					argv[k], 0) == 0))) {
4737c478bd9Sstevel@tonic-gate 			if (unlink(argv[k]) != 0) {
4747c478bd9Sstevel@tonic-gate 				fprintf(stderr, gettext(
4757c478bd9Sstevel@tonic-gate 					"pack: %s: cannot unlink: "),
4767c478bd9Sstevel@tonic-gate 					argv[k]);
4777c478bd9Sstevel@tonic-gate 				perror("");
4787c478bd9Sstevel@tonic-gate 			}
4797c478bd9Sstevel@tonic-gate 			printf(gettext(
4807c478bd9Sstevel@tonic-gate 				"pack: %s: %.1f%% Compression\n"),
4817c478bd9Sstevel@tonic-gate 				argv[k],
4827c478bd9Sstevel@tonic-gate 	    ((double)(-outsize+(insize.lint.lng))/(double)insize.lint.lng)*100);
4837c478bd9Sstevel@tonic-gate 			/* output statistics */
4847c478bd9Sstevel@tonic-gate 			if (vflag) {
4857c478bd9Sstevel@tonic-gate 				printf(gettext("\tfrom %ld to %ld bytes\n"),
4867c478bd9Sstevel@tonic-gate 					insize.lint.lng, outsize);
4877c478bd9Sstevel@tonic-gate 				printf(gettext(
4887c478bd9Sstevel@tonic-gate 				"\tHuffman tree has %d levels below root\n"),
4897c478bd9Sstevel@tonic-gate 				    maxlev);
4907c478bd9Sstevel@tonic-gate 				printf(gettext(
4917c478bd9Sstevel@tonic-gate 					"\t%d distinct bytes in input\n"),
4927c478bd9Sstevel@tonic-gate 					diffbytes);
4937c478bd9Sstevel@tonic-gate 				printf(gettext(
4947c478bd9Sstevel@tonic-gate 					"\tdictionary overhead = %ld bytes\n"),
4957c478bd9Sstevel@tonic-gate 					dictsize);
4967c478bd9Sstevel@tonic-gate 				printf(gettext(
4977c478bd9Sstevel@tonic-gate 				    "\teffective  entropy  = %.2f bits/byte\n"),
4987c478bd9Sstevel@tonic-gate 			    ((double)outsize / (double)insize.lint.lng) * 8);
4997c478bd9Sstevel@tonic-gate 				printf(gettext(
5007c478bd9Sstevel@tonic-gate 				    "\tasymptotic entropy  = %.2f bits/byte\n"),
5017c478bd9Sstevel@tonic-gate 					((double)(outsize-dictsize) /
5027c478bd9Sstevel@tonic-gate 					(double)insize.lint.lng) * 8);
5037c478bd9Sstevel@tonic-gate 			}
5047c478bd9Sstevel@tonic-gate 			u_times.actime = status.st_atime;
5057c478bd9Sstevel@tonic-gate 			u_times.modtime = status.st_mtime;
5067c478bd9Sstevel@tonic-gate 			if (utime(filename, &u_times) != 0) {
5077c478bd9Sstevel@tonic-gate 				errflg++;
5087c478bd9Sstevel@tonic-gate 				fprintf(stderr,
5097c478bd9Sstevel@tonic-gate 					gettext(
5107c478bd9Sstevel@tonic-gate 					"pack: cannot change times on %s: "),
5117c478bd9Sstevel@tonic-gate 					filename);
5127c478bd9Sstevel@tonic-gate 				perror("");
5137c478bd9Sstevel@tonic-gate 			}
5147c478bd9Sstevel@tonic-gate 			if (chmod(filename, status.st_mode) != 0) {
5157c478bd9Sstevel@tonic-gate 				errflg++;
5167c478bd9Sstevel@tonic-gate 				fprintf(stderr,
5177c478bd9Sstevel@tonic-gate 					gettext(
5187c478bd9Sstevel@tonic-gate 				"pack: can't change mode to %o on %s: "),
5197c478bd9Sstevel@tonic-gate 					status.st_mode, filename);
5207c478bd9Sstevel@tonic-gate 				perror("");
5217c478bd9Sstevel@tonic-gate 			}
5227c478bd9Sstevel@tonic-gate 			chown(filename, status.st_uid, status.st_gid);
523fa9e4066Sahrens 			if (aclp && (facl_set(outfile, aclp) < 0)) {
524fa9e4066Sahrens 				fprintf(stderr, gettext(
525fa9e4066Sahrens 				    "pack: %s: failed to set acl entries\n"),
526fa9e4066Sahrens 				    filename);
527fa9e4066Sahrens 				perror("");
528fa9e4066Sahrens 			}
5297c478bd9Sstevel@tonic-gate 			if (!errflg)
5307c478bd9Sstevel@tonic-gate 				fcount--;  /* success after all */
5317c478bd9Sstevel@tonic-gate 		} else {
5327c478bd9Sstevel@tonic-gate 			if (pathconf(filename, _PC_XATTR_EXISTS) == 1) {
5337c478bd9Sstevel@tonic-gate 				(void) mv_xattrs(outfile, infile, filename, 1);
5347c478bd9Sstevel@tonic-gate 			}
5357c478bd9Sstevel@tonic-gate 			unlink(filename);
5367c478bd9Sstevel@tonic-gate 		}
537fa9e4066Sahrens 
538*f0a2d94fSmarks 		if (aclp) {
539fa9e4066Sahrens 			acl_free(aclp);
540*f0a2d94fSmarks 			aclp = NULL;
541*f0a2d94fSmarks 		}
542fa9e4066Sahrens 
5437c478bd9Sstevel@tonic-gate closein:	close(outfile);
5447c478bd9Sstevel@tonic-gate 		close(infile);
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 	return (fcount);
5477c478bd9Sstevel@tonic-gate }
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate /*
5507c478bd9Sstevel@tonic-gate  * mv_xattrs - move (via renameat) all of the extended attributes
5517c478bd9Sstevel@tonic-gate  *	associated with the file referenced by infd to the file
5527c478bd9Sstevel@tonic-gate  *	referenced by outfd.  The infile and silent arguments are
5537c478bd9Sstevel@tonic-gate  *	provided for error message processing.  This function
5547c478bd9Sstevel@tonic-gate  *	returns 0 on success and -1 on error.
5557c478bd9Sstevel@tonic-gate  */
5567c478bd9Sstevel@tonic-gate static int
5577c478bd9Sstevel@tonic-gate mv_xattrs(int infd, int outfd, char *infile, int silent)
5587c478bd9Sstevel@tonic-gate {
5597c478bd9Sstevel@tonic-gate 	int indfd, outdfd, tmpfd;
5607c478bd9Sstevel@tonic-gate 	DIR *dirp = NULL;
5617c478bd9Sstevel@tonic-gate 	struct dirent *dp = NULL;
5627c478bd9Sstevel@tonic-gate 	int error = 0;
5637c478bd9Sstevel@tonic-gate 	char *etext;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	indfd = outdfd = tmpfd = -1;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	if ((indfd = openat(infd, ".", O_RDONLY|O_XATTR)) == -1) {
5687c478bd9Sstevel@tonic-gate 		etext = gettext("cannot open source");
5697c478bd9Sstevel@tonic-gate 		error = -1;
5707c478bd9Sstevel@tonic-gate 		goto out;
5717c478bd9Sstevel@tonic-gate 	}
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	if ((outdfd = openat(outfd, ".", O_RDONLY|O_XATTR)) == -1) {
5747c478bd9Sstevel@tonic-gate 		etext = gettext("cannot open target");
5757c478bd9Sstevel@tonic-gate 		error = -1;
5767c478bd9Sstevel@tonic-gate 		goto out;
5777c478bd9Sstevel@tonic-gate 	}
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	if ((tmpfd = dup(indfd)) == -1) {
5807c478bd9Sstevel@tonic-gate 		etext = gettext("cannot dup descriptor");
5817c478bd9Sstevel@tonic-gate 		error = -1;
5827c478bd9Sstevel@tonic-gate 		goto out;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	}
5857c478bd9Sstevel@tonic-gate 	if ((dirp = fdopendir(tmpfd)) == NULL) {
5867c478bd9Sstevel@tonic-gate 		etext = gettext("cannot access source");
5877c478bd9Sstevel@tonic-gate 		error = -1;
5887c478bd9Sstevel@tonic-gate 		goto out;
5897c478bd9Sstevel@tonic-gate 	}
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	while (dp = readdir(dirp)) {
5927c478bd9Sstevel@tonic-gate 		if ((dp->d_name[0] == '.' && dp->d_name[1] == '\0') ||
5937c478bd9Sstevel@tonic-gate 		    (dp->d_name[0] == '.' && dp->d_name[1] == '.' &&
5947c478bd9Sstevel@tonic-gate 		    dp->d_name[2] == '\0'))
5957c478bd9Sstevel@tonic-gate 			continue;
5967c478bd9Sstevel@tonic-gate 		if ((renameat(indfd, dp->d_name, outdfd, dp->d_name)) == -1) {
5977c478bd9Sstevel@tonic-gate 			etext = dp->d_name;
5987c478bd9Sstevel@tonic-gate 			error = -1;
5997c478bd9Sstevel@tonic-gate 			goto out;
6007c478bd9Sstevel@tonic-gate 		}
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate out:
6037c478bd9Sstevel@tonic-gate 	if (error == -1 && silent == 0) {
6047c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(
6057c478bd9Sstevel@tonic-gate 			"pack: %s: cannot move extended attributes, "),
6067c478bd9Sstevel@tonic-gate 			infile);
6077c478bd9Sstevel@tonic-gate 		perror(etext);
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate 	if (dirp)
6107c478bd9Sstevel@tonic-gate 		closedir(dirp);
6117c478bd9Sstevel@tonic-gate 	if (indfd != -1)
6127c478bd9Sstevel@tonic-gate 		close(indfd);
6137c478bd9Sstevel@tonic-gate 	if (outdfd != -1)
6147c478bd9Sstevel@tonic-gate 		close(outdfd);
6157c478bd9Sstevel@tonic-gate 	return (error);
6167c478bd9Sstevel@tonic-gate }
617