install approved copyright notice
[unix-history] / usr / src / lib / libc / vax / string / rindex.s
CommitLineData
586c39b1
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
493b70ca
KB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
f4f66d2c
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
586c39b1
DF
16 */
17
f4f66d2c
KB
18#if defined(LIBC_SCCS) && !defined(lint)
19 .asciz "@(#)rindex.s 5.5 (Berkeley) %G%"
20#endif /* LIBC_SCCS and not lint */
a01b1b6c
SL
21
22/*
23 * Find the last occurence of c in the string cp.
24 * Return pointer to match or null pointer.
25 *
26 * char *
27 * rindex(cp, c)
28 * char *cp, c;
29 */
dadab5a2 30#include "DEFS.h"
a01b1b6c 31
dadab5a2 32ENTRY(rindex, 0)
a01b1b6c
SL
33 movq 4(ap),r1 # r1 = cp; r2 = c
34 tstl r2 # check for special case c == '\0'
35 bneq 2f
361:
37 locc $0,$65535,(r1) # just find end of string
38 beql 1b # still looking
39 movl r1,r0 # found it
40 ret
412:
8f899db2
KM
42 moval tbl,r3 # r3 = address of table
43 bbss $0,(r3),5f # insure not reentering
44 movab (r3)[r2],r5 # table entry for c
a01b1b6c
SL
45 incb (r5)
46 clrl r4 # last found
473:
8f899db2 48 scanc $65535,(r1),(r3),$1 # look for c or '\0'
a01b1b6c
SL
49 beql 3b # keep looking
50 tstb (r1) # if have found '\0'
51 beql 4f # we are done
52 movl r1,r4 # save most recently found
53 incl r1 # skip over character
54 jbr 3b # keep looking
554:
56 movl r4,r0 # return last found (if any)
57 clrb (r5) # clean up table
8f899db2 58 clrb (r3)
a01b1b6c
SL
59 ret
60
61 .data
8f899db2 62tbl: .space 256
a01b1b6c 63 .text
8f899db2
KM
64
65/*
66 * Reentrant, but slower version of rindex
67 */
685:
69 movl r1,r3
70 clrl r4 # r4 = pointer to last match
716:
72 locc $0,$65535,(r3) # look for '\0'
73 bneq 8f
74 decw r0 # r0 = 65535
751:
76 locc r2,r0,(r3) # look for c
77 bneq 7f
78 movl r1,r3 # reset pointer and ...
79 jbr 6b # ... try again
807:
81 movl r1,r4 # stash pointer ...
82 addl3 $1,r1,r3 # ... skip over match and ...
83 decl r0 # ... decrement count
84 jbr 6b # ... try again
858:
86 subl3 r3,r1,r0 # length of short block
87 incl r0 # +1 for '\0'
889:
89 locc r2,r0,(r3) # look for c
90 beql 0f
91 movl r1,r4 # stash pointer ...
92 addl3 $1,r1,r3 # ... skip over match ...
93 decl r0 # ... adjust count and ...
94 jbr 9b # ... try again
950:
96 movl r4,r0 # return stashed pointer
97 ret