From patchwork Fri Jul 22 23:31:03 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Mayer X-Patchwork-Id: 9244387 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id DE94960757 for ; Fri, 22 Jul 2016 23:32:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CF1D427F94 for ; Fri, 22 Jul 2016 23:32:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C3373281AA; Fri, 22 Jul 2016 23:32:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5566427F94 for ; Fri, 22 Jul 2016 23:32:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752617AbcGVXcb (ORCPT ); Fri, 22 Jul 2016 19:32:31 -0400 Received: from mail-qt0-f171.google.com ([209.85.216.171]:34934 "EHLO mail-qt0-f171.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751190AbcGVXca (ORCPT ); Fri, 22 Jul 2016 19:32:30 -0400 Received: by mail-qt0-f171.google.com with SMTP id x25so70229222qtx.2 for ; Fri, 22 Jul 2016 16:32:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=4F/7jG+c1oK/h10j94FHPHFh9Kz8ozIa+2pWIJfghr4=; b=dJ4iVDLN6uVhKylVg15dRGuEfgW2Yfvhx5D47xAUTeeLjrbiwXym6+7sQy2L8LN2+k 6yC/+Bq/oXGpZVP8Ai6w5P3XW96ZiErjWBsZ7n2N8dWTUZF2GmaKhcKgYrLs9po8+VDx Tf/UiG2Wn+Q31Lwe9vHC/vZmz5qCqcoIilfR8= 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=4F/7jG+c1oK/h10j94FHPHFh9Kz8ozIa+2pWIJfghr4=; b=ImEC1vFK/qvALMvvUMtOUSagLN730MTonPSrp0kCyIznhHdNd7s6vLifTa/m+ydlCU mydQeZtrZ7YRh0NZ/H/Iy1bAxj7H/Bcnmfxvvj+7E60CawjBCLCbDm90DcS11LddyT1N wnZgzNJshGovm8veuc5EXn/NG2Pc0kXp+0ipIhk6ApFzmxCJO6beW/67pTlBXlnClafH 2IyiPhd9/oH3xCIr9rpkUDTYdJF8p5cWiGLMD7DOF9IhYdwOIbfzVdj7BXiarVeOAZh/ Nno9d94H+ricBTCIHb50AY0k3WffFsQkz8hJFjJSweQjbaEJ1lQyu8o3MJy73qFiZEpZ IzkA== X-Gm-Message-State: AEkooutIcvHOPUspU8UWelbMRjZqRwLKbFUJt7wu7O8VQcRnwpHTL6p0zdbv7IiHjEESh32x X-Received: by 10.200.54.107 with SMTP id n40mr10534562qtb.44.1469230344266; Fri, 22 Jul 2016 16:32:24 -0700 (PDT) Received: from lbrmn-mmayer.lab.lan ([216.31.219.19]) by smtp.gmail.com with ESMTPSA id c23sm8672867qtc.45.2016.07.22.16.32.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 22 Jul 2016 16:32:23 -0700 (PDT) From: Markus Mayer To: Andrew Morton , Al Viro , Rasmus Villemoes , Chris Metcalf , Kees Cook Cc: Markus Mayer , dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-acpi@vger.kernel.org, speakup@linux-speakup.org, devel@driverdev.osuosl.org, linux-scsi@vger.kernel.org, target-devel@vger.kernel.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 1/7] lib: string: add functions to case-convert strings Date: Fri, 22 Jul 2016 16:31:03 -0700 Message-Id: <1469230269-1644-2-git-send-email-mmayer@broadcom.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1469230269-1644-1-git-send-email-mmayer@broadcom.com> References: <1469230269-1644-1-git-send-email-mmayer@broadcom.com> Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add a collection of generic functions to convert strings to lowercase or uppercase. Changing the case of a string (with or without copying it first) seems to be a recurring requirement in the kernel that is currently being solved by several duplicated implementations doing the same thing. This change aims at reducing this code duplication. The new functions are int strlcpytoupper(char *dst, const char *src, size_t len); int strlcpytolower(char *dst, const char *src, size_t len); void strcpytoupper(char *dst, const char *src); void strcpytolower(char *dst, const char *src); void strtoupper(char *s); void strtolower(char *s); The "str[l]cpyto*" versions of the function take a destination string and a source string as arguments. The "strlcpyto*" versions additionally take a length argument like strlcpy() itself. Lastly, the strto* functions take a single string argument and modify the passed-in string. strlcpytoupper() and strlcpytolower() return the number of characters copied or -E2BIG if the destination string was truncated. Like strlcpy(), and unlike strncpy(), the functions guarantee NULL termination of the destination string. Signed-off-by: Markus Mayer --- include/linux/string.h | 40 ++++++++++++++++++++++++++++++++++++++++ lib/string.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/include/linux/string.h b/include/linux/string.h index 26b6f6a..257d797 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -116,6 +116,8 @@ extern void * memchr(const void *,int,__kernel_size_t); #endif void *memchr_inv(const void *s, int c, size_t n); char *strreplace(char *s, char old, char new); +extern int strlcpytoupper(char *dst, const char *src, size_t len); +extern int strlcpytolower(char *dst, const char *src, size_t len); extern void kfree_const(const void *x); @@ -169,4 +171,42 @@ static inline const char *kbasename(const char *path) return tail ? tail + 1 : path; } +/** + * strcpytoupper - Copy string and convert to uppercase. + * @dst: The buffer to store the result. + * @src: The string to convert to uppercase. + */ +static inline void strcpytoupper(char *dst, const char *src) +{ + strlcpytoupper(dst, src, ~(size_t)0); +} + +/** + * strcpytolower - Copy string and convert to lowercase. + * @dst: The buffer to store the result. + * @src: The string to convert to lowercase. + */ +static inline void strcpytolower(char *dst, const char *src) +{ + strlcpytolower(dst, src, ~(size_t)0); +} + +/** + * strtoupper - Convert string to uppercase. + * @s: The string to operate on. + */ +static inline void strtoupper(char *s) +{ + strlcpytoupper(s, s, ~(size_t)0); +} + +/** + * strtolower - Convert string to lowercase. + * @s: The string to operate on. + */ +static inline void strtolower(char *s) +{ + strlcpytolower(s, s, ~(size_t)0); +} + #endif /* _LINUX_STRING_H_ */ diff --git a/lib/string.c b/lib/string.c index ed83562..d36d5fb2 100644 --- a/lib/string.c +++ b/lib/string.c @@ -952,3 +952,49 @@ char *strreplace(char *s, char old, char new) return s; } EXPORT_SYMBOL(strreplace); + +/** + * strlcpytoupper - Copy a length-limited string and convert to uppercase. + * @dst: The buffer to store the result. + * @src: The string to convert to uppercase. + * @len: Maximum string length. May be SIZE_MAX to set no limit. + * + * Returns the number of characters copied or -E2BIG if @dst wasn't big enough. + */ +int strlcpytoupper(char *dst, const char *src, size_t len) +{ + size_t i; + + if (!len) + return -E2BIG; + + for (i = 0; i < len && src[i]; ++i) + dst[i] = toupper(src[i]); + dst[i < len ? i : i - 1] = '\0'; + + return (i < len) ? i : -E2BIG; +} +EXPORT_SYMBOL(strlcpytoupper); + +/** + * strlcpytolower - Copy a length-limited string and convert to lowercase. + * @dst: The buffer to store the result. + * @src: The string to convert to lowercase. + * @len: Maximum string length. May be SIZE_MAX to set no limit. + * + * Returns the number of characters copied or -E2BIG if @dst wasn't big enough. + */ +int strlcpytolower(char *dst, const char *src, size_t len) +{ + size_t i; + + if (!len) + return -E2BIG; + + for (i = 0; i < len && src[i]; ++i) + dst[i] = tolower(src[i]); + dst[i < len ? i : i - 1] = '\0'; + + return (i < len) ? i : -E2BIG; +} +EXPORT_SYMBOL(strlcpytolower);