ftp-wildcard.c 4.2 KB

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