X-Git-Url: http://git.thejh.net/?p=libjh.git;a=blobdiff_plain;f=error.c;fp=error.c;h=9727253c9e20dca5a7ff219570e4dcebf27f988c;hp=0000000000000000000000000000000000000000;hb=66aa58de83d6cf9dd905b1857386e89409defb88;hpb=9e73c19b7eb834507b9e62cb84b07ca53ad7a914 diff --git a/error.c b/error.c new file mode 100644 index 0000000..9727253 --- /dev/null +++ b/error.c @@ -0,0 +1,34 @@ +// Copyright (2013) Jann Horn +// This code is licensed under the AGPLv3. + +// This file contains stuff for making error-handling easier. + +#include + +#define GENERIC_ERROR \ + "unexpected generic failure of some kind" + +PUBLIC_FN void xperror(const char *s, int show_errno) { + if (!s) s=GENERIC_ERROR; + if (show_errno) { + perror(s); + } else { + fprintf(stderr, "%s\n", s); + } + exit(1); +} + +PUBLIC_FN void *fail_on_npointer(void *p, char *msg, int show_errno) { + if (!p) xperror(msg, show_errno); + return p; +} + +HEADER #define CHK_PTR(p, msg, show_errno) ( \ +HEADER (typeof (p)) \ +HEADER fail_on_npointer((p), (msg), (show_errno)) \ +HEADER ) + +PUBLIC_FN int fail_on_neg(int n, char *msg, int show_errno) { + if ((n)<0) xperror(msg, show_errno); + return n; +}