xref: /dragonfly/bin/ps/print.c (revision 8bf5b238)
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 			/* FALLTHROUGH */
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 0
285 	/* removed, no longer interesting */
286 	if ((flag & P_SYSTEM) || KI_PROC(k, lock) > 0)
287 		*cp++ = 'L';
288 #endif
289 	if (flag & P_JAILED)
290 		*cp++ = 'J';
291 	if (KI_PROC(k, auxflags) & KI_SLEADER)
292 		*cp++ = 's';
293 	if ((flag & P_CONTROLT) && KI_PROC(k, pgid) == KI_PROC(k, tpgid))
294 		*cp++ = '+';
295 	*cp = '\0';
296 	printf("%-*s", vent->width, buf);
297 }
298 
299 /*
300  * Normalized priority (lower is better).  For pure threads
301  * output a negated LWKT priority (so lower still means better).
302  *
303  * XXX bsd4 scheduler specific.
304  */
305 void
306 pri(const KINFO *k, const struct varent *vent)
307 {
308 	if (KI_LWP(k, pid) != -1)
309 	    printf("%*d", vent->width, KI_LWP(k, prio));
310 	else
311 	    printf("%*d", vent->width, -(KI_LWP(k, tdprio)));
312 }
313 
314 void
315 tdpri(const KINFO *k, const struct varent *vent)
316 {
317 	char buf[32];
318 	int val = KI_LWP(k, tdprio);
319 
320 	snprintf(buf, sizeof(buf), "%2d", val);
321 	printf("%*s", vent->width, buf);
322 }
323 
324 void
325 uname(const KINFO *k, const struct varent *vent)
326 {
327 	printf("%-*s", vent->width,
328 	       user_from_uid(KI_PROC(k, uid), 0));
329 }
330 
331 int
332 s_uname(const KINFO *k)
333 {
334 	return (strlen(user_from_uid(KI_PROC(k, uid), 0)));
335 }
336 
337 void
338 runame(const KINFO *k, const struct varent *vent)
339 {
340 	printf("%-*s", vent->width,
341 	       user_from_uid(KI_PROC(k, ruid), 0));
342 }
343 
344 int
345 s_runame(const KINFO *k)
346 {
347 	return (strlen(user_from_uid(KI_PROC(k, ruid), 0)));
348 }
349 
350 void
351 tdev(const KINFO *k, const struct varent *vent)
352 {
353 	dev_t dev;
354 	char buff[16];
355 
356 	dev = KI_PROC(k, tdev);
357 	if (dev == NODEV)
358 		printf("%*s", vent->width, "??");
359 	else {
360 		snprintf(buff, sizeof(buff), "%d/%d", major(dev), minor(dev));
361 		printf("%*s", vent->width, buff);
362 	}
363 }
364 
365 void
366 tname(const KINFO *k, const struct varent *vent)
367 {
368 	dev_t dev;
369 	const char *ttname;
370 
371 	dev = KI_PROC(k, tdev);
372 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
373 		printf("%*s ", vent->width-1, "??");
374 	else {
375 		if (strncmp(ttname, "tty", 3) == 0 ||
376 		    strncmp(ttname, "cua", 3) == 0)
377 			ttname += 3;
378 		if (strncmp(ttname, "pts/", 4) == 0)
379 			ttname += 4;
380 		printf("%*.*s%c", vent->width-1, vent->width-1, ttname,
381 			KI_PROC(k, auxflags) & KI_CTTY ? ' ' : '-');
382 	}
383 }
384 
385 void
386 longtname(const KINFO *k, const struct varent *vent)
387 {
388 	dev_t dev;
389 	const char *ttname;
390 
391 	dev = KI_PROC(k, tdev);
392 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
393 		printf("%-*s", vent->width, "??");
394 	else
395 		printf("%-*s", vent->width, ttname);
396 }
397 
398 void
399 started(const KINFO *k, const struct varent *vent)
400 {
401 	static time_t now;
402 	time_t then;
403 	struct tm *tp;
404 	char buf[100];
405 	static int  use_ampm = -1;
406 
407 	if (use_ampm < 0)
408 		use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
409 
410 	then = KI_PROC(k, start).tv_sec;
411 	if (then < btime.tv_sec) {
412 		then = btime.tv_sec;
413 	}
414 
415 	tp = localtime(&then);
416 	if (!now)
417 		time(&now);
418 	if (now - then < 24 * 3600) {
419 		strftime(buf, sizeof(buf) - 1,
420 		use_ampm ? "%l:%M%p" : "%k:%M  ", tp);
421 	} else if (now - then < 7 * 86400) {
422 		strftime(buf, sizeof(buf) - 1,
423 		use_ampm ? "%a%I%p" : "%a%H  ", tp);
424 	} else
425 		strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
426 	printf("%-*s", vent->width, buf);
427 }
428 
429 void
430 lstarted(const KINFO *k, const struct varent *vent)
431 {
432 	time_t then;
433 	char buf[100];
434 
435 	then = KI_PROC(k, start).tv_sec;
436 	strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
437 	printf("%-*s", vent->width, buf);
438 }
439 
440 void
441 wchan(const KINFO *k, const struct varent *vent)
442 {
443 	if (*KI_LWP(k, wmesg)) {
444 		printf("%-*.*s", vent->width, vent->width,
445 		       KI_LWP(k, wmesg));
446 	} else {
447 		printf("%-*s", vent->width, "-");
448 	}
449 }
450 
451 static u_int64_t
452 pgtob(u_int64_t pg)
453 {
454 	static size_t pgsize;
455 
456 	if (pgsize == 0)
457 		pgsize = getpagesize();
458 	return(pg * pgsize);
459 }
460 
461 void
462 vsize(const KINFO *k, const struct varent *vent)
463 {
464 	put64((uintmax_t)KI_PROC(k, vm_map_size)/1024, vent->width, 'k');
465 }
466 
467 void
468 rssize(const KINFO *k, const struct varent *vent)
469 {
470 	/* XXX don't have info about shared */
471 	put64(pgtob(KI_PROC(k, vm_rssize))/1024, vent->width, 'k');
472 }
473 
474 /* doesn't account for text */
475 void
476 p_rssize(const KINFO *k, const struct varent *vent)
477 {
478 	put64(pgtob(KI_PROC(k, vm_rssize))/1024, vent->width, 'k');
479 }
480 
481 void
482 cputime(const KINFO *k, const struct varent *vent)
483 {
484 	long secs;
485 	long psecs;	/* "parts" of a second. first micro, then centi */
486 	u_int64_t timeus;
487 	char obuff[128];
488 	static char decimal_point = '\0';
489 
490 	if (decimal_point == '\0')
491 		decimal_point = localeconv()->decimal_point[0];
492 
493 	/*
494 	 * This counts time spent handling interrupts.  We could
495 	 * fix this, but it is not 100% trivial (and interrupt
496 	 * time fractions only work on the sparc anyway).	XXX
497 	 */
498 	timeus = KI_LWP(k, uticks) + KI_LWP(k, sticks) +
499 		KI_LWP(k, iticks);
500 	secs = timeus / 1000000;
501 	psecs = timeus % 1000000;
502 	if (sumrusage) {
503 		secs += KI_PROC(k, cru).ru_utime.tv_sec +
504 			KI_PROC(k, cru).ru_stime.tv_sec;
505 		psecs += KI_PROC(k, cru).ru_utime.tv_usec +
506 			KI_PROC(k, cru).ru_stime.tv_usec;
507 	}
508 	/*
509 	 * round and scale to 100's
510 	 */
511 	psecs = (psecs + 5000) / 10000;
512 	secs += psecs / 100;
513 	psecs = psecs % 100;
514 #if 1
515 	if (secs >= 86400) {
516 		snprintf(obuff, sizeof(obuff), "%3ldd%02ld:%02ld",
517 			secs / 86400, secs / (60 * 60) % 24, secs / 60 % 60);
518 	} else if (secs >= 100 * 60) {
519 		snprintf(obuff, sizeof(obuff), "%2ld:%02ld:%02ld",
520 			secs / 60 / 60, secs / 60 % 60, secs % 60);
521 	} else
522 #endif
523 	{
524 		snprintf(obuff, sizeof(obuff), "%3ld:%02ld%c%02ld",
525 			 secs / 60, secs % 60,
526 			 decimal_point, psecs);
527 	}
528 	printf("%*s", vent->width, obuff);
529 }
530 
531 double
532 getpcpu(const KINFO *k)
533 {
534 	static int failure;
535 
536 	if (!nlistread)
537 		failure = donlist();
538 	if (failure)
539 		return (0.0);
540 
541 #define	fxtofl(fixpt)	((double)(fixpt) / fscale)
542 
543 	/* XXX - I don't like this */
544 	if (KI_PROC(k, swtime) == 0 || (KI_PROC(k, flags) & P_SWAPPEDOUT))
545 		return (0.0);
546 	return (100.0 * fxtofl(KI_LWP(k, pctcpu)));
547 }
548 
549 void
550 pcpu(const KINFO *k, const struct varent *vent)
551 {
552 	printf("%*.1f", vent->width, getpcpu(k));
553 }
554 
555 void
556 pnice(const KINFO *k, const struct varent *vent)
557 {
558 	int niceval;
559 
560 	switch (KI_LWP(k, rtprio).type) {
561 	case RTP_PRIO_REALTIME:
562 		niceval = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_LWP(k, rtprio).prio;
563 		break;
564 	case RTP_PRIO_IDLE:
565 		niceval = PRIO_MAX + 1 + KI_LWP(k, rtprio).prio;
566 		break;
567 	case RTP_PRIO_THREAD:
568 		niceval = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_LWP(k, rtprio).prio;
569 		break;
570 	default:
571 		niceval = KI_PROC(k, nice) - NZERO;
572 		break;
573 	}
574 	printf("%*d", vent->width, niceval);
575 }
576 
577 
578 double
579 getpmem(const KINFO *k)
580 {
581 	static int failure;
582 	double fracmem;
583 	int szptudot;
584 
585 	if (!nlistread)
586 		failure = donlist();
587 	if (failure)
588 		return (0.0);
589 
590 	if (KI_PROC(k, flags) & P_SWAPPEDOUT)
591 		return (0.0);
592 	/* XXX want pmap ptpages, segtab, etc. (per architecture) */
593 	szptudot = UPAGES;
594 	/* XXX don't have info about shared */
595 	fracmem = ((float)KI_PROC(k, vm_rssize) + szptudot)/mempages;
596 	return (100.0 * fracmem);
597 }
598 
599 void
600 pmem(const KINFO *k, const struct varent *vent)
601 {
602 	printf("%*.1f", vent->width, getpmem(k));
603 }
604 
605 void
606 pagein(const KINFO *k, const struct varent *vent)
607 {
608 	printf("%*ld", vent->width, KI_LWP(k, ru).ru_majflt);
609 }
610 
611 /* ARGSUSED */
612 void
613 maxrss(const KINFO *k __unused, const struct varent *vent)
614 {
615 	printf("%*ld", vent->width, KI_PROC(k, ru).ru_maxrss);
616 }
617 
618 void
619 tsize(const KINFO *k, const struct varent *vent)
620 {
621 	put64(pgtob(KI_PROC(k, vm_tsize))/1024, vent->width, 'k');
622 }
623 
624 void
625 rtprior(const KINFO *k, const struct varent *vent)
626 {
627 	struct rtprio *prtp;
628 	char str[8];
629 	unsigned prio, type;
630 
631 	prtp = &KI_LWP(k, rtprio);
632 	prio = prtp->prio;
633 	type = prtp->type;
634 	switch (type) {
635 	case RTP_PRIO_REALTIME:
636 		snprintf(str, sizeof(str), "real:%u", prio);
637 		break;
638 	case RTP_PRIO_NORMAL:
639 		strncpy(str, "normal", sizeof(str));
640 		break;
641 	case RTP_PRIO_IDLE:
642 		snprintf(str, sizeof(str), "idle:%u", prio);
643 		break;
644 	default:
645 		snprintf(str, sizeof(str), "%u:%u", type, prio);
646 		break;
647 	}
648 	str[sizeof(str) - 1] = '\0';
649 	printf("%*s", vent->width, str);
650 }
651 
652 /*
653  * Generic output routines.  Print fields from various prototype
654  * structures.
655  */
656 static void
657 printval(const char *bp, const struct varent *vent)
658 {
659 	static char ofmt[32] = "%";
660 	const char *fcp;
661 	char *cp;
662 
663 	cp = ofmt + 1;
664 	fcp = vent->var->fmt;
665 	if (vent->var->flag & LJUST)
666 		*cp++ = '-';
667 	*cp++ = '*';
668 	while ((*cp++ = *fcp++));
669 
670 	switch (vent->var->type) {
671 	case CHAR:
672 		printf(ofmt, vent->width, *(const char *)bp);
673 		break;
674 	case UCHAR:
675 		printf(ofmt, vent->width, *(const u_char *)bp);
676 		break;
677 	case SHORT:
678 		printf(ofmt, vent->width, *(const short *)bp);
679 		break;
680 	case USHORT:
681 		printf(ofmt, vent->width, *(const u_short *)bp);
682 		break;
683 	case INT:
684 		printf(ofmt, vent->width, *(const int *)bp);
685 		break;
686 	case UINT:
687 		printf(ofmt, vent->width, *(const u_int *)bp);
688 		break;
689 	case LONG:
690 		printf(ofmt, vent->width, *(const long *)bp);
691 		break;
692 	case ULONG:
693 		printf(ofmt, vent->width, *(const u_long *)bp);
694 		break;
695 	case KPTR:
696 		printf(ofmt, vent->width, *(const u_long *)bp);
697 		break;
698 	default:
699 		errx(1, "unknown type %d", vent->var->type);
700 	}
701 }
702 
703 void
704 pvar(const KINFO *k, const struct varent *vent)
705 {
706 	printval((char *)((char *)k->ki_proc + vent->var->off), vent);
707 }
708 
709 void
710 lpest(const KINFO *k, const struct varent *vent)
711 {
712 	int val;
713 
714 	val = *(int *)((char *)&k->ki_proc->kp_lwp + vent->var->off);
715 	val = val / 128;
716 	printval((char *)&val, vent);
717 }
718 
719 
720 void
721 lpvar(const KINFO *k, const struct varent *vent)
722 {
723 	printval((char *)((char *)&k->ki_proc->kp_lwp + vent->var->off), vent);
724 }
725 
726 void
727 rvar(const KINFO *k, const struct varent *vent)
728 {
729 	printval(((const char *)&KI_LWP(k, ru) + vent->var->off), vent);
730 }
731 
732 static const char *
733 make_printable(const char *str)
734 {
735     static char *cpy;
736     int len;
737 
738     if (cpy)
739 	free(cpy);
740     len = strlen(str);
741     if ((cpy = malloc(len * 4 + 1)) == NULL)
742 	err(1, NULL);
743     strvis(cpy, str, VIS_TAB | VIS_NL | VIS_NOSLASH);
744     return(cpy);
745 }
746 
747 static const char *
748 make_printable2(const char *str)
749 {
750     static char *cpy2;
751     int len;
752 
753     if (cpy2)
754 	free(cpy2);
755     len = strlen(str);
756     if ((cpy2 = malloc(len * 4 + 1)) == NULL)
757 	err(1, NULL);
758     strvis(cpy2, str, VIS_TAB | VIS_NL | VIS_NOSLASH);
759     return(cpy2);
760 }
761 
762 /*
763  * Output a number, divide down as needed to fit within the
764  * field.  This function differs from the code used by systat
765  * in that it tries to differentiate the display by always
766  * using a decimal point for excessively large numbers so
767  * the human eye naturally notices the difference.
768  */
769 static void
770 put64(u_int64_t n, int w, int type)
771 {
772 	char b[128];
773 	u_int64_t d;
774 	u_int64_t u;
775 	size_t len;
776 	int ntype;
777 
778 	snprintf(b, sizeof(b), "%*jd", w, (uintmax_t)n);
779 	len = strlen(b);
780 	if (len <= (size_t)w) {
781 		fwrite(b, len, 1, stdout);
782 		return;
783 	}
784 
785 	if (type == 'D')
786 		u = 1000;
787 	else
788 		u = 1024;
789 
790 	ntype = 0;
791 	for (d = 1; n / d >= 100000; d *= u) {
792 		switch(type) {
793 		case 'D':
794 		case 0:
795 			type = 'k';
796 			ntype = 'M';
797 			break;
798 		case 'k':
799 			type = 'M';
800 			ntype = 'G';
801 			break;
802 		case 'M':
803 			type = 'G';
804 			ntype = 'T';
805 			break;
806 		case 'G':
807 			type = 'T';
808 			ntype = 'X';
809 			break;
810 		case 'T':
811 			type = 'X';
812 			ntype = '?';
813 			break;
814 		default:
815 			type = '?';
816 			break;
817 		}
818 	}
819 	if (w > 4 && n / d >= u) {
820 		snprintf(b, sizeof(b), "%*ju.%02u%c",
821 			 w - 4,
822 			 (uintmax_t)(n / (d * u)),
823 			 (u_int)(n / (d * u / 100) % 100),
824 			 ntype);
825 	} else {
826 		snprintf(b, sizeof(b), "%*jd%c",
827 			w - 1, (uintmax_t)n / d, type);
828 	}
829 	len = strlen(b);
830 	fwrite(b, len, 1, stdout);
831 }
832