Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <curl/curl.h>
00027 #include <stdio.h>
00028
00029 struct callback_data {
00030 FILE *output;
00031 };
00032
00033 static long file_is_coming(struct curl_fileinfo *finfo,
00034 struct callback_data *data,
00035 int remains);
00036
00037 static long file_is_downloaded(struct callback_data *data);
00038
00039 static size_t write_it(char *buff, size_t size, size_t nmemb,
00040 void *cb_data);
00041
00042 int main(int argc, char **argv)
00043 {
00044 int rc = CURLE_OK;
00045
00046
00047 CURL *handle;
00048
00049
00050 struct callback_data data = { 0 };
00051
00052
00053 rc = curl_global_init(CURL_GLOBAL_ALL);
00054 if(rc)
00055 return rc;
00056
00057
00058 handle = curl_easy_init();
00059 if(!handle) {
00060 curl_global_cleanup();
00061 return CURLE_OUT_OF_MEMORY;
00062 }
00063
00064
00065 curl_easy_setopt(handle, CURLOPT_WILDCARDMATCH, 1L);
00066
00067
00068 curl_easy_setopt(handle, CURLOPT_CHUNK_BGN_FUNCTION, file_is_coming);
00069
00070
00071 curl_easy_setopt(handle, CURLOPT_CHUNK_END_FUNCTION, file_is_downloaded);
00072
00073
00074 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_it);
00075
00076
00077 curl_easy_setopt(handle, CURLOPT_CHUNK_DATA, &data);
00078 curl_easy_setopt(handle, CURLOPT_WRITEDATA, &data);
00079
00080
00081
00082
00083 if(argc == 2)
00084 curl_easy_setopt(handle, CURLOPT_URL, argv[1]);
00085 else
00086 curl_easy_setopt(handle, CURLOPT_URL, "ftp://example.com/test/*");
00087
00088
00089 rc = curl_easy_perform(handle);
00090
00091 curl_easy_cleanup(handle);
00092 curl_global_cleanup();
00093 return rc;
00094 }
00095
00096 static long file_is_coming(struct curl_fileinfo *finfo,
00097 struct callback_data *data,
00098 int remains)
00099 {
00100 printf("%3d %40s %10luB ", remains, finfo->filename,
00101 (unsigned long)finfo->size);
00102
00103 switch(finfo->filetype) {
00104 case CURLFILETYPE_DIRECTORY:
00105 printf(" DIR\n");
00106 break;
00107 case CURLFILETYPE_FILE:
00108 printf("FILE ");
00109 break;
00110 default:
00111 printf("OTHER\n");
00112 break;
00113 }
00114
00115 if(finfo->filetype == CURLFILETYPE_FILE) {
00116
00117 if(finfo->size > 50) {
00118 printf("SKIPPED\n");
00119 return CURL_CHUNK_BGN_FUNC_SKIP;
00120 }
00121
00122 data->output = fopen(finfo->filename, "wb");
00123 if(!data->output) {
00124 return CURL_CHUNK_BGN_FUNC_FAIL;
00125 }
00126 }
00127
00128 return CURL_CHUNK_BGN_FUNC_OK;
00129 }
00130
00131 static long file_is_downloaded(struct callback_data *data)
00132 {
00133 if(data->output) {
00134 printf("DOWNLOADED\n");
00135 fclose(data->output);
00136 data->output = 0x0;
00137 }
00138 return CURL_CHUNK_END_FUNC_OK;
00139 }
00140
00141 static size_t write_it(char *buff, size_t size, size_t nmemb,
00142 void *cb_data)
00143 {
00144 struct callback_data *data = cb_data;
00145 size_t written = 0;
00146 if(data->output)
00147 written = fwrite(buff, size, nmemb, data->output);
00148 else
00149
00150 written = fwrite(buff, size, nmemb, stdout);
00151 return written;
00152 }