commit 17f09e2cea4dda0f54841f7a273e347b53f4996e
parent bc03439e0e0c439bb9c6c3167d9c272f3b7d5632
Author: Laslo Hunhold <dev@frign.de>
Date: Thu, 13 Apr 2017 00:07:10 +0200
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:
8 files changed, 35 insertions(+), 6 deletions(-)
diff --git a/ff2jpg.c b/ff2jpg.c
@@ -118,5 +118,5 @@ main(int argc, char *argv[])
jpeg_finish_compress(&jcomp);
jpeg_destroy_compress(&jcomp);
- return 0;
+ return fshut(stdout, "<stdout>");
}
diff --git a/ff2pam.c b/ff2pam.c
@@ -62,5 +62,5 @@ main(int argc, char *argv[])
}
}
- return 0;
+ return fshut(stdout, "<stdout>");
}
diff --git a/ff2png.c b/ff2png.c
@@ -83,5 +83,5 @@ main(int argc, char *argv[])
png_write_end(pngs, NULL);
png_destroy_write_struct(&pngs, NULL);
- return 0;
+ return fshut(stdout, "<stdout>");
}
diff --git a/ff2ppm.c b/ff2ppm.c
@@ -77,5 +77,5 @@ main(int argc, char *argv[])
}
}
- return 0;
+ return fshut(stdout, "<stdout>");
}
diff --git a/jpg2ff.c b/jpg2ff.c
@@ -90,5 +90,5 @@ main(int argc, char *argv[])
jpeg_finish_decompress(&js);
jpeg_destroy_decompress(&js);
- return 0;
+ return fshut(stdout, "<stdout>");
}
diff --git a/png2ff.c b/png2ff.c
@@ -107,5 +107,5 @@ main(int argc, char *argv[])
/* clean up */
png_destroy_read_struct(&pngs, &pngi, NULL);
- return 0;
+ return fshut(stdout, "<stdout>");
}
diff --git 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)
{
diff --git a/util.h b/util.h
@@ -11,6 +11,8 @@ void ff_write_header(uint32_t width, uint32_t height);
int parse_mask(const char *, uint16_t mask[3]);
+int fshut(FILE *, const char *);
+
#undef reallocarray
void *reallocarray(void *, size_t, size_t);
void *ereallocarray(void *optr, size_t nmemb, size_t size);