--- fptools.c.orig Sun Apr 30 03:58:44 2006 +++ fptools.c Fri May 12 15:27:11 2006 @@ -450,11 +450,27 @@ if (!fgets (buf, n, stream)) return NULL; assert (*buf); - char * pch = buf + strlen(buf) - 1; - if (*pch != '\n') // eof -- ensure we end with a LF - memcpy (pch, "\n", 2); - else if (*--pch == '\r') // got CRLF -- strip the CR - memcpy (pch, "\n", 2); + int len = strlen(buf); + if(len == n - 1) { // if a line break is coming up, read it + int c; + if (!feof (stream)) { + if ((c = fgetc (stream)) == '\015' && !feof (stream)) { + if ((c = fgetc (stream)) != '\012' && !feof (stream)) { + ungetc (c, stream); + } + } + else if (c != '\012' && !feof (stream)) { + ungetc (c, stream); + } + } + } + else { + char * pch = buf + len - 1; + if (*pch != '\n') // eof -- ensure we end with a LF + memcpy (pch, "\n", 2); + else if (*--pch == '\r') // got CRLF -- strip the CR + memcpy (pch, "\n", 2); + } return buf; }