Replace homemade getline(3) by actual getline(3)

This commit is contained in:
Baptiste Daroussin 2019-12-17 11:03:32 +00:00
parent bc188bd4f8
commit 94678ee678

View File

@ -257,28 +257,16 @@ getchange(FILE *b)
static char * static char *
get_line(FILE *b, size_t *n) get_line(FILE *b, size_t *n)
{ {
char *cp; ssize_t len;
size_t len; static char *buf = NULL;
static char *buf; static size_t bufsize = 0;
static size_t bufsize;
if ((cp = fgetln(b, &len)) == NULL) if ((len = getline(&buf, &bufsize, b)) < 0)
return (NULL); return (NULL);
if (cp[len - 1] != '\n')
len++;
if (len + 1 > bufsize) {
do {
bufsize += 1024;
} while (len + 1 > bufsize);
if ((buf = realloc(buf, bufsize)) == NULL)
err(EXIT_FAILURE, NULL);
}
memcpy(buf, cp, len - 1);
buf[len - 1] = '\n';
buf[len] = '\0';
if (n != NULL) if (n != NULL)
*n = len; *n = len;
return (buf); return (buf);
} }