ftpget.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 "setup.h"
  14. #include "testconfig.h"
  15. /*
  16. * This is an example showing how to get a single file from an FTP server.
  17. * It delays the actual destination file creation until the first write
  18. * callback so that it won't create an empty file in case the remote file
  19. * doesn't exist or something else fails.
  20. */
  21. struct FtpFile {
  22. char *filename;
  23. FILE *stream;
  24. };
  25. int my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
  26. {
  27. struct FtpFile *out=(struct FtpFile *)stream;
  28. if(out && !out->stream) {
  29. /* open file for writing */
  30. out->stream=fopen(out->filename, "wb");
  31. if(!out->stream)
  32. return -1; /* failure, can't open file to write */
  33. }
  34. return fwrite(buffer, size, nmemb, out->stream);
  35. }
  36. int main(void)
  37. {
  38. CURL *curl;
  39. CURLcode res;
  40. struct FtpFile ftpfile={
  41. LIBCURL_BINARY_DIR "/Testing/ftpget-download.txt", /* name to store the file as if succesful */
  42. NULL
  43. };
  44. curl_global_init(CURL_GLOBAL_DEFAULT);
  45. curl = curl_easy_init();
  46. if(curl) {
  47. /* Get curl 7.9.2 from sunet.se's FTP site: */
  48. curl_easy_setopt(curl, CURLOPT_URL,
  49. "ftp://public.kitware.com/pub/cmake/cygwin/setup.hint");
  50. /* Define our callback to get called when there's data to be written */
  51. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
  52. /* Set a pointer to our struct to pass to the callback */
  53. curl_easy_setopt(curl, CURLOPT_FILE, &ftpfile);
  54. /* Switch on full protocol/debug output */
  55. curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE);
  56. res = curl_easy_perform(curl);
  57. /* always cleanup */
  58. curl_easy_cleanup(curl);
  59. if(CURLE_OK != res) {
  60. /* we failed */
  61. fprintf(stderr, "curl told us %d\n", res);
  62. }
  63. }
  64. if(ftpfile.stream)
  65. fclose(ftpfile.stream); /* close the local file */
  66. curl_global_cleanup();
  67. return 0;
  68. }