1 /* xmbsrtowcs.c -- replacement function for mbsrtowcs */
3 /* Copyright (C) 2002 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 it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License along
18 with Bash; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
24 /* <wchar.h>, <wctype.h> and <stdlib.h> are included in "shmbutil.h".
25 If <wchar.h>, <wctype.h>, mbsrtowcs(), exist, HANDLE_MULTIBYTE
30 /* On some locales (ex. ja_JP.sjis), mbsrtowc doesn't convert 0x5c to U<0x5c>.
31 So, this function is made for converting 0x5c to U<0x5c>. */
33 static mbstate_t local_state;
34 static int local_state_use = 0;
37 xmbsrtowcs (dest, src, len, pstate)
44 size_t mblength, wclength, n;
51 memset (&local_state, '\0', sizeof(mbstate_t));
62 char *mbsbuf, *mbsbuf_top;
65 wsbuf = (wchar_t *) malloc ((n + 1) * sizeof(wchar_t));
66 mbsbuf_top = mbsbuf = (char *) malloc (n + 1);
67 memcpy(mbsbuf, *src, n + 1);
70 wclength = mbsrtowcs (wsbuf, (const char **)&mbsbuf, n, &psbuf);
77 for(wclength = 0; wclength < len; wclength++, dest++)
87 else if (**src == '\\')
93 mblength = mbrtowc(dest, *src, n, ps);
96 mblength = mbrtowc(dest, *src, n, ps);
98 /* Cannot convert multibyte character to wide character. */
99 if (mblength == (size_t)-1 || mblength == (size_t)-2)
105 /* The multibyte string has been completely converted,
106 including the terminating '\0'. */
116 #endif /* HANDLE_MULTIBYTE */