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 7BFB97E714; Thu, 24 Mar 2022 07:31:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1648107089; bh=PD8pLr7A85iII21QaBO68Pb6e6bGmBeN0QNU1aruB6M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=R72WQfG/Ih8sBMAoKTsXCsRP8bJGHyRhUkTzyT1ugZuOw4ifSlrpseddB1nSYVjYw /jNHSFe2sj+fRex7kgMO0K7WTxXZOz7raFjEDIO8KaAUoBPotx2gli11oz3eFUXnWi Yv71wv5hanE9vJtENPVziJEEYmicGQdc5gGCHegy0vx2CPIYSGtB9SOGMT0Fn4Cs+Z SLy9iAN4U5//GFQvSAvIorqjq3bJwaPbh7F/hLSt40oHWZy/UqhJc9MX0MkL1h0rSz nZJx4kXlSh/f9/WQZvwfwnLoGfVIqF/up1wEuElVRJzn0BhEiyIcRBebiheC8PXfTH I4bEg95n+Qf+w== 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 11/11] tools/include/string: Implement `strdup()` and `strndup()` Date: Thu, 24 Mar 2022 14:30:39 +0700 Message-Id: <20220324073039.140946-12-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: These functions are currently only available on architectures that have my_syscall6() macro implemented. Since these functions use malloc(), malloc() uses mmap(), mmap() depends on my_syscall6() macro. On architectures that don't support my_syscall6(), these function will always return NULL with errno set to ENOSYS. Signed-off-by: Ammar Faizi --- @@ Changelog: Link RFC v2: https://lore.kernel.org/lkml/20220322102115.186179-9-ammarfaizi2@gnuweeb.org/ RFC v2 -> v1: * No changes * Link RFC v1: https://lore.kernel.org/lkml/20220320093750.159991-7-ammarfaizi2@gnuweeb.org/ RFC v1 -> RFC v2: - Update strdup and strndup implementation, use strlen and strnlen to get the string length first (comment from Willy and Alviro). - Fix the subject line prefix, it was "tools/include/string: ", it should be "tools/nolibc/string: ". - Update the commit message. --- tools/include/nolibc/string.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h index f43d52a44d09..bef35bee9c44 100644 --- a/tools/include/nolibc/string.h +++ b/tools/include/nolibc/string.h @@ -9,6 +9,8 @@ #include "std.h" +static void *malloc(size_t len); + /* * As much as possible, please keep functions alphabetically sorted. */ @@ -156,6 +158,36 @@ size_t strnlen(const char *str, size_t maxlen) return len; } +static __attribute__((unused)) +char *strdup(const char *str) +{ + size_t len; + char *ret; + + len = strlen(str); + ret = malloc(len + 1); + if (__builtin_expect(ret != NULL, 1)) + memcpy(ret, str, len + 1); + + return ret; +} + +static __attribute__((unused)) +char *strndup(const char *str, size_t maxlen) +{ + size_t len; + char *ret; + + len = strnlen(str, maxlen); + ret = malloc(len + 1); + if (__builtin_expect(ret != NULL, 1)) { + memcpy(ret, str, len); + ret[len] = '\0'; + } + + return ret; +} + static __attribute__((unused)) size_t strlcat(char *dst, const char *src, size_t size) { -- Ammar Faizi