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