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