1/*
2 * strstr.c --
3 *
4 *	Source code for the "strstr" library routine.
5 *
6 * Copyright (c) 1988-1993 The Regents of the University of California.
7 * Copyright (c) 1994 Sun Microsystems, Inc.
8 *
9 * See the file "license.terms" for information on usage and redistribution of
10 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 *
12 * RCS: @(#) $Id: strstr.c,v 1.7 2007/04/16 13:36:34 dkf Exp $
13 */
14
15#include "tcl.h"
16#ifndef NULL
17#define NULL 0
18#endif
19
20/*
21 *----------------------------------------------------------------------
22 *
23 * strstr --
24 *
25 *	Locate the first instance of a substring in a string.
26 *
27 * Results:
28 *	If string contains substring, the return value is the location of the
29 *	first matching instance of substring in string. If string doesn't
30 *	contain substring, the return value is 0. Matching is done on an exact
31 *	character-for-character basis with no wildcards or special characters.
32 *
33 * Side effects:
34 *	None.
35 *
36 *----------------------------------------------------------------------
37 */
38
39char *
40strstr(
41    register char *string,	/* String to search. */
42    char *substring)		/* Substring to try to find in string. */
43{
44    register char *a, *b;
45
46    /*
47     * First scan quickly through the two strings looking for a
48     * single-character match. When it's found, then compare the rest of the
49     * substring.
50     */
51
52    b = substring;
53    if (*b == 0) {
54	return string;
55    }
56    for ( ; *string != 0; string += 1) {
57	if (*string != *b) {
58	    continue;
59	}
60	a = string;
61	while (1) {
62	    if (*b == 0) {
63		return string;
64	    }
65	    if (*a++ != *b++) {
66		break;
67	    }
68	}
69	b = substring;
70    }
71    return NULL;
72}
73