]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/search/cmp/, lib/, tests/: CMP(), cmp_*(): Add macro and functions
authorAlejandro Colomar <alx@kernel.org>
Thu, 14 Nov 2024 14:07:25 +0000 (15:07 +0100)
committerSerge Hallyn <serge@hallyn.com>
Fri, 24 Jan 2025 13:58:13 +0000 (07:58 -0600)
These macros are for use with bsearch(3),lfind(3),qsort(3).

Reviewed-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/Makefile.am
lib/adds.c
lib/adds.h
lib/search/cmp/cmp.c [new file with mode: 0644]
lib/search/cmp/cmp.h [new file with mode: 0644]
tests/unit/Makefile.am

index e76e7446a90ed8decfb7055bbe2d205d65ce8f0a..c474e465d9fdaa948c0563df2443507ca6b51942 100644 (file)
@@ -155,6 +155,8 @@ libshadow_la_SOURCES = \
        run_part.h \
        run_part.c \
        salt.c \
+       search/cmp/cmp.c \
+       search/cmp/cmp.h \
        selinux.c \
        semanage.c \
        setugid.c \
index 5d8c1537f201d0dbf0fb5942a1a99185865f00e8..693d0ee87b393621d097cdf45711a49b6462d268 100644 (file)
@@ -11,5 +11,3 @@
 
 extern inline long addsl2(long a, long b);
 extern inline long addslN(size_t n, long addend[n]);
-
-extern inline int cmpl(const void *p1, const void *p2);
index 6544ce5fe540c17d5220a0225e724f6ab38456f9..e047d681c5c56f8d52f4d7a1f7beaa223ab137d8 100644 (file)
@@ -1,4 +1,4 @@
-// SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
+// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
 // SPDX-License-Identifier: BSD-3-Clause
 
 
@@ -13,6 +13,7 @@
 #include <stddef.h>
 #include <stdlib.h>
 
+#include "search/cmp/cmp.h"
 #include "sizeof.h"
 
 
@@ -27,8 +28,6 @@
 inline long addsl2(long a, long b);
 inline long addslN(size_t n, long addend[n]);
 
-inline int cmpl(const void *p1, const void *p2);
-
 
 inline long
 addsl2(long a, long b)
@@ -57,7 +56,7 @@ addslN(size_t n, long addend[n])
 
        e = errno;
        while (n > 1) {
-               qsort(addend, n, sizeof(addend[0]), cmpl);
+               qsort(addend, n, sizeof(addend[0]), cmp_long);
 
                errno = 0;
                addend[0] = addsl2(addend[0], addend[--n]);
@@ -69,18 +68,4 @@ addslN(size_t n, long addend[n])
 }
 
 
-inline int
-cmpl(const void *p1, const void *p2)
-{
-       const long  *l1 = p1;
-       const long  *l2 = p2;
-
-       if (*l1 < *l2)
-               return -1;
-       if (*l1 > *l2)
-               return +1;
-       return 0;
-}
-
-
 #endif  // include guard
diff --git a/lib/search/cmp/cmp.c b/lib/search/cmp/cmp.c
new file mode 100644 (file)
index 0000000..5677b85
--- /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/cmp/cmp.h"
+
+
+extern inline int cmp_int(const void *key, const void *elt);
+extern inline int cmp_long(const void *key, const void *elt);
+extern inline int cmp_uint(const void *key, const void *elt);
+extern inline int cmp_ulong(const void *key, const void *elt);
diff --git a/lib/search/cmp/cmp.h b/lib/search/cmp/cmp.h
new file mode 100644 (file)
index 0000000..18687d4
--- /dev/null
@@ -0,0 +1,86 @@
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#ifndef SHADOW_INCLUDE_LIB_SEARCH_CMP_CMP_H_
+#define SHADOW_INCLUDE_LIB_SEARCH_CMP_CMP_H_
+
+
+#include <config.h>
+
+
+#define CMP(TYPE)                                                     \
+(                                                                     \
+       _Generic((TYPE) 0,                                            \
+               int *:            cmp_int,                            \
+               long *:           cmp_long,                           \
+               unsigned int *:   cmp_uint,                           \
+               unsigned long *:  cmp_ulong                           \
+       )                                                             \
+)
+
+
+/* Compatible with bsearch(3), lfind(3), and qsort(3).  */
+inline int cmp_int(const void *key, const void *elt);
+inline int cmp_long(const void *key, const void *elt);
+inline int cmp_uint(const void *key, const void *elt);
+inline int cmp_ulong(const void *key, const void *elt);
+
+
+inline int
+cmp_int(const void *key, const void *elt)
+{
+       const int  *k = key;
+       const int  *e = elt;
+
+       if (*k < *e)
+               return -1;
+       if (*k > *e)
+               return +1;
+       return 0;
+}
+
+
+inline int
+cmp_long(const void *key, const void *elt)
+{
+       const long  *k = key;
+       const long  *e = elt;
+
+       if (*k < *e)
+               return -1;
+       if (*k > *e)
+               return +1;
+       return 0;
+}
+
+
+inline int
+cmp_uint(const void *key, const void *elt)
+{
+       const unsigned int  *k = key;
+       const unsigned int  *e = elt;
+
+       if (*k < *e)
+               return -1;
+       if (*k > *e)
+               return +1;
+       return 0;
+}
+
+
+inline int
+cmp_ulong(const void *key, const void *elt)
+{
+       const unsigned long  *k = key;
+       const unsigned long  *e = elt;
+
+       if (*k < *e)
+               return -1;
+       if (*k > *e)
+               return +1;
+       return 0;
+}
+
+
+#endif  // include guard
index 6a83973d7133b036362fea9d9feda94caf7b1120..4dff894ace522db671bd909e36eb2f38b965810f 100644 (file)
@@ -23,6 +23,7 @@ check_PROGRAMS += \
 
 test_adds_SOURCES = \
     ../../lib/adds.c \
+    ../../lib/search/cmp/cmp.c \
     test_adds.c \
     $(NULL)
 test_adds_CFLAGS = \