From b29c71c4a1e96390051d012b4e35c8d0718f7ce3 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Thu, 8 Jun 2017 09:37:52 -0600 Subject: [PATCH 1/1] Add strndup() function, if we don't have it Signed-off-by: Jens Axboe --- Makefile | 3 +++ configure | 21 +++++++++++++++++++++ lib/pattern.c | 1 + oslib/strndup.c | 18 ++++++++++++++++++ oslib/strndup.h | 9 +++++++++ 5 files changed, 52 insertions(+) create mode 100644 oslib/strndup.c create mode 100644 oslib/strndup.h diff --git a/Makefile b/Makefile index c3e551df..a412a43b 100644 --- a/Makefile +++ b/Makefile @@ -107,6 +107,9 @@ endif ifndef CONFIG_STRLCAT SOURCE += oslib/strlcat.c endif +ifndef CONFIG_HAVE_STRNDUP + SOURCE += oslib/strndup.c +endif ifndef CONFIG_GETOPT_LONG_ONLY SOURCE += oslib/getopt_long.c endif diff --git a/configure b/configure index 2c6bfc87..929b3569 100755 --- a/configure +++ b/configure @@ -1970,6 +1970,24 @@ if compile_prog "" "" "bool"; then fi print_config "bool" "$have_bool" +########################################## +# Check whether we have strndup() +cat > $TMPC << EOF +#include +#include +int main(int argc, char **argv) +{ + char *res = strndup("test string", 8); + + free(res); + return 0; +} +EOF +if compile_prog "" "" "strndup"; then + strndup="yes" +fi +print_config "strndup" "$strndup" + ########################################## # check march=armv8-a+crc+crypto if test "$march_armv8_a_crc_crypto" != "yes" ; then @@ -2227,6 +2245,9 @@ fi if test "$have_bool" = "yes" ; then output_sym "CONFIG_HAVE_BOOL" fi +if test "$strndup" = "yes" ; then + output_sym "CONFIG_HAVE_STRNDUP" +fi if test "$disable_opt" = "yes" ; then output_sym "CONFIG_DISABLE_OPTIMIZATIONS" fi diff --git a/lib/pattern.c b/lib/pattern.c index 420d74a9..31ee4eaf 100644 --- a/lib/pattern.c +++ b/lib/pattern.c @@ -13,6 +13,7 @@ #include "pattern.h" #include "../minmax.h" #include "../oslib/strcasestr.h" +#include "../oslib/strndup.h" /** * parse_file() - parses binary file to fill buffer diff --git a/oslib/strndup.c b/oslib/strndup.c new file mode 100644 index 00000000..318ca933 --- /dev/null +++ b/oslib/strndup.c @@ -0,0 +1,18 @@ +#include +#include + +#ifndef CONFIG_HAVE_STRNDUP + +char *strndup(const char *s, size_t n) +{ + char *str = malloc(n + 1); + + if (str) { + strncpy(str, s, n); + str[n] = '\0'; + } + + return str; +} + +#endif diff --git a/oslib/strndup.h b/oslib/strndup.h new file mode 100644 index 00000000..669364e4 --- /dev/null +++ b/oslib/strndup.h @@ -0,0 +1,9 @@ +#ifdef CONFIG_HAVE_STRNDUP + +#include + +#else + +char *strndup(const char *s, size_t n); + +#endif -- 2.25.1