Magellan Linux

Contents of /tags/mkinitrd-6_4_0/busybox/libbb/progress.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1313 - (show annotations) (download)
Fri May 27 18:20:23 2011 UTC (13 years ago) by niro
File MIME type: text/plain
File size: 4923 byte(s)
tagged 'mkinitrd-6_4_0'
1 /* vi: set sw=4 ts=4: */
2 /*
3 * Progress bar code.
4 */
5 /* Original copyright notice which applies to the CONFIG_FEATURE_WGET_STATUSBAR stuff,
6 * much of which was blatantly stolen from openssh.
7 */
8 /*-
9 * Copyright (c) 1992, 1993
10 * The Regents of the University of California. All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
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.
20 *
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>
23 *
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.
27 *
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
38 * SUCH DAMAGE.
39 */
40 #include "libbb.h"
41 #include "unicode.h"
42
43 enum {
44 /* Seconds when xfer considered "stalled" */
45 STALLTIME = 5
46 };
47
48 static unsigned int get_tty2_width(void)
49 {
50 unsigned width;
51 get_terminal_width_height(2, &width, NULL);
52 return width;
53 }
54
55 void FAST_FUNC bb_progress_init(bb_progress_t *p)
56 {
57 p->start_sec = monotonic_sec();
58 p->lastupdate_sec = p->start_sec;
59 p->lastsize = 0;
60 p->inited = 1;
61 }
62
63 void FAST_FUNC bb_progress_update(bb_progress_t *p,
64 const char *curfile,
65 off_t beg_range,
66 off_t transferred,
67 off_t totalsize)
68 {
69 off_t abbrevsize;
70 unsigned since_last_update, elapsed;
71 unsigned ratio;
72 int barlength, i;
73
74 ratio = 100;
75 if (totalsize) {
76 /* long long helps to have it working even if !LFS */
77 ratio = (unsigned) (100ULL * (transferred+beg_range) / totalsize);
78 if (ratio > 100) ratio = 100;
79 }
80
81 #if ENABLE_UNICODE_SUPPORT
82 init_unicode();
83 /* libbb candidate? */
84 {
85 wchar_t wbuf21[21];
86 char *buf = xstrdup(curfile);
87 unsigned len;
88
89 /* trim to 20 wide chars max (sets wbuf21[20] to 0)
90 * also, in case mbstowcs fails, we at least
91 * dont get garbage */
92 memset(wbuf21, 0, sizeof(wbuf21));
93 /* convert to wide chars, no more than 20 */
94 len = mbstowcs(wbuf21, curfile, 20); /* NB: may return -1 */
95 /* back to multibyte; cant overflow */
96 wcstombs(buf, wbuf21, INT_MAX);
97 len = (len > 20) ? 0 : 20 - len;
98 fprintf(stderr, "\r%s%*s%4d%% ", buf, len, "", ratio);
99 free(buf);
100 }
101 #else
102 fprintf(stderr, "\r%-20.20s%4d%% ", curfile, ratio);
103 #endif
104
105 barlength = get_tty2_width() - 49;
106 if (barlength > 0) {
107 /* god bless gcc for variable arrays :) */
108 i = barlength * ratio / 100;
109 {
110 char buf[i+1];
111 memset(buf, '*', i);
112 buf[i] = '\0';
113 fprintf(stderr, "|%s%*s|", buf, barlength - i, "");
114 }
115 }
116 i = 0;
117 abbrevsize = transferred + beg_range;
118 while (abbrevsize >= 100000) {
119 i++;
120 abbrevsize >>= 10;
121 }
122 /* see http://en.wikipedia.org/wiki/Tera */
123 fprintf(stderr, "%6d%c ", (int)abbrevsize, " kMGTPEZY"[i]);
124
125 elapsed = monotonic_sec();
126 since_last_update = elapsed - p->lastupdate_sec;
127 if (transferred > p->lastsize) {
128 p->lastupdate_sec = elapsed;
129 p->lastsize = transferred;
130 if (since_last_update >= STALLTIME) {
131 /* We "cut off" these seconds from elapsed time
132 * by adjusting start time */
133 p->start_sec += since_last_update;
134 }
135 since_last_update = 0; /* we are un-stalled now */
136 }
137 elapsed -= p->start_sec; /* now it's "elapsed since start" */
138
139 if (since_last_update >= STALLTIME) {
140 fprintf(stderr, " - stalled -");
141 } else {
142 off_t to_download = totalsize - beg_range;
143 if (!totalsize || transferred <= 0 || (int)elapsed <= 0 || transferred > to_download) {
144 fprintf(stderr, "--:--:-- ETA");
145 } else {
146 /* to_download / (transferred/elapsed) - elapsed: */
147 int eta = (int) ((unsigned long long)to_download*elapsed/transferred - elapsed);
148 /* (long long helps to have working ETA even if !LFS) */
149 i = eta % 3600;
150 fprintf(stderr, "%02d:%02d:%02d ETA", eta / 3600, i / 60, i % 60);
151 }
152 }
153 }