ftpget.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include <stdio.h>
  23. #include <curl/curl.h>
  24. /*
  25. * This is an example showing how to get a single file from an FTP server.
  26. * It delays the actual destination file creation until the first write
  27. * callback so that it won't create an empty file in case the remote file
  28. * doesn't exist or something else fails.
  29. */
  30. struct FtpFile {
  31. const char *filename;
  32. FILE *stream;
  33. };
  34. static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
  35. {
  36. struct FtpFile *out=(struct FtpFile *)stream;
  37. if(out && !out->stream) {
  38. /* open file for writing */
  39. out->stream=fopen(out->filename, "wb");
  40. if(!out->stream)
  41. return -1; /* failure, can't open file to write */
  42. }
  43. return fwrite(buffer, size, nmemb, out->stream);
  44. }
  45. int main(void)
  46. {
  47. CURL *curl;
  48. CURLcode res;
  49. struct FtpFile ftpfile={
  50. "curl.tar.gz", /* name to store the file as if succesful */
  51. NULL
  52. };
  53. curl_global_init(CURL_GLOBAL_DEFAULT);
  54. curl = curl_easy_init();
  55. if(curl) {
  56. /*
  57. * You better replace the URL with one that works!
  58. */
  59. curl_easy_setopt(curl, CURLOPT_URL,
  60. "ftp://ftp.example.com/pub/www/utilities/curl/curl-7.9.2.tar.gz");
  61. /* Define our callback to get called when there's data to be written */
  62. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
  63. /* Set a pointer to our struct to pass to the callback */
  64. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
  65. /* Switch on full protocol/debug output */
  66. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  67. res = curl_easy_perform(curl);
  68. /* always cleanup */
  69. curl_easy_cleanup(curl);
  70. if(CURLE_OK != res) {
  71. /* we failed */
  72. fprintf(stderr, "curl told us %d\n", res);
  73. }
  74. }
  75. if(ftpfile.stream)
  76. fclose(ftpfile.stream); /* close the local file */
  77. curl_global_cleanup();
  78. return 0;
  79. }