strstr.c 299 B

123456789101112131415161718192021
  1. #include <lib9.h>
  2. /*
  3. * Return pointer to first occurrence of s2 in s1,
  4. * 0 if none
  5. */
  6. char*
  7. strstr(char *s1, char *s2)
  8. {
  9. char *p;
  10. int f, n;
  11. f = s2[0];
  12. if(f == 0)
  13. return s1;
  14. n = strlen(s2);
  15. for(p=strchr(s1, f); p; p=strchr(p+1, f))
  16. if(strncmp(p, s2, n) == 0)
  17. return p;
  18. return 0;
  19. }