home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / old / ckermit4e / ckopct.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  5KB  |  185 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. #endif
  42.  
  43. #ifdef MSDOS 
  44. #ifndef CI86
  45. #include <fcntl.h>
  46. #endif
  47. #else
  48. #ifdef vax11c
  49. #include <file.h>
  50. #else
  51. #include <sys/file.h>
  52. #endif
  53. #endif
  54.  
  55. #define fixchr(x) ((x) -'0')
  56. #define NULLCHR fixchr('~')
  57.  
  58. yes_or_no_p(arg) char *arg; {
  59.   int c,x;
  60.   while (1) {
  61.     printf("%s",arg);
  62.     c = getchar();
  63.     if (c == '\n') continue;
  64.     while ((x = getchar()) != '\n')
  65.       if (x == EOF) return(0);
  66.     if ((c == 'Y') || (c == 'y')) return(1);
  67.     if ((c == 'N') || (c == 'n') || (c == EOF)) return(0);
  68.     printf("Please answer 'Y' or 'N'\n");
  69.   }
  70. }
  71.  
  72. main(argc,argv) char **argv; {
  73.   char *infile = "";                    /* input file name, with default */
  74.   char outfile[100];                    /* output file name */
  75.   FILE *ifp, *ofp;                      /* i/o files */
  76.   char inline[100],outline[200];
  77.   int f;
  78.  
  79.   if (argc > 2) {                       /* check for too many args */
  80.     printf("Too many args. Usage: msbpct input-boo-file-name\n");
  81.     exit(1);
  82.   }
  83.   if (argc > 1) {                       /* check for input file */
  84.     infile = argv[1];
  85.   } else if (argc < 2) {
  86.     printf("Usage: msbpct input-boo-file-name\n");
  87.     exit(1);
  88.   }
  89.   if ((ifp = fopen(infile,"r")) == NULL) { /* open input file */
  90.     printf("%s not found.\n",infile);   /* failure? */
  91.     exit(1);
  92.   }
  93.  
  94.   fgets(outfile,100,ifp);               /* get output file name */
  95.   if ((outfile[strlen(outfile)-2] == '\r')|    /* MK001 */
  96.       (outfile[strlen(outfile)-2] == '\n')) {
  97.     outfile[strlen(outfile)-2] = '\0';
  98.   }
  99.   else {
  100.     outfile[strlen(outfile)-1] = '\0';
  101.   }
  102.  
  103.   if ((ofp = fopen(outfile,"r")) != NULL) {
  104.     char msg[100];
  105.     sprintf(msg,"output file '%s' already exists.  continue (y/n)? ",outfile);
  106.     if (!yes_or_no_p(msg)) {
  107.       printf("ok.  bye\n");
  108.       exit(0);
  109.     }
  110.     else {
  111.       fclose(ofp);
  112.     }
  113.   }
  114.  
  115. #ifndef MSDOS
  116. #ifndef O_BINARY
  117. #define O_BINARY 0
  118. #endif
  119. #endif
  120.  
  121. #ifdef AMIGA
  122.   if ((ofp = fopen(outfile,"w")) == NULL) {
  123.     printf("could not open %s\n",outfile); /* failure */
  124.     exit(0);
  125.   }
  126. #else
  127. #ifdef CI86
  128.    if((ofp = fopen(outfile,"wb")) == NULL){
  129.     printf("could not open %s\n",outfile); /* failure */
  130.     exit(0);
  131.    }
  132. #else
  133.   f = open(outfile,O_CREAT|O_WRONLY|O_TRUNC|O_BINARY,0x1ff);
  134.   if ((ofp = fdopen(f,"w")) == NULL) { /* open it */
  135.     printf("could not open %s\n",outfile); /* failure? */
  136.     exit(1);
  137.   }
  138. #endif
  139. #endif
  140.   printf("%s ==> %s\n",infile,outfile); /* announce our intentions */
  141.  
  142.   while(fgets(inline,100,ifp) != NULL) { /* till EOF */
  143.     int index=0,outindex=0;
  144.     while (index < strlen(inline) && inline[index] != '\n' &&
  145.            inline[index] != '\r')       /* end of line? */
  146.       if (fixchr(inline[index]) == NULLCHR) {   /* null compress char... */
  147.         int rptcnt;
  148.         int i;
  149.  
  150.         index++;
  151.         rptcnt = fixchr(inline[index]); /* get repeat count */
  152.         for (i=0; i<rptcnt; i++)        /* output the nulls */
  153.           putc('\0',ofp);
  154.         index++;                        /* pass the count field */
  155.       }
  156.       else {                            /* a quad to decode... */
  157.         int a, b, c ,d;
  158.  
  159.         a = fixchr(inline[index]);
  160.         index++;
  161.         b = fixchr(inline[index]);
  162.         index++;
  163.         c = fixchr(inline[index]);
  164.         index++;
  165.         d = fixchr(inline[index]);
  166.         index++;
  167.  
  168.                                         /* output the bytes */
  169.         putc(((a*4)+(b/16)) & 255,ofp);
  170.         putc(((b*16)+(c/4)) & 255,ofp);
  171.         putc(((c*64)+d) & 255,ofp);
  172.       }
  173.     }
  174. #ifdef AMIGA
  175.     putc('\032',ofp);                   /* final ^Z for micros*/
  176. #endif
  177. #ifdef MSDOS
  178.     putc('\032',ofp);                   /* final ^Z */
  179. #endif
  180.  
  181.     fclose(ofp);            /* Close the files */
  182.     fclose(ifp);
  183.     exit(0);
  184. }                                       /* End of program */
  185.