Skip to content

Commit

Permalink
Add warn() and die()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
FRIGN committed Apr 11, 2018
1 parent a2e0774 commit d9c382a
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 40 deletions.
3 changes: 1 addition & 2 deletions ff2jpg.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ jpeg_setup_writer(struct jpeg_compress_struct *s, struct jpeg_error_mgr *e,
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
Expand Down
3 changes: 1 addition & 2 deletions ff2pam.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
static void
usage(void)
{
fprintf(stderr, "usage: %s\n", argv0);
exit(1);
die("usage: %s", argv0);
}

int
Expand Down
9 changes: 3 additions & 6 deletions ff2png.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,8 +25,7 @@ png_setup_writer(png_struct **s, png_info **i, uint32_t w, uint32_t h)
*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);
Expand All @@ -40,8 +38,7 @@ png_setup_writer(png_struct **s, png_info **i, uint32_t w, uint32_t h)
static void
usage(void)
{
fprintf(stderr, "usage: %s\n", argv0);
exit(1);
die("usage: %s", argv0);
}

int
Expand Down
3 changes: 1 addition & 2 deletions ff2ppm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions jpg2ff.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ jpeg_setup_reader(struct jpeg_decompress_struct *s, struct jpeg_error_mgr *e,
static void
usage(void)
{
fprintf(stderr, "usage: %s\n", argv0);
exit(1);
die("usage: %s", argv0);
}

int
Expand Down
12 changes: 4 additions & 8 deletions png2ff.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,8 +25,7 @@ png_setup_reader(png_struct **s, png_info **i, uint32_t *w, uint32_t *h)
*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);
Expand All @@ -46,8 +44,7 @@ png_setup_reader(png_struct **s, png_info **i, uint32_t *w, uint32_t *h)
static void
usage(void)
{
fprintf(stderr, "usage: %s\n", argv0);
exit(1);
die("usage: %s", argv0);
}

int
Expand Down Expand Up @@ -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 */
Expand Down
65 changes: 48 additions & 17 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -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)
{
Expand All @@ -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]);
Expand Down Expand Up @@ -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;
}

Expand All @@ -103,22 +140,18 @@ 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);
}
}

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:");
}
}

Expand All @@ -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;
Expand All @@ -143,8 +175,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);
exit(1);
die("strtonum '%s': %s", numstr, errstr);
}

return ll;
Expand Down
5 changes: 4 additions & 1 deletion util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d9c382a

Please sign in to comment.