summaryrefslogtreecommitdiffstats
path: root/common/readline.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/readline.c')
-rw-r--r--common/readline.c19
1 files changed, 13 insertions, 6 deletions
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 @@
1#define _POSIX_C_SOURCE 200809L
1#include "readline.h" 2#include "readline.h"
2#include "log.h" 3#include "log.h"
3#include <stdlib.h> 4#include <stdlib.h>
@@ -48,15 +49,21 @@ char *read_line(FILE *file) {
48 return string; 49 return string;
49} 50}
50 51
51char *peek_line(FILE *file, int offset) { 52char *peek_line(FILE *file, int offset, long *position) {
52 int pos = ftell(file); 53 long pos = ftell(file);
53 char *line = NULL; 54 size_t length = 1;
55 char *line = calloc(1, length);
54 for (int i = 0; i <= offset; i++) { 56 for (int i = 0; i <= offset; i++) {
55 free(line); 57 ssize_t read = getline(&line, &length, file);
56 line = read_line(file); 58 if (read < 0) {
57 if (!line) {
58 break; 59 break;
59 } 60 }
61 if (line[read - 1] == '\n') {
62 line[read - 1] = '\0';
63 }
64 }
65 if (position) {
66 *position = ftell(file);
60 } 67 }
61 fseek(file, pos, SEEK_SET); 68 fseek(file, pos, SEEK_SET);
62 return line; 69 return line;