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.71.236]) by gnuweeb.org (Postfix) with ESMTPSA id 085E67E713; Thu, 24 Mar 2022 07:31:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1648107085; bh=v3/gvQsbdB6gVOFXY4Hp+dHuM9qN2h7z2eGiF+eRI6k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BC/nROz59xu6KERhODQ7gvkBIhQ8sVd/U06p7f859dOyJZcUhxER7YARMCf9eEfuL K8zQulNlAZW8XelSktxdeZBteYY/GZeghtq+C1yDTIErF/VcVQDAfo7WDV+I/IrmPC 1bGLVnfXfJOwQvRAUUaXANooMEbQ4BtCgbOhq3Lfu65pHuVRbqfroerO2bzzovbwVg oKX34wzFrFYxbQBLb2EjgGe2HNoYlpRBGXsdx0rYgrZLy7u8CrEu8ygBn305arJ2JE MntlKJUNfuxG50JAUBH2zebFd3tDsbLmZZn7zG+dhj3uQ/SLbbLOAND0CFaLet8F0L BoIdRkfZmcncQ== From: Ammar Faizi To: Willy Tarreau Cc: "Paul E. McKenney" , Alviro Iskandar Setiawan , Nugraha , Linux Kernel Mailing List , GNU/Weeb Mailing List , Ammar Faizi Subject: [PATCH v1 10/11] tools/nolibc/string: Implement `strnlen()` Date: Thu, 24 Mar 2022 14:30:38 +0700 Message-Id: <20220324073039.140946-11-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220324073039.140946-1-ammarfaizi2@gnuweeb.org> References: <20220324073039.140946-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 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