2022-04-20 16:17:19 +02:00
|
|
|
// Copyright (c) 2020-2022 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#if defined(HAVE_CONFIG_H)
|
|
|
|
#include <config/bitcoin-config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <tinyformat.h>
|
|
|
|
#include <util/syserror.h>
|
|
|
|
|
|
|
|
#include <cstring>
|
2022-06-09 16:26:55 +01:00
|
|
|
#include <string>
|
2022-04-20 16:17:19 +02:00
|
|
|
|
|
|
|
std::string SysErrorString(int err)
|
|
|
|
{
|
2022-04-21 18:39:56 +02:00
|
|
|
char buf[1024];
|
2022-04-20 16:43:07 +02:00
|
|
|
/* Too bad there are three incompatible implementations of the
|
2022-04-20 16:17:19 +02:00
|
|
|
* thread-safe strerror. */
|
2022-04-20 19:41:30 +02:00
|
|
|
const char *s = nullptr;
|
2022-04-20 16:43:07 +02:00
|
|
|
#ifdef WIN32
|
2022-04-20 19:41:30 +02:00
|
|
|
if (strerror_s(buf, sizeof(buf), err) == 0) s = buf;
|
2022-04-20 16:43:07 +02:00
|
|
|
#else
|
2022-04-20 16:17:19 +02:00
|
|
|
#ifdef STRERROR_R_CHAR_P /* GNU variant can return a pointer outside the passed buffer */
|
|
|
|
s = strerror_r(err, buf, sizeof(buf));
|
|
|
|
#else /* POSIX variant always returns message in buffer */
|
2022-04-20 19:41:30 +02:00
|
|
|
if (strerror_r(err, buf, sizeof(buf)) == 0) s = buf;
|
2022-04-20 16:43:07 +02:00
|
|
|
#endif
|
2022-04-20 16:17:19 +02:00
|
|
|
#endif
|
2022-04-20 19:41:30 +02:00
|
|
|
if (s != nullptr) {
|
|
|
|
return strprintf("%s (%d)", s, err);
|
|
|
|
} else {
|
|
|
|
return strprintf("Unknown error (%d)", err);
|
|
|
|
}
|
2022-04-20 16:17:19 +02:00
|
|
|
}
|