[hackers] [farbfeld] Add efread() and efwrite() || Laslo Hunhold

From: <git_AT_suckless.org>
Date: Wed, 11 Apr 2018 12:08:53 +0200 (CEST)

commit 02b051634969f701ccee57505140c9e7f334e22c
Author: Laslo Hunhold <dev_AT_frign.de>
AuthorDate: Wed Apr 11 11:51:01 2018 +0200
Commit: Laslo Hunhold <dev_AT_frign.de>
CommitDate: Wed Apr 11 11:51:01 2018 +0200

    Add efread() and efwrite()
    
    Given we have a util.c anyway it does not make any sense to duplicate
    code here. This way, error handling and output is in one place and one
    does not have to change things in multiple different files. This also
    reduces code duplication by a lot.
    
    It also solves an older bug where the error reporting was not on point:
    
       $ echo "farb" | ff2png
       ff2png: fread: Success
    
    (It also lacked a newline)
    
    Now it properly reports
    
       $ echo "farb" | ff2png
       ff2png: fread: Unexpected end of file
    
    I also fixed some other minor details, for instance that all error
    messages should begin with a capital letter.

diff --git a/ff2jpg.c b/ff2jpg.c
index 23a506b..9774549 100644
--- a/ff2jpg.c
+++ b/ff2jpg.c
_AT_@ -93,16 +93,7 @@ main(int argc, char *argv[])
 
         /* write data */
         for (i = 0; i < height; ++i) {
- if (fread(row, sizeof(uint16_t), rowlen, stdin) != rowlen) {
- if (ferror(stdin)) {
- fprintf(stderr, "%s: fread: %s\n", argv0,
- strerror(errno));
- } else {
- fprintf(stderr, "%s: unexpected end of file\n",
- argv0);
- }
- return 1;
- }
+ efread(row, sizeof(uint16_t), rowlen, stdin);
                 for (j = 0, k = 0; j < rowlen; j += 4, k += 3) {
                         a = ntohs(row[j + 3]);
                         for (l = 0; l < 3; l++) {
diff --git a/ff2pam.c b/ff2pam.c
index 9a18e6b..de96bbe 100644
--- a/ff2pam.c
+++ b/ff2pam.c
_AT_@ -48,18 +48,8 @@ main(int argc, char *argv[])
                width, height);
 
         for (i = 0; i < height; i++) {
- if (fread(row, sizeof(uint16_t), rowlen, stdin) != rowlen) {
- if (ferror(stdin)) {
- fprintf(stderr, "%s: fread: %s\n", argv0, strerror(errno));
- } else {
- fprintf(stderr, "%s: unexpected end of file\n", argv0);
- }
- return 1;
- }
- if (fwrite(row, sizeof(uint16_t), rowlen, stdout) != rowlen) {
- fprintf(stderr, "%s: fwrite: %s\n", argv0, strerror(errno));
- return 1;
- }
+ efread(row, sizeof(uint16_t), rowlen, stdin);
+ efwrite(row, sizeof(uint16_t), rowlen, stdout);
         }
 
         return fshut(stdout, "<stdout>");
diff --git a/ff2png.c b/ff2png.c
index 58b3d37..3e51967 100644
--- a/ff2png.c
+++ b/ff2png.c
_AT_@ -68,14 +68,7 @@ main(int argc, char *argv[])
 
         /* write data */
         for (i = 0; i < height; ++i) {
- if (fread(row, sizeof(uint16_t), rowlen, stdin) != rowlen) {
- if (ferror(stdin)) {
- fprintf(stderr, "%s: fread: %s\n", argv0, strerror(errno));
- } else {
- fprintf(stderr, "%s: unexpected end of file\n", argv0);
- }
- return 1;
- }
+ efread(row, sizeof(uint16_t), rowlen, stdin);
                 png_write_row(pngs, (uint8_t *)row);
         }
 
diff --git a/ff2ppm.c b/ff2ppm.c
index b34d870..5e11eb5 100644
--- a/ff2ppm.c
+++ b/ff2ppm.c
_AT_@ -53,16 +53,8 @@ main(int argc, char *argv[])
         printf("P6\n%" PRIu32 " %" PRIu32 "\n255\n", width, height);
 
         for (i = 0; i < height; ++i) {
- if (fread(row, sizeof(uint16_t), rowlen, stdin) != rowlen) {
- if (ferror(stdin)) {
- fprintf(stderr, "%s: fread: %s\n", argv0,
- strerror(errno));
- } else {
- fprintf(stderr, "%s: unexpected end of file\n",
- argv0);
- }
- return 1;
- }
+ efread(row, sizeof(uint16_t), rowlen, stdin);
+
                 for (j = 0, k = 0; j < rowlen; j += 4, k += 3) {
                         a = ntohs(row[j + 3]);
                         for (l = 0; l < 3; l++) {
_AT_@ -73,10 +65,8 @@ main(int argc, char *argv[])
                                                  (UINT16_MAX / UINT8_MAX));
                         }
                 }
- if (fwrite(rowout, sizeof(uint8_t), rowoutlen, stdout) != rowoutlen) {
- fprintf(stderr, "%s: fwrite: %s\n", argv0, strerror(errno));
- return 1;
- }
+
+ efwrite(rowout, sizeof(uint8_t), rowoutlen, stdout);
         }
 
         return fshut(stdout, "<stdout>");
diff --git a/jpg2ff.c b/jpg2ff.c
index af7b03c..b00924e 100644
--- a/jpg2ff.c
+++ b/jpg2ff.c
_AT_@ -80,10 +80,7 @@ main(int argc, char *argv[])
                         row[4 * i + 3] = htons(65535);
                 }
 
