1---
2c: Copyright (C) Daniel Stenberg, <daniel.se>, et al.
3SPDX-License-Identifier: curl
4Title: CURLOPT_XFERINFOFUNCTION
5Section: 3
6Source: libcurl
7See-also:
8  - CURLOPT_NOPROGRESS (3)
9  - CURLOPT_XFERINFODATA (3)
10---
11
12# NAME
13
14CURLOPT_XFERINFOFUNCTION - progress meter callback
15
16# SYNOPSIS
17
18~~~c
19#include <curl/curl.h>
20
21int progress_callback(void *clientp,
22                      curl_off_t dltotal,
23                      curl_off_t dlnow,
24                      curl_off_t ultotal,
25                      curl_off_t ulnow);
26
27CURLcode curl_easy_setopt(CURL *handle, CURLOPT_XFERINFOFUNCTION,
28                          progress_callback);
29~~~
30
31# DESCRIPTION
32
33Pass a pointer to your callback function, which should match the prototype
34shown above.
35
36This function gets called by libcurl instead of its internal equivalent with a
37frequent interval. While data is being transferred it gets called frequently,
38and during slow periods like when nothing is being transferred it can slow
39down to about one call per second.
40
41*clientp* is the pointer set with CURLOPT_XFERINFODATA(3), it is not
42used by libcurl but is only passed along from the application to the callback.
43
44The callback gets told how much data libcurl is about to transfer and has
45already transferred, in number of bytes. *dltotal* is the total number of
46bytes libcurl expects to download in this transfer. *dlnow* is the number
47of bytes downloaded so far. *ultotal* is the total number of bytes libcurl
48expects to upload in this transfer. *ulnow* is the number of bytes
49uploaded so far.
50
51Unknown/unused argument values passed to the callback are set to zero (like if
52you only download data, the upload size remains 0). Many times the callback is
53called one or more times first, before it knows the data sizes so a program
54must be made to handle that.
55
56If your callback function returns CURL_PROGRESSFUNC_CONTINUE it makes libcurl
57to continue executing the default progress function.
58
59Returning any other non-zero value from this callback makes libcurl abort the
60transfer and return *CURLE_ABORTED_BY_CALLBACK*.
61
62If you transfer data with the multi interface, this function is not called
63during periods of idleness unless you call the appropriate libcurl function
64that performs transfers.
65
66CURLOPT_NOPROGRESS(3) must be set to 0 to make this function actually
67get called.
68
69# DEFAULT
70
71By default, libcurl has an internal progress meter. That is rarely wanted by
72users.
73
74# PROTOCOLS
75
76All
77
78# EXAMPLE
79
80~~~c
81struct progress {
82  char *private;
83  size_t size;
84};
85
86static size_t progress_callback(void *clientp,
87                                curl_off_t dltotal,
88                                curl_off_t dlnow,
89                                curl_off_t ultotal,
90                                curl_off_t ulnow)
91{
92  struct progress *memory = clientp;
93  printf("my ptr: %p\n", memory->private);
94
95  /* use the values */
96
97  return 0; /* all is good */
98}
99
100int main(void)
101{
102  CURL *curl = curl_easy_init();
103  if(curl) {
104    struct progress data;
105
106    /* pass struct to callback  */
107    curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &data);
108
109    curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
110  }
111}
112~~~
113
114# AVAILABILITY
115
116Added in 7.32.0. This callback replaces CURLOPT_PROGRESSFUNCTION(3)
117
118# RETURN VALUE
119
120Returns CURLE_OK.
121