ftp-wildcard.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 /* <DESC>
00023  * FTP wildcard pattern matching
00024  * </DESC>
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   /* curl easy handle */
00047   CURL *handle;
00048 
00049   /* help data */
00050   struct callback_data data = { 0 };
00051 
00052   /* global initialization */
00053   rc = curl_global_init(CURL_GLOBAL_ALL);
00054   if(rc)
00055     return rc;
00056 
00057   /* initialization of easy handle */
00058   handle = curl_easy_init();
00059   if(!handle) {
00060     curl_global_cleanup();
00061     return CURLE_OUT_OF_MEMORY;
00062   }
00063 
00064   /* turn on wildcard matching */
00065   curl_easy_setopt(handle, CURLOPT_WILDCARDMATCH, 1L);
00066 
00067   /* callback is called before download of concrete file started */
00068   curl_easy_setopt(handle, CURLOPT_CHUNK_BGN_FUNCTION, file_is_coming);
00069 
00070   /* callback is called after data from the file have been transferred */
00071   curl_easy_setopt(handle, CURLOPT_CHUNK_END_FUNCTION, file_is_downloaded);
00072 
00073   /* this callback will write contents into files */
00074   curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_it);
00075 
00076   /* put transfer data into callbacks */
00077   curl_easy_setopt(handle, CURLOPT_CHUNK_DATA, &data);
00078   curl_easy_setopt(handle, CURLOPT_WRITEDATA, &data);
00079 
00080   /* curl_easy_setopt(handle, CURLOPT_VERBOSE, 1L); */
00081 
00082   /* set an URL containing wildcard pattern (only in the last part) */
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   /* and start transfer! */
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     /* do not transfer files >= 50B */
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     /* listing output */
00150     written = fwrite(buff, size, nmemb, stdout);
00151   return written;
00152 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:02