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