From cb9185b1948110dcf4c6483db44163fbc5ea942f Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Wed, 27 May 2020 21:52:19 -0400 Subject: getline written to comply to c99 --- Makefile | 2 +- read.c | 22 +++++++++++++++++++--- 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); -- cgit