2 * Copyright (c) 1998 Andrzej Bialecki
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Small replacement for ps(1) - uses only sysctl(3) to retrieve info
36 #include <sys/param.h>
37 #include <sys/sysctl.h>
41 char p_stat[] = "?iRSTZWM";
44 main(int argc, char *argv[])
46 int mib[4], i, num, len, j, plen;
47 char buf[MAXPATHLEN], vty[5], pst[5], wmesg[10];
48 struct kinfo_proc *ki;
54 mib[2] = KERN_PROC_ALL;
55 if (sysctl(mib, 3, NULL, &len, NULL, 0) != 0) {
56 perror("sysctl sizing");
59 t = (char *)malloc(len);
60 if (sysctl(mib, 3, t, &len, NULL, 0) != 0) {
61 perror("sysctl info");
64 mib[2] = KERN_PROC_ARGS;
65 num = len / KINFO_PROC_SIZE;
67 printf("USERNAME PID PPID PRI NICE TTY STAT WCHAN COMMAND\n");
69 ki = (struct kinfo_proc *)(t + (num - i - 1) * KINFO_PROC_SIZE);
72 if (sysctl(mib, 4, buf, &plen, NULL, 0) != 0) {
73 perror("sysctl cmd info");
77 sprintf(buf, "(%s)", ki->ki_comm);
79 for (j = 0; j < plen - 1; j++) {
80 if (buf[j] == '\0') buf[j] = ' ';
83 if (strcmp(ki->ki_wmesg, "") == 0) {
86 strcpy(wmesg, ki->ki_wmesg);
88 ma = major(ki->ki_tdev);
89 mi = minor(ki->ki_tdev);
96 sprintf(vty, "v%d", mi);
104 sprintf(vty, "p%d", mi);
107 sprintf(pst, "%c", p_stat[ki->ki_stat]);
108 printf("%8s %5u %5u %3d %4d %3s %-4s %-7s %s\n",
112 ki->ki_pri.pri_level, /* XXX check this */