123a1cceaSRoger A. Faulkner /*
223a1cceaSRoger A. Faulkner  * CDDL HEADER START
323a1cceaSRoger A. Faulkner  *
423a1cceaSRoger A. Faulkner  * The contents of this file are subject to the terms of the
523a1cceaSRoger A. Faulkner  * Common Development and Distribution License (the "License").
623a1cceaSRoger A. Faulkner  * You may not use this file except in compliance with the License.
723a1cceaSRoger A. Faulkner  *
823a1cceaSRoger A. Faulkner  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
923a1cceaSRoger A. Faulkner  * or http://www.opensolaris.org/os/licensing.
1023a1cceaSRoger A. Faulkner  * See the License for the specific language governing permissions
1123a1cceaSRoger A. Faulkner  * and limitations under the License.
1223a1cceaSRoger A. Faulkner  *
1323a1cceaSRoger A. Faulkner  * When distributing Covered Code, include this CDDL HEADER in each
1423a1cceaSRoger A. Faulkner  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1523a1cceaSRoger A. Faulkner  * If applicable, add the following below this CDDL HEADER, with the
1623a1cceaSRoger A. Faulkner  * fields enclosed by brackets "[]" replaced with your own identifying
1723a1cceaSRoger A. Faulkner  * information: Portions Copyright [yyyy] [name of copyright owner]
1823a1cceaSRoger A. Faulkner  *
1923a1cceaSRoger A. Faulkner  * CDDL HEADER END
2023a1cceaSRoger A. Faulkner  */
2123a1cceaSRoger A. Faulkner 
2223a1cceaSRoger A. Faulkner /*
23*2d08521bSGarrett D'Amore  * Copyright 2013 Garrett D'Amore <garrett@damore.org>
2423a1cceaSRoger A. Faulkner  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2523a1cceaSRoger A. Faulkner  */
2623a1cceaSRoger A. Faulkner 
2723a1cceaSRoger A. Faulkner /*	Copyright (c) 1988 AT&T	*/
2823a1cceaSRoger A. Faulkner /*	  All Rights Reserved  	*/
2923a1cceaSRoger A. Faulkner 
3023a1cceaSRoger A. Faulkner #include "lint.h"
3123a1cceaSRoger A. Faulkner #include <string.h>
3223a1cceaSRoger A. Faulkner #include <ctype.h>
3323a1cceaSRoger A. Faulkner #include <sys/types.h>
34*2d08521bSGarrett D'Amore #include <locale.h>
35*2d08521bSGarrett D'Amore #include "lctype.h"
36*2d08521bSGarrett D'Amore #include "localeimpl.h"
3723a1cceaSRoger A. Faulkner 
3823a1cceaSRoger A. Faulkner /*
3923a1cceaSRoger A. Faulkner  * strcasestr() locates the first occurrence in the string s1 of the
4023a1cceaSRoger A. Faulkner  * sequence of characters (excluding the terminating null character)
4123a1cceaSRoger A. Faulkner  * in the string s2, ignoring case.  strcasestr() returns a pointer
4223a1cceaSRoger A. Faulkner  * to the located string, or a null pointer if the string is not found.
4323a1cceaSRoger A. Faulkner  * If s2 is empty, the function returns s1.
4423a1cceaSRoger A. Faulkner  */
4523a1cceaSRoger A. Faulkner 
4623a1cceaSRoger A. Faulkner char *
strcasestr_l(const char * s1,const char * s2,locale_t loc)47*2d08521bSGarrett D'Amore strcasestr_l(const char *s1, const char *s2, locale_t loc)
4823a1cceaSRoger A. Faulkner {
49*2d08521bSGarrett D'Amore 	const int *cm = loc->ctype->lc_trans_lower;
5023a1cceaSRoger A. Faulkner 	const uchar_t *us1 = (const uchar_t *)s1;
5123a1cceaSRoger A. Faulkner 	const uchar_t *us2 = (const uchar_t *)s2;
5223a1cceaSRoger A. Faulkner 	const uchar_t *tptr;
5323a1cceaSRoger A. Faulkner 	int c;
5423a1cceaSRoger A. Faulkner 
5523a1cceaSRoger A. Faulkner 	if (us2 == NULL || *us2 == '\0')
5623a1cceaSRoger A. Faulkner 		return ((char *)us1);
5723a1cceaSRoger A. Faulkner 
5823a1cceaSRoger A. Faulkner 	c = cm[*us2];
5923a1cceaSRoger A. Faulkner 	while (*us1 != '\0') {
6023a1cceaSRoger A. Faulkner 		if (c == cm[*us1++]) {
6123a1cceaSRoger A. Faulkner 			tptr = us1;
6223a1cceaSRoger A. Faulkner 			while (cm[c = *++us2] == cm[*us1++] && c != '\0')
6323a1cceaSRoger A. Faulkner 				continue;
6423a1cceaSRoger A. Faulkner 			if (c == '\0')
6523a1cceaSRoger A. Faulkner 				return ((char *)tptr - 1);
6623a1cceaSRoger A. Faulkner 			us1 = tptr;
6723a1cceaSRoger A. Faulkner 			us2 = (const uchar_t *)s2;
6823a1cceaSRoger A. Faulkner 			c = cm[*us2];
6923a1cceaSRoger A. Faulkner 		}
7023a1cceaSRoger A. Faulkner 	}
7123a1cceaSRoger A. Faulkner 
7223a1cceaSRoger A. Faulkner 	return (NULL);
7323a1cceaSRoger A. Faulkner }
74*2d08521bSGarrett D'Amore 
75*2d08521bSGarrett D'Amore char *
strcasestr(const char * s1,const char * s2)76*2d08521bSGarrett D'Amore strcasestr(const char *s1, const char *s2)
77*2d08521bSGarrett D'Amore {
78*2d08521bSGarrett D'Amore 	return (strcasestr_l(s1, s2, uselocale(NULL)));
79*2d08521bSGarrett D'Amore }
80