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