File: cmp.h

package info (click to toggle)
shadow 1%3A4.19.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 67,276 kB
  • sloc: sh: 44,701; ansic: 34,184; xml: 12,350; exp: 3,691; makefile: 1,656; python: 1,409; perl: 120; sed: 16
file content (86 lines) | stat: -rw-r--r-- 1,761 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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(T)                                                        \
(                                                                     \
	_Generic((T){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