strstr.c 841 B

1234567891011121314151617181920212223242526272829303132333435
  1. /* Simple implementation of strstr for systems without it.
  2. This function is in the public domain. */
  3. /*
  4. @deftypefn Supplemental char* strstr (const char *@var{string}, const char *@var{sub})
  5. This function searches for the substring @var{sub} in the string
  6. @var{string}, not including the terminating null characters. A pointer
  7. to the first occurrence of @var{sub} is returned, or @code{NULL} if the
  8. substring is absent. If @var{sub} points to a string with zero
  9. length, the function returns @var{string}.
  10. @end deftypefn
  11. */
  12. #include <stddef.h>
  13. extern int memcmp (const void *, const void *, size_t);
  14. extern size_t strlen (const char *);
  15. char *
  16. strstr (const char *s1, const char *s2)
  17. {
  18. const size_t len = strlen (s2);
  19. while (*s1)
  20. {
  21. if (!memcmp (s1, s2, len))
  22. return (char *)s1;
  23. ++s1;
  24. }
  25. return (0);
  26. }