1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
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.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 * Use the progress callbacks, old and/or new one depending on available
28 #include <curl/curl.h>
30 #define STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES 6000
31 #define MINIMAL_PROGRESS_FUNCTIONALITY_INTERVAL 3
38 /* this is how the CURLOPT_XFERINFOFUNCTION callback works */
39 static int xferinfo(void *p,
40 curl_off_t dltotal, curl_off_t dlnow,
41 curl_off_t ultotal, curl_off_t ulnow)
43 struct myprogress *myp = (struct myprogress *)p;
44 CURL *curl = myp->curl;
47 curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &curtime);
49 /* under certain circumstances it may be desirable for certain functionality
50 to only run every N seconds, in order to do this the transaction time can
52 if((curtime - myp->lastruntime) >= MINIMAL_PROGRESS_FUNCTIONALITY_INTERVAL) {
53 myp->lastruntime = curtime;
54 fprintf(stderr, "TOTAL TIME: %f \r\n", curtime);
57 fprintf(stderr, "UP: %" CURL_FORMAT_CURL_OFF_T " of %" CURL_FORMAT_CURL_OFF_T
58 " DOWN: %" CURL_FORMAT_CURL_OFF_T " of %" CURL_FORMAT_CURL_OFF_T
60 ulnow, ultotal, dlnow, dltotal);
62 if(dlnow > STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES)
67 /* for libcurl older than 7.32.0 (CURLOPT_PROGRESSFUNCTION) */
68 static int older_progress(void *p,
69 double dltotal, double dlnow,
70 double ultotal, double ulnow)
83 CURLcode res = CURLE_OK;
84 struct myprogress prog;
86 curl = curl_easy_init();
91 curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/");
93 curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, older_progress);
94 /* pass the struct pointer into the progress function */
95 curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, &prog);
97 #if LIBCURL_VERSION_NUM >= 0x072000
98 /* xferinfo was introduced in 7.32.0, no earlier libcurl versions will
99 compile as they won't have the symbols around.
101 If built with a newer libcurl, but running with an older libcurl:
102 curl_easy_setopt() will fail in run-time trying to set the new
103 callback, making the older callback get used.
105 New libcurls will prefer the new callback and instead use that one even
106 if both callbacks are set. */
108 curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, xferinfo);
109 /* pass the struct pointer into the xferinfo function, note that this is
110 an alias to CURLOPT_PROGRESSDATA */
111 curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &prog);
114 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
115 res = curl_easy_perform(curl);
118 fprintf(stderr, "%s\n", curl_easy_strerror(res));
121 curl_easy_cleanup(curl);