mirror of
https://git.FreeBSD.org/src.git
synced 2025-02-08 18:13:09 +00:00
Add strchrnul(), a GNU function similar to strchr(), except that it returns
a pointer to the end of the string, rather than NULL, if the character was not found. Approved by: theraven
This commit is contained in:
parent
626d8465fb
commit
d902844bbd
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=246766
@ -74,6 +74,9 @@ char *strcasestr(const char *, const char *) __pure;
|
|||||||
#endif
|
#endif
|
||||||
char *strcat(char * __restrict, const char * __restrict);
|
char *strcat(char * __restrict, const char * __restrict);
|
||||||
char *strchr(const char *, int) __pure;
|
char *strchr(const char *, int) __pure;
|
||||||
|
#if defined(_GNU_SOURCE)
|
||||||
|
char *strchrnul(const char*, int) __pure;
|
||||||
|
#endif
|
||||||
int strcmp(const char *, const char *) __pure;
|
int strcmp(const char *, const char *) __pure;
|
||||||
int strcoll(const char *, const char *);
|
int strcoll(const char *, const char *);
|
||||||
char *strcpy(char * __restrict, const char * __restrict);
|
char *strcpy(char * __restrict, const char * __restrict);
|
||||||
|
@ -10,9 +10,9 @@ MISRCS+=bcmp.c bcopy.c bzero.c ffs.c ffsl.c ffsll.c fls.c flsl.c flsll.c \
|
|||||||
memccpy.c memchr.c memrchr.c memcmp.c \
|
memccpy.c memchr.c memrchr.c memcmp.c \
|
||||||
memcpy.c memmem.c memmove.c memset.c \
|
memcpy.c memmem.c memmove.c memset.c \
|
||||||
stpcpy.c stpncpy.c strcasecmp.c \
|
stpcpy.c stpncpy.c strcasecmp.c \
|
||||||
strcat.c strcasestr.c strchr.c strcmp.c strcoll.c strcpy.c strcspn.c \
|
strcat.c strcasestr.c strchr.c strchrnul.c strcmp.c strcoll.c strcpy.c\
|
||||||
strdup.c strerror.c strlcat.c strlcpy.c strlen.c strmode.c strncat.c \
|
strcspn.c strdup.c strerror.c strlcat.c strlcpy.c strlen.c strmode.c \
|
||||||
strncmp.c strncpy.c strndup.c strnlen.c strnstr.c \
|
strncat.c strncmp.c strncpy.c strndup.c strnlen.c strnstr.c \
|
||||||
strpbrk.c strrchr.c strsep.c strsignal.c strspn.c strstr.c strtok.c \
|
strpbrk.c strrchr.c strsep.c strsignal.c strspn.c strstr.c strtok.c \
|
||||||
strxfrm.c swab.c wcpcpy.c wcpncpy.c wcscasecmp.c wcscat.c \
|
strxfrm.c swab.c wcpcpy.c wcpncpy.c wcscasecmp.c wcscat.c \
|
||||||
wcschr.c wcscmp.c wcscoll.c wcscpy.c wcscspn.c wcsdup.c \
|
wcschr.c wcscmp.c wcscoll.c wcscpy.c wcscspn.c wcsdup.c \
|
||||||
|
@ -94,6 +94,7 @@ FBSD_1.1 {
|
|||||||
FBSD_1.3 {
|
FBSD_1.3 {
|
||||||
strcasecmp_l;
|
strcasecmp_l;
|
||||||
strcasestr_l;
|
strcasestr_l;
|
||||||
|
strchrnul;
|
||||||
strncasecmp_l;
|
strncasecmp_l;
|
||||||
wcswidth_l;
|
wcswidth_l;
|
||||||
wcwidth_l;
|
wcwidth_l;
|
||||||
|
@ -32,11 +32,11 @@
|
|||||||
.\" @(#)strchr.3 8.2 (Berkeley) 4/19/94
|
.\" @(#)strchr.3 8.2 (Berkeley) 4/19/94
|
||||||
.\" $FreeBSD$
|
.\" $FreeBSD$
|
||||||
.\"
|
.\"
|
||||||
.Dd April 19, 1994
|
.Dd February 13, 2013
|
||||||
.Dt STRCHR 3
|
.Dt STRCHR 3
|
||||||
.Os
|
.Os
|
||||||
.Sh NAME
|
.Sh NAME
|
||||||
.Nm strchr , strrchr
|
.Nm strchr , strrchr , strchrnul
|
||||||
.Nd locate character in string
|
.Nd locate character in string
|
||||||
.Sh LIBRARY
|
.Sh LIBRARY
|
||||||
.Lb libc
|
.Lb libc
|
||||||
@ -46,6 +46,8 @@
|
|||||||
.Fn strchr "const char *s" "int c"
|
.Fn strchr "const char *s" "int c"
|
||||||
.Ft "char *"
|
.Ft "char *"
|
||||||
.Fn strrchr "const char *s" "int c"
|
.Fn strrchr "const char *s" "int c"
|
||||||
|
.Ft "char *"
|
||||||
|
.Fn strchrnul "const char *s" "int c"
|
||||||
.Sh DESCRIPTION
|
.Sh DESCRIPTION
|
||||||
The
|
The
|
||||||
.Fn strchr
|
.Fn strchr
|
||||||
@ -69,6 +71,18 @@ function is identical to
|
|||||||
.Fn strchr
|
.Fn strchr
|
||||||
except it locates the last occurrence of
|
except it locates the last occurrence of
|
||||||
.Fa c .
|
.Fa c .
|
||||||
|
.Pp
|
||||||
|
The
|
||||||
|
.Fn strchrnul
|
||||||
|
function is identical to
|
||||||
|
.Fn strchr
|
||||||
|
except that if
|
||||||
|
.Fa c
|
||||||
|
is not found in
|
||||||
|
.Fa s
|
||||||
|
a pointer to the terminating
|
||||||
|
.Ql \e0
|
||||||
|
is returned.
|
||||||
.Sh RETURN VALUES
|
.Sh RETURN VALUES
|
||||||
The functions
|
The functions
|
||||||
.Fn strchr
|
.Fn strchr
|
||||||
@ -77,6 +91,11 @@ and
|
|||||||
return a pointer to the located character, or
|
return a pointer to the located character, or
|
||||||
.Dv NULL
|
.Dv NULL
|
||||||
if the character does not appear in the string.
|
if the character does not appear in the string.
|
||||||
|
.Pp
|
||||||
|
.Fn strchrnul
|
||||||
|
returns a pointer to the terminating
|
||||||
|
.Ql \e0
|
||||||
|
if the character does not appear in the string.
|
||||||
.Sh SEE ALSO
|
.Sh SEE ALSO
|
||||||
.Xr memchr 3 ,
|
.Xr memchr 3 ,
|
||||||
.Xr memmem 3 ,
|
.Xr memmem 3 ,
|
||||||
@ -94,3 +113,11 @@ and
|
|||||||
.Fn strrchr
|
.Fn strrchr
|
||||||
conform to
|
conform to
|
||||||
.St -isoC .
|
.St -isoC .
|
||||||
|
The
|
||||||
|
.Fn strchrnul
|
||||||
|
is a GNU extension .
|
||||||
|
.Sh History
|
||||||
|
The
|
||||||
|
.Fn strchrnul
|
||||||
|
function first appeared in glibc 2.1.1 and was added in
|
||||||
|
.Fx 10.0 .
|
||||||
|
48
lib/libc/string/strchrnul.c
Normal file
48
lib/libc/string/strchrnul.c
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
/*-
|
||||||
|
* Copyright (c) 2013 Niclas Zeising
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||||
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||||
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sys/cdefs.h>
|
||||||
|
__FBSDID("$FreeBSD$");
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
__weak_reference(__strchrnul, strchrnul);
|
||||||
|
|
||||||
|
char *
|
||||||
|
__strchrnul(const char *p, int ch)
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
|
||||||
|
c = ch;
|
||||||
|
for (;; ++p) {
|
||||||
|
if (*p == c || *p == '\0')
|
||||||
|
return ((char *)p);
|
||||||
|
}
|
||||||
|
/* NOTREACHED */
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user