about summary refs log tree commit diff
path: root/wcsmbs/wctob.c
diff options
context:
space:
mode:
Diffstat (limited to 'wcsmbs/wctob.c')
-rw-r--r--wcsmbs/wctob.c41
1 files changed, 37 insertions, 4 deletions
diff --git a/wcsmbs/wctob.c b/wcsmbs/wctob.c
index ce1063a1ec..0461877d32 100644
--- a/wcsmbs/wctob.c
+++ b/wcsmbs/wctob.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
+/* Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1996.
 
@@ -17,15 +17,48 @@
    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.  */
 
+#include <gconv.h>
 #include <stdio.h>
+#include <string.h>
 #include <wchar.h>
+#include <wcsmbsload.h>
 
 
-/* We use UTF8 encoding for multibyte strings and therefore a valid
-   one byte multibyte string only can have a value from 0 to 0x7f.  */
 int
 wctob (c)
      wint_t c;
 {
-  return (c >= 0 && c <= 0x7f) ? c : EOF;
+  char buf[MB_LEN_MAX];
+  struct gconv_step_data data;
+  wchar_t inbuf[1];
+  size_t inbytes;
+  size_t converted;
+  int status;
+
+  /* Tell where we want the result.  */
+  data.outbuf = (char *) buf;
+  data.outbufavail = 0;
+  data.outbufsize = MB_LEN_MAX;
+  data.is_last = 1;
+  data.statep = &data.__state;
+
+  /* Make sure we start in the initial state.  */
+  memset (&data.__state, '\0', sizeof (mbstate_t));
+
+  /* Make sure we use the correct function.  */
+  update_conversion_ptrs ();
+
+  /* Create the input string.  */
+  inbuf[0] = c;
+  inbytes = sizeof (wchar_t);
+
+  status = (*__wcsmbs_gconv_fcts.tomb->fct) (__wcsmbs_gconv_fcts.tomb, &data,
+					     (const char *) inbuf, &inbytes,
+					     &converted, 0);
+  /* The conversion failed or the output is too long.  */
+  if (status != GCONV_OK && status != GCONV_FULL_OUTPUT
+      || data.outbufavail != 1)
+    return WEOF;
+
+  return buf[0];
 }