Add warn() and die() - farbfeld - suckless image format with conversion tools | |
git clone git://git.suckless.org/farbfeld | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit d9c382a9d873c8758f518c6aa7e6c32cf3ea7b89 | |
parent a2e07745e6e31231dd59bd1d4a3415a5f4042d0c | |
Author: Laslo Hunhold <[email protected]> | |
Date: Wed, 11 Apr 2018 12:48:50 +0200 | |
Add warn() and die() | |
To fully centralize this matter these well-tested functions are added to | |
the util.c, and implemented as we know it from many other suckless | |
projects. | |
Diffstat: | |
M ff2jpg.c | 3 +-- | |
M ff2pam.c | 3 +-- | |
M ff2png.c | 9 +++------ | |
M ff2ppm.c | 3 +-- | |
M jpg2ff.c | 3 +-- | |
M png2ff.c | 12 ++++-------- | |
M util.c | 65 +++++++++++++++++++++++------… | |
M util.h | 5 ++++- | |
8 files changed, 63 insertions(+), 40 deletions(-) | |
--- | |
diff --git a/ff2jpg.c b/ff2jpg.c | |
@@ -47,8 +47,7 @@ jpeg_setup_writer(struct jpeg_compress_struct *s, struct jpeg… | |
static void | |
usage(void) | |
{ | |
- fprintf(stderr, "usage: %s [-b colour] [-o] [-q quality]\n", argv0); | |
- exit(1); | |
+ die("usage: %s [-b colour] [-o] [-q quality]", argv0); | |
} | |
int | |
diff --git a/ff2pam.c b/ff2pam.c | |
@@ -14,8 +14,7 @@ | |
static void | |
usage(void) | |
{ | |
- fprintf(stderr, "usage: %s\n", argv0); | |
- exit(1); | |
+ die("usage: %s", argv0); | |
} | |
int | |
diff --git a/ff2png.c b/ff2png.c | |
@@ -15,8 +15,7 @@ static void | |
png_err(png_struct *pngs, const char *msg) | |
{ | |
(void)pngs; | |
- fprintf(stderr, "%s: libpng: %s\n", argv0, msg); | |
- exit(1); | |
+ die("libpng: %s", msg); | |
} | |
static void | |
@@ -26,8 +25,7 @@ png_setup_writer(png_struct **s, png_info **i, uint32_t w, ui… | |
*i = png_create_info_struct(*s); | |
if (!*s || !*i) { | |
- fprintf(stderr, "%s: failed to initialize libpng\n", argv0); | |
- exit(1); | |
+ die("Failed to initialize libpng"); | |
} | |
png_init_io(*s, stdout); | |
@@ -40,8 +38,7 @@ png_setup_writer(png_struct **s, png_info **i, uint32_t w, ui… | |
static void | |
usage(void) | |
{ | |
- fprintf(stderr, "usage: %s\n", argv0); | |
- exit(1); | |
+ die("usage: %s", argv0); | |
} | |
int | |
diff --git a/ff2ppm.c b/ff2ppm.c | |
@@ -14,8 +14,7 @@ | |
static void | |
usage(void) | |
{ | |
- fprintf(stderr, "usage: %s [-b colour]\n", argv0); | |
- exit(1); | |
+ die("usage: %s [-b colour]", argv0); | |
} | |
int | |
diff --git a/jpg2ff.c b/jpg2ff.c | |
@@ -40,8 +40,7 @@ jpeg_setup_reader(struct jpeg_decompress_struct *s, struct jp… | |
static void | |
usage(void) | |
{ | |
- fprintf(stderr, "usage: %s\n", argv0); | |
- exit(1); | |
+ die("usage: %s", argv0); | |
} | |
int | |
diff --git a/png2ff.c b/png2ff.c | |
@@ -15,8 +15,7 @@ static void | |
png_err(png_struct *pngs, const char *msg) | |
{ | |
(void)pngs; | |
- fprintf(stderr, "%s: libpng: %s\n", argv0, msg); | |
- exit(1); | |
+ die("libpng: %s", msg); | |
} | |
static void | |
@@ -26,8 +25,7 @@ png_setup_reader(png_struct **s, png_info **i, uint32_t *w, u… | |
*i = png_create_info_struct(*s); | |
if (!*s || !*i) { | |
- fprintf(stderr, "%s: failed to initialize libpng\n", argv0); | |
- exit(1); | |
+ die("Failed to initialize libpng"); | |
} | |
png_init_io(*s, stdin); | |
@@ -46,8 +44,7 @@ png_setup_reader(png_struct **s, png_info **i, uint32_t *w, u… | |
static void | |
usage(void) | |
{ | |
- fprintf(stderr, "usage: %s\n", argv0); | |
- exit(1); | |
+ die("usage: %s", argv0); | |
} | |
int | |
@@ -90,8 +87,7 @@ main(int argc, char *argv[]) | |
} | |
break; | |
default: | |
- fprintf(stderr, "%s: invalid bit-depth\n", argv0); | |
- return 1; | |
+ die("Invalid bit-depth"); | |
} | |
/* clean up */ | |
diff --git a/util.c b/util.c | |
@@ -3,6 +3,7 @@ | |
#include <errno.h> | |
#include <limits.h> | |
+#include <stdarg.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
@@ -13,6 +14,45 @@ | |
char *argv0; | |
+static void | |
+verr(const char *fmt, va_list ap) | |
+{ | |
+ if (argv0 && strncmp(fmt, "usage", sizeof("usage") - 1)) { | |
+ fprintf(stderr, "%s: ", argv0); | |
+ } | |
+ | |
+ vfprintf(stderr, fmt, ap); | |
+ | |
+ if (fmt[0] && fmt[strlen(fmt) - 1] == ':') { | |
+ fputc(' ', stderr); | |
+ perror(NULL); | |
+ } else { | |
+ fputc('\n', stderr); | |
+ } | |
+} | |
+ | |
+void | |
+warn(const char *fmt, ...) | |
+{ | |
+ va_list ap; | |
+ | |
+ va_start(ap, fmt); | |
+ verr(fmt, ap); | |
+ va_end(ap); | |
+} | |
+ | |
+void | |
+die(const char *fmt, ...) | |
+{ | |
+ va_list ap; | |
+ | |
+ va_start(ap, fmt); | |
+ verr(fmt, ap); | |
+ va_end(ap); | |
+ | |
+ exit(1); | |
+} | |
+ | |
void | |
ff_read_header(uint32_t *width, uint32_t *height) | |
{ | |
@@ -21,8 +61,7 @@ ff_read_header(uint32_t *width, uint32_t *height) | |
efread(hdr, sizeof(*hdr), LEN(hdr), stdin); | |
if (memcmp("farbfeld", hdr, sizeof("farbfeld") - 1)) { | |
- fprintf(stderr, "%s: Invalid magic value\n", argv0); | |
- exit(1); | |
+ die("Invalid magic value"); | |
} | |
*width = ntohl(hdr[2]); | |
@@ -84,14 +123,12 @@ fshut(FILE *fp, const char *fname) | |
fflush(fp); | |
if (ferror(fp) && !ret) { | |
- fprintf(stderr, "%s: ferror '%s': %s\n", argv0, fname, | |
- strerror(errno)); | |
+ warn("ferror '%s':", fname); | |
ret = 1; | |
} | |
if (fclose(fp) && !ret) { | |
- fprintf(stderr, "%s: fclose '%s': %s\n", argv0, fname, | |
- strerror(errno)); | |
+ warn("fclose '%s':", fname); | |
ret = 1; | |
} | |
@@ -103,13 +140,10 @@ 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)); | |
+ die("fread:"); | |
} else { | |
- fprintf(stderr, "%s: fread: Unexpected end of file\n", | |
- argv0); | |
+ die("fread: Unexpected end of file"); | |
} | |
- exit(1); | |
} | |
} | |
@@ -117,8 +151,7 @@ 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); | |
+ die("fwrite:"); | |
} | |
} | |
@@ -128,8 +161,7 @@ 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); | |
- exit(1); | |
+ die("reallocarray: Out of memory"); | |
} | |
return p; | |
@@ -143,8 +175,7 @@ estrtonum(const char *numstr, long long minval, long long m… | |
ll = strtonum(numstr, minval, maxval, &errstr); | |
if (errstr) { | |
- fprintf(stderr, "%s: strtonum '%s': %s\n", argv0, numstr, errs… | |
- exit(1); | |
+ die("strtonum '%s': %s", numstr, errstr); | |
} | |
return ll; | |
diff --git a/util.h b/util.h | |
@@ -2,9 +2,12 @@ | |
#include <stdint.h> | |
#include <stdio.h> | |
+#define LEN(x) (sizeof (x) / sizeof *(x)) | |
+ | |
extern char *argv0; | |
-#define LEN(x) (sizeof (x) / sizeof *(x)) | |
+void warn(const char *, ...); | |
+void die(const char *, ...); | |
void ff_read_header(uint32_t *width, uint32_t *height); | |
void ff_write_header(uint32_t width, uint32_t height); |