ftp-wildcard.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. /* <DESC>
  25. * FTP wildcard pattern matching
  26. * </DESC>
  27. */
  28. #include <curl/curl.h>
  29. #include <stdio.h>
  30. struct callback_data {
  31. FILE *output;
  32. };
  33. static long file_is_coming(struct curl_fileinfo *finfo,
  34. struct callback_data *data,
  35. int remains);
  36. static long file_is_downloaded(struct callback_data *data);
  37. static size_t write_it(char *buff, size_t size, size_t nmemb,
  38. void *cb_data);
  39. int main(int argc, char **argv)
  40. {
  41. /* curl easy handle */
  42. CURL *handle;
  43. /* help data */
  44. struct callback_data data = { 0 };
  45. /* global initialization */
  46. CURLcode rc = curl_global_init(CURL_GLOBAL_ALL);
  47. if(rc)
  48. return (int)rc;
  49. /* initialization of easy handle */
  50. handle = curl_easy_init();
  51. if(!handle) {
  52. curl_global_cleanup();
  53. return CURLE_OUT_OF_MEMORY;
  54. }
  55. /* turn on wildcard matching */
  56. curl_easy_setopt(handle, CURLOPT_WILDCARDMATCH, 1L);
  57. /* callback is called before download of concrete file started */
  58. curl_easy_setopt(handle, CURLOPT_CHUNK_BGN_FUNCTION, file_is_coming);
  59. /* callback is called after data from the file have been transferred */
  60. curl_easy_setopt(handle, CURLOPT_CHUNK_END_FUNCTION, file_is_downloaded);
  61. /* this callback writes contents into files */
  62. curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_it);
  63. /* put transfer data into callbacks */
  64. curl_easy_setopt(handle, CURLOPT_CHUNK_DATA, &data);
  65. curl_easy_setopt(handle, CURLOPT_WRITEDATA, &data);
  66. /* curl_easy_setopt(handle, CURLOPT_VERBOSE, 1L); */
  67. /* set a URL containing wildcard pattern (only in the last part) */
  68. if(argc == 2)
  69. curl_easy_setopt(handle, CURLOPT_URL, argv[1]);
  70. else
  71. curl_easy_setopt(handle, CURLOPT_URL, "ftp://example.com/test/*");
  72. /* and start transfer! */
  73. rc = curl_easy_perform(handle);
  74. curl_easy_cleanup(handle);
  75. curl_global_cleanup();
  76. return (int)rc;
  77. }
  78. static long file_is_coming(struct curl_fileinfo *finfo,
  79. struct callback_data *data,
  80. int remains)
  81. {
  82. printf("%3d %40s %10luB ", remains, finfo->filename,
  83. (unsigned long)finfo->size);
  84. switch(finfo->filetype) {
  85. case CURLFILETYPE_DIRECTORY:
  86. printf(" DIR\n");
  87. break;
  88. case CURLFILETYPE_FILE:
  89. printf("FILE ");
  90. break;
  91. default:
  92. printf("OTHER\n");
  93. break;
  94. }
  95. if(finfo->filetype == CURLFILETYPE_FILE) {
  96. /* do not transfer files >= 50B */
  97. if(finfo->size > 50) {
  98. printf("SKIPPED\n");
  99. return CURL_CHUNK_BGN_FUNC_SKIP;
  100. }
  101. data->output = fopen(finfo->filename, "wb");
  102. if(!data->output) {
  103. return CURL_CHUNK_BGN_FUNC_FAIL;
  104. }
  105. }
  106. return CURL_CHUNK_BGN_FUNC_OK;
  107. }
  108. static long file_is_downloaded(struct callback_data *data)
  109. {
  110. if(data->output) {
  111. printf("DOWNLOADED\n");
  112. fclose(data->output);
  113. data->output = 0x0;
  114. }
  115. return CURL_CHUNK_END_FUNC_OK;
  116. }
  117. static size_t write_it(char *buff, size_t size, size_t nmemb,
  118. void *cb_data)
  119. {
  120. struct callback_data *data = cb_data;
  121. size_t written = 0;
  122. if(data->output)
  123. written = fwrite(buff, size, nmemb, data->output);
  124. else
  125. /* listing output */
  126. written = fwrite(buff, size, nmemb, stdout);
  127. return written;
  128. }