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.
This commit is contained in:
FRIGN 2015-04-04 21:25:17 +02:00 committed by sin
parent 11d59a78c8
commit 11e2d472bf
60 changed files with 175 additions and 118 deletions

View File

@ -50,6 +50,7 @@ LIBUTILSRC =\
libutil/eregcomp.c\ libutil/eregcomp.c\
libutil/estrtod.c\ libutil/estrtod.c\
libutil/fnck.c\ libutil/fnck.c\
libutil/fshut.c\
libutil/getlines.c\ libutil/getlines.c\
libutil/human.c\ libutil/human.c\
libutil/md5.c\ libutil/md5.c\

View File

@ -33,5 +33,5 @@ main(int argc, char *argv[])
} }
puts(p); puts(p);
return 0; return fshut(stdout, "<stdout>");
} }

2
cal.c
View File

@ -208,5 +208,5 @@ main(int argc, char *argv[])
drawcal(year, month - 1, ncols, nmons, fday); drawcal(year, month - 1, ncols, nmons, fday);
return 0; return fshut(stdout, "<stdout>");
} }

6
cat.c
View File

@ -36,10 +36,12 @@ main(int argc, char *argv[])
ret = 1; ret = 1;
} else { } else {
concat(fp, *argv, stdout, "<stdout>"); concat(fp, *argv, stdout, "<stdout>");
fclose(fp); if (fshut(fp, *argv)) {
ret = 1;
}
} }
} }
} }
return ret; return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret;
} }

View File

@ -111,9 +111,11 @@ main(int argc, char *argv[])
continue; continue;
} }
cksum(fp, *argv); cksum(fp, *argv);
fclose(fp); if (fshut(fp, *argv)) {
ret = 1;
}
} }
} }
return ret; return fshut(stdout, "<stdout>") || ret;
} }

4
cmp.c
View File

@ -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; return !same;
} }

2
col.c
View File

@ -217,5 +217,5 @@ main(int argc, char *argv[])
col(); col();
flush(); flush();
return 0; return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>"));
} }

8
cols.c
View File

@ -25,7 +25,7 @@ main(int argc, char *argv[])
struct winsize w; struct winsize w;
struct linebuf b = EMPTY_LINEBUF; struct linebuf b = EMPTY_LINEBUF;
size_t chars = 65, maxlen = 0, i, j, k, len, bytes, cols, rows; size_t chars = 65, maxlen = 0, i, j, k, len, bytes, cols, rows;
int cflag = 0; int cflag = 0, ret = 0;
char *p; char *p;
ARGBEGIN { ARGBEGIN {
@ -50,10 +50,12 @@ main(int argc, char *argv[])
for (; *argv; argc--, argv++) { for (; *argv; argc--, argv++) {
if (!(fp = fopen(*argv, "r"))) { if (!(fp = fopen(*argv, "r"))) {
weprintf("fopen %s:", *argv); weprintf("fopen %s:", *argv);
ret = 1;
continue; continue;
} }
getlines(fp, &b); getlines(fp, &b);
fclose(fp); if (fshut(fp, *argv))
ret = 1;
} }
} }
@ -82,5 +84,5 @@ main(int argc, char *argv[])
putchar('\n'); putchar('\n');
} }
return 0; return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret;
} }

3
comm.c
View File

@ -79,5 +79,6 @@ main(int argc, char *argv[])
printline((2 - diff) % 3, line[MAX(0, diff)]); printline((2 - diff) % 3, line[MAX(0, diff)]);
} }
end: end:
return 0; return !!(fshut(fp[0], argv[0]) + (fp[0] != fp[1] && fshut(fp[1], argv[1])) +
fshut(stdout, "<stdout>"));
} }

View File

@ -11,6 +11,6 @@ AR = ar
RANLIB = ranlib RANLIB = ranlib
# For NetBSD add -D_NETBSD_SOURCE # 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 CFLAGS = -std=c99 -Wall -pedantic
LDFLAGS = -s # -lrt LDFLAGS = -s # -lrt

2
cp.c
View File

