]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
systemctl: move strv_skip_first() out of systemctl.c
authorLennart Poettering <lennart@poettering.net>
Thu, 24 Sep 2015 10:23:21 +0000 (12:23 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 29 Sep 2015 19:55:52 +0000 (21:55 +0200)
Make it generic, call it strv_skip() and move it to strv.[ch]

src/basic/strv.c
src/basic/strv.h
src/systemctl/systemctl.c
src/test/test-strv.c

index 92f900936e44c4dee5975f6d6140e36fe83a5f93..9524e80a6fbfbd6e99bacfb76f4b8ebac1c13870 100644 (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;
+}
index 713e91f8f8eecaea9fdca8ebd0835a0ef430a05a..4c4b6526de3bcdec3c0a3195a5fbafd7909a0a41 100644 (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);
index f7deb439dabd930ae9a1d653299e8d6726a20735..059e51c2cdef1c1b62e77d3d9af756e56cbf89ad 100644 (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;
                 }
index bff43950a9077cd602eb6b29f48f3c05d60343ea..cc47fd4d349ea7807baf5ed387d4333810fbf6e3 100644 (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;
 }