sbase

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

commit 11e2d472bf7ef8e6752ae995a4954d9359c11fdd
parent 11d59a78c86b78b4050524ebdd7fc34915040918
Author: FRIGN <dev@frign.de>
Date:   Sat,  4 Apr 2015 21:25:17 +0200

Add *fshut() functions to properly flush file streams

This has been a known issue for a long time. Example:

printf "word" > /dev/full

wouldn't report there's not enough space on the device.
This is due to the fact that every libc has internal buffers
for stdout which store fragments of written data until they reach
a certain size or on some callback to flush them all at once to the
kernel.
You can force the libc to flush them with fflush(). In case flushing
fails, you can check the return value of fflush() and report an error.

However, previously, sbase didn't have such checks and without fflush(),
the libc silently flushes the buffers on exit without checking the errors.
No offense, but there's no way for the libc to report errors in the exit-
condition.

GNU coreutils solve this by having onexit-callbacks to handle the flushing
and report issues, but they have obvious deficiencies.
After long discussions on IRC, we came to the conclusion that checking the
return value of every io-function would be a bit too much, and having a
general-purpose fclose-wrapper would be the best way to go.

It turned out that fclose() alone is not enough to detect errors. The right
way to do it is to fflush() + check ferror on the fp and then to a fclose().
This is what fshut does and that's how it's done before each return.
The return value is obviously affected, reporting an error in case a flush
or close failed, but also when reading failed for some reason, the error-
state is caught.

the !!( ... + ...) construction is used to call all functions inside the
brackets and not "terminating" on the first.
We want errors to be reported, but there's no reason to stop flushing buffers
when one other file buffer has issues.
Obviously, functionales come before the flush and ret-logic comes after to
prevent early exits as well without reporting warnings if there are any.

One more advantage of fshut() is that it is even able to report errors
on obscure NFS-setups which the other coreutils are unable to detect,
because they only check the return-value of fflush() and fclose(),
not ferror() as well.

Diffstat:
MMakefile | 1+
Mbasename.c | 2+-
Mcal.c | 2+-
Mcat.c | 6++++--
Mcksum.c | 6++++--
Mcmp.c | 4++++
Mcol.c | 2+-
Mcols.c | 8+++++---
Mcomm.c | 5+++--
Mconfig.mk | 2+-
Mcp.c | 2+-
Mcut.c | 5+++--
Mdate.c | 2+-
Mdirname.c | 2+-
Mdu.c | 2+-
Mecho.c | 2+-
Menv.c | 2+-
Mexpand.c | 5+++--
Mexpr.c | 1+
Mfind.c | 2+-
Mfold.c | 5+++--
Mgrep.c | 12++++++++----
Mhead.c | 5+++--
Mhostname.c | 2+-
Mkill.c | 4+---
Mlibutil/concat.c | 7++-----
Alibutil/fshut.c | 37+++++++++++++++++++++++++++++++++++++
Mlogger.c | 4+---
Mlogname.c | 2+-
Mls.c | 2+-
Mmd5sum.c | 3++-
Mmktemp.c | 4+++-
Mnl.c | 4++--
Mpaste.c | 23++++++++++++-----------
Mprintenv.c | 2+-
Mprintf.c | 2+-
Mpwd.c | 2+-
Mreadlink.c | 2+-
Msed.c | 13++++++-------
Mseq.c | 2+-
Msha1sum.c | 3++-
Msha256sum.c | 3++-
Msha512sum.c | 3++-
Msort.c | 9++++++---
Msplit.c | 9+++------
Msponge.c | 5+----
Mstrings.c | 5+++--
Mtail.c | 5+++--
Mtar.c | 7+++----
Mtee.c | 4+---
Mtr.c | 2+-
Mtty.c | 2+-
Muname.c | 2+-
Munexpand.c | 3++-
Muniq.c | 5++---
Mutil.h | 5+++++
Muudecode.c | 7++-----
Muuencode.c | 6+++---
Mwc.c | 5+++--
Mxargs.c | 3++-
60 files changed, 175 insertions(+), 118 deletions(-)

