xref: /illumos-gate/usr/src/cmd/compress/compress.c (revision bc54f855)
1a77d64afScf /*
23d63ea05Sas  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
3a77d64afScf  * Use is subject to license terms.
4a77d64afScf  */
5a77d64afScf 
67c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
77c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate 
107c478bd9Sstevel@tonic-gate /*
117c478bd9Sstevel@tonic-gate  * Copyright (c) 1986 Regents of the University of California.
127c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
137c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
147c478bd9Sstevel@tonic-gate  */
157c478bd9Sstevel@tonic-gate 
16*bc54f855SJohn Levon /*
17*bc54f855SJohn Levon  * Copyright (c) 2018, Joyent, Inc.
18*bc54f855SJohn Levon  */
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate /*
217c478bd9Sstevel@tonic-gate  * Compress - data compression program
227c478bd9Sstevel@tonic-gate  */
237c478bd9Sstevel@tonic-gate #define	min(a, b)	((a > b) ? b : a)
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
267c478bd9Sstevel@tonic-gate  * machine variants which require cc -Dmachine:  pdp11, z8000, pcxt
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Set USERMEM to the maximum amount of physical user memory available
317c478bd9Sstevel@tonic-gate  * in bytes.  USERMEM is used to determine the maximum BITS that can be used
327c478bd9Sstevel@tonic-gate  * for compression.
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * SACREDMEM is the amount of physical memory saved for others; compress
357c478bd9Sstevel@tonic-gate  * will hog the rest.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate #ifndef SACREDMEM
387c478bd9Sstevel@tonic-gate #define	SACREDMEM	0
397c478bd9Sstevel@tonic-gate #endif
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #ifndef USERMEM
427c478bd9Sstevel@tonic-gate #define	USERMEM 	450000	/* default user memory */
437c478bd9Sstevel@tonic-gate #endif
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #ifdef USERMEM
467c478bd9Sstevel@tonic-gate #if USERMEM >= (433484+SACREDMEM)
477c478bd9Sstevel@tonic-gate #define	PBITS	16
487c478bd9Sstevel@tonic-gate #else
497c478bd9Sstevel@tonic-gate #if USERMEM >= (229600+SACREDMEM)
507c478bd9Sstevel@tonic-gate #define	PBITS	15
517c478bd9Sstevel@tonic-gate #else
527c478bd9Sstevel@tonic-gate #if USERMEM >= (127536+SACREDMEM)
537c478bd9Sstevel@tonic-gate #define	PBITS	14
547c478bd9Sstevel@tonic-gate #else
557c478bd9Sstevel@tonic-gate #if USERMEM >= (73464+SACREDMEM)
567c478bd9Sstevel@tonic-gate #define	PBITS	13
577c478bd9Sstevel@tonic-gate #else
587c478bd9Sstevel@tonic-gate #define	PBITS	12
597c478bd9Sstevel@tonic-gate #endif
607c478bd9Sstevel@tonic-gate #endif
617c478bd9Sstevel@tonic-gate #endif
627c478bd9Sstevel@tonic-gate #endif
637c478bd9Sstevel@tonic-gate #undef USERMEM
647c478bd9Sstevel@tonic-gate #endif /* USERMEM */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #ifdef PBITS		/* Preferred BITS for this memory size */
677c478bd9Sstevel@tonic-gate #ifndef BITS
687c478bd9Sstevel@tonic-gate #define	BITS PBITS
697c478bd9Sstevel@tonic-gate #endif /* BITS */
707c478bd9Sstevel@tonic-gate #endif /* PBITS */
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #if BITS == 16
737c478bd9Sstevel@tonic-gate #define	HSIZE	69001		/* 95% occupancy */
747c478bd9Sstevel@tonic-gate #endif
757c478bd9Sstevel@tonic-gate #if BITS == 15
767c478bd9Sstevel@tonic-gate #define	HSIZE	35023		/* 94% occupancy */
777c478bd9Sstevel@tonic-gate #endif
787c478bd9Sstevel@tonic-gate #if BITS == 14
797c478bd9Sstevel@tonic-gate #define	HSIZE	18013		/* 91% occupancy */
807c478bd9Sstevel@tonic-gate #endif
817c478bd9Sstevel@tonic-gate #if BITS == 13
827c478bd9Sstevel@tonic-gate #define	HSIZE	9001		/* 91% occupancy */
837c478bd9Sstevel@tonic-gate #endif
847c478bd9Sstevel@tonic-gate #if BITS <= 12
857c478bd9Sstevel@tonic-gate #define	HSIZE	5003		/* 80% occupancy */
867c478bd9Sstevel@tonic-gate #endif
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #define	OUTSTACKSIZE	(2<<BITS)
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  * a code_int must be able to hold 2**BITS values of type int, and also -1
927c478bd9Sstevel@tonic-gate  */
937c478bd9Sstevel@tonic-gate #if BITS > 15
947c478bd9Sstevel@tonic-gate typedef long int	code_int;
957c478bd9Sstevel@tonic-gate #else
967c478bd9Sstevel@tonic-gate typedef int		code_int;
977c478bd9Sstevel@tonic-gate #endif
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate typedef long int	count_int;
1007c478bd9Sstevel@tonic-gate typedef long long	count_long;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate typedef	unsigned char	char_type;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static char_type magic_header[] = { "\037\235" }; /* 1F 9D */
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /* Defines for third byte of header */
1077c478bd9Sstevel@tonic-gate #define	BIT_MASK	0x1f
1087c478bd9Sstevel@tonic-gate #define	BLOCK_MASK	0x80
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate  * Masks 0x40 and 0x20 are free.  I think 0x20 should mean that there is
1117c478bd9Sstevel@tonic-gate  * a fourth header byte(for expansion).
1127c478bd9Sstevel@tonic-gate  */
1137c478bd9Sstevel@tonic-gate #define	INIT_BITS 9			/* initial number of bits/code */
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /*
1167c478bd9Sstevel@tonic-gate  * compress.c - File compression ala IEEE Computer, June 1984.
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate static char rcs_ident[] =
1197c478bd9Sstevel@tonic-gate 	"$Header: compress.c,v 4.0 85/07/30 12:50:00 joe Release $";
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate #include <ctype.h>
1227c478bd9Sstevel@tonic-gate #include <signal.h>
1237c478bd9Sstevel@tonic-gate #include <sys/param.h>
1247c478bd9Sstevel@tonic-gate #include <locale.h>
1257c478bd9Sstevel@tonic-gate #include <langinfo.h>
1267c478bd9Sstevel@tonic-gate #include <sys/acl.h>
1277c478bd9Sstevel@tonic-gate #include <utime.h>
1287c478bd9Sstevel@tonic-gate #include <libgen.h>
1297c478bd9Sstevel@tonic-gate #include <setjmp.h>
130fa9e4066Sahrens #include <aclutils.h>
131da6c28aaSamw #include <libcmdutils.h>
1323d63ea05Sas #include "getresponse.h"
1337c478bd9Sstevel@tonic-gate 
134da6c28aaSamw 
1357c478bd9Sstevel@tonic-gate static int n_bits;			/* number of bits/code */
1367c478bd9Sstevel@tonic-gate static int maxbits = BITS;	/* user settable max # bits/code */
1377c478bd9Sstevel@tonic-gate static code_int maxcode;	/* maximum code, given n_bits */
1387c478bd9Sstevel@tonic-gate 			/* should NEVER generate this code */
1397c478bd9Sstevel@tonic-gate static code_int maxmaxcode = 1 << BITS;
1407c478bd9Sstevel@tonic-gate #define	MAXCODE(n_bits)	((1 << (n_bits)) - 1)
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate static count_int htab [OUTSTACKSIZE];
1437c478bd9Sstevel@tonic-gate static unsigned short codetab [OUTSTACKSIZE];
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate #define	htabof(i)	htab[i]
1467c478bd9Sstevel@tonic-gate #define	codetabof(i)	codetab[i]
1477c478bd9Sstevel@tonic-gate static code_int hsize = HSIZE; /* for dynamic table sizing */
1487c478bd9Sstevel@tonic-gate static off_t	fsize;	/* file size of input file */
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate  * To save much memory, we overlay the table used by compress() with those
1527c478bd9Sstevel@tonic-gate  * used by decompress().  The tab_prefix table is the same size and type
1537c478bd9Sstevel@tonic-gate  * as the codetab.  The tab_suffix table needs 2**BITS characters.  We
1547c478bd9Sstevel@tonic-gate  * get this from the beginning of htab.  The output stack uses the rest
1557c478bd9Sstevel@tonic-gate  * of htab, and contains characters.  There is plenty of room for any
1567c478bd9Sstevel@tonic-gate  * possible stack (stack used to be 8000 characters).
1577c478bd9Sstevel@tonic-gate  */
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate #define	tab_prefixof(i)		codetabof(i)
1607c478bd9Sstevel@tonic-gate #define	tab_suffixof(i)		((char_type *)(htab))[i]
1617c478bd9Sstevel@tonic-gate #define	de_stack		((char_type *)&tab_suffixof(1<<BITS))
1627c478bd9Sstevel@tonic-gate #define	stack_max		((char_type *)&tab_suffixof(OUTSTACKSIZE))
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate static code_int free_ent = 0; /* first unused entry */
1657c478bd9Sstevel@tonic-gate static int newline_needed = 0;
1667c478bd9Sstevel@tonic-gate static int didnt_shrink = 0;
1677c478bd9Sstevel@tonic-gate static int perm_stat = 0;	/* permanent status */
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate static code_int getcode();
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	/* Use a 3-byte magic number header, unless old file */
1727c478bd9Sstevel@tonic-gate static int nomagic = 0;
1737c478bd9Sstevel@tonic-gate 	/* Write output on stdout, suppress messages */
1747c478bd9Sstevel@tonic-gate static int zcat_flg = 0;	/* use stdout on all files */
1757c478bd9Sstevel@tonic-gate static int zcat_cmd = 0;	/* zcat cmd */
1767c478bd9Sstevel@tonic-gate static int use_stdout = 0;	/* set for each file processed */
1777c478bd9Sstevel@tonic-gate 	/* Don't unlink output file on interrupt */
1787c478bd9Sstevel@tonic-gate static int precious = 1;
1797c478bd9Sstevel@tonic-gate static int quiet = 1;	/* don't tell me about compression */
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * block compression parameters -- after all codes are used up,
1837c478bd9Sstevel@tonic-gate  * and compression rate changes, start over.
1847c478bd9Sstevel@tonic-gate  */
1857c478bd9Sstevel@tonic-gate static int block_compress = BLOCK_MASK;
1867c478bd9Sstevel@tonic-gate static int clear_flg = 0;
1877c478bd9Sstevel@tonic-gate static long int ratio = 0;
1887c478bd9Sstevel@tonic-gate #define	CHECK_GAP 10000	/* ratio check interval */
1897c478bd9Sstevel@tonic-gate static count_long checkpoint = CHECK_GAP;
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate  * the next two codes should not be changed lightly, as they must not
1927c478bd9Sstevel@tonic-gate  * lie within the contiguous general code space.
1937c478bd9Sstevel@tonic-gate  */
1947c478bd9Sstevel@tonic-gate #define	FIRST	257	/* first free entry */
1957c478bd9Sstevel@tonic-gate #define	CLEAR	256	/* table clear output code */
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate static int force = 0;
1987c478bd9Sstevel@tonic-gate static char ofname [MAXPATHLEN];
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate static int Vflg = 0;
2017c478bd9Sstevel@tonic-gate static int vflg = 0;
2027c478bd9Sstevel@tonic-gate static int qflg = 0;
2037c478bd9Sstevel@tonic-gate static int bflg = 0;
2047c478bd9Sstevel@tonic-gate static int Fflg = 0;
2057c478bd9Sstevel@tonic-gate static int dflg = 0;
2067c478bd9Sstevel@tonic-gate static int cflg = 0;
2077c478bd9Sstevel@tonic-gate static int Cflg = 0;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate #ifdef DEBUG
2107c478bd9Sstevel@tonic-gate int verbose = 0;
2117c478bd9Sstevel@tonic-gate int debug = 0;
2127c478bd9Sstevel@tonic-gate #endif /* DEBUG */
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate static void (*oldint)();
2157c478bd9Sstevel@tonic-gate static int bgnd_flag;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate static int do_decomp = 0;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate static char *progname;
2207c478bd9Sstevel@tonic-gate static char *optstr;
2217c478bd9Sstevel@tonic-gate /*
2227c478bd9Sstevel@tonic-gate  * Fix lint errors
2237c478bd9Sstevel@tonic-gate  */
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate static char *local_basename(char *);
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate static int  addDotZ(char *, size_t);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate static void Usage(void);
2307c478bd9Sstevel@tonic-gate static void cl_block(count_long);
2317c478bd9Sstevel@tonic-gate static void cl_hash(count_int);
2327c478bd9Sstevel@tonic-gate static void compress(void);
2337c478bd9Sstevel@tonic-gate static void copystat(char *, struct stat *, char *);
2347c478bd9Sstevel@tonic-gate static void decompress(void);
2357c478bd9Sstevel@tonic-gate static void ioerror(void);
2367c478bd9Sstevel@tonic-gate static void onintr();
2377c478bd9Sstevel@tonic-gate static void oops();
2387c478bd9Sstevel@tonic-gate static void output(code_int);
2397c478bd9Sstevel@tonic-gate static void prratio(FILE *, count_long, count_long);
2407c478bd9Sstevel@tonic-gate static void version(void);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate #ifdef DEBUG
2437c478bd9Sstevel@tonic-gate static int in_stack(int, int);
2447c478bd9Sstevel@tonic-gate static void dump_tab(void);
2457c478bd9Sstevel@tonic-gate static void printcodes(void);
2467c478bd9Sstevel@tonic-gate #endif
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate /* For error-handling */
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate static jmp_buf env;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate /* For input and ouput */
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate static FILE *inp;		/* the current input file */
2557c478bd9Sstevel@tonic-gate static FILE *infile;		/* disk-based input stream */
2567c478bd9Sstevel@tonic-gate static FILE *outp;		/* current output file */
2577c478bd9Sstevel@tonic-gate static FILE *outfile;		/* disk-based output stream */
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate /* For output() */
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate static char buf[BITS];
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate static char_type lmask[9] =
2647c478bd9Sstevel@tonic-gate 	{0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
2657c478bd9Sstevel@tonic-gate static char_type rmask[9] =
2667c478bd9Sstevel@tonic-gate 	{0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate /* For compress () */
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate static int offset;
2717c478bd9Sstevel@tonic-gate static count_long bytes_out;	/* length of compressed output */
2727c478bd9Sstevel@tonic-gate 	/* # of codes output (for debugging) */
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate /* For dump_tab() */
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate #define	STACK_SIZE	15000
2777c478bd9Sstevel@tonic-gate #ifdef DEBUG
2787c478bd9Sstevel@tonic-gate code_int sorttab[1<<BITS];	/* sorted pointers into htab */
2797c478bd9Sstevel@tonic-gate #endif
2807c478bd9Sstevel@tonic-gate 
281da6c28aaSamw /* Extended system attribute support */
282da6c28aaSamw 
283da6c28aaSamw static int saflg = 0;
284da6c28aaSamw 
2857c478bd9Sstevel@tonic-gate /*
2867c478bd9Sstevel@tonic-gate  * *************************************************************
2877c478bd9Sstevel@tonic-gate  * TAG( main )
2887c478bd9Sstevel@tonic-gate  *
2897c478bd9Sstevel@tonic-gate  * Algorithm from "A Technique for High Performance Data Compression",
2907c478bd9Sstevel@tonic-gate  * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.
2917c478bd9Sstevel@tonic-gate  *
292da6c28aaSamw  * Usage: compress [-dfvc/] [-b bits] [file ...]
2937c478bd9Sstevel@tonic-gate  * Inputs:
2947c478bd9Sstevel@tonic-gate  *	-d:	    If given, decompression is done instead.
2957c478bd9Sstevel@tonic-gate  *
2967c478bd9Sstevel@tonic-gate  *	-c:	    Write output on stdout, don't remove original.
2977c478bd9Sstevel@tonic-gate  *
2987c478bd9Sstevel@tonic-gate  *	-b:	    Parameter limits the max number of bits/code.
2997c478bd9Sstevel@tonic-gate  *
3007c478bd9Sstevel@tonic-gate  *	-f:	    Forces output file to be generated, even if one already
3017c478bd9Sstevel@tonic-gate  *		    exists, and even if no space is saved by compressing.
3027c478bd9Sstevel@tonic-gate  *		    If -f is not used, the user will be prompted if stdin is
3037c478bd9Sstevel@tonic-gate  *		    a tty, otherwise, the output file will not be overwritten.
3047c478bd9Sstevel@tonic-gate  *
305da6c28aaSamw  *	-/	    Copies extended attributes and extended system attributes.
306da6c28aaSamw  *
3077c478bd9Sstevel@tonic-gate  *  -v:	    Write compression statistics
3087c478bd9Sstevel@tonic-gate  *
3097c478bd9Sstevel@tonic-gate  * 	file ...:   Files to be compressed.  If none specified, stdin
3107c478bd9Sstevel@tonic-gate  *		    is used.
3117c478bd9Sstevel@tonic-gate  * Outputs:
3127c478bd9Sstevel@tonic-gate  *	file.Z:	    Compressed form of file with same mode, owner, and utimes
3137c478bd9Sstevel@tonic-gate  * 	or stdout   (if stdin used as input)
3147c478bd9Sstevel@tonic-gate  *
3157c478bd9Sstevel@tonic-gate  * Assumptions:
3167c478bd9Sstevel@tonic-gate  * When filenames are given, replaces with the compressed version
3177c478bd9Sstevel@tonic-gate  * (.Z suffix) only if the file decreases in size.
3187c478bd9Sstevel@tonic-gate  * Algorithm:
3197c478bd9Sstevel@tonic-gate  * Modified Lempel-Ziv method (LZW).  Basically finds common
3207c478bd9Sstevel@tonic-gate  * substrings and replaces them with a variable size code.  This is
3217c478bd9Sstevel@tonic-gate  * deterministic, and can be done on the fly.  Thus, the decompression
3227c478bd9Sstevel@tonic-gate  * procedure needs no input table, but tracks the way the table was built.
3237c478bd9Sstevel@tonic-gate  */
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])3267c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	int overwrite = 0;	/* Do not overwrite unless given -f flag */
3297c478bd9Sstevel@tonic-gate 	char tempname[MAXPATHLEN];
3307c478bd9Sstevel@tonic-gate 	char line[LINE_MAX];
3317c478bd9Sstevel@tonic-gate 	char **filelist, **fileptr;
3327c478bd9Sstevel@tonic-gate 	char *cp;
3337c478bd9Sstevel@tonic-gate 	struct stat statbuf;
3347c478bd9Sstevel@tonic-gate 	struct stat ostatbuf;
3357c478bd9Sstevel@tonic-gate 	int ch;				/* XCU4 */
3363d63ea05Sas 	char	*p;
3377c478bd9Sstevel@tonic-gate 	extern int optind, optopt;
3387c478bd9Sstevel@tonic-gate 	extern char *optarg;
3397c478bd9Sstevel@tonic-gate 	int dash_count = 0;		/* times "-" is on cmdline */
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	/* XCU4 changes */
3427c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3437c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
3447c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
3457c478bd9Sstevel@tonic-gate #endif
3467c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3477c478bd9Sstevel@tonic-gate 
3483d63ea05Sas 	if (init_yes() < 0) {
3493d63ea05Sas 		(void) fprintf(stderr, gettext(ERR_MSG_INIT_YES),
3503d63ea05Sas 		    strerror(errno));
3513d63ea05Sas 		exit(1);
3523d63ea05Sas 	}
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	/* This bg check only works for sh. */
3557c478bd9Sstevel@tonic-gate 	if ((oldint = signal(SIGINT, SIG_IGN)) != SIG_IGN) {
3567c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, onintr);
3577c478bd9Sstevel@tonic-gate 		(void) signal(SIGSEGV, oops);
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 	bgnd_flag = oldint != SIG_DFL;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	/* Allocate room for argv + "-" (if stdin needs to be added) */
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	filelist = fileptr = (char **)(malloc((argc + 1) * sizeof (*argv)));
3647c478bd9Sstevel@tonic-gate 	*filelist = NULL;
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	if ((cp = rindex(argv[0], '/')) != 0) {
3677c478bd9Sstevel@tonic-gate 		cp++;
3687c478bd9Sstevel@tonic-gate 	} else {
3697c478bd9Sstevel@tonic-gate 		cp = argv[0];
3707c478bd9Sstevel@tonic-gate 	}
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	if (strcmp(cp, "uncompress") == 0) {
3737c478bd9Sstevel@tonic-gate 		do_decomp = 1;
3747c478bd9Sstevel@tonic-gate 	} else if (strcmp(cp, "zcat") == 0) {
3757c478bd9Sstevel@tonic-gate 		do_decomp = 1;
3767c478bd9Sstevel@tonic-gate 		zcat_cmd = zcat_flg = 1;
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	progname = local_basename(argv[0]);
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	/*
3827c478bd9Sstevel@tonic-gate 	 * Argument Processing
3837c478bd9Sstevel@tonic-gate 	 * All flags are optional.
3847c478bd9Sstevel@tonic-gate 	 * -D = > debug
3857c478bd9Sstevel@tonic-gate 	 * -V = > print Version; debug verbose
3867c478bd9Sstevel@tonic-gate 	 * -d = > do_decomp
3877c478bd9Sstevel@tonic-gate 	 * -v = > unquiet
3887c478bd9Sstevel@tonic-gate 	 * -f = > force overwrite of output file
3897c478bd9Sstevel@tonic-gate 	 * -n = > no header: useful to uncompress old files
3907c478bd9Sstevel@tonic-gate 	 * -b	  maxbits => maxbits.  If -b is specified,
3917c478bd9Sstevel@tonic-gate 	 *	  then maxbits MUST be given also.
3927c478bd9Sstevel@tonic-gate 	 * -c = > cat all output to stdout
3937c478bd9Sstevel@tonic-gate 	 * -C = > generate output compatible with compress 2.0.
3947c478bd9Sstevel@tonic-gate 	 * if a string is left, must be an input filename.
3957c478bd9Sstevel@tonic-gate 	 */
3967c478bd9Sstevel@tonic-gate #ifdef DEBUG
397da6c28aaSamw 	optstr = "b:cCdDfFnqvV/";
3987c478bd9Sstevel@tonic-gate #else
399da6c28aaSamw 	optstr = "b:cCdfFnqvV/";
4007c478bd9Sstevel@tonic-gate #endif
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	while ((ch = getopt(argc, argv, optstr)) != EOF) {
4037c478bd9Sstevel@tonic-gate 		/* Process all flags in this arg */
4047c478bd9Sstevel@tonic-gate 		switch (ch) {
4057c478bd9Sstevel@tonic-gate #ifdef DEBUG
4067c478bd9Sstevel@tonic-gate 			case 'D':
4077c478bd9Sstevel@tonic-gate 				debug = 1;
4087c478bd9Sstevel@tonic-gate 				break;
4097c478bd9Sstevel@tonic-gate 			case 'V':
4107c478bd9Sstevel@tonic-gate 				verbose = 1;
4117c478bd9Sstevel@tonic-gate 				version();
4127c478bd9Sstevel@tonic-gate 				break;
4137c478bd9Sstevel@tonic-gate #else
4147c478bd9Sstevel@tonic-gate 			case 'V':
4157c478bd9Sstevel@tonic-gate 				version();
4167c478bd9Sstevel@tonic-gate 				Vflg++;
4177c478bd9Sstevel@tonic-gate 				break;
4187c478bd9Sstevel@tonic-gate #endif /* DEBUG */
4197c478bd9Sstevel@tonic-gate 			case 'v':
4207c478bd9Sstevel@tonic-gate 				quiet = 0;
4217c478bd9Sstevel@tonic-gate 				vflg++;
4227c478bd9Sstevel@tonic-gate 				break;
4237c478bd9Sstevel@tonic-gate 			case 'd':
4247c478bd9Sstevel@tonic-gate 				do_decomp = 1;
4257c478bd9Sstevel@tonic-gate 				dflg++;
4267c478bd9Sstevel@tonic-gate 				break;
4277c478bd9Sstevel@tonic-gate 			case 'f':
4287c478bd9Sstevel@tonic-gate 			case 'F':
4297c478bd9Sstevel@tonic-gate 				Fflg++;
4307c478bd9Sstevel@tonic-gate 				overwrite = 1;
4317c478bd9Sstevel@tonic-gate 				force = 1;
4327c478bd9Sstevel@tonic-gate 				break;
4337c478bd9Sstevel@tonic-gate 			case 'n':
4347c478bd9Sstevel@tonic-gate 				nomagic = 1;
4357c478bd9Sstevel@tonic-gate 				break;
4367c478bd9Sstevel@tonic-gate 			case 'C':
4377c478bd9Sstevel@tonic-gate 				Cflg++;
4387c478bd9Sstevel@tonic-gate 				block_compress = 0;
4397c478bd9Sstevel@tonic-gate 				break;
4407c478bd9Sstevel@tonic-gate 			case 'b':
4417c478bd9Sstevel@tonic-gate 				bflg++;
4427c478bd9Sstevel@tonic-gate 				p = optarg;
4437c478bd9Sstevel@tonic-gate 				if (!p) {
4447c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
4453d63ea05Sas 					    "Missing maxbits\n"));
4467c478bd9Sstevel@tonic-gate 					Usage();
4477c478bd9Sstevel@tonic-gate 					exit(1);
4487c478bd9Sstevel@tonic-gate 				}
4497c478bd9Sstevel@tonic-gate 				maxbits = strtoul(optarg, &p, 10);
4507c478bd9Sstevel@tonic-gate 				if (*p) {
4517c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
4523d63ea05Sas 					    "Missing maxbits\n"));
4537c478bd9Sstevel@tonic-gate 					Usage();
4547c478bd9Sstevel@tonic-gate 					exit(1);
4557c478bd9Sstevel@tonic-gate 				}
4567c478bd9Sstevel@tonic-gate 				break;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 			case 'c':
4597c478bd9Sstevel@tonic-gate 				cflg++;
4607c478bd9Sstevel@tonic-gate 				zcat_flg = 1;
4617c478bd9Sstevel@tonic-gate 				break;
4627c478bd9Sstevel@tonic-gate 			case 'q':
4637c478bd9Sstevel@tonic-gate 				qflg++;
4647c478bd9Sstevel@tonic-gate 				quiet = 1;
4657c478bd9Sstevel@tonic-gate 				break;
466da6c28aaSamw 			case '/':
467da6c28aaSamw 				saflg++;
468da6c28aaSamw 				break;
4697c478bd9Sstevel@tonic-gate 			default:
4707c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
4713d63ea05Sas 				    "Unknown flag: '%c'\n"), optopt);
4727c478bd9Sstevel@tonic-gate 				Usage();
4737c478bd9Sstevel@tonic-gate 				exit(1);
4747c478bd9Sstevel@tonic-gate 		}
4757c478bd9Sstevel@tonic-gate 	} /* while */
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	/*
4787c478bd9Sstevel@tonic-gate 	 * Validate zcat syntax
4797c478bd9Sstevel@tonic-gate 	 */
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	if (zcat_cmd && (Fflg | Cflg | cflg |
4827c478bd9Sstevel@tonic-gate 	    bflg | qflg | dflg | nomagic)) {
4837c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
4843d63ea05Sas 		    "Invalid Option\n"));
4857c478bd9Sstevel@tonic-gate 		Usage();
4867c478bd9Sstevel@tonic-gate 		exit(1);
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	/*
4907c478bd9Sstevel@tonic-gate 	 * Process the file list
4917c478bd9Sstevel@tonic-gate 	 */
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	for (; optind < argc; optind++) {
4947c478bd9Sstevel@tonic-gate 		if (strcmp(argv[optind], "-") == 0) {
4957c478bd9Sstevel@tonic-gate 			dash_count++;
4967c478bd9Sstevel@tonic-gate 		}
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 		*fileptr++ = argv[optind];	/* Build input file list */
4997c478bd9Sstevel@tonic-gate 		*fileptr = NULL;
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	if (dash_count > 1) {
5037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5043d63ea05Sas 		    gettext("%s may only appear once in the file"
5053d63ea05Sas 		    " list\n"), "\"-\"");
5067c478bd9Sstevel@tonic-gate 		exit(1);
5077c478bd9Sstevel@tonic-gate 	}
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	if (fileptr - filelist == 0) {
5107c478bd9Sstevel@tonic-gate 		*fileptr++ = "-";
5117c478bd9Sstevel@tonic-gate 		*fileptr = NULL;
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	if (fileptr - filelist > 1 && cflg && !do_decomp) {
5157c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5163d63ea05Sas 		    gettext("compress: only one file may be compressed"
5173d63ea05Sas 		    " to stdout\n"));
5187c478bd9Sstevel@tonic-gate 		exit(1);
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	if (maxbits < INIT_BITS)
5227c478bd9Sstevel@tonic-gate 		maxbits = INIT_BITS;
5237c478bd9Sstevel@tonic-gate 	if (maxbits > BITS)
5247c478bd9Sstevel@tonic-gate 		maxbits = BITS;
5257c478bd9Sstevel@tonic-gate 	maxmaxcode = 1 << maxbits;
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	/* Need to open something to close with freopen later */
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	if ((infile = fopen("/dev/null", "r")) == NULL) {
5307c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Error opening /dev/null for "
5313d63ea05Sas 		    "input\n"));
5327c478bd9Sstevel@tonic-gate 		exit(1);
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	if ((outfile = fopen("/dev/null", "w")) == NULL) {
5367c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Error opening /dev/null for "
5373d63ea05Sas 		    "output\n"));
5387c478bd9Sstevel@tonic-gate 		exit(1);
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	for (fileptr = filelist; *fileptr; fileptr++) {
5427c478bd9Sstevel@tonic-gate 		int jmpval = 0;
5437c478bd9Sstevel@tonic-gate 		didnt_shrink = 0;
5447c478bd9Sstevel@tonic-gate 		newline_needed = 0;
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 		if (do_decomp) {
5477c478bd9Sstevel@tonic-gate 			/* DECOMPRESSION */
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 			if (strcmp(*fileptr, "-") == 0) {
5507c478bd9Sstevel@tonic-gate 				/* process stdin */
5517c478bd9Sstevel@tonic-gate 				inp = stdin;
5527c478bd9Sstevel@tonic-gate 				outp = stdout;
5537c478bd9Sstevel@tonic-gate 				use_stdout = 1;
5547c478bd9Sstevel@tonic-gate 				*fileptr = "stdin"; /* for error messages */
5557c478bd9Sstevel@tonic-gate 			} else {
5567c478bd9Sstevel@tonic-gate 				/* process the named file */
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 				inp = infile;
5597c478bd9Sstevel@tonic-gate 				outp = outfile;
5607c478bd9Sstevel@tonic-gate 				use_stdout = 0;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 				if (zcat_flg) {
5637c478bd9Sstevel@tonic-gate 					use_stdout = 1;
5647c478bd9Sstevel@tonic-gate 					outp = stdout;
5657c478bd9Sstevel@tonic-gate 				}
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 				/* Check for .Z suffix */
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 				if (strcmp(*fileptr +
5707c478bd9Sstevel@tonic-gate 				    strlen(*fileptr) - 2, ".Z") != 0) {
5717c478bd9Sstevel@tonic-gate 					/* No .Z: tack one on */
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 					if (strlcpy(tempname, *fileptr,
5743d63ea05Sas 					    sizeof (tempname)) >=
5753d63ea05Sas 					    sizeof (tempname)) {
5767c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
5777c478bd9Sstevel@tonic-gate 						    gettext("%s: filename "
5783d63ea05Sas 						    "too long\n"),
5793d63ea05Sas 						    *fileptr);
5807c478bd9Sstevel@tonic-gate 						perm_stat = 1;
5817c478bd9Sstevel@tonic-gate 						continue;
5827c478bd9Sstevel@tonic-gate 					}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 					if (addDotZ(tempname,
5857c478bd9Sstevel@tonic-gate 					    sizeof (tempname)) < 0) {
5867c478bd9Sstevel@tonic-gate 						perm_stat = 1;
5877c478bd9Sstevel@tonic-gate 						continue;
5887c478bd9Sstevel@tonic-gate 					}
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 					*fileptr = tempname;
5917c478bd9Sstevel@tonic-gate 				}
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 				/* Open input file */
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 				if (stat(*fileptr, &statbuf) < 0) {
5967c478bd9Sstevel@tonic-gate 					perror(*fileptr);
5977c478bd9Sstevel@tonic-gate 					perm_stat = 1;
5987c478bd9Sstevel@tonic-gate 					continue;
5997c478bd9Sstevel@tonic-gate 				}
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 				if ((freopen(*fileptr, "r", inp)) == NULL) {
6027c478bd9Sstevel@tonic-gate 					perror(*fileptr);
6037c478bd9Sstevel@tonic-gate 					perm_stat = 1;
6047c478bd9Sstevel@tonic-gate 					continue;
6057c478bd9Sstevel@tonic-gate 				}
6067c478bd9Sstevel@tonic-gate 			}
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 			/* Check the magic number */
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 			if (nomagic == 0) {
6117c478bd9Sstevel@tonic-gate 				if ((getc(inp) !=
6127c478bd9Sstevel@tonic-gate 				    (magic_header[0] & 0xFF)) ||
6137c478bd9Sstevel@tonic-gate 				    (getc(inp) !=
6147c478bd9Sstevel@tonic-gate 				    (magic_header[1] & 0xFF))) {
6157c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
6163d63ea05Sas 					    "%s: not in compressed "
6173d63ea05Sas 					    "format\n"),
6183d63ea05Sas 					    *fileptr);
6197c478bd9Sstevel@tonic-gate 					perm_stat = 1;
6207c478bd9Sstevel@tonic-gate 					continue;
6217c478bd9Sstevel@tonic-gate 				}
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 				/* set -b from file */
6247c478bd9Sstevel@tonic-gate 				if ((maxbits = getc(inp)) == EOF &&
6257c478bd9Sstevel@tonic-gate 				    ferror(inp)) {
6267c478bd9Sstevel@tonic-gate 					perror(*fileptr);
6277c478bd9Sstevel@tonic-gate 					perm_stat = 1;
6287c478bd9Sstevel@tonic-gate 					continue;
6297c478bd9Sstevel@tonic-gate 				}
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 				block_compress = maxbits & BLOCK_MASK;
6327c478bd9Sstevel@tonic-gate 				maxbits &= BIT_MASK;
6337c478bd9Sstevel@tonic-gate 				maxmaxcode = 1 << maxbits;
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 				if (maxbits > BITS) {
6367c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
6373d63ea05Sas 					    gettext("%s: compressed "
6383d63ea05Sas 					    "with %d bits, "
6393d63ea05Sas 					    "can only handle"
6403d63ea05Sas 					    " %d bits\n"),
6413d63ea05Sas 					    *fileptr, maxbits, BITS);
6427c478bd9Sstevel@tonic-gate 					perm_stat = 1;
6437c478bd9Sstevel@tonic-gate 					continue;
6447c478bd9Sstevel@tonic-gate 				}
6457c478bd9Sstevel@tonic-gate 			}
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 			if (!use_stdout) {
6487c478bd9Sstevel@tonic-gate 				/* Generate output filename */
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 				if (strlcpy(ofname, *fileptr,
6513d63ea05Sas 				    sizeof (ofname)) >=
6527c478bd9Sstevel@tonic-gate 				    sizeof (ofname)) {
6537c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
6543d63ea05Sas 					    gettext("%s: filename "
6553d63ea05Sas 					    "too long\n"),
6563d63ea05Sas 					    *fileptr);
6577c478bd9Sstevel@tonic-gate 					perm_stat = 1;
6587c478bd9Sstevel@tonic-gate 					continue;
6597c478bd9Sstevel@tonic-gate 				}
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 				/* Strip off .Z */
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 				ofname[strlen(*fileptr) - 2] = '\0';
6647c478bd9Sstevel@tonic-gate 			}
6657c478bd9Sstevel@tonic-gate 		} else {
6667c478bd9Sstevel@tonic-gate 			/* COMPRESSION */
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 			if (strcmp(*fileptr, "-") == 0) {
6697c478bd9Sstevel@tonic-gate 				/* process stdin */
6707c478bd9Sstevel@tonic-gate 				inp = stdin;
6717c478bd9Sstevel@tonic-gate 				outp = stdout;
6727c478bd9Sstevel@tonic-gate 				use_stdout = 1;
6737c478bd9Sstevel@tonic-gate 				*fileptr = "stdin"; /* for error messages */
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 				/* Use the largest possible hash table */
6767c478bd9Sstevel@tonic-gate 				hsize =  HSIZE;
6777c478bd9Sstevel@tonic-gate 			} else {
6787c478bd9Sstevel@tonic-gate 				/* process the named file */
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 				inp = infile;
6817c478bd9Sstevel@tonic-gate 				outp = outfile;
6827c478bd9Sstevel@tonic-gate 				use_stdout = 0;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 				if (zcat_flg) {
6857c478bd9Sstevel@tonic-gate 					use_stdout = 1;
6867c478bd9Sstevel@tonic-gate 					outp = stdout;
6877c478bd9Sstevel@tonic-gate 				}
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 				if (strcmp(*fileptr +
6907c478bd9Sstevel@tonic-gate 				    strlen(*fileptr) - 2, ".Z") == 0) {
6917c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
6923d63ea05Sas 					    "%s: already has .Z "
6933d63ea05Sas 					    "suffix -- no change\n"),
6943d63ea05Sas 					    *fileptr);
6957c478bd9Sstevel@tonic-gate 					perm_stat = 1;
6967c478bd9Sstevel@tonic-gate 					continue;
6977c478bd9Sstevel@tonic-gate 				}
6987c478bd9Sstevel@tonic-gate 				/* Open input file */
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 				if (stat(*fileptr, &statbuf) < 0) {
7017c478bd9Sstevel@tonic-gate 					perror(*fileptr);
7027c478bd9Sstevel@tonic-gate 					perm_stat = 1;
7037c478bd9Sstevel@tonic-gate 					continue;
7047c478bd9Sstevel@tonic-gate 				}
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 				if ((freopen(*fileptr, "r", inp)) == NULL) {
7077c478bd9Sstevel@tonic-gate 					perror(*fileptr);
7087c478bd9Sstevel@tonic-gate 					perm_stat = 1;
7097c478bd9Sstevel@tonic-gate 					continue;
7107c478bd9Sstevel@tonic-gate 				}
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 				fsize = (off_t)statbuf.st_size;
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 				/*
7157c478bd9Sstevel@tonic-gate 				 * tune hash table size for small
7167c478bd9Sstevel@tonic-gate 				 * files -- ad hoc,
7177c478bd9Sstevel@tonic-gate 				 * but the sizes match earlier #defines, which
7187c478bd9Sstevel@tonic-gate 				 * serve as upper bounds on the number of
7197c478bd9Sstevel@tonic-gate 				 * output codes.
7207c478bd9Sstevel@tonic-gate 				 */
7217c478bd9Sstevel@tonic-gate 				hsize = HSIZE;
7227c478bd9Sstevel@tonic-gate 				if (fsize < (1 << 12))
7237c478bd9Sstevel@tonic-gate 					hsize = min(5003, HSIZE);
7247c478bd9Sstevel@tonic-gate 				else if (fsize < (1 << 13))
7257c478bd9Sstevel@tonic-gate 					hsize = min(9001, HSIZE);
7267c478bd9Sstevel@tonic-gate 				else if (fsize < (1 << 14))
7277c478bd9Sstevel@tonic-gate 					hsize = min(18013, HSIZE);
7287c478bd9Sstevel@tonic-gate 				else if (fsize < (1 << 15))
7297c478bd9Sstevel@tonic-gate 					hsize = min(35023, HSIZE);
7307c478bd9Sstevel@tonic-gate 				else if (fsize < 47000)
7317c478bd9Sstevel@tonic-gate 					hsize = min(50021, HSIZE);
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 				if (!use_stdout) {
7347c478bd9Sstevel@tonic-gate 					/* Generate output filename */
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 					if (strlcpy(ofname, *fileptr,
7373d63ea05Sas 					    sizeof (ofname)) >=
7383d63ea05Sas 					    sizeof (ofname)) {
7397c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
7407c478bd9Sstevel@tonic-gate 						    gettext("%s: filename "
7413d63ea05Sas 						    "too long\n"),
7423d63ea05Sas 						    *fileptr);
7437c478bd9Sstevel@tonic-gate 						perm_stat = 1;
7447c478bd9Sstevel@tonic-gate 						continue;
7457c478bd9Sstevel@tonic-gate 					}
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 					if (addDotZ(ofname,
7483d63ea05Sas 					    sizeof (ofname)) < 0) {
7497c478bd9Sstevel@tonic-gate 						perm_stat = 1;
7507c478bd9Sstevel@tonic-gate 						continue;
7517c478bd9Sstevel@tonic-gate 					}
7527c478bd9Sstevel@tonic-gate 				}
7537c478bd9Sstevel@tonic-gate 			}
7547c478bd9Sstevel@tonic-gate 		}	/* if (do_decomp) */
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 		/* Check for overwrite of existing file */
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 		if (!overwrite && !use_stdout) {
7597c478bd9Sstevel@tonic-gate 			if (stat(ofname, &ostatbuf) == 0) {
7607c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
7613d63ea05Sas 				    "%s already exists;"), ofname);
7627c478bd9Sstevel@tonic-gate 				if (bgnd_flag == 0 && isatty(2)) {
7637c478bd9Sstevel@tonic-gate 					int cin;
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
7663d63ea05Sas 					    " do you wish to overwr"
7673d63ea05Sas 					    "ite %s (%s or %s)? "),
7683d63ea05Sas 					    ofname, yesstr, nostr);
7697c478bd9Sstevel@tonic-gate 					(void) fflush(stderr);
7703d63ea05Sas 					for (cin = 0; cin < LINE_MAX; cin++)
7717c478bd9Sstevel@tonic-gate 						line[cin] = 0;
7727c478bd9Sstevel@tonic-gate 					(void) read(2, line, LINE_MAX);
7737c478bd9Sstevel@tonic-gate 
7743d63ea05Sas 					if (yes_check(line) == 0) {
7757c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
7763d63ea05Sas 						    gettext(
7773d63ea05Sas 						    "\tnot overwri"
7783d63ea05Sas 						    "tten\n"));
7797c478bd9Sstevel@tonic-gate 						continue;
7807c478bd9Sstevel@tonic-gate 					}
7817c478bd9Sstevel@tonic-gate 				} else {
7827c478bd9Sstevel@tonic-gate 					/*
7837c478bd9Sstevel@tonic-gate 					 * XPG4: Assertion 1009
7847c478bd9Sstevel@tonic-gate 					 * Standard input is not
7857c478bd9Sstevel@tonic-gate 					 * terminal, and no '-f',
7867c478bd9Sstevel@tonic-gate 					 * and file exists.
7877c478bd9Sstevel@tonic-gate 					 */
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
7903d63ea05Sas 					    "%s: File exists, -f not"
7913d63ea05Sas 					    " specified, and ru"
7923d63ea05Sas 					    "nning in the backgro"
7933d63ea05Sas 					    "und.\n"), *fileptr);
7947c478bd9Sstevel@tonic-gate 					perm_stat = 1;
7957c478bd9Sstevel@tonic-gate 					continue;
7967c478bd9Sstevel@tonic-gate 				}
7977c478bd9Sstevel@tonic-gate 			}
7987c478bd9Sstevel@tonic-gate 		}
7997c478bd9Sstevel@tonic-gate 		if (!use_stdout) {
800da6c28aaSamw 			if ((pathconf(ofname, _PC_XATTR_EXISTS) == 1) ||
801da6c28aaSamw 			    (saflg && sysattr_support(ofname,
802da6c28aaSamw 			    _PC_SATTR_EXISTS) == 1)) {
8037c478bd9Sstevel@tonic-gate 				(void) unlink(ofname);
8047c478bd9Sstevel@tonic-gate 			}
8057c478bd9Sstevel@tonic-gate 			/* Open output file */
8067c478bd9Sstevel@tonic-gate 			if (freopen(ofname, "w", outp) == NULL) {
8077c478bd9Sstevel@tonic-gate 				perror(ofname);
8087c478bd9Sstevel@tonic-gate 				perm_stat = 1;
8097c478bd9Sstevel@tonic-gate 				continue;
8107c478bd9Sstevel@tonic-gate 			}
8117c478bd9Sstevel@tonic-gate 			precious = 0;
8127c478bd9Sstevel@tonic-gate 			if (!quiet) {
8137c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "%s: ",
8143d63ea05Sas 				    *fileptr);
8157c478bd9Sstevel@tonic-gate 				newline_needed = 1;
8167c478bd9Sstevel@tonic-gate 			}
8177c478bd9Sstevel@tonic-gate 		} else if (!quiet && !do_decomp) {
8187c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: ",
8193d63ea05Sas 			    *fileptr);
820*bc54f855SJohn Levon 			newline_needed = 1;
8217c478bd9Sstevel@tonic-gate 		}
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 		/* Actually do the compression/decompression */
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 		if ((jmpval = setjmp(env)) == 0) {
8267c478bd9Sstevel@tonic-gate 			/* We'll see how things go */
8277c478bd9Sstevel@tonic-gate #ifndef DEBUG
8287c478bd9Sstevel@tonic-gate 			if (do_decomp == 0)  {
8297c478bd9Sstevel@tonic-gate 				compress();
8307c478bd9Sstevel@tonic-gate 			} else {
8317c478bd9Sstevel@tonic-gate 				decompress();
8327c478bd9Sstevel@tonic-gate 			}
8337c478bd9Sstevel@tonic-gate #else
8347c478bd9Sstevel@tonic-gate 			if (do_decomp == 0)  {
8357c478bd9Sstevel@tonic-gate 				compress();
8367c478bd9Sstevel@tonic-gate 			} else if (debug == 0)  {
8377c478bd9Sstevel@tonic-gate 				decompress();
8387c478bd9Sstevel@tonic-gate 			} else {
8397c478bd9Sstevel@tonic-gate 				printcodes();
8407c478bd9Sstevel@tonic-gate 			}
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 			if (verbose) {
8437c478bd9Sstevel@tonic-gate 				dump_tab();
8447c478bd9Sstevel@tonic-gate 			}
8457c478bd9Sstevel@tonic-gate #endif
8467c478bd9Sstevel@tonic-gate 		} else {
8477c478bd9Sstevel@tonic-gate 			/*
8487c478bd9Sstevel@tonic-gate 			 * Things went badly - clean up and go on.
8497c478bd9Sstevel@tonic-gate 			 * jmpval's values break down as follows:
8507c478bd9Sstevel@tonic-gate 			 *   1 == message determined by ferror() values.
8517c478bd9Sstevel@tonic-gate 			 *   2 == input problem message needed.
8527c478bd9Sstevel@tonic-gate 			 *   3 == output problem message needed.
8537c478bd9Sstevel@tonic-gate 			 */
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 			if (ferror(inp) || jmpval == 2) {
8567c478bd9Sstevel@tonic-gate 				if (do_decomp) {
8577c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
8583d63ea05Sas 					    "uncompress: %s: corrupt"
8593d63ea05Sas 					    " input\n"), *fileptr);
8607c478bd9Sstevel@tonic-gate 				} else {
8617c478bd9Sstevel@tonic-gate 					perror(*fileptr);
8627c478bd9Sstevel@tonic-gate 				}
8637c478bd9Sstevel@tonic-gate 			}
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 			if (ferror(outp) || jmpval == 3) {
8667c478bd9Sstevel@tonic-gate 				/* handle output errors */
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 				if (use_stdout) {
8697c478bd9Sstevel@tonic-gate 					perror("");
8707c478bd9Sstevel@tonic-gate 				} else {
8717c478bd9Sstevel@tonic-gate 					perror(ofname);
8727c478bd9Sstevel@tonic-gate 				}
8737c478bd9Sstevel@tonic-gate 			}
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 			if (ofname[0] != '\0') {
8767c478bd9Sstevel@tonic-gate 				if (unlink(ofname) < 0)  {
8777c478bd9Sstevel@tonic-gate 					perror(ofname);
8787c478bd9Sstevel@tonic-gate 				}
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 				ofname[0] = '\0';
8817c478bd9Sstevel@tonic-gate 			}
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 			perm_stat = 1;
8847c478bd9Sstevel@tonic-gate 			continue;
8857c478bd9Sstevel@tonic-gate 		}
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 		/* Things went well */
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 		if (!use_stdout) {
8907c478bd9Sstevel@tonic-gate 				/* Copy stats */
8917c478bd9Sstevel@tonic-gate 			copystat(*fileptr, &statbuf, ofname);
8927c478bd9Sstevel@tonic-gate 			precious = 1;
8937c478bd9Sstevel@tonic-gate 			if (newline_needed) {
8947c478bd9Sstevel@tonic-gate 				(void) putc('\n', stderr);
8957c478bd9Sstevel@tonic-gate 			}
8967c478bd9Sstevel@tonic-gate 			/*
8977c478bd9Sstevel@tonic-gate 			 * Print the info. for unchanged file
8987c478bd9Sstevel@tonic-gate 			 * when no -v
8997c478bd9Sstevel@tonic-gate 			 */
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 			if (didnt_shrink) {
9027c478bd9Sstevel@tonic-gate 				if (!force && perm_stat == 0) {
9037c478bd9Sstevel@tonic-gate 					if (quiet) {
9047c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(
9053d63ea05Sas 						    "%s: -- file "
9063d63ea05Sas 						    "unchanged\n"),
9073d63ea05Sas 						    *fileptr);
9087c478bd9Sstevel@tonic-gate 					}
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 					perm_stat = 2;
9117c478bd9Sstevel@tonic-gate 				}
9127c478bd9Sstevel@tonic-gate 			}
9137c478bd9Sstevel@tonic-gate 		} else {
9147c478bd9Sstevel@tonic-gate 			if (didnt_shrink && !force && perm_stat == 0) {
9157c478bd9Sstevel@tonic-gate 				perm_stat = 2;
9167c478bd9Sstevel@tonic-gate 			}
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 			if (newline_needed) {
9197c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\n");
9207c478bd9Sstevel@tonic-gate 			}
9217c478bd9Sstevel@tonic-gate 		}
9227c478bd9Sstevel@tonic-gate 	}	/* for */
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	return (perm_stat);
9257c478bd9Sstevel@tonic-gate }
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate static void
cinterr(int hshift)9287c478bd9Sstevel@tonic-gate cinterr(int hshift)
9297c478bd9Sstevel@tonic-gate {
9307c478bd9Sstevel@tonic-gate 	/* we have exceeded the hash table */
9317c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
9323d63ea05Sas 	    "internal error: hashtable exceeded - hsize = %ld\n", hsize);
9337c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "hshift = %d, %d\n", hshift, (1 << hshift) -1);
9347c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "maxbits = %d\n", maxbits);
9357c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "n_bits = %d\n", n_bits);
9367c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "maxcode = %ld\n", maxcode);
9377c478bd9Sstevel@tonic-gate 	longjmp(env, 1);
9387c478bd9Sstevel@tonic-gate }
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate static code_int
adjusti(code_int i,code_int hsize_reg)9417c478bd9Sstevel@tonic-gate adjusti(code_int i, code_int hsize_reg)
9427c478bd9Sstevel@tonic-gate {
9437c478bd9Sstevel@tonic-gate 	while (i < 0) {
9447c478bd9Sstevel@tonic-gate 		i += hsize_reg;
9457c478bd9Sstevel@tonic-gate 	}
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	while (i >= hsize_reg) {
9487c478bd9Sstevel@tonic-gate 		i -= hsize_reg;
9497c478bd9Sstevel@tonic-gate 	}
9507c478bd9Sstevel@tonic-gate 	return (i);
9517c478bd9Sstevel@tonic-gate }
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate /*
9547c478bd9Sstevel@tonic-gate  * compress inp to outp
9557c478bd9Sstevel@tonic-gate  *
9567c478bd9Sstevel@tonic-gate  * Algorithm:  use open addressing double hashing(no chaining) on the
9577c478bd9Sstevel@tonic-gate  * prefix code / next character combination.  We do a variant of Knuth's
9587c478bd9Sstevel@tonic-gate  * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
9597c478bd9Sstevel@tonic-gate  * secondary probe.  Here, the modular division first probe is gives way
9607c478bd9Sstevel@tonic-gate  * to a faster exclusive-or manipulation.  Also do block compression with
9617c478bd9Sstevel@tonic-gate  * an adaptive reset, whereby the code table is cleared when the compression
9627c478bd9Sstevel@tonic-gate  * ratio decreases, but after the table fills.  The variable-length output
9637c478bd9Sstevel@tonic-gate  * codes are re-sized at this point, and a special CLEAR code is generated
9647c478bd9Sstevel@tonic-gate  * for the decompressor.  Late addition:  construct the table according to
9657c478bd9Sstevel@tonic-gate  * file size for noticeable speed improvement on small files.  Please direct
9667c478bd9Sstevel@tonic-gate  * questions about this implementation to ames!jaw.
9677c478bd9Sstevel@tonic-gate  */
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate static void
compress()9707c478bd9Sstevel@tonic-gate compress()
9717c478bd9Sstevel@tonic-gate {
9727c478bd9Sstevel@tonic-gate 	long fcode;
9737c478bd9Sstevel@tonic-gate 	code_int i = 0;
9747c478bd9Sstevel@tonic-gate 	int c;
9757c478bd9Sstevel@tonic-gate 	code_int ent;
9767c478bd9Sstevel@tonic-gate 	int disp;
9777c478bd9Sstevel@tonic-gate 	code_int hsize_reg;
9787c478bd9Sstevel@tonic-gate 	int hshift;
9797c478bd9Sstevel@tonic-gate 	int probecnt;
9807c478bd9Sstevel@tonic-gate 	count_long in_count;
9817c478bd9Sstevel@tonic-gate 	uint32_t inchi, inclo;
9827c478bd9Sstevel@tonic-gate 	int maxbits_reg;
9837c478bd9Sstevel@tonic-gate 	FILE *fin = inp;
9847c478bd9Sstevel@tonic-gate #ifdef DEBUG
9857c478bd9Sstevel@tonic-gate 	count_long out_count = 0;
9867c478bd9Sstevel@tonic-gate #endif
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 	if (nomagic == 0) {
9897c478bd9Sstevel@tonic-gate 		if ((putc(magic_header[0], outp) == EOF ||
9907c478bd9Sstevel@tonic-gate 		    putc(magic_header[1], outp) == EOF ||
9917c478bd9Sstevel@tonic-gate 		    putc((char)(maxbits | block_compress),
9923d63ea05Sas 		    outp) == EOF) &&
9937c478bd9Sstevel@tonic-gate 		    ferror(outp)) {
9947c478bd9Sstevel@tonic-gate 			ioerror();
9957c478bd9Sstevel@tonic-gate 		}
9967c478bd9Sstevel@tonic-gate 	}
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	offset = 0;
9997c478bd9Sstevel@tonic-gate 	bytes_out = 3;		/* includes 3-byte header mojo */
10007c478bd9Sstevel@tonic-gate 	clear_flg = 0;
10017c478bd9Sstevel@tonic-gate 	ratio = 0;
10027c478bd9Sstevel@tonic-gate 	in_count = 1;
10037c478bd9Sstevel@tonic-gate 	inchi = 0;
10047c478bd9Sstevel@tonic-gate 	inclo = 1;
10057c478bd9Sstevel@tonic-gate 	checkpoint = CHECK_GAP;
10067c478bd9Sstevel@tonic-gate 	maxcode = MAXCODE(n_bits = INIT_BITS);
10077c478bd9Sstevel@tonic-gate 	free_ent = ((block_compress) ? FIRST : 256);
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	if ((ent = getc(fin)) == EOF && ferror(fin)) {
10107c478bd9Sstevel@tonic-gate 		ioerror();
10117c478bd9Sstevel@tonic-gate 	}
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	hshift = 0;
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	for (fcode = (long)hsize;  fcode < 65536L; fcode *= 2L)
10167c478bd9Sstevel@tonic-gate 		hshift++;
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	hshift = 8 - hshift;		/* set hash code range bound */
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	hsize_reg = hsize;
10217c478bd9Sstevel@tonic-gate 	maxbits_reg = maxbits;
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	cl_hash((count_int) hsize_reg);		/* clear hash table */
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	while ((c = getc(fin)) != EOF) {
10267c478bd9Sstevel@tonic-gate 		if (++inclo == 0)
10277c478bd9Sstevel@tonic-gate 			inchi++;
10287c478bd9Sstevel@tonic-gate 		fcode = (long)(((long)c << maxbits_reg) + ent);
10297c478bd9Sstevel@tonic-gate 		i = ((c << hshift) ^ ent);	/* xor hashing */
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 		if ((unsigned int)i >= hsize_reg)
10327c478bd9Sstevel@tonic-gate 			i = adjusti(i, hsize_reg);
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 		if (htabof(i) == fcode) {
10357c478bd9Sstevel@tonic-gate 			ent = codetabof(i);
10367c478bd9Sstevel@tonic-gate 			continue;
10377c478bd9Sstevel@tonic-gate 		} else if ((long)htabof(i) < 0) {
10387c478bd9Sstevel@tonic-gate 			/* empty slot */
10397c478bd9Sstevel@tonic-gate 			goto nomatch;
10407c478bd9Sstevel@tonic-gate 		}
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 		/* secondary hash (after G. Knott) */
10437c478bd9Sstevel@tonic-gate 		disp = hsize_reg - i;
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 		if (i == 0) {
10467c478bd9Sstevel@tonic-gate 			disp = 1;
10477c478bd9Sstevel@tonic-gate 		}
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 		probecnt = 0;
10507c478bd9Sstevel@tonic-gate 	probe:
10517c478bd9Sstevel@tonic-gate 		if (++probecnt > hsize_reg)
10527c478bd9Sstevel@tonic-gate 			cinterr(hshift);
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 		if ((i -= disp) < 0) {
10557c478bd9Sstevel@tonic-gate 			while (i < 0)
10567c478bd9Sstevel@tonic-gate 				i += hsize_reg;
10577c478bd9Sstevel@tonic-gate 		}
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 		if (htabof(i) == fcode) {
10607c478bd9Sstevel@tonic-gate 			ent = codetabof(i);
10617c478bd9Sstevel@tonic-gate 			continue;
10627c478bd9Sstevel@tonic-gate 		}
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 		if ((long)htabof(i) > 0) {
10657c478bd9Sstevel@tonic-gate 			goto probe;
10667c478bd9Sstevel@tonic-gate 		}
10677c478bd9Sstevel@tonic-gate 	nomatch:
10687c478bd9Sstevel@tonic-gate 		output((code_int) ent);
10697c478bd9Sstevel@tonic-gate #ifdef DEBUG
10707c478bd9Sstevel@tonic-gate 		out_count++;
10717c478bd9Sstevel@tonic-gate #endif
10727c478bd9Sstevel@tonic-gate 		ent = c;
10737c478bd9Sstevel@tonic-gate 		if (free_ent < maxmaxcode) {
10747c478bd9Sstevel@tonic-gate 			codetabof(i) = free_ent++;
10757c478bd9Sstevel@tonic-gate 			/* code -> hashtable */
10767c478bd9Sstevel@tonic-gate 			htabof(i) = fcode;
10777c478bd9Sstevel@tonic-gate 		} else {
10787c478bd9Sstevel@tonic-gate 			in_count = ((long long)inchi<<32|inclo);
10797c478bd9Sstevel@tonic-gate 			if ((count_long)in_count >=
10807c478bd9Sstevel@tonic-gate 			    (count_long)checkpoint && block_compress) {
10817c478bd9Sstevel@tonic-gate 				cl_block(in_count);
10827c478bd9Sstevel@tonic-gate 			}
10837c478bd9Sstevel@tonic-gate 		}
10847c478bd9Sstevel@tonic-gate 	}
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	in_count = ((long long)inchi<<32|inclo);
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	if (ferror(fin) != 0) {
10897c478bd9Sstevel@tonic-gate 		ioerror();
10907c478bd9Sstevel@tonic-gate 	}
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	/*
10937c478bd9Sstevel@tonic-gate 	 * Put out the final code.
10947c478bd9Sstevel@tonic-gate 	 */
10957c478bd9Sstevel@tonic-gate 	output((code_int)ent);
10967c478bd9Sstevel@tonic-gate #ifdef DEBUG
10977c478bd9Sstevel@tonic-gate 	out_count++;
10987c478bd9Sstevel@tonic-gate #endif
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	output((code_int)-1);
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 	/*
11037c478bd9Sstevel@tonic-gate 	 * Print out stats on stderr
11047c478bd9Sstevel@tonic-gate 	 */
11057c478bd9Sstevel@tonic-gate 	if (!quiet) {
11067c478bd9Sstevel@tonic-gate #ifdef DEBUG
11077c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
11083d63ea05Sas 		    "%lld chars in, %lld codes (%lld bytes) out, "
11093d63ea05Sas 		    "compression factor: ",
11103d63ea05Sas 		    (count_long)in_count, (count_long)out_count,
11113d63ea05Sas 		    (count_long) bytes_out);
11127c478bd9Sstevel@tonic-gate 		prratio(stderr, (count_long)in_count,
11133d63ea05Sas 		    (count_long)bytes_out);
11147c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
11157c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\tCompression as in compact: ");
11167c478bd9Sstevel@tonic-gate 		prratio(stderr,
11173d63ea05Sas 		    (count_long)in_count-(count_long)bytes_out,
11183d63ea05Sas 		    (count_long)in_count);
11197c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
11207c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
11213d63ea05Sas 		    "\tLargest code (of last block) was %d"
11223d63ea05Sas 		    " (%d bits)\n",
11233d63ea05Sas 		    free_ent - 1, n_bits);
11247c478bd9Sstevel@tonic-gate #else /* !DEBUG */
11257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Compression: "));
11267c478bd9Sstevel@tonic-gate 		prratio(stderr,
11273d63ea05Sas 		    (count_long)in_count-(count_long)bytes_out,
11283d63ea05Sas 		    (count_long)in_count);
11297c478bd9Sstevel@tonic-gate #endif /* DEBUG */
11307c478bd9Sstevel@tonic-gate 	}
11317c478bd9Sstevel@tonic-gate 	/* report if no savings */
11327c478bd9Sstevel@tonic-gate 	if ((count_long)bytes_out > (count_long)in_count) {
11337c478bd9Sstevel@tonic-gate 		didnt_shrink = 1;
11347c478bd9Sstevel@tonic-gate 	}
11357c478bd9Sstevel@tonic-gate }
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate /*
11387c478bd9Sstevel@tonic-gate  * **************************************************************
11397c478bd9Sstevel@tonic-gate  * TAG(output)
11407c478bd9Sstevel@tonic-gate  *
11417c478bd9Sstevel@tonic-gate  * Output the given code.
11427c478bd9Sstevel@tonic-gate  * Inputs:
11437c478bd9Sstevel@tonic-gate  * 	code:	A n_bits-bit integer.  If == -1, then EOF.  This assumes
11447c478bd9Sstevel@tonic-gate  *		that n_bits = < (long)wordsize - 1.
11457c478bd9Sstevel@tonic-gate  * Outputs:
11467c478bd9Sstevel@tonic-gate  * 	Outputs code to the file.
11477c478bd9Sstevel@tonic-gate  * Assumptions:
11487c478bd9Sstevel@tonic-gate  *	Chars are 8 bits long.
11497c478bd9Sstevel@tonic-gate  * Algorithm:
11507c478bd9Sstevel@tonic-gate  * 	Maintain a BITS character long buffer(so that 8 codes will
11517c478bd9Sstevel@tonic-gate  * fit in it exactly).  Use the VAX insv instruction to insert each
11527c478bd9Sstevel@tonic-gate  * code in turn.  When the buffer fills up empty it and start over.
11537c478bd9Sstevel@tonic-gate  */
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate static void
output(code_int code)11567c478bd9Sstevel@tonic-gate output(code_int code)
11577c478bd9Sstevel@tonic-gate {
11587c478bd9Sstevel@tonic-gate #ifdef DEBUG
11597c478bd9Sstevel@tonic-gate 	static int col = 0;
11607c478bd9Sstevel@tonic-gate #endif /* DEBUG */
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 	int r_off = offset, bits = n_bits;
11637c478bd9Sstevel@tonic-gate 	char *bp = buf;
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate #ifdef DEBUG
11667c478bd9Sstevel@tonic-gate 	if (verbose)
11677c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%5d%c", code,
11683d63ea05Sas 		    (col += 6) >= 74 ? (col = 0, '\n') : ' ');
11697c478bd9Sstevel@tonic-gate #endif /* DEBUG */
11707c478bd9Sstevel@tonic-gate 	if (code >= 0) {
11717c478bd9Sstevel@tonic-gate 		/*
11727c478bd9Sstevel@tonic-gate 		 * byte/bit numbering on the VAX is simulated
11737c478bd9Sstevel@tonic-gate 		 * by the following code
11747c478bd9Sstevel@tonic-gate 		 */
11757c478bd9Sstevel@tonic-gate 		/*
11767c478bd9Sstevel@tonic-gate 		 * Get to the first byte.
11777c478bd9Sstevel@tonic-gate 		 */
11787c478bd9Sstevel@tonic-gate 		bp += (r_off >> 3);
11797c478bd9Sstevel@tonic-gate 		r_off &= 7;
11807c478bd9Sstevel@tonic-gate 		/*
11817c478bd9Sstevel@tonic-gate 		 * Since code is always >= 8 bits, only need to mask the first
11827c478bd9Sstevel@tonic-gate 		 * hunk on the left.
11837c478bd9Sstevel@tonic-gate 		 */
11847c478bd9Sstevel@tonic-gate 		*bp = (*bp & rmask[r_off]) | (code << r_off) & lmask[r_off];
11857c478bd9Sstevel@tonic-gate 		bp++;
11867c478bd9Sstevel@tonic-gate 		bits -= (8 - r_off);
11877c478bd9Sstevel@tonic-gate 		code >>= 8 - r_off;
11887c478bd9Sstevel@tonic-gate 		/*
11897c478bd9Sstevel@tonic-gate 		 * Get any 8 bit parts in the middle (<=1 for up to 16
11907c478bd9Sstevel@tonic-gate 		 * bits).
11917c478bd9Sstevel@tonic-gate 		 */
11927c478bd9Sstevel@tonic-gate 		if (bits >= 8) {
11937c478bd9Sstevel@tonic-gate 			*bp++ = code;
11947c478bd9Sstevel@tonic-gate 			code >>= 8;
11957c478bd9Sstevel@tonic-gate 			bits -= 8;
11967c478bd9Sstevel@tonic-gate 		}
11977c478bd9Sstevel@tonic-gate 		/* Last bits. */
11987c478bd9Sstevel@tonic-gate 		if (bits)
11997c478bd9Sstevel@tonic-gate 			*bp = code;
12007c478bd9Sstevel@tonic-gate 		offset += n_bits;
12017c478bd9Sstevel@tonic-gate 		if (offset == (n_bits << 3)) {
12027c478bd9Sstevel@tonic-gate 			bp = buf;
12037c478bd9Sstevel@tonic-gate 			bits = n_bits;
12047c478bd9Sstevel@tonic-gate 			bytes_out += bits;
12057c478bd9Sstevel@tonic-gate 			do {
12067c478bd9Sstevel@tonic-gate 				if (putc(*bp, outp) == EOF &&
12077c478bd9Sstevel@tonic-gate 				    ferror(outp)) {
12087c478bd9Sstevel@tonic-gate 					ioerror();
12097c478bd9Sstevel@tonic-gate 				}
12107c478bd9Sstevel@tonic-gate 				bp++;
12117c478bd9Sstevel@tonic-gate 			} while (--bits);
12127c478bd9Sstevel@tonic-gate 			offset = 0;
12137c478bd9Sstevel@tonic-gate 		}
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 		/*
12167c478bd9Sstevel@tonic-gate 		 * If the next entry is going to be too big for the code size,
12177c478bd9Sstevel@tonic-gate 		 * then increase it, if possible.
12187c478bd9Sstevel@tonic-gate 		 */
12197c478bd9Sstevel@tonic-gate 		if (free_ent > maxcode || (clear_flg > 0)) {
12207c478bd9Sstevel@tonic-gate 			/*
12217c478bd9Sstevel@tonic-gate 			 * Write the whole buffer, because the input
12227c478bd9Sstevel@tonic-gate 			 * side won't discover the size increase until
12237c478bd9Sstevel@tonic-gate 			 * after it has read it.
12247c478bd9Sstevel@tonic-gate 			 */
12257c478bd9Sstevel@tonic-gate 			if (offset > 0) {
12267c478bd9Sstevel@tonic-gate 				if (fwrite(buf, 1, n_bits, outp) != n_bits) {
12277c478bd9Sstevel@tonic-gate 					longjmp(env, 3);
12287c478bd9Sstevel@tonic-gate 				}
12297c478bd9Sstevel@tonic-gate 				bytes_out += n_bits;
12307c478bd9Sstevel@tonic-gate 			}
12317c478bd9Sstevel@tonic-gate 			offset = 0;
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 			if (clear_flg) {
12347c478bd9Sstevel@tonic-gate 				maxcode = MAXCODE(n_bits = INIT_BITS);
12357c478bd9Sstevel@tonic-gate 				clear_flg = 0;
12367c478bd9Sstevel@tonic-gate 			} else {
12377c478bd9Sstevel@tonic-gate 				n_bits++;
12387c478bd9Sstevel@tonic-gate 				if (n_bits == maxbits)
12397c478bd9Sstevel@tonic-gate 					maxcode = maxmaxcode;
12407c478bd9Sstevel@tonic-gate 				else
12417c478bd9Sstevel@tonic-gate 					maxcode = MAXCODE(n_bits);
12427c478bd9Sstevel@tonic-gate 			}
12437c478bd9Sstevel@tonic-gate #ifdef DEBUG
12447c478bd9Sstevel@tonic-gate 			if (debug) {
12457c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
12463d63ea05Sas 				    "\nChange to %d bits\n", n_bits);
12477c478bd9Sstevel@tonic-gate 				col = 0;
12487c478bd9Sstevel@tonic-gate 			}
12497c478bd9Sstevel@tonic-gate #endif /* DEBUG */
12507c478bd9Sstevel@tonic-gate 		}
12517c478bd9Sstevel@tonic-gate 	} else {
12527c478bd9Sstevel@tonic-gate 		/*
12537c478bd9Sstevel@tonic-gate 		 * At EOF, write the rest of the buffer.
12547c478bd9Sstevel@tonic-gate 		 */
12557c478bd9Sstevel@tonic-gate 		if (offset > 0) {
12567c478bd9Sstevel@tonic-gate 			if (fwrite(buf, 1, (offset + 7) / 8, outp) == 0 &&
12577c478bd9Sstevel@tonic-gate 			    ferror(outp)) {
12587c478bd9Sstevel@tonic-gate 				ioerror();
12597c478bd9Sstevel@tonic-gate 			}
12607c478bd9Sstevel@tonic-gate 			bytes_out += (offset + 7) / 8;
12617c478bd9Sstevel@tonic-gate 		}
12627c478bd9Sstevel@tonic-gate 		offset = 0;
12637c478bd9Sstevel@tonic-gate 		(void) fflush(outp);
12647c478bd9Sstevel@tonic-gate #ifdef DEBUG
12657c478bd9Sstevel@tonic-gate 		if (verbose)
12667c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "\n");
12677c478bd9Sstevel@tonic-gate #endif /* DEBUG */
12687c478bd9Sstevel@tonic-gate 		if (ferror(outp))
12697c478bd9Sstevel@tonic-gate 			ioerror();
12707c478bd9Sstevel@tonic-gate 	}
12717c478bd9Sstevel@tonic-gate }
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate /*
12747c478bd9Sstevel@tonic-gate  * Decompress inp to outp.  This routine adapts to the codes in the
12757c478bd9Sstevel@tonic-gate  * file building the "string" table on-the-fly; requiring no table to
12767c478bd9Sstevel@tonic-gate  * be stored in the compressed file.  The tables used herein are shared
12777c478bd9Sstevel@tonic-gate  * with those of the compress() routine.  See the definitions above.
12787c478bd9Sstevel@tonic-gate  */
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate static void
decompress()12817c478bd9Sstevel@tonic-gate decompress()
12827c478bd9Sstevel@tonic-gate {
12837c478bd9Sstevel@tonic-gate 	char_type *stackp, *stack_lim;
12847c478bd9Sstevel@tonic-gate 	int finchar;
12857c478bd9Sstevel@tonic-gate 	code_int code, oldcode, incode;
12867c478bd9Sstevel@tonic-gate 	FILE *fout = outp;
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate 	/*
12897c478bd9Sstevel@tonic-gate 	 * As above, initialize the first 256 entries in the table.
12907c478bd9Sstevel@tonic-gate 	 */
12917c478bd9Sstevel@tonic-gate 	maxcode = MAXCODE(n_bits = INIT_BITS);
12927c478bd9Sstevel@tonic-gate 	for (code = 255; code >= 0; code--) {
12937c478bd9Sstevel@tonic-gate 		tab_prefixof(code) = 0;
12947c478bd9Sstevel@tonic-gate 		tab_suffixof(code) = (char_type)code;
12957c478bd9Sstevel@tonic-gate 	}
12967c478bd9Sstevel@tonic-gate 	free_ent = ((block_compress) ? FIRST : 256);
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate 	finchar = oldcode = getcode();
12997c478bd9Sstevel@tonic-gate 	if (oldcode == -1)	/* EOF already? */
13007c478bd9Sstevel@tonic-gate 		return;			/* Get out of here */
13017c478bd9Sstevel@tonic-gate 	/* first code must be 8 bits = char */
13027c478bd9Sstevel@tonic-gate 	if (putc((char)finchar, outp) == EOF && ferror(outp)) {
13037c478bd9Sstevel@tonic-gate 		/* Crash if can't write */
13047c478bd9Sstevel@tonic-gate 		ioerror();
13057c478bd9Sstevel@tonic-gate 	}
13067c478bd9Sstevel@tonic-gate 	stackp = de_stack;
13077c478bd9Sstevel@tonic-gate 	stack_lim = stack_max;
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate 	while ((code = getcode()) > -1) {
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 		if ((code == CLEAR) && block_compress) {
13127c478bd9Sstevel@tonic-gate 			for (code = 255; code >= 0; code--)
1313*bc54f855SJohn Levon 				tab_prefixof(code) = 0;
13147c478bd9Sstevel@tonic-gate 			clear_flg = 1;
13157c478bd9Sstevel@tonic-gate 			free_ent = FIRST - 1;
13167c478bd9Sstevel@tonic-gate 			if ((code = getcode()) == -1)	/* O, untimely death! */
13177c478bd9Sstevel@tonic-gate 				break;
13187c478bd9Sstevel@tonic-gate 		}
13197c478bd9Sstevel@tonic-gate 		incode = code;
13207c478bd9Sstevel@tonic-gate 		/*
13217c478bd9Sstevel@tonic-gate 		 * Special case for KwKwK string.
13227c478bd9Sstevel@tonic-gate 		 */
13237c478bd9Sstevel@tonic-gate 		if (code >= free_ent) {
13247c478bd9Sstevel@tonic-gate 			if (stackp < stack_lim) {
13257c478bd9Sstevel@tonic-gate 				*stackp++ = (char_type) finchar;
13267c478bd9Sstevel@tonic-gate 				code = oldcode;
13277c478bd9Sstevel@tonic-gate 			} else {
13287c478bd9Sstevel@tonic-gate 				/* badness */
13297c478bd9Sstevel@tonic-gate 				longjmp(env, 2);
13307c478bd9Sstevel@tonic-gate 			}
13317c478bd9Sstevel@tonic-gate 		}
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 		/*
13347c478bd9Sstevel@tonic-gate 		 * Generate output characters in reverse order
13357c478bd9Sstevel@tonic-gate 		 */
13367c478bd9Sstevel@tonic-gate 		while (code >= 256) {
13377c478bd9Sstevel@tonic-gate 			if (stackp < stack_lim) {
13387c478bd9Sstevel@tonic-gate 				*stackp++ = tab_suffixof(code);
13397c478bd9Sstevel@tonic-gate 				code = tab_prefixof(code);
13407c478bd9Sstevel@tonic-gate 			} else {
13417c478bd9Sstevel@tonic-gate 				/* badness */
13427c478bd9Sstevel@tonic-gate 				longjmp(env, 2);
13437c478bd9Sstevel@tonic-gate 			}
13447c478bd9Sstevel@tonic-gate 		}
13457c478bd9Sstevel@tonic-gate 		*stackp++ = finchar = tab_suffixof(code);
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 		/*
13487c478bd9Sstevel@tonic-gate 		 * And put them out in forward order
13497c478bd9Sstevel@tonic-gate 		 */
13507c478bd9Sstevel@tonic-gate 		do {
13517c478bd9Sstevel@tonic-gate 			stackp--;
13527c478bd9Sstevel@tonic-gate 			(void) putc(*stackp, fout);
13537c478bd9Sstevel@tonic-gate 		} while (stackp > de_stack);
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 		if (ferror(fout))
13567c478bd9Sstevel@tonic-gate 			ioerror();
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 		/*
13597c478bd9Sstevel@tonic-gate 		 * Generate the new entry.
13607c478bd9Sstevel@tonic-gate 		 */
13617c478bd9Sstevel@tonic-gate 		if ((code = free_ent) < maxmaxcode) {
13627c478bd9Sstevel@tonic-gate 			tab_prefixof(code) = (unsigned short) oldcode;
13637c478bd9Sstevel@tonic-gate 			tab_suffixof(code) = (char_type) finchar;
13647c478bd9Sstevel@tonic-gate 			free_ent = code+1;
13657c478bd9Sstevel@tonic-gate 		}
13667c478bd9Sstevel@tonic-gate 		/*
13677c478bd9Sstevel@tonic-gate 		 * Remember previous code.
13687c478bd9Sstevel@tonic-gate 		 */
13697c478bd9Sstevel@tonic-gate 		oldcode = incode;
13707c478bd9Sstevel@tonic-gate 	}
13717c478bd9Sstevel@tonic-gate 	(void) fflush(outp);
13727c478bd9Sstevel@tonic-gate 	if (ferror(outp))
13737c478bd9Sstevel@tonic-gate 		ioerror();
13747c478bd9Sstevel@tonic-gate }
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate /*
13777c478bd9Sstevel@tonic-gate  * **************************************************************
13787c478bd9Sstevel@tonic-gate  * TAG( getcode )
13797c478bd9Sstevel@tonic-gate  *
13807c478bd9Sstevel@tonic-gate  * Read one code from the standard input.  If EOF, return -1.
13817c478bd9Sstevel@tonic-gate  * Inputs:
13827c478bd9Sstevel@tonic-gate  * 	inp
13837c478bd9Sstevel@tonic-gate  * Outputs:
13847c478bd9Sstevel@tonic-gate  * 	code or -1 is returned.
13857c478bd9Sstevel@tonic-gate  */
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate code_int
getcode()13887c478bd9Sstevel@tonic-gate getcode() {
13897c478bd9Sstevel@tonic-gate 	code_int code;
13907c478bd9Sstevel@tonic-gate 	static int offset = 0, size = 0;
13917c478bd9Sstevel@tonic-gate 	static char_type buf[BITS];
13927c478bd9Sstevel@tonic-gate 	int r_off, bits;
13937c478bd9Sstevel@tonic-gate 	char_type *bp = buf;
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 	if (clear_flg > 0 || offset >= size || free_ent > maxcode) {
13967c478bd9Sstevel@tonic-gate 		/*
13977c478bd9Sstevel@tonic-gate 		 * If the next entry will be too big for the current code
13987c478bd9Sstevel@tonic-gate 		 * size, then we must increase the size.  This implies reading
13997c478bd9Sstevel@tonic-gate 		 * a new buffer full, too.
14007c478bd9Sstevel@tonic-gate 		 */
14017c478bd9Sstevel@tonic-gate 		if (free_ent > maxcode) {
14027c478bd9Sstevel@tonic-gate 			n_bits++;
14037c478bd9Sstevel@tonic-gate 			if (n_bits == maxbits)
14047c478bd9Sstevel@tonic-gate 				/* won't get any bigger now */
14057c478bd9Sstevel@tonic-gate 				maxcode = maxmaxcode;
14067c478bd9Sstevel@tonic-gate 			else
14077c478bd9Sstevel@tonic-gate 				maxcode = MAXCODE(n_bits);
14087c478bd9Sstevel@tonic-gate 		}
14097c478bd9Sstevel@tonic-gate 		if (clear_flg > 0) {
14107c478bd9Sstevel@tonic-gate 			maxcode = MAXCODE(n_bits = INIT_BITS);
14117c478bd9Sstevel@tonic-gate 			clear_flg = 0;
14127c478bd9Sstevel@tonic-gate 		}
14137c478bd9Sstevel@tonic-gate 		size = fread(buf, 1, n_bits, inp);
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 		if (size <= 0) {
14167c478bd9Sstevel@tonic-gate 			if (feof(inp)) {
14177c478bd9Sstevel@tonic-gate 				/* end of file */
14187c478bd9Sstevel@tonic-gate 				return (-1);
14197c478bd9Sstevel@tonic-gate 			} else if (ferror(inp)) {
14207c478bd9Sstevel@tonic-gate 				ioerror();
14217c478bd9Sstevel@tonic-gate 			}
14227c478bd9Sstevel@tonic-gate 		}
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate 		offset = 0;
14257c478bd9Sstevel@tonic-gate 		/* Round size down to integral number of codes */
14267c478bd9Sstevel@tonic-gate 		size = (size << 3) - (n_bits - 1);
14277c478bd9Sstevel@tonic-gate 	}
14287c478bd9Sstevel@tonic-gate 	r_off = offset;
14297c478bd9Sstevel@tonic-gate 	bits = n_bits;
14307c478bd9Sstevel@tonic-gate 	/*
14317c478bd9Sstevel@tonic-gate 	 * Get to the first byte.
14327c478bd9Sstevel@tonic-gate 	 */
14337c478bd9Sstevel@tonic-gate 	bp += (r_off >> 3);
14347c478bd9Sstevel@tonic-gate 	r_off &= 7;
14357c478bd9Sstevel@tonic-gate 	/* Get first part (low order bits) */
14367c478bd9Sstevel@tonic-gate 	code = (*bp++ >> r_off);
14377c478bd9Sstevel@tonic-gate 	bits -= (8 - r_off);
14387c478bd9Sstevel@tonic-gate 	r_off = 8 - r_off;		/* now, offset into code word */
14397c478bd9Sstevel@tonic-gate 	/* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
14407c478bd9Sstevel@tonic-gate 	if (bits >= 8) {
14417c478bd9Sstevel@tonic-gate 		code |= *bp++ << r_off;
14427c478bd9Sstevel@tonic-gate 		r_off += 8;
14437c478bd9Sstevel@tonic-gate 		bits -= 8;
14447c478bd9Sstevel@tonic-gate 	}
14457c478bd9Sstevel@tonic-gate 	/* high order bits. */
14467c478bd9Sstevel@tonic-gate 	code |= (*bp & rmask[bits]) << r_off;
14477c478bd9Sstevel@tonic-gate 	offset += n_bits;
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	return (code);
14507c478bd9Sstevel@tonic-gate }
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate #ifdef DEBUG
14537c478bd9Sstevel@tonic-gate static void
printcodes()14547c478bd9Sstevel@tonic-gate printcodes()
14557c478bd9Sstevel@tonic-gate {
14567c478bd9Sstevel@tonic-gate 	/*
14577c478bd9Sstevel@tonic-gate 	 * Just print out codes from input file.  For debugging.
14587c478bd9Sstevel@tonic-gate 	 */
14597c478bd9Sstevel@tonic-gate 	code_int code;
14607c478bd9Sstevel@tonic-gate 	int col = 0, bits;
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate 	bits = n_bits = INIT_BITS;
14637c478bd9Sstevel@tonic-gate 	maxcode = MAXCODE(n_bits);
14647c478bd9Sstevel@tonic-gate 	free_ent = ((block_compress) ? FIRST : 256);
14657c478bd9Sstevel@tonic-gate 	while ((code = getcode()) >= 0) {
14667c478bd9Sstevel@tonic-gate 		if ((code == CLEAR) && block_compress) {
14677c478bd9Sstevel@tonic-gate 			free_ent = FIRST - 1;
14687c478bd9Sstevel@tonic-gate 			clear_flg = 1;
14697c478bd9Sstevel@tonic-gate 		} else if (free_ent < maxmaxcode)
14707c478bd9Sstevel@tonic-gate 			free_ent++;
14717c478bd9Sstevel@tonic-gate 		if (bits != n_bits) {
14727c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "\nChange to %d bits\n", n_bits);
14737c478bd9Sstevel@tonic-gate 			bits = n_bits;
14747c478bd9Sstevel@tonic-gate 			col = 0;
14757c478bd9Sstevel@tonic-gate 		}
14767c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%5d%c",
14773d63ea05Sas 		    code, (col += 6) >= 74 ? (col = 0, '\n') : ' ');
14787c478bd9Sstevel@tonic-gate 	}
14797c478bd9Sstevel@tonic-gate 	(void) putc('\n', stderr);
14807c478bd9Sstevel@tonic-gate }
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate #endif /* DEBUG */
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate #ifdef DEBUG
14857c478bd9Sstevel@tonic-gate static void
dump_tab()14867c478bd9Sstevel@tonic-gate dump_tab()	/* dump string table */
14877c478bd9Sstevel@tonic-gate {
14887c478bd9Sstevel@tonic-gate 	int i, first;
14897c478bd9Sstevel@tonic-gate 	int ent;
14907c478bd9Sstevel@tonic-gate 	int stack_top = STACK_SIZE;
14917c478bd9Sstevel@tonic-gate 	int c;
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate 	if (do_decomp == 0) {	/* compressing */
14947c478bd9Sstevel@tonic-gate 		int flag = 1;
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate 		for (i = 0; i < hsize; i++) {	/* build sort pointers */
14977c478bd9Sstevel@tonic-gate 			if ((long)htabof(i) >= 0) {
14987c478bd9Sstevel@tonic-gate 				sorttab[codetabof(i)] = i;
14997c478bd9Sstevel@tonic-gate 			}
15007c478bd9Sstevel@tonic-gate 		}
15017c478bd9Sstevel@tonic-gate 		first = block_compress ? FIRST : 256;
15027c478bd9Sstevel@tonic-gate 		for (i = first; i < free_ent; i++) {
15037c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%5d: \"", i);
15047c478bd9Sstevel@tonic-gate 			de_stack[--stack_top] = '\n';
15057c478bd9Sstevel@tonic-gate 			de_stack[--stack_top] = '"';
15067c478bd9Sstevel@tonic-gate 			stack_top =
15073d63ea05Sas 			    in_stack((htabof(sorttab[i]) >> maxbits) & 0xff,
15083d63ea05Sas 			    stack_top);
15097c478bd9Sstevel@tonic-gate 			for (ent = htabof(sorttab[i]) & ((1 << maxbits) -1);
15103d63ea05Sas 			    ent > 256;
15113d63ea05Sas 			    ent = htabof(sorttab[ent]) & ((1<<maxbits)-1)) {
15127c478bd9Sstevel@tonic-gate 				stack_top = in_stack(
15133d63ea05Sas 				    htabof(sorttab[ent]) >> maxbits,
15143d63ea05Sas 				    stack_top);
15157c478bd9Sstevel@tonic-gate 			}
15167c478bd9Sstevel@tonic-gate 			stack_top = in_stack(ent, stack_top);
15177c478bd9Sstevel@tonic-gate 			(void) fwrite(&de_stack[stack_top], 1,
15183d63ea05Sas 			    STACK_SIZE - stack_top, stderr);
15197c478bd9Sstevel@tonic-gate 			stack_top = STACK_SIZE;
15207c478bd9Sstevel@tonic-gate 		}
15217c478bd9Sstevel@tonic-gate 	} else if (!debug) {	/* decompressing */
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 		for (i = 0; i < free_ent; i++) {
15247c478bd9Sstevel@tonic-gate 			ent = i;
15257c478bd9Sstevel@tonic-gate 			c = tab_suffixof(ent);
15267c478bd9Sstevel@tonic-gate 			if (isascii(c) && isprint(c))
15277c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "%5d: %5d/'%c'  \"",
15283d63ea05Sas 				    ent, tab_prefixof(ent), c);
15297c478bd9Sstevel@tonic-gate 			else
15307c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "%5d: %5d/\\%03o \"",
15313d63ea05Sas 				    ent, tab_prefixof(ent), c);
15327c478bd9Sstevel@tonic-gate 			de_stack[--stack_top] = '\n';
15337c478bd9Sstevel@tonic-gate 			de_stack[--stack_top] = '"';
15347c478bd9Sstevel@tonic-gate 			for (; ent != NULL;
15353d63ea05Sas 			    ent = (ent >= FIRST ? tab_prefixof(ent) :
15363d63ea05Sas 			    NULL)) {
15377c478bd9Sstevel@tonic-gate 				stack_top = in_stack(tab_suffixof(ent),
15383d63ea05Sas 				    stack_top);
15397c478bd9Sstevel@tonic-gate 			}
15407c478bd9Sstevel@tonic-gate 			(void) fwrite(&de_stack[stack_top], 1,
15413d63ea05Sas 			    STACK_SIZE - stack_top, stderr);
15427c478bd9Sstevel@tonic-gate 			stack_top = STACK_SIZE;
15437c478bd9Sstevel@tonic-gate 		}
15447c478bd9Sstevel@tonic-gate 	}
15457c478bd9Sstevel@tonic-gate }
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate #endif /* DEBUG */
15487c478bd9Sstevel@tonic-gate #ifdef DEBUG
15497c478bd9Sstevel@tonic-gate static int
in_stack(int c,int stack_top)15507c478bd9Sstevel@tonic-gate in_stack(int c, int stack_top)
15517c478bd9Sstevel@tonic-gate {
15527c478bd9Sstevel@tonic-gate 	if ((isascii(c) && isprint(c) && c != '\\') || c == ' ') {
15537c478bd9Sstevel@tonic-gate 		de_stack[--stack_top] = c;
15547c478bd9Sstevel@tonic-gate 	} else {
15557c478bd9Sstevel@tonic-gate 		switch (c) {
15567c478bd9Sstevel@tonic-gate 		case '\n': de_stack[--stack_top] = 'n'; break;
15577c478bd9Sstevel@tonic-gate 		case '\t': de_stack[--stack_top] = 't'; break;
15587c478bd9Sstevel@tonic-gate 		case '\b': de_stack[--stack_top] = 'b'; break;
15597c478bd9Sstevel@tonic-gate 		case '\f': de_stack[--stack_top] = 'f'; break;
15607c478bd9Sstevel@tonic-gate 		case '\r': de_stack[--stack_top] = 'r'; break;
15617c478bd9Sstevel@tonic-gate 		case '\\': de_stack[--stack_top] = '\\'; break;
15627c478bd9Sstevel@tonic-gate 		default:
15637c478bd9Sstevel@tonic-gate 			de_stack[--stack_top] = '0' + c % 8;
15647c478bd9Sstevel@tonic-gate 			de_stack[--stack_top] = '0' + (c / 8) % 8;
15657c478bd9Sstevel@tonic-gate 			de_stack[--stack_top] = '0' + c / 64;
15667c478bd9Sstevel@tonic-gate 			break;
15677c478bd9Sstevel@tonic-gate 		}
15687c478bd9Sstevel@tonic-gate 		de_stack[--stack_top] = '\\';
15697c478bd9Sstevel@tonic-gate 	}
15707c478bd9Sstevel@tonic-gate 	return (stack_top);
15717c478bd9Sstevel@tonic-gate }
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate #endif /* DEBUG */
15747c478bd9Sstevel@tonic-gate static void
ioerror()15757c478bd9Sstevel@tonic-gate ioerror()
15767c478bd9Sstevel@tonic-gate {
15777c478bd9Sstevel@tonic-gate 	longjmp(env, 1);
15787c478bd9Sstevel@tonic-gate }
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate static void
copystat(char * ifname,struct stat * ifstat,char * ofname)15817c478bd9Sstevel@tonic-gate copystat(char *ifname, struct stat *ifstat, char *ofname)
15827c478bd9Sstevel@tonic-gate {
15837c478bd9Sstevel@tonic-gate 	mode_t mode;
15847c478bd9Sstevel@tonic-gate 	struct utimbuf timep;
1585f0a2d94fSmarks 	acl_t *aclp = NULL;
1586fa9e4066Sahrens 	int error;
1587da6c28aaSamw 	int sattr_exist = 0;
1588da6c28aaSamw 	int xattr_exist = 0;
1589da6c28aaSamw 
1590da6c28aaSamw 	if (pathconf(ifname, _PC_XATTR_EXISTS) == 1)
1591da6c28aaSamw 		xattr_exist = 1;
1592da6c28aaSamw 	if (saflg && sysattr_support(ifname, _PC_SATTR_EXISTS) == 1)
1593da6c28aaSamw 		sattr_exist = 1;
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate 	if (fclose(outp)) {
15967c478bd9Sstevel@tonic-gate 		perror(ofname);
15977c478bd9Sstevel@tonic-gate 		if (!quiet) {
15987c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(" -- file unchanged"));
15997c478bd9Sstevel@tonic-gate 			newline_needed = 1;
16007c478bd9Sstevel@tonic-gate 		}
16017c478bd9Sstevel@tonic-gate 		perm_stat = 1;
16027c478bd9Sstevel@tonic-gate 	} else if (ifstat == NULL) {	/* Get stat on input file */
16037c478bd9Sstevel@tonic-gate 		perror(ifname);
16047c478bd9Sstevel@tonic-gate 		return;
16057c478bd9Sstevel@tonic-gate 	} else if ((ifstat->st_mode &
16063d63ea05Sas 	    S_IFMT /* 0170000 */) != S_IFREG /* 0100000 */) {
16077c478bd9Sstevel@tonic-gate 		if (quiet) {
16087c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: ", ifname);
16097c478bd9Sstevel@tonic-gate 		}
16107c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
16113d63ea05Sas 		    " -- not a regular file: unchanged"));
16127c478bd9Sstevel@tonic-gate 		newline_needed = 1;
16137c478bd9Sstevel@tonic-gate 		perm_stat = 1;
16147c478bd9Sstevel@tonic-gate 	} else if (ifstat->st_nlink > 1) {
16157c478bd9Sstevel@tonic-gate 		if (quiet) {
16167c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: ", ifname);
16177c478bd9Sstevel@tonic-gate 		}
16187c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
16193d63ea05Sas 		    " -- has %d other links: unchanged"),
16203d63ea05Sas 		    (uint_t)ifstat->st_nlink - 1);
16217c478bd9Sstevel@tonic-gate 		newline_needed = 1;
16227c478bd9Sstevel@tonic-gate 		perm_stat = 1;
16237c478bd9Sstevel@tonic-gate 	} else if (didnt_shrink && !force) {
16247c478bd9Sstevel@tonic-gate 		/* No compression: remove file.Z */
16257c478bd9Sstevel@tonic-gate 		if (!quiet) {
16267c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
16273d63ea05Sas 			    " -- file unchanged"));
16287c478bd9Sstevel@tonic-gate 			newline_needed = 1;
16297c478bd9Sstevel@tonic-gate 		}
1630da6c28aaSamw 	} else 	if ((xattr_exist || sattr_exist) &&
1631da6c28aaSamw 	    (mv_xattrs(progname, ifname, ofname, sattr_exist, 0)
1632da6c28aaSamw 	    != 0)) {
16337c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
1634da6c28aaSamw 		    "%s: -- cannot preserve extended attributes or "
1635da6c28aaSamw 		    "system attributes, file unchanged"), ifname);
16367c478bd9Sstevel@tonic-gate 		newline_needed = 1;
16377c478bd9Sstevel@tonic-gate 		/* Move attributes back ... */
1638da6c28aaSamw 		xattr_exist = 0;
1639da6c28aaSamw 		sattr_exist = 0;
1640da6c28aaSamw 		if (pathconf(ofname, _PC_XATTR_EXISTS) == 1)
1641da6c28aaSamw 			xattr_exist = 1;
1642da6c28aaSamw 		if (saflg && sysattr_support(ofname, _PC_SATTR_EXISTS) == 1)
1643da6c28aaSamw 			sattr_exist = 1;
1644da6c28aaSamw 		if (sattr_exist || xattr_exist)
1645da6c28aaSamw 			(void) mv_xattrs(progname, ofname, ifname,
1646da6c28aaSamw 			    sattr_exist, 1);
16477c478bd9Sstevel@tonic-gate 		perm_stat = 1;
1648da6c28aaSamw 	} else { /* ***** Successful Compression ***** */
16497c478bd9Sstevel@tonic-gate 		mode = ifstat->st_mode & 07777;
1650da6c28aaSamw 		if (chmod(ofname, mode)) {	 /* Copy modes */
1651da6c28aaSamw 			if (errno == EPERM) {
1652da6c28aaSamw 				(void) fprintf(stderr,
1653da6c28aaSamw 				    gettext("failed to chmod %s"
1654da6c28aaSamw 				    "- permisssion denied\n"), ofname);
1655da6c28aaSamw 			}
16567c478bd9Sstevel@tonic-gate 			perror(ofname);
1657da6c28aaSamw 		}
1658fa9e4066Sahrens 		error = acl_get(ifname, ACL_NO_TRIVIAL, &aclp);
1659fa9e4066Sahrens 		if (error != 0) {
16607c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
1661fa9e4066Sahrens 			    "%s: failed to retrieve acl : %s\n"),
1662fa9e4066Sahrens 			    ifname, acl_strerror(error));
16637c478bd9Sstevel@tonic-gate 			perm_stat = 1;
16647c478bd9Sstevel@tonic-gate 		}
1665fa9e4066Sahrens 		if (aclp && (acl_set(ofname, aclp) < 0)) {
1666da6c28aaSamw 			(void) fprintf(stderr,
1667da6c28aaSamw 			    gettext("%s: failed to set acl "
1668fa9e4066Sahrens 			    "entries\n"), ofname);
1669fa9e4066Sahrens 			perm_stat = 1;
16707c478bd9Sstevel@tonic-gate 		}
1671f0a2d94fSmarks 		if (aclp) {
1672fa9e4066Sahrens 			acl_free(aclp);
1673f0a2d94fSmarks 			aclp = NULL;
1674f0a2d94fSmarks 		}
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 		/* Copy ownership */
16777c478bd9Sstevel@tonic-gate 		(void) chown(ofname, ifstat->st_uid, ifstat->st_gid);
16787c478bd9Sstevel@tonic-gate 		timep.actime = ifstat->st_atime;
16797c478bd9Sstevel@tonic-gate 		timep.modtime = ifstat->st_mtime;
16807c478bd9Sstevel@tonic-gate 		/* Update last accessed and modified times */
16817c478bd9Sstevel@tonic-gate 		(void) utime(ofname, &timep);
1682da6c28aaSamw 		if (unlink(ifname)) { /* Remove input file */
1683da6c28aaSamw 			if (errno == EPERM) {
1684da6c28aaSamw 				(void) fprintf(stderr,
1685da6c28aaSamw 				    gettext("failed to remove %s"
1686da6c28aaSamw 				    "- permisssion denied\n"), ifname);
1687da6c28aaSamw 			}
16887c478bd9Sstevel@tonic-gate 			perror(ifname);
1689da6c28aaSamw 		}
16907c478bd9Sstevel@tonic-gate 		if (!quiet) {
16917c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
16923d63ea05Sas 			    " -- replaced with %s"), ofname);
16937c478bd9Sstevel@tonic-gate 			newline_needed = 1;
16947c478bd9Sstevel@tonic-gate 		}
16957c478bd9Sstevel@tonic-gate 		return;		/* Successful return */
16967c478bd9Sstevel@tonic-gate 	}
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 	/* Unsuccessful return -- one of the tests failed */
16997c478bd9Sstevel@tonic-gate 	if (ofname[0] != '\0') {
17007c478bd9Sstevel@tonic-gate 		if (unlink(ofname)) {
1701da6c28aaSamw 			if (errno == EPERM) {
1702da6c28aaSamw 				(void) fprintf(stderr,
1703da6c28aaSamw 				    gettext("failed to remove %s"
1704da6c28aaSamw 				    "- permisssion denied\n"), ifname);
1705da6c28aaSamw 			}
17067c478bd9Sstevel@tonic-gate 			perror(ofname);
17077c478bd9Sstevel@tonic-gate 		}
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 		ofname[0] = '\0';
17107c478bd9Sstevel@tonic-gate 	}
17117c478bd9Sstevel@tonic-gate }
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate static void
onintr()17147c478bd9Sstevel@tonic-gate onintr()
17157c478bd9Sstevel@tonic-gate {
17167c478bd9Sstevel@tonic-gate 	if (!precious && !use_stdout && ofname[0] != '\0')
17177c478bd9Sstevel@tonic-gate 		(void) unlink(ofname);
17187c478bd9Sstevel@tonic-gate 	exit(1);
17197c478bd9Sstevel@tonic-gate }
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate static void
oops()17227c478bd9Sstevel@tonic-gate oops()	/* wild pointer -- assume bad input */
17237c478bd9Sstevel@tonic-gate {
17247c478bd9Sstevel@tonic-gate 	if (do_decomp) {
17257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("uncompress: corrupt input\n"));
17267c478bd9Sstevel@tonic-gate 	}
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 	if (!use_stdout && ofname[0] != '\0') {
17297c478bd9Sstevel@tonic-gate 		(void) unlink(ofname);
17307c478bd9Sstevel@tonic-gate 	}
17317c478bd9Sstevel@tonic-gate 
17327c478bd9Sstevel@tonic-gate 	exit(1);
17337c478bd9Sstevel@tonic-gate }
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate static void
cl_block(count_long in_count)17367c478bd9Sstevel@tonic-gate cl_block(count_long in_count)	/* table clear for block compress */
17377c478bd9Sstevel@tonic-gate {
17387c478bd9Sstevel@tonic-gate 	count_long rat;
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate 	checkpoint = (count_long)in_count + (count_long)CHECK_GAP;
17417c478bd9Sstevel@tonic-gate #ifdef DEBUG
17427c478bd9Sstevel@tonic-gate 	if (debug) {
17437c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "count: %lld, ratio: ",
17443d63ea05Sas 		    (count_long)in_count);
17457c478bd9Sstevel@tonic-gate 		prratio(stderr, (count_long)in_count, (count_long)bytes_out);
17467c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
17477c478bd9Sstevel@tonic-gate 	}
17487c478bd9Sstevel@tonic-gate #endif /* DEBUG */
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate 	/* shift will overflow */
1751a77d64afScf 	if ((count_long)in_count > 0x007fffffffffffffLL) {
17527c478bd9Sstevel@tonic-gate 		rat = (count_long)bytes_out >> 8;
17537c478bd9Sstevel@tonic-gate 		if (rat == 0) {		/* Don't divide by zero */
1754a77d64afScf 			rat = 0x7fffffffffffffffLL;
17557c478bd9Sstevel@tonic-gate 		} else {
17567c478bd9Sstevel@tonic-gate 			rat = (count_long)in_count / (count_long)rat;
17577c478bd9Sstevel@tonic-gate 		}
17587c478bd9Sstevel@tonic-gate 	} else {
17597c478bd9Sstevel@tonic-gate 		/* 8 fractional bits */
17607c478bd9Sstevel@tonic-gate 		rat = ((count_long)in_count << 8) /(count_long)bytes_out;
17617c478bd9Sstevel@tonic-gate 	}
17627c478bd9Sstevel@tonic-gate 	if (rat > ratio) {
17637c478bd9Sstevel@tonic-gate 		ratio = rat;
17647c478bd9Sstevel@tonic-gate 	} else {
17657c478bd9Sstevel@tonic-gate 		ratio = 0;
17667c478bd9Sstevel@tonic-gate #ifdef DEBUG
17677c478bd9Sstevel@tonic-gate 		if (verbose)
17687c478bd9Sstevel@tonic-gate 			dump_tab();	/* dump string table */
17697c478bd9Sstevel@tonic-gate #endif
17707c478bd9Sstevel@tonic-gate 		cl_hash((count_int) hsize);
17717c478bd9Sstevel@tonic-gate 		free_ent = FIRST;
17727c478bd9Sstevel@tonic-gate 		clear_flg = 1;
17737c478bd9Sstevel@tonic-gate 		output((code_int) CLEAR);
17747c478bd9Sstevel@tonic-gate #ifdef DEBUG
17757c478bd9Sstevel@tonic-gate 		if (debug)
17767c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "clear\n");
17777c478bd9Sstevel@tonic-gate #endif /* DEBUG */
17787c478bd9Sstevel@tonic-gate 	}
17797c478bd9Sstevel@tonic-gate }
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate static void
cl_hash(count_int hsize)17827c478bd9Sstevel@tonic-gate cl_hash(count_int hsize)		/* reset code table */
17837c478bd9Sstevel@tonic-gate {
17847c478bd9Sstevel@tonic-gate 	count_int *htab_p = htab+hsize;
17857c478bd9Sstevel@tonic-gate 	long i;
17867c478bd9Sstevel@tonic-gate 	long m1 = -1;
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 	i = hsize - 16;
17897c478bd9Sstevel@tonic-gate 	do {				/* might use Sys V memset(3) here */
17907c478bd9Sstevel@tonic-gate 		*(htab_p-16) = m1;
17917c478bd9Sstevel@tonic-gate 		*(htab_p-15) = m1;
17927c478bd9Sstevel@tonic-gate 		*(htab_p-14) = m1;
17937c478bd9Sstevel@tonic-gate 		*(htab_p-13) = m1;
17947c478bd9Sstevel@tonic-gate 		*(htab_p-12) = m1;
17957c478bd9Sstevel@tonic-gate 		*(htab_p-11) = m1;
17967c478bd9Sstevel@tonic-gate 		*(htab_p-10) = m1;
17977c478bd9Sstevel@tonic-gate 		*(htab_p-9) = m1;
17987c478bd9Sstevel@tonic-gate 		*(htab_p-8) = m1;
17997c478bd9Sstevel@tonic-gate 		*(htab_p-7) = m1;
18007c478bd9Sstevel@tonic-gate 		*(htab_p-6) = m1;
18017c478bd9Sstevel@tonic-gate 		*(htab_p-5) = m1;
18027c478bd9Sstevel@tonic-gate 		*(htab_p-4) = m1;
18037c478bd9Sstevel@tonic-gate 		*(htab_p-3) = m1;
18047c478bd9Sstevel@tonic-gate 		*(htab_p-2) = m1;
18057c478bd9Sstevel@tonic-gate 		*(htab_p-1) = m1;
18067c478bd9Sstevel@tonic-gate 		htab_p -= 16;
18077c478bd9Sstevel@tonic-gate 	} while ((i -= 16) >= 0);
1808*bc54f855SJohn Levon 
1809*bc54f855SJohn Levon 	for (i += 16; i > 0; i--)
1810*bc54f855SJohn Levon 		*--htab_p = m1;
18117c478bd9Sstevel@tonic-gate }
18127c478bd9Sstevel@tonic-gate 
18137c478bd9Sstevel@tonic-gate static void
prratio(FILE * stream,count_long num,count_long den)18147c478bd9Sstevel@tonic-gate prratio(FILE *stream, count_long num, count_long den)
18157c478bd9Sstevel@tonic-gate {
18167c478bd9Sstevel@tonic-gate 	int q;  /* store percentage */
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 	q = (int)(10000LL * (count_long)num / (count_long)den);
18197c478bd9Sstevel@tonic-gate 	if (q < 0) {
18207c478bd9Sstevel@tonic-gate 		(void) putc('-', stream);
18217c478bd9Sstevel@tonic-gate 		q = -q;
18227c478bd9Sstevel@tonic-gate 	}
18237c478bd9Sstevel@tonic-gate 	(void) fprintf(stream, "%d%s%02d%%", q / 100,
18243d63ea05Sas 	    localeconv()->decimal_point, q % 100);
18257c478bd9Sstevel@tonic-gate }
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate static void
version()18287c478bd9Sstevel@tonic-gate version()
18297c478bd9Sstevel@tonic-gate {
18307c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s, Berkeley 5.9 5/11/86\n", rcs_ident);
18317c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "Options: ");
18327c478bd9Sstevel@tonic-gate #ifdef DEBUG
18337c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "DEBUG, ");
18347c478bd9Sstevel@tonic-gate #endif
18357c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "BITS = %d\n", BITS);
18367c478bd9Sstevel@tonic-gate }
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate static void
Usage()18397c478bd9Sstevel@tonic-gate Usage()
18407c478bd9Sstevel@tonic-gate {
18417c478bd9Sstevel@tonic-gate #ifdef DEBUG
18427c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
1843da6c28aaSamw 	"Usage: compress [-dDVfc/] [-b maxbits] [file ...]\n");
18447c478bd9Sstevel@tonic-gate #else
18457c478bd9Sstevel@tonic-gate 	if (strcmp(progname, "compress") == 0) {
18467c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
18477c478bd9Sstevel@tonic-gate 		    gettext(
1848da6c28aaSamw 		    "Usage: compress [-fv/] [-b maxbits] [file ...]\n"\
1849da6c28aaSamw 		    "       compress c [-fv] [-b maxbits] [file]\n"));
18507c478bd9Sstevel@tonic-gate 	} else if (strcmp(progname, "uncompress") == 0)
18517c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
1852da6c28aaSamw 		    "Usage: uncompress [-fv] [-c || -/] [file ...]\n"));
18537c478bd9Sstevel@tonic-gate 	else if (strcmp(progname, "zcat") == 0)
18547c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Usage: zcat [file ...]\n"));
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate #endif /* DEBUG */
18577c478bd9Sstevel@tonic-gate }
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate static char *
local_basename(char * path)18607c478bd9Sstevel@tonic-gate local_basename(char *path)
18617c478bd9Sstevel@tonic-gate {
18627c478bd9Sstevel@tonic-gate 	char *p;
18637c478bd9Sstevel@tonic-gate 	char *ret = (char *)path;
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 	while ((p = (char *)strpbrk(ret, "/")) != NULL)
18667c478bd9Sstevel@tonic-gate 		ret = p + 1;
18677c478bd9Sstevel@tonic-gate 	return (ret);
18687c478bd9Sstevel@tonic-gate }
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate static int
addDotZ(char * fn,size_t fnsize)18717c478bd9Sstevel@tonic-gate addDotZ(char *fn, size_t fnsize)
18727c478bd9Sstevel@tonic-gate {
18737c478bd9Sstevel@tonic-gate 	char *fn_dup;
18747c478bd9Sstevel@tonic-gate 	char *dir;
18757c478bd9Sstevel@tonic-gate 	long int max_name;
18767c478bd9Sstevel@tonic-gate 	long int max_path;
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 	fn_dup = strdup(fn);
18797c478bd9Sstevel@tonic-gate 	dir = dirname(fn_dup);
18807c478bd9Sstevel@tonic-gate 	max_name = pathconf(dir, _PC_NAME_MAX);
18817c478bd9Sstevel@tonic-gate 	max_path = pathconf(dir, _PC_PATH_MAX);
18827c478bd9Sstevel@tonic-gate 	free(fn_dup);
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 	/* Check for component length too long */
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 	if ((strlen(local_basename(fn)) + 2) > (size_t)max_name) {
18877c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
18883d63ea05Sas 		    gettext("%s: filename too long to tack on .Z:"
18893d63ea05Sas 		    " %s\n"), progname, fn);
18907c478bd9Sstevel@tonic-gate 		return (-1);
18917c478bd9Sstevel@tonic-gate 	}
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate 	/* Check for path length too long */
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate 	if ((strlen(fn) + 2) > (size_t)max_path - 1) {
18967c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
18973d63ea05Sas 		    gettext("%s: Pathname too long to tack on .Z:"
18983d63ea05Sas 		    " %s\n"), progname, fn);
18997c478bd9Sstevel@tonic-gate 		return (-1);
19007c478bd9Sstevel@tonic-gate 	}
19017c478bd9Sstevel@tonic-gate 
19027c478bd9Sstevel@tonic-gate 	if (strlcat(fn, ".Z", fnsize) >= fnsize) {
19037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
19043d63ea05Sas 		    gettext("%s: Buffer overflow adding .Z to %s\n"),
19053d63ea05Sas 		    progname, fn);
19067c478bd9Sstevel@tonic-gate 		return (-1);
19077c478bd9Sstevel@tonic-gate 	}
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate 	return (0);
19107c478bd9Sstevel@tonic-gate }
1911