mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-10 01:58:19 +00:00
Add function to get the OS version
This commit is contained in:
parent
c70e84a865
commit
67508adff1
|
@ -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
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
#if defined(CONF_FAMILY_UNIX)
|
||||
#include <signal.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/utsname.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
11
src/test/os.cpp
Normal file
11
src/test/os.cpp
Normal file
|
@ -0,0 +1,11 @@
|
|||
#include "test.h"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <base/system.h>
|
||||
|
||||
TEST(Os, VersionStr)
|
||||
{
|
||||
char aVersion[128];
|
||||
EXPECT_FALSE(os_version_str(aVersion, sizeof(aVersion)));
|
||||
EXPECT_STRNE(aVersion, "");
|
||||
}
|
Loading…
Reference in a new issue