ftpsget.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2015, 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 
00023 #include <stdio.h>
00024 
00025 #include <curl/curl.h>
00026 
00027 /* <DESC>
00028  * Get a single file from an FTPS server.
00029  * </DESC>
00030  */
00031 
00032 struct FtpFile {
00033   const char *filename;
00034   FILE *stream;
00035 };
00036 
00037 static size_t my_fwrite(void *buffer, size_t size, size_t nmemb,
00038                         void *stream)
00039 {
00040   struct FtpFile *out=(struct FtpFile *)stream;
00041   if(out && !out->stream) {
00042     /* open file for writing */
00043     out->stream=fopen(out->filename, "wb");
00044     if(!out->stream)
00045       return -1; /* failure, can't open file to write */
00046   }
00047   return fwrite(buffer, size, nmemb, out->stream);
00048 }
00049 
00050 
00051 int main(void)
00052 {
00053   CURL *curl;
00054   CURLcode res;
00055   struct FtpFile ftpfile={
00056     "yourfile.bin", /* name to store the file as if successful */
00057     NULL
00058   };
00059 
00060   curl_global_init(CURL_GLOBAL_DEFAULT);
00061 
00062   curl = curl_easy_init();
00063   if(curl) {
00064     /*
00065      * You better replace the URL with one that works! Note that we use an
00066      * FTP:// URL with standard explicit FTPS. You can also do FTPS:// URLs if
00067      * you want to do the rarer kind of transfers: implicit.
00068      */
00069     curl_easy_setopt(curl, CURLOPT_URL,
00070                      "ftp://user@server/home/user/file.txt");
00071     /* Define our callback to get called when there's data to be written */
00072     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
00073     /* Set a pointer to our struct to pass to the callback */
00074     curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
00075 
00076     /* We activate SSL and we require it for both control and data */
00077     curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
00078 
00079     /* Switch on full protocol/debug output */
00080     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
00081 
00082     res = curl_easy_perform(curl);
00083 
00084     /* always cleanup */
00085     curl_easy_cleanup(curl);
00086 
00087     if(CURLE_OK != res) {
00088       /* we failed */
00089       fprintf(stderr, "curl told us %d\n", res);
00090     }
00091   }
00092 
00093   if(ftpfile.stream)
00094     fclose(ftpfile.stream); /* close the local file */
00095 
00096   curl_global_cleanup();
00097 
00098   return 0;
00099 }


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