home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / shark.lzh / compress.c < prev    next >
Text File  |  1990-11-12  |  28KB  |  638 lines

  1. /*@H************************ < COMPRESS utility> ****************************
  2. *   $@(#) compress.c,v 4.3d 90/01/18 03:00:00 don Release ^                 *
  3. *                                                                           *
  4. *   compress : compress.c                                                   *
  5. *              Main and Operating System Independent support functions      *
  6. *                                                                           *
  7. *   port by  : Donald J. Gloistein                                          *
  8. *                                                                           *
  9. *   Source, Documentation, Object Code:                                     *
  10. *   released to Public Domain. This code is ported from compress v4.0       *
  11. *   release joe.                                                            *
  12. *---------------------------  Module Description  --------------------------*
  13. *   The compress program is compatible with the compression/decompression   *
  14. *   used on the Unix systems compress programs.  This is version 4 and      *
  15. *   supports up to 16 bits compression. The porting retained the Unix       *
  16. *   meanings of all options, added a couple for MsDos and modified the      *
  17. *   file name conventions to make more sense.                               *
  18. *                                                                           *
  19. *--------------------------- Implementation Notes --------------------------*
  20. *                                                                           *
  21. *   compiled with : compress.h compress.fns                                 *
  22. *   linked with   : compapi.obj  compusi.obj                                *
  23. *   problems:                                                               *
  24. *              See notes in compress.h for defines needed.                  *
  25. *              It should work now with Xenix                                *
  26. *                                                                           *
  27. *              Check the signal() handler functions in your compiler        *
  28. *              documentation. This code assumes ANSI SYS V compatible       *
  29. *              header and return values. Change as appropriate for your     *
  30. *              compiler and operating system.                               *
  31. *                                                                           *
  32. *              This source compiles properly with Microsoft C compiler      *
  33. *              version 5.1.                                                 *
  34. *                                                                           *
  35. *   CAUTION:   because the program is in modules, make sure you recompile   *
  36. *              all modules if you change the header or a define in the      *
  37. *              compress.c file                                              *
  38. *                                                                           *
  39. * Algorithm from "A Technique for High Performance Data Compression",       *
  40. * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.          *
  41. *                                                                           *
  42. * Assumptions:                                                              *
  43. *  When filenames are given, replaces with the compressed version           *
  44. *  (.Z suffix) only if the file decreases in size.                          *
  45. * Algorithm:                                                                *
  46. *  Modified Lempel-Ziv method (LZW).  Basically finds common                *
  47. * substrings and replaces them with a variable size code.  This is          *
  48. * deterministic, and can be done on the fly.  Thus, the decompression       *
  49. * procedure needs no input table, but tracks the way the table was built.   *
  50. *                                                                           *
  51. *                                                                           *
  52. *---------------------------      Author(s)        -------------------------*
  53. *     Initials ---- Name ---------------------------------                  *
  54. *      DjG          Donald J. Gloistein                                     *
  55. *                   Plus many others, see rev.hst file for full list        *
  56. *      Dal          Dale A. Schumacher (Sozobon C port)                     *
  57. *      LvR          Lyle V. Rains, many thanks for improved implementation  *
  58. *************************************************************************@H*/
  59.  
  60. /*@R************************< Revision History >*****************************
  61. *                                                                           *
  62. *   version -- date -- init ---Notes----------------------                  *
  63. *    4.01    08-29-88  DjG    first cut  for 16 bit MsDos version           *
  64. *            09-04-88  DjG    fixed unlink on zcat if interupted.           *
  65. *                             added msdos filename logic and functions      *
  66. *    4.10    10-27-88  DjG  revised API with coding changes by LvR.         *
  67. *    4.10a   10-30-88  DjG  cleaned up code and fixed bug in freeing ptr.   *
  68. *    4.10b   11-01-88  DjG  cleaned up the logic for inpath/outpath         *
  69. *                           Changed the logic to finding the file name      *
  70. *                           Fixed the allocation bug in the api             *
  71. *                           Added some more portability macros              *
  72. *    4.10c   11-04-88  DjG  Changed maxcode from global to static in api.   *
  73. *                           Supplied some library functions for those who   *
  74. *                           don't have them, changed dos usi to use the     *
  75. *                           strrpbrk(). Checked casts in api again. Compiles*
  76. *                           without warnings at pick level 3.               *
  77. *    4.10d   11-25-88  DjG  revised some memory allocation, put more in the *
  78. *                           header file. Corrected some typos.              *
  79. *                           Changed prog_name() to force lower case         *
  80. *                           Corrected bug, no longer unlinks existing file  *
  81. *                           if not enough memory to compress or decompress  *
  82. *            12-06-88  DjG  VERY minor changes for casts and header defines *
  83. *            12-08-88  DjG  Adjusted path separator check in main function  *
  84. *                           Amiga uses split seg because of compiler        *
  85. *            12-09-88  DjG  Debugging done, all defaults now Unix compress  *
  86. *                           defaults, including unlinking input file and    *
  87. *                           acting as a filter. Must use -h option to get   *
  88. *                           help screen.                                    *
  89. *    4.10e   12-11-88  DjG  Fixed more casts, prototypes and header file.   *
  90. *    4.10f   12-12-88  DjG  Fixed unlinking open files on error. This fails *
  91. *                           on shared or os/2 platforms.                    *
  92. *            12-15-88  DjG  Fixed SIGTYPE for function passed to signal     *
  93. *                           Fixed problems with Xenix 2.2.1                 *
  94. *    4.2     12-19-88  DjG  Replaced adaptive reset as an option.           *
  95. *    4.3     12-26-88  DjG  Fixed long file name bug, fixed bug with        *
  96. *                           compressdir. -B option added, same as -b option *
  97. *            05-06-89  Dal  Ported to Sozobon/Alcyon C for Atari ST.  Also, *
  98. *                           created get_one() for console prompting.        *
  99. *            05-08-89  Dal  Ported to Minix-ST                              *
  100. *    4.3a    05-29-89  DjG  Combined source code changes and now compiles   *
  101. *                           on Minix.                                       *
  102. *    4.3b    08-20-89  DjG  Changed the version() to simplify it. Changed   *
  103. *                           the order of testing for linked files           *
  104. *            10-02-89  DjG  Changed the double negative #ifndef NDEBUG      *
  105. *                to a more logical coding.                       *
  106. *    4.3c    12-25-89  DjG  Fixed pointer bug in error message code         *
  107. *            01-06-90  LvR  Fixed signed expansion on 68000 cpu's           *
  108. *    4.3d    01-18-90  LvR  Fixed problem with token[] overrunning on some  *
  109. *                           files with large amounts of repeating characters*
  110. *************************************************************************@R*/
  111.  
  112. #include <stdio.h>
  113.  
  114. #define MAIN        /* header has defining instances of globals */
  115. #include "compress.h" /* contains the rest of the include file declarations */
  116.  
  117. #define ARGVAL() (*++(*argv) || (--argc && *++argv))
  118. char suffix[] = SUFFIX ;          /* only used in this file */
  119.  
  120. void main( argc, argv )
  121. register int argc; char **argv;
  122. {
  123.     char **filelist, **fileptr,*temp;
  124.     char response;
  125.     struct stat statbuf;
  126.  
  127. #ifndef NOSIGNAL
  128.     if ( (bgnd_flag = signal ( SIGINT, SIG_IGN )) != SIG_IGN ) {
  129.         /* ANSI/SYS V compatible */
  130.         /* the following test checks for error on setting signals */
  131.         /* check your documentation on the value to test          */
  132.         /* if your signal.h doesn't support the return, it is     */
  133.         /* essentially a no-op test                               */
  134.         if (bgnd_flag == SIG_ERR){   
  135.             exit_stat = SIGNAL_ERROR;
  136.             check_error();
  137.         }
  138.         if( (signal(SIGINT,onintr) == SIG_ERR)
  139.         || (signal(SIGSEGV,oops) == SIG_ERR)) {/* check your compiler docs. */
  140.             exit_stat = SIGNAL_ERROR;
  141.             check_error();
  142.         }
  143.     }
  144. #endif
  145.  
  146.     /* set up array for files to be converted */
  147. #ifdef ALLOC
  148.     filelist = fileptr = (char **)(alloc(argc * sizeof(char *)));
  149. #else
  150.     filelist = fileptr = (char **)(malloc(argc * sizeof(char *)));
  151. #endif
  152.     *filelist = NULL;
  153.  
  154.     /* gets name, compares and sets defaults */
  155.     prog_name = get_program_name(argv[0]);
  156.  
  157.     /* now parse command line and get file list */
  158.     for (argc--, argv++; argc > 0; argc--, argv++) {
  159.         if (**argv == '-') {        /* A flag argument */
  160.             while (*++(*argv)) {    /* Process all flags in this arg */
  161.                 switch (**argv) {
  162. #ifdef DEBUG
  163.                     case 'D':
  164.                         debug = TRUE;
  165.                         keep_error = TRUE;
  166.                         break;
  167.                     case 'V':
  168.                         verbose = TRUE;
  169.                         version();
  170.                         break;
  171. #else
  172.                     case 'V':
  173.                         version();
  174.                         break;
  175. #endif /*DEBUG */
  176.                     case 'v':
  177.                         quiet = FALSE;
  178.                         break;
  179.                     case 'd':
  180.                         do_decomp = TRUE;
  181.                         break;
  182.                     case 'f':
  183.                         force = overwrite = TRUE;
  184.                         break;
  185.                     case 'n':
  186.                         nomagic = TRUE;
  187.                         break;
  188.                     case 'C':
  189.                         block_compress = FALSE;
  190.                         break;
  191.                     case 'b': case 'B':
  192.                         if (!ARGVAL()) {
  193.                             fprintf(stderr, "Missing maxbits\n");
  194.                             Usage(1);
  195.                             exit(ERROR);
  196.                         }
  197.                         maxbits = atoi(*argv);
  198.                         goto nextarg;
  199.                     case 'I':
  200.                         if (!ARGVAL()) {
  201.                             fprintf(stderr, "Missing in_path name\n");
  202.                             Usage(1);
  203.                             exit(ERROR);
  204.                         }
  205.                         strcpy(inpath,*argv);
  206.                         temp = &inpath[strlen(inpath)-1];
  207. #ifdef MSDOS
  208.                         if (*temp != '\\' && *temp != '/')
  209. #else
  210.                         if (*temp != separator[0])
  211. #endif
  212.                             strcat(inpath,separator);
  213.                         goto nextarg;
  214.                     case 'O':
  215.                         if (!ARGVAL()){
  216.                             fprintf(stderr, "Missing out_path name\n");
  217.                             Usage(1);
  218.                             exit(ERROR);
  219.                         }
  220.                         strcpy(outpath,*argv);
  221.                         temp = &outpath[strlen(outpath)-1];
  222. #ifdef MSDOS
  223.                         if (*temp != '\\' && *temp != '/')
  224. #else
  225.                         if (*temp != separator[0])
  226. #endif
  227.                             strcat(outpath,separator);
  228.                         goto nextarg;
  229.                     case 'c':
  230.                         keep = zcat_flg = TRUE;
  231.                         break;
  232.                     case 'K':
  233.                         keep_error = TRUE;
  234.                         break;
  235.                     case 'k':
  236.                         keep = !keep;
  237.                         break;
  238.                     case '?':case 'h':case 'H':
  239.                         Usage(0);
  240.                         exit(NORMAL);
  241.                         break;
  242.                     case 'q':
  243.                         quiet = TRUE;
  244.                         break;
  245.                     default:
  246.                         fprintf(stderr, "%s : Unknown flag: '%c'\n",prog_name, **argv);
  247.                         Usage(1);
  248.                         exit(ERROR);
  249.                 } /* end switch */
  250.             } /* end while processing this argument */
  251.         }  /* end if option parameter */
  252.         else {                                  /* must be input file name */
  253.             *fileptr++ = *argv;                 /* Build input file list */
  254.             *fileptr = NULL;
  255.         } /* end else */
  256. nextarg:     continue;                          /* process nextarg */
  257.     } /* end command line processing */
  258.  
  259.     /* adjust for possible errors or conflicts */
  260.     if(maxbits < MINBITS || maxbits > MAXBITS){
  261.         fprintf(stderr,"\n%s: illegal bit value, range = %d to %d\n",prog_name,MINBITS,MAXBITS);
  262.         exit(NORMAL);
  263.     }
  264.     if (zcat_flg && *outpath)         /* can't have an out path and zcat */
  265.         *outpath = '\0';
  266.  
  267.     /* to make the error messages make sense */
  268.     strcpy(ifname,"stdin");
  269.     strcpy(ofname,"stdout");
  270.  
  271.     if (*filelist) {         /* Check if there are files specified */
  272.                              /* *fileptr must continue to specify  */
  273.                              /* command line in/out file name      */
  274.         is_list = TRUE;
  275.         for (fileptr = filelist; *fileptr; fileptr++) {
  276.             exit_stat = 0;
  277.             endchar[0] = '\0';
  278.             if (do_decomp) {                /* DECOMPRESSION          */
  279.                 if (*inpath){               /* adjust for inpath name */
  280.                     strcpy(ifname,inpath);  /* and copy into ifname   */
  281.                     strcat(ifname,name_index(*fileptr));
  282.                 }
  283.                 else
  284.                     strcpy(ifname,*fileptr);
  285.                 if(!is_z_name(ifname))         /* Check for .Z suffix    */
  286.                     if(!(make_z_name(ifname))) /* No .Z: tack one on     */
  287.                         continue;
  288.                                                /* Open input file        */
  289.                 if ((freopen(ifname, READ_FILE_TYPE, stdin)) == NULL) {
  290.                     perror(ifname);
  291.                     continue;
  292.                 }
  293.                 else
  294.                     setvbuf(stdin,zbuf,_IOFBF,ZBUFSIZE);
  295.                 if (!nomagic) {             /* Check the magic number */
  296.                     if ((getchar() != (magic_header[0] & 0xFF))
  297.                       || (getchar() != (magic_header[1] & 0xFF))) {
  298.                         fprintf(stderr, "%s: not in compressed format\n",
  299.                             ifname);
  300.                         continue;
  301.                     }
  302.                     maxbits = getchar();    /* set -b from file */
  303.                     block_compress = maxbits & BLOCK_MASK;
  304.                     maxbits &= BIT_MASK;
  305.                     if(maxbits > MAXBITS) {
  306.                         fprintf(stderr,
  307.                         "%s: compressed with %d bits, can only handle %d bits\n",
  308.                         ifname, maxbits, MAXBITS);
  309.                         continue;
  310.                     }
  311.                 }  /* end if nomagic */
  312.                                              /* Generate output filename */
  313.                 if (*outpath){               /* adjust for outpath name */
  314.                     strcpy(ofname,outpath);  /* and copy into ofname   */
  315.                     strcat(ofname,name_index(ifname));
  316.                 }
  317.                 else
  318.                     strcpy(ofname,ifname); /* DjG may screw up the placement */
  319.                                            /* of the outfile */
  320.                 unmake_z_name(ofname);     /* strip off Z or .Z */
  321.             }
  322.             else {            /* COMPRESSION */
  323.                 if (*inpath){               /* adjust for inpath name */
  324.                     strcpy(ifname,inpath);  /* and copy into ifname   */
  325.                     strcat(ifname,name_index(*fileptr));
  326.                 }
  327.                 else
  328.                     strcpy(ifname,*fileptr);
  329.                 if (is_z_name(ifname)) {
  330.                     fprintf(stderr, "%s: already has %s suffix -- no change\n",
  331.                         ifname,suffix);
  332.                     continue;
  333.                 }
  334.                 /* Open input file */
  335.                 if ((freopen(ifname,READ_FILE_TYPE, stdin)) == NULL) {
  336.                     perror(ifname);
  337.                     continue;
  338.                 }
  339.                 else
  340.                     setvbuf(stdin,xbuf,_IOFBF,XBUFSIZE);
  341.                 /* Generate output filename */
  342.                 if (*outpath){               /* adjust for outpath name */
  343.                     strcpy(ofname,outpath);  /* and copy into ofname   */
  344.                     strcat(ofname,name_index(ifname));
  345.                 }
  346.                 else  /* place it in directory of input file */
  347.                     strcpy(ofname,ifname); /* DjG may screw up the placement */
  348.                                            /* of the outfile */
  349.                 if (!(make_z_name(ofname)))
  350.                     continue;
  351.             } /* end else compression  we now have the files set up */
  352.  
  353.             /* Check for overwrite of existing file */
  354.             if (!overwrite && !zcat_flg) {
  355.                 if (!stat(ofname, &statbuf)) {
  356.                     response = 'n';
  357.                     fprintf(stderr, "%s already exists;", ofname);
  358. #ifndef NOSIGNAL
  359.                     if (foreground()) {
  360. #else
  361.                     if (TRUE) {
  362. #endif
  363.                         fprintf(stderr, "\ndo you wish to overwrite %s (y or n)? ",
  364.                         ofname);
  365.                         fflush(stderr);
  366.                         response = get_one();
  367.                     }
  368.                     if ((response != 'y') && (response != 'Y')){
  369.                         fprintf(stderr, "\tnot overwritten\n");
  370.                         continue;
  371.                     }
  372.                 } /* end if stat */
  373.             } /* end if overwrite */
  374.             /* Output file  is opened in compress/decompress routines */
  375.             if (!zcat_flg)
  376.                 if(test_file(ifname)){
  377.             putc('\n',stderr);
  378.                     continue;  /* either linked or not a regular file */
  379.          }
  380.             /* Actually do the compression/decompression  on files */
  381.             if (!do_decomp){
  382.                 compress();
  383.                 check_error();
  384.             }
  385.             else{
  386.                 decompress();
  387.                 check_error();
  388.             }
  389.             if(!zcat_flg) {
  390.                 copystat(ifname, ofname); /* Copy stats */
  391.                 if((exit_stat ) || (!quiet))
  392.                     putc('\n', stderr);
  393.             }       /* end if zcat */
  394.         }           /*end for  loop */
  395.     }               /* end if filelist */
  396.     else {          /* it is standard input to standard output*/
  397. #if (FILTER == FALSE)     /* filter is defined as true or false */
  398.     /* DjG added to make more sense.  The following tests for standard
  399.        input being a character device. If so, there is no use in MsDos
  400.        for the program, as that will compress from the keyboard to the
  401.        console. Sure not what is needed. Instead, the usage function
  402.        is called. In Xenix/Unix systems, there is a need for this type
  403.        of pipe as the input may be from a char dev, remote station.
  404.      */
  405.  
  406.         /* check if input is unredirected */
  407.         if ( isatty(fileno(stdin)) ){
  408.             Usage(1);
  409.             exit(NORMAL);
  410.         }
  411. #endif
  412.         /* filter */
  413.         if (do_decomp){
  414.             setvbuf(stdin,zbuf,_IOFBF,ZBUFSIZE);  /* make the buffers larger */
  415.             setvbuf(stdout,xbuf,_IOFBF,XBUFSIZE);
  416.         }
  417.         else{
  418.             setvbuf(stdin,xbuf,_IOFBF,XBUFSIZE);  /* make the buffers larger */
  419.             setvbuf(stdout,zbuf,_IOFBF,ZBUFSIZE);
  420.         }
  421.         if (!do_decomp) {   /* compress stdin to stdout */
  422.             compress();
  423.             check_error();
  424.             if(!quiet)
  425.                 putc('\n', stderr);
  426.         } /* end compress stdio */
  427.         else {   /* decompress stdin to stdout */
  428.             /* Check the magic number */
  429.             if (!nomagic) {
  430.                 if ((getchar()!=(magic_header[0] & 0xFF))
  431.                  || (getchar()!=(magic_header[1] & 0xFF))) {
  432.                     fprintf(stderr, "stdin: not in compressed format\n");
  433.                     exit(ERROR);
  434.                 }
  435.                 maxbits = getchar();    /* set -b from file */
  436.                 block_compress = maxbits & BLOCK_MASK;
  437.                 maxbits &= BIT_MASK;
  438.                 if(maxbits > MAXBITS) {
  439.                     fprintf(stderr,
  440.                     "stdin: compressed with %d bits, can only handle %d bits\n",
  441.                     maxbits, MAXBITS);
  442.                     exit(ERROR);
  443.                 }
  444.             }
  445.             decompress();
  446.             check_error();
  447.         } /* end else decomp stdio */
  448.     } /* end else standard input */
  449.     exit(exit_stat);
  450. }
  451.  
  452. void Usage(flag)
  453. int flag;
  454. {
  455. static char *keep2 =  "keep";
  456. static char *keep3 =  "kill (erase)";
  457. static char *on = "on";
  458. static char *off = "off";
  459.  
  460. #ifdef DEBUG
  461.     fprintf(stderr,"Usage: %s [-cCdDf?hkKvV][-b maxbits][-Iinpath][-Ooutpath][filenames...]\n",
  462.         prog_name);
  463. #else
  464.     fprintf(stderr,"Usage: %s [-cCdf?hkKvV][-b maxbits][-Iinpath][-Ooutpath][filenames...]\n",
  465.         prog_name);
  466. #endif
  467.     if (flag)
  468.         return;
  469.     fprintf(stderr,"Argument Processing..case is significant:\n");
  470.     fprintf(stderr,"     MUST use '-' for switch character\nAll flags are optional.\n");
  471. #ifdef DEBUG
  472.     fprintf(stderr,"     -D => debug; Keep file on error.\n");
  473.     fprintf(stderr,"     -V => print Version; debug verbose\n");
  474. #else
  475.     fprintf(stderr,"     -V => print Version\n");
  476. #endif
  477.     fprintf(stderr,"     -d => do_decomp default = %s\n",(do_decomp)?on:off);
  478.     fprintf(stderr,"     -v => verbose\n");
  479.     fprintf(stderr,"     -f => force overwrite of output file default = %s\n",
  480.         (force)?on:off);
  481.     fprintf(stderr,"     -n => no header: useful to uncompress old files\n");
  482.     fprintf(stderr,"     -c => cat all output to stdout default = %s\n",
  483.         (zcat_flg)?on:off);
  484.     fprintf(stderr,"     -C => generate output compatible with compress 2.0.\n");
  485.     fprintf(stderr,"     -k => %s input file, default == %s\n",(keep)?keep3:keep2,
  486.             (keep)?keep2:keep3);
  487.     fprintf(stderr,"     -K => %s output file on error, default == %s\n",
  488.             (keep_error)?keep3:keep2,(keep_error)?keep2:keep3);
  489.     fprintf(stderr,"     -b maxbits  => default == %d bits, max == %d bits\n",maxbits,MAXBITS);
  490.     fprintf(stderr,"     -I pathname => infile path  == %s\n",inpath);
  491.     fprintf(stderr,"     -O pathname => outfile path == %s\n",outpath);
  492.     fprintf(stderr,"     -? -h => help usage.\n");
  493. }
  494.  
  495. /*
  496.  * All previous #ifdef'ed code should return() a value.
  497.  * If no other option is available, the following is the original code.
  498.  * It not only reads from stderr (not a defined operation)
  499.  * but it does so via an explicit read() call on file descriptor 2!
  500.  * So much for portability.                    -Dal
  501.  */
  502. /* Dal included code to use the MSC getche() but it crashes because of
  503.    the freopening of stdin. Have to use the cludge for MSC. This function is
  504.    included so that others who have problems with the keyboard read can
  505.    change this function for their system.  DjG
  506.  */
  507.  
  508. char get_one()
  509. /*
  510.  * get a single character, with echo.
  511.  */
  512. {
  513.     char tmp[2];
  514.     int fd;
  515.  
  516. #ifdef SOZOBON
  517.     return(0x7F & getche());
  518. }
  519. #else
  520. #   ifdef MINIX
  521.             fd = open("/dev/tty", 0);   /* open the tty directly */
  522. #   else
  523.             fd = fileno(stderr);             /* read from stderr */
  524. #   endif
  525.         read(fd, tmp, 2);
  526.         while (tmp[1] != '\n') {
  527.                 if (read(fd, tmp+1, 1) < 0) {   /* Ack! */
  528.                         perror("stderr");
  529.                         break;
  530.                 }
  531.         }
  532.     return(tmp[0]);
  533. }
  534. #endif
  535.  
  536. void writeerr()
  537. {
  538.     perror ( ofname );
  539.     if (!zcat_flg && !keep_error){
  540.         fclose(stdout);
  541.         unlink ( ofname );
  542.     }
  543.     exit ( 1 );
  544. }
  545.  
  546. #ifndef NOSIGNAL
  547. /*
  548.  * This routine returns 1 if we are running in the foreground and stderr
  549.  * is a tty.
  550.  */
  551. int foreground()
  552. {
  553.     if(bgnd_flag) { /* background? */
  554.         return(0);
  555.     }
  556.     else {            /* foreground */
  557.         if(isatty(2)) {     /* and stderr is a tty */
  558.             return(1);
  559.         } else {
  560.             return(0);
  561.         }
  562.     }
  563. }
  564. #endif
  565.  
  566. void prratio(stream, num, den)
  567. FILE *stream;
  568. long int num, den;
  569. {
  570.     register int q;         /* Doesn't need to be long */
  571.  
  572.     if(num > 214748L) {     /* 2147483647/10000 */
  573.         q = (int) (num / (den / 10000L));
  574.     }
  575.     else {
  576.         q = (int) (10000L * num / den);     /* Long calculations, though */
  577.     }
  578.     if (q < 0) {
  579.         putc('-', stream);
  580.         q = -q;
  581.     }
  582.     fprintf(stream, "%d.%02d%%", q / 100, q % 100);
  583. }
  584.  
  585.  
  586. int check_error()     /* returning OK continues with processing next file */
  587. {
  588.     switch(exit_stat) {
  589.   case OK:
  590.     return (OK);
  591.   case NOMEM:
  592.     if (do_decomp)
  593.         fprintf(stderr,"%s: not enough memory to decompress '%s'.\n", prog_name, ifname);
  594.     else
  595.         fprintf(stderr,"%s: not enough memory to compress '%s'.\n", prog_name, ifname);
  596.     break;
  597.     /* return(OK); */   /* DjG removed causes problems */
  598.   case SIGNAL_ERROR:
  599.     fprintf(stderr,"%s: error setting signal interupt.\n",prog_name);
  600.     exit(ERROR);
  601.     break;
  602.   case READERR:
  603.     fprintf(stderr,"%s: read error on input '%s'.\n", prog_name, ifname);
  604.     break;
  605.   case WRITEERR:
  606.     fprintf(stderr,"%s: write error on output '%s'.\n", prog_name, ofname);
  607.     break;
  608.    case TOKTOOBIG:
  609.     fprintf(stderr,"%s: token too long in '%s'. Failed on realloc().\n", prog_name, ifname);
  610.     break;
  611.   case INFILEBAD:
  612.     fprintf(stderr, "%s: '%s' in unknown compressed format.\n", prog_name, ifname);
  613.     break;
  614.  case CODEBAD:
  615.     fprintf(stderr,"%s: file token bad in '%s'.\n", prog_name,ifname);
  616.     break;
  617.  case TABLEBAD:
  618.     fprintf(stderr,"%s: internal error -- tables corrupted.\n", prog_name);
  619.     break;
  620.   case NOTOPENED:
  621.     fprintf(stderr,"%s: could not open output file %s\n",prog_name,ofname);
  622.     exit(ERROR);
  623.     break;
  624.   case NOSAVING:
  625.     if (force)
  626.         exit_stat = OK;
  627.     return (OK);
  628.   default:
  629.     fprintf(stderr,"%s: internal error -- illegal return value = %d.\n", prog_name,exit_stat);
  630.   }
  631.   if (!zcat_flg && !keep_error){
  632.         fclose(stdout);         /* won't get here without an error */
  633.         unlink ( ofname );
  634.     }
  635.   exit(exit_stat);
  636.   return(ERROR);
  637. }
  638.