1 /* $NetBSD: progressbar.c,v 1.14 2009/05/20 12:53:47 lukem Exp $ */
2 /* from NetBSD: progressbar.c,v 1.21 2009/04/12 10:18:52 lukem Exp */
5 * Copyright (c) 1997-2009 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/cdefs.h>
39 __RCSID(" NetBSD: progressbar.c,v 1.21 2009/04/12 10:18:52 lukem Exp ");
43 * FTP User Program -- Misc support routines
45 #include <sys/types.h>
46 #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 #else /* ! GETPGRP_VOID */
76 #endif /* ! GETPGRP_VOID */
78 return (tcgetpgrp(fileno(ttyout)) == pgrp);
80 #endif /* !defined(NO_PROGRESS) */
83 static void updateprogressmeter(int);
86 * SIGALRM handler to update the progress meter
89 updateprogressmeter(int dummy)
98 * List of order of magnitude suffixes, per IEC 60027-2.
100 static const char * const suffixes[] = {
102 "KiB", /* 2^10 Kibibyte */
103 "MiB", /* 2^20 Mebibyte */
104 "GiB", /* 2^30 Gibibyte */
105 "TiB", /* 2^40 Tebibyte */
106 "PiB", /* 2^50 Pebibyte */
107 "EiB", /* 2^60 Exbibyte */
109 /* The following are not necessary for signed 64-bit off_t */
110 "ZiB", /* 2^70 Zebibyte */
111 "YiB", /* 2^80 Yobibyte */
114 #define NSUFFIXES (int)(sizeof(suffixes) / sizeof(suffixes[0]))
117 * Display a transfer progress bar if progress is non-zero.
118 * SIGALRM is hijacked for use by this function.
119 * - Before the transfer, set filesize to size of file (or -1 if unknown),
120 * and call with flag = -1. This starts the once per second timer,
121 * and a call to updateprogressmeter() upon SIGALRM.
122 * - During the transfer, updateprogressmeter will call progressmeter
124 * - After the transfer, call with flag = 1
126 static struct timeval start;
127 static struct timeval lastupdate;
129 #define BUFLEFT (sizeof(buf) - len)
132 progressmeter(int flag)
134 static off_t lastsize;
136 struct timeval now, wait;
139 off_t abbrevsize, bytespersec;
141 int ratio, i, remaining, barlength;
144 * Work variables for progress bar.
146 * XXX: if the format of the progress bar changes
147 * (especially the number of characters in the
148 * `static' portion of it), be sure to update
149 * these appropriately.
153 char buf[256]; /* workspace for progress bar */
155 #define BAROVERHEAD 45 /* non `*' portion of progress bar */
157 * stars should contain at least
158 * sizeof(buf) - BAROVERHEAD entries
160 static const char stars[] =
161 "*****************************************************************************"
162 "*****************************************************************************"
163 "*****************************************************************************";
168 (void)gettimeofday(&start, NULL);
170 lastsize = restart_point;
173 (void)gettimeofday(&now, NULL);
174 cursize = bytes + restart_point;
175 timersub(&now, &lastupdate, &wait);
176 if (cursize > lastsize) {
181 #ifndef STANDALONE_PROGRESS
182 if (quit_time > 0 && wait.tv_sec > quit_time) {
183 len = snprintf(buf, sizeof(buf), "\r\n%s: "
184 "transfer aborted because stalled for %lu sec.\r\n",
185 getprogname(), (unsigned long)wait.tv_sec);
186 (void)write(fileno(ttyout), buf, len);
188 (void)xsignal(SIGALRM, SIG_DFL);
189 siglongjmp(toplevel, 1);
191 #endif /* !STANDALONE_PROGRESS */
194 * Always set the handler even if we are not the foreground process.
196 #ifdef STANDALONE_PROGRESS
199 if (quit_time > 0 || progress) {
200 #endif /* !STANDALONE_PROGRESS */
202 (void)xsignal_restart(SIGALRM, updateprogressmeter, 1);
203 alarmtimer(1); /* set alarm timer for 1 Hz */
204 } else if (flag == 1) {
206 (void)xsignal(SIGALRM, SIG_DFL);
215 * print progress bar only if we are foreground process.
217 if (! foregroundproc())
220 len += snprintf(buf + len, BUFLEFT, "\r");
222 len += snprintf(buf + len, BUFLEFT, "%s", prefix);
224 ratio = (int)((double)cursize * 100.0 / (double)filesize);
225 ratio = MAX(ratio, 0);
226 ratio = MIN(ratio, 100);
227 len += snprintf(buf + len, BUFLEFT, "%3d%% ", ratio);
230 * calculate the length of the `*' bar, ensuring that
231 * the number of stars won't exceed the buffer size
233 barlength = MIN((int)(sizeof(buf) - 1), ttywidth) - BAROVERHEAD;
235 barlength -= (int)strlen(prefix);
237 i = barlength * ratio / 100;
238 len += snprintf(buf + len, BUFLEFT,
239 "|%.*s%*s|", i, stars, (int)(barlength - i), "");
243 abbrevsize = cursize;
244 for (i = 0; abbrevsize >= 100000 && i < NSUFFIXES; i++)
248 len += snprintf(buf + len, BUFLEFT, " " LLFP("5") " %-3s ",
252 timersub(&now, &start, &td);
253 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
259 bytespersec /= elapsed;
261 for (i = 1; bytespersec >= 1024000 && i < NSUFFIXES; i++)
263 len += snprintf(buf + len, BUFLEFT,
264 " " LLFP("3") ".%02d %.2sB/s ",
265 (LLT)(bytespersec / 1024),
266 (int)((bytespersec % 1024) * 100 / 1024),
270 if (bytes <= 0 || elapsed <= 0.0 || cursize > filesize) {
271 len += snprintf(buf + len, BUFLEFT, " --:-- ETA");
272 } else if (wait.tv_sec >= STALLTIME) {
273 len += snprintf(buf + len, BUFLEFT, " - stalled -");
276 ((filesize - restart_point) / (bytes / elapsed) -
278 if (remaining >= 100 * SECSPERHOUR)
279 len += snprintf(buf + len, BUFLEFT,
282 i = remaining / SECSPERHOUR;
284 len += snprintf(buf + len, BUFLEFT,
287 len += snprintf(buf + len, BUFLEFT,
289 i = remaining % SECSPERHOUR;
290 len += snprintf(buf + len, BUFLEFT,
291 "%02d:%02d ETA", i / 60, i % 60);
296 len += snprintf(buf + len, BUFLEFT, "\n");
297 (void)write(fileno(ttyout), buf, len);
299 #endif /* !NO_PROGRESS */
302 #ifndef STANDALONE_PROGRESS
304 * Display transfer statistics.
305 * Requires start to be initialised by progressmeter(-1),
306 * direction to be defined by xfer routines, and filesize and bytes
307 * to be updated by xfer routines
308 * If siginfo is nonzero, an ETA is displayed, and the output goes to stderr
312 ptransfer(int siginfo)
314 struct timeval now, td, wait;
317 int remaining, hh, i;
320 char buf[256]; /* Work variable for transfer status. */
322 if (!verbose && !progress && !siginfo)
325 (void)gettimeofday(&now, NULL);
326 timersub(&now, &start, &td);
327 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
332 bytespersec /= elapsed;
335 len += snprintf(buf + len, BUFLEFT, LLF " byte%s %s in ",
336 (LLT)bytes, bytes == 1 ? "" : "s", direction);
337 remaining = (int)elapsed;
338 if (remaining > SECSPERDAY) {
341 days = remaining / SECSPERDAY;
342 remaining %= SECSPERDAY;
343 len += snprintf(buf + len, BUFLEFT,
344 "%d day%s ", days, days == 1 ? "" : "s");
346 hh = remaining / SECSPERHOUR;
347 remaining %= SECSPERHOUR;
349 len += snprintf(buf + len, BUFLEFT, "%2d:", hh);
350 len += snprintf(buf + len, BUFLEFT,
351 "%02d:%02d ", remaining / 60, remaining % 60);
353 for (i = 1; bytespersec >= 1024000 && i < NSUFFIXES; i++)
357 len += snprintf(buf + len, BUFLEFT, "(" LLF ".%02d %.2sB/s)",
358 (LLT)(bytespersec / 1024),
359 (int)((bytespersec % 1024) * 100 / 1024),
362 if (siginfo && bytes > 0 && elapsed > 0.0 && filesize >= 0
363 && bytes + restart_point <= filesize) {
364 remaining = (int)((filesize - restart_point) /
365 (bytes / elapsed) - elapsed);
366 hh = remaining / SECSPERHOUR;
367 remaining %= SECSPERHOUR;
368 len += snprintf(buf + len, BUFLEFT, " ETA: ");
370 len += snprintf(buf + len, BUFLEFT, "%2d:", hh);
371 len += snprintf(buf + len, BUFLEFT, "%02d:%02d",
372 remaining / 60, remaining % 60);
373 timersub(&now, &lastupdate, &wait);
374 if (wait.tv_sec >= STALLTIME)
375 len += snprintf(buf + len, BUFLEFT, " (stalled)");
377 len += snprintf(buf + len, BUFLEFT, "\n");
378 (void)write(siginfo ? STDERR_FILENO : fileno(ttyout), buf, len);
382 * SIG{INFO,QUIT} handler to print transfer stats if a transfer is in progress
385 psummary(int notused)
391 write(fileno(ttyout), "\n", 1);
396 #endif /* !STANDALONE_PROGRESS */
400 * Set the SIGALRM interval timer for wait seconds, 0 to disable.
405 struct itimerval itv;
407 itv.it_value.tv_sec = wait;
408 itv.it_value.tv_usec = 0;
409 itv.it_interval = itv.it_value;
410 setitimer(ITIMER_REAL, &itv, NULL);
415 * Install a POSIX signal handler, allowing the invoker to set whether
416 * the signal should be restartable or not
419 xsignal_restart(int sig, sigfunc func, int restartable)
421 #ifdef ultrix /* XXX: this is lame - how do we test sigvec vs. sigaction? */
422 struct sigvec vec, ovec;
424 vec.sv_handler = func;
425 sigemptyset(&vec.sv_mask);
427 if (sigvec(sig, &vec, &ovec) < 0)
429 return (ovec.sv_handler);
431 struct sigaction act, oact;
432 act.sa_handler = func;
434 sigemptyset(&act.sa_mask);
435 #if defined(SA_RESTART) /* 4.4BSD, Posix(?), SVR4 */
436 act.sa_flags = restartable ? SA_RESTART : 0;
437 #elif defined(SA_INTERRUPT) /* SunOS 4.x */
438 act.sa_flags = restartable ? 0 : SA_INTERRUPT;
440 #error "system must have SA_RESTART or SA_INTERRUPT"
442 if (sigaction(sig, &act, &oact) < 0)
444 return (oact.sa_handler);
445 #endif /* ! ultrix */
449 * Install a signal handler with the `restartable' flag set dependent upon
450 * which signal is being set. (This is a wrapper to xsignal_restart())
453 xsignal(int sig, sigfunc func)
458 * Some signals print output or change the state of the process.
459 * There should be restartable, so that reads and writes are
460 * not affected. Some signals should cause program flow to change;
461 * these signals should not be restartable, so that the system call
462 * will return with EINTR, and the program will go do something
463 * different. If the signal handler calls longjmp() or siglongjmp(),
464 * it doesn't matter if it's restartable.
486 * This is unpleasant, but I don't know what would be better.
487 * Right now, this "can't happen"
489 errx(1, "xsignal_restart: called with signal %d", sig);
492 return(xsignal_restart(sig, func, restartable));