ftp-wildcard.c 4.1 KB

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