xref: /netbsd-src/external/bsd/ntp/dist/sntp/libopts/compat/strchr.c (revision cdfa2a7ef92791ba9db70a584a1d904730e6fb46)
1*cdfa2a7eSchristos /*	$NetBSD: strchr.c,v 1.5 2020/05/25 20:47:35 christos Exp $	*/
2abb0f93cSkardel 
3abb0f93cSkardel /*
4abb0f93cSkardel    SYNOPSIS
5abb0f93cSkardel        #include <string.h>
6abb0f93cSkardel 
7abb0f93cSkardel        char *strchr(char const *s, int c);
8abb0f93cSkardel 
9abb0f93cSkardel        char *strrchr(char const *s, int c);
10abb0f93cSkardel 
11abb0f93cSkardel    DESCRIPTION
12abb0f93cSkardel        The  strchr() function returns a pointer to the first occurrence of the
13abb0f93cSkardel        character c in the string s.
14abb0f93cSkardel 
15abb0f93cSkardel        The strrchr() function returns a pointer to the last occurrence of  the
16abb0f93cSkardel        character c in the string s.
17abb0f93cSkardel 
18abb0f93cSkardel        Here  "character"  means "byte" - these functions do not work with wide
19abb0f93cSkardel        or multi-byte characters.
20abb0f93cSkardel 
21abb0f93cSkardel    RETURN VALUE
22abb0f93cSkardel        The strchr() and strrchr() functions return a pointer  to  the  matched
23abb0f93cSkardel        character or NULL if the character is not found.
24abb0f93cSkardel 
25abb0f93cSkardel    CONFORMING TO
26abb0f93cSkardel        SVID 3, POSIX, BSD 4.3, ISO 9899
27abb0f93cSkardel */
28abb0f93cSkardel 
298585484eSchristos static char *
308585484eSchristos strchr(char const *s, int c);
318585484eSchristos 
328585484eSchristos static char *
338585484eSchristos strrchr(char const *s, int c);
348585484eSchristos 
358585484eSchristos static char *
strchr(char const * s,int c)36abb0f93cSkardel strchr(char const *s, int c)
37abb0f93cSkardel {
38abb0f93cSkardel     do {
398585484eSchristos         if ((unsigned char)*s == (unsigned char)c)
40abb0f93cSkardel             return s;
41abb0f93cSkardel 
42abb0f93cSkardel     } while (*(++s) != NUL);
43abb0f93cSkardel 
44abb0f93cSkardel     return NULL;
45abb0f93cSkardel }
46abb0f93cSkardel 
478585484eSchristos static char *
strrchr(char const * s,int c)48abb0f93cSkardel strrchr(char const *s, int c)
49abb0f93cSkardel {
50abb0f93cSkardel     char const *e = s + strlen(s);
51abb0f93cSkardel 
52abb0f93cSkardel     for (;;) {
53abb0f93cSkardel         if (--e < s)
54abb0f93cSkardel             break;
55abb0f93cSkardel 
568585484eSchristos         if ((unsigned char)*e == (unsigned char)c)
57abb0f93cSkardel             return e;
58abb0f93cSkardel     }
59abb0f93cSkardel     return NULL;
60abb0f93cSkardel }
61abb0f93cSkardel 
62abb0f93cSkardel /*
63abb0f93cSkardel  * Local Variables:
64abb0f93cSkardel  * mode: C
65abb0f93cSkardel  * c-file-style: "stroustrup"
66abb0f93cSkardel  * indent-tabs-mode: nil
67abb0f93cSkardel  * End:
68abb0f93cSkardel  * end of compat/strsignal.c */
69