home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / duucp-1.17 / AU-117b4-src.lha / src / unix / uuencode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-24  |  2.2 KB  |  147 lines

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