1 // SPDX-License-Identifier: BSD-3-Clause
2 /*
3  * Copyright (c) 1994-2009  Red Hat, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from this
18  * software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34 FUNCTION
35 	<<strrchr>>---reverse search for character in string
36 
37 INDEX
38 	strrchr
39 
40 ANSI_SYNOPSIS
41 	#include <string.h>
42 	char * strrchr(const char *<[string]>, int <[c]>);
43 
44 TRAD_SYNOPSIS
45 	#include <string.h>
46 	char * strrchr(<[string]>, <[c]>);
47 	char *<[string]>;
48 	int *<[c]>;
49 
50 DESCRIPTION
51 	This function finds the last occurence of <[c]> (converted to
52 	a char) in the string pointed to by <[string]> (including the
53 	terminating null character).
54 
55 RETURNS
56 	Returns a pointer to the located character, or a null pointer
57 	if <[c]> does not occur in <[string]>.
58 
59 PORTABILITY
60 <<strrchr>> is ANSI C.
61 
62 <<strrchr>> requires no supporting OS subroutines.
63 
64 QUICKREF
65 	strrchr ansi pure
66 */
67 #include "_ansi.h"
68 #include <string.h>
69 
70 char *
71 _DEFUN (strrchr, (s, i),
72 	_CONST char *s _AND
73 	int i)
74 {
75   _CONST char *last = NULL;
76 
77   if (i)
78     {
79       while ((s=strchr(s, i)))
80 	{
81 	  last = s;
82 	  s++;
83 	}
84     }
85   else
86     {
87       last = strchr(s, i);
88     }
89 
90   return (char *) last;
91 }
92