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 versions <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
23 strndup():
24 Since glibc 2.10:
25 _POSIX_C_SOURCE >= 200809L
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 du‐
32 plicate 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 er‐
46 rno set to indicate the cause of the error.
47
49 ENOMEM Insufficient memory available to allocate duplicate string.
50
52 For an explanation of the terms used in this section, see at‐
53 tributes(7).
54
55 ┌────────────────────────────────┬───────────────┬─────────┐
56 │Interface │ Attribute │ Value │
57 ├────────────────────────────────┼───────────────┼─────────┤
58 │strdup(), strndup(), strdupa(), │ Thread safety │ MT-Safe │
59 │strndupa() │ │ │
60 └────────────────────────────────┴───────────────┴─────────┘
61
63 strdup() conforms to SVr4, 4.3BSD, POSIX.1-2001. strndup() conforms to
64 POSIX.1-2008. strdupa() and strndupa() are GNU extensions.
65
67 alloca(3), calloc(3), free(3), malloc(3), realloc(3), string(3), wcs‐
68 dup(3)
69
71 This page is part of release 5.10 of the Linux man-pages project. A
72 description of the project, information about reporting bugs, and the
73 latest version of this page, can be found at
74 https://www.kernel.org/doc/man-pages/.
75
76
77
78GNU 2019-03-06 STRDUP(3)