From f5ac8d80cf5a9493ea5033c2c37e87965770edbf Mon Sep 17 00:00:00 2001 From: millert <> Date: Mon, 3 Sep 2007 14:36:40 +0000 Subject: [PATCH] add memrchr(3) --- src/include/string.h | 3 ++- src/lib/libc/string/Makefile.inc | 5 +++-- src/lib/libc/string/memchr.3 | 25 ++++++++++++++++++--- src/lib/libc/string/memrchr.c | 38 ++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 src/lib/libc/string/memrchr.c diff --git a/src/include/string.h b/src/include/string.h index 0c42e57a..52f3f718 100644 --- a/src/include/string.h +++ b/src/include/string.h @@ -1,4 +1,4 @@ -/* $OpenBSD: string.h,v 1.17 2006/01/06 18:53:04 millert Exp $ */ +/* $OpenBSD: string.h,v 1.18 2007/09/03 14:36:40 millert Exp $ */ /* $NetBSD: string.h,v 1.6 1994/10/26 00:56:30 cgd Exp $ */ /*- @@ -53,6 +53,7 @@ typedef __size_t size_t; __BEGIN_DECLS void *memchr(const void *, int, size_t); +void *memrchr(const void *, int, size_t); int memcmp(const void *, const void *, size_t); void *memcpy(void *, const void *, size_t) __attribute__ ((__bounded__(__buffer__,1,3))) diff --git a/src/lib/libc/string/Makefile.inc b/src/lib/libc/string/Makefile.inc index 72ec34e6..f003be03 100644 --- a/src/lib/libc/string/Makefile.inc +++ b/src/lib/libc/string/Makefile.inc @@ -1,9 +1,9 @@ -# $OpenBSD: Makefile.inc,v 1.18 2005/10/29 10:05:11 espie Exp $ +# $OpenBSD: Makefile.inc,v 1.19 2007/09/03 14:36:40 millert Exp $ # string sources .PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/string ${LIBCSRCDIR}/string -SRCS+= bm.c memccpy.c strcasecmp.c strcasestr.c strcoll.c strdup.c \ +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 \ wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \ @@ -146,6 +146,7 @@ MAN+= bm.3 bcmp.3 bcopy.3 bstring.3 bzero.3 ffs.3 memccpy.3 memchr.3 \ wcstok.3 wmemchr.3 MLINKS+=bm.3 bm_comp.3 bm.3 bm_exec.3 bm.3 bm_free.3 +MLINKS+=memchr.3 memrchr.3 MLINKS+=strchr.3 index.3 MLINKS+=strrchr.3 rindex.3 MLINKS+=strcasecmp.3 strncasecmp.3 diff --git a/src/lib/libc/string/memchr.3 b/src/lib/libc/string/memchr.3 index bab26296..7ce07f8e 100644 --- a/src/lib/libc/string/memchr.3 +++ b/src/lib/libc/string/memchr.3 @@ -29,9 +29,9 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $OpenBSD: memchr.3,v 1.7 2007/05/31 19:19:32 jmc Exp $ +.\" $OpenBSD: memchr.3,v 1.8 2007/09/03 14:36:40 millert Exp $ .\" -.Dd $Mdocdate: May 31 2007 $ +.Dd $Mdocdate: September 3 2007 $ .Dt MEMCHR 3 .Os .Sh NAME @@ -41,6 +41,8 @@ .Fd #include .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 @@ -50,10 +52,21 @@ function locates the first occurrence of .Li unsigned char ) in string .Fa b . +.Pp +The +.Fn memrchr +function behaves like +.Fn memchr , +except that it locates the last occurrence of +.Fa c +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 @@ -72,3 +85,9 @@ The .Fn memchr function conforms to .St -ansiC . +.Pp +The +.Fn memrchr +function is an +.Ox +extension. diff --git a/src/lib/libc/string/memrchr.c b/src/lib/libc/string/memrchr.c new file mode 100644 index 00000000..95d3c3ee --- /dev/null +++ b/src/lib/libc/string/memrchr.c @@ -0,0 +1,38 @@ +/* $OpenBSD: memrchr.c,v 1.1 2007/09/03 14:36:40 millert Exp $ */ + +/* + * Copyright (c) 2007 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 + +/* + * Reverse memchr() + * Find the last occurence of 'c' in the buffer 's' of size 'n'. + */ +void * +memrchr(const void *s, int c, size_t n) +{ + const unsigned char *cp; + + if (n != 0) { + cp = (unsigned char *)s + n; + do { + if (*(--cp) == (unsigned char)c) + return((void *)cp); + } while (--n != 0); + } + return(NULL); +}