1 /* $OpenBSD: progressmeter.c,v 1.30 2006/07/11 20:07:25 stevesk Exp $ */
3 * Copyright (c) 2003 Nils Nordman. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/ioctl.h>
33 #include "progressmeter.h"
37 #define DEFAULT_WINSIZE 80
38 #define MAX_WINSIZE 512
39 #define PADDING 1 /* padding between the progress indicators */
40 #define UPDATE_INTERVAL 1 /* update the progress meter every second */
41 #define STALL_TIME 5 /* we're stalled after this many seconds */
43 /* determines whether we can output to the terminal */
44 static int can_output(void);
46 /* formats and inserts the specified size into the given buffer */
47 static void format_size(char *, int, off_t);
48 static void format_rate(char *, int, off_t);
51 static void sig_winch(int);
52 static void setscreensize(void);
54 /* updates the progressmeter to reflect the current state of the transfer */
55 void refresh_progress_meter(void);
57 /* signal handler for updating the progress meter */
58 static void update_progress_meter(int);
60 static time_t start; /* start progress */
61 static time_t last_update; /* last progress update */
62 static char *file; /* name of the file being transferred */
63 static off_t end_pos; /* ending position of transfer */
64 static off_t cur_pos; /* transfer position as of last refresh */
65 static volatile off_t *counter; /* progress counter */
66 static long stalled; /* how long we have been stalled */
67 static int bytes_per_second; /* current speed in bytes per second */
68 static int win_size; /* terminal window size */
69 static volatile sig_atomic_t win_resized; /* for window resizing */
71 /* units for format_size */
72 static const char unit[] = " KMGT";
77 return (getpgrp() == tcgetpgrp(STDOUT_FILENO));
81 format_rate(char *buf, int size, off_t bytes)
86 for (i = 0; bytes >= 100*1000 && unit[i] != 'T'; i++)
87 bytes = (bytes + 512) / 1024;
90 bytes = (bytes + 512) / 1024;
92 snprintf(buf, size, "%3lld.%1lld%c%s",
93 (long long) (bytes + 5) / 100,
94 (long long) (bytes + 5) / 10 % 10,
100 format_size(char *buf, int size, off_t bytes)
104 for (i = 0; bytes >= 10000 && unit[i] != 'T'; i++)
105 bytes = (bytes + 512) / 1024;
106 snprintf(buf, size, "%4lld%c%s",
113 refresh_progress_meter(void)
115 char buf[MAX_WINSIZE + 1];
122 int hours, minutes, seconds;
126 transferred = *counter - cur_pos;
129 bytes_left = end_pos - cur_pos;
132 elapsed = now - last_update;
134 elapsed = now - start;
135 /* Calculate true total speed when done */
136 transferred = end_pos;
137 bytes_per_second = 0;
140 /* calculate speed */
142 cur_speed = (transferred / elapsed);
144 cur_speed = transferred;
146 #define AGE_FACTOR 0.9
147 if (bytes_per_second != 0) {
148 bytes_per_second = (bytes_per_second * AGE_FACTOR) +
149 (cur_speed * (1.0 - AGE_FACTOR));
151 bytes_per_second = cur_speed;
155 file_len = win_size - 35;
157 len = snprintf(buf, file_len + 1, "\r%s", file);
160 if (len >= file_len + 1)
162 for (i = len; i < file_len; i++ )
164 buf[file_len] = '\0';
167 /* percent of transfer done */
169 percent = ((float)cur_pos / end_pos) * 100;
172 snprintf(buf + strlen(buf), win_size - strlen(buf),
175 /* amount transferred */
176 format_size(buf + strlen(buf), win_size - strlen(buf),
178 strlcat(buf, " ", win_size);
180 /* bandwidth usage */
181 format_rate(buf + strlen(buf), win_size - strlen(buf),
182 (off_t)bytes_per_second);
183 strlcat(buf, "/s ", win_size);
191 if (stalled >= STALL_TIME)
192 strlcat(buf, "- stalled -", win_size);
193 else if (bytes_per_second == 0 && bytes_left)
194 strlcat(buf, " --:-- ETA", win_size);
197 seconds = bytes_left / bytes_per_second;
201 hours = seconds / 3600;
202 seconds -= hours * 3600;
203 minutes = seconds / 60;
204 seconds -= minutes * 60;
207 snprintf(buf + strlen(buf), win_size - strlen(buf),
208 "%d:%02d:%02d", hours, minutes, seconds);
210 snprintf(buf + strlen(buf), win_size - strlen(buf),
211 " %02d:%02d", minutes, seconds);
214 strlcat(buf, " ETA", win_size);
216 strlcat(buf, " ", win_size);
219 atomicio(vwrite, STDOUT_FILENO, buf, win_size - 1);
224 update_progress_meter(int ignore)
235 refresh_progress_meter();
237 signal(SIGALRM, update_progress_meter);
238 alarm(UPDATE_INTERVAL);
243 start_progress_meter(char *f, off_t filesize, off_t *ctr)
245 start = last_update = time(NULL);
251 bytes_per_second = 0;
255 refresh_progress_meter();
257 signal(SIGALRM, update_progress_meter);
258 signal(SIGWINCH, sig_winch);
259 alarm(UPDATE_INTERVAL);
263 stop_progress_meter(void)
270 /* Ensure we complete the progress */
271 if (cur_pos != end_pos)
272 refresh_progress_meter();
274 atomicio(vwrite, STDOUT_FILENO, "\n", 1);
287 struct winsize winsize;
289 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
290 winsize.ws_col != 0) {
291 if (winsize.ws_col > MAX_WINSIZE)
292 win_size = MAX_WINSIZE;
294 win_size = winsize.ws_col;
296 win_size = DEFAULT_WINSIZE;
297 win_size += 1; /* trailing \0 */