]>
git.cameronkatri.com Git - bsd-progress.git/blob - progressbar.c
1 /* $NetBSD: progressbar.c,v 1.17 2007/05/05 18:09:24 martin Exp $ */
4 * Copyright (c) 1997-2007 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.17 2007/05/05 18:09:24 martin 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 suffixes, per IEC 60027-2.
96 static const char * const suffixes
[] = {
98 "KiB", /* 2^10 Kibibyte */
99 "MiB", /* 2^20 Mebibyte */
100 "GiB", /* 2^30 Gibibyte */
101 "TiB", /* 2^40 Tebibyte */
102 "PiB", /* 2^50 Pebibyte */
103 "EiB", /* 2^60 Exbibyte */
105 /* The following are not necessary for signed 64-bit off_t */
106 "ZiB", /* 2^70 Zebibyte */
107 "YiB", /* 2^80 Yobibyte */
110 #define NSUFFIXES (sizeof(suffixes) / sizeof(suffixes[0]))
113 * Display a transfer progress bar if progress is non-zero.
114 * SIGALRM is hijacked for use by this function.
115 * - Before the transfer, set filesize to size of file (or -1 if unknown),
116 * and call with flag = -1. This starts the once per second timer,
117 * and a call to updateprogressmeter() upon SIGALRM.
118 * - During the transfer, updateprogressmeter will call progressmeter
120 * - After the transfer, call with flag = 1
122 static struct timeval start
;
123 static struct timeval lastupdate
;
125 #define BUFLEFT (sizeof(buf) - len)
128 progressmeter(int flag
)
130 static off_t lastsize
;
132 struct timeval now
, wait
;
135 off_t abbrevsize
, bytespersec
;
137 int ratio
, i
, remaining
, barlength
;
140 * Work variables for progress bar.
142 * XXX: if the format of the progress bar changes
143 * (especially the number of characters in the
144 * `static' portion of it), be sure to update
145 * these appropriately.
149 char buf
[256]; /* workspace for progress bar */
151 #define BAROVERHEAD 45 /* non `*' portion of progress bar */
153 * stars should contain at least
154 * sizeof(buf) - BAROVERHEAD entries
156 static const char stars
[] =
157 "*****************************************************************************"
158 "*****************************************************************************"
159 "*****************************************************************************";
164 (void)gettimeofday(&start
, NULL
);
166 lastsize
= restart_point
;
169 (void)gettimeofday(&now
, NULL
);
170 cursize
= bytes
+ restart_point
;
171 timersub(&now
, &lastupdate
, &wait
);
172 if (cursize
> lastsize
) {
177 #ifndef STANDALONE_PROGRESS
178 if (quit_time
> 0 && wait
.tv_sec
> quit_time
) {
179 len
= snprintf(buf
, sizeof(buf
), "\r\n%s: "
180 "transfer aborted because stalled for %lu sec.\r\n",
181 getprogname(), (unsigned long)wait
.tv_sec
);
182 (void)write(fileno(ttyout
), buf
, len
);
183 (void)xsignal(SIGALRM
, SIG_DFL
);
185 siglongjmp(toplevel
, 1);
187 #endif /* !STANDALONE_PROGRESS */
190 * Always set the handler even if we are not the foreground process.
192 #ifdef STANDALONE_PROGRESS
195 if (quit_time
> 0 || progress
) {
196 #endif /* !STANDALONE_PROGRESS */
198 (void)xsignal_restart(SIGALRM
, updateprogressmeter
, 1);
199 alarmtimer(1); /* set alarm timer for 1 Hz */
200 } else if (flag
== 1) {
201 (void)xsignal(SIGALRM
, SIG_DFL
);
211 * print progress bar only if we are foreground process.
213 if (! foregroundproc())
216 len
+= snprintf(buf
+ len
, BUFLEFT
, "\r");
218 len
+= snprintf(buf
+ len
, BUFLEFT
, "%s", prefix
);
220 ratio
= (int)((double)cursize
* 100.0 / (double)filesize
);
221 ratio
= MAX(ratio
, 0);
222 ratio
= MIN(ratio
, 100);
223 len
+= snprintf(buf
+ len
, BUFLEFT
, "%3d%% ", ratio
);
226 * calculate the length of the `*' bar, ensuring that
227 * the number of stars won't exceed the buffer size
229 barlength
= MIN(sizeof(buf
) - 1, ttywidth
) - BAROVERHEAD
;
231 barlength
-= (int)strlen(prefix
);
233 i
= barlength
* ratio
/ 100;
234 len
+= snprintf(buf
+ len
, BUFLEFT
,
235 "|%.*s%*s|", i
, stars
, (int)(barlength
- i
), "");
239 abbrevsize
= cursize
;
240 for (i
= 0; abbrevsize
>= 100000 && i
< NSUFFIXES
; i
++)
244 len
+= snprintf(buf
+ len
, BUFLEFT
, " " LLFP("5") " %-3s ",
248 timersub(&now
, &start
, &td
);
249 elapsed
= td
.tv_sec
+ (td
.tv_usec
/ 1000000.0);
255 bytespersec
/= elapsed
;
257 for (i
= 1; bytespersec
>= 1024000 && i
< NSUFFIXES
; i
++)
259 len
+= snprintf(buf
+ len
, BUFLEFT
,
260 " " LLFP("3") ".%02d %.2sB/s ",
261 (LLT
)(bytespersec
/ 1024),
262 (int)((bytespersec
% 1024) * 100 / 1024),
266 if (bytes
<= 0 || elapsed
<= 0.0 || cursize
> filesize
) {
267 len
+= snprintf(buf
+ len
, BUFLEFT
, " --:-- ETA");
268 } else if (wait
.tv_sec
>= STALLTIME
) {
269 len
+= snprintf(buf
+ len
, BUFLEFT
, " - stalled -");
272 ((filesize
- restart_point
) / (bytes
/ elapsed
) -
274 if (remaining
>= 100 * SECSPERHOUR
)
275 len
+= snprintf(buf
+ len
, BUFLEFT
,
278 i
= remaining
/ SECSPERHOUR
;
280 len
+= snprintf(buf
+ len
, BUFLEFT
,
283 len
+= snprintf(buf
+ len
, BUFLEFT
,
285 i
= remaining
% SECSPERHOUR
;
286 len
+= snprintf(buf
+ len
, BUFLEFT
,
287 "%02d:%02d ETA", i
/ 60, i
% 60);
292 len
+= snprintf(buf
+ len
, BUFLEFT
, "\n");
293 (void)write(fileno(ttyout
), buf
, len
);
295 #endif /* !NO_PROGRESS */
298 #ifndef STANDALONE_PROGRESS
300 * Display transfer statistics.
301 * Requires start to be initialised by progressmeter(-1),
302 * direction to be defined by xfer routines, and filesize and bytes
303 * to be updated by xfer routines
304 * If siginfo is nonzero, an ETA is displayed, and the output goes to stderr
308 ptransfer(int siginfo
)
310 struct timeval now
, td
, wait
;
313 int remaining
, hh
, i
;
316 char buf
[256]; /* Work variable for transfer status. */
318 if (!verbose
&& !progress
&& !siginfo
)
321 (void)gettimeofday(&now
, NULL
);
322 timersub(&now
, &start
, &td
);
323 elapsed
= td
.tv_sec
+ (td
.tv_usec
/ 1000000.0);
328 bytespersec
/= elapsed
;
331 len
+= snprintf(buf
+ len
, BUFLEFT
, LLF
" byte%s %s in ",
332 (LLT
)bytes
, bytes
== 1 ? "" : "s", direction
);
333 remaining
= (int)elapsed
;
334 if (remaining
> SECSPERDAY
) {
337 days
= remaining
/ SECSPERDAY
;
338 remaining
%= SECSPERDAY
;
339 len
+= snprintf(buf
+ len
, BUFLEFT
,
340 "%d day%s ", days
, days
== 1 ? "" : "s");
342 hh
= remaining
/ SECSPERHOUR
;
343 remaining
%= SECSPERHOUR
;
345 len
+= snprintf(buf
+ len
, BUFLEFT
, "%2d:", hh
);
346 len
+= snprintf(buf
+ len
, BUFLEFT
,
347 "%02d:%02d ", remaining
/ 60, remaining
% 60);
349 for (i
= 1; bytespersec
>= 1024000 && i
< NSUFFIXES
; i
++)
353 len
+= snprintf(buf
+ len
, BUFLEFT
, "(" LLF
".%02d %.2sB/s)",
354 (LLT
)(bytespersec
/ 1024),
355 (int)((bytespersec
% 1024) * 100 / 1024),
358 if (siginfo
&& bytes
> 0 && elapsed
> 0.0 && filesize
>= 0
359 && bytes
+ restart_point
<= filesize
) {
360 remaining
= (int)((filesize
- restart_point
) /
361 (bytes
/ elapsed
) - elapsed
);
362 hh
= remaining
/ SECSPERHOUR
;
363 remaining
%= SECSPERHOUR
;
364 len
+= snprintf(buf
+ len
, BUFLEFT
, " ETA: ");
366 len
+= snprintf(buf
+ len
, BUFLEFT
, "%2d:", hh
);
367 len
+= snprintf(buf
+ len
, BUFLEFT
, "%02d:%02d",
368 remaining
/ 60, remaining
% 60);
369 timersub(&now
, &lastupdate
, &wait
);
370 if (wait
.tv_sec
>= STALLTIME
)
371 len
+= snprintf(buf
+ len
, BUFLEFT
, " (stalled)");
373 len
+= snprintf(buf
+ len
, BUFLEFT
, "\n");
374 (void)write(siginfo
? STDERR_FILENO
: fileno(ttyout
), buf
, len
);
378 * SIG{INFO,QUIT} handler to print transfer stats if a transfer is in progress
381 psummary(int notused
)
387 write(fileno(ttyout
), "\n", 1);
392 #endif /* !STANDALONE_PROGRESS */
396 * Set the SIGALRM interval timer for wait seconds, 0 to disable.
401 struct itimerval itv
;
403 itv
.it_value
.tv_sec
= wait
;
404 itv
.it_value
.tv_usec
= 0;
405 itv
.it_interval
= itv
.it_value
;
406 setitimer(ITIMER_REAL
, &itv
, NULL
);
411 * Install a POSIX signal handler, allowing the invoker to set whether
412 * the signal should be restartable or not
415 xsignal_restart(int sig
, sigfunc func
, int restartable
)
417 struct sigaction act
, oact
;
418 act
.sa_handler
= func
;
420 sigemptyset(&act
.sa_mask
);
421 #if defined(SA_RESTART) /* 4.4BSD, Posix(?), SVR4 */
422 act
.sa_flags
= restartable
? SA_RESTART
: 0;
423 #elif defined(SA_INTERRUPT) /* SunOS 4.x */
424 act
.sa_flags
= restartable
? 0 : SA_INTERRUPT
;
426 #error "system must have SA_RESTART or SA_INTERRUPT"
428 if (sigaction(sig
, &act
, &oact
) < 0)
430 return (oact
.sa_handler
);
434 * Install a signal handler with the `restartable' flag set dependent upon
435 * which signal is being set. (This is a wrapper to xsignal_restart())
438 xsignal(int sig
, sigfunc func
)
443 * Some signals print output or change the state of the process.
444 * There should be restartable, so that reads and writes are
445 * not affected. Some signals should cause program flow to change;
446 * these signals should not be restartable, so that the system call
447 * will return with EINTR, and the program will go do something
448 * different. If the signal handler calls longjmp() or siglongjmp(),
449 * it doesn't matter if it's restartable.
471 * This is unpleasant, but I don't know what would be better.
472 * Right now, this "can't happen"
474 errx(1, "xsignal_restart: called with signal %d", sig
);
477 return(xsignal_restart(sig
, func
, restartable
));