Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
strstr.c
Go to the documentation of this file.
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
10  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11  *
12  * RCS: @(#) $Id: strstr.c,v 1.1.1.3 2003/03/06 00:09:04 landonf Exp $
13  */
14 
15 /*
16  *----------------------------------------------------------------------
17  *
18  * strstr --
19  *
20  * Locate the first instance of a substring in a string.
21  *
22  * Results:
23  * If string contains substring, the return value is the
24  * location of the first matching instance of substring
25  * in string. If string doesn't contain substring, the
26  * return value is 0. Matching is done on an exact
27  * character-for-character basis with no wildcards or special
28  * characters.
29  *
30  * Side effects:
31  * None.
32  *
33  *----------------------------------------------------------------------
34  */
35 
36 #include <string.h>
37 
38 char* strstr(const char* string, const char* substring)
39 {
40  const char *a, *b;
41 
42  /* First scan quickly through the two strings looking for a
43  * single-character match. When it's found, then compare the
44  * rest of the substring.
45  */
46 
47  b = substring;
48 
49  if(*b == 0)
50  {
51  return (char*)(uintptr_t)string;
52  }
53 
54  for(; *string != 0; string += 1)
55  {
56  if(*string != *b)
57  {
58  continue;
59  }
60 
61  a = string;
62 
63  while(1)
64  {
65  if(*b == 0)
66  {
67  return (char*)(uintptr_t)string;
68  }
69  if(*a++ != *b++)
70  {
71  break;
72  }
73  }
74 
75  b = substring;
76  }
77 
78  return NULL;
79 }
char * strstr(const char *string, const char *substring)
Finds the first occurrence of the substring in the string.
Definition: strstr.c:38
#define NULL
Definition: stddef.h:15