From 251c93fdeca77a7bed0075bcd74d55ad741f7bbb Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Thu, 9 Jul 2020 14:01:01 +0100 Subject: win32: import strndup from gnulib --- include/mingw.h | 5 +++++ win32/Kbuild | 1 + win32/strndup.c | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 win32/strndup.c diff --git a/include/mingw.h b/include/mingw.h index c411a8106..c75b06330 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -199,6 +199,11 @@ int unsetenv(const char *env); #define putenv mingw_putenv #define mktemp mingw_mktemp +/* + * string.h + */ +char *strndup(char const *s, size_t n); + /* * strings.h */ diff --git a/win32/Kbuild b/win32/Kbuild index f8c8fb5af..f3d0244d2 100644 --- a/win32/Kbuild +++ b/win32/Kbuild @@ -20,6 +20,7 @@ lib-$(CONFIG_PLATFORM_MINGW32) += regex.o lib-$(CONFIG_PLATFORM_MINGW32) += select.o lib-$(CONFIG_FEATURE_PRNG_SHELL) += sh_random.o lib-$(CONFIG_PLATFORM_MINGW32) += statfs.o +lib-$(CONFIG_PLATFORM_MINGW32) += strndup.o lib-$(CONFIG_PLATFORM_MINGW32) += strptime.o lib-$(CONFIG_PLATFORM_MINGW32) += system.o lib-$(CONFIG_PLATFORM_MINGW32) += termios.o diff --git a/win32/strndup.c b/win32/strndup.c new file mode 100644 index 000000000..4d04609f6 --- /dev/null +++ b/win32/strndup.c @@ -0,0 +1,36 @@ +/* A replacement function, for systems that lack strndup. + + Copyright (C) 1996-1998, 2001-2003, 2005-2007, 2009-2020 Free Software + Foundation, Inc. + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . */ + +#include "libbb.h" + +#include + +#include + +char * +strndup (char const *s, size_t n) +{ + size_t len = strnlen (s, n); + char *new = malloc (len + 1); + + if (new == NULL) + return NULL; + + new[len] = '\0'; + return memcpy (new, s, len); +} -- cgit v1.2.3-55-g6feb