+
+#if ! HAVE_STRNLEN
+static size_t
+strnlen (const char *s,
+ size_t n)
+{
+ const char *e;
+
+ e = memchr (s, '\0', n);
+ if (NULL == e)
+ return n;
+ return e - s;
+}
+#endif
+
+
+/**
+ * Dup partially a string (same semantics as strndup).
+ *
+ * @param str the string to dup
+ * @param len the length of the string to dup
+ * @param filename where in the code was the call to GNUNET_strndup
+ * @param linenumber where in the code was the call to GNUNET_strndup
+ * @return strndup(str,len)
+ */
+char *
+GNUNET_xstrndup_ (const char *str, size_t len, const char *filename,
+ int linenumber)
+{
+ char *res;
+
+ if (0 == len)
+ return GNUNET_strdup ("");
+ GNUNET_assert_at (str != NULL, filename, linenumber);
+ len = strnlen (str, len);
+ res = GNUNET_xmalloc_ (len + 1, filename, linenumber);
+ memcpy (res, str, len);
+ /* res[len] = '\0'; 'malloc' zeros out anyway */
+ return res;
+}
+
+