From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from integral2.. (unknown [182.2.70.161]) by gnuweeb.org (Postfix) with ESMTPSA id 8A8AE7E733; Tue, 29 Mar 2022 10:18:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1648549094; bh=FxtBrp49dfrBIXa72go2hDycnGwH5xLsPNTZN2AYVhY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Z4PjSn0zimerFsS+87aNyp/EZZoZauwsQvsU/2PIY2lVb7zfdJTrrk0UbA8wg5+eF Z4dC8ou5hL1Ijrnpyxs3Grepe3q0OsrDqeON2quFdTebEgVyt/1cEzrnalRByFD2ds bawZlJEq6iS0vYxCCOT48JabpWTPFJubpOZarNknj/nPw7sa8EpLw+9XIYNTd1jTKN HzJODGnYbqk8quAduX3IC0sit5YH1A9C1q63CuoFWLaCp7lLlSBaLAhGZZzbRrwNVz +bVJHfyWZbuJ3BKSIOSeTjGOK7J8wVh3Xbb6hZKjVUrfgxiNZUQnznhOpmOtDupdK/ 84I1LYdb2BbEA== From: Ammar Faizi To: Willy Tarreau Cc: Ammar Faizi , Alviro Iskandar Setiawan , Nugraha , "Paul E. McKenney" , Linux Kernel Mailing List , GNU/Weeb Mailing List Subject: [PATCH v2 8/9] tools/nolibc/string: Implement `strnlen()` Date: Tue, 29 Mar 2022 17:17:36 +0700 Message-Id: <20220329101737.58985-9-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220329101737.58985-1-ammarfaizi2@gnuweeb.org> References: <20220329101737.58985-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: size_t strnlen(const char *str, size_t maxlen); The strnlen() function returns the number of bytes in the string pointed to by sstr, excluding the terminating null byte ('\0'), but at most maxlen. In doing this, strnlen() looks only at the first maxlen characters in the string pointed to by str and never beyond str[maxlen-1]. The first use case of this function is for determining the memory allocation size in the strndup() function. Link: https://lore.kernel.org/lkml/CAOG64qMpEMh+EkOfjNdAoueC+uQyT2Uv3689_sOr37-JxdJf4g@mail.gmail.com Suggested-by: Alviro Iskandar Setiawan Signed-off-by: Ammar Faizi === @@ Changelog: Link v1: https://lore.gnuweeb.org/gwml/20220324073039.140946-11-ammarfaizi2@gnuweeb.org v1 -> v2: * No changes * Link v2: https://lore.kernel.org/lkml/20220322102115.186179-8-ammarfaizi2@gnuweeb.org RFC v2 -> v1: * No changes * --- tools/include/nolibc/string.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h index 75a453870498..f43d52a44d09 100644 --- a/tools/include/nolibc/string.h +++ b/tools/include/nolibc/string.h @@ -147,6 +147,15 @@ size_t nolibc_strlen(const char *str) #define strlen(str) nolibc_strlen((str)) #endif +static __attribute__((unused)) +size_t strnlen(const char *str, size_t maxlen) +{ + size_t len; + + for (len = 0; (len < maxlen) && str[len]; len++); + return len; +} + static __attribute__((unused)) size_t strlcat(char *dst, const char *src, size_t size) { -- Ammar Faizi