diff --git a/libguile/ChangeLog b/libguile/ChangeLog index 9118375a4..db07f23c0 100644 --- a/libguile/ChangeLog +++ b/libguile/ChangeLog @@ -1,3 +1,10 @@ +1997-12-04 Tim Pierce + + * fports.c (scm_fgets): Return if the last char in a chunk is + newline. When fgets returns a string whose length is `size-1', it + is ambiguous whether a whole line was retrieved, so we must check + explicitly whether a line terminator is present. + 1997-12-04 Mikael Djurfeldt * print.h (SCM_COERCE_OUTPORT): Check that the object is a pair diff --git a/libguile/fports.c b/libguile/fports.c index ec0c73dc8..f0ef7a502 100644 --- a/libguile/fports.c +++ b/libguile/fports.c @@ -326,23 +326,26 @@ scm_fgets (port) } } - while (1) { - p = buf + i; - if (fgets (p, limit - i, f) == NULL) { - if (i) + while (1) + { + int chunk_size = limit - i; + + p = buf + i; + if (fgets (p, chunk_size, f) == NULL) { + if (i) + return buf; + free (buf); + return NULL; + } + + if (strlen(p) < chunk_size - 1 || buf[limit-2] == '\n') return buf; - free (buf); - return NULL; + + buf = (char *) realloc (buf, sizeof(char) * limit * 2); + + i = limit - 1; + limit *= 2; } - - if (strlen(p) < limit - i - 1) - return buf; - - buf = (char *) realloc (buf, sizeof(char) * limit * 2); - - i = limit - 1; - limit *= 2; - } } #ifdef vms