1*3a184c67SAntonio Huete Jimenez /* $NetBSD: progressbar.c,v 1.18 2021/04/25 08:23:22 lukem Exp $ */
2*3a184c67SAntonio Huete Jimenez /* from NetBSD: progressbar.c,v 1.24 2021/01/06 04:43:14 lukem Exp */
36cdfca03SJohn Marino
46cdfca03SJohn Marino /*-
5*3a184c67SAntonio Huete Jimenez * Copyright (c) 1997-2021 The NetBSD Foundation, Inc.
66cdfca03SJohn Marino * All rights reserved.
76cdfca03SJohn Marino *
86cdfca03SJohn Marino * This code is derived from software contributed to The NetBSD Foundation
96cdfca03SJohn Marino * by Luke Mewburn.
106cdfca03SJohn Marino *
116cdfca03SJohn Marino * Redistribution and use in source and binary forms, with or without
126cdfca03SJohn Marino * modification, are permitted provided that the following conditions
136cdfca03SJohn Marino * are met:
146cdfca03SJohn Marino * 1. Redistributions of source code must retain the above copyright
156cdfca03SJohn Marino * notice, this list of conditions and the following disclaimer.
166cdfca03SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
176cdfca03SJohn Marino * notice, this list of conditions and the following disclaimer in the
186cdfca03SJohn Marino * documentation and/or other materials provided with the distribution.
196cdfca03SJohn Marino *
206cdfca03SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
216cdfca03SJohn Marino * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
226cdfca03SJohn Marino * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
236cdfca03SJohn Marino * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
246cdfca03SJohn Marino * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
256cdfca03SJohn Marino * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
266cdfca03SJohn Marino * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
276cdfca03SJohn Marino * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
286cdfca03SJohn Marino * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
296cdfca03SJohn Marino * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
306cdfca03SJohn Marino * POSSIBILITY OF SUCH DAMAGE.
316cdfca03SJohn Marino */
326cdfca03SJohn Marino
336cdfca03SJohn Marino #include "tnftp.h"
346cdfca03SJohn Marino
356cdfca03SJohn Marino #if 0 /* tnftp */
366cdfca03SJohn Marino
376cdfca03SJohn Marino #include <sys/cdefs.h>
386cdfca03SJohn Marino #ifndef lint
39*3a184c67SAntonio Huete Jimenez __RCSID(" NetBSD: progressbar.c,v 1.24 2021/01/06 04:43:14 lukem Exp ");
406cdfca03SJohn Marino #endif /* not lint */
416cdfca03SJohn Marino
426cdfca03SJohn Marino /*
436cdfca03SJohn Marino * FTP User Program -- Misc support routines
446cdfca03SJohn Marino */
456cdfca03SJohn Marino #include <sys/param.h>
466cdfca03SJohn Marino #include <sys/types.h>
476cdfca03SJohn Marino #include <sys/time.h>
486cdfca03SJohn Marino
496cdfca03SJohn Marino #include <err.h>
506cdfca03SJohn Marino #include <errno.h>
516cdfca03SJohn Marino #include <signal.h>
526cdfca03SJohn Marino #include <stdio.h>
536cdfca03SJohn Marino #include <stdlib.h>
546cdfca03SJohn Marino #include <string.h>
556cdfca03SJohn Marino #include <time.h>
566cdfca03SJohn Marino #include <tzfile.h>
576cdfca03SJohn Marino #include <unistd.h>
586cdfca03SJohn Marino
596cdfca03SJohn Marino #endif /* tnftp */
606cdfca03SJohn Marino
616cdfca03SJohn Marino #include "progressbar.h"
626cdfca03SJohn Marino
636cdfca03SJohn Marino #if !defined(NO_PROGRESS)
646cdfca03SJohn Marino /*
656cdfca03SJohn Marino * return non-zero if we're the current foreground process
666cdfca03SJohn Marino */
676cdfca03SJohn Marino int
foregroundproc(void)686cdfca03SJohn Marino foregroundproc(void)
696cdfca03SJohn Marino {
706cdfca03SJohn Marino static pid_t pgrp = -1;
716cdfca03SJohn Marino
726cdfca03SJohn Marino if (pgrp == -1)
736cdfca03SJohn Marino #if GETPGRP_VOID
746cdfca03SJohn Marino pgrp = getpgrp();
756cdfca03SJohn Marino #else /* ! GETPGRP_VOID */
766cdfca03SJohn Marino pgrp = getpgrp(0);
776cdfca03SJohn Marino #endif /* ! GETPGRP_VOID */
786cdfca03SJohn Marino
796cdfca03SJohn Marino return (tcgetpgrp(fileno(ttyout)) == pgrp);
806cdfca03SJohn Marino }
816cdfca03SJohn Marino #endif /* !defined(NO_PROGRESS) */
826cdfca03SJohn Marino
836cdfca03SJohn Marino
846cdfca03SJohn Marino static void updateprogressmeter(int);
856cdfca03SJohn Marino
866cdfca03SJohn Marino /*
876cdfca03SJohn Marino * SIGALRM handler to update the progress meter
886cdfca03SJohn Marino */
896cdfca03SJohn Marino static void
updateprogressmeter(int dummy)906cdfca03SJohn Marino updateprogressmeter(int dummy)
916cdfca03SJohn Marino {
926cdfca03SJohn Marino int oerrno = errno;
936cdfca03SJohn Marino
946cdfca03SJohn Marino progressmeter(0);
956cdfca03SJohn Marino errno = oerrno;
966cdfca03SJohn Marino }
976cdfca03SJohn Marino
986cdfca03SJohn Marino /*
996cdfca03SJohn Marino * List of order of magnitude suffixes, per IEC 60027-2.
1006cdfca03SJohn Marino */
101*3a184c67SAntonio Huete Jimenez #if !defined(NO_PROGRESS) || !defined(STANDALONE_PROGRESS)
1026cdfca03SJohn Marino static const char * const suffixes[] = {
1036cdfca03SJohn Marino "", /* 2^0 (byte) */
1046cdfca03SJohn Marino "KiB", /* 2^10 Kibibyte */
1056cdfca03SJohn Marino "MiB", /* 2^20 Mebibyte */
1066cdfca03SJohn Marino "GiB", /* 2^30 Gibibyte */
1076cdfca03SJohn Marino "TiB", /* 2^40 Tebibyte */
1086cdfca03SJohn Marino "PiB", /* 2^50 Pebibyte */
1096cdfca03SJohn Marino "EiB", /* 2^60 Exbibyte */
1106cdfca03SJohn Marino #if 0
1116cdfca03SJohn Marino /* The following are not necessary for signed 64-bit off_t */
1126cdfca03SJohn Marino "ZiB", /* 2^70 Zebibyte */
1136cdfca03SJohn Marino "YiB", /* 2^80 Yobibyte */
1146cdfca03SJohn Marino #endif
1156cdfca03SJohn Marino };
1166cdfca03SJohn Marino #define NSUFFIXES (int)(sizeof(suffixes) / sizeof(suffixes[0]))
117*3a184c67SAntonio Huete Jimenez #endif
1186cdfca03SJohn Marino
1196cdfca03SJohn Marino /*
1206cdfca03SJohn Marino * Display a transfer progress bar if progress is non-zero.
1216cdfca03SJohn Marino * SIGALRM is hijacked for use by this function.
1226cdfca03SJohn Marino * - Before the transfer, set filesize to size of file (or -1 if unknown),
1236cdfca03SJohn Marino * and call with flag = -1. This starts the once per second timer,
1246cdfca03SJohn Marino * and a call to updateprogressmeter() upon SIGALRM.
1256cdfca03SJohn Marino * - During the transfer, updateprogressmeter will call progressmeter
1266cdfca03SJohn Marino * with flag = 0
1276cdfca03SJohn Marino * - After the transfer, call with flag = 1
1286cdfca03SJohn Marino */
1296cdfca03SJohn Marino static struct timeval start;
1306cdfca03SJohn Marino static struct timeval lastupdate;
1316cdfca03SJohn Marino
1326cdfca03SJohn Marino #define BUFLEFT (sizeof(buf) - len)
1336cdfca03SJohn Marino
1346cdfca03SJohn Marino void
progressmeter(int flag)1356cdfca03SJohn Marino progressmeter(int flag)
1366cdfca03SJohn Marino {
1376cdfca03SJohn Marino static off_t lastsize;
1386cdfca03SJohn Marino off_t cursize;
1396cdfca03SJohn Marino struct timeval now, wait;
1406cdfca03SJohn Marino #ifndef NO_PROGRESS
1416cdfca03SJohn Marino struct timeval td;
1426cdfca03SJohn Marino off_t abbrevsize, bytespersec;
1436cdfca03SJohn Marino double elapsed;
1446cdfca03SJohn Marino int ratio, i, remaining, barlength;
1456cdfca03SJohn Marino
1466cdfca03SJohn Marino /*
1476cdfca03SJohn Marino * Work variables for progress bar.
1486cdfca03SJohn Marino *
1496cdfca03SJohn Marino * XXX: if the format of the progress bar changes
1506cdfca03SJohn Marino * (especially the number of characters in the
1516cdfca03SJohn Marino * `static' portion of it), be sure to update
1526cdfca03SJohn Marino * these appropriately.
1536cdfca03SJohn Marino */
1546cdfca03SJohn Marino #endif
155*3a184c67SAntonio Huete Jimenez #if !defined(NO_PROGRESS) || !defined(STANDALONE_PROGRESS)
1566cdfca03SJohn Marino size_t len;
1576cdfca03SJohn Marino char buf[256]; /* workspace for progress bar */
158*3a184c67SAntonio Huete Jimenez #endif
1596cdfca03SJohn Marino #ifndef NO_PROGRESS
1606cdfca03SJohn Marino #define BAROVERHEAD 45 /* non `*' portion of progress bar */
1616cdfca03SJohn Marino /*
1626cdfca03SJohn Marino * stars should contain at least
1636cdfca03SJohn Marino * sizeof(buf) - BAROVERHEAD entries
1646cdfca03SJohn Marino */
1656cdfca03SJohn Marino static const char stars[] =
1666cdfca03SJohn Marino "*****************************************************************************"
1676cdfca03SJohn Marino "*****************************************************************************"
1686cdfca03SJohn Marino "*****************************************************************************";
1696cdfca03SJohn Marino
1706cdfca03SJohn Marino #endif
1716cdfca03SJohn Marino
1726cdfca03SJohn Marino if (flag == -1) {
1736cdfca03SJohn Marino (void)gettimeofday(&start, NULL);
1746cdfca03SJohn Marino lastupdate = start;
1756cdfca03SJohn Marino lastsize = restart_point;
1766cdfca03SJohn Marino }
1776cdfca03SJohn Marino
1786cdfca03SJohn Marino (void)gettimeofday(&now, NULL);
1796cdfca03SJohn Marino cursize = bytes + restart_point;
1806cdfca03SJohn Marino timersub(&now, &lastupdate, &wait);
1816cdfca03SJohn Marino if (cursize > lastsize) {
1826cdfca03SJohn Marino lastupdate = now;
1836cdfca03SJohn Marino lastsize = cursize;
1846cdfca03SJohn Marino wait.tv_sec = 0;
1856cdfca03SJohn Marino } else {
1866cdfca03SJohn Marino #ifndef STANDALONE_PROGRESS
1876cdfca03SJohn Marino if (quit_time > 0 && wait.tv_sec > quit_time) {
1886cdfca03SJohn Marino len = snprintf(buf, sizeof(buf), "\r\n%s: "
1896cdfca03SJohn Marino "transfer aborted because stalled for %lu sec.\r\n",
1906cdfca03SJohn Marino getprogname(), (unsigned long)wait.tv_sec);
1916cdfca03SJohn Marino (void)write(fileno(ttyout), buf, len);
1926cdfca03SJohn Marino alarmtimer(0);
1936cdfca03SJohn Marino (void)xsignal(SIGALRM, SIG_DFL);
1946cdfca03SJohn Marino siglongjmp(toplevel, 1);
1956cdfca03SJohn Marino }
1966cdfca03SJohn Marino #endif /* !STANDALONE_PROGRESS */
1976cdfca03SJohn Marino }
1986cdfca03SJohn Marino /*
1996cdfca03SJohn Marino * Always set the handler even if we are not the foreground process.
2006cdfca03SJohn Marino */
2016cdfca03SJohn Marino #ifdef STANDALONE_PROGRESS
2026cdfca03SJohn Marino if (progress) {
2036cdfca03SJohn Marino #else
2046cdfca03SJohn Marino if (quit_time > 0 || progress) {
2056cdfca03SJohn Marino #endif /* !STANDALONE_PROGRESS */
2066cdfca03SJohn Marino if (flag == -1) {
207*3a184c67SAntonio Huete Jimenez (void)xsignal(SIGALRM, updateprogressmeter);
2086cdfca03SJohn Marino alarmtimer(1); /* set alarm timer for 1 Hz */
2096cdfca03SJohn Marino } else if (flag == 1) {
2106cdfca03SJohn Marino alarmtimer(0);
2116cdfca03SJohn Marino (void)xsignal(SIGALRM, SIG_DFL);
2126cdfca03SJohn Marino }
2136cdfca03SJohn Marino }
2146cdfca03SJohn Marino #ifndef NO_PROGRESS
2156cdfca03SJohn Marino if (!progress)
2166cdfca03SJohn Marino return;
2176cdfca03SJohn Marino len = 0;
2186cdfca03SJohn Marino
2196cdfca03SJohn Marino /*
2206cdfca03SJohn Marino * print progress bar only if we are foreground process.
2216cdfca03SJohn Marino */
2226cdfca03SJohn Marino if (! foregroundproc())
2236cdfca03SJohn Marino return;
2246cdfca03SJohn Marino
2256cdfca03SJohn Marino len += snprintf(buf + len, BUFLEFT, "\r");
2266cdfca03SJohn Marino if (prefix)
2276cdfca03SJohn Marino len += snprintf(buf + len, BUFLEFT, "%s", prefix);
2286cdfca03SJohn Marino if (filesize > 0) {
2296cdfca03SJohn Marino ratio = (int)((double)cursize * 100.0 / (double)filesize);
2306cdfca03SJohn Marino ratio = MAX(ratio, 0);
2316cdfca03SJohn Marino ratio = MIN(ratio, 100);
2326cdfca03SJohn Marino len += snprintf(buf + len, BUFLEFT, "%3d%% ", ratio);
2336cdfca03SJohn Marino
2346cdfca03SJohn Marino /*
2356cdfca03SJohn Marino * calculate the length of the `*' bar, ensuring that
2366cdfca03SJohn Marino * the number of stars won't exceed the buffer size
2376cdfca03SJohn Marino */
2386cdfca03SJohn Marino barlength = MIN((int)(sizeof(buf) - 1), ttywidth) - BAROVERHEAD;
2396cdfca03SJohn Marino if (prefix)
2406cdfca03SJohn Marino barlength -= (int)strlen(prefix);
2416cdfca03SJohn Marino if (barlength > 0) {
2426cdfca03SJohn Marino i = barlength * ratio / 100;
2436cdfca03SJohn Marino len += snprintf(buf + len, BUFLEFT,
2446cdfca03SJohn Marino "|%.*s%*s|", i, stars, (int)(barlength - i), "");
2456cdfca03SJohn Marino }
2466cdfca03SJohn Marino }
2476cdfca03SJohn Marino
2486cdfca03SJohn Marino abbrevsize = cursize;
2496cdfca03SJohn Marino for (i = 0; abbrevsize >= 100000 && i < NSUFFIXES; i++)
2506cdfca03SJohn Marino abbrevsize >>= 10;
2516cdfca03SJohn Marino if (i == NSUFFIXES)
2526cdfca03SJohn Marino i--;
2536cdfca03SJohn Marino len += snprintf(buf + len, BUFLEFT, " " LLFP("5") " %-3s ",
2546cdfca03SJohn Marino (LLT)abbrevsize,
2556cdfca03SJohn Marino suffixes[i]);
2566cdfca03SJohn Marino
2576cdfca03SJohn Marino timersub(&now, &start, &td);
2586cdfca03SJohn Marino elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
2596cdfca03SJohn Marino
2606cdfca03SJohn Marino bytespersec = 0;
2616cdfca03SJohn Marino if (bytes > 0) {
2626cdfca03SJohn Marino bytespersec = bytes;
2636cdfca03SJohn Marino if (elapsed > 0.0)
2646cdfca03SJohn Marino bytespersec /= elapsed;
2656cdfca03SJohn Marino }
2666cdfca03SJohn Marino for (i = 1; bytespersec >= 1024000 && i < NSUFFIXES; i++)
2676cdfca03SJohn Marino bytespersec >>= 10;
2686cdfca03SJohn Marino len += snprintf(buf + len, BUFLEFT,
2696cdfca03SJohn Marino " " LLFP("3") ".%02d %.2sB/s ",
2706cdfca03SJohn Marino (LLT)(bytespersec / 1024),
2716cdfca03SJohn Marino (int)((bytespersec % 1024) * 100 / 1024),
2726cdfca03SJohn Marino suffixes[i]);
2736cdfca03SJohn Marino
2746cdfca03SJohn Marino if (filesize > 0) {
2756cdfca03SJohn Marino if (bytes <= 0 || elapsed <= 0.0 || cursize > filesize) {
2766cdfca03SJohn Marino len += snprintf(buf + len, BUFLEFT, " --:-- ETA");
2776cdfca03SJohn Marino } else if (wait.tv_sec >= STALLTIME) {
2786cdfca03SJohn Marino len += snprintf(buf + len, BUFLEFT, " - stalled -");
2796cdfca03SJohn Marino } else {
2806cdfca03SJohn Marino remaining = (int)
2816cdfca03SJohn Marino ((filesize - restart_point) / (bytes / elapsed) -
2826cdfca03SJohn Marino elapsed);
2836cdfca03SJohn Marino if (remaining >= 100 * SECSPERHOUR)
2846cdfca03SJohn Marino len += snprintf(buf + len, BUFLEFT,
2856cdfca03SJohn Marino " --:-- ETA");
2866cdfca03SJohn Marino else {
2876cdfca03SJohn Marino i = remaining / SECSPERHOUR;
2886cdfca03SJohn Marino if (i)
2896cdfca03SJohn Marino len += snprintf(buf + len, BUFLEFT,
2906cdfca03SJohn Marino "%2d:", i);
2916cdfca03SJohn Marino else
2926cdfca03SJohn Marino len += snprintf(buf + len, BUFLEFT,
2936cdfca03SJohn Marino " ");
2946cdfca03SJohn Marino i = remaining % SECSPERHOUR;
2956cdfca03SJohn Marino len += snprintf(buf + len, BUFLEFT,
2966cdfca03SJohn Marino "%02d:%02d ETA", i / 60, i % 60);
2976cdfca03SJohn Marino }
2986cdfca03SJohn Marino }
2996cdfca03SJohn Marino }
3006cdfca03SJohn Marino if (flag == 1)
3016cdfca03SJohn Marino len += snprintf(buf + len, BUFLEFT, "\n");
3026cdfca03SJohn Marino (void)write(fileno(ttyout), buf, len);
3036cdfca03SJohn Marino
3046cdfca03SJohn Marino #endif /* !NO_PROGRESS */
3056cdfca03SJohn Marino }
3066cdfca03SJohn Marino
3076cdfca03SJohn Marino #ifndef STANDALONE_PROGRESS
3086cdfca03SJohn Marino /*
3096cdfca03SJohn Marino * Display transfer statistics.
3106cdfca03SJohn Marino * Requires start to be initialised by progressmeter(-1),
3116cdfca03SJohn Marino * direction to be defined by xfer routines, and filesize and bytes
3126cdfca03SJohn Marino * to be updated by xfer routines
3136cdfca03SJohn Marino * If siginfo is nonzero, an ETA is displayed, and the output goes to stderr
3146cdfca03SJohn Marino * instead of ttyout.
3156cdfca03SJohn Marino */
3166cdfca03SJohn Marino void
3176cdfca03SJohn Marino ptransfer(int siginfo)
3186cdfca03SJohn Marino {
3196cdfca03SJohn Marino struct timeval now, td, wait;
3206cdfca03SJohn Marino double elapsed;
3216cdfca03SJohn Marino off_t bytespersec;
3226cdfca03SJohn Marino int remaining, hh, i;
3236cdfca03SJohn Marino size_t len;
3246cdfca03SJohn Marino
3256cdfca03SJohn Marino char buf[256]; /* Work variable for transfer status. */
3266cdfca03SJohn Marino
3276cdfca03SJohn Marino if (!verbose && !progress && !siginfo)
3286cdfca03SJohn Marino return;
3296cdfca03SJohn Marino
3306cdfca03SJohn Marino (void)gettimeofday(&now, NULL);
3316cdfca03SJohn Marino timersub(&now, &start, &td);
3326cdfca03SJohn Marino elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
3336cdfca03SJohn Marino bytespersec = 0;
3346cdfca03SJohn Marino if (bytes > 0) {
3356cdfca03SJohn Marino bytespersec = bytes;
3366cdfca03SJohn Marino if (elapsed > 0.0)
3376cdfca03SJohn Marino bytespersec /= elapsed;
3386cdfca03SJohn Marino }
3396cdfca03SJohn Marino len = 0;
3406cdfca03SJohn Marino len += snprintf(buf + len, BUFLEFT, LLF " byte%s %s in ",
3416cdfca03SJohn Marino (LLT)bytes, bytes == 1 ? "" : "s", direction);
3426cdfca03SJohn Marino remaining = (int)elapsed;
3436cdfca03SJohn Marino if (remaining > SECSPERDAY) {
3446cdfca03SJohn Marino int days;
3456cdfca03SJohn Marino
3466cdfca03SJohn Marino days = remaining / SECSPERDAY;
3476cdfca03SJohn Marino remaining %= SECSPERDAY;
3486cdfca03SJohn Marino len += snprintf(buf + len, BUFLEFT,
3496cdfca03SJohn Marino "%d day%s ", days, days == 1 ? "" : "s");
3506cdfca03SJohn Marino }
3516cdfca03SJohn Marino hh = remaining / SECSPERHOUR;
3526cdfca03SJohn Marino remaining %= SECSPERHOUR;
3536cdfca03SJohn Marino if (hh)
3546cdfca03SJohn Marino len += snprintf(buf + len, BUFLEFT, "%2d:", hh);
3556cdfca03SJohn Marino len += snprintf(buf + len, BUFLEFT,
3566cdfca03SJohn Marino "%02d:%02d ", remaining / 60, remaining % 60);
3576cdfca03SJohn Marino
3586cdfca03SJohn Marino for (i = 1; bytespersec >= 1024000 && i < NSUFFIXES; i++)
3596cdfca03SJohn Marino bytespersec >>= 10;
3606cdfca03SJohn Marino if (i == NSUFFIXES)
3616cdfca03SJohn Marino i--;
3626cdfca03SJohn Marino len += snprintf(buf + len, BUFLEFT, "(" LLF ".%02d %.2sB/s)",
3636cdfca03SJohn Marino (LLT)(bytespersec / 1024),
3646cdfca03SJohn Marino (int)((bytespersec % 1024) * 100 / 1024),
3656cdfca03SJohn Marino suffixes[i]);
3666cdfca03SJohn Marino
3676cdfca03SJohn Marino if (siginfo && bytes > 0 && elapsed > 0.0 && filesize >= 0
3686cdfca03SJohn Marino && bytes + restart_point <= filesize) {
3696cdfca03SJohn Marino remaining = (int)((filesize - restart_point) /
3706cdfca03SJohn Marino (bytes / elapsed) - elapsed);
3716cdfca03SJohn Marino hh = remaining / SECSPERHOUR;
3726cdfca03SJohn Marino remaining %= SECSPERHOUR;
3736cdfca03SJohn Marino len += snprintf(buf + len, BUFLEFT, " ETA: ");
3746cdfca03SJohn Marino if (hh)
3756cdfca03SJohn Marino len += snprintf(buf + len, BUFLEFT, "%2d:", hh);
3766cdfca03SJohn Marino len += snprintf(buf + len, BUFLEFT, "%02d:%02d",
3776cdfca03SJohn Marino remaining / 60, remaining % 60);
3786cdfca03SJohn Marino timersub(&now, &lastupdate, &wait);
3796cdfca03SJohn Marino if (wait.tv_sec >= STALLTIME)
3806cdfca03SJohn Marino len += snprintf(buf + len, BUFLEFT, " (stalled)");
3816cdfca03SJohn Marino }
3826cdfca03SJohn Marino len += snprintf(buf + len, BUFLEFT, "\n");
3836cdfca03SJohn Marino (void)write(siginfo ? STDERR_FILENO : fileno(ttyout), buf, len);
3846cdfca03SJohn Marino }
3856cdfca03SJohn Marino
3866cdfca03SJohn Marino /*
3876cdfca03SJohn Marino * SIG{INFO,QUIT} handler to print transfer stats if a transfer is in progress
3886cdfca03SJohn Marino */
3896cdfca03SJohn Marino void
3906cdfca03SJohn Marino psummary(int notused)
3916cdfca03SJohn Marino {
3926cdfca03SJohn Marino int oerrno = errno;
3936cdfca03SJohn Marino
3946cdfca03SJohn Marino if (bytes > 0) {
3956cdfca03SJohn Marino if (fromatty)
3966cdfca03SJohn Marino write(fileno(ttyout), "\n", 1);
3976cdfca03SJohn Marino ptransfer(1);
3986cdfca03SJohn Marino }
3996cdfca03SJohn Marino errno = oerrno;
4006cdfca03SJohn Marino }
4016cdfca03SJohn Marino #endif /* !STANDALONE_PROGRESS */
4026cdfca03SJohn Marino
4036cdfca03SJohn Marino
4046cdfca03SJohn Marino /*
4056cdfca03SJohn Marino * Set the SIGALRM interval timer for wait seconds, 0 to disable.
4066cdfca03SJohn Marino */
4076cdfca03SJohn Marino void
4086cdfca03SJohn Marino alarmtimer(int wait)
4096cdfca03SJohn Marino {
4106cdfca03SJohn Marino struct itimerval itv;
4116cdfca03SJohn Marino
4126cdfca03SJohn Marino itv.it_value.tv_sec = wait;
4136cdfca03SJohn Marino itv.it_value.tv_usec = 0;
4146cdfca03SJohn Marino itv.it_interval = itv.it_value;
4156cdfca03SJohn Marino setitimer(ITIMER_REAL, &itv, NULL);
4166cdfca03SJohn Marino }
4176cdfca03SJohn Marino
4186cdfca03SJohn Marino /*
419*3a184c67SAntonio Huete Jimenez * Install a non-restartable POSIX signal handler.
4206cdfca03SJohn Marino */
4216cdfca03SJohn Marino sigfunc
422*3a184c67SAntonio Huete Jimenez xsignal(int sig, sigfunc func)
4236cdfca03SJohn Marino {
424*3a184c67SAntonio Huete Jimenez #ifdef ultrix /* XXX: this is suboptimal - how do we test sigvec vs. sigaction? */
4256cdfca03SJohn Marino struct sigvec vec, ovec;
4266cdfca03SJohn Marino
4276cdfca03SJohn Marino vec.sv_handler = func;
4286cdfca03SJohn Marino sigemptyset(&vec.sv_mask);
4296cdfca03SJohn Marino vec.sv_flags = 0;
4306cdfca03SJohn Marino if (sigvec(sig, &vec, &ovec) < 0)
4316cdfca03SJohn Marino return (SIG_ERR);
4326cdfca03SJohn Marino return (ovec.sv_handler);
4336cdfca03SJohn Marino #else /* ! ultrix */
4346cdfca03SJohn Marino struct sigaction act, oact;
4356cdfca03SJohn Marino act.sa_handler = func;
4366cdfca03SJohn Marino
4376cdfca03SJohn Marino sigemptyset(&act.sa_mask);
438*3a184c67SAntonio Huete Jimenez act.sa_flags = 0;
439*3a184c67SAntonio Huete Jimenez #if defined(SA_INTERRUPT) /* SunOS 4.x */
440*3a184c67SAntonio Huete Jimenez act.sa_flags = SA_INTERRUPT;
4416cdfca03SJohn Marino #endif
4426cdfca03SJohn Marino if (sigaction(sig, &act, &oact) < 0)
4436cdfca03SJohn Marino return (SIG_ERR);
4446cdfca03SJohn Marino return (oact.sa_handler);
4456cdfca03SJohn Marino #endif /* ! ultrix */
4466cdfca03SJohn Marino }
447