Git init
[external/curl.git] / docs / examples / ftpuploadresume.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  *
9  * Upload to FTP, resuming failed transfers
10  *
11  * Compile for MinGW like this:
12  *  gcc -Wall -pedantic -std=c99 ftpuploadwithresume.c -o ftpuploadresume.exe
13  *  -lcurl -lmsvcr70
14  *
15  * Written by Philip Bock
16  */
17
18 #include <stdlib.h>
19 #include <stdio.h>
20
21 #include <curl/curl.h>
22
23 #if defined(_MSC_VER) && (_MSC_VER < 1300)
24 #  error _snscanf requires MSVC 7.0 or later.
25 #endif
26
27 /* The MinGW headers are missing a few Win32 function definitions,
28    you shouldn't need this if you use VC++ */
29 #ifdef __MINGW32__
30 int __cdecl _snscanf(const char * input, size_t length, const char * format, ...);
31 #endif
32
33
34 /* parse headers for Content-Length */
35 size_t getcontentlengthfunc(void *ptr, size_t size, size_t nmemb, void *stream)
36 {
37   int r;
38   long len = 0;
39
40   /* _snscanf() is Win32 specific */
41   r = _snscanf(ptr, size * nmemb, "Content-Length: %ld\n", &len);
42
43   if (r) /* Microsoft: we don't read the specs */
44     *((long *) stream) = len;
45
46   return size * nmemb;
47 }
48
49 /* discard downloaded data */
50 size_t discardfunc(void *ptr, size_t size, size_t nmemb, void *stream)
51 {
52   return size * nmemb;
53 }
54
55 /* read data to upload */
56 size_t readfunc(void *ptr, size_t size, size_t nmemb, void *stream)
57 {
58   FILE *f = stream;
59   size_t n;
60
61   if (ferror(f))
62     return CURL_READFUNC_ABORT;
63
64   n = fread(ptr, size, nmemb, f) * size;
65
66   return n;
67 }
68
69
70 int upload(CURL *curlhandle, const char * remotepath, const char * localpath,
71            long timeout, long tries)
72 {
73   FILE *f;
74   long uploaded_len = 0;
75   CURLcode r = CURLE_GOT_NOTHING;
76   int c;
77
78   f = fopen(localpath, "rb");
79   if (f == NULL) {
80     perror(NULL);
81     return 0;
82   }
83
84   curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
85
86   curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
87
88   if (timeout)
89     curl_easy_setopt(curlhandle, CURLOPT_FTP_RESPONSE_TIMEOUT, timeout);
90
91   curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);
92   curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &uploaded_len);
93
94   curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, discardfunc);
95
96   curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);
97   curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);
98
99   curl_easy_setopt(curlhandle, CURLOPT_FTPPORT, "-"); /* disable passive mode */
100   curl_easy_setopt(curlhandle, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);
101
102   curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);
103
104   for (c = 0; (r != CURLE_OK) && (c < tries); c++) {
105     /* are we resuming? */
106     if (c) { /* yes */
107       /* determine the length of the file already written */
108
109       /*
110        * With NOBODY and NOHEADER, libcurl will issue a SIZE
111        * command, but the only way to retrieve the result is
112        * to parse the returned Content-Length header. Thus,
113        * getcontentlengthfunc(). We need discardfunc() above
114        * because HEADER will dump the headers to stdout
115        * without it.
116        */
117       curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 1L);
118       curl_easy_setopt(curlhandle, CURLOPT_HEADER, 1L);
119
120       r = curl_easy_perform(curlhandle);
121       if (r != CURLE_OK)
122         continue;
123
124       curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 0L);
125       curl_easy_setopt(curlhandle, CURLOPT_HEADER, 0L);
126
127       fseek(f, uploaded_len, SEEK_SET);
128
129       curl_easy_setopt(curlhandle, CURLOPT_APPEND, 1L);
130     }
131     else { /* no */
132       curl_easy_setopt(curlhandle, CURLOPT_APPEND, 0L);
133     }
134
135     r = curl_easy_perform(curlhandle);
136   }
137
138   fclose(f);
139
140   if (r == CURLE_OK)
141     return 1;
142   else {
143     fprintf(stderr, "%s\n", curl_easy_strerror(r));
144     return 0;
145   }
146 }
147
148 int main(int c, char **argv)
149 {
150   CURL *curlhandle = NULL;
151
152   curl_global_init(CURL_GLOBAL_ALL);
153   curlhandle = curl_easy_init();
154
155   upload(curlhandle, "ftp://user:pass@example.com/path/file", "C:\\file", 0, 3);
156
157   curl_easy_cleanup(curlhandle);
158   curl_global_cleanup();
159
160   return 0;
161 }