fileupload.c 2.8 KB

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