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