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