xref: /titanic_50/usr/src/cmd/compress/compress.c (revision 63602c90af2802f2ab666ab39bb4aea7d02500c3)
1 /*
2  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
7 /*	  All Rights Reserved  	*/
8 
9 
10 /*
11  * Copyright (c) 1986 Regents of the University of California.
12  * All rights reserved.  The Berkeley software License Agreement
13  * specifies the terms and conditions for redistribution.
14  */
15 
16 #pragma ident	"%Z%%M%	%I%	%E% SMI"
17 
18 /*
19  * Compress - data compression program
20  */
21 #define	min(a, b)	((a > b) ? b : a)
22 
23 /*
24  * machine variants which require cc -Dmachine:  pdp11, z8000, pcxt
25  */
26 
27 /*
28  * Set USERMEM to the maximum amount of physical user memory available
29  * in bytes.  USERMEM is used to determine the maximum BITS that can be used
30  * for compression.
31  *
32  * SACREDMEM is the amount of physical memory saved for others; compress
33  * will hog the rest.
34  */
35 #ifndef SACREDMEM
36 #define	SACREDMEM	0
37 #endif
38 
39 #ifndef USERMEM
40 #define	USERMEM 	450000	/* default user memory */
41 #endif
42 
43 #ifdef USERMEM
44 #if USERMEM >= (433484+SACREDMEM)
45 #define	PBITS	16
46 #else
47 #if USERMEM >= (229600+SACREDMEM)
48 #define	PBITS	15
49 #else
50 #if USERMEM >= (127536+SACREDMEM)
51 #define	PBITS	14
52 #else
53 #if USERMEM >= (73464+SACREDMEM)
54 #define	PBITS	13
55 #else
56 #define	PBITS	12
57 #endif
58 #endif
59 #endif
60 #endif
61 #undef USERMEM
62 #endif /* USERMEM */
63 
64 #ifdef PBITS		/* Preferred BITS for this memory size */
65 #ifndef BITS
66 #define	BITS PBITS
67 #endif /* BITS */
68 #endif /* PBITS */
69 
70 #if BITS == 16
71 #define	HSIZE	69001		/* 95% occupancy */
72 #endif
73 #if BITS == 15
74 #define	HSIZE	35023		/* 94% occupancy */
75 #endif
76 #if BITS == 14
77 #define	HSIZE	18013		/* 91% occupancy */
78 #endif
79 #if BITS == 13
80 #define	HSIZE	9001		/* 91% occupancy */
81 #endif
82 #if BITS <= 12
83 #define	HSIZE	5003		/* 80% occupancy */
84 #endif
85 
86 #define	OUTSTACKSIZE	(2<<BITS)
87 
88 /*
89  * a code_int must be able to hold 2**BITS values of type int, and also -1
90  */
91 #if BITS > 15
92 typedef long int	code_int;
93 #else
94 typedef int		code_int;
95 #endif
96 
97 typedef long int	count_int;
98 typedef long long	count_long;
99 
100 typedef	unsigned char	char_type;
101 
102 static char_type magic_header[] = { "\037\235" }; /* 1F 9D */
103 
104 /* Defines for third byte of header */
105 #define	BIT_MASK	0x1f
106 #define	BLOCK_MASK	0x80
107 /*
108  * Masks 0x40 and 0x20 are free.  I think 0x20 should mean that there is
109  * a fourth header byte(for expansion).
110  */
111 #define	INIT_BITS 9			/* initial number of bits/code */
112 
113 /*
114  * compress.c - File compression ala IEEE Computer, June 1984.
115  */
116 static char rcs_ident[] =
117 	"$Header: compress.c,v 4.0 85/07/30 12:50:00 joe Release $";
118 
119 #include <stdio.h>
120 #include <ctype.h>
121 #include <signal.h>
122 #include <sys/types.h>
123 #include <sys/stat.h>
124 #include <unistd.h>
125 #include <sys/param.h>
126 #include <stdlib.h>			/* XCU4 */
127 #include <limits.h>
128 #include <libintl.h>
129 #include <locale.h>
130 #include <langinfo.h>
131 #include <string.h>
132 #include <sys/acl.h>
133 #include <utime.h>
134 #include <libgen.h>
135 #include <setjmp.h>
136 #include <strings.h>
137 #include <fcntl.h>
138 #include <dirent.h>
139 #include <aclutils.h>
140 #include <errno.h>
141 #include "getresponse.h"
142 
143 static int n_bits;			/* number of bits/code */
144 static int maxbits = BITS;	/* user settable max # bits/code */
145 static code_int maxcode;	/* maximum code, given n_bits */
146 			/* should NEVER generate this code */
147 static code_int maxmaxcode = 1 << BITS;
148 #define	MAXCODE(n_bits)	((1 << (n_bits)) - 1)
149 
150 static count_int htab [OUTSTACKSIZE];
151 static unsigned short codetab [OUTSTACKSIZE];
152 
153 #define	htabof(i)	htab[i]
154 #define	codetabof(i)	codetab[i]
155 static code_int hsize = HSIZE; /* for dynamic table sizing */
156 static off_t	fsize;	/* file size of input file */
157 
158 /*
159  * To save much memory, we overlay the table used by compress() with those
160  * used by decompress().  The tab_prefix table is the same size and type
161  * as the codetab.  The tab_suffix table needs 2**BITS characters.  We
162  * get this from the beginning of htab.  The output stack uses the rest
163  * of htab, and contains characters.  There is plenty of room for any
164  * possible stack (stack used to be 8000 characters).
165  */
166 
167 #define	tab_prefixof(i)		codetabof(i)
168 #define	tab_suffixof(i)		((char_type *)(htab))[i]
169 #define	de_stack		((char_type *)&tab_suffixof(1<<BITS))
170 #define	stack_max		((char_type *)&tab_suffixof(OUTSTACKSIZE))
171 
172 static code_int free_ent = 0; /* first unused entry */
173 static int newline_needed = 0;
174 static int didnt_shrink = 0;
175 static int perm_stat = 0;	/* permanent status */
176 
177 static code_int getcode();
178 
179 	/* Use a 3-byte magic number header, unless old file */
180 static int nomagic = 0;
181 	/* Write output on stdout, suppress messages */
182 static int zcat_flg = 0;	/* use stdout on all files */
183 static int zcat_cmd = 0;	/* zcat cmd */
184 static int use_stdout = 0;	/* set for each file processed */
185 	/* Don't unlink output file on interrupt */
186 static int precious = 1;
187 static int quiet = 1;	/* don't tell me about compression */
188 
189 /*
190  * block compression parameters -- after all codes are used up,
191  * and compression rate changes, start over.
192  */
193 static int block_compress = BLOCK_MASK;
194 static int clear_flg = 0;
195 static long int ratio = 0;
196 #define	CHECK_GAP 10000	/* ratio check interval */
197 static count_long checkpoint = CHECK_GAP;
198 /*
199  * the next two codes should not be changed lightly, as they must not
200  * lie within the contiguous general code space.
201  */
202 #define	FIRST	257	/* first free entry */
203 #define	CLEAR	256	/* table clear output code */
204 
205 static int force = 0;
206 static char ofname [MAXPATHLEN];
207 
208 static int Vflg = 0;
209 static int vflg = 0;
210 static int qflg = 0;
211 static int bflg = 0;
212 static int Fflg = 0;
213 static int dflg = 0;
214 static int cflg = 0;
215 static int Cflg = 0;
216 
217 #ifdef DEBUG
218 int verbose = 0;
219 int debug = 0;
220 #endif /* DEBUG */
221 
222 static void (*oldint)();
223 static int bgnd_flag;
224 
225 static int do_decomp = 0;
226 
227 static char *progname;
228 static char *optstr;
229 /*
230  * Fix lint errors
231  */
232 
233 static char *local_basename(char *);
234 
235 static int  addDotZ(char *, size_t);
236 
237 static void Usage(void);
238 static void cl_block(count_long);
239 static void cl_hash(count_int);
240 static void compress(void);
241 static void copystat(char *, struct stat *, char *);
242 static void decompress(void);
243 static void ioerror(void);
244 static void onintr();
245 static void oops();
246 static void output(code_int);
247 static void prratio(FILE *, count_long, count_long);
248 static void version(void);
249 static int mv_xattrs(char *, char *, int);
250 
251 #ifdef DEBUG
252 static int in_stack(int, int);
253 static void dump_tab(void);
254 static void printcodes(void);
255 #endif
256 
257 /* For error-handling */
258 
259 static jmp_buf env;
260 
261 /* For input and ouput */
262 
263 static FILE *inp;		/* the current input file */
264 static FILE *infile;		/* disk-based input stream */
265 static FILE *outp;		/* current output file */
266 static FILE *outfile;		/* disk-based output stream */
267 
268 /* For output() */
269 
270 static char buf[BITS];
271 
272 static char_type lmask[9] =
273 	{0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
274 static char_type rmask[9] =
275 	{0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
276 
277 /* For compress () */
278 
279 static int offset;
280 static count_long bytes_out;	/* length of compressed output */
281 	/* # of codes output (for debugging) */
282 
283 /* For dump_tab() */
284 
285 #define	STACK_SIZE	15000
286 #ifdef DEBUG
287 code_int sorttab[1<<BITS];	/* sorted pointers into htab */
288 #endif
289 
290 /*
291  * *************************************************************
292  * TAG( main )
293  *
294  * Algorithm from "A Technique for High Performance Data Compression",
295  * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.
296  *
297  * Usage: compress [-dfvc] [-b bits] [file ...]
298  * Inputs:
299  *	-d:	    If given, decompression is done instead.
300  *
301  *	-c:	    Write output on stdout, don't remove original.
302  *
303  *	-b:	    Parameter limits the max number of bits/code.
304  *
305  *	-f:	    Forces output file to be generated, even if one already
306  *		    exists, and even if no space is saved by compressing.
307  *		    If -f is not used, the user will be prompted if stdin is
308  *		    a tty, otherwise, the output file will not be overwritten.
309  *
310  *  -v:	    Write compression statistics
311  *
312  * 	file ...:   Files to be compressed.  If none specified, stdin
313  *		    is used.
314  * Outputs:
315  *	file.Z:	    Compressed form of file with same mode, owner, and utimes
316  * 	or stdout   (if stdin used as input)
317  *
318  * Assumptions:
319  * When filenames are given, replaces with the compressed version
320  * (.Z suffix) only if the file decreases in size.
321  * Algorithm:
322  * Modified Lempel-Ziv method (LZW).  Basically finds common
323  * substrings and replaces them with a variable size code.  This is
324  * deterministic, and can be done on the fly.  Thus, the decompression
325  * procedure needs no input table, but tracks the way the table was built.
326  */
327 
328 int
329 main(int argc, char *argv[])
330 {
331 	int overwrite = 0;	/* Do not overwrite unless given -f flag */
332 	char tempname[MAXPATHLEN];
333 	char line[LINE_MAX];
334 	char **filelist, **fileptr;
335 	char *cp;
336 	struct stat statbuf;
337 	struct stat ostatbuf;
338 	int ch;				/* XCU4 */
339 	char	*p;
340 	extern int optind, optopt;
341 	extern char *optarg;
342 	int dash_count = 0;		/* times "-" is on cmdline */
343 
344 	/* XCU4 changes */
345 	(void) setlocale(LC_ALL, "");
346 #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
347 #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
348 #endif
349 	(void) textdomain(TEXT_DOMAIN);
350 
351 	if (init_yes() < 0) {
352 		(void) fprintf(stderr, gettext(ERR_MSG_INIT_YES),
353 		    strerror(errno));
354 		exit(1);
355 	}
356 
357 	/* This bg check only works for sh. */
358 	if ((oldint = signal(SIGINT, SIG_IGN)) != SIG_IGN) {
359 		(void) signal(SIGINT, onintr);
360 		(void) signal(SIGSEGV, oops);
361 	}
362 	bgnd_flag = oldint != SIG_DFL;
363 
364 	/* Allocate room for argv + "-" (if stdin needs to be added) */
365 
366 	filelist = fileptr = (char **)(malloc((argc + 1) * sizeof (*argv)));
367 	*filelist = NULL;
368 
369 	if ((cp = rindex(argv[0], '/')) != 0) {
370 		cp++;
371 	} else {
372 		cp = argv[0];
373 	}
374 
375 	if (strcmp(cp, "uncompress") == 0) {
376 		do_decomp = 1;
377 	} else if (strcmp(cp, "zcat") == 0) {
378 		do_decomp = 1;
379 		zcat_cmd = zcat_flg = 1;
380 	}
381 
382 	progname = local_basename(argv[0]);
383 
384 	/*
385 	 * Argument Processing
386 	 * All flags are optional.
387 	 * -D = > debug
388 	 * -V = > print Version; debug verbose
389 	 * -d = > do_decomp
390 	 * -v = > unquiet
391 	 * -f = > force overwrite of output file
392 	 * -n = > no header: useful to uncompress old files
393 	 * -b	  maxbits => maxbits.  If -b is specified,
394 	 *	  then maxbits MUST be given also.
395 	 * -c = > cat all output to stdout
396 	 * -C = > generate output compatible with compress 2.0.
397 	 * if a string is left, must be an input filename.
398 	 */
399 #ifdef DEBUG
400 	optstr = "b:cCdDfFnqvV";
401 #else
402 	optstr = "b:cCdfFnqvV";
403 #endif
404 
405 	while ((ch = getopt(argc, argv, optstr)) != EOF) {
406 		/* Process all flags in this arg */
407 		switch (ch) {
408 #ifdef DEBUG
409 			case 'D':
410 				debug = 1;
411 				break;
412 			case 'V':
413 				verbose = 1;
414 				version();
415 				break;
416 #else
417 			case 'V':
418 				version();
419 				Vflg++;
420 				break;
421 #endif /* DEBUG */
422 			case 'v':
423 				quiet = 0;
424 				vflg++;
425 				break;
426 			case 'd':
427 				do_decomp = 1;
428 				dflg++;
429 				break;
430 			case 'f':
431 			case 'F':
432 				Fflg++;
433 				overwrite = 1;
434 				force = 1;
435 				break;
436 			case 'n':
437 				nomagic = 1;
438 				break;
439 			case 'C':
440 				Cflg++;
441 				block_compress = 0;
442 				break;
443 			case 'b':
444 				bflg++;
445 				p = optarg;
446 				if (!p) {
447 					(void) fprintf(stderr, gettext(
448 					    "Missing maxbits\n"));
449 					Usage();
450 					exit(1);
451 				}
452 				maxbits = strtoul(optarg, &p, 10);
453 				if (*p) {
454 					(void) fprintf(stderr, gettext(
455 					    "Missing maxbits\n"));
456 					Usage();
457 					exit(1);
458 				}
459 				break;
460 
461 			case 'c':
462 				cflg++;
463 				zcat_flg = 1;
464 				break;
465 			case 'q':
466 				qflg++;
467 				quiet = 1;
468 				break;
469 			default:
470 				(void) fprintf(stderr, gettext(
471 				    "Unknown flag: '%c'\n"), optopt);
472 				Usage();
473 				exit(1);
474 		}
475 	} /* while */
476 
477 	/*
478 	 * Validate zcat syntax
479 	 */
480 
481 	if (zcat_cmd && (Fflg | Cflg | cflg |
482 	    bflg | qflg | dflg | nomagic)) {
483 		(void) fprintf(stderr, gettext(
484 		    "Invalid Option\n"));
485 		Usage();
486 		exit(1);
487 	}
488 
489 	/*
490 	 * Process the file list
491 	 */
492 
493 	for (; optind < argc; optind++) {
494 		if (strcmp(argv[optind], "-") == 0) {
495 			dash_count++;
496 		}
497 
498 		*fileptr++ = argv[optind];	/* Build input file list */
499 		*fileptr = NULL;
500 	}
501 
502 	if (dash_count > 1) {
503 		(void) fprintf(stderr,
504 		    gettext("%s may only appear once in the file"
505 		    " list\n"), "\"-\"");
506 		exit(1);
507 	}
508 
509 	if (fileptr - filelist == 0) {
510 		*fileptr++ = "-";
511 		*fileptr = NULL;
512 	}
513 
514 	if (fileptr - filelist > 1 && cflg && !do_decomp) {
515 		(void) fprintf(stderr,
516 		    gettext("compress: only one file may be compressed"
517 		    " to stdout\n"));
518 		exit(1);
519 	}
520 
521 	if (maxbits < INIT_BITS)
522 		maxbits = INIT_BITS;
523 	if (maxbits > BITS)
524 		maxbits = BITS;
525 	maxmaxcode = 1 << maxbits;
526 
527 	/* Need to open something to close with freopen later */
528 
529 	if ((infile = fopen("/dev/null", "r")) == NULL) {
530 		(void) fprintf(stderr, gettext("Error opening /dev/null for "
531 		    "input\n"));
532 		exit(1);
533 	}
534 
535 	if ((outfile = fopen("/dev/null", "w")) == NULL) {
536 		(void) fprintf(stderr, gettext("Error opening /dev/null for "
537 		    "output\n"));
538 		exit(1);
539 	}
540 
541 	for (fileptr = filelist; *fileptr; fileptr++) {
542 		int jmpval = 0;
543 		didnt_shrink = 0;
544 		newline_needed = 0;
545 
546 		if (do_decomp) {
547 			/* DECOMPRESSION */
548 
549 			if (strcmp(*fileptr, "-") == 0) {
550 				/* process stdin */
551 				inp = stdin;
552 				outp = stdout;
553 				use_stdout = 1;
554 				*fileptr = "stdin"; /* for error messages */
555 			} else {
556 				/* process the named file */
557 
558 				inp = infile;
559 				outp = outfile;
560 				use_stdout = 0;
561 
562 				if (zcat_flg) {
563 					use_stdout = 1;
564 					outp = stdout;
565 				}
566 
567 				/* Check for .Z suffix */
568 
569 				if (strcmp(*fileptr +
570 				    strlen(*fileptr) - 2, ".Z") != 0) {
571 					/* No .Z: tack one on */
572 
573 					if (strlcpy(tempname, *fileptr,
574 					    sizeof (tempname)) >=
575 					    sizeof (tempname)) {
576 						(void) fprintf(stderr,
577 						    gettext("%s: filename "
578 						    "too long\n"),
579 						    *fileptr);
580 						perm_stat = 1;
581 						continue;
582 					}
583 
584 					if (addDotZ(tempname,
585 					    sizeof (tempname)) < 0) {
586 						perm_stat = 1;
587 						continue;
588 					}
589 
590 					*fileptr = tempname;
591 				}
592 
593 				/* Open input file */
594 
595 				if (stat(*fileptr, &statbuf) < 0) {
596 					perror(*fileptr);
597 					perm_stat = 1;
598 					continue;
599 				}
600 
601 				if ((freopen(*fileptr, "r", inp)) == NULL) {
602 					perror(*fileptr);
603 					perm_stat = 1;
604 					continue;
605 				}
606 			}
607 
608 			/* Check the magic number */
609 
610 			if (nomagic == 0) {
611 				if ((getc(inp) !=
612 				    (magic_header[0] & 0xFF)) ||
613 				    (getc(inp) !=
614 				    (magic_header[1] & 0xFF))) {
615 					(void) fprintf(stderr, gettext(
616 					    "%s: not in compressed "
617 					    "format\n"),
618 					    *fileptr);
619 					perm_stat = 1;
620 					continue;
621 				}
622 
623 				/* set -b from file */
624 				if ((maxbits = getc(inp)) == EOF &&
625 				    ferror(inp)) {
626 					perror(*fileptr);
627 					perm_stat = 1;
628 					continue;
629 				}
630 
631 				block_compress = maxbits & BLOCK_MASK;
632 				maxbits &= BIT_MASK;
633 				maxmaxcode = 1 << maxbits;
634 
635 				if (maxbits > BITS) {
636 					(void) fprintf(stderr,
637 					    gettext("%s: compressed "
638 					    "with %d bits, "
639 					    "can only handle"
640 					    " %d bits\n"),
641 					    *fileptr, maxbits, BITS);
642 					perm_stat = 1;
643 					continue;
644 				}
645 			}
646 
647 			if (!use_stdout) {
648 				/* Generate output filename */
649 
650 				if (strlcpy(ofname, *fileptr,
651 				    sizeof (ofname)) >=
652 				    sizeof (ofname)) {
653 					(void) fprintf(stderr,
654 					    gettext("%s: filename "
655 					    "too long\n"),
656 					    *fileptr);
657 					perm_stat = 1;
658 					continue;
659 				}
660 
661 				/* Strip off .Z */
662 
663 				ofname[strlen(*fileptr) - 2] = '\0';
664 			}
665 		} else {
666 			/* COMPRESSION */
667 
668 			if (strcmp(*fileptr, "-") == 0) {
669 				/* process stdin */
670 				inp = stdin;
671 				outp = stdout;
672 				use_stdout = 1;
673 				*fileptr = "stdin"; /* for error messages */
674 
675 				/* Use the largest possible hash table */
676 				hsize =  HSIZE;
677 			} else {
678 				/* process the named file */
679 
680 				inp = infile;
681 				outp = outfile;
682 				use_stdout = 0;
683 
684 				if (zcat_flg) {
685 					use_stdout = 1;
686 					outp = stdout;
687 				}
688 
689 				if (strcmp(*fileptr +
690 				    strlen(*fileptr) - 2, ".Z") == 0) {
691 					(void) fprintf(stderr, gettext(
692 					    "%s: already has .Z "
693 					    "suffix -- no change\n"),
694 					    *fileptr);
695 					perm_stat = 1;
696 					continue;
697 				}
698 				/* Open input file */
699 
700 				if (stat(*fileptr, &statbuf) < 0) {
701 					perror(*fileptr);
702 					perm_stat = 1;
703 					continue;
704 				}
705 
706 				if ((freopen(*fileptr, "r", inp)) == NULL) {
707 					perror(*fileptr);
708 					perm_stat = 1;
709 					continue;
710 				}
711 
712 				fsize = (off_t)statbuf.st_size;
713 
714 				/*
715 				 * tune hash table size for small
716 				 * files -- ad hoc,
717 				 * but the sizes match earlier #defines, which
718 				 * serve as upper bounds on the number of
719 				 * output codes.
720 				 */
721 				hsize = HSIZE;
722 				if (fsize < (1 << 12))
723 					hsize = min(5003, HSIZE);
724 				else if (fsize < (1 << 13))
725 					hsize = min(9001, HSIZE);
726 				else if (fsize < (1 << 14))
727 					hsize = min(18013, HSIZE);
728 				else if (fsize < (1 << 15))
729 					hsize = min(35023, HSIZE);
730 				else if (fsize < 47000)
731 					hsize = min(50021, HSIZE);
732 
733 				if (!use_stdout) {
734 					/* Generate output filename */
735 
736 					if (strlcpy(ofname, *fileptr,
737 					    sizeof (ofname)) >=
738 					    sizeof (ofname)) {
739 						(void) fprintf(stderr,
740 						    gettext("%s: filename "
741 						    "too long\n"),
742 						    *fileptr);
743 						perm_stat = 1;
744 						continue;
745 					}
746 
747 					if (addDotZ(ofname,
748 					    sizeof (ofname)) < 0) {
749 						perm_stat = 1;
750 						continue;
751 					}
752 				}
753 			}
754 		}	/* if (do_decomp) */
755 
756 		/* Check for overwrite of existing file */
757 
758 		if (!overwrite && !use_stdout) {
759 			if (stat(ofname, &ostatbuf) == 0) {
760 				(void) fprintf(stderr, gettext(
761 				    "%s already exists;"), ofname);
762 				if (bgnd_flag == 0 && isatty(2)) {
763 					int cin;
764 
765 					(void) fprintf(stderr, gettext(
766 					    " do you wish to overwr"
767 					    "ite %s (%s or %s)? "),
768 					    ofname, yesstr, nostr);
769 					(void) fflush(stderr);
770 					for (cin = 0; cin < LINE_MAX; cin++)
771 						line[cin] = 0;
772 					(void) read(2, line, LINE_MAX);
773 
774 					if (yes_check(line) == 0) {
775 						(void) fprintf(stderr,
776 						    gettext(
777 						    "\tnot overwri"
778 						    "tten\n"));
779 						continue;
780 					}
781 				} else {
782 					/*
783 					 * XPG4: Assertion 1009
784 					 * Standard input is not
785 					 * terminal, and no '-f',
786 					 * and file exists.
787 					 */
788 
789 					(void) fprintf(stderr, gettext(
790 					    "%s: File exists, -f not"
791 					    " specified, and ru"
792 					    "nning in the backgro"
793 					    "und.\n"), *fileptr);
794 					perm_stat = 1;
795 					continue;
796 				}
797 			}
798 		}
799 		if (!use_stdout) {
800 			if (pathconf(ofname, _PC_XATTR_EXISTS) == 1) {
801 				(void) unlink(ofname);
802 			}
803 			/* Open output file */
804 			if (freopen(ofname, "w", outp) == NULL) {
805 				perror(ofname);
806 				perm_stat = 1;
807 				continue;
808 			}
809 			precious = 0;
810 			if (!quiet) {
811 				(void) fprintf(stderr, "%s: ",
812 				    *fileptr);
813 				newline_needed = 1;
814 			}
815 		} else if (!quiet && !do_decomp) {
816 			(void) fprintf(stderr, "%s: ",
817 			    *fileptr);
818 				newline_needed = 1;
819 		}
820 
821 		/* Actually do the compression/decompression */
822 
823 		if ((jmpval = setjmp(env)) == 0) {
824 			/* We'll see how things go */
825 #ifndef DEBUG
826 			if (do_decomp == 0)  {
827 				compress();
828 			} else {
829 				decompress();
830 			}
831 #else
832 			if (do_decomp == 0)  {
833 				compress();
834 			} else if (debug == 0)  {
835 				decompress();
836 			} else {
837 				printcodes();
838 			}
839 
840 			if (verbose) {
841 				dump_tab();
842 			}
843 #endif
844 		} else {
845 			/*
846 			 * Things went badly - clean up and go on.
847 			 * jmpval's values break down as follows:
848 			 *   1 == message determined by ferror() values.
849 			 *   2 == input problem message needed.
850 			 *   3 == output problem message needed.
851 			 */
852 
853 			if (ferror(inp) || jmpval == 2) {
854 				if (do_decomp) {
855 					(void) fprintf(stderr, gettext(
856 					    "uncompress: %s: corrupt"
857 					    " input\n"), *fileptr);
858 				} else {
859 					perror(*fileptr);
860 				}
861 			}
862 
863 			if (ferror(outp) || jmpval == 3) {
864 				/* handle output errors */
865 
866 				if (use_stdout) {
867 					perror("");
868 				} else {
869 					perror(ofname);
870 				}
871 			}
872 
873 			if (ofname[0] != '\0') {
874 				if (unlink(ofname) < 0)  {
875 					perror(ofname);
876 				}
877 
878 				ofname[0] = '\0';
879 			}
880 
881 			perm_stat = 1;
882 			continue;
883 		}
884 
885 		/* Things went well */
886 
887 		if (!use_stdout) {
888 				/* Copy stats */
889 			copystat(*fileptr, &statbuf, ofname);
890 			precious = 1;
891 			if (newline_needed) {
892 				(void) putc('\n', stderr);
893 			}
894 			/*
895 			 * Print the info. for unchanged file
896 			 * when no -v
897 			 */
898 
899 			if (didnt_shrink) {
900 				if (!force && perm_stat == 0) {
901 					if (quiet) {
902 						(void) fprintf(stderr, gettext(
903 						    "%s: -- file "
904 						    "unchanged\n"),
905 						    *fileptr);
906 					}
907 
908 					perm_stat = 2;
909 				}
910 			}
911 		} else {
912 			if (didnt_shrink && !force && perm_stat == 0) {
913 				perm_stat = 2;
914 			}
915 
916 			if (newline_needed) {
917 				(void) fprintf(stderr, "\n");
918 			}
919 		}
920 	}	/* for */
921 
922 	return (perm_stat);
923 }
924 
925 static void
926 cinterr(int hshift)
927 {
928 	/* we have exceeded the hash table */
929 	(void) fprintf(stderr,
930 	    "internal error: hashtable exceeded - hsize = %ld\n", hsize);
931 	(void) fprintf(stderr, "hshift = %d, %d\n", hshift, (1 << hshift) -1);
932 	(void) fprintf(stderr, "maxbits = %d\n", maxbits);
933 	(void) fprintf(stderr, "n_bits = %d\n", n_bits);
934 	(void) fprintf(stderr, "maxcode = %ld\n", maxcode);
935 	longjmp(env, 1);
936 }
937 
938 static code_int
939 adjusti(code_int i, code_int hsize_reg)
940 {
941 	while (i < 0) {
942 		i += hsize_reg;
943 	}
944 
945 	while (i >= hsize_reg) {
946 		i -= hsize_reg;
947 	}
948 	return (i);
949 }
950 
951 /*
952  * compress inp to outp
953  *
954  * Algorithm:  use open addressing double hashing(no chaining) on the
955  * prefix code / next character combination.  We do a variant of Knuth's
956  * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
957  * secondary probe.  Here, the modular division first probe is gives way
958  * to a faster exclusive-or manipulation.  Also do block compression with
959  * an adaptive reset, whereby the code table is cleared when the compression
960  * ratio decreases, but after the table fills.  The variable-length output
961  * codes are re-sized at this point, and a special CLEAR code is generated
962  * for the decompressor.  Late addition:  construct the table according to
963  * file size for noticeable speed improvement on small files.  Please direct
964  * questions about this implementation to ames!jaw.
965  */
966 
967 static void
968 compress()
969 {
970 	long fcode;
971 	code_int i = 0;
972 	int c;
973 	code_int ent;
974 	int disp;
975 	code_int hsize_reg;
976 	int hshift;
977 	int probecnt;
978 	count_long in_count;
979 	uint32_t inchi, inclo;
980 	int maxbits_reg;
981 	FILE *fin = inp;
982 #ifdef DEBUG
983 	count_long out_count = 0;
984 #endif
985 
986 	if (nomagic == 0) {
987 		if ((putc(magic_header[0], outp) == EOF ||
988 		    putc(magic_header[1], outp) == EOF ||
989 		    putc((char)(maxbits | block_compress),
990 		    outp) == EOF) &&
991 		    ferror(outp)) {
992 			ioerror();
993 		}
994 	}
995 
996 	offset = 0;
997 	bytes_out = 3;		/* includes 3-byte header mojo */
998 	clear_flg = 0;
999 	ratio = 0;
1000 	in_count = 1;
1001 	inchi = 0;
1002 	inclo = 1;
1003 	checkpoint = CHECK_GAP;
1004 	maxcode = MAXCODE(n_bits = INIT_BITS);
1005 	free_ent = ((block_compress) ? FIRST : 256);
1006 
1007 	if ((ent = getc(fin)) == EOF && ferror(fin)) {
1008 		ioerror();
1009 	}
1010 
1011 	hshift = 0;
1012 
1013 	for (fcode = (long)hsize;  fcode < 65536L; fcode *= 2L)
1014 		hshift++;
1015 
1016 	hshift = 8 - hshift;		/* set hash code range bound */
1017 
1018 	hsize_reg = hsize;
1019 	maxbits_reg = maxbits;
1020 
1021 	cl_hash((count_int) hsize_reg);		/* clear hash table */
1022 
1023 	while ((c = getc(fin)) != EOF) {
1024 		if (++inclo == 0)
1025 			inchi++;
1026 		fcode = (long)(((long)c << maxbits_reg) + ent);
1027 		i = ((c << hshift) ^ ent);	/* xor hashing */
1028 
1029 		if ((unsigned int)i >= hsize_reg)
1030 			i = adjusti(i, hsize_reg);
1031 
1032 		if (htabof(i) == fcode) {
1033 			ent = codetabof(i);
1034 			continue;
1035 		} else if ((long)htabof(i) < 0) {
1036 			/* empty slot */
1037 			goto nomatch;
1038 		}
1039 
1040 		/* secondary hash (after G. Knott) */
1041 		disp = hsize_reg - i;
1042 
1043 		if (i == 0) {
1044 			disp = 1;
1045 		}
1046 
1047 		probecnt = 0;
1048 	probe:
1049 		if (++probecnt > hsize_reg)
1050 			cinterr(hshift);
1051 
1052 		if ((i -= disp) < 0) {
1053 			while (i < 0)
1054 				i += hsize_reg;
1055 		}
1056 
1057 		if (htabof(i) == fcode) {
1058 			ent = codetabof(i);
1059 			continue;
1060 		}
1061 
1062 		if ((long)htabof(i) > 0) {
1063 			goto probe;
1064 		}
1065 	nomatch:
1066 		output((code_int) ent);
1067 #ifdef DEBUG
1068 		out_count++;
1069 #endif
1070 		ent = c;
1071 		if (free_ent < maxmaxcode) {
1072 			codetabof(i) = free_ent++;
1073 			/* code -> hashtable */
1074 			htabof(i) = fcode;
1075 		} else {
1076 			in_count = ((long long)inchi<<32|inclo);
1077 			if ((count_long)in_count >=
1078 			    (count_long)checkpoint && block_compress) {
1079 				cl_block(in_count);
1080 			}
1081 		}
1082 	}
1083 
1084 	in_count = ((long long)inchi<<32|inclo);
1085 
1086 	if (ferror(fin) != 0) {
1087 		ioerror();
1088 	}
1089 
1090 	/*
1091 	 * Put out the final code.
1092 	 */
1093 	output((code_int)ent);
1094 #ifdef DEBUG
1095 	out_count++;
1096 #endif
1097 
1098 	output((code_int)-1);
1099 
1100 	/*
1101 	 * Print out stats on stderr
1102 	 */
1103 	if (!quiet) {
1104 #ifdef DEBUG
1105 		(void) fprintf(stderr,
1106 		    "%lld chars in, %lld codes (%lld bytes) out, "
1107 		    "compression factor: ",
1108 		    (count_long)in_count, (count_long)out_count,
1109 		    (count_long) bytes_out);
1110 		prratio(stderr, (count_long)in_count,
1111 		    (count_long)bytes_out);
1112 		(void) fprintf(stderr, "\n");
1113 		(void) fprintf(stderr, "\tCompression as in compact: ");
1114 		prratio(stderr,
1115 		    (count_long)in_count-(count_long)bytes_out,
1116 		    (count_long)in_count);
1117 		(void) fprintf(stderr, "\n");
1118 		(void) fprintf(stderr,
1119 		    "\tLargest code (of last block) was %d"
1120 		    " (%d bits)\n",
1121 		    free_ent - 1, n_bits);
1122 #else /* !DEBUG */
1123 		(void) fprintf(stderr, gettext("Compression: "));
1124 		prratio(stderr,
1125 		    (count_long)in_count-(count_long)bytes_out,
1126 		    (count_long)in_count);
1127 #endif /* DEBUG */
1128 	}
1129 	/* report if no savings */
1130 	if ((count_long)bytes_out > (count_long)in_count) {
1131 		didnt_shrink = 1;
1132 	}
1133 }
1134 
1135 /*
1136  * **************************************************************
1137  * TAG(output)
1138  *
1139  * Output the given code.
1140  * Inputs:
1141  * 	code:	A n_bits-bit integer.  If == -1, then EOF.  This assumes
1142  *		that n_bits = < (long)wordsize - 1.
1143  * Outputs:
1144  * 	Outputs code to the file.
1145  * Assumptions:
1146  *	Chars are 8 bits long.
1147  * Algorithm:
1148  * 	Maintain a BITS character long buffer(so that 8 codes will
1149  * fit in it exactly).  Use the VAX insv instruction to insert each
1150  * code in turn.  When the buffer fills up empty it and start over.
1151  */
1152 
1153 static void
1154 output(code_int code)
1155 {
1156 #ifdef DEBUG
1157 	static int col = 0;
1158 #endif /* DEBUG */
1159 
1160 	int r_off = offset, bits = n_bits;
1161 	char *bp = buf;
1162 
1163 #ifdef DEBUG
1164 	if (verbose)
1165 		(void) fprintf(stderr, "%5d%c", code,
1166 		    (col += 6) >= 74 ? (col = 0, '\n') : ' ');
1167 #endif /* DEBUG */
1168 	if (code >= 0) {
1169 		/*
1170 		 * byte/bit numbering on the VAX is simulated
1171 		 * by the following code
1172 		 */
1173 		/*
1174 		 * Get to the first byte.
1175 		 */
1176 		bp += (r_off >> 3);
1177 		r_off &= 7;
1178 		/*
1179 		 * Since code is always >= 8 bits, only need to mask the first
1180 		 * hunk on the left.
1181 		 */
1182 		*bp = (*bp & rmask[r_off]) | (code << r_off) & lmask[r_off];
1183 		bp++;
1184 		bits -= (8 - r_off);
1185 		code >>= 8 - r_off;
1186 		/*
1187 		 * Get any 8 bit parts in the middle (<=1 for up to 16
1188 		 * bits).
1189 		 */
1190 		if (bits >= 8) {
1191 			*bp++ = code;
1192 			code >>= 8;
1193 			bits -= 8;
1194 		}
1195 		/* Last bits. */
1196 		if (bits)
1197 			*bp = code;
1198 		offset += n_bits;
1199 		if (offset == (n_bits << 3)) {
1200 			bp = buf;
1201 			bits = n_bits;
1202 			bytes_out += bits;
1203 			do {
1204 				if (putc(*bp, outp) == EOF &&
1205 				    ferror(outp)) {
1206 					ioerror();
1207 				}
1208 				bp++;
1209 			} while (--bits);
1210 			offset = 0;
1211 		}
1212 
1213 		/*
1214 		 * If the next entry is going to be too big for the code size,
1215 		 * then increase it, if possible.
1216 		 */
1217 		if (free_ent > maxcode || (clear_flg > 0)) {
1218 			/*
1219 			 * Write the whole buffer, because the input
1220 			 * side won't discover the size increase until
1221 			 * after it has read it.
1222 			 */
1223 			if (offset > 0) {
1224 				if (fwrite(buf, 1, n_bits, outp) != n_bits) {
1225 					longjmp(env, 3);
1226 				}
1227 				bytes_out += n_bits;
1228 			}
1229 			offset = 0;
1230 
1231 			if (clear_flg) {
1232 				maxcode = MAXCODE(n_bits = INIT_BITS);
1233 				clear_flg = 0;
1234 			} else {
1235 				n_bits++;
1236 				if (n_bits == maxbits)
1237 					maxcode = maxmaxcode;
1238 				else
1239 					maxcode = MAXCODE(n_bits);
1240 			}
1241 #ifdef DEBUG
1242 			if (debug) {
1243 				(void) fprintf(stderr,
1244 				    "\nChange to %d bits\n", n_bits);
1245 				col = 0;
1246 			}
1247 #endif /* DEBUG */
1248 		}
1249 	} else {
1250 		/*
1251 		 * At EOF, write the rest of the buffer.
1252 		 */
1253 		if (offset > 0) {
1254 			if (fwrite(buf, 1, (offset + 7) / 8, outp) == 0 &&
1255 			    ferror(outp)) {
1256 				ioerror();
1257 			}
1258 			bytes_out += (offset + 7) / 8;
1259 		}
1260 		offset = 0;
1261 		(void) fflush(outp);
1262 #ifdef DEBUG
1263 		if (verbose)
1264 			(void) fprintf(stderr, "\n");
1265 #endif /* DEBUG */
1266 		if (ferror(outp))
1267 			ioerror();
1268 	}
1269 }
1270 
1271 /*
1272  * Decompress inp to outp.  This routine adapts to the codes in the
1273  * file building the "string" table on-the-fly; requiring no table to
1274  * be stored in the compressed file.  The tables used herein are shared
1275  * with those of the compress() routine.  See the definitions above.
1276  */
1277 
1278 static void
1279 decompress()
1280 {
1281 	char_type *stackp, *stack_lim;
1282 	int finchar;
1283 	code_int code, oldcode, incode;
1284 	FILE *fout = outp;
1285 
1286 	/*
1287 	 * As above, initialize the first 256 entries in the table.
1288 	 */
1289 	maxcode = MAXCODE(n_bits = INIT_BITS);
1290 	for (code = 255; code >= 0; code--) {
1291 		tab_prefixof(code) = 0;
1292 		tab_suffixof(code) = (char_type)code;
1293 	}
1294 	free_ent = ((block_compress) ? FIRST : 256);
1295 
1296 	finchar = oldcode = getcode();
1297 	if (oldcode == -1)	/* EOF already? */
1298 		return;			/* Get out of here */
1299 	/* first code must be 8 bits = char */
1300 	if (putc((char)finchar, outp) == EOF && ferror(outp)) {
1301 		/* Crash if can't write */
1302 		ioerror();
1303 	}
1304 	stackp = de_stack;
1305 	stack_lim = stack_max;
1306 
1307 	while ((code = getcode()) > -1) {
1308 
1309 		if ((code == CLEAR) && block_compress) {
1310 			for (code = 255; code >= 0; code--)
1311 			tab_prefixof(code) = 0;
1312 			clear_flg = 1;
1313 			free_ent = FIRST - 1;
1314 			if ((code = getcode()) == -1)	/* O, untimely death! */
1315 				break;
1316 		}
1317 		incode = code;
1318 		/*
1319 		 * Special case for KwKwK string.
1320 		 */
1321 		if (code >= free_ent) {
1322 			if (stackp < stack_lim) {
1323 				*stackp++ = (char_type) finchar;
1324 				code = oldcode;
1325 			} else {
1326 				/* badness */
1327 				longjmp(env, 2);
1328 			}
1329 		}
1330 
1331 		/*
1332 		 * Generate output characters in reverse order
1333 		 */
1334 		while (code >= 256) {
1335 			if (stackp < stack_lim) {
1336 				*stackp++ = tab_suffixof(code);
1337 				code = tab_prefixof(code);
1338 			} else {
1339 				/* badness */
1340 				longjmp(env, 2);
1341 			}
1342 		}
1343 		*stackp++ = finchar = tab_suffixof(code);
1344 
1345 		/*
1346 		 * And put them out in forward order
1347 		 */
1348 		do {
1349 			stackp--;
1350 			(void) putc(*stackp, fout);
1351 		} while (stackp > de_stack);
1352 
1353 		if (ferror(fout))
1354 			ioerror();
1355 
1356 		/*
1357 		 * Generate the new entry.
1358 		 */
1359 		if ((code = free_ent) < maxmaxcode) {
1360 			tab_prefixof(code) = (unsigned short) oldcode;
1361 			tab_suffixof(code) = (char_type) finchar;
1362 			free_ent = code+1;
1363 		}
1364 		/*
1365 		 * Remember previous code.
1366 		 */
1367 		oldcode = incode;
1368 	}
1369 	(void) fflush(outp);
1370 	if (ferror(outp))
1371 		ioerror();
1372 }
1373 
1374 /*
1375  * **************************************************************
1376  * TAG( getcode )
1377  *
1378  * Read one code from the standard input.  If EOF, return -1.
1379  * Inputs:
1380  * 	inp
1381  * Outputs:
1382  * 	code or -1 is returned.
1383  */
1384 
1385 code_int
1386 getcode() {
1387 	code_int code;
1388 	static int offset = 0, size = 0;
1389 	static char_type buf[BITS];
1390 	int r_off, bits;
1391 	char_type *bp = buf;
1392 
1393 	if (clear_flg > 0 || offset >= size || free_ent > maxcode) {
1394 		/*
1395 		 * If the next entry will be too big for the current code
1396 		 * size, then we must increase the size.  This implies reading
1397 		 * a new buffer full, too.
1398 		 */
1399 		if (free_ent > maxcode) {
1400 			n_bits++;
1401 			if (n_bits == maxbits)
1402 				/* won't get any bigger now */
1403 				maxcode = maxmaxcode;
1404 			else
1405 				maxcode = MAXCODE(n_bits);
1406 		}
1407 		if (clear_flg > 0) {
1408 			maxcode = MAXCODE(n_bits = INIT_BITS);
1409 			clear_flg = 0;
1410 		}
1411 		size = fread(buf, 1, n_bits, inp);
1412 
1413 		if (size <= 0) {
1414 			if (feof(inp)) {
1415 				/* end of file */
1416 				return (-1);
1417 			} else if (ferror(inp)) {
1418 				ioerror();
1419 			}
1420 		}
1421 
1422 		offset = 0;
1423 		/* Round size down to integral number of codes */
1424 		size = (size << 3) - (n_bits - 1);
1425 	}
1426 	r_off = offset;
1427 	bits = n_bits;
1428 	/*
1429 	 * Get to the first byte.
1430 	 */
1431 	bp += (r_off >> 3);
1432 	r_off &= 7;
1433 	/* Get first part (low order bits) */
1434 	code = (*bp++ >> r_off);
1435 	bits -= (8 - r_off);
1436 	r_off = 8 - r_off;		/* now, offset into code word */
1437 	/* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
1438 	if (bits >= 8) {
1439 		code |= *bp++ << r_off;
1440 		r_off += 8;
1441 		bits -= 8;
1442 	}
1443 	/* high order bits. */
1444 	code |= (*bp & rmask[bits]) << r_off;
1445 	offset += n_bits;
1446 
1447 	return (code);
1448 }
1449 
1450 #ifdef DEBUG
1451 static void
1452 printcodes()
1453 {
1454 	/*
1455 	 * Just print out codes from input file.  For debugging.
1456 	 */
1457 	code_int code;
1458 	int col = 0, bits;
1459 
1460 	bits = n_bits = INIT_BITS;
1461 	maxcode = MAXCODE(n_bits);
1462 	free_ent = ((block_compress) ? FIRST : 256);
1463 	while ((code = getcode()) >= 0) {
1464 		if ((code == CLEAR) && block_compress) {
1465 			free_ent = FIRST - 1;
1466 			clear_flg = 1;
1467 		} else if (free_ent < maxmaxcode)
1468 			free_ent++;
1469 		if (bits != n_bits) {
1470 			(void) fprintf(stderr, "\nChange to %d bits\n", n_bits);
1471 			bits = n_bits;
1472 			col = 0;
1473 		}
1474 		(void) fprintf(stderr, "%5d%c",
1475 		    code, (col += 6) >= 74 ? (col = 0, '\n') : ' ');
1476 	}
1477 	(void) putc('\n', stderr);
1478 }
1479 
1480 #endif /* DEBUG */
1481 
1482 #ifdef DEBUG
1483 static void
1484 dump_tab()	/* dump string table */
1485 {
1486 	int i, first;
1487 	int ent;
1488 	int stack_top = STACK_SIZE;
1489 	int c;
1490 
1491 	if (do_decomp == 0) {	/* compressing */
1492 		int flag = 1;
1493 
1494 		for (i = 0; i < hsize; i++) {	/* build sort pointers */
1495 			if ((long)htabof(i) >= 0) {
1496 				sorttab[codetabof(i)] = i;
1497 			}
1498 		}
1499 		first = block_compress ? FIRST : 256;
1500 		for (i = first; i < free_ent; i++) {
1501 			(void) fprintf(stderr, "%5d: \"", i);
1502 			de_stack[--stack_top] = '\n';
1503 			de_stack[--stack_top] = '"';
1504 			stack_top =
1505 			    in_stack((htabof(sorttab[i]) >> maxbits) & 0xff,
1506 			    stack_top);
1507 			for (ent = htabof(sorttab[i]) & ((1 << maxbits) -1);
1508 			    ent > 256;
1509 			    ent = htabof(sorttab[ent]) & ((1<<maxbits)-1)) {
1510 				stack_top = in_stack(
1511 				    htabof(sorttab[ent]) >> maxbits,
1512 				    stack_top);
1513 			}
1514 			stack_top = in_stack(ent, stack_top);
1515 			(void) fwrite(&de_stack[stack_top], 1,
1516 			    STACK_SIZE - stack_top, stderr);
1517 			stack_top = STACK_SIZE;
1518 		}
1519 	} else if (!debug) {	/* decompressing */
1520 
1521 		for (i = 0; i < free_ent; i++) {
1522 			ent = i;
1523 			c = tab_suffixof(ent);
1524 			if (isascii(c) && isprint(c))
1525 				(void) fprintf(stderr, "%5d: %5d/'%c'  \"",
1526 				    ent, tab_prefixof(ent), c);
1527 			else
1528 				(void) fprintf(stderr, "%5d: %5d/\\%03o \"",
1529 				    ent, tab_prefixof(ent), c);
1530 			de_stack[--stack_top] = '\n';
1531 			de_stack[--stack_top] = '"';
1532 			for (; ent != NULL;
1533 			    ent = (ent >= FIRST ? tab_prefixof(ent) :
1534 			    NULL)) {
1535 				stack_top = in_stack(tab_suffixof(ent),
1536 				    stack_top);
1537 			}
1538 			(void) fwrite(&de_stack[stack_top], 1,
1539 			    STACK_SIZE - stack_top, stderr);
1540 			stack_top = STACK_SIZE;
1541 		}
1542 	}
1543 }
1544 
1545 #endif /* DEBUG */
1546 #ifdef DEBUG
1547 static int
1548 in_stack(int c, int stack_top)
1549 {
1550 	if ((isascii(c) && isprint(c) && c != '\\') || c == ' ') {
1551 		de_stack[--stack_top] = c;
1552 	} else {
1553 		switch (c) {
1554 		case '\n': de_stack[--stack_top] = 'n'; break;
1555 		case '\t': de_stack[--stack_top] = 't'; break;
1556 		case '\b': de_stack[--stack_top] = 'b'; break;
1557 		case '\f': de_stack[--stack_top] = 'f'; break;
1558 		case '\r': de_stack[--stack_top] = 'r'; break;
1559 		case '\\': de_stack[--stack_top] = '\\'; break;
1560 		default:
1561 			de_stack[--stack_top] = '0' + c % 8;
1562 			de_stack[--stack_top] = '0' + (c / 8) % 8;
1563 			de_stack[--stack_top] = '0' + c / 64;
1564 			break;
1565 		}
1566 		de_stack[--stack_top] = '\\';
1567 	}
1568 	return (stack_top);
1569 }
1570 
1571 #endif /* DEBUG */
1572 static void
1573 ioerror()
1574 {
1575 	longjmp(env, 1);
1576 }
1577 
1578 static void
1579 copystat(char *ifname, struct stat *ifstat, char *ofname)
1580 {
1581 	mode_t mode;
1582 	struct utimbuf timep;
1583 	acl_t *aclp = NULL;
1584 	int error;
1585 
1586 	if (fclose(outp)) {
1587 		perror(ofname);
1588 		if (!quiet) {
1589 			(void) fprintf(stderr, gettext(" -- file unchanged"));
1590 			newline_needed = 1;
1591 		}
1592 		perm_stat = 1;
1593 	} else if (ifstat == NULL) {	/* Get stat on input file */
1594 		perror(ifname);
1595 		return;
1596 	} else if ((ifstat->st_mode &
1597 	    S_IFMT /* 0170000 */) != S_IFREG /* 0100000 */) {
1598 		if (quiet) {
1599 			(void) fprintf(stderr, "%s: ", ifname);
1600 		}
1601 		(void) fprintf(stderr, gettext(
1602 		    " -- not a regular file: unchanged"));
1603 		newline_needed = 1;
1604 		perm_stat = 1;
1605 	} else if (ifstat->st_nlink > 1) {
1606 		if (quiet) {
1607 			(void) fprintf(stderr, "%s: ", ifname);
1608 		}
1609 		(void) fprintf(stderr, gettext(
1610 		    " -- has %d other links: unchanged"),
1611 		    (uint_t)ifstat->st_nlink - 1);
1612 		newline_needed = 1;
1613 		perm_stat = 1;
1614 	} else if (didnt_shrink && !force) {
1615 		/* No compression: remove file.Z */
1616 		if (!quiet) {
1617 			(void) fprintf(stderr, gettext(
1618 			    " -- file unchanged"));
1619 			newline_needed = 1;
1620 		}
1621 	} else if ((pathconf(ifname, _PC_XATTR_EXISTS) == 1) &&
1622 	    (mv_xattrs(ifname, ofname, 0) < 0)) {
1623 		(void) fprintf(stderr, gettext(
1624 		    "%s: -- cannot preserve extended attributes, "
1625 		    "file unchanged"), ifname);
1626 		newline_needed = 1;
1627 		/* Move attributes back ... */
1628 		(void) mv_xattrs(ofname, ifname, 1);
1629 		perm_stat = 1;
1630 	} else {	/* ***** Successful Compression ***** */
1631 		mode = ifstat->st_mode & 07777;
1632 		if (chmod(ofname, mode))	 /* Copy modes */
1633 			perror(ofname);
1634 
1635 		error = acl_get(ifname, ACL_NO_TRIVIAL, &aclp);
1636 		if (error != 0) {
1637 			(void) fprintf(stderr, gettext(
1638 			    "%s: failed to retrieve acl : %s\n"),
1639 			    ifname, acl_strerror(error));
1640 			perm_stat = 1;
1641 		}
1642 		if (aclp && (acl_set(ofname, aclp) < 0)) {
1643 			(void) fprintf(stderr, gettext("%s: failed to set acl "
1644 			    "entries\n"), ofname);
1645 			perm_stat = 1;
1646 		}
1647 		if (aclp) {
1648 			acl_free(aclp);
1649 			aclp = NULL;
1650 		}
1651 
1652 		/* Copy ownership */
1653 		(void) chown(ofname, ifstat->st_uid, ifstat->st_gid);
1654 		timep.actime = ifstat->st_atime;
1655 		timep.modtime = ifstat->st_mtime;
1656 		/* Update last accessed and modified times */
1657 		(void) utime(ofname, &timep);
1658 		if (unlink(ifname))	/* Remove input file */
1659 			perror(ifname);
1660 		if (!quiet) {
1661 			(void) fprintf(stderr, gettext(
1662 			    " -- replaced with %s"), ofname);
1663 			newline_needed = 1;
1664 		}
1665 		return;		/* Successful return */
1666 	}
1667 
1668 	/* Unsuccessful return -- one of the tests failed */
1669 	if (ofname[0] != '\0') {
1670 		if (unlink(ofname)) {
1671 			perror(ofname);
1672 		}
1673 
1674 		ofname[0] = '\0';
1675 	}
1676 }
1677 
1678 static void
1679 onintr()
1680 {
1681 	if (!precious && !use_stdout && ofname[0] != '\0')
1682 		(void) unlink(ofname);
1683 	exit(1);
1684 }
1685 
1686 static void
1687 oops()	/* wild pointer -- assume bad input */
1688 {
1689 	if (do_decomp) {
1690 		(void) fprintf(stderr, gettext("uncompress: corrupt input\n"));
1691 	}
1692 
1693 	if (!use_stdout && ofname[0] != '\0') {
1694 		(void) unlink(ofname);
1695 	}
1696 
1697 	exit(1);
1698 }
1699 
1700 static void
1701 cl_block(count_long in_count)	/* table clear for block compress */
1702 {
1703 	count_long rat;
1704 
1705 	checkpoint = (count_long)in_count + (count_long)CHECK_GAP;
1706 #ifdef DEBUG
1707 	if (debug) {
1708 		(void) fprintf(stderr, "count: %lld, ratio: ",
1709 		    (count_long)in_count);
1710 		prratio(stderr, (count_long)in_count, (count_long)bytes_out);
1711 		(void) fprintf(stderr, "\n");
1712 	}
1713 #endif /* DEBUG */
1714 
1715 	/* shift will overflow */
1716 	if ((count_long)in_count > 0x007fffffffffffffLL) {
1717 		rat = (count_long)bytes_out >> 8;
1718 		if (rat == 0) {		/* Don't divide by zero */
1719 			rat = 0x7fffffffffffffffLL;
1720 		} else {
1721 			rat = (count_long)in_count / (count_long)rat;
1722 		}
1723 	} else {
1724 		/* 8 fractional bits */
1725 		rat = ((count_long)in_count << 8) /(count_long)bytes_out;
1726 	}
1727 	if (rat > ratio) {
1728 		ratio = rat;
1729 	} else {
1730 		ratio = 0;
1731 #ifdef DEBUG
1732 		if (verbose)
1733 			dump_tab();	/* dump string table */
1734 #endif
1735 		cl_hash((count_int) hsize);
1736 		free_ent = FIRST;
1737 		clear_flg = 1;
1738 		output((code_int) CLEAR);
1739 #ifdef DEBUG
1740 		if (debug)
1741 			(void) fprintf(stderr, "clear\n");
1742 #endif /* DEBUG */
1743 	}
1744 }
1745 
1746 static void
1747 cl_hash(count_int hsize)		/* reset code table */
1748 {
1749 	count_int *htab_p = htab+hsize;
1750 	long i;
1751 	long m1 = -1;
1752 
1753 	i = hsize - 16;
1754 	do {				/* might use Sys V memset(3) here */
1755 		*(htab_p-16) = m1;
1756 		*(htab_p-15) = m1;
1757 		*(htab_p-14) = m1;
1758 		*(htab_p-13) = m1;
1759 		*(htab_p-12) = m1;
1760 		*(htab_p-11) = m1;
1761 		*(htab_p-10) = m1;
1762 		*(htab_p-9) = m1;
1763 		*(htab_p-8) = m1;
1764 		*(htab_p-7) = m1;
1765 		*(htab_p-6) = m1;
1766 		*(htab_p-5) = m1;
1767 		*(htab_p-4) = m1;
1768 		*(htab_p-3) = m1;
1769 		*(htab_p-2) = m1;
1770 		*(htab_p-1) = m1;
1771 		htab_p -= 16;
1772 	} while ((i -= 16) >= 0);
1773 		for (i += 16; i > 0; i--)
1774 			*--htab_p = m1;
1775 }
1776 
1777 static void
1778 prratio(FILE *stream, count_long num, count_long den)
1779 {
1780 	int q;  /* store percentage */
1781 
1782 	q = (int)(10000LL * (count_long)num / (count_long)den);
1783 	if (q < 0) {
1784 		(void) putc('-', stream);
1785 		q = -q;
1786 	}
1787 	(void) fprintf(stream, "%d%s%02d%%", q / 100,
1788 	    localeconv()->decimal_point, q % 100);
1789 }
1790 
1791 static void
1792 version()
1793 {
1794 	(void) fprintf(stderr, "%s, Berkeley 5.9 5/11/86\n", rcs_ident);
1795 	(void) fprintf(stderr, "Options: ");
1796 #ifdef DEBUG
1797 	(void) fprintf(stderr, "DEBUG, ");
1798 #endif
1799 	(void) fprintf(stderr, "BITS = %d\n", BITS);
1800 }
1801 
1802 static void
1803 Usage()
1804 {
1805 #ifdef DEBUG
1806 	(void) fprintf(stderr,
1807 	"Usage: compress [-dDVfc] [-b maxbits] [file ...]\n");
1808 #else
1809 	if (strcmp(progname, "compress") == 0) {
1810 		(void) fprintf(stderr,
1811 		    gettext(
1812 		    "Usage: compress [-fv] [-b maxbits] [file ...]\n"\
1813 		    "       compress [-cfv] [-b maxbits] [file]\n"));
1814 	} else if (strcmp(progname, "uncompress") == 0)
1815 		(void) fprintf(stderr, gettext(
1816 		    "Usage: uncompress [-cfv] [file ...]\n"));
1817 	else if (strcmp(progname, "zcat") == 0)
1818 		(void) fprintf(stderr, gettext("Usage: zcat [file ...]\n"));
1819 
1820 #endif /* DEBUG */
1821 }
1822 
1823 static char *
1824 local_basename(char *path)
1825 {
1826 	char *p;
1827 	char *ret = (char *)path;
1828 
1829 	while ((p = (char *)strpbrk(ret, "/")) != NULL)
1830 		ret = p + 1;
1831 	return (ret);
1832 }
1833 
1834 static int
1835 addDotZ(char *fn, size_t fnsize)
1836 {
1837 	char *fn_dup;
1838 	char *dir;
1839 	long int max_name;
1840 	long int max_path;
1841 
1842 	fn_dup = strdup(fn);
1843 	dir = dirname(fn_dup);
1844 	max_name = pathconf(dir, _PC_NAME_MAX);
1845 	max_path = pathconf(dir, _PC_PATH_MAX);
1846 	free(fn_dup);
1847 
1848 	/* Check for component length too long */
1849 
1850 	if ((strlen(local_basename(fn)) + 2) > (size_t)max_name) {
1851 		(void) fprintf(stderr,
1852 		    gettext("%s: filename too long to tack on .Z:"
1853 		    " %s\n"), progname, fn);
1854 		return (-1);
1855 	}
1856 
1857 	/* Check for path length too long */
1858 
1859 	if ((strlen(fn) + 2) > (size_t)max_path - 1) {
1860 		(void) fprintf(stderr,
1861 		    gettext("%s: Pathname too long to tack on .Z:"
1862 		    " %s\n"), progname, fn);
1863 		return (-1);
1864 	}
1865 
1866 	if (strlcat(fn, ".Z", fnsize) >= fnsize) {
1867 		(void) fprintf(stderr,
1868 		    gettext("%s: Buffer overflow adding .Z to %s\n"),
1869 		    progname, fn);
1870 		return (-1);
1871 	}
1872 
1873 	return (0);
1874 }
1875 
1876 /*
1877  * mv_xattrs - move (via renameat) all of the extended attributes
1878  *	associated with the file infile to the file outfile.
1879  *	This function returns 0 on success and -1 on error.
1880  */
1881 static int
1882 mv_xattrs(char *infile, char *outfile, int silent)
1883 {
1884 	int indfd, outdfd, tmpfd;
1885 	DIR *dirp = NULL;
1886 	struct dirent *dp = NULL;
1887 	int error = 0;
1888 	char *etext;
1889 
1890 	indfd = outdfd = tmpfd = -1;
1891 
1892 	if ((indfd = attropen(infile, ".", O_RDONLY)) == -1) {
1893 		etext = gettext("cannot open source");
1894 		error = -1;
1895 		goto out;
1896 	}
1897 
1898 	if ((outdfd = attropen(outfile, ".", O_RDONLY)) == -1) {
1899 		etext = gettext("cannot open target");
1900 		error = -1;
1901 		goto out;
1902 	}
1903 
1904 	if ((tmpfd = dup(indfd)) == -1) {
1905 		etext = gettext("cannot dup descriptor");
1906 		error = -1;
1907 		goto out;
1908 
1909 	}
1910 	if ((dirp = fdopendir(tmpfd)) == NULL) {
1911 		etext = gettext("cannot access source");
1912 		error = -1;
1913 		goto out;
1914 	}
1915 
1916 	while (dp = readdir(dirp)) {
1917 		if ((dp->d_name[0] == '.' && dp->d_name[1] == '\0') ||
1918 		    (dp->d_name[0] == '.' && dp->d_name[1] == '.' &&
1919 		    dp->d_name[2] == '\0'))
1920 			continue;
1921 		if ((renameat(indfd, dp->d_name, outdfd, dp->d_name)) == -1) {
1922 			etext = dp->d_name;
1923 			error = -1;
1924 			goto out;
1925 		}
1926 	}
1927 out:
1928 	if (error == -1 && silent == 0) {
1929 		if (quiet) {
1930 			(void) fprintf(stderr, "%s: ", infile);
1931 		} else {
1932 			(void) fprintf(stderr, ", ");
1933 		}
1934 		(void) fprintf(stderr, gettext("extended attribute error: "));
1935 		perror(etext);
1936 	}
1937 	if (dirp)
1938 		(void) closedir(dirp);
1939 	if (indfd != -1)
1940 		(void) close(indfd);
1941 	if (outdfd != -1)
1942 		(void) close(outdfd);
1943 	return (error);
1944 }
1945