Tizen 2.0 Release
[external/tizen-coreutils.git] / lib / xmemxfrm.c
1 /* Locale-specific memory transformation
2
3    Copyright (C) 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 /* Written by Paul Eggert <eggert@cs.ucla.edu>.  */
20
21 #include <config.h>
22
23 #include "xmemxfrm.h"
24
25 #include <errno.h>
26 #include <stdlib.h>
27
28 #include "gettext.h"
29 #define _(msgid) gettext (msgid)
30
31 #include "error.h"
32 #include "exitfail.h"
33 #include "memxfrm.h"
34 #include "quotearg.h"
35
36 /* Store into DEST (of size DESTSIZE) the text in SRC (of size
37    SRCSIZE) transformed so that the result of memcmp on two
38    transformed texts (with ties going to the longer text) is the same
39    as the result of memcoll on the two texts before their
40    transformation.  Perhaps temporarily modify the byte after SRC, but
41    restore its original contents before returning.
42
43    Return the size of the resulting text.  DEST contains an
44    indeterminate value if the resulting size is greater than DESTSIZE.
45    Report an error and exit if there is an error.  */
46
47 size_t
48 xmemxfrm (char *restrict dest, size_t destsize,
49           char *restrict src, size_t srcsize)
50 {
51   size_t translated_size = memxfrm (dest, destsize, src, srcsize);
52
53   if (errno)
54     {
55       error (0, errno, _("string transformation failed"));
56       error (0, 0, _("Set LC_ALL='C' to work around the problem."));
57       error (exit_failure, 0,
58              _("The untransformed string was %s."),
59              quotearg_n_style_mem (0, locale_quoting_style, src, srcsize));
60     }
61
62   return translated_size;
63 }