Fix PR#3585 - csh printed a negative percentage of CPU used when the total

CPU usage was more than 6 hours (integer overflow problem).
This commit is contained in:
fair 1998-04-08 22:38:18 +00:00
parent 0f668275d6
commit 000297a954
3 changed files with 71 additions and 7 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.17 1997/10/22 01:22:58 lukem Exp $
# $NetBSD: Makefile,v 1.18 1998/04/08 22:38:18 fair Exp $
# @(#)Makefile 8.1 (Berkeley) 5/31/93
#
# C Shell with process control; VM/UNIX VAX Makefile
@ -11,7 +11,7 @@ DFLAGS=-DBUILTIN -DFILEC -DNLS -DSHORT_STRINGS
CPPFLAGS+=-I${.CURDIR} -I. ${DFLAGS}
SRCS= alloc.c char.c const.c csh.c dir.c dol.c err.c exec.c exp.c file.c \
func.c glob.c hist.c init.c lex.c misc.c parse.c printf.c proc.c \
sem.c set.c str.c time.c
sem.c set.c str.c strpct.c time.c
.PATH: ${.CURDIR}/../../usr.bin/printf
MLINKS= csh.1 limit.1 csh.1 alias.1 csh.1 bg.1 csh.1 dirs.1 csh.1 fg.1 \

61
bin/csh/strpct.c Normal file
View File

@ -0,0 +1,61 @@
/*
** Calculate a percentage without resorting to floating point
** and return a pointer to a string
**
** "digits" is the number of digits past the decimal place you want
** (zero being the straight percentage with no decimals)
**
** Erik E. Fair <fair@clock.org>, May 8, 1997
*/
#include <stdio.h>
#include <machine/limits.h>
#include <sys/types.h>
extern char * strpct(u_long num, u_long denom, u_int digits);
char *
strpct(numerator, denominator, digits)
u_long numerator, denominator;
u_int digits;
{
register int i;
u_long result, factor = 100L;
static char percent[32];
/* I should check for digit overflow here, too XXX */
for(i = 0; i < digits; i++) {
factor *= 10;
}
/* watch out for overflow! */
if (numerator < (ULONG_MAX / factor)) {
numerator *= factor;
} else {
/* toss some of the bits of lesser significance */
denominator /= factor;
}
/* divide by zero is just plain bad */
if (denominator == 0L) {
denominator = 1L;
}
result = numerator / denominator;
if (digits == 0) {
(void) snprintf(percent, sizeof(percent), "%lu%%", result);
} else {
char fmt[32];
/* indirection to produce the right output format */
(void) snprintf(fmt, sizeof(fmt), "%%lu.%%0%ulu%%%%", digits);
factor /= 100L; /* undo initialization */
(void) snprintf(percent, sizeof(percent),
fmt, result / factor, result % factor);
}
return(percent);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: time.c,v 1.9 1997/07/04 21:24:11 christos Exp $ */
/* $NetBSD: time.c,v 1.10 1998/04/08 22:38:18 fair Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
@ -38,7 +38,7 @@
#if 0
static char sccsid[] = "@(#)time.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: time.c,v 1.9 1997/07/04 21:24:11 christos Exp $");
__RCSID("$NetBSD: time.c,v 1.10 1998/04/08 22:38:18 fair Exp $");
#endif
#endif /* not lint */
@ -56,6 +56,7 @@ __RCSID("$NetBSD: time.c,v 1.9 1997/07/04 21:24:11 christos Exp $");
* C Shell - routines handling process timing and niceing
*/
static void pdeltat __P((struct timeval *, struct timeval *));
extern char * strpct __P((u_long num, u_long denom, u_int digits));
void
settimes()
@ -174,9 +175,11 @@ prusage(r0, r1, e, b)
case 'P': /* percent time spent running */
/* check if it did not run at all */
i = (ms == 0) ? 0 : (t * 1000 / ms);
/* nn.n% */
(void) fprintf(cshout, "%ld.%01ld%%", i / 10, i % 10);
if (ms == 0) {
(void) fputs("0.0%", cshout);
} else {
(void) fputs(strpct((ulong)t, (ulong)ms, 1), cshout);
}
break;
case 'W': /* number of swaps */