Add strndup() function, if we don't have it
authorJens Axboe <axboe@kernel.dk>
Thu, 8 Jun 2017 15:37:52 +0000 (09:37 -0600)
committerJens Axboe <axboe@kernel.dk>
Thu, 8 Jun 2017 15:37:52 +0000 (09:37 -0600)
Signed-off-by: Jens Axboe <axboe@kernel.dk>
Makefile
configure
lib/pattern.c
oslib/strndup.c [new file with mode: 0644]
oslib/strndup.h [new file with mode: 0644]

index c3e551dffd1a2ef6f37c7bda521928180bb0e10e..a412a43b0f70b0437e3eb89f781a62562be203ac 100644 (file)
--- 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
index 2c6bfc874080721be8d1b730e94ad12f910aa265..929b3569d627782506a981acaadb7e773284a0d1 100755 (executable)
--- 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 <string.h>
+#include <stdlib.h>
+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
index 420d74a9a4338d56b605a7a6a5806466a582525a..31ee4eaf9896246b487cc5cccd4c62fb42b724f1 100644 (file)
@@ -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 (file)
index 0000000..318ca93
--- /dev/null
@@ -0,0 +1,18 @@
+#include <stdlib.h>
+#include <string.h>
+
+#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 (file)
index 0000000..669364e
--- /dev/null
@@ -0,0 +1,9 @@
+#ifdef CONFIG_HAVE_STRNDUP
+
+#include <string.h>
+
+#else
+
+char *strndup(const char *s, size_t n);
+
+#endif