Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / docs / examples / ftpget.c
blob40c8802452432c552d212feb1ea43dbf0af33ff7
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: ftpget.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
9 */
11 #include <stdio.h>
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.
24 struct FtpFile {
25 const char *filename;
26 FILE *stream;
29 static size_t 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");
35 if(!out->stream)
36 return -1; /* failure, can't open file to write */
38 return fwrite(buffer, size, nmemb, out->stream);
42 int main(void)
44 CURL *curl;
45 CURLcode res;
46 struct FtpFile ftpfile={
47 "curl.tar.gz", /* name to store the file as if succesful */
48 NULL
51 curl_global_init(CURL_GLOBAL_DEFAULT);
53 curl = curl_easy_init();
54 if(curl) {
56 * Get curl 7.9.2 from sunet.se's FTP site. curl 7.9.2 is most likely not
57 * present there by the time you read this, so you'd better replace the
58 * URL with one that works!
60 curl_easy_setopt(curl, CURLOPT_URL,
61 "ftp://ftp.sunet.se/pub/www/utilities/curl/curl-7.9.2.tar.gz");
62 /* Define our callback to get called when there's data to be written */
63 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
64 /* Set a pointer to our struct to pass to the callback */
65 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
67 /* Switch on full protocol/debug output */
68 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
70 res = curl_easy_perform(curl);
72 /* always cleanup */
73 curl_easy_cleanup(curl);
75 if(CURLE_OK != res) {
76 /* we failed */
77 fprintf(stderr, "curl told us %d\n", res);
81 if(ftpfile.stream)
82 fclose(ftpfile.stream); /* close the local file */
84 curl_global_cleanup();
86 return 0;