fzy
terminal fuzzy finder picker
git clone https://9o.is/git/fzy.git
commit d34c7a33b3a0d5bd5e9e53130bbce16c631a9c78 parent 1eb761585ee53188bc5cadaefcfdef567863ba40 Author: John Hawthorn <john.hawthorn@gmail.com> Date: Sun, 21 Sep 2014 15:23:23 -0700 Remove getline in favour of fgets/strdup getline is a little greedy with memory usage, using a little extra for every line read. strdup should always use the minimum amount. Diffstat:
| M | fzy.c | | | 17 | ++++++++--------- |
1 file changed, 8 insertions(+), 9 deletions(-)
diff --git a/fzy.c b/fzy.c @@ -19,20 +19,19 @@ size_t scrolloff = 1; const char *prompt = "> "; void read_choices(choices_t *c){ - char *line = NULL; - size_t len = 0; - ssize_t read; - - while ((read = getline(&line, &len, stdin)) != -1) { + const char *line; + char buf[4096]; + while(fgets(buf, sizeof buf, stdin)){ char *nl; - if((nl = strchr(line, '\n'))) + if((nl = strchr(buf, '\n'))) *nl = '\0'; + if(!(line = strdup(buf))){ + fprintf(stderr, "Cannot allocate memory"); + abort(); + } choices_add(c, line); - - line = NULL; } - free(line); } #define SEARCH_SIZE_MAX 4096