coreutils

General Software Utilities
git clone http://git.omkov.net/coreutils
Log | Tree | Refs | README | LICENCE | Download

AuthorJamozed <[email protected]>
Date2021-02-22 03:40:57
Commit5604fd4227f55f2664e88e5dba7c239ac1e7c54b
Parentbb4ca8532d314f3f5d163256cab5dc88a9465eca

cat: Throw error on fwrite failure

Diffstat

M src/cat.c | 11 +++++------

1 files changed, 5 insertions, 6 deletions

diff --git a/src/cat.c b/src/cat.c
index 7f9a949..a152847 100644
--- a/src/cat.c
+++ b/src/cat.c
@@ -1,4 +1,4 @@
-// cat.c, version 1.0.4
+// cat.c, version 1.0.5
 // OMKOV coreutils implementation of POSIX cat
 // Copyright (C) 2020, Jakob Wakeling
 // All rights reserved.
@@ -36,7 +36,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
 #include <stdbool.h>
 #include <stdio.h>
 
-#define VERSION "1.0.4"
+#define VERSION "1.0.5"
 
 static struct lop lops[] = {
 	{ "help",    ARG_NUL, 256 },
@@ -60,11 +60,10 @@ int main(int ac, char *av[]) { A0 = av[0];
 
 	// Disable buffering on stdout
 	setvbuf(stdout, NULL, _IONBF, 0);
-	bool warned = false;
 
 	if (opt.ind == ac) { cat("-"); }
 	else for (char **p = &av[opt.ind]; *p; ++p) if (cat(*p)) {
-		warn("%s: %s", *p, serr()); warned = true;
+		warn("%s: %s", *p, serr());
 	}
 
 	return warned;
@@ -80,8 +79,8 @@ static inline int cat(const char *file) {
 	if (file[0] == '-' && file[1] == 0) { fi = stdin; }
 	else if (!(fi = fopen(file, "r"))) { return 1; }
 
-	for (size_t c; (c = fread(buf, 1, sizeof (buf), fi)) != 0;) {
-		fwrite(buf, 1, c, stdout);
+	for (register size_t c; (c = fread(buf, 1, sizeof (buf), fi));) {
+		if (fwrite(buf, 1, c, stdout) != c) { fclose(fi); return 1; }
 	}
 
 	if (fi != stdin) { fclose(fi); } return 0;