From 77f5494dd9c7ab87875aa93d7313062566c713e9 Mon Sep 17 00:00:00 2001 From: Drath Date: Mon, 11 May 2026 12:12:43 +0000 Subject: [PATCH] simplify shit --- include/utils.h | 4 +--- src/utils.c | 47 +++++++++++++++++------------------------------ 2 files changed, 18 insertions(+), 33 deletions(-) diff --git a/include/utils.h b/include/utils.h index 91e1b79..89fa659 100644 --- a/include/utils.h +++ b/include/utils.h @@ -29,11 +29,9 @@ char *pretty_num(double num, char *buf, size_t bufsz); char *pretty_bytes(uint64_t bytes, int8_t start_suffix, char *buf, size_t bufsz); -bool file_exists(const char *path); - int get_hwmon_dir(char *path, size_t bufsz); -size_t read_file(const char *path, char *buffer, size_t n); +size_t read_file(const char *dir, const char *file, char *buffer, size_t n); void fast_read_num_u64(const char *dir, const char *file, char *buf, uint64_t *result, uint32_t divider); diff --git a/src/utils.c b/src/utils.c index 1414378..9e4ace4 100644 --- a/src/utils.c +++ b/src/utils.c @@ -10,13 +10,6 @@ #include "log.h" #include "utils.h" -#define MKPATH(path, dir, file) \ - do { \ - char *nt = stpcpy(path, dir); \ - *nt++ = '/'; \ - stpcpy(nt, file); \ - } while (0) - inline void timespec_add_ms(struct timespec *ts, const uint16_t ms) { ts->tv_nsec += ms * 1000000L; if (ts->tv_nsec >= 1000000000L) { @@ -50,10 +43,6 @@ inline char *pretty_bytes(const uint64_t bytes, int8_t start_suffix, char *buf, return buf; } -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, const size_t bufsz) { glob_t g; @@ -67,51 +56,49 @@ int get_hwmon_dir(char *path, const size_t bufsz) { return -1; } + +/* instead of building the path here we could build it once in *_detect funcs for each sensor, but thats a lot of paths damn + * actual slowdown is in the syscalls anyway, open(), read(), close () */ + /* read n bytes from path into buffer */ -inline size_t read_file(const char *path, char *buffer, const size_t n) { +inline size_t read_file(const char *dir, const char *file, char *buffer, const size_t n) { + char path[192]; // bug alert + char *nt = stpcpy(path, dir); // nt is a pointer to the null terminator of the copied string + stpcpy(nt, file); // write at the null terminator address, aka append to the end const int fd = open(path, O_RDONLY | O_CLOEXEC); if (fd < 0) { - applog(LOG_ERR, "Error reading %s", path); + if (buffer != NULL) + applog(LOG_ERR, "Error reading %s", path); return 0; } const ssize_t len = read(fd, buffer, n); - if (len >= 0) + if (len >= 0 && buffer) buffer[len] = '\0'; close(fd); return len; } inline void fast_read_num_u64(const char *dir, const char *file, char *buf, uint64_t *result, const uint32_t divider) { - char path[256]; - MKPATH(path, dir, file); - read_file(path, buf, 32); + read_file(dir, file, buf, 32); *result = strtoull(buf, NULL, 10) / divider; } inline void fast_read_num_u16(const char *dir, const char *file, char *buf, uint16_t *result, const uint32_t divider) { - char path[256]; - MKPATH(path, dir, file); - read_file(path, buf, 32); + read_file(dir, file, buf, 32); *result = strtol(buf, NULL, 10) / divider; } inline void fast_read_num_u8(const char *dir, const char *file, char *buf, uint8_t *result, const uint32_t divider) { - char path[256]; - MKPATH(path, dir, file); - read_file(path, buf, 32); + read_file(dir, file, buf, 32); *result = strtol(buf, NULL, 10) / divider; } inline void fast_read_num_i16(const char *dir, const char *file, char *buf, int16_t *result, const uint32_t divider) { - char path[256]; - MKPATH(path, dir, file); - read_file(path, buf, 32); - *result = strtol(buf, NULL, 10) / divider; + read_file(dir, file, buf, 32); + *result = (int16_t)(strtol(buf, NULL, 10) / divider); } inline void fast_read_num_f(const char *dir, const char *file, char *buf, float *result, const uint32_t divider) { - char path[256]; - MKPATH(path, dir, file); - read_file(path, buf, 32); + read_file(dir, file, buf, 32); *result = (float)strtoull(buf, NULL, 10) / (float)divider; } \ No newline at end of file