Bump to 32db2b.

readstdin: use getline(3)

currently readstdin():
   - fgets() into a local buffer,
   - strchr() the buffer to eleminate the newline
   - stdups() the buffer into items

a simpler way is to just use getline(3), which will do the allocation
for us; eliminating the need for stdup()-ing.

additionally getline returns back the amount of bytes read, which
eliminates the need for strchr()-ing to find the newline.

Ref.
https://git.suckless.org/dmenu/commit/32db2b125190d366be472ccb7cad833248696144.html
This commit is contained in:
bakkeby 2022-09-04 23:13:37 +02:00
parent 6a1ed51d47
commit 1bc6ec6fcd

30
dmenu.c
View File

@ -769,7 +769,7 @@ match(void)
#endif // NON_BLOCKING_STDIN_PATCH #endif // NON_BLOCKING_STDIN_PATCH
#if JSON_PATCH #if JSON_PATCH
if (json) if (json)
fstrstr = strcasestr; fstrstr = cistrstr;
#endif // JSON_PATCH #endif // JSON_PATCH
strcpy(buf, text); strcpy(buf, text);
@ -1416,13 +1416,18 @@ xinitvisual()
static void static void
readstdin(void) readstdin(void)
{ {
char buf[sizeof text], *p; char *line = NULL;
#if JSON_PATCH || TSV_PATCH
char *buf, *p;
#endif // JSON_PATCH | TSV_PATCH
#if JSON_PATCH #if JSON_PATCH
size_t i;
struct item *item; struct item *item;
#else #else
size_t i, size = 0; size_t size = 0;
#endif // JSON_PATCH #endif // JSON_PATCH
size_t i, junk;
ssize_t len;
#if PASSWORD_PATCH #if PASSWORD_PATCH
if (passwd) { if (passwd) {
@ -1432,7 +1437,7 @@ readstdin(void)
#endif // PASSWORD_PATCH #endif // PASSWORD_PATCH
/* read each line from stdin and add it to the item list */ /* read each line from stdin and add it to the item list */
for (i = 0; fgets(buf, sizeof buf, stdin); i++) { for (i = 0; (len = getline(&line, &junk, stdin)) != -1; i++, line = NULL) {
#if JSON_PATCH #if JSON_PATCH
item = itemnew(); item = itemnew();
#else #else
@ -1440,19 +1445,20 @@ readstdin(void)
if (!(items = realloc(items, (size += BUFSIZ)))) if (!(items = realloc(items, (size += BUFSIZ))))
die("cannot realloc %zu bytes:", size); die("cannot realloc %zu bytes:", size);
#endif // JSON_PATCH #endif // JSON_PATCH
if ((p = strchr(buf, '\n'))) if (line[len - 1] == '\n')
*p = '\0'; line[len - 1] = '\0';
#if JSON_PATCH #if JSON_PATCH
if (!(item->text = strdup(buf))) if (!(item->text = strdup(line)))
#else die("cannot strdup %zu bytes:", strlen(line) + 1);
if (!(items[i].text = strdup(buf)))
#endif // JSON_PATCH #endif // JSON_PATCH
die("cannot strdup %zu bytes:", strlen(buf) + 1); items[i].text = line;
#if TSV_PATCH #if TSV_PATCH
buf = strdup(line);
if ((p = strchr(buf, '\t'))) if ((p = strchr(buf, '\t')))
*p = '\0'; *p = '\0';
if (!(items[i].stext = strdup(buf))) if (!(items[i].stext = strdup(buf)))
die("cannot strdup %zu bytes:", strlen(buf) + 1); die("cannot strdup %zu bytes:", strlen(line) + 1);
#endif // TSV_PATCH #endif // TSV_PATCH
#if MULTI_SELECTION_PATCH #if MULTI_SELECTION_PATCH
items[i].id = i; /* for multiselect */ items[i].id = i; /* for multiselect */