summaryrefslogtreecommitdiffstats
path: root/lib/libc/string
diff options
context:
space:
mode:
authortedu <tedu@openbsd.org>2010-05-18 22:24:54 +0000
committertedu <tedu@openbsd.org>2010-05-18 22:24:54 +0000
commit243f393511fee0e3dea053d89336d8b8f85b60ee (patch)
treef0de8eb1b6b3091104481b8be30b613f807883d6 /lib/libc/string
parentAdd as yet untested support for the 82576 quad copper ET2 (diff)
downloadwireguard-openbsd-243f393511fee0e3dea053d89336d8b8f85b60ee.tar.xz
wireguard-openbsd-243f393511fee0e3dea053d89336d8b8f85b60ee.zip
add posix_madvise, posix_memalign, strndup, and strnlen. mostly from
brad and millert, with hints from guenther, jmc, and otto I think. ok previous.
Diffstat (limited to 'lib/libc/string')
-rw-r--r--lib/libc/string/Makefile.inc8
-rw-r--r--lib/libc/string/strdup.331
-rw-r--r--lib/libc/string/strlen.339
-rw-r--r--lib/libc/string/strndup.c39
-rw-r--r--lib/libc/string/strnlen.c34
5 files changed, 142 insertions, 9 deletions
diff --git a/lib/libc/string/Makefile.inc b/lib/libc/string/Makefile.inc
index 8aa072a2893..3264b799f17 100644
--- a/lib/libc/string/Makefile.inc
+++ b/lib/libc/string/Makefile.inc
@@ -1,11 +1,11 @@
-# $OpenBSD: Makefile.inc,v 1.21 2010/02/03 20:49:00 miod Exp $
+# $OpenBSD: Makefile.inc,v 1.22 2010/05/18 22:24:55 tedu Exp $
# string sources
.PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/string ${LIBCSRCDIR}/string
SRCS+= bm.c memccpy.c memrchr.c strcasecmp.c strcasestr.c strcoll.c strdup.c \
- strerror.c strerror_r.c strlcat.c strmode.c strsignal.c strtok.c \
- strxfrm.c \
+ strerror.c strerror_r.c strlcat.c strmode.c strndup.c strnlen.c \
+ strsignal.c strtok.c strxfrm.c \
wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \
wcslen.c wcsncat.c wcsncmp.c wcsncpy.c wcspbrk.c wcsrchr.c wcsspn.c \
wcsstr.c wcstok.c wcswcs.c wcswidth.c wmemchr.c wmemcmp.c wmemcpy.c \
@@ -153,7 +153,9 @@ MLINKS+=strcasecmp.3 strncasecmp.3
MLINKS+=strcat.3 strncat.3
MLINKS+=strcmp.3 strncmp.3
MLINKS+=strcpy.3 strncpy.3
+MLINKS+=strdup.3 strndup.3
MLINKS+=strlcpy.3 strlcat.3
+MLINKS+=strlen.3 strnlen.3
MLINKS+=strstr.3 strcasestr.3
MLINKS+=strtok.3 strtok_r.3
MLINKS+=strerror.3 strerror_r.3
diff --git a/lib/libc/string/strdup.3 b/lib/libc/string/strdup.3
index 60a74627771..05dcb794f04 100644
--- a/lib/libc/string/strdup.3
+++ b/lib/libc/string/strdup.3
@@ -1,4 +1,4 @@
-.\" $OpenBSD: strdup.3,v 1.15 2010/03/24 14:47:46 kettenis Exp $
+.\" $OpenBSD: strdup.3,v 1.16 2010/05/18 22:24:55 tedu Exp $
.\"
.\" Copyright (c) 1990, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@@ -29,16 +29,19 @@
.\"
.\" @(#)strdup.3 8.1 (Berkeley) 6/9/93
.\"
-.Dd $Mdocdate: March 24 2010 $
+.Dd $Mdocdate: May 18 2010 $
.Dt STRDUP 3
.Os
.Sh NAME
-.Nm strdup
+.Nm strdup ,
+.Nm strndup
.Nd save a copy of a string
.Sh SYNOPSIS
.Fd #include <string.h>
.Ft char *
.Fn strdup "const char *s"
+.Ft char *
+.Fn strndup "const char *s" "size_t maxlen"
.Sh DESCRIPTION
The
.Fn strdup
@@ -48,6 +51,16 @@ does the copy, and returns a pointer to it.
The pointer may subsequently be used as an argument to the function
.Xr free 3 .
.Pp
+The
+.Fn strndup
+function behaves similarly to
+.Nm strdup
+but only copies up to
+.Fa maxlen
+characters from
+.Fa s .
+The resulting string is always NUL-terminated.
+.Pp
If insufficient memory is available,
.Dv NULL
is returned.
@@ -83,3 +96,15 @@ The
.Fn strdup
function first appeared in
.Bx 4.4 .
+.Pp
+The
+.Fn strndup
+function first appeared in
+.Ox 4.8 .
+.Sh STANDARDS
+The
+.Fn strdup
+and
+.Fn strndup
+functions conform to
+.St -p1003.1-2008 .
diff --git a/lib/libc/string/strlen.3 b/lib/libc/string/strlen.3
index f8a4efe9d24..3b5f9a10c12 100644
--- a/lib/libc/string/strlen.3
+++ b/lib/libc/string/strlen.3
@@ -29,29 +29,55 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.\" $OpenBSD: strlen.3,v 1.6 2007/05/31 19:19:32 jmc Exp $
+.\" $OpenBSD: strlen.3,v 1.7 2010/05/18 22:24:55 tedu Exp $
.\"
-.Dd $Mdocdate: May 31 2007 $
+.Dd $Mdocdate: May 18 2010 $
.Dt STRLEN 3
.Os
.Sh NAME
-.Nm strlen
+.Nm strlen ,
+.Nm strnlen
.Nd find length of a string
.Sh SYNOPSIS
.Fd #include <string.h>
.Ft size_t
.Fn strlen "const char *s"
+.Ft size_t
+.Fn strnlen "const char *s" "size_t maxlen"
.Sh DESCRIPTION
The
.Fn strlen
function computes the length of the string
.Fa s .
+.Pp
+The
+.Fn strnlen
+function computes the length of the string
+.Fa s ,
+up to
+.Fa maxlen
+characters.
+The
+.Fn strnlen
+function will never attempt to address more than
+.Fa maxlen
+characters, making it suitable for use with character arrays that are
+not guaranteed to be NUL-terminated.
+.Pp
.Sh RETURN VALUES
The
.Fn strlen
function returns the number of characters that precede the terminating
.Tn NUL
character.
+.Pp
+The
+.Fn strnlen
+function returns the number of characters that precede the terminating
+.Tn NUL
+or
+.Fa maxlen ,
+whichever is smaller.
.Sh SEE ALSO
.Xr string 3
.Sh STANDARDS
@@ -59,3 +85,10 @@ The
.Fn strlen
function conforms to
.St -ansiC .
+.Pp
+The
+.Fn strlen
+and
+.Fn strnlen
+functions conform to
+.St -p1003.1-2008 .
diff --git a/lib/libc/string/strndup.c b/lib/libc/string/strndup.c
new file mode 100644
index 00000000000..27701ac555a
--- /dev/null
+++ b/lib/libc/string/strndup.c
@@ -0,0 +1,39 @@
+/* $OpenBSD: strndup.c,v 1.1 2010/05/18 22:24:55 tedu Exp $ */
+
+/*
+ * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+
+char *
+strndup(const char *str, size_t maxlen)
+{
+ char *copy;
+ size_t len;
+
+ len = strnlen(str, maxlen);
+ copy = malloc(len + 1);
+ if (copy != NULL) {
+ (void)memcpy(copy, str, len);
+ copy[len] = '\0';
+ }
+
+ return copy;
+}
diff --git a/lib/libc/string/strnlen.c b/lib/libc/string/strnlen.c
new file mode 100644
index 00000000000..5c999947444
--- /dev/null
+++ b/lib/libc/string/strnlen.c
@@ -0,0 +1,34 @@
+/* $OpenBSD: strnlen.c,v 1.1 2010/05/18 22:24:55 tedu Exp $ */
+
+/*
+ * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+
+#include <string.h>
+
+size_t
+strnlen(const char *str, size_t maxlen)
+{
+ const char *cp, *ep;
+ size_t len;
+
+ ep = str + maxlen;
+ for (cp = str; cp < ep && *cp != '\0'; cp++)
+ ;
+
+ return (size_t)(cp - str);
+}