From 0800e860cf192fde131a6141be73b2e01cedd301 Mon Sep 17 00:00:00 2001 From: tedu <> Date: Tue, 18 May 2010 22:24:55 +0000 Subject: add posix_madvise, posix_memalign, strndup, and strnlen. mostly from brad and millert, with hints from guenther, jmc, and otto I think. ok previous. --- src/lib/libc/string/Makefile.inc | 8 +++++--- src/lib/libc/string/strdup.3 | 31 ++++++++++++++++++++++++++++--- src/lib/libc/string/strlen.3 | 39 ++++++++++++++++++++++++++++++++++++--- src/lib/libc/string/strndup.c | 39 +++++++++++++++++++++++++++++++++++++++ src/lib/libc/string/strnlen.c | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 142 insertions(+), 9 deletions(-) create mode 100644 src/lib/libc/string/strndup.c create mode 100644 src/lib/libc/string/strnlen.c (limited to 'src/lib/libc/string') 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 @@ -# $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/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 @@ -.\" $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 .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/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 @@ .\" 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 .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/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 @@ +/* $OpenBSD: strndup.c,v 1.1 2010/05/18 22:24:55 tedu Exp $ */ + +/* + * Copyright (c) 2010 Todd C. Miller + * + * 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 + +#include +#include +#include + +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/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 @@ +/* $OpenBSD: strnlen.c,v 1.1 2010/05/18 22:24:55 tedu Exp $ */ + +/* + * Copyright (c) 2010 Todd C. Miller + * + * 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 + +#include + +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); +} -- cgit v1.2.3-55-g6feb