ref: 3f7e54a705c0f7a6e6807285e00167f2af5e3d88
parent: c30ed6428967a83580b73c7c51efb47b24d7c876
author: Roberto E. Vargas Caballero <[email protected]>
date: Sun Sep 16 03:04:24 EDT 2018
[lib/c] Fix strstr()
--- a/lib/c/strstr.c
+++ b/lib/c/strstr.c
@@ -1,3 +1,4 @@
+#include <stddef.h>
#include <string.h>
#undef strstr
@@ -4,23 +5,14 @@
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;
}