[Piglit] [PATCH] util: Add an implementation of strndup

Ian Romanick idr at freedesktop.org
Thu Sep 5 10:45:58 PDT 2013


From: Ian Romanick <ian.d.romanick at intel.com>

Commit 4c617fa7 added a use of strndup, and that broke this bulid with
various Windows compilers that don't support strndup.

Signed-off-by: Ian Romanick <ian.d.romanick at intel.com>
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=68780
Cc: Vinson Lee <vlee at freedesktop.org>
---
 CMakeLists.txt           |  1 +
 tests/util/piglit-util.c | 17 +++++++++++++++++
 tests/util/piglit-util.h |  4 ++++
 3 files changed, 22 insertions(+)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 13a984c..1ea2c80 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -255,6 +255,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL ${piglit_BINARY_DIR}/bin)
 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${piglit_BINARY_DIR}/bin)
 
 check_function_exists(strchrnul HAVE_STRCHRNUL)
+check_function_exists(strndup   HAVE_STRNDUP)
 check_function_exists(fopen_s   HAVE_FOPEN_S)
 check_function_exists(setrlimit HAVE_SETRLIMIT)
 
diff --git a/tests/util/piglit-util.c b/tests/util/piglit-util.c
index 98fffa2..71d55a7 100644
--- a/tests/util/piglit-util.c
+++ b/tests/util/piglit-util.c
@@ -216,6 +216,23 @@ char *strchrnul(const char *s, int c)
 #endif
 
 
+#ifndef HAVE_STRNDUP
+char *strndup(const char *s, size_t n)
+{
+	const size_t len = strlen(s);
+	const size_t size_to_copy = MIN2(n, len);
+
+	char *const copy = malloc(size_to_copy + 1);
+	if (copy != NULL) {
+		memcpy(copy, s, size_to_copy);
+		copy[size_to_copy] = '\0';
+	}
+
+	return copy;
+}
+#endif
+
+
 void
 piglit_set_rlimit(unsigned long lim)
 {
diff --git a/tests/util/piglit-util.h b/tests/util/piglit-util.h
index f89eb6e..52f053e 100644
--- a/tests/util/piglit-util.h
+++ b/tests/util/piglit-util.h
@@ -144,6 +144,10 @@ void piglit_report_subtest_result(enum piglit_result result,
 char *strchrnul(const char *s, int c);
 #endif
 
+#ifndef HAVE_STRNDUP
+char *strndup(const char *s, size_t n);
+#endif
+
 extern void piglit_set_rlimit(unsigned long lim);
 
 char *piglit_load_text_file(const char *file_name, unsigned *size);
-- 
1.8.1.4



More information about the Piglit mailing list