ftpgetresp.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. /* local file name to store the file as */
  33. ftpfile = fopen(LIBCURL_BINARY_DIR "/Testing/ftpgetresp-list.txt", "wb"); /* b is binary, needed on win32 */
  34. /* local file name to store the FTP server's response lines in */
  35. respfile = fopen(LIBCURL_BINARY_DIR "/Testing/ftpgetresp-responses.txt", "wb"); /* b is binary, needed on win32 */
  36. curl_global_init(CURL_GLOBAL_DEFAULT);
  37. curl = curl_easy_init();
  38. if(curl) {
  39. /* Get a file listing from sunet */
  40. curl_easy_setopt(curl, CURLOPT_URL, "ftp://public.kitware.com/");
  41. curl_easy_setopt(curl, CURLOPT_FILE, ftpfile);
  42. curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, write_response);
  43. curl_easy_setopt(curl, CURLOPT_WRITEHEADER, respfile);
  44. res = curl_easy_perform(curl);
  45. /* always cleanup */
  46. curl_easy_cleanup(curl);
  47. }
  48. fclose(ftpfile); /* close the local file */
  49. fclose(respfile); /* close the response file */
  50. return 0;
  51. }