diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | read.c | 22 |
2 files changed, 20 insertions, 4 deletions
@@ -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: @@ -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); |