diff options
-rw-r--r-- | dmenu.c | 21 |
1 files changed, 14 insertions, 7 deletions
@@ -622,24 +622,31 @@ match(char *pattern) { | |||
622 | void | 622 | void |
623 | readstdin(void) { | 623 | readstdin(void) { |
624 | char *p, buf[1024]; | 624 | char *p, buf[1024]; |
625 | unsigned int len = 0, max = 0; | 625 | unsigned int len = 0, blen = 0, max = 0; |
626 | Item *i, *new; | 626 | Item *i, *new; |
627 | 627 | ||
628 | i = NULL; | 628 | i = 0, p = NULL; |
629 | while(fgets(buf, sizeof buf, stdin)) { | 629 | while(fgets(buf, sizeof buf, stdin)) { |
630 | len = strlen(buf); | 630 | len += (blen = strlen(buf)); |
631 | if(buf[len-1] == '\n') | 631 | if(!(p = realloc(p, len))) { |
632 | buf[--len] = '\0'; | 632 | eprint("fatal: could not realloc() %u bytes\n", len); |
633 | if(!(p = strdup(buf))) | 633 | return; |
634 | eprint("fatal: could not strdup() %u bytes\n", len); | 634 | } |
635 | memcpy (p + len - blen, buf, blen); | ||
636 | if (p[len - 1] == '\n') | ||
637 | p[len - 1] = 0; | ||
638 | else if (!feof(stdin)) | ||
639 | continue; | ||
635 | if(max < len) { | 640 | if(max < len) { |
636 | maxname = p; | 641 | maxname = p; |
637 | max = len; | 642 | max = len; |
638 | } | 643 | } |
644 | len = 0; | ||
639 | if(!(new = (Item *)malloc(sizeof(Item)))) | 645 | if(!(new = (Item *)malloc(sizeof(Item)))) |
640 | eprint("fatal: could not malloc() %u bytes\n", sizeof(Item)); | 646 | eprint("fatal: could not malloc() %u bytes\n", sizeof(Item)); |
641 | new->next = new->left = new->right = NULL; | 647 | new->next = new->left = new->right = NULL; |
642 | new->text = p; | 648 | new->text = p; |
649 | p = NULL; | ||
643 | if(!i) | 650 | if(!i) |
644 | allitems = new; | 651 | allitems = new; |
645 | else | 652 | else |