diff options
-rw-r--r-- | include/string.h | 3 | ||||
-rw-r--r-- | lib/libc/string/Makefile.inc | 5 | ||||
-rw-r--r-- | lib/libc/string/memchr.3 | 25 | ||||
-rw-r--r-- | lib/libc/string/memrchr.c | 38 |
4 files changed, 65 insertions, 6 deletions
diff --git a/include/string.h b/include/string.h index 0c42e57a14c..52f3f718f7c 100644 --- a/include/string.h +++ b/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/lib/libc/string/Makefile.inc b/lib/libc/string/Makefile.inc index 72ec34e6940..f003be03c23 100644 --- a/lib/libc/string/Makefile.inc +++ b/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/lib/libc/string/memchr.3 b/lib/libc/string/memchr.3 index bab262964ef..7ce07f8ea2e 100644 --- a/lib/libc/string/memchr.3 +++ b/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 <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 @@ -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/lib/libc/string/memrchr.c b/lib/libc/string/memrchr.c new file mode 100644 index 00000000000..95d3c3ee9be --- /dev/null +++ b/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 <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 <string.h> + +/* + * 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); +} |