xref: /freebsd/bin/ps/print.c (revision 2be1a816b9ff69588e55be0a84cbe2a31efc0f2f)
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 			if (showthreads && k->ki_p->ki_numthreads > 1)
135 				printf("/%s", k->ki_p->ki_ocomm);
136 		} else
137 			(void)printf("%-*s", v->width, k->ki_p->ki_comm);
138 		return;
139 	}
140 	if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
141 		errx(1, "malloc failed");
142 	strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
143 	if (k->ki_env) {
144 		if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
145 			errx(1, "malloc failed");
146 		strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
147 	} else
148 		vis_env = NULL;
149 
150 	if (STAILQ_NEXT(ve, next_ve) == NULL) {
151 		/* last field */
152 		if (termwidth == UNLIMITED) {
153 			if (vis_env)
154 				(void)printf("%s ", vis_env);
155 			(void)printf("%s", vis_args);
156 		} else {
157 			left = termwidth - (totwidth - v->width);
158 			if (left < 1) /* already wrapped, just use std width */
159 				left = v->width;
160 			if ((cp = vis_env) != NULL) {
161 				while (--left >= 0 && *cp)
162 					(void)putchar(*cp++);
163 				if (--left >= 0)
164 					putchar(' ');
165 			}
166 			for (cp = vis_args; --left >= 0 && *cp != '\0';)
167 				(void)putchar(*cp++);
168 		}
169 	} else
170 		/* XXX env? */
171 		(void)printf("%-*.*s", v->width, v->width, vis_args);
172 	free(vis_args);
173 	if (vis_env != NULL)
174 		free(vis_env);
175 }
176 
177 void
178 ucomm(KINFO *k, VARENT *ve)
179 {
180 	VAR *v;
181 
182 	v = ve->var;
183 	if (STAILQ_NEXT(ve, next_ve) == NULL) {	/* last field, don't pad */
184 		(void)printf("%s", k->ki_p->ki_comm);
185 		if (showthreads && k->ki_p->ki_numthreads > 1)
186 			printf("/%s", k->ki_p->ki_ocomm);
187 	} else
188 		(void)printf("%-*s", v->width, k->ki_p->ki_comm);
189 }
190 
191 void
192 tdnam(KINFO *k, VARENT *ve)
193 {
194 	VAR *v;
195 
196 	v = ve->var;
197 	if (showthreads && k->ki_p->ki_numthreads > 1)
198 		(void)printf("%-*s", v->width, k->ki_p->ki_ocomm);
199         else
200 		(void)printf("%-*s", v->width, "      " );
201 
202 }
203 
204 void
205 logname(KINFO *k, VARENT *ve)
206 {
207 	VAR *v;
208 	char *s;
209 
210 	v = ve->var;
211 	(void)printf("%-*s", v->width, (s = k->ki_p->ki_login, *s) ? s : "-");
212 }
213 
214 void
215 state(KINFO *k, VARENT *ve)
216 {
217 	int flag, tdflags;
218 	char *cp;
219 	VAR *v;
220 	char buf[16];
221 
222 	v = ve->var;
223 	flag = k->ki_p->ki_flag;
224 	tdflags = k->ki_p->ki_tdflags;	/* XXXKSE */
225 	cp = buf;
226 
227 	switch (k->ki_p->ki_stat) {
228 
229 	case SSTOP:
230 		*cp = 'T';
231 		break;
232 
233 	case SSLEEP:
234 		if (tdflags & TDF_SINTR)	/* interruptable (long) */
235 			*cp = k->ki_p->ki_slptime >= MAXSLP ? 'I' : 'S';
236 		else
237 			*cp = 'D';
238 		break;
239 
240 	case SRUN:
241 	case SIDL:
242 		*cp = 'R';
243 		break;
244 
245 	case SWAIT:
246 		*cp = 'W';
247 		break;
248 
249 	case SLOCK:
250 		*cp = 'L';
251 		break;
252 
253 	case SZOMB:
254 		*cp = 'Z';
255 		break;
256 
257 	default:
258 		*cp = '?';
259 	}
260 	cp++;
261 	if (!(flag & P_INMEM))
262 		*cp++ = 'W';
263 	if (k->ki_p->ki_nice < NZERO)
264 		*cp++ = '<';
265 	else if (k->ki_p->ki_nice > NZERO)
266 		*cp++ = 'N';
267 	if (flag & P_TRACED)
268 		*cp++ = 'X';
269 	if (flag & P_WEXIT && k->ki_p->ki_stat != SZOMB)
270 		*cp++ = 'E';
271 	if (flag & P_PPWAIT)
272 		*cp++ = 'V';
273 	if ((flag & P_SYSTEM) || k->ki_p->ki_lock > 0)
274 		*cp++ = 'L';
275 	if (k->ki_p->ki_kiflag & KI_SLEADER)
276 		*cp++ = 's';
277 	if ((flag & P_CONTROLT) && k->ki_p->ki_pgid == k->ki_p->ki_tpgid)
278 		*cp++ = '+';
279 	if (flag & P_JAILED)
280 		*cp++ = 'J';
281 	*cp = '\0';
282 	(void)printf("%-*s", v->width, buf);
283 }
284 
285 #define	scalepri(x)	((x) - PZERO)
286 
287 void
288 pri(KINFO *k, VARENT *ve)
289 {
290 	VAR *v;
291 
292 	v = ve->var;
293 	(void)printf("%*d", v->width, scalepri(k->ki_p->ki_pri.pri_level));
294 }
295 
296 void
297 upr(KINFO *k, VARENT *ve)
298 {
299 	VAR *v;
300 
301 	v = ve->var;
302 	(void)printf("%*d", v->width, scalepri(k->ki_p->ki_pri.pri_user));
303 }
304 #undef scalepri
305 
306 void
307 uname(KINFO *k, VARENT *ve)
308 {
309 	VAR *v;
310 
311 	v = ve->var;
312 	(void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_uid, 0));
313 }
314 
315 int
316 s_uname(KINFO *k)
317 {
318 	return (strlen(user_from_uid(k->ki_p->ki_uid, 0)));
319 }
320 
321 void
322 rgroupname(KINFO *k, VARENT *ve)
323 {
324 	VAR *v;
325 
326 	v = ve->var;
327 	(void)printf("%-*s", v->width, group_from_gid(k->ki_p->ki_rgid, 0));
328 }
329 
330 int
331 s_rgroupname(KINFO *k)
332 {
333 	return (strlen(group_from_gid(k->ki_p->ki_rgid, 0)));
334 }
335 
336 void
337 runame(KINFO *k, VARENT *ve)
338 {
339 	VAR *v;
340 
341 	v = ve->var;
342 	(void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_ruid, 0));
343 }
344 
345 int
346 s_runame(KINFO *k)
347 {
348 	return (strlen(user_from_uid(k->ki_p->ki_ruid, 0)));
349 }
350 
351 
352 void
353 tdev(KINFO *k, VARENT *ve)
354 {
355 	VAR *v;
356 	dev_t dev;
357 	char buff[16];
358 
359 	v = ve->var;
360 	dev = k->ki_p->ki_tdev;
361 	if (dev == NODEV)
362 		(void)printf("%*s", v->width, "??");
363 	else {
364 		(void)snprintf(buff, sizeof(buff),
365 		    "%d/%d", major(dev), minor(dev));
366 		(void)printf("%*s", v->width, buff);
367 	}
368 }
369 
370 void
371 tname(KINFO *k, VARENT *ve)
372 {
373 	VAR *v;
374 	dev_t dev;
375 	char *ttname;
376 
377 	v = ve->var;
378 	dev = k->ki_p->ki_tdev;
379 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
380 		(void)printf("%*s ", v->width - 1, "??");
381 	else {
382 		if (strncmp(ttname, "tty", 3) == 0 ||
383 		    strncmp(ttname, "cua", 3) == 0)
384 			ttname += 3;
385 		if (strncmp(ttname, "pts/", 4) == 0)
386 			ttname += 4;
387 		(void)printf("%*.*s%c", v->width - 1, v->width - 1, ttname,
388 		    k->ki_p->ki_kiflag & KI_CTTY ? ' ' : '-');
389 	}
390 }
391 
392 void
393 longtname(KINFO *k, VARENT *ve)
394 {
395 	VAR *v;
396 	dev_t dev;
397 	char *ttname;
398 
399 	v = ve->var;
400 	dev = k->ki_p->ki_tdev;
401 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
402 		(void)printf("%-*s", v->width, "??");
403 	else
404 		(void)printf("%-*s", v->width, ttname);
405 }
406 
407 void
408 started(KINFO *k, VARENT *ve)
409 {
410 	VAR *v;
411 	time_t then;
412 	struct tm *tp;
413 	static int use_ampm = -1;
414 	char buf[100];
415 
416 	v = ve->var;
417 	if (!k->ki_valid) {
418 		(void)printf("%-*s", v->width, "-");
419 		return;
420 	}
421 	if (use_ampm < 0)
422 		use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
423 	then = k->ki_p->ki_start.tv_sec;
424 	tp = localtime(&then);
425 	if (now - k->ki_p->ki_start.tv_sec < 24 * 3600) {
426 		(void)strftime(buf, sizeof(buf),
427 		    use_ampm ? "%l:%M%p" : "%k:%M  ", tp);
428 	} else if (now - k->ki_p->ki_start.tv_sec < 7 * 86400) {
429 		(void)strftime(buf, sizeof(buf),
430 		    use_ampm ? "%a%I%p" : "%a%H  ", tp);
431 	} else
432 		(void)strftime(buf, sizeof(buf), "%e%b%y", tp);
433 	(void)printf("%-*s", v->width, buf);
434 }
435 
436 void
437 lstarted(KINFO *k, VARENT *ve)
438 {
439 	VAR *v;
440 	time_t then;
441 	char buf[100];
442 
443 	v = ve->var;
444 	if (!k->ki_valid) {
445 		(void)printf("%-*s", v->width, "-");
446 		return;
447 	}
448 	then = k->ki_p->ki_start.tv_sec;
449 	(void)strftime(buf, sizeof(buf), "%c", localtime(&then));
450 	(void)printf("%-*s", v->width, buf);
451 }
452 
453 void
454 lockname(KINFO *k, VARENT *ve)
455 {
456 	VAR *v;
457 
458 	v = ve->var;
459 	if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
460 		if (k->ki_p->ki_lockname[0] != 0)
461 			(void)printf("%-*.*s", v->width, v->width,
462 			    k->ki_p->ki_lockname);
463 		else
464 			(void)printf("%-*s", v->width, "???");
465 	} else
466 		(void)printf("%-*s", v->width, "-");
467 }
468 
469 void
470 wchan(KINFO *k, VARENT *ve)
471 {
472 	VAR *v;
473 
474 	v = ve->var;
475 	if (k->ki_p->ki_wchan) {
476 		if (k->ki_p->ki_wmesg[0] != 0)
477 			(void)printf("%-*.*s", v->width, v->width,
478 			    k->ki_p->ki_wmesg);
479 		else
480 			(void)printf("%-*lx", v->width,
481 			    (long)k->ki_p->ki_wchan);
482 	} else
483 		(void)printf("%-*s", v->width, "-");
484 }
485 
486 void
487 nwchan(KINFO *k, VARENT *ve)
488 {
489 	VAR *v;
490 
491 	v = ve->var;
492 	if (k->ki_p->ki_wchan) {
493 		(void)printf("%0*lx", v->width,
494 		    (long)k->ki_p->ki_wchan);
495 	} else
496 		(void)printf("%-*s", v->width, "-");
497 }
498 
499 void
500 mwchan(KINFO *k, VARENT *ve)
501 {
502 	VAR *v;
503 
504 	v = ve->var;
505 	if (k->ki_p->ki_wchan) {
506 		if (k->ki_p->ki_wmesg[0] != 0)
507 			(void)printf("%-*.*s", v->width, v->width,
508 			    k->ki_p->ki_wmesg);
509 		else
510 			(void)printf("%-*lx", v->width,
511 			    (long)k->ki_p->ki_wchan);
512 	} else if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
513 		if (k->ki_p->ki_lockname[0]) {
514 			(void)printf("%-*.*s", v->width, v->width,
515 			    k->ki_p->ki_lockname);
516 		} else
517 			(void)printf("%-*s", v->width, "???");
518 	} else
519 		(void)printf("%-*s", v->width, "-");
520 }
521 
522 void
523 vsize(KINFO *k, VARENT *ve)
524 {
525 	VAR *v;
526 
527 	v = ve->var;
528 	(void)printf("%*lu", v->width, (u_long)(k->ki_p->ki_size / 1024));
529 }
530 
531 void
532 cputime(KINFO *k, VARENT *ve)
533 {
534 	VAR *v;
535 	long secs;
536 	long psecs;	/* "parts" of a second. first micro, then centi */
537 	char obuff[128];
538 	static char decimal_point;
539 
540 	if (decimal_point == '\0')
541 		decimal_point = localeconv()->decimal_point[0];
542 	v = ve->var;
543 	if (!k->ki_valid) {
544 		secs = 0;
545 		psecs = 0;
546 	} else {
547 		/*
548 		 * This counts time spent handling interrupts.  We could
549 		 * fix this, but it is not 100% trivial (and interrupt
550 		 * time fractions only work on the sparc anyway).	XXX
551 		 */
552 		secs = k->ki_p->ki_runtime / 1000000;
553 		psecs = k->ki_p->ki_runtime % 1000000;
554 		if (sumrusage) {
555 			secs += k->ki_p->ki_childtime.tv_sec;
556 			psecs += k->ki_p->ki_childtime.tv_usec;
557 		}
558 		/*
559 		 * round and scale to 100's
560 		 */
561 		psecs = (psecs + 5000) / 10000;
562 		secs += psecs / 100;
563 		psecs = psecs % 100;
564 	}
565 	(void)snprintf(obuff, sizeof(obuff), "%3ld:%02ld%c%02ld",
566 	    secs / 60, secs % 60, decimal_point, psecs);
567 	(void)printf("%*s", v->width, obuff);
568 }
569 
570 void
571 elapsed(KINFO *k, VARENT *ve)
572 {
573 	VAR *v;
574 	time_t val;
575 	int days, hours, mins, secs;
576 	char obuff[128];
577 
578 	v = ve->var;
579 	val = now - k->ki_p->ki_start.tv_sec;
580 	days = val / (24 * 60 * 60);
581 	val %= 24 * 60 * 60;
582 	hours = val / (60 * 60);
583 	val %= 60 * 60;
584 	mins = val / 60;
585 	secs = val % 60;
586 	if (days != 0)
587 		(void)snprintf(obuff, sizeof(obuff), "%3d-%02d:%02d:%02d",
588 		    days, hours, mins, secs);
589 	else if (hours != 0)
590 		(void)snprintf(obuff, sizeof(obuff), "%02d:%02d:%02d",
591 		    hours, mins, secs);
592 	else
593 		(void)snprintf(obuff, sizeof(obuff), "%02d:%02d", mins, secs);
594 	(void)printf("%*s", v->width, obuff);
595 }
596 
597 double
598 getpcpu(const KINFO *k)
599 {
600 	static int failure;
601 
602 	if (!nlistread)
603 		failure = donlist();
604 	if (failure)
605 		return (0.0);
606 
607 #define	fxtofl(fixpt)	((double)(fixpt) / fscale)
608 
609 	/* XXX - I don't like this */
610 	if (k->ki_p->ki_swtime == 0 || (k->ki_p->ki_flag & P_INMEM) == 0)
611 		return (0.0);
612 	if (rawcpu)
613 		return (100.0 * fxtofl(k->ki_p->ki_pctcpu));
614 	return (100.0 * fxtofl(k->ki_p->ki_pctcpu) /
615 		(1.0 - exp(k->ki_p->ki_swtime * log(fxtofl(ccpu)))));
616 }
617 
618 void
619 pcpu(KINFO *k, VARENT *ve)
620 {
621 	VAR *v;
622 
623 	v = ve->var;
624 	(void)printf("%*.1f", v->width, getpcpu(k));
625 }
626 
627 static double
628 getpmem(KINFO *k)
629 {
630 	static int failure;
631 	double fracmem;
632 
633 	if (!nlistread)
634 		failure = donlist();
635 	if (failure)
636 		return (0.0);
637 
638 	if ((k->ki_p->ki_flag & P_INMEM) == 0)
639 		return (0.0);
640 	/* XXX want pmap ptpages, segtab, etc. (per architecture) */
641 	/* XXX don't have info about shared */
642 	fracmem = ((float)k->ki_p->ki_rssize) / mempages;
643 	return (100.0 * fracmem);
644 }
645 
646 void
647 pmem(KINFO *k, VARENT *ve)
648 {
649 	VAR *v;
650 
651 	v = ve->var;
652 	(void)printf("%*.1f", v->width, getpmem(k));
653 }
654 
655 void
656 pagein(KINFO *k, VARENT *ve)
657 {
658 	VAR *v;
659 
660 	v = ve->var;
661 	(void)printf("%*ld", v->width,
662 	    k->ki_valid ? k->ki_p->ki_rusage.ru_majflt : 0);
663 }
664 
665 /* ARGSUSED */
666 void
667 maxrss(KINFO *k __unused, VARENT *ve)
668 {
669 	VAR *v;
670 
671 	v = ve->var;
672 	/* XXX not yet */
673 	(void)printf("%*s", v->width, "-");
674 }
675 
676 void
677 priorityr(KINFO *k, VARENT *ve)
678 {
679 	VAR *v;
680 	struct priority *lpri;
681 	char str[8];
682 	unsigned class, level;
683 
684 	v = ve->var;
685 	lpri = &k->ki_p->ki_pri;
686 	class = lpri->pri_class;
687 	level = lpri->pri_level;
688 	switch (class) {
689 	case PRI_ITHD:
690 		snprintf(str, sizeof(str), "intr:%u", level);
691 		break;
692 	case PRI_REALTIME:
693 		snprintf(str, sizeof(str), "real:%u", level);
694 		break;
695 	case PRI_TIMESHARE:
696 		strncpy(str, "normal", sizeof(str));
697 		break;
698 	case PRI_IDLE:
699 		snprintf(str, sizeof(str), "idle:%u", level);
700 		break;
701 	default:
702 		snprintf(str, sizeof(str), "%u:%u", class, level);
703 		break;
704 	}
705 	str[sizeof(str) - 1] = '\0';
706 	(void)printf("%*s", v->width, str);
707 }
708 
709 /*
710  * Generic output routines.  Print fields from various prototype
711  * structures.
712  */
713 static void
714 printval(void *bp, VAR *v)
715 {
716 	static char ofmt[32] = "%";
717 	const char *fcp;
718 	char *cp;
719 
720 	cp = ofmt + 1;
721 	fcp = v->fmt;
722 	if (v->flag & LJUST)
723 		*cp++ = '-';
724 	*cp++ = '*';
725 	while ((*cp++ = *fcp++));
726 
727 #define	CHKINF127(n)	(((n) > 127) && (v->flag & INF127) ? 127 : (n))
728 
729 	switch (v->type) {
730 	case CHAR:
731 		(void)printf(ofmt, v->width, *(char *)bp);
732 		break;
733 	case UCHAR:
734 		(void)printf(ofmt, v->width, *(u_char *)bp);
735 		break;
736 	case SHORT:
737 		(void)printf(ofmt, v->width, *(short *)bp);
738 		break;
739 	case USHORT:
740 		(void)printf(ofmt, v->width, *(u_short *)bp);
741 		break;
742 	case INT:
743 		(void)printf(ofmt, v->width, *(int *)bp);
744 		break;
745 	case UINT:
746 		(void)printf(ofmt, v->width, CHKINF127(*(u_int *)bp));
747 		break;
748 	case LONG:
749 		(void)printf(ofmt, v->width, *(long *)bp);
750 		break;
751 	case ULONG:
752 		(void)printf(ofmt, v->width, *(u_long *)bp);
753 		break;
754 	case KPTR:
755 		(void)printf(ofmt, v->width, *(u_long *)bp);
756 		break;
757 	case PGTOK:
758 		(void)printf(ofmt, v->width, ps_pgtok(*(u_long *)bp));
759 		break;
760 	default:
761 		errx(1, "unknown type %d", v->type);
762 	}
763 }
764 
765 void
766 kvar(KINFO *k, VARENT *ve)
767 {
768 	VAR *v;
769 
770 	v = ve->var;
771 	printval((char *)((char *)k->ki_p + v->off), v);
772 }
773 
774 void
775 rvar(KINFO *k, VARENT *ve)
776 {
777 	VAR *v;
778 
779 	v = ve->var;
780 	if (k->ki_valid)
781 		printval((char *)((char *)(&k->ki_p->ki_rusage) + v->off), v);
782 	else
783 		(void)printf("%*s", v->width, "-");
784 }
785 
786 void
787 emulname(KINFO *k, VARENT *ve)
788 {
789 	VAR *v;
790 
791 	v = ve->var;
792 	printf("%-*s", v->width, *k->ki_p->ki_emul ? k->ki_p->ki_emul : "-");
793 }
794 
795 void
796 label(KINFO *k, VARENT *ve)
797 {
798 	char *string;
799 	VAR *v;
800 	mac_t proclabel;
801 	int error;
802 
803 	v = ve->var;
804 	string = NULL;
805 	if (mac_prepare_process_label(&proclabel) == -1) {
806 		warn("mac_prepare_process_label");
807 		goto out;
808 	}
809 	error = mac_get_pid(k->ki_p->ki_pid, proclabel);
810 	if (error == 0) {
811 		if (mac_to_text(proclabel, &string) == -1)
812 			string = NULL;
813 	}
814 	mac_free(proclabel);
815 out:
816 	if (string != NULL) {
817 		(void)printf("%-*s", v->width, string);
818 		free(string);
819 	} else
820 		(void)printf("%-*s", v->width, "  -");
821 	return;
822 }
823 
824 int
825 s_label(KINFO *k)
826 {
827 	char *string = NULL;
828 	mac_t proclabel;
829 	int error, size = 0;
830 
831 	if (mac_prepare_process_label(&proclabel) == -1) {
832 		warn("mac_prepare_process_label");
833 		return (0);
834 	}
835 	error = mac_get_pid(k->ki_p->ki_pid, proclabel);
836 	if (error == 0 && mac_to_text(proclabel, &string) == 0) {
837 		size = strlen(string);
838 		free(string);
839 	}
840 	mac_free(proclabel);
841 	return (size);
842 }
843