From patchwork Sat Oct 13 19:27:14 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Goffredo Baroncelli X-Patchwork-Id: 1589281 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id 9676240B11 for ; Sat, 13 Oct 2012 19:27:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754054Ab2JMT1E (ORCPT ); Sat, 13 Oct 2012 15:27:04 -0400 Received: from mail-ee0-f46.google.com ([74.125.83.46]:51930 "EHLO mail-ee0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754047Ab2JMT1C (ORCPT ); Sat, 13 Oct 2012 15:27:02 -0400 Received: by mail-ee0-f46.google.com with SMTP id b15so2406769eek.19 for ; Sat, 13 Oct 2012 12:27:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=fcTQg5mHEmovPGJTfU8PrJ43t9o6M8gOcgOgWWbkig4=; b=EE7ehfiguCykFkEHCL2KVM9s2u2jh8I7mhjpUcI2QXxQS+BwPOcVVts54NUFj+RCMR ISNw8c4a4O8c2RRdiY5KyUO7KKWpQOch7kM8E177lTIxb3Q/wcyNTanTUoCaJAtGKwJJ j2urPb9KPEog6enxWaCuujPluzQFh36em9icAfj/MbmQfda7ETdYxohhRZZqlh8Uzv2X WPDtWZhBF8h4+bOOUZYzgjzp5LukZrITuZh/aHXoh0PIMyYpsNEphYdTKgk1zz+I0p8O JynTGFs6TDPo6mAlezabCH9aqkwfSa0acqRJCIO9S6foa44u2izNObl4spptQNFWeoUo uXEw== Received: by 10.14.209.129 with SMTP id s1mr10787015eeo.24.1350156421218; Sat, 13 Oct 2012 12:27:01 -0700 (PDT) Received: from venice..bhome (host103-133-static.242-95-b.business.telecomitalia.it. [95.242.133.103]) by mx.google.com with ESMTPS id x42sm17352279eel.11.2012.10.13.12.26.59 (version=SSLv3 cipher=OTHER); Sat, 13 Oct 2012 12:27:00 -0700 (PDT) From: Goffredo Baroncelli To: kreijack@gmail.com Cc: Hugo Mills , Chris Mason , linux-btrfs@vger.kernel.org, David Sterba , Martin Steigerwald , Goffredo Baroncelli Subject: [PATCH 1/3] Add support for different unit. Date: Sat, 13 Oct 2012 21:27:14 +0200 Message-Id: <1350156436-14439-2-git-send-email-kreijack@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1350156436-14439-1-git-send-email-kreijack@gmail.com> References: <1350156436-14439-1-git-send-email-kreijack@gmail.com> Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org From: Goffredo Baroncelli The function pretty_sizes() returns a string containing the passed number. It add a suffix depending by the number: eg KiB, MiB. This change replace the old SI suffix (KB, MB..) by the IEC ones (KiB, MiB..). Moreover a space is added between the suffix and the number. Setting opprtunately the enviroment variable BTRFS_UNIT, it is possible to: BTRFS_UNIT=SI the suffix is KB for 1000bytes, MB for 10^6 bytes... BTRFS_UNIT=IEC the suffix is KiB for 1024bytes, MiB for 1024 KiB ... BTRFS_UNIT=COMPACT the suffix is KB for 1024 bytes, MB for 1024 KiB; no space between the number and the suffix. See http://en.wikipedia.org/wiki/Byte for further information about the different suffix. --- utils.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 61 insertions(+), 9 deletions(-) diff --git a/utils.c b/utils.c index 205e667..8528cc1 100644 --- a/utils.c +++ b/utils.c @@ -1085,33 +1085,85 @@ out: return ret; } -static char *size_strs[] = { "", "KB", "MB", "GB", "TB", +static char *size_strs_inf[] = { "", "KiB", "MiB", "GiB", "TiB", + "PiB", "EiB", "ZiB", "YiB"}; +static char *size_strs_dec[] = { "", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}; + +static int which_unit( ){ + static int unit=-1; + char *u; + + if( unit != -1 ) + return unit; + + unit = 0; + + u =getenv("BTRFS_UNIT"); + if(!u) return 0; + + if( !strcmp(u,"SI") ) + unit = 1; + else if( !strcmp(u, "COMPACT") ) + unit = 2; + else if( !strcmp(u, "IEC") ) + unit = 0; + /* else + Shall we raise an error ? + */ + + return unit; + +} + + char *pretty_sizes(u64 size) { int num_divs = 0; - int pretty_len = 16; + int pretty_len = 20; float fraction; - char *pretty; + char *pretty, *space; + int shift = 1024; + char **size_strs; + + if( which_unit() == 1 ){ /* SI */ + shift = 1000; + size_strs = size_strs_dec; + space = " "; + } else if( which_unit() == 2 ){ /* Old method: + SI suffix, but + multiply of 1024 */ + shift = 1024; + size_strs = size_strs_dec; + space = ""; + }else{ + shift = 1024; /* IEC */ + size_strs = size_strs_inf; + space = " "; + } - if( size < 1024 ){ + if( size < shift ){ fraction = size; num_divs = 0; } else { u64 last_size = size; num_divs = 0; - while(size >= 1024){ + while(size >= shift){ last_size = size; - size /= 1024; + size /= shift; num_divs ++; } - if (num_divs > ARRAY_SIZE(size_strs)) + if (num_divs > ARRAY_SIZE(size_strs_inf)) return NULL; - fraction = (float)last_size / 1024; + fraction = (float)last_size / shift; } pretty = malloc(pretty_len); - snprintf(pretty, pretty_len, "%.2f%s", fraction, size_strs[num_divs]); + + snprintf(pretty, pretty_len, "%.2f%s%s", + fraction, + space, + size_strs[num_divs]); return pretty; }