STRDUP(3) BSD Programmer's Manual STRDUP(3)
strdup, strndup - save a copy of a string
#include <string.h> char * strdup(const char *s); char * strndup(const char *s, size_t n);
The strdup() function allocates sufficient memory for a copy of the string s, does the copy, and returns a pointer to it. The pointer may subsequently be used as an argument to the function free(3). The strndup() function copies at most n characters from the source string before appending a NUL byte. If insufficient memory is available, NULL is returned.
The following will point p to an allocated area of memory containing the NUL-terminated string "foobar": char *p; p = strdup("foobar"); if (p == NULL) err(1, NULL);
The strdup() and strndup() functions may fail and set the external vari- able errno for any of the errors specified for the library function malloc(3).
free(3), malloc(3), strcpy(3), strlcpy(3), strlen(3), wcsdup(3)
The strdup() and strndup() functions conform to IEEE Std 1003.1-2008 ("POSIX").
A strdup() macro was first used in the 4.1cBSD debugger, dbx. It was rewritten as a C function for the 4.3BSD inetd(8) and first appeared in the C library of 4.3BSD-Reno. The strndup() function is a GNU extension and first appeared in MirOS #10. MirOS BSD #10-current November 30, 2014 1
Generated on 2016-04-09 18:24:16 by $MirOS: src/scripts/roff2htm,v 1.83 2016/03/26 23:38:28 tg Exp $
These manual pages and other documentation are copyrighted by their respective writers;
their source is available at our CVSweb,
AnonCVS, and other mirrors. The rest is Copyright © 2002–2016 The MirOS Project, Germany.
This product includes material provided by mirabilos.
This manual page’s HTML representation is supposed to be valid XHTML/1.1; if not, please send a bug report – diffs preferred.