Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 591762 | Differences between
and this patch

Collapse All | Expand All

(-)a/top/top.c (-5 / +36 lines)
Lines 1552-1566 static const char *scale_mem (int target, unsigned long num, int width, int just Link Here
1552
   if (Rc.zero_suppress && 0 >= num)
1552
   if (Rc.zero_suppress && 0 >= num)
1553
      goto end_justifies;
1553
      goto end_justifies;
1554
1554
1555
   i = SK_Kb;
1556
   psfx = Scaled_sfxtab;
1555
   scaled_num = num;
1557
   scaled_num = num;
1556
   for (i = SK_Kb, psfx = Scaled_sfxtab; i < SK_Eb; psfx++, i++) {
1558
1557
      if (i >= target
1559
   // apply the minimum scaling
1558
      && (width >= snprintf(buf, sizeof(buf), fmttab[i], scaled_num, *psfx)))
1560
   while (i < target) {
1559
         goto end_justifies;
1561
      psfx++;
1562
      i++;
1560
      scaled_num /= 1024.0;
1563
      scaled_num /= 1024.0;
1561
   }
1564
   }
1562
1565
1563
   // well shoot, this outta' fit...
1566
   // get it to fit in the buffer
1567
   while (i <= SK_Eb) {
1568
      // first try lopping off digits
1569
      static char fmt[SMLBUFSIZ];
1570
      int precision;
1571
1572
      switch(i) {
1573
      case 0: precision = 0; break; // k
1574
      case 1: precision = 1; break; // m
1575
      default: precision = 3; break; // g and up
1576
      }
1577
1578
      do {
1579
         snprintf(fmt, sizeof(fmt), "%%.%uf%%c", precision);
1580
         if (snprintf(buf, sizeof(buf), fmt, scaled_num, (i > 0 ? *psfx : '\0')) <= width) {
1581
            goto end_justifies; // it fits!
1582
         }
1583
         // drop a digit off the end and try again
1584
         precision--;
1585
      } while (precision > 0);
1586
1587
      // dropped all digits after decimal, still too big
1588
      // scale it and try again.
1589
      psfx++;
1590
      i++;
1591
      scaled_num /= 1024.0;
1592
   }
1593
1594
   // everything failed
1564
   snprintf(buf, sizeof(buf), "?");
1595
   snprintf(buf, sizeof(buf), "?");
1565
end_justifies:
1596
end_justifies:
1566
   return justify_pad(buf, width, justr);
1597
   return justify_pad(buf, width, justr);

Return to bug 591762