about summary refs log tree commit diff
path: root/util.c
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2024-06-07 12:27:29 +0200
committerLeah Neukirchen <leah@vuxu.org>2024-06-07 12:27:29 +0200
commit6b2d87eef41a3cee1dccca431dc4575fe4ce5cef (patch)
treeae950a7e85d27ac5e87e5edfc0d3f1df8ac26ddf /util.c
parent787ccd83d19d33de90db3df1554c4d05de2c30f4 (diff)
downloadnano-exporter-6b2d87eef41a3cee1dccca431dc4575fe4ce5cef.tar.gz
nano-exporter-6b2d87eef41a3cee1dccca431dc4575fe4ce5cef.tar.xz
nano-exporter-6b2d87eef41a3cee1dccca431dc4575fe4ce5cef.zip
move read_file_at to util.c
Diffstat (limited to 'util.c')
-rw-r--r--util.c19
1 files changed, 19 insertions, 0 deletions
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;
+}