changeset 12784:1c17fa7de7ee

2 We need a fully open libc (no libc_i18n) (fix build)
author Garrett D'Amore <garrett@nexenta.com>
date Fri, 30 Jul 2010 21:32:33 -0700
parents f95354c46591
children 5d30bc2ad659
files usr/src/lib/libc/port/locale/mbftowc.c
diffstat 1 files changed, 88 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/usr/src/lib/libc/port/locale/mbftowc.c	Fri Jul 30 21:32:33 2010 -0700
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
+ * Use is subject to license terms.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "lint.h"
+#include <stdlib.h>
+#include <string.h>
+#include <wchar.h>
+
+/*
+ * This function is apparently referenced by parts of ON.  It is
+ * not intended for public API usage -- and it is not documented.
+ *
+ * The usage appears to be to consume bytes until a character is
+ * gathered, using a supplied function.   It reads exactly one
+ * character and returns the number of bytes in the multibyte string
+ * that were consumed.
+ *
+ * The string "s" is storage for the multibyte string, the
+ * wc will receive the interpreted character, the peek function
+ * obtains the next character (as an int so we can get EOF),
+ * and errorc is stuffed with the character that is responsible
+ * for a parse error, if any.
+ */
+
+int
+_mbftowc(char *s, wchar_t *wc, int (*peek)(void), int *errorc)
+{
+	int		c;
+	mbstate_t	mbs;
+	char		*start = s;
+	int		cons = 0;
+
+	for (;;) {
+		c = peek();
+		if (c < 0) {
+			/* No bytes returned? */
+			return (s - start);
+		}
+
+		*s = (char)c;
+		s++;
+
+		(void) memset(&mbs, 0, sizeof (mbs));
+		cons = mbrtowc(wc, start, s - start, &mbs);
+		if (cons >= 0) {
+			/* fully translated character */
+			return (cons);
+		}
+		if (cons == -2) {
+			/* incomplete, recycle */
+			continue;
+		}
+
+		/*
+		 * Parse error, don't consider the first character part
+		 * of the error.
+		 */
+		s--;
+		cons = (s - start);
+		*errorc = c >= 0 ?  c : 0;
+		break;
+	}
+
+	return (cons);
+}