Welcome to little lamb

Code » limb » commit a74e097

Add readopt.h & related functions

author Olivier Brunel
2023-05-09 18:51:08 UTC
committer Olivier Brunel
2023-07-05 07:37:02 UTC
parent b5774e7a5b311b9ddb5b9863776185f8231d000b

Add readopt.h & related functions

src/doc/readopt.h.0.md +24 -0
src/doc/readopt.h/readopt.3.md +38 -0
src/liblimb/include/limb/readopt.h +11 -0
src/liblimb/readopt.h/readopt.c +24 -0

diff --git a/src/doc/readopt.h.0.md b/src/doc/readopt.h.0.md
new file mode 100644
index 0000000..8e05535
--- /dev/null
+++ b/src/doc/readopt.h.0.md
@@ -0,0 +1,24 @@
+% limb manual
+% readopt.h(0)
+
+# NAME
+
+readopt.h - read option from configuration directory
+
+
+# SYNOPSIS
+
+    #include <limb/readopt.h>
+
+
+# DESCRIPTION
+
+The header defines the needed functions to read options from configuration
+dirctories.
+
+## Functions
+
+The following functions are defined :
+
+: [readopt](3)
+:: To read an option for a configuration directory.
diff --git a/src/doc/readopt.h/readopt.3.md b/src/doc/readopt.h/readopt.3.md
new file mode 100644
index 0000000..8a7443e
--- /dev/null
+++ b/src/doc/readopt.h/readopt.3.md
@@ -0,0 +1,38 @@
+% limb manual
+% readopt(3)
+
+# NAME
+
+readopt - read option from configuration directory
+
+# SYNOPSIS
+
+    #include <limb/readopt.h>
+
+```pre hl
+ssize_t readopt(char *<em>dst</em>, size_t <em>dlen</em>, int <em>fd</em>, const char *<em>name</em>)
+```
+
+# DESCRIPTION
+
+The `readopt`() function will read the first line from file `name` and write it
+into the memory pointed by `dst`, up to `dlen` bytes.
+
+If `name` specifies a relative path, it is relative to the directory associated
+with the file descriptor `fd`, which may be *AT_FDCWD* to use the current
+working directory.
+
+# RETURN VALUE
+
+On success the function returns the number of bytes written into `dst` (which is
+/not/ NUL-terminated). Otherwise, it returns -1 and sets `errno` to indicate the
+error.
+
+# ERRORS
+
+This function may fail if :
+
+: *ENOBUFS*
+:: Not enough space in `dst` to write the full first line of the file.
+
+This function may also fail for the errors described for [open_read_closeat](3).
diff --git a/src/liblimb/include/limb/readopt.h b/src/liblimb/include/limb/readopt.h
new file mode 100644
index 0000000..e4ef6ed
--- /dev/null
+++ b/src/liblimb/include/limb/readopt.h
@@ -0,0 +1,11 @@
+/* This file is part of limb                           https://lila.oss/limb
+ * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef LIMB_READOPT_h
+#define LIMB_READOPT_h
+
+#include <sys/types.h>
+
+extern ssize_t readopt(char *dst, size_t dlen, int bfd, const char *name);
+
+#endif /* LIMB_READOPT_h */
diff --git a/src/liblimb/readopt.h/readopt.c b/src/liblimb/readopt.h/readopt.c
new file mode 100644
index 0000000..719a1d0
--- /dev/null
+++ b/src/liblimb/readopt.h/readopt.c
@@ -0,0 +1,24 @@
+/* This file is part of limb                           https://lila.oss/limb
+ * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
+/* SPDX-License-Identifier: GPL-2.0-only */
+#include <errno.h>
+#include <limb/bytestr.h>
+#include <limb/readopt.h>
+#include <limb/unix-transactional.h>
+
+ssize_t
+readopt(char *dst, size_t dlen, int bfd, const char *name)
+{
+    ssize_t r;
+    r = open_read_closeat(dst, dlen, bfd, name);
+    if (r < 0 && errno != ENOBUFS) return -1;
+
+    if (r < 0) {
+        r = byte_chr(dst, dlen, '\n');
+        if ((size_t) r == dlen) return -1;
+    } else {
+        r = byte_chr(dst, r, '\n');
+    }
+
+    return r;
+}