Commit | Line | Data |
---|---|---|
586c39b1 DF |
1 | /* |
2 | * Copyright (c) 1983 Regents of the University of California. | |
493b70ca KB |
3 | * All rights reserved. |
4 | * | |
019bea33 | 5 | * %sccs.include.redist.c% |
586c39b1 DF |
6 | */ |
7 | ||
f4f66d2c | 8 | #if defined(LIBC_SCCS) && !defined(lint) |
019bea33 | 9 | .asciz "@(#)rindex.s 5.6 (Berkeley) %G%" |
f4f66d2c | 10 | #endif /* LIBC_SCCS and not lint */ |
a01b1b6c SL |
11 | |
12 | /* | |
13 | * Find the last occurence of c in the string cp. | |
14 | * Return pointer to match or null pointer. | |
15 | * | |
16 | * char * | |
17 | * rindex(cp, c) | |
18 | * char *cp, c; | |
19 | */ | |
dadab5a2 | 20 | #include "DEFS.h" |
a01b1b6c | 21 | |
dadab5a2 | 22 | ENTRY(rindex, 0) |
a01b1b6c SL |
23 | movq 4(ap),r1 # r1 = cp; r2 = c |
24 | tstl r2 # check for special case c == '\0' | |
25 | bneq 2f | |
26 | 1: | |
27 | locc $0,$65535,(r1) # just find end of string | |
28 | beql 1b # still looking | |
29 | movl r1,r0 # found it | |
30 | ret | |
31 | 2: | |
8f899db2 KM |
32 | moval tbl,r3 # r3 = address of table |
33 | bbss $0,(r3),5f # insure not reentering | |
34 | movab (r3)[r2],r5 # table entry for c | |
a01b1b6c SL |
35 | incb (r5) |
36 | clrl r4 # last found | |
37 | 3: | |
8f899db2 | 38 | scanc $65535,(r1),(r3),$1 # look for c or '\0' |
a01b1b6c SL |
39 | beql 3b # keep looking |
40 | tstb (r1) # if have found '\0' | |
41 | beql 4f # we are done | |
42 | movl r1,r4 # save most recently found | |
43 | incl r1 # skip over character | |
44 | jbr 3b # keep looking | |
45 | 4: | |
46 | movl r4,r0 # return last found (if any) | |
47 | clrb (r5) # clean up table | |
8f899db2 | 48 | clrb (r3) |
a01b1b6c SL |
49 | ret |
50 | ||
51 | .data | |
8f899db2 | 52 | tbl: .space 256 |
a01b1b6c | 53 | .text |
8f899db2 KM |
54 | |
55 | /* | |
56 | * Reentrant, but slower version of rindex | |
57 | */ | |
58 | 5: | |
59 | movl r1,r3 | |
60 | clrl r4 # r4 = pointer to last match | |
61 | 6: | |
62 | locc $0,$65535,(r3) # look for '\0' | |
63 | bneq 8f | |
64 | decw r0 # r0 = 65535 | |
65 | 1: | |
66 | locc r2,r0,(r3) # look for c | |
67 | bneq 7f | |
68 | movl r1,r3 # reset pointer and ... | |
69 | jbr 6b # ... try again | |
70 | 7: | |
71 | movl r1,r4 # stash pointer ... | |
72 | addl3 $1,r1,r3 # ... skip over match and ... | |
73 | decl r0 # ... decrement count | |
74 | jbr 6b # ... try again | |
75 | 8: | |
76 | subl3 r3,r1,r0 # length of short block | |
77 | incl r0 # +1 for '\0' | |
78 | 9: | |
79 | locc r2,r0,(r3) # look for c | |
80 | beql 0f | |
81 | movl r1,r4 # stash pointer ... | |
82 | addl3 $1,r1,r3 # ... skip over match ... | |
83 | decl r0 # ... adjust count and ... | |
84 | jbr 9b # ... try again | |
85 | 0: | |
86 | movl r4,r0 # return stashed pointer | |
87 | ret |