xref: /illumos-gate/usr/src/lib/libc/port/locale/strcasestr.c (revision 8c69cc8fbe729fa7b091e901c4b50508ccc6bb33)
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 2013 Garrett D'Amore <garrett@damore.org>
24  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #include "lint.h"
31 #include <string.h>
32 #include <ctype.h>
33 #include <sys/types.h>
34 #include <locale.h>
35 #include "lctype.h"
36 #include "localeimpl.h"
37 
38 /*
39  * strcasestr() locates the first occurrence in the string s1 of the
40  * sequence of characters (excluding the terminating null character)
41  * in the string s2, ignoring case.  strcasestr() returns a pointer
42  * to the located string, or a null pointer if the string is not found.
43  * If s2 is empty, the function returns s1.
44  */
45 
46 char *
47 strcasestr_l(const char *s1, const char *s2, locale_t loc)
48 {
49 	const int *cm = loc->ctype->lc_trans_lower;
50 	const uchar_t *us1 = (const uchar_t *)s1;
51 	const uchar_t *us2 = (const uchar_t *)s2;
52 	const uchar_t *tptr;
53 	int c;
54 
55 	if (us2 == NULL || *us2 == '\0')
56 		return ((char *)us1);
57 
58 	c = cm[*us2];
59 	while (*us1 != '\0') {
60 		if (c == cm[*us1++]) {
61 			tptr = us1;
62 			while (cm[c = *++us2] == cm[*us1++] && c != '\0')
63 				continue;
64 			if (c == '\0')
65 				return ((char *)tptr - 1);
66 			us1 = tptr;
67 			us2 = (const uchar_t *)s2;
68 			c = cm[*us2];
69 		}
70 	}
71 
72 	return (NULL);
73 }
74 
75 char *
76 strcasestr(const char *s1, const char *s2)
77 {
78 	return (strcasestr_l(s1, s2, uselocale(NULL)));
79 }
80