diff options
author | Leah Neukirchen <leah@vuxu.org> | 2024-06-07 12:27:29 +0200 |
---|---|---|
committer | Leah Neukirchen <leah@vuxu.org> | 2024-06-07 12:27:29 +0200 |
commit | 6b2d87eef41a3cee1dccca431dc4575fe4ce5cef (patch) | |
tree | ae950a7e85d27ac5e87e5edfc0d3f1df8ac26ddf | |
parent | 787ccd83d19d33de90db3df1554c4d05de2c30f4 (diff) | |
download | nano-exporter-6b2d87eef41a3cee1dccca431dc4575fe4ce5cef.tar.gz nano-exporter-6b2d87eef41a3cee1dccca431dc4575fe4ce5cef.tar.xz nano-exporter-6b2d87eef41a3cee1dccca431dc4575fe4ce5cef.zip |
move read_file_at to util.c
-rw-r--r-- | mdstat.c | 18 | ||||
-rw-r--r-- | util.c | 19 | ||||
-rw-r--r-- | util.h | 3 |
3 files changed, 22 insertions, 18 deletions
diff --git a/mdstat.c b/mdstat.c index bc8b62a..d2567a4 100644 --- a/mdstat.c +++ b/mdstat.c @@ -19,24 +19,6 @@ const struct collector mdstat_collector = { .collect = mdstat_collect, }; -static ssize_t -read_file_at(int dirfd, char *pathname, char *buf, size_t bufsiz) { - int fd = openat(dirfd, pathname, O_RDONLY); - if (fd < 0) - return -1; - - ssize_t r = read(fd, buf, bufsiz - 1); - close(fd); - if (r < 0) - return -1; - - if (buf[r-1] == '\n') - r--; - - buf[r] = 0; - return r; -} - void scrape_write_md_label(scrape_req *req, char *name, char *md, char *label, char *value, double d) { struct label labels[] = { diff --git a/util.c b/util.c index 0443c98..8f34536 100644 --- a/util.c +++ b/util.c @@ -16,6 +16,7 @@ #define _POSIX_C_SOURCE 200809L +#include <fcntl.h> #include <stdarg.h> #include <stdbool.h> #include <stddef.h> @@ -246,3 +247,21 @@ int write_all(int fd, const void *buf_ptr, size_t len) { return 0; } + +ssize_t +read_file_at(int dirfd, char *pathname, char *buf, size_t bufsiz) { + int fd = openat(dirfd, pathname, O_RDONLY); + if (fd < 0) + return -1; + + ssize_t r = read(fd, buf, bufsiz - 1); + close(fd); + if (r < 0) + return -1; + + if (buf[r-1] == '\n') + r--; + + buf[r] = 0; + return r; +} diff --git a/util.h b/util.h index 12388c0..c54bfab 100644 --- a/util.h +++ b/util.h @@ -20,6 +20,7 @@ #include <stdbool.h> #include <stddef.h> #include <stdio.h> +#include <unistd.h> // character buffers @@ -120,6 +121,8 @@ char *fgets_line(char *s, int size, FILE *stream); */ int write_all(int fd, const void *buf, size_t len); +ssize_t read_file_at(int dirfd, char *pathname, char *buf, size_t bufsiz); + #ifndef PATH /** Macro for constructing absolute paths in release, relative paths in tests. */ #define PATH(p) p |