1 /* $NetBSD: progressbar.c,v 1.7 2005/04/11 01:49:31 lukem Exp $ */
4 * Copyright (c) 1997-2003 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.7 2005/04/11 01:49:31 lukem Exp $");
45 * FTP User Program -- Misc support routines
47 #include <sys/types.h>
48 #include <sys/param.h>
60 #include "progressbar.h"
62 #define SECSPERHOUR (60 * 60)
63 #define SECSPERDAY ((long)60 * 60 * 24)
65 #if !defined(NO_PROGRESS)
67 * return non-zero if we're the current foreground process
72 static pid_t pgrp = -1;
77 return (tcgetpgrp(fileno(ttyout)) == pgrp);
79 #endif /* !defined(NO_PROGRESS) */
82 static void updateprogressmeter(int);
85 * SIGALRM handler to update the progress meter
88 updateprogressmeter(int dummy)
97 * List of order of magnitude prefixes.
98 * The last is `P', as 2^64 = 16384 Petabytes
100 static const char prefixes[] = " KMGTP";
103 * Display a transfer progress bar if progress is non-zero.
104 * SIGALRM is hijacked for use by this function.
105 * - Before the transfer, set filesize to size of file (or -1 if unknown),
106 * and call with flag = -1. This starts the once per second timer,
107 * and a call to updateprogressmeter() upon SIGALRM.
108 * - During the transfer, updateprogressmeter will call progressmeter
110 * - After the transfer, call with flag = 1
112 static struct timeval start;
113 static struct timeval lastupdate;
115 #define BUFLEFT (sizeof(buf) - len)
118 progressmeter(int flag)
120 static off_t lastsize;
122 struct timeval now, wait;
125 off_t abbrevsize, bytespersec;
127 int ratio, barlength, i, remaining;
130 * Work variables for progress bar.
132 * XXX: if the format of the progress bar changes
133 * (especially the number of characters in the
134 * `static' portion of it), be sure to update
135 * these appropriately.
139 char buf[256]; /* workspace for progress bar */
141 #define BAROVERHEAD 43 /* non `*' portion of progress bar */
143 * stars should contain at least
144 * sizeof(buf) - BAROVERHEAD entries
146 static const char stars[] =
147 "*****************************************************************************"
148 "*****************************************************************************"
149 "*****************************************************************************";
154 (void)gettimeofday(&start, NULL);
156 lastsize = restart_point;
159 (void)gettimeofday(&now, NULL);
160 cursize = bytes + restart_point;
161 timersub(&now, &lastupdate, &wait);
162 if (cursize > lastsize) {
167 #ifndef STANDALONE_PROGRESS
168 if (quit_time > 0 && wait.tv_sec > quit_time) {
169 len = snprintf(buf, sizeof(buf), "\r\n%s: "
170 "transfer aborted because stalled for %lu sec.\r\n",
171 getprogname(), (unsigned long)wait.tv_sec);
172 (void)write(fileno(ttyout), buf, len);
173 (void)xsignal(SIGALRM, SIG_DFL);
175 siglongjmp(toplevel, 1);
177 #endif /* !STANDALONE_PROGRESS */
180 * Always set the handler even if we are not the foreground process.
182 #ifdef STANDALONE_PROGRESS
185 if (quit_time > 0 || progress) {
186 #endif /* !STANDALONE_PROGRESS */
188 (void)xsignal_restart(SIGALRM, updateprogressmeter, 1);
189 alarmtimer(1); /* set alarm timer for 1 Hz */
190 } else if (flag == 1) {
191 (void)xsignal(SIGALRM, SIG_DFL);
201 * print progress bar only if we are foreground process.
203 if (! foregroundproc())
206 len += snprintf(buf + len, BUFLEFT, "\r");
208 len += snprintf(buf + len, BUFLEFT, "%s", prefix);
210 ratio = (int)((double)cursize * 100.0 / (double)filesize);
211 ratio = MAX(ratio, 0);
212 ratio = MIN(ratio, 100);
213 len += snprintf(buf + len, BUFLEFT, "%3d%% ", ratio);
216 * calculate the length of the `*' bar, ensuring that
217 * the number of stars won't exceed the buffer size
219 barlength = MIN(sizeof(buf) - 1, ttywidth) - BAROVERHEAD;
221 barlength -= strlen(prefix);
223 i = barlength * ratio / 100;
224 len += snprintf(buf + len, BUFLEFT,
225 "|%.*s%*s|", i, stars, barlength - i, "");
229 abbrevsize = cursize;
230 for (i = 0; abbrevsize >= 100000 && i < sizeof(prefixes); i++)
232 len += snprintf(buf + len, BUFLEFT, " " LLFP("5") " %c%c ",
237 timersub(&now, &start, &td);
238 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
244 bytespersec /= elapsed;
246 for (i = 1; bytespersec >= 1024000 && i < sizeof(prefixes); i++)
248 len += snprintf(buf + len, BUFLEFT,
249 " " LLFP("3") ".%02d %cB/s ",
250 (LLT)(bytespersec / 1024),
251 (int)((bytespersec % 1024) * 100 / 1024),
255 if (bytes <= 0 || elapsed <= 0.0 || cursize > filesize) {
256 len += snprintf(buf + len, BUFLEFT, " --:-- ETA");
257 } else if (wait.tv_sec >= STALLTIME) {
258 len += snprintf(buf + len, BUFLEFT, " - stalled -");
261 ((filesize - restart_point) / (bytes / elapsed) -
263 if (remaining >= 100 * SECSPERHOUR)
264 len += snprintf(buf + len, BUFLEFT,
267 i = remaining / SECSPERHOUR;
269 len += snprintf(buf + len, BUFLEFT,
272 len += snprintf(buf + len, BUFLEFT,
274 i = remaining % SECSPERHOUR;
275 len += snprintf(buf + len, BUFLEFT,
276 "%02d:%02d ETA", i / 60, i % 60);
281 len += snprintf(buf + len, BUFLEFT, "\n");
282 (void)write(fileno(ttyout), buf, len);
284 #endif /* !NO_PROGRESS */
287 #ifndef STANDALONE_PROGRESS
289 * Display transfer statistics.
290 * Requires start to be initialised by progressmeter(-1),
291 * direction to be defined by xfer routines, and filesize and bytes
292 * to be updated by xfer routines
293 * If siginfo is nonzero, an ETA is displayed, and the output goes to stderr
297 ptransfer(int siginfo)
299 struct timeval now, td, wait;
302 int remaining, hh, i, len;
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 len += snprintf(buf + len, BUFLEFT, "(" LLF ".%02d %cB/s)",
340 (LLT)(bytespersec / 1024),
341 (int)((bytespersec % 1024) * 100 / 1024),
344 if (siginfo && bytes > 0 && elapsed > 0.0 && filesize >= 0
345 && bytes + restart_point <= filesize) {
346 remaining = (int)((filesize - restart_point) /
347 (bytes / elapsed) - elapsed);
348 hh = remaining / SECSPERHOUR;
349 remaining %= SECSPERHOUR;
350 len += snprintf(buf + len, BUFLEFT, " ETA: ");
352 len += snprintf(buf + len, BUFLEFT, "%2d:", hh);
353 len += snprintf(buf + len, BUFLEFT, "%02d:%02d",
354 remaining / 60, remaining % 60);
355 timersub(&now, &lastupdate, &wait);
356 if (wait.tv_sec >= STALLTIME)
357 len += snprintf(buf + len, BUFLEFT, " (stalled)");
359 len += snprintf(buf + len, BUFLEFT, "\n");
360 (void)write(siginfo ? STDERR_FILENO : fileno(ttyout), buf, len);
364 * SIG{INFO,QUIT} handler to print transfer stats if a transfer is in progress
367 psummary(int notused)
373 write(fileno(ttyout), "\n", 1);
378 #endif /* !STANDALONE_PROGRESS */
382 * Set the SIGALRM interval timer for wait seconds, 0 to disable.
387 struct itimerval itv;
389 itv.it_value.tv_sec = wait;
390 itv.it_value.tv_usec = 0;
391 itv.it_interval = itv.it_value;
392 setitimer(ITIMER_REAL, &itv, NULL);
397 * Install a POSIX signal handler, allowing the invoker to set whether
398 * the signal should be restartable or not
401 xsignal_restart(int sig, sigfunc func, int restartable)
403 struct sigaction act, oact;
404 act.sa_handler = func;
406 sigemptyset(&act.sa_mask);
407 #if defined(SA_RESTART) /* 4.4BSD, Posix(?), SVR4 */
408 act.sa_flags = restartable ? SA_RESTART : 0;
409 #elif defined(SA_INTERRUPT) /* SunOS 4.x */
410 act.sa_flags = restartable ? 0 : SA_INTERRUPT;
412 #error "system must have SA_RESTART or SA_INTERRUPT"
414 if (sigaction(sig, &act, &oact) < 0)
416 return (oact.sa_handler);
420 * Install a signal handler with the `restartable' flag set dependent upon
421 * which signal is being set. (This is a wrapper to xsignal_restart())
424 xsignal(int sig, sigfunc func)
429 * Some signals print output or change the state of the process.
430 * There should be restartable, so that reads and writes are
431 * not affected. Some signals should cause program flow to change;
432 * these signals should not be restartable, so that the system call
433 * will return with EINTR, and the program will go do something
434 * different. If the signal handler calls longjmp() or siglongjmp(),
435 * it doesn't matter if it's restartable.
457 * This is unpleasant, but I don't know what would be better.
458 * Right now, this "can't happen"
460 errx(1, "xsignal_restart called with signal %d", sig);
463 return(xsignal_restart(sig, func, restartable));