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