Kragen's err.h
I was porting boggle from NetBSD to Linux. It used several functions that
weren't in my run-time library, evidently having been introduced in 4.4BSD.
They were useful little functions -- things I'd written myself more than
once, usually under a different name, because they're very useful. So,
instead of looking for some source code on the Net for them, I just quickly
rewrote them myself.
Here's release 1.
Briefly, they're sort of like a C equivalent of the warn() and die()
functions in Perl.
The original BSD versions print the program's name followed by a colon before
anything else. Unfortunately, there's no portable way to do this, so these
quick-and-dirty substitutes just print their own names.
All of them print newlines at the ends of their messages.
- void err(int eval, const char * fmt, ...) passes 'fmt' and the ... arguments
to fprintf(stderr), prints an error corresponding to the current
value of errno, and exits with 'eval'. This is very useful!
Example:
if ((fd = open(fname, O_RDONLY)) == -1) err(1, "open: %s", fname);
might print:
verr: open: example.c: No such file or directory
and make the program exit() with exit value 1.
- void verr(int eval, const char * fmt, va_list args) is the va_list version
of err().
- void errx(int eval, const char * fmt, ...) is just like err(), but it doesn't
print the error corresponding to errno. Example:
if ((x = malloc(size)) == NULL) errx(-1, "malloc(%d): no memory", size);
might print:
verrx: malloc(2048): no memory
and make the program exit() with exit value -1.
- void verrx(int eval, const char * fmt, va_list args) is the va_list version
of errx().
- void warn(const char * fmt, ...) is the same as err(), except that it doesn't
make the program exit().
- void vwarn(const char * fmt, va_list args) is the va_list version of warn().
- void warnx(const char * fmt, ...) is the same as errx(), except that it
doesn't make the program exit().
- void vwarnx(const char * fmt, va_list args) is the va_list version of
warnx().