diff --git a/tools/common.h b/tools/common.h index 60c5c1677..3664703ea 100644 --- a/tools/common.h +++ b/tools/common.h @@ -11,6 +11,8 @@ #include #include +#define error_exit(...) exit((fprintf(stderr, __VA_ARGS__), 1)) + int getopt_long_index; #define getopt_long(argc, argv, optstring, longopts) getopt_long(argc, argv, optstring, longopts, &getopt_long_index) @@ -18,8 +20,7 @@ void *malloc_verbose(size_t size) { errno = 0; void *m = malloc(size); if (!m) { - fprintf(stderr, "Could not allocate %zu bytes: %s\n", size, strerror(errno)); - exit(1); + error_exit("Could not allocate %zu bytes: %s\n", size, strerror(errno)); } return m; } @@ -29,8 +30,7 @@ FILE *fopen_verbose(const char *filename, char rw) { errno = 0; FILE *f = fopen(filename, mode); if (!f) { - fprintf(stderr, "Could not open file \"%s\": %s\n", filename, strerror(errno)); - exit(1); + error_exit("Could not open file \"%s\": %s\n", filename, strerror(errno)); } return f; } @@ -38,18 +38,16 @@ FILE *fopen_verbose(const char *filename, char rw) { void fread_verbose(uint8_t *data, size_t size, const char *filename, FILE *f) { errno = 0; if (fread(data, 1, size, f) != size) { - fprintf(stderr, "Could not read from file \"%s\": %s\n", filename, strerror(errno)); fclose(f); - exit(1); + error_exit("Could not read from file \"%s\": %s\n", filename, strerror(errno)); } } void fwrite_verbose(const uint8_t *data, size_t size, const char *filename, FILE *f) { errno = 0; if (fwrite(data, 1, size, f) != size) { - fprintf(stderr, "Could not write to file \"%s\": %s\n", filename, strerror(errno)); fclose(f); - exit(1); + error_exit("Could not write to file \"%s\": %s\n", filename, strerror(errno)); } } @@ -63,8 +61,7 @@ long file_size_verbose(const char *filename, FILE *f) { } } if (size == -1) { - fprintf(stderr, "Could not measure file \"%s\": %s\n", filename, strerror(errno)); - exit(1); + error_exit("Could not measure file \"%s\": %s\n", filename, strerror(errno)); } return size; } @@ -94,9 +91,8 @@ uint32_t read_png_width_verbose(const char *filename) { 'I', 'H', 'D', 'R', // IHDR chunk type }; if (memcmp(header, expected_header, sizeof(header))) { - fprintf(stderr, "Not a valid PNG file: \"%s\"\n", filename); fclose(f); - exit(1); + error_exit("Not a valid PNG file: \"%s\"\n", filename); } uint8_t bytes[4] = {0}; fread_verbose(bytes, sizeof(bytes), filename, f); diff --git a/tools/png_dimensions.c b/tools/png_dimensions.c index dd71bb878..f257d0e2e 100644 --- a/tools/png_dimensions.c +++ b/tools/png_dimensions.c @@ -7,8 +7,7 @@ void usage() { uint8_t read_dimensions(const char *filename) { uint32_t width_px = read_png_width_verbose(filename); if (width_px != 40 && width_px != 48 && width_px != 56) { - fprintf(stderr, "Not a valid width for \"%s\": %" PRIu32 " px\n", filename, width_px); - exit(1); + error_exit("Not a valid width for \"%s\": %" PRIu32 " px\n", filename, width_px); } uint8_t width_tiles = (uint8_t)(width_px / 8); return (width_tiles << 4) | width_tiles; diff --git a/tools/scan_includes.c b/tools/scan_includes.c index a6351f7e7..78c0cf27e 100644 --- a/tools/scan_includes.c +++ b/tools/scan_includes.c @@ -9,8 +9,7 @@ void scan_file(const char *filename, bool strict) { FILE *f = fopen(filename, "rb"); if (!f) { if (strict) { - fprintf(stderr, "Could not open file \"%s\": %s\n", filename, strerror(errno)); - exit(1); + error_exit("Could not open file \"%s\": %s\n", filename, strerror(errno)); } else { return; }