1STRDUP(3P) POSIX Programmer's Manual STRDUP(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 strdup, strndup — duplicate a specific number of bytes from a string
13
15 #include <string.h>
16
17 char *strdup(const char *s);
18 char *strndup(const char *s, size_t size);
19
21 The strdup() function shall return a pointer to a new string, which is
22 a duplicate of the string pointed to by s. The returned pointer can be
23 passed to free(). A null pointer is returned if the new string cannot
24 be created.
25
26 The strndup() function shall be equivalent to the strdup() function,
27 duplicating the provided s in a new block of memory allocated as if by
28 using malloc(), with the exception being that strndup() copies at most
29 size plus one bytes into the newly allocated memory, terminating the
30 new string with a NUL character. If the length of s is larger than
31 size, only size bytes shall be duplicated. If size is larger than the
32 length of s, all bytes in s shall be copied into the new memory buffer,
33 including the terminating NUL character. The newly created string shall
34 always be properly terminated.
35
37 The strdup() function shall return a pointer to a new string on suc‐
38 cess. Otherwise, it shall return a null pointer and set errno to indi‐
39 cate the error.
40
41 Upon successful completion, the strndup() function shall return a
42 pointer to the newly allocated memory containing the duplicated string.
43 Otherwise, it shall return a null pointer and set errno to indicate the
44 error.
45
47 These functions shall fail if:
48
49 ENOMEM Storage space available is insufficient.
50
51 The following sections are informative.
52
54 None.
55
57 For functions that allocate memory as if by malloc(), the application
58 should release such memory when it is no longer required by a call to
59 free(). For strdup() and strndup(), this is the return value.
60
61 Implementations are free to malloc() a buffer containing either (size +
62 1) bytes or (strnlen( s, size) + 1) bytes. Applications should not
63 assume that strndup() will allocate (size + 1) bytes when strlen( s) is
64 smaller than size.
65
67 None.
68
70 None.
71
73 free(), wcsdup()
74
75 The Base Definitions volume of POSIX.1‐2017, <string.h>
76
78 Portions of this text are reprinted and reproduced in electronic form
79 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
80 table Operating System Interface (POSIX), The Open Group Base Specifi‐
81 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
82 Electrical and Electronics Engineers, Inc and The Open Group. In the
83 event of any discrepancy between this version and the original IEEE and
84 The Open Group Standard, the original IEEE and The Open Group Standard
85 is the referee document. The original Standard can be obtained online
86 at http://www.opengroup.org/unix/online.html .
87
88 Any typographical or formatting errors that appear in this page are
89 most likely to have been introduced during the conversion of the source
90 files to man page format. To report such errors, see https://www.ker‐
91 nel.org/doc/man-pages/reporting_bugs.html .
92
93
94
95IEEE/The Open Group 2017 STRDUP(3P)