• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
main(void)33 int main(void)
34 {
35   CURL *curl;
36   CURLcode res;
37   struct stat file_info;
38   curl_off_t speed_upload, total_time;
39   FILE *fd;
40 
41   fd = fopen("debugit", "rb"); /* open file to upload */
42   if(!fd)
43     return 1; /* cannot continue */
44 
45   /* to get the file size */
46   if(fstat(fileno(fd), &file_info) != 0)
47     return 1; /* cannot continue */
48 
49   curl = curl_easy_init();
50   if(curl) {
51     /* upload to this place */
52     curl_easy_setopt(curl, CURLOPT_URL,
53                      "file:///home/dast/src/curl/debug/new");
54 
55     /* tell it to "upload" to the URL */
56     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
57 
58     /* set where to read from (on Windows you need to use READFUNCTION too) */
59     curl_easy_setopt(curl, CURLOPT_READDATA, fd);
60 
61     /* and give the size of the upload (optional) */
62     curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
63                      (curl_off_t)file_info.st_size);
64 
65     /* enable verbose for easier tracing */
66     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
67 
68     res = curl_easy_perform(curl);
69     /* Check for errors */
70     if(res != CURLE_OK) {
71       fprintf(stderr, "curl_easy_perform() failed: %s\n",
72               curl_easy_strerror(res));
73     }
74     else {
75       /* now extract transfer info */
76       curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD_T, &speed_upload);
77       curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME_T, &total_time);
78 
79       fprintf(stderr, "Speed: %lu bytes/sec during %lu.%06lu seconds\n",
80               (unsigned long)speed_upload,
81               (unsigned long)(total_time / 1000000),
82               (unsigned long)(total_time % 1000000));
83     }
84     /* always cleanup */
85     curl_easy_cleanup(curl);
86   }
87   fclose(fd);
88   return 0;
89 }
90