1/* 2 * Copyright (c) 1983 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 */ 6 7#ifdef LIBC_SCCS 8 .asciz "@(#)rindex.s 5.3 (Berkeley) 03/09/86" 9#endif LIBC_SCCS 10 11/* 12 * Find the last occurence of c in the string cp. 13 * Return pointer to match or null pointer. 14 * 15 * char * 16 * rindex(cp, c) 17 * char *cp, c; 18 */ 19#include "DEFS.h" 20 21ENTRY(rindex, 0) 22 movq 4(ap),r1 # r1 = cp; r2 = c 23 tstl r2 # check for special case c == '\0' 24 bneq 2f 251: 26 locc $0,$65535,(r1) # just find end of string 27 beql 1b # still looking 28 movl r1,r0 # found it 29 ret 302: 31 moval tbl,r3 # r3 = address of table 32 bbss $0,(r3),5f # insure not reentering 33 movab (r3)[r2],r5 # table entry for c 34 incb (r5) 35 clrl r4 # last found 363: 37 scanc $65535,(r1),(r3),$1 # look for c or '\0' 38 beql 3b # keep looking 39 tstb (r1) # if have found '\0' 40 beql 4f # we are done 41 movl r1,r4 # save most recently found 42 incl r1 # skip over character 43 jbr 3b # keep looking 444: 45 movl r4,r0 # return last found (if any) 46 clrb (r5) # clean up table 47 clrb (r3) 48 ret 49 50 .data 51tbl: .space 256 52 .text 53 54/* 55 * Reentrant, but slower version of rindex 56 */ 575: 58 movl r1,r3 59 clrl r4 # r4 = pointer to last match 606: 61 locc $0,$65535,(r3) # look for '\0' 62 bneq 8f 63 decw r0 # r0 = 65535 641: 65 locc r2,r0,(r3) # look for c 66 bneq 7f 67 movl r1,r3 # reset pointer and ... 68 jbr 6b # ... try again 697: 70 movl r1,r4 # stash pointer ... 71 addl3 $1,r1,r3 # ... skip over match and ... 72 decl r0 # ... decrement count 73 jbr 6b # ... try again 748: 75 subl3 r3,r1,r0 # length of short block 76 incl r0 # +1 for '\0' 779: 78 locc r2,r0,(r3) # look for c 79 beql 0f 80 movl r1,r4 # stash pointer ... 81 addl3 $1,r1,r3 # ... skip over match ... 82 decl r0 # ... adjust count and ... 83 jbr 9b # ... try again 840: 85 movl r4,r0 # return stashed pointer 86 ret 87