- 论坛徽章:
- 0
|
急求代码实现 ftp client 的东西
- 1 /*****************************************************************************
- 2 * _ _ ____ _
- 3 * Project ___| | | | _ \| |
- 4 * / __| | | | |_) | |
- 5 * | (__| |_| | _ <| |___
- 6 * \___|\___/|_| \_\_____|
- 7 *
- 8 * $Id: ftpget.c,v 1.2 2002/01/08 13:05:44 bagder Exp $
- 9 */
- 10
- 11 #include <stdio.h>;
- 12
- 13 #include <curl/curl.h>;
- 14 #include <curl/types.h>;
- 15 #include <curl/easy.h>;
- 16
- 17 /*
- 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.
- 22 */
- 23
- 24 struct FtpFile {
- 25 char *filename;
- 26 FILE *stream;
- 27 };
- 28
- 29 int my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
- 30 {
- 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 */
- 37 }
- 38 return fwrite(buffer, size, nmemb, out->;stream);
- 39 }
- 40
- 41
- 42 int main(void)
- 43 {
- 44 CURL *curl;
- 45 CURLcode res;
- 46 struct FtpFile ftpfile={
- 47 "curl.tar.gz", /* name to store the file as if succesful */
- 48 NULL
- 49 };
- 50
- 51 curl_global_init(CURL_GLOBAL_DEFAULT);
- 52
- 53 curl = curl_easy_init();
- 54 if(curl) {
- 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_FILE, &ftpfile);
- 62
- 63 /* Switch on full protocol/debug output */
- 64 curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE);
- 65
- 66 res = curl_easy_perform(curl);
- 67
- 68 /* always cleanup */
- 69 curl_easy_cleanup(curl);
- 70
- 71 if(CURLE_OK != res) {
- 72 /* we failed */
- 73 fprintf(stderr, "curl told us %d\n", res);
- 74 }
- 75 }
- 76
- 77 if(ftpfile.stream)
- 78 fclose(ftpfile.stream); /* close the local file */
- 79
- 80 curl_global_cleanup();
- 81
- 82 return 0;
- 83 }
- 84
复制代码 |
|