2 * Copyright (c) 2003 Nils Nordman. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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.
26 RCSID("$OpenBSD: progressmeter.c,v 1.25 2006/02/10 00:27:13 stevesk Exp $");
28 #include <sys/ioctl.h>
30 #include "progressmeter.h"
34 #define DEFAULT_WINSIZE 80
35 #define MAX_WINSIZE 512
36 #define PADDING 1 /* padding between the progress indicators */
37 #define UPDATE_INTERVAL 1 /* update the progress meter every second */
38 #define STALL_TIME 5 /* we're stalled after this many seconds */
40 /* determines whether we can output to the terminal */
41 static int can_output(void);
43 /* formats and inserts the specified size into the given buffer */
44 static void format_size(char *, int, off_t);
45 static void format_rate(char *, int, off_t);
48 static void sig_winch(int);
49 static void setscreensize(void);
51 /* updates the progressmeter to reflect the current state of the transfer */
52 void refresh_progress_meter(void);
54 /* signal handler for updating the progress meter */
55 static void update_progress_meter(int);
57 static time_t start; /* start progress */
58 static time_t last_update; /* last progress update */
59 static char *file; /* name of the file being transferred */
60 static off_t end_pos; /* ending position of transfer */
61 static off_t cur_pos; /* transfer position as of last refresh */
62 static volatile off_t *counter; /* progress counter */
63 static long stalled; /* how long we have been stalled */
64 static int bytes_per_second; /* current speed in bytes per second */
65 static int win_size; /* terminal window size */
66 static volatile sig_atomic_t win_resized; /* for window resizing */
68 /* units for format_size */
69 static const char unit[] = " KMGT";
74 return (getpgrp() == tcgetpgrp(STDOUT_FILENO));
78 format_rate(char *buf, int size, off_t bytes)
83 for (i = 0; bytes >= 100*1000 && unit[i] != 'T'; i++)
84 bytes = (bytes + 512) / 1024;
87 bytes = (bytes + 512) / 1024;
89 snprintf(buf, size, "%3lld.%1lld%c%s",
90 (long long) (bytes + 5) / 100,
91 (long long) (bytes + 5) / 10 % 10,
97 format_size(char *buf, int size, off_t bytes)
101 for (i = 0; bytes >= 10000 && unit[i] != 'T'; i++)
102 bytes = (bytes + 512) / 1024;
103 snprintf(buf, size, "%4lld%c%s",
110 refresh_progress_meter(void)
112 char buf[MAX_WINSIZE + 1];
119 int hours, minutes, seconds;
123 transferred = *counter - cur_pos;
126 bytes_left = end_pos - cur_pos;
129 elapsed = now - last_update;
131 elapsed = now - start;
132 /* Calculate true total speed when done */
133 transferred = end_pos;
134 bytes_per_second = 0;
137 /* calculate speed */
139 cur_speed = (transferred / elapsed);
141 cur_speed = transferred;
143 #define AGE_FACTOR 0.9
144 if (bytes_per_second != 0) {
145 bytes_per_second = (bytes_per_second * AGE_FACTOR) +
146 (cur_speed * (1.0 - AGE_FACTOR));
148 bytes_per_second = cur_speed;
152 file_len = win_size - 35;
154 len = snprintf(buf, file_len + 1, "\r%s", file);
157 if (len >= file_len + 1)
159 for (i = len; i < file_len; i++ )
161 buf[file_len] = '\0';
164 /* percent of transfer done */
166 percent = ((float)cur_pos / end_pos) * 100;
169 snprintf(buf + strlen(buf), win_size - strlen(buf),
172 /* amount transferred */
173 format_size(buf + strlen(buf), win_size - strlen(buf),
175 strlcat(buf, " ", win_size);
177 /* bandwidth usage */
178 format_rate(buf + strlen(buf), win_size - strlen(buf),
179 (off_t)bytes_per_second);
180 strlcat(buf, "/s ", win_size);
188 if (stalled >= STALL_TIME)
189 strlcat(buf, "- stalled -", win_size);
190 else if (bytes_per_second == 0 && bytes_left)
191 strlcat(buf, " --:-- ETA", win_size);
194 seconds = bytes_left / bytes_per_second;
198 hours = seconds / 3600;
199 seconds -= hours * 3600;
200 minutes = seconds / 60;
201 seconds -= minutes * 60;
204 snprintf(buf + strlen(buf), win_size - strlen(buf),
205 "%d:%02d:%02d", hours, minutes, seconds);
207 snprintf(buf + strlen(buf), win_size - strlen(buf),
208 " %02d:%02d", minutes, seconds);
211 strlcat(buf, " ETA", win_size);
213 strlcat(buf, " ", win_size);
216 atomicio(vwrite, STDOUT_FILENO, buf, win_size - 1);
221 update_progress_meter(int ignore)
232 refresh_progress_meter();
234 signal(SIGALRM, update_progress_meter);
235 alarm(UPDATE_INTERVAL);
240 start_progress_meter(char *f, off_t filesize, off_t *ctr)
242 start = last_update = time(NULL);
248 bytes_per_second = 0;
252 refresh_progress_meter();
254 signal(SIGALRM, update_progress_meter);
255 signal(SIGWINCH, sig_winch);
256 alarm(UPDATE_INTERVAL);
260 stop_progress_meter(void)
267 /* Ensure we complete the progress */
268 if (cur_pos != end_pos)
269 refresh_progress_meter();
271 atomicio(vwrite, STDOUT_FILENO, "\n", 1);
283 struct winsize winsize;
285 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
286 winsize.ws_col != 0) {
287 if (winsize.ws_col > MAX_WINSIZE)
288 win_size = MAX_WINSIZE;
290 win_size = winsize.ws_col;
292 win_size = DEFAULT_WINSIZE;
293 win_size += 1; /* trailing \0 */