1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: ftpget.c,v 1.3 2003/12/08 14:13:19 bagder Exp $
13 #include <curl/curl.h>
14 #include <curl/types.h>
15 #include <curl/easy.h>
18 * This is an example showing how to get a single file from an FTP server.
19 * It delays the actual destination file creation until the first write
20 * callback so that it won't create an empty file in case the remote file
21 * doesn't exist or something else fails.
29 int my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
31 struct FtpFile *out=(struct FtpFile *)stream;
32 if(out && !out->stream) {
33 /* open file for writing */
34 out->stream=fopen(out->filename, "wb");
36 return -1; /* failure, can't open file to write */
38 return fwrite(buffer, size, nmemb, out->stream);
46 struct FtpFile ftpfile={
47 "curl.tar.gz", /* name to store the file as if succesful */
51 curl_global_init(CURL_GLOBAL_DEFAULT);
53 curl = curl_easy_init();
55 /* Get curl 7.9.2 from sunet.se's FTP site: */
56 curl_easy_setopt(curl, CURLOPT_URL,
57 "ftp://ftp.sunet.se/pub/www/utilities/curl/curl-7.9.2.tar.gz");
58 /* Define our callback to get called when there's data to be written */
59 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
60 /* Set a pointer to our struct to pass to the callback */
61 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
63 /* Switch on full protocol/debug output */
64 curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE);
66 res = curl_easy_perform(curl);
69 curl_easy_cleanup(curl);
73 fprintf(stderr, "curl told us %d\n", res);
78 fclose(ftpfile.stream); /* close the local file */
80 curl_global_cleanup();