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
main(void)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 if(fstat(fileno(fd), &file_info) != 0)
55 return 1; /* cannot continue */
56
57 curl = curl_easy_init();
58 if(curl) {
59 /* upload to this place */
60 curl_easy_setopt(curl, CURLOPT_URL,
61 "file:///home/dast/src/curl/debug/new");
62
63 /* tell it to "upload" to the URL */
64 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
65
66 /* set where to read from (on Windows you need to use READFUNCTION too) */
67 curl_easy_setopt(curl, CURLOPT_READDATA, fd);
68
69 /* and give the size of the upload (optional) */
70 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
71 (curl_off_t)file_info.st_size);
72
73 /* enable verbose for easier tracing */
74 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
75
76 res = curl_easy_perform(curl);
77 /* Check for errors */
78 if(res != CURLE_OK) {
79 fprintf(stderr, "curl_easy_perform() failed: %s\n",
80 curl_easy_strerror(res));
81 }
82 else {
83 /* now extract transfer info */
84 curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD_T, &speed_upload);
85 curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME_T, &total_time);
86
87 fprintf(stderr, "Speed: %lu bytes/sec during %lu.%06lu seconds\n",
88 (unsigned long)speed_upload,
89 (unsigned long)(total_time / 1000000),
90 (unsigned long)(total_time % 1000000));
91 }
92 /* always cleanup */
93 curl_easy_cleanup(curl);
94 }
95 fclose(fd);
96 return 0;
97 }
98