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.2 2003/06/17 04:24:07 dillon 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 */
36 extern char *copyright
;
38 /* imported from screen.c */
39 extern int overstrike
;
45 * show_help() - display the help screen; invoked in response to
52 printf("Top version %s, %s\n", version_string(), copyright
);
54 A top users display for Unix\n\
56 These single-character commands are available:\n\
60 h or ? - help; show this text\n", stdout
);
62 /* not all commands are availalbe with overstrike terminals */
66 Other commands are also available, but this terminal is not\n\
67 sophisticated enough to handle those commands gracefully.\n\n", stdout
);
72 d - change number of displays to show\n\
73 e - list errors generated by last \"kill\" or \"renice\" command\n\
74 i - toggle the displaying of idle processes\n\
76 k - kill processes; send a signal to a list of processes\n\
77 n or # - change number of processes to display\n", stdout
);
80 o - specify sort order (pri, size, res, cpu, time)\n", stdout
);
83 r - renice a process\n\
84 s - change number of seconds to delay between updates\n\
85 u - display processes for only one user (+ selects all users)\n\
92 * Utility routines that help with some of the commands.
100 if ((str
= strchr(str
, ' ')) == NULL
)
105 while (*++str
== ' ') /* loop */;
107 /* if there is nothing left of the string, return NULL */
108 /* This fix is dedicated to Greg Earle */
109 return(*str
== '\0' ? NULL
: str
);
118 register int val
= 0;
121 /* if there is nothing left of the string, flag it as an error */
122 /* This fix is dedicated to Greg Earle */
128 while ((ch
= *str
++) != '\0')
132 val
= val
* 10 + (ch
- '0');
134 else if (isspace(ch
))
148 * Some of the commands make system calls that could generate errors.
149 * These errors are collected up in an array of structures for later
150 * contemplation and display. Such routines return a string containing an
151 * error message, or NULL if no errors occurred. The next few routines are
152 * for manipulating and displaying these errors. We need an upper limit on
153 * the number of errors, so we arbitrarily choose 20.
158 struct errs
/* structure for a system-call error */
160 int errnum
; /* value of errno (that is, the actual error) */
161 char *arg
; /* argument that caused the error */
164 static struct errs errs
[ERRMAX
];
166 static char *err_toomany
= " too many errors occurred";
167 static char *err_listem
=
168 " Many errors occurred. Press `e' to display the list of errors.";
170 /* These macros get used to reset and log the errors */
171 #define ERR_RESET errcnt = 0
172 #define ERROR(p, e) if (errcnt >= ERRMAX) \
174 return(err_toomany); \
178 errs[errcnt].arg = (p); \
179 errs[errcnt++].errnum = (e); \
183 * err_string() - return an appropriate error string. This is what the
184 * command will return for displaying. If no errors were logged, then
185 * return NULL. The maximum length of the error string is defined by
194 register struct errs
*errp
;
195 register int cnt
= 0;
196 register int first
= Yes
;
197 register int currerr
= -1;
198 int stringlen
; /* characters still available in "string" */
199 static char string
[STRMAX
];
201 /* if there are no errors, return NULL */
207 /* sort the errors */
208 qsort((char *)errs
, errcnt
, sizeof(struct errs
), err_compar
);
210 /* need a space at the front of the error string */
213 stringlen
= STRMAX
- 2;
215 /* loop thru the sorted list, building an error string */
218 errp
= &(errs
[cnt
++]);
219 if (errp
->errnum
!= currerr
)
223 if ((stringlen
= str_adderr(string
, stringlen
, currerr
)) < 2)
227 (void) strcat(string
, "; "); /* we know there's more */
229 currerr
= errp
->errnum
;
232 if ((stringlen
= str_addarg(string
, stringlen
, errp
->arg
, first
)) ==0)
239 /* add final message */
240 stringlen
= str_adderr(string
, stringlen
, currerr
);
242 /* return the error string */
243 return(stringlen
== 0 ? err_listem
: string
);
247 * str_adderr(str, len, err) - add an explanation of error "err" to
251 str_adderr(str
, len
, err
)
261 msg
= err
== 0 ? "Not a number" : errmsg(err
);
262 msglen
= strlen(msg
) + 2;
267 (void) strcat(str
, ": ");
268 (void) strcat(str
, msg
);
269 return(len
- msglen
);
273 * str_addarg(str, len, arg, first) - add the string argument "arg" to
274 * the string "str". This is the first in the group when "first"
275 * is set (indicating that a comma should NOT be added to the front).
278 str_addarg(str
, len
, arg
, first
)
288 arglen
= strlen(arg
);
299 (void) strcat(str
, ", ");
301 (void) strcat(str
, arg
);
302 return(len
- arglen
);
306 * err_compar(p1, p2) - comparison routine used by "qsort"
307 * for sorting errors.
312 register struct errs
*p1
, *p2
;
317 if ((result
= p1
->errnum
- p2
->errnum
) == 0)
319 return(strcmp(p1
->arg
, p2
->arg
));
325 * error_count() - return the number of errors currently logged.
335 * show_errors() - display on stdout the current log of errors.
341 register int cnt
= 0;
342 register struct errs
*errp
= errs
;
344 printf("%d error%s:\n\n", errcnt
, errcnt
== 1 ? "" : "s");
345 while (cnt
++ < errcnt
)
347 printf("%5s: %s\n", errp
->arg
,
348 errp
->errnum
== 0 ? "Not a number" : errmsg(errp
->errnum
));
354 * kill_procs(str) - send signals to processes, much like the "kill"
355 * command does; invoked in response to 'k'.
358 char *kill_procs(str
)
364 int signum
= SIGTERM
; /* default */
366 struct sigdesc
*sigp
;
369 /* reset error array */
372 /* remember our uid */
375 /* skip over leading white space */
376 while (isspace(*str
)) str
++;
380 /* explicit signal specified */
381 if ((nptr
= next_field(str
)) == NULL
)
383 return(" kill: no processes specified");
388 (void) scanint(str
+ 1, &signum
);
389 if (signum
<= 0 || signum
>= NSIG
)
391 return(" invalid signal number");
396 /* translate the name into a number */
397 for (sigp
= sigdesc
; sigp
->name
!= NULL
; sigp
++)
399 if (strcmp(sigp
->name
, str
+ 1) == 0)
401 signum
= sigp
->number
;
406 /* was it ever found */
407 if (sigp
->name
== NULL
)
409 return(" bad signal name");
412 /* put the new pointer in place */
416 /* loop thru the string, killing processes */
419 if (scanint(str
, &procnum
) == -1)
425 /* check process owner if we're not root */
426 if (uid
&& (uid
!= proc_owner(procnum
)))
430 /* go in for the kill */
431 else if (kill(procnum
, signum
) == -1)
433 /* chalk up an error */
437 } while ((str
= next_field(str
)) != NULL
);
439 /* return appropriate error string */
440 return(err_string());
444 * renice_procs(str) - change the "nice" of processes, much like the
445 * "renice" command does; invoked in response to 'r'.
448 char *renice_procs(str
)
453 register char negate
;
461 /* allow for negative priority values */
462 if ((negate
= (*str
== '-')) != 0)
464 /* move past the minus sign */
468 /* use procnum as a temporary holding place and get the number */
469 procnum
= scanint(str
, &prio
);
471 /* negate if necessary */
477 #if defined(PRIO_MIN) && defined(PRIO_MAX)
478 /* check for validity */
479 if (procnum
== -1 || prio
< PRIO_MIN
|| prio
> PRIO_MAX
)
481 return(" bad priority value");
485 /* move to the first process number */
486 if ((str
= next_field(str
)) == NULL
)
488 return(" no processes specified");
491 /* loop thru the process numbers, renicing each one */
494 if (scanint(str
, &procnum
) == -1)
499 /* check process owner if we're not root */
500 else if (uid
&& (uid
!= proc_owner(procnum
)))
504 else if (setpriority(PRIO_PROCESS
, procnum
, prio
) == -1)
508 } while ((str
= next_field(str
)) != NULL
);
510 /* return appropriate error string */
511 return(err_string());