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 "test.h" 25 26#ifdef HAVE_FCNTL_H 27#include <fcntl.h> 28#endif 29 30#include "memdebug.h" 31 32/* 33 * This example shows an FTP upload, with a rename of the file just after 34 * a successful upload. 35 * 36 * Example based on source code provided by Erick Nuwendam. Thanks! 37 */ 38 39int test(char *URL) 40{ 41 CURL *curl; 42 CURLcode res = CURLE_OK; 43 FILE *hd_src; 44 int hd; 45 struct_stat file_info; 46 struct curl_slist *hl; 47 48 struct curl_slist *headerlist = NULL; 49 const char *buf_1 = "RNFR 505"; 50 const char *buf_2 = "RNTO 505-forreal"; 51 52 if(!libtest_arg2) { 53 fprintf(stderr, "Usage: <url> <file-to-upload>\n"); 54 return TEST_ERR_USAGE; 55 } 56 57 hd_src = fopen(libtest_arg2, "rb"); 58 if(!hd_src) { 59 fprintf(stderr, "fopen failed with error: %d %s\n", 60 errno, strerror(errno)); 61 fprintf(stderr, "Error opening file: %s\n", libtest_arg2); 62 return TEST_ERR_MAJOR_BAD; /* if this happens things are major weird */ 63 } 64 65 /* get the file size of the local file */ 66 hd = fstat(fileno(hd_src), &file_info); 67 if(hd == -1) { 68 /* can't open file, bail out */ 69 fprintf(stderr, "fstat() failed with error: %d %s\n", 70 errno, strerror(errno)); 71 fprintf(stderr, "ERROR: cannot open file %s\n", libtest_arg2); 72 fclose(hd_src); 73 return TEST_ERR_MAJOR_BAD; 74 } 75 76 if(!file_info.st_size) { 77 fprintf(stderr, "ERROR: file %s has zero size!\n", libtest_arg2); 78 fclose(hd_src); 79 return TEST_ERR_MAJOR_BAD; 80 } 81 82 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) { 83 fprintf(stderr, "curl_global_init() failed\n"); 84 fclose(hd_src); 85 return TEST_ERR_MAJOR_BAD; 86 } 87 88 /* get a curl handle */ 89 curl = curl_easy_init(); 90 if(!curl) { 91 fprintf(stderr, "curl_easy_init() failed\n"); 92 curl_global_cleanup(); 93 fclose(hd_src); 94 return TEST_ERR_MAJOR_BAD; 95 } 96 97 /* build a list of commands to pass to libcurl */ 98 99 hl = curl_slist_append(headerlist, buf_1); 100 if(!hl) { 101 fprintf(stderr, "curl_slist_append() failed\n"); 102 curl_easy_cleanup(curl); 103 curl_global_cleanup(); 104 fclose(hd_src); 105 return TEST_ERR_MAJOR_BAD; 106 } 107 headerlist = curl_slist_append(hl, buf_2); 108 if(!headerlist) { 109 fprintf(stderr, "curl_slist_append() failed\n"); 110 curl_slist_free_all(hl); 111 curl_easy_cleanup(curl); 112 curl_global_cleanup(); 113 fclose(hd_src); 114 return TEST_ERR_MAJOR_BAD; 115 } 116 headerlist = hl; 117 118 /* enable uploading */ 119 test_setopt(curl, CURLOPT_UPLOAD, 1L); 120 121 /* enable verbose */ 122 test_setopt(curl, CURLOPT_VERBOSE, 1L); 123 124 /* specify target */ 125 test_setopt(curl, CURLOPT_URL, URL); 126 127 /* pass in that last of FTP commands to run after the transfer */ 128 test_setopt(curl, CURLOPT_POSTQUOTE, headerlist); 129 130 /* now specify which file to upload */ 131 test_setopt(curl, CURLOPT_READDATA, hd_src); 132 133 /* and give the size of the upload (optional) */ 134 test_setopt(curl, CURLOPT_INFILESIZE_LARGE, 135 (curl_off_t)file_info.st_size); 136 137 /* Now run off and do what you've been told! */ 138 res = curl_easy_perform(curl); 139 140test_cleanup: 141 142 /* clean up the FTP commands list */ 143 curl_slist_free_all(headerlist); 144 145 /* close the local file */ 146 fclose(hd_src); 147 148 curl_easy_cleanup(curl); 149 curl_global_cleanup(); 150 151 return res; 152} 153