home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / UUENCODE.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  150 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /* uuencode.c */
  4.  
  5. /*
  6. uudecode and uuencode are easily implemented under MSDOS as well.  Here
  7. are the sources for Microsoft C v3.0, but if you have another kind of C
  8. compiler, there should be perhaps only 1 change -- the output file of
  9. uudecode and the input file of uuencode must be in binary format.
  10. (ie.  binary files, like .EXE files may have byte patterns that are the
  11. same as ^Z, which signals end-of-file in non-binary (text) mode).
  12.  
  13.     Don Kneller
  14. UUCP:   ...ucbvax!ucsfcgl!kneller
  15. ARPA:   kneller@ucsf-cgl.ARPA
  16. BITNET: kneller@ucsfcgl.BITNET
  17.  
  18.    patched up for BC++ 3.1 by Alan Eldridge 10/12/92
  19.        (UUCP: alane@wozzle.linet.org, FIDO: 1:272/38.473)
  20.  
  21. */
  22.  
  23. #ifndef lint
  24.  #if !defined(MSDOS) && !defined(__MSDOS__)
  25.  static char sccsid[] = "@(#)uuencode.c  5.1 (Berkeley) 7/2/83";
  26.  #endif
  27. #endif
  28.  
  29. #if defined(MSDOS) || defined(__MSDOS__)
  30.  #define READ_BINARY "rb"
  31. #else
  32.  #define READ_BINARY "r"
  33. #endif
  34.  
  35. /*
  36.  * uuencode [input] output
  37.  *
  38.  * Encode a file so it can be mailed to a remote system.
  39.  */
  40.  
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <sys/types.h>
  44. #include <sys/stat.h>
  45.  
  46. /* ENC is the basic 1 character encoding function to make a char printing */
  47.  
  48. #define ENC(c) (((c) & 077) + ' ')
  49.  
  50. void encode(FILE *in, FILE *out);
  51. void outdec(char *p, FILE *f);
  52. int fr(FILE *fd, char *buf, int cnt);
  53.  
  54. main(int argc, char *argv[])
  55. {
  56.       FILE *in;
  57.       struct stat sbuf;
  58.       int mode;
  59.  
  60.       /* optional 1st argument */
  61.  
  62.       if (argc > 2)
  63.       {
  64.             /* Use binary mode */
  65.             if ((in = fopen(argv[1], READ_BINARY)) == NULL)
  66.             {
  67.                   perror(argv[1]);
  68.                   exit(1);
  69.             }
  70.             argv++; argc--;
  71.       }
  72.       else  in = stdin;
  73.  
  74.       if (argc != 2)
  75.       {
  76.             printf("Usage: uuencode [infile] remotefile\n");
  77.             exit(2);
  78.       }
  79.  
  80.       /* figure out the input file mode */
  81.  
  82.       fstat(fileno(in), &sbuf);
  83.       mode = sbuf.st_mode & 0777;
  84.       printf("begin %o %s\n", mode, argv[1]);
  85.  
  86.       encode(in, stdout);
  87.  
  88.       printf("end\n");
  89.       return 0;
  90. }
  91.  
  92. /*
  93.  * copy from in to out, encoding as you go along.
  94.  */
  95.  
  96. void encode(FILE *in, FILE *out)
  97. {
  98.       char buf[80];
  99.       int i, n;
  100.  
  101.       for (;;)
  102.       {
  103.             /* 1 (up to) 45 character line */
  104.  
  105.             n = fr(in, buf, 45);
  106.             putc(ENC(n), out);
  107.  
  108.             for (i = 0; i < n; i += 3)
  109.                   outdec(&buf[i], out);
  110.  
  111.             putc('\n', out);
  112.             if (n <= 0)
  113.                   break;
  114.       }
  115. }
  116.  
  117. /*
  118.  * output one group of 3 bytes, pointed at by p, on file f.
  119.  */
  120.  
  121. void outdec(char *p, FILE *f)
  122. {
  123.       int c1, c2, c3, c4;
  124.  
  125.       c1 = *p >> 2;
  126.       c2 = ((p[0] << 4) & 060) | ((p[1] >> 4) & 017);
  127.       c3 = ((p[1] << 2) & 074) | ((p[2] >> 6) & 03);
  128.       c4 = p[2] & 077;
  129.       putc(ENC(c1), f);
  130.       putc(ENC(c2), f);
  131.       putc(ENC(c3), f);
  132.       putc(ENC(c4), f);
  133. }
  134.  
  135. /* fr: like read but stdio */
  136.  
  137. int fr(FILE *fd, char *buf, int cnt)
  138. {
  139.       int c, i;
  140.  
  141.       for (i = 0; i < cnt; i++)
  142.       {
  143.             c = getc(fd);
  144.             if (c == EOF)
  145.                   return(i);
  146.             buf[i] = (char)c;
  147.       }
  148.       return (cnt);
  149. }
  150.