ftp-wildcard.c 4.1 KB

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