From 835c9df64b90dd08282766c73b1374fbc4132d97 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Fri, 11 Jan 2013 18:53:10 +0100 Subject: [PATCH] compat: add `strnlen()` which may be absent on some older systems --- configure.ac | 2 +- src/compat/compat.h | 4 ++++ src/compat/strnlen.c | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 src/compat/strnlen.c diff --git a/configure.ac b/configure.ac index 8d494fc3..1e86355e 100644 --- a/configure.ac +++ b/configure.ac @@ -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]), diff --git a/src/compat/compat.h b/src/compat/compat.h index 9f432866..0374a4ef 100644 --- a/src/compat/compat.h +++ b/src/compat/compat.h @@ -39,6 +39,10 @@ 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 index 00000000..8bc3525c --- /dev/null +++ b/src/compat/strnlen.c @@ -0,0 +1,14 @@ +/* -*- mode: c; c-file-style: "openbsd" -*- */ + +#include + +/* + * 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; +} -- 2.39.2