libutil/getlines: use known line length
also style: linelen = length of getline(), this was slightly confusing.
This commit is contained in:
parent
adf9f47525
commit
3f01706837
@ -18,13 +18,13 @@ getlines(FILE *fp, struct linebuf *b)
|
|||||||
b->capacity += 512;
|
b->capacity += 512;
|
||||||
b->lines = erealloc(b->lines, b->capacity * sizeof(*b->lines));
|
b->lines = erealloc(b->lines, b->capacity * sizeof(*b->lines));
|
||||||
}
|
}
|
||||||
linelen = len + 1;
|
linelen = len;
|
||||||
b->lines[b->nlines - 1] = memcpy(emalloc(linelen), line, linelen);
|
b->lines[b->nlines - 1] = memcpy(emalloc(linelen + 1), line, linelen + 1);
|
||||||
}
|
}
|
||||||
free(line);
|
free(line);
|
||||||
if (b->lines && b->nlines && !strchr(b->lines[b->nlines - 1], '\n')) {
|
if (b->lines && b->nlines && b->lines[b->nlines - 1][linelen - 1] != '\n') {
|
||||||
b->lines[b->nlines - 1] = erealloc(b->lines[b->nlines - 1], linelen + 1);
|
b->lines[b->nlines - 1] = erealloc(b->lines[b->nlines - 1], linelen + 2);
|
||||||
b->lines[b->nlines - 1][linelen - 1] = '\n';
|
b->lines[b->nlines - 1][linelen] = '\n';
|
||||||
b->lines[b->nlines - 1][linelen] = '\0';
|
b->lines[b->nlines - 1][linelen + 1] = '\0';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user