W
William Ahern
So, I recently learned that Solaris doesn't, or doesn't seem, to provide
err.h. On some platforms err.h provides simple wrappers for error printing
to stderr. One of which, err(), has this signature:
void err(int exit_code, const char *fmt, ...);
It will print fmt + ... to stderr, tacking on a colon, the
result of strerror(errno), and a newline. Then it exits.
As a workaround, assuming that `fmt' will be passed as a string
literal, I came up w/ these macros to mimic err():
#define err__(ret, fmt, ...) do { \
fprintf(stderr, fmt ": %s\n", __VA_ARGS__); \
exit((ret)); \
} while(0)
#define err(ret, ...) \
err__(ret, __VA_ARGS__, strerror(errno))
This seems to work for me using two different compilers (GCC, TinyCC) on
two different platforms. Still, that doesn't resolve the standard
compliance issue. Any comments? I realize variable arguments macros were
defined by C99 only. Still, I'm curious whether this should have
well-defined behavior, as far as the standard is concerned.
- Bill
err.h. On some platforms err.h provides simple wrappers for error printing
to stderr. One of which, err(), has this signature:
void err(int exit_code, const char *fmt, ...);
It will print fmt + ... to stderr, tacking on a colon, the
result of strerror(errno), and a newline. Then it exits.
As a workaround, assuming that `fmt' will be passed as a string
literal, I came up w/ these macros to mimic err():
#define err__(ret, fmt, ...) do { \
fprintf(stderr, fmt ": %s\n", __VA_ARGS__); \
exit((ret)); \
} while(0)
#define err(ret, ...) \
err__(ret, __VA_ARGS__, strerror(errno))
This seems to work for me using two different compilers (GCC, TinyCC) on
two different platforms. Still, that doesn't resolve the standard
compliance issue. Any comments? I realize variable arguments macros were
defined by C99 only. Still, I'm curious whether this should have
well-defined behavior, as far as the standard is concerned.
- Bill