@ -53,5 +53,5 @@ main(int argc, char *argv[])
} }
enmasse(argc, argv, cp); enmasse(argc, argv, cp);
return cp_status; return fshut(stdout, "<stdout>") || cp_status;
} }

5
cut.c
View File

@ -196,10 +196,11 @@ main(int argc, char *argv[])
continue; continue;
} }
cut(fp, *argv); cut(fp, *argv);
fclose(fp); if (fshut(fp, *argv))
ret = 1;
} }
} }
} }
return ret; return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret;
} }

2
date.c
View File

@ -45,5 +45,5 @@ main(int argc, char *argv[])
strftime(buf, sizeof(buf), fmt, now); strftime(buf, sizeof(buf), fmt, now);
puts(buf); puts(buf);
return 0; return fshut(stdout, "<stdout>");
} }

View File

@ -23,5 +23,5 @@ main(int argc, char *argv[])
puts(dirname(argv[0])); puts(dirname(argv[0]));
return 0; return fshut(stdout, "<stdout>");
} }

2
du.c
View File

@ -108,5 +108,5 @@ main(int argc, char *argv[])
} }
} }
return recurse_status; return fshut(stdout, "<stdout>") || recurse_status;
} }

2
echo.c
View File

@ -18,5 +18,5 @@ main(int argc, char *argv[])
if (!nflag) if (!nflag)
putchar('\n'); putchar('\n');
return 0; return fshut(stdout, "<stdout>");
} }

2
env.c
View File

@ -45,5 +45,5 @@ main(int argc, char *argv[])
for (; environ && *environ; environ++) for (; environ && *environ; environ++)
puts(*environ); puts(*environ);
return 0; return fshut(stdout, "<stdout>");
} }

View File

@ -116,9 +116,10 @@ main(int argc, char *argv[])
continue; continue;
} }
expand(*argv, fp); expand(*argv, fp);
fclose(fp); if (fshut(fp, *argv))
ret = 1;
} }
} }
return ret; return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret;
} }

1
expr.c
View File

@ -266,5 +266,6 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
enfshut(3, stdout, "<stdout>");
return !parse(argv, argc); return !parse(argv, argc);
} }

2
find.c
View File

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

5
fold.c
View File

@ -100,10 +100,11 @@ main(int argc, char *argv[])
ret = 1; ret = 1;
} else { } else {
fold(fp, *argv); fold(fp, *argv);
fclose(fp); if (fshut(fp, *argv))
ret = 1;
} }
} }
} }
return ret; return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret;
} }

12
grep.c
View File

@ -196,7 +196,7 @@ main(int argc, char *argv[])
if (!(fp = fmemopen(arg, strlen(arg) + 1, "r"))) if (!(fp = fmemopen(arg, strlen(arg) + 1, "r")))
eprintf("fmemopen:"); eprintf("fmemopen:");
addpatternfile(fp); addpatternfile(fp);
fclose(fp); efshut(fp, arg);
eflag = 1; eflag = 1;
break; break;
case 'f': case 'f':
@ -205,7 +205,7 @@ main(int argc, char *argv[])
if (!fp) if (!fp)
enprintf(Error, "fopen %s:", arg); enprintf(Error, "fopen %s:", arg);
addpatternfile(fp); addpatternfile(fp);
fclose(fp); efshut(fp, arg);
fflag = 1; fflag = 1;
break; break;
case 'h': case 'h':
@ -246,7 +246,7 @@ main(int argc, char *argv[])
if (!(fp = fmemopen(argv[0], strlen(argv[0]) + 1, "r"))) if (!(fp = fmemopen(argv[0], strlen(argv[0]) + 1, "r")))
eprintf("fmemopen:"); eprintf("fmemopen:");
addpatternfile(fp); addpatternfile(fp);
fclose(fp); efshut(fp, argv[0]);
argc--; argc--;
argv++; argv++;
} }
@ -269,8 +269,12 @@ main(int argc, char *argv[])
m = grep(fp, argv[i]); m = grep(fp, argv[i]);
if (m == Error || (match != Error && m == Match)) if (m == Error || (match != Error && m == Match))
match = m; match = m;
fclose(fp); if (fshut(fp, argv[i]))
match = Error;
} }
} }
enfshut(Error, stdin, "<stdin>");
enfshut(Error, stdout, "<stdout>");
return match; return match;
} }

