ftpget.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 #include <stdio.h>
00023 
00024 #include <curl/curl.h>
00025 
00026 /* <DESC>
00027  * Get a single file from an FTP server.
00028  * </DESC>
00029  */
00030 
00031 struct FtpFile {
00032   const char *filename;
00033   FILE *stream;
00034 };
00035 
00036 static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
00037 {
00038   struct FtpFile *out=(struct FtpFile *)stream;
00039   if(out && !out->stream) {
00040     /* open file for writing */
00041     out->stream=fopen(out->filename, "wb");
00042     if(!out->stream)
00043       return -1; /* failure, can't open file to write */
00044   }
00045   return fwrite(buffer, size, nmemb, out->stream);
00046 }
00047 
00048 
00049 int main(void)
00050 {
00051   CURL *curl;
00052   CURLcode res;
00053   struct FtpFile ftpfile={
00054     "curl.tar.gz", /* name to store the file as if successful */
00055     NULL
00056   };
00057 
00058   curl_global_init(CURL_GLOBAL_DEFAULT);
00059 
00060   curl = curl_easy_init();
00061   if(curl) {
00062     /*
00063      * You better replace the URL with one that works!
00064      */
00065     curl_easy_setopt(curl, CURLOPT_URL,
00066                      "ftp://ftp.example.com/curl/curl-7.9.2.tar.gz");
00067     /* Define our callback to get called when there's data to be written */
00068     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
00069     /* Set a pointer to our struct to pass to the callback */
00070     curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
00071 
00072     /* Switch on full protocol/debug output */
00073     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
00074 
00075     res = curl_easy_perform(curl);
00076 
00077     /* always cleanup */
00078     curl_easy_cleanup(curl);
00079 
00080     if(CURLE_OK != res) {
00081       /* we failed */
00082       fprintf(stderr, "curl told us %d\n", res);
00083     }
00084   }
00085 
00086   if(ftpfile.stream)
00087     fclose(ftpfile.stream); /* close the local file */
00088 
00089   curl_global_cleanup();
00090 
00091   return 0;
00092 }


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