summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorLaslo Hunhold <dev@frign.de>2017-04-13 00:07:10 +0200
committerLaslo Hunhold <dev@frign.de>2017-04-13 00:07:10 +0200
commit17f09e2cea4dda0f54841f7a273e347b53f4996e (patch)
treeef8083df2105f8ed06b79f281316b537e836764b /util.c
parentbc03439e0e0c439bb9c6c3167d9c272f3b7d5632 (diff)
Use fshut() to properly flush the output stream
For small images, it could happen that the output stream would not be flushed before exit(), resulting in a lack of error-reporting on a full device. Using fflush(), a function I first introduced in sbase, we do the flushing before returning manually and report errors if they occurred.
Diffstat (limited to 'util.c')
-rw-r--r--util.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/util.c b/util.c
index 7b59d19..ea933ca 100644
--- a/util.c
+++ b/util.c
@@ -82,6 +82,33 @@ parse_mask(const char *s, uint16_t mask[3])
return 0;
}
+int
+fshut(FILE *fp, const char *fname)
+{
+ int ret = 0;
+
+ /* fflush() is undefined for input streams by ISO C,
+ * but not POSIX 2008 if you ignore ISO C overrides.
+ * Leave it unchecked and rely on the following
+ * functions to detect errors.
+ */
+ fflush(fp);
+
+ if (ferror(fp) && !ret) {
+ fprintf(stderr, "%s: ferror %s: %s\n", argv0, fname,
+ strerror(errno));
+ ret = 1;
+ }
+
+ if (fclose(fp) && !ret) {
+ fprintf(stderr, "%s: fclose %s: %s\n", argv0, fname,
+ strerror(errno));
+ ret = 1;
+ }
+
+ return ret;
+}
+
void *
ereallocarray(void *optr, size_t nmemb, size_t size)
{