]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: tools: add strnlen2() helper
authorAurelien DARRAGON <adarragon@haproxy.com>
Fri, 25 Oct 2024 15:04:37 +0000 (17:04 +0200)
committerAurelien DARRAGON <adarragon@haproxy.com>
Mon, 28 Oct 2024 13:59:35 +0000 (14:59 +0100)
strnlen2() is functionally equivalent to strnlen(). Goal is to provide
an alternative to strnlen() which is not portable since it requires
_POSIX_C_SOURCE >= 200809L

include/haproxy/tools.h

index 8d1afb5d7c3bb48279460b0f422405f11aa87a9c..0bade0fd19ad5351962b371c07d2e0365fea5f7a 100644 (file)
  */
 extern int strlcpy2(char *dst, const char *src, int size);
 
+/*
+ * portable equivalent to POSIX strnlen():
+ * returns the number of bytes in the string pointed to by <s>, excluding
+ * the terminating null byte, but at most <maxlen>. The function does not
+ * look at characters passed <maxlen>.
+ */
+static inline size_t strnlen2(const char *s, size_t maxlen)
+{
+       size_t len;
+
+       for (len = 0; len < maxlen && s[len]; len++)
+               ;
+       return len;
+}
+
 /*
  * This function simply returns a locally allocated string containing
  * the ascii representation for number 'n' in decimal.