5
head.c
View File

@ -62,9 +62,10 @@ main(int argc, char *argv[])
} }
newline = 1; newline = 1;
head(fp, *argv, n); head(fp, *argv, n);
fclose(fp); if(fshut(fp, *argv))
ret = 1;
} }
} }
return ret; return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret;
} }

View File

@ -32,5 +32,5 @@ main(int argc, char *argv[])
usage(); usage();
} }
return 0; return fshut(stdout, "<stdout>");
} }

4
kill.c
View File

@ -74,17 +74,15 @@ main(int argc, char *argv[])
if (!argc) { if (!argc) {
for (i = 0; i < LEN(sigs); i++) for (i = 0; i < LEN(sigs); i++)
puts(sigs[i].name); puts(sigs[i].name);
return 0;
} else if (argc == 1) { } else if (argc == 1) {
sig = estrtonum(*argv, 0, INT_MAX); sig = estrtonum(*argv, 0, INT_MAX);
if (sig > 128) if (sig > 128)
sig = WTERMSIG(sig); sig = WTERMSIG(sig);
puts(sig2name(sig)); puts(sig2name(sig));
return 0;
} else { } else {
usage(); usage();
} }
break; return fshut(stdout, "<stdout>");
case 's': case 's':
if ((*argv)[2]) if ((*argv)[2])
goto longopt; goto longopt;

View File

@ -12,11 +12,8 @@ concat(FILE *fp1, const char *s1, FILE *fp2, const char *s2)
while ((n = fread(buf, 1, sizeof(buf), fp1))) { while ((n = fread(buf, 1, sizeof(buf), fp1))) {
fwrite(buf, 1, n, fp2); fwrite(buf, 1, n, fp2);
if (ferror(fp2))
eprintf("fwrite %s:", s2); if (feof(fp1) || ferror(fp1) || ferror(fp2))
if (feof(fp1) || ferror(fp1))
break; break;
} }
if (ferror(fp1))
eprintf("fread %s", s1);
} }

37
libutil/fshut.c Normal file
View File

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

View File

@ -72,8 +72,6 @@ main(int argc, char *argv[])
if (!argc) { if (!argc) {
while (getline(&buf, &sz, stdin) > 0) while (getline(&buf, &sz, stdin) > 0)
syslog(priority, "%s", buf); syslog(priority, "%s", buf);
if (ferror(stdin))
eprintf("getline %s:", "<stdin>");
} else { } else {
for (i = 0, sz = 0; i < argc; i++) for (i = 0, sz = 0; i < argc; i++)
sz += strlen(argv[i]); sz += strlen(argv[i]);
@ -89,5 +87,5 @@ main(int argc, char *argv[])
closelog(); closelog();
return 0; return fshut(stdin, "<stdin>");
} }

View File

@ -25,5 +25,5 @@ main(int argc, char *argv[])
else else
eprintf("no login name\n"); eprintf("no login name\n");
return 0; return fshut(stdout, "<stdout>");
} }

2
ls.c
View File

@ -365,5 +365,5 @@ main(int argc, char *argv[])
for (i = 0; i < argc; i++) for (i = 0; i < argc; i++)
ls(&ents[rflag ? argc-i-1 : i], 1); ls(&ents[rflag ? argc-i-1 : i], 1);
return 0; return fshut(stdout, "<stdout>");
} }

View File

@ -35,5 +35,6 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } 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>"));
} }

View File

@ -61,9 +61,11 @@ main(int argc, char *argv[])
eprintf("mkstemp %s:", path); eprintf("mkstemp %s:", path);
return 1; return 1;
} }
close(fd); if (close(fd))
eprintf("close %s:", path);
} }
puts(path); puts(path);
efshut(stdout, "<stdout>");
return 0; return 0;
} }

