2 * Top users/processes display for Unix
5 * This program may be freely redistributed,
6 * but this entire comment MUST remain intact.
8 * Copyright (c) 1984, 1989, William LeFebvre, Rice University
9 * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
15 * This file contains various handy utilities used by top.
31 if (strncmp(str
, "infinity", len
) == 0 ||
32 strncmp(str
, "all", len
) == 0 ||
33 strncmp(str
, "maximum", len
) == 0)
37 else if (str
[0] == '-')
50 * itoa - convert integer (decimal) to ascii string for positive numbers
51 * only (we don't bother with negative numbers since we know we
56 * How do we know that 16 will suffice?
57 * Because the biggest number that we will
58 * ever convert will be 2^32-1, which is 10
68 static char buffer
[16]; /* result is built here */
69 /* 16 is sufficient since the largest number
70 we will ever convert will be 2^32-1,
71 which is 10 digits. */
73 ptr
= buffer
+ sizeof(buffer
);
81 *--ptr
= (val
% 10) + '0';
88 * itoa7(val) - like itoa, except the number is right justified in a 7
89 * character field. This code is a duplication of itoa instead of
90 * a front end to a more general routine for efficiency.
99 static char buffer
[16]; /* result is built here */
100 /* 16 is sufficient since the largest number
101 we will ever convert will be 2^32-1,
102 which is 10 digits. */
104 ptr
= buffer
+ sizeof(buffer
);
110 else while (val
!= 0)
112 *--ptr
= (val
% 10) + '0';
115 while (ptr
> buffer
+ sizeof(buffer
) - 7)
123 * digits(val) - return number of decimal digits in val. Only works for
124 * positive numbers. If val <= 0 then digits(val) == 0.
132 register int cnt
= 0;
143 * strecpy(to, from) - copy string "from" into "to" and return a pointer
144 * to the END of the string "to".
147 char *strecpy(to
, from
)
153 while ((*to
++ = *from
++) != '\0');
158 * string_index(string, array) - find string in array and return index
161 int string_index(string
, array
)
169 while (*array
!= NULL
)
171 if (strcmp(string
, *array
) == 0)
182 * argparse(line, cntp) - parse arguments in string "line", separating them
183 * out into an argv-like array, and setting *cntp to the number of
184 * arguments encountered. This is a simple parser that doesn't understand
185 * squat about quotes.
188 char **argparse(line
, cntp
)
200 register char **argv
;
204 /* unfortunately, the only real way to do this is to go thru the
205 input string twice. */
207 /* step thru the string counting the white space sections */
209 lastch
= cnt
= length
= 0;
210 while ((ch
= *from
++) != '\0')
213 if (ch
== ' ' && lastch
!= ' ')
220 /* add three to the count: one for the initial "dummy" argument,
221 one for the last argument and one for NULL */
224 /* allocate a char * array to hold the pointers */
225 argarray
= (char **)malloc(cnt
* sizeof(char *));
227 /* allocate another array to hold the strings themselves */
228 args
= (char *)malloc(length
+2);
230 /* initialization for main loop */
236 /* create a dummy argument to keep getopt happy */
241 /* now build argv while copying characters */
243 while ((ch
= *from
++) != '\0')
259 /* set cntp and return the allocated array */
265 * percentages(cnt, out, new, old, diffs) - calculate percentage change
266 * between array "old" and "new", putting the percentages i "out".
267 * "cnt" is size of each array and "diffs" is used for scratch space.
268 * The array "old" is updated on each call.
269 * The routine assumes modulo arithmetic. This function is especially
270 * useful on BSD mchines for calculating cpu state percentages.
273 long percentages(cnt
, out
, new, old
, diffs
)
283 register long change
;
284 register long total_change
;
292 /* calculate changes for each state and the overall change */
293 for (i
= 0; i
< cnt
; i
++)
295 if ((change
= *new - *old
) < 0)
297 /* this only happens when the counter wraps */
299 ((unsigned long)*new-(unsigned long)*old
);
301 total_change
+= (*dp
++ = change
);
305 /* avoid divide by zero potential */
306 if (total_change
== 0)
311 /* calculate percentages based on overall change, rounding up */
312 half_total
= total_change
/ 2l;
314 /* Do not divide by 0. Causes Floating point exception */
316 for (i
= 0; i
< cnt
; i
++)
318 *out
++ = (int)((*diffs
++ * 1000 + half_total
) / total_change
);
322 /* return the total in case the caller wants to use it */
323 return(total_change
);
327 * errmsg(errnum) - return an error message string appropriate to the
328 * error number "errnum". This is a substitute for the System V
329 * function "strerror". There appears to be no reliable way to
330 * determine if "strerror" exists at compile time, so I make do
331 * by providing something of similar functionality. For those
332 * systems that have strerror and NOT errlist, define
333 * -DHAVE_STRERROR in the module file and this function will
337 /* externs referenced by errmsg */
339 #ifndef HAVE_STRERROR
340 #ifndef SYS_ERRLIST_DECLARED
341 #define SYS_ERRLIST_DECLARED
342 extern char *sys_errlist
[];
354 char *msg
= strerror(errnum
);
360 if (errnum
> 0 && errnum
< sys_nerr
)
362 return((char *)sys_errlist
[errnum
]);
368 /* format_time(seconds) - format number of seconds into a suitable
369 * display that will fit within 6 characters. Note that this
370 * routine builds its string in a static area. If it needs
371 * to be called more than once without overwriting previous data,
372 * then we will need to adopt a technique similar to the
373 * one used for format_k.
377 We want to keep the output within 6 characters. For low values we use
378 the format mm:ss. For values that exceed 999:59, we switch to a format
379 that displays hours and fractions: hhh.tH. For values that exceed
380 999.9, we use hhhh.t and drop the "H" designator. For values that
381 exceed 9999.9, we use "???".
384 char *format_time(seconds
)
392 static char result
[10];
394 /* sanity protection */
395 if (seconds
< 0 || seconds
> (99999l * 360l))
397 strcpy(result
, " ???");
399 else if (seconds
>= (1000l * 60l))
401 /* alternate (slow) method displaying hours and tenths */
402 sprintf(result
, "%5.1fH", (double)seconds
/ (double)(60l * 60l));
404 /* It is possible that the sprintf took more than 6 characters.
405 If so, then the "H" appears as result[6]. If not, then there
406 is a \0 in result[6]. Either way, it is safe to step on.
412 /* standard method produces MMM:SS */
413 /* we avoid printf as must as possible to make this quick */
414 sprintf(result
, "%3ld:%02ld",
415 (long)(seconds
/ 60), (long)(seconds
% 60));
421 * format_k(amt) - format a kilobyte memory value, returning a string
422 * suitable for display. Returns a pointer to a static
423 * area that changes each call. "amt" is converted to a
424 * string with a trailing "K". If "amt" is 10000 or greater,
425 * then it is formatted as megabytes (rounded) with a
430 * Compromise time. We need to return a string, but we don't want the
431 * caller to have to worry about freeing a dynamically allocated string.
432 * Unfortunately, we can't just return a pointer to a static area as one
433 * of the common uses of this function is in a large call to sprintf where
434 * it might get invoked several times. Our compromise is to maintain an
435 * array of strings and cycle thru them with each invocation. We make the
436 * array large enough to handle the above mentioned case. The constant
437 * NUM_STRINGS defines the number of strings in this array: we can tolerate
438 * up to NUM_STRINGS calls before we start overwriting old information.
439 * Keeping NUM_STRINGS a power of two will allow an intelligent optimizer
440 * to convert the modulo operation into something quicker. What a hack!
443 #define NUM_STRINGS 8
450 static char retarray
[NUM_STRINGS
][16];
451 static int index
= 0;
454 register char tag
= 'K';
456 p
= ret
= retarray
[index
];
457 index
= (index
+ 1) % NUM_STRINGS
;
461 amt
= (amt
+ 512) / 1024;
465 amt
= (amt
+ 512) / 1024;
470 p
= strecpy(p
, itoa(amt
));
482 static char retarray
[NUM_STRINGS
][16];
483 static int index
= 0;
486 register char tag
= 'K';
488 p
= ret
= retarray
[index
];
489 index
= (index
+ 1) % NUM_STRINGS
;
493 amt
= (amt
+ 512) / 1024;
497 amt
= (amt
+ 512) / 1024;
502 p
= strecpy(p
, itoa(amt
));