Git init
[external/curl.git] / docs / examples / ftpupload.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12
13 #include <curl/curl.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <errno.h>
18 #ifdef WIN32
19 #include <io.h>
20 #else
21 #include <unistd.h>
22 #endif
23
24 /*
25  * This example shows an FTP upload, with a rename of the file just after
26  * a successful upload.
27  *
28  * Example based on source code provided by Erick Nuwendam. Thanks!
29  */
30
31 #define LOCAL_FILE      "/tmp/uploadthis.txt"
32 #define UPLOAD_FILE_AS  "while-uploading.txt"
33 #define REMOTE_URL      "ftp://example.com/"  UPLOAD_FILE_AS
34 #define RENAME_FILE_TO  "renamed-and-fine.txt"
35
36 /* NOTE: if you want this example to work on Windows with libcurl as a
37    DLL, you MUST also provide a read callback with CURLOPT_READFUNCTION.
38    Failing to do so will give you a crash since a DLL may not use the
39    variable's memory when passed in to it from an app like this. */
40 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
41 {
42   /* in real-world cases, this would probably get this data differently
43      as this fread() stuff is exactly what the library already would do
44      by default internally */
45   size_t retcode = fread(ptr, size, nmemb, stream);
46
47   fprintf(stderr, "*** We read %d bytes from file\n", retcode);
48   return retcode;
49 }
50
51 int main(int argc, char **argv)
52 {
53   CURL *curl;
54   CURLcode res;
55   FILE *hd_src;
56   struct stat file_info;
57   curl_off_t fsize;
58
59   struct curl_slist *headerlist=NULL;
60   static const char buf_1 [] = "RNFR " UPLOAD_FILE_AS;
61   static const char buf_2 [] = "RNTO " RENAME_FILE_TO;
62
63   /* get the file size of the local file */
64   if(stat(LOCAL_FILE, &file_info)) {
65     printf("Couldnt open '%s': %s\n", LOCAL_FILE, strerror(errno));
66     return 1;
67   }
68   fsize = (curl_off_t)file_info.st_size;
69
70   printf("Local file size: %" CURL_FORMAT_CURL_OFF_T " bytes.\n", fsize);
71
72   /* get a FILE * of the same file */
73   hd_src = fopen(LOCAL_FILE, "rb");
74
75   /* In windows, this will init the winsock stuff */
76   curl_global_init(CURL_GLOBAL_ALL);
77
78   /* get a curl handle */
79   curl = curl_easy_init();
80   if(curl) {
81     /* build a list of commands to pass to libcurl */
82     headerlist = curl_slist_append(headerlist, buf_1);
83     headerlist = curl_slist_append(headerlist, buf_2);
84
85     /* we want to use our own read function */
86     curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
87
88     /* enable uploading */
89     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
90
91     /* specify target */
92     curl_easy_setopt(curl,CURLOPT_URL, REMOTE_URL);
93
94     /* pass in that last of FTP commands to run after the transfer */
95     curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
96
97     /* now specify which file to upload */
98     curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
99
100     /* Set the size of the file to upload (optional).  If you give a *_LARGE
101        option you MUST make sure that the type of the passed-in argument is a
102        curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
103        make sure that to pass in a type 'long' argument. */
104     curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
105                      (curl_off_t)fsize);
106
107     /* Now run off and do what you've been told! */
108     res = curl_easy_perform(curl);
109
110     /* clean up the FTP commands list */
111     curl_slist_free_all (headerlist);
112
113     /* always cleanup */
114     curl_easy_cleanup(curl);
115   }
116   fclose(hd_src); /* close the local file */
117
118   curl_global_cleanup();
119   return 0;
120 }