sl@0: /* sl@0: * strstr.c -- sl@0: * sl@0: * Source code for the "strstr" library routine. sl@0: * sl@0: * Copyright (c) 1988-1993 The Regents of the University of California. sl@0: * Copyright (c) 1994 Sun Microsystems, Inc. sl@0: * sl@0: * See the file "license.terms" for information on usage and redistribution sl@0: * of this file, and for a DISCLAIMER OF ALL WARRANTIES. sl@0: * sl@0: * RCS: @(#) $Id: strstr.c,v 1.3.2.1 2005/04/12 18:28:56 kennykb Exp $ sl@0: */ sl@0: sl@0: #include "tcl.h" sl@0: #ifndef NULL sl@0: #define NULL 0 sl@0: #endif sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * strstr -- sl@0: * sl@0: * Locate the first instance of a substring in a string. sl@0: * sl@0: * Results: sl@0: * If string contains substring, the return value is the sl@0: * location of the first matching instance of substring sl@0: * in string. If string doesn't contain substring, the sl@0: * return value is 0. Matching is done on an exact sl@0: * character-for-character basis with no wildcards or special sl@0: * characters. sl@0: * sl@0: * Side effects: sl@0: * None. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: char * sl@0: strstr(string, substring) sl@0: register char *string; /* String to search. */ sl@0: char *substring; /* Substring to try to find in string. */ sl@0: { sl@0: register char *a, *b; sl@0: sl@0: /* First scan quickly through the two strings looking for a sl@0: * single-character match. When it's found, then compare the sl@0: * rest of the substring. sl@0: */ sl@0: sl@0: b = substring; sl@0: if (*b == 0) { sl@0: return string; sl@0: } sl@0: for ( ; *string != 0; string += 1) { sl@0: if (*string != *b) { sl@0: continue; sl@0: } sl@0: a = string; sl@0: while (1) { sl@0: if (*b == 0) { sl@0: return string; sl@0: } sl@0: if (*a++ != *b++) { sl@0: break; sl@0: } sl@0: } sl@0: b = substring; sl@0: } sl@0: return NULL; sl@0: }