113498266Sopenharmony_ci/*************************************************************************** 213498266Sopenharmony_ci * _ _ ____ _ 313498266Sopenharmony_ci * Project ___| | | | _ \| | 413498266Sopenharmony_ci * / __| | | | |_) | | 513498266Sopenharmony_ci * | (__| |_| | _ <| |___ 613498266Sopenharmony_ci * \___|\___/|_| \_\_____| 713498266Sopenharmony_ci * 813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 913498266Sopenharmony_ci * 1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which 1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms 1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html. 1313498266Sopenharmony_ci * 1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell 1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is 1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file. 1713498266Sopenharmony_ci * 1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 1913498266Sopenharmony_ci * KIND, either express or implied. 2013498266Sopenharmony_ci * 2113498266Sopenharmony_ci * SPDX-License-Identifier: curl 2213498266Sopenharmony_ci * 2313498266Sopenharmony_ci ***************************************************************************/ 2413498266Sopenharmony_ci#include <stdio.h> 2513498266Sopenharmony_ci#include <string.h> 2613498266Sopenharmony_ci 2713498266Sopenharmony_ci#include <curl/curl.h> 2813498266Sopenharmony_ci#include <sys/types.h> 2913498266Sopenharmony_ci#include <sys/stat.h> 3013498266Sopenharmony_ci#include <fcntl.h> 3113498266Sopenharmony_ci#include <errno.h> 3213498266Sopenharmony_ci#ifdef _WIN32 3313498266Sopenharmony_ci#include <io.h> 3413498266Sopenharmony_ci#else 3513498266Sopenharmony_ci#include <unistd.h> 3613498266Sopenharmony_ci#endif 3713498266Sopenharmony_ci 3813498266Sopenharmony_ci/* <DESC> 3913498266Sopenharmony_ci * Performs an FTP upload and renames the file just after a successful 4013498266Sopenharmony_ci * transfer. 4113498266Sopenharmony_ci * </DESC> 4213498266Sopenharmony_ci */ 4313498266Sopenharmony_ci 4413498266Sopenharmony_ci#define LOCAL_FILE "/tmp/uploadthis.txt" 4513498266Sopenharmony_ci#define UPLOAD_FILE_AS "while-uploading.txt" 4613498266Sopenharmony_ci#define REMOTE_URL "ftp://example.com/" UPLOAD_FILE_AS 4713498266Sopenharmony_ci#define RENAME_FILE_TO "renamed-and-fine.txt" 4813498266Sopenharmony_ci 4913498266Sopenharmony_ci/* NOTE: if you want this example to work on Windows with libcurl as a 5013498266Sopenharmony_ci DLL, you MUST also provide a read callback with CURLOPT_READFUNCTION. 5113498266Sopenharmony_ci Failing to do so will give you a crash since a DLL may not use the 5213498266Sopenharmony_ci variable's memory when passed in to it from an app like this. */ 5313498266Sopenharmony_cistatic size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream) 5413498266Sopenharmony_ci{ 5513498266Sopenharmony_ci unsigned long nread; 5613498266Sopenharmony_ci /* in real-world cases, this would probably get this data differently 5713498266Sopenharmony_ci as this fread() stuff is exactly what the library already would do 5813498266Sopenharmony_ci by default internally */ 5913498266Sopenharmony_ci size_t retcode = fread(ptr, size, nmemb, stream); 6013498266Sopenharmony_ci 6113498266Sopenharmony_ci if(retcode > 0) { 6213498266Sopenharmony_ci nread = (unsigned long)retcode; 6313498266Sopenharmony_ci fprintf(stderr, "*** We read %lu bytes from file\n", nread); 6413498266Sopenharmony_ci } 6513498266Sopenharmony_ci 6613498266Sopenharmony_ci return retcode; 6713498266Sopenharmony_ci} 6813498266Sopenharmony_ci 6913498266Sopenharmony_ciint main(void) 7013498266Sopenharmony_ci{ 7113498266Sopenharmony_ci CURL *curl; 7213498266Sopenharmony_ci CURLcode res; 7313498266Sopenharmony_ci FILE *hd_src; 7413498266Sopenharmony_ci struct stat file_info; 7513498266Sopenharmony_ci unsigned long fsize; 7613498266Sopenharmony_ci 7713498266Sopenharmony_ci struct curl_slist *headerlist = NULL; 7813498266Sopenharmony_ci static const char buf_1 [] = "RNFR " UPLOAD_FILE_AS; 7913498266Sopenharmony_ci static const char buf_2 [] = "RNTO " RENAME_FILE_TO; 8013498266Sopenharmony_ci 8113498266Sopenharmony_ci /* get the file size of the local file */ 8213498266Sopenharmony_ci if(stat(LOCAL_FILE, &file_info)) { 8313498266Sopenharmony_ci printf("Couldn't open '%s': %s\n", LOCAL_FILE, strerror(errno)); 8413498266Sopenharmony_ci return 1; 8513498266Sopenharmony_ci } 8613498266Sopenharmony_ci fsize = (unsigned long)file_info.st_size; 8713498266Sopenharmony_ci 8813498266Sopenharmony_ci printf("Local file size: %lu bytes.\n", fsize); 8913498266Sopenharmony_ci 9013498266Sopenharmony_ci /* get a FILE * of the same file */ 9113498266Sopenharmony_ci hd_src = fopen(LOCAL_FILE, "rb"); 9213498266Sopenharmony_ci 9313498266Sopenharmony_ci /* In windows, this will init the winsock stuff */ 9413498266Sopenharmony_ci curl_global_init(CURL_GLOBAL_ALL); 9513498266Sopenharmony_ci 9613498266Sopenharmony_ci /* get a curl handle */ 9713498266Sopenharmony_ci curl = curl_easy_init(); 9813498266Sopenharmony_ci if(curl) { 9913498266Sopenharmony_ci /* build a list of commands to pass to libcurl */ 10013498266Sopenharmony_ci headerlist = curl_slist_append(headerlist, buf_1); 10113498266Sopenharmony_ci headerlist = curl_slist_append(headerlist, buf_2); 10213498266Sopenharmony_ci 10313498266Sopenharmony_ci /* we want to use our own read function */ 10413498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback); 10513498266Sopenharmony_ci 10613498266Sopenharmony_ci /* enable uploading */ 10713498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); 10813498266Sopenharmony_ci 10913498266Sopenharmony_ci /* specify target */ 11013498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_URL, REMOTE_URL); 11113498266Sopenharmony_ci 11213498266Sopenharmony_ci /* pass in that last of FTP commands to run after the transfer */ 11313498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist); 11413498266Sopenharmony_ci 11513498266Sopenharmony_ci /* now specify which file to upload */ 11613498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_READDATA, hd_src); 11713498266Sopenharmony_ci 11813498266Sopenharmony_ci /* Set the size of the file to upload (optional). If you give a *_LARGE 11913498266Sopenharmony_ci option you MUST make sure that the type of the passed-in argument is a 12013498266Sopenharmony_ci curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must 12113498266Sopenharmony_ci make sure that to pass in a type 'long' argument. */ 12213498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, 12313498266Sopenharmony_ci (curl_off_t)fsize); 12413498266Sopenharmony_ci 12513498266Sopenharmony_ci /* Now run off and do what you have been told! */ 12613498266Sopenharmony_ci res = curl_easy_perform(curl); 12713498266Sopenharmony_ci /* Check for errors */ 12813498266Sopenharmony_ci if(res != CURLE_OK) 12913498266Sopenharmony_ci fprintf(stderr, "curl_easy_perform() failed: %s\n", 13013498266Sopenharmony_ci curl_easy_strerror(res)); 13113498266Sopenharmony_ci 13213498266Sopenharmony_ci /* clean up the FTP commands list */ 13313498266Sopenharmony_ci curl_slist_free_all(headerlist); 13413498266Sopenharmony_ci 13513498266Sopenharmony_ci /* always cleanup */ 13613498266Sopenharmony_ci curl_easy_cleanup(curl); 13713498266Sopenharmony_ci } 13813498266Sopenharmony_ci fclose(hd_src); /* close the local file */ 13913498266Sopenharmony_ci 14013498266Sopenharmony_ci curl_global_cleanup(); 14113498266Sopenharmony_ci return 0; 14213498266Sopenharmony_ci} 143