xref: /freebsd/lib/libc/string/strcoll.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
158f0484fSRodney W. Grimes /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni  *
42a6abeebSBaptiste Daroussin  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
5a4d5d0cbSAndrey A. Chernov  * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
6a4d5d0cbSAndrey A. Chernov  *		at Electronni Visti IA, Kiev, Ukraine.
7a4d5d0cbSAndrey A. Chernov  *			All rights reserved.
858f0484fSRodney W. Grimes  *
93c87aa1dSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
105b5fa75aSEd Maste  *
113c87aa1dSDavid Chisnall  * Portions of this software were developed by David Chisnall
123c87aa1dSDavid Chisnall  * under sponsorship from the FreeBSD Foundation.
133c87aa1dSDavid Chisnall  *
1458f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1558f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1658f0484fSRodney W. Grimes  * are met:
1758f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1858f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1958f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
2058f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
2158f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
2258f0484fSRodney W. Grimes  *
23a4d5d0cbSAndrey A. Chernov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
2458f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2558f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26a4d5d0cbSAndrey A. Chernov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
2758f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2858f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2958f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3058f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3158f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3258f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3358f0484fSRodney W. Grimes  * SUCH DAMAGE.
3458f0484fSRodney W. Grimes  */
3558f0484fSRodney W. Grimes 
36a4d5d0cbSAndrey A. Chernov #include <stdlib.h>
3758f0484fSRodney W. Grimes #include <string.h>
382a6abeebSBaptiste Daroussin #include <errno.h>
392a6abeebSBaptiste Daroussin #include <wchar.h>
40a4d5d0cbSAndrey A. Chernov #include "collate.h"
4158f0484fSRodney W. Grimes 
423c87aa1dSDavid Chisnall 
432a6abeebSBaptiste Daroussin /*
44c71b5482SBaptiste Daroussin  * In order to properly handle multibyte locales, its easiest to just
452a6abeebSBaptiste Daroussin  * convert to wide characters and then use wcscoll.  However if an
462a6abeebSBaptiste Daroussin  * error occurs, we gracefully fall back to simple strcmp.  Caller
472a6abeebSBaptiste Daroussin  * should check errno.
482a6abeebSBaptiste Daroussin  */
4958f0484fSRodney W. Grimes int
strcoll_l(const char * s,const char * s2,locale_t locale)500f701093SEitan Adler strcoll_l(const char *s, const char *s2, locale_t locale)
5158f0484fSRodney W. Grimes {
522a6abeebSBaptiste Daroussin 	int ret;
532a6abeebSBaptiste Daroussin 	wchar_t *t1 = NULL, *t2 = NULL;
542a6abeebSBaptiste Daroussin 	wchar_t *w1 = NULL, *w2 = NULL;
552a6abeebSBaptiste Daroussin 	const char *cs1, *cs2;
562a6abeebSBaptiste Daroussin 	mbstate_t mbs1;
572a6abeebSBaptiste Daroussin 	mbstate_t mbs2;
582a6abeebSBaptiste Daroussin 	size_t sz1, sz2;
592a6abeebSBaptiste Daroussin 
602a6abeebSBaptiste Daroussin 	memset(&mbs1, 0, sizeof (mbstate_t));
612a6abeebSBaptiste Daroussin 	memset(&mbs2, 0, sizeof (mbstate_t));
622a6abeebSBaptiste Daroussin 
632a6abeebSBaptiste Daroussin 	/*
642a6abeebSBaptiste Daroussin 	 * The mbsrtowcs_l function can set the src pointer to null upon
652a6abeebSBaptiste Daroussin 	 * failure, so it should act on a copy to avoid:
662a6abeebSBaptiste Daroussin 	 *   - sending null pointer to strcmp
672a6abeebSBaptiste Daroussin 	 *   - having strcoll/strcoll_l change *s or *s2 to null
682a6abeebSBaptiste Daroussin 	 */
692a6abeebSBaptiste Daroussin 	cs1 = s;
702a6abeebSBaptiste Daroussin 	cs2 = s2;
712a6abeebSBaptiste Daroussin 
723c87aa1dSDavid Chisnall 	FIX_LOCALE(locale);
733c87aa1dSDavid Chisnall 	struct xlocale_collate *table =
743c87aa1dSDavid Chisnall 		(struct xlocale_collate*)locale->components[XLC_COLLATE];
75a4d5d0cbSAndrey A. Chernov 
763c87aa1dSDavid Chisnall 	if (table->__collate_load_error)
772a6abeebSBaptiste Daroussin 		goto error;
78a4d5d0cbSAndrey A. Chernov 
792a6abeebSBaptiste Daroussin 	sz1 = strlen(s) + 1;
802a6abeebSBaptiste Daroussin 	sz2 = strlen(s2) + 1;
81a4d5d0cbSAndrey A. Chernov 
822a6abeebSBaptiste Daroussin 	/*
832a6abeebSBaptiste Daroussin 	 * Simple assumption: conversion to wide format is strictly
842a6abeebSBaptiste Daroussin 	 * reducing, i.e. a single byte (or multibyte character)
852a6abeebSBaptiste Daroussin 	 * cannot result in multiple wide characters.
862a6abeebSBaptiste Daroussin 	 */
872a6abeebSBaptiste Daroussin 	if ((t1 = malloc(sz1 * sizeof (wchar_t))) == NULL)
882a6abeebSBaptiste Daroussin 		goto error;
892a6abeebSBaptiste Daroussin 	w1 = t1;
902a6abeebSBaptiste Daroussin 	if ((t2 = malloc(sz2 * sizeof (wchar_t))) == NULL)
912a6abeebSBaptiste Daroussin 		goto error;
922a6abeebSBaptiste Daroussin 	w2 = t2;
932a6abeebSBaptiste Daroussin 
942a6abeebSBaptiste Daroussin 	if ((mbsrtowcs_l(w1, &cs1, sz1, &mbs1, locale)) == (size_t)-1)
952a6abeebSBaptiste Daroussin 		goto error;
962a6abeebSBaptiste Daroussin 
972a6abeebSBaptiste Daroussin 	if ((mbsrtowcs_l(w2, &cs2, sz2, &mbs2, locale)) == (size_t)-1)
982a6abeebSBaptiste Daroussin 		goto error;
992a6abeebSBaptiste Daroussin 
1002a6abeebSBaptiste Daroussin 	ret = wcscoll_l(w1, w2, locale);
1012a6abeebSBaptiste Daroussin 	free(t1);
1022a6abeebSBaptiste Daroussin 	free(t2);
1032a6abeebSBaptiste Daroussin 
1042a6abeebSBaptiste Daroussin 	return (ret);
1052a6abeebSBaptiste Daroussin 
1062a6abeebSBaptiste Daroussin error:
1072a6abeebSBaptiste Daroussin 	free(t1);
1082a6abeebSBaptiste Daroussin 	free(t2);
1092a6abeebSBaptiste Daroussin 	return (strcmp(s, s2));
11058f0484fSRodney W. Grimes }
1113c87aa1dSDavid Chisnall 
1123c87aa1dSDavid Chisnall int
strcoll(const char * s,const char * s2)1130f701093SEitan Adler strcoll(const char *s, const char *s2)
1143c87aa1dSDavid Chisnall {
1150f701093SEitan Adler 	return strcoll_l(s, s2, __get_locale());
1163c87aa1dSDavid Chisnall }
1173c87aa1dSDavid Chisnall 
118