strstr.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. /* FIXME: The above description is ANSI compiliant. This routine has not
  13. been validated to comply with it. -fnf */
  14. #include <stddef.h>
  15. extern char *strchr (const char *, int);
  16. extern int strncmp (const void *, const void *, size_t);
  17. extern size_t strlen (const char *);
  18. char *
  19. strstr (const char *s1, const char *s2)
  20. {
  21. const char *p = s1;
  22. const size_t len = strlen (s2);
  23. for (; (p = strchr (p, *s2)) != 0; p++)
  24. {
  25. if (strncmp (p, s2, len) == 0)
  26. return (char *)p;
  27. }
  28. return (0);
  29. }