From: Michael Tremer Date: Sun, 28 Sep 2025 11:13:45 +0000 (+0000) Subject: util: Add helper function to read integers from file X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8dd453b905acdcc67e800685832db65d4ff31349;p=telemetry.git util: Add helper function to read integers from file Signed-off-by: Michael Tremer --- diff --git a/src/daemon/util.c b/src/daemon/util.c index 6231c47..34e0de9 100644 --- a/src/daemon/util.c +++ b/src/daemon/util.c @@ -19,6 +19,7 @@ #############################################################################*/ #include +#include #include #include "util.h" @@ -40,3 +41,31 @@ int __collecty_format_number(char* buffer, size_t length, int number) { return 0; } + +int collecty_file_read_uint64(const char* path, uint64_t* number) { + unsigned long n = 0; + FILE* f = NULL; + int r; + + // Open the file + f = fopen(path, "r"); + if (!f) + return -errno; + + // Read the number + r = fscanf(f, "%lu", &n); + if (r != 1) { + r = -errno; + goto ERROR; + } + + // Return the number + *number = n; + r = 0; + +ERROR: + if (f) + fclose(f); + + return r; +} diff --git a/src/daemon/util.h b/src/daemon/util.h index 04d8dfa..b97084b 100644 --- a/src/daemon/util.h +++ b/src/daemon/util.h @@ -21,9 +21,13 @@ #ifndef COLLECTY_UTIL_H #define COLLECTY_UTIL_H +#include + #define collecty_format_number(buffer, number) \ __collecty_format_number(buffer, sizeof(buffer), number) int __collecty_format_number(char* buffer, size_t length, int number); +int collecty_file_read_uint64(const char* path, uint64_t* number); + #endif /* COLLECTY_UTIL_H */