ftpget.c 2.2 KB

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