If the ETA is too large to fit on the display (i.e. 100 hours or more), don't

try to display it.
Also, eliminate many extra calls to strlen().
This commit is contained in:
mycroft 1997-10-11 12:05:15 +00:00
parent fe47ecc300
commit 088478fe61

View File

@ -1,4 +1,4 @@
/* $NetBSD: util.c,v 1.14 1997/09/21 01:06:32 lukem Exp $ */ /* $NetBSD: util.c,v 1.15 1997/10/11 12:05:15 mycroft Exp $ */
/* /*
* Copyright (c) 1985, 1989, 1993, 1994 * Copyright (c) 1985, 1989, 1993, 1994
@ -35,7 +35,7 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#ifndef lint #ifndef lint
__RCSID("$NetBSD: util.c,v 1.14 1997/09/21 01:06:32 lukem Exp $"); __RCSID("$NetBSD: util.c,v 1.15 1997/10/11 12:05:15 mycroft Exp $");
#endif /* not lint */ #endif /* not lint */
/* /*
@ -581,10 +581,12 @@ progressmeter(flag)
static off_t lastsize; static off_t lastsize;
struct timeval now, td, wait; struct timeval now, td, wait;
off_t cursize, abbrevsize; off_t cursize, abbrevsize;
double elapsed; double elapsed, remaining;
int ratio, barlength, i, remaining; int ratio, barlength, i, len;
char buf[256]; char buf[256];
len = 0;
if (flag == -1) { if (flag == -1) {
(void)gettimeofday(&start, (struct timezone *)0); (void)gettimeofday(&start, (struct timezone *)0);
lastupdate = start; lastupdate = start;
@ -598,12 +600,12 @@ progressmeter(flag)
ratio = cursize * 100 / filesize; ratio = cursize * 100 / filesize;
ratio = MAX(ratio, 0); ratio = MAX(ratio, 0);
ratio = MIN(ratio, 100); ratio = MIN(ratio, 100);
snprintf(buf, sizeof(buf), "\r%3d%% ", ratio); len += snprintf(buf + len, sizeof(buf) - len, "\r%3d%% ", ratio);
barlength = ttywidth - 30; barlength = ttywidth - 30;
if (barlength > 0) { if (barlength > 0) {
i = barlength * ratio / 100; i = barlength * ratio / 100;
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), len += snprintf(buf + len, sizeof(buf) - len,
"|%.*s%*s|", i, "|%.*s%*s|", i,
"*****************************************************************************" "*****************************************************************************"
"*****************************************************************************", "*****************************************************************************",
@ -616,7 +618,7 @@ progressmeter(flag)
i++; i++;
abbrevsize >>= 10; abbrevsize >>= 10;
} }
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), len += snprintf(buf + len, sizeof(buf) - len,
" %5qd %c%c ", (long long)abbrevsize, prefixes[i], " %5qd %c%c ", (long long)abbrevsize, prefixes[i],
prefixes[i] == ' ' ? ' ' : 'B'); prefixes[i] == ' ' ? ' ' : 'B');
@ -635,26 +637,31 @@ progressmeter(flag)
elapsed = td.tv_sec + (td.tv_usec / 1000000.0); elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
if (bytes <= 0 || elapsed <= 0.0 || cursize > filesize) { if (bytes <= 0 || elapsed <= 0.0 || cursize > filesize) {
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), len += snprintf(buf + len, sizeof(buf) - len,
" --:-- ETA"); " --:-- ETA");
} else if (wait.tv_sec >= STALLTIME) { } else if (wait.tv_sec >= STALLTIME) {
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), len += snprintf(buf + len, sizeof(buf) - len,
" - stalled -"); " - stalled -");
} else { } else {
remaining = (int)((filesize - restart_point) / remaining =
(bytes / elapsed) - elapsed); (filesize - restart_point) / (bytes / elapsed) - elapsed;
i = remaining / 3600; if (remaining >= 100 * 3600)
if (i) len += snprintf(buf + len, sizeof(buf) - len,
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " --:-- ETA");
"%2d:", i); else {
else i = (int)remaining / 3600;
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), if (i)
" "); len += snprintf(buf + len, sizeof(buf) - len,
i = remaining % 3600; "%2d:", i);
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), else
"%02d:%02d ETA", i / 60, i % 60); len += snprintf(buf + len, sizeof(buf) - len,
" ");
i = (int)remaining % 3600;
len += snprintf(buf + len, sizeof(buf) - len,
"%02d:%02d ETA", i / 60, i % 60);
}
} }
(void)write(STDOUT_FILENO, buf, strlen(buf)); (void)write(STDOUT_FILENO, buf, len);
if (flag == -1) { if (flag == -1) {
(void)signal(SIGALRM, updateprogressmeter); (void)signal(SIGALRM, updateprogressmeter);