From b780db3979f269549cd0c05d8157b5dd222b58fa Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Wed, 29 Sep 2021 21:11:17 -0400 Subject: No need to search for newline We know it will always be the last character in line, and we know the length of line from the return value. --- src/todo.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/todo.c b/src/todo.c index 8d6334f..bb38b62 100644 --- a/src/todo.c +++ b/src/todo.c @@ -75,7 +75,8 @@ void todo_list(void) { static const char *regular_text = "\e[0m"; char *line = NULL; - size_t line_len = 0; + size_t line_buff_size = 0; + ssize_t line_len = 0; errno = 0; todo_file = fopen("TODO", "r"); @@ -89,9 +90,10 @@ void todo_list(void) { } } - for (size_t i = 0; getline(&line, &line_len, todo_file) >= 0; i++) { + for (size_t i = 0; + (line_len = getline(&line, &line_buff_size, todo_file)) >= 0; i++) { // Remove trailing newline by setting it to NULL char. - line[strcspn(line, "\n")] = '\0'; + line[line_len - 1] = '\0'; if (line_len > 5) { // Print the index in bold @@ -104,9 +106,8 @@ void todo_list(void) { puts_strikethrough(line + 4); } } - - free(line); } + free(line); fclose(todo_file); } @@ -171,14 +172,9 @@ void todo_done(int argc, char **argv) { apply_func_to_line(done, done_index); } -void todo_raw(int argc, char **argv) { - /*open_todo_file();*/ - puts("raw"); - fclose(todo_file); -} +void todo_raw(int argc, char **argv) { fclose(todo_file); } void todo_sort(void) { - /*open_todo_file();*/ puts("sort"); fclose(todo_file); } -- cgit v1.2.3