summaryrefslogtreecommitdiffhomepage
path: root/compat/strerror.cpp
blob: 0afb5f7d18810e6845a93ce198fcab34c1be51b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "strerror.hpp"
#include <errno.h>
#include <string.h>

namespace floormat {

StringView get_error_string(ArrayView<char> buf)
{
#ifndef _WIN32
#if defined __GLIBC__ && !((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE)
    char* str = ::strerror_r(errno, buf.data(), buf.size());
    if (str)
        return str;
#else
    int status { ::strerror_r(errno, buf.data(), buf.size()) };
    if (status == 0)
        return buf;
#endif

#else
    ::strerror_s(buf.data(), buf.size(), errno);
    if (buf[0])
        return buf;
#endif

    return "Unknown error"_s;
};

} // namespace floormat