diff options
author | Sergio Rodriguez <sergio.sf.rodriguez@intel.com> | 2016-07-19 11:54:43 -0700 |
---|---|---|
committer | Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> | 2016-07-19 22:35:21 +0000 |
commit | 6d5981bf28c9ba35c7a83cb3ec15987b1fde25ff (patch) | |
tree | 0c9803d46a3ab9c8879f9379fd88a92f66781f92 /lib | |
parent | f719aded819009d44fef5091df8e88e218773a5b (diff) |
lib: Adding the strstr() function
The strstr function finds the first occurrence of a substring
in another string, null terminated strings are not compared, this
function is added for compability for porting other libraries (like
mbedtls)
Jira: ZEP-327
Origin: http://www.leidinger.net/freebsd/dox/libkern/html/d3/d29/
strstr_8c_source.html
Change-Id: I52aac218ce0bd86373ec60f5afc49a92c85f6319
Signed-off-by: Sergio Rodriguez <sergio.sf.rodriguez@intel.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libc/minimal/include/string.h | 1 | ||||
-rw-r--r-- | lib/libc/minimal/source/string/Makefile | 2 | ||||
-rw-r--r-- | lib/libc/minimal/source/string/strstr.c | 61 |
3 files changed, 63 insertions, 1 deletions
diff --git a/lib/libc/minimal/include/string.h b/lib/libc/minimal/include/string.h index 2b4f860b2..8e98dd4c2 100644 --- a/lib/libc/minimal/include/string.h +++ b/lib/libc/minimal/include/string.h @@ -35,6 +35,7 @@ extern int strcmp(const char *s1, const char *s2); extern int strncmp(const char *s1, const char *s2, size_t n); extern char *strcat(char *_Restrict dest, const char *_Restrict src); extern char *strncat(char *_Restrict d, const char *_Restrict s, size_t n); +extern char *strstr(const char *s, const char *find); extern int memcmp(const void *m1, const void *m2, size_t n); extern void *memmove(void *d, const void *s, size_t n); diff --git a/lib/libc/minimal/source/string/Makefile b/lib/libc/minimal/source/string/Makefile index c64286d68..e9d047635 100644 --- a/lib/libc/minimal/source/string/Makefile +++ b/lib/libc/minimal/source/string/Makefile @@ -1,2 +1,2 @@ obj-y := string.o -obj-$(CONFIG_MINIMAL_LIBC_EXTENDED) += strncasecmp.o +obj-$(CONFIG_MINIMAL_LIBC_EXTENDED) += strncasecmp.o strstr.o diff --git a/lib/libc/minimal/source/string/strstr.c b/lib/libc/minimal/source/string/strstr.c new file mode 100644 index 000000000..73348934a --- /dev/null +++ b/lib/libc/minimal/source/string/strstr.c @@ -0,0 +1,61 @@ +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Chris Torek. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgment: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <string.h> + +/* + * Find the first occurrence of find in s. + */ +char * +strstr(const char *s, const char *find) +{ + char c, sc; + size_t len; + + c = *find++; + if (c != 0) { + len = strlen(find); + do { + do { + sc = *s++; + if (sc == 0) + return NULL; + } while (sc != c); + } while (strncmp(s, find, len) != 0); + s--; + } + return (char *)s; +} |