home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2032 / rnews.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  37.6 KB  |  1,533 lines

  1. /*
  2.  * rnews.c - rnews v1.4
  3.  * Copyright 1989 Jon Zeeff <zeeff@b-tech.ann-arbor.mi.us>
  4.  *
  5.  * rnews.c - rnews v1.5 - 11/04/90
  6.  *  - changed the include & struct setup for the statfs() call for
  7.  *    BSD systems that don't have proper calls (e.g. Ultrix)
  8.  *    kehoe@scotty.dccs.upenn.edu -or- brendan@cs.widener.edu (Brendan Kehoe)
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <sys/types.h>
  13. #include <fcntl.h>
  14. #include "rnews.h"
  15.  
  16. /* external declarations */
  17. extern char **environ;
  18. extern FILE *popen();
  19. void perror();
  20. void exit();
  21. unsigned sleep();
  22.  
  23. /* forward declarations */
  24. void copyout();
  25.     
  26. FILE *relaynews;
  27.  
  28. main()
  29. {
  30.     char buffer[20];
  31.     int ret;
  32.  
  33. #ifdef USG
  34.     ulimit(2,60000L);   /* in case the ulimit was too low */
  35. #endif
  36.  
  37.     /* get rid of our suid root status completely */
  38.     if (setgid(NEWS_GROUP) || setuid(NEWS_UID)) {
  39.            perror("can't set uid or gid\n");
  40.            exit(1); /* */
  41.         }
  42.  
  43.     /* replace environment with a clean one */
  44.         environ[0] = "IFS= \t\n";
  45.         environ[1] = "PATH=/bin:/usr/bin";
  46.         /*environ[2] = "TZ=EST5EDT";      /* some people need this */
  47.         environ[2] = 0;
  48.  
  49.     (void) umask(022);
  50. #ifdef NICE
  51.         (void) nice(5);
  52. #endif
  53.  
  54.         /* Do we have a reasonable number of free blocks? */
  55.         /* Eliminate this test if you don't have disk space problems */
  56.  
  57.     if (!have_space(SPOOL_MNT,SPOOL_FREE)) 
  58.            (void) make_space();
  59.  
  60.         if (!have_space(LIB_MNT,LIB_FREE)||!have_space(SPOOL_MNT,SPOOL_FREE)) {
  61.            fprintf(stderr,"Not enough space to run rnews\n");
  62.  
  63.            /* Why 75?  Because, I use 75 to indicate a temporary  
  64.               error that the program calling rnews (eg. uuxqt with 
  65.               the right modification) may be able to use to try again 
  66.               later.  */ 
  67.  
  68.            exit(75);   
  69.         }
  70.  
  71.     (void) fclose(stdout);
  72. #ifdef NICE
  73.     (void) sleep(2);
  74. #endif
  75.         relaynews = popen(RELAYNEWS,"w");
  76.     if (relaynews == NULL) {
  77.         perror("can't popen relaynews");
  78.         exit(3);
  79.     }
  80. #ifdef NICE
  81.     (void) sleep(2);
  82. #endif
  83.  
  84.     if (fread(buffer, 1, 13, stdin) != 13) {
  85.         fprintf(stderr, "could not read in first 13 characters\n");
  86.         exit(4);
  87.     }
  88.  
  89.     if (strncmp(buffer, "#! cunbatch\n",12) == 0) {
  90.        (void) ungetc(buffer[12], stdin);
  91.        uncompress();
  92.     }
  93. #ifdef C7
  94.     else if (strncmp(buffer, "#! c7unbatch\n",13) == 0) 
  95.         do_c7();
  96. #endif
  97.     else if (buffer[0] == 0x1f) {   /* compressed w/o cunbatch */
  98.                 rewind(stdin);          /* won't work on a pipe */
  99.                 uncompress();
  100.              }
  101.         else {                                          /* plain text */
  102.             if (fwrite(buffer,1,13,relaynews) != 13) {
  103.         fprintf(stderr, "error writing first 13 characters\n");
  104.         exit(5);
  105.         }
  106.         copyout();
  107.     }
  108.  
  109.     if ((ret = pclose(relaynews)) != 0) {
  110.            perror("pclose of relaynews failed");
  111.            exit(ret);
  112.         }
  113.  
  114.         /* Leave enough space for local people to post news */
  115.  
  116.     if (!have_space(SPOOL_MNT,SPOOL_FREE)) 
  117.            (void) make_space();
  118.  
  119.         exit(0);
  120.     return 0;    /* keep lint happy */
  121. }
  122.  
  123. /* Copy stdin to relaynews */
  124.  
  125. void
  126. copyout()
  127. {
  128.     int count;
  129.     char buf[8192];
  130.  
  131.         while ((count = fread(buf,1,8192,stdin)) > 0) {
  132.                (void) fwrite(buf,1,count,relaynews);
  133. #ifdef NICE
  134.         (void) sleep(2);   /* reduce system load (for uncompressed) */
  135. #endif        
  136.         }
  137.  
  138. }
  139.  
  140. /* 
  141.    Run progressive expires until there is enough space.  
  142.    Steve Schonberger gave me the idea of doing this in rnews.
  143. */
  144.  
  145. /* Create these files in EXPIRE_DIR and modify them to suit your system */
  146.  
  147. char *days[]={"explist.A","explist.B","explist.C","explist.D","explist.E"};
  148.  
  149. #define LOCK_FILE "LOCK.rnews"
  150.  
  151. make_space()
  152. {
  153. register int i;
  154. int ret;
  155. char string[sizeof(EXPIRE) + 20];
  156.  
  157.    (void) chdir(EXPIRE_DIR);
  158.  
  159.    /* Get a lock file to prevent another rnews from wasting cpu time */
  160.    /* It's not a disaster if we don't, so blow it away after awhile */
  161.  
  162.    for (i = 0; i < 1000; i++) {
  163.       if (open(LOCK_FILE, O_CREAT | O_EXCL | O_WRONLY, 0777) >= 0) 
  164.          break;
  165.       else
  166.          (void) sleep(5);
  167.    }
  168.  
  169.    if (have_space(SPOOL_MNT,SPOOL_FREE))
  170.       ret = 0;  /* space appeared while waiting for lock */
  171.    else
  172.       for (ret = 2,i = 0; i < sizeof(days) / sizeof(char *); ++i) {
  173.          sprintf(string,"%s %s",EXPIRE,days[i]);
  174.          if (system(string)) {
  175.             fprintf(stderr,"Error in running expire to get space\n");
  176.             ret =  1;
  177.             break;
  178.          }
  179.  
  180.          if (have_space(SPOOL_MNT,SPOOL_FREE)) {
  181.             ret = 0;
  182.             break;
  183.          }
  184.       } /* for */
  185.  
  186.    (void) unlink(LOCK_FILE);
  187.    return ret;
  188.  
  189. }
  190.  
  191. /*
  192.  
  193.   Space checker.  Based on code by:
  194.  
  195.   denny@mcmi.uucp (Denny Page)
  196.  
  197. */
  198.  
  199. /*
  200.  * Changed this so it'll deal with BSD systems that don't have proper
  201.  *  statfs() syscalls (e.g. Ultrix).
  202.  *
  203.  * kehoe@scotty.dccs.upenn.edu -or- brendan@cs.widener.edu (Brendan Kehoe)
  204.  */
  205. #ifdef HAVE_STATFS
  206. # ifdef USG
  207. #  include <sys/statfs.h>
  208. #  define FREEBLOCKS statfsb.f_bfree
  209. #  define FREENODES statfsb.f_ffree
  210. # else /* !USG */
  211. #  include <sys/vfs.h>
  212. #  define FREEBLOCKS statfsb.f_bavail
  213. #  define FREENODES statfsb.f_ffree
  214. # endif /* USG */
  215.   struct statfs statfsb;
  216. #else /* !HAVE_STATFS */
  217. # include <sys/stat.h>
  218. # include <ustat.h>
  219. # define FREEBLOCKS ustatb.f_tfree
  220. # define FREENODES ustatb.f_tinode
  221.   struct stat statb;
  222.   struct ustat ustatb;
  223. #endif /* HAVE_STATFS */
  224.  
  225. have_space(dirname, minblocks)
  226. char *dirname;
  227. int minblocks;
  228. {
  229.  
  230. #ifndef BSD 
  231. #ifndef USG
  232.    return 1;
  233. #endif
  234. #endif
  235.  
  236.  
  237. #ifdef HAVE_STATFS
  238.     if (statfs(dirname, &statfsb, sizeof(statfsb), 0) != 0) {
  239.     perror("statfs failed");
  240.     return(1);
  241.     }
  242. #else /* HAVE_STATFS */
  243.     if (stat(dirname, &statb) != 0) {
  244.     perror("stat failed");
  245.     return(1);
  246.     }
  247.     if (ustat(statb.st_dev, &ustatb) != 0) {
  248.     perror("ustat failed");
  249.     return(1);
  250.     }
  251. #endif /* HAVE_STATFS */
  252.  
  253.     if (FREEBLOCKS > minblocks && FREENODES > MIN_INODES)
  254.        return 1;
  255.     else
  256.        return 0;
  257.  
  258. }
  259.  
  260. /* The following is a slightly hacked up version of compress */
  261. /* It reads from stdin and writes to stdout */
  262.  
  263. /* 
  264.  * Compress - data compression program 
  265.  */
  266.  
  267. #define UNCOMPRESS_ONLY
  268.  
  269. #define    min(a,b)    ((a>b) ? b : a)
  270. extern char *strcpy(), *strcat();
  271. /*
  272.  * machine variants which require cc -Dmachine:  pdp11, z8000, pcxt
  273.  */
  274. /*
  275.  * Set USERMEM to the maximum amount of physical user memory available
  276.  * in bytes.  USERMEM is used to determine the maximum BITS that can be used
  277.  * for compression.
  278.  *
  279.  * SACREDMEM is the amount of physical memory saved for others; compress
  280.  * will hog the rest.
  281.  */
  282. #ifndef SACREDMEM
  283. #define SACREDMEM    0
  284. #endif
  285.  
  286. #ifndef USERMEM
  287. # define USERMEM     450000    /* default user memory */
  288. #endif
  289.  
  290. #ifdef interdata        /* (Perkin-Elmer) */
  291. #define SIGNED_COMPARE_SLOW    /* signed compare is slower than unsigned */
  292. #endif
  293.  
  294. #ifdef pdp11
  295. # define NO_UCHAR    /* also if "unsigned char" functions as signed char */
  296. # undef USERMEM 
  297. #endif /* pdp11 */    /* don't forget to compile with -i */
  298.  
  299. #ifdef z8000
  300. # undef vax        /* weird preprocessor */
  301. # undef USERMEM 
  302. #endif /* z8000 */
  303.  
  304. #ifdef pcxt
  305. # undef USERMEM
  306. #endif /* pcxt */
  307.  
  308. #ifdef USERMEM
  309. # if USERMEM >= (433484+SACREDMEM)
  310. #  define PBITS    16
  311. # else
  312. #  if USERMEM >= (229600+SACREDMEM)
  313. #   define PBITS    15
  314. #  else
  315. #   if USERMEM >= (127536+SACREDMEM)
  316. #    define PBITS    14
  317. #   else
  318. #    if USERMEM >= (73464+SACREDMEM)
  319. #     define PBITS    13
  320. #    else
  321. #     define PBITS    12
  322. #    endif
  323. #   endif
  324. #  endif
  325. # endif
  326. # undef USERMEM
  327. #endif /* USERMEM */
  328.  
  329. /* Preferred BITS for this memory size */
  330.  
  331. #ifdef PBITS    
  332. # ifndef BITS
  333. #  define BITS PBITS
  334. # endif 
  335. #endif 
  336.  
  337. #if BITS == 16
  338. # define HSIZE    69001        /* 95% occupancy */
  339. #endif
  340. #if BITS == 15
  341. # define HSIZE    35023        /* 94% occupancy */
  342. #endif
  343. #if BITS == 14
  344. # define HSIZE    18013        /* 91% occupancy */
  345. #endif
  346. #if BITS == 13
  347. # define HSIZE    9001        /* 91% occupancy */
  348. #endif
  349. #if BITS <= 12
  350. # define HSIZE    5003        /* 80% occupancy */
  351. #endif
  352.  
  353. #ifdef M_XENIX            /* Stupid compiler can't handle arrays with */
  354. # if BITS == 16            /* more than 65535 bytes - so we fake it */
  355. #  define XENIX_16
  356. # else
  357. #  if BITS > 13            /* Code only handles BITS = 12, 13, or 16 */
  358. #   define BITS    13
  359. #  endif
  360. # endif
  361. #endif
  362.  
  363. /*
  364.  * a code_int must be able to hold 2**BITS values of type int, and also -1
  365.  */
  366. #if BITS > 15
  367. typedef long int    code_int;
  368. #else
  369. typedef int        code_int;
  370. #endif
  371.  
  372. #ifdef SIGNED_COMPARE_SLOW
  373. typedef unsigned long int count_int;
  374. typedef unsigned short int count_short;
  375. #else
  376. typedef long int      count_int;
  377. #endif
  378.  
  379. #ifdef NO_UCHAR
  380.  typedef char    char_type;
  381. #else
  382.  typedef    unsigned char    char_type;
  383. #endif /* UCHAR */
  384. char_type magic_header[] = { "\037\235" };    /* 1F 9D */
  385.  
  386. /* Defines for third byte of header */
  387. #define BIT_MASK    0x1f
  388. #define BLOCK_MASK    0x80
  389. /* Masks 0x40 and 0x20 are free.  I think 0x20 should mean that there is
  390.    a fourth header byte (for expansion).
  391. */
  392. #define INIT_BITS 9            /* initial number of bits/code */
  393.  
  394. /*
  395.  * compress.c - File compression ala IEEE Computer, June 1984.
  396.  *
  397.  * Authors:    Spencer W. Thomas    (decvax!harpo!utah-cs!utah-gr!thomas)
  398.  *        Jim McKie        (decvax!mcvax!jim)
  399.  *        Steve Davies        (decvax!vax135!petsd!peora!srd)
  400.  *        Ken Turkowski        (decvax!decwrl!turtlevax!ken)
  401.  *        James A. Woods        (decvax!ihnp4!ames!jaw)
  402.  *        Joe Orost        (decvax!vax135!petsd!joe)
  403.  */
  404.  
  405. /* $Log:    rnews.c,v $
  406.  * Revision 1.1  90/10/12  14:24:34  zeeff
  407.  * Initial revision
  408.  * 
  409.  * Revision 4.0  85/07/30  12:50:00  joe
  410.  * Removed ferror() calls in output routine on every output except first.
  411.  * Prepared for release to the world.
  412.  * 
  413.  * Revision 3.6  85/07/04  01:22:21  joe
  414.  * Remove much wasted storage by overlaying hash table with the tables
  415.  * used by decompress: tab_suffix[1<<BITS], stack[8000].  Updated USERMEM
  416.  * computations.  Fixed dump_tab() DEBUG routine.
  417.  *
  418.  * Revision 3.5  85/06/30  20:47:21  jaw
  419.  * Change hash function to use exclusive-or.  Rip out hash cache.  These
  420.  * speedups render the megamemory version defunct, for now.  Make decoder
  421.  * stack global.  Parts of the RCS trunks 2.7, 2.6, and 2.1 no longer apply.
  422.  *
  423.  * Revision 3.4  85/06/27  12:00:00  ken
  424.  * Get rid of all floating-point calculations by doing all compression ratio
  425.  * calculations in fixed point.
  426.  *
  427.  * Revision 3.3  85/06/24  21:53:24  joe
  428.  * Incorporate portability suggestion for M_XENIX.  Got rid of text on #else
  429.  * and #endif lines.  Cleaned up #ifdefs for vax and interdata.
  430.  *
  431.  * Revision 3.2  85/06/06  21:53:24  jaw
  432.  * Incorporate portability suggestions for Z8000, IBM PC/XT from mailing list.
  433.  * Default to "quiet" output (no compression statistics).
  434.  *
  435.  * Revision 3.1  85/05/12  18:56:13  jaw
  436.  * Integrate decompress() stack speedups (from early pointer mods by McKie).
  437.  * Repair multi-file USERMEM gaffe.  Unify 'force' flags to mimic semantics
  438.  * of SVR2 'pack'.  Streamline block-compress table clear logic.  Increase 
  439.  * output byte count by magic number size.
  440.  * 
  441.  * Revision 3.0   84/11/27  11:50:00  petsd!joe
  442.  * Set HSIZE depending on BITS.  Set BITS depending on USERMEM.  Unrolled
  443.  * loops in clear routines.  Added "-C" flag for 2.0 compatibility.  Used
  444.  * unsigned compares on Perkin-Elmer.  Fixed foreground check.
  445.  *
  446.  * Revision 2.7   84/11/16  19:35:39  ames!jaw
  447.  * Cache common hash codes based on input statistics; this improves
  448.  * performance for low-density raster images.  Pass on #ifdef bundle
  449.  * from Turkowski.
  450.  *
  451.  * Revision 2.6   84/11/05  19:18:21  ames!jaw
  452.  * Vary size of hash tables to reduce time for small files.
  453.  * Tune PDP-11 hash function.
  454.  *
  455.  * Revision 2.5   84/10/30  20:15:14  ames!jaw
  456.  * Junk chaining; replace with the simpler (and, on the VAX, faster)
  457.  * double hashing, discussed within.  Make block compression standard.
  458.  *
  459.  * Revision 2.4   84/10/16  11:11:11  ames!jaw
  460.  * Introduce adaptive reset for block compression, to boost the rate
  461.  * another several percent.  (See mailing list notes.)
  462.  *
  463.  * Revision 2.3   84/09/22  22:00:00  petsd!joe
  464.  * Implemented "-B" block compress.  Implemented REVERSE sorting of tab_next.
  465.  * Bug fix for last bits.  Changed fwrite to putchar loop everywhere.
  466.  *
  467.  * Revision 2.2   84/09/18  14:12:21  ames!jaw
  468.  * Fold in news changes, small machine typedef from thomas,
  469.  * #ifdef interdata from joe.
  470.  *
  471.  * Revision 2.1   84/09/10  12:34:56  ames!jaw
  472.  * Configured fast table lookup for 32-bit machines.
  473.  * This cuts user time in half for b <= FBITS, and is useful for news batching
  474.  * from VAX to PDP sites.  Also sped up decompress() [fwrite->putc] and
  475.  * added signal catcher [plus beef in writeerr()] to delete effluvia.
  476.  *
  477.  * Revision 2.0   84/08/28  22:00:00  petsd!joe
  478.  * Add check for foreground before prompting user.  Insert maxbits into
  479.  * compressed file.  Force file being uncompressed to end with ".Z".
  480.  * Added "-c" flag and "zcat".  Prepared for release.
  481.  *
  482.  * Revision 1.10  84/08/24  18:28:00  turtlevax!ken
  483.  * Will only compress regular files (no directories), added a magic number
  484.  * header (plus an undocumented -n flag to handle old files without headers),
  485.  * added -f flag to force overwriting of possibly existing destination file,
  486.  * otherwise the user is prompted for a response.  Will tack on a .Z to a
  487.  * filename if it doesn't have one when decompressing.  Will only replace
  488.  * file if it was compressed.
  489.  *
  490.  * Revision 1.9  84/08/16  17:28:00  turtlevax!ken
  491.  * Removed scanargs(), getopt(), added .Z extension and unlimited number of
  492.  * filenames to compress.  Flags may be clustered (-Ddvb12) or separated
  493.  * (-D -d -v -b 12), or combination thereof.  Modes and other status is
  494.  * copied with copystat().  -O bug for 4.2 seems to have disappeared with
  495.  * 1.8.
  496.  *
  497.  * Revision 1.8  84/08/09  23:15:00  joe
  498.  * Made it compatible with vax version, installed jim's fixes/enhancements
  499.  *
  500.  * Revision 1.6  84/08/01  22:08:00  joe
  501.  * Sped up algorithm significantly by sorting the compress chain.
  502.  *
  503.  * Revision 1.5  84/07/13  13:11:00  srd
  504.  * Added C version of vax asm routines.  Changed structure to arrays to
  505.  * save much memory.  Do unsigned compares where possible (faster on
  506.  * Perkin-Elmer)
  507.  *
  508.  * Revision 1.4  84/07/05  03:11:11  thomas
  509.  * Clean up the code a little and lint it.  (Lint complains about all
  510.  * the regs used in the asm, but I'm not going to "fix" this.)
  511.  *
  512.  * Revision 1.3  84/07/05  02:06:54  thomas
  513.  * Minor fixes.
  514.  *
  515.  * Revision 1.2  84/07/05  00:27:27  thomas
  516.  * Add variable bit length output.
  517.  *
  518.  */
  519. #include <ctype.h>
  520. #include <signal.h>
  521. /* #include <sys/stat.h> */
  522.  
  523. #define ARGVAL() (*++(*argv) || (--argc && *++argv))
  524.  
  525. int n_bits;                /* number of bits/code */
  526. int maxbits = BITS;            /* user settable max # bits/code */
  527. code_int maxcode;            /* maximum code, given n_bits */
  528. code_int maxmaxcode = 1L << BITS;    /* should NEVER generate this code */
  529. #ifdef COMPATIBLE        /* But wrong! */
  530. # define MAXCODE(n_bits)    (1L << (n_bits) - 1)
  531. #else
  532. # define MAXCODE(n_bits)    ((1L << (n_bits)) - 1)
  533. #endif /* COMPATIBLE */
  534.  
  535. #ifdef XENIX_16
  536. count_int htab0[8192];
  537. count_int htab1[8192];
  538. count_int htab2[8192];
  539. count_int htab3[8192];
  540. count_int htab4[8192];
  541. count_int htab5[8192];
  542. count_int htab6[8192];
  543. count_int htab7[8192];
  544. count_int htab8[HSIZE-65536];
  545. count_int * htab[9] = {
  546.     htab0, htab1, htab2, htab3, htab4, htab5, htab6, htab7, htab8 };
  547.  
  548. #define htabof(i)    (htab[(i) >> 13][(i) & 0x1fff])
  549. unsigned short code0tab[16384];
  550. unsigned short code1tab[16384];
  551. unsigned short code2tab[16384];
  552. unsigned short code3tab[16384];
  553. unsigned short code4tab[16384];
  554. unsigned short * codetab[5] = {
  555.     code0tab, code1tab, code2tab, code3tab, code4tab };
  556.  
  557. #define codetabof(i)    (codetab[(i) >> 14][(i) & 0x3fff])
  558.  
  559. #else    /* Normal machine */
  560. /* save some memory if only doing uncompress */
  561. #ifdef UNCOMPRESS_ONLY
  562. /* should be (2 ** BITS + 8000) bytes */
  563. count_int htab[((1L << BITS) + 8000) / sizeof(count_int)];
  564. #else
  565. count_int htab [HSIZE];
  566. #endif
  567. unsigned short codetab [HSIZE]; 
  568. #define htabof(i)    htab[i]
  569. #define codetabof(i)    codetab[i]
  570. #endif    /* XENIX_16 */
  571. code_int hsize = HSIZE;            /* for dynamic table sizing */
  572. count_int fsize;
  573.  
  574. /*
  575.  * To save much memory, we overlay the table used by compress() with those
  576.  * used by decompress().  The tab_prefix table is the same size and type
  577.  * as the codetab.  The tab_suffix table needs 2**BITS characters.  We
  578.  * get this from the beginning of htab.  The output stack uses the rest
  579.  * of htab, and contains characters.  There is plenty of room for any
  580.  * possible stack (stack used to be 8000 characters).
  581.  */
  582.  
  583. #define tab_prefixof(i)    codetabof(i)
  584. #ifdef XENIX_16
  585. # define tab_suffixof(i)    ((char_type *)htab[(i)>>15])[(i) & 0x7fff]
  586. # define de_stack        ((char_type *)(htab2))
  587. #else    /* Normal machine */
  588. # define tab_suffixof(i)    ((char_type *)(htab))[i]
  589. # define de_stack        ((char_type *)&tab_suffixof(1L<<BITS))
  590. #endif    /* XENIX_16 */
  591.  
  592. code_int free_ent = 0;            /* first unused entry */
  593. int exit_stat = 0;
  594.  
  595. code_int getcode();
  596.  
  597. Usage() {
  598. #ifdef DEBUG
  599. (void)fprintf(stderr,"Usage: compress [-dDVfc] [-b maxbits] [file ...]\n");
  600. }
  601. int debug = 0;
  602. #else
  603. (void)fprintf(stderr,"Usage: compress [-dfvcV] [-b maxbits] [file ...]\n");
  604. }
  605. #endif /* DEBUG */
  606. int nomagic = 0;    /* Use a 3-byte magic number header, unless old file */
  607. int zcat_flg = 0;    /* Write output on stdout, suppress messages */
  608. int quiet = 1;        /* don't tell me about compression */
  609.  
  610. /*
  611.  * block compression parameters -- after all codes are used up,
  612.  * and compression rate changes, start over.
  613.  */
  614. int block_compress = BLOCK_MASK;
  615. int clear_flg = 0;
  616. long int ratio = 0;
  617. #define CHECK_GAP 10000    /* ratio check interval */
  618. count_int checkpoint = CHECK_GAP;
  619. /*
  620.  * the next two codes should not be changed lightly, as they must not
  621.  * lie within the contiguous general code space.
  622.  */ 
  623. #define FIRST    257    /* first free entry */
  624. #define    CLEAR    256    /* table clear output code */
  625.  
  626. int force = 0;
  627. char ofname [100];
  628. int oktozap = 0;    /* true if unlink(ofname) won't lose data */
  629.  
  630. #ifdef DEBUG
  631. int verbose = 0;
  632. #endif /* DEBUG */
  633. void (*bgnd_flag)();
  634.  
  635. int do_decomp = 0;
  636.  
  637. /*****************************************************************
  638.  * TAG( main )
  639.  *
  640.  * Algorithm from "A Technique for High Performance Data Compression",
  641.  * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.
  642.  *
  643.  * Usage: compress [-dfvc] [-b bits] [file ...]
  644.  * Inputs:
  645.  *    -d:        If given, decompression is done instead.
  646.  *
  647.  *      -c:         Write output on stdout, don't remove original.
  648.  *
  649.  *      -b:         Parameter limits the max number of bits/code.
  650.  *
  651.  *    -f:        Forces output file to be generated, even if one already
  652.  *            exists, and even if no space is saved by compressing.
  653.  *            If -f is not used, the user will be prompted if stdin is
  654.  *            a tty, otherwise, the output file will not be overwritten.
  655.  *
  656.  *      -v:        Write compression statistics
  657.  *
  658.  *     file ...:   Files to be compressed.  If none specified, stdin
  659.  *            is used.
  660.  * Outputs:
  661.  *    file.Z:        Compressed form of file with same mode, owner, and utimes
  662.  *     or stdout   (if stdin used as input)
  663.  *
  664.  * Assumptions:
  665.  *    When filenames are given, replaces with the compressed version
  666.  *    (.Z suffix) only if the file decreases in size.
  667.  * Algorithm:
  668.  *     Modified Lempel-Ziv method (LZW).  Basically finds common
  669.  * substrings and replaces them with a variable size code.  This is
  670.  * deterministic, and can be done on the fly.  Thus, the decompression
  671.  * procedure needs no input table, but tracks the way the table was built.
  672.  */
  673.  
  674. uncompress()
  675. {
  676.     char tempname[100];
  677.     char *cp, *rindex(), *malloc();
  678.     extern onintr(), oops();
  679.  
  680.     if(maxbits < INIT_BITS) maxbits = INIT_BITS;
  681.     if (maxbits > BITS) maxbits = BITS;
  682.     maxmaxcode = 1L << maxbits;
  683.  
  684.         /* Check the magic number */
  685.         if ((getchar()!=(magic_header[0] & 0xFF))
  686.          || (getchar()!=(magic_header[1] & 0xFF))) {
  687.             (void)fprintf(stderr, "stdin: not in compressed format\n");
  688.             exit(20);
  689.         }
  690.         maxbits = getchar();    /* set -b from file */
  691.         block_compress = maxbits & BLOCK_MASK;
  692.         maxbits &= BIT_MASK;
  693.         maxmaxcode = 1L << maxbits;
  694.         fsize = 100000;        /* assume stdin large for USERMEM */
  695.         if(maxbits > BITS) {
  696.             (void)fprintf(stderr,
  697.             "stdin: compressed with %d bits, can only handle %d bits\n",
  698.             maxbits, BITS);
  699.             exit(21);
  700.         }
  701.         decompress();
  702. }
  703.  
  704. static int offset;
  705. long int in_count = 1;            /* length of input */
  706. long int bytes_out;            /* length of compressed output */
  707. long int out_count = 0;            /* # of codes output (for debugging) */
  708.  
  709. /*****************************************************************
  710.  * TAG( output )
  711.  *
  712.  * Output the given code.
  713.  * Inputs:
  714.  *     code:    A n_bits-bit integer.  If == -1, then EOF.  This assumes
  715.  *        that n_bits =< (long)wordsize - 1.
  716.  * Outputs:
  717.  *     Outputs code to the file.
  718.  * Assumptions:
  719.  *    Chars are 8 bits long.
  720.  * Algorithm:
  721.  *     Maintain a BITS character long buffer (so that 8 codes will
  722.  * fit in it exactly).  Use the VAX insv instruction to insert each
  723.  * code in turn.  When the buffer fills up empty it and start over.
  724.  */
  725.  
  726. static char buf[BITS];
  727.  
  728. #ifndef vax
  729. char_type lmask[9] = {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
  730. char_type rmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
  731. #endif /* vax */
  732.  
  733. output( code )
  734. code_int  code;
  735. {
  736. #ifdef DEBUG
  737.     static int col = 0;
  738. #endif /* DEBUG */
  739.  
  740.     /*
  741.      * On the VAX, it is important to have the register declarations
  742.      * in exactly the order given, or the asm will break.
  743.      */
  744.     register int r_off = offset, bits= n_bits;
  745.     register char * bp = buf;
  746.  
  747. #ifdef DEBUG
  748.     if ( verbose )
  749.         (void)fprintf( stderr, "%5d%c", code,
  750.             (col+=6) >= 74 ? (col = 0, '\n') : ' ' );
  751. #endif /* DEBUG */
  752.     if ( code >= 0 ) {
  753. #ifdef vax
  754.     /* VAX DEPENDENT!! Implementation on other machines is below.
  755.      *
  756.      * Translation: Insert BITS bits from the argument starting at
  757.      * offset bits from the beginning of buf.
  758.      */
  759.     0;    /* Work around for pcc -O bug with asm and if stmt */
  760.     asm( "insv    4(ap),r11,r10,(r9)" );
  761. #else /* not a vax */
  762. /* 
  763.  * byte/bit numbering on the VAX is simulated by the following code
  764.  */
  765.     /*
  766.      * Get to the first byte.
  767.      */
  768.     bp += (r_off >> 3);
  769.     r_off &= 7;
  770.     /*
  771.      * Since code is always >= 8 bits, only need to mask the first
  772.      * hunk on the left.
  773.      */
  774.     *bp = (*bp & rmask[r_off]) | ((long)code << r_off) & lmask[r_off];
  775.     bp++;
  776.     bits -= (8 - r_off);
  777.     code >>= 8 - r_off;
  778.     /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
  779.     if ( bits >= 8 ) {
  780.         *bp++ = code;
  781.         code >>= 8;
  782.         bits -= 8;
  783.     }
  784.     /* Last bits. */
  785.     if(bits)
  786.         *bp = code;
  787. #endif /* vax */
  788.     offset += n_bits;
  789.     if ( offset == ((long)n_bits << 3) ) {
  790.         bp = buf;
  791.         bits = n_bits;
  792.         bytes_out += bits;
  793.         do
  794.         putchar(*bp++);
  795.         while(--bits);
  796.         offset = 0;
  797.     }
  798.  
  799.     /*
  800.      * If the next entry is going to be too big for the code size,
  801.      * then increase it, if possible.
  802.      */
  803.     if ( free_ent > maxcode || (clear_flg > 0))
  804.     {
  805.         /*
  806.          * Write the whole buffer, because the input side won't
  807.          * discover the size increase until after it has read it.
  808.          */
  809.         if ( offset > 0 ) {
  810.         if( fwrite( buf, 1, n_bits, stdout ) != n_bits)
  811.             writeerr();
  812.         bytes_out += n_bits;
  813.         }
  814.         offset = 0;
  815.  
  816.         if ( clear_flg ) {
  817.                 maxcode = MAXCODE (n_bits = INIT_BITS);
  818.             clear_flg = 0;
  819.         }
  820.         else {
  821.             n_bits++;
  822.             if ( n_bits == maxbits )
  823.             maxcode = maxmaxcode;
  824.             else
  825.             maxcode = MAXCODE(n_bits);
  826.         }
  827. #ifdef DEBUG
  828.         if ( debug ) {
  829.         (void)fprintf( stderr, "\nChange to %d bits\n", n_bits );
  830.         col = 0;
  831.         }
  832. #endif /* DEBUG */
  833.     }
  834.     } else {
  835.     /*
  836.      * At EOF, write the rest of the buffer.
  837.      */
  838.     if ( offset > 0 )
  839.         (void)fwrite( buf, 1, (offset + 7) / 8, stdout );
  840.     bytes_out += (offset + 7) / 8;
  841.     offset = 0;
  842.     (void)fflush( stdout );
  843. #ifdef DEBUG
  844.     if ( verbose )
  845.         (void)fprintf( stderr, "\n" );
  846. #endif /* DEBUG */
  847.     if( ferror( stdout ) )
  848.         writeerr();
  849.     }
  850. }
  851.  
  852. /*
  853.  * Decompress stdin to stdout.  This routine adapts to the codes in the
  854.  * file building the "string" table on-the-fly; requiring no table to
  855.  * be stored in the compressed file.  The tables used herein are shared
  856.  * with those of the compress() routine.  See the definitions above.
  857.  */
  858.  
  859. decompress() {
  860.     register char_type *stackp;
  861.     register int finchar;
  862.     register code_int code, oldcode, incode;
  863.  
  864.     /*
  865.      * As above, initialize the first 256 entries in the table.
  866.      */
  867.     maxcode = MAXCODE(n_bits = INIT_BITS);
  868.     for ( code = 255; code >= 0; code-- ) {
  869.     tab_prefixof(code) = 0;
  870.     tab_suffixof(code) = (char_type)code;
  871.     }
  872.     free_ent = ((block_compress) ? FIRST : 256 );
  873.  
  874.     finchar = oldcode = getcode();
  875.     if(oldcode == -1)    /* EOF already? */
  876.     return;            /* Get out of here */
  877.     putchar( (char)finchar );        /* first code must be 8 bits = char */
  878.     if(ferror(stdout))        /* Crash if can't write */
  879.     writeerr();
  880.     stackp = de_stack;
  881.  
  882.     while ( (code = getcode()) > -1 ) {
  883.  
  884.     if ( (code == CLEAR) && block_compress ) {
  885.         for ( code = 255; code >= 0; code-- )
  886.         tab_prefixof(code) = 0;
  887.         clear_flg = 1;
  888.         free_ent = FIRST - 1;
  889.         if ( (code = getcode ()) == -1 )    /* O, untimely death! */
  890.         break;
  891.     }
  892.     incode = code;
  893.     /*
  894.      * Special case for KwKwK string.
  895.      */
  896.     if ( code >= free_ent ) {
  897.             *stackp++ = finchar;
  898.         code = oldcode;
  899.     }
  900.  
  901.     /*
  902.      * Generate output characters in reverse order
  903.      */
  904. #ifdef SIGNED_COMPARE_SLOW
  905.     while ( ((unsigned long)code) >= ((unsigned long)256) ) {
  906. #else
  907.     while ( code >= 256 ) {
  908. #endif
  909.         *stackp++ = tab_suffixof(code);
  910.         code = tab_prefixof(code);
  911.     }
  912.     *stackp++ = finchar = tab_suffixof(code);
  913.  
  914.     /*
  915.      * And put them out in forward order
  916.      */
  917.     do
  918.         putchar ((char) *--stackp );
  919.     while ( stackp > de_stack );
  920.  
  921.     /*
  922.      * Generate the new entry.
  923.      */
  924.     if ( (code=free_ent) < maxmaxcode ) {
  925.         tab_prefixof(code) = (unsigned short)oldcode;
  926.         tab_suffixof(code) = finchar;
  927.         free_ent = code+1;
  928.     } 
  929.     /*
  930.      * Remember previous code.
  931.      */
  932.     oldcode = incode;
  933.     }
  934.     (void)fflush( stdout );
  935.     if(ferror(stdout))
  936.     writeerr();
  937. }
  938.  
  939. /*****************************************************************
  940.  * TAG( getcode )
  941.  *
  942.  * Read one code from the standard input.  If EOF, return -1.
  943.  * Inputs:
  944.  *     stdin
  945.  * Outputs:
  946.  *     code or -1 is returned.
  947.  */
  948.  
  949. code_int
  950. getcode() {
  951.     /*
  952.      * On the VAX, it is important to have the register declarations
  953.      * in exactly the order given, or the asm will break.
  954.      */
  955.     register code_int code;
  956.     static int poffset = 0, size = 0;
  957.     static char_type pbuf[BITS];
  958.     register int r_off, bits;
  959.     register char_type *bp = pbuf;
  960.  
  961.     if ( clear_flg > 0 || poffset >= size || free_ent > maxcode ) {
  962.     /*
  963.      * If the next entry will be too big for the current code
  964.      * size, then we must increase the size.  This implies reading
  965.      * a new pbuffer full, too.
  966.      */
  967.     if ( free_ent > maxcode ) {
  968.         n_bits++;
  969.         if ( n_bits == maxbits )
  970.         maxcode = maxmaxcode;    /* won't get any bigger now */
  971.         else
  972.         maxcode = MAXCODE(n_bits);
  973.     }
  974.     if ( clear_flg > 0) {
  975.             maxcode = MAXCODE (n_bits = INIT_BITS);
  976.         clear_flg = 0;
  977.     }
  978.     size = fread((char *) pbuf, 1, n_bits, stdin );
  979.     if ( size <= 0 )
  980.         return -1;            /* end of file */
  981.     poffset = 0;
  982.     /* Round size down to integral number of codes */
  983.     size = ((long)size << 3) - (n_bits - 1);
  984.     }
  985.     r_off = poffset;
  986.     bits = n_bits;
  987. #ifdef vax
  988.     asm( "extzv   r10,r9,(r8),r11" );
  989. #else /* not a vax */
  990.     /*
  991.      * Get to the first byte.
  992.      */
  993.     bp += (r_off >> 3);
  994.     r_off &= 7;
  995.     /* Get first part (low order bits) */
  996. #ifdef NO_UCHAR
  997.     code = ((*bp++ >> r_off) & rmask[8 - r_off]) & 0xff;
  998. #else
  999.     code = (*bp++ >> r_off);
  1000. #endif /* NO_UCHAR */
  1001.     bits -= (8 - r_off);
  1002.     r_off = 8 - r_off;        /* now, poffset into code word */
  1003.     /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
  1004.     if ( bits >= 8 ) {
  1005. #ifdef NO_UCHAR
  1006.         code |= (*bp++ & 0xff) << r_off;
  1007. #else
  1008.         code |= *bp++ << r_off;
  1009. #endif /* NO_UCHAR */
  1010.         r_off += 8;
  1011.         bits -= 8;
  1012.     }
  1013.     /* high order bits. */
  1014.     code |= (*bp & rmask[bits]) << r_off;
  1015. #endif /* vax */
  1016.     poffset += n_bits;
  1017.  
  1018.     return code;
  1019. }
  1020.  
  1021. char *
  1022. rindex(s, c)        /* For those who don't have it in libc.a */
  1023. register char *s, c;
  1024. {
  1025.     char *p;
  1026.     for (p = NULL; *s; s++)
  1027.         if (*s == c)
  1028.         p = s;
  1029.     return(p);
  1030. }
  1031.  
  1032. #ifdef DEBUG
  1033. printcodes()
  1034. {
  1035.     /*
  1036.      * Just print out codes from input file.  For debugging.
  1037.      */
  1038.     code_int code;
  1039.     int col = 0, bits;
  1040.  
  1041.     bits = n_bits = INIT_BITS;
  1042.     maxcode = MAXCODE(n_bits);
  1043.     free_ent = ((block_compress) ? FIRST : 256 );
  1044.     while ( ( code = getcode() ) >= 0 ) {
  1045.     if ( (code == CLEAR) && block_compress ) {
  1046.            free_ent = FIRST - 1;
  1047.            clear_flg = 1;
  1048.     }
  1049.     else if ( free_ent < maxmaxcode )
  1050.         free_ent++;
  1051.     if ( bits != n_bits ) {
  1052.         (void)fprintf(stderr, "\nChange to %d bits\n", n_bits );
  1053.         bits = n_bits;
  1054.         col = 0;
  1055.     }
  1056.     (void)fprintf(stderr, "%5d%c", code, (col+=6) >= 74 ? (col = 0, '\n') : ' ' );
  1057.     }
  1058.     putc( '\n', stderr );
  1059.     exit( 0 );
  1060. }
  1061.  
  1062. code_int sorttab[1L<<BITS];    /* sorted pointers into htab */
  1063.  
  1064. dump_tab()    /* dump string table */
  1065. {
  1066.     register int i, first;
  1067.     register ent;
  1068. #define STACK_SIZE    15000
  1069.     int stack_top = STACK_SIZE;
  1070.     register c;
  1071.  
  1072.     if(do_decomp == 0) {    /* compressing */
  1073.     register int flag = 1;
  1074.  
  1075.     for(i=0; i<hsize; i++) {    /* build sort pointers */
  1076.         if((long)htabof(i) >= 0) {
  1077.             sorttab[codetabof(i)] = i;
  1078.         }
  1079.     }
  1080.     first = block_compress ? FIRST : 256;
  1081.     for(i = first; i < free_ent; i++) {
  1082.         (void)fprintf(stderr, "%5d: \"", i);
  1083.         de_stack[--stack_top] = '\n';
  1084.         de_stack[--stack_top] = '"';
  1085.         stack_top = in_stack((htabof(sorttab[i])>>maxbits)&0xff, 
  1086.                                      stack_top);
  1087.         for(ent=htabof(sorttab[i]) & ((1<<maxbits)-1);
  1088.             ent > 256;
  1089.             ent=htabof(sorttab[ent]) & ((1<<maxbits)-1)) {
  1090.             stack_top = in_stack(htabof(sorttab[ent]) >> maxbits,
  1091.                         stack_top);
  1092.         }
  1093.         stack_top = in_stack(ent, stack_top);
  1094.         (void)fwrite( &de_stack[stack_top], 1, STACK_SIZE-stack_top, stderr);
  1095.            stack_top = STACK_SIZE;
  1096.     }
  1097.    } else if(!debug) {    /* decompressing */
  1098.  
  1099.        for ( i = 0; i < free_ent; i++ ) {
  1100.        ent = i;
  1101.        c = tab_suffixof(ent);
  1102.        if ( isascii(c) && isprint(c) )
  1103.            (void)fprintf( stderr, "%5d: %5d/'%c'  \"",
  1104.                ent, tab_prefixof(ent), c );
  1105.        else
  1106.            (void)fprintf( stderr, "%5d: %5d/\\%03o \"",
  1107.                ent, tab_prefixof(ent), c );
  1108.        de_stack[--stack_top] = '\n';
  1109.        de_stack[--stack_top] = '"';
  1110.        for ( ; ent != NULL;
  1111.            ent = (ent >= FIRST ? tab_prefixof(ent) : NULL) ) {
  1112.            stack_top = in_stack(tab_suffixof(ent), stack_top);
  1113.        }
  1114.        (void)fwrite( &de_stack[stack_top], 1, STACK_SIZE - stack_top, stderr );
  1115.        stack_top = STACK_SIZE;
  1116.        }
  1117.     }
  1118. }
  1119.  
  1120. int
  1121. in_stack(c, stack_top)
  1122.     register c, stack_top;
  1123. {
  1124.     if ( (isascii(c) && isprint(c) && c != '\\') || c == ' ' ) {
  1125.         de_stack[--stack_top] = c;
  1126.     } else {
  1127.         switch( c ) {
  1128.         case '\n': de_stack[--stack_top] = 'n'; break;
  1129.         case '\t': de_stack[--stack_top] = 't'; break;
  1130.         case '\b': de_stack[--stack_top] = 'b'; break;
  1131.         case '\f': de_stack[--stack_top] = 'f'; break;
  1132.         case '\r': de_stack[--stack_top] = 'r'; break;
  1133.         case '\\': de_stack[--stack_top] = '\\'; break;
  1134.         default:
  1135.          de_stack[--stack_top] = '0' + c % 8;
  1136.          de_stack[--stack_top] = '0' + (c / 8) % 8;
  1137.          de_stack[--stack_top] = '0' + c / 64;
  1138.          break;
  1139.         }
  1140.         de_stack[--stack_top] = '\\';
  1141.     }
  1142.     return stack_top;
  1143. }
  1144. #endif /* DEBUG */
  1145.  
  1146. writeerr()
  1147. {
  1148.     perror ( ofname );
  1149.     (void)unlink ( ofname );
  1150.     exit ( 22 );
  1151. }
  1152.  
  1153. /*
  1154.  * This routine returns 1 if we are running in the foreground and stderr
  1155.  * is a tty.
  1156.  */
  1157. foreground()
  1158. {
  1159.     if(bgnd_flag) {    /* background? */
  1160.         return(0);
  1161.     } else {            /* foreground */
  1162.         if(isatty(2)) {        /* and stderr is a tty */
  1163.             return(1);
  1164.         } else {
  1165.             return(0);
  1166.         }
  1167.     }
  1168. }
  1169.  
  1170. onintr ( )
  1171. {
  1172.     if (oktozap) (void)unlink ( ofname );
  1173.     exit ( 23 );
  1174. }
  1175.  
  1176. oops ( )    /* wild pointer -- assume bad input */
  1177. {
  1178.     if ( do_decomp == 1 ) 
  1179.         (void)fprintf ( stderr, "uncompress: corrupt input\n" );
  1180.     if (oktozap) (void)unlink ( ofname );
  1181.     exit ( 24 );
  1182. }
  1183.  
  1184. cl_block ()        /* table clear for block compress */
  1185. {
  1186.     register long int rat;
  1187.  
  1188.     checkpoint = in_count + CHECK_GAP;
  1189. #ifdef DEBUG
  1190.     if ( debug ) {
  1191.             (void)fprintf ( stderr, "count: %ld, ratio: ", in_count );
  1192.              prratio ( stderr, in_count, bytes_out );
  1193.         (void)fprintf ( stderr, "\n");
  1194.     }
  1195. #endif /* DEBUG */
  1196.  
  1197.     if(in_count > 0x007fffff) {    /* shift will overflow */
  1198.     rat = bytes_out >> 8;
  1199.     if(rat == 0) {        /* Don't divide by zero */
  1200.         rat = 0x7fffffff;
  1201.     } else {
  1202.         rat = in_count / rat;
  1203.     }
  1204.     } else {
  1205.     rat = (in_count << 8) / bytes_out;    /* 8 fractional bits */
  1206.     }
  1207.     if ( rat > ratio ) {
  1208.     ratio = rat;
  1209.     } else {
  1210.     ratio = 0;
  1211. #ifdef DEBUG
  1212.     if(verbose)
  1213.         dump_tab();    /* dump string table */
  1214. #endif
  1215.      cl_hash ( (count_int) hsize );
  1216.     free_ent = FIRST;
  1217.     clear_flg = 1;
  1218.     output ( (code_int) CLEAR );
  1219. #ifdef DEBUG
  1220.     if(debug)
  1221.             (void)fprintf ( stderr, "clear\n" );
  1222. #endif /* DEBUG */
  1223.     }
  1224. }
  1225.  
  1226. cl_hash(phsize)        /* reset code table */
  1227.     register count_int phsize;
  1228. {
  1229. #ifndef XENIX_16    /* Normal machine */
  1230.     register count_int *htab_p = htab+phsize;
  1231. #else
  1232.     register j;
  1233.     register long k = phsize;
  1234.     register count_int *htab_p;
  1235. #endif
  1236.     register long i;
  1237.     register long m1 = -1;
  1238.  
  1239. #ifdef XENIX_16
  1240.     for(j=0; j<=8 && k>=0; j++,k-=8192) {
  1241.     i = 8192;
  1242.     if(k < 8192) {
  1243.         i = k;
  1244.     }
  1245.     htab_p = &(htab[j][i]);
  1246.     i -= 16;
  1247.     if(i > 0) {
  1248. #else
  1249.     i = phsize - 16;
  1250. #endif
  1251.      do {                /* might use Sys V memset(3) here */
  1252.         *(htab_p-16) = m1;
  1253.         *(htab_p-15) = m1;
  1254.         *(htab_p-14) = m1;
  1255.         *(htab_p-13) = m1;
  1256.         *(htab_p-12) = m1;
  1257.         *(htab_p-11) = m1;
  1258.         *(htab_p-10) = m1;
  1259.         *(htab_p-9) = m1;
  1260.         *(htab_p-8) = m1;
  1261.         *(htab_p-7) = m1;
  1262.         *(htab_p-6) = m1;
  1263.         *(htab_p-5) = m1;
  1264.         *(htab_p-4) = m1;
  1265.         *(htab_p-3) = m1;
  1266.         *(htab_p-2) = m1;
  1267.         *(htab_p-1) = m1;
  1268.         htab_p -= 16;
  1269.     } while ((i -= 16) >= 0);
  1270. #ifdef XENIX_16
  1271.     }
  1272.     }
  1273. #endif
  1274.         for ( i += 16; i > 0; i-- )
  1275.         *--htab_p = m1;
  1276. }
  1277.  
  1278. prratio(stream, num, den)
  1279. FILE *stream;
  1280. long int num, den;
  1281. {
  1282.     register int q;            /* Doesn't need to be long */
  1283.  
  1284.     if(num > 214748L) {        /* 2147483647/10000 */
  1285.         q = num / (den / 10000L);
  1286.     } else {
  1287.         q = 10000L * num / den;        /* Long calculations, though */
  1288.     }
  1289.     if (q < 0) {
  1290.         putc('-', stream);
  1291.         q = -q;
  1292.     }
  1293.     (void)fprintf(stream, "%d.%02d%%", q / 100, q % 100);
  1294. }
  1295.  
  1296. #ifdef C7
  1297.  
  1298. /* C7 code by Uwe Doering (gemini@netmbx.uucp) */
  1299.  
  1300. do_c7()
  1301. {
  1302.          int pid,wpid,pfildes[2],exit_stat = 0;
  1303.          FILE *rnews_pipe;
  1304.  
  1305.  
  1306.            if (pipe(pfildes) != 0) {
  1307.               fprintf(stderr, "could not open interprocess pipe\n");
  1308.               exit(7);
  1309.            }
  1310.            if ((pid = fork()) == -1) {
  1311.               fprintf(stderr, "could not fork\n");
  1312.               exit(8);
  1313.            }
  1314.            if (pid) {
  1315.               (void) fclose(stdin);
  1316.               if ((rnews_pipe = fdopen(pfildes[0],"r")) == NULL) {
  1317.              fprintf(stderr, "could not open rnews_pipe for input\n");
  1318.              exit(9);
  1319.               }
  1320.               (void) close(pfildes[1]);
  1321.               uncompress();
  1322.               (void) fclose(rnews_pipe);
  1323.               (void) close(pfildes[0]);
  1324.               while ((wpid = wait(&exit_stat)) != pid && wpid != -1)
  1325.                 ;
  1326.               if (exit_stat)
  1327.              exit(10);
  1328.            }
  1329.            else {
  1330.               int result;
  1331.  
  1332.               (void) fclose(relaynews);
  1333.               if ((rnews_pipe = fdopen(pfildes[1],"w")) == NULL) {
  1334.              fprintf(stderr, "could not open rnews_pipe for output\n");
  1335.              exit(11);
  1336.               }
  1337.               (void) close(pfildes[0]);
  1338.               result = c7decode();
  1339.               (void) fclose(rnews_pipe);
  1340.               (void) close(pfildes[1]);
  1341.               exit(result);
  1342.            }
  1343.  
  1344. }  /* do_c7 */
  1345.  
  1346. /* c7decode */
  1347.  
  1348. /*
  1349.  * This program is the inverse of encode
  1350.  *
  1351.  * It collects runs of 12 characters, combines pairs of those
  1352.  * to form 6 13 bit numbers, extracts the top bit of each of
  1353.  * those to make a 13th 6 bit character, and splits each of
  1354.  * the remaining 6 12 bit numbers to form 12 6 bit ones.
  1355.  *
  1356.  * The strings of 6 bit numbers are collected into groups of
  1357.  * 4 and converted into 3 8 bit characters.
  1358.  *
  1359.  * Now all that would be trivial, if we didn't need to worry
  1360.  * about ending all this correctly.  About 1/2 of the following
  1361.  * program wouldn't be here if the ending didn't matter....
  1362.  */
  1363.  
  1364. /*
  1365.  * the following pair of characters can never occur as a pair
  1366.  * in legal input (since (90 * 91 + 90) > 2^13) - they are
  1367.  * noticed at the beginning of a 12 char block, and serve to
  1368.  * indicate that this block is the terminator.  The character
  1369.  * immediately following is the (expanded) terminator length.
  1370.  */
  1371. #define    ENDMARK1    ((90*91 + 90) / 91)
  1372. #define    ENDMARK2    ((90*91 + 90) % 91)
  1373.  
  1374. int errcnt = 0;
  1375.  
  1376. c7decode()
  1377. {
  1378.     register c;
  1379.     register char *p;
  1380.     register i;
  1381.     register first = 1;
  1382.     register cnt = 0;
  1383.     char b12[12];
  1384.     char c12[12];
  1385.  
  1386.     p = b12;
  1387.     i = 12;
  1388.  
  1389.     while ((c = getchar()) != EOF) {
  1390.         if (c < ' ' || c >= (' ' + 91)) {
  1391.             if (errcnt++ == 0)
  1392.                 fprintf(stderr, "c7decode: Bad data\n");
  1393.             continue;
  1394.         }
  1395.         if (i == 10 && p[-1] == ENDMARK1 && p[-2] == ENDMARK2) {
  1396.             cnt = c - ' ';
  1397.             i = 12;
  1398.             p -= 2;
  1399.             continue;
  1400.         }
  1401.         *p++ = c - ' ';
  1402.         if (--i == 0) {
  1403.             if (p == &b12[12]) {
  1404.                 if (!first)
  1405.                     pack12(c12, 12, 0);
  1406.                 else
  1407.                     first = 0;
  1408.                 p = c12;
  1409.             } else {
  1410.                 pack12(b12, 12, 0);
  1411.                 p = b12;
  1412.             }
  1413.             i = 12;
  1414.         }
  1415.     }
  1416.  
  1417.     if (p >= &b12[0] && p < &b12[12]) {
  1418.         if (!first)
  1419.             pack12(c12, 12, i == 12 ? cnt : 0);
  1420.     } else
  1421.         pack12(b12, 12, i == 12 ? cnt : 0);
  1422.  
  1423.     if (i != 12) {
  1424.         if (p >= &b12[0] && p < &b12[12])
  1425.             pack12(b12, 12-i, cnt);
  1426.         else
  1427.             pack12(c12, 12-i, cnt);
  1428.     }
  1429.  
  1430.     return((errcnt > 0) ? 1 : 0);
  1431. }
  1432.  
  1433. static char b4[4];
  1434. static int cnt = 0;
  1435.  
  1436. pack12(p, n, last)
  1437.     register char *p;
  1438.     register n;
  1439.     int last;
  1440. {
  1441.     register i;
  1442.     register char *q;
  1443.     char b13[13];
  1444.  
  1445.     {
  1446.         register c;
  1447.         register c13;
  1448.  
  1449.         q = b13;
  1450.         c13 = 0;
  1451.  
  1452.         for (i = 0; i < n; i += 2) {
  1453.             c = *p++ * 91;
  1454.             c += *p++;
  1455.             c13 <<= 1;
  1456.             if (c & (1 << 12))
  1457.                 c13 |= 1;
  1458.             *q++ = (c >> 6) & 0x3f;
  1459.             *q++ = c & 0x3f;
  1460.         }
  1461.         *q++ = c13;
  1462.         if (last)
  1463.             q = &b13[last];
  1464.     }
  1465.  
  1466.     p = b13;
  1467.     n = q - p;
  1468.     i = cnt;
  1469.     q = &b4[cnt];
  1470.  
  1471.     while (--n > 0) {
  1472.         *q++ = *p++;
  1473.         if (++i == 4) {
  1474.             char b3[3];
  1475.             register char *b = b4;
  1476.  
  1477.             /* inline expansion of pack6bit, to save calls ... */
  1478.  
  1479.             q = b3;
  1480.             *q++ = (b[0] << 2) | ((b[1] >> 4) & 0x3);
  1481.             *q++ = (b[1] << 4) | ((b[2] >> 2) & 0xf);
  1482.             *q = (b[2] << 6) | (b[3] & 0x3f);
  1483.  
  1484.             q = b3;
  1485.             while (--i > 0)
  1486.                 putchar(*q++);
  1487.  
  1488.             q = b4;
  1489.         }
  1490.     }
  1491.  
  1492.     *q++ = *p++;    /* the last octet */
  1493.     ++i;
  1494.  
  1495.     if (last || i == 4) {
  1496.         pack6bit(b4, i, last);
  1497.         i = 0;
  1498.     }
  1499.  
  1500.     cnt = i;
  1501. }
  1502.  
  1503. pack6bit(p, n, last)
  1504.     register char *p;
  1505.     register int n;
  1506.     int last;
  1507. {
  1508.     register char *q;
  1509.     register i = 3;
  1510.     char b3[3];
  1511.  
  1512.     if (last) {
  1513.         i = p[n-1];
  1514.         if (i >= 3) {
  1515.             fprintf(stderr, "c7decode: Badly encoded file\n");
  1516.             errcnt++;
  1517.             i = 3;        /* do the best we can */
  1518.         }
  1519.     }
  1520.  
  1521.     q = b3;
  1522.     *q++ = (p[0] << 2) | ((p[1] >> 4) & 0x3);
  1523.     *q++ = (p[1] << 4) | ((p[2] >> 2) & 0xf);
  1524.     *q = (p[2] << 6) | (p[3] & 0x3f);
  1525.  
  1526.     q = b3;
  1527.  
  1528.     while (--i >= 0)
  1529.         putchar(*q++);
  1530. }
  1531.  
  1532. #endif
  1533.