#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <curl/easy.h>
struct FTPFile {
const char *filename;
FILE *fp;
};
static size_t save_file(void *ptr, size_t size, size_t nmemb, void *userdata)
{
struct FTPFile *out = (struct FTPFile *)userdata;
if (out && !out->fp) {
out->fp = fopen(out->filename, "w");
if (!out->fp)
return -1;
}
return fwrite(ptr, size, nmemb, out->fp);
}
int main(int argc, char **argv)
{
CURL *curl;
CURLcode res;
char *p;
if (argc != 2 || !!strncmp(argv[1], "ftp://", 6)){
printf ("Usage: %s ftp://path/your_file\n", argv[0]);
exit (EXIT_FAILURE);
}
p = strrchr(argv[1], '/');
p += 1;
struct FTPFile ftpfile = {
p,
NULL
};
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, save_file);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf (stderr, "Error Ocurred\n");
}
curl_easy_cleanup(curl);
curl_global_cleanup();
fclose(ftpfile.fp);
return 0;
}
beyes@debian:~/C/curl$ ./getftpfile ftp://vh491563:2tz92d23@121.15.245.187/www/bbs/favicon.ico
* About to connect() to 121.15.245.187 port 21 (#0)
* Trying 121.15.245.187... * connected
* Connected to 121.15.245.187 (121.15.245.187) port 21 (#0)
< 220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
< 220-You are user number 1 of 50 allowed.
< 220-Local time is now 17:26. Server port: 21.
< 220-IPv6 connections are also welcome on this server.
< 220 You will be disconnected after 15 minutes of inactivity.
> USER vh491563
< 331 User vh492363 OK. Password required
> PASS 2tz92d23
< 230-User vh491563 has group access to: vh491563
< 230 OK. Current restricted directory is /
> PWD
< 257 "/" is your current location
* Entry path is '/'
> CWD www
< 250 OK. Current directory is /www
> CWD bbs
< 250 OK. Current directory is /www/bbs
> EPSV
* Connect data stream passively
< 229 Extended Passive mode OK (|||59625|)
* Trying 121.15.245.187... * connected
* Connecting to 121.15.245.187 (121.15.245.187) port 59625
> TYPE I
< 200 TYPE is now 8-bit binary
> SIZE favicon.ico
< 213 1758
> RETR favicon.ico
< 150 Accepted data connection
* Maxdownload = -1
* Getting file with size: 1758
* Remembering we are in dir "www/bbs/"
< 226-File successfully transferred
< 226 0.000 seconds (measured here), 39.07 Mbytes per second
* Connection #0 to host 121.15.245.187 left intact
> QUIT
< 221-Goodbye. You uploaded 0 and downloaded 2 kbytes.
< 221 Logout.
* Closing connection #0
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |