summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorglx <glx@openttd.org>2008-08-24 17:02:21 +0000
committerglx <glx@openttd.org>2008-08-24 17:02:21 +0000
commit2ddb86eea5c42d235497fa1267408db42af254b3 (patch)
tree4d583b36854c8c29c4b2c087d4a80ea73477af39
parentae32d158cbfc28faa48095025a2e65b00dae1d3b (diff)
downloadopenttd-2ddb86eea5c42d235497fa1267408db42af254b3.tar.xz
(svn r14154) -Fix (r14153): strndup is a GNU extension, so it doesn't exist on all platforms
-rw-r--r--src/stdafx.h5
-rw-r--r--src/string.cpp11
-rw-r--r--src/string_func.h7
3 files changed, 22 insertions, 1 deletions
diff --git a/src/stdafx.h b/src/stdafx.h
index f6ef278c4..5ca14d082 100644
--- a/src/stdafx.h
+++ b/src/stdafx.h
@@ -145,6 +145,11 @@
#include <malloc.h> // alloca()
#endif
+#if defined(__MINGW32__) && defined(_GNU_SOURCE)
+ /* For some weird reasons, SDL defines _GNU_SOURCE */
+ #undef _GNU_SOURCE
+#endif
+
#if defined(WIN32)
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#endif
diff --git a/src/string.cpp b/src/string.cpp
index 041fa8e45..9173dc4a5 100644
--- a/src/string.cpp
+++ b/src/string.cpp
@@ -304,3 +304,14 @@ size_t Utf8TrimString(char *s, size_t maxlen)
*s = '\0';
return length;
}
+
+#ifndef _GNU_SOURCE
+#include "core/math_func.hpp"
+char *strndup(const char *s, size_t len)
+{
+ len = min(strlen(s), len);
+ char *tmp = CallocT<char>(len + 1);
+ memcpy(tmp, s, len);
+ return tmp;
+}
+#endif /* !_GNU_SOURCE */
diff --git a/src/string_func.h b/src/string_func.h
index 82530ae65..7bc2d6c95 100644
--- a/src/string_func.h
+++ b/src/string_func.h
@@ -156,4 +156,9 @@ static inline bool IsWhitespace(WChar c)
;
}
-#endif /* STRING_FUNC_H */
+#ifndef _GNU_SOURCE
+/* strndup is a GNU extension */
+char *strndup(const char *s, size_t len);
+#endif /* WIN32 || SUNOS */
+
+#endif /* !_GNU_SOURCE */