home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Club Amiga de Montreal - CAM
/
CAM_CD_1.iso
/
files
/
149.lha
/
UuEnCode&DeCode
/
uuencode.c
< prev
next >
Wrap
C/C++ Source or Header
|
1988-04-25
|
2KB
|
140 lines
#ifndef lint
static char sccsid[] = "@(#)uuencode.c 5.3-1 (Berkeley) 1/22/85";
#endif
/* modified by ajr to include checksums */
/*
* uuencode [input] output
*
* Encode a file so it can be mailed to a remote system.
*/
#include <stdio.h>
#ifdef unix
#include <sys/types.h>
#include <sys/stat.h>
#endif
#define SUMSIZE 64
/* ENC is the basic 1 character encoding function to make a char printing */
#define ENC(c) ((c) ? ((c) & 077) + ' ': '`')
main(argc, argv)
char **argv;
{
FILE *in;
#ifdef unix
struct stat sbuf;
#endif
int mode;
/* optional 1st argument */
if (argc > 2) {
if ((in = fopen(argv[1], "r")) == NULL) {
perror(argv[1]);
exit(1);
}
argv++; argc--;
} else
in = stdin;
if (argc != 2) {
printf("Usage: uuencode [infile] remotefile\n");
exit(2);
}
#ifdef unix
/* figure out the input file mode */
fstat(fileno(in), &sbuf);
mode = sbuf.st_mode & 0777;
#else
mode = 0777;
#endif
printf("begin %o %s\n", mode, argv[1]);
encode(in, stdout);
printf("end\n");
exit(0);
}
/*
* copy from in to out, encoding as you go along.
*/
encode(in, out)
FILE *in;
FILE *out;
{
char buf[80];
int i, n, checksum;
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);
if (n <= 0)
break;
}
}
/*
* output one group of 3 bytes, pointed at by p, on file f.
* return the checksum increment.
*/
int 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);
return((p[0]+p[1]+p[2]) % SUMSIZE);
}
/* 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);
}
#if (!unix && MANX)
perror (sp)
char *sp;
{
if (sp && *sp) {
fprintf (stderr, "%s: ");
}
fprintf (stderr, "<unknown error>\n");
}
#endif /* Unix */