2
0

curl_get_line.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #if !defined(CURL_DISABLE_COOKIES) || !defined(CURL_DISABLE_ALTSVC) || \
  26. !defined(CURL_DISABLE_HSTS) || !defined(CURL_DISABLE_NETRC)
  27. #include "curl_get_line.h"
  28. #include "curl_memory.h"
  29. /* The last #include file should be: */
  30. #include "memdebug.h"
  31. /*
  32. * Curl_get_line() makes sure to only return complete whole lines that end
  33. * newlines.
  34. */
  35. int Curl_get_line(struct dynbuf *buf, FILE *input)
  36. {
  37. CURLcode result;
  38. char buffer[128];
  39. Curl_dyn_reset(buf);
  40. while(1) {
  41. char *b = fgets(buffer, sizeof(buffer), input);
  42. if(b) {
  43. size_t rlen = strlen(b);
  44. if(!rlen)
  45. break;
  46. result = Curl_dyn_addn(buf, b, rlen);
  47. if(result)
  48. /* too long line or out of memory */
  49. return 0; /* error */
  50. else if(b[rlen-1] == '\n')
  51. /* end of the line */
  52. return 1; /* all good */
  53. else if(feof(input)) {
  54. /* append a newline */
  55. result = Curl_dyn_addn(buf, "\n", 1);
  56. if(result)
  57. /* too long line or out of memory */
  58. return 0; /* error */
  59. return 1; /* all good */
  60. }
  61. }
  62. else
  63. break;
  64. }
  65. return 0;
  66. }
  67. #endif /* if not disabled */