compress.c.original 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  1. /*
  2. * Copyright (c) 1985, 1986 The Regents of the University of California.
  3. * All rights reserved.
  4. *
  5. * This code is derived from software contributed to Berkeley by
  6. * James A. Woods, derived from original work by Spencer Thomas
  7. * and Joseph Orost.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. All advertising materials mentioning features or use of this software
  18. * must display the following acknowledgement:
  19. * This product includes software developed by the University of
  20. * California, Berkeley and its contributors.
  21. * 4. Neither the name of the University nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35. * SUCH DAMAGE.
  36. */
  37. #ifndef lint
  38. char copyright[] =
  39. "@(#) Copyright (c) 1985, 1986 The Regents of the University of California.\n\
  40. All rights reserved.\n";
  41. #endif /* not lint */
  42. #ifndef lint
  43. static char sccsid[] = "@(#)compress.c 5.19 (Berkeley) 3/18/91";
  44. #endif /* not lint */
  45. /*
  46. * compress.c - File compression ala IEEE Computer, June 1984.
  47. *
  48. * Authors: Spencer W. Thomas (decvax!utah-cs!thomas)
  49. * Jim McKie (decvax!mcvax!jim)
  50. * Steve Davies (decvax!vax135!petsd!peora!srd)
  51. * Ken Turkowski (decvax!decwrl!turtlevax!ken)
  52. * James A. Woods (decvax!ihnp4!ames!jaw)
  53. * Joe Orost (decvax!vax135!petsd!joe)
  54. */
  55. #include <sys/param.h>
  56. #include <sys/stat.h>
  57. #include <signal.h>
  58. #include <utime.h>
  59. #include <errno.h>
  60. #include <unistd.h>
  61. #include <stdio.h>
  62. #include <ctype.h>
  63. #include <stdlib.h>
  64. #include <string.h>
  65. /*
  66. * Set USERMEM to the maximum amount of physical user memory available
  67. * in bytes. USERMEM is used to determine the maximum BITS that can be used
  68. * for compression.
  69. *
  70. * SACREDMEM is the amount of physical memory saved for others; compress
  71. * will hog the rest.
  72. */
  73. #ifndef SACREDMEM
  74. #define SACREDMEM 0
  75. #endif
  76. #ifndef USERMEM
  77. # define USERMEM 450000 /* default user memory */
  78. #endif
  79. #ifdef pdp11
  80. # define BITS 12 /* max bits/code for 16-bit machine */
  81. # define NO_UCHAR /* also if "unsigned char" functions as signed char */
  82. # undef USERMEM
  83. #endif /* pdp11 */ /* don't forget to compile with -i */
  84. #ifdef USERMEM
  85. # if USERMEM >= (433484+SACREDMEM)
  86. # define PBITS 16
  87. # else
  88. # if USERMEM >= (229600+SACREDMEM)
  89. # define PBITS 15
  90. # else
  91. # if USERMEM >= (127536+SACREDMEM)
  92. # define PBITS 14
  93. # else
  94. # if USERMEM >= (73464+SACREDMEM)
  95. # define PBITS 13
  96. # else
  97. # define PBITS 12
  98. # endif
  99. # endif
  100. # endif
  101. # endif
  102. # undef USERMEM
  103. #endif /* USERMEM */
  104. #ifdef PBITS /* Preferred BITS for this memory size */
  105. # ifndef BITS
  106. # define BITS PBITS
  107. # endif BITS
  108. #endif /* PBITS */
  109. #if BITS == 16
  110. # define HSIZE 69001 /* 95% occupancy */
  111. #endif
  112. #if BITS == 15
  113. # define HSIZE 35023 /* 94% occupancy */
  114. #endif
  115. #if BITS == 14
  116. # define HSIZE 18013 /* 91% occupancy */
  117. #endif
  118. #if BITS == 13
  119. # define HSIZE 9001 /* 91% occupancy */
  120. #endif
  121. #if BITS <= 12
  122. # define HSIZE 5003 /* 80% occupancy */
  123. #endif
  124. /*
  125. * a code_int must be able to hold 2**BITS values of type int, and also -1
  126. */
  127. #if BITS > 15
  128. typedef long int code_int;
  129. #else
  130. typedef int code_int;
  131. #endif
  132. #ifdef SIGNED_COMPARE_SLOW
  133. typedef unsigned long int count_int;
  134. typedef unsigned short int count_short;
  135. #else
  136. typedef long int count_int;
  137. #endif
  138. #ifdef NO_UCHAR
  139. typedef char char_type;
  140. #else
  141. typedef unsigned char char_type;
  142. #endif /* UCHAR */
  143. char_type magic_header[] = { "\037\235" }; /* 1F 9D */
  144. /* Defines for third byte of header */
  145. #define BIT_MASK 0x1f
  146. #define BLOCK_MASK 0x80
  147. /* Masks 0x40 and 0x20 are free. I think 0x20 should mean that there is
  148. a fourth header byte (for expansion).
  149. */
  150. #define INIT_BITS 9 /* initial number of bits/code */
  151. int n_bits; /* number of bits/code */
  152. int maxbits = BITS; /* user settable max # bits/code */
  153. code_int maxcode; /* maximum code, given n_bits */
  154. code_int maxmaxcode = 1 << BITS; /* should NEVER generate this code */
  155. #ifdef COMPATIBLE /* But wrong! */
  156. # define MAXCODE(n_bits) (1 << (n_bits) - 1)
  157. #else
  158. # define MAXCODE(n_bits) ((1 << (n_bits)) - 1)
  159. #endif /* COMPATIBLE */
  160. count_int htab [HSIZE];
  161. unsigned short codetab [HSIZE];
  162. #define htabof(i) htab[i]
  163. #define codetabof(i) codetab[i]
  164. code_int hsize = HSIZE; /* for dynamic table sizing */
  165. count_int fsize;
  166. /*
  167. * To save much memory, we overlay the table used by compress() with those
  168. * used by decompress(). The tab_prefix table is the same size and type
  169. * as the codetab. The tab_suffix table needs 2**BITS characters. We
  170. * get this from the beginning of htab. The output stack uses the rest
  171. * of htab, and contains characters. There is plenty of room for any
  172. * possible stack (stack used to be 8000 characters).
  173. */
  174. #define tab_prefixof(i) codetabof(i)
  175. # define tab_suffixof(i) ((char_type *)(htab))[i]
  176. # define de_stack ((char_type *)&tab_suffixof(1<<BITS))
  177. code_int free_ent = 0; /* first unused entry */
  178. int exit_stat = 0; /* per-file status */
  179. int perm_stat = 0; /* permanent status */
  180. code_int getcode();
  181. int nomagic = 0; /* Use a 3-byte magic number header, unless old file */
  182. int zcat_flg = 0; /* Write output on stdout, suppress messages */
  183. int precious = 1; /* Don't unlink output file on interrupt */
  184. int quiet = 1; /* don't tell me about compression */
  185. /*
  186. * block compression parameters -- after all codes are used up,
  187. * and compression rate changes, start over.
  188. */
  189. int block_compress = BLOCK_MASK;
  190. int clear_flg = 0;
  191. long int ratio = 0;
  192. #define CHECK_GAP 10000 /* ratio check interval */
  193. count_int checkpoint = CHECK_GAP;
  194. /*
  195. * the next two codes should not be changed lightly, as they must not
  196. * lie within the contiguous general code space.
  197. */
  198. #define FIRST 257 /* first free entry */
  199. #define CLEAR 256 /* table clear output code */
  200. int force = 0;
  201. char ofname [100];
  202. #ifdef DEBUG
  203. int debug, verbose;
  204. #endif
  205. sig_t oldint;
  206. int bgnd_flag;
  207. int do_decomp = 0;
  208. /*-
  209. * Algorithm from "A Technique for High Performance Data Compression",
  210. * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.
  211. *
  212. * Usage: compress [-dfvc] [-b bits] [file ...]
  213. * Inputs:
  214. * -d: If given, decompression is done instead.
  215. *
  216. * -c: Write output on stdout, don't remove original.
  217. *
  218. * -b: Parameter limits the max number of bits/code.
  219. *
  220. * -f: Forces output file to be generated, even if one already
  221. * exists, and even if no space is saved by compressing.
  222. * If -f is not used, the user will be prompted if stdin is
  223. * a tty, otherwise, the output file will not be overwritten.
  224. *
  225. * -v: Write compression statistics
  226. *
  227. * file ...: Files to be compressed. If none specified, stdin
  228. * is used.
  229. * Outputs:
  230. * file.Z: Compressed form of file with same mode, owner, and utimes
  231. * or stdout (if stdin used as input)
  232. *
  233. * Assumptions:
  234. * When filenames are given, replaces with the compressed version
  235. * (.Z suffix) only if the file decreases in size.
  236. * Algorithm:
  237. * Modified Lempel-Ziv method (LZW). Basically finds common
  238. * substrings and replaces them with a variable size code. This is
  239. * deterministic, and can be done on the fly. Thus, the decompression
  240. * procedure needs no input table, but tracks the way the table was built.
  241. */
  242. main(argc, argv)
  243. int argc;
  244. char **argv;
  245. {
  246. extern int optind;
  247. extern char *optarg;
  248. struct stat statbuf;
  249. int ch, overwrite;
  250. char **filelist, **fileptr, *cp, tempname[MAXPATHLEN];
  251. void onintr(), oops();
  252. /* This bg check only works for sh. */
  253. if ((oldint = signal(SIGINT, SIG_IGN)) != SIG_IGN) {
  254. (void)signal(SIGINT, onintr);
  255. (void)signal(SIGSEGV, oops); /* XXX */
  256. }
  257. bgnd_flag = oldint != SIG_DFL;
  258. #ifdef COMPATIBLE
  259. nomagic = 1; /* Original didn't have a magic number */
  260. #endif
  261. if (cp = rindex(argv[0], '/'))
  262. ++cp;
  263. else
  264. cp = argv[0];
  265. if (strcmp(cp, "uncompress") == 0)
  266. do_decomp = 1;
  267. else if(strcmp(cp, "zcat") == 0) {
  268. do_decomp = 1;
  269. zcat_flg = 1;
  270. }
  271. /*
  272. * -b maxbits => maxbits.
  273. * -C => generate output compatible with compress 2.0.
  274. * -c => cat all output to stdout
  275. * -D => debug
  276. * -d => do_decomp
  277. * -f => force overwrite of output file
  278. * -n => no header: useful to uncompress old files
  279. * -V => print Version; debug verbose
  280. * -v => unquiet
  281. */
  282. overwrite = 0;
  283. #ifdef DEBUG
  284. while ((ch = getopt(argc, argv, "b:CcDdfnVv")) != EOF)
  285. #else
  286. while ((ch = getopt(argc, argv, "b:Ccdfnv")) != EOF)
  287. #endif
  288. switch(ch) {
  289. case 'b':
  290. maxbits = atoi(optarg);
  291. break;
  292. case 'C':
  293. block_compress = 0;
  294. break;
  295. case 'c':
  296. zcat_flg = 1;
  297. break;
  298. #ifdef DEBUG
  299. case 'D':
  300. debug = 1;
  301. break;
  302. #endif
  303. case 'd':
  304. do_decomp = 1;
  305. break;
  306. case 'f':
  307. overwrite = 1;
  308. force = 1;
  309. break;
  310. case 'n':
  311. nomagic = 1;
  312. break;
  313. case 'q':
  314. quiet = 1;
  315. break;
  316. #ifdef DEBUG
  317. case 'V':
  318. verbose = 1;
  319. break;
  320. #endif
  321. case 'v':
  322. quiet = 0;
  323. break;
  324. case '?':
  325. default:
  326. usage();
  327. }
  328. argc -= optind;
  329. argv += optind;
  330. if (maxbits < INIT_BITS)
  331. maxbits = INIT_BITS;
  332. if (maxbits > BITS)
  333. maxbits = BITS;
  334. maxmaxcode = 1 << maxbits;
  335. /* Build useless input file list. */
  336. filelist = fileptr = (char **)(malloc(argc * sizeof(*argv)));
  337. while (*argv)
  338. *fileptr++ = *argv++;
  339. *fileptr = NULL;
  340. if (*filelist != NULL) {
  341. for (fileptr = filelist; *fileptr; fileptr++) {
  342. exit_stat = 0;
  343. if (do_decomp) { /* DECOMPRESSION */
  344. /* Check for .Z suffix */
  345. if (strcmp(*fileptr + strlen(*fileptr) - 2, ".Z") != 0) {
  346. /* No .Z: tack one on */
  347. strcpy(tempname, *fileptr);
  348. strcat(tempname, ".Z");
  349. *fileptr = tempname;
  350. }
  351. /* Open input file */
  352. if ((freopen(*fileptr, "r", stdin)) == NULL) {
  353. perror(*fileptr);
  354. perm_stat = 1;
  355. continue;
  356. }
  357. /* Check the magic number */
  358. if (nomagic == 0) {
  359. if ((getchar() != (magic_header[0] & 0xFF))
  360. || (getchar() != (magic_header[1] & 0xFF))) {
  361. fprintf(stderr, "%s: not in compressed format\n",
  362. *fileptr);
  363. continue;
  364. }
  365. maxbits = getchar(); /* set -b from file */
  366. block_compress = maxbits & BLOCK_MASK;
  367. maxbits &= BIT_MASK;
  368. maxmaxcode = 1 << maxbits;
  369. if(maxbits > BITS) {
  370. fprintf(stderr,
  371. "%s: compressed with %d bits, can only handle %d bits\n",
  372. *fileptr, maxbits, BITS);
  373. continue;
  374. }
  375. }
  376. /* Generate output filename */
  377. strcpy(ofname, *fileptr);
  378. ofname[strlen(*fileptr) - 2] = '\0'; /* Strip off .Z */
  379. } else { /* COMPRESSION */
  380. if (strcmp(*fileptr + strlen(*fileptr) - 2, ".Z") == 0) {
  381. fprintf(stderr, "%s: already has .Z suffix -- no change\n",
  382. *fileptr);
  383. continue;
  384. }
  385. /* Open input file */
  386. if ((freopen(*fileptr, "r", stdin)) == NULL) {
  387. perror(*fileptr);
  388. perm_stat = 1;
  389. continue;
  390. }
  391. stat ( *fileptr, &statbuf );
  392. fsize = (long) statbuf.st_size;
  393. /*
  394. * tune hash table size for small files -- ad hoc,
  395. * but the sizes match earlier #defines, which
  396. * serve as upper bounds on the number of output codes.
  397. */
  398. hsize = HSIZE;
  399. if ( fsize < (1 << 12) )
  400. hsize = MIN ( 5003, HSIZE );
  401. else if ( fsize < (1 << 13) )
  402. hsize = MIN ( 9001, HSIZE );
  403. else if ( fsize < (1 << 14) )
  404. hsize = MIN ( 18013, HSIZE );
  405. else if ( fsize < (1 << 15) )
  406. hsize = MIN ( 35023, HSIZE );
  407. else if ( fsize < 47000 )
  408. hsize = MIN ( 50021, HSIZE );
  409. /* Generate output filename */
  410. strcpy(ofname, *fileptr);
  411. strcat(ofname, ".Z");
  412. }
  413. /* Check for overwrite of existing file */
  414. if (overwrite == 0 && zcat_flg == 0) {
  415. if (stat(ofname, &statbuf) == 0) {
  416. char response[2];
  417. response[0] = 'n';
  418. fprintf(stderr, "%s already exists;", ofname);
  419. if (bgnd_flag == 0 && isatty(2)) {
  420. fprintf(stderr, " do you wish to overwrite %s (y or n)? ",
  421. ofname);
  422. fflush(stderr);
  423. read(2, response, 2);
  424. while (response[1] != '\n') {
  425. if (read(2, response+1, 1) < 0) { /* Ack! */
  426. perror("stderr"); break;
  427. }
  428. }
  429. }
  430. if (response[0] != 'y') {
  431. fprintf(stderr, "\tnot overwritten\n");
  432. continue;
  433. }
  434. }
  435. }
  436. if(zcat_flg == 0) { /* Open output file */
  437. if (freopen(ofname, "w", stdout) == NULL) {
  438. perror(ofname);
  439. perm_stat = 1;
  440. continue;
  441. }
  442. precious = 0;
  443. if(!quiet)
  444. fprintf(stderr, "%s: ", *fileptr);
  445. }
  446. /* Actually do the compression/decompression */
  447. if (do_decomp == 0) compress();
  448. #ifndef DEBUG
  449. else decompress();
  450. #else
  451. else if (debug == 0) decompress();
  452. else printcodes();
  453. if (verbose) dump_tab();
  454. #endif /* DEBUG */
  455. if(zcat_flg == 0) {
  456. copystat(*fileptr, ofname); /* Copy stats */
  457. precious = 1;
  458. if((exit_stat == 1) || (!quiet))
  459. putc('\n', stderr);
  460. }
  461. }
  462. } else { /* Standard input */
  463. if (do_decomp == 0) {
  464. compress();
  465. #ifdef DEBUG
  466. if(verbose) dump_tab();
  467. #endif /* DEBUG */
  468. if(!quiet)
  469. putc('\n', stderr);
  470. } else {
  471. /* Check the magic number */
  472. if (nomagic == 0) {
  473. if ((getchar()!=(magic_header[0] & 0xFF))
  474. || (getchar()!=(magic_header[1] & 0xFF))) {
  475. fprintf(stderr, "stdin: not in compressed format\n");
  476. exit(1);
  477. }
  478. maxbits = getchar(); /* set -b from file */
  479. block_compress = maxbits & BLOCK_MASK;
  480. maxbits &= BIT_MASK;
  481. maxmaxcode = 1 << maxbits;
  482. fsize = 100000; /* assume stdin large for USERMEM */
  483. if(maxbits > BITS) {
  484. fprintf(stderr,
  485. "stdin: compressed with %d bits, can only handle %d bits\n",
  486. maxbits, BITS);
  487. exit(1);
  488. }
  489. }
  490. #ifndef DEBUG
  491. decompress();
  492. #else
  493. if (debug == 0) decompress();
  494. else printcodes();
  495. if (verbose) dump_tab();
  496. #endif /* DEBUG */
  497. }
  498. }
  499. exit(perm_stat ? perm_stat : exit_stat);
  500. }
  501. static int offset;
  502. long int in_count = 1; /* length of input */
  503. long int bytes_out; /* length of compressed output */
  504. long int out_count = 0; /* # of codes output (for debugging) */
  505. /*
  506. * compress stdin to stdout
  507. *
  508. * Algorithm: use open addressing double hashing (no chaining) on the
  509. * prefix code / next character combination. We do a variant of Knuth's
  510. * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
  511. * secondary probe. Here, the modular division first probe is gives way
  512. * to a faster exclusive-or manipulation. Also do block compression with
  513. * an adaptive reset, whereby the code table is cleared when the compression
  514. * ratio decreases, but after the table fills. The variable-length output
  515. * codes are re-sized at this point, and a special CLEAR code is generated
  516. * for the decompressor. Late addition: construct the table according to
  517. * file size for noticeable speed improvement on small files. Please direct
  518. * questions about this implementation to ames!jaw.
  519. */
  520. compress()
  521. {
  522. register long fcode;
  523. register code_int i = 0;
  524. register int c;
  525. register code_int ent;
  526. register int disp;
  527. register code_int hsize_reg;
  528. register int hshift;
  529. #ifndef COMPATIBLE
  530. if (nomagic == 0) {
  531. putchar(magic_header[0]);
  532. putchar(magic_header[1]);
  533. putchar((char)(maxbits | block_compress));
  534. if(ferror(stdout))
  535. writeerr();
  536. }
  537. #endif /* COMPATIBLE */
  538. offset = 0;
  539. bytes_out = 3; /* includes 3-byte header mojo */
  540. out_count = 0;
  541. clear_flg = 0;
  542. ratio = 0;
  543. in_count = 1;
  544. checkpoint = CHECK_GAP;
  545. maxcode = MAXCODE(n_bits = INIT_BITS);
  546. free_ent = ((block_compress) ? FIRST : 256 );
  547. ent = getchar ();
  548. hshift = 0;
  549. for ( fcode = (long) hsize; fcode < 65536L; fcode *= 2L )
  550. hshift++;
  551. hshift = 8 - hshift; /* set hash code range bound */
  552. hsize_reg = hsize;
  553. cl_hash( (count_int) hsize_reg); /* clear hash table */
  554. #ifdef SIGNED_COMPARE_SLOW
  555. while ( (c = getchar()) != (unsigned) EOF ) {
  556. #else
  557. while ( (c = getchar()) != EOF ) {
  558. #endif
  559. in_count++;
  560. fcode = (long) (((long) c << maxbits) + ent);
  561. i = ((c << hshift) ^ ent); /* xor hashing */
  562. if ( htabof (i) == fcode ) {
  563. ent = codetabof (i);
  564. continue;
  565. } else if ( (long)htabof (i) < 0 ) /* empty slot */
  566. goto nomatch;
  567. disp = hsize_reg - i; /* secondary hash (after G. Knott) */
  568. if ( i == 0 )
  569. disp = 1;
  570. probe:
  571. if ( (i -= disp) < 0 )
  572. i += hsize_reg;
  573. if ( htabof (i) == fcode ) {
  574. ent = codetabof (i);
  575. continue;
  576. }
  577. if ( (long)htabof (i) > 0 )
  578. goto probe;
  579. nomatch:
  580. output ( (code_int) ent );
  581. out_count++;
  582. ent = c;
  583. #ifdef SIGNED_COMPARE_SLOW
  584. if ( (unsigned) free_ent < (unsigned) maxmaxcode) {
  585. #else
  586. if ( free_ent < maxmaxcode ) {
  587. #endif
  588. codetabof (i) = free_ent++; /* code -> hashtable */
  589. htabof (i) = fcode;
  590. }
  591. else if ( (count_int)in_count >= checkpoint && block_compress )
  592. cl_block ();
  593. }
  594. /*
  595. * Put out the final code.
  596. */
  597. output( (code_int)ent );
  598. out_count++;
  599. output( (code_int)-1 );
  600. /*
  601. * Print out stats on stderr
  602. */
  603. if(zcat_flg == 0 && !quiet) {
  604. #ifdef DEBUG
  605. fprintf( stderr,
  606. "%ld chars in, %ld codes (%ld bytes) out, compression factor: ",
  607. in_count, out_count, bytes_out );
  608. prratio( stderr, in_count, bytes_out );
  609. fprintf( stderr, "\n");
  610. fprintf( stderr, "\tCompression as in compact: " );
  611. prratio( stderr, in_count-bytes_out, in_count );
  612. fprintf( stderr, "\n");
  613. fprintf( stderr, "\tLargest code (of last block) was %d (%d bits)\n",
  614. free_ent - 1, n_bits );
  615. #else /* !DEBUG */
  616. fprintf( stderr, "Compression: " );
  617. prratio( stderr, in_count-bytes_out, in_count );
  618. #endif /* DEBUG */
  619. }
  620. if(bytes_out > in_count) /* exit(2) if no savings */
  621. exit_stat = 2;
  622. return;
  623. }
  624. /*-
  625. * Output the given code.
  626. * Inputs:
  627. * code: A n_bits-bit integer. If == -1, then EOF. This assumes
  628. * that n_bits =< (long)wordsize - 1.
  629. * Outputs:
  630. * Outputs code to the file.
  631. * Assumptions:
  632. * Chars are 8 bits long.
  633. * Algorithm:
  634. * Maintain a BITS character long buffer (so that 8 codes will
  635. * fit in it exactly). Use the VAX insv instruction to insert each
  636. * code in turn. When the buffer fills up empty it and start over.
  637. */
  638. static char buf[BITS];
  639. #ifndef vax
  640. char_type lmask[9] = {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
  641. char_type rmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
  642. #endif /* vax */
  643. output( code )
  644. code_int code;
  645. {
  646. #ifdef DEBUG
  647. static int col = 0;
  648. #endif /* DEBUG */
  649. /*
  650. * On the VAX, it is important to have the register declarations
  651. * in exactly the order given, or the asm will break.
  652. */
  653. register int r_off = offset, bits= n_bits;
  654. register char * bp = buf;
  655. #ifdef DEBUG
  656. if ( verbose )
  657. fprintf( stderr, "%5d%c", code,
  658. (col+=6) >= 74 ? (col = 0, '\n') : ' ' );
  659. #endif /* DEBUG */
  660. if ( code >= 0 ) {
  661. #if defined(vax) && !defined(__GNUC__)
  662. /*
  663. * VAX and PCC DEPENDENT!! Implementation on other machines is
  664. * below.
  665. *
  666. * Translation: Insert BITS bits from the argument starting at
  667. * offset bits from the beginning of buf.
  668. */
  669. 0; /* Work around for pcc -O bug with asm and if stmt */
  670. asm( "insv 4(ap),r11,r10,(r9)" );
  671. #else
  672. /*
  673. * byte/bit numbering on the VAX is simulated by the following code
  674. */
  675. /*
  676. * Get to the first byte.
  677. */
  678. bp += (r_off >> 3);
  679. r_off &= 7;
  680. /*
  681. * Since code is always >= 8 bits, only need to mask the first
  682. * hunk on the left.
  683. */
  684. *bp = (*bp & rmask[r_off]) | (code << r_off) & lmask[r_off];
  685. bp++;
  686. bits -= (8 - r_off);
  687. code >>= 8 - r_off;
  688. /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
  689. if ( bits >= 8 ) {
  690. *bp++ = code;
  691. code >>= 8;
  692. bits -= 8;
  693. }
  694. /* Last bits. */
  695. if(bits)
  696. *bp = code;
  697. #endif /* vax */
  698. offset += n_bits;
  699. if ( offset == (n_bits << 3) ) {
  700. bp = buf;
  701. bits = n_bits;
  702. bytes_out += bits;
  703. do {
  704. putchar(*bp++);
  705. if (ferror(stdout))
  706. writeerr();
  707. } while(--bits);
  708. offset = 0;
  709. }
  710. /*
  711. * If the next entry is going to be too big for the code size,
  712. * then increase it, if possible.
  713. */
  714. if ( free_ent > maxcode || (clear_flg > 0))
  715. {
  716. /*
  717. * Write the whole buffer, because the input side won't
  718. * discover the size increase until after it has read it.
  719. */
  720. if ( offset > 0 ) {
  721. if( fwrite( buf, 1, n_bits, stdout ) != n_bits)
  722. writeerr();
  723. bytes_out += n_bits;
  724. }
  725. offset = 0;
  726. if ( clear_flg ) {
  727. maxcode = MAXCODE (n_bits = INIT_BITS);
  728. clear_flg = 0;
  729. }
  730. else {
  731. n_bits++;
  732. if ( n_bits == maxbits )
  733. maxcode = maxmaxcode;
  734. else
  735. maxcode = MAXCODE(n_bits);
  736. }
  737. #ifdef DEBUG
  738. if ( debug ) {
  739. fprintf( stderr, "\nChange to %d bits\n", n_bits );
  740. col = 0;
  741. }
  742. #endif /* DEBUG */
  743. }
  744. } else {
  745. /*
  746. * At EOF, write the rest of the buffer.
  747. */
  748. if ( offset > 0 ) {
  749. offset = (offset + 7) / 8;
  750. if( fwrite( buf, 1, offset, stdout ) != offset )
  751. writeerr();
  752. bytes_out += offset;
  753. }
  754. offset = 0;
  755. (void)fflush( stdout );
  756. if( ferror( stdout ) )
  757. writeerr();
  758. #ifdef DEBUG
  759. if ( verbose )
  760. fprintf( stderr, "\n" );
  761. #endif
  762. }
  763. }
  764. /*
  765. * Decompress stdin to stdout. This routine adapts to the codes in the
  766. * file building the "string" table on-the-fly; requiring no table to
  767. * be stored in the compressed file. The tables used herein are shared
  768. * with those of the compress() routine. See the definitions above.
  769. */
  770. decompress() {
  771. register char_type *stackp;
  772. register int finchar;
  773. register code_int code, oldcode, incode;
  774. int n, nwritten, offset; /* Variables for buffered write */
  775. char buff[BUFSIZ]; /* Buffer for buffered write */
  776. /*
  777. * As above, initialize the first 256 entries in the table.
  778. */
  779. maxcode = MAXCODE(n_bits = INIT_BITS);
  780. for ( code = 255; code >= 0; code-- ) {
  781. tab_prefixof(code) = 0;
  782. tab_suffixof(code) = (char_type)code;
  783. }
  784. free_ent = ((block_compress) ? FIRST : 256 );
  785. finchar = oldcode = getcode();
  786. if(oldcode == -1) /* EOF already? */
  787. return; /* Get out of here */
  788. /* first code must be 8 bits = char */
  789. n=0;
  790. buff[n++] = (char)finchar;
  791. stackp = de_stack;
  792. while ( (code = getcode()) > -1 ) {
  793. if ( (code == CLEAR) && block_compress ) {
  794. for ( code = 255; code >= 0; code-- )
  795. tab_prefixof(code) = 0;
  796. clear_flg = 1;
  797. free_ent = FIRST - 1;
  798. if ( (code = getcode ()) == -1 ) /* O, untimely death! */
  799. break;
  800. }
  801. incode = code;
  802. /*
  803. * Special case for KwKwK string.
  804. */
  805. if ( code >= free_ent ) {
  806. *stackp++ = finchar;
  807. code = oldcode;
  808. }
  809. /*
  810. * Generate output characters in reverse order
  811. */
  812. #ifdef SIGNED_COMPARE_SLOW
  813. while ( ((unsigned long)code) >= ((unsigned long)256) ) {
  814. #else
  815. while ( code >= 256 ) {
  816. #endif
  817. *stackp++ = tab_suffixof(code);
  818. code = tab_prefixof(code);
  819. }
  820. *stackp++ = finchar = tab_suffixof(code);
  821. /*
  822. * And put them out in forward order
  823. */
  824. do {
  825. /*
  826. * About 60% of the time is spent in the putchar() call
  827. * that appeared here. It was originally
  828. * putchar ( *--stackp );
  829. * If we buffer the writes ourselves, we can go faster (about
  830. * 30%).
  831. *
  832. * At this point, the next line is the next *big* time
  833. * sink in the code. It takes up about 10% of the time.
  834. */
  835. buff[n++] = *--stackp;
  836. if (n == BUFSIZ) {
  837. offset = 0;
  838. do {
  839. nwritten = write(fileno(stdout), &buff[offset], n);
  840. if (nwritten < 0)
  841. writeerr();
  842. offset += nwritten;
  843. } while ((n -= nwritten) > 0);
  844. }
  845. } while ( stackp > de_stack );
  846. /*
  847. * Generate the new entry.
  848. */
  849. if ( (code=free_ent) < maxmaxcode ) {
  850. tab_prefixof(code) = (unsigned short)oldcode;
  851. tab_suffixof(code) = finchar;
  852. free_ent = code+1;
  853. }
  854. /*
  855. * Remember previous code.
  856. */
  857. oldcode = incode;
  858. }
  859. /*
  860. * Flush the stuff remaining in our buffer...
  861. */
  862. offset = 0;
  863. while (n > 0) {
  864. nwritten = write(fileno(stdout), &buff[offset], n);
  865. if (nwritten < 0)
  866. writeerr();
  867. offset += nwritten;
  868. n -= nwritten;
  869. }
  870. }
  871. /*-
  872. * Read one code from the standard input. If EOF, return -1.
  873. * Inputs:
  874. * stdin
  875. * Outputs:
  876. * code or -1 is returned.
  877. */
  878. code_int
  879. getcode() {
  880. /*
  881. * On the VAX, it is important to have the register declarations
  882. * in exactly the order given, or the asm will break.
  883. */
  884. register code_int code;
  885. static int offset = 0, size = 0;
  886. static char_type buf[BITS];
  887. register int r_off, bits;
  888. register char_type *bp = buf;
  889. if ( clear_flg > 0 || offset >= size || free_ent > maxcode ) {
  890. /*
  891. * If the next entry will be too big for the current code
  892. * size, then we must increase the size. This implies reading
  893. * a new buffer full, too.
  894. */
  895. if ( free_ent > maxcode ) {
  896. n_bits++;
  897. if ( n_bits == maxbits )
  898. maxcode = maxmaxcode; /* won't get any bigger now */
  899. else
  900. maxcode = MAXCODE(n_bits);
  901. }
  902. if ( clear_flg > 0) {
  903. maxcode = MAXCODE (n_bits = INIT_BITS);
  904. clear_flg = 0;
  905. }
  906. size = fread( buf, 1, n_bits, stdin );
  907. if ( size <= 0 )
  908. return -1; /* end of file */
  909. offset = 0;
  910. /* Round size down to integral number of codes */
  911. size = (size << 3) - (n_bits - 1);
  912. }
  913. r_off = offset;
  914. bits = n_bits;
  915. #ifdef vax
  916. asm( "extzv r10,r9,(r8),r11" );
  917. #else /* not a vax */
  918. /*
  919. * Get to the first byte.
  920. */
  921. bp += (r_off >> 3);
  922. r_off &= 7;
  923. /* Get first part (low order bits) */
  924. #ifdef NO_UCHAR
  925. code = ((*bp++ >> r_off) & rmask[8 - r_off]) & 0xff;
  926. #else
  927. code = (*bp++ >> r_off);
  928. #endif /* NO_UCHAR */
  929. bits -= (8 - r_off);
  930. r_off = 8 - r_off; /* now, offset into code word */
  931. /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
  932. if ( bits >= 8 ) {
  933. #ifdef NO_UCHAR
  934. code |= (*bp++ & 0xff) << r_off;
  935. #else
  936. code |= *bp++ << r_off;
  937. #endif /* NO_UCHAR */
  938. r_off += 8;
  939. bits -= 8;
  940. }
  941. /* high order bits. */
  942. code |= (*bp & rmask[bits]) << r_off;
  943. #endif /* vax */
  944. offset += n_bits;
  945. return code;
  946. }
  947. #ifdef DEBUG
  948. printcodes()
  949. {
  950. /*
  951. * Just print out codes from input file. For debugging.
  952. */
  953. code_int code;
  954. int col = 0, bits;
  955. bits = n_bits = INIT_BITS;
  956. maxcode = MAXCODE(n_bits);
  957. free_ent = ((block_compress) ? FIRST : 256 );
  958. while ( ( code = getcode() ) >= 0 ) {
  959. if ( (code == CLEAR) && block_compress ) {
  960. free_ent = FIRST - 1;
  961. clear_flg = 1;
  962. }
  963. else if ( free_ent < maxmaxcode )
  964. free_ent++;
  965. if ( bits != n_bits ) {
  966. fprintf(stderr, "\nChange to %d bits\n", n_bits );
  967. bits = n_bits;
  968. col = 0;
  969. }
  970. fprintf(stderr, "%5d%c", code, (col+=6) >= 74 ? (col = 0, '\n') : ' ' );
  971. }
  972. putc( '\n', stderr );
  973. exit( 0 );
  974. }
  975. code_int sorttab[1<<BITS]; /* sorted pointers into htab */
  976. dump_tab() /* dump string table */
  977. {
  978. register int i, first;
  979. register ent;
  980. #define STACK_SIZE 15000
  981. int stack_top = STACK_SIZE;
  982. register c;
  983. if(do_decomp == 0) { /* compressing */
  984. register int flag = 1;
  985. for(i=0; i<hsize; i++) { /* build sort pointers */
  986. if((long)htabof(i) >= 0) {
  987. sorttab[codetabof(i)] = i;
  988. }
  989. }
  990. first = block_compress ? FIRST : 256;
  991. for(i = first; i < free_ent; i++) {
  992. fprintf(stderr, "%5d: \"", i);
  993. de_stack[--stack_top] = '\n';
  994. de_stack[--stack_top] = '"';
  995. stack_top = in_stack((htabof(sorttab[i])>>maxbits)&0xff,
  996. stack_top);
  997. for(ent=htabof(sorttab[i]) & ((1<<maxbits)-1);
  998. ent > 256;
  999. ent=htabof(sorttab[ent]) & ((1<<maxbits)-1)) {
  1000. stack_top = in_stack(htabof(sorttab[ent]) >> maxbits,
  1001. stack_top);
  1002. }
  1003. stack_top = in_stack(ent, stack_top);
  1004. fwrite( &de_stack[stack_top], 1, STACK_SIZE-stack_top, stderr);
  1005. stack_top = STACK_SIZE;
  1006. }
  1007. } else if(!debug) { /* decompressing */
  1008. for ( i = 0; i < free_ent; i++ ) {
  1009. ent = i;
  1010. c = tab_suffixof(ent);
  1011. if ( isascii(c) && isprint(c) )
  1012. fprintf( stderr, "%5d: %5d/'%c' \"",
  1013. ent, tab_prefixof(ent), c );
  1014. else
  1015. fprintf( stderr, "%5d: %5d/\\%03o \"",
  1016. ent, tab_prefixof(ent), c );
  1017. de_stack[--stack_top] = '\n';
  1018. de_stack[--stack_top] = '"';
  1019. for ( ; ent != NULL;
  1020. ent = (ent >= FIRST ? tab_prefixof(ent) : NULL) ) {
  1021. stack_top = in_stack(tab_suffixof(ent), stack_top);
  1022. }
  1023. fwrite( &de_stack[stack_top], 1, STACK_SIZE - stack_top, stderr );
  1024. stack_top = STACK_SIZE;
  1025. }
  1026. }
  1027. }
  1028. int
  1029. in_stack(c, stack_top)
  1030. register c, stack_top;
  1031. {
  1032. if ( (isascii(c) && isprint(c) && c != '\\') || c == ' ' ) {
  1033. de_stack[--stack_top] = c;
  1034. } else {
  1035. switch( c ) {
  1036. case '\n': de_stack[--stack_top] = 'n'; break;
  1037. case '\t': de_stack[--stack_top] = 't'; break;
  1038. case '\b': de_stack[--stack_top] = 'b'; break;
  1039. case '\f': de_stack[--stack_top] = 'f'; break;
  1040. case '\r': de_stack[--stack_top] = 'r'; break;
  1041. case '\\': de_stack[--stack_top] = '\\'; break;
  1042. default:
  1043. de_stack[--stack_top] = '0' + c % 8;
  1044. de_stack[--stack_top] = '0' + (c / 8) % 8;
  1045. de_stack[--stack_top] = '0' + c / 64;
  1046. break;
  1047. }
  1048. de_stack[--stack_top] = '\\';
  1049. }
  1050. return stack_top;
  1051. }
  1052. #endif /* DEBUG */
  1053. writeerr()
  1054. {
  1055. (void)fprintf(stderr, "compress: %s: %s\n",
  1056. ofname[0] ? ofname : "stdout", strerror(errno));
  1057. (void)unlink(ofname);
  1058. exit(1);
  1059. }
  1060. copystat(ifname, ofname)
  1061. char *ifname, *ofname;
  1062. {
  1063. struct stat statbuf;
  1064. int mode;
  1065. struct utimbuf tp;
  1066. fclose(stdout);
  1067. if (stat(ifname, &statbuf)) { /* Get stat on input file */
  1068. perror(ifname);
  1069. return;
  1070. }
  1071. if ((statbuf.st_mode & S_IFMT/*0170000*/) != S_IFREG/*0100000*/) {
  1072. if(quiet)
  1073. fprintf(stderr, "%s: ", ifname);
  1074. fprintf(stderr, " -- not a regular file: unchanged");
  1075. exit_stat = 1;
  1076. perm_stat = 1;
  1077. } else if (statbuf.st_nlink > 1) {
  1078. if(quiet)
  1079. fprintf(stderr, "%s: ", ifname);
  1080. fprintf(stderr, " -- has %d other links: unchanged",
  1081. statbuf.st_nlink - 1);
  1082. exit_stat = 1;
  1083. perm_stat = 1;
  1084. } else if (exit_stat == 2 && (!force)) { /* No compression: remove file.Z */
  1085. if(!quiet)
  1086. fprintf(stderr, " -- file unchanged");
  1087. } else { /* ***** Successful Compression ***** */
  1088. exit_stat = 0;
  1089. mode = statbuf.st_mode & 07777;
  1090. if (chmod(ofname, mode)) /* Copy modes */
  1091. perror(ofname);
  1092. chown(ofname, statbuf.st_uid, statbuf.st_gid); /* Copy ownership */
  1093. tp.actime = statbuf.st_atime;
  1094. tp.modtime = statbuf.st_mtime;
  1095. utime(ofname, &tp); /* Update last accessed and modified times */
  1096. if (unlink(ifname)) /* Remove input file */
  1097. perror(ifname);
  1098. if(!quiet)
  1099. fprintf(stderr, " -- replaced with %s", ofname);
  1100. return; /* Successful return */
  1101. }
  1102. /* Unsuccessful return -- one of the tests failed */
  1103. if (unlink(ofname))
  1104. perror(ofname);
  1105. }
  1106. void
  1107. onintr ( )
  1108. {
  1109. if (!precious)
  1110. unlink ( ofname );
  1111. exit ( 1 );
  1112. }
  1113. void
  1114. oops ( ) /* wild pointer -- assume bad input */
  1115. {
  1116. if ( do_decomp )
  1117. fprintf ( stderr, "uncompress: corrupt input\n" );
  1118. unlink ( ofname );
  1119. exit ( 1 );
  1120. }
  1121. cl_block () /* table clear for block compress */
  1122. {
  1123. register long int rat;
  1124. checkpoint = in_count + CHECK_GAP;
  1125. #ifdef DEBUG
  1126. if ( debug ) {
  1127. fprintf ( stderr, "count: %ld, ratio: ", in_count );
  1128. prratio ( stderr, in_count, bytes_out );
  1129. fprintf ( stderr, "\n");
  1130. }
  1131. #endif /* DEBUG */
  1132. if(in_count > 0x007fffff) { /* shift will overflow */
  1133. rat = bytes_out >> 8;
  1134. if(rat == 0) { /* Don't divide by zero */
  1135. rat = 0x7fffffff;
  1136. } else {
  1137. rat = in_count / rat;
  1138. }
  1139. } else {
  1140. rat = (in_count << 8) / bytes_out; /* 8 fractional bits */
  1141. }
  1142. if ( rat > ratio ) {
  1143. ratio = rat;
  1144. } else {
  1145. ratio = 0;
  1146. #ifdef DEBUG
  1147. if(verbose)
  1148. dump_tab(); /* dump string table */
  1149. #endif
  1150. cl_hash ( (count_int) hsize );
  1151. free_ent = FIRST;
  1152. clear_flg = 1;
  1153. output ( (code_int) CLEAR );
  1154. #ifdef DEBUG
  1155. if(debug)
  1156. fprintf ( stderr, "clear\n" );
  1157. #endif /* DEBUG */
  1158. }
  1159. }
  1160. cl_hash(hsize) /* reset code table */
  1161. register count_int hsize;
  1162. {
  1163. register count_int *htab_p = htab+hsize;
  1164. register long i;
  1165. register long m1 = -1;
  1166. i = hsize - 16;
  1167. do { /* might use Sys V memset(3) here */
  1168. *(htab_p-16) = m1;
  1169. *(htab_p-15) = m1;
  1170. *(htab_p-14) = m1;
  1171. *(htab_p-13) = m1;
  1172. *(htab_p-12) = m1;
  1173. *(htab_p-11) = m1;
  1174. *(htab_p-10) = m1;
  1175. *(htab_p-9) = m1;
  1176. *(htab_p-8) = m1;
  1177. *(htab_p-7) = m1;
  1178. *(htab_p-6) = m1;
  1179. *(htab_p-5) = m1;
  1180. *(htab_p-4) = m1;
  1181. *(htab_p-3) = m1;
  1182. *(htab_p-2) = m1;
  1183. *(htab_p-1) = m1;
  1184. htab_p -= 16;
  1185. } while ((i -= 16) >= 0);
  1186. for ( i += 16; i > 0; i-- )
  1187. *--htab_p = m1;
  1188. }
  1189. prratio(stream, num, den)
  1190. FILE *stream;
  1191. long int num, den;
  1192. {
  1193. register int q; /* Doesn't need to be long */
  1194. if(num > 214748L) { /* 2147483647/10000 */
  1195. q = num / (den / 10000L);
  1196. } else {
  1197. q = 10000L * num / den; /* Long calculations, though */
  1198. }
  1199. if (q < 0) {
  1200. putc('-', stream);
  1201. q = -q;
  1202. }
  1203. fprintf(stream, "%d.%02d%%", q / 100, q % 100);
  1204. }
  1205. usage()
  1206. {
  1207. (void)fprintf(stderr,
  1208. #ifdef DEBUG
  1209. "compress [-CDVcdfnv] [-b maxbits] [file ...]\n");
  1210. #else
  1211. "compress [-Ccdfnv] [-b maxbits] [file ...]\n");
  1212. #endif
  1213. exit(1);
  1214. }