Browse Source

Split out strncpy and strncat from strcpy and strcat manuals.

Requested by deraadt@
OPENBSD_5_5
millert 11 years ago
parent
commit
d14d049245
5 changed files with 308 additions and 168 deletions
  1. +8
    -9
      src/lib/libc/string/Makefile.inc
  2. +9
    -71
      src/lib/libc/string/strcat.3
  3. +10
    -88
      src/lib/libc/string/strcpy.3
  4. +128
    -0
      src/lib/libc/string/strncat.3
  5. +153
    -0
      src/lib/libc/string/strncpy.3

+ 8
- 9
src/lib/libc/string/Makefile.inc View File

@ -1,4 +1,4 @@
# $OpenBSD: Makefile.inc,v 1.31 2013/10/14 06:55:28 guenther Exp $
# $OpenBSD: Makefile.inc,v 1.32 2013/12/19 20:52:37 millert Exp $
# string sources # string sources
.PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/string ${LIBCSRCDIR}/string .PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/string ${LIBCSRCDIR}/string
@ -146,12 +146,13 @@ strrchr.do: rindex.c
MAN+= bm.3 bcmp.3 bcopy.3 bstring.3 bzero.3 ffs.3 memccpy.3 memchr.3 \ MAN+= bm.3 bcmp.3 bcopy.3 bstring.3 bzero.3 ffs.3 memccpy.3 memchr.3 \
memcmp.3 memcpy.3 memmem.3 memmove.3 memset.3 stpcpy.3 strcasecmp.3 \ memcmp.3 memcpy.3 memmem.3 memmove.3 memset.3 stpcpy.3 strcasecmp.3 \
strcat.3 strchr.3 strcmp.3 strcoll.3 strcpy.3 strcspn.3 strerror.3 \
string.3 strlen.3 strmode.3 strdup.3 strpbrk.3 strrchr.3 strsep.3 \
strsignal.3 strspn.3 strstr.3 strtok.3 strxfrm.3 swab.3 strlcpy.3 \
wcscasecmp.3 wcscat.3 wcschr.3 wcscmp.3 wcscpy.3 wcscspn.3 wcsdup.3 \
wcslcpy.3 wcslen.3 wcspbrk.3 wcsrchr.3 wcsspn.3 wcsstr.3 wcstok.3 \
wcswidth.3 wmemchr.3 wmemcmp.3 wmemcpy.3 wmemmove.3 wmemset.3
strcat.3 strchr.3 strcmp.3 strcoll.3 strcpy.3 strcspn.3 strdup.3 \
strerror.3 string.3 strlen.3 strmode.3 strncat.3 strncpy.3 strpbrk.3 \
strrchr.3 strsep.3 strsignal.3 strspn.3 strstr.3 strtok.3 strxfrm.3 \
swab.3 strlcpy.3 wcscasecmp.3 wcscat.3 wcschr.3 wcscmp.3 wcscpy.3 \
wcscspn.3 wcsdup.3 wcslcpy.3 wcslen.3 wcspbrk.3 wcsrchr.3 wcsspn.3 \
wcsstr.3 wcstok.3 wcswidth.3 wmemchr.3 wmemcmp.3 wmemcpy.3 wmemmove.3 \
wmemset.3
MLINKS+=bm.3 bm_comp.3 bm.3 bm_exec.3 bm.3 bm_free.3 MLINKS+=bm.3 bm_comp.3 bm.3 bm_exec.3 bm.3 bm_free.3
MLINKS+=memchr.3 memrchr.3 MLINKS+=memchr.3 memrchr.3
@ -159,9 +160,7 @@ MLINKS+=stpcpy.3 stpncpy.3
MLINKS+=strchr.3 index.3 MLINKS+=strchr.3 index.3
MLINKS+=strrchr.3 rindex.3 MLINKS+=strrchr.3 rindex.3
MLINKS+=strcasecmp.3 strncasecmp.3 MLINKS+=strcasecmp.3 strncasecmp.3
MLINKS+=strcat.3 strncat.3
MLINKS+=strcmp.3 strncmp.3 MLINKS+=strcmp.3 strncmp.3
MLINKS+=strcpy.3 strncpy.3
MLINKS+=strdup.3 strndup.3 MLINKS+=strdup.3 strndup.3
MLINKS+=strlcpy.3 strlcat.3 MLINKS+=strlcpy.3 strlcat.3
MLINKS+=strlen.3 strnlen.3 MLINKS+=strlen.3 strnlen.3


