diff --git a/include/stringlist.h b/include/stringlist.h new file mode 100644 index 0000000..87380cb --- /dev/null +++ b/include/stringlist.h @@ -0,0 +1,27 @@ +#ifndef __STRING_H +#define __STRING_H + +#include + +struct string_list { + struct list_head list; + char str[0]; +}; + +int string_list_add(struct string_list *sl, char *str); +void string_list_print_by_column(struct string_list *sl); + +static inline void string_list_init(struct string_list *sl) +{ + INIT_LIST_HEAD(&sl->list); +} + +static inline void string_list_free(struct string_list *sl) +{ + struct string_list *entry, *safe; + + list_for_each_entry_safe(entry, safe, &sl->list, list) + free(entry); +} + +#endif /* __STRING_H */ diff --git a/lib/Makefile b/lib/Makefile index f6ae2d9..b15d2ee 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -13,6 +13,7 @@ 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 0000000..bc3f7e7 --- /dev/null +++ b/lib/stringlist.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include + +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"); +}