11573Srgrimes/*-
26509Sache * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
36509Sache *		at Electronni Visti IA, Kiev, Ukraine.
46509Sache *			All rights reserved.
51573Srgrimes *
6227753Stheraven * Copyright (c) 2011 The FreeBSD Foundation
7227753Stheraven * All rights reserved.
8227753Stheraven * Portions of this software were developed by David Chisnall
9227753Stheraven * under sponsorship from the FreeBSD Foundation.
10227753Stheraven *
111573Srgrimes * Redistribution and use in source and binary forms, with or without
121573Srgrimes * modification, are permitted provided that the following conditions
131573Srgrimes * are met:
141573Srgrimes * 1. Redistributions of source code must retain the above copyright
151573Srgrimes *    notice, this list of conditions and the following disclaimer.
161573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
171573Srgrimes *    notice, this list of conditions and the following disclaimer in the
181573Srgrimes *    documentation and/or other materials provided with the distribution.
191573Srgrimes *
206509Sache * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
211573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
236509Sache * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
241573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301573Srgrimes * SUCH DAMAGE.
311573Srgrimes */
321573Srgrimes
3386170Sobrien#include <sys/cdefs.h>
3486170Sobrien__FBSDID("$FreeBSD: releng/10.2/lib/libc/string/strcoll.c 228202 2011-12-02 15:41:09Z eadler $");
3577117Sobrien
366509Sache#include <stdlib.h>
371573Srgrimes#include <string.h>
386509Sache#include "collate.h"
391573Srgrimes
40227753Stheraven#include <stdio.h>
41227753Stheraven
421573Srgrimesint
43228202Seadlerstrcoll_l(const char *s, const char *s2, locale_t locale)
441573Srgrimes{
456509Sache	int len, len2, prim, prim2, sec, sec2, ret, ret2;
4651216Sdt	const char *t, *t2;
4751216Sdt	char *tt, *tt2;
48227753Stheraven	FIX_LOCALE(locale);
49227753Stheraven	struct xlocale_collate *table =
50227753Stheraven		(struct xlocale_collate*)locale->components[XLC_COLLATE];
516509Sache
52227753Stheraven	if (table->__collate_load_error)
53228202Seadler		return strcmp(s, s2);
546509Sache
556509Sache	len = len2 = 1;
566509Sache	ret = ret2 = 0;
57227753Stheraven	if (table->__collate_substitute_nontrivial) {
58228202Seadler		t = tt = __collate_substitute(table, s);
59227753Stheraven		t2 = tt2 = __collate_substitute(table, s2);
6051216Sdt	} else {
6151216Sdt		tt = tt2 = NULL;
62228202Seadler		t = s;
6351216Sdt		t2 = s2;
6451216Sdt	}
656509Sache	while(*t && *t2) {
666509Sache		prim = prim2 = 0;
676509Sache		while(*t && !prim) {
68227753Stheraven			__collate_lookup(table, t, &len, &prim, &sec);
696509Sache			t += len;
706509Sache		}
716509Sache		while(*t2 && !prim2) {
72227753Stheraven			__collate_lookup(table, t2, &len2, &prim2, &sec2);
736509Sache			t2 += len2;
746509Sache		}
756509Sache		if(!prim || !prim2)
766509Sache			break;
776509Sache		if(prim != prim2) {
786509Sache			ret = prim - prim2;
796509Sache			goto end;
806509Sache		}
816509Sache		if(!ret2)
826509Sache			ret2 = sec - sec2;
836509Sache	}
846509Sache	if(!*t && *t2)
856509Sache		ret = -(int)((u_char)*t2);
866509Sache	else if(*t && !*t2)
876509Sache		ret = (u_char)*t;
886509Sache	else if(!*t && !*t2)
896509Sache		ret = ret2;
906509Sache  end:
916509Sache	free(tt);
926509Sache	free(tt2);
936509Sache
946509Sache	return ret;
951573Srgrimes}
96227753Stheraven
97227753Stheravenint
98228202Seadlerstrcoll(const char *s, const char *s2)
99227753Stheraven{
100228202Seadler	return strcoll_l(s, s2, __get_locale());
101227753Stheraven}
102227753Stheraven
103