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 | |
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')
-rw-r--r-- | lib/Makefile | 1 | ||||
-rw-r--r-- | lib/stringlist.c | 45 |
2 files changed, 46 insertions, 0 deletions
diff --git a/lib/Makefile b/lib/Makefile index f6ae2d9891..b15d2ee5a1 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -13,6 +13,7 @@ obj-y += readkey.o obj-y += kfifo.o obj-y += libbb.o obj-y += libgen.o +obj-y += stringlist.o obj-y += recursive_action.o obj-y += make_directory.o obj-$(CONFIG_BZLIB) += bzlib.o bzlib_crctable.o bzlib_decompress.o bzlib_huffman.o bzlib_randtable.o 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"); +} |