home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d0xx / d026 / c-kermit.lha / C-kermit / other / ckiboo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-06-16  |  4.3 KB  |  154 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.  */
  23.  
  24. #include <stdio.h>
  25. #ifdef AMIGA
  26. #include <fcntl.h>
  27. #else
  28. #endif
  29. #ifdef MSDOS
  30. #include <fcntl.h>
  31. #else
  32. #ifdef vax11c
  33. #include <file.h>
  34. #else
  35. #include <sys/file.h>
  36. #endif
  37. #endif
  38.  
  39. #define fixchr(x) ((x) -'0')
  40. #define NULLCHR fixchr('~')
  41.  
  42. yes_or_no_p(arg) char *arg; {
  43.   int c,x;
  44.   while (1) {
  45.     printf("%s",arg);
  46.     c = getchar();
  47.     if (c == '\n') continue;
  48.     while ((x = getchar()) != '\n')
  49.       if (x == EOF) return(0);
  50.     if ((c == 'Y') || (c == 'y')) return(1);
  51.     if ((c == 'N') || (c == 'n') || (c == EOF)) return(0);
  52.     printf("Please answer 'Y' or 'N'\n");
  53.   }
  54. }
  55.  
  56. main(argc,argv) char **argv; {
  57. #ifdef AMIGA
  58.   char *infile = "CKIKER.BOO";          /* input file name, with default */
  59. #else
  60.   char *infile = "MSKERMIT.BOO";        /* input file name, with default */
  61. #endif
  62.   char outfile[100];                    /* output file name */
  63.   FILE *ifp, *ofp;                      /* i/o files */
  64.   char inline[100],outline[200];
  65.   int f;
  66.  
  67.   if (argc > 2) {                       /* check for too many args */
  68.     printf("too many args\n");
  69.     exit(1);
  70.   }
  71.   if (argc > 1) {                       /* check for input file */
  72.     infile = argv[1];
  73.   }
  74.   if ((ifp = fopen(infile,"r")) == NULL) { /* open input file */
  75.     printf("%s not found.\n",infile);   /* failure? */
  76.     exit(1);
  77.   }
  78.  
  79.   fgets(outfile,100,ifp);               /* get output file name */
  80.   if ((outfile[strlen(outfile)-2] == '\r')|    /* MK001 */
  81.       (outfile[strlen(outfile)-2] == '\n')) {
  82.     outfile[strlen(outfile)-2] = '\0';
  83.   }
  84.   else {
  85.     outfile[strlen(outfile)-1] = '\0';
  86.   }
  87.  
  88.   if ((ofp = fopen(outfile,"r")) != NULL) {
  89.     char msg[100];
  90.     sprintf(msg,"output file '%s' already exists.  continue (y/n)? ",outfile);
  91.     if (!yes_or_no_p(msg)) {
  92.       printf("ok.  bye\n");
  93.       exit(0);
  94.     }
  95.     else {
  96.       fclose(ofp);
  97.     }
  98.   }
  99.  
  100. #ifndef MSDOS
  101. #ifndef O_BINARY
  102. #define O_BINARY 0
  103. #endif
  104. #endif
  105.  
  106. #ifdef AMIGA
  107.   if ((ofp = fopen(outfile,"w")) == NULL) {
  108.     printf("could not open %s\n",outfile); /* failure */
  109.     exit(0);
  110.   }
  111. #else
  112.   f = open(outfile,O_CREAT|O_WRONLY|O_TRUNC|O_BINARY,0x1ff);
  113.   if ((ofp = fdopen(f,"w")) == NULL) { /* open it */
  114.     printf("could not open %s\n",outfile); /* failure? */
  115.     exit(1);
  116.   }
  117. #endif
  118.   printf("%s ==> %s\n",infile,outfile); /* announce our intentions */
  119.  
  120.   while(fgets(inline,100,ifp) != NULL) { /* till EOF */
  121.     int index=0,outindex=0;
  122.     while (index < strlen(inline) && inline[index] != '\n' &&
  123.            inline[index] != '\r')       /* end of line? */
  124.       if (fixchr(inline[index]) == NULLCHR) {   /* null compress char... */
  125.         int rptcnt;
  126.         int i;
  127.  
  128.         index++;
  129.         rptcnt = fixchr(inline[index]); /* get repeat count */
  130.         for (i=0; i<rptcnt; i++)        /* output the nulls */
  131.           putc('\0',ofp);
  132.         index++;                        /* pass the count field */
  133.       }
  134.       else {                            /* a quad to decode... */
  135.         int a, b, c ,d;
  136.  
  137.         a = fixchr(inline[index]);
  138.         index++;
  139.         b = fixchr(inline[index]);
  140.         index++;
  141.         c = fixchr(inline[index]);
  142.         index++;
  143.         d = fixchr(inline[index]);
  144.         index++;
  145.  
  146.                                         /* output the bytes */
  147.         putc(((a*4)+(b/16)) & 255,ofp);
  148.         putc(((b*16)+(c/4)) & 255,ofp);
  149.         putc(((c*64)+d) & 255,ofp);
  150.       }
  151.     }
  152.     putc('\032',ofp);                   /* final ^Z */
  153. }
  154.