dont use format string if an error occures

This commit is contained in:
Linus Dierheimer
2021-04-03 21:51:50 +02:00
parent 67465d266e
commit e01d03f98d
13 changed files with 198 additions and 152 deletions
+30 -32
View File
@@ -2,6 +2,23 @@
#include <dirent.h>
static void getKey(FFinstance* instance, FFstrbuf* key, uint8_t counter, bool showCounter)
{
if(instance->config.batteryKey.length == 0)
{
if(showCounter)
ffStrbufAppendF(key, "Battery %hhu", counter);
else
ffStrbufSetS(key, "Battery");
}
else
{
ffParseFormatString(key, &instance->config.batteryKey, 1,
(FFformatarg){FF_FORMAT_ARG_TYPE_UINT8, &counter}
);
}
}
static void printBattery(FFinstance* instance, uint8_t index)
{
FF_STRBUF_CREATE(manufactor);
@@ -29,32 +46,23 @@ static void printBattery(FFinstance* instance, uint8_t index)
sprintf(statusPath, "/sys/class/power_supply/BAT%hhu/status", index);
ffGetFileContent(statusPath, &status);
uint8_t indexPlusOne = index + 1;
FF_STRBUF_CREATE(key);
if(instance->config.batteryKey.length == 0)
{
ffStrbufAppendF(&key, "Battery %hhu", indexPlusOne);
}
else
{
ffParseFormatString(&key, &instance->config.batteryKey, 1,
(FFformatarg){FF_FORMAT_ARG_TYPE_UINT8, &indexPlusOne}
);
getKey(instance, &key, index + 1, true);
if(
manufactor.length == 0 &&
model.length == 0 &&
technology.length == 0 &&
capacity.length == 0 &&
status.length == 0
) {
ffPrintError(instance, &key, NULL, "No file in /sys/class/power_supply/BAT%hhu/ could be read or all battery options are disabled", index);
ffStrbufDestroy(&key);
return;
}
if(instance->config.batteryFormat.length == 0)
{
if(
manufactor.length == 0 &&
model.length == 0 &&
technology.length == 0 &&
capacity.length == 0 &&
status.length == 0
) {
ffPrintError(instance, &key, NULL, "No file in /sys/class/power_supply/BAT%hhu/ could be read or all battery options are disabled", index);
return;
}
ffPrintLogoAndKey(instance, &key, NULL);
@@ -125,17 +133,7 @@ void ffPrintBattery(FFinstance* instance)
return;
FF_STRBUF_CREATE(key);
if(instance->config.batteryKey.length == 0)
{
ffStrbufSetS(&key, "Battery");
}
else
{
ffParseFormatString(&key, &instance->config.batteryKey, 1,
(FFformatarg){FF_FORMAT_ARG_TYPE_UINT8, &batteryCounter}
);
}
getKey(instance, &key, 1, false);
ffPrintError(instance, &key, NULL, "No battery found in /sys/class/power_supply/");
ffStrbufDestroy(&key);
}
+49 -39
View File
@@ -23,12 +23,17 @@ void ffPrintCPU(FFinstance* instance)
if(ffPrintCachedValue(instance, &instance->config.cpuKey, "CPU"))
return;
FILE* cpuinfo = fopen("/proc/cpuinfo", "r");
if(cpuinfo == NULL)
{
ffPrintError(instance, &instance->config.cpuKey, "CPU", "fopen(\"/proc/cpuinfo\", \"r\") == NULL");
return;
}
char name[256]; name[0] = '\0';
char vendor[256]; vendor[0] = '\0';
int physicalCores = 0;
FILE* cpuinfo = fopen("/proc/cpuinfo", "r");
char* line = NULL;
size_t len = 0;
@@ -48,15 +53,6 @@ void ffPrintCPU(FFinstance* instance)
fclose(cpuinfo);
FFstrbuf namePretty;
ffStrbufInitA(&namePretty, 64);
ffStrbufAppendS(&namePretty, name);
ffStrbufRemoveStrings(&namePretty, 11,
"(R)", "(r)", "(TM)", "(tm)", " CPU", " FPU", " Processor", " Dual-Core", " Quad-Core", " Six-Core", " Eight-Core"
);
ffStrbufSubstrBeforeFirstC(&namePretty, '@'); //Cut the speed output in the name as we append our own
ffStrbufTrimRight(&namePretty, ' '); //If we removed the @ in previous step there was most likely a space before it
double biosLimit = getGhz("/sys/devices/system/cpu/cpu0/cpufreq/bios_limit");
double scalingMaxFreq = getGhz("/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq");
double scalingMinFreq = getGhz("/sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq");
@@ -66,27 +62,47 @@ void ffPrintCPU(FFinstance* instance)
int numProcsOnline = get_nprocs();
int numProcsAvailable = get_nprocs_conf();
//The current get_nprocs* returns 1 on failure. It also makes no sense to have a (1) as count
int numProcs = numProcsOnline;
if(numProcs <= 1)
numProcs = numProcsAvailable;
if(numProcs <= 1)
numProcs = physicalCores;
double ghz = biosLimit;
if(ghz == 0)
ghz = scalingMaxFreq;
if(ghz == 0)
ghz = infoMaxFreq;
if(ghz == 0)
ghz = scalingMinFreq;
if(ghz == 0)
ghz = infoMinFreq;
if(
name[0] == '\0' && //This also implies namePretty is not set
vendor[0] == '\0' &&
numProcs <= 1 &&
ghz <= 0
) {
ffPrintError(instance, &instance->config.cpuKey, "CPU", "No CPU info found in /proc/cpuinfo");
return;
}
FFstrbuf namePretty;
ffStrbufInitA(&namePretty, 64);
ffStrbufAppendS(&namePretty, name);
ffStrbufRemoveStrings(&namePretty, 11,
"(R)", "(r)", "(TM)", "(tm)", " CPU", " FPU", " Processor", " Dual-Core", " Quad-Core", " Six-Core", " Eight-Core"
);
ffStrbufSubstrBeforeFirstC(&namePretty, '@'); //Cut the speed output in the name as we append our own
ffStrbufTrimRight(&namePretty, ' '); //If we removed the @ in previous step there was most likely a space before it
FFstrbuf cpu;
ffStrbufInitA(&cpu, 128);
if(instance->config.cpuFormat.length == 0)
{
//The current get_nprocs* returns 1 on failure. It also makes no sense to have a (1) as count
int numProcs = numProcsOnline;
if(numProcs <= 1)
numProcs = numProcsAvailable;
if(numProcs <= 1)
numProcs = physicalCores;
double ghz = biosLimit;
if(ghz == 0)
ghz = scalingMaxFreq;
if(ghz == 0)
ghz = infoMaxFreq;
if(ghz == 0)
ghz = scalingMinFreq;
if(ghz == 0)
ghz = infoMinFreq;
if(namePretty.length > 0)
ffStrbufAppend(&cpu, &namePretty);
@@ -97,15 +113,8 @@ void ffPrintCPU(FFinstance* instance)
ffStrbufAppendS(&cpu, vendor);
ffStrbufAppendS(&cpu, " unknown processor");
}
else if(numProcs > 1 || ghz > 0)
ffStrbufAppendS(&cpu, " unknwon processor");
else
{
ffPrintError(instance, &instance->config.cpuKey, "CPU", "No CPU info found in /proc/cpuinfo");
ffStrbufDestroy(&cpu);
ffStrbufDestroy(&namePretty);
return;
}
ffStrbufAppendS(&cpu, " unknown processor");
if(numProcs > 1)
ffStrbufAppendF(&cpu, " (%i)", numProcs);
@@ -115,7 +124,7 @@ void ffPrintCPU(FFinstance* instance)
}
else
{
ffParseFormatString(&cpu, &instance->config.cpuFormat, 11,
ffParseFormatString(&cpu, &instance->config.cpuFormat, 13,
(FFformatarg){FF_FORMAT_ARG_TYPE_STRING, name},
(FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, &namePretty},
(FFformatarg){FF_FORMAT_ARG_TYPE_STRING, vendor},
@@ -123,15 +132,16 @@ void ffPrintCPU(FFinstance* instance)
(FFformatarg){FF_FORMAT_ARG_TYPE_INT, &numProcsAvailable},
(FFformatarg){FF_FORMAT_ARG_TYPE_INT, &physicalCores},
(FFformatarg){FF_FORMAT_ARG_TYPE_DOUBLE, &biosLimit},
(FFformatarg){FF_FORMAT_ARG_TYPE_INT, &numProcs},
(FFformatarg){FF_FORMAT_ARG_TYPE_DOUBLE, &scalingMaxFreq},
(FFformatarg){FF_FORMAT_ARG_TYPE_DOUBLE, &scalingMinFreq},
(FFformatarg){FF_FORMAT_ARG_TYPE_DOUBLE, &infoMaxFreq},
(FFformatarg){FF_FORMAT_ARG_TYPE_DOUBLE, &infoMinFreq}
(FFformatarg){FF_FORMAT_ARG_TYPE_DOUBLE, &infoMinFreq},
(FFformatarg){FF_FORMAT_ARG_TYPE_DOUBLE, &ghz}
);
}
ffPrintAndSaveCachedValue(instance, &instance->config.cpuKey, "CPU", &cpu);
ffStrbufDestroy(&cpu);
ffStrbufDestroy(&namePretty);
ffStrbufDestroy(&cpu);
}
+14 -9
View File
@@ -12,8 +12,9 @@ static void getKDE(FFstrbuf* name, FFstrbuf* version, FFstrbuf* type)
void ffPrintDesktopEnvironment(FFinstance* instance)
{
FFstrbuf sessionDesktop;
ffStrbufInitS(&sessionDesktop, getenv("XDG_CURRENT_DESKTOP"));
FF_STRBUF_CREATE(sessionDesktop);
ffStrbufAppendS(&sessionDesktop, getenv("XDG_CURRENT_DESKTOP"));
if(sessionDesktop.length == 0)
ffStrbufSetS(&sessionDesktop, getenv("XDG_SESSION_DESKTOP"));
@@ -38,13 +39,17 @@ void ffPrintDesktopEnvironment(FFinstance* instance)
ffStrbufSetS(&sessionType, xdgSessionType);
}
if(sessionDesktop.length == 0 && sessionType.length == 0)
{
ffPrintError(instance, &instance->config.deKey, "DE", "No relevant XDG_SESSION_* environment variable set");
ffStrbufDestroy(&sessionDesktop);
ffStrbufDestroy(&sessionVersion);
ffStrbufDestroy(&sessionType);
return;
}
if(instance->config.deFormat.length == 0)
{
if(sessionDesktop.length == 0 && sessionType.length == 0)
{
ffPrintError(instance, &instance->config.deKey, "DE", "Neither DE nor Display Server could be determined");
return;
}
ffPrintLogoAndKey(instance, &instance->config.deKey, "DE");
@@ -75,7 +80,7 @@ void ffPrintDesktopEnvironment(FFinstance* instance)
(FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, &sessionType}
);
}
ffStrbufDestroy(&sessionType);
ffStrbufDestroy(&sessionVersion);
ffStrbufDestroy(&sessionDesktop);
ffStrbufDestroy(&sessionVersion);
ffStrbufDestroy(&sessionType);
}
+25 -18
View File
@@ -2,6 +2,23 @@
#include <sys/statvfs.h>
static void getKey(FFinstance* instance, FFstrbuf* key, const char* folderPath, bool showFolderPath)
{
if(instance->config.diskKey.length == 0)
{
if(showFolderPath)
ffStrbufAppendF(key, "Disk (%s)", folderPath);
else
ffStrbufSetS(key, "Disk");
}
else
{
ffParseFormatString(key, &instance->config.diskKey, 1,
(FFformatarg){FF_FORMAT_ARG_TYPE_STRING, folderPath}
);
}
}
static void printStatvfs(FFinstance* instance, FFstrbuf* key, struct statvfs* fs)
{
const uint32_t GB = 1024 * 1024 * 1024;
@@ -29,24 +46,10 @@ static void printStatvfs(FFinstance* instance, FFstrbuf* key, struct statvfs* fs
}
}
static void createKey(FFinstance* instance, FFstrbuf* key, const char* folderPath)
{
if(instance->config.diskKey.length == 0)
{
ffStrbufAppendF(key, "Disk (%s)", folderPath);
}
else
{
ffParseFormatString(key, &instance->config.diskKey, 1,
(FFformatarg){FF_FORMAT_ARG_TYPE_STRING, folderPath}
);
}
}
static void printStatvfsCreateKey(FFinstance* instance, const char* folderPath, struct statvfs* fs)
{
FF_STRBUF_CREATE(key);
createKey(instance, &key, folderPath);
getKey(instance, &key, folderPath, true);
printStatvfs(instance, &key, fs);
ffStrbufDestroy(&key);
}
@@ -54,13 +57,13 @@ static void printStatvfsCreateKey(FFinstance* instance, const char* folderPath,
static void printFolder(FFinstance* instance, const char* folderPath)
{
FF_STRBUF_CREATE(key);
createKey(instance, &key, folderPath);
getKey(instance, &key, folderPath, true);
struct statvfs fs;
int ret = statvfs(folderPath, &fs);
if(ret != 0 && instance->config.diskFormat.length == 0)
{
ffPrintError(instance, &key, NULL, "statvfs(\"%s\", &fs) != 0", folderPath);
ffPrintError(instance, &key, NULL, "statvfs(\"%s\", &fs) != 0 (%i)", folderPath, ret);
ffStrbufDestroy(&key);
return;
}
@@ -73,6 +76,7 @@ void ffPrintDisk(FFinstance* instance)
{
if(instance->config.diskFolders.length == 0)
{
struct statvfs fsRoot;
int rootRet = statvfs("/", &fsRoot);
@@ -81,7 +85,10 @@ void ffPrintDisk(FFinstance* instance)
if(rootRet != 0 && homeRet != 0)
{
ffPrintError(instance, NULL, "Disk", "statvfs failed for both / and /home");
FF_STRBUF_CREATE(key);
getKey(instance, &key, "/", false);
ffPrintError(instance, &key, NULL, "statvfs failed for both / and /home");
ffStrbufDestroy(&key);
return;
}
+31 -16
View File
@@ -3,22 +3,30 @@
#include <pci/pci.h>
#include <dlfcn.h>
static void handleGPU(FFinstance* instance, struct pci_access* pacc, struct pci_dev* dev, uint32_t counter, char*(*ffpci_lookup_name)(struct pci_access*, char*, int, int, ...))
static void getKey(FFinstance* instance, FFstrbuf* key, uint8_t counter, bool showCounter)
{
char cacheKey[8];
sprintf(cacheKey, "GPU%u", counter);
FF_STRBUF_CREATE(key);
if(instance->config.gpuKey.length == 0)
{
ffStrbufAppendF(&key, "GPU %u", counter);
if(showCounter)
ffStrbufAppendF(key, "GPU %hhu", counter);
else
ffStrbufSetS(key, "GPU");
}
else
{
ffParseFormatString(&key, &instance->config.gpuKey, 1,
(FFformatarg){FF_FORMAT_ARG_TYPE_UINT, &counter}
ffParseFormatString(key, &instance->config.gpuKey, 1,
(FFformatarg){FF_FORMAT_ARG_TYPE_UINT8, &counter}
);
}
}
static void handleGPU(FFinstance* instance, struct pci_access* pacc, struct pci_dev* dev, uint8_t counter, char*(*ffpci_lookup_name)(struct pci_access*, char*, int, int, ...))
{
char cacheKey[8];
sprintf(cacheKey, "GPU%hhu", counter);
FF_STRBUF_CREATE(key);
getKey(instance, &key, counter, true);
if(ffPrintCachedValue(instance, &key, cacheKey))
return;
@@ -51,12 +59,16 @@ static void handleGPU(FFinstance* instance, struct pci_access* pacc, struct pci_
}
ffPrintAndSaveCachedValue(instance, &key, cacheKey, &gpu);
ffStrbufDestroy(&gpu);
ffStrbufDestroy(&key);
}
void ffPrintGPU(FFinstance* instance)
{
FF_STRBUF_CREATE(key);
getKey(instance, &key, 1, false);
void* pci;
if(instance->config.libPCI.length == 0)
pci = dlopen("libpci.so", RTLD_LAZY);
@@ -64,53 +76,53 @@ void ffPrintGPU(FFinstance* instance)
pci = dlopen(instance->config.libPCI.chars, RTLD_LAZY);
if(pci == NULL)
{
ffPrintError(instance, NULL, "GPU", "dlopen(\"libpci.so\", RTLD_LAZY) == NULL");
ffPrintError(instance, &key, NULL, "dlopen(\"libpci.so\", RTLD_LAZY) == NULL");
return;
}
struct pci_access*(*ffpci_alloc)() = dlsym(pci, "pci_alloc");
if(ffpci_alloc == NULL)
{
ffPrintError(instance, NULL, "GPU", "dlsym(pci, \"pci_alloc\") == NULL");
ffPrintError(instance, &key, NULL, "dlsym(pci, \"pci_alloc\") == NULL");
return;
}
void(*ffpci_init)(struct pci_access*) = dlsym(pci, "pci_init");
if(ffpci_init == NULL)
{
ffPrintError(instance, NULL, "GPU", "dlsym(pci, \"pci_init\") == NULL");
ffPrintError(instance, &key, NULL, "dlsym(pci, \"pci_init\") == NULL");
return;
}
void(*ffpci_scan_bus)(struct pci_access*) = dlsym(pci, "pci_scan_bus");
if(ffpci_scan_bus == NULL)
{
ffPrintError(instance, NULL, "GPU", "dlsym(pci, \"pci_init\") == NULL");
ffPrintError(instance, &key, NULL, "dlsym(pci, \"pci_init\") == NULL");
return;
}
int(*ffpci_fill_info)(struct pci_dev*, int) = dlsym(pci, "pci_fill_info");
if(ffpci_fill_info == NULL)
{
ffPrintError(instance, NULL, "GPU", "dlsym(pci, \"pci_fill_info\") == NULL");
ffPrintError(instance, &key, NULL, "dlsym(pci, \"pci_fill_info\") == NULL");
return;
}
char*(*ffpci_lookup_name)(struct pci_access*, char*, int, int, ...) = dlsym(pci, "pci_lookup_name");
if(ffpci_lookup_name == NULL)
{
ffPrintError(instance, NULL, "GPU", "dlsym(pci, \"pci_lookup_name\") == NULL");
ffPrintError(instance, &key, NULL, "dlsym(pci, \"pci_lookup_name\") == NULL");
return;
}
void(*ffpci_cleanup)(struct pci_access*) = dlsym(pci, "pci_cleanup");
if(ffpci_cleanup == NULL)
{
ffPrintError(instance, NULL, "GPU", "dlsym(pci, \"pci_cleanup\") == NULL");
ffPrintError(instance, &key, NULL, "dlsym(pci, \"pci_cleanup\") == NULL");
return;
}
uint32_t counter = 0;
uint8_t counter = 1;
struct pci_access *pacc;
struct pci_dev *dev;
@@ -134,4 +146,7 @@ void ffPrintGPU(FFinstance* instance)
ffpci_cleanup(pacc);
dlclose(pci);
if(counter == 1)
ffPrintError(instance, &key, NULL, "No GPU found");
}
+7 -6
View File
@@ -14,16 +14,17 @@ void ffPrintHost(FFinstance* instance)
FF_STRBUF_CREATE(version);
ffGetFileContent("/sys/devices/virtual/dmi/id/product_version", &version);
if(family.length == 0 && name.length == 0)
{
ffPrintError(instance, &instance->config.hostKey, "Host", "neither family nor name could be determined");
return;
}
FF_STRBUF_CREATE(host);
if(instance->config.hostFormat.length == 0)
{
if(family.length == 0 && name.length == 0)
{
ffPrintError(instance, &instance->config.hostKey, "Host", "neither family nor name could be determined");
return;
}
else if(name.length == 0)
if(name.length == 0)
{
ffStrbufAppend(&host, &family);
}
+4 -2
View File
@@ -10,7 +10,7 @@ void ffPrintMemory(FFinstance* instance)
}
char* line = NULL;
size_t len;
size_t len = 0;
uint32_t total, shared, memfree, buffers, cached, reclaimable;
@@ -23,7 +23,9 @@ void ffPrintMemory(FFinstance* instance)
sscanf(line, "SReclaimable: %u", &reclaimable);
}
free(line);
if(line != NULL)
free(line);
fclose(meminfo);
uint32_t used_mem = (total + shared - memfree - buffers - cached - reclaimable) / 1024;
+13 -7
View File
@@ -5,6 +5,17 @@ void ffPrintOS(FFinstance* instance)
if(ffPrintCachedValue(instance, &instance->config.osKey, "OS"))
return;
FILE* osRelease = fopen("/etc/os-release", "r");
if(osRelease == NULL)
osRelease = fopen("/usr/lib/os-release", "r");
if(osRelease == NULL)
{
ffPrintError(instance, &instance->config.osKey, "OS", "couldn't read /etc/os-release nor /usr/lib/os-release");
return;
}
// Documentation of the fields:
// https://www.freedesktop.org/software/systemd/man/os-release.html
@@ -19,11 +30,6 @@ void ffPrintOS(FFinstance* instance)
char versionCodename[128]; versionCodename[0] = '\0';
char buildId[128]; buildId[0] = '\0';
FILE* osRelease = fopen("/etc/os-release", "r");
if(osRelease == NULL)
osRelease = fopen("/usr/lib/os-release", "r");
char* line = NULL;
size_t len = 0;
@@ -51,11 +57,11 @@ void ffPrintOS(FFinstance* instance)
sscanf(line, "BUILD_ID=\"%[^\"]+", buildId);
}
fclose(osRelease);
if(line != NULL)
free(line);
fclose(osRelease);
FF_STRBUF_CREATE(os);
if(instance->config.osFormat.length == 0)
+6 -6
View File
@@ -30,14 +30,14 @@ void ffPrintPackages(FFinstance* instance)
uint32_t all = pacman + flatpak;
if(all == 0)
{
ffPrintError(instance, &instance->config.packagesKey, "Packages", "No packages from known package managers found");
return;
}
if(instance->config.packagesFormat.length == 0)
{
if(all == 0)
{
ffPrintError(instance, &instance->config.packagesKey, "Packages", "No packages from known package managers found");
return;
}
ffPrintLogoAndKey(instance, &instance->config.packagesKey, "Packages");
#define FF_PRINT_PACKAGE(name) \
+6 -6
View File
@@ -85,6 +85,12 @@ void ffPrintTerminal(FFinstance* instance)
ffGetTerminal(instance, &exeName, &processName, &error);
if(error->length > 0)
{
ffPrintError(instance, &instance->config.terminalKey, "Terminal", error->chars);
return;
}
FFstrbuf* name;
if(ffStrbufStartsWith(exeName, processName))
@@ -94,12 +100,6 @@ void ffPrintTerminal(FFinstance* instance)
if(instance->config.terminalFormat.length == 0)
{
if(error->length > 0)
{
ffPrintError(instance, &instance->config.terminalKey, "Terminal", error->chars);
return;
}
ffPrintLogoAndKey(instance, &instance->config.terminalKey, "Terminal");
ffStrbufPutTo(name, stdout);
}
+6 -6
View File
@@ -35,14 +35,14 @@ void ffPrintWM(FFinstance* instance)
closedir(proc);
if(prettyName.length == 0)
{
ffPrintError(instance, &instance->config.wmKey, "WM", "No process name matches the name of known display managers");
return;
}
if(instance->config.wmFormat.length == 0)
{
if(prettyName.length == 0)
{
ffPrintError(instance, &instance->config.wmKey, "WM", "No process name matches the name of known display managers");
return;
}
ffPrintLogoAndKey(instance, &instance->config.wmKey, "WM");
ffStrbufPutTo(&prettyName, stdout);
}