aboutsummaryrefslogtreecommitdiff
path: root/read.c
diff options
context:
space:
mode:
authorHolden Rohrer <hr@hrhr.dev>2020-05-27 21:52:19 -0400
committerHolden Rohrer <hr@hrhr.dev>2020-05-27 21:52:19 -0400
commitcb9185b1948110dcf4c6483db44163fbc5ea942f (patch)
tree469213483259d8082a1c6a2f407901fec8ac37c4 /read.c
parentd28aac66dca51fdd6d1683e90d9285c97ffa1e70 (diff)
getline written to comply to c99
Diffstat (limited to 'read.c')
-rw-r--r--read.c22
1 files changed, 19 insertions, 3 deletions
diff --git a/read.c b/read.c
index d64d1b5..069c819 100644
--- a/read.c
+++ b/read.c
@@ -31,10 +31,26 @@ char* empty(void) {
return str;
}
+char* getline(size_t* n, FILE* file) {
+ size_t sz = 100;
+ *n = 0;
+ int c;
+ char* out = malloc(sizeof(char)*100);
+ while ( (c = fgetc(file)) != EOF ) {
+ if (*n == sz) out = realloc(out, sizeof(char) * (sz *= 2));
+ out[*n] = c;
+ (*n)++;
+ if (c == '\n') break;
+ }
+ out[(*n)] = 0;
+ //(*n)++;
+ return realloc(out, sizeof(char) * (*n+1) );
+}
+
char* line(FILE* file) { // gets a line from file
- char* link = NULL;
- size_t n = 0;
- getline(&link, &n, file); // getline stores a line of size n in link
+ size_t n;
+ char* link = getline(&n, file);
+ // getline stores a line of size n in link
wsclean(link, n); // removes trailing whitespace
if (*link == '\n') { // empty line treated as ""
free(link);