Skip to content

Commit

Permalink
string: add bson_strnlen() for systems without strnlen().
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Hergert committed Feb 9, 2014
1 parent 33be947 commit 367b45f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
24 changes: 24 additions & 0 deletions bson/bson-string.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/


#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdarg.h>
#include <string.h>

Expand Down Expand Up @@ -264,3 +268,23 @@ bson_strfreev (char **str)
bson_free (str);
}
}


size_t
bson_strnlen (const char *s,
size_t maxlen)
{
#ifdef HAVE_STRNLEN
return strnlen (s, maxlen);
#else
size_t i;

for (i = 0; i < maxlen; i++) {
if (s [i] == '\0') {
return i + 1;
}
}

return maxlen;
#endif
}
3 changes: 3 additions & 0 deletions bson/bson-string.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ bson_strndup (const char *str,
void bson_strfreev (char **strv);


size_t bson_strnlen (const char *s, size_t maxlen);


BSON_END_DECLS


Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ AC_CHECK_FUNCS([clock_gettime], [CLOCK_LIB=],
[CLOCK_LIB=])])
AC_SUBST([CLOCK_LIB])

AC_CHECK_FUNCS(posix_memalign memalign)
AC_CHECK_FUNCS(posix_memalign memalign strnlen)


dnl **************************************************************************
Expand Down

0 comments on commit 367b45f

Please sign in to comment.