From 12fd317a7bd8bc6c24d034227076fbe87dfe6e31 Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Mon, 6 Feb 2012 13:22:28 +0000 Subject: cSocket: error reporting function now works correctly on GNU Linux; also includes the error number for all errors git-svn-id: http://mc-server.googlecode.com/svn/trunk@236 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/cSocket.cpp | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/source/cSocket.cpp b/source/cSocket.cpp index bb6f68249..323ed59c8 100644 --- a/source/cSocket.cpp +++ b/source/cSocket.cpp @@ -94,22 +94,43 @@ void cSocket::CloseSocket() AString cSocket::GetErrorString( int a_ErrNo ) { + char buffer[ 1024 ]; + AString Out; + #ifdef _WIN32 - char Buffer[1024]; - FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, a_ErrNo, 0, Buffer, ARRAYCOUNT(Buffer), NULL); - return AString(Buffer); + FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, a_ErrNo, 0, buffer, ARRAYCOUNT(buffer), NULL); + Printf(Out, "%d: %s", a_ErrNo, buffer); + return Out; #else // _WIN32 - - char buffer[ 256 ]; - if( strerror_r( errno, buffer, 256 ) == 0 ) + + // According to http://linux.die.net/man/3/strerror_r there are two versions of strerror_r(): + + #if (((_POSIX_C_SOURCE >= 200112L) || (_XOPEN_SOURCE >= 600)) && ! _GNU_SOURCE) // XSI version of strerror_r(): + + int res = strerror_r( errno, buffer, ARRAYCOUNT(buffer) ); + if( res == 0 ) { - return AString( buffer ); + Printf(Out, "%d: %s", a_ErrNo, buffer); + return Out; } + + #else // GNU version of strerror_r() + + char * res = strerror_r( errno, buffer, ARRAYCOUNT(buffer) ); + if( res != NULL ) + { + Printf(Out, "%d: %s", a_ErrNo, res); + return Out; + } + + #endif // strerror_r() version + else { - return "Error on getting error string!"; + Printf(Out, "Error %d while getting error string for error #%d!", errno, a_ErrNo); + return Out; } #endif // else _WIN32 -- cgit v1.2.3