summaryrefslogtreecommitdiff
path: root/progressmeter.c
diff options
context:
space:
mode:
Diffstat (limited to 'progressmeter.c')
-rw-r--r--progressmeter.c60
1 files changed, 26 insertions, 34 deletions
diff --git a/progressmeter.c b/progressmeter.c
index fe9bf52e4..e385c1254 100644
--- a/progressmeter.c
+++ b/progressmeter.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: progressmeter.c,v 1.45 2016/06/30 05:17:05 dtucker Exp $ */ 1/* $OpenBSD: progressmeter.c,v 1.47 2019/01/24 16:52:17 dtucker Exp $ */
2/* 2/*
3 * Copyright (c) 2003 Nils Nordman. All rights reserved. 3 * Copyright (c) 2003 Nils Nordman. All rights reserved.
4 * 4 *
@@ -31,6 +31,7 @@
31 31
32#include <errno.h> 32#include <errno.h>
33#include <signal.h> 33#include <signal.h>
34#include <stdarg.h>
34#include <stdio.h> 35#include <stdio.h>
35#include <string.h> 36#include <string.h>
36#include <time.h> 37#include <time.h>
@@ -39,6 +40,7 @@
39#include "progressmeter.h" 40#include "progressmeter.h"
40#include "atomicio.h" 41#include "atomicio.h"
41#include "misc.h" 42#include "misc.h"
43#include "utf8.h"
42 44
43#define DEFAULT_WINSIZE 80 45#define DEFAULT_WINSIZE 80
44#define MAX_WINSIZE 512 46#define MAX_WINSIZE 512
@@ -57,11 +59,8 @@ static void format_rate(char *, int, off_t);
57static void sig_winch(int); 59static void sig_winch(int);
58static void setscreensize(void); 60static void setscreensize(void);
59 61
60/* updates the progressmeter to reflect the current state of the transfer */
61void refresh_progress_meter(void);
62
63/* signal handler for updating the progress meter */ 62/* signal handler for updating the progress meter */
64static void update_progress_meter(int); 63static void sig_alarm(int);
65 64
66static double start; /* start progress */ 65static double start; /* start progress */
67static double last_update; /* last progress update */ 66static double last_update; /* last progress update */
@@ -74,6 +73,7 @@ static long stalled; /* how long we have been stalled */
74static int bytes_per_second; /* current speed in bytes per second */ 73static int bytes_per_second; /* current speed in bytes per second */
75static int win_size; /* terminal window size */ 74static int win_size; /* terminal window size */
76static volatile sig_atomic_t win_resized; /* for window resizing */ 75static volatile sig_atomic_t win_resized; /* for window resizing */
76static volatile sig_atomic_t alarm_fired;
77 77
78/* units for format_size */ 78/* units for format_size */
79static const char unit[] = " KMGT"; 79static const char unit[] = " KMGT";
@@ -117,7 +117,7 @@ format_size(char *buf, int size, off_t bytes)
117} 117}
118 118
119void 119void
120refresh_progress_meter(void) 120refresh_progress_meter(int force_update)
121{ 121{
122 char buf[MAX_WINSIZE + 1]; 122 char buf[MAX_WINSIZE + 1];
123 off_t transferred; 123 off_t transferred;
@@ -126,9 +126,17 @@ refresh_progress_meter(void)
126 off_t bytes_left; 126 off_t bytes_left;
127 int cur_speed; 127 int cur_speed;
128 int hours, minutes, seconds; 128 int hours, minutes, seconds;
129 int i, len;
130 int file_len; 129 int file_len;
131 130
131 if ((!force_update && !alarm_fired && !win_resized) || !can_output())
132 return;
133 alarm_fired = 0;
134
135 if (win_resized) {
136 setscreensize();
137 win_resized = 0;
138 }
139
132 transferred = *counter - (cur_pos ? cur_pos : start_pos); 140 transferred = *counter - (cur_pos ? cur_pos : start_pos);
133 cur_pos = *counter; 141 cur_pos = *counter;
134 now = monotime_double(); 142 now = monotime_double();
@@ -158,16 +166,11 @@ refresh_progress_meter(void)
158 166
159 /* filename */ 167 /* filename */
160 buf[0] = '\0'; 168 buf[0] = '\0';
161 file_len = win_size - 35; 169 file_len = win_size - 36;
162 if (file_len > 0) { 170 if (file_len > 0) {
163 len = snprintf(buf, file_len + 1, "\r%s", file); 171 buf[0] = '\r';
164 if (len < 0) 172 snmprintf(buf+1, sizeof(buf)-1 , &file_len, "%*s",
165 len = 0; 173 file_len * -1, file);
166 if (len >= file_len + 1)
167 len = file_len;
168 for (i = len; i < file_len; i++)
169 buf[i] = ' ';
170 buf[file_len] = '\0';
171 } 174 }
172 175
173 /* percent of transfer done */ 176 /* percent of transfer done */
@@ -228,22 +231,11 @@ refresh_progress_meter(void)
228 231
229/*ARGSUSED*/ 232/*ARGSUSED*/
230static void 233static void
231update_progress_meter(int ignore) 234sig_alarm(int ignore)
232{ 235{
233 int save_errno; 236 signal(SIGALRM, sig_alarm);
234 237 alarm_fired = 1;
235 save_errno = errno;
236
237 if (win_resized) {
238 setscreensize();
239 win_resized = 0;
240 }
241 if (can_output())
242 refresh_progress_meter();
243
244 signal(SIGALRM, update_progress_meter);
245 alarm(UPDATE_INTERVAL); 238 alarm(UPDATE_INTERVAL);
246 errno = save_errno;
247} 239}
248 240
249void 241void
@@ -259,10 +251,9 @@ start_progress_meter(const char *f, off_t filesize, off_t *ctr)
259 bytes_per_second = 0; 251 bytes_per_second = 0;
260 252
261 setscreensize(); 253 setscreensize();
262 if (can_output()) 254 refresh_progress_meter(1);
263 refresh_progress_meter();
264 255
265 signal(SIGALRM, update_progress_meter); 256 signal(SIGALRM, sig_alarm);
266 signal(SIGWINCH, sig_winch); 257 signal(SIGWINCH, sig_winch);
267 alarm(UPDATE_INTERVAL); 258 alarm(UPDATE_INTERVAL);
268} 259}
@@ -277,7 +268,7 @@ stop_progress_meter(void)
277 268
278 /* Ensure we complete the progress */ 269 /* Ensure we complete the progress */
279 if (cur_pos != end_pos) 270 if (cur_pos != end_pos)
280 refresh_progress_meter(); 271 refresh_progress_meter(1);
281 272
282 atomicio(vwrite, STDOUT_FILENO, "\n", 1); 273 atomicio(vwrite, STDOUT_FILENO, "\n", 1);
283} 274}
@@ -286,6 +277,7 @@ stop_progress_meter(void)
286static void 277static void
287sig_winch(int sig) 278sig_winch(int sig)
288{ 279{
280 signal(SIGWINCH, sig_winch);
289 win_resized = 1; 281 win_resized = 1;
290} 282}
291 283