1104443Stjr/*- 2104443Stjr * Copyright (c) 2002 Tim J. Robbins 3104443Stjr * All rights reserved. 4104443Stjr * 5227753Stheraven * Copyright (c) 2011 The FreeBSD Foundation 6227753Stheraven * All rights reserved. 7227753Stheraven * Portions of this software were developed by David Chisnall 8227753Stheraven * under sponsorship from the FreeBSD Foundation. 9227753Stheraven * 10104443Stjr * Redistribution and use in source and binary forms, with or without 11104443Stjr * modification, are permitted provided that the following conditions 12104443Stjr * are met: 13104443Stjr * 1. Redistributions of source code must retain the above copyright 14104443Stjr * notice, this list of conditions and the following disclaimer. 15104443Stjr * 2. Redistributions in binary form must reproduce the above copyright 16104443Stjr * notice, this list of conditions and the following disclaimer in the 17104443Stjr * documentation and/or other materials provided with the distribution. 18104443Stjr * 19104443Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20104443Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21104443Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22104443Stjr * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23104443Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24104443Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25104443Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26104443Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27104443Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28104443Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29104443Stjr * SUCH DAMAGE. 30104443Stjr */ 31104443Stjr 32104443Stjr#include <sys/cdefs.h> 33104443Stjr__FBSDID("$FreeBSD: releng/10.3/lib/libc/string/wcscoll.c 227753 2011-11-20 14:45:42Z theraven $"); 34104443Stjr 35104443Stjr#include <errno.h> 36104443Stjr#include <stdlib.h> 37104443Stjr#include <string.h> 38104443Stjr#include <wchar.h> 39104443Stjr#include "collate.h" 40104443Stjr 41104443Stjrstatic char *__mbsdup(const wchar_t *); 42104443Stjr 43104443Stjr/* 44104443Stjr * Placeholder implementation of wcscoll(). Attempts to use the single-byte 45104443Stjr * collation ordering where possible, and falls back on wcscmp() in locales 46104443Stjr * with extended character sets. 47104443Stjr */ 48104443Stjrint 49227753Stheravenwcscoll_l(const wchar_t *ws1, const wchar_t *ws2, locale_t locale) 50104443Stjr{ 51104443Stjr char *mbs1, *mbs2; 52104443Stjr int diff, sverrno; 53227753Stheraven FIX_LOCALE(locale); 54227753Stheraven struct xlocale_collate *table = 55227753Stheraven (struct xlocale_collate*)locale->components[XLC_COLLATE]; 56104443Stjr 57227753Stheraven if (table->__collate_load_error || MB_CUR_MAX > 1) 58104443Stjr /* 59104443Stjr * Locale has no special collating order, could not be 60104443Stjr * loaded, or has an extended character set; do a fast binary 61104443Stjr * comparison. 62104443Stjr */ 63104443Stjr return (wcscmp(ws1, ws2)); 64104443Stjr 65104443Stjr if ((mbs1 = __mbsdup(ws1)) == NULL || (mbs2 = __mbsdup(ws2)) == NULL) { 66104443Stjr /* 67104443Stjr * Out of memory or illegal wide chars; fall back to wcscmp() 68104443Stjr * but leave errno indicating the error. Callers that don't 69104443Stjr * check for error will get a reasonable but often slightly 70104443Stjr * incorrect result. 71104443Stjr */ 72104443Stjr sverrno = errno; 73104443Stjr free(mbs1); 74104443Stjr errno = sverrno; 75104443Stjr return (wcscmp(ws1, ws2)); 76104443Stjr } 77104443Stjr 78227753Stheraven diff = strcoll_l(mbs1, mbs2, locale); 79104443Stjr sverrno = errno; 80104443Stjr free(mbs1); 81104443Stjr free(mbs2); 82104443Stjr errno = sverrno; 83104443Stjr 84104443Stjr return (diff); 85104443Stjr} 86104443Stjr 87227753Stheravenint 88227753Stheravenwcscoll(const wchar_t *ws1, const wchar_t *ws2) 89227753Stheraven{ 90227753Stheraven return wcscoll_l(ws1, ws2, __get_locale()); 91227753Stheraven} 92227753Stheraven 93104443Stjrstatic char * 94104443Stjr__mbsdup(const wchar_t *ws) 95104443Stjr{ 96127998Stjr static const mbstate_t initial; 97127998Stjr mbstate_t st; 98104443Stjr const wchar_t *wcp; 99104443Stjr size_t len; 100104443Stjr char *mbs; 101104443Stjr 102104443Stjr wcp = ws; 103127998Stjr st = initial; 104127998Stjr if ((len = wcsrtombs(NULL, &wcp, 0, &st)) == (size_t)-1) 105104443Stjr return (NULL); 106104443Stjr if ((mbs = malloc(len + 1)) == NULL) 107104443Stjr return (NULL); 108127998Stjr st = initial; 109127998Stjr wcsrtombs(mbs, &ws, len + 1, &st); 110104443Stjr 111104443Stjr return (mbs); 112104443Stjr} 113