+ 9
- 71
src/lib/libc/string/strcat.3 View File

@ -1,4 +1,4 @@
.\" $OpenBSD: strcat.3,v 1.15 2013/07/17 05:42:11 schwarze Exp $
.\" $OpenBSD: strcat.3,v 1.16 2013/12/19 20:52:37 millert Exp $
.\" .\"
.\" Copyright (c) 1990, 1991 The Regents of the University of California. .\" Copyright (c) 1990, 1991 The Regents of the University of California.
.\" All rights reserved. .\" All rights reserved.
@ -31,91 +31,33 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE. .\" SUCH DAMAGE.
.\" .\"
.Dd $Mdocdate: July 17 2013 $
.Dd $Mdocdate: December 19 2013 $
.Dt STRCAT 3 .Dt STRCAT 3
.Os .Os
.Sh NAME .Sh NAME
.Nm strcat ,
.Nm strncat
.Nd concatenate strings
.Nm strcat
.Nd concatenate two strings
.Sh SYNOPSIS .Sh SYNOPSIS
.In string.h .In string.h
.Ft char * .Ft char *
.Fn strcat "char *s" "const char *append" .Fn strcat "char *s" "const char *append"
.Ft char *
.Fn strncat "char *s" "const char *append" "size_t count"
.Sh DESCRIPTION .Sh DESCRIPTION
The The
.Fn strcat .Fn strcat
and
.Fn strncat
functions append a copy of the NUL-terminated string
function appends a copy of the NUL-terminated string
.Fa append .Fa append
to the end of the NUL-terminated string to the end of the NUL-terminated string
.Fa s , .Fa s ,
then add a terminating
then adds a terminating
.Ql \e0 . .Ql \e0 .
The string The string
.Fa s .Fa s
must have sufficient space to hold the result. must have sufficient space to hold the result.
.Pp
The
.Fn strncat
function appends not more than
.Fa count
characters where space for the terminating
.Ql \e0
should not be included in
.Fa count .
.Sh RETURN VALUES .Sh RETURN VALUES
The The
.Fn strcat .Fn strcat
and
.Fn strncat
functions return the pointer
function return the pointer
.Fa s . .Fa s .
.Sh EXAMPLES
The following appends
.Dq Li abc
to
.Va chararray :
.Bd -literal -offset indent
char *letters = "abcdefghi";
(void)strncat(chararray, letters, 3);
.Ed
.Pp
The following example shows how to use
.Fn strncat
safely in conjunction with
.Xr strncpy 3 .
.Bd -literal -offset indent
char buf[BUFSIZ];
char *input, *suffix;
(void)strncpy(buf, input, sizeof(buf) - 1);
buf[sizeof(buf) - 1] = '\e0';
(void)strncat(buf, suffix, sizeof(buf) - 1 - strlen(buf));
.Ed
.Pp
The above will copy as many characters from
.Va input
to
.Va buf
as will fit.
It then appends as many characters from
.Va suffix
as will fit (or none
if there is no space).
For operations like this, the
.Xr strlcpy 3
and
.Xr strlcat 3
functions are a better choice, as shown below.
.Bd -literal -offset indent
(void)strlcpy(buf, input, sizeof(buf));
(void)strlcat(buf, suffix, sizeof(buf));
.Ed
.Sh SEE ALSO .Sh SEE ALSO
.Xr bcopy 3 , .Xr bcopy 3 ,
.Xr memccpy 3 , .Xr memccpy 3 ,
@ -123,21 +65,17 @@ functions are a better choice, as shown below.
.Xr memmove 3 , .Xr memmove 3 ,
.Xr strcpy 3 , .Xr strcpy 3 ,
.Xr strlcpy 3 , .Xr strlcpy 3 ,
.Xr strncat 3 ,
.Xr wcscat 3 , .Xr wcscat 3 ,
.Xr wcslcpy 3 .Xr wcslcpy 3
.Sh STANDARDS .Sh STANDARDS
The The
.Fn strcat .Fn strcat
and
.Fn strncat
functions conform to
function conforms to
.St -ansiC . .St -ansiC .
.Sh HISTORY .Sh HISTORY
The The
.Fn strcat .Fn strcat
function first appeared in the Programmer's Workbench (PWB/UNIX) function first appeared in the Programmer's Workbench (PWB/UNIX)
and was ported to and was ported to
.At v7 ;
.Fn strncat
first appeared in
.At v7 . .At v7 .

