util: inline strndup implementation in the header
[mesa.git] / src / util / strndup.h
index a3d739841322750ff996f0a3a72fc77af612bfde..6d1868370fd65906602ed3bcd63253a0fd196657 100644 (file)
 
 #if defined(_WIN32)
 
+#include <string.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-char *strndup(const char *str, size_t max);
+static inline char *
+strndup(const char *str, size_t max)
+{
+   size_t n;
+   char *ptr;
+
+   if (!str)
+      return NULL;
+
+   n = strnlen(str, max);
+   ptr = (char *) calloc(n + 1, sizeof(char));
+   if (!ptr)
+      return NULL;
+
+   memcpy(ptr, str, n);
+   return ptr;
+}
 
 #ifdef __cplusplus
 }
 #endif
 
-#endif
+#endif /* _WIN32 */
 
 #endif /* STRNDUP_H */