Changed the err and warns to macros

err and warn are BSD format but they are not recommended by library
developers.  However their consiseness is useful!

The solution is to use some macros that create xerr etc which then
just map to the error() function.  The next problem is error() uses
program_invocation_name so we set this to program_invovation_short_name

This is a global set but seems to be the convention (or at least errors
are on the short name only) used everywhere else.
This commit is contained in:
Craig Small
2012-01-03 18:48:43 +11:00
parent 76b3e91e6a
commit fb11e1fe0a
15 changed files with 100 additions and 147 deletions

View File

@ -17,10 +17,6 @@
#include <string.h>
#include <errno.h>
#ifdef HAVE_ERR_H
# include <err.h>
#endif
/*
* Compiler specific stuff
*/
@ -106,43 +102,10 @@ static inline char *prog_inv_sh_nm_from_file(char *f, char stripext)
/*
* Error printing.
*/
#ifndef HAVE_ERR_H
static inline void
errmsg(char doexit, int excode, char adderr, const char *fmt, ...)
{
fprintf(stderr, "%s: ", program_invocation_short_name);
if (fmt != NULL) {
va_list argp;
va_start(argp, fmt);
vfprintf(stderr, fmt, argp);
va_end(argp);
if (adderr)
fprintf(stderr, ": ");
}
if (adderr)
fprintf(stderr, "%m");
fprintf(stderr, "\n");
if (doexit)
exit(excode);
}
# ifndef HAVE_ERR
# define err(E, FMT...) errmsg(1, E, 1, FMT)
# endif
# ifndef HAVE_ERRX
# define errx(E, FMT...) errmsg(1, E, 0, FMT)
# endif
# ifndef HAVE_WARN
# define warn(FMT...) errmsg(0, 0, 1, FMT)
# endif
# ifndef HAVE_WARNX
# define warnx(FMT...) errmsg(0, 0, 0, FMT)
# endif
#endif /* !HAVE_ERR_H */
#define xwarn(FMT...) error(0, errno, FMT)
#define xwarnx(FMT...) error(0, 0, FMT)
#define xerr(STATUS, FMT...) error(STATUS, errno, FMT)
#define xerrx(STATUS, FMT...) error(STATUS, 0, FMT)
/*
* Constant strings for usage() functions.