]> git.ipfire.org Git - telemetry.git/commitdiff
util: Add helper function to read integers from file
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 28 Sep 2025 11:13:45 +0000 (11:13 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 28 Sep 2025 11:13:45 +0000 (11:13 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/util.c
src/daemon/util.h

index 6231c47b9dd872e5d4fd59f39086081602f19796..34e0de9555c7149f0045f19bbe2c2ea6b1e8bcd6 100644 (file)
@@ -19,6 +19,7 @@
 #############################################################################*/
 
 #include <errno.h>
+#include <stdint.h>
 #include <stdio.h>
 
 #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;
+}
index 04d8dfa15b75c19b1d9532bb5cd9795d7ed0a3e2..b97084bfd4ea920522b0a6a918c3e766907e6234 100644 (file)
 #ifndef COLLECTY_UTIL_H
 #define COLLECTY_UTIL_H
 
+#include <stdint.h>
+
 #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 */