Merge commit '281819e5f8b19cd8627541a22d261906fd190276' into merges
[unleashed.git] / usr / src / lib / libc / port / locale / mbftowc.c
blob262e11ab1c65007067b6a3b389211c20c8505f59
1 /*
2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
5 * 1.0 of the CDDL.
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
13 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
16 #include "lint.h"
17 #include <stdlib.h>
18 #include <string.h>
19 #include <wchar.h>
22 * This function is apparently referenced by parts of ON. It is
23 * not intended for public API usage -- and it is not documented.
25 * The usage appears to be to consume bytes until a character is
26 * gathered, using a supplied function. It reads exactly one
27 * character and returns the number of bytes in the multibyte string
28 * that were consumed.
30 * The string "s" is storage for the multibyte string, the
31 * wc will receive the interpreted character, the peek function
32 * obtains the next character (as an int so we can get EOF),
33 * and errorc is stuffed with the character that is responsible
34 * for a parse error, if any.
37 int
38 _mbftowc(char *s, wchar_t *wc, int (*peek)(void), int *errorc)
40 int c;
41 mbstate_t mbs;
42 char *start = s;
43 size_t cons = 0;
45 for (;;) {
46 c = peek();
47 if (c < 0) {
48 /* No bytes returned? */
49 return (s - start);
52 *s = (char)c;
53 s++;
55 (void) memset(&mbs, 0, sizeof (mbs));
56 cons = mbrtowc(wc, start, s - start, &mbs);
57 if ((int)cons >= 0) {
58 /* fully translated character */
59 return (cons);
61 if (cons == (size_t)-2) {
62 /* incomplete, recycle */
63 continue;
67 * Parse error, don't consider the first character part
68 * of the error.
70 s--;
71 cons = (s - start);
72 *errorc = c >= 0 ? c : 0;
73 break;
76 return (cons);