farbfeld

suckless image format with conversion tools
git clone git://git.suckless.org/farbfeld
Log | Files | Refs | README | LICENSE

commit 02b051634969f701ccee57505140c9e7f334e22c
parent a9877de22960f14fb16e862cf46290a477779e98
Author: Laslo Hunhold <dev@frign.de>
Date:   Wed, 11 Apr 2018 11:51:01 +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.

Diffstat:
ff2jpg.c | 11+----------
ff2pam.c | 14++------------
ff2png.c | 9+--------
ff2ppm.c | 18++++--------------
jpg2ff.c | 5+----
png2ff.c | 14++------------
util.c | 49++++++++++++++++++++++++++++++++-----------------
util.h | 3+++
8 files changed, 46 insertions(+), 77 deletions(-)

diff --git a/ff2jpg.c b/ff2jpg.c @@ -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 @@ -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 @@ -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 @@ -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++) { @@ -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 @@ -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 @@ -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 @@ -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); } @@ -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 @@ -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; } @@ -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); } @@ -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 @@ -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);