ftpgetresp.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * $Id$
  9. */
  10. #include "curl/curl.h"
  11. #include "curl/types.h"
  12. #include "curl/easy.h"
  13. #include "testconfig.h"
  14. /*
  15. * Similar to ftpget.c but this also stores the received response-lines
  16. * in a separate file using our own callback!
  17. *
  18. * This functionality was introduced in libcurl 7.9.3.
  19. */
  20. size_t
  21. write_response(void *ptr, size_t size, size_t nmemb, void *data)
  22. {
  23. FILE *writehere = (FILE *)data;
  24. return fwrite(ptr, size, nmemb, writehere);
  25. }
  26. int main(int argc, char **argv)
  27. {
  28. CURL *curl;
  29. CURLcode res;
  30. FILE *ftpfile;
  31. FILE *respfile;
  32. (void)argc; (void)argv;
  33. /* local file name to store the file as */
  34. ftpfile = fopen(LIBCURL_BINARY_DIR "/Testing/ftpgetresp-list.txt", "wb"); /* b is binary, needed on win32 */
  35. /* local file name to store the FTP server's response lines in */
  36. respfile = fopen(LIBCURL_BINARY_DIR "/Testing/ftpgetresp-responses.txt", "wb"); /* b is binary, needed on win32 */
  37. curl_global_init(CURL_GLOBAL_DEFAULT);
  38. curl = curl_easy_init();
  39. if(curl) {
  40. /* Get a file listing from sunet */
  41. curl_easy_setopt(curl, CURLOPT_URL, "ftp://public.kitware.com/");
  42. curl_easy_setopt(curl, CURLOPT_FILE, ftpfile);
  43. curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, write_response);
  44. curl_easy_setopt(curl, CURLOPT_WRITEHEADER, respfile);
  45. res = curl_easy_perform(curl);
  46. /* always cleanup */
  47. curl_easy_cleanup(curl);
  48. }
  49. fclose(ftpfile); /* close the local file */
  50. fclose(respfile); /* close the response file */
  51. return 0;
  52. }