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.23 2005/04/28 10:17:56 moritz Exp $");
28 #include "progressmeter.h"
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 */
38 /* determines whether we can output to the terminal */
39 static int can_output(void);
41 /* formats and inserts the specified size into the given buffer */
42 static void format_size(char *, int, off_t);
43 static void format_rate(char *, int, off_t);
45 /* updates the progressmeter to reflect the current state of the transfer */
46 void refresh_progress_meter(void);
48 /* signal handler for updating the progress meter */
49 static void update_progress_meter(int);
51 static time_t start; /* start progress */
52 static time_t last_update; /* last progress update */
53 static char *file; /* name of the file being transferred */
54 static off_t end_pos; /* ending position of transfer */
55 static off_t cur_pos; /* transfer position as of last refresh */
56 static volatile off_t *counter; /* progress counter */
57 static long stalled; /* how long we have been stalled */
58 static int bytes_per_second; /* current speed in bytes per second */
59 static int win_size; /* terminal window size */
61 /* units for format_size */
62 static const char unit[] = " KMGT";
67 return (getpgrp() == tcgetpgrp(STDOUT_FILENO));
71 format_rate(char *buf, int size, off_t bytes)
76 for (i = 0; bytes >= 100*1000 && unit[i] != 'T'; i++)
77 bytes = (bytes + 512) / 1024;
80 bytes = (bytes + 512) / 1024;
82 snprintf(buf, size, "%3lld.%1lld%c%s",
83 (int64_t) (bytes + 5) / 100,
84 (int64_t) (bytes + 5) / 10 % 10,
90 format_size(char *buf, int size, off_t bytes)
94 for (i = 0; bytes >= 10000 && unit[i] != 'T'; i++)
95 bytes = (bytes + 512) / 1024;
96 snprintf(buf, size, "%4lld%c%s",
103 refresh_progress_meter(void)
105 char buf[MAX_WINSIZE + 1];
112 int hours, minutes, seconds;
116 transferred = *counter - cur_pos;
119 bytes_left = end_pos - cur_pos;
122 elapsed = now - last_update;
124 elapsed = now - start;
125 /* Calculate true total speed when done */
126 transferred = end_pos;
127 bytes_per_second = 0;
130 /* calculate speed */
132 cur_speed = (transferred / elapsed);
134 cur_speed = transferred;
136 #define AGE_FACTOR 0.9
137 if (bytes_per_second != 0) {
138 bytes_per_second = (bytes_per_second * AGE_FACTOR) +
139 (cur_speed * (1.0 - AGE_FACTOR));
141 bytes_per_second = cur_speed;
145 file_len = win_size - 35;
147 len = snprintf(buf, file_len + 1, "\r%s", file);
150 if (len >= file_len + 1)
152 for (i = len; i < file_len; i++ )
154 buf[file_len] = '\0';
157 /* percent of transfer done */
159 percent = ((float)cur_pos / end_pos) * 100;
162 snprintf(buf + strlen(buf), win_size - strlen(buf),
165 /* amount transferred */
166 format_size(buf + strlen(buf), win_size - strlen(buf),
168 strlcat(buf, " ", win_size);
170 /* bandwidth usage */
171 format_rate(buf + strlen(buf), win_size - strlen(buf),
172 (off_t)bytes_per_second);
173 strlcat(buf, "/s ", win_size);
181 if (stalled >= STALL_TIME)
182 strlcat(buf, "- stalled -", win_size);
183 else if (bytes_per_second == 0 && bytes_left)
184 strlcat(buf, " --:-- ETA", win_size);
187 seconds = bytes_left / bytes_per_second;
191 hours = seconds / 3600;
192 seconds -= hours * 3600;
193 minutes = seconds / 60;
194 seconds -= minutes * 60;
197 snprintf(buf + strlen(buf), win_size - strlen(buf),
198 "%d:%02d:%02d", hours, minutes, seconds);
200 snprintf(buf + strlen(buf), win_size - strlen(buf),
201 " %02d:%02d", minutes, seconds);
204 strlcat(buf, " ETA", win_size);
206 strlcat(buf, " ", win_size);
209 atomicio(vwrite, STDOUT_FILENO, buf, win_size - 1);
214 update_progress_meter(int ignore)
221 refresh_progress_meter();
223 signal(SIGALRM, update_progress_meter);
224 alarm(UPDATE_INTERVAL);
229 start_progress_meter(char *f, off_t filesize, off_t *ctr)
231 struct winsize winsize;
233 start = last_update = time(NULL);
239 bytes_per_second = 0;
241 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
242 winsize.ws_col != 0) {
243 if (winsize.ws_col > MAX_WINSIZE)
244 win_size = MAX_WINSIZE;
246 win_size = winsize.ws_col;
248 win_size = DEFAULT_WINSIZE;
249 win_size += 1; /* trailing \0 */
252 refresh_progress_meter();
254 signal(SIGALRM, update_progress_meter);
255 alarm(UPDATE_INTERVAL);
259 stop_progress_meter(void)
266 /* Ensure we complete the progress */
267 if (cur_pos != end_pos)
268 refresh_progress_meter();
270 atomicio(vwrite, STDOUT_FILENO, "\n", 1);