- copycount = min(len, str->buflen - str->mark);
- memccpy(buf, str->buffer + str->mark, '\n', copycount);
+ copycount = MIN(len - 1, str->buflen - str->mark);
+ char *endptr = memccpy(buf, str->buffer + str->mark, '\n', copycount);
+ if(endptr) /* newline was found */
+ copycount = endptr - buf; /* Real copy count */
+ buf[copycount] = '\0';
+ str->mark += copycount;