1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
10 #include <curl/curl.h>
13 struct callback_data {
17 static long file_is_comming(struct curl_fileinfo *finfo,
18 struct callback_data *data,
21 static long file_is_downloaded(struct callback_data *data);
23 static size_t write_it(char *buff, size_t size, size_t nmemb,
24 struct callback_data *data);
26 int main(int argc, char **argv)
30 /* curl easy handle */
34 struct callback_data data = { 0 };
36 /* global initialization */
37 rc = curl_global_init(CURL_GLOBAL_ALL);
41 /* initialization of easy handle */
42 handle = curl_easy_init();
44 curl_global_cleanup();
45 return CURLE_OUT_OF_MEMORY;
48 /* turn on wildcard matching */
49 curl_easy_setopt(handle, CURLOPT_WILDCARDMATCH, 1L);
51 /* callback is called before download of concrete file started */
52 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);
57 /* this callback will write contents into files */
58 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_it);
60 /* put transfer data into callbacks */
61 curl_easy_setopt(handle, CURLOPT_CHUNK_DATA, &data);
62 curl_easy_setopt(handle, CURLOPT_WRITEDATA, &data);
64 /* curl_easy_setopt(handle, CURLOPT_VERBOSE, 1L); */
66 /* set an URL containing wildcard pattern (only in the last part) */
68 curl_easy_setopt(handle, CURLOPT_URL, argv[1]);
70 curl_easy_setopt(handle, CURLOPT_URL, "ftp://example.com/test/*");
72 /* and start transfer! */
73 rc = curl_easy_perform(handle);
75 curl_easy_cleanup(handle);
76 curl_global_cleanup();
80 static long file_is_comming(struct curl_fileinfo *finfo,
81 struct callback_data *data,
84 printf("%3d %40s %10luB ", remains, finfo->filename,
85 (unsigned long)finfo->size);
87 switch(finfo->filetype) {
88 case CURLFILETYPE_DIRECTORY:
91 case CURLFILETYPE_FILE:
99 if(finfo->filetype == CURLFILETYPE_FILE) {
100 /* do not transfer files >= 50B */
101 if(finfo->size > 50) {
103 return CURL_CHUNK_BGN_FUNC_SKIP;
106 data->output = fopen(finfo->filename, "w");
108 return CURL_CHUNK_BGN_FUNC_FAIL;
112 return CURL_CHUNK_BGN_FUNC_OK;
115 static long file_is_downloaded(struct callback_data *data)
118 printf("DOWNLOADED\n");
119 fclose(data->output);
122 return CURL_CHUNK_END_FUNC_OK;
125 static size_t write_it(char *buff, size_t size, size_t nmemb,
126 struct callback_data *data)
130 written = fwrite(buff, size, nmemb, data->output);
133 written = fwrite(buff, size, nmemb, stdout);