1fd4f1dd9STim J. Robbins /*- 2fd4f1dd9STim J. Robbins * Copyright (c) 2002 Tim J. Robbins 3fd4f1dd9STim J. Robbins * All rights reserved. 4fd4f1dd9STim J. Robbins * 5*3c87aa1dSDavid Chisnall * Copyright (c) 2011 The FreeBSD Foundation 6*3c87aa1dSDavid Chisnall * All rights reserved. 7*3c87aa1dSDavid Chisnall * Portions of this software were developed by David Chisnall 8*3c87aa1dSDavid Chisnall * under sponsorship from the FreeBSD Foundation. 9*3c87aa1dSDavid Chisnall * 10fd4f1dd9STim J. Robbins * Redistribution and use in source and binary forms, with or without 11fd4f1dd9STim J. Robbins * modification, are permitted provided that the following conditions 12fd4f1dd9STim J. Robbins * are met: 13fd4f1dd9STim J. Robbins * 1. Redistributions of source code must retain the above copyright 14fd4f1dd9STim J. Robbins * notice, this list of conditions and the following disclaimer. 15fd4f1dd9STim J. Robbins * 2. Redistributions in binary form must reproduce the above copyright 16fd4f1dd9STim J. Robbins * notice, this list of conditions and the following disclaimer in the 17fd4f1dd9STim J. Robbins * documentation and/or other materials provided with the distribution. 18fd4f1dd9STim J. Robbins * 19fd4f1dd9STim J. Robbins * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20fd4f1dd9STim J. Robbins * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21fd4f1dd9STim J. Robbins * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22fd4f1dd9STim J. Robbins * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23fd4f1dd9STim J. Robbins * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24fd4f1dd9STim J. Robbins * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25fd4f1dd9STim J. Robbins * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26fd4f1dd9STim J. Robbins * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27fd4f1dd9STim J. Robbins * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28fd4f1dd9STim J. Robbins * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29fd4f1dd9STim J. Robbins * SUCH DAMAGE. 30fd4f1dd9STim J. Robbins */ 31fd4f1dd9STim J. Robbins 32fd4f1dd9STim J. Robbins #include <sys/cdefs.h> 33fd4f1dd9STim J. Robbins __FBSDID("$FreeBSD$"); 34fd4f1dd9STim J. Robbins 35fd4f1dd9STim J. Robbins #include <errno.h> 36fd4f1dd9STim J. Robbins #include <stdlib.h> 37fd4f1dd9STim J. Robbins #include <string.h> 38fd4f1dd9STim J. Robbins #include <wchar.h> 39fd4f1dd9STim J. Robbins #include "collate.h" 40fd4f1dd9STim J. Robbins 41fd4f1dd9STim J. Robbins static char *__mbsdup(const wchar_t *); 42fd4f1dd9STim J. Robbins 43fd4f1dd9STim J. Robbins /* 44fd4f1dd9STim J. Robbins * Placeholder implementation of wcscoll(). Attempts to use the single-byte 45fd4f1dd9STim J. Robbins * collation ordering where possible, and falls back on wcscmp() in locales 46fd4f1dd9STim J. Robbins * with extended character sets. 47fd4f1dd9STim J. Robbins */ 48fd4f1dd9STim J. Robbins int 49*3c87aa1dSDavid Chisnall wcscoll_l(const wchar_t *ws1, const wchar_t *ws2, locale_t locale) 50fd4f1dd9STim J. Robbins { 51fd4f1dd9STim J. Robbins char *mbs1, *mbs2; 52fd4f1dd9STim J. Robbins int diff, sverrno; 53*3c87aa1dSDavid Chisnall FIX_LOCALE(locale); 54*3c87aa1dSDavid Chisnall struct xlocale_collate *table = 55*3c87aa1dSDavid Chisnall (struct xlocale_collate*)locale->components[XLC_COLLATE]; 56fd4f1dd9STim J. Robbins 57*3c87aa1dSDavid Chisnall if (table->__collate_load_error || MB_CUR_MAX > 1) 58fd4f1dd9STim J. Robbins /* 59fd4f1dd9STim J. Robbins * Locale has no special collating order, could not be 60fd4f1dd9STim J. Robbins * loaded, or has an extended character set; do a fast binary 61fd4f1dd9STim J. Robbins * comparison. 62fd4f1dd9STim J. Robbins */ 63fd4f1dd9STim J. Robbins return (wcscmp(ws1, ws2)); 64fd4f1dd9STim J. Robbins 65fd4f1dd9STim J. Robbins if ((mbs1 = __mbsdup(ws1)) == NULL || (mbs2 = __mbsdup(ws2)) == NULL) { 66fd4f1dd9STim J. Robbins /* 67fd4f1dd9STim J. Robbins * Out of memory or illegal wide chars; fall back to wcscmp() 68fd4f1dd9STim J. Robbins * but leave errno indicating the error. Callers that don't 69fd4f1dd9STim J. Robbins * check for error will get a reasonable but often slightly 70fd4f1dd9STim J. Robbins * incorrect result. 71fd4f1dd9STim J. Robbins */ 72fd4f1dd9STim J. Robbins sverrno = errno; 73fd4f1dd9STim J. Robbins free(mbs1); 74fd4f1dd9STim J. Robbins errno = sverrno; 75fd4f1dd9STim J. Robbins return (wcscmp(ws1, ws2)); 76fd4f1dd9STim J. Robbins } 77fd4f1dd9STim J. Robbins 78*3c87aa1dSDavid Chisnall diff = strcoll_l(mbs1, mbs2, locale); 79fd4f1dd9STim J. Robbins sverrno = errno; 80fd4f1dd9STim J. Robbins free(mbs1); 81fd4f1dd9STim J. Robbins free(mbs2); 82fd4f1dd9STim J. Robbins errno = sverrno; 83fd4f1dd9STim J. Robbins 84fd4f1dd9STim J. Robbins return (diff); 85fd4f1dd9STim J. Robbins } 86fd4f1dd9STim J. Robbins 87*3c87aa1dSDavid Chisnall int 88*3c87aa1dSDavid Chisnall wcscoll(const wchar_t *ws1, const wchar_t *ws2) 89*3c87aa1dSDavid Chisnall { 90*3c87aa1dSDavid Chisnall return wcscoll_l(ws1, ws2, __get_locale()); 91*3c87aa1dSDavid Chisnall } 92*3c87aa1dSDavid Chisnall 93fd4f1dd9STim J. Robbins static char * 94fd4f1dd9STim J. Robbins __mbsdup(const wchar_t *ws) 95fd4f1dd9STim J. Robbins { 96dc763237STim J. Robbins static const mbstate_t initial; 97dc763237STim J. Robbins mbstate_t st; 98fd4f1dd9STim J. Robbins const wchar_t *wcp; 99fd4f1dd9STim J. Robbins size_t len; 100fd4f1dd9STim J. Robbins char *mbs; 101fd4f1dd9STim J. Robbins 102fd4f1dd9STim J. Robbins wcp = ws; 103dc763237STim J. Robbins st = initial; 104dc763237STim J. Robbins if ((len = wcsrtombs(NULL, &wcp, 0, &st)) == (size_t)-1) 105fd4f1dd9STim J. Robbins return (NULL); 106fd4f1dd9STim J. Robbins if ((mbs = malloc(len + 1)) == NULL) 107fd4f1dd9STim J. Robbins return (NULL); 108dc763237STim J. Robbins st = initial; 109dc763237STim J. Robbins wcsrtombs(mbs, &ws, len + 1, &st); 110fd4f1dd9STim J. Robbins 111fd4f1dd9STim J. Robbins return (mbs); 112fd4f1dd9STim J. Robbins } 113