1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright (c) 1999 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate * All rights reserved.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate * UTF-8 encoded Unicode parsing routines. For efficiency, we convert
31*0Sstevel@tonic-gate * to wide chars only when absolutely needed. The following interfaces
32*0Sstevel@tonic-gate * are exported to libslp:
33*0Sstevel@tonic-gate *
34*0Sstevel@tonic-gate * slp_utf_strchr: same semantics as strchr, but handles UTF-8 strings
35*0Sstevel@tonic-gate * slp_fold_space: folds white space around and in between works;
36*0Sstevel@tonic-gate * handles UTF-8 strings
37*0Sstevel@tonic-gate * slp_strcasecmp: same semantics as strcasecmp, but also folds white
38*0Sstevel@tonic-gate * space and attempts locale-specific
39*0Sstevel@tonic-gate * case-insensitive comparisons.
40*0Sstevel@tonic-gate */
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate #include <stdio.h>
43*0Sstevel@tonic-gate #include <string.h>
44*0Sstevel@tonic-gate #include <widec.h>
45*0Sstevel@tonic-gate #include <stdlib.h>
46*0Sstevel@tonic-gate #include <syslog.h>
47*0Sstevel@tonic-gate #include <slp-internal.h>
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gate /*
50*0Sstevel@tonic-gate * Same semantics as strchr.
51*0Sstevel@tonic-gate * Assumes that we start on a char boundry, and that c is a 7-bit
52*0Sstevel@tonic-gate * ASCII char.
53*0Sstevel@tonic-gate */
slp_utf_strchr(const char * s,char c)54*0Sstevel@tonic-gate char *slp_utf_strchr(const char *s, char c) {
55*0Sstevel@tonic-gate int len;
56*0Sstevel@tonic-gate char *p;
57*0Sstevel@tonic-gate
58*0Sstevel@tonic-gate for (p = (char *)s; *p; p += len) {
59*0Sstevel@tonic-gate len = mblen(p, MB_CUR_MAX);
60*0Sstevel@tonic-gate if (len == 1 && *p == c)
61*0Sstevel@tonic-gate return (p);
62*0Sstevel@tonic-gate }
63*0Sstevel@tonic-gate return (NULL);
64*0Sstevel@tonic-gate }
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gate /*
67*0Sstevel@tonic-gate * folds white space around and in between words.
68*0Sstevel@tonic-gate * " aa bb " becomes "aa bb".
69*0Sstevel@tonic-gate * returns NULL if it couldn't allocate memory. The caller must free
70*0Sstevel@tonic-gate * the result when done.
71*0Sstevel@tonic-gate */
slp_fold_space(const char * s)72*0Sstevel@tonic-gate static char *slp_fold_space(const char *s) {
73*0Sstevel@tonic-gate int len;
74*0Sstevel@tonic-gate char *folded, *f;
75*0Sstevel@tonic-gate
76*0Sstevel@tonic-gate if (!(folded = malloc(strlen(s) + 1))) {
77*0Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "slp_fold_space", "out of memory");
78*0Sstevel@tonic-gate return (NULL);
79*0Sstevel@tonic-gate }
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate f = folded;
82*0Sstevel@tonic-gate for (;;) {
83*0Sstevel@tonic-gate /* step 1: skip white space */
84*0Sstevel@tonic-gate for (; *s; s++) {
85*0Sstevel@tonic-gate len = mblen(s, MB_CUR_MAX);
86*0Sstevel@tonic-gate if (len != 1)
87*0Sstevel@tonic-gate break;
88*0Sstevel@tonic-gate if (!isspace(*s))
89*0Sstevel@tonic-gate break;
90*0Sstevel@tonic-gate }
91*0Sstevel@tonic-gate
92*0Sstevel@tonic-gate if (!*s) {
93*0Sstevel@tonic-gate /* end of string */
94*0Sstevel@tonic-gate *f = 0;
95*0Sstevel@tonic-gate return (folded);
96*0Sstevel@tonic-gate }
97*0Sstevel@tonic-gate /* if we are in between words, keep one space */
98*0Sstevel@tonic-gate if (f != folded)
99*0Sstevel@tonic-gate *f++ = ' ';
100*0Sstevel@tonic-gate
101*0Sstevel@tonic-gate /* step 2: copy into folded until we hit more white space */
102*0Sstevel@tonic-gate while (*s) {
103*0Sstevel@tonic-gate int i;
104*0Sstevel@tonic-gate len = mblen(s, MB_CUR_MAX);
105*0Sstevel@tonic-gate if (len == 1 && isspace(*s))
106*0Sstevel@tonic-gate break;
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate for (i = 0; i < len; i++)
109*0Sstevel@tonic-gate *f++ = *s++;
110*0Sstevel@tonic-gate }
111*0Sstevel@tonic-gate *f = *s;
112*0Sstevel@tonic-gate if (!*s++)
113*0Sstevel@tonic-gate return (folded);
114*0Sstevel@tonic-gate }
115*0Sstevel@tonic-gate }
116*0Sstevel@tonic-gate
117*0Sstevel@tonic-gate /*
118*0Sstevel@tonic-gate * performs like strcasecmp, but also folds white space before comparing,
119*0Sstevel@tonic-gate * and will handle UTF-8 comparisons (including case). Note that the
120*0Sstevel@tonic-gate * application's locale must have been set to a UTF-8 locale for this
121*0Sstevel@tonic-gate * to work properly.
122*0Sstevel@tonic-gate */
slp_strcasecmp(const char * s1,const char * s2)123*0Sstevel@tonic-gate int slp_strcasecmp(const char *s1, const char *s2) {
124*0Sstevel@tonic-gate int diff = -1;
125*0Sstevel@tonic-gate char *p1, *p2;
126*0Sstevel@tonic-gate size_t wcslen1, wcslen2;
127*0Sstevel@tonic-gate wchar_t *wcs1, *wcs2;
128*0Sstevel@tonic-gate
129*0Sstevel@tonic-gate p1 = p2 = NULL; wcs1 = wcs2 = NULL;
130*0Sstevel@tonic-gate
131*0Sstevel@tonic-gate /* optimization: try simple case first */
132*0Sstevel@tonic-gate if (strcasecmp(s1, s2) == 0)
133*0Sstevel@tonic-gate return (0);
134*0Sstevel@tonic-gate
135*0Sstevel@tonic-gate /* fold white space, and try again */
136*0Sstevel@tonic-gate p1 = slp_fold_space(s1);
137*0Sstevel@tonic-gate p2 = slp_fold_space(s2);
138*0Sstevel@tonic-gate if (!p1 || !p2)
139*0Sstevel@tonic-gate goto cleanup;
140*0Sstevel@tonic-gate
141*0Sstevel@tonic-gate if ((diff = strcasecmp(p1, p2)) == 0)
142*0Sstevel@tonic-gate goto cleanup;
143*0Sstevel@tonic-gate
144*0Sstevel@tonic-gate /*
145*0Sstevel@tonic-gate * try converting to wide char -- we must be in a locale which
146*0Sstevel@tonic-gate * supports the UTF8 codeset for this to work.
147*0Sstevel@tonic-gate */
148*0Sstevel@tonic-gate if ((wcslen1 = mbstowcs(NULL, p1, 0)) == (size_t)-1)
149*0Sstevel@tonic-gate goto cleanup;
150*0Sstevel@tonic-gate
151*0Sstevel@tonic-gate if (!(wcs1 = malloc(sizeof (*wcs1) * (wcslen1 + 1)))) {
152*0Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "slp_strcasecmp", "out of memory");
153*0Sstevel@tonic-gate goto cleanup;
154*0Sstevel@tonic-gate }
155*0Sstevel@tonic-gate
156*0Sstevel@tonic-gate if ((wcslen2 = mbstowcs(NULL, p2, 0)) == (size_t)-1)
157*0Sstevel@tonic-gate goto cleanup;
158*0Sstevel@tonic-gate
159*0Sstevel@tonic-gate if (!(wcs2 = malloc(sizeof (*wcs2) * (wcslen2 + 1)))) {
160*0Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "slp_strcasecmp", "out of memory");
161*0Sstevel@tonic-gate goto cleanup;
162*0Sstevel@tonic-gate }
163*0Sstevel@tonic-gate if (mbstowcs(wcs1, p1, wcslen1 + 1) == (size_t)-1)
164*0Sstevel@tonic-gate goto cleanup;
165*0Sstevel@tonic-gate if (mbstowcs(wcs2, p2, wcslen2 + 1) == (size_t)-1)
166*0Sstevel@tonic-gate goto cleanup;
167*0Sstevel@tonic-gate
168*0Sstevel@tonic-gate diff = wscasecmp(wcs1, wcs2);
169*0Sstevel@tonic-gate
170*0Sstevel@tonic-gate cleanup:
171*0Sstevel@tonic-gate if (p1) free(p1);
172*0Sstevel@tonic-gate if (p2) free(p2);
173*0Sstevel@tonic-gate if (wcs1) free(wcs1);
174*0Sstevel@tonic-gate if (wcs2) free(wcs2);
175*0Sstevel@tonic-gate return (diff);
176*0Sstevel@tonic-gate }
177