xref: /minix3/usr.bin/ftp/progressbar.c (revision 04203a83a6848544e5157be39991291ba0c82525)
1*04203a83SThomas Cort /*	$NetBSD: progressbar.c,v 1.22 2012/06/27 22:07:36 riastradh Exp $	*/
2*04203a83SThomas Cort 
3*04203a83SThomas Cort /*-
4*04203a83SThomas Cort  * Copyright (c) 1997-2009 The NetBSD Foundation, Inc.
5*04203a83SThomas Cort  * All rights reserved.
6*04203a83SThomas Cort  *
7*04203a83SThomas Cort  * This code is derived from software contributed to The NetBSD Foundation
8*04203a83SThomas Cort  * by Luke Mewburn.
9*04203a83SThomas Cort  *
10*04203a83SThomas Cort  * Redistribution and use in source and binary forms, with or without
11*04203a83SThomas Cort  * modification, are permitted provided that the following conditions
12*04203a83SThomas Cort  * are met:
13*04203a83SThomas Cort  * 1. Redistributions of source code must retain the above copyright
14*04203a83SThomas Cort  *    notice, this list of conditions and the following disclaimer.
15*04203a83SThomas Cort  * 2. Redistributions in binary form must reproduce the above copyright
16*04203a83SThomas Cort  *    notice, this list of conditions and the following disclaimer in the
17*04203a83SThomas Cort  *    documentation and/or other materials provided with the distribution.
18*04203a83SThomas Cort  *
19*04203a83SThomas Cort  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*04203a83SThomas Cort  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*04203a83SThomas Cort  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*04203a83SThomas Cort  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*04203a83SThomas Cort  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*04203a83SThomas Cort  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*04203a83SThomas Cort  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*04203a83SThomas Cort  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*04203a83SThomas Cort  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*04203a83SThomas Cort  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*04203a83SThomas Cort  * POSSIBILITY OF SUCH DAMAGE.
30*04203a83SThomas Cort  */
31*04203a83SThomas Cort 
32*04203a83SThomas Cort #include <sys/cdefs.h>
33*04203a83SThomas Cort #ifndef lint
34*04203a83SThomas Cort __RCSID("$NetBSD: progressbar.c,v 1.22 2012/06/27 22:07:36 riastradh Exp $");
35*04203a83SThomas Cort #endif /* not lint */
36*04203a83SThomas Cort 
37*04203a83SThomas Cort /*
38*04203a83SThomas Cort  * FTP User Program -- Misc support routines
39*04203a83SThomas Cort  */
40*04203a83SThomas Cort #include <sys/param.h>
41*04203a83SThomas Cort #include <sys/types.h>
42*04203a83SThomas Cort #include <sys/time.h>
43*04203a83SThomas Cort 
44*04203a83SThomas Cort #include <err.h>
45*04203a83SThomas Cort #include <errno.h>
46*04203a83SThomas Cort #include <signal.h>
47*04203a83SThomas Cort #include <stdio.h>
48*04203a83SThomas Cort #include <stdlib.h>
49*04203a83SThomas Cort #include <string.h>
50*04203a83SThomas Cort #include <time.h>
51*04203a83SThomas Cort #include <tzfile.h>
52*04203a83SThomas Cort #include <unistd.h>
53*04203a83SThomas Cort 
54*04203a83SThomas Cort #include "progressbar.h"
55*04203a83SThomas Cort 
56*04203a83SThomas Cort #if !defined(NO_PROGRESS)
57*04203a83SThomas Cort /*
58*04203a83SThomas Cort  * return non-zero if we're the current foreground process
59*04203a83SThomas Cort  */
60*04203a83SThomas Cort int
foregroundproc(void)61*04203a83SThomas Cort foregroundproc(void)
62*04203a83SThomas Cort {
63*04203a83SThomas Cort 	static pid_t pgrp = -1;
64*04203a83SThomas Cort 
65*04203a83SThomas Cort 	if (pgrp == -1)
66*04203a83SThomas Cort 		pgrp = getpgrp();
67*04203a83SThomas Cort 
68*04203a83SThomas Cort 	return (tcgetpgrp(fileno(ttyout)) == pgrp);
69*04203a83SThomas Cort }
70*04203a83SThomas Cort #endif	/* !defined(NO_PROGRESS) */
71*04203a83SThomas Cort 
72*04203a83SThomas Cort 
73*04203a83SThomas Cort static void updateprogressmeter(int);
74*04203a83SThomas Cort 
75*04203a83SThomas Cort /*
76*04203a83SThomas Cort  * SIGALRM handler to update the progress meter
77*04203a83SThomas Cort  */
78*04203a83SThomas Cort static void
updateprogressmeter(int dummy)79*04203a83SThomas Cort updateprogressmeter(int dummy)
80*04203a83SThomas Cort {
81*04203a83SThomas Cort 	int oerrno = errno;
82*04203a83SThomas Cort 
83*04203a83SThomas Cort 	progressmeter(0);
84*04203a83SThomas Cort 	errno = oerrno;
85*04203a83SThomas Cort }
86*04203a83SThomas Cort 
87*04203a83SThomas Cort /*
88*04203a83SThomas Cort  * List of order of magnitude suffixes, per IEC 60027-2.
89*04203a83SThomas Cort  */
90*04203a83SThomas Cort static const char * const suffixes[] = {
91*04203a83SThomas Cort 	"",	/* 2^0  (byte) */
92*04203a83SThomas Cort 	"KiB",	/* 2^10 Kibibyte */
93*04203a83SThomas Cort 	"MiB",	/* 2^20 Mebibyte */
94*04203a83SThomas Cort 	"GiB",	/* 2^30 Gibibyte */
95*04203a83SThomas Cort 	"TiB",	/* 2^40 Tebibyte */
96*04203a83SThomas Cort 	"PiB",	/* 2^50 Pebibyte */
97*04203a83SThomas Cort 	"EiB",	/* 2^60 Exbibyte */
98*04203a83SThomas Cort #if 0
99*04203a83SThomas Cort 		/* The following are not necessary for signed 64-bit off_t */
100*04203a83SThomas Cort 	"ZiB",	/* 2^70 Zebibyte */
101*04203a83SThomas Cort 	"YiB",	/* 2^80 Yobibyte */
102*04203a83SThomas Cort #endif
103*04203a83SThomas Cort };
104*04203a83SThomas Cort #define NSUFFIXES	(int)(sizeof(suffixes) / sizeof(suffixes[0]))
105*04203a83SThomas Cort 
106*04203a83SThomas Cort /*
107*04203a83SThomas Cort  * Display a transfer progress bar if progress is non-zero.
108*04203a83SThomas Cort  * SIGALRM is hijacked for use by this function.
109*04203a83SThomas Cort  * - Before the transfer, set filesize to size of file (or -1 if unknown),
110*04203a83SThomas Cort  *   and call with flag = -1. This starts the once per second timer,
111*04203a83SThomas Cort  *   and a call to updateprogressmeter() upon SIGALRM.
112*04203a83SThomas Cort  * - During the transfer, updateprogressmeter will call progressmeter
113*04203a83SThomas Cort  *   with flag = 0
114*04203a83SThomas Cort  * - After the transfer, call with flag = 1
115*04203a83SThomas Cort  */
116*04203a83SThomas Cort static struct timeval start;
117*04203a83SThomas Cort static struct timeval lastupdate;
118*04203a83SThomas Cort 
119*04203a83SThomas Cort #define	BUFLEFT	(sizeof(buf) - len)
120*04203a83SThomas Cort 
121*04203a83SThomas Cort void
progressmeter(int flag)122*04203a83SThomas Cort progressmeter(int flag)
123*04203a83SThomas Cort {
124*04203a83SThomas Cort 	static off_t lastsize;
125*04203a83SThomas Cort 	off_t cursize;
126*04203a83SThomas Cort 	struct timeval now, wait;
127*04203a83SThomas Cort #ifndef NO_PROGRESS
128*04203a83SThomas Cort 	struct timeval td;
129*04203a83SThomas Cort 	off_t abbrevsize, bytespersec;
130*04203a83SThomas Cort 	double elapsed;
131*04203a83SThomas Cort 	int ratio, i, remaining, barlength;
132*04203a83SThomas Cort 
133*04203a83SThomas Cort 			/*
134*04203a83SThomas Cort 			 * Work variables for progress bar.
135*04203a83SThomas Cort 			 *
136*04203a83SThomas Cort 			 * XXX:	if the format of the progress bar changes
137*04203a83SThomas Cort 			 *	(especially the number of characters in the
138*04203a83SThomas Cort 			 *	`static' portion of it), be sure to update
139*04203a83SThomas Cort 			 *	these appropriately.
140*04203a83SThomas Cort 			 */
141*04203a83SThomas Cort #endif
142*04203a83SThomas Cort 	size_t		len;
143*04203a83SThomas Cort 	char		buf[256];	/* workspace for progress bar */
144*04203a83SThomas Cort #ifndef NO_PROGRESS
145*04203a83SThomas Cort #define	BAROVERHEAD	45		/* non `*' portion of progress bar */
146*04203a83SThomas Cort 					/*
147*04203a83SThomas Cort 					 * stars should contain at least
148*04203a83SThomas Cort 					 * sizeof(buf) - BAROVERHEAD entries
149*04203a83SThomas Cort 					 */
150*04203a83SThomas Cort 	static const char	stars[] =
151*04203a83SThomas Cort "*****************************************************************************"
152*04203a83SThomas Cort "*****************************************************************************"
153*04203a83SThomas Cort "*****************************************************************************";
154*04203a83SThomas Cort 
155*04203a83SThomas Cort #endif
156*04203a83SThomas Cort 
157*04203a83SThomas Cort 	if (flag == -1) {
158*04203a83SThomas Cort 		(void)gettimeofday(&start, NULL);
159*04203a83SThomas Cort 		lastupdate = start;
160*04203a83SThomas Cort 		lastsize = restart_point;
161*04203a83SThomas Cort 	}
162*04203a83SThomas Cort 
163*04203a83SThomas Cort 	(void)gettimeofday(&now, NULL);
164*04203a83SThomas Cort 	cursize = bytes + restart_point;
165*04203a83SThomas Cort 	timersub(&now, &lastupdate, &wait);
166*04203a83SThomas Cort 	if (cursize > lastsize) {
167*04203a83SThomas Cort 		lastupdate = now;
168*04203a83SThomas Cort 		lastsize = cursize;
169*04203a83SThomas Cort 		wait.tv_sec = 0;
170*04203a83SThomas Cort 	} else {
171*04203a83SThomas Cort #ifndef STANDALONE_PROGRESS
172*04203a83SThomas Cort 		if (quit_time > 0 && wait.tv_sec > quit_time) {
173*04203a83SThomas Cort 			len = snprintf(buf, sizeof(buf), "\r\n%s: "
174*04203a83SThomas Cort 			    "transfer aborted because stalled for %lu sec.\r\n",
175*04203a83SThomas Cort 			    getprogname(), (unsigned long)wait.tv_sec);
176*04203a83SThomas Cort 			(void)write(fileno(ttyout), buf, len);
177*04203a83SThomas Cort 			alarmtimer(0);
178*04203a83SThomas Cort 			(void)xsignal(SIGALRM, SIG_DFL);
179*04203a83SThomas Cort 			siglongjmp(toplevel, 1);
180*04203a83SThomas Cort 		}
181*04203a83SThomas Cort #endif	/* !STANDALONE_PROGRESS */
182*04203a83SThomas Cort 	}
183*04203a83SThomas Cort 	/*
184*04203a83SThomas Cort 	 * Always set the handler even if we are not the foreground process.
185*04203a83SThomas Cort 	 */
186*04203a83SThomas Cort #ifdef STANDALONE_PROGRESS
187*04203a83SThomas Cort 	if (progress) {
188*04203a83SThomas Cort #else
189*04203a83SThomas Cort 	if (quit_time > 0 || progress) {
190*04203a83SThomas Cort #endif /* !STANDALONE_PROGRESS */
191*04203a83SThomas Cort 		if (flag == -1) {
192*04203a83SThomas Cort 			(void)xsignal_restart(SIGALRM, updateprogressmeter, 1);
193*04203a83SThomas Cort 			alarmtimer(1);		/* set alarm timer for 1 Hz */
194*04203a83SThomas Cort 		} else if (flag == 1) {
195*04203a83SThomas Cort 			alarmtimer(0);
196*04203a83SThomas Cort 			(void)xsignal(SIGALRM, SIG_DFL);
197*04203a83SThomas Cort 		}
198*04203a83SThomas Cort 	}
199*04203a83SThomas Cort #ifndef NO_PROGRESS
200*04203a83SThomas Cort 	if (!progress)
201*04203a83SThomas Cort 		return;
202*04203a83SThomas Cort 	len = 0;
203*04203a83SThomas Cort 
204*04203a83SThomas Cort 	/*
205*04203a83SThomas Cort 	 * print progress bar only if we are foreground process.
206*04203a83SThomas Cort 	 */
207*04203a83SThomas Cort 	if (! foregroundproc())
208*04203a83SThomas Cort 		return;
209*04203a83SThomas Cort 
210*04203a83SThomas Cort 	len += snprintf(buf + len, BUFLEFT, "\r");
211*04203a83SThomas Cort 	if (prefix)
212*04203a83SThomas Cort 	  len += snprintf(buf + len, BUFLEFT, "%s", prefix);
213*04203a83SThomas Cort 	if (filesize > 0) {
214*04203a83SThomas Cort 		ratio = (int)((double)cursize * 100.0 / (double)filesize);
215*04203a83SThomas Cort 		ratio = MAX(ratio, 0);
216*04203a83SThomas Cort 		ratio = MIN(ratio, 100);
217*04203a83SThomas Cort 		len += snprintf(buf + len, BUFLEFT, "%3d%% ", ratio);
218*04203a83SThomas Cort 
219*04203a83SThomas Cort 			/*
220*04203a83SThomas Cort 			 * calculate the length of the `*' bar, ensuring that
221*04203a83SThomas Cort 			 * the number of stars won't exceed the buffer size
222*04203a83SThomas Cort 			 */
223*04203a83SThomas Cort 		barlength = MIN((int)(sizeof(buf) - 1), ttywidth) - BAROVERHEAD;
224*04203a83SThomas Cort 		if (prefix)
225*04203a83SThomas Cort 			barlength -= (int)strlen(prefix);
226*04203a83SThomas Cort 		if (barlength > 0) {
227*04203a83SThomas Cort 			i = barlength * ratio / 100;
228*04203a83SThomas Cort 			len += snprintf(buf + len, BUFLEFT,
229*04203a83SThomas Cort 			    "|%.*s%*s|", i, stars, (int)(barlength - i), "");
230*04203a83SThomas Cort 		}
231*04203a83SThomas Cort 	}
232*04203a83SThomas Cort 
233*04203a83SThomas Cort 	abbrevsize = cursize;
234*04203a83SThomas Cort 	for (i = 0; abbrevsize >= 100000 && i < NSUFFIXES; i++)
235*04203a83SThomas Cort 		abbrevsize >>= 10;
236*04203a83SThomas Cort 	if (i == NSUFFIXES)
237*04203a83SThomas Cort 		i--;
238*04203a83SThomas Cort 	len += snprintf(buf + len, BUFLEFT, " " LLFP("5") " %-3s ",
239*04203a83SThomas Cort 	    (LLT)abbrevsize,
240*04203a83SThomas Cort 	    suffixes[i]);
241*04203a83SThomas Cort 
242*04203a83SThomas Cort 	timersub(&now, &start, &td);
243*04203a83SThomas Cort 	elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
244*04203a83SThomas Cort 
245*04203a83SThomas Cort 	bytespersec = 0;
246*04203a83SThomas Cort 	if (bytes > 0) {
247*04203a83SThomas Cort 		bytespersec = bytes;
248*04203a83SThomas Cort 		if (elapsed > 0.0)
249*04203a83SThomas Cort 			bytespersec /= elapsed;
250*04203a83SThomas Cort 	}
251*04203a83SThomas Cort 	for (i = 1; bytespersec >= 1024000 && i < NSUFFIXES; i++)
252*04203a83SThomas Cort 		bytespersec >>= 10;
253*04203a83SThomas Cort 	len += snprintf(buf + len, BUFLEFT,
254*04203a83SThomas Cort 	    " " LLFP("3") ".%02d %.2sB/s ",
255*04203a83SThomas Cort 	    (LLT)(bytespersec / 1024),
256*04203a83SThomas Cort 	    (int)((bytespersec % 1024) * 100 / 1024),
257*04203a83SThomas Cort 	    suffixes[i]);
258*04203a83SThomas Cort 
259*04203a83SThomas Cort 	if (filesize > 0) {
260*04203a83SThomas Cort 		if (bytes <= 0 || elapsed <= 0.0 || cursize > filesize) {
261*04203a83SThomas Cort 			len += snprintf(buf + len, BUFLEFT, "   --:-- ETA");
262*04203a83SThomas Cort 		} else if (wait.tv_sec >= STALLTIME) {
263*04203a83SThomas Cort 			len += snprintf(buf + len, BUFLEFT, " - stalled -");
264*04203a83SThomas Cort 		} else {
265*04203a83SThomas Cort 			remaining = (int)
266*04203a83SThomas Cort 			    ((filesize - restart_point) / (bytes / elapsed) -
267*04203a83SThomas Cort 			    elapsed);
268*04203a83SThomas Cort 			if (remaining >= 100 * SECSPERHOUR)
269*04203a83SThomas Cort 				len += snprintf(buf + len, BUFLEFT,
270*04203a83SThomas Cort 				    "   --:-- ETA");
271*04203a83SThomas Cort 			else {
272*04203a83SThomas Cort 				i = remaining / SECSPERHOUR;
273*04203a83SThomas Cort 				if (i)
274*04203a83SThomas Cort 					len += snprintf(buf + len, BUFLEFT,
275*04203a83SThomas Cort 					    "%2d:", i);
276*04203a83SThomas Cort 				else
277*04203a83SThomas Cort 					len += snprintf(buf + len, BUFLEFT,
278*04203a83SThomas Cort 					    "   ");
279*04203a83SThomas Cort 				i = remaining % SECSPERHOUR;
280*04203a83SThomas Cort 				len += snprintf(buf + len, BUFLEFT,
281*04203a83SThomas Cort 				    "%02d:%02d ETA", i / 60, i % 60);
282*04203a83SThomas Cort 			}
283*04203a83SThomas Cort 		}
284*04203a83SThomas Cort 	}
285*04203a83SThomas Cort 	if (flag == 1)
286*04203a83SThomas Cort 		len += snprintf(buf + len, BUFLEFT, "\n");
287*04203a83SThomas Cort 	(void)write(fileno(ttyout), buf, len);
288*04203a83SThomas Cort 
289*04203a83SThomas Cort #endif	/* !NO_PROGRESS */
290*04203a83SThomas Cort }
291*04203a83SThomas Cort 
292*04203a83SThomas Cort #ifndef STANDALONE_PROGRESS
293*04203a83SThomas Cort /*
294*04203a83SThomas Cort  * Display transfer statistics.
295*04203a83SThomas Cort  * Requires start to be initialised by progressmeter(-1),
296*04203a83SThomas Cort  * direction to be defined by xfer routines, and filesize and bytes
297*04203a83SThomas Cort  * to be updated by xfer routines
298*04203a83SThomas Cort  * If siginfo is nonzero, an ETA is displayed, and the output goes to stderr
299*04203a83SThomas Cort  * instead of ttyout.
300*04203a83SThomas Cort  */
301*04203a83SThomas Cort void
302*04203a83SThomas Cort ptransfer(int siginfo)
303*04203a83SThomas Cort {
304*04203a83SThomas Cort 	struct timeval now, td, wait;
305*04203a83SThomas Cort 	double elapsed;
306*04203a83SThomas Cort 	off_t bytespersec;
307*04203a83SThomas Cort 	int remaining, hh, i;
308*04203a83SThomas Cort 	size_t len;
309*04203a83SThomas Cort 
310*04203a83SThomas Cort 	char buf[256];		/* Work variable for transfer status. */
311*04203a83SThomas Cort 
312*04203a83SThomas Cort 	if (!verbose && !progress && !siginfo)
313*04203a83SThomas Cort 		return;
314*04203a83SThomas Cort 
315*04203a83SThomas Cort 	(void)gettimeofday(&now, NULL);
316*04203a83SThomas Cort 	timersub(&now, &start, &td);
317*04203a83SThomas Cort 	elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
318*04203a83SThomas Cort 	bytespersec = 0;
319*04203a83SThomas Cort 	if (bytes > 0) {
320*04203a83SThomas Cort 		bytespersec = bytes;
321*04203a83SThomas Cort 		if (elapsed > 0.0)
322*04203a83SThomas Cort 			bytespersec /= elapsed;
323*04203a83SThomas Cort 	}
324*04203a83SThomas Cort 	len = 0;
325*04203a83SThomas Cort 	len += snprintf(buf + len, BUFLEFT, LLF " byte%s %s in ",
326*04203a83SThomas Cort 	    (LLT)bytes, bytes == 1 ? "" : "s", direction);
327*04203a83SThomas Cort 	remaining = (int)elapsed;
328*04203a83SThomas Cort 	if (remaining > SECSPERDAY) {
329*04203a83SThomas Cort 		int days;
330*04203a83SThomas Cort 
331*04203a83SThomas Cort 		days = remaining / SECSPERDAY;
332*04203a83SThomas Cort 		remaining %= SECSPERDAY;
333*04203a83SThomas Cort 		len += snprintf(buf + len, BUFLEFT,
334*04203a83SThomas Cort 		    "%d day%s ", days, days == 1 ? "" : "s");
335*04203a83SThomas Cort 	}
336*04203a83SThomas Cort 	hh = remaining / SECSPERHOUR;
337*04203a83SThomas Cort 	remaining %= SECSPERHOUR;
338*04203a83SThomas Cort 	if (hh)
339*04203a83SThomas Cort 		len += snprintf(buf + len, BUFLEFT, "%2d:", hh);
340*04203a83SThomas Cort 	len += snprintf(buf + len, BUFLEFT,
341*04203a83SThomas Cort 	    "%02d:%02d ", remaining / 60, remaining % 60);
342*04203a83SThomas Cort 
343*04203a83SThomas Cort 	for (i = 1; bytespersec >= 1024000 && i < NSUFFIXES; i++)
344*04203a83SThomas Cort 		bytespersec >>= 10;
345*04203a83SThomas Cort 	if (i == NSUFFIXES)
346*04203a83SThomas Cort 		i--;
347*04203a83SThomas Cort 	len += snprintf(buf + len, BUFLEFT, "(" LLF ".%02d %.2sB/s)",
348*04203a83SThomas Cort 	    (LLT)(bytespersec / 1024),
349*04203a83SThomas Cort 	    (int)((bytespersec % 1024) * 100 / 1024),
350*04203a83SThomas Cort 	    suffixes[i]);
351*04203a83SThomas Cort 
352*04203a83SThomas Cort 	if (siginfo && bytes > 0 && elapsed > 0.0 && filesize >= 0
353*04203a83SThomas Cort 	    && bytes + restart_point <= filesize) {
354*04203a83SThomas Cort 		remaining = (int)((filesize - restart_point) /
355*04203a83SThomas Cort 				  (bytes / elapsed) - elapsed);
356*04203a83SThomas Cort 		hh = remaining / SECSPERHOUR;
357*04203a83SThomas Cort 		remaining %= SECSPERHOUR;
358*04203a83SThomas Cort 		len += snprintf(buf + len, BUFLEFT, "  ETA: ");
359*04203a83SThomas Cort 		if (hh)
360*04203a83SThomas Cort 			len += snprintf(buf + len, BUFLEFT, "%2d:", hh);
361*04203a83SThomas Cort 		len += snprintf(buf + len, BUFLEFT, "%02d:%02d",
362*04203a83SThomas Cort 		    remaining / 60, remaining % 60);
363*04203a83SThomas Cort 		timersub(&now, &lastupdate, &wait);
364*04203a83SThomas Cort 		if (wait.tv_sec >= STALLTIME)
365*04203a83SThomas Cort 			len += snprintf(buf + len, BUFLEFT, "  (stalled)");
366*04203a83SThomas Cort 	}
367*04203a83SThomas Cort 	len += snprintf(buf + len, BUFLEFT, "\n");
368*04203a83SThomas Cort 	(void)write(siginfo ? STDERR_FILENO : fileno(ttyout), buf, len);
369*04203a83SThomas Cort }
370*04203a83SThomas Cort 
371*04203a83SThomas Cort /*
372*04203a83SThomas Cort  * SIG{INFO,QUIT} handler to print transfer stats if a transfer is in progress
373*04203a83SThomas Cort  */
374*04203a83SThomas Cort void
375*04203a83SThomas Cort psummary(int notused)
376*04203a83SThomas Cort {
377*04203a83SThomas Cort 	int oerrno = errno;
378*04203a83SThomas Cort 
379*04203a83SThomas Cort 	if (bytes > 0) {
380*04203a83SThomas Cort 		if (fromatty)
381*04203a83SThomas Cort 			write(fileno(ttyout), "\n", 1);
382*04203a83SThomas Cort 		ptransfer(1);
383*04203a83SThomas Cort 	}
384*04203a83SThomas Cort 	errno = oerrno;
385*04203a83SThomas Cort }
386*04203a83SThomas Cort #endif	/* !STANDALONE_PROGRESS */
387*04203a83SThomas Cort 
388*04203a83SThomas Cort 
389*04203a83SThomas Cort /*
390*04203a83SThomas Cort  * Set the SIGALRM interval timer for wait seconds, 0 to disable.
391*04203a83SThomas Cort  */
392*04203a83SThomas Cort void
393*04203a83SThomas Cort alarmtimer(int wait)
394*04203a83SThomas Cort {
395*04203a83SThomas Cort 	struct itimerval itv;
396*04203a83SThomas Cort 
397*04203a83SThomas Cort 	itv.it_value.tv_sec = wait;
398*04203a83SThomas Cort 	itv.it_value.tv_usec = 0;
399*04203a83SThomas Cort 	itv.it_interval = itv.it_value;
400*04203a83SThomas Cort 	setitimer(ITIMER_REAL, &itv, NULL);
401*04203a83SThomas Cort }
402*04203a83SThomas Cort 
403*04203a83SThomas Cort 
404*04203a83SThomas Cort /*
405*04203a83SThomas Cort  * Install a POSIX signal handler, allowing the invoker to set whether
406*04203a83SThomas Cort  * the signal should be restartable or not
407*04203a83SThomas Cort  */
408*04203a83SThomas Cort sigfunc
409*04203a83SThomas Cort xsignal_restart(int sig, sigfunc func, int restartable)
410*04203a83SThomas Cort {
411*04203a83SThomas Cort 	struct sigaction act, oact;
412*04203a83SThomas Cort 	act.sa_handler = func;
413*04203a83SThomas Cort 
414*04203a83SThomas Cort 	sigemptyset(&act.sa_mask);
415*04203a83SThomas Cort #if defined(SA_RESTART)			/* 4.4BSD, Posix(?), SVR4 */
416*04203a83SThomas Cort 	act.sa_flags = restartable ? SA_RESTART : 0;
417*04203a83SThomas Cort #elif defined(SA_INTERRUPT)		/* SunOS 4.x */
418*04203a83SThomas Cort 	act.sa_flags = restartable ? 0 : SA_INTERRUPT;
419*04203a83SThomas Cort #else
420*04203a83SThomas Cort #error "system must have SA_RESTART or SA_INTERRUPT"
421*04203a83SThomas Cort #endif
422*04203a83SThomas Cort 	if (sigaction(sig, &act, &oact) < 0)
423*04203a83SThomas Cort 		return (SIG_ERR);
424*04203a83SThomas Cort 	return (oact.sa_handler);
425*04203a83SThomas Cort }
426*04203a83SThomas Cort 
427*04203a83SThomas Cort /*
428*04203a83SThomas Cort  * Install a signal handler with the `restartable' flag set dependent upon
429*04203a83SThomas Cort  * which signal is being set. (This is a wrapper to xsignal_restart())
430*04203a83SThomas Cort  */
431*04203a83SThomas Cort sigfunc
432*04203a83SThomas Cort xsignal(int sig, sigfunc func)
433*04203a83SThomas Cort {
434*04203a83SThomas Cort 	int restartable;
435*04203a83SThomas Cort 
436*04203a83SThomas Cort 	/*
437*04203a83SThomas Cort 	 * Some signals print output or change the state of the process.
438*04203a83SThomas Cort 	 * There should be restartable, so that reads and writes are
439*04203a83SThomas Cort 	 * not affected.  Some signals should cause program flow to change;
440*04203a83SThomas Cort 	 * these signals should not be restartable, so that the system call
441*04203a83SThomas Cort 	 * will return with EINTR, and the program will go do something
442*04203a83SThomas Cort 	 * different.  If the signal handler calls longjmp() or siglongjmp(),
443*04203a83SThomas Cort 	 * it doesn't matter if it's restartable.
444*04203a83SThomas Cort 	 */
445*04203a83SThomas Cort 
446*04203a83SThomas Cort 	switch(sig) {
447*04203a83SThomas Cort #ifdef SIGINFO
448*04203a83SThomas Cort 	case SIGINFO:
449*04203a83SThomas Cort #endif
450*04203a83SThomas Cort 	case SIGQUIT:
451*04203a83SThomas Cort 	case SIGUSR1:
452*04203a83SThomas Cort 	case SIGUSR2:
453*04203a83SThomas Cort 	case SIGWINCH:
454*04203a83SThomas Cort 		restartable = 1;
455*04203a83SThomas Cort 		break;
456*04203a83SThomas Cort 
457*04203a83SThomas Cort 	case SIGALRM:
458*04203a83SThomas Cort 	case SIGINT:
459*04203a83SThomas Cort 	case SIGPIPE:
460*04203a83SThomas Cort 		restartable = 0;
461*04203a83SThomas Cort 		break;
462*04203a83SThomas Cort 
463*04203a83SThomas Cort 	default:
464*04203a83SThomas Cort 		/*
465*04203a83SThomas Cort 		 * This is unpleasant, but I don't know what would be better.
466*04203a83SThomas Cort 		 * Right now, this "can't happen"
467*04203a83SThomas Cort 		 */
468*04203a83SThomas Cort 		errx(1, "xsignal_restart: called with signal %d", sig);
469*04203a83SThomas Cort 	}
470*04203a83SThomas Cort 
471*04203a83SThomas Cort 	return(xsignal_restart(sig, func, restartable));
472*04203a83SThomas Cort }
473