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: 23935 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ie0-f198.google.com (mail-ie0-f198.google.com [209.85.223.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 9724B20341 for ; Thu, 30 Jan 2014 21:12:22 +0000 (UTC) Received: by mail-ie0-f198.google.com with SMTP id ar20sf12835010iec.5 for ; Thu, 30 Jan 2014 13:12:21 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe; bh=hLhZTS1cnKbQH1TX2DfeDp+7QHE41RGXzCOeu22gG9k=; b=HPrm87hXNNqoK6AXI12USRLJ4hyUf7OCAfuNc8K/fuNAlrdQZZm4WWEQfrp4mUF1BO NN/VRec0BsnC04yYSB37JSlIhhperNgwfFUt0SLUcRWdG7GxTRTXIDN/5/znj5vX4MuU uetVswpiDzrpocEy9NWGmF5oE26G6dzQWXh3EPk4/Gxvr5dXJQ2elRPry77dgbaPRF4U JcmdM9mPgPgjKi7pqaYSIDgVH49eQYy05JMwDTyHjYSIrSd/S0gV2KBi+57wSK2JF7UV qwrsRoGym1Jlutv5wlW97aZI2JxwDgEazNojA19aApnVQAVTuVfx2fX50GDweNGiC/Vi ObcA== X-Gm-Message-State: ALoCoQkY7BD1BE392ZeNsSLWAHMH0OqMvoLIqzuwzMaCYYkbr84p5q+T5B9FhgHE0Q8g0EfrCcUF X-Received: by 10.42.70.142 with SMTP id f14mr5384454icj.6.1391116341866; Thu, 30 Jan 2014 13:12:21 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.24.49 with SMTP id 46ls691470qgq.21.gmail; Thu, 30 Jan 2014 13:12:21 -0800 (PST) X-Received: by 10.58.161.227 with SMTP id xv3mr2020965veb.31.1391116341660; Thu, 30 Jan 2014 13:12:21 -0800 (PST) Received: from mail-ve0-f177.google.com (mail-ve0-f177.google.com [209.85.128.177]) by mx.google.com with ESMTPS id y3si2532481vdo.71.2014.01.30.13.12.21 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 30 Jan 2014 13:12:21 -0800 (PST) Received-SPF: neutral (google.com: 209.85.128.177 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.128.177; Received: by mail-ve0-f177.google.com with SMTP id jz11so2512085veb.8 for ; Thu, 30 Jan 2014 13:12:21 -0800 (PST) X-Received: by 10.220.139.136 with SMTP id e8mr545097vcu.34.1391116341567; Thu, 30 Jan 2014 13:12:21 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.220.174.196 with SMTP id u4csp45130vcz; Thu, 30 Jan 2014 13:12:20 -0800 (PST) X-Received: by 10.68.43.2 with SMTP id s2mr16749691pbl.60.1391116340286; Thu, 30 Jan 2014 13:12:20 -0800 (PST) Received: from mail-pd0-f175.google.com (mail-pd0-f175.google.com [209.85.192.175]) by mx.google.com with ESMTPS id ds4si7883178pbb.139.2014.01.30.13.12.18 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 30 Jan 2014 13:12:20 -0800 (PST) Received-SPF: neutral (google.com: 209.85.192.175 is neither permitted nor denied by best guess record for domain of sebastian.capella@linaro.org) client-ip=209.85.192.175; Received: by mail-pd0-f175.google.com with SMTP id w10so3498469pde.20 for ; Thu, 30 Jan 2014 13:12:18 -0800 (PST) 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> X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: sebastian.capella@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.128.177 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , 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