From patchwork Tue Feb 4 20:43:49 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Capella X-Patchwork-Id: 24144 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ob0-f199.google.com (mail-ob0-f199.google.com [209.85.214.199]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 375C5202FA for ; Tue, 4 Feb 2014 20:43:58 +0000 (UTC) Received: by mail-ob0-f199.google.com with SMTP id vb8sf36697973obc.10 for ; Tue, 04 Feb 2014 12:43:57 -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=Q8htu7gucHuTA5b8+wZjIaa9XLTfueaWN083sR7d1zs=; b=E1z+BM0aKvqwWY9xXI/hRSjJTG12SkkJwA0FXfnh/pjIPFJpwPrZK963zqFMn0NOnG TTQxXiHD/ACKaX6d1rnk8i8OlZ/ft+pMi9DL+ZXxF8rL+Mqte7NRYg1zwwB9mJV91OoE UxyQgxQCPhQUKXaaMnmvgih7H9iSzSIH3wqNEu6UcGd/LfK8REvN/ibH9wm64CKeMISF cGhbMkMY8PEOYHmHUzLnsv4YD9T2fO1mypEDZEv2TWVz/E/k2rQ6xpkHvtqPFMHTz3rY uHAClHOat5Q2kgCxtYwDJ9OWPNu/FA49H6ZHH44O7dtg2jfK1NzohkTjtuJL1eEKG/yj yDJg== X-Gm-Message-State: ALoCoQlkBkxSgw6gKhDYBJQnXaX3a7XiV2o722ShZor4Wa4DVG2sGfHg8hYAeyy+wtEGKCOao0dP X-Received: by 10.43.14.137 with SMTP id pq9mr13874299icb.12.1391546637234; Tue, 04 Feb 2014 12:43:57 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.98.161 with SMTP id o30ls2531245qge.98.gmail; Tue, 04 Feb 2014 12:43:57 -0800 (PST) X-Received: by 10.52.164.203 with SMTP id ys11mr2125115vdb.37.1391546637101; Tue, 04 Feb 2014 12:43:57 -0800 (PST) Received: from mail-vc0-f171.google.com (mail-vc0-f171.google.com [209.85.220.171]) by mx.google.com with ESMTPS id b2si8706252vcy.85.2014.02.04.12.43.57 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 04 Feb 2014 12:43:57 -0800 (PST) Received-SPF: neutral (google.com: 209.85.220.171 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.220.171; Received: by mail-vc0-f171.google.com with SMTP id le5so6354382vcb.30 for ; Tue, 04 Feb 2014 12:43:57 -0800 (PST) X-Received: by 10.220.251.73 with SMTP id mr9mr33941079vcb.2.1391546637014; Tue, 04 Feb 2014 12:43:57 -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 u4csp273858vcz; Tue, 4 Feb 2014 12:43:56 -0800 (PST) X-Received: by 10.68.201.10 with SMTP id jw10mr46407903pbc.25.1391546636091; Tue, 04 Feb 2014 12:43:56 -0800 (PST) Received: from mail-pb0-f51.google.com (mail-pb0-f51.google.com [209.85.160.51]) by mx.google.com with ESMTPS id ef2si26090326pbb.41.2014.02.04.12.43.55 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 04 Feb 2014 12:43:56 -0800 (PST) Received-SPF: neutral (google.com: 209.85.160.51 is neither permitted nor denied by best guess record for domain of sebastian.capella@linaro.org) client-ip=209.85.160.51; Received: by mail-pb0-f51.google.com with SMTP id un15so8875912pbc.24 for ; Tue, 04 Feb 2014 12:43:55 -0800 (PST) X-Received: by 10.66.231.104 with SMTP id tf8mr8466755pac.48.1391546635611; Tue, 04 Feb 2014 12:43:55 -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 y7sm36604826pbb.8.2014.02.04.12.43.52 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Tue, 04 Feb 2014 12:43:54 -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 , Joe Perches , David Rientjes , Alexey Dobriyan Subject: [PATCH v7 1/3] mm: add kstrdup_trimnl function Date: Tue, 4 Feb 2014 12:43:49 -0800 Message-Id: <1391546631-7715-2-git-send-email-sebastian.capella@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1391546631-7715-1-git-send-email-sebastian.capella@linaro.org> References: <1391546631-7715-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.220.171 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: , kstrdup_trimnl creates a duplicate of the passed in null-terminated string. If a trailing newline is found, it is removed before duplicating. 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 Cc: Joe Perches Cc: David Rientjes Cc: Alexey Dobriyan --- include/linux/string.h | 1 + mm/util.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/include/linux/string.h b/include/linux/string.h index ac889c5..e7ec8c0 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 *kstrdup_trimnl(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..0bab867 100644 --- a/mm/util.c +++ b/mm/util.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -63,6 +64,34 @@ char *kstrndup(const char *s, size_t max, gfp_t gfp) EXPORT_SYMBOL(kstrndup); /** + * kstrdup_trimnl - Copy a %NUL terminated string, removing one trailing + * newline if present. + * @s: the string to 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 a single trailing newline + * removed if present. + */ +char *kstrdup_trimnl(const char *s, gfp_t gfp) +{ + char *buf; + size_t len = strlen(s); + if (len >= 1 && s[len - 1] == '\n') + len--; + + buf = kmalloc_track_caller(len + 1, gfp); + if (!buf) + return NULL; + + memcpy(buf, s, len); + buf[len] = '\0'; + + return buf; +} +EXPORT_SYMBOL(kstrdup_trimnl); + +/** * kmemdup - duplicate region of memory * * @src: memory region to duplicate