aboutsummaryrefslogtreecommitdiff
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
parentd28aac66dca51fdd6d1683e90d9285c97ffa1e70 (diff)
getline written to comply to c99
-rw-r--r--Makefile2
-rw-r--r--read.c22
2 files changed, 20 insertions, 4 deletions
diff --git a/Makefile b/Makefile
index d3b1b6e..ee441d7 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
.POSIX:
OBJS = strbst.o ll.o nodelink.o read.o main.o
CFLAGS = -Wall -pedantic
-CC = gcc
+CC = c99
tmplt: $(OBJS)
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) -o $@
clean:
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);