1 /* $NetBSD: progressbar.c,v 1.13 2006/05/01 23:02:03 christos Exp $ */
4 * Copyright (c) 1997-2005 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
39 #include <sys/cdefs.h>
41 __RCSID("$NetBSD: progressbar.c,v 1.13 2006/05/01 23:02:03 christos Exp $");
45 * FTP User Program -- Misc support routines
47 #include <sys/types.h>
48 #include <sys/param.h>
60 #include "progressbar.h"
62 #if !defined(NO_PROGRESS)
64 * return non-zero if we're the current foreground process
69 static pid_t pgrp = -1;
74 return (tcgetpgrp(fileno(ttyout)) == pgrp);
76 #endif /* !defined(NO_PROGRESS) */
79 static void updateprogressmeter(int);
82 * SIGALRM handler to update the progress meter
85 updateprogressmeter(int dummy)
94 * List of order of magnitude prefixes.
95 * The last is `P', as 2^64 = 16384 Petabytes
97 static const char prefixes[] = " KMGTP";
100 * Display a transfer progress bar if progress is non-zero.
101 * SIGALRM is hijacked for use by this function.
102 * - Before the transfer, set filesize to size of file (or -1 if unknown),
103 * and call with flag = -1. This starts the once per second timer,
104 * and a call to updateprogressmeter() upon SIGALRM.
105 * - During the transfer, updateprogressmeter will call progressmeter
107 * - After the transfer, call with flag = 1
109 static struct timeval start;
110 static struct timeval lastupdate;
112 #define BUFLEFT (sizeof(buf) - len)
115 progressmeter(int flag)
117 static off_t lastsize;
119 struct timeval now, wait;
122 off_t abbrevsize, bytespersec;
124 int ratio, i, remaining, barlength;
127 * Work variables for progress bar.
129 * XXX: if the format of the progress bar changes
130 * (especially the number of characters in the
131 * `static' portion of it), be sure to update
132 * these appropriately.
136 char buf[256]; /* workspace for progress bar */
138 #define BAROVERHEAD 43 /* non `*' portion of progress bar */
140 * stars should contain at least
141 * sizeof(buf) - BAROVERHEAD entries
143 static const char stars[] =
144 "*****************************************************************************"
145 "*****************************************************************************"
146 "*****************************************************************************";
151 (void)gettimeofday(&start, NULL);
153 lastsize = restart_point;
156 (void)gettimeofday(&now, NULL);
157 cursize = bytes + restart_point;
158 timersub(&now, &lastupdate, &wait);
159 if (cursize > lastsize) {
164 #ifndef STANDALONE_PROGRESS
165 if (quit_time > 0 && wait.tv_sec > quit_time) {
166 len = snprintf(buf, sizeof(buf), "\r\n%s: "
167 "transfer aborted because stalled for %lu sec.\r\n",
168 getprogname(), (unsigned long)wait.tv_sec);
169 (void)write(fileno(ttyout), buf, len);
170 (void)xsignal(SIGALRM, SIG_DFL);
172 siglongjmp(toplevel, 1);
174 #endif /* !STANDALONE_PROGRESS */
177 * Always set the handler even if we are not the foreground process.
179 #ifdef STANDALONE_PROGRESS
182 if (quit_time > 0 || progress) {
183 #endif /* !STANDALONE_PROGRESS */
185 (void)xsignal_restart(SIGALRM, updateprogressmeter, 1);
186 alarmtimer(1); /* set alarm timer for 1 Hz */
187 } else if (flag == 1) {
188 (void)xsignal(SIGALRM, SIG_DFL);
198 * print progress bar only if we are foreground process.
200 if (! foregroundproc())
203 len += snprintf(buf + len, BUFLEFT, "\r");
205 len += snprintf(buf + len, BUFLEFT, "%s", prefix);
207 ratio = (int)((double)cursize * 100.0 / (double)filesize);
208 ratio = MAX(ratio, 0);
209 ratio = MIN(ratio, 100);
210 len += snprintf(buf + len, BUFLEFT, "%3d%% ", ratio);
213 * calculate the length of the `*' bar, ensuring that
214 * the number of stars won't exceed the buffer size
216 barlength = MIN(sizeof(buf) - 1, ttywidth) - BAROVERHEAD;
218 barlength -= (int)strlen(prefix);
220 i = barlength * ratio / 100;
221 len += snprintf(buf + len, BUFLEFT,
222 "|%.*s%*s|", i, stars, (int)(barlength - i), "");
226 abbrevsize = cursize;
227 for (i = 0; abbrevsize >= 100000 && i < sizeof(prefixes); i++)
229 if (i == sizeof(prefixes))
231 len += snprintf(buf + len, BUFLEFT, " " LLFP("5") " %c%c ",
236 timersub(&now, &start, &td);
237 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
243 bytespersec /= elapsed;
245 for (i = 1; bytespersec >= 1024000 && i < sizeof(prefixes); i++)
247 len += snprintf(buf + len, BUFLEFT,
248 " " LLFP("3") ".%02d %cB/s ",
249 (LLT)(bytespersec / 1024),
250 (int)((bytespersec % 1024) * 100 / 1024),
254 if (bytes <= 0 || elapsed <= 0.0 || cursize > filesize) {
255 len += snprintf(buf + len, BUFLEFT, " --:-- ETA");
256 } else if (wait.tv_sec >= STALLTIME) {
257 len += snprintf(buf + len, BUFLEFT, " - stalled -");
260 ((filesize - restart_point) / (bytes / elapsed) -
262 if (remaining >= 100 * SECSPERHOUR)
263 len += snprintf(buf + len, BUFLEFT,
266 i = remaining / SECSPERHOUR;
268 len += snprintf(buf + len, BUFLEFT,
271 len += snprintf(buf + len, BUFLEFT,
273 i = remaining % SECSPERHOUR;
274 len += snprintf(buf + len, BUFLEFT,
275 "%02d:%02d ETA", i / 60, i % 60);
280 len += snprintf(buf + len, BUFLEFT, "\n");
281 (void)write(fileno(ttyout), buf, len);
283 #endif /* !NO_PROGRESS */
286 #ifndef STANDALONE_PROGRESS
288 * Display transfer statistics.
289 * Requires start to be initialised by progressmeter(-1),
290 * direction to be defined by xfer routines, and filesize and bytes
291 * to be updated by xfer routines
292 * If siginfo is nonzero, an ETA is displayed, and the output goes to stderr
296 ptransfer(int siginfo)
298 struct timeval now, td, wait;
301 int remaining, hh, i;
304 char buf[256]; /* Work variable for transfer status. */
306 if (!verbose && !progress && !siginfo)
309 (void)gettimeofday(&now, NULL);
310 timersub(&now, &start, &td);
311 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
316 bytespersec /= elapsed;
319 len += snprintf(buf + len, BUFLEFT, LLF " byte%s %s in ",
320 (LLT)bytes, bytes == 1 ? "" : "s", direction);
321 remaining = (int)elapsed;
322 if (remaining > SECSPERDAY) {
325 days = remaining / SECSPERDAY;
326 remaining %= SECSPERDAY;
327 len += snprintf(buf + len, BUFLEFT,
328 "%d day%s ", days, days == 1 ? "" : "s");
330 hh = remaining / SECSPERHOUR;
331 remaining %= SECSPERHOUR;
333 len += snprintf(buf + len, BUFLEFT, "%2d:", hh);
334 len += snprintf(buf + len, BUFLEFT,
335 "%02d:%02d ", remaining / 60, remaining % 60);
337 for (i = 1; bytespersec >= 1024000 && i < sizeof(prefixes); i++)
339 if (i == sizeof(prefixes))
341 len += snprintf(buf + len, BUFLEFT, "(" LLF ".%02d %cB/s)",
342 (LLT)(bytespersec / 1024),
343 (int)((bytespersec % 1024) * 100 / 1024),
346 if (siginfo && bytes > 0 && elapsed > 0.0 && filesize >= 0
347 && bytes + restart_point <= filesize) {
348 remaining = (int)((filesize - restart_point) /
349 (bytes / elapsed) - elapsed);
350 hh = remaining / SECSPERHOUR;
351 remaining %= SECSPERHOUR;
352 len += snprintf(buf + len, BUFLEFT, " ETA: ");
354 len += snprintf(buf + len, BUFLEFT, "%2d:", hh);
355 len += snprintf(buf + len, BUFLEFT, "%02d:%02d",
356 remaining / 60, remaining % 60);
357 timersub(&now, &lastupdate, &wait);
358 if (wait.tv_sec >= STALLTIME)
359 len += snprintf(buf + len, BUFLEFT, " (stalled)");
361 len += snprintf(buf + len, BUFLEFT, "\n");
362 (void)write(siginfo ? STDERR_FILENO : fileno(ttyout), buf, len);
366 * SIG{INFO,QUIT} handler to print transfer stats if a transfer is in progress
369 psummary(int notused)
375 write(fileno(ttyout), "\n", 1);
380 #endif /* !STANDALONE_PROGRESS */
384 * Set the SIGALRM interval timer for wait seconds, 0 to disable.
389 struct itimerval itv;
391 itv.it_value.tv_sec = wait;
392 itv.it_value.tv_usec = 0;
393 itv.it_interval = itv.it_value;
394 setitimer(ITIMER_REAL, &itv, NULL);
399 * Install a POSIX signal handler, allowing the invoker to set whether
400 * the signal should be restartable or not
403 xsignal_restart(int sig, sigfunc func, int restartable)
405 struct sigaction act, oact;
406 act.sa_handler = func;
408 sigemptyset(&act.sa_mask);
409 #if defined(SA_RESTART) /* 4.4BSD, Posix(?), SVR4 */
410 act.sa_flags = restartable ? SA_RESTART : 0;
411 #elif defined(SA_INTERRUPT) /* SunOS 4.x */
412 act.sa_flags = restartable ? 0 : SA_INTERRUPT;
414 #error "system must have SA_RESTART or SA_INTERRUPT"
416 if (sigaction(sig, &act, &oact) < 0)
418 return (oact.sa_handler);
422 * Install a signal handler with the `restartable' flag set dependent upon
423 * which signal is being set. (This is a wrapper to xsignal_restart())
426 xsignal(int sig, sigfunc func)
431 * Some signals print output or change the state of the process.
432 * There should be restartable, so that reads and writes are
433 * not affected. Some signals should cause program flow to change;
434 * these signals should not be restartable, so that the system call
435 * will return with EINTR, and the program will go do something
436 * different. If the signal handler calls longjmp() or siglongjmp(),
437 * it doesn't matter if it's restartable.
459 * This is unpleasant, but I don't know what would be better.
460 * Right now, this "can't happen"
462 errx(1, "xsignal_restart called with signal %d", sig);
465 return(xsignal_restart(sig, func, restartable));