2 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
3 * Copyright (c) 1999 Aaron Campbell. 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.
27 * Copyright (c) 1997-2003 The NetBSD Foundation, Inc.
28 * All rights reserved.
30 * This code is derived from software contributed to The NetBSD Foundation
33 * This code is derived from software contributed to The NetBSD Foundation
34 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
35 * NASA Ames Research Center.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the NetBSD
48 * Foundation, Inc. and its contributors.
49 * 4. Neither the name of The NetBSD Foundation nor the names of its
50 * contributors may be used to endorse or promote products derived
51 * from this software without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
54 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
57 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
59 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
61 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63 * POSSIBILITY OF SUCH DAMAGE.
67 RCSID("$OpenBSD: progressmeter.c,v 1.6 2003/04/07 21:58:05 millert Exp $");
74 #include "progressmeter.h"
76 /* Number of seconds before xfer considered "stalled". */
78 /* alarm() interval for updating progress meter. */
79 #define PROGRESSTIME 1
81 /* Signal handler used for updating the progress meter. */
82 static void update_progress_meter(int);
84 /* Returns non-zero if we are the foreground process. */
85 static int foregroundproc(void);
87 /* Returns width of the terminal (for progress meter calculations). */
88 static int get_tty_width(void);
90 /* Visual statistics about files as they are transferred. */
91 static void draw_progress_meter(void);
93 /* Time a transfer started. */
94 static struct timeval start;
96 /* Number of bytes of current file transferred so far. */
97 static volatile off_t *statbytes;
99 /* Total size of current file. */
100 static off_t totalbytes;
102 /* Name of current file being transferred. */
103 static char *curfile;
105 /* Time of last update. */
106 static struct timeval lastupdate;
108 /* Size at the time of the last update. */
109 static off_t lastsize;
112 start_progress_meter(char *file, off_t filesize, off_t *counter)
114 if ((curfile = basename(file)) == NULL)
117 totalbytes = filesize;
119 (void) gettimeofday(&start, (struct timezone *) 0);
123 draw_progress_meter();
124 signal(SIGALRM, update_progress_meter);
129 stop_progress_meter()
132 draw_progress_meter();
133 if (foregroundproc() != 0)
134 atomicio(write, fileno(stdout), "\n", 1);
138 update_progress_meter(int ignore)
140 int save_errno = errno;
142 draw_progress_meter();
143 signal(SIGALRM, update_progress_meter);
151 static pid_t pgrp = -1;
157 #ifdef HAVE_TCGETPGRP
158 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
161 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
167 draw_progress_meter()
169 static const char spaces[] = " "
175 static const char prefixes[] = " KMGTP";
176 struct timeval now, td, wait;
177 off_t cursize, abbrevsize, bytespersec;
179 int ratio, remaining, i, ai, bi, nspaces;
182 if (foregroundproc() == 0)
185 (void) gettimeofday(&now, (struct timezone *) 0);
186 cursize = *statbytes;
187 if (totalbytes != 0) {
188 ratio = 100.0 * cursize / totalbytes;
189 ratio = MAX(ratio, 0);
190 ratio = MIN(ratio, 100);
194 abbrevsize = cursize;
195 for (ai = 0; abbrevsize >= 10000 && ai < sizeof(prefixes); ai++)
198 timersub(&now, &lastupdate, &wait);
199 if (cursize > lastsize) {
204 timersub(&now, &start, &td);
205 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
209 bytespersec = cursize;
211 bytespersec /= elapsed;
213 for (bi = 1; bytespersec >= 1024000 && bi < sizeof(prefixes); bi++)
216 nspaces = MIN(get_tty_width() - 79, sizeof(spaces) - 1);
218 snprintf(buf, sizeof(buf),
219 "\r%-45.45s%.*s%3d%% %4lld%c%c %3lld.%01d%cB/s",
227 (int64_t)(bytespersec / 1024),
228 (int)((bytespersec % 1024) * 10 / 1024),
232 if (cursize <= 0 || elapsed <= 0.0 || cursize > totalbytes) {
233 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
235 } else if (wait.tv_sec >= STALLTIME) {
236 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
239 if (cursize != totalbytes)
240 remaining = (int)(totalbytes / (cursize / elapsed) -
245 i = remaining / 3600;
247 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
250 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
252 i = remaining % 3600;
253 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
254 "%02d:%02d%s", i / 60, i % 60,
255 (cursize != totalbytes) ? " ETA" : " ");
257 atomicio(write, fileno(stdout), buf, strlen(buf));
263 struct winsize winsize;
265 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
266 return (winsize.ws_col ? winsize.ws_col : 80);