curl_get_line.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, 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. #ifdef BUILDING_LIBCURL
  29. #include "curl_memory.h"
  30. #endif
  31. /* The last #include file should be: */
  32. #include "memdebug.h"
  33. /*
  34. * Curl_get_line() makes sure to only return complete whole lines that end
  35. * newlines.
  36. */
  37. int Curl_get_line(struct dynbuf *buf, FILE *input)
  38. {
  39. CURLcode result;
  40. char buffer[128];
  41. Curl_dyn_reset(buf);
  42. while(1) {
  43. char *b = fgets(buffer, sizeof(buffer), input);
  44. if(b) {
  45. size_t rlen = strlen(b);
  46. if(!rlen)
  47. break;
  48. result = Curl_dyn_addn(buf, b, rlen);
  49. if(result)
  50. /* too long line or out of memory */
  51. return 0; /* error */
  52. else if(b[rlen-1] == '\n')
  53. /* end of the line */
  54. return 1; /* all good */
  55. else if(feof(input)) {
  56. /* append a newline */
  57. result = Curl_dyn_addn(buf, "\n", 1);
  58. if(result)
  59. /* too long line or out of memory */
  60. return 0; /* error */
  61. return 1; /* all good */
  62. }
  63. }
  64. else
  65. break;
  66. }
  67. return 0;
  68. }
  69. #endif /* if not disabled */