1 /* vi: set sw=4 ts=4: */
5 /* Original copyright notice which applies to the CONFIG_FEATURE_WGET_STATUSBAR stuff,
6 * much of which was blatantly stolen from openssh.
9 * Copyright (c) 1992, 1993
10 * The Regents of the University of California. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * 3. BSD Advertising Clause omitted per the July 22, 1999 licensing change
22 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 /* Seconds when xfer considered "stalled" */
48 void FAST_FUNC
bb_progress_init(bb_progress_t
*p
, const char *curfile
)
50 #if ENABLE_UNICODE_SUPPORT
52 p
->curfile
= unicode_conv_to_printable_fixedwidth(/*NULL,*/ curfile
, 20);
56 p
->start_sec
= monotonic_sec();
57 p
->last_update_sec
= p
->start_sec
;
58 p
->last_change_sec
= p
->start_sec
;
62 void FAST_FUNC
bb_progress_update(bb_progress_t
*p
,
67 uoff_t beg_and_transferred
;
68 unsigned since_last_update
, elapsed
;
72 //transferred = 1234; /* use for stall detection testing */
73 //totalsize = 0; /* use for unknown size download testing */
75 elapsed
= monotonic_sec();
76 since_last_update
= elapsed
- p
->last_update_sec
;
77 p
->last_update_sec
= elapsed
;
79 if (totalsize
!= 0 && transferred
>= totalsize
- beg_size
) {
80 /* Last call. Do not skip this update */
81 transferred
= totalsize
- beg_size
; /* sanitize just in case */
83 else if (since_last_update
== 0) {
85 * Do not update on every call
86 * (we can be called on every network read!)
93 * Scale sizes down if they are close to overflowing.
94 * This allows calculations like (100 * transferred / totalsize)
95 * without risking overflow: we guarantee 10 highest bits to be 0.
96 * Introduced error is less than 1 / 2^12 ~= 0.025%
98 if (ULONG_MAX
> 0xffffffff || sizeof(off_t
) == 4 || sizeof(off_t
) != 8) {
100 * 64-bit CPU || small off_t: in either case,
101 * >> is cheap, single-word operation.
102 * ... || strange off_t: also use this code
103 * (it is safe, just suboptimal wrt code size),
104 * because 32/64 optimized one works only for 64-bit off_t.
106 if (totalsize
>= (1 << 22)) {
113 /* 32-bit CPU and 64-bit off_t.
114 * Use a 40-bit shift, it is easier to do on 32-bit CPU.
116 /* ONE suppresses "warning: shift count >= width of type" */
117 #define ONE (sizeof(off_t) > 4)
118 if (totalsize
>= (uoff_t
)(1ULL << 54*ONE
)) {
119 totalsize
= (uint32_t)(totalsize
>> 32*ONE
) >> 8;
120 beg_size
= (uint32_t)(beg_size
>> 32*ONE
) >> 8;
121 transferred
= (uint32_t)(transferred
>> 32*ONE
) >> 8;
126 notty
= !isatty(STDERR_FILENO
);
128 if (ENABLE_UNICODE_SUPPORT
)
129 fprintf(stderr
, "\r%s" + notty
, p
->curfile
);
131 fprintf(stderr
, "\r%-20.20s" + notty
, p
->curfile
);
133 beg_and_transferred
= beg_size
+ transferred
;
135 if (totalsize
!= 0) {
137 unsigned ratio
= 100 * beg_and_transferred
/ totalsize
;
138 fprintf(stderr
, "%4u%%", ratio
);
140 barlength
= get_terminal_width(2) - 49;
142 /* god bless gcc for variable arrays :) */
143 char buf
[barlength
+ 1];
144 unsigned stars
= (unsigned)barlength
* beg_and_transferred
/ totalsize
;
145 memset(buf
, ' ', barlength
);
146 buf
[barlength
] = '\0';
147 memset(buf
, '*', stars
);
148 fprintf(stderr
, " |%s|", buf
);
152 while (beg_and_transferred
>= 100000) {
153 beg_and_transferred
>>= 10;
156 /* see http://en.wikipedia.org/wiki/Tera */
157 fprintf(stderr
, "%6u%c", (unsigned)beg_and_transferred
, " kMGTPEZY"[kiloscale
]);
158 #define beg_and_transferred dont_use_beg_and_transferred_below()
160 since_last_update
= elapsed
- p
->last_change_sec
;
161 if ((unsigned)transferred
!= p
->last_size
) {
162 p
->last_change_sec
= elapsed
;
163 p
->last_size
= (unsigned)transferred
;
164 if (since_last_update
>= STALLTIME
) {
165 /* We "cut out" these seconds from elapsed time
166 * by adjusting start time */
167 p
->start_sec
+= since_last_update
;
169 since_last_update
= 0; /* we are un-stalled now */
171 elapsed
-= p
->start_sec
; /* now it's "elapsed since start" */
173 if (since_last_update
>= STALLTIME
) {
174 fprintf(stderr
, " - stalled -");
175 } else if (!totalsize
|| !transferred
|| (int)elapsed
< 0) {
176 fprintf(stderr
, " --:--:-- ETA");
178 unsigned eta
, secs
, hours
;
180 totalsize
-= beg_size
; /* now it's "total to upload" */
182 /* Estimated remaining time =
183 * estimated_sec_to_dl_totalsize_bytes - elapsed_sec =
184 * totalsize / average_bytes_sec_so_far - elapsed =
185 * totalsize / (transferred/elapsed) - elapsed =
186 * totalsize * elapsed / transferred - elapsed
188 eta
= totalsize
* elapsed
/ transferred
- elapsed
;
189 if (eta
>= 1000*60*60)
190 eta
= 1000*60*60 - 1;
193 fprintf(stderr
, "%3u:%02u:%02u ETA", hours
, secs
/ 60, secs
% 60);