1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, 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 http://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 #include <curl/curl.h>
25 #define STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES 6000
26 #define MINIMAL_PROGRESS_FUNCTIONALITY_INTERVAL 3
33 /* this is how the CURLOPT_XFERINFOFUNCTION callback works */
34 static int xferinfo(void *p,
35 curl_off_t dltotal, curl_off_t dlnow,
36 curl_off_t ultotal, curl_off_t ulnow)
38 struct myprogress *myp = (struct myprogress *)p;
39 CURL *curl = myp->curl;
42 curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &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
47 if((curtime - myp->lastruntime) >= MINIMAL_PROGRESS_FUNCTIONALITY_INTERVAL) {
48 myp->lastruntime = curtime;
49 fprintf(stderr, "TOTAL TIME: %f \r\n", curtime);
52 fprintf(stderr, "UP: %" CURL_FORMAT_CURL_OFF_T " of %" CURL_FORMAT_CURL_OFF_T
53 " DOWN: %" CURL_FORMAT_CURL_OFF_T " of %" CURL_FORMAT_CURL_OFF_T
55 ulnow, ultotal, dlnow, dltotal);
57 if(dlnow > STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES)
62 /* for libcurl older than 7.32.0 (CURLOPT_PROGRESSFUNCTION) */
63 static int older_progress(void *p,
64 double dltotal, double dlnow,
65 double ultotal, double ulnow)
78 CURLcode res = CURLE_OK;
79 struct myprogress prog;
81 curl = curl_easy_init();
86 curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/");
88 curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, older_progress);
89 /* pass the struct pointer into the progress function */
90 curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, &prog);
92 #if LIBCURL_VERSION_NUM >= 0x072000
93 /* xferinfo was introduced in 7.32.0, no earlier libcurl versions will
94 compile as they won't have the symbols around.
96 If built with a newer libcurl, but running with an older libcurl:
97 curl_easy_setopt() will fail in run-time trying to set the new
98 callback, making the older callback get used.
100 New libcurls will prefer the new callback and instead use that one even
101 if both callbacks are set. */
103 curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, xferinfo);
104 /* pass the struct pointer into the xferinfo function, note that this is
105 an alias to CURLOPT_PROGRESSDATA */
106 curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &prog);
109 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
110 res = curl_easy_perform(curl);
113 fprintf(stderr, "%s\n", curl_easy_strerror(res));
116 curl_easy_cleanup(curl);