4
nl.c
View File

@ -193,8 +193,8 @@ main(int argc, char *argv[])
if (!(fp = fopen(argv[0], "r"))) if (!(fp = fopen(argv[0], "r")))
eprintf("fopen %s:", argv[0]); eprintf("fopen %s:", argv[0]);
nl(argv[0], fp); nl(argv[0], fp);
fclose(fp);
} }
return 0; return !!(fshut(fp, argv[0]) + fshut(stdin, "<stdin>") +
fshut(stdout, "<stdout>"));
} }

19
paste.c
View File

@ -88,7 +88,7 @@ main(int argc, char *argv[])
struct fdescr *dsc; struct fdescr *dsc;
Rune *delim; Rune *delim;
size_t i, len; size_t i, len;
int seq = 0; int seq = 0, ret = 0;
char *adelim = "\t"; char *adelim = "\t";
ARGBEGIN { ARGBEGIN {
@ -116,22 +116,23 @@ main(int argc, char *argv[])
dsc = ereallocarray(NULL, argc, sizeof(*dsc)); dsc = ereallocarray(NULL, argc, sizeof(*dsc));
for (i = 0; i < argc; i++) { for (i = 0; i < argc; i++) {
if (!strcmp(argv[i], "-")) if (!strcmp(argv[i], "-")) {
dsc[i].fp = stdin; dsc[i].fp = stdin;
else } else if (!(dsc[i].fp = fopen(argv[i], "r"))) {
dsc[i].fp = fopen(argv[i], "r");
if (!dsc[i].fp)
eprintf("fopen %s:", argv[i]); eprintf("fopen %s:", argv[i]);
}
dsc[i].name = argv[i]; dsc[i].name = argv[i];
} }
if (seq) if (seq) {
sequential(dsc, argc, delim, len); sequential(dsc, argc, delim, len);
else } else {
parallel(dsc, argc, delim, len); parallel(dsc, argc, delim, len);
}
for (i = 0; i < argc; i++) 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;
} }

View File

@ -35,5 +35,5 @@ main(int argc, char *argv[])
} }
} }
return ret; return fshut(stdout, "<stdout>") || ret;
} }

View File

@ -152,5 +152,5 @@ main(int argc, char *argv[])
cooldown = 1; cooldown = 1;
} }
return 0; return fshut(stdout, "<stdout>");
} }

2
pwd.c
View File

@ -46,5 +46,5 @@ main(int argc, char *argv[])
eprintf("getcwd:"); eprintf("getcwd:");
puts((mode == 'L') ? getpwd(cwd) : cwd); puts((mode == 'L') ? getpwd(cwd) : cwd);
return 0; return fshut(stdout, "<stdout>");
} }

View File

@ -91,5 +91,5 @@ mdone:
if (!nflag) if (!nflag)
putchar('\n'); putchar('\n');
return 0; return fshut(stdout, "<stdout>");
} }

13
sed.c
View File

