AC_FUNC_MALLOC
AC_FUNC_REALLOC
AC_SEARCH_LIBS([setproctitle], [util bsd])
-AC_REPLACE_FUNCS([strlcpy strnlen fgetln setproctitle])
+AC_REPLACE_FUNCS([strlcpy strnlen strndup fgetln setproctitle])
AC_CHECK_FUNCS([setresuid setresgid])
case " $LIBS " in
size_t strnlen(const char *, size_t);
#endif
+#if !HAVE_STRNDUP
+char *strndup(const char *, size_t);
+#endif
+
#if !HAVE_FGETLN
char *fgetln(FILE *, size_t *);
#endif
--- /dev/null
+/* -*- mode: c; c-file-style: "openbsd" -*- */
+
+#include <stdlib.h>
+#include <string.h>
+
+/*
+ * Similar to `strdup()` but copies at most n bytes.
+ */
+char*
+strndup(const char *string, size_t maxlen)
+{
+ char *result;
+ /* We may use `strnlen()` but it may be unavailable. */
+ const char *end = memchr(string, '\0', maxlen);
+ size_t len = end?(size_t)(end - string):maxlen;
+
+ result = malloc(len + 1);
+ if (!result) return 0;
+
+ memcpy(result, string, len);
+ return result;
+}