xref: /curl/docs/examples/ftpupload.c (revision 46093d9e)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 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 https://curl.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  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include <curl/curl.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #ifdef _WIN32
33 #include <io.h>
34 #undef stat
35 #define stat _stat
36 #else
37 #include <unistd.h>
38 #endif
39 
40 /* <DESC>
41  * Performs an FTP upload and renames the file just after a successful
42  * transfer.
43  * </DESC>
44  */
45 
46 #define LOCAL_FILE      "/tmp/uploadthis.txt"
47 #define UPLOAD_FILE_AS  "while-uploading.txt"
48 #define REMOTE_URL      "ftp://example.com/"  UPLOAD_FILE_AS
49 #define RENAME_FILE_TO  "renamed-and-fine.txt"
50 
51 /* NOTE: if you want this example to work on Windows with libcurl as a DLL,
52    you MUST also provide a read callback with CURLOPT_READFUNCTION. Failing to
53    do so might give you a crash since a DLL may not use the variable's memory
54    when passed in to it from an app like this. */
read_callback(char * ptr,size_t size,size_t nmemb,void * stream)55 static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream)
56 {
57   unsigned long nread;
58   /* in real-world cases, this would probably get this data differently
59      as this fread() stuff is exactly what the library already would do
60      by default internally */
61   size_t retcode = fread(ptr, size, nmemb, stream);
62 
63   if(retcode > 0) {
64     nread = (unsigned long)retcode;
65     fprintf(stderr, "*** We read %lu bytes from file\n", nread);
66   }
67 
68   return retcode;
69 }
70 
main(void)71 int main(void)
72 {
73   CURL *curl;
74   CURLcode res;
75   FILE *hd_src;
76   struct stat file_info;
77   unsigned long fsize;
78 
79   struct curl_slist *headerlist = NULL;
80   static const char buf_1 [] = "RNFR " UPLOAD_FILE_AS;
81   static const char buf_2 [] = "RNTO " RENAME_FILE_TO;
82 
83   /* get the file size of the local file */
84   if(stat(LOCAL_FILE, &file_info)) {
85     printf("Couldn't open '%s': %s\n", LOCAL_FILE, strerror(errno));
86     return 1;
87   }
88   fsize = (unsigned long)file_info.st_size;
89 
90   printf("Local file size: %lu bytes.\n", fsize);
91 
92   /* get a FILE * of the same file */
93   hd_src = fopen(LOCAL_FILE, "rb");
94 
95   /* In Windows, this inits the Winsock stuff */
96   curl_global_init(CURL_GLOBAL_ALL);
97 
98   /* get a curl handle */
99   curl = curl_easy_init();
100   if(curl) {
101     /* build a list of commands to pass to libcurl */
102     headerlist = curl_slist_append(headerlist, buf_1);
103     headerlist = curl_slist_append(headerlist, buf_2);
104 
105     /* we want to use our own read function */
106     curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
107 
108     /* enable uploading */
109     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
110 
111     /* specify target */
112     curl_easy_setopt(curl, CURLOPT_URL, REMOTE_URL);
113 
114     /* pass in that last of FTP commands to run after the transfer */
115     curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
116 
117     /* now specify which file to upload */
118     curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
119 
120     /* Set the size of the file to upload (optional).  If you give a *_LARGE
121        option you MUST make sure that the type of the passed-in argument is a
122        curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
123        make sure that to pass in a type 'long' argument. */
124     curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
125                      (curl_off_t)fsize);
126 
127     /* Now run off and do what you have been told! */
128     res = curl_easy_perform(curl);
129     /* Check for errors */
130     if(res != CURLE_OK)
131       fprintf(stderr, "curl_easy_perform() failed: %s\n",
132               curl_easy_strerror(res));
133 
134     /* clean up the FTP commands list */
135     curl_slist_free_all(headerlist);
136 
137     /* always cleanup */
138     curl_easy_cleanup(curl);
139   }
140   fclose(hd_src); /* close the local file */
141 
142   curl_global_cleanup();
143   return 0;
144 }
145