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/commands.c,v 1.4.6.1 2002/08/11 17:09:25 dwmalone Exp $
12 * $DragonFly: src/contrib/top/commands.c,v 1.3 2006/02/15 12:54:36 corecode Exp $
16 * This file contains the routines that implement some of the interactive
17 * mode commands. Note that some of the commands are implemented in-line
18 * in "main". This is necessary because they change the global state of
19 * "top" (i.e.: changing the number of processes to display).
27 #include <sys/resource.h>
29 #include "sigdesc.h" /* generated automatically */
34 extern char *copyright;
36 /* imported from screen.c */
37 extern int overstrike;
43 * show_help() - display the help screen; invoked in response to
50 printf("Top version %s, %s\n", version_string(), copyright);
52 A top users display for Unix\n\
54 These single-character commands are available:\n\
58 h or ? - help; show this text\n", stdout);
60 /* not all commands are availalbe with overstrike terminals */
64 Other commands are also available, but this terminal is not\n\
65 sophisticated enough to handle those commands gracefully.\n\n", stdout);
70 d - change number of displays to show\n\
71 e - list errors generated by last \"kill\" or \"renice\" command\n\
72 i - toggle the displaying of idle processes\n\
74 O - only display threads\n\
75 T - toggle the displaying of threads\n\
76 S - toggle the displaying of system processes\n\
77 k - kill processes; send a signal to a list of processes\n\
78 n or # - change number of processes to display\n", stdout);
81 o - specify sort order (pri, size, res, cpu, time, thr)\n", stdout);
84 r - renice a process\n\
85 s - change number of seconds to delay between updates\n\
86 u - display processes for only one user (+ selects all users)\n\
93 * Utility routines that help with some of the commands.
101 if ((str = strchr(str, ' ')) == NULL)
106 while (*++str == ' ') /* loop */;
108 /* if there is nothing left of the string, return NULL */
109 /* This fix is dedicated to Greg Earle */
110 return(*str == '\0' ? NULL : str);
119 register int val = 0;
122 /* if there is nothing left of the string, flag it as an error */
123 /* This fix is dedicated to Greg Earle */
129 while ((ch = *str++) != '\0')
133 val = val * 10 + (ch - '0');
135 else if (isspace(ch))
149 * Some of the commands make system calls that could generate errors.
150 * These errors are collected up in an array of structures for later
151 * contemplation and display. Such routines return a string containing an
152 * error message, or NULL if no errors occurred. The next few routines are
153 * for manipulating and displaying these errors. We need an upper limit on
154 * the number of errors, so we arbitrarily choose 20.
159 struct errs /* structure for a system-call error */
161 int errnum; /* value of errno (that is, the actual error) */
162 char *arg; /* argument that caused the error */
165 static struct errs errs[ERRMAX];
167 static char *err_toomany = " too many errors occurred";
168 static char *err_listem =
169 " Many errors occurred. Press `e' to display the list of errors.";
171 /* These macros get used to reset and log the errors */
172 #define ERR_RESET errcnt = 0
173 #define ERROR(p, e) if (errcnt >= ERRMAX) \
175 return(err_toomany); \
179 errs[errcnt].arg = (p); \
180 errs[errcnt++].errnum = (e); \
184 * err_string() - return an appropriate error string. This is what the
185 * command will return for displaying. If no errors were logged, then
186 * return NULL. The maximum length of the error string is defined by
195 register struct errs *errp;
196 register int cnt = 0;
197 register int first = Yes;
198 register int currerr = -1;
199 int stringlen; /* characters still available in "string" */
200 static char string[STRMAX];
202 /* if there are no errors, return NULL */
208 /* sort the errors */
209 qsort((char *)errs, errcnt, sizeof(struct errs), err_compar);
211 /* need a space at the front of the error string */
214 stringlen = STRMAX - 2;
216 /* loop thru the sorted list, building an error string */
219 errp = &(errs[cnt++]);
220 if (errp->errnum != currerr)
224 if ((stringlen = str_adderr(string, stringlen, currerr)) < 2)
228 (void) strcat(string, "; "); /* we know there's more */
230 currerr = errp->errnum;
233 if ((stringlen = str_addarg(string, stringlen, errp->arg, first)) ==0)
240 /* add final message */
241 stringlen = str_adderr(string, stringlen, currerr);
243 /* return the error string */
244 return(stringlen == 0 ? err_listem : string);
248 * str_adderr(str, len, err) - add an explanation of error "err" to
252 str_adderr(str, len, err)
262 msg = err == 0 ? "Not a number" : errmsg(err);
263 msglen = strlen(msg) + 2;
268 (void) strcat(str, ": ");
269 (void) strcat(str, msg);
270 return(len - msglen);
274 * str_addarg(str, len, arg, first) - add the string argument "arg" to
275 * the string "str". This is the first in the group when "first"
276 * is set (indicating that a comma should NOT be added to the front).
279 str_addarg(str, len, arg, first)
289 arglen = strlen(arg);
300 (void) strcat(str, ", ");
302 (void) strcat(str, arg);
303 return(len - arglen);
307 * err_compar(p1, p2) - comparison routine used by "qsort"
308 * for sorting errors.
313 register struct errs *p1, *p2;
318 if ((result = p1->errnum - p2->errnum) == 0)
320 return(strcmp(p1->arg, p2->arg));
326 * error_count() - return the number of errors currently logged.
336 * show_errors() - display on stdout the current log of errors.
342 register int cnt = 0;
343 register struct errs *errp = errs;
345 printf("%d error%s:\n\n", errcnt, errcnt == 1 ? "" : "s");
346 while (cnt++ < errcnt)
348 printf("%5s: %s\n", errp->arg,
349 errp->errnum == 0 ? "Not a number" : errmsg(errp->errnum));
355 * kill_procs(str) - send signals to processes, much like the "kill"
356 * command does; invoked in response to 'k'.
359 char *kill_procs(str)
365 int signum = SIGTERM; /* default */
367 struct sigdesc *sigp;
370 /* reset error array */
373 /* remember our uid */
376 /* skip over leading white space */
377 while (isspace(*str)) str++;
381 /* explicit signal specified */
382 if ((nptr = next_field(str)) == NULL)
384 return(" kill: no processes specified");
389 (void) scanint(str + 1, &signum);
390 if (signum <= 0 || signum >= NSIG)
392 return(" invalid signal number");
397 /* translate the name into a number */
398 for (sigp = sigdesc; sigp->name != NULL; sigp++)
400 if (strcmp(sigp->name, str + 1) == 0)
402 signum = sigp->number;
407 /* was it ever found */
408 if (sigp->name == NULL)
410 return(" bad signal name");
413 /* put the new pointer in place */
417 /* loop thru the string, killing processes */
420 if (scanint(str, &procnum) == -1)
426 /* check process owner if we're not root */
427 if (uid && (uid != proc_owner(procnum)))
431 /* go in for the kill */
432 else if (kill(procnum, signum) == -1)
434 /* chalk up an error */
438 } while ((str = next_field(str)) != NULL);
440 /* return appropriate error string */
441 return(err_string());
445 * renice_procs(str) - change the "nice" of processes, much like the
446 * "renice" command does; invoked in response to 'r'.
449 char *renice_procs(str)
454 register char negate;
462 /* allow for negative priority values */
463 if ((negate = (*str == '-')) != 0)
465 /* move past the minus sign */
469 /* use procnum as a temporary holding place and get the number */
470 procnum = scanint(str, &prio);
472 /* negate if necessary */
478 #if defined(PRIO_MIN) && defined(PRIO_MAX)
479 /* check for validity */
480 if (procnum == -1 || prio < PRIO_MIN || prio > PRIO_MAX)
482 return(" bad priority value");
486 /* move to the first process number */
487 if ((str = next_field(str)) == NULL)
489 return(" no processes specified");
492 /* loop thru the process numbers, renicing each one */
495 if (scanint(str, &procnum) == -1)
500 /* check process owner if we're not root */
501 else if (uid && (uid != proc_owner(procnum)))
505 else if (setpriority(PRIO_PROCESS, procnum, prio) == -1)
509 } while ((str = next_field(str)) != NULL);
511 /* return appropriate error string */
512 return(err_string());