summaryrefslogtreecommitdiff
path: root/sway/readline.c
diff options
context:
space:
mode:
authorDrew DeVault <[email protected]>2015-08-04 21:30:40 -0400
committerDrew DeVault <[email protected]>2015-08-04 21:30:40 -0400
commit542ef0c77700e67a95fcd08b5f305d1ab42046e1 (patch)
tree17333ebc1a65a28b9538de2da9264a45fc0cf9a9 /sway/readline.c
parent6a33e1e3cddac31b762e4376e29c03ccf8f92107 (diff)
Pull in some scas code and read i3 config file
Diffstat (limited to 'sway/readline.c')
-rw-r--r--sway/readline.c36
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 @@
+#include "readline.h"
+#include <stdlib.h>
+#include <stdio.h>
+
+char *read_line(FILE *file) {
+ int i = 0, 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 (i == size) {
+ string = realloc(string, length *= 2);
+ if (!string) {
+ return NULL;
+ }
+ }
+ string[i++] = (char)c;
+ length++;
+ }
+ if (i + 1 != size) {
+ string = realloc(string, length + 1);
+ if (!string) {
+ return NULL;
+ }
+ }
+ string[i] = '\0';
+ return string;
+}