xref: /freebsd/sys/libkern/strrchr.c (revision 51369649b03ece2aed3eb61b0c8214b9aa5b2fa2)
122fec34aSEd Schouten /*-
2*51369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*51369649SPedro F. Giffuni  *
422fec34aSEd Schouten  * Copyright (c) 1988, 1993
522fec34aSEd Schouten  *	The Regents of the University of California.  All rights reserved.
622fec34aSEd Schouten  *
722fec34aSEd Schouten  * Redistribution and use in source and binary forms, with or without
822fec34aSEd Schouten  * modification, are permitted provided that the following conditions
922fec34aSEd Schouten  * are met:
1022fec34aSEd Schouten  * 1. Redistributions of source code must retain the above copyright
1122fec34aSEd Schouten  *    notice, this list of conditions and the following disclaimer.
1222fec34aSEd Schouten  * 2. Redistributions in binary form must reproduce the above copyright
1322fec34aSEd Schouten  *    notice, this list of conditions and the following disclaimer in the
1422fec34aSEd Schouten  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1622fec34aSEd Schouten  *    may be used to endorse or promote products derived from this software
1722fec34aSEd Schouten  *    without specific prior written permission.
1822fec34aSEd Schouten  *
1922fec34aSEd Schouten  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2022fec34aSEd Schouten  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2122fec34aSEd Schouten  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2222fec34aSEd Schouten  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2322fec34aSEd Schouten  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2422fec34aSEd Schouten  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2522fec34aSEd Schouten  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2622fec34aSEd Schouten  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2722fec34aSEd Schouten  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2822fec34aSEd Schouten  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2922fec34aSEd Schouten  * SUCH DAMAGE.
3022fec34aSEd Schouten  */
3122fec34aSEd Schouten 
3222fec34aSEd Schouten #include <sys/cdefs.h>
3322fec34aSEd Schouten __FBSDID("$FreeBSD$");
3422fec34aSEd Schouten 
3522fec34aSEd Schouten #include <sys/param.h>
3622fec34aSEd Schouten #include <sys/libkern.h>
3722fec34aSEd Schouten 
3822fec34aSEd Schouten char *
3922fec34aSEd Schouten strrchr(const char *p, int ch)
4022fec34aSEd Schouten {
4122fec34aSEd Schouten 	union {
4222fec34aSEd Schouten 		const char *cp;
4322fec34aSEd Schouten 		char *p;
4422fec34aSEd Schouten 	} u;
4522fec34aSEd Schouten 	char *save;
4622fec34aSEd Schouten 
4722fec34aSEd Schouten 	u.cp = p;
4822fec34aSEd Schouten 	for (save = NULL;; ++u.p) {
4922fec34aSEd Schouten 		if (*u.p == ch)
5022fec34aSEd Schouten 			save = u.p;
5122fec34aSEd Schouten 		if (*u.p == '\0')
5222fec34aSEd Schouten 			return(save);
5322fec34aSEd Schouten 	}
5422fec34aSEd Schouten 	/* NOTREACHED */
5522fec34aSEd Schouten }
56