Bitcoin Core  24.99.0
P2P Digital Currency
syserror.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2022 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #if defined(HAVE_CONFIG_H)
7 #endif
8 
9 #include <tinyformat.h>
10 #include <util/syserror.h>
11 
12 #include <cstring>
13 #include <string>
14 
15 std::string SysErrorString(int err)
16 {
17  char buf[1024];
18  /* Too bad there are three incompatible implementations of the
19  * thread-safe strerror. */
20  const char *s = nullptr;
21 #ifdef WIN32
22  if (strerror_s(buf, sizeof(buf), err) == 0) s = buf;
23 #else
24 #ifdef STRERROR_R_CHAR_P /* GNU variant can return a pointer outside the passed buffer */
25  s = strerror_r(err, buf, sizeof(buf));
26 #else /* POSIX variant always returns message in buffer */
27  if (strerror_r(err, buf, sizeof(buf)) == 0) s = buf;
28 #endif
29 #endif
30  if (s != nullptr) {
31  return strprintf("%s (%d)", s, err);
32  } else {
33  return strprintf("Unknown error (%d)", err);
34  }
35 }
std::string SysErrorString(int err)
Return system error string from errno value.
Definition: syserror.cpp:15
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164