+ 10
- 88
src/lib/libc/string/strcpy.3 View File

@ -1,4 +1,4 @@
.\" $OpenBSD: strcpy.3,v 1.19 2013/09/25 21:49:31 millert Exp $
.\" $OpenBSD: strcpy.3,v 1.20 2013/12/19 20:52:37 millert Exp $
.\" .\"
.\" Copyright (c) 1990, 1991 The Regents of the University of California. .\" Copyright (c) 1990, 1991 The Regents of the University of California.
.\" All rights reserved. .\" All rights reserved.
@ -31,52 +31,31 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE. .\" SUCH DAMAGE.
.\" .\"
.Dd $Mdocdate: September 25 2013 $
.Dd $Mdocdate: December 19 2013 $
.Dt STRCPY 3 .Dt STRCPY 3
.Os .Os
.Sh NAME .Sh NAME
.Nm strcpy ,
.Nm strncpy
.Nd copy strings
.Nm strcpy
.Nd copy a string
.Sh SYNOPSIS .Sh SYNOPSIS
.In string.h .In string.h
.Ft char * .Ft char *
.Fn strcpy "char *dst" "const char *src" .Fn strcpy "char *dst" "const char *src"
.Ft char *
.Fn strncpy "char *dst" "const char *src" "size_t len"
.Sh DESCRIPTION .Sh DESCRIPTION
The The
.Fn strcpy .Fn strcpy
and
.Fn strncpy
functions copy the string
function copies the string
.Fa src .Fa src
to to
.Fa dst .Fa dst
(including the terminating (including the terminating
.Ql \e0 .Ql \e0
character). character).
.Pp
The
.Fn strncpy
function copies not more than
.Fa len
characters into
.Fa dst ,
appending
.Ql \e0
characters if
.Fa src
is less than
.Fa len
characters long, and
.Em not
terminating
The string
.Fa dst .Fa dst
if the length of
must be at least as large as
.Fa src .Fa src
is greater than or equal to
.Fa len .
to hold the result.
.Pp .Pp
If the If the
.Fa src .Fa src
@ -86,63 +65,8 @@ strings overlap, the behavior is undefined.
.Sh RETURN VALUES .Sh RETURN VALUES
The The
.Fn strcpy .Fn strcpy
and
.Fn strncpy
functions return
function returns
.Fa dst . .Fa dst .
.Sh EXAMPLES
The following sets
.Va chararray
to
.Dq abc\e0\e0\e0 :
.Bd -literal -offset indent
(void)strncpy(chararray, "abc", 6);
.Ed
.Pp
The following sets
.Va chararray
to
.Dq abcdef
and does
.Em not
NUL terminate
.Va chararray
because the length of the source string is greater than or equal to the
length parameter.
.Fn strncpy
.Em only
NUL terminates the destination string when the length of the source
string is less than the length parameter.
.Bd -literal -offset indent
(void)strncpy(chararray, "abcdefgh", 6);
.Ed
.Pp
The following copies as many characters from
.Va input
to
.Va buf
as will fit and NUL terminates the result.
Because
.Fn strncpy
does
.Em not
guarantee to NUL terminate the string itself, it must be done by hand.
.Bd -literal -offset indent
char buf[BUFSIZ];
(void)strncpy(buf, input, sizeof(buf) - 1);
buf[sizeof(buf) - 1] = '\e0';
.Ed
.Pp
Note that
.Xr strlcpy 3
is a better choice for this kind of operation.
The equivalent using
.Xr strlcpy 3
is simply:
.Bd -literal -offset indent
(void)strlcpy(buf, input, sizeof(buf));
.Ed
.Sh SEE ALSO .Sh SEE ALSO
.Xr bcopy 3 , .Xr bcopy 3 ,
.Xr memccpy 3 , .Xr memccpy 3 ,
@ -150,6 +74,7 @@ is simply:
.Xr memmove 3 , .Xr memmove 3 ,
.Xr strcat 3 , .Xr strcat 3 ,
.Xr strlcpy 3 , .Xr strlcpy 3 ,
.Xr strncpy 3 ,
.Xr wcscpy 3 , .Xr wcscpy 3 ,
.Xr wcslcpy 3 .Xr wcslcpy 3
.Sh STANDARDS .Sh STANDARDS
@ -164,7 +89,4 @@ The
.Fn strcpy .Fn strcpy
function first appeared in the Programmer's Workbench (PWB/UNIX) function first appeared in the Programmer's Workbench (PWB/UNIX)
and was ported to and was ported to
.At v7 ;
.Fn strncpy
first appeared in
.At v7 . .At v7 .

