From patchwork Wed Jan 29 23:48:23 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Capella X-Patchwork-Id: 23912 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ob0-f197.google.com (mail-ob0-f197.google.com [209.85.214.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id A143C202FA for ; Wed, 29 Jan 2014 23:48:53 +0000 (UTC) Received: by mail-ob0-f197.google.com with SMTP id gq1sf8470000obb.8 for ; Wed, 29 Jan 2014 15:48:52 -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=ANYiLN/nSrfm2GmwD1wKas0aIfFk31jQxORfZnN9Q60=; b=Auy2xESDF5FYQe1WMclKZCe8xNgcXOfgXRPHSmNuP46wWuFvremGCtRnXOYva42shx 96nb4MXAnQZI2AIM3aQG0n9hp6rwPaTSO7ibwZKBKknDXjXKggsKHfpTb14Lv6/5tfhr uvjBM1b9Hvd8vPdm5163Oy7IgIrYLVbtB62qwcvbZ/q7Pd6TxUivv+LRNfaPA4DIhT0y +uTwHdQIOBDihRPt3Qwck08rMbYanGXIYAMvPhTSIuEljwfClTVjWcz4ZIy+6HxZCNIR SttQsj8hTFRkKbbi1oRVTghyMdJASZWqXWvjYVs/aVHffcZ3VC+sogJ100fFrFIVdvTK iTtg== X-Gm-Message-State: ALoCoQnAwxJeMvGPKs1rYMD/9CuUhLw99b2unaVYyiEjgiGnXBU0U8CYQU2m/6XD7qa//ATCCMyb X-Received: by 10.182.108.136 with SMTP id hk8mr4127296obb.11.1391039332754; Wed, 29 Jan 2014 15:48:52 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.34.235 with SMTP id l98ls346033qgl.90.gmail; Wed, 29 Jan 2014 15:48:52 -0800 (PST) X-Received: by 10.220.58.202 with SMTP id i10mr3822588vch.23.1391039332618; Wed, 29 Jan 2014 15:48:52 -0800 (PST) Received: from mail-ve0-f173.google.com (mail-ve0-f173.google.com [209.85.128.173]) by mx.google.com with ESMTPS id uo16si1304822veb.86.2014.01.29.15.48.52 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 29 Jan 2014 15:48:52 -0800 (PST) Received-SPF: neutral (google.com: 209.85.128.173 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.173; Received: by mail-ve0-f173.google.com with SMTP id oz11so1677855veb.18 for ; Wed, 29 Jan 2014 15:48:52 -0800 (PST) X-Received: by 10.52.227.193 with SMTP id sc1mr7327668vdc.1.1391039332543; Wed, 29 Jan 2014 15:48:52 -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 u4csp156735vcz; Wed, 29 Jan 2014 15:48:52 -0800 (PST) X-Received: by 10.68.204.161 with SMTP id kz1mr11117808pbc.156.1391039331373; Wed, 29 Jan 2014 15:48:51 -0800 (PST) Received: from mail-pa0-f48.google.com (mail-pa0-f48.google.com [209.85.220.48]) by mx.google.com with ESMTPS id kn7si4236949pbc.246.2014.01.29.15.48.51 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 29 Jan 2014 15:48:51 -0800 (PST) Received-SPF: neutral (google.com: 209.85.220.48 is neither permitted nor denied by best guess record for domain of sebastian.capella@linaro.org) client-ip=209.85.220.48; Received: by mail-pa0-f48.google.com with SMTP id kx10so2401142pab.21 for ; Wed, 29 Jan 2014 15:48:51 -0800 (PST) X-Received: by 10.68.76.68 with SMTP id i4mr10886865pbw.73.1391039330972; Wed, 29 Jan 2014 15:48:50 -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 yz5sm27432615pac.9.2014.01.29.15.48.48 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Wed, 29 Jan 2014 15:48:49 -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 , Michel Lespinasse , Shaohua Li , Jerome Marchand , Mikulas Patocka , Joonsoo Kim Subject: [PATCH v4 1/2] mm: add kstrimdup function Date: Wed, 29 Jan 2014 15:48:23 -0800 Message-Id: <1391039304-3172-2-git-send-email-sebastian.capella@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1391039304-3172-1-git-send-email-sebastian.capella@linaro.org> References: <1391039304-3172-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.173 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: 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 | 19 +++++++++++++++++++ 2 files changed, 20 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 a24aa22..da17de5 100644 --- a/mm/util.c +++ b/mm/util.c @@ -63,6 +63,25 @@ 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 *ret = kstrdup(skip_spaces(s), gfp); + + if (ret) + strim(ret); + return ret; +} +EXPORT_SYMBOL(kstrimdup); + +/** * kmemdup - duplicate region of memory * * @src: memory region to duplicate