From patchwork Thu Jan 30 21:11:57 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Capella X-Patchwork-Id: 3559231 Return-Path: X-Original-To: patchwork-linux-pm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 024A19F381 for ; Thu, 30 Jan 2014 21:12:53 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 2AA55201B9 for ; Thu, 30 Jan 2014 21:12:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4F80B20173 for ; Thu, 30 Jan 2014 21:12:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752064AbaA3VMW (ORCPT ); Thu, 30 Jan 2014 16:12:22 -0500 Received: from mail-pd0-f180.google.com ([209.85.192.180]:49500 "EHLO mail-pd0-f180.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753552AbaA3VMS (ORCPT ); Thu, 30 Jan 2014 16:12:18 -0500 Received: by mail-pd0-f180.google.com with SMTP id x10so3502693pdj.11 for ; Thu, 30 Jan 2014 13:12:18 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=hLhZTS1cnKbQH1TX2DfeDp+7QHE41RGXzCOeu22gG9k=; b=PrsHru6/lNNwWawLsvuQqr9cAqnRu9Hcc5Qm8XrTH4j3djlCcTGTqxeOzmvxfx5bi9 DGhkzzoCQHsZ6qaRxMXZcyC7lreQt3tNbc4zUOg5B+03L3Mwl5ggKG8k5lSa5kSJPAwN wqqfxlq4uaL2AT/IDf8TJem1IE4YF/J2vbb6X+fkEp2FxU1+igRg6b5l/Hkv4nvS/lx4 bbha0z9M8MSft8D35qDRA5zhiUKxIT8MKShRvLi107x3CIxa5Xef75hU4223wI521SCu jPI7esWxY3rnEyZqzRT5JwDoSECsHS5kaWv2pu4iAUb4KeS0wGqhVvBERB+/9mKGJTf2 N5XA== X-Gm-Message-State: ALoCoQl/gSkV9UmuRGEGvukg7wdHwyy9BLYReJM0i769E5PdwdDR8m0PRt4intgvm8SA4tXXHZHz X-Received: by 10.66.182.199 with SMTP id eg7mr16686716pac.135.1391116338280; Thu, 30 Jan 2014 13:12:18 -0800 (PST) Received: from localhost (cpe-76-93-135-111.san.res.rr.com. [76.93.135.111]) by mx.google.com with ESMTPSA id eb5sm50005551pad.22.2014.01.30.13.12.15 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Thu, 30 Jan 2014 13:12:16 -0800 (PST) From: Sebastian Capella To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-pm@vger.kernel.org, linaro-kernel@lists.linaro.org, patches@linaro.org Cc: Sebastian Capella , Andrew Morton , Joe Perches , Mikulas Patocka , Michel Lespinasse , Shaohua Li , Jerome Marchand , Joonsoo Kim Subject: [PATCH v5 1/2] mm: add kstrimdup function Date: Thu, 30 Jan 2014 13:11:57 -0800 Message-Id: <1391116318-17253-2-git-send-email-sebastian.capella@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1391116318-17253-1-git-send-email-sebastian.capella@linaro.org> References: <1391116318-17253-1-git-send-email-sebastian.capella@linaro.org> Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP kstrimdup will duplicate and trim spaces from the passed in null terminated string. This is useful for strings coming from sysfs that often include trailing whitespace due to user input. Signed-off-by: Sebastian Capella Cc: Andrew Morton Cc: Joe Perches Cc: Mikulas Patocka Cc: Rik van Riel (commit_signer:5/10=50%) Cc: Michel Lespinasse Cc: Shaohua Li Cc: Jerome Marchand Cc: Mikulas Patocka Cc: Joonsoo Kim --- include/linux/string.h | 1 + mm/util.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/include/linux/string.h b/include/linux/string.h index ac889c5..f29f9a0 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -114,6 +114,7 @@ void *memchr_inv(const void *s, int c, size_t n); extern char *kstrdup(const char *s, gfp_t gfp); extern char *kstrndup(const char *s, size_t len, gfp_t gfp); +extern char *kstrimdup(const char *s, gfp_t gfp); extern void *kmemdup(const void *src, size_t len, gfp_t gfp); extern char **argv_split(gfp_t gfp, const char *str, int *argcp); diff --git a/mm/util.c b/mm/util.c index 808f375..2066b33 100644 --- a/mm/util.c +++ b/mm/util.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -63,6 +64,35 @@ char *kstrndup(const char *s, size_t max, gfp_t gfp) EXPORT_SYMBOL(kstrndup); /** + * kstrimdup - Trim and copy a %NUL terminated string. + * @s: the string to trim and duplicate + * @gfp: the GFP mask used in the kmalloc() call when allocating memory + * + * Returns an address, which the caller must kfree, containing + * a duplicate of the passed string with leading and/or trailing + * whitespace (as defined by isspace) removed. + */ +char *kstrimdup(const char *s, gfp_t gfp) +{ + char *buf; + char *begin = skip_spaces(s); + size_t len = strlen(begin); + + while (len > 1 && isspace(begin[len - 1])) + len--; + + buf = kmalloc_track_caller(len + 1, gfp); + if (!buf) + return NULL; + + memcpy(buf, begin, len); + buf[len] = '\0'; + + return buf; +} +EXPORT_SYMBOL(kstrimdup); + +/** * kmemdup - duplicate region of memory * * @src: memory region to duplicate