tzfile.5, tzselect.8: sync from tzdb upstream
[man-pages.git] / man3 / strdup.3
blob0e928b909c3b6523a4c5670adace4e7b2b2f8818
1 '\" t
2 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
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 (faith@cs.unc.edu)
11 .\" Modified Wed Oct 17 01:12:26 2001 by John Levon <moz@compsoc.man.ac.uk>
12 .TH strdup 3 (date) "Linux man-pages (unreleased)"
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 .PP
22 .BI "char *strdup(const char *" s );
23 .PP
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 .PP
29 .RS -4
30 Feature Test Macro Requirements for glibc (see
31 .BR feature_test_macros (7)):
32 .RE
33 .PP
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 .PP
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 .PP
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 .PP
67 The
68 .BR strndup ()
69 function is similar, but copies at most
70 .I n
71 bytes.
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]\e0\[aq]) is added.
79 .PP
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 .ad l
104 allbox;
105 lbx lb lb
106 l l l.
107 Interface       Attribute       Value
109 .BR strdup (),
110 .BR strndup (),
111 .BR strdupa (),
112 .BR strndupa ()
113 T}      Thread safety   MT-Safe
117 .sp 1
118 .SH STANDARDS
119 .\" 4.3BSD-Reno, not (first) 4.3BSD.
120 .BR strdup ()
121 conforms to SVr4, 4.3BSD, POSIX.1-2001.
122 .BR strndup ()
123 conforms to POSIX.1-2008.
124 .BR strdupa ()
126 .BR strndupa ()
127 are GNU extensions.
128 .SH SEE ALSO
129 .BR alloca (3),
130 .BR calloc (3),
131 .BR free (3),
132 .BR malloc (3),
133 .BR realloc (3),
134 .BR string (3),
135 .BR wcsdup (3)