strstr.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk>
  2. * This file is part of the Linux-8086 C library and is distributed
  3. * under the GNU Library General Public License.
  4. */
  5. #include <string.h>
  6. #if 1
  7. /* We've now got a nice fast strchr and memcmp use them */
  8. char *
  9. strstr(s1, s2)
  10. char *s1; char *s2;
  11. {
  12. register int l = strlen(s2);
  13. register char * p = s1;
  14. if( l==0 ) return p;
  15. while (p = strchr(p, *s2))
  16. {
  17. if( memcmp(p, s2, l) == 0 )
  18. return p;
  19. p++;
  20. }
  21. return (char *) 0;
  22. }
  23. #else
  24. /* This is a nice simple self contained strstr,
  25. now go and work out why the GNU one is faster :-) */
  26. char *strstr(str1, str2)
  27. char *str1, *str2;
  28. {
  29. register char *Sptr, *Tptr;
  30. int len = strlen(str1) -strlen(str2) + 1;
  31. if (*str2)
  32. for (; len > 0; len--, str1++){
  33. if (*str1 != *str2)
  34. continue;
  35. for (Sptr = str1, Tptr = str2; *Tptr != '\0'; Sptr++, Tptr++)
  36. if (*Sptr != *Tptr)
  37. break;
  38. if (*Tptr == '\0')
  39. return (char*) str1;
  40. }
  41. return (char*)0;
  42. }
  43. #endif