1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: ftpget.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman 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 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");
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();
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
);
73 curl_easy_cleanup(curl
);
77 fprintf(stderr
, "curl told us %d\n", res
);
82 fclose(ftpfile
.stream
); /* close the local file */
84 curl_global_cleanup();