]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/search/l/: LFIND(): Add macro
authorAlejandro Colomar <alx@kernel.org>
Thu, 14 Nov 2024 14:21:00 +0000 (15:21 +0100)
committerSerge Hallyn <serge@hallyn.com>
Fri, 24 Jan 2025 13:58:13 +0000 (07:58 -0600)
The use of typeof() for the function pointer argument was suggested by
Jorenar.  This improves readability of these complex types.

Co-authored-by: Martin Uecker <uecker@tugraz.at>
Cc: Jorenar <dev@jorenar.com>
Reviewed-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/Makefile.am
lib/search/l/lfind.c [new file with mode: 0644]
lib/search/l/lfind.h [new file with mode: 0644]

index c474e465d9fdaa948c0563df2443507ca6b51942..f34325a52c2746cd7c9dcb980a7bf8f0f64e695c 100644 (file)
@@ -157,6 +157,8 @@ libshadow_la_SOURCES = \
        salt.c \
        search/cmp/cmp.c \
        search/cmp/cmp.h \
+       search/l/lfind.c \
+       search/l/lfind.h \
        selinux.c \
        semanage.c \
        setugid.c \
diff --git a/lib/search/l/lfind.c b/lib/search/l/lfind.c
new file mode 100644 (file)
index 0000000..b41ea10
--- /dev/null
@@ -0,0 +1,13 @@
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#include <config.h>
+
+#include "search/l/lfind.h"
+
+#include <stddef.h>
+
+
+extern inline void *lfind_(const void *k, const void *a, size_t n, size_t ksize,
+    typeof(int (const void *k, const void *elt)) *cmp);
diff --git a/lib/search/l/lfind.h b/lib/search/l/lfind.h
new file mode 100644 (file)
index 0000000..2a6f6f6
--- /dev/null
@@ -0,0 +1,44 @@
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#ifndef SHADOW_INCLUDE_LIB_SEARCH_L_LFIND_H_
+#define SHADOW_INCLUDE_LIB_SEARCH_L_LFIND_H_
+
+
+#include <config.h>
+
+#include <search.h>
+#include <stddef.h>
+
+#include "must_be.h"
+#include "search/cmp/cmp.h"
+
+#include <assert.h>
+
+
+#define LFIND(k, a, n)                                                \
+({                                                                    \
+       __auto_type  k_ = k;                                          \
+       __auto_type  a_ = a;                                          \
+                                                                      \
+       static_assert(is_same_typeof(k_, a_), "");                    \
+                                                                      \
+       (typeof(k_)) lfind_(k_, a_, n, sizeof(*k_), CMP(typeof(k_))); \
+})
+
+
+inline void *lfind_(const void *k, const void *a, size_t n, size_t ksize,
+    typeof(int (const void *k, const void *elt)) *cmp);
+
+
+inline void *
+lfind_(const void *k, const void *a, size_t n, size_t ksize,
+    typeof(int (const void *k, const void *elt)) *cmp)
+{
+       // lfind(3) wants a pointer to n for historic reasons.
+       return lfind(k, a, &n, ksize, cmp);
+}
+
+
+#endif  // include guard