OS name with older glib API
This commit is contained in:
parent
0de8b1cabc
commit
66c54c9e4e
1 changed files with 30 additions and 25 deletions
|
|
@ -262,35 +262,40 @@ bool isZenityAvailable()
|
||||||
std::string getOperatingSystemName()
|
std::string getOperatingSystemName()
|
||||||
{
|
{
|
||||||
std::string name;
|
std::string name;
|
||||||
name.reserve(256);
|
|
||||||
|
|
||||||
#if GLIB_CHECK_VERSION(2, 64, 0)
|
std::ifstream in("/etc/os-release", std::ios::binary);
|
||||||
if (char *osName = g_get_os_info(G_OS_INFO_KEY_NAME)) {
|
if (!in)
|
||||||
name.append(osName);
|
in = std::ifstream("/usr/lib/os-release", std::ios::binary);
|
||||||
g_free(osName);
|
if (in) {
|
||||||
}
|
std::string line;
|
||||||
else {
|
line.reserve(256);
|
||||||
name.append("Unknown");
|
for (bool found = false; !found && std::getline(in, line); ) {
|
||||||
|
const char prefix[] = "PRETTY_NAME=";
|
||||||
|
size_t length = sizeof(prefix) - 1;
|
||||||
|
found = line.size() >= length && !memcmp(line.data(), prefix, length);
|
||||||
|
if (found) {
|
||||||
|
if (char* value = g_shell_unquote(line.c_str() + length, nullptr)) {
|
||||||
|
name.assign(value);
|
||||||
|
g_free(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
in.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (char *osVersion = g_get_os_info(G_OS_INFO_KEY_VERSION_ID)) {
|
if (name.empty()) {
|
||||||
name.push_back(' ');
|
utsname un {};
|
||||||
name.append(osVersion);
|
int ret = uname(&un);
|
||||||
g_free(osVersion);
|
if (ret != -1 && un.sysname[0] != '\0')
|
||||||
|
name.append(un.sysname);
|
||||||
|
else {
|
||||||
|
name.append("Unknown");
|
||||||
|
}
|
||||||
|
if (ret != -1 && un.release[0] != '\0') {
|
||||||
|
name.push_back(' ');
|
||||||
|
name.append(un.release);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
utsname un {};
|
|
||||||
int ret = uname(&un);
|
|
||||||
if (ret != -1 && un.sysname[0] != '\0')
|
|
||||||
name.append(un.sysname);
|
|
||||||
else {
|
|
||||||
name.append("Unknown");
|
|
||||||
}
|
|
||||||
if (ret != -1 && un.release[0] != '\0') {
|
|
||||||
name.push_back(' ');
|
|
||||||
name.append(un.release);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue