1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-25 01:34:28 +03:00

systemctl: move strv_skip_first() out of systemctl.c

Make it generic, call it strv_skip() and move it to strv.[ch]
This commit is contained in:
Lennart Poettering 2015-09-24 12:23:21 +02:00
parent 56a730fa56
commit e3ead6bb42
4 changed files with 44 additions and 13 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -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;
}

View File

@ -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;
}