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