Debian Codegraph @

upstream/mageia-cauldron/man3/strdup.3

raw
1 '\" t
2 .\" Copyright 1993, David Metcalfe <[email protected]>
3 .\" Copyright, the authors of the Linux man-pages project
4 .\"
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\"
7 .TH strdup 3 2026-02-08 "Linux man-pages 6.17"
8 .SH NAME
9 strdup, strndup, strdupa, strndupa \- duplicate a string
10 .SH LIBRARY
11 Standard C library
12 .RI ( libc ,\~ \-lc )
13 .SH SYNOPSIS
14 .nf
15 .B #include <string.h>
16 .P
17 .BI "char *strdup(const char *" s );
18 .P
19 .BR "char *strndup(" "size_t n;"
20 .BI " const char " s [ n "], size_t " n );
21 .BI "char *strdupa(const char *" s );
22 .BR "char *strndupa(" "size_t n;"
23 .BI " const char " s [ n "], size_t " n );
24 .fi
25 .P
26 .RS -4
27 Feature Test Macro Requirements for glibc (see
28 .BR feature_test_macros (7)):
29 .RE
30 .P
31 .BR strdup ():
32 .nf
33 _XOPEN_SOURCE >= 500
34 .\" || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
35 || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
36 || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
37 .fi
38 .P
39 .BR strndup ():
40 .nf
41 Since glibc 2.10:
42 _POSIX_C_SOURCE >= 200809L
43 Before glibc 2.10:
44 _GNU_SOURCE
45 .fi
46 .P
47 .BR strdupa (),
48 .BR strndupa ():
49 .nf
50 _GNU_SOURCE
51 .fi
52 .SH DESCRIPTION
53 The
54 .BR strdup ()
55 function returns a pointer to a new string which
56 is a duplicate of the string
57 .IR s .
58 Memory for the new string is
59 obtained with
60 .BR malloc (3),
61 and can be freed with
62 .BR free (3).
63 .P
64 The
65 .BR strndup ()
66 function is similar, but copies at most
67 .I n
68 bytes.
69 If
70 .I s
71 is longer than
72 .IR n ,
73 only
74 .I n
75 bytes are copied, and a terminating null byte (\[aq]\[rs]0\[aq]) is added.
76 .P
77 .BR strdupa ()
78 and
79 .BR strndupa ()
80 are similar, but use
81 .BR alloca (3)
82 to allocate the buffer.
83 .SH RETURN VALUE
84 On success, the
85 .BR strdup ()
86 function returns a pointer to the duplicated
87 string.
88 It returns NULL if insufficient memory was available, with
89 .I errno
90 set to indicate the error.
91 .SH ERRORS
92 .TP
93 .B ENOMEM
94 Insufficient memory available to allocate duplicate string.
95 .SH ATTRIBUTES
96 For an explanation of the terms used in this section, see
97 .BR attributes (7).
98 .TS
99 allbox;
100 lbx lb lb
101 l l l.
102 Interface Attribute Value
103 T{
104 .na
105 .nh
106 .BR strdup (),
107 .BR strndup (),
108 .BR strdupa (),
109 .BR strndupa ()
110 T} Thread safety MT-Safe
111 .TE
112 .SH STANDARDS
113 .TP
114 .BR strdup ()
115 .TQ
116 .BR strndup ()
117 POSIX.1-2008.
118 .TP
119 .BR strdupa ()
120 .TQ
121 .BR strndupa ()
122 GNU.
123 .SH HISTORY
124 .TP
125 .BR strdup ()
126 SVr4, 4.3BSD-Reno, POSIX.1-2001.
127 .TP
128 .BR strndup ()
129 POSIX.1-2008.
130 .TP
131 .BR strdupa ()
132 .TQ
133 .BR strndupa ()
134 GNU.
135 .SH SEE ALSO
136 .BR alloca (3),
137 .BR calloc (3),
138 .BR free (3),
139 .BR malloc (3),
140 .BR realloc (3),
141 .BR string (3),
142 .BR wcsdup (3)