home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d0xx / d038 / uudecode.lha / UUdecode / uuencode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-10-20  |  1.8 KB  |  128 lines

  1. /*
  2.  * uuencode [input] output
  3.  *
  4.  * Encode a file so it can be mailed to a remote system.
  5.  */
  6. #include <stdio.h>
  7.  
  8. #ifdef unix
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #endif
  12.  
  13. /* ENC is the basic 1 character encoding function to make a char printing */
  14. #define ENC(c) (((c) & 077) + ' ')
  15.  
  16. main(argc, argv)
  17. char **argv;
  18. {
  19.     FILE *in;
  20. #ifdef unix
  21.     struct stat sbuf;
  22. #endif
  23.     int mode;
  24.  
  25.     /* optional 1st argument */
  26.     if (argc > 2) {
  27.         if ((in = fopen(argv[1], "r")) == NULL) {
  28.             perror(argv[1]);
  29.             exit(1);
  30.         }
  31.         argv++; argc--;
  32.     } else
  33.         in = stdin;
  34.  
  35.     if (argc != 2) {
  36.         printf("Usage: uuencode [infile] remotefile\n");
  37.         exit(2);
  38.     }
  39.  
  40. #ifdef unix
  41.     /* figure out the input file mode */
  42.     fstat(fileno(in), &sbuf);
  43.     mode = sbuf.st_mode & 0777;
  44. #else
  45.     mode = 0777;
  46. #endif
  47.     printf("begin %o %s\n", mode, argv[1]);
  48.  
  49.     encode(in, stdout);
  50.  
  51.     printf("end\n");
  52.     exit(0);
  53. }
  54.  
  55. /*
  56.  * copy from in to out, encoding as you go along.
  57.  */
  58. encode(in, out)
  59. FILE *in;
  60. FILE *out;
  61. {
  62.     char buf[80];
  63.     int i, n;
  64.  
  65.     for (;;) {
  66.         /* 1 (up to) 45 character line */
  67.         n = fr(in, buf, 45);
  68.         putc(ENC(n), out);
  69.  
  70.         for (i=0; i<n; i += 3)
  71.             outdec(&buf[i], out);
  72.  
  73.         putc('\n', out);
  74.         if (n <= 0)
  75.             break;
  76.     }
  77. }
  78.  
  79. /*
  80.  * output one group of 3 bytes, pointed at by p, on file f.
  81.  */
  82. outdec(p, f)
  83. char *p;
  84. FILE *f;
  85. {
  86.     int c1, c2, c3, c4;
  87.  
  88.     c1 = *p >> 2;
  89.     c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
  90.     c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
  91.     c4 = p[2] & 077;
  92.     putc(ENC(c1), f);
  93.     putc(ENC(c2), f);
  94.     putc(ENC(c3), f);
  95.     putc(ENC(c4), f);
  96. }
  97.  
  98. /* fr: like read but stdio */
  99. int
  100. fr(fd, buf, cnt)
  101. FILE *fd;
  102. char *buf;
  103. int cnt;
  104. {
  105.     int c, i;
  106.  
  107.     for (i=0; i<cnt; i++) {
  108.         c = getc(fd);
  109.         if (c == EOF)
  110.             return(i);
  111.         buf[i] = c;
  112.     }
  113.     return (cnt);
  114. }
  115.  
  116. #ifndef unix
  117.  
  118. perror (sp)
  119. char *sp;
  120. {
  121.     if (sp && *sp) {
  122.         fprintf (stderr, "%s: ");
  123.     }
  124.     fprintf (stderr, "<unknown error>\n");
  125. }
  126.  
  127. #endif    /* Unix */
  128.