2
0

ftpsget.c 2.9 KB

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