|
|
1.1 root 1: /*
2: * Copyright (c) 1988 Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms are permitted
6: * provided that: (1) source distributions retain this entire copyright
7: * notice and comment, and (2) distributions including binaries display
8: * the following acknowledgement: ``This product includes software
9: * developed by the University of California, Berkeley and its contributors''
10: * in the documentation or other materials provided with the distribution
11: * and in all advertising materials mentioning features or use of this
12: * software. Neither the name of the University nor the names of its
13: * contributors may be used to endorse or promote products derived
14: * from this software without specific prior written permission.
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18: */
19:
20: #if defined(LIBC_SCCS) && !defined(lint)
21: .asciz "@(#)strchr.s 5.4 (Berkeley) 6/1/90"
22: #endif /* LIBC_SCCS and not lint */
23:
24: /*
25: * Find the first occurence of c in the string cp.
26: * Return pointer to match or null pointer.
27: *
28: * char *
29: * strchr(cp, c)
30: * char *cp, c;
31: */
32: #include "DEFS.h"
33:
34: .lcomm tbl,256
35:
36: ENTRY(strchr, 0)
37: movzwl $65535,r4 /* handy constant */
38: movq 4(ap),r1 /* r1 = cp; r2 = c */
39: movzbl r2,r2
40: beql Lzero /* special case for c == '\0' */
41:
42: /*
43: * Fancy scanc version. Alas, it is not reentrant.
44: */
45: movab tbl,r3 /* r3 = base of table */
46: bbss $0,(r3),Lreent /* ensure not reentering */
47: movab (r3)[r2],r5
48: incb (r5) /* mark both '\0' and c */
49: 0:
50: scanc r4,(r1),(r3),$1 /* look for c or '\0' */
51: beql 0b /* still looking */
52: movl r1,r0 /* return whatever we found */
53: tstb (r0)
54: bneq 1f # unless it was '\0':
55: clrl r0 # then return NULL
56: 1:
57: clrb (r5) /* clean up table */
58: clrb (r3)
59: ret
60:
61: /*
62: * Special case for \0.
63: */
64: Lzero:
65: locc r2,r4,(r1) /* just find end of string */
66: beql Lzero /* still looking */
67: movl r1,r0 /* found it */
68: ret
69:
70: /*
71: * Slower reentrant version is two two-step searches. The first
72: * phase runs until we know where the string ends; it locates the
73: * first occurrence of c within a 65535-byte block. If we find
74: * the end of the string first, we switch to the second phase,
75: * were we look only up to the known end of string.
76: */
77: Lreent:
78: 0: /* first phase */
79: movl r1,r3
80: locc $0,r4,(r3) /* look for '\0' */
81: bneq 1f
82: locc r2,r4,(r3) /* look for c */
83: beql 0b /* not found: reset pointer and loop */
84: movl r1,r0 /* found: return it */
85: ret
86: 1: /* second phase */
87: subl3 r3,r1,r0 /* length of short block */
88: locc r2,r0,(r3) /* look for c */
89: beql 2f /* not found: return NULL */
90: movl r1,r0
91: 2: ret
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.