xref: /illumos-gate/usr/src/lib/libc/port/locale/strcoll.c (revision 6ea3c060)
1 /*
2  * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
3  *		at Electronni Visti IA, Kiev, Ukraine.
4  *			All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /*
29  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
30  * Use is subject to license terms.
31  */
32 
33 #include "lint.h"
34 #include <stdlib.h>
35 #include <string.h>
36 #include <errno.h>
37 #include "collate.h"
38 
39 int
40 strcoll(const char *s, const char *s2)
41 {
42 	int len, len2, prim, prim2, sec, sec2, ret, ret2;
43 	const char *t, *t2;
44 	char *tt, *tt2;
45 
46 	if (__collate_load_error)
47 		return (strcmp(s, s2));
48 
49 	len = len2 = 1;
50 	ret = ret2 = 0;
51 	if (__collate_substitute_nontrivial) {
52 		t = tt = __collate_substitute(s);
53 		t2 = tt2 = __collate_substitute(s2);
54 		if ((tt == NULL) || (tt2 == NULL)) {
55 			errno = ENOMEM;
56 			if (tt)
57 				free(tt);
58 			if (tt2)
59 				free(tt2);
60 			/*
61 			 * All we can do here is set errno, the application
62 			 * is obliged to check it.
63 			 */
64 			return (strcmp(s, s2));
65 		}
66 	} else {
67 		tt = tt2 = NULL;
68 		t = s;
69 		t2 = s2;
70 	}
71 	while (*t && *t2) {
72 		prim = prim2 = 0;
73 		while (*t && !prim) {
74 			__collate_lookup(t, &len, &prim, &sec);
75 			t += len;
76 		}
77 		while (*t2 && !prim2) {
78 			__collate_lookup(t2, &len2, &prim2, &sec2);
79 			t2 += len2;
80 		}
81 		if (!prim || !prim2)
82 			break;
83 		if (prim != prim2) {
84 			ret = prim - prim2;
85 			goto end;
86 		}
87 		if (!ret2)
88 			ret2 = sec - sec2;
89 	}
90 	if (!*t && *t2)
91 		ret = -(int)((uchar_t)*t2);
92 	else if (*t && !*t2)
93 		ret = (uchar_t)*t;
94 	else if (!*t && !*t2)
95 		ret = ret2;
96 end:
97 	free(tt);
98 	free(tt2);
99 
100 	return (ret);
101 }
102