home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / OS2MNX1.ZIP / UUE.C < prev    next >
C/C++ Source or Header  |  1989-12-26  |  6KB  |  227 lines

  1. /* uue - bulletproof version of uuencode */
  2.  
  3. /* Uue -- encode a file so that it's printable ascii, short lines
  4.  *
  5.  * Slightly modified from a version posted to net.sources a while back,
  6.  * and suitable for compilation on the IBM PC
  7.  *
  8.  * modified for Lattice C on the ST - 11.05.85 by MSD
  9.  * modified for ALCYON on the ST -    10-24-86 by RDR
  10.  * modified a little more for MWC...  02/09/87 by JPHD
  11.  * (An optional first argument of the form: -nnumber (e.g. -500), will
  12.  * produce a serie of files that long, linked by the include statement,
  13.  * such files are automatically uudecoded by the companion program.)
  14.  * More mods, - ...           05/06/87 by jphd
  15.  * Mods for TOPS 20, and more.     08/06/87 by jphd
  16.  *     (remove freopen and rindex...change filename generation...)
  17.  * (A lot more to do about I/O speed, avoiding completely the stdio.h...)
  18.  *
  19.  */
  20.  
  21. /* $Header: D:/RCS/RCS/uue.c 1.1 89/12/09 05:21:39 RCA Exp $
  22.  * $Log:    uue.c $
  23.  * Revision 1.1  89/12/09  05:21:39  RCA
  24.  * Initial revision
  25.  * 
  26.  */
  27.  
  28. #include <stdio.h>
  29. #include <ctype.h>
  30.  
  31. #define USAGE
  32. #define FILE_NAME 10        /* affects how long names are truncated */
  33.  
  34. /* Prototypes for functions defined in uue.c */
  35. int __near main(int,
  36.              char **);
  37. int __near maketable(void);
  38. int __near makename(void);
  39. int __near encode(void);
  40. int __near outdec(register char *);
  41. int __near fr(register char *,
  42.            register int);
  43.  
  44.  
  45.  
  46.  
  47. /* ENC is the basic 1 character encoding function to make a char printing */
  48. #define ENC(c) (((c) & 077) + ' ')
  49.  
  50. extern FILE *fopen();
  51. FILE *fp, *outp;
  52. char ofname[80];
  53. int lenofname;
  54. int stdo = 0;
  55.  
  56. #ifdef ST
  57. #define READ "rb"
  58. #else
  59. #define READ "r"
  60. #endif
  61.  
  62. int part = 'a', chap = 'a';
  63. #define SEQMAX 'z'
  64. #define SEQMIN 'a'
  65. char seqc = SEQMAX;
  66.  
  67. int split = 0;
  68. fileln = 32000;
  69.  
  70. main(argc, argv)
  71. int argc;
  72. char *argv[];
  73. {
  74.   char *fname;
  75.  
  76.   if (argc < 2) {
  77.         fprintf(stderr, "█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█\n");
  78.         fprintf(stderr, "█ UUE (UUEncode)                           $Author: RCA $  █\n");
  79.         fprintf(stderr, "█             $Date: 89/12/09 05:21:39 $ $Revision: 1.1 $  █\n");
  80.         fprintf(stderr, "█ Usage:   uue [-n] inputfile [-]                          █\n");
  81.         fprintf(stderr, "█ Purpose: Translate a binary file to seven-bit ASCII.     █\n");
  82.         fprintf(stderr, "█ Options: -n Set the number of lines in the output file   █\n");
  83.         fprintf(stderr, "█             (try n=300).  Large files will be split.     █\n");
  84.         fprintf(stderr, "█          \"-\" (following inputfile): write to standard    █\n");
  85.         fprintf(stderr, "█              output.                                     █\n");
  86.         fprintf(stderr, "█ OS:      For DOS or OS/2.                                █\n");
  87.         fprintf(stderr, "█ Credits: MSD, RDR, JPHD                                  █\n");
  88.         fprintf(stderr, "█▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄█\n");
  89.     exit(2);
  90.   }
  91.   if (argv[1][0] == '-') {
  92.     fileln = -atoi(argv[1]);
  93.     if (fileln <= 0) {
  94.         fprintf(stderr, "Wrong file length arg.\n");
  95.         exit();
  96.     }
  97.     split = 1;
  98.     argv++;
  99.     argc--;
  100.   }
  101.   if ((fp = fopen(argv[1], READ)) == NULL) {    /* binary input !!! */
  102.     fprintf(stderr, "Cannot open %s\n", argv[1]);
  103.     exit(1);
  104.   }
  105.   strcpy(ofname, argv[1]);
  106.   fname = ofname;
  107.   do {
  108.     if (*fname == '.') *fname = '\0';
  109.   } while (*fname++);
  110.   /* 10 char prefix + .uue -> 14 chars MAX */
  111.   lenofname = strlen(ofname);
  112.   if (lenofname > FILE_NAME) ofname[FILE_NAME] = '\0';
  113.   strcat(ofname, ".uue");
  114.   lenofname = strlen(ofname);
  115.   if (!split && (argc > 2) && (argv[2][0] == '-')) {
  116.     stdo = 1;
  117.     outp = stdout;
  118.   } else {
  119.         makename();
  120.     if ((outp = fopen(ofname, "w")) == NULL) {
  121.         fprintf(stderr, "Cannot open %s\n", ofname);
  122.         exit(1);
  123.     }
  124.   }
  125.   maketable();
  126.   fprintf(outp, "begin %o %s\n", 0644, argv[1]);
  127.   encode();
  128.   fprintf(outp, "end\n");
  129.   fclose(outp);
  130.   exit(0);
  131. }
  132.  
  133. /* Create ASCII table so a mailer can screw it up and the decode
  134.  * program can restore the error.
  135.  */
  136. maketable()
  137. {
  138.   register int i, j;
  139.  
  140.   fputs("table\n", outp);
  141.   for (i = ' ', j = 0; i < '`'; j++) {
  142.     if (j == 32) putc('\n', outp);
  143.     fputc(i++, outp);
  144.   }
  145.   putc('\n', outp);
  146. }
  147.  
  148. /* Generate the names needed for single and multiple part encoding.  */
  149. makename(void)
  150. {
  151.   if (split) {
  152.     ofname[lenofname - 1] = part;
  153.     ofname[lenofname - 2] = chap;
  154.   }
  155. }
  156.  
  157. /* Copy from in to out, encoding as you go along.  */
  158. encode(void)
  159. {
  160.   char buf[80];
  161.   register int i, n;
  162.   register int lines;
  163.   lines = 6;
  164.  
  165.   for (;;) {
  166.     n = fr(buf, 45);
  167.     putc(ENC(n), outp);
  168.     for (i = 0; i < n; i += 3) outdec(&buf[i]);
  169.     putc(seqc, outp);
  170.     seqc--;
  171.     if (seqc < SEQMIN) seqc = SEQMAX;
  172.     putc('\n', outp);
  173.     ++lines;
  174.     if (split && (lines > fileln)) {
  175.         part++;
  176.         if (part > 'z') {
  177.             part = 'a';
  178.             if (chap == 'z')
  179.                 chap = 'a';    /* loop ... */
  180.             else
  181.                 chap++;
  182.         }
  183.         makename();
  184.         fprintf(outp, "include %s\n", ofname);
  185.         fclose(outp);
  186.         if ((outp = fopen(ofname, "w")) == NULL) {
  187.             fprintf(stderr, "Cannot open %s\n", ofname);
  188.             exit(1);
  189.         }
  190.         maketable();
  191.         fprintf(outp, "begin part %c %s\n", part, ofname);
  192.         lines = 6;
  193.     }
  194.     if (n <= 0) break;
  195.   }
  196. }
  197.  
  198. /* Output one group of 3 bytes, pointed at by p, on file f.  */
  199. outdec(p)
  200. register char *p;
  201. {
  202.   register int c1, c2, c3, c4;
  203.  
  204.   c1 = *p >> 2;
  205.   c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
  206.   c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
  207.   c4 = p[2] & 077;
  208.   putc(ENC(c1), outp);
  209.   putc(ENC(c2), outp);
  210.   putc(ENC(c3), outp);
  211.   putc(ENC(c4), outp);
  212. }
  213.  
  214. /* Fr: like read but stdio */
  215. int fr(buf, cnt)
  216. register char *buf;
  217. register int cnt;
  218. {
  219.   register int c, i;
  220.   for (i = 0; i < cnt; i++) {
  221.     c = fgetc(fp);
  222.     if (feof(fp)) return(i);
  223.     buf[i] = c;
  224.   }
  225.   return(cnt);
  226. }
  227.