Fix handling of collating elements in fnmatch (bug 17396, bug 16976)
[platform/upstream/glibc.git] / wcsmbs / wmemcmp.c
1 /* Copyright (C) 1996-2019 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1996.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library 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 GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <wchar.h>
20
21 #ifdef WMEMCMP
22 # define __wmemcmp WMEMCMP
23 #endif
24
25 int
26 __wmemcmp (const wchar_t *s1, const wchar_t *s2, size_t n)
27 {
28   wchar_t c1;
29   wchar_t c2;
30
31   while (n >= 4)
32     {
33       c1 = s1[0];
34       c2 = s2[0];
35       if (c1 - c2 != 0)
36         return c1 > c2 ? 1 : -1;
37       c1 = s1[1];
38       c2 = s2[1];
39       if (c1 - c2 != 0)
40         return c1 > c2 ? 1 : -1;
41       c1 = s1[2];
42       c2 = s2[2];
43       if (c1 - c2 != 0)
44         return c1 > c2 ? 1 : -1;
45       c1 = s1[3];
46       c2 = s2[3];
47       if (c1 - c2 != 0)
48         return c1 > c2 ? 1 : -1;
49       s1 += 4;
50       s2 += 4;
51       n -= 4;
52     }
53
54   if (n > 0)
55     {
56       c1 = s1[0];
57       c2 = s2[0];
58       if (c1 - c2 != 0)
59         return c1 > c2 ? 1 : -1;
60       ++s1;
61       ++s2;
62       --n;
63     }
64   if (n > 0)
65     {
66       c1 = s1[0];
67       c2 = s2[0];
68       if (c1 - c2 != 0)
69         return c1 > c2 ? 1 : -1;
70       ++s1;
71       ++s2;
72       --n;
73     }
74   if (n > 0)
75     {
76       c1 = s1[0];
77       c2 = s2[0];
78       if (c1 - c2 != 0)
79         return c1 > c2 ? 1 : -1;
80     }
81
82   return 0;
83 }
84 #ifndef WMEMCMP
85 weak_alias (__wmemcmp, wmemcmp)
86 #endif