*** empty log message ***
[gnulib.git] / lib / xmemcoll.c
1 /* Locale-specific memory comparison.
2    Copyright (C) 2002 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 /* Contributed by Paul Eggert <eggert@twinsun.com>.  */
19
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <errno.h>
25 #ifndef errno
26 extern int errno;
27 #endif
28
29 #include <stdlib.h>
30
31 #if ENABLE_NLS
32 # include <libintl.h>
33 # define _(Text) gettext (Text)
34 #else
35 # define _(Text) Text
36 #endif
37
38 #include "error.h"
39 #include "memcoll.h"
40 #include "quotearg.h"
41 #include "xmemcoll.h"
42
43 /* Exit value when xmemcoll fails.
44    The caller may set it to some other value.  */
45 int xmemcoll_exit_failure = EXIT_FAILURE;
46
47 /* Compare S1 (with length S1LEN) and S2 (with length S2LEN) according
48    to the LC_COLLATE locale.  S1 and S2 do not overlap, and are not
49    adjacent.  Temporarily modify the bytes after S1 and S2, but
50    restore their original contents before returning.  Report an error
51    and exit if there is an error.  */
52
53 int
54 xmemcoll (char *s1, size_t s1len, char *s2, size_t s2len)
55 {
56   int diff = memcoll (s1, s1len, s2, s2len);
57   int collation_errno = errno;
58
59   if (collation_errno)
60     {
61       error (0, collation_errno, _("string comparison failed"));
62       error (0, 0, _("Set LC_ALL='C' to work around the problem."));
63       error (xmemcoll_exit_failure, 0,
64              _("The strings compared were %s and %s."),
65              quotearg_n_style_mem (0, locale_quoting_style, s1, s1len),
66              quotearg_n_style_mem (1, locale_quoting_style, s2, s2len));
67     }
68
69   return diff;
70 }