diff options
Diffstat (limited to 'src/lib/libc/string/strdup.3')
| -rw-r--r-- | src/lib/libc/string/strdup.3 | 91 |
1 files changed, 69 insertions, 22 deletions
diff --git a/src/lib/libc/string/strdup.3 b/src/lib/libc/string/strdup.3 index 925cbf3d46..45a7bd1826 100644 --- a/src/lib/libc/string/strdup.3 +++ b/src/lib/libc/string/strdup.3 | |||
| @@ -1,5 +1,7 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | 1 | .\" $OpenBSD: strdup.3,v 1.19 2011/12/08 08:06:19 lum Exp $ |
| 2 | .\" All rights reserved. | 2 | .\" |
| 3 | .\" Copyright (c) 1990, 1991, 1993 | ||
| 4 | .\" The Regents of the University of California. All rights reserved. | ||
| 3 | .\" | 5 | .\" |
| 4 | .\" Redistribution and use in source and binary forms, with or without | 6 | .\" Redistribution and use in source and binary forms, with or without |
| 5 | .\" modification, are permitted provided that the following conditions | 7 | .\" modification, are permitted provided that the following conditions |
| @@ -9,11 +11,7 @@ | |||
| 9 | .\" 2. Redistributions in binary form must reproduce the above copyright | 11 | .\" 2. Redistributions in binary form must reproduce the above copyright |
| 10 | .\" notice, this list of conditions and the following disclaimer in the | 12 | .\" notice, this list of conditions and the following disclaimer in the |
| 11 | .\" documentation and/or other materials provided with the distribution. | 13 | .\" documentation and/or other materials provided with the distribution. |
| 12 | .\" 3. All advertising materials mentioning features or use of this software | 14 | .\" 3. Neither the name of the University nor the names of its contributors |
| 13 | .\" must display the following acknowledgement: | ||
| 14 | .\" This product includes software developed by the University of | ||
| 15 | .\" California, Berkeley and its contributors. | ||
| 16 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 17 | .\" may be used to endorse or promote products derived from this software | 15 | .\" may be used to endorse or promote products derived from this software |
| 18 | .\" without specific prior written permission. | 16 | .\" without specific prior written permission. |
| 19 | .\" | 17 | .\" |
| @@ -29,37 +27,86 @@ | |||
| 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 30 | .\" SUCH DAMAGE. | 28 | .\" SUCH DAMAGE. |
| 31 | .\" | 29 | .\" |
| 32 | .\" from: @(#)strdup.3 5.3 (Berkeley) 4/19/91 | 30 | .\" @(#)strdup.3 8.1 (Berkeley) 6/9/93 |
| 33 | .\" $Id: strdup.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $ | ||
| 34 | .\" | 31 | .\" |
| 35 | .Dd April 19, 1991 | 32 | .Dd $Mdocdate: December 8 2011 $ |
| 36 | .Dt STRDUP 3 | 33 | .Dt STRDUP 3 |
| 37 | .Os | 34 | .Os |
| 38 | .Sh NAME | 35 | .Sh NAME |
| 39 | .Nm strdup | 36 | .Nm strdup , |
| 37 | .Nm strndup | ||
| 40 | .Nd save a copy of a string | 38 | .Nd save a copy of a string |
| 41 | .Sh SYNOPSIS | 39 | .Sh SYNOPSIS |
| 42 | .Fd #include <string.h> | 40 | .Fd #include <string.h> |
| 43 | .Ft char * | 41 | .Ft char * |
| 44 | .Fn strdup "const char *str" | 42 | .Fn strdup "const char *s" |
| 43 | .Ft char * | ||
| 44 | .Fn strndup "const char *s" "size_t maxlen" | ||
| 45 | .Sh DESCRIPTION | 45 | .Sh DESCRIPTION |
| 46 | The | 46 | The |
| 47 | .Fn strdup | 47 | .Fn strdup |
| 48 | function | 48 | function allocates sufficient memory for a copy of the string |
| 49 | allocates sufficient memory for a copy | 49 | .Fa s , |
| 50 | of the string | ||
| 51 | .Fa str , | ||
| 52 | does the copy, and returns a pointer to it. | 50 | does the copy, and returns a pointer to it. |
| 53 | The pointer may subsequently be used as an | 51 | The pointer may subsequently be used as an argument to the function |
| 54 | argument to the function | ||
| 55 | .Xr free 3 . | 52 | .Xr free 3 . |
| 53 | .Pp | ||
| 54 | The | ||
| 55 | .Fn strndup | ||
| 56 | function behaves similarly to | ||
| 57 | .Nm strdup | ||
| 58 | but only copies up to | ||
| 59 | .Fa maxlen | ||
| 60 | characters from | ||
| 61 | .Fa s . | ||
| 62 | The resulting string is always NUL-terminated. | ||
| 63 | .Pp | ||
| 64 | If insufficient memory is available, | ||
| 65 | .Dv NULL | ||
| 66 | is returned. | ||
| 67 | .Sh EXAMPLES | ||
| 68 | The following will point | ||
| 69 | .Va p | ||
| 70 | to an allocated area of memory containing the NUL-terminated string | ||
| 71 | .Qq foobar : | ||
| 72 | .Bd -literal -offset indent | ||
| 73 | char *p; | ||
| 74 | |||
| 75 | p = strdup("foobar"); | ||
| 76 | if (p == NULL) | ||
| 77 | err(1, NULL); | ||
| 78 | .Ed | ||
| 79 | .Sh ERRORS | ||
| 80 | The | ||
| 81 | .Fn strdup | ||
| 82 | and | ||
| 83 | .Fn strndup | ||
| 84 | functions may fail and set the external variable | ||
| 85 | .Va errno | ||
| 86 | for any of the errors specified for the library function | ||
| 87 | .Xr malloc 3 . | ||
| 56 | .Sh SEE ALSO | 88 | .Sh SEE ALSO |
| 57 | .Xr free 3 , | 89 | .Xr free 3 , |
| 58 | .Xr malloc 3 , | 90 | .Xr malloc 3 , |
| 59 | .Xt strcpy 3 , | 91 | .Xr strcpy 3 , |
| 60 | .Xt strlen 3 | 92 | .Xr strlcpy 3 , |
| 93 | .Xr strlen 3 , | ||
| 94 | .Xr wcsdup 3 | ||
| 95 | .Sh STANDARDS | ||
| 96 | The | ||
| 97 | .Fn strdup | ||
| 98 | and | ||
| 99 | .Fn strndup | ||
| 100 | functions conform to | ||
| 101 | .St -p1003.1-2008 . | ||
| 61 | .Sh HISTORY | 102 | .Sh HISTORY |
| 62 | The | 103 | The |
| 63 | .Fn strdup | 104 | .Fn strdup |
| 64 | function | 105 | function first appeared in |
| 65 | .Ud . | 106 | .Bx 4.3 Reno . |
| 107 | The | ||
| 108 | .Fn strndup | ||
| 109 | function appeared in glibc 2.0, was reimplemented for | ||
| 110 | .Nx 4.0 , | ||
| 111 | and ported to | ||
| 112 | .Ox 4.8 . | ||
