... this one is even smaller. Plus it uses the familiar strlen() function:
size_t strnlen(const char *string, size_t max_count)
{
return strlen(string) < max_count ? strlen(string) : max_count;
}
Please do not use that implementation.
The major goal of strnlen is to avoid looking beyond &string[max_count].
strlen(string) looks all the way to the end, which may be very much longer than
max_count;
and which may cause SIGSEGV by running into a memory page that does not exist
before the terminating '\0' is found.
[Besides, some compilers do not recognize that "strlen(string)"
need not be evaluated twice.]
--