xref: /curl/docs/examples/ftpget.c (revision 95a882d2)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 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.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  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 #include <stdio.h>
25 
26 #include <curl/curl.h>
27 
28 /* <DESC>
29  * Get a single file from an FTP server.
30  * </DESC>
31  */
32 
33 struct FtpFile {
34   const char *filename;
35   FILE *stream;
36 };
37 
my_fwrite(void * buffer,size_t size,size_t nmemb,void * stream)38 static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
39 {
40   struct FtpFile *out = (struct FtpFile *)stream;
41   if(!out->stream) {
42     /* open file for writing */
43     out->stream = fopen(out->filename, "wb");
44     if(!out->stream)
45       return 0; /* failure, cannot open file to write */
46   }
47   return fwrite(buffer, size, nmemb, out->stream);
48 }
49 
50 
main(void)51 int main(void)
52 {
53   CURL *curl;
54   CURLcode res;
55   struct FtpFile ftpfile = {
56     "curl.tar.gz", /* name to store the file as if successful */
57     NULL
58   };
59 
60   curl_global_init(CURL_GLOBAL_DEFAULT);
61 
62   curl = curl_easy_init();
63   if(curl) {
64     /*
65      * You better replace the URL with one that works!
66      */
67     curl_easy_setopt(curl, CURLOPT_URL,
68                      "ftp://ftp.example.com/curl/curl-7.9.2.tar.gz");
69     /* Define our callback to get called when there is data to be written */
70     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
71     /* Set a pointer to our struct to pass to the callback */
72     curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
73 
74     /* Switch on full protocol/debug output */
75     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
76 
77     res = curl_easy_perform(curl);
78 
79     /* always cleanup */
80     curl_easy_cleanup(curl);
81 
82     if(CURLE_OK != res) {
83       /* we failed */
84       fprintf(stderr, "curl told us %d\n", res);
85     }
86   }
87 
88   if(ftpfile.stream)
89     fclose(ftpfile.stream); /* close the local file */
90 
91   curl_global_cleanup();
92 
93   return 0;
94 }
95