[hackers] [sbase] Simplify return & fshut() logic || FRIGN
commit d23cc72490f42351a20cd12202d94d6e2d65c1f9
Author: FRIGN <dev_AT_frign.de>
Date: Mon May 25 01:33:19 2015 +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.
diff --git a/cat.c b/cat.c
index fb569f1..8ba02a2 100644
--- a/cat.c
+++ b/cat.c
_AT_@ -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
index 9a0d195..3355b4c 100644
--- a/cksum.c
+++ b/cksum.c
_AT_@ -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
index c04fce6..980c7ff 100644
--- a/cmp.c
+++ b/cmp.c
_AT_@ -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':
_AT_@ -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
index 358d4a8..f7d4885 100644
--- a/col.c
+++ b/col.c
_AT_@ -190,6 +190,8 @@ usage(void)
int
main(int argc, char *argv[])
{
+ int ret = 0;
+
ARGBEGIN {
case 'b':
backspace = 1;
_AT_@ -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
index 6f43e35..981b36c 100644
--- a/cols.c
+++ b/cols.c
_AT_@ -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
index 661aed2..e0ec7b7 100644
--- a/comm.c
+++ b/comm.c
_AT_@ -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 {
_AT_@ -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
index 33eecd0..0d3375e 100644
--- a/cut.c
+++ b/cut.c
_AT_@ -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
index 985dc96..f0e6298 100644
--- a/expand.c
+++ b/expand.c
_AT_@ -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
index 608d9a8..4570b33 100644
--- a/expr.c
+++ b/expr.c
_AT_@ -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
index cb437ba..dcefca5 100644
--- a/find.c
+++ b/find.c
_AT_@ -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
index 8019125..ce59488 100644
--- a/fold.c
+++ b/fold.c
_AT_@ -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
index 5836eaa..6e8daef 100644
--- a/grep.c
+++ b/grep.c
_AT_@ -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
index bb6b180..24cd1f9 100644
--- a/head.c
+++ b/head.c
_AT_@ -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
index 572bed0..b2fd07e 100644
--- a/join.c
+++ b/join.c
_AT_@ -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 {
_AT_@ -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
index 7aeb9e1..ade4c97 100644
--- a/md5sum.c
+++ b/md5sum.c
_AT_@ -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 {
_AT_@ -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
index 3685221..aa566ec 100644
--- a/nl.c
+++ b/nl.c
_AT_@ -113,6 +113,7 @@ main(int argc, char *argv[])
{
FILE *fp = NULL;
size_t l, s;
+ int ret = 0;
char *d, *formattype, *formatblit;
ARGBEGIN {
_AT_@ -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
index 417d93c..7f7fb81 100644
--- a/paste.c
+++ b/paste.c
_AT_@ -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
index 586cecb..b8e4be2 100644
--- a/sed.c
+++ b/sed.c
_AT_@ -1682,7 +1682,7 @@ int
main(int argc, char *argv[])
{
char *arg;
- int script = 0;
+ int ret = 0, script = 0;
ARGBEGIN {
case 'n':
_AT_@ -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
index 6a564e5..9743ee9 100644
--- a/sha1sum.c
+++ b/sha1sum.c
_AT_@ -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 {
_AT_@ -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
index 3c898c7..57fc977 100644
--- a/sha256sum.c
+++ b/sha256sum.c
_AT_@ -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 {
_AT_@ -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
index 7dd547b..3b7594a 100644
--- a/sha512sum.c
+++ b/sha512sum.c
_AT_@ -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 {
_AT_@ -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
index f98a428..c3bab4f 100644
--- a/sort.c
+++ b/sort.c
_AT_@ -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
index c4c777d..c5bd82d 100644
--- a/split.c
+++ b/split.c
_AT_@ -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 {
_AT_@ -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
index a2861a0..baeac7f 100644
--- a/sponge.c
+++ b/sponge.c
_AT_@ -14,6 +14,7 @@ int
main(int argc, char *argv[])
{
FILE *fp, *tmpfp;
+ int ret = 0;
argv0 = argv[0], argc--, argv++;
_AT_@ -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
index 0a14c20..a3d722f 100644
--- a/strings.c
+++ b/strings.c
_AT_@ -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
index 0667877..306bbc1 100644
--- a/tail.c
+++ b/tail.c
_AT_@ -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));
_AT_@ -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
index c0ad6f2..260b964 100644
--- a/tee.c
+++ b/tee.c
_AT_@ -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 {
_AT_@ -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
index ad5c690..cc35a57 100644
--- a/tr.c
+++ b/tr.c
_AT_@ -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':
_AT_@ -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
index c107dbf..56aa5e2 100644
--- a/unexpand.c
+++ b/unexpand.c
_AT_@ -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
index 1b30c1a..0476d34 100644
--- a/uniq.c
+++ b/uniq.c
_AT_@ -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 {
_AT_@ -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
index 7140db7..41636ce 100644
--- a/uudecode.c
+++ b/uudecode.c
_AT_@ -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;
_AT_@ -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
index b4332e4..c560f92 100644
--- a/uuencode.c
+++ b/uuencode.c
_AT_@ -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':
_AT_@ -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
index 8870761..6b8631a 100644
--- a/wc.c
+++ b/wc.c
_AT_@ -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
index f64400c..34c82be 100644
--- a/xargs.c
+++ b/xargs.c
_AT_@ -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 = "";
_AT_@ -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;
}
Received on Tue May 26 2015 - 17:41:51 CEST
This archive was generated by hypermail 2.3.0
: Tue May 26 2015 - 17:48:10 CEST