From 8bfa2def8876bb507aa0174e6b159a0a226889b4 Mon Sep 17 00:00:00 2001 From: Brian Ashworth <bosrsf04@gmail.com> Date: Wed, 30 May 2018 22:23:11 -0400 Subject: Address first round of review for generic blocks --- common/readline.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'common') diff --git a/common/readline.c b/common/readline.c index abe986c4..f637b64d 100644 --- a/common/readline.c +++ b/common/readline.c @@ -1,3 +1,4 @@ +#define _POSIX_C_SOURCE 200809L #include "readline.h" #include "log.h" #include <stdlib.h> @@ -48,15 +49,21 @@ char *read_line(FILE *file) { return string; } -char *peek_line(FILE *file, int offset) { - int pos = ftell(file); - char *line = NULL; +char *peek_line(FILE *file, int offset, long *position) { + long pos = ftell(file); + size_t length = 1; + char *line = calloc(1, length); for (int i = 0; i <= offset; i++) { - free(line); - line = read_line(file); - if (!line) { + ssize_t read = getline(&line, &length, file); + if (read < 0) { break; } + if (line[read - 1] == '\n') { + line[read - 1] = '\0'; + } + } + if (position) { + *position = ftell(file); } fseek(file, pos, SEEK_SET); return line; -- cgit v1.2.3