Debian Codegraph @

upstream/debian-trixie/man3/strdup.3

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