[comp.sys.m6809] UUENCODE/UUDECODE for OS9

pete@wlbr.EATON.COM (Pete Lyall) (07/30/87)

These are the sources for 'uuencode' and 'uudecode' that I recently
posted to this group. - PWL


#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	uudecode.c
#	uuencode.c
# This archive created: Thu Jul 30 11:21:39 1987
export PATH; PATH=/bin:$PATH
if test -f 'uudecode.c'
then
	echo shar: will not over-write existing file "'uudecode.c'"
else
sed 's/^X//' << \SHAR_EOF > 'uudecode.c'
X/*
X** uudecode [input]
X**
X** create the specified file, decoding as you go.
X** used with uuencode.
X*/
X
X#include <stdio.h>
X
X#define DEC(c) (((c) - ' ')&077)         /* single character decode */
X
Xmain(argc, argv)
Xint   argc;
Xchar  **argv;
X   {
X   FILE  *in, *out;
X   char  dest[128], buf[80];
X
X   if (argc > 1)
X      {
X      if ((in = fopen(argv[1], "r")) == NULL)
X         {
X         fprintf(stderr, "can't open %s\n", argv[1]);
X         exit(1);
X         }
X      argv++;
X      argc--;
X      }
X   else
X      in = stdin;
X   if (argc != 1)
X      {
X      fprintf(stderr, "Usage: uudecode [infile]\n");
X      exit(2);
X      }
X   for (; ; )            /* search for header line */
X      {
X      if (fgets(buf, sizeof buf, in) == NULL)
X         {
X         fprintf(stderr, "No begin line\n");
X         exit(3);
X         }
X      if (strncmp(buf, "begin ", 6) == 0)
X         break;
X      }
X   sscanf(buf, "begin %s", dest);
X   if ((out = fopen(dest, "w")) == NULL)        /* create output file */
X      {
X      fprintf(stderr, "can't open %s\n", dest);
X      exit(4);
X      }
X
X   decode(in, out);
X   if (fgets(buf, sizeof buf, in) == NULL || strcmp(buf, "end\n"))
X      {
X      fprintf(stderr, "No end line\n");
X      exit(5);
X      }
X   exit(0);
X   }
X/*page*/
X/*
X** copy from in to out, decoding as you go along.
X*/
X
Xdecode(in, out)
XFILE  *in;
XFILE  *out;
X   {
X   char  buf[80];
X   char  *bp;
X   int   n;
X
X   for (; ; )             /* for each input line */
X      {
X      if (fgets(buf, sizeof buf, in) == NULL)
X         {
X         printf("Short file\n");
X         exit(10);
X         }
X      n = DEC(buf[0]);
X      if (n <= 0)
X         break;
X
X      bp = &buf[1];
X      while (n > 0)
X         {
X         outdec(bp, out, n);
X         bp += 4;
X         n -= 3;
X         }
X      }
X   }
X
X
X/*
X** output a group of 3 bytes (4 input characters).
X** the input chars are pointed to by p, they are to
X** be output to file f.  n is used to tell us not to
X** output all of them at the end of the file.
X*/
X
Xoutdec(p, f, n)
Xchar  *p;
XFILE  *f;
Xint   n;
X   {
X   int   c1, c2, c3;
X
X   c1 = DEC(*p) << 2 | DEC(p[1]) >> 4;
X   c2 = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
X   c3 = DEC(p[2]) << 6 | DEC(p[3]);
X   if (n >= 1)
X      putc(c1, f);
X   if (n >= 2)
X      putc(c2, f);
X   if (n >= 3)
X      putc(c3, f);
X   }
SHAR_EOF
fi # end of overwriting check
if test -f 'uuencode.c'
then
	echo shar: will not over-write existing file "'uuencode.c'"
else
sed 's/^X//' << \SHAR_EOF > 'uuencode.c'
X/*
X** uuencode input >output
X**
X** Encode a file so it can be mailed to a remote system.
X*/
X
X#include <stdio.h>
X
X/* ENC is the basic 1 character encoding function to make a char printing
X*/
X#define ENC(c) (((c)&077) + ' ')
X
Xmain(argc, argv)
Xchar  **argv;
X   {
X   FILE  *in;
X   char  sbuf[32], *mod = argv[0];
X
X   if (argc != 2)
X      {
X      fprintf(stderr, "%s: Usage: uuencode infile\n", mod);
X      exit(2);
X      }
X
X   if ((in = fopen(argv[1], "r")) == NULL)
X      {
X      fprintf(stderr, "%s: Can't open %s\n", mod, argv[1]);
X      exit(1);
X      }
X
X   printf("begin %s\n", argv[1]);
X
X   encode(in, stdout);
X
X   printf("end\n");
X   exit(0);
X   }
X/*page*/
X/*
X** copy from in to out, encoding as you go along.
X*/
Xencode(in, out)
XFILE  *in;
XFILE  *out;
X   {
X   char  buf[80];
X   int   i, n;
X
X   for (; ; )
X      {
X      /* 1 (up to) 45 character line */
X      n = fr(in, buf, 45);
X      putc(ENC(n), out);
X
X      for (i = 0; i < n; i += 3)
X         outdec(&buf[i], out);
X
X      putc('\n', out);
X      if (n <= 0)
X         break;
X      }
X   }
X
X/*
X** output one group of 3 bytes, pointed at by p, on file f.
X*/
Xoutdec(p, f)char *p;
XFILE  *f;
X   {
X   int   c1, c2, c3, c4;
X
X   c1 = *p >> 2;
X   c2 = (*p << 4)&060 | (p[1] >> 4)&017;
X   c3 = (p[1] << 2)&074 | (p[2] >> 6)&03;
X   c4 = p[2] & 077;
X   putc(ENC(c1), f);
X   putc(ENC(c2), f);
X   putc(ENC(c3), f);
X   putc(ENC(c4), f);
X   }
X
X/* fr: like read but stdio */
Xfr(fd, buf, cnt)
XFILE  *fd;
Xchar  *buf;
Xint   cnt;
X   {
X   int   c, i;
X
X   for (i = 0; i < cnt; i++)
X      {
X      c = getc(fd);
X      if (c == EOF)
X         return (i);
X      buf[i] = c;
X      }
X   return (cnt);
X   }
SHAR_EOF
fi # end of overwriting check
#	End of shell archive
exit 0
-- 
                                                   Pete Lyall

Usenet:     {trwrb, scgvaxd, ihnp4, voder, vortex}!wlbr!pete
Compuserve: 76703,4230 (OS9 Sysop) OS9 (home): (805)-985-0632 (24hr./1200 baud)