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