diff options
author | Brian Ashworth <bosrsf04@gmail.com> | 2018-05-30 22:23:11 -0400 |
---|---|---|
committer | Brian Ashworth <bosrsf04@gmail.com> | 2018-06-02 08:07:44 -0400 |
commit | 8bfa2def8876bb507aa0174e6b159a0a226889b4 (patch) | |
tree | cb913921a89cf5436a6f0912fcdf680dde973216 /common | |
parent | 51bb9d8573efce7c46703d070ad963ba7d8dee76 (diff) | |
download | sway-8bfa2def8876bb507aa0174e6b159a0a226889b4.tar.xz |
Address first round of review for generic blocks
Diffstat (limited to 'common')
-rw-r--r-- | common/readline.c | 19 |
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 @@ +#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; |