diff --git a/src/common/properties.c b/src/common/properties.c index 3dead5045..f45c9928c 100644 --- a/src/common/properties.c +++ b/src/common/properties.c @@ -1,5 +1,7 @@ #include "fastfetch.h" #include "common/properties.h" +#include "common/io/io.h" +#include "util/mallocHelper.h" #include #ifdef _WIN32 @@ -95,50 +97,44 @@ bool ffParsePropLines(const char* lines, const char* start, FFstrbuf* buffer) bool ffParsePropFileValues(const char* filename, uint32_t numQueries, FFpropquery* queries) { - FILE* file = fopen(filename, "r"); - if(file == NULL) + FF_AUTO_CLOSE_FILE FILE* file = fopen(filename, "r"); + if (file == NULL) return false; - bool valueStorage[4]; - bool* unsetValues; + bool valueStorage[32]; + bool* unsetValues = valueStorage; - if(numQueries > sizeof(valueStorage) / sizeof(valueStorage[0])) + if (numQueries > sizeof(valueStorage) / sizeof(valueStorage[0])) unsetValues = malloc(sizeof(bool) * numQueries); - else - unsetValues = valueStorage; bool allSet = true; - for(uint32_t i = 0; i < numQueries; i++) + for (uint32_t i = 0; i < numQueries; i++) { - if((unsetValues[i] = queries[i].buffer->length == 0)) + unsetValues[i] = queries[i].buffer->length == 0; + if (unsetValues[i]) allSet = false; } - if(allSet) - goto done; - - char* line = NULL; - size_t len = 0; - - while (getline(&line, &len, file) != -1) + if (!allSet) { - for(uint32_t i = 0; i < numQueries; i++) - { - if(!unsetValues[i]) - continue; + FF_AUTO_FREE char* line = NULL; + size_t len = 0; - uint32_t currentLength = queries[i].buffer->length; - queries[i].buffer->length = 0; - if(!ffParsePropLine(line, queries[i].start, queries[i].buffer)) - queries[i].buffer->length = currentLength; + while (getline(&line, &len, file) != -1) + { + for(uint32_t i = 0; i < numQueries; i++) + { + if(!unsetValues[i]) + continue; + + uint32_t currentLength = queries[i].buffer->length; + queries[i].buffer->length = 0; + if(!ffParsePropLine(line, queries[i].start, queries[i].buffer)) + queries[i].buffer->length = currentLength; + } } } - if(line != NULL) - free(line); - -done: - fclose(file); if(unsetValues != valueStorage) free(unsetValues); return true; diff --git a/src/detection/os/os_linux.c b/src/detection/os/os_linux.c index 74e9f1475..69fe1ddad 100644 --- a/src/detection/os/os_linux.c +++ b/src/detection/os/os_linux.c @@ -12,6 +12,7 @@ static inline bool allRelevantValuesSet(const FFOSResult* result) return result->id.length > 0 && result->name.length > 0 && result->prettyName.length > 0 + && result->version.length > 0 ; } @@ -150,22 +151,20 @@ static void detectOS(FFOSResult* os) if(os->prettyName.length == 0) ffStrbufAppendS(&os->prettyName, "Bedrock Linux"); - - parseFile("/bedrock"FASTFETCH_TARGET_DIR_ETC"/os-release", os); - if(allRelevantValuesSet(os)) + if(parseFile("/bedrock"FASTFETCH_TARGET_DIR_ETC"/os-release", os) && allRelevantValuesSet(os)) return; } - parseFile(FASTFETCH_TARGET_DIR_ETC"/os-release", os); - if(allRelevantValuesSet(os)) + // Seems some distros contain real distro name only in lsb-release + // https://github.com/fastfetch-cli/fastfetch/issues/847#issuecomment-2091999419 + if(parseFile(FASTFETCH_TARGET_DIR_ETC"/lsb-release", os) && allRelevantValuesSet(os)) + return; + + if(parseFile(FASTFETCH_TARGET_DIR_ETC"/os-release", os) && allRelevantValuesSet(os)) return; parseFile(FASTFETCH_TARGET_DIR_USR"/lib/os-release", os); - if(allRelevantValuesSet(os)) - return; - - parseFile(FASTFETCH_TARGET_DIR_ETC"/lsb-release", os); } void ffDetectOSImpl(FFOSResult* os)