uuencode/decode for the millionth time...

lab at and.cs.liv.ac.uk lab at and.cs.liv.ac.uk
Sat Feb 9 11:04:07 AEST 1991


This seems to be wanted frequently....so here's saving somebody a trip
to find the archives. This is as sent to me.

Alan Thew
qq11 at liverpool.ac.uk
------------------------------cut here-----------------------
#!/bin/sh
# This is a shell archive (produced by shar 3.49)
# To extract the files from this archive, save it to a file, remove
# everything above the "!/bin/sh" line above, and type "sh file_name".
#
# made 09/22/1990 02:48 UTC by rhg at cpsolv
# Source directory /u/rhg/src
#
# existing files will NOT be overwritten unless -c is specified
#
# This shar contains:
# length  mode       name
# ------ ---------- ------------------------------------------
#   1967 -rw-rw-rw- uuencode.c
#   3054 -rw-rw-rw- uudecode.c
#
# ============= uuencode.c ==============
if test -f 'uuencode.c' -a X"$1" != X"-c"; then
	echo 'x - skipping uuencode.c (File already exists)'
else
echo 'x - extracting uuencode.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'uuencode.c' &&
#ifndef lint
static char sccsid[] = "@(#)uuencode.c	5.1 (Berkeley) 7/2/83";
#endif
X
/*
X * uuencode [input] output
X *
X * Encode a file so it can be mailed to a remote system.
X */
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
X
/* ENC is the basic 1 character encoding function to make a char printing */
#if 1 /* Richard H. Gumpertz (RHG at CPS.COM), 24 April 1990 */
#define ENC(c) ((((c) + 077) & 077) + 041)
#else /* RHG */
#define ENC(c) (((c) & 077) + ' ')
#endif /* RHG */
X
main(argc, argv)
char **argv;
{
X	FILE *in;
X	struct stat sbuf;
X	int mode;
X
X	/* optional 1st argument */
X	if (argc > 2) {
#if 0 /* Richard H Gumpertz (RHG at CPS.COM) */
X		if ((in = fopen(argv[1], "rb")) == NULL) {
#else /* RHG */
X		if ((in = fopen(argv[1], "r")) == NULL) {
#endif /* RHG */
X			perror(argv[1]);
X			exit(1);
X		}
X		argv++; argc--;
X	} else
X		in = stdin;
X
X	if (argc != 2) {
X		printf("Usage: uuencode [infile] remotefile\n");
X		exit(2);
X	}
X
X	/* figure out the input file mode */
X	fstat(fileno(in), &sbuf);
X	mode = sbuf.st_mode & 0777;
X	printf("begin %o %s\n", mode, argv[1]);
X
X	encode(in, stdout);
X
X	printf("end\n");
X	exit(0);
}
X
/*
X * copy from in to out, encoding as you go along.
X */
encode(in, out)
FILE *in;
FILE *out;
{
X	char buf[80];
X	int i, n;
X
X	for (;;) {
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 * output one group of 3 bytes, pointed at by p, on file f.
X */
outdec(p, f)
char *p;
FILE *f;
{
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
/* fr: like read but stdio */
int
fr(fd, buf, cnt)
FILE *fd;
char *buf;
int cnt;
{
X	int c, i;
X
X	for (i=0; i<cnt; i++) {
X		c = getc(fd);
X		if (c == EOF)
X			return(i);
X		buf[i] = c;
X	}
X	return (cnt);
}
SHAR_EOF
chmod 0666 uuencode.c ||
echo 'restore of uuencode.c failed'
Wc_c="`wc -c < 'uuencode.c'`"
test 1967 -eq "$Wc_c" ||
	echo 'uuencode.c: original size 1967, current size' "$Wc_c"
fi
# ============= uudecode.c ==============
if test -f 'uudecode.c' -a X"$1" != X"-c"; then
	echo 'x - skipping uudecode.c (File already exists)'
else
echo 'x - extracting uudecode.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'uudecode.c' &&
#ifndef lint
static char sccsid[] = "@(#)uudecode.c	5.1 (Berkeley) 7/2/83";
#endif
X
/*
X * uudecode [input]
X *
X * create the specified file, decoding as you go.
X * used with uuencode.
X */
#include <stdio.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/stat.h>
X
/* single character decode */
#define DEC(c)	(((c) - ' ') & 077)
X
main(argc, argv)
char **argv;
{
X	FILE *in, *out;
X	struct stat sbuf;
X	int mode;
X	char dest[128];
X	char buf[80];
X
X	/* optional input arg */
X	if (argc > 1) {
X		if ((in = fopen(argv[1], "r")) == NULL) {
X			perror(argv[1]);
X			exit(1);
X		}
X		argv++; argc--;
X	} else
X		in = stdin;
X
X	if (argc != 1) {
X		printf("Usage: uudecode [infile]\n");
X		exit(2);
X	}
X
X	/* search for header line */
X	for (;;) {
X		if (fgets(buf, sizeof buf, in) == NULL) {
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 %o %s", &mode, dest);
X
X	/* handle ~user/file format */
X	if (dest[0] == '~') {
X		char *sl;
X		struct passwd *getpwnam();
X		char *index();
X		struct passwd *user;
X		char dnbuf[100];
X
X		sl = index(dest, '/');
X		if (sl == NULL) {
X			fprintf(stderr, "Illegal ~user\n");
X			exit(3);
X		}
X		*sl++ = 0;
X		user = getpwnam(dest+1);
X		if (user == NULL) {
X			fprintf(stderr, "No such user as %s\n", dest);
X			exit(4);
X		}
X		strcpy(dnbuf, user->pw_dir);
X		strcat(dnbuf, "/");
X		strcat(dnbuf, sl);
X		strcpy(dest, dnbuf);
X	}
X
X	/* create output file */
#if 0 /* Richard H. Gumpertz (RHG at CPS.COM) */
X	out = fopen(dest, "wb");
#else /* RHG */
X	out = fopen(dest, "w");
#endif /* RHG */
X	if (out == NULL) {
X		perror(dest);
X		exit(4);
X	}
X	chmod(dest, mode);
X
X	decode(in, out);
X
X	if (fgets(buf, sizeof buf, in) == NULL || strcmp(buf, "end\n")) {
X		fprintf(stderr, "No end line\n");
X		exit(5);
X	}
X	exit(0);
}
X
/*
X * copy from in to out, decoding as you go along.
X */
decode(in, out)
FILE *in;
FILE *out;
{
X	char buf[80];
X	char *bp;
X	int n;
X
X	for (;;) {
X		/* for each input line */
X		if (fgets(buf, sizeof buf, in) == NULL) {
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			outdec(bp, out, n);
X			bp += 4;
X			n -= 3;
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 */
outdec(p, f, n)
char *p;
FILE *f;
{
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
X
/* fr: like read but stdio */
int
fr(fd, buf, cnt)
FILE *fd;
char *buf;
int cnt;
{
X	int c, i;
X
X	for (i=0; i<cnt; i++) {
X		c = getc(fd);
X		if (c == EOF)
X			return(i);
X		buf[i] = c;
X	}
X	return (cnt);
}
X
/*
X * Return the ptr in sp at which the character c appears;
X * NULL if not found
X */
X
#define	NULL	0
X
char *
index(sp, c)
register char *sp, c;
{
X	do {
X		if (*sp == c)
X			return(sp);
X	} while (*sp++);
X	return(NULL);
}
SHAR_EOF
chmod 0666 uudecode.c ||
echo 'restore of uudecode.c failed'
Wc_c="`wc -c < 'uudecode.c'`"
test 3054 -eq "$Wc_c" ||
	echo 'uudecode.c: original size 3054, current size' "$Wc_c"
fi
exit 0



More information about the Alt.sources mailing list