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