diff options
author | Harry Jeffery <harry@exec64.co.uk> | 2019-08-18 14:54:26 +0100 |
---|---|---|
committer | Harry Jeffery <harry@exec64.co.uk> | 2019-08-18 14:54:26 +0100 |
commit | 00ad6b1d8e946069963bc2ab09f4f01c8e81ab03 (patch) | |
tree | e3e0cef7a0c4ed26a1c7a4cb29630b0af3c92b48 /src/list.c | |
parent | 901243b3f97368c05f7ed1c723d4ef5d18f634a1 (diff) | |
download | imv-00ad6b1d8e946069963bc2ab09f4f01c8e81ab03.tar.gz |
imv: Add bind command
Diffstat (limited to 'src/list.c')
-rw-r--r-- | src/list.c | 27 |
1 files changed, 27 insertions, 0 deletions
@@ -1,5 +1,7 @@ #include "list.h" +#include <assert.h> + struct list *list_create(void) { struct list *list = malloc(sizeof *list); @@ -107,4 +109,29 @@ int list_find(struct list *list, int (*cmp)(const void *, const void *), const v return -1; } +char *list_to_string(struct list *list, const char *sep, size_t start) +{ + size_t len = 0; + size_t cap = 512; + char *buf = malloc(cap); + buf[0] = 0; + + size_t sep_len = strlen(sep); + for (size_t i = start; i < list->len; ++i) { + size_t item_len = strlen(list->items[i]); + if (len + item_len + sep_len >= cap) { + cap *= 2; + buf = realloc(buf, cap); + assert(buf); + } + + strncat(buf, list->items[i], cap - 1); + len += item_len; + + strncat(buf, sep, cap - 1); + len += sep_len; + } + return buf; +} + /* vim:set ts=2 sts=2 sw=2 et: */ |