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