diff options
Diffstat (limited to 'libs/libcurl/src/strerror.c')
-rw-r--r-- | libs/libcurl/src/strerror.c | 130 |
1 files changed, 21 insertions, 109 deletions
diff --git a/libs/libcurl/src/strerror.c b/libs/libcurl/src/strerror.c index b9f29ea096..a48ab988aa 100644 --- a/libs/libcurl/src/strerror.c +++ b/libs/libcurl/src/strerror.c @@ -42,16 +42,13 @@ #include "curl_sspi.h"
#endif
+#include "curlx/winapi.h"
#include "strerror.h"
/* The last 3 #include files should be in this order */
#include "curl_printf.h"
#include "curl_memory.h"
#include "memdebug.h"
-#ifdef _WIN32
-#define PRESERVE_WINDOWS_ERROR_CODE
-#endif
-
const char *
curl_easy_strerror(CURLcode error)
{
@@ -782,50 +779,6 @@ get_winsock_error(int err, char *buf, size_t len) }
#endif /* USE_WINSOCK */
-#ifdef _WIN32
-/* This is a helper function for Curl_strerror that converts Windows API error
- * codes (GetLastError) to error messages.
- * Returns NULL if no error message was found for error code.
- */
-static const char *
-get_winapi_error(int err, char *buf, size_t buflen)
-{
- char *p;
- wchar_t wbuf[256];
-
- if(!buflen)
- return NULL;
-
- *buf = '\0';
- *wbuf = L'\0';
-
- /* We return the local codepage version of the error string because if it is
- output to the user's terminal it will likely be with functions which
- expect the local codepage (eg fprintf, failf, infof).
- FormatMessageW -> wcstombs is used for Windows CE compatibility. */
- if(FormatMessageW((FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS), NULL, (DWORD)err,
- LANG_NEUTRAL, wbuf, CURL_ARRAYSIZE(wbuf), NULL)) {
- size_t written = wcstombs(buf, wbuf, buflen - 1);
- if(written != (size_t)-1)
- buf[written] = '\0';
- else
- *buf = '\0';
- }
-
- /* Truncate multiple lines */
- p = strchr(buf, '\n');
- if(p) {
- if(p > buf && *(p-1) == '\r')
- *(p-1) = '\0';
- else
- *p = '\0';
- }
-
- return *buf ? buf : NULL;
-}
-#endif /* _WIN32 */
-
/*
* Our thread-safe and smart strerror() replacement.
*
@@ -847,7 +800,7 @@ get_winapi_error(int err, char *buf, size_t buflen) */
const char *Curl_strerror(int err, char *buf, size_t buflen)
{
-#ifdef PRESERVE_WINDOWS_ERROR_CODE
+#ifdef _WIN32
DWORD old_win_err = GetLastError();
#endif
int old_errno = errno;
@@ -866,16 +819,16 @@ const char *Curl_strerror(int err, char *buf, size_t buflen) #ifndef UNDER_CE
/* 'sys_nerr' is the maximum errno number, it is not widely portable */
if(err >= 0 && err < sys_nerr)
- msnprintf(buf, buflen, "%s", sys_errlist[err]);
+ curl_msnprintf(buf, buflen, "%s", sys_errlist[err]);
else
#endif
{
if(
#ifdef USE_WINSOCK
- !get_winsock_error(err, buf, buflen) &&
+ !get_winsock_error(err, buf, buflen) &&
#endif
- !get_winapi_error(err, buf, buflen))
- msnprintf(buf, buflen, "Unknown error %d (%#x)", err, err);
+ !curlx_get_winapi_error(err, buf, buflen))
+ curl_msnprintf(buf, buflen, "Unknown error %d (%#x)", err, err);
}
#else /* not Windows coming up */
@@ -887,7 +840,7 @@ const char *Curl_strerror(int err, char *buf, size_t buflen) */
if(0 != strerror_r(err, buf, buflen)) {
if('\0' == buf[0])
- msnprintf(buf, buflen, "Unknown error %d", err);
+ curl_msnprintf(buf, buflen, "Unknown error %d", err);
}
#elif defined(HAVE_STRERROR_R) && defined(HAVE_GLIBC_STRERROR_R)
/*
@@ -899,18 +852,18 @@ const char *Curl_strerror(int err, char *buf, size_t buflen) char buffer[256];
char *msg = strerror_r(err, buffer, sizeof(buffer));
if(msg)
- msnprintf(buf, buflen, "%s", msg);
+ curl_msnprintf(buf, buflen, "%s", msg);
else
- msnprintf(buf, buflen, "Unknown error %d", err);
+ curl_msnprintf(buf, buflen, "Unknown error %d", err);
}
#else
{
/* !checksrc! disable BANNEDFUNC 1 */
const char *msg = strerror(err);
if(msg)
- msnprintf(buf, buflen, "%s", msg);
+ curl_msnprintf(buf, buflen, "%s", msg);
else
- msnprintf(buf, buflen, "Unknown error %d", err);
+ curl_msnprintf(buf, buflen, "Unknown error %d", err);
}
#endif
@@ -927,54 +880,13 @@ const char *Curl_strerror(int err, char *buf, size_t buflen) if(errno != old_errno)
CURL_SETERRNO(old_errno);
-#ifdef PRESERVE_WINDOWS_ERROR_CODE
- if(old_win_err != GetLastError())
- SetLastError(old_win_err);
-#endif
-
- return buf;
-}
-
-/*
- * curlx_winapi_strerror:
- * Variant of Curl_strerror if the error code is definitely Windows API.
- */
#ifdef _WIN32
-const char *curlx_winapi_strerror(DWORD err, char *buf, size_t buflen)
-{
-#ifdef PRESERVE_WINDOWS_ERROR_CODE
- DWORD old_win_err = GetLastError();
-#endif
- int old_errno = errno;
-
- if(!buflen)
- return NULL;
-
- *buf = '\0';
-
-#ifndef CURL_DISABLE_VERBOSE_STRINGS
- if(!get_winapi_error((int)err, buf, buflen)) {
- msnprintf(buf, buflen, "Unknown error %lu (0x%08lX)", err, err);
- }
-#else
- {
- const char *txt = (err == ERROR_SUCCESS) ? "No error" : "Error";
- if(strlen(txt) < buflen)
- strcpy(buf, txt);
- }
-#endif
-
- if(errno != old_errno)
- CURL_SETERRNO(old_errno);
-
-#ifdef PRESERVE_WINDOWS_ERROR_CODE
if(old_win_err != GetLastError())
SetLastError(old_win_err);
#endif
return buf;
}
-#endif /* _WIN32 */
#ifdef USE_WINDOWS_SSPI
/*
@@ -983,7 +895,7 @@ const char *curlx_winapi_strerror(DWORD err, char *buf, size_t buflen) */
const char *Curl_sspi_strerror(int err, char *buf, size_t buflen)
{
-#ifdef PRESERVE_WINDOWS_ERROR_CODE
+#ifdef _WIN32
DWORD old_win_err = GetLastError();
#endif
int old_errno = errno;
@@ -1090,18 +1002,18 @@ const char *Curl_sspi_strerror(int err, char *buf, size_t buflen) }
if(err == SEC_E_ILLEGAL_MESSAGE) {
- msnprintf(buf, buflen,
- "SEC_E_ILLEGAL_MESSAGE (0x%08X) - This error usually occurs "
- "when a fatal SSL/TLS alert is received (e.g. handshake failed)."
- " More detail may be available in the Windows System event log.",
- err);
+ curl_msnprintf(buf, buflen,
+ "SEC_E_ILLEGAL_MESSAGE (0x%08X) - This error usually "
+ "occurs when a fatal SSL/TLS alert is received (e.g. "
+ "handshake failed). More detail may be available in "
+ "the Windows System event log.", err);
}
else {
char msgbuf[256];
- if(get_winapi_error(err, msgbuf, sizeof(msgbuf)))
- msnprintf(buf, buflen, "%s (0x%08X) - %s", txt, err, msgbuf);
+ if(curlx_get_winapi_error(err, msgbuf, sizeof(msgbuf)))
+ curl_msnprintf(buf, buflen, "%s (0x%08X) - %s", txt, err, msgbuf);
else
- msnprintf(buf, buflen, "%s (0x%08X)", txt, err);
+ curl_msnprintf(buf, buflen, "%s (0x%08X)", txt, err);
}
#else
@@ -1116,7 +1028,7 @@ const char *Curl_sspi_strerror(int err, char *buf, size_t buflen) if(errno != old_errno)
CURL_SETERRNO(old_errno);
-#ifdef PRESERVE_WINDOWS_ERROR_CODE
+#ifdef _WIN32
if(old_win_err != GetLastError())
SetLastError(old_win_err);
#endif
|