diff options
author | Drew DeVault <sir@cmpwn.com> | 2018-06-02 08:09:18 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-02 08:09:18 -0700 |
commit | b0f2fd94791708a84aac2eccc57855a4ce8b6ec0 (patch) | |
tree | 919f22247b71c5562b3c1caff4f0bfa9dfcf0528 /common/readline.c | |
parent | 2d480e754e8287ba747faf1b21d8ecb927d565a1 (diff) | |
parent | 85a5c8dabd2561dfe616bea50faf2549e8cb345e (diff) |
Merge pull request #2070 from RedSoxFan/generic-config-blocks
Make command block implementation generic
Diffstat (limited to 'common/readline.c')
-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 @@ +#define _POSIX_C_SOURCE 200809L #include "readline.h" #include "log.h" #include <stdlib.h> @@ -48,6 +49,28 @@ char *read_line(FILE *file) { return string; } +char *peek_line(FILE *file, int line_offset, long *position) { + long pos = ftell(file); + size_t length = 0; + char *line = NULL; + for (int i = 0; i <= line_offset; i++) { + ssize_t read = getline(&line, &length, file); + if (read < 0) { + free(line); + line = NULL; + break; + } + if (read > 0 && line[read - 1] == '\n') { + line[read - 1] = '\0'; + } + } + if (position) { + *position = ftell(file); + } + fseek(file, pos, SEEK_SET); + return line; +} + char *read_line_buffer(FILE *file, char *string, size_t string_len) { size_t length = 0; if (!string) { |