From: Zbigniew Jędrzejewski-Szmek Date: Thu, 1 Feb 2018 11:50:18 +0000 (+0100) Subject: basic/strv: add function to insert items at position X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=5cef9f46e2e33b6843310531dd7f283537c30465;p=elogind.git basic/strv: add function to insert items at position --- diff --git a/src/basic/strv.c b/src/basic/strv.c index f3bb6965a..85c55041e 100644 --- a/src/basic/strv.c +++ b/src/basic/strv.c @@ -452,7 +452,7 @@ int strv_push_pair(char ***l, char *a, char *b) { return 0; } -int strv_push_prepend(char ***l, char *value) { +int strv_insert(char ***l, unsigned position, char *value) { char **c; unsigned n, m, i; @@ -460,6 +460,7 @@ int strv_push_prepend(char ***l, char *value) { return 0; n = strv_length(*l); + position = MIN(position, n); /* increase and check for overflow */ m = n + 2; @@ -470,10 +471,12 @@ int strv_push_prepend(char ***l, char *value) { if (!c) return -ENOMEM; - for (i = 0; i < n; i++) + for (i = 0; i < position; i++) + c[i] = (*l)[i]; + c[position] = value; + for (i = position; i < n; i++) c[i+1] = (*l)[i]; - c[0] = value; c[n+1] = NULL; free(*l); diff --git a/src/basic/strv.h b/src/basic/strv.h index 489be2f77..2ef6d325d 100644 --- a/src/basic/strv.h +++ b/src/basic/strv.h @@ -58,7 +58,12 @@ int strv_extendf(char ***l, const char *format, ...) _printf_(2,0); int strv_extend_front(char ***l, const char *value); 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_insert(char ***l, unsigned position, char *value); + +static inline int strv_push_prepend(char ***l, char *value) { + return strv_insert(l, 0, value); +} + int strv_consume(char ***l, char *value); #if 0 /// UNNEEDED by elogind int strv_consume_pair(char ***l, char *a, char *b);