• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at https://curl.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 
25 #include "curl_setup.h"
26 
27 #if !defined(CURL_DISABLE_COOKIES) || !defined(CURL_DISABLE_ALTSVC) ||  \
28   !defined(CURL_DISABLE_HSTS) || !defined(CURL_DISABLE_NETRC)
29 
30 #include "curl_get_line.h"
31 #ifdef BUILDING_LIBCURL
32 #include "curl_memory.h"
33 #endif
34 /* The last #include file should be: */
35 #include "memdebug.h"
36 
37 /*
38  * Curl_get_line() makes sure to only return complete whole lines that end
39  * newlines.
40  */
Curl_get_line(struct dynbuf * buf,FILE * input)41 int Curl_get_line(struct dynbuf *buf, FILE *input)
42 {
43   CURLcode result;
44   char buffer[128];
45   Curl_dyn_reset(buf);
46   while(1) {
47     char *b = fgets(buffer, sizeof(buffer), input);
48 
49     if(b) {
50       size_t rlen = strlen(b);
51 
52       if(!rlen)
53         break;
54 
55       result = Curl_dyn_addn(buf, b, rlen);
56       if(result)
57         /* too long line or out of memory */
58         return 0; /* error */
59 
60       else if(b[rlen-1] == '\n')
61         /* end of the line */
62         return 1; /* all good */
63 
64       else if(feof(input)) {
65         /* append a newline */
66         result = Curl_dyn_addn(buf, "\n", 1);
67         if(result)
68           /* too long line or out of memory */
69           return 0; /* error */
70         return 1; /* all good */
71       }
72     }
73     else
74       break;
75   }
76   return 0;
77 }
78 
79 #endif /* if not disabled */
80