strstr.c 811 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <string.h>
  2. /* FIXME: LICENSE */
  3. __attribute__((used))
  4. char *strstr(const char *haystack, const char *needle)
  5. {
  6. const char *start_s1 = (void *)0;
  7. const char *in_s2 = (void *)0;
  8. for (; *haystack != '\0'; haystack++) {
  9. if (!start_s1) {
  10. /* first char of match */
  11. if (*haystack == *needle) {
  12. /* remember start of matching substring in haystack */
  13. start_s1 = haystack;
  14. in_s2 = needle + 1;
  15. /* done already? */
  16. if (*in_s2 == '\0')
  17. return (char*)start_s1;
  18. }
  19. /* continued mis-match
  20. else
  21. nothing ; */
  22. }
  23. else {
  24. /* continued match */
  25. if (*haystack == *in_s2) {
  26. in_s2++;
  27. /* done */
  28. if (*in_s2 == '\0')
  29. return (char*)start_s1;
  30. }
  31. else
  32. /* first char of mis-match */
  33. start_s1 = (void *)0;
  34. }
  35. }
  36. return (void *)0;
  37. }