sbase

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

commit d23cc72490f42351a20cd12202d94d6e2d65c1f9
parent e8a4f378847b5e8717c1a0c54179d91ce4b04265
Author: FRIGN <dev@frign.de>
Date:   Mon, 25 May 2015 01:33:19 +0200

Simplify return & fshut() logic

Get rid of the !!()-constructs and use ret where available (or introduce it).

In some cases, there would be an "abort" on the first fshut-error, but we want
to close all files and report all warnings and then quit, not just the warning
for the first file.

Diffstat:
Mcat.c | 4+++-
Mcksum.c | 4+++-
Mcmp.c | 14++++++++------
Mcol.c | 6+++++-
Mcols.c | 4+++-
Mcomm.c | 9++++++---
Mcut.c | 4+++-
Mexpand.c | 4+++-
Mexpr.c | 5++++-
Mfind.c | 4+++-
Mfold.c | 4+++-
Mgrep.c | 5+++--
Mhead.c | 4+++-
Mjoin.c | 12++++++------
Mmd5sum.c | 8+++++---
Mnl.c | 7+++++--
Mpaste.c | 6++++--
Msed.c | 6++++--
Msha1sum.c | 8+++++---
Msha256sum.c | 8+++++---
Msha512sum.c | 8+++++---
Msort.c | 5+++--
Msplit.c | 9++++++---
Msponge.c | 5++++-
Mstrings.c | 4+++-
Mtail.c | 6++++--
Mtee.c | 6++++--
Mtr.c | 7+++++--
Munexpand.c | 4++--
Muniq.c | 8++++----
Muudecode.c | 7+++++--
Muuencode.c | 7+++++--
Mwc.c | 4+++-
Mxargs.c | 8+++++---
34 files changed, 142 insertions(+), 72 deletions(-)

