home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1997 March / Simtel-MSDOS-Mar1997-CD2.iso / 00_start / xxencode.c < prev    next >
C/C++ Source or Header  |  1996-02-23  |  3KB  |  162 lines

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