1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 * FTP wildcard pattern matching
26 #include <curl/curl.h>
29 struct callback_data {
33 static long file_is_coming(struct curl_fileinfo *finfo,
34 struct callback_data *data,
37 static long file_is_downloaded(struct callback_data *data);
39 static size_t write_it(char *buff, size_t size, size_t nmemb,
42 int main(int argc, char **argv)
46 /* curl easy handle */
50 struct callback_data data = { 0 };
52 /* global initialization */
53 rc = curl_global_init(CURL_GLOBAL_ALL);
57 /* initialization of easy handle */
58 handle = curl_easy_init();
60 curl_global_cleanup();
61 return CURLE_OUT_OF_MEMORY;
64 /* turn on wildcard matching */
65 curl_easy_setopt(handle, CURLOPT_WILDCARDMATCH, 1L);
67 /* callback is called before download of concrete file started */
68 curl_easy_setopt(handle, CURLOPT_CHUNK_BGN_FUNCTION, file_is_coming);
70 /* callback is called after data from the file have been transferred */
71 curl_easy_setopt(handle, CURLOPT_CHUNK_END_FUNCTION, file_is_downloaded);
73 /* this callback will write contents into files */
74 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_it);
76 /* put transfer data into callbacks */
77 curl_easy_setopt(handle, CURLOPT_CHUNK_DATA, &data);
78 curl_easy_setopt(handle, CURLOPT_WRITEDATA, &data);
80 /* curl_easy_setopt(handle, CURLOPT_VERBOSE, 1L); */
82 /* set an URL containing wildcard pattern (only in the last part) */
84 curl_easy_setopt(handle, CURLOPT_URL, argv[1]);
86 curl_easy_setopt(handle, CURLOPT_URL, "ftp://example.com/test/*");
88 /* and start transfer! */
89 rc = curl_easy_perform(handle);
91 curl_easy_cleanup(handle);
92 curl_global_cleanup();
96 static long file_is_coming(struct curl_fileinfo *finfo,
97 struct callback_data *data,
100 printf("%3d %40s %10luB ", remains, finfo->filename,
101 (unsigned long)finfo->size);
103 switch(finfo->filetype) {
104 case CURLFILETYPE_DIRECTORY:
107 case CURLFILETYPE_FILE:
115 if(finfo->filetype == CURLFILETYPE_FILE) {
116 /* do not transfer files >= 50B */
117 if(finfo->size > 50) {
119 return CURL_CHUNK_BGN_FUNC_SKIP;
122 data->output = fopen(finfo->filename, "w");
124 return CURL_CHUNK_BGN_FUNC_FAIL;
128 return CURL_CHUNK_BGN_FUNC_OK;
131 static long file_is_downloaded(struct callback_data *data)
134 printf("DOWNLOADED\n");
135 fclose(data->output);
138 return CURL_CHUNK_END_FUNC_OK;
141 static size_t write_it(char *buff, size_t size, size_t nmemb,
144 struct callback_data *data = cb_data;
147 written = fwrite(buff, size, nmemb, data->output);
150 written = fwrite(buff, size, nmemb, stdout);