getlines.c (606B)
1 /* See LICENSE file for copyright and license details. */ 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 6 #include "../text.h" 7 #include "../util.h" 8 9 void 10 getlines(FILE *fp, struct linebuf *b) 11 { 12 char *line = NULL, **nline; 13 size_t size = 0, linelen; 14 ssize_t len; 15 16 while ((len = getline(&line, &size, fp)) != -1) { 17 if (++b->nlines > b->capacity) { 18 b->capacity += 512; 19 nline = erealloc(b->lines, b->capacity * sizeof(*b->lines)); 20 b->lines = nline; 21 } 22 linelen = len + 1; 23 b->lines[b->nlines-1] = emalloc(linelen); 24 memcpy(b->lines[b->nlines-1], line, linelen); 25 } 26 free(line); 27 }