fileupload.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #ifdef _WIN32
  33. #undef stat
  34. #define stat _stat
  35. #undef fstat
  36. #define fstat _fstat
  37. #define fileno _fileno
  38. #endif
  39. int main(void)
  40. {
  41. CURL *curl;
  42. CURLcode res;
  43. struct stat file_info;
  44. curl_off_t speed_upload, total_time;
  45. FILE *fd;
  46. fd = fopen("debugit", "rb"); /* open file to upload */
  47. if(!fd)
  48. return 1; /* cannot continue */
  49. /* to get the file size */
  50. if(fstat(fileno(fd), &file_info) != 0)
  51. return 1; /* cannot continue */
  52. curl = curl_easy_init();
  53. if(curl) {
  54. /* upload to this place */
  55. curl_easy_setopt(curl, CURLOPT_URL,
  56. "file:///home/dast/src/curl/debug/new");
  57. /* tell it to "upload" to the URL */
  58. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  59. /* set where to read from (on Windows you need to use READFUNCTION too) */
  60. curl_easy_setopt(curl, CURLOPT_READDATA, fd);
  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. /* enable verbose for easier tracing */
  65. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  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. else {
  73. /* now extract transfer info */
  74. curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD_T, &speed_upload);
  75. curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME_T, &total_time);
  76. fprintf(stderr, "Speed: %lu bytes/sec during %lu.%06lu seconds\n",
  77. (unsigned long)speed_upload,
  78. (unsigned long)(total_time / 1000000),
  79. (unsigned long)(total_time % 1000000));
  80. }
  81. /* always cleanup */
  82. curl_easy_cleanup(curl);
  83. }
  84. fclose(fd);
  85. return 0;
  86. }