xref: /freebsd/lib/libc/string/strrchr.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
1*8a16b7a1SPedro F. Giffuni /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
446632c18SEd Schouten  * Copyright (c) 1988, 1993
546632c18SEd Schouten  *	The Regents of the University of California.  All rights reserved.
646632c18SEd Schouten  *
746632c18SEd Schouten  * Redistribution and use in source and binary forms, with or without
846632c18SEd Schouten  * modification, are permitted provided that the following conditions
946632c18SEd Schouten  * are met:
1046632c18SEd Schouten  * 1. Redistributions of source code must retain the above copyright
1146632c18SEd Schouten  *    notice, this list of conditions and the following disclaimer.
1246632c18SEd Schouten  * 2. Redistributions in binary form must reproduce the above copyright
1346632c18SEd Schouten  *    notice, this list of conditions and the following disclaimer in the
1446632c18SEd Schouten  *    documentation and/or other materials provided with the distribution.
153fb3b97cSEd Maste  * 3. Neither the name of the University nor the names of its contributors
1646632c18SEd Schouten  *    may be used to endorse or promote products derived from this software
1746632c18SEd Schouten  *    without specific prior written permission.
1846632c18SEd Schouten  *
1946632c18SEd Schouten  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2046632c18SEd Schouten  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2146632c18SEd Schouten  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2246632c18SEd Schouten  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2346632c18SEd Schouten  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2446632c18SEd Schouten  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2546632c18SEd Schouten  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2646632c18SEd Schouten  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2746632c18SEd Schouten  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2846632c18SEd Schouten  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2946632c18SEd Schouten  * SUCH DAMAGE.
3046632c18SEd Schouten  */
3146632c18SEd Schouten 
3246632c18SEd Schouten #include <stddef.h>
3346632c18SEd Schouten #include <string.h>
3446632c18SEd Schouten 
3546632c18SEd Schouten char *
strrchr(const char * p,int ch)3646632c18SEd Schouten strrchr(const char *p, int ch)
3746632c18SEd Schouten {
3846632c18SEd Schouten 	char *save;
3946632c18SEd Schouten 	char c;
4046632c18SEd Schouten 
4146632c18SEd Schouten 	c = ch;
4246632c18SEd Schouten 	for (save = NULL;; ++p) {
4346632c18SEd Schouten 		if (*p == c)
4446632c18SEd Schouten 			save = (char *)p;
4546632c18SEd Schouten 		if (*p == '\0')
4646632c18SEd Schouten 			return (save);
4746632c18SEd Schouten 	}
4846632c18SEd Schouten 	/* NOTREACHED */
4946632c18SEd Schouten }
5046632c18SEd Schouten 
5119c262feSEd Schouten __weak_reference(strrchr, rindex);
52