home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / uucd_pls.zip / UUENCODE.C < prev    next >
C/C++ Source or Header  |  1996-01-08  |  2KB  |  91 lines

  1. /* uuencode [input] output uuencode-produced-file
  2.  * Encode a file so it can be mailed to a remote system.
  3.  */
  4. /*last modified by boris@innonyc.com
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. /* ENC is the basic 1-character encoding function to make a char printing */
  12. #define ENC(c) ((c) ? ((c) & 077) + ' ': '`')
  13. void outdec(char * p, FILE * f);
  14. void encode(FILE *in,FILE *out);
  15. void main(int argc, char ** argv)
  16. {
  17.     FILE *out, *in;
  18.     if ((argc != 2)&&(argc != 4)){
  19.         fprintf(stderr, "Usage: %s [infile] remotefile [uufile]\n",argv[0]);
  20.         exit(2);
  21.     }
  22.  
  23. if((!strcmp(argv[1],"-?"))||(!strcmp(argv[1],"-h"))
  24. ||(!strcmp(argv[1],"/?"))||(!strcmp(argv[1],"/h")) ){
  25. printf("Program performs uucode on a file.\nTo use type %s [infile] \
  26. remotefile [uufile]\nWhen activated without first and third arguments uses \
  27. standart input and output\n",argv[0]);
  28. exit(0);
  29. }
  30.  
  31.     /* optional 1st argument */
  32.     if (argc == 4) {
  33.         if ((in = fopen(argv[1], "rb")) == NULL) {
  34.             perror(argv[1]);
  35.             exit(1);
  36.         }
  37.         if ((out = fopen(argv[3], "w")) == NULL) {
  38.          perror(argv[3]);fclose (in);
  39.          exit(4);
  40.         }
  41.  
  42.     } else
  43.      {in = stdin;out=stdout;
  44.      }
  45. /* mandatory 3rd argument is name of uuencoded file */
  46.  
  47.     fprintf(out, "begin %o %s\n", 0755, argv[1]);
  48.  
  49.     encode(in, out);
  50.  
  51.     fprintf(out, "end\n");fclose (in);fclose (out);
  52.     exit(0);
  53. }
  54.  
  55. /*
  56.  * copy from in to out, encoding as you go along.
  57.  */
  58. void encode(FILE *in,FILE *out)
  59. {
  60.     char buf[80];
  61.     register int i, n;
  62.  
  63.     for (;;) {
  64.         /* 1 (up to) 45 character line */
  65.         n = fread(buf, 1, 45, in);
  66.         putc(ENC(n), out);
  67.  
  68.         for (i=0; i<n; i += 3)
  69.             outdec(&buf[i], out);
  70.  
  71.         putc('\n', out);
  72.         if (n <= 0)
  73.             break;
  74.     }
  75. }
  76.  
  77. /*
  78.  * output one group of 3 bytes, pointed at by p, on file f.
  79.  */
  80. void outdec(char * p, FILE * f)
  81. {
  82.     register int c1, c2, c3, c4;
  83.  
  84.     c1 = *p >> 2;
  85.     c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
  86.     c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
  87.     c4 = p[2] & 077;
  88.     putc(ENC(c1), f);putc(ENC(c2), f);
  89.     putc(ENC(c3), f);putc(ENC(c4), f);
  90. }
  91.