• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /***************************************************************************
2   *                                  _   _ ____  _
3   *  Project                     ___| | | |  _ \| |
4   *                             / __| | | | |_) | |
5   *                            | (__| |_| |  _ <| |___
6   *                             \___|\___/|_| \_\_____|
7   *
8   * Copyright (C) 1998 - 2011, 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 http://curl.haxx.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   ***************************************************************************/
22  #include <stdio.h>
23  #include <curl/curl.h>
24  #include <sys/stat.h>
25  #include <fcntl.h>
26  
main(void)27  int main(void)
28  {
29    CURL *curl;
30    CURLcode res;
31    struct stat file_info;
32    double speed_upload, total_time;
33    FILE *fd;
34  
35    fd = fopen("debugit", "rb"); /* open file to upload */
36    if(!fd) {
37  
38      return 1; /* can't continue */
39    }
40  
41    /* to get the file size */
42    if(fstat(fileno(fd), &file_info) != 0) {
43  
44      return 1; /* can't continue */
45    }
46  
47    curl = curl_easy_init();
48    if(curl) {
49      /* upload to this place */
50      curl_easy_setopt(curl, CURLOPT_URL,
51                       "file:///home/dast/src/curl/debug/new");
52  
53      /* tell it to "upload" to the URL */
54      curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
55  
56      /* set where to read from (on Windows you need to use READFUNCTION too) */
57      curl_easy_setopt(curl, CURLOPT_READDATA, fd);
58  
59      /* and give the size of the upload (optional) */
60      curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
61                       (curl_off_t)file_info.st_size);
62  
63      /* enable verbose for easier tracing */
64      curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
65  
66      res = curl_easy_perform(curl);
67      /* Check for errors */
68      if(res != CURLE_OK) {
69        fprintf(stderr, "curl_easy_perform() failed: %s\n",
70                curl_easy_strerror(res));
71  
72      }
73      else {
74        /* now extract transfer info */
75        curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
76        curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);
77  
78        fprintf(stderr, "Speed: %.3f bytes/sec during %.3f seconds\n",
79                speed_upload, total_time);
80  
81      }
82      /* always cleanup */
83      curl_easy_cleanup(curl);
84    }
85    return 0;
86  }
87