From 4e5151659401c43679ca2eaa2968650fa051d930 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Tue, 1 Sep 2020 15:39:57 +0200 Subject: [PATCH] configure.ac: Check for strnlen, implement alternative. Solaris 10 doesn't provide a strnlen(3). Provide a simple alternative if so. --- configure.ac | 1 + src/utils/common/common.c | 20 +++++++++++--------- src/utils/common/common.h | 6 +++++- src/utils/strbuf/strbuf.c | 11 +++++++++++ 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/configure.ac b/configure.ac index 45b94ce0d..19c386749 100644 --- a/configure.ac +++ b/configure.ac @@ -920,6 +920,7 @@ AC_CHECK_FUNCS([host_statistics], [have_host_statistics="yes"], [have_host_stati AC_CHECK_FUNCS([processor_info], [have_processor_info="yes"], [have_processor_info="no"]) AC_CHECK_FUNCS([statfs], [have_statfs="yes"], [have_statfs="no"]) AC_CHECK_FUNCS([statvfs], [have_statvfs="yes"], [have_statvfs="no"]) +AC_CHECK_FUNCS([strnlen], [have_strnlen="yes"], [have_strnlen="no"]) AC_CHECK_FUNCS([sysctl], [have_sysctl="yes"], [have_sysctl="no"]) AC_CHECK_FUNCS([sysctlbyname], [have_sysctlbyname="yes"], [have_sysctlbyname="no"]) AC_CHECK_FUNCS([syslog], [have_syslog="yes"], [have_syslog="no"]) diff --git a/src/utils/common/common.c b/src/utils/common/common.c index 89fe6f52b..eace40f3d 100644 --- a/src/utils/common/common.c +++ b/src/utils/common/common.c @@ -162,14 +162,16 @@ char *sstrdup(const char *s) { return r; } /* char *sstrdup */ -size_t sstrnlen(const char *s, size_t n) { - const char *p = s; - - while (n-- > 0 && *p) - p++; - - return p - s; -} /* size_t sstrnlen */ +#if !HAVE_STRNLEN +size_t strnlen(const char *s, size_t maxlen) { + for (size_t i = 0; i < maxlen; i++) { + if (s[i] == 0) { + return i; + } + } + return maxlen; +} +#endif char *sstrndup(const char *s, size_t n) { char *r; @@ -178,7 +180,7 @@ char *sstrndup(const char *s, size_t n) { if (s == NULL) return NULL; - sz = sstrnlen(s, n); + sz = strnlen(s, n); r = malloc(sz + 1); if (r == NULL) { ERROR("sstrndup: Out of memory."); diff --git a/src/utils/common/common.h b/src/utils/common/common.h index b7c9c08ca..2812644a2 100644 --- a/src/utils/common/common.h +++ b/src/utils/common/common.h @@ -73,7 +73,11 @@ __attribute__((format(printf, 1, 2))) char *ssnprintf_alloc(char const *format, ...); char *sstrdup(const char *s); -size_t sstrnlen(const char *s, size_t n); + +#if !HAVE_STRNLEN +size_t strnlen(const char *s, size_t maxlen); +#endif + char *sstrndup(const char *s, size_t n); void *smalloc(size_t size); char *sstrerror(int errnum, char *buf, size_t buflen); diff --git a/src/utils/strbuf/strbuf.c b/src/utils/strbuf/strbuf.c index d72db6a78..3e4db07a4 100644 --- a/src/utils/strbuf/strbuf.c +++ b/src/utils/strbuf/strbuf.c @@ -200,6 +200,17 @@ int strbuf_printf(strbuf_t *buf, char const *format, ...) { return 0; } +#if !HAVE_STRNLEN +static size_t strnlen(const char *s, size_t maxlen) { + for (size_t i = 0; i < maxlen; i++) { + if (s[i] == 0) { + return i; + } + } + return maxlen; +} +#endif + int strbuf_printn(strbuf_t *buf, char const *s, size_t n) { if ((buf == NULL) || (s == NULL)) return EINVAL; -- 2.47.2