Ticket #3666: util.c.patch

File util.c.patch, 1.8 KB (added by michael-o, 8 years ago)
  • lib/util.c

    diff --git a/lib/util.c b/lib/util.c
    index 304351d..284219c 100644
    a b size_trunc (uintmax_t size, gboolean use_si) 
    346346{ 
    347347    static char x[BUF_TINY]; 
    348348    uintmax_t divisor = 1; 
    349     const char *xtra = _("B"); 
     349    const char *scaled_symbol = _("B"); 
     350    double scaled_size; 
    350351 
    351     if (size > 999999999UL) 
     352    if ((use_si && size >= 1000000000UL) || size >= (1024UL * 1024UL * 1024UL)) 
     353    { 
     354        divisor = use_si ? (1000 * 1000 * 1000) : (1024 * 1024 * 1024); 
     355        scaled_symbol = use_si ? _("GB") : _("GiB"); 
     356    } 
     357    else if ((use_si && size >= 1000000UL) || size >= (1024UL * 1024UL)) 
     358    { 
     359        divisor = use_si ? (1000 * 1000) : (1024 * 1024); 
     360        scaled_symbol = use_si ? _("MB") : _("MiB"); 
     361    } 
     362    else if ((use_si && size >= 1000UL) || size >= 1024UL) 
    352363    { 
    353364        divisor = use_si ? 1000 : 1024; 
    354         xtra = use_si ? _("kB") : _("KiB"); 
     365        scaled_symbol = use_si ? _("kB") : _("KiB"); 
     366    } 
     367    else 
     368    { 
     369        g_snprintf (x, sizeof (x), "%" PRIuMAX " %s", size, scaled_symbol); 
     370        return x; 
     371    } 
    355372 
    356         if (size / divisor > 999999999UL) 
    357         { 
    358             divisor = use_si ? (1000 * 1000) : (1024 * 1024); 
    359             xtra = use_si ? _("MB") : _("MiB"); 
     373   scaled_size = 1.0 * size / divisor; 
     374 
     375   if (scaled_size < 0.05 || scaled_size >= 10.0) 
     376       g_snprintf (x, sizeof (x), "%.0f %s", scaled_size, scaled_symbol); 
     377   else 
     378       g_snprintf (x, sizeof (x), "%.1f %s" PRIuMAX , scaled_size, scaled_symbol); 
    360379 
    361             if (size / divisor > 999999999UL) 
    362             { 
    363                 divisor = use_si ? (1000 * 1000 * 1000) : (1024 * 1024 * 1024); 
    364                 xtra = use_si ? _("GB") : _("GiB"); 
    365             } 
    366         } 
    367     } 
    368     g_snprintf (x, sizeof (x), "%.0f %s", 1.0 * size / divisor, xtra); 
    369380    return x; 
    370381} 
    371382