Add memrchr(3).

Obtained from:	OpenBSD
This commit is contained in:
Xin LI 2008-04-10 00:12:44 +00:00
parent 0d06f230fd
commit 82e45205c8
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=178051
6 changed files with 76 additions and 5 deletions

View File

@ -59,6 +59,7 @@ __BEGIN_DECLS
void *memccpy(void * __restrict, const void * __restrict, int, size_t);
#endif
void *memchr(const void *, int, size_t) __pure;
void *memrchr(const void *, int, size_t) __pure;
int memcmp(const void *, const void *, size_t) __pure;
void *memcpy(void * __restrict, const void * __restrict, size_t);
#if __BSD_VISIBLE

View File

@ -7,7 +7,7 @@ CFLAGS+= -I${.CURDIR}/locale
# machine-independent string sources
MISRCS+=bcmp.c bcopy.c bzero.c ffs.c ffsl.c fls.c flsl.c index.c memccpy.c \
memchr.c memcmp.c \
memchr.c memrchr.c memcmp.c \
memcpy.c memmem.c memmove.c memset.c rindex.c stpcpy.c strcasecmp.c \
strcat.c strchr.c strcmp.c strcoll.c strcpy.c strcspn.c strdup.c \
strerror.c strlcat.c strlcpy.c strlen.c strmode.c strncat.c strncmp.c \
@ -39,6 +39,7 @@ MLINKS+=ffs.3 ffsl.3
MLINKS+=ffs.3 fls.3
MLINKS+=ffs.3 flsl.3
MLINKS+=index.3 rindex.3
MLINKS+=memchr.3 memrchr.3
MLINKS+=strcasecmp.3 strncasecmp.3
MLINKS+=strcat.3 strncat.3
MLINKS+=strchr.3 strrchr.3

View File

@ -77,6 +77,10 @@ FBSD_1.0 {
wmemset;
};
FBSD_1.1 {
memrchr;
};
FBSDprivate_1.0 {
__strtok_r;
};

View File

@ -32,7 +32,7 @@
.\" @(#)memchr.3 8.1 (Berkeley) 6/4/93
.\" $FreeBSD$
.\"
.Dd June 4, 1993
.Dd April 9, 2008
.Dt MEMCHR 3
.Os
.Sh NAME
@ -44,6 +44,8 @@
.In 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
@ -53,11 +55,22 @@ locates the first occurrence of
(converted to an 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,
and
.Fn memrchr
functions
return a pointer to the byte located,
or NULL if no such byte exists within
.Fa len
bytes.
@ -77,3 +90,15 @@ The
function
conforms to
.St -isoC .
.Pp
The
.Fn memrchr
function is a GNU extension and conforms to no standard.
.Sh HISTORY
The
.Fn memrchr
function first appeared in GNU libc 2.1.91, this implementation
first appeared in
.Fx 8.0 ,
coming from
.Ox 4.3 .

40
lib/libc/string/memrchr.c Normal file
View File

@ -0,0 +1,40 @@
/* $OpenBSD: memrchr.c,v 1.2 2007/11/27 16:22:12 martynas 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.
*
* $FreeBSD$
*/
#include <string.h>
/*
* Reverse memchr()
* Find the last occurrence 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);
}

View File

@ -57,7 +57,7 @@
* is created, otherwise 1.
*/
#undef __FreeBSD_version
#define __FreeBSD_version 800033 /* Master, propagated to newvers */
#define __FreeBSD_version 800034 /* Master, propagated to newvers */
#ifndef LOCORE
#include <sys/types.h>