Several of our newer meters have merged coding concerns in terms of extracting values and displaying those values. This commit rectifies that for the SysArch and Hostname meters, allowing use of this code with alternative front/back ends. The SysArch code is also refined to detect whether the platform has an os-release file at all and/or the sys/utsname.h header via configure.ac.main
parent
2328e52403
commit
5b50ae3aa3
14 changed files with 210 additions and 77 deletions
@ -0,0 +1,102 @@ |
||||
/*
|
||||
htop - Generic.c |
||||
(C) 2021 htop dev team |
||||
Released under the GNU GPLv2, see the COPYING file |
||||
in the source distribution for its full text. |
||||
*/ |
||||
#include "config.h" // IWYU pragma: keep |
||||
|
||||
#include "Generic.h" |
||||
|
||||
#include <stdio.h> |
||||
#ifdef HAVE_SYS_UTSNAME_H |
||||
#include <sys/utsname.h> |
||||
#endif |
||||
#include <unistd.h> |
||||
|
||||
#include "XUtils.h" |
||||
|
||||
void Generic_Hostname(char* buffer, size_t size) { |
||||
gethostname(buffer, size - 1); |
||||
} |
||||
|
||||
#ifdef HAVE_SYS_UTSNAME_H |
||||
|
||||
#ifndef OSRELEASEFILE |
||||
#define OSRELEASEFILE "/etc/os-release" |
||||
#endif |
||||
|
||||
static void parseOSRelease(char* buffer, size_t bufferLen) { |
||||
FILE* stream = fopen(OSRELEASEFILE, "r"); |
||||
if (!stream) { |
||||
xSnprintf(buffer, bufferLen, "No OS Release"); |
||||
return; |
||||
} |
||||
|
||||
char name[64] = {'\0'}; |
||||
char version[64] = {'\0'}; |
||||
char lineBuffer[256]; |
||||
while (fgets(lineBuffer, sizeof(lineBuffer), stream)) { |
||||
if (String_startsWith(lineBuffer, "PRETTY_NAME=\"")) { |
||||
const char* start = lineBuffer + strlen("PRETTY_NAME=\""); |
||||
const char* stop = strrchr(lineBuffer, '"'); |
||||
if (!stop || stop <= start) |
||||
continue; |
||||
String_safeStrncpy(buffer, start, MINIMUM(bufferLen, (size_t)(stop - start + 1))); |
||||
fclose(stream); |
||||
return; |
||||
} |
||||
if (String_startsWith(lineBuffer, "NAME=\"")) { |
||||
const char* start = lineBuffer + strlen("NAME=\""); |
||||
const char* stop = strrchr(lineBuffer, '"'); |
||||
if (!stop || stop <= start) |
||||
continue; |
||||
String_safeStrncpy(name, start, MINIMUM(sizeof(name), (size_t)(stop - start + 1))); |
||||
continue; |
||||
} |
||||
if (String_startsWith(lineBuffer, "VERSION=\"")) { |
||||
const char* start = lineBuffer + strlen("VERSION=\""); |
||||
const char* stop = strrchr(lineBuffer, '"'); |
||||
if (!stop || stop <= start) |
||||
continue; |
||||
String_safeStrncpy(version, start, MINIMUM(sizeof(version), (size_t)(stop - start + 1))); |
||||
continue; |
||||
} |
||||
} |
||||
fclose(stream); |
||||
|
||||
snprintf(buffer, bufferLen, "%s%s%s", name[0] ? name : "", name[0] && version[0] ? " " : "", version); |
||||
} |
||||
|
||||
char* Generic_OSRelease(void) { |
||||
static struct utsname uname_info; |
||||
|
||||
static char savedString[ |
||||
/* uname structure fields - manpages recommend sizeof */ |
||||
sizeof(uname_info.sysname) + |
||||
sizeof(uname_info.release) + |
||||
sizeof(uname_info.machine) + |
||||
16/*markup*/ + |
||||
128/*distro*/] = {'\0'}; |
||||
static bool loaded_data = false; |
||||
|
||||
if (!loaded_data) { |
||||
int uname_result = uname(&uname_info); |
||||
|
||||
char distro[128]; |
||||
parseOSRelease(distro, sizeof(distro)); |
||||
|
||||
if (uname_result == 0) { |
||||
size_t written = xSnprintf(savedString, sizeof(savedString), "%s %s [%s]", uname_info.sysname, uname_info.release, uname_info.machine); |
||||
if (!String_contains_i(savedString, distro) && sizeof(savedString) > written) |
||||
snprintf(savedString + written, sizeof(savedString) - written, " @ %s", distro); |
||||
} else { |
||||
snprintf(savedString, sizeof(savedString), "%s", distro); |
||||
} |
||||
|
||||
loaded_data = true; |
||||
} |
||||
|
||||
return savedString; |
||||
} |
||||
#endif |
||||
@ -0,0 +1,16 @@ |
||||
#ifndef HEADER_Generic |
||||
#define HEADER_Generic |
||||
/*
|
||||
htop - Generic.h |
||||
(C) 2021 htop dev team |
||||
Released under the GNU GPLv2, see the COPYING file |
||||
in the source distribution for its full text. |
||||
*/ |
||||
|
||||
#include <stddef.h> |
||||
|
||||
void Generic_Hostname(char* buffer, size_t size); |
||||
|
||||
char* Generic_OSRelease(void); |
||||
|
||||
#endif |
||||
Loading…
Reference in new issue