From 8e2d2e04deb4f6fc9f657264d0666ccb522e02ed Mon Sep 17 00:00:00 2001 From: drath Date: Sun, 10 May 2026 00:47:59 +0200 Subject: [PATCH] nah it was ass --- include/utils.h | 10 ++++++---- src/utils.c | 30 +++++++++++++++++++++++++----- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/include/utils.h b/include/utils.h index 15b634c..c9d4a80 100644 --- a/include/utils.h +++ b/include/utils.h @@ -1,9 +1,11 @@ #ifndef UTILS_H #define UTILS_H -#define fastReadNum(path, buf, result_ptr, div) \ -_Generic((result_ptr), \ -uint64_t*: _fRN_u64, \ -)(path, buf, sizeof(buf), result_ptr, div) +void fast_read_num_u64(const char* dir, const char* file, char* buf, uint64_t* result, uint16_t divider); +void fast_read_num_i16(const char* dir, const char* file, char* buf, int16_t* result, uint16_t divider); + +bool file_exists(const char *path); + +int get_hwmon_dir(char *path); #endif diff --git a/src/utils.c b/src/utils.c index e995251..a779108 100644 --- a/src/utils.c +++ b/src/utils.c @@ -2,20 +2,31 @@ #include #include #include -#include "log.h" #include +#include +#include "log.h" #include "utils.h" +#define MKPATH(path, dir, file) do {char *nt = stpcpy(path, dir); *nt++ = '/'; stpcpy(nt, file); } while (0) + inline bool file_exists(const char* path) { return access(path, F_OK) == 0; } +/* path has to have at least 8 free chars at the end for glob */ int get_hwmon_dir(char* path) { glob_t g; - if (glob(path, 0, NULL, &g) == 0) { + // const size_t len = strlen(path); + // strcpy(&path[len], "/hwmon*"); // append the glob to the end of the path + strncat(path, "/hwmon*", 8); + if (glob(path, 0, nullptr, &g) == 0) { + strncpy(path, g.gl_pathv[0], 256); + globfree(&g); + return 0; } + return -1; } /* read n bytes from path into buffer */ @@ -31,7 +42,16 @@ inline ssize_t read_file(const char* path, char* buffer, const ssize_t n) { return len; } -inline void _fRN_u64(const char* path, char* buf, uint64_t* result, const uint16_t divider) { - read_file(path, buf, 24); // wont ever be longer than 24 chars right? potential bug - *result = strtoull(buf, NULL, 10) / divider; +inline void fast_read_num_u64(const char* dir, const char* file, char* buf, uint64_t* result, const uint16_t divider) { + char path[256]; + MKPATH(path, dir, file); + read_file(path, buf, 32); + *result = strtoull(buf, nullptr, 10) / divider; +} + +inline void fast_read_num_i16(const char* dir, const char* file, char* buf, int16_t* result, const uint16_t divider) { + char path[256]; + MKPATH(path, dir, file); + read_file(path, buf, 32); + *result = strtol(buf, nullptr, 10) / divider; } \ No newline at end of file