1 /*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) 1998 - 2012, 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 http://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 23 #include <stdio.h> 24 25 #include <curl/curl.h> 26 27 /* 28 * This is an example showing how to get a single file from an FTPS server. 29 * It delays the actual destination file creation until the first write 30 * callback so that it won't create an empty file in case the remote file 31 * doesn't exist or something else fails. 32 */ 33 34 struct FtpFile { 35 const char *filename; 36 FILE *stream; 37 }; 38 39 static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, 40 void *stream) 41 { 42 struct FtpFile *out=(struct FtpFile *)stream; 43 if(out && !out->stream) { 44 /* open file for writing */ 45 out->stream=fopen(out->filename, "wb"); 46 if(!out->stream) 47 return -1; /* failure, can't open file to write */ 48 } 49 return fwrite(buffer, size, nmemb, out->stream); 50 } 51 52 53 int main(void) 54 { 55 CURL *curl; 56 CURLcode res; 57 struct FtpFile ftpfile={ 58 "yourfile.bin", /* name to store the file as if successful */ 59 NULL 60 }; 61 62 curl_global_init(CURL_GLOBAL_DEFAULT); 63 64 curl = curl_easy_init(); 65 if(curl) { 66 /* 67 * You better replace the URL with one that works! Note that we use an 68 * FTP:// URL with standard explicit FTPS. You can also do FTPS:// URLs if 69 * you want to do the rarer kind of transfers: implicit. 70 */ 71 curl_easy_setopt(curl, CURLOPT_URL, 72 "ftp://user@server/home/user/file.txt"); 73 /* Define our callback to get called when there's data to be written */ 74 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite); 75 /* Set a pointer to our struct to pass to the callback */ 76 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile); 77 78 /* We activate SSL and we require it for both control and data */ 79 curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL); 80 81 /* Switch on full protocol/debug output */ 82 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); 83 84 res = curl_easy_perform(curl); 85 86 /* always cleanup */ 87 curl_easy_cleanup(curl); 88 89 if(CURLE_OK != res) { 90 /* we failed */ 91 fprintf(stderr, "curl told us %d\n", res); 92 } 93 } 94 95 if(ftpfile.stream) 96 fclose(ftpfile.stream); /* close the local file */ 97 98 curl_global_cleanup(); 99 100 return 0; 101 } 102