@ -433,8 +433,7 @@ compile(char *s, int isfile)
} }
} }
if (fclose(f)) fshut(f, s);
weprintf("fclose:");
} }
/* FIXME: if we decide to honor lack of trailing newline, set/clear a global /* FIXME: if we decide to honor lack of trailing newline, set/clear a global
@ -1119,8 +1118,8 @@ next_file(void)
if (file == stdin) if (file == stdin)
clearerr(file); clearerr(file);
else if (file && fclose(file)) if (file)
weprintf("fclose:"); fshut(file, "<file>");
file = NULL; file = NULL;
do { do {
@ -1188,8 +1187,7 @@ write_file(char *path, FILE *out)
while (read_line(in, &genbuf) != EOF) while (read_line(in, &genbuf) != EOF)
check_puts(genbuf.str, out); check_puts(genbuf.str, out);
if (fclose(in)) fshut(in, path);
weprintf("fclose:");
} }
static void static void
@ -1730,5 +1728,6 @@ main(int argc, char *argv[])
files = argv; files = argv;
run(); run();
return 0;
return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>"));
} }

2
seq.c
View File

@ -143,5 +143,5 @@ main(int argc, char *argv[])
} }
putchar('\n'); putchar('\n');
return 0; return fshut(stdout, "<stdout>");
} }

View File

@ -34,5 +34,6 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } 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>"));
} }

View File

@ -34,5 +34,6 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } 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>"));
} }

View File

@ -34,5 +34,6 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } 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>"));
} }

9
sort.c
View File

@ -236,7 +236,7 @@ main(int argc, char *argv[])
FILE *fp, *ofp = stdout; FILE *fp, *ofp = stdout;
struct linebuf linebuf = EMPTY_LINEBUF; struct linebuf linebuf = EMPTY_LINEBUF;
size_t i; size_t i;
int global_flags = 0; int global_flags = 0, ret = 0;
char *outfile = NULL; char *outfile = NULL;
ARGBEGIN { ARGBEGIN {
@ -300,7 +300,8 @@ main(int argc, char *argv[])
} else { } else {
getlines(fp, &linebuf); getlines(fp, &linebuf);
} }
fclose(fp); if (fshut(fp, *argv))
ret = 1;
} }
if (!Cflag && !cflag) { if (!Cflag && !cflag) {
@ -318,5 +319,7 @@ main(int argc, char *argv[])
} }
} }
return 0; enfshut(2, stdin, "<stdin>");
enfshut(2, stdout, "<stdout>");
return ret;
} }

View File

@ -27,7 +27,7 @@ nextfile(FILE *f, char *buf, int plen, int slen)
static int filecount = 0; static int filecount = 0;
if (f) if (f)
fclose(f); fshut(f, "<file>");
if (itostr(buf + plen, filecount++, slen) < 0) if (itostr(buf + plen, filecount++, slen) < 0)
return NULL; return NULL;
@ -124,9 +124,6 @@ main(int argc, char *argv[])
putc(ch, out); putc(ch, out);
} }
fclose(in); return !!(fshut(in, "<infile>") + (out && fshut(out, "<outfile>")) +
if (out) fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>"));
fclose(out);
return 0;
} }

View File

@ -32,8 +32,5 @@ 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]);
fclose(fp); return !!(fshut(fp, argv[0]) + fshut(tmpfp, "<tmpfile>"));
fclose(tmpfp);
return 0;
} }

View File

@ -85,9 +85,10 @@ main(int argc, char *argv[])
continue; continue;
} }
strings(fp, *argv, len); strings(fp, *argv, len);
fclose(fp); if (fshut(fp, *argv))
ret = 1;
} }
} }
return ret; return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret;
} }

5
tail.c
View File

@ -130,7 +130,8 @@ main(int argc, char *argv[])
tail(fp, *argv); tail(fp, *argv);
if (!fflag) { if (!fflag) {
fclose(fp); if (fshut(fp, *argv))
ret = 1;
continue; continue;
} }
for (tmp = NULL, tmpsize = 0;;) { for (tmp = NULL, tmpsize = 0;;) {
@ -155,5 +156,5 @@ main(int argc, char *argv[])
} }
} }
return ret; return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret;
} }

7
tar.c
View File

@ -166,8 +166,7 @@ archive(const char *path)
if (fwrite(b, BLKSIZ, 1, tarfile) != 1) if (fwrite(b, BLKSIZ, 1, tarfile) != 1)
eprintf("fwrite:"); eprintf("fwrite:");
} }
if (fclose(f) == EOF) efshut(f, path);
eprintf("fclose %s:", path);
} }
return 0; 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) if (f && fwrite(b, MIN(l, BLKSIZ), 1, f) != 1)
eprintf("fwrite %s:", fname); eprintf("fwrite %s:", fname);
} }
if (f && fclose(f) == EOF) if (f)
eprintf("fclose %s:", fname); fshut(f, fname);
if (!mflag) { if (!mflag) {
times[0].tv_sec = times[1].tv_sec = mtime; times[0].tv_sec = times[1].tv_sec = mtime;

4
tee.c
View File

@ -46,8 +46,6 @@ main(int argc, char *argv[])
eprintf("fwrite %s:", (i != argc) ? argv[i] : "<stdout>"); eprintf("fwrite %s:", (i != argc) ? argv[i] : "<stdout>");
} }
} }
if (ferror(stdin))
eprintf("fread <stdin>:");
return 0; return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>"));
} }

2
tr.c
View File

@ -204,7 +204,7 @@ main(int argc, char *argv[])
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 0; return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>"));
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) {

2
tty.c
View File

@ -26,5 +26,5 @@ main(int argc, char *argv[])
tty = ttyname(STDIN_FILENO); tty = ttyname(STDIN_FILENO);
puts(tty ? tty : "not a tty"); puts(tty ? tty : "not a tty");
return !tty; return fshut(stdout, "<stdout>") || !tty;
} }

View File

@ -55,5 +55,5 @@ main(int argc, char *argv[])
putword(u.machine); putword(u.machine);
putchar('\n'); putchar('\n');
return 0; return fshut(stdout, "<stdout>");
} }

View File

@ -159,7 +159,8 @@ main(int argc, char *argv[])
continue; continue;
} }
unexpand(*argv, fp); unexpand(*argv, fp);
fclose(fp); if (fshut(fp, *argv))
ret = 1;
} }
} }

5
uniq.c
View File

@ -131,10 +131,9 @@ main(int argc, char *argv[])
eprintf("fopen %s:", argv[1]); eprintf("fopen %s:", argv[1]);
} }
uniq(fp, ofp); uniq(fp, ofp);
fclose(fp);
} }
uniqfinish(ofp); uniqfinish(ofp);
fclose(ofp);
return 0; return !!(fshut(fp, fp == stdin ? "<stdin>" : argv[0]) +
fshut(ofp, ofp == stdout ? "<stdout>" : argv[1]));
} }

5
util.h
View File

@ -3,6 +3,7 @@
#include <regex.h> #include <regex.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h>
#include "arg.h" #include "arg.h"
#include "compat.h" #include "compat.h"
@ -34,6 +35,10 @@ void *enrealloc(int, void *, size_t);
char *enstrdup(int, const char *); char *enstrdup(int, const char *);
char *enstrndup(int, const char *, size_t); 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 enprintf(int, const char *, ...);
void eprintf(const char *, ...); void eprintf(const char *, ...);
void weprintf(const char *, ...); void weprintf(const char *, ...);

View File

@ -273,10 +273,7 @@ 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);
if (fp)
fclose(fp);
if (nfp)
fclose(nfp);
return 0; return !!(fshut(fp, fp == stdin ? "<stdin>" : argv[0]) +
fshut(nfp, nfp == stdout ? "<stdout>" : fname));
} }

View File

@ -101,7 +101,7 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
FILE *fp; FILE *fp = NULL;
ARGBEGIN { ARGBEGIN {
case 'm': case 'm':
@ -126,8 +126,8 @@ main(int argc, char *argv[])
uuencodeb64(fp, argv[1], argv[0]); uuencodeb64(fp, argv[1], argv[0]);
else else
uuencode(fp, argv[1], argv[0]); uuencode(fp, argv[1], argv[0]);
fclose(fp);
} }
return 0; return !!((fp && fshut(fp, argv[0])) + fshut(stdin, "<stdin>") +
fshut(stdout, "<stdout>"));
} }

5
wc.c
View File

@ -91,11 +91,12 @@ main(int argc, char *argv[])
continue; continue;
} }
wc(fp, *argv); wc(fp, *argv);
fclose(fp); if (fshut(fp, *argv))
ret = 1;
} }
if (many) if (many)
output("total", tc, tl, tw); output("total", tc, tl, tw);
} }
return ret; return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret;
} }

View File

@ -269,5 +269,6 @@ main(int argc, char *argv[])
free(argb); free(argb);
return nerrors ? 123 : 0; return (nerrors || !!(fshut(stdin, "<stdin>") +
fshut(stdout, "<stdout>"))) ? 123 : 0;
} }