From 2240264d02a3ea405dc250d2adb36997b85300ba Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Sun, 7 Mar 2021 11:58:24 +0100 Subject: [PATCH] --host-version --- completions/bash | 1 + src/common.c | 2 ++ src/fastfetch.c | 13 +++++++++++++ src/fastfetch.h | 3 +++ src/modules/host.c | 25 ++++++++----------------- 5 files changed, 27 insertions(+), 17 deletions(-) diff --git a/completions/bash b/completions/bash index d3319cdb0..5cfc15d3d 100644 --- a/completions/bash +++ b/completions/bash @@ -87,6 +87,7 @@ __fastfetch_completion() "--show-errors" "--color-logo" "--os-architecture" + "--host-version" "--shell-path" "--battery-manufacturer" "--battery-model" diff --git a/src/common.c b/src/common.c index 9f48f1632..d15905f65 100644 --- a/src/common.c +++ b/src/common.c @@ -28,6 +28,8 @@ void ffDefaultConfig(FFconfig* config) config->osShowArchitecture = true; + config->hostShowVersion = true; + config->shellShowPath = false; config->batteryShowManufacturer = true; diff --git a/src/fastfetch.c b/src/fastfetch.c index 930721444..1ed8258e0 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -34,6 +34,9 @@ "## OS options:\n" \ "# --os-architecture true\n" \ "\n" \ + "## Host options:\n" \ + "# --host-version true\n" \ + "\n" \ "## Shell options:\n" \ "# --shell-path false\n" \ "\n" \ @@ -82,6 +85,9 @@ static void printHelp() "OS options:\n" " --os-architecture : Show the architecture of the os\n" "\n" + "Host options:\n" + " --host-version : Show the version of the host platform, if possible. Most likely, this will be the BIOS version\n" + "\n" "Shell options:\n" " --shell-path : Show the full path of the shell\n" "\n" @@ -366,6 +372,13 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con else instance->config.osShowArchitecture = parseBoolean(value); } + else if(strcasecmp(key, "--host-version") == 0) + { + if(value == NULL) + instance->config.hostShowVersion = true; + else + instance->config.hostShowVersion = parseBoolean(value); + } else if(strcasecmp(key, "--shell-path") == 0) { if(value == NULL) diff --git a/src/fastfetch.h b/src/fastfetch.h index 91b298cab..32044fbfe 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -46,6 +46,9 @@ typedef struct FFconfig //OS bool osShowArchitecture; + //Host + bool hostShowVersion; + //Shell bool shellShowPath; diff --git a/src/modules/host.c b/src/modules/host.c index 8e4f02f27..5a03cf8ed 100644 --- a/src/modules/host.c +++ b/src/modules/host.c @@ -5,28 +5,19 @@ void ffPrintHost(FFinstance* instance) if(ffPrintCachedValue(instance, "Host")) return; - char host[1024]; - - FILE* nameFile = fopen("/sys/devices/virtual/dmi/id/product_name", "r"); - if(nameFile == NULL) + char host[256]; + ffGetFileContent("/sys/devices/virtual/dmi/id/product_name", host, sizeof(host) - 32); //We subtract 32 to have at least this padding for the version + if(host[0] == '\0') { - ffPrintError(instance, "Host", "fopen(\"/sys/devices/virtual/dmi/id/product_name\", \"r\") == NULL"); + ffPrintError(instance, "Host", "ffGetFileContent(\"/sys/devices/virtual/dmi/id/product_name\", host, sizeof(host)) failed"); return; } - if(fscanf(nameFile, "%[^\n]", host) != 1) - { - ffPrintError(instance, "Host", "fscanf(nameFile, \"%[^\\n]\", name) != 1"); - return; - } - fclose(nameFile); - FILE* versionFile = fopen("/sys/devices/virtual/dmi/id/product_version", "r"); - if(versionFile != NULL) + if(instance->config.hostShowVersion) { - ssize_t len = strlen(host); - host[len] = ' '; - if(fscanf(versionFile, "%[^\n]", host + len + 1) == 0); //fail silently, if reading of version fails, just show now version - fclose(versionFile); + size_t len = strlen(host); + host[len++] = ' '; + ffGetFileContent("/sys/devices/virtual/dmi/id/product_version", host + len, sizeof(host) - len); } ffPrintAndSaveCachedValue(instance, "Host", host);