aboutsummaryrefslogtreecommitdiff
path: root/docs/examples/progressfunc.c
diff options
context:
space:
mode:
Diffstat (limited to 'docs/examples/progressfunc.c')
-rw-r--r--docs/examples/progressfunc.c47
1 files changed, 41 insertions, 6 deletions
diff --git a/docs/examples/progressfunc.c b/docs/examples/progressfunc.c
index 51a9c9b5e..b2635bc8a 100644
--- a/docs/examples/progressfunc.c
+++ b/docs/examples/progressfunc.c
@@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
- * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@@ -30,9 +30,10 @@ struct myprogress {
CURL *curl;
};
-static int progress(void *p,
- double dltotal, double dlnow,
- double ultotal, double ulnow)
+/* this is how the CURLOPT_XFERINFOFUNCTION callback works */
+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;
@@ -48,7 +49,9 @@ static int progress(void *p,
fprintf(stderr, "TOTAL TIME: %f \r\n", curtime);
}
- fprintf(stderr, "UP: %g of %g DOWN: %g of %g\r\n",
+ fprintf(stderr, "UP: %" CURL_FORMAT_CURL_OFF_T " of %" CURL_FORMAT_CURL_OFF_T
+ " DOWN: %" CURL_FORMAT_CURL_OFF_T " of %" CURL_FORMAT_CURL_OFF_T
+ "\r\n",
ulnow, ultotal, dlnow, dltotal);
if(dlnow > STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES)
@@ -56,6 +59,19 @@ static int progress(void *p,
return 0;
}
+/* for libcurl older than 7.32.0 (CURLOPT_PROGRESSFUNCTION) */
+static int older_progress(void *p,
+ double dltotal, double dlnow,
+ double ultotal, double ulnow)
+{
+ return xferinfo(p,
+ (curl_off_t)dltotal,
+ (curl_off_t)dlnow,
+ (curl_off_t)ultotal,
+ (curl_off_t)ulnow);
+}
+
+
int main(void)
{
CURL *curl;
@@ -68,9 +84,28 @@ int main(void)
prog.curl = curl;
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/");
- curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress);
+
+ curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, older_progress);
/* pass the struct pointer into the progress function */
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, &prog);
+
+#if LIBCURL_VERSION_NUM >= 0x072000
+ /* xferinfo was introduced in 7.32.0, no earlier libcurl versions will
+ compile as they won't have the symbols around.
+
+ If built with a newer libcurl, but running with an older libcurl:
+ curl_easy_setopt() will fail in run-time trying to set the new
+ callback, making the older callback get used.
+
+ New libcurls will prefer the new callback and instead use that one even
+ if both callbacks are set. */
+
+ curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, xferinfo);
+ /* pass the struct pointer into the xferinfo function, note that this is
+ an alias to CURLOPT_PROGRESSDATA */
+ curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &prog);
+#endif
+
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
res = curl_easy_perform(curl);