1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
24 #ifdef HAVE_SYS_SOCKET_H
25 #include <sys/socket.h>
27 #ifdef HAVE_SYS_TYPES_H
28 #include <sys/types.h>
30 #ifdef HAVE_SYS_STAT_H
44 * This example shows an FTP upload, with a rename of the file just after
45 * a successful upload.
47 * Example based on source code provided by Erick Nuwendam. Thanks!
53 CURLcode res = CURLE_OK;
56 struct_stat file_info;
57 struct curl_slist *hl;
60 struct curl_slist *headerlist=NULL;
61 const char *buf_1 = "RNFR 505";
62 const char *buf_2 = "RNTO 505-forreal";
65 fprintf(stderr, "Usage: <url> <file-to-upload>\n");
69 hd_src = fopen(libtest_arg2, "rb");
72 fprintf(stderr, "fopen() failed with error: %d %s\n",
73 error, strerror(error));
74 fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
75 return -2; /* if this happens things are major weird */
78 /* get the file size of the local file */
79 hd = fstat(fileno(hd_src), &file_info);
81 /* can't open file, bail out */
83 fprintf(stderr, "fstat() failed with error: %d %s\n",
84 error, strerror(error));
85 fprintf(stderr, "ERROR: cannot open file %s\n", libtest_arg2);
90 if(! file_info.st_size) {
91 fprintf(stderr, "ERROR: file %s has zero size!\n", libtest_arg2);
96 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
97 fprintf(stderr, "curl_global_init() failed\n");
99 return TEST_ERR_MAJOR_BAD;
102 /* get a curl handle */
103 if ((curl = curl_easy_init()) == NULL) {
104 fprintf(stderr, "curl_easy_init() failed\n");
105 curl_global_cleanup();
107 return TEST_ERR_MAJOR_BAD;
110 /* build a list of commands to pass to libcurl */
112 if ((hl = curl_slist_append(headerlist, buf_1)) == NULL) {
113 fprintf(stderr, "curl_slist_append() failed\n");
114 curl_easy_cleanup(curl);
115 curl_global_cleanup();
117 return TEST_ERR_MAJOR_BAD;
119 if ((headerlist = curl_slist_append(hl, buf_2)) == NULL) {
120 fprintf(stderr, "curl_slist_append() failed\n");
121 curl_slist_free_all(hl);
122 curl_easy_cleanup(curl);
123 curl_global_cleanup();
125 return TEST_ERR_MAJOR_BAD;
129 /* enable uploading */
130 test_setopt(curl, CURLOPT_UPLOAD, 1L);
133 test_setopt(curl, CURLOPT_VERBOSE, 1L);
136 test_setopt(curl,CURLOPT_URL, URL);
138 /* pass in that last of FTP commands to run after the transfer */
139 test_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
141 /* now specify which file to upload */
142 test_setopt(curl, CURLOPT_INFILE, hd_src);
144 /* and give the size of the upload (optional) */
145 test_setopt(curl, CURLOPT_INFILESIZE_LARGE,
146 (curl_off_t)file_info.st_size);
148 /* Now run off and do what you've been told! */
149 res = curl_easy_perform(curl);
153 /* clean up the FTP commands list */
154 curl_slist_free_all(headerlist);
156 /* close the local file */
159 curl_easy_cleanup(curl);
160 curl_global_cleanup();