home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Columbia Kermit
/
kermit.zip
/
archives
/
atarist.tar.gz
/
atarist.tar
/
astuue.c
< prev
Wrap
C/C++ Source or Header
|
1986-06-19
|
3KB
|
117 lines
/*
*
* Uuencode -- encode a file so that it's printable ascii, short lines
*
* Slightly modified from a version posted to net.sources a while back,
* and suitable for compilation on the IBM PC
*
* modified for Lattice C on the ST - 11.05.85 by MSD
*
*/
#include <stdio.h>
#include <osbind.h>
char *progname = "UUENCODE";
#define USAGE "Usage: UUENCODE file [>outfile]\n"
/* ENC is the basic 1 character encoding function to make a char printing */
#define ENC(c) (((c) & 077) + ' ')
FILE *in, *out, *efopen(), *fopen();
main(argc, argv)
int argc; char *argv[];
{
if (argc < 2) {
fprintf(stderr, USAGE);
exit(2);
}
in = efopen(argv[1], "rb"); /* binary input !!! */
if (argc == 3) {
out = efopen(argv[2], "w");
}
else out = stdout;
fprintf(out, "begin %o %s\n", 0777, argv[1]);
encode(in, out);
fprintf(out, "end\n");
}
/*
* copy from in to out, encoding as you go along.
*/
encode(in, out)
FILE *in, *out;
{
char buf[80];
int i, n;
for (;;) {
n = fr(in, buf, 45);
putc(ENC(n), out);
for (i=0; i<n; i += 3)
outdec(&buf[i], out);
putc('\n', out);
if (n <= 0)
break;
}
}
/*
* output one group of 3 bytes, pointed at by p, on file f.
*/
outdec(p, f)
char *p; FILE *f;
{
int 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);
}
/* fr: like read but stdio */
int fr(fd, buf, cnt)
FILE *fd; char *buf; int cnt;
{
int c, i;
for (i = 0; i < cnt; i++) {
c = getc(fd);
if (c == EOF)
return(i);
buf[i] = c;
}
return (cnt);
}
FILE *
efopen(fn, mode)
char *fn, *mode;
{
FILE *unit;
if ((unit = fopen(fn, mode)) == NULL)
error("Cannot open file %s", fn);
else
return unit;
}
extern char *progname;
error(s1, s2)
char *s1, *s2;
{
fprintf(stderr, "%s: ", progname);
fprintf(stderr, s1, s2);
exit(1);
}