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

@ -15,19 +15,14 @@ long strtol_or_err(const char *str, const char *errmesg)
long num;
char *end = NULL;
if (str == NULL || *str == '\0')
goto err;
errno = 0;
num = strtol(str, &end, 10);
if (errno || str == end || (end && *end))
goto err;
return num;
err:
if (errno)
err(EXIT_FAILURE, "%s: '%s'", errmesg, str);
else
errx(EXIT_FAILURE, "%s: '%s'", errmesg, str);
if (str != NULL && *str != '\0') {
errno = 0;
num = strtol(str, &end, 10);
if (errno == 0 && str != end && end != NULL && *end == '\0')
return num;
}
error(EXIT_FAILURE, errno, "%s: '%s'", errmesg, str);
return 0;
}
/*
* same as strtod(3) but exit on failure instead of returning crap
@ -37,20 +32,13 @@ double strtod_or_err(const char *str, const char *errmesg)
double num;
char *end = NULL;
if (str == NULL || *str == '\0')
goto err;
errno = 0;
num = strtod(str, &end);
if (errno || str == end || (end && *end))
goto err;
return num;
err:
if (errno)
err(EXIT_FAILURE, "%s: '%s'", errmesg, str);
else
errx(EXIT_FAILURE, "%s: '%s'", errmesg, str);
if (str != NULL && *str != '\0') {
errno = 0;
num = strtod(str, &end);
if (errno == 0 && str != end && end != NULL && *end == '\0')
return num;
}
error(EXIT_FAILURE, errno, "%s: '%s'", errmesg, str);
return 0;
}