shl: misc: add shl_dup_array_size() helper

This is the same as shl_dup_array() but the source might not be NULL
terminated so it takes a "size" argument.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
This commit is contained in:
David Herrmann 2012-10-23 12:32:37 +02:00
parent 936992298c
commit 0cd57e21ae

View File

@ -140,7 +140,7 @@ static inline int shl_split_string(const char *arg, char ***out,
return 0;
}
static inline int shl_dup_array(char ***out, char **argv)
static inline int shl_dup_array_size(char ***out, char **argv, size_t len)
{
char **t, *off;
unsigned int size, i;
@ -149,8 +149,11 @@ static inline int shl_dup_array(char ***out, char **argv)
return -EINVAL;
size = 0;
for (i = 0; argv[i]; ++i)
size += strlen(argv[i]) + 1;
for (i = 0; i < len; ++i) {
++size;
if (argv[i])
size += strlen(argv[i]);
}
++i;
size += i * sizeof(char*);
@ -161,9 +164,9 @@ static inline int shl_dup_array(char ***out, char **argv)
*out = t;
off = (char*)t + i * sizeof(char*);
while (*argv) {
while (len--) {
*t++ = off;
for (i = 0; argv[0][i]; ++i)
for (i = 0; *argv && argv[0][i]; ++i)
*off++ = argv[0][i];
*off++ = 0;
argv++;
@ -173,6 +176,19 @@ static inline int shl_dup_array(char ***out, char **argv)
return 0;
}
static inline int shl_dup_array(char ***out, char **argv)
{
unsigned int i;
if (!out || !argv)
return -EINVAL;
for (i = 0; argv[i]; ++i)
/* empty */ ;
return shl_dup_array_size(out, argv, i);
}
/* TODO: xkbcommon should provide these flags!
* We currently copy them into each library API we use so we need to keep
* them in sync. Currently, they're used in uterm-input and tsm-vte. */