mirror of
https://github.com/curl/curl.git
synced 2026-08-24 13:43:32 +03:00
curl_get_line: allow last line without newline char
improve backwards compatibility Test 3200 verifies Closes #9973
This commit is contained in:
parent
73d6f41489
commit
73c4f9696a
5 changed files with 220 additions and 5 deletions
|
|
@ -41,17 +41,41 @@ char *Curl_get_line(char *buf, int len, FILE *input)
|
|||
bool partial = FALSE;
|
||||
while(1) {
|
||||
char *b = fgets(buf, len, input);
|
||||
|
||||
if(b) {
|
||||
size_t rlen = strlen(b);
|
||||
if(rlen && (b[rlen-1] == '\n')) {
|
||||
|
||||
if(!rlen)
|
||||
break;
|
||||
|
||||
if(b[rlen-1] == '\n') {
|
||||
/* b is \n terminated */
|
||||
if(partial) {
|
||||
partial = FALSE;
|
||||
continue;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
/* read a partial, discard the next piece that ends with newline */
|
||||
partial = TRUE;
|
||||
else if(feof(input)) {
|
||||
if(partial)
|
||||
/* Line is already too large to return, ignore rest */
|
||||
break;
|
||||
|
||||
if(rlen + 1 < (size_t) len) {
|
||||
/* b is EOF terminated, insert missing \n */
|
||||
b[rlen] = '\n';
|
||||
b[rlen + 1] = '\0';
|
||||
return b;
|
||||
}
|
||||
else
|
||||
/* Maximum buffersize reached + EOF
|
||||
* This line is impossible to add a \n to so we'll ignore it
|
||||
*/
|
||||
break;
|
||||
}
|
||||
else
|
||||
/* Maximum buffersize reached */
|
||||
partial = TRUE;
|
||||
}
|
||||
else
|
||||
break;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue