- /* Be careful not to look at the entire extent of haystack or needle
- until needed. This is useful because of these two cases:
- - haystack may be very long, and a match of needle found early,
- - needle may be very long, and not even a short initial segment of
- needle may be found in haystack. */
- if (*needle != '\0')
- {
- /* Speed up the following searches of needle by caching its first
- character. */
- unsigned char b = (unsigned char) *needle;
-
- needle++;
- for (;; haystack++)
- {
- if (*haystack == '\0')
- /* No match. */
- return NULL;
- if ((unsigned char) *haystack == b)
- /* The first character matches. */
- {
- const char *rhaystack = haystack + 1;
- const char *rneedle = needle;
-
- for (;; rhaystack++, rneedle++)
- {
- if (*rneedle == '\0')
- /* Found a match. */
- return (char *) haystack;
- if (*rhaystack == '\0')
- /* No match. */
- return NULL;
- if ((unsigned char) *rhaystack != (unsigned char) *rneedle)
- /* Nothing in this round. */
- break;
- }
- }
- }
- }
- else
- return (char *) haystack;
+ /* POSIX says that strstr() interprets the strings as byte sequences, not
+ as character sequences in the current locale. */
+ return strstr (haystack, needle);