home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / c-kermit / ckiboo.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  4KB  |  164 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
  12.  * This program runs, as is, under Microsoft C on MSDOS, and under UNIX(4.2).
  13.  *
  14.  * Modification history:
  15.  *
  16.  * 3/23/86 - Davide P. Cervone -- University of Rochester
  17.  *   added AMIGA and VAX11C support
  18.  *
  19.  * 3/24/86 - Martin Knoblauch -- TH-Darmstadt              (MK001)
  20.  *   test if 1. line of inputfile is delimited by "\r\n" instead of "\n"
  21.  *
  22.  * 5/5/86 - John Matthews, U of Delaware.
  23.  *   Explicitly close the files.
  24.  */
  25.  
  26. #include <stdio.h>
  27. #ifdef AMIGA
  28.    #include <fcntl.h>
  29. #else
  30.    #ifdef MSDOS
  31.       #include <fcntl.h>
  32.    #else
  33.       #ifdef vax11c
  34.          #include <file.h>
  35.       #else
  36.          #include <sys/file.h>
  37.       #endif
  38.    #endif
  39. #endif
  40.  
  41. #define fixchr(x) ((x) -'0')
  42. #define NULLCHR fixchr('~')
  43.  
  44. yes_or_no_p(arg) char *arg; {
  45.   int c,x;
  46.   while (1) {
  47.     printf("%s",arg);
  48.     c = getchar();
  49.     if (c == '\n') continue;
  50.     while ((x = getchar()) != '\n')
  51.       if (x == EOF) return(0);
  52.     if ((c == 'Y') || (c == 'y')) return(1);
  53.     if ((c == 'N') || (c == 'n') || (c == EOF)) return(0);
  54.     printf("Please answer 'Y' or 'N'\n");
  55.   }
  56. }
  57.  
  58. main(argc,argv) char **argv; {
  59. #ifdef AMIGA
  60.   char *infile = "CKIKER.BOO";          /* input file name, with default */
  61. #else
  62.   char *infile = "MSKERMIT.BOO";        /* input file name, with default */
  63. #endif
  64.   char outfile[100];                    /* output file name */
  65.   FILE *ifp, *ofp;                      /* i/o files */
  66.   char inline[100],outline[200];
  67.   int f;
  68.  
  69.   if (argc > 2) {                       /* check for too many args */
  70.     printf("too many args\n");
  71.     exit(1);
  72.   }
  73.   if (argc > 1) {                       /* check for input file */
  74.     infile = argv[1];
  75.   }
  76.   if ((ifp = fopen(infile,"r")) == NULL) { /* open input file */
  77.     printf("%s not found.\n",infile);   /* failure? */
  78.     exit(1);
  79.   }
  80.  
  81.   fgets(outfile,100,ifp);               /* get output file name */
  82.   if ((outfile[strlen(outfile)-2] == '\r')|    /* MK001 */
  83.       (outfile[strlen(outfile)-2] == '\n')) {
  84.     outfile[strlen(outfile)-2] = '\0';
  85.   }
  86.   else {
  87.     outfile[strlen(outfile)-1] = '\0';
  88.   }
  89.  
  90.   if ((ofp = fopen(outfile,"r")) != NULL) {
  91.     char msg[100];
  92.     sprintf(msg,"output file '%s' already exists.  continue (y/n)? ",outfile);
  93.     if (!yes_or_no_p(msg)) {
  94.       printf("ok.  bye\n");
  95.       exit(0);
  96.     }
  97.     else {
  98.       fclose(ofp);
  99.     }
  100.   }
  101.  
  102. #ifndef MSDOS
  103. #ifndef O_BINARY
  104. #define O_BINARY 0
  105. #endif
  106. #endif
  107.  
  108. #ifdef AMIGA
  109.   if ((ofp = fopen(outfile,"w")) == NULL) {
  110.     printf("could not open %s\n",outfile); /* failure */
  111.     exit(0);
  112.   }
  113. #else
  114.   f = open(outfile,O_CREAT|O_WRONLY|O_TRUNC|O_BINARY,0x1ff);
  115.   if ((ofp = fdopen(f,"w")) == NULL) { /* open it */
  116.     printf("could not open %s\n",outfile); /* failure? */
  117.     exit(1);
  118.   }
  119. #endif
  120.   printf("%s ==> %s\n",infile,outfile); /* announce our intentions */
  121.  
  122.   while(fgets(inline,100,ifp) != NULL) { /* till EOF */
  123.     int index=0,outindex=0;
  124.     while (index < strlen(inline) && inline[index] != '\n' &&
  125.            inline[index] != '\r')       /* end of line? */
  126.       if (fixchr(inline[index]) == NULLCHR) {   /* null compress char... */
  127.         int rptcnt;
  128.         int i;
  129.  
  130.         index++;
  131.         rptcnt = fixchr(inline[index]); /* get repeat count */
  132.         for (i=0; i<rptcnt; i++)        /* output the nulls */
  133.           putc('\0',ofp);
  134.         index++;                        /* pass the count field */
  135.       }
  136.       else {                            /* a quad to decode... */
  137.         int a, b, c ,d;
  138.  
  139.         a = fixchr(inline[index]);
  140.         index++;
  141.         b = fixchr(inline[index]);
  142.         index++;
  143.         c = fixchr(inline[index]);
  144.         index++;
  145.         d = fixchr(inline[index]);
  146.         index++;
  147.  
  148.                                         /* output the bytes */
  149.         putc(((a*4)+(b/16)) & 255,ofp);
  150.         putc(((b*16)+(c/4)) & 255,ofp);
  151.         putc(((c*64)+d) & 255,ofp);
  152.       }
  153.     }
  154. #ifdef AMIGA
  155.     putc('\032',ofp);                   /* final ^Z for micros*/
  156. #endif
  157. #ifdef MSDOS
  158.     putc('\032',ofp);                   /* final ^Z */
  159. #endif
  160.  
  161.     fclose(ofp);            /* Close the files */
  162.     fclose(ifp);
  163. }                    /* End of program */
  164.