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