1/*- 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8#if defined(LIBC_SCCS) && !defined(lint) 9 .asciz "@(#)strstr.s 8.1 (Berkeley) 06/04/93" 10#endif /* LIBC_SCCS and not lint */ 11 12/* 13 * Find the first occurrence of s2 as a substring in s1. 14 * If s2 is empty, return s1. 15 * 16 * char *strstr(s1, s2) 17 * const char *s1, *s2; 18 */ 19#include "DEFS.h" 20 21ENTRY(strstr, 0) 22 movq 4(ap),r3 /* r3 = s1, r4 = s2 */ 23 movzwl $65535,r2 /* r2 = locc/matchc limit */ 24 locc $0,r2,(r4) /* find '\0' in s2 */ 25 beql 4f 26 subl3 r4,r1,r5 /* r5 = strlen(s2) */ 27 beql 1f /* if r5 == 0, return s1 */ 28 29 /* 30 * s2 is short enough to apply matchc. 31 * If s1 is long, we have to do it in stages. 32 */ 330: locc $0,r2,(r3) /* find '\0' in s1 */ 34 beql 3f 35 36 /* 37 * Both strings are `short'; we can use matchc directly. 38 */ 39 subl3 r3,r1,r1 /* r1 = strlen(s1) */ 40 matchc r5,(r4),r1,(r3) /* find substring */ 41 bneq 2f 42 43 /* 44 * r3 points r5 bytes past match. Return the match. 45 */ 461: subl3 r5,r3,r0 /* return (byte_past_match - strlen(s2)) */ 47 ret 48 49 /* 50 * There is no matching substring. 51 */ 522: clrl r0 /* return NULL */ 53 ret 54 55 /* 56 * s1 is too long (> 65535 bytes) to apply matchc directly, 57 * but s2 is short enough. Apply s2 to s1, then (if not 58 * found yet) advancing s1 by (65536-strlen(s2)) bytes and 59 * loop. 60 */ 613: matchc r5,(r4),r2,(r3) /* search */ 62 beql 1b /* if found, go return it */ 63 decw r2 /* from 0 to 65535 */ 64 incl r3 /* already advanced 65535, now 65536 */ 65 subl2 r5,r3 /* ... minus strlen(s2) */ 66 brb 0b 67 68 /* 69 * s2 is too long (> 65535 bytes) to bother with matchc. 70 */ 714: locc $0,r2,(r1) /* continue working on strlen(s2) */ 72 beql 4b 73 subl3 r1,r4,r5 /* r5 = strlen(s2) */ 74 movb (r4)+,r2 /* r2 = *s2++ */ 75 decl r5 /* fix up length */ 765: movb (r3)+,r0 /* r0 = *s1++ */ 77 beql 2b /* if '\0', return NULL */ 78 cmpb r0,r2 79 bneq 5b /* loop until first char found */ 80 pushr R5|R4|R3|R2 /* save c, s1, s2, n */ 81 pushr R5|R4|R3 /* strncmp(s1, s2, n) */ 82 calls $3,_strncmp 83 popr R2|R3|R4|R5 /* restore */ 84 tstl r0 85 bneq 5b /* loop until strncmp says rest same too */ 86 subl3 $1,r3,r0 /* return previous s1 */ 87 ret 88