- if (fwrite(row, sizeof(uint16_t), rowlen, stdout) != rowlen) {
- fprintf(stderr, "%s: fwrite: %s\n", argv0, strerror(errno));
- return 1;
- }
+ efwrite(row, sizeof(uint16_t), rowlen, stdout);
         }
 
         /* clean up */
diff --git a/png2ff.c b/png2ff.c
index c7237c4..b76fde9 100644
--- a/png2ff.c
+++ b/png2ff.c
_AT_@ -81,22 +81,12 @@ main(int argc, char *argv[])
                         for (i = 0; i < rowlen; i++) {
                                 row[i] = htons(257 * pngrows[r][i]);
                         }
- if (fwrite(row, sizeof(uint16_t), rowlen,
- stdout) != rowlen) {
- fprintf(stderr, "%s: fwrite: %s\n", argv0,
- strerror(errno));
- return 1;
- }
+ efwrite(row, sizeof(uint16_t), rowlen, stdout);
                 }
                 break;
         case 16:
                 for (r = 0; r < height; ++r) {
- if (fwrite(pngrows[r], sizeof(uint16_t), rowlen,
- stdout) != rowlen) {
- fprintf(stderr, "%s: fwrite: %s\n", argv0,
- strerror(errno));
- return 1;
- }
+ efwrite(pngrows[r], sizeof(uint16_t), rowlen, stdout);
                 }
                 break;
         default:
diff --git a/util.c b/util.c
index f9cf33e..212f595 100644
--- a/util.c
+++ b/util.c
_AT_@ -18,13 +18,10 @@ ff_read_header(uint32_t *width, uint32_t *height)
 {
         uint32_t hdr[4];
 
- if (fread(hdr, sizeof(*hdr), LEN(hdr), stdin) != LEN(hdr)) {
- fprintf(stderr, "%s: fread: %s", argv0, strerror(errno));
- exit(1);
- }
+ efread(hdr, sizeof(*hdr), LEN(hdr), stdin);
 
         if (memcmp("farbfeld", hdr, sizeof("farbfeld") - 1)) {
- fprintf(stderr, "%s: invalid magic value\n", argv0);
+ fprintf(stderr, "%s: Invalid magic value\n", argv0);
                 exit(1);
         }
 
_AT_@ -40,16 +37,10 @@ ff_write_header(uint32_t width, uint32_t height)
         fputs("farbfeld", stdout);
 
         tmp = htonl(width);
- if (fwrite(&tmp, sizeof(tmp), 1, stdout) != 1) {
- fprintf(stderr, "%s: write: %s", argv0, strerror(errno));
- exit(1);
- }
+ efwrite(&tmp, sizeof(tmp), 1, stdout);
 
         tmp = htonl(height);
- if (fwrite(&tmp, sizeof(tmp), 1, stdout) != 1) {
- fprintf(stderr, "%s: write: %s", argv0, strerror(errno));
- exit(1);
- }
+ efwrite(&tmp, sizeof(tmp), 1, stdout);
 }
 
 int
_AT_@ -93,13 +84,13 @@ fshut(FILE *fp, const char *fname)
         fflush(fp);
 
         if (ferror(fp) && !ret) {
- fprintf(stderr, "%s: ferror %s: %s\n", argv0, fname,
+ 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,
+ fprintf(stderr, "%s: fclose '%s': %s\n", argv0, fname,
                         strerror(errno));
                 ret = 1;
         }
_AT_@ -107,13 +98,37 @@ fshut(FILE *fp, const char *fname)
         return ret;
 }
 
+void
+efread(void *p, size_t s, size_t n, FILE *f)
+{
+ if (fread(p, s, n, f) != n) {
+ if (ferror(f)) {
+ fprintf(stderr, "%s: fread: %s\n", argv0,
+ strerror(errno));
+ } else {
+ fprintf(stderr, "%s: fread: Unexpected end of file\n",
+ argv0);
+ }
+ exit(1);
+ }
+}
+
+void
+efwrite(const void *p, size_t s, size_t n, FILE *f)
+{
+ if (fwrite(p, s, n, f) != n) {
+ fprintf(stderr, "%s: fwrite: %s\n", argv0, strerror(errno));
+ exit(1);
+ }
+}
+
 void *
 ereallocarray(void *optr, size_t nmemb, size_t size)
 {
         void *p;
 
         if (!(p = reallocarray(optr, nmemb, size))) {
- fprintf(stderr, "%s: reallocarray: out of memory\n", argv0);
+ fprintf(stderr, "%s: reallocarray: Out of memory\n", argv0);
                 exit(1);
         }
 
_AT_@ -128,7 +143,7 @@ estrtonum(const char *numstr, long long minval, long long maxval)
 
         ll = strtonum(numstr, minval, maxval, &errstr);
         if (errstr) {
- fprintf(stderr, "%s: strtonum %s: %s\n", argv0, numstr, errstr);
+ fprintf(stderr, "%s: strtonum '%s': %s\n", argv0, numstr, errstr);
                 exit(1);
         }
 
diff --git a/util.h b/util.h
index fcb5257..0081b13 100644
--- a/util.h
+++ b/util.h
_AT_@ -13,6 +13,9 @@ int parse_mask(const char *, uint16_t mask[3]);
 
 int fshut(FILE *, const char *);
 
+void efread(void *, size_t, size_t, FILE *);
+void efwrite(const void *, size_t, size_t, FILE *);
+
 #undef reallocarray
 void *reallocarray(void *, size_t, size_t);
 void *ereallocarray(void *optr, size_t nmemb, size_t size);
Received on Wed Apr 11 2018 - 12:08:53 CEST

This archive was generated by hypermail 2.3.0 : Wed Apr 11 2018 - 12:12:22 CEST