Debian Codegraph @

upstream/debian-unstable/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-25 "Linux man-pages 6.18"
8 .SH NAME
9 strdup, strndup \- 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 .fi
22 .P
23 .RS -4
24 Feature Test Macro Requirements for glibc (see
25 .BR feature_test_macros (7)):
26 .RE
27 .P
28 .BR strdup ():
29 .nf
30 _XOPEN_SOURCE >= 500
31 .\" || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
32 || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
33 || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
34 .fi
35 .P
36 .BR strndup ():
37 .nf
38 Since glibc 2.10:
39 _POSIX_C_SOURCE >= 200809L
40 Before glibc 2.10:
41 _GNU_SOURCE
42 .fi
43 .SH DESCRIPTION
44 The
45 .BR strdup ()
46 function returns a pointer to a new string which
47 is a duplicate of the string
48 .IR s .
49 Memory for the new string is
50 obtained with
51 .BR malloc (3),
52 and can be freed with
53 .BR free (3).
54 .P
55 The
56 .BR strndup ()
57 function is similar, but copies at most
58 .I n
59 bytes.
60 If
61 .I s
62 is longer than
63 .IR n ,
64 only
65 .I n
66 bytes are copied, and a terminating null byte (\[aq]\[rs]0\[aq]) is added.
67 .SH RETURN VALUE
68 On success, the
69 .BR strdup ()
70 function returns a pointer to the duplicated
71 string.
72 It returns NULL if insufficient memory was available, with
73 .I errno
74 set to indicate the error.
75 .SH ERRORS
76 .TP
77 .B ENOMEM
78 Insufficient memory available to allocate duplicate string.
79 .SH ATTRIBUTES
80 For an explanation of the terms used in this section, see
81 .BR attributes (7).
82 .TS
83 allbox;
84 lbx lb lb
85 l l l.
86 Interface Attribute Value
87 T{
88 .na
89 .nh
90 .BR strdup (),
91 .BR strndup ()
92 T} Thread safety MT-Safe
93 .TE
94 .SH STANDARDS
95 POSIX.1-2008.
96 .SH HISTORY
97 .TP
98 .BR strdup ()
99 SVr4, 4.3BSD-Reno, POSIX.1-2001.
100 .TP
101 .BR strndup ()
102 POSIX.1-2008.
103 .SH SEE ALSO
104 .BR alloca (3),
105 .BR calloc (3),
106 .BR free (3),
107 .BR malloc (3),
108 .BR realloc (3),
109 .BR string (3),
110 .BR strdupa (3),
111 .BR strndupa (3),
112 .BR wcsdup (3)