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_mbox.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'sfeed_mbox.c') diff --git a/sfeed_mbox.c b/sfeed_mbox.c index c2827d4..33c9ec6 100644 --- a/sfeed_mbox.c +++ b/sfeed_mbox.c @@ -63,7 +63,8 @@ printfeed(FILE *fp, const char *feedname) ssize_t linelen; int ishtml; - while ((linelen = getline(&line, &linesize, fp)) > 0) { + while ((linelen = getline(&line, &linesize, fp)) > 0 && + !ferror(stdout)) { if (line[linelen - 1] == '\n') line[--linelen] = '\0'; hash = djb2((unsigned char *)line, 5381ULL); @@ -163,17 +164,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