sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit 7768918d6a0d1a0b026a962963ca1ef619d05a37
parent d61add5dee986e3abd462fd648884e2aba47e8f6
Author: sin <sin@2f30.org>
Date:   Fri, 13 Feb 2015 11:19:19 +0000

uudecode: Style fix

Diffstat:
Muudecode.c | 138++++++++++++++++++++++++++++++++++++++-----------------------------------------
1 file changed, 67 insertions(+), 71 deletions(-)

diff --git a/uudecode.c b/uudecode.c @@ -11,80 +11,9 @@ #include "text.h" #include "util.h" -static void uudecodeb64(FILE *, FILE *); -static void uudecode(FILE *, FILE *); -static void parseheader(FILE *, const char *, char **, mode_t *, char **); -static FILE *parsefile(const char *); - -static void -usage(void) -{ - eprintf("usage: %s [-m] [-o output] [file]\n", argv0); -} - static int mflag = 0; static int oflag = 0; -int -main(int argc, char *argv[]) -{ - FILE *fp = NULL, *nfp = NULL; - char *fname, *header; - const char *ifname; - mode_t mode = 0; - void (*d) (FILE *, FILE *) = NULL; - char *ofname = NULL; - - ARGBEGIN { - case 'm': - mflag = 1; /* accepted but unused (autodetect file type) */ - break; - case 'o': - oflag = 1; - ofname = EARGF(usage()); - break; - default: - usage(); - } ARGEND; - - if (argc > 1) - usage(); - - if (argc == 0) { - fp = stdin; - ifname = "<stdin>"; - } else { - if (!(fp = fopen(argv[0], "r"))) - eprintf("fopen %s:", argv[0]); - ifname = argv[0]; - } - - parseheader(fp, ifname, &header, &mode, &fname); - - if (!strncmp(header, "begin", sizeof("begin"))) - d = uudecode; - else if (!strncmp(header, "begin-base64", sizeof("begin-base64"))) - d = uudecodeb64; - else - eprintf("unknown header %s:", header); - - if (oflag) - fname = ofname; - if (!(nfp = parsefile(fname))) - eprintf("fopen %s:", fname); - - d(fp, nfp); - - if (nfp != stdout && chmod(fname, mode) < 0) - eprintf("chmod %s:", fname); - if (fp) - fclose(fp); - if (nfp) - fclose(nfp); - - return 0; -} - static FILE * parsefile(const char *fname) { @@ -142,6 +71,7 @@ parseheader(FILE *fp, const char *s, char **header, mode_t *mode, if (n > 0) *fname = q; } + static const char b64dt[] = { -1,-1,-1,-1,-1,-1,-1,-1,-1,-2,-2,-2,-2,-2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, @@ -286,3 +216,69 @@ uudecode(FILE *fp, FILE *outfp) eprintf("invalid uudecode footer \"end\" not found\n"); free(bufb); } + +static void +usage(void) +{ + eprintf("usage: %s [-m] [-o output] [file]\n", argv0); +} + +int +main(int argc, char *argv[]) +{ + FILE *fp = NULL, *nfp = NULL; + char *fname, *header; + const char *ifname; + mode_t mode = 0; + void (*d) (FILE *, FILE *) = NULL; + char *ofname = NULL; + + ARGBEGIN { + case 'm': + mflag = 1; /* accepted but unused (autodetect file type) */ + break; + case 'o': + oflag = 1; + ofname = EARGF(usage()); + break; + default: + usage(); + } ARGEND; + + if (argc > 1) + usage(); + + if (argc == 0) { + fp = stdin; + ifname = "<stdin>"; + } else { + if (!(fp = fopen(argv[0], "r"))) + eprintf("fopen %s:", argv[0]); + ifname = argv[0]; + } + + parseheader(fp, ifname, &header, &mode, &fname); + + if (!strncmp(header, "begin", sizeof("begin"))) + d = uudecode; + else if (!strncmp(header, "begin-base64", sizeof("begin-base64"))) + d = uudecodeb64; + else + eprintf("unknown header %s:", header); + + if (oflag) + fname = ofname; + if (!(nfp = parsefile(fname))) + eprintf("fopen %s:", fname); + + d(fp, nfp); + + if (nfp != stdout && chmod(fname, mode) < 0) + eprintf("chmod %s:", fname); + if (fp) + fclose(fp); + if (nfp) + fclose(nfp); + + return 0; +}