diff --git a/Makefile b/Makefile @@ -50,6 +50,7 @@ LIBUTILSRC =\ libutil/eregcomp.c\ libutil/estrtod.c\ libutil/fnck.c\ + libutil/fshut.c\ libutil/getlines.c\ libutil/human.c\ libutil/md5.c\ diff --git a/basename.c b/basename.c @@ -33,5 +33,5 @@ main(int argc, char *argv[]) } puts(p); - return 0; + return fshut(stdout, "<stdout>"); } diff --git a/cal.c b/cal.c @@ -208,5 +208,5 @@ main(int argc, char *argv[]) drawcal(year, month - 1, ncols, nmons, fday); - return 0; + return fshut(stdout, "<stdout>"); } diff --git a/cat.c b/cat.c @@ -36,10 +36,12 @@ main(int argc, char *argv[]) ret = 1; } else { concat(fp, *argv, stdout, "<stdout>"); - fclose(fp); + if (fshut(fp, *argv)) { + ret = 1; + } } } } - return ret; + return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret; } diff --git a/cksum.c b/cksum.c @@ -111,9 +111,11 @@ main(int argc, char *argv[]) continue; } cksum(fp, *argv); - fclose(fp); + if (fshut(fp, *argv)) { + ret = 1; + } } } - return ret; + return fshut(stdout, "<stdout>") || ret; } diff --git a/cmp.c b/cmp.c @@ -71,5 +71,9 @@ 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; } diff --git a/col.c b/col.c @@ -217,5 +217,5 @@ main(int argc, char *argv[]) col(); flush(); - return 0; + return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); } diff --git a/cols.c b/cols.c @@ -25,7 +25,7 @@ main(int argc, char *argv[]) struct winsize w; struct linebuf b = EMPTY_LINEBUF; size_t chars = 65, maxlen = 0, i, j, k, len, bytes, cols, rows; - int cflag = 0; + int cflag = 0, ret = 0; char *p; ARGBEGIN { @@ -50,10 +50,12 @@ main(int argc, char *argv[]) for (; *argv; argc--, argv++) { if (!(fp = fopen(*argv, "r"))) { weprintf("fopen %s:", *argv); + ret = 1; continue; } getlines(fp, &b); - fclose(fp); + if (fshut(fp, *argv)) + ret = 1; } } @@ -82,5 +84,5 @@ main(int argc, char *argv[]) putchar('\n'); } - return 0; + return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret; } diff --git a/comm.c b/comm.c @@ -76,8 +76,9 @@ main(int argc, char *argv[]) } diff = strcmp(line[0], line[1]); LIMIT(diff, -1, 1); - printline((2-diff) % 3, line[MAX(0, diff)]); + printline((2 - diff) % 3, line[MAX(0, diff)]); } end: - return 0; + return !!(fshut(fp[0], argv[0]) + (fp[0] != fp[1] && fshut(fp[1], argv[1])) + + fshut(stdout, "<stdout>")); } diff --git a/config.mk b/config.mk @@ -11,6 +11,6 @@ AR = ar RANLIB = ranlib # For NetBSD add -D_NETBSD_SOURCE -CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 +CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -DDEBUG CFLAGS = -std=c99 -Wall -pedantic LDFLAGS = -s # -lrt diff --git a/cp.c b/cp.c @@ -53,5 +53,5 @@ main(int argc, char *argv[]) } enmasse(argc, argv, cp); - return cp_status; + return fshut(stdout, "<stdout>") || cp_status; } diff --git a/cut.c b/cut.c @@ -196,10 +196,11 @@ main(int argc, char *argv[]) continue; } cut(fp, *argv); - fclose(fp); + if (fshut(fp, *argv)) + ret = 1; } } } - return ret; + return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret; } diff --git a/date.c b/date.c @@ -45,5 +45,5 @@ main(int argc, char *argv[]) strftime(buf, sizeof(buf), fmt, now); puts(buf); - return 0; + return fshut(stdout, "<stdout>"); } diff --git a/dirname.c b/dirname.c @@ -23,5 +23,5 @@ main(int argc, char *argv[]) puts(dirname(argv[0])); - return 0; + return fshut(stdout, "<stdout>"); } diff --git a/du.c b/du.c @@ -108,5 +108,5 @@ main(int argc, char *argv[]) } } - return recurse_status; + return fshut(stdout, "<stdout>") || recurse_status; } diff --git a/echo.c b/echo.c @@ -18,5 +18,5 @@ main(int argc, char *argv[]) if (!nflag) putchar('\n'); - return 0; + return fshut(stdout, "<stdout>"); } diff --git a/env.c b/env.c @@ -45,5 +45,5 @@ main(int argc, char *argv[]) for (; environ && *environ; environ++) puts(*environ); - return 0; + return fshut(stdout, "<stdout>"); } diff --git a/expand.c b/expand.c @@ -116,9 +116,10 @@ main(int argc, char *argv[]) continue; } expand(*argv, fp); - fclose(fp); + if (fshut(fp, *argv)) + ret = 1; } } - return ret; + return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret; } diff --git a/expr.c b/expr.c @@ -266,5 +266,6 @@ main(int argc, char *argv[]) usage(); } ARGEND; + enfshut(3, stdout, "<stdout>"); return !parse(argv, argc); } diff --git a/find.c b/find.c @@ -1036,5 +1036,5 @@ main(int argc, char **argv) t->u.pinfo->freearg(t->extra); free(toks); - return gflags.ret; + return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || gflags.ret; } diff --git a/fold.c b/fold.c @@ -100,10 +100,11 @@ main(int argc, char *argv[]) ret = 1; } else { fold(fp, *argv); - fclose(fp); + if (fshut(fp, *argv)) + ret = 1; } } } - return ret; + return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret; } diff --git a/grep.c b/grep.c @@ -196,7 +196,7 @@ main(int argc, char *argv[]) if (!(fp = fmemopen(arg, strlen(arg) + 1, "r"))) eprintf("fmemopen:"); addpatternfile(fp); - fclose(fp); + efshut(fp, arg); eflag = 1; break; case 'f': @@ -205,7 +205,7 @@ main(int argc, char *argv[]) if (!fp) enprintf(Error, "fopen %s:", arg); addpatternfile(fp); - fclose(fp); + efshut(fp, arg); fflag = 1; break; case 'h': @@ -246,7 +246,7 @@ main(int argc, char *argv[]) if (!(fp = fmemopen(argv[0], strlen(argv[0]) + 1, "r"))) eprintf("fmemopen:"); addpatternfile(fp); - fclose(fp); + efshut(fp, argv[0]); argc--; argv++; } @@ -269,8 +269,12 @@ main(int argc, char *argv[]) m = grep(fp, argv[i]); if (m == Error || (match != Error && m == Match)) match = m; - fclose(fp); + if (fshut(fp, argv[i])) + match = Error; } } + + enfshut(Error, stdin, "<stdin>"); + enfshut(Error, stdout, "<stdout>"); return match; } diff --git a/head.c b/head.c @@ -62,9 +62,10 @@ main(int argc, char *argv[]) } newline = 1; head(fp, *argv, n); - fclose(fp); + if(fshut(fp, *argv)) + ret = 1; } } - return ret; + return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret; } diff --git a/hostname.c b/hostname.c @@ -32,5 +32,5 @@ main(int argc, char *argv[]) usage(); } - return 0; + return fshut(stdout, "<stdout>"); } diff --git a/kill.c b/kill.c @@ -74,17 +74,15 @@ main(int argc, char *argv[]) if (!argc) { for (i = 0; i < LEN(sigs); i++) puts(sigs[i].name); - return 0; } else if (argc == 1) { sig = estrtonum(*argv, 0, INT_MAX); if (sig > 128) sig = WTERMSIG(sig); puts(sig2name(sig)); - return 0; } else { usage(); } - break; + return fshut(stdout, "<stdout>"); case 's': if ((*argv)[2]) goto longopt; diff --git a/libutil/concat.c b/libutil/concat.c @@ -12,11 +12,8 @@ concat(FILE *fp1, const char *s1, FILE *fp2, const char *s2) while ((n = fread(buf, 1, sizeof(buf), fp1))) { fwrite(buf, 1, n, fp2); - if (ferror(fp2)) - eprintf("fwrite %s:", s2); - if (feof(fp1) || ferror(fp1)) + + if (feof(fp1) || ferror(fp1) || ferror(fp2)) break; } - if (ferror(fp1)) - eprintf("fread %s", s1); } diff --git a/libutil/fshut.c b/libutil/fshut.c @@ -0,0 +1,37 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "../util.h" + +int fshut(FILE *fp, const char *fname) +{ + int ret = 0; + + if (fflush(fp) && !ret) { + weprintf("fflush %s:", fname); + ret = 1; + } + + if (ferror(fp) && !ret) { + weprintf("ferror %s:", fname); + ret = 1; + } + + if (fclose(fp) && !ret) { + weprintf("fclose %s:", fname); + ret = 1; + } + + return ret; +} + +void enfshut(int status, FILE *fp, const char *fname) +{ + if (fshut(fp, fname)) + exit(status); +} + +void efshut(FILE *fp, const char *fname) +{ + enfshut(1, fp, fname); +} diff --git a/logger.c b/logger.c @@ -72,8 +72,6 @@ main(int argc, char *argv[]) if (!argc) { while (getline(&buf, &sz, stdin) > 0) syslog(priority, "%s", buf); - if (ferror(stdin)) - eprintf("getline %s:", "<stdin>"); } else { for (i = 0, sz = 0; i < argc; i++) sz += strlen(argv[i]); @@ -89,5 +87,5 @@ main(int argc, char *argv[]) closelog(); - return 0; + return fshut(stdin, "<stdin>"); } diff --git a/logname.c b/logname.c @@ -25,5 +25,5 @@ main(int argc, char *argv[]) else eprintf("no login name\n"); - return 0; + return fshut(stdout, "<stdout>"); } diff --git a/ls.c b/ls.c @@ -365,5 +365,5 @@ main(int argc, char *argv[]) for (i = 0; i < argc; i++) ls(&ents[rflag ? argc-i-1 : i], 1); - return 0; + return fshut(stdout, "<stdout>"); } diff --git a/md5sum.c b/md5sum.c @@ -35,5 +35,6 @@ main(int argc, char *argv[]) usage(); } ARGEND; - return cryptfunc(argc, argv, &md5_ops, md, sizeof(md)); + return cryptfunc(argc, argv, &md5_ops, md, sizeof(md)) || + !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); } diff --git a/mktemp.c b/mktemp.c @@ -61,9 +61,11 @@ main(int argc, char *argv[]) eprintf("mkstemp %s:", path); return 1; } - close(fd); + if (close(fd)) + eprintf("close %s:", path); } puts(path); + efshut(stdout, "<stdout>"); return 0; } diff --git a/nl.c b/nl.c @@ -193,8 +193,8 @@ main(int argc, char *argv[]) if (!(fp = fopen(argv[0], "r"))) eprintf("fopen %s:", argv[0]); nl(argv[0], fp); - fclose(fp); } - return 0; + return !!(fshut(fp, argv[0]) + fshut(stdin, "<stdin>") + + fshut(stdout, "<stdout>")); } diff --git a/paste.c b/paste.c @@ -86,10 +86,10 @@ int main(int argc, char *argv[]) { struct fdescr *dsc; - Rune *delim; + Rune *delim; size_t i, len; - int seq = 0; - char *adelim = "\t"; + int seq = 0, ret = 0; + char *adelim = "\t"; ARGBEGIN { case 's': @@ -116,22 +116,23 @@ main(int argc, char *argv[]) dsc = ereallocarray(NULL, argc, sizeof(*dsc)); for (i = 0; i < argc; i++) { - if (!strcmp(argv[i], "-")) + if (!strcmp(argv[i], "-")) { dsc[i].fp = stdin; - else - dsc[i].fp = fopen(argv[i], "r"); - if (!dsc[i].fp) + } else if (!(dsc[i].fp = fopen(argv[i], "r"))) { eprintf("fopen %s:", argv[i]); + } dsc[i].name = argv[i]; } - if (seq) + if (seq) { sequential(dsc, argc, delim, len); - else + } else { parallel(dsc, argc, delim, len); + } for (i = 0; i < argc; i++) - fclose(dsc[i].fp); + if (dsc[i].fp != stdin && fshut(dsc[i].fp, argv[i])) + ret = 1; - return 0; + return fshut(stdin, "<stdin>") || fshut(stdout, "<stdout>") || ret; } diff --git a/printenv.c b/printenv.c @@ -35,5 +35,5 @@ main(int argc, char *argv[]) } } - return ret; + return fshut(stdout, "<stdout>") || ret; } diff --git a/printf.c b/printf.c @@ -152,5 +152,5 @@ main(int argc, char *argv[]) cooldown = 1; } - return 0; + return fshut(stdout, "<stdout>"); } diff --git a/pwd.c b/pwd.c @@ -46,5 +46,5 @@ main(int argc, char *argv[]) eprintf("getcwd:"); puts((mode == 'L') ? getpwd(cwd) : cwd); - return 0; + return fshut(stdout, "<stdout>"); } diff --git a/readlink.c b/readlink.c @@ -91,5 +91,5 @@ mdone: if (!nflag) putchar('\n'); - return 0; + return fshut(stdout, "<stdout>"); } diff --git a/sed.c b/sed.c @@ -433,8 +433,7 @@ compile(char *s, int isfile) } } - if (fclose(f)) - weprintf("fclose:"); + fshut(f, s); } /* FIXME: if we decide to honor lack of trailing newline, set/clear a global @@ -1119,8 +1118,8 @@ next_file(void) if (file == stdin) clearerr(file); - else if (file && fclose(file)) - weprintf("fclose:"); + if (file) + fshut(file, "<file>"); file = NULL; do { @@ -1188,8 +1187,7 @@ write_file(char *path, FILE *out) while (read_line(in, &genbuf) != EOF) check_puts(genbuf.str, out); - if (fclose(in)) - weprintf("fclose:"); + fshut(in, path); } static void @@ -1730,5 +1728,6 @@ main(int argc, char *argv[]) files = argv; run(); - return 0; + + return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); } diff --git a/seq.c b/seq.c @@ -143,5 +143,5 @@ main(int argc, char *argv[]) } putchar('\n'); - return 0; + return fshut(stdout, "<stdout>"); } diff --git a/sha1sum.c b/sha1sum.c @@ -34,5 +34,6 @@ main(int argc, char *argv[]) usage(); } ARGEND; - return cryptfunc(argc, argv, &sha1_ops, md, sizeof(md)); + return cryptfunc(argc, argv, &sha1_ops, md, sizeof(md)) || + !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); } diff --git a/sha256sum.c b/sha256sum.c @@ -34,5 +34,6 @@ main(int argc, char *argv[]) usage(); } ARGEND; - return cryptfunc(argc, argv, &sha256_ops, md, sizeof(md)); + return cryptfunc(argc, argv, &sha256_ops, md, sizeof(md)) || + !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); } diff --git a/sha512sum.c b/sha512sum.c @@ -34,5 +34,6 @@ main(int argc, char *argv[]) usage(); } ARGEND; - return cryptfunc(argc, argv, &sha512_ops, md, sizeof(md)); + return cryptfunc(argc, argv, &sha512_ops, md, sizeof(md)) || + !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); } diff --git a/sort.c b/sort.c @@ -236,7 +236,7 @@ main(int argc, char *argv[]) FILE *fp, *ofp = stdout; struct linebuf linebuf = EMPTY_LINEBUF; size_t i; - int global_flags = 0; + int global_flags = 0, ret = 0; char *outfile = NULL; ARGBEGIN { @@ -300,7 +300,8 @@ main(int argc, char *argv[]) } else { getlines(fp, &linebuf); } - fclose(fp); + if (fshut(fp, *argv)) + ret = 1; } if (!Cflag && !cflag) { @@ -318,5 +319,7 @@ main(int argc, char *argv[]) } } - return 0; + enfshut(2, stdin, "<stdin>"); + enfshut(2, stdout, "<stdout>"); + return ret; } diff --git a/split.c b/split.c @@ -27,7 +27,7 @@ nextfile(FILE *f, char *buf, int plen, int slen) static int filecount = 0; if (f) - fclose(f); + fshut(f, "<file>"); if (itostr(buf + plen, filecount++, slen) < 0) return NULL; @@ -124,9 +124,6 @@ main(int argc, char *argv[]) putc(ch, out); } - fclose(in); - if (out) - fclose(out); - - return 0; + return !!(fshut(in, "<infile>") + (out && fshut(out, "<outfile>")) + + fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); } diff --git a/sponge.c b/sponge.c @@ -32,8 +32,5 @@ main(int argc, char *argv[]) eprintf("fopen %s:", argv[0]); concat(tmpfp, "<tmpfile>", fp, argv[0]); - fclose(fp); - fclose(tmpfp); - - return 0; + return !!(fshut(fp, argv[0]) + fshut(tmpfp, "<tmpfile>")); } diff --git a/strings.c b/strings.c @@ -85,9 +85,10 @@ main(int argc, char *argv[]) continue; } strings(fp, *argv, len); - fclose(fp); + if (fshut(fp, *argv)) + ret = 1; } } - return ret; + return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret; } diff --git a/tail.c b/tail.c @@ -130,7 +130,8 @@ main(int argc, char *argv[]) tail(fp, *argv); if (!fflag) { - fclose(fp); + if (fshut(fp, *argv)) + ret = 1; continue; } for (tmp = NULL, tmpsize = 0;;) { @@ -155,5 +156,5 @@ main(int argc, char *argv[]) } } - return ret; + return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret; } diff --git a/tar.c b/tar.c @@ -166,8 +166,7 @@ archive(const char *path) if (fwrite(b, BLKSIZ, 1, tarfile) != 1) eprintf("fwrite:"); } - if (fclose(f) == EOF) - eprintf("fclose %s:", path); + efshut(f, path); } return 0; @@ -246,8 +245,8 @@ unarchive(char *fname, ssize_t l, char b[BLKSIZ]) if (f && fwrite(b, MIN(l, BLKSIZ), 1, f) != 1) eprintf("fwrite %s:", fname); } - if (f && fclose(f) == EOF) - eprintf("fclose %s:", fname); + if (f) + fshut(f, fname); if (!mflag) { times[0].tv_sec = times[1].tv_sec = mtime; diff --git a/tee.c b/tee.c @@ -46,8 +46,6 @@ main(int argc, char *argv[]) eprintf("fwrite %s:", (i != argc) ? argv[i] : "<stdout>"); } } - if (ferror(stdin)) - eprintf("fread <stdin>:"); - return 0; + return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); } diff --git a/tr.c b/tr.c @@ -204,7 +204,7 @@ main(int argc, char *argv[]) eprintf("set2 can't be imaged to from a complement.\n"); read: if (!efgetrune(&r, stdin, "<stdin>")) - return 0; + return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")); off1 = off2 = 0; for (i = 0; i < set1ranges; i++) { if (set1[i].start <= r && r <= set1[i].end) { diff --git a/tty.c b/tty.c @@ -26,5 +26,5 @@ main(int argc, char *argv[]) tty = ttyname(STDIN_FILENO); puts(tty ? tty : "not a tty"); - return !tty; + return fshut(stdout, "<stdout>") || !tty; } diff --git a/uname.c b/uname.c @@ -55,5 +55,5 @@ main(int argc, char *argv[]) putword(u.machine); putchar('\n'); - return 0; + return fshut(stdout, "<stdout>"); } diff --git a/unexpand.c b/unexpand.c @@ -159,7 +159,8 @@ main(int argc, char *argv[]) continue; } unexpand(*argv, fp); - fclose(fp); + if (fshut(fp, *argv)) + ret = 1; } } diff --git a/uniq.c b/uniq.c @@ -131,10 +131,9 @@ main(int argc, char *argv[]) eprintf("fopen %s:", argv[1]); } uniq(fp, ofp); - fclose(fp); } uniqfinish(ofp); - fclose(ofp); - return 0; + return !!(fshut(fp, fp == stdin ? "<stdin>" : argv[0]) + + fshut(ofp, ofp == stdout ? "<stdout>" : argv[1])); } diff --git a/util.h b/util.h @@ -3,6 +3,7 @@ #include <regex.h> #include <stddef.h> +#include <stdio.h> #include "arg.h" #include "compat.h" @@ -34,6 +35,10 @@ void *enrealloc(int, void *, size_t); char *enstrdup(int, const char *); char *enstrndup(int, const char *, size_t); +void enfshut(int, FILE *, const char *); +void efshut(FILE *, const char *); +int fshut(FILE *, const char *); + void enprintf(int, const char *, ...); void eprintf(const char *, ...); void weprintf(const char *, ...); diff --git a/uudecode.c b/uudecode.c @@ -273,10 +273,7 @@ main(int argc, char *argv[]) if (nfp != stdout && chmod(fname, mode) < 0) eprintf("chmod %s:", fname); - if (fp) - fclose(fp); - if (nfp) - fclose(nfp); - return 0; + return !!(fshut(fp, fp == stdin ? "<stdin>" : argv[0]) + + fshut(nfp, nfp == stdout ? "<stdout>" : fname)); } diff --git a/uuencode.c b/uuencode.c @@ -101,7 +101,7 @@ usage(void) int main(int argc, char *argv[]) { - FILE *fp; + FILE *fp = NULL; ARGBEGIN { case 'm': @@ -126,8 +126,8 @@ main(int argc, char *argv[]) uuencodeb64(fp, argv[1], argv[0]); else uuencode(fp, argv[1], argv[0]); - fclose(fp); } - return 0; + return !!((fp && fshut(fp, argv[0])) + fshut(stdin, "<stdin>") + + fshut(stdout, "<stdout>")); } diff --git a/wc.c b/wc.c @@ -91,11 +91,12 @@ main(int argc, char *argv[]) continue; } wc(fp, *argv); - fclose(fp); + if (fshut(fp, *argv)) + ret = 1; } if (many) output("total", tc, tl, tw); } - return ret; + return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret; } diff --git a/xargs.c b/xargs.c @@ -269,5 +269,6 @@ main(int argc, char *argv[]) free(argb); - return nerrors ? 123 : 0; + return (nerrors || !!(fshut(stdin, "<stdin>") + + fshut(stdout, "<stdout>"))) ? 123 : 0; }