]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
compat: add `strnlen()` which may be absent on some older systems
authorVincent Bernat <vincent.bernat@dailymotion.com>
Fri, 11 Jan 2013 17:53:10 +0000 (18:53 +0100)
committerVincent Bernat <vincent.bernat@dailymotion.com>
Fri, 11 Jan 2013 17:53:10 +0000 (18:53 +0100)
configure.ac
src/compat/compat.h
src/compat/strnlen.c [new file with mode: 0644]

index 8d494fc3ebdfcc84d9ae7099707a8dd52e17b524..1e86355e26c3116897a095c4328b68e4b5306adf 100644 (file)
@@ -93,7 +93,7 @@ lldp_CHECK___PROGNAME
 AC_CONFIG_LIBOBJ_DIR([src/compat])
 AC_FUNC_MALLOC
 AC_FUNC_REALLOC
-AC_REPLACE_FUNCS([strlcpy])
+AC_REPLACE_FUNCS([strlcpy strnlen])
 AC_CHECK_FUNCS([setresuid setresgid])
 
 AC_SEARCH_LIBS([__res_init], resolv bind, AC_DEFINE([HAVE_RES_INIT], 1, [Define to indicate that res_init() exists]),
index 9f432866cbdb8a822c605619e6e0f6fe449ea63d..0374a4ef858ba42c1d538ad7a9ba73c030e94061 100644 (file)
 size_t strlcpy(char *, const char *, size_t);
 #endif
 
+#if !HAVE_STRNLEN
+size_t strnlen(const char *, size_t);
+#endif
+
 #if !HAVE_MALLOC
 void *malloc(size_t size);
 #endif
diff --git a/src/compat/strnlen.c b/src/compat/strnlen.c
new file mode 100644 (file)
index 0000000..8bc3525
--- /dev/null
@@ -0,0 +1,14 @@
+/* -*- mode: c; c-file-style: "openbsd" -*- */
+
+#include <string.h>
+
+/*
+ * Determine the length of a fixed-size string. This is really a
+ * wrapper around `memchr()`.
+ */
+size_t
+strnlen(const char *string, size_t maxlen)
+{
+       const char *end = memchr(string, '\0', maxlen);
+       return end?(size_t)(end - string):maxlen;
+}