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

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