113498266Sopenharmony_ci/*************************************************************************** 213498266Sopenharmony_ci * _ _ ____ _ 313498266Sopenharmony_ci * Project ___| | | | _ \| | 413498266Sopenharmony_ci * / __| | | | |_) | | 513498266Sopenharmony_ci * | (__| |_| | _ <| |___ 613498266Sopenharmony_ci * \___|\___/|_| \_\_____| 713498266Sopenharmony_ci * 813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 913498266Sopenharmony_ci * 1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which 1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms 1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html. 1313498266Sopenharmony_ci * 1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell 1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is 1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file. 1713498266Sopenharmony_ci * 1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 1913498266Sopenharmony_ci * KIND, either express or implied. 2013498266Sopenharmony_ci * 2113498266Sopenharmony_ci * SPDX-License-Identifier: curl 2213498266Sopenharmony_ci * 2313498266Sopenharmony_ci ***************************************************************************/ 2413498266Sopenharmony_ci/* <DESC> 2513498266Sopenharmony_ci * FTP wildcard pattern matching 2613498266Sopenharmony_ci * </DESC> 2713498266Sopenharmony_ci */ 2813498266Sopenharmony_ci#include <curl/curl.h> 2913498266Sopenharmony_ci#include <stdio.h> 3013498266Sopenharmony_ci 3113498266Sopenharmony_cistruct callback_data { 3213498266Sopenharmony_ci FILE *output; 3313498266Sopenharmony_ci}; 3413498266Sopenharmony_ci 3513498266Sopenharmony_cistatic long file_is_coming(struct curl_fileinfo *finfo, 3613498266Sopenharmony_ci struct callback_data *data, 3713498266Sopenharmony_ci int remains); 3813498266Sopenharmony_ci 3913498266Sopenharmony_cistatic long file_is_downloaded(struct callback_data *data); 4013498266Sopenharmony_ci 4113498266Sopenharmony_cistatic size_t write_it(char *buff, size_t size, size_t nmemb, 4213498266Sopenharmony_ci void *cb_data); 4313498266Sopenharmony_ci 4413498266Sopenharmony_ciint main(int argc, char **argv) 4513498266Sopenharmony_ci{ 4613498266Sopenharmony_ci /* curl easy handle */ 4713498266Sopenharmony_ci CURL *handle; 4813498266Sopenharmony_ci 4913498266Sopenharmony_ci /* help data */ 5013498266Sopenharmony_ci struct callback_data data = { 0 }; 5113498266Sopenharmony_ci 5213498266Sopenharmony_ci /* global initialization */ 5313498266Sopenharmony_ci int rc = curl_global_init(CURL_GLOBAL_ALL); 5413498266Sopenharmony_ci if(rc) 5513498266Sopenharmony_ci return rc; 5613498266Sopenharmony_ci 5713498266Sopenharmony_ci /* initialization of easy handle */ 5813498266Sopenharmony_ci handle = curl_easy_init(); 5913498266Sopenharmony_ci if(!handle) { 6013498266Sopenharmony_ci curl_global_cleanup(); 6113498266Sopenharmony_ci return CURLE_OUT_OF_MEMORY; 6213498266Sopenharmony_ci } 6313498266Sopenharmony_ci 6413498266Sopenharmony_ci /* turn on wildcard matching */ 6513498266Sopenharmony_ci curl_easy_setopt(handle, CURLOPT_WILDCARDMATCH, 1L); 6613498266Sopenharmony_ci 6713498266Sopenharmony_ci /* callback is called before download of concrete file started */ 6813498266Sopenharmony_ci curl_easy_setopt(handle, CURLOPT_CHUNK_BGN_FUNCTION, file_is_coming); 6913498266Sopenharmony_ci 7013498266Sopenharmony_ci /* callback is called after data from the file have been transferred */ 7113498266Sopenharmony_ci curl_easy_setopt(handle, CURLOPT_CHUNK_END_FUNCTION, file_is_downloaded); 7213498266Sopenharmony_ci 7313498266Sopenharmony_ci /* this callback will write contents into files */ 7413498266Sopenharmony_ci curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_it); 7513498266Sopenharmony_ci 7613498266Sopenharmony_ci /* put transfer data into callbacks */ 7713498266Sopenharmony_ci curl_easy_setopt(handle, CURLOPT_CHUNK_DATA, &data); 7813498266Sopenharmony_ci curl_easy_setopt(handle, CURLOPT_WRITEDATA, &data); 7913498266Sopenharmony_ci 8013498266Sopenharmony_ci /* curl_easy_setopt(handle, CURLOPT_VERBOSE, 1L); */ 8113498266Sopenharmony_ci 8213498266Sopenharmony_ci /* set a URL containing wildcard pattern (only in the last part) */ 8313498266Sopenharmony_ci if(argc == 2) 8413498266Sopenharmony_ci curl_easy_setopt(handle, CURLOPT_URL, argv[1]); 8513498266Sopenharmony_ci else 8613498266Sopenharmony_ci curl_easy_setopt(handle, CURLOPT_URL, "ftp://example.com/test/*"); 8713498266Sopenharmony_ci 8813498266Sopenharmony_ci /* and start transfer! */ 8913498266Sopenharmony_ci rc = curl_easy_perform(handle); 9013498266Sopenharmony_ci 9113498266Sopenharmony_ci curl_easy_cleanup(handle); 9213498266Sopenharmony_ci curl_global_cleanup(); 9313498266Sopenharmony_ci return rc; 9413498266Sopenharmony_ci} 9513498266Sopenharmony_ci 9613498266Sopenharmony_cistatic long file_is_coming(struct curl_fileinfo *finfo, 9713498266Sopenharmony_ci struct callback_data *data, 9813498266Sopenharmony_ci int remains) 9913498266Sopenharmony_ci{ 10013498266Sopenharmony_ci printf("%3d %40s %10luB ", remains, finfo->filename, 10113498266Sopenharmony_ci (unsigned long)finfo->size); 10213498266Sopenharmony_ci 10313498266Sopenharmony_ci switch(finfo->filetype) { 10413498266Sopenharmony_ci case CURLFILETYPE_DIRECTORY: 10513498266Sopenharmony_ci printf(" DIR\n"); 10613498266Sopenharmony_ci break; 10713498266Sopenharmony_ci case CURLFILETYPE_FILE: 10813498266Sopenharmony_ci printf("FILE "); 10913498266Sopenharmony_ci break; 11013498266Sopenharmony_ci default: 11113498266Sopenharmony_ci printf("OTHER\n"); 11213498266Sopenharmony_ci break; 11313498266Sopenharmony_ci } 11413498266Sopenharmony_ci 11513498266Sopenharmony_ci if(finfo->filetype == CURLFILETYPE_FILE) { 11613498266Sopenharmony_ci /* do not transfer files >= 50B */ 11713498266Sopenharmony_ci if(finfo->size > 50) { 11813498266Sopenharmony_ci printf("SKIPPED\n"); 11913498266Sopenharmony_ci return CURL_CHUNK_BGN_FUNC_SKIP; 12013498266Sopenharmony_ci } 12113498266Sopenharmony_ci 12213498266Sopenharmony_ci data->output = fopen(finfo->filename, "wb"); 12313498266Sopenharmony_ci if(!data->output) { 12413498266Sopenharmony_ci return CURL_CHUNK_BGN_FUNC_FAIL; 12513498266Sopenharmony_ci } 12613498266Sopenharmony_ci } 12713498266Sopenharmony_ci 12813498266Sopenharmony_ci return CURL_CHUNK_BGN_FUNC_OK; 12913498266Sopenharmony_ci} 13013498266Sopenharmony_ci 13113498266Sopenharmony_cistatic long file_is_downloaded(struct callback_data *data) 13213498266Sopenharmony_ci{ 13313498266Sopenharmony_ci if(data->output) { 13413498266Sopenharmony_ci printf("DOWNLOADED\n"); 13513498266Sopenharmony_ci fclose(data->output); 13613498266Sopenharmony_ci data->output = 0x0; 13713498266Sopenharmony_ci } 13813498266Sopenharmony_ci return CURL_CHUNK_END_FUNC_OK; 13913498266Sopenharmony_ci} 14013498266Sopenharmony_ci 14113498266Sopenharmony_cistatic size_t write_it(char *buff, size_t size, size_t nmemb, 14213498266Sopenharmony_ci void *cb_data) 14313498266Sopenharmony_ci{ 14413498266Sopenharmony_ci struct callback_data *data = cb_data; 14513498266Sopenharmony_ci size_t written = 0; 14613498266Sopenharmony_ci if(data->output) 14713498266Sopenharmony_ci written = fwrite(buff, size, nmemb, data->output); 14813498266Sopenharmony_ci else 14913498266Sopenharmony_ci /* listing output */ 15013498266Sopenharmony_ci written = fwrite(buff, size, nmemb, stdout); 15113498266Sopenharmony_ci return written; 15213498266Sopenharmony_ci} 153