improve stream handling of read and write errors - json2tsv - JSON to TSV conve… | |
git clone git://git.codemadness.org/json2tsv | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit ca7aea61b3a593c782c7da48ec3e6552b47a698e | |
parent eb921bbad3f1120270014eaad9b2cba8a2958130 | |
Author: Hiltjo Posthuma <[email protected]> | |
Date: Sat, 19 Mar 2022 13:10:53 +0100 | |
improve stream handling of read and write errors | |
Diffstat: | |
M json2tsv.1 | 4 ++-- | |
M json2tsv.c | 14 ++++++++++++++ | |
2 files changed, 16 insertions(+), 2 deletions(-) | |
--- | |
diff --git a/json2tsv.1 b/json2tsv.1 | |
@@ -1,4 +1,4 @@ | |
-.Dd February 20, 2022 | |
+.Dd March 19, 2022 | |
.Dt JSON2TSV 1 | |
.Os | |
.Sh NAME | |
@@ -101,7 +101,7 @@ s for string | |
.Sh EXIT STATUS | |
.Nm | |
exits with the exit status 0 on success, 1 on a parse error, 2 when out of | |
-memory or 3 with an usage error. | |
+memory or a read/write error or 3 with an usage error. | |
.Sh EXAMPLES | |
.Bd -literal | |
json2tsv < input.json | awk -F '\\t' '$1 == ".url" { print $3 }' | |
diff --git a/json2tsv.c b/json2tsv.c | |
@@ -110,6 +110,11 @@ processnode(struct json_node *nodes, size_t depth, const c… | |
putchar(fs); | |
printvalue(value); | |
putchar(rs); | |
+ | |
+ if (ferror(stdout)) { | |
+ fprintf(stderr, "write error: <stdout>\n"); | |
+ exit(2); | |
+ } | |
} | |
int | |
@@ -217,5 +222,14 @@ nextarg:; | |
return 1; | |
} | |
+ if (ferror(stdin)) { | |
+ fprintf(stderr, "read error: <stdin>\n"); | |
+ return 2; | |
+ } | |
+ if (fflush(stdout) || ferror(stdout)) { | |
+ fprintf(stderr, "write error: <stdout>\n"); | |
+ return 2; | |
+ } | |
+ | |
return 0; | |
} |