diff options
Diffstat (limited to 'libntp/lib/isc/win32/strerror.c')
-rw-r--r-- | libntp/lib/isc/win32/strerror.c | 64 |
1 files changed, 49 insertions, 15 deletions
diff --git a/libntp/lib/isc/win32/strerror.c b/libntp/lib/isc/win32/strerror.c index 40c5da002f8c..21bdad97778e 100644 --- a/libntp/lib/isc/win32/strerror.c +++ b/libntp/lib/isc/win32/strerror.c @@ -29,6 +29,8 @@ #include <isc/strerror.h> #include <isc/util.h> +#include "lib_strbuf.h" + /* * Forward declarations */ @@ -88,19 +90,47 @@ isc__strerror(int num, char *buf, size_t size) { */ char * FormatError(int error) { - LPVOID lpMsgBuf = NULL; + char *lpMsgBuf = NULL; + char *pch; + const char boiler[] = + " For information about network troubleshooting, see Windows Help."; + size_t last; + FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, + (FORMAT_MESSAGE_MAX_WIDTH_MASK - 1), NULL, error, /* Default language */ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPTSTR) &lpMsgBuf, + (LPTSTR)(PVOID)&lpMsgBuf, 0, NULL); + /* remove useless boilerplate */ + pch = strstr(lpMsgBuf, boiler); + if (pch != NULL) { + *pch = '\0'; + } + + /* strip any trailing CR/LF and spaces */ + if (lpMsgBuf != NULL) { + last = strlen(lpMsgBuf); + if (last > 0) { + --last; + } + while ('\n' == lpMsgBuf[last] || + '\r' == lpMsgBuf[last] || + ' ' == lpMsgBuf[last]) { + + lpMsgBuf[last] = '\0'; + if (last > 0) { + --last; + } + } + } + return (lpMsgBuf); } @@ -113,27 +143,31 @@ char * NTstrerror(int err, BOOL *bfreebuf) { char *retmsg = NULL; - /* Copy the error value first in case of other errors */ - DWORD errval = err; - *bfreebuf = FALSE; /* Get the Winsock2 error messages */ - if (errval >= WSABASEERR && errval <= (WSABASEERR + 1999)) { - retmsg = GetWSAErrorMessage(errval); - if (retmsg != NULL) - return (retmsg); + /* DLH this may not be needed, FormatError/FormatMessage may handle Winsock error codes */ + if (err >= WSABASEERR && err <= (WSABASEERR + 1999)) { + retmsg = GetWSAErrorMessage(err); } /* * If it's not one of the standard Unix error codes, * try a system error message */ - if (errval > (DWORD) _sys_nerr) { - *bfreebuf = TRUE; - return (FormatError(errval)); - } else { - return (strerror(errval)); + if (NULL == retmsg) { + if (err > _sys_nerr) { + *bfreebuf = TRUE; + retmsg = FormatError(err); + } else { + retmsg = lib_getbuf(); + if (0 != strerror_s(retmsg, LIB_BUFLENGTH, err)) { + snprintf(retmsg, LIB_BUFLENGTH, + "Unknown error number %d/0x%x", + err, err); + } + } } + return retmsg; } /* |