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.
This commit is contained in:
FRIGN 2015-05-25 01:33:19 +02:00 committed by sin
parent e8a4f37884
commit d23cc72490
34 changed files with 142 additions and 72 deletions

4
cat.c
View File

@ -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;
} }

View File

@ -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;
} }

14
cmp.c
View File

@ -16,7 +16,7 @@ main(int argc, char *argv[])
{ {
FILE *fp[2]; FILE *fp[2];
size_t line = 1, n; 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 { ARGBEGIN {
case 'l': case 'l':
@ -72,9 +72,11 @@ main(int argc, char *argv[])
} }
} }
enfshut(2, fp[0], argv[0]); if (!ret)
if (fp[0] != fp[1]) ret = !same;
enfshut(2, fp[1], argv[1]); if (fshut(fp[0], argv[0]) | (fp[0] != fp[1] && fshut(fp[1], argv[1])) |
enfshut(2, stdout, "<stdout>"); fshut(stdout, "<stdout>"))
return !same; ret = 2;
return ret;
} }

6
col.c
View File

@ -190,6 +190,8 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int ret = 0;
ARGBEGIN { ARGBEGIN {
case 'b': case 'b':
backspace = 1; backspace = 1;
@ -217,5 +219,7 @@ main(int argc, char *argv[])
col(); col();
flush(); flush();
return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
return ret;
} }

4
cols.c
View File

@ -87,5 +87,7 @@ main(int argc, char *argv[])
putchar('\n'); putchar('\n');
} }
return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret; ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
return ret;
} }

9
comm.c
View File

@ -33,7 +33,7 @@ main(int argc, char *argv[])
{ {
FILE *fp[2]; FILE *fp[2];
size_t linelen[2] = { 0, 0 }; size_t linelen[2] = { 0, 0 };
int i, diff = 0; int ret = 0, i, diff = 0;
char *line[2] = { NULL, NULL }; char *line[2] = { NULL, NULL };
ARGBEGIN { ARGBEGIN {
@ -79,6 +79,9 @@ main(int argc, char *argv[])
printline((2 - diff) % 3, line[MAX(0, diff)]); printline((2 - diff) % 3, line[MAX(0, diff)]);
} }
end: end:
return !!(fshut(fp[0], argv[0]) + (fp[0] != fp[1] && fshut(fp[1], argv[1])) + ret |= fshut(fp[0], argv[0]);
fshut(stdout, "<stdout>")); ret |= (fp[0] != fp[1]) && fshut(fp[1], argv[1]);
ret |= fshut(stdout, "<stdout>");
return ret;
} }

4
cut.c
View File

@ -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;
} }

View File

@ -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;
} }

5
expr.c
View File

@ -258,8 +258,11 @@ main(int argc, char *argv[])
int ret; int ret;
argv0 = argv[0], argc--, argv++; argv0 = argv[0], argc--, argv++;
ret = !parse(argv, argc); ret = !parse(argv, argc);
enfshut(3, stdout, "<stdout>");
if (fshut(stdout, "<stdout>"))
ret = 3;
return ret; return ret;
} }

4
find.c
View File

@ -1036,5 +1036,7 @@ main(int argc, char **argv)
t->u.pinfo->freearg(t->extra); t->u.pinfo->freearg(t->extra);
free(toks); free(toks);
return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || gflags.ret; gflags.ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
return gflags.ret;
} }

4
fold.c
View File

@ -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;
} }

5
grep.c
View File

@ -277,7 +277,8 @@ main(int argc, char *argv[])
} }
} }
enfshut(Error, stdin, "<stdin>"); if (fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"))
enfshut(Error, stdout, "<stdout>"); match = Error;
return match; return match;
} }

4
head.c
View File

@ -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;
} }

12
join.c
View File

