util/format: Use SATURATE
[mesa.git] / src / util / strndup.h
index 54346823596e760387abcbb92d98ba00fb298da4..dcaa429dbe81b1b0fdb6d537972d93f41e73695e 100644 (file)
  * IN THE SOFTWARE.
  */
 
-#pragma once
+#ifndef STRNDUP_H
+#define STRNDUP_H
+
+#if defined(_WIN32)
 
 #include <stdlib.h> // size_t
+#include <string.h>
 
-#if defined(_WIN32)
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+static inline char *
+strndup(const char *str, size_t max)
+{
+   size_t n;
+   char *ptr;
 
-extern char *strndup(const char *str, size_t max);
+   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 /* _WIN32 */
+
+#endif /* STRNDUP_H */