Skip to content

Commit 6b45ba4

Browse files
committed
string: Add strchrnul()
This functions works like strchr() but returns the end of the string if the character is not found. Add an implementation of this. Signed-off-by: Simon Glass <sjg@chromium.org>
1 parent a4b8e37 commit 6b45ba4

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

include/linux/string.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ extern int strncasecmp(const char *s1, const char *s2, __kernel_size_t len);
5050
#ifndef __HAVE_ARCH_STRCHR
5151
extern char * strchr(const char *,int);
5252
#endif
53+
54+
/**
55+
* strchrnul() - return position of a character in the string, or end of string
56+
*
57+
* The strchrnul() function is like strchr() except that if c is not found
58+
* in s, then it returns a pointer to the nul byte at the end of s, rather than
59+
* NULL
60+
* @s: string to search
61+
* @c: character to search for
62+
* @return position of @c in @s, or end of @s if not found
63+
*/
64+
const char *strchrnul(const char *s, int c);
65+
5366
#ifndef __HAVE_ARCH_STRRCHR
5467
extern char * strrchr(const char *,int);
5568
#endif

lib/string.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,14 @@ char * strchr(const char * s, int c)
230230
}
231231
#endif
232232

233+
const char *strchrnul(const char *s, int c)
234+
{
235+
for (; *s != (char)c; ++s)
236+
if (*s == '\0')
237+
break;
238+
return s;
239+
}
240+
233241
#ifndef __HAVE_ARCH_STRRCHR
234242
/**
235243
* strrchr - Find the last occurrence of a character in a string

0 commit comments

Comments
 (0)