12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- #include <stdio.h>
- #include <curl/curl.h>
- #define MINIMAL_PROGRESS_FUNCTIONALITY_INTERVAL 3000000
- #define STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES 6000
- struct myprogress {
- curl_off_t lastruntime;
- CURL *curl;
- };
- static int xferinfo(void *p,
- curl_off_t dltotal, curl_off_t dlnow,
- curl_off_t ultotal, curl_off_t ulnow)
- {
- struct myprogress *myp = (struct myprogress *)p;
- CURL *curl = myp->curl;
- curl_off_t curtime = 0;
- curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME_T, &curtime);
-
- if((curtime - myp->lastruntime) >= MINIMAL_PROGRESS_FUNCTIONALITY_INTERVAL) {
- myp->lastruntime = curtime;
- fprintf(stderr, "TOTAL TIME: %lu.%06lu\r\n",
- (unsigned long)(curtime / 1000000),
- (unsigned long)(curtime % 1000000));
- }
- fprintf(stderr, "UP: %lu of %lu DOWN: %lu of %lu\r\n",
- (unsigned long)ulnow, (unsigned long)ultotal,
- (unsigned long)dlnow, (unsigned long)dltotal);
- if(dlnow > STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES)
- return 1;
- return 0;
- }
- int main(void)
- {
- CURL *curl;
- CURLcode res = CURLE_OK;
- struct myprogress prog;
- curl = curl_easy_init();
- if(curl) {
- prog.lastruntime = 0;
- prog.curl = curl;
- curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
- curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, xferinfo);
-
- curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &prog);
- curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
- res = curl_easy_perform(curl);
- if(res != CURLE_OK)
- fprintf(stderr, "%s\n", curl_easy_strerror(res));
-
- curl_easy_cleanup(curl);
- }
- return (int)res;
- }
|