diff options
author | Sascha Hauer <s.hauer@pengutronix.de> | 2008-03-11 21:38:22 +0100 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2008-03-11 21:38:22 +0100 |
commit | 8f35e16333c98737c034ab1c0b060a577387255e (patch) | |
tree | 7eb2d00753be5b86ae582cb3f05cd9b1dc7359e2 /lib/stringlist.c | |
parent | 8759e68de2e2b4ce1793d36d668359eb80278c2c (diff) | |
download | barebox-8f35e16333c98737c034ab1c0b060a577387255e.tar.gz barebox-8f35e16333c98737c034ab1c0b060a577387255e.tar.xz |
add stringlist function. They can be used to build a list
of strings. For now mainly useful to print the resulting
list in columns which is used in tab completion and ls.
Diffstat (limited to 'lib/stringlist.c')
-rw-r--r-- | lib/stringlist.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/lib/stringlist.c b/lib/stringlist.c new file mode 100644 index 0000000000..bc3f7e7721 --- /dev/null +++ b/lib/stringlist.c @@ -0,0 +1,45 @@ +#include <common.h> +#include <xfuncs.h> +#include <malloc.h> +#include <stringlist.h> + +int string_list_add(struct string_list *sl, char *str) +{ + struct string_list *new; + + new = xmalloc(sizeof(struct string_list) + strlen(str) + 1); + + strcpy(new->str, str); + + list_add_tail(&new->list, &sl->list); + + return 0; +} + +void string_list_print_by_column(struct string_list *sl) +{ + int len = 0, num, i; + struct string_list *entry; + + list_for_each_entry(entry, &sl->list, list) { + int l = strlen(entry->str) + 4; + if (l > len) + len = l; + } + + if (!len) + return; + + num = 80 / len; + if (len == 0) + len = 1; + + i = 0; + list_for_each_entry(entry, &sl->list, list) { + printf("%-*s ", len, entry->str); + if (!(++i % num)) + printf("\n"); + } + if (i % num) + printf("\n"); +} |