xref: /original-bsd/usr.bin/f77/libF77/rindex_.c (revision 8d528b7a)
1*8d528b7aSbostic /*-
2*8d528b7aSbostic  * Copyright (c) 1980 The Regents of the University of California.
3*8d528b7aSbostic  * All rights reserved.
4*8d528b7aSbostic  *
5*8d528b7aSbostic  * %sccs.include.proprietary.c%
6*8d528b7aSbostic  */
7*8d528b7aSbostic 
8*8d528b7aSbostic #ifndef lint
9*8d528b7aSbostic static char sccsid[] = "@(#)rindex_.c	5.3 (Berkeley) 04/12/91";
10*8d528b7aSbostic #endif /* not lint */
11*8d528b7aSbostic 
129af033faSdlw /*
139af033faSdlw  * find last occurrence of substring in string
149af033faSdlw  *
159af033faSdlw  * calling sequence:
169af033faSdlw  *	character*(*) substr, string
179af033faSdlw  *	indx = rindex (string, substr)
189af033faSdlw  * where:
199af033faSdlw  *	indx will be the index of the first character of the last occurence
209af033faSdlw  *	of substr in string, or zero if not found.
219af033faSdlw  */
229af033faSdlw 
rindex_(str,substr,slen,sublen)239af033faSdlw long rindex_(str, substr, slen, sublen)
249af033faSdlw char *str, *substr; long slen, sublen;
259af033faSdlw {
269af033faSdlw 	register char	*p = str + (slen - sublen);
2770f77587Sdlw 	register char	*p1, *p2;
2870f77587Sdlw 	register int	len;
299af033faSdlw 
3070f77587Sdlw 	if (sublen == 0)
3170f77587Sdlw 		return(0L);
3270f77587Sdlw 	while (p >= str) {
3370f77587Sdlw 		p1 = p;
3470f77587Sdlw 		p2 = substr;
3570f77587Sdlw 		len = sublen;
3670f77587Sdlw 		while ( *p1++ == *p2++ && --len > 0) ;
3770f77587Sdlw 		if ( len <= 0 )
389af033faSdlw 			return((long)(++p - str));
399af033faSdlw 		p--;
4070f77587Sdlw 	}
419af033faSdlw 	return(0L);
429af033faSdlw }
43