xref: /dragonfly/bin/ps/print.c (revision e98bdfd3)
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  * 3. 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  * @(#)print.c	8.6 (Berkeley) 4/16/94
30  * $FreeBSD: src/bin/ps/print.c,v 1.36.2.4 2002/11/30 13:00:14 tjr Exp $
31  * $DragonFly: src/bin/ps/print.c,v 1.34 2008/11/10 14:56:33 swildner Exp $
32  */
33 
34 #include <sys/user.h>
35 #include <sys/param.h>
36 #include <sys/time.h>
37 #include <sys/resource.h>
38 #include <sys/stat.h>
39 
40 #include <sys/ucred.h>
41 #include <sys/sysctl.h>
42 #include <sys/rtprio.h>
43 #include <vm/vm.h>
44 
45 #include <err.h>
46 #include <langinfo.h>
47 #include <locale.h>
48 #include <math.h>
49 #include <nlist.h>
50 #include <pwd.h>
51 #include <stddef.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <unistd.h>
55 #include <string.h>
56 #include <vis.h>
57 
58 #include "ps.h"
59 
60 static const char *make_printable(const char *str);
61 static const char *make_printable2(const char *str);
62 static void put64(u_int64_t n, int w, int type);
63 
64 #define SHOW_THRNAME(k) \
65 	(showtid && KI_PROC(k, pid) != -1 && KI_PROC(k, nthreads) > 1)
66 
67 void
68 printheader(void)
69 {
70 	const VAR *v;
71 	struct varent *vent;
72 	int allempty;
73 
74 	allempty = 1;
75 	STAILQ_FOREACH(vent, &var_head, link) {
76 		if (*vent->header != '\0') {
77 			allempty = 0;
78 			break;
79 		}
80 	}
81 	if (allempty)
82 		return;
83 	STAILQ_FOREACH(vent, &var_head, link) {
84 		v = vent->var;
85 		if (v->flag & LJUST) {
86 			if (STAILQ_NEXT(vent, link) == NULL)	/* last one */
87 				printf("%s", vent->header);
88 			else
89 				printf("%-*s", vent->width, vent->header);
90 		} else
91 			printf("%*s", vent->width, vent->header);
92 		if (STAILQ_NEXT(vent, link) != NULL)
93 			putchar(' ');
94 	}
95 	putchar('\n');
96 }
97 
98 void
99 command(const KINFO *k, const struct varent *vent)
100 {
101 	int left;
102 	int indent;
103 	char *cp, *vis_env, *vis_args;
104 
105 	if (cflag) {
106 		/* Don't pad the last field. */
107 		if (STAILQ_NEXT(vent, link) == NULL) {
108 			if (SHOW_THRNAME(k)) {
109 				printf("%s/%s",
110 				    make_printable(KI_PROC(k, comm)),
111 				    make_printable2(KI_LWP(k, comm)));
112 			} else {
113 				printf("%s", make_printable(KI_PROC(k, comm)));
114 			}
115 		} else {
116 			if (SHOW_THRNAME(k)) {
117 				printf("%-*s/%s", vent->width,
118 				    make_printable(KI_PROC(k, comm)),
119 				    make_printable2(KI_LWP(k, comm)));
120 			} else {
121 				printf("%-*s", vent->width,
122 					make_printable(KI_PROC(k, comm)));
123 			}
124 		}
125 		return;
126 	}
127 
128 	if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
129 		err(1, NULL);
130 	strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
131 	if (k->ki_env) {
132 		if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
133 			err(1, NULL);
134 		strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
135 	} else {
136 		vis_env = NULL;
137 	}
138 
139 	indent = k->ki_indent;
140 	if (indent < 0)
141 		indent = 0;
142 
143 	if (STAILQ_NEXT(vent, link) == NULL) {
144 		/* last field */
145 		if (termwidth == UNLIMITED) {
146 			if (vis_env)
147 				printf("%s ", vis_env);
148 			while (indent) {
149 				putchar(' ');
150 				--indent;
151 			}
152 			printf("%s", vis_args);
153 		} else {
154 			left = termwidth - (totwidth - vent->width);
155 			if (left < 1) /* already wrapped, just use std width */
156 				left = vent->width;
157 			while (indent && left > 1) {
158 				putchar(' ');
159 				--indent;
160 				--left;
161 			}
162 			if ((cp = vis_env) != NULL) {
163 				while (--left >= 0 && *cp)
164 					putchar(*cp++);
165 				if (--left >= 0)
166 					putchar(' ');
167 			}
168 			for (cp = vis_args; --left >= 0 && *cp != '\0';)
169 				putchar(*cp++);
170 		}
171 	} else
172 		/* XXX env? */
173 		printf("%-*.*s", vent->width, vent->width, vis_args);
174 	free(vis_args);
175 	if (vis_env != NULL)
176 		free(vis_env);
177 }
178 
179 void
180 ucomm(const KINFO *k, const struct varent *vent)
181 {
182 	/* Do not pad the last field */
183 	if (STAILQ_NEXT(vent, link) == NULL) {
184 		if (SHOW_THRNAME(k)) {
185 			printf("%s/%s", make_printable(KI_PROC(k, comm)),
186 			    make_printable2(KI_LWP(k, comm)));
187 		} else {
188 			printf("%s", make_printable(KI_PROC(k, comm)));
189 		}
190 	} else {
191 		if (SHOW_THRNAME(k)) {
192 			printf("%-*s/%s", vent->width,
193 			    make_printable(KI_PROC(k, comm)),
194 			    make_printable2(KI_LWP(k, comm)));
195 		} else {
196 			printf("%-*s", vent->width,
197 			    make_printable(KI_PROC(k, comm)));
198 		}
199 	}
200 }
201 
202 void
203 logname(const KINFO *k, const struct varent *vent)
204 {
205 	const char *s = KI_PROC(k, login);
206 
207 	printf("%-*s", vent->width, *s != '\0' ? s : "-");
208 }
209 
210 void
211 state(const KINFO *k, const struct varent *vent)
212 {
213 	int flag;
214 	char *cp;
215 	char buf[16];
216 
217 	flag = KI_PROC(k, flags);
218 	cp = buf;
219 
220 	switch (KI_PROC(k, stat)) {
221 
222 	case SSTOP:
223 		*cp = 'T';
224 		break;
225 
226 	case SACTIVE:
227 		switch (KI_LWP(k, stat)) {
228 		case LSSLEEP:
229 			if (KI_LWP(k, flags) & LWP_SINTR) {
230 				/* interruptable wait short/long */
231 				*cp = KI_LWP(k, slptime) >= MAXSLP ? 'I' : 'S';
232 			}
233 			else if (KI_LWP(k, tdflags) & TDF_SINTR)
234 				*cp = 'S';	/* interruptable lwkt wait */
235 			else if (KI_PROC(k, paddr))
236 				*cp = 'D';	/* uninterruptable wait */
237 			else
238 				*cp = 'B';	/* uninterruptable lwkt wait */
239 			/*break;*/
240 
241 		case LSRUN:
242 			if (KI_LWP(k, stat) == LSRUN) {
243 				*cp = 'R';
244 				if (!(KI_LWP(k, tdflags) &
245 				    (TDF_RUNNING | TDF_RUNQ)))
246 					*++cp = 'Q';
247 			}
248 			/*if (KI_LWP(k, tdflags) & (TDF_RUNNING | TDF_RUNQ))*/ {
249 			    ++cp;
250 			    sprintf(cp, "%d", KI_LWP(k, cpuid));
251 			    while (cp[1])
252 				++cp;
253 			}
254 			break;
255 
256 		case LSSTOP:
257 			/* shouldn't happen anyways */
258 			*cp = 'T';
259 			break;
260 		}
261 		break;
262 
263 	case SZOMB:
264 		*cp = 'Z';
265 		break;
266 
267 	default:
268 		*cp = '?';
269 	}
270 
271 	cp++;
272 	if (flag & P_SWAPPEDOUT)
273 		*cp++ = 'W';
274 	if (KI_PROC(k, nice) < NZERO)
275 		*cp++ = '<';
276 	else if (KI_PROC(k, nice) > NZERO)
277 		*cp++ = 'N';
278 	if (flag & P_TRACED)
279 		*cp++ = 'X';
280 	if (flag & P_WEXIT && KI_PROC(k, stat) != SZOMB)
281 		*cp++ = 'E';
282 	if (flag & P_PPWAIT)
283 		*cp++ = 'V';
284 	if ((flag & P_SYSTEM) || KI_PROC(k, lock) > 0)
285 		*cp++ = 'L';
286 	if (flag & P_JAILED)
287 		*cp++ = 'J';
288 	if (KI_PROC(k, auxflags) & KI_SLEADER)
289 		*cp++ = 's';
290 	if ((flag & P_CONTROLT) && KI_PROC(k, pgid) == KI_PROC(k, tpgid))
291 		*cp++ = '+';
292 	*cp = '\0';
293 	printf("%-*s", vent->width, buf);
294 }
295 
296 /*
297  * Normalized priority (lower is better).  For pure threads
298  * output a negated LWKT priority (so lower still means better).
299  *
300  * XXX bsd4 scheduler specific.
301  */
302 void
303 pri(const KINFO *k, const struct varent *vent)
304 {
305 	if (KI_LWP(k, pid) != -1)
306 	    printf("%*d", vent->width, KI_LWP(k, prio));
307 	else
308 	    printf("%*d", vent->width, -(KI_LWP(k, tdprio)));
309 }
310 
311 void
312 tdpri(const KINFO *k, const struct varent *vent)
313 {
314 	char buf[32];
315 	int val = KI_LWP(k, tdprio);
316 
317 	snprintf(buf, sizeof(buf), "%2d", val);
318 	printf("%*s", vent->width, buf);
319 }
320 
321 void
322 uname(const KINFO *k, const struct varent *vent)
323 {
324 	printf("%-*s", vent->width,
325 	       user_from_uid(KI_PROC(k, uid), 0));
326 }
327 
328 int
329 s_uname(const KINFO *k)
330 {
331 	return (strlen(user_from_uid(KI_PROC(k, uid), 0)));
332 }
333 
334 void
335 runame(const KINFO *k, const struct varent *vent)
336 {
337 	printf("%-*s", vent->width,
338 	       user_from_uid(KI_PROC(k, ruid), 0));
339 }
340 
341 int
342 s_runame(const KINFO *k)
343 {
344 	return (strlen(user_from_uid(KI_PROC(k, ruid), 0)));
345 }
346 
347 void
348 tdev(const KINFO *k, const struct varent *vent)
349 {
350 	dev_t dev;
351 	char buff[16];
352 
353 	dev = KI_PROC(k, tdev);
354 	if (dev == NODEV)
355 		printf("%*s", vent->width, "??");
356 	else {
357 		snprintf(buff, sizeof(buff), "%d/%d", major(dev), minor(dev));
358 		printf("%*s", vent->width, buff);
359 	}
360 }
361 
362 void
363 tname(const KINFO *k, const struct varent *vent)
364 {
365 	dev_t dev;
366 	const char *ttname;
367 
368 	dev = KI_PROC(k, tdev);
369 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
370 		printf("%*s ", vent->width-1, "??");
371 	else {
372 		if (strncmp(ttname, "tty", 3) == 0 ||
373 		    strncmp(ttname, "cua", 3) == 0)
374 			ttname += 3;
375 		if (strncmp(ttname, "pts/", 4) == 0)
376 			ttname += 4;
377 		printf("%*.*s%c", vent->width-1, vent->width-1, ttname,
378 			KI_PROC(k, auxflags) & KI_CTTY ? ' ' : '-');
379 	}
380 }
381 
382 void
383 longtname(const KINFO *k, const struct varent *vent)
384 {
385 	dev_t dev;
386 	const char *ttname;
387 
388 	dev = KI_PROC(k, tdev);
389 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
390 		printf("%-*s", vent->width, "??");
391 	else
392 		printf("%-*s", vent->width, ttname);
393 }
394 
395 void
396 started(const KINFO *k, const struct varent *vent)
397 {
398 	static time_t now;
399 	time_t then;
400 	struct tm *tp;
401 	char buf[100];
402 	static int  use_ampm = -1;
403 
404 	if (use_ampm < 0)
405 		use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
406 
407 	then = KI_PROC(k, start).tv_sec;
408 	if (then < btime.tv_sec) {
409 		then = btime.tv_sec;
410 	}
411 
412 	tp = localtime(&then);
413 	if (!now)
414 		time(&now);
415 	if (now - then < 24 * 3600) {
416 		strftime(buf, sizeof(buf) - 1,
417 		use_ampm ? "%l:%M%p" : "%k:%M  ", tp);
418 	} else if (now - then < 7 * 86400) {
419 		strftime(buf, sizeof(buf) - 1,
420 		use_ampm ? "%a%I%p" : "%a%H  ", tp);
421 	} else
422 		strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
423 	printf("%-*s", vent->width, buf);
424 }
425 
426 void
427 lstarted(const KINFO *k, const struct varent *vent)
428 {
429 	time_t then;
430 	char buf[100];
431 
432 	then = KI_PROC(k, start).tv_sec;
433 	strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
434 	printf("%-*s", vent->width, buf);
435 }
436 
437 void
438 wchan(const KINFO *k, const struct varent *vent)
439 {
440 	if (*KI_LWP(k, wmesg)) {
441 		printf("%-*.*s", vent->width, vent->width,
442 		       KI_LWP(k, wmesg));
443 	} else {
444 		printf("%-*s", vent->width, "-");
445 	}
446 }
447 
448 static u_int64_t
449 pgtob(u_int64_t pg)
450 {
451 	static size_t pgsize;
452 
453 	if (pgsize == 0)
454 		pgsize = getpagesize();
455 	return(pg * pgsize);
456 }
457 
458 void
459 vsize(const KINFO *k, const struct varent *vent)
460 {
461 	put64((uintmax_t)KI_PROC(k, vm_map_size)/1024, vent->width, 'k');
462 }
463 
464 void
465 rssize(const KINFO *k, const struct varent *vent)
466 {
467 	/* XXX don't have info about shared */
468 	put64(pgtob(KI_PROC(k, vm_rssize))/1024, vent->width, 'k');
469 }
470 
471 /* doesn't account for text */
472 void
473 p_rssize(const KINFO *k, const struct varent *vent)
474 {
475 	put64(pgtob(KI_PROC(k, vm_rssize))/1024, vent->width, 'k');
476 }
477 
478 void
479 cputime(const KINFO *k, const struct varent *vent)
480 {
481 	long secs;
482 	long psecs;	/* "parts" of a second. first micro, then centi */
483 	u_int64_t timeus;
484 	char obuff[128];
485 	static char decimal_point = '\0';
486 
487 	if (decimal_point == '\0')
488 		decimal_point = localeconv()->decimal_point[0];
489 
490 	/*
491 	 * This counts time spent handling interrupts.  We could
492 	 * fix this, but it is not 100% trivial (and interrupt
493 	 * time fractions only work on the sparc anyway).	XXX
494 	 */
495 	timeus = KI_LWP(k, uticks) + KI_LWP(k, sticks) +
496 		KI_LWP(k, iticks);
497 	secs = timeus / 1000000;
498 	psecs = timeus % 1000000;
499 	if (sumrusage) {
500 		secs += KI_PROC(k, cru).ru_utime.tv_sec +
501 			KI_PROC(k, cru).ru_stime.tv_sec;
502 		psecs += KI_PROC(k, cru).ru_utime.tv_usec +
503 			KI_PROC(k, cru).ru_stime.tv_usec;
504 	}
505 	/*
506 	 * round and scale to 100's
507 	 */
508 	psecs = (psecs + 5000) / 10000;
509 	secs += psecs / 100;
510 	psecs = psecs % 100;
511 #if 1
512 	if (secs >= 86400) {
513 		snprintf(obuff, sizeof(obuff), "%3ldd%02ld:%02ld",
514 			secs / 86400, secs / (60 * 60) % 24, secs / 60 % 60);
515 	} else if (secs >= 100 * 60) {
516 		snprintf(obuff, sizeof(obuff), "%2ld:%02ld:%02ld",
517 			secs / 60 / 60, secs / 60 % 60, secs % 60);
518 	} else
519 #endif
520 	{
521 		snprintf(obuff, sizeof(obuff), "%3ld:%02ld%c%02ld",
522 			 secs / 60, secs % 60,
523 			 decimal_point, psecs);
524 	}
525 	printf("%*s", vent->width, obuff);
526 }
527 
528 double
529 getpcpu(const KINFO *k)
530 {
531 	static int failure;
532 
533 	if (!nlistread)
534 		failure = donlist();
535 	if (failure)
536 		return (0.0);
537 
538 #define	fxtofl(fixpt)	((double)(fixpt) / fscale)
539 
540 	/* XXX - I don't like this */
541 	if (KI_PROC(k, swtime) == 0 || (KI_PROC(k, flags) & P_SWAPPEDOUT))
542 		return (0.0);
543 	return (100.0 * fxtofl(KI_LWP(k, pctcpu)));
544 }
545 
546 void
547 pcpu(const KINFO *k, const struct varent *vent)
548 {
549 	printf("%*.1f", vent->width, getpcpu(k));
550 }
551 
552 void
553 pnice(const KINFO *k, const struct varent *vent)
554 {
555 	int niceval;
556 
557 	switch (KI_LWP(k, rtprio).type) {
558 	case RTP_PRIO_REALTIME:
559 		niceval = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_LWP(k, rtprio).prio;
560 		break;
561 	case RTP_PRIO_IDLE:
562 		niceval = PRIO_MAX + 1 + KI_LWP(k, rtprio).prio;
563 		break;
564 	case RTP_PRIO_THREAD:
565 		niceval = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_LWP(k, rtprio).prio;
566 		break;
567 	default:
568 		niceval = KI_PROC(k, nice) - NZERO;
569 		break;
570 	}
571 	printf("%*d", vent->width, niceval);
572 }
573 
574 
575 double
576 getpmem(const KINFO *k)
577 {
578 	static int failure;
579 	double fracmem;
580 	int szptudot;
581 
582 	if (!nlistread)
583 		failure = donlist();
584 	if (failure)
585 		return (0.0);
586 
587 	if (KI_PROC(k, flags) & P_SWAPPEDOUT)
588 		return (0.0);
589 	/* XXX want pmap ptpages, segtab, etc. (per architecture) */
590 	szptudot = UPAGES;
591 	/* XXX don't have info about shared */
592 	fracmem = ((float)KI_PROC(k, vm_rssize) + szptudot)/mempages;
593 	return (100.0 * fracmem);
594 }
595 
596 void
597 pmem(const KINFO *k, const struct varent *vent)
598 {
599 	printf("%*.1f", vent->width, getpmem(k));
600 }
601 
602 void
603 pagein(const KINFO *k, const struct varent *vent)
604 {
605 	printf("%*ld", vent->width, KI_LWP(k, ru).ru_majflt);
606 }
607 
608 /* ARGSUSED */
609 void
610 maxrss(const KINFO *k __unused, const struct varent *vent)
611 {
612 	printf("%*ld", vent->width, KI_PROC(k, ru).ru_maxrss);
613 }
614 
615 void
616 tsize(const KINFO *k, const struct varent *vent)
617 {
618 	put64(pgtob(KI_PROC(k, vm_tsize))/1024, vent->width, 'k');
619 }
620 
621 void
622 rtprior(const KINFO *k, const struct varent *vent)
623 {
624 	struct rtprio *prtp;
625 	char str[8];
626 	unsigned prio, type;
627 
628 	prtp = &KI_LWP(k, rtprio);
629 	prio = prtp->prio;
630 	type = prtp->type;
631 	switch (type) {
632 	case RTP_PRIO_REALTIME:
633 		snprintf(str, sizeof(str), "real:%u", prio);
634 		break;
635 	case RTP_PRIO_NORMAL:
636 		strncpy(str, "normal", sizeof(str));
637 		break;
638 	case RTP_PRIO_IDLE:
639 		snprintf(str, sizeof(str), "idle:%u", prio);
640 		break;
641 	default:
642 		snprintf(str, sizeof(str), "%u:%u", type, prio);
643 		break;
644 	}
645 	str[sizeof(str) - 1] = '\0';
646 	printf("%*s", vent->width, str);
647 }
648 
649 /*
650  * Generic output routines.  Print fields from various prototype
651  * structures.
652  */
653 static void
654 printval(const char *bp, const struct varent *vent)
655 {
656 	static char ofmt[32] = "%";
657 	const char *fcp;
658 	char *cp;
659 
660 	cp = ofmt + 1;
661 	fcp = vent->var->fmt;
662 	if (vent->var->flag & LJUST)
663 		*cp++ = '-';
664 	*cp++ = '*';
665 	while ((*cp++ = *fcp++));
666 
667 	switch (vent->var->type) {
668 	case CHAR:
669 		printf(ofmt, vent->width, *(const char *)bp);
670 		break;
671 	case UCHAR:
672 		printf(ofmt, vent->width, *(const u_char *)bp);
673 		break;
674 	case SHORT:
675 		printf(ofmt, vent->width, *(const short *)bp);
676 		break;
677 	case USHORT:
678 		printf(ofmt, vent->width, *(const u_short *)bp);
679 		break;
680 	case INT:
681 		printf(ofmt, vent->width, *(const int *)bp);
682 		break;
683 	case UINT:
684 		printf(ofmt, vent->width, *(const u_int *)bp);
685 		break;
686 	case LONG:
687 		printf(ofmt, vent->width, *(const long *)bp);
688 		break;
689 	case ULONG:
690 		printf(ofmt, vent->width, *(const u_long *)bp);
691 		break;
692 	case KPTR:
693 		printf(ofmt, vent->width, *(const u_long *)bp);
694 		break;
695 	default:
696 		errx(1, "unknown type %d", vent->var->type);
697 	}
698 }
699 
700 void
701 pvar(const KINFO *k, const struct varent *vent)
702 {
703 	printval((char *)((char *)k->ki_proc + vent->var->off), vent);
704 }
705 
706 void
707 lpest(const KINFO *k, const struct varent *vent)
708 {
709 	int val;
710 
711 	val = *(int *)((char *)&k->ki_proc->kp_lwp + vent->var->off);
712 	val = val / 128;
713 	printval((char *)&val, vent);
714 }
715 
716 
717 void
718 lpvar(const KINFO *k, const struct varent *vent)
719 {
720 	printval((char *)((char *)&k->ki_proc->kp_lwp + vent->var->off), vent);
721 }
722 
723 void
724 rvar(const KINFO *k, const struct varent *vent)
725 {
726 	printval(((const char *)&KI_LWP(k, ru) + vent->var->off), vent);
727 }
728 
729 static const char *
730 make_printable(const char *str)
731 {
732     static char *cpy;
733     int len;
734 
735     if (cpy)
736 	free(cpy);
737     len = strlen(str);
738     if ((cpy = malloc(len * 4 + 1)) == NULL)
739 	err(1, NULL);
740     strvis(cpy, str, VIS_TAB | VIS_NL | VIS_NOSLASH);
741     return(cpy);
742 }
743 
744 static const char *
745 make_printable2(const char *str)
746 {
747     static char *cpy2;
748     int len;
749 
750     if (cpy2)
751 	free(cpy2);
752     len = strlen(str);
753     if ((cpy2 = malloc(len * 4 + 1)) == NULL)
754 	err(1, NULL);
755     strvis(cpy2, str, VIS_TAB | VIS_NL | VIS_NOSLASH);
756     return(cpy2);
757 }
758 
759 /*
760  * Output a number, divide down as needed to fit within the
761  * field.  This function differs from the code used by systat
762  * in that it tries to differentiate the display by always
763  * using a decimal point for excessively large numbers so
764  * the human eye naturally notices the difference.
765  */
766 static void
767 put64(u_int64_t n, int w, int type)
768 {
769 	char b[128];
770 	u_int64_t d;
771 	u_int64_t u;
772 	size_t len;
773 	int ntype;
774 
775 	snprintf(b, sizeof(b), "%*jd", w, (uintmax_t)n);
776 	len = strlen(b);
777 	if (len <= (size_t)w) {
778 		fwrite(b, len, 1, stdout);
779 		return;
780 	}
781 
782 	if (type == 'D')
783 		u = 1000;
784 	else
785 		u = 1024;
786 
787 	ntype = 0;
788 	for (d = 1; n / d >= 100000; d *= u) {
789 		switch(type) {
790 		case 'D':
791 		case 0:
792 			type = 'k';
793 			ntype = 'M';
794 			break;
795 		case 'k':
796 			type = 'M';
797 			ntype = 'G';
798 			break;
799 		case 'M':
800 			type = 'G';
801 			ntype = 'T';
802 			break;
803 		case 'G':
804 			type = 'T';
805 			ntype = 'X';
806 			break;
807 		case 'T':
808 			type = 'X';
809 			ntype = '?';
810 			break;
811 		default:
812 			type = '?';
813 			break;
814 		}
815 	}
816 	if (w > 4 && n / d >= u) {
817 		snprintf(b, sizeof(b), "%*ju.%02u%c",
818 			 w - 4,
819 			 (uintmax_t)(n / (d * u)),
820 			 (u_int)(n / (d * u / 100) % 100),
821 			 ntype);
822 	} else {
823 		snprintf(b, sizeof(b), "%*jd%c",
824 			w - 1, (uintmax_t)n / d, type);
825 	}
826 	len = strlen(b);
827 	fwrite(b, len, 1, stdout);
828 }
829