From patchwork Mon Oct 1 20:11:27 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Goffredo Baroncelli X-Patchwork-Id: 1532551 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id 3B836DFE80 for ; Mon, 1 Oct 2012 20:11:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752204Ab2JAULx (ORCPT ); Mon, 1 Oct 2012 16:11:53 -0400 Received: from mail-bk0-f46.google.com ([209.85.214.46]:64239 "EHLO mail-bk0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752076Ab2JAULx (ORCPT ); Mon, 1 Oct 2012 16:11:53 -0400 Received: by bkcjk13 with SMTP id jk13so5179865bkc.19 for ; Mon, 01 Oct 2012 13:11:51 -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; bh=FBvKW9lzpnAU7zRdK043sEok9tOj2/nAL8hiE+wL1yQ=; b=gxQuImzeONgrFgBKmiAqakf2B848CZdm+AEBslzwJrQJwYwRJdX6Ie9IjMyTtWwBwE 5d2MjG4h/NZO6oHbOI++TrtOFaqtNSkt5hxDRhaosx2csqn6q2myGwC9vba0dRExPWdB fpcenmx65wlKDtKvid4lmOkW9PNewm0jMTbCTE+zEz49W+i0mM0Mogoik8WisPAbuh4I iciNF6dlYDjrhZVkfLD8XxSV1hmov48bbyLXRqY0s4f3SZSmo9rySqH3OwnhXoef9Tfs CQHD04bKKLHsMre774Cb6rj5nONyOxopuE5SdTTK4Neylq9Qc1bI1+3z/AmK963cxfTE ChzQ== Received: by 10.204.128.89 with SMTP id j25mr6386866bks.23.1349122311841; Mon, 01 Oct 2012 13:11:51 -0700 (PDT) Received: from venice..bhome (host158-224-dynamic.53-79-r.retail.telecomitalia.it. [79.53.224.158]) by mx.google.com with ESMTPS id g8sm4777796bkv.6.2012.10.01.13.11.50 (version=SSLv3 cipher=OTHER); Mon, 01 Oct 2012 13:11:51 -0700 (PDT) From: Goffredo Baroncelli To: linux-btrfs@vger.kernel.org Cc: Chris Mason , Goffredo Baroncelli Subject: [BTRFS-PROGS][PATCH] pretty_sizes() returns incorrect values Date: Mon, 1 Oct 2012 22:11:27 +0200 Message-Id: <1349122287-6078-1-git-send-email-kreijack@gmail.com> X-Mailer: git-send-email 1.7.10.4 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org From: Goffredo Baroncelli pretty_sizes() returns incorrect values if the argument is < 1024. pretty_sizes(0) -> 0.00 OK pretty_sizes(102) -> 0.10 WRONG pretty_sizes(1023) -> 1.00 WRONG pretty_sizes(1024) -> 1.00KB OK Signed-off-by: Goffredo Baroncelli --- utils.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/utils.c b/utils.c index aade9e2..04c3e82 100644 --- a/utils.c +++ b/utils.c @@ -1097,25 +1097,27 @@ char *pretty_sizes(u64 size) { int num_divs = 0; int pretty_len = 16; - u64 last_size = size; - u64 fract_size = size; float fraction; char *pretty; - while(size > 0) { - fract_size = last_size; - last_size = size; - size /= 1024; - num_divs++; - } - if (num_divs == 0) - num_divs = 1; - if (num_divs > ARRAY_SIZE(size_strs)) - return NULL; + if( size < 1024 ){ + fraction = size; + num_divs = 0; + } else { + u64 last_size = size; + num_divs = 0; + while(size >= 1024){ + last_size = size; + size /= 1024; + num_divs ++; + } - fraction = (float)fract_size / 1024; + if (num_divs > ARRAY_SIZE(size_strs)) + return NULL; + fraction = (float)last_size / 1024; + } pretty = malloc(pretty_len); - snprintf(pretty, pretty_len, "%.2f%s", fraction, size_strs[num_divs-1]); + snprintf(pretty, pretty_len, "%.2f%s", fraction, size_strs[num_divs]); return pretty; }