/***************************************************************************1* _ _ ____ _2* Project ___| | | | _ \| |3* / __| | | | |_) | |4* | (__| |_| | _ <| |___5* \___|\___/|_| \_\_____|6*7* Copyright (C) Daniel Stenberg, <[email protected]>, et al.8*9* This software is licensed as described in the file COPYING, which10* you should have received as part of this distribution. The terms11* are also available at https://curl.se/docs/copyright.html.12*13* You may opt to use, copy, modify, merge, publish, distribute and/or sell14* copies of the Software, and permit persons to whom the Software is15* furnished to do so, under the terms of the COPYING file.16*17* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY18* KIND, either express or implied.19*20* SPDX-License-Identifier: curl21*22***************************************************************************/2324#include "curl_setup.h"2526#if !defined(CURL_DISABLE_COOKIES) || !defined(CURL_DISABLE_ALTSVC) || \27!defined(CURL_DISABLE_HSTS) || !defined(CURL_DISABLE_NETRC)2829#include "curl_get_line.h"30#include "curl_memory.h"31/* The last #include file should be: */32#include "memdebug.h"3334static int appendnl(struct dynbuf *buf)35{36CURLcode result = curlx_dyn_addn(buf, "\n", 1);37if(result)38/* too long line or out of memory */39return 0; /* error */40return 1; /* all good */41}4243/*44* Curl_get_line() makes sure to only return complete whole lines that end45* newlines.46*/47int Curl_get_line(struct dynbuf *buf, FILE *input)48{49CURLcode result;50char buffer[128];51curlx_dyn_reset(buf);52while(1) {53char *b = fgets(buffer, sizeof(buffer), input);54size_t rlen;5556if(b) {57rlen = strlen(b);5859if(!rlen)60break;6162result = curlx_dyn_addn(buf, b, rlen);63if(result)64/* too long line or out of memory */65return 0; /* error */6667else if(b[rlen-1] == '\n')68/* end of the line */69return 1; /* all good */7071else if(feof(input))72/* append a newline */73return appendnl(buf);74}75else {76rlen = curlx_dyn_len(buf);77if(rlen) {78b = curlx_dyn_ptr(buf);7980if(b[rlen-1] != '\n')81/* append a newline */82return appendnl(buf);8384return 1; /* all good */85}86else87break;88}89}90return 0;91}9293#endif /* if not disabled */949596