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