From patchwork Wed Aug 10 21:04:51 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Mayer X-Patchwork-Id: 9274133 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 ABC7E6075E for ; Wed, 10 Aug 2016 21:08:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9930A27FC0 for ; Wed, 10 Aug 2016 21:08:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8DBE128409; Wed, 10 Aug 2016 21:08:25 +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 E4911283E9 for ; Wed, 10 Aug 2016 21:08:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932868AbcHJVF4 (ORCPT ); Wed, 10 Aug 2016 17:05:56 -0400 Received: from mail-qt0-f177.google.com ([209.85.216.177]:36556 "EHLO mail-qt0-f177.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932910AbcHJVFb (ORCPT ); Wed, 10 Aug 2016 17:05:31 -0400 Received: by mail-qt0-f177.google.com with SMTP id 52so27166753qtq.3 for ; Wed, 10 Aug 2016 14:05:31 -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=W/emPouL0bSRRY9Z6fRjmVEHBGrXiZS1Bhl8vUnRC1wUZDIeUTms3AOLjX1BXY2Jh+ UNIQqtD+Oqqdc31kmy41sV5a9UhQY/NzZgqQAJliCaUUfErEXdI3e3g9XyOIAZSK+k0y cHZSpx1p52lKBoIYtnbDcpLJX3hOr1N7pnhBA= 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=EJdAWCKj/uDak7wDYlA24+iBlBkOv2Q9Axkz237fkJPhQFuhqxxWuuMBZ3ogLeus6O e1BPRX2rcNRUxnvb6NBpY7RnkbCpwD/YH9DsLFl2Nc++kQZ58ckhm4S9/OgsPT1tmJLt Qdu0K48B9xj55Sa//81VDLNsf+QqDxd/fcoQwCgBCeVLTwHtH6MC+llmtOxzM05rZy6O DKXaz0yFtMUXs/CN1MHhSyxN5FCPBRk3bER5T7B1sKif2R5WEN5Hv3xJUwIxrK2RT13J zfHT80ny/dsDMZzG6sG2KMQ863GBjSiyyWxl797Vs9rcHKLBGu9DIBhsy4PLjPAMUt2/ ATcg== X-Gm-Message-State: AEkooutM2vEnxBWLPo9BGGdBg095cB6HCCbmthrOzmicNkbDeRRTD7jzZyeBUDiiRRBrQUa9 X-Received: by 10.237.42.197 with SMTP id t63mr7030276qtd.84.1470863130113; Wed, 10 Aug 2016 14:05:30 -0700 (PDT) Received: from lbrmn-mmayer.lab.lan ([216.31.219.19]) by smtp.gmail.com with ESMTPSA id t42sm24370553qte.24.2016.08.10.14.05.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 10 Aug 2016 14:05:29 -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 v5 1/7] lib: string: add functions to case-convert strings Date: Wed, 10 Aug 2016 14:04:51 -0700 Message-Id: <1470863097-21138-2-git-send-email-mmayer@broadcom.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1470863097-21138-1-git-send-email-mmayer@broadcom.com> References: <1470863097-21138-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);