diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/readline.c | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/common/readline.c b/common/readline.c index ed5801de..1c396a90 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,6 +49,28 @@ char *read_line(FILE *file) { | |||
48 | return string; | 49 | return string; |
49 | } | 50 | } |
50 | 51 | ||
52 | char *peek_line(FILE *file, int line_offset, long *position) { | ||
53 | long pos = ftell(file); | ||
54 | size_t length = 0; | ||
55 | char *line = NULL; | ||
56 | for (int i = 0; i <= line_offset; i++) { | ||
57 | ssize_t read = getline(&line, &length, file); | ||
58 | if (read < 0) { | ||
59 | free(line); | ||
60 | line = NULL; | ||
61 | break; | ||
62 | } | ||
63 | if (read > 0 && line[read - 1] == '\n') { | ||
64 | line[read - 1] = '\0'; | ||
65 | } | ||
66 | } | ||
67 | if (position) { | ||
68 | *position = ftell(file); | ||
69 | } | ||
70 | fseek(file, pos, SEEK_SET); | ||
71 | return line; | ||
72 | } | ||
73 | |||
51 | char *read_line_buffer(FILE *file, char *string, size_t string_len) { | 74 | char *read_line_buffer(FILE *file, char *string, size_t string_len) { |
52 | size_t length = 0; | 75 | size_t length = 0; |
53 | if (!string) { | 76 | if (!string) { |