fchmod-tests, fchmodat tests, lchmod tests: Add more tests.
[gnulib.git] / lib / wcsxfrm-impl.h
blob2377cb2acd7b321765f15d7378a119f3531ad72f
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.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 size_t
19 wcsxfrm (wchar_t *s1, const wchar_t *s2, size_t n)
21 char mbbuf2[1024];
22 char *mbs2;
25 int saved_errno = errno;
26 size_t result;
28 /* Convert s2 to a multibyte string, trying to avoid malloc(). */
30 size_t ret;
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
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 ();
51 /* Transform the multibyte string. */
52 errno = 0;
53 result = strxfrm ((char *)s1, mbs2, n);
54 if (errno != 0)
56 /* An error occurred. */
57 if (mbs2 != mbbuf2)
59 saved_errno = errno;
60 free (mbs2);
61 errno = saved_errno;
63 return 0;
66 if (result < n)
68 /* Convert the result by mapping char[] -> wchar_t[].
69 Since strcmp() compares the elements as 'unsigned char' values,
70 whereas wcscmp() compares the elements as 'wchar_t' values, we need
71 to map 1 'unsigned char' to 1 'wchar_t'. (We could also map 2
72 consecutive 'unsigned char' values to 1 'wchar_t' value, but this is
73 not needed. */
74 wchar_t *wcp = s1 + n;
75 char *cp = (char *)s1 + n;
77 while (wcp > s1)
78 *--wcp = (wchar_t) (unsigned char) *--cp;
81 if (mbs2 != mbbuf2)
82 free (mbs2);
84 /* No error. */
85 errno = saved_errno;
86 return result;
89 out_of_memory:
90 errno = ENOMEM;
91 return 0;
93 failed:
94 errno = EILSEQ;
95 return 0;