From 0e57122ecfd554131fd6ad54fcbb556733bf6c88 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Thu, 28 Jan 2021 19:27:23 -0500 Subject: qsort_with_context on systems without qsort_r/s --- main.c | 5 +++++ util.c | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index 176f972..8248ac3 100644 --- a/main.c +++ b/main.c @@ -179,6 +179,11 @@ int main(int argc, char **argv) { } #endif } + #if TED_FORCE_SEARCH_CWD + // override whether or not we are in the executable's directory + // (for testing on Unix systems without /proc) + ted->search_cwd = true; + #endif Settings *settings = &ted->settings; diff --git a/util.c b/util.c index b68efd3..d805830 100644 --- a/util.c +++ b/util.c @@ -191,6 +191,12 @@ static int str_qsort_case_insensitive_cmp(const void *av, const void *bv) { return strcmp_case_insensitive(*a, *b); } +static void *qsort_ctx_data; +static int (*qsort_ctx_cmp)(const void *, const void *, void *data); +static int qsort_with_context_cmp(const void *a, const void *b) { + return qsort_ctx_cmp(a, b, qsort_ctx_data); +} + // qsort but with a user void* static void qsort_with_context(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *arg) { #if _WIN32 @@ -199,6 +205,9 @@ static void qsort_with_context(void *base, size_t nmemb, size_t size, int (*comp // GNU doesn't have qsort_s ): qsort_r(base, nmemb, size, compar, arg); #else -#error "No qsort_r/qsort_s. You can try filling in this function if you know what you're doing." + // just use global variables. hopefully we don't try to run this in something multithreaded! + qsort_ctx_data = arg; + qsort_ctx_cmp = compar; + qsort(base, nmemb, size, qsort_with_context_cmp); #endif } -- cgit v1.2.3