1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, 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 ***************************************************************************/
22 #include "tool_setup.h"
24 #define ENABLE_CURLX_PRINTF
25 /* use our own printf() functions */
28 #include "tool_cfgable.h"
29 #include "tool_cb_prg.h"
30 #include "tool_util.h"
32 #include "memdebug.h" /* keep this as LAST include */
35 ** callback for CURLOPT_XFERINFOFUNCTION
38 #define MAX_BARLENGTH 256
40 int tool_progress_cb(void *clientp,
41 curl_off_t dltotal, curl_off_t dlnow,
42 curl_off_t ultotal, curl_off_t ulnow)
44 /* The original progress-bar source code was written for curl by Lars Aas,
45 and this new edition inherits some of his concepts. */
47 char line[MAX_BARLENGTH+1];
53 struct timeval now = tvnow();
54 struct ProgressData *bar = (struct ProgressData *)clientp;
58 /* expected transfer size */
59 total = dltotal + ultotal + bar->initial_size;
61 /* we've come this far */
62 point = dlnow + ulnow + bar->initial_size;
64 if(bar->calls && (tvdiff(now, bar->prevtime) < 100L) && point < total)
65 /* after first call, limit progress-bar updating to 10 Hz */
66 /* update when we're at 100% even if last update is less than 200ms ago */
70 /* we have got more than the expected total! */
73 /* simply count invokes */
77 curl_off_t prevblock = bar->prev / 1024;
78 curl_off_t thisblock = point / 1024;
79 while(thisblock > prevblock) {
80 fprintf(bar->out, "#");
84 else if(point != bar->prev) {
85 frac = (double)point / (double)total;
86 percent = frac * 100.0f;
87 barwidth = bar->width - 7;
88 num = (int) (((double)barwidth) * frac);
89 if(num > MAX_BARLENGTH)
91 memset(line, '#', num);
93 snprintf(format, sizeof(format), "\r%%-%ds %%5.1f%%%%", barwidth);
94 fprintf(bar->out, format, line, percent);
103 void progressbarinit(struct ProgressData *bar,
104 struct OperationConfig *config)
112 memset(bar, 0, sizeof(struct ProgressData));
114 /* pass this through to progress function so
115 * it can display progress towards total file
116 * not just the part that's left. (21-may-03, dbyron) */
117 if(config->use_resume)
118 bar->initial_size = config->resume_from;
120 /* TODO: get terminal width through ansi escapes or something similar.
121 try to update width when xterm is resized... - 19990617 larsa */
124 * OS/2 users most likely won't have this env var set, and besides that
125 * we're using our own way to determine screen width */
126 colp = curlx_getenv("COLUMNS");
129 long num = strtol(colp, &endptr, 10);
130 if((endptr != colp) && (endptr == colp + strlen(colp)) && (num > 0))
131 bar->width = (int)num;
140 * We use this emx library call to get the screen width, and subtract
141 * one from what we got in order to avoid a problem with the cursor
142 * advancing to the next line if we print a string that is as long as
143 * the screen is wide. */
146 bar->width = scr_size[0] - 1;
149 bar->out = config->global->errors;