summaryrefslogtreecommitdiff
path: root/common/compat.c
diff options
context:
space:
mode:
authorStef Walter <stefw@gnome.org>2013-03-03 09:52:21 +0100
committerStef Walter <stefw@gnome.org>2013-03-03 10:07:14 +0100
commit66ee55e5947682d10eed7a36b9da72a8cf6a40f2 (patch)
treec31d63c6149d67470f928151344643bbe4f3d194 /common/compat.c
parentae76545a0094114ef29dba52df97e69ab28b3dbc (diff)
Add compat strndup() function
Not available on Win32 and ancient unixes
Diffstat (limited to 'common/compat.c')
-rw-r--r--common/compat.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/common/compat.c b/common/compat.c
index af28e1d..ff8ee08 100644
--- a/common/compat.c
+++ b/common/compat.c
@@ -424,6 +424,28 @@ memdup (const void *data,
#endif /* HAVE_MEMDUP */
+#ifndef HAVE_STRNDUP
+
+char *
+strndup (const char *data,
+ size_t length)
+{
+ char *ret;
+ size_t len;
+
+ len = strlen (data);
+ if (length > len)
+ length = len;
+
+ ret = memdup (data, length + 1);
+ if (ret != NULL)
+ ret[length] = 0;
+
+ return ret;
+}
+
+#endif /* HAVE_STRDUP */
+
#ifndef HAVE_STRCONCAT
#include <stdarg.h>