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