xref: /third_party/curl/lib/curl_get_line.c (revision 13498266)
113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci
2513498266Sopenharmony_ci#include "curl_setup.h"
2613498266Sopenharmony_ci
2713498266Sopenharmony_ci#if !defined(CURL_DISABLE_COOKIES) || !defined(CURL_DISABLE_ALTSVC) ||  \
2813498266Sopenharmony_ci  !defined(CURL_DISABLE_HSTS) || !defined(CURL_DISABLE_NETRC)
2913498266Sopenharmony_ci
3013498266Sopenharmony_ci#include "curl_get_line.h"
3113498266Sopenharmony_ci#include "curl_memory.h"
3213498266Sopenharmony_ci/* The last #include file should be: */
3313498266Sopenharmony_ci#include "memdebug.h"
3413498266Sopenharmony_ci
3513498266Sopenharmony_ci/*
3613498266Sopenharmony_ci * Curl_get_line() makes sure to only return complete whole lines that fit in
3713498266Sopenharmony_ci * 'len' bytes and end with a newline.
3813498266Sopenharmony_ci */
3913498266Sopenharmony_cichar *Curl_get_line(char *buf, int len, FILE *input)
4013498266Sopenharmony_ci{
4113498266Sopenharmony_ci  bool partial = FALSE;
4213498266Sopenharmony_ci  while(1) {
4313498266Sopenharmony_ci    char *b = fgets(buf, len, input);
4413498266Sopenharmony_ci
4513498266Sopenharmony_ci    if(b) {
4613498266Sopenharmony_ci      size_t rlen = strlen(b);
4713498266Sopenharmony_ci
4813498266Sopenharmony_ci      if(!rlen)
4913498266Sopenharmony_ci        break;
5013498266Sopenharmony_ci
5113498266Sopenharmony_ci      if(b[rlen-1] == '\n') {
5213498266Sopenharmony_ci        /* b is \n terminated */
5313498266Sopenharmony_ci        if(partial) {
5413498266Sopenharmony_ci          partial = FALSE;
5513498266Sopenharmony_ci          continue;
5613498266Sopenharmony_ci        }
5713498266Sopenharmony_ci        return b;
5813498266Sopenharmony_ci      }
5913498266Sopenharmony_ci      else if(feof(input)) {
6013498266Sopenharmony_ci        if(partial)
6113498266Sopenharmony_ci          /* Line is already too large to return, ignore rest */
6213498266Sopenharmony_ci          break;
6313498266Sopenharmony_ci
6413498266Sopenharmony_ci        if(rlen + 1 < (size_t) len) {
6513498266Sopenharmony_ci          /* b is EOF terminated, insert missing \n */
6613498266Sopenharmony_ci          b[rlen] = '\n';
6713498266Sopenharmony_ci          b[rlen + 1] = '\0';
6813498266Sopenharmony_ci          return b;
6913498266Sopenharmony_ci        }
7013498266Sopenharmony_ci        else
7113498266Sopenharmony_ci          /* Maximum buffersize reached + EOF
7213498266Sopenharmony_ci           * This line is impossible to add a \n to so we'll ignore it
7313498266Sopenharmony_ci           */
7413498266Sopenharmony_ci          break;
7513498266Sopenharmony_ci      }
7613498266Sopenharmony_ci      else
7713498266Sopenharmony_ci        /* Maximum buffersize reached */
7813498266Sopenharmony_ci        partial = TRUE;
7913498266Sopenharmony_ci    }
8013498266Sopenharmony_ci    else
8113498266Sopenharmony_ci      break;
8213498266Sopenharmony_ci  }
8313498266Sopenharmony_ci  return NULL;
8413498266Sopenharmony_ci}
8513498266Sopenharmony_ci
8613498266Sopenharmony_ci#endif /* if not disabled */
87