ftpgetresp.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * $Id: ftpgetresp.c,v 1.2 2003/12/08 14:13:19 bagder Exp $
  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. 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. curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, write_response);
  42. curl_easy_setopt(curl, CURLOPT_WRITEHEADER, respfile);
  43. res = curl_easy_perform(curl);
  44. /* always cleanup */
  45. curl_easy_cleanup(curl);
  46. }
  47. fclose(ftpfile); /* close the local file */
  48. fclose(respfile); /* close the response file */
  49. return 0;
  50. }