|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h> |
|
|
#include <string.h> |
|
|
|
|
|
#include <curl/curl.h> |
|
|
#include <sys/types.h> |
|
|
#include <sys/stat.h> |
|
|
#include <fcntl.h> |
|
|
#include <errno.h> |
|
|
#ifdef _WIN32 |
|
|
#include <io.h> |
|
|
#else |
|
|
#include <unistd.h> |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define LOCAL_FILE "/tmp/uploadthis.txt" |
|
|
#define UPLOAD_FILE_AS "while-uploading.txt" |
|
|
#define REMOTE_URL "ftp://example.com/" UPLOAD_FILE_AS |
|
|
#define RENAME_FILE_TO "renamed-and-fine.txt" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream) |
|
|
{ |
|
|
unsigned long nread; |
|
|
|
|
|
|
|
|
|
|
|
size_t retcode = fread(ptr, size, nmemb, stream); |
|
|
|
|
|
if(retcode > 0) { |
|
|
nread = (unsigned long)retcode; |
|
|
fprintf(stderr, "*** We read %lu bytes from file\n", nread); |
|
|
} |
|
|
|
|
|
return retcode; |
|
|
} |
|
|
|
|
|
int main(void) |
|
|
{ |
|
|
CURL *curl; |
|
|
CURLcode res; |
|
|
FILE *hd_src; |
|
|
struct stat file_info; |
|
|
unsigned long fsize; |
|
|
|
|
|
struct curl_slist *headerlist = NULL; |
|
|
static const char buf_1 [] = "RNFR " UPLOAD_FILE_AS; |
|
|
static const char buf_2 [] = "RNTO " RENAME_FILE_TO; |
|
|
|
|
|
|
|
|
if(stat(LOCAL_FILE, &file_info)) { |
|
|
printf("Couldn't open '%s': %s\n", LOCAL_FILE, strerror(errno)); |
|
|
return 1; |
|
|
} |
|
|
fsize = (unsigned long)file_info.st_size; |
|
|
|
|
|
printf("Local file size: %lu bytes.\n", fsize); |
|
|
|
|
|
|
|
|
hd_src = fopen(LOCAL_FILE, "rb"); |
|
|
|
|
|
|
|
|
curl_global_init(CURL_GLOBAL_ALL); |
|
|
|
|
|
|
|
|
curl = curl_easy_init(); |
|
|
if(curl) { |
|
|
|
|
|
headerlist = curl_slist_append(headerlist, buf_1); |
|
|
headerlist = curl_slist_append(headerlist, buf_2); |
|
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback); |
|
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); |
|
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, REMOTE_URL); |
|
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist); |
|
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_READDATA, hd_src); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, |
|
|
(curl_off_t)fsize); |
|
|
|
|
|
|
|
|
res = curl_easy_perform(curl); |
|
|
|
|
|
if(res != CURLE_OK) |
|
|
fprintf(stderr, "curl_easy_perform() failed: %s\n", |
|
|
curl_easy_strerror(res)); |
|
|
|
|
|
|
|
|
curl_slist_free_all(headerlist); |
|
|
|
|
|
|
|
|
curl_easy_cleanup(curl); |
|
|
} |
|
|
fclose(hd_src); |
|
|
|
|
|
curl_global_cleanup(); |
|
|
return 0; |
|
|
} |
|
|
|