diff options
| author | christos <christos@NetBSD.org> | 2009-04-10 23:13:38 +0000 |
|---|---|---|
| committer | christos <christos@NetBSD.org> | 2009-04-10 23:13:38 +0000 |
| commit | 3cf907e2b23d144e6d45dbf74771a1eebc85ef6f (patch) | |
| tree | 272088b7c6e2e8e30ceeab4f2d44632739ce9f3d /lib/libc/string | |
| parent | ce40f4f667a7c2d7af37c5f2de4d4bc16aee1762 (diff) | |
add memrchr
Diffstat (limited to 'lib/libc/string')
| -rw-r--r-- | lib/libc/string/Makefile.inc | 5 | ||||
| -rw-r--r-- | lib/libc/string/memchr.3 | 22 | ||||
| -rw-r--r-- | lib/libc/string/memrchr.c | 61 |
3 files changed, 81 insertions, 7 deletions
diff --git a/lib/libc/string/Makefile.inc b/lib/libc/string/Makefile.inc index 9ba9f361ca9..4d42670f904 100644 --- a/lib/libc/string/Makefile.inc +++ b/lib/libc/string/Makefile.inc @@ -1,5 +1,5 @@ # from: @(#)Makefile.inc 8.1 (Berkeley) 6/4/93 -# $NetBSD: Makefile.inc,v 1.69 2009/01/11 02:46:30 christos Exp $ +# $NetBSD: Makefile.inc,v 1.70 2009/04/10 23:13:38 christos Exp $ # string sources .PATH: ${ARCHDIR}/string ${.CURDIR}/string @@ -7,7 +7,7 @@ SRCS+= bm.c strcasecmp.c strncasecmp.c strcasestr.c strcoll.c strdup.c \ strerror.c strlcat.c strlcpy.c strmode.c strsignal.c strtok.c \ strtok_r.c strxfrm.c __strsignal.c strerror_r.c strndup.c \ - stresep.c + stresep.c memrchr.c # wide char SRCS+= wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \ @@ -69,6 +69,7 @@ MLINKS+=strcmp.3 strncmp.3 MLINKS+=strcpy.3 strncpy.3 MLINKS+=strlcpy.3 strlcat.3 MLINKS+=strstr.3 strcasestr.3 +MLINKS+=memchr.3 memrchr.3 MLINKS+=strtok.3 strtok_r.3 MLINKS+=strerror.3 strerror_r.3 strerror.3 perror.3 \ strerror.3 sys_errlist.3 strerror.3 sys_nerr.3 diff --git a/lib/libc/string/memchr.3 b/lib/libc/string/memchr.3 index 9927a1cf79b..1f04eff787c 100644 --- a/lib/libc/string/memchr.3 +++ b/lib/libc/string/memchr.3 @@ -30,13 +30,13 @@ .\" SUCH DAMAGE. .\" .\" from: @(#)memchr.3 8.1 (Berkeley) 6/4/93 -.\" $NetBSD: memchr.3,v 1.9 2006/10/16 08:48:45 wiz Exp $ +.\" $NetBSD: memchr.3,v 1.10 2009/04/10 23:13:38 christos Exp $ .\" -.Dd June 4, 1993 +.Dd April 10, 2009 .Dt MEMCHR 3 .Os .Sh NAME -.Nm memchr +.Nm memchr, memrchr .Nd locate byte in byte string .Sh LIBRARY .Lb libc @@ -44,6 +44,8 @@ .In string.h .Ft void * .Fn memchr "const void *b" "int c" "size_t len" +.Ft void * +.Fn memrchr "const void *b" "int c" "size_t len" .Sh DESCRIPTION The .Fn memchr @@ -53,11 +55,21 @@ locates the first occurrence of (converted to an unsigned char) in string .Fa b . +The +.Fn memrchr +function +locates the last occurrence of +.Fa c +(converted to an unsigned char) +in string +.Fa b . .Sh RETURN VALUES The .Fn memchr -function -returns a pointer to the byte located, or +and +.Fn memrchr +functions +return a pointer to the byte located, or .Dv NULL if no such byte exists within .Fa len diff --git a/lib/libc/string/memrchr.c b/lib/libc/string/memrchr.c new file mode 100644 index 00000000000..2043fefc852 --- /dev/null +++ b/lib/libc/string/memrchr.c @@ -0,0 +1,61 @@ +/* $NetBSD: memrchr.c,v 1.1 2009/04/10 23:13:38 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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. + */ +#include <sys/cdefs.h> +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: memrchr.c,v 1.1 2009/04/10 23:13:38 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include <assert.h> +#include <string.h> + +void * +memrchr(const void *s, int c, size_t n) +{ + _DIAGASSERT(s != NULL); + + if (n != 0) { + const unsigned char *p = s; + const unsigned char cmp = c; + + do { + if (*p-- == cmp) + return __UNCONST(p + 1); + } while (--n != 0); + } + return NULL; +} |
