1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
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.se/docs/copyright.html.
13 *
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.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24 /* <DESC>
25 * Use the progress callbacks, old and/or new one depending on available
26 * libcurl version.
27 * </DESC>
28 */
29 #include <stdio.h>
30 #include <curl/curl.h>
31
32 #define MINIMAL_PROGRESS_FUNCTIONALITY_INTERVAL 3000000
33 #define STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES 6000
34
35 struct myprogress {
36 curl_off_t lastruntime; /* type depends on version, see above */
37 CURL *curl;
38 };
39
40 /* this is how the CURLOPT_XFERINFOFUNCTION callback works */
xferinfo(void * p,curl_off_t dltotal,curl_off_t dlnow,curl_off_t ultotal,curl_off_t ulnow)41 static int xferinfo(void *p,
42 curl_off_t dltotal, curl_off_t dlnow,
43 curl_off_t ultotal, curl_off_t ulnow)
44 {
45 struct myprogress *myp = (struct myprogress *)p;
46 CURL *curl = myp->curl;
47 curl_off_t curtime = 0;
48
49 curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME_T, &curtime);
50
51 /* under certain circumstances it may be desirable for certain functionality
52 to only run every N seconds, in order to do this the transaction time can
53 be used */
54 if((curtime - myp->lastruntime) >= MINIMAL_PROGRESS_FUNCTIONALITY_INTERVAL) {
55 myp->lastruntime = curtime;
56 fprintf(stderr, "TOTAL TIME: %lu.%06lu\r\n",
57 (unsigned long)(curtime / 1000000),
58 (unsigned long)(curtime % 1000000));
59 }
60
61 fprintf(stderr, "UP: %lu of %lu DOWN: %lu of %lu\r\n",
62 (unsigned long)ulnow, (unsigned long)ultotal,
63 (unsigned long)dlnow, (unsigned long)dltotal);
64
65 if(dlnow > STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES)
66 return 1;
67 return 0;
68 }
69
main(void)70 int main(void)
71 {
72 CURL *curl;
73 CURLcode res = CURLE_OK;
74 struct myprogress prog;
75
76 curl = curl_easy_init();
77 if(curl) {
78 prog.lastruntime = 0;
79 prog.curl = curl;
80
81 curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
82
83 curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, xferinfo);
84 /* pass the struct pointer into the xferinfo function */
85 curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &prog);
86
87 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
88 res = curl_easy_perform(curl);
89
90 if(res != CURLE_OK)
91 fprintf(stderr, "%s\n", curl_easy_strerror(res));
92
93 /* always cleanup */
94 curl_easy_cleanup(curl);
95 }
96 return (int)res;
97 }
98