| 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 |
|
| 13 |
#include "tcl.h" |
| 14 |
#ifndef NULL |
| 15 |
#define NULL 0 |
| 16 |
#endif |
| 17 |
|
| 18 |
/* |
| 19 |
*---------------------------------------------------------------------- |
| 20 |
* |
| 21 |
* strstr -- |
| 22 |
* |
| 23 |
* Locate the first instance of a substring in a string. |
| 24 |
* |
| 25 |
* Results: |
| 26 |
* If string contains substring, the return value is the location of the |
| 27 |
* first matching instance of substring in string. If string doesn't |
| 28 |
* contain substring, the return value is 0. Matching is done on an exact |
| 29 |
* character-for-character basis with no wildcards or special characters. |
| 30 |
* |
| 31 |
* Side effects: |
| 32 |
* None. |
| 33 |
* |
| 34 |
*---------------------------------------------------------------------- |
| 35 |
*/ |
| 36 |
|
| 37 |
char * |
| 38 |
strstr( |
| 39 |
const char *string, /* String to search. */ |
| 40 |
const char *substring) /* Substring to try to find in string. */ |
| 41 |
{ |
| 42 |
const char *a, *b; |
| 43 |
|
| 44 |
/* |
| 45 |
* First scan quickly through the two strings looking for a |
| 46 |
* single-character match. When it's found, then compare the rest of the |
| 47 |
* substring. |
| 48 |
*/ |
| 49 |
|
| 50 |
b = substring; |
| 51 |
if (*b == 0) { |
| 52 |
return (char *)string; |
| 53 |
} |
| 54 |
for ( ; *string != 0; string += 1) { |
| 55 |
if (*string != *b) { |
| 56 |
continue; |
| 57 |
} |
| 58 |
a = string; |
| 59 |
while (1) { |
| 60 |
if (*b == 0) { |
| 61 |
return (char *)string; |
| 62 |
} |
| 63 |
if (*a++ != *b++) { |
| 64 |
break; |
| 65 |
} |
| 66 |
} |
| 67 |
b = substring; |
| 68 |
} |
| 69 |
return NULL; |
| 70 |
} |