home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2729 / uuencode.c < prev   
Encoding:
C/C++ Source or Header  |  1991-02-10  |  1.9 KB  |  118 lines

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