nah it was ass

This commit is contained in:
2026-05-10 00:47:59 +02:00
parent 4be515392d
commit 8e2d2e04de
2 changed files with 31 additions and 9 deletions

View File

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

View File

@@ -2,20 +2,31 @@
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include "log.h"
#include <glob.h>
#include <string.h>
#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;
}