c/string/byte/strndup

Returns a pointer to a null-terminated byte string, which contains copies of at most bytes from the string pointed to by. The space for the new string is obtained as if malloc was called. If the null terminator is not encountered in the first bytes, it is appended to the duplicated string.

The returned pointer must be passed to free to avoid a memory leak.

If an error occurs, a null pointer is returned and errno might be set.

Return value
A pointer to the newly allocated string, or a null pointer if an error occurred.