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.
This commit is contained in:
parent
8759e68de2
commit
8f35e16333
|
@ -0,0 +1,27 @@
|
|||
#ifndef __STRING_H
|
||||
#define __STRING_H
|
||||
|
||||
#include <list.h>
|
||||
|
||||
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 */
|
|
@ -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
|
||||
|
|
|
@ -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");
|
||||
}
|
Loading…
Reference in New Issue