00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <stdlib.h>
00028 #include <stdio.h>
00029
00030 #include <curl/curl.h>
00031
00032 #if defined(_MSC_VER) && (_MSC_VER < 1300)
00033 # error _snscanf requires MSVC 7.0 or later.
00034 #endif
00035
00036
00037
00038 #if defined(__MINGW32__) && !defined(__MINGW64__)
00039 int __cdecl _snscanf(const char *input, size_t length,
00040 const char *format, ...);
00041 #endif
00042
00043
00044
00045 size_t getcontentlengthfunc(void *ptr, size_t size, size_t nmemb, void *stream)
00046 {
00047 int r;
00048 long len = 0;
00049
00050
00051 r = _snscanf(ptr, size * nmemb, "Content-Length: %ld\n", &len);
00052
00053 if(r)
00054 *((long *) stream) = len;
00055
00056 return size * nmemb;
00057 }
00058
00059
00060 size_t discardfunc(void *ptr, size_t size, size_t nmemb, void *stream)
00061 {
00062 return size * nmemb;
00063 }
00064
00065
00066 size_t readfunc(void *ptr, size_t size, size_t nmemb, void *stream)
00067 {
00068 FILE *f = stream;
00069 size_t n;
00070
00071 if(ferror(f))
00072 return CURL_READFUNC_ABORT;
00073
00074 n = fread(ptr, size, nmemb, f) * size;
00075
00076 return n;
00077 }
00078
00079
00080 int upload(CURL *curlhandle, const char *remotepath, const char *localpath,
00081 long timeout, long tries)
00082 {
00083 FILE *f;
00084 long uploaded_len = 0;
00085 CURLcode r = CURLE_GOT_NOTHING;
00086 int c;
00087
00088 f = fopen(localpath, "rb");
00089 if(!f) {
00090 perror(NULL);
00091 return 0;
00092 }
00093
00094 curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
00095
00096 curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
00097
00098 if(timeout)
00099 curl_easy_setopt(curlhandle, CURLOPT_FTP_RESPONSE_TIMEOUT, timeout);
00100
00101 curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);
00102 curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &uploaded_len);
00103
00104 curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, discardfunc);
00105
00106 curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);
00107 curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);
00108
00109
00110 curl_easy_setopt(curlhandle, CURLOPT_FTPPORT, "-");
00111 curl_easy_setopt(curlhandle, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);
00112
00113 curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);
00114
00115 for(c = 0; (r != CURLE_OK) && (c < tries); c++) {
00116
00117 if(c) {
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 1L);
00129 curl_easy_setopt(curlhandle, CURLOPT_HEADER, 1L);
00130
00131 r = curl_easy_perform(curlhandle);
00132 if(r != CURLE_OK)
00133 continue;
00134
00135 curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 0L);
00136 curl_easy_setopt(curlhandle, CURLOPT_HEADER, 0L);
00137
00138 fseek(f, uploaded_len, SEEK_SET);
00139
00140 curl_easy_setopt(curlhandle, CURLOPT_APPEND, 1L);
00141 }
00142 else {
00143 curl_easy_setopt(curlhandle, CURLOPT_APPEND, 0L);
00144 }
00145
00146 r = curl_easy_perform(curlhandle);
00147 }
00148
00149 fclose(f);
00150
00151 if(r == CURLE_OK)
00152 return 1;
00153 else {
00154 fprintf(stderr, "%s\n", curl_easy_strerror(r));
00155 return 0;
00156 }
00157 }
00158
00159 int main(int c, char **argv)
00160 {
00161 CURL *curlhandle = NULL;
00162
00163 curl_global_init(CURL_GLOBAL_ALL);
00164 curlhandle = curl_easy_init();
00165
00166 upload(curlhandle, "ftp://user:pass@example.com/path/file", "C:\\file",
00167 0, 3);
00168
00169 curl_easy_cleanup(curlhandle);
00170 curl_global_cleanup();
00171
00172 return 0;
00173 }