aboutsummaryrefslogtreecommitdiff
path: root/lib/strerror.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/strerror.c')
-rw-r--r--lib/strerror.c67
1 files changed, 42 insertions, 25 deletions
diff --git a/lib/strerror.c b/lib/strerror.c
index a85c216a5..83a96dda1 100644
--- a/lib/strerror.c
+++ b/lib/strerror.c
@@ -49,6 +49,10 @@
#include "curl_memory.h"
#include "memdebug.h"
+#if defined(WIN32) || defined(_WIN32_WCE)
+#define PRESERVE_WINDOWS_ERROR_CODE
+#endif
+
const char *
curl_easy_strerror(CURLcode error)
{
@@ -432,6 +436,10 @@ curl_share_strerror(CURLSHcode error)
static const char *
get_winsock_error (int err, char *buf, size_t len)
{
+#ifdef PRESERVE_WINDOWS_ERROR_CODE
+ DWORD old_win_err = GetLastError();
+#endif
+ int old_errno = errno;
const char *p;
#ifndef CURL_DISABLE_VERBOSE_STRINGS
@@ -611,6 +619,15 @@ get_winsock_error (int err, char *buf, size_t len)
#endif
strncpy(buf, p, len);
buf [len-1] = '\0';
+
+ if(errno != old_errno)
+ errno = old_errno;
+
+#ifdef PRESERVE_WINDOWS_ERROR_CODE
+ if(old_win_err != GetLastError())
+ SetLastError(old_win_err);
+#endif
+
return buf;
}
#endif /* USE_WINSOCK */
@@ -628,19 +645,16 @@ get_winsock_error (int err, char *buf, size_t len)
*/
const char *Curl_strerror(struct connectdata *conn, int err)
{
+#ifdef PRESERVE_WINDOWS_ERROR_CODE
+ DWORD old_win_err = GetLastError();
+#endif
+ int old_errno = errno;
char *buf, *p;
size_t max;
- int old_errno;
-#ifdef WIN32
- DWORD old_win_err;
-#endif
+
DEBUGASSERT(conn);
DEBUGASSERT(err >= 0);
- old_errno = errno;
-#ifdef WIN32
- old_win_err = GetLastError();
-#endif
buf = conn->syserr_buf;
max = sizeof(conn->syserr_buf)-1;
*buf = '\0';
@@ -727,12 +741,14 @@ const char *Curl_strerror(struct connectdata *conn, int err)
p = strrchr(buf, '\r');
if(p && (p - buf) >= 1)
*p = '\0';
-#ifdef WIN32
+
+ if(errno != old_errno)
+ errno = old_errno;
+
+#ifdef PRESERVE_WINDOWS_ERROR_CODE
if(old_win_err != GetLastError())
SetLastError(old_win_err);
#endif
- if(errno != old_errno)
- errno = old_errno;
return buf;
}
@@ -740,17 +756,19 @@ const char *Curl_strerror(struct connectdata *conn, int err)
#ifdef USE_WINDOWS_SSPI
const char *Curl_sspi_strerror (struct connectdata *conn, int err)
{
+#ifdef PRESERVE_WINDOWS_ERROR_CODE
+ DWORD old_win_err = GetLastError();
+#endif
+ int old_errno = errno;
+ const char *txt;
+ char *outbuf;
+ size_t outmax;
#ifndef CURL_DISABLE_VERBOSE_STRINGS
char txtbuf[80];
char msgbuf[sizeof(conn->syserr_buf)];
char *p, *str, *msg = NULL;
bool msg_formatted = FALSE;
- int old_errno;
- DWORD old_win_err;
#endif
- const char *txt;
- char *outbuf;
- size_t outmax;
DEBUGASSERT(conn);
@@ -760,9 +778,6 @@ const char *Curl_sspi_strerror (struct connectdata *conn, int err)
#ifndef CURL_DISABLE_VERBOSE_STRINGS
- old_errno = errno;
- old_win_err = GetLastError();
-
switch(err) {
case SEC_E_OK:
txt = "No error";
@@ -1062,12 +1077,6 @@ const char *Curl_sspi_strerror (struct connectdata *conn, int err)
strncpy(outbuf, str, outmax);
}
- if(old_win_err != GetLastError())
- SetLastError(old_win_err);
-
- if(errno != old_errno)
- errno = old_errno;
-
#else
if(err == SEC_E_OK)
@@ -1081,6 +1090,14 @@ const char *Curl_sspi_strerror (struct connectdata *conn, int err)
outbuf[outmax] = '\0';
+ if(errno != old_errno)
+ errno = old_errno;
+
+#ifdef PRESERVE_WINDOWS_ERROR_CODE
+ if(old_win_err != GetLastError())
+ SetLastError(old_win_err);
+#endif
+
return outbuf;
}
#endif /* USE_WINDOWS_SSPI */