1 /* fnxform - use iconv(3) to transform strings to and from "filename" format */
3 /* Copyright (C) 2009 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
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.
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.
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/>.
22 #if defined (HAVE_UNISTD_H)
27 #include "bashtypes.h"
33 #if defined (HAVE_ICONV)
37 #if defined (HAVE_LOCALE_CHARSET)
38 extern const char *locale_charset __P((void));
40 extern char *get_locale_var __P((char *));
43 #if defined (HAVE_ICONV)
44 static iconv_t conv_fromfs = (iconv_t)-1;
45 static iconv_t conv_tofs = (iconv_t)-1;
47 #define OUTLEN_MAX 4096
49 static char *outbuf = 0;
50 static size_t outlen = 0;
52 static char *curencoding __P((void));
53 static void init_tofs __P((void));
54 static void init_fromfs __P((void));
60 #if defined (HAVE_LOCALE_CHARSET)
61 loc = (char *)locale_charset ();
66 loc = get_locale_var ("LC_CTYPE");
67 if (loc == 0 || *loc == 0)
69 dot = strchr (loc, '.');
72 mod = strchr (dot, '@');
85 conv_tofs = iconv_open ("UTF-8-MAC", cur);
94 conv_fromfs = iconv_open (cur, "UTF-8-MAC");
98 fnx_tofs (string, len)
103 ICONV_CONST char *inbuf;
107 if (conv_tofs == (iconv_t)-1)
109 if (conv_tofs == (iconv_t)-1)
112 /* Free and reallocate outbuf if it's *too* big */
113 if (outlen >= OUTLEN_MAX && len < OUTLEN_MAX - 8)
121 if (outbuf == 0 || outlen < len + 8)
124 outbuf = outbuf ? xrealloc (outbuf, outlen + 1) : xmalloc (outlen + 1);
129 iconv (conv_tofs, NULL, NULL, NULL, NULL);
131 if (iconv (conv_tofs, &inbuf, &len, &tempbuf, &templen) == (size_t)-1)
142 fnx_fromfs (string, len)
147 ICONV_CONST char *inbuf;
151 if (conv_fromfs == (iconv_t)-1)
153 if (conv_fromfs == (iconv_t)-1)
156 /* Free and reallocate outbuf if it's *too* big */
157 if (outlen >= OUTLEN_MAX && len < OUTLEN_MAX - 8)
165 if (outbuf == 0 || outlen < (len + 8))
168 outbuf = outbuf ? xrealloc (outbuf, outlen + 1) : xmalloc (outlen + 1);
173 iconv (conv_fromfs, NULL, NULL, NULL, NULL);
175 if (iconv (conv_fromfs, &inbuf, &len, &tempbuf, &templen) == (size_t)-1)