summaryrefslogtreecommitdiffstats
path: root/lib/stringlist.c
blob: 9ccf8fa836e6a700fec502d57494b72b8f2ac5ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#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 + 1);
	if (len == 0)
		len = 1;

	i = 0;
	list_for_each_entry(entry, &sl->list, list) {
		if (!(++i % num))
			printf("%s\n", entry->str);
		else
			printf("%-*s", len, entry->str);
	}
	if (i % num)
		printf("\n");
}