use array for items 3c067598
Connor Lane Smith · 2011-05-14 17:46 1 file(s) · +10 −10
dmenu.c +10 −10
1 -
/* See LICENSE file for copyright and license details. */
1 +
/* See LICENSE file for copynext and license details. */
2 2
#include <ctype.h>
3 3
#include <stdio.h>
4 4
#include <stdlib.h>
19 19
typedef struct Item Item;
20 20
struct Item {
21 21
	char *text;
22 -
	Item *next;          /* traverses all items */
23 -
	Item *left, *right;  /* traverses matching items */
22 +
	Item *left, *right;
24 23
};
25 24
26 25
static void appenditem(Item *item, Item **list, Item **last);
386 385
387 386
	len = strlen(text);
388 387
	matches = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
389 -
	for(item = items; item; item = item->next)
388 +
	for(item = items; item && item->text; item++)
390 389
		if(!fstrncmp(text, item->text, len + 1))
391 390
			appenditem(item, &lexact, &exactend);
392 391
		else if(!fstrncmp(text, item->text, len))
445 444
void
446 445
readstdin(void) {
447 446
	char buf[sizeof text], *p;
448 -
	Item *item, **end;
447 +
	size_t i, size = 0;
449 448
450 -
	for(end = &items; fgets(buf, sizeof buf, stdin); *end = item, end = &item->next) {
449 +
	for(i = 0; fgets(buf, sizeof buf, stdin); items[++i].text = NULL) {
450 +
		if(i+1 == size / sizeof *items || !items)
451 +
			if(!(items = realloc(items, (size += BUFSIZ))))
452 +
				eprintf("cannot realloc %u bytes:", size);
451 453
		if((p = strchr(buf, '\n')))
452 454
			*p = '\0';
453 -
		if(!(item = calloc(1, sizeof *item)))
454 -
			eprintf("cannot malloc %u bytes:", sizeof *item);
455 -
		if(!(item->text = strdup(buf)))
455 +
		if(!(items[i].text = strdup(buf)))
456 456
			eprintf("cannot strdup %u bytes:", strlen(buf)+1);
457 -
		inputw = MAX(inputw, textw(dc, item->text));
457 +
		inputw = MAX(inputw, textw(dc, items[i].text));
458 458
	}
459 459
}
460 460