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