welibc
A clear, secure, and well documented standard C library
strstr.c
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright (c) 2021 Walt Elam
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are met:
7 
8  1. Redistributions of source code must retain the above copyright notice,
9  this list of conditions and the following disclaimer.
10 
11  2. Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in the
13  documentation and/or other materials provided with the distribution.
14 
15  3. Neither the name of the copyright holder nor the names of its
16  contributors may be used to endorse or promote products derived from
17  this software without specific prior written permission.
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  POSSIBILITY OF SUCH DAMAGE.
30 *******************************************************************************/
31 /***************************************************************************//**
32  @file strstr.c
33 
34  @brief Defines the strstr function
35 *******************************************************************************/
36 #include <string.h>
37 
38 /*******************************************************************************
39  strstr
40 *//**
41  @brief Locate the first occurrence of a string in a another string
42  @param *s1 String being searched
43  @param *s2 String being found
44 
45  The strstr function locates the first occurrence in the string pointed to by
46  s1 of the sequence of characters (excluding the terminating null character)
47  in the string pointed to by s2.
48 
49  @return A pointer to the located string
50  @retval NULL The sequence in s2 was not found in s1
51 *******************************************************************************/
52 char *
53 strstr(const char *s1, const char *s2)
54 {
55  size_t s2_len = 0;
56 
57  if (!s1 || !s2)
58  {
59  return NULL;
60  }
61 
62  s2_len = strlen(s2);
63  if (0 == s2_len)
64  {
65  return (char *) s1;
66  }
67 
68  /* A longer string will never be found */
69  if (s2_len > strlen(s1))
70  {
71  return NULL;
72  }
73 
74  while (*s1)
75  {
76  /* Jump to the next match on the first character */
77  s1 = strchr(s1, *s2);
78  if (!s1)
79  {
80  break;
81  }
82 
83  if (0 == strncmp(s1, s2, s2_len))
84  {
85  break;
86  }
87 
88  s1++;
89  }
90 
91  return (char *) s1;
92 }
93 
strlen
size_t strlen(const char *s)
Computes the length of a string.
Definition: strlen.c:51
NULL
#define NULL
Definition: stddef.h:57
string.h
Declares types, functions, and macros for manipulating arrays of character type and other objects tre...
strstr
char * strstr(const char *s1, const char *s2)
Locate the first occurrence of a string in a another string.
Definition: strstr.c:53
strchr
char * strchr(const char *s, int c)
Locate the first occurrence of a character in a string.
Definition: strchr.c:53
strncmp
int strncmp(const char *s1, const char *s2, size_t n)
Compares the strings pointed to be s1 and s2.
Definition: strncmp.c:56