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
11 * $FreeBSD: src/contrib/top/utils.c,v 1.3.6.1 2002/08/11 17:09:25 dwmalone Exp $
12 * $DragonFly: src/contrib/top/utils.c,v 1.3 2003/07/11 23:33:24 dillon Exp $
16 * This file contains various handy utilities used by top.
32 if (strncmp(str
, "infinity", len
) == 0 ||
33 strncmp(str
, "all", len
) == 0 ||
34 strncmp(str
, "maximum", len
) == 0)
38 else if (str
[0] == '-')
51 * itoa - convert integer (decimal) to ascii string for positive numbers
52 * only (we don't bother with negative numbers since we know we
57 * How do we know that 16 will suffice?
58 * Because the biggest number that we will
59 * ever convert will be 2^32-1, which is 10
69 static char buffer
[16]; /* result is built here */
70 /* 16 is sufficient since the largest number
71 we will ever convert will be 2^32-1,
72 which is 10 digits. */
74 ptr
= buffer
+ sizeof(buffer
);
82 *--ptr
= (val
% 10) + '0';
89 * itoa7(val) - like itoa, except the number is right justified in a 7
90 * character field. This code is a duplication of itoa instead of
91 * a front end to a more general routine for efficiency.
100 static char buffer
[16]; /* result is built here */
101 /* 16 is sufficient since the largest number
102 we will ever convert will be 2^32-1,
103 which is 10 digits. */
105 ptr
= buffer
+ sizeof(buffer
);
111 else while (val
!= 0)
113 *--ptr
= (val
% 10) + '0';
116 while (ptr
> buffer
+ sizeof(buffer
) - 7)
124 * digits(val) - return number of decimal digits in val. Only works for
125 * positive numbers. If val <= 0 then digits(val) == 0.
133 register int cnt
= 0;
144 * strecpy(to, from) - copy string "from" into "to" and return a pointer
145 * to the END of the string "to".
148 char *strecpy(to
, from
)
154 while ((*to
++ = *from
++) != '\0');
159 * string_index(string, array) - find string in array and return index
162 int string_index(string
, array
)
170 while (*array
!= NULL
)
172 if (strcmp(string
, *array
) == 0)
183 * argparse(line, cntp) - parse arguments in string "line", separating them
184 * out into an argv-like array, and setting *cntp to the number of
185 * arguments encountered. This is a simple parser that doesn't understand
186 * squat about quotes.
189 char **argparse(line
, cntp
)
201 register char **argv
;
205 /* unfortunately, the only real way to do this is to go thru the
206 input string twice. */
208 /* step thru the string counting the white space sections */
210 lastch
= cnt
= length
= 0;
211 while ((ch
= *from
++) != '\0')
214 if (ch
== ' ' && lastch
!= ' ')
221 /* add three to the count: one for the initial "dummy" argument,
222 one for the last argument and one for NULL */
225 /* allocate a char * array to hold the pointers */
226 argarray
= (char **)malloc(cnt
* sizeof(char *));
228 /* allocate another array to hold the strings themselves */
229 args
= (char *)malloc(length
+2);
231 /* initialization for main loop */
237 /* create a dummy argument to keep getopt happy */
242 /* now build argv while copying characters */
244 while ((ch
= *from
++) != '\0')
260 /* set cntp and return the allocated array */
266 * percentages(cnt, out, new, old, diffs) - calculate percentage change
267 * between array "old" and "new", putting the percentages i "out".
268 * "cnt" is size of each array and "diffs" is used for scratch space.
269 * The array "old" is updated on each call.
270 * The routine assumes modulo arithmetic. This function is especially
271 * useful on BSD mchines for calculating cpu state percentages.
274 long percentages(cnt
, out
, new, old
, diffs
)
284 register long change
;
285 register long total_change
;
293 /* calculate changes for each state and the overall change */
294 for (i
= 0; i
< cnt
; i
++)
296 if ((change
= *new - *old
) < 0)
298 /* this only happens when the counter wraps */
300 ((unsigned long)*new-(unsigned long)*old
);
302 total_change
+= (*dp
++ = change
);
306 /* avoid divide by zero potential */
307 if (total_change
== 0)
312 /* calculate percentages based on overall change, rounding up */
313 half_total
= total_change
/ 2l;
315 /* Do not divide by 0. Causes Floating point exception */
317 for (i
= 0; i
< cnt
; i
++)
319 *out
++ = (int)((*diffs
++ * 1000LL + half_total
) / total_change
);
323 /* return the total in case the caller wants to use it */
324 return(total_change
);
328 * errmsg(errnum) - return an error message string appropriate to the
329 * error number "errnum". This is a substitute for the System V
330 * function "strerror". There appears to be no reliable way to
331 * determine if "strerror" exists at compile time, so I make do
332 * by providing something of similar functionality. For those
333 * systems that have strerror and NOT errlist, define
334 * -DHAVE_STRERROR in the module file and this function will
338 /* externs referenced by errmsg */
340 #ifndef HAVE_STRERROR
341 #ifndef SYS_ERRLIST_DECLARED
342 #define SYS_ERRLIST_DECLARED
343 extern char *sys_errlist
[];
355 char *msg
= strerror(errnum
);
361 if (errnum
> 0 && errnum
< sys_nerr
)
363 return((char *)sys_errlist
[errnum
]);
369 /* format_time(seconds) - format number of seconds into a suitable
370 * display that will fit within 6 characters. Note that this
371 * routine builds its string in a static area. If it needs
372 * to be called more than once without overwriting previous data,
373 * then we will need to adopt a technique similar to the
374 * one used for format_k.
378 We want to keep the output within 6 characters. For low values we use
379 the format mm:ss. For values that exceed 999:59, we switch to a format
380 that displays hours and fractions: hhh.tH. For values that exceed
381 999.9, we use hhhh.t and drop the "H" designator. For values that
382 exceed 9999.9, we use "???".
385 char *format_time(seconds
)
393 static char result
[10];
395 /* sanity protection */
396 if (seconds
< 0 || seconds
> (99999l * 360l))
398 strcpy(result
, " ???");
400 else if (seconds
>= (1000l * 60l))
402 /* alternate (slow) method displaying hours and tenths */
403 sprintf(result
, "%5.1fH", (double)seconds
/ (double)(60l * 60l));
405 /* It is possible that the sprintf took more than 6 characters.
406 If so, then the "H" appears as result[6]. If not, then there
407 is a \0 in result[6]. Either way, it is safe to step on.
413 /* standard method produces MMM:SS */
414 /* we avoid printf as must as possible to make this quick */
415 sprintf(result
, "%3ld:%02ld",
416 (long)(seconds
/ 60), (long)(seconds
% 60));
422 * format_k(amt) - format a kilobyte memory value, returning a string
423 * suitable for display. Returns a pointer to a static
424 * area that changes each call. "amt" is converted to a
425 * string with a trailing "K". If "amt" is 10000 or greater,
426 * then it is formatted as megabytes (rounded) with a
431 * Compromise time. We need to return a string, but we don't want the
432 * caller to have to worry about freeing a dynamically allocated string.
433 * Unfortunately, we can't just return a pointer to a static area as one
434 * of the common uses of this function is in a large call to sprintf where
435 * it might get invoked several times. Our compromise is to maintain an
436 * array of strings and cycle thru them with each invocation. We make the
437 * array large enough to handle the above mentioned case. The constant
438 * NUM_STRINGS defines the number of strings in this array: we can tolerate
439 * up to NUM_STRINGS calls before we start overwriting old information.
440 * Keeping NUM_STRINGS a power of two will allow an intelligent optimizer
441 * to convert the modulo operation into something quicker. What a hack!
444 #define NUM_STRINGS 8
451 static char retarray
[NUM_STRINGS
][16];
452 static int index
= 0;
455 register char tag
= 'K';
457 p
= ret
= retarray
[index
];
458 index
= (index
+ 1) % NUM_STRINGS
;
462 amt
= (amt
+ 512) / 1024;
466 amt
= (amt
+ 512) / 1024;
471 p
= strecpy(p
, itoa(amt
));
483 static char retarray
[NUM_STRINGS
][16];
484 static int index
= 0;
487 register char tag
= 'K';
489 p
= ret
= retarray
[index
];
490 index
= (index
+ 1) % NUM_STRINGS
;
494 amt
= (amt
+ 512) / 1024;
498 amt
= (amt
+ 512) / 1024;
503 p
= strecpy(p
, itoa(amt
));