/src/curl/lib/curl_get_line.c
Line | Count | Source |
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 | | #include "curl_setup.h" |
25 | | |
26 | | #if !defined(CURL_DISABLE_COOKIES) || !defined(CURL_DISABLE_ALTSVC) || \ |
27 | | !defined(CURL_DISABLE_HSTS) || !defined(CURL_DISABLE_NETRC) |
28 | | |
29 | | #include "curl_get_line.h" |
30 | | |
31 | 0 | #define appendnl(b) curlx_dyn_addn(buf, "\n", 1) |
32 | | |
33 | | /* |
34 | | * Curl_get_line() returns only complete whole lines that end with newline. |
35 | | * When 'eof' is set TRUE, the last line has been read. |
36 | | */ |
37 | | CURLcode Curl_get_line(struct dynbuf *buf, FILE *input, bool *eof) |
38 | 0 | { |
39 | 0 | CURLcode result; |
40 | 0 | char buffer[128]; |
41 | 0 | curlx_dyn_reset(buf); |
42 | 0 | while(1) { |
43 | 0 | size_t rlen; |
44 | 0 | char *b = fgets(buffer, sizeof(buffer), input); |
45 | |
|
46 | 0 | *eof = feof(input); |
47 | |
|
48 | 0 | rlen = b ? strlen(b) : 0; |
49 | 0 | if(rlen) { |
50 | 0 | result = curlx_dyn_addn(buf, b, rlen); |
51 | 0 | if(result) |
52 | | /* too long line or out of memory */ |
53 | 0 | return result; |
54 | 0 | } |
55 | | /* now check the full line */ |
56 | 0 | rlen = curlx_dyn_len(buf); |
57 | 0 | b = curlx_dyn_ptr(buf); |
58 | 0 | if(rlen && (b[rlen - 1] == '\n')) |
59 | | /* LF at end of the line */ |
60 | 0 | return CURLE_OK; /* all good */ |
61 | 0 | if(*eof) |
62 | | /* append a newline */ |
63 | 0 | return appendnl(buf); |
64 | | /* otherwise get next line to append */ |
65 | 0 | } |
66 | | /* UNREACHABLE */ |
67 | 0 | } |
68 | | |
69 | | #endif /* if not disabled */ |