Debian Codegraph @

src/progress.c

raw
1 /* Download progress.
2 Copyright (C) 2001-2011, 2015, 2018-2024 Free Software Foundation,
3 Inc.
4
5 This file is part of GNU Wget.
6
7 GNU Wget is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Wget is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Wget. If not, see <http://www.gnu.org/licenses/>.
19
20 Additional permission under GNU GPL version 3 section 7
21
22 If you modify this program, or any covered work, by linking or
23 combining it with the OpenSSL project's OpenSSL library (or a
24 modified version of that library), containing parts covered by the
25 terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
26 grants you additional permission to convey the resulting work.
27 Corresponding Source for a non-source form of such a combination
28 shall include the source code for the parts of OpenSSL used as well
29 as that of the covered work. */
30
31 #include "wget.h"
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <assert.h>
37 #include <unistd.h>
38 #include <signal.h>
39 #include <wchar.h>
40 #include <mbiter.h>
41
42 #include "progress.h"
43 #include "utils.h"
44 #include "retr.h"
45 #include "c-strcase.h"
46
47 struct progress_implementation {
48 const char *name;
49 bool interactive;
50 void *(*create) (const char *, wgint, wgint);
51 void (*update) (void *, wgint, double);
52 void (*draw) (void *);
53 void (*finish) (void *, double);
54 void (*set_params) (const char *);
55 };
56
57 /* Necessary forward declarations. */
58
59 static void *dot_create (const char *, wgint, wgint);
60 static void dot_update (void *, wgint, double);
61 static void dot_finish (void *, double);
62 static void dot_draw (void *);
63 static void dot_set_params (const char *);
64
65 static void *bar_create (const char *, wgint, wgint);
66 static void bar_update (void *, wgint, double);
67 static void bar_draw (void *);
68 static void bar_finish (void *, double);
69 static void bar_set_params (const char *);
70
71 static struct progress_implementation implementations[] = {
72 { "dot", 0, dot_create, dot_update, dot_draw, dot_finish, dot_set_params },
73 { "bar", 1, bar_create, bar_update, bar_draw, bar_finish, bar_set_params }
74 };
75 static struct progress_implementation *current_impl;
76 static int current_impl_locked;
77
78 /* Progress implementation used by default. Can be overridden in
79 wgetrc or by the fallback one. */
80
81 #define DEFAULT_PROGRESS_IMPLEMENTATION "bar"
82
83 /* Fallback progress implementation should be something that works
84 under all display types. If you put something other than "dot"
85 here, remember that bar_set_params tries to switch to this if we're
86 not running on a TTY. So changing this to "bar" could cause
87 infloop. */
88
89 #define FALLBACK_PROGRESS_IMPLEMENTATION "dot"
90
91 /* Return true if NAME names a valid progress bar implementation. The
92 characters after the first : will be ignored. */
93
94 bool
95 valid_progress_implementation_p (const char *name)
96 {
97 size_t i;
98 struct progress_implementation *pi = implementations;
99 char *colon = strchr (name, ':');
100 size_t namelen = colon ? (size_t) (colon - name) : strlen (name);
101
102 for (i = 0; i < countof (implementations); i++, pi++)
103 if (!strncmp (pi->name, name, namelen))
104 return true;
105 return false;
106 }
107
108 /* Set the progress implementation to NAME. */
109
110 void
111 set_progress_implementation (const char *name)
112 {
113 size_t i, namelen;
114 struct progress_implementation *pi = implementations;
115 const char *colon;
116
117 if (!name)
118 name = DEFAULT_PROGRESS_IMPLEMENTATION;
119
120 colon = strchr (name, ':');
121 namelen = colon ? (size_t) (colon - name) : strlen (name);
122
123 for (i = 0; i < countof (implementations); i++, pi++)
124 if (!strncmp (pi->name, name, namelen))
125 {
126 current_impl = pi;
127 current_impl_locked = 0;
128
129 if (colon)
130 /* We call pi->set_params even if colon is NULL because we
131 want to give the implementation a chance to set up some
132 things it needs to run. */
133 ++colon;
134
135 if (pi->set_params)
136 pi->set_params (colon);
137 return;
138 }
139 abort ();
140 }
141
142 static int output_redirected;
143
144 void
145 progress_schedule_redirect (void)
146 {
147 output_redirected = 1;
148 }
149
150 /* Create a progress gauge. INITIAL is the number of bytes the
151 download starts from (zero if the download starts from scratch).
152 TOTAL is the expected total number of bytes in this download. If
153 TOTAL is zero, it means that the download size is not known in
154 advance. */
155
156 void *
157 progress_create (const char *f_download, wgint initial, wgint total)
158 {
159 /* Check if the log status has changed under our feet. */
160 if (output_redirected)
161 {
162 if (!current_impl_locked)
163 set_progress_implementation (FALLBACK_PROGRESS_IMPLEMENTATION);
164 output_redirected = 0;
165 }
166
167 return current_impl->create (f_download, initial, total);
168 }
169
170 /* Return true if the progress gauge is "interactive", i.e. if it can
171 profit from being called regularly even in absence of data. The
172 progress bar is interactive because it regularly updates the ETA
173 and current update. */
174
175 bool
176 progress_interactive_p (void *progress _GL_UNUSED)
177 {
178 return current_impl->interactive;
179 }
180
181 /* Inform the progress gauge of newly received bytes. DLTIME is the
182 time since the beginning of the download. */
183
184 void
185 progress_update (void *progress, wgint howmuch, double dltime)
186 {
187 // sanitize input
188 if (dltime >= INT_MAX)
189 dltime = INT_MAX - 1;
190 else if (dltime < 0)
191 dltime = 0;
192
193 if (howmuch < 0)
194 howmuch = 0;
195
196 current_impl->update (progress, howmuch, dltime);
197 current_impl->draw (progress);
198 }
199
200 /* Tell the progress gauge to clean up. Calling this will free the
201 PROGRESS object, the further use of which is not allowed. */
202
203 void
204 progress_finish (void *progress, double dltime)
205 {
206 // sanitize input
207 if (dltime >= INT_MAX)
208 dltime = INT_MAX - 1;
209 else if (dltime < 0)
210 dltime = 0;
211
212 current_impl->finish (progress, dltime);
213 }
214
215 /* Dot-printing. */
216
217 struct dot_progress {
218 wgint initial_length; /* how many bytes have been downloaded
219 previously. */
220 wgint total_length; /* expected total byte count when the
221 download finishes */
222
223 wgint accumulated; /* number of bytes accumulated after
224 the last printed dot */
225
226 double dltime; /* download time so far */
227 wgint rows; /* number of rows printed so far */
228 int dots; /* number of dots printed in this row */
229
230 double last_timer_value;
231 };
232
233 /* Dot-progress backend for progress_create. */
234
235 static void *
236 dot_create (const char *f_download _GL_UNUSED, wgint initial, wgint total)
237 {
238 struct dot_progress *dp = xnew0 (struct dot_progress);
239 dp->initial_length = initial;
240 dp->total_length = total;
241
242 if (dp->initial_length)
243 {
244 int dot_bytes = opt.dot_bytes;
245 const wgint ROW_BYTES = opt.dot_bytes * opt.dots_in_line;
246
247 int remainder = dp->initial_length % ROW_BYTES;
248 wgint skipped = dp->initial_length - remainder;
249
250 if (skipped)
251 {
252 wgint skipped_k = skipped / 1024; /* skipped amount in K */
253 int skipped_k_len = numdigit (skipped_k);
254 if (skipped_k_len < 6)
255 skipped_k_len = 6;
256
257 /* Align the [ skipping ... ] line with the dots. To do
258 that, insert the number of spaces equal to the number of
259 digits in the skipped amount in K. */
260 logprintf (LOG_PROGRESS, _("\n%*s[ skipping %sK ]"),
261 2 + skipped_k_len, "",
262 number_to_static_string (skipped_k));
263 }
264
265 logprintf (LOG_PROGRESS, "\n%6sK",
266 number_to_static_string (skipped / 1024));
267 for (; remainder >= dot_bytes; remainder -= dot_bytes)
268 {
269 if (dp->dots % opt.dot_spacing == 0)
270 logputs (LOG_PROGRESS, " ");
271 logputs (LOG_PROGRESS, ",");
272 ++dp->dots;
273 }
274 assert (dp->dots < opt.dots_in_line);
275
276 dp->accumulated = remainder;
277 dp->rows = skipped / ROW_BYTES;
278 }
279
280 return dp;
281 }
282
283 static const char *eta_to_human_short (int, bool);
284
285 /* ADD_DOT_ROWS_THRS - minimal (1 << ADD_DOT_ROWS_THRS) ROWS to be added
286 to the current row if dp->accumulated too much.
287 Allows to reduce dot_draw io, times.
288 According to the way progress_update is currently has being called, this
289 should happens only when fuzzing, or (paranoia) if somehow buffer will
290 be too large.
291 Can be disabled by default if this is not fuzzing build. */
292 #ifndef ADD_DOT_ROWS_THRS
293 #if FUZZING
294 #define ADD_DOT_ROWS_THRS 2
295 #else
296 #define ADD_DOT_ROWS_THRS 2
297 #endif
298 #endif /* ADD_DOT_ROWS_THRS */
299
300 /* Prints the stats (percentage of completion, speed, ETA) for current
301 row. DLTIME is the time spent downloading the data in current
302 row.
303
304 #### This function is somewhat uglified by the fact that current
305 row and last row have somewhat different stats requirements. It
306 might be worthwhile to split it to two different functions. */
307
308 static void
309 #if ADD_DOT_ROWS_THRS
310 print_row_stats (struct dot_progress *dp, double dltime, bool last, wgint added_rows)
311 #else
312 print_row_stats (struct dot_progress *dp, double dltime, bool last)
313 #endif
314 {
315 const wgint ROW_BYTES = opt.dot_bytes * opt.dots_in_line;
316
317 /* bytes_displayed is the number of bytes indicated to the user by
318 dots printed so far, includes the initially "skipped" amount */
319 wgint bytes_displayed = dp->rows * ROW_BYTES + dp->dots * opt.dot_bytes;
320
321 if (last)
322 /* For last row also count bytes accumulated after last dot */
323 bytes_displayed += dp->accumulated;
324
325 if (bytes_displayed < 0)
326 bytes_displayed = 0;
327
328 if (dp->total_length)
329 {
330 /* Round to floor value to provide gauge how much data *has*
331 been retrieved. 12.8% will round to 12% because the 13% mark
332 has not yet been reached. 100% is only shown when done. */
333 int percentage = 100.0 * bytes_displayed / dp->total_length;
334 logprintf (LOG_PROGRESS, "%3d%%", percentage);
335 }
336
337 {
338 static char names[] = {' ', 'K', 'M', 'G', 'T'};
339 int units;
340 double rate;
341 wgint bytes_this_row;
342 if (!last)
343 #if ADD_DOT_ROWS_THRS
344 bytes_this_row = ROW_BYTES * added_rows;
345 #else
346 bytes_this_row = ROW_BYTES;
347 #endif
348 else
349 /* For last row also include bytes accumulated after last dot. */
350 bytes_this_row = dp->dots * opt.dot_bytes + dp->accumulated;
351 /* Don't count the portion of the row belonging to initial_length */
352 if (dp->rows == dp->initial_length / ROW_BYTES)
353 bytes_this_row -= dp->initial_length % ROW_BYTES;
354 rate = calc_rate (bytes_this_row, dltime - dp->last_timer_value, &units);
355 logprintf (LOG_PROGRESS, " %4.*f%c",
356 rate >= 99.95 ? 0 : rate >= 9.995 ? 1 : 2,
357 rate, names[units]);
358 dp->last_timer_value = dltime;
359 }
360
361 if (!last)
362 {
363 /* Display ETA based on average speed. Inspired by Vladi
364 Belperchinov-Shabanski's "wget-new-percentage" patch. */
365 if (dp->total_length)
366 {
367 wgint bytes_remaining = dp->total_length > bytes_displayed ? dp->total_length - bytes_displayed : 0;
368 /* The quantity downloaded in this download run. */
369 wgint bytes_sofar = bytes_displayed > dp->initial_length ? bytes_displayed - dp->initial_length : 1;
370 double eta = dltime * bytes_remaining / bytes_sofar;
371 if (eta < 0)
372 eta = 0;
373 if (eta < INT_MAX - 1)
374 logprintf (LOG_PROGRESS, " %s",
375 eta_to_human_short ((int) (eta + 0.5), true));
376 }
377 }
378 else
379 {
380 /* When done, print the total download time */
381 if (dltime >= 10)
382 logprintf (LOG_PROGRESS, "=%s",
383 eta_to_human_short ((int) (dltime + 0.5), true));
384 else
385 logprintf (LOG_PROGRESS, "=%ss", print_decimal (dltime));
386 }
387 }
388
389 /* Dot-progress backend for progress_update. */
390
391 static void
392 dot_update (void *progress, wgint howmuch, double dltime)
393 {
394 // sanitize input
395 if (dltime >= INT_MAX)
396 dltime = INT_MAX - 1;
397 else if (dltime < 0)
398 dltime = 0;
399
400 if (howmuch < 0)
401 howmuch = 0;
402
403 struct dot_progress *dp = progress;
404 dp->accumulated += howmuch;
405 dp->dltime = dltime;
406 }
407
408 static void
409 dot_draw (void *progress)
410 {
411 struct dot_progress *dp = progress;
412 int dot_bytes = opt.dot_bytes;
413 wgint ROW_BYTES = opt.dot_bytes * opt.dots_in_line;
414
415 log_set_flush (false);
416
417 while (dp->accumulated >= dot_bytes)
418 {
419 dp->accumulated -= dot_bytes;
420 if (dp->dots == 0)
421 logprintf (LOG_PROGRESS, "\n%6sK",
422 number_to_static_string (dp->rows * ROW_BYTES / 1024));
423
424 if (dp->dots % opt.dot_spacing == 0)
425 logputs (LOG_PROGRESS, " ");
426 logputs (LOG_PROGRESS, ".");
427
428 ++dp->dots;
429 if (dp->dots >= opt.dots_in_line)
430 {
431 dp->dots = 0;
432 #if ADD_DOT_ROWS_THRS
433 {
434 wgint added_rows = 1;
435 if (dp->accumulated >= (ROW_BYTES << ADD_DOT_ROWS_THRS))
436 {
437 added_rows += dp->accumulated / ROW_BYTES;
438 dp->accumulated %= ROW_BYTES;
439 }
440 if (WGINT_MAX - dp->rows >= added_rows)
441 dp->rows += added_rows;
442 else
443 dp->rows = WGINT_MAX;
444 print_row_stats (dp, dp->dltime, false, added_rows);
445 }
446 #else
447 if (dp->rows < WGINT_MAX)
448 ++dp->rows;
449 print_row_stats (dp, dp->dltime, false);
450 #endif /* ADD_DOT_ROWS_THRS */
451 }
452 }
453
454 log_set_flush (true);
455 }
456
457 /* Dot-progress backend for progress_finish. */
458
459 static void
460 dot_finish (void *progress, double dltime)
461 {
462 struct dot_progress *dp = progress;
463 wgint ROW_BYTES = opt.dot_bytes * opt.dots_in_line;
464 int i;
465
466 log_set_flush (false);
467
468 if (dp->dots == 0)
469 logprintf (LOG_PROGRESS, "\n%6sK",
470 number_to_static_string (dp->rows * ROW_BYTES / 1024));
471 for (i = dp->dots; i < opt.dots_in_line; i++)
472 {
473 if (i % opt.dot_spacing == 0)
474 logputs (LOG_PROGRESS, " ");
475 logputs (LOG_PROGRESS, " ");
476 }
477
478 // sanitize input
479 if (dltime >= INT_MAX)
480 dltime = INT_MAX - 1;
481 else if (dltime < 0)
482 dltime = 0;
483 #if ADD_DOT_ROWS_THRS
484 print_row_stats (dp, dltime, true, 1);
485 #else
486 print_row_stats (dp, dltime, true);
487 #endif
488 logputs (LOG_PROGRESS, "\n\n");
489 log_set_flush (false);
490
491 xfree (dp);
492 }
493
494 /* This function interprets the progress "parameters". For example,
495 if Wget is invoked with --progress=dot:mega, it will set the
496 "dot-style" to "mega". Valid styles are default, binary, mega, and
497 giga. */
498
499 static void
500 dot_set_params (const char *params)
501 {
502 current_impl->interactive = false;
503 if (!params || !*params)
504 params = opt.dot_style;
505
506 if (!params)
507 return;
508
509 /* We use this to set the retrieval style. */
510 if (!c_strcasecmp (params, "default"))
511 {
512 /* Default style: 1K dots, 10 dots in a cluster, 50 dots in a
513 line. */
514 opt.dot_bytes = 1024;
515 opt.dot_spacing = 10;
516 opt.dots_in_line = 50;
517 }
518 else if (!c_strcasecmp (params, "binary"))
519 {
520 /* "Binary" retrieval: 8K dots, 16 dots in a cluster, 48 dots
521 (384K) in a line. */
522 opt.dot_bytes = 8192;
523 opt.dot_spacing = 16;
524 opt.dots_in_line = 48;
525 }
526 else if (!c_strcasecmp (params, "mega"))
527 {
528 /* "Mega" retrieval, for retrieving very long files; each dot is
529 64K, 8 dots in a cluster, 6 clusters (3M) in a line. */
530 opt.dot_bytes = 65536L;
531 opt.dot_spacing = 8;
532 opt.dots_in_line = 48;
533 }
534 else if (!c_strcasecmp (params, "giga"))
535 {
536 /* "Giga" retrieval, for retrieving very very *very* long files;
537 each dot is 1M, 8 dots in a cluster, 4 clusters (32M) in a
538 line. */
539 opt.dot_bytes = (1L << 20);
540 opt.dot_spacing = 8;
541 opt.dots_in_line = 32;
542 }
543 else
544 fprintf (stderr,
545 _("Invalid dot style specification %s; leaving unchanged.\n"),
546 quote (params));
547 }
548
549 /* "Thermometer" (bar) progress. */
550
551 /* Assumed screen width if we can't find the real value. */
552 #define DEFAULT_SCREEN_WIDTH 80
553
554 /* Minimum screen width we'll try to work with. If this is too small,
555 create_image will overflow the buffer.
556 width = width/4 + 1 + PROGRESS_PERCENT_LEN + PROGRESS_FILESIZE_LEN
557 + PROGRESS_DWNLOAD_RATE + PROGRESS_ETA_LEN => widh = 38*4/3 (+1) */
558 #define MINIMUM_SCREEN_WIDTH 51
559
560 /* The last known screen width. This can be updated by the code that
561 detects that SIGWINCH was received (but it's never updated from the
562 signal handler). */
563 static int screen_width;
564
565 /* A flag that, when set, means SIGWINCH was received. */
566 static volatile sig_atomic_t received_sigwinch;
567
568 /* Size of the download speed history ring. */
569 #define DLSPEED_HISTORY_SIZE 20
570
571 /* The minimum time length of a history sample. By default, each
572 sample is at least 150ms long, which means that, over the course of
573 20 samples, "current" download speed spans at least 3s into the
574 past. */
575 #define DLSPEED_SAMPLE_MIN 0.15
576
577 /* The time after which the download starts to be considered
578 "stalled", i.e. the current bandwidth is not printed and the recent
579 download speeds are scratched. */
580 #define STALL_START_TIME 5
581
582 /* Time between screen refreshes will not be shorter than this, so
583 that Wget doesn't swamp the TTY with output. */
584 #define REFRESH_INTERVAL 0.2
585
586 /* Don't refresh the ETA too often to avoid jerkiness in predictions.
587 This allows ETA to change approximately once per second. */
588 #define ETA_REFRESH_INTERVAL 0.99
589
590 struct bar_progress {
591 char *f_download; /* Filename of the downloaded file */
592 wgint initial_length; /* how many bytes have been downloaded
593 previously. */
594 wgint total_length; /* expected total byte count when the
595 download finishes */
596 wgint count; /* bytes downloaded so far */
597
598 double last_screen_update; /* time of the last screen update,
599 measured since the beginning of
600 download. */
601
602 double dltime; /* download time so far */
603 int width; /* screen width we're using at the
604 time the progress gauge was
605 created. this is different from
606 the screen_width global variable in
607 that the latter can be changed by a
608 signal. */
609 char *buffer; /* buffer where the bar "image" is
610 stored. */
611 int tick; /* counter used for drawing the
612 progress bar where the total size
613 is not known. */
614
615 /* The following variables (kept in a struct for namespace reasons)
616 keep track of recent download speeds. See bar_update() for
617 details. */
618 struct bar_progress_hist {
619 int pos;
620 double times[DLSPEED_HISTORY_SIZE];
621 wgint bytes[DLSPEED_HISTORY_SIZE];
622
623 /* The sum of times and bytes respectively, maintained for
624 efficiency. */
625 double total_time;
626 wgint total_bytes;
627 } hist;
628
629 double recent_start; /* timestamp of beginning of current
630 position. */
631 wgint recent_bytes; /* bytes downloaded so far. */
632
633 bool stalled; /* set when no data arrives for longer
634 than STALL_START_TIME, then reset
635 when new data arrives. */
636
637 /* create_image() uses these to make sure that ETA information
638 doesn't flicker. */
639 double last_eta_time; /* time of the last update to download
640 speed and ETA, measured since the
641 beginning of download. */
642 int last_eta_value;
643 };
644
645 static void create_image (struct bar_progress *, double, bool);
646 static void display_image (char *);
647
648 #if USE_NLS_PROGRESS_BAR
649 static size_t
650 prepare_filename (char *dest, const char *src)
651 {
652 size_t ret = 1;
653 if (src)
654 {
655 mbi_iterator_t iter;
656 mbchar_t mbc;
657 mbi_init (iter, src, strlen (src));
658 while (mbi_avail (iter))
659 {
660 size_t i;
661 mbc = mbi_cur(iter);
662 /* replace invalid || unprintable || zero-width mbc ws hexdgt code */
663 if (!mb_isprint (mbc) || !mb_width (mbc))
664 for (i=0; i < mb_len (mbc); i++)
665 {
666 if (dest)
667 dest += sprintf (dest, "%%%02x", (unsigned char) *(mb_ptr(mbc) + i));
668 ret += 3;
669 }
670 else
671 {
672 if (dest)
673 for (i=0; i < mb_len (mbc); i++)
674 *dest++ = *(mb_ptr (mbc) + i);
675 ret += mb_len (mbc);
676 }
677 mbi_advance (iter);
678 }
679 }
680 if (dest)
681 *dest = 0;
682 return ret;
683 }
684 #else
685 #include <ctype.h>
686 static size_t
687 prepare_filename (char *dest, const char *src)
688 {
689 size_t ret = 1;
690 if (src)
691 while (*src)
692 {
693 /* isprint with some lang return false for some chars */
694 if(!iscntrl (*src))
695 {
696 if (dest)
697 *dest++ = *src;
698 ret++;
699 }
700 else
701 {
702 if (dest)
703 dest += sprintf (dest, "%%%02x", (unsigned char) *src );
704 ret += 3;
705 }
706 src++;
707 }
708 if (dest)
709 *dest = 0;
710 return ret;
711 }
712 #endif /* USE_NLS_PROGRESS_BAR */
713
714 static void *
715 bar_create (const char *f_download, wgint initial, wgint total)
716 {
717 struct bar_progress *bp = xnew0 (struct bar_progress);
718
719 /* In theory, our callers should take care of this pathological
720 case, but it can sometimes happen. */
721 if (initial > total)
722 total = initial;
723
724 bp->initial_length = initial;
725 bp->total_length = total;
726 /* Zero-width mbc must be replaced to avoid buffer overflow.
727 Another way is to allocate a buffer that allows contain
728 full f_download, but in this case some escape sequences may break "bar" */
729 bp->f_download = xmalloc (prepare_filename (NULL, f_download));
730 prepare_filename (bp->f_download, f_download);
731
732 /* Initialize screen_width if this hasn't been done or if it might
733 have changed, as indicated by receiving SIGWINCH. */
734 if (!screen_width || received_sigwinch)
735 {
736 screen_width = determine_screen_width ();
737 if (!screen_width)
738 screen_width = DEFAULT_SCREEN_WIDTH;
739 else if (screen_width < MINIMUM_SCREEN_WIDTH)
740 screen_width = MINIMUM_SCREEN_WIDTH;
741 received_sigwinch = 0;
742 }
743
744 /* - 1 because we don't want to use the last screen column. */
745 bp->width = screen_width - 1;
746 /* + enough space for the terminating zero, and hopefully enough room
747 * for multibyte characters. */
748 #define BUF_LEN (bp->width * 2 + 100)
749 bp->buffer = xcalloc (BUF_LEN, 1);
750
751 logputs (LOG_VERBOSE, "\n");
752
753 create_image (bp, 0, false);
754 display_image (bp->buffer);
755
756 return bp;
757 }
758
759 static void update_speed_ring (struct bar_progress *, wgint, double);
760
761 static void
762 bar_update (void *progress, wgint howmuch, double dltime)
763 {
764 struct bar_progress *bp = progress;
765
766 bp->dltime = dltime;
767 if (WGINT_MAX - (bp->count + bp->initial_length) >= howmuch)
768 bp->count += howmuch;
769 else
770 bp->count = WGINT_MAX - bp->initial_length;
771 if (bp->total_length > 0
772 && bp->count + bp->initial_length > bp->total_length)
773 /* We could be downloading more than total_length, e.g. when the
774 server sends an incorrect Content-Length header. In that case,
775 adjust bp->total_length to the new reality, so that the code in
776 create_image() that depends on total size being smaller or
777 equal to the expected size doesn't abort. */
778 bp->total_length = bp->initial_length + bp->count;
779
780 update_speed_ring (bp, howmuch, dltime);
781 }
782
783 static void
784 bar_draw (void *progress)
785 {
786 bool force_screen_update = false;
787 struct bar_progress *bp = progress;
788
789 /* If SIGWINCH (the window size change signal) been received,
790 determine the new screen size and update the screen. */
791 if (received_sigwinch)
792 {
793 int old_width = screen_width;
794 screen_width = determine_screen_width ();
795 if (!screen_width)
796 screen_width = DEFAULT_SCREEN_WIDTH;
797 else if (screen_width < MINIMUM_SCREEN_WIDTH)
798 screen_width = MINIMUM_SCREEN_WIDTH;
799 if (screen_width != old_width)
800 {
801 bp->width = screen_width - 1;
802 bp->buffer = xrealloc (bp->buffer, BUF_LEN);
803 force_screen_update = true;
804 }
805 received_sigwinch = 0;
806 }
807
808 if (bp->dltime - bp->last_screen_update < REFRESH_INTERVAL && !force_screen_update)
809 /* Don't update more often than five times per second. */
810 return;
811
812 create_image (bp, bp->dltime, false);
813 display_image (bp->buffer);
814 bp->last_screen_update = bp->dltime;
815 }
816
817 static void
818 bar_finish (void *progress, double dltime)
819 {
820 struct bar_progress *bp = progress;
821
822 if (bp->total_length > 0
823 && bp->count + bp->initial_length > bp->total_length)
824 /* See bar_update() for explanation. */
825 bp->total_length = bp->initial_length + bp->count;
826
827 create_image (bp, dltime, true);
828 display_image (bp->buffer);
829
830 logputs (LOG_VERBOSE, "\n");
831 logputs (LOG_PROGRESS, "\n");
832
833 xfree (bp->f_download);
834 xfree (bp->buffer);
835 xfree (bp);
836 }
837
838 /* This code attempts to maintain the notion of a "current" download
839 speed, over the course of no less than 3s. (Shorter intervals
840 produce very erratic results.)
841
842 To do so, it samples the speed in 150ms intervals and stores the
843 recorded samples in a FIFO history ring. The ring stores no more
844 than 20 intervals, hence the history covers the period of at least
845 three seconds and at most 20 reads into the past. This method
846 should produce reasonable results for downloads ranging from very
847 slow to very fast.
848
849 The idea is that for fast downloads, we get the speed over exactly
850 the last three seconds. For slow downloads (where a network read
851 takes more than 150ms to complete), we get the speed over a larger
852 time period, as large as it takes to complete twenty reads. This
853 is good because slow downloads tend to fluctuate more and a
854 3-second average would be too erratic. */
855
856 static void
857 update_speed_ring (struct bar_progress *bp, wgint howmuch, double dltime)
858 {
859 struct bar_progress_hist *hist = &bp->hist;
860 double recent_age = dltime - bp->recent_start;
861
862 /* Update the download count. */
863 bp->recent_bytes += howmuch;
864
865 /* For very small time intervals, we return after having updated the
866 "recent" download count. When its age reaches or exceeds minimum
867 sample time, it will be recorded in the history ring. */
868 if (recent_age < DLSPEED_SAMPLE_MIN)
869 return;
870
871 if (howmuch == 0)
872 {
873 /* If we're not downloading anything, we might be stalling,
874 i.e. not downloading anything for an extended period of time.
875 Since 0-reads do not enter the history ring, recent_age
876 effectively measures the time since last read. */
877 if (recent_age >= STALL_START_TIME)
878 {
879 /* If we're stalling, reset the ring contents because it's
880 stale and because it will make bar_update stop printing
881 the (bogus) current bandwidth. */
882 bp->stalled = true;
883 xzero (*hist);
884 bp->recent_bytes = 0;
885 }
886 return;
887 }
888
889 /* We now have a non-zero amount of to store to the speed ring. */
890
891 /* If the stall status was acquired, reset it. */
892 if (bp->stalled)
893 {
894 bp->stalled = false;
895 /* "recent_age" includes the entire stalled period, which
896 could be very long. Don't update the speed ring with that
897 value because the current bandwidth would start too small.
898 Start with an arbitrary (but more reasonable) time value and
899 let it level out. */
900 recent_age = 1;
901 }
902
903 /* Store "recent" bytes and download time to history ring at the
904 position POS. */
905
906 /* To correctly maintain the totals, first invalidate existing data
907 (least recent in time) at this position. */
908 hist->total_time -= hist->times[hist->pos];
909 hist->total_bytes -= hist->bytes[hist->pos];
910
911 /* Now store the new data and update the totals. */
912 hist->times[hist->pos] = recent_age;
913 hist->bytes[hist->pos] = bp->recent_bytes;
914 hist->total_time += recent_age;
915 hist->total_bytes += bp->recent_bytes;
916
917 /* Start a new "recent" period. */
918 bp->recent_start = dltime;
919 bp->recent_bytes = 0;
920
921 /* Advance the current ring position. */
922 if (++hist->pos == DLSPEED_HISTORY_SIZE)
923 hist->pos = 0;
924
925 #if 0
926 /* Sledgehammer check to verify that the totals are accurate. */
927 {
928 int i;
929 double sumt = 0, sumb = 0;
930 for (i = 0; i < DLSPEED_HISTORY_SIZE; i++)
931 {
932 sumt += hist->times[i];
933 sumb += hist->bytes[i];
934 }
935 assert (sumb == hist->total_bytes);
936 /* We can't use assert(sumt==hist->total_time) because some
937 precision is lost by adding and subtracting floating-point
938 numbers. But during a download this precision should not be
939 detectable, i.e. no larger than 1ns. */
940 double diff = sumt - hist->total_time;
941 if (diff < 0) diff = -diff;
942 assert (diff < 1e-9);
943 }
944 #endif
945 }
946
947 #if USE_NLS_PROGRESS_BAR
948 static int
949 count_cols (const char *mbs)
950 {
951 mbchar_t mbc;
952 mbi_iterator_t iter;
953 int cols = 0;
954 mbi_init (iter, mbs, strlen(mbs));
955 while (mbi_avail (iter))
956 {
957 mbc = mbi_cur (iter);
958 cols += mb_width (mbc);
959 mbi_advance (iter);
960 }
961 return cols;
962 }
963
964 static int
965 cols_to_bytes (const char *mbs, const int cols, int *ncols)
966 {
967 int p_cols = 0, bytes = 0;
968 mbchar_t mbc;
969 mbi_iterator_t iter;
970 mbi_init (iter, mbs, strlen(mbs));
971 while (p_cols < cols && mbi_avail (iter))
972 {
973 mbc = mbi_cur (iter);
974 p_cols += mb_width (mbc);
975 /* The multibyte character has exceeded the total number of columns we
976 * have available. The remaining bytes will be padded with a space. */
977 if (p_cols > cols)
978 {
979 p_cols -= mb_width (mbc);
980 break;
981 }
982 bytes += mb_len (mbc);
983 mbi_advance (iter);
984 }
985 *ncols = p_cols;
986 return bytes;
987 }
988 #else
989 static int count_cols (const char *mbs) { return (int) strlen(mbs); }
990
991 static int
992 cols_to_bytes (const char *mbs, const int cols, int *ncols)
993 {
994 int len = strlen(mbs);
995 int ret = len < cols ? len : cols;
996 *ncols = ret;
997 return ret;
998 }
999 #endif
1000
1001 static const char *
1002 get_eta (int *bcd)
1003 {
1004 /* TRANSLATORS: "ETA" is English-centric, but this must
1005 be short, ideally 3 chars. Abbreviate if necessary. */
1006 static const char eta_str[] = N_(" eta %s");
1007 static const char *eta_trans;
1008 static int bytes_cols_diff;
1009 if (eta_trans == NULL)
1010 {
1011 int nbytes;
1012 int ncols;
1013
1014 #if USE_NLS_PROGRESS_BAR
1015 eta_trans = _(eta_str);
1016 #else
1017 eta_trans = eta_str;
1018 #endif
1019
1020 /* Determine the number of bytes used in the translated string,
1021 * versus the number of columns used. This is to figure out how
1022 * many spaces to add at the end to pad to the full line width.
1023 *
1024 * We'll store the difference between the number of bytes and
1025 * number of columns, so that removing this from the string length
1026 * will reveal the total number of columns in the progress bar. */
1027 nbytes = strlen (eta_trans);
1028 ncols = count_cols (eta_trans);
1029 bytes_cols_diff = nbytes - ncols;
1030 }
1031
1032 if (bcd != NULL)
1033 *bcd = bytes_cols_diff;
1034
1035 return eta_trans;
1036 }
1037
1038 #define APPEND_LITERAL(s) do { \
1039 memcpy (p, s, sizeof (s) - 1); \
1040 p += sizeof (s) - 1; \
1041 } while (0)
1042
1043 static void
1044 create_image (struct bar_progress *bp, double dl_total_time, bool done)
1045 {
1046 const int MAX_FILENAME_COLS = bp->width / 4;
1047 char *p = bp->buffer;
1048 wgint size = bp->initial_length + bp->count;
1049
1050 struct bar_progress_hist *hist = &bp->hist;
1051 int orig_filename_cols = count_cols (bp->f_download);
1052
1053 int padding;
1054
1055 /* The progress bar should look like this:
1056 file xx% [=======> ] nnn.nnK 12.34KB/s eta 36m 51s
1057
1058 Calculate the geometry. The idea is to assign as much room as
1059 possible to the progress bar. The other idea is to never let
1060 things "jitter", i.e. pad elements that vary in size so that
1061 their variance does not affect the placement of other elements.
1062 It would be especially bad for the progress bar to be resized
1063 randomly.
1064
1065 "file " - Downloaded filename - MAX_FILENAME_COLS chars + 1
1066 "xx% " or "100%" - percentage - 4 chars
1067 "[]" - progress bar decorations - 2 chars
1068 " nnn.nnK" - downloaded bytes - 7 chars + 1
1069 " 12.5KB/s" - download rate - 8 chars + 1
1070 " eta 36m 51s" - ETA - 14 chars
1071
1072 "=====>..." - progress bar - the rest
1073 */
1074
1075 /* TODO: Ask the Turkish Translators to fix their translation for the "done"
1076 * mode of progress bar. Use one less character. Once that is done, redice
1077 * PROGRESS_ETA_LEN by 1.
1078 */
1079 #define PROGRESS_FILENAME_LEN MAX_FILENAME_COLS + 1
1080 #define PROGRESS_PERCENT_LEN 4
1081 #define PROGRESS_DECORAT_LEN 2
1082 #define PROGRESS_FILESIZE_LEN 7 + 1
1083 #define PROGRESS_DWNLOAD_RATE 8 + 2
1084 #define PROGRESS_ETA_LEN 15
1085
1086 int progress_size = bp->width - (PROGRESS_FILENAME_LEN + PROGRESS_PERCENT_LEN +
1087 PROGRESS_DECORAT_LEN + PROGRESS_FILESIZE_LEN +
1088 PROGRESS_DWNLOAD_RATE + PROGRESS_ETA_LEN);
1089
1090 /* The difference between the number of bytes used,
1091 and the number of columns used. */
1092 int bytes_cols_diff = 0;
1093 int cols_diff;
1094 const char *down_size;
1095
1096 if (progress_size < 5)
1097 progress_size = 0;
1098
1099 // sanitize input
1100 if (dl_total_time >= INT_MAX)
1101 dl_total_time = INT_MAX - 1;
1102 else if (dl_total_time < 0)
1103 dl_total_time = 0;
1104
1105 if (orig_filename_cols < MAX_FILENAME_COLS)
1106 {
1107 p += sprintf (p, "%s", bp->f_download);
1108 padding = MAX_FILENAME_COLS - orig_filename_cols + 1;
1109 memset (p, ' ', padding);
1110 p += padding;
1111 }
1112 else
1113 {
1114 /*
1115 memcpy(p, bp->f_download, MAX_FILENAME_COLS);
1116 p += MAX_FILENAME_COLS;
1117 *p++ = ' ';
1118 }
1119 */
1120 int offset_cols;
1121 int bytes_in_filename, offset_bytes, col;
1122 int *cols_ret = &col;
1123
1124 #define MIN_SCROLL_TEXT 5
1125 if ((orig_filename_cols > MAX_FILENAME_COLS + MIN_SCROLL_TEXT) &&
1126 !opt.noscroll &&
1127 !done)
1128 {
1129 offset_cols = ((int) bp->tick + orig_filename_cols + MAX_FILENAME_COLS / 2)
1130 % (orig_filename_cols + MAX_FILENAME_COLS);
1131 if (offset_cols > orig_filename_cols)
1132 {
1133 padding = MAX_FILENAME_COLS - (offset_cols - orig_filename_cols);
1134 memset(p, ' ', padding);
1135 p += padding;
1136 offset_cols = 0;
1137 }
1138 else
1139 padding = 0;
1140 }
1141 else
1142 {
1143 padding = 0;
1144 offset_cols = 0;
1145 }
1146 offset_bytes = cols_to_bytes (bp->f_download, offset_cols, cols_ret);
1147 bytes_in_filename = cols_to_bytes (bp->f_download + offset_bytes,
1148 MAX_FILENAME_COLS - padding,
1149 cols_ret);
1150 memcpy (p, bp->f_download + offset_bytes, bytes_in_filename);
1151 p += bytes_in_filename;
1152 padding = MAX_FILENAME_COLS - (padding + *cols_ret);
1153 memset (p, ' ', padding + 1);
1154 p += padding + 1;
1155 }
1156
1157
1158 /* "xx% " */
1159 if (bp->total_length > 0)
1160 {
1161 int percentage = 100.0 * size / bp->total_length;
1162 assert (percentage <= 100);
1163 p += sprintf (p, "%3d%%", percentage);
1164 }
1165 else
1166 {
1167 memset (p, ' ', PROGRESS_PERCENT_LEN);
1168 p += PROGRESS_PERCENT_LEN;
1169 }
1170
1171 /* The progress bar: "[====> ]" or "[++==> ]". */
1172 if (progress_size && bp->total_length > 0)
1173 {
1174 /* Size of the initial portion. */
1175 int insz = (double)bp->initial_length / bp->total_length * progress_size;
1176
1177 /* Size of the downloaded portion. */
1178 int dlsz = (double)size / bp->total_length * progress_size;
1179
1180 char *begin;
1181
1182 assert (dlsz <= progress_size);
1183 assert (insz <= dlsz);
1184
1185 *p++ = '[';
1186 begin = p;
1187
1188 /* Print the initial portion of the download with '+' chars, the
1189 rest with '=' and one '>'. */
1190 memset (p, '+', insz);
1191 p += insz;
1192
1193 dlsz -= insz;
1194 if (dlsz > 0)
1195 {
1196 memset (p, '=', dlsz-1);
1197 p += dlsz - 1;
1198 *p++ = '>';
1199 }
1200
1201 memset (p, ' ', (progress_size - (p - begin)));
1202 p += (progress_size - (p - begin));
1203 *p++ = ']';
1204 }
1205 else if (progress_size)
1206 {
1207 /* If we can't draw a real progress bar, then at least show
1208 *something* to the user. */
1209 int ind = bp->tick % (progress_size * 2 - 6);
1210 int i, pos;
1211
1212 /* Make the star move in two directions. */
1213 if (ind < progress_size - 2)
1214 pos = ind + 1;
1215 else
1216 pos = progress_size - (ind - progress_size + 5);
1217
1218 *p++ = '[';
1219 for (i = 0; i < progress_size; i++)
1220 {
1221 if (i == pos - 1) *p++ = '<';
1222 else if (i == pos ) *p++ = '=';
1223 else if (i == pos + 1) *p++ = '>';
1224 else
1225 *p++ = ' ';
1226 }
1227 *p++ = ']';
1228
1229 }
1230 ++bp->tick;
1231
1232 /* " 234.56M" */
1233 down_size = human_readable (size, 1000, 2);
1234 cols_diff = PROGRESS_FILESIZE_LEN - count_cols (down_size);
1235 if (cols_diff > 0)
1236 {
1237 memset (p, ' ', cols_diff);
1238 p += cols_diff;
1239 }
1240 p += sprintf (p, "%s", down_size);
1241
1242 /* " 12.52Kb/s or 12.52KB/s" */
1243 if (hist->total_time > 0 && hist->total_bytes)
1244 {
1245 static const char *short_units[] = { " B/s", "KB/s", "MB/s", "GB/s", "TB/s" };
1246 static const char *short_units_bits[] = { " b/s", "Kb/s", "Mb/s", "Gb/s", "Tb/s" };
1247 int units = 0;
1248 /* Calculate the download speed using the history ring and
1249 recent data that hasn't made it to the ring yet. */
1250 wgint dlquant = hist->total_bytes + bp->recent_bytes;
1251 double dltime = hist->total_time + (dl_total_time - bp->recent_start);
1252 double dlspeed = calc_rate (dlquant, dltime, &units);
1253 p += sprintf (p, " %4.*f%s", dlspeed >= 99.95 ? 0 : dlspeed >= 9.995 ? 1 : 2,
1254 dlspeed, !opt.report_bps ? short_units[units] : short_units_bits[units]);
1255 }
1256 else
1257 APPEND_LITERAL (" --.-KB/s");
1258
1259 if (!done)
1260 {
1261 /* " eta ..m ..s"; wait for three seconds before displaying the ETA.
1262 That's because the ETA value needs a while to become
1263 reliable. */
1264 if (bp->total_length > 0 && bp->count > 0 && dl_total_time > 3)
1265 {
1266 int eta;
1267
1268 /* Don't change the value of ETA more than approximately once
1269 per second; doing so would cause flashing without providing
1270 any value to the user. */
1271 if (bp->total_length != size
1272 && bp->last_eta_value != 0
1273 && dl_total_time - bp->last_eta_time < ETA_REFRESH_INTERVAL)
1274 eta = bp->last_eta_value;
1275 else
1276 {
1277 /* Calculate ETA using the average download speed to predict
1278 the future speed. If you want to use a speed averaged
1279 over a more recent period, replace dl_total_time with
1280 hist->total_time and bp->count with hist->total_bytes.
1281 I found that doing that results in a very jerky and
1282 ultimately unreliable ETA. */
1283 wgint bytes_remaining = bp->total_length - size;
1284 double eta_ = dl_total_time * bytes_remaining / bp->count;
1285 if (eta_ >= INT_MAX - 1)
1286 goto skip_eta;
1287 eta = (int) (eta_ + 0.5);
1288 bp->last_eta_value = eta;
1289 bp->last_eta_time = dl_total_time;
1290 }
1291
1292 p += sprintf (p, get_eta(&bytes_cols_diff),
1293 eta_to_human_short (eta, false));
1294 }
1295 else if (bp->total_length > 0)
1296 {
1297 skip_eta:
1298 memset (p, ' ', PROGRESS_ETA_LEN);
1299 p += PROGRESS_ETA_LEN;
1300 }
1301 }
1302 else
1303 {
1304 /* When the download is done, print the elapsed time. */
1305 int nbytes;
1306 int ncols;
1307
1308 /* TRANSLATORS: The meaning is "elapsed time", and it is shown
1309 * next to the progress bar once the download is done.
1310 * This should not take up more room than
1311 * available here (6 columns). Abbreviate if necessary. */
1312 strcpy (p, _(" in "));
1313 nbytes = strlen (p);
1314 ncols = count_cols (p);
1315 bytes_cols_diff = nbytes - ncols;
1316 if (dl_total_time >= 10)
1317 ncols += sprintf (p + nbytes, "%s", eta_to_human_short ((int) (dl_total_time + 0.5), false));
1318 else
1319 ncols += sprintf (p + nbytes, "%ss", print_decimal (dl_total_time));
1320 p += ncols + bytes_cols_diff;
1321 if (ncols < PROGRESS_ETA_LEN)
1322 {
1323 memset (p, ' ', PROGRESS_ETA_LEN - ncols);
1324 p += PROGRESS_ETA_LEN - ncols;
1325 }
1326 }
1327
1328 *p = '\0';
1329
1330 padding = bp->width - count_cols (bp->buffer);
1331 assert (padding >= 0 && "Padding length became non-positive!");
1332 if (padding > 0)
1333 {
1334 // if (padding > BUF_LEN - (p - bp->buffer) - 1)
1335 // padding = BUF_LEN - (p - bp->buffer) - 1;
1336 memset (p, ' ', padding);
1337 p += padding;
1338 *p = '\0';
1339 }
1340
1341 /* 2014-11-14 Darshit Shah <[email protected]>
1342 * Assert that the length of the progress bar is lesser than the size of the
1343 * screen with which we are dealing. This assertion *MUST* always be removed
1344 * from the release code since we do not want Wget to crash and burn when the
1345 * assertion fails. Instead Wget should continue downloading and display a
1346 * horrible and irritating progress bar that spams the screen with newlines.
1347 *
1348 * By default, all assertions are disabled in a Wget build and are enabled
1349 * only with the --enable-assert configure option.
1350 */
1351 assert (count_cols (bp->buffer) == bp->width);
1352 }
1353
1354 /* Print the contents of the buffer as a one-line ASCII "image" so
1355 that it can be overwritten next time. */
1356
1357 static void
1358 display_image (char *buf)
1359 {
1360 bool old = log_set_save_context (false);
1361 logputs (LOG_PROGRESS, "\r");
1362 logputs (LOG_PROGRESS, buf);
1363 log_set_save_context (old);
1364 }
1365
1366 static void
1367 bar_set_params (const char *params)
1368 {
1369 /* if run_with_timeout() will be used for read, needs to disable interactive bar,
1370 or on every timeout(1s) we will have 'retry' with error "decryption failed" */
1371 #if (defined(HAVE_LIBSSL) || defined(HAVE_LIBSSL32)) && defined(OPENSSL_RUN_WITHTIMEOUT)
1372 current_impl->interactive = false;
1373 #else
1374 current_impl->interactive = true;
1375 #endif
1376 if (params)
1377 {
1378 for (const char *param = params; *param; )
1379 {
1380 if (!strncmp (param, "force", 5))
1381 current_impl_locked = 1;
1382 else if (!strncmp (param, "noscroll", 8))
1383 opt.noscroll = true;
1384
1385 if (*(param = strchrnul(param, ':')))
1386 param++;
1387 }
1388 }
1389
1390 if (((opt.lfilename && opt.show_progress != 1)
1391 #ifdef HAVE_ISATTY
1392 /* The progress bar doesn't make sense if the output is not a
1393 TTY -- when logging to file, it is better to review the
1394 dots. */
1395 || !isatty (fileno (stderr))
1396 #endif
1397 )
1398 && !current_impl_locked)
1399 {
1400 /* We're not printing to a TTY, so revert to the fallback
1401 display. #### We're recursively calling
1402 set_progress_implementation here, which is slightly kludgy.
1403 It would be nicer if we provided that function a return value
1404 indicating a failure of some sort. */
1405 set_progress_implementation (FALLBACK_PROGRESS_IMPLEMENTATION);
1406 return;
1407 }
1408 }
1409
1410 #ifdef SIGWINCH
1411 void
1412 progress_handle_sigwinch (int sig _GL_UNUSED)
1413 {
1414 received_sigwinch = 1;
1415 signal (SIGWINCH, progress_handle_sigwinch);
1416 }
1417 #endif
1418
1419 /* Provide a short human-readable rendition of the ETA. This is like
1420 secs_to_human_time in main.c, except the output doesn't include
1421 fractions (which would look silly in by nature imprecise ETA) and
1422 takes less room. If the time is measured in hours, hours and
1423 minutes (but not seconds) are shown; if measured in days, then days
1424 and hours are shown. This ensures brevity while still displaying
1425 as much as possible.
1426
1427 If CONDENSED is true, the separator between minutes and seconds
1428 (and hours and minutes, etc.) is not included, shortening the
1429 display by one additional character. This is used for dot
1430 progress.
1431
1432 The display never occupies more than 7 characters of screen
1433 space. */
1434
1435 static const char *
1436 eta_to_human_short (int secs, bool condensed)
1437 {
1438 static char buf[16]; /* enough space to be on the safe side */
1439 static int last = -1;
1440 const char *space = condensed ? "" : " ";
1441
1442 /* Trivial optimization. create_image can call us every 200 msecs
1443 (see bar_update) for fast downloads, but ETA will only change
1444 once per 900 msecs. */
1445 if (secs == last)
1446 return buf;
1447 last = secs;
1448
1449 if (secs < 100)
1450 sprintf (buf, "%ds", secs);
1451 else if (secs < 100 * 60)
1452 sprintf (buf, "%dm%s%ds", secs / 60, space, secs % 60);
1453 else if (secs < 48 * 3600)
1454 sprintf (buf, "%dh%s%dm", secs / 3600, space, (secs / 60) % 60);
1455 else if (secs < 100 * 86400)
1456 sprintf (buf, "%dd%s%dh", secs / 86400, space, (secs / 3600) % 24);
1457 else
1458 /* even (2^31-1)/86400 doesn't overflow BUF. */
1459 sprintf (buf, "%dd", secs / 86400);
1460
1461 return buf;
1462 }