diff options
Diffstat (limited to 'sway/readline.c')
-rw-r--r-- | sway/readline.c | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/sway/readline.c b/sway/readline.c new file mode 100644 index 00000000..be8c35cc --- /dev/null +++ b/sway/readline.c | |||
@@ -0,0 +1,36 @@ | |||
1 | #include "readline.h" | ||
2 | #include <stdlib.h> | ||
3 | #include <stdio.h> | ||
4 | |||
5 | char *read_line(FILE *file) { | ||
6 | int i = 0, length = 0, size = 128; | ||
7 | char *string = malloc(size); | ||
8 | if (!string) { | ||
9 | return NULL; | ||
10 | } | ||
11 | while (1) { | ||
12 | int c = getc(file); | ||
13 | if (c == EOF || c == '\n' || c == '\0') { | ||
14 | break; | ||
15 | } | ||
16 | if (c == '\r') { | ||
17 | continue; | ||
18 | } | ||
19 | if (i == size) { | ||
20 | string = realloc(string, length *= 2); | ||
21 | if (!string) { | ||
22 | return NULL; | ||
23 | } | ||
24 | } | ||
25 | string[i++] = (char)c; | ||
26 | length++; | ||
27 | } | ||
28 | if (i + 1 != size) { | ||
29 | string = realloc(string, length + 1); | ||
30 | if (!string) { | ||
31 | return NULL; | ||
32 | } | ||
33 | } | ||
34 | string[i] = '\0'; | ||
35 | return string; | ||
36 | } | ||