1/*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) 1998 - 2011, 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 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#include <stdio.h> 23 24#include <curl/curl.h> 25#include <curl/types.h> 26#include <curl/easy.h> 27 28/* 29 * This is an example showing how to get a single file from an FTP server. 30 * It delays the actual destination file creation until the first write 31 * callback so that it won't create an empty file in case the remote file 32 * doesn't exist or something else fails. 33 */ 34 35struct FtpFile { 36 const char *filename; 37 FILE *stream; 38}; 39 40static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, 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 53int main(void) 54{ 55 CURL *curl; 56 CURLcode res; 57 struct FtpFile ftpfile={ 58 "curl.tar.gz", /* name to store the file as if succesful */ 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! 68 */ 69 curl_easy_setopt(curl, CURLOPT_URL, 70 "ftp://ftp.example.com/pub/www/utilities/curl/curl-7.9.2.tar.gz"); 71 /* Define our callback to get called when there's data to be written */ 72 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite); 73 /* Set a pointer to our struct to pass to the callback */ 74 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile); 75 76 /* Switch on full protocol/debug output */ 77 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); 78 79 res = curl_easy_perform(curl); 80 81 /* always cleanup */ 82 curl_easy_cleanup(curl); 83 84 if(CURLE_OK != res) { 85 /* we failed */ 86 fprintf(stderr, "curl told us %d\n", res); 87 } 88 } 89 90 if(ftpfile.stream) 91 fclose(ftpfile.stream); /* close the local file */ 92 93 curl_global_cleanup(); 94 95 return 0; 96} 97