Browse Source

add memrchr(3)

OPENBSD_4_3
millert 17 years ago
parent
commit
f5ac8d80cf
4 changed files with 65 additions and 6 deletions
  1. +2
    -1
      src/include/string.h
  2. +3
    -2
      src/lib/libc/string/Makefile.inc
  3. +22
    -3
      src/lib/libc/string/memchr.3
  4. +38
    -0
      src/lib/libc/string/memrchr.c

+ 2
- 1
src/include/string.h View File

@ -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)))


+ 3
- 2
src/lib/libc/string/Makefile.inc View File

@ -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


+ 22
- 3
src/lib/libc/string/memchr.3 View File

@ -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.

+ 38
- 0
src/lib/libc/string/memrchr.c View File

@ -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);
}

Loading…
Cancel
Save