/src/abseil-cpp/absl/base/internal/strerror.cc
Line | Count | Source |
1 | | // Copyright 2020 The Abseil Authors. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include "absl/base/internal/strerror.h" |
16 | | |
17 | | #include <array> |
18 | | #include <cerrno> |
19 | | #include <cstddef> |
20 | | #include <cstdio> |
21 | | #include <cstring> |
22 | | #include <string> |
23 | | #include <type_traits> |
24 | | |
25 | | #include "absl/base/config.h" |
26 | | #include "absl/base/internal/errno_saver.h" |
27 | | |
28 | | namespace absl { |
29 | | ABSL_NAMESPACE_BEGIN |
30 | | namespace base_internal { |
31 | | namespace { |
32 | | |
33 | 0 | const char* StrErrorAdaptor(int errnum, char* buf, size_t buflen) { |
34 | | #if defined(_WIN32) |
35 | | int rc = strerror_s(buf, buflen, errnum); |
36 | | buf[buflen - 1] = '\0'; // guarantee NUL termination |
37 | | if (rc == 0 && strncmp(buf, "Unknown error", buflen) == 0) *buf = '\0'; |
38 | | return buf; |
39 | | #else |
40 | | // The type of `ret` is platform-specific; both of these branches must compile |
41 | | // either way but only one will execute on any given platform: |
42 | 0 | auto ret = strerror_r(errnum, buf, buflen); |
43 | 0 | if (std::is_same_v<decltype(ret), int>) { |
44 | | // XSI `strerror_r`; `ret` is `int`: |
45 | 0 | if (ret) *buf = '\0'; |
46 | 0 | return buf; |
47 | 0 | } else { |
48 | | // GNU `strerror_r`; `ret` is `char *`: |
49 | 0 | return reinterpret_cast<const char*>(ret); |
50 | 0 | } |
51 | 0 | #endif |
52 | 0 | } |
53 | | |
54 | 0 | std::string StrErrorInternal(int errnum) { |
55 | 0 | char buf[100]; |
56 | 0 | const char* str = StrErrorAdaptor(errnum, buf, sizeof buf); |
57 | 0 | if (*str == '\0') { |
58 | 0 | snprintf(buf, sizeof buf, "Unknown error %d", errnum); |
59 | 0 | str = buf; |
60 | 0 | } |
61 | 0 | return str; |
62 | 0 | } |
63 | | |
64 | | // kSysNerr is the number of errors from a recent glibc. `StrError()` falls back |
65 | | // to `StrErrorInternal()` if the value is larger than this. |
66 | | constexpr int kSysNerr = 135; |
67 | | |
68 | 0 | std::array<std::string, kSysNerr>* NewStrErrorTable() { |
69 | 0 | auto* table = new std::array<std::string, kSysNerr>; |
70 | 0 | for (size_t i = 0; i < table->size(); ++i) { |
71 | 0 | (*table)[i] = StrErrorInternal(static_cast<int>(i)); |
72 | 0 | } |
73 | 0 | return table; |
74 | 0 | } |
75 | | |
76 | | } // namespace |
77 | | |
78 | 0 | std::string StrError(int errnum) { |
79 | 0 | absl::base_internal::ErrnoSaver errno_saver; |
80 | 0 | static const auto* table = NewStrErrorTable(); |
81 | 0 | if (errnum >= 0 && static_cast<size_t>(errnum) < table->size()) { |
82 | 0 | return (*table)[static_cast<size_t>(errnum)]; |
83 | 0 | } |
84 | 0 | return StrErrorInternal(errnum); |
85 | 0 | } |
86 | | |
87 | | } // namespace base_internal |
88 | | ABSL_NAMESPACE_END |
89 | | } // namespace absl |