readstdin: allocate amount of items ba1a347d
Keep track of the amount of items (not a total buffer size), allocate an array of
new items. For now change BUFSIZ bytes to 256 * sizeof(struct item)).
Hiltjo Posthuma · 2022-10-31 11:52 1 file(s) · +6 −4
dmenu.c +6 −4
550 550
readstdin(void)
551 551
{
552 552
	char *line = NULL;
553 -
	size_t i, junk, size = 0;
553 +
	size_t i, junk, itemsiz = 0;
554 554
	ssize_t len;
555 555
556 556
	/* read each line from stdin and add it to the item list */
557 557
	for (i = 0; (len = getline(&line, &junk, stdin)) != -1; i++) {
558 -
		if (i + 1 >= size / sizeof *items)
559 -
			if (!(items = realloc(items, (size += BUFSIZ))))
560 -
				die("cannot realloc %zu bytes:", size);
558 +
		if (i + 1 >= itemsiz) {
559 +
			itemsiz += 256;
560 +
			if (!(items = realloc(items, itemsiz * sizeof(*items))))
561 +
				die("cannot realloc %zu bytes:", itemsiz * sizeof(*items));
562 +
		}
561 563
		if (line[len - 1] == '\n')
562 564
			line[len - 1] = '\0';
563 565
		items[i].text = line;