From e3ead6bb42f7c0f18d0ac100d33b71913fe4dcca Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 24 Sep 2015 12:23:21 +0200 Subject: systemctl: move strv_skip_first() out of systemctl.c Make it generic, call it strv_skip() and move it to strv.[ch] --- src/basic/strv.c | 12 ++++++++++++ src/basic/strv.h | 2 ++ src/systemctl/systemctl.c | 20 +++++++------------- src/test/test-strv.c | 23 +++++++++++++++++++++++ 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/basic/strv.c b/src/basic/strv.c index 92f900936e..9524e80a6f 100644 --- a/src/basic/strv.c +++ b/src/basic/strv.c @@ -733,3 +733,15 @@ char ***strv_free_free(char ***l) { free(l); return NULL; } + +char **strv_skip(char **l, size_t n) { + + while (n > 0) { + if (strv_isempty(l)) + return l; + + l++, n--; + } + + return l; +} diff --git a/src/basic/strv.h b/src/basic/strv.h index 713e91f8f8..4c4b6526de 100644 --- a/src/basic/strv.h +++ b/src/basic/strv.h @@ -156,3 +156,5 @@ static inline bool strv_fnmatch_or_empty(char* const* patterns, const char *s, i } char ***strv_free_free(char ***l); + +char **strv_skip(char **l, size_t n); diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index f7deb439da..059e51c2cd 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -146,12 +146,6 @@ static int daemon_reload(sd_bus *bus, char **args); static int halt_now(enum action a); static int check_one_unit(sd_bus *bus, const char *name, const char *good_states, bool quiet); -static char** strv_skip_first(char **strv) { - if (strv_length(strv) > 0) - return strv + 1; - return NULL; -} - static void pager_open_if_enabled(void) { if (arg_no_pager) @@ -664,7 +658,7 @@ static int list_units(sd_bus *bus, char **args) { pager_open_if_enabled(); - r = get_unit_list_recursive(bus, strv_skip_first(args), &unit_infos, &replies, &machines); + r = get_unit_list_recursive(bus, strv_skip(args, 1), &unit_infos, &replies, &machines); if (r < 0) return r; @@ -870,7 +864,7 @@ static int list_sockets(sd_bus *bus, char **args) { pager_open_if_enabled(); - n = get_unit_list_recursive(bus, strv_skip_first(args), &unit_infos, &replies, &machines); + n = get_unit_list_recursive(bus, strv_skip(args, 1), &unit_infos, &replies, &machines); if (n < 0) return n; @@ -1178,7 +1172,7 @@ static int list_timers(sd_bus *bus, char **args) { pager_open_if_enabled(); - n = get_unit_list_recursive(bus, strv_skip_first(args), &unit_infos, &replies, &machines); + n = get_unit_list_recursive(bus, strv_skip(args, 1), &unit_infos, &replies, &machines); if (n < 0) return n; @@ -1368,7 +1362,7 @@ static int list_unit_files(sd_bus *bus, char **args) { } HASHMAP_FOREACH(u, h, i) { - if (!output_show_unit_file(u, strv_skip_first(args))) + if (!output_show_unit_file(u, strv_skip(args, 1))) continue; units[c++] = *u; @@ -1408,7 +1402,7 @@ static int list_unit_files(sd_bus *bus, char **args) { unit_file_state_from_string(state) }; - if (output_show_unit_file(&units[c], strv_skip_first(args))) + if (output_show_unit_file(&units[c], strv_skip(args, 1))) c ++; } @@ -1889,7 +1883,7 @@ static int list_machines(sd_bus *bus, char **args) { pager_open_if_enabled(); - r = get_machine_list(bus, &machine_infos, strv_skip_first(args)); + r = get_machine_list(bus, &machine_infos, strv_skip(args, 1)); if (r < 0) return r; @@ -2121,7 +2115,7 @@ static int list_jobs(sd_bus *bus, char **args) { while ((r = sd_bus_message_read(reply, "(usssoo)", &id, &name, &type, &state, &job_path, &unit_path)) > 0) { struct job_info job = { id, name, type, state }; - if (!output_show_job(&job, strv_skip_first(args))) { + if (!output_show_job(&job, strv_skip(args, 1))) { skipped = true; continue; } diff --git a/src/test/test-strv.c b/src/test/test-strv.c index bff43950a9..cc47fd4d34 100644 --- a/src/test/test-strv.c +++ b/src/test/test-strv.c @@ -569,6 +569,28 @@ static void test_strv_shell_escape(void) { assert_se(streq_ptr(v[3], NULL)); } +static void test_strv_skip_one(char **a, size_t n, char **b) { + a = strv_skip(a, n); + assert_se(strv_equal(a, b)); +} + +static void test_strv_skip(void) { + test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 0, STRV_MAKE("foo", "bar", "baz")); + test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 1, STRV_MAKE("bar", "baz")); + test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 2, STRV_MAKE("baz")); + test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 3, STRV_MAKE(NULL)); + test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 4, STRV_MAKE(NULL)); + test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 55, STRV_MAKE(NULL)); + + test_strv_skip_one(STRV_MAKE("quux"), 0, STRV_MAKE("quux")); + test_strv_skip_one(STRV_MAKE("quux"), 1, STRV_MAKE(NULL)); + test_strv_skip_one(STRV_MAKE("quux"), 55, STRV_MAKE(NULL)); + + test_strv_skip_one(STRV_MAKE(NULL), 0, STRV_MAKE(NULL)); + test_strv_skip_one(STRV_MAKE(NULL), 1, STRV_MAKE(NULL)); + test_strv_skip_one(STRV_MAKE(NULL), 55, STRV_MAKE(NULL)); +} + int main(int argc, char *argv[]) { test_specifier_printf(); test_strv_foreach(); @@ -627,6 +649,7 @@ int main(int argc, char *argv[]) { test_strv_is_uniq(); test_strv_reverse(); test_strv_shell_escape(); + test_strv_skip(); return 0; } -- cgit v1.2.3-54-g00ecf