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 _XOPEN_SOURCE >= 500
21 || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
22 || /* Glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
23
24 strndup():
25 Since glibc 2.10:
26 _POSIX_C_SOURCE >= 200809L
27 Before glibc 2.10:
28 _GNU_SOURCE
29
30 strdupa(), strndupa():
31 _GNU_SOURCE
32
34 The strdup() function returns a pointer to a new string which is a du‐
35 plicate of the string s. Memory for the new string is obtained with
36 malloc(3), and can be freed with free(3).
37
38 The strndup() function is similar, but copies at most n bytes. If s is
39 longer than n, only n bytes are copied, and a terminating null byte
40 ('\0') is added.
41
42 strdupa() and strndupa() are similar, but use alloca(3) to allocate the
43 buffer. They are available only when using the GNU GCC suite, and suf‐
44 fer from the same limitations described in alloca(3).
45
47 On success, the strdup() function returns a pointer to the duplicated
48 string. It returns NULL if insufficient memory was available, with er‐
49 rno set to indicate the error.
50
52 ENOMEM Insufficient memory available to allocate duplicate string.
53
55 For an explanation of the terms used in this section, see at‐
56 tributes(7).
57
58 ┌────────────────────────────────────────────┬───────────────┬─────────┐
59 │Interface │ Attribute │ Value │
60 ├────────────────────────────────────────────┼───────────────┼─────────┤
61 │strdup(), strndup(), strdupa(), strndupa() │ Thread safety │ MT-Safe │
62 └────────────────────────────────────────────┴───────────────┴─────────┘
63
65 strdup() conforms to SVr4, 4.3BSD, POSIX.1-2001. strndup() conforms to
66 POSIX.1-2008. strdupa() and strndupa() are GNU extensions.
67
69 alloca(3), calloc(3), free(3), malloc(3), realloc(3), string(3), wcs‐
70 dup(3)
71
73 This page is part of release 5.13 of the Linux man-pages project. A
74 description of the project, information about reporting bugs, and the
75 latest version of this page, can be found at
76 https://www.kernel.org/doc/man-pages/.
77
78
79
80GNU 2021-03-22 STRDUP(3)