xref: /freebsd/bin/ps/print.c (revision 282a3889ebf826db9839be296ff1dd903f6d6d6e)
1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #if 0
31 #ifndef lint
32 static char sccsid[] = "@(#)print.c	8.6 (Berkeley) 4/16/94";
33 #endif /* not lint */
34 #endif
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include <sys/param.h>
40 #include <sys/time.h>
41 #include <sys/resource.h>
42 #include <sys/proc.h>
43 #include <sys/stat.h>
44 
45 #include <sys/mac.h>
46 #include <sys/user.h>
47 #include <sys/sysctl.h>
48 
49 #include <err.h>
50 #include <grp.h>
51 #include <langinfo.h>
52 #include <locale.h>
53 #include <math.h>
54 #include <nlist.h>
55 #include <pwd.h>
56 #include <stddef.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 #include <vis.h>
62 
63 #include "ps.h"
64 
65 #define	ps_pgtok(a)	(((a) * getpagesize()) / 1024)
66 
67 void
68 printheader(void)
69 {
70 	VAR *v;
71 	struct varent *vent;
72 
73 	STAILQ_FOREACH(vent, &varlist, next_ve)
74 		if (*vent->header != '\0')
75 			break;
76 	if (!vent)
77 		return;
78 
79 	STAILQ_FOREACH(vent, &varlist, next_ve) {
80 		v = vent->var;
81 		if (v->flag & LJUST) {
82 			if (STAILQ_NEXT(vent, next_ve) == NULL)	/* last one */
83 				(void)printf("%s", vent->header);
84 			else
85 				(void)printf("%-*s", v->width, vent->header);
86 		} else
87 			(void)printf("%*s", v->width, vent->header);
88 		if (STAILQ_NEXT(vent, next_ve) != NULL)
89 			(void)putchar(' ');
90 	}
91 	(void)putchar('\n');
92 }
93 
94 void
95 arguments(KINFO *k, VARENT *ve)
96 {
97 	VAR *v;
98 	int left;
99 	char *cp, *vis_args;
100 
101 	v = ve->var;
102 	if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
103 		errx(1, "malloc failed");
104 	strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
105 	if (STAILQ_NEXT(ve, next_ve) == NULL) {
106 		/* last field */
107 		if (termwidth == UNLIMITED) {
108 			(void)printf("%s", vis_args);
109 		} else {
110 			left = termwidth - (totwidth - v->width);
111 			if (left < 1) /* already wrapped, just use std width */
112 				left = v->width;
113 			for (cp = vis_args; --left >= 0 && *cp != '\0';)
114 				(void)putchar(*cp++);
115 		}
116 	} else {
117 		(void)printf("%-*.*s", v->width, v->width, vis_args);
118 	}
119 	free(vis_args);
120 }
121 
122 void
123 command(KINFO *k, VARENT *ve)
124 {
125 	VAR *v;
126 	int left;
127 	char *cp, *vis_env, *vis_args;
128 
129 	v = ve->var;
130 	if (cflag) {
131 		/* If it is the last field, then don't pad */
132 		if (STAILQ_NEXT(ve, next_ve) == NULL)
133 			(void)printf("%s", k->ki_p->ki_comm);
134 		else
135 			(void)printf("%-*s", v->width, k->ki_p->ki_comm);
136 		return;
137 	}
138 	if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
139 		errx(1, "malloc failed");
140 	strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
141 	if (k->ki_env) {
142 		if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
143 			errx(1, "malloc failed");
144 		strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
145 	} else
146 		vis_env = NULL;
147 
148 	if (STAILQ_NEXT(ve, next_ve) == NULL) {
149 		/* last field */
150 		if (termwidth == UNLIMITED) {
151 			if (vis_env)
152 				(void)printf("%s ", vis_env);
153 			(void)printf("%s", vis_args);
154 		} else {
155 			left = termwidth - (totwidth - v->width);
156 			if (left < 1) /* already wrapped, just use std width */
157 				left = v->width;
158 			if ((cp = vis_env) != NULL) {
159 				while (--left >= 0 && *cp)
160 					(void)putchar(*cp++);
161 				if (--left >= 0)
162 					putchar(' ');
163 			}
164 			for (cp = vis_args; --left >= 0 && *cp != '\0';)
165 				(void)putchar(*cp++);
166 		}
167 	} else
168 		/* XXX env? */
169 		(void)printf("%-*.*s", v->width, v->width, vis_args);
170 	free(vis_args);
171 	if (vis_env != NULL)
172 		free(vis_env);
173 }
174 
175 void
176 ucomm(KINFO *k, VARENT *ve)
177 {
178 	VAR *v;
179 
180 	v = ve->var;
181 	if (STAILQ_NEXT(ve, next_ve) == NULL)	/* last field, don't pad */
182 		(void)printf("%s", k->ki_p->ki_comm);
183 	else
184 		(void)printf("%-*s", v->width, k->ki_p->ki_comm);
185 }
186 
187 void
188 logname(KINFO *k, VARENT *ve)
189 {
190 	VAR *v;
191 	char *s;
192 
193 	v = ve->var;
194 	(void)printf("%-*s", v->width, (s = k->ki_p->ki_login, *s) ? s : "-");
195 }
196 
197 void
198 state(KINFO *k, VARENT *ve)
199 {
200 	int flag, sflag, tdflags;
201 	char *cp;
202 	VAR *v;
203 	char buf[16];
204 
205 	v = ve->var;
206 	flag = k->ki_p->ki_flag;
207 	sflag = k->ki_p->ki_sflag;
208 	tdflags = k->ki_p->ki_tdflags;	/* XXXKSE */
209 	cp = buf;
210 
211 	switch (k->ki_p->ki_stat) {
212 
213 	case SSTOP:
214 		*cp = 'T';
215 		break;
216 
217 	case SSLEEP:
218 		if (tdflags & TDF_SINTR)	/* interruptable (long) */
219 			*cp = k->ki_p->ki_slptime >= MAXSLP ? 'I' : 'S';
220 		else
221 			*cp = 'D';
222 		break;
223 
224 	case SRUN:
225 	case SIDL:
226 		*cp = 'R';
227 		break;
228 
229 	case SWAIT:
230 		*cp = 'W';
231 		break;
232 
233 	case SLOCK:
234 		*cp = 'L';
235 		break;
236 
237 	case SZOMB:
238 		*cp = 'Z';
239 		break;
240 
241 	default:
242 		*cp = '?';
243 	}
244 	cp++;
245 	if (!(sflag & PS_INMEM))
246 		*cp++ = 'W';
247 	if (k->ki_p->ki_nice < NZERO)
248 		*cp++ = '<';
249 	else if (k->ki_p->ki_nice > NZERO)
250 		*cp++ = 'N';
251 	if (flag & P_TRACED)
252 		*cp++ = 'X';
253 	if (flag & P_WEXIT && k->ki_p->ki_stat != SZOMB)
254 		*cp++ = 'E';
255 	if (flag & P_PPWAIT)
256 		*cp++ = 'V';
257 	if ((flag & P_SYSTEM) || k->ki_p->ki_lock > 0)
258 		*cp++ = 'L';
259 	if (k->ki_p->ki_kiflag & KI_SLEADER)
260 		*cp++ = 's';
261 	if ((flag & P_CONTROLT) && k->ki_p->ki_pgid == k->ki_p->ki_tpgid)
262 		*cp++ = '+';
263 	if (flag & P_JAILED)
264 		*cp++ = 'J';
265 	*cp = '\0';
266 	(void)printf("%-*s", v->width, buf);
267 }
268 
269 #define	scalepri(x)	((x) - PZERO)
270 
271 void
272 pri(KINFO *k, VARENT *ve)
273 {
274 	VAR *v;
275 
276 	v = ve->var;
277 	(void)printf("%*d", v->width, scalepri(k->ki_p->ki_pri.pri_level));
278 }
279 
280 void
281 upr(KINFO *k, VARENT *ve)
282 {
283 	VAR *v;
284 
285 	v = ve->var;
286 	(void)printf("%*d", v->width, scalepri(k->ki_p->ki_pri.pri_user));
287 }
288 #undef scalepri
289 
290 void
291 uname(KINFO *k, VARENT *ve)
292 {
293 	VAR *v;
294 
295 	v = ve->var;
296 	(void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_uid, 0));
297 }
298 
299 int
300 s_uname(KINFO *k)
301 {
302 	return (strlen(user_from_uid(k->ki_p->ki_uid, 0)));
303 }
304 
305 void
306 rgroupname(KINFO *k, VARENT *ve)
307 {
308 	VAR *v;
309 
310 	v = ve->var;
311 	(void)printf("%-*s", v->width, group_from_gid(k->ki_p->ki_rgid, 0));
312 }
313 
314 int
315 s_rgroupname(KINFO *k)
316 {
317 	return (strlen(group_from_gid(k->ki_p->ki_rgid, 0)));
318 }
319 
320 void
321 runame(KINFO *k, VARENT *ve)
322 {
323 	VAR *v;
324 
325 	v = ve->var;
326 	(void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_ruid, 0));
327 }
328 
329 int
330 s_runame(KINFO *k)
331 {
332 	return (strlen(user_from_uid(k->ki_p->ki_ruid, 0)));
333 }
334 
335 
336 void
337 tdev(KINFO *k, VARENT *ve)
338 {
339 	VAR *v;
340 	dev_t dev;
341 	char buff[16];
342 
343 	v = ve->var;
344 	dev = k->ki_p->ki_tdev;
345 	if (dev == NODEV)
346 		(void)printf("%*s", v->width, "??");
347 	else {
348 		(void)snprintf(buff, sizeof(buff),
349 		    "%d/%d", major(dev), minor(dev));
350 		(void)printf("%*s", v->width, buff);
351 	}
352 }
353 
354 void
355 tname(KINFO *k, VARENT *ve)
356 {
357 	VAR *v;
358 	dev_t dev;
359 	char *ttname;
360 
361 	v = ve->var;
362 	dev = k->ki_p->ki_tdev;
363 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
364 		(void)printf("%*s ", v->width - 1, "??");
365 	else {
366 		if (strncmp(ttname, "tty", 3) == 0 ||
367 		    strncmp(ttname, "cua", 3) == 0)
368 			ttname += 3;
369 		if (strncmp(ttname, "pts/", 4) == 0)
370 			ttname += 4;
371 		(void)printf("%*.*s%c", v->width - 1, v->width - 1, ttname,
372 		    k->ki_p->ki_kiflag & KI_CTTY ? ' ' : '-');
373 	}
374 }
375 
376 void
377 longtname(KINFO *k, VARENT *ve)
378 {
379 	VAR *v;
380 	dev_t dev;
381 	char *ttname;
382 
383 	v = ve->var;
384 	dev = k->ki_p->ki_tdev;
385 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
386 		(void)printf("%-*s", v->width, "??");
387 	else
388 		(void)printf("%-*s", v->width, ttname);
389 }
390 
391 void
392 started(KINFO *k, VARENT *ve)
393 {
394 	VAR *v;
395 	time_t then;
396 	struct tm *tp;
397 	static int use_ampm = -1;
398 	char buf[100];
399 
400 	v = ve->var;
401 	if (!k->ki_valid) {
402 		(void)printf("%-*s", v->width, "-");
403 		return;
404 	}
405 	if (use_ampm < 0)
406 		use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
407 	then = k->ki_p->ki_start.tv_sec;
408 	tp = localtime(&then);
409 	if (now - k->ki_p->ki_start.tv_sec < 24 * 3600) {
410 		(void)strftime(buf, sizeof(buf),
411 		    use_ampm ? "%l:%M%p" : "%k:%M  ", tp);
412 	} else if (now - k->ki_p->ki_start.tv_sec < 7 * 86400) {
413 		(void)strftime(buf, sizeof(buf),
414 		    use_ampm ? "%a%I%p" : "%a%H  ", tp);
415 	} else
416 		(void)strftime(buf, sizeof(buf), "%e%b%y", tp);
417 	(void)printf("%-*s", v->width, buf);
418 }
419 
420 void
421 lstarted(KINFO *k, VARENT *ve)
422 {
423 	VAR *v;
424 	time_t then;
425 	char buf[100];
426 
427 	v = ve->var;
428 	if (!k->ki_valid) {
429 		(void)printf("%-*s", v->width, "-");
430 		return;
431 	}
432 	then = k->ki_p->ki_start.tv_sec;
433 	(void)strftime(buf, sizeof(buf), "%c", localtime(&then));
434 	(void)printf("%-*s", v->width, buf);
435 }
436 
437 void
438 lockname(KINFO *k, VARENT *ve)
439 {
440 	VAR *v;
441 
442 	v = ve->var;
443 	if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
444 		if (k->ki_p->ki_lockname[0] != 0)
445 			(void)printf("%-*.*s", v->width, v->width,
446 			    k->ki_p->ki_lockname);
447 		else
448 			(void)printf("%-*s", v->width, "???");
449 	} else
450 		(void)printf("%-*s", v->width, "-");
451 }
452 
453 void
454 wchan(KINFO *k, VARENT *ve)
455 {
456 	VAR *v;
457 
458 	v = ve->var;
459 	if (k->ki_p->ki_wchan) {
460 		if (k->ki_p->ki_wmesg[0] != 0)
461 			(void)printf("%-*.*s", v->width, v->width,
462 			    k->ki_p->ki_wmesg);
463 		else
464 			(void)printf("%-*lx", v->width,
465 			    (long)k->ki_p->ki_wchan);
466 	} else
467 		(void)printf("%-*s", v->width, "-");
468 }
469 
470 void
471 nwchan(KINFO *k, VARENT *ve)
472 {
473 	VAR *v;
474 
475 	v = ve->var;
476 	if (k->ki_p->ki_wchan) {
477 		(void)printf("%0*lx", v->width,
478 		    (long)k->ki_p->ki_wchan);
479 	} else
480 		(void)printf("%-*s", v->width, "-");
481 }
482 
483 void
484 mwchan(KINFO *k, VARENT *ve)
485 {
486 	VAR *v;
487 
488 	v = ve->var;
489 	if (k->ki_p->ki_wchan) {
490 		if (k->ki_p->ki_wmesg[0] != 0)
491 			(void)printf("%-*.*s", v->width, v->width,
492 			    k->ki_p->ki_wmesg);
493 		else
494 			(void)printf("%-*lx", v->width,
495 			    (long)k->ki_p->ki_wchan);
496 	} else if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
497 		if (k->ki_p->ki_lockname[0]) {
498 			(void)printf("%-*.*s", v->width, v->width,
499 			    k->ki_p->ki_lockname);
500 		} else
501 			(void)printf("%-*s", v->width, "???");
502 	} else
503 		(void)printf("%-*s", v->width, "-");
504 }
505 
506 void
507 vsize(KINFO *k, VARENT *ve)
508 {
509 	VAR *v;
510 
511 	v = ve->var;
512 	(void)printf("%*lu", v->width, (u_long)(k->ki_p->ki_size / 1024));
513 }
514 
515 void
516 cputime(KINFO *k, VARENT *ve)
517 {
518 	VAR *v;
519 	long secs;
520 	long psecs;	/* "parts" of a second. first micro, then centi */
521 	char obuff[128];
522 	static char decimal_point;
523 
524 	if (decimal_point == '\0')
525 		decimal_point = localeconv()->decimal_point[0];
526 	v = ve->var;
527 	if (!k->ki_valid) {
528 		secs = 0;
529 		psecs = 0;
530 	} else {
531 		/*
532 		 * This counts time spent handling interrupts.  We could
533 		 * fix this, but it is not 100% trivial (and interrupt
534 		 * time fractions only work on the sparc anyway).	XXX
535 		 */
536 		secs = k->ki_p->ki_runtime / 1000000;
537 		psecs = k->ki_p->ki_runtime % 1000000;
538 		if (sumrusage) {
539 			secs += k->ki_p->ki_childtime.tv_sec;
540 			psecs += k->ki_p->ki_childtime.tv_usec;
541 		}
542 		/*
543 		 * round and scale to 100's
544 		 */
545 		psecs = (psecs + 5000) / 10000;
546 		secs += psecs / 100;
547 		psecs = psecs % 100;
548 	}
549 	(void)snprintf(obuff, sizeof(obuff), "%3ld:%02ld%c%02ld",
550 	    secs / 60, secs % 60, decimal_point, psecs);
551 	(void)printf("%*s", v->width, obuff);
552 }
553 
554 void
555 elapsed(KINFO *k, VARENT *ve)
556 {
557 	VAR *v;
558 	time_t val;
559 	int days, hours, mins, secs;
560 	char obuff[128];
561 
562 	v = ve->var;
563 	val = now - k->ki_p->ki_start.tv_sec;
564 	days = val / (24 * 60 * 60);
565 	val %= 24 * 60 * 60;
566 	hours = val / (60 * 60);
567 	val %= 60 * 60;
568 	mins = val / 60;
569 	secs = val % 60;
570 	if (days != 0)
571 		(void)snprintf(obuff, sizeof(obuff), "%3d-%02d:%02d:%02d",
572 		    days, hours, mins, secs);
573 	else if (hours != 0)
574 		(void)snprintf(obuff, sizeof(obuff), "%02d:%02d:%02d",
575 		    hours, mins, secs);
576 	else
577 		(void)snprintf(obuff, sizeof(obuff), "%02d:%02d", mins, secs);
578 	(void)printf("%*s", v->width, obuff);
579 }
580 
581 double
582 getpcpu(const KINFO *k)
583 {
584 	static int failure;
585 
586 	if (!nlistread)
587 		failure = donlist();
588 	if (failure)
589 		return (0.0);
590 
591 #define	fxtofl(fixpt)	((double)(fixpt) / fscale)
592 
593 	/* XXX - I don't like this */
594 	if (k->ki_p->ki_swtime == 0 || (k->ki_p->ki_sflag & PS_INMEM) == 0)
595 		return (0.0);
596 	if (rawcpu)
597 		return (100.0 * fxtofl(k->ki_p->ki_pctcpu));
598 	return (100.0 * fxtofl(k->ki_p->ki_pctcpu) /
599 		(1.0 - exp(k->ki_p->ki_swtime * log(fxtofl(ccpu)))));
600 }
601 
602 void
603 pcpu(KINFO *k, VARENT *ve)
604 {
605 	VAR *v;
606 
607 	v = ve->var;
608 	(void)printf("%*.1f", v->width, getpcpu(k));
609 }
610 
611 static double
612 getpmem(KINFO *k)
613 {
614 	static int failure;
615 	double fracmem;
616 
617 	if (!nlistread)
618 		failure = donlist();
619 	if (failure)
620 		return (0.0);
621 
622 	if ((k->ki_p->ki_sflag & PS_INMEM) == 0)
623 		return (0.0);
624 	/* XXX want pmap ptpages, segtab, etc. (per architecture) */
625 	/* XXX don't have info about shared */
626 	fracmem = ((float)k->ki_p->ki_rssize) / mempages;
627 	return (100.0 * fracmem);
628 }
629 
630 void
631 pmem(KINFO *k, VARENT *ve)
632 {
633 	VAR *v;
634 
635 	v = ve->var;
636 	(void)printf("%*.1f", v->width, getpmem(k));
637 }
638 
639 void
640 pagein(KINFO *k, VARENT *ve)
641 {
642 	VAR *v;
643 
644 	v = ve->var;
645 	(void)printf("%*ld", v->width,
646 	    k->ki_valid ? k->ki_p->ki_rusage.ru_majflt : 0);
647 }
648 
649 /* ARGSUSED */
650 void
651 maxrss(KINFO *k __unused, VARENT *ve)
652 {
653 	VAR *v;
654 
655 	v = ve->var;
656 	/* XXX not yet */
657 	(void)printf("%*s", v->width, "-");
658 }
659 
660 void
661 priorityr(KINFO *k, VARENT *ve)
662 {
663 	VAR *v;
664 	struct priority *lpri;
665 	char str[8];
666 	unsigned class, level;
667 
668 	v = ve->var;
669 	lpri = &k->ki_p->ki_pri;
670 	class = lpri->pri_class;
671 	level = lpri->pri_level;
672 	switch (class) {
673 	case PRI_ITHD:
674 		snprintf(str, sizeof(str), "intr:%u", level);
675 		break;
676 	case PRI_REALTIME:
677 		snprintf(str, sizeof(str), "real:%u", level);
678 		break;
679 	case PRI_TIMESHARE:
680 		strncpy(str, "normal", sizeof(str));
681 		break;
682 	case PRI_IDLE:
683 		snprintf(str, sizeof(str), "idle:%u", level);
684 		break;
685 	default:
686 		snprintf(str, sizeof(str), "%u:%u", class, level);
687 		break;
688 	}
689 	str[sizeof(str) - 1] = '\0';
690 	(void)printf("%*s", v->width, str);
691 }
692 
693 /*
694  * Generic output routines.  Print fields from various prototype
695  * structures.
696  */
697 static void
698 printval(void *bp, VAR *v)
699 {
700 	static char ofmt[32] = "%";
701 	const char *fcp;
702 	char *cp;
703 
704 	cp = ofmt + 1;
705 	fcp = v->fmt;
706 	if (v->flag & LJUST)
707 		*cp++ = '-';
708 	*cp++ = '*';
709 	while ((*cp++ = *fcp++));
710 
711 #define	CHKINF127(n)	(((n) > 127) && (v->flag & INF127) ? 127 : (n))
712 
713 	switch (v->type) {
714 	case CHAR:
715 		(void)printf(ofmt, v->width, *(char *)bp);
716 		break;
717 	case UCHAR:
718 		(void)printf(ofmt, v->width, *(u_char *)bp);
719 		break;
720 	case SHORT:
721 		(void)printf(ofmt, v->width, *(short *)bp);
722 		break;
723 	case USHORT:
724 		(void)printf(ofmt, v->width, *(u_short *)bp);
725 		break;
726 	case INT:
727 		(void)printf(ofmt, v->width, *(int *)bp);
728 		break;
729 	case UINT:
730 		(void)printf(ofmt, v->width, CHKINF127(*(u_int *)bp));
731 		break;
732 	case LONG:
733 		(void)printf(ofmt, v->width, *(long *)bp);
734 		break;
735 	case ULONG:
736 		(void)printf(ofmt, v->width, *(u_long *)bp);
737 		break;
738 	case KPTR:
739 		(void)printf(ofmt, v->width, *(u_long *)bp);
740 		break;
741 	case PGTOK:
742 		(void)printf(ofmt, v->width, ps_pgtok(*(u_long *)bp));
743 		break;
744 	default:
745 		errx(1, "unknown type %d", v->type);
746 	}
747 }
748 
749 void
750 kvar(KINFO *k, VARENT *ve)
751 {
752 	VAR *v;
753 
754 	v = ve->var;
755 	printval((char *)((char *)k->ki_p + v->off), v);
756 }
757 
758 void
759 rvar(KINFO *k, VARENT *ve)
760 {
761 	VAR *v;
762 
763 	v = ve->var;
764 	if (k->ki_valid)
765 		printval((char *)((char *)(&k->ki_p->ki_rusage) + v->off), v);
766 	else
767 		(void)printf("%*s", v->width, "-");
768 }
769 
770 void
771 emulname(KINFO *k, VARENT *ve)
772 {
773 	VAR *v;
774 
775 	v = ve->var;
776 	printf("%-*s", v->width, *k->ki_p->ki_emul ? k->ki_p->ki_emul : "-");
777 }
778 
779 void
780 label(KINFO *k, VARENT *ve)
781 {
782 	char *string;
783 	VAR *v;
784 	mac_t proclabel;
785 	int error;
786 
787 	v = ve->var;
788 	string = NULL;
789 	if (mac_prepare_process_label(&proclabel) == -1) {
790 		warn("mac_prepare_process_label");
791 		goto out;
792 	}
793 	error = mac_get_pid(k->ki_p->ki_pid, proclabel);
794 	if (error == 0) {
795 		if (mac_to_text(proclabel, &string) == -1)
796 			string = NULL;
797 	}
798 	mac_free(proclabel);
799 out:
800 	if (string != NULL) {
801 		(void)printf("%-*s", v->width, string);
802 		free(string);
803 	} else
804 		(void)printf("%-*s", v->width, "  -");
805 	return;
806 }
807 
808 int
809 s_label(KINFO *k)
810 {
811 	char *string = NULL;
812 	mac_t proclabel;
813 	int error, size = 0;
814 
815 	if (mac_prepare_process_label(&proclabel) == -1) {
816 		warn("mac_prepare_process_label");
817 		return (0);
818 	}
819 	error = mac_get_pid(k->ki_p->ki_pid, proclabel);
820 	if (error == 0 && mac_to_text(proclabel, &string) == 0) {
821 		size = strlen(string);
822 		free(string);
823 	}
824 	mac_free(proclabel);
825 	return (size);
826 }
827