Fix a problem with the cd(4) driver -- the CAMGETPASSTHRU ioctl wouldn't
[dragonfly.git] / contrib / top / utils.c
blobab0a33db5ea505c49438369d5ab3795735235956
1 /*
2 * Top users/processes display for Unix
3 * Version 3
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.
19 #include "top.h"
20 #include "os.h"
22 int atoiwi(str)
24 char *str;
27 register int len;
29 len = strlen(str);
30 if (len != 0)
32 if (strncmp(str, "infinity", len) == 0 ||
33 strncmp(str, "all", len) == 0 ||
34 strncmp(str, "maximum", len) == 0)
36 return(Infinity);
38 else if (str[0] == '-')
40 return(Invalid);
42 else
44 return(atoi(str));
47 return(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
53 * don't use them).
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
60 * digits.
63 char *itoa(val)
65 register int val;
68 register char *ptr;
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);
75 *--ptr = '\0';
76 if (val == 0)
78 *--ptr = '0';
80 else while (val != 0)
82 *--ptr = (val % 10) + '0';
83 val /= 10;
85 return(ptr);
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.
94 char *itoa7(val)
96 register int val;
99 register char *ptr;
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);
106 *--ptr = '\0';
107 if (val == 0)
109 *--ptr = '0';
111 else while (val != 0)
113 *--ptr = (val % 10) + '0';
114 val /= 10;
116 while (ptr > buffer + sizeof(buffer) - 7)
118 *--ptr = ' ';
120 return(ptr);
124 * digits(val) - return number of decimal digits in val. Only works for
125 * positive numbers. If val <= 0 then digits(val) == 0.
128 int digits(val)
130 int val;
133 register int cnt = 0;
135 while (val > 0)
137 cnt++;
138 val /= 10;
140 return(cnt);
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)
150 register char *to;
151 register char *from;
154 while ((*to++ = *from++) != '\0');
155 return(--to);
159 * string_index(string, array) - find string in array and return index
162 int string_index(string, array)
164 char *string;
165 char **array;
168 register int i = 0;
170 while (*array != NULL)
172 if (strcmp(string, *array) == 0)
174 return(i);
176 array++;
177 i++;
179 return(-1);
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)
191 char *line;
192 int *cntp;
195 register char *from;
196 register char *to;
197 register int cnt;
198 register int ch;
199 int length;
200 int lastch;
201 register char **argv;
202 char **argarray;
203 char *args;
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 */
209 from = line;
210 lastch = cnt = length = 0;
211 while ((ch = *from++) != '\0')
213 length++;
214 if (ch == ' ' && lastch != ' ')
216 cnt++;
218 lastch = ch;
221 /* add three to the count: one for the initial "dummy" argument,
222 one for the last argument and one for NULL */
223 cnt += 3;
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 */
232 from = line;
233 to = args;
234 argv = argarray;
235 lastch = '\0';
237 /* create a dummy argument to keep getopt happy */
238 *argv++ = to;
239 *to++ = '\0';
240 cnt = 2;
242 /* now build argv while copying characters */
243 *argv++ = to;
244 while ((ch = *from++) != '\0')
246 if (ch != ' ')
248 if (lastch == ' ')
250 *to++ = '\0';
251 *argv++ = to;
252 cnt++;
254 *to++ = ch;
256 lastch = ch;
258 *to++ = '\0';
260 /* set cntp and return the allocated array */
261 *cntp = cnt;
262 return(argarray);
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)
276 int cnt;
277 int *out;
278 register long *new;
279 register long *old;
280 long *diffs;
283 register int i;
284 register long change;
285 register long total_change;
286 register long *dp;
287 long half_total;
289 /* initialization */
290 total_change = 0;
291 dp = diffs;
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 */
299 change = (int)
300 ((unsigned long)*new-(unsigned long)*old);
302 total_change += (*dp++ = change);
303 *old++ = *new++;
306 /* avoid divide by zero potential */
307 if (total_change == 0)
309 total_change = 1;
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 */
316 if(total_change) {
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
335 * use strerror.
338 /* externs referenced by errmsg */
340 #ifndef HAVE_STRERROR
341 #ifndef SYS_ERRLIST_DECLARED
342 #define SYS_ERRLIST_DECLARED
343 extern char *sys_errlist[];
344 #endif
346 extern int sys_nerr;
347 #endif
349 char *errmsg(errnum)
351 int errnum;
354 #ifdef HAVE_STRERROR
355 char *msg = strerror(errnum);
356 if (msg != NULL)
358 return msg;
360 #else
361 if (errnum > 0 && errnum < sys_nerr)
363 return((char *)sys_errlist[errnum]);
365 #endif
366 return("No error");
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.
377 /* Explanation:
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)
387 long seconds;
390 register int value;
391 register int digit;
392 register char *ptr;
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.
409 result[6] = '\0';
411 else
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));
418 return(result);
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
427 * trailing "M".
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
446 char *format_k(amt)
448 int amt;
451 static char retarray[NUM_STRINGS][16];
452 static int index = 0;
453 register char *p;
454 register char *ret;
455 register char tag = 'K';
457 p = ret = retarray[index];
458 index = (index + 1) % NUM_STRINGS;
460 if (amt >= 10000)
462 amt = (amt + 512) / 1024;
463 tag = 'M';
464 if (amt >= 10000)
466 amt = (amt + 512) / 1024;
467 tag = 'G';
471 p = strecpy(p, itoa(amt));
472 *p++ = tag;
473 *p = '\0';
475 return(ret);
478 char *format_k2(amt)
480 int amt;
483 static char retarray[NUM_STRINGS][16];
484 static int index = 0;
485 register char *p;
486 register char *ret;
487 register char tag = 'K';
489 p = ret = retarray[index];
490 index = (index + 1) % NUM_STRINGS;
492 if (amt >= 100000)
494 amt = (amt + 512) / 1024;
495 tag = 'M';
496 if (amt >= 100000)
498 amt = (amt + 512) / 1024;
499 tag = 'G';
503 p = strecpy(p, itoa(amt));
504 *p++ = tag;
505 *p = '\0';
507 return(ret);