home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / atarist / astuue.c < prev   
C/C++ Source or Header  |  2020-01-01  |  3KB  |  117 lines

  1. /*
  2.  *
  3.  * Uuencode -- encode a file so that it's printable ascii, short lines
  4.  *
  5.  * Slightly modified from a version posted to net.sources a while back,
  6.  * and suitable for compilation on the IBM PC
  7.  *
  8.  * modified for Lattice C on the ST - 11.05.85 by MSD
  9.  *
  10.  */
  11.  
  12.  
  13. #include <stdio.h>
  14. #include <osbind.h>
  15.  
  16. char *progname = "UUENCODE";
  17.  
  18. #define USAGE "Usage: UUENCODE file [>outfile]\n"
  19.  
  20. /* ENC is the basic 1 character encoding function to make a char printing */
  21. #define ENC(c) (((c) & 077) + ' ')
  22.  
  23. FILE *in, *out, *efopen(), *fopen();
  24.  
  25. main(argc, argv)
  26.         int argc; char *argv[];
  27.         {
  28.  
  29.         if (argc < 2) {
  30.                 fprintf(stderr, USAGE);
  31.                 exit(2);
  32.                 }
  33.         in = efopen(argv[1], "rb");   /* binary input !!! */
  34.         if (argc == 3) {
  35.            out = efopen(argv[2], "w");
  36.         }
  37.         else out = stdout;
  38.         fprintf(out, "begin %o %s\n", 0777, argv[1]);
  39.         encode(in, out);
  40.         fprintf(out, "end\n");
  41.         }
  42.  
  43. /*
  44.  * copy from in to out, encoding as you go along.
  45.  */
  46.  
  47. encode(in, out)
  48.         FILE *in, *out;
  49.         {
  50.         char buf[80];
  51.         int i, n;
  52.         for (;;) {
  53.                 n = fr(in, buf, 45);
  54.                 putc(ENC(n), out);
  55.                 for (i=0; i<n; i += 3)
  56.                       outdec(&buf[i], out);
  57.                 putc('\n', out);
  58.                 if (n <= 0)
  59.                         break;
  60.                 }
  61.         }
  62.  
  63. /*
  64.  * output one group of 3 bytes, pointed at by p, on file f.
  65.  */
  66.  
  67. outdec(p, f)
  68.         char *p; FILE *f;
  69.         {
  70.         int c1, c2, c3, c4;
  71.         c1 = *p >> 2;
  72.         c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
  73.         c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
  74.         c4 = p[2] & 077;
  75.         putc(ENC(c1), f);
  76.         putc(ENC(c2), f);
  77.         putc(ENC(c3), f);
  78.         putc(ENC(c4), f);
  79.         }
  80.  
  81. /* fr: like read but stdio */
  82.  
  83. int fr(fd, buf, cnt)
  84.         FILE *fd; char *buf; int cnt;
  85.         {
  86.         int c, i;
  87.         for (i = 0; i < cnt; i++) {
  88.                 c = getc(fd);
  89.                 if (c == EOF)
  90.                         return(i);
  91.                 buf[i] = c;
  92.                 }
  93.         return (cnt);
  94.         }
  95.  
  96.  
  97. FILE *
  98. efopen(fn, mode)
  99.         char *fn, *mode;
  100.         {
  101.         FILE *unit;
  102.         if ((unit = fopen(fn, mode)) == NULL)
  103.                 error("Cannot open file %s", fn);
  104.         else
  105.                 return unit;
  106.         }
  107.  
  108. extern char *progname;
  109.  
  110. error(s1, s2)
  111.         char *s1, *s2;
  112.         {
  113.         fprintf(stderr, "%s: ", progname);
  114.         fprintf(stderr, s1, s2);
  115.         exit(1);
  116.         }
  117.