2021-03-02 04:58:11 +00:00
|
|
|
/*
|
2021-03-02 23:59:28 +00:00
|
|
|
htop - generic/uname.c
|
2021-03-02 04:58:11 +00:00
|
|
|
(C) 2021 htop dev team
|
2021-09-22 09:33:00 +00:00
|
|
|
Released under the GNU GPLv2+, see the COPYING file
|
2021-03-02 04:58:11 +00:00
|
|
|
in the source distribution for its full text.
|
|
|
|
*/
|
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2021-04-29 18:13:36 +00:00
|
|
|
#include "generic/uname.h"
|
|
|
|
|
2021-04-29 15:12:43 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "Macros.h"
|
|
|
|
#include "XUtils.h"
|
2021-03-02 04:58:11 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_SYS_UTSNAME_H
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#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);
|
|
|
|
}
|
2021-03-04 04:16:32 +00:00
|
|
|
|
2021-03-02 23:59:28 +00:00
|
|
|
char* Generic_uname(void) {
|
2021-03-02 04:58:11 +00:00
|
|
|
static char savedString[
|
|
|
|
/* uname structure fields - manpages recommend sizeof */
|
2021-03-03 22:00:34 +00:00
|
|
|
sizeof(((struct utsname*)0)->sysname) +
|
|
|
|
sizeof(((struct utsname*)0)->release) +
|
|
|
|
sizeof(((struct utsname*)0)->machine) +
|
2021-03-02 04:58:11 +00:00
|
|
|
16/*markup*/ +
|
|
|
|
128/*distro*/] = {'\0'};
|
|
|
|
static bool loaded_data = false;
|
|
|
|
|
|
|
|
if (!loaded_data) {
|
2021-03-03 22:00:34 +00:00
|
|
|
struct utsname uname_info;
|
2021-03-02 04:58:11 +00:00
|
|
|
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);
|
2022-03-25 15:24:24 +00:00
|
|
|
if (!String_contains_i(savedString, distro, false) && sizeof(savedString) > written)
|
2021-03-02 04:58:11 +00:00
|
|
|
snprintf(savedString + written, sizeof(savedString) - written, " @ %s", distro);
|
|
|
|
} else {
|
|
|
|
snprintf(savedString, sizeof(savedString), "%s", distro);
|
|
|
|
}
|
|
|
|
|
|
|
|
loaded_data = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return savedString;
|
|
|
|
}
|