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