strdup, strndup, strdupa, strndupa - string duplication
#include <string.h> char *strdup(const char *s);
char *strndup(const char *s, size_t size);
char *strdupa(const char *s);
char *strndupa(const char *s, size_t size);
These functions return a pointer to a newly allocated string (array of characters) which is a duplicate of the string s. Depending on the function, the provided strings may be either terminated by a null character or bounded by a maximum length of n bytes. The newly allocated strings will always be null-terminated.
All character counts are measured in individual bytes, even if a string with multibyte characters is used, which may result in copying only part of a multibyte character if the full character does not fit within the byte count specified. They do not check for null pointers, and programs may crash if passing null or otherwise invalid pointers to these functions.
The strdup() function returns a pointer to a new string that is a duplicate of the null-terminated string pointed to by s. The returned pointer can be passed to free(). The space for the new string is obtained using malloc(3C). If the new string cannot be created, a null pointer is returned and errno may be set to ENOMEM to indicate that the storage space available is insufficient.
The strndup() function is similar to strdup(), except that it copies at most size bytes. If the length of s is larger than size, only size bytes are copied and a terminating null byte is added. If size is larger than the length of s, all bytes in s are copied, including the terminating null character.
The strdupa() and strndupa() functions are similar to strdup() and strndup(), respectively, but use alloca(3C) to allocate the buffer.
See attributes(7) for descriptions of the following attributes:
The strdup() and strndup() functions are MT-Safe.
The strdupa() and strndupa() functions are Async-Signal-Safe.
See standards(7) for descriptions of the following standards:
The strndup(), strdupa(), and strndupa() functions were added to Oracle Solaris in the Solaris 11.0.0 release.
The strdup() function has been included in all Sun and Oracle releases of Solaris.