summaryrefslogtreecommitdiffhomepage
path: root/compat/strerror.cpp
blob: 8b2371538d70a6c65ef9777a72e543fbeddf9e44 (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
30
31
#include "strerror.hpp"
#include <cerrno>
#include <string.h>

namespace floormat {

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

    return "Unknown error"_s;
};

StringView get_error_string(ArrayView<char> buf)
{
    return get_error_string(buf, errno);
}

} // namespace floormat