
/*
 * uuencode >outfile [infile] name
 *
 * Encode a file so it can be mailed to a remote system.  This version
 * transparantly adds line checksums and a file size for sanity checks.
 *
 */

/* Modified by Andreas R. Kleinert
 *
 * 31.08.97: - reworked for SAS/C 6.58
 *           - better compiler settings
 *           - ANSI-fied
 *
 * 04.08.94: - first, internal version
 *
 *
 * Original authors:
 *
 * Written by Mark Horton
 * Modified by ajr (Alan J Rosenthatl,flaps@utcsri.UUCP) to use checksums
 * Modified by fnf (Fred Fish,well!fnf) to use Keith Pyle's suggestion for
 * compatibility
 * Modified by bcn (Bryce Nesbitt,ucbvax!hoser!bryce) to enable CTRL-C for
 * Amiga Lattice C.  Added a transparent file size trailer for later check.
 * Changed fopen from "r" to "rb" for Messy-dos machines (thanks to Andrew
 * Wylie)
 */

#define __USE_SYSBASE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/stat.h>

#include <exec/types.h>

#include <proto/exec.h>


LONG filemode(FILE *in);
void encode(FILE *in, FILE *out);
LONG outdec(UBYTE *p, FILE *f);
LONG fr(FILE *fd, UBYTE *buf, LONG cnt);

LONG totalsize = 0; /* Used to count the file size because ftell() does
		       not return sane results for pipes */

UBYTE __aligned version [] = "\0$VER: uuencode 1.0 (31.8.97)";

long main(long argc, char **argv)
{
    FILE *in;
    LONG mode;

	/* optional 1st argument */
	if (argc > 2) {
		if ((in = fopen(argv[1], "r")) == NULL) {
			fprintf(stderr, "ERROR: can't find %s\n", argv[1]);
			fprintf(stderr, "USAGE: uuencode >outfile [infile] name\n");
			exit(20);
		}
		argv++; argc--;
	} else
		in = stdin;

	if (argc != 2) {
		fprintf(stderr, "USAGE: uuencode >outfile [infile] name\n");
		exit(0);
	}

        mode = filemode(in);

	printf("\nbegin %o %s\n", mode, argv[1]);

	encode(in, stdout);

	printf("end\n");
	printf("size %ld\n",totalsize);
	exit(0);
}

LONG filemode(FILE *in)
{
#ifdef unix_stat
    struct stat sbuf;
	
    fstat(fileno(in), &sbuf);
    return( sbuf.st_mode & 0777); /* figure out the input file mode */

#else
    return( 0644 );               /* Default permissions */
#endif
}

#define SUMSIZE 64  /* 6 bits */
/* ENC is the basic 1 character encode function to make a char printing */
/* Each output character represents 6 bits of input */
#define ENC(c) ((c) ? ((c) & 077) + ' ': '`')

/*
 * copy from in to out, encoding as you go along.
 */
void encode(FILE *in, FILE *out)
{
 extern errno;

	LONG i, n, checksum;
	char buf[256];

	for (;;) {
		/* 1 (up to) 45 character line */
		n = fr(in, buf, 45);
		putc(ENC(n), out);

		checksum = 0;
		for (i=0; i<n; i += 3)
		    checksum = (checksum+outdec(&buf[i], out)) % SUMSIZE;

		putc(ENC(checksum), out);
		putc('\n', out);

		/* Error checking under UNIX?? You must be kidding! */
		if (errno) {
		    fprintf(stderr, "ERROR: error writing to output\n");
			exit(20);
		    }

		if (n <= 0)
			break;
	}
}

/*
 * output one group of 3 bytes, pointed at by p, on file f.
 * return the checksum increment.
 */
LONG outdec(UBYTE *p, FILE *f)
{
	LONG c1, c2, c3, c4;

	c1 = *p >> 2;
	c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
	c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
	c4 = p[2] & 077;
	putc(ENC(c1), f);
	putc(ENC(c2), f);
	putc(ENC(c3), f);
	putc(ENC(c4), f);

	return((p[0]+p[1]+p[2]) % SUMSIZE);
}

/* fr: like read but stdio */
LONG fr(FILE *fd, UBYTE *buf, LONG cnt)
{
	LONG c, i;

	for (i=0; i<cnt; i++) {
		c = getc(fd);
		if (c == EOF)
			return(i);
		totalsize++;
		buf[i] = c;
	}
	return (cnt);
}