+ 128
- 0
src/lib/libc/string/strncat.3 View File

@ -0,0 +1,128 @@
.\" $OpenBSD: strncat.3,v 1.1 2013/12/19 20:52:37 millert Exp $
.\"
.\" Copyright (c) 1990, 1991 The Regents of the University of California.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" Chris Torek and the American National Standards Committee X3,
.\" on Information Processing Systems.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.Dd $Mdocdate: December 19 2013 $
.Dt STRNCAT 3
.Os
.Sh NAME
.Nm strncat
.Nd concatenate a string with part of another
.Sh SYNOPSIS
.In string.h
.Ft char *
.Fn strncat "char *s" "const char *append" "size_t count"
.Sh DESCRIPTION
The
.Fn strncat
function appends not more than
.Fa count
characters of the NUL-terminated string
.Fa append
to the end of the NUL-terminated string
.Fa s .
Space for the terminating
.Ql \e0
should not be included in
.Fa count .
The string
.Fa s
must have sufficient space to hold the result.
.Sh RETURN VALUES
The
.Fn strncat
function returns the pointer
.Fa s .
.Sh EXAMPLES
The following appends
.Dq Li abc
to
.Va chararray :
.Bd -literal -offset indent
char *letters = "abcdefghi";
(void)strncat(chararray, letters, 3);
.Ed
.Pp
The following example shows how to use
.Fn strncat
safely in conjunction with
.Xr strncpy 3 .
.Bd -literal -offset indent
char buf[BUFSIZ];
char *input, *suffix;
(void)strncpy(buf, input, sizeof(buf) - 1);
buf[sizeof(buf) - 1] = '\e0';
(void)strncat(buf, suffix, sizeof(buf) - 1 - strlen(buf));
.Ed
.Pp
The above will copy as many characters from
.Va input
to
.Va buf
as will fit.
It then appends as many characters from
.Va suffix
as will fit (or none
if there is no space).
For operations like this, the
.Xr strlcpy 3
and
.Xr strlcat 3
functions are a better choice, as shown below.
.Bd -literal -offset indent
(void)strlcpy(buf, input, sizeof(buf));
(void)strlcat(buf, suffix, sizeof(buf));
.Ed
.Sh SEE ALSO
.Xr bcopy 3 ,
.Xr memccpy 3 ,
.Xr memcpy 3 ,
.Xr memmove 3 ,
.Xr strcat 3 ,
.Xr strcpy 3 ,
.Xr strlcpy 3 ,
.Xr wcscat 3 ,
.Xr wcslcpy 3
.Sh STANDARDS
The
.Fn strcat
and
.Fn strncat
functions conform to
.St -ansiC .
.Sh HISTORY
The
.Fn strncat
function first first appeared in
.At v7 .

