1STRDUP(3) Linux Programmer's Manual STRDUP(3)
2
3
4
6 strdup, strndup, strdupa, strndupa - duplicate a string
7
9 #include <string.h>
10
11 char *strdup(const char *s);
12
13 char *strndup(const char *s, size_t n);
14 char *strdupa(const char *s);
15 char *strndupa(const char *s, size_t n);
16
17 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19 strdup():
20 _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE >= 500 ||
21 _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
22 || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
23 strndup():
24 Since glibc 2.10:
25 POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
26 Before glibc 2.10:
27 _GNU_SOURCE
28 strdupa(), strndupa(): _GNU_SOURCE
29
31 The strdup() function returns a pointer to a new string which is a
32 duplicate of the string s. Memory for the new string is obtained with
33 malloc(3), and can be freed with free(3).
34
35 The strndup() function is similar, but copies at most n bytes. If s is
36 longer than n, only n bytes are copied, and a terminating null byte
37 ('\0') is added.
38
39 strdupa() and strndupa() are similar, but use alloca(3) to allocate the
40 buffer. They are available only when using the GNU GCC suite, and suf‐
41 fer from the same limitations described in alloca(3).
42
44 On success, the strdup() function returns a pointer to the duplicated
45 string. It returns NULL if insufficient memory was available, with
46 errno set to indicate the cause of the error.
47
49 ENOMEM Insufficient memory available to allocate duplicate string.
50
52 strdup() conforms to SVr4, 4.3BSD, POSIX.1-2001. strndup() conforms to
53 POSIX.1-2008. strdupa() and strndupa() are GNU extensions.
54
56 alloca(3), calloc(3), free(3), malloc(3), realloc(3), string(3), wcs‐
57 dup(3)
58
60 This page is part of release 3.53 of the Linux man-pages project. A
61 description of the project, and information about reporting bugs, can
62 be found at http://www.kernel.org/doc/man-pages/.
63
64
65
66GNU 2013-04-19 STRDUP(3)