nah it was ass
This commit is contained in:
30
src/utils.c
30
src/utils.c
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user