From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754467AbbLCU44 (ORCPT ); Thu, 3 Dec 2015 15:56:56 -0500 Received: from mail-lb0-f171.google.com ([209.85.217.171]:35872 "EHLO mail-lb0-f171.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753808AbbLCUvb (ORCPT ); Thu, 3 Dec 2015 15:51:31 -0500 From: Rasmus Villemoes To: Andrew Morton , Andy Shevchenko , Rasmus Villemoes , Kees Cook , Martin Kletzander Cc: Al Viro , Ingo Molnar , linux-kernel@vger.kernel.org Subject: [PATCH v3 01/14] lib/vsprintf.c: pull out padding code from dentry_name() Date: Thu, 3 Dec 2015 21:51:00 +0100 Message-Id: <1449175873-1780-2-git-send-email-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.6.1 In-Reply-To: <1449175873-1780-1-git-send-email-linux@rasmusvillemoes.dk> References: <1449175873-1780-1-git-send-email-linux@rasmusvillemoes.dk> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Pull out the logic in dentry_name() which handles field width space padding, in preparation for reusing it from string(). Rename the widen() helper to move_right(), since it is used for handling the !(flags & LEFT) case. Cc: Al Viro Cc: Ingo Molnar Signed-off-by: Rasmus Villemoes --- lib/vsprintf.c | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/lib/vsprintf.c b/lib/vsprintf.c index f9cee8e1233c..d7452563a6a6 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -538,7 +538,7 @@ char *string(char *buf, char *end, const char *s, struct printf_spec spec) return buf; } -static void widen(char *buf, char *end, unsigned len, unsigned spaces) +static void move_right(char *buf, char *end, unsigned len, unsigned spaces) { size_t size; if (buf >= end) /* nowhere to put anything */ @@ -556,6 +556,35 @@ static void widen(char *buf, char *end, unsigned len, unsigned spaces) memset(buf, ' ', spaces); } +/* + * Handle field width padding for a string. + * @buf: current buffer position + * @n: length of string + * @end: end of output buffer + * @spec: for field width and flags + * Returns: new buffer position after padding. + */ +static noinline_for_stack +char *widen_string(char *buf, int n, char *end, struct printf_spec spec) +{ + unsigned spaces; + + if (likely(n >= spec.field_width)) + return buf; + /* we want to pad the sucker */ + spaces = spec.field_width - n; + if (!(spec.flags & LEFT)) { + move_right(buf - n, end, n, spaces); + return buf + spaces; + } + while (spaces--) { + if (buf < end) + *buf = ' '; + ++buf; + } + return buf; +} + static noinline_for_stack char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec, const char *fmt) @@ -597,20 +626,7 @@ char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp *buf = c; } rcu_read_unlock(); - if (n < spec.field_width) { - /* we want to pad the sucker */ - unsigned spaces = spec.field_width - n; - if (!(spec.flags & LEFT)) { - widen(buf - n, end, n, spaces); - return buf + spaces; - } - while (spaces--) { - if (buf < end) - *buf = ' '; - ++buf; - } - } - return buf; + return widen_string(buf, n, end, spec); } static noinline_for_stack -- 2.6.1