Tizen 2.0 Release
[external/tizen-coreutils.git] / lib / xmemcoll.c
1 /* Locale-specific memory comparison.
2
3    Copyright (C) 2002, 2003, 2004, 2006 Free Software Foundation, Inc.
4
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 2, or (at your option)
8    any later version.
9
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.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 /* Contributed by Paul Eggert <eggert@twinsun.com>.  */
20
21 #include <config.h>
22
23 #include <errno.h>
24 #include <stdlib.h>
25
26 #include "gettext.h"
27 #define _(msgid) gettext (msgid)
28
29 #include "error.h"
30 #include "exitfail.h"
31 #include "memcoll.h"
32 #include "quotearg.h"
33 #include "xmemcoll.h"
34
35 /* Compare S1 (with length S1LEN) and S2 (with length S2LEN) according
36    to the LC_COLLATE locale.  S1 and S2 do not overlap, and are not
37    adjacent.  Temporarily modify the bytes after S1 and S2, but
38    restore their original contents before returning.  Report an error
39    and exit if there is an error.  */
40
41 int
42 xmemcoll (char *s1, size_t s1len, char *s2, size_t s2len)
43 {
44   int diff = memcoll (s1, s1len, s2, s2len);
45   int collation_errno = errno;
46
47   if (collation_errno)
48     {
49       error (0, collation_errno, _("string comparison failed"));
50       error (0, 0, _("Set LC_ALL='C' to work around the problem."));
51       error (exit_failure, 0,
52              _("The strings compared were %s and %s."),
53              quotearg_n_style_mem (0, locale_quoting_style, s1, s1len),
54              quotearg_n_style_mem (1, locale_quoting_style, s2, s2len));
55     }
56
57   return diff;
58 }