summaryrefslogtreecommitdiff
path: root/lib/libc/string
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>2007-09-03 14:36:41 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>2007-09-03 14:36:41 +0000
commitce1239c9976aa8f3748a2143bb12a543a27293e4 (patch)
tree32f9d2cb6e849018fcf5e7310b77337b43e7258a /lib/libc/string
parent11f3d60016d0a4de4014121ff35d6d1b7d6817bf (diff)
add memrchr(3)
Diffstat (limited to 'lib/libc/string')
-rw-r--r--lib/libc/string/Makefile.inc5
-rw-r--r--lib/libc/string/memchr.325
-rw-r--r--lib/libc/string/memrchr.c38
3 files changed, 63 insertions, 5 deletions
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);
+}