home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.muug.mb.ca
/
2014.06.ftp.muug.mb.ca.tar
/
ftp.muug.mb.ca
/
pub
/
src
/
top
/
utils.c
< prev
Wrap
C/C++ Source or Header
|
1992-02-01
|
2KB
|
122 lines
/*
* Top - a top users display for Berkeley Unix
*
* This file contains various handy utilities used by top.
*/
#include "top.h"
atoiwi(str)
char *str;
{
register int len;
len = strlen(str);
if (len != 0)
{
if (strncmp(str, "infinity", len) == 0 ||
strncmp(str, "all", len) == 0 ||
strncmp(str, "maximum", len) == 0)
{
return(Infinity);
}
else if (str[0] == '-')
{
return(Invalid);
}
else
{
return(atoi(str));
}
}
return(0);
}
/*
* itoa - convert integer (decimal) to ascii string for positive numbers
* only (we don't bother with negative numbers since we know we
* don't use them).
*/
static char buffer[16]; /* shared by the next two routines */
/*
* How do we know that 16 will suffice?
* Because the biggest number that we will
* ever convert will be 2^32-1, which is 10
* digits.
*/
char *itoa(val)
register int val;
{
register char *ptr;
ptr = buffer + sizeof(buffer);
*--ptr = '\0';
if (val == 0)
{
*--ptr = '0';
}
else while (val != 0)
{
*--ptr = (val % 10) + '0';
val /= 10;
}
return(ptr);
}
/*
* itoa7(val) - like itoa, except the number is right justified in a 7
* character field. This code is a duplication of itoa instead of
* a front end to a more general routine for efficiency.
*/
char *itoa7(val)
register int val;
{
register char *ptr;
ptr = buffer + sizeof(buffer);
*--ptr = '\0';
if (val == 0)
{
*--ptr = '0';
}
else while (val != 0)
{
*--ptr = (val % 10) + '0';
val /= 10;
}
while (ptr > buffer + sizeof(buffer) - 7)
{
*--ptr = ' ';
}
return(ptr);
}
/*
* digits(val) - return number of decimal digits in val. Only works for
* positive numbers. If val <= 0 then digits(val) == 0.
*/
digits(val)
int val;
{
register int cnt = 0;
while (val > 0)
{
cnt++;
val /= 10;
}
return(cnt);
}