From f674d7ab131f67c094b688faa21d0498be575509 Mon Sep 17 00:00:00 2001 From: Jeremy Sowden Date: Sun, 3 May 2020 10:55:36 +0100 Subject: [PATCH] wmacpi: use qsort. Signed-off-by: Jeremy Sowden --- wmacpi/libacpi.c | 43 +++++++++++++------------------------------ 1 file changed, 13 insertions(+), 30 deletions(-) diff --git a/wmacpi/libacpi.c b/wmacpi/libacpi.c index 4d51e10..47e27f9 100644 --- a/wmacpi/libacpi.c +++ b/wmacpi/libacpi.c @@ -23,6 +23,15 @@ static int data_source; /* local proto */ int acpi_get_design_cap(int batt); +static int +cmpstr (const void *va, const void *vb) +{ + const char *a = *(const char **) va; + const char *b = *(const char **) vb; + + return strcmp (a, b); +} + static int read_sysfs_file(char *node, char *prop, char *buf, size_t buflen) { char tmp[256]; @@ -63,7 +72,7 @@ static int sysfs_init_batteries(global_t *globals) char *name; char *names[MAXBATT]; char ps_type[16]; - int i, j; + int i; /* now enumerate batteries */ globals->battery_count = 0; @@ -96,20 +105,7 @@ static int sysfs_init_batteries(global_t *globals) } closedir(battdir); - /* A nice quick insertion sort, ala CLR. */ - { - char *tmp1, *tmp2; - - for (i = 1; i < globals->battery_count; i++) { - tmp1 = names[i]; - j = i - 1; - while ((j >= 0) && ((strcmp(tmp1, names[j])) < 0)) { - tmp2 = names[j+1]; - names[j+1] = names[j]; - names[j] = tmp2; - } - } - } + qsort(names, globals->battery_count, sizeof *names, cmpstr); for (i = 0; i < globals->battery_count; i++) { snprintf(batteries[i].name, MAX_NAME, "%s", names[i]); @@ -143,7 +139,7 @@ static int procfs_init_batteries(global_t *globals) struct dirent *batt; char *name; char *names[MAXBATT]; - int i, j; + int i; /* now enumerate batteries */ globals->battery_count = 0; @@ -170,20 +166,7 @@ static int procfs_init_batteries(global_t *globals) } closedir(battdir); - /* A nice quick insertion sort, ala CLR. */ - { - char *tmp1, *tmp2; - - for (i = 1; i < globals->battery_count; i++) { - tmp1 = names[i]; - j = i - 1; - while ((j >= 0) && ((strcmp(tmp1, names[j])) < 0)) { - tmp2 = names[j+1]; - names[j+1] = names[j]; - names[j] = tmp2; - } - } - } + qsort(names, globals->battery_count, sizeof *names, cmpstr); for (i = 0; i < globals->battery_count; i++) { snprintf(batteries[i].name, MAX_NAME, "%s", names[i]); -- 2.11.4.GIT