strchr.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. char*
  20. strchr( char const *s, int c)
  21. {
  22. do {
  23. if ((unsigned)*s == (unsigned)c)
  24. return s;
  25. } while (*(++s) != NUL);
  26. return NULL;
  27. }
  28. char*
  29. strrchr( char const *s, int c)
  30. {
  31. char const *e = s + strlen(s);
  32. for (;;) {
  33. if (--e < s)
  34. break;
  35. if ((unsigned)*e == (unsigned)c)
  36. return e;
  37. }
  38. return NULL;
  39. }
  40. /*
  41. * Local Variables:
  42. * mode: C
  43. * c-file-style: "stroustrup"
  44. * indent-tabs-mode: nil
  45. * End:
  46. * end of compat/strsignal.c */