strchr.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. SYNOPSIS
  3. #include <string.h>
  4. char *strchr(char const *s, int c);
  5. char *strrchr(char const *s, int c);
  6. DESCRIPTION
  7. The strchr() function returns a pointer to the first occurrence of the
  8. character c in the string s.
  9. The strrchr() function returns a pointer to the last occurrence of the
  10. character c in the string s.
  11. Here "character" means "byte" - these functions do not work with wide
  12. or multi-byte characters.
  13. RETURN VALUE
  14. The strchr() and strrchr() functions return a pointer to the matched
  15. character or NULL if the character is not found.
  16. CONFORMING TO
  17. SVID 3, POSIX, BSD 4.3, ISO 9899
  18. */
  19. static char *
  20. strchr(char const *s, int c);
  21. static char *
  22. strrchr(char const *s, int c);
  23. static char *
  24. strchr(char const *s, int c)
  25. {
  26. do {
  27. if ((unsigned char)*s == (unsigned char)c)
  28. return s;
  29. } while (*(++s) != NUL);
  30. return NULL;
  31. }
  32. static char *
  33. strrchr(char const *s, int c)
  34. {
  35. char const *e = s + strlen(s);
  36. for (;;) {
  37. if (--e < s)
  38. break;
  39. if ((unsigned char)*e == (unsigned char)c)
  40. return e;
  41. }
  42. return NULL;
  43. }
  44. /*
  45. * Local Variables:
  46. * mode: C
  47. * c-file-style: "stroustrup"
  48. * indent-tabs-mode: nil
  49. * End:
  50. * end of compat/strsignal.c */