[systemd-commits] src/shared src/test
Lennart Poettering
lennart at kemper.freedesktop.org
Wed Dec 3 09:32:10 PST 2014
src/shared/strv.c | 42 +++++++++++++++++++++++++++++++++++++++++-
src/shared/strv.h | 2 ++
src/test/test-strv.c | 18 ++++++++++++++++++
3 files changed, 61 insertions(+), 1 deletion(-)
New commits:
commit 98940a3cd93807b5a3809bb1fb7ab43d450939f1
Author: Lennart Poettering <lennart at poettering.net>
Date: Wed Dec 3 18:31:51 2014 +0100
strv: add calls to add two entries to an strv at once
diff --git a/src/shared/strv.c b/src/shared/strv.c
index a5f8a2a..fdb658c 100644
--- a/src/shared/strv.c
+++ b/src/shared/strv.c
@@ -388,7 +388,7 @@ int strv_push(char ***l, char *value) {
n = strv_length(*l);
- /* increase and check for overflow */
+ /* Increase and check for overflow */
m = n + 2;
if (m < n)
return -ENOMEM;
@@ -404,6 +404,34 @@ int strv_push(char ***l, char *value) {
return 0;
}
+int strv_push_pair(char ***l, char *a, char *b) {
+ char **c;
+ unsigned n, m;
+
+ if (!a && !b)
+ return 0;
+
+ n = strv_length(*l);
+
+ /* increase and check for overflow */
+ m = n + !!a + !!b + 1;
+ if (m < n)
+ return -ENOMEM;
+
+ c = realloc_multiply(*l, sizeof(char*), m);
+ if (!c)
+ return -ENOMEM;
+
+ if (a)
+ c[n++] = a;
+ if (b)
+ c[n++] = b;
+ c[n] = NULL;
+
+ *l = c;
+ return 0;
+}
+
int strv_push_prepend(char ***l, char *value) {
char **c;
unsigned n, m, i;
@@ -444,6 +472,18 @@ int strv_consume(char ***l, char *value) {
return r;
}
+int strv_consume_pair(char ***l, char *a, char *b) {
+ int r;
+
+ r = strv_push_pair(l, a, b);
+ if (r < 0) {
+ free(a);
+ free(b);
+ }
+
+ return r;
+}
+
int strv_consume_prepend(char ***l, char *value) {
int r;
diff --git a/src/shared/strv.h b/src/shared/strv.h
index 774c67a..2c0280b 100644
--- a/src/shared/strv.h
+++ b/src/shared/strv.h
@@ -42,8 +42,10 @@ int strv_extend_strv_concat(char ***a, char **b, const char *suffix);
int strv_extend(char ***l, const char *value);
int strv_extendf(char ***l, const char *format, ...) _printf_(2,0);
int strv_push(char ***l, char *value);
+int strv_push_pair(char ***l, char *a, char *b);
int strv_push_prepend(char ***l, char *value);
int strv_consume(char ***l, char *value);
+int strv_consume_pair(char ***l, char *a, char *b);
int strv_consume_prepend(char ***l, char *value);
char **strv_remove(char **l, const char *s);
diff --git a/src/test/test-strv.c b/src/test/test-strv.c
index 180f062..674c1b5 100644
--- a/src/test/test-strv.c
+++ b/src/test/test-strv.c
@@ -453,6 +453,23 @@ static void test_strv_push_prepend(void) {
assert_se(!a[5]);
}
+static void test_strv_push(void) {
+ _cleanup_strv_free_ char **a = NULL;
+ char *i, *j;
+
+ assert_se(i = strdup("foo"));
+ assert_se(strv_push(&a, i) >= 0);
+
+ assert_se(i = strdup("a"));
+ assert_se(j = strdup("b"));
+ assert_se(strv_push_pair(&a, i, j) >= 0);
+
+ assert_se(streq_ptr(a[0], "foo"));
+ assert_se(streq_ptr(a[1], "a"));
+ assert_se(streq_ptr(a[2], "b"));
+ assert_se(streq_ptr(a[3], NULL));
+}
+
int main(int argc, char *argv[]) {
test_specifier_printf();
test_strv_foreach();
@@ -501,6 +518,7 @@ int main(int argc, char *argv[]) {
test_strv_extendf();
test_strv_from_stdarg_alloca();
test_strv_push_prepend();
+ test_strv_push();
return 0;
}
More information about the systemd-commits
mailing list