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