summaryrefslogtreecommitdiff
path: root/progressmeter.c
diff options
context:
space:
mode:
authorColin Watson <cjwatson@debian.org>2005-09-14 12:45:47 +0000
committerColin Watson <cjwatson@debian.org>2005-09-14 12:45:47 +0000
commit9b71add4cecf753c45f5fbd6ff0913bc95b3e95d (patch)
treed4ea8fdb30c7949c6433f5277c39548ea579d4dc /progressmeter.c
parented07bcbea56007ab5b218ddf3aa6a7d4e21966e0 (diff)
parent16704d57999d987fb8d9ba53379841a79f016d67 (diff)
Merge 4.2p1 to the trunk.
Diffstat (limited to 'progressmeter.c')
-rw-r--r--progressmeter.c49
1 files changed, 36 insertions, 13 deletions
diff --git a/progressmeter.c b/progressmeter.c
index 93f5a3e62..3cda09061 100644
--- a/progressmeter.c
+++ b/progressmeter.c
@@ -23,7 +23,7 @@
23 */ 23 */
24 24
25#include "includes.h" 25#include "includes.h"
26RCSID("$OpenBSD: progressmeter.c,v 1.22 2004/07/11 17:48:47 deraadt Exp $"); 26RCSID("$OpenBSD: progressmeter.c,v 1.24 2005/06/07 13:25:23 jaredy Exp $");
27 27
28#include "progressmeter.h" 28#include "progressmeter.h"
29#include "atomicio.h" 29#include "atomicio.h"
@@ -42,6 +42,10 @@ static int can_output(void);
42static void format_size(char *, int, off_t); 42static void format_size(char *, int, off_t);
43static void format_rate(char *, int, off_t); 43static void format_rate(char *, int, off_t);
44 44
45/* window resizing */
46static void sig_winch(int);
47static void setscreensize(void);
48
45/* updates the progressmeter to reflect the current state of the transfer */ 49/* updates the progressmeter to reflect the current state of the transfer */
46void refresh_progress_meter(void); 50void refresh_progress_meter(void);
47 51
@@ -57,6 +61,7 @@ static volatile off_t *counter; /* progress counter */
57static long stalled; /* how long we have been stalled */ 61static long stalled; /* how long we have been stalled */
58static int bytes_per_second; /* current speed in bytes per second */ 62static int bytes_per_second; /* current speed in bytes per second */
59static int win_size; /* terminal window size */ 63static int win_size; /* terminal window size */
64static volatile sig_atomic_t win_resized; /* for window resizing */
60 65
61/* units for format_size */ 66/* units for format_size */
62static const char unit[] = " KMGT"; 67static const char unit[] = " KMGT";
@@ -147,6 +152,8 @@ refresh_progress_meter(void)
147 len = snprintf(buf, file_len + 1, "\r%s", file); 152 len = snprintf(buf, file_len + 1, "\r%s", file);
148 if (len < 0) 153 if (len < 0)
149 len = 0; 154 len = 0;
155 if (len >= file_len + 1)
156 len = file_len;
150 for (i = len; i < file_len; i++ ) 157 for (i = len; i < file_len; i++ )
151 buf[i] = ' '; 158 buf[i] = ' ';
152 buf[file_len] = '\0'; 159 buf[file_len] = '\0';
@@ -215,6 +222,10 @@ update_progress_meter(int ignore)
215 222
216 save_errno = errno; 223 save_errno = errno;
217 224
225 if (win_resized) {
226 setscreensize();
227 win_resized = 0;
228 }
218 if (can_output()) 229 if (can_output())
219 refresh_progress_meter(); 230 refresh_progress_meter();
220 231
@@ -226,8 +237,6 @@ update_progress_meter(int ignore)
226void 237void
227start_progress_meter(char *f, off_t filesize, off_t *ctr) 238start_progress_meter(char *f, off_t filesize, off_t *ctr)
228{ 239{
229 struct winsize winsize;
230
231 start = last_update = time(NULL); 240 start = last_update = time(NULL);
232 file = f; 241 file = f;
233 end_pos = filesize; 242 end_pos = filesize;
@@ -236,20 +245,12 @@ start_progress_meter(char *f, off_t filesize, off_t *ctr)
236 stalled = 0; 245 stalled = 0;
237 bytes_per_second = 0; 246 bytes_per_second = 0;
238 247
239 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 && 248 setscreensize();
240 winsize.ws_col != 0) {
241 if (winsize.ws_col > MAX_WINSIZE)
242 win_size = MAX_WINSIZE;
243 else
244 win_size = winsize.ws_col;
245 } else
246 win_size = DEFAULT_WINSIZE;
247 win_size += 1; /* trailing \0 */
248
249 if (can_output()) 249 if (can_output())
250 refresh_progress_meter(); 250 refresh_progress_meter();
251 251
252 signal(SIGALRM, update_progress_meter); 252 signal(SIGALRM, update_progress_meter);
253 signal(SIGWINCH, sig_winch);
253 alarm(UPDATE_INTERVAL); 254 alarm(UPDATE_INTERVAL);
254} 255}
255 256
@@ -267,3 +268,25 @@ stop_progress_meter(void)
267 268
268 atomicio(vwrite, STDOUT_FILENO, "\n", 1); 269 atomicio(vwrite, STDOUT_FILENO, "\n", 1);
269} 270}
271
272static void
273sig_winch(int sig)
274{
275 win_resized = 1;
276}
277
278static void
279setscreensize(void)
280{
281 struct winsize winsize;
282
283 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
284 winsize.ws_col != 0) {
285 if (winsize.ws_col > MAX_WINSIZE)
286 win_size = MAX_WINSIZE;
287 else
288 win_size = winsize.ws_col;
289 } else
290 win_size = DEFAULT_WINSIZE;
291 win_size += 1; /* trailing \0 */
292}