From fad48ffa27af96ee0d9489ded88f80c1eeb238dc Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Mon, 14 Mar 2022 19:22:42 +0100 Subject: stricter error checking in file streams (input, output) This also makes the programs exit with a non-zero status when a read or write error occurs. This makes checking the exit status more reliable in scripts. A simple example to simulate a disk with no space left: curl -s 'https://codemadness.org/atom.xml' | sfeed > f /mnt/test: write failed, file system is full echo $? 0 Which now produces: curl -s 'https://codemadness.org/atom.xml' | sfeed > f /mnt/test: write failed, file system is full write error: echo $? 1 Tested with a small mfs on OpenBSD, fstab entry: swap /mnt/test mfs rw,nodev,nosuid,-s=1M 0 0 --- sfeed_twtxt.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'sfeed_twtxt.c') diff --git a/sfeed_twtxt.c b/sfeed_twtxt.c index 6107fc7..8514bd6 100644 --- a/sfeed_twtxt.c +++ b/sfeed_twtxt.c @@ -17,7 +17,8 @@ printfeed(FILE *fp, const char *feedname) time_t parsedtime; ssize_t linelen; - while ((linelen = getline(&line, &linesize, fp)) > 0) { + while ((linelen = getline(&line, &linesize, fp)) > 0 && + !ferror(stdout)) { if (line[linelen - 1] == '\n') line[--linelen] = '\0'; parseline(line, fields); @@ -54,17 +55,19 @@ main(int argc, char *argv[]) if (argc == 1) { printfeed(stdin, ""); + checkfileerror(stdin, "", 'r'); } else { for (i = 1; i < argc; i++) { if (!(fp = fopen(argv[i], "r"))) err(1, "fopen: %s", argv[i]); name = ((name = strrchr(argv[i], '/'))) ? name + 1 : argv[i]; printfeed(fp, name); - if (ferror(fp)) - err(1, "ferror: %s", argv[i]); + checkfileerror(fp, argv[i], 'r'); fclose(fp); } } + checkfileerror(stdout, "", 'w'); + return 0; } -- cgit v1.2.3 From 665ce267d994f7feb090c0c695c8462ae2d87a50 Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Sun, 20 Mar 2022 13:19:12 +0100 Subject: check write errors on the output stream after each feed This will detect write errors sooner. --- sfeed_atom.c | 1 + sfeed_html.c | 1 + sfeed_mbox.c | 1 + sfeed_plain.c | 1 + sfeed_twtxt.c | 1 + 5 files changed, 5 insertions(+) (limited to 'sfeed_twtxt.c') diff --git a/sfeed_atom.c b/sfeed_atom.c index 07c34cd..3ce5cf0 100644 --- a/sfeed_atom.c +++ b/sfeed_atom.c @@ -150,6 +150,7 @@ main(int argc, char *argv[]) name = ((name = strrchr(argv[i], '/'))) ? name + 1 : argv[i]; printfeed(fp, name); checkfileerror(fp, argv[i], 'r'); + checkfileerror(stdout, "", 'w'); fclose(fp); } } diff --git a/sfeed_html.c b/sfeed_html.c index f3cff2b..ce96687 100644 --- a/sfeed_html.c +++ b/sfeed_html.c @@ -119,6 +119,7 @@ main(int argc, char *argv[]) err(1, "fopen: %s", argv[i]); printfeed(fp, &feeds[i - 1]); checkfileerror(fp, argv[i], 'r'); + checkfileerror(stdout, "", 'w'); fclose(fp); } } diff --git a/sfeed_mbox.c b/sfeed_mbox.c index 33c9ec6..30ac875 100644 --- a/sfeed_mbox.c +++ b/sfeed_mbox.c @@ -172,6 +172,7 @@ main(int argc, char *argv[]) name = ((name = strrchr(argv[i], '/'))) ? name + 1 : argv[i]; printfeed(fp, name); checkfileerror(fp, argv[i], 'r'); + checkfileerror(stdout, "", 'w'); fclose(fp); } } diff --git a/sfeed_plain.c b/sfeed_plain.c index c954eff..8b1f00f 100644 --- a/sfeed_plain.c +++ b/sfeed_plain.c @@ -78,6 +78,7 @@ main(int argc, char *argv[]) name = ((name = strrchr(argv[i], '/'))) ? name + 1 : argv[i]; printfeed(fp, name); checkfileerror(fp, argv[i], 'r'); + checkfileerror(stdout, "", 'w'); fclose(fp); } } diff --git a/sfeed_twtxt.c b/sfeed_twtxt.c index 8514bd6..1d8ab36 100644 --- a/sfeed_twtxt.c +++ b/sfeed_twtxt.c @@ -63,6 +63,7 @@ main(int argc, char *argv[]) name = ((name = strrchr(argv[i], '/'))) ? name + 1 : argv[i]; printfeed(fp, name); checkfileerror(fp, argv[i], 'r'); + checkfileerror(stdout, "", 'w'); fclose(fp); } } -- cgit v1.2.3 From db1dcafd03997127f2cbc82376e2cc8df9b77356 Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Fri, 25 Mar 2022 23:18:58 +0100 Subject: rm sys/types.h include and improve portability This include is not needed. It was intended for ssize_t but this is already defined by stdio.h for getline(). --- sfeed_atom.c | 2 -- sfeed_curses.c | 1 - sfeed_frames.c | 2 -- sfeed_gopher.c | 2 -- sfeed_html.c | 2 -- sfeed_plain.c | 2 -- sfeed_twtxt.c | 2 -- 7 files changed, 13 deletions(-) (limited to 'sfeed_twtxt.c') diff --git a/sfeed_atom.c b/sfeed_atom.c index 3ce5cf0..c74e5fa 100644 --- a/sfeed_atom.c +++ b/sfeed_atom.c @@ -1,5 +1,3 @@ -#include - #include #include #include diff --git a/sfeed_curses.c b/sfeed_curses.c index 484593b..0d878f7 100644 --- a/sfeed_curses.c +++ b/sfeed_curses.c @@ -4,7 +4,6 @@ #include #include -#include #include #include #include diff --git a/sfeed_frames.c b/sfeed_frames.c index 178a4a2..7fcf3ee 100644 --- a/sfeed_frames.c +++ b/sfeed_frames.c @@ -1,5 +1,3 @@ -#include - #include #include #include diff --git a/sfeed_gopher.c b/sfeed_gopher.c index 7da41f6..0d3ea38 100644 --- a/sfeed_gopher.c +++ b/sfeed_gopher.c @@ -1,5 +1,3 @@ -#include - #include #include #include diff --git a/sfeed_html.c b/sfeed_html.c index ce96687..ebc9acd 100644 --- a/sfeed_html.c +++ b/sfeed_html.c @@ -1,5 +1,3 @@ -#include - #include #include #include diff --git a/sfeed_plain.c b/sfeed_plain.c index 8b1f00f..732eb04 100644 --- a/sfeed_plain.c +++ b/sfeed_plain.c @@ -1,5 +1,3 @@ -#include - #include #include #include diff --git a/sfeed_twtxt.c b/sfeed_twtxt.c index 1d8ab36..1bb9d3b 100644 --- a/sfeed_twtxt.c +++ b/sfeed_twtxt.c @@ -1,5 +1,3 @@ -#include - #include #include #include -- cgit v1.2.3 From ebc33326e1da17131171a76a9136c1f1af4f0c55 Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Sun, 27 Mar 2022 14:57:05 +0200 Subject: Revert "rm sys/types.h include and improve portability" This reverts commit db1dcafd03997127f2cbc82376e2cc8df9b77356. This is needed. Tested on an (old) Slackware 11 install. --- sfeed_atom.c | 2 ++ sfeed_curses.c | 1 + sfeed_frames.c | 2 ++ sfeed_gopher.c | 2 ++ sfeed_html.c | 2 ++ sfeed_plain.c | 2 ++ sfeed_twtxt.c | 2 ++ 7 files changed, 13 insertions(+) (limited to 'sfeed_twtxt.c') diff --git a/sfeed_atom.c b/sfeed_atom.c index c74e5fa..3ce5cf0 100644 --- a/sfeed_atom.c +++ b/sfeed_atom.c @@ -1,3 +1,5 @@ +#include + #include #include #include diff --git a/sfeed_curses.c b/sfeed_curses.c index b5d1901..7d16b05 100644 --- a/sfeed_curses.c +++ b/sfeed_curses.c @@ -4,6 +4,7 @@ #include #include +#include #include #include #include diff --git a/sfeed_frames.c b/sfeed_frames.c index 7fcf3ee..178a4a2 100644 --- a/sfeed_frames.c +++ b/sfeed_frames.c @@ -1,3 +1,5 @@ +#include + #include #include #include diff --git a/sfeed_gopher.c b/sfeed_gopher.c index 0d3ea38..7da41f6 100644 --- a/sfeed_gopher.c +++ b/sfeed_gopher.c @@ -1,3 +1,5 @@ +#include + #include #include #include diff --git a/sfeed_html.c b/sfeed_html.c index ebc9acd..ce96687 100644 --- a/sfeed_html.c +++ b/sfeed_html.c @@ -1,3 +1,5 @@ +#include + #include #include #include diff --git a/sfeed_plain.c b/sfeed_plain.c index 732eb04..8b1f00f 100644 --- a/sfeed_plain.c +++ b/sfeed_plain.c @@ -1,3 +1,5 @@ +#include + #include #include #include diff --git a/sfeed_twtxt.c b/sfeed_twtxt.c index 1bb9d3b..1d8ab36 100644 --- a/sfeed_twtxt.c +++ b/sfeed_twtxt.c @@ -1,3 +1,5 @@ +#include + #include #include #include -- cgit v1.2.3 From 21790adad1672689225efe38b6a59494181d323b Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Tue, 16 May 2023 20:11:21 +0200 Subject: improve to use proper includes Reduce using some of the unneeded sys/* headers too. This makes it slightly more portable or easier to port also. --- sfeed_atom.c | 2 -- sfeed_curses.c | 2 -- sfeed_frames.c | 2 -- sfeed_gopher.c | 2 -- sfeed_html.c | 2 -- sfeed_json.c | 3 +-- sfeed_plain.c | 2 -- sfeed_twtxt.c | 2 -- 8 files changed, 1 insertion(+), 16 deletions(-) (limited to 'sfeed_twtxt.c') diff --git a/sfeed_atom.c b/sfeed_atom.c index 99facad..d0b139d 100644 --- a/sfeed_atom.c +++ b/sfeed_atom.c @@ -1,5 +1,3 @@ -#include - #include #include #include diff --git a/sfeed_curses.c b/sfeed_curses.c index f54b4d6..0ce9a4e 100644 --- a/sfeed_curses.c +++ b/sfeed_curses.c @@ -1,7 +1,5 @@ #include #include -#include -#include #include #include diff --git a/sfeed_frames.c b/sfeed_frames.c index b2f75cf..53f44a6 100644 --- a/sfeed_frames.c +++ b/sfeed_frames.c @@ -1,5 +1,3 @@ -#include - #include #include #include diff --git a/sfeed_gopher.c b/sfeed_gopher.c index af6a49c..c879864 100644 --- a/sfeed_gopher.c +++ b/sfeed_gopher.c @@ -1,5 +1,3 @@ -#include - #include #include #include diff --git a/sfeed_html.c b/sfeed_html.c index 9269413..2142145 100644 --- a/sfeed_html.c +++ b/sfeed_html.c @@ -1,5 +1,3 @@ -#include - #include #include #include diff --git a/sfeed_json.c b/sfeed_json.c index e177d2b..4fe5942 100644 --- a/sfeed_json.c +++ b/sfeed_json.c @@ -1,7 +1,6 @@ #include -#include #include -#include +#include #include "util.h" diff --git a/sfeed_plain.c b/sfeed_plain.c index f8ce7ec..adeefdb 100644 --- a/sfeed_plain.c +++ b/sfeed_plain.c @@ -1,5 +1,3 @@ -#include - #include #include #include diff --git a/sfeed_twtxt.c b/sfeed_twtxt.c index 1d8ab36..1bb9d3b 100644 --- a/sfeed_twtxt.c +++ b/sfeed_twtxt.c @@ -1,5 +1,3 @@ -#include - #include #include #include -- cgit v1.2.3