From patchwork Tue Apr 11 12:15:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yauheni Kaliuta X-Patchwork-Id: 9675043 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id CB9CB60381 for ; Tue, 11 Apr 2017 12:12:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C697528565 for ; Tue, 11 Apr 2017 12:12:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BB3D328569; Tue, 11 Apr 2017 12:12:44 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5930E28565 for ; Tue, 11 Apr 2017 12:12:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752397AbdDKMMn (ORCPT ); Tue, 11 Apr 2017 08:12:43 -0400 Received: from mx1.redhat.com ([209.132.183.28]:51726 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752314AbdDKMMn (ORCPT ); Tue, 11 Apr 2017 08:12:43 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 217B26316C; Tue, 11 Apr 2017 12:12:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 217B26316C Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=yauheni.kaliuta@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 217B26316C Received: from astarta.redhat.com (ovpn-117-212.ams2.redhat.com [10.36.117.212]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3B85217164; Tue, 11 Apr 2017 12:12:42 +0000 (UTC) From: Yauheni Kaliuta To: linux-modules Cc: Lucas De Marchi , Mark van Dijk Subject: [PATCH 3/4] libkmod: modinfo: implement line splitting in hex_to_str Date: Tue, 11 Apr 2017 15:15:02 +0300 Message-Id: <20170411121503.26181-4-yauheni.kaliuta@redhat.com> In-Reply-To: <20170411121503.26181-1-yauheni.kaliuta@redhat.com> References: <20170411121503.26181-1-yauheni.kaliuta@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Tue, 11 Apr 2017 12:12:43 +0000 (UTC) Sender: owner-linux-modules@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP The key output is usually short, but for signature it is more readable to output it in several lines. Implement line splitting. Set line limit hardcoded to 20 hex numbers (not characters). Signed-off-by: Yauheni Kaliuta --- libkmod/libkmod-module.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c index 22c8f4c852a2..9e155f080277 100644 --- a/libkmod/libkmod-module.c +++ b/libkmod/libkmod-module.c @@ -2197,15 +2197,25 @@ static char *kmod_module_hex_to_str(const char *hex, size_t len) { char *str; int i; + int j; + const size_t line_limit = 20; + size_t str_len; - str = malloc(len * 3); + str_len = len * 3; /* XX: or XX\0 */ + str_len += ((str_len + line_limit - 1) / line_limit - 1) * 3; /* \n\t\t */ + + str = malloc(str_len); if (str == NULL) return NULL; - for (i = 0; i < (int)len; i++) { - sprintf(str + i * 3, "%02X", (unsigned char)hex[i]); - if (i < (int)len - 1) - str[i * 3 + 2] = ':'; + for (i = 0, j = 0; i < (int)len; i++) { + j += sprintf(str + j, "%02X", (unsigned char)hex[i]); + if (i < (int)len - 1) { + str[j++] = ':'; + + if ((i + 1) % line_limit == 0) + j += sprintf(str + j, "\n\t\t"); + } } return str; }