home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / decode / xxcode.zoo / xxencode.c < prev   
Encoding:
C/C++ Source or Header  |  1990-01-17  |  3.1 KB  |  163 lines

  1.  
  2. #ifndef lint
  3. static char sccsid[] = "@(#)xxencode.c  5.3 (Berkeley) 1/22/85";
  4. #endif
  5.  
  6. /*
  7.  * xxencode [input] output
  8.  *
  9.  * Encode a file so it can be mailed to a remote system.
  10.  */
  11. #define MSDOS
  12. #include <stdio.h>
  13. #ifdef MSDOS
  14. #include <fcntl.h>
  15. #include <io.h>
  16. #endif /* MSDOS */
  17.  
  18. #ifndef VMCMS
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #else  /* VMCMS */
  22. #include <types.h>
  23. #include <stat.h>
  24. #define perror(string) fprintf (stderr, "%s\n", string)
  25. #endif /* VMCMS */
  26.  
  27. /* ENC is the basic 1 character encoding function to make a char printing */
  28. #define ENC(c) ( set[ (c) & 077 ] )
  29. static char set[] = "+-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  30.  
  31. main (argc, argv)
  32. int argc;
  33. char * argv [];
  34.  
  35. {
  36. FILE *in;
  37. struct stat sbuf;
  38. int mode;
  39. char buffer [256];
  40. int i;
  41.  
  42. /* optional 1st argument */
  43. if (argc > 2)
  44.     {
  45.     strcpy (buffer, argv [1]);
  46. #ifdef VMCMS
  47.     for (i = 2; i < argc - 1; i++)
  48.         {
  49.         strcat (buffer, " ");
  50.         strcat (buffer, argv [i]);
  51.         }
  52.     strcat (buffer, " (bin");
  53. #endif /* VMCMS */
  54.     if ((in = fopen (buffer, "r")) == NULL)
  55.         {
  56.         perror (buffer);
  57.         exit(1);
  58.         }
  59.     }
  60. else
  61.     in = stdin;
  62. #ifdef MSDOS
  63. if (setmode (fileno (in), O_BINARY) == -1)
  64.     {
  65.     perror ("Cannot open input file as binary\n");
  66.     exit (3);
  67.     }
  68. #endif /* MSDOS */
  69.  
  70. #ifndef VMCMS
  71. if (isatty (fileno (in)) || argc < 2 || argc > 3)
  72.     {
  73.     fprintf (stderr, "Usage: xxencode [infile] remotefile\n");
  74.     exit(2);
  75.     }
  76. #else
  77. if (isatty (fileno (in)) || argc < 2)
  78.     {
  79.     fprintf (stderr, "Usage: xxencode fn ft fm (options remotefile ");
  80.     fprintf (stderr, "> fn ft fm\n");
  81.     fprintf (stderr, "   or: xxencode remotefile < fn ft fm (options ");
  82.     fprintf (stderr, "> fn ft fm\n");
  83.     fprintf (stderr, "remotefile is a Unix syntax filename.\n");
  84.     exit(2);
  85.     }
  86. #endif /* VMCMS */
  87.  
  88. /* figure out the input file mode */
  89. fstat (fileno (in), &sbuf);
  90. mode = sbuf.st_mode & 0777;
  91. printf ("begin %o %s\n", mode, argv [argc - 1]);
  92.  
  93. encode (in, stdout);
  94.  
  95. printf ("end\n");
  96. exit (0);
  97. }
  98.  
  99. /*
  100.  * copy from in to out, encoding as you go along.
  101.  */
  102. encode(in, out)
  103. FILE *in;
  104. FILE *out;
  105. {
  106.         char buf[80];
  107.         int i, n;
  108.  
  109.         for (;;) {
  110.                 /* 1 (up to) 45 character line */
  111.                 n = fr(in, buf, 45);
  112.                 putc(ENC(n), out);
  113.  
  114.                 for (i=0; i<n; i += 3)
  115.                         outdec(&buf[i], out);
  116.  
  117.                 putc('\n', out);
  118.                 if (n <= 0)
  119.                         break;
  120.         }
  121. }
  122.  
  123. /*
  124.  * output one group of 3 bytes, pointed at by p, on file f.
  125.  */
  126. outdec(p, f)
  127. char *p;
  128. FILE *f;
  129. {
  130.         int c1, c2, c3, c4;
  131.  
  132.         c1 = *p >> 2;
  133.         c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
  134.         c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
  135.         c4 = p[2] & 077;
  136.         putc(ENC(c1), f);
  137.         putc(ENC(c2), f);
  138.         putc(ENC(c3), f);
  139.         putc(ENC(c4), f);
  140. }
  141.  
  142. /* fr: like read but stdio */
  143. int
  144. fr(fd, buf, cnt)
  145. FILE *fd;
  146. char *buf;
  147. int cnt;
  148. {
  149.         int c, i;
  150.  
  151.         for (i=0; i<cnt; i++) {
  152.                 c = getc(fd);
  153.                 if (c == EOF)
  154.                         return(i);
  155.                 buf[i] = c;
  156.         }
  157.         return (cnt);
  158. }
  159.  
  160.  
  161.  
  162.  
  163.