memmem.c 850 B

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