OpenCoverage

progressmeter.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/openssh/src/progressmeter.c
Switch to Source codePreprocessed file
LineSourceCount
1-
2-
3-
4-
5-
6-
7-
8static int can_output(void);-
9-
10-
11static void format_size(char *, int, off_t);-
12static void format_rate(char *, int, off_t);-
13-
14-
15static void sig_winch(int);-
16static void setscreensize(void);-
17-
18-
19void refresh_progress_meter(void);-
20-
21-
22static void update_progress_meter(int);-
23-
24static double start;-
25static double last_update;-
26static const char *file;-
27static off_t start_pos;-
28static off_t end_pos;-
29static off_t cur_pos;-
30static volatile off_t *counter;-
31static long stalled;-
32static int bytes_per_second;-
33static int win_size;-
34static volatile sig_atomic_t win_resized;-
35-
36-
37static const char unit[] = " KMGT";-
38-
39static int-
40can_output(void)-
41{-
42 return
never executed: return (getpgrp() == tcgetpgrp( 1 ));
(getpgrp() == tcgetpgrp(
never executed: return (getpgrp() == tcgetpgrp( 1 ));
0
43 1
never executed: return (getpgrp() == tcgetpgrp( 1 ));
0
44 ));
never executed: return (getpgrp() == tcgetpgrp( 1 ));
0
45}-
46-
47static void-
48format_rate(char *buf, int size, off_t bytes)-
49{-
50 int i;-
51-
52 bytes *= 100;-
53 for (i = 0; bytes >= 100*1000
bytes >= 100*1000Description
TRUEnever evaluated
FALSEnever evaluated
&& unit[i] != 'T'
unit[i] != 'T'Description
TRUEnever evaluated
FALSEnever evaluated
; i++)
0
54 bytes = (bytes + 512) / 1024;
never executed: bytes = (bytes + 512) / 1024;
0
55 if (i == 0
i == 0Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
56 i++;-
57 bytes = (bytes + 512) / 1024;-
58 }
never executed: end of block
0
59 snprintf(buf, size, "%3lld.%1lld%c%s",-
60 (long long) (bytes + 5) / 100,-
61 (long long) (bytes + 5) / 10 % 10,-
62 unit[i],-
63 i ? "B" : " ");-
64}
never executed: end of block
0
65-
66static void-
67format_size(char *buf, int size, off_t bytes)-
68{-
69 int i;-
70-
71 for (i = 0; bytes >= 10000
bytes >= 10000Description
TRUEnever evaluated
FALSEnever evaluated
&& unit[i] != 'T'
unit[i] != 'T'Description
TRUEnever evaluated
FALSEnever evaluated
; i++)
0
72 bytes = (bytes + 512) / 1024;
never executed: bytes = (bytes + 512) / 1024;
0
73 snprintf(buf, size, "%4lld%c%s",-
74 (long long) bytes,-
75 unit[i],-
76 i ? "B" : " ");-
77}
never executed: end of block
0
78-
79void-
80refresh_progress_meter(void)-
81{-
82 char buf[512 + 1];-
83 off_t transferred;-
84 double elapsed, now;-
85 int percent;-
86 off_t bytes_left;-
87 int cur_speed;-
88 int hours, minutes, seconds;-
89 int i, len;-
90 int file_len;-
91-
92 transferred = *counter - (cur_pos
cur_posDescription
TRUEnever evaluated
FALSEnever evaluated
? cur_pos : start_pos);
0
93 cur_pos = *counter;-
94 now = monotime_double();-
95 bytes_left = end_pos - cur_pos;-
96-
97 if (bytes_left > 0
bytes_left > 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0
98 elapsed = now - last_update;
never executed: elapsed = now - last_update;
0
99 else {-
100 elapsed = now - start;-
101-
102 transferred = end_pos - start_pos;-
103 bytes_per_second = 0;-
104 }
never executed: end of block
0
105-
106-
107 if (elapsed != 0
elapsed != 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0
108 cur_speed = (transferred / elapsed);
never executed: cur_speed = (transferred / elapsed);
0
109 else-
110 cur_speed = transferred;
never executed: cur_speed = transferred;
0
111-
112-
113 if (bytes_per_second != 0
bytes_per_second != 0Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
114 bytes_per_second = (bytes_per_second * 0.9) +-
115 (cur_speed * (1.0 - 0.9));-
116 }
never executed: end of block
else
0
117 bytes_per_second = cur_speed;
never executed: bytes_per_second = cur_speed;
0
118-
119-
120 buf[0] = '\0';-
121 file_len = win_size - 35;-
122 if (file_len > 0
file_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
123 len = snprintf(buf, file_len + 1, "\r%s", file);-
124 if (len < 0
len < 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0
125 len = 0;
never executed: len = 0;
0
126 if (len >= file_len + 1
len >= file_len + 1Description
TRUEnever evaluated
FALSEnever evaluated
)
0
127 len = file_len;
never executed: len = file_len;
0
128 for (i = len; i < file_len
i < file_lenDescription
TRUEnever evaluated
FALSEnever evaluated
; i++)
0
129 buf[i] = ' ';
never executed: buf[i] = ' ';
0
130 buf[file_len] = '\0';-
131 }
never executed: end of block
0
132-
133-
134 if (end_pos == 0
end_pos == 0Description
TRUEnever evaluated
FALSEnever evaluated
|| cur_pos == end_pos
cur_pos == end_posDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
135 percent = 100;
never executed: percent = 100;
0
136 else-
137 percent = ((float)cur_pos / end_pos) * 100;
never executed: percent = ((float)cur_pos / end_pos) * 100;
0
138 snprintf(buf + strlen(buf), win_size - strlen(buf),-
139 " %3d%% ", percent);-
140-
141-
142 format_size(buf + strlen(buf), win_size - strlen(buf),-
143 cur_pos);-
144 strlcat(buf, " ", win_size);-
145-
146-
147 format_rate(buf + strlen(buf), win_size - strlen(buf),-
148 (off_t)bytes_per_second);-
149 strlcat(buf, "/s ", win_size);-
150-
151-
152 if (!transferred
!transferredDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
153 stalled += elapsed;
never executed: stalled += elapsed;
0
154 else-
155 stalled = 0;
never executed: stalled = 0;
0
156-
157 if (stalled >= 5
stalled >= 5Description
TRUEnever evaluated
FALSEnever evaluated
)
0
158 strlcat(buf, "- stalled -", win_size);
never executed: strlcat(buf, "- stalled -", win_size);
0
159 else if (bytes_per_second == 0
bytes_per_second == 0Description
TRUEnever evaluated
FALSEnever evaluated
&& bytes_left
bytes_leftDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
160 strlcat(buf, " --:-- ETA", win_size);
never executed: strlcat(buf, " --:-- ETA", win_size);
0
161 else {-
162 if (bytes_left > 0
bytes_left > 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0
163 seconds = bytes_left / bytes_per_second;
never executed: seconds = bytes_left / bytes_per_second;
0
164 else-
165 seconds = elapsed;
never executed: seconds = elapsed;
0
166-
167 hours = seconds / 3600;-
168 seconds -= hours * 3600;-
169 minutes = seconds / 60;-
170 seconds -= minutes * 60;-
171-
172 if (hours != 0
hours != 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0
173 snprintf(buf + strlen(buf), win_size - strlen(buf),
never executed: snprintf(buf + strlen(buf), win_size - strlen(buf), "%d:%02d:%02d", hours, minutes, seconds);
0
174 "%d:%02d:%02d", hours, minutes, seconds);
never executed: snprintf(buf + strlen(buf), win_size - strlen(buf), "%d:%02d:%02d", hours, minutes, seconds);
0
175 else-
176 snprintf(buf + strlen(buf), win_size - strlen(buf),
never executed: snprintf(buf + strlen(buf), win_size - strlen(buf), " %02d:%02d", minutes, seconds);
0
177 " %02d:%02d", minutes, seconds);
never executed: snprintf(buf + strlen(buf), win_size - strlen(buf), " %02d:%02d", minutes, seconds);
0
178-
179 if (bytes_left > 0
bytes_left > 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0
180 strlcat(buf, " ETA", win_size);
never executed: strlcat(buf, " ETA", win_size);
0
181 else-
182 strlcat(buf, " ", win_size);
never executed: strlcat(buf, " ", win_size);
0
183 }-
184-
185 atomicio((ssize_t (*)(int, void *, size_t))write, -
186 1-
187 , buf, win_size - 1);-
188 last_update = now;-
189}
never executed: end of block
0
190-
191-
192static void-
193update_progress_meter(int ignore)-
194{-
195 int save_errno;-
196-
197 save_errno = -
198 (*__errno_location ())-
199 ;-
200-
201 if (win_resized
win_resizedDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
202 setscreensize();-
203 win_resized = 0;-
204 }
never executed: end of block
0
205 if (can_output()
can_output()Description
TRUEnever evaluated
FALSEnever evaluated
)
0
206 refresh_progress_meter();
never executed: refresh_progress_meter();
0
207-
208 mysignal(-
20914-
210,update_progress_meter);-
211 alarm(1);-
212 -
213(*__errno_location ()) -
214 = save_errno;-
215}
never executed: end of block
0
216-
217void-
218start_progress_meter(const char *f, off_t filesize, off_t *ctr)-
219{-
220 start = last_update = monotime_double();-
221 file = f;-
222 start_pos = *ctr;-
223 end_pos = filesize;-
224 cur_pos = 0;-
225 counter = ctr;-
226 stalled = 0;-
227 bytes_per_second = 0;-
228-
229 setscreensize();-
230 if (can_output()
can_output()Description
TRUEnever evaluated
FALSEnever evaluated
)
0
231 refresh_progress_meter();
never executed: refresh_progress_meter();
0
232-
233 mysignal(-
23414-
235,update_progress_meter);-
236 mysignal(-
23728-
238,sig_winch);-
239 alarm(1);-
240}
never executed: end of block
0
241-
242void-
243stop_progress_meter(void)-
244{-
245 alarm(0);-
246-
247 if (!can_output()
!can_output()Description
TRUEnever evaluated
FALSEnever evaluated
)
0
248 return;
never executed: return;
0
249-
250-
251 if (cur_pos != end_pos
cur_pos != end_posDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
252 refresh_progress_meter();
never executed: refresh_progress_meter();
0
253-
254 atomicio((ssize_t (*)(int, void *, size_t))write, -
255 1-
256 , "\n", 1);-
257}
never executed: end of block
0
258-
259-
260static void-
261sig_winch(int sig)-
262{-
263 win_resized = 1;-
264}
never executed: end of block
0
265-
266static void-
267setscreensize(void)-
268{-
269 struct winsize winsize;-
270-
271 if (ioctl(
ioctl( 1 , 0x5...winsize) != -1Description
TRUEnever evaluated
FALSEnever evaluated
0
272 1
ioctl( 1 , 0x5...winsize) != -1Description
TRUEnever evaluated
FALSEnever evaluated
0
273 ,
ioctl( 1 , 0x5...winsize) != -1Description
TRUEnever evaluated
FALSEnever evaluated
0
274 0x5413
ioctl( 1 , 0x5...winsize) != -1Description
TRUEnever evaluated
FALSEnever evaluated
0
275 , &winsize) != -1
ioctl( 1 , 0x5...winsize) != -1Description
TRUEnever evaluated
FALSEnever evaluated
&&
0
276 winsize.ws_col != 0
winsize.ws_col != 0Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
277 if (winsize.ws_col > 512
winsize.ws_col > 512Description
TRUEnever evaluated
FALSEnever evaluated
)
0
278 win_size = 512;
never executed: win_size = 512;
0
279 else-
280 win_size = winsize.ws_col;
never executed: win_size = winsize.ws_col;
0
281 } else-
282 win_size = 80;
never executed: win_size = 80;
0
283 win_size += 1;-
284}
never executed: end of block
0
Switch to Source codePreprocessed file

Generated by Squish Coco 4.2.2