progressfunc.c 3.1 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. * Use the progress callbacks, old and/or new one depending on available
  26. * libcurl version.
  27. * </DESC>
  28. */
  29. #include <stdio.h>
  30. #include <curl/curl.h>
  31. #define MINIMAL_PROGRESS_FUNCTIONALITY_INTERVAL 3000000
  32. #define STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES 6000
  33. struct myprogress {
  34. curl_off_t lastruntime; /* type depends on version, see above */
  35. CURL *curl;
  36. };
  37. /* this is how the CURLOPT_XFERINFOFUNCTION callback works */
  38. static int xferinfo(void *p,
  39. curl_off_t dltotal, curl_off_t dlnow,
  40. curl_off_t ultotal, curl_off_t ulnow)
  41. {
  42. struct myprogress *myp = (struct myprogress *)p;
  43. CURL *curl = myp->curl;
  44. curl_off_t curtime = 0;
  45. curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME_T, &curtime);
  46. /* under certain circumstances it may be desirable for certain functionality
  47. to only run every N seconds, in order to do this the transaction time can
  48. be used */
  49. if((curtime - myp->lastruntime) >= MINIMAL_PROGRESS_FUNCTIONALITY_INTERVAL) {
  50. myp->lastruntime = curtime;
  51. fprintf(stderr, "TOTAL TIME: %lu.%06lu\r\n",
  52. (unsigned long)(curtime / 1000000),
  53. (unsigned long)(curtime % 1000000));
  54. }
  55. fprintf(stderr, "UP: %lu of %lu DOWN: %lu of %lu\r\n",
  56. (unsigned long)ulnow, (unsigned long)ultotal,
  57. (unsigned long)dlnow, (unsigned long)dltotal);
  58. if(dlnow > STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES)
  59. return 1;
  60. return 0;
  61. }
  62. int main(void)
  63. {
  64. CURL *curl;
  65. CURLcode res = CURLE_OK;
  66. struct myprogress prog;
  67. curl = curl_easy_init();
  68. if(curl) {
  69. prog.lastruntime = 0;
  70. prog.curl = curl;
  71. curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
  72. curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, xferinfo);
  73. /* pass the struct pointer into the xferinfo function */
  74. curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &prog);
  75. curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
  76. res = curl_easy_perform(curl);
  77. if(res != CURLE_OK)
  78. fprintf(stderr, "%s\n", curl_easy_strerror(res));
  79. /* always cleanup */
  80. curl_easy_cleanup(curl);
  81. }
  82. return (int)res;
  83. }