@ -477,7 +477,7 @@ main(int argc, char *argv[])
{ {
size_t jf[2] = { jfield, jfield, }; size_t jf[2] = { jfield, jfield, };
FILE *fp[2]; FILE *fp[2];
int n; int ret = 0, n;
char *fno; char *fno;
ARGBEGIN { ARGBEGIN {
@ -544,9 +544,9 @@ main(int argc, char *argv[])
if (oflag) if (oflag)
freespecs(&output); freespecs(&output);
enfshut(2, fp[0], argv[0]); if (fshut(fp[0], argv[0]) | (fp[0] != fp[1] && fshut(fp[1], argv[1])) |
if (fp[0] != fp[1]) fshut(stdout, "<stdout>"))
enfshut(2, fp[1], argv[1]); ret = 2;
enfshut(2, stdout, "<stdout>");
return 0; return ret;
} }

View File

@ -24,7 +24,7 @@ usage(void)
int int
main(int argc, char *argv[]) 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]; uint8_t md[MD5_DIGEST_LENGTH];
ARGBEGIN { ARGBEGIN {
@ -35,6 +35,8 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
return cryptfunc(argc, argv, &md5_ops, md, sizeof(md)) || ret |= cryptfunc(argc, argv, &md5_ops, md, sizeof(md));
!!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
return ret;
} }

7
nl.c
View File

@ -113,6 +113,7 @@ main(int argc, char *argv[])
{ {
FILE *fp = NULL; FILE *fp = NULL;
size_t l, s; size_t l, s;
int ret = 0;
char *d, *formattype, *formatblit; char *d, *formattype, *formatblit;
ARGBEGIN { ARGBEGIN {
@ -199,6 +200,8 @@ main(int argc, char *argv[])
nl(argv[0], fp); nl(argv[0], fp);
} }
return !!((fp && fp != stdin && fshut(fp, argv[0])) ret |= fp && fp != stdin && fshut(fp, argv[0]);
+ fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
return ret;
} }

View File

@ -133,7 +133,9 @@ main(int argc, char *argv[])
for (i = 0; i < argc; i++) for (i = 0; i < argc; i++)
if (dsc[i].fp != stdin && fshut(dsc[i].fp, argv[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;
} }

6
sed.c
View File

@ -1682,7 +1682,7 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char *arg; char *arg;
int script = 0; int ret = 0, script = 0;
ARGBEGIN { ARGBEGIN {
case 'n': case 'n':
@ -1717,5 +1717,7 @@ main(int argc, char *argv[])
files = argv; files = argv;
run(); run();
return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
return ret;
} }

View File

@ -23,7 +23,7 @@ usage(void)
int int
main(int argc, char *argv[]) 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]; uint8_t md[SHA1_DIGEST_LENGTH];
ARGBEGIN { ARGBEGIN {
@ -34,6 +34,8 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
return cryptfunc(argc, argv, &sha1_ops, md, sizeof(md)) || ret |= cryptfunc(argc, argv, &sha1_ops, md, sizeof(md));
!!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
return ret;
} }

View File

@ -23,7 +23,7 @@ usage(void)
int int
main(int argc, char *argv[]) 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]; uint8_t md[SHA256_DIGEST_LENGTH];
ARGBEGIN { ARGBEGIN {
@ -34,6 +34,8 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
return cryptfunc(argc, argv, &sha256_ops, md, sizeof(md)) || ret |= cryptfunc(argc, argv, &sha256_ops, md, sizeof(md));
!!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
return ret;
} }

View File

@ -23,7 +23,7 @@ usage(void)
int int
main(int argc, char *argv[]) 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]; uint8_t md[SHA512_DIGEST_LENGTH];
ARGBEGIN { ARGBEGIN {
@ -34,6 +34,8 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
return cryptfunc(argc, argv, &sha512_ops, md, sizeof(md)) || ret |= cryptfunc(argc, argv, &sha512_ops, md, sizeof(md));
!!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
return ret;
} }

5
sort.c
View File

@ -332,7 +332,8 @@ main(int argc, char *argv[])
} }
} }
enfshut(2, stdin, "<stdin>"); if (fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"))
enfshut(2, stdout, "<stdout>"); ret = 2;
return ret; return ret;
} }

View File

@ -50,7 +50,7 @@ main(int argc, char *argv[])
FILE *in = stdin, *out = NULL; FILE *in = stdin, *out = NULL;
size_t size = 1000, scale = 1, n; size_t size = 1000, scale = 1, n;
long l; 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; char name[NAME_MAX + 1], *prefix = "x", *file = NULL, *tmp, *end;
ARGBEGIN { ARGBEGIN {
@ -124,6 +124,9 @@ main(int argc, char *argv[])
putc(ch, out); putc(ch, out);
} }
return !!(fshut(in, "<infile>") + (out && fshut(out, "<outfile>")) + ret |= (in != stdin) && fshut(in, "<infile>");
fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); ret |= out && (out != stdout) && fshut(out, "<outfile>");
ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
return ret;
} }

View File

@ -14,6 +14,7 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
FILE *fp, *tmpfp; FILE *fp, *tmpfp;
int ret = 0;
argv0 = argv[0], argc--, argv++; argv0 = argv[0], argc--, argv++;
@ -29,5 +30,7 @@ main(int argc, char *argv[])
eprintf("fopen %s:", argv[0]); eprintf("fopen %s:", argv[0]);
concat(tmpfp, "<tmpfile>", fp, 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;
} }

View File

@ -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;
} }

6
tail.c
View File

@ -47,7 +47,7 @@ taketail(FILE *fp, const char *str, size_t n)
ring = ecalloc(n, sizeof(*ring)); ring = ecalloc(n, sizeof(*ring));
size = ecalloc(n, sizeof(*size)); 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; i = j = (i + 1) % n;
} else { } else {
r = ecalloc(n, sizeof(*r)); 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;
} }

6
tee.c
View File

