From patchwork Fri Jul 8 22:43:08 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Mayer X-Patchwork-Id: 9221891 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 2910260467 for ; Fri, 8 Jul 2016 22:45:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 193B6285F0 for ; Fri, 8 Jul 2016 22:45:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0D501285F4; Fri, 8 Jul 2016 22:45:57 +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 96BA4285F0 for ; Fri, 8 Jul 2016 22:45:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932758AbcGHWoJ (ORCPT ); Fri, 8 Jul 2016 18:44:09 -0400 Received: from mail-pa0-f50.google.com ([209.85.220.50]:35966 "EHLO mail-pa0-f50.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932493AbcGHWoF (ORCPT ); Fri, 8 Jul 2016 18:44:05 -0400 Received: by mail-pa0-f50.google.com with SMTP id hu1so3783333pad.3 for ; Fri, 08 Jul 2016 15:44:04 -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=xImHYWwuXaxVCSsCPeS98DcV/OJ/Oqgs4S0wOkZQKTs=; b=bjGH2MaKUhX9Ln5SzGy1cz0QU0HcFofQCfC6aPRCTSkNOd82PXwGkWYmdJTpoXFP1o yXrbnD0y1y0C9ENC8po/8ooseFZd5AZ6Ve8UvCHOW2vn2ZzEpYZtUge/MJUlPUtlFChG oig0EFfsIo6VsI0tDCBRAUht6D+avOK7ABVDM= 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=xImHYWwuXaxVCSsCPeS98DcV/OJ/Oqgs4S0wOkZQKTs=; b=FOvu54TEkjE/YtBVqiL1WvRmMDsKDWgQNkKB34bdzj96m33FOjWe63OVR4cQkS7M6B 1f/jFScLvbqGynntU7WfdaEBi9ph61oxoXmboF48/BRauP4Ttg4Zrw0z2Rn5Kf7RRUg4 njB/FrtdQSO05unjcVLrhkSj4KJTLhgS42SeIlHFX0/AQxtQy9pW83+bIgxyL0dfqyON TQkO2ggj5U2zMxL3L/PCr+06uSqpcKY+MnBfbXTV3aUsVBU0GnPCnYoCKHrG4ZQ+Mmpn OLvrO+NJctepkdsxASVHl/Ry0+L3cJGb/wEzwuW+shjRCs+0qIhdxYLBNS9k517bgB1K KwyQ== X-Gm-Message-State: ALyK8tIf2fu2jd+6ZGCaY4BzVxCWQyLzEiaeykQ38pdNjjAgOYxyTQ2JXfj6f5+Dfc00MOpe X-Received: by 10.66.80.2 with SMTP id n2mr6758091pax.134.1468017843507; Fri, 08 Jul 2016 15:44:03 -0700 (PDT) Received: from lbrmn-mmayer.lab.lan ([216.31.219.19]) by smtp.gmail.com with ESMTPSA id br6sm5796387pac.0.2016.07.08.15.44.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 08 Jul 2016 15:44:02 -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 v3 1/7] lib: string: add functions to case-convert strings Date: Fri, 8 Jul 2016 15:43:08 -0700 Message-Id: <1468017794-4818-2-git-send-email-mmayer@broadcom.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1468017794-4818-1-git-send-email-mmayer@broadcom.com> References: <1468017794-4818-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 void strlcpytoupper(char *dst, const char *src, size_t len); void 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. 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 | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/include/linux/string.h b/include/linux/string.h index 26b6f6a..36c9d14 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 void strlcpytoupper(char *dst, const char *src, size_t len); +extern void 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, -1); +} + +/** + * 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, -1); +} + +/** + * strtoupper - Convert string to uppercase. + * @s: The string to operate on. + */ +static inline void strtoupper(char *s) +{ + strlcpytoupper(s, s, -1); +} + +/** + * strtolower - Convert string to lowercase. + * @s: The string to operate on. + */ +static inline void strtolower(char *s) +{ + strlcpytolower(s, s, -1); +} + #endif /* _LINUX_STRING_H_ */ diff --git a/lib/string.c b/lib/string.c index ed83562..fd8c427 100644 --- a/lib/string.c +++ b/lib/string.c @@ -952,3 +952,41 @@ 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 (-1) to set no limit. + */ +void strlcpytoupper(char *dst, const char *src, size_t len) +{ + size_t i; + + if (!len) + return; + + for (i = 0; i < len && src[i]; ++i) + dst[i] = toupper(src[i]); + dst[i < len ? i : i - 1] = '\0'; +} +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 (-1) to set no limit. + */ +void strlcpytolower(char *dst, const char *src, size_t len) +{ + size_t i; + + if (!len) + return; + + for (i = 0; i < len && src[i]; ++i) + dst[i] = tolower(src[i]); + dst[i < len ? i : i - 1] = '\0'; +} +EXPORT_SYMBOL(strlcpytolower);