1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24 */
25
26 /* Copyright (c) 1988 AT&T */
27 /* All Rights Reserved */
28
29 #include "lint.h"
30 #include <string.h>
31 #include <ctype.h>
32 #include <sys/types.h>
33
34 /*
35 * strcasestr() locates the first occurrence in the string s1 of the
36 * sequence of characters (excluding the terminating null character)
37 * in the string s2, ignoring case. strcasestr() returns a pointer
38 * to the located string, or a null pointer if the string is not found.
39 * If s2 is empty, the function returns s1.
40 */
41
42 char *
43 strcasestr(const char *s1, const char *s2)
44 {
45 int *cm = __trans_lower;
46 const uchar_t *us1 = (const uchar_t *)s1;
47 const uchar_t *us2 = (const uchar_t *)s2;
48 const uchar_t *tptr;
49 int c;
50
51 if (us2 == NULL || *us2 == '\0')
52 return ((char *)us1);
53
54 c = cm[*us2];
55 while (*us1 != '\0') {
56 if (c == cm[*us1++]) {
57 tptr = us1;
58 while (cm[c = *++us2] == cm[*us1++] && c != '\0')
59 continue;
60 if (c == '\0')
61 return ((char *)tptr - 1);
62 us1 = tptr;
63 us2 = (const uchar_t *)s2;
64 c = cm[*us2];
65 }
66 }
67
68 return (NULL);
69 }