about summary refs log tree commit diff
path: root/wcsmbs/btowc.c
diff options
context:
space:
mode:
Diffstat (limited to 'wcsmbs/btowc.c')
-rw-r--r--wcsmbs/btowc.c46
1 files changed, 40 insertions, 6 deletions
diff --git a/wcsmbs/btowc.c b/wcsmbs/btowc.c
index 4c2f9df215..268b1f2378 100644
--- a/wcsmbs/btowc.c
+++ b/wcsmbs/btowc.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>
 
@@ -17,18 +17,52 @@
    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.  */
 wint_t
 btowc (c)
      int c;
 {
-  if (WEOF != (wint_t) EOF || c < 0 || c > 0x7f)
+  char buf[sizeof (wchar_t)];
+  struct gconv_step_data data;
+  char inbuf[1];
+  size_t inbytes;
+  size_t converted;
+  int status;
+
+  /* If the parameter does not fit into one byte or it is the EOF value
+     we can give the answer now.  */
+  if (c < -128 || c > 127 || c == EOF)
+    return WEOF;
+
+  /* Tell where we want the result.  */
+  data.outbuf = (char *) buf;
+  data.outbufavail = 0;
+  data.outbufsize = sizeof (wchar_t);
+  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 = 1;
+
+  status = (*__wcsmbs_gconv_fcts.towc->fct) (__wcsmbs_gconv_fcts.towc,
+					     &data, inbuf, &inbytes,
+					     &converted, 0);
+  /* The conversion failed.  */
+  if (status != GCONV_OK && status != GCONV_FULL_OUTPUT)
     return WEOF;
-  else
-    return (wint_t) c;
+
+  return *(wchar_t *)buf;
 }