xref: /titanic_51/usr/src/cmd/pack/pack.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
30*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate /*
34*7c478bd9Sstevel@tonic-gate  *	Huffman encoding program
35*7c478bd9Sstevel@tonic-gate  *	Usage:	pack [[ -f ] [ - ] filename ... ] filename ...
36*7c478bd9Sstevel@tonic-gate  *		- option: enable/disable listing of statistics
37*7c478bd9Sstevel@tonic-gate  */
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include <stdio.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
42*7c478bd9Sstevel@tonic-gate #include <unistd.h>
43*7c478bd9Sstevel@tonic-gate #include <locale.h>
44*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
45*7c478bd9Sstevel@tonic-gate #include <errno.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
47*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
48*7c478bd9Sstevel@tonic-gate #include <limits.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
50*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
51*7c478bd9Sstevel@tonic-gate #include <utime.h>
52*7c478bd9Sstevel@tonic-gate #include <string.h>
53*7c478bd9Sstevel@tonic-gate #include <dirent.h>
54*7c478bd9Sstevel@tonic-gate #include <unistd.h>
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate #undef lint
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate #define	END	256
59*7c478bd9Sstevel@tonic-gate #define	PACKED 017436 /* <US><RS> - Unlikely value */
60*7c478bd9Sstevel@tonic-gate #define	SUF0	'.'
61*7c478bd9Sstevel@tonic-gate #define	SUF1	'z'
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate struct stat status, ostatus;
64*7c478bd9Sstevel@tonic-gate static struct utimbuf u_times;
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate /* union for overlaying a long int with a set of four characters */
67*7c478bd9Sstevel@tonic-gate union FOUR {
68*7c478bd9Sstevel@tonic-gate 	struct { long int lng; } lint;
69*7c478bd9Sstevel@tonic-gate 	struct { char c0, c1, c2, c3; } chars;
70*7c478bd9Sstevel@tonic-gate };
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate /* character counters */
73*7c478bd9Sstevel@tonic-gate long	count [END+1];
74*7c478bd9Sstevel@tonic-gate union	FOUR insize;
75*7c478bd9Sstevel@tonic-gate long	outsize;
76*7c478bd9Sstevel@tonic-gate long	dictsize;
77*7c478bd9Sstevel@tonic-gate int	diffbytes;
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate /* i/o stuff */
80*7c478bd9Sstevel@tonic-gate char	vflag = 0;
81*7c478bd9Sstevel@tonic-gate int	force = 0;	/* allow forced packing for consistency in directory */
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate static	char filename [MAXPATHLEN];
84*7c478bd9Sstevel@tonic-gate static int max_name;
85*7c478bd9Sstevel@tonic-gate static int max_path = MAXPATHLEN;
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate int	infile;		/* unpacked file */
88*7c478bd9Sstevel@tonic-gate int	outfile;	/* packed file */
89*7c478bd9Sstevel@tonic-gate char	inbuff [BUFSIZ];
90*7c478bd9Sstevel@tonic-gate char	outbuff [BUFSIZ+4];
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate /* variables associated with the tree */
93*7c478bd9Sstevel@tonic-gate int	maxlev;
94*7c478bd9Sstevel@tonic-gate int	levcount [25];
95*7c478bd9Sstevel@tonic-gate int	lastnode;
96*7c478bd9Sstevel@tonic-gate int	parent [2*END+1];
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate /* variables associated with the encoding process */
99*7c478bd9Sstevel@tonic-gate char	length [END+1];
100*7c478bd9Sstevel@tonic-gate long	bits [END+1];
101*7c478bd9Sstevel@tonic-gate union	FOUR mask;
102*7c478bd9Sstevel@tonic-gate long	inc;
103*7c478bd9Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN)
104*7c478bd9Sstevel@tonic-gate char	*maskshuff[4]  = {&(mask.chars.c3),
105*7c478bd9Sstevel@tonic-gate 			    &(mask.chars.c2),
106*7c478bd9Sstevel@tonic-gate 			    &(mask.chars.c1),
107*7c478bd9Sstevel@tonic-gate 			    &(mask.chars.c0)};
108*7c478bd9Sstevel@tonic-gate #elif defined(_BIG_ENDIAN)
109*7c478bd9Sstevel@tonic-gate char	*maskshuff[4]  = {&(mask.chars.c0),
110*7c478bd9Sstevel@tonic-gate 			    &(mask.chars.c1),
111*7c478bd9Sstevel@tonic-gate 			    &(mask.chars.c2),
112*7c478bd9Sstevel@tonic-gate 			    &(mask.chars.c3)};
113*7c478bd9Sstevel@tonic-gate #else
114*7c478bd9Sstevel@tonic-gate #error Unknown byte ordering!
115*7c478bd9Sstevel@tonic-gate #endif
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate /* the heap */
118*7c478bd9Sstevel@tonic-gate int	n;
119*7c478bd9Sstevel@tonic-gate struct	heap {
120*7c478bd9Sstevel@tonic-gate 	long int count;
121*7c478bd9Sstevel@tonic-gate 	int node;
122*7c478bd9Sstevel@tonic-gate } heap [END+2];
123*7c478bd9Sstevel@tonic-gate #define	hmove(a, b) {(b).count = (a).count; (b).node = (a).node; }
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate static void heapify(int i);
126*7c478bd9Sstevel@tonic-gate static int mv_xattrs(int, int, char *, int);
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate /* gather character frequency statistics */
129*7c478bd9Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */
130*7c478bd9Sstevel@tonic-gate input(char *source)
131*7c478bd9Sstevel@tonic-gate {
132*7c478bd9Sstevel@tonic-gate 	register int i;
133*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < END; i++)
134*7c478bd9Sstevel@tonic-gate 		count[i] = 0;
135*7c478bd9Sstevel@tonic-gate 	while ((i = read(infile, inbuff, BUFSIZ)) > 0)
136*7c478bd9Sstevel@tonic-gate 		while (i > 0)
137*7c478bd9Sstevel@tonic-gate 			count[inbuff[--i]&0377] += 2;
138*7c478bd9Sstevel@tonic-gate 	if (i == 0)
139*7c478bd9Sstevel@tonic-gate 		return (1);
140*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext(
141*7c478bd9Sstevel@tonic-gate 		"pack: %s: read error - file unchanged: "), source);
142*7c478bd9Sstevel@tonic-gate 	perror("");
143*7c478bd9Sstevel@tonic-gate 	return (0);
144*7c478bd9Sstevel@tonic-gate }
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate /* encode the current file */
147*7c478bd9Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */
148*7c478bd9Sstevel@tonic-gate output(char *source)
149*7c478bd9Sstevel@tonic-gate {
150*7c478bd9Sstevel@tonic-gate 	int c, i, inleft;
151*7c478bd9Sstevel@tonic-gate 	char *inp;
152*7c478bd9Sstevel@tonic-gate 	register char **q, *outp;
153*7c478bd9Sstevel@tonic-gate 	register int bitsleft;
154*7c478bd9Sstevel@tonic-gate 	long temp;
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	/* output ``PACKED'' header */
157*7c478bd9Sstevel@tonic-gate 	outbuff[0] = 037; 	/* ascii US */
158*7c478bd9Sstevel@tonic-gate 	outbuff[1] = 036; 	/* ascii RS */
159*7c478bd9Sstevel@tonic-gate 	/* output the length and the dictionary */
160*7c478bd9Sstevel@tonic-gate 	temp = insize.lint.lng;
161*7c478bd9Sstevel@tonic-gate 	for (i = 5; i >= 2; i--) {
162*7c478bd9Sstevel@tonic-gate 		outbuff[i] =  (char)(temp & 0377);
163*7c478bd9Sstevel@tonic-gate 		temp >>= 8;
164*7c478bd9Sstevel@tonic-gate 	}
165*7c478bd9Sstevel@tonic-gate 	outp = &outbuff[6];
166*7c478bd9Sstevel@tonic-gate 	*outp++ = maxlev;
167*7c478bd9Sstevel@tonic-gate 	for (i = 1; i < maxlev; i++)
168*7c478bd9Sstevel@tonic-gate 		*outp++ = levcount[i];
169*7c478bd9Sstevel@tonic-gate 	*outp++ = levcount[maxlev]-2;
170*7c478bd9Sstevel@tonic-gate 	for (i = 1; i <= maxlev; i++)
171*7c478bd9Sstevel@tonic-gate 		for (c = 0; c < END; c++)
172*7c478bd9Sstevel@tonic-gate 			if (length[c] == i)
173*7c478bd9Sstevel@tonic-gate 				*outp++ = c;
174*7c478bd9Sstevel@tonic-gate 	dictsize = outp-&outbuff[0];
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 	/* output the text */
177*7c478bd9Sstevel@tonic-gate 	lseek(infile, 0L, 0);
178*7c478bd9Sstevel@tonic-gate 	outsize = 0;
179*7c478bd9Sstevel@tonic-gate 	bitsleft = 8;
180*7c478bd9Sstevel@tonic-gate 	inleft = 0;
181*7c478bd9Sstevel@tonic-gate 	do {
182*7c478bd9Sstevel@tonic-gate 		if (inleft <= 0) {
183*7c478bd9Sstevel@tonic-gate 			inleft = read(infile, inp = &inbuff[0], BUFSIZ);
184*7c478bd9Sstevel@tonic-gate 			if (inleft < 0) {
185*7c478bd9Sstevel@tonic-gate 				fprintf(stderr, gettext(
186*7c478bd9Sstevel@tonic-gate 				    "pack: %s: read error - file unchanged: "),
187*7c478bd9Sstevel@tonic-gate 					    source);
188*7c478bd9Sstevel@tonic-gate 				perror("");
189*7c478bd9Sstevel@tonic-gate 				return (0);
190*7c478bd9Sstevel@tonic-gate 			}
191*7c478bd9Sstevel@tonic-gate 		}
192*7c478bd9Sstevel@tonic-gate 		c = (--inleft < 0) ? END : (*inp++ & 0377);
193*7c478bd9Sstevel@tonic-gate 		mask.lint.lng = bits[c]<<bitsleft;
194*7c478bd9Sstevel@tonic-gate 		q = &maskshuff[0];
195*7c478bd9Sstevel@tonic-gate 		if (bitsleft == 8)
196*7c478bd9Sstevel@tonic-gate 			*outp = **q++;
197*7c478bd9Sstevel@tonic-gate 		else
198*7c478bd9Sstevel@tonic-gate 			*outp |= **q++;
199*7c478bd9Sstevel@tonic-gate 		bitsleft -= length[c];
200*7c478bd9Sstevel@tonic-gate 		while (bitsleft < 0) {
201*7c478bd9Sstevel@tonic-gate 			*++outp = **q++;
202*7c478bd9Sstevel@tonic-gate 			bitsleft += 8;
203*7c478bd9Sstevel@tonic-gate 		}
204*7c478bd9Sstevel@tonic-gate 		if (outp >= &outbuff[BUFSIZ]) {
205*7c478bd9Sstevel@tonic-gate 			if (write(outfile, outbuff, BUFSIZ) != BUFSIZ) {
206*7c478bd9Sstevel@tonic-gate wrerr:				fprintf(stderr, gettext(
207*7c478bd9Sstevel@tonic-gate 				"pack: %s.z: write error - file unchanged: "),
208*7c478bd9Sstevel@tonic-gate 					source);
209*7c478bd9Sstevel@tonic-gate 				perror("");
210*7c478bd9Sstevel@tonic-gate 				return (0);
211*7c478bd9Sstevel@tonic-gate 			}
212*7c478bd9Sstevel@tonic-gate 			((union FOUR *)outbuff)->lint.lng =
213*7c478bd9Sstevel@tonic-gate 				    ((union FOUR *)&outbuff[BUFSIZ])->lint.lng;
214*7c478bd9Sstevel@tonic-gate 			outp -= BUFSIZ;
215*7c478bd9Sstevel@tonic-gate 			outsize += BUFSIZ;
216*7c478bd9Sstevel@tonic-gate 		}
217*7c478bd9Sstevel@tonic-gate 	} while (c != END);
218*7c478bd9Sstevel@tonic-gate 	if (bitsleft < 8)
219*7c478bd9Sstevel@tonic-gate 		outp++;
220*7c478bd9Sstevel@tonic-gate 	c = outp-outbuff;
221*7c478bd9Sstevel@tonic-gate 	if (write(outfile, outbuff, c) != c)
222*7c478bd9Sstevel@tonic-gate 		goto wrerr;
223*7c478bd9Sstevel@tonic-gate 	outsize += c;
224*7c478bd9Sstevel@tonic-gate 	return (1);
225*7c478bd9Sstevel@tonic-gate }
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate /* makes a heap out of heap[i],...,heap[n] */
228*7c478bd9Sstevel@tonic-gate void
229*7c478bd9Sstevel@tonic-gate heapify(int i)
230*7c478bd9Sstevel@tonic-gate {
231*7c478bd9Sstevel@tonic-gate 	register int k;
232*7c478bd9Sstevel@tonic-gate 	int lastparent;
233*7c478bd9Sstevel@tonic-gate 	struct heap heapsubi;
234*7c478bd9Sstevel@tonic-gate 	hmove(heap[i], heapsubi);
235*7c478bd9Sstevel@tonic-gate 	lastparent = n/2;
236*7c478bd9Sstevel@tonic-gate 	while (i <= lastparent) {
237*7c478bd9Sstevel@tonic-gate 		k = 2*i;
238*7c478bd9Sstevel@tonic-gate 		if (heap[k].count > heap[k+1].count && k < n)
239*7c478bd9Sstevel@tonic-gate 			k++;
240*7c478bd9Sstevel@tonic-gate 		if (heapsubi.count < heap[k].count)
241*7c478bd9Sstevel@tonic-gate 			break;
242*7c478bd9Sstevel@tonic-gate 		hmove(heap[k], heap[i]);
243*7c478bd9Sstevel@tonic-gate 		i = k;
244*7c478bd9Sstevel@tonic-gate 	}
245*7c478bd9Sstevel@tonic-gate 	hmove(heapsubi, heap[i]);
246*7c478bd9Sstevel@tonic-gate }
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate /* return 1 after successful packing, 0 otherwise */
249*7c478bd9Sstevel@tonic-gate int
250*7c478bd9Sstevel@tonic-gate packfile(char *source)
251*7c478bd9Sstevel@tonic-gate {
252*7c478bd9Sstevel@tonic-gate 	register int c, i, p;
253*7c478bd9Sstevel@tonic-gate 	long bitsout;
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 	/* gather frequency statistics */
256*7c478bd9Sstevel@tonic-gate 	if (input(source) == 0)
257*7c478bd9Sstevel@tonic-gate 		return (0);
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 	/* put occurring chars in heap with their counts */
260*7c478bd9Sstevel@tonic-gate 	diffbytes = -1;
261*7c478bd9Sstevel@tonic-gate 	count[END] = 1;
262*7c478bd9Sstevel@tonic-gate 	insize.lint.lng = n = 0;
263*7c478bd9Sstevel@tonic-gate 	for (i = END; i >= 0; i--) {
264*7c478bd9Sstevel@tonic-gate 		parent[i] = 0;
265*7c478bd9Sstevel@tonic-gate 		if (count[i] > 0) {
266*7c478bd9Sstevel@tonic-gate 			diffbytes++;
267*7c478bd9Sstevel@tonic-gate 			insize.lint.lng += count[i];
268*7c478bd9Sstevel@tonic-gate 			heap[++n].count = count[i];
269*7c478bd9Sstevel@tonic-gate 			heap[n].node = i;
270*7c478bd9Sstevel@tonic-gate 		}
271*7c478bd9Sstevel@tonic-gate 	}
272*7c478bd9Sstevel@tonic-gate 	if (diffbytes == 1) {
273*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(
274*7c478bd9Sstevel@tonic-gate 			"pack: %s: trivial file - file unchanged\n"), source);
275*7c478bd9Sstevel@tonic-gate 		return (0);
276*7c478bd9Sstevel@tonic-gate 	}
277*7c478bd9Sstevel@tonic-gate 	insize.lint.lng >>= 1;
278*7c478bd9Sstevel@tonic-gate 	for (i = n/2; i >= 1; i--)
279*7c478bd9Sstevel@tonic-gate 		heapify(i);
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate 	/* build Huffman tree */
282*7c478bd9Sstevel@tonic-gate 	lastnode = END;
283*7c478bd9Sstevel@tonic-gate 	while (n > 1) {
284*7c478bd9Sstevel@tonic-gate 		parent[heap[1].node] = ++lastnode;
285*7c478bd9Sstevel@tonic-gate 		inc = heap[1].count;
286*7c478bd9Sstevel@tonic-gate 		hmove(heap[n], heap[1]);
287*7c478bd9Sstevel@tonic-gate 		n--;
288*7c478bd9Sstevel@tonic-gate 		heapify(1);
289*7c478bd9Sstevel@tonic-gate 		parent[heap[1].node] = lastnode;
290*7c478bd9Sstevel@tonic-gate 		heap[1].node = lastnode;
291*7c478bd9Sstevel@tonic-gate 		heap[1].count += inc;
292*7c478bd9Sstevel@tonic-gate 		heapify(1);
293*7c478bd9Sstevel@tonic-gate 	}
294*7c478bd9Sstevel@tonic-gate 	parent[lastnode] = 0;
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate 	/* assign lengths to encoding for each character */
297*7c478bd9Sstevel@tonic-gate 	bitsout = maxlev = 0;
298*7c478bd9Sstevel@tonic-gate 	for (i = 1; i <= 24; i++)
299*7c478bd9Sstevel@tonic-gate 		levcount[i] = 0;
300*7c478bd9Sstevel@tonic-gate 	for (i = 0; i <= END; i++) {
301*7c478bd9Sstevel@tonic-gate 		c = 0;
302*7c478bd9Sstevel@tonic-gate 		for (p = parent[i]; p != 0; p = parent[p])
303*7c478bd9Sstevel@tonic-gate 			c++;
304*7c478bd9Sstevel@tonic-gate 		levcount[c]++;
305*7c478bd9Sstevel@tonic-gate 		length[i] = c;
306*7c478bd9Sstevel@tonic-gate 		if (c > maxlev)
307*7c478bd9Sstevel@tonic-gate 			maxlev = c;
308*7c478bd9Sstevel@tonic-gate 		bitsout += c*(count[i]>>1);
309*7c478bd9Sstevel@tonic-gate 	}
310*7c478bd9Sstevel@tonic-gate 	if (maxlev > 24) {
311*7c478bd9Sstevel@tonic-gate 		/* can't occur unless insize.lint.lng >= 2**24 */
312*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(
313*7c478bd9Sstevel@tonic-gate 	"pack: %s: Huffman tree has too many levels - file unchanged\n"),
314*7c478bd9Sstevel@tonic-gate 			source);
315*7c478bd9Sstevel@tonic-gate 		return (0);
316*7c478bd9Sstevel@tonic-gate 	}
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 	/* don't bother if no compression results */
319*7c478bd9Sstevel@tonic-gate 	outsize = ((bitsout+7)>>3)+6+maxlev+diffbytes;
320*7c478bd9Sstevel@tonic-gate 	if ((insize.lint.lng+BUFSIZ-1)/BUFSIZ <=
321*7c478bd9Sstevel@tonic-gate 				    (outsize+BUFSIZ-1)/BUFSIZ && !force) {
322*7c478bd9Sstevel@tonic-gate 		printf(gettext(
323*7c478bd9Sstevel@tonic-gate 			"pack: %s: no saving - file unchanged\n"), source);
324*7c478bd9Sstevel@tonic-gate 		return (0);
325*7c478bd9Sstevel@tonic-gate 	}
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 	/* compute bit patterns for each character */
328*7c478bd9Sstevel@tonic-gate 	inc = 1L << 24;
329*7c478bd9Sstevel@tonic-gate 	inc >>= maxlev;
330*7c478bd9Sstevel@tonic-gate 	mask.lint.lng = 0;
331*7c478bd9Sstevel@tonic-gate 	for (i = maxlev; i > 0; i--) {
332*7c478bd9Sstevel@tonic-gate 		for (c = 0; c <= END; c++)
333*7c478bd9Sstevel@tonic-gate 			if (length[c] == i) {
334*7c478bd9Sstevel@tonic-gate 				bits[c] = mask.lint.lng;
335*7c478bd9Sstevel@tonic-gate 				mask.lint.lng += inc;
336*7c478bd9Sstevel@tonic-gate 			}
337*7c478bd9Sstevel@tonic-gate 		mask.lint.lng &= ~inc;
338*7c478bd9Sstevel@tonic-gate 		inc <<= 1;
339*7c478bd9Sstevel@tonic-gate 	}
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 	return (output(source));
342*7c478bd9Sstevel@tonic-gate }
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
345*7c478bd9Sstevel@tonic-gate {
346*7c478bd9Sstevel@tonic-gate 	extern  int optind;
347*7c478bd9Sstevel@tonic-gate 	register int i;
348*7c478bd9Sstevel@tonic-gate 	register char *cp;
349*7c478bd9Sstevel@tonic-gate 	int k, sep, errflg = 0;
350*7c478bd9Sstevel@tonic-gate 	int c;
351*7c478bd9Sstevel@tonic-gate 	int fcount = 0; /* count failures */
352*7c478bd9Sstevel@tonic-gate 
353*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
354*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
355*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
356*7c478bd9Sstevel@tonic-gate #endif
357*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "f-")) != EOF) {
360*7c478bd9Sstevel@tonic-gate 		if (c == 'f')
361*7c478bd9Sstevel@tonic-gate 			force++;
362*7c478bd9Sstevel@tonic-gate 		else
363*7c478bd9Sstevel@tonic-gate 			++errflg;
364*7c478bd9Sstevel@tonic-gate 	}
365*7c478bd9Sstevel@tonic-gate 	/*
366*7c478bd9Sstevel@tonic-gate 	 * Check for invalid option.  Also check for missing
367*7c478bd9Sstevel@tonic-gate 	 * file operand, ie: "pack" or "pack -".
368*7c478bd9Sstevel@tonic-gate 	 */
369*7c478bd9Sstevel@tonic-gate 	argc -= optind;
370*7c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
371*7c478bd9Sstevel@tonic-gate 	if (errflg || argc < 1 ||
372*7c478bd9Sstevel@tonic-gate 		(argc == 1 && argv[0][0] == '-' && argv[0][1] == '\0')) {
373*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(
374*7c478bd9Sstevel@tonic-gate 			"usage: pack [-f] [-] file...\n"));
375*7c478bd9Sstevel@tonic-gate 		if (argc < 1 ||
376*7c478bd9Sstevel@tonic-gate 			(argc == 1 && argv[0][0] == '-' &&
377*7c478bd9Sstevel@tonic-gate 				argv[0][1] == '\0')) {
378*7c478bd9Sstevel@tonic-gate 			/*
379*7c478bd9Sstevel@tonic-gate 			 * return 1 for usage error when no file was specified
380*7c478bd9Sstevel@tonic-gate 			 */
381*7c478bd9Sstevel@tonic-gate 			return (1);
382*7c478bd9Sstevel@tonic-gate 		}
383*7c478bd9Sstevel@tonic-gate 	}
384*7c478bd9Sstevel@tonic-gate 	/* loop through the file names */
385*7c478bd9Sstevel@tonic-gate 	for (k = 0; k < argc; k++) {
386*7c478bd9Sstevel@tonic-gate 		if (argv[k][0] == '-' && argv[k][1] == '\0') {
387*7c478bd9Sstevel@tonic-gate 			vflag = 1 - vflag;
388*7c478bd9Sstevel@tonic-gate 			continue;
389*7c478bd9Sstevel@tonic-gate 		}
390*7c478bd9Sstevel@tonic-gate 		fcount++; /* increase failure count - expect the worst */
391*7c478bd9Sstevel@tonic-gate 		if (errflg) {
392*7c478bd9Sstevel@tonic-gate 			/*
393*7c478bd9Sstevel@tonic-gate 			 * invalid option; just count the number of files not
394*7c478bd9Sstevel@tonic-gate 			 * packed
395*7c478bd9Sstevel@tonic-gate 			 */
396*7c478bd9Sstevel@tonic-gate 			continue;
397*7c478bd9Sstevel@tonic-gate 		}
398*7c478bd9Sstevel@tonic-gate 		/* remove any .z suffix the user may have added */
399*7c478bd9Sstevel@tonic-gate 		for (cp = argv[k]; *cp != '\0'; ++cp)
400*7c478bd9Sstevel@tonic-gate 			;
401*7c478bd9Sstevel@tonic-gate 		if (cp[-1] == SUF1 && cp[-2] == SUF0) {
402*7c478bd9Sstevel@tonic-gate 			*cp-- = '\0'; *cp-- = '\0'; *cp = '\0';
403*7c478bd9Sstevel@tonic-gate 		}
404*7c478bd9Sstevel@tonic-gate 		sep = -1;  cp = filename;
405*7c478bd9Sstevel@tonic-gate 		max_name = pathconf(argv[k], _PC_NAME_MAX);
406*7c478bd9Sstevel@tonic-gate 		if (max_name == -1) {
407*7c478bd9Sstevel@tonic-gate 			/* pathname invalid or no limit on length of filename */
408*7c478bd9Sstevel@tonic-gate 			max_name = _POSIX_NAME_MAX;
409*7c478bd9Sstevel@tonic-gate 		}
410*7c478bd9Sstevel@tonic-gate 		/* copy argv[k] to filename and count chars in base name */
411*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < (MAXPATHLEN-3) && (*cp = argv[k][i]); i++)
412*7c478bd9Sstevel@tonic-gate 			if (*cp++ == '/') sep = i;
413*7c478bd9Sstevel@tonic-gate 		if ((infile = open(filename, 0)) < 0) {
414*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(
415*7c478bd9Sstevel@tonic-gate 				"pack: %s: cannot open: "), filename);
416*7c478bd9Sstevel@tonic-gate 			perror("");
417*7c478bd9Sstevel@tonic-gate 			continue;
418*7c478bd9Sstevel@tonic-gate 		}
419*7c478bd9Sstevel@tonic-gate 		if (i >= (MAXPATHLEN-3) || (i-sep) > (max_name - 1)) {
420*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(
421*7c478bd9Sstevel@tonic-gate 				"pack: %s: file name too long\n"), argv[k]);
422*7c478bd9Sstevel@tonic-gate 			continue;
423*7c478bd9Sstevel@tonic-gate 		}
424*7c478bd9Sstevel@tonic-gate 		fstat(infile, &status);
425*7c478bd9Sstevel@tonic-gate 		if (status.st_mode&S_IFDIR) {
426*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(
427*7c478bd9Sstevel@tonic-gate 				"pack: %s: cannot pack a directory\n"),
428*7c478bd9Sstevel@tonic-gate 				    argv[k]);
429*7c478bd9Sstevel@tonic-gate 			goto closein;
430*7c478bd9Sstevel@tonic-gate 		}
431*7c478bd9Sstevel@tonic-gate 		if (status.st_size == 0) {
432*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(
433*7c478bd9Sstevel@tonic-gate 				"pack: %s: cannot pack a zero length file\n"),
434*7c478bd9Sstevel@tonic-gate 				argv[k]);
435*7c478bd9Sstevel@tonic-gate 			goto closein;
436*7c478bd9Sstevel@tonic-gate 		}
437*7c478bd9Sstevel@tonic-gate 		if (status.st_nlink != 1) {
438*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(
439*7c478bd9Sstevel@tonic-gate 				"pack: %s: has links\n"),
440*7c478bd9Sstevel@tonic-gate 				argv[k]);
441*7c478bd9Sstevel@tonic-gate 			goto closein;
442*7c478bd9Sstevel@tonic-gate 		}
443*7c478bd9Sstevel@tonic-gate 		*cp++ = SUF0;  *cp++ = SUF1;  *cp = '\0';
444*7c478bd9Sstevel@tonic-gate 		if (stat(filename, &ostatus) != -1) {
445*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(
446*7c478bd9Sstevel@tonic-gate 				"pack: %s: already exists\n"), filename);
447*7c478bd9Sstevel@tonic-gate 			goto closein;
448*7c478bd9Sstevel@tonic-gate 		}
449*7c478bd9Sstevel@tonic-gate 		if ((outfile = creat(filename, status.st_mode)) < 0) {
450*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(
451*7c478bd9Sstevel@tonic-gate 				"pack: %s: cannot create: "), filename);
452*7c478bd9Sstevel@tonic-gate 			perror("");
453*7c478bd9Sstevel@tonic-gate 			goto closein;
454*7c478bd9Sstevel@tonic-gate 		}
455*7c478bd9Sstevel@tonic-gate 
456*7c478bd9Sstevel@tonic-gate 		if (packfile(argv[k]) &&
457*7c478bd9Sstevel@tonic-gate 		    ((pathconf(argv[k], _PC_XATTR_EXISTS) != 1) ||
458*7c478bd9Sstevel@tonic-gate 				(mv_xattrs(infile, outfile,
459*7c478bd9Sstevel@tonic-gate 					argv[k], 0) == 0))) {
460*7c478bd9Sstevel@tonic-gate 			if (unlink(argv[k]) != 0) {
461*7c478bd9Sstevel@tonic-gate 				fprintf(stderr, gettext(
462*7c478bd9Sstevel@tonic-gate 					"pack: %s: cannot unlink: "),
463*7c478bd9Sstevel@tonic-gate 					argv[k]);
464*7c478bd9Sstevel@tonic-gate 				perror("");
465*7c478bd9Sstevel@tonic-gate 			}
466*7c478bd9Sstevel@tonic-gate 			printf(gettext(
467*7c478bd9Sstevel@tonic-gate 				"pack: %s: %.1f%% Compression\n"),
468*7c478bd9Sstevel@tonic-gate 				argv[k],
469*7c478bd9Sstevel@tonic-gate 	    ((double)(-outsize+(insize.lint.lng))/(double)insize.lint.lng)*100);
470*7c478bd9Sstevel@tonic-gate 			/* output statistics */
471*7c478bd9Sstevel@tonic-gate 			if (vflag) {
472*7c478bd9Sstevel@tonic-gate 				printf(gettext("\tfrom %ld to %ld bytes\n"),
473*7c478bd9Sstevel@tonic-gate 					insize.lint.lng, outsize);
474*7c478bd9Sstevel@tonic-gate 				printf(gettext(
475*7c478bd9Sstevel@tonic-gate 				"\tHuffman tree has %d levels below root\n"),
476*7c478bd9Sstevel@tonic-gate 				    maxlev);
477*7c478bd9Sstevel@tonic-gate 				printf(gettext(
478*7c478bd9Sstevel@tonic-gate 					"\t%d distinct bytes in input\n"),
479*7c478bd9Sstevel@tonic-gate 					diffbytes);
480*7c478bd9Sstevel@tonic-gate 				printf(gettext(
481*7c478bd9Sstevel@tonic-gate 					"\tdictionary overhead = %ld bytes\n"),
482*7c478bd9Sstevel@tonic-gate 					dictsize);
483*7c478bd9Sstevel@tonic-gate 				printf(gettext(
484*7c478bd9Sstevel@tonic-gate 				    "\teffective  entropy  = %.2f bits/byte\n"),
485*7c478bd9Sstevel@tonic-gate 			    ((double)outsize / (double)insize.lint.lng) * 8);
486*7c478bd9Sstevel@tonic-gate 				printf(gettext(
487*7c478bd9Sstevel@tonic-gate 				    "\tasymptotic entropy  = %.2f bits/byte\n"),
488*7c478bd9Sstevel@tonic-gate 					((double)(outsize-dictsize) /
489*7c478bd9Sstevel@tonic-gate 					(double)insize.lint.lng) * 8);
490*7c478bd9Sstevel@tonic-gate 			}
491*7c478bd9Sstevel@tonic-gate 			u_times.actime = status.st_atime;
492*7c478bd9Sstevel@tonic-gate 			u_times.modtime = status.st_mtime;
493*7c478bd9Sstevel@tonic-gate 			if (utime(filename, &u_times) != 0) {
494*7c478bd9Sstevel@tonic-gate 				errflg++;
495*7c478bd9Sstevel@tonic-gate 				fprintf(stderr,
496*7c478bd9Sstevel@tonic-gate 					gettext(
497*7c478bd9Sstevel@tonic-gate 					"pack: cannot change times on %s: "),
498*7c478bd9Sstevel@tonic-gate 					filename);
499*7c478bd9Sstevel@tonic-gate 				perror("");
500*7c478bd9Sstevel@tonic-gate 			}
501*7c478bd9Sstevel@tonic-gate 			if (chmod(filename, status.st_mode) != 0) {
502*7c478bd9Sstevel@tonic-gate 				errflg++;
503*7c478bd9Sstevel@tonic-gate 				fprintf(stderr,
504*7c478bd9Sstevel@tonic-gate 					gettext(
505*7c478bd9Sstevel@tonic-gate 				"pack: can't change mode to %o on %s: "),
506*7c478bd9Sstevel@tonic-gate 					status.st_mode, filename);
507*7c478bd9Sstevel@tonic-gate 				perror("");
508*7c478bd9Sstevel@tonic-gate 			}
509*7c478bd9Sstevel@tonic-gate 			chown(filename, status.st_uid, status.st_gid);
510*7c478bd9Sstevel@tonic-gate 			if (!errflg)
511*7c478bd9Sstevel@tonic-gate 				fcount--;  /* success after all */
512*7c478bd9Sstevel@tonic-gate 		} else {
513*7c478bd9Sstevel@tonic-gate 			if (pathconf(filename, _PC_XATTR_EXISTS) == 1) {
514*7c478bd9Sstevel@tonic-gate 				(void) mv_xattrs(outfile, infile, filename, 1);
515*7c478bd9Sstevel@tonic-gate 			}
516*7c478bd9Sstevel@tonic-gate 			unlink(filename);
517*7c478bd9Sstevel@tonic-gate 		}
518*7c478bd9Sstevel@tonic-gate closein:	close(outfile);
519*7c478bd9Sstevel@tonic-gate 		close(infile);
520*7c478bd9Sstevel@tonic-gate 	}
521*7c478bd9Sstevel@tonic-gate 	return (fcount);
522*7c478bd9Sstevel@tonic-gate }
523*7c478bd9Sstevel@tonic-gate 
524*7c478bd9Sstevel@tonic-gate /*
525*7c478bd9Sstevel@tonic-gate  * mv_xattrs - move (via renameat) all of the extended attributes
526*7c478bd9Sstevel@tonic-gate  *	associated with the file referenced by infd to the file
527*7c478bd9Sstevel@tonic-gate  *	referenced by outfd.  The infile and silent arguments are
528*7c478bd9Sstevel@tonic-gate  *	provided for error message processing.  This function
529*7c478bd9Sstevel@tonic-gate  *	returns 0 on success and -1 on error.
530*7c478bd9Sstevel@tonic-gate  */
531*7c478bd9Sstevel@tonic-gate static int
532*7c478bd9Sstevel@tonic-gate mv_xattrs(int infd, int outfd, char *infile, int silent)
533*7c478bd9Sstevel@tonic-gate {
534*7c478bd9Sstevel@tonic-gate 	int indfd, outdfd, tmpfd;
535*7c478bd9Sstevel@tonic-gate 	DIR *dirp = NULL;
536*7c478bd9Sstevel@tonic-gate 	struct dirent *dp = NULL;
537*7c478bd9Sstevel@tonic-gate 	int error = 0;
538*7c478bd9Sstevel@tonic-gate 	char *etext;
539*7c478bd9Sstevel@tonic-gate 
540*7c478bd9Sstevel@tonic-gate 	indfd = outdfd = tmpfd = -1;
541*7c478bd9Sstevel@tonic-gate 
542*7c478bd9Sstevel@tonic-gate 	if ((indfd = openat(infd, ".", O_RDONLY|O_XATTR)) == -1) {
543*7c478bd9Sstevel@tonic-gate 		etext = gettext("cannot open source");
544*7c478bd9Sstevel@tonic-gate 		error = -1;
545*7c478bd9Sstevel@tonic-gate 		goto out;
546*7c478bd9Sstevel@tonic-gate 	}
547*7c478bd9Sstevel@tonic-gate 
548*7c478bd9Sstevel@tonic-gate 	if ((outdfd = openat(outfd, ".", O_RDONLY|O_XATTR)) == -1) {
549*7c478bd9Sstevel@tonic-gate 		etext = gettext("cannot open target");
550*7c478bd9Sstevel@tonic-gate 		error = -1;
551*7c478bd9Sstevel@tonic-gate 		goto out;
552*7c478bd9Sstevel@tonic-gate 	}
553*7c478bd9Sstevel@tonic-gate 
554*7c478bd9Sstevel@tonic-gate 	if ((tmpfd = dup(indfd)) == -1) {
555*7c478bd9Sstevel@tonic-gate 		etext = gettext("cannot dup descriptor");
556*7c478bd9Sstevel@tonic-gate 		error = -1;
557*7c478bd9Sstevel@tonic-gate 		goto out;
558*7c478bd9Sstevel@tonic-gate 
559*7c478bd9Sstevel@tonic-gate 	}
560*7c478bd9Sstevel@tonic-gate 	if ((dirp = fdopendir(tmpfd)) == NULL) {
561*7c478bd9Sstevel@tonic-gate 		etext = gettext("cannot access source");
562*7c478bd9Sstevel@tonic-gate 		error = -1;
563*7c478bd9Sstevel@tonic-gate 		goto out;
564*7c478bd9Sstevel@tonic-gate 	}
565*7c478bd9Sstevel@tonic-gate 
566*7c478bd9Sstevel@tonic-gate 	while (dp = readdir(dirp)) {
567*7c478bd9Sstevel@tonic-gate 		if ((dp->d_name[0] == '.' && dp->d_name[1] == '\0') ||
568*7c478bd9Sstevel@tonic-gate 		    (dp->d_name[0] == '.' && dp->d_name[1] == '.' &&
569*7c478bd9Sstevel@tonic-gate 		    dp->d_name[2] == '\0'))
570*7c478bd9Sstevel@tonic-gate 			continue;
571*7c478bd9Sstevel@tonic-gate 		if ((renameat(indfd, dp->d_name, outdfd, dp->d_name)) == -1) {
572*7c478bd9Sstevel@tonic-gate 			etext = dp->d_name;
573*7c478bd9Sstevel@tonic-gate 			error = -1;
574*7c478bd9Sstevel@tonic-gate 			goto out;
575*7c478bd9Sstevel@tonic-gate 		}
576*7c478bd9Sstevel@tonic-gate 	}
577*7c478bd9Sstevel@tonic-gate out:
578*7c478bd9Sstevel@tonic-gate 	if (error == -1 && silent == 0) {
579*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(
580*7c478bd9Sstevel@tonic-gate 			"pack: %s: cannot move extended attributes, "),
581*7c478bd9Sstevel@tonic-gate 			infile);
582*7c478bd9Sstevel@tonic-gate 		perror(etext);
583*7c478bd9Sstevel@tonic-gate 	}
584*7c478bd9Sstevel@tonic-gate 	if (dirp)
585*7c478bd9Sstevel@tonic-gate 		closedir(dirp);
586*7c478bd9Sstevel@tonic-gate 	if (indfd != -1)
587*7c478bd9Sstevel@tonic-gate 		close(indfd);
588*7c478bd9Sstevel@tonic-gate 	if (outdfd != -1)
589*7c478bd9Sstevel@tonic-gate 		close(outdfd);
590*7c478bd9Sstevel@tonic-gate 	return (error);
591*7c478bd9Sstevel@tonic-gate }
592