progressmeter.c (137015) | progressmeter.c (149749) |
---|---|
1/* 2 * Copyright (c) 2003 Nils Nordman. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. --- 9 unchanged lines hidden (view full) --- 18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 */ 24 25#include "includes.h" | 1/* 2 * Copyright (c) 2003 Nils Nordman. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. --- 9 unchanged lines hidden (view full) --- 18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 */ 24 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 28#include "progressmeter.h" 29#include "atomicio.h" 30#include "misc.h" 31 32#define DEFAULT_WINSIZE 80 33#define MAX_WINSIZE 512 34#define PADDING 1 /* padding between the progress indicators */ 35#define UPDATE_INTERVAL 1 /* update the progress meter every second */ 36#define STALL_TIME 5 /* we're stalled after this many seconds */ 37 38/* determines whether we can output to the terminal */ 39static int can_output(void); 40 41/* formats and inserts the specified size into the given buffer */ 42static void format_size(char *, int, off_t); 43static void format_rate(char *, int, off_t); 44 | 27 28#include "progressmeter.h" 29#include "atomicio.h" 30#include "misc.h" 31 32#define DEFAULT_WINSIZE 80 33#define MAX_WINSIZE 512 34#define PADDING 1 /* padding between the progress indicators */ 35#define UPDATE_INTERVAL 1 /* update the progress meter every second */ 36#define STALL_TIME 5 /* we're stalled after this many seconds */ 37 38/* determines whether we can output to the terminal */ 39static int can_output(void); 40 41/* formats and inserts the specified size into the given buffer */ 42static void format_size(char *, int, off_t); 43static void format_rate(char *, int, off_t); 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 */ 46void refresh_progress_meter(void); 47 48/* signal handler for updating the progress meter */ 49static void update_progress_meter(int); 50 51static time_t start; /* start progress */ 52static time_t last_update; /* last progress update */ 53static char *file; /* name of the file being transferred */ 54static off_t end_pos; /* ending position of transfer */ 55static off_t cur_pos; /* transfer position as of last refresh */ 56static volatile off_t *counter; /* progress counter */ 57static long stalled; /* how long we have been stalled */ 58static int bytes_per_second; /* current speed in bytes per second */ 59static int win_size; /* terminal window size */ | 49/* updates the progressmeter to reflect the current state of the transfer */ 50void refresh_progress_meter(void); 51 52/* signal handler for updating the progress meter */ 53static void update_progress_meter(int); 54 55static time_t start; /* start progress */ 56static time_t last_update; /* last progress update */ 57static char *file; /* name of the file being transferred */ 58static off_t end_pos; /* ending position of transfer */ 59static off_t cur_pos; /* transfer position as of last refresh */ 60static volatile off_t *counter; /* progress counter */ 61static long stalled; /* how long we have been stalled */ 62static int bytes_per_second; /* current speed in bytes per second */ 63static int win_size; /* terminal window size */ |
64static volatile sig_atomic_t win_resized; /* for window resizing */ |
|
60 61/* units for format_size */ 62static const char unit[] = " KMGT"; 63 64static int 65can_output(void) 66{ 67 return (getpgrp() == tcgetpgrp(STDOUT_FILENO)); --- 74 unchanged lines hidden (view full) --- 142 143 /* filename */ 144 buf[0] = '\0'; 145 file_len = win_size - 35; 146 if (file_len > 0) { 147 len = snprintf(buf, file_len + 1, "\r%s", file); 148 if (len < 0) 149 len = 0; | 65 66/* units for format_size */ 67static const char unit[] = " KMGT"; 68 69static int 70can_output(void) 71{ 72 return (getpgrp() == tcgetpgrp(STDOUT_FILENO)); --- 74 unchanged lines hidden (view full) --- 147 148 /* filename */ 149 buf[0] = '\0'; 150 file_len = win_size - 35; 151 if (file_len > 0) { 152 len = snprintf(buf, file_len + 1, "\r%s", file); 153 if (len < 0) 154 len = 0; |
155 if (len >= file_len + 1) 156 len = file_len; |
|
150 for (i = len; i < file_len; i++ ) 151 buf[i] = ' '; 152 buf[file_len] = '\0'; 153 } 154 155 /* percent of transfer done */ 156 if (end_pos != 0) 157 percent = ((float)cur_pos / end_pos) * 100; --- 52 unchanged lines hidden (view full) --- 210 211static void 212update_progress_meter(int ignore) 213{ 214 int save_errno; 215 216 save_errno = errno; 217 | 157 for (i = len; i < file_len; i++ ) 158 buf[i] = ' '; 159 buf[file_len] = '\0'; 160 } 161 162 /* percent of transfer done */ 163 if (end_pos != 0) 164 percent = ((float)cur_pos / end_pos) * 100; --- 52 unchanged lines hidden (view full) --- 217 218static void 219update_progress_meter(int ignore) 220{ 221 int save_errno; 222 223 save_errno = errno; 224 |
225 if (win_resized) { 226 setscreensize(); 227 win_resized = 0; 228 } |
|
218 if (can_output()) 219 refresh_progress_meter(); 220 221 signal(SIGALRM, update_progress_meter); 222 alarm(UPDATE_INTERVAL); 223 errno = save_errno; 224} 225 226void 227start_progress_meter(char *f, off_t filesize, off_t *ctr) 228{ | 229 if (can_output()) 230 refresh_progress_meter(); 231 232 signal(SIGALRM, update_progress_meter); 233 alarm(UPDATE_INTERVAL); 234 errno = save_errno; 235} 236 237void 238start_progress_meter(char *f, off_t filesize, off_t *ctr) 239{ |
229 struct winsize winsize; 230 | |
231 start = last_update = time(NULL); 232 file = f; 233 end_pos = filesize; 234 cur_pos = 0; 235 counter = ctr; 236 stalled = 0; 237 bytes_per_second = 0; 238 | 240 start = last_update = time(NULL); 241 file = f; 242 end_pos = filesize; 243 cur_pos = 0; 244 counter = ctr; 245 stalled = 0; 246 bytes_per_second = 0; 247 |
239 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 && 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 | 248 setscreensize(); |
249 if (can_output()) 250 refresh_progress_meter(); 251 252 signal(SIGALRM, update_progress_meter); | 249 if (can_output()) 250 refresh_progress_meter(); 251 252 signal(SIGALRM, update_progress_meter); |
253 signal(SIGWINCH, sig_winch); |
|
253 alarm(UPDATE_INTERVAL); 254} 255 256void 257stop_progress_meter(void) 258{ 259 alarm(0); 260 261 if (!can_output()) 262 return; 263 264 /* Ensure we complete the progress */ 265 if (cur_pos != end_pos) 266 refresh_progress_meter(); 267 268 atomicio(vwrite, STDOUT_FILENO, "\n", 1); 269} | 254 alarm(UPDATE_INTERVAL); 255} 256 257void 258stop_progress_meter(void) 259{ 260 alarm(0); 261 262 if (!can_output()) 263 return; 264 265 /* Ensure we complete the progress */ 266 if (cur_pos != end_pos) 267 refresh_progress_meter(); 268 269 atomicio(vwrite, STDOUT_FILENO, "\n", 1); 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} |
|