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