home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / UUCPbb_2_1_src.lzh / UUCPBB21 / uuencode.c < prev    next >
Text File  |  1994-09-25  |  3KB  |  134 lines

  1. /* uuencode input >output
  2.  
  3.    Encode a file so it can be mailed to a remote system.
  4.  
  5.    modified so it does not put an absolute pathname in the encoded file's
  6.    header.  Bob Billson <...uunet!kc2wz!bob>   92 Apr 14
  7.  
  8.    Added stdin support to emulate UNIX version.
  9.    If only one parameter is specified, uuencode uses that parameter as the
  10.    name and takes input from stdin. - Boisy G. Pitre  05/16/92  */
  11.  
  12. #include "uucp.h"
  13.  
  14. /* ENC is the basic 1 character encoding function to make a char printing */
  15.  
  16. #define ENC(c) (((c)&077) + ' ')
  17.  
  18.  
  19. main (argc, argv)
  20. int argc;
  21. char  *argv[];
  22. {
  23.      FILE *in;
  24.      char filename[128];
  25.      register char *p;
  26.  
  27.      in = stdin;
  28.  
  29.      if ((argc < 2 || argc > 3)  ||  strcmp (argv[1], "-?") == 0)
  30.        {
  31.           fprintf (stderr, "%s: Usage: uuencode name [infile]\n", argv[0]);
  32.           fputs ("If infile is not given, file is read from standard input",
  33.                  stderr);
  34.           exit (0);
  35.        }
  36.  
  37.      if (argc == 3)
  38.           if ((in = fopen (argv[2], "r")) == NULL)
  39.             {
  40.                fprintf (stderr, "%s: Can't open %s\n", argv[0], argv[2]);
  41.                exit (1);
  42.             }
  43.  
  44.      strncpy (filename, argv[1], sizeof (filename));
  45.      filename[sizeof (filename) - 1] = '\0';
  46.  
  47.      /* strip off any directory names */
  48.      p = strrchr (filename, '/');
  49.  
  50.      if (p == NULL)
  51.           p = filename;
  52.      else
  53.           ++p;
  54.  
  55.      printf ("begin 644 %s\n", p);
  56.      encode (in, stdout);
  57.      puts ("end");
  58.      exit (0);
  59. }
  60.  
  61.  
  62.  
  63. /* copy from in to out, encoding as you go along */
  64.  
  65. int encode (in, out)
  66. FILE  *in;
  67. FILE  *out;
  68. {
  69.      char  buf[80];
  70.      register int i;
  71.      int   n;
  72.  
  73.      for (;;)
  74.        {
  75.           /* 1 (up to) 45 character line */
  76.           n = fr (in, buf, 45);
  77.           putc (ENC(n), out);
  78.  
  79.           for (i = 0; i < n; i += 3)
  80.                outdec (&buf[i], out);
  81.  
  82.           putc ('\n', out);
  83.  
  84.           if (n <= 0)
  85.                break;
  86.        }
  87. }
  88.  
  89.  
  90.  
  91. /* output one group of 3 bytes, pointed at by p, on file f. */
  92.  
  93. int outdec (p, f)
  94. register char *p;
  95. FILE  *f;
  96. {
  97.      int   c1, c2, c3, c4;
  98.  
  99.      c1 = *p >> 2;
  100.      c2 = (*p << 4)&060 | (*(p+1) >> 4)&017;
  101.      c3 = (*(p+1) << 2)&074 | (*(p+2) >> 6)&03;
  102.      c4 = *(p+2) & 077;
  103.  
  104.      putc (ENC(c1), f);
  105.      putc (ENC(c2), f);
  106.      putc (ENC(c3), f);
  107.      putc (ENC(c4), f);
  108. }
  109.  
  110.  
  111.  
  112. /* fr: like read but stdio */
  113.  
  114. int fr (fd, buf, cnt)
  115. FILE  *fd;
  116. char  *buf;
  117. int   cnt;
  118. {
  119.      register int i;
  120.      int c;
  121.  
  122.      for (i = 0; i < cnt; i++)
  123.        {
  124.           c = getc (fd);
  125.  
  126.           if (c == EOF)
  127.                return (i);
  128.  
  129.           buf[i] = c;
  130.        }
  131.  
  132.      return (cnt);
  133. }
  134.