From 67508adff1a43202480b4fa35e88e28b99b43e48 Mon Sep 17 00:00:00 2001 From: heinrich5991 Date: Thu, 27 Jan 2022 02:35:08 +0100 Subject: [PATCH] Add function to get the OS version --- CMakeLists.txt | 3 +- src/base/system.cpp | 80 +++++++++++++++++++++++++++++++++++++++++++++ src/base/system.h | 14 ++++++++ src/test/os.cpp | 11 +++++++ 4 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 src/test/os.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index da795e99a..9a0fe2984 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -570,7 +570,7 @@ endif() if(TARGET_OS STREQUAL "windows") set(PLATFORM_CLIENT) set(PLATFORM_CLIENT_LIBS opengl32 winmm) - set(PLATFORM_LIBS ws2_32) # Windows sockets + set(PLATFORM_LIBS version ws2_32) # Windows sockets elseif(TARGET_OS STREQUAL "mac") find_library(CARBON Carbon) find_library(COCOA Cocoa) @@ -2275,6 +2275,7 @@ if(GTEST_FOUND OR DOWNLOAD_GTEST) mapbugs.cpp name_ban.cpp netaddr.cpp + os.cpp packer.cpp prng.cpp score.cpp diff --git a/src/base/system.cpp b/src/base/system.cpp index df2fe9fdf..7b702205d 100644 --- a/src/base/system.cpp +++ b/src/base/system.cpp @@ -26,6 +26,7 @@ #if defined(CONF_FAMILY_UNIX) #include #include +#include #include #include @@ -3895,4 +3896,83 @@ void set_console_msg_color(const void *rgbvoid) stdout_nonewline_logger.logger(buff, stdout_nonewline_logger.user); #endif } + +int os_version_str(char *version, int length) +{ +#if defined(CONF_FAMILY_WINDOWS) + const char *DLL = "C:\\Windows\\System32\\user32.dll"; + DWORD handle; + DWORD size = GetFileVersionInfoSizeA(DLL, &handle); + if(!size) + { + return 1; + } + void *data = malloc(size); + if(!GetFileVersionInfoA(DLL, handle, size, data)) + { + free(data); + return 1; + } + VS_FIXEDFILEINFO *fileinfo; + UINT unused; + if(!VerQueryValueA(data, "\\", (void **)&fileinfo, &unused)) + { + free(data); + return 1; + } + str_format(version, length, "Windows %d.%d.%d.%d", + HIWORD(fileinfo->dwProductVersionMS), + LOWORD(fileinfo->dwProductVersionMS), + HIWORD(fileinfo->dwProductVersionLS), + LOWORD(fileinfo->dwProductVersionLS)); + free(data); + return 0; +#else + struct utsname u; + if(uname(&u)) + { + return 1; + } + char extra[128]; + extra[0] = 0; + + do + { + IOHANDLE os_release = io_open("/etc/os-release", IOFLAG_READ); + char buf[4096]; + int read; + int offset; + char *newline; + if(!os_release) + { + break; + } + read = io_read(os_release, buf, sizeof(buf) - 1); + io_close(os_release); + buf[read] = 0; + if(str_startswith(buf, "PRETTY_NAME=")) + { + offset = 0; + } + else + { + const char *found = str_find(buf, "\nPRETTY_NAME="); + if(!found) + { + break; + } + offset = found - buf + 1; + } + newline = (char *)str_find(buf + offset, "\n"); + if(newline) + { + *newline = 0; + } + str_format(extra, sizeof(extra), "; %s", buf + offset + 12); + } while(0); + + str_format(version, length, "%s %s (%s, %s)%s", u.sysname, u.release, u.machine, u.version, extra); + return 0; +#endif +} } diff --git a/src/base/system.h b/src/base/system.h index 2821db284..90b8d465c 100644 --- a/src/base/system.h +++ b/src/base/system.h @@ -2326,6 +2326,20 @@ int secure_rand_below(int below); */ void set_console_msg_color(const void *rgbvoid); +/* + Function: os_version_str + Returns a human-readable version string of the operating system + + Parameters: + version - Buffer to use for the output. + length - Length of the output buffer. + + Returns: + 0 - Success in getting the version. + 1 - Failure in getting the version. +*/ +int os_version_str(char *version, int length); + #if defined(__cplusplus) } #endif diff --git a/src/test/os.cpp b/src/test/os.cpp new file mode 100644 index 000000000..4b83a9acc --- /dev/null +++ b/src/test/os.cpp @@ -0,0 +1,11 @@ +#include "test.h" +#include + +#include + +TEST(Os, VersionStr) +{ + char aVersion[128]; + EXPECT_FALSE(os_version_str(aVersion, sizeof(aVersion))); + EXPECT_STRNE(aVersion, ""); +}