[hackers] [farbfeld] Use fshut() to properly flush the output stream || Laslo Hunhold

From: <git_AT_suckless.org>
Date: Thu, 13 Apr 2017 00:09:16 +0200 (CEST)

commit 17f09e2cea4dda0f54841f7a273e347b53f4996e
Author: Laslo Hunhold <dev_AT_frign.de>
AuthorDate: Thu Apr 13 00:07:10 2017 +0200
Commit: Laslo Hunhold <dev_AT_frign.de>
CommitDate: Thu Apr 13 00:07:10 2017 +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.

diff --git a/ff2jpg.c b/ff2jpg.c
index 78cb122..213f90d 100644
--- a/ff2jpg.c
+++ b/ff2jpg.c
_AT_@ -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
_AT_@ -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
_AT_@ -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
_AT_@ -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
_AT_@ -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
_AT_@ -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
_AT_@ -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
_AT_@ -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);
Received on Thu Apr 13 2017 - 00:09:16 CEST

This archive was generated by hypermail 2.3.0 : Thu Apr 13 2017 - 00:12:48 CEST