Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <stdio.h>
00023 #include <string.h>
00024
00025 #include <curl/curl.h>
00026 #include <sys/types.h>
00027 #include <sys/stat.h>
00028 #include <fcntl.h>
00029 #include <errno.h>
00030 #ifdef WIN32
00031 #include <io.h>
00032 #else
00033 #include <unistd.h>
00034 #endif
00035
00036
00037
00038
00039
00040
00041
00042 #define LOCAL_FILE "/tmp/uploadthis.txt"
00043 #define UPLOAD_FILE_AS "while-uploading.txt"
00044 #define REMOTE_URL "ftp://example.com/" UPLOAD_FILE_AS
00045 #define RENAME_FILE_TO "renamed-and-fine.txt"
00046
00047
00048
00049
00050
00051 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
00052 {
00053 curl_off_t nread;
00054
00055
00056
00057 size_t retcode = fread(ptr, size, nmemb, stream);
00058
00059 nread = (curl_off_t)retcode;
00060
00061 fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
00062 " bytes from file\n", nread);
00063 return retcode;
00064 }
00065
00066 int main(void)
00067 {
00068 CURL *curl;
00069 CURLcode res;
00070 FILE *hd_src;
00071 struct stat file_info;
00072 curl_off_t fsize;
00073
00074 struct curl_slist *headerlist=NULL;
00075 static const char buf_1 [] = "RNFR " UPLOAD_FILE_AS;
00076 static const char buf_2 [] = "RNTO " RENAME_FILE_TO;
00077
00078
00079 if(stat(LOCAL_FILE, &file_info)) {
00080 printf("Couldnt open '%s': %s\n", LOCAL_FILE, strerror(errno));
00081 return 1;
00082 }
00083 fsize = (curl_off_t)file_info.st_size;
00084
00085 printf("Local file size: %" CURL_FORMAT_CURL_OFF_T " bytes.\n", fsize);
00086
00087
00088 hd_src = fopen(LOCAL_FILE, "rb");
00089
00090
00091 curl_global_init(CURL_GLOBAL_ALL);
00092
00093
00094 curl = curl_easy_init();
00095 if(curl) {
00096
00097 headerlist = curl_slist_append(headerlist, buf_1);
00098 headerlist = curl_slist_append(headerlist, buf_2);
00099
00100
00101 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
00102
00103
00104 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
00105
00106
00107 curl_easy_setopt(curl, CURLOPT_URL, REMOTE_URL);
00108
00109
00110 curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
00111
00112
00113 curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
00114
00115
00116
00117
00118
00119 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
00120 (curl_off_t)fsize);
00121
00122
00123 res = curl_easy_perform(curl);
00124
00125 if(res != CURLE_OK)
00126 fprintf(stderr, "curl_easy_perform() failed: %s\n",
00127 curl_easy_strerror(res));
00128
00129
00130 curl_slist_free_all(headerlist);
00131
00132
00133 curl_easy_cleanup(curl);
00134 }
00135 fclose(hd_src);
00136
00137 curl_global_cleanup();
00138 return 0;
00139 }