commit 1442c1da6a94c2355ba0b11292086e9e9f698d4d
parent 2bfb5f5576e1723e10aebaaed7476e3608941c1e
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Sun, 16 Sep 2018 07:04:24 +0100
[lib/c] Fix strstr()
Diffstat:
1 file changed, 8 insertions(+), 16 deletions(-)
diff --git a/lib/c/strstr.c b/lib/c/strstr.c
@@ -1,26 +1,18 @@
+#include <stddef.h>
#include <string.h>
#undef strstr
char *
strstr(const char *s1, const char *s2)
{
- const char *p, *q;
- int c0, c;
+ const char *p;
+ int c = *s2;
- c0 = *s2;
- if (c0 == '\0')
- return (char *) s1;
- --s1;
- while ((s1 = strchr(s1 + 1, c0)) != NULL) {
- p = s1;
- q = s2;
- for (;;) {
- if ((c = *++p) == '\0')
- return (char *) s1;
- if (c != *++q)
- break;
- }
+ if (c == '\0')
+ return NULL;
+ for (p = s1; p = strchr(p, c); ++p) {
+ if (!strcmp(p, s2))
+ return (char *) p;
}
-
return NULL;
}