ftpgetresp.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * $Id$
  9. */
  10. #include <stdio.h>
  11. #include <curl/curl.h>
  12. #include <curl/types.h>
  13. #include <curl/easy.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. static 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("ftp-list", "wb"); /* b is binary, needed on win32 */
  34. /* local file name to store the FTP server's response lines in */
  35. respfile = fopen("ftp-responses", "wb"); /* b is binary, needed on win32 */
  36. curl = curl_easy_init();
  37. if(curl) {
  38. /* Get a file listing from sunet */
  39. curl_easy_setopt(curl, CURLOPT_URL, "ftp://ftp.sunet.se/");
  40. curl_easy_setopt(curl, CURLOPT_WRITEDATA, ftpfile);
  41. /* If you intend to use this on windows with a libcurl DLL, you must use
  42. CURLOPT_WRITEFUNCTION as well */
  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. }