|STRDUP(3P)||POSIX Programmer's Manual||STRDUP(3P)|
char *strdup(const char *s); char *strndup(const char *s, size_t size);
The strndup() function shall be equivalent to the strdup() function, duplicating the provided s in a new block of memory allocated as if by using malloc(), with the exception being that strndup() copies at most size plus one bytes into the newly allocated memory, terminating the new string with a NUL character. If the length of s is larger than size, only size bytes shall be duplicated. If size is larger than the length of s, all bytes in s shall be copied into the new memory buffer, including the terminating NUL character. The newly created string shall always be properly terminated.
Upon successful completion, the strndup() function shall return a pointer to the newly allocated memory containing the duplicated string. Otherwise, it shall return a null pointer and set errno to indicate the error.
- Storage space available is insufficient.
The following sections are informative.
Implementations are free to malloc() a buffer containing either (size + 1) bytes or (strnlen( s, size) + 1) bytes. Applications should not assume that strndup() will allocate (size + 1) bytes when strlen( s) is smaller than size.
The Base Definitions volume of POSIX.1‐2017, <string.h>
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
|2017||IEEE/The Open Group|