summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormillert <>2007-09-03 14:36:40 +0000
committermillert <>2007-09-03 14:36:40 +0000
commit6fdd2cf23672d7bc626fee11d4ca588e6882a436 (patch)
tree6e615c7bca555d0215313900997eb8f344809aca
parent4dce4206f0cafd0811f23aedc04a436aa9d145c1 (diff)
downloadopenbsd-6fdd2cf23672d7bc626fee11d4ca588e6882a436.tar.gz
openbsd-6fdd2cf23672d7bc626fee11d4ca588e6882a436.tar.bz2
openbsd-6fdd2cf23672d7bc626fee11d4ca588e6882a436.zip
add memrchr(3)
-rw-r--r--src/lib/libc/string/Makefile.inc5
-rw-r--r--src/lib/libc/string/memchr.325
-rw-r--r--src/lib/libc/string/memrchr.c38
3 files changed, 63 insertions, 5 deletions
diff --git a/src/lib/libc/string/Makefile.inc b/src/lib/libc/string/Makefile.inc
index 72ec34e694..f003be03c2 100644
--- a/src/lib/libc/string/Makefile.inc
+++ b/src/lib/libc/string/Makefile.inc
@@ -1,9 +1,9 @@
1# $OpenBSD: Makefile.inc,v 1.18 2005/10/29 10:05:11 espie Exp $ 1# $OpenBSD: Makefile.inc,v 1.19 2007/09/03 14:36:40 millert Exp $
2 2
3# string sources 3# string sources
4.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/string ${LIBCSRCDIR}/string 4.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/string ${LIBCSRCDIR}/string
5 5
6SRCS+= bm.c memccpy.c strcasecmp.c strcasestr.c strcoll.c strdup.c \ 6SRCS+= bm.c memccpy.c memrchr.c strcasecmp.c strcasestr.c strcoll.c strdup.c \
7 strerror.c strerror_r.c strlcat.c strmode.c strsignal.c strtok.c \ 7 strerror.c strerror_r.c strlcat.c strmode.c strsignal.c strtok.c \
8 strxfrm.c \ 8 strxfrm.c \
9 wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \ 9 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 \
146 wcstok.3 wmemchr.3 146 wcstok.3 wmemchr.3
147 147
148MLINKS+=bm.3 bm_comp.3 bm.3 bm_exec.3 bm.3 bm_free.3 148MLINKS+=bm.3 bm_comp.3 bm.3 bm_exec.3 bm.3 bm_free.3
149MLINKS+=memchr.3 memrchr.3
149MLINKS+=strchr.3 index.3 150MLINKS+=strchr.3 index.3
150MLINKS+=strrchr.3 rindex.3 151MLINKS+=strrchr.3 rindex.3
151MLINKS+=strcasecmp.3 strncasecmp.3 152MLINKS+=strcasecmp.3 strncasecmp.3
diff --git a/src/lib/libc/string/memchr.3 b/src/lib/libc/string/memchr.3
index bab262964e..7ce07f8ea2 100644
--- a/src/lib/libc/string/memchr.3
+++ b/src/lib/libc/string/memchr.3
@@ -29,9 +29,9 @@
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE. 30.\" SUCH DAMAGE.
31.\" 31.\"
32.\" $OpenBSD: memchr.3,v 1.7 2007/05/31 19:19:32 jmc Exp $ 32.\" $OpenBSD: memchr.3,v 1.8 2007/09/03 14:36:40 millert Exp $
33.\" 33.\"
34.Dd $Mdocdate: May 31 2007 $ 34.Dd $Mdocdate: September 3 2007 $
35.Dt MEMCHR 3 35.Dt MEMCHR 3
36.Os 36.Os
37.Sh NAME 37.Sh NAME
@@ -41,6 +41,8 @@
41.Fd #include <string.h> 41.Fd #include <string.h>
42.Ft void * 42.Ft void *
43.Fn memchr "const void *b" "int c" "size_t len" 43.Fn memchr "const void *b" "int c" "size_t len"
44.Ft void *
45.Fn memrchr "const void *b" "int c" "size_t len"
44.Sh DESCRIPTION 46.Sh DESCRIPTION
45The 47The
46.Fn memchr 48.Fn memchr
@@ -50,10 +52,21 @@ function locates the first occurrence of
50.Li unsigned char ) 52.Li unsigned char )
51in string 53in string
52.Fa b . 54.Fa b .
55.Pp
56The
57.Fn memrchr
58function behaves like
59.Fn memchr ,
60except that it locates the last occurrence of
61.Fa c
62in string
63.Fa b .
53.Sh RETURN VALUES 64.Sh RETURN VALUES
54The 65The
55.Fn memchr 66.Fn memchr
56function returns a pointer to the byte located, or 67and
68.Fn memrchr
69functions return a pointer to the byte located, or
57.Dv NULL 70.Dv NULL
58if no such byte exists within 71if no such byte exists within
59.Fa len 72.Fa len
@@ -72,3 +85,9 @@ The
72.Fn memchr 85.Fn memchr
73function conforms to 86function conforms to
74.St -ansiC . 87.St -ansiC .
88.Pp
89The
90.Fn memrchr
91function is an
92.Ox
93extension.
diff --git a/src/lib/libc/string/memrchr.c b/src/lib/libc/string/memrchr.c
new file mode 100644
index 0000000000..95d3c3ee9b
--- /dev/null
+++ b/src/lib/libc/string/memrchr.c
@@ -0,0 +1,38 @@
1/* $OpenBSD: memrchr.c,v 1.1 2007/09/03 14:36:40 millert Exp $ */
2
3/*
4 * Copyright (c) 2007 Todd C. Miller <Todd.Miller@courtesan.com>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <string.h>
20
21/*
22 * Reverse memchr()
23 * Find the last occurence of 'c' in the buffer 's' of size 'n'.
24 */
25void *
26memrchr(const void *s, int c, size_t n)
27{
28 const unsigned char *cp;
29
30 if (n != 0) {
31 cp = (unsigned char *)s + n;
32 do {
33 if (*(--cp) == (unsigned char)c)
34 return((void *)cp);
35 } while (--n != 0);
36 }
37 return(NULL);
38}