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