summaryrefslogtreecommitdiff
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
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.
-rw-r--r--ff2jpg.c2
-rw-r--r--ff2pam.c2
-rw-r--r--ff2png.c2
-rw-r--r--ff2ppm.c2
-rw-r--r--jpg2ff.c2
-rw-r--r--png2ff.c2
-rw-r--r--util.c27
-rw-r--r--util.h2
8 files changed, 35 insertions, 6 deletions
diff --git a/ff2jpg.c b/ff2jpg.c
index 78cb122..213f90d 100644
--- 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
index 2c4922c..9a18e6b 100644
--- 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
index a00e257..58b3d37 100644
--- 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
index f490d94..746425b 100644
--- 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
index 8d51fb6..af7b03c 100644
--- 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
index dd9073c..d76d707 100644
--- 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
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)
{
diff --git a/util.h b/util.h
index 93492a5..fcb5257 100644
--- 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);