summaryrefslogtreecommitdiff
path: root/src/lib/libc/string
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/string')
-rw-r--r--src/lib/libc/string/Makefile.inc8
-rw-r--r--src/lib/libc/string/strdup.331
-rw-r--r--src/lib/libc/string/strlen.339
-rw-r--r--src/lib/libc/string/strndup.c39
-rw-r--r--src/lib/libc/string/strnlen.c34
5 files changed, 142 insertions, 9 deletions
diff --git a/src/lib/libc/string/Makefile.inc b/src/lib/libc/string/Makefile.inc
index 8aa072a289..3264b799f1 100644
--- a/src/lib/libc/string/Makefile.inc
+++ b/src/lib/libc/string/Makefile.inc
@@ -1,11 +1,11 @@
1# $OpenBSD: Makefile.inc,v 1.21 2010/02/03 20:49:00 miod Exp $ 1# $OpenBSD: Makefile.inc,v 1.22 2010/05/18 22:24:55 tedu 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
5 5
6SRCS+= bm.c memccpy.c memrchr.c strcasecmp.c strcasestr.c strcoll.c strdup.c \ 6SRCS+= bm.c memccpy.c memrchr.c strcasecmp.c strcasestr.c strcoll.c strdup.c \
7 strerror.c strerror_r.c strlcat.c strmode.c strsignal.c strtok.c \ 7 strerror.c strerror_r.c strlcat.c strmode.c strndup.c strnlen.c \
8 strxfrm.c \ 8 strsignal.c strtok.c strxfrm.c \
9 wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \ 9 wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \
10 wcslen.c wcsncat.c wcsncmp.c wcsncpy.c wcspbrk.c wcsrchr.c wcsspn.c \ 10 wcslen.c wcsncat.c wcsncmp.c wcsncpy.c wcspbrk.c wcsrchr.c wcsspn.c \
11 wcsstr.c wcstok.c wcswcs.c wcswidth.c wmemchr.c wmemcmp.c wmemcpy.c \ 11 wcsstr.c wcstok.c wcswcs.c wcswidth.c wmemchr.c wmemcmp.c wmemcpy.c \
@@ -153,7 +153,9 @@ MLINKS+=strcasecmp.3 strncasecmp.3
153MLINKS+=strcat.3 strncat.3 153MLINKS+=strcat.3 strncat.3
154MLINKS+=strcmp.3 strncmp.3 154MLINKS+=strcmp.3 strncmp.3
155MLINKS+=strcpy.3 strncpy.3 155MLINKS+=strcpy.3 strncpy.3
156MLINKS+=strdup.3 strndup.3
156MLINKS+=strlcpy.3 strlcat.3 157MLINKS+=strlcpy.3 strlcat.3
158MLINKS+=strlen.3 strnlen.3
157MLINKS+=strstr.3 strcasestr.3 159MLINKS+=strstr.3 strcasestr.3
158MLINKS+=strtok.3 strtok_r.3 160MLINKS+=strtok.3 strtok_r.3
159MLINKS+=strerror.3 strerror_r.3 161MLINKS+=strerror.3 strerror_r.3
diff --git a/src/lib/libc/string/strdup.3 b/src/lib/libc/string/strdup.3
index 60a7462777..05dcb794f0 100644
--- a/src/lib/libc/string/strdup.3
+++ b/src/lib/libc/string/strdup.3
@@ -1,4 +1,4 @@
1.\" $OpenBSD: strdup.3,v 1.15 2010/03/24 14:47:46 kettenis Exp $ 1.\" $OpenBSD: strdup.3,v 1.16 2010/05/18 22:24:55 tedu Exp $
2.\" 2.\"
3.\" Copyright (c) 1990, 1991, 1993 3.\" Copyright (c) 1990, 1991, 1993
4.\" The Regents of the University of California. All rights reserved. 4.\" The Regents of the University of California. All rights reserved.
@@ -29,16 +29,19 @@
29.\" 29.\"
30.\" @(#)strdup.3 8.1 (Berkeley) 6/9/93 30.\" @(#)strdup.3 8.1 (Berkeley) 6/9/93
31.\" 31.\"
32.Dd $Mdocdate: March 24 2010 $ 32.Dd $Mdocdate: May 18 2010 $
33.Dt STRDUP 3 33.Dt STRDUP 3
34.Os 34.Os
35.Sh NAME 35.Sh NAME
36.Nm strdup 36.Nm strdup ,
37.Nm strndup
37.Nd save a copy of a string 38.Nd save a copy of a string
38.Sh SYNOPSIS 39.Sh SYNOPSIS
39.Fd #include <string.h> 40.Fd #include <string.h>
40.Ft char * 41.Ft char *
41.Fn strdup "const char *s" 42.Fn strdup "const char *s"
43.Ft char *
44.Fn strndup "const char *s" "size_t maxlen"
42.Sh DESCRIPTION 45.Sh DESCRIPTION
43The 46The
44.Fn strdup 47.Fn strdup
@@ -48,6 +51,16 @@ does the copy, and returns a pointer to it.
48The pointer may subsequently be used as an argument to the function 51The pointer may subsequently be used as an argument to the function
49.Xr free 3 . 52.Xr free 3 .
50.Pp 53.Pp
54The
55.Fn strndup
56function behaves similarly to
57.Nm strdup
58but only copies up to
59.Fa maxlen
60characters from
61.Fa s .
62The resulting string is always NUL-terminated.
63.Pp
51If insufficient memory is available, 64If insufficient memory is available,
52.Dv NULL 65.Dv NULL
53is returned. 66is returned.
@@ -83,3 +96,15 @@ The
83.Fn strdup 96.Fn strdup
84function first appeared in 97function first appeared in
85.Bx 4.4 . 98.Bx 4.4 .
99.Pp
100The
101.Fn strndup
102function first appeared in
103.Ox 4.8 .
104.Sh STANDARDS
105The
106.Fn strdup
107and
108.Fn strndup
109functions conform to
110.St -p1003.1-2008 .
diff --git a/src/lib/libc/string/strlen.3 b/src/lib/libc/string/strlen.3
index f8a4efe9d2..3b5f9a10c1 100644
--- a/src/lib/libc/string/strlen.3
+++ b/src/lib/libc/string/strlen.3
@@ -29,29 +29,55 @@
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE. 30.\" SUCH DAMAGE.
31.\" 31.\"
32.\" $OpenBSD: strlen.3,v 1.6 2007/05/31 19:19:32 jmc Exp $ 32.\" $OpenBSD: strlen.3,v 1.7 2010/05/18 22:24:55 tedu Exp $
33.\" 33.\"
34.Dd $Mdocdate: May 31 2007 $ 34.Dd $Mdocdate: May 18 2010 $
35.Dt STRLEN 3 35.Dt STRLEN 3
36.Os 36.Os
37.Sh NAME 37.Sh NAME
38.Nm strlen 38.Nm strlen ,
39.Nm strnlen
39.Nd find length of a string 40.Nd find length of a string
40.Sh SYNOPSIS 41.Sh SYNOPSIS
41.Fd #include <string.h> 42.Fd #include <string.h>
42.Ft size_t 43.Ft size_t
43.Fn strlen "const char *s" 44.Fn strlen "const char *s"
45.Ft size_t
46.Fn strnlen "const char *s" "size_t maxlen"
44.Sh DESCRIPTION 47.Sh DESCRIPTION
45The 48The
46.Fn strlen 49.Fn strlen
47function computes the length of the string 50function computes the length of the string
48.Fa s . 51.Fa s .
52.Pp
53The
54.Fn strnlen
55function computes the length of the string
56.Fa s ,
57up to
58.Fa maxlen
59characters.
60The
61.Fn strnlen
62function will never attempt to address more than
63.Fa maxlen
64characters, making it suitable for use with character arrays that are
65not guaranteed to be NUL-terminated.
66.Pp
49.Sh RETURN VALUES 67.Sh RETURN VALUES
50The 68The
51.Fn strlen 69.Fn strlen
52function returns the number of characters that precede the terminating 70function returns the number of characters that precede the terminating
53.Tn NUL 71.Tn NUL
54character. 72character.
73.Pp
74The
75.Fn strnlen
76function returns the number of characters that precede the terminating
77.Tn NUL
78or
79.Fa maxlen ,
80whichever is smaller.
55.Sh SEE ALSO 81.Sh SEE ALSO
56.Xr string 3 82.Xr string 3
57.Sh STANDARDS 83.Sh STANDARDS
@@ -59,3 +85,10 @@ The
59.Fn strlen 85.Fn strlen
60function conforms to 86function conforms to
61.St -ansiC . 87.St -ansiC .
88.Pp
89The
90.Fn strlen
91and
92.Fn strnlen
93functions conform to
94.St -p1003.1-2008 .
diff --git a/src/lib/libc/string/strndup.c b/src/lib/libc/string/strndup.c
new file mode 100644
index 0000000000..27701ac555
--- /dev/null
+++ b/src/lib/libc/string/strndup.c
@@ -0,0 +1,39 @@
1/* $OpenBSD: strndup.c,v 1.1 2010/05/18 22:24:55 tedu Exp $ */
2
3/*
4 * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <sys/types.h>
20
21#include <stddef.h>
22#include <stdlib.h>
23#include <string.h>
24
25char *
26strndup(const char *str, size_t maxlen)
27{
28 char *copy;
29 size_t len;
30
31 len = strnlen(str, maxlen);
32 copy = malloc(len + 1);
33 if (copy != NULL) {
34 (void)memcpy(copy, str, len);
35 copy[len] = '\0';
36 }
37
38 return copy;
39}
diff --git a/src/lib/libc/string/strnlen.c b/src/lib/libc/string/strnlen.c
new file mode 100644
index 0000000000..5c99994744
--- /dev/null
+++ b/src/lib/libc/string/strnlen.c
@@ -0,0 +1,34 @@
1/* $OpenBSD: strnlen.c,v 1.1 2010/05/18 22:24:55 tedu Exp $ */
2
3/*
4 * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <sys/types.h>
20
21#include <string.h>
22
23size_t
24strnlen(const char *str, size_t maxlen)
25{
26 const char *cp, *ep;
27 size_t len;
28
29 ep = str + maxlen;
30 for (cp = str; cp < ep && *cp != '\0'; cp++)
31 ;
32
33 return (size_t)(cp - str);
34}