memmem.c 773 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /*
  2. * Copyright (C) 2002 Manuel Novoa III
  3. * Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
  4. *
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. */
  7. #include "_string.h"
  8. #ifdef __USE_GNU
  9. void *memmem(const void *haystack, size_t haystacklen,
  10. const void *needle, size_t needlelen)
  11. {
  12. register const char *ph;
  13. register const char *pn;
  14. const char *plast;
  15. size_t n;
  16. if (needlelen == 0) {
  17. return (void *) haystack;
  18. }
  19. if (haystacklen >= needlelen) {
  20. ph = (const char *) haystack;
  21. pn = (const char *) needle;
  22. plast = ph + (haystacklen - needlelen);
  23. do {
  24. n = 0;
  25. while (ph[n] == pn[n]) {
  26. if (++n == needlelen) {
  27. return (void *) ph;
  28. }
  29. }
  30. } while (++ph <= plast);
  31. }
  32. return NULL;
  33. }
  34. #endif