summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorLaslo Hunhold <dev@frign.de>2018-04-11 11:51:01 +0200
committerLaslo Hunhold <dev@frign.de>2018-04-11 11:51:01 +0200
commit02b051634969f701ccee57505140c9e7f334e22c (patch)
tree5fe33152959cbe29996754dee7529ccc07bf9440 /util.c
parenta9877de22960f14fb16e862cf46290a477779e98 (diff)
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 (limited to 'util.c')
-rw-r--r--util.c49
1 files changed, 32 insertions, 17 deletions
diff --git a/util.c b/util.c
index f9cf33e..212f595 100644
--- 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);
}