NeoMutt  2025-12-11-694-ga89709
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
qsort_r.c
Go to the documentation of this file.
1
23
29
30#ifdef __sun
31/* According to qsort(3C) Solaris needs this definition to access qsort_s */
32#define __STDC_WANT_LIB_EXT1__ 1
33#endif
34
35#include "config.h"
36#include <stddef.h>
37#include <stdlib.h>
38#include "qsort_r.h"
39#include "lib.h" // IWYU pragma: keep
40
41#if !defined(HAVE_QSORT_S) && !defined(HAVE_QSORT_R)
43static sort_t GlobalCompar = NULL;
45static void *GlobalData = NULL;
46
55static int relay_compar(const void *a, const void *b)
56{
57 return GlobalCompar(a, b, GlobalData);
58}
59#endif
60
72void mutt_qsort_r(void *base, size_t nmemb, size_t size, sort_t compar, void *sdata)
73{
74#ifdef HAVE_QSORT_S
75 /* FreeBSD 13, where qsort_r had incompatible signature but qsort_s works */
76 qsort_s(base, nmemb, size, compar, sdata);
77#elif defined(HAVE_QSORT_R)
78 /* glibc, POSIX (https://www.austingroupbugs.net/view.php?id=900) */
79 qsort_r(base, nmemb, size, compar, sdata);
80#else
81 /* This fallback is not re-entrant. */
83 GlobalCompar = compar;
84 GlobalData = sdata;
85 qsort(base, nmemb, size, relay_compar);
86 GlobalCompar = NULL;
87 GlobalData = NULL;
88#endif
89}
Convenience wrapper for the library headers.
static sort_t GlobalCompar
Original comparator in fallback implementation.
Definition qsort_r.c:43
static int relay_compar(const void *a, const void *b)
Shim to pass context through to real comparator.
Definition qsort_r.c:55
static void * GlobalData
Original opaque data in fallback implementation.
Definition qsort_r.c:45
void mutt_qsort_r(void *base, size_t nmemb, size_t size, sort_t compar, void *sdata)
Sort an array, where the comparator has access to opaque data rather than requiring global variables.
Definition qsort_r.c:72
Context-free sorting function.
int(* sort_t)(const void *a, const void *b, void *sdata)
Definition qsort_r.h:41
#define ASSERT(COND)
Definition signal2.h:59