ftpsget.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, 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 https://curl.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. /* <DESC>
  25. * Get a single file from an FTPS server.
  26. * </DESC>
  27. */
  28. struct FtpFile {
  29. const char *filename;
  30. FILE *stream;
  31. };
  32. static size_t my_fwrite(void *buffer, size_t size, size_t nmemb,
  33. void *stream)
  34. {
  35. struct FtpFile *out = (struct FtpFile *)stream;
  36. if(!out->stream) {
  37. /* open file for writing */
  38. out->stream = fopen(out->filename, "wb");
  39. if(!out->stream)
  40. return -1; /* failure, cannot open file to write */
  41. }
  42. return fwrite(buffer, size, nmemb, out->stream);
  43. }
  44. int main(void)
  45. {
  46. CURL *curl;
  47. CURLcode res;
  48. struct FtpFile ftpfile = {
  49. "yourfile.bin", /* name to store the file as if successful */
  50. NULL
  51. };
  52. curl_global_init(CURL_GLOBAL_DEFAULT);
  53. curl = curl_easy_init();
  54. if(curl) {
  55. /*
  56. * You better replace the URL with one that works! Note that we use an
  57. * FTP:// URL with standard explicit FTPS. You can also do FTPS:// URLs if
  58. * you want to do the rarer kind of transfers: implicit.
  59. */
  60. curl_easy_setopt(curl, CURLOPT_URL,
  61. "ftp://user@server/home/user/file.txt");
  62. /* Define our callback to get called when there's data to be written */
  63. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
  64. /* Set a pointer to our struct to pass to the callback */
  65. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
  66. /* We activate SSL and we require it for both control and data */
  67. curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
  68. /* Switch on full protocol/debug output */
  69. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  70. res = curl_easy_perform(curl);
  71. /* always cleanup */
  72. curl_easy_cleanup(curl);
  73. if(CURLE_OK != res) {
  74. /* we failed */
  75. fprintf(stderr, "curl told us %d\n", res);
  76. }
  77. }
  78. if(ftpfile.stream)
  79. fclose(ftpfile.stream); /* close the local file */
  80. curl_global_cleanup();
  81. return 0;
  82. }