1 /* $NetBSD: progressbar.c,v 1.20 2008/09/30 03:41:53 lukem 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.20 2008/09/30 03:41:53 lukem Exp $");
38 * FTP User Program -- Misc support routines
40 #include <sys/types.h>
41 #include <sys/param.h>
53 #include "progressbar.h"
55 #if !defined(NO_PROGRESS)
57 * return non-zero if we're the current foreground process
62 static pid_t pgrp
= -1;
67 return (tcgetpgrp(fileno(ttyout
)) == pgrp
);
69 #endif /* !defined(NO_PROGRESS) */
72 static void updateprogressmeter(int);
75 * SIGALRM handler to update the progress meter
78 updateprogressmeter(int dummy
)
87 * List of order of magnitude suffixes, per IEC 60027-2.
89 static const char * const suffixes
[] = {
91 "KiB", /* 2^10 Kibibyte */
92 "MiB", /* 2^20 Mebibyte */
93 "GiB", /* 2^30 Gibibyte */
94 "TiB", /* 2^40 Tebibyte */
95 "PiB", /* 2^50 Pebibyte */
96 "EiB", /* 2^60 Exbibyte */
98 /* The following are not necessary for signed 64-bit off_t */
99 "ZiB", /* 2^70 Zebibyte */
100 "YiB", /* 2^80 Yobibyte */
103 #define NSUFFIXES (int)(sizeof(suffixes) / sizeof(suffixes[0]))
106 * Display a transfer progress bar if progress is non-zero.
107 * SIGALRM is hijacked for use by this function.
108 * - Before the transfer, set filesize to size of file (or -1 if unknown),
109 * and call with flag = -1. This starts the once per second timer,
110 * and a call to updateprogressmeter() upon SIGALRM.
111 * - During the transfer, updateprogressmeter will call progressmeter
113 * - After the transfer, call with flag = 1
115 static struct timeval start
;
116 static struct timeval lastupdate
;
118 #define BUFLEFT (sizeof(buf) - len)
121 progressmeter(int flag
)
123 static off_t lastsize
;
125 struct timeval now
, wait
;
128 off_t abbrevsize
, bytespersec
;
130 int ratio
, i
, remaining
, barlength
;
133 * Work variables for progress bar.
135 * XXX: if the format of the progress bar changes
136 * (especially the number of characters in the
137 * `static' portion of it), be sure to update
138 * these appropriately.
142 char buf
[256]; /* workspace for progress bar */
144 #define BAROVERHEAD 45 /* non `*' portion of progress bar */
146 * stars should contain at least
147 * sizeof(buf) - BAROVERHEAD entries
149 static const char stars
[] =
150 "*****************************************************************************"
151 "*****************************************************************************"
152 "*****************************************************************************";
157 (void)gettimeofday(&start
, NULL
);
159 lastsize
= restart_point
;
162 (void)gettimeofday(&now
, NULL
);
163 cursize
= bytes
+ restart_point
;
164 timersub(&now
, &lastupdate
, &wait
);
165 if (cursize
> lastsize
) {
170 #ifndef STANDALONE_PROGRESS
171 if (quit_time
> 0 && wait
.tv_sec
> quit_time
) {
172 len
= snprintf(buf
, sizeof(buf
), "\r\n%s: "
173 "transfer aborted because stalled for %lu sec.\r\n",
174 getprogname(), (unsigned long)wait
.tv_sec
);
175 (void)write(fileno(ttyout
), buf
, len
);
177 (void)xsignal(SIGALRM
, SIG_DFL
);
178 siglongjmp(toplevel
, 1);
180 #endif /* !STANDALONE_PROGRESS */
183 * Always set the handler even if we are not the foreground process.
185 #ifdef STANDALONE_PROGRESS
188 if (quit_time
> 0 || progress
) {
189 #endif /* !STANDALONE_PROGRESS */
191 (void)xsignal_restart(SIGALRM
, updateprogressmeter
, 1);
192 alarmtimer(1); /* set alarm timer for 1 Hz */
193 } else if (flag
== 1) {
195 (void)xsignal(SIGALRM
, SIG_DFL
);
204 * print progress bar only if we are foreground process.
206 if (! foregroundproc())
209 len
+= snprintf(buf
+ len
, BUFLEFT
, "\r");
211 len
+= snprintf(buf
+ len
, BUFLEFT
, "%s", prefix
);
213 ratio
= (int)((double)cursize
* 100.0 / (double)filesize
);
214 ratio
= MAX(ratio
, 0);
215 ratio
= MIN(ratio
, 100);
216 len
+= snprintf(buf
+ len
, BUFLEFT
, "%3d%% ", ratio
);
219 * calculate the length of the `*' bar, ensuring that
220 * the number of stars won't exceed the buffer size
222 barlength
= MIN((int)(sizeof(buf
) - 1), ttywidth
) - BAROVERHEAD
;
224 barlength
-= (int)strlen(prefix
);
226 i
= barlength
* ratio
/ 100;
227 len
+= snprintf(buf
+ len
, BUFLEFT
,
228 "|%.*s%*s|", i
, stars
, (int)(barlength
- i
), "");
232 abbrevsize
= cursize
;
233 for (i
= 0; abbrevsize
>= 100000 && i
< NSUFFIXES
; i
++)
237 len
+= snprintf(buf
+ len
, BUFLEFT
, " " LLFP("5") " %-3s ",
241 timersub(&now
, &start
, &td
);
242 elapsed
= td
.tv_sec
+ (td
.tv_usec
/ 1000000.0);
248 bytespersec
/= elapsed
;
250 for (i
= 1; bytespersec
>= 1024000 && i
< NSUFFIXES
; i
++)
252 len
+= snprintf(buf
+ len
, BUFLEFT
,
253 " " LLFP("3") ".%02d %.2sB/s ",
254 (LLT
)(bytespersec
/ 1024),
255 (int)((bytespersec
% 1024) * 100 / 1024),
259 if (bytes
<= 0 || elapsed
<= 0.0 || cursize
> filesize
) {
260 len
+= snprintf(buf
+ len
, BUFLEFT
, " --:-- ETA");
261 } else if (wait
.tv_sec
>= STALLTIME
) {
262 len
+= snprintf(buf
+ len
, BUFLEFT
, " - stalled -");
265 ((filesize
- restart_point
) / (bytes
/ elapsed
) -
267 if (remaining
>= 100 * SECSPERHOUR
)
268 len
+= snprintf(buf
+ len
, BUFLEFT
,
271 i
= remaining
/ SECSPERHOUR
;
273 len
+= snprintf(buf
+ len
, BUFLEFT
,
276 len
+= snprintf(buf
+ len
, BUFLEFT
,
278 i
= remaining
% SECSPERHOUR
;
279 len
+= snprintf(buf
+ len
, BUFLEFT
,
280 "%02d:%02d ETA", i
/ 60, i
% 60);
285 len
+= snprintf(buf
+ len
, BUFLEFT
, "\n");
286 (void)write(fileno(ttyout
), buf
, len
);
288 #endif /* !NO_PROGRESS */
291 #ifndef STANDALONE_PROGRESS
293 * Display transfer statistics.
294 * Requires start to be initialised by progressmeter(-1),
295 * direction to be defined by xfer routines, and filesize and bytes
296 * to be updated by xfer routines
297 * If siginfo is nonzero, an ETA is displayed, and the output goes to stderr
301 ptransfer(int siginfo
)
303 struct timeval now
, td
, wait
;
306 int remaining
, hh
, i
;
309 char buf
[256]; /* Work variable for transfer status. */
311 if (!verbose
&& !progress
&& !siginfo
)
314 (void)gettimeofday(&now
, NULL
);
315 timersub(&now
, &start
, &td
);
316 elapsed
= td
.tv_sec
+ (td
.tv_usec
/ 1000000.0);
321 bytespersec
/= elapsed
;
324 len
+= snprintf(buf
+ len
, BUFLEFT
, LLF
" byte%s %s in ",
325 (LLT
)bytes
, bytes
== 1 ? "" : "s", direction
);
326 remaining
= (int)elapsed
;
327 if (remaining
> SECSPERDAY
) {
330 days
= remaining
/ SECSPERDAY
;
331 remaining
%= SECSPERDAY
;
332 len
+= snprintf(buf
+ len
, BUFLEFT
,
333 "%d day%s ", days
, days
== 1 ? "" : "s");
335 hh
= remaining
/ SECSPERHOUR
;
336 remaining
%= SECSPERHOUR
;
338 len
+= snprintf(buf
+ len
, BUFLEFT
, "%2d:", hh
);
339 len
+= snprintf(buf
+ len
, BUFLEFT
,
340 "%02d:%02d ", remaining
/ 60, remaining
% 60);
342 for (i
= 1; bytespersec
>= 1024000 && i
< NSUFFIXES
; i
++)
346 len
+= snprintf(buf
+ len
, BUFLEFT
, "(" LLF
".%02d %.2sB/s)",
347 (LLT
)(bytespersec
/ 1024),
348 (int)((bytespersec
% 1024) * 100 / 1024),
351 if (siginfo
&& bytes
> 0 && elapsed
> 0.0 && filesize
>= 0
352 && bytes
+ restart_point
<= filesize
) {
353 remaining
= (int)((filesize
- restart_point
) /
354 (bytes
/ elapsed
) - elapsed
);
355 hh
= remaining
/ SECSPERHOUR
;
356 remaining
%= SECSPERHOUR
;
357 len
+= snprintf(buf
+ len
, BUFLEFT
, " ETA: ");
359 len
+= snprintf(buf
+ len
, BUFLEFT
, "%2d:", hh
);
360 len
+= snprintf(buf
+ len
, BUFLEFT
, "%02d:%02d",
361 remaining
/ 60, remaining
% 60);
362 timersub(&now
, &lastupdate
, &wait
);
363 if (wait
.tv_sec
>= STALLTIME
)
364 len
+= snprintf(buf
+ len
, BUFLEFT
, " (stalled)");
366 len
+= snprintf(buf
+ len
, BUFLEFT
, "\n");
367 (void)write(siginfo
? STDERR_FILENO
: fileno(ttyout
), buf
, len
);
371 * SIG{INFO,QUIT} handler to print transfer stats if a transfer is in progress
374 psummary(int notused
)
380 write(fileno(ttyout
), "\n", 1);
385 #endif /* !STANDALONE_PROGRESS */
389 * Set the SIGALRM interval timer for wait seconds, 0 to disable.
394 struct itimerval itv
;
396 itv
.it_value
.tv_sec
= wait
;
397 itv
.it_value
.tv_usec
= 0;
398 itv
.it_interval
= itv
.it_value
;
399 setitimer(ITIMER_REAL
, &itv
, NULL
);
404 * Install a POSIX signal handler, allowing the invoker to set whether
405 * the signal should be restartable or not
408 xsignal_restart(int sig
, sigfunc func
, int restartable
)
410 struct sigaction act
, oact
;
411 act
.sa_handler
= func
;
413 sigemptyset(&act
.sa_mask
);
414 #if defined(SA_RESTART) /* 4.4BSD, Posix(?), SVR4 */
415 act
.sa_flags
= restartable
? SA_RESTART
: 0;
416 #elif defined(SA_INTERRUPT) /* SunOS 4.x */
417 act
.sa_flags
= restartable
? 0 : SA_INTERRUPT
;
419 #error "system must have SA_RESTART or SA_INTERRUPT"
421 if (sigaction(sig
, &act
, &oact
) < 0)
423 return (oact
.sa_handler
);
427 * Install a signal handler with the `restartable' flag set dependent upon
428 * which signal is being set. (This is a wrapper to xsignal_restart())
431 xsignal(int sig
, sigfunc func
)
436 * Some signals print output or change the state of the process.
437 * There should be restartable, so that reads and writes are
438 * not affected. Some signals should cause program flow to change;
439 * these signals should not be restartable, so that the system call
440 * will return with EINTR, and the program will go do something
441 * different. If the signal handler calls longjmp() or siglongjmp(),
442 * it doesn't matter if it's restartable.
464 * This is unpleasant, but I don't know what would be better.
465 * Right now, this "can't happen"
467 errx(1, "xsignal_restart: called with signal %d", sig
);
470 return(xsignal_restart(sig
, func
, restartable
));