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.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'sfeed.c') diff --git a/sfeed.c b/sfeed.c index 4dd89c1..f2aa6b0 100644 --- a/sfeed.c +++ b/sfeed.c @@ -679,6 +679,9 @@ printfields(void) putchar(FieldSeparator); string_print_trimmed_multi(&ctx.fields[FeedFieldCategory].str); putchar('\n'); + + if (ferror(stdout)) /* check for errors but do not flush */ + checkfileerror(stdout, "", 'w'); } static int @@ -1059,5 +1062,8 @@ main(int argc, char *argv[]) /* NOTE: getnext is defined in xml.h for inline optimization */ xml_parse(&parser); + checkfileerror(stdin, "", 'r'); + checkfileerror(stdout, "", 'w'); + return 0; } -- cgit v1.2.3