diff options
Diffstat (limited to 'src/lib/libc/string')
| -rw-r--r-- | src/lib/libc/string/Makefile.inc | 17 | ||||
| -rw-r--r-- | src/lib/libc/string/strcat.3 | 80 | ||||
| -rw-r--r-- | src/lib/libc/string/strcpy.3 | 98 | ||||
| -rw-r--r-- | src/lib/libc/string/strncat.3 | 128 | ||||
| -rw-r--r-- | src/lib/libc/string/strncpy.3 | 153 |
5 files changed, 308 insertions, 168 deletions
diff --git a/src/lib/libc/string/Makefile.inc b/src/lib/libc/string/Makefile.inc index 05bd93a417..9d6d1b2368 100644 --- a/src/lib/libc/string/Makefile.inc +++ b/src/lib/libc/string/Makefile.inc | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | # $OpenBSD: Makefile.inc,v 1.31 2013/10/14 06:55:28 guenther Exp $ | 1 | # $OpenBSD: Makefile.inc,v 1.32 2013/12/19 20:52:37 millert Exp $ |
| 2 | 2 | ||
| 3 | # string sources | 3 | # string sources |
| 4 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/string ${LIBCSRCDIR}/string | 4 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/string ${LIBCSRCDIR}/string |
| @@ -146,12 +146,13 @@ strrchr.do: rindex.c | |||
| 146 | 146 | ||
| 147 | MAN+= bm.3 bcmp.3 bcopy.3 bstring.3 bzero.3 ffs.3 memccpy.3 memchr.3 \ | 147 | MAN+= bm.3 bcmp.3 bcopy.3 bstring.3 bzero.3 ffs.3 memccpy.3 memchr.3 \ |
| 148 | memcmp.3 memcpy.3 memmem.3 memmove.3 memset.3 stpcpy.3 strcasecmp.3 \ | 148 | memcmp.3 memcpy.3 memmem.3 memmove.3 memset.3 stpcpy.3 strcasecmp.3 \ |
| 149 | strcat.3 strchr.3 strcmp.3 strcoll.3 strcpy.3 strcspn.3 strerror.3 \ | 149 | strcat.3 strchr.3 strcmp.3 strcoll.3 strcpy.3 strcspn.3 strdup.3 \ |
| 150 | string.3 strlen.3 strmode.3 strdup.3 strpbrk.3 strrchr.3 strsep.3 \ | 150 | strerror.3 string.3 strlen.3 strmode.3 strncat.3 strncpy.3 strpbrk.3 \ |
| 151 | strsignal.3 strspn.3 strstr.3 strtok.3 strxfrm.3 swab.3 strlcpy.3 \ | 151 | strrchr.3 strsep.3 strsignal.3 strspn.3 strstr.3 strtok.3 strxfrm.3 \ |
| 152 | wcscasecmp.3 wcscat.3 wcschr.3 wcscmp.3 wcscpy.3 wcscspn.3 wcsdup.3 \ | 152 | swab.3 strlcpy.3 wcscasecmp.3 wcscat.3 wcschr.3 wcscmp.3 wcscpy.3 \ |
| 153 | wcslcpy.3 wcslen.3 wcspbrk.3 wcsrchr.3 wcsspn.3 wcsstr.3 wcstok.3 \ | 153 | wcscspn.3 wcsdup.3 wcslcpy.3 wcslen.3 wcspbrk.3 wcsrchr.3 wcsspn.3 \ |
| 154 | wcswidth.3 wmemchr.3 wmemcmp.3 wmemcpy.3 wmemmove.3 wmemset.3 | 154 | wcsstr.3 wcstok.3 wcswidth.3 wmemchr.3 wmemcmp.3 wmemcpy.3 wmemmove.3 \ |
| 155 | wmemset.3 | ||
| 155 | 156 | ||
| 156 | MLINKS+=bm.3 bm_comp.3 bm.3 bm_exec.3 bm.3 bm_free.3 | 157 | MLINKS+=bm.3 bm_comp.3 bm.3 bm_exec.3 bm.3 bm_free.3 |
| 157 | MLINKS+=memchr.3 memrchr.3 | 158 | MLINKS+=memchr.3 memrchr.3 |
| @@ -159,9 +160,7 @@ MLINKS+=stpcpy.3 stpncpy.3 | |||
| 159 | MLINKS+=strchr.3 index.3 | 160 | MLINKS+=strchr.3 index.3 |
| 160 | MLINKS+=strrchr.3 rindex.3 | 161 | MLINKS+=strrchr.3 rindex.3 |
| 161 | MLINKS+=strcasecmp.3 strncasecmp.3 | 162 | MLINKS+=strcasecmp.3 strncasecmp.3 |
| 162 | MLINKS+=strcat.3 strncat.3 | ||
| 163 | MLINKS+=strcmp.3 strncmp.3 | 163 | MLINKS+=strcmp.3 strncmp.3 |
| 164 | MLINKS+=strcpy.3 strncpy.3 | ||
| 165 | MLINKS+=strdup.3 strndup.3 | 164 | MLINKS+=strdup.3 strndup.3 |
| 166 | MLINKS+=strlcpy.3 strlcat.3 | 165 | MLINKS+=strlcpy.3 strlcat.3 |
| 167 | MLINKS+=strlen.3 strnlen.3 | 166 | MLINKS+=strlen.3 strnlen.3 |
diff --git a/src/lib/libc/string/strcat.3 b/src/lib/libc/string/strcat.3 index 6b5e4003b6..fba992edd9 100644 --- a/src/lib/libc/string/strcat.3 +++ b/src/lib/libc/string/strcat.3 | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | .\" $OpenBSD: strcat.3,v 1.15 2013/07/17 05:42:11 schwarze Exp $ | 1 | .\" $OpenBSD: strcat.3,v 1.16 2013/12/19 20:52:37 millert Exp $ |
| 2 | .\" | 2 | .\" |
| 3 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | 3 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. |
| 4 | .\" All rights reserved. | 4 | .\" All rights reserved. |
| @@ -31,91 +31,33 @@ | |||
| 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 32 | .\" SUCH DAMAGE. | 32 | .\" SUCH DAMAGE. |
| 33 | .\" | 33 | .\" |
| 34 | .Dd $Mdocdate: July 17 2013 $ | 34 | .Dd $Mdocdate: December 19 2013 $ |
| 35 | .Dt STRCAT 3 | 35 | .Dt STRCAT 3 |
| 36 | .Os | 36 | .Os |
| 37 | .Sh NAME | 37 | .Sh NAME |
| 38 | .Nm strcat , | 38 | .Nm strcat |
| 39 | .Nm strncat | 39 | .Nd concatenate two strings |
| 40 | .Nd concatenate strings | ||
| 41 | .Sh SYNOPSIS | 40 | .Sh SYNOPSIS |
| 42 | .In string.h | 41 | .In string.h |
| 43 | .Ft char * | 42 | .Ft char * |
| 44 | .Fn strcat "char *s" "const char *append" | 43 | .Fn strcat "char *s" "const char *append" |
| 45 | .Ft char * | ||
| 46 | .Fn strncat "char *s" "const char *append" "size_t count" | ||
| 47 | .Sh DESCRIPTION | 44 | .Sh DESCRIPTION |
| 48 | The | 45 | The |
| 49 | .Fn strcat | 46 | .Fn strcat |
| 50 | and | 47 | function appends a copy of the NUL-terminated string |
| 51 | .Fn strncat | ||
| 52 | functions append a copy of the NUL-terminated string | ||
| 53 | .Fa append | 48 | .Fa append |
| 54 | to the end of the NUL-terminated string | 49 | to the end of the NUL-terminated string |
| 55 | .Fa s , | 50 | .Fa s , |
| 56 | then add a terminating | 51 | then adds a terminating |
| 57 | .Ql \e0 . | 52 | .Ql \e0 . |
| 58 | The string | 53 | The string |
| 59 | .Fa s | 54 | .Fa s |
| 60 | must have sufficient space to hold the result. | 55 | must have sufficient space to hold the result. |
| 61 | .Pp | ||
| 62 | The | ||
| 63 | .Fn strncat | ||
| 64 | function appends not more than | ||
| 65 | .Fa count | ||
| 66 | characters where space for the terminating | ||
| 67 | .Ql \e0 | ||
| 68 | should not be included in | ||
| 69 | .Fa count . | ||
| 70 | .Sh RETURN VALUES | 56 | .Sh RETURN VALUES |
| 71 | The | 57 | The |
| 72 | .Fn strcat | 58 | .Fn strcat |
| 73 | and | 59 | function return the pointer |
| 74 | .Fn strncat | ||
| 75 | functions return the pointer | ||
| 76 | .Fa s . | 60 | .Fa s . |
| 77 | .Sh EXAMPLES | ||
| 78 | The following appends | ||
| 79 | .Dq Li abc | ||
| 80 | to | ||
| 81 | .Va chararray : | ||
| 82 | .Bd -literal -offset indent | ||
| 83 | char *letters = "abcdefghi"; | ||
| 84 | |||
| 85 | (void)strncat(chararray, letters, 3); | ||
| 86 | .Ed | ||
| 87 | .Pp | ||
| 88 | The following example shows how to use | ||
| 89 | .Fn strncat | ||
| 90 | safely in conjunction with | ||
| 91 | .Xr strncpy 3 . | ||
| 92 | .Bd -literal -offset indent | ||
| 93 | char buf[BUFSIZ]; | ||
| 94 | char *input, *suffix; | ||
| 95 | |||
| 96 | (void)strncpy(buf, input, sizeof(buf) - 1); | ||
| 97 | buf[sizeof(buf) - 1] = '\e0'; | ||
| 98 | (void)strncat(buf, suffix, sizeof(buf) - 1 - strlen(buf)); | ||
| 99 | .Ed | ||
| 100 | .Pp | ||
| 101 | The above will copy as many characters from | ||
| 102 | .Va input | ||
| 103 | to | ||
| 104 | .Va buf | ||
| 105 | as will fit. | ||
| 106 | It then appends as many characters from | ||
| 107 | .Va suffix | ||
| 108 | as will fit (or none | ||
| 109 | if there is no space). | ||
| 110 | For operations like this, the | ||
| 111 | .Xr strlcpy 3 | ||
| 112 | and | ||
| 113 | .Xr strlcat 3 | ||
| 114 | functions are a better choice, as shown below. | ||
| 115 | .Bd -literal -offset indent | ||
| 116 | (void)strlcpy(buf, input, sizeof(buf)); | ||
| 117 | (void)strlcat(buf, suffix, sizeof(buf)); | ||
| 118 | .Ed | ||
| 119 | .Sh SEE ALSO | 61 | .Sh SEE ALSO |
| 120 | .Xr bcopy 3 , | 62 | .Xr bcopy 3 , |
| 121 | .Xr memccpy 3 , | 63 | .Xr memccpy 3 , |
| @@ -123,21 +65,17 @@ functions are a better choice, as shown below. | |||
| 123 | .Xr memmove 3 , | 65 | .Xr memmove 3 , |
| 124 | .Xr strcpy 3 , | 66 | .Xr strcpy 3 , |
| 125 | .Xr strlcpy 3 , | 67 | .Xr strlcpy 3 , |
| 68 | .Xr strncat 3 , | ||
| 126 | .Xr wcscat 3 , | 69 | .Xr wcscat 3 , |
| 127 | .Xr wcslcpy 3 | 70 | .Xr wcslcpy 3 |
| 128 | .Sh STANDARDS | 71 | .Sh STANDARDS |
| 129 | The | 72 | The |
| 130 | .Fn strcat | 73 | .Fn strcat |
| 131 | and | 74 | function conforms to |
| 132 | .Fn strncat | ||
| 133 | functions conform to | ||
| 134 | .St -ansiC . | 75 | .St -ansiC . |
| 135 | .Sh HISTORY | 76 | .Sh HISTORY |
| 136 | The | 77 | The |
| 137 | .Fn strcat | 78 | .Fn strcat |
| 138 | function first appeared in the Programmer's Workbench (PWB/UNIX) | 79 | function first appeared in the Programmer's Workbench (PWB/UNIX) |
| 139 | and was ported to | 80 | and was ported to |
| 140 | .At v7 ; | ||
| 141 | .Fn strncat | ||
| 142 | first appeared in | ||
| 143 | .At v7 . | 81 | .At v7 . |
diff --git a/src/lib/libc/string/strcpy.3 b/src/lib/libc/string/strcpy.3 index 798c7e8d99..849184d1f5 100644 --- a/src/lib/libc/string/strcpy.3 +++ b/src/lib/libc/string/strcpy.3 | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | .\" $OpenBSD: strcpy.3,v 1.19 2013/09/25 21:49:31 millert Exp $ | 1 | .\" $OpenBSD: strcpy.3,v 1.20 2013/12/19 20:52:37 millert Exp $ |
| 2 | .\" | 2 | .\" |
| 3 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | 3 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. |
| 4 | .\" All rights reserved. | 4 | .\" All rights reserved. |
| @@ -31,52 +31,31 @@ | |||
| 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 32 | .\" SUCH DAMAGE. | 32 | .\" SUCH DAMAGE. |
| 33 | .\" | 33 | .\" |
| 34 | .Dd $Mdocdate: September 25 2013 $ | 34 | .Dd $Mdocdate: December 19 2013 $ |
| 35 | .Dt STRCPY 3 | 35 | .Dt STRCPY 3 |
| 36 | .Os | 36 | .Os |
| 37 | .Sh NAME | 37 | .Sh NAME |
| 38 | .Nm strcpy , | 38 | .Nm strcpy |
| 39 | .Nm strncpy | 39 | .Nd copy a string |
| 40 | .Nd copy strings | ||
| 41 | .Sh SYNOPSIS | 40 | .Sh SYNOPSIS |
| 42 | .In string.h | 41 | .In string.h |
| 43 | .Ft char * | 42 | .Ft char * |
| 44 | .Fn strcpy "char *dst" "const char *src" | 43 | .Fn strcpy "char *dst" "const char *src" |
| 45 | .Ft char * | ||
| 46 | .Fn strncpy "char *dst" "const char *src" "size_t len" | ||
| 47 | .Sh DESCRIPTION | 44 | .Sh DESCRIPTION |
| 48 | The | 45 | The |
| 49 | .Fn strcpy | 46 | .Fn strcpy |
| 50 | and | 47 | function copies the string |
| 51 | .Fn strncpy | ||
| 52 | functions copy the string | ||
| 53 | .Fa src | 48 | .Fa src |
| 54 | to | 49 | to |
| 55 | .Fa dst | 50 | .Fa dst |
| 56 | (including the terminating | 51 | (including the terminating |
| 57 | .Ql \e0 | 52 | .Ql \e0 |
| 58 | character). | 53 | character). |
| 59 | .Pp | 54 | The string |
| 60 | The | ||
| 61 | .Fn strncpy | ||
| 62 | function copies not more than | ||
| 63 | .Fa len | ||
| 64 | characters into | ||
| 65 | .Fa dst , | ||
| 66 | appending | ||
| 67 | .Ql \e0 | ||
| 68 | characters if | ||
| 69 | .Fa src | ||
| 70 | is less than | ||
| 71 | .Fa len | ||
| 72 | characters long, and | ||
| 73 | .Em not | ||
| 74 | terminating | ||
| 75 | .Fa dst | 55 | .Fa dst |
| 76 | if the length of | 56 | must be at least as large as |
| 77 | .Fa src | 57 | .Fa src |
| 78 | is greater than or equal to | 58 | to hold the result. |
| 79 | .Fa len . | ||
| 80 | .Pp | 59 | .Pp |
| 81 | If the | 60 | If the |
| 82 | .Fa src | 61 | .Fa src |
| @@ -86,63 +65,8 @@ strings overlap, the behavior is undefined. | |||
| 86 | .Sh RETURN VALUES | 65 | .Sh RETURN VALUES |
| 87 | The | 66 | The |
| 88 | .Fn strcpy | 67 | .Fn strcpy |
| 89 | and | 68 | function returns |
| 90 | .Fn strncpy | ||
| 91 | functions return | ||
| 92 | .Fa dst . | 69 | .Fa dst . |
| 93 | .Sh EXAMPLES | ||
| 94 | The following sets | ||
| 95 | .Va chararray | ||
| 96 | to | ||
| 97 | .Dq abc\e0\e0\e0 : | ||
| 98 | .Bd -literal -offset indent | ||
| 99 | (void)strncpy(chararray, "abc", 6); | ||
| 100 | .Ed | ||
| 101 | .Pp | ||
| 102 | The following sets | ||
| 103 | .Va chararray | ||
| 104 | to | ||
| 105 | .Dq abcdef | ||
| 106 | and does | ||
| 107 | .Em not | ||
| 108 | NUL terminate | ||
| 109 | .Va chararray | ||
| 110 | because the length of the source string is greater than or equal to the | ||
| 111 | length parameter. | ||
| 112 | .Fn strncpy | ||
| 113 | .Em only | ||
| 114 | NUL terminates the destination string when the length of the source | ||
| 115 | string is less than the length parameter. | ||
| 116 | .Bd -literal -offset indent | ||
| 117 | (void)strncpy(chararray, "abcdefgh", 6); | ||
| 118 | .Ed | ||
| 119 | .Pp | ||
| 120 | The following copies as many characters from | ||
| 121 | .Va input | ||
| 122 | to | ||
| 123 | .Va buf | ||
| 124 | as will fit and NUL terminates the result. | ||
| 125 | Because | ||
| 126 | .Fn strncpy | ||
| 127 | does | ||
| 128 | .Em not | ||
| 129 | guarantee to NUL terminate the string itself, it must be done by hand. | ||
| 130 | .Bd -literal -offset indent | ||
| 131 | char buf[BUFSIZ]; | ||
| 132 | |||
| 133 | (void)strncpy(buf, input, sizeof(buf) - 1); | ||
| 134 | buf[sizeof(buf) - 1] = '\e0'; | ||
| 135 | .Ed | ||
| 136 | .Pp | ||
| 137 | Note that | ||
| 138 | .Xr strlcpy 3 | ||
| 139 | is a better choice for this kind of operation. | ||
| 140 | The equivalent using | ||
| 141 | .Xr strlcpy 3 | ||
| 142 | is simply: | ||
| 143 | .Bd -literal -offset indent | ||
| 144 | (void)strlcpy(buf, input, sizeof(buf)); | ||
| 145 | .Ed | ||
| 146 | .Sh SEE ALSO | 70 | .Sh SEE ALSO |
| 147 | .Xr bcopy 3 , | 71 | .Xr bcopy 3 , |
| 148 | .Xr memccpy 3 , | 72 | .Xr memccpy 3 , |
| @@ -150,6 +74,7 @@ is simply: | |||
| 150 | .Xr memmove 3 , | 74 | .Xr memmove 3 , |
| 151 | .Xr strcat 3 , | 75 | .Xr strcat 3 , |
| 152 | .Xr strlcpy 3 , | 76 | .Xr strlcpy 3 , |
| 77 | .Xr strncpy 3 , | ||
| 153 | .Xr wcscpy 3 , | 78 | .Xr wcscpy 3 , |
| 154 | .Xr wcslcpy 3 | 79 | .Xr wcslcpy 3 |
| 155 | .Sh STANDARDS | 80 | .Sh STANDARDS |
| @@ -164,7 +89,4 @@ The | |||
| 164 | .Fn strcpy | 89 | .Fn strcpy |
| 165 | function first appeared in the Programmer's Workbench (PWB/UNIX) | 90 | function first appeared in the Programmer's Workbench (PWB/UNIX) |
| 166 | and was ported to | 91 | and was ported to |
| 167 | .At v7 ; | ||
| 168 | .Fn strncpy | ||
| 169 | first appeared in | ||
| 170 | .At v7 . | 92 | .At v7 . |
diff --git a/src/lib/libc/string/strncat.3 b/src/lib/libc/string/strncat.3 new file mode 100644 index 0000000000..38eabe3474 --- /dev/null +++ b/src/lib/libc/string/strncat.3 | |||
| @@ -0,0 +1,128 @@ | |||
| 1 | .\" $OpenBSD: strncat.3,v 1.1 2013/12/19 20:52:37 millert Exp $ | ||
| 2 | .\" | ||
| 3 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 4 | .\" All rights reserved. | ||
| 5 | .\" | ||
| 6 | .\" This code is derived from software contributed to Berkeley by | ||
| 7 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 8 | .\" on Information Processing Systems. | ||
| 9 | .\" | ||
| 10 | .\" Redistribution and use in source and binary forms, with or without | ||
| 11 | .\" modification, are permitted provided that the following conditions | ||
| 12 | .\" are met: | ||
| 13 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer. | ||
| 15 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 16 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 17 | .\" documentation and/or other materials provided with the distribution. | ||
| 18 | .\" 3. Neither the name of the University nor the names of its contributors | ||
| 19 | .\" may be used to endorse or promote products derived from this software | ||
| 20 | .\" without specific prior written permission. | ||
| 21 | .\" | ||
| 22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 32 | .\" SUCH DAMAGE. | ||
| 33 | .\" | ||
| 34 | .Dd $Mdocdate: December 19 2013 $ | ||
| 35 | .Dt STRNCAT 3 | ||
| 36 | .Os | ||
| 37 | .Sh NAME | ||
| 38 | .Nm strncat | ||
| 39 | .Nd concatenate a string with part of another | ||
| 40 | .Sh SYNOPSIS | ||
| 41 | .In string.h | ||
| 42 | .Ft char * | ||
| 43 | .Fn strncat "char *s" "const char *append" "size_t count" | ||
| 44 | .Sh DESCRIPTION | ||
| 45 | The | ||
| 46 | .Fn strncat | ||
| 47 | function appends not more than | ||
| 48 | .Fa count | ||
| 49 | characters of the NUL-terminated string | ||
| 50 | .Fa append | ||
| 51 | to the end of the NUL-terminated string | ||
| 52 | .Fa s . | ||
| 53 | Space for the terminating | ||
| 54 | .Ql \e0 | ||
| 55 | should not be included in | ||
| 56 | .Fa count . | ||
| 57 | The string | ||
| 58 | .Fa s | ||
| 59 | must have sufficient space to hold the result. | ||
| 60 | .Sh RETURN VALUES | ||
| 61 | The | ||
| 62 | .Fn strncat | ||
| 63 | function returns the pointer | ||
| 64 | .Fa s . | ||
| 65 | .Sh EXAMPLES | ||
| 66 | The following appends | ||
| 67 | .Dq Li abc | ||
| 68 | to | ||
| 69 | .Va chararray : | ||
| 70 | .Bd -literal -offset indent | ||
| 71 | char *letters = "abcdefghi"; | ||
| 72 | |||
| 73 | (void)strncat(chararray, letters, 3); | ||
| 74 | .Ed | ||
| 75 | .Pp | ||
| 76 | The following example shows how to use | ||
| 77 | .Fn strncat | ||
| 78 | safely in conjunction with | ||
| 79 | .Xr strncpy 3 . | ||
| 80 | .Bd -literal -offset indent | ||
| 81 | char buf[BUFSIZ]; | ||
| 82 | char *input, *suffix; | ||
| 83 | |||
| 84 | (void)strncpy(buf, input, sizeof(buf) - 1); | ||
| 85 | buf[sizeof(buf) - 1] = '\e0'; | ||
| 86 | (void)strncat(buf, suffix, sizeof(buf) - 1 - strlen(buf)); | ||
| 87 | .Ed | ||
| 88 | .Pp | ||
| 89 | The above will copy as many characters from | ||
| 90 | .Va input | ||
| 91 | to | ||
| 92 | .Va buf | ||
| 93 | as will fit. | ||
| 94 | It then appends as many characters from | ||
| 95 | .Va suffix | ||
| 96 | as will fit (or none | ||
| 97 | if there is no space). | ||
| 98 | For operations like this, the | ||
| 99 | .Xr strlcpy 3 | ||
| 100 | and | ||
| 101 | .Xr strlcat 3 | ||
| 102 | functions are a better choice, as shown below. | ||
| 103 | .Bd -literal -offset indent | ||
| 104 | (void)strlcpy(buf, input, sizeof(buf)); | ||
| 105 | (void)strlcat(buf, suffix, sizeof(buf)); | ||
| 106 | .Ed | ||
| 107 | .Sh SEE ALSO | ||
| 108 | .Xr bcopy 3 , | ||
| 109 | .Xr memccpy 3 , | ||
| 110 | .Xr memcpy 3 , | ||
| 111 | .Xr memmove 3 , | ||
| 112 | .Xr strcat 3 , | ||
| 113 | .Xr strcpy 3 , | ||
| 114 | .Xr strlcpy 3 , | ||
| 115 | .Xr wcscat 3 , | ||
| 116 | .Xr wcslcpy 3 | ||
| 117 | .Sh STANDARDS | ||
| 118 | The | ||
| 119 | .Fn strcat | ||
| 120 | and | ||
| 121 | .Fn strncat | ||
| 122 | functions conform to | ||
| 123 | .St -ansiC . | ||
| 124 | .Sh HISTORY | ||
| 125 | The | ||
| 126 | .Fn strncat | ||
| 127 | function first first appeared in | ||
| 128 | .At v7 . | ||
diff --git a/src/lib/libc/string/strncpy.3 b/src/lib/libc/string/strncpy.3 new file mode 100644 index 0000000000..dd8ddb86fc --- /dev/null +++ b/src/lib/libc/string/strncpy.3 | |||
| @@ -0,0 +1,153 @@ | |||
| 1 | .\" $OpenBSD: strncpy.3,v 1.1 2013/12/19 20:52:37 millert Exp $ | ||
| 2 | .\" | ||
| 3 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 4 | .\" All rights reserved. | ||
| 5 | .\" | ||
| 6 | .\" This code is derived from software contributed to Berkeley by | ||
| 7 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 8 | .\" on Information Processing Systems. | ||
| 9 | .\" | ||
| 10 | .\" Redistribution and use in source and binary forms, with or without | ||
| 11 | .\" modification, are permitted provided that the following conditions | ||
| 12 | .\" are met: | ||
| 13 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer. | ||
| 15 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 16 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 17 | .\" documentation and/or other materials provided with the distribution. | ||
| 18 | .\" 3. Neither the name of the University nor the names of its contributors | ||
| 19 | .\" may be used to endorse or promote products derived from this software | ||
| 20 | .\" without specific prior written permission. | ||
| 21 | .\" | ||
| 22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 32 | .\" SUCH DAMAGE. | ||
| 33 | .\" | ||
| 34 | .Dd $Mdocdate: December 19 2013 $ | ||
| 35 | .Dt STRNCPY 3 | ||
| 36 | .Os | ||
| 37 | .Sh NAME | ||
| 38 | .Nm strncpy | ||
| 39 | .Nd copy part of a string to another | ||
| 40 | .Sh SYNOPSIS | ||
| 41 | .In string.h | ||
| 42 | .Ft char * | ||
| 43 | .Fn strncpy "char *dst" "const char *src" "size_t len" | ||
| 44 | .Sh DESCRIPTION | ||
| 45 | The | ||
| 46 | .Fn strncpy | ||
| 47 | function copies not more than | ||
| 48 | .Fa len | ||
| 49 | characters from the string | ||
| 50 | .Fa src | ||
| 51 | to | ||
| 52 | .Fa dst . | ||
| 53 | If | ||
| 54 | .Fa src | ||
| 55 | is less than | ||
| 56 | .Fa len | ||
| 57 | characters long, | ||
| 58 | it appends | ||
| 59 | .Ql \e0 | ||
| 60 | characters for the rest of | ||
| 61 | .Fa len . | ||
| 62 | If the length of | ||
| 63 | .Fa src | ||
| 64 | is greater than or equal to | ||
| 65 | .Fa len , | ||
| 66 | .Fa dst | ||
| 67 | will | ||
| 68 | .Em not | ||
| 69 | be NUL-terminated. | ||
| 70 | .Pp | ||
| 71 | If the | ||
| 72 | .Fa src | ||
| 73 | and | ||
| 74 | .Fa dst | ||
| 75 | strings overlap, the behavior is undefined. | ||
| 76 | .Sh RETURN VALUES | ||
| 77 | The | ||
| 78 | .Fn strncpy | ||
| 79 | function returns | ||
| 80 | .Fa dst . | ||
| 81 | .Sh EXAMPLES | ||
| 82 | The following sets | ||
| 83 | .Va chararray | ||
| 84 | to | ||
| 85 | .Dq abc\e0\e0\e0 : | ||
| 86 | .Bd -literal -offset indent | ||
| 87 | (void)strncpy(chararray, "abc", 6); | ||
| 88 | .Ed | ||
| 89 | .Pp | ||
| 90 | The following sets | ||
| 91 | .Va chararray | ||
| 92 | to | ||
| 93 | .Dq abcdef | ||
| 94 | and does | ||
| 95 | .Em not | ||
| 96 | NUL terminate | ||
| 97 | .Va chararray | ||
| 98 | because the length of the source string is greater than or equal to the | ||
| 99 | length parameter. | ||
| 100 | .Fn strncpy | ||
| 101 | .Em only | ||
| 102 | NUL terminates the destination string when the length of the source | ||
| 103 | string is less than the length parameter. | ||
| 104 | .Bd -literal -offset indent | ||
| 105 | (void)strncpy(chararray, "abcdefgh", 6); | ||
| 106 | .Ed | ||
| 107 | .Pp | ||
| 108 | The following copies as many characters from | ||
| 109 | .Va input | ||
| 110 | to | ||
| 111 | .Va buf | ||
| 112 | as will fit and NUL terminates the result. | ||
| 113 | Because | ||
| 114 | .Fn strncpy | ||
| 115 | does | ||
| 116 | .Em not | ||
| 117 | guarantee to NUL terminate the string itself, it must be done by hand. | ||
| 118 | .Bd -literal -offset indent | ||
| 119 | char buf[BUFSIZ]; | ||
| 120 | |||
| 121 | (void)strncpy(buf, input, sizeof(buf) - 1); | ||
| 122 | buf[sizeof(buf) - 1] = '\e0'; | ||
| 123 | .Ed | ||
| 124 | .Pp | ||
| 125 | Note that | ||
| 126 | .Xr strlcpy 3 | ||
| 127 | is a better choice for this kind of operation. | ||
| 128 | The equivalent using | ||
| 129 | .Xr strlcpy 3 | ||
| 130 | is simply: | ||
| 131 | .Bd -literal -offset indent | ||
| 132 | (void)strlcpy(buf, input, sizeof(buf)); | ||
| 133 | .Ed | ||
| 134 | .Sh SEE ALSO | ||
| 135 | .Xr bcopy 3 , | ||
| 136 | .Xr memccpy 3 , | ||
| 137 | .Xr memcpy 3 , | ||
| 138 | .Xr memmove 3 , | ||
| 139 | .Xr strcat 3 , | ||
| 140 | .Xr strlcpy 3 , | ||
| 141 | .Xr strncat 3 , | ||
| 142 | .Xr wcscpy 3 , | ||
| 143 | .Xr wcslcpy 3 | ||
| 144 | .Sh STANDARDS | ||
| 145 | The | ||
| 146 | .Fn strncpy | ||
| 147 | function conforms to | ||
| 148 | .St -ansiC . | ||
| 149 | .Sh HISTORY | ||
| 150 | The | ||
| 151 | .Fn strncpy | ||
| 152 | function first appeared in | ||
| 153 | .At v7 . | ||
