From 224848ebb6483faab8fef8db43d228b5d3eb465e Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Thu, 14 Nov 2024 15:07:25 +0100 Subject: [PATCH] lib/search/cmp/, lib/, tests/: CMP(), cmp_*(): Add macro and functions These macros are for use with bsearch(3),lfind(3),qsort(3). Reviewed-by: Serge Hallyn Signed-off-by: Alejandro Colomar --- lib/Makefile.am | 2 + lib/adds.c | 2 - lib/adds.h | 21 ++--------- lib/search/cmp/cmp.c | 13 +++++++ lib/search/cmp/cmp.h | 86 ++++++++++++++++++++++++++++++++++++++++++ tests/unit/Makefile.am | 1 + 6 files changed, 105 insertions(+), 20 deletions(-) create mode 100644 lib/search/cmp/cmp.c create mode 100644 lib/search/cmp/cmp.h diff --git a/lib/Makefile.am b/lib/Makefile.am index e76e7446a..c474e465d 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -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 \ diff --git a/lib/adds.c b/lib/adds.c index 5d8c1537f..693d0ee87 100644 --- a/lib/adds.c +++ b/lib/adds.c @@ -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); diff --git a/lib/adds.h b/lib/adds.h index 6544ce5fe..e047d681c 100644 --- a/lib/adds.h +++ b/lib/adds.h @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2023, Alejandro Colomar +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar // SPDX-License-Identifier: BSD-3-Clause @@ -13,6 +13,7 @@ #include #include +#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 index 000000000..5677b8514 --- /dev/null +++ b/lib/search/cmp/cmp.c @@ -0,0 +1,13 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#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 index 000000000..18687d444 --- /dev/null +++ b/lib/search/cmp/cmp.h @@ -0,0 +1,86 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_SEARCH_CMP_CMP_H_ +#define SHADOW_INCLUDE_LIB_SEARCH_CMP_CMP_H_ + + +#include + + +#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 diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am index 6a83973d7..4dff894ac 100644 --- a/tests/unit/Makefile.am +++ b/tests/unit/Makefile.am @@ -23,6 +23,7 @@ check_PROGRAMS += \ test_adds_SOURCES = \ ../../lib/adds.c \ + ../../lib/search/cmp/cmp.c \ test_adds.c \ $(NULL) test_adds_CFLAGS = \