From bff465ff809319981b131ddaf0c30d0b2208e6a0 Mon Sep 17 00:00:00 2001 From: Tony Finch Date: Thu, 27 Feb 2003 03:28:29 +0000 Subject: [PATCH] Add a relaxed mode for handling raw input which is missing the initial and possibly the final framing lines. This is particularly handy for dealing with broken files or fragments of MIME messages. --- usr.bin/uudecode/uudecode.c | 47 ++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/usr.bin/uudecode/uudecode.c b/usr.bin/uudecode/uudecode.c index 7bf2fcc02f5..b4697eefa84 100644 --- a/usr.bin/uudecode/uudecode.c +++ b/usr.bin/uudecode/uudecode.c @@ -61,6 +61,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -70,7 +71,7 @@ __FBSDID("$FreeBSD$"); static const char *infile, *outfile; static FILE *infp, *outfp; -static int cflag, iflag, oflag, pflag, sflag; +static int base64, cflag, iflag, oflag, pflag, rflag, sflag; static void usage(void); static int decode(void); @@ -83,18 +84,24 @@ main(int argc, char *argv[]) { int rval, ch; - while ((ch = getopt(argc, argv, "cio:ps")) != -1) { + if (strcmp(basename(argv[0]), "b64decode") == 0) + base64 = 1; + + while ((ch = getopt(argc, argv, "cimo:prs")) != -1) { switch(ch) { case 'c': - if (oflag) + if (oflag || rflag) usage(); cflag = 1; /* multiple uudecode'd files */ break; case 'i': iflag = 1; /* ask before override files */ break; + case 'm': + base64 = 1; + break; case 'o': - if (cflag || pflag || sflag) + if (cflag || pflag || rflag || sflag) usage(); oflag = 1; /* output to the specified file */ sflag = 1; /* do not strip pathnames for output */ @@ -105,6 +112,11 @@ main(int argc, char *argv[]) usage(); pflag = 1; /* print output to stdout */ break; + case 'r': + if (cflag || oflag) + usage(); + rflag = 1; /* decode raw data */ + break; case 's': if (oflag) usage(); @@ -142,6 +154,15 @@ decode(void) { int r, v; + if (rflag) { + /* relaxed alternative to decode2() */ + outfile = "/dev/stdout"; + outfp = stdout; + if (base64) + return (base64_decode()); + else + return (uu_decode()); + } v = decode2(); if (v == EOF) { warnx("%s: missing or bad \"begin\" line", infile); @@ -158,7 +179,7 @@ decode(void) static int decode2(void) { - int base64, flags, fd, mode; + int flags, fd, mode; size_t n, m; char *p, *q; void *handle; @@ -292,7 +313,9 @@ uu_decode(void) /* for each input line */ for (;;) { if (fgets(p = buf, sizeof(buf), infp) == NULL) { - warnx("%s: short file", infile); + if (rflag) + return (0); + warnx("%s: %s: short file", infile, outfile); return (1); } @@ -355,7 +378,7 @@ uu_decode(void) return (1); } if (fclose(outfp) != 0) { - warnx("%s: %s", infile, outfile); + warn("%s: %s", infile, outfile); return (1); } return (0); @@ -370,14 +393,16 @@ base64_decode(void) for (;;) { if (fgets(inbuf, sizeof(inbuf), infp) == NULL) { - warnx("%s: short file", infile); + if (rflag) + return (0); + warnx("%s: %s: short file", infile, outfile); return (1); } if (strcmp(inbuf, "====") == 0 || strcmp(inbuf, "====\n") == 0 || strcmp(inbuf, "====\r\n") == 0) { if (fclose(outfp) != 0) { - warnx("%s: %s", infile, outfile); + warn("%s: %s", infile, outfile); return (1); } return (0); @@ -398,9 +423,9 @@ static void usage(void) { (void)fprintf(stderr, -"usage: uudecode [-cips] [file ...]\n" +"usage: uudecode [-cimprs] [file ...]\n" " uudecode [-i] -o output_file [file]\n" -" b64decode [-cips] [file ...]\n" +" b64decode [-cimprs] [file ...]\n" " b64decode [-i] -o output_file [file]\n"); exit(1); } -- 2.45.2