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


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:09