diff options
author | Drew DeVault <[email protected]> | 2015-11-26 14:31:29 -0500 |
---|---|---|
committer | Drew DeVault <[email protected]> | 2015-11-26 14:31:29 -0500 |
commit | 9a15371ba3ab8497ac393d18baeb2f953a3b2762 (patch) | |
tree | f2f797205fd8816af277c3178204dc355cfd4a74 /common/readline.c | |
parent | d69cbeabc0d0f59847e0118f61ad7e4d76fc95e4 (diff) |
Parse command line args for swaymsg
Diffstat (limited to 'common/readline.c')
-rw-r--r-- | common/readline.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/common/readline.c b/common/readline.c new file mode 100644 index 00000000..e75b183f --- /dev/null +++ b/common/readline.c @@ -0,0 +1,39 @@ +#include "readline.h" +#include <stdlib.h> +#include <stdio.h> + +char *read_line(FILE *file) { + int length = 0, size = 128; + char *string = malloc(size); + if (!string) { + return NULL; + } + while (1) { + int c = getc(file); + if (c == EOF || c == '\n' || c == '\0') { + break; + } + if (c == '\r') { + continue; + } + if (length == size) { + char *new_string = realloc(string, size *= 2); + if (!new_string) { + free(string); + return NULL; + } + string = new_string; + } + string[length++] = c; + } + if (length + 1 == size) { + char *new_string = realloc(string, length + 1); + if (!new_string) { + free(string); + return NULL; + } + string = new_string; + } + string[length] = '\0'; + return string; +} |