1 /* $NetBSD: progressbar.c,v 1.22 2012/06/27 22:07:36 riastradh Exp $ */
4 * Copyright (c) 1997-2009 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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: progressbar.c,v 1.22 2012/06/27 22:07:36 riastradh Exp $");
38 * FTP User Program -- Misc support routines
40 #include <sys/param.h>
41 #include <sys/types.h>
54 #include "progressbar.h"
56 #if !defined(NO_PROGRESS)
58 * return non-zero if we're the current foreground process
63 static pid_t pgrp
= -1;
68 return (tcgetpgrp(fileno(ttyout
)) == pgrp
);
70 #endif /* !defined(NO_PROGRESS) */
73 static void updateprogressmeter(int);
76 * SIGALRM handler to update the progress meter
79 updateprogressmeter(int dummy
)
88 * List of order of magnitude suffixes, per IEC 60027-2.
90 static const char * const suffixes
[] = {
92 "KiB", /* 2^10 Kibibyte */
93 "MiB", /* 2^20 Mebibyte */
94 "GiB", /* 2^30 Gibibyte */
95 "TiB", /* 2^40 Tebibyte */
96 "PiB", /* 2^50 Pebibyte */
97 "EiB", /* 2^60 Exbibyte */
99 /* The following are not necessary for signed 64-bit off_t */
100 "ZiB", /* 2^70 Zebibyte */
101 "YiB", /* 2^80 Yobibyte */
104 #define NSUFFIXES (int)(sizeof(suffixes) / sizeof(suffixes[0]))
107 * Display a transfer progress bar if progress is non-zero.
108 * SIGALRM is hijacked for use by this function.
109 * - Before the transfer, set filesize to size of file (or -1 if unknown),
110 * and call with flag = -1. This starts the once per second timer,
111 * and a call to updateprogressmeter() upon SIGALRM.
112 * - During the transfer, updateprogressmeter will call progressmeter
114 * - After the transfer, call with flag = 1
116 static struct timeval start
;
117 static struct timeval lastupdate
;
119 #define BUFLEFT (sizeof(buf) - len)
122 progressmeter(int flag
)
124 static off_t lastsize
;
126 struct timeval now
, wait
;
129 off_t abbrevsize
, bytespersec
;
131 int ratio
, i
, remaining
, barlength
;
134 * Work variables for progress bar.
136 * XXX: if the format of the progress bar changes
137 * (especially the number of characters in the
138 * `static' portion of it), be sure to update
139 * these appropriately.
143 char buf
[256]; /* workspace for progress bar */
145 #define BAROVERHEAD 45 /* non `*' portion of progress bar */
147 * stars should contain at least
148 * sizeof(buf) - BAROVERHEAD entries
150 static const char stars
[] =
151 "*****************************************************************************"
152 "*****************************************************************************"
153 "*****************************************************************************";
158 (void)gettimeofday(&start
, NULL
);
160 lastsize
= restart_point
;
163 (void)gettimeofday(&now
, NULL
);
164 cursize
= bytes
+ restart_point
;
165 timersub(&now
, &lastupdate
, &wait
);
166 if (cursize
> lastsize
) {
171 #ifndef STANDALONE_PROGRESS
172 if (quit_time
> 0 && wait
.tv_sec
> quit_time
) {
173 len
= snprintf(buf
, sizeof(buf
), "\r\n%s: "
174 "transfer aborted because stalled for %lu sec.\r\n",
175 getprogname(), (unsigned long)wait
.tv_sec
);
176 (void)write(fileno(ttyout
), buf
, len
);
178 (void)xsignal(SIGALRM
, SIG_DFL
);
179 siglongjmp(toplevel
, 1);
181 #endif /* !STANDALONE_PROGRESS */
184 * Always set the handler even if we are not the foreground process.
186 #ifdef STANDALONE_PROGRESS
189 if (quit_time
> 0 || progress
) {
190 #endif /* !STANDALONE_PROGRESS */
192 (void)xsignal_restart(SIGALRM
, updateprogressmeter
, 1);
193 alarmtimer(1); /* set alarm timer for 1 Hz */
194 } else if (flag
== 1) {
196 (void)xsignal(SIGALRM
, SIG_DFL
);
205 * print progress bar only if we are foreground process.
207 if (! foregroundproc())
210 len
+= snprintf(buf
+ len
, BUFLEFT
, "\r");
212 len
+= snprintf(buf
+ len
, BUFLEFT
, "%s", prefix
);
214 ratio
= (int)((double)cursize
* 100.0 / (double)filesize
);
215 ratio
= MAX(ratio
, 0);
216 ratio
= MIN(ratio
, 100);
217 len
+= snprintf(buf
+ len
, BUFLEFT
, "%3d%% ", ratio
);
220 * calculate the length of the `*' bar, ensuring that
221 * the number of stars won't exceed the buffer size
223 barlength
= MIN((int)(sizeof(buf
) - 1), ttywidth
) - BAROVERHEAD
;
225 barlength
-= (int)strlen(prefix
);
227 i
= barlength
* ratio
/ 100;
228 len
+= snprintf(buf
+ len
, BUFLEFT
,
229 "|%.*s%*s|", i
, stars
, (int)(barlength
- i
), "");
233 abbrevsize
= cursize
;
234 for (i
= 0; abbrevsize
>= 100000 && i
< NSUFFIXES
; i
++)
238 len
+= snprintf(buf
+ len
, BUFLEFT
, " " LLFP("5") " %-3s ",
242 timersub(&now
, &start
, &td
);
243 elapsed
= td
.tv_sec
+ (td
.tv_usec
/ 1000000.0);
249 bytespersec
/= elapsed
;
251 for (i
= 1; bytespersec
>= 1024000 && i
< NSUFFIXES
; i
++)
253 len
+= snprintf(buf
+ len
, BUFLEFT
,
254 " " LLFP("3") ".%02d %.2sB/s ",
255 (LLT
)(bytespersec
/ 1024),
256 (int)((bytespersec
% 1024) * 100 / 1024),
260 if (bytes
<= 0 || elapsed
<= 0.0 || cursize
> filesize
) {
261 len
+= snprintf(buf
+ len
, BUFLEFT
, " --:-- ETA");
262 } else if (wait
.tv_sec
>= STALLTIME
) {
263 len
+= snprintf(buf
+ len
, BUFLEFT
, " - stalled -");
266 ((filesize
- restart_point
) / (bytes
/ elapsed
) -
268 if (remaining
>= 100 * SECSPERHOUR
)
269 len
+= snprintf(buf
+ len
, BUFLEFT
,
272 i
= remaining
/ SECSPERHOUR
;
274 len
+= snprintf(buf
+ len
, BUFLEFT
,
277 len
+= snprintf(buf
+ len
, BUFLEFT
,
279 i
= remaining
% SECSPERHOUR
;
280 len
+= snprintf(buf
+ len
, BUFLEFT
,
281 "%02d:%02d ETA", i
/ 60, i
% 60);
286 len
+= snprintf(buf
+ len
, BUFLEFT
, "\n");
287 (void)write(fileno(ttyout
), buf
, len
);
289 #endif /* !NO_PROGRESS */
292 #ifndef STANDALONE_PROGRESS
294 * Display transfer statistics.
295 * Requires start to be initialised by progressmeter(-1),
296 * direction to be defined by xfer routines, and filesize and bytes
297 * to be updated by xfer routines
298 * If siginfo is nonzero, an ETA is displayed, and the output goes to stderr
302 ptransfer(int siginfo
)
304 struct timeval now
, td
, wait
;
307 int remaining
, hh
, i
;
310 char buf
[256]; /* Work variable for transfer status. */
312 if (!verbose
&& !progress
&& !siginfo
)
315 (void)gettimeofday(&now
, NULL
);
316 timersub(&now
, &start
, &td
);
317 elapsed
= td
.tv_sec
+ (td
.tv_usec
/ 1000000.0);
322 bytespersec
/= elapsed
;
325 len
+= snprintf(buf
+ len
, BUFLEFT
, LLF
" byte%s %s in ",
326 (LLT
)bytes
, bytes
== 1 ? "" : "s", direction
);
327 remaining
= (int)elapsed
;
328 if (remaining
> SECSPERDAY
) {
331 days
= remaining
/ SECSPERDAY
;
332 remaining
%= SECSPERDAY
;
333 len
+= snprintf(buf
+ len
, BUFLEFT
,
334 "%d day%s ", days
, days
== 1 ? "" : "s");
336 hh
= remaining
/ SECSPERHOUR
;
337 remaining
%= SECSPERHOUR
;
339 len
+= snprintf(buf
+ len
, BUFLEFT
, "%2d:", hh
);
340 len
+= snprintf(buf
+ len
, BUFLEFT
,
341 "%02d:%02d ", remaining
/ 60, remaining
% 60);
343 for (i
= 1; bytespersec
>= 1024000 && i
< NSUFFIXES
; i
++)
347 len
+= snprintf(buf
+ len
, BUFLEFT
, "(" LLF
".%02d %.2sB/s)",
348 (LLT
)(bytespersec
/ 1024),
349 (int)((bytespersec
% 1024) * 100 / 1024),
352 if (siginfo
&& bytes
> 0 && elapsed
> 0.0 && filesize
>= 0
353 && bytes
+ restart_point
<= filesize
) {
354 remaining
= (int)((filesize
- restart_point
) /
355 (bytes
/ elapsed
) - elapsed
);
356 hh
= remaining
/ SECSPERHOUR
;
357 remaining
%= SECSPERHOUR
;
358 len
+= snprintf(buf
+ len
, BUFLEFT
, " ETA: ");
360 len
+= snprintf(buf
+ len
, BUFLEFT
, "%2d:", hh
);
361 len
+= snprintf(buf
+ len
, BUFLEFT
, "%02d:%02d",
362 remaining
/ 60, remaining
% 60);
363 timersub(&now
, &lastupdate
, &wait
);
364 if (wait
.tv_sec
>= STALLTIME
)
365 len
+= snprintf(buf
+ len
, BUFLEFT
, " (stalled)");
367 len
+= snprintf(buf
+ len
, BUFLEFT
, "\n");
368 (void)write(siginfo
? STDERR_FILENO
: fileno(ttyout
), buf
, len
);
372 * SIG{INFO,QUIT} handler to print transfer stats if a transfer is in progress
375 psummary(int notused
)
381 write(fileno(ttyout
), "\n", 1);
386 #endif /* !STANDALONE_PROGRESS */
390 * Set the SIGALRM interval timer for wait seconds, 0 to disable.
395 struct itimerval itv
;
397 itv
.it_value
.tv_sec
= wait
;
398 itv
.it_value
.tv_usec
= 0;
399 itv
.it_interval
= itv
.it_value
;
400 setitimer(ITIMER_REAL
, &itv
, NULL
);
405 * Install a POSIX signal handler, allowing the invoker to set whether
406 * the signal should be restartable or not
409 xsignal_restart(int sig
, sigfunc func
, int restartable
)
411 struct sigaction act
, oact
;
412 act
.sa_handler
= func
;
414 sigemptyset(&act
.sa_mask
);
415 #if defined(SA_RESTART) /* 4.4BSD, Posix(?), SVR4 */
416 act
.sa_flags
= restartable
? SA_RESTART
: 0;
417 #elif defined(SA_INTERRUPT) /* SunOS 4.x */
418 act
.sa_flags
= restartable
? 0 : SA_INTERRUPT
;
420 #error "system must have SA_RESTART or SA_INTERRUPT"
422 if (sigaction(sig
, &act
, &oact
) < 0)
424 return (oact
.sa_handler
);
428 * Install a signal handler with the `restartable' flag set dependent upon
429 * which signal is being set. (This is a wrapper to xsignal_restart())
432 xsignal(int sig
, sigfunc func
)
437 * Some signals print output or change the state of the process.
438 * There should be restartable, so that reads and writes are
439 * not affected. Some signals should cause program flow to change;
440 * these signals should not be restartable, so that the system call
441 * will return with EINTR, and the program will go do something
442 * different. If the signal handler calls longjmp() or siglongjmp(),
443 * it doesn't matter if it's restartable.
465 * This is unpleasant, but I don't know what would be better.
466 * Right now, this "can't happen"
468 errx(1, "xsignal_restart: called with signal %d", sig
);
471 return(xsignal_restart(sig
, func
, restartable
));