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.69.158]) by gnuweeb.org (Postfix) with ESMTPSA id 518A57E33D; Tue, 22 Mar 2022 10:21:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1647944520; bh=hiJ9XJQoRgMa4/fOsRSCSX7gvGvKUHkqA0mcpuI4lqQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EiId7k7nK05JcFz25EpP9QwMckMfGq8uRCIBPB502ocHCAJcJs/TMlGzyrNVjAP6X 9x5LBwkE6kgRu8yXphis65A8JsYykGjUw/VgNSCwmyPZKsQ5J1lJ7dtVuCvysG9wJ/ VYO4vNaNqa8L1cyohARBq/OmIpm40LniLsNk2wZDWlrBeshuNVZZ1Xd2xasKfg2tww nfQcZicD+NmVWJfHs5oM8Girks6EsAV/M0g7EGrJg93/mogpafTBU857OnB5fjls0j KO/BYpyhByauLwl6zYOAaVtxFPJ8sUQOmQQuzaqVStMrOc84H2oZ4dWhB2Yx47ALh1 d6O4rSwBoXTQQ== 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: [RFC PATCH v2 7/8] tools/nolibc/string: Implement `strnlen()` Date: Tue, 22 Mar 2022 17:21:14 +0700 Message-Id: <20220322102115.186179-8-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220322102115.186179-1-ammarfaizi2@gnuweeb.org> References: <20220322102115.186179-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 --- 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 0d5e870c7c0b..1426eefc1ef2 100644 --- a/tools/include/nolibc/string.h +++ b/tools/include/nolibc/string.h @@ -138,6 +138,15 @@ size_t nolibc_strlen(const char *str) nolibc_strlen((str)); \ }) +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