Skip to content

Commit

Permalink
lib/search/cmp/, lib/, tests/: CMP(), cmp_*(): Add macro and functions
Browse files Browse the repository at this point in the history
These macros are for use with bsearch(3),lfind(3),qsort(3).

Reviewed-by: Serge Hallyn <[email protected]>
Signed-off-by: Alejandro Colomar <[email protected]>
  • Loading branch information
alejandro-colomar authored and hallyn committed Jan 24, 2025
1 parent de941a7 commit 224848e
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 20 deletions.
2 changes: 2 additions & 0 deletions lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
2 changes: 0 additions & 2 deletions lib/adds.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
21 changes: 3 additions & 18 deletions lib/adds.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2023, Alejandro Colomar <[email protected]>
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


Expand All @@ -13,6 +13,7 @@
#include <stddef.h>
#include <stdlib.h>

#include "search/cmp/cmp.h"
#include "sizeof.h"


Expand All @@ -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)
Expand Down Expand Up @@ -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]);
Expand All @@ -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
13 changes: 13 additions & 0 deletions lib/search/cmp/cmp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <[email protected]>
// 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);
86 changes: 86 additions & 0 deletions lib/search/cmp/cmp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <[email protected]>
// 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
1 change: 1 addition & 0 deletions tests/unit/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ check_PROGRAMS += \

test_adds_SOURCES = \
../../lib/adds.c \
../../lib/search/cmp/cmp.c \
test_adds.c \
$(NULL)
test_adds_CFLAGS = \
Expand Down

0 comments on commit 224848e

Please sign in to comment.