home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / c-kermit / ckipct.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  5KB  |  181 lines

  1. /*
  2.  * MSBPCT.C
  3.  *
  4.  * Howie Kaye -- Columbia University 3/11/86
  5.  *
  6.  * sibling program to MSBMKB.C.  It is used to unpack BOO files, used for
  7.  * encoding binary files into text, and back.  This program does the decoding.
  8.  * It is meant to replace the program "MSBPCT.BAS", and runs approximately
  9.  * 200 times faster.
  10.  *
  11.  * For documentation on BOO file format, see MSBMKB.C, or MS-Kermit manual.
  12.  * This program runs, as is, under Microsoft C on MSDOS, and under UNIX(4.2).
  13.  *
  14.  * Modification history:
  15.  *
  16.  * 1/1/88, Frank da Cruz.  Remove default input file names -- too
  17.  *   confusing.  Add exit(0) for good return code upon success. 
  18.  *
  19.  * 3/23/86 - Davide P. Cervone -- University of Rochester
  20.  *   added AMIGA and VAX11C support
  21.  *
  22.  * 3/24/86 - Martin Knoblauch -- TH-Darmstadt              (MK001)
  23.  *   test if 1. line of inputfile is delimited by "\r\n" instead of "\n"
  24.  *
  25.  * 5/5/86 - John Matthews, U of Delaware.
  26.  *   Explicitly close the files.
  27.  *
  28.  * 5/8/86 - L. John Junod, DTNSRDC.
  29.  *   Adapt for Computer Innovations MS-DOS CI-86 Compiler
  30.  *   Improve too many args error message
  31.  */
  32.  
  33. #define MSDOS
  34. #define CI86
  35.  
  36. #include <stdio.h>
  37.  
  38. #ifdef AMIGA
  39. #include <fcntl.h>
  40. #else
  41. #ifdef MSDOS 
  42. #ifndef CI86
  43. #include <fcntl.h>
  44. #endif
  45. #else
  46. #ifdef vax11c
  47. #include <file.h>
  48. #else
  49. #include <sys/file.h>
  50. #endif
  51. #endif
  52. #endif
  53.  
  54. #define fixchr(x) ((x) -'0')
  55. #define NULLCHR fixchr('~')
  56.  
  57. yes_or_no_p(arg) char *arg; {
  58.   int c,x;
  59.   while (1) {
  60.     printf("%s",arg);
  61.     c = getchar();
  62.     if (c == '\n') continue;
  63.     while ((x = getchar()) != '\n')
  64.       if (x == EOF) return(0);
  65.     if ((c == 'Y') || (c == 'y')) return(1);
  66.     if ((c == 'N') || (c == 'n') || (c == EOF)) return(0);
  67.     printf("Please answer 'Y' or 'N'\n");
  68.   }
  69. }
  70.  
  71. main(argc,argv) char **argv; {
  72.   char *infile = "";                    /* input file name, with default */
  73.   char outfile[100];                    /* output file name */
  74.   FILE *ifp, *ofp;                      /* i/o files */
  75.   char inline[100],outline[200];
  76.   int f;
  77.  
  78.   if (argc > 2) {                       /* check for too many args */
  79.     printf("Too many args. Usage: msbpct input-boo-file-name\n");
  80.     exit(1);
  81.   }
  82.   if (argc > 1) {                       /* check for input file */
  83.     infile = argv[1];
  84.   } else if (argc < 2) {
  85.     printf("Usage: msbpct input-boo-file-name\n");
  86.     exit(1);
  87.   }
  88.   if ((ifp = fopen(infile,"r")) == NULL) { /* open input file */
  89.     printf("%s not found.\n",infile);   /* failure? */
  90.     exit(1);
  91.   }
  92.  
  93.   fgets(outfile,100,ifp);               /* get output file name */
  94.   if ((outfile[strlen(outfile)-2] == '\r')|    /* MK001 */
  95.       (outfile[strlen(outfile)-2] == '\n')) {
  96.     outfile[strlen(outfile)-2] = '\0';
  97.   }
  98.   else {
  99.     outfile[strlen(outfile)-1] = '\0';
  100.   }
  101.  
  102.   if ((ofp = fopen(outfile,"r")) != NULL) {
  103.     char msg[100];
  104.     sprintf(msg,"output file '%s' already exists.  continue (y/n)? ",outfile);
  105.     if (!yes_or_no_p(msg)) {
  106.       printf("ok.  bye\n");
  107.       exit(0);
  108.     }
  109.     else {
  110.       fclose(ofp);
  111.     }
  112.   }
  113.  
  114. #ifndef MSDOS
  115. #ifndef O_BINARY
  116. #define O_BINARY 0
  117. #endif
  118. #endif
  119.  
  120. #ifdef AMIGA
  121.   if ((ofp = fopen(outfile,"w")) == NULL) {
  122.     printf("could not open %s\n",outfile); /* failure */
  123.     exit(0);
  124.   }
  125. #else
  126. #ifdef CI86
  127.    if((ofp = fopen(outfile,"wb")) == NULL){
  128.     printf("could not open %s\n",outfile); /* failure */
  129.     exit(0);
  130.    }
  131. #else
  132.   f = open(outfile,O_CREAT|O_WRONLY|O_TRUNC|O_BINARY,0x1ff);
  133.   if ((ofp = fdopen(f,"w")) == NULL) { /* open it */
  134.     printf("could not open %s\n",outfile); /* failure? */
  135.     exit(1);
  136.   }
  137. #endif
  138. #endif
  139.   printf("%s ==> %s\n",infile,outfile); /* announce our intentions */
  140.  
  141.   while(fgets(inline,100,ifp) != NULL) { /* till EOF */
  142.     int index=0,outindex=0;
  143.     while (index < strlen(inline) && inline[index] != '\n' &&
  144.            inline[index] != '\r')       /* end of line? */
  145.       if (fixchr(inline[index]) == NULLCHR) {   /* null compress char... */
  146.         int rptcnt;
  147.         int i;
  148.  
  149.         index++;
  150.         rptcnt = fixchr(inline[index]); /* get repeat count */
  151.         for (i=0; i<rptcnt; i++)        /* output the nulls */
  152.           putc('\0',ofp);
  153.         index++;                        /* pass the count field */
  154.       }
  155.       else {                            /* a quad to decode... */
  156.         int a, b, c ,d;
  157.  
  158.         a = fixchr(inline[index]);
  159.         index++;
  160.         b = fixchr(inline[index]);
  161.         index++;
  162.         c = fixchr(inline[index]);
  163.         index++;
  164.         d = fixchr(inline[index]);
  165.         index++;
  166.  
  167.                                         /* output the bytes */
  168.         putc(((a*4)+(b/16)) & 255,ofp);
  169.         putc(((b*16)+(c/4)) & 255,ofp);
  170.         putc(((c*64)+d) & 255,ofp);
  171.       }
  172.     }
  173. #ifdef MSDOS
  174.     putc('\032',ofp);                   /* final ^Z */
  175. #endif
  176.  
  177.     fclose(ofp);            /* Close the files */
  178.     fclose(ifp);
  179.     exit(0);
  180. }                                       /* End of program */
  181.