Browse code

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.

John Hawthorn authored on 21/09/2014 22:23:23
Showing 1 changed files

  • fzy.c index bd40708..6eee30f 100644
... ...
@@ -19,20 +19,19 @@ size_t scrolloff = 1;
19 19
 const char *prompt = "> ";
20 20
 
21 21
 void read_choices(choices_t *c){
22
-	char *line = NULL;
23
-	size_t len = 0;
24
-	ssize_t read;
25
-
26
-	while ((read = getline(&line, &len, stdin)) != -1) {
22
+	const char *line;
23
+	char buf[4096];
24
+	while(fgets(buf, sizeof buf, stdin)){
27 25
 		char *nl;
28
-		if((nl = strchr(line, '\n')))
26
+		if((nl = strchr(buf, '\n')))
29 27
 			*nl = '\0';
30 28
 
29
+		if(!(line = strdup(buf))){
30
+			fprintf(stderr, "Cannot allocate memory");
31
+			abort();
32
+		}
31 33
 		choices_add(c, line);
32
-
33
-		line = NULL;
34 34
 	}
35
-	free(line);
36 35
 }
37 36
 
38 37
 #define SEARCH_SIZE_MAX 4096