From patchwork Fri Jan 31 00:54:13 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Capella X-Patchwork-Id: 23942 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-vb0-f69.google.com (mail-vb0-f69.google.com [209.85.212.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 8C4FB202B2 for ; Fri, 31 Jan 2014 00:54:25 +0000 (UTC) Received: by mail-vb0-f69.google.com with SMTP id m10sf8844590vbh.8 for ; Thu, 30 Jan 2014 16:54:24 -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=QX6fvDTWFWvJyPZZU1IteHsPQ2bRQW1V3aOmuc8Fh9I=; b=hnXGd/vhNuJ6KAm1SgyVZcrMIvL0SNKElss1G0U9d7s0r+oEk2+G7CHN8G8TNAv6to h3xOVRbWxpdyyxHPwlVgs4ZEirZFHrMYwHJhWZs/ltksZ+N+VVAbfeb1s5BdVvsWaKzY mfQslKqvx2CohkeJkuHrDF3rgaCR7GQMBl0G2gRygtQ9U4PTYmBvBGSdbTodLYUPpde9 D7TgVpehaUvY0HLQQfRYIx+74Mtv3kTx3sj6HJwFXZBOiLHUbOWSe36wKce5m3dabv6P 0TvmAVhp9T/ZUERHuU6b8bv16gv+ky7izBjfTfCDilxMrB0mmXV36IAwkfGUr3Q/WYqE hjww== X-Gm-Message-State: ALoCoQkpgsis+j/6Z+VneOP+766ksFtn7eBEX62GEkA7Hxdcnm5I2pOJ2id8uTmTbR4ZNDkp6Z5e X-Received: by 10.236.19.36 with SMTP id m24mr5464279yhm.14.1391129664775; Thu, 30 Jan 2014 16:54:24 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.26.195 with SMTP id 61ls679205qgv.19.gmail; Thu, 30 Jan 2014 16:54:24 -0800 (PST) X-Received: by 10.58.69.111 with SMTP id d15mr14226292veu.3.1391129664715; Thu, 30 Jan 2014 16:54:24 -0800 (PST) Received: from mail-vb0-f44.google.com (mail-vb0-f44.google.com [209.85.212.44]) by mx.google.com with ESMTPS id sg4si2764469vdc.27.2014.01.30.16.54.24 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 30 Jan 2014 16:54:24 -0800 (PST) Received-SPF: neutral (google.com: 209.85.212.44 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.212.44; Received: by mail-vb0-f44.google.com with SMTP id f12so2577918vbg.3 for ; Thu, 30 Jan 2014 16:54:24 -0800 (PST) X-Received: by 10.58.133.15 with SMTP id oy15mr4792889veb.19.1391129664582; Thu, 30 Jan 2014 16:54:24 -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 u4csp57662vcz; Thu, 30 Jan 2014 16:54:24 -0800 (PST) X-Received: by 10.66.154.75 with SMTP id vm11mr17890118pab.124.1391129663420; Thu, 30 Jan 2014 16:54:23 -0800 (PST) Received: from mail-pd0-f178.google.com (mail-pd0-f178.google.com [209.85.192.178]) by mx.google.com with ESMTPS id sj5si8392610pab.197.2014.01.30.16.54.23 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 30 Jan 2014 16:54:23 -0800 (PST) Received-SPF: neutral (google.com: 209.85.192.178 is neither permitted nor denied by best guess record for domain of sebastian.capella@linaro.org) client-ip=209.85.192.178; Received: by mail-pd0-f178.google.com with SMTP id y13so3680684pdi.37 for ; Thu, 30 Jan 2014 16:54:23 -0800 (PST) X-Received: by 10.67.3.97 with SMTP id bv1mr17775950pad.54.1391129662950; Thu, 30 Jan 2014 16:54:22 -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 xn12sm53562728pac.12.2014.01.30.16.54.20 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Thu, 30 Jan 2014 16:54:21 -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 , David Rientjes , Michel Lespinasse , Shaohua Li , Jerome Marchand , Joonsoo Kim Subject: [PATCH v6 1/2] mm: add kstrimdup function Date: Thu, 30 Jan 2014 16:54:13 -0800 Message-Id: <1391129654-12854-2-git-send-email-sebastian.capella@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1391129654-12854-1-git-send-email-sebastian.capella@linaro.org> References: <1391129654-12854-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.212.44 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 creates a whitespace-trimmed duplicate of the passed in null-terminated string. This is useful for strings coming from sysfs that often include trailing whitespace due to user input. Thanks to Joe Perches for this implementation. Signed-off-by: Sebastian Capella Cc: Andrew Morton Cc: Joe Perches Cc: Mikulas Patocka Cc: David Rientjes 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..a8b731c 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 && 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