Fix problem reading the last line when it did not contain a delimiter

(Hubert Garavel)
This commit is contained in:
christos 2014-09-16 17:23:50 +00:00
parent 9dcf9072c5
commit 17d648a1fc
1 changed files with 9 additions and 5 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: getline.c,v 1.1 2011/03/20 20:48:57 christos Exp $ */
/* $NetBSD: getline.c,v 1.2 2014/09/16 17:23:50 christos Exp $ */
/*-
* Copyright (c) 2011 The NetBSD Foundation, Inc.
@ -59,10 +59,14 @@ getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp)
for (ptr = *buf, eptr = *buf + *bufsiz;;) {
int c = fgetc(fp);
if (c == -1) {
if (feof(fp))
return ptr == *buf ? -1 : ptr - *buf;
else
return -1;
if (feof(fp)) {
ssize_t diff = (ssize_t)(ptr - *buf);
if (diff != 0) {
*ptr = '\0';
return diff;
}
}
return -1;
}
*ptr++ = c;
if (c == delimiter) {