1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
12 #include <curl/curl.h>
13 #include <curl/types.h>
14 #include <curl/easy.h>
17 * This is an example showing how to get a single file from an FTP server.
18 * It delays the actual destination file creation until the first write
19 * callback so that it won't create an empty file in case the remote file
20 * doesn't exist or something else fails.
28 static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
30 struct FtpFile *out=(struct FtpFile *)stream;
31 if(out && !out->stream) {
32 /* open file for writing */
33 out->stream=fopen(out->filename, "wb");
35 return -1; /* failure, can't open file to write */
37 return fwrite(buffer, size, nmemb, out->stream);
45 struct FtpFile ftpfile={
46 "curl.tar.gz", /* name to store the file as if succesful */
50 curl_global_init(CURL_GLOBAL_DEFAULT);
52 curl = curl_easy_init();
55 * You better replace the URL with one that works!
57 curl_easy_setopt(curl, CURLOPT_URL,
58 "ftp://ftp.example.com/pub/www/utilities/curl/curl-7.9.2.tar.gz");
59 /* Define our callback to get called when there's data to be written */
60 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
61 /* Set a pointer to our struct to pass to the callback */
62 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
64 /* Switch on full protocol/debug output */
65 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
67 res = curl_easy_perform(curl);
70 curl_easy_cleanup(curl);
74 fprintf(stderr, "curl told us %d\n", res);
79 fclose(ftpfile.stream); /* close the local file */
81 curl_global_cleanup();