/*-
- * Copyright (c) 2002 Tim J. Robbins.
+ * Copyright (c) 2002, 2003 Tim J. Robbins.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
*/
#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/lib/libc/locale/btowc.c,v 1.1 2002/08/03 13:49:55 tjr Exp $");
+__FBSDID("$FreeBSD: src/lib/libc/locale/btowc.c,v 1.4 2004/05/12 14:26:54 tjr Exp $");
-#include <rune.h>
+#include "xlocale_private.h"
+
+#include <stdio.h>
#include <wchar.h>
+#include "mblocal.h"
wint_t
-btowc(int c)
+btowc_l(int c, locale_t loc)
{
- rune_t r;
+ static const mbstate_t initial;
+ mbstate_t mbs = initial;
char cc;
+ wchar_t wc;
+ NORMALIZE_LOCALE(loc);
if (c == EOF)
return (WEOF);
+ /*
+ * We expect mbrtowc() to return 0 or 1, hence the check for n > 1
+ * which detects error return values as well as "impossible" byte
+ * counts.
+ */
cc = (char)c;
- if ((r = sgetrune(&cc, 1, NULL)) == _INVALID_RUNE)
+ if (loc->__lc_ctype->__mbrtowc(&wc, &cc, 1, &mbs, loc) > 1)
return (WEOF);
- return (r);
+ return (wc);
+}
+
+wint_t
+btowc(int c)
+{
+ return btowc_l(c, __current_locale());
}