rmtodd@uokmax.UUCP (Richard Michael Todd) (12/10/87)
OK, here's compress for MINIX. I've put in a rather messy maze of #ifdefs in there so that if you've got Aztec C, you can use the same source file to compile a version for MS-DOS as well. To compile a MSDOS version with Aztec C: cc -DAZTEC86 compress.c ln -o compress.exe compress.o -lc To compile a MINIX version with Aztec C: cc -DAZTEC86 -DMINIX compress.c (then follow up with the usual junk to link with the MINIX library, use dos2out, etc.) To compile under MINIX: cc -i -T. -DAZTEC86 -DMINIX compress.c Note that compress won't work if it isn't compiled split I&D. Note also that the MINIX cc-compiled version is about 40% slower than the Aztec-compiled one. Once you've got a MINIX version created, with whatever compiler you used, move it to /usr/bin/compress and make links to it named /usr/bin/zcat and /usr/bin/uncompress. This allows you to invoke compress under the other two names and have it perform the appropriate functions. (Alas, you can't make links under MS-DOS. Sigh.) ------------------------- cut here ---------------------------------- #!/bin/sh # Shell archive created with Richard Todd's makeshar on Wed Dec 9 12:55:38 1987 echo x - compress.c sed 's/^X//' >compress.c << 'EOF' X/* Here is source that will compile with AZTEC C86. Define AZTEC86 and compile it. X** it is completely self-contained - no makefile. Just "cc -dAZTEC86 compress ; X**ln compress.o -lc". X** for some debug output add -DDEBUG X** for more add -DDEBUG -DDEBUG2 -DAZTECBITS=12 (won't work w 13 bits) X** X** It also has defines for the Metaware compiler, but since that is not yet very X** popular, I won't include the functions that Metaware lacks (stat and utime). X** Altered by Richard Todd to allow compilation for MINIX with Aztec cc and X** the appropriate library: X** cc -DMINIX -DAZTEC86 compress.c X** ln -o compress.exe -S 1 crtso.o compress.o -lminix X** dos2out -d compress.exe X** If MINIX isn't defined and AZTEC86 is, PCDOS will be automatically defined. X*/ X#ifdef _lint X/* flags for gimpel's pc-lint */ X#define AZTEC86 X/*lint -e701 -e702 signed integers are shifted with reckless abandon */ X#endif X/* X * Compress - data compression program X */ X#define min(a,b) ((a>b) ? b : a) X X/* X * machine variants which require cc -Dmachine: pdp11, z8000 X * compiler variants for PC-DOS which require -Dcompiler: AZTEC86 X */ X X/******************************************************************* X* X* IMPORTANT MODEL 16 NOTE X* X* cc -DM_XENIX -DUSERMEM=128000 compress.c -o compress X* X*******************************************************************/ X X X/* X * Set USERMEM to the maximum amount of physical user memory available X * in bytes. USERMEM is used to determine the maximum BITS that can be used X * for compression. X * X * SACREDMEM is the amount of physical memory saved for others; compress X * will hog the rest. X */ X#ifndef SACREDMEM X#define SACREDMEM 0 X#endif X X#ifndef USERMEM X# define USERMEM 450000 /* default user memory */ X#endif X X#ifdef interdata /* (Perkin-Elmer) */ X#define SIGNED_COMPARE_SLOW /* signed compare is slower than unsigned */ X#endif X X#ifdef pdp11 X# define BITS 12 /* max bits/code for 16-bit machine */ X# define NO_UCHAR /* also if "unsigned char" functions as signed char */ X# undef USERMEM X#endif /* pdp11 */ /* don't forget to compile with -i */ X X#ifdef z8000 X# define BITS 12 X# undef vax /* weird preprocessor */ X# undef USERMEM X#endif /* z8000 */ X X#ifdef METAWARE X#define REGISTER X#define AZTEC86 X#include <stdlib.h> X#include <string.h> X#include <system.cf> X#include <ufile.h> X#else X#define REGISTER register X#endif /* METAWARE */ X X#ifdef AZTEC86 Xvoid prratio(),cl_block(),cl_hash(),output(),decompress(), Xcopystat(),writeerr(),compress(),Usage(),version(); X#ifdef AZTECBITS X# define BITS AZTECBITS X#else X# define BITS 13 X#endif X# undef USERMEM X#endif /* AZTEC86 */ X X#ifdef USERMEM X# if USERMEM >= (433484+SACREDMEM) X# define PBITS 16 X# else X# if USERMEM >= (229600+SACREDMEM) X# define PBITS 15 X# else X# if USERMEM >= (127536+SACREDMEM) X# define PBITS 14 X# else X# if USERMEM >= (73464+SACREDMEM) X# define PBITS 13 X# else X# define PBITS 12 X# endif X# endif X# endif X# endif X# undef USERMEM X#endif /* USERMEM */ X X#ifdef PBITS /* Preferred BITS for this memory size */ X# ifndef BITS X# define BITS PBITS X# endif X#endif /* PBITS */ X X#if BITS == 16 X# define HSIZE 69001 /* 95% occupancy */ X#endif X#if BITS == 15 X# define HSIZE 35023 /* 94% occupancy */ X#endif X#if BITS == 14 X# define HSIZE 18013 /* 91% occupancy */ X#endif X#if BITS == 13 X# define HSIZE 9001 /* 91% occupancy */ X#endif X#if BITS <= 12 X# define HSIZE 5003 /* 80% occupancy */ X#endif X X#ifdef M_XENIX /* Stupid compiler can't handle arrays with */ X# if BITS == 16 /* more than 65535 bytes - so we fake it */ X# define XENIX_16 X# else X# if BITS > 13 /* Code only handles BITS = 12, 13, or 16 */ X# define BITS 13 X# endif X# endif X#endif X X/* X * a code_int must be able to hold 2**BITS values of type int, and also -1 X */ X#if BITS > 15 Xtypedef long int code_int; X#else Xtypedef int code_int; X#endif X X#ifdef SIGNED_COMPARE_SLOW Xtypedef unsigned long int count_int; Xtypedef unsigned short int count_short; X#else Xtypedef long int count_int; X#endif X X#ifdef NO_UCHAR X typedef char char_type; X#else X typedef unsigned char char_type; X#endif /* UCHAR */ Xchar_type magic_header[] = { "\037\235" }; /* 1F 9D */ X X/* Defines for third byte of header */ X#define BIT_MASK 0x1f X#define BLOCK_MASK 0x80 X/* Masks 0x40 and 0x20 are free. I think 0x20 should mean that there is X a fourth header byte (for expansion). X*/ X#define INIT_BITS 9 /* initial number of bits/code */ X X/* X * compress.c - File compression ala IEEE Computer, June 1984. X * X * Authors: Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas) X * Jim McKie (decvax!mcvax!jim) X * Steve Davies (decvax!vax135!petsd!peora!srd) X * Ken Turkowski (decvax!decwrl!turtlevax!ken) X * James A. Woods (decvax!ihnp4!ames!jaw) X * Joe Orost (decvax!vax135!petsd!joe) X * X * $Header: compress.c,v 4.0 85/07/30 12:50:00 joe Release $ X * $Log: compress.c,v $ X * Revision 4.1 85/12/02 Kent Williams X * Ported to AZTEC C86 under MSDOS X * Changed appending a ".Z" to just "Z" so that MSDOS won't choke X * changed getchar and putchar to getc(stdin) and putc(c,stdout) to X * suppress cr/nl mapping and hi bit masking X * X * Revision 4.0 85/07/30 12:50:00 joe X * Removed ferror() calls in output routine on every output except first. X * Prepared for release to the world. X * X * Revision 3.6 85/07/04 01:22:21 joe X * Remove much wasted storage by overlaying hash table with the tables X * used by decompress: tab_suffix[1<<BITS], stack[8000]. Updated USERMEM X * computations. Fixed dump_tab() DEBUG routine. X * X * Revision 3.5 85/06/30 20:47:21 jaw X * Change hash function to use exclusive-or. Rip out hash cache. These X * speedups render the megamemory version defunct, for now. Make decoder X * stack global. Parts of the RCS trunks 2.7, 2.6, and 2.1 no longer apply. X * X * Revision 3.4 85/06/27 12:00:00 ken X * Get rid of all floating-point calculations by doing all compression ratio X * calculations in fixed point. X * X * Revision 3.3 85/06/24 21:53:24 joe X * Incorporate portability suggestion for M_XENIX. Got rid of text on #else X * and #endif lines. Cleaned up #ifdefs for vax and interdata. X * X * Revision 3.2 85/06/06 21:53:24 jaw X * Incorporate portability suggestions for Z8000, IBM PC/XT from mailing list. X * Default to "quiet" output (no compression statistics). X * X * Revision 3.1 85/05/12 18:56:13 jaw X * Integrate decompress() stack speedups (from early pointer mods by McKie). X * Repair multi-file USERMEM gaffe. Unify 'force' flags to mimic semantics X * of SVR2 'pack'. Streamline block-compress table clear logic. Increase X * output byte count by magic number size. X * X * Revision 3.0 84/11/27 11:50:00 petsd!joe X * Set HSIZE depending on BITS. Set BITS depending on USERMEM. Unrolled X * loops in clear routines. Added "-C" flag for 2.0 compatibility. Used X * unsigned compares on Perkin-Elmer. Fixed foreground check. X * X * Revision 2.7 84/11/16 19:35:39 ames!jaw X * Cache common hash codes based on input statistics; this improves X * performance for low-density raster images. Pass on #ifdef bundle X * from Turkowski. X * X * Revision 2.6 84/11/05 19:18:21 ames!jaw X * Vary size of hash tables to reduce time for small files. X * Tune PDP-11 hash function. X * X * Revision 2.5 84/10/30 20:15:14 ames!jaw X * Junk chaining; replace with the simpler (and, on the VAX, faster) X * double hashing, discussed within. Make block compression standard. X * X * Revision 2.4 84/10/16 11:11:11 ames!jaw X * Introduce adaptive reset for block compression, to boost the rate X * another several percent. (See mailing list notes.) X * X * Revision 2.3 84/09/22 22:00:00 petsd!joe X * Implemented "-B" block compress. Implemented REVERSE sorting of tab_next. X * Bug fix for last bits. Changed fwrite to putc loop everywhere. X * X * Revision 2.2 84/09/18 14:12:21 ames!jaw X * Fold in news changes, small machine typedef from thomas, X * #ifdef interdata from joe. X * X * Revision 2.1 84/09/10 12:34:56 ames!jaw X * Configured fast table lookup for 32-bit machines. X * This cuts user time in half for b <= FBITS, and is useful for news batching X * from VAX to PDP sites. Also sped up decompress() [fwrite->putc] and X * added signal catcher [plus beef in writeerr()] to delete effluvia. X * X * Revision 2.0 84/08/28 22:00:00 petsd!joe X * Add check for foreground before prompting user. Insert maxbits into X * compressed file. Force file being uncompressed to end with ".Z". X * Added "-c" flag and "zcat". Prepared for release. X * X * Revision 1.10 84/08/24 18:28:00 turtlevax!ken X * Will only compress regular files (no directories), added a magic number X * header (plus an undocumented -n flag to handle old files without headers), X * added -f flag to force overwriting of possibly existing destination file, X * otherwise the user is prompted for a response. Will tack on a .Z to a X * filename if it doesn't have one when decompressing. Will only replace X * file if it was compressed. X * X * Revision 1.9 84/08/16 17:28:00 turtlevax!ken X * Removed scanargs(), getopt(), added .Z extension and unlimited number of X * filenames to compress. Flags may be clustered (-Ddvb12) or separated X * (-D -d -v -b 12), or combination thereof. Modes and other status is X * copied with copystat(). -O bug for 4.2 seems to have disappeared with X * 1.8. X * X * Revision 1.8 84/08/09 23:15:00 joe X * Made it compatible with vax version, installed jim's fixes/enhancements X * X * Revision 1.6 84/08/01 22:08:00 joe X * Sped up algorithm significantly by sorting the compress chain. X * X * Revision 1.5 84/07/13 13:11:00 srd X * Added C version of vax asm routines. Changed structure to arrays to X * save much memory. Do unsigned compares where possible (faster on X * Perkin-Elmer) X * X * Revision 1.4 84/07/05 03:11:11 thomas X * Clean up the code a little and lint it. (Lint complains about all X * the regs used in the asm, but I'm not going to "fix" this.) X * X * Revision 1.3 84/07/05 02:06:54 thomas X * Minor fixes. X * X * Revision 1.2 84/07/05 00:27:27 thomas X * Add variable bit length output. X * X */ Xstatic char rcs_ident[] = "$Header: compress.c,v 4.1 85/12/05 09:00:00 kent Release $"; X X#include <stdio.h> X#include <ctype.h> X X#ifndef METAWARE X#include <signal.h> X#endif X X#ifndef AZTEC86 X#include <sys/types.h> X#include <sys/stat.h> X#else X/* stuff defined for Aztec compiler */ X#ifndef MINIX X#define PCDOS X/* MINIX can handle full-length filenames, DOS can't */ X/* also, MINIX has full stat capability */ X#endif X X#include <stat.h> X#ifndef METAWARE X#ifndef MINIX X/* MINIX signal.h includes definition of signal */ Xvoid (*signal())(); X#endif XFILE *freopen(); X#endif X#endif X X#ifdef PCDOS X#define DOTZ "Z" X#else X#define DOTZ ".Z" X#endif X X#define ARGVAL() (*++(*argv) || (--argc && *++argv)) X Xint n_bits; /* number of bits/code */ Xint maxbits = BITS; /* user settable max # bits/code */ Xcode_int maxcode; /* maximum code, given n_bits */ Xcode_int maxmaxcode = 1 << BITS; /* should NEVER generate this code */ X#ifdef COMPATIBLE /* But wrong! */ X# define MAXCODE(n_bits) (1 << (n_bits) - 1) X#else X# define MAXCODE(n_bits) ((1 << (n_bits)) - 1) X#endif /* COMPATIBLE */ X X#ifdef XENIX_16 Xcount_int htab0[8192]; Xcount_int htab1[8192]; Xcount_int htab2[8192]; Xcount_int htab3[8192]; Xcount_int htab4[8192]; Xcount_int htab5[8192]; Xcount_int htab6[8192]; Xcount_int htab7[8192]; Xcount_int htab8[HSIZE-65536]; Xcount_int * htab[9] = { X htab0, htab1, htab2, htab3, htab4, htab5, htab6, htab7, htab8 }; X#define htabof(i) (htab[(i) >> 13][(i) & 0x1fff]) Xunsigned short code0tab[16384]; Xunsigned short code1tab[16384]; Xunsigned short code2tab[16384]; Xunsigned short code3tab[16384]; Xunsigned short code4tab[16384]; Xunsigned short * codetab[5] = { X code0tab, code1tab, code2tab, code3tab, code4tab }; X X#define codetabof(i) (codetab[(i) >> 14][(i) & 0x3fff]) X X#else /* Normal machine */ X X#ifndef AZTEC86 X count_int htab [HSIZE]; X unsigned short codetab [HSIZE]; X#else X count_int *htab; X unsigned short *codetab; X# define HTABSIZE ((unsigned)(HSIZE*sizeof(count_int))) X# define CODETABSIZE ((unsigned)(HSIZE*sizeof(unsigned short))) X X#endif X#define htabof(i) htab[i] X#define codetabof(i) codetab[i] X#endif /* XENIX_16 */ Xcode_int hsize = HSIZE; /* for dynamic table sizing */ Xcount_int fsize; X X/* X * To save much memory, we overlay the table used by compress() with those X * used by decompress(). The tab_prefix table is the same size and type X * as the codetab. The tab_suffix table needs 2**BITS characters. We X * get this from the beginning of htab. The output stack uses the rest X * of htab, and contains characters. There is plenty of room for any X * possible stack (stack used to be 8000 characters). X */ X X#define tab_prefixof(i) codetabof(i) X#ifdef XENIX_16 X# define tab_suffixof(i) ((char_type *)htab[(i)>>15])[(i) & 0x7fff] X# define de_stack ((char_type *)(htab2)) X#else /* Normal machine */ X# define tab_suffixof(i) ((char_type *)(htab))[i] X# define de_stack ((char_type *)&tab_suffixof(1<<BITS)) X#endif /* XENIX_16 */ X Xcode_int free_ent = 0; /* first unused entry */ Xint exit_stat = 0; X Xcode_int getcode(); X Xvoid Usage() { X#ifdef DEBUG Xfprintf(stderr,"Usage: compress [-dDVfc] [-b maxbits] [file ...]\n"); X} Xint debug = 0; X#else Xfprintf(stderr,"Usage: compress [-dfvcV] [-b maxbits] [file ...]\n"); X} X#endif /* DEBUG */ Xint nomagic = 0; /* Use a 3-byte magic number header, unless old file */ Xint zcat_flg = 0; /* Write output on stdout, suppress messages */ Xint quiet = 0; /* don't tell me about compression */ X X/* X * block compression parameters -- after all codes are used up, X * and compression rate changes, start over. X */ Xint block_compress = BLOCK_MASK; Xint clear_flg = 0; Xlong int ratio = 0; X#define CHECK_GAP 10000 /* ratio check interval */ Xcount_int checkpoint = CHECK_GAP; X/* X * the next two codes should not be changed lightly, as they must not X * lie within the contiguous general code space. X */ X#define FIRST 257 /* first free entry */ X#define CLEAR 256 /* table clear output code */ X Xint force = 0; Xchar ofname [100]; X#ifdef DEBUG Xint verbose = 0; X#endif /* DEBUG */ X X#ifndef METAWARE X#ifdef AZTEC86 Xvoid X#else Xint X#endif X(*bgnd_flag)(); X#endif X Xint do_decomp = 0; X X/***************************************************************** X * TAG( main ) X * X * Algorithm from "A Technique for High Performance Data Compression", X * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19. X * X * Usage: compress [-dfvc] [-b bits] [file ...] X * Inputs: X * -d: If given, decompression is done instead. X * X * -c: Write output on stdout, don't remove original. X * X * -b: Parameter limits the max number of bits/code. X * X * -f: Forces output file to be generated, even if one already X * exists, and even if no space is saved by compressing. X * If -f is not used, the user will be prompted if stdin is X * a tty, otherwise, the output file will not be overwritten. X * X * -v: Write compression statistics X * X * file ...: Files to be compressed. If none specified, stdin X * is used. X * Outputs: X * file.Z: Compressed form of file with same mode, owner, and utimes X * or stdout (if stdin used as input) X * X * Assumptions: X * When filenames are given, replaces with the compressed version X * (.Z suffix) only if the file decreases in size. X * Algorithm: X * Modified Lempel-Ziv method (LZW). Basically finds common X * substrings and replaces them with a variable size code. This is X * deterministic, and can be done on the fly. Thus, the decompression X * procedure needs no input table, but tracks the way the table was built. X */ X Xvoid main( argc, argv ) XREGISTER int argc; char **argv; X{ X int overwrite = 0; /* Do not overwrite unless given -f flag */ X char tempname[100]; X char **filelist, **fileptr; X char *cp;extern char *strrchr(), *malloc(); X struct stat statbuf; X#ifndef METAWARE X extern void onintr(), oops(); X if ( (bgnd_flag = signal ( SIGINT, SIG_IGN )) != SIG_IGN ) { X signal ( SIGINT, onintr ); X signal ( SIGSEGV, oops ); X } X#endif X#ifdef AZTEC86 X#ifdef METAWARE X _setmode(NULL,_ALL_FILES_BINARY); X _setmode(stdin,_BINARY); X _setmode(stdout,_BINARY); X _setmode(stderr,_TEXT); X#endif X if (NULL == (htab = (count_int *)malloc(HTABSIZE))) X { X fprintf(stderr,"Can't allocate htab\n"); X exit(1); X } X if (NULL == (codetab = (unsigned short *)malloc(CODETABSIZE))) X { X fprintf(stderr,"Can't allocate codetab\n"); X exit(1); X } X#endif X#ifdef COMPATIBLE X nomagic = 1; /* Original didn't have a magic number */ X#endif /* COMPATIBLE */ X X filelist = fileptr = (char **)(malloc((unsigned)(argc * sizeof(*argv)))); X *filelist = NULL; X X if((cp = strrchr(argv[0], '/')) != 0) { X cp++; X } else { X cp = argv[0]; X } X if(strcmp(cp, "uncompress") == 0) { X do_decomp = 1; X } else if(strcmp(cp, "zcat") == 0) { X do_decomp = 1; X zcat_flg = 1; X } X X#ifdef BSD4_2 X /* 4.2BSD dependent - take it out if not */ X setlinebuf( stderr ); X#endif /* BSD4_2 */ X X /* Argument Processing X * All flags are optional. X * -D => debug X * -V => print Version; debug verbose X * -d => do_decomp X * -v => unquiet X * -f => force overwrite of output file X * -n => no header: useful to uncompress old files X * -b maxbits => maxbits. If -b is specified, then maxbits MUST be X * given also. X * -c => cat all output to stdout X * -C => generate output compatible with compress 2.0. X * if a string is left, must be an input filename. X */ X for (argc--, argv++; argc > 0; argc--, argv++) X { X if (**argv == '-') X { /* A flag argument */ X while (*++(*argv)) X { /* Process all flags in this arg */ X switch (**argv) X { X#ifdef DEBUG X case 'D': X debug = 1; X break; X case 'V': X verbose = 1; X version(); X break; X#else X case 'V': X version(); X break; X#endif /* DEBUG */ X case 'v': X quiet = 0; X break; X case 'd': X do_decomp = 1; X break; X case 'f': X case 'F': X overwrite = 1; X force = 1; X break; X case 'n': X nomagic = 1; X break; X case 'C': X block_compress = 0; X break; X case 'b': X if (!ARGVAL()) X { X fprintf(stderr, "Missing maxbits\n"); X Usage(); X exit(1); X } X maxbits = atoi(*argv); X goto nextarg; X case 'c': X zcat_flg = 1; X break; X case 'q': X quiet = 1; X break; X default: X fprintf(stderr, "Unknown flag: '%c'; ", **argv); X Usage(); X exit(1); X } X } X } X else X { /* Input file name */ X *fileptr++ = *argv; /* Build input file list */ X *fileptr = NULL; X /* process nextarg; */ X } X nextarg: continue; X } X X if(maxbits < INIT_BITS) maxbits = INIT_BITS; X if (maxbits > BITS) maxbits = BITS; X maxmaxcode = 1 << maxbits; X X if (*filelist != NULL) X { X for (fileptr = filelist; *fileptr; fileptr++) X { X exit_stat = 0; X if (do_decomp != 0) X { /* DECOMPRESSION */ X /* Check for .Z suffix */ X#ifndef PCDOS X if (strcmp(*fileptr + strlen(*fileptr) - 2, DOTZ) != 0) X#else X if (strcmp(*fileptr + strlen(*fileptr) - 1, DOTZ) != 0) X#endif X { X /* No .Z: tack one on */ X strcpy(tempname, *fileptr); X#ifndef PCDOS X strcat(tempname, DOTZ); X#else X /* either tack one on or replace last character */ X { X char *dot;extern char *strchr(); X if (NULL == (dot = strchr(tempname,'.'))) X { X strcat(tempname,".Z"); X } X else X /* if there is a dot then either tack a z on X or replace last character */ X { X if (strlen(dot) < 4) X strcat(tempname,DOTZ); X else X dot[3] = 'Z'; X } X } X#endif X *fileptr = tempname; X } X /* Open input file */ X if ((freopen(*fileptr, "r", stdin)) == NULL) X { X perror(*fileptr); continue; X } X /* Check the magic number */ X if (nomagic == 0) X { X unsigned magic1, magic2; X if (((magic1 = getc(stdin)) != (magic_header[0] & 0xFF)) X || ((magic2 = getc(stdin)) != (magic_header[1] & 0xFF))) X { X fprintf(stderr, X "%s: not in compressed format %x %x\n", X *fileptr,magic1,magic2); X continue; X } X maxbits = getc(stdin); /* set -b from file */ X block_compress = maxbits & BLOCK_MASK; X maxbits &= BIT_MASK; X maxmaxcode = 1 << maxbits; X if(maxbits > BITS) X { X fprintf(stderr, X "%s: compressed with %d bits, can only handle %d bits\n", X *fileptr, maxbits, BITS); X continue; X } X } X /* Generate output filename */ X strcpy(ofname, *fileptr); X#ifndef PCDOS X ofname[strlen(*fileptr) - 2] = '\0'; /* Strip off .Z */ X#else X /* kludge to handle various common three character extension */ X { X char *dot; extern char *strchr(); X char fixup = '\0'; X /* first off, map name to upper case */ X for (dot = ofname; *dot; dot++) X *dot = toupper(*dot); X if (NULL == (dot = strchr(ofname,'.'))) X { X fprintf(stderr,"Bad filename %s\n",ofname); X exit(1); X } X if (strlen(dot) == 4) X /* we got three letter extensions */ X { X if (strcmp(dot,".EXZ") == 0) X fixup = 'E'; X else if (strcmp(dot,".COZ") == 0) X fixup = 'M'; X else if (strcmp(dot,".BAZ") == 0) X fixup = 'S'; X else if (strcmp(dot,".OBZ") == 0) X fixup = 'J'; X else if (strcmp(dot,".SYZ") == 0) X fixup = 'S'; X else if (strcmp(dot,".DOZ") == 0) X fixup = 'C'; X X } X /* replace the Z */ X ofname[strlen(*fileptr) - 1] = fixup; X } X#endif X } else X { /* COMPRESSION */ X if (strcmp(*fileptr + strlen(*fileptr) - 2, DOTZ) == 0) X { X fprintf(stderr, "%s: already has .Z suffix -- no change\n", X *fileptr); X continue; X } X /* Open input file */ X if ((freopen(*fileptr, "r", stdin)) == NULL) X { X perror(*fileptr); continue; X } X (void)stat( *fileptr, &statbuf ); X fsize = (long) statbuf.st_size; X /* X * tune hash table size for small files -- ad hoc, X * but the sizes match earlier #defines, which X * serve as upper bounds on the number of output codes. X */ X hsize = HSIZE; /*lint -e506 -e712 */ X if ( fsize < (1 << 12) ) X hsize = min ( 5003, HSIZE ); X else if ( fsize < (1 << 13) ) X hsize = min ( 9001, HSIZE ); X else if ( fsize < (1 << 14) ) X hsize = min ( 18013, HSIZE ); X else if ( fsize < (1 << 15) ) X hsize = min ( 35023, HSIZE ); X else if ( fsize < 47000 ) X hsize = min ( 50021, HSIZE ); /*lint +e506 +e712 */ X X /* Generate output filename */ X strcpy(ofname, *fileptr); X#ifndef BSD4_2 /* Short filenames */ X if ((cp=strrchr(ofname,'/')) != NULL) X cp++; X else X cp = ofname; X if (strlen(cp) > 12) X { X fprintf(stderr,"%s: filename too long to tack on .Z\n",cp); X continue; X } X#ifdef PCDOS X else X { X /* either tack one on or replace last character */ X char *dot;extern char *strchr(); X if (NULL == (dot = strchr(cp,'.'))) X { X strcat(cp,".Z"); X } X else X /* if there is a dot then either tack a z on X or replace last character */ X { X if (strlen(dot) < 4) X strcat(cp,DOTZ); X else X dot[3] = 'Z'; X } X } X#endif X#endif /* BSD4_2 Long filenames allowed */ X#ifndef PCDOS X /* PCDOS takes care of this above */ X strcat(ofname, DOTZ); X#endif X } X /* Check for overwrite of existing file */ X if (overwrite == 0 && zcat_flg == 0) X { X if (stat(ofname, &statbuf) == 0) X { X char response[2]; X response[0] = 'n'; X fprintf(stderr, "%s already exists;", ofname); X if (foreground()) X { X fprintf(stderr, X " do you wish to overwrite %s (y or n)? ", ofname); X fflush(stderr); X (void)read(2, response, 2); X while (response[1] != '\n') X { X if (read(2, response+1, 1) < 0) X { /* Ack! */ X perror("stderr"); X break; X } X } X } X if (response[0] != 'y') X { X fprintf(stderr, "\tnot overwritten\n"); X continue; X } X } X } X if(zcat_flg == 0) X { /* Open output file */ X if (freopen(ofname, "w", stdout) == NULL) X { X perror(ofname); X continue; X } X if(!quiet) X fprintf(stderr, "%s: ", *fileptr); X } X X /* Actually do the compression/decompression */ X if (do_decomp == 0) X compress(); X#ifndef DEBUG X else X decompress(); X#else X else if (debug == 0) X decompress(); X else X printcodes(); X if (verbose) X dump_tab(); X#endif /* DEBUG */ X if(zcat_flg == 0) X { X copystat(*fileptr, ofname); /* Copy stats */ X if((exit_stat == 1) || (!quiet)) X putc('\n', stderr); X } X } X } else X { /* Standard input */ X if (do_decomp == 0) X { X compress(); X#ifdef DEBUG X if(verbose) dump_tab(); X#endif /* DEBUG */ X if(!quiet) X putc('\n', stderr); X } else X { X /* Check the magic number */ X if (nomagic == 0) X { X if ((getc(stdin)!=(magic_header[0] & 0xFF)) X || (getc(stdin)!=(magic_header[1] & 0xFF))) X { X fprintf(stderr, "stdin: not in compressed format\n"); X exit(1); X } X maxbits = getc(stdin); /* set -b from file */ X block_compress = maxbits & BLOCK_MASK; X maxbits &= BIT_MASK; X maxmaxcode = 1 << maxbits; X fsize = 100000; /* assume stdin large for USERMEM */ X if(maxbits > BITS) X { X fprintf(stderr, X "stdin: compressed with %d bits, can only handle %d bits\n", X maxbits, BITS); X exit(1); X } X } X#ifndef DEBUG X decompress(); X#else X if (debug == 0) decompress(); X else printcodes(); X if (verbose) dump_tab(); X#endif /* DEBUG */ X } X } X exit(exit_stat); X} X Xstatic int offset; Xlong int in_count = 1; /* length of input */ Xlong int bytes_out; /* length of compressed output */ Xlong int out_count = 0; /* # of codes output (for debugging) */ X X/* X * compress stdin to stdout X * X * Algorithm: use open addressing double hashing (no chaining) on the X * prefix code / next character combination. We do a variant of Knuth's X * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime X * secondary probe. Here, the modular division first probe is gives way X * to a faster exclusive-or manipulation. Also do block compression with X * an adaptive reset, whereby the code table is cleared when the compression X * ratio decreases, but after the table fills. The variable-length output X * codes are re-sized at this point, and a special CLEAR code is generated X * for the decompressor. Late addition: construct the table according to X * file size for noticeable speed improvement on small files. Please direct X * questions about this implementation to ames!jaw. X */ X Xvoid compress() X{ X REGISTER long fcode; X REGISTER code_int i = 0; X REGISTER int c; X REGISTER code_int ent; X#ifdef XENIX_16 X REGISTER code_int disp; X#else /* Normal machine */ X REGISTER int disp; X#endif X REGISTER code_int hsize_reg; X REGISTER int hshift; X X#ifndef COMPATIBLE X if (nomagic == 0) X { X putc(magic_header[0],stdout); X putc(magic_header[1],stdout); X putc((char)(maxbits | block_compress),stdout); X if(ferror(stdout)) X writeerr(); X } X#endif /* COMPATIBLE */ X X offset = 0; X bytes_out = 3; /* includes 3-byte header mojo */ X out_count = 0; X clear_flg = 0; X ratio = 0; X in_count = 1; X checkpoint = CHECK_GAP; X maxcode = MAXCODE(n_bits = INIT_BITS); X free_ent = ((block_compress) ? FIRST : 256 ); X X ent = getc(stdin); X X hshift = 0; X for ( fcode = (long) hsize; fcode < 65536L; fcode *= 2L ) X hshift++; X hshift = 8 - hshift; /* set hash code range bound */ X X hsize_reg = hsize; X cl_hash( (count_int) hsize_reg); /* clear hash table */ X X#ifdef SIGNED_COMPARE_SLOW X while ( (c = getc(stdin)) != (unsigned) EOF ) X#else X while ( (c = getc(stdin)) != EOF ) X#endif X { X in_count++; X fcode = (long) (((long) c << maxbits) + ent); X i = ((c << hshift) ^ ent); /* xor hashing */ X X if ( htabof (i) == fcode ) X { X ent = codetabof (i); X continue; X } else if ( (long)htabof (i) < 0 ) /* empty slot */ X goto nomatch; X disp = hsize_reg - i; /* secondary hash (after G. Knott) */ X if ( i == 0 ) X disp = 1; Xprobe: X if ( (i -= disp) < 0 ) X i += hsize_reg; X X if ( htabof (i) == fcode ) X { X ent = codetabof (i); X continue; X } X if ( (long)htabof (i) > 0 ) X goto probe; Xnomatch: X output ( (code_int) ent ); X out_count++; X ent = c; X#ifdef SIGNED_COMPARE_SLOW X if ( (unsigned) free_ent < (unsigned) maxmaxcode) X#else X if ( free_ent < maxmaxcode ) X#endif X { X codetabof (i) = free_ent++; /* code -> hashtable */ X htabof (i) = fcode; X } X else if ( (count_int)in_count >= checkpoint && block_compress ) X cl_block (); X } X /* X * Put out the final code. X */ X output( (code_int)ent ); X out_count++; X output( (code_int)-1 ); X X /* X * Print out stats on stderr X */ X if(zcat_flg == 0 && !quiet) X { X#ifdef DEBUG X fprintf( stderr, X "%ld chars in, %ld codes (%ld bytes) out, compression factor: ", X in_count, out_count, bytes_out ); X prratio( stderr, in_count, bytes_out ); X fprintf( stderr, "\n"); X fprintf( stderr, "\tCompression as in compact: " ); X prratio( stderr, in_count-bytes_out, in_count ); X fprintf( stderr, "\n"); X fprintf( stderr, "\tLargest code (of last block) was %d (%d bits)\n", X free_ent - 1, n_bits ); X#else /* !DEBUG */ X fprintf( stderr, "Compression: " ); X prratio( stderr, in_count-bytes_out, in_count ); X#endif /* DEBUG */ X } X if(bytes_out > in_count) /* exit(2) if no savings */ X exit_stat = 2; X return; X} X X/***************************************************************** X * TAG( output ) X * X * Output the given code. X * Inputs: X * code: A n_bits-bit integer. If == -1, then EOF. This assumes X * that n_bits =< (long)wordsize - 1. X * Outputs: X * Outputs code to the file. X * Assumptions: X * Chars are 8 bits long. X * Algorithm: X * Maintain a BITS character long buffer (so that 8 codes will X * fit in it exactly). Use the VAX insv instruction to insert each X * code in turn. When the buffer fills up empty it and start over. X */ X Xstatic char buf[BITS]; X X#ifndef vax Xchar_type lmask[9] = {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00}; Xchar_type rmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff}; X#endif /* vax */ Xvoid output( code ) Xcode_int code; X{ X#ifdef DEBUG X static int col = 0; X#endif /* DEBUG */ X X /* X * On the VAX, it is important to have the REGISTER declarations X * in exactly the order given, or the asm will break. X */ X REGISTER int r_off = offset, bits= n_bits; X REGISTER char * bp = buf; X#ifndef BREAKHIGHC X#ifdef METAWARE X int temp; X#endif X#endif X#ifdef DEBUG X if ( verbose ) X fprintf( stderr, "%5d%c", code, X (col+=6) >= 74 ? (col = 0, '\n') : ' ' ); X#endif /* DEBUG */ X if ( code >= 0 ) X { X#ifdef vax X /* VAX DEPENDENT!! Implementation on other machines is below. X * X * Translation: Insert BITS bits from the argument starting at X * offset bits from the beginning of buf. X */ X 0; /* Work around for pcc -O bug with asm and if stmt */ X asm( "insv 4(ap),r11,r10,(r9)" ); X#else /* not a vax */ X/* X * byte/bit numbering on the VAX is simulated by the following code X */ X /* X * Get to the first byte. X */ X bp += (r_off >> 3); X r_off &= 7; X /* X * Since code is always >= 8 bits, only need to mask the first X * hunk on the left. X */ X#ifndef BREAKHIGHC X#ifdef METAWARE X *bp &= rmask[r_off]; X temp = (code << r_off) & lmask[r_off]; X *bp |= temp; X#else X *bp = (*bp & rmask[r_off]) | (code << r_off) & lmask[r_off]; X#endif X#else X *bp = (*bp & rmask[r_off]) | (code << r_off) & lmask[r_off]; X#endif X bp++; X bits -= (8 - r_off); X code >>= (8 - r_off); X /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */ X if ( bits >= 8 ) X { X *bp++ = code; X code >>= 8; X bits -= 8; X } X /* Last bits. */ X if(bits) X *bp = code; X#endif /* vax */ X offset += n_bits; X if ( offset == (n_bits << 3) ) X { X bp = buf; X bits = n_bits; X bytes_out += bits; X do X putc(*bp++,stdout); X while(--bits); X offset = 0; X } X X /* X * If the next entry is going to be too big for the code size, X * then increase it, if possible. X */ X if ( free_ent > maxcode || (clear_flg > 0)) X { X /* X * Write the whole buffer, because the input side won't X * discover the size increase until after it has read it. X */ X if ( offset > 0 ) X { X if( fwrite( buf, 1, n_bits, stdout ) != n_bits) X writeerr(); X bytes_out += n_bits; X } X offset = 0; X X if ( clear_flg ) X { X maxcode = MAXCODE (n_bits = INIT_BITS); X clear_flg = 0; X } X else X { X n_bits++; X if ( n_bits == maxbits ) X maxcode = maxmaxcode; X else X maxcode = MAXCODE(n_bits); X } X#ifdef DEBUG X if ( debug ) X { X fprintf( stderr, "\nChange to %d bits\n", n_bits ); X col = 0; X } X#endif /* DEBUG */ X } X } else X { X /* X * At EOF, write the rest of the buffer. X */ X if ( offset > 0 ) X fwrite( buf, 1, (offset + 7) / 8, stdout ); X bytes_out += (offset + 7) / 8; X offset = 0; X fflush( stdout ); X#ifdef DEBUG X if ( verbose ) X fprintf( stderr, "\n" ); X#endif /* DEBUG */ X if( ferror( stdout ) ) X writeerr(); X } X} X/* X * Decompress stdin to stdout. This routine adapts to the codes in the X * file building the "string" table on-the-fly; requiring no table to X * be stored in the compressed file. The tables used herein are shared X * with those of the compress() routine. See the definitions above. X */ X Xvoid decompress() { X REGISTER char_type *stackp; X REGISTER int finchar; X REGISTER code_int code, oldcode, incode; X X /* X * As above, initialize the first 256 entries in the table. X */ X maxcode = MAXCODE(n_bits = INIT_BITS); X for ( code = 255; code >= 0; code-- ) { X tab_prefixof(code) = 0; X tab_suffixof(code) = (char_type)code; X } X free_ent = ((block_compress) ? FIRST : 256 ); X X finchar = oldcode = getcode(); X if(oldcode == -1) /* EOF already? */ X return; /* Get out of here */ X putc( (char)finchar,stdout ); /* first code must be 8 bits = char */ X if(ferror(stdout)) /* Crash if can't write */ X writeerr(); X stackp = de_stack; X X while ( (code = getcode()) > -1 ) { X X if ( (code == CLEAR) && block_compress ) { X for ( code = 255; code >= 0; code-- ) X tab_prefixof(code) = 0; X clear_flg = 1; X free_ent = FIRST - 1; X if ( (code = getcode ()) == -1 ) /* O, untimely death! */ X break; X } X incode = code; X /* X * Special case for KwKwK string. X */ X if ( code >= free_ent ) { X *stackp++ = finchar; X code = oldcode; X } X X /* X * Generate output characters in reverse order X */ X#ifdef SIGNED_COMPARE_SLOW X while ( ((unsigned long)code) >= ((unsigned long)256) ) { X#else X while ( code >= 256 ) { X#endif X *stackp++ = tab_suffixof(code); X code = tab_prefixof(code); X } X *stackp++ = finchar = tab_suffixof(code); X X /* X * And put them out in forward order X */ X do X putc ( *--stackp ,stdout); X while ( stackp > de_stack ); X X /* X * Generate the new entry. X */ X if ( (code=free_ent) < maxmaxcode ) X { X tab_prefixof(code) = (unsigned short)oldcode; X tab_suffixof(code) = finchar; X free_ent = code+1; X } X /* X * Remember previous code. X */ X oldcode = incode; X } X fflush( stdout ); X if(ferror(stdout)) X writeerr(); X} X X/***************************************************************** X * TAG( getcode ) X * X * Read one code from the standard input. If EOF, return -1. X * Inputs: X * stdin X * Outputs: X * code or -1 is returned. X */ X Xcode_int Xgetcode() X{ X /* X * On the VAX, it is important to have the REGISTER declarations X * in exactly the order given, or the asm will break. X */ X REGISTER code_int code; X static int offset = 0, size = 0; X static char_type buf[BITS]; X REGISTER int r_off, bits; X REGISTER char_type *bp = buf; X X if ( clear_flg > 0 || offset >= size || free_ent > maxcode ) X { X /* X * If the next entry will be too big for the current code X * size, then we must increase the size. This implies reading X * a new buffer full, too. X */ X if ( free_ent > maxcode ) X { X n_bits++; X if ( n_bits == maxbits ) X maxcode = maxmaxcode; /* won't get any bigger now */ X else X maxcode = MAXCODE(n_bits); X } X if ( clear_flg > 0) X { X maxcode = MAXCODE (n_bits = INIT_BITS); X clear_flg = 0; X } X size = fread( buf, 1, n_bits, stdin ); X if ( size <= 0 ) X return -1; /* end of file */ X offset = 0; X /* Round size down to integral number of codes */ X size = (size << 3) - (n_bits - 1); X } X r_off = offset; X bits = n_bits; X#ifdef vax X asm( "extzv r10,r9,(r8),r11" ); X#else /* not a vax */ X /* X * Get to the first byte. X */ X bp += (r_off >> 3); X r_off &= 7; X /* Get first part (low order bits) */ X#ifdef NO_UCHAR X code = ((*bp++ >> r_off) & rmask[8 - r_off]) & 0xff; X#else X code = (*bp++ >> r_off); X#endif /* NO_UCHAR */ X bits -= (8 - r_off); X r_off = 8 - r_off; /* now, offset into code word */ X /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */ X if ( bits >= 8 ) X { X#ifdef NO_UCHAR X code |= (*bp++ & 0xff) << r_off; X#else X code |= *bp++ << r_off; X#endif /* NO_UCHAR */ X r_off += 8; X bits -= 8; X } X /* high order bits. */ X code |= (*bp & rmask[bits]) << r_off; X#endif /* vax */ X offset += n_bits; X X return code; X} X X#ifndef AZTEC86 Xchar * Xstrrchr(s, c) /* For those who don't have it in libc.a */ XREGISTER char *s, c; X{ X char *p; X for (p = NULL; *s; s++) X if (*s == c) X p = s; X return(p); X} X#endif X#ifdef MINIX Xchar * Xstrrchr(s, c) /* For those who don't have it in libc.a */ XREGISTER char *s, c; X{ X char *p; X for (p = NULL; *s; s++) X if (*s == c) X p = s; X return(p); X} X#endif X X X#ifndef METAWARE X#ifdef DEBUG Xprintcodes() X{ X /* X * Just print out codes from input file. For debugging. X */ X code_int code; X int col = 0, bits; X X bits = n_bits = INIT_BITS; X maxcode = MAXCODE(n_bits); X free_ent = ((block_compress) ? FIRST : 256 ); X while ( ( code = getcode() ) >= 0 ) { X if ( (code == CLEAR) && block_compress ) { X free_ent = FIRST - 1; X clear_flg = 1; X } X else if ( free_ent < maxmaxcode ) X free_ent++; X if ( bits != n_bits ) { X fprintf(stderr, "\nChange to %d bits\n", n_bits ); X bits = n_bits; X col = 0; X } X fprintf(stderr, "%5d%c", code, (col+=6) >= 74 ? (col = 0, '\n') : ' ' ); X } X putc( '\n', stderr ); X exit( 0 ); X} X#ifdef DEBUG2 Xcode_int sorttab[1<<BITS]; /* sorted pointers into htab */ X#define STACK_SIZE 500 Xstatic char stack[STACK_SIZE]; X/* dumptab doesn't use main stack now -prevents distressing crashes */ Xdump_tab() /* dump string table */ X{ X REGISTER int i, first; X REGISTER ent; X int stack_top = STACK_SIZE; X REGISTER c; X X if(do_decomp == 0) { /* compressing */ X REGISTER int flag = 1; X X for(i=0; i<hsize; i++) { /* build sort pointers */ X if((long)htabof(i) >= 0) { X sorttab[codetabof(i)] = i; X } X } X first = block_compress ? FIRST : 256; X for(i = first; i < free_ent; i++) { X fprintf(stderr, "%5d: \"", i); X stack[--stack_top] = '\n'; X stack[--stack_top] = '"'; /* " */ X stack_top = in_stack((int)(htabof(sorttab[i])>>maxbits)&0xff, X stack_top); X for(ent=htabof(sorttab[i]) & ((1<<maxbits)-1); X ent > 256; X ent=htabof(sorttab[ent]) & ((1<<maxbits)-1)) { X stack_top = in_stack((int)(htabof(sorttab[ent]) >> maxbits), X stack_top); X } X stack_top = in_stack(ent, stack_top); X fwrite( &stack[stack_top], 1, STACK_SIZE-stack_top, stderr); X stack_top = STACK_SIZE; X } X } else if(!debug) { /* decompressing */ X X for ( i = 0; i < free_ent; i++ ) { X ent = i; X c = tab_suffixof(ent); X if ( isascii(c) && isprint(c) ) X fprintf( stderr, "%5d: %5d/'%c' \"", X ent, tab_prefixof(ent), c ); X else X fprintf( stderr, "%5d: %5d/\\%03o \"", X ent, tab_prefixof(ent), c ); X stack[--stack_top] = '\n'; X stack[--stack_top] = '"'; /* " */ X for ( ; ent != NULL; X ent = (ent >= FIRST ? tab_prefixof(ent) : NULL) ) { X stack_top = in_stack(tab_suffixof(ent), stack_top); X } X fwrite( &stack[stack_top], 1, STACK_SIZE - stack_top, stderr ); X stack_top = STACK_SIZE; X } X } X} X Xint Xin_stack(c, stack_top) X REGISTER int c, stack_top; X{ X if ( (isascii(c) && isprint(c) && c != '\\') || c == ' ' ) { X stack[--stack_top] = c; X } else { X switch( c ) { X case '\n': stack[--stack_top] = 'n'; break; X case '\t': stack[--stack_top] = 't'; break; X case '\b': stack[--stack_top] = 'b'; break; X case '\f': stack[--stack_top] = 'f'; break; X case '\r': stack[--stack_top] = 'r'; break; X case '\\': stack[--stack_top] = '\\'; break; X default: X stack[--stack_top] = '0' + c % 8; X stack[--stack_top] = '0' + (c / 8) % 8; X stack[--stack_top] = '0' + c / 64; X break; X } X stack[--stack_top] = '\\'; X } X if (stack_top<0) { X fprintf(stderr,"dump_tab stack overflow!!!\n"); X exit(1); X } X return stack_top; X} X#else Xdump_tab() {} X#endif /* DEBUG2 */ X#endif /* DEBUG */ X#endif /* METAWARE */ X Xvoid writeerr() X{ X perror ( ofname ); X unlink ( ofname ); X exit ( 1 ); X} X Xvoid copystat(ifname, ofname) Xchar *ifname, *ofname; X{ X struct stat statbuf; X int mode; X#ifndef AZTEC86 X time_t timep[2]; X#else X unsigned long timep[2]; X#endif X fclose(stdout); X if (stat(ifname, &statbuf)) X { /* Get stat on input file */ X perror(ifname); X return; X } X#ifndef PCDOS X /* meddling with UNIX-style file modes */ X if ((statbuf.st_mode & S_IFMT/*0170000*/) != S_IFREG/*0100000*/) X { X if(quiet) X fprintf(stderr, "%s: ", ifname); X fprintf(stderr, " -- not a regular file: unchanged"); X exit_stat = 1; X } else if (statbuf.st_nlink > 1) X { X if(quiet) X fprintf(stderr, "%s: ", ifname); X fprintf(stderr, " -- has %d other links: unchanged", X statbuf.st_nlink - 1); X exit_stat = 1; X } else X#endif X if (exit_stat == 2 && (!force)) X { /* No compression: remove file.Z */ X if(!quiet) X fprintf(stderr, " -- file unchanged"); X } else X { /* ***** Successful Compression ***** */ X exit_stat = 0; X#ifndef PCDOS X mode = statbuf.st_mode & 07777; X#else X mode = statbuf.st_attr & 07777; X#endif X if (chmod(ofname, mode)) /* Copy modes */ X perror(ofname); X#ifndef PCDOS X chown(ofname, statbuf.st_uid, statbuf.st_gid); /* Copy ownership */ X timep[0] = statbuf.st_atime; X timep[1] = statbuf.st_mtime; X#else X timep[0] = statbuf.st_mtime; X timep[1] = statbuf.st_mtime; X#endif X utime(ofname, timep); /* Update last accessed and modified times */ X if (unlink(ifname)) /* Remove input file */ X perror(ifname); X if(!quiet) X fprintf(stderr, " -- replaced with %s", ofname); X return; /* Successful return */ X } X X /* Unsuccessful return -- one of the tests failed */ X if (unlink(ofname)) X perror(ofname); X} X/* X * This routine returns 1 if we are running in the foreground and stderr X * is a tty. X */ Xint foreground() X{ X#ifndef METAWARE X if(bgnd_flag) { /* background? */ X return(0); X } else { /* foreground */ X#endif X if(isatty(2)) { /* and stderr is a tty */ X return(1); X } else { X return(0); X } X#ifndef METAWARE X } X#endif X} X#ifndef METAWARE Xvoid onintr ( ) X{ X (void)signal(SIGINT,SIG_IGN); X unlink ( ofname ); X exit ( 1 ); X} X Xvoid oops ( ) /* wild pointer -- assume bad input */ X{ X (void)signal(SIGSEGV,SIG_IGN); X if ( do_decomp == 1 ) X fprintf ( stderr, "uncompress: corrupt input\n" ); X unlink ( ofname ); X exit ( 1 ); X} X#endif Xvoid cl_block () /* table clear for block compress */ X{ X REGISTER long int rat; X X checkpoint = in_count + CHECK_GAP; X#ifdef DEBUG X if ( debug ) { X fprintf ( stderr, "count: %ld, ratio: ", in_count ); X prratio ( stderr, in_count, bytes_out ); X fprintf ( stderr, "\n"); X } X#endif /* DEBUG */ X X if(in_count > 0x007fffff) { /* shift will overflow */ X rat = bytes_out >> 8; X if(rat == 0) { /* Don't divide by zero */ X rat = 0x7fffffff; X } else { X rat = in_count / rat; X } X } else { X rat = (in_count << 8) / bytes_out; /* 8 fractional bits */ X } X if ( rat > ratio ) { X ratio = rat; X } else { X ratio = 0; X#ifdef DEBUG X if(verbose) X dump_tab(); /* dump string table */ X#endif X cl_hash ( (count_int) hsize ); X free_ent = FIRST; X clear_flg = 1; X output ( (code_int) CLEAR ); X#ifdef DEBUG X if(debug) X fprintf ( stderr, "clear\n" ); X#endif /* DEBUG */ X } X} X Xvoid cl_hash(hsize) /* reset code table */ X REGISTER count_int hsize; X{ X#ifdef AZTEC86 X#ifdef PCDOS X /* this function only in PC-DOS lib, not in MINIX lib */ X memset(htab,-1,(int)(hsize * sizeof(count_int))); X#else X/* MINIX and all non-PC machines do it this way */ X#ifndef XENIX_16 /* Normal machine */ X REGISTER count_int *htab_p = htab+hsize; X#else X REGISTER j; X REGISTER long k = hsize; X REGISTER count_int *htab_p; X#endif X REGISTER long i; X REGISTER long m1 = -1; X X#ifdef XENIX_16 X for(j=0; j<=8 && k>=0; j++,k-=8192) X { X i = 8192; X if(k < 8192) X { X i = k; X } X htab_p = &(htab[j][i]); X i -= 16; X if(i > 0) X { X#else X i = hsize - 16; X#endif X do X { /* might use Sys V memset(3) here */ X *(htab_p-16) = m1; X *(htab_p-15) = m1; X *(htab_p-14) = m1; X *(htab_p-13) = m1; X *(htab_p-12) = m1; X *(htab_p-11) = m1; X *(htab_p-10) = m1; X *(htab_p-9) = m1; X *(htab_p-8) = m1; X *(htab_p-7) = m1; X *(htab_p-6) = m1; X *(htab_p-5) = m1; X *(htab_p-4) = m1; X *(htab_p-3) = m1; X *(htab_p-2) = m1; X *(htab_p-1) = m1; X htab_p -= 16; X } while ((i -= 16) >= 0); X#ifdef XENIX_16 X } X } X#endif X for ( i += 16; i > 0; i-- ) X *--htab_p = m1; X#endif X#endif X} X Xvoid prratio(stream, num, den) XFILE *stream; Xlong int num, den; X{ X REGISTER int q; /* Doesn't need to be long */ X if(num > 214748L) X { /* 2147483647/10000 */ X q = (int)(num / (den / 10000L)); X } else X { X q = (int)(10000L * num / den); /* Long calculations, though */ X } X if (q < 0) X { X putc('-', stream); X q = -q; X } X fprintf(stream, "%d.%02d%%", q / 100, q % 100); X} X Xvoid version() X{ X fprintf(stderr, "%s\n", rcs_ident); X fprintf(stderr, "Options: "); X#ifdef vax X fprintf(stderr, "vax, "); X#endif X#ifdef MINIX X fprintf(stderr, "MINIX, "); X#endif X#ifdef NO_UCHAR X fprintf(stderr, "NO_UCHAR, "); X#endif X#ifdef SIGNED_COMPARE_SLOW X fprintf(stderr, "SIGNED_COMPARE_SLOW, "); X#endif X#ifdef XENIX_16 X fprintf(stderr, "XENIX_16, "); X#endif X#ifdef COMPATIBLE X fprintf(stderr, "COMPATIBLE, "); X#endif X#ifdef DEBUG X fprintf(stderr, "DEBUG, "); X#endif X#ifdef BSD4_2 X fprintf(stderr, "BSD4_2, "); X#endif X fprintf(stderr, "BITS = %d\n", BITS); X} X/* End of text from uok.UUCP:net.sources */ X EOF