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