/* * Uudecode -- decode a uuencoded file back to binary form. * * Slightly modified from a version posted to net.sources once; * suitable for compilation on an IBM PC. * * modified for Lattice C on the ST - 11.05.86 by MSD * */ #include #include char *Progname = "UUDECODE"; #define USAGE "Usage: UUDECODE [file]\n" /* single character decode */ #define DEC(c) (((c) - ' ') & 077) FILE *in, *out, *efopen(), *fopen(); main(argc, argv) int argc; char *argv[]; { int mode; char dest[128]; char buf[80]; /* optional input arg */ if (argc > 1) { in = efopen(argv[1], "r"); argv++; argc--; } else in = stdin; if (argc != 1) { fprintf(stderr, USAGE); exit(2); } /* search for header line */ for (;;) { if (fgets(buf, sizeof buf, in) == NULL) { fprintf(stderr, "No begin line\n"); exit(3); } if (strncmp(buf, "begin ", 6) == 0) break; } sscanf(buf, "begin %o %s", &mode, dest); out = efopen(dest, "wb"); /* create output file - binary output !!! */ decode(in, out); fclose(out); if (fgets(buf, sizeof buf, in) == NULL || strcmp(buf, "end\n")) { fprintf(stderr, "No end line\n"); exit(5); } } /* * copy from in to out, decoding as you go along. */ decode(in, out) FILE *in, *out; { char buf[80]; char *bp; int n; for (;;) { if (fgets(buf, sizeof buf, in) == NULL) { fprintf(stderr, "Short file\n"); break; } n = DEC(buf[0]); if (n <= 0) break; bp = &buf[1]; while (n > 0) { outdec(bp, out, n); bp += 4; n -= 3; } } } /* * output a group of 3 bytes (4 input characters). * the input chars are pointed to by p, they are to * be output to file f. n is used to tell us not to * output all of them at the end of the file. */ outdec(p, f, n) char *p; FILE *f; int n; { int c1, c2, c3; c1 = DEC(*p) << 2 | DEC(p[1]) >> 4; c2 = DEC(p[1]) << 4 | DEC(p[2]) >> 2; c3 = DEC(p[2]) << 6 | DEC(p[3]); if (n >= 1) putc(c1, f); if (n >= 2) putc(c2, f); if (n >= 3) putc(c3, 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); } /* If your library already has this function, use it and nuke the code below */ /* Open a file, aborting on failure */ /* Written by Bernie Roehl, June 1985 */ 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); }