simplify shit

This commit is contained in:
2026-05-11 12:12:43 +00:00
parent 00656d4ba3
commit 77f5494dd9
2 changed files with 18 additions and 33 deletions

View File

@@ -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);

View File

@@ -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;
}