make sigar_strerror usable internally w/o sigar_t

This commit is contained in:
Doug MacEachern 2007-01-03 22:47:00 +00:00
parent 8f837228b3
commit 07c0cb7ff0
2 changed files with 17 additions and 10 deletions

View File

@ -146,6 +146,8 @@ int sigar_os_close(sigar_t *sigar);
char *sigar_os_error_string(sigar_t *sigar, int err);
char *sigar_strerror_get(int err, char *errbuf, int buflen);
int sigar_sys_info_get_uname(sigar_sys_info_t *sysinfo);
int sigar_os_sys_info_get(sigar_t *sigar, sigar_sys_info_t *sysinfo);

View File

@ -114,10 +114,7 @@ static char *sigar_error_string(int err)
SIGAR_DECLARE(char *) sigar_strerror(sigar_t *sigar, int err)
{
char *buf = NULL;
#ifdef WIN32
DWORD len;
#endif
char *buf;
if (err > SIGAR_OS_START_ERROR) {
if ((buf = sigar_os_error_string(sigar, err)) != NULL) {
@ -130,14 +127,22 @@ SIGAR_DECLARE(char *) sigar_strerror(sigar_t *sigar, int err)
return sigar_error_string(err);
}
return sigar_strerror_get(err, sigar->errbuf, sizeof(sigar->errbuf));
}
char *sigar_strerror_get(int err, char *errbuf, int buflen)
{
char *buf = NULL;
#ifdef WIN32
DWORD len;
len = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
err,
0, /* default language */
(LPTSTR)sigar->errbuf,
(DWORD)sizeof(sigar->errbuf),
(LPTSTR)errbuf,
(DWORD)buflen,
NULL);
#else
@ -146,9 +151,9 @@ SIGAR_DECLARE(char *) sigar_strerror(sigar_t *sigar, int err)
* strerror_r man page says:
* "The GNU version may, but need not, use the user supplied buffer"
*/
buf = strerror_r(err, sigar->errbuf, sizeof(sigar->errbuf));
buf = strerror_r(err, errbuf, buflen);
#elif defined(HAVE_STRERROR_R)
if (strerror_r(err, sigar->errbuf, sizeof(sigar->errbuf)) < 0) {
if (strerror_r(err, errbuf, buflen) < 0) {
buf = "Unknown Error";
}
#else
@ -157,11 +162,11 @@ SIGAR_DECLARE(char *) sigar_strerror(sigar_t *sigar, int err)
#endif
if (buf != NULL) {
SIGAR_SSTRCPY(sigar->errbuf, buf);
SIGAR_STRNCPY(errbuf, buf, buflen);
}
#endif
return sigar->errbuf;
return errbuf;
}
#include <stdio.h> /* for sprintf */