memmem.c 749 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. void *memmem(const void *haystack, size_t haystacklen,
  9. const void *needle, size_t needlelen)
  10. {
  11. register const char *ph;
  12. register const char *pn;
  13. const char *plast;
  14. size_t n;
  15. if (needlelen == 0) {
  16. return (void *) haystack;
  17. }
  18. if (haystacklen >= needlelen) {
  19. ph = (const char *) haystack;
  20. pn = (const char *) needle;
  21. plast = ph + (haystacklen - needlelen);
  22. do {
  23. n = 0;
  24. while (ph[n] == pn[n]) {
  25. if (++n == needlelen) {
  26. return (void *) ph;
  27. }
  28. }
  29. } while (++ph <= plast);
  30. }
  31. return NULL;
  32. }