diff --git a/cat.c b/cat.c @@ -44,5 +44,7 @@ main(int argc, char *argv[]) } } - return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret; + ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); + + return ret; } diff --git a/cksum.c b/cksum.c @@ -111,5 +111,7 @@ main(int argc, char *argv[]) } } - return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret; + ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); + + return ret; } diff --git a/cmp.c b/cmp.c @@ -16,7 +16,7 @@ main(int argc, char *argv[]) { FILE *fp[2]; size_t line = 1, n; - int lflag = 0, sflag = 0, same = 1, b[2]; + int ret = 0, lflag = 0, sflag = 0, same = 1, b[2]; ARGBEGIN { case 'l': @@ -72,9 +72,11 @@ main(int argc, char *argv[]) } } - enfshut(2, fp[0], argv[0]); - if (fp[0] != fp[1]) - enfshut(2, fp[1], argv[1]); - enfshut(2, stdout, "<stdout>"); - return !same; + if (!ret) + ret = !same; + if (fshut(fp[0], argv[0]) | (fp[0] != fp[1] && fshut(fp[1], argv[1])) | + fshut(stdout, "<stdout>")) + ret = 2; + + return ret; } diff --git a/col.c b/col.c @@ -190,6 +190,8 @@ usage(void) int main(int argc, char *argv[]) { + int ret = 0; + ARGBEGIN { case 'b': backspace = 1; @@ -217,5 +219,7 @@ main(int argc, char *argv[]) col(); flush(); - return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); + ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); + + return ret; } diff --git a/cols.c b/cols.c @@ -87,5 +87,7 @@ main(int argc, char *argv[]) putchar('\n'); } - return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret; + ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); + + return ret; } diff --git a/comm.c b/comm.c @@ -33,7 +33,7 @@ main(int argc, char *argv[]) { FILE *fp[2]; size_t linelen[2] = { 0, 0 }; - int i, diff = 0; + int ret = 0, i, diff = 0; char *line[2] = { NULL, NULL }; ARGBEGIN { @@ -79,6 +79,9 @@ main(int argc, char *argv[]) printline((2 - diff) % 3, line[MAX(0, diff)]); } end: - return !!(fshut(fp[0], argv[0]) + (fp[0] != fp[1] && fshut(fp[1], argv[1])) + - fshut(stdout, "<stdout>")); + ret |= fshut(fp[0], argv[0]); + ret |= (fp[0] != fp[1]) && fshut(fp[1], argv[1]); + ret |= fshut(stdout, "<stdout>"); + + return ret; } diff --git a/cut.c b/cut.c @@ -201,5 +201,7 @@ main(int argc, char *argv[]) } } - return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret; + ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); + + return ret; } diff --git a/expand.c b/expand.c @@ -125,5 +125,7 @@ main(int argc, char *argv[]) } } - return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret; + ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); + + return ret; } diff --git a/expr.c b/expr.c @@ -258,8 +258,11 @@ main(int argc, char *argv[]) int ret; argv0 = argv[0], argc--, argv++; + ret = !parse(argv, argc); - enfshut(3, stdout, "<stdout>"); + + if (fshut(stdout, "<stdout>")) + ret = 3; return ret; } diff --git a/find.c b/find.c @@ -1036,5 +1036,7 @@ main(int argc, char **argv) t->u.pinfo->freearg(t->extra); free(toks); - return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || gflags.ret; + gflags.ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); + + return gflags.ret; } diff --git a/fold.c b/fold.c @@ -110,5 +110,7 @@ main(int argc, char *argv[]) } } - return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret; + ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); + + return ret; } diff --git a/grep.c b/grep.c @@ -277,7 +277,8 @@ main(int argc, char *argv[]) } } - enfshut(Error, stdin, "<stdin>"); - enfshut(Error, stdout, "<stdout>"); + if (fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>")) + match = Error; + return match; } diff --git a/head.c b/head.c @@ -71,5 +71,7 @@ main(int argc, char *argv[]) } } - return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret; + ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); + + return ret; } diff --git a/join.c b/join.c @@ -477,7 +477,7 @@ main(int argc, char *argv[]) { size_t jf[2] = { jfield, jfield, }; FILE *fp[2]; - int n; + int ret = 0, n; char *fno; ARGBEGIN { @@ -544,9 +544,9 @@ main(int argc, char *argv[]) if (oflag) freespecs(&output); - enfshut(2, fp[0], argv[0]); - if (fp[0] != fp[1]) - enfshut(2, fp[1], argv[1]); - enfshut(2, stdout, "<stdout>"); - return 0; + if (fshut(fp[0], argv[0]) | (fp[0] != fp[1] && fshut(fp[1], argv[1])) | + fshut(stdout, "<stdout>")) + ret = 2; + + return ret; } diff --git a/md5sum.c b/md5sum.c @@ -24,7 +24,7 @@ usage(void) int main(int argc, char *argv[]) { - int (*cryptfunc)(int, char **, struct crypt_ops *, uint8_t *, size_t) = cryptmain; + int ret = 0, (*cryptfunc)(int, char **, struct crypt_ops *, uint8_t *, size_t) = cryptmain; uint8_t md[MD5_DIGEST_LENGTH]; ARGBEGIN { @@ -35,6 +35,8 @@ main(int argc, char *argv[]) usage(); } ARGEND; - return cryptfunc(argc, argv, &md5_ops, md, sizeof(md)) || - !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); + ret |= cryptfunc(argc, argv, &md5_ops, md, sizeof(md)); + ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); + + return ret; } diff --git a/nl.c b/nl.c @@ -113,6 +113,7 @@ main(int argc, char *argv[]) { FILE *fp = NULL; size_t l, s; + int ret = 0; char *d, *formattype, *formatblit; ARGBEGIN { @@ -199,6 +200,8 @@ main(int argc, char *argv[]) nl(argv[0], fp); } - return !!((fp && fp != stdin && fshut(fp, argv[0])) - + fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); + ret |= fp && fp != stdin && fshut(fp, argv[0]); + ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); + + return ret; } diff --git a/paste.c b/paste.c @@ -133,7 +133,9 @@ main(int argc, char *argv[]) for (i = 0; i < argc; i++) if (dsc[i].fp != stdin && fshut(dsc[i].fp, argv[i])) - ret = 1; + ret |= fshut(dsc[i].fp, argv[i]); - return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret; + ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); + + return ret; } diff --git a/sed.c b/sed.c @@ -1682,7 +1682,7 @@ int main(int argc, char *argv[]) { char *arg; - int script = 0; + int ret = 0, script = 0; ARGBEGIN { case 'n': @@ -1717,5 +1717,7 @@ main(int argc, char *argv[]) files = argv; run(); - return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); + ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); + + return ret; } diff --git a/sha1sum.c b/sha1sum.c @@ -23,7 +23,7 @@ usage(void) int main(int argc, char *argv[]) { - int (*cryptfunc)(int, char **, struct crypt_ops *, uint8_t *, size_t) = cryptmain; + int ret = 0, (*cryptfunc)(int, char **, struct crypt_ops *, uint8_t *, size_t) = cryptmain; uint8_t md[SHA1_DIGEST_LENGTH]; ARGBEGIN { @@ -34,6 +34,8 @@ main(int argc, char *argv[]) usage(); } ARGEND; - return cryptfunc(argc, argv, &sha1_ops, md, sizeof(md)) || - !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); + ret |= cryptfunc(argc, argv, &sha1_ops, md, sizeof(md)); + ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); + + return ret; } diff --git a/sha256sum.c b/sha256sum.c @@ -23,7 +23,7 @@ usage(void) int main(int argc, char *argv[]) { - int (*cryptfunc)(int, char **, struct crypt_ops *, uint8_t *, size_t) = cryptmain; + int ret = 0, (*cryptfunc)(int, char **, struct crypt_ops *, uint8_t *, size_t) = cryptmain; uint8_t md[SHA256_DIGEST_LENGTH]; ARGBEGIN { @@ -34,6 +34,8 @@ main(int argc, char *argv[]) usage(); } ARGEND; - return cryptfunc(argc, argv, &sha256_ops, md, sizeof(md)) || - !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); + ret |= cryptfunc(argc, argv, &sha256_ops, md, sizeof(md)); + ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); + + return ret; } diff --git a/sha512sum.c b/sha512sum.c @@ -23,7 +23,7 @@ usage(void) int main(int argc, char *argv[]) { - int (*cryptfunc)(int, char **, struct crypt_ops *, uint8_t *, size_t) = cryptmain; + int ret = 0, (*cryptfunc)(int, char **, struct crypt_ops *, uint8_t *, size_t) = cryptmain; uint8_t md[SHA512_DIGEST_LENGTH]; ARGBEGIN { @@ -34,6 +34,8 @@ main(int argc, char *argv[]) usage(); } ARGEND; - return cryptfunc(argc, argv, &sha512_ops, md, sizeof(md)) || - !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); + ret |= cryptfunc(argc, argv, &sha512_ops, md, sizeof(md)); + ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); + + return ret; } diff --git a/sort.c b/sort.c @@ -332,7 +332,8 @@ main(int argc, char *argv[]) } } - enfshut(2, stdin, "<stdin>"); - enfshut(2, stdout, "<stdout>"); + if (fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>")) + ret = 2; + return ret; } diff --git a/split.c b/split.c @@ -50,7 +50,7 @@ main(int argc, char *argv[]) FILE *in = stdin, *out = NULL; size_t size = 1000, scale = 1, n; long l; - int ch, plen, slen = 2, always = 0; + int ret = 0, ch, plen, slen = 2, always = 0; char name[NAME_MAX + 1], *prefix = "x", *file = NULL, *tmp, *end; ARGBEGIN { @@ -124,6 +124,9 @@ main(int argc, char *argv[]) putc(ch, out); } - return !!(fshut(in, "<infile>") + (out && fshut(out, "<outfile>")) + - fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); + ret |= (in != stdin) && fshut(in, "<infile>"); + ret |= out && (out != stdout) && fshut(out, "<outfile>"); + ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); + + return ret; } diff --git a/sponge.c b/sponge.c @@ -14,6 +14,7 @@ int main(int argc, char *argv[]) { FILE *fp, *tmpfp; + int ret = 0; argv0 = argv[0], argc--, argv++; @@ -29,5 +30,7 @@ main(int argc, char *argv[]) eprintf("fopen %s:", argv[0]); concat(tmpfp, "<tmpfile>", fp, argv[0]); - return !!(fshut(fp, argv[0]) + fshut(tmpfp, "<tmpfile>")); + ret |= fshut(fp, argv[0]) | fshut(tmpfp, "<tmpfile>"); + + return ret; } diff --git a/strings.c b/strings.c @@ -94,5 +94,7 @@ main(int argc, char *argv[]) } } - return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret; + ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); + + return ret; } diff --git a/tail.c b/tail.c @@ -47,7 +47,7 @@ taketail(FILE *fp, const char *str, size_t n) ring = ecalloc(n, sizeof(*ring)); size = ecalloc(n, sizeof(*size)); - for (i = j = 0; getline(&ring[i], &size[i], fp) > 0; ) + for (i = j = 0; getline(ring + i, size + i, fp) > 0; ) i = j = (i + 1) % n; } else { r = ecalloc(n, sizeof(*r)); @@ -157,5 +157,7 @@ main(int argc, char *argv[]) } } - return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret; + ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); + + return ret; } diff --git a/tee.c b/tee.c @@ -15,7 +15,7 @@ main(int argc, char *argv[]) { FILE **fps = NULL; size_t i, n, nfps; - int aflag = 0, iflag = 0; + int ret = 0, aflag = 0, iflag = 0; char buf[BUFSIZ]; ARGBEGIN { @@ -47,5 +47,7 @@ main(int argc, char *argv[]) } } - return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); + ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); + + return ret; } diff --git a/tr.c b/tr.c @@ -174,6 +174,7 @@ main(int argc, char *argv[]) { Rune r = 0, lastrune = 0; size_t off1, off2, i, m; + int ret = 0; ARGBEGIN { case 'c': @@ -204,8 +205,10 @@ main(int argc, char *argv[]) if (set2check && cflag && !dflag) eprintf("set2 can't be imaged to from a complement.\n"); read: - if (!efgetrune(&r, stdin, "<stdin>")) - return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); + if (!efgetrune(&r, stdin, "<stdin>")) { + ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); + return ret; + } off1 = off2 = 0; for (i = 0; i < set1ranges; i++) { if (set1[i].start <= r && r <= set1[i].end) { diff --git a/unexpand.c b/unexpand.c @@ -168,7 +168,7 @@ main(int argc, char *argv[]) } } - efshut(stdin, "<stdin>"); - efshut(stdout, "<stdout>"); + ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); + return ret; } diff --git a/uniq.c b/uniq.c @@ -97,7 +97,7 @@ int main(int argc, char *argv[]) { FILE *fp[2] = { stdin, stdout }; - int i; + int ret = 0, i; char *fname[2] = { "<stdin>", "<stdout>" }; ARGBEGIN { @@ -134,7 +134,7 @@ main(int argc, char *argv[]) uniq(fp[0], fp[1]); uniqfinish(fp[1]); - efshut(fp[0], fname[0]); - efshut(fp[1], fname[1]); - return 0; + ret |= fshut(fp[0], fname[0]) | fshut(fp[1], fname[1]); + + return ret; } diff --git a/uudecode.c b/uudecode.c @@ -228,6 +228,7 @@ main(int argc, char *argv[]) { FILE *fp = NULL, *nfp = NULL; mode_t mode = 0; + int ret = 0; char *fname, *header, *ifname, *ofname = NULL; void (*d) (FILE *, FILE *) = NULL; @@ -274,6 +275,8 @@ main(int argc, char *argv[]) if (nfp != stdout && chmod(fname, mode) < 0) eprintf("chmod %s:", fname); - return !!(fshut(fp, fp == stdin ? "<stdin>" : argv[0]) + - fshut(nfp, nfp == stdout ? "<stdout>" : fname)); + ret |= fshut(fp, (fp == stdin) ? "<stdin>" : argv[0]); + ret |= fshut(nfp, (nfp == stdout) ? "<stdout>" : fname); + + return ret; } diff --git a/uuencode.c b/uuencode.c @@ -102,6 +102,7 @@ main(int argc, char *argv[]) { FILE *fp = NULL; void (*uuencode_f)(FILE *, const char *, const char *) = uuencode; + int ret = 0; ARGBEGIN { case 'm': @@ -122,6 +123,8 @@ main(int argc, char *argv[]) uuencode_f(fp, argv[1], argv[0]); } - return !!((fp && fshut(fp, argv[0])) + fshut(stdin, "<stdin>") + - fshut(stdout, "<stdout>")); + ret |= fp && fshut(fp, argv[0]); + ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); + + return ret; } diff --git a/wc.c b/wc.c @@ -102,5 +102,7 @@ main(int argc, char *argv[]) output("total", tc, tl, tw); } - return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret; + ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); + + return ret; } diff --git a/xargs.c b/xargs.c @@ -194,7 +194,7 @@ usage(void) int main(int argc, char *argv[]) { - int leftover = 0, i; + int ret = 0, leftover = 0, i; size_t argsz, argmaxsz; size_t arglen, a; char *arg = ""; @@ -272,6 +272,8 @@ main(int argc, char *argv[]) free(argb); - return (nerrors || !!(fshut(stdin, "<stdin>") + - fshut(stdout, "<stdout>"))) ? 123 : 0; + if (nerrors || (fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"))) + ret = 123; + + return ret; }