2
0

ftpgetresp.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 <stdio.h>
  25. #include <curl/curl.h>
  26. /* <DESC>
  27. * Similar to ftpget.c but also stores the received response-lines
  28. * in a separate file using our own callback!
  29. * </DESC>
  30. */
  31. static size_t
  32. write_response(void *ptr, size_t size, size_t nmemb, void *data)
  33. {
  34. FILE *writehere = (FILE *)data;
  35. return fwrite(ptr, size, nmemb, writehere);
  36. }
  37. #define FTPBODY "ftp-list"
  38. #define FTPHEADERS "ftp-responses"
  39. int main(void)
  40. {
  41. CURL *curl;
  42. CURLcode res;
  43. FILE *ftpfile;
  44. FILE *respfile;
  45. /* local filename to store the file as */
  46. ftpfile = fopen(FTPBODY, "wb"); /* b is binary, needed on win32 */
  47. /* local filename to store the FTP server's response lines in */
  48. respfile = fopen(FTPHEADERS, "wb"); /* b is binary, needed on win32 */
  49. curl = curl_easy_init();
  50. if(curl) {
  51. /* Get a file listing from sunet */
  52. curl_easy_setopt(curl, CURLOPT_URL, "ftp://ftp.example.com/");
  53. curl_easy_setopt(curl, CURLOPT_WRITEDATA, ftpfile);
  54. /* If you intend to use this on windows with a libcurl DLL, you must use
  55. CURLOPT_WRITEFUNCTION as well */
  56. curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, write_response);
  57. curl_easy_setopt(curl, CURLOPT_HEADERDATA, respfile);
  58. res = curl_easy_perform(curl);
  59. /* Check for errors */
  60. if(res != CURLE_OK)
  61. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  62. curl_easy_strerror(res));
  63. /* always cleanup */
  64. curl_easy_cleanup(curl);
  65. }
  66. fclose(ftpfile); /* close the local file */
  67. fclose(respfile); /* close the response file */
  68. return 0;
  69. }