2
0

progressfunc.c 3.1 KB

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