ref: cc41bb23348ac34075e905a5efc965880e6cfad3
parent: 05484c9578997847673a50b1874e1ce720930c94
author: Roberto E. Vargas Caballero <[email protected]>
date: Thu Feb 23 10:16:38 EST 2017
[libc] Add strstr()
--- a/libc/src/Makefile
+++ b/libc/src/Makefile
@@ -3,7 +3,7 @@
LIBCOBJ = assert.o strcpy.o strcmp.o strlen.o strchr.o \
strrchr.o strcat.o strncmp.o strncpy.o strncat.o strcoll.o \
- strxfrm.o strtok.o \
+ strxfrm.o strtok.o strstr.o \
memset.o memcpy.o memmove.o memcmp.o memchr.o \
isalnum.o isalpha.o isascii.o isblank.o iscntrl.o isdigit.o \
isgraph.o islower.o isprint.o ispunct.o isspace.o isupper.o \
--- /dev/null
+++ b/libc/src/strstr.c
@@ -1,0 +1,27 @@
+/* See LICENSE file for copyright and license details. */
+
+#include <string.h>
+
+char *
+strstr(const char *s1, const char *s2)
+{
+ const char *p, *q;
+ int c;
+
+ c = *s2++;
+ if (c == '\0')
+ return (char *) s1;
+
+ while (*s1) {
+ if (*s1 != c) {
+ ++s1;
+ } else {
+ p = s1++;
+ for (q = s2; *q && *s1 == *q; ++s1, ++q)
+ /* nothing */;
+ if (*q == '\0')
+ return (char *) p;
+ }
+ }
+ return NULL;
+}