home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / fish / telecom / uucp_442 / src / unix / new / uucodes.lzh / uuencode.c < prev   
C/C++ Source or Header  |  1990-12-29  |  3KB  |  165 lines

  1. /* #ifndef lint
  2. static char sccsid[] = "@(#)uuencode.c  5.3-1 (Berkeley) 9/16/87";
  3. #endif */
  4.  
  5. /* Written by Mark Horton */
  6. /* Modified by ajr (Alan J Rosenthatl,flaps@utcsri.UUCP) to use checksums */
  7. /* Modified by fnf (Fred Fish,well!fnf) to use Keith Pyle's suggestion for
  8.    compatibility */
  9. /* Modified by bcn (Bryce Nesbitt,ucbvax!hoser!bryce) to enable CTRL-C for
  10.    Amiga Lattice C.  Added a transparent file size trailer for later check.
  11.    Changed fopen from "r" to "rb" for Messy-dos machines (thanks to Andrew
  12.    Wylie) */
  13.  
  14. /*
  15.  * uuencode >outfile [infile] name
  16.  *
  17.  * Encode a file so it can be mailed to a remote system.  This version
  18.  * transparantly adds line checksums and a file size for sanity checks.
  19.  *
  20.  */
  21.  
  22. #include <stdio.h>
  23.  
  24. #ifdef    AMIGA            /* Amiga Lattice C */
  25. #define AMIGA_LATTICE
  26. #define MCH_AMIGA
  27. #define MPU68000
  28. #endif
  29.  
  30. #ifdef unix
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #endif
  34.  
  35. #define SUMSIZE 64  /* 6 bits */
  36. /* ENC is the basic 1 character encode function to make a char printing */
  37. /* Each output character represents 6 bits of input */
  38. #define ENC(c) ((c) ? ((c) & 077) + ' ': '`')
  39. long    totalsize=0;    /* Used to count the file size because ftell() does
  40.                not return sane results for pipes */
  41.  
  42. main(argc, argv)
  43. char **argv;
  44. {
  45.     FILE *in;
  46.     int mode;
  47. #ifdef unix
  48.     struct stat sbuf;
  49. #endif
  50. #ifdef AMIGA_LATTICE
  51.     extern int Enable_Abort;    /* Enable CTRL-C for Lattice */
  52.     Enable_Abort=1;
  53. #endif
  54.  
  55.     /* optional 1st argument */
  56.     if (argc > 2) {
  57.         if ((in = fopen(argv[1], "r")) == NULL) {
  58.             fprintf(stderr, "ERROR: can't find %s\n", argv[1]);
  59.             fprintf(stderr, "USAGE: uuencode >outfile [infile] name\n");
  60.             exit(10);
  61.         }
  62.         argv++; argc--;
  63.     } else
  64.         in = stdin;
  65.  
  66.     if (argc != 2) {
  67.         fprintf(stderr, "USAGE: uuencode >outfile [infile] name\n");
  68.         exit(11);
  69.     }
  70.  
  71. #ifdef unix
  72.     /* figure out the input file mode */
  73.     fstat(fileno(in), &sbuf);
  74.     mode = sbuf.st_mode & 0777;
  75. #else
  76.     mode = 0644;        /* Default permissions */
  77. #endif
  78.  
  79.     printf("\nbegin %o %s\n", mode, argv[1]);
  80.  
  81.     encode(in, stdout);
  82.  
  83.     printf("end\n");
  84.     printf("size %ld\n",totalsize);
  85.     exit(0);
  86. }
  87.  
  88. /*
  89.  * copy from in to out, encoding as you go along.
  90.  */
  91. encode(in, out)
  92. FILE *in;
  93. FILE *out;
  94. {
  95. #ifndef unix
  96. extern errno;
  97. #endif
  98.     char buf[80];
  99.     int i, n, checksum;
  100.  
  101.     for (;;) {
  102.         /* 1 (up to) 45 character line */
  103.         n = fr(in, buf, 45);
  104.         putc(ENC(n), out);
  105.  
  106.         checksum = 0;
  107.         for (i=0; i<n; i += 3)
  108.             checksum = (checksum+outdec(&buf[i], out)) % SUMSIZE;
  109.  
  110.         putc(ENC(checksum), out);
  111.         putc('\n', out);
  112.  
  113. #ifndef unix
  114.         /* Error checking under UNIX?? You must be kidding! */
  115.         if (errno) {
  116.             fprintf(stderr, "ERROR: error writing to output\n");
  117.             exit(12);
  118.             }
  119. #endif
  120.         if (n <= 0)
  121.             break;
  122.     }
  123. }
  124.  
  125. /*
  126.  * output one group of 3 bytes, pointed at by p, on file f.
  127.  * return the checksum increment.
  128.  */
  129. int outdec(p, f)
  130. char *p;
  131. FILE *f;
  132. {
  133.     int c1, c2, c3, c4;
  134.  
  135.     c1 = *p >> 2;
  136.     c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
  137.     c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
  138.     c4 = p[2] & 077;
  139.     putc(ENC(c1), f);
  140.     putc(ENC(c2), f);
  141.     putc(ENC(c3), f);
  142.     putc(ENC(c4), f);
  143.  
  144.     return((p[0]+p[1]+p[2]) % SUMSIZE);
  145. }
  146.  
  147. /* fr: like read but stdio */
  148. int
  149. fr(fd, buf, cnt)
  150. FILE *fd;
  151. char *buf;
  152. int cnt;
  153. {
  154.     int c, i;
  155.  
  156.     for (i=0; i<cnt; i++) {
  157.         c = getc(fd);
  158.         if (c == EOF)
  159.             return(i);
  160.         totalsize++;
  161.         buf[i] = c;
  162.     }
  163.     return (cnt);
  164. }
  165.