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>
run_part.h \
run_part.c \
salt.c \
+ search/cmp/cmp.c \
+ search/cmp/cmp.h \
selinux.c \
semanage.c \
setugid.c \
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);
-// SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
+// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#include <stddef.h>
#include <stdlib.h>
+#include "search/cmp/cmp.h"
#include "sizeof.h"
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)
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]);
}
-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
--- /dev/null
+// 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);
--- /dev/null
+// 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
test_adds_SOURCES = \
../../lib/adds.c \
+ ../../lib/search/cmp/cmp.c \
test_adds.c \
$(NULL)
test_adds_CFLAGS = \