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/readline.c | |
parent | Support braces on next line for config blocks (diff) | |
download | sway-8bfa2def8876bb507aa0174e6b159a0a226889b4.tar.gz sway-8bfa2def8876bb507aa0174e6b159a0a226889b4.tar.zst sway-8bfa2def8876bb507aa0174e6b159a0a226889b4.zip |
Address first round of review for generic blocks
Diffstat (limited to 'common/readline.c')
-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 @@ | |||
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 | ||
51 | char *peek_line(FILE *file, int offset) { | 52 | char *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; |