diff options
Diffstat (limited to 'src/lib/libc/string')
-rw-r--r-- | src/lib/libc/string/Makefile.inc | 8 | ||||
-rw-r--r-- | src/lib/libc/string/strdup.3 | 31 | ||||
-rw-r--r-- | src/lib/libc/string/strlen.3 | 39 | ||||
-rw-r--r-- | src/lib/libc/string/strndup.c | 39 | ||||
-rw-r--r-- | src/lib/libc/string/strnlen.c | 34 |
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 | ||
6 | SRCS+= bm.c memccpy.c memrchr.c strcasecmp.c strcasestr.c strcoll.c strdup.c \ | 6 | SRCS+= 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 | |||
153 | MLINKS+=strcat.3 strncat.3 | 153 | MLINKS+=strcat.3 strncat.3 |
154 | MLINKS+=strcmp.3 strncmp.3 | 154 | MLINKS+=strcmp.3 strncmp.3 |
155 | MLINKS+=strcpy.3 strncpy.3 | 155 | MLINKS+=strcpy.3 strncpy.3 |
156 | MLINKS+=strdup.3 strndup.3 | ||
156 | MLINKS+=strlcpy.3 strlcat.3 | 157 | MLINKS+=strlcpy.3 strlcat.3 |
158 | MLINKS+=strlen.3 strnlen.3 | ||
157 | MLINKS+=strstr.3 strcasestr.3 | 159 | MLINKS+=strstr.3 strcasestr.3 |
158 | MLINKS+=strtok.3 strtok_r.3 | 160 | MLINKS+=strtok.3 strtok_r.3 |
159 | MLINKS+=strerror.3 strerror_r.3 | 161 | MLINKS+=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 |
43 | The | 46 | The |
44 | .Fn strdup | 47 | .Fn strdup |
@@ -48,6 +51,16 @@ does the copy, and returns a pointer to it. | |||
48 | The pointer may subsequently be used as an argument to the function | 51 | The pointer may subsequently be used as an argument to the function |
49 | .Xr free 3 . | 52 | .Xr free 3 . |
50 | .Pp | 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 | ||
51 | If insufficient memory is available, | 64 | If insufficient memory is available, |
52 | .Dv NULL | 65 | .Dv NULL |
53 | is returned. | 66 | is returned. |
@@ -83,3 +96,15 @@ The | |||
83 | .Fn strdup | 96 | .Fn strdup |
84 | function first appeared in | 97 | function first appeared in |
85 | .Bx 4.4 . | 98 | .Bx 4.4 . |
99 | .Pp | ||
100 | The | ||
101 | .Fn strndup | ||
102 | function first appeared in | ||
103 | .Ox 4.8 . | ||
104 | .Sh STANDARDS | ||
105 | The | ||
106 | .Fn strdup | ||
107 | and | ||
108 | .Fn strndup | ||
109 | functions 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 |
45 | The | 48 | The |
46 | .Fn strlen | 49 | .Fn strlen |
47 | function computes the length of the string | 50 | function computes the length of the string |
48 | .Fa s . | 51 | .Fa s . |
52 | .Pp | ||
53 | The | ||
54 | .Fn strnlen | ||
55 | function computes the length of the string | ||
56 | .Fa s , | ||
57 | up to | ||
58 | .Fa maxlen | ||
59 | characters. | ||
60 | The | ||
61 | .Fn strnlen | ||
62 | function will never attempt to address more than | ||
63 | .Fa maxlen | ||
64 | characters, making it suitable for use with character arrays that are | ||
65 | not guaranteed to be NUL-terminated. | ||
66 | .Pp | ||
49 | .Sh RETURN VALUES | 67 | .Sh RETURN VALUES |
50 | The | 68 | The |
51 | .Fn strlen | 69 | .Fn strlen |
52 | function returns the number of characters that precede the terminating | 70 | function returns the number of characters that precede the terminating |
53 | .Tn NUL | 71 | .Tn NUL |
54 | character. | 72 | character. |
73 | .Pp | ||
74 | The | ||
75 | .Fn strnlen | ||
76 | function returns the number of characters that precede the terminating | ||
77 | .Tn NUL | ||
78 | or | ||
79 | .Fa maxlen , | ||
80 | whichever 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 |
60 | function conforms to | 86 | function conforms to |
61 | .St -ansiC . | 87 | .St -ansiC . |
88 | .Pp | ||
89 | The | ||
90 | .Fn strlen | ||
91 | and | ||
92 | .Fn strnlen | ||
93 | functions 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 | |||
25 | char * | ||
26 | strndup(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 | |||
23 | size_t | ||
24 | strnlen(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 | } | ||