From 857a493d55f94731394e4d9f61ffce661858e9a0 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 19 Oct 2012 04:52:25 +0200 Subject: [PATCH] set: introduce strv_sort() --- src/shared/strv.c | 15 +++++++++++++++ src/shared/strv.h | 2 ++ src/timedate/timedatectl.c | 11 ++--------- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/shared/strv.c b/src/shared/strv.c index 822b2dc88..1b8e27bc1 100644 --- a/src/shared/strv.c +++ b/src/shared/strv.c @@ -735,3 +735,18 @@ bool strv_overlap(char **a, char **b) { return false; } + +static int str_compare(const void *_a, const void *_b) { + const char **a = (const char**) _a, **b = (const char**) _b; + + return strcmp(*a, *b); +} + +char **strv_sort(char **l) { + + if (strv_isempty(l)) + return l; + + qsort(l, strv_length(l), sizeof(char*), str_compare); + return l; +} diff --git a/src/shared/strv.h b/src/shared/strv.h index 81e33356f..45558d896 100644 --- a/src/shared/strv.h +++ b/src/shared/strv.h @@ -80,3 +80,5 @@ bool strv_overlap(char **a, char **b); #define STRV_FOREACH_BACKWARDS(s, l) \ for (; (l) && ((s) >= (l)); (s)--) + +char **strv_sort(char **l); diff --git a/src/timedate/timedatectl.c b/src/timedate/timedatectl.c index 2b006ce92..469a65e63 100644 --- a/src/timedate/timedatectl.c +++ b/src/timedate/timedatectl.c @@ -349,12 +349,6 @@ static int set_ntp(DBusConnection *bus, char **args, unsigned n) { DBUS_TYPE_INVALID); } -static int zone_compare(const void *_a, const void *_b) { - const char **a = (const char**) _a, **b = (const char**) _b; - - return strcmp(*a, *b); -} - static int list_timezones(DBusConnection *bus, char **args, unsigned n) { _cleanup_fclose_ FILE *f = NULL; _cleanup_strv_free_ char **zones = NULL; @@ -416,12 +410,11 @@ static int list_timezones(DBusConnection *bus, char **args, unsigned n) { } if (zones) - zones[n_zones] = 0; - - qsort(zones, n_zones, sizeof(char*), zone_compare); + zones[n_zones] = NULL; pager_open_if_enabled(); + strv_sort(zones); STRV_FOREACH(i, zones) puts(*i); -- 2.30.2