@ -15,7 +15,7 @@ main(int argc, char *argv[])
{ {
FILE **fps = NULL; FILE **fps = NULL;
size_t i, n, nfps; size_t i, n, nfps;
int aflag = 0, iflag = 0; int ret = 0, aflag = 0, iflag = 0;
char buf[BUFSIZ]; char buf[BUFSIZ];
ARGBEGIN { 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;
} }

7
tr.c
View File

@ -174,6 +174,7 @@ main(int argc, char *argv[])
{ {
Rune r = 0, lastrune = 0; Rune r = 0, lastrune = 0;
size_t off1, off2, i, m; size_t off1, off2, i, m;
int ret = 0;
ARGBEGIN { ARGBEGIN {
case 'c': case 'c':
@ -204,8 +205,10 @@ main(int argc, char *argv[])
if (set2check && cflag && !dflag) if (set2check && cflag && !dflag)
eprintf("set2 can't be imaged to from a complement.\n"); eprintf("set2 can't be imaged to from a complement.\n");
read: read:
if (!efgetrune(&r, stdin, "<stdin>")) if (!efgetrune(&r, stdin, "<stdin>")) {
return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
return ret;
}
off1 = off2 = 0; off1 = off2 = 0;
for (i = 0; i < set1ranges; i++) { for (i = 0; i < set1ranges; i++) {
if (set1[i].start <= r && r <= set1[i].end) { if (set1[i].start <= r && r <= set1[i].end) {

View File

@ -168,7 +168,7 @@ main(int argc, char *argv[])
} }
} }
efshut(stdin, "<stdin>"); ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
efshut(stdout, "<stdout>");
return ret; return ret;
} }

8
uniq.c
View File

@ -97,7 +97,7 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
FILE *fp[2] = { stdin, stdout }; FILE *fp[2] = { stdin, stdout };
int i; int ret = 0, i;
char *fname[2] = { "<stdin>", "<stdout>" }; char *fname[2] = { "<stdin>", "<stdout>" };
ARGBEGIN { ARGBEGIN {
@ -134,7 +134,7 @@ main(int argc, char *argv[])
uniq(fp[0], fp[1]); uniq(fp[0], fp[1]);
uniqfinish(fp[1]); uniqfinish(fp[1]);
efshut(fp[0], fname[0]); ret |= fshut(fp[0], fname[0]) | fshut(fp[1], fname[1]);
efshut(fp[1], fname[1]);
return 0; return ret;
} }

View File

@ -228,6 +228,7 @@ main(int argc, char *argv[])
{ {
FILE *fp = NULL, *nfp = NULL; FILE *fp = NULL, *nfp = NULL;
mode_t mode = 0; mode_t mode = 0;
int ret = 0;
char *fname, *header, *ifname, *ofname = NULL; char *fname, *header, *ifname, *ofname = NULL;
void (*d) (FILE *, FILE *) = NULL; void (*d) (FILE *, FILE *) = NULL;
@ -274,6 +275,8 @@ main(int argc, char *argv[])
if (nfp != stdout && chmod(fname, mode) < 0) if (nfp != stdout && chmod(fname, mode) < 0)
eprintf("chmod %s:", fname); eprintf("chmod %s:", fname);
return !!(fshut(fp, fp == stdin ? "<stdin>" : argv[0]) + ret |= fshut(fp, (fp == stdin) ? "<stdin>" : argv[0]);
fshut(nfp, nfp == stdout ? "<stdout>" : fname)); ret |= fshut(nfp, (nfp == stdout) ? "<stdout>" : fname);
return ret;
} }

View File

@ -102,6 +102,7 @@ main(int argc, char *argv[])
{ {
FILE *fp = NULL; FILE *fp = NULL;
void (*uuencode_f)(FILE *, const char *, const char *) = uuencode; void (*uuencode_f)(FILE *, const char *, const char *) = uuencode;
int ret = 0;
ARGBEGIN { ARGBEGIN {
case 'm': case 'm':
@ -122,6 +123,8 @@ main(int argc, char *argv[])
uuencode_f(fp, argv[1], argv[0]); uuencode_f(fp, argv[1], argv[0]);
} }
return !!((fp && fshut(fp, argv[0])) + fshut(stdin, "<stdin>") + ret |= fp && fshut(fp, argv[0]);
fshut(stdout, "<stdout>")); ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
return ret;
} }

4
wc.c
View File

@ -102,5 +102,7 @@ main(int argc, char *argv[])
output("total", tc, tl, tw); output("total", tc, tl, tw);
} }
return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret; ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
return ret;
} }

View File

@ -194,7 +194,7 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int leftover = 0, i; int ret = 0, leftover = 0, i;
size_t argsz, argmaxsz; size_t argsz, argmaxsz;
size_t arglen, a; size_t arglen, a;
char *arg = ""; char *arg = "";
@ -272,6 +272,8 @@ main(int argc, char *argv[])
free(argb); free(argb);
return (nerrors || !!(fshut(stdin, "<stdin>") + if (nerrors || (fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>")))
fshut(stdout, "<stdout>"))) ? 123 : 0; ret = 123;
return ret;
} }