home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / auucp+-1.02 / fuucp_plus_src.lzh / unix / uuencode.c < prev   
Encoding:
C/C++ Source or Header  |  1990-11-21  |  2.1 KB  |  148 lines

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