1 /* Transform wide string for comparison using the current locale. 2 Copyright (C) 2011-2021 Free Software Foundation, Inc. 3 Written by Bruno Haible <bruno@clisp.org>, 2011. 4 5 This file is free software: you can redistribute it and/or modify 6 it under the terms of the GNU Lesser General Public License as 7 published by the Free Software Foundation; either version 3 of the 8 License, or (at your option) any later version. 9 10 This file is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public License 16 along with this program. If not, see <https://www.gnu.org/licenses/>. */ 17 18 size_t 19 wcsxfrm (wchar_t *s1, const wchar_t *s2, size_t n) /* */ 20 { 21 char mbbuf2[1024]; 22 char *mbs2; 23 24 { 25 int saved_errno = errno; 26 size_t result; 27 28 /* Convert s2 to a multibyte string, trying to avoid malloc(). */ 29 { 30 size_t ret; 31 32 ret = wcstombs (mbbuf2, s2, sizeof (mbbuf2)); 33 if (ret == (size_t)-1) 34 goto failed; 35 if (ret < sizeof (mbbuf2)) 36 mbs2 = mbbuf2; 37 else 38 { 39 size_t need = wcstombs (NULL, s2, 0); 40 if (need == (size_t)-1) 41 goto failed; 42 mbs2 = (char *) malloc (need + 1); 43 if (mbs2 == NULL) 44 goto out_of_memory; 45 ret = wcstombs (mbs2, s2, need + 1); 46 if (ret != need) 47 abort (); 48 } 49 } 50 51 /* Transform the multibyte string. */ 52 errno = 0; 53 result = strxfrm ((char *)s1, mbs2, n); 54 if (errno != 0) 55 { 56 /* An error occurred. */ 57 if (mbs2 != mbbuf2) 58 free (mbs2); 59 return 0; 60 } 61 62 if (result < n) 63 { 64 /* Convert the result by mapping char[] -> wchar_t[]. 65 Since strcmp() compares the elements as 'unsigned char' values, 66 whereas wcscmp() compares the elements as 'wchar_t' values, we need 67 to map 1 'unsigned char' to 1 'wchar_t'. (We could also map 2 68 consecutive 'unsigned char' values to 1 'wchar_t' value, but this is 69 not needed. */ 70 wchar_t *wcp = s1 + n; 71 char *cp = (char *)s1 + n; 72 73 while (wcp > s1) 74 *--wcp = (wchar_t) (unsigned char) *--cp; 75 } 76 77 if (mbs2 != mbbuf2) 78 free (mbs2); 79 80 /* No error. */ 81 errno = saved_errno; 82 return result; 83 } 84 85 out_of_memory: 86 errno = ENOMEM; 87 return 0; 88 89 failed: 90 errno = EILSEQ; 91 return 0; 92 }