fileupload.c (3060B)
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 /* <DESC> 25 * Upload to a file:// URL 26 * </DESC> 27 */ 28 #include <stdio.h> 29 #include <curl/curl.h> 30 #include <sys/stat.h> 31 #include <fcntl.h> 32 33 #ifdef _WIN32 34 #undef stat 35 #define stat _stat 36 #undef fstat 37 #define fstat _fstat 38 #define fileno _fileno 39 #endif 40 41 int main(void) 42 { 43 CURL *curl; 44 CURLcode res; 45 struct stat file_info; 46 curl_off_t speed_upload, total_time; 47 FILE *fd; 48 49 fd = fopen("debugit", "rb"); /* open file to upload */ 50 if(!fd) 51 return 1; /* cannot continue */ 52 53 /* to get the file size */ 54 #ifdef UNDER_CE 55 if(stat("debugit", &file_info) != 0) { 56 #else 57 if(fstat(fileno(fd), &file_info) != 0) { 58 #endif 59 fclose(fd); 60 return 1; /* cannot continue */ 61 } 62 63 curl = curl_easy_init(); 64 if(curl) { 65 /* upload to this place */ 66 curl_easy_setopt(curl, CURLOPT_URL, 67 "file:///home/dast/src/curl/debug/new"); 68 69 /* tell it to "upload" to the URL */ 70 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); 71 72 /* set where to read from (on Windows you need to use READFUNCTION too) */ 73 curl_easy_setopt(curl, CURLOPT_READDATA, fd); 74 75 /* and give the size of the upload (optional) */ 76 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, 77 (curl_off_t)file_info.st_size); 78 79 /* enable verbose for easier tracing */ 80 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); 81 82 res = curl_easy_perform(curl); 83 /* Check for errors */ 84 if(res != CURLE_OK) { 85 fprintf(stderr, "curl_easy_perform() failed: %s\n", 86 curl_easy_strerror(res)); 87 } 88 else { 89 /* now extract transfer info */ 90 curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD_T, &speed_upload); 91 curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME_T, &total_time); 92 93 fprintf(stderr, "Speed: %lu bytes/sec during %lu.%06lu seconds\n", 94 (unsigned long)speed_upload, 95 (unsigned long)(total_time / 1000000), 96 (unsigned long)(total_time % 1000000)); 97 } 98 /* always cleanup */ 99 curl_easy_cleanup(curl); 100 } 101 fclose(fd); 102 return 0; 103 }