blob: e0eae5dcc74ecf14fdaadb1f774b73ad44d389ea [file] [log] [blame]
Jari Aalto31859422009-01-12 13:36:28 +00001/* mbscmp - multibyte string comparison. */
2
3/* Copyright (C) 1995 Free Software Foundation, Inc.
4
5 This file is part of GNU Bash, the Bourne Again SHell.
6
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#include <config.h>
22
23#if !defined (HAVE_MBSCMP) && defined (HANDLE_MULTIBYTE)
24
25#include <stdlib.h>
26#include <stddef.h>
Chet Ramey00018032011-11-21 20:51:19 -050027#include <string.h>
Jari Aalto31859422009-01-12 13:36:28 +000028
29/* Compare MBS1 and MBS2. */
30int
31mbscmp (mbs1, mbs2)
32 const char *mbs1;
33 const char *mbs2;
34{
35 int len1, len2;
36 wchar_t c1, c2;
37
38 len1 = len2 = 0;
39 /* Reset multibyte characters to their initial state. */
40 (void) mblen ((char *) NULL, 0);
41
42 do
43 {
Chet Ramey00018032011-11-21 20:51:19 -050044 len1 = mbtowc (&c1, mbs1, MB_CUR_MAX);
45 len2 = mbtowc (&c2, mbs2, MB_CUR_MAX);
Jari Aalto31859422009-01-12 13:36:28 +000046
47 if (len1 == 0)
48 return len2 == 0 ? 0 : -1;
Chet Ramey00018032011-11-21 20:51:19 -050049 else if (len2 == 0)
Jari Aalto31859422009-01-12 13:36:28 +000050 return 1;
Chet Ramey00018032011-11-21 20:51:19 -050051 else if (len1 > 0 && len2 < 0)
52 return -1;
53 else if (len1 < 0 && len2 > 0)
54 return 1;
55 else if (len1 < 0 && len2 < 0)
56 {
57 len1 = strlen (mbs1);
58 len2 = strlen (mbs2);
59 return (len1 == len2 ? memcmp (mbs1, mbs2, len1)
60 : ((len1 < len2) ? (memcmp (mbs1, mbs2, len1) > 0 ? 1 : -1)
61 : (memcmp (mbs1, mbs2, len2) >= 0 ? 1 : -1)));
62 }
Jari Aalto31859422009-01-12 13:36:28 +000063
64 mbs1 += len1;
65 mbs2 += len2;
66 }
67 while (c1 == c2);
68
69 return c1 - c2;
70}
71
72#endif