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

  1. /*
  2.  * Uudecode -- decode a uuencoded file back to binary form.
  3.  *
  4.  * Slightly modified from a version posted to net.sources once;
  5.  * suitable for compilation on an IBM PC.
  6.  *
  7.  * modified for Lattice C on the ST - 11.05.86 by MSD
  8.  *
  9.  */
  10.  
  11.  
  12. #include <stdio.h>
  13. #include <osbind.h>
  14.  
  15. char *Progname = "UUDECODE";
  16.  
  17. #define USAGE "Usage: UUDECODE [file]\n"
  18.  
  19. /* single character decode */
  20. #define DEC(c)  (((c) - ' ') & 077)
  21.  
  22. FILE *in, *out, *efopen(), *fopen();
  23.  
  24. main(argc, argv)
  25.         int argc; char *argv[];
  26.         {
  27.  
  28.         int mode;
  29.         char dest[128];
  30.         char buf[80];
  31.         /* optional input arg */
  32.         if (argc > 1) {
  33.                 in = efopen(argv[1], "r");
  34.                 argv++; argc--;
  35.                 }
  36.         else
  37.                 in = stdin;
  38.         if (argc != 1) {
  39.                 fprintf(stderr, USAGE);
  40.                 exit(2);
  41.                 }
  42.         /* search for header line */
  43.         for (;;) {
  44.                 if (fgets(buf, sizeof buf, in) == NULL) {
  45.                         fprintf(stderr, "No begin line\n");
  46.                         exit(3);
  47.                         }
  48.                 if (strncmp(buf, "begin ", 6) == 0)
  49.                         break;
  50.                 }
  51.         sscanf(buf, "begin %o %s", &mode, dest);
  52.         out = efopen(dest, "wb");  /* create output file - binary output !!! */
  53.         decode(in, out);
  54.         fclose(out);
  55.         if (fgets(buf, sizeof buf, in) == NULL || strcmp(buf, "end\n")) {
  56.                 fprintf(stderr, "No end line\n");
  57.                 exit(5);
  58.                 }
  59.         }
  60.  
  61. /*
  62.  * copy from in to out, decoding as you go along.
  63.  */
  64.  
  65. decode(in, out)
  66.         FILE *in, *out;
  67.         {
  68.         char buf[80];
  69.         char *bp;
  70.         int n;
  71.         for (;;) {
  72.                 if (fgets(buf, sizeof buf, in) == NULL) {
  73.                         fprintf(stderr, "Short file\n");
  74.                         break;
  75.                         }
  76.                 n = DEC(buf[0]);
  77.                 if (n <= 0)
  78.                         break;
  79.                 bp = &buf[1];
  80.                 while (n > 0) {
  81.                         outdec(bp, out, n);
  82.                         bp += 4;
  83.                         n -= 3;
  84.                         }
  85.                 }
  86.         }
  87.  
  88. /*
  89.  * output a group of 3 bytes (4 input characters).
  90.  * the input chars are pointed to by p, they are to
  91.  * be output to file f.  n is used to tell us not to
  92.  * output all of them at the end of the file.
  93.  */
  94.  
  95. outdec(p, f, n)
  96.         char *p; FILE *f; int n;
  97.         {
  98.         int c1, c2, c3;
  99.         c1 = DEC(*p) << 2 | DEC(p[1]) >> 4;
  100.         c2 = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
  101.         c3 = DEC(p[2]) << 6 | DEC(p[3]);
  102.         if (n >= 1)
  103.                 putc(c1, f);
  104.         if (n >= 2)
  105.                 putc(c2, f);
  106.         if (n >= 3)
  107.                 putc(c3, f);
  108.         }
  109.  
  110.  
  111. /* fr: like read but stdio */
  112.  
  113. int fr(fd, buf, cnt)
  114.         FILE *fd; char *buf; int cnt;
  115.         {
  116.         int c, i;
  117.         for (i = 0; i < cnt; i++) {
  118.                 c = getc(fd);
  119.                 if (c == EOF)
  120.                         return(i);
  121.                 buf[i] = c;
  122.                 }
  123.         return (cnt);
  124.         }
  125.  
  126. /* If your library already has this function, use it and nuke the code below */
  127.  
  128.  
  129. /* Open a file, aborting on failure */
  130.  
  131. /* Written by Bernie Roehl, June 1985 */
  132.  
  133. FILE *
  134. efopen(fn, mode)
  135.         char *fn, *mode;
  136.         {
  137.         FILE *unit;
  138.         if ((unit = fopen(fn, mode)) == NULL)
  139.                 error("Cannot open file %s", fn);
  140.         else
  141.                 return unit;
  142.         }
  143.  
  144. extern char *Progname;
  145.  
  146. error(s1, s2)
  147.         char *s1, *s2;
  148.         {
  149.         fprintf(stderr, "%s: ", Progname);
  150.         fprintf(stderr, s1, s2);
  151.         exit(1);
  152.         }
  153.