diff --git a/tools/compat/getline.c b/tools/compat/getline.c index e214c779bbdf..1861533dfe39 100644 --- a/tools/compat/getline.c +++ b/tools/compat/getline.c @@ -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) {