string.c 996 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Taken from:
  4. * linux/lib/string.c
  5. *
  6. * Copyright (C) 1991, 1992 Linus Torvalds
  7. */
  8. #include <linux/types.h>
  9. #include <linux/string.h>
  10. #ifndef __HAVE_ARCH_STRSTR
  11. /**
  12. * strstr - Find the first substring in a %NUL terminated string
  13. * @s1: The string to be searched
  14. * @s2: The string to search for
  15. */
  16. char *strstr(const char *s1, const char *s2)
  17. {
  18. size_t l1, l2;
  19. l2 = strlen(s2);
  20. if (!l2)
  21. return (char *)s1;
  22. l1 = strlen(s1);
  23. while (l1 >= l2) {
  24. l1--;
  25. if (!memcmp(s1, s2, l2))
  26. return (char *)s1;
  27. s1++;
  28. }
  29. return NULL;
  30. }
  31. #endif
  32. #ifndef __HAVE_ARCH_STRNCMP
  33. /**
  34. * strncmp - Compare two length-limited strings
  35. * @cs: One string
  36. * @ct: Another string
  37. * @count: The maximum number of bytes to compare
  38. */
  39. int strncmp(const char *cs, const char *ct, size_t count)
  40. {
  41. unsigned char c1, c2;
  42. while (count) {
  43. c1 = *cs++;
  44. c2 = *ct++;
  45. if (c1 != c2)
  46. return c1 < c2 ? -1 : 1;
  47. if (!c1)
  48. break;
  49. count--;
  50. }
  51. return 0;
  52. }
  53. #endif