From b9440f4b8ad9b1b5684facc409e13d963cb37f11 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 12 Jun 2019 17:18:39 -0700 Subject: [PATCH] Linux doesn't have mergesort, so just use qsort by (length, alphabetic) --- ask.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/ask.c b/ask.c index 0adfd64..22d329f 100644 --- a/ask.c +++ b/ask.c @@ -297,9 +297,12 @@ static char *get_input(FILE *in, FILE *out, const char *prompt, const char *init return picked; } -static int cmp_len(const void *a, const void *b) +static int cmp_len(const void *v1, const void *v2) { - return (int)(strlen(*(char**)a) - strlen(*(char**)b)); + char *s1 = *(char**)v1, *s2 = *(char**)v2; + size_t len1 = strlen(s1), len2 = strlen(s2); + if (len1 != len2) return (int)(len1 - len2); + return strcmp(s1, s2); } int main(int argc, char *argv[]) @@ -404,8 +407,8 @@ int main(int argc, char *argv[]) if (tcsetattr(fileno(tty_out), TCSAFLUSH, &bb_termios) == -1) return 1; - // Prefer shorter matches, but otherwise keep original order (mergesort is stable) - mergesort(opts, (size_t)nopts, sizeof(char*), cmp_len); + // Prefer shorter matches, but otherwise use alphabetic order + qsort(opts, (size_t)nopts, sizeof(char*), cmp_len); char *output = get_input(tty_in, tty_out, prompt, query, nopts, opts); fflush(tty_out);