Synopsis
char *strndup(const char *s1,
              size_t n);
Description

strndup duplicates at most n characters from the the string pointed to by s1 by using malloc to allocate memory for a copy of s1.

If the length of string pointed to by s1 is greater than n characters, only n characters will be duplicated. If n is greater than the length of string pointed to by s1, all characters in the string are copied into the allocated array including the terminating null character.

strndup returns a pointer to the new string or a null pointer if the new string cannot be created. The returned pointer can be passed to free.

Note

strndup conforms to POSIX.1-2008 and SC22 TR 24731-2.