diff options
author | Hiltjo Posthuma <hiltjo@codemadness.org> | 2022-03-14 19:22:42 +0100 |
---|---|---|
committer | Hiltjo Posthuma <hiltjo@codemadness.org> | 2022-03-15 14:46:46 +0100 |
commit | fad48ffa27af96ee0d9489ded88f80c1eeb238dc (patch) | |
tree | dbd19eb13b389eb230325049d576c600bde606fa /sfeed_frames.c | |
parent | 813a96b517ae96716fb018ff93ab2d6a4bbcda95 (diff) |
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: <stdout>
echo $?
1
Tested with a small mfs on OpenBSD, fstab entry:
swap /mnt/test mfs rw,nodev,nosuid,-s=1M 0 0
Diffstat (limited to 'sfeed_frames.c')
-rw-r--r-- | sfeed_frames.c | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/sfeed_frames.c b/sfeed_frames.c index d345c60..178a4a2 100644 --- a/sfeed_frames.c +++ b/sfeed_frames.c @@ -34,7 +34,8 @@ printfeed(FILE *fpitems, FILE *fpin, struct feed *f) } fputs("<pre>\n", fpitems); - while ((linelen = getline(&line, &linesize, fpin)) > 0) { + while ((linelen = getline(&line, &linesize, fpin)) > 0 && + !ferror(fpitems)) { if (line[linelen - 1] == '\n') line[--linelen] = '\0'; parseline(line, fields); @@ -114,6 +115,7 @@ main(int argc, char *argv[]) if (argc == 1) { feeds[0].name = ""; printfeed(fpitems, stdin, &feeds[0]); + checkfileerror(stdin, "<stdin>", 'r'); } else { for (i = 1; i < argc; i++) { name = ((name = strrchr(argv[i], '/'))) ? name + 1 : argv[i]; @@ -122,8 +124,8 @@ main(int argc, char *argv[]) if (!(fp = fopen(argv[i], "r"))) err(1, "fopen: %s", argv[i]); printfeed(fpitems, fp, &feeds[i - 1]); - if (ferror(fp)) - err(1, "ferror: %s", argv[i]); + checkfileerror(fp, argv[i], 'r'); + checkfileerror(fpitems, "items.html", 'w'); fclose(fp); } } @@ -174,10 +176,15 @@ main(int argc, char *argv[]) "</frameset>\n" "</html>\n", fpindex); + checkfileerror(fpindex, "index.html", 'w'); + checkfileerror(fpitems, "items.html", 'w'); + fclose(fpindex); fclose(fpitems); - if (fpmenu) + if (fpmenu) { + checkfileerror(fpmenu, "menu.html", 'w'); fclose(fpmenu); + } return 0; } |