+ 153
- 0
src/lib/libc/string/strncpy.3 View File

@ -0,0 +1,153 @@
.\" $OpenBSD: strncpy.3,v 1.1 2013/12/19 20:52:37 millert Exp $
.\"
.\" Copyright (c) 1990, 1991 The Regents of the University of California.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" Chris Torek and the American National Standards Committee X3,
.\" on Information Processing Systems.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.Dd $Mdocdate: December 19 2013 $
.Dt STRNCPY 3
.Os
.Sh NAME
.Nm strncpy
.Nd copy part of a string to another
.Sh SYNOPSIS
.In string.h
.Ft char *
.Fn strncpy "char *dst" "const char *src" "size_t len"
.Sh DESCRIPTION
The
.Fn strncpy
function copies not more than
.Fa len
characters from the string
.Fa src
to
.Fa dst .
If
.Fa src
is less than
.Fa len
characters long,
it appends
.Ql \e0
characters for the rest of
.Fa len .
If the length of
.Fa src
is greater than or equal to
.Fa len ,
.Fa dst
will
.Em not
be NUL-terminated.
.Pp
If the
.Fa src
and
.Fa dst
strings overlap, the behavior is undefined.
.Sh RETURN VALUES
The
.Fn strncpy
function returns
.Fa dst .
.Sh EXAMPLES
The following sets
.Va chararray
to
.Dq abc\e0\e0\e0 :
.Bd -literal -offset indent
(void)strncpy(chararray, "abc", 6);
.Ed
.Pp
The following sets
.Va chararray
to
.Dq abcdef
and does
.Em not
NUL terminate
.Va chararray
because the length of the source string is greater than or equal to the
length parameter.
.Fn strncpy
.Em only
NUL terminates the destination string when the length of the source
string is less than the length parameter.
.Bd -literal -offset indent
(void)strncpy(chararray, "abcdefgh", 6);
.Ed
.Pp
The following copies as many characters from
.Va input
to
.Va buf
as will fit and NUL terminates the result.
Because
.Fn strncpy
does
.Em not
guarantee to NUL terminate the string itself, it must be done by hand.
.Bd -literal -offset indent
char buf[BUFSIZ];
(void)strncpy(buf, input, sizeof(buf) - 1);
buf[sizeof(buf) - 1] = '\e0';
.Ed
.Pp
Note that
.Xr strlcpy 3
is a better choice for this kind of operation.
The equivalent using
.Xr strlcpy 3
is simply:
.Bd -literal -offset indent
(void)strlcpy(buf, input, sizeof(buf));
.Ed
.Sh SEE ALSO
.Xr bcopy 3 ,
.Xr memccpy 3 ,
.Xr memcpy 3 ,
.Xr memmove 3 ,
.Xr strcat 3 ,
.Xr strlcpy 3 ,
.Xr strncat 3 ,
.Xr wcscpy 3 ,
.Xr wcslcpy 3
.Sh STANDARDS
The
.Fn strncpy
function conforms to
.St -ansiC .
.Sh HISTORY
The
.Fn strncpy
function first appeared in
.At v7 .

Loading…
Cancel
Save