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

  1. /* The boo and de-boo programs do not work, and I found them too cumbersome
  2.  * and complicated to install on the DG.  In their place, I have provided
  3.  * a few other tools:
  4.  *
  5.  *      uuencode and uudecode:  
  6.  *          Convert any binary file to a non-binary file, and preserve the
  7.  *          DG record and file types, if done on DG systems.
  8.  *      sq and usq:
  9.  *          CP/M (and others) squeeze and unsqueeze programs.  Should be
  10.  *          compatible with Unix and other hosts.
  11.  *      compress:
  12.  *          a more efficient compression than sq/usq.  Also compatible with
  13.  *          many hosts.
  14.  *
  15.  * If anyone can get these other files working, more power to you.  The
  16.  * uudecode program is slow, but the squeeze programs are very fast.  The
  17.  * uudecode could be faster, but I did not want to wasted alot of time on
  18.  * it.  Anyway, the uudecode source is very simple, and should be easy to
  19.  * convert to another language.  If you do convert the uudecode program,
  20.  * please let me know, so that it can be distributed to hosts that do not
  21.  * have the C compiler.
  22.  *
  23.  *   Phil Julian, SAS Institute, Inc., Box 8000, Cary, NC 27512-8000
  24.  *
  25.  */
  26.  
  27. /*
  28.  * MSBPCT.C
  29.  *
  30.  * Howie Kaye -- Columbia University 3/11/86
  31.  *
  32.  * sibling program to MSBMKB.C.  It is used to unpack BOO files, used for
  33.  * encoding binary files into text, and back.  This program does the decoding.
  34.  * It is meant to replace the program "MSBPCT.BAS", and runs approximately
  35.  * 200 times faster.
  36.  *
  37.  * For documentation on BOO file format, see MSBMKB.C
  38.  * This program runs, as is, under Microsoft C on MSDOS, and under UNIX(4.2).
  39.  *
  40.  * Modification history:
  41.  *
  42.  * 3/23/86 - Davide P. Cervone -- University of Rochester
  43.  *   added AMIGA and VAX11C support
  44.  *
  45.  * 3/24/86 - Martin Knoblauch -- TH-Darmstadt              (MK001)
  46.  *   test if 1. line of inputfile is delimited by "\r\n" instead of "\n"
  47.  *
  48.  * 5/5/86 - John Matthews, U of Delaware.
  49.  *   Explicitly close the files.
  50.  *
  51.  * 5/8/86 - L. John Junod, DTNSRDC.
  52.  *   Adapt for Computer Innovations MS-DOS CI-86 Compiler
  53.  *   Improve too many args error message
  54.  */
  55.  
  56. /***
  57. #define MSDOS
  58. #define CI86
  59. ***/
  60.  
  61. #include <stdio.h>
  62.  
  63. #ifdef AMIGA
  64. #include <fcntl.h>
  65. #else
  66. #endif
  67.  
  68. #ifdef MSDOS 
  69. #ifndef CI86
  70. #include <fcntl.h>
  71. #endif
  72. #else
  73. #ifdef vax11c
  74. #include <file.h>
  75. #else
  76. #ifdef datageneral
  77. #include <fcntl.h>
  78. #include <sys_calls.h>
  79. #include <packets/filestatus.h>         /* Used for ?GNFN */
  80. #include <packets:normal_io.h>
  81. #include <paru.h>
  82. struct p_nio_ex w_io_parms;             /* ?write system call structure */
  83. P_FSTAT buf; 
  84. int ac0,ac2;
  85. char name[256];
  86. #define R_ACC 4
  87. #ifdef putc
  88. #undef putc
  89. #endif
  90. #define putc(c,file) { char ch = (char) (c); dg_binw(fchannel(file),&ch,1); }
  91. #define write(filen,chs,len) dg_binw(filen,chs,len)
  92. #else
  93. #include <sys/file.h>
  94. #endif
  95. #endif
  96. #endif
  97.  
  98. #define fixchr(x) ((x) -'0')
  99. #define NULLCHR fixchr('#')
  100.  
  101. yes_or_no_p(arg) char *arg; {
  102.   int c,x;
  103.   while (1) {
  104.     printf("%s",arg);
  105.     c = getchar();
  106.     if (c == '\n') continue;
  107.     while ((x = getchar()) != '\n')
  108.       if (x == EOF) return(0);
  109.     if ((c == 'Y') || (c == 'y')) return(1);
  110.     if ((c == 'N') || (c == 'n') || (c == EOF)) return(0);
  111.     printf("Please answer 'Y' or 'N'\n");
  112.   }
  113. }
  114.  
  115. main(argc,argv) char **argv; {
  116. #ifdef AMIGA
  117.   char *infile = "CKIKER.BOO";          /* input file name, with default */
  118. #else
  119. #ifdef datageneral
  120.   char *infile = "CKDKER.BOO";          /* input file name, with default */
  121. #else
  122.   char *infile = "MSKERMIT.BOO";        /* input file name, with default */
  123. #endif
  124. #endif
  125.   char outfile[100];                    /* output file name */
  126.   FILE *ifp, *ofp;                      /* i/o files */
  127.   char inline[100],outline[200];
  128.   int f;
  129.  
  130.   if (argc > 2) {                       /* check for too many args */
  131.     printf("Too many args. Usage: msbpct [inputfile]\n");
  132.     exit(1);
  133.   }
  134.   if (argc > 1) {                       /* check for input file */
  135.     infile = argv[1];
  136.   }
  137. #ifdef datageneral
  138.   /* Initialize the i/o block for putc() */
  139.   zero((char *) &w_io_parms, sizeof(w_io_parms));
  140.   w_io_parms.isti = $IBIN|$RTDY|$ICRF|$OFOT;
  141.   w_io_parms.isti &= ~$IPST;
  142.   w_io_parms.imrs = 2048;
  143.   w_io_parms.ibad = -1;
  144.   w_io_parms.ircl = -1;
  145.  
  146.   if ((ifp = dg_open(infile,$OFIN|$RTDS|$ICRF,$FUDF)) == NULL) {
  147. #else
  148.   if ((ifp = fopen(infile,"r")) == NULL) { /* open input file */
  149. #endif
  150.     printf("%s not found.\n",infile);   /* failure? */
  151.     exit(1);
  152.   }
  153.  
  154.   fgets(outfile,100,ifp);               /* get output file name */
  155.   if ((outfile[strlen(outfile)-2] == '\r')|    /* MK001 */
  156.       (outfile[strlen(outfile)-2] == '\n')) {
  157.     outfile[strlen(outfile)-2] = '\0';
  158.   }
  159.   else {
  160.     outfile[strlen(outfile)-1] = '\0';
  161.   }
  162.  
  163.   if ((ofp = fopen(outfile,"r")) != NULL) {
  164.     char msg[100];
  165.     sprintf(msg,"output file '%s' already exists.  continue (y/n)? ",outfile);
  166.     if (!yes_or_no_p(msg)) {
  167.       printf("ok.  bye\n");
  168.       exit(0);
  169.     }
  170.     else {
  171.       fclose(ofp);
  172.     }
  173.   }
  174.  
  175. #ifndef MSDOS
  176. #ifndef O_BINARY
  177. #define O_BINARY 0
  178. #endif
  179. #endif
  180.  
  181. #ifdef AMIGA
  182.   if ((ofp = fopen(outfile,"w")) == NULL) {
  183.     printf("could not open %s\n",outfile); /* failure */
  184.     exit(0);
  185.   }
  186. #else
  187. #ifdef CI86
  188.    if((ofp = fopen(outfile,"wb")) == NULL){
  189.     printf("could not open %s\n",outfile); /* failure */
  190.     exit(0);
  191.    }
  192. #else
  193. #ifdef datageneral
  194.    if ((ofp = dg_open(outfile,$OFOT|$IBIN|$OFCR|$OFCE|$RTUN,$FUNX)) == NULL) {
  195.    /* if((ofp = fopen(outfile,"k")) == NULL){ */
  196.     perror("Open outfile");
  197.     printf("Could not open %s\n",outfile); /* failure */
  198.     exit(0);
  199.    }
  200. #else
  201.   f = open(outfile,O_CREAT|O_WRONLY|O_TRUNC|O_BINARY,0x1ff);
  202.   if ((ofp = fdopen(f,"w")) == NULL) { /* open it */
  203.     printf("could not open %s\n",outfile); /* failure? */
  204.     exit(1);
  205.   }
  206. #endif
  207. #endif
  208. #endif
  209.   printf("%s ==> %s\n",infile,outfile); /* announce our intentions */
  210.  
  211.   while(fgets(inline,100,ifp) != NULL) { /* till EOF */
  212.     int index=0,outindex=0;
  213.     while (index < strlen(inline) && inline[index] != '\n' &&
  214.            inline[index] != '\r')       /* end of line? */
  215.       if (fixchr(inline[index]) == NULLCHR) {   /* null compress char... */
  216.         int rptcnt;
  217.         int i;
  218.  
  219.         index++;
  220.         rptcnt = fixchr(inline[index]); /* get repeat count */
  221.         for (i=0; i<rptcnt; i++)        /* output the nulls */
  222.           putc('\0',ofp);
  223.         index++;                        /* pass the count field */
  224.       }
  225.       else {                            /* a quad to decode... */
  226.         int a, b, c ,d;
  227.  
  228.         a = fixchr(inline[index]);
  229.         index++;
  230.         b = fixchr(inline[index]);
  231.         index++;
  232.         c = fixchr(inline[index]);
  233.         index++;
  234.         d = fixchr(inline[index]);
  235.         index++;
  236.  
  237.                                         /* output the bytes */
  238.         putc(((a*4)+(b/16)) & 255,ofp);
  239.         putc(((b*16)+(c/4)) & 255,ofp);
  240.         putc(((c*64)+d) & 255,ofp);
  241.       }
  242.     }
  243. #ifdef AMIGA
  244.     putc('\032',ofp);                   /* final ^Z for micros*/
  245. #endif
  246. #ifdef MSDOS
  247.     putc('\032',ofp);                   /* final ^Z */
  248. #endif
  249.  
  250.     fclose(ofp);            /* Close the files */
  251.     fclose(ifp);
  252. }                    /* End of program */
  253.  
  254.  
  255. #ifdef datageneral
  256. /* D G _ B I N W -- Output len characters to the file number filenum 
  257.  * 
  258.  *  The syntax is like the Unix write command.
  259.  *  This code was borrowed from my Kermit source -- ckdtio.c
  260.  */
  261.  
  262. dg_binw(channel,chs,len) int channel, len; char *chs; 
  263. {
  264.      int ac2,err;
  265.  
  266.      if (len == 0) return(0);
  267.  
  268.      w_io_parms.ich = channel;
  269.      w_io_parms.ibad = chs;
  270.      w_io_parms.ircl = len;
  271.      ac2 = &w_io_parms;
  272.      
  273.      if ((err = sys_write(ac2)) == 0) return(0);
  274.  
  275.      if ( err != ERLTL && err != EREOF ) {
  276.           perror("dg_binw: sys_write ");
  277.           exit(err);
  278.      }
  279. }
  280. #endif
  281.  
  282.  
  283.  
  284.