14297a3b0SGarrett D'Amore /* 22d08521bSGarrett D'Amore * Copyright 2013 Garrett D'Amore <garrett@damore.org> 36b5e5868SGarrett D'Amore * Copyright 2010 Nexenta Systems, Inc. All rights reserved. 44297a3b0SGarrett D'Amore * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua> 54297a3b0SGarrett D'Amore * at Electronni Visti IA, Kiev, Ukraine. 64297a3b0SGarrett D'Amore * All rights reserved. 74297a3b0SGarrett D'Amore * 84297a3b0SGarrett D'Amore * Redistribution and use in source and binary forms, with or without 94297a3b0SGarrett D'Amore * modification, are permitted provided that the following conditions 104297a3b0SGarrett D'Amore * are met: 114297a3b0SGarrett D'Amore * 1. Redistributions of source code must retain the above copyright 124297a3b0SGarrett D'Amore * notice, this list of conditions and the following disclaimer. 134297a3b0SGarrett D'Amore * 2. Redistributions in binary form must reproduce the above copyright 144297a3b0SGarrett D'Amore * notice, this list of conditions and the following disclaimer in the 154297a3b0SGarrett D'Amore * documentation and/or other materials provided with the distribution. 164297a3b0SGarrett D'Amore * 174297a3b0SGarrett D'Amore * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 184297a3b0SGarrett D'Amore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 194297a3b0SGarrett D'Amore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 204297a3b0SGarrett D'Amore * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE 214297a3b0SGarrett D'Amore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 224297a3b0SGarrett D'Amore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 234297a3b0SGarrett D'Amore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 244297a3b0SGarrett D'Amore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 254297a3b0SGarrett D'Amore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 264297a3b0SGarrett D'Amore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 274297a3b0SGarrett D'Amore * SUCH DAMAGE. 284297a3b0SGarrett D'Amore */ 294297a3b0SGarrett D'Amore 304297a3b0SGarrett D'Amore #include "lint.h" 316b5e5868SGarrett D'Amore #include "file64.h" 326b5e5868SGarrett D'Amore #include <alloca.h> 334297a3b0SGarrett D'Amore #include <stdlib.h> 344297a3b0SGarrett D'Amore #include <string.h> 354297a3b0SGarrett D'Amore #include <errno.h> 366b5e5868SGarrett D'Amore #include <wchar.h> 372d08521bSGarrett D'Amore #include <xlocale.h> 382d08521bSGarrett D'Amore #include "localeimpl.h" 394297a3b0SGarrett D'Amore #include "collate.h" 404297a3b0SGarrett D'Amore 416b5e5868SGarrett D'Amore #define ALLOCA_LIMIT 16 424297a3b0SGarrett D'Amore 434297a3b0SGarrett D'Amore /* 446b5e5868SGarrett D'Amore * In order to properly handle multibyte locales, its easiet to just 456b5e5868SGarrett D'Amore * convert to wide characters and then use wcscoll. However if an 466b5e5868SGarrett D'Amore * error occurs, we gracefully fall back to simple strcmp. Caller 476b5e5868SGarrett D'Amore * should check errno. 484297a3b0SGarrett D'Amore */ 496b5e5868SGarrett D'Amore int 502d08521bSGarrett D'Amore strcoll_l(const char *s1, const char *s2, locale_t loc) 516b5e5868SGarrett D'Amore { 526b5e5868SGarrett D'Amore int ret; 536b5e5868SGarrett D'Amore wchar_t *t1 = NULL, *t2 = NULL; 546b5e5868SGarrett D'Amore wchar_t *w1 = NULL, *w2 = NULL; 556b5e5868SGarrett D'Amore size_t sz1, sz2; 562d08521bSGarrett D'Amore const struct lc_collate *lcc = loc->collate; 576b5e5868SGarrett D'Amore 582d08521bSGarrett D'Amore if (lcc->lc_is_posix) 592d08521bSGarrett D'Amore return (strcmp(s1, s2)); 606b5e5868SGarrett D'Amore 616b5e5868SGarrett D'Amore sz1 = strlen(s1) + 1; 626b5e5868SGarrett D'Amore sz2 = strlen(s2) + 1; 636b5e5868SGarrett D'Amore 646b5e5868SGarrett D'Amore /* 656b5e5868SGarrett D'Amore * Simple assumption: conversion to wide format is strictly 666b5e5868SGarrett D'Amore * reducing, i.e. a single byte (or multibyte character) 676b5e5868SGarrett D'Amore * cannot result in multiple wide characters. 686b5e5868SGarrett D'Amore * 696b5e5868SGarrett D'Amore * We gain a bit of performance by giving preference to alloca 706b5e5868SGarrett D'Amore * for small string allocations. 716b5e5868SGarrett D'Amore */ 726b5e5868SGarrett D'Amore if (sz1 > ALLOCA_LIMIT) { 736b5e5868SGarrett D'Amore if ((t1 = malloc(sz1 * sizeof (wchar_t))) == NULL) 746b5e5868SGarrett D'Amore goto error; 756b5e5868SGarrett D'Amore w1 = t1; 764297a3b0SGarrett D'Amore } else { 776b5e5868SGarrett D'Amore if ((w1 = alloca(sz1 * sizeof (wchar_t))) == NULL) 786b5e5868SGarrett D'Amore goto error; 794297a3b0SGarrett D'Amore } 806b5e5868SGarrett D'Amore if (sz2 > ALLOCA_LIMIT) { 816b5e5868SGarrett D'Amore if ((t2 = malloc(sz2 * sizeof (wchar_t))) == NULL) 826b5e5868SGarrett D'Amore goto error; 836b5e5868SGarrett D'Amore w2 = t2; 846b5e5868SGarrett D'Amore } else { 856b5e5868SGarrett D'Amore if ((w2 = alloca(sz2 * sizeof (wchar_t))) == NULL) 866b5e5868SGarrett D'Amore goto error; 874297a3b0SGarrett D'Amore } 886b5e5868SGarrett D'Amore 89*83ff55dcSRichard Lowe if ((mbstowcs_l(w1, s1, sz1, loc)) == (size_t)-1) 906b5e5868SGarrett D'Amore goto error; 916b5e5868SGarrett D'Amore 92*83ff55dcSRichard Lowe if ((mbstowcs_l(w2, s2, sz2, loc)) == (size_t)-1) 936b5e5868SGarrett D'Amore goto error; 946b5e5868SGarrett D'Amore 952d08521bSGarrett D'Amore ret = wcscoll_l(w1, w2, loc); 966b5e5868SGarrett D'Amore if (t1) 976b5e5868SGarrett D'Amore free(t1); 986b5e5868SGarrett D'Amore if (t2) 996b5e5868SGarrett D'Amore free(t2); 1004297a3b0SGarrett D'Amore 1014297a3b0SGarrett D'Amore return (ret); 1026b5e5868SGarrett D'Amore 1036b5e5868SGarrett D'Amore error: 1046b5e5868SGarrett D'Amore if (t1) 1056b5e5868SGarrett D'Amore free(t1); 1066b5e5868SGarrett D'Amore if (t2) 1076b5e5868SGarrett D'Amore free(t2); 1086b5e5868SGarrett D'Amore return (strcmp(s1, s2)); 1094297a3b0SGarrett D'Amore } 1102d08521bSGarrett D'Amore 1112d08521bSGarrett D'Amore int 1122d08521bSGarrett D'Amore strcoll(const char *s1, const char *s2) 1132d08521bSGarrett D'Amore { 1142d08521bSGarrett D'Amore return (strcoll_l(s1, s2, uselocale(NULL))); 1152d08521bSGarrett D'Amore } 116