| 1 | // Copyright 2009 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "Common/CommonFuncs.h" |
| 5 | |
| 6 | #include <cstddef> |
| 7 | #include <cstring> |
| 8 | #include <errno.h> |
| 9 | |
| 10 | #ifdef _WIN32 |
| 11 | #include <windows.h> |
| 12 | #include <SetupAPI.h> |
| 13 | |
| 14 | #define strerror_r(err, buf, len) strerror_s(buf, len, err) |
| 15 | |
| 16 | #include "Common/StringUtil.h" |
| 17 | #endif |
| 18 | |
| 19 | namespace Common |
| 20 | { |
| 21 | constexpr size_t BUFFER_SIZE = 256; |
| 22 | |
| 23 | // There are two variants of strerror_r. The XSI version stores the message to the passed-in |
| 24 | // buffer and returns an int (0 on success). The GNU version returns a pointer to the message, |
| 25 | // which might have been stored in the passed-in buffer or might be a static string. |
| 26 | // |
| 27 | // This function might change the errno value. |
| 28 | // |
| 29 | // References: |
| 30 | // https://www.gnu.org/software/gnulib/manual/html_node/strerror_005fr.html |
| 31 | // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strerror_r.html |
| 32 | // https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/baselib-strerror-r.html |
| 33 | const char* StrErrorWrapper(int error, char* buffer, std::size_t length) |
| 34 | { |
| 35 | // We check defines in order to figure out which variant is in use. |
| 36 | #if (defined(__GLIBC__) || __ANDROID_API__ >= 23) && \ |
| 37 | (_GNU_SOURCE || (_POSIX_C_SOURCE < 200112L && _XOPEN_SOURCE < 600)) |
| 38 | return strerror_r(error, buffer, length); |
| 39 | #else |
| 40 | const int error_code = strerror_r(error, buffer, length); |
| 41 | return error_code == 0 ? buffer : ""; |
| 42 | #endif |
| 43 | } |
| 44 | |
| 45 | std::string StrerrorString(int error) |
| 46 | { |
| 47 | char error_message[BUFFER_SIZE]; |
| 48 | |
| 49 | return StrErrorWrapper(error, error_message, BUFFER_SIZE); |
| 50 | } |
| 51 | |
| 52 | // Wrapper function to get last strerror(errno) string. |
| 53 | // This function might change the error code. |
| 54 | std::string LastStrerrorString() |
| 55 | { |
| 56 | return StrerrorString(errno); |
| 57 | } |
| 58 | |
| 59 | #ifdef _WIN32 |
| 60 | // Wrapper function to get GetLastError() string. |
| 61 | // This function might change the error code. |
| 62 | std::string GetLastErrorString() |
| 63 | { |
| 64 | return GetWin32ErrorString(GetLastError()); |
| 65 | } |
| 66 | |
| 67 | // Like GetLastErrorString() but if you have already queried the error code. |
| 68 | std::string GetWin32ErrorString(DWORD error_code) |
| 69 | { |
| 70 | wchar_t error_message[BUFFER_SIZE]; |
| 71 | |
| 72 | FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, error_code, |
| 73 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), error_message, BUFFER_SIZE, nullptr); |
| 74 | return WStringToUTF8(error_message); |
| 75 | } |
| 76 | |
| 77 | // Obtains a full path to the specified module. |
| 78 | std::optional<std::wstring> GetModuleName(void* hInstance) |
| 79 | { |
| 80 | DWORD max_size = 50; // Start with space for 50 characters and grow if needed |
| 81 | std::wstring name(max_size, L'\0'); |
| 82 | |
| 83 | DWORD size; |
| 84 | while ((size = GetModuleFileNameW(static_cast<HMODULE>(hInstance), name.data(), max_size)) == |
| 85 | max_size && |
| 86 | GetLastError() == ERROR_INSUFFICIENT_BUFFER) |
| 87 | { |
| 88 | max_size *= 2; |
| 89 | name.resize(max_size); |
| 90 | } |
| 91 | |
| 92 | if (size == 0) |
| 93 | { |
| 94 | return std::nullopt; |
| 95 | } |
| 96 | name.resize(size); |
| 97 | return name; |
| 98 | } |
| 99 | #endif |
| 100 | } // namespace Common |